sfxge_tx.c revision 342404
1227569Sphilip/*- 2300607Sarybchik * Copyright (c) 2010-2016 Solarflare Communications Inc. 3227569Sphilip * All rights reserved. 4227569Sphilip * 5227569Sphilip * This software was developed in part by Philip Paeps under contract for 6227569Sphilip * Solarflare Communications, Inc. 7227569Sphilip * 8227569Sphilip * Redistribution and use in source and binary forms, with or without 9283514Sarybchik * modification, are permitted provided that the following conditions are met: 10227569Sphilip * 11283514Sarybchik * 1. Redistributions of source code must retain the above copyright notice, 12283514Sarybchik * this list of conditions and the following disclaimer. 13283514Sarybchik * 2. Redistributions in binary form must reproduce the above copyright notice, 14283514Sarybchik * this list of conditions and the following disclaimer in the documentation 15283514Sarybchik * and/or other materials provided with the distribution. 16283514Sarybchik * 17283514Sarybchik * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18283514Sarybchik * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 19283514Sarybchik * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20283514Sarybchik * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 21283514Sarybchik * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 22283514Sarybchik * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 23283514Sarybchik * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 24283514Sarybchik * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 25283514Sarybchik * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 26283514Sarybchik * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 27283514Sarybchik * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28283514Sarybchik * 29283514Sarybchik * The views and conclusions contained in the software and documentation are 30283514Sarybchik * those of the authors and should not be interpreted as representing official 31283514Sarybchik * policies, either expressed or implied, of the FreeBSD Project. 32227569Sphilip */ 33227569Sphilip 34264461Sgnn/* Theory of operation: 35264461Sgnn * 36264461Sgnn * Tx queues allocation and mapping 37264461Sgnn * 38264461Sgnn * One Tx queue with enabled checksum offload is allocated per Rx channel 39264461Sgnn * (event queue). Also 2 Tx queues (one without checksum offload and one 40264461Sgnn * with IP checksum offload only) are allocated and bound to event queue 0. 41264461Sgnn * sfxge_txq_type is used as Tx queue label. 42264461Sgnn * 43264461Sgnn * So, event queue plus label mapping to Tx queue index is: 44264461Sgnn * if event queue index is 0, TxQ-index = TxQ-label * [0..SFXGE_TXQ_NTYPES) 45264461Sgnn * else TxQ-index = SFXGE_TXQ_NTYPES + EvQ-index - 1 46264461Sgnn * See sfxge_get_txq_by_label() sfxge_ev.c 47264461Sgnn */ 48264461Sgnn 49227569Sphilip#include <sys/cdefs.h> 50227569Sphilip__FBSDID("$FreeBSD: stable/11/sys/dev/sfxge/sfxge_tx.c 342404 2018-12-25 06:36:33Z arybchik $"); 51227569Sphilip 52301494Sarybchik#include "opt_rss.h" 53301494Sarybchik 54295126Sglebius#include <sys/param.h> 55295126Sglebius#include <sys/malloc.h> 56227569Sphilip#include <sys/mbuf.h> 57227569Sphilip#include <sys/smp.h> 58227569Sphilip#include <sys/socket.h> 59227569Sphilip#include <sys/sysctl.h> 60272331Sgnn#include <sys/syslog.h> 61294077Sarybchik#include <sys/limits.h> 62227569Sphilip 63227569Sphilip#include <net/bpf.h> 64227569Sphilip#include <net/ethernet.h> 65227569Sphilip#include <net/if.h> 66227569Sphilip#include <net/if_vlan_var.h> 67227569Sphilip 68227569Sphilip#include <netinet/in.h> 69227569Sphilip#include <netinet/ip.h> 70227569Sphilip#include <netinet/ip6.h> 71227569Sphilip#include <netinet/tcp.h> 72227569Sphilip 73301494Sarybchik#ifdef RSS 74301494Sarybchik#include <net/rss_config.h> 75301494Sarybchik#endif 76301494Sarybchik 77227569Sphilip#include "common/efx.h" 78227569Sphilip 79227569Sphilip#include "sfxge.h" 80227569Sphilip#include "sfxge_tx.h" 81227569Sphilip 82227569Sphilip 83272331Sgnn#define SFXGE_PARAM_TX_DPL_GET_MAX SFXGE_PARAM(tx_dpl_get_max) 84272331Sgnnstatic int sfxge_tx_dpl_get_max = SFXGE_TX_DPL_GET_PKT_LIMIT_DEFAULT; 85272331SgnnTUNABLE_INT(SFXGE_PARAM_TX_DPL_GET_MAX, &sfxge_tx_dpl_get_max); 86272331SgnnSYSCTL_INT(_hw_sfxge, OID_AUTO, tx_dpl_get_max, CTLFLAG_RDTUN, 87272331Sgnn &sfxge_tx_dpl_get_max, 0, 88277895Sarybchik "Maximum number of any packets in deferred packet get-list"); 89272331Sgnn 90277895Sarybchik#define SFXGE_PARAM_TX_DPL_GET_NON_TCP_MAX \ 91277895Sarybchik SFXGE_PARAM(tx_dpl_get_non_tcp_max) 92277895Sarybchikstatic int sfxge_tx_dpl_get_non_tcp_max = 93277895Sarybchik SFXGE_TX_DPL_GET_NON_TCP_PKT_LIMIT_DEFAULT; 94277895SarybchikTUNABLE_INT(SFXGE_PARAM_TX_DPL_GET_NON_TCP_MAX, &sfxge_tx_dpl_get_non_tcp_max); 95277895SarybchikSYSCTL_INT(_hw_sfxge, OID_AUTO, tx_dpl_get_non_tcp_max, CTLFLAG_RDTUN, 96277895Sarybchik &sfxge_tx_dpl_get_non_tcp_max, 0, 97277895Sarybchik "Maximum number of non-TCP packets in deferred packet get-list"); 98277895Sarybchik 99272331Sgnn#define SFXGE_PARAM_TX_DPL_PUT_MAX SFXGE_PARAM(tx_dpl_put_max) 100272331Sgnnstatic int sfxge_tx_dpl_put_max = SFXGE_TX_DPL_PUT_PKT_LIMIT_DEFAULT; 101272331SgnnTUNABLE_INT(SFXGE_PARAM_TX_DPL_PUT_MAX, &sfxge_tx_dpl_put_max); 102272331SgnnSYSCTL_INT(_hw_sfxge, OID_AUTO, tx_dpl_put_max, CTLFLAG_RDTUN, 103272331Sgnn &sfxge_tx_dpl_put_max, 0, 104277895Sarybchik "Maximum number of any packets in deferred packet put-list"); 105272331Sgnn 106283514Sarybchik#define SFXGE_PARAM_TSO_FW_ASSISTED SFXGE_PARAM(tso_fw_assisted) 107294077Sarybchikstatic int sfxge_tso_fw_assisted = (SFXGE_FATSOV1 | SFXGE_FATSOV2); 108283514SarybchikTUNABLE_INT(SFXGE_PARAM_TSO_FW_ASSISTED, &sfxge_tso_fw_assisted); 109283514SarybchikSYSCTL_INT(_hw_sfxge, OID_AUTO, tso_fw_assisted, CTLFLAG_RDTUN, 110283514Sarybchik &sfxge_tso_fw_assisted, 0, 111294077Sarybchik "Bitmask of FW-assisted TSO allowed to use if supported by NIC firmware"); 112272331Sgnn 113283514Sarybchik 114280377Sarybchikstatic const struct { 115280377Sarybchik const char *name; 116280377Sarybchik size_t offset; 117280377Sarybchik} sfxge_tx_stats[] = { 118280377Sarybchik#define SFXGE_TX_STAT(name, member) \ 119280377Sarybchik { #name, offsetof(struct sfxge_txq, member) } 120280377Sarybchik SFXGE_TX_STAT(tso_bursts, tso_bursts), 121280377Sarybchik SFXGE_TX_STAT(tso_packets, tso_packets), 122280377Sarybchik SFXGE_TX_STAT(tso_long_headers, tso_long_headers), 123280377Sarybchik SFXGE_TX_STAT(tso_pdrop_too_many, tso_pdrop_too_many), 124280377Sarybchik SFXGE_TX_STAT(tso_pdrop_no_rsrc, tso_pdrop_no_rsrc), 125280377Sarybchik SFXGE_TX_STAT(tx_collapses, collapses), 126280377Sarybchik SFXGE_TX_STAT(tx_drops, drops), 127280377Sarybchik SFXGE_TX_STAT(tx_get_overflow, get_overflow), 128280377Sarybchik SFXGE_TX_STAT(tx_get_non_tcp_overflow, get_non_tcp_overflow), 129280377Sarybchik SFXGE_TX_STAT(tx_put_overflow, put_overflow), 130280377Sarybchik SFXGE_TX_STAT(tx_netdown_drops, netdown_drops), 131280377Sarybchik}; 132280377Sarybchik 133280377Sarybchik 134227569Sphilip/* Forward declarations. */ 135278837Sarybchikstatic void sfxge_tx_qdpl_service(struct sfxge_txq *txq); 136227569Sphilipstatic void sfxge_tx_qlist_post(struct sfxge_txq *txq); 137227569Sphilipstatic void sfxge_tx_qunblock(struct sfxge_txq *txq); 138227569Sphilipstatic int sfxge_tx_queue_tso(struct sfxge_txq *txq, struct mbuf *mbuf, 139283514Sarybchik const bus_dma_segment_t *dma_seg, int n_dma_seg, 140283514Sarybchik int vlan_tagged); 141227569Sphilip 142283514Sarybchikstatic int 143283514Sarybchiksfxge_tx_maybe_insert_tag(struct sfxge_txq *txq, struct mbuf *mbuf) 144283514Sarybchik{ 145283514Sarybchik uint16_t this_tag = ((mbuf->m_flags & M_VLANTAG) ? 146283514Sarybchik mbuf->m_pkthdr.ether_vtag : 147283514Sarybchik 0); 148283514Sarybchik 149283514Sarybchik if (this_tag == txq->hw_vlan_tci) 150283514Sarybchik return (0); 151283514Sarybchik 152283514Sarybchik efx_tx_qdesc_vlantci_create(txq->common, 153283514Sarybchik bswap16(this_tag), 154283514Sarybchik &txq->pend_desc[0]); 155283514Sarybchik txq->n_pend_desc = 1; 156283514Sarybchik txq->hw_vlan_tci = this_tag; 157283514Sarybchik return (1); 158283514Sarybchik} 159283514Sarybchik 160283514Sarybchikstatic inline void 161283514Sarybchiksfxge_next_stmp(struct sfxge_txq *txq, struct sfxge_tx_mapping **pstmp) 162283514Sarybchik{ 163283514Sarybchik KASSERT((*pstmp)->flags == 0, ("stmp flags are not 0")); 164283514Sarybchik if (__predict_false(*pstmp == 165283514Sarybchik &txq->stmp[txq->ptr_mask])) 166283514Sarybchik *pstmp = &txq->stmp[0]; 167283514Sarybchik else 168283514Sarybchik (*pstmp)++; 169283514Sarybchik} 170283514Sarybchik 171283514Sarybchik 172227569Sphilipvoid 173277889Sarybchiksfxge_tx_qcomplete(struct sfxge_txq *txq, struct sfxge_evq *evq) 174227569Sphilip{ 175227569Sphilip unsigned int completed; 176227569Sphilip 177278221Sarybchik SFXGE_EVQ_LOCK_ASSERT_OWNED(evq); 178227569Sphilip 179227569Sphilip completed = txq->completed; 180227569Sphilip while (completed != txq->pending) { 181227569Sphilip struct sfxge_tx_mapping *stmp; 182227569Sphilip unsigned int id; 183227569Sphilip 184272328Sgnn id = completed++ & txq->ptr_mask; 185227569Sphilip 186227569Sphilip stmp = &txq->stmp[id]; 187227569Sphilip if (stmp->flags & TX_BUF_UNMAP) { 188227569Sphilip bus_dmamap_unload(txq->packet_dma_tag, stmp->map); 189227569Sphilip if (stmp->flags & TX_BUF_MBUF) { 190227569Sphilip struct mbuf *m = stmp->u.mbuf; 191227569Sphilip do 192227569Sphilip m = m_free(m); 193227569Sphilip while (m != NULL); 194227569Sphilip } else { 195227569Sphilip free(stmp->u.heap_buf, M_SFXGE); 196227569Sphilip } 197227569Sphilip stmp->flags = 0; 198227569Sphilip } 199227569Sphilip } 200227569Sphilip txq->completed = completed; 201227569Sphilip 202227569Sphilip /* Check whether we need to unblock the queue. */ 203227569Sphilip mb(); 204227569Sphilip if (txq->blocked) { 205227569Sphilip unsigned int level; 206227569Sphilip 207227569Sphilip level = txq->added - txq->completed; 208272328Sgnn if (level <= SFXGE_TXQ_UNBLOCK_LEVEL(txq->entries)) 209227569Sphilip sfxge_tx_qunblock(txq); 210227569Sphilip } 211227569Sphilip} 212227569Sphilip 213278837Sarybchikstatic unsigned int 214277895Sarybchiksfxge_is_mbuf_non_tcp(struct mbuf *mbuf) 215277895Sarybchik{ 216298955Spfg /* Absence of TCP checksum flags does not mean that it is non-TCP 217277895Sarybchik * but it should be true if user wants to achieve high throughput. 218277895Sarybchik */ 219277895Sarybchik return (!(mbuf->m_pkthdr.csum_flags & (CSUM_IP_TCP | CSUM_IP6_TCP))); 220277895Sarybchik} 221277895Sarybchik 222227569Sphilip/* 223227569Sphilip * Reorder the put list and append it to the get list. 224227569Sphilip */ 225227569Sphilipstatic void 226227569Sphilipsfxge_tx_qdpl_swizzle(struct sfxge_txq *txq) 227227569Sphilip{ 228227569Sphilip struct sfxge_tx_dpl *stdp; 229227569Sphilip struct mbuf *mbuf, *get_next, **get_tailp; 230227569Sphilip volatile uintptr_t *putp; 231227569Sphilip uintptr_t put; 232227569Sphilip unsigned int count; 233277895Sarybchik unsigned int non_tcp_count; 234227569Sphilip 235278221Sarybchik SFXGE_TXQ_LOCK_ASSERT_OWNED(txq); 236227569Sphilip 237227569Sphilip stdp = &txq->dpl; 238227569Sphilip 239227569Sphilip /* Acquire the put list. */ 240227569Sphilip putp = &stdp->std_put; 241227640Smarius put = atomic_readandclear_ptr(putp); 242227569Sphilip mbuf = (void *)put; 243227569Sphilip 244227569Sphilip if (mbuf == NULL) 245227569Sphilip return; 246227569Sphilip 247227569Sphilip /* Reverse the put list. */ 248227569Sphilip get_tailp = &mbuf->m_nextpkt; 249227569Sphilip get_next = NULL; 250227569Sphilip 251227569Sphilip count = 0; 252277895Sarybchik non_tcp_count = 0; 253227569Sphilip do { 254227569Sphilip struct mbuf *put_next; 255227569Sphilip 256277895Sarybchik non_tcp_count += sfxge_is_mbuf_non_tcp(mbuf); 257227569Sphilip put_next = mbuf->m_nextpkt; 258227569Sphilip mbuf->m_nextpkt = get_next; 259227569Sphilip get_next = mbuf; 260227569Sphilip mbuf = put_next; 261227569Sphilip 262227569Sphilip count++; 263227569Sphilip } while (mbuf != NULL); 264227569Sphilip 265279231Sarybchik if (count > stdp->std_put_hiwat) 266279231Sarybchik stdp->std_put_hiwat = count; 267279231Sarybchik 268227569Sphilip /* Append the reversed put list to the get list. */ 269227569Sphilip KASSERT(*get_tailp == NULL, ("*get_tailp != NULL")); 270227569Sphilip *stdp->std_getp = get_next; 271227569Sphilip stdp->std_getp = get_tailp; 272272330Sgnn stdp->std_get_count += count; 273277895Sarybchik stdp->std_get_non_tcp_count += non_tcp_count; 274227569Sphilip} 275227569Sphilip 276227569Sphilipstatic void 277227569Sphilipsfxge_tx_qreap(struct sfxge_txq *txq) 278227569Sphilip{ 279278221Sarybchik SFXGE_TXQ_LOCK_ASSERT_OWNED(txq); 280227569Sphilip 281227569Sphilip txq->reaped = txq->completed; 282227569Sphilip} 283227569Sphilip 284227569Sphilipstatic void 285227569Sphilipsfxge_tx_qlist_post(struct sfxge_txq *txq) 286227569Sphilip{ 287227569Sphilip unsigned int old_added; 288283514Sarybchik unsigned int block_level; 289227569Sphilip unsigned int level; 290227569Sphilip int rc; 291227569Sphilip 292278221Sarybchik SFXGE_TXQ_LOCK_ASSERT_OWNED(txq); 293227569Sphilip 294227569Sphilip KASSERT(txq->n_pend_desc != 0, ("txq->n_pend_desc == 0")); 295283514Sarybchik KASSERT(txq->n_pend_desc <= txq->max_pkt_desc, 296227569Sphilip ("txq->n_pend_desc too large")); 297227569Sphilip KASSERT(!txq->blocked, ("txq->blocked")); 298227569Sphilip 299227569Sphilip old_added = txq->added; 300227569Sphilip 301227569Sphilip /* Post the fragment list. */ 302283514Sarybchik rc = efx_tx_qdesc_post(txq->common, txq->pend_desc, txq->n_pend_desc, 303227569Sphilip txq->reaped, &txq->added); 304283514Sarybchik KASSERT(rc == 0, ("efx_tx_qdesc_post() failed")); 305227569Sphilip 306283514Sarybchik /* If efx_tx_qdesc_post() had to refragment, our information about 307227569Sphilip * buffers to free may be associated with the wrong 308227569Sphilip * descriptors. 309227569Sphilip */ 310227569Sphilip KASSERT(txq->added - old_added == txq->n_pend_desc, 311283514Sarybchik ("efx_tx_qdesc_post() refragmented descriptors")); 312227569Sphilip 313227569Sphilip level = txq->added - txq->reaped; 314272328Sgnn KASSERT(level <= txq->entries, ("overfilled TX queue")); 315227569Sphilip 316227569Sphilip /* Clear the fragment list. */ 317227569Sphilip txq->n_pend_desc = 0; 318227569Sphilip 319283514Sarybchik /* 320283514Sarybchik * Set the block level to ensure there is space to generate a 321283514Sarybchik * large number of descriptors for TSO. 322283514Sarybchik */ 323283514Sarybchik block_level = EFX_TXQ_LIMIT(txq->entries) - txq->max_pkt_desc; 324283514Sarybchik 325227569Sphilip /* Have we reached the block level? */ 326283514Sarybchik if (level < block_level) 327227569Sphilip return; 328227569Sphilip 329227569Sphilip /* Reap, and check again */ 330227569Sphilip sfxge_tx_qreap(txq); 331227569Sphilip level = txq->added - txq->reaped; 332283514Sarybchik if (level < block_level) 333227569Sphilip return; 334227569Sphilip 335227569Sphilip txq->blocked = 1; 336227569Sphilip 337227569Sphilip /* 338227569Sphilip * Avoid a race with completion interrupt handling that could leave 339227569Sphilip * the queue blocked. 340227569Sphilip */ 341227569Sphilip mb(); 342227569Sphilip sfxge_tx_qreap(txq); 343227569Sphilip level = txq->added - txq->reaped; 344283514Sarybchik if (level < block_level) { 345227569Sphilip mb(); 346227569Sphilip txq->blocked = 0; 347227569Sphilip } 348227569Sphilip} 349227569Sphilip 350227569Sphilipstatic int sfxge_tx_queue_mbuf(struct sfxge_txq *txq, struct mbuf *mbuf) 351227569Sphilip{ 352227569Sphilip bus_dmamap_t *used_map; 353227569Sphilip bus_dmamap_t map; 354227569Sphilip bus_dma_segment_t dma_seg[SFXGE_TX_MAPPING_MAX_SEG]; 355227569Sphilip unsigned int id; 356227569Sphilip struct sfxge_tx_mapping *stmp; 357283514Sarybchik efx_desc_t *desc; 358227569Sphilip int n_dma_seg; 359227569Sphilip int rc; 360227569Sphilip int i; 361283514Sarybchik int eop; 362283514Sarybchik int vlan_tagged; 363227569Sphilip 364227569Sphilip KASSERT(!txq->blocked, ("txq->blocked")); 365227569Sphilip 366312157Sarybchik#if SFXGE_TX_PARSE_EARLY 367312157Sarybchik /* 368312157Sarybchik * If software TSO is used, we still need to copy packet header, 369312157Sarybchik * even if we have already parsed it early before enqueue. 370312157Sarybchik */ 371312157Sarybchik if ((mbuf->m_pkthdr.csum_flags & CSUM_TSO) && 372312157Sarybchik (txq->tso_fw_assisted == 0)) 373312157Sarybchik prefetch_read_many(mbuf->m_data); 374312157Sarybchik#else 375312157Sarybchik /* 376312157Sarybchik * Prefetch packet header since we need to parse it and extract 377312157Sarybchik * IP ID, TCP sequence number and flags. 378312157Sarybchik */ 379227569Sphilip if (mbuf->m_pkthdr.csum_flags & CSUM_TSO) 380227569Sphilip prefetch_read_many(mbuf->m_data); 381312157Sarybchik#endif 382227569Sphilip 383279351Sarybchik if (__predict_false(txq->init_state != SFXGE_TXQ_STARTED)) { 384227569Sphilip rc = EINTR; 385227569Sphilip goto reject; 386227569Sphilip } 387227569Sphilip 388227569Sphilip /* Load the packet for DMA. */ 389272328Sgnn id = txq->added & txq->ptr_mask; 390227569Sphilip stmp = &txq->stmp[id]; 391227569Sphilip rc = bus_dmamap_load_mbuf_sg(txq->packet_dma_tag, stmp->map, 392227569Sphilip mbuf, dma_seg, &n_dma_seg, 0); 393227569Sphilip if (rc == EFBIG) { 394227569Sphilip /* Try again. */ 395243857Sglebius struct mbuf *new_mbuf = m_collapse(mbuf, M_NOWAIT, 396227569Sphilip SFXGE_TX_MAPPING_MAX_SEG); 397227569Sphilip if (new_mbuf == NULL) 398227569Sphilip goto reject; 399227569Sphilip ++txq->collapses; 400227569Sphilip mbuf = new_mbuf; 401227569Sphilip rc = bus_dmamap_load_mbuf_sg(txq->packet_dma_tag, 402227569Sphilip stmp->map, mbuf, 403227569Sphilip dma_seg, &n_dma_seg, 0); 404227569Sphilip } 405227569Sphilip if (rc != 0) 406227569Sphilip goto reject; 407227569Sphilip 408227569Sphilip /* Make the packet visible to the hardware. */ 409227569Sphilip bus_dmamap_sync(txq->packet_dma_tag, stmp->map, BUS_DMASYNC_PREWRITE); 410227569Sphilip 411227569Sphilip used_map = &stmp->map; 412227569Sphilip 413283514Sarybchik vlan_tagged = sfxge_tx_maybe_insert_tag(txq, mbuf); 414283514Sarybchik if (vlan_tagged) { 415283514Sarybchik sfxge_next_stmp(txq, &stmp); 416283514Sarybchik } 417227569Sphilip if (mbuf->m_pkthdr.csum_flags & CSUM_TSO) { 418283514Sarybchik rc = sfxge_tx_queue_tso(txq, mbuf, dma_seg, n_dma_seg, vlan_tagged); 419227569Sphilip if (rc < 0) 420227569Sphilip goto reject_mapped; 421283514Sarybchik stmp = &txq->stmp[(rc - 1) & txq->ptr_mask]; 422227569Sphilip } else { 423227569Sphilip /* Add the mapping to the fragment list, and set flags 424227569Sphilip * for the buffer. 425227569Sphilip */ 426283514Sarybchik 427227569Sphilip i = 0; 428227569Sphilip for (;;) { 429283514Sarybchik desc = &txq->pend_desc[i + vlan_tagged]; 430283514Sarybchik eop = (i == n_dma_seg - 1); 431283514Sarybchik efx_tx_qdesc_dma_create(txq->common, 432283514Sarybchik dma_seg[i].ds_addr, 433283514Sarybchik dma_seg[i].ds_len, 434283514Sarybchik eop, 435283514Sarybchik desc); 436283514Sarybchik if (eop) 437227569Sphilip break; 438227569Sphilip i++; 439283514Sarybchik sfxge_next_stmp(txq, &stmp); 440227569Sphilip } 441283514Sarybchik txq->n_pend_desc = n_dma_seg + vlan_tagged; 442227569Sphilip } 443227569Sphilip 444227569Sphilip /* 445227569Sphilip * If the mapping required more than one descriptor 446227569Sphilip * then we need to associate the DMA map with the last 447227569Sphilip * descriptor, not the first. 448227569Sphilip */ 449227569Sphilip if (used_map != &stmp->map) { 450227569Sphilip map = stmp->map; 451227569Sphilip stmp->map = *used_map; 452227569Sphilip *used_map = map; 453227569Sphilip } 454227569Sphilip 455227569Sphilip stmp->u.mbuf = mbuf; 456227569Sphilip stmp->flags = TX_BUF_UNMAP | TX_BUF_MBUF; 457227569Sphilip 458227569Sphilip /* Post the fragment list. */ 459227569Sphilip sfxge_tx_qlist_post(txq); 460227569Sphilip 461272325Sgnn return (0); 462227569Sphilip 463227569Sphilipreject_mapped: 464227569Sphilip bus_dmamap_unload(txq->packet_dma_tag, *used_map); 465227569Sphilipreject: 466227569Sphilip /* Drop the packet on the floor. */ 467227569Sphilip m_freem(mbuf); 468227569Sphilip ++txq->drops; 469227569Sphilip 470272325Sgnn return (rc); 471227569Sphilip} 472227569Sphilip 473227569Sphilip/* 474227569Sphilip * Drain the deferred packet list into the transmit queue. 475227569Sphilip */ 476227569Sphilipstatic void 477227569Sphilipsfxge_tx_qdpl_drain(struct sfxge_txq *txq) 478227569Sphilip{ 479227569Sphilip struct sfxge_softc *sc; 480227569Sphilip struct sfxge_tx_dpl *stdp; 481227569Sphilip struct mbuf *mbuf, *next; 482227569Sphilip unsigned int count; 483277895Sarybchik unsigned int non_tcp_count; 484227569Sphilip unsigned int pushed; 485227569Sphilip int rc; 486227569Sphilip 487278221Sarybchik SFXGE_TXQ_LOCK_ASSERT_OWNED(txq); 488227569Sphilip 489227569Sphilip sc = txq->sc; 490227569Sphilip stdp = &txq->dpl; 491227569Sphilip pushed = txq->added; 492227569Sphilip 493280163Sarybchik if (__predict_true(txq->init_state == SFXGE_TXQ_STARTED)) { 494280163Sarybchik prefetch_read_many(sc->enp); 495280163Sarybchik prefetch_read_many(txq->common); 496280163Sarybchik } 497227569Sphilip 498227569Sphilip mbuf = stdp->std_get; 499272330Sgnn count = stdp->std_get_count; 500277895Sarybchik non_tcp_count = stdp->std_get_non_tcp_count; 501227569Sphilip 502277895Sarybchik if (count > stdp->std_get_hiwat) 503277895Sarybchik stdp->std_get_hiwat = count; 504277895Sarybchik 505227569Sphilip while (count != 0) { 506227569Sphilip KASSERT(mbuf != NULL, ("mbuf == NULL")); 507227569Sphilip 508227569Sphilip next = mbuf->m_nextpkt; 509227569Sphilip mbuf->m_nextpkt = NULL; 510227569Sphilip 511227569Sphilip ETHER_BPF_MTAP(sc->ifnet, mbuf); /* packet capture */ 512227569Sphilip 513227569Sphilip if (next != NULL) 514227569Sphilip prefetch_read_many(next); 515227569Sphilip 516227569Sphilip rc = sfxge_tx_queue_mbuf(txq, mbuf); 517227569Sphilip --count; 518277895Sarybchik non_tcp_count -= sfxge_is_mbuf_non_tcp(mbuf); 519227569Sphilip mbuf = next; 520227569Sphilip if (rc != 0) 521227569Sphilip continue; 522227569Sphilip 523227569Sphilip if (txq->blocked) 524227569Sphilip break; 525227569Sphilip 526227569Sphilip /* Push the fragments to the hardware in batches. */ 527227569Sphilip if (txq->added - pushed >= SFXGE_TX_BATCH) { 528283514Sarybchik efx_tx_qpush(txq->common, txq->added, pushed); 529227569Sphilip pushed = txq->added; 530227569Sphilip } 531227569Sphilip } 532227569Sphilip 533227569Sphilip if (count == 0) { 534227569Sphilip KASSERT(mbuf == NULL, ("mbuf != NULL")); 535277895Sarybchik KASSERT(non_tcp_count == 0, 536277895Sarybchik ("inconsistent TCP/non-TCP detection")); 537227569Sphilip stdp->std_get = NULL; 538272330Sgnn stdp->std_get_count = 0; 539277895Sarybchik stdp->std_get_non_tcp_count = 0; 540227569Sphilip stdp->std_getp = &stdp->std_get; 541227569Sphilip } else { 542227569Sphilip stdp->std_get = mbuf; 543272330Sgnn stdp->std_get_count = count; 544277895Sarybchik stdp->std_get_non_tcp_count = non_tcp_count; 545227569Sphilip } 546227569Sphilip 547227569Sphilip if (txq->added != pushed) 548283514Sarybchik efx_tx_qpush(txq->common, txq->added, pushed); 549227569Sphilip 550272330Sgnn KASSERT(txq->blocked || stdp->std_get_count == 0, 551227569Sphilip ("queue unblocked but count is non-zero")); 552227569Sphilip} 553227569Sphilip 554283514Sarybchik#define SFXGE_TX_QDPL_PENDING(_txq) ((_txq)->dpl.std_put != 0) 555227569Sphilip 556227569Sphilip/* 557227569Sphilip * Service the deferred packet list. 558227569Sphilip * 559227569Sphilip * NOTE: drops the txq mutex! 560227569Sphilip */ 561278837Sarybchikstatic void 562227569Sphilipsfxge_tx_qdpl_service(struct sfxge_txq *txq) 563227569Sphilip{ 564278221Sarybchik SFXGE_TXQ_LOCK_ASSERT_OWNED(txq); 565227569Sphilip 566227569Sphilip do { 567227569Sphilip if (SFXGE_TX_QDPL_PENDING(txq)) 568227569Sphilip sfxge_tx_qdpl_swizzle(txq); 569227569Sphilip 570227569Sphilip if (!txq->blocked) 571227569Sphilip sfxge_tx_qdpl_drain(txq); 572227569Sphilip 573278221Sarybchik SFXGE_TXQ_UNLOCK(txq); 574227569Sphilip } while (SFXGE_TX_QDPL_PENDING(txq) && 575278221Sarybchik SFXGE_TXQ_TRYLOCK(txq)); 576227569Sphilip} 577227569Sphilip 578227569Sphilip/* 579282942Sarybchik * Put a packet on the deferred packet get-list. 580227569Sphilip */ 581278837Sarybchikstatic int 582282942Sarybchiksfxge_tx_qdpl_put_locked(struct sfxge_txq *txq, struct mbuf *mbuf) 583227569Sphilip{ 584227569Sphilip struct sfxge_tx_dpl *stdp; 585227569Sphilip 586227569Sphilip stdp = &txq->dpl; 587227569Sphilip 588227569Sphilip KASSERT(mbuf->m_nextpkt == NULL, ("mbuf->m_nextpkt != NULL")); 589227569Sphilip 590282942Sarybchik SFXGE_TXQ_LOCK_ASSERT_OWNED(txq); 591227569Sphilip 592282942Sarybchik if (stdp->std_get_count >= stdp->std_get_max) { 593282942Sarybchik txq->get_overflow++; 594282942Sarybchik return (ENOBUFS); 595282942Sarybchik } 596282942Sarybchik if (sfxge_is_mbuf_non_tcp(mbuf)) { 597282942Sarybchik if (stdp->std_get_non_tcp_count >= 598282942Sarybchik stdp->std_get_non_tcp_max) { 599282942Sarybchik txq->get_non_tcp_overflow++; 600263649Sglebius return (ENOBUFS); 601277895Sarybchik } 602282942Sarybchik stdp->std_get_non_tcp_count++; 603282942Sarybchik } 604263649Sglebius 605282942Sarybchik *(stdp->std_getp) = mbuf; 606282942Sarybchik stdp->std_getp = &mbuf->m_nextpkt; 607282942Sarybchik stdp->std_get_count++; 608227569Sphilip 609282942Sarybchik return (0); 610282942Sarybchik} 611227569Sphilip 612282942Sarybchik/* 613282942Sarybchik * Put a packet on the deferred packet put-list. 614282942Sarybchik * 615282942Sarybchik * We overload the csum_data field in the mbuf to keep track of this length 616282942Sarybchik * because there is no cheap alternative to avoid races. 617282942Sarybchik */ 618282942Sarybchikstatic int 619282942Sarybchiksfxge_tx_qdpl_put_unlocked(struct sfxge_txq *txq, struct mbuf *mbuf) 620282942Sarybchik{ 621282942Sarybchik struct sfxge_tx_dpl *stdp; 622282942Sarybchik volatile uintptr_t *putp; 623282942Sarybchik uintptr_t old; 624282942Sarybchik uintptr_t new; 625311028Sarybchik unsigned int put_count; 626227569Sphilip 627282942Sarybchik KASSERT(mbuf->m_nextpkt == NULL, ("mbuf->m_nextpkt != NULL")); 628282942Sarybchik 629282942Sarybchik SFXGE_TXQ_LOCK_ASSERT_NOTOWNED(txq); 630282942Sarybchik 631282942Sarybchik stdp = &txq->dpl; 632282942Sarybchik putp = &stdp->std_put; 633282942Sarybchik new = (uintptr_t)mbuf; 634282942Sarybchik 635282942Sarybchik do { 636282942Sarybchik old = *putp; 637282942Sarybchik if (old != 0) { 638282942Sarybchik struct mbuf *mp = (struct mbuf *)old; 639311028Sarybchik put_count = mp->m_pkthdr.csum_data; 640282942Sarybchik } else 641311028Sarybchik put_count = 0; 642311028Sarybchik if (put_count >= stdp->std_put_max) { 643282942Sarybchik atomic_add_long(&txq->put_overflow, 1); 644282942Sarybchik return (ENOBUFS); 645282942Sarybchik } 646311028Sarybchik mbuf->m_pkthdr.csum_data = put_count + 1; 647282942Sarybchik mbuf->m_nextpkt = (void *)old; 648282942Sarybchik } while (atomic_cmpset_ptr(putp, old, new) == 0); 649282942Sarybchik 650227569Sphilip return (0); 651227569Sphilip} 652227569Sphilip 653227569Sphilip/* 654227569Sphilip * Called from if_transmit - will try to grab the txq lock and enqueue to the 655279141Sarybchik * put list if it succeeds, otherwise try to push onto the defer list if space. 656227569Sphilip */ 657283514Sarybchikstatic int 658227569Sphilipsfxge_tx_packet_add(struct sfxge_txq *txq, struct mbuf *m) 659227569Sphilip{ 660227569Sphilip int rc; 661227569Sphilip 662263332Sglebius if (!SFXGE_LINK_UP(txq->sc)) { 663277895Sarybchik atomic_add_long(&txq->netdown_drops, 1); 664282998Sarybchik return (ENETDOWN); 665263332Sglebius } 666263332Sglebius 667227569Sphilip /* 668227569Sphilip * Try to grab the txq lock. If we are able to get the lock, 669227569Sphilip * the packet will be appended to the "get list" of the deferred 670227569Sphilip * packet list. Otherwise, it will be pushed on the "put list". 671227569Sphilip */ 672282997Sarybchik if (SFXGE_TXQ_TRYLOCK(txq)) { 673282942Sarybchik /* First swizzle put-list to get-list to keep order */ 674282942Sarybchik sfxge_tx_qdpl_swizzle(txq); 675282942Sarybchik 676282942Sarybchik rc = sfxge_tx_qdpl_put_locked(txq, m); 677282997Sarybchik 678282997Sarybchik /* Try to service the list. */ 679282997Sarybchik sfxge_tx_qdpl_service(txq); 680282997Sarybchik /* Lock has been dropped. */ 681282942Sarybchik } else { 682282942Sarybchik rc = sfxge_tx_qdpl_put_unlocked(txq, m); 683227569Sphilip 684282942Sarybchik /* 685282942Sarybchik * Try to grab the lock again. 686282942Sarybchik * 687282942Sarybchik * If we are able to get the lock, we need to process 688282942Sarybchik * the deferred packet list. If we are not able to get 689282942Sarybchik * the lock, another thread is processing the list. 690282942Sarybchik */ 691283048Sarybchik if ((rc == 0) && SFXGE_TXQ_TRYLOCK(txq)) { 692282997Sarybchik sfxge_tx_qdpl_service(txq); 693282997Sarybchik /* Lock has been dropped. */ 694282997Sarybchik } 695282942Sarybchik } 696227569Sphilip 697282997Sarybchik SFXGE_TXQ_LOCK_ASSERT_NOTOWNED(txq); 698227569Sphilip 699283048Sarybchik return (rc); 700227569Sphilip} 701227569Sphilip 702227569Sphilipstatic void 703227569Sphilipsfxge_tx_qdpl_flush(struct sfxge_txq *txq) 704227569Sphilip{ 705227569Sphilip struct sfxge_tx_dpl *stdp = &txq->dpl; 706227569Sphilip struct mbuf *mbuf, *next; 707227569Sphilip 708278221Sarybchik SFXGE_TXQ_LOCK(txq); 709227569Sphilip 710227569Sphilip sfxge_tx_qdpl_swizzle(txq); 711227569Sphilip for (mbuf = stdp->std_get; mbuf != NULL; mbuf = next) { 712227569Sphilip next = mbuf->m_nextpkt; 713227569Sphilip m_freem(mbuf); 714227569Sphilip } 715227569Sphilip stdp->std_get = NULL; 716272330Sgnn stdp->std_get_count = 0; 717277895Sarybchik stdp->std_get_non_tcp_count = 0; 718272325Sgnn stdp->std_getp = &stdp->std_get; 719227569Sphilip 720278221Sarybchik SFXGE_TXQ_UNLOCK(txq); 721227569Sphilip} 722227569Sphilip 723227569Sphilipvoid 724227569Sphilipsfxge_if_qflush(struct ifnet *ifp) 725227569Sphilip{ 726227569Sphilip struct sfxge_softc *sc; 727280433Sarybchik unsigned int i; 728227569Sphilip 729227569Sphilip sc = ifp->if_softc; 730227569Sphilip 731278938Sarybchik for (i = 0; i < sc->txq_count; i++) 732227569Sphilip sfxge_tx_qdpl_flush(sc->txq[i]); 733227569Sphilip} 734227569Sphilip 735291584Sarybchik#if SFXGE_TX_PARSE_EARLY 736291584Sarybchik 737291584Sarybchik/* There is little space for user data in mbuf pkthdr, so we 738291584Sarybchik * use l*hlen fields which are not used by the driver otherwise 739291584Sarybchik * to store header offsets. 740291584Sarybchik * The fields are 8-bit, but it's ok, no header may be longer than 255 bytes. 741291584Sarybchik */ 742291584Sarybchik 743291584Sarybchik 744291584Sarybchik#define TSO_MBUF_PROTO(_mbuf) ((_mbuf)->m_pkthdr.PH_loc.sixteen[0]) 745291584Sarybchik/* We abuse l5hlen here because PH_loc can hold only 64 bits of data */ 746291584Sarybchik#define TSO_MBUF_FLAGS(_mbuf) ((_mbuf)->m_pkthdr.l5hlen) 747291584Sarybchik#define TSO_MBUF_PACKETID(_mbuf) ((_mbuf)->m_pkthdr.PH_loc.sixteen[1]) 748291584Sarybchik#define TSO_MBUF_SEQNUM(_mbuf) ((_mbuf)->m_pkthdr.PH_loc.thirtytwo[1]) 749291584Sarybchik 750291584Sarybchikstatic void sfxge_parse_tx_packet(struct mbuf *mbuf) 751291584Sarybchik{ 752291584Sarybchik struct ether_header *eh = mtod(mbuf, struct ether_header *); 753291584Sarybchik const struct tcphdr *th; 754291584Sarybchik struct tcphdr th_copy; 755291584Sarybchik 756291584Sarybchik /* Find network protocol and header */ 757291584Sarybchik TSO_MBUF_PROTO(mbuf) = eh->ether_type; 758291584Sarybchik if (TSO_MBUF_PROTO(mbuf) == htons(ETHERTYPE_VLAN)) { 759291584Sarybchik struct ether_vlan_header *veh = 760291584Sarybchik mtod(mbuf, struct ether_vlan_header *); 761291584Sarybchik TSO_MBUF_PROTO(mbuf) = veh->evl_proto; 762291584Sarybchik mbuf->m_pkthdr.l2hlen = sizeof(*veh); 763291584Sarybchik } else { 764291584Sarybchik mbuf->m_pkthdr.l2hlen = sizeof(*eh); 765291584Sarybchik } 766291584Sarybchik 767291584Sarybchik /* Find TCP header */ 768291584Sarybchik if (TSO_MBUF_PROTO(mbuf) == htons(ETHERTYPE_IP)) { 769291584Sarybchik const struct ip *iph = (const struct ip *)mtodo(mbuf, mbuf->m_pkthdr.l2hlen); 770291584Sarybchik 771291584Sarybchik KASSERT(iph->ip_p == IPPROTO_TCP, 772291584Sarybchik ("TSO required on non-TCP packet")); 773291584Sarybchik mbuf->m_pkthdr.l3hlen = mbuf->m_pkthdr.l2hlen + 4 * iph->ip_hl; 774291584Sarybchik TSO_MBUF_PACKETID(mbuf) = iph->ip_id; 775291584Sarybchik } else { 776291584Sarybchik KASSERT(TSO_MBUF_PROTO(mbuf) == htons(ETHERTYPE_IPV6), 777291584Sarybchik ("TSO required on non-IP packet")); 778291584Sarybchik KASSERT(((const struct ip6_hdr *)mtodo(mbuf, mbuf->m_pkthdr.l2hlen))->ip6_nxt == 779291584Sarybchik IPPROTO_TCP, 780291584Sarybchik ("TSO required on non-TCP packet")); 781291584Sarybchik mbuf->m_pkthdr.l3hlen = mbuf->m_pkthdr.l2hlen + sizeof(struct ip6_hdr); 782291584Sarybchik TSO_MBUF_PACKETID(mbuf) = 0; 783291584Sarybchik } 784291584Sarybchik 785291584Sarybchik KASSERT(mbuf->m_len >= mbuf->m_pkthdr.l3hlen, 786291584Sarybchik ("network header is fragmented in mbuf")); 787291584Sarybchik 788291584Sarybchik /* We need TCP header including flags (window is the next) */ 789291584Sarybchik if (mbuf->m_len < mbuf->m_pkthdr.l3hlen + offsetof(struct tcphdr, th_win)) { 790291584Sarybchik m_copydata(mbuf, mbuf->m_pkthdr.l3hlen, sizeof(th_copy), 791291584Sarybchik (caddr_t)&th_copy); 792291584Sarybchik th = &th_copy; 793291584Sarybchik } else { 794291584Sarybchik th = (const struct tcphdr *)mtodo(mbuf, mbuf->m_pkthdr.l3hlen); 795291584Sarybchik } 796291584Sarybchik 797291584Sarybchik mbuf->m_pkthdr.l4hlen = mbuf->m_pkthdr.l3hlen + 4 * th->th_off; 798291584Sarybchik TSO_MBUF_SEQNUM(mbuf) = ntohl(th->th_seq); 799291584Sarybchik 800291584Sarybchik /* These flags must not be duplicated */ 801291584Sarybchik /* 802291584Sarybchik * RST should not be duplicated as well, but FreeBSD kernel 803291584Sarybchik * generates TSO packets with RST flag. So, do not assert 804291584Sarybchik * its absence. 805291584Sarybchik */ 806291584Sarybchik KASSERT(!(th->th_flags & (TH_URG | TH_SYN)), 807291584Sarybchik ("incompatible TCP flag 0x%x on TSO packet", 808291584Sarybchik th->th_flags & (TH_URG | TH_SYN))); 809291584Sarybchik TSO_MBUF_FLAGS(mbuf) = th->th_flags; 810291584Sarybchik} 811291584Sarybchik#endif 812291584Sarybchik 813227569Sphilip/* 814227569Sphilip * TX start -- called by the stack. 815227569Sphilip */ 816227569Sphilipint 817227569Sphilipsfxge_if_transmit(struct ifnet *ifp, struct mbuf *m) 818272325Sgnn{ 819227569Sphilip struct sfxge_softc *sc; 820227569Sphilip struct sfxge_txq *txq; 821227569Sphilip int rc; 822227569Sphilip 823227569Sphilip sc = (struct sfxge_softc *)ifp->if_softc; 824227569Sphilip 825280374Sarybchik /* 826280374Sarybchik * Transmit may be called when interface is up from the kernel 827280374Sarybchik * point of view, but not yet up (in progress) from the driver 828280374Sarybchik * point of view. I.e. link aggregation bring up. 829280374Sarybchik * Transmit may be called when interface is up from the driver 830280374Sarybchik * point of view, but already down from the kernel point of 831280374Sarybchik * view. I.e. Rx when interface shutdown is in progress. 832280374Sarybchik */ 833280374Sarybchik KASSERT((ifp->if_flags & IFF_UP) || (sc->if_flags & IFF_UP), 834280374Sarybchik ("interface not up")); 835227569Sphilip 836227569Sphilip /* Pick the desired transmit queue. */ 837282903Sarybchik if (m->m_pkthdr.csum_flags & 838282903Sarybchik (CSUM_DELAY_DATA | CSUM_TCP_IPV6 | CSUM_UDP_IPV6 | CSUM_TSO)) { 839227569Sphilip int index = 0; 840227569Sphilip 841301494Sarybchik#ifdef RSS 842301494Sarybchik uint32_t bucket_id; 843301494Sarybchik 844301494Sarybchik /* 845301494Sarybchik * Select a TX queue which matches the corresponding 846301494Sarybchik * RX queue for the hash in order to assign both 847301494Sarybchik * TX and RX parts of the flow to the same CPU 848301494Sarybchik */ 849301494Sarybchik if (rss_m2bucket(m, &bucket_id) == 0) 850301494Sarybchik index = bucket_id % (sc->txq_count - (SFXGE_TXQ_NTYPES - 1)); 851301494Sarybchik#else 852275358Shselasky /* check if flowid is set */ 853275358Shselasky if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) { 854227569Sphilip uint32_t hash = m->m_pkthdr.flowid; 855310831Sarybchik uint32_t idx = hash % nitems(sc->rx_indir_table); 856227569Sphilip 857310831Sarybchik index = sc->rx_indir_table[idx]; 858227569Sphilip } 859301494Sarybchik#endif 860291584Sarybchik#if SFXGE_TX_PARSE_EARLY 861291584Sarybchik if (m->m_pkthdr.csum_flags & CSUM_TSO) 862291584Sarybchik sfxge_parse_tx_packet(m); 863291584Sarybchik#endif 864227569Sphilip txq = sc->txq[SFXGE_TXQ_IP_TCP_UDP_CKSUM + index]; 865227569Sphilip } else if (m->m_pkthdr.csum_flags & CSUM_DELAY_IP) { 866227569Sphilip txq = sc->txq[SFXGE_TXQ_IP_CKSUM]; 867227569Sphilip } else { 868227569Sphilip txq = sc->txq[SFXGE_TXQ_NON_CKSUM]; 869227569Sphilip } 870227569Sphilip 871227569Sphilip rc = sfxge_tx_packet_add(txq, m); 872282998Sarybchik if (rc != 0) 873282998Sarybchik m_freem(m); 874227569Sphilip 875227569Sphilip return (rc); 876227569Sphilip} 877227569Sphilip 878227569Sphilip/* 879227569Sphilip * Software "TSO". Not quite as good as doing it in hardware, but 880227569Sphilip * still faster than segmenting in the stack. 881227569Sphilip */ 882227569Sphilip 883227569Sphilipstruct sfxge_tso_state { 884227569Sphilip /* Output position */ 885227569Sphilip unsigned out_len; /* Remaining length in current segment */ 886227569Sphilip unsigned seqnum; /* Current sequence number */ 887227569Sphilip unsigned packet_space; /* Remaining space in current packet */ 888294077Sarybchik unsigned segs_space; /* Remaining number of DMA segments 889294077Sarybchik for the packet (FATSOv2 only) */ 890227569Sphilip 891227569Sphilip /* Input position */ 892227569Sphilip uint64_t dma_addr; /* DMA address of current position */ 893227569Sphilip unsigned in_len; /* Remaining length in current mbuf */ 894227569Sphilip 895227569Sphilip const struct mbuf *mbuf; /* Input mbuf (head of chain) */ 896227569Sphilip u_short protocol; /* Network protocol (after VLAN decap) */ 897227569Sphilip ssize_t nh_off; /* Offset of network header */ 898227569Sphilip ssize_t tcph_off; /* Offset of TCP header */ 899227569Sphilip unsigned header_len; /* Number of bytes of header */ 900278937Sarybchik unsigned seg_size; /* TCP segment size */ 901283514Sarybchik int fw_assisted; /* Use FW-assisted TSO */ 902283514Sarybchik u_short packet_id; /* IPv4 packet ID from the original packet */ 903291584Sarybchik uint8_t tcp_flags; /* TCP flags */ 904283514Sarybchik efx_desc_t header_desc; /* Precomputed header descriptor for 905283514Sarybchik * FW-assisted TSO */ 906227569Sphilip}; 907227569Sphilip 908291584Sarybchik#if !SFXGE_TX_PARSE_EARLY 909278837Sarybchikstatic const struct ip *tso_iph(const struct sfxge_tso_state *tso) 910227569Sphilip{ 911227569Sphilip KASSERT(tso->protocol == htons(ETHERTYPE_IP), 912227569Sphilip ("tso_iph() in non-IPv4 state")); 913227569Sphilip return (const struct ip *)(tso->mbuf->m_data + tso->nh_off); 914227569Sphilip} 915291584Sarybchik 916278837Sarybchikstatic __unused const struct ip6_hdr *tso_ip6h(const struct sfxge_tso_state *tso) 917227569Sphilip{ 918227569Sphilip KASSERT(tso->protocol == htons(ETHERTYPE_IPV6), 919227569Sphilip ("tso_ip6h() in non-IPv6 state")); 920227569Sphilip return (const struct ip6_hdr *)(tso->mbuf->m_data + tso->nh_off); 921227569Sphilip} 922291584Sarybchik 923278837Sarybchikstatic const struct tcphdr *tso_tcph(const struct sfxge_tso_state *tso) 924227569Sphilip{ 925227569Sphilip return (const struct tcphdr *)(tso->mbuf->m_data + tso->tcph_off); 926227569Sphilip} 927291584Sarybchik#endif 928227569Sphilip 929291584Sarybchik 930227569Sphilip/* Size of preallocated TSO header buffers. Larger blocks must be 931227569Sphilip * allocated from the heap. 932227569Sphilip */ 933272325Sgnn#define TSOH_STD_SIZE 128 934227569Sphilip 935227569Sphilip/* At most half the descriptors in the queue at any time will refer to 936227569Sphilip * a TSO header buffer, since they must always be followed by a 937227569Sphilip * payload descriptor referring to an mbuf. 938227569Sphilip */ 939272328Sgnn#define TSOH_COUNT(_txq_entries) ((_txq_entries) / 2u) 940272325Sgnn#define TSOH_PER_PAGE (PAGE_SIZE / TSOH_STD_SIZE) 941272328Sgnn#define TSOH_PAGE_COUNT(_txq_entries) \ 942298646Spfg howmany(TSOH_COUNT(_txq_entries), TSOH_PER_PAGE) 943227569Sphilip 944227569Sphilipstatic int tso_init(struct sfxge_txq *txq) 945227569Sphilip{ 946227569Sphilip struct sfxge_softc *sc = txq->sc; 947272328Sgnn unsigned int tsoh_page_count = TSOH_PAGE_COUNT(sc->txq_entries); 948227569Sphilip int i, rc; 949227569Sphilip 950227569Sphilip /* Allocate TSO header buffers */ 951272328Sgnn txq->tsoh_buffer = malloc(tsoh_page_count * sizeof(txq->tsoh_buffer[0]), 952227569Sphilip M_SFXGE, M_WAITOK); 953227569Sphilip 954272328Sgnn for (i = 0; i < tsoh_page_count; i++) { 955227569Sphilip rc = sfxge_dma_alloc(sc, PAGE_SIZE, &txq->tsoh_buffer[i]); 956272325Sgnn if (rc != 0) 957227569Sphilip goto fail; 958227569Sphilip } 959227569Sphilip 960272325Sgnn return (0); 961227569Sphilip 962227569Sphilipfail: 963227569Sphilip while (i-- > 0) 964227569Sphilip sfxge_dma_free(&txq->tsoh_buffer[i]); 965227569Sphilip free(txq->tsoh_buffer, M_SFXGE); 966227569Sphilip txq->tsoh_buffer = NULL; 967272325Sgnn return (rc); 968227569Sphilip} 969227569Sphilip 970227569Sphilipstatic void tso_fini(struct sfxge_txq *txq) 971227569Sphilip{ 972227569Sphilip int i; 973227569Sphilip 974272325Sgnn if (txq->tsoh_buffer != NULL) { 975272328Sgnn for (i = 0; i < TSOH_PAGE_COUNT(txq->sc->txq_entries); i++) 976227569Sphilip sfxge_dma_free(&txq->tsoh_buffer[i]); 977227569Sphilip free(txq->tsoh_buffer, M_SFXGE); 978227569Sphilip } 979227569Sphilip} 980227569Sphilip 981283514Sarybchikstatic void tso_start(struct sfxge_txq *txq, struct sfxge_tso_state *tso, 982283514Sarybchik const bus_dma_segment_t *hdr_dma_seg, 983283514Sarybchik struct mbuf *mbuf) 984227569Sphilip{ 985291584Sarybchik const efx_nic_cfg_t *encp = efx_nic_cfg_get(txq->sc->enp); 986291584Sarybchik#if !SFXGE_TX_PARSE_EARLY 987227569Sphilip struct ether_header *eh = mtod(mbuf, struct ether_header *); 988279046Sarybchik const struct tcphdr *th; 989279046Sarybchik struct tcphdr th_copy; 990291584Sarybchik#endif 991227569Sphilip 992294077Sarybchik tso->fw_assisted = txq->tso_fw_assisted; 993227569Sphilip tso->mbuf = mbuf; 994227569Sphilip 995227569Sphilip /* Find network protocol and header */ 996291584Sarybchik#if !SFXGE_TX_PARSE_EARLY 997227569Sphilip tso->protocol = eh->ether_type; 998227569Sphilip if (tso->protocol == htons(ETHERTYPE_VLAN)) { 999227569Sphilip struct ether_vlan_header *veh = 1000227569Sphilip mtod(mbuf, struct ether_vlan_header *); 1001227569Sphilip tso->protocol = veh->evl_proto; 1002227569Sphilip tso->nh_off = sizeof(*veh); 1003227569Sphilip } else { 1004227569Sphilip tso->nh_off = sizeof(*eh); 1005227569Sphilip } 1006291584Sarybchik#else 1007291584Sarybchik tso->protocol = TSO_MBUF_PROTO(mbuf); 1008291584Sarybchik tso->nh_off = mbuf->m_pkthdr.l2hlen; 1009291584Sarybchik tso->tcph_off = mbuf->m_pkthdr.l3hlen; 1010301607Sarybchik tso->packet_id = ntohs(TSO_MBUF_PACKETID(mbuf)); 1011291584Sarybchik#endif 1012227569Sphilip 1013291584Sarybchik#if !SFXGE_TX_PARSE_EARLY 1014227569Sphilip /* Find TCP header */ 1015227569Sphilip if (tso->protocol == htons(ETHERTYPE_IP)) { 1016227569Sphilip KASSERT(tso_iph(tso)->ip_p == IPPROTO_TCP, 1017227569Sphilip ("TSO required on non-TCP packet")); 1018227569Sphilip tso->tcph_off = tso->nh_off + 4 * tso_iph(tso)->ip_hl; 1019301607Sarybchik tso->packet_id = ntohs(tso_iph(tso)->ip_id); 1020227569Sphilip } else { 1021227569Sphilip KASSERT(tso->protocol == htons(ETHERTYPE_IPV6), 1022227569Sphilip ("TSO required on non-IP packet")); 1023227569Sphilip KASSERT(tso_ip6h(tso)->ip6_nxt == IPPROTO_TCP, 1024227569Sphilip ("TSO required on non-TCP packet")); 1025227569Sphilip tso->tcph_off = tso->nh_off + sizeof(struct ip6_hdr); 1026283514Sarybchik tso->packet_id = 0; 1027227569Sphilip } 1028291584Sarybchik#endif 1029291584Sarybchik 1030291584Sarybchik 1031283514Sarybchik if (tso->fw_assisted && 1032283514Sarybchik __predict_false(tso->tcph_off > 1033283514Sarybchik encp->enc_tx_tso_tcp_header_offset_limit)) { 1034283514Sarybchik tso->fw_assisted = 0; 1035283514Sarybchik } 1036227569Sphilip 1037291584Sarybchik 1038291584Sarybchik#if !SFXGE_TX_PARSE_EARLY 1039279046Sarybchik KASSERT(mbuf->m_len >= tso->tcph_off, 1040279046Sarybchik ("network header is fragmented in mbuf")); 1041279046Sarybchik /* We need TCP header including flags (window is the next) */ 1042279046Sarybchik if (mbuf->m_len < tso->tcph_off + offsetof(struct tcphdr, th_win)) { 1043279046Sarybchik m_copydata(tso->mbuf, tso->tcph_off, sizeof(th_copy), 1044279046Sarybchik (caddr_t)&th_copy); 1045279046Sarybchik th = &th_copy; 1046279046Sarybchik } else { 1047279046Sarybchik th = tso_tcph(tso); 1048279046Sarybchik } 1049279046Sarybchik tso->header_len = tso->tcph_off + 4 * th->th_off; 1050291584Sarybchik#else 1051291584Sarybchik tso->header_len = mbuf->m_pkthdr.l4hlen; 1052291584Sarybchik#endif 1053278937Sarybchik tso->seg_size = mbuf->m_pkthdr.tso_segsz; 1054227569Sphilip 1055291584Sarybchik#if !SFXGE_TX_PARSE_EARLY 1056279046Sarybchik tso->seqnum = ntohl(th->th_seq); 1057227569Sphilip 1058227569Sphilip /* These flags must not be duplicated */ 1059283278Sarybchik /* 1060283278Sarybchik * RST should not be duplicated as well, but FreeBSD kernel 1061283278Sarybchik * generates TSO packets with RST flag. So, do not assert 1062283278Sarybchik * its absence. 1063283278Sarybchik */ 1064283278Sarybchik KASSERT(!(th->th_flags & (TH_URG | TH_SYN)), 1065283278Sarybchik ("incompatible TCP flag 0x%x on TSO packet", 1066283278Sarybchik th->th_flags & (TH_URG | TH_SYN))); 1067291584Sarybchik tso->tcp_flags = th->th_flags; 1068291584Sarybchik#else 1069291584Sarybchik tso->seqnum = TSO_MBUF_SEQNUM(mbuf); 1070291584Sarybchik tso->tcp_flags = TSO_MBUF_FLAGS(mbuf); 1071291584Sarybchik#endif 1072227569Sphilip 1073227569Sphilip tso->out_len = mbuf->m_pkthdr.len - tso->header_len; 1074283514Sarybchik 1075283514Sarybchik if (tso->fw_assisted) { 1076283514Sarybchik if (hdr_dma_seg->ds_len >= tso->header_len) 1077283514Sarybchik efx_tx_qdesc_dma_create(txq->common, 1078283514Sarybchik hdr_dma_seg->ds_addr, 1079283514Sarybchik tso->header_len, 1080283514Sarybchik B_FALSE, 1081283514Sarybchik &tso->header_desc); 1082283514Sarybchik else 1083283514Sarybchik tso->fw_assisted = 0; 1084283514Sarybchik } 1085227569Sphilip} 1086227569Sphilip 1087227569Sphilip/* 1088227569Sphilip * tso_fill_packet_with_fragment - form descriptors for the current fragment 1089227569Sphilip * 1090227569Sphilip * Form descriptors for the current fragment, until we reach the end 1091227569Sphilip * of fragment or end-of-packet. Return 0 on success, 1 if not enough 1092227569Sphilip * space. 1093227569Sphilip */ 1094227569Sphilipstatic void tso_fill_packet_with_fragment(struct sfxge_txq *txq, 1095227569Sphilip struct sfxge_tso_state *tso) 1096227569Sphilip{ 1097283514Sarybchik efx_desc_t *desc; 1098227569Sphilip int n; 1099294077Sarybchik uint64_t dma_addr = tso->dma_addr; 1100294077Sarybchik boolean_t eop; 1101227569Sphilip 1102227569Sphilip if (tso->in_len == 0 || tso->packet_space == 0) 1103227569Sphilip return; 1104227569Sphilip 1105227569Sphilip KASSERT(tso->in_len > 0, ("TSO input length went negative")); 1106227569Sphilip KASSERT(tso->packet_space > 0, ("TSO packet space went negative")); 1107227569Sphilip 1108294077Sarybchik if (tso->fw_assisted & SFXGE_FATSOV2) { 1109294077Sarybchik n = tso->in_len; 1110294077Sarybchik tso->out_len -= n; 1111294077Sarybchik tso->seqnum += n; 1112294077Sarybchik tso->in_len = 0; 1113294077Sarybchik if (n < tso->packet_space) { 1114294077Sarybchik tso->packet_space -= n; 1115294077Sarybchik tso->segs_space--; 1116294077Sarybchik } else { 1117294077Sarybchik tso->packet_space = tso->seg_size - 1118294077Sarybchik (n - tso->packet_space) % tso->seg_size; 1119294077Sarybchik tso->segs_space = 1120294077Sarybchik EFX_TX_FATSOV2_DMA_SEGS_PER_PKT_MAX - 1 - 1121294077Sarybchik (tso->packet_space != tso->seg_size); 1122294077Sarybchik } 1123294077Sarybchik } else { 1124294077Sarybchik n = min(tso->in_len, tso->packet_space); 1125294077Sarybchik tso->packet_space -= n; 1126294077Sarybchik tso->out_len -= n; 1127294077Sarybchik tso->dma_addr += n; 1128294077Sarybchik tso->in_len -= n; 1129294077Sarybchik } 1130227569Sphilip 1131294077Sarybchik /* 1132294077Sarybchik * It is OK to use binary OR below to avoid extra branching 1133294077Sarybchik * since all conditions may always be checked. 1134294077Sarybchik */ 1135294077Sarybchik eop = (tso->out_len == 0) | (tso->packet_space == 0) | 1136294077Sarybchik (tso->segs_space == 0); 1137227569Sphilip 1138227569Sphilip desc = &txq->pend_desc[txq->n_pend_desc++]; 1139294077Sarybchik efx_tx_qdesc_dma_create(txq->common, dma_addr, n, eop, desc); 1140227569Sphilip} 1141227569Sphilip 1142227569Sphilip/* Callback from bus_dmamap_load() for long TSO headers. */ 1143227569Sphilipstatic void tso_map_long_header(void *dma_addr_ret, 1144227569Sphilip bus_dma_segment_t *segs, int nseg, 1145227569Sphilip int error) 1146227569Sphilip{ 1147227569Sphilip *(uint64_t *)dma_addr_ret = ((__predict_true(error == 0) && 1148227569Sphilip __predict_true(nseg == 1)) ? 1149227569Sphilip segs->ds_addr : 0); 1150227569Sphilip} 1151227569Sphilip 1152227569Sphilip/* 1153227569Sphilip * tso_start_new_packet - generate a new header and prepare for the new packet 1154227569Sphilip * 1155227569Sphilip * Generate a new header and prepare for the new packet. Return 0 on 1156227569Sphilip * success, or an error code if failed to alloc header. 1157227569Sphilip */ 1158227569Sphilipstatic int tso_start_new_packet(struct sfxge_txq *txq, 1159227569Sphilip struct sfxge_tso_state *tso, 1160283514Sarybchik unsigned int *idp) 1161227569Sphilip{ 1162283514Sarybchik unsigned int id = *idp; 1163227569Sphilip struct tcphdr *tsoh_th; 1164227569Sphilip unsigned ip_length; 1165227569Sphilip caddr_t header; 1166227569Sphilip uint64_t dma_addr; 1167227569Sphilip bus_dmamap_t map; 1168283514Sarybchik efx_desc_t *desc; 1169227569Sphilip int rc; 1170227569Sphilip 1171283514Sarybchik if (tso->fw_assisted) { 1172294077Sarybchik if (tso->fw_assisted & SFXGE_FATSOV2) { 1173294077Sarybchik /* Add 2 FATSOv2 option descriptors */ 1174294077Sarybchik desc = &txq->pend_desc[txq->n_pend_desc]; 1175294077Sarybchik efx_tx_qdesc_tso2_create(txq->common, 1176294077Sarybchik tso->packet_id, 1177294077Sarybchik tso->seqnum, 1178294077Sarybchik tso->seg_size, 1179294077Sarybchik desc, 1180294077Sarybchik EFX_TX_FATSOV2_OPT_NDESCS); 1181294077Sarybchik desc += EFX_TX_FATSOV2_OPT_NDESCS; 1182294077Sarybchik txq->n_pend_desc += EFX_TX_FATSOV2_OPT_NDESCS; 1183294077Sarybchik KASSERT(txq->stmp[id].flags == 0, ("stmp flags are not 0")); 1184294077Sarybchik id = (id + EFX_TX_FATSOV2_OPT_NDESCS) & txq->ptr_mask; 1185227569Sphilip 1186294077Sarybchik tso->segs_space = 1187294077Sarybchik EFX_TX_FATSOV2_DMA_SEGS_PER_PKT_MAX - 1; 1188294077Sarybchik } else { 1189294077Sarybchik uint8_t tcp_flags = tso->tcp_flags; 1190227569Sphilip 1191294077Sarybchik if (tso->out_len > tso->seg_size) 1192294077Sarybchik tcp_flags &= ~(TH_FIN | TH_PUSH); 1193283514Sarybchik 1194294077Sarybchik /* Add FATSOv1 option descriptor */ 1195294077Sarybchik desc = &txq->pend_desc[txq->n_pend_desc++]; 1196294077Sarybchik efx_tx_qdesc_tso_create(txq->common, 1197294077Sarybchik tso->packet_id, 1198294077Sarybchik tso->seqnum, 1199294077Sarybchik tcp_flags, 1200294077Sarybchik desc++); 1201294077Sarybchik KASSERT(txq->stmp[id].flags == 0, ("stmp flags are not 0")); 1202294077Sarybchik id = (id + 1) & txq->ptr_mask; 1203294077Sarybchik 1204294077Sarybchik tso->seqnum += tso->seg_size; 1205294077Sarybchik tso->segs_space = UINT_MAX; 1206294077Sarybchik } 1207294077Sarybchik 1208283514Sarybchik /* Header DMA descriptor */ 1209283514Sarybchik *desc = tso->header_desc; 1210283514Sarybchik txq->n_pend_desc++; 1211283514Sarybchik KASSERT(txq->stmp[id].flags == 0, ("stmp flags are not 0")); 1212283514Sarybchik id = (id + 1) & txq->ptr_mask; 1213227569Sphilip } else { 1214283514Sarybchik /* Allocate a DMA-mapped header buffer. */ 1215283514Sarybchik if (__predict_true(tso->header_len <= TSOH_STD_SIZE)) { 1216283514Sarybchik unsigned int page_index = (id / 2) / TSOH_PER_PAGE; 1217283514Sarybchik unsigned int buf_index = (id / 2) % TSOH_PER_PAGE; 1218283514Sarybchik 1219283514Sarybchik header = (txq->tsoh_buffer[page_index].esm_base + 1220283514Sarybchik buf_index * TSOH_STD_SIZE); 1221283514Sarybchik dma_addr = (txq->tsoh_buffer[page_index].esm_addr + 1222283514Sarybchik buf_index * TSOH_STD_SIZE); 1223283514Sarybchik map = txq->tsoh_buffer[page_index].esm_map; 1224283514Sarybchik 1225283514Sarybchik KASSERT(txq->stmp[id].flags == 0, 1226283514Sarybchik ("stmp flags are not 0")); 1227283514Sarybchik } else { 1228283514Sarybchik struct sfxge_tx_mapping *stmp = &txq->stmp[id]; 1229283514Sarybchik 1230283514Sarybchik /* We cannot use bus_dmamem_alloc() as that may sleep */ 1231283514Sarybchik header = malloc(tso->header_len, M_SFXGE, M_NOWAIT); 1232283514Sarybchik if (__predict_false(!header)) 1233283514Sarybchik return (ENOMEM); 1234283514Sarybchik rc = bus_dmamap_load(txq->packet_dma_tag, stmp->map, 1235283514Sarybchik header, tso->header_len, 1236283514Sarybchik tso_map_long_header, &dma_addr, 1237283514Sarybchik BUS_DMA_NOWAIT); 1238283514Sarybchik if (__predict_false(dma_addr == 0)) { 1239283514Sarybchik if (rc == 0) { 1240283514Sarybchik /* Succeeded but got >1 segment */ 1241283514Sarybchik bus_dmamap_unload(txq->packet_dma_tag, 1242283514Sarybchik stmp->map); 1243283514Sarybchik rc = EINVAL; 1244283514Sarybchik } 1245283514Sarybchik free(header, M_SFXGE); 1246283514Sarybchik return (rc); 1247227569Sphilip } 1248283514Sarybchik map = stmp->map; 1249283514Sarybchik 1250283514Sarybchik txq->tso_long_headers++; 1251283514Sarybchik stmp->u.heap_buf = header; 1252283514Sarybchik stmp->flags = TX_BUF_UNMAP; 1253227569Sphilip } 1254227569Sphilip 1255283514Sarybchik tsoh_th = (struct tcphdr *)(header + tso->tcph_off); 1256227569Sphilip 1257283514Sarybchik /* Copy and update the headers. */ 1258283514Sarybchik m_copydata(tso->mbuf, 0, tso->header_len, header); 1259227569Sphilip 1260283514Sarybchik tsoh_th->th_seq = htonl(tso->seqnum); 1261283514Sarybchik tso->seqnum += tso->seg_size; 1262283514Sarybchik if (tso->out_len > tso->seg_size) { 1263283514Sarybchik /* This packet will not finish the TSO burst. */ 1264283514Sarybchik ip_length = tso->header_len - tso->nh_off + tso->seg_size; 1265283514Sarybchik tsoh_th->th_flags &= ~(TH_FIN | TH_PUSH); 1266283514Sarybchik } else { 1267283514Sarybchik /* This packet will be the last in the TSO burst. */ 1268283514Sarybchik ip_length = tso->header_len - tso->nh_off + tso->out_len; 1269283514Sarybchik } 1270227569Sphilip 1271283514Sarybchik if (tso->protocol == htons(ETHERTYPE_IP)) { 1272283514Sarybchik struct ip *tsoh_iph = (struct ip *)(header + tso->nh_off); 1273283514Sarybchik tsoh_iph->ip_len = htons(ip_length); 1274283514Sarybchik /* XXX We should increment ip_id, but FreeBSD doesn't 1275283514Sarybchik * currently allocate extra IDs for multiple segments. 1276283514Sarybchik */ 1277283514Sarybchik } else { 1278283514Sarybchik struct ip6_hdr *tsoh_iph = 1279283514Sarybchik (struct ip6_hdr *)(header + tso->nh_off); 1280283514Sarybchik tsoh_iph->ip6_plen = htons(ip_length - sizeof(*tsoh_iph)); 1281283514Sarybchik } 1282227569Sphilip 1283283514Sarybchik /* Make the header visible to the hardware. */ 1284283514Sarybchik bus_dmamap_sync(txq->packet_dma_tag, map, BUS_DMASYNC_PREWRITE); 1285283514Sarybchik 1286283514Sarybchik /* Form a descriptor for this header. */ 1287283514Sarybchik desc = &txq->pend_desc[txq->n_pend_desc++]; 1288283514Sarybchik efx_tx_qdesc_dma_create(txq->common, 1289283514Sarybchik dma_addr, 1290283514Sarybchik tso->header_len, 1291283514Sarybchik 0, 1292283514Sarybchik desc); 1293283514Sarybchik id = (id + 1) & txq->ptr_mask; 1294294077Sarybchik 1295294077Sarybchik tso->segs_space = UINT_MAX; 1296227569Sphilip } 1297278937Sarybchik tso->packet_space = tso->seg_size; 1298227569Sphilip txq->tso_packets++; 1299283514Sarybchik *idp = id; 1300227569Sphilip 1301272325Sgnn return (0); 1302227569Sphilip} 1303227569Sphilip 1304227569Sphilipstatic int 1305227569Sphilipsfxge_tx_queue_tso(struct sfxge_txq *txq, struct mbuf *mbuf, 1306283514Sarybchik const bus_dma_segment_t *dma_seg, int n_dma_seg, 1307283514Sarybchik int vlan_tagged) 1308227569Sphilip{ 1309227569Sphilip struct sfxge_tso_state tso; 1310283514Sarybchik unsigned int id; 1311277888Sarybchik unsigned skipped = 0; 1312227569Sphilip 1313283514Sarybchik tso_start(txq, &tso, dma_seg, mbuf); 1314227569Sphilip 1315277888Sarybchik while (dma_seg->ds_len + skipped <= tso.header_len) { 1316277888Sarybchik skipped += dma_seg->ds_len; 1317227569Sphilip --n_dma_seg; 1318227569Sphilip KASSERT(n_dma_seg, ("no payload found in TSO packet")); 1319227569Sphilip ++dma_seg; 1320227569Sphilip } 1321280807Sarybchik tso.in_len = dma_seg->ds_len - (tso.header_len - skipped); 1322277888Sarybchik tso.dma_addr = dma_seg->ds_addr + (tso.header_len - skipped); 1323227569Sphilip 1324283514Sarybchik id = (txq->added + vlan_tagged) & txq->ptr_mask; 1325283514Sarybchik if (__predict_false(tso_start_new_packet(txq, &tso, &id))) 1326272328Sgnn return (-1); 1327227569Sphilip 1328227569Sphilip while (1) { 1329283514Sarybchik tso_fill_packet_with_fragment(txq, &tso); 1330283514Sarybchik /* Exactly one DMA descriptor is added */ 1331283514Sarybchik KASSERT(txq->stmp[id].flags == 0, ("stmp flags are not 0")); 1332272328Sgnn id = (id + 1) & txq->ptr_mask; 1333227569Sphilip 1334227569Sphilip /* Move onto the next fragment? */ 1335227569Sphilip if (tso.in_len == 0) { 1336227569Sphilip --n_dma_seg; 1337227569Sphilip if (n_dma_seg == 0) 1338227569Sphilip break; 1339227569Sphilip ++dma_seg; 1340227569Sphilip tso.in_len = dma_seg->ds_len; 1341227569Sphilip tso.dma_addr = dma_seg->ds_addr; 1342227569Sphilip } 1343227569Sphilip 1344227569Sphilip /* End of packet? */ 1345294077Sarybchik if ((tso.packet_space == 0) | (tso.segs_space == 0)) { 1346294077Sarybchik unsigned int n_fatso_opt_desc = 1347294077Sarybchik (tso.fw_assisted & SFXGE_FATSOV2) ? 1348294077Sarybchik EFX_TX_FATSOV2_OPT_NDESCS : 1349294077Sarybchik (tso.fw_assisted & SFXGE_FATSOV1) ? 1 : 0; 1350294077Sarybchik 1351227569Sphilip /* If the queue is now full due to tiny MSS, 1352227569Sphilip * or we can't create another header, discard 1353227569Sphilip * the remainder of the input mbuf but do not 1354227569Sphilip * roll back the work we have done. 1355227569Sphilip */ 1356294077Sarybchik if (txq->n_pend_desc + n_fatso_opt_desc + 1357294077Sarybchik 1 /* header */ + n_dma_seg > txq->max_pkt_desc) { 1358278255Sarybchik txq->tso_pdrop_too_many++; 1359227569Sphilip break; 1360278255Sarybchik } 1361227569Sphilip if (__predict_false(tso_start_new_packet(txq, &tso, 1362283514Sarybchik &id))) { 1363278255Sarybchik txq->tso_pdrop_no_rsrc++; 1364227569Sphilip break; 1365278255Sarybchik } 1366227569Sphilip } 1367227569Sphilip } 1368227569Sphilip 1369227569Sphilip txq->tso_bursts++; 1370272325Sgnn return (id); 1371227569Sphilip} 1372227569Sphilip 1373227569Sphilipstatic void 1374227569Sphilipsfxge_tx_qunblock(struct sfxge_txq *txq) 1375227569Sphilip{ 1376227569Sphilip struct sfxge_softc *sc; 1377227569Sphilip struct sfxge_evq *evq; 1378227569Sphilip 1379227569Sphilip sc = txq->sc; 1380227569Sphilip evq = sc->evq[txq->evq_index]; 1381227569Sphilip 1382278221Sarybchik SFXGE_EVQ_LOCK_ASSERT_OWNED(evq); 1383227569Sphilip 1384279351Sarybchik if (__predict_false(txq->init_state != SFXGE_TXQ_STARTED)) 1385227569Sphilip return; 1386227569Sphilip 1387278221Sarybchik SFXGE_TXQ_LOCK(txq); 1388227569Sphilip 1389227569Sphilip if (txq->blocked) { 1390227569Sphilip unsigned int level; 1391227569Sphilip 1392227569Sphilip level = txq->added - txq->completed; 1393279080Sarybchik if (level <= SFXGE_TXQ_UNBLOCK_LEVEL(txq->entries)) { 1394279080Sarybchik /* reaped must be in sync with blocked */ 1395279080Sarybchik sfxge_tx_qreap(txq); 1396227569Sphilip txq->blocked = 0; 1397279080Sarybchik } 1398227569Sphilip } 1399227569Sphilip 1400227569Sphilip sfxge_tx_qdpl_service(txq); 1401227569Sphilip /* note: lock has been dropped */ 1402227569Sphilip} 1403227569Sphilip 1404227569Sphilipvoid 1405227569Sphilipsfxge_tx_qflush_done(struct sfxge_txq *txq) 1406227569Sphilip{ 1407227569Sphilip 1408227569Sphilip txq->flush_state = SFXGE_FLUSH_DONE; 1409227569Sphilip} 1410227569Sphilip 1411227569Sphilipstatic void 1412227569Sphilipsfxge_tx_qstop(struct sfxge_softc *sc, unsigned int index) 1413227569Sphilip{ 1414227569Sphilip struct sfxge_txq *txq; 1415227569Sphilip struct sfxge_evq *evq; 1416227569Sphilip unsigned int count; 1417227569Sphilip 1418283514Sarybchik SFXGE_ADAPTER_LOCK_ASSERT_OWNED(sc); 1419283514Sarybchik 1420227569Sphilip txq = sc->txq[index]; 1421227569Sphilip evq = sc->evq[txq->evq_index]; 1422227569Sphilip 1423283514Sarybchik SFXGE_EVQ_LOCK(evq); 1424278221Sarybchik SFXGE_TXQ_LOCK(txq); 1425227569Sphilip 1426227569Sphilip KASSERT(txq->init_state == SFXGE_TXQ_STARTED, 1427227569Sphilip ("txq->init_state != SFXGE_TXQ_STARTED")); 1428227569Sphilip 1429227569Sphilip txq->init_state = SFXGE_TXQ_INITIALIZED; 1430227569Sphilip 1431283514Sarybchik if (txq->flush_state != SFXGE_FLUSH_DONE) { 1432283514Sarybchik txq->flush_state = SFXGE_FLUSH_PENDING; 1433227569Sphilip 1434283514Sarybchik SFXGE_EVQ_UNLOCK(evq); 1435283514Sarybchik SFXGE_TXQ_UNLOCK(txq); 1436227569Sphilip 1437283514Sarybchik /* Flush the transmit queue. */ 1438283514Sarybchik if (efx_tx_qflush(txq->common) != 0) { 1439283514Sarybchik log(LOG_ERR, "%s: Flushing Tx queue %u failed\n", 1440283514Sarybchik device_get_nameunit(sc->dev), index); 1441283514Sarybchik txq->flush_state = SFXGE_FLUSH_DONE; 1442283514Sarybchik } else { 1443283514Sarybchik count = 0; 1444283514Sarybchik do { 1445283514Sarybchik /* Spin for 100ms. */ 1446283514Sarybchik DELAY(100000); 1447283514Sarybchik if (txq->flush_state != SFXGE_FLUSH_PENDING) 1448283514Sarybchik break; 1449283514Sarybchik } while (++count < 20); 1450283514Sarybchik } 1451283514Sarybchik SFXGE_EVQ_LOCK(evq); 1452283514Sarybchik SFXGE_TXQ_LOCK(txq); 1453227569Sphilip 1454283514Sarybchik KASSERT(txq->flush_state != SFXGE_FLUSH_FAILED, 1455283514Sarybchik ("txq->flush_state == SFXGE_FLUSH_FAILED")); 1456227569Sphilip 1457283514Sarybchik if (txq->flush_state != SFXGE_FLUSH_DONE) { 1458283514Sarybchik /* Flush timeout */ 1459283514Sarybchik log(LOG_ERR, "%s: Cannot flush Tx queue %u\n", 1460283514Sarybchik device_get_nameunit(sc->dev), index); 1461283514Sarybchik txq->flush_state = SFXGE_FLUSH_DONE; 1462283514Sarybchik } 1463283514Sarybchik } 1464227569Sphilip 1465227569Sphilip txq->blocked = 0; 1466227569Sphilip txq->pending = txq->added; 1467227569Sphilip 1468277889Sarybchik sfxge_tx_qcomplete(txq, evq); 1469227569Sphilip KASSERT(txq->completed == txq->added, 1470227569Sphilip ("txq->completed != txq->added")); 1471227569Sphilip 1472227569Sphilip sfxge_tx_qreap(txq); 1473227569Sphilip KASSERT(txq->reaped == txq->completed, 1474227569Sphilip ("txq->reaped != txq->completed")); 1475227569Sphilip 1476227569Sphilip txq->added = 0; 1477227569Sphilip txq->pending = 0; 1478227569Sphilip txq->completed = 0; 1479227569Sphilip txq->reaped = 0; 1480227569Sphilip 1481227569Sphilip /* Destroy the common code transmit queue. */ 1482227569Sphilip efx_tx_qdestroy(txq->common); 1483227569Sphilip txq->common = NULL; 1484227569Sphilip 1485227569Sphilip efx_sram_buf_tbl_clear(sc->enp, txq->buf_base_id, 1486272328Sgnn EFX_TXQ_NBUFS(sc->txq_entries)); 1487227569Sphilip 1488278221Sarybchik SFXGE_EVQ_UNLOCK(evq); 1489278221Sarybchik SFXGE_TXQ_UNLOCK(txq); 1490227569Sphilip} 1491227569Sphilip 1492294077Sarybchik/* 1493294077Sarybchik * Estimate maximum number of Tx descriptors required for TSO packet. 1494294077Sarybchik * With minimum MSS and maximum mbuf length we might need more (even 1495294077Sarybchik * than a ring-ful of descriptors), but this should not happen in 1496294077Sarybchik * practice except due to deliberate attack. In that case we will 1497294077Sarybchik * truncate the output at a packet boundary. 1498294077Sarybchik */ 1499294077Sarybchikstatic unsigned int 1500294077Sarybchiksfxge_tx_max_pkt_desc(const struct sfxge_softc *sc, enum sfxge_txq_type type, 1501294077Sarybchik unsigned int tso_fw_assisted) 1502294077Sarybchik{ 1503294077Sarybchik /* One descriptor for every input fragment */ 1504294077Sarybchik unsigned int max_descs = SFXGE_TX_MAPPING_MAX_SEG; 1505294077Sarybchik unsigned int sw_tso_max_descs; 1506294077Sarybchik unsigned int fa_tso_v1_max_descs = 0; 1507294077Sarybchik unsigned int fa_tso_v2_max_descs = 0; 1508294077Sarybchik 1509294077Sarybchik /* VLAN tagging Tx option descriptor may be required */ 1510294077Sarybchik if (efx_nic_cfg_get(sc->enp)->enc_hw_tx_insert_vlan_enabled) 1511294077Sarybchik max_descs++; 1512294077Sarybchik 1513294077Sarybchik if (type == SFXGE_TXQ_IP_TCP_UDP_CKSUM) { 1514294077Sarybchik /* 1515294077Sarybchik * Plus header and payload descriptor for each output segment. 1516294077Sarybchik * Minus one since header fragment is already counted. 1517294077Sarybchik * Even if FATSO is used, we should be ready to fallback 1518294077Sarybchik * to do it in the driver. 1519294077Sarybchik */ 1520294077Sarybchik sw_tso_max_descs = SFXGE_TSO_MAX_SEGS * 2 - 1; 1521294077Sarybchik 1522294077Sarybchik /* FW assisted TSOv1 requires one more descriptor per segment 1523294077Sarybchik * in comparison to SW TSO */ 1524294077Sarybchik if (tso_fw_assisted & SFXGE_FATSOV1) 1525294077Sarybchik fa_tso_v1_max_descs = 1526294077Sarybchik sw_tso_max_descs + SFXGE_TSO_MAX_SEGS; 1527294077Sarybchik 1528294077Sarybchik /* FW assisted TSOv2 requires 3 (2 FATSO plus header) extra 1529294077Sarybchik * descriptors per superframe limited by number of DMA fetches 1530294077Sarybchik * per packet. The first packet header is already counted. 1531294077Sarybchik */ 1532294077Sarybchik if (tso_fw_assisted & SFXGE_FATSOV2) { 1533294077Sarybchik fa_tso_v2_max_descs = 1534294077Sarybchik howmany(SFXGE_TX_MAPPING_MAX_SEG, 1535294077Sarybchik EFX_TX_FATSOV2_DMA_SEGS_PER_PKT_MAX - 1) * 1536294077Sarybchik (EFX_TX_FATSOV2_OPT_NDESCS + 1) - 1; 1537294077Sarybchik } 1538294077Sarybchik 1539294077Sarybchik max_descs += MAX(sw_tso_max_descs, 1540294077Sarybchik MAX(fa_tso_v1_max_descs, fa_tso_v2_max_descs)); 1541294077Sarybchik } 1542294077Sarybchik 1543294077Sarybchik return (max_descs); 1544294077Sarybchik} 1545294077Sarybchik 1546227569Sphilipstatic int 1547227569Sphilipsfxge_tx_qstart(struct sfxge_softc *sc, unsigned int index) 1548227569Sphilip{ 1549227569Sphilip struct sfxge_txq *txq; 1550227569Sphilip efsys_mem_t *esmp; 1551227569Sphilip uint16_t flags; 1552294077Sarybchik unsigned int tso_fw_assisted; 1553227569Sphilip struct sfxge_evq *evq; 1554283514Sarybchik unsigned int desc_index; 1555227569Sphilip int rc; 1556227569Sphilip 1557283514Sarybchik SFXGE_ADAPTER_LOCK_ASSERT_OWNED(sc); 1558283514Sarybchik 1559227569Sphilip txq = sc->txq[index]; 1560227569Sphilip esmp = &txq->mem; 1561227569Sphilip evq = sc->evq[txq->evq_index]; 1562227569Sphilip 1563227569Sphilip KASSERT(txq->init_state == SFXGE_TXQ_INITIALIZED, 1564227569Sphilip ("txq->init_state != SFXGE_TXQ_INITIALIZED")); 1565227569Sphilip KASSERT(evq->init_state == SFXGE_EVQ_STARTED, 1566227569Sphilip ("evq->init_state != SFXGE_EVQ_STARTED")); 1567227569Sphilip 1568227569Sphilip /* Program the buffer table. */ 1569227569Sphilip if ((rc = efx_sram_buf_tbl_set(sc->enp, txq->buf_base_id, esmp, 1570272328Sgnn EFX_TXQ_NBUFS(sc->txq_entries))) != 0) 1571272328Sgnn return (rc); 1572227569Sphilip 1573227569Sphilip /* Determine the kind of queue we are creating. */ 1574294077Sarybchik tso_fw_assisted = 0; 1575227569Sphilip switch (txq->type) { 1576227569Sphilip case SFXGE_TXQ_NON_CKSUM: 1577227569Sphilip flags = 0; 1578227569Sphilip break; 1579227569Sphilip case SFXGE_TXQ_IP_CKSUM: 1580291924Sarybchik flags = EFX_TXQ_CKSUM_IPV4; 1581227569Sphilip break; 1582227569Sphilip case SFXGE_TXQ_IP_TCP_UDP_CKSUM: 1583291924Sarybchik flags = EFX_TXQ_CKSUM_IPV4 | EFX_TXQ_CKSUM_TCPUDP; 1584294077Sarybchik tso_fw_assisted = sc->tso_fw_assisted; 1585294077Sarybchik if (tso_fw_assisted & SFXGE_FATSOV2) 1586294077Sarybchik flags |= EFX_TXQ_FATSOV2; 1587227569Sphilip break; 1588227569Sphilip default: 1589227569Sphilip KASSERT(0, ("Impossible TX queue")); 1590227569Sphilip flags = 0; 1591227569Sphilip break; 1592227569Sphilip } 1593227569Sphilip 1594227569Sphilip /* Create the common code transmit queue. */ 1595264461Sgnn if ((rc = efx_tx_qcreate(sc->enp, index, txq->type, esmp, 1596272328Sgnn sc->txq_entries, txq->buf_base_id, flags, evq->common, 1597294077Sarybchik &txq->common, &desc_index)) != 0) { 1598294077Sarybchik /* Retry if no FATSOv2 resources, otherwise fail */ 1599294077Sarybchik if ((rc != ENOSPC) || (~flags & EFX_TXQ_FATSOV2)) 1600294077Sarybchik goto fail; 1601227569Sphilip 1602294077Sarybchik /* Looks like all FATSOv2 contexts are used */ 1603294077Sarybchik flags &= ~EFX_TXQ_FATSOV2; 1604294077Sarybchik tso_fw_assisted &= ~SFXGE_FATSOV2; 1605294077Sarybchik if ((rc = efx_tx_qcreate(sc->enp, index, txq->type, esmp, 1606294077Sarybchik sc->txq_entries, txq->buf_base_id, flags, evq->common, 1607294077Sarybchik &txq->common, &desc_index)) != 0) 1608294077Sarybchik goto fail; 1609294077Sarybchik } 1610294077Sarybchik 1611283514Sarybchik /* Initialise queue descriptor indexes */ 1612283514Sarybchik txq->added = txq->pending = txq->completed = txq->reaped = desc_index; 1613283514Sarybchik 1614278221Sarybchik SFXGE_TXQ_LOCK(txq); 1615272325Sgnn 1616227569Sphilip /* Enable the transmit queue. */ 1617227569Sphilip efx_tx_qenable(txq->common); 1618227569Sphilip 1619227569Sphilip txq->init_state = SFXGE_TXQ_STARTED; 1620283514Sarybchik txq->flush_state = SFXGE_FLUSH_REQUIRED; 1621294077Sarybchik txq->tso_fw_assisted = tso_fw_assisted; 1622227569Sphilip 1623294077Sarybchik txq->max_pkt_desc = sfxge_tx_max_pkt_desc(sc, txq->type, 1624294077Sarybchik tso_fw_assisted); 1625294077Sarybchik 1626342404Sarybchik txq->hw_vlan_tci = 0; 1627342404Sarybchik 1628278221Sarybchik SFXGE_TXQ_UNLOCK(txq); 1629227569Sphilip 1630227569Sphilip return (0); 1631227569Sphilip 1632227569Sphilipfail: 1633227569Sphilip efx_sram_buf_tbl_clear(sc->enp, txq->buf_base_id, 1634272328Sgnn EFX_TXQ_NBUFS(sc->txq_entries)); 1635272328Sgnn return (rc); 1636227569Sphilip} 1637227569Sphilip 1638227569Sphilipvoid 1639227569Sphilipsfxge_tx_stop(struct sfxge_softc *sc) 1640227569Sphilip{ 1641227569Sphilip int index; 1642227569Sphilip 1643278938Sarybchik index = sc->txq_count; 1644227569Sphilip while (--index >= 0) 1645278938Sarybchik sfxge_tx_qstop(sc, index); 1646227569Sphilip 1647227569Sphilip /* Tear down the transmit module */ 1648227569Sphilip efx_tx_fini(sc->enp); 1649227569Sphilip} 1650227569Sphilip 1651227569Sphilipint 1652227569Sphilipsfxge_tx_start(struct sfxge_softc *sc) 1653227569Sphilip{ 1654227569Sphilip int index; 1655227569Sphilip int rc; 1656227569Sphilip 1657227569Sphilip /* Initialize the common code transmit module. */ 1658227569Sphilip if ((rc = efx_tx_init(sc->enp)) != 0) 1659227569Sphilip return (rc); 1660227569Sphilip 1661278938Sarybchik for (index = 0; index < sc->txq_count; index++) { 1662278938Sarybchik if ((rc = sfxge_tx_qstart(sc, index)) != 0) 1663278938Sarybchik goto fail; 1664227569Sphilip } 1665227569Sphilip 1666227569Sphilip return (0); 1667227569Sphilip 1668278938Sarybchikfail: 1669227569Sphilip while (--index >= 0) 1670278938Sarybchik sfxge_tx_qstop(sc, index); 1671227569Sphilip 1672227569Sphilip efx_tx_fini(sc->enp); 1673227569Sphilip 1674227569Sphilip return (rc); 1675227569Sphilip} 1676227569Sphilip 1677280377Sarybchikstatic int 1678280377Sarybchiksfxge_txq_stat_init(struct sfxge_txq *txq, struct sysctl_oid *txq_node) 1679280377Sarybchik{ 1680280377Sarybchik struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(txq->sc->dev); 1681280377Sarybchik struct sysctl_oid *stat_node; 1682280377Sarybchik unsigned int id; 1683280377Sarybchik 1684280377Sarybchik stat_node = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(txq_node), OID_AUTO, 1685280377Sarybchik "stats", CTLFLAG_RD, NULL, 1686280377Sarybchik "Tx queue statistics"); 1687280377Sarybchik if (stat_node == NULL) 1688280377Sarybchik return (ENOMEM); 1689280377Sarybchik 1690280377Sarybchik for (id = 0; id < nitems(sfxge_tx_stats); id++) { 1691280377Sarybchik SYSCTL_ADD_ULONG( 1692280377Sarybchik ctx, SYSCTL_CHILDREN(stat_node), OID_AUTO, 1693280377Sarybchik sfxge_tx_stats[id].name, CTLFLAG_RD | CTLFLAG_STATS, 1694280377Sarybchik (unsigned long *)((caddr_t)txq + sfxge_tx_stats[id].offset), 1695280377Sarybchik ""); 1696280377Sarybchik } 1697280377Sarybchik 1698280377Sarybchik return (0); 1699280377Sarybchik} 1700280377Sarybchik 1701227569Sphilip/** 1702227569Sphilip * Destroy a transmit queue. 1703227569Sphilip */ 1704227569Sphilipstatic void 1705227569Sphilipsfxge_tx_qfini(struct sfxge_softc *sc, unsigned int index) 1706227569Sphilip{ 1707227569Sphilip struct sfxge_txq *txq; 1708272328Sgnn unsigned int nmaps; 1709227569Sphilip 1710227569Sphilip txq = sc->txq[index]; 1711227569Sphilip 1712227569Sphilip KASSERT(txq->init_state == SFXGE_TXQ_INITIALIZED, 1713227569Sphilip ("txq->init_state != SFXGE_TXQ_INITIALIZED")); 1714227569Sphilip 1715227569Sphilip if (txq->type == SFXGE_TXQ_IP_TCP_UDP_CKSUM) 1716227569Sphilip tso_fini(txq); 1717227569Sphilip 1718227569Sphilip /* Free the context arrays. */ 1719227569Sphilip free(txq->pend_desc, M_SFXGE); 1720272328Sgnn nmaps = sc->txq_entries; 1721272325Sgnn while (nmaps-- != 0) 1722227569Sphilip bus_dmamap_destroy(txq->packet_dma_tag, txq->stmp[nmaps].map); 1723227569Sphilip free(txq->stmp, M_SFXGE); 1724227569Sphilip 1725227569Sphilip /* Release DMA memory mapping. */ 1726227569Sphilip sfxge_dma_free(&txq->mem); 1727227569Sphilip 1728227569Sphilip sc->txq[index] = NULL; 1729227569Sphilip 1730278221Sarybchik SFXGE_TXQ_LOCK_DESTROY(txq); 1731227569Sphilip 1732227569Sphilip free(txq, M_SFXGE); 1733227569Sphilip} 1734227569Sphilip 1735227569Sphilipstatic int 1736227569Sphilipsfxge_tx_qinit(struct sfxge_softc *sc, unsigned int txq_index, 1737283514Sarybchik enum sfxge_txq_type type, unsigned int evq_index) 1738227569Sphilip{ 1739311765Sarybchik const efx_nic_cfg_t *encp = efx_nic_cfg_get(sc->enp); 1740272330Sgnn char name[16]; 1741280161Sarybchik struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev); 1742272330Sgnn struct sysctl_oid *txq_node; 1743227569Sphilip struct sfxge_txq *txq; 1744227569Sphilip struct sfxge_evq *evq; 1745227569Sphilip struct sfxge_tx_dpl *stdp; 1746280161Sarybchik struct sysctl_oid *dpl_node; 1747227569Sphilip efsys_mem_t *esmp; 1748227569Sphilip unsigned int nmaps; 1749227569Sphilip int rc; 1750227569Sphilip 1751227569Sphilip txq = malloc(sizeof(struct sfxge_txq), M_SFXGE, M_ZERO | M_WAITOK); 1752227569Sphilip txq->sc = sc; 1753272328Sgnn txq->entries = sc->txq_entries; 1754272328Sgnn txq->ptr_mask = txq->entries - 1; 1755227569Sphilip 1756227569Sphilip sc->txq[txq_index] = txq; 1757227569Sphilip esmp = &txq->mem; 1758227569Sphilip 1759227569Sphilip evq = sc->evq[evq_index]; 1760227569Sphilip 1761227569Sphilip /* Allocate and zero DMA space for the descriptor ring. */ 1762272328Sgnn if ((rc = sfxge_dma_alloc(sc, EFX_TXQ_SIZE(sc->txq_entries), esmp)) != 0) 1763227569Sphilip return (rc); 1764227569Sphilip 1765227569Sphilip /* Allocate buffer table entries. */ 1766272328Sgnn sfxge_sram_buf_tbl_alloc(sc, EFX_TXQ_NBUFS(sc->txq_entries), 1767227569Sphilip &txq->buf_base_id); 1768227569Sphilip 1769227569Sphilip /* Create a DMA tag for packet mappings. */ 1770311765Sarybchik if (bus_dma_tag_create(sc->parent_dma_tag, 1, 1771311765Sarybchik encp->enc_tx_dma_desc_boundary, 1772227640Smarius MIN(0x3FFFFFFFFFFFUL, BUS_SPACE_MAXADDR), BUS_SPACE_MAXADDR, NULL, 1773311765Sarybchik NULL, 0x11000, SFXGE_TX_MAPPING_MAX_SEG, 1774311765Sarybchik encp->enc_tx_dma_desc_size_max, 0, NULL, NULL, 1775227569Sphilip &txq->packet_dma_tag) != 0) { 1776227569Sphilip device_printf(sc->dev, "Couldn't allocate txq DMA tag\n"); 1777227569Sphilip rc = ENOMEM; 1778227569Sphilip goto fail; 1779227569Sphilip } 1780227569Sphilip 1781227569Sphilip /* Allocate pending descriptor array for batching writes. */ 1782283514Sarybchik txq->pend_desc = malloc(sizeof(efx_desc_t) * sc->txq_entries, 1783227569Sphilip M_SFXGE, M_ZERO | M_WAITOK); 1784227569Sphilip 1785227569Sphilip /* Allocate and initialise mbuf DMA mapping array. */ 1786272328Sgnn txq->stmp = malloc(sizeof(struct sfxge_tx_mapping) * sc->txq_entries, 1787227569Sphilip M_SFXGE, M_ZERO | M_WAITOK); 1788272328Sgnn for (nmaps = 0; nmaps < sc->txq_entries; nmaps++) { 1789227569Sphilip rc = bus_dmamap_create(txq->packet_dma_tag, 0, 1790227569Sphilip &txq->stmp[nmaps].map); 1791227569Sphilip if (rc != 0) 1792227569Sphilip goto fail2; 1793227569Sphilip } 1794227569Sphilip 1795272330Sgnn snprintf(name, sizeof(name), "%u", txq_index); 1796280161Sarybchik txq_node = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(sc->txqs_node), 1797280161Sarybchik OID_AUTO, name, CTLFLAG_RD, NULL, ""); 1798272330Sgnn if (txq_node == NULL) { 1799272330Sgnn rc = ENOMEM; 1800272330Sgnn goto fail_txq_node; 1801272330Sgnn } 1802272330Sgnn 1803227569Sphilip if (type == SFXGE_TXQ_IP_TCP_UDP_CKSUM && 1804227569Sphilip (rc = tso_init(txq)) != 0) 1805227569Sphilip goto fail3; 1806227569Sphilip 1807227569Sphilip /* Initialize the deferred packet list. */ 1808227569Sphilip stdp = &txq->dpl; 1809272331Sgnn stdp->std_put_max = sfxge_tx_dpl_put_max; 1810272331Sgnn stdp->std_get_max = sfxge_tx_dpl_get_max; 1811277895Sarybchik stdp->std_get_non_tcp_max = sfxge_tx_dpl_get_non_tcp_max; 1812227569Sphilip stdp->std_getp = &stdp->std_get; 1813227569Sphilip 1814278250Sarybchik SFXGE_TXQ_LOCK_INIT(txq, device_get_nameunit(sc->dev), txq_index); 1815272330Sgnn 1816280161Sarybchik dpl_node = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(txq_node), OID_AUTO, 1817280161Sarybchik "dpl", CTLFLAG_RD, NULL, 1818280161Sarybchik "Deferred packet list statistics"); 1819280161Sarybchik if (dpl_node == NULL) { 1820280161Sarybchik rc = ENOMEM; 1821280161Sarybchik goto fail_dpl_node; 1822280161Sarybchik } 1823280161Sarybchik 1824280161Sarybchik SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(dpl_node), OID_AUTO, 1825280161Sarybchik "get_count", CTLFLAG_RD | CTLFLAG_STATS, 1826272330Sgnn &stdp->std_get_count, 0, ""); 1827280161Sarybchik SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(dpl_node), OID_AUTO, 1828280161Sarybchik "get_non_tcp_count", CTLFLAG_RD | CTLFLAG_STATS, 1829277895Sarybchik &stdp->std_get_non_tcp_count, 0, ""); 1830280161Sarybchik SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(dpl_node), OID_AUTO, 1831280161Sarybchik "get_hiwat", CTLFLAG_RD | CTLFLAG_STATS, 1832277895Sarybchik &stdp->std_get_hiwat, 0, ""); 1833280161Sarybchik SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(dpl_node), OID_AUTO, 1834280161Sarybchik "put_hiwat", CTLFLAG_RD | CTLFLAG_STATS, 1835279231Sarybchik &stdp->std_put_hiwat, 0, ""); 1836227569Sphilip 1837280377Sarybchik rc = sfxge_txq_stat_init(txq, txq_node); 1838280377Sarybchik if (rc != 0) 1839280377Sarybchik goto fail_txq_stat_init; 1840280377Sarybchik 1841227569Sphilip txq->type = type; 1842227569Sphilip txq->evq_index = evq_index; 1843227569Sphilip txq->init_state = SFXGE_TXQ_INITIALIZED; 1844227569Sphilip 1845227569Sphilip return (0); 1846227569Sphilip 1847280377Sarybchikfail_txq_stat_init: 1848280161Sarybchikfail_dpl_node: 1849227569Sphilipfail3: 1850272330Sgnnfail_txq_node: 1851227569Sphilip free(txq->pend_desc, M_SFXGE); 1852227569Sphilipfail2: 1853272325Sgnn while (nmaps-- != 0) 1854227569Sphilip bus_dmamap_destroy(txq->packet_dma_tag, txq->stmp[nmaps].map); 1855227569Sphilip free(txq->stmp, M_SFXGE); 1856227569Sphilip bus_dma_tag_destroy(txq->packet_dma_tag); 1857227569Sphilip 1858227569Sphilipfail: 1859227569Sphilip sfxge_dma_free(esmp); 1860227569Sphilip 1861227569Sphilip return (rc); 1862227569Sphilip} 1863227569Sphilip 1864227569Sphilipstatic int 1865227569Sphilipsfxge_tx_stat_handler(SYSCTL_HANDLER_ARGS) 1866227569Sphilip{ 1867227569Sphilip struct sfxge_softc *sc = arg1; 1868227569Sphilip unsigned int id = arg2; 1869227569Sphilip unsigned long sum; 1870227569Sphilip unsigned int index; 1871227569Sphilip 1872227569Sphilip /* Sum across all TX queues */ 1873227569Sphilip sum = 0; 1874278938Sarybchik for (index = 0; index < sc->txq_count; index++) 1875227569Sphilip sum += *(unsigned long *)((caddr_t)sc->txq[index] + 1876227569Sphilip sfxge_tx_stats[id].offset); 1877227569Sphilip 1878272325Sgnn return (SYSCTL_OUT(req, &sum, sizeof(sum))); 1879227569Sphilip} 1880227569Sphilip 1881227569Sphilipstatic void 1882227569Sphilipsfxge_tx_stat_init(struct sfxge_softc *sc) 1883227569Sphilip{ 1884227569Sphilip struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev); 1885227569Sphilip struct sysctl_oid_list *stat_list; 1886227569Sphilip unsigned int id; 1887227569Sphilip 1888227569Sphilip stat_list = SYSCTL_CHILDREN(sc->stats_node); 1889227569Sphilip 1890279077Sarybchik for (id = 0; id < nitems(sfxge_tx_stats); id++) { 1891227569Sphilip SYSCTL_ADD_PROC( 1892227569Sphilip ctx, stat_list, 1893227569Sphilip OID_AUTO, sfxge_tx_stats[id].name, 1894227569Sphilip CTLTYPE_ULONG|CTLFLAG_RD, 1895227569Sphilip sc, id, sfxge_tx_stat_handler, "LU", 1896227569Sphilip ""); 1897227569Sphilip } 1898227569Sphilip} 1899227569Sphilip 1900279184Sarybchikuint64_t 1901279184Sarybchiksfxge_tx_get_drops(struct sfxge_softc *sc) 1902279184Sarybchik{ 1903279184Sarybchik unsigned int index; 1904279184Sarybchik uint64_t drops = 0; 1905279184Sarybchik struct sfxge_txq *txq; 1906279184Sarybchik 1907279184Sarybchik /* Sum across all TX queues */ 1908279184Sarybchik for (index = 0; index < sc->txq_count; index++) { 1909279184Sarybchik txq = sc->txq[index]; 1910279184Sarybchik /* 1911279184Sarybchik * In theory, txq->put_overflow and txq->netdown_drops 1912279184Sarybchik * should use atomic operation and other should be 1913279184Sarybchik * obtained under txq lock, but it is just statistics. 1914279184Sarybchik */ 1915279184Sarybchik drops += txq->drops + txq->get_overflow + 1916279184Sarybchik txq->get_non_tcp_overflow + 1917279184Sarybchik txq->put_overflow + txq->netdown_drops + 1918279184Sarybchik txq->tso_pdrop_too_many + txq->tso_pdrop_no_rsrc; 1919279184Sarybchik } 1920279184Sarybchik return (drops); 1921279184Sarybchik} 1922279184Sarybchik 1923227569Sphilipvoid 1924227569Sphilipsfxge_tx_fini(struct sfxge_softc *sc) 1925227569Sphilip{ 1926227569Sphilip int index; 1927227569Sphilip 1928278938Sarybchik index = sc->txq_count; 1929227569Sphilip while (--index >= 0) 1930278938Sarybchik sfxge_tx_qfini(sc, index); 1931227569Sphilip 1932278938Sarybchik sc->txq_count = 0; 1933227569Sphilip} 1934227569Sphilip 1935227569Sphilip 1936227569Sphilipint 1937227569Sphilipsfxge_tx_init(struct sfxge_softc *sc) 1938227569Sphilip{ 1939283514Sarybchik const efx_nic_cfg_t *encp = efx_nic_cfg_get(sc->enp); 1940227569Sphilip struct sfxge_intr *intr; 1941227569Sphilip int index; 1942227569Sphilip int rc; 1943227569Sphilip 1944227569Sphilip intr = &sc->intr; 1945227569Sphilip 1946227569Sphilip KASSERT(intr->state == SFXGE_INTR_INITIALIZED, 1947227569Sphilip ("intr->state != SFXGE_INTR_INITIALIZED")); 1948227569Sphilip 1949311029Sarybchik if (sfxge_tx_dpl_get_max <= 0) { 1950311029Sarybchik log(LOG_ERR, "%s=%d must be greater than 0", 1951311029Sarybchik SFXGE_PARAM_TX_DPL_GET_MAX, sfxge_tx_dpl_get_max); 1952311029Sarybchik rc = EINVAL; 1953311029Sarybchik goto fail_tx_dpl_get_max; 1954311029Sarybchik } 1955311029Sarybchik if (sfxge_tx_dpl_get_non_tcp_max <= 0) { 1956311029Sarybchik log(LOG_ERR, "%s=%d must be greater than 0", 1957311029Sarybchik SFXGE_PARAM_TX_DPL_GET_NON_TCP_MAX, 1958311029Sarybchik sfxge_tx_dpl_get_non_tcp_max); 1959311029Sarybchik rc = EINVAL; 1960311029Sarybchik goto fail_tx_dpl_get_non_tcp_max; 1961311029Sarybchik } 1962311029Sarybchik if (sfxge_tx_dpl_put_max < 0) { 1963311029Sarybchik log(LOG_ERR, "%s=%d must be greater or equal to 0", 1964311029Sarybchik SFXGE_PARAM_TX_DPL_PUT_MAX, sfxge_tx_dpl_put_max); 1965311029Sarybchik rc = EINVAL; 1966311029Sarybchik goto fail_tx_dpl_put_max; 1967311029Sarybchik } 1968311029Sarybchik 1969278938Sarybchik sc->txq_count = SFXGE_TXQ_NTYPES - 1 + sc->intr.n_alloc; 1970278938Sarybchik 1971283514Sarybchik sc->tso_fw_assisted = sfxge_tso_fw_assisted; 1972294077Sarybchik if ((~encp->enc_features & EFX_FEATURE_FW_ASSISTED_TSO) || 1973294077Sarybchik (!encp->enc_fw_assisted_tso_enabled)) 1974294077Sarybchik sc->tso_fw_assisted &= ~SFXGE_FATSOV1; 1975294077Sarybchik if ((~encp->enc_features & EFX_FEATURE_FW_ASSISTED_TSO_V2) || 1976294077Sarybchik (!encp->enc_fw_assisted_tso_v2_enabled)) 1977294077Sarybchik sc->tso_fw_assisted &= ~SFXGE_FATSOV2; 1978283514Sarybchik 1979272330Sgnn sc->txqs_node = SYSCTL_ADD_NODE( 1980272330Sgnn device_get_sysctl_ctx(sc->dev), 1981272330Sgnn SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)), 1982272330Sgnn OID_AUTO, "txq", CTLFLAG_RD, NULL, "Tx queues"); 1983272330Sgnn if (sc->txqs_node == NULL) { 1984272330Sgnn rc = ENOMEM; 1985272330Sgnn goto fail_txq_node; 1986272330Sgnn } 1987272330Sgnn 1988227569Sphilip /* Initialize the transmit queues */ 1989227569Sphilip if ((rc = sfxge_tx_qinit(sc, SFXGE_TXQ_NON_CKSUM, 1990227569Sphilip SFXGE_TXQ_NON_CKSUM, 0)) != 0) 1991227569Sphilip goto fail; 1992227569Sphilip 1993227569Sphilip if ((rc = sfxge_tx_qinit(sc, SFXGE_TXQ_IP_CKSUM, 1994227569Sphilip SFXGE_TXQ_IP_CKSUM, 0)) != 0) 1995227569Sphilip goto fail2; 1996227569Sphilip 1997278938Sarybchik for (index = 0; 1998278938Sarybchik index < sc->txq_count - SFXGE_TXQ_NTYPES + 1; 1999278938Sarybchik index++) { 2000278938Sarybchik if ((rc = sfxge_tx_qinit(sc, SFXGE_TXQ_NTYPES - 1 + index, 2001227569Sphilip SFXGE_TXQ_IP_TCP_UDP_CKSUM, index)) != 0) 2002227569Sphilip goto fail3; 2003227569Sphilip } 2004227569Sphilip 2005227569Sphilip sfxge_tx_stat_init(sc); 2006227569Sphilip 2007227569Sphilip return (0); 2008227569Sphilip 2009227569Sphilipfail3: 2010227569Sphilip while (--index >= 0) 2011227569Sphilip sfxge_tx_qfini(sc, SFXGE_TXQ_IP_TCP_UDP_CKSUM + index); 2012227569Sphilip 2013278938Sarybchik sfxge_tx_qfini(sc, SFXGE_TXQ_IP_CKSUM); 2014278938Sarybchik 2015227569Sphilipfail2: 2016227569Sphilip sfxge_tx_qfini(sc, SFXGE_TXQ_NON_CKSUM); 2017227569Sphilip 2018227569Sphilipfail: 2019272330Sgnnfail_txq_node: 2020278938Sarybchik sc->txq_count = 0; 2021311029Sarybchikfail_tx_dpl_put_max: 2022311029Sarybchikfail_tx_dpl_get_non_tcp_max: 2023311029Sarybchikfail_tx_dpl_get_max: 2024227569Sphilip return (rc); 2025227569Sphilip} 2026