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> 4009f6ff4fSMatt Macy #include <sys/jail.h> 414c7070dbSScott Long #include <sys/kernel.h> 424c7070dbSScott Long #include <sys/lock.h> 4309f6ff4fSMatt Macy #include <sys/md5.h> 444c7070dbSScott Long #include <sys/mutex.h> 454c7070dbSScott Long #include <sys/module.h> 464c7070dbSScott Long #include <sys/kobj.h> 474c7070dbSScott Long #include <sys/rman.h> 4809f6ff4fSMatt Macy #include <sys/proc.h> 494c7070dbSScott Long #include <sys/sbuf.h> 504c7070dbSScott Long #include <sys/smp.h> 514c7070dbSScott Long #include <sys/socket.h> 5209f6ff4fSMatt Macy #include <sys/sockio.h> 534c7070dbSScott Long #include <sys/sysctl.h> 544c7070dbSScott Long #include <sys/syslog.h> 554c7070dbSScott Long #include <sys/taskqueue.h> 5623ac9029SStephen Hurd #include <sys/limits.h> 574c7070dbSScott Long 584c7070dbSScott Long #include <net/if.h> 594c7070dbSScott Long #include <net/if_var.h> 604c7070dbSScott Long #include <net/if_types.h> 614c7070dbSScott Long #include <net/if_media.h> 624c7070dbSScott Long #include <net/bpf.h> 634c7070dbSScott Long #include <net/ethernet.h> 644c7070dbSScott Long #include <net/mp_ring.h> 6535e4e998SStephen Hurd #include <net/vnet.h> 664c7070dbSScott Long 674c7070dbSScott Long #include <netinet/in.h> 684c7070dbSScott Long #include <netinet/in_pcb.h> 694c7070dbSScott Long #include <netinet/tcp_lro.h> 704c7070dbSScott Long #include <netinet/in_systm.h> 714c7070dbSScott Long #include <netinet/if_ether.h> 724c7070dbSScott Long #include <netinet/ip.h> 734c7070dbSScott Long #include <netinet/ip6.h> 744c7070dbSScott Long #include <netinet/tcp.h> 7535e4e998SStephen Hurd #include <netinet/ip_var.h> 7694618825SMark Johnston #include <netinet/netdump/netdump.h> 7735e4e998SStephen Hurd #include <netinet6/ip6_var.h> 784c7070dbSScott Long 794c7070dbSScott Long #include <machine/bus.h> 804c7070dbSScott Long #include <machine/in_cksum.h> 814c7070dbSScott Long 824c7070dbSScott Long #include <vm/vm.h> 834c7070dbSScott Long #include <vm/pmap.h> 844c7070dbSScott Long 854c7070dbSScott Long #include <dev/led/led.h> 864c7070dbSScott Long #include <dev/pci/pcireg.h> 874c7070dbSScott Long #include <dev/pci/pcivar.h> 884c7070dbSScott Long #include <dev/pci/pci_private.h> 894c7070dbSScott Long 904c7070dbSScott Long #include <net/iflib.h> 9109f6ff4fSMatt Macy #include <net/iflib_private.h> 924c7070dbSScott Long 934c7070dbSScott Long #include "ifdi_if.h" 944c7070dbSScott Long 9577c1fcecSEric Joyner #ifdef PCI_IOV 9677c1fcecSEric Joyner #include <dev/pci/pci_iov.h> 9777c1fcecSEric Joyner #endif 9877c1fcecSEric Joyner 9987890dbaSSean Bruno #include <sys/bitstring.h> 1004c7070dbSScott Long /* 10195246abbSSean Bruno * enable accounting of every mbuf as it comes in to and goes out of 10295246abbSSean Bruno * iflib's software descriptor references 1034c7070dbSScott Long */ 1044c7070dbSScott Long #define MEMORY_LOGGING 0 1054c7070dbSScott Long /* 1064c7070dbSScott Long * Enable mbuf vectors for compressing long mbuf chains 1074c7070dbSScott Long */ 1084c7070dbSScott Long 1094c7070dbSScott Long /* 1104c7070dbSScott Long * NB: 1114c7070dbSScott Long * - Prefetching in tx cleaning should perhaps be a tunable. The distance ahead 1124c7070dbSScott Long * we prefetch needs to be determined by the time spent in m_free vis a vis 1134c7070dbSScott Long * the cost of a prefetch. This will of course vary based on the workload: 1144c7070dbSScott Long * - NFLX's m_free path is dominated by vm-based M_EXT manipulation which 1154c7070dbSScott Long * is quite expensive, thus suggesting very little prefetch. 1164c7070dbSScott Long * - small packet forwarding which is just returning a single mbuf to 1174c7070dbSScott Long * UMA will typically be very fast vis a vis the cost of a memory 1184c7070dbSScott Long * access. 1194c7070dbSScott Long */ 1204c7070dbSScott Long 1214c7070dbSScott Long 1224c7070dbSScott Long /* 1234c7070dbSScott Long * File organization: 1244c7070dbSScott Long * - private structures 1254c7070dbSScott Long * - iflib private utility functions 1264c7070dbSScott Long * - ifnet functions 1274c7070dbSScott Long * - vlan registry and other exported functions 1284c7070dbSScott Long * - iflib public core functions 1294c7070dbSScott Long * 1304c7070dbSScott Long * 1314c7070dbSScott Long */ 13209f6ff4fSMatt Macy MALLOC_DEFINE(M_IFLIB, "iflib", "ifnet library"); 1334c7070dbSScott Long 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; 1744c7070dbSScott Long 1754c7070dbSScott Long int ifc_link_state; 1764c7070dbSScott Long int ifc_link_irq; 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; 1864c7070dbSScott Long 1874c7070dbSScott Long struct sysctl_oid *ifc_sysctl_node; 1884c7070dbSScott Long uint16_t ifc_sysctl_ntxqs; 1894c7070dbSScott Long uint16_t ifc_sysctl_nrxqs; 19023ac9029SStephen Hurd uint16_t ifc_sysctl_qs_eq_override; 191f4d2154eSStephen Hurd uint16_t ifc_sysctl_rx_budget; 192fe51d4cdSStephen Hurd uint16_t ifc_sysctl_tx_abdicate; 19323ac9029SStephen Hurd 19495246abbSSean Bruno qidx_t ifc_sysctl_ntxds[8]; 19595246abbSSean Bruno qidx_t ifc_sysctl_nrxds[8]; 1964c7070dbSScott Long struct if_txrx ifc_txrx; 1974c7070dbSScott Long #define isc_txd_encap ifc_txrx.ift_txd_encap 1984c7070dbSScott Long #define isc_txd_flush ifc_txrx.ift_txd_flush 1994c7070dbSScott Long #define isc_txd_credits_update ifc_txrx.ift_txd_credits_update 2004c7070dbSScott Long #define isc_rxd_available ifc_txrx.ift_rxd_available 2014c7070dbSScott Long #define isc_rxd_pkt_get ifc_txrx.ift_rxd_pkt_get 2024c7070dbSScott Long #define isc_rxd_refill ifc_txrx.ift_rxd_refill 2034c7070dbSScott Long #define isc_rxd_flush ifc_txrx.ift_rxd_flush 2044c7070dbSScott Long #define isc_rxd_refill ifc_txrx.ift_rxd_refill 2054c7070dbSScott Long #define isc_rxd_refill ifc_txrx.ift_rxd_refill 2064c7070dbSScott Long #define isc_legacy_intr ifc_txrx.ift_legacy_intr 2074c7070dbSScott Long eventhandler_tag ifc_vlan_attach_event; 2084c7070dbSScott Long eventhandler_tag ifc_vlan_detach_event; 2094c7070dbSScott Long uint8_t ifc_mac[ETHER_ADDR_LEN]; 2104c7070dbSScott Long char ifc_mtx_name[16]; 2114c7070dbSScott Long }; 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 2394c7070dbSScott Long return (&ctx->ifc_media); 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 2524c7070dbSScott Long bcopy(mac, ctx->ifc_mac, 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 2904c7070dbSScott Long /* magic number that should be high enough for any hardware */ 2914c7070dbSScott Long #define IFLIB_MAX_TX_SEGS 128 29295246abbSSean Bruno #define IFLIB_RX_COPY_THRESH 128 2934c7070dbSScott Long #define IFLIB_MAX_RX_REFRESH 32 29495246abbSSean Bruno /* The minimum descriptors per second before we start coalescing */ 29595246abbSSean Bruno #define IFLIB_MIN_DESC_SEC 16384 29695246abbSSean Bruno #define IFLIB_DEFAULT_TX_UPDATE_FREQ 16 2974c7070dbSScott Long #define IFLIB_QUEUE_IDLE 0 2984c7070dbSScott Long #define IFLIB_QUEUE_HUNG 1 2994c7070dbSScott Long #define IFLIB_QUEUE_WORKING 2 30095246abbSSean Bruno /* maximum number of txqs that can share an rx interrupt */ 30195246abbSSean Bruno #define IFLIB_MAX_TX_SHARED_INTR 4 3024c7070dbSScott Long 30395246abbSSean Bruno /* this should really scale with ring size - this is a fairly arbitrary value */ 30495246abbSSean Bruno #define TX_BATCH_SIZE 32 3054c7070dbSScott Long 3064c7070dbSScott Long #define IFLIB_RESTART_BUDGET 8 3074c7070dbSScott Long 3084c7070dbSScott Long 3094c7070dbSScott Long #define CSUM_OFFLOAD (CSUM_IP_TSO|CSUM_IP6_TSO|CSUM_IP| \ 3104c7070dbSScott Long CSUM_IP_UDP|CSUM_IP_TCP|CSUM_IP_SCTP| \ 3114c7070dbSScott Long CSUM_IP6_UDP|CSUM_IP6_TCP|CSUM_IP6_SCTP) 3124c7070dbSScott Long struct iflib_txq { 31395246abbSSean Bruno qidx_t ift_in_use; 31495246abbSSean Bruno qidx_t ift_cidx; 31595246abbSSean Bruno qidx_t ift_cidx_processed; 31695246abbSSean Bruno qidx_t ift_pidx; 3174c7070dbSScott Long uint8_t ift_gen; 31823ac9029SStephen Hurd uint8_t ift_br_offset; 31995246abbSSean Bruno uint16_t ift_npending; 32095246abbSSean Bruno uint16_t ift_db_pending; 32195246abbSSean Bruno uint16_t ift_rs_pending; 3224c7070dbSScott Long /* implicit pad */ 32395246abbSSean Bruno uint8_t ift_txd_size[8]; 3244c7070dbSScott Long uint64_t ift_processed; 3254c7070dbSScott Long uint64_t ift_cleaned; 32695246abbSSean Bruno uint64_t ift_cleaned_prev; 3274c7070dbSScott Long #if MEMORY_LOGGING 3284c7070dbSScott Long uint64_t ift_enqueued; 3294c7070dbSScott Long uint64_t ift_dequeued; 3304c7070dbSScott Long #endif 3314c7070dbSScott Long uint64_t ift_no_tx_dma_setup; 3324c7070dbSScott Long uint64_t ift_no_desc_avail; 3334c7070dbSScott Long uint64_t ift_mbuf_defrag_failed; 3344c7070dbSScott Long uint64_t ift_mbuf_defrag; 3354c7070dbSScott Long uint64_t ift_map_failed; 3364c7070dbSScott Long uint64_t ift_txd_encap_efbig; 3374c7070dbSScott Long uint64_t ift_pullups; 338dd7fbcf1SStephen Hurd uint64_t ift_last_timer_tick; 3394c7070dbSScott Long 3404c7070dbSScott Long struct mtx ift_mtx; 3414c7070dbSScott Long struct mtx ift_db_mtx; 3424c7070dbSScott Long 3434c7070dbSScott Long /* constant values */ 3444c7070dbSScott Long if_ctx_t ift_ctx; 34595246abbSSean Bruno struct ifmp_ring *ift_br; 3464c7070dbSScott Long struct grouptask ift_task; 34795246abbSSean Bruno qidx_t ift_size; 3484c7070dbSScott Long uint16_t ift_id; 3494c7070dbSScott Long struct callout ift_timer; 3504c7070dbSScott Long 35195246abbSSean Bruno if_txsd_vec_t ift_sds; 3524c7070dbSScott Long uint8_t ift_qstatus; 3534c7070dbSScott Long uint8_t ift_closed; 35495246abbSSean Bruno uint8_t ift_update_freq; 3554c7070dbSScott Long struct iflib_filter_info ift_filter_info; 356bfce461eSMarius Strobl bus_dma_tag_t ift_buf_tag; 357bfce461eSMarius Strobl bus_dma_tag_t ift_tso_buf_tag; 3584c7070dbSScott Long iflib_dma_info_t ift_ifdi; 3594c7070dbSScott Long #define MTX_NAME_LEN 16 3604c7070dbSScott Long char ift_mtx_name[MTX_NAME_LEN]; 3614c7070dbSScott Long char ift_db_mtx_name[MTX_NAME_LEN]; 3624c7070dbSScott Long bus_dma_segment_t ift_segs[IFLIB_MAX_TX_SEGS] __aligned(CACHE_LINE_SIZE); 3631248952aSSean Bruno #ifdef IFLIB_DIAGNOSTICS 3641248952aSSean Bruno uint64_t ift_cpu_exec_count[256]; 3651248952aSSean Bruno #endif 3664c7070dbSScott Long } __aligned(CACHE_LINE_SIZE); 3674c7070dbSScott Long 3684c7070dbSScott Long struct iflib_fl { 36995246abbSSean Bruno qidx_t ifl_cidx; 37095246abbSSean Bruno qidx_t ifl_pidx; 37195246abbSSean Bruno qidx_t ifl_credits; 3724c7070dbSScott Long uint8_t ifl_gen; 37395246abbSSean Bruno uint8_t ifl_rxd_size; 3744c7070dbSScott Long #if MEMORY_LOGGING 3754c7070dbSScott Long uint64_t ifl_m_enqueued; 3764c7070dbSScott Long uint64_t ifl_m_dequeued; 3774c7070dbSScott Long uint64_t ifl_cl_enqueued; 3784c7070dbSScott Long uint64_t ifl_cl_dequeued; 3794c7070dbSScott Long #endif 3804c7070dbSScott Long /* implicit pad */ 3814c7070dbSScott Long 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); 3954c7070dbSScott Long caddr_t ifl_vm_addrs[IFLIB_MAX_RX_REFRESH]; 39695246abbSSean Bruno qidx_t ifl_rxd_idxs[IFLIB_MAX_RX_REFRESH]; 3974c7070dbSScott Long } __aligned(CACHE_LINE_SIZE); 3984c7070dbSScott Long 39995246abbSSean Bruno static inline qidx_t 40095246abbSSean Bruno get_inuse(int size, qidx_t cidx, qidx_t pidx, uint8_t gen) 4014c7070dbSScott Long { 40295246abbSSean Bruno qidx_t used; 4034c7070dbSScott Long 4044c7070dbSScott Long if (pidx > cidx) 4054c7070dbSScott Long used = pidx - cidx; 4064c7070dbSScott Long else if (pidx < cidx) 4074c7070dbSScott Long used = size - cidx + pidx; 4084c7070dbSScott Long else if (gen == 0 && pidx == cidx) 4094c7070dbSScott Long used = 0; 4104c7070dbSScott Long else if (gen == 1 && pidx == cidx) 4114c7070dbSScott Long used = size; 4124c7070dbSScott Long else 4134c7070dbSScott Long panic("bad state"); 4144c7070dbSScott Long 4154c7070dbSScott Long return (used); 4164c7070dbSScott Long } 4174c7070dbSScott Long 4184c7070dbSScott Long #define TXQ_AVAIL(txq) (txq->ift_size - get_inuse(txq->ift_size, txq->ift_cidx, txq->ift_pidx, txq->ift_gen)) 4194c7070dbSScott Long 4204c7070dbSScott Long #define IDXDIFF(head, tail, wrap) \ 4214c7070dbSScott Long ((head) >= (tail) ? (head) - (tail) : (wrap) - (tail) + (head)) 4224c7070dbSScott Long 4234c7070dbSScott Long struct iflib_rxq { 4244c7070dbSScott Long /* If there is a separate completion queue - 4254c7070dbSScott Long * these are the cq cidx and pidx. Otherwise 4264c7070dbSScott Long * these are unused. 4274c7070dbSScott Long */ 42895246abbSSean Bruno qidx_t ifr_size; 42995246abbSSean Bruno qidx_t ifr_cq_cidx; 43095246abbSSean Bruno qidx_t ifr_cq_pidx; 4314c7070dbSScott Long uint8_t ifr_cq_gen; 43223ac9029SStephen Hurd uint8_t ifr_fl_offset; 4334c7070dbSScott Long 4344c7070dbSScott Long if_ctx_t ifr_ctx; 4354c7070dbSScott Long iflib_fl_t ifr_fl; 4364c7070dbSScott Long uint64_t ifr_rx_irq; 4374c7070dbSScott Long uint16_t ifr_id; 4384c7070dbSScott Long uint8_t ifr_lro_enabled; 4394c7070dbSScott Long uint8_t ifr_nfl; 44095246abbSSean Bruno uint8_t ifr_ntxqirq; 44195246abbSSean Bruno uint8_t ifr_txqid[IFLIB_MAX_TX_SHARED_INTR]; 4424c7070dbSScott Long struct lro_ctrl ifr_lc; 4434c7070dbSScott Long struct grouptask ifr_task; 4444c7070dbSScott Long struct iflib_filter_info ifr_filter_info; 4454c7070dbSScott Long iflib_dma_info_t ifr_ifdi; 446ab2e3f79SStephen Hurd 4474c7070dbSScott Long /* dynamically allocate if any drivers need a value substantially larger than this */ 4484c7070dbSScott Long struct if_rxd_frag ifr_frags[IFLIB_MAX_RX_SEGS] __aligned(CACHE_LINE_SIZE); 4491248952aSSean Bruno #ifdef IFLIB_DIAGNOSTICS 4501248952aSSean Bruno uint64_t ifr_cpu_exec_count[256]; 4511248952aSSean Bruno #endif 4524c7070dbSScott Long } __aligned(CACHE_LINE_SIZE); 4534c7070dbSScott Long 45495246abbSSean Bruno typedef struct if_rxsd { 45595246abbSSean Bruno caddr_t *ifsd_cl; 45695246abbSSean Bruno struct mbuf **ifsd_m; 45795246abbSSean Bruno iflib_fl_t ifsd_fl; 45895246abbSSean Bruno qidx_t ifsd_cidx; 45995246abbSSean Bruno } *if_rxsd_t; 46095246abbSSean Bruno 46195246abbSSean Bruno /* multiple of word size */ 46295246abbSSean Bruno #ifdef __LP64__ 463ab2e3f79SStephen Hurd #define PKT_INFO_SIZE 6 46495246abbSSean Bruno #define RXD_INFO_SIZE 5 46595246abbSSean Bruno #define PKT_TYPE uint64_t 46695246abbSSean Bruno #else 467ab2e3f79SStephen Hurd #define PKT_INFO_SIZE 11 46895246abbSSean Bruno #define RXD_INFO_SIZE 8 46995246abbSSean Bruno #define PKT_TYPE uint32_t 47095246abbSSean Bruno #endif 47195246abbSSean Bruno #define PKT_LOOP_BOUND ((PKT_INFO_SIZE/3)*3) 47295246abbSSean Bruno #define RXD_LOOP_BOUND ((RXD_INFO_SIZE/4)*4) 47395246abbSSean Bruno 47495246abbSSean Bruno typedef struct if_pkt_info_pad { 47595246abbSSean Bruno PKT_TYPE pkt_val[PKT_INFO_SIZE]; 47695246abbSSean Bruno } *if_pkt_info_pad_t; 47795246abbSSean Bruno typedef struct if_rxd_info_pad { 47895246abbSSean Bruno PKT_TYPE rxd_val[RXD_INFO_SIZE]; 47995246abbSSean Bruno } *if_rxd_info_pad_t; 48095246abbSSean Bruno 48195246abbSSean Bruno CTASSERT(sizeof(struct if_pkt_info_pad) == sizeof(struct if_pkt_info)); 48295246abbSSean Bruno CTASSERT(sizeof(struct if_rxd_info_pad) == sizeof(struct if_rxd_info)); 48395246abbSSean Bruno 48495246abbSSean Bruno 48595246abbSSean Bruno static inline void 48695246abbSSean Bruno pkt_info_zero(if_pkt_info_t pi) 48795246abbSSean Bruno { 48895246abbSSean Bruno if_pkt_info_pad_t pi_pad; 48995246abbSSean Bruno 49095246abbSSean Bruno pi_pad = (if_pkt_info_pad_t)pi; 49195246abbSSean Bruno pi_pad->pkt_val[0] = 0; pi_pad->pkt_val[1] = 0; pi_pad->pkt_val[2] = 0; 49295246abbSSean Bruno pi_pad->pkt_val[3] = 0; pi_pad->pkt_val[4] = 0; pi_pad->pkt_val[5] = 0; 49395246abbSSean Bruno #ifndef __LP64__ 494ab2e3f79SStephen Hurd pi_pad->pkt_val[6] = 0; pi_pad->pkt_val[7] = 0; pi_pad->pkt_val[8] = 0; 495ab2e3f79SStephen Hurd pi_pad->pkt_val[9] = 0; pi_pad->pkt_val[10] = 0; 49695246abbSSean Bruno #endif 49795246abbSSean Bruno } 49895246abbSSean Bruno 49909f6ff4fSMatt Macy static device_method_t iflib_pseudo_methods[] = { 50009f6ff4fSMatt Macy DEVMETHOD(device_attach, noop_attach), 50109f6ff4fSMatt Macy DEVMETHOD(device_detach, iflib_pseudo_detach), 50209f6ff4fSMatt Macy DEVMETHOD_END 50309f6ff4fSMatt Macy }; 50409f6ff4fSMatt Macy 50509f6ff4fSMatt Macy driver_t iflib_pseudodriver = { 50609f6ff4fSMatt Macy "iflib_pseudo", iflib_pseudo_methods, sizeof(struct iflib_ctx), 50709f6ff4fSMatt Macy }; 50809f6ff4fSMatt Macy 50995246abbSSean Bruno static inline void 51095246abbSSean Bruno rxd_info_zero(if_rxd_info_t ri) 51195246abbSSean Bruno { 51295246abbSSean Bruno if_rxd_info_pad_t ri_pad; 51395246abbSSean Bruno int i; 51495246abbSSean Bruno 51595246abbSSean Bruno ri_pad = (if_rxd_info_pad_t)ri; 51695246abbSSean Bruno for (i = 0; i < RXD_LOOP_BOUND; i += 4) { 51795246abbSSean Bruno ri_pad->rxd_val[i] = 0; 51895246abbSSean Bruno ri_pad->rxd_val[i+1] = 0; 51995246abbSSean Bruno ri_pad->rxd_val[i+2] = 0; 52095246abbSSean Bruno ri_pad->rxd_val[i+3] = 0; 52195246abbSSean Bruno } 52295246abbSSean Bruno #ifdef __LP64__ 52395246abbSSean Bruno ri_pad->rxd_val[RXD_INFO_SIZE-1] = 0; 52495246abbSSean Bruno #endif 52595246abbSSean Bruno } 52695246abbSSean Bruno 5274c7070dbSScott Long /* 5284c7070dbSScott Long * Only allow a single packet to take up most 1/nth of the tx ring 5294c7070dbSScott Long */ 5304c7070dbSScott Long #define MAX_SINGLE_PACKET_FRACTION 12 5314c7070dbSScott Long #define IF_BAD_DMA (bus_addr_t)-1 5324c7070dbSScott Long 5334c7070dbSScott Long #define CTX_ACTIVE(ctx) ((if_getdrvflags((ctx)->ifc_ifp) & IFF_DRV_RUNNING)) 5344c7070dbSScott Long 535aa8a24d3SStephen Hurd #define CTX_LOCK_INIT(_sc) sx_init(&(_sc)->ifc_ctx_sx, "iflib ctx lock") 536aa8a24d3SStephen Hurd #define CTX_LOCK(ctx) sx_xlock(&(ctx)->ifc_ctx_sx) 537aa8a24d3SStephen Hurd #define CTX_UNLOCK(ctx) sx_xunlock(&(ctx)->ifc_ctx_sx) 538aa8a24d3SStephen Hurd #define CTX_LOCK_DESTROY(ctx) sx_destroy(&(ctx)->ifc_ctx_sx) 5394c7070dbSScott Long 5407b610b60SSean Bruno 5417b610b60SSean Bruno #define STATE_LOCK_INIT(_sc, _name) mtx_init(&(_sc)->ifc_state_mtx, _name, "iflib state lock", MTX_DEF) 5427b610b60SSean Bruno #define STATE_LOCK(ctx) mtx_lock(&(ctx)->ifc_state_mtx) 5437b610b60SSean Bruno #define STATE_UNLOCK(ctx) mtx_unlock(&(ctx)->ifc_state_mtx) 5447b610b60SSean Bruno #define STATE_LOCK_DESTROY(ctx) mtx_destroy(&(ctx)->ifc_state_mtx) 5457b610b60SSean Bruno 546ab2e3f79SStephen Hurd 5474c7070dbSScott Long 5484c7070dbSScott Long #define CALLOUT_LOCK(txq) mtx_lock(&txq->ift_mtx) 5494c7070dbSScott Long #define CALLOUT_UNLOCK(txq) mtx_unlock(&txq->ift_mtx) 5504c7070dbSScott Long 55177c1fcecSEric Joyner void 55277c1fcecSEric Joyner iflib_set_detach(if_ctx_t ctx) 55377c1fcecSEric Joyner { 55477c1fcecSEric Joyner STATE_LOCK(ctx); 55577c1fcecSEric Joyner ctx->ifc_flags |= IFC_IN_DETACH; 55677c1fcecSEric Joyner STATE_UNLOCK(ctx); 55777c1fcecSEric Joyner } 5584c7070dbSScott Long 5594c7070dbSScott Long /* Our boot-time initialization hook */ 5604c7070dbSScott Long static int iflib_module_event_handler(module_t, int, void *); 5614c7070dbSScott Long 5624c7070dbSScott Long static moduledata_t iflib_moduledata = { 5634c7070dbSScott Long "iflib", 5644c7070dbSScott Long iflib_module_event_handler, 5654c7070dbSScott Long NULL 5664c7070dbSScott Long }; 5674c7070dbSScott Long 5684c7070dbSScott Long DECLARE_MODULE(iflib, iflib_moduledata, SI_SUB_INIT_IF, SI_ORDER_ANY); 5694c7070dbSScott Long MODULE_VERSION(iflib, 1); 5704c7070dbSScott Long 5714c7070dbSScott Long MODULE_DEPEND(iflib, pci, 1, 1, 1); 5724c7070dbSScott Long MODULE_DEPEND(iflib, ether, 1, 1, 1); 5734c7070dbSScott Long 574ab2e3f79SStephen Hurd TASKQGROUP_DEFINE(if_io_tqg, mp_ncpus, 1); 575ab2e3f79SStephen Hurd TASKQGROUP_DEFINE(if_config_tqg, 1, 1); 576ab2e3f79SStephen Hurd 5774c7070dbSScott Long #ifndef IFLIB_DEBUG_COUNTERS 5784c7070dbSScott Long #ifdef INVARIANTS 5794c7070dbSScott Long #define IFLIB_DEBUG_COUNTERS 1 5804c7070dbSScott Long #else 5814c7070dbSScott Long #define IFLIB_DEBUG_COUNTERS 0 5824c7070dbSScott Long #endif /* !INVARIANTS */ 5834c7070dbSScott Long #endif 5844c7070dbSScott Long 585ab2e3f79SStephen Hurd static SYSCTL_NODE(_net, OID_AUTO, iflib, CTLFLAG_RD, 0, 586ab2e3f79SStephen Hurd "iflib driver parameters"); 587ab2e3f79SStephen Hurd 5884c7070dbSScott Long /* 5894c7070dbSScott Long * XXX need to ensure that this can't accidentally cause the head to be moved backwards 5904c7070dbSScott Long */ 5914c7070dbSScott Long static int iflib_min_tx_latency = 0; 5924c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, min_tx_latency, CTLFLAG_RW, 593da69b8f9SSean Bruno &iflib_min_tx_latency, 0, "minimize transmit latency at the possible expense of throughput"); 59495246abbSSean Bruno static int iflib_no_tx_batch = 0; 59595246abbSSean Bruno SYSCTL_INT(_net_iflib, OID_AUTO, no_tx_batch, CTLFLAG_RW, 59695246abbSSean Bruno &iflib_no_tx_batch, 0, "minimize transmit latency at the possible expense of throughput"); 5974c7070dbSScott Long 5984c7070dbSScott Long 5994c7070dbSScott Long #if IFLIB_DEBUG_COUNTERS 6004c7070dbSScott Long 6014c7070dbSScott Long static int iflib_tx_seen; 6024c7070dbSScott Long static int iflib_tx_sent; 6034c7070dbSScott Long static int iflib_tx_encap; 6044c7070dbSScott Long static int iflib_rx_allocs; 6054c7070dbSScott Long static int iflib_fl_refills; 6064c7070dbSScott Long static int iflib_fl_refills_large; 6074c7070dbSScott Long static int iflib_tx_frees; 6084c7070dbSScott Long 6094c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, tx_seen, CTLFLAG_RD, 6104c7070dbSScott Long &iflib_tx_seen, 0, "# tx mbufs seen"); 6114c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, tx_sent, CTLFLAG_RD, 6124c7070dbSScott Long &iflib_tx_sent, 0, "# tx mbufs sent"); 6134c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, tx_encap, CTLFLAG_RD, 6144c7070dbSScott Long &iflib_tx_encap, 0, "# tx mbufs encapped"); 6154c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, tx_frees, CTLFLAG_RD, 6164c7070dbSScott Long &iflib_tx_frees, 0, "# tx frees"); 6174c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rx_allocs, CTLFLAG_RD, 6184c7070dbSScott Long &iflib_rx_allocs, 0, "# rx allocations"); 6194c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, fl_refills, CTLFLAG_RD, 6204c7070dbSScott Long &iflib_fl_refills, 0, "# refills"); 6214c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, fl_refills_large, CTLFLAG_RD, 6224c7070dbSScott Long &iflib_fl_refills_large, 0, "# large refills"); 6234c7070dbSScott Long 6244c7070dbSScott Long 6254c7070dbSScott Long static int iflib_txq_drain_flushing; 6264c7070dbSScott Long static int iflib_txq_drain_oactive; 6274c7070dbSScott Long static int iflib_txq_drain_notready; 6284c7070dbSScott Long 6294c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, txq_drain_flushing, CTLFLAG_RD, 6304c7070dbSScott Long &iflib_txq_drain_flushing, 0, "# drain flushes"); 6314c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, txq_drain_oactive, CTLFLAG_RD, 6324c7070dbSScott Long &iflib_txq_drain_oactive, 0, "# drain oactives"); 6334c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, txq_drain_notready, CTLFLAG_RD, 6344c7070dbSScott Long &iflib_txq_drain_notready, 0, "# drain notready"); 6354c7070dbSScott Long 6364c7070dbSScott Long 6374c7070dbSScott Long static int iflib_encap_load_mbuf_fail; 638d14c853bSStephen Hurd static int iflib_encap_pad_mbuf_fail; 6394c7070dbSScott Long static int iflib_encap_txq_avail_fail; 6404c7070dbSScott Long static int iflib_encap_txd_encap_fail; 6414c7070dbSScott Long 6424c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, encap_load_mbuf_fail, CTLFLAG_RD, 6434c7070dbSScott Long &iflib_encap_load_mbuf_fail, 0, "# busdma load failures"); 644d14c853bSStephen Hurd SYSCTL_INT(_net_iflib, OID_AUTO, encap_pad_mbuf_fail, CTLFLAG_RD, 645d14c853bSStephen Hurd &iflib_encap_pad_mbuf_fail, 0, "# runt frame pad failures"); 6464c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, encap_txq_avail_fail, CTLFLAG_RD, 6474c7070dbSScott Long &iflib_encap_txq_avail_fail, 0, "# txq avail failures"); 6484c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, encap_txd_encap_fail, CTLFLAG_RD, 6494c7070dbSScott Long &iflib_encap_txd_encap_fail, 0, "# driver encap failures"); 6504c7070dbSScott Long 6514c7070dbSScott Long static int iflib_task_fn_rxs; 6524c7070dbSScott Long static int iflib_rx_intr_enables; 6534c7070dbSScott Long static int iflib_fast_intrs; 6544c7070dbSScott Long static int iflib_rx_unavail; 6554c7070dbSScott Long static int iflib_rx_ctx_inactive; 6564c7070dbSScott Long static int iflib_rx_if_input; 6574c7070dbSScott Long static int iflib_rx_mbuf_null; 6584c7070dbSScott Long static int iflib_rxd_flush; 6594c7070dbSScott Long 6604c7070dbSScott Long static int iflib_verbose_debug; 6614c7070dbSScott Long 6624c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, task_fn_rx, CTLFLAG_RD, 6634c7070dbSScott Long &iflib_task_fn_rxs, 0, "# task_fn_rx calls"); 6644c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rx_intr_enables, CTLFLAG_RD, 6654c7070dbSScott Long &iflib_rx_intr_enables, 0, "# rx intr enables"); 6664c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, fast_intrs, CTLFLAG_RD, 6674c7070dbSScott Long &iflib_fast_intrs, 0, "# fast_intr calls"); 6684c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rx_unavail, CTLFLAG_RD, 6694c7070dbSScott Long &iflib_rx_unavail, 0, "# times rxeof called with no available data"); 6704c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rx_ctx_inactive, CTLFLAG_RD, 6714c7070dbSScott Long &iflib_rx_ctx_inactive, 0, "# times rxeof called with inactive context"); 6724c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rx_if_input, CTLFLAG_RD, 6734c7070dbSScott Long &iflib_rx_if_input, 0, "# times rxeof called if_input"); 6744c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rx_mbuf_null, CTLFLAG_RD, 6754c7070dbSScott Long &iflib_rx_mbuf_null, 0, "# times rxeof got null mbuf"); 6764c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rxd_flush, CTLFLAG_RD, 6774c7070dbSScott Long &iflib_rxd_flush, 0, "# times rxd_flush called"); 6784c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, verbose_debug, CTLFLAG_RW, 6794c7070dbSScott Long &iflib_verbose_debug, 0, "enable verbose debugging"); 6804c7070dbSScott Long 6814c7070dbSScott Long #define DBG_COUNTER_INC(name) atomic_add_int(&(iflib_ ## name), 1) 682da69b8f9SSean Bruno static void 683da69b8f9SSean Bruno iflib_debug_reset(void) 684da69b8f9SSean Bruno { 685da69b8f9SSean Bruno iflib_tx_seen = iflib_tx_sent = iflib_tx_encap = iflib_rx_allocs = 686da69b8f9SSean Bruno iflib_fl_refills = iflib_fl_refills_large = iflib_tx_frees = 687da69b8f9SSean Bruno iflib_txq_drain_flushing = iflib_txq_drain_oactive = 68864e6fc13SStephen Hurd iflib_txq_drain_notready = 689d14c853bSStephen Hurd iflib_encap_load_mbuf_fail = iflib_encap_pad_mbuf_fail = 690d14c853bSStephen Hurd iflib_encap_txq_avail_fail = iflib_encap_txd_encap_fail = 691d14c853bSStephen Hurd iflib_task_fn_rxs = iflib_rx_intr_enables = iflib_fast_intrs = 69264e6fc13SStephen Hurd iflib_rx_unavail = 69364e6fc13SStephen Hurd iflib_rx_ctx_inactive = iflib_rx_if_input = 694da69b8f9SSean Bruno iflib_rx_mbuf_null = iflib_rxd_flush = 0; 695da69b8f9SSean Bruno } 6964c7070dbSScott Long 6974c7070dbSScott Long #else 6984c7070dbSScott Long #define DBG_COUNTER_INC(name) 699da69b8f9SSean Bruno static void iflib_debug_reset(void) {} 7004c7070dbSScott Long #endif 7014c7070dbSScott Long 7024c7070dbSScott Long #define IFLIB_DEBUG 0 7034c7070dbSScott Long 7044c7070dbSScott Long static void iflib_tx_structures_free(if_ctx_t ctx); 7054c7070dbSScott Long static void iflib_rx_structures_free(if_ctx_t ctx); 7064c7070dbSScott Long static int iflib_queues_alloc(if_ctx_t ctx); 7074c7070dbSScott Long static int iflib_tx_credits_update(if_ctx_t ctx, iflib_txq_t txq); 70895246abbSSean Bruno static int iflib_rxd_avail(if_ctx_t ctx, iflib_rxq_t rxq, qidx_t cidx, qidx_t budget); 7094c7070dbSScott Long static int iflib_qset_structures_setup(if_ctx_t ctx); 7104c7070dbSScott Long static int iflib_msix_init(if_ctx_t ctx); 7113e0e6330SStephen Hurd static int iflib_legacy_setup(if_ctx_t ctx, driver_filter_t filter, void *filterarg, int *rid, const char *str); 7124c7070dbSScott Long static void iflib_txq_check_drain(iflib_txq_t txq, int budget); 7134c7070dbSScott Long static uint32_t iflib_txq_can_drain(struct ifmp_ring *); 714b8ca4756SPatrick Kelsey #ifdef ALTQ 715b8ca4756SPatrick Kelsey static void iflib_altq_if_start(if_t ifp); 716b8ca4756SPatrick Kelsey static int iflib_altq_if_transmit(if_t ifp, struct mbuf *m); 717b8ca4756SPatrick Kelsey #endif 7184c7070dbSScott Long static int iflib_register(if_ctx_t); 7194c7070dbSScott Long static void iflib_init_locked(if_ctx_t ctx); 7204c7070dbSScott Long static void iflib_add_device_sysctl_pre(if_ctx_t ctx); 7214c7070dbSScott Long static void iflib_add_device_sysctl_post(if_ctx_t ctx); 722da69b8f9SSean Bruno static void iflib_ifmp_purge(iflib_txq_t txq); 7231248952aSSean Bruno static void _iflib_pre_assert(if_softc_ctx_t scctx); 72495246abbSSean Bruno static void iflib_if_init_locked(if_ctx_t ctx); 72577c1fcecSEric Joyner static void iflib_free_intr_mem(if_ctx_t ctx); 72695246abbSSean Bruno #ifndef __NO_STRICT_ALIGNMENT 72795246abbSSean Bruno static struct mbuf * iflib_fixup_rx(struct mbuf *m); 72895246abbSSean Bruno #endif 7294c7070dbSScott Long 73094618825SMark Johnston NETDUMP_DEFINE(iflib); 73194618825SMark Johnston 7324c7070dbSScott Long #ifdef DEV_NETMAP 7334c7070dbSScott Long #include <sys/selinfo.h> 7344c7070dbSScott Long #include <net/netmap.h> 7354c7070dbSScott Long #include <dev/netmap/netmap_kern.h> 7364c7070dbSScott Long 7374c7070dbSScott Long MODULE_DEPEND(iflib, netmap, 1, 1, 1); 7384c7070dbSScott Long 7392d873474SStephen Hurd static int netmap_fl_refill(iflib_rxq_t rxq, struct netmap_kring *kring, uint32_t nm_i, bool init); 7402d873474SStephen Hurd 7414c7070dbSScott Long /* 7424c7070dbSScott Long * device-specific sysctl variables: 7434c7070dbSScott Long * 74491d546a0SConrad Meyer * iflib_crcstrip: 0: keep CRC in rx frames (default), 1: strip it. 7454c7070dbSScott Long * During regular operations the CRC is stripped, but on some 7464c7070dbSScott Long * hardware reception of frames not multiple of 64 is slower, 7474c7070dbSScott Long * so using crcstrip=0 helps in benchmarks. 7484c7070dbSScott Long * 74991d546a0SConrad Meyer * iflib_rx_miss, iflib_rx_miss_bufs: 7504c7070dbSScott Long * count packets that might be missed due to lost interrupts. 7514c7070dbSScott Long */ 7524c7070dbSScott Long SYSCTL_DECL(_dev_netmap); 7534c7070dbSScott Long /* 7544c7070dbSScott Long * The xl driver by default strips CRCs and we do not override it. 7554c7070dbSScott Long */ 7564c7070dbSScott Long 7574c7070dbSScott Long int iflib_crcstrip = 1; 7584c7070dbSScott Long SYSCTL_INT(_dev_netmap, OID_AUTO, iflib_crcstrip, 7594c7070dbSScott Long CTLFLAG_RW, &iflib_crcstrip, 1, "strip CRC on rx frames"); 7604c7070dbSScott Long 7614c7070dbSScott Long int iflib_rx_miss, iflib_rx_miss_bufs; 7624c7070dbSScott Long SYSCTL_INT(_dev_netmap, OID_AUTO, iflib_rx_miss, 7634c7070dbSScott Long CTLFLAG_RW, &iflib_rx_miss, 0, "potentially missed rx intr"); 76491d546a0SConrad Meyer SYSCTL_INT(_dev_netmap, OID_AUTO, iflib_rx_miss_bufs, 7654c7070dbSScott Long CTLFLAG_RW, &iflib_rx_miss_bufs, 0, "potentially missed rx intr bufs"); 7664c7070dbSScott Long 7674c7070dbSScott Long /* 7684c7070dbSScott Long * Register/unregister. We are already under netmap lock. 7694c7070dbSScott Long * Only called on the first register or the last unregister. 7704c7070dbSScott Long */ 7714c7070dbSScott Long static int 7724c7070dbSScott Long iflib_netmap_register(struct netmap_adapter *na, int onoff) 7734c7070dbSScott Long { 7744c7070dbSScott Long struct ifnet *ifp = na->ifp; 7754c7070dbSScott Long if_ctx_t ctx = ifp->if_softc; 77695246abbSSean Bruno int status; 7774c7070dbSScott Long 7784c7070dbSScott Long CTX_LOCK(ctx); 7794c7070dbSScott Long IFDI_INTR_DISABLE(ctx); 7804c7070dbSScott Long 7814c7070dbSScott Long /* Tell the stack that the interface is no longer active */ 7824c7070dbSScott Long ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 7834c7070dbSScott Long 7844c7070dbSScott Long if (!CTX_IS_VF(ctx)) 7851248952aSSean Bruno IFDI_CRCSTRIP_SET(ctx, onoff, iflib_crcstrip); 7864c7070dbSScott Long 7874c7070dbSScott Long /* enable or disable flags and callbacks in na and ifp */ 7884c7070dbSScott Long if (onoff) { 7894c7070dbSScott Long nm_set_native_flags(na); 7904c7070dbSScott Long } else { 7914c7070dbSScott Long nm_clear_native_flags(na); 7924c7070dbSScott Long } 79395246abbSSean Bruno iflib_stop(ctx); 79495246abbSSean Bruno iflib_init_locked(ctx); 7951248952aSSean Bruno IFDI_CRCSTRIP_SET(ctx, onoff, iflib_crcstrip); // XXX why twice ? 79695246abbSSean Bruno status = ifp->if_drv_flags & IFF_DRV_RUNNING ? 0 : 1; 79795246abbSSean Bruno if (status) 79895246abbSSean Bruno nm_clear_native_flags(na); 7994c7070dbSScott Long CTX_UNLOCK(ctx); 80095246abbSSean Bruno return (status); 8014c7070dbSScott Long } 8024c7070dbSScott Long 8032d873474SStephen Hurd static int 8042d873474SStephen Hurd netmap_fl_refill(iflib_rxq_t rxq, struct netmap_kring *kring, uint32_t nm_i, bool init) 8052d873474SStephen Hurd { 8062d873474SStephen Hurd struct netmap_adapter *na = kring->na; 8072d873474SStephen Hurd u_int const lim = kring->nkr_num_slots - 1; 8082d873474SStephen Hurd u_int head = kring->rhead; 8092d873474SStephen Hurd struct netmap_ring *ring = kring->ring; 8102d873474SStephen Hurd bus_dmamap_t *map; 8112d873474SStephen Hurd struct if_rxd_update iru; 8122d873474SStephen Hurd if_ctx_t ctx = rxq->ifr_ctx; 8132d873474SStephen Hurd iflib_fl_t fl = &rxq->ifr_fl[0]; 8142d873474SStephen Hurd uint32_t refill_pidx, nic_i; 81564e6fc13SStephen Hurd #if IFLIB_DEBUG_COUNTERS 81664e6fc13SStephen Hurd int rf_count = 0; 81764e6fc13SStephen Hurd #endif 8182d873474SStephen Hurd 8192d873474SStephen Hurd if (nm_i == head && __predict_true(!init)) 8202d873474SStephen Hurd return 0; 8212d873474SStephen Hurd iru_init(&iru, rxq, 0 /* flid */); 8222d873474SStephen Hurd map = fl->ifl_sds.ifsd_map; 8232d873474SStephen Hurd refill_pidx = netmap_idx_k2n(kring, nm_i); 8242d873474SStephen Hurd /* 8252d873474SStephen Hurd * IMPORTANT: we must leave one free slot in the ring, 8262d873474SStephen Hurd * so move head back by one unit 8272d873474SStephen Hurd */ 8282d873474SStephen Hurd head = nm_prev(head, lim); 8291ae4848cSMatt Macy nic_i = UINT_MAX; 83064e6fc13SStephen Hurd DBG_COUNTER_INC(fl_refills); 8312d873474SStephen Hurd while (nm_i != head) { 83264e6fc13SStephen Hurd #if IFLIB_DEBUG_COUNTERS 83364e6fc13SStephen Hurd if (++rf_count == 9) 83464e6fc13SStephen Hurd DBG_COUNTER_INC(fl_refills_large); 83564e6fc13SStephen Hurd #endif 8362d873474SStephen Hurd for (int tmp_pidx = 0; tmp_pidx < IFLIB_MAX_RX_REFRESH && nm_i != head; tmp_pidx++) { 8372d873474SStephen Hurd struct netmap_slot *slot = &ring->slot[nm_i]; 8382d873474SStephen Hurd void *addr = PNMB(na, slot, &fl->ifl_bus_addrs[tmp_pidx]); 8392d873474SStephen Hurd uint32_t nic_i_dma = refill_pidx; 8402d873474SStephen Hurd nic_i = netmap_idx_k2n(kring, nm_i); 8412d873474SStephen Hurd 8422d873474SStephen Hurd MPASS(tmp_pidx < IFLIB_MAX_RX_REFRESH); 8432d873474SStephen Hurd 8442d873474SStephen Hurd if (addr == NETMAP_BUF_BASE(na)) /* bad buf */ 8452d873474SStephen Hurd return netmap_ring_reinit(kring); 8462d873474SStephen Hurd 8472d873474SStephen Hurd fl->ifl_vm_addrs[tmp_pidx] = addr; 84895dcf343SMarius Strobl if (__predict_false(init)) { 84995dcf343SMarius Strobl netmap_load_map(na, fl->ifl_buf_tag, 85095dcf343SMarius Strobl map[nic_i], addr); 85195dcf343SMarius Strobl } else if (slot->flags & NS_BUF_CHANGED) { 8522d873474SStephen Hurd /* buffer has changed, reload map */ 85395dcf343SMarius Strobl netmap_reload_map(na, fl->ifl_buf_tag, 85495dcf343SMarius Strobl map[nic_i], addr); 8552d873474SStephen Hurd } 8562d873474SStephen Hurd slot->flags &= ~NS_BUF_CHANGED; 8572d873474SStephen Hurd 8582d873474SStephen Hurd nm_i = nm_next(nm_i, lim); 8592d873474SStephen Hurd fl->ifl_rxd_idxs[tmp_pidx] = nic_i = nm_next(nic_i, lim); 8602d873474SStephen Hurd if (nm_i != head && tmp_pidx < IFLIB_MAX_RX_REFRESH-1) 8612d873474SStephen Hurd continue; 8622d873474SStephen Hurd 8632d873474SStephen Hurd iru.iru_pidx = refill_pidx; 8642d873474SStephen Hurd iru.iru_count = tmp_pidx+1; 8652d873474SStephen Hurd ctx->isc_rxd_refill(ctx->ifc_softc, &iru); 8662d873474SStephen Hurd refill_pidx = nic_i; 8672d873474SStephen Hurd for (int n = 0; n < iru.iru_count; n++) { 86895dcf343SMarius Strobl bus_dmamap_sync(fl->ifl_buf_tag, map[nic_i_dma], 8692d873474SStephen Hurd BUS_DMASYNC_PREREAD); 8702d873474SStephen Hurd /* XXX - change this to not use the netmap func*/ 8712d873474SStephen Hurd nic_i_dma = nm_next(nic_i_dma, lim); 8722d873474SStephen Hurd } 8732d873474SStephen Hurd } 8742d873474SStephen Hurd } 8752d873474SStephen Hurd kring->nr_hwcur = head; 8762d873474SStephen Hurd 8772d873474SStephen Hurd bus_dmamap_sync(fl->ifl_ifdi->idi_tag, fl->ifl_ifdi->idi_map, 8782d873474SStephen Hurd BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 87964e6fc13SStephen Hurd if (__predict_true(nic_i != UINT_MAX)) { 8802d873474SStephen Hurd ctx->isc_rxd_flush(ctx->ifc_softc, rxq->ifr_id, fl->ifl_id, nic_i); 88164e6fc13SStephen Hurd DBG_COUNTER_INC(rxd_flush); 88264e6fc13SStephen Hurd } 8832d873474SStephen Hurd return (0); 8842d873474SStephen Hurd } 8852d873474SStephen Hurd 8864c7070dbSScott Long /* 8874c7070dbSScott Long * Reconcile kernel and user view of the transmit ring. 8884c7070dbSScott Long * 8894c7070dbSScott Long * All information is in the kring. 8904c7070dbSScott Long * Userspace wants to send packets up to the one before kring->rhead, 8914c7070dbSScott Long * kernel knows kring->nr_hwcur is the first unsent packet. 8924c7070dbSScott Long * 8934c7070dbSScott Long * Here we push packets out (as many as possible), and possibly 8944c7070dbSScott Long * reclaim buffers from previously completed transmission. 8954c7070dbSScott Long * 8964c7070dbSScott Long * The caller (netmap) guarantees that there is only one instance 8974c7070dbSScott Long * running at any time. Any interference with other driver 8984c7070dbSScott Long * methods should be handled by the individual drivers. 8994c7070dbSScott Long */ 9004c7070dbSScott Long static int 9014c7070dbSScott Long iflib_netmap_txsync(struct netmap_kring *kring, int flags) 9024c7070dbSScott Long { 9034c7070dbSScott Long struct netmap_adapter *na = kring->na; 9044c7070dbSScott Long struct ifnet *ifp = na->ifp; 9054c7070dbSScott Long struct netmap_ring *ring = kring->ring; 906dd7fbcf1SStephen Hurd u_int nm_i; /* index into the netmap kring */ 9074c7070dbSScott Long u_int nic_i; /* index into the NIC ring */ 9084c7070dbSScott Long u_int n; 9094c7070dbSScott Long u_int const lim = kring->nkr_num_slots - 1; 9104c7070dbSScott Long u_int const head = kring->rhead; 9114c7070dbSScott Long struct if_pkt_info pi; 9124c7070dbSScott Long 9134c7070dbSScott Long /* 9144c7070dbSScott Long * interrupts on every tx packet are expensive so request 9154c7070dbSScott Long * them every half ring, or where NS_REPORT is set 9164c7070dbSScott Long */ 9174c7070dbSScott Long u_int report_frequency = kring->nkr_num_slots >> 1; 9184c7070dbSScott Long /* device-specific */ 9194c7070dbSScott Long if_ctx_t ctx = ifp->if_softc; 9204c7070dbSScott Long iflib_txq_t txq = &ctx->ifc_txqs[kring->ring_id]; 9214c7070dbSScott Long 92295dcf343SMarius Strobl bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map, 9234c7070dbSScott Long BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 9244c7070dbSScott Long 9254c7070dbSScott Long /* 9264c7070dbSScott Long * First part: process new packets to send. 927dd7fbcf1SStephen Hurd * nm_i is the current index in the netmap kring, 9284c7070dbSScott Long * nic_i is the corresponding index in the NIC ring. 9294c7070dbSScott Long * 9304c7070dbSScott Long * If we have packets to send (nm_i != head) 9314c7070dbSScott Long * iterate over the netmap ring, fetch length and update 9324c7070dbSScott Long * the corresponding slot in the NIC ring. Some drivers also 9334c7070dbSScott Long * need to update the buffer's physical address in the NIC slot 9344c7070dbSScott Long * even NS_BUF_CHANGED is not set (PNMB computes the addresses). 9354c7070dbSScott Long * 9364c7070dbSScott Long * The netmap_reload_map() calls is especially expensive, 9374c7070dbSScott Long * even when (as in this case) the tag is 0, so do only 9384c7070dbSScott Long * when the buffer has actually changed. 9394c7070dbSScott Long * 9404c7070dbSScott Long * If possible do not set the report/intr bit on all slots, 9414c7070dbSScott Long * but only a few times per ring or when NS_REPORT is set. 9424c7070dbSScott Long * 9434c7070dbSScott Long * Finally, on 10G and faster drivers, it might be useful 9444c7070dbSScott Long * to prefetch the next slot and txr entry. 9454c7070dbSScott Long */ 9464c7070dbSScott Long 947dd7fbcf1SStephen Hurd nm_i = kring->nr_hwcur; 9485ee36c68SStephen Hurd if (nm_i != head) { /* we have new packets to send */ 94995246abbSSean Bruno pkt_info_zero(&pi); 95095246abbSSean Bruno pi.ipi_segs = txq->ift_segs; 95195246abbSSean Bruno pi.ipi_qsidx = kring->ring_id; 9524c7070dbSScott Long nic_i = netmap_idx_k2n(kring, nm_i); 9534c7070dbSScott Long 9544c7070dbSScott Long __builtin_prefetch(&ring->slot[nm_i]); 9554c7070dbSScott Long __builtin_prefetch(&txq->ift_sds.ifsd_m[nic_i]); 9564c7070dbSScott Long __builtin_prefetch(&txq->ift_sds.ifsd_map[nic_i]); 9574c7070dbSScott Long 9584c7070dbSScott Long for (n = 0; nm_i != head; n++) { 9594c7070dbSScott Long struct netmap_slot *slot = &ring->slot[nm_i]; 9604c7070dbSScott Long u_int len = slot->len; 9610a1b74a3SSean Bruno uint64_t paddr; 9624c7070dbSScott Long void *addr = PNMB(na, slot, &paddr); 9634c7070dbSScott Long int flags = (slot->flags & NS_REPORT || 9644c7070dbSScott Long nic_i == 0 || nic_i == report_frequency) ? 9654c7070dbSScott Long IPI_TX_INTR : 0; 9664c7070dbSScott Long 9674c7070dbSScott Long /* device-specific */ 96895246abbSSean Bruno pi.ipi_len = len; 96995246abbSSean Bruno pi.ipi_segs[0].ds_addr = paddr; 97095246abbSSean Bruno pi.ipi_segs[0].ds_len = len; 97195246abbSSean Bruno pi.ipi_nsegs = 1; 97295246abbSSean Bruno pi.ipi_ndescs = 0; 9734c7070dbSScott Long pi.ipi_pidx = nic_i; 9744c7070dbSScott Long pi.ipi_flags = flags; 9754c7070dbSScott Long 9764c7070dbSScott Long /* Fill the slot in the NIC ring. */ 9774c7070dbSScott Long ctx->isc_txd_encap(ctx->ifc_softc, &pi); 97864e6fc13SStephen Hurd DBG_COUNTER_INC(tx_encap); 9794c7070dbSScott Long 9804c7070dbSScott Long /* prefetch for next round */ 9814c7070dbSScott Long __builtin_prefetch(&ring->slot[nm_i + 1]); 9824c7070dbSScott Long __builtin_prefetch(&txq->ift_sds.ifsd_m[nic_i + 1]); 9834c7070dbSScott Long __builtin_prefetch(&txq->ift_sds.ifsd_map[nic_i + 1]); 9844c7070dbSScott Long 9854c7070dbSScott Long NM_CHECK_ADDR_LEN(na, addr, len); 9864c7070dbSScott Long 9874c7070dbSScott Long if (slot->flags & NS_BUF_CHANGED) { 9884c7070dbSScott Long /* buffer has changed, reload map */ 989bfce461eSMarius Strobl netmap_reload_map(na, txq->ift_buf_tag, 990bfce461eSMarius Strobl txq->ift_sds.ifsd_map[nic_i], addr); 9914c7070dbSScott Long } 9924c7070dbSScott Long /* make sure changes to the buffer are synced */ 99395dcf343SMarius Strobl bus_dmamap_sync(txq->ift_buf_tag, 99495dcf343SMarius Strobl txq->ift_sds.ifsd_map[nic_i], 9954c7070dbSScott Long BUS_DMASYNC_PREWRITE); 99695dcf343SMarius Strobl 99795246abbSSean Bruno slot->flags &= ~(NS_REPORT | NS_BUF_CHANGED); 9984c7070dbSScott Long nm_i = nm_next(nm_i, lim); 9994c7070dbSScott Long nic_i = nm_next(nic_i, lim); 10004c7070dbSScott Long } 1001dd7fbcf1SStephen Hurd kring->nr_hwcur = nm_i; 10024c7070dbSScott Long 10034c7070dbSScott Long /* synchronize the NIC ring */ 100495dcf343SMarius Strobl bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map, 10054c7070dbSScott Long BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 10064c7070dbSScott Long 10074c7070dbSScott Long /* (re)start the tx unit up to slot nic_i (excluded) */ 10084c7070dbSScott Long ctx->isc_txd_flush(ctx->ifc_softc, txq->ift_id, nic_i); 10094c7070dbSScott Long } 10104c7070dbSScott Long 10114c7070dbSScott Long /* 10124c7070dbSScott Long * Second part: reclaim buffers for completed transmissions. 10135ee36c68SStephen Hurd * 10145ee36c68SStephen Hurd * If there are unclaimed buffers, attempt to reclaim them. 10155ee36c68SStephen Hurd * If none are reclaimed, and TX IRQs are not in use, do an initial 10165ee36c68SStephen Hurd * minimal delay, then trigger the tx handler which will spin in the 10175ee36c68SStephen Hurd * group task queue. 10184c7070dbSScott Long */ 1019dd7fbcf1SStephen Hurd if (kring->nr_hwtail != nm_prev(kring->nr_hwcur, lim)) { 10204c7070dbSScott Long if (iflib_tx_credits_update(ctx, txq)) { 10214c7070dbSScott Long /* some tx completed, increment avail */ 10224c7070dbSScott Long nic_i = txq->ift_cidx_processed; 10234c7070dbSScott Long kring->nr_hwtail = nm_prev(netmap_idx_n2k(kring, nic_i), lim); 10244c7070dbSScott Long } 10255ee36c68SStephen Hurd } 1026dd7fbcf1SStephen Hurd if (!(ctx->ifc_flags & IFC_NETMAP_TX_IRQ)) 1027dd7fbcf1SStephen Hurd if (kring->nr_hwtail != nm_prev(kring->nr_hwcur, lim)) { 1028dd7fbcf1SStephen Hurd callout_reset_on(&txq->ift_timer, hz < 2000 ? 1 : hz / 1000, 1029dd7fbcf1SStephen Hurd iflib_timer, txq, txq->ift_timer.c_cpu); 10305ee36c68SStephen Hurd } 10314c7070dbSScott Long return (0); 10324c7070dbSScott Long } 10334c7070dbSScott Long 10344c7070dbSScott Long /* 10354c7070dbSScott Long * Reconcile kernel and user view of the receive ring. 10364c7070dbSScott Long * Same as for the txsync, this routine must be efficient. 10374c7070dbSScott Long * The caller guarantees a single invocations, but races against 10384c7070dbSScott Long * the rest of the driver should be handled here. 10394c7070dbSScott Long * 10404c7070dbSScott Long * On call, kring->rhead is the first packet that userspace wants 10414c7070dbSScott Long * to keep, and kring->rcur is the wakeup point. 10424c7070dbSScott Long * The kernel has previously reported packets up to kring->rtail. 10434c7070dbSScott Long * 10444c7070dbSScott Long * If (flags & NAF_FORCE_READ) also check for incoming packets irrespective 10454c7070dbSScott Long * of whether or not we received an interrupt. 10464c7070dbSScott Long */ 10474c7070dbSScott Long static int 10484c7070dbSScott Long iflib_netmap_rxsync(struct netmap_kring *kring, int flags) 10494c7070dbSScott Long { 10504c7070dbSScott Long struct netmap_adapter *na = kring->na; 10514c7070dbSScott Long struct netmap_ring *ring = kring->ring; 105295dcf343SMarius Strobl iflib_fl_t fl; 105395246abbSSean Bruno uint32_t nm_i; /* index into the netmap ring */ 10542d873474SStephen Hurd uint32_t nic_i; /* index into the NIC ring */ 10554c7070dbSScott Long u_int i, n; 10564c7070dbSScott Long u_int const lim = kring->nkr_num_slots - 1; 1057dd7fbcf1SStephen Hurd u_int const head = kring->rhead; 10584c7070dbSScott Long int force_update = (flags & NAF_FORCE_READ) || kring->nr_kflags & NKR_PENDINTR; 1059ab2e3f79SStephen Hurd struct if_rxd_info ri; 106095246abbSSean Bruno 106195246abbSSean Bruno struct ifnet *ifp = na->ifp; 10624c7070dbSScott Long if_ctx_t ctx = ifp->if_softc; 10634c7070dbSScott Long iflib_rxq_t rxq = &ctx->ifc_rxqs[kring->ring_id]; 10644c7070dbSScott Long if (head > lim) 10654c7070dbSScott Long return netmap_ring_reinit(kring); 10664c7070dbSScott Long 106795dcf343SMarius Strobl /* 106895dcf343SMarius Strobl * XXX netmap_fl_refill() only ever (re)fills free list 0 so far. 106995dcf343SMarius Strobl */ 107095dcf343SMarius Strobl 107195246abbSSean Bruno for (i = 0, fl = rxq->ifr_fl; i < rxq->ifr_nfl; i++, fl++) { 107295dcf343SMarius Strobl bus_dmamap_sync(fl->ifl_ifdi->idi_tag, fl->ifl_ifdi->idi_map, 10734c7070dbSScott Long BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 107495246abbSSean Bruno } 107595dcf343SMarius Strobl 10764c7070dbSScott Long /* 10774c7070dbSScott Long * First part: import newly received packets. 10784c7070dbSScott Long * 10794c7070dbSScott Long * nm_i is the index of the next free slot in the netmap ring, 10804c7070dbSScott Long * nic_i is the index of the next received packet in the NIC ring, 10814c7070dbSScott Long * and they may differ in case if_init() has been called while 10824c7070dbSScott Long * in netmap mode. For the receive ring we have 10834c7070dbSScott Long * 10844c7070dbSScott Long * nic_i = rxr->next_check; 10854c7070dbSScott Long * nm_i = kring->nr_hwtail (previous) 10864c7070dbSScott Long * and 10874c7070dbSScott Long * nm_i == (nic_i + kring->nkr_hwofs) % ring_size 10884c7070dbSScott Long * 10894c7070dbSScott Long * rxr->next_check is set to 0 on a ring reinit 10904c7070dbSScott Long */ 10914c7070dbSScott Long if (netmap_no_pendintr || force_update) { 10924c7070dbSScott Long int crclen = iflib_crcstrip ? 0 : 4; 10934c7070dbSScott Long int error, avail; 10944c7070dbSScott Long 10952d873474SStephen Hurd for (i = 0; i < rxq->ifr_nfl; i++) { 10962d873474SStephen Hurd fl = &rxq->ifr_fl[i]; 10974c7070dbSScott Long nic_i = fl->ifl_cidx; 10984c7070dbSScott Long nm_i = netmap_idx_n2k(kring, nic_i); 109995dcf343SMarius Strobl avail = ctx->isc_rxd_available(ctx->ifc_softc, 110095dcf343SMarius Strobl rxq->ifr_id, nic_i, USHRT_MAX); 11014c7070dbSScott Long for (n = 0; avail > 0; n++, avail--) { 1102ab2e3f79SStephen Hurd rxd_info_zero(&ri); 1103ab2e3f79SStephen Hurd ri.iri_frags = rxq->ifr_frags; 1104ab2e3f79SStephen Hurd ri.iri_qsidx = kring->ring_id; 1105ab2e3f79SStephen Hurd ri.iri_ifp = ctx->ifc_ifp; 1106ab2e3f79SStephen Hurd ri.iri_cidx = nic_i; 110795246abbSSean Bruno 1108ab2e3f79SStephen Hurd error = ctx->isc_rxd_pkt_get(ctx->ifc_softc, &ri); 1109ab2e3f79SStephen Hurd ring->slot[nm_i].len = error ? 0 : ri.iri_len - crclen; 11107cb7c6e3SNavdeep Parhar ring->slot[nm_i].flags = 0; 111195dcf343SMarius Strobl bus_dmamap_sync(fl->ifl_buf_tag, 1112e035717eSSean Bruno fl->ifl_sds.ifsd_map[nic_i], BUS_DMASYNC_POSTREAD); 11134c7070dbSScott Long nm_i = nm_next(nm_i, lim); 11144c7070dbSScott Long nic_i = nm_next(nic_i, lim); 11154c7070dbSScott Long } 11164c7070dbSScott Long if (n) { /* update the state variables */ 11174c7070dbSScott Long if (netmap_no_pendintr && !force_update) { 11184c7070dbSScott Long /* diagnostics */ 11194c7070dbSScott Long iflib_rx_miss ++; 11204c7070dbSScott Long iflib_rx_miss_bufs += n; 11214c7070dbSScott Long } 11224c7070dbSScott Long fl->ifl_cidx = nic_i; 1123dd7fbcf1SStephen Hurd kring->nr_hwtail = nm_i; 11244c7070dbSScott Long } 11254c7070dbSScott Long kring->nr_kflags &= ~NKR_PENDINTR; 11264c7070dbSScott Long } 11274c7070dbSScott Long } 11284c7070dbSScott Long /* 11294c7070dbSScott Long * Second part: skip past packets that userspace has released. 11304c7070dbSScott Long * (kring->nr_hwcur to head excluded), 11314c7070dbSScott Long * and make the buffers available for reception. 11324c7070dbSScott Long * As usual nm_i is the index in the netmap ring, 11334c7070dbSScott Long * nic_i is the index in the NIC ring, and 11344c7070dbSScott Long * nm_i == (nic_i + kring->nkr_hwofs) % ring_size 11354c7070dbSScott Long */ 11364c7070dbSScott Long /* XXX not sure how this will work with multiple free lists */ 1137dd7fbcf1SStephen Hurd nm_i = kring->nr_hwcur; 113895246abbSSean Bruno 11392d873474SStephen Hurd return (netmap_fl_refill(rxq, kring, nm_i, false)); 11404c7070dbSScott Long } 11414c7070dbSScott Long 114295246abbSSean Bruno static void 114395246abbSSean Bruno iflib_netmap_intr(struct netmap_adapter *na, int onoff) 114495246abbSSean Bruno { 114595246abbSSean Bruno struct ifnet *ifp = na->ifp; 114695246abbSSean Bruno if_ctx_t ctx = ifp->if_softc; 114795246abbSSean Bruno 1148ab2e3f79SStephen Hurd CTX_LOCK(ctx); 114995246abbSSean Bruno if (onoff) { 115095246abbSSean Bruno IFDI_INTR_ENABLE(ctx); 115195246abbSSean Bruno } else { 115295246abbSSean Bruno IFDI_INTR_DISABLE(ctx); 115395246abbSSean Bruno } 1154ab2e3f79SStephen Hurd CTX_UNLOCK(ctx); 115595246abbSSean Bruno } 115695246abbSSean Bruno 115795246abbSSean Bruno 11584c7070dbSScott Long static int 11594c7070dbSScott Long iflib_netmap_attach(if_ctx_t ctx) 11604c7070dbSScott Long { 11614c7070dbSScott Long struct netmap_adapter na; 116223ac9029SStephen Hurd if_softc_ctx_t scctx = &ctx->ifc_softc_ctx; 11634c7070dbSScott Long 11644c7070dbSScott Long bzero(&na, sizeof(na)); 11654c7070dbSScott Long 11664c7070dbSScott Long na.ifp = ctx->ifc_ifp; 11674c7070dbSScott Long na.na_flags = NAF_BDG_MAYSLEEP; 11684c7070dbSScott Long MPASS(ctx->ifc_softc_ctx.isc_ntxqsets); 11694c7070dbSScott Long MPASS(ctx->ifc_softc_ctx.isc_nrxqsets); 11704c7070dbSScott Long 117123ac9029SStephen Hurd na.num_tx_desc = scctx->isc_ntxd[0]; 117223ac9029SStephen Hurd na.num_rx_desc = scctx->isc_nrxd[0]; 11734c7070dbSScott Long na.nm_txsync = iflib_netmap_txsync; 11744c7070dbSScott Long na.nm_rxsync = iflib_netmap_rxsync; 11754c7070dbSScott Long na.nm_register = iflib_netmap_register; 117695246abbSSean Bruno na.nm_intr = iflib_netmap_intr; 11774c7070dbSScott Long na.num_tx_rings = ctx->ifc_softc_ctx.isc_ntxqsets; 11784c7070dbSScott Long na.num_rx_rings = ctx->ifc_softc_ctx.isc_nrxqsets; 11794c7070dbSScott Long return (netmap_attach(&na)); 11804c7070dbSScott Long } 11814c7070dbSScott Long 11824c7070dbSScott Long static void 11834c7070dbSScott Long iflib_netmap_txq_init(if_ctx_t ctx, iflib_txq_t txq) 11844c7070dbSScott Long { 11854c7070dbSScott Long struct netmap_adapter *na = NA(ctx->ifc_ifp); 11864c7070dbSScott Long struct netmap_slot *slot; 11874c7070dbSScott Long 11884c7070dbSScott Long slot = netmap_reset(na, NR_TX, txq->ift_id, 0); 1189e099b90bSPedro F. Giffuni if (slot == NULL) 11904c7070dbSScott Long return; 119123ac9029SStephen Hurd for (int i = 0; i < ctx->ifc_softc_ctx.isc_ntxd[0]; i++) { 11924c7070dbSScott Long 11934c7070dbSScott Long /* 11944c7070dbSScott Long * In netmap mode, set the map for the packet buffer. 11954c7070dbSScott Long * NOTE: Some drivers (not this one) also need to set 11964c7070dbSScott Long * the physical buffer address in the NIC ring. 11974c7070dbSScott Long * netmap_idx_n2k() maps a nic index, i, into the corresponding 11984c7070dbSScott Long * netmap slot index, si 11994c7070dbSScott Long */ 12002ff91c17SVincenzo Maffione int si = netmap_idx_n2k(na->tx_rings[txq->ift_id], i); 1201bfce461eSMarius Strobl netmap_load_map(na, txq->ift_buf_tag, txq->ift_sds.ifsd_map[i], 1202bfce461eSMarius Strobl NMB(na, slot + si)); 12034c7070dbSScott Long } 12044c7070dbSScott Long } 12052d873474SStephen Hurd 12064c7070dbSScott Long static void 12074c7070dbSScott Long iflib_netmap_rxq_init(if_ctx_t ctx, iflib_rxq_t rxq) 12084c7070dbSScott Long { 12094c7070dbSScott Long struct netmap_adapter *na = NA(ctx->ifc_ifp); 12102ff91c17SVincenzo Maffione struct netmap_kring *kring = na->rx_rings[rxq->ifr_id]; 12114c7070dbSScott Long struct netmap_slot *slot; 12122d873474SStephen Hurd uint32_t nm_i; 12134c7070dbSScott Long 12144c7070dbSScott Long slot = netmap_reset(na, NR_RX, rxq->ifr_id, 0); 1215e099b90bSPedro F. Giffuni if (slot == NULL) 12164c7070dbSScott Long return; 12172d873474SStephen Hurd nm_i = netmap_idx_n2k(kring, 0); 12182d873474SStephen Hurd netmap_fl_refill(rxq, kring, nm_i, true); 12194c7070dbSScott Long } 12204c7070dbSScott Long 1221dd7fbcf1SStephen Hurd static void 122295dcf343SMarius Strobl iflib_netmap_timer_adjust(if_ctx_t ctx, iflib_txq_t txq, uint32_t *reset_on) 1223dd7fbcf1SStephen Hurd { 1224dd7fbcf1SStephen Hurd struct netmap_kring *kring; 122595dcf343SMarius Strobl uint16_t txqid; 1226dd7fbcf1SStephen Hurd 122795dcf343SMarius Strobl txqid = txq->ift_id; 1228dd7fbcf1SStephen Hurd kring = NA(ctx->ifc_ifp)->tx_rings[txqid]; 1229dd7fbcf1SStephen Hurd 1230dd7fbcf1SStephen Hurd if (kring->nr_hwcur != nm_next(kring->nr_hwtail, kring->nkr_num_slots - 1)) { 123195dcf343SMarius Strobl bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map, 123295dcf343SMarius Strobl BUS_DMASYNC_POSTREAD); 1233dd7fbcf1SStephen Hurd if (ctx->isc_txd_credits_update(ctx->ifc_softc, txqid, false)) 1234dd7fbcf1SStephen Hurd netmap_tx_irq(ctx->ifc_ifp, txqid); 1235dd7fbcf1SStephen Hurd if (!(ctx->ifc_flags & IFC_NETMAP_TX_IRQ)) { 1236dd7fbcf1SStephen Hurd if (hz < 2000) 1237dd7fbcf1SStephen Hurd *reset_on = 1; 1238dd7fbcf1SStephen Hurd else 1239dd7fbcf1SStephen Hurd *reset_on = hz / 1000; 1240dd7fbcf1SStephen Hurd } 1241dd7fbcf1SStephen Hurd } 1242dd7fbcf1SStephen Hurd } 1243dd7fbcf1SStephen Hurd 12444c7070dbSScott Long #define iflib_netmap_detach(ifp) netmap_detach(ifp) 12454c7070dbSScott Long 12464c7070dbSScott Long #else 12474c7070dbSScott Long #define iflib_netmap_txq_init(ctx, txq) 12484c7070dbSScott Long #define iflib_netmap_rxq_init(ctx, rxq) 12494c7070dbSScott Long #define iflib_netmap_detach(ifp) 12504c7070dbSScott Long 12514c7070dbSScott Long #define iflib_netmap_attach(ctx) (0) 12524c7070dbSScott Long #define netmap_rx_irq(ifp, qid, budget) (0) 125395246abbSSean Bruno #define netmap_tx_irq(ifp, qid) do {} while (0) 125495dcf343SMarius Strobl #define iflib_netmap_timer_adjust(ctx, txq, reset_on) 12554c7070dbSScott Long 12564c7070dbSScott Long #endif 12574c7070dbSScott Long 12584c7070dbSScott Long #if defined(__i386__) || defined(__amd64__) 12594c7070dbSScott Long static __inline void 12604c7070dbSScott Long prefetch(void *x) 12614c7070dbSScott Long { 12624c7070dbSScott Long __asm volatile("prefetcht0 %0" :: "m" (*(unsigned long *)x)); 12634c7070dbSScott Long } 12643429c02fSStephen Hurd static __inline void 12653429c02fSStephen Hurd prefetch2cachelines(void *x) 12663429c02fSStephen Hurd { 12673429c02fSStephen Hurd __asm volatile("prefetcht0 %0" :: "m" (*(unsigned long *)x)); 12683429c02fSStephen Hurd #if (CACHE_LINE_SIZE < 128) 12693429c02fSStephen Hurd __asm volatile("prefetcht0 %0" :: "m" (*(((unsigned long *)x)+CACHE_LINE_SIZE/(sizeof(unsigned long))))); 12703429c02fSStephen Hurd #endif 12713429c02fSStephen Hurd } 12724c7070dbSScott Long #else 12734c7070dbSScott Long #define prefetch(x) 12743429c02fSStephen Hurd #define prefetch2cachelines(x) 12754c7070dbSScott Long #endif 12764c7070dbSScott Long 12774c7070dbSScott Long static void 127809f6ff4fSMatt Macy iflib_gen_mac(if_ctx_t ctx) 127909f6ff4fSMatt Macy { 128009f6ff4fSMatt Macy struct thread *td; 128109f6ff4fSMatt Macy MD5_CTX mdctx; 128209f6ff4fSMatt Macy char uuid[HOSTUUIDLEN+1]; 128309f6ff4fSMatt Macy char buf[HOSTUUIDLEN+16]; 128409f6ff4fSMatt Macy uint8_t *mac; 128509f6ff4fSMatt Macy unsigned char digest[16]; 128609f6ff4fSMatt Macy 128709f6ff4fSMatt Macy td = curthread; 128809f6ff4fSMatt Macy mac = ctx->ifc_mac; 128909f6ff4fSMatt Macy uuid[HOSTUUIDLEN] = 0; 129009f6ff4fSMatt Macy bcopy(td->td_ucred->cr_prison->pr_hostuuid, uuid, HOSTUUIDLEN); 129109f6ff4fSMatt Macy snprintf(buf, HOSTUUIDLEN+16, "%s-%s", uuid, device_get_nameunit(ctx->ifc_dev)); 129209f6ff4fSMatt Macy /* 129309f6ff4fSMatt Macy * Generate a pseudo-random, deterministic MAC 129409f6ff4fSMatt Macy * address based on the UUID and unit number. 129509f6ff4fSMatt Macy * The FreeBSD Foundation OUI of 58-9C-FC is used. 129609f6ff4fSMatt Macy */ 129709f6ff4fSMatt Macy MD5Init(&mdctx); 129809f6ff4fSMatt Macy MD5Update(&mdctx, buf, strlen(buf)); 129909f6ff4fSMatt Macy MD5Final(digest, &mdctx); 130009f6ff4fSMatt Macy 130109f6ff4fSMatt Macy mac[0] = 0x58; 130209f6ff4fSMatt Macy mac[1] = 0x9C; 130309f6ff4fSMatt Macy mac[2] = 0xFC; 130409f6ff4fSMatt Macy mac[3] = digest[0]; 130509f6ff4fSMatt Macy mac[4] = digest[1]; 130609f6ff4fSMatt Macy mac[5] = digest[2]; 130709f6ff4fSMatt Macy } 130809f6ff4fSMatt Macy 130909f6ff4fSMatt Macy static void 131010e0d938SStephen Hurd iru_init(if_rxd_update_t iru, iflib_rxq_t rxq, uint8_t flid) 131110e0d938SStephen Hurd { 131210e0d938SStephen Hurd iflib_fl_t fl; 131310e0d938SStephen Hurd 131410e0d938SStephen Hurd fl = &rxq->ifr_fl[flid]; 131510e0d938SStephen Hurd iru->iru_paddrs = fl->ifl_bus_addrs; 131610e0d938SStephen Hurd iru->iru_vaddrs = &fl->ifl_vm_addrs[0]; 131710e0d938SStephen Hurd iru->iru_idxs = fl->ifl_rxd_idxs; 131810e0d938SStephen Hurd iru->iru_qsidx = rxq->ifr_id; 131910e0d938SStephen Hurd iru->iru_buf_size = fl->ifl_buf_size; 132010e0d938SStephen Hurd iru->iru_flidx = fl->ifl_id; 132110e0d938SStephen Hurd } 132210e0d938SStephen Hurd 132310e0d938SStephen Hurd static void 13244c7070dbSScott Long _iflib_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int err) 13254c7070dbSScott Long { 13264c7070dbSScott Long if (err) 13274c7070dbSScott Long return; 13284c7070dbSScott Long *(bus_addr_t *) arg = segs[0].ds_addr; 13294c7070dbSScott Long } 13304c7070dbSScott Long 13314c7070dbSScott Long int 13328f82136aSPatrick Kelsey iflib_dma_alloc_align(if_ctx_t ctx, int size, int align, iflib_dma_info_t dma, int mapflags) 13334c7070dbSScott Long { 13344c7070dbSScott Long int err; 13354c7070dbSScott Long device_t dev = ctx->ifc_dev; 13364c7070dbSScott Long 13374c7070dbSScott Long err = bus_dma_tag_create(bus_get_dma_tag(dev), /* parent */ 13388f82136aSPatrick Kelsey align, 0, /* alignment, bounds */ 13394c7070dbSScott Long BUS_SPACE_MAXADDR, /* lowaddr */ 13404c7070dbSScott Long BUS_SPACE_MAXADDR, /* highaddr */ 13414c7070dbSScott Long NULL, NULL, /* filter, filterarg */ 13424c7070dbSScott Long size, /* maxsize */ 13434c7070dbSScott Long 1, /* nsegments */ 13444c7070dbSScott Long size, /* maxsegsize */ 13454c7070dbSScott Long BUS_DMA_ALLOCNOW, /* flags */ 13464c7070dbSScott Long NULL, /* lockfunc */ 13474c7070dbSScott Long NULL, /* lockarg */ 13484c7070dbSScott Long &dma->idi_tag); 13494c7070dbSScott Long if (err) { 13504c7070dbSScott Long device_printf(dev, 13514c7070dbSScott Long "%s: bus_dma_tag_create failed: %d\n", 13524c7070dbSScott Long __func__, err); 13534c7070dbSScott Long goto fail_0; 13544c7070dbSScott Long } 13554c7070dbSScott Long 13564c7070dbSScott Long err = bus_dmamem_alloc(dma->idi_tag, (void**) &dma->idi_vaddr, 13574c7070dbSScott Long BUS_DMA_NOWAIT | BUS_DMA_COHERENT | BUS_DMA_ZERO, &dma->idi_map); 13584c7070dbSScott Long if (err) { 13594c7070dbSScott Long device_printf(dev, 13604c7070dbSScott Long "%s: bus_dmamem_alloc(%ju) failed: %d\n", 13614c7070dbSScott Long __func__, (uintmax_t)size, err); 13624c7070dbSScott Long goto fail_1; 13634c7070dbSScott Long } 13644c7070dbSScott Long 13654c7070dbSScott Long dma->idi_paddr = IF_BAD_DMA; 13664c7070dbSScott Long err = bus_dmamap_load(dma->idi_tag, dma->idi_map, dma->idi_vaddr, 13674c7070dbSScott Long size, _iflib_dmamap_cb, &dma->idi_paddr, mapflags | BUS_DMA_NOWAIT); 13684c7070dbSScott Long if (err || dma->idi_paddr == IF_BAD_DMA) { 13694c7070dbSScott Long device_printf(dev, 13704c7070dbSScott Long "%s: bus_dmamap_load failed: %d\n", 13714c7070dbSScott Long __func__, err); 13724c7070dbSScott Long goto fail_2; 13734c7070dbSScott Long } 13744c7070dbSScott Long 13754c7070dbSScott Long dma->idi_size = size; 13764c7070dbSScott Long return (0); 13774c7070dbSScott Long 13784c7070dbSScott Long fail_2: 13794c7070dbSScott Long bus_dmamem_free(dma->idi_tag, dma->idi_vaddr, dma->idi_map); 13804c7070dbSScott Long fail_1: 13814c7070dbSScott Long bus_dma_tag_destroy(dma->idi_tag); 13824c7070dbSScott Long fail_0: 13834c7070dbSScott Long dma->idi_tag = NULL; 13844c7070dbSScott Long 13854c7070dbSScott Long return (err); 13864c7070dbSScott Long } 13874c7070dbSScott Long 13884c7070dbSScott Long int 13898f82136aSPatrick Kelsey iflib_dma_alloc(if_ctx_t ctx, int size, iflib_dma_info_t dma, int mapflags) 13908f82136aSPatrick Kelsey { 13918f82136aSPatrick Kelsey if_shared_ctx_t sctx = ctx->ifc_sctx; 13928f82136aSPatrick Kelsey 13938f82136aSPatrick Kelsey KASSERT(sctx->isc_q_align != 0, ("alignment value not initialized")); 13948f82136aSPatrick Kelsey 13958f82136aSPatrick Kelsey return (iflib_dma_alloc_align(ctx, size, sctx->isc_q_align, dma, mapflags)); 13968f82136aSPatrick Kelsey } 13978f82136aSPatrick Kelsey 13988f82136aSPatrick Kelsey int 13994c7070dbSScott Long iflib_dma_alloc_multi(if_ctx_t ctx, int *sizes, iflib_dma_info_t *dmalist, int mapflags, int count) 14004c7070dbSScott Long { 14014c7070dbSScott Long int i, err; 14024c7070dbSScott Long iflib_dma_info_t *dmaiter; 14034c7070dbSScott Long 14044c7070dbSScott Long dmaiter = dmalist; 14054c7070dbSScott Long for (i = 0; i < count; i++, dmaiter++) { 14064c7070dbSScott Long if ((err = iflib_dma_alloc(ctx, sizes[i], *dmaiter, mapflags)) != 0) 14074c7070dbSScott Long break; 14084c7070dbSScott Long } 14094c7070dbSScott Long if (err) 14104c7070dbSScott Long iflib_dma_free_multi(dmalist, i); 14114c7070dbSScott Long return (err); 14124c7070dbSScott Long } 14134c7070dbSScott Long 14144c7070dbSScott Long void 14154c7070dbSScott Long iflib_dma_free(iflib_dma_info_t dma) 14164c7070dbSScott Long { 14174c7070dbSScott Long if (dma->idi_tag == NULL) 14184c7070dbSScott Long return; 14194c7070dbSScott Long if (dma->idi_paddr != IF_BAD_DMA) { 14204c7070dbSScott Long bus_dmamap_sync(dma->idi_tag, dma->idi_map, 14214c7070dbSScott Long BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 14224c7070dbSScott Long bus_dmamap_unload(dma->idi_tag, dma->idi_map); 14234c7070dbSScott Long dma->idi_paddr = IF_BAD_DMA; 14244c7070dbSScott Long } 14254c7070dbSScott Long if (dma->idi_vaddr != NULL) { 14264c7070dbSScott Long bus_dmamem_free(dma->idi_tag, dma->idi_vaddr, dma->idi_map); 14274c7070dbSScott Long dma->idi_vaddr = NULL; 14284c7070dbSScott Long } 14294c7070dbSScott Long bus_dma_tag_destroy(dma->idi_tag); 14304c7070dbSScott Long dma->idi_tag = NULL; 14314c7070dbSScott Long } 14324c7070dbSScott Long 14334c7070dbSScott Long void 14344c7070dbSScott Long iflib_dma_free_multi(iflib_dma_info_t *dmalist, int count) 14354c7070dbSScott Long { 14364c7070dbSScott Long int i; 14374c7070dbSScott Long iflib_dma_info_t *dmaiter = dmalist; 14384c7070dbSScott Long 14394c7070dbSScott Long for (i = 0; i < count; i++, dmaiter++) 14404c7070dbSScott Long iflib_dma_free(*dmaiter); 14414c7070dbSScott Long } 14424c7070dbSScott Long 1443bd84f700SSean Bruno #ifdef EARLY_AP_STARTUP 1444bd84f700SSean Bruno static const int iflib_started = 1; 1445bd84f700SSean Bruno #else 1446bd84f700SSean Bruno /* 1447bd84f700SSean Bruno * We used to abuse the smp_started flag to decide if the queues have been 1448bd84f700SSean Bruno * fully initialized (by late taskqgroup_adjust() calls in a SYSINIT()). 1449bd84f700SSean Bruno * That gave bad races, since the SYSINIT() runs strictly after smp_started 1450bd84f700SSean Bruno * is set. Run a SYSINIT() strictly after that to just set a usable 1451bd84f700SSean Bruno * completion flag. 1452bd84f700SSean Bruno */ 1453bd84f700SSean Bruno 1454bd84f700SSean Bruno static int iflib_started; 1455bd84f700SSean Bruno 1456bd84f700SSean Bruno static void 1457bd84f700SSean Bruno iflib_record_started(void *arg) 1458bd84f700SSean Bruno { 1459bd84f700SSean Bruno iflib_started = 1; 1460bd84f700SSean Bruno } 1461bd84f700SSean Bruno 1462bd84f700SSean Bruno SYSINIT(iflib_record_started, SI_SUB_SMP + 1, SI_ORDER_FIRST, 1463bd84f700SSean Bruno iflib_record_started, NULL); 1464bd84f700SSean Bruno #endif 1465bd84f700SSean Bruno 14664c7070dbSScott Long static int 14674c7070dbSScott Long iflib_fast_intr(void *arg) 14684c7070dbSScott Long { 14694c7070dbSScott Long iflib_filter_info_t info = arg; 14704c7070dbSScott Long struct grouptask *gtask = info->ifi_task; 1471*ca62461bSStephen Hurd int result; 1472*ca62461bSStephen Hurd 147395246abbSSean Bruno if (!iflib_started) 1474*ca62461bSStephen Hurd return (FILTER_STRAY); 147595246abbSSean Bruno 147695246abbSSean Bruno DBG_COUNTER_INC(fast_intrs); 1477*ca62461bSStephen Hurd if (info->ifi_filter != NULL) { 1478*ca62461bSStephen Hurd result = info->ifi_filter(info->ifi_filter_arg); 1479*ca62461bSStephen Hurd if ((result & FILTER_SCHEDULE_THREAD) == 0) 1480*ca62461bSStephen Hurd return (result); 1481*ca62461bSStephen Hurd } 148295246abbSSean Bruno 148395246abbSSean Bruno GROUPTASK_ENQUEUE(gtask); 148495246abbSSean Bruno return (FILTER_HANDLED); 148595246abbSSean Bruno } 148695246abbSSean Bruno 148795246abbSSean Bruno static int 148895246abbSSean Bruno iflib_fast_intr_rxtx(void *arg) 148995246abbSSean Bruno { 149095246abbSSean Bruno iflib_filter_info_t info = arg; 149195246abbSSean Bruno struct grouptask *gtask = info->ifi_task; 149295dcf343SMarius Strobl if_ctx_t ctx; 149395246abbSSean Bruno iflib_rxq_t rxq = (iflib_rxq_t)info->ifi_ctx; 149495dcf343SMarius Strobl iflib_txq_t txq; 149595dcf343SMarius Strobl void *sc; 1496*ca62461bSStephen Hurd int i, cidx, result; 149795dcf343SMarius Strobl qidx_t txqid; 149895246abbSSean Bruno 149995246abbSSean Bruno if (!iflib_started) 1500*ca62461bSStephen Hurd return (FILTER_STRAY); 150195246abbSSean Bruno 150295246abbSSean Bruno DBG_COUNTER_INC(fast_intrs); 1503*ca62461bSStephen Hurd if (info->ifi_filter != NULL) { 1504*ca62461bSStephen Hurd result = info->ifi_filter(info->ifi_filter_arg); 1505*ca62461bSStephen Hurd if ((result & FILTER_SCHEDULE_THREAD) == 0) 1506*ca62461bSStephen Hurd return (result); 1507*ca62461bSStephen Hurd } 150895246abbSSean Bruno 150995dcf343SMarius Strobl ctx = rxq->ifr_ctx; 151095dcf343SMarius Strobl sc = ctx->ifc_softc; 15111ae4848cSMatt Macy MPASS(rxq->ifr_ntxqirq); 151295246abbSSean Bruno for (i = 0; i < rxq->ifr_ntxqirq; i++) { 151395dcf343SMarius Strobl txqid = rxq->ifr_txqid[i]; 151495dcf343SMarius Strobl txq = &ctx->ifc_txqs[txqid]; 151595dcf343SMarius Strobl bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map, 15168a04b53dSKonstantin Belousov BUS_DMASYNC_POSTREAD); 151795dcf343SMarius Strobl if (!ctx->isc_txd_credits_update(sc, txqid, false)) { 151895246abbSSean Bruno IFDI_TX_QUEUE_INTR_ENABLE(ctx, txqid); 151995246abbSSean Bruno continue; 152095246abbSSean Bruno } 152195dcf343SMarius Strobl GROUPTASK_ENQUEUE(&txq->ift_task); 152295246abbSSean Bruno } 152395246abbSSean Bruno if (ctx->ifc_sctx->isc_flags & IFLIB_HAS_RXCQ) 152495246abbSSean Bruno cidx = rxq->ifr_cq_cidx; 152595246abbSSean Bruno else 152695246abbSSean Bruno cidx = rxq->ifr_fl[0].ifl_cidx; 152795246abbSSean Bruno if (iflib_rxd_avail(ctx, rxq, cidx, 1)) 152895246abbSSean Bruno GROUPTASK_ENQUEUE(gtask); 152964e6fc13SStephen Hurd else { 153095246abbSSean Bruno IFDI_RX_QUEUE_INTR_ENABLE(ctx, rxq->ifr_id); 153164e6fc13SStephen Hurd DBG_COUNTER_INC(rx_intr_enables); 153264e6fc13SStephen Hurd } 153395246abbSSean Bruno return (FILTER_HANDLED); 153495246abbSSean Bruno } 153595246abbSSean Bruno 153695246abbSSean Bruno 153795246abbSSean Bruno static int 153895246abbSSean Bruno iflib_fast_intr_ctx(void *arg) 153995246abbSSean Bruno { 154095246abbSSean Bruno iflib_filter_info_t info = arg; 154195246abbSSean Bruno struct grouptask *gtask = info->ifi_task; 1542*ca62461bSStephen Hurd int result; 15434c7070dbSScott Long 1544bd84f700SSean Bruno if (!iflib_started) 1545*ca62461bSStephen Hurd return (FILTER_STRAY); 15461248952aSSean Bruno 15474c7070dbSScott Long DBG_COUNTER_INC(fast_intrs); 1548*ca62461bSStephen Hurd if (info->ifi_filter != NULL) { 1549*ca62461bSStephen Hurd result = info->ifi_filter(info->ifi_filter_arg); 1550*ca62461bSStephen Hurd if ((result & FILTER_SCHEDULE_THREAD) == 0) 1551*ca62461bSStephen Hurd return (result); 1552*ca62461bSStephen Hurd } 15534c7070dbSScott Long 15544c7070dbSScott Long GROUPTASK_ENQUEUE(gtask); 15554c7070dbSScott Long return (FILTER_HANDLED); 15564c7070dbSScott Long } 15574c7070dbSScott Long 15584c7070dbSScott Long static int 15594c7070dbSScott Long _iflib_irq_alloc(if_ctx_t ctx, if_irq_t irq, int rid, 15604c7070dbSScott Long driver_filter_t filter, driver_intr_t handler, void *arg, 15613e0e6330SStephen Hurd const char *name) 15624c7070dbSScott Long { 15632b2fc973SSean Bruno int rc, flags; 15644c7070dbSScott Long struct resource *res; 15652b2fc973SSean Bruno void *tag = NULL; 15664c7070dbSScott Long device_t dev = ctx->ifc_dev; 15674c7070dbSScott Long 15682b2fc973SSean Bruno flags = RF_ACTIVE; 15692b2fc973SSean Bruno if (ctx->ifc_flags & IFC_LEGACY) 15702b2fc973SSean Bruno flags |= RF_SHAREABLE; 15714c7070dbSScott Long MPASS(rid < 512); 15724c7070dbSScott Long irq->ii_rid = rid; 15732b2fc973SSean Bruno res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &irq->ii_rid, flags); 15744c7070dbSScott Long if (res == NULL) { 15754c7070dbSScott Long device_printf(dev, 15764c7070dbSScott Long "failed to allocate IRQ for rid %d, name %s.\n", rid, name); 15774c7070dbSScott Long return (ENOMEM); 15784c7070dbSScott Long } 15794c7070dbSScott Long irq->ii_res = res; 15804c7070dbSScott Long KASSERT(filter == NULL || handler == NULL, ("filter and handler can't both be non-NULL")); 15814c7070dbSScott Long rc = bus_setup_intr(dev, res, INTR_MPSAFE | INTR_TYPE_NET, 15824c7070dbSScott Long filter, handler, arg, &tag); 15834c7070dbSScott Long if (rc != 0) { 15844c7070dbSScott Long device_printf(dev, 15854c7070dbSScott Long "failed to setup interrupt for rid %d, name %s: %d\n", 15864c7070dbSScott Long rid, name ? name : "unknown", rc); 15874c7070dbSScott Long return (rc); 15884c7070dbSScott Long } else if (name) 1589f454e7ebSJohn Baldwin bus_describe_intr(dev, res, tag, "%s", name); 15904c7070dbSScott Long 15914c7070dbSScott Long irq->ii_tag = tag; 15924c7070dbSScott Long return (0); 15934c7070dbSScott Long } 15944c7070dbSScott Long 15954c7070dbSScott Long 15964c7070dbSScott Long /********************************************************************* 15974c7070dbSScott Long * 1598bfce461eSMarius Strobl * Allocate DMA resources for TX buffers as well as memory for the TX 1599bfce461eSMarius Strobl * mbuf map. TX DMA maps (non-TSO/TSO) and TX mbuf map are kept in a 1600bfce461eSMarius Strobl * iflib_sw_tx_desc_array structure, storing all the information that 1601bfce461eSMarius Strobl * is needed to transmit a packet on the wire. This is called only 1602bfce461eSMarius Strobl * once at attach, setup is done every reset. 16034c7070dbSScott Long * 16044c7070dbSScott Long **********************************************************************/ 16054c7070dbSScott Long static int 16064c7070dbSScott Long iflib_txsd_alloc(iflib_txq_t txq) 16074c7070dbSScott Long { 16084c7070dbSScott Long if_ctx_t ctx = txq->ift_ctx; 16094c7070dbSScott Long if_shared_ctx_t sctx = ctx->ifc_sctx; 16104c7070dbSScott Long if_softc_ctx_t scctx = &ctx->ifc_softc_ctx; 16114c7070dbSScott Long device_t dev = ctx->ifc_dev; 16127f87c040SMarius Strobl bus_size_t tsomaxsize; 16134c7070dbSScott Long int err, nsegments, ntsosegments; 16148a04b53dSKonstantin Belousov bool tso; 16154c7070dbSScott Long 16164c7070dbSScott Long nsegments = scctx->isc_tx_nsegments; 16174c7070dbSScott Long ntsosegments = scctx->isc_tx_tso_segments_max; 16187f87c040SMarius Strobl tsomaxsize = scctx->isc_tx_tso_size_max; 16197f87c040SMarius Strobl if (if_getcapabilities(ctx->ifc_ifp) & IFCAP_VLAN_MTU) 16207f87c040SMarius Strobl tsomaxsize += sizeof(struct ether_vlan_header); 162123ac9029SStephen Hurd MPASS(scctx->isc_ntxd[0] > 0); 162223ac9029SStephen Hurd MPASS(scctx->isc_ntxd[txq->ift_br_offset] > 0); 16234c7070dbSScott Long MPASS(nsegments > 0); 16247f87c040SMarius Strobl if (if_getcapabilities(ctx->ifc_ifp) & IFCAP_TSO) { 16254c7070dbSScott Long MPASS(ntsosegments > 0); 16267f87c040SMarius Strobl MPASS(sctx->isc_tso_maxsize >= tsomaxsize); 16277f87c040SMarius Strobl } 16287f87c040SMarius Strobl 16294c7070dbSScott Long /* 1630bfce461eSMarius Strobl * Set up DMA tags for TX buffers. 16314c7070dbSScott Long */ 16324c7070dbSScott Long if ((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 */ 16374c7070dbSScott Long sctx->isc_tx_maxsize, /* maxsize */ 16384c7070dbSScott Long nsegments, /* nsegments */ 16394c7070dbSScott Long sctx->isc_tx_maxsegsize, /* maxsegsize */ 16404c7070dbSScott Long 0, /* flags */ 16414c7070dbSScott Long NULL, /* lockfunc */ 16424c7070dbSScott Long NULL, /* lockfuncarg */ 1643bfce461eSMarius Strobl &txq->ift_buf_tag))) { 16444c7070dbSScott Long device_printf(dev,"Unable to allocate TX DMA tag: %d\n", err); 16459d0a88deSDimitry Andric device_printf(dev,"maxsize: %ju nsegments: %d maxsegsize: %ju\n", 16469d0a88deSDimitry Andric (uintmax_t)sctx->isc_tx_maxsize, nsegments, (uintmax_t)sctx->isc_tx_maxsegsize); 16474c7070dbSScott Long goto fail; 16484c7070dbSScott Long } 16498a04b53dSKonstantin Belousov tso = (if_getcapabilities(ctx->ifc_ifp) & IFCAP_TSO) != 0; 16508a04b53dSKonstantin Belousov if (tso && (err = bus_dma_tag_create(bus_get_dma_tag(dev), 16514c7070dbSScott Long 1, 0, /* alignment, bounds */ 16524c7070dbSScott Long BUS_SPACE_MAXADDR, /* lowaddr */ 16534c7070dbSScott Long BUS_SPACE_MAXADDR, /* highaddr */ 16544c7070dbSScott Long NULL, NULL, /* filter, filterarg */ 16557f87c040SMarius Strobl tsomaxsize, /* maxsize */ 16564c7070dbSScott Long ntsosegments, /* nsegments */ 16577f87c040SMarius Strobl sctx->isc_tso_maxsegsize,/* maxsegsize */ 16584c7070dbSScott Long 0, /* flags */ 16594c7070dbSScott Long NULL, /* lockfunc */ 16604c7070dbSScott Long NULL, /* lockfuncarg */ 1661bfce461eSMarius Strobl &txq->ift_tso_buf_tag))) { 1662bfce461eSMarius Strobl device_printf(dev, "Unable to allocate TSO TX DMA tag: %d\n", 1663bfce461eSMarius Strobl err); 16644c7070dbSScott Long goto fail; 16654c7070dbSScott Long } 1666bfce461eSMarius Strobl 1667bfce461eSMarius Strobl /* Allocate memory for the TX mbuf map. */ 16684c7070dbSScott Long if (!(txq->ift_sds.ifsd_m = 1669ac2fffa4SPedro F. Giffuni (struct mbuf **) malloc(sizeof(struct mbuf *) * 1670ac2fffa4SPedro F. Giffuni scctx->isc_ntxd[txq->ift_br_offset], M_IFLIB, M_NOWAIT | M_ZERO))) { 1671bfce461eSMarius Strobl device_printf(dev, "Unable to allocate TX mbuf map memory\n"); 16724c7070dbSScott Long err = ENOMEM; 16734c7070dbSScott Long goto fail; 16744c7070dbSScott Long } 16754c7070dbSScott Long 1676bfce461eSMarius Strobl /* 1677bfce461eSMarius Strobl * Create the DMA maps for TX buffers. 1678bfce461eSMarius Strobl */ 16798a04b53dSKonstantin Belousov if ((txq->ift_sds.ifsd_map = (bus_dmamap_t *)malloc( 16808a04b53dSKonstantin Belousov sizeof(bus_dmamap_t) * scctx->isc_ntxd[txq->ift_br_offset], 16818a04b53dSKonstantin Belousov M_IFLIB, M_NOWAIT | M_ZERO)) == NULL) { 1682bfce461eSMarius Strobl device_printf(dev, 1683bfce461eSMarius Strobl "Unable to allocate TX buffer DMA map memory\n"); 16844c7070dbSScott Long err = ENOMEM; 16854c7070dbSScott Long goto fail; 16864c7070dbSScott Long } 16878a04b53dSKonstantin Belousov if (tso && (txq->ift_sds.ifsd_tso_map = (bus_dmamap_t *)malloc( 16888a04b53dSKonstantin Belousov sizeof(bus_dmamap_t) * scctx->isc_ntxd[txq->ift_br_offset], 16898a04b53dSKonstantin Belousov M_IFLIB, M_NOWAIT | M_ZERO)) == NULL) { 1690bfce461eSMarius Strobl device_printf(dev, 1691bfce461eSMarius Strobl "Unable to allocate TSO TX buffer map memory\n"); 16928a04b53dSKonstantin Belousov err = ENOMEM; 16938a04b53dSKonstantin Belousov goto fail; 16948a04b53dSKonstantin Belousov } 169523ac9029SStephen Hurd for (int i = 0; i < scctx->isc_ntxd[txq->ift_br_offset]; i++) { 1696bfce461eSMarius Strobl err = bus_dmamap_create(txq->ift_buf_tag, 0, 16978a04b53dSKonstantin Belousov &txq->ift_sds.ifsd_map[i]); 16984c7070dbSScott Long if (err != 0) { 16994c7070dbSScott Long device_printf(dev, "Unable to create TX DMA map\n"); 17004c7070dbSScott Long goto fail; 17014c7070dbSScott Long } 17028a04b53dSKonstantin Belousov if (!tso) 17038a04b53dSKonstantin Belousov continue; 1704bfce461eSMarius Strobl err = bus_dmamap_create(txq->ift_tso_buf_tag, 0, 17058a04b53dSKonstantin Belousov &txq->ift_sds.ifsd_tso_map[i]); 17068a04b53dSKonstantin Belousov if (err != 0) { 17078a04b53dSKonstantin Belousov device_printf(dev, "Unable to create TSO TX DMA map\n"); 17088a04b53dSKonstantin Belousov goto fail; 17098a04b53dSKonstantin Belousov } 17104c7070dbSScott Long } 17114c7070dbSScott Long return (0); 17124c7070dbSScott Long fail: 17134c7070dbSScott Long /* We free all, it handles case where we are in the middle */ 17144c7070dbSScott Long iflib_tx_structures_free(ctx); 17154c7070dbSScott Long return (err); 17164c7070dbSScott Long } 17174c7070dbSScott Long 17184c7070dbSScott Long static void 17194c7070dbSScott Long iflib_txsd_destroy(if_ctx_t ctx, iflib_txq_t txq, int i) 17204c7070dbSScott Long { 17214c7070dbSScott Long bus_dmamap_t map; 17224c7070dbSScott Long 17234c7070dbSScott Long map = NULL; 17244c7070dbSScott Long if (txq->ift_sds.ifsd_map != NULL) 17254c7070dbSScott Long map = txq->ift_sds.ifsd_map[i]; 17264c7070dbSScott Long if (map != NULL) { 1727bfce461eSMarius Strobl bus_dmamap_sync(txq->ift_buf_tag, map, BUS_DMASYNC_POSTWRITE); 1728bfce461eSMarius Strobl bus_dmamap_unload(txq->ift_buf_tag, map); 1729bfce461eSMarius Strobl bus_dmamap_destroy(txq->ift_buf_tag, map); 17304c7070dbSScott Long txq->ift_sds.ifsd_map[i] = NULL; 17314c7070dbSScott Long } 17328a04b53dSKonstantin Belousov 17338a04b53dSKonstantin Belousov map = NULL; 17348a04b53dSKonstantin Belousov if (txq->ift_sds.ifsd_tso_map != NULL) 17358a04b53dSKonstantin Belousov map = txq->ift_sds.ifsd_tso_map[i]; 17368a04b53dSKonstantin Belousov if (map != NULL) { 1737bfce461eSMarius Strobl bus_dmamap_sync(txq->ift_tso_buf_tag, map, 17388a04b53dSKonstantin Belousov BUS_DMASYNC_POSTWRITE); 1739bfce461eSMarius Strobl bus_dmamap_unload(txq->ift_tso_buf_tag, map); 1740bfce461eSMarius Strobl bus_dmamap_destroy(txq->ift_tso_buf_tag, map); 17418a04b53dSKonstantin Belousov txq->ift_sds.ifsd_tso_map[i] = NULL; 17428a04b53dSKonstantin Belousov } 17434c7070dbSScott Long } 17444c7070dbSScott Long 17454c7070dbSScott Long static void 17464c7070dbSScott Long iflib_txq_destroy(iflib_txq_t txq) 17474c7070dbSScott Long { 17484c7070dbSScott Long if_ctx_t ctx = txq->ift_ctx; 17494c7070dbSScott Long 175023ac9029SStephen Hurd for (int i = 0; i < txq->ift_size; i++) 17514c7070dbSScott Long iflib_txsd_destroy(ctx, txq, i); 17524c7070dbSScott Long if (txq->ift_sds.ifsd_map != NULL) { 17534c7070dbSScott Long free(txq->ift_sds.ifsd_map, M_IFLIB); 17544c7070dbSScott Long txq->ift_sds.ifsd_map = NULL; 17554c7070dbSScott Long } 17568a04b53dSKonstantin Belousov if (txq->ift_sds.ifsd_tso_map != NULL) { 17578a04b53dSKonstantin Belousov free(txq->ift_sds.ifsd_tso_map, M_IFLIB); 17588a04b53dSKonstantin Belousov txq->ift_sds.ifsd_tso_map = NULL; 17598a04b53dSKonstantin Belousov } 17604c7070dbSScott Long if (txq->ift_sds.ifsd_m != NULL) { 17614c7070dbSScott Long free(txq->ift_sds.ifsd_m, M_IFLIB); 17624c7070dbSScott Long txq->ift_sds.ifsd_m = NULL; 17634c7070dbSScott Long } 1764bfce461eSMarius Strobl if (txq->ift_buf_tag != NULL) { 1765bfce461eSMarius Strobl bus_dma_tag_destroy(txq->ift_buf_tag); 1766bfce461eSMarius Strobl txq->ift_buf_tag = NULL; 17674c7070dbSScott Long } 1768bfce461eSMarius Strobl if (txq->ift_tso_buf_tag != NULL) { 1769bfce461eSMarius Strobl bus_dma_tag_destroy(txq->ift_tso_buf_tag); 1770bfce461eSMarius Strobl txq->ift_tso_buf_tag = NULL; 17714c7070dbSScott Long } 17724c7070dbSScott Long } 17734c7070dbSScott Long 17744c7070dbSScott Long static void 17754c7070dbSScott Long iflib_txsd_free(if_ctx_t ctx, iflib_txq_t txq, int i) 17764c7070dbSScott Long { 17774c7070dbSScott Long struct mbuf **mp; 17784c7070dbSScott Long 17794c7070dbSScott Long mp = &txq->ift_sds.ifsd_m[i]; 17804c7070dbSScott Long if (*mp == NULL) 17814c7070dbSScott Long return; 17824c7070dbSScott Long 17834c7070dbSScott Long if (txq->ift_sds.ifsd_map != NULL) { 1784bfce461eSMarius Strobl bus_dmamap_sync(txq->ift_buf_tag, 17858a04b53dSKonstantin Belousov txq->ift_sds.ifsd_map[i], BUS_DMASYNC_POSTWRITE); 1786bfce461eSMarius Strobl bus_dmamap_unload(txq->ift_buf_tag, txq->ift_sds.ifsd_map[i]); 17878a04b53dSKonstantin Belousov } 17888a04b53dSKonstantin Belousov if (txq->ift_sds.ifsd_tso_map != NULL) { 1789bfce461eSMarius Strobl bus_dmamap_sync(txq->ift_tso_buf_tag, 17908a04b53dSKonstantin Belousov txq->ift_sds.ifsd_tso_map[i], BUS_DMASYNC_POSTWRITE); 1791bfce461eSMarius Strobl bus_dmamap_unload(txq->ift_tso_buf_tag, 17928a04b53dSKonstantin Belousov txq->ift_sds.ifsd_tso_map[i]); 17934c7070dbSScott Long } 179423ac9029SStephen Hurd m_free(*mp); 17954c7070dbSScott Long DBG_COUNTER_INC(tx_frees); 17964c7070dbSScott Long *mp = NULL; 17974c7070dbSScott Long } 17984c7070dbSScott Long 17994c7070dbSScott Long static int 18004c7070dbSScott Long iflib_txq_setup(iflib_txq_t txq) 18014c7070dbSScott Long { 18024c7070dbSScott Long if_ctx_t ctx = txq->ift_ctx; 180323ac9029SStephen Hurd if_softc_ctx_t scctx = &ctx->ifc_softc_ctx; 18044d261ce2SStephen Hurd if_shared_ctx_t sctx = ctx->ifc_sctx; 18054c7070dbSScott Long iflib_dma_info_t di; 18064c7070dbSScott Long int i; 18074c7070dbSScott Long 18084c7070dbSScott Long /* Set number of descriptors available */ 18094c7070dbSScott Long txq->ift_qstatus = IFLIB_QUEUE_IDLE; 181095246abbSSean Bruno /* XXX make configurable */ 181195246abbSSean Bruno txq->ift_update_freq = IFLIB_DEFAULT_TX_UPDATE_FREQ; 18124c7070dbSScott Long 18134c7070dbSScott Long /* Reset indices */ 181495246abbSSean Bruno txq->ift_cidx_processed = 0; 181595246abbSSean Bruno txq->ift_pidx = txq->ift_cidx = txq->ift_npending = 0; 181623ac9029SStephen Hurd txq->ift_size = scctx->isc_ntxd[txq->ift_br_offset]; 18174c7070dbSScott Long 18184d261ce2SStephen Hurd for (i = 0, di = txq->ift_ifdi; i < sctx->isc_ntxqs; i++, di++) 18194c7070dbSScott Long bzero((void *)di->idi_vaddr, di->idi_size); 18204c7070dbSScott Long 18214c7070dbSScott Long IFDI_TXQ_SETUP(ctx, txq->ift_id); 18224d261ce2SStephen Hurd for (i = 0, di = txq->ift_ifdi; i < sctx->isc_ntxqs; i++, di++) 18234c7070dbSScott Long bus_dmamap_sync(di->idi_tag, di->idi_map, 18244c7070dbSScott Long BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 18254c7070dbSScott Long return (0); 18264c7070dbSScott Long } 18274c7070dbSScott Long 18284c7070dbSScott Long /********************************************************************* 18294c7070dbSScott Long * 1830bfce461eSMarius Strobl * Allocate DMA resources for RX buffers as well as memory for the RX 1831bfce461eSMarius Strobl * mbuf map, direct RX cluster pointer map and RX cluster bus address 1832bfce461eSMarius Strobl * map. RX DMA map, RX mbuf map, direct RX cluster pointer map and 1833bfce461eSMarius Strobl * RX cluster map are kept in a iflib_sw_rx_desc_array structure. 1834bfce461eSMarius Strobl * Since we use use one entry in iflib_sw_rx_desc_array per received 1835bfce461eSMarius Strobl * packet, the maximum number of entries we'll need is equal to the 1836bfce461eSMarius Strobl * number of hardware receive descriptors that we've allocated. 18374c7070dbSScott Long * 18384c7070dbSScott Long **********************************************************************/ 18394c7070dbSScott Long static int 18404c7070dbSScott Long iflib_rxsd_alloc(iflib_rxq_t rxq) 18414c7070dbSScott Long { 18424c7070dbSScott Long if_ctx_t ctx = rxq->ifr_ctx; 18434c7070dbSScott Long if_shared_ctx_t sctx = ctx->ifc_sctx; 184423ac9029SStephen Hurd if_softc_ctx_t scctx = &ctx->ifc_softc_ctx; 18454c7070dbSScott Long device_t dev = ctx->ifc_dev; 18464c7070dbSScott Long iflib_fl_t fl; 18474c7070dbSScott Long int err; 18484c7070dbSScott Long 184923ac9029SStephen Hurd MPASS(scctx->isc_nrxd[0] > 0); 185023ac9029SStephen Hurd MPASS(scctx->isc_nrxd[rxq->ifr_fl_offset] > 0); 18514c7070dbSScott Long 18524c7070dbSScott Long fl = rxq->ifr_fl; 18534c7070dbSScott Long for (int i = 0; i < rxq->ifr_nfl; i++, fl++) { 185423ac9029SStephen Hurd fl->ifl_size = scctx->isc_nrxd[rxq->ifr_fl_offset]; /* this isn't necessarily the same */ 1855bfce461eSMarius Strobl /* Set up DMA tag for RX buffers. */ 18564c7070dbSScott Long err = bus_dma_tag_create(bus_get_dma_tag(dev), /* parent */ 18574c7070dbSScott Long 1, 0, /* alignment, bounds */ 18584c7070dbSScott Long BUS_SPACE_MAXADDR, /* lowaddr */ 18594c7070dbSScott Long BUS_SPACE_MAXADDR, /* highaddr */ 18604c7070dbSScott Long NULL, NULL, /* filter, filterarg */ 18614c7070dbSScott Long sctx->isc_rx_maxsize, /* maxsize */ 18624c7070dbSScott Long sctx->isc_rx_nsegments, /* nsegments */ 18634c7070dbSScott Long sctx->isc_rx_maxsegsize, /* maxsegsize */ 18644c7070dbSScott Long 0, /* flags */ 18654c7070dbSScott Long NULL, /* lockfunc */ 18664c7070dbSScott Long NULL, /* lockarg */ 1867bfce461eSMarius Strobl &fl->ifl_buf_tag); 18684c7070dbSScott Long if (err) { 1869bfce461eSMarius Strobl device_printf(dev, 1870bfce461eSMarius Strobl "Unable to allocate RX DMA tag: %d\n", err); 18714c7070dbSScott Long goto fail; 18724c7070dbSScott Long } 1873bfce461eSMarius Strobl 1874bfce461eSMarius Strobl /* Allocate memory for the RX mbuf map. */ 1875e035717eSSean Bruno if (!(fl->ifl_sds.ifsd_m = 1876ac2fffa4SPedro F. Giffuni (struct mbuf **) malloc(sizeof(struct mbuf *) * 1877ac2fffa4SPedro F. Giffuni scctx->isc_nrxd[rxq->ifr_fl_offset], M_IFLIB, M_NOWAIT | M_ZERO))) { 1878bfce461eSMarius Strobl device_printf(dev, 1879bfce461eSMarius Strobl "Unable to allocate RX mbuf map memory\n"); 1880e035717eSSean Bruno err = ENOMEM; 1881e035717eSSean Bruno goto fail; 1882e035717eSSean Bruno } 1883bfce461eSMarius Strobl 1884bfce461eSMarius Strobl /* Allocate memory for the direct RX cluster pointer map. */ 1885e035717eSSean Bruno if (!(fl->ifl_sds.ifsd_cl = 1886ac2fffa4SPedro F. Giffuni (caddr_t *) malloc(sizeof(caddr_t) * 1887ac2fffa4SPedro F. Giffuni scctx->isc_nrxd[rxq->ifr_fl_offset], M_IFLIB, M_NOWAIT | M_ZERO))) { 1888bfce461eSMarius Strobl device_printf(dev, 1889bfce461eSMarius Strobl "Unable to allocate RX cluster map memory\n"); 1890e035717eSSean Bruno err = ENOMEM; 1891e035717eSSean Bruno goto fail; 1892e035717eSSean Bruno } 18934c7070dbSScott Long 1894bfce461eSMarius Strobl /* Allocate memory for the RX cluster bus address map. */ 1895fbec776dSAndrew Gallatin if (!(fl->ifl_sds.ifsd_ba = 1896fbec776dSAndrew Gallatin (bus_addr_t *) malloc(sizeof(bus_addr_t) * 1897fbec776dSAndrew Gallatin scctx->isc_nrxd[rxq->ifr_fl_offset], M_IFLIB, M_NOWAIT | M_ZERO))) { 1898bfce461eSMarius Strobl device_printf(dev, 1899bfce461eSMarius Strobl "Unable to allocate RX bus address map memory\n"); 1900fbec776dSAndrew Gallatin err = ENOMEM; 1901fbec776dSAndrew Gallatin goto fail; 1902fbec776dSAndrew Gallatin } 1903e035717eSSean Bruno 1904bfce461eSMarius Strobl /* 1905bfce461eSMarius Strobl * Create the DMA maps for RX buffers. 1906bfce461eSMarius Strobl */ 1907e035717eSSean Bruno if (!(fl->ifl_sds.ifsd_map = 1908ac2fffa4SPedro F. Giffuni (bus_dmamap_t *) malloc(sizeof(bus_dmamap_t) * scctx->isc_nrxd[rxq->ifr_fl_offset], M_IFLIB, M_NOWAIT | M_ZERO))) { 1909bfce461eSMarius Strobl device_printf(dev, 1910bfce461eSMarius Strobl "Unable to allocate RX buffer DMA map memory\n"); 1911e035717eSSean Bruno err = ENOMEM; 1912e035717eSSean Bruno goto fail; 1913e035717eSSean Bruno } 1914e035717eSSean Bruno for (int i = 0; i < scctx->isc_nrxd[rxq->ifr_fl_offset]; i++) { 1915bfce461eSMarius Strobl err = bus_dmamap_create(fl->ifl_buf_tag, 0, 1916bfce461eSMarius Strobl &fl->ifl_sds.ifsd_map[i]); 1917e035717eSSean Bruno if (err != 0) { 191895246abbSSean Bruno device_printf(dev, "Unable to create RX buffer DMA map\n"); 19194c7070dbSScott Long goto fail; 19204c7070dbSScott Long } 19214c7070dbSScott Long } 1922835809f9SSean Bruno } 19234c7070dbSScott Long return (0); 19244c7070dbSScott Long 19254c7070dbSScott Long fail: 19264c7070dbSScott Long iflib_rx_structures_free(ctx); 19274c7070dbSScott Long return (err); 19284c7070dbSScott Long } 19294c7070dbSScott Long 19304c7070dbSScott Long 19314c7070dbSScott Long /* 19324c7070dbSScott Long * Internal service routines 19334c7070dbSScott Long */ 19344c7070dbSScott Long 19354c7070dbSScott Long struct rxq_refill_cb_arg { 19364c7070dbSScott Long int error; 19374c7070dbSScott Long bus_dma_segment_t seg; 19384c7070dbSScott Long int nseg; 19394c7070dbSScott Long }; 19404c7070dbSScott Long 19414c7070dbSScott Long static void 19424c7070dbSScott Long _rxq_refill_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error) 19434c7070dbSScott Long { 19444c7070dbSScott Long struct rxq_refill_cb_arg *cb_arg = arg; 19454c7070dbSScott Long 19464c7070dbSScott Long cb_arg->error = error; 19474c7070dbSScott Long cb_arg->seg = segs[0]; 19484c7070dbSScott Long cb_arg->nseg = nseg; 19494c7070dbSScott Long } 19504c7070dbSScott Long 19514c7070dbSScott Long /** 19524c7070dbSScott Long * rxq_refill - refill an rxq free-buffer list 19534c7070dbSScott Long * @ctx: the iflib context 19544c7070dbSScott Long * @rxq: the free-list to refill 19554c7070dbSScott Long * @n: the number of new buffers to allocate 19564c7070dbSScott Long * 19574c7070dbSScott Long * (Re)populate an rxq free-buffer list with up to @n new packet buffers. 19584c7070dbSScott Long * The caller must assure that @n does not exceed the queue's capacity. 19594c7070dbSScott Long */ 19604c7070dbSScott Long static void 19614c7070dbSScott Long _iflib_fl_refill(if_ctx_t ctx, iflib_fl_t fl, int count) 19624c7070dbSScott Long { 196395246abbSSean Bruno struct if_rxd_update iru; 1964fbec776dSAndrew Gallatin struct rxq_refill_cb_arg cb_arg; 19653db348b5SMarius Strobl struct mbuf *m; 19663db348b5SMarius Strobl caddr_t cl, *sd_cl; 19673db348b5SMarius Strobl struct mbuf **sd_m; 1968e035717eSSean Bruno bus_dmamap_t *sd_map; 1969fbec776dSAndrew Gallatin bus_addr_t bus_addr, *sd_ba; 19703db348b5SMarius Strobl int err, frag_idx, i, idx, n, pidx; 1971a1b799caSStephen Hurd qidx_t credits; 19724c7070dbSScott Long 1973e035717eSSean Bruno sd_m = fl->ifl_sds.ifsd_m; 1974e035717eSSean Bruno sd_map = fl->ifl_sds.ifsd_map; 1975e035717eSSean Bruno sd_cl = fl->ifl_sds.ifsd_cl; 1976fbec776dSAndrew Gallatin sd_ba = fl->ifl_sds.ifsd_ba; 19773db348b5SMarius Strobl pidx = fl->ifl_pidx; 1978e035717eSSean Bruno idx = pidx; 19793db348b5SMarius Strobl frag_idx = fl->ifl_fragidx; 1980a1b799caSStephen Hurd credits = fl->ifl_credits; 1981e035717eSSean Bruno 19823db348b5SMarius Strobl i = 0; 19834c7070dbSScott Long n = count; 19844c7070dbSScott Long MPASS(n > 0); 1985a1b799caSStephen Hurd MPASS(credits + n <= fl->ifl_size); 19864c7070dbSScott Long 19874c7070dbSScott Long if (pidx < fl->ifl_cidx) 19884c7070dbSScott Long MPASS(pidx + n <= fl->ifl_cidx); 1989a1b799caSStephen Hurd if (pidx == fl->ifl_cidx && (credits < fl->ifl_size)) 19904c7070dbSScott Long MPASS(fl->ifl_gen == 0); 19914c7070dbSScott Long if (pidx > fl->ifl_cidx) 19924c7070dbSScott Long MPASS(n <= fl->ifl_size - pidx + fl->ifl_cidx); 19934c7070dbSScott Long 19944c7070dbSScott Long DBG_COUNTER_INC(fl_refills); 19954c7070dbSScott Long if (n > 8) 19964c7070dbSScott Long DBG_COUNTER_INC(fl_refills_large); 19972d873474SStephen Hurd iru_init(&iru, fl->ifl_rxq, fl->ifl_id); 19984c7070dbSScott Long while (n--) { 19994c7070dbSScott Long /* 20004c7070dbSScott Long * We allocate an uninitialized mbuf + cluster, mbuf is 20014c7070dbSScott Long * initialized after rx. 20024c7070dbSScott Long * 20034c7070dbSScott Long * If the cluster is still set then we know a minimum sized packet was received 20044c7070dbSScott Long */ 20053db348b5SMarius Strobl bit_ffc_at(fl->ifl_rx_bitmap, frag_idx, fl->ifl_size, 20063db348b5SMarius Strobl &frag_idx); 20073db348b5SMarius Strobl if (frag_idx < 0) 200887890dbaSSean Bruno bit_ffc(fl->ifl_rx_bitmap, fl->ifl_size, &frag_idx); 20093db348b5SMarius Strobl MPASS(frag_idx >= 0); 201087890dbaSSean Bruno if ((cl = sd_cl[frag_idx]) == NULL) { 2011fbec776dSAndrew Gallatin if ((cl = m_cljget(NULL, M_NOWAIT, fl->ifl_buf_size)) == NULL) 20124c7070dbSScott Long break; 20134c7070dbSScott Long 20144c7070dbSScott Long cb_arg.error = 0; 201595246abbSSean Bruno MPASS(sd_map != NULL); 2016bfce461eSMarius Strobl err = bus_dmamap_load(fl->ifl_buf_tag, sd_map[frag_idx], 20178a04b53dSKonstantin Belousov cl, fl->ifl_buf_size, _rxq_refill_cb, &cb_arg, 20188a04b53dSKonstantin Belousov BUS_DMA_NOWAIT); 20194c7070dbSScott Long if (err != 0 || cb_arg.error) { 20204c7070dbSScott Long /* 20214c7070dbSScott Long * !zone_pack ? 20224c7070dbSScott Long */ 20234c7070dbSScott Long if (fl->ifl_zone == zone_pack) 20244c7070dbSScott Long uma_zfree(fl->ifl_zone, cl); 2025fbec776dSAndrew Gallatin break; 20264c7070dbSScott Long } 20274c7070dbSScott Long 2028fbec776dSAndrew Gallatin sd_ba[frag_idx] = bus_addr = cb_arg.seg.ds_addr; 202987890dbaSSean Bruno sd_cl[frag_idx] = cl; 2030fbec776dSAndrew Gallatin #if MEMORY_LOGGING 2031fbec776dSAndrew Gallatin fl->ifl_cl_enqueued++; 2032fbec776dSAndrew Gallatin #endif 2033fbec776dSAndrew Gallatin } else { 2034fbec776dSAndrew Gallatin bus_addr = sd_ba[frag_idx]; 2035fbec776dSAndrew Gallatin } 203695dcf343SMarius Strobl bus_dmamap_sync(fl->ifl_buf_tag, sd_map[frag_idx], 203795dcf343SMarius Strobl BUS_DMASYNC_PREREAD); 2038fbec776dSAndrew Gallatin 2039fbec776dSAndrew Gallatin MPASS(sd_m[frag_idx] == NULL); 2040fbec776dSAndrew Gallatin if ((m = m_gethdr(M_NOWAIT, MT_NOINIT)) == NULL) { 2041fbec776dSAndrew Gallatin break; 2042fbec776dSAndrew Gallatin } 204387890dbaSSean Bruno sd_m[frag_idx] = m; 20443db348b5SMarius Strobl bit_set(fl->ifl_rx_bitmap, frag_idx); 2045fbec776dSAndrew Gallatin #if MEMORY_LOGGING 2046fbec776dSAndrew Gallatin fl->ifl_m_enqueued++; 2047fbec776dSAndrew Gallatin #endif 2048fbec776dSAndrew Gallatin 2049fbec776dSAndrew Gallatin DBG_COUNTER_INC(rx_allocs); 205087890dbaSSean Bruno fl->ifl_rxd_idxs[i] = frag_idx; 20514c7070dbSScott Long fl->ifl_bus_addrs[i] = bus_addr; 20524c7070dbSScott Long fl->ifl_vm_addrs[i] = cl; 2053a1b799caSStephen Hurd credits++; 20544c7070dbSScott Long i++; 2055a1b799caSStephen Hurd MPASS(credits <= fl->ifl_size); 2056e035717eSSean Bruno if (++idx == fl->ifl_size) { 20574c7070dbSScott Long fl->ifl_gen = 1; 2058e035717eSSean Bruno idx = 0; 20594c7070dbSScott Long } 20604c7070dbSScott Long if (n == 0 || i == IFLIB_MAX_RX_REFRESH) { 206195246abbSSean Bruno iru.iru_pidx = pidx; 206295246abbSSean Bruno iru.iru_count = i; 206395246abbSSean Bruno ctx->isc_rxd_refill(ctx->ifc_softc, &iru); 20644c7070dbSScott Long i = 0; 2065e035717eSSean Bruno pidx = idx; 2066fa5416a8SSean Bruno fl->ifl_pidx = idx; 2067a1b799caSStephen Hurd fl->ifl_credits = credits; 206887890dbaSSean Bruno } 20694c7070dbSScott Long } 2070fbec776dSAndrew Gallatin 2071a1b799caSStephen Hurd if (i) { 2072a1b799caSStephen Hurd iru.iru_pidx = pidx; 2073a1b799caSStephen Hurd iru.iru_count = i; 2074a1b799caSStephen Hurd ctx->isc_rxd_refill(ctx->ifc_softc, &iru); 2075a1b799caSStephen Hurd fl->ifl_pidx = idx; 2076a1b799caSStephen Hurd fl->ifl_credits = credits; 2077a1b799caSStephen Hurd } 20784c7070dbSScott Long DBG_COUNTER_INC(rxd_flush); 20794c7070dbSScott Long if (fl->ifl_pidx == 0) 20804c7070dbSScott Long pidx = fl->ifl_size - 1; 20814c7070dbSScott Long else 20824c7070dbSScott Long pidx = fl->ifl_pidx - 1; 208395246abbSSean Bruno 208495246abbSSean Bruno bus_dmamap_sync(fl->ifl_ifdi->idi_tag, fl->ifl_ifdi->idi_map, 208595246abbSSean Bruno BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 20864c7070dbSScott Long ctx->isc_rxd_flush(ctx->ifc_softc, fl->ifl_rxq->ifr_id, fl->ifl_id, pidx); 208787890dbaSSean Bruno fl->ifl_fragidx = frag_idx; 20884c7070dbSScott Long } 20894c7070dbSScott Long 20904c7070dbSScott Long static __inline void 20914c7070dbSScott Long __iflib_fl_refill_lt(if_ctx_t ctx, iflib_fl_t fl, int max) 20924c7070dbSScott Long { 20934c7070dbSScott Long /* we avoid allowing pidx to catch up with cidx as it confuses ixl */ 20944c7070dbSScott Long int32_t reclaimable = fl->ifl_size - fl->ifl_credits - 1; 20954c7070dbSScott Long #ifdef INVARIANTS 20964c7070dbSScott Long int32_t delta = fl->ifl_size - get_inuse(fl->ifl_size, fl->ifl_cidx, fl->ifl_pidx, fl->ifl_gen) - 1; 20974c7070dbSScott Long #endif 20984c7070dbSScott Long 20994c7070dbSScott Long MPASS(fl->ifl_credits <= fl->ifl_size); 21004c7070dbSScott Long MPASS(reclaimable == delta); 21014c7070dbSScott Long 21024c7070dbSScott Long if (reclaimable > 0) 21034c7070dbSScott Long _iflib_fl_refill(ctx, fl, min(max, reclaimable)); 21044c7070dbSScott Long } 21054c7070dbSScott Long 210677c1fcecSEric Joyner uint8_t 210777c1fcecSEric Joyner iflib_in_detach(if_ctx_t ctx) 210877c1fcecSEric Joyner { 210977c1fcecSEric Joyner bool in_detach; 211077c1fcecSEric Joyner STATE_LOCK(ctx); 211177c1fcecSEric Joyner in_detach = !!(ctx->ifc_flags & IFC_IN_DETACH); 211277c1fcecSEric Joyner STATE_UNLOCK(ctx); 211377c1fcecSEric Joyner return (in_detach); 211477c1fcecSEric Joyner } 211577c1fcecSEric Joyner 21164c7070dbSScott Long static void 21174c7070dbSScott Long iflib_fl_bufs_free(iflib_fl_t fl) 21184c7070dbSScott Long { 21194c7070dbSScott Long iflib_dma_info_t idi = fl->ifl_ifdi; 21208a04b53dSKonstantin Belousov bus_dmamap_t sd_map; 21214c7070dbSScott Long uint32_t i; 21224c7070dbSScott Long 21234c7070dbSScott Long for (i = 0; i < fl->ifl_size; i++) { 2124e035717eSSean Bruno struct mbuf **sd_m = &fl->ifl_sds.ifsd_m[i]; 2125e035717eSSean Bruno caddr_t *sd_cl = &fl->ifl_sds.ifsd_cl[i]; 21264c7070dbSScott Long 2127fbec776dSAndrew Gallatin if (*sd_cl != NULL) { 21288a04b53dSKonstantin Belousov sd_map = fl->ifl_sds.ifsd_map[i]; 2129bfce461eSMarius Strobl bus_dmamap_sync(fl->ifl_buf_tag, sd_map, 21308a04b53dSKonstantin Belousov BUS_DMASYNC_POSTREAD); 2131bfce461eSMarius Strobl bus_dmamap_unload(fl->ifl_buf_tag, sd_map); 2132fbec776dSAndrew Gallatin if (*sd_cl != NULL) 2133fbec776dSAndrew Gallatin uma_zfree(fl->ifl_zone, *sd_cl); 213477c1fcecSEric Joyner // XXX: Should this get moved out? 213577c1fcecSEric Joyner if (iflib_in_detach(fl->ifl_rxq->ifr_ctx)) 2136bfce461eSMarius Strobl bus_dmamap_destroy(fl->ifl_buf_tag, sd_map); 2137e035717eSSean Bruno if (*sd_m != NULL) { 2138e035717eSSean Bruno m_init(*sd_m, M_NOWAIT, MT_DATA, 0); 2139e035717eSSean Bruno uma_zfree(zone_mbuf, *sd_m); 2140e035717eSSean Bruno } 21414c7070dbSScott Long } else { 2142e035717eSSean Bruno MPASS(*sd_cl == NULL); 2143e035717eSSean Bruno MPASS(*sd_m == NULL); 21444c7070dbSScott Long } 21454c7070dbSScott Long #if MEMORY_LOGGING 21464c7070dbSScott Long fl->ifl_m_dequeued++; 21474c7070dbSScott Long fl->ifl_cl_dequeued++; 21484c7070dbSScott Long #endif 2149e035717eSSean Bruno *sd_cl = NULL; 2150e035717eSSean Bruno *sd_m = NULL; 21514c7070dbSScott Long } 215295246abbSSean Bruno #ifdef INVARIANTS 215395246abbSSean Bruno for (i = 0; i < fl->ifl_size; i++) { 215495246abbSSean Bruno MPASS(fl->ifl_sds.ifsd_cl[i] == NULL); 215595246abbSSean Bruno MPASS(fl->ifl_sds.ifsd_m[i] == NULL); 215695246abbSSean Bruno } 215795246abbSSean Bruno #endif 21584c7070dbSScott Long /* 21594c7070dbSScott Long * Reset free list values 21604c7070dbSScott Long */ 216187890dbaSSean Bruno fl->ifl_credits = fl->ifl_cidx = fl->ifl_pidx = fl->ifl_gen = fl->ifl_fragidx = 0; 21624c7070dbSScott Long bzero(idi->idi_vaddr, idi->idi_size); 21634c7070dbSScott Long } 21644c7070dbSScott Long 21654c7070dbSScott Long /********************************************************************* 21664c7070dbSScott Long * 21674c7070dbSScott Long * Initialize a receive ring and its buffers. 21684c7070dbSScott Long * 21694c7070dbSScott Long **********************************************************************/ 21704c7070dbSScott Long static int 21714c7070dbSScott Long iflib_fl_setup(iflib_fl_t fl) 21724c7070dbSScott Long { 21734c7070dbSScott Long iflib_rxq_t rxq = fl->ifl_rxq; 21744c7070dbSScott Long if_ctx_t ctx = rxq->ifr_ctx; 21754c7070dbSScott Long if_softc_ctx_t sctx = &ctx->ifc_softc_ctx; 21764c7070dbSScott Long 21777274b2f6SStephen Hurd bit_nclear(fl->ifl_rx_bitmap, 0, fl->ifl_size - 1); 21784c7070dbSScott Long /* 21794c7070dbSScott Long ** Free current RX buffer structs and their mbufs 21804c7070dbSScott Long */ 21814c7070dbSScott Long iflib_fl_bufs_free(fl); 21824c7070dbSScott Long /* Now replenish the mbufs */ 21834c7070dbSScott Long MPASS(fl->ifl_credits == 0); 21844c7070dbSScott Long /* 21854c7070dbSScott Long * XXX don't set the max_frame_size to larger 21864c7070dbSScott Long * than the hardware can handle 21874c7070dbSScott Long */ 21884c7070dbSScott Long if (sctx->isc_max_frame_size <= 2048) 21894c7070dbSScott Long fl->ifl_buf_size = MCLBYTES; 219095246abbSSean Bruno #ifndef CONTIGMALLOC_WORKS 219195246abbSSean Bruno else 219295246abbSSean Bruno fl->ifl_buf_size = MJUMPAGESIZE; 219395246abbSSean Bruno #else 21944c7070dbSScott Long else if (sctx->isc_max_frame_size <= 4096) 21954c7070dbSScott Long fl->ifl_buf_size = MJUMPAGESIZE; 21964c7070dbSScott Long else if (sctx->isc_max_frame_size <= 9216) 21974c7070dbSScott Long fl->ifl_buf_size = MJUM9BYTES; 21984c7070dbSScott Long else 21994c7070dbSScott Long fl->ifl_buf_size = MJUM16BYTES; 220095246abbSSean Bruno #endif 22014c7070dbSScott Long if (fl->ifl_buf_size > ctx->ifc_max_fl_buf_size) 22024c7070dbSScott Long ctx->ifc_max_fl_buf_size = fl->ifl_buf_size; 22034c7070dbSScott Long fl->ifl_cltype = m_gettype(fl->ifl_buf_size); 22044c7070dbSScott Long fl->ifl_zone = m_getzone(fl->ifl_buf_size); 22054c7070dbSScott Long 22064c7070dbSScott Long 22074c7070dbSScott Long /* avoid pre-allocating zillions of clusters to an idle card 22084c7070dbSScott Long * potentially speeding up attach 22094c7070dbSScott Long */ 22104c7070dbSScott Long _iflib_fl_refill(ctx, fl, min(128, fl->ifl_size)); 22114c7070dbSScott Long MPASS(min(128, fl->ifl_size) == fl->ifl_credits); 22124c7070dbSScott Long if (min(128, fl->ifl_size) != 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++) { 224177102fd6SAndrew Gallatin if (fl->ifl_sds.ifsd_map[j] == 22428a04b53dSKonstantin Belousov NULL) 22438a04b53dSKonstantin Belousov continue; 22448a04b53dSKonstantin Belousov bus_dmamap_sync( 2245bfce461eSMarius Strobl fl->ifl_buf_tag, 224677102fd6SAndrew Gallatin fl->ifl_sds.ifsd_map[j], 22478a04b53dSKonstantin Belousov BUS_DMASYNC_POSTREAD); 22488a04b53dSKonstantin Belousov bus_dmamap_unload( 2249bfce461eSMarius Strobl fl->ifl_buf_tag, 225077102fd6SAndrew Gallatin 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; 22674c7070dbSScott Long rxq->ifr_cq_gen = rxq->ifr_cq_cidx = rxq->ifr_cq_pidx = 0; 22684c7070dbSScott Long } 22694c7070dbSScott Long } 22704c7070dbSScott Long 22714c7070dbSScott Long /* 22724c7070dbSScott Long * MI independent logic 22734c7070dbSScott Long * 22744c7070dbSScott Long */ 22754c7070dbSScott Long static void 22764c7070dbSScott Long iflib_timer(void *arg) 22774c7070dbSScott Long { 2278ab2e3f79SStephen Hurd iflib_txq_t txq = arg; 22794c7070dbSScott Long if_ctx_t ctx = txq->ift_ctx; 2280ab2e3f79SStephen Hurd if_softc_ctx_t sctx = &ctx->ifc_softc_ctx; 2281dd7fbcf1SStephen Hurd uint64_t this_tick = ticks; 2282dd7fbcf1SStephen Hurd uint32_t reset_on = hz / 2; 22834c7070dbSScott Long 22844c7070dbSScott Long if (!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING)) 22854c7070dbSScott Long return; 22864c7070dbSScott Long /* 22874c7070dbSScott Long ** Check on the state of the TX queue(s), this 22884c7070dbSScott Long ** can be done without the lock because its RO 22894c7070dbSScott Long ** and the HUNG state will be static if set. 22904c7070dbSScott Long */ 2291dd7fbcf1SStephen Hurd if (this_tick - txq->ift_last_timer_tick >= hz / 2) { 2292dd7fbcf1SStephen Hurd txq->ift_last_timer_tick = this_tick; 2293ab2e3f79SStephen Hurd IFDI_TIMER(ctx, txq->ift_id); 2294ab2e3f79SStephen Hurd if ((txq->ift_qstatus == IFLIB_QUEUE_HUNG) && 2295ab2e3f79SStephen Hurd ((txq->ift_cleaned_prev == txq->ift_cleaned) || 2296ab2e3f79SStephen Hurd (sctx->isc_pause_frames == 0))) 2297ab2e3f79SStephen Hurd goto hung; 2298a9693502SSean Bruno 2299ab2e3f79SStephen Hurd if (ifmp_ring_is_stalled(txq->ift_br)) 2300ab2e3f79SStephen Hurd txq->ift_qstatus = IFLIB_QUEUE_HUNG; 2301ab2e3f79SStephen Hurd txq->ift_cleaned_prev = txq->ift_cleaned; 2302dd7fbcf1SStephen Hurd } 2303dd7fbcf1SStephen Hurd #ifdef DEV_NETMAP 2304dd7fbcf1SStephen Hurd if (if_getcapenable(ctx->ifc_ifp) & IFCAP_NETMAP) 230595dcf343SMarius Strobl iflib_netmap_timer_adjust(ctx, txq, &reset_on); 2306dd7fbcf1SStephen Hurd #endif 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) 2313dd7fbcf1SStephen Hurd callout_reset_on(&txq->ift_timer, reset_on, iflib_timer, txq, txq->ift_timer.c_cpu); 2314ab2e3f79SStephen Hurd return; 2315ab2e3f79SStephen Hurd hung: 2316ab2e3f79SStephen Hurd device_printf(ctx->ifc_dev, "TX(%d) desc avail = %d, pidx = %d\n", 2317ab2e3f79SStephen Hurd txq->ift_id, TXQ_AVAIL(txq), txq->ift_pidx); 23187b610b60SSean Bruno STATE_LOCK(ctx); 23197b610b60SSean Bruno if_setdrvflagbits(ctx->ifc_ifp, IFF_DRV_OACTIVE, IFF_DRV_RUNNING); 23207b610b60SSean Bruno ctx->ifc_flags |= (IFC_DO_WATCHDOG|IFC_DO_RESET); 2321940f62d6SEric Joyner iflib_admin_intr_deferred(ctx); 232246fa0c25SEric Joyner STATE_UNLOCK(ctx); 23234c7070dbSScott Long } 23244c7070dbSScott Long 23254c7070dbSScott Long static void 23264c7070dbSScott Long iflib_init_locked(if_ctx_t ctx) 23274c7070dbSScott Long { 23284c7070dbSScott Long if_softc_ctx_t sctx = &ctx->ifc_softc_ctx; 23291248952aSSean Bruno if_softc_ctx_t scctx = &ctx->ifc_softc_ctx; 23304c7070dbSScott Long if_t ifp = ctx->ifc_ifp; 23314c7070dbSScott Long iflib_fl_t fl; 23324c7070dbSScott Long iflib_txq_t txq; 23334c7070dbSScott Long iflib_rxq_t rxq; 2334ab2e3f79SStephen Hurd int i, j, tx_ip_csum_flags, tx_ip6_csum_flags; 23354c7070dbSScott Long 23364c7070dbSScott Long 23374c7070dbSScott Long if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, IFF_DRV_RUNNING); 23384c7070dbSScott Long IFDI_INTR_DISABLE(ctx); 23394c7070dbSScott Long 23401248952aSSean Bruno tx_ip_csum_flags = scctx->isc_tx_csum_flags & (CSUM_IP | CSUM_TCP | CSUM_UDP | CSUM_SCTP); 23411248952aSSean Bruno tx_ip6_csum_flags = scctx->isc_tx_csum_flags & (CSUM_IP6_TCP | CSUM_IP6_UDP | CSUM_IP6_SCTP); 23424c7070dbSScott Long /* Set hardware offload abilities */ 23434c7070dbSScott Long if_clearhwassist(ifp); 23444c7070dbSScott Long if (if_getcapenable(ifp) & IFCAP_TXCSUM) 23451248952aSSean Bruno if_sethwassistbits(ifp, tx_ip_csum_flags, 0); 23464c7070dbSScott Long if (if_getcapenable(ifp) & IFCAP_TXCSUM_IPV6) 23471248952aSSean Bruno if_sethwassistbits(ifp, tx_ip6_csum_flags, 0); 23484c7070dbSScott Long if (if_getcapenable(ifp) & IFCAP_TSO4) 23494c7070dbSScott Long if_sethwassistbits(ifp, CSUM_IP_TSO, 0); 23504c7070dbSScott Long if (if_getcapenable(ifp) & IFCAP_TSO6) 23514c7070dbSScott Long if_sethwassistbits(ifp, CSUM_IP6_TSO, 0); 23524c7070dbSScott Long 23534c7070dbSScott Long for (i = 0, txq = ctx->ifc_txqs; i < sctx->isc_ntxqsets; i++, txq++) { 23544c7070dbSScott Long CALLOUT_LOCK(txq); 23554c7070dbSScott Long callout_stop(&txq->ift_timer); 23564c7070dbSScott Long CALLOUT_UNLOCK(txq); 23574c7070dbSScott Long iflib_netmap_txq_init(ctx, txq); 23584c7070dbSScott Long } 235923ac9029SStephen Hurd #ifdef INVARIANTS 236023ac9029SStephen Hurd i = if_getdrvflags(ifp); 236123ac9029SStephen Hurd #endif 23624c7070dbSScott Long IFDI_INIT(ctx); 236323ac9029SStephen Hurd MPASS(if_getdrvflags(ifp) == i); 23644c7070dbSScott Long for (i = 0, rxq = ctx->ifc_rxqs; i < sctx->isc_nrxqsets; i++, rxq++) { 236595246abbSSean Bruno /* XXX this should really be done on a per-queue basis */ 2366d0d0ad0aSStephen Hurd if (if_getcapenable(ifp) & IFCAP_NETMAP) { 2367d0d0ad0aSStephen Hurd MPASS(rxq->ifr_id == i); 2368d0d0ad0aSStephen Hurd iflib_netmap_rxq_init(ctx, rxq); 236995246abbSSean Bruno continue; 2370d0d0ad0aSStephen Hurd } 23714c7070dbSScott Long for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++) { 23724c7070dbSScott Long if (iflib_fl_setup(fl)) { 23734c7070dbSScott Long device_printf(ctx->ifc_dev, "freelist setup failed - check cluster settings\n"); 23744c7070dbSScott Long goto done; 23754c7070dbSScott Long } 23764c7070dbSScott Long } 23774c7070dbSScott Long } 23784c7070dbSScott Long done: 23794c7070dbSScott Long if_setdrvflagbits(ctx->ifc_ifp, IFF_DRV_RUNNING, IFF_DRV_OACTIVE); 23804c7070dbSScott Long IFDI_INTR_ENABLE(ctx); 23814c7070dbSScott Long txq = ctx->ifc_txqs; 23824c7070dbSScott Long for (i = 0; i < sctx->isc_ntxqsets; i++, txq++) 2383ab2e3f79SStephen Hurd callout_reset_on(&txq->ift_timer, hz/2, iflib_timer, txq, 2384ab2e3f79SStephen Hurd txq->ift_timer.c_cpu); 23854c7070dbSScott Long } 23864c7070dbSScott Long 23874c7070dbSScott Long static int 23884c7070dbSScott Long iflib_media_change(if_t ifp) 23894c7070dbSScott Long { 23904c7070dbSScott Long if_ctx_t ctx = if_getsoftc(ifp); 23914c7070dbSScott Long int err; 23924c7070dbSScott Long 23934c7070dbSScott Long CTX_LOCK(ctx); 23944c7070dbSScott Long if ((err = IFDI_MEDIA_CHANGE(ctx)) == 0) 23954c7070dbSScott Long iflib_init_locked(ctx); 23964c7070dbSScott Long CTX_UNLOCK(ctx); 23974c7070dbSScott Long return (err); 23984c7070dbSScott Long } 23994c7070dbSScott Long 24004c7070dbSScott Long static void 24014c7070dbSScott Long iflib_media_status(if_t ifp, struct ifmediareq *ifmr) 24024c7070dbSScott Long { 24034c7070dbSScott Long if_ctx_t ctx = if_getsoftc(ifp); 24044c7070dbSScott Long 24054c7070dbSScott Long CTX_LOCK(ctx); 2406ab2e3f79SStephen Hurd IFDI_UPDATE_ADMIN_STATUS(ctx); 24074c7070dbSScott Long IFDI_MEDIA_STATUS(ctx, ifmr); 24084c7070dbSScott Long CTX_UNLOCK(ctx); 24094c7070dbSScott Long } 24104c7070dbSScott Long 241109f6ff4fSMatt Macy void 24124c7070dbSScott Long iflib_stop(if_ctx_t ctx) 24134c7070dbSScott Long { 24144c7070dbSScott Long iflib_txq_t txq = ctx->ifc_txqs; 24154c7070dbSScott Long iflib_rxq_t rxq = ctx->ifc_rxqs; 24164c7070dbSScott Long if_softc_ctx_t scctx = &ctx->ifc_softc_ctx; 24174d261ce2SStephen Hurd if_shared_ctx_t sctx = ctx->ifc_sctx; 24184c7070dbSScott Long iflib_dma_info_t di; 24194c7070dbSScott Long iflib_fl_t fl; 24204c7070dbSScott Long int i, j; 24214c7070dbSScott Long 24224c7070dbSScott Long /* Tell the stack that the interface is no longer active */ 24234c7070dbSScott Long if_setdrvflagbits(ctx->ifc_ifp, IFF_DRV_OACTIVE, IFF_DRV_RUNNING); 24244c7070dbSScott Long 24254c7070dbSScott Long IFDI_INTR_DISABLE(ctx); 2426ab2e3f79SStephen Hurd DELAY(1000); 2427da69b8f9SSean Bruno IFDI_STOP(ctx); 2428ab2e3f79SStephen Hurd DELAY(1000); 24294c7070dbSScott Long 2430da69b8f9SSean Bruno iflib_debug_reset(); 24314c7070dbSScott Long /* Wait for current tx queue users to exit to disarm watchdog timer. */ 24324c7070dbSScott Long for (i = 0; i < scctx->isc_ntxqsets; i++, txq++) { 24334c7070dbSScott Long /* make sure all transmitters have completed before proceeding XXX */ 24344c7070dbSScott Long 2435226fb85dSStephen Hurd CALLOUT_LOCK(txq); 2436226fb85dSStephen Hurd callout_stop(&txq->ift_timer); 2437226fb85dSStephen Hurd CALLOUT_UNLOCK(txq); 2438226fb85dSStephen Hurd 24394c7070dbSScott Long /* clean any enqueued buffers */ 2440da69b8f9SSean Bruno iflib_ifmp_purge(txq); 24414c7070dbSScott Long /* Free any existing tx buffers. */ 244223ac9029SStephen Hurd for (j = 0; j < txq->ift_size; j++) { 24434c7070dbSScott Long iflib_txsd_free(ctx, txq, j); 24444c7070dbSScott Long } 2445ab2e3f79SStephen Hurd txq->ift_processed = txq->ift_cleaned = txq->ift_cidx_processed = 0; 2446ab2e3f79SStephen Hurd txq->ift_in_use = txq->ift_gen = txq->ift_cidx = txq->ift_pidx = txq->ift_no_desc_avail = 0; 24474c7070dbSScott Long txq->ift_closed = txq->ift_mbuf_defrag = txq->ift_mbuf_defrag_failed = 0; 24484c7070dbSScott Long txq->ift_no_tx_dma_setup = txq->ift_txd_encap_efbig = txq->ift_map_failed = 0; 2449ab2e3f79SStephen Hurd txq->ift_pullups = 0; 245095246abbSSean Bruno ifmp_ring_reset_stats(txq->ift_br); 24514d261ce2SStephen Hurd for (j = 0, di = txq->ift_ifdi; j < sctx->isc_ntxqs; j++, di++) 24524c7070dbSScott Long bzero((void *)di->idi_vaddr, di->idi_size); 24534c7070dbSScott Long } 24544c7070dbSScott Long for (i = 0; i < scctx->isc_nrxqsets; i++, rxq++) { 24554c7070dbSScott Long /* make sure all transmitters have completed before proceeding XXX */ 24564c7070dbSScott Long 24570efb1a46SStephen Hurd rxq->ifr_cq_gen = rxq->ifr_cq_cidx = rxq->ifr_cq_pidx = 0; 24584d261ce2SStephen Hurd for (j = 0, di = rxq->ifr_ifdi; j < sctx->isc_nrxqs; j++, di++) 24594c7070dbSScott Long bzero((void *)di->idi_vaddr, di->idi_size); 24604c7070dbSScott Long /* also resets the free lists pidx/cidx */ 24614c7070dbSScott Long for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++) 24624c7070dbSScott Long iflib_fl_bufs_free(fl); 24634c7070dbSScott Long } 24644c7070dbSScott Long } 24654c7070dbSScott Long 246695246abbSSean Bruno static inline caddr_t 246795246abbSSean Bruno calc_next_rxd(iflib_fl_t fl, int cidx) 246895246abbSSean Bruno { 246995246abbSSean Bruno qidx_t size; 247095246abbSSean Bruno int nrxd; 247195246abbSSean Bruno caddr_t start, end, cur, next; 247295246abbSSean Bruno 247395246abbSSean Bruno nrxd = fl->ifl_size; 247495246abbSSean Bruno size = fl->ifl_rxd_size; 247595246abbSSean Bruno start = fl->ifl_ifdi->idi_vaddr; 247695246abbSSean Bruno 247795246abbSSean Bruno if (__predict_false(size == 0)) 247895246abbSSean Bruno return (start); 247995246abbSSean Bruno cur = start + size*cidx; 248095246abbSSean Bruno end = start + size*nrxd; 248195246abbSSean Bruno next = CACHE_PTR_NEXT(cur); 248295246abbSSean Bruno return (next < end ? next : start); 248395246abbSSean Bruno } 248495246abbSSean Bruno 2485e035717eSSean Bruno static inline void 2486e035717eSSean Bruno prefetch_pkts(iflib_fl_t fl, int cidx) 2487e035717eSSean Bruno { 2488e035717eSSean Bruno int nextptr; 2489e035717eSSean Bruno int nrxd = fl->ifl_size; 249095246abbSSean Bruno caddr_t next_rxd; 249195246abbSSean Bruno 2492e035717eSSean Bruno 2493e035717eSSean Bruno nextptr = (cidx + CACHE_PTR_INCREMENT) & (nrxd-1); 2494e035717eSSean Bruno prefetch(&fl->ifl_sds.ifsd_m[nextptr]); 2495e035717eSSean Bruno prefetch(&fl->ifl_sds.ifsd_cl[nextptr]); 249695246abbSSean Bruno next_rxd = calc_next_rxd(fl, cidx); 249795246abbSSean Bruno prefetch(next_rxd); 2498e035717eSSean Bruno prefetch(fl->ifl_sds.ifsd_m[(cidx + 1) & (nrxd-1)]); 2499e035717eSSean Bruno prefetch(fl->ifl_sds.ifsd_m[(cidx + 2) & (nrxd-1)]); 2500e035717eSSean Bruno prefetch(fl->ifl_sds.ifsd_m[(cidx + 3) & (nrxd-1)]); 2501e035717eSSean Bruno prefetch(fl->ifl_sds.ifsd_m[(cidx + 4) & (nrxd-1)]); 2502e035717eSSean Bruno prefetch(fl->ifl_sds.ifsd_cl[(cidx + 1) & (nrxd-1)]); 2503e035717eSSean Bruno prefetch(fl->ifl_sds.ifsd_cl[(cidx + 2) & (nrxd-1)]); 2504e035717eSSean Bruno prefetch(fl->ifl_sds.ifsd_cl[(cidx + 3) & (nrxd-1)]); 2505e035717eSSean Bruno prefetch(fl->ifl_sds.ifsd_cl[(cidx + 4) & (nrxd-1)]); 2506e035717eSSean Bruno } 2507e035717eSSean Bruno 2508e035717eSSean Bruno static void 250995246abbSSean Bruno rxd_frag_to_sd(iflib_rxq_t rxq, if_rxd_frag_t irf, int unload, if_rxsd_t sd) 25104c7070dbSScott Long { 25114c7070dbSScott Long int flid, cidx; 2512e035717eSSean Bruno bus_dmamap_t map; 25134c7070dbSScott Long iflib_fl_t fl; 2514e035717eSSean Bruno int next; 25154c7070dbSScott Long 251695246abbSSean Bruno map = NULL; 25174c7070dbSScott Long flid = irf->irf_flid; 25184c7070dbSScott Long cidx = irf->irf_idx; 25194c7070dbSScott Long fl = &rxq->ifr_fl[flid]; 252095246abbSSean Bruno sd->ifsd_fl = fl; 252195246abbSSean Bruno sd->ifsd_cidx = cidx; 252295246abbSSean Bruno sd->ifsd_m = &fl->ifl_sds.ifsd_m[cidx]; 252395246abbSSean Bruno sd->ifsd_cl = &fl->ifl_sds.ifsd_cl[cidx]; 25244c7070dbSScott Long fl->ifl_credits--; 25254c7070dbSScott Long #if MEMORY_LOGGING 25264c7070dbSScott Long fl->ifl_m_dequeued++; 25274c7070dbSScott Long #endif 252895246abbSSean Bruno if (rxq->ifr_ctx->ifc_flags & IFC_PREFETCH) 2529e035717eSSean Bruno prefetch_pkts(fl, cidx); 2530e035717eSSean Bruno next = (cidx + CACHE_PTR_INCREMENT) & (fl->ifl_size-1); 2531e035717eSSean Bruno prefetch(&fl->ifl_sds.ifsd_map[next]); 2532e035717eSSean Bruno map = fl->ifl_sds.ifsd_map[cidx]; 2533e035717eSSean Bruno next = (cidx + CACHE_LINE_SIZE) & (fl->ifl_size-1); 25344c7070dbSScott Long 25354c7070dbSScott Long /* not valid assert if bxe really does SGE from non-contiguous elements */ 25364c7070dbSScott Long MPASS(fl->ifl_cidx == cidx); 2537bfce461eSMarius Strobl bus_dmamap_sync(fl->ifl_buf_tag, map, BUS_DMASYNC_POSTREAD); 25384c7070dbSScott Long if (unload) 2539bfce461eSMarius Strobl bus_dmamap_unload(fl->ifl_buf_tag, map); 254095246abbSSean Bruno fl->ifl_cidx = (fl->ifl_cidx + 1) & (fl->ifl_size-1); 254195246abbSSean Bruno if (__predict_false(fl->ifl_cidx == 0)) 25424c7070dbSScott Long fl->ifl_gen = 0; 254387890dbaSSean Bruno bit_clear(fl->ifl_rx_bitmap, cidx); 25444c7070dbSScott Long } 25454c7070dbSScott Long 25464c7070dbSScott Long static struct mbuf * 254795246abbSSean Bruno assemble_segments(iflib_rxq_t rxq, if_rxd_info_t ri, if_rxsd_t sd) 25484c7070dbSScott Long { 254995246abbSSean Bruno int i, padlen , flags; 255095246abbSSean Bruno struct mbuf *m, *mh, *mt; 255195246abbSSean Bruno caddr_t cl; 25524c7070dbSScott Long 25534c7070dbSScott Long i = 0; 255423ac9029SStephen Hurd mh = NULL; 25554c7070dbSScott Long do { 255695246abbSSean Bruno rxd_frag_to_sd(rxq, &ri->iri_frags[i], TRUE, sd); 25574c7070dbSScott Long 255895246abbSSean Bruno MPASS(*sd->ifsd_cl != NULL); 255995246abbSSean Bruno MPASS(*sd->ifsd_m != NULL); 256023ac9029SStephen Hurd 256123ac9029SStephen Hurd /* Don't include zero-length frags */ 256223ac9029SStephen Hurd if (ri->iri_frags[i].irf_len == 0) { 256323ac9029SStephen Hurd /* XXX we can save the cluster here, but not the mbuf */ 256495246abbSSean Bruno m_init(*sd->ifsd_m, M_NOWAIT, MT_DATA, 0); 256595246abbSSean Bruno m_free(*sd->ifsd_m); 256695246abbSSean Bruno *sd->ifsd_m = NULL; 256723ac9029SStephen Hurd continue; 256823ac9029SStephen Hurd } 256995246abbSSean Bruno m = *sd->ifsd_m; 257095246abbSSean Bruno *sd->ifsd_m = NULL; 257123ac9029SStephen Hurd if (mh == NULL) { 25724c7070dbSScott Long flags = M_PKTHDR|M_EXT; 25734c7070dbSScott Long mh = mt = m; 25744c7070dbSScott Long padlen = ri->iri_pad; 25754c7070dbSScott Long } else { 25764c7070dbSScott Long flags = M_EXT; 25774c7070dbSScott Long mt->m_next = m; 25784c7070dbSScott Long mt = m; 25794c7070dbSScott Long /* assuming padding is only on the first fragment */ 25804c7070dbSScott Long padlen = 0; 25814c7070dbSScott Long } 258295246abbSSean Bruno cl = *sd->ifsd_cl; 258395246abbSSean Bruno *sd->ifsd_cl = NULL; 25844c7070dbSScott Long 25854c7070dbSScott Long /* Can these two be made one ? */ 25864c7070dbSScott Long m_init(m, M_NOWAIT, MT_DATA, flags); 258795246abbSSean Bruno m_cljset(m, cl, sd->ifsd_fl->ifl_cltype); 25884c7070dbSScott Long /* 25894c7070dbSScott Long * These must follow m_init and m_cljset 25904c7070dbSScott Long */ 25914c7070dbSScott Long m->m_data += padlen; 25924c7070dbSScott Long ri->iri_len -= padlen; 259323ac9029SStephen Hurd m->m_len = ri->iri_frags[i].irf_len; 25944c7070dbSScott Long } while (++i < ri->iri_nfrags); 25954c7070dbSScott Long 25964c7070dbSScott Long return (mh); 25974c7070dbSScott Long } 25984c7070dbSScott Long 25994c7070dbSScott Long /* 26004c7070dbSScott Long * Process one software descriptor 26014c7070dbSScott Long */ 26024c7070dbSScott Long static struct mbuf * 26034c7070dbSScott Long iflib_rxd_pkt_get(iflib_rxq_t rxq, if_rxd_info_t ri) 26044c7070dbSScott Long { 260595246abbSSean Bruno struct if_rxsd sd; 26064c7070dbSScott Long struct mbuf *m; 26074c7070dbSScott Long 26084c7070dbSScott Long /* should I merge this back in now that the two paths are basically duplicated? */ 260923ac9029SStephen Hurd if (ri->iri_nfrags == 1 && 261018628b74SMark Johnston ri->iri_frags[0].irf_len <= MIN(IFLIB_RX_COPY_THRESH, MHLEN)) { 261195246abbSSean Bruno rxd_frag_to_sd(rxq, &ri->iri_frags[0], FALSE, &sd); 261295246abbSSean Bruno m = *sd.ifsd_m; 261395246abbSSean Bruno *sd.ifsd_m = NULL; 26144c7070dbSScott Long m_init(m, M_NOWAIT, MT_DATA, M_PKTHDR); 261595246abbSSean Bruno #ifndef __NO_STRICT_ALIGNMENT 261695246abbSSean Bruno if (!IP_ALIGNED(m)) 261795246abbSSean Bruno m->m_data += 2; 261895246abbSSean Bruno #endif 261995246abbSSean Bruno memcpy(m->m_data, *sd.ifsd_cl, ri->iri_len); 262023ac9029SStephen Hurd m->m_len = ri->iri_frags[0].irf_len; 26214c7070dbSScott Long } else { 262295246abbSSean Bruno m = assemble_segments(rxq, ri, &sd); 26234c7070dbSScott Long } 26244c7070dbSScott Long m->m_pkthdr.len = ri->iri_len; 26254c7070dbSScott Long m->m_pkthdr.rcvif = ri->iri_ifp; 26264c7070dbSScott Long m->m_flags |= ri->iri_flags; 26274c7070dbSScott Long m->m_pkthdr.ether_vtag = ri->iri_vtag; 26284c7070dbSScott Long m->m_pkthdr.flowid = ri->iri_flowid; 26294c7070dbSScott Long M_HASHTYPE_SET(m, ri->iri_rsstype); 26304c7070dbSScott Long m->m_pkthdr.csum_flags = ri->iri_csum_flags; 26314c7070dbSScott Long m->m_pkthdr.csum_data = ri->iri_csum_data; 26324c7070dbSScott Long return (m); 26334c7070dbSScott Long } 26344c7070dbSScott Long 263535e4e998SStephen Hurd #if defined(INET6) || defined(INET) 2636fe1bcadaSStephen Hurd static void 2637fe1bcadaSStephen Hurd iflib_get_ip_forwarding(struct lro_ctrl *lc, bool *v4, bool *v6) 2638fe1bcadaSStephen Hurd { 2639fe1bcadaSStephen Hurd CURVNET_SET(lc->ifp->if_vnet); 2640fe1bcadaSStephen Hurd #if defined(INET6) 2641fe1bcadaSStephen Hurd *v6 = VNET(ip6_forwarding); 2642fe1bcadaSStephen Hurd #endif 2643fe1bcadaSStephen Hurd #if defined(INET) 2644fe1bcadaSStephen Hurd *v4 = VNET(ipforwarding); 2645fe1bcadaSStephen Hurd #endif 2646fe1bcadaSStephen Hurd CURVNET_RESTORE(); 2647fe1bcadaSStephen Hurd } 2648fe1bcadaSStephen Hurd 264935e4e998SStephen Hurd /* 265035e4e998SStephen Hurd * Returns true if it's possible this packet could be LROed. 265135e4e998SStephen Hurd * if it returns false, it is guaranteed that tcp_lro_rx() 265235e4e998SStephen Hurd * would not return zero. 265335e4e998SStephen Hurd */ 265435e4e998SStephen Hurd static bool 2655fe1bcadaSStephen Hurd iflib_check_lro_possible(struct mbuf *m, bool v4_forwarding, bool v6_forwarding) 265635e4e998SStephen Hurd { 265735e4e998SStephen Hurd struct ether_header *eh; 265835e4e998SStephen Hurd uint16_t eh_type; 265935e4e998SStephen Hurd 266035e4e998SStephen Hurd eh = mtod(m, struct ether_header *); 266135e4e998SStephen Hurd eh_type = ntohs(eh->ether_type); 266235e4e998SStephen Hurd switch (eh_type) { 2663abec4724SSean Bruno #if defined(INET6) 266435e4e998SStephen Hurd case ETHERTYPE_IPV6: 2665fe1bcadaSStephen Hurd return !v6_forwarding; 2666abec4724SSean Bruno #endif 2667abec4724SSean Bruno #if defined (INET) 266835e4e998SStephen Hurd case ETHERTYPE_IP: 2669fe1bcadaSStephen Hurd return !v4_forwarding; 2670abec4724SSean Bruno #endif 267135e4e998SStephen Hurd } 267235e4e998SStephen Hurd 267335e4e998SStephen Hurd return false; 267435e4e998SStephen Hurd } 2675fe1bcadaSStephen Hurd #else 2676fe1bcadaSStephen Hurd static void 2677fe1bcadaSStephen Hurd iflib_get_ip_forwarding(struct lro_ctrl *lc __unused, bool *v4 __unused, bool *v6 __unused) 2678fe1bcadaSStephen Hurd { 2679fe1bcadaSStephen Hurd } 268035e4e998SStephen Hurd #endif 268135e4e998SStephen Hurd 26824c7070dbSScott Long static bool 268395246abbSSean Bruno iflib_rxeof(iflib_rxq_t rxq, qidx_t budget) 26844c7070dbSScott Long { 26854c7070dbSScott Long if_ctx_t ctx = rxq->ifr_ctx; 26864c7070dbSScott Long if_shared_ctx_t sctx = ctx->ifc_sctx; 268723ac9029SStephen Hurd if_softc_ctx_t scctx = &ctx->ifc_softc_ctx; 26884c7070dbSScott Long int avail, i; 268995246abbSSean Bruno qidx_t *cidxp; 26904c7070dbSScott Long struct if_rxd_info ri; 26914c7070dbSScott Long int err, budget_left, rx_bytes, rx_pkts; 26924c7070dbSScott Long iflib_fl_t fl; 26934c7070dbSScott Long struct ifnet *ifp; 26944c7070dbSScott Long int lro_enabled; 2695f6cb0deaSMatt Macy bool v4_forwarding, v6_forwarding, lro_possible; 269695246abbSSean Bruno 26974c7070dbSScott Long /* 26984c7070dbSScott Long * XXX early demux data packets so that if_input processing only handles 26994c7070dbSScott Long * acks in interrupt context 27004c7070dbSScott Long */ 270120f63282SStephen Hurd struct mbuf *m, *mh, *mt, *mf; 27024c7070dbSScott Long 2703f6cb0deaSMatt Macy lro_possible = v4_forwarding = v6_forwarding = false; 270495246abbSSean Bruno ifp = ctx->ifc_ifp; 27054c7070dbSScott Long mh = mt = NULL; 27064c7070dbSScott Long MPASS(budget > 0); 27074c7070dbSScott Long rx_pkts = rx_bytes = 0; 270823ac9029SStephen Hurd if (sctx->isc_flags & IFLIB_HAS_RXCQ) 27094c7070dbSScott Long cidxp = &rxq->ifr_cq_cidx; 27104c7070dbSScott Long else 27114c7070dbSScott Long cidxp = &rxq->ifr_fl[0].ifl_cidx; 271223ac9029SStephen Hurd if ((avail = iflib_rxd_avail(ctx, rxq, *cidxp, budget)) == 0) { 27134c7070dbSScott Long for (i = 0, fl = &rxq->ifr_fl[0]; i < sctx->isc_nfl; i++, fl++) 27144c7070dbSScott Long __iflib_fl_refill_lt(ctx, fl, budget + 8); 27154c7070dbSScott Long DBG_COUNTER_INC(rx_unavail); 27164c7070dbSScott Long return (false); 27174c7070dbSScott Long } 27184c7070dbSScott Long 27198b8d9093SMarius Strobl for (budget_left = budget; budget_left > 0 && avail > 0;) { 27204c7070dbSScott Long if (__predict_false(!CTX_ACTIVE(ctx))) { 27214c7070dbSScott Long DBG_COUNTER_INC(rx_ctx_inactive); 27224c7070dbSScott Long break; 27234c7070dbSScott Long } 27244c7070dbSScott Long /* 27254c7070dbSScott Long * Reset client set fields to their default values 27264c7070dbSScott Long */ 272795246abbSSean Bruno rxd_info_zero(&ri); 27284c7070dbSScott Long ri.iri_qsidx = rxq->ifr_id; 27294c7070dbSScott Long ri.iri_cidx = *cidxp; 273095246abbSSean Bruno ri.iri_ifp = ifp; 27314c7070dbSScott Long ri.iri_frags = rxq->ifr_frags; 27324c7070dbSScott Long err = ctx->isc_rxd_pkt_get(ctx->ifc_softc, &ri); 27334c7070dbSScott Long 273495246abbSSean Bruno if (err) 273595246abbSSean Bruno goto err; 273623ac9029SStephen Hurd if (sctx->isc_flags & IFLIB_HAS_RXCQ) { 273723ac9029SStephen Hurd *cidxp = ri.iri_cidx; 273823ac9029SStephen Hurd /* Update our consumer index */ 273995246abbSSean Bruno /* XXX NB: shurd - check if this is still safe */ 274023ac9029SStephen Hurd while (rxq->ifr_cq_cidx >= scctx->isc_nrxd[0]) { 274123ac9029SStephen Hurd rxq->ifr_cq_cidx -= scctx->isc_nrxd[0]; 27424c7070dbSScott Long rxq->ifr_cq_gen = 0; 27434c7070dbSScott Long } 27444c7070dbSScott Long /* was this only a completion queue message? */ 27454c7070dbSScott Long if (__predict_false(ri.iri_nfrags == 0)) 27464c7070dbSScott Long continue; 27474c7070dbSScott Long } 27484c7070dbSScott Long MPASS(ri.iri_nfrags != 0); 27494c7070dbSScott Long MPASS(ri.iri_len != 0); 27504c7070dbSScott Long 27514c7070dbSScott Long /* will advance the cidx on the corresponding free lists */ 27524c7070dbSScott Long m = iflib_rxd_pkt_get(rxq, &ri); 27538b8d9093SMarius Strobl avail--; 27548b8d9093SMarius Strobl budget_left--; 27554c7070dbSScott Long if (avail == 0 && budget_left) 275623ac9029SStephen Hurd avail = iflib_rxd_avail(ctx, rxq, *cidxp, budget_left); 27574c7070dbSScott Long 27584c7070dbSScott Long if (__predict_false(m == NULL)) { 27594c7070dbSScott Long DBG_COUNTER_INC(rx_mbuf_null); 27604c7070dbSScott Long continue; 27614c7070dbSScott Long } 27624c7070dbSScott Long /* imm_pkt: -- cxgb */ 27634c7070dbSScott Long if (mh == NULL) 27644c7070dbSScott Long mh = mt = m; 27654c7070dbSScott Long else { 27664c7070dbSScott Long mt->m_nextpkt = m; 27674c7070dbSScott Long mt = m; 27684c7070dbSScott Long } 27694c7070dbSScott Long } 27704c7070dbSScott Long /* make sure that we can refill faster than drain */ 27714c7070dbSScott Long for (i = 0, fl = &rxq->ifr_fl[0]; i < sctx->isc_nfl; i++, fl++) 2772ab2e3f79SStephen Hurd __iflib_fl_refill_lt(ctx, fl, budget + 8); 27734c7070dbSScott Long 27744c7070dbSScott Long lro_enabled = (if_getcapenable(ifp) & IFCAP_LRO); 2775fe1bcadaSStephen Hurd if (lro_enabled) 2776fe1bcadaSStephen Hurd iflib_get_ip_forwarding(&rxq->ifr_lc, &v4_forwarding, &v6_forwarding); 277720f63282SStephen Hurd mt = mf = NULL; 27784c7070dbSScott Long while (mh != NULL) { 27794c7070dbSScott Long m = mh; 27804c7070dbSScott Long mh = mh->m_nextpkt; 27814c7070dbSScott Long m->m_nextpkt = NULL; 278295246abbSSean Bruno #ifndef __NO_STRICT_ALIGNMENT 278395246abbSSean Bruno if (!IP_ALIGNED(m) && (m = iflib_fixup_rx(m)) == NULL) 278495246abbSSean Bruno continue; 278595246abbSSean Bruno #endif 27864c7070dbSScott Long rx_bytes += m->m_pkthdr.len; 27874c7070dbSScott Long rx_pkts++; 2788aaeb188aSBjoern A. Zeeb #if defined(INET6) || defined(INET) 278935e4e998SStephen Hurd if (lro_enabled) { 279035e4e998SStephen Hurd if (!lro_possible) { 2791fe1bcadaSStephen Hurd lro_possible = iflib_check_lro_possible(m, v4_forwarding, v6_forwarding); 279235e4e998SStephen Hurd if (lro_possible && mf != NULL) { 279335e4e998SStephen Hurd ifp->if_input(ifp, mf); 279435e4e998SStephen Hurd DBG_COUNTER_INC(rx_if_input); 279535e4e998SStephen Hurd mt = mf = NULL; 279635e4e998SStephen Hurd } 279735e4e998SStephen Hurd } 279825ac1dd5SStephen Hurd if ((m->m_pkthdr.csum_flags & (CSUM_L4_CALC|CSUM_L4_VALID)) == 279925ac1dd5SStephen Hurd (CSUM_L4_CALC|CSUM_L4_VALID)) { 280035e4e998SStephen Hurd if (lro_possible && tcp_lro_rx(&rxq->ifr_lc, m, 0) == 0) 28014c7070dbSScott Long continue; 280220f63282SStephen Hurd } 280325ac1dd5SStephen Hurd } 2804aaeb188aSBjoern A. Zeeb #endif 280535e4e998SStephen Hurd if (lro_possible) { 280635e4e998SStephen Hurd ifp->if_input(ifp, m); 280735e4e998SStephen Hurd DBG_COUNTER_INC(rx_if_input); 280835e4e998SStephen Hurd continue; 280935e4e998SStephen Hurd } 281035e4e998SStephen Hurd 281135e4e998SStephen Hurd if (mf == NULL) 281235e4e998SStephen Hurd mf = m; 281320f63282SStephen Hurd if (mt != NULL) 281420f63282SStephen Hurd mt->m_nextpkt = m; 281520f63282SStephen Hurd mt = m; 281620f63282SStephen Hurd } 281720f63282SStephen Hurd if (mf != NULL) { 281820f63282SStephen Hurd ifp->if_input(ifp, mf); 28194c7070dbSScott Long DBG_COUNTER_INC(rx_if_input); 28204c7070dbSScott Long } 282123ac9029SStephen Hurd 28224c7070dbSScott Long if_inc_counter(ifp, IFCOUNTER_IBYTES, rx_bytes); 28234c7070dbSScott Long if_inc_counter(ifp, IFCOUNTER_IPACKETS, rx_pkts); 28244c7070dbSScott Long 28254c7070dbSScott Long /* 28264c7070dbSScott Long * Flush any outstanding LRO work 28274c7070dbSScott Long */ 2828aaeb188aSBjoern A. Zeeb #if defined(INET6) || defined(INET) 282923ac9029SStephen Hurd tcp_lro_flush_all(&rxq->ifr_lc); 2830aaeb188aSBjoern A. Zeeb #endif 2831ab2e3f79SStephen Hurd if (avail) 2832ab2e3f79SStephen Hurd return true; 2833ab2e3f79SStephen Hurd return (iflib_rxd_avail(ctx, rxq, *cidxp, 1)); 283495246abbSSean Bruno err: 28357b610b60SSean Bruno STATE_LOCK(ctx); 2836ab2e3f79SStephen Hurd ctx->ifc_flags |= IFC_DO_RESET; 2837940f62d6SEric Joyner iflib_admin_intr_deferred(ctx); 283846fa0c25SEric Joyner STATE_UNLOCK(ctx); 283995246abbSSean Bruno return (false); 284095246abbSSean Bruno } 284195246abbSSean Bruno 284295246abbSSean Bruno #define TXD_NOTIFY_COUNT(txq) (((txq)->ift_size / (txq)->ift_update_freq)-1) 284395246abbSSean Bruno static inline qidx_t 284495246abbSSean Bruno txq_max_db_deferred(iflib_txq_t txq, qidx_t in_use) 284595246abbSSean Bruno { 284695246abbSSean Bruno qidx_t notify_count = TXD_NOTIFY_COUNT(txq); 284795246abbSSean Bruno qidx_t minthresh = txq->ift_size / 8; 284895246abbSSean Bruno if (in_use > 4*minthresh) 284995246abbSSean Bruno return (notify_count); 285095246abbSSean Bruno if (in_use > 2*minthresh) 285195246abbSSean Bruno return (notify_count >> 1); 285295246abbSSean Bruno if (in_use > minthresh) 285395246abbSSean Bruno return (notify_count >> 3); 285495246abbSSean Bruno return (0); 285595246abbSSean Bruno } 285695246abbSSean Bruno 285795246abbSSean Bruno static inline qidx_t 285895246abbSSean Bruno txq_max_rs_deferred(iflib_txq_t txq) 285995246abbSSean Bruno { 286095246abbSSean Bruno qidx_t notify_count = TXD_NOTIFY_COUNT(txq); 286195246abbSSean Bruno qidx_t minthresh = txq->ift_size / 8; 286295246abbSSean Bruno if (txq->ift_in_use > 4*minthresh) 286395246abbSSean Bruno return (notify_count); 286495246abbSSean Bruno if (txq->ift_in_use > 2*minthresh) 286595246abbSSean Bruno return (notify_count >> 1); 286695246abbSSean Bruno if (txq->ift_in_use > minthresh) 286795246abbSSean Bruno return (notify_count >> 2); 28682b2fc973SSean Bruno return (2); 28694c7070dbSScott Long } 28704c7070dbSScott Long 28714c7070dbSScott Long #define M_CSUM_FLAGS(m) ((m)->m_pkthdr.csum_flags) 28724c7070dbSScott Long #define M_HAS_VLANTAG(m) (m->m_flags & M_VLANTAG) 287395246abbSSean Bruno 287495246abbSSean Bruno #define TXQ_MAX_DB_DEFERRED(txq, in_use) txq_max_db_deferred((txq), (in_use)) 287595246abbSSean Bruno #define TXQ_MAX_RS_DEFERRED(txq) txq_max_rs_deferred(txq) 287623ac9029SStephen Hurd #define TXQ_MAX_DB_CONSUMED(size) (size >> 4) 28774c7070dbSScott Long 287895246abbSSean Bruno /* forward compatibility for cxgb */ 287995246abbSSean Bruno #define FIRST_QSET(ctx) 0 288095246abbSSean Bruno #define NTXQSETS(ctx) ((ctx)->ifc_softc_ctx.isc_ntxqsets) 288195246abbSSean Bruno #define NRXQSETS(ctx) ((ctx)->ifc_softc_ctx.isc_nrxqsets) 288295246abbSSean Bruno #define QIDX(ctx, m) ((((m)->m_pkthdr.flowid & ctx->ifc_softc_ctx.isc_rss_table_mask) % NTXQSETS(ctx)) + FIRST_QSET(ctx)) 288395246abbSSean Bruno #define DESC_RECLAIMABLE(q) ((int)((q)->ift_processed - (q)->ift_cleaned - (q)->ift_ctx->ifc_softc_ctx.isc_tx_nsegments)) 288495246abbSSean Bruno 288595246abbSSean Bruno /* XXX we should be setting this to something other than zero */ 288695246abbSSean Bruno #define RECLAIM_THRESH(ctx) ((ctx)->ifc_sctx->isc_tx_reclaim_thresh) 28877474544bSMarius Strobl #define MAX_TX_DESC(ctx) max((ctx)->ifc_softc_ctx.isc_tx_tso_segments_max, \ 28887474544bSMarius Strobl (ctx)->ifc_softc_ctx.isc_tx_nsegments) 288995246abbSSean Bruno 289095246abbSSean Bruno static inline bool 289195246abbSSean Bruno iflib_txd_db_check(if_ctx_t ctx, iflib_txq_t txq, int ring, qidx_t in_use) 28924c7070dbSScott Long { 289395246abbSSean Bruno qidx_t dbval, max; 289495246abbSSean Bruno bool rang; 28954c7070dbSScott Long 289695246abbSSean Bruno rang = false; 289795246abbSSean Bruno max = TXQ_MAX_DB_DEFERRED(txq, in_use); 289895246abbSSean Bruno if (ring || txq->ift_db_pending >= max) { 28994c7070dbSScott Long dbval = txq->ift_npending ? txq->ift_npending : txq->ift_pidx; 290095dcf343SMarius Strobl bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map, 290195dcf343SMarius Strobl BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 29024c7070dbSScott Long ctx->isc_txd_flush(ctx->ifc_softc, txq->ift_id, dbval); 29034c7070dbSScott Long txq->ift_db_pending = txq->ift_npending = 0; 290495246abbSSean Bruno rang = true; 29054c7070dbSScott Long } 290695246abbSSean Bruno return (rang); 29074c7070dbSScott Long } 29084c7070dbSScott Long 29094c7070dbSScott Long #ifdef PKT_DEBUG 29104c7070dbSScott Long static void 29114c7070dbSScott Long print_pkt(if_pkt_info_t pi) 29124c7070dbSScott Long { 29134c7070dbSScott Long printf("pi len: %d qsidx: %d nsegs: %d ndescs: %d flags: %x pidx: %d\n", 29144c7070dbSScott Long pi->ipi_len, pi->ipi_qsidx, pi->ipi_nsegs, pi->ipi_ndescs, pi->ipi_flags, pi->ipi_pidx); 29154c7070dbSScott Long printf("pi new_pidx: %d csum_flags: %lx tso_segsz: %d mflags: %x vtag: %d\n", 29164c7070dbSScott Long pi->ipi_new_pidx, pi->ipi_csum_flags, pi->ipi_tso_segsz, pi->ipi_mflags, pi->ipi_vtag); 29174c7070dbSScott Long printf("pi etype: %d ehdrlen: %d ip_hlen: %d ipproto: %d\n", 29184c7070dbSScott Long pi->ipi_etype, pi->ipi_ehdrlen, pi->ipi_ip_hlen, pi->ipi_ipproto); 29194c7070dbSScott Long } 29204c7070dbSScott Long #endif 29214c7070dbSScott Long 29224c7070dbSScott Long #define IS_TSO4(pi) ((pi)->ipi_csum_flags & CSUM_IP_TSO) 2923a06424ddSEric Joyner #define IS_TX_OFFLOAD4(pi) ((pi)->ipi_csum_flags & (CSUM_IP_TCP | CSUM_IP_TSO)) 29244c7070dbSScott Long #define IS_TSO6(pi) ((pi)->ipi_csum_flags & CSUM_IP6_TSO) 2925a06424ddSEric Joyner #define IS_TX_OFFLOAD6(pi) ((pi)->ipi_csum_flags & (CSUM_IP6_TCP | CSUM_IP6_TSO)) 29264c7070dbSScott Long 29274c7070dbSScott Long static int 29284c7070dbSScott Long iflib_parse_header(iflib_txq_t txq, if_pkt_info_t pi, struct mbuf **mp) 29294c7070dbSScott Long { 2930ab2e3f79SStephen Hurd if_shared_ctx_t sctx = txq->ift_ctx->ifc_sctx; 29314c7070dbSScott Long struct ether_vlan_header *eh; 2932c9a49a4fSMarius Strobl struct mbuf *m; 29334c7070dbSScott Long 29348b8d9093SMarius Strobl m = *mp; 2935ab2e3f79SStephen Hurd if ((sctx->isc_flags & IFLIB_NEED_SCRATCH) && 2936ab2e3f79SStephen Hurd M_WRITABLE(m) == 0) { 2937ab2e3f79SStephen Hurd if ((m = m_dup(m, M_NOWAIT)) == NULL) { 2938ab2e3f79SStephen Hurd return (ENOMEM); 2939ab2e3f79SStephen Hurd } else { 2940ab2e3f79SStephen Hurd m_freem(*mp); 294164e6fc13SStephen Hurd DBG_COUNTER_INC(tx_frees); 29428b8d9093SMarius Strobl *mp = m; 2943ab2e3f79SStephen Hurd } 2944ab2e3f79SStephen Hurd } 29451248952aSSean Bruno 29464c7070dbSScott Long /* 29474c7070dbSScott Long * Determine where frame payload starts. 29484c7070dbSScott Long * Jump over vlan headers if already present, 29494c7070dbSScott Long * helpful for QinQ too. 29504c7070dbSScott Long */ 29514c7070dbSScott Long if (__predict_false(m->m_len < sizeof(*eh))) { 29524c7070dbSScott Long txq->ift_pullups++; 29534c7070dbSScott Long if (__predict_false((m = m_pullup(m, sizeof(*eh))) == NULL)) 29544c7070dbSScott Long return (ENOMEM); 29554c7070dbSScott Long } 29564c7070dbSScott Long eh = mtod(m, struct ether_vlan_header *); 29574c7070dbSScott Long if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) { 29584c7070dbSScott Long pi->ipi_etype = ntohs(eh->evl_proto); 29594c7070dbSScott Long pi->ipi_ehdrlen = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN; 29604c7070dbSScott Long } else { 29614c7070dbSScott Long pi->ipi_etype = ntohs(eh->evl_encap_proto); 29624c7070dbSScott Long pi->ipi_ehdrlen = ETHER_HDR_LEN; 29634c7070dbSScott Long } 29644c7070dbSScott Long 29654c7070dbSScott Long switch (pi->ipi_etype) { 29664c7070dbSScott Long #ifdef INET 29674c7070dbSScott Long case ETHERTYPE_IP: 29684c7070dbSScott Long { 2969c9a49a4fSMarius Strobl struct mbuf *n; 29704c7070dbSScott Long struct ip *ip = NULL; 29714c7070dbSScott Long struct tcphdr *th = NULL; 29724c7070dbSScott Long int minthlen; 29734c7070dbSScott Long 29744c7070dbSScott Long minthlen = min(m->m_pkthdr.len, pi->ipi_ehdrlen + sizeof(*ip) + sizeof(*th)); 29754c7070dbSScott Long if (__predict_false(m->m_len < minthlen)) { 29764c7070dbSScott Long /* 29774c7070dbSScott Long * if this code bloat is causing too much of a hit 29784c7070dbSScott Long * move it to a separate function and mark it noinline 29794c7070dbSScott Long */ 29804c7070dbSScott Long if (m->m_len == pi->ipi_ehdrlen) { 29814c7070dbSScott Long n = m->m_next; 29824c7070dbSScott Long MPASS(n); 29834c7070dbSScott Long if (n->m_len >= sizeof(*ip)) { 29844c7070dbSScott Long ip = (struct ip *)n->m_data; 29854c7070dbSScott Long if (n->m_len >= (ip->ip_hl << 2) + sizeof(*th)) 29864c7070dbSScott Long th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2)); 29874c7070dbSScott Long } else { 29884c7070dbSScott Long txq->ift_pullups++; 29894c7070dbSScott Long if (__predict_false((m = m_pullup(m, minthlen)) == NULL)) 29904c7070dbSScott Long return (ENOMEM); 29914c7070dbSScott Long ip = (struct ip *)(m->m_data + pi->ipi_ehdrlen); 29924c7070dbSScott Long } 29934c7070dbSScott Long } else { 29944c7070dbSScott Long txq->ift_pullups++; 29954c7070dbSScott Long if (__predict_false((m = m_pullup(m, minthlen)) == NULL)) 29964c7070dbSScott Long return (ENOMEM); 29974c7070dbSScott Long ip = (struct ip *)(m->m_data + pi->ipi_ehdrlen); 29984c7070dbSScott Long if (m->m_len >= (ip->ip_hl << 2) + sizeof(*th)) 29994c7070dbSScott Long th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2)); 30004c7070dbSScott Long } 30014c7070dbSScott Long } else { 30024c7070dbSScott Long ip = (struct ip *)(m->m_data + pi->ipi_ehdrlen); 30034c7070dbSScott Long if (m->m_len >= (ip->ip_hl << 2) + sizeof(*th)) 30044c7070dbSScott Long th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2)); 30054c7070dbSScott Long } 30064c7070dbSScott Long pi->ipi_ip_hlen = ip->ip_hl << 2; 30074c7070dbSScott Long pi->ipi_ipproto = ip->ip_p; 30084c7070dbSScott Long pi->ipi_flags |= IPI_TX_IPV4; 30094c7070dbSScott Long 3010a06424ddSEric Joyner /* TCP checksum offload may require TCP header length */ 3011a06424ddSEric Joyner if (IS_TX_OFFLOAD4(pi)) { 3012a06424ddSEric Joyner if (__predict_true(pi->ipi_ipproto == IPPROTO_TCP)) { 30134c7070dbSScott Long if (__predict_false(th == NULL)) { 30144c7070dbSScott Long txq->ift_pullups++; 30154c7070dbSScott Long if (__predict_false((m = m_pullup(m, (ip->ip_hl << 2) + sizeof(*th))) == NULL)) 30164c7070dbSScott Long return (ENOMEM); 30174c7070dbSScott Long th = (struct tcphdr *)((caddr_t)ip + pi->ipi_ip_hlen); 30184c7070dbSScott Long } 30194c7070dbSScott Long pi->ipi_tcp_hflags = th->th_flags; 30204c7070dbSScott Long pi->ipi_tcp_hlen = th->th_off << 2; 30214c7070dbSScott Long pi->ipi_tcp_seq = th->th_seq; 30224c7070dbSScott Long } 3023a06424ddSEric Joyner if (IS_TSO4(pi)) { 30244c7070dbSScott Long if (__predict_false(ip->ip_p != IPPROTO_TCP)) 30254c7070dbSScott Long return (ENXIO); 30268d4ceb9cSStephen Hurd /* 30278d4ceb9cSStephen Hurd * TSO always requires hardware checksum offload. 30288d4ceb9cSStephen Hurd */ 30298d4ceb9cSStephen Hurd pi->ipi_csum_flags |= (CSUM_IP_TCP | CSUM_IP); 30304c7070dbSScott Long th->th_sum = in_pseudo(ip->ip_src.s_addr, 30314c7070dbSScott Long ip->ip_dst.s_addr, htons(IPPROTO_TCP)); 30324c7070dbSScott Long pi->ipi_tso_segsz = m->m_pkthdr.tso_segsz; 30331248952aSSean Bruno if (sctx->isc_flags & IFLIB_TSO_INIT_IP) { 30341248952aSSean Bruno ip->ip_sum = 0; 30351248952aSSean Bruno ip->ip_len = htons(pi->ipi_ip_hlen + pi->ipi_tcp_hlen + pi->ipi_tso_segsz); 30361248952aSSean Bruno } 30374c7070dbSScott Long } 3038a06424ddSEric Joyner } 30398d4ceb9cSStephen Hurd if ((sctx->isc_flags & IFLIB_NEED_ZERO_CSUM) && (pi->ipi_csum_flags & CSUM_IP)) 30408d4ceb9cSStephen Hurd ip->ip_sum = 0; 30418d4ceb9cSStephen Hurd 30424c7070dbSScott Long break; 30434c7070dbSScott Long } 30444c7070dbSScott Long #endif 30454c7070dbSScott Long #ifdef INET6 30464c7070dbSScott Long case ETHERTYPE_IPV6: 30474c7070dbSScott Long { 30484c7070dbSScott Long struct ip6_hdr *ip6 = (struct ip6_hdr *)(m->m_data + pi->ipi_ehdrlen); 30494c7070dbSScott Long struct tcphdr *th; 30504c7070dbSScott Long pi->ipi_ip_hlen = sizeof(struct ip6_hdr); 30514c7070dbSScott Long 30524c7070dbSScott Long if (__predict_false(m->m_len < pi->ipi_ehdrlen + sizeof(struct ip6_hdr))) { 305364e6fc13SStephen Hurd txq->ift_pullups++; 30544c7070dbSScott Long if (__predict_false((m = m_pullup(m, pi->ipi_ehdrlen + sizeof(struct ip6_hdr))) == NULL)) 30554c7070dbSScott Long return (ENOMEM); 30564c7070dbSScott Long } 30574c7070dbSScott Long th = (struct tcphdr *)((caddr_t)ip6 + pi->ipi_ip_hlen); 30584c7070dbSScott Long 30594c7070dbSScott Long /* XXX-BZ this will go badly in case of ext hdrs. */ 30604c7070dbSScott Long pi->ipi_ipproto = ip6->ip6_nxt; 30614c7070dbSScott Long pi->ipi_flags |= IPI_TX_IPV6; 30624c7070dbSScott Long 3063a06424ddSEric Joyner /* TCP checksum offload may require TCP header length */ 3064a06424ddSEric Joyner if (IS_TX_OFFLOAD6(pi)) { 30654c7070dbSScott Long if (pi->ipi_ipproto == IPPROTO_TCP) { 30664c7070dbSScott Long if (__predict_false(m->m_len < pi->ipi_ehdrlen + sizeof(struct ip6_hdr) + sizeof(struct tcphdr))) { 3067a06424ddSEric Joyner txq->ift_pullups++; 30684c7070dbSScott Long if (__predict_false((m = m_pullup(m, pi->ipi_ehdrlen + sizeof(struct ip6_hdr) + sizeof(struct tcphdr))) == NULL)) 30694c7070dbSScott Long return (ENOMEM); 30704c7070dbSScott Long } 30714c7070dbSScott Long pi->ipi_tcp_hflags = th->th_flags; 30724c7070dbSScott Long pi->ipi_tcp_hlen = th->th_off << 2; 3073a06424ddSEric Joyner pi->ipi_tcp_seq = th->th_seq; 30744c7070dbSScott Long } 3075a06424ddSEric Joyner if (IS_TSO6(pi)) { 30764c7070dbSScott Long if (__predict_false(ip6->ip6_nxt != IPPROTO_TCP)) 30774c7070dbSScott Long return (ENXIO); 30784c7070dbSScott Long /* 30798d4ceb9cSStephen Hurd * TSO always requires hardware checksum offload. 30804c7070dbSScott Long */ 3081a06424ddSEric Joyner pi->ipi_csum_flags |= CSUM_IP6_TCP; 30824c7070dbSScott Long th->th_sum = in6_cksum_pseudo(ip6, 0, IPPROTO_TCP, 0); 30834c7070dbSScott Long pi->ipi_tso_segsz = m->m_pkthdr.tso_segsz; 30844c7070dbSScott Long } 3085a06424ddSEric Joyner } 30864c7070dbSScott Long break; 30874c7070dbSScott Long } 30884c7070dbSScott Long #endif 30894c7070dbSScott Long default: 30904c7070dbSScott Long pi->ipi_csum_flags &= ~CSUM_OFFLOAD; 30914c7070dbSScott Long pi->ipi_ip_hlen = 0; 30924c7070dbSScott Long break; 30934c7070dbSScott Long } 30944c7070dbSScott Long *mp = m; 30951248952aSSean Bruno 30964c7070dbSScott Long return (0); 30974c7070dbSScott Long } 30984c7070dbSScott Long 30994c7070dbSScott Long /* 31004c7070dbSScott Long * If dodgy hardware rejects the scatter gather chain we've handed it 310123ac9029SStephen Hurd * we'll need to remove the mbuf chain from ifsg_m[] before we can add the 310223ac9029SStephen Hurd * m_defrag'd mbufs 31034c7070dbSScott Long */ 31044c7070dbSScott Long static __noinline struct mbuf * 310523ac9029SStephen Hurd iflib_remove_mbuf(iflib_txq_t txq) 31064c7070dbSScott Long { 3107fbec776dSAndrew Gallatin int ntxd, pidx; 3108fbec776dSAndrew Gallatin struct mbuf *m, **ifsd_m; 31094c7070dbSScott Long 31104c7070dbSScott Long ifsd_m = txq->ift_sds.ifsd_m; 311123ac9029SStephen Hurd ntxd = txq->ift_size; 3112fbec776dSAndrew Gallatin pidx = txq->ift_pidx & (ntxd - 1); 3113fbec776dSAndrew Gallatin ifsd_m = txq->ift_sds.ifsd_m; 3114fbec776dSAndrew Gallatin m = ifsd_m[pidx]; 31154c7070dbSScott Long ifsd_m[pidx] = NULL; 3116bfce461eSMarius Strobl bus_dmamap_unload(txq->ift_buf_tag, txq->ift_sds.ifsd_map[pidx]); 31178a04b53dSKonstantin Belousov if (txq->ift_sds.ifsd_tso_map != NULL) 3118bfce461eSMarius Strobl bus_dmamap_unload(txq->ift_tso_buf_tag, 31198a04b53dSKonstantin Belousov txq->ift_sds.ifsd_tso_map[pidx]); 31204c7070dbSScott Long #if MEMORY_LOGGING 31214c7070dbSScott Long txq->ift_dequeued++; 31224c7070dbSScott Long #endif 3123fbec776dSAndrew Gallatin return (m); 31244c7070dbSScott Long } 31254c7070dbSScott Long 312695246abbSSean Bruno static inline caddr_t 312795246abbSSean Bruno calc_next_txd(iflib_txq_t txq, int cidx, uint8_t qid) 312895246abbSSean Bruno { 312995246abbSSean Bruno qidx_t size; 313095246abbSSean Bruno int ntxd; 313195246abbSSean Bruno caddr_t start, end, cur, next; 313295246abbSSean Bruno 313395246abbSSean Bruno ntxd = txq->ift_size; 313495246abbSSean Bruno size = txq->ift_txd_size[qid]; 313595246abbSSean Bruno start = txq->ift_ifdi[qid].idi_vaddr; 313695246abbSSean Bruno 313795246abbSSean Bruno if (__predict_false(size == 0)) 313895246abbSSean Bruno return (start); 313995246abbSSean Bruno cur = start + size*cidx; 314095246abbSSean Bruno end = start + size*ntxd; 314195246abbSSean Bruno next = CACHE_PTR_NEXT(cur); 314295246abbSSean Bruno return (next < end ? next : start); 314395246abbSSean Bruno } 314495246abbSSean Bruno 3145d14c853bSStephen Hurd /* 3146d14c853bSStephen Hurd * Pad an mbuf to ensure a minimum ethernet frame size. 3147d14c853bSStephen Hurd * min_frame_size is the frame size (less CRC) to pad the mbuf to 3148d14c853bSStephen Hurd */ 3149d14c853bSStephen Hurd static __noinline int 3150a15fbbb8SStephen Hurd iflib_ether_pad(device_t dev, struct mbuf **m_head, uint16_t min_frame_size) 3151d14c853bSStephen Hurd { 3152d14c853bSStephen Hurd /* 3153d14c853bSStephen Hurd * 18 is enough bytes to pad an ARP packet to 46 bytes, and 3154d14c853bSStephen Hurd * and ARP message is the smallest common payload I can think of 3155d14c853bSStephen Hurd */ 3156d14c853bSStephen Hurd static char pad[18]; /* just zeros */ 3157d14c853bSStephen Hurd int n; 3158a15fbbb8SStephen Hurd struct mbuf *new_head; 3159d14c853bSStephen Hurd 3160a15fbbb8SStephen Hurd if (!M_WRITABLE(*m_head)) { 3161a15fbbb8SStephen Hurd new_head = m_dup(*m_head, M_NOWAIT); 3162a15fbbb8SStephen Hurd if (new_head == NULL) { 316304993890SStephen Hurd m_freem(*m_head); 3164a15fbbb8SStephen Hurd device_printf(dev, "cannot pad short frame, m_dup() failed"); 316506c47d48SStephen Hurd DBG_COUNTER_INC(encap_pad_mbuf_fail); 316664e6fc13SStephen Hurd DBG_COUNTER_INC(tx_frees); 3167a15fbbb8SStephen Hurd return ENOMEM; 3168a15fbbb8SStephen Hurd } 3169a15fbbb8SStephen Hurd m_freem(*m_head); 3170a15fbbb8SStephen Hurd *m_head = new_head; 3171a15fbbb8SStephen Hurd } 3172a15fbbb8SStephen Hurd 3173a15fbbb8SStephen Hurd for (n = min_frame_size - (*m_head)->m_pkthdr.len; 3174d14c853bSStephen Hurd n > 0; n -= sizeof(pad)) 3175a15fbbb8SStephen Hurd if (!m_append(*m_head, min(n, sizeof(pad)), pad)) 3176d14c853bSStephen Hurd break; 3177d14c853bSStephen Hurd 3178d14c853bSStephen Hurd if (n > 0) { 3179a15fbbb8SStephen Hurd m_freem(*m_head); 3180d14c853bSStephen Hurd device_printf(dev, "cannot pad short frame\n"); 3181d14c853bSStephen Hurd DBG_COUNTER_INC(encap_pad_mbuf_fail); 318264e6fc13SStephen Hurd DBG_COUNTER_INC(tx_frees); 3183d14c853bSStephen Hurd return (ENOBUFS); 3184d14c853bSStephen Hurd } 3185d14c853bSStephen Hurd 3186d14c853bSStephen Hurd return 0; 3187d14c853bSStephen Hurd } 3188d14c853bSStephen Hurd 31894c7070dbSScott Long static int 31904c7070dbSScott Long iflib_encap(iflib_txq_t txq, struct mbuf **m_headp) 31914c7070dbSScott Long { 31924c7070dbSScott Long if_ctx_t ctx; 31934c7070dbSScott Long if_shared_ctx_t sctx; 31944c7070dbSScott Long if_softc_ctx_t scctx; 3195bfce461eSMarius Strobl bus_dma_tag_t buf_tag; 31964c7070dbSScott Long bus_dma_segment_t *segs; 3197fbec776dSAndrew Gallatin struct mbuf *m_head, **ifsd_m; 319895246abbSSean Bruno void *next_txd; 31994c7070dbSScott Long bus_dmamap_t map; 32004c7070dbSScott Long struct if_pkt_info pi; 32014c7070dbSScott Long int remap = 0; 32024c7070dbSScott Long int err, nsegs, ndesc, max_segs, pidx, cidx, next, ntxd; 32034c7070dbSScott Long 32044c7070dbSScott Long ctx = txq->ift_ctx; 32054c7070dbSScott Long sctx = ctx->ifc_sctx; 32064c7070dbSScott Long scctx = &ctx->ifc_softc_ctx; 32074c7070dbSScott Long segs = txq->ift_segs; 320823ac9029SStephen Hurd ntxd = txq->ift_size; 32094c7070dbSScott Long m_head = *m_headp; 32104c7070dbSScott Long map = NULL; 32114c7070dbSScott Long 32124c7070dbSScott Long /* 32134c7070dbSScott Long * If we're doing TSO the next descriptor to clean may be quite far ahead 32144c7070dbSScott Long */ 32154c7070dbSScott Long cidx = txq->ift_cidx; 32164c7070dbSScott Long pidx = txq->ift_pidx; 321795246abbSSean Bruno if (ctx->ifc_flags & IFC_PREFETCH) { 32184c7070dbSScott Long next = (cidx + CACHE_PTR_INCREMENT) & (ntxd-1); 321995246abbSSean Bruno if (!(ctx->ifc_flags & IFLIB_HAS_TXCQ)) { 322095246abbSSean Bruno next_txd = calc_next_txd(txq, cidx, 0); 322195246abbSSean Bruno prefetch(next_txd); 322295246abbSSean Bruno } 32234c7070dbSScott Long 32244c7070dbSScott Long /* prefetch the next cache line of mbuf pointers and flags */ 32254c7070dbSScott Long prefetch(&txq->ift_sds.ifsd_m[next]); 32264c7070dbSScott Long prefetch(&txq->ift_sds.ifsd_map[next]); 32274c7070dbSScott Long next = (cidx + CACHE_LINE_SIZE) & (ntxd-1); 32284c7070dbSScott Long } 322995246abbSSean Bruno map = txq->ift_sds.ifsd_map[pidx]; 3230fbec776dSAndrew Gallatin ifsd_m = txq->ift_sds.ifsd_m; 32314c7070dbSScott Long 32324c7070dbSScott Long if (m_head->m_pkthdr.csum_flags & CSUM_TSO) { 3233bfce461eSMarius Strobl buf_tag = txq->ift_tso_buf_tag; 32344c7070dbSScott Long max_segs = scctx->isc_tx_tso_segments_max; 32358a04b53dSKonstantin Belousov map = txq->ift_sds.ifsd_tso_map[pidx]; 3236bfce461eSMarius Strobl MPASS(buf_tag != NULL); 32377f87c040SMarius Strobl MPASS(max_segs > 0); 32384c7070dbSScott Long } else { 3239bfce461eSMarius Strobl buf_tag = txq->ift_buf_tag; 32404c7070dbSScott Long max_segs = scctx->isc_tx_nsegments; 32418a04b53dSKonstantin Belousov map = txq->ift_sds.ifsd_map[pidx]; 32424c7070dbSScott Long } 3243d14c853bSStephen Hurd if ((sctx->isc_flags & IFLIB_NEED_ETHER_PAD) && 3244d14c853bSStephen Hurd __predict_false(m_head->m_pkthdr.len < scctx->isc_min_frame_size)) { 3245a15fbbb8SStephen Hurd err = iflib_ether_pad(ctx->ifc_dev, m_headp, scctx->isc_min_frame_size); 324664e6fc13SStephen Hurd if (err) { 324764e6fc13SStephen Hurd DBG_COUNTER_INC(encap_txd_encap_fail); 3248d14c853bSStephen Hurd return err; 3249d14c853bSStephen Hurd } 325064e6fc13SStephen Hurd } 3251a15fbbb8SStephen Hurd m_head = *m_headp; 325295246abbSSean Bruno 325395246abbSSean Bruno pkt_info_zero(&pi); 3254ab2e3f79SStephen Hurd pi.ipi_mflags = (m_head->m_flags & (M_VLANTAG|M_BCAST|M_MCAST)); 3255ab2e3f79SStephen Hurd pi.ipi_pidx = pidx; 3256ab2e3f79SStephen Hurd pi.ipi_qsidx = txq->ift_id; 32573429c02fSStephen Hurd pi.ipi_len = m_head->m_pkthdr.len; 32583429c02fSStephen Hurd pi.ipi_csum_flags = m_head->m_pkthdr.csum_flags; 32593429c02fSStephen Hurd pi.ipi_vtag = (m_head->m_flags & M_VLANTAG) ? m_head->m_pkthdr.ether_vtag : 0; 32604c7070dbSScott Long 32614c7070dbSScott Long /* deliberate bitwise OR to make one condition */ 32624c7070dbSScott Long if (__predict_true((pi.ipi_csum_flags | pi.ipi_vtag))) { 326364e6fc13SStephen Hurd if (__predict_false((err = iflib_parse_header(txq, &pi, m_headp)) != 0)) { 326464e6fc13SStephen Hurd DBG_COUNTER_INC(encap_txd_encap_fail); 32654c7070dbSScott Long return (err); 326664e6fc13SStephen Hurd } 32674c7070dbSScott Long m_head = *m_headp; 32684c7070dbSScott Long } 32694c7070dbSScott Long 32704c7070dbSScott Long retry: 3271bfce461eSMarius Strobl err = bus_dmamap_load_mbuf_sg(buf_tag, map, m_head, segs, &nsegs, 3272fbec776dSAndrew Gallatin BUS_DMA_NOWAIT); 32734c7070dbSScott Long defrag: 32744c7070dbSScott Long if (__predict_false(err)) { 32754c7070dbSScott Long switch (err) { 32764c7070dbSScott Long case EFBIG: 32774c7070dbSScott Long /* try collapse once and defrag once */ 3278f7594707SAndrew Gallatin if (remap == 0) { 32794c7070dbSScott Long m_head = m_collapse(*m_headp, M_NOWAIT, max_segs); 3280f7594707SAndrew Gallatin /* try defrag if collapsing fails */ 3281f7594707SAndrew Gallatin if (m_head == NULL) 3282f7594707SAndrew Gallatin remap++; 3283f7594707SAndrew Gallatin } 328464e6fc13SStephen Hurd if (remap == 1) { 328564e6fc13SStephen Hurd txq->ift_mbuf_defrag++; 32864c7070dbSScott Long m_head = m_defrag(*m_headp, M_NOWAIT); 328764e6fc13SStephen Hurd } 32884c7070dbSScott Long remap++; 32894c7070dbSScott Long if (__predict_false(m_head == NULL)) 32904c7070dbSScott Long goto defrag_failed; 32914c7070dbSScott Long *m_headp = m_head; 32924c7070dbSScott Long goto retry; 32934c7070dbSScott Long break; 32944c7070dbSScott Long case ENOMEM: 32954c7070dbSScott Long txq->ift_no_tx_dma_setup++; 32964c7070dbSScott Long break; 32974c7070dbSScott Long default: 32984c7070dbSScott Long txq->ift_no_tx_dma_setup++; 32994c7070dbSScott Long m_freem(*m_headp); 33004c7070dbSScott Long DBG_COUNTER_INC(tx_frees); 33014c7070dbSScott Long *m_headp = NULL; 33024c7070dbSScott Long break; 33034c7070dbSScott Long } 33044c7070dbSScott Long txq->ift_map_failed++; 33054c7070dbSScott Long DBG_COUNTER_INC(encap_load_mbuf_fail); 330664e6fc13SStephen Hurd DBG_COUNTER_INC(encap_txd_encap_fail); 33074c7070dbSScott Long return (err); 33084c7070dbSScott Long } 3309fbec776dSAndrew Gallatin ifsd_m[pidx] = m_head; 33104c7070dbSScott Long /* 33114c7070dbSScott Long * XXX assumes a 1 to 1 relationship between segments and 33124c7070dbSScott Long * descriptors - this does not hold true on all drivers, e.g. 33134c7070dbSScott Long * cxgb 33144c7070dbSScott Long */ 33154c7070dbSScott Long if (__predict_false(nsegs + 2 > TXQ_AVAIL(txq))) { 33164c7070dbSScott Long txq->ift_no_desc_avail++; 3317bfce461eSMarius Strobl bus_dmamap_unload(buf_tag, map); 33184c7070dbSScott Long DBG_COUNTER_INC(encap_txq_avail_fail); 331964e6fc13SStephen Hurd DBG_COUNTER_INC(encap_txd_encap_fail); 332023ac9029SStephen Hurd if ((txq->ift_task.gt_task.ta_flags & TASK_ENQUEUED) == 0) 33214c7070dbSScott Long GROUPTASK_ENQUEUE(&txq->ift_task); 33224c7070dbSScott Long return (ENOBUFS); 33234c7070dbSScott Long } 332495246abbSSean Bruno /* 332595246abbSSean Bruno * On Intel cards we can greatly reduce the number of TX interrupts 332695246abbSSean Bruno * we see by only setting report status on every Nth descriptor. 332795246abbSSean Bruno * However, this also means that the driver will need to keep track 332895246abbSSean Bruno * of the descriptors that RS was set on to check them for the DD bit. 332995246abbSSean Bruno */ 333095246abbSSean Bruno txq->ift_rs_pending += nsegs + 1; 333195246abbSSean Bruno if (txq->ift_rs_pending > TXQ_MAX_RS_DEFERRED(txq) || 33321f7ce05dSAndrew Gallatin iflib_no_tx_batch || (TXQ_AVAIL(txq) - nsegs) <= MAX_TX_DESC(ctx) + 2) { 333395246abbSSean Bruno pi.ipi_flags |= IPI_TX_INTR; 333495246abbSSean Bruno txq->ift_rs_pending = 0; 333595246abbSSean Bruno } 333695246abbSSean Bruno 33374c7070dbSScott Long pi.ipi_segs = segs; 33384c7070dbSScott Long pi.ipi_nsegs = nsegs; 33394c7070dbSScott Long 334023ac9029SStephen Hurd MPASS(pidx >= 0 && pidx < txq->ift_size); 33414c7070dbSScott Long #ifdef PKT_DEBUG 33424c7070dbSScott Long print_pkt(&pi); 33434c7070dbSScott Long #endif 33444c7070dbSScott Long if ((err = ctx->isc_txd_encap(ctx->ifc_softc, &pi)) == 0) { 334595dcf343SMarius Strobl bus_dmamap_sync(buf_tag, map, BUS_DMASYNC_PREWRITE); 33464c7070dbSScott Long DBG_COUNTER_INC(tx_encap); 334795246abbSSean Bruno MPASS(pi.ipi_new_pidx < txq->ift_size); 33484c7070dbSScott Long 33494c7070dbSScott Long ndesc = pi.ipi_new_pidx - pi.ipi_pidx; 33504c7070dbSScott Long if (pi.ipi_new_pidx < pi.ipi_pidx) { 335123ac9029SStephen Hurd ndesc += txq->ift_size; 33524c7070dbSScott Long txq->ift_gen = 1; 33534c7070dbSScott Long } 33541248952aSSean Bruno /* 33551248952aSSean Bruno * drivers can need as many as 33561248952aSSean Bruno * two sentinels 33571248952aSSean Bruno */ 33581248952aSSean Bruno MPASS(ndesc <= pi.ipi_nsegs + 2); 33594c7070dbSScott Long MPASS(pi.ipi_new_pidx != pidx); 33604c7070dbSScott Long MPASS(ndesc > 0); 33614c7070dbSScott Long txq->ift_in_use += ndesc; 336295246abbSSean Bruno 33634c7070dbSScott Long /* 33644c7070dbSScott Long * We update the last software descriptor again here because there may 33654c7070dbSScott Long * be a sentinel and/or there may be more mbufs than segments 33664c7070dbSScott Long */ 33674c7070dbSScott Long txq->ift_pidx = pi.ipi_new_pidx; 33684c7070dbSScott Long txq->ift_npending += pi.ipi_ndescs; 3369f7594707SAndrew Gallatin } else { 337023ac9029SStephen Hurd *m_headp = m_head = iflib_remove_mbuf(txq); 3371f7594707SAndrew Gallatin if (err == EFBIG) { 33724c7070dbSScott Long txq->ift_txd_encap_efbig++; 3373f7594707SAndrew Gallatin if (remap < 2) { 3374f7594707SAndrew Gallatin remap = 1; 33754c7070dbSScott Long goto defrag; 3376f7594707SAndrew Gallatin } 3377f7594707SAndrew Gallatin } 3378f7594707SAndrew Gallatin goto defrag_failed; 3379f7594707SAndrew Gallatin } 338064e6fc13SStephen Hurd /* 338164e6fc13SStephen Hurd * err can't possibly be non-zero here, so we don't neet to test it 338264e6fc13SStephen Hurd * to see if we need to DBG_COUNTER_INC(encap_txd_encap_fail). 338364e6fc13SStephen Hurd */ 33844c7070dbSScott Long return (err); 33854c7070dbSScott Long 33864c7070dbSScott Long defrag_failed: 33874c7070dbSScott Long txq->ift_mbuf_defrag_failed++; 33884c7070dbSScott Long txq->ift_map_failed++; 33894c7070dbSScott Long m_freem(*m_headp); 33904c7070dbSScott Long DBG_COUNTER_INC(tx_frees); 33914c7070dbSScott Long *m_headp = NULL; 339264e6fc13SStephen Hurd DBG_COUNTER_INC(encap_txd_encap_fail); 33934c7070dbSScott Long return (ENOMEM); 33944c7070dbSScott Long } 33954c7070dbSScott Long 33964c7070dbSScott Long static void 33974c7070dbSScott Long iflib_tx_desc_free(iflib_txq_t txq, int n) 33984c7070dbSScott Long { 33994c7070dbSScott Long uint32_t qsize, cidx, mask, gen; 34004c7070dbSScott Long struct mbuf *m, **ifsd_m; 340195246abbSSean Bruno bool do_prefetch; 34024c7070dbSScott Long 34034c7070dbSScott Long cidx = txq->ift_cidx; 34044c7070dbSScott Long gen = txq->ift_gen; 340523ac9029SStephen Hurd qsize = txq->ift_size; 34064c7070dbSScott Long mask = qsize-1; 34074c7070dbSScott Long ifsd_m = txq->ift_sds.ifsd_m; 340895246abbSSean Bruno do_prefetch = (txq->ift_ctx->ifc_flags & IFC_PREFETCH); 34094c7070dbSScott Long 341094618825SMark Johnston while (n-- > 0) { 341195246abbSSean Bruno if (do_prefetch) { 34124c7070dbSScott Long prefetch(ifsd_m[(cidx + 3) & mask]); 34134c7070dbSScott Long prefetch(ifsd_m[(cidx + 4) & mask]); 341495246abbSSean Bruno } 34154c7070dbSScott Long if ((m = ifsd_m[cidx]) != NULL) { 3416fbec776dSAndrew Gallatin prefetch(&ifsd_m[(cidx + CACHE_PTR_INCREMENT) & mask]); 34178a04b53dSKonstantin Belousov if (m->m_pkthdr.csum_flags & CSUM_TSO) { 3418bfce461eSMarius Strobl bus_dmamap_sync(txq->ift_tso_buf_tag, 34198a04b53dSKonstantin Belousov txq->ift_sds.ifsd_tso_map[cidx], 34208a04b53dSKonstantin Belousov BUS_DMASYNC_POSTWRITE); 3421bfce461eSMarius Strobl bus_dmamap_unload(txq->ift_tso_buf_tag, 34228a04b53dSKonstantin Belousov txq->ift_sds.ifsd_tso_map[cidx]); 34238a04b53dSKonstantin Belousov } else { 3424bfce461eSMarius Strobl bus_dmamap_sync(txq->ift_buf_tag, 34258a04b53dSKonstantin Belousov txq->ift_sds.ifsd_map[cidx], 34268a04b53dSKonstantin Belousov BUS_DMASYNC_POSTWRITE); 3427bfce461eSMarius Strobl bus_dmamap_unload(txq->ift_buf_tag, 34288a04b53dSKonstantin Belousov txq->ift_sds.ifsd_map[cidx]); 34298a04b53dSKonstantin Belousov } 34304c7070dbSScott Long /* XXX we don't support any drivers that batch packets yet */ 34314c7070dbSScott Long MPASS(m->m_nextpkt == NULL); 34325c5ca36cSSean Bruno m_freem(m); 34334c7070dbSScott Long ifsd_m[cidx] = NULL; 34344c7070dbSScott Long #if MEMORY_LOGGING 34354c7070dbSScott Long txq->ift_dequeued++; 34364c7070dbSScott Long #endif 34374c7070dbSScott Long DBG_COUNTER_INC(tx_frees); 34384c7070dbSScott Long } 34394c7070dbSScott Long if (__predict_false(++cidx == qsize)) { 34404c7070dbSScott Long cidx = 0; 34414c7070dbSScott Long gen = 0; 34424c7070dbSScott Long } 34434c7070dbSScott Long } 34444c7070dbSScott Long txq->ift_cidx = cidx; 34454c7070dbSScott Long txq->ift_gen = gen; 34464c7070dbSScott Long } 34474c7070dbSScott Long 34484c7070dbSScott Long static __inline int 34494c7070dbSScott Long iflib_completed_tx_reclaim(iflib_txq_t txq, int thresh) 34504c7070dbSScott Long { 34514c7070dbSScott Long int reclaim; 34524c7070dbSScott Long if_ctx_t ctx = txq->ift_ctx; 34534c7070dbSScott Long 34544c7070dbSScott Long KASSERT(thresh >= 0, ("invalid threshold to reclaim")); 34554c7070dbSScott Long MPASS(thresh /*+ MAX_TX_DESC(txq->ift_ctx) */ < txq->ift_size); 34564c7070dbSScott Long 34574c7070dbSScott Long /* 34584c7070dbSScott Long * Need a rate-limiting check so that this isn't called every time 34594c7070dbSScott Long */ 34604c7070dbSScott Long iflib_tx_credits_update(ctx, txq); 34614c7070dbSScott Long reclaim = DESC_RECLAIMABLE(txq); 34624c7070dbSScott Long 34634c7070dbSScott Long if (reclaim <= thresh /* + MAX_TX_DESC(txq->ift_ctx) */) { 34644c7070dbSScott Long #ifdef INVARIANTS 34654c7070dbSScott Long if (iflib_verbose_debug) { 34664c7070dbSScott Long printf("%s processed=%ju cleaned=%ju tx_nsegments=%d reclaim=%d thresh=%d\n", __FUNCTION__, 34674c7070dbSScott Long txq->ift_processed, txq->ift_cleaned, txq->ift_ctx->ifc_softc_ctx.isc_tx_nsegments, 34684c7070dbSScott Long reclaim, thresh); 34694c7070dbSScott Long 34704c7070dbSScott Long } 34714c7070dbSScott Long #endif 34724c7070dbSScott Long return (0); 34734c7070dbSScott Long } 34744c7070dbSScott Long iflib_tx_desc_free(txq, reclaim); 34754c7070dbSScott Long txq->ift_cleaned += reclaim; 34764c7070dbSScott Long txq->ift_in_use -= reclaim; 34774c7070dbSScott Long 34784c7070dbSScott Long return (reclaim); 34794c7070dbSScott Long } 34804c7070dbSScott Long 34814c7070dbSScott Long static struct mbuf ** 348295246abbSSean Bruno _ring_peek_one(struct ifmp_ring *r, int cidx, int offset, int remaining) 34834c7070dbSScott Long { 348495246abbSSean Bruno int next, size; 348595246abbSSean Bruno struct mbuf **items; 34864c7070dbSScott Long 348795246abbSSean Bruno size = r->size; 348895246abbSSean Bruno next = (cidx + CACHE_PTR_INCREMENT) & (size-1); 348995246abbSSean Bruno items = __DEVOLATILE(struct mbuf **, &r->items[0]); 349095246abbSSean Bruno 349195246abbSSean Bruno prefetch(items[(cidx + offset) & (size-1)]); 349295246abbSSean Bruno if (remaining > 1) { 34933429c02fSStephen Hurd prefetch2cachelines(&items[next]); 34943429c02fSStephen Hurd prefetch2cachelines(items[(cidx + offset + 1) & (size-1)]); 34953429c02fSStephen Hurd prefetch2cachelines(items[(cidx + offset + 2) & (size-1)]); 34963429c02fSStephen Hurd prefetch2cachelines(items[(cidx + offset + 3) & (size-1)]); 349795246abbSSean Bruno } 349895246abbSSean Bruno return (__DEVOLATILE(struct mbuf **, &r->items[(cidx + offset) & (size-1)])); 34994c7070dbSScott Long } 35004c7070dbSScott Long 35014c7070dbSScott Long static void 35024c7070dbSScott Long iflib_txq_check_drain(iflib_txq_t txq, int budget) 35034c7070dbSScott Long { 35044c7070dbSScott Long 350595246abbSSean Bruno ifmp_ring_check_drainage(txq->ift_br, budget); 35064c7070dbSScott Long } 35074c7070dbSScott Long 35084c7070dbSScott Long static uint32_t 35094c7070dbSScott Long iflib_txq_can_drain(struct ifmp_ring *r) 35104c7070dbSScott Long { 35114c7070dbSScott Long iflib_txq_t txq = r->cookie; 35124c7070dbSScott Long if_ctx_t ctx = txq->ift_ctx; 35134c7070dbSScott Long 351495dcf343SMarius Strobl if (TXQ_AVAIL(txq) > MAX_TX_DESC(ctx) + 2) 351595dcf343SMarius Strobl return (1); 35168a04b53dSKonstantin Belousov bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map, 35178a04b53dSKonstantin Belousov BUS_DMASYNC_POSTREAD); 351895dcf343SMarius Strobl return (ctx->isc_txd_credits_update(ctx->ifc_softc, txq->ift_id, 351995dcf343SMarius Strobl false)); 35204c7070dbSScott Long } 35214c7070dbSScott Long 35224c7070dbSScott Long static uint32_t 35234c7070dbSScott Long iflib_txq_drain(struct ifmp_ring *r, uint32_t cidx, uint32_t pidx) 35244c7070dbSScott Long { 35254c7070dbSScott Long iflib_txq_t txq = r->cookie; 35264c7070dbSScott Long if_ctx_t ctx = txq->ift_ctx; 352795246abbSSean Bruno struct ifnet *ifp = ctx->ifc_ifp; 35284c7070dbSScott Long struct mbuf **mp, *m; 352995246abbSSean Bruno int i, count, consumed, pkt_sent, bytes_sent, mcast_sent, avail; 353095246abbSSean Bruno int reclaimed, err, in_use_prev, desc_used; 353195246abbSSean Bruno bool do_prefetch, ring, rang; 35324c7070dbSScott Long 35334c7070dbSScott Long if (__predict_false(!(if_getdrvflags(ifp) & IFF_DRV_RUNNING) || 35344c7070dbSScott Long !LINK_ACTIVE(ctx))) { 35354c7070dbSScott Long DBG_COUNTER_INC(txq_drain_notready); 35364c7070dbSScott Long return (0); 35374c7070dbSScott Long } 353895246abbSSean Bruno reclaimed = iflib_completed_tx_reclaim(txq, RECLAIM_THRESH(ctx)); 353995246abbSSean Bruno rang = iflib_txd_db_check(ctx, txq, reclaimed, txq->ift_in_use); 35404c7070dbSScott Long avail = IDXDIFF(pidx, cidx, r->size); 35414c7070dbSScott Long if (__predict_false(ctx->ifc_flags & IFC_QFLUSH)) { 35424c7070dbSScott Long DBG_COUNTER_INC(txq_drain_flushing); 35434c7070dbSScott Long for (i = 0; i < avail; i++) { 3544bc0e855bSStephen Hurd if (__predict_true(r->items[(cidx + i) & (r->size-1)] != (void *)txq)) 354523ac9029SStephen Hurd m_free(r->items[(cidx + i) & (r->size-1)]); 35464c7070dbSScott Long r->items[(cidx + i) & (r->size-1)] = NULL; 35474c7070dbSScott Long } 35484c7070dbSScott Long return (avail); 35494c7070dbSScott Long } 355095246abbSSean Bruno 35514c7070dbSScott Long if (__predict_false(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_OACTIVE)) { 35524c7070dbSScott Long txq->ift_qstatus = IFLIB_QUEUE_IDLE; 35534c7070dbSScott Long CALLOUT_LOCK(txq); 35544c7070dbSScott Long callout_stop(&txq->ift_timer); 35554c7070dbSScott Long CALLOUT_UNLOCK(txq); 35564c7070dbSScott Long DBG_COUNTER_INC(txq_drain_oactive); 35574c7070dbSScott Long return (0); 35584c7070dbSScott Long } 355995246abbSSean Bruno if (reclaimed) 356095246abbSSean Bruno txq->ift_qstatus = IFLIB_QUEUE_IDLE; 35614c7070dbSScott Long consumed = mcast_sent = bytes_sent = pkt_sent = 0; 35624c7070dbSScott Long count = MIN(avail, TX_BATCH_SIZE); 3563da69b8f9SSean Bruno #ifdef INVARIANTS 3564da69b8f9SSean Bruno if (iflib_verbose_debug) 3565da69b8f9SSean Bruno printf("%s avail=%d ifc_flags=%x txq_avail=%d ", __FUNCTION__, 3566da69b8f9SSean Bruno avail, ctx->ifc_flags, TXQ_AVAIL(txq)); 3567da69b8f9SSean Bruno #endif 356895246abbSSean Bruno do_prefetch = (ctx->ifc_flags & IFC_PREFETCH); 356995246abbSSean Bruno avail = TXQ_AVAIL(txq); 35701ae4848cSMatt Macy err = 0; 357195246abbSSean Bruno for (desc_used = i = 0; i < count && avail > MAX_TX_DESC(ctx) + 2; i++) { 35721ae4848cSMatt Macy int rem = do_prefetch ? count - i : 0; 35734c7070dbSScott Long 357495246abbSSean Bruno mp = _ring_peek_one(r, cidx, i, rem); 3575da69b8f9SSean Bruno MPASS(mp != NULL && *mp != NULL); 357695246abbSSean Bruno if (__predict_false(*mp == (struct mbuf *)txq)) { 357795246abbSSean Bruno consumed++; 357895246abbSSean Bruno reclaimed++; 357995246abbSSean Bruno continue; 358095246abbSSean Bruno } 35814c7070dbSScott Long in_use_prev = txq->ift_in_use; 358295246abbSSean Bruno err = iflib_encap(txq, mp); 358395246abbSSean Bruno if (__predict_false(err)) { 3584da69b8f9SSean Bruno /* no room - bail out */ 358595246abbSSean Bruno if (err == ENOBUFS) 35864c7070dbSScott Long break; 35874c7070dbSScott Long consumed++; 3588da69b8f9SSean Bruno /* we can't send this packet - skip it */ 35894c7070dbSScott Long continue; 3590da69b8f9SSean Bruno } 359195246abbSSean Bruno consumed++; 35924c7070dbSScott Long pkt_sent++; 35934c7070dbSScott Long m = *mp; 35944c7070dbSScott Long DBG_COUNTER_INC(tx_sent); 35954c7070dbSScott Long bytes_sent += m->m_pkthdr.len; 359695246abbSSean Bruno mcast_sent += !!(m->m_flags & M_MCAST); 359795246abbSSean Bruno avail = TXQ_AVAIL(txq); 35984c7070dbSScott Long 35994c7070dbSScott Long txq->ift_db_pending += (txq->ift_in_use - in_use_prev); 36004c7070dbSScott Long desc_used += (txq->ift_in_use - in_use_prev); 36014c7070dbSScott Long ETHER_BPF_MTAP(ifp, m); 360295246abbSSean Bruno if (__predict_false(!(ifp->if_drv_flags & IFF_DRV_RUNNING))) 36034c7070dbSScott Long break; 360495246abbSSean Bruno rang = iflib_txd_db_check(ctx, txq, false, in_use_prev); 36054c7070dbSScott Long } 36064c7070dbSScott Long 360795246abbSSean Bruno /* deliberate use of bitwise or to avoid gratuitous short-circuit */ 360895246abbSSean Bruno ring = rang ? false : (iflib_min_tx_latency | err) || (TXQ_AVAIL(txq) < MAX_TX_DESC(ctx)); 360995246abbSSean Bruno iflib_txd_db_check(ctx, txq, ring, txq->ift_in_use); 36104c7070dbSScott Long if_inc_counter(ifp, IFCOUNTER_OBYTES, bytes_sent); 36114c7070dbSScott Long if_inc_counter(ifp, IFCOUNTER_OPACKETS, pkt_sent); 36124c7070dbSScott Long if (mcast_sent) 36134c7070dbSScott Long if_inc_counter(ifp, IFCOUNTER_OMCASTS, mcast_sent); 3614da69b8f9SSean Bruno #ifdef INVARIANTS 3615da69b8f9SSean Bruno if (iflib_verbose_debug) 3616da69b8f9SSean Bruno printf("consumed=%d\n", consumed); 3617da69b8f9SSean Bruno #endif 36184c7070dbSScott Long return (consumed); 36194c7070dbSScott Long } 36204c7070dbSScott Long 3621da69b8f9SSean Bruno static uint32_t 3622da69b8f9SSean Bruno iflib_txq_drain_always(struct ifmp_ring *r) 3623da69b8f9SSean Bruno { 3624da69b8f9SSean Bruno return (1); 3625da69b8f9SSean Bruno } 3626da69b8f9SSean Bruno 3627da69b8f9SSean Bruno static uint32_t 3628da69b8f9SSean Bruno iflib_txq_drain_free(struct ifmp_ring *r, uint32_t cidx, uint32_t pidx) 3629da69b8f9SSean Bruno { 3630da69b8f9SSean Bruno int i, avail; 3631da69b8f9SSean Bruno struct mbuf **mp; 3632da69b8f9SSean Bruno iflib_txq_t txq; 3633da69b8f9SSean Bruno 3634da69b8f9SSean Bruno txq = r->cookie; 3635da69b8f9SSean Bruno 3636da69b8f9SSean Bruno txq->ift_qstatus = IFLIB_QUEUE_IDLE; 3637da69b8f9SSean Bruno CALLOUT_LOCK(txq); 3638da69b8f9SSean Bruno callout_stop(&txq->ift_timer); 3639da69b8f9SSean Bruno CALLOUT_UNLOCK(txq); 3640da69b8f9SSean Bruno 3641da69b8f9SSean Bruno avail = IDXDIFF(pidx, cidx, r->size); 3642da69b8f9SSean Bruno for (i = 0; i < avail; i++) { 364395246abbSSean Bruno mp = _ring_peek_one(r, cidx, i, avail - i); 364495246abbSSean Bruno if (__predict_false(*mp == (struct mbuf *)txq)) 364595246abbSSean Bruno continue; 3646da69b8f9SSean Bruno m_freem(*mp); 364764e6fc13SStephen Hurd DBG_COUNTER_INC(tx_frees); 3648da69b8f9SSean Bruno } 3649da69b8f9SSean Bruno MPASS(ifmp_ring_is_stalled(r) == 0); 3650da69b8f9SSean Bruno return (avail); 3651da69b8f9SSean Bruno } 3652da69b8f9SSean Bruno 3653da69b8f9SSean Bruno static void 3654da69b8f9SSean Bruno iflib_ifmp_purge(iflib_txq_t txq) 3655da69b8f9SSean Bruno { 3656da69b8f9SSean Bruno struct ifmp_ring *r; 3657da69b8f9SSean Bruno 365895246abbSSean Bruno r = txq->ift_br; 3659da69b8f9SSean Bruno r->drain = iflib_txq_drain_free; 3660da69b8f9SSean Bruno r->can_drain = iflib_txq_drain_always; 3661da69b8f9SSean Bruno 3662da69b8f9SSean Bruno ifmp_ring_check_drainage(r, r->size); 3663da69b8f9SSean Bruno 3664da69b8f9SSean Bruno r->drain = iflib_txq_drain; 3665da69b8f9SSean Bruno r->can_drain = iflib_txq_can_drain; 3666da69b8f9SSean Bruno } 3667da69b8f9SSean Bruno 36684c7070dbSScott Long static void 366923ac9029SStephen Hurd _task_fn_tx(void *context) 36704c7070dbSScott Long { 36714c7070dbSScott Long iflib_txq_t txq = context; 36724c7070dbSScott Long if_ctx_t ctx = txq->ift_ctx; 3673a6611c93SMarius Strobl #if defined(ALTQ) || defined(DEV_NETMAP) 3674a6611c93SMarius Strobl if_t ifp = ctx->ifc_ifp; 3675a6611c93SMarius Strobl #endif 3676fe51d4cdSStephen Hurd int abdicate = ctx->ifc_sysctl_tx_abdicate; 36774c7070dbSScott Long 36781248952aSSean Bruno #ifdef IFLIB_DIAGNOSTICS 36791248952aSSean Bruno txq->ift_cpu_exec_count[curcpu]++; 36801248952aSSean Bruno #endif 36814c7070dbSScott Long if (!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING)) 36824c7070dbSScott Long return; 368395dcf343SMarius Strobl #ifdef DEV_NETMAP 3684a6611c93SMarius Strobl if (if_getcapenable(ifp) & IFCAP_NETMAP) { 36858a04b53dSKonstantin Belousov bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map, 36868a04b53dSKonstantin Belousov BUS_DMASYNC_POSTREAD); 368795246abbSSean Bruno if (ctx->isc_txd_credits_update(ctx->ifc_softc, txq->ift_id, false)) 3688a6611c93SMarius Strobl netmap_tx_irq(ifp, txq->ift_id); 368995246abbSSean Bruno IFDI_TX_QUEUE_INTR_ENABLE(ctx, txq->ift_id); 369095246abbSSean Bruno return; 369195246abbSSean Bruno } 369295dcf343SMarius Strobl #endif 3693b8ca4756SPatrick Kelsey #ifdef ALTQ 3694b8ca4756SPatrick Kelsey if (ALTQ_IS_ENABLED(&ifp->if_snd)) 3695b8ca4756SPatrick Kelsey iflib_altq_if_start(ifp); 3696b8ca4756SPatrick Kelsey #endif 369795246abbSSean Bruno if (txq->ift_db_pending) 3698fe51d4cdSStephen Hurd ifmp_ring_enqueue(txq->ift_br, (void **)&txq, 1, TX_BATCH_SIZE, abdicate); 3699fe51d4cdSStephen Hurd else if (!abdicate) 3700fe51d4cdSStephen Hurd ifmp_ring_check_drainage(txq->ift_br, TX_BATCH_SIZE); 3701fe51d4cdSStephen Hurd /* 3702fe51d4cdSStephen Hurd * When abdicating, we always need to check drainage, not just when we don't enqueue 3703fe51d4cdSStephen Hurd */ 3704fe51d4cdSStephen Hurd if (abdicate) 3705fe51d4cdSStephen Hurd ifmp_ring_check_drainage(txq->ift_br, TX_BATCH_SIZE); 370695246abbSSean Bruno if (ctx->ifc_flags & IFC_LEGACY) 370795246abbSSean Bruno IFDI_INTR_ENABLE(ctx); 370895246abbSSean Bruno else { 37091ae4848cSMatt Macy #ifdef INVARIANTS 37101ae4848cSMatt Macy int rc = 37111ae4848cSMatt Macy #endif 37121ae4848cSMatt Macy IFDI_TX_QUEUE_INTR_ENABLE(ctx, txq->ift_id); 371395246abbSSean Bruno KASSERT(rc != ENOTSUP, ("MSI-X support requires queue_intr_enable, but not implemented in driver")); 371495246abbSSean Bruno } 37154c7070dbSScott Long } 37164c7070dbSScott Long 37174c7070dbSScott Long static void 371823ac9029SStephen Hurd _task_fn_rx(void *context) 37194c7070dbSScott Long { 37204c7070dbSScott Long iflib_rxq_t rxq = context; 37214c7070dbSScott Long if_ctx_t ctx = rxq->ifr_ctx; 37224c7070dbSScott Long bool more; 3723f4d2154eSStephen Hurd uint16_t budget; 37244c7070dbSScott Long 37251248952aSSean Bruno #ifdef IFLIB_DIAGNOSTICS 37261248952aSSean Bruno rxq->ifr_cpu_exec_count[curcpu]++; 37271248952aSSean Bruno #endif 37284c7070dbSScott Long DBG_COUNTER_INC(task_fn_rxs); 37294c7070dbSScott Long if (__predict_false(!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING))) 37304c7070dbSScott Long return; 3731d0d0ad0aSStephen Hurd more = true; 3732d0d0ad0aSStephen Hurd #ifdef DEV_NETMAP 3733d0d0ad0aSStephen Hurd if (if_getcapenable(ctx->ifc_ifp) & IFCAP_NETMAP) { 3734d0d0ad0aSStephen Hurd u_int work = 0; 3735d0d0ad0aSStephen Hurd if (netmap_rx_irq(ctx->ifc_ifp, rxq->ifr_id, &work)) { 3736d0d0ad0aSStephen Hurd more = false; 3737d0d0ad0aSStephen Hurd } 3738d0d0ad0aSStephen Hurd } 3739d0d0ad0aSStephen Hurd #endif 3740f4d2154eSStephen Hurd budget = ctx->ifc_sysctl_rx_budget; 3741f4d2154eSStephen Hurd if (budget == 0) 3742f4d2154eSStephen Hurd budget = 16; /* XXX */ 3743f4d2154eSStephen Hurd if (more == false || (more = iflib_rxeof(rxq, budget)) == false) { 37444c7070dbSScott Long if (ctx->ifc_flags & IFC_LEGACY) 37454c7070dbSScott Long IFDI_INTR_ENABLE(ctx); 37464c7070dbSScott Long else { 37471ae4848cSMatt Macy #ifdef INVARIANTS 37481ae4848cSMatt Macy int rc = 37491ae4848cSMatt Macy #endif 37501ae4848cSMatt Macy IFDI_RX_QUEUE_INTR_ENABLE(ctx, rxq->ifr_id); 375123ac9029SStephen Hurd KASSERT(rc != ENOTSUP, ("MSI-X support requires queue_intr_enable, but not implemented in driver")); 37521ae4848cSMatt Macy DBG_COUNTER_INC(rx_intr_enables); 37534c7070dbSScott Long } 37544c7070dbSScott Long } 37554c7070dbSScott Long if (__predict_false(!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING))) 37564c7070dbSScott Long return; 37574c7070dbSScott Long if (more) 37584c7070dbSScott Long GROUPTASK_ENQUEUE(&rxq->ifr_task); 37594c7070dbSScott Long } 37604c7070dbSScott Long 37614c7070dbSScott Long static void 376223ac9029SStephen Hurd _task_fn_admin(void *context) 37634c7070dbSScott Long { 37644c7070dbSScott Long if_ctx_t ctx = context; 37654c7070dbSScott Long if_softc_ctx_t sctx = &ctx->ifc_softc_ctx; 37664c7070dbSScott Long iflib_txq_t txq; 3767ab2e3f79SStephen Hurd int i; 376877c1fcecSEric Joyner bool oactive, running, do_reset, do_watchdog, in_detach; 3769dd7fbcf1SStephen Hurd uint32_t reset_on = hz / 2; 3770ab2e3f79SStephen Hurd 37717b610b60SSean Bruno STATE_LOCK(ctx); 37727b610b60SSean Bruno running = (if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING); 37737b610b60SSean Bruno oactive = (if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_OACTIVE); 37747b610b60SSean Bruno do_reset = (ctx->ifc_flags & IFC_DO_RESET); 37757b610b60SSean Bruno do_watchdog = (ctx->ifc_flags & IFC_DO_WATCHDOG); 377677c1fcecSEric Joyner in_detach = (ctx->ifc_flags & IFC_IN_DETACH); 37777b610b60SSean Bruno ctx->ifc_flags &= ~(IFC_DO_RESET|IFC_DO_WATCHDOG); 37787b610b60SSean Bruno STATE_UNLOCK(ctx); 37797b610b60SSean Bruno 378077c1fcecSEric Joyner if ((!running && !oactive) && !(ctx->ifc_sctx->isc_flags & IFLIB_ADMIN_ALWAYS_RUN)) 378177c1fcecSEric Joyner return; 378277c1fcecSEric Joyner if (in_detach) 3783ab2e3f79SStephen Hurd return; 37844c7070dbSScott Long 37854c7070dbSScott Long CTX_LOCK(ctx); 37864c7070dbSScott Long for (txq = ctx->ifc_txqs, i = 0; i < sctx->isc_ntxqsets; i++, txq++) { 37874c7070dbSScott Long CALLOUT_LOCK(txq); 37884c7070dbSScott Long callout_stop(&txq->ift_timer); 37894c7070dbSScott Long CALLOUT_UNLOCK(txq); 37904c7070dbSScott Long } 37917b610b60SSean Bruno if (do_watchdog) { 37927b610b60SSean Bruno ctx->ifc_watchdog_events++; 37937b610b60SSean Bruno IFDI_WATCHDOG_RESET(ctx); 37947b610b60SSean Bruno } 3795d300df01SStephen Hurd IFDI_UPDATE_ADMIN_STATUS(ctx); 3796dd7fbcf1SStephen Hurd for (txq = ctx->ifc_txqs, i = 0; i < sctx->isc_ntxqsets; i++, txq++) { 3797dd7fbcf1SStephen Hurd #ifdef DEV_NETMAP 3798dd7fbcf1SStephen Hurd reset_on = hz / 2; 3799dd7fbcf1SStephen Hurd if (if_getcapenable(ctx->ifc_ifp) & IFCAP_NETMAP) 380095dcf343SMarius Strobl iflib_netmap_timer_adjust(ctx, txq, &reset_on); 3801dd7fbcf1SStephen Hurd #endif 3802dd7fbcf1SStephen Hurd callout_reset_on(&txq->ift_timer, reset_on, iflib_timer, txq, txq->ift_timer.c_cpu); 3803dd7fbcf1SStephen Hurd } 3804ab2e3f79SStephen Hurd IFDI_LINK_INTR_ENABLE(ctx); 38057b610b60SSean Bruno if (do_reset) 3806ab2e3f79SStephen Hurd iflib_if_init_locked(ctx); 38074c7070dbSScott Long CTX_UNLOCK(ctx); 38084c7070dbSScott Long 3809ab2e3f79SStephen Hurd if (LINK_ACTIVE(ctx) == 0) 38104c7070dbSScott Long return; 38114c7070dbSScott Long for (txq = ctx->ifc_txqs, i = 0; i < sctx->isc_ntxqsets; i++, txq++) 38124c7070dbSScott Long iflib_txq_check_drain(txq, IFLIB_RESTART_BUDGET); 38134c7070dbSScott Long } 38144c7070dbSScott Long 38154c7070dbSScott Long 38164c7070dbSScott Long static void 381723ac9029SStephen Hurd _task_fn_iov(void *context) 38184c7070dbSScott Long { 38194c7070dbSScott Long if_ctx_t ctx = context; 38204c7070dbSScott Long 382177c1fcecSEric Joyner if (!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING) && 382277c1fcecSEric Joyner !(ctx->ifc_sctx->isc_flags & IFLIB_ADMIN_ALWAYS_RUN)) 38234c7070dbSScott Long return; 38244c7070dbSScott Long 38254c7070dbSScott Long CTX_LOCK(ctx); 38264c7070dbSScott Long IFDI_VFLR_HANDLE(ctx); 38274c7070dbSScott Long CTX_UNLOCK(ctx); 38284c7070dbSScott Long } 38294c7070dbSScott Long 38304c7070dbSScott Long static int 38314c7070dbSScott Long iflib_sysctl_int_delay(SYSCTL_HANDLER_ARGS) 38324c7070dbSScott Long { 38334c7070dbSScott Long int err; 38344c7070dbSScott Long if_int_delay_info_t info; 38354c7070dbSScott Long if_ctx_t ctx; 38364c7070dbSScott Long 38374c7070dbSScott Long info = (if_int_delay_info_t)arg1; 38384c7070dbSScott Long ctx = info->iidi_ctx; 38394c7070dbSScott Long info->iidi_req = req; 38404c7070dbSScott Long info->iidi_oidp = oidp; 38414c7070dbSScott Long CTX_LOCK(ctx); 38424c7070dbSScott Long err = IFDI_SYSCTL_INT_DELAY(ctx, info); 38434c7070dbSScott Long CTX_UNLOCK(ctx); 38444c7070dbSScott Long return (err); 38454c7070dbSScott Long } 38464c7070dbSScott Long 38474c7070dbSScott Long /********************************************************************* 38484c7070dbSScott Long * 38494c7070dbSScott Long * IFNET FUNCTIONS 38504c7070dbSScott Long * 38514c7070dbSScott Long **********************************************************************/ 38524c7070dbSScott Long 38534c7070dbSScott Long static void 38544c7070dbSScott Long iflib_if_init_locked(if_ctx_t ctx) 38554c7070dbSScott Long { 38564c7070dbSScott Long iflib_stop(ctx); 38574c7070dbSScott Long iflib_init_locked(ctx); 38584c7070dbSScott Long } 38594c7070dbSScott Long 38604c7070dbSScott Long 38614c7070dbSScott Long static void 38624c7070dbSScott Long iflib_if_init(void *arg) 38634c7070dbSScott Long { 38644c7070dbSScott Long if_ctx_t ctx = arg; 38654c7070dbSScott Long 38664c7070dbSScott Long CTX_LOCK(ctx); 38674c7070dbSScott Long iflib_if_init_locked(ctx); 38684c7070dbSScott Long CTX_UNLOCK(ctx); 38694c7070dbSScott Long } 38704c7070dbSScott Long 38714c7070dbSScott Long static int 38724c7070dbSScott Long iflib_if_transmit(if_t ifp, struct mbuf *m) 38734c7070dbSScott Long { 38744c7070dbSScott Long if_ctx_t ctx = if_getsoftc(ifp); 38754c7070dbSScott Long 38764c7070dbSScott Long iflib_txq_t txq; 387723ac9029SStephen Hurd int err, qidx; 3878fe51d4cdSStephen Hurd int abdicate = ctx->ifc_sysctl_tx_abdicate; 38794c7070dbSScott Long 38804c7070dbSScott Long if (__predict_false((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || !LINK_ACTIVE(ctx))) { 38814c7070dbSScott Long DBG_COUNTER_INC(tx_frees); 38824c7070dbSScott Long m_freem(m); 3883da69b8f9SSean Bruno return (ENOBUFS); 38844c7070dbSScott Long } 38854c7070dbSScott Long 388623ac9029SStephen Hurd MPASS(m->m_nextpkt == NULL); 3887b8ca4756SPatrick Kelsey /* ALTQ-enabled interfaces always use queue 0. */ 38884c7070dbSScott Long qidx = 0; 3889b8ca4756SPatrick Kelsey if ((NTXQSETS(ctx) > 1) && M_HASHTYPE_GET(m) && !ALTQ_IS_ENABLED(&ifp->if_snd)) 38904c7070dbSScott Long qidx = QIDX(ctx, m); 38914c7070dbSScott Long /* 38924c7070dbSScott Long * XXX calculate buf_ring based on flowid (divvy up bits?) 38934c7070dbSScott Long */ 38944c7070dbSScott Long txq = &ctx->ifc_txqs[qidx]; 38954c7070dbSScott Long 38964c7070dbSScott Long #ifdef DRIVER_BACKPRESSURE 38974c7070dbSScott Long if (txq->ift_closed) { 38984c7070dbSScott Long while (m != NULL) { 38994c7070dbSScott Long next = m->m_nextpkt; 39004c7070dbSScott Long m->m_nextpkt = NULL; 39014c7070dbSScott Long m_freem(m); 390264e6fc13SStephen Hurd DBG_COUNTER_INC(tx_frees); 39034c7070dbSScott Long m = next; 39044c7070dbSScott Long } 39054c7070dbSScott Long return (ENOBUFS); 39064c7070dbSScott Long } 39074c7070dbSScott Long #endif 390823ac9029SStephen Hurd #ifdef notyet 39094c7070dbSScott Long qidx = count = 0; 39104c7070dbSScott Long mp = marr; 39114c7070dbSScott Long next = m; 39124c7070dbSScott Long do { 39134c7070dbSScott Long count++; 39144c7070dbSScott Long next = next->m_nextpkt; 39154c7070dbSScott Long } while (next != NULL); 39164c7070dbSScott Long 391716fb86abSConrad Meyer if (count > nitems(marr)) 39184c7070dbSScott Long if ((mp = malloc(count*sizeof(struct mbuf *), M_IFLIB, M_NOWAIT)) == NULL) { 39194c7070dbSScott Long /* XXX check nextpkt */ 39204c7070dbSScott Long m_freem(m); 39214c7070dbSScott Long /* XXX simplify for now */ 39224c7070dbSScott Long DBG_COUNTER_INC(tx_frees); 39234c7070dbSScott Long return (ENOBUFS); 39244c7070dbSScott Long } 39254c7070dbSScott Long for (next = m, i = 0; next != NULL; i++) { 39264c7070dbSScott Long mp[i] = next; 39274c7070dbSScott Long next = next->m_nextpkt; 39284c7070dbSScott Long mp[i]->m_nextpkt = NULL; 39294c7070dbSScott Long } 393023ac9029SStephen Hurd #endif 39314c7070dbSScott Long DBG_COUNTER_INC(tx_seen); 3932fe51d4cdSStephen Hurd err = ifmp_ring_enqueue(txq->ift_br, (void **)&m, 1, TX_BATCH_SIZE, abdicate); 39334c7070dbSScott Long 3934fe51d4cdSStephen Hurd if (abdicate) 3935ab2e3f79SStephen Hurd GROUPTASK_ENQUEUE(&txq->ift_task); 39361225d9daSStephen Hurd if (err) { 3937fe51d4cdSStephen Hurd if (!abdicate) 3938fe51d4cdSStephen Hurd GROUPTASK_ENQUEUE(&txq->ift_task); 39394c7070dbSScott Long /* support forthcoming later */ 39404c7070dbSScott Long #ifdef DRIVER_BACKPRESSURE 39414c7070dbSScott Long txq->ift_closed = TRUE; 39424c7070dbSScott Long #endif 394395246abbSSean Bruno ifmp_ring_check_drainage(txq->ift_br, TX_BATCH_SIZE); 394423ac9029SStephen Hurd m_freem(m); 394564e6fc13SStephen Hurd DBG_COUNTER_INC(tx_frees); 39464c7070dbSScott Long } 39474c7070dbSScott Long 39484c7070dbSScott Long return (err); 39494c7070dbSScott Long } 39504c7070dbSScott Long 3951b8ca4756SPatrick Kelsey #ifdef ALTQ 3952b8ca4756SPatrick Kelsey /* 3953b8ca4756SPatrick Kelsey * The overall approach to integrating iflib with ALTQ is to continue to use 3954b8ca4756SPatrick Kelsey * the iflib mp_ring machinery between the ALTQ queue(s) and the hardware 3955b8ca4756SPatrick Kelsey * ring. Technically, when using ALTQ, queueing to an intermediate mp_ring 3956b8ca4756SPatrick Kelsey * is redundant/unnecessary, but doing so minimizes the amount of 3957b8ca4756SPatrick Kelsey * ALTQ-specific code required in iflib. It is assumed that the overhead of 3958b8ca4756SPatrick Kelsey * redundantly queueing to an intermediate mp_ring is swamped by the 3959b8ca4756SPatrick Kelsey * performance limitations inherent in using ALTQ. 3960b8ca4756SPatrick Kelsey * 3961b8ca4756SPatrick Kelsey * When ALTQ support is compiled in, all iflib drivers will use a transmit 3962b8ca4756SPatrick Kelsey * routine, iflib_altq_if_transmit(), that checks if ALTQ is enabled for the 3963b8ca4756SPatrick Kelsey * given interface. If ALTQ is enabled for an interface, then all 3964b8ca4756SPatrick Kelsey * transmitted packets for that interface will be submitted to the ALTQ 3965b8ca4756SPatrick Kelsey * subsystem via IFQ_ENQUEUE(). We don't use the legacy if_transmit() 3966b8ca4756SPatrick Kelsey * implementation because it uses IFQ_HANDOFF(), which will duplicatively 3967b8ca4756SPatrick Kelsey * update stats that the iflib machinery handles, and which is sensitve to 3968b8ca4756SPatrick Kelsey * the disused IFF_DRV_OACTIVE flag. Additionally, iflib_altq_if_start() 3969b8ca4756SPatrick Kelsey * will be installed as the start routine for use by ALTQ facilities that 3970b8ca4756SPatrick Kelsey * need to trigger queue drains on a scheduled basis. 3971b8ca4756SPatrick Kelsey * 3972b8ca4756SPatrick Kelsey */ 3973b8ca4756SPatrick Kelsey static void 3974b8ca4756SPatrick Kelsey iflib_altq_if_start(if_t ifp) 3975b8ca4756SPatrick Kelsey { 3976b8ca4756SPatrick Kelsey struct ifaltq *ifq = &ifp->if_snd; 3977b8ca4756SPatrick Kelsey struct mbuf *m; 3978b8ca4756SPatrick Kelsey 3979b8ca4756SPatrick Kelsey IFQ_LOCK(ifq); 3980b8ca4756SPatrick Kelsey IFQ_DEQUEUE_NOLOCK(ifq, m); 3981b8ca4756SPatrick Kelsey while (m != NULL) { 3982b8ca4756SPatrick Kelsey iflib_if_transmit(ifp, m); 3983b8ca4756SPatrick Kelsey IFQ_DEQUEUE_NOLOCK(ifq, m); 3984b8ca4756SPatrick Kelsey } 3985b8ca4756SPatrick Kelsey IFQ_UNLOCK(ifq); 3986b8ca4756SPatrick Kelsey } 3987b8ca4756SPatrick Kelsey 3988b8ca4756SPatrick Kelsey static int 3989b8ca4756SPatrick Kelsey iflib_altq_if_transmit(if_t ifp, struct mbuf *m) 3990b8ca4756SPatrick Kelsey { 3991b8ca4756SPatrick Kelsey int err; 3992b8ca4756SPatrick Kelsey 3993b8ca4756SPatrick Kelsey if (ALTQ_IS_ENABLED(&ifp->if_snd)) { 3994b8ca4756SPatrick Kelsey IFQ_ENQUEUE(&ifp->if_snd, m, err); 3995b8ca4756SPatrick Kelsey if (err == 0) 3996b8ca4756SPatrick Kelsey iflib_altq_if_start(ifp); 3997b8ca4756SPatrick Kelsey } else 3998b8ca4756SPatrick Kelsey err = iflib_if_transmit(ifp, m); 3999b8ca4756SPatrick Kelsey 4000b8ca4756SPatrick Kelsey return (err); 4001b8ca4756SPatrick Kelsey } 4002b8ca4756SPatrick Kelsey #endif /* ALTQ */ 4003b8ca4756SPatrick Kelsey 40044c7070dbSScott Long static void 40054c7070dbSScott Long iflib_if_qflush(if_t ifp) 40064c7070dbSScott Long { 40074c7070dbSScott Long if_ctx_t ctx = if_getsoftc(ifp); 40084c7070dbSScott Long iflib_txq_t txq = ctx->ifc_txqs; 40094c7070dbSScott Long int i; 40104c7070dbSScott Long 40117b610b60SSean Bruno STATE_LOCK(ctx); 40124c7070dbSScott Long ctx->ifc_flags |= IFC_QFLUSH; 40137b610b60SSean Bruno STATE_UNLOCK(ctx); 40144c7070dbSScott Long for (i = 0; i < NTXQSETS(ctx); i++, txq++) 401595246abbSSean Bruno while (!(ifmp_ring_is_idle(txq->ift_br) || ifmp_ring_is_stalled(txq->ift_br))) 40164c7070dbSScott Long iflib_txq_check_drain(txq, 0); 40177b610b60SSean Bruno STATE_LOCK(ctx); 40184c7070dbSScott Long ctx->ifc_flags &= ~IFC_QFLUSH; 40197b610b60SSean Bruno STATE_UNLOCK(ctx); 40204c7070dbSScott Long 4021b8ca4756SPatrick Kelsey /* 4022b8ca4756SPatrick Kelsey * When ALTQ is enabled, this will also take care of purging the 4023b8ca4756SPatrick Kelsey * ALTQ queue(s). 4024b8ca4756SPatrick Kelsey */ 40254c7070dbSScott Long if_qflush(ifp); 40264c7070dbSScott Long } 40274c7070dbSScott Long 40284c7070dbSScott Long 40290c919c23SStephen Hurd #define IFCAP_FLAGS (IFCAP_HWCSUM_IPV6 | IFCAP_HWCSUM | IFCAP_LRO | \ 40300c919c23SStephen Hurd IFCAP_TSO | IFCAP_VLAN_HWTAGGING | IFCAP_HWSTATS | \ 40310c919c23SStephen Hurd IFCAP_VLAN_MTU | IFCAP_VLAN_HWFILTER | \ 40320c919c23SStephen Hurd IFCAP_VLAN_HWTSO | IFCAP_VLAN_HWCSUM) 40334c7070dbSScott Long 40344c7070dbSScott Long static int 40354c7070dbSScott Long iflib_if_ioctl(if_t ifp, u_long command, caddr_t data) 40364c7070dbSScott Long { 40374c7070dbSScott Long if_ctx_t ctx = if_getsoftc(ifp); 40384c7070dbSScott Long struct ifreq *ifr = (struct ifreq *)data; 40394c7070dbSScott Long #if defined(INET) || defined(INET6) 40404c7070dbSScott Long struct ifaddr *ifa = (struct ifaddr *)data; 40414c7070dbSScott Long #endif 40424c7070dbSScott Long bool avoid_reset = FALSE; 40434c7070dbSScott Long int err = 0, reinit = 0, bits; 40444c7070dbSScott Long 40454c7070dbSScott Long switch (command) { 40464c7070dbSScott Long case SIOCSIFADDR: 40474c7070dbSScott Long #ifdef INET 40484c7070dbSScott Long if (ifa->ifa_addr->sa_family == AF_INET) 40494c7070dbSScott Long avoid_reset = TRUE; 40504c7070dbSScott Long #endif 40514c7070dbSScott Long #ifdef INET6 40524c7070dbSScott Long if (ifa->ifa_addr->sa_family == AF_INET6) 40534c7070dbSScott Long avoid_reset = TRUE; 40544c7070dbSScott Long #endif 40554c7070dbSScott Long /* 40564c7070dbSScott Long ** Calling init results in link renegotiation, 40574c7070dbSScott Long ** so we avoid doing it when possible. 40584c7070dbSScott Long */ 40594c7070dbSScott Long if (avoid_reset) { 40604c7070dbSScott Long if_setflagbits(ifp, IFF_UP,0); 40614c7070dbSScott Long if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) 40624c7070dbSScott Long reinit = 1; 40634c7070dbSScott Long #ifdef INET 40644c7070dbSScott Long if (!(if_getflags(ifp) & IFF_NOARP)) 40654c7070dbSScott Long arp_ifinit(ifp, ifa); 40664c7070dbSScott Long #endif 40674c7070dbSScott Long } else 40684c7070dbSScott Long err = ether_ioctl(ifp, command, data); 40694c7070dbSScott Long break; 40704c7070dbSScott Long case SIOCSIFMTU: 40714c7070dbSScott Long CTX_LOCK(ctx); 40724c7070dbSScott Long if (ifr->ifr_mtu == if_getmtu(ifp)) { 40734c7070dbSScott Long CTX_UNLOCK(ctx); 40744c7070dbSScott Long break; 40754c7070dbSScott Long } 40764c7070dbSScott Long bits = if_getdrvflags(ifp); 40774c7070dbSScott Long /* stop the driver and free any clusters before proceeding */ 40784c7070dbSScott Long iflib_stop(ctx); 40794c7070dbSScott Long 40804c7070dbSScott Long if ((err = IFDI_MTU_SET(ctx, ifr->ifr_mtu)) == 0) { 40817b610b60SSean Bruno STATE_LOCK(ctx); 40824c7070dbSScott Long if (ifr->ifr_mtu > ctx->ifc_max_fl_buf_size) 40834c7070dbSScott Long ctx->ifc_flags |= IFC_MULTISEG; 40844c7070dbSScott Long else 40854c7070dbSScott Long ctx->ifc_flags &= ~IFC_MULTISEG; 40867b610b60SSean Bruno STATE_UNLOCK(ctx); 40874c7070dbSScott Long err = if_setmtu(ifp, ifr->ifr_mtu); 40884c7070dbSScott Long } 40894c7070dbSScott Long iflib_init_locked(ctx); 40907b610b60SSean Bruno STATE_LOCK(ctx); 40914c7070dbSScott Long if_setdrvflags(ifp, bits); 40927b610b60SSean Bruno STATE_UNLOCK(ctx); 40934c7070dbSScott Long CTX_UNLOCK(ctx); 40944c7070dbSScott Long break; 40954c7070dbSScott Long case SIOCSIFFLAGS: 4096ab2e3f79SStephen Hurd CTX_LOCK(ctx); 4097ab2e3f79SStephen Hurd if (if_getflags(ifp) & IFF_UP) { 4098ab2e3f79SStephen Hurd if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) { 4099ab2e3f79SStephen Hurd if ((if_getflags(ifp) ^ ctx->ifc_if_flags) & 4100ab2e3f79SStephen Hurd (IFF_PROMISC | IFF_ALLMULTI)) { 4101ab2e3f79SStephen Hurd err = IFDI_PROMISC_SET(ctx, if_getflags(ifp)); 4102ab2e3f79SStephen Hurd } 4103ab2e3f79SStephen Hurd } else 4104ab2e3f79SStephen Hurd reinit = 1; 4105ab2e3f79SStephen Hurd } else if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) { 4106ab2e3f79SStephen Hurd iflib_stop(ctx); 4107ab2e3f79SStephen Hurd } 4108ab2e3f79SStephen Hurd ctx->ifc_if_flags = if_getflags(ifp); 4109ab2e3f79SStephen Hurd CTX_UNLOCK(ctx); 41104c7070dbSScott Long break; 41114c7070dbSScott Long case SIOCADDMULTI: 41124c7070dbSScott Long case SIOCDELMULTI: 41134c7070dbSScott Long if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) { 4114ab2e3f79SStephen Hurd CTX_LOCK(ctx); 4115ab2e3f79SStephen Hurd IFDI_INTR_DISABLE(ctx); 4116ab2e3f79SStephen Hurd IFDI_MULTI_SET(ctx); 4117ab2e3f79SStephen Hurd IFDI_INTR_ENABLE(ctx); 4118ab2e3f79SStephen Hurd CTX_UNLOCK(ctx); 41194c7070dbSScott Long } 41204c7070dbSScott Long break; 41214c7070dbSScott Long case SIOCSIFMEDIA: 41224c7070dbSScott Long CTX_LOCK(ctx); 41234c7070dbSScott Long IFDI_MEDIA_SET(ctx); 41244c7070dbSScott Long CTX_UNLOCK(ctx); 41254c7070dbSScott Long /* falls thru */ 41264c7070dbSScott Long case SIOCGIFMEDIA: 4127a027c8e9SStephen Hurd case SIOCGIFXMEDIA: 41284c7070dbSScott Long err = ifmedia_ioctl(ifp, ifr, &ctx->ifc_media, command); 41294c7070dbSScott Long break; 41304c7070dbSScott Long case SIOCGI2C: 41314c7070dbSScott Long { 41324c7070dbSScott Long struct ifi2creq i2c; 41334c7070dbSScott Long 4134541d96aaSBrooks Davis err = copyin(ifr_data_get_ptr(ifr), &i2c, sizeof(i2c)); 41354c7070dbSScott Long if (err != 0) 41364c7070dbSScott Long break; 41374c7070dbSScott Long if (i2c.dev_addr != 0xA0 && i2c.dev_addr != 0xA2) { 41384c7070dbSScott Long err = EINVAL; 41394c7070dbSScott Long break; 41404c7070dbSScott Long } 41414c7070dbSScott Long if (i2c.len > sizeof(i2c.data)) { 41424c7070dbSScott Long err = EINVAL; 41434c7070dbSScott Long break; 41444c7070dbSScott Long } 41454c7070dbSScott Long 41464c7070dbSScott Long if ((err = IFDI_I2C_REQ(ctx, &i2c)) == 0) 4147541d96aaSBrooks Davis err = copyout(&i2c, ifr_data_get_ptr(ifr), 4148541d96aaSBrooks Davis sizeof(i2c)); 41494c7070dbSScott Long break; 41504c7070dbSScott Long } 41514c7070dbSScott Long case SIOCSIFCAP: 41524c7070dbSScott Long { 41530c919c23SStephen Hurd int mask, setmask, oldmask; 41544c7070dbSScott Long 41550c919c23SStephen Hurd oldmask = if_getcapenable(ifp); 41560c919c23SStephen Hurd mask = ifr->ifr_reqcap ^ oldmask; 41570c919c23SStephen Hurd mask &= ctx->ifc_softc_ctx.isc_capabilities; 41584c7070dbSScott Long setmask = 0; 41594c7070dbSScott Long #ifdef TCP_OFFLOAD 41604c7070dbSScott Long setmask |= mask & (IFCAP_TOE4|IFCAP_TOE6); 41614c7070dbSScott Long #endif 41624c7070dbSScott Long setmask |= (mask & IFCAP_FLAGS); 41630c919c23SStephen Hurd setmask |= (mask & IFCAP_WOL); 41644c7070dbSScott Long 41650c919c23SStephen Hurd /* 4166a42546dfSStephen Hurd * If any RX csum has changed, change all the ones that 4167a42546dfSStephen Hurd * are supported by the driver. 41680c919c23SStephen Hurd */ 4169a42546dfSStephen Hurd if (setmask & (IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6)) { 4170a42546dfSStephen Hurd setmask |= ctx->ifc_softc_ctx.isc_capabilities & 4171a42546dfSStephen Hurd (IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6); 4172a42546dfSStephen Hurd } 41730c919c23SStephen Hurd 41744c7070dbSScott Long /* 41754c7070dbSScott Long * want to ensure that traffic has stopped before we change any of the flags 41764c7070dbSScott Long */ 41774c7070dbSScott Long if (setmask) { 41784c7070dbSScott Long CTX_LOCK(ctx); 41794c7070dbSScott Long bits = if_getdrvflags(ifp); 41800c919c23SStephen Hurd if (bits & IFF_DRV_RUNNING && setmask & ~IFCAP_WOL) 41814c7070dbSScott Long iflib_stop(ctx); 41827b610b60SSean Bruno STATE_LOCK(ctx); 41834c7070dbSScott Long if_togglecapenable(ifp, setmask); 41847b610b60SSean Bruno STATE_UNLOCK(ctx); 41850c919c23SStephen Hurd if (bits & IFF_DRV_RUNNING && setmask & ~IFCAP_WOL) 41864c7070dbSScott Long iflib_init_locked(ctx); 41877b610b60SSean Bruno STATE_LOCK(ctx); 41884c7070dbSScott Long if_setdrvflags(ifp, bits); 41897b610b60SSean Bruno STATE_UNLOCK(ctx); 41904c7070dbSScott Long CTX_UNLOCK(ctx); 41914c7070dbSScott Long } 41920c919c23SStephen Hurd if_vlancap(ifp); 41934c7070dbSScott Long break; 41944c7070dbSScott Long } 41954c7070dbSScott Long case SIOCGPRIVATE_0: 41964c7070dbSScott Long case SIOCSDRVSPEC: 41974c7070dbSScott Long case SIOCGDRVSPEC: 41984c7070dbSScott Long CTX_LOCK(ctx); 41994c7070dbSScott Long err = IFDI_PRIV_IOCTL(ctx, command, data); 42004c7070dbSScott Long CTX_UNLOCK(ctx); 42014c7070dbSScott Long break; 42024c7070dbSScott Long default: 42034c7070dbSScott Long err = ether_ioctl(ifp, command, data); 42044c7070dbSScott Long break; 42054c7070dbSScott Long } 42064c7070dbSScott Long if (reinit) 42074c7070dbSScott Long iflib_if_init(ctx); 42084c7070dbSScott Long return (err); 42094c7070dbSScott Long } 42104c7070dbSScott Long 42114c7070dbSScott Long static uint64_t 42124c7070dbSScott Long iflib_if_get_counter(if_t ifp, ift_counter cnt) 42134c7070dbSScott Long { 42144c7070dbSScott Long if_ctx_t ctx = if_getsoftc(ifp); 42154c7070dbSScott Long 42164c7070dbSScott Long return (IFDI_GET_COUNTER(ctx, cnt)); 42174c7070dbSScott Long } 42184c7070dbSScott Long 42194c7070dbSScott Long /********************************************************************* 42204c7070dbSScott Long * 42214c7070dbSScott Long * OTHER FUNCTIONS EXPORTED TO THE STACK 42224c7070dbSScott Long * 42234c7070dbSScott Long **********************************************************************/ 42244c7070dbSScott Long 42254c7070dbSScott Long static void 42264c7070dbSScott Long iflib_vlan_register(void *arg, if_t ifp, uint16_t vtag) 42274c7070dbSScott Long { 42284c7070dbSScott Long if_ctx_t ctx = if_getsoftc(ifp); 42294c7070dbSScott Long 42304c7070dbSScott Long if ((void *)ctx != arg) 42314c7070dbSScott Long return; 42324c7070dbSScott Long 42334c7070dbSScott Long if ((vtag == 0) || (vtag > 4095)) 42344c7070dbSScott Long return; 42354c7070dbSScott Long 42364c7070dbSScott Long CTX_LOCK(ctx); 42374c7070dbSScott Long IFDI_VLAN_REGISTER(ctx, vtag); 42384c7070dbSScott Long /* Re-init to load the changes */ 42394c7070dbSScott Long if (if_getcapenable(ifp) & IFCAP_VLAN_HWFILTER) 424021e10b16SSean Bruno iflib_if_init_locked(ctx); 42414c7070dbSScott Long CTX_UNLOCK(ctx); 42424c7070dbSScott Long } 42434c7070dbSScott Long 42444c7070dbSScott Long static void 42454c7070dbSScott Long iflib_vlan_unregister(void *arg, if_t ifp, uint16_t vtag) 42464c7070dbSScott Long { 42474c7070dbSScott Long if_ctx_t ctx = if_getsoftc(ifp); 42484c7070dbSScott Long 42494c7070dbSScott Long if ((void *)ctx != arg) 42504c7070dbSScott Long return; 42514c7070dbSScott Long 42524c7070dbSScott Long if ((vtag == 0) || (vtag > 4095)) 42534c7070dbSScott Long return; 42544c7070dbSScott Long 42554c7070dbSScott Long CTX_LOCK(ctx); 42564c7070dbSScott Long IFDI_VLAN_UNREGISTER(ctx, vtag); 42574c7070dbSScott Long /* Re-init to load the changes */ 42584c7070dbSScott Long if (if_getcapenable(ifp) & IFCAP_VLAN_HWFILTER) 425921e10b16SSean Bruno iflib_if_init_locked(ctx); 42604c7070dbSScott Long CTX_UNLOCK(ctx); 42614c7070dbSScott Long } 42624c7070dbSScott Long 42634c7070dbSScott Long static void 42644c7070dbSScott Long iflib_led_func(void *arg, int onoff) 42654c7070dbSScott Long { 42664c7070dbSScott Long if_ctx_t ctx = arg; 42674c7070dbSScott Long 42684c7070dbSScott Long CTX_LOCK(ctx); 42694c7070dbSScott Long IFDI_LED_FUNC(ctx, onoff); 42704c7070dbSScott Long CTX_UNLOCK(ctx); 42714c7070dbSScott Long } 42724c7070dbSScott Long 42734c7070dbSScott Long /********************************************************************* 42744c7070dbSScott Long * 42754c7070dbSScott Long * BUS FUNCTION DEFINITIONS 42764c7070dbSScott Long * 42774c7070dbSScott Long **********************************************************************/ 42784c7070dbSScott Long 42794c7070dbSScott Long int 42804c7070dbSScott Long iflib_device_probe(device_t dev) 42814c7070dbSScott Long { 42824c7070dbSScott Long pci_vendor_info_t *ent; 42834c7070dbSScott Long 42844c7070dbSScott Long uint16_t pci_vendor_id, pci_device_id; 42854c7070dbSScott Long uint16_t pci_subvendor_id, pci_subdevice_id; 42864c7070dbSScott Long uint16_t pci_rev_id; 42874c7070dbSScott Long if_shared_ctx_t sctx; 42884c7070dbSScott Long 42894c7070dbSScott Long if ((sctx = DEVICE_REGISTER(dev)) == NULL || sctx->isc_magic != IFLIB_MAGIC) 42904c7070dbSScott Long return (ENOTSUP); 42914c7070dbSScott Long 42924c7070dbSScott Long pci_vendor_id = pci_get_vendor(dev); 42934c7070dbSScott Long pci_device_id = pci_get_device(dev); 42944c7070dbSScott Long pci_subvendor_id = pci_get_subvendor(dev); 42954c7070dbSScott Long pci_subdevice_id = pci_get_subdevice(dev); 42964c7070dbSScott Long pci_rev_id = pci_get_revid(dev); 42974c7070dbSScott Long if (sctx->isc_parse_devinfo != NULL) 42984c7070dbSScott Long sctx->isc_parse_devinfo(&pci_device_id, &pci_subvendor_id, &pci_subdevice_id, &pci_rev_id); 42994c7070dbSScott Long 43004c7070dbSScott Long ent = sctx->isc_vendor_info; 43014c7070dbSScott Long while (ent->pvi_vendor_id != 0) { 43024c7070dbSScott Long if (pci_vendor_id != ent->pvi_vendor_id) { 43034c7070dbSScott Long ent++; 43044c7070dbSScott Long continue; 43054c7070dbSScott Long } 43064c7070dbSScott Long if ((pci_device_id == ent->pvi_device_id) && 43074c7070dbSScott Long ((pci_subvendor_id == ent->pvi_subvendor_id) || 43084c7070dbSScott Long (ent->pvi_subvendor_id == 0)) && 43094c7070dbSScott Long ((pci_subdevice_id == ent->pvi_subdevice_id) || 43104c7070dbSScott Long (ent->pvi_subdevice_id == 0)) && 43114c7070dbSScott Long ((pci_rev_id == ent->pvi_rev_id) || 43124c7070dbSScott Long (ent->pvi_rev_id == 0))) { 43134c7070dbSScott Long 43144c7070dbSScott Long device_set_desc_copy(dev, ent->pvi_name); 43154c7070dbSScott Long /* this needs to be changed to zero if the bus probing code 43164c7070dbSScott Long * ever stops re-probing on best match because the sctx 43174c7070dbSScott Long * may have its values over written by register calls 43184c7070dbSScott Long * in subsequent probes 43194c7070dbSScott Long */ 43204c7070dbSScott Long return (BUS_PROBE_DEFAULT); 43214c7070dbSScott Long } 43224c7070dbSScott Long ent++; 43234c7070dbSScott Long } 43244c7070dbSScott Long return (ENXIO); 43254c7070dbSScott Long } 43264c7070dbSScott Long 432709f6ff4fSMatt Macy static void 432809f6ff4fSMatt Macy iflib_reset_qvalues(if_ctx_t ctx) 43294c7070dbSScott Long { 433009f6ff4fSMatt Macy if_softc_ctx_t scctx = &ctx->ifc_softc_ctx; 433109f6ff4fSMatt Macy if_shared_ctx_t sctx = ctx->ifc_sctx; 433209f6ff4fSMatt Macy device_t dev = ctx->ifc_dev; 433346d0f824SMatt Macy int i; 43344c7070dbSScott Long 433509f6ff4fSMatt Macy scctx->isc_txrx_budget_bytes_max = IFLIB_MAX_TX_BYTES; 433609f6ff4fSMatt Macy scctx->isc_tx_qdepth = IFLIB_DEFAULT_TX_QDEPTH; 433723ac9029SStephen Hurd /* 433823ac9029SStephen Hurd * XXX sanity check that ntxd & nrxd are a power of 2 433923ac9029SStephen Hurd */ 434023ac9029SStephen Hurd if (ctx->ifc_sysctl_ntxqs != 0) 434123ac9029SStephen Hurd scctx->isc_ntxqsets = ctx->ifc_sysctl_ntxqs; 434223ac9029SStephen Hurd if (ctx->ifc_sysctl_nrxqs != 0) 434323ac9029SStephen Hurd scctx->isc_nrxqsets = ctx->ifc_sysctl_nrxqs; 434423ac9029SStephen Hurd 434523ac9029SStephen Hurd for (i = 0; i < sctx->isc_ntxqs; i++) { 434623ac9029SStephen Hurd if (ctx->ifc_sysctl_ntxds[i] != 0) 434723ac9029SStephen Hurd scctx->isc_ntxd[i] = ctx->ifc_sysctl_ntxds[i]; 434823ac9029SStephen Hurd else 434923ac9029SStephen Hurd scctx->isc_ntxd[i] = sctx->isc_ntxd_default[i]; 435023ac9029SStephen Hurd } 435123ac9029SStephen Hurd 435223ac9029SStephen Hurd for (i = 0; i < sctx->isc_nrxqs; i++) { 435323ac9029SStephen Hurd if (ctx->ifc_sysctl_nrxds[i] != 0) 435423ac9029SStephen Hurd scctx->isc_nrxd[i] = ctx->ifc_sysctl_nrxds[i]; 435523ac9029SStephen Hurd else 435623ac9029SStephen Hurd scctx->isc_nrxd[i] = sctx->isc_nrxd_default[i]; 435723ac9029SStephen Hurd } 435823ac9029SStephen Hurd 435923ac9029SStephen Hurd for (i = 0; i < sctx->isc_nrxqs; i++) { 436023ac9029SStephen Hurd if (scctx->isc_nrxd[i] < sctx->isc_nrxd_min[i]) { 436123ac9029SStephen Hurd device_printf(dev, "nrxd%d: %d less than nrxd_min %d - resetting to min\n", 436223ac9029SStephen Hurd i, scctx->isc_nrxd[i], sctx->isc_nrxd_min[i]); 436323ac9029SStephen Hurd scctx->isc_nrxd[i] = sctx->isc_nrxd_min[i]; 436423ac9029SStephen Hurd } 436523ac9029SStephen Hurd if (scctx->isc_nrxd[i] > sctx->isc_nrxd_max[i]) { 436623ac9029SStephen Hurd device_printf(dev, "nrxd%d: %d greater than nrxd_max %d - resetting to max\n", 436723ac9029SStephen Hurd i, scctx->isc_nrxd[i], sctx->isc_nrxd_max[i]); 436823ac9029SStephen Hurd scctx->isc_nrxd[i] = sctx->isc_nrxd_max[i]; 436923ac9029SStephen Hurd } 437023ac9029SStephen Hurd } 437123ac9029SStephen Hurd 437223ac9029SStephen Hurd for (i = 0; i < sctx->isc_ntxqs; i++) { 437323ac9029SStephen Hurd if (scctx->isc_ntxd[i] < sctx->isc_ntxd_min[i]) { 437423ac9029SStephen Hurd device_printf(dev, "ntxd%d: %d less than ntxd_min %d - resetting to min\n", 437523ac9029SStephen Hurd i, scctx->isc_ntxd[i], sctx->isc_ntxd_min[i]); 437623ac9029SStephen Hurd scctx->isc_ntxd[i] = sctx->isc_ntxd_min[i]; 437723ac9029SStephen Hurd } 437823ac9029SStephen Hurd if (scctx->isc_ntxd[i] > sctx->isc_ntxd_max[i]) { 437923ac9029SStephen Hurd device_printf(dev, "ntxd%d: %d greater than ntxd_max %d - resetting to max\n", 438023ac9029SStephen Hurd i, scctx->isc_ntxd[i], sctx->isc_ntxd_max[i]); 438123ac9029SStephen Hurd scctx->isc_ntxd[i] = sctx->isc_ntxd_max[i]; 438223ac9029SStephen Hurd } 438323ac9029SStephen Hurd } 438409f6ff4fSMatt Macy } 4385ab2e3f79SStephen Hurd 438609f6ff4fSMatt Macy int 438709f6ff4fSMatt Macy iflib_device_register(device_t dev, void *sc, if_shared_ctx_t sctx, if_ctx_t *ctxp) 438809f6ff4fSMatt Macy { 438909f6ff4fSMatt Macy int err, rid, msix; 439009f6ff4fSMatt Macy if_ctx_t ctx; 439109f6ff4fSMatt Macy if_t ifp; 439209f6ff4fSMatt Macy if_softc_ctx_t scctx; 439309f6ff4fSMatt Macy int i; 439409f6ff4fSMatt Macy uint16_t main_txq; 439509f6ff4fSMatt Macy uint16_t main_rxq; 439609f6ff4fSMatt Macy 439709f6ff4fSMatt Macy 439809f6ff4fSMatt Macy ctx = malloc(sizeof(* ctx), M_IFLIB, M_WAITOK|M_ZERO); 439909f6ff4fSMatt Macy 440009f6ff4fSMatt Macy if (sc == NULL) { 440109f6ff4fSMatt Macy sc = malloc(sctx->isc_driver->size, M_IFLIB, M_WAITOK|M_ZERO); 440209f6ff4fSMatt Macy device_set_softc(dev, ctx); 440309f6ff4fSMatt Macy ctx->ifc_flags |= IFC_SC_ALLOCATED; 440409f6ff4fSMatt Macy } 440509f6ff4fSMatt Macy 440609f6ff4fSMatt Macy ctx->ifc_sctx = sctx; 440709f6ff4fSMatt Macy ctx->ifc_dev = dev; 440809f6ff4fSMatt Macy ctx->ifc_softc = sc; 440909f6ff4fSMatt Macy 441009f6ff4fSMatt Macy if ((err = iflib_register(ctx)) != 0) { 441109f6ff4fSMatt Macy device_printf(dev, "iflib_register failed %d\n", err); 44127f3eb9daSPatrick Kelsey goto fail_ctx_free; 441309f6ff4fSMatt Macy } 441409f6ff4fSMatt Macy iflib_add_device_sysctl_pre(ctx); 441509f6ff4fSMatt Macy 441609f6ff4fSMatt Macy scctx = &ctx->ifc_softc_ctx; 441709f6ff4fSMatt Macy ifp = ctx->ifc_ifp; 441809f6ff4fSMatt Macy 441909f6ff4fSMatt Macy iflib_reset_qvalues(ctx); 4420aa8a24d3SStephen Hurd CTX_LOCK(ctx); 4421ab2e3f79SStephen Hurd if ((err = IFDI_ATTACH_PRE(ctx)) != 0) { 44224c7070dbSScott Long device_printf(dev, "IFDI_ATTACH_PRE failed %d\n", err); 44237f3eb9daSPatrick Kelsey goto fail_unlock; 44244c7070dbSScott Long } 44251248952aSSean Bruno _iflib_pre_assert(scctx); 44261248952aSSean Bruno ctx->ifc_txrx = *scctx->isc_txrx; 44271248952aSSean Bruno 44281248952aSSean Bruno #ifdef INVARIANTS 44297f87c040SMarius Strobl MPASS(scctx->isc_capabilities); 44307f87c040SMarius Strobl if (scctx->isc_capabilities & IFCAP_TXCSUM) 44311248952aSSean Bruno MPASS(scctx->isc_tx_csum_flags); 44321248952aSSean Bruno #endif 44331248952aSSean Bruno 44347f87c040SMarius Strobl if_setcapabilities(ifp, scctx->isc_capabilities | IFCAP_HWSTATS); 443518a660b3SSean Bruno if_setcapenable(ifp, scctx->isc_capenable | IFCAP_HWSTATS); 44361248952aSSean Bruno 44371248952aSSean Bruno if (scctx->isc_ntxqsets == 0 || (scctx->isc_ntxqsets_max && scctx->isc_ntxqsets_max < scctx->isc_ntxqsets)) 44381248952aSSean Bruno scctx->isc_ntxqsets = scctx->isc_ntxqsets_max; 44391248952aSSean Bruno if (scctx->isc_nrxqsets == 0 || (scctx->isc_nrxqsets_max && scctx->isc_nrxqsets_max < scctx->isc_nrxqsets)) 44401248952aSSean Bruno scctx->isc_nrxqsets = scctx->isc_nrxqsets_max; 444123ac9029SStephen Hurd 444295246abbSSean Bruno main_txq = (sctx->isc_flags & IFLIB_HAS_TXCQ) ? 1 : 0; 444395246abbSSean Bruno main_rxq = (sctx->isc_flags & IFLIB_HAS_RXCQ) ? 1 : 0; 444423ac9029SStephen Hurd 444523ac9029SStephen Hurd /* XXX change for per-queue sizes */ 4446b97de13aSMarius Strobl device_printf(dev, "Using %d tx descriptors and %d rx descriptors\n", 444723ac9029SStephen Hurd scctx->isc_ntxd[main_txq], scctx->isc_nrxd[main_rxq]); 444823ac9029SStephen Hurd for (i = 0; i < sctx->isc_nrxqs; i++) { 444923ac9029SStephen Hurd if (!powerof2(scctx->isc_nrxd[i])) { 445023ac9029SStephen Hurd /* round down instead? */ 445123ac9029SStephen Hurd device_printf(dev, "# rx descriptors must be a power of 2\n"); 445223ac9029SStephen Hurd err = EINVAL; 44537f3eb9daSPatrick Kelsey goto fail_iflib_detach; 445423ac9029SStephen Hurd } 445523ac9029SStephen Hurd } 445623ac9029SStephen Hurd for (i = 0; i < sctx->isc_ntxqs; i++) { 445723ac9029SStephen Hurd if (!powerof2(scctx->isc_ntxd[i])) { 445823ac9029SStephen Hurd device_printf(dev, 445923ac9029SStephen Hurd "# tx descriptors must be a power of 2"); 446023ac9029SStephen Hurd err = EINVAL; 44617f3eb9daSPatrick Kelsey goto fail_iflib_detach; 446223ac9029SStephen Hurd } 446323ac9029SStephen Hurd } 446423ac9029SStephen Hurd 446523ac9029SStephen Hurd if (scctx->isc_tx_nsegments > scctx->isc_ntxd[main_txq] / 446623ac9029SStephen Hurd MAX_SINGLE_PACKET_FRACTION) 446723ac9029SStephen Hurd scctx->isc_tx_nsegments = max(1, scctx->isc_ntxd[main_txq] / 446823ac9029SStephen Hurd MAX_SINGLE_PACKET_FRACTION); 446923ac9029SStephen Hurd if (scctx->isc_tx_tso_segments_max > scctx->isc_ntxd[main_txq] / 447023ac9029SStephen Hurd MAX_SINGLE_PACKET_FRACTION) 447123ac9029SStephen Hurd scctx->isc_tx_tso_segments_max = max(1, 447223ac9029SStephen Hurd scctx->isc_ntxd[main_txq] / MAX_SINGLE_PACKET_FRACTION); 44734c7070dbSScott Long 44744c7070dbSScott Long /* TSO parameters - dig these out of the data sheet - simply correspond to tag setup */ 44757f87c040SMarius Strobl if (if_getcapabilities(ifp) & IFCAP_TSO) { 44767f87c040SMarius Strobl /* 44777f87c040SMarius Strobl * The stack can't handle a TSO size larger than IP_MAXPACKET, 44787f87c040SMarius Strobl * but some MACs do. 44797f87c040SMarius Strobl */ 44807f87c040SMarius Strobl if_sethwtsomax(ifp, min(scctx->isc_tx_tso_size_max, 44817f87c040SMarius Strobl IP_MAXPACKET)); 44827f87c040SMarius Strobl /* 44837f87c040SMarius Strobl * Take maximum number of m_pullup(9)'s in iflib_parse_header() 44847f87c040SMarius Strobl * into account. In the worst case, each of these calls will 44857f87c040SMarius Strobl * add another mbuf and, thus, the requirement for another DMA 44867f87c040SMarius Strobl * segment. So for best performance, it doesn't make sense to 44877f87c040SMarius Strobl * advertize a maximum of TSO segments that typically will 44887f87c040SMarius Strobl * require defragmentation in iflib_encap(). 44897f87c040SMarius Strobl */ 44907f87c040SMarius Strobl if_sethwtsomaxsegcount(ifp, scctx->isc_tx_tso_segments_max - 3); 44917f87c040SMarius Strobl if_sethwtsomaxsegsize(ifp, scctx->isc_tx_tso_segsize_max); 44927f87c040SMarius Strobl } 44934c7070dbSScott Long if (scctx->isc_rss_table_size == 0) 44944c7070dbSScott Long scctx->isc_rss_table_size = 64; 449523ac9029SStephen Hurd scctx->isc_rss_table_mask = scctx->isc_rss_table_size-1; 4496da69b8f9SSean Bruno 4497da69b8f9SSean Bruno GROUPTASK_INIT(&ctx->ifc_admin_task, 0, _task_fn_admin, ctx); 4498da69b8f9SSean Bruno /* XXX format name */ 4499f855ec81SMarius Strobl taskqgroup_attach(qgroup_if_config_tqg, &ctx->ifc_admin_task, ctx, 4500f855ec81SMarius Strobl NULL, NULL, "admin"); 4501e516b535SStephen Hurd 4502772593dbSStephen Hurd /* Set up cpu set. If it fails, use the set of all CPUs. */ 4503e516b535SStephen Hurd if (bus_get_cpus(dev, INTR_CPUS, sizeof(ctx->ifc_cpus), &ctx->ifc_cpus) != 0) { 4504e516b535SStephen Hurd device_printf(dev, "Unable to fetch CPU list\n"); 4505e516b535SStephen Hurd CPU_COPY(&all_cpus, &ctx->ifc_cpus); 4506e516b535SStephen Hurd } 4507e516b535SStephen Hurd MPASS(CPU_COUNT(&ctx->ifc_cpus) > 0); 4508e516b535SStephen Hurd 45094c7070dbSScott Long /* 4510b97de13aSMarius Strobl ** Now set up MSI or MSI-X, should return us the number of supported 4511b97de13aSMarius Strobl ** vectors (will be 1 for a legacy interrupt and MSI). 45124c7070dbSScott Long */ 45134c7070dbSScott Long if (sctx->isc_flags & IFLIB_SKIP_MSIX) { 45144c7070dbSScott Long msix = scctx->isc_vectors; 45154c7070dbSScott Long } else if (scctx->isc_msix_bar != 0) 4516f7ae9a84SSean Bruno /* 4517f7ae9a84SSean Bruno * The simple fact that isc_msix_bar is not 0 does not mean we 4518f7ae9a84SSean Bruno * we have a good value there that is known to work. 4519f7ae9a84SSean Bruno */ 45204c7070dbSScott Long msix = iflib_msix_init(ctx); 45214c7070dbSScott Long else { 45224c7070dbSScott Long scctx->isc_vectors = 1; 45234c7070dbSScott Long scctx->isc_ntxqsets = 1; 45244c7070dbSScott Long scctx->isc_nrxqsets = 1; 45254c7070dbSScott Long scctx->isc_intr = IFLIB_INTR_LEGACY; 45264c7070dbSScott Long msix = 0; 45274c7070dbSScott Long } 45284c7070dbSScott Long /* Get memory for the station queues */ 45294c7070dbSScott Long if ((err = iflib_queues_alloc(ctx))) { 45304c7070dbSScott Long device_printf(dev, "Unable to allocate queue memory\n"); 45317f3eb9daSPatrick Kelsey goto fail_intr_free; 45324c7070dbSScott Long } 45334c7070dbSScott Long 4534ac88e6daSStephen Hurd if ((err = iflib_qset_structures_setup(ctx))) 45354c7070dbSScott Long goto fail_queues; 453669b7fc3eSSean Bruno 4537bd84f700SSean Bruno /* 4538bd84f700SSean Bruno * Group taskqueues aren't properly set up until SMP is started, 4539bd84f700SSean Bruno * so we disable interrupts until we can handle them post 4540bd84f700SSean Bruno * SI_SUB_SMP. 4541bd84f700SSean Bruno * 4542bd84f700SSean Bruno * XXX: disabling interrupts doesn't actually work, at least for 4543bd84f700SSean Bruno * the non-MSI case. When they occur before SI_SUB_SMP completes, 4544bd84f700SSean Bruno * we do null handling and depend on this not causing too large an 4545bd84f700SSean Bruno * interrupt storm. 4546bd84f700SSean Bruno */ 45471248952aSSean Bruno IFDI_INTR_DISABLE(ctx); 45484c7070dbSScott Long if (msix > 1 && (err = IFDI_MSIX_INTR_ASSIGN(ctx, msix)) != 0) { 45494c7070dbSScott Long device_printf(dev, "IFDI_MSIX_INTR_ASSIGN failed %d\n", err); 45507f3eb9daSPatrick Kelsey goto fail_queues; 45514c7070dbSScott Long } 45524c7070dbSScott Long if (msix <= 1) { 45534c7070dbSScott Long rid = 0; 45544c7070dbSScott Long if (scctx->isc_intr == IFLIB_INTR_MSI) { 45554c7070dbSScott Long MPASS(msix == 1); 45564c7070dbSScott Long rid = 1; 45574c7070dbSScott Long } 455823ac9029SStephen Hurd if ((err = iflib_legacy_setup(ctx, ctx->isc_legacy_intr, ctx->ifc_softc, &rid, "irq0")) != 0) { 45594c7070dbSScott Long device_printf(dev, "iflib_legacy_setup failed %d\n", err); 45607f3eb9daSPatrick Kelsey goto fail_queues; 45614c7070dbSScott Long } 45624c7070dbSScott Long } 45637f87c040SMarius Strobl 45644c7070dbSScott Long ether_ifattach(ctx->ifc_ifp, ctx->ifc_mac); 45657f87c040SMarius Strobl 4566ab2e3f79SStephen Hurd if ((err = IFDI_ATTACH_POST(ctx)) != 0) { 45674c7070dbSScott Long device_printf(dev, "IFDI_ATTACH_POST failed %d\n", err); 45684c7070dbSScott Long goto fail_detach; 45694c7070dbSScott Long } 45707f87c040SMarius Strobl 45717f87c040SMarius Strobl /* 45727f87c040SMarius Strobl * Tell the upper layer(s) if IFCAP_VLAN_MTU is supported. 45737f87c040SMarius Strobl * This must appear after the call to ether_ifattach() because 45747f87c040SMarius Strobl * ether_ifattach() sets if_hdrlen to the default value. 45757f87c040SMarius Strobl */ 45767f87c040SMarius Strobl if (if_getcapabilities(ifp) & IFCAP_VLAN_MTU) 45777f87c040SMarius Strobl if_setifheaderlen(ifp, sizeof(struct ether_vlan_header)); 45787f87c040SMarius Strobl 45794c7070dbSScott Long if ((err = iflib_netmap_attach(ctx))) { 45804c7070dbSScott Long device_printf(ctx->ifc_dev, "netmap attach failed: %d\n", err); 45814c7070dbSScott Long goto fail_detach; 45824c7070dbSScott Long } 45834c7070dbSScott Long *ctxp = ctx; 45844c7070dbSScott Long 458594618825SMark Johnston NETDUMP_SET(ctx->ifc_ifp, iflib); 458694618825SMark Johnston 458723ac9029SStephen Hurd if_setgetcounterfn(ctx->ifc_ifp, iflib_if_get_counter); 45884c7070dbSScott Long iflib_add_device_sysctl_post(ctx); 45894ecb427aSSean Bruno ctx->ifc_flags |= IFC_INIT_DONE; 4590aa8a24d3SStephen Hurd CTX_UNLOCK(ctx); 45914c7070dbSScott Long return (0); 459277c1fcecSEric Joyner 45934c7070dbSScott Long fail_detach: 45944c7070dbSScott Long ether_ifdetach(ctx->ifc_ifp); 45954c7070dbSScott Long fail_intr_free: 45967f3eb9daSPatrick Kelsey iflib_free_intr_mem(ctx); 45974c7070dbSScott Long fail_queues: 45986108c013SStephen Hurd iflib_tx_structures_free(ctx); 45996108c013SStephen Hurd iflib_rx_structures_free(ctx); 46007f3eb9daSPatrick Kelsey fail_iflib_detach: 46014c7070dbSScott Long IFDI_DETACH(ctx); 46027f3eb9daSPatrick Kelsey fail_unlock: 4603aa8a24d3SStephen Hurd CTX_UNLOCK(ctx); 46047f3eb9daSPatrick Kelsey fail_ctx_free: 46057f3eb9daSPatrick Kelsey if (ctx->ifc_flags & IFC_SC_ALLOCATED) 46067f3eb9daSPatrick Kelsey free(ctx->ifc_softc, M_IFLIB); 46077f3eb9daSPatrick Kelsey free(ctx, M_IFLIB); 46084c7070dbSScott Long return (err); 46094c7070dbSScott Long } 46104c7070dbSScott Long 46114c7070dbSScott Long int 461209f6ff4fSMatt Macy iflib_pseudo_register(device_t dev, if_shared_ctx_t sctx, if_ctx_t *ctxp, 461309f6ff4fSMatt Macy struct iflib_cloneattach_ctx *clctx) 461409f6ff4fSMatt Macy { 461509f6ff4fSMatt Macy int err; 461609f6ff4fSMatt Macy if_ctx_t ctx; 461709f6ff4fSMatt Macy if_t ifp; 461809f6ff4fSMatt Macy if_softc_ctx_t scctx; 461909f6ff4fSMatt Macy int i; 462009f6ff4fSMatt Macy void *sc; 462109f6ff4fSMatt Macy uint16_t main_txq; 462209f6ff4fSMatt Macy uint16_t main_rxq; 462309f6ff4fSMatt Macy 462409f6ff4fSMatt Macy ctx = malloc(sizeof(*ctx), M_IFLIB, M_WAITOK|M_ZERO); 462509f6ff4fSMatt Macy sc = malloc(sctx->isc_driver->size, M_IFLIB, M_WAITOK|M_ZERO); 462609f6ff4fSMatt Macy ctx->ifc_flags |= IFC_SC_ALLOCATED; 462709f6ff4fSMatt Macy if (sctx->isc_flags & (IFLIB_PSEUDO|IFLIB_VIRTUAL)) 462809f6ff4fSMatt Macy ctx->ifc_flags |= IFC_PSEUDO; 462909f6ff4fSMatt Macy 463009f6ff4fSMatt Macy ctx->ifc_sctx = sctx; 463109f6ff4fSMatt Macy ctx->ifc_softc = sc; 463209f6ff4fSMatt Macy ctx->ifc_dev = dev; 463309f6ff4fSMatt Macy 463409f6ff4fSMatt Macy if ((err = iflib_register(ctx)) != 0) { 463509f6ff4fSMatt Macy device_printf(dev, "%s: iflib_register failed %d\n", __func__, err); 46367f3eb9daSPatrick Kelsey goto fail_ctx_free; 463709f6ff4fSMatt Macy } 463809f6ff4fSMatt Macy iflib_add_device_sysctl_pre(ctx); 463909f6ff4fSMatt Macy 464009f6ff4fSMatt Macy scctx = &ctx->ifc_softc_ctx; 464109f6ff4fSMatt Macy ifp = ctx->ifc_ifp; 464209f6ff4fSMatt Macy 464309f6ff4fSMatt Macy /* 464409f6ff4fSMatt Macy * XXX sanity check that ntxd & nrxd are a power of 2 464509f6ff4fSMatt Macy */ 464609f6ff4fSMatt Macy iflib_reset_qvalues(ctx); 464709f6ff4fSMatt Macy 464809f6ff4fSMatt Macy if ((err = IFDI_ATTACH_PRE(ctx)) != 0) { 464909f6ff4fSMatt Macy device_printf(dev, "IFDI_ATTACH_PRE failed %d\n", err); 46507f3eb9daSPatrick Kelsey goto fail_ctx_free; 465109f6ff4fSMatt Macy } 465209f6ff4fSMatt Macy if (sctx->isc_flags & IFLIB_GEN_MAC) 465309f6ff4fSMatt Macy iflib_gen_mac(ctx); 465409f6ff4fSMatt Macy if ((err = IFDI_CLONEATTACH(ctx, clctx->cc_ifc, clctx->cc_name, 465509f6ff4fSMatt Macy clctx->cc_params)) != 0) { 465609f6ff4fSMatt Macy device_printf(dev, "IFDI_CLONEATTACH failed %d\n", err); 46577f3eb9daSPatrick Kelsey goto fail_ctx_free; 465809f6ff4fSMatt Macy } 465909f6ff4fSMatt Macy ifmedia_add(&ctx->ifc_media, IFM_ETHER | IFM_1000_T | IFM_FDX, 0, NULL); 466009f6ff4fSMatt Macy ifmedia_add(&ctx->ifc_media, IFM_ETHER | IFM_AUTO, 0, NULL); 466109f6ff4fSMatt Macy ifmedia_set(&ctx->ifc_media, IFM_ETHER | IFM_AUTO); 466209f6ff4fSMatt Macy 466309f6ff4fSMatt Macy #ifdef INVARIANTS 46647f87c040SMarius Strobl MPASS(scctx->isc_capabilities); 46657f87c040SMarius Strobl if (scctx->isc_capabilities & IFCAP_TXCSUM) 466609f6ff4fSMatt Macy MPASS(scctx->isc_tx_csum_flags); 466709f6ff4fSMatt Macy #endif 466809f6ff4fSMatt Macy 46697f87c040SMarius Strobl if_setcapabilities(ifp, scctx->isc_capabilities | IFCAP_HWSTATS | IFCAP_LINKSTATE); 467009f6ff4fSMatt Macy if_setcapenable(ifp, scctx->isc_capenable | IFCAP_HWSTATS | IFCAP_LINKSTATE); 467109f6ff4fSMatt Macy 467209f6ff4fSMatt Macy ifp->if_flags |= IFF_NOGROUP; 467309f6ff4fSMatt Macy if (sctx->isc_flags & IFLIB_PSEUDO) { 467409f6ff4fSMatt Macy ether_ifattach(ctx->ifc_ifp, ctx->ifc_mac); 467509f6ff4fSMatt Macy 467609f6ff4fSMatt Macy if ((err = IFDI_ATTACH_POST(ctx)) != 0) { 467709f6ff4fSMatt Macy device_printf(dev, "IFDI_ATTACH_POST failed %d\n", err); 467809f6ff4fSMatt Macy goto fail_detach; 467909f6ff4fSMatt Macy } 468009f6ff4fSMatt Macy *ctxp = ctx; 468109f6ff4fSMatt Macy 46827f87c040SMarius Strobl /* 46837f87c040SMarius Strobl * Tell the upper layer(s) if IFCAP_VLAN_MTU is supported. 46847f87c040SMarius Strobl * This must appear after the call to ether_ifattach() because 46857f87c040SMarius Strobl * ether_ifattach() sets if_hdrlen to the default value. 46867f87c040SMarius Strobl */ 46877f87c040SMarius Strobl if (if_getcapabilities(ifp) & IFCAP_VLAN_MTU) 46887f87c040SMarius Strobl if_setifheaderlen(ifp, 46897f87c040SMarius Strobl sizeof(struct ether_vlan_header)); 46907f87c040SMarius Strobl 469109f6ff4fSMatt Macy if_setgetcounterfn(ctx->ifc_ifp, iflib_if_get_counter); 469209f6ff4fSMatt Macy iflib_add_device_sysctl_post(ctx); 469309f6ff4fSMatt Macy ctx->ifc_flags |= IFC_INIT_DONE; 469409f6ff4fSMatt Macy return (0); 469509f6ff4fSMatt Macy } 469609f6ff4fSMatt Macy _iflib_pre_assert(scctx); 469709f6ff4fSMatt Macy ctx->ifc_txrx = *scctx->isc_txrx; 469809f6ff4fSMatt Macy 469909f6ff4fSMatt Macy if (scctx->isc_ntxqsets == 0 || (scctx->isc_ntxqsets_max && scctx->isc_ntxqsets_max < scctx->isc_ntxqsets)) 470009f6ff4fSMatt Macy scctx->isc_ntxqsets = scctx->isc_ntxqsets_max; 470109f6ff4fSMatt Macy if (scctx->isc_nrxqsets == 0 || (scctx->isc_nrxqsets_max && scctx->isc_nrxqsets_max < scctx->isc_nrxqsets)) 470209f6ff4fSMatt Macy scctx->isc_nrxqsets = scctx->isc_nrxqsets_max; 470309f6ff4fSMatt Macy 470409f6ff4fSMatt Macy main_txq = (sctx->isc_flags & IFLIB_HAS_TXCQ) ? 1 : 0; 470509f6ff4fSMatt Macy main_rxq = (sctx->isc_flags & IFLIB_HAS_RXCQ) ? 1 : 0; 470609f6ff4fSMatt Macy 470709f6ff4fSMatt Macy /* XXX change for per-queue sizes */ 4708b97de13aSMarius Strobl device_printf(dev, "Using %d tx descriptors and %d rx descriptors\n", 470909f6ff4fSMatt Macy scctx->isc_ntxd[main_txq], scctx->isc_nrxd[main_rxq]); 471009f6ff4fSMatt Macy for (i = 0; i < sctx->isc_nrxqs; i++) { 471109f6ff4fSMatt Macy if (!powerof2(scctx->isc_nrxd[i])) { 471209f6ff4fSMatt Macy /* round down instead? */ 471309f6ff4fSMatt Macy device_printf(dev, "# rx descriptors must be a power of 2\n"); 471409f6ff4fSMatt Macy err = EINVAL; 47157f3eb9daSPatrick Kelsey goto fail_iflib_detach; 471609f6ff4fSMatt Macy } 471709f6ff4fSMatt Macy } 471809f6ff4fSMatt Macy for (i = 0; i < sctx->isc_ntxqs; i++) { 471909f6ff4fSMatt Macy if (!powerof2(scctx->isc_ntxd[i])) { 472009f6ff4fSMatt Macy device_printf(dev, 472109f6ff4fSMatt Macy "# tx descriptors must be a power of 2"); 472209f6ff4fSMatt Macy err = EINVAL; 47237f3eb9daSPatrick Kelsey goto fail_iflib_detach; 472409f6ff4fSMatt Macy } 472509f6ff4fSMatt Macy } 472609f6ff4fSMatt Macy 472709f6ff4fSMatt Macy if (scctx->isc_tx_nsegments > scctx->isc_ntxd[main_txq] / 472809f6ff4fSMatt Macy MAX_SINGLE_PACKET_FRACTION) 472909f6ff4fSMatt Macy scctx->isc_tx_nsegments = max(1, scctx->isc_ntxd[main_txq] / 473009f6ff4fSMatt Macy MAX_SINGLE_PACKET_FRACTION); 473109f6ff4fSMatt Macy if (scctx->isc_tx_tso_segments_max > scctx->isc_ntxd[main_txq] / 473209f6ff4fSMatt Macy MAX_SINGLE_PACKET_FRACTION) 473309f6ff4fSMatt Macy scctx->isc_tx_tso_segments_max = max(1, 473409f6ff4fSMatt Macy scctx->isc_ntxd[main_txq] / MAX_SINGLE_PACKET_FRACTION); 473509f6ff4fSMatt Macy 473609f6ff4fSMatt Macy /* TSO parameters - dig these out of the data sheet - simply correspond to tag setup */ 47377f87c040SMarius Strobl if (if_getcapabilities(ifp) & IFCAP_TSO) { 47387f87c040SMarius Strobl /* 47397f87c040SMarius Strobl * The stack can't handle a TSO size larger than IP_MAXPACKET, 47407f87c040SMarius Strobl * but some MACs do. 47417f87c040SMarius Strobl */ 47427f87c040SMarius Strobl if_sethwtsomax(ifp, min(scctx->isc_tx_tso_size_max, 47437f87c040SMarius Strobl IP_MAXPACKET)); 47447f87c040SMarius Strobl /* 47457f87c040SMarius Strobl * Take maximum number of m_pullup(9)'s in iflib_parse_header() 47467f87c040SMarius Strobl * into account. In the worst case, each of these calls will 47477f87c040SMarius Strobl * add another mbuf and, thus, the requirement for another DMA 47487f87c040SMarius Strobl * segment. So for best performance, it doesn't make sense to 47497f87c040SMarius Strobl * advertize a maximum of TSO segments that typically will 47507f87c040SMarius Strobl * require defragmentation in iflib_encap(). 47517f87c040SMarius Strobl */ 47527f87c040SMarius Strobl if_sethwtsomaxsegcount(ifp, scctx->isc_tx_tso_segments_max - 3); 47537f87c040SMarius Strobl if_sethwtsomaxsegsize(ifp, scctx->isc_tx_tso_segsize_max); 47547f87c040SMarius Strobl } 475509f6ff4fSMatt Macy if (scctx->isc_rss_table_size == 0) 475609f6ff4fSMatt Macy scctx->isc_rss_table_size = 64; 475709f6ff4fSMatt Macy scctx->isc_rss_table_mask = scctx->isc_rss_table_size-1; 475809f6ff4fSMatt Macy 475909f6ff4fSMatt Macy GROUPTASK_INIT(&ctx->ifc_admin_task, 0, _task_fn_admin, ctx); 476009f6ff4fSMatt Macy /* XXX format name */ 4761f855ec81SMarius Strobl taskqgroup_attach(qgroup_if_config_tqg, &ctx->ifc_admin_task, ctx, 4762f855ec81SMarius Strobl NULL, NULL, "admin"); 476309f6ff4fSMatt Macy 476409f6ff4fSMatt Macy /* XXX --- can support > 1 -- but keep it simple for now */ 476509f6ff4fSMatt Macy scctx->isc_intr = IFLIB_INTR_LEGACY; 476609f6ff4fSMatt Macy 476709f6ff4fSMatt Macy /* Get memory for the station queues */ 476809f6ff4fSMatt Macy if ((err = iflib_queues_alloc(ctx))) { 476909f6ff4fSMatt Macy device_printf(dev, "Unable to allocate queue memory\n"); 47707f3eb9daSPatrick Kelsey goto fail_iflib_detach; 477109f6ff4fSMatt Macy } 477209f6ff4fSMatt Macy 477309f6ff4fSMatt Macy if ((err = iflib_qset_structures_setup(ctx))) { 477409f6ff4fSMatt Macy device_printf(dev, "qset structure setup failed %d\n", err); 477509f6ff4fSMatt Macy goto fail_queues; 477609f6ff4fSMatt Macy } 47777f87c040SMarius Strobl 477809f6ff4fSMatt Macy /* 477909f6ff4fSMatt Macy * XXX What if anything do we want to do about interrupts? 478009f6ff4fSMatt Macy */ 478109f6ff4fSMatt Macy ether_ifattach(ctx->ifc_ifp, ctx->ifc_mac); 478209f6ff4fSMatt Macy if ((err = IFDI_ATTACH_POST(ctx)) != 0) { 478309f6ff4fSMatt Macy device_printf(dev, "IFDI_ATTACH_POST failed %d\n", err); 478409f6ff4fSMatt Macy goto fail_detach; 478509f6ff4fSMatt Macy } 47867f87c040SMarius Strobl 47877f87c040SMarius Strobl /* 47887f87c040SMarius Strobl * Tell the upper layer(s) if IFCAP_VLAN_MTU is supported. 47897f87c040SMarius Strobl * This must appear after the call to ether_ifattach() because 47907f87c040SMarius Strobl * ether_ifattach() sets if_hdrlen to the default value. 47917f87c040SMarius Strobl */ 47927f87c040SMarius Strobl if (if_getcapabilities(ifp) & IFCAP_VLAN_MTU) 47937f87c040SMarius Strobl if_setifheaderlen(ifp, sizeof(struct ether_vlan_header)); 47947f87c040SMarius Strobl 479509f6ff4fSMatt Macy /* XXX handle more than one queue */ 479609f6ff4fSMatt Macy for (i = 0; i < scctx->isc_nrxqsets; i++) 479709f6ff4fSMatt Macy IFDI_RX_CLSET(ctx, 0, i, ctx->ifc_rxqs[i].ifr_fl[0].ifl_sds.ifsd_cl); 479809f6ff4fSMatt Macy 479909f6ff4fSMatt Macy *ctxp = ctx; 480009f6ff4fSMatt Macy 480109f6ff4fSMatt Macy if_setgetcounterfn(ctx->ifc_ifp, iflib_if_get_counter); 480209f6ff4fSMatt Macy iflib_add_device_sysctl_post(ctx); 480309f6ff4fSMatt Macy ctx->ifc_flags |= IFC_INIT_DONE; 480409f6ff4fSMatt Macy return (0); 480509f6ff4fSMatt Macy fail_detach: 480609f6ff4fSMatt Macy ether_ifdetach(ctx->ifc_ifp); 480709f6ff4fSMatt Macy fail_queues: 480809f6ff4fSMatt Macy iflib_tx_structures_free(ctx); 480909f6ff4fSMatt Macy iflib_rx_structures_free(ctx); 48107f3eb9daSPatrick Kelsey fail_iflib_detach: 481109f6ff4fSMatt Macy IFDI_DETACH(ctx); 48127f3eb9daSPatrick Kelsey fail_ctx_free: 48137f3eb9daSPatrick Kelsey free(ctx->ifc_softc, M_IFLIB); 48147f3eb9daSPatrick Kelsey free(ctx, M_IFLIB); 481509f6ff4fSMatt Macy return (err); 481609f6ff4fSMatt Macy } 481709f6ff4fSMatt Macy 481809f6ff4fSMatt Macy int 481909f6ff4fSMatt Macy iflib_pseudo_deregister(if_ctx_t ctx) 482009f6ff4fSMatt Macy { 482109f6ff4fSMatt Macy if_t ifp = ctx->ifc_ifp; 482209f6ff4fSMatt Macy iflib_txq_t txq; 482309f6ff4fSMatt Macy iflib_rxq_t rxq; 482409f6ff4fSMatt Macy int i, j; 482509f6ff4fSMatt Macy struct taskqgroup *tqg; 482609f6ff4fSMatt Macy iflib_fl_t fl; 482709f6ff4fSMatt Macy 482809f6ff4fSMatt Macy /* Unregister VLAN events */ 482909f6ff4fSMatt Macy if (ctx->ifc_vlan_attach_event != NULL) 483009f6ff4fSMatt Macy EVENTHANDLER_DEREGISTER(vlan_config, ctx->ifc_vlan_attach_event); 483109f6ff4fSMatt Macy if (ctx->ifc_vlan_detach_event != NULL) 483209f6ff4fSMatt Macy EVENTHANDLER_DEREGISTER(vlan_unconfig, ctx->ifc_vlan_detach_event); 483309f6ff4fSMatt Macy 483409f6ff4fSMatt Macy ether_ifdetach(ifp); 483509f6ff4fSMatt Macy /* ether_ifdetach calls if_qflush - lock must be destroy afterwards*/ 483609f6ff4fSMatt Macy CTX_LOCK_DESTROY(ctx); 483709f6ff4fSMatt Macy /* XXX drain any dependent tasks */ 483809f6ff4fSMatt Macy tqg = qgroup_if_io_tqg; 483909f6ff4fSMatt Macy for (txq = ctx->ifc_txqs, i = 0; i < NTXQSETS(ctx); i++, txq++) { 484009f6ff4fSMatt Macy callout_drain(&txq->ift_timer); 484109f6ff4fSMatt Macy if (txq->ift_task.gt_uniq != NULL) 484209f6ff4fSMatt Macy taskqgroup_detach(tqg, &txq->ift_task); 484309f6ff4fSMatt Macy } 484409f6ff4fSMatt Macy for (i = 0, rxq = ctx->ifc_rxqs; i < NRXQSETS(ctx); i++, rxq++) { 484509f6ff4fSMatt Macy if (rxq->ifr_task.gt_uniq != NULL) 484609f6ff4fSMatt Macy taskqgroup_detach(tqg, &rxq->ifr_task); 484709f6ff4fSMatt Macy 484809f6ff4fSMatt Macy for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++) 484909f6ff4fSMatt Macy free(fl->ifl_rx_bitmap, M_IFLIB); 485009f6ff4fSMatt Macy } 485109f6ff4fSMatt Macy tqg = qgroup_if_config_tqg; 485209f6ff4fSMatt Macy if (ctx->ifc_admin_task.gt_uniq != NULL) 485309f6ff4fSMatt Macy taskqgroup_detach(tqg, &ctx->ifc_admin_task); 485409f6ff4fSMatt Macy if (ctx->ifc_vflr_task.gt_uniq != NULL) 485509f6ff4fSMatt Macy taskqgroup_detach(tqg, &ctx->ifc_vflr_task); 485609f6ff4fSMatt Macy 485709f6ff4fSMatt Macy if_free(ifp); 485809f6ff4fSMatt Macy 485909f6ff4fSMatt Macy iflib_tx_structures_free(ctx); 486009f6ff4fSMatt Macy iflib_rx_structures_free(ctx); 486109f6ff4fSMatt Macy if (ctx->ifc_flags & IFC_SC_ALLOCATED) 486209f6ff4fSMatt Macy free(ctx->ifc_softc, M_IFLIB); 486309f6ff4fSMatt Macy free(ctx, M_IFLIB); 486409f6ff4fSMatt Macy return (0); 486509f6ff4fSMatt Macy } 486609f6ff4fSMatt Macy 486709f6ff4fSMatt Macy int 48684c7070dbSScott Long iflib_device_attach(device_t dev) 48694c7070dbSScott Long { 48704c7070dbSScott Long if_ctx_t ctx; 48714c7070dbSScott Long if_shared_ctx_t sctx; 48724c7070dbSScott Long 48734c7070dbSScott Long if ((sctx = DEVICE_REGISTER(dev)) == NULL || sctx->isc_magic != IFLIB_MAGIC) 48744c7070dbSScott Long return (ENOTSUP); 48754c7070dbSScott Long 48764c7070dbSScott Long pci_enable_busmaster(dev); 48774c7070dbSScott Long 48784c7070dbSScott Long return (iflib_device_register(dev, NULL, sctx, &ctx)); 48794c7070dbSScott Long } 48804c7070dbSScott Long 48814c7070dbSScott Long int 48824c7070dbSScott Long iflib_device_deregister(if_ctx_t ctx) 48834c7070dbSScott Long { 48844c7070dbSScott Long if_t ifp = ctx->ifc_ifp; 48854c7070dbSScott Long iflib_txq_t txq; 48864c7070dbSScott Long iflib_rxq_t rxq; 48874c7070dbSScott Long device_t dev = ctx->ifc_dev; 488887890dbaSSean Bruno int i, j; 48894c7070dbSScott Long struct taskqgroup *tqg; 489087890dbaSSean Bruno iflib_fl_t fl; 48914c7070dbSScott Long 48924c7070dbSScott Long /* Make sure VLANS are not using driver */ 48934c7070dbSScott Long if (if_vlantrunkinuse(ifp)) { 48944c7070dbSScott Long device_printf(dev, "Vlan in use, detach first\n"); 48954c7070dbSScott Long return (EBUSY); 48964c7070dbSScott Long } 489777c1fcecSEric Joyner #ifdef PCI_IOV 489877c1fcecSEric Joyner if (!CTX_IS_VF(ctx) && pci_iov_detach(dev) != 0) { 489977c1fcecSEric Joyner device_printf(dev, "SR-IOV in use; detach first.\n"); 490077c1fcecSEric Joyner return (EBUSY); 490177c1fcecSEric Joyner } 490277c1fcecSEric Joyner #endif 490377c1fcecSEric Joyner 490477c1fcecSEric Joyner STATE_LOCK(ctx); 490577c1fcecSEric Joyner ctx->ifc_flags |= IFC_IN_DETACH; 490677c1fcecSEric Joyner STATE_UNLOCK(ctx); 49074c7070dbSScott Long 49084c7070dbSScott Long CTX_LOCK(ctx); 49094c7070dbSScott Long iflib_stop(ctx); 49104c7070dbSScott Long CTX_UNLOCK(ctx); 49114c7070dbSScott Long 49124c7070dbSScott Long /* Unregister VLAN events */ 49134c7070dbSScott Long if (ctx->ifc_vlan_attach_event != NULL) 49144c7070dbSScott Long EVENTHANDLER_DEREGISTER(vlan_config, ctx->ifc_vlan_attach_event); 49154c7070dbSScott Long if (ctx->ifc_vlan_detach_event != NULL) 49164c7070dbSScott Long EVENTHANDLER_DEREGISTER(vlan_unconfig, ctx->ifc_vlan_detach_event); 49174c7070dbSScott Long 49184c7070dbSScott Long iflib_netmap_detach(ifp); 49194c7070dbSScott Long ether_ifdetach(ifp); 49204c7070dbSScott Long if (ctx->ifc_led_dev != NULL) 49214c7070dbSScott Long led_destroy(ctx->ifc_led_dev); 49224c7070dbSScott Long /* XXX drain any dependent tasks */ 4923ab2e3f79SStephen Hurd tqg = qgroup_if_io_tqg; 492423ac9029SStephen Hurd for (txq = ctx->ifc_txqs, i = 0; i < NTXQSETS(ctx); i++, txq++) { 49254c7070dbSScott Long callout_drain(&txq->ift_timer); 49264c7070dbSScott Long if (txq->ift_task.gt_uniq != NULL) 49274c7070dbSScott Long taskqgroup_detach(tqg, &txq->ift_task); 49284c7070dbSScott Long } 49294c7070dbSScott Long for (i = 0, rxq = ctx->ifc_rxqs; i < NRXQSETS(ctx); i++, rxq++) { 49304c7070dbSScott Long if (rxq->ifr_task.gt_uniq != NULL) 49314c7070dbSScott Long taskqgroup_detach(tqg, &rxq->ifr_task); 493287890dbaSSean Bruno 493387890dbaSSean Bruno for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++) 493487890dbaSSean Bruno free(fl->ifl_rx_bitmap, M_IFLIB); 49354c7070dbSScott Long } 4936ab2e3f79SStephen Hurd tqg = qgroup_if_config_tqg; 49374c7070dbSScott Long if (ctx->ifc_admin_task.gt_uniq != NULL) 49384c7070dbSScott Long taskqgroup_detach(tqg, &ctx->ifc_admin_task); 49394c7070dbSScott Long if (ctx->ifc_vflr_task.gt_uniq != NULL) 49404c7070dbSScott Long taskqgroup_detach(tqg, &ctx->ifc_vflr_task); 49416c3c3194SMatt Macy CTX_LOCK(ctx); 49424c7070dbSScott Long IFDI_DETACH(ctx); 49436c3c3194SMatt Macy CTX_UNLOCK(ctx); 49446c3c3194SMatt Macy 49456c3c3194SMatt Macy /* ether_ifdetach calls if_qflush - lock must be destroy afterwards*/ 49466c3c3194SMatt Macy CTX_LOCK_DESTROY(ctx); 494723ac9029SStephen Hurd device_set_softc(ctx->ifc_dev, NULL); 494877c1fcecSEric Joyner iflib_free_intr_mem(ctx); 494977c1fcecSEric Joyner 495077c1fcecSEric Joyner bus_generic_detach(dev); 495177c1fcecSEric Joyner if_free(ifp); 495277c1fcecSEric Joyner 495377c1fcecSEric Joyner iflib_tx_structures_free(ctx); 495477c1fcecSEric Joyner iflib_rx_structures_free(ctx); 495577c1fcecSEric Joyner if (ctx->ifc_flags & IFC_SC_ALLOCATED) 495677c1fcecSEric Joyner free(ctx->ifc_softc, M_IFLIB); 495777c1fcecSEric Joyner STATE_LOCK_DESTROY(ctx); 495877c1fcecSEric Joyner free(ctx, M_IFLIB); 495977c1fcecSEric Joyner return (0); 496077c1fcecSEric Joyner } 496177c1fcecSEric Joyner 496277c1fcecSEric Joyner static void 496377c1fcecSEric Joyner iflib_free_intr_mem(if_ctx_t ctx) 496477c1fcecSEric Joyner { 496577c1fcecSEric Joyner 49664c7070dbSScott Long if (ctx->ifc_softc_ctx.isc_intr != IFLIB_INTR_MSIX) { 49674c7070dbSScott Long iflib_irq_free(ctx, &ctx->ifc_legacy_irq); 49684c7070dbSScott Long } 4969b97de13aSMarius Strobl if (ctx->ifc_softc_ctx.isc_intr != IFLIB_INTR_LEGACY) { 4970b97de13aSMarius Strobl pci_release_msi(ctx->ifc_dev); 4971b97de13aSMarius Strobl } 49724c7070dbSScott Long if (ctx->ifc_msix_mem != NULL) { 49734c7070dbSScott Long bus_release_resource(ctx->ifc_dev, SYS_RES_MEMORY, 4974b97de13aSMarius Strobl rman_get_rid(ctx->ifc_msix_mem), ctx->ifc_msix_mem); 49754c7070dbSScott Long ctx->ifc_msix_mem = NULL; 49764c7070dbSScott Long } 49774c7070dbSScott Long } 49784c7070dbSScott Long 49794c7070dbSScott Long int 49804c7070dbSScott Long iflib_device_detach(device_t dev) 49814c7070dbSScott Long { 49824c7070dbSScott Long if_ctx_t ctx = device_get_softc(dev); 49834c7070dbSScott Long 49844c7070dbSScott Long return (iflib_device_deregister(ctx)); 49854c7070dbSScott Long } 49864c7070dbSScott Long 49874c7070dbSScott Long int 49884c7070dbSScott Long iflib_device_suspend(device_t dev) 49894c7070dbSScott Long { 49904c7070dbSScott Long if_ctx_t ctx = device_get_softc(dev); 49914c7070dbSScott Long 49924c7070dbSScott Long CTX_LOCK(ctx); 49934c7070dbSScott Long IFDI_SUSPEND(ctx); 49944c7070dbSScott Long CTX_UNLOCK(ctx); 49954c7070dbSScott Long 49964c7070dbSScott Long return bus_generic_suspend(dev); 49974c7070dbSScott Long } 49984c7070dbSScott Long int 49994c7070dbSScott Long iflib_device_shutdown(device_t dev) 50004c7070dbSScott Long { 50014c7070dbSScott Long if_ctx_t ctx = device_get_softc(dev); 50024c7070dbSScott Long 50034c7070dbSScott Long CTX_LOCK(ctx); 50044c7070dbSScott Long IFDI_SHUTDOWN(ctx); 50054c7070dbSScott Long CTX_UNLOCK(ctx); 50064c7070dbSScott Long 50074c7070dbSScott Long return bus_generic_suspend(dev); 50084c7070dbSScott Long } 50094c7070dbSScott Long 50104c7070dbSScott Long 50114c7070dbSScott Long int 50124c7070dbSScott Long iflib_device_resume(device_t dev) 50134c7070dbSScott Long { 50144c7070dbSScott Long if_ctx_t ctx = device_get_softc(dev); 50154c7070dbSScott Long iflib_txq_t txq = ctx->ifc_txqs; 50164c7070dbSScott Long 50174c7070dbSScott Long CTX_LOCK(ctx); 50184c7070dbSScott Long IFDI_RESUME(ctx); 5019cd28ea92SStephen Hurd iflib_if_init_locked(ctx); 50204c7070dbSScott Long CTX_UNLOCK(ctx); 50214c7070dbSScott Long for (int i = 0; i < NTXQSETS(ctx); i++, txq++) 50224c7070dbSScott Long iflib_txq_check_drain(txq, IFLIB_RESTART_BUDGET); 50234c7070dbSScott Long 50244c7070dbSScott Long return (bus_generic_resume(dev)); 50254c7070dbSScott Long } 50264c7070dbSScott Long 50274c7070dbSScott Long int 50284c7070dbSScott Long iflib_device_iov_init(device_t dev, uint16_t num_vfs, const nvlist_t *params) 50294c7070dbSScott Long { 50304c7070dbSScott Long int error; 50314c7070dbSScott Long if_ctx_t ctx = device_get_softc(dev); 50324c7070dbSScott Long 50334c7070dbSScott Long CTX_LOCK(ctx); 50344c7070dbSScott Long error = IFDI_IOV_INIT(ctx, num_vfs, params); 50354c7070dbSScott Long CTX_UNLOCK(ctx); 50364c7070dbSScott Long 50374c7070dbSScott Long return (error); 50384c7070dbSScott Long } 50394c7070dbSScott Long 50404c7070dbSScott Long void 50414c7070dbSScott Long iflib_device_iov_uninit(device_t dev) 50424c7070dbSScott Long { 50434c7070dbSScott Long if_ctx_t ctx = device_get_softc(dev); 50444c7070dbSScott Long 50454c7070dbSScott Long CTX_LOCK(ctx); 50464c7070dbSScott Long IFDI_IOV_UNINIT(ctx); 50474c7070dbSScott Long CTX_UNLOCK(ctx); 50484c7070dbSScott Long } 50494c7070dbSScott Long 50504c7070dbSScott Long int 50514c7070dbSScott Long iflib_device_iov_add_vf(device_t dev, uint16_t vfnum, const nvlist_t *params) 50524c7070dbSScott Long { 50534c7070dbSScott Long int error; 50544c7070dbSScott Long if_ctx_t ctx = device_get_softc(dev); 50554c7070dbSScott Long 50564c7070dbSScott Long CTX_LOCK(ctx); 50574c7070dbSScott Long error = IFDI_IOV_VF_ADD(ctx, vfnum, params); 50584c7070dbSScott Long CTX_UNLOCK(ctx); 50594c7070dbSScott Long 50604c7070dbSScott Long return (error); 50614c7070dbSScott Long } 50624c7070dbSScott Long 50634c7070dbSScott Long /********************************************************************* 50644c7070dbSScott Long * 50654c7070dbSScott Long * MODULE FUNCTION DEFINITIONS 50664c7070dbSScott Long * 50674c7070dbSScott Long **********************************************************************/ 50684c7070dbSScott Long 5069ab2e3f79SStephen Hurd /* 5070ab2e3f79SStephen Hurd * - Start a fast taskqueue thread for each core 5071ab2e3f79SStephen Hurd * - Start a taskqueue for control operations 5072ab2e3f79SStephen Hurd */ 50734c7070dbSScott Long static int 50744c7070dbSScott Long iflib_module_init(void) 50754c7070dbSScott Long { 50764c7070dbSScott Long return (0); 50774c7070dbSScott Long } 50784c7070dbSScott Long 50794c7070dbSScott Long static int 50804c7070dbSScott Long iflib_module_event_handler(module_t mod, int what, void *arg) 50814c7070dbSScott Long { 50824c7070dbSScott Long int err; 50834c7070dbSScott Long 50844c7070dbSScott Long switch (what) { 50854c7070dbSScott Long case MOD_LOAD: 50864c7070dbSScott Long if ((err = iflib_module_init()) != 0) 50874c7070dbSScott Long return (err); 50884c7070dbSScott Long break; 50894c7070dbSScott Long case MOD_UNLOAD: 50904c7070dbSScott Long return (EBUSY); 50914c7070dbSScott Long default: 50924c7070dbSScott Long return (EOPNOTSUPP); 50934c7070dbSScott Long } 50944c7070dbSScott Long 50954c7070dbSScott Long return (0); 50964c7070dbSScott Long } 50974c7070dbSScott Long 50984c7070dbSScott Long /********************************************************************* 50994c7070dbSScott Long * 51004c7070dbSScott Long * PUBLIC FUNCTION DEFINITIONS 51014c7070dbSScott Long * ordered as in iflib.h 51024c7070dbSScott Long * 51034c7070dbSScott Long **********************************************************************/ 51044c7070dbSScott Long 51054c7070dbSScott Long 51064c7070dbSScott Long static void 51074c7070dbSScott Long _iflib_assert(if_shared_ctx_t sctx) 51084c7070dbSScott Long { 51094c7070dbSScott Long MPASS(sctx->isc_tx_maxsize); 51104c7070dbSScott Long MPASS(sctx->isc_tx_maxsegsize); 51114c7070dbSScott Long 51124c7070dbSScott Long MPASS(sctx->isc_rx_maxsize); 51134c7070dbSScott Long MPASS(sctx->isc_rx_nsegments); 51144c7070dbSScott Long MPASS(sctx->isc_rx_maxsegsize); 51154c7070dbSScott Long 511623ac9029SStephen Hurd MPASS(sctx->isc_nrxd_min[0]); 511723ac9029SStephen Hurd MPASS(sctx->isc_nrxd_max[0]); 511823ac9029SStephen Hurd MPASS(sctx->isc_nrxd_default[0]); 511923ac9029SStephen Hurd MPASS(sctx->isc_ntxd_min[0]); 512023ac9029SStephen Hurd MPASS(sctx->isc_ntxd_max[0]); 512123ac9029SStephen Hurd MPASS(sctx->isc_ntxd_default[0]); 51224c7070dbSScott Long } 51234c7070dbSScott Long 51241248952aSSean Bruno static void 51251248952aSSean Bruno _iflib_pre_assert(if_softc_ctx_t scctx) 51261248952aSSean Bruno { 51271248952aSSean Bruno 51281248952aSSean Bruno MPASS(scctx->isc_txrx->ift_txd_encap); 51291248952aSSean Bruno MPASS(scctx->isc_txrx->ift_txd_flush); 51301248952aSSean Bruno MPASS(scctx->isc_txrx->ift_txd_credits_update); 51311248952aSSean Bruno MPASS(scctx->isc_txrx->ift_rxd_available); 51321248952aSSean Bruno MPASS(scctx->isc_txrx->ift_rxd_pkt_get); 51331248952aSSean Bruno MPASS(scctx->isc_txrx->ift_rxd_refill); 51341248952aSSean Bruno MPASS(scctx->isc_txrx->ift_rxd_flush); 51351248952aSSean Bruno } 51362fe66646SSean Bruno 51374c7070dbSScott Long static int 51384c7070dbSScott Long iflib_register(if_ctx_t ctx) 51394c7070dbSScott Long { 51404c7070dbSScott Long if_shared_ctx_t sctx = ctx->ifc_sctx; 51414c7070dbSScott Long driver_t *driver = sctx->isc_driver; 51424c7070dbSScott Long device_t dev = ctx->ifc_dev; 51434c7070dbSScott Long if_t ifp; 51444c7070dbSScott Long 51454c7070dbSScott Long _iflib_assert(sctx); 51464c7070dbSScott Long 5147aa8a24d3SStephen Hurd CTX_LOCK_INIT(ctx); 51487b610b60SSean Bruno STATE_LOCK_INIT(ctx, device_get_nameunit(ctx->ifc_dev)); 514977c1fcecSEric Joyner ifp = ctx->ifc_ifp = if_alloc(IFT_ETHER); 51504c7070dbSScott Long if (ifp == NULL) { 51514c7070dbSScott Long device_printf(dev, "can not allocate ifnet structure\n"); 51524c7070dbSScott Long return (ENOMEM); 51534c7070dbSScott Long } 51544c7070dbSScott Long 51554c7070dbSScott Long /* 51564c7070dbSScott Long * Initialize our context's device specific methods 51574c7070dbSScott Long */ 51584c7070dbSScott Long kobj_init((kobj_t) ctx, (kobj_class_t) driver); 51594c7070dbSScott Long kobj_class_compile((kobj_class_t) driver); 51604c7070dbSScott Long driver->refs++; 51614c7070dbSScott Long 51624c7070dbSScott Long if_initname(ifp, device_get_name(dev), device_get_unit(dev)); 51634c7070dbSScott Long if_setsoftc(ifp, ctx); 51644c7070dbSScott Long if_setdev(ifp, dev); 51654c7070dbSScott Long if_setinitfn(ifp, iflib_if_init); 51664c7070dbSScott Long if_setioctlfn(ifp, iflib_if_ioctl); 5167b8ca4756SPatrick Kelsey #ifdef ALTQ 5168b8ca4756SPatrick Kelsey if_setstartfn(ifp, iflib_altq_if_start); 5169b8ca4756SPatrick Kelsey if_settransmitfn(ifp, iflib_altq_if_transmit); 51708f410865SPatrick Kelsey if_setsendqready(ifp); 5171b8ca4756SPatrick Kelsey #else 51724c7070dbSScott Long if_settransmitfn(ifp, iflib_if_transmit); 5173b8ca4756SPatrick Kelsey #endif 51744c7070dbSScott Long if_setqflushfn(ifp, iflib_if_qflush); 51754c7070dbSScott Long if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST); 51764c7070dbSScott Long 51774c7070dbSScott Long ctx->ifc_vlan_attach_event = 51784c7070dbSScott Long EVENTHANDLER_REGISTER(vlan_config, iflib_vlan_register, ctx, 51794c7070dbSScott Long EVENTHANDLER_PRI_FIRST); 51804c7070dbSScott Long ctx->ifc_vlan_detach_event = 51814c7070dbSScott Long EVENTHANDLER_REGISTER(vlan_unconfig, iflib_vlan_unregister, ctx, 51824c7070dbSScott Long EVENTHANDLER_PRI_FIRST); 51834c7070dbSScott Long 51844c7070dbSScott Long ifmedia_init(&ctx->ifc_media, IFM_IMASK, 51854c7070dbSScott Long iflib_media_change, iflib_media_status); 51864c7070dbSScott Long 51874c7070dbSScott Long return (0); 51884c7070dbSScott Long } 51894c7070dbSScott Long 51904c7070dbSScott Long 51914c7070dbSScott Long static int 51924c7070dbSScott Long iflib_queues_alloc(if_ctx_t ctx) 51934c7070dbSScott Long { 51944c7070dbSScott Long if_shared_ctx_t sctx = ctx->ifc_sctx; 519523ac9029SStephen Hurd if_softc_ctx_t scctx = &ctx->ifc_softc_ctx; 51964c7070dbSScott Long device_t dev = ctx->ifc_dev; 519723ac9029SStephen Hurd int nrxqsets = scctx->isc_nrxqsets; 519823ac9029SStephen Hurd int ntxqsets = scctx->isc_ntxqsets; 51994c7070dbSScott Long iflib_txq_t txq; 52004c7070dbSScott Long iflib_rxq_t rxq; 52014c7070dbSScott Long iflib_fl_t fl = NULL; 520223ac9029SStephen Hurd int i, j, cpu, err, txconf, rxconf; 52034c7070dbSScott Long iflib_dma_info_t ifdip; 520423ac9029SStephen Hurd uint32_t *rxqsizes = scctx->isc_rxqsizes; 520523ac9029SStephen Hurd uint32_t *txqsizes = scctx->isc_txqsizes; 52064c7070dbSScott Long uint8_t nrxqs = sctx->isc_nrxqs; 52074c7070dbSScott Long uint8_t ntxqs = sctx->isc_ntxqs; 52084c7070dbSScott Long int nfree_lists = sctx->isc_nfl ? sctx->isc_nfl : 1; 52094c7070dbSScott Long caddr_t *vaddrs; 52104c7070dbSScott Long uint64_t *paddrs; 52114c7070dbSScott Long 521223ac9029SStephen Hurd KASSERT(ntxqs > 0, ("number of queues per qset must be at least 1")); 521323ac9029SStephen Hurd KASSERT(nrxqs > 0, ("number of queues per qset must be at least 1")); 52144c7070dbSScott Long 52154c7070dbSScott Long /* Allocate the TX ring struct memory */ 5216b89827a0SStephen Hurd if (!(ctx->ifc_txqs = 5217ac2fffa4SPedro F. Giffuni (iflib_txq_t) malloc(sizeof(struct iflib_txq) * 5218ac2fffa4SPedro F. Giffuni ntxqsets, M_IFLIB, M_NOWAIT | M_ZERO))) { 52194c7070dbSScott Long device_printf(dev, "Unable to allocate TX ring memory\n"); 52204c7070dbSScott Long err = ENOMEM; 52214c7070dbSScott Long goto fail; 52224c7070dbSScott Long } 52234c7070dbSScott Long 52244c7070dbSScott Long /* Now allocate the RX */ 5225b89827a0SStephen Hurd if (!(ctx->ifc_rxqs = 5226ac2fffa4SPedro F. Giffuni (iflib_rxq_t) malloc(sizeof(struct iflib_rxq) * 5227ac2fffa4SPedro F. Giffuni nrxqsets, M_IFLIB, M_NOWAIT | M_ZERO))) { 52284c7070dbSScott Long device_printf(dev, "Unable to allocate RX ring memory\n"); 52294c7070dbSScott Long err = ENOMEM; 52304c7070dbSScott Long goto rx_fail; 52314c7070dbSScott Long } 52324c7070dbSScott Long 5233b89827a0SStephen Hurd txq = ctx->ifc_txqs; 5234b89827a0SStephen Hurd rxq = ctx->ifc_rxqs; 52354c7070dbSScott Long 52364c7070dbSScott Long /* 52374c7070dbSScott Long * XXX handle allocation failure 52384c7070dbSScott Long */ 523996c85efbSNathan Whitehorn for (txconf = i = 0, cpu = CPU_FIRST(); i < ntxqsets; i++, txconf++, txq++, cpu = CPU_NEXT(cpu)) { 52404c7070dbSScott Long /* Set up some basics */ 52414c7070dbSScott Long 5242bfce461eSMarius Strobl if ((ifdip = malloc(sizeof(struct iflib_dma_info) * ntxqs, 5243bfce461eSMarius Strobl M_IFLIB, M_NOWAIT | M_ZERO)) == NULL) { 5244bfce461eSMarius Strobl device_printf(dev, 5245bfce461eSMarius Strobl "Unable to allocate TX DMA info memory\n"); 52464c7070dbSScott Long err = ENOMEM; 52470d0338afSConrad Meyer goto err_tx_desc; 52484c7070dbSScott Long } 52494c7070dbSScott Long txq->ift_ifdi = ifdip; 52504c7070dbSScott Long for (j = 0; j < ntxqs; j++, ifdip++) { 5251bfce461eSMarius Strobl if (iflib_dma_alloc(ctx, txqsizes[j], ifdip, 0)) { 5252bfce461eSMarius Strobl device_printf(dev, 5253bfce461eSMarius Strobl "Unable to allocate TX descriptors\n"); 52544c7070dbSScott Long err = ENOMEM; 52554c7070dbSScott Long goto err_tx_desc; 52564c7070dbSScott Long } 525795246abbSSean Bruno txq->ift_txd_size[j] = scctx->isc_txd_size[j]; 52584c7070dbSScott Long bzero((void *)ifdip->idi_vaddr, txqsizes[j]); 52594c7070dbSScott Long } 52604c7070dbSScott Long txq->ift_ctx = ctx; 52614c7070dbSScott Long txq->ift_id = i; 526223ac9029SStephen Hurd if (sctx->isc_flags & IFLIB_HAS_TXCQ) { 526323ac9029SStephen Hurd txq->ift_br_offset = 1; 526423ac9029SStephen Hurd } else { 526523ac9029SStephen Hurd txq->ift_br_offset = 0; 526623ac9029SStephen Hurd } 52674c7070dbSScott Long /* XXX fix this */ 526896c85efbSNathan Whitehorn txq->ift_timer.c_cpu = cpu; 52694c7070dbSScott Long 52704c7070dbSScott Long if (iflib_txsd_alloc(txq)) { 52714c7070dbSScott Long device_printf(dev, "Critical Failure setting up TX buffers\n"); 52724c7070dbSScott Long err = ENOMEM; 52734c7070dbSScott Long goto err_tx_desc; 52744c7070dbSScott Long } 52754c7070dbSScott Long 52764c7070dbSScott Long /* Initialize the TX lock */ 52774c7070dbSScott Long snprintf(txq->ift_mtx_name, MTX_NAME_LEN, "%s:tx(%d):callout", 52784c7070dbSScott Long device_get_nameunit(dev), txq->ift_id); 52794c7070dbSScott Long mtx_init(&txq->ift_mtx, txq->ift_mtx_name, NULL, MTX_DEF); 52804c7070dbSScott Long callout_init_mtx(&txq->ift_timer, &txq->ift_mtx, 0); 52814c7070dbSScott Long 52824c7070dbSScott Long snprintf(txq->ift_db_mtx_name, MTX_NAME_LEN, "%s:tx(%d):db", 52834c7070dbSScott Long device_get_nameunit(dev), txq->ift_id); 52844c7070dbSScott Long 528595246abbSSean Bruno err = ifmp_ring_alloc(&txq->ift_br, 2048, txq, iflib_txq_drain, 52864c7070dbSScott Long iflib_txq_can_drain, M_IFLIB, M_WAITOK); 52874c7070dbSScott Long if (err) { 52884c7070dbSScott Long /* XXX free any allocated rings */ 52894c7070dbSScott Long device_printf(dev, "Unable to allocate buf_ring\n"); 52900d0338afSConrad Meyer goto err_tx_desc; 52914c7070dbSScott Long } 52924c7070dbSScott Long } 52934c7070dbSScott Long 52944c7070dbSScott Long for (rxconf = i = 0; i < nrxqsets; i++, rxconf++, rxq++) { 52954c7070dbSScott Long /* Set up some basics */ 52964c7070dbSScott Long 5297bfce461eSMarius Strobl if ((ifdip = malloc(sizeof(struct iflib_dma_info) * nrxqs, 5298bfce461eSMarius Strobl M_IFLIB, M_NOWAIT | M_ZERO)) == NULL) { 5299bfce461eSMarius Strobl device_printf(dev, 5300bfce461eSMarius Strobl "Unable to allocate RX DMA info memory\n"); 53014c7070dbSScott Long err = ENOMEM; 53020d0338afSConrad Meyer goto err_tx_desc; 53034c7070dbSScott Long } 53044c7070dbSScott Long 53054c7070dbSScott Long rxq->ifr_ifdi = ifdip; 530695246abbSSean Bruno /* XXX this needs to be changed if #rx queues != #tx queues */ 530795246abbSSean Bruno rxq->ifr_ntxqirq = 1; 530895246abbSSean Bruno rxq->ifr_txqid[0] = i; 53094c7070dbSScott Long for (j = 0; j < nrxqs; j++, ifdip++) { 5310bfce461eSMarius Strobl if (iflib_dma_alloc(ctx, rxqsizes[j], ifdip, 0)) { 5311bfce461eSMarius Strobl device_printf(dev, 5312bfce461eSMarius Strobl "Unable to allocate RX descriptors\n"); 53134c7070dbSScott Long err = ENOMEM; 53144c7070dbSScott Long goto err_tx_desc; 53154c7070dbSScott Long } 53164c7070dbSScott Long bzero((void *)ifdip->idi_vaddr, rxqsizes[j]); 53174c7070dbSScott Long } 53184c7070dbSScott Long rxq->ifr_ctx = ctx; 53194c7070dbSScott Long rxq->ifr_id = i; 532023ac9029SStephen Hurd if (sctx->isc_flags & IFLIB_HAS_RXCQ) { 532123ac9029SStephen Hurd rxq->ifr_fl_offset = 1; 53224c7070dbSScott Long } else { 532323ac9029SStephen Hurd rxq->ifr_fl_offset = 0; 53244c7070dbSScott Long } 53254c7070dbSScott Long rxq->ifr_nfl = nfree_lists; 53264c7070dbSScott Long if (!(fl = 5327ac2fffa4SPedro F. Giffuni (iflib_fl_t) malloc(sizeof(struct iflib_fl) * nfree_lists, M_IFLIB, M_NOWAIT | M_ZERO))) { 53284c7070dbSScott Long device_printf(dev, "Unable to allocate free list memory\n"); 53294c7070dbSScott Long err = ENOMEM; 53300d0338afSConrad Meyer goto err_tx_desc; 53314c7070dbSScott Long } 53324c7070dbSScott Long rxq->ifr_fl = fl; 53334c7070dbSScott Long for (j = 0; j < nfree_lists; j++) { 533495246abbSSean Bruno fl[j].ifl_rxq = rxq; 533595246abbSSean Bruno fl[j].ifl_id = j; 533695246abbSSean Bruno fl[j].ifl_ifdi = &rxq->ifr_ifdi[j + rxq->ifr_fl_offset]; 533795246abbSSean Bruno fl[j].ifl_rxd_size = scctx->isc_rxd_size[j]; 53384c7070dbSScott Long } 53394c7070dbSScott Long /* Allocate receive buffers for the ring */ 53404c7070dbSScott Long if (iflib_rxsd_alloc(rxq)) { 53414c7070dbSScott Long device_printf(dev, 53424c7070dbSScott Long "Critical Failure setting up receive buffers\n"); 53434c7070dbSScott Long err = ENOMEM; 53444c7070dbSScott Long goto err_rx_desc; 53454c7070dbSScott Long } 534687890dbaSSean Bruno 534787890dbaSSean Bruno for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++) 53483db348b5SMarius Strobl fl->ifl_rx_bitmap = bit_alloc(fl->ifl_size, M_IFLIB, 53493db348b5SMarius Strobl M_WAITOK); 53504c7070dbSScott Long } 53514c7070dbSScott Long 53524c7070dbSScott Long /* TXQs */ 53534c7070dbSScott Long vaddrs = malloc(sizeof(caddr_t)*ntxqsets*ntxqs, M_IFLIB, M_WAITOK); 53544c7070dbSScott Long paddrs = malloc(sizeof(uint64_t)*ntxqsets*ntxqs, M_IFLIB, M_WAITOK); 53554c7070dbSScott Long for (i = 0; i < ntxqsets; i++) { 53564c7070dbSScott Long iflib_dma_info_t di = ctx->ifc_txqs[i].ift_ifdi; 53574c7070dbSScott Long 53584c7070dbSScott Long for (j = 0; j < ntxqs; j++, di++) { 53594c7070dbSScott Long vaddrs[i*ntxqs + j] = di->idi_vaddr; 53604c7070dbSScott Long paddrs[i*ntxqs + j] = di->idi_paddr; 53614c7070dbSScott Long } 53624c7070dbSScott Long } 53634c7070dbSScott Long if ((err = IFDI_TX_QUEUES_ALLOC(ctx, vaddrs, paddrs, ntxqs, ntxqsets)) != 0) { 5364bfce461eSMarius Strobl device_printf(ctx->ifc_dev, 5365bfce461eSMarius Strobl "Unable to allocate device TX queue\n"); 53664c7070dbSScott Long iflib_tx_structures_free(ctx); 53674c7070dbSScott Long free(vaddrs, M_IFLIB); 53684c7070dbSScott Long free(paddrs, M_IFLIB); 53694c7070dbSScott Long goto err_rx_desc; 53704c7070dbSScott Long } 53714c7070dbSScott Long free(vaddrs, M_IFLIB); 53724c7070dbSScott Long free(paddrs, M_IFLIB); 53734c7070dbSScott Long 53744c7070dbSScott Long /* RXQs */ 53754c7070dbSScott Long vaddrs = malloc(sizeof(caddr_t)*nrxqsets*nrxqs, M_IFLIB, M_WAITOK); 53764c7070dbSScott Long paddrs = malloc(sizeof(uint64_t)*nrxqsets*nrxqs, M_IFLIB, M_WAITOK); 53774c7070dbSScott Long for (i = 0; i < nrxqsets; i++) { 53784c7070dbSScott Long iflib_dma_info_t di = ctx->ifc_rxqs[i].ifr_ifdi; 53794c7070dbSScott Long 53804c7070dbSScott Long for (j = 0; j < nrxqs; j++, di++) { 53814c7070dbSScott Long vaddrs[i*nrxqs + j] = di->idi_vaddr; 53824c7070dbSScott Long paddrs[i*nrxqs + j] = di->idi_paddr; 53834c7070dbSScott Long } 53844c7070dbSScott Long } 53854c7070dbSScott Long if ((err = IFDI_RX_QUEUES_ALLOC(ctx, vaddrs, paddrs, nrxqs, nrxqsets)) != 0) { 5386bfce461eSMarius Strobl device_printf(ctx->ifc_dev, 5387bfce461eSMarius Strobl "Unable to allocate device RX queue\n"); 53884c7070dbSScott Long iflib_tx_structures_free(ctx); 53894c7070dbSScott Long free(vaddrs, M_IFLIB); 53904c7070dbSScott Long free(paddrs, M_IFLIB); 53914c7070dbSScott Long goto err_rx_desc; 53924c7070dbSScott Long } 53934c7070dbSScott Long free(vaddrs, M_IFLIB); 53944c7070dbSScott Long free(paddrs, M_IFLIB); 53954c7070dbSScott Long 53964c7070dbSScott Long return (0); 53974c7070dbSScott Long 53984c7070dbSScott Long /* XXX handle allocation failure changes */ 53994c7070dbSScott Long err_rx_desc: 54004c7070dbSScott Long err_tx_desc: 5401b89827a0SStephen Hurd rx_fail: 54024c7070dbSScott Long if (ctx->ifc_rxqs != NULL) 54034c7070dbSScott Long free(ctx->ifc_rxqs, M_IFLIB); 54044c7070dbSScott Long ctx->ifc_rxqs = NULL; 54054c7070dbSScott Long if (ctx->ifc_txqs != NULL) 54064c7070dbSScott Long free(ctx->ifc_txqs, M_IFLIB); 54074c7070dbSScott Long ctx->ifc_txqs = NULL; 54084c7070dbSScott Long fail: 54094c7070dbSScott Long return (err); 54104c7070dbSScott Long } 54114c7070dbSScott Long 54124c7070dbSScott Long static int 54134c7070dbSScott Long iflib_tx_structures_setup(if_ctx_t ctx) 54144c7070dbSScott Long { 54154c7070dbSScott Long iflib_txq_t txq = ctx->ifc_txqs; 54164c7070dbSScott Long int i; 54174c7070dbSScott Long 54184c7070dbSScott Long for (i = 0; i < NTXQSETS(ctx); i++, txq++) 54194c7070dbSScott Long iflib_txq_setup(txq); 54204c7070dbSScott Long 54214c7070dbSScott Long return (0); 54224c7070dbSScott Long } 54234c7070dbSScott Long 54244c7070dbSScott Long static void 54254c7070dbSScott Long iflib_tx_structures_free(if_ctx_t ctx) 54264c7070dbSScott Long { 54274c7070dbSScott Long iflib_txq_t txq = ctx->ifc_txqs; 54284d261ce2SStephen Hurd if_shared_ctx_t sctx = ctx->ifc_sctx; 54294c7070dbSScott Long int i, j; 54304c7070dbSScott Long 54314c7070dbSScott Long for (i = 0; i < NTXQSETS(ctx); i++, txq++) { 54324c7070dbSScott Long iflib_txq_destroy(txq); 54334d261ce2SStephen Hurd for (j = 0; j < sctx->isc_ntxqs; j++) 54344c7070dbSScott Long iflib_dma_free(&txq->ift_ifdi[j]); 54354c7070dbSScott Long } 54364c7070dbSScott Long free(ctx->ifc_txqs, M_IFLIB); 54374c7070dbSScott Long ctx->ifc_txqs = NULL; 54384c7070dbSScott Long IFDI_QUEUES_FREE(ctx); 54394c7070dbSScott Long } 54404c7070dbSScott Long 54414c7070dbSScott Long /********************************************************************* 54424c7070dbSScott Long * 54434c7070dbSScott Long * Initialize all receive rings. 54444c7070dbSScott Long * 54454c7070dbSScott Long **********************************************************************/ 54464c7070dbSScott Long static int 54474c7070dbSScott Long iflib_rx_structures_setup(if_ctx_t ctx) 54484c7070dbSScott Long { 54494c7070dbSScott Long iflib_rxq_t rxq = ctx->ifc_rxqs; 5450aaeb188aSBjoern A. Zeeb int q; 5451aaeb188aSBjoern A. Zeeb #if defined(INET6) || defined(INET) 5452aaeb188aSBjoern A. Zeeb int i, err; 5453aaeb188aSBjoern A. Zeeb #endif 54544c7070dbSScott Long 54554c7070dbSScott Long for (q = 0; q < ctx->ifc_softc_ctx.isc_nrxqsets; q++, rxq++) { 5456aaeb188aSBjoern A. Zeeb #if defined(INET6) || defined(INET) 54574c7070dbSScott Long tcp_lro_free(&rxq->ifr_lc); 545823ac9029SStephen Hurd if ((err = tcp_lro_init_args(&rxq->ifr_lc, ctx->ifc_ifp, 545923ac9029SStephen Hurd TCP_LRO_ENTRIES, min(1024, 546023ac9029SStephen Hurd ctx->ifc_softc_ctx.isc_nrxd[rxq->ifr_fl_offset]))) != 0) { 54614c7070dbSScott Long device_printf(ctx->ifc_dev, "LRO Initialization failed!\n"); 54624c7070dbSScott Long goto fail; 54634c7070dbSScott Long } 54644c7070dbSScott Long rxq->ifr_lro_enabled = TRUE; 5465aaeb188aSBjoern A. Zeeb #endif 54664c7070dbSScott Long IFDI_RXQ_SETUP(ctx, rxq->ifr_id); 54674c7070dbSScott Long } 54684c7070dbSScott Long return (0); 5469aaeb188aSBjoern A. Zeeb #if defined(INET6) || defined(INET) 54704c7070dbSScott Long fail: 54714c7070dbSScott Long /* 54724c7070dbSScott Long * Free RX software descriptors allocated so far, we will only handle 54734c7070dbSScott Long * the rings that completed, the failing case will have 54744c7070dbSScott Long * cleaned up for itself. 'q' failed, so its the terminus. 54754c7070dbSScott Long */ 54764c7070dbSScott Long rxq = ctx->ifc_rxqs; 54774c7070dbSScott Long for (i = 0; i < q; ++i, rxq++) { 54784c7070dbSScott Long iflib_rx_sds_free(rxq); 54794c7070dbSScott Long rxq->ifr_cq_gen = rxq->ifr_cq_cidx = rxq->ifr_cq_pidx = 0; 54804c7070dbSScott Long } 54814c7070dbSScott Long return (err); 5482aaeb188aSBjoern A. Zeeb #endif 54834c7070dbSScott Long } 54844c7070dbSScott Long 54854c7070dbSScott Long /********************************************************************* 54864c7070dbSScott Long * 54874c7070dbSScott Long * Free all receive rings. 54884c7070dbSScott Long * 54894c7070dbSScott Long **********************************************************************/ 54904c7070dbSScott Long static void 54914c7070dbSScott Long iflib_rx_structures_free(if_ctx_t ctx) 54924c7070dbSScott Long { 54934c7070dbSScott Long iflib_rxq_t rxq = ctx->ifc_rxqs; 54944c7070dbSScott Long 549523ac9029SStephen Hurd for (int i = 0; i < ctx->ifc_softc_ctx.isc_nrxqsets; i++, rxq++) { 54964c7070dbSScott Long iflib_rx_sds_free(rxq); 54974c7070dbSScott Long } 549877c1fcecSEric Joyner free(ctx->ifc_rxqs, M_IFLIB); 549977c1fcecSEric Joyner ctx->ifc_rxqs = NULL; 55004c7070dbSScott Long } 55014c7070dbSScott Long 55024c7070dbSScott Long static int 55034c7070dbSScott Long iflib_qset_structures_setup(if_ctx_t ctx) 55044c7070dbSScott Long { 55054c7070dbSScott Long int err; 55064c7070dbSScott Long 55076108c013SStephen Hurd /* 55086108c013SStephen Hurd * It is expected that the caller takes care of freeing queues if this 55096108c013SStephen Hurd * fails. 55106108c013SStephen Hurd */ 5511ac88e6daSStephen Hurd if ((err = iflib_tx_structures_setup(ctx)) != 0) { 5512ac88e6daSStephen Hurd device_printf(ctx->ifc_dev, "iflib_tx_structures_setup failed: %d\n", err); 55134c7070dbSScott Long return (err); 5514ac88e6daSStephen Hurd } 55154c7070dbSScott Long 55166108c013SStephen Hurd if ((err = iflib_rx_structures_setup(ctx)) != 0) 55174c7070dbSScott Long device_printf(ctx->ifc_dev, "iflib_rx_structures_setup failed: %d\n", err); 55186108c013SStephen Hurd 55194c7070dbSScott Long return (err); 55204c7070dbSScott Long } 55214c7070dbSScott Long 55224c7070dbSScott Long int 55234c7070dbSScott Long iflib_irq_alloc(if_ctx_t ctx, if_irq_t irq, int rid, 55243e0e6330SStephen Hurd driver_filter_t filter, void *filter_arg, driver_intr_t handler, void *arg, const char *name) 55254c7070dbSScott Long { 55264c7070dbSScott Long 55274c7070dbSScott Long return (_iflib_irq_alloc(ctx, irq, rid, filter, handler, arg, name)); 55284c7070dbSScott Long } 55294c7070dbSScott Long 5530b103855eSStephen Hurd #ifdef SMP 5531aa3c5dd8SSean Bruno static int 5532b103855eSStephen Hurd find_nth(if_ctx_t ctx, int qid) 55334c7070dbSScott Long { 5534b103855eSStephen Hurd cpuset_t cpus; 5535aa3c5dd8SSean Bruno int i, cpuid, eqid, count; 55364c7070dbSScott Long 5537b103855eSStephen Hurd CPU_COPY(&ctx->ifc_cpus, &cpus); 5538b103855eSStephen Hurd count = CPU_COUNT(&cpus); 5539aa3c5dd8SSean Bruno eqid = qid % count; 55404c7070dbSScott Long /* clear up to the qid'th bit */ 5541aa3c5dd8SSean Bruno for (i = 0; i < eqid; i++) { 5542b103855eSStephen Hurd cpuid = CPU_FFS(&cpus); 5543aa3c5dd8SSean Bruno MPASS(cpuid != 0); 5544b103855eSStephen Hurd CPU_CLR(cpuid-1, &cpus); 55454c7070dbSScott Long } 5546b103855eSStephen Hurd cpuid = CPU_FFS(&cpus); 5547aa3c5dd8SSean Bruno MPASS(cpuid != 0); 5548aa3c5dd8SSean Bruno return (cpuid-1); 55494c7070dbSScott Long } 55504c7070dbSScott Long 5551b103855eSStephen Hurd #ifdef SCHED_ULE 5552b103855eSStephen Hurd extern struct cpu_group *cpu_top; /* CPU topology */ 5553b103855eSStephen Hurd 5554b103855eSStephen Hurd static int 5555b103855eSStephen Hurd find_child_with_core(int cpu, struct cpu_group *grp) 5556b103855eSStephen Hurd { 5557b103855eSStephen Hurd int i; 5558b103855eSStephen Hurd 5559b103855eSStephen Hurd if (grp->cg_children == 0) 5560b103855eSStephen Hurd return -1; 5561b103855eSStephen Hurd 5562b103855eSStephen Hurd MPASS(grp->cg_child); 5563b103855eSStephen Hurd for (i = 0; i < grp->cg_children; i++) { 5564b103855eSStephen Hurd if (CPU_ISSET(cpu, &grp->cg_child[i].cg_mask)) 5565b103855eSStephen Hurd return i; 5566b103855eSStephen Hurd } 5567b103855eSStephen Hurd 5568b103855eSStephen Hurd return -1; 5569b103855eSStephen Hurd } 5570b103855eSStephen Hurd 5571b103855eSStephen Hurd /* 55720b75ac77SStephen Hurd * Find the nth "close" core to the specified core 55730b75ac77SStephen Hurd * "close" is defined as the deepest level that shares 55740b75ac77SStephen Hurd * at least an L2 cache. With threads, this will be 55750b75ac77SStephen Hurd * threads on the same core. If the sahred cache is L3 55760b75ac77SStephen Hurd * or higher, simply returns the same core. 5577b103855eSStephen Hurd */ 5578b103855eSStephen Hurd static int 55790b75ac77SStephen Hurd find_close_core(int cpu, int core_offset) 5580b103855eSStephen Hurd { 5581b103855eSStephen Hurd struct cpu_group *grp; 5582b103855eSStephen Hurd int i; 55830b75ac77SStephen Hurd int fcpu; 5584b103855eSStephen Hurd cpuset_t cs; 5585b103855eSStephen Hurd 5586b103855eSStephen Hurd grp = cpu_top; 5587b103855eSStephen Hurd if (grp == NULL) 5588b103855eSStephen Hurd return cpu; 5589b103855eSStephen Hurd i = 0; 5590b103855eSStephen Hurd while ((i = find_child_with_core(cpu, grp)) != -1) { 5591b103855eSStephen Hurd /* If the child only has one cpu, don't descend */ 5592b103855eSStephen Hurd if (grp->cg_child[i].cg_count <= 1) 5593b103855eSStephen Hurd break; 5594b103855eSStephen Hurd grp = &grp->cg_child[i]; 5595b103855eSStephen Hurd } 5596b103855eSStephen Hurd 5597b103855eSStephen Hurd /* If they don't share at least an L2 cache, use the same CPU */ 5598b103855eSStephen Hurd if (grp->cg_level > CG_SHARE_L2 || grp->cg_level == CG_SHARE_NONE) 5599b103855eSStephen Hurd return cpu; 5600b103855eSStephen Hurd 5601b103855eSStephen Hurd /* Now pick one */ 5602b103855eSStephen Hurd CPU_COPY(&grp->cg_mask, &cs); 56030b75ac77SStephen Hurd 56040b75ac77SStephen Hurd /* Add the selected CPU offset to core offset. */ 56050b75ac77SStephen Hurd for (i = 0; (fcpu = CPU_FFS(&cs)) != 0; i++) { 56060b75ac77SStephen Hurd if (fcpu - 1 == cpu) 56070b75ac77SStephen Hurd break; 56080b75ac77SStephen Hurd CPU_CLR(fcpu - 1, &cs); 56090b75ac77SStephen Hurd } 56100b75ac77SStephen Hurd MPASS(fcpu); 56110b75ac77SStephen Hurd 56120b75ac77SStephen Hurd core_offset += i; 56130b75ac77SStephen Hurd 56140b75ac77SStephen Hurd CPU_COPY(&grp->cg_mask, &cs); 56150b75ac77SStephen Hurd for (i = core_offset % grp->cg_count; i > 0; i--) { 5616b103855eSStephen Hurd MPASS(CPU_FFS(&cs)); 5617b103855eSStephen Hurd CPU_CLR(CPU_FFS(&cs) - 1, &cs); 5618b103855eSStephen Hurd } 5619b103855eSStephen Hurd MPASS(CPU_FFS(&cs)); 5620b103855eSStephen Hurd return CPU_FFS(&cs) - 1; 5621b103855eSStephen Hurd } 5622b103855eSStephen Hurd #else 5623b103855eSStephen Hurd static int 56240b75ac77SStephen Hurd find_close_core(int cpu, int core_offset __unused) 5625b103855eSStephen Hurd { 562697755e83SKonstantin Belousov return cpu; 5627b103855eSStephen Hurd } 5628b103855eSStephen Hurd #endif 5629b103855eSStephen Hurd 5630b103855eSStephen Hurd static int 56310b75ac77SStephen Hurd get_core_offset(if_ctx_t ctx, iflib_intr_type_t type, int qid) 5632b103855eSStephen Hurd { 5633b103855eSStephen Hurd switch (type) { 5634b103855eSStephen Hurd case IFLIB_INTR_TX: 56350b75ac77SStephen Hurd /* TX queues get cores which share at least an L2 cache with the corresponding RX queue */ 56360b75ac77SStephen Hurd /* XXX handle multiple RX threads per core and more than two core per L2 group */ 5637b103855eSStephen Hurd return qid / CPU_COUNT(&ctx->ifc_cpus) + 1; 5638b103855eSStephen Hurd case IFLIB_INTR_RX: 5639b103855eSStephen Hurd case IFLIB_INTR_RXTX: 56400b75ac77SStephen Hurd /* RX queues get the specified core */ 5641b103855eSStephen Hurd return qid / CPU_COUNT(&ctx->ifc_cpus); 5642b103855eSStephen Hurd default: 5643b103855eSStephen Hurd return -1; 5644b103855eSStephen Hurd } 5645b103855eSStephen Hurd } 5646b103855eSStephen Hurd #else 56470b75ac77SStephen Hurd #define get_core_offset(ctx, type, qid) CPU_FIRST() 56480b75ac77SStephen Hurd #define find_close_core(cpuid, tid) CPU_FIRST() 5649b103855eSStephen Hurd #define find_nth(ctx, gid) CPU_FIRST() 5650b103855eSStephen Hurd #endif 5651b103855eSStephen Hurd 5652b103855eSStephen Hurd /* Just to avoid copy/paste */ 5653b103855eSStephen Hurd static inline int 5654f855ec81SMarius Strobl iflib_irq_set_affinity(if_ctx_t ctx, if_irq_t irq, iflib_intr_type_t type, 5655f855ec81SMarius Strobl int qid, struct grouptask *gtask, struct taskqgroup *tqg, void *uniq, 5656f855ec81SMarius Strobl const char *name) 5657b103855eSStephen Hurd { 5658f855ec81SMarius Strobl device_t dev; 5659f855ec81SMarius Strobl int err, cpuid, tid; 5660b103855eSStephen Hurd 5661f855ec81SMarius Strobl dev = ctx->ifc_dev; 5662b103855eSStephen Hurd cpuid = find_nth(ctx, qid); 56630b75ac77SStephen Hurd tid = get_core_offset(ctx, type, qid); 5664b103855eSStephen Hurd MPASS(tid >= 0); 56650b75ac77SStephen Hurd cpuid = find_close_core(cpuid, tid); 5666f855ec81SMarius Strobl err = taskqgroup_attach_cpu(tqg, gtask, uniq, cpuid, dev, irq->ii_res, 5667f855ec81SMarius Strobl name); 5668b103855eSStephen Hurd if (err) { 5669f855ec81SMarius Strobl device_printf(dev, "taskqgroup_attach_cpu failed %d\n", err); 5670b103855eSStephen Hurd return (err); 5671b103855eSStephen Hurd } 5672b103855eSStephen Hurd #ifdef notyet 5673b103855eSStephen Hurd if (cpuid > ctx->ifc_cpuid_highest) 5674b103855eSStephen Hurd ctx->ifc_cpuid_highest = cpuid; 5675b103855eSStephen Hurd #endif 5676b103855eSStephen Hurd return 0; 5677b103855eSStephen Hurd } 5678b103855eSStephen Hurd 56794c7070dbSScott Long int 56804c7070dbSScott Long iflib_irq_alloc_generic(if_ctx_t ctx, if_irq_t irq, int rid, 56814c7070dbSScott Long iflib_intr_type_t type, driver_filter_t *filter, 56823e0e6330SStephen Hurd void *filter_arg, int qid, const char *name) 56834c7070dbSScott Long { 5684f855ec81SMarius Strobl device_t dev; 56854c7070dbSScott Long struct grouptask *gtask; 56864c7070dbSScott Long struct taskqgroup *tqg; 56874c7070dbSScott Long iflib_filter_info_t info; 568823ac9029SStephen Hurd gtask_fn_t *fn; 5689b103855eSStephen Hurd int tqrid, err; 569095246abbSSean Bruno driver_filter_t *intr_fast; 56914c7070dbSScott Long void *q; 56924c7070dbSScott Long 56934c7070dbSScott Long info = &ctx->ifc_filter_info; 5694add6f7d0SSean Bruno tqrid = rid; 56954c7070dbSScott Long 56964c7070dbSScott Long switch (type) { 56974c7070dbSScott Long /* XXX merge tx/rx for netmap? */ 56984c7070dbSScott Long case IFLIB_INTR_TX: 56994c7070dbSScott Long q = &ctx->ifc_txqs[qid]; 57004c7070dbSScott Long info = &ctx->ifc_txqs[qid].ift_filter_info; 57014c7070dbSScott Long gtask = &ctx->ifc_txqs[qid].ift_task; 5702ab2e3f79SStephen Hurd tqg = qgroup_if_io_tqg; 57034c7070dbSScott Long fn = _task_fn_tx; 570495246abbSSean Bruno intr_fast = iflib_fast_intr; 5705da69b8f9SSean Bruno GROUPTASK_INIT(gtask, 0, fn, q); 57065ee36c68SStephen Hurd ctx->ifc_flags |= IFC_NETMAP_TX_IRQ; 57074c7070dbSScott Long break; 57084c7070dbSScott Long case IFLIB_INTR_RX: 57094c7070dbSScott Long q = &ctx->ifc_rxqs[qid]; 57104c7070dbSScott Long info = &ctx->ifc_rxqs[qid].ifr_filter_info; 57114c7070dbSScott Long gtask = &ctx->ifc_rxqs[qid].ifr_task; 5712ab2e3f79SStephen Hurd tqg = qgroup_if_io_tqg; 57134c7070dbSScott Long fn = _task_fn_rx; 5714ab2e3f79SStephen Hurd intr_fast = iflib_fast_intr; 571595246abbSSean Bruno GROUPTASK_INIT(gtask, 0, fn, q); 571695246abbSSean Bruno break; 571795246abbSSean Bruno case IFLIB_INTR_RXTX: 571895246abbSSean Bruno q = &ctx->ifc_rxqs[qid]; 571995246abbSSean Bruno info = &ctx->ifc_rxqs[qid].ifr_filter_info; 572095246abbSSean Bruno gtask = &ctx->ifc_rxqs[qid].ifr_task; 5721ab2e3f79SStephen Hurd tqg = qgroup_if_io_tqg; 572295246abbSSean Bruno fn = _task_fn_rx; 572395246abbSSean Bruno intr_fast = iflib_fast_intr_rxtx; 5724da69b8f9SSean Bruno GROUPTASK_INIT(gtask, 0, fn, q); 57254c7070dbSScott Long break; 57264c7070dbSScott Long case IFLIB_INTR_ADMIN: 57274c7070dbSScott Long q = ctx; 5728da69b8f9SSean Bruno tqrid = -1; 57294c7070dbSScott Long info = &ctx->ifc_filter_info; 57304c7070dbSScott Long gtask = &ctx->ifc_admin_task; 5731ab2e3f79SStephen Hurd tqg = qgroup_if_config_tqg; 57324c7070dbSScott Long fn = _task_fn_admin; 573395246abbSSean Bruno intr_fast = iflib_fast_intr_ctx; 57344c7070dbSScott Long break; 57354c7070dbSScott Long default: 57364c7070dbSScott Long panic("unknown net intr type"); 57374c7070dbSScott Long } 57384c7070dbSScott Long 57394c7070dbSScott Long info->ifi_filter = filter; 57404c7070dbSScott Long info->ifi_filter_arg = filter_arg; 57414c7070dbSScott Long info->ifi_task = gtask; 574295246abbSSean Bruno info->ifi_ctx = q; 57434c7070dbSScott Long 5744f855ec81SMarius Strobl dev = ctx->ifc_dev; 574595246abbSSean Bruno err = _iflib_irq_alloc(ctx, irq, rid, intr_fast, NULL, info, name); 5746da69b8f9SSean Bruno if (err != 0) { 5747f855ec81SMarius Strobl device_printf(dev, "_iflib_irq_alloc failed %d\n", err); 57484c7070dbSScott Long return (err); 5749da69b8f9SSean Bruno } 5750da69b8f9SSean Bruno if (type == IFLIB_INTR_ADMIN) 5751da69b8f9SSean Bruno return (0); 5752da69b8f9SSean Bruno 57534c7070dbSScott Long if (tqrid != -1) { 5754f855ec81SMarius Strobl err = iflib_irq_set_affinity(ctx, irq, type, qid, gtask, tqg, 5755f855ec81SMarius Strobl q, name); 5756b103855eSStephen Hurd if (err) 5757b103855eSStephen Hurd return (err); 5758aa3c5dd8SSean Bruno } else { 5759f855ec81SMarius Strobl taskqgroup_attach(tqg, gtask, q, dev, irq->ii_res, name); 5760aa3c5dd8SSean Bruno } 57614c7070dbSScott Long 57624c7070dbSScott Long return (0); 57634c7070dbSScott Long } 57644c7070dbSScott Long 57654c7070dbSScott Long void 57663e0e6330SStephen 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) 57674c7070dbSScott Long { 57684c7070dbSScott Long struct grouptask *gtask; 57694c7070dbSScott Long struct taskqgroup *tqg; 577023ac9029SStephen Hurd gtask_fn_t *fn; 57714c7070dbSScott Long void *q; 5772b103855eSStephen Hurd int err; 57734c7070dbSScott Long 57744c7070dbSScott Long switch (type) { 57754c7070dbSScott Long case IFLIB_INTR_TX: 57764c7070dbSScott Long q = &ctx->ifc_txqs[qid]; 57774c7070dbSScott Long gtask = &ctx->ifc_txqs[qid].ift_task; 5778ab2e3f79SStephen Hurd tqg = qgroup_if_io_tqg; 57794c7070dbSScott Long fn = _task_fn_tx; 57804c7070dbSScott Long break; 57814c7070dbSScott Long case IFLIB_INTR_RX: 57824c7070dbSScott Long q = &ctx->ifc_rxqs[qid]; 57834c7070dbSScott Long gtask = &ctx->ifc_rxqs[qid].ifr_task; 5784ab2e3f79SStephen Hurd tqg = qgroup_if_io_tqg; 57854c7070dbSScott Long fn = _task_fn_rx; 57864c7070dbSScott Long break; 57874c7070dbSScott Long case IFLIB_INTR_IOV: 57884c7070dbSScott Long q = ctx; 57894c7070dbSScott Long gtask = &ctx->ifc_vflr_task; 5790ab2e3f79SStephen Hurd tqg = qgroup_if_config_tqg; 57914c7070dbSScott Long fn = _task_fn_iov; 57924c7070dbSScott Long break; 57934c7070dbSScott Long default: 57944c7070dbSScott Long panic("unknown net intr type"); 57954c7070dbSScott Long } 57964c7070dbSScott Long GROUPTASK_INIT(gtask, 0, fn, q); 5797f855ec81SMarius Strobl if (irq != NULL) { 5798f855ec81SMarius Strobl err = iflib_irq_set_affinity(ctx, irq, type, qid, gtask, tqg, 5799f855ec81SMarius Strobl q, name); 5800b103855eSStephen Hurd if (err) 5801f855ec81SMarius Strobl taskqgroup_attach(tqg, gtask, q, ctx->ifc_dev, 5802f855ec81SMarius Strobl irq->ii_res, name); 5803f855ec81SMarius Strobl } else { 5804f855ec81SMarius Strobl taskqgroup_attach(tqg, gtask, q, NULL, NULL, name); 5805b103855eSStephen Hurd } 5806b103855eSStephen Hurd } 58074c7070dbSScott Long 58084c7070dbSScott Long void 58094c7070dbSScott Long iflib_irq_free(if_ctx_t ctx, if_irq_t irq) 58104c7070dbSScott Long { 5811b97de13aSMarius Strobl 58124c7070dbSScott Long if (irq->ii_tag) 58134c7070dbSScott Long bus_teardown_intr(ctx->ifc_dev, irq->ii_res, irq->ii_tag); 58144c7070dbSScott Long 58154c7070dbSScott Long if (irq->ii_res) 5816b97de13aSMarius Strobl bus_release_resource(ctx->ifc_dev, SYS_RES_IRQ, 5817b97de13aSMarius Strobl rman_get_rid(irq->ii_res), irq->ii_res); 58184c7070dbSScott Long } 58194c7070dbSScott Long 58204c7070dbSScott Long static int 58213e0e6330SStephen Hurd iflib_legacy_setup(if_ctx_t ctx, driver_filter_t filter, void *filter_arg, int *rid, const char *name) 58224c7070dbSScott Long { 58234c7070dbSScott Long iflib_txq_t txq = ctx->ifc_txqs; 58244c7070dbSScott Long iflib_rxq_t rxq = ctx->ifc_rxqs; 58254c7070dbSScott Long if_irq_t irq = &ctx->ifc_legacy_irq; 58264c7070dbSScott Long iflib_filter_info_t info; 5827f855ec81SMarius Strobl device_t dev; 58284c7070dbSScott Long struct grouptask *gtask; 5829f855ec81SMarius Strobl struct resource *res; 58304c7070dbSScott Long struct taskqgroup *tqg; 583123ac9029SStephen Hurd gtask_fn_t *fn; 58324c7070dbSScott Long int tqrid; 58334c7070dbSScott Long void *q; 58344c7070dbSScott Long int err; 58354c7070dbSScott Long 58364c7070dbSScott Long q = &ctx->ifc_rxqs[0]; 58374c7070dbSScott Long info = &rxq[0].ifr_filter_info; 58384c7070dbSScott Long gtask = &rxq[0].ifr_task; 5839ab2e3f79SStephen Hurd tqg = qgroup_if_io_tqg; 58404c7070dbSScott Long tqrid = irq->ii_rid = *rid; 58414c7070dbSScott Long fn = _task_fn_rx; 58424c7070dbSScott Long 58434c7070dbSScott Long ctx->ifc_flags |= IFC_LEGACY; 58444c7070dbSScott Long info->ifi_filter = filter; 58454c7070dbSScott Long info->ifi_filter_arg = filter_arg; 58464c7070dbSScott Long info->ifi_task = gtask; 58474ecb427aSSean Bruno info->ifi_ctx = ctx; 58484c7070dbSScott Long 5849f855ec81SMarius Strobl dev = ctx->ifc_dev; 58504c7070dbSScott Long /* We allocate a single interrupt resource */ 585195246abbSSean Bruno if ((err = _iflib_irq_alloc(ctx, irq, tqrid, iflib_fast_intr_ctx, NULL, info, name)) != 0) 58524c7070dbSScott Long return (err); 58534c7070dbSScott Long GROUPTASK_INIT(gtask, 0, fn, q); 5854f855ec81SMarius Strobl res = irq->ii_res; 5855f855ec81SMarius Strobl taskqgroup_attach(tqg, gtask, q, dev, res, name); 58564c7070dbSScott Long 58574c7070dbSScott Long GROUPTASK_INIT(&txq->ift_task, 0, _task_fn_tx, txq); 5858f855ec81SMarius Strobl taskqgroup_attach(qgroup_if_io_tqg, &txq->ift_task, txq, dev, res, 5859f855ec81SMarius Strobl "tx"); 58604c7070dbSScott Long return (0); 58614c7070dbSScott Long } 58624c7070dbSScott Long 58634c7070dbSScott Long void 58644c7070dbSScott Long iflib_led_create(if_ctx_t ctx) 58654c7070dbSScott Long { 58664c7070dbSScott Long 58674c7070dbSScott Long ctx->ifc_led_dev = led_create(iflib_led_func, ctx, 58684c7070dbSScott Long device_get_nameunit(ctx->ifc_dev)); 58694c7070dbSScott Long } 58704c7070dbSScott Long 58714c7070dbSScott Long void 58724c7070dbSScott Long iflib_tx_intr_deferred(if_ctx_t ctx, int txqid) 58734c7070dbSScott Long { 58744c7070dbSScott Long 58754c7070dbSScott Long GROUPTASK_ENQUEUE(&ctx->ifc_txqs[txqid].ift_task); 58764c7070dbSScott Long } 58774c7070dbSScott Long 58784c7070dbSScott Long void 58794c7070dbSScott Long iflib_rx_intr_deferred(if_ctx_t ctx, int rxqid) 58804c7070dbSScott Long { 58814c7070dbSScott Long 58824c7070dbSScott Long GROUPTASK_ENQUEUE(&ctx->ifc_rxqs[rxqid].ifr_task); 58834c7070dbSScott Long } 58844c7070dbSScott Long 58854c7070dbSScott Long void 58864c7070dbSScott Long iflib_admin_intr_deferred(if_ctx_t ctx) 58874c7070dbSScott Long { 58881248952aSSean Bruno #ifdef INVARIANTS 58891248952aSSean Bruno struct grouptask *gtask; 589046fa0c25SEric Joyner 58911248952aSSean Bruno gtask = &ctx->ifc_admin_task; 5892d0d0ad0aSStephen Hurd MPASS(gtask != NULL && gtask->gt_taskqueue != NULL); 58931248952aSSean Bruno #endif 58944c7070dbSScott Long 58954c7070dbSScott Long GROUPTASK_ENQUEUE(&ctx->ifc_admin_task); 58964c7070dbSScott Long } 58974c7070dbSScott Long 58984c7070dbSScott Long void 58994c7070dbSScott Long iflib_iov_intr_deferred(if_ctx_t ctx) 59004c7070dbSScott Long { 59014c7070dbSScott Long 59024c7070dbSScott Long GROUPTASK_ENQUEUE(&ctx->ifc_vflr_task); 59034c7070dbSScott Long } 59044c7070dbSScott Long 59054c7070dbSScott Long void 59064c7070dbSScott Long iflib_io_tqg_attach(struct grouptask *gt, void *uniq, int cpu, char *name) 59074c7070dbSScott Long { 59084c7070dbSScott Long 5909f855ec81SMarius Strobl taskqgroup_attach_cpu(qgroup_if_io_tqg, gt, uniq, cpu, NULL, NULL, 5910f855ec81SMarius Strobl name); 59114c7070dbSScott Long } 59124c7070dbSScott Long 59134c7070dbSScott Long void 5914aa8a24d3SStephen Hurd iflib_config_gtask_init(void *ctx, struct grouptask *gtask, gtask_fn_t *fn, 5915aa8a24d3SStephen Hurd const char *name) 59164c7070dbSScott Long { 59174c7070dbSScott Long 59184c7070dbSScott Long GROUPTASK_INIT(gtask, 0, fn, ctx); 5919f855ec81SMarius Strobl taskqgroup_attach(qgroup_if_config_tqg, gtask, gtask, NULL, NULL, 5920f855ec81SMarius Strobl name); 59214c7070dbSScott Long } 59224c7070dbSScott Long 59234c7070dbSScott Long void 592423ac9029SStephen Hurd iflib_config_gtask_deinit(struct grouptask *gtask) 592523ac9029SStephen Hurd { 592623ac9029SStephen Hurd 5927ab2e3f79SStephen Hurd taskqgroup_detach(qgroup_if_config_tqg, gtask); 592823ac9029SStephen Hurd } 592923ac9029SStephen Hurd 593023ac9029SStephen Hurd void 593123ac9029SStephen Hurd iflib_link_state_change(if_ctx_t ctx, int link_state, uint64_t baudrate) 59324c7070dbSScott Long { 59334c7070dbSScott Long if_t ifp = ctx->ifc_ifp; 59344c7070dbSScott Long iflib_txq_t txq = ctx->ifc_txqs; 59354c7070dbSScott Long 59364c7070dbSScott Long if_setbaudrate(ifp, baudrate); 59377b610b60SSean Bruno if (baudrate >= IF_Gbps(10)) { 59387b610b60SSean Bruno STATE_LOCK(ctx); 593995246abbSSean Bruno ctx->ifc_flags |= IFC_PREFETCH; 59407b610b60SSean Bruno STATE_UNLOCK(ctx); 59417b610b60SSean Bruno } 59424c7070dbSScott Long /* If link down, disable watchdog */ 59434c7070dbSScott Long if ((ctx->ifc_link_state == LINK_STATE_UP) && (link_state == LINK_STATE_DOWN)) { 59444c7070dbSScott Long for (int i = 0; i < ctx->ifc_softc_ctx.isc_ntxqsets; i++, txq++) 59454c7070dbSScott Long txq->ift_qstatus = IFLIB_QUEUE_IDLE; 59464c7070dbSScott Long } 59474c7070dbSScott Long ctx->ifc_link_state = link_state; 59484c7070dbSScott Long if_link_state_change(ifp, link_state); 59494c7070dbSScott Long } 59504c7070dbSScott Long 59514c7070dbSScott Long static int 59524c7070dbSScott Long iflib_tx_credits_update(if_ctx_t ctx, iflib_txq_t txq) 59534c7070dbSScott Long { 59544c7070dbSScott Long int credits; 59551248952aSSean Bruno #ifdef INVARIANTS 59561248952aSSean Bruno int credits_pre = txq->ift_cidx_processed; 59571248952aSSean Bruno #endif 59584c7070dbSScott Long 59594c7070dbSScott Long if (ctx->isc_txd_credits_update == NULL) 59604c7070dbSScott Long return (0); 59614c7070dbSScott Long 59628a04b53dSKonstantin Belousov bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map, 59638a04b53dSKonstantin Belousov BUS_DMASYNC_POSTREAD); 596495246abbSSean Bruno if ((credits = ctx->isc_txd_credits_update(ctx->ifc_softc, txq->ift_id, true)) == 0) 59654c7070dbSScott Long return (0); 59664c7070dbSScott Long 59674c7070dbSScott Long txq->ift_processed += credits; 59684c7070dbSScott Long txq->ift_cidx_processed += credits; 59694c7070dbSScott Long 59701248952aSSean Bruno MPASS(credits_pre + credits == txq->ift_cidx_processed); 59714c7070dbSScott Long if (txq->ift_cidx_processed >= txq->ift_size) 59724c7070dbSScott Long txq->ift_cidx_processed -= txq->ift_size; 59734c7070dbSScott Long return (credits); 59744c7070dbSScott Long } 59754c7070dbSScott Long 59764c7070dbSScott Long static int 597795246abbSSean Bruno iflib_rxd_avail(if_ctx_t ctx, iflib_rxq_t rxq, qidx_t cidx, qidx_t budget) 59784c7070dbSScott Long { 597995dcf343SMarius Strobl iflib_fl_t fl; 598095dcf343SMarius Strobl u_int i; 59814c7070dbSScott Long 598295dcf343SMarius Strobl for (i = 0, fl = &rxq->ifr_fl[0]; i < rxq->ifr_nfl; i++, fl++) 598395dcf343SMarius Strobl bus_dmamap_sync(fl->ifl_ifdi->idi_tag, fl->ifl_ifdi->idi_map, 598495dcf343SMarius Strobl BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 598523ac9029SStephen Hurd return (ctx->isc_rxd_available(ctx->ifc_softc, rxq->ifr_id, cidx, 598623ac9029SStephen Hurd budget)); 59874c7070dbSScott Long } 59884c7070dbSScott Long 59894c7070dbSScott Long void 59904c7070dbSScott Long iflib_add_int_delay_sysctl(if_ctx_t ctx, const char *name, 59914c7070dbSScott Long const char *description, if_int_delay_info_t info, 59924c7070dbSScott Long int offset, int value) 59934c7070dbSScott Long { 59944c7070dbSScott Long info->iidi_ctx = ctx; 59954c7070dbSScott Long info->iidi_offset = offset; 59964c7070dbSScott Long info->iidi_value = value; 59974c7070dbSScott Long SYSCTL_ADD_PROC(device_get_sysctl_ctx(ctx->ifc_dev), 59984c7070dbSScott Long SYSCTL_CHILDREN(device_get_sysctl_tree(ctx->ifc_dev)), 59994c7070dbSScott Long OID_AUTO, name, CTLTYPE_INT|CTLFLAG_RW, 60004c7070dbSScott Long info, 0, iflib_sysctl_int_delay, "I", description); 60014c7070dbSScott Long } 60024c7070dbSScott Long 6003aa8a24d3SStephen Hurd struct sx * 60044c7070dbSScott Long iflib_ctx_lock_get(if_ctx_t ctx) 60054c7070dbSScott Long { 60064c7070dbSScott Long 6007aa8a24d3SStephen Hurd return (&ctx->ifc_ctx_sx); 60084c7070dbSScott Long } 60094c7070dbSScott Long 60104c7070dbSScott Long static int 60114c7070dbSScott Long iflib_msix_init(if_ctx_t ctx) 60124c7070dbSScott Long { 60134c7070dbSScott Long device_t dev = ctx->ifc_dev; 60144c7070dbSScott Long if_shared_ctx_t sctx = ctx->ifc_sctx; 60154c7070dbSScott Long if_softc_ctx_t scctx = &ctx->ifc_softc_ctx; 60164c7070dbSScott Long int vectors, queues, rx_queues, tx_queues, queuemsgs, msgs; 60174c7070dbSScott Long int iflib_num_tx_queues, iflib_num_rx_queues; 60184c7070dbSScott Long int err, admincnt, bar; 60194c7070dbSScott Long 6020d2735264SStephen Hurd iflib_num_tx_queues = ctx->ifc_sysctl_ntxqs; 6021d2735264SStephen Hurd iflib_num_rx_queues = ctx->ifc_sysctl_nrxqs; 602223ac9029SStephen Hurd 6023b97de13aSMarius Strobl if (bootverbose) 6024b97de13aSMarius Strobl device_printf(dev, "msix_init qsets capped at %d\n", 6025b97de13aSMarius Strobl imax(scctx->isc_ntxqsets, scctx->isc_nrxqsets)); 60261248952aSSean Bruno 60274c7070dbSScott Long bar = ctx->ifc_softc_ctx.isc_msix_bar; 60284c7070dbSScott Long admincnt = sctx->isc_admin_intrcnt; 60294c7070dbSScott Long /* Override by tuneable */ 6030ea351d3fSSean Bruno if (scctx->isc_disable_msix) 60314c7070dbSScott Long goto msi; 60324c7070dbSScott Long 6033b97de13aSMarius Strobl /* First try MSI-X */ 6034b97de13aSMarius Strobl if ((msgs = pci_msix_count(dev)) == 0) { 6035b97de13aSMarius Strobl if (bootverbose) 6036b97de13aSMarius Strobl device_printf(dev, "MSI-X not supported or disabled\n"); 6037b97de13aSMarius Strobl goto msi; 6038b97de13aSMarius Strobl } 60394c7070dbSScott Long /* 60404c7070dbSScott Long * bar == -1 => "trust me I know what I'm doing" 60414c7070dbSScott Long * Some drivers are for hardware that is so shoddily 60424c7070dbSScott Long * documented that no one knows which bars are which 60434c7070dbSScott Long * so the developer has to map all bars. This hack 6044b97de13aSMarius Strobl * allows shoddy garbage to use MSI-X in this framework. 60454c7070dbSScott Long */ 60464c7070dbSScott Long if (bar != -1) { 60474c7070dbSScott Long ctx->ifc_msix_mem = bus_alloc_resource_any(dev, 60484c7070dbSScott Long SYS_RES_MEMORY, &bar, RF_ACTIVE); 60494c7070dbSScott Long if (ctx->ifc_msix_mem == NULL) { 6050b97de13aSMarius Strobl device_printf(dev, "Unable to map MSI-X table\n"); 60514c7070dbSScott Long goto msi; 60524c7070dbSScott Long } 60534c7070dbSScott Long } 60544c7070dbSScott Long #if IFLIB_DEBUG 60554c7070dbSScott Long /* use only 1 qset in debug mode */ 60564c7070dbSScott Long queuemsgs = min(msgs - admincnt, 1); 60574c7070dbSScott Long #else 60584c7070dbSScott Long queuemsgs = msgs - admincnt; 60594c7070dbSScott Long #endif 60604c7070dbSScott Long #ifdef RSS 60614c7070dbSScott Long queues = imin(queuemsgs, rss_getnumbuckets()); 60624c7070dbSScott Long #else 60634c7070dbSScott Long queues = queuemsgs; 60644c7070dbSScott Long #endif 60654c7070dbSScott Long queues = imin(CPU_COUNT(&ctx->ifc_cpus), queues); 6066b97de13aSMarius Strobl if (bootverbose) 6067b97de13aSMarius Strobl device_printf(dev, 6068b97de13aSMarius Strobl "intr CPUs: %d queue msgs: %d admincnt: %d\n", 60694c7070dbSScott Long CPU_COUNT(&ctx->ifc_cpus), queuemsgs, admincnt); 60704c7070dbSScott Long #ifdef RSS 60714c7070dbSScott Long /* If we're doing RSS, clamp at the number of RSS buckets */ 60724c7070dbSScott Long if (queues > rss_getnumbuckets()) 60734c7070dbSScott Long queues = rss_getnumbuckets(); 60744c7070dbSScott Long #endif 607523ac9029SStephen Hurd if (iflib_num_rx_queues > 0 && iflib_num_rx_queues < queuemsgs - admincnt) 607623ac9029SStephen Hurd rx_queues = iflib_num_rx_queues; 60774c7070dbSScott Long else 60784c7070dbSScott Long rx_queues = queues; 6079d2735264SStephen Hurd 6080d2735264SStephen Hurd if (rx_queues > scctx->isc_nrxqsets) 6081d2735264SStephen Hurd rx_queues = scctx->isc_nrxqsets; 6082d2735264SStephen Hurd 608323ac9029SStephen Hurd /* 608423ac9029SStephen Hurd * We want this to be all logical CPUs by default 608523ac9029SStephen Hurd */ 60864c7070dbSScott Long if (iflib_num_tx_queues > 0 && iflib_num_tx_queues < queues) 60874c7070dbSScott Long tx_queues = iflib_num_tx_queues; 60884c7070dbSScott Long else 608923ac9029SStephen Hurd tx_queues = mp_ncpus; 609023ac9029SStephen Hurd 6091d2735264SStephen Hurd if (tx_queues > scctx->isc_ntxqsets) 6092d2735264SStephen Hurd tx_queues = scctx->isc_ntxqsets; 6093d2735264SStephen Hurd 609423ac9029SStephen Hurd if (ctx->ifc_sysctl_qs_eq_override == 0) { 609523ac9029SStephen Hurd #ifdef INVARIANTS 609623ac9029SStephen Hurd if (tx_queues != rx_queues) 609777c1fcecSEric Joyner device_printf(dev, 609877c1fcecSEric Joyner "queue equality override not set, capping rx_queues at %d and tx_queues at %d\n", 609923ac9029SStephen Hurd min(rx_queues, tx_queues), min(rx_queues, tx_queues)); 610023ac9029SStephen Hurd #endif 610123ac9029SStephen Hurd tx_queues = min(rx_queues, tx_queues); 610223ac9029SStephen Hurd rx_queues = min(rx_queues, tx_queues); 610323ac9029SStephen Hurd } 61044c7070dbSScott Long 6105b97de13aSMarius Strobl device_printf(dev, "Using %d rx queues %d tx queues\n", 6106b97de13aSMarius Strobl rx_queues, tx_queues); 61074c7070dbSScott Long 6108ab2e3f79SStephen Hurd vectors = rx_queues + admincnt; 61094c7070dbSScott Long if ((err = pci_alloc_msix(dev, &vectors)) == 0) { 6110b97de13aSMarius Strobl device_printf(dev, "Using MSI-X interrupts with %d vectors\n", 6111b97de13aSMarius Strobl vectors); 61124c7070dbSScott Long scctx->isc_vectors = vectors; 61134c7070dbSScott Long scctx->isc_nrxqsets = rx_queues; 61144c7070dbSScott Long scctx->isc_ntxqsets = tx_queues; 61154c7070dbSScott Long scctx->isc_intr = IFLIB_INTR_MSIX; 611623ac9029SStephen Hurd 61174c7070dbSScott Long return (vectors); 61184c7070dbSScott Long } else { 611977c1fcecSEric Joyner device_printf(dev, 6120b97de13aSMarius Strobl "failed to allocate %d MSI-X vectors, err: %d - using MSI\n", 6121b97de13aSMarius Strobl vectors, err); 6122e4defe55SMarius Strobl bus_release_resource(dev, SYS_RES_MEMORY, bar, 6123e4defe55SMarius Strobl ctx->ifc_msix_mem); 6124e4defe55SMarius Strobl ctx->ifc_msix_mem = NULL; 61254c7070dbSScott Long } 61264c7070dbSScott Long msi: 61274c7070dbSScott Long vectors = pci_msi_count(dev); 61284c7070dbSScott Long scctx->isc_nrxqsets = 1; 61294c7070dbSScott Long scctx->isc_ntxqsets = 1; 61304c7070dbSScott Long scctx->isc_vectors = vectors; 61314c7070dbSScott Long if (vectors == 1 && pci_alloc_msi(dev, &vectors) == 0) { 61324c7070dbSScott Long device_printf(dev,"Using an MSI interrupt\n"); 61334c7070dbSScott Long scctx->isc_intr = IFLIB_INTR_MSI; 61344c7070dbSScott Long } else { 6135e4defe55SMarius Strobl scctx->isc_vectors = 1; 61364c7070dbSScott Long device_printf(dev,"Using a Legacy interrupt\n"); 61374c7070dbSScott Long scctx->isc_intr = IFLIB_INTR_LEGACY; 61384c7070dbSScott Long } 61394c7070dbSScott Long 61404c7070dbSScott Long return (vectors); 61414c7070dbSScott Long } 61424c7070dbSScott Long 6143e4defe55SMarius Strobl static const char *ring_states[] = { "IDLE", "BUSY", "STALLED", "ABDICATED" }; 61444c7070dbSScott Long 61454c7070dbSScott Long static int 61464c7070dbSScott Long mp_ring_state_handler(SYSCTL_HANDLER_ARGS) 61474c7070dbSScott Long { 61484c7070dbSScott Long int rc; 61494c7070dbSScott Long uint16_t *state = ((uint16_t *)oidp->oid_arg1); 61504c7070dbSScott Long struct sbuf *sb; 6151e4defe55SMarius Strobl const char *ring_state = "UNKNOWN"; 61524c7070dbSScott Long 61534c7070dbSScott Long /* XXX needed ? */ 61544c7070dbSScott Long rc = sysctl_wire_old_buffer(req, 0); 61554c7070dbSScott Long MPASS(rc == 0); 61564c7070dbSScott Long if (rc != 0) 61574c7070dbSScott Long return (rc); 61584c7070dbSScott Long sb = sbuf_new_for_sysctl(NULL, NULL, 80, req); 61594c7070dbSScott Long MPASS(sb != NULL); 61604c7070dbSScott Long if (sb == NULL) 61614c7070dbSScott Long return (ENOMEM); 61624c7070dbSScott Long if (state[3] <= 3) 61634c7070dbSScott Long ring_state = ring_states[state[3]]; 61644c7070dbSScott Long 61654c7070dbSScott Long sbuf_printf(sb, "pidx_head: %04hd pidx_tail: %04hd cidx: %04hd state: %s", 61664c7070dbSScott Long state[0], state[1], state[2], ring_state); 61674c7070dbSScott Long rc = sbuf_finish(sb); 61684c7070dbSScott Long sbuf_delete(sb); 61694c7070dbSScott Long return(rc); 61704c7070dbSScott Long } 61714c7070dbSScott Long 617223ac9029SStephen Hurd enum iflib_ndesc_handler { 617323ac9029SStephen Hurd IFLIB_NTXD_HANDLER, 617423ac9029SStephen Hurd IFLIB_NRXD_HANDLER, 617523ac9029SStephen Hurd }; 61764c7070dbSScott Long 617723ac9029SStephen Hurd static int 617823ac9029SStephen Hurd mp_ndesc_handler(SYSCTL_HANDLER_ARGS) 617923ac9029SStephen Hurd { 618023ac9029SStephen Hurd if_ctx_t ctx = (void *)arg1; 618123ac9029SStephen Hurd enum iflib_ndesc_handler type = arg2; 618223ac9029SStephen Hurd char buf[256] = {0}; 618395246abbSSean Bruno qidx_t *ndesc; 618423ac9029SStephen Hurd char *p, *next; 618523ac9029SStephen Hurd int nqs, rc, i; 618623ac9029SStephen Hurd 618723ac9029SStephen Hurd MPASS(type == IFLIB_NTXD_HANDLER || type == IFLIB_NRXD_HANDLER); 618823ac9029SStephen Hurd 618923ac9029SStephen Hurd nqs = 8; 619023ac9029SStephen Hurd switch(type) { 619123ac9029SStephen Hurd case IFLIB_NTXD_HANDLER: 619223ac9029SStephen Hurd ndesc = ctx->ifc_sysctl_ntxds; 619323ac9029SStephen Hurd if (ctx->ifc_sctx) 619423ac9029SStephen Hurd nqs = ctx->ifc_sctx->isc_ntxqs; 619523ac9029SStephen Hurd break; 619623ac9029SStephen Hurd case IFLIB_NRXD_HANDLER: 619723ac9029SStephen Hurd ndesc = ctx->ifc_sysctl_nrxds; 619823ac9029SStephen Hurd if (ctx->ifc_sctx) 619923ac9029SStephen Hurd nqs = ctx->ifc_sctx->isc_nrxqs; 620023ac9029SStephen Hurd break; 62011ae4848cSMatt Macy default: 62021ae4848cSMatt Macy panic("unhandled type"); 620323ac9029SStephen Hurd } 620423ac9029SStephen Hurd if (nqs == 0) 620523ac9029SStephen Hurd nqs = 8; 620623ac9029SStephen Hurd 620723ac9029SStephen Hurd for (i=0; i<8; i++) { 620823ac9029SStephen Hurd if (i >= nqs) 620923ac9029SStephen Hurd break; 621023ac9029SStephen Hurd if (i) 621123ac9029SStephen Hurd strcat(buf, ","); 621223ac9029SStephen Hurd sprintf(strchr(buf, 0), "%d", ndesc[i]); 621323ac9029SStephen Hurd } 621423ac9029SStephen Hurd 621523ac9029SStephen Hurd rc = sysctl_handle_string(oidp, buf, sizeof(buf), req); 621623ac9029SStephen Hurd if (rc || req->newptr == NULL) 621723ac9029SStephen Hurd return rc; 621823ac9029SStephen Hurd 621923ac9029SStephen Hurd for (i = 0, next = buf, p = strsep(&next, " ,"); i < 8 && p; 622023ac9029SStephen Hurd i++, p = strsep(&next, " ,")) { 622123ac9029SStephen Hurd ndesc[i] = strtoul(p, NULL, 10); 622223ac9029SStephen Hurd } 622323ac9029SStephen Hurd 622423ac9029SStephen Hurd return(rc); 622523ac9029SStephen Hurd } 62264c7070dbSScott Long 62274c7070dbSScott Long #define NAME_BUFLEN 32 62284c7070dbSScott Long static void 62294c7070dbSScott Long iflib_add_device_sysctl_pre(if_ctx_t ctx) 62304c7070dbSScott Long { 62314c7070dbSScott Long device_t dev = iflib_get_dev(ctx); 62324c7070dbSScott Long struct sysctl_oid_list *child, *oid_list; 62334c7070dbSScott Long struct sysctl_ctx_list *ctx_list; 62344c7070dbSScott Long struct sysctl_oid *node; 62354c7070dbSScott Long 62364c7070dbSScott Long ctx_list = device_get_sysctl_ctx(dev); 62374c7070dbSScott Long child = SYSCTL_CHILDREN(device_get_sysctl_tree(dev)); 62384c7070dbSScott Long ctx->ifc_sysctl_node = node = SYSCTL_ADD_NODE(ctx_list, child, OID_AUTO, "iflib", 62394c7070dbSScott Long CTLFLAG_RD, NULL, "IFLIB fields"); 62404c7070dbSScott Long oid_list = SYSCTL_CHILDREN(node); 62414c7070dbSScott Long 624223ac9029SStephen Hurd SYSCTL_ADD_STRING(ctx_list, oid_list, OID_AUTO, "driver_version", 624323ac9029SStephen Hurd CTLFLAG_RD, ctx->ifc_sctx->isc_driver_version, 0, 624423ac9029SStephen Hurd "driver version"); 624523ac9029SStephen Hurd 62464c7070dbSScott Long SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "override_ntxqs", 62474c7070dbSScott Long CTLFLAG_RWTUN, &ctx->ifc_sysctl_ntxqs, 0, 62484c7070dbSScott Long "# of txqs to use, 0 => use default #"); 62494c7070dbSScott Long SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "override_nrxqs", 625023ac9029SStephen Hurd CTLFLAG_RWTUN, &ctx->ifc_sysctl_nrxqs, 0, 625123ac9029SStephen Hurd "# of rxqs to use, 0 => use default #"); 625223ac9029SStephen Hurd SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "override_qs_enable", 625323ac9029SStephen Hurd CTLFLAG_RWTUN, &ctx->ifc_sysctl_qs_eq_override, 0, 625423ac9029SStephen Hurd "permit #txq != #rxq"); 6255ea351d3fSSean Bruno SYSCTL_ADD_INT(ctx_list, oid_list, OID_AUTO, "disable_msix", 6256ea351d3fSSean Bruno CTLFLAG_RWTUN, &ctx->ifc_softc_ctx.isc_disable_msix, 0, 6257b97de13aSMarius Strobl "disable MSI-X (default 0)"); 6258f4d2154eSStephen Hurd SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "rx_budget", 6259f4d2154eSStephen Hurd CTLFLAG_RWTUN, &ctx->ifc_sysctl_rx_budget, 0, 6260f4d2154eSStephen Hurd "set the rx budget"); 6261fe51d4cdSStephen Hurd SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "tx_abdicate", 6262fe51d4cdSStephen Hurd CTLFLAG_RWTUN, &ctx->ifc_sysctl_tx_abdicate, 0, 6263fe51d4cdSStephen Hurd "cause tx to abdicate instead of running to completion"); 62644c7070dbSScott Long 626523ac9029SStephen Hurd /* XXX change for per-queue sizes */ 626623ac9029SStephen Hurd SYSCTL_ADD_PROC(ctx_list, oid_list, OID_AUTO, "override_ntxds", 626723ac9029SStephen Hurd CTLTYPE_STRING|CTLFLAG_RWTUN, ctx, IFLIB_NTXD_HANDLER, 626823ac9029SStephen Hurd mp_ndesc_handler, "A", 626923ac9029SStephen Hurd "list of # of tx descriptors to use, 0 = use default #"); 627023ac9029SStephen Hurd SYSCTL_ADD_PROC(ctx_list, oid_list, OID_AUTO, "override_nrxds", 627123ac9029SStephen Hurd CTLTYPE_STRING|CTLFLAG_RWTUN, ctx, IFLIB_NRXD_HANDLER, 627223ac9029SStephen Hurd mp_ndesc_handler, "A", 627323ac9029SStephen Hurd "list of # of rx descriptors to use, 0 = use default #"); 62744c7070dbSScott Long } 62754c7070dbSScott Long 62764c7070dbSScott Long static void 62774c7070dbSScott Long iflib_add_device_sysctl_post(if_ctx_t ctx) 62784c7070dbSScott Long { 62794c7070dbSScott Long if_shared_ctx_t sctx = ctx->ifc_sctx; 62804c7070dbSScott Long if_softc_ctx_t scctx = &ctx->ifc_softc_ctx; 62814c7070dbSScott Long device_t dev = iflib_get_dev(ctx); 62824c7070dbSScott Long struct sysctl_oid_list *child; 62834c7070dbSScott Long struct sysctl_ctx_list *ctx_list; 62844c7070dbSScott Long iflib_fl_t fl; 62854c7070dbSScott Long iflib_txq_t txq; 62864c7070dbSScott Long iflib_rxq_t rxq; 62874c7070dbSScott Long int i, j; 62884c7070dbSScott Long char namebuf[NAME_BUFLEN]; 62894c7070dbSScott Long char *qfmt; 62904c7070dbSScott Long struct sysctl_oid *queue_node, *fl_node, *node; 62914c7070dbSScott Long struct sysctl_oid_list *queue_list, *fl_list; 62924c7070dbSScott Long ctx_list = device_get_sysctl_ctx(dev); 62934c7070dbSScott Long 62944c7070dbSScott Long node = ctx->ifc_sysctl_node; 62954c7070dbSScott Long child = SYSCTL_CHILDREN(node); 62964c7070dbSScott Long 62974c7070dbSScott Long if (scctx->isc_ntxqsets > 100) 62984c7070dbSScott Long qfmt = "txq%03d"; 62994c7070dbSScott Long else if (scctx->isc_ntxqsets > 10) 63004c7070dbSScott Long qfmt = "txq%02d"; 63014c7070dbSScott Long else 63024c7070dbSScott Long qfmt = "txq%d"; 63034c7070dbSScott Long for (i = 0, txq = ctx->ifc_txqs; i < scctx->isc_ntxqsets; i++, txq++) { 63044c7070dbSScott Long snprintf(namebuf, NAME_BUFLEN, qfmt, i); 63054c7070dbSScott Long queue_node = SYSCTL_ADD_NODE(ctx_list, child, OID_AUTO, namebuf, 63064c7070dbSScott Long CTLFLAG_RD, NULL, "Queue Name"); 63074c7070dbSScott Long queue_list = SYSCTL_CHILDREN(queue_node); 63084c7070dbSScott Long #if MEMORY_LOGGING 63094c7070dbSScott Long SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "txq_dequeued", 63104c7070dbSScott Long CTLFLAG_RD, 63114c7070dbSScott Long &txq->ift_dequeued, "total mbufs freed"); 63124c7070dbSScott Long SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "txq_enqueued", 63134c7070dbSScott Long CTLFLAG_RD, 63144c7070dbSScott Long &txq->ift_enqueued, "total mbufs enqueued"); 63154c7070dbSScott Long #endif 63164c7070dbSScott Long SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "mbuf_defrag", 63174c7070dbSScott Long CTLFLAG_RD, 63184c7070dbSScott Long &txq->ift_mbuf_defrag, "# of times m_defrag was called"); 63194c7070dbSScott Long SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "m_pullups", 63204c7070dbSScott Long CTLFLAG_RD, 63214c7070dbSScott Long &txq->ift_pullups, "# of times m_pullup was called"); 63224c7070dbSScott Long SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "mbuf_defrag_failed", 63234c7070dbSScott Long CTLFLAG_RD, 63244c7070dbSScott Long &txq->ift_mbuf_defrag_failed, "# of times m_defrag failed"); 63254c7070dbSScott Long SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "no_desc_avail", 63264c7070dbSScott Long CTLFLAG_RD, 632723ac9029SStephen Hurd &txq->ift_no_desc_avail, "# of times no descriptors were available"); 63284c7070dbSScott Long SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "tx_map_failed", 63294c7070dbSScott Long CTLFLAG_RD, 63304c7070dbSScott Long &txq->ift_map_failed, "# of times dma map failed"); 63314c7070dbSScott Long SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "txd_encap_efbig", 63324c7070dbSScott Long CTLFLAG_RD, 63334c7070dbSScott Long &txq->ift_txd_encap_efbig, "# of times txd_encap returned EFBIG"); 63344c7070dbSScott Long SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "no_tx_dma_setup", 63354c7070dbSScott Long CTLFLAG_RD, 63364c7070dbSScott Long &txq->ift_no_tx_dma_setup, "# of times map failed for other than EFBIG"); 63374c7070dbSScott Long SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "txq_pidx", 63384c7070dbSScott Long CTLFLAG_RD, 63394c7070dbSScott Long &txq->ift_pidx, 1, "Producer Index"); 63404c7070dbSScott Long SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "txq_cidx", 63414c7070dbSScott Long CTLFLAG_RD, 63424c7070dbSScott Long &txq->ift_cidx, 1, "Consumer Index"); 63434c7070dbSScott Long SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "txq_cidx_processed", 63444c7070dbSScott Long CTLFLAG_RD, 63454c7070dbSScott Long &txq->ift_cidx_processed, 1, "Consumer Index seen by credit update"); 63464c7070dbSScott Long SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "txq_in_use", 63474c7070dbSScott Long CTLFLAG_RD, 63484c7070dbSScott Long &txq->ift_in_use, 1, "descriptors in use"); 63494c7070dbSScott Long SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "txq_processed", 63504c7070dbSScott Long CTLFLAG_RD, 63514c7070dbSScott Long &txq->ift_processed, "descriptors procesed for clean"); 63524c7070dbSScott Long SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "txq_cleaned", 63534c7070dbSScott Long CTLFLAG_RD, 63544c7070dbSScott Long &txq->ift_cleaned, "total cleaned"); 63554c7070dbSScott Long SYSCTL_ADD_PROC(ctx_list, queue_list, OID_AUTO, "ring_state", 635695246abbSSean Bruno CTLTYPE_STRING | CTLFLAG_RD, __DEVOLATILE(uint64_t *, &txq->ift_br->state), 63574c7070dbSScott Long 0, mp_ring_state_handler, "A", "soft ring state"); 63584c7070dbSScott Long SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_enqueues", 635995246abbSSean Bruno CTLFLAG_RD, &txq->ift_br->enqueues, 63604c7070dbSScott Long "# of enqueues to the mp_ring for this queue"); 63614c7070dbSScott Long SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_drops", 636295246abbSSean Bruno CTLFLAG_RD, &txq->ift_br->drops, 63634c7070dbSScott Long "# of drops in the mp_ring for this queue"); 63644c7070dbSScott Long SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_starts", 636595246abbSSean Bruno CTLFLAG_RD, &txq->ift_br->starts, 63664c7070dbSScott Long "# of normal consumer starts in the mp_ring for this queue"); 63674c7070dbSScott Long SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_stalls", 636895246abbSSean Bruno CTLFLAG_RD, &txq->ift_br->stalls, 63694c7070dbSScott Long "# of consumer stalls in the mp_ring for this queue"); 63704c7070dbSScott Long SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_restarts", 637195246abbSSean Bruno CTLFLAG_RD, &txq->ift_br->restarts, 63724c7070dbSScott Long "# of consumer restarts in the mp_ring for this queue"); 63734c7070dbSScott Long SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_abdications", 637495246abbSSean Bruno CTLFLAG_RD, &txq->ift_br->abdications, 63754c7070dbSScott Long "# of consumer abdications in the mp_ring for this queue"); 63764c7070dbSScott Long } 63774c7070dbSScott Long 63784c7070dbSScott Long if (scctx->isc_nrxqsets > 100) 63794c7070dbSScott Long qfmt = "rxq%03d"; 63804c7070dbSScott Long else if (scctx->isc_nrxqsets > 10) 63814c7070dbSScott Long qfmt = "rxq%02d"; 63824c7070dbSScott Long else 63834c7070dbSScott Long qfmt = "rxq%d"; 63844c7070dbSScott Long for (i = 0, rxq = ctx->ifc_rxqs; i < scctx->isc_nrxqsets; i++, rxq++) { 63854c7070dbSScott Long snprintf(namebuf, NAME_BUFLEN, qfmt, i); 63864c7070dbSScott Long queue_node = SYSCTL_ADD_NODE(ctx_list, child, OID_AUTO, namebuf, 63874c7070dbSScott Long CTLFLAG_RD, NULL, "Queue Name"); 63884c7070dbSScott Long queue_list = SYSCTL_CHILDREN(queue_node); 638923ac9029SStephen Hurd if (sctx->isc_flags & IFLIB_HAS_RXCQ) { 63904c7070dbSScott Long SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "rxq_cq_pidx", 63914c7070dbSScott Long CTLFLAG_RD, 63924c7070dbSScott Long &rxq->ifr_cq_pidx, 1, "Producer Index"); 63934c7070dbSScott Long SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "rxq_cq_cidx", 63944c7070dbSScott Long CTLFLAG_RD, 63954c7070dbSScott Long &rxq->ifr_cq_cidx, 1, "Consumer Index"); 63964c7070dbSScott Long } 6397da69b8f9SSean Bruno 63984c7070dbSScott Long for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++) { 63994c7070dbSScott Long snprintf(namebuf, NAME_BUFLEN, "rxq_fl%d", j); 64004c7070dbSScott Long fl_node = SYSCTL_ADD_NODE(ctx_list, queue_list, OID_AUTO, namebuf, 64014c7070dbSScott Long CTLFLAG_RD, NULL, "freelist Name"); 64024c7070dbSScott Long fl_list = SYSCTL_CHILDREN(fl_node); 64034c7070dbSScott Long SYSCTL_ADD_U16(ctx_list, fl_list, OID_AUTO, "pidx", 64044c7070dbSScott Long CTLFLAG_RD, 64054c7070dbSScott Long &fl->ifl_pidx, 1, "Producer Index"); 64064c7070dbSScott Long SYSCTL_ADD_U16(ctx_list, fl_list, OID_AUTO, "cidx", 64074c7070dbSScott Long CTLFLAG_RD, 64084c7070dbSScott Long &fl->ifl_cidx, 1, "Consumer Index"); 64094c7070dbSScott Long SYSCTL_ADD_U16(ctx_list, fl_list, OID_AUTO, "credits", 64104c7070dbSScott Long CTLFLAG_RD, 64114c7070dbSScott Long &fl->ifl_credits, 1, "credits available"); 64124c7070dbSScott Long #if MEMORY_LOGGING 64134c7070dbSScott Long SYSCTL_ADD_QUAD(ctx_list, fl_list, OID_AUTO, "fl_m_enqueued", 64144c7070dbSScott Long CTLFLAG_RD, 64154c7070dbSScott Long &fl->ifl_m_enqueued, "mbufs allocated"); 64164c7070dbSScott Long SYSCTL_ADD_QUAD(ctx_list, fl_list, OID_AUTO, "fl_m_dequeued", 64174c7070dbSScott Long CTLFLAG_RD, 64184c7070dbSScott Long &fl->ifl_m_dequeued, "mbufs freed"); 64194c7070dbSScott Long SYSCTL_ADD_QUAD(ctx_list, fl_list, OID_AUTO, "fl_cl_enqueued", 64204c7070dbSScott Long CTLFLAG_RD, 64214c7070dbSScott Long &fl->ifl_cl_enqueued, "clusters allocated"); 64224c7070dbSScott Long SYSCTL_ADD_QUAD(ctx_list, fl_list, OID_AUTO, "fl_cl_dequeued", 64234c7070dbSScott Long CTLFLAG_RD, 64244c7070dbSScott Long &fl->ifl_cl_dequeued, "clusters freed"); 64254c7070dbSScott Long #endif 64264c7070dbSScott Long 64274c7070dbSScott Long } 64284c7070dbSScott Long } 64294c7070dbSScott Long 64304c7070dbSScott Long } 643195246abbSSean Bruno 643277c1fcecSEric Joyner void 643377c1fcecSEric Joyner iflib_request_reset(if_ctx_t ctx) 643477c1fcecSEric Joyner { 643577c1fcecSEric Joyner 643677c1fcecSEric Joyner STATE_LOCK(ctx); 643777c1fcecSEric Joyner ctx->ifc_flags |= IFC_DO_RESET; 643877c1fcecSEric Joyner STATE_UNLOCK(ctx); 643977c1fcecSEric Joyner } 644077c1fcecSEric Joyner 644195246abbSSean Bruno #ifndef __NO_STRICT_ALIGNMENT 644295246abbSSean Bruno static struct mbuf * 644395246abbSSean Bruno iflib_fixup_rx(struct mbuf *m) 644495246abbSSean Bruno { 644595246abbSSean Bruno struct mbuf *n; 644695246abbSSean Bruno 644795246abbSSean Bruno if (m->m_len <= (MCLBYTES - ETHER_HDR_LEN)) { 644895246abbSSean Bruno bcopy(m->m_data, m->m_data + ETHER_HDR_LEN, m->m_len); 644995246abbSSean Bruno m->m_data += ETHER_HDR_LEN; 645095246abbSSean Bruno n = m; 645195246abbSSean Bruno } else { 645295246abbSSean Bruno MGETHDR(n, M_NOWAIT, MT_DATA); 645395246abbSSean Bruno if (n == NULL) { 645495246abbSSean Bruno m_freem(m); 645595246abbSSean Bruno return (NULL); 645695246abbSSean Bruno } 645795246abbSSean Bruno bcopy(m->m_data, n->m_data, ETHER_HDR_LEN); 645895246abbSSean Bruno m->m_data += ETHER_HDR_LEN; 645995246abbSSean Bruno m->m_len -= ETHER_HDR_LEN; 646095246abbSSean Bruno n->m_len = ETHER_HDR_LEN; 646195246abbSSean Bruno M_MOVE_PKTHDR(n, m); 646295246abbSSean Bruno n->m_next = m; 646395246abbSSean Bruno } 646495246abbSSean Bruno return (n); 646595246abbSSean Bruno } 646695246abbSSean Bruno #endif 646794618825SMark Johnston 646894618825SMark Johnston #ifdef NETDUMP 646994618825SMark Johnston static void 647094618825SMark Johnston iflib_netdump_init(struct ifnet *ifp, int *nrxr, int *ncl, int *clsize) 647194618825SMark Johnston { 647294618825SMark Johnston if_ctx_t ctx; 647394618825SMark Johnston 647494618825SMark Johnston ctx = if_getsoftc(ifp); 647594618825SMark Johnston CTX_LOCK(ctx); 647694618825SMark Johnston *nrxr = NRXQSETS(ctx); 647794618825SMark Johnston *ncl = ctx->ifc_rxqs[0].ifr_fl->ifl_size; 647894618825SMark Johnston *clsize = ctx->ifc_rxqs[0].ifr_fl->ifl_buf_size; 647994618825SMark Johnston CTX_UNLOCK(ctx); 648094618825SMark Johnston } 648194618825SMark Johnston 648294618825SMark Johnston static void 648394618825SMark Johnston iflib_netdump_event(struct ifnet *ifp, enum netdump_ev event) 648494618825SMark Johnston { 648594618825SMark Johnston if_ctx_t ctx; 648694618825SMark Johnston if_softc_ctx_t scctx; 648794618825SMark Johnston iflib_fl_t fl; 648894618825SMark Johnston iflib_rxq_t rxq; 648994618825SMark Johnston int i, j; 649094618825SMark Johnston 649194618825SMark Johnston ctx = if_getsoftc(ifp); 649294618825SMark Johnston scctx = &ctx->ifc_softc_ctx; 649394618825SMark Johnston 649494618825SMark Johnston switch (event) { 649594618825SMark Johnston case NETDUMP_START: 649694618825SMark Johnston for (i = 0; i < scctx->isc_nrxqsets; i++) { 649794618825SMark Johnston rxq = &ctx->ifc_rxqs[i]; 649894618825SMark Johnston for (j = 0; j < rxq->ifr_nfl; j++) { 649994618825SMark Johnston fl = rxq->ifr_fl; 650094618825SMark Johnston fl->ifl_zone = m_getzone(fl->ifl_buf_size); 650194618825SMark Johnston } 650294618825SMark Johnston } 650394618825SMark Johnston iflib_no_tx_batch = 1; 650494618825SMark Johnston break; 650594618825SMark Johnston default: 650694618825SMark Johnston break; 650794618825SMark Johnston } 650894618825SMark Johnston } 650994618825SMark Johnston 651094618825SMark Johnston static int 651194618825SMark Johnston iflib_netdump_transmit(struct ifnet *ifp, struct mbuf *m) 651294618825SMark Johnston { 651394618825SMark Johnston if_ctx_t ctx; 651494618825SMark Johnston iflib_txq_t txq; 651594618825SMark Johnston int error; 651694618825SMark Johnston 651794618825SMark Johnston ctx = if_getsoftc(ifp); 651894618825SMark Johnston if ((if_getdrvflags(ifp) & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) != 651994618825SMark Johnston IFF_DRV_RUNNING) 652094618825SMark Johnston return (EBUSY); 652194618825SMark Johnston 652294618825SMark Johnston txq = &ctx->ifc_txqs[0]; 652394618825SMark Johnston error = iflib_encap(txq, &m); 652494618825SMark Johnston if (error == 0) 652594618825SMark Johnston (void)iflib_txd_db_check(ctx, txq, true, txq->ift_in_use); 652694618825SMark Johnston return (error); 652794618825SMark Johnston } 652894618825SMark Johnston 652994618825SMark Johnston static int 653094618825SMark Johnston iflib_netdump_poll(struct ifnet *ifp, int count) 653194618825SMark Johnston { 653294618825SMark Johnston if_ctx_t ctx; 653394618825SMark Johnston if_softc_ctx_t scctx; 653494618825SMark Johnston iflib_txq_t txq; 653594618825SMark Johnston int i; 653694618825SMark Johnston 653794618825SMark Johnston ctx = if_getsoftc(ifp); 653894618825SMark Johnston scctx = &ctx->ifc_softc_ctx; 653994618825SMark Johnston 654094618825SMark Johnston if ((if_getdrvflags(ifp) & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) != 654194618825SMark Johnston IFF_DRV_RUNNING) 654294618825SMark Johnston return (EBUSY); 654394618825SMark Johnston 654494618825SMark Johnston txq = &ctx->ifc_txqs[0]; 654594618825SMark Johnston (void)iflib_completed_tx_reclaim(txq, RECLAIM_THRESH(ctx)); 654694618825SMark Johnston 654794618825SMark Johnston for (i = 0; i < scctx->isc_nrxqsets; i++) 654894618825SMark Johnston (void)iflib_rxeof(&ctx->ifc_rxqs[i], 16 /* XXX */); 654994618825SMark Johnston return (0); 655094618825SMark Johnston } 655194618825SMark Johnston #endif /* NETDUMP */ 6552