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