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