2 * Copyright (c) 2010-2015 Solarflare Communications Inc.
5 * This software was developed in part by Philip Paeps under contract for
6 * Solarflare Communications, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
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.
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.
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.
34 /* Theory of operation:
36 * Tx queues allocation and mapping
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.
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
49 #include <sys/cdefs.h>
50 __FBSDID("$FreeBSD$");
52 #include <sys/param.h>
53 #include <sys/malloc.h>
56 #include <sys/socket.h>
57 #include <sys/sysctl.h>
58 #include <sys/syslog.h>
59 #include <sys/limits.h>
62 #include <net/ethernet.h>
64 #include <net/if_vlan_var.h>
66 #include <netinet/in.h>
67 #include <netinet/ip.h>
68 #include <netinet/ip6.h>
69 #include <netinet/tcp.h>
71 #include "common/efx.h"
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");
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");
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");
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");
108 static const struct {
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),
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,
137 sfxge_tx_maybe_insert_tag(struct sfxge_txq *txq, struct mbuf *mbuf)
139 uint16_t this_tag = ((mbuf->m_flags & M_VLANTAG) ?
140 mbuf->m_pkthdr.ether_vtag :
143 if (this_tag == txq->hw_vlan_tci)
146 efx_tx_qdesc_vlantci_create(txq->common,
149 txq->n_pend_desc = 1;
150 txq->hw_vlan_tci = this_tag;
155 sfxge_next_stmp(struct sfxge_txq *txq, struct sfxge_tx_mapping **pstmp)
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];
167 sfxge_tx_qcomplete(struct sfxge_txq *txq, struct sfxge_evq *evq)
169 unsigned int completed;
171 SFXGE_EVQ_LOCK_ASSERT_OWNED(evq);
173 completed = txq->completed;
174 while (completed != txq->pending) {
175 struct sfxge_tx_mapping *stmp;
178 id = completed++ & txq->ptr_mask;
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;
189 free(stmp->u.heap_buf, M_SFXGE);
194 txq->completed = completed;
196 /* Check whether we need to unblock the queue. */
201 level = txq->added - txq->completed;
202 if (level <= SFXGE_TXQ_UNBLOCK_LEVEL(txq->entries))
203 sfxge_tx_qunblock(txq);
208 sfxge_is_mbuf_non_tcp(struct mbuf *mbuf)
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.
213 return (!(mbuf->m_pkthdr.csum_flags & (CSUM_IP_TCP | CSUM_IP6_TCP)));
217 * Reorder the put list and append it to the get list.
220 sfxge_tx_qdpl_swizzle(struct sfxge_txq *txq)
222 struct sfxge_tx_dpl *stdp;
223 struct mbuf *mbuf, *get_next, **get_tailp;
224 volatile uintptr_t *putp;
227 unsigned int non_tcp_count;
229 SFXGE_TXQ_LOCK_ASSERT_OWNED(txq);
233 /* Acquire the put list. */
234 putp = &stdp->std_put;
235 put = atomic_readandclear_ptr(putp);
241 /* Reverse the put list. */
242 get_tailp = &mbuf->m_nextpkt;
248 struct mbuf *put_next;
250 non_tcp_count += sfxge_is_mbuf_non_tcp(mbuf);
251 put_next = mbuf->m_nextpkt;
252 mbuf->m_nextpkt = get_next;
257 } while (mbuf != NULL);
259 if (count > stdp->std_put_hiwat)
260 stdp->std_put_hiwat = count;
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;
271 sfxge_tx_qreap(struct sfxge_txq *txq)
273 SFXGE_TXQ_LOCK_ASSERT_OWNED(txq);
275 txq->reaped = txq->completed;
279 sfxge_tx_qlist_post(struct sfxge_txq *txq)
281 unsigned int old_added;
282 unsigned int block_level;
286 SFXGE_TXQ_LOCK_ASSERT_OWNED(txq);
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"));
293 old_added = txq->added;
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"));
300 /* If efx_tx_qdesc_post() had to refragment, our information about
301 * buffers to free may be associated with the wrong
304 KASSERT(txq->added - old_added == txq->n_pend_desc,
305 ("efx_tx_qdesc_post() refragmented descriptors"));
307 level = txq->added - txq->reaped;
308 KASSERT(level <= txq->entries, ("overfilled TX queue"));
310 /* Clear the fragment list. */
311 txq->n_pend_desc = 0;
314 * Set the block level to ensure there is space to generate a
315 * large number of descriptors for TSO.
317 block_level = EFX_TXQ_LIMIT(txq->entries) - txq->max_pkt_desc;
319 /* Have we reached the block level? */
320 if (level < block_level)
323 /* Reap, and check again */
325 level = txq->added - txq->reaped;
326 if (level < block_level)
332 * Avoid a race with completion interrupt handling that could leave
337 level = txq->added - txq->reaped;
338 if (level < block_level) {
344 static int sfxge_tx_queue_mbuf(struct sfxge_txq *txq, struct mbuf *mbuf)
346 bus_dmamap_t *used_map;
348 bus_dma_segment_t dma_seg[SFXGE_TX_MAPPING_MAX_SEG];
350 struct sfxge_tx_mapping *stmp;
358 KASSERT(!txq->blocked, ("txq->blocked"));
360 if (mbuf->m_pkthdr.csum_flags & CSUM_TSO)
361 prefetch_read_many(mbuf->m_data);
363 if (__predict_false(txq->init_state != SFXGE_TXQ_STARTED)) {
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);
375 struct mbuf *new_mbuf = m_collapse(mbuf, M_NOWAIT,
376 SFXGE_TX_MAPPING_MAX_SEG);
377 if (new_mbuf == NULL)
381 rc = bus_dmamap_load_mbuf_sg(txq->packet_dma_tag,
383 dma_seg, &n_dma_seg, 0);
388 /* Make the packet visible to the hardware. */
389 bus_dmamap_sync(txq->packet_dma_tag, stmp->map, BUS_DMASYNC_PREWRITE);
391 used_map = &stmp->map;
393 vlan_tagged = sfxge_tx_maybe_insert_tag(txq, mbuf);
395 sfxge_next_stmp(txq, &stmp);
397 if (mbuf->m_pkthdr.csum_flags & CSUM_TSO) {
398 rc = sfxge_tx_queue_tso(txq, mbuf, dma_seg, n_dma_seg, vlan_tagged);
401 stmp = &txq->stmp[(rc - 1) & txq->ptr_mask];
403 /* Add the mapping to the fragment list, and set flags
409 desc = &txq->pend_desc[i + vlan_tagged];
410 eop = (i == n_dma_seg - 1);
411 efx_tx_qdesc_dma_create(txq->common,
419 sfxge_next_stmp(txq, &stmp);
421 txq->n_pend_desc = n_dma_seg + vlan_tagged;
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.
429 if (used_map != &stmp->map) {
431 stmp->map = *used_map;
436 stmp->flags = TX_BUF_UNMAP | TX_BUF_MBUF;
438 /* Post the fragment list. */
439 sfxge_tx_qlist_post(txq);
444 bus_dmamap_unload(txq->packet_dma_tag, *used_map);
446 /* Drop the packet on the floor. */
454 * Drain the deferred packet list into the transmit queue.
457 sfxge_tx_qdpl_drain(struct sfxge_txq *txq)
459 struct sfxge_softc *sc;
460 struct sfxge_tx_dpl *stdp;
461 struct mbuf *mbuf, *next;
463 unsigned int non_tcp_count;
467 SFXGE_TXQ_LOCK_ASSERT_OWNED(txq);
473 if (__predict_true(txq->init_state == SFXGE_TXQ_STARTED)) {
474 prefetch_read_many(sc->enp);
475 prefetch_read_many(txq->common);
478 mbuf = stdp->std_get;
479 count = stdp->std_get_count;
480 non_tcp_count = stdp->std_get_non_tcp_count;
482 if (count > stdp->std_get_hiwat)
483 stdp->std_get_hiwat = count;
486 KASSERT(mbuf != NULL, ("mbuf == NULL"));
488 next = mbuf->m_nextpkt;
489 mbuf->m_nextpkt = NULL;
491 ETHER_BPF_MTAP(sc->ifnet, mbuf); /* packet capture */
494 prefetch_read_many(next);
496 rc = sfxge_tx_queue_mbuf(txq, mbuf);
498 non_tcp_count -= sfxge_is_mbuf_non_tcp(mbuf);
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);
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;
522 stdp->std_get = mbuf;
523 stdp->std_get_count = count;
524 stdp->std_get_non_tcp_count = non_tcp_count;
527 if (txq->added != pushed)
528 efx_tx_qpush(txq->common, txq->added, pushed);
530 KASSERT(txq->blocked || stdp->std_get_count == 0,
531 ("queue unblocked but count is non-zero"));
534 #define SFXGE_TX_QDPL_PENDING(_txq) ((_txq)->dpl.std_put != 0)
537 * Service the deferred packet list.
539 * NOTE: drops the txq mutex!
542 sfxge_tx_qdpl_service(struct sfxge_txq *txq)
544 SFXGE_TXQ_LOCK_ASSERT_OWNED(txq);
547 if (SFXGE_TX_QDPL_PENDING(txq))
548 sfxge_tx_qdpl_swizzle(txq);
551 sfxge_tx_qdpl_drain(txq);
553 SFXGE_TXQ_UNLOCK(txq);
554 } while (SFXGE_TX_QDPL_PENDING(txq) &&
555 SFXGE_TXQ_TRYLOCK(txq));
559 * Put a packet on the deferred packet get-list.
562 sfxge_tx_qdpl_put_locked(struct sfxge_txq *txq, struct mbuf *mbuf)
564 struct sfxge_tx_dpl *stdp;
568 KASSERT(mbuf->m_nextpkt == NULL, ("mbuf->m_nextpkt != NULL"));
570 SFXGE_TXQ_LOCK_ASSERT_OWNED(txq);
572 if (stdp->std_get_count >= stdp->std_get_max) {
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++;
582 stdp->std_get_non_tcp_count++;
585 *(stdp->std_getp) = mbuf;
586 stdp->std_getp = &mbuf->m_nextpkt;
587 stdp->std_get_count++;
593 * Put a packet on the deferred packet put-list.
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.
599 sfxge_tx_qdpl_put_unlocked(struct sfxge_txq *txq, struct mbuf *mbuf)
601 struct sfxge_tx_dpl *stdp;
602 volatile uintptr_t *putp;
607 KASSERT(mbuf->m_nextpkt == NULL, ("mbuf->m_nextpkt != NULL"));
609 SFXGE_TXQ_LOCK_ASSERT_NOTOWNED(txq);
612 putp = &stdp->std_put;
613 new = (uintptr_t)mbuf;
618 struct mbuf *mp = (struct mbuf *)old;
619 old_len = mp->m_pkthdr.csum_data;
622 if (old_len >= stdp->std_put_max) {
623 atomic_add_long(&txq->put_overflow, 1);
626 mbuf->m_pkthdr.csum_data = old_len + 1;
627 mbuf->m_nextpkt = (void *)old;
628 } while (atomic_cmpset_ptr(putp, old, new) == 0);
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.
638 sfxge_tx_packet_add(struct sfxge_txq *txq, struct mbuf *m)
642 if (!SFXGE_LINK_UP(txq->sc)) {
643 atomic_add_long(&txq->netdown_drops, 1);
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".
652 if (SFXGE_TXQ_TRYLOCK(txq)) {
653 /* First swizzle put-list to get-list to keep order */
654 sfxge_tx_qdpl_swizzle(txq);
656 rc = sfxge_tx_qdpl_put_locked(txq, m);
658 /* Try to service the list. */
659 sfxge_tx_qdpl_service(txq);
660 /* Lock has been dropped. */
662 rc = sfxge_tx_qdpl_put_unlocked(txq, m);
665 * Try to grab the lock again.
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.
671 if ((rc == 0) && SFXGE_TXQ_TRYLOCK(txq)) {
672 sfxge_tx_qdpl_service(txq);
673 /* Lock has been dropped. */
677 SFXGE_TXQ_LOCK_ASSERT_NOTOWNED(txq);
683 sfxge_tx_qdpl_flush(struct sfxge_txq *txq)
685 struct sfxge_tx_dpl *stdp = &txq->dpl;
686 struct mbuf *mbuf, *next;
690 sfxge_tx_qdpl_swizzle(txq);
691 for (mbuf = stdp->std_get; mbuf != NULL; mbuf = next) {
692 next = mbuf->m_nextpkt;
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;
700 SFXGE_TXQ_UNLOCK(txq);
704 sfxge_if_qflush(struct ifnet *ifp)
706 struct sfxge_softc *sc;
711 for (i = 0; i < sc->txq_count; i++)
712 sfxge_tx_qdpl_flush(sc->txq[i]);
715 #if SFXGE_TX_PARSE_EARLY
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.
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])
730 static void sfxge_parse_tx_packet(struct mbuf *mbuf)
732 struct ether_header *eh = mtod(mbuf, struct ether_header *);
733 const struct tcphdr *th;
734 struct tcphdr th_copy;
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);
744 mbuf->m_pkthdr.l2hlen = sizeof(*eh);
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);
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;
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 ==
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;
765 KASSERT(mbuf->m_len >= mbuf->m_pkthdr.l3hlen,
766 ("network header is fragmented in mbuf"));
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),
774 th = (const struct tcphdr *)mtodo(mbuf, mbuf->m_pkthdr.l3hlen);
777 mbuf->m_pkthdr.l4hlen = mbuf->m_pkthdr.l3hlen + 4 * th->th_off;
778 TSO_MBUF_SEQNUM(mbuf) = ntohl(th->th_seq);
780 /* These flags must not be duplicated */
782 * RST should not be duplicated as well, but FreeBSD kernel
783 * generates TSO packets with RST flag. So, do not assert
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;
794 * TX start -- called by the stack.
797 sfxge_if_transmit(struct ifnet *ifp, struct mbuf *m)
799 struct sfxge_softc *sc;
800 struct sfxge_txq *txq;
803 sc = (struct sfxge_softc *)ifp->if_softc;
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.
813 KASSERT((ifp->if_flags & IFF_UP) || (sc->if_flags & IFF_UP),
814 ("interface not up"));
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)) {
821 /* check if flowid is set */
822 if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) {
823 uint32_t hash = m->m_pkthdr.flowid;
825 index = sc->rx_indir_table[hash % SFXGE_RX_SCALE_MAX];
827 #if SFXGE_TX_PARSE_EARLY
828 if (m->m_pkthdr.csum_flags & CSUM_TSO)
829 sfxge_parse_tx_packet(m);
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];
835 txq = sc->txq[SFXGE_TXQ_NON_CKSUM];
838 rc = sfxge_tx_packet_add(txq, m);
846 * Software "TSO". Not quite as good as doing it in hardware, but
847 * still faster than segmenting in the stack.
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) */
859 uint64_t dma_addr; /* DMA address of current position */
860 unsigned in_len; /* Remaining length in current mbuf */
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
875 #if !SFXGE_TX_PARSE_EARLY
876 static const struct ip *tso_iph(const struct sfxge_tso_state *tso)
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);
883 static __unused const struct ip6_hdr *tso_ip6h(const struct sfxge_tso_state *tso)
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);
890 static const struct tcphdr *tso_tcph(const struct sfxge_tso_state *tso)
892 return (const struct tcphdr *)(tso->mbuf->m_data + tso->tcph_off);
897 /* Size of preallocated TSO header buffers. Larger blocks must be
898 * allocated from the heap.
900 #define TSOH_STD_SIZE 128
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.
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)
911 static int tso_init(struct sfxge_txq *txq)
913 struct sfxge_softc *sc = txq->sc;
914 unsigned int tsoh_page_count = TSOH_PAGE_COUNT(sc->txq_entries);
917 /* Allocate TSO header buffers */
918 txq->tsoh_buffer = malloc(tsoh_page_count * sizeof(txq->tsoh_buffer[0]),
921 for (i = 0; i < tsoh_page_count; i++) {
922 rc = sfxge_dma_alloc(sc, PAGE_SIZE, &txq->tsoh_buffer[i]);
931 sfxge_dma_free(&txq->tsoh_buffer[i]);
932 free(txq->tsoh_buffer, M_SFXGE);
933 txq->tsoh_buffer = NULL;
937 static void tso_fini(struct sfxge_txq *txq)
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);
948 static void tso_start(struct sfxge_txq *txq, struct sfxge_tso_state *tso,
949 const bus_dma_segment_t *hdr_dma_seg,
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;
959 tso->fw_assisted = txq->tso_fw_assisted;
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);
971 tso->nh_off = sizeof(*eh);
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);
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;
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);
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;
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),
1016 tso->header_len = tso->tcph_off + 4 * th->th_off;
1018 tso->header_len = mbuf->m_pkthdr.l4hlen;
1020 tso->seg_size = mbuf->m_pkthdr.tso_segsz;
1022 #if !SFXGE_TX_PARSE_EARLY
1023 tso->seqnum = ntohl(th->th_seq);
1025 /* These flags must not be duplicated */
1027 * RST should not be duplicated as well, but FreeBSD kernel
1028 * generates TSO packets with RST flag. So, do not assert
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;
1036 tso->seqnum = TSO_MBUF_SEQNUM(mbuf);
1037 tso->tcp_flags = TSO_MBUF_FLAGS(mbuf);
1040 tso->out_len = mbuf->m_pkthdr.len - tso->header_len;
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,
1050 tso->fw_assisted = 0;
1055 * tso_fill_packet_with_fragment - form descriptors for the current fragment
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
1061 static void tso_fill_packet_with_fragment(struct sfxge_txq *txq,
1062 struct sfxge_tso_state *tso)
1066 uint64_t dma_addr = tso->dma_addr;
1069 if (tso->in_len == 0 || tso->packet_space == 0)
1072 KASSERT(tso->in_len > 0, ("TSO input length went negative"));
1073 KASSERT(tso->packet_space > 0, ("TSO packet space went negative"));
1075 if (tso->fw_assisted & SFXGE_FATSOV2) {
1080 if (n < tso->packet_space) {
1081 tso->packet_space -= n;
1084 tso->packet_space = tso->seg_size -
1085 (n - tso->packet_space) % tso->seg_size;
1087 EFX_TX_FATSOV2_DMA_SEGS_PER_PKT_MAX - 1 -
1088 (tso->packet_space != tso->seg_size);
1091 n = min(tso->in_len, tso->packet_space);
1092 tso->packet_space -= n;
1099 * It is OK to use binary OR below to avoid extra branching
1100 * since all conditions may always be checked.
1102 eop = (tso->out_len == 0) | (tso->packet_space == 0) |
1103 (tso->segs_space == 0);
1105 desc = &txq->pend_desc[txq->n_pend_desc++];
1106 efx_tx_qdesc_dma_create(txq->common, dma_addr, n, eop, desc);
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,
1114 *(uint64_t *)dma_addr_ret = ((__predict_true(error == 0) &&
1115 __predict_true(nseg == 1)) ?
1120 * tso_start_new_packet - generate a new header and prepare for the new packet
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.
1125 static int tso_start_new_packet(struct sfxge_txq *txq,
1126 struct sfxge_tso_state *tso,
1129 unsigned int id = *idp;
1130 struct tcphdr *tsoh_th;
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,
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;
1154 EFX_TX_FATSOV2_DMA_SEGS_PER_PKT_MAX - 1;
1156 uint8_t tcp_flags = tso->tcp_flags;
1158 if (tso->out_len > tso->seg_size)
1159 tcp_flags &= ~(TH_FIN | TH_PUSH);
1161 /* Add FATSOv1 option descriptor */
1162 desc = &txq->pend_desc[txq->n_pend_desc++];
1163 efx_tx_qdesc_tso_create(txq->common,
1168 KASSERT(txq->stmp[id].flags == 0, ("stmp flags are not 0"));
1169 id = (id + 1) & txq->ptr_mask;
1171 tso->seqnum += tso->seg_size;
1172 tso->segs_space = UINT_MAX;
1175 /* Header DMA descriptor */
1176 *desc = tso->header_desc;
1178 KASSERT(txq->stmp[id].flags == 0, ("stmp flags are not 0"));
1179 id = (id + 1) & txq->ptr_mask;
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;
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;
1192 KASSERT(txq->stmp[id].flags == 0,
1193 ("stmp flags are not 0"));
1195 struct sfxge_tx_mapping *stmp = &txq->stmp[id];
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))
1201 rc = bus_dmamap_load(txq->packet_dma_tag, stmp->map,
1202 header, tso->header_len,
1203 tso_map_long_header, &dma_addr,
1205 if (__predict_false(dma_addr == 0)) {
1207 /* Succeeded but got >1 segment */
1208 bus_dmamap_unload(txq->packet_dma_tag,
1212 free(header, M_SFXGE);
1217 txq->tso_long_headers++;
1218 stmp->u.heap_buf = header;
1219 stmp->flags = TX_BUF_UNMAP;
1222 tsoh_th = (struct tcphdr *)(header + tso->tcph_off);
1224 /* Copy and update the headers. */
1225 m_copydata(tso->mbuf, 0, tso->header_len, header);
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);
1234 /* This packet will be the last in the TSO burst. */
1235 ip_length = tso->header_len - tso->nh_off + tso->out_len;
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.
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));
1250 /* Make the header visible to the hardware. */
1251 bus_dmamap_sync(txq->packet_dma_tag, map, BUS_DMASYNC_PREWRITE);
1253 /* Form a descriptor for this header. */
1254 desc = &txq->pend_desc[txq->n_pend_desc++];
1255 efx_tx_qdesc_dma_create(txq->common,
1260 id = (id + 1) & txq->ptr_mask;
1262 tso->segs_space = UINT_MAX;
1264 tso->packet_space = tso->seg_size;
1272 sfxge_tx_queue_tso(struct sfxge_txq *txq, struct mbuf *mbuf,
1273 const bus_dma_segment_t *dma_seg, int n_dma_seg,
1276 struct sfxge_tso_state tso;
1278 unsigned skipped = 0;
1280 tso_start(txq, &tso, dma_seg, mbuf);
1282 while (dma_seg->ds_len + skipped <= tso.header_len) {
1283 skipped += dma_seg->ds_len;
1285 KASSERT(n_dma_seg, ("no payload found in TSO packet"));
1288 tso.in_len = dma_seg->ds_len - (tso.header_len - skipped);
1289 tso.dma_addr = dma_seg->ds_addr + (tso.header_len - skipped);
1291 id = (txq->added + vlan_tagged) & txq->ptr_mask;
1292 if (__predict_false(tso_start_new_packet(txq, &tso, &id)))
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;
1301 /* Move onto the next fragment? */
1302 if (tso.in_len == 0) {
1307 tso.in_len = dma_seg->ds_len;
1308 tso.dma_addr = dma_seg->ds_addr;
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;
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.
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++;
1328 if (__predict_false(tso_start_new_packet(txq, &tso,
1330 txq->tso_pdrop_no_rsrc++;
1341 sfxge_tx_qunblock(struct sfxge_txq *txq)
1343 struct sfxge_softc *sc;
1344 struct sfxge_evq *evq;
1347 evq = sc->evq[txq->evq_index];
1349 SFXGE_EVQ_LOCK_ASSERT_OWNED(evq);
1351 if (__predict_false(txq->init_state != SFXGE_TXQ_STARTED))
1354 SFXGE_TXQ_LOCK(txq);
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);
1367 sfxge_tx_qdpl_service(txq);
1368 /* note: lock has been dropped */
1372 sfxge_tx_qflush_done(struct sfxge_txq *txq)
1375 txq->flush_state = SFXGE_FLUSH_DONE;
1379 sfxge_tx_qstop(struct sfxge_softc *sc, unsigned int index)
1381 struct sfxge_txq *txq;
1382 struct sfxge_evq *evq;
1385 SFXGE_ADAPTER_LOCK_ASSERT_OWNED(sc);
1387 txq = sc->txq[index];
1388 evq = sc->evq[txq->evq_index];
1390 SFXGE_EVQ_LOCK(evq);
1391 SFXGE_TXQ_LOCK(txq);
1393 KASSERT(txq->init_state == SFXGE_TXQ_STARTED,
1394 ("txq->init_state != SFXGE_TXQ_STARTED"));
1396 txq->init_state = SFXGE_TXQ_INITIALIZED;
1398 if (txq->flush_state != SFXGE_FLUSH_DONE) {
1399 txq->flush_state = SFXGE_FLUSH_PENDING;
1401 SFXGE_EVQ_UNLOCK(evq);
1402 SFXGE_TXQ_UNLOCK(txq);
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;
1412 /* Spin for 100ms. */
1414 if (txq->flush_state != SFXGE_FLUSH_PENDING)
1416 } while (++count < 20);
1418 SFXGE_EVQ_LOCK(evq);
1419 SFXGE_TXQ_LOCK(txq);
1421 KASSERT(txq->flush_state != SFXGE_FLUSH_FAILED,
1422 ("txq->flush_state == SFXGE_FLUSH_FAILED"));
1424 if (txq->flush_state != SFXGE_FLUSH_DONE) {
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;
1433 txq->pending = txq->added;
1435 sfxge_tx_qcomplete(txq, evq);
1436 KASSERT(txq->completed == txq->added,
1437 ("txq->completed != txq->added"));
1439 sfxge_tx_qreap(txq);
1440 KASSERT(txq->reaped == txq->completed,
1441 ("txq->reaped != txq->completed"));
1448 /* Destroy the common code transmit queue. */
1449 efx_tx_qdestroy(txq->common);
1452 efx_sram_buf_tbl_clear(sc->enp, txq->buf_base_id,
1453 EFX_TXQ_NBUFS(sc->txq_entries));
1455 SFXGE_EVQ_UNLOCK(evq);
1456 SFXGE_TXQ_UNLOCK(txq);
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.
1467 sfxge_tx_max_pkt_desc(const struct sfxge_softc *sc, enum sfxge_txq_type type,
1468 unsigned int tso_fw_assisted)
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;
1476 /* VLAN tagging Tx option descriptor may be required */
1477 if (efx_nic_cfg_get(sc->enp)->enc_hw_tx_insert_vlan_enabled)
1480 if (type == SFXGE_TXQ_IP_TCP_UDP_CKSUM) {
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.
1487 sw_tso_max_descs = SFXGE_TSO_MAX_SEGS * 2 - 1;
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;
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.
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;
1506 max_descs += MAX(sw_tso_max_descs,
1507 MAX(fa_tso_v1_max_descs, fa_tso_v2_max_descs));
1514 sfxge_tx_qstart(struct sfxge_softc *sc, unsigned int index)
1516 struct sfxge_txq *txq;
1519 unsigned int tso_fw_assisted;
1520 struct sfxge_evq *evq;
1521 unsigned int desc_index;
1524 SFXGE_ADAPTER_LOCK_ASSERT_OWNED(sc);
1526 txq = sc->txq[index];
1528 evq = sc->evq[txq->evq_index];
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"));
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)
1540 /* Determine the kind of queue we are creating. */
1541 tso_fw_assisted = 0;
1542 switch (txq->type) {
1543 case SFXGE_TXQ_NON_CKSUM:
1546 case SFXGE_TXQ_IP_CKSUM:
1547 flags = EFX_TXQ_CKSUM_IPV4;
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;
1556 KASSERT(0, ("Impossible TX queue"));
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))
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)
1578 /* Initialise queue descriptor indexes */
1579 txq->added = txq->pending = txq->completed = txq->reaped = desc_index;
1581 SFXGE_TXQ_LOCK(txq);
1583 /* Enable the transmit queue. */
1584 efx_tx_qenable(txq->common);
1586 txq->init_state = SFXGE_TXQ_STARTED;
1587 txq->flush_state = SFXGE_FLUSH_REQUIRED;
1588 txq->tso_fw_assisted = tso_fw_assisted;
1590 txq->max_pkt_desc = sfxge_tx_max_pkt_desc(sc, txq->type,
1593 SFXGE_TXQ_UNLOCK(txq);
1598 efx_sram_buf_tbl_clear(sc->enp, txq->buf_base_id,
1599 EFX_TXQ_NBUFS(sc->txq_entries));
1604 sfxge_tx_stop(struct sfxge_softc *sc)
1608 index = sc->txq_count;
1609 while (--index >= 0)
1610 sfxge_tx_qstop(sc, index);
1612 /* Tear down the transmit module */
1613 efx_tx_fini(sc->enp);
1617 sfxge_tx_start(struct sfxge_softc *sc)
1622 /* Initialize the common code transmit module. */
1623 if ((rc = efx_tx_init(sc->enp)) != 0)
1626 for (index = 0; index < sc->txq_count; index++) {
1627 if ((rc = sfxge_tx_qstart(sc, index)) != 0)
1634 while (--index >= 0)
1635 sfxge_tx_qstop(sc, index);
1637 efx_tx_fini(sc->enp);
1643 sfxge_txq_stat_init(struct sfxge_txq *txq, struct sysctl_oid *txq_node)
1645 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(txq->sc->dev);
1646 struct sysctl_oid *stat_node;
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)
1655 for (id = 0; id < nitems(sfxge_tx_stats); id++) {
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),
1667 * Destroy a transmit queue.
1670 sfxge_tx_qfini(struct sfxge_softc *sc, unsigned int index)
1672 struct sfxge_txq *txq;
1675 txq = sc->txq[index];
1677 KASSERT(txq->init_state == SFXGE_TXQ_INITIALIZED,
1678 ("txq->init_state != SFXGE_TXQ_INITIALIZED"));
1680 if (txq->type == SFXGE_TXQ_IP_TCP_UDP_CKSUM)
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);
1690 /* Release DMA memory mapping. */
1691 sfxge_dma_free(&txq->mem);
1693 sc->txq[index] = NULL;
1695 SFXGE_TXQ_LOCK_DESTROY(txq);
1701 sfxge_tx_qinit(struct sfxge_softc *sc, unsigned int txq_index,
1702 enum sfxge_txq_type type, unsigned int evq_index)
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;
1715 txq = malloc(sizeof(struct sfxge_txq), M_SFXGE, M_ZERO | M_WAITOK);
1717 txq->entries = sc->txq_entries;
1718 txq->ptr_mask = txq->entries - 1;
1720 sc->txq[txq_index] = txq;
1723 evq = sc->evq[evq_index];
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)
1729 /* Allocate buffer table entries. */
1730 sfxge_sram_buf_tbl_alloc(sc, EFX_TXQ_NBUFS(sc->txq_entries),
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");
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);
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);
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) {
1765 if (type == SFXGE_TXQ_IP_TCP_UDP_CKSUM &&
1766 (rc = tso_init(txq)) != 0)
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);
1773 goto fail_tx_dpl_get_max;
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);
1780 goto fail_tx_dpl_get_max;
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);
1786 goto fail_tx_dpl_put_max;
1789 /* Initialize the deferred packet list. */
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;
1796 SFXGE_TXQ_LOCK_INIT(txq, device_get_nameunit(sc->dev), txq_index);
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) {
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, "");
1819 rc = sfxge_txq_stat_init(txq, txq_node);
1821 goto fail_txq_stat_init;
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;
1833 fail_tx_dpl_put_max:
1834 fail_tx_dpl_get_max:
1837 free(txq->pend_desc, M_SFXGE);
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);
1845 sfxge_dma_free(esmp);
1851 sfxge_tx_stat_handler(SYSCTL_HANDLER_ARGS)
1853 struct sfxge_softc *sc = arg1;
1854 unsigned int id = arg2;
1858 /* Sum across all TX queues */
1860 for (index = 0; index < sc->txq_count; index++)
1861 sum += *(unsigned long *)((caddr_t)sc->txq[index] +
1862 sfxge_tx_stats[id].offset);
1864 return (SYSCTL_OUT(req, &sum, sizeof(sum)));
1868 sfxge_tx_stat_init(struct sfxge_softc *sc)
1870 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev);
1871 struct sysctl_oid_list *stat_list;
1874 stat_list = SYSCTL_CHILDREN(sc->stats_node);
1876 for (id = 0; id < nitems(sfxge_tx_stats); id++) {
1879 OID_AUTO, sfxge_tx_stats[id].name,
1880 CTLTYPE_ULONG|CTLFLAG_RD,
1881 sc, id, sfxge_tx_stat_handler, "LU",
1887 sfxge_tx_get_drops(struct sfxge_softc *sc)
1891 struct sfxge_txq *txq;
1893 /* Sum across all TX queues */
1894 for (index = 0; index < sc->txq_count; index++) {
1895 txq = sc->txq[index];
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.
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;
1910 sfxge_tx_fini(struct sfxge_softc *sc)
1914 index = sc->txq_count;
1915 while (--index >= 0)
1916 sfxge_tx_qfini(sc, index);
1923 sfxge_tx_init(struct sfxge_softc *sc)
1925 const efx_nic_cfg_t *encp = efx_nic_cfg_get(sc->enp);
1926 struct sfxge_intr *intr;
1932 KASSERT(intr->state == SFXGE_INTR_INITIALIZED,
1933 ("intr->state != SFXGE_INTR_INITIALIZED"));
1935 sc->txq_count = SFXGE_TXQ_NTYPES - 1 + sc->intr.n_alloc;
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;
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) {
1954 /* Initialize the transmit queues */
1955 if ((rc = sfxge_tx_qinit(sc, SFXGE_TXQ_NON_CKSUM,
1956 SFXGE_TXQ_NON_CKSUM, 0)) != 0)
1959 if ((rc = sfxge_tx_qinit(sc, SFXGE_TXQ_IP_CKSUM,
1960 SFXGE_TXQ_IP_CKSUM, 0)) != 0)
1964 index < sc->txq_count - SFXGE_TXQ_NTYPES + 1;
1966 if ((rc = sfxge_tx_qinit(sc, SFXGE_TXQ_NTYPES - 1 + index,
1967 SFXGE_TXQ_IP_TCP_UDP_CKSUM, index)) != 0)
1971 sfxge_tx_stat_init(sc);
1976 while (--index >= 0)
1977 sfxge_tx_qfini(sc, SFXGE_TXQ_IP_TCP_UDP_CKSUM + index);
1979 sfxge_tx_qfini(sc, SFXGE_TXQ_IP_CKSUM);
1982 sfxge_tx_qfini(sc, SFXGE_TXQ_NON_CKSUM);