14c7070dbSScott Long /*- 27b610b60SSean Bruno * Copyright (c) 2014-2018, Matthew Macy <mmacy@mattmacy.io> 34c7070dbSScott Long * All rights reserved. 44c7070dbSScott Long * 54c7070dbSScott Long * Redistribution and use in source and binary forms, with or without 64c7070dbSScott Long * modification, are permitted provided that the following conditions are met: 74c7070dbSScott Long * 84c7070dbSScott Long * 1. Redistributions of source code must retain the above copyright notice, 94c7070dbSScott Long * this list of conditions and the following disclaimer. 104c7070dbSScott Long * 114c7070dbSScott Long * 2. Neither the name of Matthew Macy nor the names of its 124c7070dbSScott Long * contributors may be used to endorse or promote products derived from 134c7070dbSScott Long * this software without specific prior written permission. 144c7070dbSScott Long * 154c7070dbSScott Long * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 164c7070dbSScott Long * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 174c7070dbSScott Long * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 184c7070dbSScott Long * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 194c7070dbSScott Long * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 204c7070dbSScott Long * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 214c7070dbSScott Long * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 224c7070dbSScott Long * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 234c7070dbSScott Long * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 244c7070dbSScott Long * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 254c7070dbSScott Long * POSSIBILITY OF SUCH DAMAGE. 264c7070dbSScott Long */ 274c7070dbSScott Long 284c7070dbSScott Long #include <sys/cdefs.h> 294c7070dbSScott Long __FBSDID("$FreeBSD$"); 304c7070dbSScott Long 31aaeb188aSBjoern A. Zeeb #include "opt_inet.h" 32aaeb188aSBjoern A. Zeeb #include "opt_inet6.h" 33aaeb188aSBjoern A. Zeeb #include "opt_acpi.h" 34b103855eSStephen Hurd #include "opt_sched.h" 35aaeb188aSBjoern A. Zeeb 364c7070dbSScott Long #include <sys/param.h> 374c7070dbSScott Long #include <sys/types.h> 384c7070dbSScott Long #include <sys/bus.h> 394c7070dbSScott Long #include <sys/eventhandler.h> 404c7070dbSScott Long #include <sys/kernel.h> 414c7070dbSScott Long #include <sys/lock.h> 424c7070dbSScott Long #include <sys/mutex.h> 434c7070dbSScott Long #include <sys/module.h> 444c7070dbSScott Long #include <sys/kobj.h> 454c7070dbSScott Long #include <sys/rman.h> 464c7070dbSScott Long #include <sys/sbuf.h> 474c7070dbSScott Long #include <sys/smp.h> 484c7070dbSScott Long #include <sys/socket.h> 4909f6ff4fSMatt Macy #include <sys/sockio.h> 504c7070dbSScott Long #include <sys/sysctl.h> 514c7070dbSScott Long #include <sys/syslog.h> 524c7070dbSScott Long #include <sys/taskqueue.h> 5323ac9029SStephen Hurd #include <sys/limits.h> 544c7070dbSScott Long 554c7070dbSScott Long #include <net/if.h> 564c7070dbSScott Long #include <net/if_var.h> 574c7070dbSScott Long #include <net/if_types.h> 584c7070dbSScott Long #include <net/if_media.h> 594c7070dbSScott Long #include <net/bpf.h> 604c7070dbSScott Long #include <net/ethernet.h> 614c7070dbSScott Long #include <net/mp_ring.h> 627790c8c1SConrad Meyer #include <net/debugnet.h> 636d49b41eSAndrew Gallatin #include <net/pfil.h> 6435e4e998SStephen Hurd #include <net/vnet.h> 654c7070dbSScott Long 664c7070dbSScott Long #include <netinet/in.h> 674c7070dbSScott Long #include <netinet/in_pcb.h> 684c7070dbSScott Long #include <netinet/tcp_lro.h> 694c7070dbSScott Long #include <netinet/in_systm.h> 704c7070dbSScott Long #include <netinet/if_ether.h> 714c7070dbSScott Long #include <netinet/ip.h> 724c7070dbSScott Long #include <netinet/ip6.h> 734c7070dbSScott Long #include <netinet/tcp.h> 7435e4e998SStephen Hurd #include <netinet/ip_var.h> 7535e4e998SStephen Hurd #include <netinet6/ip6_var.h> 764c7070dbSScott Long 774c7070dbSScott Long #include <machine/bus.h> 784c7070dbSScott Long #include <machine/in_cksum.h> 794c7070dbSScott Long 804c7070dbSScott Long #include <vm/vm.h> 814c7070dbSScott Long #include <vm/pmap.h> 824c7070dbSScott Long 834c7070dbSScott Long #include <dev/led/led.h> 844c7070dbSScott Long #include <dev/pci/pcireg.h> 854c7070dbSScott Long #include <dev/pci/pcivar.h> 864c7070dbSScott Long #include <dev/pci/pci_private.h> 874c7070dbSScott Long 884c7070dbSScott Long #include <net/iflib.h> 8909f6ff4fSMatt Macy #include <net/iflib_private.h> 904c7070dbSScott Long 914c7070dbSScott Long #include "ifdi_if.h" 924c7070dbSScott Long 9377c1fcecSEric Joyner #ifdef PCI_IOV 9477c1fcecSEric Joyner #include <dev/pci/pci_iov.h> 9577c1fcecSEric Joyner #endif 9677c1fcecSEric Joyner 9787890dbaSSean Bruno #include <sys/bitstring.h> 984c7070dbSScott Long /* 9995246abbSSean Bruno * enable accounting of every mbuf as it comes in to and goes out of 10095246abbSSean Bruno * iflib's software descriptor references 1014c7070dbSScott Long */ 1024c7070dbSScott Long #define MEMORY_LOGGING 0 1034c7070dbSScott Long /* 1044c7070dbSScott Long * Enable mbuf vectors for compressing long mbuf chains 1054c7070dbSScott Long */ 1064c7070dbSScott Long 1074c7070dbSScott Long /* 1084c7070dbSScott Long * NB: 1094c7070dbSScott Long * - Prefetching in tx cleaning should perhaps be a tunable. The distance ahead 1104c7070dbSScott Long * we prefetch needs to be determined by the time spent in m_free vis a vis 1114c7070dbSScott Long * the cost of a prefetch. This will of course vary based on the workload: 1124c7070dbSScott Long * - NFLX's m_free path is dominated by vm-based M_EXT manipulation which 1134c7070dbSScott Long * is quite expensive, thus suggesting very little prefetch. 1144c7070dbSScott Long * - small packet forwarding which is just returning a single mbuf to 1154c7070dbSScott Long * UMA will typically be very fast vis a vis the cost of a memory 1164c7070dbSScott Long * access. 1174c7070dbSScott Long */ 1184c7070dbSScott Long 1194c7070dbSScott Long /* 1204c7070dbSScott Long * File organization: 1214c7070dbSScott Long * - private structures 1224c7070dbSScott Long * - iflib private utility functions 1234c7070dbSScott Long * - ifnet functions 1244c7070dbSScott Long * - vlan registry and other exported functions 1254c7070dbSScott Long * - iflib public core functions 1264c7070dbSScott Long * 1274c7070dbSScott Long * 1284c7070dbSScott Long */ 12909f6ff4fSMatt Macy MALLOC_DEFINE(M_IFLIB, "iflib", "ifnet library"); 1304c7070dbSScott Long 131fb1a29b4SHans Petter Selasky #define IFLIB_RXEOF_MORE (1U << 0) 132fb1a29b4SHans Petter Selasky #define IFLIB_RXEOF_EMPTY (2U << 0) 133fb1a29b4SHans Petter Selasky 1344c7070dbSScott Long struct iflib_txq; 1354c7070dbSScott Long typedef struct iflib_txq *iflib_txq_t; 1364c7070dbSScott Long struct iflib_rxq; 1374c7070dbSScott Long typedef struct iflib_rxq *iflib_rxq_t; 1384c7070dbSScott Long struct iflib_fl; 1394c7070dbSScott Long typedef struct iflib_fl *iflib_fl_t; 1404c7070dbSScott Long 1414ecb427aSSean Bruno struct iflib_ctx; 1424ecb427aSSean Bruno 1432d873474SStephen Hurd static void iru_init(if_rxd_update_t iru, iflib_rxq_t rxq, uint8_t flid); 144dd7fbcf1SStephen Hurd static void iflib_timer(void *arg); 1452d873474SStephen Hurd 1464c7070dbSScott Long typedef struct iflib_filter_info { 1474c7070dbSScott Long driver_filter_t *ifi_filter; 1484c7070dbSScott Long void *ifi_filter_arg; 1494c7070dbSScott Long struct grouptask *ifi_task; 15095246abbSSean Bruno void *ifi_ctx; 1514c7070dbSScott Long } *iflib_filter_info_t; 1524c7070dbSScott Long 1534c7070dbSScott Long struct iflib_ctx { 1544c7070dbSScott Long KOBJ_FIELDS; 1554c7070dbSScott Long /* 1564c7070dbSScott Long * Pointer to hardware driver's softc 1574c7070dbSScott Long */ 1584c7070dbSScott Long void *ifc_softc; 1594c7070dbSScott Long device_t ifc_dev; 1604c7070dbSScott Long if_t ifc_ifp; 1614c7070dbSScott Long 1624c7070dbSScott Long cpuset_t ifc_cpus; 1634c7070dbSScott Long if_shared_ctx_t ifc_sctx; 1644c7070dbSScott Long struct if_softc_ctx ifc_softc_ctx; 1654c7070dbSScott Long 166aa8a24d3SStephen Hurd struct sx ifc_ctx_sx; 1677b610b60SSean Bruno struct mtx ifc_state_mtx; 1684c7070dbSScott Long 1694c7070dbSScott Long iflib_txq_t ifc_txqs; 1704c7070dbSScott Long iflib_rxq_t ifc_rxqs; 1714c7070dbSScott Long uint32_t ifc_if_flags; 1724c7070dbSScott Long uint32_t ifc_flags; 1734c7070dbSScott Long uint32_t ifc_max_fl_buf_size; 1741b9d9394SEric Joyner uint32_t ifc_rx_mbuf_sz; 1754c7070dbSScott Long 1764c7070dbSScott Long int ifc_link_state; 1774c7070dbSScott Long int ifc_watchdog_events; 1784c7070dbSScott Long struct cdev *ifc_led_dev; 1794c7070dbSScott Long struct resource *ifc_msix_mem; 1804c7070dbSScott Long 1814c7070dbSScott Long struct if_irq ifc_legacy_irq; 1824c7070dbSScott Long struct grouptask ifc_admin_task; 1834c7070dbSScott Long struct grouptask ifc_vflr_task; 1844c7070dbSScott Long struct iflib_filter_info ifc_filter_info; 1854c7070dbSScott Long struct ifmedia ifc_media; 186e2621d96SMatt Macy struct ifmedia *ifc_mediap; 1874c7070dbSScott Long 1884c7070dbSScott Long struct sysctl_oid *ifc_sysctl_node; 1894c7070dbSScott Long uint16_t ifc_sysctl_ntxqs; 1904c7070dbSScott Long uint16_t ifc_sysctl_nrxqs; 19123ac9029SStephen Hurd uint16_t ifc_sysctl_qs_eq_override; 192f4d2154eSStephen Hurd uint16_t ifc_sysctl_rx_budget; 193fe51d4cdSStephen Hurd uint16_t ifc_sysctl_tx_abdicate; 194f154ece0SStephen Hurd uint16_t ifc_sysctl_core_offset; 195f154ece0SStephen Hurd #define CORE_OFFSET_UNSPECIFIED 0xffff 196f154ece0SStephen Hurd uint8_t ifc_sysctl_separate_txrx; 19723ac9029SStephen Hurd 19895246abbSSean Bruno qidx_t ifc_sysctl_ntxds[8]; 19995246abbSSean Bruno qidx_t ifc_sysctl_nrxds[8]; 2004c7070dbSScott Long struct if_txrx ifc_txrx; 2014c7070dbSScott Long #define isc_txd_encap ifc_txrx.ift_txd_encap 2024c7070dbSScott Long #define isc_txd_flush ifc_txrx.ift_txd_flush 2034c7070dbSScott Long #define isc_txd_credits_update ifc_txrx.ift_txd_credits_update 2044c7070dbSScott Long #define isc_rxd_available ifc_txrx.ift_rxd_available 2054c7070dbSScott Long #define isc_rxd_pkt_get ifc_txrx.ift_rxd_pkt_get 2064c7070dbSScott Long #define isc_rxd_refill ifc_txrx.ift_rxd_refill 2074c7070dbSScott Long #define isc_rxd_flush ifc_txrx.ift_rxd_flush 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; 349*17cec474SVincenzo Maffione #ifdef DEV_NETMAP 350*17cec474SVincenzo Maffione struct callout ift_netmap_timer; 351*17cec474SVincenzo Maffione #endif /* DEV_NETMAP */ 3524c7070dbSScott Long 35395246abbSSean Bruno if_txsd_vec_t ift_sds; 3544c7070dbSScott Long uint8_t ift_qstatus; 3554c7070dbSScott Long uint8_t ift_closed; 35695246abbSSean Bruno uint8_t ift_update_freq; 3574c7070dbSScott Long struct iflib_filter_info ift_filter_info; 358bfce461eSMarius Strobl bus_dma_tag_t ift_buf_tag; 359bfce461eSMarius Strobl bus_dma_tag_t ift_tso_buf_tag; 3604c7070dbSScott Long iflib_dma_info_t ift_ifdi; 361814fa34dSMark Johnston #define MTX_NAME_LEN 32 3624c7070dbSScott Long char ift_mtx_name[MTX_NAME_LEN]; 3634c7070dbSScott Long bus_dma_segment_t ift_segs[IFLIB_MAX_TX_SEGS] __aligned(CACHE_LINE_SIZE); 3641248952aSSean Bruno #ifdef IFLIB_DIAGNOSTICS 3651248952aSSean Bruno uint64_t ift_cpu_exec_count[256]; 3661248952aSSean Bruno #endif 3674c7070dbSScott Long } __aligned(CACHE_LINE_SIZE); 3684c7070dbSScott Long 3694c7070dbSScott Long struct iflib_fl { 37095246abbSSean Bruno qidx_t ifl_cidx; 37195246abbSSean Bruno qidx_t ifl_pidx; 37295246abbSSean Bruno qidx_t ifl_credits; 3734c7070dbSScott Long uint8_t ifl_gen; 37495246abbSSean Bruno uint8_t ifl_rxd_size; 3754c7070dbSScott Long #if MEMORY_LOGGING 3764c7070dbSScott Long uint64_t ifl_m_enqueued; 3774c7070dbSScott Long uint64_t ifl_m_dequeued; 3784c7070dbSScott Long uint64_t ifl_cl_enqueued; 3794c7070dbSScott Long uint64_t ifl_cl_dequeued; 3804c7070dbSScott Long #endif 3814c7070dbSScott Long /* implicit pad */ 38287890dbaSSean Bruno bitstr_t *ifl_rx_bitmap; 38387890dbaSSean Bruno qidx_t ifl_fragidx; 3844c7070dbSScott Long /* constant */ 38595246abbSSean Bruno qidx_t ifl_size; 3864c7070dbSScott Long uint16_t ifl_buf_size; 3874c7070dbSScott Long uint16_t ifl_cltype; 3884c7070dbSScott Long uma_zone_t ifl_zone; 389e035717eSSean Bruno iflib_rxsd_array_t ifl_sds; 3904c7070dbSScott Long iflib_rxq_t ifl_rxq; 3914c7070dbSScott Long uint8_t ifl_id; 392bfce461eSMarius Strobl bus_dma_tag_t ifl_buf_tag; 3934c7070dbSScott Long iflib_dma_info_t ifl_ifdi; 3944c7070dbSScott Long uint64_t ifl_bus_addrs[IFLIB_MAX_RX_REFRESH] __aligned(CACHE_LINE_SIZE); 39595246abbSSean Bruno qidx_t ifl_rxd_idxs[IFLIB_MAX_RX_REFRESH]; 3964c7070dbSScott Long } __aligned(CACHE_LINE_SIZE); 3974c7070dbSScott Long 39895246abbSSean Bruno static inline qidx_t 39995246abbSSean Bruno get_inuse(int size, qidx_t cidx, qidx_t pidx, uint8_t gen) 4004c7070dbSScott Long { 40195246abbSSean Bruno qidx_t used; 4024c7070dbSScott Long 4034c7070dbSScott Long if (pidx > cidx) 4044c7070dbSScott Long used = pidx - cidx; 4054c7070dbSScott Long else if (pidx < cidx) 4064c7070dbSScott Long used = size - cidx + pidx; 4074c7070dbSScott Long else if (gen == 0 && pidx == cidx) 4084c7070dbSScott Long used = 0; 4094c7070dbSScott Long else if (gen == 1 && pidx == cidx) 4104c7070dbSScott Long used = size; 4114c7070dbSScott Long else 4124c7070dbSScott Long panic("bad state"); 4134c7070dbSScott Long 4144c7070dbSScott Long return (used); 4154c7070dbSScott Long } 4164c7070dbSScott Long 4174c7070dbSScott Long #define TXQ_AVAIL(txq) (txq->ift_size - get_inuse(txq->ift_size, txq->ift_cidx, txq->ift_pidx, txq->ift_gen)) 4184c7070dbSScott Long 4194c7070dbSScott Long #define IDXDIFF(head, tail, wrap) \ 4204c7070dbSScott Long ((head) >= (tail) ? (head) - (tail) : (wrap) - (tail) + (head)) 4214c7070dbSScott Long 4224c7070dbSScott Long struct iflib_rxq { 4234c7070dbSScott Long if_ctx_t ifr_ctx; 4244c7070dbSScott Long iflib_fl_t ifr_fl; 4254c7070dbSScott Long uint64_t ifr_rx_irq; 4266d49b41eSAndrew Gallatin struct pfil_head *pfil; 4271722eeacSMarius Strobl /* 4281722eeacSMarius Strobl * If there is a separate completion queue (IFLIB_HAS_RXCQ), this is 4296d84e76aSVincenzo Maffione * the completion queue consumer index. Otherwise it's unused. 4301722eeacSMarius Strobl */ 4311722eeacSMarius Strobl qidx_t ifr_cq_cidx; 4324c7070dbSScott Long uint16_t ifr_id; 4334c7070dbSScott Long uint8_t ifr_nfl; 43495246abbSSean Bruno uint8_t ifr_ntxqirq; 43595246abbSSean Bruno uint8_t ifr_txqid[IFLIB_MAX_TX_SHARED_INTR]; 4361722eeacSMarius Strobl uint8_t ifr_fl_offset; 4374c7070dbSScott Long struct lro_ctrl ifr_lc; 4384c7070dbSScott Long struct grouptask ifr_task; 439fb1a29b4SHans Petter Selasky struct callout ifr_watchdog; 4404c7070dbSScott Long struct iflib_filter_info ifr_filter_info; 4414c7070dbSScott Long iflib_dma_info_t ifr_ifdi; 442ab2e3f79SStephen Hurd 4434c7070dbSScott Long /* dynamically allocate if any drivers need a value substantially larger than this */ 4444c7070dbSScott Long struct if_rxd_frag ifr_frags[IFLIB_MAX_RX_SEGS] __aligned(CACHE_LINE_SIZE); 4451248952aSSean Bruno #ifdef IFLIB_DIAGNOSTICS 4461248952aSSean Bruno uint64_t ifr_cpu_exec_count[256]; 4471248952aSSean Bruno #endif 4484c7070dbSScott Long } __aligned(CACHE_LINE_SIZE); 4494c7070dbSScott Long 45095246abbSSean Bruno typedef struct if_rxsd { 45195246abbSSean Bruno caddr_t *ifsd_cl; 45295246abbSSean Bruno iflib_fl_t ifsd_fl; 45395246abbSSean Bruno } *if_rxsd_t; 45495246abbSSean Bruno 45595246abbSSean Bruno /* multiple of word size */ 45695246abbSSean Bruno #ifdef __LP64__ 457ab2e3f79SStephen Hurd #define PKT_INFO_SIZE 6 45895246abbSSean Bruno #define RXD_INFO_SIZE 5 45995246abbSSean Bruno #define PKT_TYPE uint64_t 46095246abbSSean Bruno #else 461ab2e3f79SStephen Hurd #define PKT_INFO_SIZE 11 46295246abbSSean Bruno #define RXD_INFO_SIZE 8 46395246abbSSean Bruno #define PKT_TYPE uint32_t 46495246abbSSean Bruno #endif 46595246abbSSean Bruno #define PKT_LOOP_BOUND ((PKT_INFO_SIZE/3)*3) 46695246abbSSean Bruno #define RXD_LOOP_BOUND ((RXD_INFO_SIZE/4)*4) 46795246abbSSean Bruno 46895246abbSSean Bruno typedef struct if_pkt_info_pad { 46995246abbSSean Bruno PKT_TYPE pkt_val[PKT_INFO_SIZE]; 47095246abbSSean Bruno } *if_pkt_info_pad_t; 47195246abbSSean Bruno typedef struct if_rxd_info_pad { 47295246abbSSean Bruno PKT_TYPE rxd_val[RXD_INFO_SIZE]; 47395246abbSSean Bruno } *if_rxd_info_pad_t; 47495246abbSSean Bruno 47595246abbSSean Bruno CTASSERT(sizeof(struct if_pkt_info_pad) == sizeof(struct if_pkt_info)); 47695246abbSSean Bruno CTASSERT(sizeof(struct if_rxd_info_pad) == sizeof(struct if_rxd_info)); 47795246abbSSean Bruno 47895246abbSSean Bruno static inline void 47995246abbSSean Bruno pkt_info_zero(if_pkt_info_t pi) 48095246abbSSean Bruno { 48195246abbSSean Bruno if_pkt_info_pad_t pi_pad; 48295246abbSSean Bruno 48395246abbSSean Bruno pi_pad = (if_pkt_info_pad_t)pi; 48495246abbSSean Bruno pi_pad->pkt_val[0] = 0; pi_pad->pkt_val[1] = 0; pi_pad->pkt_val[2] = 0; 48595246abbSSean Bruno pi_pad->pkt_val[3] = 0; pi_pad->pkt_val[4] = 0; pi_pad->pkt_val[5] = 0; 48695246abbSSean Bruno #ifndef __LP64__ 487ab2e3f79SStephen Hurd pi_pad->pkt_val[6] = 0; pi_pad->pkt_val[7] = 0; pi_pad->pkt_val[8] = 0; 488ab2e3f79SStephen Hurd pi_pad->pkt_val[9] = 0; pi_pad->pkt_val[10] = 0; 48995246abbSSean Bruno #endif 49095246abbSSean Bruno } 49195246abbSSean Bruno 49209f6ff4fSMatt Macy static device_method_t iflib_pseudo_methods[] = { 49309f6ff4fSMatt Macy DEVMETHOD(device_attach, noop_attach), 49409f6ff4fSMatt Macy DEVMETHOD(device_detach, iflib_pseudo_detach), 49509f6ff4fSMatt Macy DEVMETHOD_END 49609f6ff4fSMatt Macy }; 49709f6ff4fSMatt Macy 49809f6ff4fSMatt Macy driver_t iflib_pseudodriver = { 49909f6ff4fSMatt Macy "iflib_pseudo", iflib_pseudo_methods, sizeof(struct iflib_ctx), 50009f6ff4fSMatt Macy }; 50109f6ff4fSMatt Macy 50295246abbSSean Bruno static inline void 50395246abbSSean Bruno rxd_info_zero(if_rxd_info_t ri) 50495246abbSSean Bruno { 50595246abbSSean Bruno if_rxd_info_pad_t ri_pad; 50695246abbSSean Bruno int i; 50795246abbSSean Bruno 50895246abbSSean Bruno ri_pad = (if_rxd_info_pad_t)ri; 50995246abbSSean Bruno for (i = 0; i < RXD_LOOP_BOUND; i += 4) { 51095246abbSSean Bruno ri_pad->rxd_val[i] = 0; 51195246abbSSean Bruno ri_pad->rxd_val[i+1] = 0; 51295246abbSSean Bruno ri_pad->rxd_val[i+2] = 0; 51395246abbSSean Bruno ri_pad->rxd_val[i+3] = 0; 51495246abbSSean Bruno } 51595246abbSSean Bruno #ifdef __LP64__ 51695246abbSSean Bruno ri_pad->rxd_val[RXD_INFO_SIZE-1] = 0; 51795246abbSSean Bruno #endif 51895246abbSSean Bruno } 51995246abbSSean Bruno 5204c7070dbSScott Long /* 5214c7070dbSScott Long * Only allow a single packet to take up most 1/nth of the tx ring 5224c7070dbSScott Long */ 5234c7070dbSScott Long #define MAX_SINGLE_PACKET_FRACTION 12 5244c7070dbSScott Long #define IF_BAD_DMA (bus_addr_t)-1 5254c7070dbSScott Long 5264c7070dbSScott Long #define CTX_ACTIVE(ctx) ((if_getdrvflags((ctx)->ifc_ifp) & IFF_DRV_RUNNING)) 5274c7070dbSScott Long 528aa8a24d3SStephen Hurd #define CTX_LOCK_INIT(_sc) sx_init(&(_sc)->ifc_ctx_sx, "iflib ctx lock") 529aa8a24d3SStephen Hurd #define CTX_LOCK(ctx) sx_xlock(&(ctx)->ifc_ctx_sx) 530aa8a24d3SStephen Hurd #define CTX_UNLOCK(ctx) sx_xunlock(&(ctx)->ifc_ctx_sx) 531aa8a24d3SStephen Hurd #define CTX_LOCK_DESTROY(ctx) sx_destroy(&(ctx)->ifc_ctx_sx) 5324c7070dbSScott Long 5337b610b60SSean Bruno #define STATE_LOCK_INIT(_sc, _name) mtx_init(&(_sc)->ifc_state_mtx, _name, "iflib state lock", MTX_DEF) 5347b610b60SSean Bruno #define STATE_LOCK(ctx) mtx_lock(&(ctx)->ifc_state_mtx) 5357b610b60SSean Bruno #define STATE_UNLOCK(ctx) mtx_unlock(&(ctx)->ifc_state_mtx) 5367b610b60SSean Bruno #define STATE_LOCK_DESTROY(ctx) mtx_destroy(&(ctx)->ifc_state_mtx) 5377b610b60SSean Bruno 5384c7070dbSScott Long #define CALLOUT_LOCK(txq) mtx_lock(&txq->ift_mtx) 5394c7070dbSScott Long #define CALLOUT_UNLOCK(txq) mtx_unlock(&txq->ift_mtx) 5404c7070dbSScott Long 54177c1fcecSEric Joyner void 54277c1fcecSEric Joyner iflib_set_detach(if_ctx_t ctx) 54377c1fcecSEric Joyner { 54477c1fcecSEric Joyner STATE_LOCK(ctx); 54577c1fcecSEric Joyner ctx->ifc_flags |= IFC_IN_DETACH; 54677c1fcecSEric Joyner STATE_UNLOCK(ctx); 54777c1fcecSEric Joyner } 5484c7070dbSScott Long 5494c7070dbSScott Long /* Our boot-time initialization hook */ 5504c7070dbSScott Long static int iflib_module_event_handler(module_t, int, void *); 5514c7070dbSScott Long 5524c7070dbSScott Long static moduledata_t iflib_moduledata = { 5534c7070dbSScott Long "iflib", 5544c7070dbSScott Long iflib_module_event_handler, 5554c7070dbSScott Long NULL 5564c7070dbSScott Long }; 5574c7070dbSScott Long 5584c7070dbSScott Long DECLARE_MODULE(iflib, iflib_moduledata, SI_SUB_INIT_IF, SI_ORDER_ANY); 5594c7070dbSScott Long MODULE_VERSION(iflib, 1); 5604c7070dbSScott Long 5614c7070dbSScott Long MODULE_DEPEND(iflib, pci, 1, 1, 1); 5624c7070dbSScott Long MODULE_DEPEND(iflib, ether, 1, 1, 1); 5634c7070dbSScott Long 564ab2e3f79SStephen Hurd TASKQGROUP_DEFINE(if_io_tqg, mp_ncpus, 1); 565ab2e3f79SStephen Hurd TASKQGROUP_DEFINE(if_config_tqg, 1, 1); 566ab2e3f79SStephen Hurd 5674c7070dbSScott Long #ifndef IFLIB_DEBUG_COUNTERS 5684c7070dbSScott Long #ifdef INVARIANTS 5694c7070dbSScott Long #define IFLIB_DEBUG_COUNTERS 1 5704c7070dbSScott Long #else 5714c7070dbSScott Long #define IFLIB_DEBUG_COUNTERS 0 5724c7070dbSScott Long #endif /* !INVARIANTS */ 5734c7070dbSScott Long #endif 5744c7070dbSScott Long 5757029da5cSPawel Biernacki static SYSCTL_NODE(_net, OID_AUTO, iflib, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 576ab2e3f79SStephen Hurd "iflib driver parameters"); 577ab2e3f79SStephen Hurd 5784c7070dbSScott Long /* 5794c7070dbSScott Long * XXX need to ensure that this can't accidentally cause the head to be moved backwards 5804c7070dbSScott Long */ 5814c7070dbSScott Long static int iflib_min_tx_latency = 0; 5824c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, min_tx_latency, CTLFLAG_RW, 583da69b8f9SSean Bruno &iflib_min_tx_latency, 0, "minimize transmit latency at the possible expense of throughput"); 58495246abbSSean Bruno static int iflib_no_tx_batch = 0; 58595246abbSSean Bruno SYSCTL_INT(_net_iflib, OID_AUTO, no_tx_batch, CTLFLAG_RW, 58695246abbSSean Bruno &iflib_no_tx_batch, 0, "minimize transmit latency at the possible expense of throughput"); 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 static int iflib_txq_drain_flushing; 6144c7070dbSScott Long static int iflib_txq_drain_oactive; 6154c7070dbSScott Long static int iflib_txq_drain_notready; 6164c7070dbSScott Long 6174c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, txq_drain_flushing, CTLFLAG_RD, 6184c7070dbSScott Long &iflib_txq_drain_flushing, 0, "# drain flushes"); 6194c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, txq_drain_oactive, CTLFLAG_RD, 6204c7070dbSScott Long &iflib_txq_drain_oactive, 0, "# drain oactives"); 6214c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, txq_drain_notready, CTLFLAG_RD, 6224c7070dbSScott Long &iflib_txq_drain_notready, 0, "# drain notready"); 6234c7070dbSScott Long 6244c7070dbSScott Long static int iflib_encap_load_mbuf_fail; 625d14c853bSStephen Hurd static int iflib_encap_pad_mbuf_fail; 6264c7070dbSScott Long static int iflib_encap_txq_avail_fail; 6274c7070dbSScott Long static int iflib_encap_txd_encap_fail; 6284c7070dbSScott Long 6294c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, encap_load_mbuf_fail, CTLFLAG_RD, 6304c7070dbSScott Long &iflib_encap_load_mbuf_fail, 0, "# busdma load failures"); 631d14c853bSStephen Hurd SYSCTL_INT(_net_iflib, OID_AUTO, encap_pad_mbuf_fail, CTLFLAG_RD, 632d14c853bSStephen Hurd &iflib_encap_pad_mbuf_fail, 0, "# runt frame pad failures"); 6334c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, encap_txq_avail_fail, CTLFLAG_RD, 6344c7070dbSScott Long &iflib_encap_txq_avail_fail, 0, "# txq avail failures"); 6354c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, encap_txd_encap_fail, CTLFLAG_RD, 6364c7070dbSScott Long &iflib_encap_txd_encap_fail, 0, "# driver encap failures"); 6374c7070dbSScott Long 6384c7070dbSScott Long static int iflib_task_fn_rxs; 6394c7070dbSScott Long static int iflib_rx_intr_enables; 6404c7070dbSScott Long static int iflib_fast_intrs; 6414c7070dbSScott Long static int iflib_rx_unavail; 6424c7070dbSScott Long static int iflib_rx_ctx_inactive; 6434c7070dbSScott Long static int iflib_rx_if_input; 6444c7070dbSScott Long static int iflib_rxd_flush; 6454c7070dbSScott Long 6464c7070dbSScott Long static int iflib_verbose_debug; 6474c7070dbSScott Long 6484c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, task_fn_rx, CTLFLAG_RD, 6494c7070dbSScott Long &iflib_task_fn_rxs, 0, "# task_fn_rx calls"); 6504c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rx_intr_enables, CTLFLAG_RD, 6511722eeacSMarius Strobl &iflib_rx_intr_enables, 0, "# RX intr enables"); 6524c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, fast_intrs, CTLFLAG_RD, 6534c7070dbSScott Long &iflib_fast_intrs, 0, "# fast_intr calls"); 6544c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rx_unavail, CTLFLAG_RD, 6554c7070dbSScott Long &iflib_rx_unavail, 0, "# times rxeof called with no available data"); 6564c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rx_ctx_inactive, CTLFLAG_RD, 6574c7070dbSScott Long &iflib_rx_ctx_inactive, 0, "# times rxeof called with inactive context"); 6584c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rx_if_input, CTLFLAG_RD, 6594c7070dbSScott Long &iflib_rx_if_input, 0, "# times rxeof called if_input"); 6604c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rxd_flush, CTLFLAG_RD, 6614c7070dbSScott Long &iflib_rxd_flush, 0, "# times rxd_flush called"); 6624c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, verbose_debug, CTLFLAG_RW, 6634c7070dbSScott Long &iflib_verbose_debug, 0, "enable verbose debugging"); 6644c7070dbSScott Long 6654c7070dbSScott Long #define DBG_COUNTER_INC(name) atomic_add_int(&(iflib_ ## name), 1) 666da69b8f9SSean Bruno static void 667da69b8f9SSean Bruno iflib_debug_reset(void) 668da69b8f9SSean Bruno { 669da69b8f9SSean Bruno iflib_tx_seen = iflib_tx_sent = iflib_tx_encap = iflib_rx_allocs = 670da69b8f9SSean Bruno iflib_fl_refills = iflib_fl_refills_large = iflib_tx_frees = 671da69b8f9SSean Bruno iflib_txq_drain_flushing = iflib_txq_drain_oactive = 67264e6fc13SStephen Hurd iflib_txq_drain_notready = 673d14c853bSStephen Hurd iflib_encap_load_mbuf_fail = iflib_encap_pad_mbuf_fail = 674d14c853bSStephen Hurd iflib_encap_txq_avail_fail = iflib_encap_txd_encap_fail = 675d14c853bSStephen Hurd iflib_task_fn_rxs = iflib_rx_intr_enables = iflib_fast_intrs = 67664e6fc13SStephen Hurd iflib_rx_unavail = 67764e6fc13SStephen Hurd iflib_rx_ctx_inactive = iflib_rx_if_input = 6786d49b41eSAndrew Gallatin iflib_rxd_flush = 0; 679da69b8f9SSean Bruno } 6804c7070dbSScott Long 6814c7070dbSScott Long #else 6824c7070dbSScott Long #define DBG_COUNTER_INC(name) 683da69b8f9SSean Bruno static void iflib_debug_reset(void) {} 6844c7070dbSScott Long #endif 6854c7070dbSScott Long 6864c7070dbSScott Long #define IFLIB_DEBUG 0 6874c7070dbSScott Long 6884c7070dbSScott Long static void iflib_tx_structures_free(if_ctx_t ctx); 6894c7070dbSScott Long static void iflib_rx_structures_free(if_ctx_t ctx); 6904c7070dbSScott Long static int iflib_queues_alloc(if_ctx_t ctx); 6914c7070dbSScott Long static int iflib_tx_credits_update(if_ctx_t ctx, iflib_txq_t txq); 69295246abbSSean Bruno static int iflib_rxd_avail(if_ctx_t ctx, iflib_rxq_t rxq, qidx_t cidx, qidx_t budget); 6934c7070dbSScott Long static int iflib_qset_structures_setup(if_ctx_t ctx); 6944c7070dbSScott Long static int iflib_msix_init(if_ctx_t ctx); 6953e0e6330SStephen Hurd static int iflib_legacy_setup(if_ctx_t ctx, driver_filter_t filter, void *filterarg, int *rid, const char *str); 6964c7070dbSScott Long static void iflib_txq_check_drain(iflib_txq_t txq, int budget); 6974c7070dbSScott Long static uint32_t iflib_txq_can_drain(struct ifmp_ring *); 698b8ca4756SPatrick Kelsey #ifdef ALTQ 699b8ca4756SPatrick Kelsey static void iflib_altq_if_start(if_t ifp); 700b8ca4756SPatrick Kelsey static int iflib_altq_if_transmit(if_t ifp, struct mbuf *m); 701b8ca4756SPatrick Kelsey #endif 7024c7070dbSScott Long static int iflib_register(if_ctx_t); 70356614414SEric Joyner static void iflib_deregister(if_ctx_t); 7041558015eSEric Joyner static void iflib_unregister_vlan_handlers(if_ctx_t ctx); 705b3813609SPatrick Kelsey static uint16_t iflib_get_mbuf_size_for(unsigned int size); 7064c7070dbSScott Long static void iflib_init_locked(if_ctx_t ctx); 7074c7070dbSScott Long static void iflib_add_device_sysctl_pre(if_ctx_t ctx); 7084c7070dbSScott Long static void iflib_add_device_sysctl_post(if_ctx_t ctx); 709da69b8f9SSean Bruno static void iflib_ifmp_purge(iflib_txq_t txq); 7101248952aSSean Bruno static void _iflib_pre_assert(if_softc_ctx_t scctx); 71195246abbSSean Bruno static void iflib_if_init_locked(if_ctx_t ctx); 71277c1fcecSEric Joyner static void iflib_free_intr_mem(if_ctx_t ctx); 71395246abbSSean Bruno #ifndef __NO_STRICT_ALIGNMENT 71495246abbSSean Bruno static struct mbuf * iflib_fixup_rx(struct mbuf *m); 71595246abbSSean Bruno #endif 7164c7070dbSScott Long 717f154ece0SStephen Hurd static SLIST_HEAD(cpu_offset_list, cpu_offset) cpu_offsets = 718f154ece0SStephen Hurd SLIST_HEAD_INITIALIZER(cpu_offsets); 719f154ece0SStephen Hurd struct cpu_offset { 720f154ece0SStephen Hurd SLIST_ENTRY(cpu_offset) entries; 721f154ece0SStephen Hurd cpuset_t set; 722f154ece0SStephen Hurd unsigned int refcount; 723f154ece0SStephen Hurd uint16_t offset; 724f154ece0SStephen Hurd }; 725f154ece0SStephen Hurd static struct mtx cpu_offset_mtx; 726f154ece0SStephen Hurd MTX_SYSINIT(iflib_cpu_offset, &cpu_offset_mtx, "iflib_cpu_offset lock", 727f154ece0SStephen Hurd MTX_DEF); 728f154ece0SStephen Hurd 7297790c8c1SConrad Meyer DEBUGNET_DEFINE(iflib); 73094618825SMark Johnston 731ac11d857SVincenzo Maffione static int 732ac11d857SVincenzo Maffione iflib_num_rx_descs(if_ctx_t ctx) 733ac11d857SVincenzo Maffione { 734ac11d857SVincenzo Maffione if_softc_ctx_t scctx = &ctx->ifc_softc_ctx; 735ac11d857SVincenzo Maffione if_shared_ctx_t sctx = ctx->ifc_sctx; 736ac11d857SVincenzo Maffione uint16_t first_rxq = (sctx->isc_flags & IFLIB_HAS_RXCQ) ? 1 : 0; 737ac11d857SVincenzo Maffione 738ac11d857SVincenzo Maffione return scctx->isc_nrxd[first_rxq]; 739ac11d857SVincenzo Maffione } 740ac11d857SVincenzo Maffione 741ac11d857SVincenzo Maffione static int 742ac11d857SVincenzo Maffione iflib_num_tx_descs(if_ctx_t ctx) 743ac11d857SVincenzo Maffione { 744ac11d857SVincenzo Maffione if_softc_ctx_t scctx = &ctx->ifc_softc_ctx; 745ac11d857SVincenzo Maffione if_shared_ctx_t sctx = ctx->ifc_sctx; 746ac11d857SVincenzo Maffione uint16_t first_txq = (sctx->isc_flags & IFLIB_HAS_TXCQ) ? 1 : 0; 747ac11d857SVincenzo Maffione 748ac11d857SVincenzo Maffione return scctx->isc_ntxd[first_txq]; 749ac11d857SVincenzo Maffione } 750ac11d857SVincenzo Maffione 7514c7070dbSScott Long #ifdef DEV_NETMAP 7524c7070dbSScott Long #include <sys/selinfo.h> 7534c7070dbSScott Long #include <net/netmap.h> 7544c7070dbSScott Long #include <dev/netmap/netmap_kern.h> 7554c7070dbSScott Long 7564c7070dbSScott Long MODULE_DEPEND(iflib, netmap, 1, 1, 1); 7574c7070dbSScott Long 758de5b4610SVincenzo Maffione static int netmap_fl_refill(iflib_rxq_t rxq, struct netmap_kring *kring, bool init); 759*17cec474SVincenzo Maffione static void iflib_netmap_timer(void *arg); 7602d873474SStephen Hurd 7614c7070dbSScott Long /* 7624c7070dbSScott Long * device-specific sysctl variables: 7634c7070dbSScott Long * 76491d546a0SConrad Meyer * iflib_crcstrip: 0: keep CRC in rx frames (default), 1: strip it. 7654c7070dbSScott Long * During regular operations the CRC is stripped, but on some 7664c7070dbSScott Long * hardware reception of frames not multiple of 64 is slower, 7674c7070dbSScott Long * so using crcstrip=0 helps in benchmarks. 7684c7070dbSScott Long * 76991d546a0SConrad Meyer * iflib_rx_miss, iflib_rx_miss_bufs: 7704c7070dbSScott Long * count packets that might be missed due to lost interrupts. 7714c7070dbSScott Long */ 7724c7070dbSScott Long SYSCTL_DECL(_dev_netmap); 7734c7070dbSScott Long /* 7744c7070dbSScott Long * The xl driver by default strips CRCs and we do not override it. 7754c7070dbSScott Long */ 7764c7070dbSScott Long 7774c7070dbSScott Long int iflib_crcstrip = 1; 7784c7070dbSScott Long SYSCTL_INT(_dev_netmap, OID_AUTO, iflib_crcstrip, 7791722eeacSMarius Strobl CTLFLAG_RW, &iflib_crcstrip, 1, "strip CRC on RX frames"); 7804c7070dbSScott Long 7814c7070dbSScott Long int iflib_rx_miss, iflib_rx_miss_bufs; 7824c7070dbSScott Long SYSCTL_INT(_dev_netmap, OID_AUTO, iflib_rx_miss, 7831722eeacSMarius Strobl CTLFLAG_RW, &iflib_rx_miss, 0, "potentially missed RX intr"); 78491d546a0SConrad Meyer SYSCTL_INT(_dev_netmap, OID_AUTO, iflib_rx_miss_bufs, 7851722eeacSMarius Strobl CTLFLAG_RW, &iflib_rx_miss_bufs, 0, "potentially missed RX intr bufs"); 7864c7070dbSScott Long 7874c7070dbSScott Long /* 7884c7070dbSScott Long * Register/unregister. We are already under netmap lock. 7894c7070dbSScott Long * Only called on the first register or the last unregister. 7904c7070dbSScott Long */ 7914c7070dbSScott Long static int 7924c7070dbSScott Long iflib_netmap_register(struct netmap_adapter *na, int onoff) 7934c7070dbSScott Long { 7941722eeacSMarius Strobl if_t ifp = na->ifp; 7954c7070dbSScott Long if_ctx_t ctx = ifp->if_softc; 79695246abbSSean Bruno int status; 7974c7070dbSScott Long 7984c7070dbSScott Long CTX_LOCK(ctx); 7994c7070dbSScott Long IFDI_INTR_DISABLE(ctx); 8004c7070dbSScott Long 8014c7070dbSScott Long /* Tell the stack that the interface is no longer active */ 8024c7070dbSScott Long ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 8034c7070dbSScott Long 8044c7070dbSScott Long if (!CTX_IS_VF(ctx)) 8051248952aSSean Bruno IFDI_CRCSTRIP_SET(ctx, onoff, iflib_crcstrip); 8064c7070dbSScott Long 8070a182b4cSVincenzo Maffione iflib_stop(ctx); 8080a182b4cSVincenzo Maffione 8090a182b4cSVincenzo Maffione /* 8100a182b4cSVincenzo Maffione * Enable (or disable) netmap flags, and intercept (or restore) 8110a182b4cSVincenzo Maffione * ifp->if_transmit. This is done once the device has been stopped 8120a182b4cSVincenzo Maffione * to prevent race conditions. 8130a182b4cSVincenzo Maffione */ 8144c7070dbSScott Long if (onoff) { 8154c7070dbSScott Long nm_set_native_flags(na); 8164c7070dbSScott Long } else { 8174c7070dbSScott Long nm_clear_native_flags(na); 8184c7070dbSScott Long } 8190a182b4cSVincenzo Maffione 82095246abbSSean Bruno iflib_init_locked(ctx); 8211248952aSSean Bruno IFDI_CRCSTRIP_SET(ctx, onoff, iflib_crcstrip); // XXX why twice ? 82295246abbSSean Bruno status = ifp->if_drv_flags & IFF_DRV_RUNNING ? 0 : 1; 82395246abbSSean Bruno if (status) 82495246abbSSean Bruno nm_clear_native_flags(na); 8254c7070dbSScott Long CTX_UNLOCK(ctx); 82695246abbSSean Bruno return (status); 8274c7070dbSScott Long } 8284c7070dbSScott Long 8292d873474SStephen Hurd static int 830de5b4610SVincenzo Maffione netmap_fl_refill(iflib_rxq_t rxq, struct netmap_kring *kring, bool init) 8312d873474SStephen Hurd { 8322d873474SStephen Hurd struct netmap_adapter *na = kring->na; 8332d873474SStephen Hurd u_int const lim = kring->nkr_num_slots - 1; 834de5b4610SVincenzo Maffione u_int nm_i = kring->nr_hwcur; 8352d873474SStephen Hurd struct netmap_ring *ring = kring->ring; 8362d873474SStephen Hurd bus_dmamap_t *map; 8372d873474SStephen Hurd struct if_rxd_update iru; 8382d873474SStephen Hurd if_ctx_t ctx = rxq->ifr_ctx; 8392d873474SStephen Hurd iflib_fl_t fl = &rxq->ifr_fl[0]; 840de5b4610SVincenzo Maffione u_int nic_i_first, nic_i; 841ae750d5cSVincenzo Maffione int i, n; 84264e6fc13SStephen Hurd #if IFLIB_DEBUG_COUNTERS 84364e6fc13SStephen Hurd int rf_count = 0; 84464e6fc13SStephen Hurd #endif 8452d873474SStephen Hurd 846de5b4610SVincenzo Maffione /* 847ae750d5cSVincenzo Maffione * This function is used both at initialization and in rxsync. 848ae750d5cSVincenzo Maffione * At initialization we need to prepare (with isc_rxd_refill()) 849ae750d5cSVincenzo Maffione * all the (N) netmap buffers in the ring, in such a way to keep 850ae750d5cSVincenzo Maffione * fl->ifl_pidx and kring->nr_hwcur in sync (except for 851ae750d5cSVincenzo Maffione * kring->nkr_hwofs); at rxsync time, both indexes point to the 852ae750d5cSVincenzo Maffione * next buffer to be refilled. 853ae750d5cSVincenzo Maffione * In any case we publish (with isc_rxd_flush()) up to 854ae750d5cSVincenzo Maffione * (fl->ifl_pidx - 1) % N (included), to avoid the NIC tail/prod 855ae750d5cSVincenzo Maffione * pointer to overrun the head/cons pointer, although this is 856ae750d5cSVincenzo Maffione * not necessary for some NICs (e.g. vmx). 857de5b4610SVincenzo Maffione */ 858ae750d5cSVincenzo Maffione if (__predict_false(init)) 859ae750d5cSVincenzo Maffione n = kring->nkr_num_slots; 860ae750d5cSVincenzo Maffione else { 861ae750d5cSVincenzo Maffione n = kring->rhead - nm_i; 862ae750d5cSVincenzo Maffione if (n == 0) 863ae750d5cSVincenzo Maffione return (0); /* Nothing to do. */ 864ae750d5cSVincenzo Maffione if (n < 0) 865ae750d5cSVincenzo Maffione n += kring->nkr_num_slots; 866de5b4610SVincenzo Maffione } 867530960beSVincenzo Maffione 868ae750d5cSVincenzo Maffione /* Start to refill from nr_hwcur, publishing n buffers. */ 8692d873474SStephen Hurd iru_init(&iru, rxq, 0 /* flid */); 8702d873474SStephen Hurd map = fl->ifl_sds.ifsd_map; 871ae750d5cSVincenzo Maffione nic_i = fl->ifl_pidx; 872ae750d5cSVincenzo Maffione MPASS(nic_i == netmap_idx_k2n(kring, nm_i)); 87364e6fc13SStephen Hurd DBG_COUNTER_INC(fl_refills); 874ae750d5cSVincenzo Maffione while (n > 0) { 87564e6fc13SStephen Hurd #if IFLIB_DEBUG_COUNTERS 87664e6fc13SStephen Hurd if (++rf_count == 9) 87764e6fc13SStephen Hurd DBG_COUNTER_INC(fl_refills_large); 87864e6fc13SStephen Hurd #endif 879530960beSVincenzo Maffione nic_i_first = nic_i; 880ae750d5cSVincenzo Maffione for (i = 0; n > 0 && i < IFLIB_MAX_RX_REFRESH; n--, i++) { 8812d873474SStephen Hurd struct netmap_slot *slot = &ring->slot[nm_i]; 882530960beSVincenzo Maffione void *addr = PNMB(na, slot, &fl->ifl_bus_addrs[i]); 8832d873474SStephen Hurd 884530960beSVincenzo Maffione MPASS(i < IFLIB_MAX_RX_REFRESH); 8852d873474SStephen Hurd 8862d873474SStephen Hurd if (addr == NETMAP_BUF_BASE(na)) /* bad buf */ 8872d873474SStephen Hurd return netmap_ring_reinit(kring); 8882d873474SStephen Hurd 889530960beSVincenzo Maffione fl->ifl_rxd_idxs[i] = nic_i; 890530960beSVincenzo Maffione 89195dcf343SMarius Strobl if (__predict_false(init)) { 89295dcf343SMarius Strobl netmap_load_map(na, fl->ifl_buf_tag, 89395dcf343SMarius Strobl map[nic_i], addr); 89495dcf343SMarius Strobl } else if (slot->flags & NS_BUF_CHANGED) { 8952d873474SStephen Hurd /* buffer has changed, reload map */ 89695dcf343SMarius Strobl netmap_reload_map(na, fl->ifl_buf_tag, 89795dcf343SMarius Strobl map[nic_i], addr); 8982d873474SStephen Hurd } 899530960beSVincenzo Maffione bus_dmamap_sync(fl->ifl_buf_tag, map[nic_i], 900530960beSVincenzo Maffione BUS_DMASYNC_PREREAD); 9012d873474SStephen Hurd slot->flags &= ~NS_BUF_CHANGED; 9022d873474SStephen Hurd 9032d873474SStephen Hurd nm_i = nm_next(nm_i, lim); 904530960beSVincenzo Maffione nic_i = nm_next(nic_i, lim); 905530960beSVincenzo Maffione } 9062d873474SStephen Hurd 907530960beSVincenzo Maffione iru.iru_pidx = nic_i_first; 908530960beSVincenzo Maffione iru.iru_count = i; 9092d873474SStephen Hurd ctx->isc_rxd_refill(ctx->ifc_softc, &iru); 9102d873474SStephen Hurd } 911ae750d5cSVincenzo Maffione fl->ifl_pidx = nic_i; 912ae750d5cSVincenzo Maffione MPASS(!init || nm_i == 0); 913ae750d5cSVincenzo Maffione MPASS(nm_i == kring->rhead); 914ae750d5cSVincenzo Maffione kring->nr_hwcur = nm_i; 9152d873474SStephen Hurd 9162d873474SStephen Hurd bus_dmamap_sync(fl->ifl_ifdi->idi_tag, fl->ifl_ifdi->idi_map, 9172d873474SStephen Hurd BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 918de5b4610SVincenzo Maffione ctx->isc_rxd_flush(ctx->ifc_softc, rxq->ifr_id, fl->ifl_id, 919de5b4610SVincenzo Maffione nm_prev(nic_i, lim)); 92064e6fc13SStephen Hurd DBG_COUNTER_INC(rxd_flush); 921530960beSVincenzo Maffione 9222d873474SStephen Hurd return (0); 9232d873474SStephen Hurd } 9242d873474SStephen Hurd 925*17cec474SVincenzo Maffione #define NETMAP_TX_TIMER_US 90 926*17cec474SVincenzo Maffione 9274c7070dbSScott Long /* 9284c7070dbSScott Long * Reconcile kernel and user view of the transmit ring. 9294c7070dbSScott Long * 9304c7070dbSScott Long * All information is in the kring. 9314c7070dbSScott Long * Userspace wants to send packets up to the one before kring->rhead, 9324c7070dbSScott Long * kernel knows kring->nr_hwcur is the first unsent packet. 9334c7070dbSScott Long * 9344c7070dbSScott Long * Here we push packets out (as many as possible), and possibly 9354c7070dbSScott Long * reclaim buffers from previously completed transmission. 9364c7070dbSScott Long * 9374c7070dbSScott Long * The caller (netmap) guarantees that there is only one instance 9384c7070dbSScott Long * running at any time. Any interference with other driver 9394c7070dbSScott Long * methods should be handled by the individual drivers. 9404c7070dbSScott Long */ 9414c7070dbSScott Long static int 9424c7070dbSScott Long iflib_netmap_txsync(struct netmap_kring *kring, int flags) 9434c7070dbSScott Long { 9444c7070dbSScott Long struct netmap_adapter *na = kring->na; 9451722eeacSMarius Strobl if_t ifp = na->ifp; 9464c7070dbSScott Long struct netmap_ring *ring = kring->ring; 947dd7fbcf1SStephen Hurd u_int nm_i; /* index into the netmap kring */ 9484c7070dbSScott Long u_int nic_i; /* index into the NIC ring */ 9494c7070dbSScott Long u_int n; 9504c7070dbSScott Long u_int const lim = kring->nkr_num_slots - 1; 9514c7070dbSScott Long u_int const head = kring->rhead; 9524c7070dbSScott Long struct if_pkt_info pi; 9534c7070dbSScott Long 9544c7070dbSScott Long /* 9554c7070dbSScott Long * interrupts on every tx packet are expensive so request 9564c7070dbSScott Long * them every half ring, or where NS_REPORT is set 9574c7070dbSScott Long */ 9584c7070dbSScott Long u_int report_frequency = kring->nkr_num_slots >> 1; 9594c7070dbSScott Long /* device-specific */ 9604c7070dbSScott Long if_ctx_t ctx = ifp->if_softc; 9614c7070dbSScott Long iflib_txq_t txq = &ctx->ifc_txqs[kring->ring_id]; 9624c7070dbSScott Long 96395dcf343SMarius Strobl bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map, 9644c7070dbSScott Long BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 9654c7070dbSScott Long 9664c7070dbSScott Long /* 9674c7070dbSScott Long * First part: process new packets to send. 968dd7fbcf1SStephen Hurd * nm_i is the current index in the netmap kring, 9694c7070dbSScott Long * nic_i is the corresponding index in the NIC ring. 9704c7070dbSScott Long * 9714c7070dbSScott Long * If we have packets to send (nm_i != head) 9724c7070dbSScott Long * iterate over the netmap ring, fetch length and update 9734c7070dbSScott Long * the corresponding slot in the NIC ring. Some drivers also 9744c7070dbSScott Long * need to update the buffer's physical address in the NIC slot 9754c7070dbSScott Long * even NS_BUF_CHANGED is not set (PNMB computes the addresses). 9764c7070dbSScott Long * 9774c7070dbSScott Long * The netmap_reload_map() calls is especially expensive, 9784c7070dbSScott Long * even when (as in this case) the tag is 0, so do only 9794c7070dbSScott Long * when the buffer has actually changed. 9804c7070dbSScott Long * 9814c7070dbSScott Long * If possible do not set the report/intr bit on all slots, 9824c7070dbSScott Long * but only a few times per ring or when NS_REPORT is set. 9834c7070dbSScott Long * 9844c7070dbSScott Long * Finally, on 10G and faster drivers, it might be useful 9854c7070dbSScott Long * to prefetch the next slot and txr entry. 9864c7070dbSScott Long */ 9874c7070dbSScott Long 988dd7fbcf1SStephen Hurd nm_i = kring->nr_hwcur; 9895ee36c68SStephen Hurd if (nm_i != head) { /* we have new packets to send */ 99095246abbSSean Bruno pkt_info_zero(&pi); 99195246abbSSean Bruno pi.ipi_segs = txq->ift_segs; 99295246abbSSean Bruno pi.ipi_qsidx = kring->ring_id; 9934c7070dbSScott Long nic_i = netmap_idx_k2n(kring, nm_i); 9944c7070dbSScott Long 9954c7070dbSScott Long __builtin_prefetch(&ring->slot[nm_i]); 9964c7070dbSScott Long __builtin_prefetch(&txq->ift_sds.ifsd_m[nic_i]); 9974c7070dbSScott Long __builtin_prefetch(&txq->ift_sds.ifsd_map[nic_i]); 9984c7070dbSScott Long 9994c7070dbSScott Long for (n = 0; nm_i != head; n++) { 10004c7070dbSScott Long struct netmap_slot *slot = &ring->slot[nm_i]; 10014c7070dbSScott Long u_int len = slot->len; 10020a1b74a3SSean Bruno uint64_t paddr; 10034c7070dbSScott Long void *addr = PNMB(na, slot, &paddr); 10044c7070dbSScott Long int flags = (slot->flags & NS_REPORT || 10054c7070dbSScott Long nic_i == 0 || nic_i == report_frequency) ? 10064c7070dbSScott Long IPI_TX_INTR : 0; 10074c7070dbSScott Long 10084c7070dbSScott Long /* device-specific */ 100995246abbSSean Bruno pi.ipi_len = len; 101095246abbSSean Bruno pi.ipi_segs[0].ds_addr = paddr; 101195246abbSSean Bruno pi.ipi_segs[0].ds_len = len; 101295246abbSSean Bruno pi.ipi_nsegs = 1; 101395246abbSSean Bruno pi.ipi_ndescs = 0; 10144c7070dbSScott Long pi.ipi_pidx = nic_i; 10154c7070dbSScott Long pi.ipi_flags = flags; 10164c7070dbSScott Long 10174c7070dbSScott Long /* Fill the slot in the NIC ring. */ 10184c7070dbSScott Long ctx->isc_txd_encap(ctx->ifc_softc, &pi); 101964e6fc13SStephen Hurd DBG_COUNTER_INC(tx_encap); 10204c7070dbSScott Long 10214c7070dbSScott Long /* prefetch for next round */ 10224c7070dbSScott Long __builtin_prefetch(&ring->slot[nm_i + 1]); 10234c7070dbSScott Long __builtin_prefetch(&txq->ift_sds.ifsd_m[nic_i + 1]); 10244c7070dbSScott Long __builtin_prefetch(&txq->ift_sds.ifsd_map[nic_i + 1]); 10254c7070dbSScott Long 10264c7070dbSScott Long NM_CHECK_ADDR_LEN(na, addr, len); 10274c7070dbSScott Long 10284c7070dbSScott Long if (slot->flags & NS_BUF_CHANGED) { 10294c7070dbSScott Long /* buffer has changed, reload map */ 1030bfce461eSMarius Strobl netmap_reload_map(na, txq->ift_buf_tag, 1031bfce461eSMarius Strobl txq->ift_sds.ifsd_map[nic_i], addr); 10324c7070dbSScott Long } 10334c7070dbSScott Long /* make sure changes to the buffer are synced */ 103495dcf343SMarius Strobl bus_dmamap_sync(txq->ift_buf_tag, 103595dcf343SMarius Strobl txq->ift_sds.ifsd_map[nic_i], 10364c7070dbSScott Long BUS_DMASYNC_PREWRITE); 103795dcf343SMarius Strobl 103895246abbSSean Bruno slot->flags &= ~(NS_REPORT | NS_BUF_CHANGED); 10394c7070dbSScott Long nm_i = nm_next(nm_i, lim); 10404c7070dbSScott Long nic_i = nm_next(nic_i, lim); 10414c7070dbSScott Long } 1042dd7fbcf1SStephen Hurd kring->nr_hwcur = nm_i; 10434c7070dbSScott Long 10444c7070dbSScott Long /* synchronize the NIC ring */ 104595dcf343SMarius Strobl bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map, 10464c7070dbSScott Long BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 10474c7070dbSScott Long 10484c7070dbSScott Long /* (re)start the tx unit up to slot nic_i (excluded) */ 10494c7070dbSScott Long ctx->isc_txd_flush(ctx->ifc_softc, txq->ift_id, nic_i); 10504c7070dbSScott Long } 10514c7070dbSScott Long 10524c7070dbSScott Long /* 10534c7070dbSScott Long * Second part: reclaim buffers for completed transmissions. 10545ee36c68SStephen Hurd * 10555ee36c68SStephen Hurd * If there are unclaimed buffers, attempt to reclaim them. 1056*17cec474SVincenzo Maffione * If we don't manage to reclaim them all, and TX IRQs are not in use, 1057*17cec474SVincenzo Maffione * trigger a per-tx-queue timer to try again later. 10584c7070dbSScott Long */ 1059dd7fbcf1SStephen Hurd if (kring->nr_hwtail != nm_prev(kring->nr_hwcur, lim)) { 10604c7070dbSScott Long if (iflib_tx_credits_update(ctx, txq)) { 10614c7070dbSScott Long /* some tx completed, increment avail */ 10624c7070dbSScott Long nic_i = txq->ift_cidx_processed; 10634c7070dbSScott Long kring->nr_hwtail = nm_prev(netmap_idx_n2k(kring, nic_i), lim); 10644c7070dbSScott Long } 10655ee36c68SStephen Hurd } 1066*17cec474SVincenzo Maffione 1067dd7fbcf1SStephen Hurd if (!(ctx->ifc_flags & IFC_NETMAP_TX_IRQ)) 1068dd7fbcf1SStephen Hurd if (kring->nr_hwtail != nm_prev(kring->nr_hwcur, lim)) { 1069*17cec474SVincenzo Maffione callout_reset_sbt(&txq->ift_netmap_timer, 1070*17cec474SVincenzo Maffione NETMAP_TX_TIMER_US * SBT_1US, SBT_1US, 1071*17cec474SVincenzo Maffione iflib_netmap_timer, txq, txq->ift_netmap_timer.c_cpu); 10725ee36c68SStephen Hurd } 10734c7070dbSScott Long return (0); 10744c7070dbSScott Long } 10754c7070dbSScott Long 10764c7070dbSScott Long /* 10774c7070dbSScott Long * Reconcile kernel and user view of the receive ring. 10784c7070dbSScott Long * Same as for the txsync, this routine must be efficient. 10794c7070dbSScott Long * The caller guarantees a single invocations, but races against 10804c7070dbSScott Long * the rest of the driver should be handled here. 10814c7070dbSScott Long * 10824c7070dbSScott Long * On call, kring->rhead is the first packet that userspace wants 10834c7070dbSScott Long * to keep, and kring->rcur is the wakeup point. 10844c7070dbSScott Long * The kernel has previously reported packets up to kring->rtail. 10854c7070dbSScott Long * 10864c7070dbSScott Long * If (flags & NAF_FORCE_READ) also check for incoming packets irrespective 10874c7070dbSScott Long * of whether or not we received an interrupt. 10884c7070dbSScott Long */ 10894c7070dbSScott Long static int 10904c7070dbSScott Long iflib_netmap_rxsync(struct netmap_kring *kring, int flags) 10914c7070dbSScott Long { 10924c7070dbSScott Long struct netmap_adapter *na = kring->na; 10934c7070dbSScott Long struct netmap_ring *ring = kring->ring; 10941722eeacSMarius Strobl if_t ifp = na->ifp; 109595246abbSSean Bruno uint32_t nm_i; /* index into the netmap ring */ 10962d873474SStephen Hurd uint32_t nic_i; /* index into the NIC ring */ 1097ee07345dSVincenzo Maffione u_int n; 10984c7070dbSScott Long u_int const lim = kring->nkr_num_slots - 1; 10994c7070dbSScott Long int force_update = (flags & NAF_FORCE_READ) || kring->nr_kflags & NKR_PENDINTR; 110095246abbSSean Bruno 11014c7070dbSScott Long if_ctx_t ctx = ifp->if_softc; 11026d84e76aSVincenzo Maffione if_shared_ctx_t sctx = ctx->ifc_sctx; 11036d84e76aSVincenzo Maffione if_softc_ctx_t scctx = &ctx->ifc_softc_ctx; 11044c7070dbSScott Long iflib_rxq_t rxq = &ctx->ifc_rxqs[kring->ring_id]; 1105ee07345dSVincenzo Maffione iflib_fl_t fl = &rxq->ifr_fl[0]; 1106ee07345dSVincenzo Maffione struct if_rxd_info ri; 11076d84e76aSVincenzo Maffione qidx_t *cidxp; 1108ee07345dSVincenzo Maffione 110995dcf343SMarius Strobl /* 1110ee07345dSVincenzo Maffione * netmap only uses free list 0, to avoid out of order consumption 1111ee07345dSVincenzo Maffione * of receive buffers 111295dcf343SMarius Strobl */ 111395dcf343SMarius Strobl 111495dcf343SMarius Strobl bus_dmamap_sync(fl->ifl_ifdi->idi_tag, fl->ifl_ifdi->idi_map, 11154c7070dbSScott Long BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 111695dcf343SMarius Strobl 11174c7070dbSScott Long /* 11184c7070dbSScott Long * First part: import newly received packets. 11194c7070dbSScott Long * 11204c7070dbSScott Long * nm_i is the index of the next free slot in the netmap ring, 11216d84e76aSVincenzo Maffione * nic_i is the index of the next received packet in the NIC ring 11226d84e76aSVincenzo Maffione * (or in the free list 0 if IFLIB_HAS_RXCQ is set), and they may 11236d84e76aSVincenzo Maffione * differ in case if_init() has been called while 11244c7070dbSScott Long * in netmap mode. For the receive ring we have 11254c7070dbSScott Long * 11266d84e76aSVincenzo Maffione * nic_i = fl->ifl_cidx; 11274c7070dbSScott Long * nm_i = kring->nr_hwtail (previous) 11284c7070dbSScott Long * and 11294c7070dbSScott Long * nm_i == (nic_i + kring->nkr_hwofs) % ring_size 11304c7070dbSScott Long * 11316d84e76aSVincenzo Maffione * fl->ifl_cidx is set to 0 on a ring reinit 11324c7070dbSScott Long */ 11334c7070dbSScott Long if (netmap_no_pendintr || force_update) { 11340ff21267SVincenzo Maffione uint32_t hwtail_lim = nm_prev(kring->nr_hwcur, lim); 11356d84e76aSVincenzo Maffione bool have_rxcq = sctx->isc_flags & IFLIB_HAS_RXCQ; 11364c7070dbSScott Long int crclen = iflib_crcstrip ? 0 : 4; 11374c7070dbSScott Long int error, avail; 11384c7070dbSScott Long 11396d84e76aSVincenzo Maffione /* 11406d84e76aSVincenzo Maffione * For the free list consumer index, we use the same 11416d84e76aSVincenzo Maffione * logic as in iflib_rxeof(). 11426d84e76aSVincenzo Maffione */ 11436d84e76aSVincenzo Maffione if (have_rxcq) 11446d84e76aSVincenzo Maffione cidxp = &rxq->ifr_cq_cidx; 11456d84e76aSVincenzo Maffione else 11466d84e76aSVincenzo Maffione cidxp = &fl->ifl_cidx; 11476d84e76aSVincenzo Maffione avail = ctx->isc_rxd_available(ctx->ifc_softc, 11486d84e76aSVincenzo Maffione rxq->ifr_id, *cidxp, USHRT_MAX); 11496d84e76aSVincenzo Maffione 11504c7070dbSScott Long nic_i = fl->ifl_cidx; 11514c7070dbSScott Long nm_i = netmap_idx_n2k(kring, nic_i); 1152de5b4610SVincenzo Maffione MPASS(nm_i == kring->nr_hwtail); 11530ff21267SVincenzo Maffione for (n = 0; avail > 0 && nm_i != hwtail_lim; n++, avail--) { 1154ab2e3f79SStephen Hurd rxd_info_zero(&ri); 1155ab2e3f79SStephen Hurd ri.iri_frags = rxq->ifr_frags; 1156ab2e3f79SStephen Hurd ri.iri_qsidx = kring->ring_id; 1157ab2e3f79SStephen Hurd ri.iri_ifp = ctx->ifc_ifp; 11586d84e76aSVincenzo Maffione ri.iri_cidx = *cidxp; 115995246abbSSean Bruno 1160ab2e3f79SStephen Hurd error = ctx->isc_rxd_pkt_get(ctx->ifc_softc, &ri); 1161ab2e3f79SStephen Hurd ring->slot[nm_i].len = error ? 0 : ri.iri_len - crclen; 11627cb7c6e3SNavdeep Parhar ring->slot[nm_i].flags = 0; 11636d84e76aSVincenzo Maffione if (have_rxcq) { 11646d84e76aSVincenzo Maffione *cidxp = ri.iri_cidx; 11656d84e76aSVincenzo Maffione while (*cidxp >= scctx->isc_nrxd[0]) 11666d84e76aSVincenzo Maffione *cidxp -= scctx->isc_nrxd[0]; 11676d84e76aSVincenzo Maffione } 116895dcf343SMarius Strobl bus_dmamap_sync(fl->ifl_buf_tag, 1169e035717eSSean Bruno fl->ifl_sds.ifsd_map[nic_i], BUS_DMASYNC_POSTREAD); 11704c7070dbSScott Long nm_i = nm_next(nm_i, lim); 11716d84e76aSVincenzo Maffione fl->ifl_cidx = nic_i = nm_next(nic_i, lim); 11724c7070dbSScott Long } 11734c7070dbSScott Long if (n) { /* update the state variables */ 11744c7070dbSScott Long if (netmap_no_pendintr && !force_update) { 11754c7070dbSScott Long /* diagnostics */ 11764c7070dbSScott Long iflib_rx_miss ++; 11774c7070dbSScott Long iflib_rx_miss_bufs += n; 11784c7070dbSScott Long } 1179dd7fbcf1SStephen Hurd kring->nr_hwtail = nm_i; 11804c7070dbSScott Long } 11814c7070dbSScott Long kring->nr_kflags &= ~NKR_PENDINTR; 11824c7070dbSScott Long } 11834c7070dbSScott Long /* 11844c7070dbSScott Long * Second part: skip past packets that userspace has released. 11854c7070dbSScott Long * (kring->nr_hwcur to head excluded), 11864c7070dbSScott Long * and make the buffers available for reception. 11874c7070dbSScott Long * As usual nm_i is the index in the netmap ring, 11884c7070dbSScott Long * nic_i is the index in the NIC ring, and 11894c7070dbSScott Long * nm_i == (nic_i + kring->nkr_hwofs) % ring_size 11904c7070dbSScott Long */ 1191de5b4610SVincenzo Maffione netmap_fl_refill(rxq, kring, false); 119295246abbSSean Bruno 1193de5b4610SVincenzo Maffione return (0); 11944c7070dbSScott Long } 11954c7070dbSScott Long 119695246abbSSean Bruno static void 119795246abbSSean Bruno iflib_netmap_intr(struct netmap_adapter *na, int onoff) 119895246abbSSean Bruno { 11991722eeacSMarius Strobl if_ctx_t ctx = na->ifp->if_softc; 120095246abbSSean Bruno 1201ab2e3f79SStephen Hurd CTX_LOCK(ctx); 120295246abbSSean Bruno if (onoff) { 120395246abbSSean Bruno IFDI_INTR_ENABLE(ctx); 120495246abbSSean Bruno } else { 120595246abbSSean Bruno IFDI_INTR_DISABLE(ctx); 120695246abbSSean Bruno } 1207ab2e3f79SStephen Hurd CTX_UNLOCK(ctx); 120895246abbSSean Bruno } 120995246abbSSean Bruno 12104c7070dbSScott Long static int 12114c7070dbSScott Long iflib_netmap_attach(if_ctx_t ctx) 12124c7070dbSScott Long { 12134c7070dbSScott Long struct netmap_adapter na; 12144c7070dbSScott Long 12154c7070dbSScott Long bzero(&na, sizeof(na)); 12164c7070dbSScott Long 12174c7070dbSScott Long na.ifp = ctx->ifc_ifp; 12184c7070dbSScott Long na.na_flags = NAF_BDG_MAYSLEEP; 12194c7070dbSScott Long MPASS(ctx->ifc_softc_ctx.isc_ntxqsets); 12204c7070dbSScott Long MPASS(ctx->ifc_softc_ctx.isc_nrxqsets); 12214c7070dbSScott Long 1222ac11d857SVincenzo Maffione na.num_tx_desc = iflib_num_tx_descs(ctx); 1223ac11d857SVincenzo Maffione na.num_rx_desc = iflib_num_rx_descs(ctx); 12244c7070dbSScott Long na.nm_txsync = iflib_netmap_txsync; 12254c7070dbSScott Long na.nm_rxsync = iflib_netmap_rxsync; 12264c7070dbSScott Long na.nm_register = iflib_netmap_register; 122795246abbSSean Bruno na.nm_intr = iflib_netmap_intr; 12284c7070dbSScott Long na.num_tx_rings = ctx->ifc_softc_ctx.isc_ntxqsets; 12294c7070dbSScott Long na.num_rx_rings = ctx->ifc_softc_ctx.isc_nrxqsets; 12304c7070dbSScott Long return (netmap_attach(&na)); 12314c7070dbSScott Long } 12324c7070dbSScott Long 1233d8b2d26bSVincenzo Maffione static int 12344c7070dbSScott Long iflib_netmap_txq_init(if_ctx_t ctx, iflib_txq_t txq) 12354c7070dbSScott Long { 12364c7070dbSScott Long struct netmap_adapter *na = NA(ctx->ifc_ifp); 12374c7070dbSScott Long struct netmap_slot *slot; 12384c7070dbSScott Long 12394c7070dbSScott Long slot = netmap_reset(na, NR_TX, txq->ift_id, 0); 1240e099b90bSPedro F. Giffuni if (slot == NULL) 1241d8b2d26bSVincenzo Maffione return (0); 124223ac9029SStephen Hurd for (int i = 0; i < ctx->ifc_softc_ctx.isc_ntxd[0]; i++) { 12434c7070dbSScott Long /* 12444c7070dbSScott Long * In netmap mode, set the map for the packet buffer. 12454c7070dbSScott Long * NOTE: Some drivers (not this one) also need to set 12464c7070dbSScott Long * the physical buffer address in the NIC ring. 12474c7070dbSScott Long * netmap_idx_n2k() maps a nic index, i, into the corresponding 12484c7070dbSScott Long * netmap slot index, si 12494c7070dbSScott Long */ 12502ff91c17SVincenzo Maffione int si = netmap_idx_n2k(na->tx_rings[txq->ift_id], i); 1251bfce461eSMarius Strobl netmap_load_map(na, txq->ift_buf_tag, txq->ift_sds.ifsd_map[i], 1252bfce461eSMarius Strobl NMB(na, slot + si)); 12534c7070dbSScott Long } 1254d8b2d26bSVincenzo Maffione return (1); 12554c7070dbSScott Long } 12562d873474SStephen Hurd 1257d8b2d26bSVincenzo Maffione static int 12584c7070dbSScott Long iflib_netmap_rxq_init(if_ctx_t ctx, iflib_rxq_t rxq) 12594c7070dbSScott Long { 12604c7070dbSScott Long struct netmap_adapter *na = NA(ctx->ifc_ifp); 1261d8b2d26bSVincenzo Maffione struct netmap_kring *kring; 12624c7070dbSScott Long struct netmap_slot *slot; 12634c7070dbSScott Long 12644c7070dbSScott Long slot = netmap_reset(na, NR_RX, rxq->ifr_id, 0); 1265e099b90bSPedro F. Giffuni if (slot == NULL) 1266d8b2d26bSVincenzo Maffione return (0); 1267d8b2d26bSVincenzo Maffione kring = na->rx_rings[rxq->ifr_id]; 1268de5b4610SVincenzo Maffione netmap_fl_refill(rxq, kring, true); 1269d8b2d26bSVincenzo Maffione return (1); 12704c7070dbSScott Long } 12714c7070dbSScott Long 1272dd7fbcf1SStephen Hurd static void 1273*17cec474SVincenzo Maffione iflib_netmap_timer(void *arg) 1274dd7fbcf1SStephen Hurd { 1275*17cec474SVincenzo Maffione iflib_txq_t txq = arg; 1276*17cec474SVincenzo Maffione if_ctx_t ctx = txq->ift_ctx; 1277dd7fbcf1SStephen Hurd 1278*17cec474SVincenzo Maffione /* 1279*17cec474SVincenzo Maffione * Wake up the netmap application, to give it a chance to 1280*17cec474SVincenzo Maffione * call txsync and reclaim more completed TX buffers. 1281*17cec474SVincenzo Maffione */ 1282*17cec474SVincenzo Maffione netmap_tx_irq(ctx->ifc_ifp, txq->ift_id); 1283dd7fbcf1SStephen Hurd } 1284dd7fbcf1SStephen Hurd 12854c7070dbSScott Long #define iflib_netmap_detach(ifp) netmap_detach(ifp) 12864c7070dbSScott Long 12874c7070dbSScott Long #else 1288d8b2d26bSVincenzo Maffione #define iflib_netmap_txq_init(ctx, txq) (0) 1289d8b2d26bSVincenzo Maffione #define iflib_netmap_rxq_init(ctx, rxq) (0) 12904c7070dbSScott Long #define iflib_netmap_detach(ifp) 12914c7070dbSScott Long 12924c7070dbSScott Long #define iflib_netmap_attach(ctx) (0) 12934c7070dbSScott Long #define netmap_rx_irq(ifp, qid, budget) (0) 12944c7070dbSScott Long #endif 12954c7070dbSScott Long 12964c7070dbSScott Long #if defined(__i386__) || defined(__amd64__) 12974c7070dbSScott Long static __inline void 12984c7070dbSScott Long prefetch(void *x) 12994c7070dbSScott Long { 13004c7070dbSScott Long __asm volatile("prefetcht0 %0" :: "m" (*(unsigned long *)x)); 13014c7070dbSScott Long } 13023429c02fSStephen Hurd static __inline void 13033429c02fSStephen Hurd prefetch2cachelines(void *x) 13043429c02fSStephen Hurd { 13053429c02fSStephen Hurd __asm volatile("prefetcht0 %0" :: "m" (*(unsigned long *)x)); 13063429c02fSStephen Hurd #if (CACHE_LINE_SIZE < 128) 13073429c02fSStephen Hurd __asm volatile("prefetcht0 %0" :: "m" (*(((unsigned long *)x)+CACHE_LINE_SIZE/(sizeof(unsigned long))))); 13083429c02fSStephen Hurd #endif 13093429c02fSStephen Hurd } 13104c7070dbSScott Long #else 13114c7070dbSScott Long #define prefetch(x) 13123429c02fSStephen Hurd #define prefetch2cachelines(x) 13134c7070dbSScott Long #endif 13144c7070dbSScott Long 13154c7070dbSScott Long static void 131610e0d938SStephen Hurd iru_init(if_rxd_update_t iru, iflib_rxq_t rxq, uint8_t flid) 131710e0d938SStephen Hurd { 131810e0d938SStephen Hurd iflib_fl_t fl; 131910e0d938SStephen Hurd 132010e0d938SStephen Hurd fl = &rxq->ifr_fl[flid]; 132110e0d938SStephen Hurd iru->iru_paddrs = fl->ifl_bus_addrs; 132210e0d938SStephen Hurd iru->iru_idxs = fl->ifl_rxd_idxs; 132310e0d938SStephen Hurd iru->iru_qsidx = rxq->ifr_id; 132410e0d938SStephen Hurd iru->iru_buf_size = fl->ifl_buf_size; 132510e0d938SStephen Hurd iru->iru_flidx = fl->ifl_id; 132610e0d938SStephen Hurd } 132710e0d938SStephen Hurd 132810e0d938SStephen Hurd static void 13294c7070dbSScott Long _iflib_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int err) 13304c7070dbSScott Long { 13314c7070dbSScott Long if (err) 13324c7070dbSScott Long return; 13334c7070dbSScott Long *(bus_addr_t *) arg = segs[0].ds_addr; 13344c7070dbSScott Long } 13354c7070dbSScott Long 13364c7070dbSScott Long int 13378f82136aSPatrick Kelsey iflib_dma_alloc_align(if_ctx_t ctx, int size, int align, iflib_dma_info_t dma, int mapflags) 13384c7070dbSScott Long { 13394c7070dbSScott Long int err; 13404c7070dbSScott Long device_t dev = ctx->ifc_dev; 13414c7070dbSScott Long 13424c7070dbSScott Long err = bus_dma_tag_create(bus_get_dma_tag(dev), /* parent */ 13438f82136aSPatrick Kelsey align, 0, /* alignment, bounds */ 13444c7070dbSScott Long BUS_SPACE_MAXADDR, /* lowaddr */ 13454c7070dbSScott Long BUS_SPACE_MAXADDR, /* highaddr */ 13464c7070dbSScott Long NULL, NULL, /* filter, filterarg */ 13474c7070dbSScott Long size, /* maxsize */ 13484c7070dbSScott Long 1, /* nsegments */ 13494c7070dbSScott Long size, /* maxsegsize */ 13504c7070dbSScott Long BUS_DMA_ALLOCNOW, /* flags */ 13514c7070dbSScott Long NULL, /* lockfunc */ 13524c7070dbSScott Long NULL, /* lockarg */ 13534c7070dbSScott Long &dma->idi_tag); 13544c7070dbSScott Long if (err) { 13554c7070dbSScott Long device_printf(dev, 13564c7070dbSScott Long "%s: bus_dma_tag_create failed: %d\n", 13574c7070dbSScott Long __func__, err); 13584c7070dbSScott Long goto fail_0; 13594c7070dbSScott Long } 13604c7070dbSScott Long 13614c7070dbSScott Long err = bus_dmamem_alloc(dma->idi_tag, (void**) &dma->idi_vaddr, 13624c7070dbSScott Long BUS_DMA_NOWAIT | BUS_DMA_COHERENT | BUS_DMA_ZERO, &dma->idi_map); 13634c7070dbSScott Long if (err) { 13644c7070dbSScott Long device_printf(dev, 13654c7070dbSScott Long "%s: bus_dmamem_alloc(%ju) failed: %d\n", 13664c7070dbSScott Long __func__, (uintmax_t)size, err); 13674c7070dbSScott Long goto fail_1; 13684c7070dbSScott Long } 13694c7070dbSScott Long 13704c7070dbSScott Long dma->idi_paddr = IF_BAD_DMA; 13714c7070dbSScott Long err = bus_dmamap_load(dma->idi_tag, dma->idi_map, dma->idi_vaddr, 13724c7070dbSScott Long size, _iflib_dmamap_cb, &dma->idi_paddr, mapflags | BUS_DMA_NOWAIT); 13734c7070dbSScott Long if (err || dma->idi_paddr == IF_BAD_DMA) { 13744c7070dbSScott Long device_printf(dev, 13754c7070dbSScott Long "%s: bus_dmamap_load failed: %d\n", 13764c7070dbSScott Long __func__, err); 13774c7070dbSScott Long goto fail_2; 13784c7070dbSScott Long } 13794c7070dbSScott Long 13804c7070dbSScott Long dma->idi_size = size; 13814c7070dbSScott Long return (0); 13824c7070dbSScott Long 13834c7070dbSScott Long fail_2: 13844c7070dbSScott Long bus_dmamem_free(dma->idi_tag, dma->idi_vaddr, dma->idi_map); 13854c7070dbSScott Long fail_1: 13864c7070dbSScott Long bus_dma_tag_destroy(dma->idi_tag); 13874c7070dbSScott Long fail_0: 13884c7070dbSScott Long dma->idi_tag = NULL; 13894c7070dbSScott Long 13904c7070dbSScott Long return (err); 13914c7070dbSScott Long } 13924c7070dbSScott Long 13934c7070dbSScott Long int 13948f82136aSPatrick Kelsey iflib_dma_alloc(if_ctx_t ctx, int size, iflib_dma_info_t dma, int mapflags) 13958f82136aSPatrick Kelsey { 13968f82136aSPatrick Kelsey if_shared_ctx_t sctx = ctx->ifc_sctx; 13978f82136aSPatrick Kelsey 13988f82136aSPatrick Kelsey KASSERT(sctx->isc_q_align != 0, ("alignment value not initialized")); 13998f82136aSPatrick Kelsey 14008f82136aSPatrick Kelsey return (iflib_dma_alloc_align(ctx, size, sctx->isc_q_align, dma, mapflags)); 14018f82136aSPatrick Kelsey } 14028f82136aSPatrick Kelsey 14038f82136aSPatrick Kelsey int 14044c7070dbSScott Long iflib_dma_alloc_multi(if_ctx_t ctx, int *sizes, iflib_dma_info_t *dmalist, int mapflags, int count) 14054c7070dbSScott Long { 14064c7070dbSScott Long int i, err; 14074c7070dbSScott Long iflib_dma_info_t *dmaiter; 14084c7070dbSScott Long 14094c7070dbSScott Long dmaiter = dmalist; 14104c7070dbSScott Long for (i = 0; i < count; i++, dmaiter++) { 14114c7070dbSScott Long if ((err = iflib_dma_alloc(ctx, sizes[i], *dmaiter, mapflags)) != 0) 14124c7070dbSScott Long break; 14134c7070dbSScott Long } 14144c7070dbSScott Long if (err) 14154c7070dbSScott Long iflib_dma_free_multi(dmalist, i); 14164c7070dbSScott Long return (err); 14174c7070dbSScott Long } 14184c7070dbSScott Long 14194c7070dbSScott Long void 14204c7070dbSScott Long iflib_dma_free(iflib_dma_info_t dma) 14214c7070dbSScott Long { 14224c7070dbSScott Long if (dma->idi_tag == NULL) 14234c7070dbSScott Long return; 14244c7070dbSScott Long if (dma->idi_paddr != IF_BAD_DMA) { 14254c7070dbSScott Long bus_dmamap_sync(dma->idi_tag, dma->idi_map, 14264c7070dbSScott Long BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 14274c7070dbSScott Long bus_dmamap_unload(dma->idi_tag, dma->idi_map); 14284c7070dbSScott Long dma->idi_paddr = IF_BAD_DMA; 14294c7070dbSScott Long } 14304c7070dbSScott Long if (dma->idi_vaddr != NULL) { 14314c7070dbSScott Long bus_dmamem_free(dma->idi_tag, dma->idi_vaddr, dma->idi_map); 14324c7070dbSScott Long dma->idi_vaddr = NULL; 14334c7070dbSScott Long } 14344c7070dbSScott Long bus_dma_tag_destroy(dma->idi_tag); 14354c7070dbSScott Long dma->idi_tag = NULL; 14364c7070dbSScott Long } 14374c7070dbSScott Long 14384c7070dbSScott Long void 14394c7070dbSScott Long iflib_dma_free_multi(iflib_dma_info_t *dmalist, int count) 14404c7070dbSScott Long { 14414c7070dbSScott Long int i; 14424c7070dbSScott Long iflib_dma_info_t *dmaiter = dmalist; 14434c7070dbSScott Long 14444c7070dbSScott Long for (i = 0; i < count; i++, dmaiter++) 14454c7070dbSScott Long iflib_dma_free(*dmaiter); 14464c7070dbSScott Long } 14474c7070dbSScott Long 14484c7070dbSScott Long static int 14494c7070dbSScott Long iflib_fast_intr(void *arg) 14504c7070dbSScott Long { 14514c7070dbSScott Long iflib_filter_info_t info = arg; 14524c7070dbSScott Long struct grouptask *gtask = info->ifi_task; 1453ca62461bSStephen Hurd int result; 1454ca62461bSStephen Hurd 145595246abbSSean Bruno DBG_COUNTER_INC(fast_intrs); 1456ca62461bSStephen Hurd if (info->ifi_filter != NULL) { 1457ca62461bSStephen Hurd result = info->ifi_filter(info->ifi_filter_arg); 1458ca62461bSStephen Hurd if ((result & FILTER_SCHEDULE_THREAD) == 0) 1459ca62461bSStephen Hurd return (result); 1460ca62461bSStephen Hurd } 146195246abbSSean Bruno 146295246abbSSean Bruno GROUPTASK_ENQUEUE(gtask); 146395246abbSSean Bruno return (FILTER_HANDLED); 146495246abbSSean Bruno } 146595246abbSSean Bruno 146695246abbSSean Bruno static int 146795246abbSSean Bruno iflib_fast_intr_rxtx(void *arg) 146895246abbSSean Bruno { 146995246abbSSean Bruno iflib_filter_info_t info = arg; 147095246abbSSean Bruno struct grouptask *gtask = info->ifi_task; 147195dcf343SMarius Strobl if_ctx_t ctx; 147295246abbSSean Bruno iflib_rxq_t rxq = (iflib_rxq_t)info->ifi_ctx; 147395dcf343SMarius Strobl iflib_txq_t txq; 147495dcf343SMarius Strobl void *sc; 1475ca62461bSStephen Hurd int i, cidx, result; 147695dcf343SMarius Strobl qidx_t txqid; 14773d10e9edSMarius Strobl bool intr_enable, intr_legacy; 147895246abbSSean Bruno 147995246abbSSean Bruno DBG_COUNTER_INC(fast_intrs); 1480ca62461bSStephen Hurd if (info->ifi_filter != NULL) { 1481ca62461bSStephen Hurd result = info->ifi_filter(info->ifi_filter_arg); 1482ca62461bSStephen Hurd if ((result & FILTER_SCHEDULE_THREAD) == 0) 1483ca62461bSStephen Hurd return (result); 1484ca62461bSStephen Hurd } 148595246abbSSean Bruno 148695dcf343SMarius Strobl ctx = rxq->ifr_ctx; 148795dcf343SMarius Strobl sc = ctx->ifc_softc; 14883d10e9edSMarius Strobl intr_enable = false; 14893d10e9edSMarius Strobl intr_legacy = !!(ctx->ifc_flags & IFC_LEGACY); 14901ae4848cSMatt Macy MPASS(rxq->ifr_ntxqirq); 149195246abbSSean Bruno for (i = 0; i < rxq->ifr_ntxqirq; i++) { 149295dcf343SMarius Strobl txqid = rxq->ifr_txqid[i]; 149395dcf343SMarius Strobl txq = &ctx->ifc_txqs[txqid]; 149495dcf343SMarius Strobl bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map, 14958a04b53dSKonstantin Belousov BUS_DMASYNC_POSTREAD); 149695dcf343SMarius Strobl if (!ctx->isc_txd_credits_update(sc, txqid, false)) { 14973d10e9edSMarius Strobl if (intr_legacy) 14983d10e9edSMarius Strobl intr_enable = true; 14993d10e9edSMarius Strobl else 150095246abbSSean Bruno IFDI_TX_QUEUE_INTR_ENABLE(ctx, txqid); 150195246abbSSean Bruno continue; 150295246abbSSean Bruno } 150395dcf343SMarius Strobl GROUPTASK_ENQUEUE(&txq->ift_task); 150495246abbSSean Bruno } 150595246abbSSean Bruno if (ctx->ifc_sctx->isc_flags & IFLIB_HAS_RXCQ) 150695246abbSSean Bruno cidx = rxq->ifr_cq_cidx; 150795246abbSSean Bruno else 150895246abbSSean Bruno cidx = rxq->ifr_fl[0].ifl_cidx; 150995246abbSSean Bruno if (iflib_rxd_avail(ctx, rxq, cidx, 1)) 151095246abbSSean Bruno GROUPTASK_ENQUEUE(gtask); 151164e6fc13SStephen Hurd else { 15123d10e9edSMarius Strobl if (intr_legacy) 15133d10e9edSMarius Strobl intr_enable = true; 15143d10e9edSMarius Strobl else 151595246abbSSean Bruno IFDI_RX_QUEUE_INTR_ENABLE(ctx, rxq->ifr_id); 151664e6fc13SStephen Hurd DBG_COUNTER_INC(rx_intr_enables); 151764e6fc13SStephen Hurd } 15183d10e9edSMarius Strobl if (intr_enable) 15193d10e9edSMarius Strobl IFDI_INTR_ENABLE(ctx); 152095246abbSSean Bruno return (FILTER_HANDLED); 152195246abbSSean Bruno } 152295246abbSSean Bruno 152395246abbSSean Bruno static int 152495246abbSSean Bruno iflib_fast_intr_ctx(void *arg) 152595246abbSSean Bruno { 152695246abbSSean Bruno iflib_filter_info_t info = arg; 152795246abbSSean Bruno struct grouptask *gtask = info->ifi_task; 1528ca62461bSStephen Hurd int result; 15294c7070dbSScott Long 15304c7070dbSScott Long DBG_COUNTER_INC(fast_intrs); 1531ca62461bSStephen Hurd if (info->ifi_filter != NULL) { 1532ca62461bSStephen Hurd result = info->ifi_filter(info->ifi_filter_arg); 1533ca62461bSStephen Hurd if ((result & FILTER_SCHEDULE_THREAD) == 0) 1534ca62461bSStephen Hurd return (result); 1535ca62461bSStephen Hurd } 15364c7070dbSScott Long 15374c7070dbSScott Long GROUPTASK_ENQUEUE(gtask); 15384c7070dbSScott Long return (FILTER_HANDLED); 15394c7070dbSScott Long } 15404c7070dbSScott Long 15414c7070dbSScott Long static int 15424c7070dbSScott Long _iflib_irq_alloc(if_ctx_t ctx, if_irq_t irq, int rid, 15434c7070dbSScott Long driver_filter_t filter, driver_intr_t handler, void *arg, 15443e0e6330SStephen Hurd const char *name) 15454c7070dbSScott Long { 15464c7070dbSScott Long struct resource *res; 15472b2fc973SSean Bruno void *tag = NULL; 15484c7070dbSScott Long device_t dev = ctx->ifc_dev; 1549d49e83eaSMarius Strobl int flags, i, rc; 15504c7070dbSScott Long 15512b2fc973SSean Bruno flags = RF_ACTIVE; 15522b2fc973SSean Bruno if (ctx->ifc_flags & IFC_LEGACY) 15532b2fc973SSean Bruno flags |= RF_SHAREABLE; 15544c7070dbSScott Long MPASS(rid < 512); 1555d49e83eaSMarius Strobl i = rid; 1556d49e83eaSMarius Strobl res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &i, flags); 15574c7070dbSScott Long if (res == NULL) { 15584c7070dbSScott Long device_printf(dev, 15594c7070dbSScott Long "failed to allocate IRQ for rid %d, name %s.\n", rid, name); 15604c7070dbSScott Long return (ENOMEM); 15614c7070dbSScott Long } 15624c7070dbSScott Long irq->ii_res = res; 15634c7070dbSScott Long KASSERT(filter == NULL || handler == NULL, ("filter and handler can't both be non-NULL")); 15644c7070dbSScott Long rc = bus_setup_intr(dev, res, INTR_MPSAFE | INTR_TYPE_NET, 15654c7070dbSScott Long filter, handler, arg, &tag); 15664c7070dbSScott Long if (rc != 0) { 15674c7070dbSScott Long device_printf(dev, 15684c7070dbSScott Long "failed to setup interrupt for rid %d, name %s: %d\n", 15694c7070dbSScott Long rid, name ? name : "unknown", rc); 15704c7070dbSScott Long return (rc); 15714c7070dbSScott Long } else if (name) 1572f454e7ebSJohn Baldwin bus_describe_intr(dev, res, tag, "%s", name); 15734c7070dbSScott Long 15744c7070dbSScott Long irq->ii_tag = tag; 15754c7070dbSScott Long return (0); 15764c7070dbSScott Long } 15774c7070dbSScott Long 15784c7070dbSScott Long /********************************************************************* 15794c7070dbSScott Long * 1580bfce461eSMarius Strobl * Allocate DMA resources for TX buffers as well as memory for the TX 1581bfce461eSMarius Strobl * mbuf map. TX DMA maps (non-TSO/TSO) and TX mbuf map are kept in a 1582bfce461eSMarius Strobl * iflib_sw_tx_desc_array structure, storing all the information that 1583bfce461eSMarius Strobl * is needed to transmit a packet on the wire. This is called only 1584bfce461eSMarius Strobl * once at attach, setup is done every reset. 15854c7070dbSScott Long * 15864c7070dbSScott Long **********************************************************************/ 15874c7070dbSScott Long static int 15884c7070dbSScott Long iflib_txsd_alloc(iflib_txq_t txq) 15894c7070dbSScott Long { 15904c7070dbSScott Long if_ctx_t ctx = txq->ift_ctx; 15914c7070dbSScott Long if_shared_ctx_t sctx = ctx->ifc_sctx; 15924c7070dbSScott Long if_softc_ctx_t scctx = &ctx->ifc_softc_ctx; 15934c7070dbSScott Long device_t dev = ctx->ifc_dev; 15947f87c040SMarius Strobl bus_size_t tsomaxsize; 15954c7070dbSScott Long int err, nsegments, ntsosegments; 15968a04b53dSKonstantin Belousov bool tso; 15974c7070dbSScott Long 15984c7070dbSScott Long nsegments = scctx->isc_tx_nsegments; 15994c7070dbSScott Long ntsosegments = scctx->isc_tx_tso_segments_max; 16007f87c040SMarius Strobl tsomaxsize = scctx->isc_tx_tso_size_max; 16017f87c040SMarius Strobl if (if_getcapabilities(ctx->ifc_ifp) & IFCAP_VLAN_MTU) 16027f87c040SMarius Strobl tsomaxsize += sizeof(struct ether_vlan_header); 160323ac9029SStephen Hurd MPASS(scctx->isc_ntxd[0] > 0); 160423ac9029SStephen Hurd MPASS(scctx->isc_ntxd[txq->ift_br_offset] > 0); 16054c7070dbSScott Long MPASS(nsegments > 0); 16067f87c040SMarius Strobl if (if_getcapabilities(ctx->ifc_ifp) & IFCAP_TSO) { 16074c7070dbSScott Long MPASS(ntsosegments > 0); 16087f87c040SMarius Strobl MPASS(sctx->isc_tso_maxsize >= tsomaxsize); 16097f87c040SMarius Strobl } 16107f87c040SMarius Strobl 16114c7070dbSScott Long /* 1612bfce461eSMarius Strobl * Set up DMA tags for TX buffers. 16134c7070dbSScott Long */ 16144c7070dbSScott Long if ((err = bus_dma_tag_create(bus_get_dma_tag(dev), 16154c7070dbSScott Long 1, 0, /* alignment, bounds */ 16164c7070dbSScott Long BUS_SPACE_MAXADDR, /* lowaddr */ 16174c7070dbSScott Long BUS_SPACE_MAXADDR, /* highaddr */ 16184c7070dbSScott Long NULL, NULL, /* filter, filterarg */ 16194c7070dbSScott Long sctx->isc_tx_maxsize, /* maxsize */ 16204c7070dbSScott Long nsegments, /* nsegments */ 16214c7070dbSScott Long sctx->isc_tx_maxsegsize, /* maxsegsize */ 16224c7070dbSScott Long 0, /* flags */ 16234c7070dbSScott Long NULL, /* lockfunc */ 16244c7070dbSScott Long NULL, /* lockfuncarg */ 1625bfce461eSMarius Strobl &txq->ift_buf_tag))) { 16264c7070dbSScott Long device_printf(dev,"Unable to allocate TX DMA tag: %d\n", err); 16279d0a88deSDimitry Andric device_printf(dev,"maxsize: %ju nsegments: %d maxsegsize: %ju\n", 16289d0a88deSDimitry Andric (uintmax_t)sctx->isc_tx_maxsize, nsegments, (uintmax_t)sctx->isc_tx_maxsegsize); 16294c7070dbSScott Long goto fail; 16304c7070dbSScott Long } 16318a04b53dSKonstantin Belousov tso = (if_getcapabilities(ctx->ifc_ifp) & IFCAP_TSO) != 0; 16328a04b53dSKonstantin Belousov if (tso && (err = bus_dma_tag_create(bus_get_dma_tag(dev), 16334c7070dbSScott Long 1, 0, /* alignment, bounds */ 16344c7070dbSScott Long BUS_SPACE_MAXADDR, /* lowaddr */ 16354c7070dbSScott Long BUS_SPACE_MAXADDR, /* highaddr */ 16364c7070dbSScott Long NULL, NULL, /* filter, filterarg */ 16377f87c040SMarius Strobl tsomaxsize, /* maxsize */ 16384c7070dbSScott Long ntsosegments, /* nsegments */ 16397f87c040SMarius Strobl sctx->isc_tso_maxsegsize,/* maxsegsize */ 16404c7070dbSScott Long 0, /* flags */ 16414c7070dbSScott Long NULL, /* lockfunc */ 16424c7070dbSScott Long NULL, /* lockfuncarg */ 1643bfce461eSMarius Strobl &txq->ift_tso_buf_tag))) { 1644bfce461eSMarius Strobl device_printf(dev, "Unable to allocate TSO TX DMA tag: %d\n", 1645bfce461eSMarius Strobl err); 16464c7070dbSScott Long goto fail; 16474c7070dbSScott Long } 1648bfce461eSMarius Strobl 1649bfce461eSMarius Strobl /* Allocate memory for the TX mbuf map. */ 16504c7070dbSScott Long if (!(txq->ift_sds.ifsd_m = 1651ac2fffa4SPedro F. Giffuni (struct mbuf **) malloc(sizeof(struct mbuf *) * 1652ac2fffa4SPedro F. Giffuni scctx->isc_ntxd[txq->ift_br_offset], M_IFLIB, M_NOWAIT | M_ZERO))) { 1653bfce461eSMarius Strobl device_printf(dev, "Unable to allocate TX mbuf map memory\n"); 16544c7070dbSScott Long err = ENOMEM; 16554c7070dbSScott Long goto fail; 16564c7070dbSScott Long } 16574c7070dbSScott Long 1658bfce461eSMarius Strobl /* 1659bfce461eSMarius Strobl * Create the DMA maps for TX buffers. 1660bfce461eSMarius Strobl */ 16618a04b53dSKonstantin Belousov if ((txq->ift_sds.ifsd_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 TX buffer DMA map memory\n"); 16664c7070dbSScott Long err = ENOMEM; 16674c7070dbSScott Long goto fail; 16684c7070dbSScott Long } 16698a04b53dSKonstantin Belousov if (tso && (txq->ift_sds.ifsd_tso_map = (bus_dmamap_t *)malloc( 16708a04b53dSKonstantin Belousov sizeof(bus_dmamap_t) * scctx->isc_ntxd[txq->ift_br_offset], 16718a04b53dSKonstantin Belousov M_IFLIB, M_NOWAIT | M_ZERO)) == NULL) { 1672bfce461eSMarius Strobl device_printf(dev, 1673bfce461eSMarius Strobl "Unable to allocate TSO TX buffer map memory\n"); 16748a04b53dSKonstantin Belousov err = ENOMEM; 16758a04b53dSKonstantin Belousov goto fail; 16768a04b53dSKonstantin Belousov } 167723ac9029SStephen Hurd for (int i = 0; i < scctx->isc_ntxd[txq->ift_br_offset]; i++) { 1678bfce461eSMarius Strobl err = bus_dmamap_create(txq->ift_buf_tag, 0, 16798a04b53dSKonstantin Belousov &txq->ift_sds.ifsd_map[i]); 16804c7070dbSScott Long if (err != 0) { 16814c7070dbSScott Long device_printf(dev, "Unable to create TX DMA map\n"); 16824c7070dbSScott Long goto fail; 16834c7070dbSScott Long } 16848a04b53dSKonstantin Belousov if (!tso) 16858a04b53dSKonstantin Belousov continue; 1686bfce461eSMarius Strobl err = bus_dmamap_create(txq->ift_tso_buf_tag, 0, 16878a04b53dSKonstantin Belousov &txq->ift_sds.ifsd_tso_map[i]); 16888a04b53dSKonstantin Belousov if (err != 0) { 16898a04b53dSKonstantin Belousov device_printf(dev, "Unable to create TSO TX DMA map\n"); 16908a04b53dSKonstantin Belousov goto fail; 16918a04b53dSKonstantin Belousov } 16924c7070dbSScott Long } 16934c7070dbSScott Long return (0); 16944c7070dbSScott Long fail: 16954c7070dbSScott Long /* We free all, it handles case where we are in the middle */ 16964c7070dbSScott Long iflib_tx_structures_free(ctx); 16974c7070dbSScott Long return (err); 16984c7070dbSScott Long } 16994c7070dbSScott Long 17004c7070dbSScott Long static void 17014c7070dbSScott Long iflib_txsd_destroy(if_ctx_t ctx, iflib_txq_t txq, int i) 17024c7070dbSScott Long { 17034c7070dbSScott Long bus_dmamap_t map; 17044c7070dbSScott Long 1705db8e8f1eSEric Joyner if (txq->ift_sds.ifsd_map != NULL) { 17064c7070dbSScott Long map = txq->ift_sds.ifsd_map[i]; 1707bfce461eSMarius Strobl bus_dmamap_sync(txq->ift_buf_tag, map, BUS_DMASYNC_POSTWRITE); 1708bfce461eSMarius Strobl bus_dmamap_unload(txq->ift_buf_tag, map); 1709bfce461eSMarius Strobl bus_dmamap_destroy(txq->ift_buf_tag, map); 17104c7070dbSScott Long txq->ift_sds.ifsd_map[i] = NULL; 17114c7070dbSScott Long } 17128a04b53dSKonstantin Belousov 1713db8e8f1eSEric Joyner if (txq->ift_sds.ifsd_tso_map != NULL) { 17148a04b53dSKonstantin Belousov map = txq->ift_sds.ifsd_tso_map[i]; 1715bfce461eSMarius Strobl bus_dmamap_sync(txq->ift_tso_buf_tag, map, 17168a04b53dSKonstantin Belousov BUS_DMASYNC_POSTWRITE); 1717bfce461eSMarius Strobl bus_dmamap_unload(txq->ift_tso_buf_tag, map); 1718bfce461eSMarius Strobl bus_dmamap_destroy(txq->ift_tso_buf_tag, map); 17198a04b53dSKonstantin Belousov txq->ift_sds.ifsd_tso_map[i] = NULL; 17208a04b53dSKonstantin Belousov } 17214c7070dbSScott Long } 17224c7070dbSScott Long 17234c7070dbSScott Long static void 17244c7070dbSScott Long iflib_txq_destroy(iflib_txq_t txq) 17254c7070dbSScott Long { 17264c7070dbSScott Long if_ctx_t ctx = txq->ift_ctx; 17274c7070dbSScott Long 172823ac9029SStephen Hurd for (int i = 0; i < txq->ift_size; i++) 17294c7070dbSScott Long iflib_txsd_destroy(ctx, txq, i); 1730244e7cffSEric Joyner 1731244e7cffSEric Joyner if (txq->ift_br != NULL) { 1732244e7cffSEric Joyner ifmp_ring_free(txq->ift_br); 1733244e7cffSEric Joyner txq->ift_br = NULL; 1734244e7cffSEric Joyner } 1735244e7cffSEric Joyner 1736244e7cffSEric Joyner mtx_destroy(&txq->ift_mtx); 1737244e7cffSEric Joyner 17384c7070dbSScott Long if (txq->ift_sds.ifsd_map != NULL) { 17394c7070dbSScott Long free(txq->ift_sds.ifsd_map, M_IFLIB); 17404c7070dbSScott Long txq->ift_sds.ifsd_map = NULL; 17414c7070dbSScott Long } 17428a04b53dSKonstantin Belousov if (txq->ift_sds.ifsd_tso_map != NULL) { 17438a04b53dSKonstantin Belousov free(txq->ift_sds.ifsd_tso_map, M_IFLIB); 17448a04b53dSKonstantin Belousov txq->ift_sds.ifsd_tso_map = NULL; 17458a04b53dSKonstantin Belousov } 17464c7070dbSScott Long if (txq->ift_sds.ifsd_m != NULL) { 17474c7070dbSScott Long free(txq->ift_sds.ifsd_m, M_IFLIB); 17484c7070dbSScott Long txq->ift_sds.ifsd_m = NULL; 17494c7070dbSScott Long } 1750bfce461eSMarius Strobl if (txq->ift_buf_tag != NULL) { 1751bfce461eSMarius Strobl bus_dma_tag_destroy(txq->ift_buf_tag); 1752bfce461eSMarius Strobl txq->ift_buf_tag = NULL; 17534c7070dbSScott Long } 1754bfce461eSMarius Strobl if (txq->ift_tso_buf_tag != NULL) { 1755bfce461eSMarius Strobl bus_dma_tag_destroy(txq->ift_tso_buf_tag); 1756bfce461eSMarius Strobl txq->ift_tso_buf_tag = NULL; 17574c7070dbSScott Long } 1758244e7cffSEric Joyner if (txq->ift_ifdi != NULL) { 1759244e7cffSEric Joyner free(txq->ift_ifdi, M_IFLIB); 1760244e7cffSEric Joyner } 17614c7070dbSScott Long } 17624c7070dbSScott Long 17634c7070dbSScott Long static void 17644c7070dbSScott Long iflib_txsd_free(if_ctx_t ctx, iflib_txq_t txq, int i) 17654c7070dbSScott Long { 17664c7070dbSScott Long struct mbuf **mp; 17674c7070dbSScott Long 17684c7070dbSScott Long mp = &txq->ift_sds.ifsd_m[i]; 17694c7070dbSScott Long if (*mp == NULL) 17704c7070dbSScott Long return; 17714c7070dbSScott Long 17724c7070dbSScott Long if (txq->ift_sds.ifsd_map != NULL) { 1773bfce461eSMarius Strobl bus_dmamap_sync(txq->ift_buf_tag, 17748a04b53dSKonstantin Belousov txq->ift_sds.ifsd_map[i], BUS_DMASYNC_POSTWRITE); 1775bfce461eSMarius Strobl bus_dmamap_unload(txq->ift_buf_tag, txq->ift_sds.ifsd_map[i]); 17768a04b53dSKonstantin Belousov } 17778a04b53dSKonstantin Belousov if (txq->ift_sds.ifsd_tso_map != NULL) { 1778bfce461eSMarius Strobl bus_dmamap_sync(txq->ift_tso_buf_tag, 17798a04b53dSKonstantin Belousov txq->ift_sds.ifsd_tso_map[i], BUS_DMASYNC_POSTWRITE); 1780bfce461eSMarius Strobl bus_dmamap_unload(txq->ift_tso_buf_tag, 17818a04b53dSKonstantin Belousov txq->ift_sds.ifsd_tso_map[i]); 17824c7070dbSScott Long } 178323ac9029SStephen Hurd m_free(*mp); 17844c7070dbSScott Long DBG_COUNTER_INC(tx_frees); 17854c7070dbSScott Long *mp = NULL; 17864c7070dbSScott Long } 17874c7070dbSScott Long 17884c7070dbSScott Long static int 17894c7070dbSScott Long iflib_txq_setup(iflib_txq_t txq) 17904c7070dbSScott Long { 17914c7070dbSScott Long if_ctx_t ctx = txq->ift_ctx; 179223ac9029SStephen Hurd if_softc_ctx_t scctx = &ctx->ifc_softc_ctx; 17934d261ce2SStephen Hurd if_shared_ctx_t sctx = ctx->ifc_sctx; 17944c7070dbSScott Long iflib_dma_info_t di; 17954c7070dbSScott Long int i; 17964c7070dbSScott Long 17974c7070dbSScott Long /* Set number of descriptors available */ 17984c7070dbSScott Long txq->ift_qstatus = IFLIB_QUEUE_IDLE; 179995246abbSSean Bruno /* XXX make configurable */ 180095246abbSSean Bruno txq->ift_update_freq = IFLIB_DEFAULT_TX_UPDATE_FREQ; 18014c7070dbSScott Long 18024c7070dbSScott Long /* Reset indices */ 180395246abbSSean Bruno txq->ift_cidx_processed = 0; 180495246abbSSean Bruno txq->ift_pidx = txq->ift_cidx = txq->ift_npending = 0; 180523ac9029SStephen Hurd txq->ift_size = scctx->isc_ntxd[txq->ift_br_offset]; 18064c7070dbSScott Long 18074d261ce2SStephen Hurd for (i = 0, di = txq->ift_ifdi; i < sctx->isc_ntxqs; i++, di++) 18084c7070dbSScott Long bzero((void *)di->idi_vaddr, di->idi_size); 18094c7070dbSScott Long 18104c7070dbSScott Long IFDI_TXQ_SETUP(ctx, txq->ift_id); 18114d261ce2SStephen Hurd for (i = 0, di = txq->ift_ifdi; i < sctx->isc_ntxqs; i++, di++) 18124c7070dbSScott Long bus_dmamap_sync(di->idi_tag, di->idi_map, 18134c7070dbSScott Long BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 18144c7070dbSScott Long return (0); 18154c7070dbSScott Long } 18164c7070dbSScott Long 18174c7070dbSScott Long /********************************************************************* 18184c7070dbSScott Long * 1819bfce461eSMarius Strobl * Allocate DMA resources for RX buffers as well as memory for the RX 1820bfce461eSMarius Strobl * mbuf map, direct RX cluster pointer map and RX cluster bus address 1821bfce461eSMarius Strobl * map. RX DMA map, RX mbuf map, direct RX cluster pointer map and 1822bfce461eSMarius Strobl * RX cluster map are kept in a iflib_sw_rx_desc_array structure. 1823bfce461eSMarius Strobl * Since we use use one entry in iflib_sw_rx_desc_array per received 1824bfce461eSMarius Strobl * packet, the maximum number of entries we'll need is equal to the 1825bfce461eSMarius Strobl * number of hardware receive descriptors that we've allocated. 18264c7070dbSScott Long * 18274c7070dbSScott Long **********************************************************************/ 18284c7070dbSScott Long static int 18294c7070dbSScott Long iflib_rxsd_alloc(iflib_rxq_t rxq) 18304c7070dbSScott Long { 18314c7070dbSScott Long if_ctx_t ctx = rxq->ifr_ctx; 18324c7070dbSScott Long if_shared_ctx_t sctx = ctx->ifc_sctx; 183323ac9029SStephen Hurd if_softc_ctx_t scctx = &ctx->ifc_softc_ctx; 18344c7070dbSScott Long device_t dev = ctx->ifc_dev; 18354c7070dbSScott Long iflib_fl_t fl; 18364c7070dbSScott Long int err; 18374c7070dbSScott Long 183823ac9029SStephen Hurd MPASS(scctx->isc_nrxd[0] > 0); 183923ac9029SStephen Hurd MPASS(scctx->isc_nrxd[rxq->ifr_fl_offset] > 0); 18404c7070dbSScott Long 18414c7070dbSScott Long fl = rxq->ifr_fl; 18424c7070dbSScott Long for (int i = 0; i < rxq->ifr_nfl; i++, fl++) { 184323ac9029SStephen Hurd fl->ifl_size = scctx->isc_nrxd[rxq->ifr_fl_offset]; /* this isn't necessarily the same */ 1844bfce461eSMarius Strobl /* Set up DMA tag for RX buffers. */ 18454c7070dbSScott Long err = bus_dma_tag_create(bus_get_dma_tag(dev), /* parent */ 18464c7070dbSScott Long 1, 0, /* alignment, bounds */ 18474c7070dbSScott Long BUS_SPACE_MAXADDR, /* lowaddr */ 18484c7070dbSScott Long BUS_SPACE_MAXADDR, /* highaddr */ 18494c7070dbSScott Long NULL, NULL, /* filter, filterarg */ 18504c7070dbSScott Long sctx->isc_rx_maxsize, /* maxsize */ 18514c7070dbSScott Long sctx->isc_rx_nsegments, /* nsegments */ 18524c7070dbSScott Long sctx->isc_rx_maxsegsize, /* maxsegsize */ 18534c7070dbSScott Long 0, /* flags */ 18544c7070dbSScott Long NULL, /* lockfunc */ 18554c7070dbSScott Long NULL, /* lockarg */ 1856bfce461eSMarius Strobl &fl->ifl_buf_tag); 18574c7070dbSScott Long if (err) { 1858bfce461eSMarius Strobl device_printf(dev, 1859bfce461eSMarius Strobl "Unable to allocate RX DMA tag: %d\n", err); 18604c7070dbSScott Long goto fail; 18614c7070dbSScott Long } 1862bfce461eSMarius Strobl 1863bfce461eSMarius Strobl /* Allocate memory for the RX mbuf map. */ 1864e035717eSSean Bruno if (!(fl->ifl_sds.ifsd_m = 1865ac2fffa4SPedro F. Giffuni (struct mbuf **) malloc(sizeof(struct mbuf *) * 1866ac2fffa4SPedro F. Giffuni scctx->isc_nrxd[rxq->ifr_fl_offset], M_IFLIB, M_NOWAIT | M_ZERO))) { 1867bfce461eSMarius Strobl device_printf(dev, 1868bfce461eSMarius Strobl "Unable to allocate RX mbuf map memory\n"); 1869e035717eSSean Bruno err = ENOMEM; 1870e035717eSSean Bruno goto fail; 1871e035717eSSean Bruno } 1872bfce461eSMarius Strobl 1873bfce461eSMarius Strobl /* Allocate memory for the direct RX cluster pointer map. */ 1874e035717eSSean Bruno if (!(fl->ifl_sds.ifsd_cl = 1875ac2fffa4SPedro F. Giffuni (caddr_t *) malloc(sizeof(caddr_t) * 1876ac2fffa4SPedro F. Giffuni scctx->isc_nrxd[rxq->ifr_fl_offset], M_IFLIB, M_NOWAIT | M_ZERO))) { 1877bfce461eSMarius Strobl device_printf(dev, 1878bfce461eSMarius Strobl "Unable to allocate RX cluster map memory\n"); 1879e035717eSSean Bruno err = ENOMEM; 1880e035717eSSean Bruno goto fail; 1881e035717eSSean Bruno } 18824c7070dbSScott Long 1883bfce461eSMarius Strobl /* Allocate memory for the RX cluster bus address map. */ 1884fbec776dSAndrew Gallatin if (!(fl->ifl_sds.ifsd_ba = 1885fbec776dSAndrew Gallatin (bus_addr_t *) malloc(sizeof(bus_addr_t) * 1886fbec776dSAndrew Gallatin scctx->isc_nrxd[rxq->ifr_fl_offset], M_IFLIB, M_NOWAIT | M_ZERO))) { 1887bfce461eSMarius Strobl device_printf(dev, 1888bfce461eSMarius Strobl "Unable to allocate RX bus address map memory\n"); 1889fbec776dSAndrew Gallatin err = ENOMEM; 1890fbec776dSAndrew Gallatin goto fail; 1891fbec776dSAndrew Gallatin } 1892e035717eSSean Bruno 1893bfce461eSMarius Strobl /* 1894bfce461eSMarius Strobl * Create the DMA maps for RX buffers. 1895bfce461eSMarius Strobl */ 1896e035717eSSean Bruno if (!(fl->ifl_sds.ifsd_map = 1897ac2fffa4SPedro F. Giffuni (bus_dmamap_t *) malloc(sizeof(bus_dmamap_t) * scctx->isc_nrxd[rxq->ifr_fl_offset], M_IFLIB, M_NOWAIT | M_ZERO))) { 1898bfce461eSMarius Strobl device_printf(dev, 1899bfce461eSMarius Strobl "Unable to allocate RX buffer DMA map memory\n"); 1900e035717eSSean Bruno err = ENOMEM; 1901e035717eSSean Bruno goto fail; 1902e035717eSSean Bruno } 1903e035717eSSean Bruno for (int i = 0; i < scctx->isc_nrxd[rxq->ifr_fl_offset]; i++) { 1904bfce461eSMarius Strobl err = bus_dmamap_create(fl->ifl_buf_tag, 0, 1905bfce461eSMarius Strobl &fl->ifl_sds.ifsd_map[i]); 1906e035717eSSean Bruno if (err != 0) { 190795246abbSSean Bruno device_printf(dev, "Unable to create RX buffer DMA map\n"); 19084c7070dbSScott Long goto fail; 19094c7070dbSScott Long } 19104c7070dbSScott Long } 1911835809f9SSean Bruno } 19124c7070dbSScott Long return (0); 19134c7070dbSScott Long 19144c7070dbSScott Long fail: 19154c7070dbSScott Long iflib_rx_structures_free(ctx); 19164c7070dbSScott Long return (err); 19174c7070dbSScott Long } 19184c7070dbSScott Long 19194c7070dbSScott Long /* 19204c7070dbSScott Long * Internal service routines 19214c7070dbSScott Long */ 19224c7070dbSScott Long 19234c7070dbSScott Long struct rxq_refill_cb_arg { 19244c7070dbSScott Long int error; 19254c7070dbSScott Long bus_dma_segment_t seg; 19264c7070dbSScott Long int nseg; 19274c7070dbSScott Long }; 19284c7070dbSScott Long 19294c7070dbSScott Long static void 19304c7070dbSScott Long _rxq_refill_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error) 19314c7070dbSScott Long { 19324c7070dbSScott Long struct rxq_refill_cb_arg *cb_arg = arg; 19334c7070dbSScott Long 19344c7070dbSScott Long cb_arg->error = error; 19354c7070dbSScott Long cb_arg->seg = segs[0]; 19364c7070dbSScott Long cb_arg->nseg = nseg; 19374c7070dbSScott Long } 19384c7070dbSScott Long 19394c7070dbSScott Long /** 1940b256d25cSMark Johnston * iflib_fl_refill - refill an rxq free-buffer list 19414c7070dbSScott Long * @ctx: the iflib context 19421722eeacSMarius Strobl * @fl: the free list to refill 19431722eeacSMarius Strobl * @count: the number of new buffers to allocate 19444c7070dbSScott Long * 19451722eeacSMarius Strobl * (Re)populate an rxq free-buffer list with up to @count new packet buffers. 194635d8a463SVincenzo Maffione * The caller must assure that @count does not exceed the queue's capacity 194735d8a463SVincenzo Maffione * minus one (since we always leave a descriptor unavailable). 19484c7070dbSScott Long */ 1949fb1a29b4SHans Petter Selasky static uint8_t 1950b256d25cSMark Johnston iflib_fl_refill(if_ctx_t ctx, iflib_fl_t fl, int count) 19514c7070dbSScott Long { 195295246abbSSean Bruno struct if_rxd_update iru; 1953fbec776dSAndrew Gallatin struct rxq_refill_cb_arg cb_arg; 19543db348b5SMarius Strobl struct mbuf *m; 19553db348b5SMarius Strobl caddr_t cl, *sd_cl; 19563db348b5SMarius Strobl struct mbuf **sd_m; 1957e035717eSSean Bruno bus_dmamap_t *sd_map; 1958fbec776dSAndrew Gallatin bus_addr_t bus_addr, *sd_ba; 19593db348b5SMarius Strobl int err, frag_idx, i, idx, n, pidx; 1960a1b799caSStephen Hurd qidx_t credits; 19614c7070dbSScott Long 196235d8a463SVincenzo Maffione MPASS(count <= fl->ifl_size - fl->ifl_credits - 1); 196335d8a463SVincenzo Maffione 1964e035717eSSean Bruno sd_m = fl->ifl_sds.ifsd_m; 1965e035717eSSean Bruno sd_map = fl->ifl_sds.ifsd_map; 1966e035717eSSean Bruno sd_cl = fl->ifl_sds.ifsd_cl; 1967fbec776dSAndrew Gallatin sd_ba = fl->ifl_sds.ifsd_ba; 19683db348b5SMarius Strobl pidx = fl->ifl_pidx; 1969e035717eSSean Bruno idx = pidx; 19703db348b5SMarius Strobl frag_idx = fl->ifl_fragidx; 1971a1b799caSStephen Hurd credits = fl->ifl_credits; 1972e035717eSSean Bruno 19733db348b5SMarius Strobl i = 0; 19744c7070dbSScott Long n = count; 19754c7070dbSScott Long MPASS(n > 0); 1976a1b799caSStephen Hurd MPASS(credits + n <= fl->ifl_size); 19774c7070dbSScott Long 19784c7070dbSScott Long if (pidx < fl->ifl_cidx) 19794c7070dbSScott Long MPASS(pidx + n <= fl->ifl_cidx); 1980a1b799caSStephen Hurd if (pidx == fl->ifl_cidx && (credits < fl->ifl_size)) 19814c7070dbSScott Long MPASS(fl->ifl_gen == 0); 19824c7070dbSScott Long if (pidx > fl->ifl_cidx) 19834c7070dbSScott Long MPASS(n <= fl->ifl_size - pidx + fl->ifl_cidx); 19844c7070dbSScott Long 19854c7070dbSScott Long DBG_COUNTER_INC(fl_refills); 19864c7070dbSScott Long if (n > 8) 19874c7070dbSScott Long DBG_COUNTER_INC(fl_refills_large); 19882d873474SStephen Hurd iru_init(&iru, fl->ifl_rxq, fl->ifl_id); 1989b256d25cSMark Johnston while (n-- > 0) { 19904c7070dbSScott Long /* 19914c7070dbSScott Long * We allocate an uninitialized mbuf + cluster, mbuf is 19924c7070dbSScott Long * initialized after rx. 19934c7070dbSScott Long * 1994b256d25cSMark Johnston * If the cluster is still set then we know a minimum sized 1995b256d25cSMark Johnston * packet was received 19964c7070dbSScott Long */ 19973db348b5SMarius Strobl bit_ffc_at(fl->ifl_rx_bitmap, frag_idx, fl->ifl_size, 19983db348b5SMarius Strobl &frag_idx); 19993db348b5SMarius Strobl if (frag_idx < 0) 200087890dbaSSean Bruno bit_ffc(fl->ifl_rx_bitmap, fl->ifl_size, &frag_idx); 20013db348b5SMarius Strobl MPASS(frag_idx >= 0); 200287890dbaSSean Bruno if ((cl = sd_cl[frag_idx]) == NULL) { 2003b256d25cSMark Johnston cl = uma_zalloc(fl->ifl_zone, M_NOWAIT); 2004a363e1d4SMark Johnston if (__predict_false(cl == NULL)) 20054c7070dbSScott Long break; 20064c7070dbSScott Long 20074c7070dbSScott Long cb_arg.error = 0; 200895246abbSSean Bruno MPASS(sd_map != NULL); 2009bfce461eSMarius Strobl err = bus_dmamap_load(fl->ifl_buf_tag, sd_map[frag_idx], 20108a04b53dSKonstantin Belousov cl, fl->ifl_buf_size, _rxq_refill_cb, &cb_arg, 20118a04b53dSKonstantin Belousov BUS_DMA_NOWAIT); 2012a363e1d4SMark Johnston if (__predict_false(err != 0 || cb_arg.error)) { 20134c7070dbSScott Long uma_zfree(fl->ifl_zone, cl); 2014fbec776dSAndrew Gallatin break; 20154c7070dbSScott Long } 20164c7070dbSScott Long 2017fbec776dSAndrew Gallatin sd_ba[frag_idx] = bus_addr = cb_arg.seg.ds_addr; 201887890dbaSSean Bruno sd_cl[frag_idx] = cl; 2019fbec776dSAndrew Gallatin #if MEMORY_LOGGING 2020fbec776dSAndrew Gallatin fl->ifl_cl_enqueued++; 2021fbec776dSAndrew Gallatin #endif 2022fbec776dSAndrew Gallatin } else { 2023fbec776dSAndrew Gallatin bus_addr = sd_ba[frag_idx]; 2024fbec776dSAndrew Gallatin } 202595dcf343SMarius Strobl bus_dmamap_sync(fl->ifl_buf_tag, sd_map[frag_idx], 202695dcf343SMarius Strobl BUS_DMASYNC_PREREAD); 2027fbec776dSAndrew Gallatin 20286d49b41eSAndrew Gallatin if (sd_m[frag_idx] == NULL) { 2029a363e1d4SMark Johnston m = m_gethdr(M_NOWAIT, MT_NOINIT); 2030a363e1d4SMark Johnston if (__predict_false(m == NULL)) 2031fbec776dSAndrew Gallatin break; 203287890dbaSSean Bruno sd_m[frag_idx] = m; 20336d49b41eSAndrew Gallatin } 20343db348b5SMarius Strobl bit_set(fl->ifl_rx_bitmap, frag_idx); 2035fbec776dSAndrew Gallatin #if MEMORY_LOGGING 2036fbec776dSAndrew Gallatin fl->ifl_m_enqueued++; 2037fbec776dSAndrew Gallatin #endif 2038fbec776dSAndrew Gallatin 2039fbec776dSAndrew Gallatin DBG_COUNTER_INC(rx_allocs); 204087890dbaSSean Bruno fl->ifl_rxd_idxs[i] = frag_idx; 20414c7070dbSScott Long fl->ifl_bus_addrs[i] = bus_addr; 2042a1b799caSStephen Hurd credits++; 20434c7070dbSScott Long i++; 2044a1b799caSStephen Hurd MPASS(credits <= fl->ifl_size); 2045e035717eSSean Bruno if (++idx == fl->ifl_size) { 2046b256d25cSMark Johnston #ifdef INVARIANTS 20474c7070dbSScott Long fl->ifl_gen = 1; 2048b256d25cSMark Johnston #endif 2049e035717eSSean Bruno idx = 0; 20504c7070dbSScott Long } 20514c7070dbSScott Long if (n == 0 || i == IFLIB_MAX_RX_REFRESH) { 205295246abbSSean Bruno iru.iru_pidx = pidx; 205395246abbSSean Bruno iru.iru_count = i; 205495246abbSSean Bruno ctx->isc_rxd_refill(ctx->ifc_softc, &iru); 2055fa5416a8SSean Bruno fl->ifl_pidx = idx; 2056a1b799caSStephen Hurd fl->ifl_credits = credits; 2057b256d25cSMark Johnston pidx = idx; 2058b256d25cSMark Johnston i = 0; 205987890dbaSSean Bruno } 20604c7070dbSScott Long } 2061fbec776dSAndrew Gallatin 2062a363e1d4SMark Johnston if (n < count - 1) { 2063a363e1d4SMark Johnston if (i != 0) { 2064a1b799caSStephen Hurd iru.iru_pidx = pidx; 2065a1b799caSStephen Hurd iru.iru_count = i; 2066a1b799caSStephen Hurd ctx->isc_rxd_refill(ctx->ifc_softc, &iru); 2067a1b799caSStephen Hurd fl->ifl_pidx = idx; 2068a1b799caSStephen Hurd fl->ifl_credits = credits; 2069a1b799caSStephen Hurd } 20704c7070dbSScott Long DBG_COUNTER_INC(rxd_flush); 207195246abbSSean Bruno bus_dmamap_sync(fl->ifl_ifdi->idi_tag, fl->ifl_ifdi->idi_map, 207295246abbSSean Bruno BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 2073a363e1d4SMark Johnston ctx->isc_rxd_flush(ctx->ifc_softc, fl->ifl_rxq->ifr_id, 207435d8a463SVincenzo Maffione fl->ifl_id, fl->ifl_pidx); 2075a363e1d4SMark Johnston if (__predict_true(bit_test(fl->ifl_rx_bitmap, frag_idx))) { 20769e9b738aSPatrick Kelsey fl->ifl_fragidx = frag_idx + 1; 20779e9b738aSPatrick Kelsey if (fl->ifl_fragidx == fl->ifl_size) 20789e9b738aSPatrick Kelsey fl->ifl_fragidx = 0; 2079a363e1d4SMark Johnston } else { 2080a363e1d4SMark Johnston fl->ifl_fragidx = frag_idx; 2081a363e1d4SMark Johnston } 2082a363e1d4SMark Johnston } 2083fb1a29b4SHans Petter Selasky 2084fb1a29b4SHans Petter Selasky return (n == -1 ? 0 : IFLIB_RXEOF_EMPTY); 20854c7070dbSScott Long } 20864c7070dbSScott Long 2087b256d25cSMark Johnston static inline uint8_t 2088b256d25cSMark Johnston iflib_fl_refill_all(if_ctx_t ctx, iflib_fl_t fl) 20894c7070dbSScott Long { 209035d8a463SVincenzo Maffione /* 209135d8a463SVincenzo Maffione * We leave an unused descriptor to avoid pidx to catch up with cidx. 209235d8a463SVincenzo Maffione * This is important as it confuses most NICs. For instance, 209335d8a463SVincenzo Maffione * Intel NICs have (per receive ring) RDH and RDT registers, where 209435d8a463SVincenzo Maffione * RDH points to the next receive descriptor to be used by the NIC, 209535d8a463SVincenzo Maffione * and RDT for the next receive descriptor to be published by the 209635d8a463SVincenzo Maffione * driver to the NIC (RDT - 1 is thus the last valid one). 209735d8a463SVincenzo Maffione * The condition RDH == RDT means no descriptors are available to 209835d8a463SVincenzo Maffione * the NIC, and thus it would be ambiguous if it also meant that 209935d8a463SVincenzo Maffione * all the descriptors are available to the NIC. 210035d8a463SVincenzo Maffione */ 21014c7070dbSScott Long int32_t reclaimable = fl->ifl_size - fl->ifl_credits - 1; 21024c7070dbSScott Long #ifdef INVARIANTS 21034c7070dbSScott Long int32_t delta = fl->ifl_size - get_inuse(fl->ifl_size, fl->ifl_cidx, fl->ifl_pidx, fl->ifl_gen) - 1; 21044c7070dbSScott Long #endif 21054c7070dbSScott Long 21064c7070dbSScott Long MPASS(fl->ifl_credits <= fl->ifl_size); 21074c7070dbSScott Long MPASS(reclaimable == delta); 21084c7070dbSScott Long 21094c7070dbSScott Long if (reclaimable > 0) 2110b256d25cSMark Johnston return (iflib_fl_refill(ctx, fl, reclaimable)); 2111fb1a29b4SHans Petter Selasky return (0); 21124c7070dbSScott Long } 21134c7070dbSScott Long 211477c1fcecSEric Joyner uint8_t 211577c1fcecSEric Joyner iflib_in_detach(if_ctx_t ctx) 211677c1fcecSEric Joyner { 211777c1fcecSEric Joyner bool in_detach; 21181722eeacSMarius Strobl 211977c1fcecSEric Joyner STATE_LOCK(ctx); 212077c1fcecSEric Joyner in_detach = !!(ctx->ifc_flags & IFC_IN_DETACH); 212177c1fcecSEric Joyner STATE_UNLOCK(ctx); 212277c1fcecSEric Joyner return (in_detach); 212377c1fcecSEric Joyner } 212477c1fcecSEric Joyner 21254c7070dbSScott Long static void 21264c7070dbSScott Long iflib_fl_bufs_free(iflib_fl_t fl) 21274c7070dbSScott Long { 21284c7070dbSScott Long iflib_dma_info_t idi = fl->ifl_ifdi; 21298a04b53dSKonstantin Belousov bus_dmamap_t sd_map; 21304c7070dbSScott Long uint32_t i; 21314c7070dbSScott Long 21324c7070dbSScott Long for (i = 0; i < fl->ifl_size; i++) { 2133e035717eSSean Bruno struct mbuf **sd_m = &fl->ifl_sds.ifsd_m[i]; 2134e035717eSSean Bruno caddr_t *sd_cl = &fl->ifl_sds.ifsd_cl[i]; 21354c7070dbSScott Long 2136fbec776dSAndrew Gallatin if (*sd_cl != NULL) { 21378a04b53dSKonstantin Belousov sd_map = fl->ifl_sds.ifsd_map[i]; 2138bfce461eSMarius Strobl bus_dmamap_sync(fl->ifl_buf_tag, sd_map, 21398a04b53dSKonstantin Belousov BUS_DMASYNC_POSTREAD); 2140bfce461eSMarius Strobl bus_dmamap_unload(fl->ifl_buf_tag, sd_map); 2141fbec776dSAndrew Gallatin uma_zfree(fl->ifl_zone, *sd_cl); 2142b256d25cSMark Johnston *sd_cl = NULL; 2143e035717eSSean Bruno if (*sd_m != NULL) { 2144e035717eSSean Bruno m_init(*sd_m, M_NOWAIT, MT_DATA, 0); 2145e035717eSSean Bruno uma_zfree(zone_mbuf, *sd_m); 2146b256d25cSMark Johnston *sd_m = NULL; 2147e035717eSSean Bruno } 21484c7070dbSScott Long } else { 2149e035717eSSean Bruno MPASS(*sd_m == NULL); 21504c7070dbSScott Long } 21514c7070dbSScott Long #if MEMORY_LOGGING 21524c7070dbSScott Long fl->ifl_m_dequeued++; 21534c7070dbSScott Long fl->ifl_cl_dequeued++; 21544c7070dbSScott Long #endif 21554c7070dbSScott Long } 215695246abbSSean Bruno #ifdef INVARIANTS 215795246abbSSean Bruno for (i = 0; i < fl->ifl_size; i++) { 215895246abbSSean Bruno MPASS(fl->ifl_sds.ifsd_cl[i] == NULL); 215995246abbSSean Bruno MPASS(fl->ifl_sds.ifsd_m[i] == NULL); 216095246abbSSean Bruno } 216195246abbSSean Bruno #endif 21624c7070dbSScott Long /* 21634c7070dbSScott Long * Reset free list values 21644c7070dbSScott Long */ 216587890dbaSSean Bruno fl->ifl_credits = fl->ifl_cidx = fl->ifl_pidx = fl->ifl_gen = fl->ifl_fragidx = 0; 21664c7070dbSScott Long bzero(idi->idi_vaddr, idi->idi_size); 21674c7070dbSScott Long } 21684c7070dbSScott Long 21694c7070dbSScott Long /********************************************************************* 21704c7070dbSScott Long * 21711722eeacSMarius Strobl * Initialize a free list and its buffers. 21724c7070dbSScott Long * 21734c7070dbSScott Long **********************************************************************/ 21744c7070dbSScott Long static int 21754c7070dbSScott Long iflib_fl_setup(iflib_fl_t fl) 21764c7070dbSScott Long { 21774c7070dbSScott Long iflib_rxq_t rxq = fl->ifl_rxq; 21784c7070dbSScott Long if_ctx_t ctx = rxq->ifr_ctx; 2179b3813609SPatrick Kelsey if_softc_ctx_t scctx = &ctx->ifc_softc_ctx; 2180b3813609SPatrick Kelsey int qidx; 21814c7070dbSScott Long 21827274b2f6SStephen Hurd bit_nclear(fl->ifl_rx_bitmap, 0, fl->ifl_size - 1); 21834c7070dbSScott Long /* 21844c7070dbSScott Long ** Free current RX buffer structs and their mbufs 21854c7070dbSScott Long */ 21864c7070dbSScott Long iflib_fl_bufs_free(fl); 21874c7070dbSScott Long /* Now replenish the mbufs */ 21884c7070dbSScott Long MPASS(fl->ifl_credits == 0); 2189b3813609SPatrick Kelsey qidx = rxq->ifr_fl_offset + fl->ifl_id; 2190b3813609SPatrick Kelsey if (scctx->isc_rxd_buf_size[qidx] != 0) 2191b3813609SPatrick Kelsey fl->ifl_buf_size = scctx->isc_rxd_buf_size[qidx]; 2192b3813609SPatrick Kelsey else 21931b9d9394SEric Joyner fl->ifl_buf_size = ctx->ifc_rx_mbuf_sz; 2194b3813609SPatrick Kelsey /* 2195b3813609SPatrick Kelsey * ifl_buf_size may be a driver-supplied value, so pull it up 2196b3813609SPatrick Kelsey * to the selected mbuf size. 2197b3813609SPatrick Kelsey */ 2198b3813609SPatrick Kelsey fl->ifl_buf_size = iflib_get_mbuf_size_for(fl->ifl_buf_size); 21994c7070dbSScott Long if (fl->ifl_buf_size > ctx->ifc_max_fl_buf_size) 22004c7070dbSScott Long ctx->ifc_max_fl_buf_size = fl->ifl_buf_size; 22014c7070dbSScott Long fl->ifl_cltype = m_gettype(fl->ifl_buf_size); 22024c7070dbSScott Long fl->ifl_zone = m_getzone(fl->ifl_buf_size); 22034c7070dbSScott Long 220435d8a463SVincenzo Maffione /* 220535d8a463SVincenzo Maffione * Avoid pre-allocating zillions of clusters to an idle card 220635d8a463SVincenzo Maffione * potentially speeding up attach. In any case make sure 220735d8a463SVincenzo Maffione * to leave a descriptor unavailable. See the comment in 220835d8a463SVincenzo Maffione * iflib_fl_refill_all(). 22094c7070dbSScott Long */ 221035d8a463SVincenzo Maffione MPASS(fl->ifl_size > 0); 221135d8a463SVincenzo Maffione (void)iflib_fl_refill(ctx, fl, min(128, fl->ifl_size - 1)); 221235d8a463SVincenzo Maffione if (min(128, fl->ifl_size - 1) != fl->ifl_credits) 22134c7070dbSScott Long return (ENOBUFS); 22144c7070dbSScott Long /* 22154c7070dbSScott Long * handle failure 22164c7070dbSScott Long */ 22174c7070dbSScott Long MPASS(rxq != NULL); 22184c7070dbSScott Long MPASS(fl->ifl_ifdi != NULL); 22194c7070dbSScott Long bus_dmamap_sync(fl->ifl_ifdi->idi_tag, fl->ifl_ifdi->idi_map, 22204c7070dbSScott Long BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 22214c7070dbSScott Long return (0); 22224c7070dbSScott Long } 22234c7070dbSScott Long 22244c7070dbSScott Long /********************************************************************* 22254c7070dbSScott Long * 22264c7070dbSScott Long * Free receive ring data structures 22274c7070dbSScott Long * 22284c7070dbSScott Long **********************************************************************/ 22294c7070dbSScott Long static void 22304c7070dbSScott Long iflib_rx_sds_free(iflib_rxq_t rxq) 22314c7070dbSScott Long { 22324c7070dbSScott Long iflib_fl_t fl; 22338a04b53dSKonstantin Belousov int i, j; 22344c7070dbSScott Long 22354c7070dbSScott Long if (rxq->ifr_fl != NULL) { 22364c7070dbSScott Long for (i = 0; i < rxq->ifr_nfl; i++) { 22374c7070dbSScott Long fl = &rxq->ifr_fl[i]; 2238bfce461eSMarius Strobl if (fl->ifl_buf_tag != NULL) { 22398a04b53dSKonstantin Belousov if (fl->ifl_sds.ifsd_map != NULL) { 224077102fd6SAndrew Gallatin for (j = 0; j < fl->ifl_size; j++) { 22418a04b53dSKonstantin Belousov bus_dmamap_sync( 2242bfce461eSMarius Strobl fl->ifl_buf_tag, 224377102fd6SAndrew Gallatin fl->ifl_sds.ifsd_map[j], 22448a04b53dSKonstantin Belousov BUS_DMASYNC_POSTREAD); 22458a04b53dSKonstantin Belousov bus_dmamap_unload( 2246bfce461eSMarius Strobl fl->ifl_buf_tag, 224777102fd6SAndrew Gallatin fl->ifl_sds.ifsd_map[j]); 2248db8e8f1eSEric Joyner bus_dmamap_destroy( 2249db8e8f1eSEric Joyner fl->ifl_buf_tag, 2250db8e8f1eSEric Joyner fl->ifl_sds.ifsd_map[j]); 22518a04b53dSKonstantin Belousov } 22528a04b53dSKonstantin Belousov } 2253bfce461eSMarius Strobl bus_dma_tag_destroy(fl->ifl_buf_tag); 2254bfce461eSMarius Strobl fl->ifl_buf_tag = NULL; 22554c7070dbSScott Long } 2256e035717eSSean Bruno free(fl->ifl_sds.ifsd_m, M_IFLIB); 2257e035717eSSean Bruno free(fl->ifl_sds.ifsd_cl, M_IFLIB); 2258fbec776dSAndrew Gallatin free(fl->ifl_sds.ifsd_ba, M_IFLIB); 2259e035717eSSean Bruno free(fl->ifl_sds.ifsd_map, M_IFLIB); 2260e035717eSSean Bruno fl->ifl_sds.ifsd_m = NULL; 2261e035717eSSean Bruno fl->ifl_sds.ifsd_cl = NULL; 2262fbec776dSAndrew Gallatin fl->ifl_sds.ifsd_ba = NULL; 2263e035717eSSean Bruno fl->ifl_sds.ifsd_map = NULL; 22644c7070dbSScott Long } 22654c7070dbSScott Long free(rxq->ifr_fl, M_IFLIB); 22664c7070dbSScott Long rxq->ifr_fl = NULL; 2267244e7cffSEric Joyner free(rxq->ifr_ifdi, M_IFLIB); 2268244e7cffSEric Joyner rxq->ifr_ifdi = NULL; 22691722eeacSMarius Strobl rxq->ifr_cq_cidx = 0; 22704c7070dbSScott Long } 22714c7070dbSScott Long } 22724c7070dbSScott Long 22734c7070dbSScott Long /* 22741722eeacSMarius Strobl * Timer routine 22754c7070dbSScott Long */ 22764c7070dbSScott Long static void 22774c7070dbSScott Long iflib_timer(void *arg) 22784c7070dbSScott Long { 2279ab2e3f79SStephen Hurd iflib_txq_t txq = arg; 22804c7070dbSScott Long if_ctx_t ctx = txq->ift_ctx; 2281ab2e3f79SStephen Hurd if_softc_ctx_t sctx = &ctx->ifc_softc_ctx; 2282dd7fbcf1SStephen Hurd uint64_t this_tick = ticks; 22834c7070dbSScott Long 22844c7070dbSScott Long if (!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING)) 22854c7070dbSScott Long return; 22861722eeacSMarius Strobl 22874c7070dbSScott Long /* 22884c7070dbSScott Long ** Check on the state of the TX queue(s), this 22894c7070dbSScott Long ** can be done without the lock because its RO 22904c7070dbSScott Long ** and the HUNG state will be static if set. 22914c7070dbSScott Long */ 2292dd7fbcf1SStephen Hurd if (this_tick - txq->ift_last_timer_tick >= hz / 2) { 2293dd7fbcf1SStephen Hurd txq->ift_last_timer_tick = this_tick; 2294ab2e3f79SStephen Hurd IFDI_TIMER(ctx, txq->ift_id); 2295ab2e3f79SStephen Hurd if ((txq->ift_qstatus == IFLIB_QUEUE_HUNG) && 2296ab2e3f79SStephen Hurd ((txq->ift_cleaned_prev == txq->ift_cleaned) || 2297ab2e3f79SStephen Hurd (sctx->isc_pause_frames == 0))) 2298ab2e3f79SStephen Hurd goto hung; 2299a9693502SSean Bruno 2300f6afed72SEric Joyner if (txq->ift_qstatus != IFLIB_QUEUE_IDLE && 2301f6afed72SEric Joyner ifmp_ring_is_stalled(txq->ift_br)) { 2302f6afed72SEric Joyner KASSERT(ctx->ifc_link_state == LINK_STATE_UP, ("queue can't be marked as hung if interface is down")); 2303ab2e3f79SStephen Hurd txq->ift_qstatus = IFLIB_QUEUE_HUNG; 2304f6afed72SEric Joyner } 2305ab2e3f79SStephen Hurd txq->ift_cleaned_prev = txq->ift_cleaned; 2306dd7fbcf1SStephen Hurd } 2307ab2e3f79SStephen Hurd /* handle any laggards */ 2308ab2e3f79SStephen Hurd if (txq->ift_db_pending) 2309ab2e3f79SStephen Hurd GROUPTASK_ENQUEUE(&txq->ift_task); 2310a9693502SSean Bruno 2311ab2e3f79SStephen Hurd sctx->isc_pause_frames = 0; 2312d300df01SStephen Hurd if (if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING) 2313*17cec474SVincenzo Maffione callout_reset_on(&txq->ift_timer, hz / 2, iflib_timer, txq, txq->ift_timer.c_cpu); 2314ab2e3f79SStephen Hurd return; 23151722eeacSMarius Strobl 2316ab2e3f79SStephen Hurd hung: 23171722eeacSMarius Strobl device_printf(ctx->ifc_dev, 23181722eeacSMarius Strobl "Watchdog timeout (TX: %d desc avail: %d pidx: %d) -- resetting\n", 2319ab2e3f79SStephen Hurd txq->ift_id, TXQ_AVAIL(txq), txq->ift_pidx); 23207b610b60SSean Bruno STATE_LOCK(ctx); 23217b610b60SSean Bruno if_setdrvflagbits(ctx->ifc_ifp, IFF_DRV_OACTIVE, IFF_DRV_RUNNING); 23227b610b60SSean Bruno ctx->ifc_flags |= (IFC_DO_WATCHDOG|IFC_DO_RESET); 2323940f62d6SEric Joyner iflib_admin_intr_deferred(ctx); 232446fa0c25SEric Joyner STATE_UNLOCK(ctx); 23254c7070dbSScott Long } 23264c7070dbSScott Long 2327b3813609SPatrick Kelsey static uint16_t 2328b3813609SPatrick Kelsey iflib_get_mbuf_size_for(unsigned int size) 2329b3813609SPatrick Kelsey { 2330b3813609SPatrick Kelsey 2331b3813609SPatrick Kelsey if (size <= MCLBYTES) 2332b3813609SPatrick Kelsey return (MCLBYTES); 2333b3813609SPatrick Kelsey else 2334b3813609SPatrick Kelsey return (MJUMPAGESIZE); 2335b3813609SPatrick Kelsey } 2336b3813609SPatrick Kelsey 23374c7070dbSScott Long static void 23381b9d9394SEric Joyner iflib_calc_rx_mbuf_sz(if_ctx_t ctx) 23391b9d9394SEric Joyner { 23401b9d9394SEric Joyner if_softc_ctx_t sctx = &ctx->ifc_softc_ctx; 23411b9d9394SEric Joyner 23421b9d9394SEric Joyner /* 23431b9d9394SEric Joyner * XXX don't set the max_frame_size to larger 23441b9d9394SEric Joyner * than the hardware can handle 23451b9d9394SEric Joyner */ 2346b3813609SPatrick Kelsey ctx->ifc_rx_mbuf_sz = 2347b3813609SPatrick Kelsey iflib_get_mbuf_size_for(sctx->isc_max_frame_size); 23481b9d9394SEric Joyner } 23491b9d9394SEric Joyner 23501b9d9394SEric Joyner uint32_t 23511b9d9394SEric Joyner iflib_get_rx_mbuf_sz(if_ctx_t ctx) 23521b9d9394SEric Joyner { 23531722eeacSMarius Strobl 23541b9d9394SEric Joyner return (ctx->ifc_rx_mbuf_sz); 23551b9d9394SEric Joyner } 23561b9d9394SEric Joyner 23571b9d9394SEric Joyner static void 23584c7070dbSScott Long iflib_init_locked(if_ctx_t ctx) 23594c7070dbSScott Long { 23604c7070dbSScott Long if_softc_ctx_t sctx = &ctx->ifc_softc_ctx; 23611248952aSSean Bruno if_softc_ctx_t scctx = &ctx->ifc_softc_ctx; 23624c7070dbSScott Long if_t ifp = ctx->ifc_ifp; 23634c7070dbSScott Long iflib_fl_t fl; 23644c7070dbSScott Long iflib_txq_t txq; 23654c7070dbSScott Long iflib_rxq_t rxq; 2366ab2e3f79SStephen Hurd int i, j, tx_ip_csum_flags, tx_ip6_csum_flags; 23674c7070dbSScott Long 23684c7070dbSScott Long if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, IFF_DRV_RUNNING); 23694c7070dbSScott Long IFDI_INTR_DISABLE(ctx); 23704c7070dbSScott Long 23711248952aSSean Bruno tx_ip_csum_flags = scctx->isc_tx_csum_flags & (CSUM_IP | CSUM_TCP | CSUM_UDP | CSUM_SCTP); 23721248952aSSean Bruno tx_ip6_csum_flags = scctx->isc_tx_csum_flags & (CSUM_IP6_TCP | CSUM_IP6_UDP | CSUM_IP6_SCTP); 23734c7070dbSScott Long /* Set hardware offload abilities */ 23744c7070dbSScott Long if_clearhwassist(ifp); 23754c7070dbSScott Long if (if_getcapenable(ifp) & IFCAP_TXCSUM) 23761248952aSSean Bruno if_sethwassistbits(ifp, tx_ip_csum_flags, 0); 23774c7070dbSScott Long if (if_getcapenable(ifp) & IFCAP_TXCSUM_IPV6) 23781248952aSSean Bruno if_sethwassistbits(ifp, tx_ip6_csum_flags, 0); 23794c7070dbSScott Long if (if_getcapenable(ifp) & IFCAP_TSO4) 23804c7070dbSScott Long if_sethwassistbits(ifp, CSUM_IP_TSO, 0); 23814c7070dbSScott Long if (if_getcapenable(ifp) & IFCAP_TSO6) 23824c7070dbSScott Long if_sethwassistbits(ifp, CSUM_IP6_TSO, 0); 23834c7070dbSScott Long 23844c7070dbSScott Long for (i = 0, txq = ctx->ifc_txqs; i < sctx->isc_ntxqsets; i++, txq++) { 23854c7070dbSScott Long CALLOUT_LOCK(txq); 23864c7070dbSScott Long callout_stop(&txq->ift_timer); 2387*17cec474SVincenzo Maffione #ifdef DEV_NETMAP 2388*17cec474SVincenzo Maffione callout_stop(&txq->ift_netmap_timer); 2389*17cec474SVincenzo Maffione #endif /* DEV_NETMAP */ 23904c7070dbSScott Long CALLOUT_UNLOCK(txq); 23914c7070dbSScott Long iflib_netmap_txq_init(ctx, txq); 23924c7070dbSScott Long } 23931b9d9394SEric Joyner 23941b9d9394SEric Joyner /* 23951b9d9394SEric Joyner * Calculate a suitable Rx mbuf size prior to calling IFDI_INIT, so 23961b9d9394SEric Joyner * that drivers can use the value when setting up the hardware receive 23971b9d9394SEric Joyner * buffers. 23981b9d9394SEric Joyner */ 23991b9d9394SEric Joyner iflib_calc_rx_mbuf_sz(ctx); 24001b9d9394SEric Joyner 240123ac9029SStephen Hurd #ifdef INVARIANTS 240223ac9029SStephen Hurd i = if_getdrvflags(ifp); 240323ac9029SStephen Hurd #endif 24044c7070dbSScott Long IFDI_INIT(ctx); 240523ac9029SStephen Hurd MPASS(if_getdrvflags(ifp) == i); 24064c7070dbSScott Long for (i = 0, rxq = ctx->ifc_rxqs; i < sctx->isc_nrxqsets; i++, rxq++) { 2407d8b2d26bSVincenzo Maffione if (iflib_netmap_rxq_init(ctx, rxq) > 0) { 2408d8b2d26bSVincenzo Maffione /* This rxq is in netmap mode. Skip normal init. */ 240995246abbSSean Bruno continue; 2410d0d0ad0aSStephen Hurd } 24114c7070dbSScott Long for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++) { 24124c7070dbSScott Long if (iflib_fl_setup(fl)) { 24133d10e9edSMarius Strobl device_printf(ctx->ifc_dev, 24143d10e9edSMarius Strobl "setting up free list %d failed - " 24153d10e9edSMarius Strobl "check cluster settings\n", j); 24164c7070dbSScott Long goto done; 24174c7070dbSScott Long } 24184c7070dbSScott Long } 24194c7070dbSScott Long } 24204c7070dbSScott Long done: 24214c7070dbSScott Long if_setdrvflagbits(ctx->ifc_ifp, IFF_DRV_RUNNING, IFF_DRV_OACTIVE); 24224c7070dbSScott Long IFDI_INTR_ENABLE(ctx); 24234c7070dbSScott Long txq = ctx->ifc_txqs; 24244c7070dbSScott Long for (i = 0; i < sctx->isc_ntxqsets; i++, txq++) 2425ab2e3f79SStephen Hurd callout_reset_on(&txq->ift_timer, hz/2, iflib_timer, txq, 2426ab2e3f79SStephen Hurd txq->ift_timer.c_cpu); 24274c7070dbSScott Long } 24284c7070dbSScott Long 24294c7070dbSScott Long static int 24304c7070dbSScott Long iflib_media_change(if_t ifp) 24314c7070dbSScott Long { 24324c7070dbSScott Long if_ctx_t ctx = if_getsoftc(ifp); 24334c7070dbSScott Long int err; 24344c7070dbSScott Long 24354c7070dbSScott Long CTX_LOCK(ctx); 24364c7070dbSScott Long if ((err = IFDI_MEDIA_CHANGE(ctx)) == 0) 24374c7070dbSScott Long iflib_init_locked(ctx); 24384c7070dbSScott Long CTX_UNLOCK(ctx); 24394c7070dbSScott Long return (err); 24404c7070dbSScott Long } 24414c7070dbSScott Long 24424c7070dbSScott Long static void 24434c7070dbSScott Long iflib_media_status(if_t ifp, struct ifmediareq *ifmr) 24444c7070dbSScott Long { 24454c7070dbSScott Long if_ctx_t ctx = if_getsoftc(ifp); 24464c7070dbSScott Long 24474c7070dbSScott Long CTX_LOCK(ctx); 2448ab2e3f79SStephen Hurd IFDI_UPDATE_ADMIN_STATUS(ctx); 24494c7070dbSScott Long IFDI_MEDIA_STATUS(ctx, ifmr); 24504c7070dbSScott Long CTX_UNLOCK(ctx); 24514c7070dbSScott Long } 24524c7070dbSScott Long 245309f6ff4fSMatt Macy void 24544c7070dbSScott Long iflib_stop(if_ctx_t ctx) 24554c7070dbSScott Long { 24564c7070dbSScott Long iflib_txq_t txq = ctx->ifc_txqs; 24574c7070dbSScott Long iflib_rxq_t rxq = ctx->ifc_rxqs; 24584c7070dbSScott Long if_softc_ctx_t scctx = &ctx->ifc_softc_ctx; 24594d261ce2SStephen Hurd if_shared_ctx_t sctx = ctx->ifc_sctx; 24604c7070dbSScott Long iflib_dma_info_t di; 24614c7070dbSScott Long iflib_fl_t fl; 24624c7070dbSScott Long int i, j; 24634c7070dbSScott Long 24644c7070dbSScott Long /* Tell the stack that the interface is no longer active */ 24654c7070dbSScott Long if_setdrvflagbits(ctx->ifc_ifp, IFF_DRV_OACTIVE, IFF_DRV_RUNNING); 24664c7070dbSScott Long 24674c7070dbSScott Long IFDI_INTR_DISABLE(ctx); 2468ab2e3f79SStephen Hurd DELAY(1000); 2469da69b8f9SSean Bruno IFDI_STOP(ctx); 2470ab2e3f79SStephen Hurd DELAY(1000); 24714c7070dbSScott Long 2472da69b8f9SSean Bruno iflib_debug_reset(); 24734c7070dbSScott Long /* Wait for current tx queue users to exit to disarm watchdog timer. */ 24744c7070dbSScott Long for (i = 0; i < scctx->isc_ntxqsets; i++, txq++) { 24754c7070dbSScott Long /* make sure all transmitters have completed before proceeding XXX */ 24764c7070dbSScott Long 2477226fb85dSStephen Hurd CALLOUT_LOCK(txq); 2478226fb85dSStephen Hurd callout_stop(&txq->ift_timer); 2479*17cec474SVincenzo Maffione #ifdef DEV_NETMAP 2480*17cec474SVincenzo Maffione callout_stop(&txq->ift_netmap_timer); 2481*17cec474SVincenzo Maffione #endif /* DEV_NETMAP */ 2482226fb85dSStephen Hurd CALLOUT_UNLOCK(txq); 2483226fb85dSStephen Hurd 24844c7070dbSScott Long /* clean any enqueued buffers */ 2485da69b8f9SSean Bruno iflib_ifmp_purge(txq); 24864c7070dbSScott Long /* Free any existing tx buffers. */ 248723ac9029SStephen Hurd for (j = 0; j < txq->ift_size; j++) { 24884c7070dbSScott Long iflib_txsd_free(ctx, txq, j); 24894c7070dbSScott Long } 2490ab2e3f79SStephen Hurd txq->ift_processed = txq->ift_cleaned = txq->ift_cidx_processed = 0; 2491ab2e3f79SStephen Hurd txq->ift_in_use = txq->ift_gen = txq->ift_cidx = txq->ift_pidx = txq->ift_no_desc_avail = 0; 24924c7070dbSScott Long txq->ift_closed = txq->ift_mbuf_defrag = txq->ift_mbuf_defrag_failed = 0; 24934c7070dbSScott Long txq->ift_no_tx_dma_setup = txq->ift_txd_encap_efbig = txq->ift_map_failed = 0; 2494ab2e3f79SStephen Hurd txq->ift_pullups = 0; 249595246abbSSean Bruno ifmp_ring_reset_stats(txq->ift_br); 24964d261ce2SStephen Hurd for (j = 0, di = txq->ift_ifdi; j < sctx->isc_ntxqs; j++, di++) 24974c7070dbSScott Long bzero((void *)di->idi_vaddr, di->idi_size); 24984c7070dbSScott Long } 24994c7070dbSScott Long for (i = 0; i < scctx->isc_nrxqsets; i++, rxq++) { 25004c7070dbSScott Long /* make sure all transmitters have completed before proceeding XXX */ 25014c7070dbSScott Long 25021722eeacSMarius Strobl rxq->ifr_cq_cidx = 0; 25034d261ce2SStephen Hurd for (j = 0, di = rxq->ifr_ifdi; j < sctx->isc_nrxqs; j++, di++) 25044c7070dbSScott Long bzero((void *)di->idi_vaddr, di->idi_size); 25054c7070dbSScott Long /* also resets the free lists pidx/cidx */ 25064c7070dbSScott Long for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++) 25074c7070dbSScott Long iflib_fl_bufs_free(fl); 25084c7070dbSScott Long } 25094c7070dbSScott Long } 25104c7070dbSScott Long 251195246abbSSean Bruno static inline caddr_t 251295246abbSSean Bruno calc_next_rxd(iflib_fl_t fl, int cidx) 251395246abbSSean Bruno { 251495246abbSSean Bruno qidx_t size; 251595246abbSSean Bruno int nrxd; 251695246abbSSean Bruno caddr_t start, end, cur, next; 251795246abbSSean Bruno 251895246abbSSean Bruno nrxd = fl->ifl_size; 251995246abbSSean Bruno size = fl->ifl_rxd_size; 252095246abbSSean Bruno start = fl->ifl_ifdi->idi_vaddr; 252195246abbSSean Bruno 252295246abbSSean Bruno if (__predict_false(size == 0)) 252395246abbSSean Bruno return (start); 252495246abbSSean Bruno cur = start + size*cidx; 252595246abbSSean Bruno end = start + size*nrxd; 252695246abbSSean Bruno next = CACHE_PTR_NEXT(cur); 252795246abbSSean Bruno return (next < end ? next : start); 252895246abbSSean Bruno } 252995246abbSSean Bruno 2530e035717eSSean Bruno static inline void 2531e035717eSSean Bruno prefetch_pkts(iflib_fl_t fl, int cidx) 2532e035717eSSean Bruno { 2533e035717eSSean Bruno int nextptr; 2534e035717eSSean Bruno int nrxd = fl->ifl_size; 253595246abbSSean Bruno caddr_t next_rxd; 253695246abbSSean Bruno 2537e035717eSSean Bruno nextptr = (cidx + CACHE_PTR_INCREMENT) & (nrxd-1); 2538e035717eSSean Bruno prefetch(&fl->ifl_sds.ifsd_m[nextptr]); 2539e035717eSSean Bruno prefetch(&fl->ifl_sds.ifsd_cl[nextptr]); 254095246abbSSean Bruno next_rxd = calc_next_rxd(fl, cidx); 254195246abbSSean Bruno prefetch(next_rxd); 2542e035717eSSean Bruno prefetch(fl->ifl_sds.ifsd_m[(cidx + 1) & (nrxd-1)]); 2543e035717eSSean Bruno prefetch(fl->ifl_sds.ifsd_m[(cidx + 2) & (nrxd-1)]); 2544e035717eSSean Bruno prefetch(fl->ifl_sds.ifsd_m[(cidx + 3) & (nrxd-1)]); 2545e035717eSSean Bruno prefetch(fl->ifl_sds.ifsd_m[(cidx + 4) & (nrxd-1)]); 2546e035717eSSean Bruno prefetch(fl->ifl_sds.ifsd_cl[(cidx + 1) & (nrxd-1)]); 2547e035717eSSean Bruno prefetch(fl->ifl_sds.ifsd_cl[(cidx + 2) & (nrxd-1)]); 2548e035717eSSean Bruno prefetch(fl->ifl_sds.ifsd_cl[(cidx + 3) & (nrxd-1)]); 2549e035717eSSean Bruno prefetch(fl->ifl_sds.ifsd_cl[(cidx + 4) & (nrxd-1)]); 2550e035717eSSean Bruno } 2551e035717eSSean Bruno 25526d49b41eSAndrew Gallatin static struct mbuf * 25536d49b41eSAndrew Gallatin rxd_frag_to_sd(iflib_rxq_t rxq, if_rxd_frag_t irf, bool unload, if_rxsd_t sd, 25546d49b41eSAndrew Gallatin int *pf_rv, if_rxd_info_t ri) 25554c7070dbSScott Long { 2556e035717eSSean Bruno bus_dmamap_t map; 25574c7070dbSScott Long iflib_fl_t fl; 25586d49b41eSAndrew Gallatin caddr_t payload; 25596d49b41eSAndrew Gallatin struct mbuf *m; 25606d49b41eSAndrew Gallatin int flid, cidx, len, next; 25614c7070dbSScott Long 256295246abbSSean Bruno map = NULL; 25634c7070dbSScott Long flid = irf->irf_flid; 25644c7070dbSScott Long cidx = irf->irf_idx; 25654c7070dbSScott Long fl = &rxq->ifr_fl[flid]; 256695246abbSSean Bruno sd->ifsd_fl = fl; 25676d49b41eSAndrew Gallatin m = fl->ifl_sds.ifsd_m[cidx]; 256895246abbSSean Bruno sd->ifsd_cl = &fl->ifl_sds.ifsd_cl[cidx]; 25694c7070dbSScott Long fl->ifl_credits--; 25704c7070dbSScott Long #if MEMORY_LOGGING 25714c7070dbSScott Long fl->ifl_m_dequeued++; 25724c7070dbSScott Long #endif 257395246abbSSean Bruno if (rxq->ifr_ctx->ifc_flags & IFC_PREFETCH) 2574e035717eSSean Bruno prefetch_pkts(fl, cidx); 2575e035717eSSean Bruno next = (cidx + CACHE_PTR_INCREMENT) & (fl->ifl_size-1); 2576e035717eSSean Bruno prefetch(&fl->ifl_sds.ifsd_map[next]); 2577e035717eSSean Bruno map = fl->ifl_sds.ifsd_map[cidx]; 25784c7070dbSScott Long 2579bfce461eSMarius Strobl bus_dmamap_sync(fl->ifl_buf_tag, map, BUS_DMASYNC_POSTREAD); 25806d49b41eSAndrew Gallatin 25814f2beb72SPatrick Kelsey if (rxq->pfil != NULL && PFIL_HOOKED_IN(rxq->pfil) && pf_rv != NULL && 25824f2beb72SPatrick Kelsey irf->irf_len != 0) { 25836d49b41eSAndrew Gallatin payload = *sd->ifsd_cl; 25846d49b41eSAndrew Gallatin payload += ri->iri_pad; 25856d49b41eSAndrew Gallatin len = ri->iri_len - ri->iri_pad; 25866d49b41eSAndrew Gallatin *pf_rv = pfil_run_hooks(rxq->pfil, payload, ri->iri_ifp, 25876d49b41eSAndrew Gallatin len | PFIL_MEMPTR | PFIL_IN, NULL); 25886d49b41eSAndrew Gallatin switch (*pf_rv) { 25896d49b41eSAndrew Gallatin case PFIL_DROPPED: 25906d49b41eSAndrew Gallatin case PFIL_CONSUMED: 25916d49b41eSAndrew Gallatin /* 25926d49b41eSAndrew Gallatin * The filter ate it. Everything is recycled. 25936d49b41eSAndrew Gallatin */ 25946d49b41eSAndrew Gallatin m = NULL; 25956d49b41eSAndrew Gallatin unload = 0; 25966d49b41eSAndrew Gallatin break; 25976d49b41eSAndrew Gallatin case PFIL_REALLOCED: 25986d49b41eSAndrew Gallatin /* 25996d49b41eSAndrew Gallatin * The filter copied it. Everything is recycled. 26006d49b41eSAndrew Gallatin */ 26016d49b41eSAndrew Gallatin m = pfil_mem2mbuf(payload); 26026d49b41eSAndrew Gallatin unload = 0; 26036d49b41eSAndrew Gallatin break; 26046d49b41eSAndrew Gallatin case PFIL_PASS: 26056d49b41eSAndrew Gallatin /* 26066d49b41eSAndrew Gallatin * Filter said it was OK, so receive like 26076d49b41eSAndrew Gallatin * normal 26086d49b41eSAndrew Gallatin */ 26096d49b41eSAndrew Gallatin fl->ifl_sds.ifsd_m[cidx] = NULL; 26106d49b41eSAndrew Gallatin break; 26116d49b41eSAndrew Gallatin default: 26126d49b41eSAndrew Gallatin MPASS(0); 26136d49b41eSAndrew Gallatin } 26146d49b41eSAndrew Gallatin } else { 26156d49b41eSAndrew Gallatin fl->ifl_sds.ifsd_m[cidx] = NULL; 26166d49b41eSAndrew Gallatin *pf_rv = PFIL_PASS; 26176d49b41eSAndrew Gallatin } 26186d49b41eSAndrew Gallatin 26194f2beb72SPatrick Kelsey if (unload && irf->irf_len != 0) 2620bfce461eSMarius Strobl bus_dmamap_unload(fl->ifl_buf_tag, map); 262195246abbSSean Bruno fl->ifl_cidx = (fl->ifl_cidx + 1) & (fl->ifl_size-1); 262295246abbSSean Bruno if (__predict_false(fl->ifl_cidx == 0)) 26234c7070dbSScott Long fl->ifl_gen = 0; 262487890dbaSSean Bruno bit_clear(fl->ifl_rx_bitmap, cidx); 26256d49b41eSAndrew Gallatin return (m); 26264c7070dbSScott Long } 26274c7070dbSScott Long 26284c7070dbSScott Long static struct mbuf * 26296d49b41eSAndrew Gallatin assemble_segments(iflib_rxq_t rxq, if_rxd_info_t ri, if_rxsd_t sd, int *pf_rv) 26304c7070dbSScott Long { 263195246abbSSean Bruno struct mbuf *m, *mh, *mt; 263295246abbSSean Bruno caddr_t cl; 26336d49b41eSAndrew Gallatin int *pf_rv_ptr, flags, i, padlen; 26346d49b41eSAndrew Gallatin bool consumed; 26354c7070dbSScott Long 26364c7070dbSScott Long i = 0; 263723ac9029SStephen Hurd mh = NULL; 26386d49b41eSAndrew Gallatin consumed = false; 26396d49b41eSAndrew Gallatin *pf_rv = PFIL_PASS; 26406d49b41eSAndrew Gallatin pf_rv_ptr = pf_rv; 26414c7070dbSScott Long do { 26426d49b41eSAndrew Gallatin m = rxd_frag_to_sd(rxq, &ri->iri_frags[i], !consumed, sd, 26436d49b41eSAndrew Gallatin pf_rv_ptr, ri); 26444c7070dbSScott Long 264595246abbSSean Bruno MPASS(*sd->ifsd_cl != NULL); 264623ac9029SStephen Hurd 26476d49b41eSAndrew Gallatin /* 26486d49b41eSAndrew Gallatin * Exclude zero-length frags & frags from 26496d49b41eSAndrew Gallatin * packets the filter has consumed or dropped 26506d49b41eSAndrew Gallatin */ 26516d49b41eSAndrew Gallatin if (ri->iri_frags[i].irf_len == 0 || consumed || 26526d49b41eSAndrew Gallatin *pf_rv == PFIL_CONSUMED || *pf_rv == PFIL_DROPPED) { 26536d49b41eSAndrew Gallatin if (mh == NULL) { 26546d49b41eSAndrew Gallatin /* everything saved here */ 26556d49b41eSAndrew Gallatin consumed = true; 26566d49b41eSAndrew Gallatin pf_rv_ptr = NULL; 265723ac9029SStephen Hurd continue; 265823ac9029SStephen Hurd } 26596d49b41eSAndrew Gallatin /* XXX we can save the cluster here, but not the mbuf */ 26606d49b41eSAndrew Gallatin m_init(m, M_NOWAIT, MT_DATA, 0); 26616d49b41eSAndrew Gallatin m_free(m); 26626d49b41eSAndrew Gallatin continue; 26636d49b41eSAndrew Gallatin } 266423ac9029SStephen Hurd if (mh == NULL) { 26654c7070dbSScott Long flags = M_PKTHDR|M_EXT; 26664c7070dbSScott Long mh = mt = m; 26674c7070dbSScott Long padlen = ri->iri_pad; 26684c7070dbSScott Long } else { 26694c7070dbSScott Long flags = M_EXT; 26704c7070dbSScott Long mt->m_next = m; 26714c7070dbSScott Long mt = m; 26724c7070dbSScott Long /* assuming padding is only on the first fragment */ 26734c7070dbSScott Long padlen = 0; 26744c7070dbSScott Long } 267595246abbSSean Bruno cl = *sd->ifsd_cl; 267695246abbSSean Bruno *sd->ifsd_cl = NULL; 26774c7070dbSScott Long 26784c7070dbSScott Long /* Can these two be made one ? */ 26794c7070dbSScott Long m_init(m, M_NOWAIT, MT_DATA, flags); 268095246abbSSean Bruno m_cljset(m, cl, sd->ifsd_fl->ifl_cltype); 26814c7070dbSScott Long /* 26824c7070dbSScott Long * These must follow m_init and m_cljset 26834c7070dbSScott Long */ 26844c7070dbSScott Long m->m_data += padlen; 26854c7070dbSScott Long ri->iri_len -= padlen; 268623ac9029SStephen Hurd m->m_len = ri->iri_frags[i].irf_len; 26874c7070dbSScott Long } while (++i < ri->iri_nfrags); 26884c7070dbSScott Long 26894c7070dbSScott Long return (mh); 26904c7070dbSScott Long } 26914c7070dbSScott Long 26924c7070dbSScott Long /* 26934c7070dbSScott Long * Process one software descriptor 26944c7070dbSScott Long */ 26954c7070dbSScott Long static struct mbuf * 26964c7070dbSScott Long iflib_rxd_pkt_get(iflib_rxq_t rxq, if_rxd_info_t ri) 26974c7070dbSScott Long { 269895246abbSSean Bruno struct if_rxsd sd; 26994c7070dbSScott Long struct mbuf *m; 27006d49b41eSAndrew Gallatin int pf_rv; 27014c7070dbSScott Long 27024c7070dbSScott Long /* should I merge this back in now that the two paths are basically duplicated? */ 270323ac9029SStephen Hurd if (ri->iri_nfrags == 1 && 27044f2beb72SPatrick Kelsey ri->iri_frags[0].irf_len != 0 && 270518628b74SMark Johnston ri->iri_frags[0].irf_len <= MIN(IFLIB_RX_COPY_THRESH, MHLEN)) { 27066d49b41eSAndrew Gallatin m = rxd_frag_to_sd(rxq, &ri->iri_frags[0], false, &sd, 27076d49b41eSAndrew Gallatin &pf_rv, ri); 27086d49b41eSAndrew Gallatin if (pf_rv != PFIL_PASS && pf_rv != PFIL_REALLOCED) 27096d49b41eSAndrew Gallatin return (m); 27106d49b41eSAndrew Gallatin if (pf_rv == PFIL_PASS) { 27114c7070dbSScott Long m_init(m, M_NOWAIT, MT_DATA, M_PKTHDR); 271295246abbSSean Bruno #ifndef __NO_STRICT_ALIGNMENT 271395246abbSSean Bruno if (!IP_ALIGNED(m)) 271495246abbSSean Bruno m->m_data += 2; 271595246abbSSean Bruno #endif 271695246abbSSean Bruno memcpy(m->m_data, *sd.ifsd_cl, ri->iri_len); 271723ac9029SStephen Hurd m->m_len = ri->iri_frags[0].irf_len; 27186d49b41eSAndrew Gallatin } 27194c7070dbSScott Long } else { 27206d49b41eSAndrew Gallatin m = assemble_segments(rxq, ri, &sd, &pf_rv); 27214f2beb72SPatrick Kelsey if (m == NULL) 27224f2beb72SPatrick Kelsey return (NULL); 27236d49b41eSAndrew Gallatin if (pf_rv != PFIL_PASS && pf_rv != PFIL_REALLOCED) 27246d49b41eSAndrew Gallatin return (m); 27254c7070dbSScott Long } 27264c7070dbSScott Long m->m_pkthdr.len = ri->iri_len; 27274c7070dbSScott Long m->m_pkthdr.rcvif = ri->iri_ifp; 27284c7070dbSScott Long m->m_flags |= ri->iri_flags; 27294c7070dbSScott Long m->m_pkthdr.ether_vtag = ri->iri_vtag; 27304c7070dbSScott Long m->m_pkthdr.flowid = ri->iri_flowid; 27314c7070dbSScott Long M_HASHTYPE_SET(m, ri->iri_rsstype); 27324c7070dbSScott Long m->m_pkthdr.csum_flags = ri->iri_csum_flags; 27334c7070dbSScott Long m->m_pkthdr.csum_data = ri->iri_csum_data; 27344c7070dbSScott Long return (m); 27354c7070dbSScott Long } 27364c7070dbSScott Long 273735e4e998SStephen Hurd #if defined(INET6) || defined(INET) 2738fe1bcadaSStephen Hurd static void 2739fe1bcadaSStephen Hurd iflib_get_ip_forwarding(struct lro_ctrl *lc, bool *v4, bool *v6) 2740fe1bcadaSStephen Hurd { 2741fe1bcadaSStephen Hurd CURVNET_SET(lc->ifp->if_vnet); 2742fe1bcadaSStephen Hurd #if defined(INET6) 2743188adcb7SMarko Zec *v6 = V_ip6_forwarding; 2744fe1bcadaSStephen Hurd #endif 2745fe1bcadaSStephen Hurd #if defined(INET) 2746188adcb7SMarko Zec *v4 = V_ipforwarding; 2747fe1bcadaSStephen Hurd #endif 2748fe1bcadaSStephen Hurd CURVNET_RESTORE(); 2749fe1bcadaSStephen Hurd } 2750fe1bcadaSStephen Hurd 275135e4e998SStephen Hurd /* 275235e4e998SStephen Hurd * Returns true if it's possible this packet could be LROed. 275335e4e998SStephen Hurd * if it returns false, it is guaranteed that tcp_lro_rx() 275435e4e998SStephen Hurd * would not return zero. 275535e4e998SStephen Hurd */ 275635e4e998SStephen Hurd static bool 2757fe1bcadaSStephen Hurd iflib_check_lro_possible(struct mbuf *m, bool v4_forwarding, bool v6_forwarding) 275835e4e998SStephen Hurd { 275935e4e998SStephen Hurd struct ether_header *eh; 276035e4e998SStephen Hurd 276135e4e998SStephen Hurd eh = mtod(m, struct ether_header *); 27626aee0bfaSMarko Zec switch (eh->ether_type) { 2763abec4724SSean Bruno #if defined(INET6) 27646aee0bfaSMarko Zec case htons(ETHERTYPE_IPV6): 27656aee0bfaSMarko Zec return (!v6_forwarding); 2766abec4724SSean Bruno #endif 2767abec4724SSean Bruno #if defined (INET) 27686aee0bfaSMarko Zec case htons(ETHERTYPE_IP): 27696aee0bfaSMarko Zec return (!v4_forwarding); 2770abec4724SSean Bruno #endif 277135e4e998SStephen Hurd } 277235e4e998SStephen Hurd 277335e4e998SStephen Hurd return false; 277435e4e998SStephen Hurd } 2775fe1bcadaSStephen Hurd #else 2776fe1bcadaSStephen Hurd static void 2777fe1bcadaSStephen Hurd iflib_get_ip_forwarding(struct lro_ctrl *lc __unused, bool *v4 __unused, bool *v6 __unused) 2778fe1bcadaSStephen Hurd { 2779fe1bcadaSStephen Hurd } 278035e4e998SStephen Hurd #endif 278135e4e998SStephen Hurd 2782fb1a29b4SHans Petter Selasky static void 2783fb1a29b4SHans Petter Selasky _task_fn_rx_watchdog(void *context) 2784fb1a29b4SHans Petter Selasky { 2785fb1a29b4SHans Petter Selasky iflib_rxq_t rxq = context; 2786fb1a29b4SHans Petter Selasky 2787fb1a29b4SHans Petter Selasky GROUPTASK_ENQUEUE(&rxq->ifr_task); 2788fb1a29b4SHans Petter Selasky } 2789fb1a29b4SHans Petter Selasky 2790fb1a29b4SHans Petter Selasky static uint8_t 279195246abbSSean Bruno iflib_rxeof(iflib_rxq_t rxq, qidx_t budget) 27924c7070dbSScott Long { 27931722eeacSMarius Strobl if_t ifp; 27944c7070dbSScott Long if_ctx_t ctx = rxq->ifr_ctx; 27954c7070dbSScott Long if_shared_ctx_t sctx = ctx->ifc_sctx; 279623ac9029SStephen Hurd if_softc_ctx_t scctx = &ctx->ifc_softc_ctx; 27974c7070dbSScott Long int avail, i; 279895246abbSSean Bruno qidx_t *cidxp; 27994c7070dbSScott Long struct if_rxd_info ri; 28004c7070dbSScott Long int err, budget_left, rx_bytes, rx_pkts; 28014c7070dbSScott Long iflib_fl_t fl; 28024c7070dbSScott Long int lro_enabled; 2803f6cb0deaSMatt Macy bool v4_forwarding, v6_forwarding, lro_possible; 2804fb1a29b4SHans Petter Selasky uint8_t retval = 0; 280595246abbSSean Bruno 28064c7070dbSScott Long /* 28074c7070dbSScott Long * XXX early demux data packets so that if_input processing only handles 28084c7070dbSScott Long * acks in interrupt context 28094c7070dbSScott Long */ 281020f63282SStephen Hurd struct mbuf *m, *mh, *mt, *mf; 28114c7070dbSScott Long 28120b8df657SGleb Smirnoff NET_EPOCH_ASSERT(); 28130b8df657SGleb Smirnoff 2814f6cb0deaSMatt Macy lro_possible = v4_forwarding = v6_forwarding = false; 281595246abbSSean Bruno ifp = ctx->ifc_ifp; 28164c7070dbSScott Long mh = mt = NULL; 28174c7070dbSScott Long MPASS(budget > 0); 28184c7070dbSScott Long rx_pkts = rx_bytes = 0; 281923ac9029SStephen Hurd if (sctx->isc_flags & IFLIB_HAS_RXCQ) 28204c7070dbSScott Long cidxp = &rxq->ifr_cq_cidx; 28214c7070dbSScott Long else 28224c7070dbSScott Long cidxp = &rxq->ifr_fl[0].ifl_cidx; 282323ac9029SStephen Hurd if ((avail = iflib_rxd_avail(ctx, rxq, *cidxp, budget)) == 0) { 28244c7070dbSScott Long for (i = 0, fl = &rxq->ifr_fl[0]; i < sctx->isc_nfl; i++, fl++) 2825b256d25cSMark Johnston retval |= iflib_fl_refill_all(ctx, fl); 28264c7070dbSScott Long DBG_COUNTER_INC(rx_unavail); 2827fb1a29b4SHans Petter Selasky return (retval); 28284c7070dbSScott Long } 28294c7070dbSScott Long 28306d49b41eSAndrew Gallatin /* pfil needs the vnet to be set */ 28316d49b41eSAndrew Gallatin CURVNET_SET_QUIET(ifp->if_vnet); 28328b8d9093SMarius Strobl for (budget_left = budget; budget_left > 0 && avail > 0;) { 28334c7070dbSScott Long if (__predict_false(!CTX_ACTIVE(ctx))) { 28344c7070dbSScott Long DBG_COUNTER_INC(rx_ctx_inactive); 28354c7070dbSScott Long break; 28364c7070dbSScott Long } 28374c7070dbSScott Long /* 28384c7070dbSScott Long * Reset client set fields to their default values 28394c7070dbSScott Long */ 284095246abbSSean Bruno rxd_info_zero(&ri); 28414c7070dbSScott Long ri.iri_qsidx = rxq->ifr_id; 28424c7070dbSScott Long ri.iri_cidx = *cidxp; 284395246abbSSean Bruno ri.iri_ifp = ifp; 28444c7070dbSScott Long ri.iri_frags = rxq->ifr_frags; 28454c7070dbSScott Long err = ctx->isc_rxd_pkt_get(ctx->ifc_softc, &ri); 28464c7070dbSScott Long 284795246abbSSean Bruno if (err) 284895246abbSSean Bruno goto err; 28496d49b41eSAndrew Gallatin rx_pkts += 1; 28506d49b41eSAndrew Gallatin rx_bytes += ri.iri_len; 285123ac9029SStephen Hurd if (sctx->isc_flags & IFLIB_HAS_RXCQ) { 285223ac9029SStephen Hurd *cidxp = ri.iri_cidx; 285323ac9029SStephen Hurd /* Update our consumer index */ 285495246abbSSean Bruno /* XXX NB: shurd - check if this is still safe */ 28551722eeacSMarius Strobl while (rxq->ifr_cq_cidx >= scctx->isc_nrxd[0]) 285623ac9029SStephen Hurd rxq->ifr_cq_cidx -= scctx->isc_nrxd[0]; 28574c7070dbSScott Long /* was this only a completion queue message? */ 28584c7070dbSScott Long if (__predict_false(ri.iri_nfrags == 0)) 28594c7070dbSScott Long continue; 28604c7070dbSScott Long } 28614c7070dbSScott Long MPASS(ri.iri_nfrags != 0); 28624c7070dbSScott Long MPASS(ri.iri_len != 0); 28634c7070dbSScott Long 28644c7070dbSScott Long /* will advance the cidx on the corresponding free lists */ 28654c7070dbSScott Long m = iflib_rxd_pkt_get(rxq, &ri); 28668b8d9093SMarius Strobl avail--; 28678b8d9093SMarius Strobl budget_left--; 28684c7070dbSScott Long if (avail == 0 && budget_left) 286923ac9029SStephen Hurd avail = iflib_rxd_avail(ctx, rxq, *cidxp, budget_left); 28704c7070dbSScott Long 28716d49b41eSAndrew Gallatin if (__predict_false(m == NULL)) 28724c7070dbSScott Long continue; 28736d49b41eSAndrew Gallatin 28744c7070dbSScott Long /* imm_pkt: -- cxgb */ 28754c7070dbSScott Long if (mh == NULL) 28764c7070dbSScott Long mh = mt = m; 28774c7070dbSScott Long else { 28784c7070dbSScott Long mt->m_nextpkt = m; 28794c7070dbSScott Long mt = m; 28804c7070dbSScott Long } 28814c7070dbSScott Long } 28826d49b41eSAndrew Gallatin CURVNET_RESTORE(); 28834c7070dbSScott Long /* make sure that we can refill faster than drain */ 28844c7070dbSScott Long for (i = 0, fl = &rxq->ifr_fl[0]; i < sctx->isc_nfl; i++, fl++) 2885b256d25cSMark Johnston retval |= iflib_fl_refill_all(ctx, fl); 28864c7070dbSScott Long 28874c7070dbSScott Long lro_enabled = (if_getcapenable(ifp) & IFCAP_LRO); 2888fe1bcadaSStephen Hurd if (lro_enabled) 2889fe1bcadaSStephen Hurd iflib_get_ip_forwarding(&rxq->ifr_lc, &v4_forwarding, &v6_forwarding); 289020f63282SStephen Hurd mt = mf = NULL; 28914c7070dbSScott Long while (mh != NULL) { 28924c7070dbSScott Long m = mh; 28934c7070dbSScott Long mh = mh->m_nextpkt; 28944c7070dbSScott Long m->m_nextpkt = NULL; 289595246abbSSean Bruno #ifndef __NO_STRICT_ALIGNMENT 289695246abbSSean Bruno if (!IP_ALIGNED(m) && (m = iflib_fixup_rx(m)) == NULL) 289795246abbSSean Bruno continue; 289895246abbSSean Bruno #endif 28994c7070dbSScott Long rx_bytes += m->m_pkthdr.len; 29004c7070dbSScott Long rx_pkts++; 2901aaeb188aSBjoern A. Zeeb #if defined(INET6) || defined(INET) 290235e4e998SStephen Hurd if (lro_enabled) { 290335e4e998SStephen Hurd if (!lro_possible) { 2904fe1bcadaSStephen Hurd lro_possible = iflib_check_lro_possible(m, v4_forwarding, v6_forwarding); 290535e4e998SStephen Hurd if (lro_possible && mf != NULL) { 290635e4e998SStephen Hurd ifp->if_input(ifp, mf); 290735e4e998SStephen Hurd DBG_COUNTER_INC(rx_if_input); 290835e4e998SStephen Hurd mt = mf = NULL; 290935e4e998SStephen Hurd } 291035e4e998SStephen Hurd } 291125ac1dd5SStephen Hurd if ((m->m_pkthdr.csum_flags & (CSUM_L4_CALC|CSUM_L4_VALID)) == 291225ac1dd5SStephen Hurd (CSUM_L4_CALC|CSUM_L4_VALID)) { 291335e4e998SStephen Hurd if (lro_possible && tcp_lro_rx(&rxq->ifr_lc, m, 0) == 0) 29144c7070dbSScott Long continue; 291520f63282SStephen Hurd } 291625ac1dd5SStephen Hurd } 2917aaeb188aSBjoern A. Zeeb #endif 291835e4e998SStephen Hurd if (lro_possible) { 291935e4e998SStephen Hurd ifp->if_input(ifp, m); 292035e4e998SStephen Hurd DBG_COUNTER_INC(rx_if_input); 292135e4e998SStephen Hurd continue; 292235e4e998SStephen Hurd } 292335e4e998SStephen Hurd 292435e4e998SStephen Hurd if (mf == NULL) 292535e4e998SStephen Hurd mf = m; 292620f63282SStephen Hurd if (mt != NULL) 292720f63282SStephen Hurd mt->m_nextpkt = m; 292820f63282SStephen Hurd mt = m; 292920f63282SStephen Hurd } 293020f63282SStephen Hurd if (mf != NULL) { 293120f63282SStephen Hurd ifp->if_input(ifp, mf); 29324c7070dbSScott Long DBG_COUNTER_INC(rx_if_input); 29334c7070dbSScott Long } 293423ac9029SStephen Hurd 29354c7070dbSScott Long if_inc_counter(ifp, IFCOUNTER_IBYTES, rx_bytes); 29364c7070dbSScott Long if_inc_counter(ifp, IFCOUNTER_IPACKETS, rx_pkts); 29374c7070dbSScott Long 29384c7070dbSScott Long /* 29394c7070dbSScott Long * Flush any outstanding LRO work 29404c7070dbSScott Long */ 2941aaeb188aSBjoern A. Zeeb #if defined(INET6) || defined(INET) 294223ac9029SStephen Hurd tcp_lro_flush_all(&rxq->ifr_lc); 2943aaeb188aSBjoern A. Zeeb #endif 2944fb1a29b4SHans Petter Selasky if (avail != 0 || iflib_rxd_avail(ctx, rxq, *cidxp, 1) != 0) 2945fb1a29b4SHans Petter Selasky retval |= IFLIB_RXEOF_MORE; 2946fb1a29b4SHans Petter Selasky return (retval); 294795246abbSSean Bruno err: 29487b610b60SSean Bruno STATE_LOCK(ctx); 2949ab2e3f79SStephen Hurd ctx->ifc_flags |= IFC_DO_RESET; 2950940f62d6SEric Joyner iflib_admin_intr_deferred(ctx); 295146fa0c25SEric Joyner STATE_UNLOCK(ctx); 2952fb1a29b4SHans Petter Selasky return (0); 295395246abbSSean Bruno } 295495246abbSSean Bruno 295595246abbSSean Bruno #define TXD_NOTIFY_COUNT(txq) (((txq)->ift_size / (txq)->ift_update_freq)-1) 295695246abbSSean Bruno static inline qidx_t 295795246abbSSean Bruno txq_max_db_deferred(iflib_txq_t txq, qidx_t in_use) 295895246abbSSean Bruno { 295995246abbSSean Bruno qidx_t notify_count = TXD_NOTIFY_COUNT(txq); 296095246abbSSean Bruno qidx_t minthresh = txq->ift_size / 8; 296195246abbSSean Bruno if (in_use > 4*minthresh) 296295246abbSSean Bruno return (notify_count); 296395246abbSSean Bruno if (in_use > 2*minthresh) 296495246abbSSean Bruno return (notify_count >> 1); 296595246abbSSean Bruno if (in_use > minthresh) 296695246abbSSean Bruno return (notify_count >> 3); 296795246abbSSean Bruno return (0); 296895246abbSSean Bruno } 296995246abbSSean Bruno 297095246abbSSean Bruno static inline qidx_t 297195246abbSSean Bruno txq_max_rs_deferred(iflib_txq_t txq) 297295246abbSSean Bruno { 297395246abbSSean Bruno qidx_t notify_count = TXD_NOTIFY_COUNT(txq); 297495246abbSSean Bruno qidx_t minthresh = txq->ift_size / 8; 297595246abbSSean Bruno if (txq->ift_in_use > 4*minthresh) 297695246abbSSean Bruno return (notify_count); 297795246abbSSean Bruno if (txq->ift_in_use > 2*minthresh) 297895246abbSSean Bruno return (notify_count >> 1); 297995246abbSSean Bruno if (txq->ift_in_use > minthresh) 298095246abbSSean Bruno return (notify_count >> 2); 29812b2fc973SSean Bruno return (2); 29824c7070dbSScott Long } 29834c7070dbSScott Long 29844c7070dbSScott Long #define M_CSUM_FLAGS(m) ((m)->m_pkthdr.csum_flags) 29854c7070dbSScott Long #define M_HAS_VLANTAG(m) (m->m_flags & M_VLANTAG) 298695246abbSSean Bruno 298795246abbSSean Bruno #define TXQ_MAX_DB_DEFERRED(txq, in_use) txq_max_db_deferred((txq), (in_use)) 298895246abbSSean Bruno #define TXQ_MAX_RS_DEFERRED(txq) txq_max_rs_deferred(txq) 298923ac9029SStephen Hurd #define TXQ_MAX_DB_CONSUMED(size) (size >> 4) 29904c7070dbSScott Long 299195246abbSSean Bruno /* forward compatibility for cxgb */ 299295246abbSSean Bruno #define FIRST_QSET(ctx) 0 299395246abbSSean Bruno #define NTXQSETS(ctx) ((ctx)->ifc_softc_ctx.isc_ntxqsets) 299495246abbSSean Bruno #define NRXQSETS(ctx) ((ctx)->ifc_softc_ctx.isc_nrxqsets) 299595246abbSSean Bruno #define QIDX(ctx, m) ((((m)->m_pkthdr.flowid & ctx->ifc_softc_ctx.isc_rss_table_mask) % NTXQSETS(ctx)) + FIRST_QSET(ctx)) 299695246abbSSean Bruno #define DESC_RECLAIMABLE(q) ((int)((q)->ift_processed - (q)->ift_cleaned - (q)->ift_ctx->ifc_softc_ctx.isc_tx_nsegments)) 299795246abbSSean Bruno 299895246abbSSean Bruno /* XXX we should be setting this to something other than zero */ 299995246abbSSean Bruno #define RECLAIM_THRESH(ctx) ((ctx)->ifc_sctx->isc_tx_reclaim_thresh) 30007474544bSMarius Strobl #define MAX_TX_DESC(ctx) max((ctx)->ifc_softc_ctx.isc_tx_tso_segments_max, \ 30017474544bSMarius Strobl (ctx)->ifc_softc_ctx.isc_tx_nsegments) 300295246abbSSean Bruno 300395246abbSSean Bruno static inline bool 300495246abbSSean Bruno iflib_txd_db_check(if_ctx_t ctx, iflib_txq_t txq, int ring, qidx_t in_use) 30054c7070dbSScott Long { 300695246abbSSean Bruno qidx_t dbval, max; 300795246abbSSean Bruno bool rang; 30084c7070dbSScott Long 300995246abbSSean Bruno rang = false; 301095246abbSSean Bruno max = TXQ_MAX_DB_DEFERRED(txq, in_use); 301195246abbSSean Bruno if (ring || txq->ift_db_pending >= max) { 30124c7070dbSScott Long dbval = txq->ift_npending ? txq->ift_npending : txq->ift_pidx; 301395dcf343SMarius Strobl bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map, 301495dcf343SMarius Strobl BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 30154c7070dbSScott Long ctx->isc_txd_flush(ctx->ifc_softc, txq->ift_id, dbval); 30164c7070dbSScott Long txq->ift_db_pending = txq->ift_npending = 0; 301795246abbSSean Bruno rang = true; 30184c7070dbSScott Long } 301995246abbSSean Bruno return (rang); 30204c7070dbSScott Long } 30214c7070dbSScott Long 30224c7070dbSScott Long #ifdef PKT_DEBUG 30234c7070dbSScott Long static void 30244c7070dbSScott Long print_pkt(if_pkt_info_t pi) 30254c7070dbSScott Long { 30264c7070dbSScott Long printf("pi len: %d qsidx: %d nsegs: %d ndescs: %d flags: %x pidx: %d\n", 30274c7070dbSScott Long pi->ipi_len, pi->ipi_qsidx, pi->ipi_nsegs, pi->ipi_ndescs, pi->ipi_flags, pi->ipi_pidx); 30284c7070dbSScott Long printf("pi new_pidx: %d csum_flags: %lx tso_segsz: %d mflags: %x vtag: %d\n", 30294c7070dbSScott Long pi->ipi_new_pidx, pi->ipi_csum_flags, pi->ipi_tso_segsz, pi->ipi_mflags, pi->ipi_vtag); 30304c7070dbSScott Long printf("pi etype: %d ehdrlen: %d ip_hlen: %d ipproto: %d\n", 30314c7070dbSScott Long pi->ipi_etype, pi->ipi_ehdrlen, pi->ipi_ip_hlen, pi->ipi_ipproto); 30324c7070dbSScott Long } 30334c7070dbSScott Long #endif 30344c7070dbSScott Long 30354c7070dbSScott Long #define IS_TSO4(pi) ((pi)->ipi_csum_flags & CSUM_IP_TSO) 3036a06424ddSEric Joyner #define IS_TX_OFFLOAD4(pi) ((pi)->ipi_csum_flags & (CSUM_IP_TCP | CSUM_IP_TSO)) 30374c7070dbSScott Long #define IS_TSO6(pi) ((pi)->ipi_csum_flags & CSUM_IP6_TSO) 3038a06424ddSEric Joyner #define IS_TX_OFFLOAD6(pi) ((pi)->ipi_csum_flags & (CSUM_IP6_TCP | CSUM_IP6_TSO)) 30394c7070dbSScott Long 30404c7070dbSScott Long static int 30414c7070dbSScott Long iflib_parse_header(iflib_txq_t txq, if_pkt_info_t pi, struct mbuf **mp) 30424c7070dbSScott Long { 3043ab2e3f79SStephen Hurd if_shared_ctx_t sctx = txq->ift_ctx->ifc_sctx; 30444c7070dbSScott Long struct ether_vlan_header *eh; 3045c9a49a4fSMarius Strobl struct mbuf *m; 30464c7070dbSScott Long 30478b8d9093SMarius Strobl m = *mp; 3048ab2e3f79SStephen Hurd if ((sctx->isc_flags & IFLIB_NEED_SCRATCH) && 3049ab2e3f79SStephen Hurd M_WRITABLE(m) == 0) { 3050ab2e3f79SStephen Hurd if ((m = m_dup(m, M_NOWAIT)) == NULL) { 3051ab2e3f79SStephen Hurd return (ENOMEM); 3052ab2e3f79SStephen Hurd } else { 3053ab2e3f79SStephen Hurd m_freem(*mp); 305464e6fc13SStephen Hurd DBG_COUNTER_INC(tx_frees); 30558b8d9093SMarius Strobl *mp = m; 3056ab2e3f79SStephen Hurd } 3057ab2e3f79SStephen Hurd } 30581248952aSSean Bruno 30594c7070dbSScott Long /* 30604c7070dbSScott Long * Determine where frame payload starts. 30614c7070dbSScott Long * Jump over vlan headers if already present, 30624c7070dbSScott Long * helpful for QinQ too. 30634c7070dbSScott Long */ 30644c7070dbSScott Long if (__predict_false(m->m_len < sizeof(*eh))) { 30654c7070dbSScott Long txq->ift_pullups++; 30664c7070dbSScott Long if (__predict_false((m = m_pullup(m, sizeof(*eh))) == NULL)) 30674c7070dbSScott Long return (ENOMEM); 30684c7070dbSScott Long } 30694c7070dbSScott Long eh = mtod(m, struct ether_vlan_header *); 30704c7070dbSScott Long if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) { 30714c7070dbSScott Long pi->ipi_etype = ntohs(eh->evl_proto); 30724c7070dbSScott Long pi->ipi_ehdrlen = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN; 30734c7070dbSScott Long } else { 30744c7070dbSScott Long pi->ipi_etype = ntohs(eh->evl_encap_proto); 30754c7070dbSScott Long pi->ipi_ehdrlen = ETHER_HDR_LEN; 30764c7070dbSScott Long } 30774c7070dbSScott Long 30784c7070dbSScott Long switch (pi->ipi_etype) { 30794c7070dbSScott Long #ifdef INET 30804c7070dbSScott Long case ETHERTYPE_IP: 30814c7070dbSScott Long { 3082c9a49a4fSMarius Strobl struct mbuf *n; 30834c7070dbSScott Long struct ip *ip = NULL; 30844c7070dbSScott Long struct tcphdr *th = NULL; 30854c7070dbSScott Long int minthlen; 30864c7070dbSScott Long 30874c7070dbSScott Long minthlen = min(m->m_pkthdr.len, pi->ipi_ehdrlen + sizeof(*ip) + sizeof(*th)); 30884c7070dbSScott Long if (__predict_false(m->m_len < minthlen)) { 30894c7070dbSScott Long /* 30904c7070dbSScott Long * if this code bloat is causing too much of a hit 30914c7070dbSScott Long * move it to a separate function and mark it noinline 30924c7070dbSScott Long */ 30934c7070dbSScott Long if (m->m_len == pi->ipi_ehdrlen) { 30944c7070dbSScott Long n = m->m_next; 30954c7070dbSScott Long MPASS(n); 30964c7070dbSScott Long if (n->m_len >= sizeof(*ip)) { 30974c7070dbSScott Long ip = (struct ip *)n->m_data; 30984c7070dbSScott Long if (n->m_len >= (ip->ip_hl << 2) + sizeof(*th)) 30994c7070dbSScott Long th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2)); 31004c7070dbSScott Long } else { 31014c7070dbSScott Long txq->ift_pullups++; 31024c7070dbSScott Long if (__predict_false((m = m_pullup(m, minthlen)) == NULL)) 31034c7070dbSScott Long return (ENOMEM); 31044c7070dbSScott Long ip = (struct ip *)(m->m_data + pi->ipi_ehdrlen); 31054c7070dbSScott Long } 31064c7070dbSScott Long } else { 31074c7070dbSScott Long txq->ift_pullups++; 31084c7070dbSScott Long if (__predict_false((m = m_pullup(m, minthlen)) == NULL)) 31094c7070dbSScott Long return (ENOMEM); 31104c7070dbSScott Long ip = (struct ip *)(m->m_data + pi->ipi_ehdrlen); 31114c7070dbSScott Long if (m->m_len >= (ip->ip_hl << 2) + sizeof(*th)) 31124c7070dbSScott Long th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2)); 31134c7070dbSScott Long } 31144c7070dbSScott Long } else { 31154c7070dbSScott Long ip = (struct ip *)(m->m_data + pi->ipi_ehdrlen); 31164c7070dbSScott Long if (m->m_len >= (ip->ip_hl << 2) + sizeof(*th)) 31174c7070dbSScott Long th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2)); 31184c7070dbSScott Long } 31194c7070dbSScott Long pi->ipi_ip_hlen = ip->ip_hl << 2; 31204c7070dbSScott Long pi->ipi_ipproto = ip->ip_p; 31214c7070dbSScott Long pi->ipi_flags |= IPI_TX_IPV4; 31224c7070dbSScott Long 3123a06424ddSEric Joyner /* TCP checksum offload may require TCP header length */ 3124a06424ddSEric Joyner if (IS_TX_OFFLOAD4(pi)) { 3125a06424ddSEric Joyner if (__predict_true(pi->ipi_ipproto == IPPROTO_TCP)) { 31264c7070dbSScott Long if (__predict_false(th == NULL)) { 31274c7070dbSScott Long txq->ift_pullups++; 31284c7070dbSScott Long if (__predict_false((m = m_pullup(m, (ip->ip_hl << 2) + sizeof(*th))) == NULL)) 31294c7070dbSScott Long return (ENOMEM); 31304c7070dbSScott Long th = (struct tcphdr *)((caddr_t)ip + pi->ipi_ip_hlen); 31314c7070dbSScott Long } 31324c7070dbSScott Long pi->ipi_tcp_hflags = th->th_flags; 31334c7070dbSScott Long pi->ipi_tcp_hlen = th->th_off << 2; 31344c7070dbSScott Long pi->ipi_tcp_seq = th->th_seq; 31354c7070dbSScott Long } 3136a06424ddSEric Joyner if (IS_TSO4(pi)) { 31374c7070dbSScott Long if (__predict_false(ip->ip_p != IPPROTO_TCP)) 31384c7070dbSScott Long return (ENXIO); 31398d4ceb9cSStephen Hurd /* 31408d4ceb9cSStephen Hurd * TSO always requires hardware checksum offload. 31418d4ceb9cSStephen Hurd */ 31428d4ceb9cSStephen Hurd pi->ipi_csum_flags |= (CSUM_IP_TCP | CSUM_IP); 31434c7070dbSScott Long th->th_sum = in_pseudo(ip->ip_src.s_addr, 31444c7070dbSScott Long ip->ip_dst.s_addr, htons(IPPROTO_TCP)); 31454c7070dbSScott Long pi->ipi_tso_segsz = m->m_pkthdr.tso_segsz; 31461248952aSSean Bruno if (sctx->isc_flags & IFLIB_TSO_INIT_IP) { 31471248952aSSean Bruno ip->ip_sum = 0; 31481248952aSSean Bruno ip->ip_len = htons(pi->ipi_ip_hlen + pi->ipi_tcp_hlen + pi->ipi_tso_segsz); 31491248952aSSean Bruno } 31504c7070dbSScott Long } 3151a06424ddSEric Joyner } 31528d4ceb9cSStephen Hurd if ((sctx->isc_flags & IFLIB_NEED_ZERO_CSUM) && (pi->ipi_csum_flags & CSUM_IP)) 31538d4ceb9cSStephen Hurd ip->ip_sum = 0; 31548d4ceb9cSStephen Hurd 31554c7070dbSScott Long break; 31564c7070dbSScott Long } 31574c7070dbSScott Long #endif 31584c7070dbSScott Long #ifdef INET6 31594c7070dbSScott Long case ETHERTYPE_IPV6: 31604c7070dbSScott Long { 31614c7070dbSScott Long struct ip6_hdr *ip6 = (struct ip6_hdr *)(m->m_data + pi->ipi_ehdrlen); 31624c7070dbSScott Long struct tcphdr *th; 31634c7070dbSScott Long pi->ipi_ip_hlen = sizeof(struct ip6_hdr); 31644c7070dbSScott Long 31654c7070dbSScott Long if (__predict_false(m->m_len < pi->ipi_ehdrlen + sizeof(struct ip6_hdr))) { 316664e6fc13SStephen Hurd txq->ift_pullups++; 31674c7070dbSScott Long if (__predict_false((m = m_pullup(m, pi->ipi_ehdrlen + sizeof(struct ip6_hdr))) == NULL)) 31684c7070dbSScott Long return (ENOMEM); 31694c7070dbSScott Long } 31704c7070dbSScott Long th = (struct tcphdr *)((caddr_t)ip6 + pi->ipi_ip_hlen); 31714c7070dbSScott Long 31724c7070dbSScott Long /* XXX-BZ this will go badly in case of ext hdrs. */ 31734c7070dbSScott Long pi->ipi_ipproto = ip6->ip6_nxt; 31744c7070dbSScott Long pi->ipi_flags |= IPI_TX_IPV6; 31754c7070dbSScott Long 3176a06424ddSEric Joyner /* TCP checksum offload may require TCP header length */ 3177a06424ddSEric Joyner if (IS_TX_OFFLOAD6(pi)) { 31784c7070dbSScott Long if (pi->ipi_ipproto == IPPROTO_TCP) { 31794c7070dbSScott Long if (__predict_false(m->m_len < pi->ipi_ehdrlen + sizeof(struct ip6_hdr) + sizeof(struct tcphdr))) { 3180a06424ddSEric Joyner txq->ift_pullups++; 31814c7070dbSScott Long if (__predict_false((m = m_pullup(m, pi->ipi_ehdrlen + sizeof(struct ip6_hdr) + sizeof(struct tcphdr))) == NULL)) 31824c7070dbSScott Long return (ENOMEM); 31834c7070dbSScott Long } 31844c7070dbSScott Long pi->ipi_tcp_hflags = th->th_flags; 31854c7070dbSScott Long pi->ipi_tcp_hlen = th->th_off << 2; 3186a06424ddSEric Joyner pi->ipi_tcp_seq = th->th_seq; 31874c7070dbSScott Long } 3188a06424ddSEric Joyner if (IS_TSO6(pi)) { 31894c7070dbSScott Long if (__predict_false(ip6->ip6_nxt != IPPROTO_TCP)) 31904c7070dbSScott Long return (ENXIO); 31914c7070dbSScott Long /* 31928d4ceb9cSStephen Hurd * TSO always requires hardware checksum offload. 31934c7070dbSScott Long */ 3194a06424ddSEric Joyner pi->ipi_csum_flags |= CSUM_IP6_TCP; 31954c7070dbSScott Long th->th_sum = in6_cksum_pseudo(ip6, 0, IPPROTO_TCP, 0); 31964c7070dbSScott Long pi->ipi_tso_segsz = m->m_pkthdr.tso_segsz; 31974c7070dbSScott Long } 3198a06424ddSEric Joyner } 31994c7070dbSScott Long break; 32004c7070dbSScott Long } 32014c7070dbSScott Long #endif 32024c7070dbSScott Long default: 32034c7070dbSScott Long pi->ipi_csum_flags &= ~CSUM_OFFLOAD; 32044c7070dbSScott Long pi->ipi_ip_hlen = 0; 32054c7070dbSScott Long break; 32064c7070dbSScott Long } 32074c7070dbSScott Long *mp = m; 32081248952aSSean Bruno 32094c7070dbSScott Long return (0); 32104c7070dbSScott Long } 32114c7070dbSScott Long 32124c7070dbSScott Long /* 32134c7070dbSScott Long * If dodgy hardware rejects the scatter gather chain we've handed it 321423ac9029SStephen Hurd * we'll need to remove the mbuf chain from ifsg_m[] before we can add the 321523ac9029SStephen Hurd * m_defrag'd mbufs 32164c7070dbSScott Long */ 32174c7070dbSScott Long static __noinline struct mbuf * 321823ac9029SStephen Hurd iflib_remove_mbuf(iflib_txq_t txq) 32194c7070dbSScott Long { 3220fbec776dSAndrew Gallatin int ntxd, pidx; 3221fbec776dSAndrew Gallatin struct mbuf *m, **ifsd_m; 32224c7070dbSScott Long 32234c7070dbSScott Long ifsd_m = txq->ift_sds.ifsd_m; 322423ac9029SStephen Hurd ntxd = txq->ift_size; 3225fbec776dSAndrew Gallatin pidx = txq->ift_pidx & (ntxd - 1); 3226fbec776dSAndrew Gallatin ifsd_m = txq->ift_sds.ifsd_m; 3227fbec776dSAndrew Gallatin m = ifsd_m[pidx]; 32284c7070dbSScott Long ifsd_m[pidx] = NULL; 3229bfce461eSMarius Strobl bus_dmamap_unload(txq->ift_buf_tag, txq->ift_sds.ifsd_map[pidx]); 32308a04b53dSKonstantin Belousov if (txq->ift_sds.ifsd_tso_map != NULL) 3231bfce461eSMarius Strobl bus_dmamap_unload(txq->ift_tso_buf_tag, 32328a04b53dSKonstantin Belousov txq->ift_sds.ifsd_tso_map[pidx]); 32334c7070dbSScott Long #if MEMORY_LOGGING 32344c7070dbSScott Long txq->ift_dequeued++; 32354c7070dbSScott Long #endif 3236fbec776dSAndrew Gallatin return (m); 32374c7070dbSScott Long } 32384c7070dbSScott Long 323995246abbSSean Bruno static inline caddr_t 324095246abbSSean Bruno calc_next_txd(iflib_txq_t txq, int cidx, uint8_t qid) 324195246abbSSean Bruno { 324295246abbSSean Bruno qidx_t size; 324395246abbSSean Bruno int ntxd; 324495246abbSSean Bruno caddr_t start, end, cur, next; 324595246abbSSean Bruno 324695246abbSSean Bruno ntxd = txq->ift_size; 324795246abbSSean Bruno size = txq->ift_txd_size[qid]; 324895246abbSSean Bruno start = txq->ift_ifdi[qid].idi_vaddr; 324995246abbSSean Bruno 325095246abbSSean Bruno if (__predict_false(size == 0)) 325195246abbSSean Bruno return (start); 325295246abbSSean Bruno cur = start + size*cidx; 325395246abbSSean Bruno end = start + size*ntxd; 325495246abbSSean Bruno next = CACHE_PTR_NEXT(cur); 325595246abbSSean Bruno return (next < end ? next : start); 325695246abbSSean Bruno } 325795246abbSSean Bruno 3258d14c853bSStephen Hurd /* 3259d14c853bSStephen Hurd * Pad an mbuf to ensure a minimum ethernet frame size. 3260d14c853bSStephen Hurd * min_frame_size is the frame size (less CRC) to pad the mbuf to 3261d14c853bSStephen Hurd */ 3262d14c853bSStephen Hurd static __noinline int 3263a15fbbb8SStephen Hurd iflib_ether_pad(device_t dev, struct mbuf **m_head, uint16_t min_frame_size) 3264d14c853bSStephen Hurd { 3265d14c853bSStephen Hurd /* 3266d14c853bSStephen Hurd * 18 is enough bytes to pad an ARP packet to 46 bytes, and 3267d14c853bSStephen Hurd * and ARP message is the smallest common payload I can think of 3268d14c853bSStephen Hurd */ 3269d14c853bSStephen Hurd static char pad[18]; /* just zeros */ 3270d14c853bSStephen Hurd int n; 3271a15fbbb8SStephen Hurd struct mbuf *new_head; 3272d14c853bSStephen Hurd 3273a15fbbb8SStephen Hurd if (!M_WRITABLE(*m_head)) { 3274a15fbbb8SStephen Hurd new_head = m_dup(*m_head, M_NOWAIT); 3275a15fbbb8SStephen Hurd if (new_head == NULL) { 327604993890SStephen Hurd m_freem(*m_head); 3277a15fbbb8SStephen Hurd device_printf(dev, "cannot pad short frame, m_dup() failed"); 327806c47d48SStephen Hurd DBG_COUNTER_INC(encap_pad_mbuf_fail); 327964e6fc13SStephen Hurd DBG_COUNTER_INC(tx_frees); 3280a15fbbb8SStephen Hurd return ENOMEM; 3281a15fbbb8SStephen Hurd } 3282a15fbbb8SStephen Hurd m_freem(*m_head); 3283a15fbbb8SStephen Hurd *m_head = new_head; 3284a15fbbb8SStephen Hurd } 3285a15fbbb8SStephen Hurd 3286a15fbbb8SStephen Hurd for (n = min_frame_size - (*m_head)->m_pkthdr.len; 3287d14c853bSStephen Hurd n > 0; n -= sizeof(pad)) 3288a15fbbb8SStephen Hurd if (!m_append(*m_head, min(n, sizeof(pad)), pad)) 3289d14c853bSStephen Hurd break; 3290d14c853bSStephen Hurd 3291d14c853bSStephen Hurd if (n > 0) { 3292a15fbbb8SStephen Hurd m_freem(*m_head); 3293d14c853bSStephen Hurd device_printf(dev, "cannot pad short frame\n"); 3294d14c853bSStephen Hurd DBG_COUNTER_INC(encap_pad_mbuf_fail); 329564e6fc13SStephen Hurd DBG_COUNTER_INC(tx_frees); 3296d14c853bSStephen Hurd return (ENOBUFS); 3297d14c853bSStephen Hurd } 3298d14c853bSStephen Hurd 3299d14c853bSStephen Hurd return 0; 3300d14c853bSStephen Hurd } 3301d14c853bSStephen Hurd 33024c7070dbSScott Long static int 33034c7070dbSScott Long iflib_encap(iflib_txq_t txq, struct mbuf **m_headp) 33044c7070dbSScott Long { 33054c7070dbSScott Long if_ctx_t ctx; 33064c7070dbSScott Long if_shared_ctx_t sctx; 33074c7070dbSScott Long if_softc_ctx_t scctx; 3308bfce461eSMarius Strobl bus_dma_tag_t buf_tag; 33094c7070dbSScott Long bus_dma_segment_t *segs; 3310fbec776dSAndrew Gallatin struct mbuf *m_head, **ifsd_m; 331195246abbSSean Bruno void *next_txd; 33124c7070dbSScott Long bus_dmamap_t map; 33134c7070dbSScott Long struct if_pkt_info pi; 33144c7070dbSScott Long int remap = 0; 33154c7070dbSScott Long int err, nsegs, ndesc, max_segs, pidx, cidx, next, ntxd; 33164c7070dbSScott Long 33174c7070dbSScott Long ctx = txq->ift_ctx; 33184c7070dbSScott Long sctx = ctx->ifc_sctx; 33194c7070dbSScott Long scctx = &ctx->ifc_softc_ctx; 33204c7070dbSScott Long segs = txq->ift_segs; 332123ac9029SStephen Hurd ntxd = txq->ift_size; 33224c7070dbSScott Long m_head = *m_headp; 33234c7070dbSScott Long map = NULL; 33244c7070dbSScott Long 33254c7070dbSScott Long /* 33264c7070dbSScott Long * If we're doing TSO the next descriptor to clean may be quite far ahead 33274c7070dbSScott Long */ 33284c7070dbSScott Long cidx = txq->ift_cidx; 33294c7070dbSScott Long pidx = txq->ift_pidx; 333095246abbSSean Bruno if (ctx->ifc_flags & IFC_PREFETCH) { 33314c7070dbSScott Long next = (cidx + CACHE_PTR_INCREMENT) & (ntxd-1); 333295246abbSSean Bruno if (!(ctx->ifc_flags & IFLIB_HAS_TXCQ)) { 333395246abbSSean Bruno next_txd = calc_next_txd(txq, cidx, 0); 333495246abbSSean Bruno prefetch(next_txd); 333595246abbSSean Bruno } 33364c7070dbSScott Long 33374c7070dbSScott Long /* prefetch the next cache line of mbuf pointers and flags */ 33384c7070dbSScott Long prefetch(&txq->ift_sds.ifsd_m[next]); 33394c7070dbSScott Long prefetch(&txq->ift_sds.ifsd_map[next]); 33404c7070dbSScott Long next = (cidx + CACHE_LINE_SIZE) & (ntxd-1); 33414c7070dbSScott Long } 334295246abbSSean Bruno map = txq->ift_sds.ifsd_map[pidx]; 3343fbec776dSAndrew Gallatin ifsd_m = txq->ift_sds.ifsd_m; 33444c7070dbSScott Long 33454c7070dbSScott Long if (m_head->m_pkthdr.csum_flags & CSUM_TSO) { 3346bfce461eSMarius Strobl buf_tag = txq->ift_tso_buf_tag; 33474c7070dbSScott Long max_segs = scctx->isc_tx_tso_segments_max; 33488a04b53dSKonstantin Belousov map = txq->ift_sds.ifsd_tso_map[pidx]; 3349bfce461eSMarius Strobl MPASS(buf_tag != NULL); 33507f87c040SMarius Strobl MPASS(max_segs > 0); 33514c7070dbSScott Long } else { 3352bfce461eSMarius Strobl buf_tag = txq->ift_buf_tag; 33534c7070dbSScott Long max_segs = scctx->isc_tx_nsegments; 33548a04b53dSKonstantin Belousov map = txq->ift_sds.ifsd_map[pidx]; 33554c7070dbSScott Long } 3356d14c853bSStephen Hurd if ((sctx->isc_flags & IFLIB_NEED_ETHER_PAD) && 3357d14c853bSStephen Hurd __predict_false(m_head->m_pkthdr.len < scctx->isc_min_frame_size)) { 3358a15fbbb8SStephen Hurd err = iflib_ether_pad(ctx->ifc_dev, m_headp, scctx->isc_min_frame_size); 335964e6fc13SStephen Hurd if (err) { 336064e6fc13SStephen Hurd DBG_COUNTER_INC(encap_txd_encap_fail); 3361d14c853bSStephen Hurd return err; 3362d14c853bSStephen Hurd } 336364e6fc13SStephen Hurd } 3364a15fbbb8SStephen Hurd m_head = *m_headp; 336595246abbSSean Bruno 336695246abbSSean Bruno pkt_info_zero(&pi); 3367ab2e3f79SStephen Hurd pi.ipi_mflags = (m_head->m_flags & (M_VLANTAG|M_BCAST|M_MCAST)); 3368ab2e3f79SStephen Hurd pi.ipi_pidx = pidx; 3369ab2e3f79SStephen Hurd pi.ipi_qsidx = txq->ift_id; 33703429c02fSStephen Hurd pi.ipi_len = m_head->m_pkthdr.len; 33713429c02fSStephen Hurd pi.ipi_csum_flags = m_head->m_pkthdr.csum_flags; 33721722eeacSMarius Strobl pi.ipi_vtag = M_HAS_VLANTAG(m_head) ? m_head->m_pkthdr.ether_vtag : 0; 33734c7070dbSScott Long 33744c7070dbSScott Long /* deliberate bitwise OR to make one condition */ 33754c7070dbSScott Long if (__predict_true((pi.ipi_csum_flags | pi.ipi_vtag))) { 337664e6fc13SStephen Hurd if (__predict_false((err = iflib_parse_header(txq, &pi, m_headp)) != 0)) { 337764e6fc13SStephen Hurd DBG_COUNTER_INC(encap_txd_encap_fail); 33784c7070dbSScott Long return (err); 337964e6fc13SStephen Hurd } 33804c7070dbSScott Long m_head = *m_headp; 33814c7070dbSScott Long } 33824c7070dbSScott Long 33834c7070dbSScott Long retry: 3384bfce461eSMarius Strobl err = bus_dmamap_load_mbuf_sg(buf_tag, map, m_head, segs, &nsegs, 3385fbec776dSAndrew Gallatin BUS_DMA_NOWAIT); 33864c7070dbSScott Long defrag: 33874c7070dbSScott Long if (__predict_false(err)) { 33884c7070dbSScott Long switch (err) { 33894c7070dbSScott Long case EFBIG: 33904c7070dbSScott Long /* try collapse once and defrag once */ 3391f7594707SAndrew Gallatin if (remap == 0) { 33924c7070dbSScott Long m_head = m_collapse(*m_headp, M_NOWAIT, max_segs); 3393f7594707SAndrew Gallatin /* try defrag if collapsing fails */ 3394f7594707SAndrew Gallatin if (m_head == NULL) 3395f7594707SAndrew Gallatin remap++; 3396f7594707SAndrew Gallatin } 339764e6fc13SStephen Hurd if (remap == 1) { 339864e6fc13SStephen Hurd txq->ift_mbuf_defrag++; 33994c7070dbSScott Long m_head = m_defrag(*m_headp, M_NOWAIT); 340064e6fc13SStephen Hurd } 34013e8d1baeSEric Joyner /* 34023e8d1baeSEric Joyner * remap should never be >1 unless bus_dmamap_load_mbuf_sg 34033e8d1baeSEric Joyner * failed to map an mbuf that was run through m_defrag 34043e8d1baeSEric Joyner */ 34053e8d1baeSEric Joyner MPASS(remap <= 1); 34063e8d1baeSEric Joyner if (__predict_false(m_head == NULL || remap > 1)) 34074c7070dbSScott Long goto defrag_failed; 34083e8d1baeSEric Joyner remap++; 34094c7070dbSScott Long *m_headp = m_head; 34104c7070dbSScott Long goto retry; 34114c7070dbSScott Long break; 34124c7070dbSScott Long case ENOMEM: 34134c7070dbSScott Long txq->ift_no_tx_dma_setup++; 34144c7070dbSScott Long break; 34154c7070dbSScott Long default: 34164c7070dbSScott Long txq->ift_no_tx_dma_setup++; 34174c7070dbSScott Long m_freem(*m_headp); 34184c7070dbSScott Long DBG_COUNTER_INC(tx_frees); 34194c7070dbSScott Long *m_headp = NULL; 34204c7070dbSScott Long break; 34214c7070dbSScott Long } 34224c7070dbSScott Long txq->ift_map_failed++; 34234c7070dbSScott Long DBG_COUNTER_INC(encap_load_mbuf_fail); 342464e6fc13SStephen Hurd DBG_COUNTER_INC(encap_txd_encap_fail); 34254c7070dbSScott Long return (err); 34264c7070dbSScott Long } 3427fbec776dSAndrew Gallatin ifsd_m[pidx] = m_head; 34284c7070dbSScott Long /* 34294c7070dbSScott Long * XXX assumes a 1 to 1 relationship between segments and 34304c7070dbSScott Long * descriptors - this does not hold true on all drivers, e.g. 34314c7070dbSScott Long * cxgb 34324c7070dbSScott Long */ 34334c7070dbSScott Long if (__predict_false(nsegs + 2 > TXQ_AVAIL(txq))) { 34344c7070dbSScott Long txq->ift_no_desc_avail++; 3435bfce461eSMarius Strobl bus_dmamap_unload(buf_tag, map); 34364c7070dbSScott Long DBG_COUNTER_INC(encap_txq_avail_fail); 343764e6fc13SStephen Hurd DBG_COUNTER_INC(encap_txd_encap_fail); 343823ac9029SStephen Hurd if ((txq->ift_task.gt_task.ta_flags & TASK_ENQUEUED) == 0) 34394c7070dbSScott Long GROUPTASK_ENQUEUE(&txq->ift_task); 34404c7070dbSScott Long return (ENOBUFS); 34414c7070dbSScott Long } 344295246abbSSean Bruno /* 344395246abbSSean Bruno * On Intel cards we can greatly reduce the number of TX interrupts 344495246abbSSean Bruno * we see by only setting report status on every Nth descriptor. 344595246abbSSean Bruno * However, this also means that the driver will need to keep track 344695246abbSSean Bruno * of the descriptors that RS was set on to check them for the DD bit. 344795246abbSSean Bruno */ 344895246abbSSean Bruno txq->ift_rs_pending += nsegs + 1; 344995246abbSSean Bruno if (txq->ift_rs_pending > TXQ_MAX_RS_DEFERRED(txq) || 34501f7ce05dSAndrew Gallatin iflib_no_tx_batch || (TXQ_AVAIL(txq) - nsegs) <= MAX_TX_DESC(ctx) + 2) { 345195246abbSSean Bruno pi.ipi_flags |= IPI_TX_INTR; 345295246abbSSean Bruno txq->ift_rs_pending = 0; 345395246abbSSean Bruno } 345495246abbSSean Bruno 34554c7070dbSScott Long pi.ipi_segs = segs; 34564c7070dbSScott Long pi.ipi_nsegs = nsegs; 34574c7070dbSScott Long 345823ac9029SStephen Hurd MPASS(pidx >= 0 && pidx < txq->ift_size); 34594c7070dbSScott Long #ifdef PKT_DEBUG 34604c7070dbSScott Long print_pkt(&pi); 34614c7070dbSScott Long #endif 34624c7070dbSScott Long if ((err = ctx->isc_txd_encap(ctx->ifc_softc, &pi)) == 0) { 346395dcf343SMarius Strobl bus_dmamap_sync(buf_tag, map, BUS_DMASYNC_PREWRITE); 34644c7070dbSScott Long DBG_COUNTER_INC(tx_encap); 346595246abbSSean Bruno MPASS(pi.ipi_new_pidx < txq->ift_size); 34664c7070dbSScott Long 34674c7070dbSScott Long ndesc = pi.ipi_new_pidx - pi.ipi_pidx; 34684c7070dbSScott Long if (pi.ipi_new_pidx < pi.ipi_pidx) { 346923ac9029SStephen Hurd ndesc += txq->ift_size; 34704c7070dbSScott Long txq->ift_gen = 1; 34714c7070dbSScott Long } 34721248952aSSean Bruno /* 34731248952aSSean Bruno * drivers can need as many as 34741248952aSSean Bruno * two sentinels 34751248952aSSean Bruno */ 34761248952aSSean Bruno MPASS(ndesc <= pi.ipi_nsegs + 2); 34774c7070dbSScott Long MPASS(pi.ipi_new_pidx != pidx); 34784c7070dbSScott Long MPASS(ndesc > 0); 34794c7070dbSScott Long txq->ift_in_use += ndesc; 348095246abbSSean Bruno 34814c7070dbSScott Long /* 34824c7070dbSScott Long * We update the last software descriptor again here because there may 34834c7070dbSScott Long * be a sentinel and/or there may be more mbufs than segments 34844c7070dbSScott Long */ 34854c7070dbSScott Long txq->ift_pidx = pi.ipi_new_pidx; 34864c7070dbSScott Long txq->ift_npending += pi.ipi_ndescs; 3487f7594707SAndrew Gallatin } else { 348823ac9029SStephen Hurd *m_headp = m_head = iflib_remove_mbuf(txq); 3489f7594707SAndrew Gallatin if (err == EFBIG) { 34904c7070dbSScott Long txq->ift_txd_encap_efbig++; 3491f7594707SAndrew Gallatin if (remap < 2) { 3492f7594707SAndrew Gallatin remap = 1; 34934c7070dbSScott Long goto defrag; 3494f7594707SAndrew Gallatin } 3495f7594707SAndrew Gallatin } 3496f7594707SAndrew Gallatin goto defrag_failed; 3497f7594707SAndrew Gallatin } 349864e6fc13SStephen Hurd /* 349964e6fc13SStephen Hurd * err can't possibly be non-zero here, so we don't neet to test it 350064e6fc13SStephen Hurd * to see if we need to DBG_COUNTER_INC(encap_txd_encap_fail). 350164e6fc13SStephen Hurd */ 35024c7070dbSScott Long return (err); 35034c7070dbSScott Long 35044c7070dbSScott Long defrag_failed: 35054c7070dbSScott Long txq->ift_mbuf_defrag_failed++; 35064c7070dbSScott Long txq->ift_map_failed++; 35074c7070dbSScott Long m_freem(*m_headp); 35084c7070dbSScott Long DBG_COUNTER_INC(tx_frees); 35094c7070dbSScott Long *m_headp = NULL; 351064e6fc13SStephen Hurd DBG_COUNTER_INC(encap_txd_encap_fail); 35114c7070dbSScott Long return (ENOMEM); 35124c7070dbSScott Long } 35134c7070dbSScott Long 35144c7070dbSScott Long static void 35154c7070dbSScott Long iflib_tx_desc_free(iflib_txq_t txq, int n) 35164c7070dbSScott Long { 35174c7070dbSScott Long uint32_t qsize, cidx, mask, gen; 35184c7070dbSScott Long struct mbuf *m, **ifsd_m; 351995246abbSSean Bruno bool do_prefetch; 35204c7070dbSScott Long 35214c7070dbSScott Long cidx = txq->ift_cidx; 35224c7070dbSScott Long gen = txq->ift_gen; 352323ac9029SStephen Hurd qsize = txq->ift_size; 35244c7070dbSScott Long mask = qsize-1; 35254c7070dbSScott Long ifsd_m = txq->ift_sds.ifsd_m; 352695246abbSSean Bruno do_prefetch = (txq->ift_ctx->ifc_flags & IFC_PREFETCH); 35274c7070dbSScott Long 352894618825SMark Johnston while (n-- > 0) { 352995246abbSSean Bruno if (do_prefetch) { 35304c7070dbSScott Long prefetch(ifsd_m[(cidx + 3) & mask]); 35314c7070dbSScott Long prefetch(ifsd_m[(cidx + 4) & mask]); 353295246abbSSean Bruno } 35334c7070dbSScott Long if ((m = ifsd_m[cidx]) != NULL) { 3534fbec776dSAndrew Gallatin prefetch(&ifsd_m[(cidx + CACHE_PTR_INCREMENT) & mask]); 35358a04b53dSKonstantin Belousov if (m->m_pkthdr.csum_flags & CSUM_TSO) { 3536bfce461eSMarius Strobl bus_dmamap_sync(txq->ift_tso_buf_tag, 35378a04b53dSKonstantin Belousov txq->ift_sds.ifsd_tso_map[cidx], 35388a04b53dSKonstantin Belousov BUS_DMASYNC_POSTWRITE); 3539bfce461eSMarius Strobl bus_dmamap_unload(txq->ift_tso_buf_tag, 35408a04b53dSKonstantin Belousov txq->ift_sds.ifsd_tso_map[cidx]); 35418a04b53dSKonstantin Belousov } else { 3542bfce461eSMarius Strobl bus_dmamap_sync(txq->ift_buf_tag, 35438a04b53dSKonstantin Belousov txq->ift_sds.ifsd_map[cidx], 35448a04b53dSKonstantin Belousov BUS_DMASYNC_POSTWRITE); 3545bfce461eSMarius Strobl bus_dmamap_unload(txq->ift_buf_tag, 35468a04b53dSKonstantin Belousov txq->ift_sds.ifsd_map[cidx]); 35478a04b53dSKonstantin Belousov } 35484c7070dbSScott Long /* XXX we don't support any drivers that batch packets yet */ 35494c7070dbSScott Long MPASS(m->m_nextpkt == NULL); 35505c5ca36cSSean Bruno m_freem(m); 35514c7070dbSScott Long ifsd_m[cidx] = NULL; 35524c7070dbSScott Long #if MEMORY_LOGGING 35534c7070dbSScott Long txq->ift_dequeued++; 35544c7070dbSScott Long #endif 35554c7070dbSScott Long DBG_COUNTER_INC(tx_frees); 35564c7070dbSScott Long } 35574c7070dbSScott Long if (__predict_false(++cidx == qsize)) { 35584c7070dbSScott Long cidx = 0; 35594c7070dbSScott Long gen = 0; 35604c7070dbSScott Long } 35614c7070dbSScott Long } 35624c7070dbSScott Long txq->ift_cidx = cidx; 35634c7070dbSScott Long txq->ift_gen = gen; 35644c7070dbSScott Long } 35654c7070dbSScott Long 35664c7070dbSScott Long static __inline int 35674c7070dbSScott Long iflib_completed_tx_reclaim(iflib_txq_t txq, int thresh) 35684c7070dbSScott Long { 35694c7070dbSScott Long int reclaim; 35704c7070dbSScott Long if_ctx_t ctx = txq->ift_ctx; 35714c7070dbSScott Long 35724c7070dbSScott Long KASSERT(thresh >= 0, ("invalid threshold to reclaim")); 35734c7070dbSScott Long MPASS(thresh /*+ MAX_TX_DESC(txq->ift_ctx) */ < txq->ift_size); 35744c7070dbSScott Long 35754c7070dbSScott Long /* 35764c7070dbSScott Long * Need a rate-limiting check so that this isn't called every time 35774c7070dbSScott Long */ 35784c7070dbSScott Long iflib_tx_credits_update(ctx, txq); 35794c7070dbSScott Long reclaim = DESC_RECLAIMABLE(txq); 35804c7070dbSScott Long 35814c7070dbSScott Long if (reclaim <= thresh /* + MAX_TX_DESC(txq->ift_ctx) */) { 35824c7070dbSScott Long #ifdef INVARIANTS 35834c7070dbSScott Long if (iflib_verbose_debug) { 35844c7070dbSScott Long printf("%s processed=%ju cleaned=%ju tx_nsegments=%d reclaim=%d thresh=%d\n", __FUNCTION__, 35854c7070dbSScott Long txq->ift_processed, txq->ift_cleaned, txq->ift_ctx->ifc_softc_ctx.isc_tx_nsegments, 35864c7070dbSScott Long reclaim, thresh); 35874c7070dbSScott Long } 35884c7070dbSScott Long #endif 35894c7070dbSScott Long return (0); 35904c7070dbSScott Long } 35914c7070dbSScott Long iflib_tx_desc_free(txq, reclaim); 35924c7070dbSScott Long txq->ift_cleaned += reclaim; 35934c7070dbSScott Long txq->ift_in_use -= reclaim; 35944c7070dbSScott Long 35954c7070dbSScott Long return (reclaim); 35964c7070dbSScott Long } 35974c7070dbSScott Long 35984c7070dbSScott Long static struct mbuf ** 359995246abbSSean Bruno _ring_peek_one(struct ifmp_ring *r, int cidx, int offset, int remaining) 36004c7070dbSScott Long { 360195246abbSSean Bruno int next, size; 360295246abbSSean Bruno struct mbuf **items; 36034c7070dbSScott Long 360495246abbSSean Bruno size = r->size; 360595246abbSSean Bruno next = (cidx + CACHE_PTR_INCREMENT) & (size-1); 360695246abbSSean Bruno items = __DEVOLATILE(struct mbuf **, &r->items[0]); 360795246abbSSean Bruno 360895246abbSSean Bruno prefetch(items[(cidx + offset) & (size-1)]); 360995246abbSSean Bruno if (remaining > 1) { 36103429c02fSStephen Hurd prefetch2cachelines(&items[next]); 36113429c02fSStephen Hurd prefetch2cachelines(items[(cidx + offset + 1) & (size-1)]); 36123429c02fSStephen Hurd prefetch2cachelines(items[(cidx + offset + 2) & (size-1)]); 36133429c02fSStephen Hurd prefetch2cachelines(items[(cidx + offset + 3) & (size-1)]); 361495246abbSSean Bruno } 361595246abbSSean Bruno return (__DEVOLATILE(struct mbuf **, &r->items[(cidx + offset) & (size-1)])); 36164c7070dbSScott Long } 36174c7070dbSScott Long 36184c7070dbSScott Long static void 36194c7070dbSScott Long iflib_txq_check_drain(iflib_txq_t txq, int budget) 36204c7070dbSScott Long { 36214c7070dbSScott Long 362295246abbSSean Bruno ifmp_ring_check_drainage(txq->ift_br, budget); 36234c7070dbSScott Long } 36244c7070dbSScott Long 36254c7070dbSScott Long static uint32_t 36264c7070dbSScott Long iflib_txq_can_drain(struct ifmp_ring *r) 36274c7070dbSScott Long { 36284c7070dbSScott Long iflib_txq_t txq = r->cookie; 36294c7070dbSScott Long if_ctx_t ctx = txq->ift_ctx; 36304c7070dbSScott Long 363195dcf343SMarius Strobl if (TXQ_AVAIL(txq) > MAX_TX_DESC(ctx) + 2) 363295dcf343SMarius Strobl return (1); 36338a04b53dSKonstantin Belousov bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map, 36348a04b53dSKonstantin Belousov BUS_DMASYNC_POSTREAD); 363595dcf343SMarius Strobl return (ctx->isc_txd_credits_update(ctx->ifc_softc, txq->ift_id, 363695dcf343SMarius Strobl false)); 36374c7070dbSScott Long } 36384c7070dbSScott Long 36394c7070dbSScott Long static uint32_t 36404c7070dbSScott Long iflib_txq_drain(struct ifmp_ring *r, uint32_t cidx, uint32_t pidx) 36414c7070dbSScott Long { 36424c7070dbSScott Long iflib_txq_t txq = r->cookie; 36434c7070dbSScott Long if_ctx_t ctx = txq->ift_ctx; 36441722eeacSMarius Strobl if_t ifp = ctx->ifc_ifp; 3645c2c5d1e7SMarius Strobl struct mbuf *m, **mp; 3646c2c5d1e7SMarius Strobl int avail, bytes_sent, consumed, count, err, i, in_use_prev; 3647c2c5d1e7SMarius Strobl int mcast_sent, pkt_sent, reclaimed, txq_avail; 3648c2c5d1e7SMarius Strobl bool do_prefetch, rang, ring; 36494c7070dbSScott Long 36504c7070dbSScott Long if (__predict_false(!(if_getdrvflags(ifp) & IFF_DRV_RUNNING) || 36514c7070dbSScott Long !LINK_ACTIVE(ctx))) { 36524c7070dbSScott Long DBG_COUNTER_INC(txq_drain_notready); 36534c7070dbSScott Long return (0); 36544c7070dbSScott Long } 365595246abbSSean Bruno reclaimed = iflib_completed_tx_reclaim(txq, RECLAIM_THRESH(ctx)); 365695246abbSSean Bruno rang = iflib_txd_db_check(ctx, txq, reclaimed, txq->ift_in_use); 36574c7070dbSScott Long avail = IDXDIFF(pidx, cidx, r->size); 36584c7070dbSScott Long if (__predict_false(ctx->ifc_flags & IFC_QFLUSH)) { 36594c7070dbSScott Long DBG_COUNTER_INC(txq_drain_flushing); 36604c7070dbSScott Long for (i = 0; i < avail; i++) { 3661bc0e855bSStephen Hurd if (__predict_true(r->items[(cidx + i) & (r->size-1)] != (void *)txq)) 366223ac9029SStephen Hurd m_free(r->items[(cidx + i) & (r->size-1)]); 36634c7070dbSScott Long r->items[(cidx + i) & (r->size-1)] = NULL; 36644c7070dbSScott Long } 36654c7070dbSScott Long return (avail); 36664c7070dbSScott Long } 366795246abbSSean Bruno 36684c7070dbSScott Long if (__predict_false(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_OACTIVE)) { 36694c7070dbSScott Long txq->ift_qstatus = IFLIB_QUEUE_IDLE; 36704c7070dbSScott Long CALLOUT_LOCK(txq); 36714c7070dbSScott Long callout_stop(&txq->ift_timer); 36724c7070dbSScott Long CALLOUT_UNLOCK(txq); 36734c7070dbSScott Long DBG_COUNTER_INC(txq_drain_oactive); 36744c7070dbSScott Long return (0); 36754c7070dbSScott Long } 367695246abbSSean Bruno if (reclaimed) 367795246abbSSean Bruno txq->ift_qstatus = IFLIB_QUEUE_IDLE; 36784c7070dbSScott Long consumed = mcast_sent = bytes_sent = pkt_sent = 0; 36794c7070dbSScott Long count = MIN(avail, TX_BATCH_SIZE); 3680da69b8f9SSean Bruno #ifdef INVARIANTS 3681da69b8f9SSean Bruno if (iflib_verbose_debug) 3682da69b8f9SSean Bruno printf("%s avail=%d ifc_flags=%x txq_avail=%d ", __FUNCTION__, 3683da69b8f9SSean Bruno avail, ctx->ifc_flags, TXQ_AVAIL(txq)); 3684da69b8f9SSean Bruno #endif 368595246abbSSean Bruno do_prefetch = (ctx->ifc_flags & IFC_PREFETCH); 3686c2c5d1e7SMarius Strobl txq_avail = TXQ_AVAIL(txq); 36871ae4848cSMatt Macy err = 0; 3688c2c5d1e7SMarius Strobl for (i = 0; i < count && txq_avail > MAX_TX_DESC(ctx) + 2; i++) { 36891ae4848cSMatt Macy int rem = do_prefetch ? count - i : 0; 36904c7070dbSScott Long 369195246abbSSean Bruno mp = _ring_peek_one(r, cidx, i, rem); 3692da69b8f9SSean Bruno MPASS(mp != NULL && *mp != NULL); 369395246abbSSean Bruno if (__predict_false(*mp == (struct mbuf *)txq)) { 369495246abbSSean Bruno consumed++; 369595246abbSSean Bruno continue; 369695246abbSSean Bruno } 36974c7070dbSScott Long in_use_prev = txq->ift_in_use; 369895246abbSSean Bruno err = iflib_encap(txq, mp); 369995246abbSSean Bruno if (__predict_false(err)) { 3700da69b8f9SSean Bruno /* no room - bail out */ 370195246abbSSean Bruno if (err == ENOBUFS) 37024c7070dbSScott Long break; 37034c7070dbSScott Long consumed++; 3704da69b8f9SSean Bruno /* we can't send this packet - skip it */ 37054c7070dbSScott Long continue; 3706da69b8f9SSean Bruno } 370795246abbSSean Bruno consumed++; 37084c7070dbSScott Long pkt_sent++; 37094c7070dbSScott Long m = *mp; 37104c7070dbSScott Long DBG_COUNTER_INC(tx_sent); 37114c7070dbSScott Long bytes_sent += m->m_pkthdr.len; 371295246abbSSean Bruno mcast_sent += !!(m->m_flags & M_MCAST); 3713c2c5d1e7SMarius Strobl txq_avail = TXQ_AVAIL(txq); 37144c7070dbSScott Long 37154c7070dbSScott Long txq->ift_db_pending += (txq->ift_in_use - in_use_prev); 37164c7070dbSScott Long ETHER_BPF_MTAP(ifp, m); 371795246abbSSean Bruno if (__predict_false(!(ifp->if_drv_flags & IFF_DRV_RUNNING))) 37184c7070dbSScott Long break; 371995246abbSSean Bruno rang = iflib_txd_db_check(ctx, txq, false, in_use_prev); 37204c7070dbSScott Long } 37214c7070dbSScott Long 372295246abbSSean Bruno /* deliberate use of bitwise or to avoid gratuitous short-circuit */ 372395246abbSSean Bruno ring = rang ? false : (iflib_min_tx_latency | err) || (TXQ_AVAIL(txq) < MAX_TX_DESC(ctx)); 372495246abbSSean Bruno iflib_txd_db_check(ctx, txq, ring, txq->ift_in_use); 37254c7070dbSScott Long if_inc_counter(ifp, IFCOUNTER_OBYTES, bytes_sent); 37264c7070dbSScott Long if_inc_counter(ifp, IFCOUNTER_OPACKETS, pkt_sent); 37274c7070dbSScott Long if (mcast_sent) 37284c7070dbSScott Long if_inc_counter(ifp, IFCOUNTER_OMCASTS, mcast_sent); 3729da69b8f9SSean Bruno #ifdef INVARIANTS 3730da69b8f9SSean Bruno if (iflib_verbose_debug) 3731da69b8f9SSean Bruno printf("consumed=%d\n", consumed); 3732da69b8f9SSean Bruno #endif 37334c7070dbSScott Long return (consumed); 37344c7070dbSScott Long } 37354c7070dbSScott Long 3736da69b8f9SSean Bruno static uint32_t 3737da69b8f9SSean Bruno iflib_txq_drain_always(struct ifmp_ring *r) 3738da69b8f9SSean Bruno { 3739da69b8f9SSean Bruno return (1); 3740da69b8f9SSean Bruno } 3741da69b8f9SSean Bruno 3742da69b8f9SSean Bruno static uint32_t 3743da69b8f9SSean Bruno iflib_txq_drain_free(struct ifmp_ring *r, uint32_t cidx, uint32_t pidx) 3744da69b8f9SSean Bruno { 3745da69b8f9SSean Bruno int i, avail; 3746da69b8f9SSean Bruno struct mbuf **mp; 3747da69b8f9SSean Bruno iflib_txq_t txq; 3748da69b8f9SSean Bruno 3749da69b8f9SSean Bruno txq = r->cookie; 3750da69b8f9SSean Bruno 3751da69b8f9SSean Bruno txq->ift_qstatus = IFLIB_QUEUE_IDLE; 3752da69b8f9SSean Bruno CALLOUT_LOCK(txq); 3753da69b8f9SSean Bruno callout_stop(&txq->ift_timer); 3754da69b8f9SSean Bruno CALLOUT_UNLOCK(txq); 3755da69b8f9SSean Bruno 3756da69b8f9SSean Bruno avail = IDXDIFF(pidx, cidx, r->size); 3757da69b8f9SSean Bruno for (i = 0; i < avail; i++) { 375895246abbSSean Bruno mp = _ring_peek_one(r, cidx, i, avail - i); 375995246abbSSean Bruno if (__predict_false(*mp == (struct mbuf *)txq)) 376095246abbSSean Bruno continue; 3761da69b8f9SSean Bruno m_freem(*mp); 376264e6fc13SStephen Hurd DBG_COUNTER_INC(tx_frees); 3763da69b8f9SSean Bruno } 3764da69b8f9SSean Bruno MPASS(ifmp_ring_is_stalled(r) == 0); 3765da69b8f9SSean Bruno return (avail); 3766da69b8f9SSean Bruno } 3767da69b8f9SSean Bruno 3768da69b8f9SSean Bruno static void 3769da69b8f9SSean Bruno iflib_ifmp_purge(iflib_txq_t txq) 3770da69b8f9SSean Bruno { 3771da69b8f9SSean Bruno struct ifmp_ring *r; 3772da69b8f9SSean Bruno 377395246abbSSean Bruno r = txq->ift_br; 3774da69b8f9SSean Bruno r->drain = iflib_txq_drain_free; 3775da69b8f9SSean Bruno r->can_drain = iflib_txq_drain_always; 3776da69b8f9SSean Bruno 3777da69b8f9SSean Bruno ifmp_ring_check_drainage(r, r->size); 3778da69b8f9SSean Bruno 3779da69b8f9SSean Bruno r->drain = iflib_txq_drain; 3780da69b8f9SSean Bruno r->can_drain = iflib_txq_can_drain; 3781da69b8f9SSean Bruno } 3782da69b8f9SSean Bruno 37834c7070dbSScott Long static void 378423ac9029SStephen Hurd _task_fn_tx(void *context) 37854c7070dbSScott Long { 37864c7070dbSScott Long iflib_txq_t txq = context; 37874c7070dbSScott Long if_ctx_t ctx = txq->ift_ctx; 3788a6611c93SMarius Strobl if_t ifp = ctx->ifc_ifp; 3789fe51d4cdSStephen Hurd int abdicate = ctx->ifc_sysctl_tx_abdicate; 37904c7070dbSScott Long 37911248952aSSean Bruno #ifdef IFLIB_DIAGNOSTICS 37921248952aSSean Bruno txq->ift_cpu_exec_count[curcpu]++; 37931248952aSSean Bruno #endif 379488a68866SVincenzo Maffione if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) 37954c7070dbSScott Long return; 379695dcf343SMarius Strobl #ifdef DEV_NETMAP 379788a68866SVincenzo Maffione if ((if_getcapenable(ifp) & IFCAP_NETMAP) && 379888a68866SVincenzo Maffione netmap_tx_irq(ifp, txq->ift_id)) 379988a68866SVincenzo Maffione goto skip_ifmp; 380095dcf343SMarius Strobl #endif 3801b8ca4756SPatrick Kelsey #ifdef ALTQ 3802b8ca4756SPatrick Kelsey if (ALTQ_IS_ENABLED(&ifp->if_snd)) 3803b8ca4756SPatrick Kelsey iflib_altq_if_start(ifp); 3804b8ca4756SPatrick Kelsey #endif 380595246abbSSean Bruno if (txq->ift_db_pending) 3806fe51d4cdSStephen Hurd ifmp_ring_enqueue(txq->ift_br, (void **)&txq, 1, TX_BATCH_SIZE, abdicate); 3807fe51d4cdSStephen Hurd else if (!abdicate) 3808fe51d4cdSStephen Hurd ifmp_ring_check_drainage(txq->ift_br, TX_BATCH_SIZE); 3809fe51d4cdSStephen Hurd /* 3810fe51d4cdSStephen Hurd * When abdicating, we always need to check drainage, not just when we don't enqueue 3811fe51d4cdSStephen Hurd */ 3812fe51d4cdSStephen Hurd if (abdicate) 3813fe51d4cdSStephen Hurd ifmp_ring_check_drainage(txq->ift_br, TX_BATCH_SIZE); 381488a68866SVincenzo Maffione #ifdef DEV_NETMAP 381588a68866SVincenzo Maffione skip_ifmp: 381688a68866SVincenzo Maffione #endif 381795246abbSSean Bruno if (ctx->ifc_flags & IFC_LEGACY) 381895246abbSSean Bruno IFDI_INTR_ENABLE(ctx); 38193d10e9edSMarius Strobl else 38201ae4848cSMatt Macy IFDI_TX_QUEUE_INTR_ENABLE(ctx, txq->ift_id); 38214c7070dbSScott Long } 38224c7070dbSScott Long 38234c7070dbSScott Long static void 382423ac9029SStephen Hurd _task_fn_rx(void *context) 38254c7070dbSScott Long { 38264c7070dbSScott Long iflib_rxq_t rxq = context; 38274c7070dbSScott Long if_ctx_t ctx = rxq->ifr_ctx; 3828fb1a29b4SHans Petter Selasky uint8_t more; 3829f4d2154eSStephen Hurd uint16_t budget; 3830e136e9c8SVincenzo Maffione #ifdef DEV_NETMAP 3831e136e9c8SVincenzo Maffione u_int work = 0; 3832e136e9c8SVincenzo Maffione int nmirq; 3833e136e9c8SVincenzo Maffione #endif 38344c7070dbSScott Long 38351248952aSSean Bruno #ifdef IFLIB_DIAGNOSTICS 38361248952aSSean Bruno rxq->ifr_cpu_exec_count[curcpu]++; 38371248952aSSean Bruno #endif 38384c7070dbSScott Long DBG_COUNTER_INC(task_fn_rxs); 38394c7070dbSScott Long if (__predict_false(!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING))) 38404c7070dbSScott Long return; 3841d0d0ad0aSStephen Hurd #ifdef DEV_NETMAP 3842e136e9c8SVincenzo Maffione nmirq = netmap_rx_irq(ctx->ifc_ifp, rxq->ifr_id, &work); 3843e136e9c8SVincenzo Maffione if (nmirq != NM_IRQ_PASS) { 3844e136e9c8SVincenzo Maffione more = (nmirq == NM_IRQ_RESCHED) ? IFLIB_RXEOF_MORE : 0; 3845fb1a29b4SHans Petter Selasky goto skip_rxeof; 3846d0d0ad0aSStephen Hurd } 3847d0d0ad0aSStephen Hurd #endif 3848f4d2154eSStephen Hurd budget = ctx->ifc_sysctl_rx_budget; 3849f4d2154eSStephen Hurd if (budget == 0) 3850f4d2154eSStephen Hurd budget = 16; /* XXX */ 3851fb1a29b4SHans Petter Selasky more = iflib_rxeof(rxq, budget); 3852fb1a29b4SHans Petter Selasky #ifdef DEV_NETMAP 3853fb1a29b4SHans Petter Selasky skip_rxeof: 3854fb1a29b4SHans Petter Selasky #endif 3855fb1a29b4SHans Petter Selasky if ((more & IFLIB_RXEOF_MORE) == 0) { 38564c7070dbSScott Long if (ctx->ifc_flags & IFC_LEGACY) 38574c7070dbSScott Long IFDI_INTR_ENABLE(ctx); 38583d10e9edSMarius Strobl else 38591ae4848cSMatt Macy IFDI_RX_QUEUE_INTR_ENABLE(ctx, rxq->ifr_id); 38601ae4848cSMatt Macy DBG_COUNTER_INC(rx_intr_enables); 38614c7070dbSScott Long } 38624c7070dbSScott Long if (__predict_false(!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING))) 38634c7070dbSScott Long return; 3864fb1a29b4SHans Petter Selasky 3865fb1a29b4SHans Petter Selasky if (more & IFLIB_RXEOF_MORE) 38664c7070dbSScott Long GROUPTASK_ENQUEUE(&rxq->ifr_task); 3867fb1a29b4SHans Petter Selasky else if (more & IFLIB_RXEOF_EMPTY) 3868fb1a29b4SHans Petter Selasky callout_reset_curcpu(&rxq->ifr_watchdog, 1, &_task_fn_rx_watchdog, rxq); 38694c7070dbSScott Long } 38704c7070dbSScott Long 38714c7070dbSScott Long static void 387223ac9029SStephen Hurd _task_fn_admin(void *context) 38734c7070dbSScott Long { 38744c7070dbSScott Long if_ctx_t ctx = context; 38754c7070dbSScott Long if_softc_ctx_t sctx = &ctx->ifc_softc_ctx; 38764c7070dbSScott Long iflib_txq_t txq; 3877ab2e3f79SStephen Hurd int i; 387877c1fcecSEric Joyner bool oactive, running, do_reset, do_watchdog, in_detach; 3879ab2e3f79SStephen Hurd 38807b610b60SSean Bruno STATE_LOCK(ctx); 38817b610b60SSean Bruno running = (if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING); 38827b610b60SSean Bruno oactive = (if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_OACTIVE); 38837b610b60SSean Bruno do_reset = (ctx->ifc_flags & IFC_DO_RESET); 38847b610b60SSean Bruno do_watchdog = (ctx->ifc_flags & IFC_DO_WATCHDOG); 388577c1fcecSEric Joyner in_detach = (ctx->ifc_flags & IFC_IN_DETACH); 38867b610b60SSean Bruno ctx->ifc_flags &= ~(IFC_DO_RESET|IFC_DO_WATCHDOG); 38877b610b60SSean Bruno STATE_UNLOCK(ctx); 38887b610b60SSean Bruno 388977c1fcecSEric Joyner if ((!running && !oactive) && !(ctx->ifc_sctx->isc_flags & IFLIB_ADMIN_ALWAYS_RUN)) 389077c1fcecSEric Joyner return; 389177c1fcecSEric Joyner if (in_detach) 3892ab2e3f79SStephen Hurd return; 38934c7070dbSScott Long 38944c7070dbSScott Long CTX_LOCK(ctx); 38954c7070dbSScott Long for (txq = ctx->ifc_txqs, i = 0; i < sctx->isc_ntxqsets; i++, txq++) { 38964c7070dbSScott Long CALLOUT_LOCK(txq); 38974c7070dbSScott Long callout_stop(&txq->ift_timer); 38984c7070dbSScott Long CALLOUT_UNLOCK(txq); 38994c7070dbSScott Long } 39007b610b60SSean Bruno if (do_watchdog) { 39017b610b60SSean Bruno ctx->ifc_watchdog_events++; 39027b610b60SSean Bruno IFDI_WATCHDOG_RESET(ctx); 39037b610b60SSean Bruno } 3904d300df01SStephen Hurd IFDI_UPDATE_ADMIN_STATUS(ctx); 3905dd7fbcf1SStephen Hurd for (txq = ctx->ifc_txqs, i = 0; i < sctx->isc_ntxqsets; i++, txq++) { 3906*17cec474SVincenzo Maffione callout_reset_on(&txq->ift_timer, hz / 2, iflib_timer, txq, 3907*17cec474SVincenzo Maffione txq->ift_timer.c_cpu); 3908dd7fbcf1SStephen Hurd } 3909ab2e3f79SStephen Hurd IFDI_LINK_INTR_ENABLE(ctx); 39107b610b60SSean Bruno if (do_reset) 3911ab2e3f79SStephen Hurd iflib_if_init_locked(ctx); 39124c7070dbSScott Long CTX_UNLOCK(ctx); 39134c7070dbSScott Long 3914ab2e3f79SStephen Hurd if (LINK_ACTIVE(ctx) == 0) 39154c7070dbSScott Long return; 39164c7070dbSScott Long for (txq = ctx->ifc_txqs, i = 0; i < sctx->isc_ntxqsets; i++, txq++) 39174c7070dbSScott Long iflib_txq_check_drain(txq, IFLIB_RESTART_BUDGET); 39184c7070dbSScott Long } 39194c7070dbSScott Long 39204c7070dbSScott Long static void 392123ac9029SStephen Hurd _task_fn_iov(void *context) 39224c7070dbSScott Long { 39234c7070dbSScott Long if_ctx_t ctx = context; 39244c7070dbSScott Long 392577c1fcecSEric Joyner if (!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING) && 392677c1fcecSEric Joyner !(ctx->ifc_sctx->isc_flags & IFLIB_ADMIN_ALWAYS_RUN)) 39274c7070dbSScott Long return; 39284c7070dbSScott Long 39294c7070dbSScott Long CTX_LOCK(ctx); 39304c7070dbSScott Long IFDI_VFLR_HANDLE(ctx); 39314c7070dbSScott Long CTX_UNLOCK(ctx); 39324c7070dbSScott Long } 39334c7070dbSScott Long 39344c7070dbSScott Long static int 39354c7070dbSScott Long iflib_sysctl_int_delay(SYSCTL_HANDLER_ARGS) 39364c7070dbSScott Long { 39374c7070dbSScott Long int err; 39384c7070dbSScott Long if_int_delay_info_t info; 39394c7070dbSScott Long if_ctx_t ctx; 39404c7070dbSScott Long 39414c7070dbSScott Long info = (if_int_delay_info_t)arg1; 39424c7070dbSScott Long ctx = info->iidi_ctx; 39434c7070dbSScott Long info->iidi_req = req; 39444c7070dbSScott Long info->iidi_oidp = oidp; 39454c7070dbSScott Long CTX_LOCK(ctx); 39464c7070dbSScott Long err = IFDI_SYSCTL_INT_DELAY(ctx, info); 39474c7070dbSScott Long CTX_UNLOCK(ctx); 39484c7070dbSScott Long return (err); 39494c7070dbSScott Long } 39504c7070dbSScott Long 39514c7070dbSScott Long /********************************************************************* 39524c7070dbSScott Long * 39534c7070dbSScott Long * IFNET FUNCTIONS 39544c7070dbSScott Long * 39554c7070dbSScott Long **********************************************************************/ 39564c7070dbSScott Long 39574c7070dbSScott Long static void 39584c7070dbSScott Long iflib_if_init_locked(if_ctx_t ctx) 39594c7070dbSScott Long { 39604c7070dbSScott Long iflib_stop(ctx); 39614c7070dbSScott Long iflib_init_locked(ctx); 39624c7070dbSScott Long } 39634c7070dbSScott Long 39644c7070dbSScott Long static void 39654c7070dbSScott Long iflib_if_init(void *arg) 39664c7070dbSScott Long { 39674c7070dbSScott Long if_ctx_t ctx = arg; 39684c7070dbSScott Long 39694c7070dbSScott Long CTX_LOCK(ctx); 39704c7070dbSScott Long iflib_if_init_locked(ctx); 39714c7070dbSScott Long CTX_UNLOCK(ctx); 39724c7070dbSScott Long } 39734c7070dbSScott Long 39744c7070dbSScott Long static int 39754c7070dbSScott Long iflib_if_transmit(if_t ifp, struct mbuf *m) 39764c7070dbSScott Long { 39774c7070dbSScott Long if_ctx_t ctx = if_getsoftc(ifp); 39784c7070dbSScott Long 39794c7070dbSScott Long iflib_txq_t txq; 398023ac9029SStephen Hurd int err, qidx; 3981fe51d4cdSStephen Hurd int abdicate = ctx->ifc_sysctl_tx_abdicate; 39824c7070dbSScott Long 39834c7070dbSScott Long if (__predict_false((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || !LINK_ACTIVE(ctx))) { 39844c7070dbSScott Long DBG_COUNTER_INC(tx_frees); 39854c7070dbSScott Long m_freem(m); 3986225eae1bSEric Joyner return (ENETDOWN); 39874c7070dbSScott Long } 39884c7070dbSScott Long 398923ac9029SStephen Hurd MPASS(m->m_nextpkt == NULL); 3990b8ca4756SPatrick Kelsey /* ALTQ-enabled interfaces always use queue 0. */ 39914c7070dbSScott Long qidx = 0; 3992b8ca4756SPatrick Kelsey if ((NTXQSETS(ctx) > 1) && M_HASHTYPE_GET(m) && !ALTQ_IS_ENABLED(&ifp->if_snd)) 39934c7070dbSScott Long qidx = QIDX(ctx, m); 39944c7070dbSScott Long /* 39954c7070dbSScott Long * XXX calculate buf_ring based on flowid (divvy up bits?) 39964c7070dbSScott Long */ 39974c7070dbSScott Long txq = &ctx->ifc_txqs[qidx]; 39984c7070dbSScott Long 39994c7070dbSScott Long #ifdef DRIVER_BACKPRESSURE 40004c7070dbSScott Long if (txq->ift_closed) { 40014c7070dbSScott Long while (m != NULL) { 40024c7070dbSScott Long next = m->m_nextpkt; 40034c7070dbSScott Long m->m_nextpkt = NULL; 40044c7070dbSScott Long m_freem(m); 400564e6fc13SStephen Hurd DBG_COUNTER_INC(tx_frees); 40064c7070dbSScott Long m = next; 40074c7070dbSScott Long } 40084c7070dbSScott Long return (ENOBUFS); 40094c7070dbSScott Long } 40104c7070dbSScott Long #endif 401123ac9029SStephen Hurd #ifdef notyet 40124c7070dbSScott Long qidx = count = 0; 40134c7070dbSScott Long mp = marr; 40144c7070dbSScott Long next = m; 40154c7070dbSScott Long do { 40164c7070dbSScott Long count++; 40174c7070dbSScott Long next = next->m_nextpkt; 40184c7070dbSScott Long } while (next != NULL); 40194c7070dbSScott Long 402016fb86abSConrad Meyer if (count > nitems(marr)) 40214c7070dbSScott Long if ((mp = malloc(count*sizeof(struct mbuf *), M_IFLIB, M_NOWAIT)) == NULL) { 40224c7070dbSScott Long /* XXX check nextpkt */ 40234c7070dbSScott Long m_freem(m); 40244c7070dbSScott Long /* XXX simplify for now */ 40254c7070dbSScott Long DBG_COUNTER_INC(tx_frees); 40264c7070dbSScott Long return (ENOBUFS); 40274c7070dbSScott Long } 40284c7070dbSScott Long for (next = m, i = 0; next != NULL; i++) { 40294c7070dbSScott Long mp[i] = next; 40304c7070dbSScott Long next = next->m_nextpkt; 40314c7070dbSScott Long mp[i]->m_nextpkt = NULL; 40324c7070dbSScott Long } 403323ac9029SStephen Hurd #endif 40344c7070dbSScott Long DBG_COUNTER_INC(tx_seen); 4035fe51d4cdSStephen Hurd err = ifmp_ring_enqueue(txq->ift_br, (void **)&m, 1, TX_BATCH_SIZE, abdicate); 40364c7070dbSScott Long 4037fe51d4cdSStephen Hurd if (abdicate) 4038ab2e3f79SStephen Hurd GROUPTASK_ENQUEUE(&txq->ift_task); 40391225d9daSStephen Hurd if (err) { 4040fe51d4cdSStephen Hurd if (!abdicate) 4041fe51d4cdSStephen Hurd GROUPTASK_ENQUEUE(&txq->ift_task); 40424c7070dbSScott Long /* support forthcoming later */ 40434c7070dbSScott Long #ifdef DRIVER_BACKPRESSURE 40444c7070dbSScott Long txq->ift_closed = TRUE; 40454c7070dbSScott Long #endif 404695246abbSSean Bruno ifmp_ring_check_drainage(txq->ift_br, TX_BATCH_SIZE); 404723ac9029SStephen Hurd m_freem(m); 404864e6fc13SStephen Hurd DBG_COUNTER_INC(tx_frees); 40494c7070dbSScott Long } 40504c7070dbSScott Long 40514c7070dbSScott Long return (err); 40524c7070dbSScott Long } 40534c7070dbSScott Long 4054b8ca4756SPatrick Kelsey #ifdef ALTQ 4055b8ca4756SPatrick Kelsey /* 4056b8ca4756SPatrick Kelsey * The overall approach to integrating iflib with ALTQ is to continue to use 4057b8ca4756SPatrick Kelsey * the iflib mp_ring machinery between the ALTQ queue(s) and the hardware 4058b8ca4756SPatrick Kelsey * ring. Technically, when using ALTQ, queueing to an intermediate mp_ring 4059b8ca4756SPatrick Kelsey * is redundant/unnecessary, but doing so minimizes the amount of 4060b8ca4756SPatrick Kelsey * ALTQ-specific code required in iflib. It is assumed that the overhead of 4061b8ca4756SPatrick Kelsey * redundantly queueing to an intermediate mp_ring is swamped by the 4062b8ca4756SPatrick Kelsey * performance limitations inherent in using ALTQ. 4063b8ca4756SPatrick Kelsey * 4064b8ca4756SPatrick Kelsey * When ALTQ support is compiled in, all iflib drivers will use a transmit 4065b8ca4756SPatrick Kelsey * routine, iflib_altq_if_transmit(), that checks if ALTQ is enabled for the 4066b8ca4756SPatrick Kelsey * given interface. If ALTQ is enabled for an interface, then all 4067b8ca4756SPatrick Kelsey * transmitted packets for that interface will be submitted to the ALTQ 4068b8ca4756SPatrick Kelsey * subsystem via IFQ_ENQUEUE(). We don't use the legacy if_transmit() 4069b8ca4756SPatrick Kelsey * implementation because it uses IFQ_HANDOFF(), which will duplicatively 4070b8ca4756SPatrick Kelsey * update stats that the iflib machinery handles, and which is sensitve to 4071b8ca4756SPatrick Kelsey * the disused IFF_DRV_OACTIVE flag. Additionally, iflib_altq_if_start() 4072b8ca4756SPatrick Kelsey * will be installed as the start routine for use by ALTQ facilities that 4073b8ca4756SPatrick Kelsey * need to trigger queue drains on a scheduled basis. 4074b8ca4756SPatrick Kelsey * 4075b8ca4756SPatrick Kelsey */ 4076b8ca4756SPatrick Kelsey static void 4077b8ca4756SPatrick Kelsey iflib_altq_if_start(if_t ifp) 4078b8ca4756SPatrick Kelsey { 4079b8ca4756SPatrick Kelsey struct ifaltq *ifq = &ifp->if_snd; 4080b8ca4756SPatrick Kelsey struct mbuf *m; 4081b8ca4756SPatrick Kelsey 4082b8ca4756SPatrick Kelsey IFQ_LOCK(ifq); 4083b8ca4756SPatrick Kelsey IFQ_DEQUEUE_NOLOCK(ifq, m); 4084b8ca4756SPatrick Kelsey while (m != NULL) { 4085b8ca4756SPatrick Kelsey iflib_if_transmit(ifp, m); 4086b8ca4756SPatrick Kelsey IFQ_DEQUEUE_NOLOCK(ifq, m); 4087b8ca4756SPatrick Kelsey } 4088b8ca4756SPatrick Kelsey IFQ_UNLOCK(ifq); 4089b8ca4756SPatrick Kelsey } 4090b8ca4756SPatrick Kelsey 4091b8ca4756SPatrick Kelsey static int 4092b8ca4756SPatrick Kelsey iflib_altq_if_transmit(if_t ifp, struct mbuf *m) 4093b8ca4756SPatrick Kelsey { 4094b8ca4756SPatrick Kelsey int err; 4095b8ca4756SPatrick Kelsey 4096b8ca4756SPatrick Kelsey if (ALTQ_IS_ENABLED(&ifp->if_snd)) { 4097b8ca4756SPatrick Kelsey IFQ_ENQUEUE(&ifp->if_snd, m, err); 4098b8ca4756SPatrick Kelsey if (err == 0) 4099b8ca4756SPatrick Kelsey iflib_altq_if_start(ifp); 4100b8ca4756SPatrick Kelsey } else 4101b8ca4756SPatrick Kelsey err = iflib_if_transmit(ifp, m); 4102b8ca4756SPatrick Kelsey 4103b8ca4756SPatrick Kelsey return (err); 4104b8ca4756SPatrick Kelsey } 4105b8ca4756SPatrick Kelsey #endif /* ALTQ */ 4106b8ca4756SPatrick Kelsey 41074c7070dbSScott Long static void 41084c7070dbSScott Long iflib_if_qflush(if_t ifp) 41094c7070dbSScott Long { 41104c7070dbSScott Long if_ctx_t ctx = if_getsoftc(ifp); 41114c7070dbSScott Long iflib_txq_t txq = ctx->ifc_txqs; 41124c7070dbSScott Long int i; 41134c7070dbSScott Long 41147b610b60SSean Bruno STATE_LOCK(ctx); 41154c7070dbSScott Long ctx->ifc_flags |= IFC_QFLUSH; 41167b610b60SSean Bruno STATE_UNLOCK(ctx); 41174c7070dbSScott Long for (i = 0; i < NTXQSETS(ctx); i++, txq++) 411895246abbSSean Bruno while (!(ifmp_ring_is_idle(txq->ift_br) || ifmp_ring_is_stalled(txq->ift_br))) 41194c7070dbSScott Long iflib_txq_check_drain(txq, 0); 41207b610b60SSean Bruno STATE_LOCK(ctx); 41214c7070dbSScott Long ctx->ifc_flags &= ~IFC_QFLUSH; 41227b610b60SSean Bruno STATE_UNLOCK(ctx); 41234c7070dbSScott Long 4124b8ca4756SPatrick Kelsey /* 4125b8ca4756SPatrick Kelsey * When ALTQ is enabled, this will also take care of purging the 4126b8ca4756SPatrick Kelsey * ALTQ queue(s). 4127b8ca4756SPatrick Kelsey */ 41284c7070dbSScott Long if_qflush(ifp); 41294c7070dbSScott Long } 41304c7070dbSScott Long 41310c919c23SStephen Hurd #define IFCAP_FLAGS (IFCAP_HWCSUM_IPV6 | IFCAP_HWCSUM | IFCAP_LRO | \ 41320c919c23SStephen Hurd IFCAP_TSO | IFCAP_VLAN_HWTAGGING | IFCAP_HWSTATS | \ 41330c919c23SStephen Hurd IFCAP_VLAN_MTU | IFCAP_VLAN_HWFILTER | \ 41346554362cSAndrew Gallatin IFCAP_VLAN_HWTSO | IFCAP_VLAN_HWCSUM | IFCAP_NOMAP) 41354c7070dbSScott Long 41364c7070dbSScott Long static int 41374c7070dbSScott Long iflib_if_ioctl(if_t ifp, u_long command, caddr_t data) 41384c7070dbSScott Long { 41394c7070dbSScott Long if_ctx_t ctx = if_getsoftc(ifp); 41404c7070dbSScott Long struct ifreq *ifr = (struct ifreq *)data; 41414c7070dbSScott Long #if defined(INET) || defined(INET6) 41424c7070dbSScott Long struct ifaddr *ifa = (struct ifaddr *)data; 41434c7070dbSScott Long #endif 41441722eeacSMarius Strobl bool avoid_reset = false; 41454c7070dbSScott Long int err = 0, reinit = 0, bits; 41464c7070dbSScott Long 41474c7070dbSScott Long switch (command) { 41484c7070dbSScott Long case SIOCSIFADDR: 41494c7070dbSScott Long #ifdef INET 41504c7070dbSScott Long if (ifa->ifa_addr->sa_family == AF_INET) 41511722eeacSMarius Strobl avoid_reset = true; 41524c7070dbSScott Long #endif 41534c7070dbSScott Long #ifdef INET6 41544c7070dbSScott Long if (ifa->ifa_addr->sa_family == AF_INET6) 41551722eeacSMarius Strobl avoid_reset = true; 41564c7070dbSScott Long #endif 41574c7070dbSScott Long /* 41584c7070dbSScott Long ** Calling init results in link renegotiation, 41594c7070dbSScott Long ** so we avoid doing it when possible. 41604c7070dbSScott Long */ 41614c7070dbSScott Long if (avoid_reset) { 41624c7070dbSScott Long if_setflagbits(ifp, IFF_UP,0); 41634c7070dbSScott Long if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) 41644c7070dbSScott Long reinit = 1; 41654c7070dbSScott Long #ifdef INET 41664c7070dbSScott Long if (!(if_getflags(ifp) & IFF_NOARP)) 41674c7070dbSScott Long arp_ifinit(ifp, ifa); 41684c7070dbSScott Long #endif 41694c7070dbSScott Long } else 41704c7070dbSScott Long err = ether_ioctl(ifp, command, data); 41714c7070dbSScott Long break; 41724c7070dbSScott Long case SIOCSIFMTU: 41734c7070dbSScott Long CTX_LOCK(ctx); 41744c7070dbSScott Long if (ifr->ifr_mtu == if_getmtu(ifp)) { 41754c7070dbSScott Long CTX_UNLOCK(ctx); 41764c7070dbSScott Long break; 41774c7070dbSScott Long } 41784c7070dbSScott Long bits = if_getdrvflags(ifp); 41794c7070dbSScott Long /* stop the driver and free any clusters before proceeding */ 41804c7070dbSScott Long iflib_stop(ctx); 41814c7070dbSScott Long 41824c7070dbSScott Long if ((err = IFDI_MTU_SET(ctx, ifr->ifr_mtu)) == 0) { 41837b610b60SSean Bruno STATE_LOCK(ctx); 41844c7070dbSScott Long if (ifr->ifr_mtu > ctx->ifc_max_fl_buf_size) 41854c7070dbSScott Long ctx->ifc_flags |= IFC_MULTISEG; 41864c7070dbSScott Long else 41874c7070dbSScott Long ctx->ifc_flags &= ~IFC_MULTISEG; 41887b610b60SSean Bruno STATE_UNLOCK(ctx); 41894c7070dbSScott Long err = if_setmtu(ifp, ifr->ifr_mtu); 41904c7070dbSScott Long } 41914c7070dbSScott Long iflib_init_locked(ctx); 41927b610b60SSean Bruno STATE_LOCK(ctx); 41934c7070dbSScott Long if_setdrvflags(ifp, bits); 41947b610b60SSean Bruno STATE_UNLOCK(ctx); 41954c7070dbSScott Long CTX_UNLOCK(ctx); 41964c7070dbSScott Long break; 41974c7070dbSScott Long case SIOCSIFFLAGS: 4198ab2e3f79SStephen Hurd CTX_LOCK(ctx); 4199ab2e3f79SStephen Hurd if (if_getflags(ifp) & IFF_UP) { 4200ab2e3f79SStephen Hurd if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) { 4201ab2e3f79SStephen Hurd if ((if_getflags(ifp) ^ ctx->ifc_if_flags) & 4202ab2e3f79SStephen Hurd (IFF_PROMISC | IFF_ALLMULTI)) { 42030ae0e8d2SMatt Macy CTX_UNLOCK(ctx); 4204ab2e3f79SStephen Hurd err = IFDI_PROMISC_SET(ctx, if_getflags(ifp)); 42050ae0e8d2SMatt Macy CTX_LOCK(ctx); 4206ab2e3f79SStephen Hurd } 4207ab2e3f79SStephen Hurd } else 4208ab2e3f79SStephen Hurd reinit = 1; 4209ab2e3f79SStephen Hurd } else if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) { 4210ab2e3f79SStephen Hurd iflib_stop(ctx); 4211ab2e3f79SStephen Hurd } 4212ab2e3f79SStephen Hurd ctx->ifc_if_flags = if_getflags(ifp); 4213ab2e3f79SStephen Hurd CTX_UNLOCK(ctx); 42144c7070dbSScott Long break; 42154c7070dbSScott Long case SIOCADDMULTI: 42164c7070dbSScott Long case SIOCDELMULTI: 42174c7070dbSScott Long if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) { 4218ab2e3f79SStephen Hurd CTX_LOCK(ctx); 4219ab2e3f79SStephen Hurd IFDI_INTR_DISABLE(ctx); 4220ab2e3f79SStephen Hurd IFDI_MULTI_SET(ctx); 4221ab2e3f79SStephen Hurd IFDI_INTR_ENABLE(ctx); 4222ab2e3f79SStephen Hurd CTX_UNLOCK(ctx); 42234c7070dbSScott Long } 42244c7070dbSScott Long break; 42254c7070dbSScott Long case SIOCSIFMEDIA: 42264c7070dbSScott Long CTX_LOCK(ctx); 42274c7070dbSScott Long IFDI_MEDIA_SET(ctx); 42284c7070dbSScott Long CTX_UNLOCK(ctx); 42291722eeacSMarius Strobl /* FALLTHROUGH */ 42304c7070dbSScott Long case SIOCGIFMEDIA: 4231a027c8e9SStephen Hurd case SIOCGIFXMEDIA: 4232e2621d96SMatt Macy err = ifmedia_ioctl(ifp, ifr, ctx->ifc_mediap, command); 42334c7070dbSScott Long break; 42344c7070dbSScott Long case SIOCGI2C: 42354c7070dbSScott Long { 42364c7070dbSScott Long struct ifi2creq i2c; 42374c7070dbSScott Long 4238541d96aaSBrooks Davis err = copyin(ifr_data_get_ptr(ifr), &i2c, sizeof(i2c)); 42394c7070dbSScott Long if (err != 0) 42404c7070dbSScott Long break; 42414c7070dbSScott Long if (i2c.dev_addr != 0xA0 && i2c.dev_addr != 0xA2) { 42424c7070dbSScott Long err = EINVAL; 42434c7070dbSScott Long break; 42444c7070dbSScott Long } 42454c7070dbSScott Long if (i2c.len > sizeof(i2c.data)) { 42464c7070dbSScott Long err = EINVAL; 42474c7070dbSScott Long break; 42484c7070dbSScott Long } 42494c7070dbSScott Long 42504c7070dbSScott Long if ((err = IFDI_I2C_REQ(ctx, &i2c)) == 0) 4251541d96aaSBrooks Davis err = copyout(&i2c, ifr_data_get_ptr(ifr), 4252541d96aaSBrooks Davis sizeof(i2c)); 42534c7070dbSScott Long break; 42544c7070dbSScott Long } 42554c7070dbSScott Long case SIOCSIFCAP: 42564c7070dbSScott Long { 42570c919c23SStephen Hurd int mask, setmask, oldmask; 42584c7070dbSScott Long 42590c919c23SStephen Hurd oldmask = if_getcapenable(ifp); 42600c919c23SStephen Hurd mask = ifr->ifr_reqcap ^ oldmask; 42616554362cSAndrew Gallatin mask &= ctx->ifc_softc_ctx.isc_capabilities | IFCAP_NOMAP; 42624c7070dbSScott Long setmask = 0; 42634c7070dbSScott Long #ifdef TCP_OFFLOAD 42644c7070dbSScott Long setmask |= mask & (IFCAP_TOE4|IFCAP_TOE6); 42654c7070dbSScott Long #endif 42664c7070dbSScott Long setmask |= (mask & IFCAP_FLAGS); 42670c919c23SStephen Hurd setmask |= (mask & IFCAP_WOL); 42684c7070dbSScott Long 42690c919c23SStephen Hurd /* 4270a42546dfSStephen Hurd * If any RX csum has changed, change all the ones that 4271a42546dfSStephen Hurd * are supported by the driver. 42720c919c23SStephen Hurd */ 4273a42546dfSStephen Hurd if (setmask & (IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6)) { 4274a42546dfSStephen Hurd setmask |= ctx->ifc_softc_ctx.isc_capabilities & 4275a42546dfSStephen Hurd (IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6); 4276a42546dfSStephen Hurd } 42770c919c23SStephen Hurd 42784c7070dbSScott Long /* 42794c7070dbSScott Long * want to ensure that traffic has stopped before we change any of the flags 42804c7070dbSScott Long */ 42814c7070dbSScott Long if (setmask) { 42824c7070dbSScott Long CTX_LOCK(ctx); 42834c7070dbSScott Long bits = if_getdrvflags(ifp); 42840c919c23SStephen Hurd if (bits & IFF_DRV_RUNNING && setmask & ~IFCAP_WOL) 42854c7070dbSScott Long iflib_stop(ctx); 42867b610b60SSean Bruno STATE_LOCK(ctx); 42874c7070dbSScott Long if_togglecapenable(ifp, setmask); 42887b610b60SSean Bruno STATE_UNLOCK(ctx); 42890c919c23SStephen Hurd if (bits & IFF_DRV_RUNNING && setmask & ~IFCAP_WOL) 42904c7070dbSScott Long iflib_init_locked(ctx); 42917b610b60SSean Bruno STATE_LOCK(ctx); 42924c7070dbSScott Long if_setdrvflags(ifp, bits); 42937b610b60SSean Bruno STATE_UNLOCK(ctx); 42944c7070dbSScott Long CTX_UNLOCK(ctx); 42954c7070dbSScott Long } 42960c919c23SStephen Hurd if_vlancap(ifp); 42974c7070dbSScott Long break; 42984c7070dbSScott Long } 42994c7070dbSScott Long case SIOCGPRIVATE_0: 43004c7070dbSScott Long case SIOCSDRVSPEC: 43014c7070dbSScott Long case SIOCGDRVSPEC: 43024c7070dbSScott Long CTX_LOCK(ctx); 43034c7070dbSScott Long err = IFDI_PRIV_IOCTL(ctx, command, data); 43044c7070dbSScott Long CTX_UNLOCK(ctx); 43054c7070dbSScott Long break; 43064c7070dbSScott Long default: 43074c7070dbSScott Long err = ether_ioctl(ifp, command, data); 43084c7070dbSScott Long break; 43094c7070dbSScott Long } 43104c7070dbSScott Long if (reinit) 43114c7070dbSScott Long iflib_if_init(ctx); 43124c7070dbSScott Long return (err); 43134c7070dbSScott Long } 43144c7070dbSScott Long 43154c7070dbSScott Long static uint64_t 43164c7070dbSScott Long iflib_if_get_counter(if_t ifp, ift_counter cnt) 43174c7070dbSScott Long { 43184c7070dbSScott Long if_ctx_t ctx = if_getsoftc(ifp); 43194c7070dbSScott Long 43204c7070dbSScott Long return (IFDI_GET_COUNTER(ctx, cnt)); 43214c7070dbSScott Long } 43224c7070dbSScott Long 43234c7070dbSScott Long /********************************************************************* 43244c7070dbSScott Long * 43254c7070dbSScott Long * OTHER FUNCTIONS EXPORTED TO THE STACK 43264c7070dbSScott Long * 43274c7070dbSScott Long **********************************************************************/ 43284c7070dbSScott Long 43294c7070dbSScott Long static void 43304c7070dbSScott Long iflib_vlan_register(void *arg, if_t ifp, uint16_t vtag) 43314c7070dbSScott Long { 43324c7070dbSScott Long if_ctx_t ctx = if_getsoftc(ifp); 43334c7070dbSScott Long 43344c7070dbSScott Long if ((void *)ctx != arg) 43354c7070dbSScott Long return; 43364c7070dbSScott Long 43374c7070dbSScott Long if ((vtag == 0) || (vtag > 4095)) 43384c7070dbSScott Long return; 43394c7070dbSScott Long 434053b5b9b0SEric Joyner if (iflib_in_detach(ctx)) 434153b5b9b0SEric Joyner return; 434253b5b9b0SEric Joyner 43434c7070dbSScott Long CTX_LOCK(ctx); 434445818bf1SEric Joyner /* Driver may need all untagged packets to be flushed */ 434545818bf1SEric Joyner if (IFDI_NEEDS_RESTART(ctx, IFLIB_RESTART_VLAN_CONFIG)) 434645818bf1SEric Joyner iflib_stop(ctx); 43474c7070dbSScott Long IFDI_VLAN_REGISTER(ctx, vtag); 434845818bf1SEric Joyner /* Re-init to load the changes, if required */ 434945818bf1SEric Joyner if (IFDI_NEEDS_RESTART(ctx, IFLIB_RESTART_VLAN_CONFIG)) 435045818bf1SEric Joyner iflib_init_locked(ctx); 43514c7070dbSScott Long CTX_UNLOCK(ctx); 43524c7070dbSScott Long } 43534c7070dbSScott Long 43544c7070dbSScott Long static void 43554c7070dbSScott Long iflib_vlan_unregister(void *arg, if_t ifp, uint16_t vtag) 43564c7070dbSScott Long { 43574c7070dbSScott Long if_ctx_t ctx = if_getsoftc(ifp); 43584c7070dbSScott Long 43594c7070dbSScott Long if ((void *)ctx != arg) 43604c7070dbSScott Long return; 43614c7070dbSScott Long 43624c7070dbSScott Long if ((vtag == 0) || (vtag > 4095)) 43634c7070dbSScott Long return; 43644c7070dbSScott Long 43654c7070dbSScott Long CTX_LOCK(ctx); 436645818bf1SEric Joyner /* Driver may need all tagged packets to be flushed */ 436745818bf1SEric Joyner if (IFDI_NEEDS_RESTART(ctx, IFLIB_RESTART_VLAN_CONFIG)) 436845818bf1SEric Joyner iflib_stop(ctx); 43694c7070dbSScott Long IFDI_VLAN_UNREGISTER(ctx, vtag); 437045818bf1SEric Joyner /* Re-init to load the changes, if required */ 437145818bf1SEric Joyner if (IFDI_NEEDS_RESTART(ctx, IFLIB_RESTART_VLAN_CONFIG)) 437245818bf1SEric Joyner iflib_init_locked(ctx); 43734c7070dbSScott Long CTX_UNLOCK(ctx); 43744c7070dbSScott Long } 43754c7070dbSScott Long 43764c7070dbSScott Long static void 43774c7070dbSScott Long iflib_led_func(void *arg, int onoff) 43784c7070dbSScott Long { 43794c7070dbSScott Long if_ctx_t ctx = arg; 43804c7070dbSScott Long 43814c7070dbSScott Long CTX_LOCK(ctx); 43824c7070dbSScott Long IFDI_LED_FUNC(ctx, onoff); 43834c7070dbSScott Long CTX_UNLOCK(ctx); 43844c7070dbSScott Long } 43854c7070dbSScott Long 43864c7070dbSScott Long /********************************************************************* 43874c7070dbSScott Long * 43884c7070dbSScott Long * BUS FUNCTION DEFINITIONS 43894c7070dbSScott Long * 43904c7070dbSScott Long **********************************************************************/ 43914c7070dbSScott Long 43924c7070dbSScott Long int 43934c7070dbSScott Long iflib_device_probe(device_t dev) 43944c7070dbSScott Long { 4395d49e83eaSMarius Strobl const pci_vendor_info_t *ent; 43964c7070dbSScott Long if_shared_ctx_t sctx; 4397d49e83eaSMarius Strobl uint16_t pci_device_id, pci_rev_id, pci_subdevice_id, pci_subvendor_id; 4398d49e83eaSMarius Strobl uint16_t pci_vendor_id; 43994c7070dbSScott Long 44004c7070dbSScott Long if ((sctx = DEVICE_REGISTER(dev)) == NULL || sctx->isc_magic != IFLIB_MAGIC) 44014c7070dbSScott Long return (ENOTSUP); 44024c7070dbSScott Long 44034c7070dbSScott Long pci_vendor_id = pci_get_vendor(dev); 44044c7070dbSScott Long pci_device_id = pci_get_device(dev); 44054c7070dbSScott Long pci_subvendor_id = pci_get_subvendor(dev); 44064c7070dbSScott Long pci_subdevice_id = pci_get_subdevice(dev); 44074c7070dbSScott Long pci_rev_id = pci_get_revid(dev); 44084c7070dbSScott Long if (sctx->isc_parse_devinfo != NULL) 44094c7070dbSScott Long sctx->isc_parse_devinfo(&pci_device_id, &pci_subvendor_id, &pci_subdevice_id, &pci_rev_id); 44104c7070dbSScott Long 44114c7070dbSScott Long ent = sctx->isc_vendor_info; 44124c7070dbSScott Long while (ent->pvi_vendor_id != 0) { 44134c7070dbSScott Long if (pci_vendor_id != ent->pvi_vendor_id) { 44144c7070dbSScott Long ent++; 44154c7070dbSScott Long continue; 44164c7070dbSScott Long } 44174c7070dbSScott Long if ((pci_device_id == ent->pvi_device_id) && 44184c7070dbSScott Long ((pci_subvendor_id == ent->pvi_subvendor_id) || 44194c7070dbSScott Long (ent->pvi_subvendor_id == 0)) && 44204c7070dbSScott Long ((pci_subdevice_id == ent->pvi_subdevice_id) || 44214c7070dbSScott Long (ent->pvi_subdevice_id == 0)) && 44224c7070dbSScott Long ((pci_rev_id == ent->pvi_rev_id) || 44234c7070dbSScott Long (ent->pvi_rev_id == 0))) { 44244c7070dbSScott Long device_set_desc_copy(dev, ent->pvi_name); 44254c7070dbSScott Long /* this needs to be changed to zero if the bus probing code 44264c7070dbSScott Long * ever stops re-probing on best match because the sctx 44274c7070dbSScott Long * may have its values over written by register calls 44284c7070dbSScott Long * in subsequent probes 44294c7070dbSScott Long */ 44304c7070dbSScott Long return (BUS_PROBE_DEFAULT); 44314c7070dbSScott Long } 44324c7070dbSScott Long ent++; 44334c7070dbSScott Long } 44344c7070dbSScott Long return (ENXIO); 44354c7070dbSScott Long } 44364c7070dbSScott Long 4437668d6dbbSEric Joyner int 4438668d6dbbSEric Joyner iflib_device_probe_vendor(device_t dev) 4439668d6dbbSEric Joyner { 4440668d6dbbSEric Joyner int probe; 4441668d6dbbSEric Joyner 4442668d6dbbSEric Joyner probe = iflib_device_probe(dev); 4443668d6dbbSEric Joyner if (probe == BUS_PROBE_DEFAULT) 4444668d6dbbSEric Joyner return (BUS_PROBE_VENDOR); 4445668d6dbbSEric Joyner else 4446668d6dbbSEric Joyner return (probe); 4447668d6dbbSEric Joyner } 4448668d6dbbSEric Joyner 444909f6ff4fSMatt Macy static void 445009f6ff4fSMatt Macy iflib_reset_qvalues(if_ctx_t ctx) 44514c7070dbSScott Long { 445209f6ff4fSMatt Macy if_softc_ctx_t scctx = &ctx->ifc_softc_ctx; 445309f6ff4fSMatt Macy if_shared_ctx_t sctx = ctx->ifc_sctx; 445409f6ff4fSMatt Macy device_t dev = ctx->ifc_dev; 445546d0f824SMatt Macy int i; 44564c7070dbSScott Long 445723ac9029SStephen Hurd if (ctx->ifc_sysctl_ntxqs != 0) 445823ac9029SStephen Hurd scctx->isc_ntxqsets = ctx->ifc_sysctl_ntxqs; 445923ac9029SStephen Hurd if (ctx->ifc_sysctl_nrxqs != 0) 446023ac9029SStephen Hurd scctx->isc_nrxqsets = ctx->ifc_sysctl_nrxqs; 446123ac9029SStephen Hurd 446223ac9029SStephen Hurd for (i = 0; i < sctx->isc_ntxqs; i++) { 446323ac9029SStephen Hurd if (ctx->ifc_sysctl_ntxds[i] != 0) 446423ac9029SStephen Hurd scctx->isc_ntxd[i] = ctx->ifc_sysctl_ntxds[i]; 446523ac9029SStephen Hurd else 446623ac9029SStephen Hurd scctx->isc_ntxd[i] = sctx->isc_ntxd_default[i]; 446723ac9029SStephen Hurd } 446823ac9029SStephen Hurd 446923ac9029SStephen Hurd for (i = 0; i < sctx->isc_nrxqs; i++) { 447023ac9029SStephen Hurd if (ctx->ifc_sysctl_nrxds[i] != 0) 447123ac9029SStephen Hurd scctx->isc_nrxd[i] = ctx->ifc_sysctl_nrxds[i]; 447223ac9029SStephen Hurd else 447323ac9029SStephen Hurd scctx->isc_nrxd[i] = sctx->isc_nrxd_default[i]; 447423ac9029SStephen Hurd } 447523ac9029SStephen Hurd 447623ac9029SStephen Hurd for (i = 0; i < sctx->isc_nrxqs; i++) { 447723ac9029SStephen Hurd if (scctx->isc_nrxd[i] < sctx->isc_nrxd_min[i]) { 447823ac9029SStephen Hurd device_printf(dev, "nrxd%d: %d less than nrxd_min %d - resetting to min\n", 447923ac9029SStephen Hurd i, scctx->isc_nrxd[i], sctx->isc_nrxd_min[i]); 448023ac9029SStephen Hurd scctx->isc_nrxd[i] = sctx->isc_nrxd_min[i]; 448123ac9029SStephen Hurd } 448223ac9029SStephen Hurd if (scctx->isc_nrxd[i] > sctx->isc_nrxd_max[i]) { 448323ac9029SStephen Hurd device_printf(dev, "nrxd%d: %d greater than nrxd_max %d - resetting to max\n", 448423ac9029SStephen Hurd i, scctx->isc_nrxd[i], sctx->isc_nrxd_max[i]); 448523ac9029SStephen Hurd scctx->isc_nrxd[i] = sctx->isc_nrxd_max[i]; 448623ac9029SStephen Hurd } 4487afb77372SEric Joyner if (!powerof2(scctx->isc_nrxd[i])) { 4488afb77372SEric Joyner device_printf(dev, "nrxd%d: %d is not a power of 2 - using default value of %d\n", 4489afb77372SEric Joyner i, scctx->isc_nrxd[i], sctx->isc_nrxd_default[i]); 4490afb77372SEric Joyner scctx->isc_nrxd[i] = sctx->isc_nrxd_default[i]; 4491afb77372SEric Joyner } 449223ac9029SStephen Hurd } 449323ac9029SStephen Hurd 449423ac9029SStephen Hurd for (i = 0; i < sctx->isc_ntxqs; i++) { 449523ac9029SStephen Hurd if (scctx->isc_ntxd[i] < sctx->isc_ntxd_min[i]) { 449623ac9029SStephen Hurd device_printf(dev, "ntxd%d: %d less than ntxd_min %d - resetting to min\n", 449723ac9029SStephen Hurd i, scctx->isc_ntxd[i], sctx->isc_ntxd_min[i]); 449823ac9029SStephen Hurd scctx->isc_ntxd[i] = sctx->isc_ntxd_min[i]; 449923ac9029SStephen Hurd } 450023ac9029SStephen Hurd if (scctx->isc_ntxd[i] > sctx->isc_ntxd_max[i]) { 450123ac9029SStephen Hurd device_printf(dev, "ntxd%d: %d greater than ntxd_max %d - resetting to max\n", 450223ac9029SStephen Hurd i, scctx->isc_ntxd[i], sctx->isc_ntxd_max[i]); 450323ac9029SStephen Hurd scctx->isc_ntxd[i] = sctx->isc_ntxd_max[i]; 450423ac9029SStephen Hurd } 4505afb77372SEric Joyner if (!powerof2(scctx->isc_ntxd[i])) { 4506afb77372SEric Joyner device_printf(dev, "ntxd%d: %d is not a power of 2 - using default value of %d\n", 4507afb77372SEric Joyner i, scctx->isc_ntxd[i], sctx->isc_ntxd_default[i]); 4508afb77372SEric Joyner scctx->isc_ntxd[i] = sctx->isc_ntxd_default[i]; 4509afb77372SEric Joyner } 451023ac9029SStephen Hurd } 451109f6ff4fSMatt Macy } 4512ab2e3f79SStephen Hurd 45136d49b41eSAndrew Gallatin static void 45146d49b41eSAndrew Gallatin iflib_add_pfil(if_ctx_t ctx) 45156d49b41eSAndrew Gallatin { 45166d49b41eSAndrew Gallatin struct pfil_head *pfil; 45176d49b41eSAndrew Gallatin struct pfil_head_args pa; 45186d49b41eSAndrew Gallatin iflib_rxq_t rxq; 45196d49b41eSAndrew Gallatin int i; 45206d49b41eSAndrew Gallatin 45216d49b41eSAndrew Gallatin pa.pa_version = PFIL_VERSION; 45226d49b41eSAndrew Gallatin pa.pa_flags = PFIL_IN; 45236d49b41eSAndrew Gallatin pa.pa_type = PFIL_TYPE_ETHERNET; 45246d49b41eSAndrew Gallatin pa.pa_headname = ctx->ifc_ifp->if_xname; 45256d49b41eSAndrew Gallatin pfil = pfil_head_register(&pa); 45266d49b41eSAndrew Gallatin 45276d49b41eSAndrew Gallatin for (i = 0, rxq = ctx->ifc_rxqs; i < NRXQSETS(ctx); i++, rxq++) { 45286d49b41eSAndrew Gallatin rxq->pfil = pfil; 45296d49b41eSAndrew Gallatin } 45306d49b41eSAndrew Gallatin } 45316d49b41eSAndrew Gallatin 45326d49b41eSAndrew Gallatin static void 45336d49b41eSAndrew Gallatin iflib_rem_pfil(if_ctx_t ctx) 45346d49b41eSAndrew Gallatin { 45356d49b41eSAndrew Gallatin struct pfil_head *pfil; 45366d49b41eSAndrew Gallatin iflib_rxq_t rxq; 45376d49b41eSAndrew Gallatin int i; 45386d49b41eSAndrew Gallatin 45396d49b41eSAndrew Gallatin rxq = ctx->ifc_rxqs; 45406d49b41eSAndrew Gallatin pfil = rxq->pfil; 45416d49b41eSAndrew Gallatin for (i = 0; i < NRXQSETS(ctx); i++, rxq++) { 45426d49b41eSAndrew Gallatin rxq->pfil = NULL; 45436d49b41eSAndrew Gallatin } 45446d49b41eSAndrew Gallatin pfil_head_unregister(pfil); 45456d49b41eSAndrew Gallatin } 45466d49b41eSAndrew Gallatin 4547f154ece0SStephen Hurd static uint16_t 4548f154ece0SStephen Hurd get_ctx_core_offset(if_ctx_t ctx) 4549f154ece0SStephen Hurd { 4550f154ece0SStephen Hurd if_softc_ctx_t scctx = &ctx->ifc_softc_ctx; 4551f154ece0SStephen Hurd struct cpu_offset *op; 4552f154ece0SStephen Hurd uint16_t qc; 4553f154ece0SStephen Hurd uint16_t ret = ctx->ifc_sysctl_core_offset; 4554f154ece0SStephen Hurd 4555f154ece0SStephen Hurd if (ret != CORE_OFFSET_UNSPECIFIED) 4556f154ece0SStephen Hurd return (ret); 4557f154ece0SStephen Hurd 4558f154ece0SStephen Hurd if (ctx->ifc_sysctl_separate_txrx) 4559f154ece0SStephen Hurd qc = scctx->isc_ntxqsets + scctx->isc_nrxqsets; 4560f154ece0SStephen Hurd else 4561f154ece0SStephen Hurd qc = max(scctx->isc_ntxqsets, scctx->isc_nrxqsets); 4562f154ece0SStephen Hurd 4563f154ece0SStephen Hurd mtx_lock(&cpu_offset_mtx); 4564f154ece0SStephen Hurd SLIST_FOREACH(op, &cpu_offsets, entries) { 4565f154ece0SStephen Hurd if (CPU_CMP(&ctx->ifc_cpus, &op->set) == 0) { 4566f154ece0SStephen Hurd ret = op->offset; 4567f154ece0SStephen Hurd op->offset += qc; 4568f154ece0SStephen Hurd MPASS(op->refcount < UINT_MAX); 4569f154ece0SStephen Hurd op->refcount++; 4570f154ece0SStephen Hurd break; 4571f154ece0SStephen Hurd } 4572f154ece0SStephen Hurd } 4573f154ece0SStephen Hurd if (ret == CORE_OFFSET_UNSPECIFIED) { 4574f154ece0SStephen Hurd ret = 0; 4575f154ece0SStephen Hurd op = malloc(sizeof(struct cpu_offset), M_IFLIB, 4576f154ece0SStephen Hurd M_NOWAIT | M_ZERO); 4577f154ece0SStephen Hurd if (op == NULL) { 4578f154ece0SStephen Hurd device_printf(ctx->ifc_dev, 4579f154ece0SStephen Hurd "allocation for cpu offset failed.\n"); 4580f154ece0SStephen Hurd } else { 4581f154ece0SStephen Hurd op->offset = qc; 4582f154ece0SStephen Hurd op->refcount = 1; 4583f154ece0SStephen Hurd CPU_COPY(&ctx->ifc_cpus, &op->set); 4584f154ece0SStephen Hurd SLIST_INSERT_HEAD(&cpu_offsets, op, entries); 4585f154ece0SStephen Hurd } 4586f154ece0SStephen Hurd } 4587f154ece0SStephen Hurd mtx_unlock(&cpu_offset_mtx); 4588f154ece0SStephen Hurd 4589f154ece0SStephen Hurd return (ret); 4590f154ece0SStephen Hurd } 4591f154ece0SStephen Hurd 4592f154ece0SStephen Hurd static void 4593f154ece0SStephen Hurd unref_ctx_core_offset(if_ctx_t ctx) 4594f154ece0SStephen Hurd { 4595f154ece0SStephen Hurd struct cpu_offset *op, *top; 4596f154ece0SStephen Hurd 4597f154ece0SStephen Hurd mtx_lock(&cpu_offset_mtx); 4598f154ece0SStephen Hurd SLIST_FOREACH_SAFE(op, &cpu_offsets, entries, top) { 4599f154ece0SStephen Hurd if (CPU_CMP(&ctx->ifc_cpus, &op->set) == 0) { 4600f154ece0SStephen Hurd MPASS(op->refcount > 0); 4601f154ece0SStephen Hurd op->refcount--; 4602f154ece0SStephen Hurd if (op->refcount == 0) { 4603f154ece0SStephen Hurd SLIST_REMOVE(&cpu_offsets, op, cpu_offset, entries); 4604f154ece0SStephen Hurd free(op, M_IFLIB); 4605f154ece0SStephen Hurd } 4606f154ece0SStephen Hurd break; 4607f154ece0SStephen Hurd } 4608f154ece0SStephen Hurd } 4609f154ece0SStephen Hurd mtx_unlock(&cpu_offset_mtx); 4610f154ece0SStephen Hurd } 4611f154ece0SStephen Hurd 461209f6ff4fSMatt Macy int 461309f6ff4fSMatt Macy iflib_device_register(device_t dev, void *sc, if_shared_ctx_t sctx, if_ctx_t *ctxp) 461409f6ff4fSMatt Macy { 461509f6ff4fSMatt Macy if_ctx_t ctx; 461609f6ff4fSMatt Macy if_t ifp; 461709f6ff4fSMatt Macy if_softc_ctx_t scctx; 46183d10e9edSMarius Strobl kobjop_desc_t kobj_desc; 46193d10e9edSMarius Strobl kobj_method_t *kobj_method; 4620afb77372SEric Joyner int err, msix, rid; 4621ac11d857SVincenzo Maffione int num_txd, num_rxd; 462209f6ff4fSMatt Macy 462309f6ff4fSMatt Macy ctx = malloc(sizeof(* ctx), M_IFLIB, M_WAITOK|M_ZERO); 462409f6ff4fSMatt Macy 462509f6ff4fSMatt Macy if (sc == NULL) { 462609f6ff4fSMatt Macy sc = malloc(sctx->isc_driver->size, M_IFLIB, M_WAITOK|M_ZERO); 462709f6ff4fSMatt Macy device_set_softc(dev, ctx); 462809f6ff4fSMatt Macy ctx->ifc_flags |= IFC_SC_ALLOCATED; 462909f6ff4fSMatt Macy } 463009f6ff4fSMatt Macy 463109f6ff4fSMatt Macy ctx->ifc_sctx = sctx; 463209f6ff4fSMatt Macy ctx->ifc_dev = dev; 463309f6ff4fSMatt Macy ctx->ifc_softc = sc; 463409f6ff4fSMatt Macy 463509f6ff4fSMatt Macy if ((err = iflib_register(ctx)) != 0) { 463609f6ff4fSMatt Macy device_printf(dev, "iflib_register failed %d\n", err); 46377f3eb9daSPatrick Kelsey goto fail_ctx_free; 463809f6ff4fSMatt Macy } 463909f6ff4fSMatt Macy iflib_add_device_sysctl_pre(ctx); 464009f6ff4fSMatt Macy 464109f6ff4fSMatt Macy scctx = &ctx->ifc_softc_ctx; 464209f6ff4fSMatt Macy ifp = ctx->ifc_ifp; 464309f6ff4fSMatt Macy 464409f6ff4fSMatt Macy iflib_reset_qvalues(ctx); 4645aa8a24d3SStephen Hurd CTX_LOCK(ctx); 4646ab2e3f79SStephen Hurd if ((err = IFDI_ATTACH_PRE(ctx)) != 0) { 46474c7070dbSScott Long device_printf(dev, "IFDI_ATTACH_PRE failed %d\n", err); 46487f3eb9daSPatrick Kelsey goto fail_unlock; 46494c7070dbSScott Long } 46501248952aSSean Bruno _iflib_pre_assert(scctx); 46511248952aSSean Bruno ctx->ifc_txrx = *scctx->isc_txrx; 46521248952aSSean Bruno 4653e2621d96SMatt Macy if (sctx->isc_flags & IFLIB_DRIVER_MEDIA) 4654e2621d96SMatt Macy ctx->ifc_mediap = scctx->isc_media; 4655e2621d96SMatt Macy 46561248952aSSean Bruno #ifdef INVARIANTS 46577f87c040SMarius Strobl if (scctx->isc_capabilities & IFCAP_TXCSUM) 46581248952aSSean Bruno MPASS(scctx->isc_tx_csum_flags); 46591248952aSSean Bruno #endif 46601248952aSSean Bruno 46616554362cSAndrew Gallatin if_setcapabilities(ifp, 46626554362cSAndrew Gallatin scctx->isc_capabilities | IFCAP_HWSTATS | IFCAP_NOMAP); 46636554362cSAndrew Gallatin if_setcapenable(ifp, 46646554362cSAndrew Gallatin scctx->isc_capenable | IFCAP_HWSTATS | IFCAP_NOMAP); 46651248952aSSean Bruno 46661248952aSSean Bruno if (scctx->isc_ntxqsets == 0 || (scctx->isc_ntxqsets_max && scctx->isc_ntxqsets_max < scctx->isc_ntxqsets)) 46671248952aSSean Bruno scctx->isc_ntxqsets = scctx->isc_ntxqsets_max; 46681248952aSSean Bruno if (scctx->isc_nrxqsets == 0 || (scctx->isc_nrxqsets_max && scctx->isc_nrxqsets_max < scctx->isc_nrxqsets)) 46691248952aSSean Bruno scctx->isc_nrxqsets = scctx->isc_nrxqsets_max; 467023ac9029SStephen Hurd 4671ac11d857SVincenzo Maffione num_txd = iflib_num_tx_descs(ctx); 4672ac11d857SVincenzo Maffione num_rxd = iflib_num_rx_descs(ctx); 467323ac9029SStephen Hurd 467423ac9029SStephen Hurd /* XXX change for per-queue sizes */ 46751722eeacSMarius Strobl device_printf(dev, "Using %d TX descriptors and %d RX descriptors\n", 4676ac11d857SVincenzo Maffione num_txd, num_rxd); 467723ac9029SStephen Hurd 4678ac11d857SVincenzo Maffione if (scctx->isc_tx_nsegments > num_txd / MAX_SINGLE_PACKET_FRACTION) 4679ac11d857SVincenzo Maffione scctx->isc_tx_nsegments = max(1, num_txd / 468023ac9029SStephen Hurd MAX_SINGLE_PACKET_FRACTION); 4681ac11d857SVincenzo Maffione if (scctx->isc_tx_tso_segments_max > num_txd / 468223ac9029SStephen Hurd MAX_SINGLE_PACKET_FRACTION) 468323ac9029SStephen Hurd scctx->isc_tx_tso_segments_max = max(1, 4684ac11d857SVincenzo Maffione num_txd / MAX_SINGLE_PACKET_FRACTION); 46854c7070dbSScott Long 46864c7070dbSScott Long /* TSO parameters - dig these out of the data sheet - simply correspond to tag setup */ 46877f87c040SMarius Strobl if (if_getcapabilities(ifp) & IFCAP_TSO) { 46887f87c040SMarius Strobl /* 46897f87c040SMarius Strobl * The stack can't handle a TSO size larger than IP_MAXPACKET, 46907f87c040SMarius Strobl * but some MACs do. 46917f87c040SMarius Strobl */ 46927f87c040SMarius Strobl if_sethwtsomax(ifp, min(scctx->isc_tx_tso_size_max, 46937f87c040SMarius Strobl IP_MAXPACKET)); 46947f87c040SMarius Strobl /* 46957f87c040SMarius Strobl * Take maximum number of m_pullup(9)'s in iflib_parse_header() 46967f87c040SMarius Strobl * into account. In the worst case, each of these calls will 46977f87c040SMarius Strobl * add another mbuf and, thus, the requirement for another DMA 46987f87c040SMarius Strobl * segment. So for best performance, it doesn't make sense to 46997f87c040SMarius Strobl * advertize a maximum of TSO segments that typically will 47007f87c040SMarius Strobl * require defragmentation in iflib_encap(). 47017f87c040SMarius Strobl */ 47027f87c040SMarius Strobl if_sethwtsomaxsegcount(ifp, scctx->isc_tx_tso_segments_max - 3); 47037f87c040SMarius Strobl if_sethwtsomaxsegsize(ifp, scctx->isc_tx_tso_segsize_max); 47047f87c040SMarius Strobl } 47054c7070dbSScott Long if (scctx->isc_rss_table_size == 0) 47064c7070dbSScott Long scctx->isc_rss_table_size = 64; 470723ac9029SStephen Hurd scctx->isc_rss_table_mask = scctx->isc_rss_table_size-1; 4708da69b8f9SSean Bruno 4709da69b8f9SSean Bruno GROUPTASK_INIT(&ctx->ifc_admin_task, 0, _task_fn_admin, ctx); 4710da69b8f9SSean Bruno /* XXX format name */ 4711f855ec81SMarius Strobl taskqgroup_attach(qgroup_if_config_tqg, &ctx->ifc_admin_task, ctx, 4712f855ec81SMarius Strobl NULL, NULL, "admin"); 4713e516b535SStephen Hurd 4714772593dbSStephen Hurd /* Set up cpu set. If it fails, use the set of all CPUs. */ 4715e516b535SStephen Hurd if (bus_get_cpus(dev, INTR_CPUS, sizeof(ctx->ifc_cpus), &ctx->ifc_cpus) != 0) { 4716e516b535SStephen Hurd device_printf(dev, "Unable to fetch CPU list\n"); 4717e516b535SStephen Hurd CPU_COPY(&all_cpus, &ctx->ifc_cpus); 4718e516b535SStephen Hurd } 4719e516b535SStephen Hurd MPASS(CPU_COUNT(&ctx->ifc_cpus) > 0); 4720e516b535SStephen Hurd 47214c7070dbSScott Long /* 4722b97de13aSMarius Strobl ** Now set up MSI or MSI-X, should return us the number of supported 4723b97de13aSMarius Strobl ** vectors (will be 1 for a legacy interrupt and MSI). 47244c7070dbSScott Long */ 47254c7070dbSScott Long if (sctx->isc_flags & IFLIB_SKIP_MSIX) { 47264c7070dbSScott Long msix = scctx->isc_vectors; 47274c7070dbSScott Long } else if (scctx->isc_msix_bar != 0) 4728f7ae9a84SSean Bruno /* 4729f7ae9a84SSean Bruno * The simple fact that isc_msix_bar is not 0 does not mean we 4730f7ae9a84SSean Bruno * we have a good value there that is known to work. 4731f7ae9a84SSean Bruno */ 47324c7070dbSScott Long msix = iflib_msix_init(ctx); 47334c7070dbSScott Long else { 47344c7070dbSScott Long scctx->isc_vectors = 1; 47354c7070dbSScott Long scctx->isc_ntxqsets = 1; 47364c7070dbSScott Long scctx->isc_nrxqsets = 1; 47374c7070dbSScott Long scctx->isc_intr = IFLIB_INTR_LEGACY; 47384c7070dbSScott Long msix = 0; 47394c7070dbSScott Long } 47404c7070dbSScott Long /* Get memory for the station queues */ 47414c7070dbSScott Long if ((err = iflib_queues_alloc(ctx))) { 47424c7070dbSScott Long device_printf(dev, "Unable to allocate queue memory\n"); 47437f3eb9daSPatrick Kelsey goto fail_intr_free; 47444c7070dbSScott Long } 47454c7070dbSScott Long 4746ac88e6daSStephen Hurd if ((err = iflib_qset_structures_setup(ctx))) 47474c7070dbSScott Long goto fail_queues; 474869b7fc3eSSean Bruno 4749bd84f700SSean Bruno /* 4750f154ece0SStephen Hurd * Now that we know how many queues there are, get the core offset. 4751f154ece0SStephen Hurd */ 4752f154ece0SStephen Hurd ctx->ifc_sysctl_core_offset = get_ctx_core_offset(ctx); 4753f154ece0SStephen Hurd 47543d10e9edSMarius Strobl if (msix > 1) { 47553d10e9edSMarius Strobl /* 47563d10e9edSMarius Strobl * When using MSI-X, ensure that ifdi_{r,t}x_queue_intr_enable 47573d10e9edSMarius Strobl * aren't the default NULL implementation. 47583d10e9edSMarius Strobl */ 47593d10e9edSMarius Strobl kobj_desc = &ifdi_rx_queue_intr_enable_desc; 47603d10e9edSMarius Strobl kobj_method = kobj_lookup_method(((kobj_t)ctx)->ops->cls, NULL, 47613d10e9edSMarius Strobl kobj_desc); 47623d10e9edSMarius Strobl if (kobj_method == &kobj_desc->deflt) { 47633d10e9edSMarius Strobl device_printf(dev, 47643d10e9edSMarius Strobl "MSI-X requires ifdi_rx_queue_intr_enable method"); 47653d10e9edSMarius Strobl err = EOPNOTSUPP; 47667f3eb9daSPatrick Kelsey goto fail_queues; 47674c7070dbSScott Long } 47683d10e9edSMarius Strobl kobj_desc = &ifdi_tx_queue_intr_enable_desc; 47693d10e9edSMarius Strobl kobj_method = kobj_lookup_method(((kobj_t)ctx)->ops->cls, NULL, 47703d10e9edSMarius Strobl kobj_desc); 47713d10e9edSMarius Strobl if (kobj_method == &kobj_desc->deflt) { 47723d10e9edSMarius Strobl device_printf(dev, 47733d10e9edSMarius Strobl "MSI-X requires ifdi_tx_queue_intr_enable method"); 47743d10e9edSMarius Strobl err = EOPNOTSUPP; 47753d10e9edSMarius Strobl goto fail_queues; 47763d10e9edSMarius Strobl } 47773d10e9edSMarius Strobl 47783d10e9edSMarius Strobl /* 47793d10e9edSMarius Strobl * Assign the MSI-X vectors. 47803d10e9edSMarius Strobl * Note that the default NULL ifdi_msix_intr_assign method will 47813d10e9edSMarius Strobl * fail here, too. 47823d10e9edSMarius Strobl */ 47833d10e9edSMarius Strobl err = IFDI_MSIX_INTR_ASSIGN(ctx, msix); 47843d10e9edSMarius Strobl if (err != 0) { 47853d10e9edSMarius Strobl device_printf(dev, "IFDI_MSIX_INTR_ASSIGN failed %d\n", 47863d10e9edSMarius Strobl err); 47873d10e9edSMarius Strobl goto fail_queues; 47883d10e9edSMarius Strobl } 4789197c6798SEric Joyner } else if (scctx->isc_intr != IFLIB_INTR_MSIX) { 47904c7070dbSScott Long rid = 0; 47914c7070dbSScott Long if (scctx->isc_intr == IFLIB_INTR_MSI) { 47924c7070dbSScott Long MPASS(msix == 1); 47934c7070dbSScott Long rid = 1; 47944c7070dbSScott Long } 479523ac9029SStephen Hurd if ((err = iflib_legacy_setup(ctx, ctx->isc_legacy_intr, ctx->ifc_softc, &rid, "irq0")) != 0) { 47964c7070dbSScott Long device_printf(dev, "iflib_legacy_setup failed %d\n", err); 47977f3eb9daSPatrick Kelsey goto fail_queues; 47984c7070dbSScott Long } 4799197c6798SEric Joyner } else { 4800197c6798SEric Joyner device_printf(dev, 4801197c6798SEric Joyner "Cannot use iflib with only 1 MSI-X interrupt!\n"); 4802197c6798SEric Joyner err = ENODEV; 4803197c6798SEric Joyner goto fail_intr_free; 48044c7070dbSScott Long } 48057f87c040SMarius Strobl 48061fd8c72cSKyle Evans ether_ifattach(ctx->ifc_ifp, ctx->ifc_mac.octet); 48077f87c040SMarius Strobl 4808ab2e3f79SStephen Hurd if ((err = IFDI_ATTACH_POST(ctx)) != 0) { 48094c7070dbSScott Long device_printf(dev, "IFDI_ATTACH_POST failed %d\n", err); 48104c7070dbSScott Long goto fail_detach; 48114c7070dbSScott Long } 48127f87c040SMarius Strobl 48137f87c040SMarius Strobl /* 48147f87c040SMarius Strobl * Tell the upper layer(s) if IFCAP_VLAN_MTU is supported. 48157f87c040SMarius Strobl * This must appear after the call to ether_ifattach() because 48167f87c040SMarius Strobl * ether_ifattach() sets if_hdrlen to the default value. 48177f87c040SMarius Strobl */ 48187f87c040SMarius Strobl if (if_getcapabilities(ifp) & IFCAP_VLAN_MTU) 48197f87c040SMarius Strobl if_setifheaderlen(ifp, sizeof(struct ether_vlan_header)); 48207f87c040SMarius Strobl 48214c7070dbSScott Long if ((err = iflib_netmap_attach(ctx))) { 48224c7070dbSScott Long device_printf(ctx->ifc_dev, "netmap attach failed: %d\n", err); 48234c7070dbSScott Long goto fail_detach; 48244c7070dbSScott Long } 48254c7070dbSScott Long *ctxp = ctx; 48264c7070dbSScott Long 48277790c8c1SConrad Meyer DEBUGNET_SET(ctx->ifc_ifp, iflib); 482894618825SMark Johnston 482923ac9029SStephen Hurd if_setgetcounterfn(ctx->ifc_ifp, iflib_if_get_counter); 48304c7070dbSScott Long iflib_add_device_sysctl_post(ctx); 48316d49b41eSAndrew Gallatin iflib_add_pfil(ctx); 48324ecb427aSSean Bruno ctx->ifc_flags |= IFC_INIT_DONE; 4833aa8a24d3SStephen Hurd CTX_UNLOCK(ctx); 48343d10e9edSMarius Strobl 48354c7070dbSScott Long return (0); 483677c1fcecSEric Joyner 48374c7070dbSScott Long fail_detach: 48384c7070dbSScott Long ether_ifdetach(ctx->ifc_ifp); 48394c7070dbSScott Long fail_intr_free: 48407f3eb9daSPatrick Kelsey iflib_free_intr_mem(ctx); 48414c7070dbSScott Long fail_queues: 48426108c013SStephen Hurd iflib_tx_structures_free(ctx); 48436108c013SStephen Hurd iflib_rx_structures_free(ctx); 4844197c6798SEric Joyner taskqgroup_detach(qgroup_if_config_tqg, &ctx->ifc_admin_task); 48454c7070dbSScott Long IFDI_DETACH(ctx); 48467f3eb9daSPatrick Kelsey fail_unlock: 4847aa8a24d3SStephen Hurd CTX_UNLOCK(ctx); 484856614414SEric Joyner iflib_deregister(ctx); 48497f3eb9daSPatrick Kelsey fail_ctx_free: 48507f3f6aadSEric Joyner device_set_softc(ctx->ifc_dev, NULL); 48517f3eb9daSPatrick Kelsey if (ctx->ifc_flags & IFC_SC_ALLOCATED) 48527f3eb9daSPatrick Kelsey free(ctx->ifc_softc, M_IFLIB); 48537f3eb9daSPatrick Kelsey free(ctx, M_IFLIB); 48544c7070dbSScott Long return (err); 48554c7070dbSScott Long } 48564c7070dbSScott Long 48574c7070dbSScott Long int 485809f6ff4fSMatt Macy iflib_pseudo_register(device_t dev, if_shared_ctx_t sctx, if_ctx_t *ctxp, 485909f6ff4fSMatt Macy struct iflib_cloneattach_ctx *clctx) 486009f6ff4fSMatt Macy { 4861ac11d857SVincenzo Maffione int num_txd, num_rxd; 486209f6ff4fSMatt Macy int err; 486309f6ff4fSMatt Macy if_ctx_t ctx; 486409f6ff4fSMatt Macy if_t ifp; 486509f6ff4fSMatt Macy if_softc_ctx_t scctx; 486609f6ff4fSMatt Macy int i; 486709f6ff4fSMatt Macy void *sc; 486809f6ff4fSMatt Macy 486909f6ff4fSMatt Macy ctx = malloc(sizeof(*ctx), M_IFLIB, M_WAITOK|M_ZERO); 487009f6ff4fSMatt Macy sc = malloc(sctx->isc_driver->size, M_IFLIB, M_WAITOK|M_ZERO); 487109f6ff4fSMatt Macy ctx->ifc_flags |= IFC_SC_ALLOCATED; 487209f6ff4fSMatt Macy if (sctx->isc_flags & (IFLIB_PSEUDO|IFLIB_VIRTUAL)) 487309f6ff4fSMatt Macy ctx->ifc_flags |= IFC_PSEUDO; 487409f6ff4fSMatt Macy 487509f6ff4fSMatt Macy ctx->ifc_sctx = sctx; 487609f6ff4fSMatt Macy ctx->ifc_softc = sc; 487709f6ff4fSMatt Macy ctx->ifc_dev = dev; 487809f6ff4fSMatt Macy 487909f6ff4fSMatt Macy if ((err = iflib_register(ctx)) != 0) { 488009f6ff4fSMatt Macy device_printf(dev, "%s: iflib_register failed %d\n", __func__, err); 48817f3eb9daSPatrick Kelsey goto fail_ctx_free; 488209f6ff4fSMatt Macy } 488309f6ff4fSMatt Macy iflib_add_device_sysctl_pre(ctx); 488409f6ff4fSMatt Macy 488509f6ff4fSMatt Macy scctx = &ctx->ifc_softc_ctx; 488609f6ff4fSMatt Macy ifp = ctx->ifc_ifp; 488709f6ff4fSMatt Macy 488809f6ff4fSMatt Macy iflib_reset_qvalues(ctx); 4889aac9c817SEric Joyner CTX_LOCK(ctx); 489009f6ff4fSMatt Macy if ((err = IFDI_ATTACH_PRE(ctx)) != 0) { 489109f6ff4fSMatt Macy device_printf(dev, "IFDI_ATTACH_PRE failed %d\n", err); 4892aac9c817SEric Joyner goto fail_unlock; 489309f6ff4fSMatt Macy } 489409f6ff4fSMatt Macy if (sctx->isc_flags & IFLIB_GEN_MAC) 48951fd8c72cSKyle Evans ether_gen_addr(ifp, &ctx->ifc_mac); 489609f6ff4fSMatt Macy if ((err = IFDI_CLONEATTACH(ctx, clctx->cc_ifc, clctx->cc_name, 489709f6ff4fSMatt Macy clctx->cc_params)) != 0) { 489809f6ff4fSMatt Macy device_printf(dev, "IFDI_CLONEATTACH failed %d\n", err); 48999aeca213SMatt Macy goto fail_unlock; 490009f6ff4fSMatt Macy } 490109f6ff4fSMatt Macy #ifdef INVARIANTS 49027f87c040SMarius Strobl if (scctx->isc_capabilities & IFCAP_TXCSUM) 490309f6ff4fSMatt Macy MPASS(scctx->isc_tx_csum_flags); 490409f6ff4fSMatt Macy #endif 490509f6ff4fSMatt Macy 49067f87c040SMarius Strobl if_setcapabilities(ifp, scctx->isc_capabilities | IFCAP_HWSTATS | IFCAP_LINKSTATE); 490709f6ff4fSMatt Macy if_setcapenable(ifp, scctx->isc_capenable | IFCAP_HWSTATS | IFCAP_LINKSTATE); 490809f6ff4fSMatt Macy 490909f6ff4fSMatt Macy ifp->if_flags |= IFF_NOGROUP; 491009f6ff4fSMatt Macy if (sctx->isc_flags & IFLIB_PSEUDO) { 49119aeca213SMatt Macy ifmedia_add(ctx->ifc_mediap, IFM_ETHER | IFM_AUTO, 0, NULL); 49129aeca213SMatt Macy ifmedia_set(ctx->ifc_mediap, IFM_ETHER | IFM_AUTO); 49139aeca213SMatt Macy if (sctx->isc_flags & IFLIB_PSEUDO_ETHER) { 49141fd8c72cSKyle Evans ether_ifattach(ctx->ifc_ifp, ctx->ifc_mac.octet); 49159aeca213SMatt Macy } else { 49169aeca213SMatt Macy if_attach(ctx->ifc_ifp); 49179aeca213SMatt Macy bpfattach(ctx->ifc_ifp, DLT_NULL, sizeof(u_int32_t)); 49189aeca213SMatt Macy } 491909f6ff4fSMatt Macy 492009f6ff4fSMatt Macy if ((err = IFDI_ATTACH_POST(ctx)) != 0) { 492109f6ff4fSMatt Macy device_printf(dev, "IFDI_ATTACH_POST failed %d\n", err); 492209f6ff4fSMatt Macy goto fail_detach; 492309f6ff4fSMatt Macy } 492409f6ff4fSMatt Macy *ctxp = ctx; 492509f6ff4fSMatt Macy 49267f87c040SMarius Strobl /* 49277f87c040SMarius Strobl * Tell the upper layer(s) if IFCAP_VLAN_MTU is supported. 49287f87c040SMarius Strobl * This must appear after the call to ether_ifattach() because 49297f87c040SMarius Strobl * ether_ifattach() sets if_hdrlen to the default value. 49307f87c040SMarius Strobl */ 49317f87c040SMarius Strobl if (if_getcapabilities(ifp) & IFCAP_VLAN_MTU) 49327f87c040SMarius Strobl if_setifheaderlen(ifp, 49337f87c040SMarius Strobl sizeof(struct ether_vlan_header)); 49347f87c040SMarius Strobl 493509f6ff4fSMatt Macy if_setgetcounterfn(ctx->ifc_ifp, iflib_if_get_counter); 493609f6ff4fSMatt Macy iflib_add_device_sysctl_post(ctx); 493709f6ff4fSMatt Macy ctx->ifc_flags |= IFC_INIT_DONE; 49381f93e931SMatt Macy CTX_UNLOCK(ctx); 493909f6ff4fSMatt Macy return (0); 494009f6ff4fSMatt Macy } 49419aeca213SMatt Macy ifmedia_add(ctx->ifc_mediap, IFM_ETHER | IFM_1000_T | IFM_FDX, 0, NULL); 49429aeca213SMatt Macy ifmedia_add(ctx->ifc_mediap, IFM_ETHER | IFM_AUTO, 0, NULL); 49439aeca213SMatt Macy ifmedia_set(ctx->ifc_mediap, IFM_ETHER | IFM_AUTO); 49449aeca213SMatt Macy 494509f6ff4fSMatt Macy _iflib_pre_assert(scctx); 494609f6ff4fSMatt Macy ctx->ifc_txrx = *scctx->isc_txrx; 494709f6ff4fSMatt Macy 494809f6ff4fSMatt Macy if (scctx->isc_ntxqsets == 0 || (scctx->isc_ntxqsets_max && scctx->isc_ntxqsets_max < scctx->isc_ntxqsets)) 494909f6ff4fSMatt Macy scctx->isc_ntxqsets = scctx->isc_ntxqsets_max; 495009f6ff4fSMatt Macy if (scctx->isc_nrxqsets == 0 || (scctx->isc_nrxqsets_max && scctx->isc_nrxqsets_max < scctx->isc_nrxqsets)) 495109f6ff4fSMatt Macy scctx->isc_nrxqsets = scctx->isc_nrxqsets_max; 495209f6ff4fSMatt Macy 4953ac11d857SVincenzo Maffione num_txd = iflib_num_tx_descs(ctx); 4954ac11d857SVincenzo Maffione num_rxd = iflib_num_rx_descs(ctx); 495509f6ff4fSMatt Macy 495609f6ff4fSMatt Macy /* XXX change for per-queue sizes */ 49571722eeacSMarius Strobl device_printf(dev, "Using %d TX descriptors and %d RX descriptors\n", 4958ac11d857SVincenzo Maffione num_txd, num_rxd); 495909f6ff4fSMatt Macy 4960ac11d857SVincenzo Maffione if (scctx->isc_tx_nsegments > num_txd / MAX_SINGLE_PACKET_FRACTION) 4961ac11d857SVincenzo Maffione scctx->isc_tx_nsegments = max(1, num_txd / 496209f6ff4fSMatt Macy MAX_SINGLE_PACKET_FRACTION); 4963ac11d857SVincenzo Maffione if (scctx->isc_tx_tso_segments_max > num_txd / 496409f6ff4fSMatt Macy MAX_SINGLE_PACKET_FRACTION) 496509f6ff4fSMatt Macy scctx->isc_tx_tso_segments_max = max(1, 4966ac11d857SVincenzo Maffione num_txd / MAX_SINGLE_PACKET_FRACTION); 496709f6ff4fSMatt Macy 496809f6ff4fSMatt Macy /* TSO parameters - dig these out of the data sheet - simply correspond to tag setup */ 49697f87c040SMarius Strobl if (if_getcapabilities(ifp) & IFCAP_TSO) { 49707f87c040SMarius Strobl /* 49717f87c040SMarius Strobl * The stack can't handle a TSO size larger than IP_MAXPACKET, 49727f87c040SMarius Strobl * but some MACs do. 49737f87c040SMarius Strobl */ 49747f87c040SMarius Strobl if_sethwtsomax(ifp, min(scctx->isc_tx_tso_size_max, 49757f87c040SMarius Strobl IP_MAXPACKET)); 49767f87c040SMarius Strobl /* 49777f87c040SMarius Strobl * Take maximum number of m_pullup(9)'s in iflib_parse_header() 49787f87c040SMarius Strobl * into account. In the worst case, each of these calls will 49797f87c040SMarius Strobl * add another mbuf and, thus, the requirement for another DMA 49807f87c040SMarius Strobl * segment. So for best performance, it doesn't make sense to 49817f87c040SMarius Strobl * advertize a maximum of TSO segments that typically will 49827f87c040SMarius Strobl * require defragmentation in iflib_encap(). 49837f87c040SMarius Strobl */ 49847f87c040SMarius Strobl if_sethwtsomaxsegcount(ifp, scctx->isc_tx_tso_segments_max - 3); 49857f87c040SMarius Strobl if_sethwtsomaxsegsize(ifp, scctx->isc_tx_tso_segsize_max); 49867f87c040SMarius Strobl } 498709f6ff4fSMatt Macy if (scctx->isc_rss_table_size == 0) 498809f6ff4fSMatt Macy scctx->isc_rss_table_size = 64; 498909f6ff4fSMatt Macy scctx->isc_rss_table_mask = scctx->isc_rss_table_size-1; 499009f6ff4fSMatt Macy 499109f6ff4fSMatt Macy GROUPTASK_INIT(&ctx->ifc_admin_task, 0, _task_fn_admin, ctx); 499209f6ff4fSMatt Macy /* XXX format name */ 4993f855ec81SMarius Strobl taskqgroup_attach(qgroup_if_config_tqg, &ctx->ifc_admin_task, ctx, 4994f855ec81SMarius Strobl NULL, NULL, "admin"); 499509f6ff4fSMatt Macy 499609f6ff4fSMatt Macy /* XXX --- can support > 1 -- but keep it simple for now */ 499709f6ff4fSMatt Macy scctx->isc_intr = IFLIB_INTR_LEGACY; 499809f6ff4fSMatt Macy 499909f6ff4fSMatt Macy /* Get memory for the station queues */ 500009f6ff4fSMatt Macy if ((err = iflib_queues_alloc(ctx))) { 500109f6ff4fSMatt Macy device_printf(dev, "Unable to allocate queue memory\n"); 50027f3eb9daSPatrick Kelsey goto fail_iflib_detach; 500309f6ff4fSMatt Macy } 500409f6ff4fSMatt Macy 500509f6ff4fSMatt Macy if ((err = iflib_qset_structures_setup(ctx))) { 500609f6ff4fSMatt Macy device_printf(dev, "qset structure setup failed %d\n", err); 500709f6ff4fSMatt Macy goto fail_queues; 500809f6ff4fSMatt Macy } 50097f87c040SMarius Strobl 501009f6ff4fSMatt Macy /* 501109f6ff4fSMatt Macy * XXX What if anything do we want to do about interrupts? 501209f6ff4fSMatt Macy */ 50131fd8c72cSKyle Evans ether_ifattach(ctx->ifc_ifp, ctx->ifc_mac.octet); 501409f6ff4fSMatt Macy if ((err = IFDI_ATTACH_POST(ctx)) != 0) { 501509f6ff4fSMatt Macy device_printf(dev, "IFDI_ATTACH_POST failed %d\n", err); 501609f6ff4fSMatt Macy goto fail_detach; 501709f6ff4fSMatt Macy } 50187f87c040SMarius Strobl 50197f87c040SMarius Strobl /* 50207f87c040SMarius Strobl * Tell the upper layer(s) if IFCAP_VLAN_MTU is supported. 50217f87c040SMarius Strobl * This must appear after the call to ether_ifattach() because 50227f87c040SMarius Strobl * ether_ifattach() sets if_hdrlen to the default value. 50237f87c040SMarius Strobl */ 50247f87c040SMarius Strobl if (if_getcapabilities(ifp) & IFCAP_VLAN_MTU) 50257f87c040SMarius Strobl if_setifheaderlen(ifp, sizeof(struct ether_vlan_header)); 50267f87c040SMarius Strobl 502709f6ff4fSMatt Macy /* XXX handle more than one queue */ 502809f6ff4fSMatt Macy for (i = 0; i < scctx->isc_nrxqsets; i++) 502909f6ff4fSMatt Macy IFDI_RX_CLSET(ctx, 0, i, ctx->ifc_rxqs[i].ifr_fl[0].ifl_sds.ifsd_cl); 503009f6ff4fSMatt Macy 503109f6ff4fSMatt Macy *ctxp = ctx; 503209f6ff4fSMatt Macy 503309f6ff4fSMatt Macy if_setgetcounterfn(ctx->ifc_ifp, iflib_if_get_counter); 503409f6ff4fSMatt Macy iflib_add_device_sysctl_post(ctx); 503509f6ff4fSMatt Macy ctx->ifc_flags |= IFC_INIT_DONE; 5036aac9c817SEric Joyner CTX_UNLOCK(ctx); 50373d10e9edSMarius Strobl 503809f6ff4fSMatt Macy return (0); 503909f6ff4fSMatt Macy fail_detach: 504009f6ff4fSMatt Macy ether_ifdetach(ctx->ifc_ifp); 504109f6ff4fSMatt Macy fail_queues: 504209f6ff4fSMatt Macy iflib_tx_structures_free(ctx); 504309f6ff4fSMatt Macy iflib_rx_structures_free(ctx); 50447f3eb9daSPatrick Kelsey fail_iflib_detach: 504509f6ff4fSMatt Macy IFDI_DETACH(ctx); 5046aac9c817SEric Joyner fail_unlock: 5047aac9c817SEric Joyner CTX_UNLOCK(ctx); 504856614414SEric Joyner iflib_deregister(ctx); 50497f3eb9daSPatrick Kelsey fail_ctx_free: 50507f3eb9daSPatrick Kelsey free(ctx->ifc_softc, M_IFLIB); 50517f3eb9daSPatrick Kelsey free(ctx, M_IFLIB); 505209f6ff4fSMatt Macy return (err); 505309f6ff4fSMatt Macy } 505409f6ff4fSMatt Macy 505509f6ff4fSMatt Macy int 505609f6ff4fSMatt Macy iflib_pseudo_deregister(if_ctx_t ctx) 505709f6ff4fSMatt Macy { 505809f6ff4fSMatt Macy if_t ifp = ctx->ifc_ifp; 50599aeca213SMatt Macy if_shared_ctx_t sctx = ctx->ifc_sctx; 506009f6ff4fSMatt Macy iflib_txq_t txq; 506109f6ff4fSMatt Macy iflib_rxq_t rxq; 506209f6ff4fSMatt Macy int i, j; 506309f6ff4fSMatt Macy struct taskqgroup *tqg; 506409f6ff4fSMatt Macy iflib_fl_t fl; 506509f6ff4fSMatt Macy 50661558015eSEric Joyner /* Unregister VLAN event handlers early */ 50671558015eSEric Joyner iflib_unregister_vlan_handlers(ctx); 50681558015eSEric Joyner 50699aeca213SMatt Macy if ((sctx->isc_flags & IFLIB_PSEUDO) && 50709aeca213SMatt Macy (sctx->isc_flags & IFLIB_PSEUDO_ETHER) == 0) { 50719aeca213SMatt Macy bpfdetach(ifp); 50729aeca213SMatt Macy if_detach(ifp); 50739aeca213SMatt Macy } else { 507409f6ff4fSMatt Macy ether_ifdetach(ifp); 50759aeca213SMatt Macy } 507609f6ff4fSMatt Macy /* XXX drain any dependent tasks */ 507709f6ff4fSMatt Macy tqg = qgroup_if_io_tqg; 507809f6ff4fSMatt Macy for (txq = ctx->ifc_txqs, i = 0; i < NTXQSETS(ctx); i++, txq++) { 507909f6ff4fSMatt Macy callout_drain(&txq->ift_timer); 5080*17cec474SVincenzo Maffione #ifdef DEV_NETMAP 5081*17cec474SVincenzo Maffione callout_drain(&txq->ift_netmap_timer); 5082*17cec474SVincenzo Maffione #endif /* DEV_NETMAP */ 508309f6ff4fSMatt Macy if (txq->ift_task.gt_uniq != NULL) 508409f6ff4fSMatt Macy taskqgroup_detach(tqg, &txq->ift_task); 508509f6ff4fSMatt Macy } 508609f6ff4fSMatt Macy for (i = 0, rxq = ctx->ifc_rxqs; i < NRXQSETS(ctx); i++, rxq++) { 5087fb1a29b4SHans Petter Selasky callout_drain(&rxq->ifr_watchdog); 508809f6ff4fSMatt Macy if (rxq->ifr_task.gt_uniq != NULL) 508909f6ff4fSMatt Macy taskqgroup_detach(tqg, &rxq->ifr_task); 509009f6ff4fSMatt Macy 509109f6ff4fSMatt Macy for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++) 509209f6ff4fSMatt Macy free(fl->ifl_rx_bitmap, M_IFLIB); 509309f6ff4fSMatt Macy } 509409f6ff4fSMatt Macy tqg = qgroup_if_config_tqg; 509509f6ff4fSMatt Macy if (ctx->ifc_admin_task.gt_uniq != NULL) 509609f6ff4fSMatt Macy taskqgroup_detach(tqg, &ctx->ifc_admin_task); 509709f6ff4fSMatt Macy if (ctx->ifc_vflr_task.gt_uniq != NULL) 509809f6ff4fSMatt Macy taskqgroup_detach(tqg, &ctx->ifc_vflr_task); 509909f6ff4fSMatt Macy 510009f6ff4fSMatt Macy iflib_tx_structures_free(ctx); 510109f6ff4fSMatt Macy iflib_rx_structures_free(ctx); 510256614414SEric Joyner 510356614414SEric Joyner iflib_deregister(ctx); 510456614414SEric Joyner 510509f6ff4fSMatt Macy if (ctx->ifc_flags & IFC_SC_ALLOCATED) 510609f6ff4fSMatt Macy free(ctx->ifc_softc, M_IFLIB); 510709f6ff4fSMatt Macy free(ctx, M_IFLIB); 510809f6ff4fSMatt Macy return (0); 510909f6ff4fSMatt Macy } 511009f6ff4fSMatt Macy 511109f6ff4fSMatt Macy int 51124c7070dbSScott Long iflib_device_attach(device_t dev) 51134c7070dbSScott Long { 51144c7070dbSScott Long if_ctx_t ctx; 51154c7070dbSScott Long if_shared_ctx_t sctx; 51164c7070dbSScott Long 51174c7070dbSScott Long if ((sctx = DEVICE_REGISTER(dev)) == NULL || sctx->isc_magic != IFLIB_MAGIC) 51184c7070dbSScott Long return (ENOTSUP); 51194c7070dbSScott Long 51204c7070dbSScott Long pci_enable_busmaster(dev); 51214c7070dbSScott Long 51224c7070dbSScott Long return (iflib_device_register(dev, NULL, sctx, &ctx)); 51234c7070dbSScott Long } 51244c7070dbSScott Long 51254c7070dbSScott Long int 51264c7070dbSScott Long iflib_device_deregister(if_ctx_t ctx) 51274c7070dbSScott Long { 51284c7070dbSScott Long if_t ifp = ctx->ifc_ifp; 51294c7070dbSScott Long iflib_txq_t txq; 51304c7070dbSScott Long iflib_rxq_t rxq; 51314c7070dbSScott Long device_t dev = ctx->ifc_dev; 513287890dbaSSean Bruno int i, j; 51334c7070dbSScott Long struct taskqgroup *tqg; 513487890dbaSSean Bruno iflib_fl_t fl; 51354c7070dbSScott Long 51364c7070dbSScott Long /* Make sure VLANS are not using driver */ 51374c7070dbSScott Long if (if_vlantrunkinuse(ifp)) { 51384c7070dbSScott Long device_printf(dev, "Vlan in use, detach first\n"); 51394c7070dbSScott Long return (EBUSY); 51404c7070dbSScott Long } 514177c1fcecSEric Joyner #ifdef PCI_IOV 514277c1fcecSEric Joyner if (!CTX_IS_VF(ctx) && pci_iov_detach(dev) != 0) { 514377c1fcecSEric Joyner device_printf(dev, "SR-IOV in use; detach first.\n"); 514477c1fcecSEric Joyner return (EBUSY); 514577c1fcecSEric Joyner } 514677c1fcecSEric Joyner #endif 514777c1fcecSEric Joyner 514877c1fcecSEric Joyner STATE_LOCK(ctx); 514977c1fcecSEric Joyner ctx->ifc_flags |= IFC_IN_DETACH; 515077c1fcecSEric Joyner STATE_UNLOCK(ctx); 51514c7070dbSScott Long 51521558015eSEric Joyner /* Unregister VLAN handlers before calling iflib_stop() */ 51531558015eSEric Joyner iflib_unregister_vlan_handlers(ctx); 51541558015eSEric Joyner 51551558015eSEric Joyner iflib_netmap_detach(ifp); 51561558015eSEric Joyner ether_ifdetach(ifp); 51571558015eSEric Joyner 51584c7070dbSScott Long CTX_LOCK(ctx); 51594c7070dbSScott Long iflib_stop(ctx); 51604c7070dbSScott Long CTX_UNLOCK(ctx); 51614c7070dbSScott Long 51626d49b41eSAndrew Gallatin iflib_rem_pfil(ctx); 51634c7070dbSScott Long if (ctx->ifc_led_dev != NULL) 51644c7070dbSScott Long led_destroy(ctx->ifc_led_dev); 51654c7070dbSScott Long /* XXX drain any dependent tasks */ 5166ab2e3f79SStephen Hurd tqg = qgroup_if_io_tqg; 516723ac9029SStephen Hurd for (txq = ctx->ifc_txqs, i = 0; i < NTXQSETS(ctx); i++, txq++) { 51684c7070dbSScott Long callout_drain(&txq->ift_timer); 5169*17cec474SVincenzo Maffione #ifdef DEV_NETMAP 5170*17cec474SVincenzo Maffione callout_drain(&txq->ift_netmap_timer); 5171*17cec474SVincenzo Maffione #endif /* DEV_NETMAP */ 51724c7070dbSScott Long if (txq->ift_task.gt_uniq != NULL) 51734c7070dbSScott Long taskqgroup_detach(tqg, &txq->ift_task); 51744c7070dbSScott Long } 51754c7070dbSScott Long for (i = 0, rxq = ctx->ifc_rxqs; i < NRXQSETS(ctx); i++, rxq++) { 51764c7070dbSScott Long if (rxq->ifr_task.gt_uniq != NULL) 51774c7070dbSScott Long taskqgroup_detach(tqg, &rxq->ifr_task); 517887890dbaSSean Bruno 517987890dbaSSean Bruno for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++) 518087890dbaSSean Bruno free(fl->ifl_rx_bitmap, M_IFLIB); 51814c7070dbSScott Long } 5182ab2e3f79SStephen Hurd tqg = qgroup_if_config_tqg; 51834c7070dbSScott Long if (ctx->ifc_admin_task.gt_uniq != NULL) 51844c7070dbSScott Long taskqgroup_detach(tqg, &ctx->ifc_admin_task); 51854c7070dbSScott Long if (ctx->ifc_vflr_task.gt_uniq != NULL) 51864c7070dbSScott Long taskqgroup_detach(tqg, &ctx->ifc_vflr_task); 51876c3c3194SMatt Macy CTX_LOCK(ctx); 51884c7070dbSScott Long IFDI_DETACH(ctx); 51896c3c3194SMatt Macy CTX_UNLOCK(ctx); 51906c3c3194SMatt Macy 51916c3c3194SMatt Macy /* ether_ifdetach calls if_qflush - lock must be destroy afterwards*/ 519277c1fcecSEric Joyner iflib_free_intr_mem(ctx); 519377c1fcecSEric Joyner 519477c1fcecSEric Joyner bus_generic_detach(dev); 519577c1fcecSEric Joyner 519677c1fcecSEric Joyner iflib_tx_structures_free(ctx); 519777c1fcecSEric Joyner iflib_rx_structures_free(ctx); 519856614414SEric Joyner 519956614414SEric Joyner iflib_deregister(ctx); 520056614414SEric Joyner 520156614414SEric Joyner device_set_softc(ctx->ifc_dev, NULL); 520277c1fcecSEric Joyner if (ctx->ifc_flags & IFC_SC_ALLOCATED) 520377c1fcecSEric Joyner free(ctx->ifc_softc, M_IFLIB); 5204f154ece0SStephen Hurd unref_ctx_core_offset(ctx); 520577c1fcecSEric Joyner free(ctx, M_IFLIB); 520677c1fcecSEric Joyner return (0); 520777c1fcecSEric Joyner } 520877c1fcecSEric Joyner 520977c1fcecSEric Joyner static void 521077c1fcecSEric Joyner iflib_free_intr_mem(if_ctx_t ctx) 521177c1fcecSEric Joyner { 521277c1fcecSEric Joyner 52134c7070dbSScott Long if (ctx->ifc_softc_ctx.isc_intr != IFLIB_INTR_MSIX) { 52144c7070dbSScott Long iflib_irq_free(ctx, &ctx->ifc_legacy_irq); 52154c7070dbSScott Long } 5216b97de13aSMarius Strobl if (ctx->ifc_softc_ctx.isc_intr != IFLIB_INTR_LEGACY) { 5217b97de13aSMarius Strobl pci_release_msi(ctx->ifc_dev); 5218b97de13aSMarius Strobl } 52194c7070dbSScott Long if (ctx->ifc_msix_mem != NULL) { 52204c7070dbSScott Long bus_release_resource(ctx->ifc_dev, SYS_RES_MEMORY, 5221b97de13aSMarius Strobl rman_get_rid(ctx->ifc_msix_mem), ctx->ifc_msix_mem); 52224c7070dbSScott Long ctx->ifc_msix_mem = NULL; 52234c7070dbSScott Long } 52244c7070dbSScott Long } 52254c7070dbSScott Long 52264c7070dbSScott Long int 52274c7070dbSScott Long iflib_device_detach(device_t dev) 52284c7070dbSScott Long { 52294c7070dbSScott Long if_ctx_t ctx = device_get_softc(dev); 52304c7070dbSScott Long 52314c7070dbSScott Long return (iflib_device_deregister(ctx)); 52324c7070dbSScott Long } 52334c7070dbSScott Long 52344c7070dbSScott Long int 52354c7070dbSScott Long iflib_device_suspend(device_t dev) 52364c7070dbSScott Long { 52374c7070dbSScott Long if_ctx_t ctx = device_get_softc(dev); 52384c7070dbSScott Long 52394c7070dbSScott Long CTX_LOCK(ctx); 52404c7070dbSScott Long IFDI_SUSPEND(ctx); 52414c7070dbSScott Long CTX_UNLOCK(ctx); 52424c7070dbSScott Long 52434c7070dbSScott Long return bus_generic_suspend(dev); 52444c7070dbSScott Long } 52454c7070dbSScott Long int 52464c7070dbSScott Long iflib_device_shutdown(device_t dev) 52474c7070dbSScott Long { 52484c7070dbSScott Long if_ctx_t ctx = device_get_softc(dev); 52494c7070dbSScott Long 52504c7070dbSScott Long CTX_LOCK(ctx); 52514c7070dbSScott Long IFDI_SHUTDOWN(ctx); 52524c7070dbSScott Long CTX_UNLOCK(ctx); 52534c7070dbSScott Long 52544c7070dbSScott Long return bus_generic_suspend(dev); 52554c7070dbSScott Long } 52564c7070dbSScott Long 52574c7070dbSScott Long int 52584c7070dbSScott Long iflib_device_resume(device_t dev) 52594c7070dbSScott Long { 52604c7070dbSScott Long if_ctx_t ctx = device_get_softc(dev); 52614c7070dbSScott Long iflib_txq_t txq = ctx->ifc_txqs; 52624c7070dbSScott Long 52634c7070dbSScott Long CTX_LOCK(ctx); 52644c7070dbSScott Long IFDI_RESUME(ctx); 5265cd28ea92SStephen Hurd iflib_if_init_locked(ctx); 52664c7070dbSScott Long CTX_UNLOCK(ctx); 52674c7070dbSScott Long for (int i = 0; i < NTXQSETS(ctx); i++, txq++) 52684c7070dbSScott Long iflib_txq_check_drain(txq, IFLIB_RESTART_BUDGET); 52694c7070dbSScott Long 52704c7070dbSScott Long return (bus_generic_resume(dev)); 52714c7070dbSScott Long } 52724c7070dbSScott Long 52734c7070dbSScott Long int 52744c7070dbSScott Long iflib_device_iov_init(device_t dev, uint16_t num_vfs, const nvlist_t *params) 52754c7070dbSScott Long { 52764c7070dbSScott Long int error; 52774c7070dbSScott Long if_ctx_t ctx = device_get_softc(dev); 52784c7070dbSScott Long 52794c7070dbSScott Long CTX_LOCK(ctx); 52804c7070dbSScott Long error = IFDI_IOV_INIT(ctx, num_vfs, params); 52814c7070dbSScott Long CTX_UNLOCK(ctx); 52824c7070dbSScott Long 52834c7070dbSScott Long return (error); 52844c7070dbSScott Long } 52854c7070dbSScott Long 52864c7070dbSScott Long void 52874c7070dbSScott Long iflib_device_iov_uninit(device_t dev) 52884c7070dbSScott Long { 52894c7070dbSScott Long if_ctx_t ctx = device_get_softc(dev); 52904c7070dbSScott Long 52914c7070dbSScott Long CTX_LOCK(ctx); 52924c7070dbSScott Long IFDI_IOV_UNINIT(ctx); 52934c7070dbSScott Long CTX_UNLOCK(ctx); 52944c7070dbSScott Long } 52954c7070dbSScott Long 52964c7070dbSScott Long int 52974c7070dbSScott Long iflib_device_iov_add_vf(device_t dev, uint16_t vfnum, const nvlist_t *params) 52984c7070dbSScott Long { 52994c7070dbSScott Long int error; 53004c7070dbSScott Long if_ctx_t ctx = device_get_softc(dev); 53014c7070dbSScott Long 53024c7070dbSScott Long CTX_LOCK(ctx); 53034c7070dbSScott Long error = IFDI_IOV_VF_ADD(ctx, vfnum, params); 53044c7070dbSScott Long CTX_UNLOCK(ctx); 53054c7070dbSScott Long 53064c7070dbSScott Long return (error); 53074c7070dbSScott Long } 53084c7070dbSScott Long 53094c7070dbSScott Long /********************************************************************* 53104c7070dbSScott Long * 53114c7070dbSScott Long * MODULE FUNCTION DEFINITIONS 53124c7070dbSScott Long * 53134c7070dbSScott Long **********************************************************************/ 53144c7070dbSScott Long 5315ab2e3f79SStephen Hurd /* 5316ab2e3f79SStephen Hurd * - Start a fast taskqueue thread for each core 5317ab2e3f79SStephen Hurd * - Start a taskqueue for control operations 5318ab2e3f79SStephen Hurd */ 53194c7070dbSScott Long static int 53204c7070dbSScott Long iflib_module_init(void) 53214c7070dbSScott Long { 53224c7070dbSScott Long return (0); 53234c7070dbSScott Long } 53244c7070dbSScott Long 53254c7070dbSScott Long static int 53264c7070dbSScott Long iflib_module_event_handler(module_t mod, int what, void *arg) 53274c7070dbSScott Long { 53284c7070dbSScott Long int err; 53294c7070dbSScott Long 53304c7070dbSScott Long switch (what) { 53314c7070dbSScott Long case MOD_LOAD: 53324c7070dbSScott Long if ((err = iflib_module_init()) != 0) 53334c7070dbSScott Long return (err); 53344c7070dbSScott Long break; 53354c7070dbSScott Long case MOD_UNLOAD: 53364c7070dbSScott Long return (EBUSY); 53374c7070dbSScott Long default: 53384c7070dbSScott Long return (EOPNOTSUPP); 53394c7070dbSScott Long } 53404c7070dbSScott Long 53414c7070dbSScott Long return (0); 53424c7070dbSScott Long } 53434c7070dbSScott Long 53444c7070dbSScott Long /********************************************************************* 53454c7070dbSScott Long * 53464c7070dbSScott Long * PUBLIC FUNCTION DEFINITIONS 53474c7070dbSScott Long * ordered as in iflib.h 53484c7070dbSScott Long * 53494c7070dbSScott Long **********************************************************************/ 53504c7070dbSScott Long 53514c7070dbSScott Long static void 53524c7070dbSScott Long _iflib_assert(if_shared_ctx_t sctx) 53534c7070dbSScott Long { 5354afb77372SEric Joyner int i; 5355afb77372SEric Joyner 53564c7070dbSScott Long MPASS(sctx->isc_tx_maxsize); 53574c7070dbSScott Long MPASS(sctx->isc_tx_maxsegsize); 53584c7070dbSScott Long 53594c7070dbSScott Long MPASS(sctx->isc_rx_maxsize); 53604c7070dbSScott Long MPASS(sctx->isc_rx_nsegments); 53614c7070dbSScott Long MPASS(sctx->isc_rx_maxsegsize); 53624c7070dbSScott Long 5363afb77372SEric Joyner MPASS(sctx->isc_nrxqs >= 1 && sctx->isc_nrxqs <= 8); 5364afb77372SEric Joyner for (i = 0; i < sctx->isc_nrxqs; i++) { 5365afb77372SEric Joyner MPASS(sctx->isc_nrxd_min[i]); 5366afb77372SEric Joyner MPASS(powerof2(sctx->isc_nrxd_min[i])); 5367afb77372SEric Joyner MPASS(sctx->isc_nrxd_max[i]); 5368afb77372SEric Joyner MPASS(powerof2(sctx->isc_nrxd_max[i])); 5369afb77372SEric Joyner MPASS(sctx->isc_nrxd_default[i]); 5370afb77372SEric Joyner MPASS(powerof2(sctx->isc_nrxd_default[i])); 5371afb77372SEric Joyner } 5372afb77372SEric Joyner 5373afb77372SEric Joyner MPASS(sctx->isc_ntxqs >= 1 && sctx->isc_ntxqs <= 8); 5374afb77372SEric Joyner for (i = 0; i < sctx->isc_ntxqs; i++) { 5375afb77372SEric Joyner MPASS(sctx->isc_ntxd_min[i]); 5376afb77372SEric Joyner MPASS(powerof2(sctx->isc_ntxd_min[i])); 5377afb77372SEric Joyner MPASS(sctx->isc_ntxd_max[i]); 5378afb77372SEric Joyner MPASS(powerof2(sctx->isc_ntxd_max[i])); 5379afb77372SEric Joyner MPASS(sctx->isc_ntxd_default[i]); 5380afb77372SEric Joyner MPASS(powerof2(sctx->isc_ntxd_default[i])); 5381afb77372SEric Joyner } 53824c7070dbSScott Long } 53834c7070dbSScott Long 53841248952aSSean Bruno static void 53851248952aSSean Bruno _iflib_pre_assert(if_softc_ctx_t scctx) 53861248952aSSean Bruno { 53871248952aSSean Bruno 53881248952aSSean Bruno MPASS(scctx->isc_txrx->ift_txd_encap); 53891248952aSSean Bruno MPASS(scctx->isc_txrx->ift_txd_flush); 53901248952aSSean Bruno MPASS(scctx->isc_txrx->ift_txd_credits_update); 53911248952aSSean Bruno MPASS(scctx->isc_txrx->ift_rxd_available); 53921248952aSSean Bruno MPASS(scctx->isc_txrx->ift_rxd_pkt_get); 53931248952aSSean Bruno MPASS(scctx->isc_txrx->ift_rxd_refill); 53941248952aSSean Bruno MPASS(scctx->isc_txrx->ift_rxd_flush); 53951248952aSSean Bruno } 53962fe66646SSean Bruno 53974c7070dbSScott Long static int 53984c7070dbSScott Long iflib_register(if_ctx_t ctx) 53994c7070dbSScott Long { 54004c7070dbSScott Long if_shared_ctx_t sctx = ctx->ifc_sctx; 54014c7070dbSScott Long driver_t *driver = sctx->isc_driver; 54024c7070dbSScott Long device_t dev = ctx->ifc_dev; 54034c7070dbSScott Long if_t ifp; 54049aeca213SMatt Macy u_char type; 54059aeca213SMatt Macy int iflags; 54064c7070dbSScott Long 54071f93e931SMatt Macy if ((sctx->isc_flags & IFLIB_PSEUDO) == 0) 54084c7070dbSScott Long _iflib_assert(sctx); 54094c7070dbSScott Long 5410aa8a24d3SStephen Hurd CTX_LOCK_INIT(ctx); 54117b610b60SSean Bruno STATE_LOCK_INIT(ctx, device_get_nameunit(ctx->ifc_dev)); 54129aeca213SMatt Macy if (sctx->isc_flags & IFLIB_PSEUDO) { 54139aeca213SMatt Macy if (sctx->isc_flags & IFLIB_PSEUDO_ETHER) 54149aeca213SMatt Macy type = IFT_ETHER; 54159aeca213SMatt Macy else 54169aeca213SMatt Macy type = IFT_PPP; 54179aeca213SMatt Macy } else 54189aeca213SMatt Macy type = IFT_ETHER; 54199aeca213SMatt Macy ifp = ctx->ifc_ifp = if_alloc(type); 54204c7070dbSScott Long if (ifp == NULL) { 54214c7070dbSScott Long device_printf(dev, "can not allocate ifnet structure\n"); 54224c7070dbSScott Long return (ENOMEM); 54234c7070dbSScott Long } 54244c7070dbSScott Long 54254c7070dbSScott Long /* 54264c7070dbSScott Long * Initialize our context's device specific methods 54274c7070dbSScott Long */ 54284c7070dbSScott Long kobj_init((kobj_t) ctx, (kobj_class_t) driver); 54294c7070dbSScott Long kobj_class_compile((kobj_class_t) driver); 54304c7070dbSScott Long 54314c7070dbSScott Long if_initname(ifp, device_get_name(dev), device_get_unit(dev)); 54324c7070dbSScott Long if_setsoftc(ifp, ctx); 54334c7070dbSScott Long if_setdev(ifp, dev); 54344c7070dbSScott Long if_setinitfn(ifp, iflib_if_init); 54354c7070dbSScott Long if_setioctlfn(ifp, iflib_if_ioctl); 5436b8ca4756SPatrick Kelsey #ifdef ALTQ 5437b8ca4756SPatrick Kelsey if_setstartfn(ifp, iflib_altq_if_start); 5438b8ca4756SPatrick Kelsey if_settransmitfn(ifp, iflib_altq_if_transmit); 54398f410865SPatrick Kelsey if_setsendqready(ifp); 5440b8ca4756SPatrick Kelsey #else 54414c7070dbSScott Long if_settransmitfn(ifp, iflib_if_transmit); 5442b8ca4756SPatrick Kelsey #endif 54434c7070dbSScott Long if_setqflushfn(ifp, iflib_if_qflush); 54449aeca213SMatt Macy iflags = IFF_MULTICAST | IFF_KNOWSEPOCH; 54454c7070dbSScott Long 54469aeca213SMatt Macy if ((sctx->isc_flags & IFLIB_PSEUDO) && 54479aeca213SMatt Macy (sctx->isc_flags & IFLIB_PSEUDO_ETHER) == 0) 54489aeca213SMatt Macy iflags |= IFF_POINTOPOINT; 54499aeca213SMatt Macy else 54509aeca213SMatt Macy iflags |= IFF_BROADCAST | IFF_SIMPLEX; 54519aeca213SMatt Macy if_setflags(ifp, iflags); 54524c7070dbSScott Long ctx->ifc_vlan_attach_event = 54534c7070dbSScott Long EVENTHANDLER_REGISTER(vlan_config, iflib_vlan_register, ctx, 54544c7070dbSScott Long EVENTHANDLER_PRI_FIRST); 54554c7070dbSScott Long ctx->ifc_vlan_detach_event = 54564c7070dbSScott Long EVENTHANDLER_REGISTER(vlan_unconfig, iflib_vlan_unregister, ctx, 54574c7070dbSScott Long EVENTHANDLER_PRI_FIRST); 54584c7070dbSScott Long 5459e2621d96SMatt Macy if ((sctx->isc_flags & IFLIB_DRIVER_MEDIA) == 0) { 5460e2621d96SMatt Macy ctx->ifc_mediap = &ctx->ifc_media; 5461e2621d96SMatt Macy ifmedia_init(ctx->ifc_mediap, IFM_IMASK, 54624c7070dbSScott Long iflib_media_change, iflib_media_status); 5463e2621d96SMatt Macy } 54644c7070dbSScott Long return (0); 54654c7070dbSScott Long } 54664c7070dbSScott Long 546756614414SEric Joyner static void 54681558015eSEric Joyner iflib_unregister_vlan_handlers(if_ctx_t ctx) 546956614414SEric Joyner { 547056614414SEric Joyner /* Unregister VLAN events */ 547156614414SEric Joyner if (ctx->ifc_vlan_attach_event != NULL) { 547256614414SEric Joyner EVENTHANDLER_DEREGISTER(vlan_config, ctx->ifc_vlan_attach_event); 547356614414SEric Joyner ctx->ifc_vlan_attach_event = NULL; 547456614414SEric Joyner } 547556614414SEric Joyner if (ctx->ifc_vlan_detach_event != NULL) { 547656614414SEric Joyner EVENTHANDLER_DEREGISTER(vlan_unconfig, ctx->ifc_vlan_detach_event); 547756614414SEric Joyner ctx->ifc_vlan_detach_event = NULL; 547856614414SEric Joyner } 547956614414SEric Joyner 54801558015eSEric Joyner } 54811558015eSEric Joyner 54821558015eSEric Joyner static void 54831558015eSEric Joyner iflib_deregister(if_ctx_t ctx) 54841558015eSEric Joyner { 54851558015eSEric Joyner if_t ifp = ctx->ifc_ifp; 54861558015eSEric Joyner 54871558015eSEric Joyner /* Remove all media */ 54881558015eSEric Joyner ifmedia_removeall(&ctx->ifc_media); 54891558015eSEric Joyner 54901558015eSEric Joyner /* Ensure that VLAN event handlers are unregistered */ 54911558015eSEric Joyner iflib_unregister_vlan_handlers(ctx); 54921558015eSEric Joyner 549356614414SEric Joyner /* Release kobject reference */ 549456614414SEric Joyner kobj_delete((kobj_t) ctx, NULL); 549556614414SEric Joyner 549656614414SEric Joyner /* Free the ifnet structure */ 549756614414SEric Joyner if_free(ifp); 549856614414SEric Joyner 549956614414SEric Joyner STATE_LOCK_DESTROY(ctx); 550056614414SEric Joyner 550156614414SEric Joyner /* ether_ifdetach calls if_qflush - lock must be destroy afterwards*/ 550256614414SEric Joyner CTX_LOCK_DESTROY(ctx); 550356614414SEric Joyner } 550456614414SEric Joyner 55054c7070dbSScott Long static int 55064c7070dbSScott Long iflib_queues_alloc(if_ctx_t ctx) 55074c7070dbSScott Long { 55084c7070dbSScott Long if_shared_ctx_t sctx = ctx->ifc_sctx; 550923ac9029SStephen Hurd if_softc_ctx_t scctx = &ctx->ifc_softc_ctx; 55104c7070dbSScott Long device_t dev = ctx->ifc_dev; 551123ac9029SStephen Hurd int nrxqsets = scctx->isc_nrxqsets; 551223ac9029SStephen Hurd int ntxqsets = scctx->isc_ntxqsets; 55134c7070dbSScott Long iflib_txq_t txq; 55144c7070dbSScott Long iflib_rxq_t rxq; 55154c7070dbSScott Long iflib_fl_t fl = NULL; 551623ac9029SStephen Hurd int i, j, cpu, err, txconf, rxconf; 55174c7070dbSScott Long iflib_dma_info_t ifdip; 551823ac9029SStephen Hurd uint32_t *rxqsizes = scctx->isc_rxqsizes; 551923ac9029SStephen Hurd uint32_t *txqsizes = scctx->isc_txqsizes; 55204c7070dbSScott Long uint8_t nrxqs = sctx->isc_nrxqs; 55214c7070dbSScott Long uint8_t ntxqs = sctx->isc_ntxqs; 55224c7070dbSScott Long int nfree_lists = sctx->isc_nfl ? sctx->isc_nfl : 1; 55234c7070dbSScott Long caddr_t *vaddrs; 55244c7070dbSScott Long uint64_t *paddrs; 55254c7070dbSScott Long 552623ac9029SStephen Hurd KASSERT(ntxqs > 0, ("number of queues per qset must be at least 1")); 552723ac9029SStephen Hurd KASSERT(nrxqs > 0, ("number of queues per qset must be at least 1")); 55284c7070dbSScott Long 55294c7070dbSScott Long /* Allocate the TX ring struct memory */ 5530b89827a0SStephen Hurd if (!(ctx->ifc_txqs = 5531ac2fffa4SPedro F. Giffuni (iflib_txq_t) malloc(sizeof(struct iflib_txq) * 5532ac2fffa4SPedro F. Giffuni ntxqsets, M_IFLIB, M_NOWAIT | M_ZERO))) { 55334c7070dbSScott Long device_printf(dev, "Unable to allocate TX ring memory\n"); 55344c7070dbSScott Long err = ENOMEM; 55354c7070dbSScott Long goto fail; 55364c7070dbSScott Long } 55374c7070dbSScott Long 55384c7070dbSScott Long /* Now allocate the RX */ 5539b89827a0SStephen Hurd if (!(ctx->ifc_rxqs = 5540ac2fffa4SPedro F. Giffuni (iflib_rxq_t) malloc(sizeof(struct iflib_rxq) * 5541ac2fffa4SPedro F. Giffuni nrxqsets, M_IFLIB, M_NOWAIT | M_ZERO))) { 55424c7070dbSScott Long device_printf(dev, "Unable to allocate RX ring memory\n"); 55434c7070dbSScott Long err = ENOMEM; 55444c7070dbSScott Long goto rx_fail; 55454c7070dbSScott Long } 55464c7070dbSScott Long 5547b89827a0SStephen Hurd txq = ctx->ifc_txqs; 5548b89827a0SStephen Hurd rxq = ctx->ifc_rxqs; 55494c7070dbSScott Long 55504c7070dbSScott Long /* 55514c7070dbSScott Long * XXX handle allocation failure 55524c7070dbSScott Long */ 555396c85efbSNathan Whitehorn for (txconf = i = 0, cpu = CPU_FIRST(); i < ntxqsets; i++, txconf++, txq++, cpu = CPU_NEXT(cpu)) { 55544c7070dbSScott Long /* Set up some basics */ 55554c7070dbSScott Long 5556bfce461eSMarius Strobl if ((ifdip = malloc(sizeof(struct iflib_dma_info) * ntxqs, 5557bfce461eSMarius Strobl M_IFLIB, M_NOWAIT | M_ZERO)) == NULL) { 5558bfce461eSMarius Strobl device_printf(dev, 5559bfce461eSMarius Strobl "Unable to allocate TX DMA info memory\n"); 55604c7070dbSScott Long err = ENOMEM; 55610d0338afSConrad Meyer goto err_tx_desc; 55624c7070dbSScott Long } 55634c7070dbSScott Long txq->ift_ifdi = ifdip; 55644c7070dbSScott Long for (j = 0; j < ntxqs; j++, ifdip++) { 5565bfce461eSMarius Strobl if (iflib_dma_alloc(ctx, txqsizes[j], ifdip, 0)) { 5566bfce461eSMarius Strobl device_printf(dev, 5567bfce461eSMarius Strobl "Unable to allocate TX descriptors\n"); 55684c7070dbSScott Long err = ENOMEM; 55694c7070dbSScott Long goto err_tx_desc; 55704c7070dbSScott Long } 557195246abbSSean Bruno txq->ift_txd_size[j] = scctx->isc_txd_size[j]; 55724c7070dbSScott Long bzero((void *)ifdip->idi_vaddr, txqsizes[j]); 55734c7070dbSScott Long } 55744c7070dbSScott Long txq->ift_ctx = ctx; 55754c7070dbSScott Long txq->ift_id = i; 557623ac9029SStephen Hurd if (sctx->isc_flags & IFLIB_HAS_TXCQ) { 557723ac9029SStephen Hurd txq->ift_br_offset = 1; 557823ac9029SStephen Hurd } else { 557923ac9029SStephen Hurd txq->ift_br_offset = 0; 558023ac9029SStephen Hurd } 55814c7070dbSScott Long 55824c7070dbSScott Long if (iflib_txsd_alloc(txq)) { 55834c7070dbSScott Long device_printf(dev, "Critical Failure setting up TX buffers\n"); 55844c7070dbSScott Long err = ENOMEM; 55854c7070dbSScott Long goto err_tx_desc; 55864c7070dbSScott Long } 55874c7070dbSScott Long 55884c7070dbSScott Long /* Initialize the TX lock */ 55891722eeacSMarius Strobl snprintf(txq->ift_mtx_name, MTX_NAME_LEN, "%s:TX(%d):callout", 55904c7070dbSScott Long device_get_nameunit(dev), txq->ift_id); 55914c7070dbSScott Long mtx_init(&txq->ift_mtx, txq->ift_mtx_name, NULL, MTX_DEF); 55924c7070dbSScott Long callout_init_mtx(&txq->ift_timer, &txq->ift_mtx, 0); 5593*17cec474SVincenzo Maffione txq->ift_timer.c_cpu = cpu; 5594*17cec474SVincenzo Maffione #ifdef DEV_NETMAP 5595*17cec474SVincenzo Maffione callout_init_mtx(&txq->ift_netmap_timer, &txq->ift_mtx, 0); 5596*17cec474SVincenzo Maffione txq->ift_netmap_timer.c_cpu = cpu; 5597*17cec474SVincenzo Maffione #endif /* DEV_NETMAP */ 55984c7070dbSScott Long 559995246abbSSean Bruno err = ifmp_ring_alloc(&txq->ift_br, 2048, txq, iflib_txq_drain, 56004c7070dbSScott Long iflib_txq_can_drain, M_IFLIB, M_WAITOK); 56014c7070dbSScott Long if (err) { 56024c7070dbSScott Long /* XXX free any allocated rings */ 56034c7070dbSScott Long device_printf(dev, "Unable to allocate buf_ring\n"); 56040d0338afSConrad Meyer goto err_tx_desc; 56054c7070dbSScott Long } 56064c7070dbSScott Long } 56074c7070dbSScott Long 56084c7070dbSScott Long for (rxconf = i = 0; i < nrxqsets; i++, rxconf++, rxq++) { 56094c7070dbSScott Long /* Set up some basics */ 5610fb1a29b4SHans Petter Selasky callout_init(&rxq->ifr_watchdog, 1); 56114c7070dbSScott Long 5612bfce461eSMarius Strobl if ((ifdip = malloc(sizeof(struct iflib_dma_info) * nrxqs, 5613bfce461eSMarius Strobl M_IFLIB, M_NOWAIT | M_ZERO)) == NULL) { 5614bfce461eSMarius Strobl device_printf(dev, 5615bfce461eSMarius Strobl "Unable to allocate RX DMA info memory\n"); 56164c7070dbSScott Long err = ENOMEM; 56170d0338afSConrad Meyer goto err_tx_desc; 56184c7070dbSScott Long } 56194c7070dbSScott Long 56204c7070dbSScott Long rxq->ifr_ifdi = ifdip; 562195246abbSSean Bruno /* XXX this needs to be changed if #rx queues != #tx queues */ 562295246abbSSean Bruno rxq->ifr_ntxqirq = 1; 562395246abbSSean Bruno rxq->ifr_txqid[0] = i; 56244c7070dbSScott Long for (j = 0; j < nrxqs; j++, ifdip++) { 5625bfce461eSMarius Strobl if (iflib_dma_alloc(ctx, rxqsizes[j], ifdip, 0)) { 5626bfce461eSMarius Strobl device_printf(dev, 5627bfce461eSMarius Strobl "Unable to allocate RX descriptors\n"); 56284c7070dbSScott Long err = ENOMEM; 56294c7070dbSScott Long goto err_tx_desc; 56304c7070dbSScott Long } 56314c7070dbSScott Long bzero((void *)ifdip->idi_vaddr, rxqsizes[j]); 56324c7070dbSScott Long } 56334c7070dbSScott Long rxq->ifr_ctx = ctx; 56344c7070dbSScott Long rxq->ifr_id = i; 563523ac9029SStephen Hurd if (sctx->isc_flags & IFLIB_HAS_RXCQ) { 563623ac9029SStephen Hurd rxq->ifr_fl_offset = 1; 56374c7070dbSScott Long } else { 563823ac9029SStephen Hurd rxq->ifr_fl_offset = 0; 56394c7070dbSScott Long } 56404c7070dbSScott Long rxq->ifr_nfl = nfree_lists; 56414c7070dbSScott Long if (!(fl = 5642ac2fffa4SPedro F. Giffuni (iflib_fl_t) malloc(sizeof(struct iflib_fl) * nfree_lists, M_IFLIB, M_NOWAIT | M_ZERO))) { 56434c7070dbSScott Long device_printf(dev, "Unable to allocate free list memory\n"); 56444c7070dbSScott Long err = ENOMEM; 56450d0338afSConrad Meyer goto err_tx_desc; 56464c7070dbSScott Long } 56474c7070dbSScott Long rxq->ifr_fl = fl; 56484c7070dbSScott Long for (j = 0; j < nfree_lists; j++) { 564995246abbSSean Bruno fl[j].ifl_rxq = rxq; 565095246abbSSean Bruno fl[j].ifl_id = j; 565195246abbSSean Bruno fl[j].ifl_ifdi = &rxq->ifr_ifdi[j + rxq->ifr_fl_offset]; 565295246abbSSean Bruno fl[j].ifl_rxd_size = scctx->isc_rxd_size[j]; 56534c7070dbSScott Long } 56544c7070dbSScott Long /* Allocate receive buffers for the ring */ 56554c7070dbSScott Long if (iflib_rxsd_alloc(rxq)) { 56564c7070dbSScott Long device_printf(dev, 56574c7070dbSScott Long "Critical Failure setting up receive buffers\n"); 56584c7070dbSScott Long err = ENOMEM; 56594c7070dbSScott Long goto err_rx_desc; 56604c7070dbSScott Long } 566187890dbaSSean Bruno 566287890dbaSSean Bruno for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++) 56633db348b5SMarius Strobl fl->ifl_rx_bitmap = bit_alloc(fl->ifl_size, M_IFLIB, 56643db348b5SMarius Strobl M_WAITOK); 56654c7070dbSScott Long } 56664c7070dbSScott Long 56674c7070dbSScott Long /* TXQs */ 56684c7070dbSScott Long vaddrs = malloc(sizeof(caddr_t)*ntxqsets*ntxqs, M_IFLIB, M_WAITOK); 56694c7070dbSScott Long paddrs = malloc(sizeof(uint64_t)*ntxqsets*ntxqs, M_IFLIB, M_WAITOK); 56704c7070dbSScott Long for (i = 0; i < ntxqsets; i++) { 56714c7070dbSScott Long iflib_dma_info_t di = ctx->ifc_txqs[i].ift_ifdi; 56724c7070dbSScott Long 56734c7070dbSScott Long for (j = 0; j < ntxqs; j++, di++) { 56744c7070dbSScott Long vaddrs[i*ntxqs + j] = di->idi_vaddr; 56754c7070dbSScott Long paddrs[i*ntxqs + j] = di->idi_paddr; 56764c7070dbSScott Long } 56774c7070dbSScott Long } 56784c7070dbSScott Long if ((err = IFDI_TX_QUEUES_ALLOC(ctx, vaddrs, paddrs, ntxqs, ntxqsets)) != 0) { 5679bfce461eSMarius Strobl device_printf(ctx->ifc_dev, 5680bfce461eSMarius Strobl "Unable to allocate device TX queue\n"); 56814c7070dbSScott Long iflib_tx_structures_free(ctx); 56824c7070dbSScott Long free(vaddrs, M_IFLIB); 56834c7070dbSScott Long free(paddrs, M_IFLIB); 56844c7070dbSScott Long goto err_rx_desc; 56854c7070dbSScott Long } 56864c7070dbSScott Long free(vaddrs, M_IFLIB); 56874c7070dbSScott Long free(paddrs, M_IFLIB); 56884c7070dbSScott Long 56894c7070dbSScott Long /* RXQs */ 56904c7070dbSScott Long vaddrs = malloc(sizeof(caddr_t)*nrxqsets*nrxqs, M_IFLIB, M_WAITOK); 56914c7070dbSScott Long paddrs = malloc(sizeof(uint64_t)*nrxqsets*nrxqs, M_IFLIB, M_WAITOK); 56924c7070dbSScott Long for (i = 0; i < nrxqsets; i++) { 56934c7070dbSScott Long iflib_dma_info_t di = ctx->ifc_rxqs[i].ifr_ifdi; 56944c7070dbSScott Long 56954c7070dbSScott Long for (j = 0; j < nrxqs; j++, di++) { 56964c7070dbSScott Long vaddrs[i*nrxqs + j] = di->idi_vaddr; 56974c7070dbSScott Long paddrs[i*nrxqs + j] = di->idi_paddr; 56984c7070dbSScott Long } 56994c7070dbSScott Long } 57004c7070dbSScott Long if ((err = IFDI_RX_QUEUES_ALLOC(ctx, vaddrs, paddrs, nrxqs, nrxqsets)) != 0) { 5701bfce461eSMarius Strobl device_printf(ctx->ifc_dev, 5702bfce461eSMarius Strobl "Unable to allocate device RX queue\n"); 57034c7070dbSScott Long iflib_tx_structures_free(ctx); 57044c7070dbSScott Long free(vaddrs, M_IFLIB); 57054c7070dbSScott Long free(paddrs, M_IFLIB); 57064c7070dbSScott Long goto err_rx_desc; 57074c7070dbSScott Long } 57084c7070dbSScott Long free(vaddrs, M_IFLIB); 57094c7070dbSScott Long free(paddrs, M_IFLIB); 57104c7070dbSScott Long 57114c7070dbSScott Long return (0); 57124c7070dbSScott Long 57134c7070dbSScott Long /* XXX handle allocation failure changes */ 57144c7070dbSScott Long err_rx_desc: 57154c7070dbSScott Long err_tx_desc: 5716b89827a0SStephen Hurd rx_fail: 57174c7070dbSScott Long if (ctx->ifc_rxqs != NULL) 57184c7070dbSScott Long free(ctx->ifc_rxqs, M_IFLIB); 57194c7070dbSScott Long ctx->ifc_rxqs = NULL; 57204c7070dbSScott Long if (ctx->ifc_txqs != NULL) 57214c7070dbSScott Long free(ctx->ifc_txqs, M_IFLIB); 57224c7070dbSScott Long ctx->ifc_txqs = NULL; 57234c7070dbSScott Long fail: 57244c7070dbSScott Long return (err); 57254c7070dbSScott Long } 57264c7070dbSScott Long 57274c7070dbSScott Long static int 57284c7070dbSScott Long iflib_tx_structures_setup(if_ctx_t ctx) 57294c7070dbSScott Long { 57304c7070dbSScott Long iflib_txq_t txq = ctx->ifc_txqs; 57314c7070dbSScott Long int i; 57324c7070dbSScott Long 57334c7070dbSScott Long for (i = 0; i < NTXQSETS(ctx); i++, txq++) 57344c7070dbSScott Long iflib_txq_setup(txq); 57354c7070dbSScott Long 57364c7070dbSScott Long return (0); 57374c7070dbSScott Long } 57384c7070dbSScott Long 57394c7070dbSScott Long static void 57404c7070dbSScott Long iflib_tx_structures_free(if_ctx_t ctx) 57414c7070dbSScott Long { 57424c7070dbSScott Long iflib_txq_t txq = ctx->ifc_txqs; 57434d261ce2SStephen Hurd if_shared_ctx_t sctx = ctx->ifc_sctx; 57444c7070dbSScott Long int i, j; 57454c7070dbSScott Long 57464c7070dbSScott Long for (i = 0; i < NTXQSETS(ctx); i++, txq++) { 57474d261ce2SStephen Hurd for (j = 0; j < sctx->isc_ntxqs; j++) 57484c7070dbSScott Long iflib_dma_free(&txq->ift_ifdi[j]); 5749244e7cffSEric Joyner iflib_txq_destroy(txq); 57504c7070dbSScott Long } 57514c7070dbSScott Long free(ctx->ifc_txqs, M_IFLIB); 57524c7070dbSScott Long ctx->ifc_txqs = NULL; 57534c7070dbSScott Long IFDI_QUEUES_FREE(ctx); 57544c7070dbSScott Long } 57554c7070dbSScott Long 57564c7070dbSScott Long /********************************************************************* 57574c7070dbSScott Long * 57584c7070dbSScott Long * Initialize all receive rings. 57594c7070dbSScott Long * 57604c7070dbSScott Long **********************************************************************/ 57614c7070dbSScott Long static int 57624c7070dbSScott Long iflib_rx_structures_setup(if_ctx_t ctx) 57634c7070dbSScott Long { 57644c7070dbSScott Long iflib_rxq_t rxq = ctx->ifc_rxqs; 5765aaeb188aSBjoern A. Zeeb int q; 5766aaeb188aSBjoern A. Zeeb #if defined(INET6) || defined(INET) 57673d10e9edSMarius Strobl int err, i; 5768aaeb188aSBjoern A. Zeeb #endif 57694c7070dbSScott Long 57704c7070dbSScott Long for (q = 0; q < ctx->ifc_softc_ctx.isc_nrxqsets; q++, rxq++) { 5771aaeb188aSBjoern A. Zeeb #if defined(INET6) || defined(INET) 57723d10e9edSMarius Strobl if (if_getcapabilities(ctx->ifc_ifp) & IFCAP_LRO) { 57733d10e9edSMarius Strobl err = tcp_lro_init_args(&rxq->ifr_lc, ctx->ifc_ifp, 577423ac9029SStephen Hurd TCP_LRO_ENTRIES, min(1024, 57753d10e9edSMarius Strobl ctx->ifc_softc_ctx.isc_nrxd[rxq->ifr_fl_offset])); 57763d10e9edSMarius Strobl if (err != 0) { 57773d10e9edSMarius Strobl device_printf(ctx->ifc_dev, 57783d10e9edSMarius Strobl "LRO Initialization failed!\n"); 57794c7070dbSScott Long goto fail; 57804c7070dbSScott Long } 57813d10e9edSMarius Strobl } 5782aaeb188aSBjoern A. Zeeb #endif 57834c7070dbSScott Long IFDI_RXQ_SETUP(ctx, rxq->ifr_id); 57844c7070dbSScott Long } 57854c7070dbSScott Long return (0); 5786aaeb188aSBjoern A. Zeeb #if defined(INET6) || defined(INET) 57874c7070dbSScott Long fail: 57884c7070dbSScott Long /* 57893d10e9edSMarius Strobl * Free LRO resources allocated so far, we will only handle 57904c7070dbSScott Long * the rings that completed, the failing case will have 57914c7070dbSScott Long * cleaned up for itself. 'q' failed, so its the terminus. 57924c7070dbSScott Long */ 57934c7070dbSScott Long rxq = ctx->ifc_rxqs; 57944c7070dbSScott Long for (i = 0; i < q; ++i, rxq++) { 57953d10e9edSMarius Strobl if (if_getcapabilities(ctx->ifc_ifp) & IFCAP_LRO) 57963d10e9edSMarius Strobl tcp_lro_free(&rxq->ifr_lc); 57974c7070dbSScott Long } 57984c7070dbSScott Long return (err); 5799aaeb188aSBjoern A. Zeeb #endif 58004c7070dbSScott Long } 58014c7070dbSScott Long 58024c7070dbSScott Long /********************************************************************* 58034c7070dbSScott Long * 58044c7070dbSScott Long * Free all receive rings. 58054c7070dbSScott Long * 58064c7070dbSScott Long **********************************************************************/ 58074c7070dbSScott Long static void 58084c7070dbSScott Long iflib_rx_structures_free(if_ctx_t ctx) 58094c7070dbSScott Long { 58104c7070dbSScott Long iflib_rxq_t rxq = ctx->ifc_rxqs; 5811db8e8f1eSEric Joyner if_shared_ctx_t sctx = ctx->ifc_sctx; 5812db8e8f1eSEric Joyner int i, j; 58134c7070dbSScott Long 58143d10e9edSMarius Strobl for (i = 0; i < ctx->ifc_softc_ctx.isc_nrxqsets; i++, rxq++) { 5815db8e8f1eSEric Joyner for (j = 0; j < sctx->isc_nrxqs; j++) 5816db8e8f1eSEric Joyner iflib_dma_free(&rxq->ifr_ifdi[j]); 58174c7070dbSScott Long iflib_rx_sds_free(rxq); 5818007b804fSMarius Strobl #if defined(INET6) || defined(INET) 58193d10e9edSMarius Strobl if (if_getcapabilities(ctx->ifc_ifp) & IFCAP_LRO) 58203d10e9edSMarius Strobl tcp_lro_free(&rxq->ifr_lc); 5821007b804fSMarius Strobl #endif 58224c7070dbSScott Long } 582377c1fcecSEric Joyner free(ctx->ifc_rxqs, M_IFLIB); 582477c1fcecSEric Joyner ctx->ifc_rxqs = NULL; 58254c7070dbSScott Long } 58264c7070dbSScott Long 58274c7070dbSScott Long static int 58284c7070dbSScott Long iflib_qset_structures_setup(if_ctx_t ctx) 58294c7070dbSScott Long { 58304c7070dbSScott Long int err; 58314c7070dbSScott Long 58326108c013SStephen Hurd /* 58336108c013SStephen Hurd * It is expected that the caller takes care of freeing queues if this 58346108c013SStephen Hurd * fails. 58356108c013SStephen Hurd */ 5836ac88e6daSStephen Hurd if ((err = iflib_tx_structures_setup(ctx)) != 0) { 5837ac88e6daSStephen Hurd device_printf(ctx->ifc_dev, "iflib_tx_structures_setup failed: %d\n", err); 58384c7070dbSScott Long return (err); 5839ac88e6daSStephen Hurd } 58404c7070dbSScott Long 58416108c013SStephen Hurd if ((err = iflib_rx_structures_setup(ctx)) != 0) 58424c7070dbSScott Long device_printf(ctx->ifc_dev, "iflib_rx_structures_setup failed: %d\n", err); 58436108c013SStephen Hurd 58444c7070dbSScott Long return (err); 58454c7070dbSScott Long } 58464c7070dbSScott Long 58474c7070dbSScott Long int 58484c7070dbSScott Long iflib_irq_alloc(if_ctx_t ctx, if_irq_t irq, int rid, 58493e0e6330SStephen Hurd driver_filter_t filter, void *filter_arg, driver_intr_t handler, void *arg, const char *name) 58504c7070dbSScott Long { 58514c7070dbSScott Long 58524c7070dbSScott Long return (_iflib_irq_alloc(ctx, irq, rid, filter, handler, arg, name)); 58534c7070dbSScott Long } 58544c7070dbSScott Long 5855b103855eSStephen Hurd #ifdef SMP 5856aa3c5dd8SSean Bruno static int 5857b103855eSStephen Hurd find_nth(if_ctx_t ctx, int qid) 58584c7070dbSScott Long { 5859b103855eSStephen Hurd cpuset_t cpus; 5860aa3c5dd8SSean Bruno int i, cpuid, eqid, count; 58614c7070dbSScott Long 5862b103855eSStephen Hurd CPU_COPY(&ctx->ifc_cpus, &cpus); 5863b103855eSStephen Hurd count = CPU_COUNT(&cpus); 5864aa3c5dd8SSean Bruno eqid = qid % count; 58654c7070dbSScott Long /* clear up to the qid'th bit */ 5866aa3c5dd8SSean Bruno for (i = 0; i < eqid; i++) { 5867b103855eSStephen Hurd cpuid = CPU_FFS(&cpus); 5868aa3c5dd8SSean Bruno MPASS(cpuid != 0); 5869b103855eSStephen Hurd CPU_CLR(cpuid-1, &cpus); 58704c7070dbSScott Long } 5871b103855eSStephen Hurd cpuid = CPU_FFS(&cpus); 5872aa3c5dd8SSean Bruno MPASS(cpuid != 0); 5873aa3c5dd8SSean Bruno return (cpuid-1); 58744c7070dbSScott Long } 58754c7070dbSScott Long 5876b103855eSStephen Hurd #ifdef SCHED_ULE 5877b103855eSStephen Hurd extern struct cpu_group *cpu_top; /* CPU topology */ 5878b103855eSStephen Hurd 5879b103855eSStephen Hurd static int 5880b103855eSStephen Hurd find_child_with_core(int cpu, struct cpu_group *grp) 5881b103855eSStephen Hurd { 5882b103855eSStephen Hurd int i; 5883b103855eSStephen Hurd 5884b103855eSStephen Hurd if (grp->cg_children == 0) 5885b103855eSStephen Hurd return -1; 5886b103855eSStephen Hurd 5887b103855eSStephen Hurd MPASS(grp->cg_child); 5888b103855eSStephen Hurd for (i = 0; i < grp->cg_children; i++) { 5889b103855eSStephen Hurd if (CPU_ISSET(cpu, &grp->cg_child[i].cg_mask)) 5890b103855eSStephen Hurd return i; 5891b103855eSStephen Hurd } 5892b103855eSStephen Hurd 5893b103855eSStephen Hurd return -1; 5894b103855eSStephen Hurd } 5895b103855eSStephen Hurd 5896b103855eSStephen Hurd /* 58970b75ac77SStephen Hurd * Find the nth "close" core to the specified core 58980b75ac77SStephen Hurd * "close" is defined as the deepest level that shares 58990b75ac77SStephen Hurd * at least an L2 cache. With threads, this will be 5900f154ece0SStephen Hurd * threads on the same core. If the shared cache is L3 59010b75ac77SStephen Hurd * or higher, simply returns the same core. 5902b103855eSStephen Hurd */ 5903b103855eSStephen Hurd static int 59040b75ac77SStephen Hurd find_close_core(int cpu, int core_offset) 5905b103855eSStephen Hurd { 5906b103855eSStephen Hurd struct cpu_group *grp; 5907b103855eSStephen Hurd int i; 59080b75ac77SStephen Hurd int fcpu; 5909b103855eSStephen Hurd cpuset_t cs; 5910b103855eSStephen Hurd 5911b103855eSStephen Hurd grp = cpu_top; 5912b103855eSStephen Hurd if (grp == NULL) 5913b103855eSStephen Hurd return cpu; 5914b103855eSStephen Hurd i = 0; 5915b103855eSStephen Hurd while ((i = find_child_with_core(cpu, grp)) != -1) { 5916b103855eSStephen Hurd /* If the child only has one cpu, don't descend */ 5917b103855eSStephen Hurd if (grp->cg_child[i].cg_count <= 1) 5918b103855eSStephen Hurd break; 5919b103855eSStephen Hurd grp = &grp->cg_child[i]; 5920b103855eSStephen Hurd } 5921b103855eSStephen Hurd 5922b103855eSStephen Hurd /* If they don't share at least an L2 cache, use the same CPU */ 5923b103855eSStephen Hurd if (grp->cg_level > CG_SHARE_L2 || grp->cg_level == CG_SHARE_NONE) 5924b103855eSStephen Hurd return cpu; 5925b103855eSStephen Hurd 5926b103855eSStephen Hurd /* Now pick one */ 5927b103855eSStephen Hurd CPU_COPY(&grp->cg_mask, &cs); 59280b75ac77SStephen Hurd 59290b75ac77SStephen Hurd /* Add the selected CPU offset to core offset. */ 59300b75ac77SStephen Hurd for (i = 0; (fcpu = CPU_FFS(&cs)) != 0; i++) { 59310b75ac77SStephen Hurd if (fcpu - 1 == cpu) 59320b75ac77SStephen Hurd break; 59330b75ac77SStephen Hurd CPU_CLR(fcpu - 1, &cs); 59340b75ac77SStephen Hurd } 59350b75ac77SStephen Hurd MPASS(fcpu); 59360b75ac77SStephen Hurd 59370b75ac77SStephen Hurd core_offset += i; 59380b75ac77SStephen Hurd 59390b75ac77SStephen Hurd CPU_COPY(&grp->cg_mask, &cs); 59400b75ac77SStephen Hurd for (i = core_offset % grp->cg_count; i > 0; i--) { 5941b103855eSStephen Hurd MPASS(CPU_FFS(&cs)); 5942b103855eSStephen Hurd CPU_CLR(CPU_FFS(&cs) - 1, &cs); 5943b103855eSStephen Hurd } 5944b103855eSStephen Hurd MPASS(CPU_FFS(&cs)); 5945b103855eSStephen Hurd return CPU_FFS(&cs) - 1; 5946b103855eSStephen Hurd } 5947b103855eSStephen Hurd #else 5948b103855eSStephen Hurd static int 59490b75ac77SStephen Hurd find_close_core(int cpu, int core_offset __unused) 5950b103855eSStephen Hurd { 595197755e83SKonstantin Belousov return cpu; 5952b103855eSStephen Hurd } 5953b103855eSStephen Hurd #endif 5954b103855eSStephen Hurd 5955b103855eSStephen Hurd static int 59560b75ac77SStephen Hurd get_core_offset(if_ctx_t ctx, iflib_intr_type_t type, int qid) 5957b103855eSStephen Hurd { 5958b103855eSStephen Hurd switch (type) { 5959b103855eSStephen Hurd case IFLIB_INTR_TX: 59600b75ac77SStephen Hurd /* TX queues get cores which share at least an L2 cache with the corresponding RX queue */ 59610b75ac77SStephen Hurd /* XXX handle multiple RX threads per core and more than two core per L2 group */ 5962b103855eSStephen Hurd return qid / CPU_COUNT(&ctx->ifc_cpus) + 1; 5963b103855eSStephen Hurd case IFLIB_INTR_RX: 5964b103855eSStephen Hurd case IFLIB_INTR_RXTX: 59650b75ac77SStephen Hurd /* RX queues get the specified core */ 5966b103855eSStephen Hurd return qid / CPU_COUNT(&ctx->ifc_cpus); 5967b103855eSStephen Hurd default: 5968b103855eSStephen Hurd return -1; 5969b103855eSStephen Hurd } 5970b103855eSStephen Hurd } 5971b103855eSStephen Hurd #else 59720b75ac77SStephen Hurd #define get_core_offset(ctx, type, qid) CPU_FIRST() 59730b75ac77SStephen Hurd #define find_close_core(cpuid, tid) CPU_FIRST() 5974b103855eSStephen Hurd #define find_nth(ctx, gid) CPU_FIRST() 5975b103855eSStephen Hurd #endif 5976b103855eSStephen Hurd 5977b103855eSStephen Hurd /* Just to avoid copy/paste */ 5978b103855eSStephen Hurd static inline int 5979f855ec81SMarius Strobl iflib_irq_set_affinity(if_ctx_t ctx, if_irq_t irq, iflib_intr_type_t type, 5980f855ec81SMarius Strobl int qid, struct grouptask *gtask, struct taskqgroup *tqg, void *uniq, 5981f855ec81SMarius Strobl const char *name) 5982b103855eSStephen Hurd { 5983f855ec81SMarius Strobl device_t dev; 5984f154ece0SStephen Hurd int co, cpuid, err, tid; 5985b103855eSStephen Hurd 5986f855ec81SMarius Strobl dev = ctx->ifc_dev; 5987f154ece0SStephen Hurd co = ctx->ifc_sysctl_core_offset; 5988f154ece0SStephen Hurd if (ctx->ifc_sysctl_separate_txrx && type == IFLIB_INTR_TX) 5989f154ece0SStephen Hurd co += ctx->ifc_softc_ctx.isc_nrxqsets; 5990f154ece0SStephen Hurd cpuid = find_nth(ctx, qid + co); 59910b75ac77SStephen Hurd tid = get_core_offset(ctx, type, qid); 59923d10e9edSMarius Strobl if (tid < 0) { 59933d10e9edSMarius Strobl device_printf(dev, "get_core_offset failed\n"); 59943d10e9edSMarius Strobl return (EOPNOTSUPP); 59953d10e9edSMarius Strobl } 59960b75ac77SStephen Hurd cpuid = find_close_core(cpuid, tid); 5997f855ec81SMarius Strobl err = taskqgroup_attach_cpu(tqg, gtask, uniq, cpuid, dev, irq->ii_res, 5998f855ec81SMarius Strobl name); 5999b103855eSStephen Hurd if (err) { 6000f855ec81SMarius Strobl device_printf(dev, "taskqgroup_attach_cpu failed %d\n", err); 6001b103855eSStephen Hurd return (err); 6002b103855eSStephen Hurd } 6003b103855eSStephen Hurd #ifdef notyet 6004b103855eSStephen Hurd if (cpuid > ctx->ifc_cpuid_highest) 6005b103855eSStephen Hurd ctx->ifc_cpuid_highest = cpuid; 6006b103855eSStephen Hurd #endif 60073d10e9edSMarius Strobl return (0); 6008b103855eSStephen Hurd } 6009b103855eSStephen Hurd 60104c7070dbSScott Long int 60114c7070dbSScott Long iflib_irq_alloc_generic(if_ctx_t ctx, if_irq_t irq, int rid, 60124c7070dbSScott Long iflib_intr_type_t type, driver_filter_t *filter, 60133e0e6330SStephen Hurd void *filter_arg, int qid, const char *name) 60144c7070dbSScott Long { 6015f855ec81SMarius Strobl device_t dev; 60164c7070dbSScott Long struct grouptask *gtask; 60174c7070dbSScott Long struct taskqgroup *tqg; 60184c7070dbSScott Long iflib_filter_info_t info; 601923ac9029SStephen Hurd gtask_fn_t *fn; 6020b103855eSStephen Hurd int tqrid, err; 602195246abbSSean Bruno driver_filter_t *intr_fast; 60224c7070dbSScott Long void *q; 60234c7070dbSScott Long 60244c7070dbSScott Long info = &ctx->ifc_filter_info; 6025add6f7d0SSean Bruno tqrid = rid; 60264c7070dbSScott Long 60274c7070dbSScott Long switch (type) { 60284c7070dbSScott Long /* XXX merge tx/rx for netmap? */ 60294c7070dbSScott Long case IFLIB_INTR_TX: 60304c7070dbSScott Long q = &ctx->ifc_txqs[qid]; 60314c7070dbSScott Long info = &ctx->ifc_txqs[qid].ift_filter_info; 60324c7070dbSScott Long gtask = &ctx->ifc_txqs[qid].ift_task; 6033ab2e3f79SStephen Hurd tqg = qgroup_if_io_tqg; 60344c7070dbSScott Long fn = _task_fn_tx; 603595246abbSSean Bruno intr_fast = iflib_fast_intr; 6036da69b8f9SSean Bruno GROUPTASK_INIT(gtask, 0, fn, q); 60375ee36c68SStephen Hurd ctx->ifc_flags |= IFC_NETMAP_TX_IRQ; 60384c7070dbSScott Long break; 60394c7070dbSScott Long case IFLIB_INTR_RX: 60404c7070dbSScott Long q = &ctx->ifc_rxqs[qid]; 60414c7070dbSScott Long info = &ctx->ifc_rxqs[qid].ifr_filter_info; 60424c7070dbSScott Long gtask = &ctx->ifc_rxqs[qid].ifr_task; 6043ab2e3f79SStephen Hurd tqg = qgroup_if_io_tqg; 60444c7070dbSScott Long fn = _task_fn_rx; 6045ab2e3f79SStephen Hurd intr_fast = iflib_fast_intr; 60466c3e93cbSGleb Smirnoff NET_GROUPTASK_INIT(gtask, 0, fn, q); 604795246abbSSean Bruno break; 604895246abbSSean Bruno case IFLIB_INTR_RXTX: 604995246abbSSean Bruno q = &ctx->ifc_rxqs[qid]; 605095246abbSSean Bruno info = &ctx->ifc_rxqs[qid].ifr_filter_info; 605195246abbSSean Bruno gtask = &ctx->ifc_rxqs[qid].ifr_task; 6052ab2e3f79SStephen Hurd tqg = qgroup_if_io_tqg; 605395246abbSSean Bruno fn = _task_fn_rx; 605495246abbSSean Bruno intr_fast = iflib_fast_intr_rxtx; 60556c3e93cbSGleb Smirnoff NET_GROUPTASK_INIT(gtask, 0, fn, q); 60564c7070dbSScott Long break; 60574c7070dbSScott Long case IFLIB_INTR_ADMIN: 60584c7070dbSScott Long q = ctx; 6059da69b8f9SSean Bruno tqrid = -1; 60604c7070dbSScott Long info = &ctx->ifc_filter_info; 60614c7070dbSScott Long gtask = &ctx->ifc_admin_task; 6062ab2e3f79SStephen Hurd tqg = qgroup_if_config_tqg; 60634c7070dbSScott Long fn = _task_fn_admin; 606495246abbSSean Bruno intr_fast = iflib_fast_intr_ctx; 60654c7070dbSScott Long break; 60664c7070dbSScott Long default: 60673d10e9edSMarius Strobl device_printf(ctx->ifc_dev, "%s: unknown net intr type\n", 60683d10e9edSMarius Strobl __func__); 60693d10e9edSMarius Strobl return (EINVAL); 60704c7070dbSScott Long } 60714c7070dbSScott Long 60724c7070dbSScott Long info->ifi_filter = filter; 60734c7070dbSScott Long info->ifi_filter_arg = filter_arg; 60744c7070dbSScott Long info->ifi_task = gtask; 607595246abbSSean Bruno info->ifi_ctx = q; 60764c7070dbSScott Long 6077f855ec81SMarius Strobl dev = ctx->ifc_dev; 607895246abbSSean Bruno err = _iflib_irq_alloc(ctx, irq, rid, intr_fast, NULL, info, name); 6079da69b8f9SSean Bruno if (err != 0) { 6080f855ec81SMarius Strobl device_printf(dev, "_iflib_irq_alloc failed %d\n", err); 60814c7070dbSScott Long return (err); 6082da69b8f9SSean Bruno } 6083da69b8f9SSean Bruno if (type == IFLIB_INTR_ADMIN) 6084da69b8f9SSean Bruno return (0); 6085da69b8f9SSean Bruno 60864c7070dbSScott Long if (tqrid != -1) { 6087f855ec81SMarius Strobl err = iflib_irq_set_affinity(ctx, irq, type, qid, gtask, tqg, 6088f855ec81SMarius Strobl q, name); 6089b103855eSStephen Hurd if (err) 6090b103855eSStephen Hurd return (err); 6091aa3c5dd8SSean Bruno } else { 6092f855ec81SMarius Strobl taskqgroup_attach(tqg, gtask, q, dev, irq->ii_res, name); 6093aa3c5dd8SSean Bruno } 60944c7070dbSScott Long 60954c7070dbSScott Long return (0); 60964c7070dbSScott Long } 60974c7070dbSScott Long 60984c7070dbSScott Long void 60993e0e6330SStephen 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) 61004c7070dbSScott Long { 61014c7070dbSScott Long struct grouptask *gtask; 61024c7070dbSScott Long struct taskqgroup *tqg; 610323ac9029SStephen Hurd gtask_fn_t *fn; 61044c7070dbSScott Long void *q; 6105b103855eSStephen Hurd int err; 61064c7070dbSScott Long 61074c7070dbSScott Long switch (type) { 61084c7070dbSScott Long case IFLIB_INTR_TX: 61094c7070dbSScott Long q = &ctx->ifc_txqs[qid]; 61104c7070dbSScott Long gtask = &ctx->ifc_txqs[qid].ift_task; 6111ab2e3f79SStephen Hurd tqg = qgroup_if_io_tqg; 61124c7070dbSScott Long fn = _task_fn_tx; 6113f98977b5SHans Petter Selasky GROUPTASK_INIT(gtask, 0, fn, q); 61144c7070dbSScott Long break; 61154c7070dbSScott Long case IFLIB_INTR_RX: 61164c7070dbSScott Long q = &ctx->ifc_rxqs[qid]; 61174c7070dbSScott Long gtask = &ctx->ifc_rxqs[qid].ifr_task; 6118ab2e3f79SStephen Hurd tqg = qgroup_if_io_tqg; 61194c7070dbSScott Long fn = _task_fn_rx; 6120f98977b5SHans Petter Selasky NET_GROUPTASK_INIT(gtask, 0, fn, q); 61214c7070dbSScott Long break; 61224c7070dbSScott Long case IFLIB_INTR_IOV: 61234c7070dbSScott Long q = ctx; 61244c7070dbSScott Long gtask = &ctx->ifc_vflr_task; 6125ab2e3f79SStephen Hurd tqg = qgroup_if_config_tqg; 61264c7070dbSScott Long fn = _task_fn_iov; 6127f98977b5SHans Petter Selasky GROUPTASK_INIT(gtask, 0, fn, q); 61284c7070dbSScott Long break; 61294c7070dbSScott Long default: 61304c7070dbSScott Long panic("unknown net intr type"); 61314c7070dbSScott Long } 6132f855ec81SMarius Strobl if (irq != NULL) { 6133f855ec81SMarius Strobl err = iflib_irq_set_affinity(ctx, irq, type, qid, gtask, tqg, 6134f855ec81SMarius Strobl q, name); 6135b103855eSStephen Hurd if (err) 6136f855ec81SMarius Strobl taskqgroup_attach(tqg, gtask, q, ctx->ifc_dev, 6137f855ec81SMarius Strobl irq->ii_res, name); 6138f855ec81SMarius Strobl } else { 6139f855ec81SMarius Strobl taskqgroup_attach(tqg, gtask, q, NULL, NULL, name); 6140b103855eSStephen Hurd } 6141b103855eSStephen Hurd } 61424c7070dbSScott Long 61434c7070dbSScott Long void 61444c7070dbSScott Long iflib_irq_free(if_ctx_t ctx, if_irq_t irq) 61454c7070dbSScott Long { 6146b97de13aSMarius Strobl 61474c7070dbSScott Long if (irq->ii_tag) 61484c7070dbSScott Long bus_teardown_intr(ctx->ifc_dev, irq->ii_res, irq->ii_tag); 61494c7070dbSScott Long 61504c7070dbSScott Long if (irq->ii_res) 6151b97de13aSMarius Strobl bus_release_resource(ctx->ifc_dev, SYS_RES_IRQ, 6152b97de13aSMarius Strobl rman_get_rid(irq->ii_res), irq->ii_res); 61534c7070dbSScott Long } 61544c7070dbSScott Long 61554c7070dbSScott Long static int 61563e0e6330SStephen Hurd iflib_legacy_setup(if_ctx_t ctx, driver_filter_t filter, void *filter_arg, int *rid, const char *name) 61574c7070dbSScott Long { 61584c7070dbSScott Long iflib_txq_t txq = ctx->ifc_txqs; 61594c7070dbSScott Long iflib_rxq_t rxq = ctx->ifc_rxqs; 61604c7070dbSScott Long if_irq_t irq = &ctx->ifc_legacy_irq; 61614c7070dbSScott Long iflib_filter_info_t info; 6162f855ec81SMarius Strobl device_t dev; 61634c7070dbSScott Long struct grouptask *gtask; 6164f855ec81SMarius Strobl struct resource *res; 61654c7070dbSScott Long struct taskqgroup *tqg; 61664c7070dbSScott Long void *q; 6167d49e83eaSMarius Strobl int err, tqrid; 616841669133SMark Johnston bool rx_only; 61694c7070dbSScott Long 61704c7070dbSScott Long q = &ctx->ifc_rxqs[0]; 61714c7070dbSScott Long info = &rxq[0].ifr_filter_info; 61724c7070dbSScott Long gtask = &rxq[0].ifr_task; 6173ab2e3f79SStephen Hurd tqg = qgroup_if_io_tqg; 6174d49e83eaSMarius Strobl tqrid = *rid; 617541669133SMark Johnston rx_only = (ctx->ifc_sctx->isc_flags & IFLIB_SINGLE_IRQ_RX_ONLY) != 0; 61764c7070dbSScott Long 61774c7070dbSScott Long ctx->ifc_flags |= IFC_LEGACY; 61784c7070dbSScott Long info->ifi_filter = filter; 61794c7070dbSScott Long info->ifi_filter_arg = filter_arg; 61804c7070dbSScott Long info->ifi_task = gtask; 618141669133SMark Johnston info->ifi_ctx = rx_only ? ctx : q; 61824c7070dbSScott Long 6183f855ec81SMarius Strobl dev = ctx->ifc_dev; 61844c7070dbSScott Long /* We allocate a single interrupt resource */ 618541669133SMark Johnston err = _iflib_irq_alloc(ctx, irq, tqrid, rx_only ? iflib_fast_intr_ctx : 618641669133SMark Johnston iflib_fast_intr_rxtx, NULL, info, name); 618741669133SMark Johnston if (err != 0) 61884c7070dbSScott Long return (err); 6189f98977b5SHans Petter Selasky NET_GROUPTASK_INIT(gtask, 0, _task_fn_rx, q); 6190f855ec81SMarius Strobl res = irq->ii_res; 6191f855ec81SMarius Strobl taskqgroup_attach(tqg, gtask, q, dev, res, name); 61924c7070dbSScott Long 61934c7070dbSScott Long GROUPTASK_INIT(&txq->ift_task, 0, _task_fn_tx, txq); 6194f855ec81SMarius Strobl taskqgroup_attach(qgroup_if_io_tqg, &txq->ift_task, txq, dev, res, 6195f855ec81SMarius Strobl "tx"); 61964c7070dbSScott Long return (0); 61974c7070dbSScott Long } 61984c7070dbSScott Long 61994c7070dbSScott Long void 62004c7070dbSScott Long iflib_led_create(if_ctx_t ctx) 62014c7070dbSScott Long { 62024c7070dbSScott Long 62034c7070dbSScott Long ctx->ifc_led_dev = led_create(iflib_led_func, ctx, 62044c7070dbSScott Long device_get_nameunit(ctx->ifc_dev)); 62054c7070dbSScott Long } 62064c7070dbSScott Long 62074c7070dbSScott Long void 62084c7070dbSScott Long iflib_tx_intr_deferred(if_ctx_t ctx, int txqid) 62094c7070dbSScott Long { 62104c7070dbSScott Long 62114c7070dbSScott Long GROUPTASK_ENQUEUE(&ctx->ifc_txqs[txqid].ift_task); 62124c7070dbSScott Long } 62134c7070dbSScott Long 62144c7070dbSScott Long void 62154c7070dbSScott Long iflib_rx_intr_deferred(if_ctx_t ctx, int rxqid) 62164c7070dbSScott Long { 62174c7070dbSScott Long 62184c7070dbSScott Long GROUPTASK_ENQUEUE(&ctx->ifc_rxqs[rxqid].ifr_task); 62194c7070dbSScott Long } 62204c7070dbSScott Long 62214c7070dbSScott Long void 62224c7070dbSScott Long iflib_admin_intr_deferred(if_ctx_t ctx) 62234c7070dbSScott Long { 622446fa0c25SEric Joyner 6225ed6611ccSEd Maste MPASS(ctx->ifc_admin_task.gt_taskqueue != NULL); 62264c7070dbSScott Long GROUPTASK_ENQUEUE(&ctx->ifc_admin_task); 62274c7070dbSScott Long } 62284c7070dbSScott Long 62294c7070dbSScott Long void 62304c7070dbSScott Long iflib_iov_intr_deferred(if_ctx_t ctx) 62314c7070dbSScott Long { 62324c7070dbSScott Long 62334c7070dbSScott Long GROUPTASK_ENQUEUE(&ctx->ifc_vflr_task); 62344c7070dbSScott Long } 62354c7070dbSScott Long 62364c7070dbSScott Long void 6237d49e83eaSMarius Strobl iflib_io_tqg_attach(struct grouptask *gt, void *uniq, int cpu, const char *name) 62384c7070dbSScott Long { 62394c7070dbSScott Long 6240f855ec81SMarius Strobl taskqgroup_attach_cpu(qgroup_if_io_tqg, gt, uniq, cpu, NULL, NULL, 6241f855ec81SMarius Strobl name); 62424c7070dbSScott Long } 62434c7070dbSScott Long 62444c7070dbSScott Long void 6245aa8a24d3SStephen Hurd iflib_config_gtask_init(void *ctx, struct grouptask *gtask, gtask_fn_t *fn, 6246aa8a24d3SStephen Hurd const char *name) 62474c7070dbSScott Long { 62484c7070dbSScott Long 62494c7070dbSScott Long GROUPTASK_INIT(gtask, 0, fn, ctx); 6250f855ec81SMarius Strobl taskqgroup_attach(qgroup_if_config_tqg, gtask, gtask, NULL, NULL, 6251f855ec81SMarius Strobl name); 62524c7070dbSScott Long } 62534c7070dbSScott Long 62544c7070dbSScott Long void 625523ac9029SStephen Hurd iflib_config_gtask_deinit(struct grouptask *gtask) 625623ac9029SStephen Hurd { 625723ac9029SStephen Hurd 6258ab2e3f79SStephen Hurd taskqgroup_detach(qgroup_if_config_tqg, gtask); 625923ac9029SStephen Hurd } 626023ac9029SStephen Hurd 626123ac9029SStephen Hurd void 626223ac9029SStephen Hurd iflib_link_state_change(if_ctx_t ctx, int link_state, uint64_t baudrate) 62634c7070dbSScott Long { 62644c7070dbSScott Long if_t ifp = ctx->ifc_ifp; 62654c7070dbSScott Long iflib_txq_t txq = ctx->ifc_txqs; 62664c7070dbSScott Long 62674c7070dbSScott Long if_setbaudrate(ifp, baudrate); 62687b610b60SSean Bruno if (baudrate >= IF_Gbps(10)) { 62697b610b60SSean Bruno STATE_LOCK(ctx); 627095246abbSSean Bruno ctx->ifc_flags |= IFC_PREFETCH; 62717b610b60SSean Bruno STATE_UNLOCK(ctx); 62727b610b60SSean Bruno } 62734c7070dbSScott Long /* If link down, disable watchdog */ 62744c7070dbSScott Long if ((ctx->ifc_link_state == LINK_STATE_UP) && (link_state == LINK_STATE_DOWN)) { 62754c7070dbSScott Long for (int i = 0; i < ctx->ifc_softc_ctx.isc_ntxqsets; i++, txq++) 62764c7070dbSScott Long txq->ift_qstatus = IFLIB_QUEUE_IDLE; 62774c7070dbSScott Long } 62784c7070dbSScott Long ctx->ifc_link_state = link_state; 62794c7070dbSScott Long if_link_state_change(ifp, link_state); 62804c7070dbSScott Long } 62814c7070dbSScott Long 62824c7070dbSScott Long static int 62834c7070dbSScott Long iflib_tx_credits_update(if_ctx_t ctx, iflib_txq_t txq) 62844c7070dbSScott Long { 62854c7070dbSScott Long int credits; 62861248952aSSean Bruno #ifdef INVARIANTS 62871248952aSSean Bruno int credits_pre = txq->ift_cidx_processed; 62881248952aSSean Bruno #endif 62894c7070dbSScott Long 62908a04b53dSKonstantin Belousov bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map, 62918a04b53dSKonstantin Belousov BUS_DMASYNC_POSTREAD); 629295246abbSSean Bruno if ((credits = ctx->isc_txd_credits_update(ctx->ifc_softc, txq->ift_id, true)) == 0) 62934c7070dbSScott Long return (0); 62944c7070dbSScott Long 62954c7070dbSScott Long txq->ift_processed += credits; 62964c7070dbSScott Long txq->ift_cidx_processed += credits; 62974c7070dbSScott Long 62981248952aSSean Bruno MPASS(credits_pre + credits == txq->ift_cidx_processed); 62994c7070dbSScott Long if (txq->ift_cidx_processed >= txq->ift_size) 63004c7070dbSScott Long txq->ift_cidx_processed -= txq->ift_size; 63014c7070dbSScott Long return (credits); 63024c7070dbSScott Long } 63034c7070dbSScott Long 63044c7070dbSScott Long static int 630595246abbSSean Bruno iflib_rxd_avail(if_ctx_t ctx, iflib_rxq_t rxq, qidx_t cidx, qidx_t budget) 63064c7070dbSScott Long { 630795dcf343SMarius Strobl iflib_fl_t fl; 630895dcf343SMarius Strobl u_int i; 63094c7070dbSScott Long 631095dcf343SMarius Strobl for (i = 0, fl = &rxq->ifr_fl[0]; i < rxq->ifr_nfl; i++, fl++) 631195dcf343SMarius Strobl bus_dmamap_sync(fl->ifl_ifdi->idi_tag, fl->ifl_ifdi->idi_map, 631295dcf343SMarius Strobl BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 631323ac9029SStephen Hurd return (ctx->isc_rxd_available(ctx->ifc_softc, rxq->ifr_id, cidx, 631423ac9029SStephen Hurd budget)); 63154c7070dbSScott Long } 63164c7070dbSScott Long 63174c7070dbSScott Long void 63184c7070dbSScott Long iflib_add_int_delay_sysctl(if_ctx_t ctx, const char *name, 63194c7070dbSScott Long const char *description, if_int_delay_info_t info, 63204c7070dbSScott Long int offset, int value) 63214c7070dbSScott Long { 63224c7070dbSScott Long info->iidi_ctx = ctx; 63234c7070dbSScott Long info->iidi_offset = offset; 63244c7070dbSScott Long info->iidi_value = value; 63254c7070dbSScott Long SYSCTL_ADD_PROC(device_get_sysctl_ctx(ctx->ifc_dev), 63264c7070dbSScott Long SYSCTL_CHILDREN(device_get_sysctl_tree(ctx->ifc_dev)), 63277029da5cSPawel Biernacki OID_AUTO, name, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 63284c7070dbSScott Long info, 0, iflib_sysctl_int_delay, "I", description); 63294c7070dbSScott Long } 63304c7070dbSScott Long 6331aa8a24d3SStephen Hurd struct sx * 63324c7070dbSScott Long iflib_ctx_lock_get(if_ctx_t ctx) 63334c7070dbSScott Long { 63344c7070dbSScott Long 6335aa8a24d3SStephen Hurd return (&ctx->ifc_ctx_sx); 63364c7070dbSScott Long } 63374c7070dbSScott Long 63384c7070dbSScott Long static int 63394c7070dbSScott Long iflib_msix_init(if_ctx_t ctx) 63404c7070dbSScott Long { 63414c7070dbSScott Long device_t dev = ctx->ifc_dev; 63424c7070dbSScott Long if_shared_ctx_t sctx = ctx->ifc_sctx; 63434c7070dbSScott Long if_softc_ctx_t scctx = &ctx->ifc_softc_ctx; 63443d10e9edSMarius Strobl int admincnt, bar, err, iflib_num_rx_queues, iflib_num_tx_queues; 63453d10e9edSMarius Strobl int msgs, queuemsgs, queues, rx_queues, tx_queues, vectors; 63464c7070dbSScott Long 6347d2735264SStephen Hurd iflib_num_tx_queues = ctx->ifc_sysctl_ntxqs; 6348d2735264SStephen Hurd iflib_num_rx_queues = ctx->ifc_sysctl_nrxqs; 634923ac9029SStephen Hurd 6350b97de13aSMarius Strobl if (bootverbose) 6351b97de13aSMarius Strobl device_printf(dev, "msix_init qsets capped at %d\n", 6352b97de13aSMarius Strobl imax(scctx->isc_ntxqsets, scctx->isc_nrxqsets)); 63531248952aSSean Bruno 63544c7070dbSScott Long /* Override by tuneable */ 6355ea351d3fSSean Bruno if (scctx->isc_disable_msix) 63564c7070dbSScott Long goto msi; 63574c7070dbSScott Long 6358b97de13aSMarius Strobl /* First try MSI-X */ 6359b97de13aSMarius Strobl if ((msgs = pci_msix_count(dev)) == 0) { 6360b97de13aSMarius Strobl if (bootverbose) 6361b97de13aSMarius Strobl device_printf(dev, "MSI-X not supported or disabled\n"); 6362b97de13aSMarius Strobl goto msi; 6363b97de13aSMarius Strobl } 63643d10e9edSMarius Strobl 63653d10e9edSMarius Strobl bar = ctx->ifc_softc_ctx.isc_msix_bar; 63664c7070dbSScott Long /* 63674c7070dbSScott Long * bar == -1 => "trust me I know what I'm doing" 63684c7070dbSScott Long * Some drivers are for hardware that is so shoddily 63694c7070dbSScott Long * documented that no one knows which bars are which 63704c7070dbSScott Long * so the developer has to map all bars. This hack 6371b97de13aSMarius Strobl * allows shoddy garbage to use MSI-X in this framework. 63724c7070dbSScott Long */ 63734c7070dbSScott Long if (bar != -1) { 63744c7070dbSScott Long ctx->ifc_msix_mem = bus_alloc_resource_any(dev, 63754c7070dbSScott Long SYS_RES_MEMORY, &bar, RF_ACTIVE); 63764c7070dbSScott Long if (ctx->ifc_msix_mem == NULL) { 6377b97de13aSMarius Strobl device_printf(dev, "Unable to map MSI-X table\n"); 63784c7070dbSScott Long goto msi; 63794c7070dbSScott Long } 63804c7070dbSScott Long } 63813d10e9edSMarius Strobl 63823d10e9edSMarius Strobl admincnt = sctx->isc_admin_intrcnt; 63834c7070dbSScott Long #if IFLIB_DEBUG 63844c7070dbSScott Long /* use only 1 qset in debug mode */ 63854c7070dbSScott Long queuemsgs = min(msgs - admincnt, 1); 63864c7070dbSScott Long #else 63874c7070dbSScott Long queuemsgs = msgs - admincnt; 63884c7070dbSScott Long #endif 63894c7070dbSScott Long #ifdef RSS 63904c7070dbSScott Long queues = imin(queuemsgs, rss_getnumbuckets()); 63914c7070dbSScott Long #else 63924c7070dbSScott Long queues = queuemsgs; 63934c7070dbSScott Long #endif 63944c7070dbSScott Long queues = imin(CPU_COUNT(&ctx->ifc_cpus), queues); 6395b97de13aSMarius Strobl if (bootverbose) 6396b97de13aSMarius Strobl device_printf(dev, 6397b97de13aSMarius Strobl "intr CPUs: %d queue msgs: %d admincnt: %d\n", 63984c7070dbSScott Long CPU_COUNT(&ctx->ifc_cpus), queuemsgs, admincnt); 63994c7070dbSScott Long #ifdef RSS 64004c7070dbSScott Long /* If we're doing RSS, clamp at the number of RSS buckets */ 64014c7070dbSScott Long if (queues > rss_getnumbuckets()) 64024c7070dbSScott Long queues = rss_getnumbuckets(); 64034c7070dbSScott Long #endif 640423ac9029SStephen Hurd if (iflib_num_rx_queues > 0 && iflib_num_rx_queues < queuemsgs - admincnt) 640523ac9029SStephen Hurd rx_queues = iflib_num_rx_queues; 64064c7070dbSScott Long else 64074c7070dbSScott Long rx_queues = queues; 6408d2735264SStephen Hurd 6409d2735264SStephen Hurd if (rx_queues > scctx->isc_nrxqsets) 6410d2735264SStephen Hurd rx_queues = scctx->isc_nrxqsets; 6411d2735264SStephen Hurd 641223ac9029SStephen Hurd /* 641323ac9029SStephen Hurd * We want this to be all logical CPUs by default 641423ac9029SStephen Hurd */ 64154c7070dbSScott Long if (iflib_num_tx_queues > 0 && iflib_num_tx_queues < queues) 64164c7070dbSScott Long tx_queues = iflib_num_tx_queues; 64174c7070dbSScott Long else 641823ac9029SStephen Hurd tx_queues = mp_ncpus; 641923ac9029SStephen Hurd 6420d2735264SStephen Hurd if (tx_queues > scctx->isc_ntxqsets) 6421d2735264SStephen Hurd tx_queues = scctx->isc_ntxqsets; 6422d2735264SStephen Hurd 642323ac9029SStephen Hurd if (ctx->ifc_sysctl_qs_eq_override == 0) { 642423ac9029SStephen Hurd #ifdef INVARIANTS 642523ac9029SStephen Hurd if (tx_queues != rx_queues) 642677c1fcecSEric Joyner device_printf(dev, 642777c1fcecSEric Joyner "queue equality override not set, capping rx_queues at %d and tx_queues at %d\n", 642823ac9029SStephen Hurd min(rx_queues, tx_queues), min(rx_queues, tx_queues)); 642923ac9029SStephen Hurd #endif 643023ac9029SStephen Hurd tx_queues = min(rx_queues, tx_queues); 643123ac9029SStephen Hurd rx_queues = min(rx_queues, tx_queues); 643223ac9029SStephen Hurd } 64334c7070dbSScott Long 64343d10e9edSMarius Strobl vectors = rx_queues + admincnt; 64353d10e9edSMarius Strobl if (msgs < vectors) { 64363d10e9edSMarius Strobl device_printf(dev, 64373d10e9edSMarius Strobl "insufficient number of MSI-X vectors " 64383d10e9edSMarius Strobl "(supported %d, need %d)\n", msgs, vectors); 64393d10e9edSMarius Strobl goto msi; 64403d10e9edSMarius Strobl } 64413d10e9edSMarius Strobl 64421722eeacSMarius Strobl device_printf(dev, "Using %d RX queues %d TX queues\n", rx_queues, 64431722eeacSMarius Strobl tx_queues); 64443d10e9edSMarius Strobl msgs = vectors; 64454c7070dbSScott Long if ((err = pci_alloc_msix(dev, &vectors)) == 0) { 64463d10e9edSMarius Strobl if (vectors != msgs) { 64473d10e9edSMarius Strobl device_printf(dev, 64483d10e9edSMarius Strobl "Unable to allocate sufficient MSI-X vectors " 64493d10e9edSMarius Strobl "(got %d, need %d)\n", vectors, msgs); 64503d10e9edSMarius Strobl pci_release_msi(dev); 64513d10e9edSMarius Strobl if (bar != -1) { 64523d10e9edSMarius Strobl bus_release_resource(dev, SYS_RES_MEMORY, bar, 64533d10e9edSMarius Strobl ctx->ifc_msix_mem); 64543d10e9edSMarius Strobl ctx->ifc_msix_mem = NULL; 64553d10e9edSMarius Strobl } 64563d10e9edSMarius Strobl goto msi; 64573d10e9edSMarius Strobl } 6458b97de13aSMarius Strobl device_printf(dev, "Using MSI-X interrupts with %d vectors\n", 6459b97de13aSMarius Strobl vectors); 64604c7070dbSScott Long scctx->isc_vectors = vectors; 64614c7070dbSScott Long scctx->isc_nrxqsets = rx_queues; 64624c7070dbSScott Long scctx->isc_ntxqsets = tx_queues; 64634c7070dbSScott Long scctx->isc_intr = IFLIB_INTR_MSIX; 646423ac9029SStephen Hurd 64654c7070dbSScott Long return (vectors); 64664c7070dbSScott Long } else { 646777c1fcecSEric Joyner device_printf(dev, 64683d10e9edSMarius Strobl "failed to allocate %d MSI-X vectors, err: %d\n", vectors, 64693d10e9edSMarius Strobl err); 64703d10e9edSMarius Strobl if (bar != -1) { 6471e4defe55SMarius Strobl bus_release_resource(dev, SYS_RES_MEMORY, bar, 6472e4defe55SMarius Strobl ctx->ifc_msix_mem); 6473e4defe55SMarius Strobl ctx->ifc_msix_mem = NULL; 64744c7070dbSScott Long } 64753d10e9edSMarius Strobl } 64763d10e9edSMarius Strobl 64774c7070dbSScott Long msi: 64784c7070dbSScott Long vectors = pci_msi_count(dev); 64794c7070dbSScott Long scctx->isc_nrxqsets = 1; 64804c7070dbSScott Long scctx->isc_ntxqsets = 1; 64814c7070dbSScott Long scctx->isc_vectors = vectors; 64824c7070dbSScott Long if (vectors == 1 && pci_alloc_msi(dev, &vectors) == 0) { 64834c7070dbSScott Long device_printf(dev,"Using an MSI interrupt\n"); 64844c7070dbSScott Long scctx->isc_intr = IFLIB_INTR_MSI; 64854c7070dbSScott Long } else { 6486e4defe55SMarius Strobl scctx->isc_vectors = 1; 64874c7070dbSScott Long device_printf(dev,"Using a Legacy interrupt\n"); 64884c7070dbSScott Long scctx->isc_intr = IFLIB_INTR_LEGACY; 64894c7070dbSScott Long } 64904c7070dbSScott Long 64914c7070dbSScott Long return (vectors); 64924c7070dbSScott Long } 64934c7070dbSScott Long 6494e4defe55SMarius Strobl static const char *ring_states[] = { "IDLE", "BUSY", "STALLED", "ABDICATED" }; 64954c7070dbSScott Long 64964c7070dbSScott Long static int 64974c7070dbSScott Long mp_ring_state_handler(SYSCTL_HANDLER_ARGS) 64984c7070dbSScott Long { 64994c7070dbSScott Long int rc; 65004c7070dbSScott Long uint16_t *state = ((uint16_t *)oidp->oid_arg1); 65014c7070dbSScott Long struct sbuf *sb; 6502e4defe55SMarius Strobl const char *ring_state = "UNKNOWN"; 65034c7070dbSScott Long 65044c7070dbSScott Long /* XXX needed ? */ 65054c7070dbSScott Long rc = sysctl_wire_old_buffer(req, 0); 65064c7070dbSScott Long MPASS(rc == 0); 65074c7070dbSScott Long if (rc != 0) 65084c7070dbSScott Long return (rc); 65094c7070dbSScott Long sb = sbuf_new_for_sysctl(NULL, NULL, 80, req); 65104c7070dbSScott Long MPASS(sb != NULL); 65114c7070dbSScott Long if (sb == NULL) 65124c7070dbSScott Long return (ENOMEM); 65134c7070dbSScott Long if (state[3] <= 3) 65144c7070dbSScott Long ring_state = ring_states[state[3]]; 65154c7070dbSScott Long 65164c7070dbSScott Long sbuf_printf(sb, "pidx_head: %04hd pidx_tail: %04hd cidx: %04hd state: %s", 65174c7070dbSScott Long state[0], state[1], state[2], ring_state); 65184c7070dbSScott Long rc = sbuf_finish(sb); 65194c7070dbSScott Long sbuf_delete(sb); 65204c7070dbSScott Long return(rc); 65214c7070dbSScott Long } 65224c7070dbSScott Long 652323ac9029SStephen Hurd enum iflib_ndesc_handler { 652423ac9029SStephen Hurd IFLIB_NTXD_HANDLER, 652523ac9029SStephen Hurd IFLIB_NRXD_HANDLER, 652623ac9029SStephen Hurd }; 65274c7070dbSScott Long 652823ac9029SStephen Hurd static int 652923ac9029SStephen Hurd mp_ndesc_handler(SYSCTL_HANDLER_ARGS) 653023ac9029SStephen Hurd { 653123ac9029SStephen Hurd if_ctx_t ctx = (void *)arg1; 653223ac9029SStephen Hurd enum iflib_ndesc_handler type = arg2; 653323ac9029SStephen Hurd char buf[256] = {0}; 653495246abbSSean Bruno qidx_t *ndesc; 653523ac9029SStephen Hurd char *p, *next; 653623ac9029SStephen Hurd int nqs, rc, i; 653723ac9029SStephen Hurd 653823ac9029SStephen Hurd nqs = 8; 653923ac9029SStephen Hurd switch(type) { 654023ac9029SStephen Hurd case IFLIB_NTXD_HANDLER: 654123ac9029SStephen Hurd ndesc = ctx->ifc_sysctl_ntxds; 654223ac9029SStephen Hurd if (ctx->ifc_sctx) 654323ac9029SStephen Hurd nqs = ctx->ifc_sctx->isc_ntxqs; 654423ac9029SStephen Hurd break; 654523ac9029SStephen Hurd case IFLIB_NRXD_HANDLER: 654623ac9029SStephen Hurd ndesc = ctx->ifc_sysctl_nrxds; 654723ac9029SStephen Hurd if (ctx->ifc_sctx) 654823ac9029SStephen Hurd nqs = ctx->ifc_sctx->isc_nrxqs; 654923ac9029SStephen Hurd break; 65501ae4848cSMatt Macy default: 65513d10e9edSMarius Strobl printf("%s: unhandled type\n", __func__); 65523d10e9edSMarius Strobl return (EINVAL); 655323ac9029SStephen Hurd } 655423ac9029SStephen Hurd if (nqs == 0) 655523ac9029SStephen Hurd nqs = 8; 655623ac9029SStephen Hurd 655723ac9029SStephen Hurd for (i=0; i<8; i++) { 655823ac9029SStephen Hurd if (i >= nqs) 655923ac9029SStephen Hurd break; 656023ac9029SStephen Hurd if (i) 656123ac9029SStephen Hurd strcat(buf, ","); 656223ac9029SStephen Hurd sprintf(strchr(buf, 0), "%d", ndesc[i]); 656323ac9029SStephen Hurd } 656423ac9029SStephen Hurd 656523ac9029SStephen Hurd rc = sysctl_handle_string(oidp, buf, sizeof(buf), req); 656623ac9029SStephen Hurd if (rc || req->newptr == NULL) 656723ac9029SStephen Hurd return rc; 656823ac9029SStephen Hurd 656923ac9029SStephen Hurd for (i = 0, next = buf, p = strsep(&next, " ,"); i < 8 && p; 657023ac9029SStephen Hurd i++, p = strsep(&next, " ,")) { 657123ac9029SStephen Hurd ndesc[i] = strtoul(p, NULL, 10); 657223ac9029SStephen Hurd } 657323ac9029SStephen Hurd 657423ac9029SStephen Hurd return(rc); 657523ac9029SStephen Hurd } 65764c7070dbSScott Long 65774c7070dbSScott Long #define NAME_BUFLEN 32 65784c7070dbSScott Long static void 65794c7070dbSScott Long iflib_add_device_sysctl_pre(if_ctx_t ctx) 65804c7070dbSScott Long { 65814c7070dbSScott Long device_t dev = iflib_get_dev(ctx); 65824c7070dbSScott Long struct sysctl_oid_list *child, *oid_list; 65834c7070dbSScott Long struct sysctl_ctx_list *ctx_list; 65844c7070dbSScott Long struct sysctl_oid *node; 65854c7070dbSScott Long 65864c7070dbSScott Long ctx_list = device_get_sysctl_ctx(dev); 65874c7070dbSScott Long child = SYSCTL_CHILDREN(device_get_sysctl_tree(dev)); 65884c7070dbSScott Long ctx->ifc_sysctl_node = node = SYSCTL_ADD_NODE(ctx_list, child, OID_AUTO, "iflib", 65897029da5cSPawel Biernacki CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "IFLIB fields"); 65904c7070dbSScott Long oid_list = SYSCTL_CHILDREN(node); 65914c7070dbSScott Long 659210a1e981SEric Joyner SYSCTL_ADD_CONST_STRING(ctx_list, oid_list, OID_AUTO, "driver_version", 659310a1e981SEric Joyner CTLFLAG_RD, ctx->ifc_sctx->isc_driver_version, 659423ac9029SStephen Hurd "driver version"); 659523ac9029SStephen Hurd 65964c7070dbSScott Long SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "override_ntxqs", 65974c7070dbSScott Long CTLFLAG_RWTUN, &ctx->ifc_sysctl_ntxqs, 0, 65984c7070dbSScott Long "# of txqs to use, 0 => use default #"); 65994c7070dbSScott Long SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "override_nrxqs", 660023ac9029SStephen Hurd CTLFLAG_RWTUN, &ctx->ifc_sysctl_nrxqs, 0, 660123ac9029SStephen Hurd "# of rxqs to use, 0 => use default #"); 660223ac9029SStephen Hurd SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "override_qs_enable", 660323ac9029SStephen Hurd CTLFLAG_RWTUN, &ctx->ifc_sysctl_qs_eq_override, 0, 660423ac9029SStephen Hurd "permit #txq != #rxq"); 6605ea351d3fSSean Bruno SYSCTL_ADD_INT(ctx_list, oid_list, OID_AUTO, "disable_msix", 6606ea351d3fSSean Bruno CTLFLAG_RWTUN, &ctx->ifc_softc_ctx.isc_disable_msix, 0, 6607b97de13aSMarius Strobl "disable MSI-X (default 0)"); 6608f4d2154eSStephen Hurd SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "rx_budget", 6609f4d2154eSStephen Hurd CTLFLAG_RWTUN, &ctx->ifc_sysctl_rx_budget, 0, 66101722eeacSMarius Strobl "set the RX budget"); 6611fe51d4cdSStephen Hurd SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "tx_abdicate", 6612fe51d4cdSStephen Hurd CTLFLAG_RWTUN, &ctx->ifc_sysctl_tx_abdicate, 0, 66131722eeacSMarius Strobl "cause TX to abdicate instead of running to completion"); 6614f154ece0SStephen Hurd ctx->ifc_sysctl_core_offset = CORE_OFFSET_UNSPECIFIED; 6615f154ece0SStephen Hurd SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "core_offset", 6616f154ece0SStephen Hurd CTLFLAG_RDTUN, &ctx->ifc_sysctl_core_offset, 0, 6617f154ece0SStephen Hurd "offset to start using cores at"); 6618f154ece0SStephen Hurd SYSCTL_ADD_U8(ctx_list, oid_list, OID_AUTO, "separate_txrx", 6619f154ece0SStephen Hurd CTLFLAG_RDTUN, &ctx->ifc_sysctl_separate_txrx, 0, 6620f154ece0SStephen Hurd "use separate cores for TX and RX"); 66214c7070dbSScott Long 662223ac9029SStephen Hurd /* XXX change for per-queue sizes */ 662323ac9029SStephen Hurd SYSCTL_ADD_PROC(ctx_list, oid_list, OID_AUTO, "override_ntxds", 66247029da5cSPawel Biernacki CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT, ctx, 66257029da5cSPawel Biernacki IFLIB_NTXD_HANDLER, mp_ndesc_handler, "A", 66261722eeacSMarius Strobl "list of # of TX descriptors to use, 0 = use default #"); 662723ac9029SStephen Hurd SYSCTL_ADD_PROC(ctx_list, oid_list, OID_AUTO, "override_nrxds", 66287029da5cSPawel Biernacki CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT, ctx, 66297029da5cSPawel Biernacki IFLIB_NRXD_HANDLER, mp_ndesc_handler, "A", 66301722eeacSMarius Strobl "list of # of RX descriptors to use, 0 = use default #"); 66314c7070dbSScott Long } 66324c7070dbSScott Long 66334c7070dbSScott Long static void 66344c7070dbSScott Long iflib_add_device_sysctl_post(if_ctx_t ctx) 66354c7070dbSScott Long { 66364c7070dbSScott Long if_shared_ctx_t sctx = ctx->ifc_sctx; 66374c7070dbSScott Long if_softc_ctx_t scctx = &ctx->ifc_softc_ctx; 66384c7070dbSScott Long device_t dev = iflib_get_dev(ctx); 66394c7070dbSScott Long struct sysctl_oid_list *child; 66404c7070dbSScott Long struct sysctl_ctx_list *ctx_list; 66414c7070dbSScott Long iflib_fl_t fl; 66424c7070dbSScott Long iflib_txq_t txq; 66434c7070dbSScott Long iflib_rxq_t rxq; 66444c7070dbSScott Long int i, j; 66454c7070dbSScott Long char namebuf[NAME_BUFLEN]; 66464c7070dbSScott Long char *qfmt; 66474c7070dbSScott Long struct sysctl_oid *queue_node, *fl_node, *node; 66484c7070dbSScott Long struct sysctl_oid_list *queue_list, *fl_list; 66494c7070dbSScott Long ctx_list = device_get_sysctl_ctx(dev); 66504c7070dbSScott Long 66514c7070dbSScott Long node = ctx->ifc_sysctl_node; 66524c7070dbSScott Long child = SYSCTL_CHILDREN(node); 66534c7070dbSScott Long 66544c7070dbSScott Long if (scctx->isc_ntxqsets > 100) 66554c7070dbSScott Long qfmt = "txq%03d"; 66564c7070dbSScott Long else if (scctx->isc_ntxqsets > 10) 66574c7070dbSScott Long qfmt = "txq%02d"; 66584c7070dbSScott Long else 66594c7070dbSScott Long qfmt = "txq%d"; 66604c7070dbSScott Long for (i = 0, txq = ctx->ifc_txqs; i < scctx->isc_ntxqsets; i++, txq++) { 66614c7070dbSScott Long snprintf(namebuf, NAME_BUFLEN, qfmt, i); 66624c7070dbSScott Long queue_node = SYSCTL_ADD_NODE(ctx_list, child, OID_AUTO, namebuf, 66637029da5cSPawel Biernacki CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "Queue Name"); 66644c7070dbSScott Long queue_list = SYSCTL_CHILDREN(queue_node); 66654c7070dbSScott Long #if MEMORY_LOGGING 66664c7070dbSScott Long SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "txq_dequeued", 66674c7070dbSScott Long CTLFLAG_RD, 66684c7070dbSScott Long &txq->ift_dequeued, "total mbufs freed"); 66694c7070dbSScott Long SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "txq_enqueued", 66704c7070dbSScott Long CTLFLAG_RD, 66714c7070dbSScott Long &txq->ift_enqueued, "total mbufs enqueued"); 66724c7070dbSScott Long #endif 66734c7070dbSScott Long SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "mbuf_defrag", 66744c7070dbSScott Long CTLFLAG_RD, 66754c7070dbSScott Long &txq->ift_mbuf_defrag, "# of times m_defrag was called"); 66764c7070dbSScott Long SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "m_pullups", 66774c7070dbSScott Long CTLFLAG_RD, 66784c7070dbSScott Long &txq->ift_pullups, "# of times m_pullup was called"); 66794c7070dbSScott Long SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "mbuf_defrag_failed", 66804c7070dbSScott Long CTLFLAG_RD, 66814c7070dbSScott Long &txq->ift_mbuf_defrag_failed, "# of times m_defrag failed"); 66824c7070dbSScott Long SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "no_desc_avail", 66834c7070dbSScott Long CTLFLAG_RD, 668423ac9029SStephen Hurd &txq->ift_no_desc_avail, "# of times no descriptors were available"); 66854c7070dbSScott Long SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "tx_map_failed", 66864c7070dbSScott Long CTLFLAG_RD, 66871722eeacSMarius Strobl &txq->ift_map_failed, "# of times DMA map failed"); 66884c7070dbSScott Long SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "txd_encap_efbig", 66894c7070dbSScott Long CTLFLAG_RD, 66904c7070dbSScott Long &txq->ift_txd_encap_efbig, "# of times txd_encap returned EFBIG"); 66914c7070dbSScott Long SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "no_tx_dma_setup", 66924c7070dbSScott Long CTLFLAG_RD, 66934c7070dbSScott Long &txq->ift_no_tx_dma_setup, "# of times map failed for other than EFBIG"); 66944c7070dbSScott Long SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "txq_pidx", 66954c7070dbSScott Long CTLFLAG_RD, 66964c7070dbSScott Long &txq->ift_pidx, 1, "Producer Index"); 66974c7070dbSScott Long SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "txq_cidx", 66984c7070dbSScott Long CTLFLAG_RD, 66994c7070dbSScott Long &txq->ift_cidx, 1, "Consumer Index"); 67004c7070dbSScott Long SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "txq_cidx_processed", 67014c7070dbSScott Long CTLFLAG_RD, 67024c7070dbSScott Long &txq->ift_cidx_processed, 1, "Consumer Index seen by credit update"); 67034c7070dbSScott Long SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "txq_in_use", 67044c7070dbSScott Long CTLFLAG_RD, 67054c7070dbSScott Long &txq->ift_in_use, 1, "descriptors in use"); 67064c7070dbSScott Long SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "txq_processed", 67074c7070dbSScott Long CTLFLAG_RD, 67084c7070dbSScott Long &txq->ift_processed, "descriptors procesed for clean"); 67094c7070dbSScott Long SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "txq_cleaned", 67104c7070dbSScott Long CTLFLAG_RD, 67114c7070dbSScott Long &txq->ift_cleaned, "total cleaned"); 67124c7070dbSScott Long SYSCTL_ADD_PROC(ctx_list, queue_list, OID_AUTO, "ring_state", 67137029da5cSPawel Biernacki CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT, 67147029da5cSPawel Biernacki __DEVOLATILE(uint64_t *, &txq->ift_br->state), 0, 67157029da5cSPawel Biernacki mp_ring_state_handler, "A", "soft ring state"); 67164c7070dbSScott Long SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_enqueues", 671795246abbSSean Bruno CTLFLAG_RD, &txq->ift_br->enqueues, 67184c7070dbSScott Long "# of enqueues to the mp_ring for this queue"); 67194c7070dbSScott Long SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_drops", 672095246abbSSean Bruno CTLFLAG_RD, &txq->ift_br->drops, 67214c7070dbSScott Long "# of drops in the mp_ring for this queue"); 67224c7070dbSScott Long SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_starts", 672395246abbSSean Bruno CTLFLAG_RD, &txq->ift_br->starts, 67244c7070dbSScott Long "# of normal consumer starts in the mp_ring for this queue"); 67254c7070dbSScott Long SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_stalls", 672695246abbSSean Bruno CTLFLAG_RD, &txq->ift_br->stalls, 67274c7070dbSScott Long "# of consumer stalls in the mp_ring for this queue"); 67284c7070dbSScott Long SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_restarts", 672995246abbSSean Bruno CTLFLAG_RD, &txq->ift_br->restarts, 67304c7070dbSScott Long "# of consumer restarts in the mp_ring for this queue"); 67314c7070dbSScott Long SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_abdications", 673295246abbSSean Bruno CTLFLAG_RD, &txq->ift_br->abdications, 67334c7070dbSScott Long "# of consumer abdications in the mp_ring for this queue"); 67344c7070dbSScott Long } 67354c7070dbSScott Long 67364c7070dbSScott Long if (scctx->isc_nrxqsets > 100) 67374c7070dbSScott Long qfmt = "rxq%03d"; 67384c7070dbSScott Long else if (scctx->isc_nrxqsets > 10) 67394c7070dbSScott Long qfmt = "rxq%02d"; 67404c7070dbSScott Long else 67414c7070dbSScott Long qfmt = "rxq%d"; 67424c7070dbSScott Long for (i = 0, rxq = ctx->ifc_rxqs; i < scctx->isc_nrxqsets; i++, rxq++) { 67434c7070dbSScott Long snprintf(namebuf, NAME_BUFLEN, qfmt, i); 67444c7070dbSScott Long queue_node = SYSCTL_ADD_NODE(ctx_list, child, OID_AUTO, namebuf, 67457029da5cSPawel Biernacki CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "Queue Name"); 67464c7070dbSScott Long queue_list = SYSCTL_CHILDREN(queue_node); 674723ac9029SStephen Hurd if (sctx->isc_flags & IFLIB_HAS_RXCQ) { 67484c7070dbSScott Long SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "rxq_cq_cidx", 67494c7070dbSScott Long CTLFLAG_RD, 67504c7070dbSScott Long &rxq->ifr_cq_cidx, 1, "Consumer Index"); 67514c7070dbSScott Long } 6752da69b8f9SSean Bruno 67534c7070dbSScott Long for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++) { 67544c7070dbSScott Long snprintf(namebuf, NAME_BUFLEN, "rxq_fl%d", j); 67554c7070dbSScott Long fl_node = SYSCTL_ADD_NODE(ctx_list, queue_list, OID_AUTO, namebuf, 67567029da5cSPawel Biernacki CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "freelist Name"); 67574c7070dbSScott Long fl_list = SYSCTL_CHILDREN(fl_node); 67584c7070dbSScott Long SYSCTL_ADD_U16(ctx_list, fl_list, OID_AUTO, "pidx", 67594c7070dbSScott Long CTLFLAG_RD, 67604c7070dbSScott Long &fl->ifl_pidx, 1, "Producer Index"); 67614c7070dbSScott Long SYSCTL_ADD_U16(ctx_list, fl_list, OID_AUTO, "cidx", 67624c7070dbSScott Long CTLFLAG_RD, 67634c7070dbSScott Long &fl->ifl_cidx, 1, "Consumer Index"); 67644c7070dbSScott Long SYSCTL_ADD_U16(ctx_list, fl_list, OID_AUTO, "credits", 67654c7070dbSScott Long CTLFLAG_RD, 67664c7070dbSScott Long &fl->ifl_credits, 1, "credits available"); 6767b3813609SPatrick Kelsey SYSCTL_ADD_U16(ctx_list, fl_list, OID_AUTO, "buf_size", 6768b3813609SPatrick Kelsey CTLFLAG_RD, 6769b3813609SPatrick Kelsey &fl->ifl_buf_size, 1, "buffer size"); 67704c7070dbSScott Long #if MEMORY_LOGGING 67714c7070dbSScott Long SYSCTL_ADD_QUAD(ctx_list, fl_list, OID_AUTO, "fl_m_enqueued", 67724c7070dbSScott Long CTLFLAG_RD, 67734c7070dbSScott Long &fl->ifl_m_enqueued, "mbufs allocated"); 67744c7070dbSScott Long SYSCTL_ADD_QUAD(ctx_list, fl_list, OID_AUTO, "fl_m_dequeued", 67754c7070dbSScott Long CTLFLAG_RD, 67764c7070dbSScott Long &fl->ifl_m_dequeued, "mbufs freed"); 67774c7070dbSScott Long SYSCTL_ADD_QUAD(ctx_list, fl_list, OID_AUTO, "fl_cl_enqueued", 67784c7070dbSScott Long CTLFLAG_RD, 67794c7070dbSScott Long &fl->ifl_cl_enqueued, "clusters allocated"); 67804c7070dbSScott Long SYSCTL_ADD_QUAD(ctx_list, fl_list, OID_AUTO, "fl_cl_dequeued", 67814c7070dbSScott Long CTLFLAG_RD, 67824c7070dbSScott Long &fl->ifl_cl_dequeued, "clusters freed"); 67834c7070dbSScott Long #endif 67844c7070dbSScott Long } 67854c7070dbSScott Long } 67864c7070dbSScott Long 67874c7070dbSScott Long } 678895246abbSSean Bruno 678977c1fcecSEric Joyner void 679077c1fcecSEric Joyner iflib_request_reset(if_ctx_t ctx) 679177c1fcecSEric Joyner { 679277c1fcecSEric Joyner 679377c1fcecSEric Joyner STATE_LOCK(ctx); 679477c1fcecSEric Joyner ctx->ifc_flags |= IFC_DO_RESET; 679577c1fcecSEric Joyner STATE_UNLOCK(ctx); 679677c1fcecSEric Joyner } 679777c1fcecSEric Joyner 679895246abbSSean Bruno #ifndef __NO_STRICT_ALIGNMENT 679995246abbSSean Bruno static struct mbuf * 680095246abbSSean Bruno iflib_fixup_rx(struct mbuf *m) 680195246abbSSean Bruno { 680295246abbSSean Bruno struct mbuf *n; 680395246abbSSean Bruno 680495246abbSSean Bruno if (m->m_len <= (MCLBYTES - ETHER_HDR_LEN)) { 680595246abbSSean Bruno bcopy(m->m_data, m->m_data + ETHER_HDR_LEN, m->m_len); 680695246abbSSean Bruno m->m_data += ETHER_HDR_LEN; 680795246abbSSean Bruno n = m; 680895246abbSSean Bruno } else { 680995246abbSSean Bruno MGETHDR(n, M_NOWAIT, MT_DATA); 681095246abbSSean Bruno if (n == NULL) { 681195246abbSSean Bruno m_freem(m); 681295246abbSSean Bruno return (NULL); 681395246abbSSean Bruno } 681495246abbSSean Bruno bcopy(m->m_data, n->m_data, ETHER_HDR_LEN); 681595246abbSSean Bruno m->m_data += ETHER_HDR_LEN; 681695246abbSSean Bruno m->m_len -= ETHER_HDR_LEN; 681795246abbSSean Bruno n->m_len = ETHER_HDR_LEN; 681895246abbSSean Bruno M_MOVE_PKTHDR(n, m); 681995246abbSSean Bruno n->m_next = m; 682095246abbSSean Bruno } 682195246abbSSean Bruno return (n); 682295246abbSSean Bruno } 682395246abbSSean Bruno #endif 682494618825SMark Johnston 68257790c8c1SConrad Meyer #ifdef DEBUGNET 682694618825SMark Johnston static void 68277790c8c1SConrad Meyer iflib_debugnet_init(if_t ifp, int *nrxr, int *ncl, int *clsize) 682894618825SMark Johnston { 682994618825SMark Johnston if_ctx_t ctx; 683094618825SMark Johnston 683194618825SMark Johnston ctx = if_getsoftc(ifp); 683294618825SMark Johnston CTX_LOCK(ctx); 683394618825SMark Johnston *nrxr = NRXQSETS(ctx); 683494618825SMark Johnston *ncl = ctx->ifc_rxqs[0].ifr_fl->ifl_size; 683594618825SMark Johnston *clsize = ctx->ifc_rxqs[0].ifr_fl->ifl_buf_size; 683694618825SMark Johnston CTX_UNLOCK(ctx); 683794618825SMark Johnston } 683894618825SMark Johnston 683994618825SMark Johnston static void 68407790c8c1SConrad Meyer iflib_debugnet_event(if_t ifp, enum debugnet_ev event) 684194618825SMark Johnston { 684294618825SMark Johnston if_ctx_t ctx; 684394618825SMark Johnston if_softc_ctx_t scctx; 684494618825SMark Johnston iflib_fl_t fl; 684594618825SMark Johnston iflib_rxq_t rxq; 684694618825SMark Johnston int i, j; 684794618825SMark Johnston 684894618825SMark Johnston ctx = if_getsoftc(ifp); 684994618825SMark Johnston scctx = &ctx->ifc_softc_ctx; 685094618825SMark Johnston 685194618825SMark Johnston switch (event) { 68527790c8c1SConrad Meyer case DEBUGNET_START: 685394618825SMark Johnston for (i = 0; i < scctx->isc_nrxqsets; i++) { 685494618825SMark Johnston rxq = &ctx->ifc_rxqs[i]; 685594618825SMark Johnston for (j = 0; j < rxq->ifr_nfl; j++) { 685694618825SMark Johnston fl = rxq->ifr_fl; 685794618825SMark Johnston fl->ifl_zone = m_getzone(fl->ifl_buf_size); 685894618825SMark Johnston } 685994618825SMark Johnston } 686094618825SMark Johnston iflib_no_tx_batch = 1; 686194618825SMark Johnston break; 686294618825SMark Johnston default: 686394618825SMark Johnston break; 686494618825SMark Johnston } 686594618825SMark Johnston } 686694618825SMark Johnston 686794618825SMark Johnston static int 68687790c8c1SConrad Meyer iflib_debugnet_transmit(if_t ifp, struct mbuf *m) 686994618825SMark Johnston { 687094618825SMark Johnston if_ctx_t ctx; 687194618825SMark Johnston iflib_txq_t txq; 687294618825SMark Johnston int error; 687394618825SMark Johnston 687494618825SMark Johnston ctx = if_getsoftc(ifp); 687594618825SMark Johnston if ((if_getdrvflags(ifp) & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) != 687694618825SMark Johnston IFF_DRV_RUNNING) 687794618825SMark Johnston return (EBUSY); 687894618825SMark Johnston 687994618825SMark Johnston txq = &ctx->ifc_txqs[0]; 688094618825SMark Johnston error = iflib_encap(txq, &m); 688194618825SMark Johnston if (error == 0) 688294618825SMark Johnston (void)iflib_txd_db_check(ctx, txq, true, txq->ift_in_use); 688394618825SMark Johnston return (error); 688494618825SMark Johnston } 688594618825SMark Johnston 688694618825SMark Johnston static int 68877790c8c1SConrad Meyer iflib_debugnet_poll(if_t ifp, int count) 688894618825SMark Johnston { 68890b8df657SGleb Smirnoff struct epoch_tracker et; 689094618825SMark Johnston if_ctx_t ctx; 689194618825SMark Johnston if_softc_ctx_t scctx; 689294618825SMark Johnston iflib_txq_t txq; 689394618825SMark Johnston int i; 689494618825SMark Johnston 689594618825SMark Johnston ctx = if_getsoftc(ifp); 689694618825SMark Johnston scctx = &ctx->ifc_softc_ctx; 689794618825SMark Johnston 689894618825SMark Johnston if ((if_getdrvflags(ifp) & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) != 689994618825SMark Johnston IFF_DRV_RUNNING) 690094618825SMark Johnston return (EBUSY); 690194618825SMark Johnston 690294618825SMark Johnston txq = &ctx->ifc_txqs[0]; 690394618825SMark Johnston (void)iflib_completed_tx_reclaim(txq, RECLAIM_THRESH(ctx)); 690494618825SMark Johnston 69050b8df657SGleb Smirnoff NET_EPOCH_ENTER(et); 690694618825SMark Johnston for (i = 0; i < scctx->isc_nrxqsets; i++) 690794618825SMark Johnston (void)iflib_rxeof(&ctx->ifc_rxqs[i], 16 /* XXX */); 69080b8df657SGleb Smirnoff NET_EPOCH_EXIT(et); 690994618825SMark Johnston return (0); 691094618825SMark Johnston } 69117790c8c1SConrad Meyer #endif /* DEBUGNET */ 6912