1218792Snp/*- 2218792Snp * Copyright (c) 2011 Chelsio Communications, Inc. 3218792Snp * All rights reserved. 4218792Snp * Written by: Navdeep Parhar <np@FreeBSD.org> 5218792Snp * 6218792Snp * Redistribution and use in source and binary forms, with or without 7218792Snp * modification, are permitted provided that the following conditions 8218792Snp * are met: 9218792Snp * 1. Redistributions of source code must retain the above copyright 10218792Snp * notice, this list of conditions and the following disclaimer. 11218792Snp * 2. Redistributions in binary form must reproduce the above copyright 12218792Snp * notice, this list of conditions and the following disclaimer in the 13218792Snp * documentation and/or other materials provided with the distribution. 14218792Snp * 15218792Snp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16218792Snp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17218792Snp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18218792Snp * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19218792Snp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20218792Snp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21218792Snp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22218792Snp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23218792Snp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24218792Snp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25218792Snp * SUCH DAMAGE. 26218792Snp */ 27218792Snp 28218792Snp#include <sys/cdefs.h> 29218792Snp__FBSDID("$FreeBSD: releng/10.3/sys/dev/cxgbe/t4_sge.c 286274 2015-08-04 04:00:36Z np $"); 30218792Snp 31218792Snp#include "opt_inet.h" 32237819Snp#include "opt_inet6.h" 33218792Snp 34218792Snp#include <sys/types.h> 35218792Snp#include <sys/mbuf.h> 36218792Snp#include <sys/socket.h> 37218792Snp#include <sys/kernel.h> 38219286Snp#include <sys/malloc.h> 39219286Snp#include <sys/queue.h> 40265425Snp#include <sys/sbuf.h> 41219286Snp#include <sys/taskqueue.h> 42255015Snp#include <sys/time.h> 43284052Snp#include <sys/sglist.h> 44218792Snp#include <sys/sysctl.h> 45228561Snp#include <sys/smp.h> 46269356Snp#include <sys/counter.h> 47218792Snp#include <net/bpf.h> 48218792Snp#include <net/ethernet.h> 49218792Snp#include <net/if.h> 50218792Snp#include <net/if_vlan_var.h> 51218792Snp#include <netinet/in.h> 52218792Snp#include <netinet/ip.h> 53237819Snp#include <netinet/ip6.h> 54218792Snp#include <netinet/tcp.h> 55256131Sdim#include <machine/md_var.h> 56265425Snp#include <vm/vm.h> 57265425Snp#include <vm/pmap.h> 58270297Snp#ifdef DEV_NETMAP 59270297Snp#include <machine/bus.h> 60270297Snp#include <sys/selinfo.h> 61270297Snp#include <net/if_var.h> 62270297Snp#include <net/netmap.h> 63270297Snp#include <dev/netmap/netmap_kern.h> 64270297Snp#endif 65218792Snp 66218792Snp#include "common/common.h" 67218792Snp#include "common/t4_regs.h" 68218792Snp#include "common/t4_regs_values.h" 69218792Snp#include "common/t4_msg.h" 70284052Snp#include "t4_mp_ring.h" 71218792Snp 72248925Snp#ifdef T4_PKT_TIMESTAMP 73248925Snp#define RX_COPY_THRESHOLD (MINCLSIZE - 8) 74248925Snp#else 75248925Snp#define RX_COPY_THRESHOLD MINCLSIZE 76248925Snp#endif 77248925Snp 78239258Snp/* 79239258Snp * Ethernet frames are DMA'd at this byte offset into the freelist buffer. 80239258Snp * 0-7 are valid values. 81239258Snp */ 82270297Snpint fl_pktshift = 2; 83239258SnpTUNABLE_INT("hw.cxgbe.fl_pktshift", &fl_pktshift); 84218792Snp 85239258Snp/* 86239258Snp * Pad ethernet payload up to this boundary. 87239258Snp * -1: driver should figure out a good value. 88255050Snp * 0: disable padding. 89255050Snp * Any power of 2 from 32 to 4096 (both inclusive) is also a valid value. 90239258Snp */ 91270297Snpint fl_pad = -1; 92239258SnpTUNABLE_INT("hw.cxgbe.fl_pad", &fl_pad); 93218792Snp 94239258Snp/* 95239258Snp * Status page length. 96239258Snp * -1: driver should figure out a good value. 97239258Snp * 64 or 128 are the only other valid values. 98239258Snp */ 99270297Snpint spg_len = -1; 100239258SnpTUNABLE_INT("hw.cxgbe.spg_len", &spg_len); 101239258Snp 102239258Snp/* 103239258Snp * Congestion drops. 104239258Snp * -1: no congestion feedback (not recommended). 105239258Snp * 0: backpressure the channel instead of dropping packets right away. 106239258Snp * 1: no backpressure, drop packets for the congested queue immediately. 107239258Snp */ 108239258Snpstatic int cong_drop = 0; 109239258SnpTUNABLE_INT("hw.cxgbe.cong_drop", &cong_drop); 110239258Snp 111255050Snp/* 112255050Snp * Deliver multiple frames in the same free list buffer if they fit. 113255050Snp * -1: let the driver decide whether to enable buffer packing or not. 114255050Snp * 0: disable buffer packing. 115255050Snp * 1: enable buffer packing. 116255050Snp */ 117255050Snpstatic int buffer_packing = -1; 118255050SnpTUNABLE_INT("hw.cxgbe.buffer_packing", &buffer_packing); 119255050Snp 120255050Snp/* 121255050Snp * Start next frame in a packed buffer at this boundary. 122255050Snp * -1: driver should figure out a good value. 123281212Snp * T4: driver will ignore this and use the same value as fl_pad above. 124281212Snp * T5: 16, or a power of 2 from 64 to 4096 (both inclusive) is a valid value. 125255050Snp */ 126255050Snpstatic int fl_pack = -1; 127255050SnpTUNABLE_INT("hw.cxgbe.fl_pack", &fl_pack); 128255050Snp 129265425Snp/* 130265425Snp * Allow the driver to create mbuf(s) in a cluster allocated for rx. 131265425Snp * 0: never; always allocate mbufs from the zone_mbuf UMA zone. 132265425Snp * 1: ok to create mbuf(s) within a cluster if there is room. 133265425Snp */ 134265425Snpstatic int allow_mbufs_in_cluster = 1; 135265425SnpTUNABLE_INT("hw.cxgbe.allow_mbufs_in_cluster", &allow_mbufs_in_cluster); 136265425Snp 137265425Snp/* 138265425Snp * Largest rx cluster size that the driver is allowed to allocate. 139265425Snp */ 140265425Snpstatic int largest_rx_cluster = MJUM16BYTES; 141265425SnpTUNABLE_INT("hw.cxgbe.largest_rx_cluster", &largest_rx_cluster); 142265425Snp 143265425Snp/* 144265425Snp * Size of cluster allocation that's most likely to succeed. The driver will 145265425Snp * fall back to this size if it fails to allocate clusters larger than this. 146265425Snp */ 147265425Snpstatic int safest_rx_cluster = PAGE_SIZE; 148265425SnpTUNABLE_INT("hw.cxgbe.safest_rx_cluster", &safest_rx_cluster); 149265425Snp 150218792Snpstruct txpkts { 151284052Snp u_int wr_type; /* type 0 or type 1 */ 152284052Snp u_int npkt; /* # of packets in this work request */ 153284052Snp u_int plen; /* total payload (sum of all packets) */ 154284052Snp u_int len16; /* # of 16B pieces used by this work request */ 155218792Snp}; 156218792Snp 157218792Snp/* A packet's SGL. This + m_pkthdr has all info needed for tx */ 158218792Snpstruct sgl { 159284052Snp struct sglist sg; 160284052Snp struct sglist_seg seg[TX_SGL_SEGS]; 161218792Snp}; 162218792Snp 163228561Snpstatic int service_iq(struct sge_iq *, int); 164270297Snpstatic struct mbuf *get_fl_payload(struct adapter *, struct sge_fl *, uint32_t); 165228561Snpstatic int t4_eth_rx(struct sge_iq *, const struct rss_header *, struct mbuf *); 166270297Snpstatic inline void init_iq(struct sge_iq *, struct adapter *, int, int, int); 167281212Snpstatic inline void init_fl(struct adapter *, struct sge_fl *, int, int, char *); 168228561Snpstatic inline void init_eq(struct sge_eq *, int, int, uint8_t, uint16_t, 169228561Snp char *); 170218792Snpstatic int alloc_ring(struct adapter *, size_t, bus_dma_tag_t *, bus_dmamap_t *, 171218792Snp bus_addr_t *, void **); 172218792Snpstatic int free_ring(struct adapter *, bus_dma_tag_t, bus_dmamap_t, bus_addr_t, 173218792Snp void *); 174218792Snpstatic int alloc_iq_fl(struct port_info *, struct sge_iq *, struct sge_fl *, 175222085Snp int, int); 176218792Snpstatic int free_iq_fl(struct port_info *, struct sge_iq *, struct sge_fl *); 177265425Snpstatic void add_fl_sysctls(struct sysctl_ctx_list *, struct sysctl_oid *, 178265425Snp struct sge_fl *); 179228561Snpstatic int alloc_fwq(struct adapter *); 180228561Snpstatic int free_fwq(struct adapter *); 181228561Snpstatic int alloc_mgmtq(struct adapter *); 182228561Snpstatic int free_mgmtq(struct adapter *); 183228561Snpstatic int alloc_rxq(struct port_info *, struct sge_rxq *, int, int, 184228561Snp struct sysctl_oid *); 185218792Snpstatic int free_rxq(struct port_info *, struct sge_rxq *); 186237263Snp#ifdef TCP_OFFLOAD 187228561Snpstatic int alloc_ofld_rxq(struct port_info *, struct sge_ofld_rxq *, int, int, 188228561Snp struct sysctl_oid *); 189228561Snpstatic int free_ofld_rxq(struct port_info *, struct sge_ofld_rxq *); 190228561Snp#endif 191270297Snp#ifdef DEV_NETMAP 192270297Snpstatic int alloc_nm_rxq(struct port_info *, struct sge_nm_rxq *, int, int, 193270297Snp struct sysctl_oid *); 194270297Snpstatic int free_nm_rxq(struct port_info *, struct sge_nm_rxq *); 195270297Snpstatic int alloc_nm_txq(struct port_info *, struct sge_nm_txq *, int, int, 196270297Snp struct sysctl_oid *); 197270297Snpstatic int free_nm_txq(struct port_info *, struct sge_nm_txq *); 198270297Snp#endif 199228561Snpstatic int ctrl_eq_alloc(struct adapter *, struct sge_eq *); 200228561Snpstatic int eth_eq_alloc(struct adapter *, struct port_info *, struct sge_eq *); 201237263Snp#ifdef TCP_OFFLOAD 202228561Snpstatic int ofld_eq_alloc(struct adapter *, struct port_info *, struct sge_eq *); 203228561Snp#endif 204228561Snpstatic int alloc_eq(struct adapter *, struct port_info *, struct sge_eq *); 205228561Snpstatic int free_eq(struct adapter *, struct sge_eq *); 206228561Snpstatic int alloc_wrq(struct adapter *, struct port_info *, struct sge_wrq *, 207228561Snp struct sysctl_oid *); 208228561Snpstatic int free_wrq(struct adapter *, struct sge_wrq *); 209228561Snpstatic int alloc_txq(struct port_info *, struct sge_txq *, int, 210228561Snp struct sysctl_oid *); 211218792Snpstatic int free_txq(struct port_info *, struct sge_txq *); 212218792Snpstatic void oneseg_dma_callback(void *, bus_dma_segment_t *, int, int); 213218792Snpstatic inline void ring_fl_db(struct adapter *, struct sge_fl *); 214228561Snpstatic int refill_fl(struct adapter *, struct sge_fl *, int); 215228561Snpstatic void refill_sfl(void *); 216218792Snpstatic int alloc_fl_sdesc(struct sge_fl *); 217255050Snpstatic void free_fl_sdesc(struct adapter *, struct sge_fl *); 218265425Snpstatic void find_best_refill_source(struct adapter *, struct sge_fl *, int); 219265425Snpstatic void find_safe_refill_source(struct adapter *, struct sge_fl *); 220228561Snpstatic void add_fl_to_sfl(struct adapter *, struct sge_fl *); 221218792Snp 222284052Snpstatic inline void get_pkt_gl(struct mbuf *, struct sglist *); 223284052Snpstatic inline u_int txpkt_len16(u_int, u_int); 224284052Snpstatic inline u_int txpkts0_len16(u_int); 225284052Snpstatic inline u_int txpkts1_len16(void); 226284052Snpstatic u_int write_txpkt_wr(struct sge_txq *, struct fw_eth_tx_pkt_wr *, 227284052Snp struct mbuf *, u_int); 228284052Snpstatic int try_txpkts(struct mbuf *, struct mbuf *, struct txpkts *, u_int); 229284052Snpstatic int add_to_txpkts(struct mbuf *, struct txpkts *, u_int); 230284052Snpstatic u_int write_txpkts_wr(struct sge_txq *, struct fw_eth_tx_pkts_wr *, 231284052Snp struct mbuf *, const struct txpkts *, u_int); 232284052Snpstatic void write_gl_to_txd(struct sge_txq *, struct mbuf *, caddr_t *, int); 233218792Snpstatic inline void copy_to_txd(struct sge_eq *, caddr_t, caddr_t *, int); 234284052Snpstatic inline void ring_eq_db(struct adapter *, struct sge_eq *, u_int); 235284052Snpstatic inline uint16_t read_hw_cidx(struct sge_eq *); 236284052Snpstatic inline u_int reclaimable_tx_desc(struct sge_eq *); 237284052Snpstatic inline u_int total_available_tx_desc(struct sge_eq *); 238284052Snpstatic u_int reclaim_tx_descs(struct sge_txq *, u_int); 239284052Snpstatic void tx_reclaim(void *, int); 240284052Snpstatic __be64 get_flit(struct sglist_seg *, int, int); 241228561Snpstatic int handle_sge_egr_update(struct sge_iq *, const struct rss_header *, 242228561Snp struct mbuf *); 243239336Snpstatic int handle_fw_msg(struct sge_iq *, const struct rss_header *, 244228561Snp struct mbuf *); 245284052Snpstatic void wrq_tx_drain(void *, int); 246284052Snpstatic void drain_wrq_wr_list(struct adapter *, struct sge_wrq *); 247218792Snp 248222510Snpstatic int sysctl_uint16(SYSCTL_HANDLER_ARGS); 249265425Snpstatic int sysctl_bufsizes(SYSCTL_HANDLER_ARGS); 250220873Snp 251269356Snpstatic counter_u64_t extfree_refs; 252269356Snpstatic counter_u64_t extfree_rels; 253269356Snp 254219392Snp/* 255255050Snp * Called on MOD_LOAD. Validates and calculates the SGE tunables. 256219392Snp */ 257219392Snpvoid 258219392Snpt4_sge_modload(void) 259219392Snp{ 260255050Snp 261239258Snp if (fl_pktshift < 0 || fl_pktshift > 7) { 262239258Snp printf("Invalid hw.cxgbe.fl_pktshift value (%d)," 263239258Snp " using 2 instead.\n", fl_pktshift); 264239258Snp fl_pktshift = 2; 265239258Snp } 266239258Snp 267239258Snp if (spg_len != 64 && spg_len != 128) { 268239258Snp int len; 269239258Snp 270239258Snp#if defined(__i386__) || defined(__amd64__) 271239258Snp len = cpu_clflush_line_size > 64 ? 128 : 64; 272239258Snp#else 273239258Snp len = 64; 274239258Snp#endif 275239258Snp if (spg_len != -1) { 276239258Snp printf("Invalid hw.cxgbe.spg_len value (%d)," 277239258Snp " using %d instead.\n", spg_len, len); 278239258Snp } 279239258Snp spg_len = len; 280239258Snp } 281239258Snp 282239258Snp if (cong_drop < -1 || cong_drop > 1) { 283239258Snp printf("Invalid hw.cxgbe.cong_drop value (%d)," 284239258Snp " using 0 instead.\n", cong_drop); 285239258Snp cong_drop = 0; 286239258Snp } 287269356Snp 288269356Snp extfree_refs = counter_u64_alloc(M_WAITOK); 289269356Snp extfree_rels = counter_u64_alloc(M_WAITOK); 290269356Snp counter_u64_zero(extfree_refs); 291269356Snp counter_u64_zero(extfree_rels); 292219392Snp} 293219392Snp 294248925Snpvoid 295269356Snpt4_sge_modunload(void) 296269356Snp{ 297269356Snp 298269356Snp counter_u64_free(extfree_refs); 299269356Snp counter_u64_free(extfree_rels); 300269356Snp} 301269356Snp 302269356Snpuint64_t 303269356Snpt4_sge_extfree_refs(void) 304269356Snp{ 305269356Snp uint64_t refs, rels; 306269356Snp 307269356Snp rels = counter_u64_fetch(extfree_rels); 308269356Snp refs = counter_u64_fetch(extfree_refs); 309269356Snp 310269356Snp return (refs - rels); 311269356Snp} 312269356Snp 313269356Snpvoid 314248925Snpt4_init_sge_cpl_handlers(struct adapter *sc) 315218792Snp{ 316218792Snp 317248925Snp t4_register_cpl_handler(sc, CPL_FW4_MSG, handle_fw_msg); 318248925Snp t4_register_cpl_handler(sc, CPL_FW6_MSG, handle_fw_msg); 319248925Snp t4_register_cpl_handler(sc, CPL_SGE_EGR_UPDATE, handle_sge_egr_update); 320248925Snp t4_register_cpl_handler(sc, CPL_RX_PKT, t4_eth_rx); 321248925Snp t4_register_fw_msg_handler(sc, FW6_TYPE_CMD_RPL, t4_handle_fw_rpl); 322248925Snp} 323248925Snp 324281212Snpstatic inline void 325281212Snpsetup_pad_and_pack_boundaries(struct adapter *sc) 326281212Snp{ 327281212Snp uint32_t v, m; 328281212Snp int pad, pack; 329281212Snp 330281212Snp pad = fl_pad; 331281212Snp if (fl_pad < 32 || fl_pad > 4096 || !powerof2(fl_pad)) { 332281212Snp /* 333281212Snp * If there is any chance that we might use buffer packing and 334281212Snp * the chip is a T4, then pick 64 as the pad/pack boundary. Set 335281212Snp * it to 32 in all other cases. 336281212Snp */ 337281212Snp pad = is_t4(sc) && buffer_packing ? 64 : 32; 338281212Snp 339281212Snp /* 340281212Snp * For fl_pad = 0 we'll still write a reasonable value to the 341281212Snp * register but all the freelists will opt out of padding. 342281212Snp * We'll complain here only if the user tried to set it to a 343281212Snp * value greater than 0 that was invalid. 344281212Snp */ 345281212Snp if (fl_pad > 0) { 346281212Snp device_printf(sc->dev, "Invalid hw.cxgbe.fl_pad value" 347281212Snp " (%d), using %d instead.\n", fl_pad, pad); 348281212Snp } 349281212Snp } 350281212Snp m = V_INGPADBOUNDARY(M_INGPADBOUNDARY); 351281212Snp v = V_INGPADBOUNDARY(ilog2(pad) - 5); 352281212Snp t4_set_reg_field(sc, A_SGE_CONTROL, m, v); 353281212Snp 354281212Snp if (is_t4(sc)) { 355281212Snp if (fl_pack != -1 && fl_pack != pad) { 356281212Snp /* Complain but carry on. */ 357281212Snp device_printf(sc->dev, "hw.cxgbe.fl_pack (%d) ignored," 358281212Snp " using %d instead.\n", fl_pack, pad); 359281212Snp } 360281212Snp return; 361281212Snp } 362281212Snp 363281212Snp pack = fl_pack; 364281212Snp if (fl_pack < 16 || fl_pack == 32 || fl_pack > 4096 || 365281212Snp !powerof2(fl_pack)) { 366281212Snp pack = max(sc->params.pci.mps, CACHE_LINE_SIZE); 367281212Snp MPASS(powerof2(pack)); 368281212Snp if (pack < 16) 369281212Snp pack = 16; 370281212Snp if (pack == 32) 371281212Snp pack = 64; 372281212Snp if (pack > 4096) 373281212Snp pack = 4096; 374281212Snp if (fl_pack != -1) { 375281212Snp device_printf(sc->dev, "Invalid hw.cxgbe.fl_pack value" 376281212Snp " (%d), using %d instead.\n", fl_pack, pack); 377281212Snp } 378281212Snp } 379281212Snp m = V_INGPACKBOUNDARY(M_INGPACKBOUNDARY); 380281212Snp if (pack == 16) 381281212Snp v = V_INGPACKBOUNDARY(0); 382281212Snp else 383281212Snp v = V_INGPACKBOUNDARY(ilog2(pack) - 5); 384281212Snp 385281212Snp MPASS(!is_t4(sc)); /* T4 doesn't have SGE_CONTROL2 */ 386281212Snp t4_set_reg_field(sc, A_SGE_CONTROL2, m, v); 387281212Snp} 388281212Snp 389249391Snp/* 390249391Snp * adap->params.vpd.cclk must be set up before this is called. 391249391Snp */ 392248925Snpvoid 393248925Snpt4_tweak_chip_settings(struct adapter *sc) 394248925Snp{ 395248925Snp int i; 396248925Snp uint32_t v, m; 397248925Snp int intr_timer[SGE_NTIMERS] = {1, 5, 10, 50, 100, 200}; 398249391Snp int timer_max = M_TIMERVALUE0 * 1000 / sc->params.vpd.cclk; 399248925Snp int intr_pktcount[SGE_NCOUNTERS] = {1, 8, 16, 32}; /* 63 max */ 400248925Snp uint16_t indsz = min(RX_COPY_THRESHOLD - 1, M_INDICATESIZE); 401265425Snp static int sge_flbuf_sizes[] = { 402255050Snp MCLBYTES, 403255050Snp#if MJUMPAGESIZE != MCLBYTES 404255050Snp MJUMPAGESIZE, 405265425Snp MJUMPAGESIZE - CL_METADATA_SIZE, 406265425Snp MJUMPAGESIZE - 2 * MSIZE - CL_METADATA_SIZE, 407255050Snp#endif 408255050Snp MJUM9BYTES, 409255050Snp MJUM16BYTES, 410265425Snp MCLBYTES - MSIZE - CL_METADATA_SIZE, 411265425Snp MJUM9BYTES - CL_METADATA_SIZE, 412265425Snp MJUM16BYTES - CL_METADATA_SIZE, 413255050Snp }; 414248925Snp 415248925Snp KASSERT(sc->flags & MASTER_PF, 416248925Snp ("%s: trying to change chip settings when not master.", __func__)); 417248925Snp 418255050Snp m = V_PKTSHIFT(M_PKTSHIFT) | F_RXPKTCPLMODE | F_EGRSTATUSPAGESIZE; 419248925Snp v = V_PKTSHIFT(fl_pktshift) | F_RXPKTCPLMODE | 420237512Snp V_EGRSTATUSPAGESIZE(spg_len == 128); 421248925Snp t4_set_reg_field(sc, A_SGE_CONTROL, m, v); 422218792Snp 423281212Snp setup_pad_and_pack_boundaries(sc); 424255050Snp 425248925Snp v = V_HOSTPAGESIZEPF0(PAGE_SHIFT - 10) | 426228561Snp V_HOSTPAGESIZEPF1(PAGE_SHIFT - 10) | 427228561Snp V_HOSTPAGESIZEPF2(PAGE_SHIFT - 10) | 428228561Snp V_HOSTPAGESIZEPF3(PAGE_SHIFT - 10) | 429228561Snp V_HOSTPAGESIZEPF4(PAGE_SHIFT - 10) | 430228561Snp V_HOSTPAGESIZEPF5(PAGE_SHIFT - 10) | 431228561Snp V_HOSTPAGESIZEPF6(PAGE_SHIFT - 10) | 432228561Snp V_HOSTPAGESIZEPF7(PAGE_SHIFT - 10); 433248925Snp t4_write_reg(sc, A_SGE_HOST_PAGE_SIZE, v); 434228561Snp 435265425Snp KASSERT(nitems(sge_flbuf_sizes) <= SGE_FLBUF_SIZES, 436265425Snp ("%s: hw buffer size table too big", __func__)); 437265425Snp for (i = 0; i < min(nitems(sge_flbuf_sizes), SGE_FLBUF_SIZES); i++) { 438248925Snp t4_write_reg(sc, A_SGE_FL_BUFFER_SIZE0 + (4 * i), 439265425Snp sge_flbuf_sizes[i]); 440248925Snp } 441228561Snp 442248925Snp v = V_THRESHOLD_0(intr_pktcount[0]) | V_THRESHOLD_1(intr_pktcount[1]) | 443248925Snp V_THRESHOLD_2(intr_pktcount[2]) | V_THRESHOLD_3(intr_pktcount[3]); 444248925Snp t4_write_reg(sc, A_SGE_INGRESS_RX_THRESHOLD, v); 445228561Snp 446249391Snp KASSERT(intr_timer[0] <= timer_max, 447249391Snp ("%s: not a single usable timer (%d, %d)", __func__, intr_timer[0], 448249391Snp timer_max)); 449249391Snp for (i = 1; i < nitems(intr_timer); i++) { 450249391Snp KASSERT(intr_timer[i] >= intr_timer[i - 1], 451249391Snp ("%s: timers not listed in increasing order (%d)", 452249391Snp __func__, i)); 453249391Snp 454249391Snp while (intr_timer[i] > timer_max) { 455249391Snp if (i == nitems(intr_timer) - 1) { 456249391Snp intr_timer[i] = timer_max; 457249391Snp break; 458249391Snp } 459249391Snp intr_timer[i] += intr_timer[i - 1]; 460249391Snp intr_timer[i] /= 2; 461249391Snp } 462249391Snp } 463249391Snp 464248925Snp v = V_TIMERVALUE0(us_to_core_ticks(sc, intr_timer[0])) | 465248925Snp V_TIMERVALUE1(us_to_core_ticks(sc, intr_timer[1])); 466248925Snp t4_write_reg(sc, A_SGE_TIMER_VALUE_0_AND_1, v); 467248925Snp v = V_TIMERVALUE2(us_to_core_ticks(sc, intr_timer[2])) | 468248925Snp V_TIMERVALUE3(us_to_core_ticks(sc, intr_timer[3])); 469248925Snp t4_write_reg(sc, A_SGE_TIMER_VALUE_2_AND_3, v); 470248925Snp v = V_TIMERVALUE4(us_to_core_ticks(sc, intr_timer[4])) | 471248925Snp V_TIMERVALUE5(us_to_core_ticks(sc, intr_timer[5])); 472248925Snp t4_write_reg(sc, A_SGE_TIMER_VALUE_4_AND_5, v); 473228561Snp 474248925Snp /* 4K, 16K, 64K, 256K DDP "page sizes" */ 475248925Snp v = V_HPZ0(0) | V_HPZ1(2) | V_HPZ2(4) | V_HPZ3(6); 476248925Snp t4_write_reg(sc, A_ULP_RX_TDDP_PSZ, v); 477248925Snp 478248925Snp m = v = F_TDDPTAGTCB; 479248925Snp t4_set_reg_field(sc, A_ULP_RX_CTL, m, v); 480248925Snp 481248925Snp m = V_INDICATESIZE(M_INDICATESIZE) | F_REARMDDPOFFSET | 482248925Snp F_RESETDDPOFFSET; 483248925Snp v = V_INDICATESIZE(indsz) | F_REARMDDPOFFSET | F_RESETDDPOFFSET; 484248925Snp t4_set_reg_field(sc, A_TP_PARA_REG5, m, v); 485248925Snp} 486248925Snp 487248925Snp/* 488281212Snp * SGE wants the buffer to be at least 64B and then a multiple of 16. If 489281212Snp * padding is is use the buffer's start and end need to be aligned to the pad 490281212Snp * boundary as well. We'll just make sure that the size is a multiple of the 491281212Snp * boundary here, it is up to the buffer allocation code to make sure the start 492281212Snp * of the buffer is aligned as well. 493265425Snp */ 494265425Snpstatic inline int 495281212Snphwsz_ok(struct adapter *sc, int hwsz) 496265425Snp{ 497281212Snp int mask = fl_pad ? sc->sge.pad_boundary - 1 : 16 - 1; 498265425Snp 499265425Snp return (hwsz >= 64 && (hwsz & mask) == 0); 500265425Snp} 501265425Snp 502265425Snp/* 503248925Snp * XXX: driver really should be able to deal with unexpected settings. 504248925Snp */ 505248925Snpint 506248925Snpt4_read_chip_settings(struct adapter *sc) 507248925Snp{ 508248925Snp struct sge *s = &sc->sge; 509255050Snp int i, j, n, rc = 0; 510248925Snp uint32_t m, v, r; 511248925Snp uint16_t indsz = min(RX_COPY_THRESHOLD - 1, M_INDICATESIZE); 512265425Snp static int sw_buf_sizes[] = { /* Sorted by size */ 513255050Snp MCLBYTES, 514255050Snp#if MJUMPAGESIZE != MCLBYTES 515255050Snp MJUMPAGESIZE, 516255050Snp#endif 517255050Snp MJUM9BYTES, 518255050Snp MJUM16BYTES 519255050Snp }; 520265425Snp struct sw_zone_info *swz, *safe_swz; 521265425Snp struct hw_buf_info *hwb; 522248925Snp 523255050Snp m = V_PKTSHIFT(M_PKTSHIFT) | F_RXPKTCPLMODE | F_EGRSTATUSPAGESIZE; 524248925Snp v = V_PKTSHIFT(fl_pktshift) | F_RXPKTCPLMODE | 525248925Snp V_EGRSTATUSPAGESIZE(spg_len == 128); 526248925Snp r = t4_read_reg(sc, A_SGE_CONTROL); 527248925Snp if ((r & m) != v) { 528248925Snp device_printf(sc->dev, "invalid SGE_CONTROL(0x%x)\n", r); 529228561Snp rc = EINVAL; 530228561Snp } 531281212Snp s->pad_boundary = 1 << (G_INGPADBOUNDARY(r) + 5); 532228561Snp 533281212Snp if (is_t4(sc)) 534281212Snp s->pack_boundary = s->pad_boundary; 535281212Snp else { 536281212Snp r = t4_read_reg(sc, A_SGE_CONTROL2); 537281212Snp if (G_INGPACKBOUNDARY(r) == 0) 538281212Snp s->pack_boundary = 16; 539255050Snp else 540281212Snp s->pack_boundary = 1 << (G_INGPACKBOUNDARY(r) + 5); 541255050Snp } 542255050Snp 543248925Snp v = V_HOSTPAGESIZEPF0(PAGE_SHIFT - 10) | 544248925Snp V_HOSTPAGESIZEPF1(PAGE_SHIFT - 10) | 545248925Snp V_HOSTPAGESIZEPF2(PAGE_SHIFT - 10) | 546248925Snp V_HOSTPAGESIZEPF3(PAGE_SHIFT - 10) | 547248925Snp V_HOSTPAGESIZEPF4(PAGE_SHIFT - 10) | 548248925Snp V_HOSTPAGESIZEPF5(PAGE_SHIFT - 10) | 549248925Snp V_HOSTPAGESIZEPF6(PAGE_SHIFT - 10) | 550248925Snp V_HOSTPAGESIZEPF7(PAGE_SHIFT - 10); 551248925Snp r = t4_read_reg(sc, A_SGE_HOST_PAGE_SIZE); 552248925Snp if (r != v) { 553248925Snp device_printf(sc->dev, "invalid SGE_HOST_PAGE_SIZE(0x%x)\n", r); 554228561Snp rc = EINVAL; 555228561Snp } 556228561Snp 557265425Snp /* Filter out unusable hw buffer sizes entirely (mark with -2). */ 558265425Snp hwb = &s->hw_buf_info[0]; 559265425Snp for (i = 0; i < nitems(s->hw_buf_info); i++, hwb++) { 560265425Snp r = t4_read_reg(sc, A_SGE_FL_BUFFER_SIZE0 + (4 * i)); 561265425Snp hwb->size = r; 562281212Snp hwb->zidx = hwsz_ok(sc, r) ? -1 : -2; 563265425Snp hwb->next = -1; 564265425Snp } 565265425Snp 566255050Snp /* 567265425Snp * Create a sorted list in decreasing order of hw buffer sizes (and so 568265425Snp * increasing order of spare area) for each software zone. 569281212Snp * 570281212Snp * If padding is enabled then the start and end of the buffer must align 571281212Snp * to the pad boundary; if packing is enabled then they must align with 572281212Snp * the pack boundary as well. Allocations from the cluster zones are 573281212Snp * aligned to min(size, 4K), so the buffer starts at that alignment and 574281212Snp * ends at hwb->size alignment. If mbuf inlining is allowed the 575281212Snp * starting alignment will be reduced to MSIZE and the driver will 576281212Snp * exercise appropriate caution when deciding on the best buffer layout 577281212Snp * to use. 578255050Snp */ 579265425Snp n = 0; /* no usable buffer size to begin with */ 580265425Snp swz = &s->sw_zone_info[0]; 581265425Snp safe_swz = NULL; 582265425Snp for (i = 0; i < SW_ZONE_SIZES; i++, swz++) { 583265425Snp int8_t head = -1, tail = -1; 584265425Snp 585265425Snp swz->size = sw_buf_sizes[i]; 586265425Snp swz->zone = m_getzone(swz->size); 587265425Snp swz->type = m_gettype(swz->size); 588265425Snp 589281212Snp if (swz->size < PAGE_SIZE) { 590281212Snp MPASS(powerof2(swz->size)); 591281212Snp if (fl_pad && (swz->size % sc->sge.pad_boundary != 0)) 592281212Snp continue; 593281212Snp } 594281212Snp 595265425Snp if (swz->size == safest_rx_cluster) 596265425Snp safe_swz = swz; 597265425Snp 598265425Snp hwb = &s->hw_buf_info[0]; 599265425Snp for (j = 0; j < SGE_FLBUF_SIZES; j++, hwb++) { 600265425Snp if (hwb->zidx != -1 || hwb->size > swz->size) 601265425Snp continue; 602281212Snp#ifdef INVARIANTS 603281212Snp if (fl_pad) 604281212Snp MPASS(hwb->size % sc->sge.pad_boundary == 0); 605281212Snp#endif 606265425Snp hwb->zidx = i; 607265425Snp if (head == -1) 608265425Snp head = tail = j; 609265425Snp else if (hwb->size < s->hw_buf_info[tail].size) { 610265425Snp s->hw_buf_info[tail].next = j; 611265425Snp tail = j; 612265425Snp } else { 613265425Snp int8_t *cur; 614265425Snp struct hw_buf_info *t; 615265425Snp 616265425Snp for (cur = &head; *cur != -1; cur = &t->next) { 617265425Snp t = &s->hw_buf_info[*cur]; 618265425Snp if (hwb->size == t->size) { 619265425Snp hwb->zidx = -2; 620265425Snp break; 621265425Snp } 622265425Snp if (hwb->size > t->size) { 623265425Snp hwb->next = *cur; 624265425Snp *cur = j; 625265425Snp break; 626265425Snp } 627265425Snp } 628265425Snp } 629228561Snp } 630265425Snp swz->head_hwidx = head; 631265425Snp swz->tail_hwidx = tail; 632265425Snp 633265425Snp if (tail != -1) { 634255050Snp n++; 635265425Snp if (swz->size - s->hw_buf_info[tail].size >= 636265425Snp CL_METADATA_SIZE) 637265425Snp sc->flags |= BUF_PACKING_OK; 638255050Snp } 639255050Snp } 640255050Snp if (n == 0) { 641255050Snp device_printf(sc->dev, "no usable SGE FL buffer size.\n"); 642255050Snp rc = EINVAL; 643255050Snp } 644218792Snp 645265425Snp s->safe_hwidx1 = -1; 646265425Snp s->safe_hwidx2 = -1; 647265425Snp if (safe_swz != NULL) { 648265425Snp s->safe_hwidx1 = safe_swz->head_hwidx; 649265425Snp for (i = safe_swz->head_hwidx; i != -1; i = hwb->next) { 650265425Snp int spare; 651265425Snp 652265425Snp hwb = &s->hw_buf_info[i]; 653281212Snp#ifdef INVARIANTS 654281212Snp if (fl_pad) 655281212Snp MPASS(hwb->size % sc->sge.pad_boundary == 0); 656281212Snp#endif 657265425Snp spare = safe_swz->size - hwb->size; 658281212Snp if (spare >= CL_METADATA_SIZE) { 659265425Snp s->safe_hwidx2 = i; 660265425Snp break; 661281212Snp } 662265425Snp } 663265425Snp } 664265425Snp 665248925Snp r = t4_read_reg(sc, A_SGE_INGRESS_RX_THRESHOLD); 666248925Snp s->counter_val[0] = G_THRESHOLD_0(r); 667248925Snp s->counter_val[1] = G_THRESHOLD_1(r); 668248925Snp s->counter_val[2] = G_THRESHOLD_2(r); 669248925Snp s->counter_val[3] = G_THRESHOLD_3(r); 670222701Snp 671248925Snp r = t4_read_reg(sc, A_SGE_TIMER_VALUE_0_AND_1); 672248925Snp s->timer_val[0] = G_TIMERVALUE0(r) / core_ticks_per_usec(sc); 673248925Snp s->timer_val[1] = G_TIMERVALUE1(r) / core_ticks_per_usec(sc); 674248925Snp r = t4_read_reg(sc, A_SGE_TIMER_VALUE_2_AND_3); 675248925Snp s->timer_val[2] = G_TIMERVALUE2(r) / core_ticks_per_usec(sc); 676248925Snp s->timer_val[3] = G_TIMERVALUE3(r) / core_ticks_per_usec(sc); 677248925Snp r = t4_read_reg(sc, A_SGE_TIMER_VALUE_4_AND_5); 678248925Snp s->timer_val[4] = G_TIMERVALUE4(r) / core_ticks_per_usec(sc); 679248925Snp s->timer_val[5] = G_TIMERVALUE5(r) / core_ticks_per_usec(sc); 680218792Snp 681248925Snp v = V_HPZ0(0) | V_HPZ1(2) | V_HPZ2(4) | V_HPZ3(6); 682248925Snp r = t4_read_reg(sc, A_ULP_RX_TDDP_PSZ); 683248925Snp if (r != v) { 684248925Snp device_printf(sc->dev, "invalid ULP_RX_TDDP_PSZ(0x%x)\n", r); 685248925Snp rc = EINVAL; 686248925Snp } 687228561Snp 688248925Snp m = v = F_TDDPTAGTCB; 689248925Snp r = t4_read_reg(sc, A_ULP_RX_CTL); 690248925Snp if ((r & m) != v) { 691248925Snp device_printf(sc->dev, "invalid ULP_RX_CTL(0x%x)\n", r); 692248925Snp rc = EINVAL; 693248925Snp } 694239336Snp 695248925Snp m = V_INDICATESIZE(M_INDICATESIZE) | F_REARMDDPOFFSET | 696248925Snp F_RESETDDPOFFSET; 697248925Snp v = V_INDICATESIZE(indsz) | F_REARMDDPOFFSET | F_RESETDDPOFFSET; 698248925Snp r = t4_read_reg(sc, A_TP_PARA_REG5); 699248925Snp if ((r & m) != v) { 700248925Snp device_printf(sc->dev, "invalid TP_PARA_REG5(0x%x)\n", r); 701248925Snp rc = EINVAL; 702248925Snp } 703248925Snp 704248925Snp r = t4_read_reg(sc, A_SGE_CONM_CTRL); 705248925Snp s->fl_starve_threshold = G_EGRTHRESHOLD(r) * 2 + 1; 706265410Snp if (is_t4(sc)) 707265410Snp s->fl_starve_threshold2 = s->fl_starve_threshold; 708265410Snp else 709265410Snp s->fl_starve_threshold2 = G_EGRTHRESHOLDPACKING(r) * 2 + 1; 710248925Snp 711256794Snp /* egress queues: log2 of # of doorbells per BAR2 page */ 712256794Snp r = t4_read_reg(sc, A_SGE_EGRESS_QUEUES_PER_PAGE_PF); 713256794Snp r >>= S_QUEUESPERPAGEPF0 + 714256794Snp (S_QUEUESPERPAGEPF1 - S_QUEUESPERPAGEPF0) * sc->pf; 715256794Snp s->eq_s_qpp = r & M_QUEUESPERPAGEPF0; 716248925Snp 717256794Snp /* ingress queues: log2 of # of doorbells per BAR2 page */ 718256794Snp r = t4_read_reg(sc, A_SGE_INGRESS_QUEUES_PER_PAGE_PF); 719256794Snp r >>= S_QUEUESPERPAGEPF0 + 720256794Snp (S_QUEUESPERPAGEPF1 - S_QUEUESPERPAGEPF0) * sc->pf; 721256794Snp s->iq_s_qpp = r & M_QUEUESPERPAGEPF0; 722256794Snp 723252705Snp t4_init_tp_params(sc); 724248925Snp 725248925Snp t4_read_mtu_tbl(sc, sc->params.mtus, NULL); 726248925Snp t4_load_mtus(sc, sc->params.mtus, sc->params.a_wnd, sc->params.b_wnd); 727248925Snp 728228561Snp return (rc); 729218792Snp} 730218792Snp 731218792Snpint 732218792Snpt4_create_dma_tag(struct adapter *sc) 733218792Snp{ 734218792Snp int rc; 735218792Snp 736218792Snp rc = bus_dma_tag_create(bus_get_dma_tag(sc->dev), 1, 0, 737218792Snp BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, BUS_SPACE_MAXSIZE, 738218792Snp BUS_SPACE_UNRESTRICTED, BUS_SPACE_MAXSIZE, BUS_DMA_ALLOCNOW, NULL, 739218792Snp NULL, &sc->dmat); 740218792Snp if (rc != 0) { 741218792Snp device_printf(sc->dev, 742218792Snp "failed to create main DMA tag: %d\n", rc); 743218792Snp } 744218792Snp 745218792Snp return (rc); 746218792Snp} 747218792Snp 748253829Snpvoid 749253829Snpt4_sge_sysctls(struct adapter *sc, struct sysctl_ctx_list *ctx, 750253829Snp struct sysctl_oid_list *children) 751253829Snp{ 752253829Snp 753265425Snp SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "buffer_sizes", 754265425Snp CTLTYPE_STRING | CTLFLAG_RD, &sc->sge, 0, sysctl_bufsizes, "A", 755265425Snp "freelist buffer sizes"); 756265425Snp 757253829Snp SYSCTL_ADD_INT(ctx, children, OID_AUTO, "fl_pktshift", CTLFLAG_RD, 758253829Snp NULL, fl_pktshift, "payload DMA offset in rx buffer (bytes)"); 759253829Snp 760253829Snp SYSCTL_ADD_INT(ctx, children, OID_AUTO, "fl_pad", CTLFLAG_RD, 761281212Snp NULL, sc->sge.pad_boundary, "payload pad boundary (bytes)"); 762253829Snp 763253829Snp SYSCTL_ADD_INT(ctx, children, OID_AUTO, "spg_len", CTLFLAG_RD, 764253829Snp NULL, spg_len, "status page size (bytes)"); 765253829Snp 766253829Snp SYSCTL_ADD_INT(ctx, children, OID_AUTO, "cong_drop", CTLFLAG_RD, 767253829Snp NULL, cong_drop, "congestion drop setting"); 768255050Snp 769255050Snp SYSCTL_ADD_INT(ctx, children, OID_AUTO, "fl_pack", CTLFLAG_RD, 770265425Snp NULL, sc->sge.pack_boundary, "payload pack boundary (bytes)"); 771253829Snp} 772253829Snp 773218792Snpint 774218792Snpt4_destroy_dma_tag(struct adapter *sc) 775218792Snp{ 776218792Snp if (sc->dmat) 777218792Snp bus_dma_tag_destroy(sc->dmat); 778218792Snp 779218792Snp return (0); 780218792Snp} 781218792Snp 782218792Snp/* 783228561Snp * Allocate and initialize the firmware event queue and the management queue. 784218792Snp * 785218792Snp * Returns errno on failure. Resources allocated up to that point may still be 786218792Snp * allocated. Caller is responsible for cleanup in case this function fails. 787218792Snp */ 788218792Snpint 789220873Snpt4_setup_adapter_queues(struct adapter *sc) 790218792Snp{ 791228561Snp int rc; 792218792Snp 793218792Snp ADAPTER_LOCK_ASSERT_NOTOWNED(sc); 794218792Snp 795228561Snp sysctl_ctx_init(&sc->ctx); 796228561Snp sc->flags |= ADAP_SYSCTL_CTX; 797220873Snp 798222510Snp /* 799222510Snp * Firmware event queue 800222510Snp */ 801228561Snp rc = alloc_fwq(sc); 802241398Snp if (rc != 0) 803220873Snp return (rc); 804218792Snp 805220873Snp /* 806228561Snp * Management queue. This is just a control queue that uses the fwq as 807228561Snp * its associated iq. 808220873Snp */ 809228561Snp rc = alloc_mgmtq(sc); 810220873Snp 811218792Snp return (rc); 812218792Snp} 813218792Snp 814218792Snp/* 815218792Snp * Idempotent 816218792Snp */ 817218792Snpint 818220873Snpt4_teardown_adapter_queues(struct adapter *sc) 819218792Snp{ 820218792Snp 821218792Snp ADAPTER_LOCK_ASSERT_NOTOWNED(sc); 822218792Snp 823228561Snp /* Do this before freeing the queue */ 824228561Snp if (sc->flags & ADAP_SYSCTL_CTX) { 825220873Snp sysctl_ctx_free(&sc->ctx); 826228561Snp sc->flags &= ~ADAP_SYSCTL_CTX; 827220873Snp } 828220873Snp 829228561Snp free_mgmtq(sc); 830228561Snp free_fwq(sc); 831220873Snp 832228561Snp return (0); 833228561Snp} 834222510Snp 835228561Snpstatic inline int 836270297Snpport_intr_count(struct port_info *pi) 837270297Snp{ 838270297Snp int rc = 0; 839270297Snp 840270297Snp if (pi->flags & INTR_RXQ) 841270297Snp rc += pi->nrxq; 842270297Snp#ifdef TCP_OFFLOAD 843270297Snp if (pi->flags & INTR_OFLD_RXQ) 844270297Snp rc += pi->nofldrxq; 845270297Snp#endif 846270297Snp#ifdef DEV_NETMAP 847270297Snp if (pi->flags & INTR_NM_RXQ) 848270297Snp rc += pi->nnmrxq; 849270297Snp#endif 850270297Snp return (rc); 851270297Snp} 852270297Snp 853270297Snpstatic inline int 854228561Snpfirst_vector(struct port_info *pi) 855228561Snp{ 856228561Snp struct adapter *sc = pi->adapter; 857228561Snp int rc = T4_EXTRA_INTR, i; 858228561Snp 859228561Snp if (sc->intr_count == 1) 860228561Snp return (0); 861228561Snp 862228561Snp for_each_port(sc, i) { 863228561Snp if (i == pi->port_id) 864228561Snp break; 865228561Snp 866270297Snp rc += port_intr_count(sc->port[i]); 867218792Snp } 868218792Snp 869228561Snp return (rc); 870218792Snp} 871218792Snp 872228561Snp/* 873228561Snp * Given an arbitrary "index," come up with an iq that can be used by other 874228561Snp * queues (of this port) for interrupt forwarding, SGE egress updates, etc. 875228561Snp * The iq returned is guaranteed to be something that takes direct interrupts. 876228561Snp */ 877228561Snpstatic struct sge_iq * 878228561Snpport_intr_iq(struct port_info *pi, int idx) 879228561Snp{ 880228561Snp struct adapter *sc = pi->adapter; 881228561Snp struct sge *s = &sc->sge; 882228561Snp struct sge_iq *iq = NULL; 883270297Snp int nintr, i; 884228561Snp 885228561Snp if (sc->intr_count == 1) 886228561Snp return (&sc->sge.fwq); 887228561Snp 888270297Snp nintr = port_intr_count(pi); 889270297Snp KASSERT(nintr != 0, 890270297Snp ("%s: pi %p has no exclusive interrupts, total interrupts = %d", 891270297Snp __func__, pi, sc->intr_count)); 892270297Snp#ifdef DEV_NETMAP 893270297Snp /* Exclude netmap queues as they can't take anyone else's interrupts */ 894270297Snp if (pi->flags & INTR_NM_RXQ) 895270297Snp nintr -= pi->nnmrxq; 896270297Snp KASSERT(nintr > 0, 897270297Snp ("%s: pi %p has nintr %d after netmap adjustment of %d", __func__, 898270297Snp pi, nintr, pi->nnmrxq)); 899270297Snp#endif 900270297Snp i = idx % nintr; 901265425Snp 902270297Snp if (pi->flags & INTR_RXQ) { 903270297Snp if (i < pi->nrxq) { 904270297Snp iq = &s->rxq[pi->first_rxq + i].iq; 905270297Snp goto done; 906270297Snp } 907270297Snp i -= pi->nrxq; 908228561Snp } 909270297Snp#ifdef TCP_OFFLOAD 910270297Snp if (pi->flags & INTR_OFLD_RXQ) { 911270297Snp if (i < pi->nofldrxq) { 912270297Snp iq = &s->ofld_rxq[pi->first_ofld_rxq + i].iq; 913270297Snp goto done; 914270297Snp } 915270297Snp i -= pi->nofldrxq; 916270297Snp } 917228561Snp#endif 918270297Snp panic("%s: pi %p, intr_flags 0x%lx, idx %d, total intr %d\n", __func__, 919270297Snp pi, pi->flags & INTR_ALL, idx, nintr); 920270297Snpdone: 921270297Snp MPASS(iq != NULL); 922270297Snp KASSERT(iq->flags & IQ_INTR, 923270297Snp ("%s: iq %p (port %p, intr_flags 0x%lx, idx %d)", __func__, iq, pi, 924270297Snp pi->flags & INTR_ALL, idx)); 925228561Snp return (iq); 926228561Snp} 927228561Snp 928265425Snp/* Maximum payload that can be delivered with a single iq descriptor */ 929239266Snpstatic inline int 930265425Snpmtu_to_max_payload(struct adapter *sc, int mtu, const int toe) 931239266Snp{ 932265425Snp int payload; 933239266Snp 934252728Snp#ifdef TCP_OFFLOAD 935265425Snp if (toe) { 936265425Snp payload = sc->tt.rx_coalesce ? 937265425Snp G_RXCOALESCESIZE(t4_read_reg(sc, A_TP_PARA_REG2)) : mtu; 938265425Snp } else { 939265425Snp#endif 940265425Snp /* large enough even when hw VLAN extraction is disabled */ 941265425Snp payload = fl_pktshift + ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN + 942265425Snp mtu; 943265425Snp#ifdef TCP_OFFLOAD 944265425Snp } 945265425Snp#endif 946252728Snp 947265425Snp return (payload); 948252728Snp} 949252728Snp 950218792Snpint 951228561Snpt4_setup_port_queues(struct port_info *pi) 952218792Snp{ 953228561Snp int rc = 0, i, j, intr_idx, iqid; 954218792Snp struct sge_rxq *rxq; 955218792Snp struct sge_txq *txq; 956228561Snp struct sge_wrq *ctrlq; 957237263Snp#ifdef TCP_OFFLOAD 958228561Snp struct sge_ofld_rxq *ofld_rxq; 959228561Snp struct sge_wrq *ofld_txq; 960228561Snp#endif 961270297Snp#ifdef DEV_NETMAP 962270297Snp struct sge_nm_rxq *nm_rxq; 963270297Snp struct sge_nm_txq *nm_txq; 964270297Snp#endif 965218792Snp char name[16]; 966218792Snp struct adapter *sc = pi->adapter; 967252728Snp struct ifnet *ifp = pi->ifp; 968237263Snp struct sysctl_oid *oid = device_get_sysctl_tree(pi->dev); 969228561Snp struct sysctl_oid_list *children = SYSCTL_CHILDREN(oid); 970281212Snp int maxp, mtu = ifp->if_mtu; 971218792Snp 972228561Snp /* Interrupt vector to start from (when using multiple vectors) */ 973228561Snp intr_idx = first_vector(pi); 974228561Snp 975228561Snp /* 976270297Snp * First pass over all NIC and TOE rx queues: 977228561Snp * a) initialize iq and fl 978228561Snp * b) allocate queue iff it will take direct interrupts. 979228561Snp */ 980265425Snp maxp = mtu_to_max_payload(sc, mtu, 0); 981270297Snp if (pi->flags & INTR_RXQ) { 982270297Snp oid = SYSCTL_ADD_NODE(&pi->ctx, children, OID_AUTO, "rxq", 983270297Snp CTLFLAG_RD, NULL, "rx queues"); 984270297Snp } 985218792Snp for_each_rxq(pi, i, rxq) { 986218792Snp 987270297Snp init_iq(&rxq->iq, sc, pi->tmr_idx, pi->pktc_idx, pi->qsize_rxq); 988218792Snp 989218792Snp snprintf(name, sizeof(name), "%s rxq%d-fl", 990218792Snp device_get_nameunit(pi->dev), i); 991281212Snp init_fl(sc, &rxq->fl, pi->qsize_rxq / 8, maxp, name); 992218792Snp 993270297Snp if (pi->flags & INTR_RXQ) { 994228561Snp rxq->iq.flags |= IQ_INTR; 995228561Snp rc = alloc_rxq(pi, rxq, intr_idx, i, oid); 996228561Snp if (rc != 0) 997228561Snp goto done; 998228561Snp intr_idx++; 999228561Snp } 1000228561Snp } 1001237263Snp#ifdef TCP_OFFLOAD 1002265425Snp maxp = mtu_to_max_payload(sc, mtu, 1); 1003270297Snp if (is_offload(sc) && pi->flags & INTR_OFLD_RXQ) { 1004270297Snp oid = SYSCTL_ADD_NODE(&pi->ctx, children, OID_AUTO, "ofld_rxq", 1005270297Snp CTLFLAG_RD, NULL, 1006270297Snp "rx queues for offloaded TCP connections"); 1007270297Snp } 1008228561Snp for_each_ofld_rxq(pi, i, ofld_rxq) { 1009228561Snp 1010228561Snp init_iq(&ofld_rxq->iq, sc, pi->tmr_idx, pi->pktc_idx, 1011270297Snp pi->qsize_rxq); 1012228561Snp 1013228561Snp snprintf(name, sizeof(name), "%s ofld_rxq%d-fl", 1014228561Snp device_get_nameunit(pi->dev), i); 1015281212Snp init_fl(sc, &ofld_rxq->fl, pi->qsize_rxq / 8, maxp, name); 1016228561Snp 1017270297Snp if (pi->flags & INTR_OFLD_RXQ) { 1018228561Snp ofld_rxq->iq.flags |= IQ_INTR; 1019270297Snp rc = alloc_ofld_rxq(pi, ofld_rxq, intr_idx, i, oid); 1020228561Snp if (rc != 0) 1021228561Snp goto done; 1022228561Snp intr_idx++; 1023228561Snp } 1024228561Snp } 1025228561Snp#endif 1026270297Snp#ifdef DEV_NETMAP 1027270297Snp /* 1028270297Snp * We don't have buffers to back the netmap rx queues right now so we 1029270297Snp * create the queues in a way that doesn't set off any congestion signal 1030270297Snp * in the chip. 1031270297Snp */ 1032270297Snp if (pi->flags & INTR_NM_RXQ) { 1033270297Snp oid = SYSCTL_ADD_NODE(&pi->ctx, children, OID_AUTO, "nm_rxq", 1034270297Snp CTLFLAG_RD, NULL, "rx queues for netmap"); 1035270297Snp for_each_nm_rxq(pi, i, nm_rxq) { 1036270297Snp rc = alloc_nm_rxq(pi, nm_rxq, intr_idx, i, oid); 1037270297Snp if (rc != 0) 1038270297Snp goto done; 1039270297Snp intr_idx++; 1040270297Snp } 1041270297Snp } 1042270297Snp#endif 1043228561Snp 1044228561Snp /* 1045270297Snp * Second pass over all NIC and TOE rx queues. The queues forwarding 1046228561Snp * their interrupts are allocated now. 1047228561Snp */ 1048228561Snp j = 0; 1049270297Snp if (!(pi->flags & INTR_RXQ)) { 1050270297Snp oid = SYSCTL_ADD_NODE(&pi->ctx, children, OID_AUTO, "rxq", 1051270297Snp CTLFLAG_RD, NULL, "rx queues"); 1052270297Snp for_each_rxq(pi, i, rxq) { 1053270297Snp MPASS(!(rxq->iq.flags & IQ_INTR)); 1054228561Snp 1055270297Snp intr_idx = port_intr_iq(pi, j)->abs_id; 1056228561Snp 1057270297Snp rc = alloc_rxq(pi, rxq, intr_idx, i, oid); 1058270297Snp if (rc != 0) 1059270297Snp goto done; 1060270297Snp j++; 1061270297Snp } 1062218792Snp } 1063237263Snp#ifdef TCP_OFFLOAD 1064270297Snp if (is_offload(sc) && !(pi->flags & INTR_OFLD_RXQ)) { 1065270297Snp oid = SYSCTL_ADD_NODE(&pi->ctx, children, OID_AUTO, "ofld_rxq", 1066270297Snp CTLFLAG_RD, NULL, 1067270297Snp "rx queues for offloaded TCP connections"); 1068270297Snp for_each_ofld_rxq(pi, i, ofld_rxq) { 1069270297Snp MPASS(!(ofld_rxq->iq.flags & IQ_INTR)); 1070228561Snp 1071270297Snp intr_idx = port_intr_iq(pi, j)->abs_id; 1072228561Snp 1073270297Snp rc = alloc_ofld_rxq(pi, ofld_rxq, intr_idx, i, oid); 1074270297Snp if (rc != 0) 1075270297Snp goto done; 1076270297Snp j++; 1077270297Snp } 1078228561Snp } 1079228561Snp#endif 1080270297Snp#ifdef DEV_NETMAP 1081270297Snp if (!(pi->flags & INTR_NM_RXQ)) 1082270297Snp CXGBE_UNIMPLEMENTED(__func__); 1083270297Snp#endif 1084228561Snp 1085228561Snp /* 1086228561Snp * Now the tx queues. Only one pass needed. 1087228561Snp */ 1088228561Snp oid = SYSCTL_ADD_NODE(&pi->ctx, children, OID_AUTO, "txq", CTLFLAG_RD, 1089228561Snp NULL, "tx queues"); 1090228561Snp j = 0; 1091218792Snp for_each_txq(pi, i, txq) { 1092228561Snp iqid = port_intr_iq(pi, j)->cntxt_id; 1093218792Snp snprintf(name, sizeof(name), "%s txq%d", 1094218792Snp device_get_nameunit(pi->dev), i); 1095228561Snp init_eq(&txq->eq, EQ_ETH, pi->qsize_txq, pi->tx_chan, iqid, 1096228561Snp name); 1097218792Snp 1098228561Snp rc = alloc_txq(pi, txq, i, oid); 1099218792Snp if (rc != 0) 1100218792Snp goto done; 1101228561Snp j++; 1102218792Snp } 1103237263Snp#ifdef TCP_OFFLOAD 1104228561Snp oid = SYSCTL_ADD_NODE(&pi->ctx, children, OID_AUTO, "ofld_txq", 1105228561Snp CTLFLAG_RD, NULL, "tx queues for offloaded TCP connections"); 1106228561Snp for_each_ofld_txq(pi, i, ofld_txq) { 1107270297Snp struct sysctl_oid *oid2; 1108228561Snp 1109228561Snp iqid = port_intr_iq(pi, j)->cntxt_id; 1110228561Snp snprintf(name, sizeof(name), "%s ofld_txq%d", 1111228561Snp device_get_nameunit(pi->dev), i); 1112228561Snp init_eq(&ofld_txq->eq, EQ_OFLD, pi->qsize_txq, pi->tx_chan, 1113228561Snp iqid, name); 1114228561Snp 1115228561Snp snprintf(name, sizeof(name), "%d", i); 1116228561Snp oid2 = SYSCTL_ADD_NODE(&pi->ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 1117228561Snp name, CTLFLAG_RD, NULL, "offload tx queue"); 1118228561Snp 1119228561Snp rc = alloc_wrq(sc, pi, ofld_txq, oid2); 1120228561Snp if (rc != 0) 1121228561Snp goto done; 1122228561Snp j++; 1123228561Snp } 1124228561Snp#endif 1125270297Snp#ifdef DEV_NETMAP 1126270297Snp oid = SYSCTL_ADD_NODE(&pi->ctx, children, OID_AUTO, "nm_txq", 1127270297Snp CTLFLAG_RD, NULL, "tx queues for netmap use"); 1128270297Snp for_each_nm_txq(pi, i, nm_txq) { 1129270297Snp iqid = pi->first_nm_rxq + (j % pi->nnmrxq); 1130270297Snp rc = alloc_nm_txq(pi, nm_txq, iqid, i, oid); 1131270297Snp if (rc != 0) 1132270297Snp goto done; 1133270297Snp j++; 1134270297Snp } 1135270297Snp#endif 1136228561Snp 1137228561Snp /* 1138228561Snp * Finally, the control queue. 1139228561Snp */ 1140228561Snp oid = SYSCTL_ADD_NODE(&pi->ctx, children, OID_AUTO, "ctrlq", CTLFLAG_RD, 1141228561Snp NULL, "ctrl queue"); 1142228561Snp ctrlq = &sc->sge.ctrlq[pi->port_id]; 1143228561Snp iqid = port_intr_iq(pi, 0)->cntxt_id; 1144228561Snp snprintf(name, sizeof(name), "%s ctrlq", device_get_nameunit(pi->dev)); 1145228561Snp init_eq(&ctrlq->eq, EQ_CTRL, CTRL_EQ_QSIZE, pi->tx_chan, iqid, name); 1146228561Snp rc = alloc_wrq(sc, pi, ctrlq, oid); 1147228561Snp 1148218792Snpdone: 1149218792Snp if (rc) 1150228561Snp t4_teardown_port_queues(pi); 1151218792Snp 1152218792Snp return (rc); 1153218792Snp} 1154218792Snp 1155218792Snp/* 1156218792Snp * Idempotent 1157218792Snp */ 1158218792Snpint 1159228561Snpt4_teardown_port_queues(struct port_info *pi) 1160218792Snp{ 1161218792Snp int i; 1162228561Snp struct adapter *sc = pi->adapter; 1163218792Snp struct sge_rxq *rxq; 1164218792Snp struct sge_txq *txq; 1165237263Snp#ifdef TCP_OFFLOAD 1166228561Snp struct sge_ofld_rxq *ofld_rxq; 1167228561Snp struct sge_wrq *ofld_txq; 1168228561Snp#endif 1169270297Snp#ifdef DEV_NETMAP 1170270297Snp struct sge_nm_rxq *nm_rxq; 1171270297Snp struct sge_nm_txq *nm_txq; 1172270297Snp#endif 1173218792Snp 1174218792Snp /* Do this before freeing the queues */ 1175228561Snp if (pi->flags & PORT_SYSCTL_CTX) { 1176218792Snp sysctl_ctx_free(&pi->ctx); 1177228561Snp pi->flags &= ~PORT_SYSCTL_CTX; 1178218792Snp } 1179218792Snp 1180228561Snp /* 1181228561Snp * Take down all the tx queues first, as they reference the rx queues 1182228561Snp * (for egress updates, etc.). 1183228561Snp */ 1184228561Snp 1185228561Snp free_wrq(sc, &sc->sge.ctrlq[pi->port_id]); 1186228561Snp 1187218792Snp for_each_txq(pi, i, txq) { 1188218792Snp free_txq(pi, txq); 1189218792Snp } 1190237263Snp#ifdef TCP_OFFLOAD 1191228561Snp for_each_ofld_txq(pi, i, ofld_txq) { 1192228561Snp free_wrq(sc, ofld_txq); 1193228561Snp } 1194228561Snp#endif 1195270297Snp#ifdef DEV_NETMAP 1196270297Snp for_each_nm_txq(pi, i, nm_txq) 1197270297Snp free_nm_txq(pi, nm_txq); 1198270297Snp#endif 1199228561Snp 1200228561Snp /* 1201228561Snp * Then take down the rx queues that forward their interrupts, as they 1202228561Snp * reference other rx queues. 1203228561Snp */ 1204228561Snp 1205218792Snp for_each_rxq(pi, i, rxq) { 1206228561Snp if ((rxq->iq.flags & IQ_INTR) == 0) 1207228561Snp free_rxq(pi, rxq); 1208218792Snp } 1209237263Snp#ifdef TCP_OFFLOAD 1210228561Snp for_each_ofld_rxq(pi, i, ofld_rxq) { 1211228561Snp if ((ofld_rxq->iq.flags & IQ_INTR) == 0) 1212228561Snp free_ofld_rxq(pi, ofld_rxq); 1213228561Snp } 1214228561Snp#endif 1215270297Snp#ifdef DEV_NETMAP 1216270297Snp for_each_nm_rxq(pi, i, nm_rxq) 1217270297Snp free_nm_rxq(pi, nm_rxq); 1218270297Snp#endif 1219228561Snp 1220228561Snp /* 1221228561Snp * Then take down the rx queues that take direct interrupts. 1222228561Snp */ 1223228561Snp 1224228561Snp for_each_rxq(pi, i, rxq) { 1225228561Snp if (rxq->iq.flags & IQ_INTR) 1226228561Snp free_rxq(pi, rxq); 1227228561Snp } 1228237263Snp#ifdef TCP_OFFLOAD 1229228561Snp for_each_ofld_rxq(pi, i, ofld_rxq) { 1230228561Snp if (ofld_rxq->iq.flags & IQ_INTR) 1231228561Snp free_ofld_rxq(pi, ofld_rxq); 1232228561Snp } 1233228561Snp#endif 1234228561Snp 1235218792Snp return (0); 1236218792Snp} 1237218792Snp 1238228561Snp/* 1239228561Snp * Deals with errors and the firmware event queue. All data rx queues forward 1240228561Snp * their interrupt to the firmware event queue. 1241228561Snp */ 1242218792Snpvoid 1243218792Snpt4_intr_all(void *arg) 1244218792Snp{ 1245218792Snp struct adapter *sc = arg; 1246228561Snp struct sge_iq *fwq = &sc->sge.fwq; 1247218792Snp 1248218792Snp t4_intr_err(arg); 1249228561Snp if (atomic_cmpset_int(&fwq->state, IQS_IDLE, IQS_BUSY)) { 1250228561Snp service_iq(fwq, 0); 1251228561Snp atomic_cmpset_int(&fwq->state, IQS_BUSY, IQS_IDLE); 1252218792Snp } 1253218792Snp} 1254218792Snp 1255218792Snp/* Deals with error interrupts */ 1256218792Snpvoid 1257218792Snpt4_intr_err(void *arg) 1258218792Snp{ 1259218792Snp struct adapter *sc = arg; 1260218792Snp 1261222510Snp t4_write_reg(sc, MYPF_REG(A_PCIE_PF_CLI), 0); 1262218792Snp t4_slow_intr_handler(sc); 1263218792Snp} 1264218792Snp 1265218792Snpvoid 1266218792Snpt4_intr_evt(void *arg) 1267218792Snp{ 1268218792Snp struct sge_iq *iq = arg; 1269220649Snp 1270228561Snp if (atomic_cmpset_int(&iq->state, IQS_IDLE, IQS_BUSY)) { 1271228561Snp service_iq(iq, 0); 1272228561Snp atomic_cmpset_int(&iq->state, IQS_BUSY, IQS_IDLE); 1273222510Snp } 1274220649Snp} 1275220649Snp 1276228561Snpvoid 1277228561Snpt4_intr(void *arg) 1278220649Snp{ 1279220649Snp struct sge_iq *iq = arg; 1280228561Snp 1281228561Snp if (atomic_cmpset_int(&iq->state, IQS_IDLE, IQS_BUSY)) { 1282228561Snp service_iq(iq, 0); 1283228561Snp atomic_cmpset_int(&iq->state, IQS_BUSY, IQS_IDLE); 1284228561Snp } 1285228561Snp} 1286228561Snp 1287228561Snp/* 1288228561Snp * Deals with anything and everything on the given ingress queue. 1289228561Snp */ 1290228561Snpstatic int 1291228561Snpservice_iq(struct sge_iq *iq, int budget) 1292228561Snp{ 1293228561Snp struct sge_iq *q; 1294237263Snp struct sge_rxq *rxq = iq_to_rxq(iq); /* Use iff iq is part of rxq */ 1295270297Snp struct sge_fl *fl; /* Use iff IQ_HAS_FL */ 1296218792Snp struct adapter *sc = iq->adapter; 1297270297Snp struct iq_desc *d = &iq->desc[iq->cidx]; 1298270297Snp int ndescs = 0, limit; 1299270297Snp int rsp_type, refill; 1300228561Snp uint32_t lq; 1301270297Snp uint16_t fl_hw_cidx; 1302228561Snp struct mbuf *m0; 1303228561Snp STAILQ_HEAD(, sge_iq) iql = STAILQ_HEAD_INITIALIZER(iql); 1304255015Snp#if defined(INET) || defined(INET6) 1305255015Snp const struct timeval lro_timeout = {0, sc->lro_timeout}; 1306255015Snp#endif 1307218792Snp 1308228561Snp KASSERT(iq->state == IQS_BUSY, ("%s: iq %p not BUSY", __func__, iq)); 1309218792Snp 1310270297Snp limit = budget ? budget : iq->qsize / 16; 1311270297Snp 1312270297Snp if (iq->flags & IQ_HAS_FL) { 1313270297Snp fl = &rxq->fl; 1314270297Snp fl_hw_cidx = fl->hw_cidx; /* stable snapshot */ 1315270297Snp } else { 1316270297Snp fl = NULL; 1317270297Snp fl_hw_cidx = 0; /* to silence gcc warning */ 1318270297Snp } 1319270297Snp 1320228561Snp /* 1321228561Snp * We always come back and check the descriptor ring for new indirect 1322228561Snp * interrupts and other responses after running a single handler. 1323228561Snp */ 1324228561Snp for (;;) { 1325270297Snp while ((d->rsp.u.type_gen & F_RSPD_GEN) == iq->gen) { 1326218792Snp 1327228561Snp rmb(); 1328218792Snp 1329270297Snp refill = 0; 1330228561Snp m0 = NULL; 1331270297Snp rsp_type = G_RSPD_TYPE(d->rsp.u.type_gen); 1332270297Snp lq = be32toh(d->rsp.pldbuflen_qid); 1333218792Snp 1334228561Snp switch (rsp_type) { 1335228561Snp case X_RSPD_TYPE_FLBUF: 1336228561Snp 1337228561Snp KASSERT(iq->flags & IQ_HAS_FL, 1338228561Snp ("%s: data for an iq (%p) with no freelist", 1339228561Snp __func__, iq)); 1340228561Snp 1341270297Snp m0 = get_fl_payload(sc, fl, lq); 1342255050Snp if (__predict_false(m0 == NULL)) 1343255050Snp goto process_iql; 1344270297Snp refill = IDXDIFF(fl->hw_cidx, fl_hw_cidx, fl->sidx) > 2; 1345228561Snp#ifdef T4_PKT_TIMESTAMP 1346228561Snp /* 1347228561Snp * 60 bit timestamp for the payload is 1348228561Snp * *(uint64_t *)m0->m_pktdat. Note that it is 1349228561Snp * in the leading free-space in the mbuf. The 1350228561Snp * kernel can clobber it during a pullup, 1351228561Snp * m_copymdata, etc. You need to make sure that 1352228561Snp * the mbuf reaches you unmolested if you care 1353228561Snp * about the timestamp. 1354228561Snp */ 1355228561Snp *(uint64_t *)m0->m_pktdat = 1356228561Snp be64toh(ctrl->u.last_flit) & 1357228561Snp 0xfffffffffffffff; 1358228561Snp#endif 1359228561Snp 1360228561Snp /* fall through */ 1361228561Snp 1362228561Snp case X_RSPD_TYPE_CPL: 1363270297Snp KASSERT(d->rss.opcode < NUM_CPL_CMDS, 1364228561Snp ("%s: bad opcode %02x.", __func__, 1365270297Snp d->rss.opcode)); 1366270297Snp sc->cpl_handler[d->rss.opcode](iq, &d->rss, m0); 1367228561Snp break; 1368228561Snp 1369228561Snp case X_RSPD_TYPE_INTR: 1370228561Snp 1371228561Snp /* 1372228561Snp * Interrupts should be forwarded only to queues 1373228561Snp * that are not forwarding their interrupts. 1374228561Snp * This means service_iq can recurse but only 1 1375228561Snp * level deep. 1376228561Snp */ 1377228561Snp KASSERT(budget == 0, 1378228561Snp ("%s: budget %u, rsp_type %u", __func__, 1379228561Snp budget, rsp_type)); 1380228561Snp 1381255005Snp /* 1382255005Snp * There are 1K interrupt-capable queues (qids 0 1383255005Snp * through 1023). A response type indicating a 1384255005Snp * forwarded interrupt with a qid >= 1K is an 1385255005Snp * iWARP async notification. 1386255005Snp */ 1387255005Snp if (lq >= 1024) { 1388270297Snp sc->an_handler(iq, &d->rsp); 1389255005Snp break; 1390255005Snp } 1391255005Snp 1392228561Snp q = sc->sge.iqmap[lq - sc->sge.iq_start]; 1393228561Snp if (atomic_cmpset_int(&q->state, IQS_IDLE, 1394228561Snp IQS_BUSY)) { 1395270297Snp if (service_iq(q, q->qsize / 16) == 0) { 1396228561Snp atomic_cmpset_int(&q->state, 1397228561Snp IQS_BUSY, IQS_IDLE); 1398228561Snp } else { 1399228561Snp STAILQ_INSERT_TAIL(&iql, q, 1400228561Snp link); 1401228561Snp } 1402228561Snp } 1403228561Snp break; 1404228561Snp 1405228561Snp default: 1406255005Snp KASSERT(0, 1407255005Snp ("%s: illegal response type %d on iq %p", 1408255005Snp __func__, rsp_type, iq)); 1409255005Snp log(LOG_ERR, 1410255005Snp "%s: illegal response type %d on iq %p", 1411255005Snp device_get_nameunit(sc->dev), rsp_type, iq); 1412237263Snp break; 1413228561Snp } 1414228561Snp 1415270297Snp d++; 1416270297Snp if (__predict_false(++iq->cidx == iq->sidx)) { 1417270297Snp iq->cidx = 0; 1418270297Snp iq->gen ^= F_RSPD_GEN; 1419270297Snp d = &iq->desc[0]; 1420265425Snp } 1421270297Snp if (__predict_false(++ndescs == limit)) { 1422228561Snp t4_write_reg(sc, MYPF_REG(A_SGE_PF_GTS), 1423228561Snp V_CIDXINC(ndescs) | 1424228561Snp V_INGRESSQID(iq->cntxt_id) | 1425228561Snp V_SEINTARM(V_QINTR_TIMER_IDX(X_TIMERREG_UPDATE_CIDX))); 1426228561Snp ndescs = 0; 1427228561Snp 1428255015Snp#if defined(INET) || defined(INET6) 1429255015Snp if (iq->flags & IQ_LRO_ENABLED && 1430255015Snp sc->lro_timeout != 0) { 1431255015Snp tcp_lro_flush_inactive(&rxq->lro, 1432255015Snp &lro_timeout); 1433255015Snp } 1434255015Snp#endif 1435255015Snp 1436267244Snp if (budget) { 1437270297Snp if (iq->flags & IQ_HAS_FL) { 1438267244Snp FL_LOCK(fl); 1439267244Snp refill_fl(sc, fl, 32); 1440267244Snp FL_UNLOCK(fl); 1441267244Snp } 1442228561Snp return (EINPROGRESS); 1443267244Snp } 1444228561Snp } 1445270297Snp if (refill) { 1446270297Snp FL_LOCK(fl); 1447270297Snp refill_fl(sc, fl, 32); 1448270297Snp FL_UNLOCK(fl); 1449270297Snp fl_hw_cidx = fl->hw_cidx; 1450270297Snp } 1451218792Snp } 1452222510Snp 1453255050Snpprocess_iql: 1454228561Snp if (STAILQ_EMPTY(&iql)) 1455228561Snp break; 1456228561Snp 1457228561Snp /* 1458228561Snp * Process the head only, and send it to the back of the list if 1459228561Snp * it's still not done. 1460228561Snp */ 1461228561Snp q = STAILQ_FIRST(&iql); 1462228561Snp STAILQ_REMOVE_HEAD(&iql, link); 1463228561Snp if (service_iq(q, q->qsize / 8) == 0) 1464228561Snp atomic_cmpset_int(&q->state, IQS_BUSY, IQS_IDLE); 1465228561Snp else 1466228561Snp STAILQ_INSERT_TAIL(&iql, q, link); 1467218792Snp } 1468218792Snp 1469237819Snp#if defined(INET) || defined(INET6) 1470228561Snp if (iq->flags & IQ_LRO_ENABLED) { 1471228561Snp struct lro_ctrl *lro = &rxq->lro; 1472228561Snp struct lro_entry *l; 1473228561Snp 1474228561Snp while (!SLIST_EMPTY(&lro->lro_active)) { 1475228561Snp l = SLIST_FIRST(&lro->lro_active); 1476228561Snp SLIST_REMOVE_HEAD(&lro->lro_active, next); 1477228561Snp tcp_lro_flush(lro, l); 1478228561Snp } 1479228561Snp } 1480228561Snp#endif 1481228561Snp 1482228561Snp t4_write_reg(sc, MYPF_REG(A_SGE_PF_GTS), V_CIDXINC(ndescs) | 1483228561Snp V_INGRESSQID((u32)iq->cntxt_id) | V_SEINTARM(iq->intr_params)); 1484228561Snp 1485228561Snp if (iq->flags & IQ_HAS_FL) { 1486228561Snp int starved; 1487228561Snp 1488228561Snp FL_LOCK(fl); 1489265425Snp starved = refill_fl(sc, fl, 64); 1490228561Snp FL_UNLOCK(fl); 1491228561Snp if (__predict_false(starved != 0)) 1492228561Snp add_fl_to_sfl(sc, fl); 1493228561Snp } 1494228561Snp 1495228561Snp return (0); 1496218792Snp} 1497218792Snp 1498265425Snpstatic inline int 1499265425Snpcl_has_metadata(struct sge_fl *fl, struct cluster_layout *cll) 1500255050Snp{ 1501265425Snp int rc = fl->flags & FL_BUF_PACKING || cll->region1 > 0; 1502255050Snp 1503265425Snp if (rc) 1504265425Snp MPASS(cll->region3 >= CL_METADATA_SIZE); 1505255050Snp 1506265425Snp return (rc); 1507255050Snp} 1508255050Snp 1509265425Snpstatic inline struct cluster_metadata * 1510265425Snpcl_metadata(struct adapter *sc, struct sge_fl *fl, struct cluster_layout *cll, 1511265425Snp caddr_t cl) 1512255050Snp{ 1513255050Snp 1514265425Snp if (cl_has_metadata(fl, cll)) { 1515265425Snp struct sw_zone_info *swz = &sc->sge.sw_zone_info[cll->zidx]; 1516255050Snp 1517265425Snp return ((struct cluster_metadata *)(cl + swz->size) - 1); 1518255050Snp } 1519265425Snp return (NULL); 1520255050Snp} 1521255050Snp 1522255050Snpstatic int 1523255050Snprxb_free(struct mbuf *m, void *arg1, void *arg2) 1524255050Snp{ 1525255050Snp uma_zone_t zone = arg1; 1526255050Snp caddr_t cl = arg2; 1527255050Snp 1528255050Snp uma_zfree(zone, cl); 1529269356Snp counter_u64_add(extfree_rels, 1); 1530255050Snp 1531255050Snp return (EXT_FREE_OK); 1532255050Snp} 1533255050Snp 1534265425Snp/* 1535265425Snp * The mbuf returned by this function could be allocated from zone_mbuf or 1536265425Snp * constructed in spare room in the cluster. 1537265425Snp * 1538265425Snp * The mbuf carries the payload in one of these ways 1539265425Snp * a) frame inside the mbuf (mbuf from zone_mbuf) 1540265425Snp * b) m_cljset (for clusters without metadata) zone_mbuf 1541265425Snp * c) m_extaddref (cluster with metadata) inline mbuf 1542265425Snp * d) m_extaddref (cluster with metadata) zone_mbuf 1543265425Snp */ 1544255050Snpstatic struct mbuf * 1545281212Snpget_scatter_segment(struct adapter *sc, struct sge_fl *fl, int fr_offset, 1546281212Snp int remaining) 1547218792Snp{ 1548265425Snp struct mbuf *m; 1549228561Snp struct fl_sdesc *sd = &fl->sdesc[fl->cidx]; 1550265425Snp struct cluster_layout *cll = &sd->cll; 1551265425Snp struct sw_zone_info *swz = &sc->sge.sw_zone_info[cll->zidx]; 1552265425Snp struct hw_buf_info *hwb = &sc->sge.hw_buf_info[cll->hwidx]; 1553265425Snp struct cluster_metadata *clm = cl_metadata(sc, fl, cll, sd->cl); 1554281212Snp int len, blen; 1555265425Snp caddr_t payload; 1556218792Snp 1557281212Snp blen = hwb->size - fl->rx_offset; /* max possible in this buf */ 1558281212Snp len = min(remaining, blen); 1559265425Snp payload = sd->cl + cll->region1 + fl->rx_offset; 1560281212Snp if (fl->flags & FL_BUF_PACKING) { 1561281212Snp const u_int l = fr_offset + len; 1562281212Snp const u_int pad = roundup2(l, fl->buf_boundary) - l; 1563219290Snp 1564281212Snp if (fl->rx_offset + len + pad < hwb->size) 1565281212Snp blen = len + pad; 1566281212Snp MPASS(fl->rx_offset + blen <= hwb->size); 1567281212Snp } else { 1568281212Snp MPASS(fl->rx_offset == 0); /* not packing */ 1569281212Snp } 1570281212Snp 1571281212Snp 1572265425Snp if (sc->sc_do_rxcopy && len < RX_COPY_THRESHOLD) { 1573255050Snp 1574265425Snp /* 1575265425Snp * Copy payload into a freshly allocated mbuf. 1576265425Snp */ 1577255050Snp 1578281212Snp m = fr_offset == 0 ? 1579265425Snp m_gethdr(M_NOWAIT, MT_DATA) : m_get(M_NOWAIT, MT_DATA); 1580265425Snp if (m == NULL) 1581255050Snp return (NULL); 1582265425Snp fl->mbuf_allocated++; 1583255050Snp#ifdef T4_PKT_TIMESTAMP 1584265425Snp /* Leave room for a timestamp */ 1585265425Snp m->m_data += 8; 1586255050Snp#endif 1587265425Snp /* copy data to mbuf */ 1588265425Snp bcopy(payload, mtod(m, caddr_t), len); 1589255050Snp 1590269356Snp } else if (sd->nmbuf * MSIZE < cll->region1) { 1591255050Snp 1592265425Snp /* 1593265425Snp * There's spare room in the cluster for an mbuf. Create one 1594267694Snp * and associate it with the payload that's in the cluster. 1595265425Snp */ 1596255050Snp 1597265425Snp MPASS(clm != NULL); 1598269356Snp m = (struct mbuf *)(sd->cl + sd->nmbuf * MSIZE); 1599265425Snp /* No bzero required */ 1600281212Snp if (m_init(m, NULL, 0, M_NOWAIT, MT_DATA, 1601281212Snp fr_offset == 0 ? M_PKTHDR | M_NOFREE : M_NOFREE)) 1602265425Snp return (NULL); 1603265425Snp fl->mbuf_inlined++; 1604281212Snp m_extaddref(m, payload, blen, &clm->refcount, rxb_free, 1605265425Snp swz->zone, sd->cl); 1606269356Snp if (sd->nmbuf++ == 0) 1607269356Snp counter_u64_add(extfree_refs, 1); 1608255050Snp 1609265425Snp } else { 1610255050Snp 1611265425Snp /* 1612265425Snp * Grab an mbuf from zone_mbuf and associate it with the 1613265425Snp * payload in the cluster. 1614265425Snp */ 1615255050Snp 1616281212Snp m = fr_offset == 0 ? 1617265425Snp m_gethdr(M_NOWAIT, MT_DATA) : m_get(M_NOWAIT, MT_DATA); 1618265425Snp if (m == NULL) 1619265425Snp return (NULL); 1620265425Snp fl->mbuf_allocated++; 1621267694Snp if (clm != NULL) { 1622281212Snp m_extaddref(m, payload, blen, &clm->refcount, 1623265425Snp rxb_free, swz->zone, sd->cl); 1624269356Snp if (sd->nmbuf++ == 0) 1625269356Snp counter_u64_add(extfree_refs, 1); 1626267694Snp } else { 1627265425Snp m_cljset(m, sd->cl, swz->type); 1628265425Snp sd->cl = NULL; /* consumed, not a recycle candidate */ 1629255050Snp } 1630255050Snp } 1631281212Snp if (fr_offset == 0) 1632281212Snp m->m_pkthdr.len = remaining; 1633265425Snp m->m_len = len; 1634255050Snp 1635265425Snp if (fl->flags & FL_BUF_PACKING) { 1636281212Snp fl->rx_offset += blen; 1637265425Snp MPASS(fl->rx_offset <= hwb->size); 1638265425Snp if (fl->rx_offset < hwb->size) 1639265425Snp return (m); /* without advancing the cidx */ 1640265425Snp } 1641255050Snp 1642270297Snp if (__predict_false(++fl->cidx % 8 == 0)) { 1643270297Snp uint16_t cidx = fl->cidx / 8; 1644270297Snp 1645270297Snp if (__predict_false(cidx == fl->sidx)) 1646270297Snp fl->cidx = cidx = 0; 1647270297Snp fl->hw_cidx = cidx; 1648270297Snp } 1649265425Snp fl->rx_offset = 0; 1650255050Snp 1651265425Snp return (m); 1652255050Snp} 1653255050Snp 1654255050Snpstatic struct mbuf * 1655270297Snpget_fl_payload(struct adapter *sc, struct sge_fl *fl, uint32_t len_newbuf) 1656255050Snp{ 1657265425Snp struct mbuf *m0, *m, **pnext; 1658281212Snp u_int remaining; 1659281212Snp const u_int total = G_RSPD_LEN(len_newbuf); 1660255050Snp 1661270297Snp if (__predict_false(fl->flags & FL_BUF_RESUME)) { 1662266965Snp M_ASSERTPKTHDR(fl->m0); 1663281212Snp MPASS(fl->m0->m_pkthdr.len == total); 1664281212Snp MPASS(fl->remaining < total); 1665218792Snp 1666265425Snp m0 = fl->m0; 1667265425Snp pnext = fl->pnext; 1668281212Snp remaining = fl->remaining; 1669270297Snp fl->flags &= ~FL_BUF_RESUME; 1670265425Snp goto get_segment; 1671255050Snp } 1672255050Snp 1673265425Snp if (fl->rx_offset > 0 && len_newbuf & F_RSPD_NEWBUF) { 1674265425Snp fl->rx_offset = 0; 1675270297Snp if (__predict_false(++fl->cidx % 8 == 0)) { 1676270297Snp uint16_t cidx = fl->cidx / 8; 1677270297Snp 1678270297Snp if (__predict_false(cidx == fl->sidx)) 1679270297Snp fl->cidx = cidx = 0; 1680270297Snp fl->hw_cidx = cidx; 1681270297Snp } 1682228561Snp } 1683218792Snp 1684265425Snp /* 1685265425Snp * Payload starts at rx_offset in the current hw buffer. Its length is 1686265425Snp * 'len' and it may span multiple hw buffers. 1687265425Snp */ 1688218792Snp 1689281212Snp m0 = get_scatter_segment(sc, fl, 0, total); 1690266965Snp if (m0 == NULL) 1691270297Snp return (NULL); 1692281212Snp remaining = total - m0->m_len; 1693265425Snp pnext = &m0->m_next; 1694281212Snp while (remaining > 0) { 1695265425Snpget_segment: 1696265425Snp MPASS(fl->rx_offset == 0); 1697281212Snp m = get_scatter_segment(sc, fl, total - remaining, remaining); 1698270297Snp if (__predict_false(m == NULL)) { 1699265425Snp fl->m0 = m0; 1700265425Snp fl->pnext = pnext; 1701281212Snp fl->remaining = remaining; 1702270297Snp fl->flags |= FL_BUF_RESUME; 1703270297Snp return (NULL); 1704218792Snp } 1705265425Snp *pnext = m; 1706265425Snp pnext = &m->m_next; 1707281212Snp remaining -= m->m_len; 1708265425Snp } 1709265425Snp *pnext = NULL; 1710270297Snp 1711286273Snp M_ASSERTPKTHDR(m0); 1712228561Snp return (m0); 1713228561Snp} 1714218792Snp 1715228561Snpstatic int 1716228561Snpt4_eth_rx(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m0) 1717228561Snp{ 1718237463Snp struct sge_rxq *rxq = iq_to_rxq(iq); 1719228561Snp struct ifnet *ifp = rxq->ifp; 1720228561Snp const struct cpl_rx_pkt *cpl = (const void *)(rss + 1); 1721237819Snp#if defined(INET) || defined(INET6) 1722228561Snp struct lro_ctrl *lro = &rxq->lro; 1723228561Snp#endif 1724219290Snp 1725228561Snp KASSERT(m0 != NULL, ("%s: no payload with opcode %02x", __func__, 1726228561Snp rss->opcode)); 1727219290Snp 1728239258Snp m0->m_pkthdr.len -= fl_pktshift; 1729239258Snp m0->m_len -= fl_pktshift; 1730239258Snp m0->m_data += fl_pktshift; 1731219290Snp 1732228561Snp m0->m_pkthdr.rcvif = ifp; 1733281955Shiren M_HASHTYPE_SET(m0, M_HASHTYPE_OPAQUE); 1734259142Snp m0->m_pkthdr.flowid = be32toh(rss->hash_val); 1735219290Snp 1736237799Snp if (cpl->csum_calc && !cpl->err_vec) { 1737237799Snp if (ifp->if_capenable & IFCAP_RXCSUM && 1738237799Snp cpl->l2info & htobe32(F_RXF_IP)) { 1739237831Snp m0->m_pkthdr.csum_flags = (CSUM_IP_CHECKED | 1740237799Snp CSUM_IP_VALID | CSUM_DATA_VALID | CSUM_PSEUDO_HDR); 1741237799Snp rxq->rxcsum++; 1742237799Snp } else if (ifp->if_capenable & IFCAP_RXCSUM_IPV6 && 1743237799Snp cpl->l2info & htobe32(F_RXF_IP6)) { 1744237831Snp m0->m_pkthdr.csum_flags = (CSUM_DATA_VALID_IPV6 | 1745237799Snp CSUM_PSEUDO_HDR); 1746237799Snp rxq->rxcsum++; 1747237799Snp } 1748237799Snp 1749237799Snp if (__predict_false(cpl->ip_frag)) 1750228561Snp m0->m_pkthdr.csum_data = be16toh(cpl->csum); 1751228561Snp else 1752228561Snp m0->m_pkthdr.csum_data = 0xffff; 1753228561Snp } 1754219290Snp 1755228561Snp if (cpl->vlan_ex) { 1756228561Snp m0->m_pkthdr.ether_vtag = be16toh(cpl->vlan); 1757228561Snp m0->m_flags |= M_VLANTAG; 1758228561Snp rxq->vlan_extraction++; 1759228561Snp } 1760219290Snp 1761237819Snp#if defined(INET) || defined(INET6) 1762228561Snp if (cpl->l2info & htobe32(F_RXF_LRO) && 1763228561Snp iq->flags & IQ_LRO_ENABLED && 1764228561Snp tcp_lro_rx(lro, m0, 0) == 0) { 1765228561Snp /* queued for LRO */ 1766228561Snp } else 1767218792Snp#endif 1768228561Snp ifp->if_input(ifp, m0); 1769218792Snp 1770228561Snp return (0); 1771228561Snp} 1772218792Snp 1773228561Snp/* 1774284052Snp * Must drain the wrq or make sure that someone else will. 1775284052Snp */ 1776284052Snpstatic void 1777284052Snpwrq_tx_drain(void *arg, int n) 1778284052Snp{ 1779284052Snp struct sge_wrq *wrq = arg; 1780284052Snp struct sge_eq *eq = &wrq->eq; 1781284052Snp 1782284052Snp EQ_LOCK(eq); 1783284052Snp if (TAILQ_EMPTY(&wrq->incomplete_wrs) && !STAILQ_EMPTY(&wrq->wr_list)) 1784284052Snp drain_wrq_wr_list(wrq->adapter, wrq); 1785284052Snp EQ_UNLOCK(eq); 1786284052Snp} 1787284052Snp 1788284052Snpstatic void 1789284052Snpdrain_wrq_wr_list(struct adapter *sc, struct sge_wrq *wrq) 1790284052Snp{ 1791284052Snp struct sge_eq *eq = &wrq->eq; 1792284052Snp u_int available, dbdiff; /* # of hardware descriptors */ 1793284052Snp u_int n; 1794284052Snp struct wrqe *wr; 1795284052Snp struct fw_eth_tx_pkt_wr *dst; /* any fw WR struct will do */ 1796284052Snp 1797284052Snp EQ_LOCK_ASSERT_OWNED(eq); 1798284052Snp MPASS(TAILQ_EMPTY(&wrq->incomplete_wrs)); 1799284052Snp wr = STAILQ_FIRST(&wrq->wr_list); 1800284052Snp MPASS(wr != NULL); /* Must be called with something useful to do */ 1801284052Snp dbdiff = IDXDIFF(eq->pidx, eq->dbidx, eq->sidx); 1802284052Snp 1803284052Snp do { 1804284052Snp eq->cidx = read_hw_cidx(eq); 1805284052Snp if (eq->pidx == eq->cidx) 1806284052Snp available = eq->sidx - 1; 1807284052Snp else 1808284052Snp available = IDXDIFF(eq->cidx, eq->pidx, eq->sidx) - 1; 1809284052Snp 1810284052Snp MPASS(wr->wrq == wrq); 1811284052Snp n = howmany(wr->wr_len, EQ_ESIZE); 1812284052Snp if (available < n) 1813284052Snp return; 1814284052Snp 1815284052Snp dst = (void *)&eq->desc[eq->pidx]; 1816284052Snp if (__predict_true(eq->sidx - eq->pidx > n)) { 1817284052Snp /* Won't wrap, won't end exactly at the status page. */ 1818284052Snp bcopy(&wr->wr[0], dst, wr->wr_len); 1819284052Snp eq->pidx += n; 1820284052Snp } else { 1821284052Snp int first_portion = (eq->sidx - eq->pidx) * EQ_ESIZE; 1822284052Snp 1823284052Snp bcopy(&wr->wr[0], dst, first_portion); 1824284052Snp if (wr->wr_len > first_portion) { 1825284052Snp bcopy(&wr->wr[first_portion], &eq->desc[0], 1826284052Snp wr->wr_len - first_portion); 1827284052Snp } 1828284052Snp eq->pidx = n - (eq->sidx - eq->pidx); 1829284052Snp } 1830284052Snp 1831284052Snp if (available < eq->sidx / 4 && 1832284052Snp atomic_cmpset_int(&eq->equiq, 0, 1)) { 1833284052Snp dst->equiq_to_len16 |= htobe32(F_FW_WR_EQUIQ | 1834284052Snp F_FW_WR_EQUEQ); 1835284052Snp eq->equeqidx = eq->pidx; 1836284052Snp } else if (IDXDIFF(eq->pidx, eq->equeqidx, eq->sidx) >= 32) { 1837284052Snp dst->equiq_to_len16 |= htobe32(F_FW_WR_EQUEQ); 1838284052Snp eq->equeqidx = eq->pidx; 1839284052Snp } 1840284052Snp 1841284052Snp dbdiff += n; 1842284052Snp if (dbdiff >= 16) { 1843284052Snp ring_eq_db(sc, eq, dbdiff); 1844284052Snp dbdiff = 0; 1845284052Snp } 1846284052Snp 1847284052Snp STAILQ_REMOVE_HEAD(&wrq->wr_list, link); 1848284052Snp free_wrqe(wr); 1849284052Snp MPASS(wrq->nwr_pending > 0); 1850284052Snp wrq->nwr_pending--; 1851284052Snp MPASS(wrq->ndesc_needed >= n); 1852284052Snp wrq->ndesc_needed -= n; 1853284052Snp } while ((wr = STAILQ_FIRST(&wrq->wr_list)) != NULL); 1854284052Snp 1855284052Snp if (dbdiff) 1856284052Snp ring_eq_db(sc, eq, dbdiff); 1857284052Snp} 1858284052Snp 1859284052Snp/* 1860228561Snp * Doesn't fail. Holds on to work requests it can't send right away. 1861228561Snp */ 1862237263Snpvoid 1863237263Snpt4_wrq_tx_locked(struct adapter *sc, struct sge_wrq *wrq, struct wrqe *wr) 1864228561Snp{ 1865284052Snp#ifdef INVARIANTS 1866228561Snp struct sge_eq *eq = &wrq->eq; 1867284052Snp#endif 1868228561Snp 1869284052Snp EQ_LOCK_ASSERT_OWNED(eq); 1870284052Snp MPASS(wr != NULL); 1871284052Snp MPASS(wr->wr_len > 0 && wr->wr_len <= SGE_MAX_WR_LEN); 1872284052Snp MPASS((wr->wr_len & 0x7) == 0); 1873284052Snp 1874284052Snp STAILQ_INSERT_TAIL(&wrq->wr_list, wr, link); 1875284052Snp wrq->nwr_pending++; 1876284052Snp wrq->ndesc_needed += howmany(wr->wr_len, EQ_ESIZE); 1877284052Snp 1878284052Snp if (!TAILQ_EMPTY(&wrq->incomplete_wrs)) 1879284052Snp return; /* commit_wrq_wr will drain wr_list as well. */ 1880284052Snp 1881284052Snp drain_wrq_wr_list(sc, wrq); 1882284052Snp 1883284052Snp /* Doorbell must have caught up to the pidx. */ 1884284052Snp MPASS(eq->pidx == eq->dbidx); 1885284052Snp} 1886284052Snp 1887284052Snpvoid 1888284052Snpt4_update_fl_bufsize(struct ifnet *ifp) 1889284052Snp{ 1890284052Snp struct port_info *pi = ifp->if_softc; 1891284052Snp struct adapter *sc = pi->adapter; 1892284052Snp struct sge_rxq *rxq; 1893237263Snp#ifdef TCP_OFFLOAD 1894284052Snp struct sge_ofld_rxq *ofld_rxq; 1895237263Snp#endif 1896284052Snp struct sge_fl *fl; 1897284052Snp int i, maxp, mtu = ifp->if_mtu; 1898228561Snp 1899284052Snp maxp = mtu_to_max_payload(sc, mtu, 0); 1900284052Snp for_each_rxq(pi, i, rxq) { 1901284052Snp fl = &rxq->fl; 1902218792Snp 1903284052Snp FL_LOCK(fl); 1904284052Snp find_best_refill_source(sc, fl, maxp); 1905284052Snp FL_UNLOCK(fl); 1906218792Snp } 1907284052Snp#ifdef TCP_OFFLOAD 1908284052Snp maxp = mtu_to_max_payload(sc, mtu, 1); 1909284052Snp for_each_ofld_rxq(pi, i, ofld_rxq) { 1910284052Snp fl = &ofld_rxq->fl; 1911228561Snp 1912284052Snp FL_LOCK(fl); 1913284052Snp find_best_refill_source(sc, fl, maxp); 1914284052Snp FL_UNLOCK(fl); 1915284052Snp } 1916284052Snp#endif 1917284052Snp} 1918228561Snp 1919284052Snpstatic inline int 1920284052Snpmbuf_nsegs(struct mbuf *m) 1921284052Snp{ 1922228561Snp 1923284052Snp M_ASSERTPKTHDR(m); 1924284052Snp KASSERT(m->m_pkthdr.l5hlen > 0, 1925284052Snp ("%s: mbuf %p missing information on # of segments.", __func__, m)); 1926218792Snp 1927284052Snp return (m->m_pkthdr.l5hlen); 1928284052Snp} 1929218792Snp 1930284052Snpstatic inline void 1931284052Snpset_mbuf_nsegs(struct mbuf *m, uint8_t nsegs) 1932284052Snp{ 1933218792Snp 1934284052Snp M_ASSERTPKTHDR(m); 1935284052Snp m->m_pkthdr.l5hlen = nsegs; 1936284052Snp} 1937228561Snp 1938284052Snpstatic inline int 1939284052Snpmbuf_len16(struct mbuf *m) 1940284052Snp{ 1941284052Snp int n; 1942228561Snp 1943284052Snp M_ASSERTPKTHDR(m); 1944284052Snp n = m->m_pkthdr.PH_loc.eigth[0]; 1945284052Snp MPASS(n > 0 && n <= SGE_MAX_WR_LEN / 16); 1946228561Snp 1947284052Snp return (n); 1948284052Snp} 1949228561Snp 1950284052Snpstatic inline void 1951284052Snpset_mbuf_len16(struct mbuf *m, uint8_t len16) 1952284052Snp{ 1953228561Snp 1954284052Snp M_ASSERTPKTHDR(m); 1955284052Snp m->m_pkthdr.PH_loc.eigth[0] = len16; 1956220873Snp} 1957220873Snp 1958284052Snpstatic inline int 1959284052Snpneeds_tso(struct mbuf *m) 1960284052Snp{ 1961218792Snp 1962284052Snp M_ASSERTPKTHDR(m); 1963218792Snp 1964284052Snp if (m->m_pkthdr.csum_flags & CSUM_TSO) { 1965284052Snp KASSERT(m->m_pkthdr.tso_segsz > 0, 1966284052Snp ("%s: TSO requested in mbuf %p but MSS not provided", 1967284052Snp __func__, m)); 1968284052Snp return (1); 1969284052Snp } 1970218792Snp 1971284052Snp return (0); 1972284052Snp} 1973218792Snp 1974284052Snpstatic inline int 1975284052Snpneeds_l3_csum(struct mbuf *m) 1976218792Snp{ 1977218792Snp 1978284052Snp M_ASSERTPKTHDR(m); 1979218792Snp 1980284052Snp if (m->m_pkthdr.csum_flags & (CSUM_IP | CSUM_TSO)) 1981284052Snp return (1); 1982284052Snp return (0); 1983284052Snp} 1984219292Snp 1985284052Snpstatic inline int 1986284052Snpneeds_l4_csum(struct mbuf *m) 1987284052Snp{ 1988218792Snp 1989284052Snp M_ASSERTPKTHDR(m); 1990218792Snp 1991284052Snp if (m->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP | CSUM_UDP_IPV6 | 1992284052Snp CSUM_TCP_IPV6 | CSUM_TSO)) 1993284052Snp return (1); 1994284052Snp return (0); 1995284052Snp} 1996284052Snp 1997284052Snpstatic inline int 1998284052Snpneeds_vlan_insertion(struct mbuf *m) 1999284052Snp{ 2000284052Snp 2001284052Snp M_ASSERTPKTHDR(m); 2002284052Snp 2003284052Snp if (m->m_flags & M_VLANTAG) { 2004284052Snp KASSERT(m->m_pkthdr.ether_vtag != 0, 2005284052Snp ("%s: HWVLAN requested in mbuf %p but tag not provided", 2006284052Snp __func__, m)); 2007284052Snp return (1); 2008228561Snp } 2009284052Snp return (0); 2010284052Snp} 2011228561Snp 2012284052Snpstatic void * 2013284052Snpm_advance(struct mbuf **pm, int *poffset, int len) 2014284052Snp{ 2015284052Snp struct mbuf *m = *pm; 2016284052Snp int offset = *poffset; 2017284052Snp uintptr_t p = 0; 2018228561Snp 2019284052Snp MPASS(len > 0); 2020218792Snp 2021284052Snp while (len) { 2022284052Snp if (offset + len < m->m_len) { 2023284052Snp offset += len; 2024284052Snp p = mtod(m, uintptr_t) + offset; 2025218792Snp break; 2026284052Snp } 2027284052Snp len -= m->m_len - offset; 2028284052Snp m = m->m_next; 2029284052Snp offset = 0; 2030284052Snp MPASS(m != NULL); 2031284052Snp } 2032284052Snp *poffset = offset; 2033284052Snp *pm = m; 2034284052Snp return ((void *)p); 2035284052Snp} 2036218792Snp 2037284052Snpstatic inline int 2038284052Snpsame_paddr(char *a, char *b) 2039284052Snp{ 2040218792Snp 2041284052Snp if (a == b) 2042284052Snp return (1); 2043284052Snp else if (a != NULL && b != NULL) { 2044284052Snp vm_offset_t x = (vm_offset_t)a; 2045284052Snp vm_offset_t y = (vm_offset_t)b; 2046218792Snp 2047284052Snp if ((x & PAGE_MASK) == (y & PAGE_MASK) && 2048284052Snp pmap_kextract(x) == pmap_kextract(y)) 2049284052Snp return (1); 2050284052Snp } 2051218792Snp 2052284052Snp return (0); 2053284052Snp} 2054218792Snp 2055284052Snp/* 2056284052Snp * Can deal with empty mbufs in the chain that have m_len = 0, but the chain 2057284052Snp * must have at least one mbuf that's not empty. 2058284052Snp */ 2059284052Snpstatic inline int 2060284052Snpcount_mbuf_nsegs(struct mbuf *m) 2061284052Snp{ 2062284052Snp char *prev_end, *start; 2063284052Snp int len, nsegs; 2064218792Snp 2065284052Snp MPASS(m != NULL); 2066218792Snp 2067284052Snp nsegs = 0; 2068284052Snp prev_end = NULL; 2069284052Snp for (; m; m = m->m_next) { 2070284052Snp 2071284052Snp len = m->m_len; 2072284052Snp if (__predict_false(len == 0)) 2073218792Snp continue; 2074284052Snp start = mtod(m, char *); 2075218792Snp 2076284052Snp nsegs += sglist_count(start, len); 2077284052Snp if (same_paddr(prev_end, start)) 2078284052Snp nsegs--; 2079284052Snp prev_end = start + len; 2080284052Snp } 2081218792Snp 2082284052Snp MPASS(nsegs > 0); 2083284052Snp return (nsegs); 2084284052Snp} 2085218792Snp 2086284052Snp/* 2087284052Snp * Analyze the mbuf to determine its tx needs. The mbuf passed in may change: 2088284052Snp * a) caller can assume it's been freed if this function returns with an error. 2089284052Snp * b) it may get defragged up if the gather list is too long for the hardware. 2090284052Snp */ 2091284052Snpint 2092284052Snpparse_pkt(struct mbuf **mp) 2093284052Snp{ 2094284052Snp struct mbuf *m0 = *mp, *m; 2095284052Snp int rc, nsegs, defragged = 0, offset; 2096284052Snp struct ether_header *eh; 2097284052Snp void *l3hdr; 2098284052Snp#if defined(INET) || defined(INET6) 2099284052Snp struct tcphdr *tcp; 2100284052Snp#endif 2101284052Snp uint16_t eh_type; 2102284052Snp 2103284052Snp M_ASSERTPKTHDR(m0); 2104284052Snp if (__predict_false(m0->m_pkthdr.len < ETHER_HDR_LEN)) { 2105284052Snp rc = EINVAL; 2106284052Snpfail: 2107284052Snp m_freem(m0); 2108284052Snp *mp = NULL; 2109284052Snp return (rc); 2110284052Snp } 2111284052Snprestart: 2112284052Snp /* 2113284052Snp * First count the number of gather list segments in the payload. 2114284052Snp * Defrag the mbuf if nsegs exceeds the hardware limit. 2115284052Snp */ 2116284052Snp M_ASSERTPKTHDR(m0); 2117284052Snp MPASS(m0->m_pkthdr.len > 0); 2118284052Snp nsegs = count_mbuf_nsegs(m0); 2119284052Snp if (nsegs > (needs_tso(m0) ? TX_SGL_SEGS_TSO : TX_SGL_SEGS)) { 2120284052Snp if (defragged++ > 0 || (m = m_defrag(m0, M_NOWAIT)) == NULL) { 2121284052Snp rc = EFBIG; 2122284052Snp goto fail; 2123218792Snp } 2124284052Snp *mp = m0 = m; /* update caller's copy after defrag */ 2125284052Snp goto restart; 2126284052Snp } 2127218792Snp 2128284052Snp if (__predict_false(nsegs > 2 && m0->m_pkthdr.len <= MHLEN)) { 2129284052Snp m0 = m_pullup(m0, m0->m_pkthdr.len); 2130284052Snp if (m0 == NULL) { 2131284052Snp /* Should have left well enough alone. */ 2132284052Snp rc = EFBIG; 2133284052Snp goto fail; 2134284052Snp } 2135284052Snp *mp = m0; /* update caller's copy after pullup */ 2136284052Snp goto restart; 2137284052Snp } 2138284052Snp set_mbuf_nsegs(m0, nsegs); 2139284052Snp set_mbuf_len16(m0, txpkt_len16(nsegs, needs_tso(m0))); 2140218792Snp 2141284052Snp if (!needs_tso(m0)) 2142284052Snp return (0); 2143218792Snp 2144284052Snp m = m0; 2145284052Snp eh = mtod(m, struct ether_header *); 2146284052Snp eh_type = ntohs(eh->ether_type); 2147284052Snp if (eh_type == ETHERTYPE_VLAN) { 2148284052Snp struct ether_vlan_header *evh = (void *)eh; 2149218792Snp 2150284052Snp eh_type = ntohs(evh->evl_proto); 2151284052Snp m0->m_pkthdr.l2hlen = sizeof(*evh); 2152284052Snp } else 2153284052Snp m0->m_pkthdr.l2hlen = sizeof(*eh); 2154218792Snp 2155284052Snp offset = 0; 2156284052Snp l3hdr = m_advance(&m, &offset, m0->m_pkthdr.l2hlen); 2157218792Snp 2158284052Snp switch (eh_type) { 2159284052Snp#ifdef INET6 2160284052Snp case ETHERTYPE_IPV6: 2161284052Snp { 2162284052Snp struct ip6_hdr *ip6 = l3hdr; 2163218792Snp 2164284052Snp MPASS(ip6->ip6_nxt == IPPROTO_TCP); 2165219292Snp 2166284052Snp m0->m_pkthdr.l3hlen = sizeof(*ip6); 2167284052Snp break; 2168218792Snp } 2169284052Snp#endif 2170284052Snp#ifdef INET 2171284052Snp case ETHERTYPE_IP: 2172284052Snp { 2173284052Snp struct ip *ip = l3hdr; 2174218792Snp 2175284052Snp m0->m_pkthdr.l3hlen = ip->ip_hl * 4; 2176284052Snp break; 2177284052Snp } 2178284052Snp#endif 2179284052Snp default: 2180284052Snp panic("%s: ethertype 0x%04x unknown. if_cxgbe must be compiled" 2181284052Snp " with the same INET/INET6 options as the kernel.", 2182284052Snp __func__, eh_type); 2183284052Snp } 2184218792Snp 2185284052Snp#if defined(INET) || defined(INET6) 2186284052Snp tcp = m_advance(&m, &offset, m0->m_pkthdr.l3hlen); 2187284052Snp m0->m_pkthdr.l4hlen = tcp->th_off * 4; 2188284052Snp#endif 2189284052Snp MPASS(m0 == *mp); 2190284052Snp return (0); 2191284052Snp} 2192220873Snp 2193284052Snpvoid * 2194284052Snpstart_wrq_wr(struct sge_wrq *wrq, int len16, struct wrq_cookie *cookie) 2195284052Snp{ 2196284052Snp struct sge_eq *eq = &wrq->eq; 2197284052Snp struct adapter *sc = wrq->adapter; 2198284052Snp int ndesc, available; 2199284052Snp struct wrqe *wr; 2200284052Snp void *w; 2201228561Snp 2202284052Snp MPASS(len16 > 0); 2203284052Snp ndesc = howmany(len16, EQ_ESIZE / 16); 2204284052Snp MPASS(ndesc > 0 && ndesc <= SGE_MAX_WR_NDESC); 2205284052Snp 2206284052Snp EQ_LOCK(eq); 2207284052Snp 2208284052Snp if (!STAILQ_EMPTY(&wrq->wr_list)) 2209284052Snp drain_wrq_wr_list(sc, wrq); 2210284052Snp 2211284052Snp if (!STAILQ_EMPTY(&wrq->wr_list)) { 2212284052Snpslowpath: 2213284052Snp EQ_UNLOCK(eq); 2214284052Snp wr = alloc_wrqe(len16 * 16, wrq); 2215284052Snp if (__predict_false(wr == NULL)) 2216284052Snp return (NULL); 2217284052Snp cookie->pidx = -1; 2218284052Snp cookie->ndesc = ndesc; 2219284052Snp return (&wr->wr); 2220220873Snp } 2221218792Snp 2222284052Snp eq->cidx = read_hw_cidx(eq); 2223284052Snp if (eq->pidx == eq->cidx) 2224284052Snp available = eq->sidx - 1; 2225284052Snp else 2226284052Snp available = IDXDIFF(eq->cidx, eq->pidx, eq->sidx) - 1; 2227284052Snp if (available < ndesc) 2228284052Snp goto slowpath; 2229218792Snp 2230284052Snp cookie->pidx = eq->pidx; 2231284052Snp cookie->ndesc = ndesc; 2232284052Snp TAILQ_INSERT_TAIL(&wrq->incomplete_wrs, cookie, link); 2233218792Snp 2234284052Snp w = &eq->desc[eq->pidx]; 2235284052Snp IDXINCR(eq->pidx, ndesc, eq->sidx); 2236284052Snp if (__predict_false(eq->pidx < ndesc - 1)) { 2237284052Snp w = &wrq->ss[0]; 2238284052Snp wrq->ss_pidx = cookie->pidx; 2239284052Snp wrq->ss_len = len16 * 16; 2240284052Snp } 2241228561Snp 2242284052Snp EQ_UNLOCK(eq); 2243284052Snp 2244284052Snp return (w); 2245218792Snp} 2246218792Snp 2247218792Snpvoid 2248284052Snpcommit_wrq_wr(struct sge_wrq *wrq, void *w, struct wrq_cookie *cookie) 2249218792Snp{ 2250284052Snp struct sge_eq *eq = &wrq->eq; 2251284052Snp struct adapter *sc = wrq->adapter; 2252284052Snp int ndesc, pidx; 2253284052Snp struct wrq_cookie *prev, *next; 2254218792Snp 2255284052Snp if (cookie->pidx == -1) { 2256284052Snp struct wrqe *wr = __containerof(w, struct wrqe, wr); 2257218792Snp 2258284052Snp t4_wrq_tx(sc, wr); 2259284052Snp return; 2260218792Snp } 2261252728Snp 2262284052Snp ndesc = cookie->ndesc; /* Can be more than SGE_MAX_WR_NDESC here. */ 2263284052Snp pidx = cookie->pidx; 2264284052Snp MPASS(pidx >= 0 && pidx < eq->sidx); 2265284052Snp if (__predict_false(w == &wrq->ss[0])) { 2266284052Snp int n = (eq->sidx - wrq->ss_pidx) * EQ_ESIZE; 2267284052Snp 2268284052Snp MPASS(wrq->ss_len > n); /* WR had better wrap around. */ 2269284052Snp bcopy(&wrq->ss[0], &eq->desc[wrq->ss_pidx], n); 2270284052Snp bcopy(&wrq->ss[n], &eq->desc[0], wrq->ss_len - n); 2271284052Snp wrq->tx_wrs_ss++; 2272284052Snp } else 2273284052Snp wrq->tx_wrs_direct++; 2274284052Snp 2275284052Snp EQ_LOCK(eq); 2276284052Snp prev = TAILQ_PREV(cookie, wrq_incomplete_wrs, link); 2277284052Snp next = TAILQ_NEXT(cookie, link); 2278284052Snp if (prev == NULL) { 2279284052Snp MPASS(pidx == eq->dbidx); 2280284052Snp if (next == NULL || ndesc >= 16) 2281284052Snp ring_eq_db(wrq->adapter, eq, ndesc); 2282284052Snp else { 2283284052Snp MPASS(IDXDIFF(next->pidx, pidx, eq->sidx) == ndesc); 2284284052Snp next->pidx = pidx; 2285284052Snp next->ndesc += ndesc; 2286284052Snp } 2287284052Snp } else { 2288284052Snp MPASS(IDXDIFF(pidx, prev->pidx, eq->sidx) == prev->ndesc); 2289284052Snp prev->ndesc += ndesc; 2290252728Snp } 2291284052Snp TAILQ_REMOVE(&wrq->incomplete_wrs, cookie, link); 2292284052Snp 2293284052Snp if (TAILQ_EMPTY(&wrq->incomplete_wrs) && !STAILQ_EMPTY(&wrq->wr_list)) 2294284052Snp drain_wrq_wr_list(sc, wrq); 2295284052Snp 2296284052Snp#ifdef INVARIANTS 2297284052Snp if (TAILQ_EMPTY(&wrq->incomplete_wrs)) { 2298284052Snp /* Doorbell must have caught up to the pidx. */ 2299284052Snp MPASS(wrq->eq.pidx == wrq->eq.dbidx); 2300284052Snp } 2301252728Snp#endif 2302284052Snp EQ_UNLOCK(eq); 2303218792Snp} 2304218792Snp 2305284052Snpstatic u_int 2306284052Snpcan_resume_eth_tx(struct mp_ring *r) 2307228561Snp{ 2308284052Snp struct sge_eq *eq = r->cookie; 2309267764Snp 2310284052Snp return (total_available_tx_desc(eq) > eq->sidx / 8); 2311228561Snp} 2312228561Snp 2313284052Snpstatic inline int 2314284052Snpcannot_use_txpkts(struct mbuf *m) 2315284052Snp{ 2316284052Snp /* maybe put a GL limit too, to avoid silliness? */ 2317284052Snp 2318284052Snp return (needs_tso(m)); 2319284052Snp} 2320284052Snp 2321284052Snp/* 2322284052Snp * r->items[cidx] to r->items[pidx], with a wraparound at r->size, are ready to 2323284052Snp * be consumed. Return the actual number consumed. 0 indicates a stall. 2324284052Snp */ 2325284052Snpstatic u_int 2326284052Snpeth_tx(struct mp_ring *r, u_int cidx, u_int pidx) 2327284052Snp{ 2328284052Snp struct sge_txq *txq = r->cookie; 2329284052Snp struct sge_eq *eq = &txq->eq; 2330284052Snp struct ifnet *ifp = txq->ifp; 2331284052Snp struct port_info *pi = (void *)ifp->if_softc; 2332284052Snp struct adapter *sc = pi->adapter; 2333284052Snp u_int total, remaining; /* # of packets */ 2334284052Snp u_int available, dbdiff; /* # of hardware descriptors */ 2335284052Snp u_int n, next_cidx; 2336284052Snp struct mbuf *m0, *tail; 2337284052Snp struct txpkts txp; 2338284052Snp struct fw_eth_tx_pkts_wr *wr; /* any fw WR struct will do */ 2339284052Snp 2340284052Snp remaining = IDXDIFF(pidx, cidx, r->size); 2341284052Snp MPASS(remaining > 0); /* Must not be called without work to do. */ 2342284052Snp total = 0; 2343284052Snp 2344284052Snp TXQ_LOCK(txq); 2345284052Snp if (__predict_false((eq->flags & EQ_ENABLED) == 0)) { 2346284052Snp while (cidx != pidx) { 2347284052Snp m0 = r->items[cidx]; 2348284052Snp m_freem(m0); 2349284052Snp if (++cidx == r->size) 2350284052Snp cidx = 0; 2351284052Snp } 2352284052Snp reclaim_tx_descs(txq, 2048); 2353284052Snp total = remaining; 2354284052Snp goto done; 2355284052Snp } 2356284052Snp 2357284052Snp /* How many hardware descriptors do we have readily available. */ 2358284052Snp if (eq->pidx == eq->cidx) 2359284052Snp available = eq->sidx - 1; 2360284052Snp else 2361284052Snp available = IDXDIFF(eq->cidx, eq->pidx, eq->sidx) - 1; 2362284052Snp dbdiff = IDXDIFF(eq->pidx, eq->dbidx, eq->sidx); 2363284052Snp 2364284052Snp while (remaining > 0) { 2365284052Snp 2366284052Snp m0 = r->items[cidx]; 2367284052Snp M_ASSERTPKTHDR(m0); 2368284052Snp MPASS(m0->m_nextpkt == NULL); 2369284052Snp 2370284052Snp if (available < SGE_MAX_WR_NDESC) { 2371284052Snp available += reclaim_tx_descs(txq, 64); 2372284052Snp if (available < howmany(mbuf_len16(m0), EQ_ESIZE / 16)) 2373284052Snp break; /* out of descriptors */ 2374284052Snp } 2375284052Snp 2376284052Snp next_cidx = cidx + 1; 2377284052Snp if (__predict_false(next_cidx == r->size)) 2378284052Snp next_cidx = 0; 2379284052Snp 2380284052Snp wr = (void *)&eq->desc[eq->pidx]; 2381284052Snp if (remaining > 1 && 2382284052Snp try_txpkts(m0, r->items[next_cidx], &txp, available) == 0) { 2383284052Snp 2384284052Snp /* pkts at cidx, next_cidx should both be in txp. */ 2385284052Snp MPASS(txp.npkt == 2); 2386284052Snp tail = r->items[next_cidx]; 2387284052Snp MPASS(tail->m_nextpkt == NULL); 2388284052Snp ETHER_BPF_MTAP(ifp, m0); 2389284052Snp ETHER_BPF_MTAP(ifp, tail); 2390284052Snp m0->m_nextpkt = tail; 2391284052Snp 2392284052Snp if (__predict_false(++next_cidx == r->size)) 2393284052Snp next_cidx = 0; 2394284052Snp 2395284052Snp while (next_cidx != pidx) { 2396284052Snp if (add_to_txpkts(r->items[next_cidx], &txp, 2397284052Snp available) != 0) 2398284052Snp break; 2399284052Snp tail->m_nextpkt = r->items[next_cidx]; 2400284052Snp tail = tail->m_nextpkt; 2401284052Snp ETHER_BPF_MTAP(ifp, tail); 2402284052Snp if (__predict_false(++next_cidx == r->size)) 2403284052Snp next_cidx = 0; 2404284052Snp } 2405284052Snp 2406284052Snp n = write_txpkts_wr(txq, wr, m0, &txp, available); 2407284052Snp total += txp.npkt; 2408284052Snp remaining -= txp.npkt; 2409284052Snp } else { 2410284052Snp total++; 2411284052Snp remaining--; 2412284052Snp n = write_txpkt_wr(txq, (void *)wr, m0, available); 2413284052Snp ETHER_BPF_MTAP(ifp, m0); 2414284052Snp } 2415284052Snp MPASS(n >= 1 && n <= available && n <= SGE_MAX_WR_NDESC); 2416284052Snp 2417284052Snp available -= n; 2418284052Snp dbdiff += n; 2419284052Snp IDXINCR(eq->pidx, n, eq->sidx); 2420284052Snp 2421284052Snp if (total_available_tx_desc(eq) < eq->sidx / 4 && 2422284052Snp atomic_cmpset_int(&eq->equiq, 0, 1)) { 2423284052Snp wr->equiq_to_len16 |= htobe32(F_FW_WR_EQUIQ | 2424284052Snp F_FW_WR_EQUEQ); 2425284052Snp eq->equeqidx = eq->pidx; 2426284052Snp } else if (IDXDIFF(eq->pidx, eq->equeqidx, eq->sidx) >= 32) { 2427284052Snp wr->equiq_to_len16 |= htobe32(F_FW_WR_EQUEQ); 2428284052Snp eq->equeqidx = eq->pidx; 2429284052Snp } 2430284052Snp 2431284052Snp if (dbdiff >= 16 && remaining >= 4) { 2432284052Snp ring_eq_db(sc, eq, dbdiff); 2433284052Snp available += reclaim_tx_descs(txq, 4 * dbdiff); 2434284052Snp dbdiff = 0; 2435284052Snp } 2436284052Snp 2437284052Snp cidx = next_cidx; 2438284052Snp } 2439284052Snp if (dbdiff != 0) { 2440284052Snp ring_eq_db(sc, eq, dbdiff); 2441284052Snp reclaim_tx_descs(txq, 32); 2442284052Snp } 2443284052Snpdone: 2444284052Snp TXQ_UNLOCK(txq); 2445284052Snp 2446284052Snp return (total); 2447284052Snp} 2448284052Snp 2449218792Snpstatic inline void 2450218792Snpinit_iq(struct sge_iq *iq, struct adapter *sc, int tmr_idx, int pktc_idx, 2451270297Snp int qsize) 2452218792Snp{ 2453270297Snp 2454218792Snp KASSERT(tmr_idx >= 0 && tmr_idx < SGE_NTIMERS, 2455218792Snp ("%s: bad tmr_idx %d", __func__, tmr_idx)); 2456218792Snp KASSERT(pktc_idx < SGE_NCOUNTERS, /* -ve is ok, means don't use */ 2457218792Snp ("%s: bad pktc_idx %d", __func__, pktc_idx)); 2458218792Snp 2459218792Snp iq->flags = 0; 2460218792Snp iq->adapter = sc; 2461234833Snp iq->intr_params = V_QINTR_TIMER_IDX(tmr_idx); 2462234833Snp iq->intr_pktc_idx = SGE_NCOUNTERS - 1; 2463234833Snp if (pktc_idx >= 0) { 2464234833Snp iq->intr_params |= F_QINTR_CNT_EN; 2465234833Snp iq->intr_pktc_idx = pktc_idx; 2466234833Snp } 2467248925Snp iq->qsize = roundup2(qsize, 16); /* See FW_IQ_CMD/iqsize */ 2468270297Snp iq->sidx = iq->qsize - spg_len / IQ_ESIZE; 2469218792Snp} 2470218792Snp 2471218792Snpstatic inline void 2472281212Snpinit_fl(struct adapter *sc, struct sge_fl *fl, int qsize, int maxp, char *name) 2473218792Snp{ 2474255050Snp 2475218792Snp fl->qsize = qsize; 2476270297Snp fl->sidx = qsize - spg_len / EQ_ESIZE; 2477218792Snp strlcpy(fl->lockname, name, sizeof(fl->lockname)); 2478281212Snp if (sc->flags & BUF_PACKING_OK && 2479281212Snp ((!is_t4(sc) && buffer_packing) || /* T5+: enabled unless 0 */ 2480281212Snp (is_t4(sc) && buffer_packing == 1)))/* T4: disabled unless 1 */ 2481255050Snp fl->flags |= FL_BUF_PACKING; 2482265425Snp find_best_refill_source(sc, fl, maxp); 2483265425Snp find_safe_refill_source(sc, fl); 2484218792Snp} 2485218792Snp 2486218792Snpstatic inline void 2487228561Snpinit_eq(struct sge_eq *eq, int eqtype, int qsize, uint8_t tx_chan, 2488228561Snp uint16_t iqid, char *name) 2489218792Snp{ 2490228561Snp KASSERT(tx_chan < NCHAN, ("%s: bad tx channel %d", __func__, tx_chan)); 2491228561Snp KASSERT(eqtype <= EQ_TYPEMASK, ("%s: bad qtype %d", __func__, eqtype)); 2492228561Snp 2493228561Snp eq->flags = eqtype & EQ_TYPEMASK; 2494228561Snp eq->tx_chan = tx_chan; 2495228561Snp eq->iqid = iqid; 2496284052Snp eq->sidx = qsize - spg_len / EQ_ESIZE; 2497220873Snp strlcpy(eq->lockname, name, sizeof(eq->lockname)); 2498218792Snp} 2499218792Snp 2500218792Snpstatic int 2501218792Snpalloc_ring(struct adapter *sc, size_t len, bus_dma_tag_t *tag, 2502218792Snp bus_dmamap_t *map, bus_addr_t *pa, void **va) 2503218792Snp{ 2504218792Snp int rc; 2505218792Snp 2506218792Snp rc = bus_dma_tag_create(sc->dmat, 512, 0, BUS_SPACE_MAXADDR, 2507218792Snp BUS_SPACE_MAXADDR, NULL, NULL, len, 1, len, 0, NULL, NULL, tag); 2508218792Snp if (rc != 0) { 2509218792Snp device_printf(sc->dev, "cannot allocate DMA tag: %d\n", rc); 2510218792Snp goto done; 2511218792Snp } 2512218792Snp 2513218792Snp rc = bus_dmamem_alloc(*tag, va, 2514218792Snp BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO, map); 2515218792Snp if (rc != 0) { 2516218792Snp device_printf(sc->dev, "cannot allocate DMA memory: %d\n", rc); 2517218792Snp goto done; 2518218792Snp } 2519218792Snp 2520218792Snp rc = bus_dmamap_load(*tag, *map, *va, len, oneseg_dma_callback, pa, 0); 2521218792Snp if (rc != 0) { 2522218792Snp device_printf(sc->dev, "cannot load DMA map: %d\n", rc); 2523218792Snp goto done; 2524218792Snp } 2525218792Snpdone: 2526218792Snp if (rc) 2527218792Snp free_ring(sc, *tag, *map, *pa, *va); 2528218792Snp 2529218792Snp return (rc); 2530218792Snp} 2531218792Snp 2532218792Snpstatic int 2533218792Snpfree_ring(struct adapter *sc, bus_dma_tag_t tag, bus_dmamap_t map, 2534218792Snp bus_addr_t pa, void *va) 2535218792Snp{ 2536218792Snp if (pa) 2537218792Snp bus_dmamap_unload(tag, map); 2538218792Snp if (va) 2539218792Snp bus_dmamem_free(tag, va, map); 2540218792Snp if (tag) 2541218792Snp bus_dma_tag_destroy(tag); 2542218792Snp 2543218792Snp return (0); 2544218792Snp} 2545218792Snp 2546218792Snp/* 2547218792Snp * Allocates the ring for an ingress queue and an optional freelist. If the 2548218792Snp * freelist is specified it will be allocated and then associated with the 2549218792Snp * ingress queue. 2550218792Snp * 2551218792Snp * Returns errno on failure. Resources allocated up to that point may still be 2552218792Snp * allocated. Caller is responsible for cleanup in case this function fails. 2553218792Snp * 2554228561Snp * If the ingress queue will take interrupts directly (iq->flags & IQ_INTR) then 2555218792Snp * the intr_idx specifies the vector, starting from 0. Otherwise it specifies 2556228561Snp * the abs_id of the ingress queue to which its interrupts should be forwarded. 2557218792Snp */ 2558218792Snpstatic int 2559218792Snpalloc_iq_fl(struct port_info *pi, struct sge_iq *iq, struct sge_fl *fl, 2560222085Snp int intr_idx, int cong) 2561218792Snp{ 2562218792Snp int rc, i, cntxt_id; 2563218792Snp size_t len; 2564218792Snp struct fw_iq_cmd c; 2565218792Snp struct adapter *sc = iq->adapter; 2566218792Snp __be32 v = 0; 2567218792Snp 2568270297Snp len = iq->qsize * IQ_ESIZE; 2569218792Snp rc = alloc_ring(sc, len, &iq->desc_tag, &iq->desc_map, &iq->ba, 2570218792Snp (void **)&iq->desc); 2571218792Snp if (rc != 0) 2572218792Snp return (rc); 2573218792Snp 2574218792Snp bzero(&c, sizeof(c)); 2575218792Snp c.op_to_vfn = htobe32(V_FW_CMD_OP(FW_IQ_CMD) | F_FW_CMD_REQUEST | 2576218792Snp F_FW_CMD_WRITE | F_FW_CMD_EXEC | V_FW_IQ_CMD_PFN(sc->pf) | 2577218792Snp V_FW_IQ_CMD_VFN(0)); 2578218792Snp 2579218792Snp c.alloc_to_len16 = htobe32(F_FW_IQ_CMD_ALLOC | F_FW_IQ_CMD_IQSTART | 2580218792Snp FW_LEN16(c)); 2581218792Snp 2582218792Snp /* Special handling for firmware event queue */ 2583218792Snp if (iq == &sc->sge.fwq) 2584218792Snp v |= F_FW_IQ_CMD_IQASYNCH; 2585218792Snp 2586228561Snp if (iq->flags & IQ_INTR) { 2587218792Snp KASSERT(intr_idx < sc->intr_count, 2588218792Snp ("%s: invalid direct intr_idx %d", __func__, intr_idx)); 2589228561Snp } else 2590228561Snp v |= F_FW_IQ_CMD_IQANDST; 2591228561Snp v |= V_FW_IQ_CMD_IQANDSTINDEX(intr_idx); 2592218792Snp 2593218792Snp c.type_to_iqandstindex = htobe32(v | 2594218792Snp V_FW_IQ_CMD_TYPE(FW_IQ_TYPE_FL_INT_CAP) | 2595218792Snp V_FW_IQ_CMD_VIID(pi->viid) | 2596218792Snp V_FW_IQ_CMD_IQANUD(X_UPDATEDELIVERY_INTERRUPT)); 2597218792Snp c.iqdroprss_to_iqesize = htobe16(V_FW_IQ_CMD_IQPCIECH(pi->tx_chan) | 2598218792Snp F_FW_IQ_CMD_IQGTSMODE | 2599218792Snp V_FW_IQ_CMD_IQINTCNTTHRESH(iq->intr_pktc_idx) | 2600270297Snp V_FW_IQ_CMD_IQESIZE(ilog2(IQ_ESIZE) - 4)); 2601218792Snp c.iqsize = htobe16(iq->qsize); 2602218792Snp c.iqaddr = htobe64(iq->ba); 2603222085Snp if (cong >= 0) 2604222085Snp c.iqns_to_fl0congen = htobe32(F_FW_IQ_CMD_IQFLINTCONGEN); 2605218792Snp 2606218792Snp if (fl) { 2607218792Snp mtx_init(&fl->fl_lock, fl->lockname, NULL, MTX_DEF); 2608218792Snp 2609270297Snp len = fl->qsize * EQ_ESIZE; 2610218792Snp rc = alloc_ring(sc, len, &fl->desc_tag, &fl->desc_map, 2611218792Snp &fl->ba, (void **)&fl->desc); 2612218792Snp if (rc) 2613218792Snp return (rc); 2614218792Snp 2615218792Snp /* Allocate space for one software descriptor per buffer. */ 2616218792Snp rc = alloc_fl_sdesc(fl); 2617218792Snp if (rc != 0) { 2618218792Snp device_printf(sc->dev, 2619218792Snp "failed to setup fl software descriptors: %d\n", 2620218792Snp rc); 2621218792Snp return (rc); 2622218792Snp } 2623218792Snp 2624270297Snp if (fl->flags & FL_BUF_PACKING) { 2625270297Snp fl->lowat = roundup2(sc->sge.fl_starve_threshold2, 8); 2626281212Snp fl->buf_boundary = sc->sge.pack_boundary; 2627270297Snp } else { 2628270297Snp fl->lowat = roundup2(sc->sge.fl_starve_threshold, 8); 2629281212Snp fl->buf_boundary = 16; 2630270297Snp } 2631281212Snp if (fl_pad && fl->buf_boundary < sc->sge.pad_boundary) 2632281212Snp fl->buf_boundary = sc->sge.pad_boundary; 2633270297Snp 2634228491Snp c.iqns_to_fl0congen |= 2635222085Snp htobe32(V_FW_IQ_CMD_FL0HOSTFCMODE(X_HOSTFCMODE_NONE) | 2636222085Snp F_FW_IQ_CMD_FL0FETCHRO | F_FW_IQ_CMD_FL0DATARO | 2637255050Snp (fl_pad ? F_FW_IQ_CMD_FL0PADEN : 0) | 2638255050Snp (fl->flags & FL_BUF_PACKING ? F_FW_IQ_CMD_FL0PACKEN : 2639255050Snp 0)); 2640222085Snp if (cong >= 0) { 2641222085Snp c.iqns_to_fl0congen |= 2642222085Snp htobe32(V_FW_IQ_CMD_FL0CNGCHMAP(cong) | 2643222085Snp F_FW_IQ_CMD_FL0CONGCIF | 2644222085Snp F_FW_IQ_CMD_FL0CONGEN); 2645222085Snp } 2646218792Snp c.fl0dcaen_to_fl0cidxfthresh = 2647284093Snp htobe16(V_FW_IQ_CMD_FL0FBMIN(X_FETCHBURSTMIN_128B) | 2648218792Snp V_FW_IQ_CMD_FL0FBMAX(X_FETCHBURSTMAX_512B)); 2649218792Snp c.fl0size = htobe16(fl->qsize); 2650218792Snp c.fl0addr = htobe64(fl->ba); 2651218792Snp } 2652218792Snp 2653218792Snp rc = -t4_wr_mbox(sc, sc->mbox, &c, sizeof(c), &c); 2654218792Snp if (rc != 0) { 2655218792Snp device_printf(sc->dev, 2656218792Snp "failed to create ingress queue: %d\n", rc); 2657218792Snp return (rc); 2658218792Snp } 2659218792Snp 2660218792Snp iq->cidx = 0; 2661270297Snp iq->gen = F_RSPD_GEN; 2662218792Snp iq->intr_next = iq->intr_params; 2663218792Snp iq->cntxt_id = be16toh(c.iqid); 2664218792Snp iq->abs_id = be16toh(c.physiqid); 2665228561Snp iq->flags |= IQ_ALLOCATED; 2666218792Snp 2667218792Snp cntxt_id = iq->cntxt_id - sc->sge.iq_start; 2668228561Snp if (cntxt_id >= sc->sge.niq) { 2669228561Snp panic ("%s: iq->cntxt_id (%d) more than the max (%d)", __func__, 2670228561Snp cntxt_id, sc->sge.niq - 1); 2671228561Snp } 2672218792Snp sc->sge.iqmap[cntxt_id] = iq; 2673218792Snp 2674218792Snp if (fl) { 2675270297Snp u_int qid; 2676270297Snp 2677270297Snp iq->flags |= IQ_HAS_FL; 2678218792Snp fl->cntxt_id = be16toh(c.fl0id); 2679218792Snp fl->pidx = fl->cidx = 0; 2680218792Snp 2681219883Snp cntxt_id = fl->cntxt_id - sc->sge.eq_start; 2682228561Snp if (cntxt_id >= sc->sge.neq) { 2683228561Snp panic("%s: fl->cntxt_id (%d) more than the max (%d)", 2684228561Snp __func__, cntxt_id, sc->sge.neq - 1); 2685228561Snp } 2686218792Snp sc->sge.eqmap[cntxt_id] = (void *)fl; 2687218792Snp 2688270297Snp qid = fl->cntxt_id; 2689270297Snp if (isset(&sc->doorbells, DOORBELL_UDB)) { 2690270297Snp uint32_t s_qpp = sc->sge.eq_s_qpp; 2691270297Snp uint32_t mask = (1 << s_qpp) - 1; 2692270297Snp volatile uint8_t *udb; 2693270297Snp 2694270297Snp udb = sc->udbs_base + UDBS_DB_OFFSET; 2695270297Snp udb += (qid >> s_qpp) << PAGE_SHIFT; 2696270297Snp qid &= mask; 2697270297Snp if (qid < PAGE_SIZE / UDBS_SEG_SIZE) { 2698270297Snp udb += qid << UDBS_SEG_SHIFT; 2699270297Snp qid = 0; 2700270297Snp } 2701270297Snp fl->udb = (volatile void *)udb; 2702270297Snp } 2703270297Snp fl->dbval = F_DBPRIO | V_QID(qid); 2704270297Snp if (is_t5(sc)) 2705270297Snp fl->dbval |= F_DBTYPE; 2706270297Snp 2707218792Snp FL_LOCK(fl); 2708228561Snp /* Enough to make sure the SGE doesn't think it's starved */ 2709228561Snp refill_fl(sc, fl, fl->lowat); 2710218792Snp FL_UNLOCK(fl); 2711218792Snp } 2712218792Snp 2713253873Snp if (is_t5(sc) && cong >= 0) { 2714253873Snp uint32_t param, val; 2715253873Snp 2716253873Snp param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DMAQ) | 2717253873Snp V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DMAQ_CONM_CTXT) | 2718253873Snp V_FW_PARAMS_PARAM_YZ(iq->cntxt_id); 2719253889Snp if (cong == 0) 2720253889Snp val = 1 << 19; 2721253889Snp else { 2722253889Snp val = 2 << 19; 2723253889Snp for (i = 0; i < 4; i++) { 2724253889Snp if (cong & (1 << i)) 2725253889Snp val |= 1 << (i << 2); 2726253889Snp } 2727253889Snp } 2728253889Snp 2729253873Snp rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, ¶m, &val); 2730253873Snp if (rc != 0) { 2731253873Snp /* report error but carry on */ 2732253873Snp device_printf(sc->dev, 2733253873Snp "failed to set congestion manager context for " 2734253873Snp "ingress queue %d: %d\n", iq->cntxt_id, rc); 2735253873Snp } 2736253873Snp } 2737253873Snp 2738218792Snp /* Enable IQ interrupts */ 2739228561Snp atomic_store_rel_int(&iq->state, IQS_IDLE); 2740218792Snp t4_write_reg(sc, MYPF_REG(A_SGE_PF_GTS), V_SEINTARM(iq->intr_params) | 2741218792Snp V_INGRESSQID(iq->cntxt_id)); 2742218792Snp 2743218792Snp return (0); 2744218792Snp} 2745218792Snp 2746218792Snpstatic int 2747218792Snpfree_iq_fl(struct port_info *pi, struct sge_iq *iq, struct sge_fl *fl) 2748218792Snp{ 2749265425Snp int rc; 2750218792Snp struct adapter *sc = iq->adapter; 2751218792Snp device_t dev; 2752218792Snp 2753218792Snp if (sc == NULL) 2754218792Snp return (0); /* nothing to do */ 2755218792Snp 2756218792Snp dev = pi ? pi->dev : sc->dev; 2757218792Snp 2758218792Snp if (iq->flags & IQ_ALLOCATED) { 2759218792Snp rc = -t4_iq_free(sc, sc->mbox, sc->pf, 0, 2760218792Snp FW_IQ_TYPE_FL_INT_CAP, iq->cntxt_id, 2761218792Snp fl ? fl->cntxt_id : 0xffff, 0xffff); 2762218792Snp if (rc != 0) { 2763218792Snp device_printf(dev, 2764218792Snp "failed to free queue %p: %d\n", iq, rc); 2765218792Snp return (rc); 2766218792Snp } 2767218792Snp iq->flags &= ~IQ_ALLOCATED; 2768218792Snp } 2769218792Snp 2770218792Snp free_ring(sc, iq->desc_tag, iq->desc_map, iq->ba, iq->desc); 2771218792Snp 2772218792Snp bzero(iq, sizeof(*iq)); 2773218792Snp 2774218792Snp if (fl) { 2775218792Snp free_ring(sc, fl->desc_tag, fl->desc_map, fl->ba, 2776218792Snp fl->desc); 2777218792Snp 2778254727Snp if (fl->sdesc) 2779255050Snp free_fl_sdesc(sc, fl); 2780218792Snp 2781218792Snp if (mtx_initialized(&fl->fl_lock)) 2782218792Snp mtx_destroy(&fl->fl_lock); 2783218792Snp 2784218792Snp bzero(fl, sizeof(*fl)); 2785218792Snp } 2786218792Snp 2787218792Snp return (0); 2788218792Snp} 2789218792Snp 2790265425Snpstatic void 2791265425Snpadd_fl_sysctls(struct sysctl_ctx_list *ctx, struct sysctl_oid *oid, 2792265425Snp struct sge_fl *fl) 2793265425Snp{ 2794265425Snp struct sysctl_oid_list *children = SYSCTL_CHILDREN(oid); 2795265425Snp 2796265425Snp oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "fl", CTLFLAG_RD, NULL, 2797265425Snp "freelist"); 2798265425Snp children = SYSCTL_CHILDREN(oid); 2799265425Snp 2800265425Snp SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cntxt_id", 2801265425Snp CTLTYPE_INT | CTLFLAG_RD, &fl->cntxt_id, 0, sysctl_uint16, "I", 2802265425Snp "SGE context id of the freelist"); 2803281212Snp SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "padding", CTLFLAG_RD, NULL, 2804281212Snp fl_pad ? 1 : 0, "padding enabled"); 2805281212Snp SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "packing", CTLFLAG_RD, NULL, 2806281212Snp fl->flags & FL_BUF_PACKING ? 1 : 0, "packing enabled"); 2807265425Snp SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "cidx", CTLFLAG_RD, &fl->cidx, 2808265425Snp 0, "consumer index"); 2809265425Snp if (fl->flags & FL_BUF_PACKING) { 2810265425Snp SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "rx_offset", 2811265425Snp CTLFLAG_RD, &fl->rx_offset, 0, "packing rx offset"); 2812265425Snp } 2813265425Snp SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "pidx", CTLFLAG_RD, &fl->pidx, 2814265425Snp 0, "producer index"); 2815265425Snp SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "mbuf_allocated", 2816265425Snp CTLFLAG_RD, &fl->mbuf_allocated, "# of mbuf allocated"); 2817265425Snp SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "mbuf_inlined", 2818265425Snp CTLFLAG_RD, &fl->mbuf_inlined, "# of mbuf inlined in clusters"); 2819265425Snp SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "cluster_allocated", 2820265425Snp CTLFLAG_RD, &fl->cl_allocated, "# of clusters allocated"); 2821265425Snp SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "cluster_recycled", 2822265425Snp CTLFLAG_RD, &fl->cl_recycled, "# of clusters recycled"); 2823265425Snp SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "cluster_fast_recycled", 2824265425Snp CTLFLAG_RD, &fl->cl_fast_recycled, "# of clusters recycled (fast)"); 2825265425Snp} 2826265425Snp 2827218792Snpstatic int 2828228561Snpalloc_fwq(struct adapter *sc) 2829218792Snp{ 2830228561Snp int rc, intr_idx; 2831228561Snp struct sge_iq *fwq = &sc->sge.fwq; 2832228561Snp struct sysctl_oid *oid = device_get_sysctl_tree(sc->dev); 2833228561Snp struct sysctl_oid_list *children = SYSCTL_CHILDREN(oid); 2834222510Snp 2835270297Snp init_iq(fwq, sc, 0, 0, FW_IQ_QSIZE); 2836228561Snp fwq->flags |= IQ_INTR; /* always */ 2837228561Snp intr_idx = sc->intr_count > 1 ? 1 : 0; 2838228561Snp rc = alloc_iq_fl(sc->port[0], fwq, NULL, intr_idx, -1); 2839228561Snp if (rc != 0) { 2840228561Snp device_printf(sc->dev, 2841228561Snp "failed to create firmware event queue: %d\n", rc); 2842222510Snp return (rc); 2843228561Snp } 2844222510Snp 2845228561Snp oid = SYSCTL_ADD_NODE(&sc->ctx, children, OID_AUTO, "fwq", CTLFLAG_RD, 2846228561Snp NULL, "firmware event queue"); 2847222510Snp children = SYSCTL_CHILDREN(oid); 2848222510Snp 2849228561Snp SYSCTL_ADD_PROC(&sc->ctx, children, OID_AUTO, "abs_id", 2850228561Snp CTLTYPE_INT | CTLFLAG_RD, &fwq->abs_id, 0, sysctl_uint16, "I", 2851228561Snp "absolute id of the queue"); 2852228561Snp SYSCTL_ADD_PROC(&sc->ctx, children, OID_AUTO, "cntxt_id", 2853228561Snp CTLTYPE_INT | CTLFLAG_RD, &fwq->cntxt_id, 0, sysctl_uint16, "I", 2854228561Snp "SGE context id of the queue"); 2855222510Snp SYSCTL_ADD_PROC(&sc->ctx, children, OID_AUTO, "cidx", 2856228561Snp CTLTYPE_INT | CTLFLAG_RD, &fwq->cidx, 0, sysctl_uint16, "I", 2857222510Snp "consumer index"); 2858222510Snp 2859228561Snp return (0); 2860218792Snp} 2861218792Snp 2862218792Snpstatic int 2863228561Snpfree_fwq(struct adapter *sc) 2864218792Snp{ 2865228561Snp return free_iq_fl(NULL, &sc->sge.fwq, NULL); 2866218792Snp} 2867218792Snp 2868218792Snpstatic int 2869228561Snpalloc_mgmtq(struct adapter *sc) 2870222510Snp{ 2871222510Snp int rc; 2872228561Snp struct sge_wrq *mgmtq = &sc->sge.mgmtq; 2873228561Snp char name[16]; 2874228561Snp struct sysctl_oid *oid = device_get_sysctl_tree(sc->dev); 2875228561Snp struct sysctl_oid_list *children = SYSCTL_CHILDREN(oid); 2876222510Snp 2877228561Snp oid = SYSCTL_ADD_NODE(&sc->ctx, children, OID_AUTO, "mgmtq", CTLFLAG_RD, 2878228561Snp NULL, "management queue"); 2879228561Snp 2880228561Snp snprintf(name, sizeof(name), "%s mgmtq", device_get_nameunit(sc->dev)); 2881228561Snp init_eq(&mgmtq->eq, EQ_CTRL, CTRL_EQ_QSIZE, sc->port[0]->tx_chan, 2882228561Snp sc->sge.fwq.cntxt_id, name); 2883228561Snp rc = alloc_wrq(sc, NULL, mgmtq, oid); 2884228561Snp if (rc != 0) { 2885228561Snp device_printf(sc->dev, 2886228561Snp "failed to create management queue: %d\n", rc); 2887222510Snp return (rc); 2888228561Snp } 2889222510Snp 2890228561Snp return (0); 2891222510Snp} 2892222510Snp 2893222510Snpstatic int 2894228561Snpfree_mgmtq(struct adapter *sc) 2895222510Snp{ 2896237263Snp 2897228561Snp return free_wrq(sc, &sc->sge.mgmtq); 2898222510Snp} 2899222510Snp 2900281253Snpint 2901286274Snptnl_cong(struct port_info *pi, int drop) 2902239258Snp{ 2903239258Snp 2904286274Snp if (drop == -1) 2905239258Snp return (-1); 2906286274Snp else if (drop == 1) 2907239258Snp return (0); 2908239258Snp else 2909265410Snp return (pi->rx_chan_map); 2910239258Snp} 2911239258Snp 2912222510Snpstatic int 2913228561Snpalloc_rxq(struct port_info *pi, struct sge_rxq *rxq, int intr_idx, int idx, 2914228561Snp struct sysctl_oid *oid) 2915218792Snp{ 2916218792Snp int rc; 2917218792Snp struct sysctl_oid_list *children; 2918218792Snp char name[16]; 2919218792Snp 2920286274Snp rc = alloc_iq_fl(pi, &rxq->iq, &rxq->fl, intr_idx, 2921286274Snp tnl_cong(pi, cong_drop)); 2922218792Snp if (rc != 0) 2923218792Snp return (rc); 2924218792Snp 2925270297Snp /* 2926270297Snp * The freelist is just barely above the starvation threshold right now, 2927270297Snp * fill it up a bit more. 2928270297Snp */ 2929222701Snp FL_LOCK(&rxq->fl); 2930270297Snp refill_fl(pi->adapter, &rxq->fl, 128); 2931222701Snp FL_UNLOCK(&rxq->fl); 2932222701Snp 2933237819Snp#if defined(INET) || defined(INET6) 2934218792Snp rc = tcp_lro_init(&rxq->lro); 2935218792Snp if (rc != 0) 2936218792Snp return (rc); 2937218792Snp rxq->lro.ifp = pi->ifp; /* also indicates LRO init'ed */ 2938218792Snp 2939218792Snp if (pi->ifp->if_capenable & IFCAP_LRO) 2940228561Snp rxq->iq.flags |= IQ_LRO_ENABLED; 2941218792Snp#endif 2942219289Snp rxq->ifp = pi->ifp; 2943218792Snp 2944228561Snp children = SYSCTL_CHILDREN(oid); 2945218792Snp 2946218792Snp snprintf(name, sizeof(name), "%d", idx); 2947218792Snp oid = SYSCTL_ADD_NODE(&pi->ctx, children, OID_AUTO, name, CTLFLAG_RD, 2948218792Snp NULL, "rx queue"); 2949218792Snp children = SYSCTL_CHILDREN(oid); 2950218792Snp 2951221911Snp SYSCTL_ADD_PROC(&pi->ctx, children, OID_AUTO, "abs_id", 2952222510Snp CTLTYPE_INT | CTLFLAG_RD, &rxq->iq.abs_id, 0, sysctl_uint16, "I", 2953221911Snp "absolute id of the queue"); 2954222973Snp SYSCTL_ADD_PROC(&pi->ctx, children, OID_AUTO, "cntxt_id", 2955222973Snp CTLTYPE_INT | CTLFLAG_RD, &rxq->iq.cntxt_id, 0, sysctl_uint16, "I", 2956222973Snp "SGE context id of the queue"); 2957222973Snp SYSCTL_ADD_PROC(&pi->ctx, children, OID_AUTO, "cidx", 2958222973Snp CTLTYPE_INT | CTLFLAG_RD, &rxq->iq.cidx, 0, sysctl_uint16, "I", 2959222973Snp "consumer index"); 2960237819Snp#if defined(INET) || defined(INET6) 2961218792Snp SYSCTL_ADD_INT(&pi->ctx, children, OID_AUTO, "lro_queued", CTLFLAG_RD, 2962218792Snp &rxq->lro.lro_queued, 0, NULL); 2963218792Snp SYSCTL_ADD_INT(&pi->ctx, children, OID_AUTO, "lro_flushed", CTLFLAG_RD, 2964218792Snp &rxq->lro.lro_flushed, 0, NULL); 2965219290Snp#endif 2966218792Snp SYSCTL_ADD_UQUAD(&pi->ctx, children, OID_AUTO, "rxcsum", CTLFLAG_RD, 2967218792Snp &rxq->rxcsum, "# of times hardware assisted with checksum"); 2968218792Snp SYSCTL_ADD_UQUAD(&pi->ctx, children, OID_AUTO, "vlan_extraction", 2969218792Snp CTLFLAG_RD, &rxq->vlan_extraction, 2970218792Snp "# of times hardware extracted 802.1Q tag"); 2971218792Snp 2972265425Snp add_fl_sysctls(&pi->ctx, oid, &rxq->fl); 2973222973Snp 2974218792Snp return (rc); 2975218792Snp} 2976218792Snp 2977218792Snpstatic int 2978218792Snpfree_rxq(struct port_info *pi, struct sge_rxq *rxq) 2979218792Snp{ 2980218792Snp int rc; 2981218792Snp 2982237819Snp#if defined(INET) || defined(INET6) 2983218792Snp if (rxq->lro.ifp) { 2984218792Snp tcp_lro_free(&rxq->lro); 2985218792Snp rxq->lro.ifp = NULL; 2986218792Snp } 2987218792Snp#endif 2988218792Snp 2989218792Snp rc = free_iq_fl(pi, &rxq->iq, &rxq->fl); 2990218792Snp if (rc == 0) 2991218792Snp bzero(rxq, sizeof(*rxq)); 2992218792Snp 2993218792Snp return (rc); 2994218792Snp} 2995218792Snp 2996237263Snp#ifdef TCP_OFFLOAD 2997218792Snpstatic int 2998228561Snpalloc_ofld_rxq(struct port_info *pi, struct sge_ofld_rxq *ofld_rxq, 2999228561Snp int intr_idx, int idx, struct sysctl_oid *oid) 3000220873Snp{ 3001228561Snp int rc; 3002228561Snp struct sysctl_oid_list *children; 3003220873Snp char name[16]; 3004220873Snp 3005228561Snp rc = alloc_iq_fl(pi, &ofld_rxq->iq, &ofld_rxq->fl, intr_idx, 3006265410Snp pi->rx_chan_map); 3007228561Snp if (rc != 0) 3008220873Snp return (rc); 3009220873Snp 3010228561Snp children = SYSCTL_CHILDREN(oid); 3011220873Snp 3012228561Snp snprintf(name, sizeof(name), "%d", idx); 3013228561Snp oid = SYSCTL_ADD_NODE(&pi->ctx, children, OID_AUTO, name, CTLFLAG_RD, 3014228561Snp NULL, "rx queue"); 3015228561Snp children = SYSCTL_CHILDREN(oid); 3016228561Snp 3017228561Snp SYSCTL_ADD_PROC(&pi->ctx, children, OID_AUTO, "abs_id", 3018228561Snp CTLTYPE_INT | CTLFLAG_RD, &ofld_rxq->iq.abs_id, 0, sysctl_uint16, 3019228561Snp "I", "absolute id of the queue"); 3020228561Snp SYSCTL_ADD_PROC(&pi->ctx, children, OID_AUTO, "cntxt_id", 3021228561Snp CTLTYPE_INT | CTLFLAG_RD, &ofld_rxq->iq.cntxt_id, 0, sysctl_uint16, 3022228561Snp "I", "SGE context id of the queue"); 3023228561Snp SYSCTL_ADD_PROC(&pi->ctx, children, OID_AUTO, "cidx", 3024228561Snp CTLTYPE_INT | CTLFLAG_RD, &ofld_rxq->iq.cidx, 0, sysctl_uint16, "I", 3025228561Snp "consumer index"); 3026228561Snp 3027265425Snp add_fl_sysctls(&pi->ctx, oid, &ofld_rxq->fl); 3028228561Snp 3029228561Snp return (rc); 3030228561Snp} 3031228561Snp 3032228561Snpstatic int 3033228561Snpfree_ofld_rxq(struct port_info *pi, struct sge_ofld_rxq *ofld_rxq) 3034228561Snp{ 3035228561Snp int rc; 3036228561Snp 3037228561Snp rc = free_iq_fl(pi, &ofld_rxq->iq, &ofld_rxq->fl); 3038228561Snp if (rc == 0) 3039228561Snp bzero(ofld_rxq, sizeof(*ofld_rxq)); 3040228561Snp 3041228561Snp return (rc); 3042228561Snp} 3043228561Snp#endif 3044228561Snp 3045270297Snp#ifdef DEV_NETMAP 3046228561Snpstatic int 3047270297Snpalloc_nm_rxq(struct port_info *pi, struct sge_nm_rxq *nm_rxq, int intr_idx, 3048270297Snp int idx, struct sysctl_oid *oid) 3049270297Snp{ 3050270297Snp int rc; 3051270297Snp struct sysctl_oid_list *children; 3052270297Snp struct sysctl_ctx_list *ctx; 3053270297Snp char name[16]; 3054270297Snp size_t len; 3055270297Snp struct adapter *sc = pi->adapter; 3056270297Snp struct netmap_adapter *na = NA(pi->nm_ifp); 3057270297Snp 3058270297Snp MPASS(na != NULL); 3059270297Snp 3060270297Snp len = pi->qsize_rxq * IQ_ESIZE; 3061270297Snp rc = alloc_ring(sc, len, &nm_rxq->iq_desc_tag, &nm_rxq->iq_desc_map, 3062270297Snp &nm_rxq->iq_ba, (void **)&nm_rxq->iq_desc); 3063270297Snp if (rc != 0) 3064270297Snp return (rc); 3065270297Snp 3066270297Snp len = na->num_rx_desc * EQ_ESIZE + spg_len; 3067270297Snp rc = alloc_ring(sc, len, &nm_rxq->fl_desc_tag, &nm_rxq->fl_desc_map, 3068270297Snp &nm_rxq->fl_ba, (void **)&nm_rxq->fl_desc); 3069270297Snp if (rc != 0) 3070270297Snp return (rc); 3071270297Snp 3072270297Snp nm_rxq->pi = pi; 3073270297Snp nm_rxq->nid = idx; 3074270297Snp nm_rxq->iq_cidx = 0; 3075270297Snp nm_rxq->iq_sidx = pi->qsize_rxq - spg_len / IQ_ESIZE; 3076270297Snp nm_rxq->iq_gen = F_RSPD_GEN; 3077270297Snp nm_rxq->fl_pidx = nm_rxq->fl_cidx = 0; 3078270297Snp nm_rxq->fl_sidx = na->num_rx_desc; 3079270297Snp nm_rxq->intr_idx = intr_idx; 3080270297Snp 3081270297Snp ctx = &pi->ctx; 3082270297Snp children = SYSCTL_CHILDREN(oid); 3083270297Snp 3084270297Snp snprintf(name, sizeof(name), "%d", idx); 3085270297Snp oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, name, CTLFLAG_RD, NULL, 3086270297Snp "rx queue"); 3087270297Snp children = SYSCTL_CHILDREN(oid); 3088270297Snp 3089270297Snp SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "abs_id", 3090270297Snp CTLTYPE_INT | CTLFLAG_RD, &nm_rxq->iq_abs_id, 0, sysctl_uint16, 3091270297Snp "I", "absolute id of the queue"); 3092270297Snp SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cntxt_id", 3093270297Snp CTLTYPE_INT | CTLFLAG_RD, &nm_rxq->iq_cntxt_id, 0, sysctl_uint16, 3094270297Snp "I", "SGE context id of the queue"); 3095270297Snp SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cidx", 3096270297Snp CTLTYPE_INT | CTLFLAG_RD, &nm_rxq->iq_cidx, 0, sysctl_uint16, "I", 3097270297Snp "consumer index"); 3098270297Snp 3099270297Snp children = SYSCTL_CHILDREN(oid); 3100270297Snp oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "fl", CTLFLAG_RD, NULL, 3101270297Snp "freelist"); 3102270297Snp children = SYSCTL_CHILDREN(oid); 3103270297Snp 3104270297Snp SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cntxt_id", 3105270297Snp CTLTYPE_INT | CTLFLAG_RD, &nm_rxq->fl_cntxt_id, 0, sysctl_uint16, 3106270297Snp "I", "SGE context id of the freelist"); 3107270297Snp SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "cidx", CTLFLAG_RD, 3108270297Snp &nm_rxq->fl_cidx, 0, "consumer index"); 3109270297Snp SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "pidx", CTLFLAG_RD, 3110270297Snp &nm_rxq->fl_pidx, 0, "producer index"); 3111270297Snp 3112270297Snp return (rc); 3113270297Snp} 3114270297Snp 3115270297Snp 3116270297Snpstatic int 3117270297Snpfree_nm_rxq(struct port_info *pi, struct sge_nm_rxq *nm_rxq) 3118270297Snp{ 3119270297Snp struct adapter *sc = pi->adapter; 3120270297Snp 3121270297Snp free_ring(sc, nm_rxq->iq_desc_tag, nm_rxq->iq_desc_map, nm_rxq->iq_ba, 3122270297Snp nm_rxq->iq_desc); 3123270297Snp free_ring(sc, nm_rxq->fl_desc_tag, nm_rxq->fl_desc_map, nm_rxq->fl_ba, 3124270297Snp nm_rxq->fl_desc); 3125270297Snp 3126270297Snp return (0); 3127270297Snp} 3128270297Snp 3129270297Snpstatic int 3130270297Snpalloc_nm_txq(struct port_info *pi, struct sge_nm_txq *nm_txq, int iqidx, int idx, 3131270297Snp struct sysctl_oid *oid) 3132270297Snp{ 3133270297Snp int rc; 3134270297Snp size_t len; 3135270297Snp struct adapter *sc = pi->adapter; 3136270297Snp struct netmap_adapter *na = NA(pi->nm_ifp); 3137270297Snp char name[16]; 3138270297Snp struct sysctl_oid_list *children = SYSCTL_CHILDREN(oid); 3139270297Snp 3140270297Snp len = na->num_tx_desc * EQ_ESIZE + spg_len; 3141270297Snp rc = alloc_ring(sc, len, &nm_txq->desc_tag, &nm_txq->desc_map, 3142270297Snp &nm_txq->ba, (void **)&nm_txq->desc); 3143270297Snp if (rc) 3144270297Snp return (rc); 3145270297Snp 3146270297Snp nm_txq->pidx = nm_txq->cidx = 0; 3147270297Snp nm_txq->sidx = na->num_tx_desc; 3148270297Snp nm_txq->nid = idx; 3149270297Snp nm_txq->iqidx = iqidx; 3150270297Snp nm_txq->cpl_ctrl0 = htobe32(V_TXPKT_OPCODE(CPL_TX_PKT) | 3151270297Snp V_TXPKT_INTF(pi->tx_chan) | V_TXPKT_PF(sc->pf)); 3152270297Snp 3153270297Snp snprintf(name, sizeof(name), "%d", idx); 3154270297Snp oid = SYSCTL_ADD_NODE(&pi->ctx, children, OID_AUTO, name, CTLFLAG_RD, 3155270297Snp NULL, "netmap tx queue"); 3156270297Snp children = SYSCTL_CHILDREN(oid); 3157270297Snp 3158270297Snp SYSCTL_ADD_UINT(&pi->ctx, children, OID_AUTO, "cntxt_id", CTLFLAG_RD, 3159270297Snp &nm_txq->cntxt_id, 0, "SGE context id of the queue"); 3160270297Snp SYSCTL_ADD_PROC(&pi->ctx, children, OID_AUTO, "cidx", 3161270297Snp CTLTYPE_INT | CTLFLAG_RD, &nm_txq->cidx, 0, sysctl_uint16, "I", 3162270297Snp "consumer index"); 3163270297Snp SYSCTL_ADD_PROC(&pi->ctx, children, OID_AUTO, "pidx", 3164270297Snp CTLTYPE_INT | CTLFLAG_RD, &nm_txq->pidx, 0, sysctl_uint16, "I", 3165270297Snp "producer index"); 3166270297Snp 3167270297Snp return (rc); 3168270297Snp} 3169270297Snp 3170270297Snpstatic int 3171270297Snpfree_nm_txq(struct port_info *pi, struct sge_nm_txq *nm_txq) 3172270297Snp{ 3173270297Snp struct adapter *sc = pi->adapter; 3174270297Snp 3175270297Snp free_ring(sc, nm_txq->desc_tag, nm_txq->desc_map, nm_txq->ba, 3176270297Snp nm_txq->desc); 3177270297Snp 3178270297Snp return (0); 3179270297Snp} 3180270297Snp#endif 3181270297Snp 3182270297Snpstatic int 3183228561Snpctrl_eq_alloc(struct adapter *sc, struct sge_eq *eq) 3184228561Snp{ 3185228561Snp int rc, cntxt_id; 3186228561Snp struct fw_eq_ctrl_cmd c; 3187284052Snp int qsize = eq->sidx + spg_len / EQ_ESIZE; 3188228561Snp 3189220873Snp bzero(&c, sizeof(c)); 3190220873Snp 3191220873Snp c.op_to_vfn = htobe32(V_FW_CMD_OP(FW_EQ_CTRL_CMD) | F_FW_CMD_REQUEST | 3192220873Snp F_FW_CMD_WRITE | F_FW_CMD_EXEC | V_FW_EQ_CTRL_CMD_PFN(sc->pf) | 3193220873Snp V_FW_EQ_CTRL_CMD_VFN(0)); 3194220873Snp c.alloc_to_len16 = htobe32(F_FW_EQ_CTRL_CMD_ALLOC | 3195220873Snp F_FW_EQ_CTRL_CMD_EQSTART | FW_LEN16(c)); 3196284052Snp c.cmpliqid_eqid = htonl(V_FW_EQ_CTRL_CMD_CMPLIQID(eq->iqid)); 3197220873Snp c.physeqid_pkd = htobe32(0); 3198220873Snp c.fetchszm_to_iqid = 3199284052Snp htobe32(V_FW_EQ_CTRL_CMD_HOSTFCMODE(X_HOSTFCMODE_NONE) | 3200228561Snp V_FW_EQ_CTRL_CMD_PCIECHN(eq->tx_chan) | 3201222510Snp F_FW_EQ_CTRL_CMD_FETCHRO | V_FW_EQ_CTRL_CMD_IQID(eq->iqid)); 3202220873Snp c.dcaen_to_eqsize = 3203220873Snp htobe32(V_FW_EQ_CTRL_CMD_FBMIN(X_FETCHBURSTMIN_64B) | 3204220873Snp V_FW_EQ_CTRL_CMD_FBMAX(X_FETCHBURSTMAX_512B) | 3205284052Snp V_FW_EQ_CTRL_CMD_EQSIZE(qsize)); 3206220873Snp c.eqaddr = htobe64(eq->ba); 3207220873Snp 3208220873Snp rc = -t4_wr_mbox(sc, sc->mbox, &c, sizeof(c), &c); 3209220873Snp if (rc != 0) { 3210220873Snp device_printf(sc->dev, 3211228561Snp "failed to create control queue %d: %d\n", eq->tx_chan, rc); 3212220873Snp return (rc); 3213220873Snp } 3214228561Snp eq->flags |= EQ_ALLOCATED; 3215220873Snp 3216220873Snp eq->cntxt_id = G_FW_EQ_CTRL_CMD_EQID(be32toh(c.cmpliqid_eqid)); 3217228561Snp cntxt_id = eq->cntxt_id - sc->sge.eq_start; 3218228561Snp if (cntxt_id >= sc->sge.neq) 3219228561Snp panic("%s: eq->cntxt_id (%d) more than the max (%d)", __func__, 3220228561Snp cntxt_id, sc->sge.neq - 1); 3221228561Snp sc->sge.eqmap[cntxt_id] = eq; 3222220873Snp 3223228561Snp return (rc); 3224228561Snp} 3225228561Snp 3226228561Snpstatic int 3227228561Snpeth_eq_alloc(struct adapter *sc, struct port_info *pi, struct sge_eq *eq) 3228228561Snp{ 3229228561Snp int rc, cntxt_id; 3230228561Snp struct fw_eq_eth_cmd c; 3231284052Snp int qsize = eq->sidx + spg_len / EQ_ESIZE; 3232228561Snp 3233228561Snp bzero(&c, sizeof(c)); 3234228561Snp 3235228561Snp c.op_to_vfn = htobe32(V_FW_CMD_OP(FW_EQ_ETH_CMD) | F_FW_CMD_REQUEST | 3236228561Snp F_FW_CMD_WRITE | F_FW_CMD_EXEC | V_FW_EQ_ETH_CMD_PFN(sc->pf) | 3237228561Snp V_FW_EQ_ETH_CMD_VFN(0)); 3238228561Snp c.alloc_to_len16 = htobe32(F_FW_EQ_ETH_CMD_ALLOC | 3239228561Snp F_FW_EQ_ETH_CMD_EQSTART | FW_LEN16(c)); 3240284052Snp c.autoequiqe_to_viid = htobe32(F_FW_EQ_ETH_CMD_AUTOEQUIQE | 3241284052Snp F_FW_EQ_ETH_CMD_AUTOEQUEQE | V_FW_EQ_ETH_CMD_VIID(pi->viid)); 3242228561Snp c.fetchszm_to_iqid = 3243284052Snp htobe32(V_FW_EQ_ETH_CMD_HOSTFCMODE(X_HOSTFCMODE_NONE) | 3244228561Snp V_FW_EQ_ETH_CMD_PCIECHN(eq->tx_chan) | F_FW_EQ_ETH_CMD_FETCHRO | 3245228561Snp V_FW_EQ_ETH_CMD_IQID(eq->iqid)); 3246228561Snp c.dcaen_to_eqsize = htobe32(V_FW_EQ_ETH_CMD_FBMIN(X_FETCHBURSTMIN_64B) | 3247284052Snp V_FW_EQ_ETH_CMD_FBMAX(X_FETCHBURSTMAX_512B) | 3248284052Snp V_FW_EQ_ETH_CMD_EQSIZE(qsize)); 3249228561Snp c.eqaddr = htobe64(eq->ba); 3250228561Snp 3251228561Snp rc = -t4_wr_mbox(sc, sc->mbox, &c, sizeof(c), &c); 3252228561Snp if (rc != 0) { 3253228561Snp device_printf(pi->dev, 3254228561Snp "failed to create Ethernet egress queue: %d\n", rc); 3255228561Snp return (rc); 3256228561Snp } 3257228561Snp eq->flags |= EQ_ALLOCATED; 3258228561Snp 3259228561Snp eq->cntxt_id = G_FW_EQ_ETH_CMD_EQID(be32toh(c.eqid_pkd)); 3260220873Snp cntxt_id = eq->cntxt_id - sc->sge.eq_start; 3261228561Snp if (cntxt_id >= sc->sge.neq) 3262228561Snp panic("%s: eq->cntxt_id (%d) more than the max (%d)", __func__, 3263228561Snp cntxt_id, sc->sge.neq - 1); 3264220873Snp sc->sge.eqmap[cntxt_id] = eq; 3265220873Snp 3266228561Snp return (rc); 3267228561Snp} 3268220873Snp 3269237263Snp#ifdef TCP_OFFLOAD 3270228561Snpstatic int 3271228561Snpofld_eq_alloc(struct adapter *sc, struct port_info *pi, struct sge_eq *eq) 3272228561Snp{ 3273228561Snp int rc, cntxt_id; 3274228561Snp struct fw_eq_ofld_cmd c; 3275284052Snp int qsize = eq->sidx + spg_len / EQ_ESIZE; 3276220873Snp 3277228561Snp bzero(&c, sizeof(c)); 3278220873Snp 3279228561Snp c.op_to_vfn = htonl(V_FW_CMD_OP(FW_EQ_OFLD_CMD) | F_FW_CMD_REQUEST | 3280228561Snp F_FW_CMD_WRITE | F_FW_CMD_EXEC | V_FW_EQ_OFLD_CMD_PFN(sc->pf) | 3281228561Snp V_FW_EQ_OFLD_CMD_VFN(0)); 3282228561Snp c.alloc_to_len16 = htonl(F_FW_EQ_OFLD_CMD_ALLOC | 3283228561Snp F_FW_EQ_OFLD_CMD_EQSTART | FW_LEN16(c)); 3284228561Snp c.fetchszm_to_iqid = 3285284052Snp htonl(V_FW_EQ_OFLD_CMD_HOSTFCMODE(X_HOSTFCMODE_NONE) | 3286228561Snp V_FW_EQ_OFLD_CMD_PCIECHN(eq->tx_chan) | 3287228561Snp F_FW_EQ_OFLD_CMD_FETCHRO | V_FW_EQ_OFLD_CMD_IQID(eq->iqid)); 3288228561Snp c.dcaen_to_eqsize = 3289228561Snp htobe32(V_FW_EQ_OFLD_CMD_FBMIN(X_FETCHBURSTMIN_64B) | 3290228561Snp V_FW_EQ_OFLD_CMD_FBMAX(X_FETCHBURSTMAX_512B) | 3291284052Snp V_FW_EQ_OFLD_CMD_EQSIZE(qsize)); 3292228561Snp c.eqaddr = htobe64(eq->ba); 3293228561Snp 3294228561Snp rc = -t4_wr_mbox(sc, sc->mbox, &c, sizeof(c), &c); 3295228561Snp if (rc != 0) { 3296228561Snp device_printf(pi->dev, 3297228561Snp "failed to create egress queue for TCP offload: %d\n", rc); 3298228561Snp return (rc); 3299228561Snp } 3300228561Snp eq->flags |= EQ_ALLOCATED; 3301228561Snp 3302228561Snp eq->cntxt_id = G_FW_EQ_OFLD_CMD_EQID(be32toh(c.eqid_pkd)); 3303228561Snp cntxt_id = eq->cntxt_id - sc->sge.eq_start; 3304228561Snp if (cntxt_id >= sc->sge.neq) 3305228561Snp panic("%s: eq->cntxt_id (%d) more than the max (%d)", __func__, 3306228561Snp cntxt_id, sc->sge.neq - 1); 3307228561Snp sc->sge.eqmap[cntxt_id] = eq; 3308228561Snp 3309220873Snp return (rc); 3310220873Snp} 3311228561Snp#endif 3312220873Snp 3313220873Snpstatic int 3314228561Snpalloc_eq(struct adapter *sc, struct port_info *pi, struct sge_eq *eq) 3315220873Snp{ 3316284052Snp int rc, qsize; 3317228561Snp size_t len; 3318220873Snp 3319228561Snp mtx_init(&eq->eq_lock, eq->lockname, NULL, MTX_DEF); 3320228561Snp 3321284052Snp qsize = eq->sidx + spg_len / EQ_ESIZE; 3322284052Snp len = qsize * EQ_ESIZE; 3323228561Snp rc = alloc_ring(sc, len, &eq->desc_tag, &eq->desc_map, 3324228561Snp &eq->ba, (void **)&eq->desc); 3325228561Snp if (rc) 3326228561Snp return (rc); 3327228561Snp 3328228561Snp eq->pidx = eq->cidx = 0; 3329284052Snp eq->equeqidx = eq->dbidx = 0; 3330248925Snp eq->doorbells = sc->doorbells; 3331228561Snp 3332228561Snp switch (eq->flags & EQ_TYPEMASK) { 3333228561Snp case EQ_CTRL: 3334228561Snp rc = ctrl_eq_alloc(sc, eq); 3335228561Snp break; 3336228561Snp 3337228561Snp case EQ_ETH: 3338228561Snp rc = eth_eq_alloc(sc, pi, eq); 3339228561Snp break; 3340228561Snp 3341237263Snp#ifdef TCP_OFFLOAD 3342228561Snp case EQ_OFLD: 3343228561Snp rc = ofld_eq_alloc(sc, pi, eq); 3344228561Snp break; 3345228561Snp#endif 3346228561Snp 3347228561Snp default: 3348228561Snp panic("%s: invalid eq type %d.", __func__, 3349228561Snp eq->flags & EQ_TYPEMASK); 3350228561Snp } 3351228561Snp if (rc != 0) { 3352228561Snp device_printf(sc->dev, 3353269082Snp "failed to allocate egress queue(%d): %d\n", 3354228561Snp eq->flags & EQ_TYPEMASK, rc); 3355228561Snp } 3356228561Snp 3357248925Snp if (isset(&eq->doorbells, DOORBELL_UDB) || 3358248925Snp isset(&eq->doorbells, DOORBELL_UDBWC) || 3359249392Snp isset(&eq->doorbells, DOORBELL_WCWR)) { 3360256794Snp uint32_t s_qpp = sc->sge.eq_s_qpp; 3361248925Snp uint32_t mask = (1 << s_qpp) - 1; 3362248925Snp volatile uint8_t *udb; 3363248925Snp 3364248925Snp udb = sc->udbs_base + UDBS_DB_OFFSET; 3365248925Snp udb += (eq->cntxt_id >> s_qpp) << PAGE_SHIFT; /* pg offset */ 3366248925Snp eq->udb_qid = eq->cntxt_id & mask; /* id in page */ 3367270297Snp if (eq->udb_qid >= PAGE_SIZE / UDBS_SEG_SIZE) 3368249392Snp clrbit(&eq->doorbells, DOORBELL_WCWR); 3369248925Snp else { 3370248925Snp udb += eq->udb_qid << UDBS_SEG_SHIFT; /* seg offset */ 3371248925Snp eq->udb_qid = 0; 3372248925Snp } 3373248925Snp eq->udb = (volatile void *)udb; 3374248925Snp } 3375248925Snp 3376228561Snp return (rc); 3377228561Snp} 3378228561Snp 3379228561Snpstatic int 3380228561Snpfree_eq(struct adapter *sc, struct sge_eq *eq) 3381228561Snp{ 3382228561Snp int rc; 3383228561Snp 3384228561Snp if (eq->flags & EQ_ALLOCATED) { 3385228561Snp switch (eq->flags & EQ_TYPEMASK) { 3386228561Snp case EQ_CTRL: 3387228561Snp rc = -t4_ctrl_eq_free(sc, sc->mbox, sc->pf, 0, 3388228561Snp eq->cntxt_id); 3389228561Snp break; 3390228561Snp 3391228561Snp case EQ_ETH: 3392228561Snp rc = -t4_eth_eq_free(sc, sc->mbox, sc->pf, 0, 3393228561Snp eq->cntxt_id); 3394228561Snp break; 3395228561Snp 3396237263Snp#ifdef TCP_OFFLOAD 3397228561Snp case EQ_OFLD: 3398228561Snp rc = -t4_ofld_eq_free(sc, sc->mbox, sc->pf, 0, 3399228561Snp eq->cntxt_id); 3400228561Snp break; 3401228561Snp#endif 3402228561Snp 3403228561Snp default: 3404228561Snp panic("%s: invalid eq type %d.", __func__, 3405228561Snp eq->flags & EQ_TYPEMASK); 3406228561Snp } 3407220873Snp if (rc != 0) { 3408220873Snp device_printf(sc->dev, 3409228561Snp "failed to free egress queue (%d): %d\n", 3410228561Snp eq->flags & EQ_TYPEMASK, rc); 3411220873Snp return (rc); 3412220873Snp } 3413228561Snp eq->flags &= ~EQ_ALLOCATED; 3414220873Snp } 3415220873Snp 3416220873Snp free_ring(sc, eq->desc_tag, eq->desc_map, eq->ba, eq->desc); 3417220873Snp 3418220873Snp if (mtx_initialized(&eq->eq_lock)) 3419220873Snp mtx_destroy(&eq->eq_lock); 3420220873Snp 3421228561Snp bzero(eq, sizeof(*eq)); 3422220873Snp return (0); 3423220873Snp} 3424220873Snp 3425220873Snpstatic int 3426228561Snpalloc_wrq(struct adapter *sc, struct port_info *pi, struct sge_wrq *wrq, 3427228561Snp struct sysctl_oid *oid) 3428218792Snp{ 3429228561Snp int rc; 3430228561Snp struct sysctl_ctx_list *ctx = pi ? &pi->ctx : &sc->ctx; 3431228561Snp struct sysctl_oid_list *children = SYSCTL_CHILDREN(oid); 3432228561Snp 3433228561Snp rc = alloc_eq(sc, pi, &wrq->eq); 3434228561Snp if (rc) 3435228561Snp return (rc); 3436228561Snp 3437228561Snp wrq->adapter = sc; 3438284052Snp TASK_INIT(&wrq->wrq_tx_task, 0, wrq_tx_drain, wrq); 3439284052Snp TAILQ_INIT(&wrq->incomplete_wrs); 3440237263Snp STAILQ_INIT(&wrq->wr_list); 3441284052Snp wrq->nwr_pending = 0; 3442284052Snp wrq->ndesc_needed = 0; 3443228561Snp 3444228561Snp SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "cntxt_id", CTLFLAG_RD, 3445228561Snp &wrq->eq.cntxt_id, 0, "SGE context id of the queue"); 3446228561Snp SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cidx", 3447228561Snp CTLTYPE_INT | CTLFLAG_RD, &wrq->eq.cidx, 0, sysctl_uint16, "I", 3448228561Snp "consumer index"); 3449228561Snp SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "pidx", 3450228561Snp CTLTYPE_INT | CTLFLAG_RD, &wrq->eq.pidx, 0, sysctl_uint16, "I", 3451228561Snp "producer index"); 3452284052Snp SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "tx_wrs_direct", CTLFLAG_RD, 3453284052Snp &wrq->tx_wrs_direct, "# of work requests (direct)"); 3454284052Snp SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "tx_wrs_copied", CTLFLAG_RD, 3455284052Snp &wrq->tx_wrs_copied, "# of work requests (copied)"); 3456228561Snp 3457228561Snp return (rc); 3458228561Snp} 3459228561Snp 3460228561Snpstatic int 3461228561Snpfree_wrq(struct adapter *sc, struct sge_wrq *wrq) 3462228561Snp{ 3463228561Snp int rc; 3464228561Snp 3465228561Snp rc = free_eq(sc, &wrq->eq); 3466228561Snp if (rc) 3467228561Snp return (rc); 3468228561Snp 3469228561Snp bzero(wrq, sizeof(*wrq)); 3470228561Snp return (0); 3471228561Snp} 3472228561Snp 3473228561Snpstatic int 3474228561Snpalloc_txq(struct port_info *pi, struct sge_txq *txq, int idx, 3475228561Snp struct sysctl_oid *oid) 3476228561Snp{ 3477228561Snp int rc; 3478218792Snp struct adapter *sc = pi->adapter; 3479218792Snp struct sge_eq *eq = &txq->eq; 3480218792Snp char name[16]; 3481228561Snp struct sysctl_oid_list *children = SYSCTL_CHILDREN(oid); 3482218792Snp 3483284052Snp rc = mp_ring_alloc(&txq->r, eq->sidx, txq, eth_tx, can_resume_eth_tx, 3484284052Snp M_CXGBE, M_WAITOK); 3485218792Snp if (rc != 0) { 3486284052Snp device_printf(sc->dev, "failed to allocate mp_ring: %d\n", rc); 3487218792Snp return (rc); 3488218792Snp } 3489218792Snp 3490284052Snp rc = alloc_eq(sc, pi, eq); 3491218792Snp if (rc != 0) { 3492284052Snp mp_ring_free(txq->r); 3493284052Snp txq->r = NULL; 3494218792Snp return (rc); 3495218792Snp } 3496218792Snp 3497284052Snp /* Can't fail after this point. */ 3498284052Snp 3499284052Snp TASK_INIT(&txq->tx_reclaim_task, 0, tx_reclaim, eq); 3500284052Snp txq->ifp = pi->ifp; 3501284052Snp txq->gl = sglist_alloc(TX_SGL_SEGS, M_WAITOK); 3502284052Snp txq->cpl_ctrl0 = htobe32(V_TXPKT_OPCODE(CPL_TX_PKT) | 3503284052Snp V_TXPKT_INTF(pi->tx_chan) | V_TXPKT_PF(sc->pf)); 3504284052Snp txq->sdesc = malloc(eq->sidx * sizeof(struct tx_sdesc), M_CXGBE, 3505284052Snp M_ZERO | M_WAITOK); 3506284052Snp 3507218792Snp snprintf(name, sizeof(name), "%d", idx); 3508218792Snp oid = SYSCTL_ADD_NODE(&pi->ctx, children, OID_AUTO, name, CTLFLAG_RD, 3509218792Snp NULL, "tx queue"); 3510218792Snp children = SYSCTL_CHILDREN(oid); 3511218792Snp 3512222973Snp SYSCTL_ADD_UINT(&pi->ctx, children, OID_AUTO, "cntxt_id", CTLFLAG_RD, 3513222973Snp &eq->cntxt_id, 0, "SGE context id of the queue"); 3514222973Snp SYSCTL_ADD_PROC(&pi->ctx, children, OID_AUTO, "cidx", 3515222973Snp CTLTYPE_INT | CTLFLAG_RD, &eq->cidx, 0, sysctl_uint16, "I", 3516222973Snp "consumer index"); 3517222973Snp SYSCTL_ADD_PROC(&pi->ctx, children, OID_AUTO, "pidx", 3518222973Snp CTLTYPE_INT | CTLFLAG_RD, &eq->pidx, 0, sysctl_uint16, "I", 3519222973Snp "producer index"); 3520222973Snp 3521218792Snp SYSCTL_ADD_UQUAD(&pi->ctx, children, OID_AUTO, "txcsum", CTLFLAG_RD, 3522218792Snp &txq->txcsum, "# of times hardware assisted with checksum"); 3523218792Snp SYSCTL_ADD_UQUAD(&pi->ctx, children, OID_AUTO, "vlan_insertion", 3524218792Snp CTLFLAG_RD, &txq->vlan_insertion, 3525218792Snp "# of times hardware inserted 802.1Q tag"); 3526218792Snp SYSCTL_ADD_UQUAD(&pi->ctx, children, OID_AUTO, "tso_wrs", CTLFLAG_RD, 3527237819Snp &txq->tso_wrs, "# of TSO work requests"); 3528218792Snp SYSCTL_ADD_UQUAD(&pi->ctx, children, OID_AUTO, "imm_wrs", CTLFLAG_RD, 3529218792Snp &txq->imm_wrs, "# of work requests with immediate data"); 3530218792Snp SYSCTL_ADD_UQUAD(&pi->ctx, children, OID_AUTO, "sgl_wrs", CTLFLAG_RD, 3531218792Snp &txq->sgl_wrs, "# of work requests with direct SGL"); 3532218792Snp SYSCTL_ADD_UQUAD(&pi->ctx, children, OID_AUTO, "txpkt_wrs", CTLFLAG_RD, 3533218792Snp &txq->txpkt_wrs, "# of txpkt work requests (one pkt/WR)"); 3534284052Snp SYSCTL_ADD_UQUAD(&pi->ctx, children, OID_AUTO, "txpkts0_wrs", 3535284052Snp CTLFLAG_RD, &txq->txpkts0_wrs, 3536284052Snp "# of txpkts (type 0) work requests"); 3537284052Snp SYSCTL_ADD_UQUAD(&pi->ctx, children, OID_AUTO, "txpkts1_wrs", 3538284052Snp CTLFLAG_RD, &txq->txpkts1_wrs, 3539284052Snp "# of txpkts (type 1) work requests"); 3540284052Snp SYSCTL_ADD_UQUAD(&pi->ctx, children, OID_AUTO, "txpkts0_pkts", 3541284052Snp CTLFLAG_RD, &txq->txpkts0_pkts, 3542284052Snp "# of frames tx'd using type0 txpkts work requests"); 3543284052Snp SYSCTL_ADD_UQUAD(&pi->ctx, children, OID_AUTO, "txpkts1_pkts", 3544284052Snp CTLFLAG_RD, &txq->txpkts1_pkts, 3545284052Snp "# of frames tx'd using type1 txpkts work requests"); 3546218792Snp 3547284052Snp SYSCTL_ADD_COUNTER_U64(&pi->ctx, children, OID_AUTO, "r_enqueues", 3548284052Snp CTLFLAG_RD, &txq->r->enqueues, 3549284052Snp "# of enqueues to the mp_ring for this queue"); 3550284052Snp SYSCTL_ADD_COUNTER_U64(&pi->ctx, children, OID_AUTO, "r_drops", 3551284052Snp CTLFLAG_RD, &txq->r->drops, 3552284052Snp "# of drops in the mp_ring for this queue"); 3553284052Snp SYSCTL_ADD_COUNTER_U64(&pi->ctx, children, OID_AUTO, "r_starts", 3554284052Snp CTLFLAG_RD, &txq->r->starts, 3555284052Snp "# of normal consumer starts in the mp_ring for this queue"); 3556284052Snp SYSCTL_ADD_COUNTER_U64(&pi->ctx, children, OID_AUTO, "r_stalls", 3557284052Snp CTLFLAG_RD, &txq->r->stalls, 3558284052Snp "# of consumer stalls in the mp_ring for this queue"); 3559284052Snp SYSCTL_ADD_COUNTER_U64(&pi->ctx, children, OID_AUTO, "r_restarts", 3560284052Snp CTLFLAG_RD, &txq->r->restarts, 3561284052Snp "# of consumer restarts in the mp_ring for this queue"); 3562284052Snp SYSCTL_ADD_COUNTER_U64(&pi->ctx, children, OID_AUTO, "r_abdications", 3563284052Snp CTLFLAG_RD, &txq->r->abdications, 3564284052Snp "# of consumer abdications in the mp_ring for this queue"); 3565218792Snp 3566284052Snp return (0); 3567218792Snp} 3568218792Snp 3569218792Snpstatic int 3570218792Snpfree_txq(struct port_info *pi, struct sge_txq *txq) 3571218792Snp{ 3572218792Snp int rc; 3573218792Snp struct adapter *sc = pi->adapter; 3574218792Snp struct sge_eq *eq = &txq->eq; 3575218792Snp 3576228561Snp rc = free_eq(sc, eq); 3577228561Snp if (rc) 3578228561Snp return (rc); 3579220649Snp 3580284052Snp sglist_free(txq->gl); 3581220873Snp free(txq->sdesc, M_CXGBE); 3582284052Snp mp_ring_free(txq->r); 3583218792Snp 3584218792Snp bzero(txq, sizeof(*txq)); 3585218792Snp return (0); 3586218792Snp} 3587218792Snp 3588218792Snpstatic void 3589218792Snponeseg_dma_callback(void *arg, bus_dma_segment_t *segs, int nseg, int error) 3590218792Snp{ 3591218792Snp bus_addr_t *ba = arg; 3592218792Snp 3593218792Snp KASSERT(nseg == 1, 3594218792Snp ("%s meant for single segment mappings only.", __func__)); 3595218792Snp 3596218792Snp *ba = error ? 0 : segs->ds_addr; 3597218792Snp} 3598218792Snp 3599218792Snpstatic inline void 3600218792Snpring_fl_db(struct adapter *sc, struct sge_fl *fl) 3601218792Snp{ 3602270297Snp uint32_t n, v; 3603218792Snp 3604270297Snp n = IDXDIFF(fl->pidx / 8, fl->dbidx, fl->sidx); 3605270297Snp MPASS(n > 0); 3606218792Snp 3607218792Snp wmb(); 3608270297Snp v = fl->dbval | V_PIDX(n); 3609270297Snp if (fl->udb) 3610270297Snp *fl->udb = htole32(v); 3611270297Snp else 3612270297Snp t4_write_reg(sc, MYPF_REG(A_SGE_PF_KDOORBELL), v); 3613270297Snp IDXINCR(fl->dbidx, n, fl->sidx); 3614218792Snp} 3615218792Snp 3616220905Snp/* 3617270297Snp * Fills up the freelist by allocating upto 'n' buffers. Buffers that are 3618270297Snp * recycled do not count towards this allocation budget. 3619228561Snp * 3620270297Snp * Returns non-zero to indicate that this freelist should be added to the list 3621270297Snp * of starving freelists. 3622220905Snp */ 3623228561Snpstatic int 3624270297Snprefill_fl(struct adapter *sc, struct sge_fl *fl, int n) 3625218792Snp{ 3626270297Snp __be64 *d; 3627270297Snp struct fl_sdesc *sd; 3628265425Snp uintptr_t pa; 3629218792Snp caddr_t cl; 3630270297Snp struct cluster_layout *cll; 3631270297Snp struct sw_zone_info *swz; 3632265425Snp struct cluster_metadata *clm; 3633270297Snp uint16_t max_pidx; 3634270297Snp uint16_t hw_cidx = fl->hw_cidx; /* stable snapshot */ 3635218792Snp 3636218792Snp FL_LOCK_ASSERT_OWNED(fl); 3637218792Snp 3638270297Snp /* 3639270297Snp * We always stop at the begining of the hardware descriptor that's just 3640270297Snp * before the one with the hw cidx. This is to avoid hw pidx = hw cidx, 3641270297Snp * which would mean an empty freelist to the chip. 3642270297Snp */ 3643270297Snp max_pidx = __predict_false(hw_cidx == 0) ? fl->sidx - 1 : hw_cidx - 1; 3644270297Snp if (fl->pidx == max_pidx * 8) 3645270297Snp return (0); 3646218792Snp 3647270297Snp d = &fl->desc[fl->pidx]; 3648270297Snp sd = &fl->sdesc[fl->pidx]; 3649270297Snp cll = &fl->cll_def; /* default layout */ 3650270297Snp swz = &sc->sge.sw_zone_info[cll->zidx]; 3651218792Snp 3652270297Snp while (n > 0) { 3653270297Snp 3654218792Snp if (sd->cl != NULL) { 3655218792Snp 3656269356Snp if (sd->nmbuf == 0) { 3657255050Snp /* 3658265425Snp * Fast recycle without involving any atomics on 3659265425Snp * the cluster's metadata (if the cluster has 3660265425Snp * metadata). This happens when all frames 3661265425Snp * received in the cluster were small enough to 3662265425Snp * fit within a single mbuf each. 3663255050Snp */ 3664265425Snp fl->cl_fast_recycled++; 3665267694Snp#ifdef INVARIANTS 3666267694Snp clm = cl_metadata(sc, fl, &sd->cll, sd->cl); 3667267694Snp if (clm != NULL) 3668267694Snp MPASS(clm->refcount == 1); 3669267694Snp#endif 3670265425Snp goto recycled_fast; 3671255050Snp } 3672218792Snp 3673218792Snp /* 3674265425Snp * Cluster is guaranteed to have metadata. Clusters 3675265425Snp * without metadata always take the fast recycle path 3676265425Snp * when they're recycled. 3677218792Snp */ 3678265425Snp clm = cl_metadata(sc, fl, &sd->cll, sd->cl); 3679265425Snp MPASS(clm != NULL); 3680265425Snp 3681265425Snp if (atomic_fetchadd_int(&clm->refcount, -1) == 1) { 3682265425Snp fl->cl_recycled++; 3683269356Snp counter_u64_add(extfree_rels, 1); 3684265425Snp goto recycled; 3685218792Snp } 3686265425Snp sd->cl = NULL; /* gave up my reference */ 3687218792Snp } 3688265425Snp MPASS(sd->cl == NULL); 3689265425Snpalloc: 3690265425Snp cl = uma_zalloc(swz->zone, M_NOWAIT); 3691265425Snp if (__predict_false(cl == NULL)) { 3692265425Snp if (cll == &fl->cll_alt || fl->cll_alt.zidx == -1 || 3693265425Snp fl->cll_def.zidx == fl->cll_alt.zidx) 3694265425Snp break; 3695218792Snp 3696265425Snp /* fall back to the safe zone */ 3697265425Snp cll = &fl->cll_alt; 3698265425Snp swz = &sc->sge.sw_zone_info[cll->zidx]; 3699265425Snp goto alloc; 3700255050Snp } 3701265425Snp fl->cl_allocated++; 3702270297Snp n--; 3703218792Snp 3704265425Snp pa = pmap_kextract((vm_offset_t)cl); 3705265425Snp pa += cll->region1; 3706218792Snp sd->cl = cl; 3707265425Snp sd->cll = *cll; 3708265425Snp *d = htobe64(pa | cll->hwidx); 3709265425Snp clm = cl_metadata(sc, fl, cll, cl); 3710265425Snp if (clm != NULL) { 3711265425Snprecycled: 3712218792Snp#ifdef INVARIANTS 3713265425Snp clm->sd = sd; 3714218792Snp#endif 3715265425Snp clm->refcount = 1; 3716265425Snp } 3717269356Snp sd->nmbuf = 0; 3718265425Snprecycled_fast: 3719265425Snp d++; 3720218792Snp sd++; 3721270297Snp if (__predict_false(++fl->pidx % 8 == 0)) { 3722270297Snp uint16_t pidx = fl->pidx / 8; 3723270297Snp 3724270297Snp if (__predict_false(pidx == fl->sidx)) { 3725270297Snp fl->pidx = 0; 3726270297Snp pidx = 0; 3727270297Snp sd = fl->sdesc; 3728270297Snp d = fl->desc; 3729270297Snp } 3730270297Snp if (pidx == max_pidx) 3731270297Snp break; 3732270297Snp 3733270297Snp if (IDXDIFF(pidx, fl->dbidx, fl->sidx) >= 4) 3734270297Snp ring_fl_db(sc, fl); 3735218792Snp } 3736218792Snp } 3737220905Snp 3738270297Snp if (fl->pidx / 8 != fl->dbidx) 3739220905Snp ring_fl_db(sc, fl); 3740228561Snp 3741228561Snp return (FL_RUNNING_LOW(fl) && !(fl->flags & FL_STARVING)); 3742218792Snp} 3743218792Snp 3744228561Snp/* 3745228561Snp * Attempt to refill all starving freelists. 3746228561Snp */ 3747228561Snpstatic void 3748228561Snprefill_sfl(void *arg) 3749228561Snp{ 3750228561Snp struct adapter *sc = arg; 3751228561Snp struct sge_fl *fl, *fl_temp; 3752228561Snp 3753228561Snp mtx_lock(&sc->sfl_lock); 3754228561Snp TAILQ_FOREACH_SAFE(fl, &sc->sfl, link, fl_temp) { 3755228561Snp FL_LOCK(fl); 3756228561Snp refill_fl(sc, fl, 64); 3757228561Snp if (FL_NOT_RUNNING_LOW(fl) || fl->flags & FL_DOOMED) { 3758228561Snp TAILQ_REMOVE(&sc->sfl, fl, link); 3759228561Snp fl->flags &= ~FL_STARVING; 3760228561Snp } 3761228561Snp FL_UNLOCK(fl); 3762228561Snp } 3763228561Snp 3764228561Snp if (!TAILQ_EMPTY(&sc->sfl)) 3765228561Snp callout_schedule(&sc->sfl_callout, hz / 5); 3766228561Snp mtx_unlock(&sc->sfl_lock); 3767228561Snp} 3768228561Snp 3769218792Snpstatic int 3770218792Snpalloc_fl_sdesc(struct sge_fl *fl) 3771218792Snp{ 3772218792Snp 3773270297Snp fl->sdesc = malloc(fl->sidx * 8 * sizeof(struct fl_sdesc), M_CXGBE, 3774218792Snp M_ZERO | M_WAITOK); 3775218792Snp 3776218792Snp return (0); 3777218792Snp} 3778218792Snp 3779218792Snpstatic void 3780255050Snpfree_fl_sdesc(struct adapter *sc, struct sge_fl *fl) 3781218792Snp{ 3782218792Snp struct fl_sdesc *sd; 3783265425Snp struct cluster_metadata *clm; 3784265425Snp struct cluster_layout *cll; 3785218792Snp int i; 3786218792Snp 3787218792Snp sd = fl->sdesc; 3788270297Snp for (i = 0; i < fl->sidx * 8; i++, sd++) { 3789265425Snp if (sd->cl == NULL) 3790265425Snp continue; 3791218792Snp 3792265425Snp cll = &sd->cll; 3793265425Snp clm = cl_metadata(sc, fl, cll, sd->cl); 3794269356Snp if (sd->nmbuf == 0) 3795265425Snp uma_zfree(sc->sge.sw_zone_info[cll->zidx].zone, sd->cl); 3796269356Snp else if (clm && atomic_fetchadd_int(&clm->refcount, -1) == 1) { 3797269356Snp uma_zfree(sc->sge.sw_zone_info[cll->zidx].zone, sd->cl); 3798269356Snp counter_u64_add(extfree_rels, 1); 3799218792Snp } 3800265425Snp sd->cl = NULL; 3801218792Snp } 3802218792Snp 3803218792Snp free(fl->sdesc, M_CXGBE); 3804218792Snp fl->sdesc = NULL; 3805218792Snp} 3806218792Snp 3807284052Snpstatic inline void 3808284052Snpget_pkt_gl(struct mbuf *m, struct sglist *gl) 3809218792Snp{ 3810284052Snp int rc; 3811218792Snp 3812284052Snp M_ASSERTPKTHDR(m); 3813218792Snp 3814284052Snp sglist_reset(gl); 3815284052Snp rc = sglist_append_mbuf(gl, m); 3816284052Snp if (__predict_false(rc != 0)) { 3817284052Snp panic("%s: mbuf %p (%d segs) was vetted earlier but now fails " 3818284052Snp "with %d.", __func__, m, mbuf_nsegs(m), rc); 3819218792Snp } 3820218792Snp 3821284052Snp KASSERT(gl->sg_nseg == mbuf_nsegs(m), 3822284052Snp ("%s: nsegs changed for mbuf %p from %d to %d", __func__, m, 3823284052Snp mbuf_nsegs(m), gl->sg_nseg)); 3824284052Snp KASSERT(gl->sg_nseg > 0 && 3825284052Snp gl->sg_nseg <= (needs_tso(m) ? TX_SGL_SEGS_TSO : TX_SGL_SEGS), 3826284052Snp ("%s: %d segments, should have been 1 <= nsegs <= %d", __func__, 3827284052Snp gl->sg_nseg, needs_tso(m) ? TX_SGL_SEGS_TSO : TX_SGL_SEGS)); 3828218792Snp} 3829218792Snp 3830284052Snp/* 3831284052Snp * len16 for a txpkt WR with a GL. Includes the firmware work request header. 3832284052Snp */ 3833284052Snpstatic inline u_int 3834284052Snptxpkt_len16(u_int nsegs, u_int tso) 3835218792Snp{ 3836284052Snp u_int n; 3837218792Snp 3838284052Snp MPASS(nsegs > 0); 3839218792Snp 3840284052Snp nsegs--; /* first segment is part of ulptx_sgl */ 3841284052Snp n = sizeof(struct fw_eth_tx_pkt_wr) + sizeof(struct cpl_tx_pkt_core) + 3842284052Snp sizeof(struct ulptx_sgl) + 8 * ((3 * nsegs) / 2 + (nsegs & 1)); 3843284052Snp if (tso) 3844284052Snp n += sizeof(struct cpl_tx_pkt_lso_core); 3845218792Snp 3846284052Snp return (howmany(n, 16)); 3847218792Snp} 3848218792Snp 3849218792Snp/* 3850284052Snp * len16 for a txpkts type 0 WR with a GL. Does not include the firmware work 3851284052Snp * request header. 3852218792Snp */ 3853284052Snpstatic inline u_int 3854284052Snptxpkts0_len16(u_int nsegs) 3855218792Snp{ 3856284052Snp u_int n; 3857218792Snp 3858284052Snp MPASS(nsegs > 0); 3859218792Snp 3860284052Snp nsegs--; /* first segment is part of ulptx_sgl */ 3861284052Snp n = sizeof(struct ulp_txpkt) + sizeof(struct ulptx_idata) + 3862284052Snp sizeof(struct cpl_tx_pkt_core) + sizeof(struct ulptx_sgl) + 3863284052Snp 8 * ((3 * nsegs) / 2 + (nsegs & 1)); 3864218792Snp 3865284052Snp return (howmany(n, 16)); 3866218792Snp} 3867218792Snp 3868218792Snp/* 3869284052Snp * len16 for a txpkts type 1 WR with a GL. Does not include the firmware work 3870284052Snp * request header. 3871218792Snp */ 3872284052Snpstatic inline u_int 3873284052Snptxpkts1_len16(void) 3874218792Snp{ 3875284052Snp u_int n; 3876218792Snp 3877284052Snp n = sizeof(struct cpl_tx_pkt_core) + sizeof(struct ulptx_sgl); 3878218792Snp 3879284052Snp return (howmany(n, 16)); 3880284052Snp} 3881218792Snp 3882284052Snpstatic inline u_int 3883284052Snpimm_payload(u_int ndesc) 3884284052Snp{ 3885284052Snp u_int n; 3886228561Snp 3887284052Snp n = ndesc * EQ_ESIZE - sizeof(struct fw_eth_tx_pkt_wr) - 3888284052Snp sizeof(struct cpl_tx_pkt_core); 3889218792Snp 3890284052Snp return (n); 3891218792Snp} 3892218792Snp 3893284052Snp/* 3894284052Snp * Write a txpkt WR for this packet to the hardware descriptors, update the 3895284052Snp * software descriptor, and advance the pidx. It is guaranteed that enough 3896284052Snp * descriptors are available. 3897284052Snp * 3898284052Snp * The return value is the # of hardware descriptors used. 3899284052Snp */ 3900284052Snpstatic u_int 3901284052Snpwrite_txpkt_wr(struct sge_txq *txq, struct fw_eth_tx_pkt_wr *wr, 3902284052Snp struct mbuf *m0, u_int available) 3903218792Snp{ 3904218792Snp struct sge_eq *eq = &txq->eq; 3905284052Snp struct tx_sdesc *txsd; 3906218792Snp struct cpl_tx_pkt_core *cpl; 3907218792Snp uint32_t ctrl; /* used in many unrelated places */ 3908218792Snp uint64_t ctrl1; 3909284052Snp int len16, ndesc, pktlen, nsegs; 3910218792Snp caddr_t dst; 3911218792Snp 3912218792Snp TXQ_LOCK_ASSERT_OWNED(txq); 3913284052Snp M_ASSERTPKTHDR(m0); 3914284052Snp MPASS(available > 0 && available < eq->sidx); 3915218792Snp 3916284052Snp len16 = mbuf_len16(m0); 3917284052Snp nsegs = mbuf_nsegs(m0); 3918284052Snp pktlen = m0->m_pkthdr.len; 3919218792Snp ctrl = sizeof(struct cpl_tx_pkt_core); 3920284052Snp if (needs_tso(m0)) 3921237436Snp ctrl += sizeof(struct cpl_tx_pkt_lso_core); 3922284052Snp else if (pktlen <= imm_payload(2) && available >= 2) { 3923284052Snp /* Immediate data. Recalculate len16 and set nsegs to 0. */ 3924219286Snp ctrl += pktlen; 3925284052Snp len16 = howmany(sizeof(struct fw_eth_tx_pkt_wr) + 3926284052Snp sizeof(struct cpl_tx_pkt_core) + pktlen, 16); 3927284052Snp nsegs = 0; 3928218792Snp } 3929284052Snp ndesc = howmany(len16, EQ_ESIZE / 16); 3930284052Snp MPASS(ndesc <= available); 3931218792Snp 3932218792Snp /* Firmware work request header */ 3933284052Snp MPASS(wr == (void *)&eq->desc[eq->pidx]); 3934218792Snp wr->op_immdlen = htobe32(V_FW_WR_OP(FW_ETH_TX_PKT_WR) | 3935228561Snp V_FW_ETH_TX_PKT_WR_IMMDLEN(ctrl)); 3936220643Snp 3937284052Snp ctrl = V_FW_WR_LEN16(len16); 3938218792Snp wr->equiq_to_len16 = htobe32(ctrl); 3939218792Snp wr->r3 = 0; 3940218792Snp 3941284052Snp if (needs_tso(m0)) { 3942237436Snp struct cpl_tx_pkt_lso_core *lso = (void *)(wr + 1); 3943218792Snp 3944284052Snp KASSERT(m0->m_pkthdr.l2hlen > 0 && m0->m_pkthdr.l3hlen > 0 && 3945284052Snp m0->m_pkthdr.l4hlen > 0, 3946284052Snp ("%s: mbuf %p needs TSO but missing header lengths", 3947284052Snp __func__, m0)); 3948284052Snp 3949218792Snp ctrl = V_LSO_OPCODE(CPL_TX_PKT_LSO) | F_LSO_FIRST_SLICE | 3950284052Snp F_LSO_LAST_SLICE | V_LSO_IPHDR_LEN(m0->m_pkthdr.l3hlen >> 2) 3951284052Snp | V_LSO_TCPHDR_LEN(m0->m_pkthdr.l4hlen >> 2); 3952284052Snp if (m0->m_pkthdr.l2hlen == sizeof(struct ether_vlan_header)) 3953218792Snp ctrl |= V_LSO_ETHHDR_LEN(1); 3954284052Snp if (m0->m_pkthdr.l3hlen == sizeof(struct ip6_hdr)) 3955237819Snp ctrl |= F_LSO_IPV6; 3956237819Snp 3957218792Snp lso->lso_ctrl = htobe32(ctrl); 3958218792Snp lso->ipid_ofst = htobe16(0); 3959284052Snp lso->mss = htobe16(m0->m_pkthdr.tso_segsz); 3960218792Snp lso->seqno_offset = htobe32(0); 3961219286Snp lso->len = htobe32(pktlen); 3962218792Snp 3963218792Snp cpl = (void *)(lso + 1); 3964218792Snp 3965218792Snp txq->tso_wrs++; 3966218792Snp } else 3967218792Snp cpl = (void *)(wr + 1); 3968218792Snp 3969218792Snp /* Checksum offload */ 3970218792Snp ctrl1 = 0; 3971284052Snp if (needs_l3_csum(m0) == 0) 3972218792Snp ctrl1 |= F_TXPKT_IPCSUM_DIS; 3973284052Snp if (needs_l4_csum(m0) == 0) 3974218792Snp ctrl1 |= F_TXPKT_L4CSUM_DIS; 3975284052Snp if (m0->m_pkthdr.csum_flags & (CSUM_IP | CSUM_TCP | CSUM_UDP | 3976247062Snp CSUM_UDP_IPV6 | CSUM_TCP_IPV6 | CSUM_TSO)) 3977218792Snp txq->txcsum++; /* some hardware assistance provided */ 3978218792Snp 3979218792Snp /* VLAN tag insertion */ 3980284052Snp if (needs_vlan_insertion(m0)) { 3981284052Snp ctrl1 |= F_TXPKT_VLAN_VLD | V_TXPKT_VLAN(m0->m_pkthdr.ether_vtag); 3982218792Snp txq->vlan_insertion++; 3983218792Snp } 3984218792Snp 3985218792Snp /* CPL header */ 3986284052Snp cpl->ctrl0 = txq->cpl_ctrl0; 3987218792Snp cpl->pack = 0; 3988219286Snp cpl->len = htobe16(pktlen); 3989218792Snp cpl->ctrl1 = htobe64(ctrl1); 3990218792Snp 3991218792Snp /* SGL */ 3992218792Snp dst = (void *)(cpl + 1); 3993284052Snp if (nsegs > 0) { 3994284052Snp 3995284052Snp write_gl_to_txd(txq, m0, &dst, eq->sidx - ndesc < eq->pidx); 3996218792Snp txq->sgl_wrs++; 3997218792Snp } else { 3998284052Snp struct mbuf *m; 3999284052Snp 4000284052Snp for (m = m0; m != NULL; m = m->m_next) { 4001218792Snp copy_to_txd(eq, mtod(m, caddr_t), &dst, m->m_len); 4002219286Snp#ifdef INVARIANTS 4003219286Snp pktlen -= m->m_len; 4004219286Snp#endif 4005218792Snp } 4006219286Snp#ifdef INVARIANTS 4007219286Snp KASSERT(pktlen == 0, ("%s: %d bytes left.", __func__, pktlen)); 4008219286Snp#endif 4009284052Snp txq->imm_wrs++; 4010218792Snp } 4011218792Snp 4012218792Snp txq->txpkt_wrs++; 4013284052Snp 4014284052Snp txsd = &txq->sdesc[eq->pidx]; 4015284052Snp txsd->m = m0; 4016284052Snp txsd->desc_used = ndesc; 4017284052Snp 4018284052Snp return (ndesc); 4019218792Snp} 4020218792Snp 4021218792Snpstatic int 4022284052Snptry_txpkts(struct mbuf *m, struct mbuf *n, struct txpkts *txp, u_int available) 4023218792Snp{ 4024284052Snp u_int needed, nsegs1, nsegs2, l1, l2; 4025218792Snp 4026284052Snp if (cannot_use_txpkts(m) || cannot_use_txpkts(n)) 4027284052Snp return (1); 4028218792Snp 4029284052Snp nsegs1 = mbuf_nsegs(m); 4030284052Snp nsegs2 = mbuf_nsegs(n); 4031284052Snp if (nsegs1 + nsegs2 == 2) { 4032284052Snp txp->wr_type = 1; 4033284052Snp l1 = l2 = txpkts1_len16(); 4034284052Snp } else { 4035284052Snp txp->wr_type = 0; 4036284052Snp l1 = txpkts0_len16(nsegs1); 4037284052Snp l2 = txpkts0_len16(nsegs2); 4038284052Snp } 4039284052Snp txp->len16 = howmany(sizeof(struct fw_eth_tx_pkts_wr), 16) + l1 + l2; 4040284052Snp needed = howmany(txp->len16, EQ_ESIZE / 16); 4041284052Snp if (needed > SGE_MAX_WR_NDESC || needed > available) 4042284052Snp return (1); 4043228561Snp 4044284052Snp txp->plen = m->m_pkthdr.len + n->m_pkthdr.len; 4045284052Snp if (txp->plen > 65535) 4046284052Snp return (1); 4047218792Snp 4048284052Snp txp->npkt = 2; 4049284052Snp set_mbuf_len16(m, l1); 4050284052Snp set_mbuf_len16(n, l2); 4051218792Snp 4052284052Snp return (0); 4053284052Snp} 4054218792Snp 4055284052Snpstatic int 4056284052Snpadd_to_txpkts(struct mbuf *m, struct txpkts *txp, u_int available) 4057284052Snp{ 4058284052Snp u_int plen, len16, needed, nsegs; 4059218792Snp 4060284052Snp MPASS(txp->wr_type == 0 || txp->wr_type == 1); 4061218792Snp 4062284052Snp nsegs = mbuf_nsegs(m); 4063284052Snp if (needs_tso(m) || (txp->wr_type == 1 && nsegs != 1)) 4064284052Snp return (1); 4065218792Snp 4066284052Snp plen = txp->plen + m->m_pkthdr.len; 4067284052Snp if (plen > 65535) 4068284052Snp return (1); 4069218792Snp 4070284052Snp if (txp->wr_type == 0) 4071284052Snp len16 = txpkts0_len16(nsegs); 4072284052Snp else 4073284052Snp len16 = txpkts1_len16(); 4074284052Snp needed = howmany(txp->len16 + len16, EQ_ESIZE / 16); 4075284052Snp if (needed > SGE_MAX_WR_NDESC || needed > available) 4076284052Snp return (1); 4077218792Snp 4078284052Snp txp->npkt++; 4079284052Snp txp->plen = plen; 4080284052Snp txp->len16 += len16; 4081284052Snp set_mbuf_len16(m, len16); 4082218792Snp 4083218792Snp return (0); 4084218792Snp} 4085218792Snp 4086218792Snp/* 4087284052Snp * Write a txpkts WR for the packets in txp to the hardware descriptors, update 4088284052Snp * the software descriptor, and advance the pidx. It is guaranteed that enough 4089284052Snp * descriptors are available. 4090284052Snp * 4091284052Snp * The return value is the # of hardware descriptors used. 4092218792Snp */ 4093284052Snpstatic u_int 4094284052Snpwrite_txpkts_wr(struct sge_txq *txq, struct fw_eth_tx_pkts_wr *wr, 4095284052Snp struct mbuf *m0, const struct txpkts *txp, u_int available) 4096218792Snp{ 4097218792Snp struct sge_eq *eq = &txq->eq; 4098218792Snp struct tx_sdesc *txsd; 4099284052Snp struct cpl_tx_pkt_core *cpl; 4100218792Snp uint32_t ctrl; 4101284052Snp uint64_t ctrl1; 4102284052Snp int ndesc, checkwrap; 4103284052Snp struct mbuf *m; 4104284052Snp void *flitp; 4105218792Snp 4106218792Snp TXQ_LOCK_ASSERT_OWNED(txq); 4107284052Snp MPASS(txp->npkt > 0); 4108284052Snp MPASS(txp->plen < 65536); 4109284052Snp MPASS(m0 != NULL); 4110284052Snp MPASS(m0->m_nextpkt != NULL); 4111284052Snp MPASS(txp->len16 <= howmany(SGE_MAX_WR_LEN, 16)); 4112284052Snp MPASS(available > 0 && available < eq->sidx); 4113218792Snp 4114284052Snp ndesc = howmany(txp->len16, EQ_ESIZE / 16); 4115284052Snp MPASS(ndesc <= available); 4116218792Snp 4117284052Snp MPASS(wr == (void *)&eq->desc[eq->pidx]); 4118228561Snp wr->op_pkd = htobe32(V_FW_WR_OP(FW_ETH_TX_PKTS_WR)); 4119284052Snp ctrl = V_FW_WR_LEN16(txp->len16); 4120218792Snp wr->equiq_to_len16 = htobe32(ctrl); 4121284052Snp wr->plen = htobe16(txp->plen); 4122284052Snp wr->npkt = txp->npkt; 4123284052Snp wr->r3 = 0; 4124284052Snp wr->type = txp->wr_type; 4125284052Snp flitp = wr + 1; 4126218792Snp 4127284052Snp /* 4128284052Snp * At this point we are 16B into a hardware descriptor. If checkwrap is 4129284052Snp * set then we know the WR is going to wrap around somewhere. We'll 4130284052Snp * check for that at appropriate points. 4131284052Snp */ 4132284052Snp checkwrap = eq->sidx - ndesc < eq->pidx; 4133284052Snp for (m = m0; m != NULL; m = m->m_nextpkt) { 4134284052Snp if (txp->wr_type == 0) { 4135284052Snp struct ulp_txpkt *ulpmc; 4136284052Snp struct ulptx_idata *ulpsc; 4137218792Snp 4138284052Snp /* ULP master command */ 4139284052Snp ulpmc = flitp; 4140284052Snp ulpmc->cmd_dest = htobe32(V_ULPTX_CMD(ULP_TX_PKT) | 4141284052Snp V_ULP_TXPKT_DEST(0) | V_ULP_TXPKT_FID(eq->iqid)); 4142284052Snp ulpmc->len = htobe32(mbuf_len16(m)); 4143218792Snp 4144284052Snp /* ULP subcommand */ 4145284052Snp ulpsc = (void *)(ulpmc + 1); 4146284052Snp ulpsc->cmd_more = htobe32(V_ULPTX_CMD(ULP_TX_SC_IMM) | 4147284052Snp F_ULP_TX_SC_MORE); 4148284052Snp ulpsc->len = htobe32(sizeof(struct cpl_tx_pkt_core)); 4149218792Snp 4150284052Snp cpl = (void *)(ulpsc + 1); 4151284052Snp if (checkwrap && 4152284052Snp (uintptr_t)cpl == (uintptr_t)&eq->desc[eq->sidx]) 4153284052Snp cpl = (void *)&eq->desc[0]; 4154284052Snp txq->txpkts0_pkts += txp->npkt; 4155284052Snp txq->txpkts0_wrs++; 4156284052Snp } else { 4157284052Snp cpl = flitp; 4158284052Snp txq->txpkts1_pkts += txp->npkt; 4159284052Snp txq->txpkts1_wrs++; 4160284052Snp } 4161218792Snp 4162284052Snp /* Checksum offload */ 4163284052Snp ctrl1 = 0; 4164284052Snp if (needs_l3_csum(m) == 0) 4165284052Snp ctrl1 |= F_TXPKT_IPCSUM_DIS; 4166284052Snp if (needs_l4_csum(m) == 0) 4167284052Snp ctrl1 |= F_TXPKT_L4CSUM_DIS; 4168284052Snp if (m->m_pkthdr.csum_flags & (CSUM_IP | CSUM_TCP | CSUM_UDP | 4169284052Snp CSUM_UDP_IPV6 | CSUM_TCP_IPV6 | CSUM_TSO)) 4170284052Snp txq->txcsum++; /* some hardware assistance provided */ 4171218792Snp 4172284052Snp /* VLAN tag insertion */ 4173284052Snp if (needs_vlan_insertion(m)) { 4174284052Snp ctrl1 |= F_TXPKT_VLAN_VLD | 4175284052Snp V_TXPKT_VLAN(m->m_pkthdr.ether_vtag); 4176284052Snp txq->vlan_insertion++; 4177284052Snp } 4178218792Snp 4179284052Snp /* CPL header */ 4180284052Snp cpl->ctrl0 = txq->cpl_ctrl0; 4181284052Snp cpl->pack = 0; 4182284052Snp cpl->len = htobe16(m->m_pkthdr.len); 4183284052Snp cpl->ctrl1 = htobe64(ctrl1); 4184218792Snp 4185284052Snp flitp = cpl + 1; 4186284052Snp if (checkwrap && 4187284052Snp (uintptr_t)flitp == (uintptr_t)&eq->desc[eq->sidx]) 4188284052Snp flitp = (void *)&eq->desc[0]; 4189218792Snp 4190284052Snp write_gl_to_txd(txq, m, (caddr_t *)(&flitp), checkwrap); 4191218792Snp 4192218792Snp } 4193218792Snp 4194284052Snp txsd = &txq->sdesc[eq->pidx]; 4195284052Snp txsd->m = m0; 4196284052Snp txsd->desc_used = ndesc; 4197218792Snp 4198284052Snp return (ndesc); 4199218792Snp} 4200218792Snp 4201218792Snp/* 4202218792Snp * If the SGL ends on an address that is not 16 byte aligned, this function will 4203284052Snp * add a 0 filled flit at the end. 4204218792Snp */ 4205284052Snpstatic void 4206284052Snpwrite_gl_to_txd(struct sge_txq *txq, struct mbuf *m, caddr_t *to, int checkwrap) 4207218792Snp{ 4208284052Snp struct sge_eq *eq = &txq->eq; 4209284052Snp struct sglist *gl = txq->gl; 4210284052Snp struct sglist_seg *seg; 4211284052Snp __be64 *flitp, *wrap; 4212218792Snp struct ulptx_sgl *usgl; 4213284052Snp int i, nflits, nsegs; 4214218792Snp 4215218792Snp KASSERT(((uintptr_t)(*to) & 0xf) == 0, 4216218792Snp ("%s: SGL must start at a 16 byte boundary: %p", __func__, *to)); 4217284052Snp MPASS((uintptr_t)(*to) >= (uintptr_t)&eq->desc[0]); 4218284052Snp MPASS((uintptr_t)(*to) < (uintptr_t)&eq->desc[eq->sidx]); 4219218792Snp 4220284052Snp get_pkt_gl(m, gl); 4221284052Snp nsegs = gl->sg_nseg; 4222284052Snp MPASS(nsegs > 0); 4223284052Snp 4224284052Snp nflits = (3 * (nsegs - 1)) / 2 + ((nsegs - 1) & 1) + 2; 4225218792Snp flitp = (__be64 *)(*to); 4226284052Snp wrap = (__be64 *)(&eq->desc[eq->sidx]); 4227284052Snp seg = &gl->sg_segs[0]; 4228218792Snp usgl = (void *)flitp; 4229218792Snp 4230218792Snp /* 4231218792Snp * We start at a 16 byte boundary somewhere inside the tx descriptor 4232218792Snp * ring, so we're at least 16 bytes away from the status page. There is 4233218792Snp * no chance of a wrap around in the middle of usgl (which is 16 bytes). 4234218792Snp */ 4235218792Snp 4236218792Snp usgl->cmd_nsge = htobe32(V_ULPTX_CMD(ULP_TX_SC_DSGL) | 4237284052Snp V_ULPTX_NSGE(nsegs)); 4238284052Snp usgl->len0 = htobe32(seg->ss_len); 4239284052Snp usgl->addr0 = htobe64(seg->ss_paddr); 4240218792Snp seg++; 4241218792Snp 4242284052Snp if (checkwrap == 0 || (uintptr_t)(flitp + nflits) <= (uintptr_t)wrap) { 4243218792Snp 4244218792Snp /* Won't wrap around at all */ 4245218792Snp 4246284052Snp for (i = 0; i < nsegs - 1; i++, seg++) { 4247284052Snp usgl->sge[i / 2].len[i & 1] = htobe32(seg->ss_len); 4248284052Snp usgl->sge[i / 2].addr[i & 1] = htobe64(seg->ss_paddr); 4249218792Snp } 4250218792Snp if (i & 1) 4251218792Snp usgl->sge[i / 2].len[1] = htobe32(0); 4252284052Snp flitp += nflits; 4253218792Snp } else { 4254218792Snp 4255218792Snp /* Will wrap somewhere in the rest of the SGL */ 4256218792Snp 4257218792Snp /* 2 flits already written, write the rest flit by flit */ 4258218792Snp flitp = (void *)(usgl + 1); 4259284052Snp for (i = 0; i < nflits - 2; i++) { 4260284052Snp if (flitp == wrap) 4261218792Snp flitp = (void *)eq->desc; 4262284052Snp *flitp++ = get_flit(seg, nsegs - 1, i); 4263218792Snp } 4264218792Snp } 4265218792Snp 4266284052Snp if (nflits & 1) { 4267284052Snp MPASS(((uintptr_t)flitp) & 0xf); 4268284052Snp *flitp++ = 0; 4269284052Snp } 4270218792Snp 4271284052Snp MPASS((((uintptr_t)flitp) & 0xf) == 0); 4272284052Snp if (__predict_false(flitp == wrap)) 4273218792Snp *to = (void *)eq->desc; 4274218792Snp else 4275284052Snp *to = (void *)flitp; 4276218792Snp} 4277218792Snp 4278218792Snpstatic inline void 4279218792Snpcopy_to_txd(struct sge_eq *eq, caddr_t from, caddr_t *to, int len) 4280218792Snp{ 4281284052Snp 4282284052Snp MPASS((uintptr_t)(*to) >= (uintptr_t)&eq->desc[0]); 4283284052Snp MPASS((uintptr_t)(*to) < (uintptr_t)&eq->desc[eq->sidx]); 4284284052Snp 4285284052Snp if (__predict_true((uintptr_t)(*to) + len <= 4286284052Snp (uintptr_t)&eq->desc[eq->sidx])) { 4287218792Snp bcopy(from, *to, len); 4288218792Snp (*to) += len; 4289218792Snp } else { 4290284052Snp int portion = (uintptr_t)&eq->desc[eq->sidx] - (uintptr_t)(*to); 4291218792Snp 4292218792Snp bcopy(from, *to, portion); 4293218792Snp from += portion; 4294218792Snp portion = len - portion; /* remaining */ 4295218792Snp bcopy(from, (void *)eq->desc, portion); 4296218792Snp (*to) = (caddr_t)eq->desc + portion; 4297218792Snp } 4298218792Snp} 4299218792Snp 4300218792Snpstatic inline void 4301284052Snpring_eq_db(struct adapter *sc, struct sge_eq *eq, u_int n) 4302218792Snp{ 4303284052Snp u_int db; 4304248925Snp 4305284052Snp MPASS(n > 0); 4306284052Snp 4307248925Snp db = eq->doorbells; 4308284052Snp if (n > 1) 4309249392Snp clrbit(&db, DOORBELL_WCWR); 4310218792Snp wmb(); 4311248925Snp 4312248925Snp switch (ffs(db) - 1) { 4313248925Snp case DOORBELL_UDB: 4314284052Snp *eq->udb = htole32(V_QID(eq->udb_qid) | V_PIDX(n)); 4315284052Snp break; 4316248925Snp 4317249392Snp case DOORBELL_WCWR: { 4318248925Snp volatile uint64_t *dst, *src; 4319248925Snp int i; 4320248925Snp 4321248925Snp /* 4322248925Snp * Queues whose 128B doorbell segment fits in the page do not 4323248925Snp * use relative qid (udb_qid is always 0). Only queues with 4324249392Snp * doorbell segments can do WCWR. 4325248925Snp */ 4326284052Snp KASSERT(eq->udb_qid == 0 && n == 1, 4327248925Snp ("%s: inappropriate doorbell (0x%x, %d, %d) for eq %p", 4328284052Snp __func__, eq->doorbells, n, eq->dbidx, eq)); 4329248925Snp 4330248925Snp dst = (volatile void *)((uintptr_t)eq->udb + UDBS_WR_OFFSET - 4331248925Snp UDBS_DB_OFFSET); 4332284052Snp i = eq->dbidx; 4333248925Snp src = (void *)&eq->desc[i]; 4334248925Snp while (src != (void *)&eq->desc[i + 1]) 4335248925Snp *dst++ = *src++; 4336248925Snp wmb(); 4337284052Snp break; 4338248925Snp } 4339248925Snp 4340248925Snp case DOORBELL_UDBWC: 4341284052Snp *eq->udb = htole32(V_QID(eq->udb_qid) | V_PIDX(n)); 4342248925Snp wmb(); 4343284052Snp break; 4344248925Snp 4345248925Snp case DOORBELL_KDB: 4346248925Snp t4_write_reg(sc, MYPF_REG(A_SGE_PF_KDOORBELL), 4347284052Snp V_QID(eq->cntxt_id) | V_PIDX(n)); 4348284052Snp break; 4349248925Snp } 4350284052Snp 4351284052Snp IDXINCR(eq->dbidx, n, eq->sidx); 4352218792Snp} 4353218792Snp 4354284052Snpstatic inline u_int 4355284052Snpreclaimable_tx_desc(struct sge_eq *eq) 4356218792Snp{ 4357284052Snp uint16_t hw_cidx; 4358218792Snp 4359284052Snp hw_cidx = read_hw_cidx(eq); 4360284052Snp return (IDXDIFF(hw_cidx, eq->cidx, eq->sidx)); 4361284052Snp} 4362218792Snp 4363284052Snpstatic inline u_int 4364284052Snptotal_available_tx_desc(struct sge_eq *eq) 4365284052Snp{ 4366284052Snp uint16_t hw_cidx, pidx; 4367284052Snp 4368284052Snp hw_cidx = read_hw_cidx(eq); 4369284052Snp pidx = eq->pidx; 4370284052Snp 4371284052Snp if (pidx == hw_cidx) 4372284052Snp return (eq->sidx - 1); 4373218792Snp else 4374284052Snp return (IDXDIFF(hw_cidx, pidx, eq->sidx) - 1); 4375219292Snp} 4376218792Snp 4377284052Snpstatic inline uint16_t 4378284052Snpread_hw_cidx(struct sge_eq *eq) 4379284052Snp{ 4380284052Snp struct sge_qstat *spg = (void *)&eq->desc[eq->sidx]; 4381284052Snp uint16_t cidx = spg->cidx; /* stable snapshot */ 4382284052Snp 4383284052Snp return (be16toh(cidx)); 4384284052Snp} 4385284052Snp 4386219292Snp/* 4387284052Snp * Reclaim 'n' descriptors approximately. 4388219292Snp */ 4389284052Snpstatic u_int 4390284052Snpreclaim_tx_descs(struct sge_txq *txq, u_int n) 4391219292Snp{ 4392219292Snp struct tx_sdesc *txsd; 4393220873Snp struct sge_eq *eq = &txq->eq; 4394284052Snp u_int can_reclaim, reclaimed; 4395218792Snp 4396228561Snp TXQ_LOCK_ASSERT_OWNED(txq); 4397284052Snp MPASS(n > 0); 4398218792Snp 4399284052Snp reclaimed = 0; 4400284052Snp can_reclaim = reclaimable_tx_desc(eq); 4401284052Snp while (can_reclaim && reclaimed < n) { 4402218792Snp int ndesc; 4403284052Snp struct mbuf *m, *nextpkt; 4404218792Snp 4405220873Snp txsd = &txq->sdesc[eq->cidx]; 4406218792Snp ndesc = txsd->desc_used; 4407218792Snp 4408218792Snp /* Firmware doesn't return "partial" credits. */ 4409218792Snp KASSERT(can_reclaim >= ndesc, 4410218792Snp ("%s: unexpected number of credits: %d, %d", 4411218792Snp __func__, can_reclaim, ndesc)); 4412218792Snp 4413284052Snp for (m = txsd->m; m != NULL; m = nextpkt) { 4414284052Snp nextpkt = m->m_nextpkt; 4415284052Snp m->m_nextpkt = NULL; 4416284052Snp m_freem(m); 4417284052Snp } 4418218792Snp reclaimed += ndesc; 4419219292Snp can_reclaim -= ndesc; 4420284052Snp IDXINCR(eq->cidx, ndesc, eq->sidx); 4421219292Snp } 4422218792Snp 4423218792Snp return (reclaimed); 4424218792Snp} 4425218792Snp 4426218792Snpstatic void 4427284052Snptx_reclaim(void *arg, int n) 4428218792Snp{ 4429284052Snp struct sge_txq *txq = arg; 4430284052Snp struct sge_eq *eq = &txq->eq; 4431218792Snp 4432284052Snp do { 4433284052Snp if (TXQ_TRYLOCK(txq) == 0) 4434284052Snp break; 4435284052Snp n = reclaim_tx_descs(txq, 32); 4436284052Snp if (eq->cidx == eq->pidx) 4437284052Snp eq->equeqidx = eq->pidx; 4438284052Snp TXQ_UNLOCK(txq); 4439284052Snp } while (n > 0); 4440218792Snp} 4441218792Snp 4442218792Snpstatic __be64 4443284052Snpget_flit(struct sglist_seg *segs, int nsegs, int idx) 4444218792Snp{ 4445218792Snp int i = (idx / 3) * 2; 4446218792Snp 4447218792Snp switch (idx % 3) { 4448218792Snp case 0: { 4449218792Snp __be64 rc; 4450218792Snp 4451284052Snp rc = htobe32(segs[i].ss_len); 4452218792Snp if (i + 1 < nsegs) 4453284052Snp rc |= (uint64_t)htobe32(segs[i + 1].ss_len) << 32; 4454218792Snp 4455218792Snp return (rc); 4456218792Snp } 4457218792Snp case 1: 4458284052Snp return (htobe64(segs[i].ss_paddr)); 4459218792Snp case 2: 4460284052Snp return (htobe64(segs[i + 1].ss_paddr)); 4461218792Snp } 4462218792Snp 4463218792Snp return (0); 4464218792Snp} 4465218792Snp 4466218792Snpstatic void 4467265425Snpfind_best_refill_source(struct adapter *sc, struct sge_fl *fl, int maxp) 4468218792Snp{ 4469265425Snp int8_t zidx, hwidx, idx; 4470265425Snp uint16_t region1, region3; 4471265425Snp int spare, spare_needed, n; 4472265425Snp struct sw_zone_info *swz; 4473265425Snp struct hw_buf_info *hwb, *hwb_list = &sc->sge.hw_buf_info[0]; 4474218792Snp 4475265425Snp /* 4476265425Snp * Buffer Packing: Look for PAGE_SIZE or larger zone which has a bufsize 4477265425Snp * large enough for the max payload and cluster metadata. Otherwise 4478265425Snp * settle for the largest bufsize that leaves enough room in the cluster 4479265425Snp * for metadata. 4480265425Snp * 4481265425Snp * Without buffer packing: Look for the smallest zone which has a 4482265425Snp * bufsize large enough for the max payload. Settle for the largest 4483265425Snp * bufsize available if there's nothing big enough for max payload. 4484265425Snp */ 4485265425Snp spare_needed = fl->flags & FL_BUF_PACKING ? CL_METADATA_SIZE : 0; 4486265425Snp swz = &sc->sge.sw_zone_info[0]; 4487265425Snp hwidx = -1; 4488265425Snp for (zidx = 0; zidx < SW_ZONE_SIZES; zidx++, swz++) { 4489265425Snp if (swz->size > largest_rx_cluster) { 4490265425Snp if (__predict_true(hwidx != -1)) 4491265425Snp break; 4492218792Snp 4493265425Snp /* 4494265425Snp * This is a misconfiguration. largest_rx_cluster is 4495265425Snp * preventing us from finding a refill source. See 4496265425Snp * dev.t5nex.<n>.buffer_sizes to figure out why. 4497265425Snp */ 4498265425Snp device_printf(sc->dev, "largest_rx_cluster=%u leaves no" 4499265425Snp " refill source for fl %p (dma %u). Ignored.\n", 4500265425Snp largest_rx_cluster, fl, maxp); 4501265425Snp } 4502265425Snp for (idx = swz->head_hwidx; idx != -1; idx = hwb->next) { 4503265425Snp hwb = &hwb_list[idx]; 4504265425Snp spare = swz->size - hwb->size; 4505265425Snp if (spare < spare_needed) 4506265425Snp continue; 4507265425Snp 4508265425Snp hwidx = idx; /* best option so far */ 4509265425Snp if (hwb->size >= maxp) { 4510265425Snp 4511265425Snp if ((fl->flags & FL_BUF_PACKING) == 0) 4512265425Snp goto done; /* stop looking (not packing) */ 4513265425Snp 4514265425Snp if (swz->size >= safest_rx_cluster) 4515265425Snp goto done; /* stop looking (packing) */ 4516265425Snp } 4517265425Snp break; /* keep looking, next zone */ 4518265425Snp } 4519255050Snp } 4520265425Snpdone: 4521265425Snp /* A usable hwidx has been located. */ 4522265425Snp MPASS(hwidx != -1); 4523265425Snp hwb = &hwb_list[hwidx]; 4524265425Snp zidx = hwb->zidx; 4525265425Snp swz = &sc->sge.sw_zone_info[zidx]; 4526265425Snp region1 = 0; 4527265425Snp region3 = swz->size - hwb->size; 4528255050Snp 4529265425Snp /* 4530265425Snp * Stay within this zone and see if there is a better match when mbuf 4531265425Snp * inlining is allowed. Remember that the hwidx's are sorted in 4532265425Snp * decreasing order of size (so in increasing order of spare area). 4533265425Snp */ 4534265425Snp for (idx = hwidx; idx != -1; idx = hwb->next) { 4535265425Snp hwb = &hwb_list[idx]; 4536265425Snp spare = swz->size - hwb->size; 4537255050Snp 4538265425Snp if (allow_mbufs_in_cluster == 0 || hwb->size < maxp) 4539265425Snp break; 4540281212Snp 4541281212Snp /* 4542281212Snp * Do not inline mbufs if doing so would violate the pad/pack 4543281212Snp * boundary alignment requirement. 4544281212Snp */ 4545281212Snp if (fl_pad && (MSIZE % sc->sge.pad_boundary) != 0) 4546281212Snp continue; 4547281212Snp if (fl->flags & FL_BUF_PACKING && 4548281212Snp (MSIZE % sc->sge.pack_boundary) != 0) 4549281212Snp continue; 4550281212Snp 4551265425Snp if (spare < CL_METADATA_SIZE + MSIZE) 4552265425Snp continue; 4553265425Snp n = (spare - CL_METADATA_SIZE) / MSIZE; 4554265425Snp if (n > howmany(hwb->size, maxp)) 4555265425Snp break; 4556255050Snp 4557265425Snp hwidx = idx; 4558265425Snp if (fl->flags & FL_BUF_PACKING) { 4559265425Snp region1 = n * MSIZE; 4560265425Snp region3 = spare - region1; 4561265425Snp } else { 4562265425Snp region1 = MSIZE; 4563265425Snp region3 = spare - region1; 4564265425Snp break; 4565255050Snp } 4566255050Snp } 4567255050Snp 4568265425Snp KASSERT(zidx >= 0 && zidx < SW_ZONE_SIZES, 4569265425Snp ("%s: bad zone %d for fl %p, maxp %d", __func__, zidx, fl, maxp)); 4570265425Snp KASSERT(hwidx >= 0 && hwidx <= SGE_FLBUF_SIZES, 4571265425Snp ("%s: bad hwidx %d for fl %p, maxp %d", __func__, hwidx, fl, maxp)); 4572265425Snp KASSERT(region1 + sc->sge.hw_buf_info[hwidx].size + region3 == 4573265425Snp sc->sge.sw_zone_info[zidx].size, 4574265425Snp ("%s: bad buffer layout for fl %p, maxp %d. " 4575265425Snp "cl %d; r1 %d, payload %d, r3 %d", __func__, fl, maxp, 4576265425Snp sc->sge.sw_zone_info[zidx].size, region1, 4577265425Snp sc->sge.hw_buf_info[hwidx].size, region3)); 4578265425Snp if (fl->flags & FL_BUF_PACKING || region1 > 0) { 4579265425Snp KASSERT(region3 >= CL_METADATA_SIZE, 4580265425Snp ("%s: no room for metadata. fl %p, maxp %d; " 4581265425Snp "cl %d; r1 %d, payload %d, r3 %d", __func__, fl, maxp, 4582265425Snp sc->sge.sw_zone_info[zidx].size, region1, 4583265425Snp sc->sge.hw_buf_info[hwidx].size, region3)); 4584265425Snp KASSERT(region1 % MSIZE == 0, 4585265425Snp ("%s: bad mbuf region for fl %p, maxp %d. " 4586265425Snp "cl %d; r1 %d, payload %d, r3 %d", __func__, fl, maxp, 4587265425Snp sc->sge.sw_zone_info[zidx].size, region1, 4588265425Snp sc->sge.hw_buf_info[hwidx].size, region3)); 4589265425Snp } 4590265425Snp 4591265425Snp fl->cll_def.zidx = zidx; 4592265425Snp fl->cll_def.hwidx = hwidx; 4593265425Snp fl->cll_def.region1 = region1; 4594265425Snp fl->cll_def.region3 = region3; 4595265425Snp} 4596265425Snp 4597265425Snpstatic void 4598265425Snpfind_safe_refill_source(struct adapter *sc, struct sge_fl *fl) 4599265425Snp{ 4600265425Snp struct sge *s = &sc->sge; 4601265425Snp struct hw_buf_info *hwb; 4602265425Snp struct sw_zone_info *swz; 4603265425Snp int spare; 4604265425Snp int8_t hwidx; 4605265425Snp 4606265425Snp if (fl->flags & FL_BUF_PACKING) 4607265425Snp hwidx = s->safe_hwidx2; /* with room for metadata */ 4608265425Snp else if (allow_mbufs_in_cluster && s->safe_hwidx2 != -1) { 4609265425Snp hwidx = s->safe_hwidx2; 4610265425Snp hwb = &s->hw_buf_info[hwidx]; 4611265425Snp swz = &s->sw_zone_info[hwb->zidx]; 4612265425Snp spare = swz->size - hwb->size; 4613265425Snp 4614265425Snp /* no good if there isn't room for an mbuf as well */ 4615265425Snp if (spare < CL_METADATA_SIZE + MSIZE) 4616265425Snp hwidx = s->safe_hwidx1; 4617265425Snp } else 4618265425Snp hwidx = s->safe_hwidx1; 4619265425Snp 4620265425Snp if (hwidx == -1) { 4621265425Snp /* No fallback source */ 4622265425Snp fl->cll_alt.hwidx = -1; 4623265425Snp fl->cll_alt.zidx = -1; 4624265425Snp 4625265425Snp return; 4626265425Snp } 4627265425Snp 4628265425Snp hwb = &s->hw_buf_info[hwidx]; 4629265425Snp swz = &s->sw_zone_info[hwb->zidx]; 4630265425Snp spare = swz->size - hwb->size; 4631265425Snp fl->cll_alt.hwidx = hwidx; 4632265425Snp fl->cll_alt.zidx = hwb->zidx; 4633281212Snp if (allow_mbufs_in_cluster && 4634281212Snp (fl_pad == 0 || (MSIZE % sc->sge.pad_boundary) == 0)) 4635265425Snp fl->cll_alt.region1 = ((spare - CL_METADATA_SIZE) / MSIZE) * MSIZE; 4636255050Snp else 4637265425Snp fl->cll_alt.region1 = 0; 4638265425Snp fl->cll_alt.region3 = spare - fl->cll_alt.region1; 4639218792Snp} 4640219286Snp 4641222510Snpstatic void 4642228561Snpadd_fl_to_sfl(struct adapter *sc, struct sge_fl *fl) 4643222510Snp{ 4644228561Snp mtx_lock(&sc->sfl_lock); 4645228561Snp FL_LOCK(fl); 4646228561Snp if ((fl->flags & FL_DOOMED) == 0) { 4647228561Snp fl->flags |= FL_STARVING; 4648228561Snp TAILQ_INSERT_TAIL(&sc->sfl, fl, link); 4649228561Snp callout_reset(&sc->sfl_callout, hz / 5, refill_sfl, sc); 4650222510Snp } 4651228561Snp FL_UNLOCK(fl); 4652228561Snp mtx_unlock(&sc->sfl_lock); 4653222510Snp} 4654222510Snp 4655284052Snpstatic void 4656284052Snphandle_wrq_egr_update(struct adapter *sc, struct sge_eq *eq) 4657284052Snp{ 4658284052Snp struct sge_wrq *wrq = (void *)eq; 4659284052Snp 4660284052Snp atomic_readandclear_int(&eq->equiq); 4661284052Snp taskqueue_enqueue(sc->tq[eq->tx_chan], &wrq->wrq_tx_task); 4662284052Snp} 4663284052Snp 4664284052Snpstatic void 4665284052Snphandle_eth_egr_update(struct adapter *sc, struct sge_eq *eq) 4666284052Snp{ 4667284052Snp struct sge_txq *txq = (void *)eq; 4668284052Snp 4669284052Snp MPASS((eq->flags & EQ_TYPEMASK) == EQ_ETH); 4670284052Snp 4671284052Snp atomic_readandclear_int(&eq->equiq); 4672284052Snp mp_ring_check_drainage(txq->r, 0); 4673284052Snp taskqueue_enqueue(sc->tq[eq->tx_chan], &txq->tx_reclaim_task); 4674284052Snp} 4675284052Snp 4676220873Snpstatic int 4677228561Snphandle_sge_egr_update(struct sge_iq *iq, const struct rss_header *rss, 4678228561Snp struct mbuf *m) 4679220873Snp{ 4680228561Snp const struct cpl_sge_egr_update *cpl = (const void *)(rss + 1); 4681228561Snp unsigned int qid = G_EGR_QID(ntohl(cpl->opcode_qid)); 4682228561Snp struct adapter *sc = iq->adapter; 4683228561Snp struct sge *s = &sc->sge; 4684228561Snp struct sge_eq *eq; 4685284052Snp static void (*h[])(struct adapter *, struct sge_eq *) = {NULL, 4686284052Snp &handle_wrq_egr_update, &handle_eth_egr_update, 4687284052Snp &handle_wrq_egr_update}; 4688220873Snp 4689228561Snp KASSERT(m == NULL, ("%s: payload with opcode %02x", __func__, 4690228561Snp rss->opcode)); 4691220873Snp 4692228561Snp eq = s->eqmap[qid - s->eq_start]; 4693284052Snp (*h[eq->flags & EQ_TYPEMASK])(sc, eq); 4694220873Snp 4695228561Snp return (0); 4696228561Snp} 4697220873Snp 4698247291Snp/* handle_fw_msg works for both fw4_msg and fw6_msg because this is valid */ 4699247291SnpCTASSERT(offsetof(struct cpl_fw4_msg, data) == \ 4700247291Snp offsetof(struct cpl_fw6_msg, data)); 4701247291Snp 4702228561Snpstatic int 4703239336Snphandle_fw_msg(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m) 4704228561Snp{ 4705239336Snp struct adapter *sc = iq->adapter; 4706228561Snp const struct cpl_fw6_msg *cpl = (const void *)(rss + 1); 4707220873Snp 4708228561Snp KASSERT(m == NULL, ("%s: payload with opcode %02x", __func__, 4709228561Snp rss->opcode)); 4710220873Snp 4711247291Snp if (cpl->type == FW_TYPE_RSSCPL || cpl->type == FW6_TYPE_RSSCPL) { 4712247291Snp const struct rss_header *rss2; 4713247291Snp 4714247291Snp rss2 = (const struct rss_header *)&cpl->data[0]; 4715247291Snp return (sc->cpl_handler[rss2->opcode](iq, rss2, m)); 4716247291Snp } 4717247291Snp 4718239336Snp return (sc->fw_msg_handler[cpl->type](sc, &cpl->data[0])); 4719220873Snp} 4720221911Snp 4721221911Snpstatic int 4722222510Snpsysctl_uint16(SYSCTL_HANDLER_ARGS) 4723221911Snp{ 4724221911Snp uint16_t *id = arg1; 4725221911Snp int i = *id; 4726221911Snp 4727221911Snp return sysctl_handle_int(oidp, &i, 0, req); 4728221911Snp} 4729265425Snp 4730265425Snpstatic int 4731265425Snpsysctl_bufsizes(SYSCTL_HANDLER_ARGS) 4732265425Snp{ 4733265425Snp struct sge *s = arg1; 4734265425Snp struct hw_buf_info *hwb = &s->hw_buf_info[0]; 4735265425Snp struct sw_zone_info *swz = &s->sw_zone_info[0]; 4736265425Snp int i, rc; 4737265425Snp struct sbuf sb; 4738265425Snp char c; 4739265425Snp 4740265425Snp sbuf_new(&sb, NULL, 32, SBUF_AUTOEXTEND); 4741265425Snp for (i = 0; i < SGE_FLBUF_SIZES; i++, hwb++) { 4742265425Snp if (hwb->zidx >= 0 && swz[hwb->zidx].size <= largest_rx_cluster) 4743265425Snp c = '*'; 4744265425Snp else 4745265425Snp c = '\0'; 4746265425Snp 4747265425Snp sbuf_printf(&sb, "%u%c ", hwb->size, c); 4748265425Snp } 4749265425Snp sbuf_trim(&sb); 4750265425Snp sbuf_finish(&sb); 4751265425Snp rc = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req); 4752265425Snp sbuf_delete(&sb); 4753265425Snp return (rc); 4754265425Snp} 4755