1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2010-2016 Solarflare Communications Inc. 5 * All rights reserved. 6 * 7 * This software was developed in part by Philip Paeps under contract for 8 * Solarflare Communications, Inc. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright notice, 14 * this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright notice, 16 * this list of conditions and the following disclaimer in the documentation 17 * and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 29 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 * 31 * The views and conclusions contained in the software and documentation are 32 * those of the authors and should not be interpreted as representing official 33 * policies, either expressed or implied, of the FreeBSD Project. 34 */ 35 36 /* Theory of operation: 37 * 38 * Tx queues allocation and mapping 39 * 40 * One Tx queue with enabled checksum offload is allocated per Rx channel 41 * (event queue). Also 2 Tx queues (one without checksum offload and one 42 * with IP checksum offload only) are allocated and bound to event queue 0. 43 * sfxge_txq_type is used as Tx queue label. 44 * 45 * So, event queue plus label mapping to Tx queue index is: 46 * if event queue index is 0, TxQ-index = TxQ-label * [0..SFXGE_TXQ_NTYPES) 47 * else TxQ-index = SFXGE_TXQ_NTYPES + EvQ-index - 1 48 * See sfxge_get_txq_by_label() sfxge_ev.c 49 */ 50 51 #include <sys/cdefs.h> 52 __FBSDID("$FreeBSD$"); 53 54 #include "opt_rss.h" 55 56 #include <sys/param.h> 57 #include <sys/malloc.h> 58 #include <sys/mbuf.h> 59 #include <sys/smp.h> 60 #include <sys/socket.h> 61 #include <sys/sysctl.h> 62 #include <sys/syslog.h> 63 #include <sys/limits.h> 64 65 #include <net/bpf.h> 66 #include <net/ethernet.h> 67 #include <net/if.h> 68 #include <net/if_vlan_var.h> 69 70 #include <netinet/in.h> 71 #include <netinet/ip.h> 72 #include <netinet/ip6.h> 73 #include <netinet/tcp.h> 74 75 #ifdef RSS 76 #include <net/rss_config.h> 77 #endif 78 79 #include "common/efx.h" 80 81 #include "sfxge.h" 82 #include "sfxge_tx.h" 83 84 85 #define SFXGE_PARAM_TX_DPL_GET_MAX SFXGE_PARAM(tx_dpl_get_max) 86 static int sfxge_tx_dpl_get_max = SFXGE_TX_DPL_GET_PKT_LIMIT_DEFAULT; 87 TUNABLE_INT(SFXGE_PARAM_TX_DPL_GET_MAX, &sfxge_tx_dpl_get_max); 88 SYSCTL_INT(_hw_sfxge, OID_AUTO, tx_dpl_get_max, CTLFLAG_RDTUN, 89 &sfxge_tx_dpl_get_max, 0, 90 "Maximum number of any packets in deferred packet get-list"); 91 92 #define SFXGE_PARAM_TX_DPL_GET_NON_TCP_MAX \ 93 SFXGE_PARAM(tx_dpl_get_non_tcp_max) 94 static int sfxge_tx_dpl_get_non_tcp_max = 95 SFXGE_TX_DPL_GET_NON_TCP_PKT_LIMIT_DEFAULT; 96 TUNABLE_INT(SFXGE_PARAM_TX_DPL_GET_NON_TCP_MAX, &sfxge_tx_dpl_get_non_tcp_max); 97 SYSCTL_INT(_hw_sfxge, OID_AUTO, tx_dpl_get_non_tcp_max, CTLFLAG_RDTUN, 98 &sfxge_tx_dpl_get_non_tcp_max, 0, 99 "Maximum number of non-TCP packets in deferred packet get-list"); 100 101 #define SFXGE_PARAM_TX_DPL_PUT_MAX SFXGE_PARAM(tx_dpl_put_max) 102 static int sfxge_tx_dpl_put_max = SFXGE_TX_DPL_PUT_PKT_LIMIT_DEFAULT; 103 TUNABLE_INT(SFXGE_PARAM_TX_DPL_PUT_MAX, &sfxge_tx_dpl_put_max); 104 SYSCTL_INT(_hw_sfxge, OID_AUTO, tx_dpl_put_max, CTLFLAG_RDTUN, 105 &sfxge_tx_dpl_put_max, 0, 106 "Maximum number of any packets in deferred packet put-list"); 107 108 #define SFXGE_PARAM_TSO_FW_ASSISTED SFXGE_PARAM(tso_fw_assisted) 109 static int sfxge_tso_fw_assisted = (SFXGE_FATSOV1 | SFXGE_FATSOV2); 110 TUNABLE_INT(SFXGE_PARAM_TSO_FW_ASSISTED, &sfxge_tso_fw_assisted); 111 SYSCTL_INT(_hw_sfxge, OID_AUTO, tso_fw_assisted, CTLFLAG_RDTUN, 112 &sfxge_tso_fw_assisted, 0, 113 "Bitmask of FW-assisted TSO allowed to use if supported by NIC firmware"); 114 115 116 static const struct { 117 const char *name; 118 size_t offset; 119 } sfxge_tx_stats[] = { 120 #define SFXGE_TX_STAT(name, member) \ 121 { #name, offsetof(struct sfxge_txq, member) } 122 SFXGE_TX_STAT(tso_bursts, tso_bursts), 123 SFXGE_TX_STAT(tso_packets, tso_packets), 124 SFXGE_TX_STAT(tso_long_headers, tso_long_headers), 125 SFXGE_TX_STAT(tso_pdrop_too_many, tso_pdrop_too_many), 126 SFXGE_TX_STAT(tso_pdrop_no_rsrc, tso_pdrop_no_rsrc), 127 SFXGE_TX_STAT(tx_collapses, collapses), 128 SFXGE_TX_STAT(tx_drops, drops), 129 SFXGE_TX_STAT(tx_get_overflow, get_overflow), 130 SFXGE_TX_STAT(tx_get_non_tcp_overflow, get_non_tcp_overflow), 131 SFXGE_TX_STAT(tx_put_overflow, put_overflow), 132 SFXGE_TX_STAT(tx_netdown_drops, netdown_drops), 133 }; 134 135 136 /* Forward declarations. */ 137 static void sfxge_tx_qdpl_service(struct sfxge_txq *txq); 138 static void sfxge_tx_qlist_post(struct sfxge_txq *txq); 139 static void sfxge_tx_qunblock(struct sfxge_txq *txq); 140 static int sfxge_tx_queue_tso(struct sfxge_txq *txq, struct mbuf *mbuf, 141 const bus_dma_segment_t *dma_seg, int n_dma_seg, 142 int vlan_tagged); 143 144 static int 145 sfxge_tx_maybe_insert_tag(struct sfxge_txq *txq, struct mbuf *mbuf) 146 { 147 uint16_t this_tag = ((mbuf->m_flags & M_VLANTAG) ? 148 mbuf->m_pkthdr.ether_vtag : 149 0); 150 151 if (this_tag == txq->hw_vlan_tci) 152 return (0); 153 154 efx_tx_qdesc_vlantci_create(txq->common, 155 bswap16(this_tag), 156 &txq->pend_desc[0]); 157 txq->n_pend_desc = 1; 158 txq->hw_vlan_tci = this_tag; 159 return (1); 160 } 161 162 static inline void 163 sfxge_next_stmp(struct sfxge_txq *txq, struct sfxge_tx_mapping **pstmp) 164 { 165 KASSERT((*pstmp)->flags == 0, ("stmp flags are not 0")); 166 if (__predict_false(*pstmp == 167 &txq->stmp[txq->ptr_mask])) 168 *pstmp = &txq->stmp[0]; 169 else 170 (*pstmp)++; 171 } 172 173 174 void 175 sfxge_tx_qcomplete(struct sfxge_txq *txq, struct sfxge_evq *evq) 176 { 177 unsigned int completed; 178 179 SFXGE_EVQ_LOCK_ASSERT_OWNED(evq); 180 181 completed = txq->completed; 182 while (completed != txq->pending) { 183 struct sfxge_tx_mapping *stmp; 184 unsigned int id; 185 186 id = completed++ & txq->ptr_mask; 187 188 stmp = &txq->stmp[id]; 189 if (stmp->flags & TX_BUF_UNMAP) { 190 bus_dmamap_unload(txq->packet_dma_tag, stmp->map); 191 if (stmp->flags & TX_BUF_MBUF) { 192 struct mbuf *m = stmp->u.mbuf; 193 do 194 m = m_free(m); 195 while (m != NULL); 196 } else { 197 free(stmp->u.heap_buf, M_SFXGE); 198 } 199 stmp->flags = 0; 200 } 201 } 202 txq->completed = completed; 203 204 /* Check whether we need to unblock the queue. */ 205 mb(); 206 if (txq->blocked) { 207 unsigned int level; 208 209 level = txq->added - txq->completed; 210 if (level <= SFXGE_TXQ_UNBLOCK_LEVEL(txq->entries)) 211 sfxge_tx_qunblock(txq); 212 } 213 } 214 215 static unsigned int 216 sfxge_is_mbuf_non_tcp(struct mbuf *mbuf) 217 { 218 /* Absence of TCP checksum flags does not mean that it is non-TCP 219 * but it should be true if user wants to achieve high throughput. 220 */ 221 return (!(mbuf->m_pkthdr.csum_flags & (CSUM_IP_TCP | CSUM_IP6_TCP))); 222 } 223 224 /* 225 * Reorder the put list and append it to the get list. 226 */ 227 static void 228 sfxge_tx_qdpl_swizzle(struct sfxge_txq *txq) 229 { 230 struct sfxge_tx_dpl *stdp; 231 struct mbuf *mbuf, *get_next, **get_tailp; 232 volatile uintptr_t *putp; 233 uintptr_t put; 234 unsigned int count; 235 unsigned int non_tcp_count; 236 237 SFXGE_TXQ_LOCK_ASSERT_OWNED(txq); 238 239 stdp = &txq->dpl; 240 241 /* Acquire the put list. */ 242 putp = &stdp->std_put; 243 put = atomic_readandclear_ptr(putp); 244 mbuf = (void *)put; 245 246 if (mbuf == NULL) 247 return; 248 249 /* Reverse the put list. */ 250 get_tailp = &mbuf->m_nextpkt; 251 get_next = NULL; 252 253 count = 0; 254 non_tcp_count = 0; 255 do { 256 struct mbuf *put_next; 257 258 non_tcp_count += sfxge_is_mbuf_non_tcp(mbuf); 259 put_next = mbuf->m_nextpkt; 260 mbuf->m_nextpkt = get_next; 261 get_next = mbuf; 262 mbuf = put_next; 263 264 count++; 265 } while (mbuf != NULL); 266 267 if (count > stdp->std_put_hiwat) 268 stdp->std_put_hiwat = count; 269 270 /* Append the reversed put list to the get list. */ 271 KASSERT(*get_tailp == NULL, ("*get_tailp != NULL")); 272 *stdp->std_getp = get_next; 273 stdp->std_getp = get_tailp; 274 stdp->std_get_count += count; 275 stdp->std_get_non_tcp_count += non_tcp_count; 276 } 277 278 static void 279 sfxge_tx_qreap(struct sfxge_txq *txq) 280 { 281 SFXGE_TXQ_LOCK_ASSERT_OWNED(txq); 282 283 txq->reaped = txq->completed; 284 } 285 286 static void 287 sfxge_tx_qlist_post(struct sfxge_txq *txq) 288 { 289 unsigned int old_added; 290 unsigned int block_level; 291 unsigned int level; 292 int rc; 293 294 SFXGE_TXQ_LOCK_ASSERT_OWNED(txq); 295 296 KASSERT(txq->n_pend_desc != 0, ("txq->n_pend_desc == 0")); 297 KASSERT(txq->n_pend_desc <= txq->max_pkt_desc, 298 ("txq->n_pend_desc too large")); 299 KASSERT(!txq->blocked, ("txq->blocked")); 300 301 old_added = txq->added; 302 303 /* Post the fragment list. */ 304 rc = efx_tx_qdesc_post(txq->common, txq->pend_desc, txq->n_pend_desc, 305 txq->reaped, &txq->added); 306 KASSERT(rc == 0, ("efx_tx_qdesc_post() failed")); 307 308 /* If efx_tx_qdesc_post() had to refragment, our information about 309 * buffers to free may be associated with the wrong 310 * descriptors. 311 */ 312 KASSERT(txq->added - old_added == txq->n_pend_desc, 313 ("efx_tx_qdesc_post() refragmented descriptors")); 314 315 level = txq->added - txq->reaped; 316 KASSERT(level <= txq->entries, ("overfilled TX queue")); 317 318 /* Clear the fragment list. */ 319 txq->n_pend_desc = 0; 320 321 /* 322 * Set the block level to ensure there is space to generate a 323 * large number of descriptors for TSO. 324 */ 325 block_level = EFX_TXQ_LIMIT(txq->entries) - txq->max_pkt_desc; 326 327 /* Have we reached the block level? */ 328 if (level < block_level) 329 return; 330 331 /* Reap, and check again */ 332 sfxge_tx_qreap(txq); 333 level = txq->added - txq->reaped; 334 if (level < block_level) 335 return; 336 337 txq->blocked = 1; 338 339 /* 340 * Avoid a race with completion interrupt handling that could leave 341 * the queue blocked. 342 */ 343 mb(); 344 sfxge_tx_qreap(txq); 345 level = txq->added - txq->reaped; 346 if (level < block_level) { 347 mb(); 348 txq->blocked = 0; 349 } 350 } 351 352 static int sfxge_tx_queue_mbuf(struct sfxge_txq *txq, struct mbuf *mbuf) 353 { 354 bus_dmamap_t *used_map; 355 bus_dmamap_t map; 356 bus_dma_segment_t dma_seg[SFXGE_TX_MAPPING_MAX_SEG]; 357 unsigned int id; 358 struct sfxge_tx_mapping *stmp; 359 efx_desc_t *desc; 360 int n_dma_seg; 361 int rc; 362 int i; 363 int eop; 364 int vlan_tagged; 365 366 KASSERT(!txq->blocked, ("txq->blocked")); 367 368 #if SFXGE_TX_PARSE_EARLY 369 /* 370 * If software TSO is used, we still need to copy packet header, 371 * even if we have already parsed it early before enqueue. 372 */ 373 if ((mbuf->m_pkthdr.csum_flags & CSUM_TSO) && 374 (txq->tso_fw_assisted == 0)) 375 prefetch_read_many(mbuf->m_data); 376 #else 377 /* 378 * Prefetch packet header since we need to parse it and extract 379 * IP ID, TCP sequence number and flags. 380 */ 381 if (mbuf->m_pkthdr.csum_flags & CSUM_TSO) 382 prefetch_read_many(mbuf->m_data); 383 #endif 384 385 if (__predict_false(txq->init_state != SFXGE_TXQ_STARTED)) { 386 rc = EINTR; 387 goto reject; 388 } 389 390 /* Load the packet for DMA. */ 391 id = txq->added & txq->ptr_mask; 392 stmp = &txq->stmp[id]; 393 rc = bus_dmamap_load_mbuf_sg(txq->packet_dma_tag, stmp->map, 394 mbuf, dma_seg, &n_dma_seg, 0); 395 if (rc == EFBIG) { 396 /* Try again. */ 397 struct mbuf *new_mbuf = m_collapse(mbuf, M_NOWAIT, 398 SFXGE_TX_MAPPING_MAX_SEG); 399 if (new_mbuf == NULL) 400 goto reject; 401 ++txq->collapses; 402 mbuf = new_mbuf; 403 rc = bus_dmamap_load_mbuf_sg(txq->packet_dma_tag, 404 stmp->map, mbuf, 405 dma_seg, &n_dma_seg, 0); 406 } 407 if (rc != 0) 408 goto reject; 409 410 /* Make the packet visible to the hardware. */ 411 bus_dmamap_sync(txq->packet_dma_tag, stmp->map, BUS_DMASYNC_PREWRITE); 412 413 used_map = &stmp->map; 414 415 vlan_tagged = sfxge_tx_maybe_insert_tag(txq, mbuf); 416 if (vlan_tagged) { 417 sfxge_next_stmp(txq, &stmp); 418 } 419 if (mbuf->m_pkthdr.csum_flags & CSUM_TSO) { 420 rc = sfxge_tx_queue_tso(txq, mbuf, dma_seg, n_dma_seg, vlan_tagged); 421 if (rc < 0) 422 goto reject_mapped; 423 stmp = &txq->stmp[(rc - 1) & txq->ptr_mask]; 424 } else { 425 /* Add the mapping to the fragment list, and set flags 426 * for the buffer. 427 */ 428 429 i = 0; 430 for (;;) { 431 desc = &txq->pend_desc[i + vlan_tagged]; 432 eop = (i == n_dma_seg - 1); 433 efx_tx_qdesc_dma_create(txq->common, 434 dma_seg[i].ds_addr, 435 dma_seg[i].ds_len, 436 eop, 437 desc); 438 if (eop) 439 break; 440 i++; 441 sfxge_next_stmp(txq, &stmp); 442 } 443 txq->n_pend_desc = n_dma_seg + vlan_tagged; 444 } 445 446 /* 447 * If the mapping required more than one descriptor 448 * then we need to associate the DMA map with the last 449 * descriptor, not the first. 450 */ 451 if (used_map != &stmp->map) { 452 map = stmp->map; 453 stmp->map = *used_map; 454 *used_map = map; 455 } 456 457 stmp->u.mbuf = mbuf; 458 stmp->flags = TX_BUF_UNMAP | TX_BUF_MBUF; 459 460 /* Post the fragment list. */ 461 sfxge_tx_qlist_post(txq); 462 463 return (0); 464 465 reject_mapped: 466 bus_dmamap_unload(txq->packet_dma_tag, *used_map); 467 reject: 468 /* Drop the packet on the floor. */ 469 m_freem(mbuf); 470 ++txq->drops; 471 472 return (rc); 473 } 474 475 /* 476 * Drain the deferred packet list into the transmit queue. 477 */ 478 static void 479 sfxge_tx_qdpl_drain(struct sfxge_txq *txq) 480 { 481 struct sfxge_softc *sc; 482 struct sfxge_tx_dpl *stdp; 483 struct mbuf *mbuf, *next; 484 unsigned int count; 485 unsigned int non_tcp_count; 486 unsigned int pushed; 487 int rc; 488 489 SFXGE_TXQ_LOCK_ASSERT_OWNED(txq); 490 491 sc = txq->sc; 492 stdp = &txq->dpl; 493 pushed = txq->added; 494 495 if (__predict_true(txq->init_state == SFXGE_TXQ_STARTED)) { 496 prefetch_read_many(sc->enp); 497 prefetch_read_many(txq->common); 498 } 499 500 mbuf = stdp->std_get; 501 count = stdp->std_get_count; 502 non_tcp_count = stdp->std_get_non_tcp_count; 503 504 if (count > stdp->std_get_hiwat) 505 stdp->std_get_hiwat = count; 506 507 while (count != 0) { 508 KASSERT(mbuf != NULL, ("mbuf == NULL")); 509 510 next = mbuf->m_nextpkt; 511 mbuf->m_nextpkt = NULL; 512 513 ETHER_BPF_MTAP(sc->ifnet, mbuf); /* packet capture */ 514 515 if (next != NULL) 516 prefetch_read_many(next); 517 518 rc = sfxge_tx_queue_mbuf(txq, mbuf); 519 --count; 520 non_tcp_count -= sfxge_is_mbuf_non_tcp(mbuf); 521 mbuf = next; 522 if (rc != 0) 523 continue; 524 525 if (txq->blocked) 526 break; 527 528 /* Push the fragments to the hardware in batches. */ 529 if (txq->added - pushed >= SFXGE_TX_BATCH) { 530 efx_tx_qpush(txq->common, txq->added, pushed); 531 pushed = txq->added; 532 } 533 } 534 535 if (count == 0) { 536 KASSERT(mbuf == NULL, ("mbuf != NULL")); 537 KASSERT(non_tcp_count == 0, 538 ("inconsistent TCP/non-TCP detection")); 539 stdp->std_get = NULL; 540 stdp->std_get_count = 0; 541 stdp->std_get_non_tcp_count = 0; 542 stdp->std_getp = &stdp->std_get; 543 } else { 544 stdp->std_get = mbuf; 545 stdp->std_get_count = count; 546 stdp->std_get_non_tcp_count = non_tcp_count; 547 } 548 549 if (txq->added != pushed) 550 efx_tx_qpush(txq->common, txq->added, pushed); 551 552 KASSERT(txq->blocked || stdp->std_get_count == 0, 553 ("queue unblocked but count is non-zero")); 554 } 555 556 #define SFXGE_TX_QDPL_PENDING(_txq) ((_txq)->dpl.std_put != 0) 557 558 /* 559 * Service the deferred packet list. 560 * 561 * NOTE: drops the txq mutex! 562 */ 563 static void 564 sfxge_tx_qdpl_service(struct sfxge_txq *txq) 565 { 566 SFXGE_TXQ_LOCK_ASSERT_OWNED(txq); 567 568 do { 569 if (SFXGE_TX_QDPL_PENDING(txq)) 570 sfxge_tx_qdpl_swizzle(txq); 571 572 if (!txq->blocked) 573 sfxge_tx_qdpl_drain(txq); 574 575 SFXGE_TXQ_UNLOCK(txq); 576 } while (SFXGE_TX_QDPL_PENDING(txq) && 577 SFXGE_TXQ_TRYLOCK(txq)); 578 } 579 580 /* 581 * Put a packet on the deferred packet get-list. 582 */ 583 static int 584 sfxge_tx_qdpl_put_locked(struct sfxge_txq *txq, struct mbuf *mbuf) 585 { 586 struct sfxge_tx_dpl *stdp; 587 588 stdp = &txq->dpl; 589 590 KASSERT(mbuf->m_nextpkt == NULL, ("mbuf->m_nextpkt != NULL")); 591 592 SFXGE_TXQ_LOCK_ASSERT_OWNED(txq); 593 594 if (stdp->std_get_count >= stdp->std_get_max) { 595 txq->get_overflow++; 596 return (ENOBUFS); 597 } 598 if (sfxge_is_mbuf_non_tcp(mbuf)) { 599 if (stdp->std_get_non_tcp_count >= 600 stdp->std_get_non_tcp_max) { 601 txq->get_non_tcp_overflow++; 602 return (ENOBUFS); 603 } 604 stdp->std_get_non_tcp_count++; 605 } 606 607 *(stdp->std_getp) = mbuf; 608 stdp->std_getp = &mbuf->m_nextpkt; 609 stdp->std_get_count++; 610 611 return (0); 612 } 613 614 /* 615 * Put a packet on the deferred packet put-list. 616 * 617 * We overload the csum_data field in the mbuf to keep track of this length 618 * because there is no cheap alternative to avoid races. 619 */ 620 static int 621 sfxge_tx_qdpl_put_unlocked(struct sfxge_txq *txq, struct mbuf *mbuf) 622 { 623 struct sfxge_tx_dpl *stdp; 624 volatile uintptr_t *putp; 625 uintptr_t old; 626 uintptr_t new; 627 unsigned int put_count; 628 629 KASSERT(mbuf->m_nextpkt == NULL, ("mbuf->m_nextpkt != NULL")); 630 631 SFXGE_TXQ_LOCK_ASSERT_NOTOWNED(txq); 632 633 stdp = &txq->dpl; 634 putp = &stdp->std_put; 635 new = (uintptr_t)mbuf; 636 637 do { 638 old = *putp; 639 if (old != 0) { 640 struct mbuf *mp = (struct mbuf *)old; 641 put_count = mp->m_pkthdr.csum_data; 642 } else 643 put_count = 0; 644 if (put_count >= stdp->std_put_max) { 645 atomic_add_long(&txq->put_overflow, 1); 646 return (ENOBUFS); 647 } 648 mbuf->m_pkthdr.csum_data = put_count + 1; 649 mbuf->m_nextpkt = (void *)old; 650 } while (atomic_cmpset_ptr(putp, old, new) == 0); 651 652 return (0); 653 } 654 655 /* 656 * Called from if_transmit - will try to grab the txq lock and enqueue to the 657 * put list if it succeeds, otherwise try to push onto the defer list if space. 658 */ 659 static int 660 sfxge_tx_packet_add(struct sfxge_txq *txq, struct mbuf *m) 661 { 662 int rc; 663 664 if (!SFXGE_LINK_UP(txq->sc)) { 665 atomic_add_long(&txq->netdown_drops, 1); 666 return (ENETDOWN); 667 } 668 669 /* 670 * Try to grab the txq lock. If we are able to get the lock, 671 * the packet will be appended to the "get list" of the deferred 672 * packet list. Otherwise, it will be pushed on the "put list". 673 */ 674 if (SFXGE_TXQ_TRYLOCK(txq)) { 675 /* First swizzle put-list to get-list to keep order */ 676 sfxge_tx_qdpl_swizzle(txq); 677 678 rc = sfxge_tx_qdpl_put_locked(txq, m); 679 680 /* Try to service the list. */ 681 sfxge_tx_qdpl_service(txq); 682 /* Lock has been dropped. */ 683 } else { 684 rc = sfxge_tx_qdpl_put_unlocked(txq, m); 685 686 /* 687 * Try to grab the lock again. 688 * 689 * If we are able to get the lock, we need to process 690 * the deferred packet list. If we are not able to get 691 * the lock, another thread is processing the list. 692 */ 693 if ((rc == 0) && SFXGE_TXQ_TRYLOCK(txq)) { 694 sfxge_tx_qdpl_service(txq); 695 /* Lock has been dropped. */ 696 } 697 } 698 699 SFXGE_TXQ_LOCK_ASSERT_NOTOWNED(txq); 700 701 return (rc); 702 } 703 704 static void 705 sfxge_tx_qdpl_flush(struct sfxge_txq *txq) 706 { 707 struct sfxge_tx_dpl *stdp = &txq->dpl; 708 struct mbuf *mbuf, *next; 709 710 SFXGE_TXQ_LOCK(txq); 711 712 sfxge_tx_qdpl_swizzle(txq); 713 for (mbuf = stdp->std_get; mbuf != NULL; mbuf = next) { 714 next = mbuf->m_nextpkt; 715 m_freem(mbuf); 716 } 717 stdp->std_get = NULL; 718 stdp->std_get_count = 0; 719 stdp->std_get_non_tcp_count = 0; 720 stdp->std_getp = &stdp->std_get; 721 722 SFXGE_TXQ_UNLOCK(txq); 723 } 724 725 void 726 sfxge_if_qflush(struct ifnet *ifp) 727 { 728 struct sfxge_softc *sc; 729 unsigned int i; 730 731 sc = ifp->if_softc; 732 733 for (i = 0; i < sc->txq_count; i++) 734 sfxge_tx_qdpl_flush(sc->txq[i]); 735 } 736 737 #if SFXGE_TX_PARSE_EARLY 738 739 /* There is little space for user data in mbuf pkthdr, so we 740 * use l*hlen fields which are not used by the driver otherwise 741 * to store header offsets. 742 * The fields are 8-bit, but it's ok, no header may be longer than 255 bytes. 743 */ 744 745 746 #define TSO_MBUF_PROTO(_mbuf) ((_mbuf)->m_pkthdr.PH_loc.sixteen[0]) 747 /* We abuse l5hlen here because PH_loc can hold only 64 bits of data */ 748 #define TSO_MBUF_FLAGS(_mbuf) ((_mbuf)->m_pkthdr.l5hlen) 749 #define TSO_MBUF_PACKETID(_mbuf) ((_mbuf)->m_pkthdr.PH_loc.sixteen[1]) 750 #define TSO_MBUF_SEQNUM(_mbuf) ((_mbuf)->m_pkthdr.PH_loc.thirtytwo[1]) 751 752 static void sfxge_parse_tx_packet(struct mbuf *mbuf) 753 { 754 struct ether_header *eh = mtod(mbuf, struct ether_header *); 755 const struct tcphdr *th; 756 struct tcphdr th_copy; 757 758 /* Find network protocol and header */ 759 TSO_MBUF_PROTO(mbuf) = eh->ether_type; 760 if (TSO_MBUF_PROTO(mbuf) == htons(ETHERTYPE_VLAN)) { 761 struct ether_vlan_header *veh = 762 mtod(mbuf, struct ether_vlan_header *); 763 TSO_MBUF_PROTO(mbuf) = veh->evl_proto; 764 mbuf->m_pkthdr.l2hlen = sizeof(*veh); 765 } else { 766 mbuf->m_pkthdr.l2hlen = sizeof(*eh); 767 } 768 769 /* Find TCP header */ 770 if (TSO_MBUF_PROTO(mbuf) == htons(ETHERTYPE_IP)) { 771 const struct ip *iph = (const struct ip *)mtodo(mbuf, mbuf->m_pkthdr.l2hlen); 772 773 KASSERT(iph->ip_p == IPPROTO_TCP, 774 ("TSO required on non-TCP packet")); 775 mbuf->m_pkthdr.l3hlen = mbuf->m_pkthdr.l2hlen + 4 * iph->ip_hl; 776 TSO_MBUF_PACKETID(mbuf) = iph->ip_id; 777 } else { 778 KASSERT(TSO_MBUF_PROTO(mbuf) == htons(ETHERTYPE_IPV6), 779 ("TSO required on non-IP packet")); 780 KASSERT(((const struct ip6_hdr *)mtodo(mbuf, mbuf->m_pkthdr.l2hlen))->ip6_nxt == 781 IPPROTO_TCP, 782 ("TSO required on non-TCP packet")); 783 mbuf->m_pkthdr.l3hlen = mbuf->m_pkthdr.l2hlen + sizeof(struct ip6_hdr); 784 TSO_MBUF_PACKETID(mbuf) = 0; 785 } 786 787 KASSERT(mbuf->m_len >= mbuf->m_pkthdr.l3hlen, 788 ("network header is fragmented in mbuf")); 789 790 /* We need TCP header including flags (window is the next) */ 791 if (mbuf->m_len < mbuf->m_pkthdr.l3hlen + offsetof(struct tcphdr, th_win)) { 792 m_copydata(mbuf, mbuf->m_pkthdr.l3hlen, sizeof(th_copy), 793 (caddr_t)&th_copy); 794 th = &th_copy; 795 } else { 796 th = (const struct tcphdr *)mtodo(mbuf, mbuf->m_pkthdr.l3hlen); 797 } 798 799 mbuf->m_pkthdr.l4hlen = mbuf->m_pkthdr.l3hlen + 4 * th->th_off; 800 TSO_MBUF_SEQNUM(mbuf) = ntohl(th->th_seq); 801 802 /* These flags must not be duplicated */ 803 /* 804 * RST should not be duplicated as well, but FreeBSD kernel 805 * generates TSO packets with RST flag. So, do not assert 806 * its absence. 807 */ 808 KASSERT(!(th->th_flags & (TH_URG | TH_SYN)), 809 ("incompatible TCP flag 0x%x on TSO packet", 810 th->th_flags & (TH_URG | TH_SYN))); 811 TSO_MBUF_FLAGS(mbuf) = th->th_flags; 812 } 813 #endif 814 815 /* 816 * TX start -- called by the stack. 817 */ 818 int 819 sfxge_if_transmit(struct ifnet *ifp, struct mbuf *m) 820 { 821 struct sfxge_softc *sc; 822 struct sfxge_txq *txq; 823 int rc; 824 825 sc = (struct sfxge_softc *)ifp->if_softc; 826 827 /* 828 * Transmit may be called when interface is up from the kernel 829 * point of view, but not yet up (in progress) from the driver 830 * point of view. I.e. link aggregation bring up. 831 * Transmit may be called when interface is up from the driver 832 * point of view, but already down from the kernel point of 833 * view. I.e. Rx when interface shutdown is in progress. 834 */ 835 KASSERT((ifp->if_flags & IFF_UP) || (sc->if_flags & IFF_UP), 836 ("interface not up")); 837 838 /* Pick the desired transmit queue. */ 839 if (m->m_pkthdr.csum_flags & 840 (CSUM_DELAY_DATA | CSUM_TCP_IPV6 | CSUM_UDP_IPV6 | CSUM_TSO)) { 841 int index = 0; 842 843 #ifdef RSS 844 uint32_t bucket_id; 845 846 /* 847 * Select a TX queue which matches the corresponding 848 * RX queue for the hash in order to assign both 849 * TX and RX parts of the flow to the same CPU 850 */ 851 if (rss_m2bucket(m, &bucket_id) == 0) 852 index = bucket_id % (sc->txq_count - (SFXGE_TXQ_NTYPES - 1)); 853 #else 854 /* check if flowid is set */ 855 if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) { 856 uint32_t hash = m->m_pkthdr.flowid; 857 uint32_t idx = hash % nitems(sc->rx_indir_table); 858 859 index = sc->rx_indir_table[idx]; 860 } 861 #endif 862 #if SFXGE_TX_PARSE_EARLY 863 if (m->m_pkthdr.csum_flags & CSUM_TSO) 864 sfxge_parse_tx_packet(m); 865 #endif 866 txq = sc->txq[SFXGE_TXQ_IP_TCP_UDP_CKSUM + index]; 867 } else if (m->m_pkthdr.csum_flags & CSUM_DELAY_IP) { 868 txq = sc->txq[SFXGE_TXQ_IP_CKSUM]; 869 } else { 870 txq = sc->txq[SFXGE_TXQ_NON_CKSUM]; 871 } 872 873 rc = sfxge_tx_packet_add(txq, m); 874 if (rc != 0) 875 m_freem(m); 876 877 return (rc); 878 } 879 880 /* 881 * Software "TSO". Not quite as good as doing it in hardware, but 882 * still faster than segmenting in the stack. 883 */ 884 885 struct sfxge_tso_state { 886 /* Output position */ 887 unsigned out_len; /* Remaining length in current segment */ 888 unsigned seqnum; /* Current sequence number */ 889 unsigned packet_space; /* Remaining space in current packet */ 890 unsigned segs_space; /* Remaining number of DMA segments 891 for the packet (FATSOv2 only) */ 892 893 /* Input position */ 894 uint64_t dma_addr; /* DMA address of current position */ 895 unsigned in_len; /* Remaining length in current mbuf */ 896 897 const struct mbuf *mbuf; /* Input mbuf (head of chain) */ 898 u_short protocol; /* Network protocol (after VLAN decap) */ 899 ssize_t nh_off; /* Offset of network header */ 900 ssize_t tcph_off; /* Offset of TCP header */ 901 unsigned header_len; /* Number of bytes of header */ 902 unsigned seg_size; /* TCP segment size */ 903 int fw_assisted; /* Use FW-assisted TSO */ 904 u_short packet_id; /* IPv4 packet ID from the original packet */ 905 uint8_t tcp_flags; /* TCP flags */ 906 efx_desc_t header_desc; /* Precomputed header descriptor for 907 * FW-assisted TSO */ 908 }; 909 910 #if !SFXGE_TX_PARSE_EARLY 911 static const struct ip *tso_iph(const struct sfxge_tso_state *tso) 912 { 913 KASSERT(tso->protocol == htons(ETHERTYPE_IP), 914 ("tso_iph() in non-IPv4 state")); 915 return (const struct ip *)(tso->mbuf->m_data + tso->nh_off); 916 } 917 918 static __unused const struct ip6_hdr *tso_ip6h(const struct sfxge_tso_state *tso) 919 { 920 KASSERT(tso->protocol == htons(ETHERTYPE_IPV6), 921 ("tso_ip6h() in non-IPv6 state")); 922 return (const struct ip6_hdr *)(tso->mbuf->m_data + tso->nh_off); 923 } 924 925 static const struct tcphdr *tso_tcph(const struct sfxge_tso_state *tso) 926 { 927 return (const struct tcphdr *)(tso->mbuf->m_data + tso->tcph_off); 928 } 929 #endif 930 931 932 /* Size of preallocated TSO header buffers. Larger blocks must be 933 * allocated from the heap. 934 */ 935 #define TSOH_STD_SIZE 128 936 937 /* At most half the descriptors in the queue at any time will refer to 938 * a TSO header buffer, since they must always be followed by a 939 * payload descriptor referring to an mbuf. 940 */ 941 #define TSOH_COUNT(_txq_entries) ((_txq_entries) / 2u) 942 #define TSOH_PER_PAGE (PAGE_SIZE / TSOH_STD_SIZE) 943 #define TSOH_PAGE_COUNT(_txq_entries) \ 944 howmany(TSOH_COUNT(_txq_entries), TSOH_PER_PAGE) 945 946 static int tso_init(struct sfxge_txq *txq) 947 { 948 struct sfxge_softc *sc = txq->sc; 949 unsigned int tsoh_page_count = TSOH_PAGE_COUNT(sc->txq_entries); 950 int i, rc; 951 952 /* Allocate TSO header buffers */ 953 txq->tsoh_buffer = malloc(tsoh_page_count * sizeof(txq->tsoh_buffer[0]), 954 M_SFXGE, M_WAITOK); 955 956 for (i = 0; i < tsoh_page_count; i++) { 957 rc = sfxge_dma_alloc(sc, PAGE_SIZE, &txq->tsoh_buffer[i]); 958 if (rc != 0) 959 goto fail; 960 } 961 962 return (0); 963 964 fail: 965 while (i-- > 0) 966 sfxge_dma_free(&txq->tsoh_buffer[i]); 967 free(txq->tsoh_buffer, M_SFXGE); 968 txq->tsoh_buffer = NULL; 969 return (rc); 970 } 971 972 static void tso_fini(struct sfxge_txq *txq) 973 { 974 int i; 975 976 if (txq->tsoh_buffer != NULL) { 977 for (i = 0; i < TSOH_PAGE_COUNT(txq->sc->txq_entries); i++) 978 sfxge_dma_free(&txq->tsoh_buffer[i]); 979 free(txq->tsoh_buffer, M_SFXGE); 980 } 981 } 982 983 static void tso_start(struct sfxge_txq *txq, struct sfxge_tso_state *tso, 984 const bus_dma_segment_t *hdr_dma_seg, 985 struct mbuf *mbuf) 986 { 987 const efx_nic_cfg_t *encp = efx_nic_cfg_get(txq->sc->enp); 988 #if !SFXGE_TX_PARSE_EARLY 989 struct ether_header *eh = mtod(mbuf, struct ether_header *); 990 const struct tcphdr *th; 991 struct tcphdr th_copy; 992 #endif 993 994 tso->fw_assisted = txq->tso_fw_assisted; 995 tso->mbuf = mbuf; 996 997 /* Find network protocol and header */ 998 #if !SFXGE_TX_PARSE_EARLY 999 tso->protocol = eh->ether_type; 1000 if (tso->protocol == htons(ETHERTYPE_VLAN)) { 1001 struct ether_vlan_header *veh = 1002 mtod(mbuf, struct ether_vlan_header *); 1003 tso->protocol = veh->evl_proto; 1004 tso->nh_off = sizeof(*veh); 1005 } else { 1006 tso->nh_off = sizeof(*eh); 1007 } 1008 #else 1009 tso->protocol = TSO_MBUF_PROTO(mbuf); 1010 tso->nh_off = mbuf->m_pkthdr.l2hlen; 1011 tso->tcph_off = mbuf->m_pkthdr.l3hlen; 1012 tso->packet_id = ntohs(TSO_MBUF_PACKETID(mbuf)); 1013 #endif 1014 1015 #if !SFXGE_TX_PARSE_EARLY 1016 /* Find TCP header */ 1017 if (tso->protocol == htons(ETHERTYPE_IP)) { 1018 KASSERT(tso_iph(tso)->ip_p == IPPROTO_TCP, 1019 ("TSO required on non-TCP packet")); 1020 tso->tcph_off = tso->nh_off + 4 * tso_iph(tso)->ip_hl; 1021 tso->packet_id = ntohs(tso_iph(tso)->ip_id); 1022 } else { 1023 KASSERT(tso->protocol == htons(ETHERTYPE_IPV6), 1024 ("TSO required on non-IP packet")); 1025 KASSERT(tso_ip6h(tso)->ip6_nxt == IPPROTO_TCP, 1026 ("TSO required on non-TCP packet")); 1027 tso->tcph_off = tso->nh_off + sizeof(struct ip6_hdr); 1028 tso->packet_id = 0; 1029 } 1030 #endif 1031 1032 1033 if (tso->fw_assisted && 1034 __predict_false(tso->tcph_off > 1035 encp->enc_tx_tso_tcp_header_offset_limit)) { 1036 tso->fw_assisted = 0; 1037 } 1038 1039 1040 #if !SFXGE_TX_PARSE_EARLY 1041 KASSERT(mbuf->m_len >= tso->tcph_off, 1042 ("network header is fragmented in mbuf")); 1043 /* We need TCP header including flags (window is the next) */ 1044 if (mbuf->m_len < tso->tcph_off + offsetof(struct tcphdr, th_win)) { 1045 m_copydata(tso->mbuf, tso->tcph_off, sizeof(th_copy), 1046 (caddr_t)&th_copy); 1047 th = &th_copy; 1048 } else { 1049 th = tso_tcph(tso); 1050 } 1051 tso->header_len = tso->tcph_off + 4 * th->th_off; 1052 #else 1053 tso->header_len = mbuf->m_pkthdr.l4hlen; 1054 #endif 1055 tso->seg_size = mbuf->m_pkthdr.tso_segsz; 1056 1057 #if !SFXGE_TX_PARSE_EARLY 1058 tso->seqnum = ntohl(th->th_seq); 1059 1060 /* These flags must not be duplicated */ 1061 /* 1062 * RST should not be duplicated as well, but FreeBSD kernel 1063 * generates TSO packets with RST flag. So, do not assert 1064 * its absence. 1065 */ 1066 KASSERT(!(th->th_flags & (TH_URG | TH_SYN)), 1067 ("incompatible TCP flag 0x%x on TSO packet", 1068 th->th_flags & (TH_URG | TH_SYN))); 1069 tso->tcp_flags = th->th_flags; 1070 #else 1071 tso->seqnum = TSO_MBUF_SEQNUM(mbuf); 1072 tso->tcp_flags = TSO_MBUF_FLAGS(mbuf); 1073 #endif 1074 1075 tso->out_len = mbuf->m_pkthdr.len - tso->header_len; 1076 1077 if (tso->fw_assisted) { 1078 if (hdr_dma_seg->ds_len >= tso->header_len) 1079 efx_tx_qdesc_dma_create(txq->common, 1080 hdr_dma_seg->ds_addr, 1081 tso->header_len, 1082 B_FALSE, 1083 &tso->header_desc); 1084 else 1085 tso->fw_assisted = 0; 1086 } 1087 } 1088 1089 /* 1090 * tso_fill_packet_with_fragment - form descriptors for the current fragment 1091 * 1092 * Form descriptors for the current fragment, until we reach the end 1093 * of fragment or end-of-packet. Return 0 on success, 1 if not enough 1094 * space. 1095 */ 1096 static void tso_fill_packet_with_fragment(struct sfxge_txq *txq, 1097 struct sfxge_tso_state *tso) 1098 { 1099 efx_desc_t *desc; 1100 int n; 1101 uint64_t dma_addr = tso->dma_addr; 1102 boolean_t eop; 1103 1104 if (tso->in_len == 0 || tso->packet_space == 0) 1105 return; 1106 1107 KASSERT(tso->in_len > 0, ("TSO input length went negative")); 1108 KASSERT(tso->packet_space > 0, ("TSO packet space went negative")); 1109 1110 if (tso->fw_assisted & SFXGE_FATSOV2) { 1111 n = tso->in_len; 1112 tso->out_len -= n; 1113 tso->seqnum += n; 1114 tso->in_len = 0; 1115 if (n < tso->packet_space) { 1116 tso->packet_space -= n; 1117 tso->segs_space--; 1118 } else { 1119 tso->packet_space = tso->seg_size - 1120 (n - tso->packet_space) % tso->seg_size; 1121 tso->segs_space = 1122 EFX_TX_FATSOV2_DMA_SEGS_PER_PKT_MAX - 1 - 1123 (tso->packet_space != tso->seg_size); 1124 } 1125 } else { 1126 n = min(tso->in_len, tso->packet_space); 1127 tso->packet_space -= n; 1128 tso->out_len -= n; 1129 tso->dma_addr += n; 1130 tso->in_len -= n; 1131 } 1132 1133 /* 1134 * It is OK to use binary OR below to avoid extra branching 1135 * since all conditions may always be checked. 1136 */ 1137 eop = (tso->out_len == 0) | (tso->packet_space == 0) | 1138 (tso->segs_space == 0); 1139 1140 desc = &txq->pend_desc[txq->n_pend_desc++]; 1141 efx_tx_qdesc_dma_create(txq->common, dma_addr, n, eop, desc); 1142 } 1143 1144 /* Callback from bus_dmamap_load() for long TSO headers. */ 1145 static void tso_map_long_header(void *dma_addr_ret, 1146 bus_dma_segment_t *segs, int nseg, 1147 int error) 1148 { 1149 *(uint64_t *)dma_addr_ret = ((__predict_true(error == 0) && 1150 __predict_true(nseg == 1)) ? 1151 segs->ds_addr : 0); 1152 } 1153 1154 /* 1155 * tso_start_new_packet - generate a new header and prepare for the new packet 1156 * 1157 * Generate a new header and prepare for the new packet. Return 0 on 1158 * success, or an error code if failed to alloc header. 1159 */ 1160 static int tso_start_new_packet(struct sfxge_txq *txq, 1161 struct sfxge_tso_state *tso, 1162 unsigned int *idp) 1163 { 1164 unsigned int id = *idp; 1165 struct tcphdr *tsoh_th; 1166 unsigned ip_length; 1167 caddr_t header; 1168 uint64_t dma_addr; 1169 bus_dmamap_t map; 1170 efx_desc_t *desc; 1171 int rc; 1172 1173 if (tso->fw_assisted) { 1174 if (tso->fw_assisted & SFXGE_FATSOV2) { 1175 /* Add 2 FATSOv2 option descriptors */ 1176 desc = &txq->pend_desc[txq->n_pend_desc]; 1177 efx_tx_qdesc_tso2_create(txq->common, 1178 tso->packet_id, 1179 tso->seqnum, 1180 tso->seg_size, 1181 desc, 1182 EFX_TX_FATSOV2_OPT_NDESCS); 1183 desc += EFX_TX_FATSOV2_OPT_NDESCS; 1184 txq->n_pend_desc += EFX_TX_FATSOV2_OPT_NDESCS; 1185 KASSERT(txq->stmp[id].flags == 0, ("stmp flags are not 0")); 1186 id = (id + EFX_TX_FATSOV2_OPT_NDESCS) & txq->ptr_mask; 1187 1188 tso->segs_space = 1189 EFX_TX_FATSOV2_DMA_SEGS_PER_PKT_MAX - 1; 1190 } else { 1191 uint8_t tcp_flags = tso->tcp_flags; 1192 1193 if (tso->out_len > tso->seg_size) 1194 tcp_flags &= ~(TH_FIN | TH_PUSH); 1195 1196 /* Add FATSOv1 option descriptor */ 1197 desc = &txq->pend_desc[txq->n_pend_desc++]; 1198 efx_tx_qdesc_tso_create(txq->common, 1199 tso->packet_id, 1200 tso->seqnum, 1201 tcp_flags, 1202 desc++); 1203 KASSERT(txq->stmp[id].flags == 0, ("stmp flags are not 0")); 1204 id = (id + 1) & txq->ptr_mask; 1205 1206 tso->seqnum += tso->seg_size; 1207 tso->segs_space = UINT_MAX; 1208 } 1209 1210 /* Header DMA descriptor */ 1211 *desc = tso->header_desc; 1212 txq->n_pend_desc++; 1213 KASSERT(txq->stmp[id].flags == 0, ("stmp flags are not 0")); 1214 id = (id + 1) & txq->ptr_mask; 1215 } else { 1216 /* Allocate a DMA-mapped header buffer. */ 1217 if (__predict_true(tso->header_len <= TSOH_STD_SIZE)) { 1218 unsigned int page_index = (id / 2) / TSOH_PER_PAGE; 1219 unsigned int buf_index = (id / 2) % TSOH_PER_PAGE; 1220 1221 header = (txq->tsoh_buffer[page_index].esm_base + 1222 buf_index * TSOH_STD_SIZE); 1223 dma_addr = (txq->tsoh_buffer[page_index].esm_addr + 1224 buf_index * TSOH_STD_SIZE); 1225 map = txq->tsoh_buffer[page_index].esm_map; 1226 1227 KASSERT(txq->stmp[id].flags == 0, 1228 ("stmp flags are not 0")); 1229 } else { 1230 struct sfxge_tx_mapping *stmp = &txq->stmp[id]; 1231 1232 /* We cannot use bus_dmamem_alloc() as that may sleep */ 1233 header = malloc(tso->header_len, M_SFXGE, M_NOWAIT); 1234 if (__predict_false(!header)) 1235 return (ENOMEM); 1236 rc = bus_dmamap_load(txq->packet_dma_tag, stmp->map, 1237 header, tso->header_len, 1238 tso_map_long_header, &dma_addr, 1239 BUS_DMA_NOWAIT); 1240 if (__predict_false(dma_addr == 0)) { 1241 if (rc == 0) { 1242 /* Succeeded but got >1 segment */ 1243 bus_dmamap_unload(txq->packet_dma_tag, 1244 stmp->map); 1245 rc = EINVAL; 1246 } 1247 free(header, M_SFXGE); 1248 return (rc); 1249 } 1250 map = stmp->map; 1251 1252 txq->tso_long_headers++; 1253 stmp->u.heap_buf = header; 1254 stmp->flags = TX_BUF_UNMAP; 1255 } 1256 1257 tsoh_th = (struct tcphdr *)(header + tso->tcph_off); 1258 1259 /* Copy and update the headers. */ 1260 m_copydata(tso->mbuf, 0, tso->header_len, header); 1261 1262 tsoh_th->th_seq = htonl(tso->seqnum); 1263 tso->seqnum += tso->seg_size; 1264 if (tso->out_len > tso->seg_size) { 1265 /* This packet will not finish the TSO burst. */ 1266 ip_length = tso->header_len - tso->nh_off + tso->seg_size; 1267 tsoh_th->th_flags &= ~(TH_FIN | TH_PUSH); 1268 } else { 1269 /* This packet will be the last in the TSO burst. */ 1270 ip_length = tso->header_len - tso->nh_off + tso->out_len; 1271 } 1272 1273 if (tso->protocol == htons(ETHERTYPE_IP)) { 1274 struct ip *tsoh_iph = (struct ip *)(header + tso->nh_off); 1275 tsoh_iph->ip_len = htons(ip_length); 1276 /* XXX We should increment ip_id, but FreeBSD doesn't 1277 * currently allocate extra IDs for multiple segments. 1278 */ 1279 } else { 1280 struct ip6_hdr *tsoh_iph = 1281 (struct ip6_hdr *)(header + tso->nh_off); 1282 tsoh_iph->ip6_plen = htons(ip_length - sizeof(*tsoh_iph)); 1283 } 1284 1285 /* Make the header visible to the hardware. */ 1286 bus_dmamap_sync(txq->packet_dma_tag, map, BUS_DMASYNC_PREWRITE); 1287 1288 /* Form a descriptor for this header. */ 1289 desc = &txq->pend_desc[txq->n_pend_desc++]; 1290 efx_tx_qdesc_dma_create(txq->common, 1291 dma_addr, 1292 tso->header_len, 1293 0, 1294 desc); 1295 id = (id + 1) & txq->ptr_mask; 1296 1297 tso->segs_space = UINT_MAX; 1298 } 1299 tso->packet_space = tso->seg_size; 1300 txq->tso_packets++; 1301 *idp = id; 1302 1303 return (0); 1304 } 1305 1306 static int 1307 sfxge_tx_queue_tso(struct sfxge_txq *txq, struct mbuf *mbuf, 1308 const bus_dma_segment_t *dma_seg, int n_dma_seg, 1309 int vlan_tagged) 1310 { 1311 struct sfxge_tso_state tso; 1312 unsigned int id; 1313 unsigned skipped = 0; 1314 1315 tso_start(txq, &tso, dma_seg, mbuf); 1316 1317 while (dma_seg->ds_len + skipped <= tso.header_len) { 1318 skipped += dma_seg->ds_len; 1319 --n_dma_seg; 1320 KASSERT(n_dma_seg, ("no payload found in TSO packet")); 1321 ++dma_seg; 1322 } 1323 tso.in_len = dma_seg->ds_len - (tso.header_len - skipped); 1324 tso.dma_addr = dma_seg->ds_addr + (tso.header_len - skipped); 1325 1326 id = (txq->added + vlan_tagged) & txq->ptr_mask; 1327 if (__predict_false(tso_start_new_packet(txq, &tso, &id))) 1328 return (-1); 1329 1330 while (1) { 1331 tso_fill_packet_with_fragment(txq, &tso); 1332 /* Exactly one DMA descriptor is added */ 1333 KASSERT(txq->stmp[id].flags == 0, ("stmp flags are not 0")); 1334 id = (id + 1) & txq->ptr_mask; 1335 1336 /* Move onto the next fragment? */ 1337 if (tso.in_len == 0) { 1338 --n_dma_seg; 1339 if (n_dma_seg == 0) 1340 break; 1341 ++dma_seg; 1342 tso.in_len = dma_seg->ds_len; 1343 tso.dma_addr = dma_seg->ds_addr; 1344 } 1345 1346 /* End of packet? */ 1347 if ((tso.packet_space == 0) | (tso.segs_space == 0)) { 1348 unsigned int n_fatso_opt_desc = 1349 (tso.fw_assisted & SFXGE_FATSOV2) ? 1350 EFX_TX_FATSOV2_OPT_NDESCS : 1351 (tso.fw_assisted & SFXGE_FATSOV1) ? 1 : 0; 1352 1353 /* If the queue is now full due to tiny MSS, 1354 * or we can't create another header, discard 1355 * the remainder of the input mbuf but do not 1356 * roll back the work we have done. 1357 */ 1358 if (txq->n_pend_desc + n_fatso_opt_desc + 1359 1 /* header */ + n_dma_seg > txq->max_pkt_desc) { 1360 txq->tso_pdrop_too_many++; 1361 break; 1362 } 1363 if (__predict_false(tso_start_new_packet(txq, &tso, 1364 &id))) { 1365 txq->tso_pdrop_no_rsrc++; 1366 break; 1367 } 1368 } 1369 } 1370 1371 txq->tso_bursts++; 1372 return (id); 1373 } 1374 1375 static void 1376 sfxge_tx_qunblock(struct sfxge_txq *txq) 1377 { 1378 struct sfxge_softc *sc; 1379 struct sfxge_evq *evq; 1380 1381 sc = txq->sc; 1382 evq = sc->evq[txq->evq_index]; 1383 1384 SFXGE_EVQ_LOCK_ASSERT_OWNED(evq); 1385 1386 if (__predict_false(txq->init_state != SFXGE_TXQ_STARTED)) 1387 return; 1388 1389 SFXGE_TXQ_LOCK(txq); 1390 1391 if (txq->blocked) { 1392 unsigned int level; 1393 1394 level = txq->added - txq->completed; 1395 if (level <= SFXGE_TXQ_UNBLOCK_LEVEL(txq->entries)) { 1396 /* reaped must be in sync with blocked */ 1397 sfxge_tx_qreap(txq); 1398 txq->blocked = 0; 1399 } 1400 } 1401 1402 sfxge_tx_qdpl_service(txq); 1403 /* note: lock has been dropped */ 1404 } 1405 1406 void 1407 sfxge_tx_qflush_done(struct sfxge_txq *txq) 1408 { 1409 1410 txq->flush_state = SFXGE_FLUSH_DONE; 1411 } 1412 1413 static void 1414 sfxge_tx_qstop(struct sfxge_softc *sc, unsigned int index) 1415 { 1416 struct sfxge_txq *txq; 1417 struct sfxge_evq *evq; 1418 unsigned int count; 1419 1420 SFXGE_ADAPTER_LOCK_ASSERT_OWNED(sc); 1421 1422 txq = sc->txq[index]; 1423 evq = sc->evq[txq->evq_index]; 1424 1425 SFXGE_EVQ_LOCK(evq); 1426 SFXGE_TXQ_LOCK(txq); 1427 1428 KASSERT(txq->init_state == SFXGE_TXQ_STARTED, 1429 ("txq->init_state != SFXGE_TXQ_STARTED")); 1430 1431 txq->init_state = SFXGE_TXQ_INITIALIZED; 1432 1433 if (txq->flush_state != SFXGE_FLUSH_DONE) { 1434 txq->flush_state = SFXGE_FLUSH_PENDING; 1435 1436 SFXGE_EVQ_UNLOCK(evq); 1437 SFXGE_TXQ_UNLOCK(txq); 1438 1439 /* Flush the transmit queue. */ 1440 if (efx_tx_qflush(txq->common) != 0) { 1441 log(LOG_ERR, "%s: Flushing Tx queue %u failed\n", 1442 device_get_nameunit(sc->dev), index); 1443 txq->flush_state = SFXGE_FLUSH_DONE; 1444 } else { 1445 count = 0; 1446 do { 1447 /* Spin for 100ms. */ 1448 DELAY(100000); 1449 if (txq->flush_state != SFXGE_FLUSH_PENDING) 1450 break; 1451 } while (++count < 20); 1452 } 1453 SFXGE_EVQ_LOCK(evq); 1454 SFXGE_TXQ_LOCK(txq); 1455 1456 KASSERT(txq->flush_state != SFXGE_FLUSH_FAILED, 1457 ("txq->flush_state == SFXGE_FLUSH_FAILED")); 1458 1459 if (txq->flush_state != SFXGE_FLUSH_DONE) { 1460 /* Flush timeout */ 1461 log(LOG_ERR, "%s: Cannot flush Tx queue %u\n", 1462 device_get_nameunit(sc->dev), index); 1463 txq->flush_state = SFXGE_FLUSH_DONE; 1464 } 1465 } 1466 1467 txq->blocked = 0; 1468 txq->pending = txq->added; 1469 1470 sfxge_tx_qcomplete(txq, evq); 1471 KASSERT(txq->completed == txq->added, 1472 ("txq->completed != txq->added")); 1473 1474 sfxge_tx_qreap(txq); 1475 KASSERT(txq->reaped == txq->completed, 1476 ("txq->reaped != txq->completed")); 1477 1478 txq->added = 0; 1479 txq->pending = 0; 1480 txq->completed = 0; 1481 txq->reaped = 0; 1482 1483 /* Destroy the common code transmit queue. */ 1484 efx_tx_qdestroy(txq->common); 1485 txq->common = NULL; 1486 1487 efx_sram_buf_tbl_clear(sc->enp, txq->buf_base_id, 1488 EFX_TXQ_NBUFS(sc->txq_entries)); 1489 1490 SFXGE_EVQ_UNLOCK(evq); 1491 SFXGE_TXQ_UNLOCK(txq); 1492 } 1493 1494 /* 1495 * Estimate maximum number of Tx descriptors required for TSO packet. 1496 * With minimum MSS and maximum mbuf length we might need more (even 1497 * than a ring-ful of descriptors), but this should not happen in 1498 * practice except due to deliberate attack. In that case we will 1499 * truncate the output at a packet boundary. 1500 */ 1501 static unsigned int 1502 sfxge_tx_max_pkt_desc(const struct sfxge_softc *sc, enum sfxge_txq_type type, 1503 unsigned int tso_fw_assisted) 1504 { 1505 /* One descriptor for every input fragment */ 1506 unsigned int max_descs = SFXGE_TX_MAPPING_MAX_SEG; 1507 unsigned int sw_tso_max_descs; 1508 unsigned int fa_tso_v1_max_descs = 0; 1509 unsigned int fa_tso_v2_max_descs = 0; 1510 1511 /* VLAN tagging Tx option descriptor may be required */ 1512 if (efx_nic_cfg_get(sc->enp)->enc_hw_tx_insert_vlan_enabled) 1513 max_descs++; 1514 1515 if (type == SFXGE_TXQ_IP_TCP_UDP_CKSUM) { 1516 /* 1517 * Plus header and payload descriptor for each output segment. 1518 * Minus one since header fragment is already counted. 1519 * Even if FATSO is used, we should be ready to fallback 1520 * to do it in the driver. 1521 */ 1522 sw_tso_max_descs = SFXGE_TSO_MAX_SEGS * 2 - 1; 1523 1524 /* FW assisted TSOv1 requires one more descriptor per segment 1525 * in comparison to SW TSO */ 1526 if (tso_fw_assisted & SFXGE_FATSOV1) 1527 fa_tso_v1_max_descs = 1528 sw_tso_max_descs + SFXGE_TSO_MAX_SEGS; 1529 1530 /* FW assisted TSOv2 requires 3 (2 FATSO plus header) extra 1531 * descriptors per superframe limited by number of DMA fetches 1532 * per packet. The first packet header is already counted. 1533 */ 1534 if (tso_fw_assisted & SFXGE_FATSOV2) { 1535 fa_tso_v2_max_descs = 1536 howmany(SFXGE_TX_MAPPING_MAX_SEG, 1537 EFX_TX_FATSOV2_DMA_SEGS_PER_PKT_MAX - 1) * 1538 (EFX_TX_FATSOV2_OPT_NDESCS + 1) - 1; 1539 } 1540 1541 max_descs += MAX(sw_tso_max_descs, 1542 MAX(fa_tso_v1_max_descs, fa_tso_v2_max_descs)); 1543 } 1544 1545 return (max_descs); 1546 } 1547 1548 static int 1549 sfxge_tx_qstart(struct sfxge_softc *sc, unsigned int index) 1550 { 1551 struct sfxge_txq *txq; 1552 efsys_mem_t *esmp; 1553 uint16_t flags; 1554 unsigned int tso_fw_assisted; 1555 struct sfxge_evq *evq; 1556 unsigned int desc_index; 1557 int rc; 1558 1559 SFXGE_ADAPTER_LOCK_ASSERT_OWNED(sc); 1560 1561 txq = sc->txq[index]; 1562 esmp = &txq->mem; 1563 evq = sc->evq[txq->evq_index]; 1564 1565 KASSERT(txq->init_state == SFXGE_TXQ_INITIALIZED, 1566 ("txq->init_state != SFXGE_TXQ_INITIALIZED")); 1567 KASSERT(evq->init_state == SFXGE_EVQ_STARTED, 1568 ("evq->init_state != SFXGE_EVQ_STARTED")); 1569 1570 /* Program the buffer table. */ 1571 if ((rc = efx_sram_buf_tbl_set(sc->enp, txq->buf_base_id, esmp, 1572 EFX_TXQ_NBUFS(sc->txq_entries))) != 0) 1573 return (rc); 1574 1575 /* Determine the kind of queue we are creating. */ 1576 tso_fw_assisted = 0; 1577 switch (txq->type) { 1578 case SFXGE_TXQ_NON_CKSUM: 1579 flags = 0; 1580 break; 1581 case SFXGE_TXQ_IP_CKSUM: 1582 flags = EFX_TXQ_CKSUM_IPV4; 1583 break; 1584 case SFXGE_TXQ_IP_TCP_UDP_CKSUM: 1585 flags = EFX_TXQ_CKSUM_IPV4 | EFX_TXQ_CKSUM_TCPUDP; 1586 tso_fw_assisted = sc->tso_fw_assisted; 1587 if (tso_fw_assisted & SFXGE_FATSOV2) 1588 flags |= EFX_TXQ_FATSOV2; 1589 break; 1590 default: 1591 KASSERT(0, ("Impossible TX queue")); 1592 flags = 0; 1593 break; 1594 } 1595 1596 /* Create the common code transmit queue. */ 1597 if ((rc = efx_tx_qcreate(sc->enp, index, txq->type, esmp, 1598 sc->txq_entries, txq->buf_base_id, flags, evq->common, 1599 &txq->common, &desc_index)) != 0) { 1600 /* Retry if no FATSOv2 resources, otherwise fail */ 1601 if ((rc != ENOSPC) || (~flags & EFX_TXQ_FATSOV2)) 1602 goto fail; 1603 1604 /* Looks like all FATSOv2 contexts are used */ 1605 flags &= ~EFX_TXQ_FATSOV2; 1606 tso_fw_assisted &= ~SFXGE_FATSOV2; 1607 if ((rc = efx_tx_qcreate(sc->enp, index, txq->type, esmp, 1608 sc->txq_entries, txq->buf_base_id, flags, evq->common, 1609 &txq->common, &desc_index)) != 0) 1610 goto fail; 1611 } 1612 1613 /* Initialise queue descriptor indexes */ 1614 txq->added = txq->pending = txq->completed = txq->reaped = desc_index; 1615 1616 SFXGE_TXQ_LOCK(txq); 1617 1618 /* Enable the transmit queue. */ 1619 efx_tx_qenable(txq->common); 1620 1621 txq->init_state = SFXGE_TXQ_STARTED; 1622 txq->flush_state = SFXGE_FLUSH_REQUIRED; 1623 txq->tso_fw_assisted = tso_fw_assisted; 1624 1625 txq->max_pkt_desc = sfxge_tx_max_pkt_desc(sc, txq->type, 1626 tso_fw_assisted); 1627 1628 txq->hw_vlan_tci = 0; 1629 1630 SFXGE_TXQ_UNLOCK(txq); 1631 1632 return (0); 1633 1634 fail: 1635 efx_sram_buf_tbl_clear(sc->enp, txq->buf_base_id, 1636 EFX_TXQ_NBUFS(sc->txq_entries)); 1637 return (rc); 1638 } 1639 1640 void 1641 sfxge_tx_stop(struct sfxge_softc *sc) 1642 { 1643 int index; 1644 1645 index = sc->txq_count; 1646 while (--index >= 0) 1647 sfxge_tx_qstop(sc, index); 1648 1649 /* Tear down the transmit module */ 1650 efx_tx_fini(sc->enp); 1651 } 1652 1653 int 1654 sfxge_tx_start(struct sfxge_softc *sc) 1655 { 1656 int index; 1657 int rc; 1658 1659 /* Initialize the common code transmit module. */ 1660 if ((rc = efx_tx_init(sc->enp)) != 0) 1661 return (rc); 1662 1663 for (index = 0; index < sc->txq_count; index++) { 1664 if ((rc = sfxge_tx_qstart(sc, index)) != 0) 1665 goto fail; 1666 } 1667 1668 return (0); 1669 1670 fail: 1671 while (--index >= 0) 1672 sfxge_tx_qstop(sc, index); 1673 1674 efx_tx_fini(sc->enp); 1675 1676 return (rc); 1677 } 1678 1679 static int 1680 sfxge_txq_stat_init(struct sfxge_txq *txq, struct sysctl_oid *txq_node) 1681 { 1682 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(txq->sc->dev); 1683 struct sysctl_oid *stat_node; 1684 unsigned int id; 1685 1686 stat_node = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(txq_node), OID_AUTO, 1687 "stats", CTLFLAG_RD, NULL, 1688 "Tx queue statistics"); 1689 if (stat_node == NULL) 1690 return (ENOMEM); 1691 1692 for (id = 0; id < nitems(sfxge_tx_stats); id++) { 1693 SYSCTL_ADD_ULONG( 1694 ctx, SYSCTL_CHILDREN(stat_node), OID_AUTO, 1695 sfxge_tx_stats[id].name, CTLFLAG_RD | CTLFLAG_STATS, 1696 (unsigned long *)((caddr_t)txq + sfxge_tx_stats[id].offset), 1697 ""); 1698 } 1699 1700 return (0); 1701 } 1702 1703 /** 1704 * Destroy a transmit queue. 1705 */ 1706 static void 1707 sfxge_tx_qfini(struct sfxge_softc *sc, unsigned int index) 1708 { 1709 struct sfxge_txq *txq; 1710 unsigned int nmaps; 1711 1712 txq = sc->txq[index]; 1713 1714 KASSERT(txq->init_state == SFXGE_TXQ_INITIALIZED, 1715 ("txq->init_state != SFXGE_TXQ_INITIALIZED")); 1716 1717 if (txq->type == SFXGE_TXQ_IP_TCP_UDP_CKSUM) 1718 tso_fini(txq); 1719 1720 /* Free the context arrays. */ 1721 free(txq->pend_desc, M_SFXGE); 1722 nmaps = sc->txq_entries; 1723 while (nmaps-- != 0) 1724 bus_dmamap_destroy(txq->packet_dma_tag, txq->stmp[nmaps].map); 1725 free(txq->stmp, M_SFXGE); 1726 1727 /* Release DMA memory mapping. */ 1728 sfxge_dma_free(&txq->mem); 1729 1730 sc->txq[index] = NULL; 1731 1732 SFXGE_TXQ_LOCK_DESTROY(txq); 1733 1734 free(txq, M_SFXGE); 1735 } 1736 1737 static int 1738 sfxge_tx_qinit(struct sfxge_softc *sc, unsigned int txq_index, 1739 enum sfxge_txq_type type, unsigned int evq_index) 1740 { 1741 const efx_nic_cfg_t *encp = efx_nic_cfg_get(sc->enp); 1742 char name[16]; 1743 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev); 1744 struct sysctl_oid *txq_node; 1745 struct sfxge_txq *txq; 1746 struct sfxge_evq *evq; 1747 struct sfxge_tx_dpl *stdp; 1748 struct sysctl_oid *dpl_node; 1749 efsys_mem_t *esmp; 1750 unsigned int nmaps; 1751 int rc; 1752 1753 txq = malloc(sizeof(struct sfxge_txq), M_SFXGE, M_ZERO | M_WAITOK); 1754 txq->sc = sc; 1755 txq->entries = sc->txq_entries; 1756 txq->ptr_mask = txq->entries - 1; 1757 1758 sc->txq[txq_index] = txq; 1759 esmp = &txq->mem; 1760 1761 evq = sc->evq[evq_index]; 1762 1763 /* Allocate and zero DMA space for the descriptor ring. */ 1764 if ((rc = sfxge_dma_alloc(sc, EFX_TXQ_SIZE(sc->txq_entries), esmp)) != 0) 1765 return (rc); 1766 1767 /* Allocate buffer table entries. */ 1768 sfxge_sram_buf_tbl_alloc(sc, EFX_TXQ_NBUFS(sc->txq_entries), 1769 &txq->buf_base_id); 1770 1771 /* Create a DMA tag for packet mappings. */ 1772 if (bus_dma_tag_create(sc->parent_dma_tag, 1, 1773 encp->enc_tx_dma_desc_boundary, 1774 MIN(0x3FFFFFFFFFFFUL, BUS_SPACE_MAXADDR), BUS_SPACE_MAXADDR, NULL, 1775 NULL, 0x11000, SFXGE_TX_MAPPING_MAX_SEG, 1776 encp->enc_tx_dma_desc_size_max, 0, NULL, NULL, 1777 &txq->packet_dma_tag) != 0) { 1778 device_printf(sc->dev, "Couldn't allocate txq DMA tag\n"); 1779 rc = ENOMEM; 1780 goto fail; 1781 } 1782 1783 /* Allocate pending descriptor array for batching writes. */ 1784 txq->pend_desc = malloc(sizeof(efx_desc_t) * sc->txq_entries, 1785 M_SFXGE, M_ZERO | M_WAITOK); 1786 1787 /* Allocate and initialise mbuf DMA mapping array. */ 1788 txq->stmp = malloc(sizeof(struct sfxge_tx_mapping) * sc->txq_entries, 1789 M_SFXGE, M_ZERO | M_WAITOK); 1790 for (nmaps = 0; nmaps < sc->txq_entries; nmaps++) { 1791 rc = bus_dmamap_create(txq->packet_dma_tag, 0, 1792 &txq->stmp[nmaps].map); 1793 if (rc != 0) 1794 goto fail2; 1795 } 1796 1797 snprintf(name, sizeof(name), "%u", txq_index); 1798 txq_node = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(sc->txqs_node), 1799 OID_AUTO, name, CTLFLAG_RD, NULL, ""); 1800 if (txq_node == NULL) { 1801 rc = ENOMEM; 1802 goto fail_txq_node; 1803 } 1804 1805 if (type == SFXGE_TXQ_IP_TCP_UDP_CKSUM && 1806 (rc = tso_init(txq)) != 0) 1807 goto fail3; 1808 1809 /* Initialize the deferred packet list. */ 1810 stdp = &txq->dpl; 1811 stdp->std_put_max = sfxge_tx_dpl_put_max; 1812 stdp->std_get_max = sfxge_tx_dpl_get_max; 1813 stdp->std_get_non_tcp_max = sfxge_tx_dpl_get_non_tcp_max; 1814 stdp->std_getp = &stdp->std_get; 1815 1816 SFXGE_TXQ_LOCK_INIT(txq, device_get_nameunit(sc->dev), txq_index); 1817 1818 dpl_node = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(txq_node), OID_AUTO, 1819 "dpl", CTLFLAG_RD, NULL, 1820 "Deferred packet list statistics"); 1821 if (dpl_node == NULL) { 1822 rc = ENOMEM; 1823 goto fail_dpl_node; 1824 } 1825 1826 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(dpl_node), OID_AUTO, 1827 "get_count", CTLFLAG_RD | CTLFLAG_STATS, 1828 &stdp->std_get_count, 0, ""); 1829 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(dpl_node), OID_AUTO, 1830 "get_non_tcp_count", CTLFLAG_RD | CTLFLAG_STATS, 1831 &stdp->std_get_non_tcp_count, 0, ""); 1832 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(dpl_node), OID_AUTO, 1833 "get_hiwat", CTLFLAG_RD | CTLFLAG_STATS, 1834 &stdp->std_get_hiwat, 0, ""); 1835 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(dpl_node), OID_AUTO, 1836 "put_hiwat", CTLFLAG_RD | CTLFLAG_STATS, 1837 &stdp->std_put_hiwat, 0, ""); 1838 1839 rc = sfxge_txq_stat_init(txq, txq_node); 1840 if (rc != 0) 1841 goto fail_txq_stat_init; 1842 1843 txq->type = type; 1844 txq->evq_index = evq_index; 1845 txq->init_state = SFXGE_TXQ_INITIALIZED; 1846 1847 return (0); 1848 1849 fail_txq_stat_init: 1850 fail_dpl_node: 1851 fail3: 1852 fail_txq_node: 1853 free(txq->pend_desc, M_SFXGE); 1854 fail2: 1855 while (nmaps-- != 0) 1856 bus_dmamap_destroy(txq->packet_dma_tag, txq->stmp[nmaps].map); 1857 free(txq->stmp, M_SFXGE); 1858 bus_dma_tag_destroy(txq->packet_dma_tag); 1859 1860 fail: 1861 sfxge_dma_free(esmp); 1862 1863 return (rc); 1864 } 1865 1866 static int 1867 sfxge_tx_stat_handler(SYSCTL_HANDLER_ARGS) 1868 { 1869 struct sfxge_softc *sc = arg1; 1870 unsigned int id = arg2; 1871 unsigned long sum; 1872 unsigned int index; 1873 1874 /* Sum across all TX queues */ 1875 sum = 0; 1876 for (index = 0; index < sc->txq_count; index++) 1877 sum += *(unsigned long *)((caddr_t)sc->txq[index] + 1878 sfxge_tx_stats[id].offset); 1879 1880 return (SYSCTL_OUT(req, &sum, sizeof(sum))); 1881 } 1882 1883 static void 1884 sfxge_tx_stat_init(struct sfxge_softc *sc) 1885 { 1886 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev); 1887 struct sysctl_oid_list *stat_list; 1888 unsigned int id; 1889 1890 stat_list = SYSCTL_CHILDREN(sc->stats_node); 1891 1892 for (id = 0; id < nitems(sfxge_tx_stats); id++) { 1893 SYSCTL_ADD_PROC( 1894 ctx, stat_list, 1895 OID_AUTO, sfxge_tx_stats[id].name, 1896 CTLTYPE_ULONG|CTLFLAG_RD, 1897 sc, id, sfxge_tx_stat_handler, "LU", 1898 ""); 1899 } 1900 } 1901 1902 uint64_t 1903 sfxge_tx_get_drops(struct sfxge_softc *sc) 1904 { 1905 unsigned int index; 1906 uint64_t drops = 0; 1907 struct sfxge_txq *txq; 1908 1909 /* Sum across all TX queues */ 1910 for (index = 0; index < sc->txq_count; index++) { 1911 txq = sc->txq[index]; 1912 /* 1913 * In theory, txq->put_overflow and txq->netdown_drops 1914 * should use atomic operation and other should be 1915 * obtained under txq lock, but it is just statistics. 1916 */ 1917 drops += txq->drops + txq->get_overflow + 1918 txq->get_non_tcp_overflow + 1919 txq->put_overflow + txq->netdown_drops + 1920 txq->tso_pdrop_too_many + txq->tso_pdrop_no_rsrc; 1921 } 1922 return (drops); 1923 } 1924 1925 void 1926 sfxge_tx_fini(struct sfxge_softc *sc) 1927 { 1928 int index; 1929 1930 index = sc->txq_count; 1931 while (--index >= 0) 1932 sfxge_tx_qfini(sc, index); 1933 1934 sc->txq_count = 0; 1935 } 1936 1937 1938 int 1939 sfxge_tx_init(struct sfxge_softc *sc) 1940 { 1941 const efx_nic_cfg_t *encp = efx_nic_cfg_get(sc->enp); 1942 struct sfxge_intr *intr; 1943 int index; 1944 int rc; 1945 1946 intr = &sc->intr; 1947 1948 KASSERT(intr->state == SFXGE_INTR_INITIALIZED, 1949 ("intr->state != SFXGE_INTR_INITIALIZED")); 1950 1951 if (sfxge_tx_dpl_get_max <= 0) { 1952 log(LOG_ERR, "%s=%d must be greater than 0", 1953 SFXGE_PARAM_TX_DPL_GET_MAX, sfxge_tx_dpl_get_max); 1954 rc = EINVAL; 1955 goto fail_tx_dpl_get_max; 1956 } 1957 if (sfxge_tx_dpl_get_non_tcp_max <= 0) { 1958 log(LOG_ERR, "%s=%d must be greater than 0", 1959 SFXGE_PARAM_TX_DPL_GET_NON_TCP_MAX, 1960 sfxge_tx_dpl_get_non_tcp_max); 1961 rc = EINVAL; 1962 goto fail_tx_dpl_get_non_tcp_max; 1963 } 1964 if (sfxge_tx_dpl_put_max < 0) { 1965 log(LOG_ERR, "%s=%d must be greater or equal to 0", 1966 SFXGE_PARAM_TX_DPL_PUT_MAX, sfxge_tx_dpl_put_max); 1967 rc = EINVAL; 1968 goto fail_tx_dpl_put_max; 1969 } 1970 1971 sc->txq_count = SFXGE_TXQ_NTYPES - 1 + sc->intr.n_alloc; 1972 1973 sc->tso_fw_assisted = sfxge_tso_fw_assisted; 1974 if ((~encp->enc_features & EFX_FEATURE_FW_ASSISTED_TSO) || 1975 (!encp->enc_fw_assisted_tso_enabled)) 1976 sc->tso_fw_assisted &= ~SFXGE_FATSOV1; 1977 if ((~encp->enc_features & EFX_FEATURE_FW_ASSISTED_TSO_V2) || 1978 (!encp->enc_fw_assisted_tso_v2_enabled)) 1979 sc->tso_fw_assisted &= ~SFXGE_FATSOV2; 1980 1981 sc->txqs_node = SYSCTL_ADD_NODE( 1982 device_get_sysctl_ctx(sc->dev), 1983 SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)), 1984 OID_AUTO, "txq", CTLFLAG_RD, NULL, "Tx queues"); 1985 if (sc->txqs_node == NULL) { 1986 rc = ENOMEM; 1987 goto fail_txq_node; 1988 } 1989 1990 /* Initialize the transmit queues */ 1991 if ((rc = sfxge_tx_qinit(sc, SFXGE_TXQ_NON_CKSUM, 1992 SFXGE_TXQ_NON_CKSUM, 0)) != 0) 1993 goto fail; 1994 1995 if ((rc = sfxge_tx_qinit(sc, SFXGE_TXQ_IP_CKSUM, 1996 SFXGE_TXQ_IP_CKSUM, 0)) != 0) 1997 goto fail2; 1998 1999 for (index = 0; 2000 index < sc->txq_count - SFXGE_TXQ_NTYPES + 1; 2001 index++) { 2002 if ((rc = sfxge_tx_qinit(sc, SFXGE_TXQ_NTYPES - 1 + index, 2003 SFXGE_TXQ_IP_TCP_UDP_CKSUM, index)) != 0) 2004 goto fail3; 2005 } 2006 2007 sfxge_tx_stat_init(sc); 2008 2009 return (0); 2010 2011 fail3: 2012 while (--index >= 0) 2013 sfxge_tx_qfini(sc, SFXGE_TXQ_IP_TCP_UDP_CKSUM + index); 2014 2015 sfxge_tx_qfini(sc, SFXGE_TXQ_IP_CKSUM); 2016 2017 fail2: 2018 sfxge_tx_qfini(sc, SFXGE_TXQ_NON_CKSUM); 2019 2020 fail: 2021 fail_txq_node: 2022 sc->txq_count = 0; 2023 fail_tx_dpl_put_max: 2024 fail_tx_dpl_get_non_tcp_max: 2025 fail_tx_dpl_get_max: 2026 return (rc); 2027 } 2028