2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2010-2016 Solarflare Communications Inc.
7 * This software was developed in part by Philip Paeps under contract for
8 * Solarflare Communications, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
13 * 1. Redistributions of source code must retain the above copyright notice,
14 * this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
29 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 * The views and conclusions contained in the software and documentation are
32 * those of the authors and should not be interpreted as representing official
33 * policies, either expressed or implied, of the FreeBSD Project.
36 /* Theory of operation:
38 * Tx queues allocation and mapping
40 * One Tx queue with enabled checksum offload is allocated per Rx channel
41 * (event queue). Also 2 Tx queues (one without checksum offload and one
42 * with IP checksum offload only) are allocated and bound to event queue 0.
43 * sfxge_txq_type is used as Tx queue label.
45 * So, event queue plus label mapping to Tx queue index is:
46 * if event queue index is 0, TxQ-index = TxQ-label * [0..SFXGE_TXQ_NTYPES)
47 * else TxQ-index = SFXGE_TXQ_NTYPES + EvQ-index - 1
48 * See sfxge_get_txq_by_label() sfxge_ev.c
51 #include <sys/cdefs.h>
52 __FBSDID("$FreeBSD$");
56 #include <sys/param.h>
57 #include <sys/malloc.h>
60 #include <sys/socket.h>
61 #include <sys/sysctl.h>
62 #include <sys/syslog.h>
63 #include <sys/limits.h>
66 #include <net/ethernet.h>
68 #include <net/if_vlan_var.h>
70 #include <netinet/in.h>
71 #include <netinet/ip.h>
72 #include <netinet/ip6.h>
73 #include <netinet/tcp.h>
76 #include <net/rss_config.h>
79 #include "common/efx.h"
85 #define SFXGE_PARAM_TX_DPL_GET_MAX SFXGE_PARAM(tx_dpl_get_max)
86 static int sfxge_tx_dpl_get_max = SFXGE_TX_DPL_GET_PKT_LIMIT_DEFAULT;
87 TUNABLE_INT(SFXGE_PARAM_TX_DPL_GET_MAX, &sfxge_tx_dpl_get_max);
88 SYSCTL_INT(_hw_sfxge, OID_AUTO, tx_dpl_get_max, CTLFLAG_RDTUN,
89 &sfxge_tx_dpl_get_max, 0,
90 "Maximum number of any packets in deferred packet get-list");
92 #define SFXGE_PARAM_TX_DPL_GET_NON_TCP_MAX \
93 SFXGE_PARAM(tx_dpl_get_non_tcp_max)
94 static int sfxge_tx_dpl_get_non_tcp_max =
95 SFXGE_TX_DPL_GET_NON_TCP_PKT_LIMIT_DEFAULT;
96 TUNABLE_INT(SFXGE_PARAM_TX_DPL_GET_NON_TCP_MAX, &sfxge_tx_dpl_get_non_tcp_max);
97 SYSCTL_INT(_hw_sfxge, OID_AUTO, tx_dpl_get_non_tcp_max, CTLFLAG_RDTUN,
98 &sfxge_tx_dpl_get_non_tcp_max, 0,
99 "Maximum number of non-TCP packets in deferred packet get-list");
101 #define SFXGE_PARAM_TX_DPL_PUT_MAX SFXGE_PARAM(tx_dpl_put_max)
102 static int sfxge_tx_dpl_put_max = SFXGE_TX_DPL_PUT_PKT_LIMIT_DEFAULT;
103 TUNABLE_INT(SFXGE_PARAM_TX_DPL_PUT_MAX, &sfxge_tx_dpl_put_max);
104 SYSCTL_INT(_hw_sfxge, OID_AUTO, tx_dpl_put_max, CTLFLAG_RDTUN,
105 &sfxge_tx_dpl_put_max, 0,
106 "Maximum number of any packets in deferred packet put-list");
108 #define SFXGE_PARAM_TSO_FW_ASSISTED SFXGE_PARAM(tso_fw_assisted)
109 static int sfxge_tso_fw_assisted = (SFXGE_FATSOV1 | SFXGE_FATSOV2);
110 TUNABLE_INT(SFXGE_PARAM_TSO_FW_ASSISTED, &sfxge_tso_fw_assisted);
111 SYSCTL_INT(_hw_sfxge, OID_AUTO, tso_fw_assisted, CTLFLAG_RDTUN,
112 &sfxge_tso_fw_assisted, 0,
113 "Bitmask of FW-assisted TSO allowed to use if supported by NIC firmware");
116 static const struct {
119 } sfxge_tx_stats[] = {
120 #define SFXGE_TX_STAT(name, member) \
121 { #name, offsetof(struct sfxge_txq, member) }
122 SFXGE_TX_STAT(tso_bursts, tso_bursts),
123 SFXGE_TX_STAT(tso_packets, tso_packets),
124 SFXGE_TX_STAT(tso_long_headers, tso_long_headers),
125 SFXGE_TX_STAT(tso_pdrop_too_many, tso_pdrop_too_many),
126 SFXGE_TX_STAT(tso_pdrop_no_rsrc, tso_pdrop_no_rsrc),
127 SFXGE_TX_STAT(tx_collapses, collapses),
128 SFXGE_TX_STAT(tx_drops, drops),
129 SFXGE_TX_STAT(tx_get_overflow, get_overflow),
130 SFXGE_TX_STAT(tx_get_non_tcp_overflow, get_non_tcp_overflow),
131 SFXGE_TX_STAT(tx_put_overflow, put_overflow),
132 SFXGE_TX_STAT(tx_netdown_drops, netdown_drops),
136 /* Forward declarations. */
137 static void sfxge_tx_qdpl_service(struct sfxge_txq *txq);
138 static void sfxge_tx_qlist_post(struct sfxge_txq *txq);
139 static void sfxge_tx_qunblock(struct sfxge_txq *txq);
140 static int sfxge_tx_queue_tso(struct sfxge_txq *txq, struct mbuf *mbuf,
141 const bus_dma_segment_t *dma_seg, int n_dma_seg,
145 sfxge_tx_maybe_insert_tag(struct sfxge_txq *txq, struct mbuf *mbuf)
147 uint16_t this_tag = ((mbuf->m_flags & M_VLANTAG) ?
148 mbuf->m_pkthdr.ether_vtag :
151 if (this_tag == txq->hw_vlan_tci)
154 efx_tx_qdesc_vlantci_create(txq->common,
157 txq->n_pend_desc = 1;
158 txq->hw_vlan_tci = this_tag;
163 sfxge_next_stmp(struct sfxge_txq *txq, struct sfxge_tx_mapping **pstmp)
165 KASSERT((*pstmp)->flags == 0, ("stmp flags are not 0"));
166 if (__predict_false(*pstmp ==
167 &txq->stmp[txq->ptr_mask]))
168 *pstmp = &txq->stmp[0];
175 sfxge_tx_qcomplete(struct sfxge_txq *txq, struct sfxge_evq *evq)
177 unsigned int completed;
179 SFXGE_EVQ_LOCK_ASSERT_OWNED(evq);
181 completed = txq->completed;
182 while (completed != txq->pending) {
183 struct sfxge_tx_mapping *stmp;
186 id = completed++ & txq->ptr_mask;
188 stmp = &txq->stmp[id];
189 if (stmp->flags & TX_BUF_UNMAP) {
190 bus_dmamap_unload(txq->packet_dma_tag, stmp->map);
191 if (stmp->flags & TX_BUF_MBUF) {
192 struct mbuf *m = stmp->u.mbuf;
197 free(stmp->u.heap_buf, M_SFXGE);
202 txq->completed = completed;
204 /* Check whether we need to unblock the queue. */
209 level = txq->added - txq->completed;
210 if (level <= SFXGE_TXQ_UNBLOCK_LEVEL(txq->entries))
211 sfxge_tx_qunblock(txq);
216 sfxge_is_mbuf_non_tcp(struct mbuf *mbuf)
218 /* Absence of TCP checksum flags does not mean that it is non-TCP
219 * but it should be true if user wants to achieve high throughput.
221 return (!(mbuf->m_pkthdr.csum_flags & (CSUM_IP_TCP | CSUM_IP6_TCP)));
225 * Reorder the put list and append it to the get list.
228 sfxge_tx_qdpl_swizzle(struct sfxge_txq *txq)
230 struct sfxge_tx_dpl *stdp;
231 struct mbuf *mbuf, *get_next, **get_tailp;
232 volatile uintptr_t *putp;
235 unsigned int non_tcp_count;
237 SFXGE_TXQ_LOCK_ASSERT_OWNED(txq);
241 /* Acquire the put list. */
242 putp = &stdp->std_put;
243 put = atomic_readandclear_ptr(putp);
249 /* Reverse the put list. */
250 get_tailp = &mbuf->m_nextpkt;
256 struct mbuf *put_next;
258 non_tcp_count += sfxge_is_mbuf_non_tcp(mbuf);
259 put_next = mbuf->m_nextpkt;
260 mbuf->m_nextpkt = get_next;
265 } while (mbuf != NULL);
267 if (count > stdp->std_put_hiwat)
268 stdp->std_put_hiwat = count;
270 /* Append the reversed put list to the get list. */
271 KASSERT(*get_tailp == NULL, ("*get_tailp != NULL"));
272 *stdp->std_getp = get_next;
273 stdp->std_getp = get_tailp;
274 stdp->std_get_count += count;
275 stdp->std_get_non_tcp_count += non_tcp_count;
279 sfxge_tx_qreap(struct sfxge_txq *txq)
281 SFXGE_TXQ_LOCK_ASSERT_OWNED(txq);
283 txq->reaped = txq->completed;
287 sfxge_tx_qlist_post(struct sfxge_txq *txq)
289 unsigned int old_added;
290 unsigned int block_level;
294 SFXGE_TXQ_LOCK_ASSERT_OWNED(txq);
296 KASSERT(txq->n_pend_desc != 0, ("txq->n_pend_desc == 0"));
297 KASSERT(txq->n_pend_desc <= txq->max_pkt_desc,
298 ("txq->n_pend_desc too large"));
299 KASSERT(!txq->blocked, ("txq->blocked"));
301 old_added = txq->added;
303 /* Post the fragment list. */
304 rc = efx_tx_qdesc_post(txq->common, txq->pend_desc, txq->n_pend_desc,
305 txq->reaped, &txq->added);
306 KASSERT(rc == 0, ("efx_tx_qdesc_post() failed"));
308 /* If efx_tx_qdesc_post() had to refragment, our information about
309 * buffers to free may be associated with the wrong
312 KASSERT(txq->added - old_added == txq->n_pend_desc,
313 ("efx_tx_qdesc_post() refragmented descriptors"));
315 level = txq->added - txq->reaped;
316 KASSERT(level <= txq->entries, ("overfilled TX queue"));
318 /* Clear the fragment list. */
319 txq->n_pend_desc = 0;
322 * Set the block level to ensure there is space to generate a
323 * large number of descriptors for TSO.
325 block_level = EFX_TXQ_LIMIT(txq->entries) - txq->max_pkt_desc;
327 /* Have we reached the block level? */
328 if (level < block_level)
331 /* Reap, and check again */
333 level = txq->added - txq->reaped;
334 if (level < block_level)
340 * Avoid a race with completion interrupt handling that could leave
345 level = txq->added - txq->reaped;
346 if (level < block_level) {
352 static int sfxge_tx_queue_mbuf(struct sfxge_txq *txq, struct mbuf *mbuf)
354 bus_dmamap_t *used_map;
356 bus_dma_segment_t dma_seg[SFXGE_TX_MAPPING_MAX_SEG];
358 struct sfxge_tx_mapping *stmp;
364 uint16_t hw_vlan_tci_prev;
367 KASSERT(!txq->blocked, ("txq->blocked"));
369 #if SFXGE_TX_PARSE_EARLY
371 * If software TSO is used, we still need to copy packet header,
372 * even if we have already parsed it early before enqueue.
374 if ((mbuf->m_pkthdr.csum_flags & CSUM_TSO) &&
375 (txq->tso_fw_assisted == 0))
376 prefetch_read_many(mbuf->m_data);
379 * Prefetch packet header since we need to parse it and extract
380 * IP ID, TCP sequence number and flags.
382 if (mbuf->m_pkthdr.csum_flags & CSUM_TSO)
383 prefetch_read_many(mbuf->m_data);
386 if (__predict_false(txq->init_state != SFXGE_TXQ_STARTED)) {
391 /* Load the packet for DMA. */
392 id = txq->added & txq->ptr_mask;
393 stmp = &txq->stmp[id];
394 rc = bus_dmamap_load_mbuf_sg(txq->packet_dma_tag, stmp->map,
395 mbuf, dma_seg, &n_dma_seg, 0);
398 struct mbuf *new_mbuf = m_collapse(mbuf, M_NOWAIT,
399 SFXGE_TX_MAPPING_MAX_SEG);
400 if (new_mbuf == NULL)
404 rc = bus_dmamap_load_mbuf_sg(txq->packet_dma_tag,
406 dma_seg, &n_dma_seg, 0);
411 /* Make the packet visible to the hardware. */
412 bus_dmamap_sync(txq->packet_dma_tag, stmp->map, BUS_DMASYNC_PREWRITE);
414 used_map = &stmp->map;
416 hw_vlan_tci_prev = txq->hw_vlan_tci;
418 vlan_tagged = sfxge_tx_maybe_insert_tag(txq, mbuf);
420 sfxge_next_stmp(txq, &stmp);
422 if (mbuf->m_pkthdr.csum_flags & CSUM_TSO) {
423 rc = sfxge_tx_queue_tso(txq, mbuf, dma_seg, n_dma_seg, vlan_tagged);
426 stmp = &txq->stmp[(rc - 1) & txq->ptr_mask];
428 /* Add the mapping to the fragment list, and set flags
434 desc = &txq->pend_desc[i + vlan_tagged];
435 eop = (i == n_dma_seg - 1);
436 efx_tx_qdesc_dma_create(txq->common,
444 sfxge_next_stmp(txq, &stmp);
446 txq->n_pend_desc = n_dma_seg + vlan_tagged;
450 * If the mapping required more than one descriptor
451 * then we need to associate the DMA map with the last
452 * descriptor, not the first.
454 if (used_map != &stmp->map) {
456 stmp->map = *used_map;
461 stmp->flags = TX_BUF_UNMAP | TX_BUF_MBUF;
463 /* Post the fragment list. */
464 sfxge_tx_qlist_post(txq);
469 txq->hw_vlan_tci = hw_vlan_tci_prev;
470 bus_dmamap_unload(txq->packet_dma_tag, *used_map);
472 /* Drop the packet on the floor. */
480 * Drain the deferred packet list into the transmit queue.
483 sfxge_tx_qdpl_drain(struct sfxge_txq *txq)
485 struct sfxge_softc *sc;
486 struct sfxge_tx_dpl *stdp;
487 struct mbuf *mbuf, *next;
489 unsigned int non_tcp_count;
493 SFXGE_TXQ_LOCK_ASSERT_OWNED(txq);
499 if (__predict_true(txq->init_state == SFXGE_TXQ_STARTED)) {
500 prefetch_read_many(sc->enp);
501 prefetch_read_many(txq->common);
504 mbuf = stdp->std_get;
505 count = stdp->std_get_count;
506 non_tcp_count = stdp->std_get_non_tcp_count;
508 if (count > stdp->std_get_hiwat)
509 stdp->std_get_hiwat = count;
512 KASSERT(mbuf != NULL, ("mbuf == NULL"));
514 next = mbuf->m_nextpkt;
515 mbuf->m_nextpkt = NULL;
517 ETHER_BPF_MTAP(sc->ifnet, mbuf); /* packet capture */
520 prefetch_read_many(next);
522 rc = sfxge_tx_queue_mbuf(txq, mbuf);
524 non_tcp_count -= sfxge_is_mbuf_non_tcp(mbuf);
532 /* Push the fragments to the hardware in batches. */
533 if (txq->added - pushed >= SFXGE_TX_BATCH) {
534 efx_tx_qpush(txq->common, txq->added, pushed);
540 KASSERT(mbuf == NULL, ("mbuf != NULL"));
541 KASSERT(non_tcp_count == 0,
542 ("inconsistent TCP/non-TCP detection"));
543 stdp->std_get = NULL;
544 stdp->std_get_count = 0;
545 stdp->std_get_non_tcp_count = 0;
546 stdp->std_getp = &stdp->std_get;
548 stdp->std_get = mbuf;
549 stdp->std_get_count = count;
550 stdp->std_get_non_tcp_count = non_tcp_count;
553 if (txq->added != pushed)
554 efx_tx_qpush(txq->common, txq->added, pushed);
556 KASSERT(txq->blocked || stdp->std_get_count == 0,
557 ("queue unblocked but count is non-zero"));
560 #define SFXGE_TX_QDPL_PENDING(_txq) ((_txq)->dpl.std_put != 0)
563 * Service the deferred packet list.
565 * NOTE: drops the txq mutex!
568 sfxge_tx_qdpl_service(struct sfxge_txq *txq)
570 SFXGE_TXQ_LOCK_ASSERT_OWNED(txq);
573 if (SFXGE_TX_QDPL_PENDING(txq))
574 sfxge_tx_qdpl_swizzle(txq);
577 sfxge_tx_qdpl_drain(txq);
579 SFXGE_TXQ_UNLOCK(txq);
580 } while (SFXGE_TX_QDPL_PENDING(txq) &&
581 SFXGE_TXQ_TRYLOCK(txq));
585 * Put a packet on the deferred packet get-list.
588 sfxge_tx_qdpl_put_locked(struct sfxge_txq *txq, struct mbuf *mbuf)
590 struct sfxge_tx_dpl *stdp;
594 KASSERT(mbuf->m_nextpkt == NULL, ("mbuf->m_nextpkt != NULL"));
596 SFXGE_TXQ_LOCK_ASSERT_OWNED(txq);
598 if (stdp->std_get_count >= stdp->std_get_max) {
602 if (sfxge_is_mbuf_non_tcp(mbuf)) {
603 if (stdp->std_get_non_tcp_count >=
604 stdp->std_get_non_tcp_max) {
605 txq->get_non_tcp_overflow++;
608 stdp->std_get_non_tcp_count++;
611 *(stdp->std_getp) = mbuf;
612 stdp->std_getp = &mbuf->m_nextpkt;
613 stdp->std_get_count++;
619 * Put a packet on the deferred packet put-list.
621 * We overload the csum_data field in the mbuf to keep track of this length
622 * because there is no cheap alternative to avoid races.
625 sfxge_tx_qdpl_put_unlocked(struct sfxge_txq *txq, struct mbuf *mbuf)
627 struct sfxge_tx_dpl *stdp;
628 volatile uintptr_t *putp;
631 unsigned int put_count;
633 KASSERT(mbuf->m_nextpkt == NULL, ("mbuf->m_nextpkt != NULL"));
635 SFXGE_TXQ_LOCK_ASSERT_NOTOWNED(txq);
638 putp = &stdp->std_put;
639 new = (uintptr_t)mbuf;
644 struct mbuf *mp = (struct mbuf *)old;
645 put_count = mp->m_pkthdr.csum_data;
648 if (put_count >= stdp->std_put_max) {
649 atomic_add_long(&txq->put_overflow, 1);
652 mbuf->m_pkthdr.csum_data = put_count + 1;
653 mbuf->m_nextpkt = (void *)old;
654 } while (atomic_cmpset_ptr(putp, old, new) == 0);
660 * Called from if_transmit - will try to grab the txq lock and enqueue to the
661 * put list if it succeeds, otherwise try to push onto the defer list if space.
664 sfxge_tx_packet_add(struct sfxge_txq *txq, struct mbuf *m)
668 if (!SFXGE_LINK_UP(txq->sc)) {
669 atomic_add_long(&txq->netdown_drops, 1);
674 * Try to grab the txq lock. If we are able to get the lock,
675 * the packet will be appended to the "get list" of the deferred
676 * packet list. Otherwise, it will be pushed on the "put list".
678 if (SFXGE_TXQ_TRYLOCK(txq)) {
679 /* First swizzle put-list to get-list to keep order */
680 sfxge_tx_qdpl_swizzle(txq);
682 rc = sfxge_tx_qdpl_put_locked(txq, m);
684 /* Try to service the list. */
685 sfxge_tx_qdpl_service(txq);
686 /* Lock has been dropped. */
688 rc = sfxge_tx_qdpl_put_unlocked(txq, m);
691 * Try to grab the lock again.
693 * If we are able to get the lock, we need to process
694 * the deferred packet list. If we are not able to get
695 * the lock, another thread is processing the list.
697 if ((rc == 0) && SFXGE_TXQ_TRYLOCK(txq)) {
698 sfxge_tx_qdpl_service(txq);
699 /* Lock has been dropped. */
703 SFXGE_TXQ_LOCK_ASSERT_NOTOWNED(txq);
709 sfxge_tx_qdpl_flush(struct sfxge_txq *txq)
711 struct sfxge_tx_dpl *stdp = &txq->dpl;
712 struct mbuf *mbuf, *next;
716 sfxge_tx_qdpl_swizzle(txq);
717 for (mbuf = stdp->std_get; mbuf != NULL; mbuf = next) {
718 next = mbuf->m_nextpkt;
721 stdp->std_get = NULL;
722 stdp->std_get_count = 0;
723 stdp->std_get_non_tcp_count = 0;
724 stdp->std_getp = &stdp->std_get;
726 SFXGE_TXQ_UNLOCK(txq);
730 sfxge_if_qflush(struct ifnet *ifp)
732 struct sfxge_softc *sc;
737 for (i = 0; i < sc->txq_count; i++)
738 sfxge_tx_qdpl_flush(sc->txq[i]);
741 #if SFXGE_TX_PARSE_EARLY
743 /* There is little space for user data in mbuf pkthdr, so we
744 * use l*hlen fields which are not used by the driver otherwise
745 * to store header offsets.
746 * The fields are 8-bit, but it's ok, no header may be longer than 255 bytes.
750 #define TSO_MBUF_PROTO(_mbuf) ((_mbuf)->m_pkthdr.PH_loc.sixteen[0])
751 /* We abuse l5hlen here because PH_loc can hold only 64 bits of data */
752 #define TSO_MBUF_FLAGS(_mbuf) ((_mbuf)->m_pkthdr.l5hlen)
753 #define TSO_MBUF_PACKETID(_mbuf) ((_mbuf)->m_pkthdr.PH_loc.sixteen[1])
754 #define TSO_MBUF_SEQNUM(_mbuf) ((_mbuf)->m_pkthdr.PH_loc.thirtytwo[1])
756 static void sfxge_parse_tx_packet(struct mbuf *mbuf)
758 struct ether_header *eh = mtod(mbuf, struct ether_header *);
759 const struct tcphdr *th;
760 struct tcphdr th_copy;
762 /* Find network protocol and header */
763 TSO_MBUF_PROTO(mbuf) = eh->ether_type;
764 if (TSO_MBUF_PROTO(mbuf) == htons(ETHERTYPE_VLAN)) {
765 struct ether_vlan_header *veh =
766 mtod(mbuf, struct ether_vlan_header *);
767 TSO_MBUF_PROTO(mbuf) = veh->evl_proto;
768 mbuf->m_pkthdr.l2hlen = sizeof(*veh);
770 mbuf->m_pkthdr.l2hlen = sizeof(*eh);
773 /* Find TCP header */
774 if (TSO_MBUF_PROTO(mbuf) == htons(ETHERTYPE_IP)) {
775 const struct ip *iph = (const struct ip *)mtodo(mbuf, mbuf->m_pkthdr.l2hlen);
777 KASSERT(iph->ip_p == IPPROTO_TCP,
778 ("TSO required on non-TCP packet"));
779 mbuf->m_pkthdr.l3hlen = mbuf->m_pkthdr.l2hlen + 4 * iph->ip_hl;
780 TSO_MBUF_PACKETID(mbuf) = iph->ip_id;
782 KASSERT(TSO_MBUF_PROTO(mbuf) == htons(ETHERTYPE_IPV6),
783 ("TSO required on non-IP packet"));
784 KASSERT(((const struct ip6_hdr *)mtodo(mbuf, mbuf->m_pkthdr.l2hlen))->ip6_nxt ==
786 ("TSO required on non-TCP packet"));
787 mbuf->m_pkthdr.l3hlen = mbuf->m_pkthdr.l2hlen + sizeof(struct ip6_hdr);
788 TSO_MBUF_PACKETID(mbuf) = 0;
791 KASSERT(mbuf->m_len >= mbuf->m_pkthdr.l3hlen,
792 ("network header is fragmented in mbuf"));
794 /* We need TCP header including flags (window is the next) */
795 if (mbuf->m_len < mbuf->m_pkthdr.l3hlen + offsetof(struct tcphdr, th_win)) {
796 m_copydata(mbuf, mbuf->m_pkthdr.l3hlen, sizeof(th_copy),
800 th = (const struct tcphdr *)mtodo(mbuf, mbuf->m_pkthdr.l3hlen);
803 mbuf->m_pkthdr.l4hlen = mbuf->m_pkthdr.l3hlen + 4 * th->th_off;
804 TSO_MBUF_SEQNUM(mbuf) = ntohl(th->th_seq);
806 /* These flags must not be duplicated */
808 * RST should not be duplicated as well, but FreeBSD kernel
809 * generates TSO packets with RST flag. So, do not assert
812 KASSERT(!(th->th_flags & (TH_URG | TH_SYN)),
813 ("incompatible TCP flag 0x%x on TSO packet",
814 th->th_flags & (TH_URG | TH_SYN)));
815 TSO_MBUF_FLAGS(mbuf) = th->th_flags;
820 * TX start -- called by the stack.
823 sfxge_if_transmit(struct ifnet *ifp, struct mbuf *m)
825 struct sfxge_softc *sc;
826 struct sfxge_txq *txq;
829 sc = (struct sfxge_softc *)ifp->if_softc;
832 * Transmit may be called when interface is up from the kernel
833 * point of view, but not yet up (in progress) from the driver
834 * point of view. I.e. link aggregation bring up.
835 * Transmit may be called when interface is up from the driver
836 * point of view, but already down from the kernel point of
837 * view. I.e. Rx when interface shutdown is in progress.
839 KASSERT((ifp->if_flags & IFF_UP) || (sc->if_flags & IFF_UP),
840 ("interface not up"));
842 /* Pick the desired transmit queue. */
843 if (m->m_pkthdr.csum_flags &
844 (CSUM_DELAY_DATA | CSUM_TCP_IPV6 | CSUM_UDP_IPV6 | CSUM_TSO)) {
851 * Select a TX queue which matches the corresponding
852 * RX queue for the hash in order to assign both
853 * TX and RX parts of the flow to the same CPU
855 if (rss_m2bucket(m, &bucket_id) == 0)
856 index = bucket_id % (sc->txq_count - (SFXGE_TXQ_NTYPES - 1));
858 /* check if flowid is set */
859 if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) {
860 uint32_t hash = m->m_pkthdr.flowid;
861 uint32_t idx = hash % nitems(sc->rx_indir_table);
863 index = sc->rx_indir_table[idx];
866 #if SFXGE_TX_PARSE_EARLY
867 if (m->m_pkthdr.csum_flags & CSUM_TSO)
868 sfxge_parse_tx_packet(m);
870 txq = sc->txq[SFXGE_TXQ_IP_TCP_UDP_CKSUM + index];
871 } else if (m->m_pkthdr.csum_flags & CSUM_DELAY_IP) {
872 txq = sc->txq[SFXGE_TXQ_IP_CKSUM];
874 txq = sc->txq[SFXGE_TXQ_NON_CKSUM];
877 rc = sfxge_tx_packet_add(txq, m);
885 * Software "TSO". Not quite as good as doing it in hardware, but
886 * still faster than segmenting in the stack.
889 struct sfxge_tso_state {
890 /* Output position */
891 unsigned out_len; /* Remaining length in current segment */
892 unsigned seqnum; /* Current sequence number */
893 unsigned packet_space; /* Remaining space in current packet */
894 unsigned segs_space; /* Remaining number of DMA segments
895 for the packet (FATSOv2 only) */
898 uint64_t dma_addr; /* DMA address of current position */
899 unsigned in_len; /* Remaining length in current mbuf */
901 const struct mbuf *mbuf; /* Input mbuf (head of chain) */
902 u_short protocol; /* Network protocol (after VLAN decap) */
903 ssize_t nh_off; /* Offset of network header */
904 ssize_t tcph_off; /* Offset of TCP header */
905 unsigned header_len; /* Number of bytes of header */
906 unsigned seg_size; /* TCP segment size */
907 int fw_assisted; /* Use FW-assisted TSO */
908 u_short packet_id; /* IPv4 packet ID from the original packet */
909 uint8_t tcp_flags; /* TCP flags */
910 efx_desc_t header_desc; /* Precomputed header descriptor for
914 #if !SFXGE_TX_PARSE_EARLY
915 static const struct ip *tso_iph(const struct sfxge_tso_state *tso)
917 KASSERT(tso->protocol == htons(ETHERTYPE_IP),
918 ("tso_iph() in non-IPv4 state"));
919 return (const struct ip *)(tso->mbuf->m_data + tso->nh_off);
922 static __unused const struct ip6_hdr *tso_ip6h(const struct sfxge_tso_state *tso)
924 KASSERT(tso->protocol == htons(ETHERTYPE_IPV6),
925 ("tso_ip6h() in non-IPv6 state"));
926 return (const struct ip6_hdr *)(tso->mbuf->m_data + tso->nh_off);
929 static const struct tcphdr *tso_tcph(const struct sfxge_tso_state *tso)
931 return (const struct tcphdr *)(tso->mbuf->m_data + tso->tcph_off);
936 /* Size of preallocated TSO header buffers. Larger blocks must be
937 * allocated from the heap.
939 #define TSOH_STD_SIZE 128
941 /* At most half the descriptors in the queue at any time will refer to
942 * a TSO header buffer, since they must always be followed by a
943 * payload descriptor referring to an mbuf.
945 #define TSOH_COUNT(_txq_entries) ((_txq_entries) / 2u)
946 #define TSOH_PER_PAGE (PAGE_SIZE / TSOH_STD_SIZE)
947 #define TSOH_PAGE_COUNT(_txq_entries) \
948 howmany(TSOH_COUNT(_txq_entries), TSOH_PER_PAGE)
950 static int tso_init(struct sfxge_txq *txq)
952 struct sfxge_softc *sc = txq->sc;
953 unsigned int tsoh_page_count = TSOH_PAGE_COUNT(sc->txq_entries);
956 /* Allocate TSO header buffers */
957 txq->tsoh_buffer = malloc(tsoh_page_count * sizeof(txq->tsoh_buffer[0]),
960 for (i = 0; i < tsoh_page_count; i++) {
961 rc = sfxge_dma_alloc(sc, PAGE_SIZE, &txq->tsoh_buffer[i]);
970 sfxge_dma_free(&txq->tsoh_buffer[i]);
971 free(txq->tsoh_buffer, M_SFXGE);
972 txq->tsoh_buffer = NULL;
976 static void tso_fini(struct sfxge_txq *txq)
980 if (txq->tsoh_buffer != NULL) {
981 for (i = 0; i < TSOH_PAGE_COUNT(txq->sc->txq_entries); i++)
982 sfxge_dma_free(&txq->tsoh_buffer[i]);
983 free(txq->tsoh_buffer, M_SFXGE);
987 static void tso_start(struct sfxge_txq *txq, struct sfxge_tso_state *tso,
988 const bus_dma_segment_t *hdr_dma_seg,
991 const efx_nic_cfg_t *encp = efx_nic_cfg_get(txq->sc->enp);
992 #if !SFXGE_TX_PARSE_EARLY
993 struct ether_header *eh = mtod(mbuf, struct ether_header *);
994 const struct tcphdr *th;
995 struct tcphdr th_copy;
998 tso->fw_assisted = txq->tso_fw_assisted;
1001 /* Find network protocol and header */
1002 #if !SFXGE_TX_PARSE_EARLY
1003 tso->protocol = eh->ether_type;
1004 if (tso->protocol == htons(ETHERTYPE_VLAN)) {
1005 struct ether_vlan_header *veh =
1006 mtod(mbuf, struct ether_vlan_header *);
1007 tso->protocol = veh->evl_proto;
1008 tso->nh_off = sizeof(*veh);
1010 tso->nh_off = sizeof(*eh);
1013 tso->protocol = TSO_MBUF_PROTO(mbuf);
1014 tso->nh_off = mbuf->m_pkthdr.l2hlen;
1015 tso->tcph_off = mbuf->m_pkthdr.l3hlen;
1016 tso->packet_id = ntohs(TSO_MBUF_PACKETID(mbuf));
1019 #if !SFXGE_TX_PARSE_EARLY
1020 /* Find TCP header */
1021 if (tso->protocol == htons(ETHERTYPE_IP)) {
1022 KASSERT(tso_iph(tso)->ip_p == IPPROTO_TCP,
1023 ("TSO required on non-TCP packet"));
1024 tso->tcph_off = tso->nh_off + 4 * tso_iph(tso)->ip_hl;
1025 tso->packet_id = ntohs(tso_iph(tso)->ip_id);
1027 KASSERT(tso->protocol == htons(ETHERTYPE_IPV6),
1028 ("TSO required on non-IP packet"));
1029 KASSERT(tso_ip6h(tso)->ip6_nxt == IPPROTO_TCP,
1030 ("TSO required on non-TCP packet"));
1031 tso->tcph_off = tso->nh_off + sizeof(struct ip6_hdr);
1037 if (tso->fw_assisted &&
1038 __predict_false(tso->tcph_off >
1039 encp->enc_tx_tso_tcp_header_offset_limit)) {
1040 tso->fw_assisted = 0;
1044 #if !SFXGE_TX_PARSE_EARLY
1045 KASSERT(mbuf->m_len >= tso->tcph_off,
1046 ("network header is fragmented in mbuf"));
1047 /* We need TCP header including flags (window is the next) */
1048 if (mbuf->m_len < tso->tcph_off + offsetof(struct tcphdr, th_win)) {
1049 m_copydata(tso->mbuf, tso->tcph_off, sizeof(th_copy),
1055 tso->header_len = tso->tcph_off + 4 * th->th_off;
1057 tso->header_len = mbuf->m_pkthdr.l4hlen;
1059 tso->seg_size = mbuf->m_pkthdr.tso_segsz;
1061 #if !SFXGE_TX_PARSE_EARLY
1062 tso->seqnum = ntohl(th->th_seq);
1064 /* These flags must not be duplicated */
1066 * RST should not be duplicated as well, but FreeBSD kernel
1067 * generates TSO packets with RST flag. So, do not assert
1070 KASSERT(!(th->th_flags & (TH_URG | TH_SYN)),
1071 ("incompatible TCP flag 0x%x on TSO packet",
1072 th->th_flags & (TH_URG | TH_SYN)));
1073 tso->tcp_flags = th->th_flags;
1075 tso->seqnum = TSO_MBUF_SEQNUM(mbuf);
1076 tso->tcp_flags = TSO_MBUF_FLAGS(mbuf);
1079 tso->out_len = mbuf->m_pkthdr.len - tso->header_len;
1081 if (tso->fw_assisted) {
1082 if (hdr_dma_seg->ds_len >= tso->header_len)
1083 efx_tx_qdesc_dma_create(txq->common,
1084 hdr_dma_seg->ds_addr,
1089 tso->fw_assisted = 0;
1094 * tso_fill_packet_with_fragment - form descriptors for the current fragment
1096 * Form descriptors for the current fragment, until we reach the end
1097 * of fragment or end-of-packet. Return 0 on success, 1 if not enough
1100 static void tso_fill_packet_with_fragment(struct sfxge_txq *txq,
1101 struct sfxge_tso_state *tso)
1105 uint64_t dma_addr = tso->dma_addr;
1108 if (tso->in_len == 0 || tso->packet_space == 0)
1111 KASSERT(tso->in_len > 0, ("TSO input length went negative"));
1112 KASSERT(tso->packet_space > 0, ("TSO packet space went negative"));
1114 if (tso->fw_assisted & SFXGE_FATSOV2) {
1119 if (n < tso->packet_space) {
1120 tso->packet_space -= n;
1123 tso->packet_space = tso->seg_size -
1124 (n - tso->packet_space) % tso->seg_size;
1126 EFX_TX_FATSOV2_DMA_SEGS_PER_PKT_MAX - 1 -
1127 (tso->packet_space != tso->seg_size);
1130 n = min(tso->in_len, tso->packet_space);
1131 tso->packet_space -= n;
1138 * It is OK to use binary OR below to avoid extra branching
1139 * since all conditions may always be checked.
1141 eop = (tso->out_len == 0) | (tso->packet_space == 0) |
1142 (tso->segs_space == 0);
1144 desc = &txq->pend_desc[txq->n_pend_desc++];
1145 efx_tx_qdesc_dma_create(txq->common, dma_addr, n, eop, desc);
1148 /* Callback from bus_dmamap_load() for long TSO headers. */
1149 static void tso_map_long_header(void *dma_addr_ret,
1150 bus_dma_segment_t *segs, int nseg,
1153 *(uint64_t *)dma_addr_ret = ((__predict_true(error == 0) &&
1154 __predict_true(nseg == 1)) ?
1159 * tso_start_new_packet - generate a new header and prepare for the new packet
1161 * Generate a new header and prepare for the new packet. Return 0 on
1162 * success, or an error code if failed to alloc header.
1164 static int tso_start_new_packet(struct sfxge_txq *txq,
1165 struct sfxge_tso_state *tso,
1168 unsigned int id = *idp;
1169 struct tcphdr *tsoh_th;
1177 if (tso->fw_assisted) {
1178 if (tso->fw_assisted & SFXGE_FATSOV2) {
1179 /* Add 2 FATSOv2 option descriptors */
1180 desc = &txq->pend_desc[txq->n_pend_desc];
1181 efx_tx_qdesc_tso2_create(txq->common,
1187 EFX_TX_FATSOV2_OPT_NDESCS);
1188 desc += EFX_TX_FATSOV2_OPT_NDESCS;
1189 txq->n_pend_desc += EFX_TX_FATSOV2_OPT_NDESCS;
1190 KASSERT(txq->stmp[id].flags == 0, ("stmp flags are not 0"));
1191 id = (id + EFX_TX_FATSOV2_OPT_NDESCS) & txq->ptr_mask;
1194 EFX_TX_FATSOV2_DMA_SEGS_PER_PKT_MAX - 1;
1196 uint8_t tcp_flags = tso->tcp_flags;
1198 if (tso->out_len > tso->seg_size)
1199 tcp_flags &= ~(TH_FIN | TH_PUSH);
1201 /* Add FATSOv1 option descriptor */
1202 desc = &txq->pend_desc[txq->n_pend_desc++];
1203 efx_tx_qdesc_tso_create(txq->common,
1208 KASSERT(txq->stmp[id].flags == 0, ("stmp flags are not 0"));
1209 id = (id + 1) & txq->ptr_mask;
1211 tso->seqnum += tso->seg_size;
1212 tso->segs_space = UINT_MAX;
1215 /* Header DMA descriptor */
1216 *desc = tso->header_desc;
1218 KASSERT(txq->stmp[id].flags == 0, ("stmp flags are not 0"));
1219 id = (id + 1) & txq->ptr_mask;
1221 /* Allocate a DMA-mapped header buffer. */
1222 if (__predict_true(tso->header_len <= TSOH_STD_SIZE)) {
1223 unsigned int page_index = (id / 2) / TSOH_PER_PAGE;
1224 unsigned int buf_index = (id / 2) % TSOH_PER_PAGE;
1226 header = (txq->tsoh_buffer[page_index].esm_base +
1227 buf_index * TSOH_STD_SIZE);
1228 dma_addr = (txq->tsoh_buffer[page_index].esm_addr +
1229 buf_index * TSOH_STD_SIZE);
1230 map = txq->tsoh_buffer[page_index].esm_map;
1232 KASSERT(txq->stmp[id].flags == 0,
1233 ("stmp flags are not 0"));
1235 struct sfxge_tx_mapping *stmp = &txq->stmp[id];
1237 /* We cannot use bus_dmamem_alloc() as that may sleep */
1238 header = malloc(tso->header_len, M_SFXGE, M_NOWAIT);
1239 if (__predict_false(!header))
1241 rc = bus_dmamap_load(txq->packet_dma_tag, stmp->map,
1242 header, tso->header_len,
1243 tso_map_long_header, &dma_addr,
1245 if (__predict_false(dma_addr == 0)) {
1247 /* Succeeded but got >1 segment */
1248 bus_dmamap_unload(txq->packet_dma_tag,
1252 free(header, M_SFXGE);
1257 txq->tso_long_headers++;
1258 stmp->u.heap_buf = header;
1259 stmp->flags = TX_BUF_UNMAP;
1262 tsoh_th = (struct tcphdr *)(header + tso->tcph_off);
1264 /* Copy and update the headers. */
1265 m_copydata(tso->mbuf, 0, tso->header_len, header);
1267 tsoh_th->th_seq = htonl(tso->seqnum);
1268 tso->seqnum += tso->seg_size;
1269 if (tso->out_len > tso->seg_size) {
1270 /* This packet will not finish the TSO burst. */
1271 ip_length = tso->header_len - tso->nh_off + tso->seg_size;
1272 tsoh_th->th_flags &= ~(TH_FIN | TH_PUSH);
1274 /* This packet will be the last in the TSO burst. */
1275 ip_length = tso->header_len - tso->nh_off + tso->out_len;
1278 if (tso->protocol == htons(ETHERTYPE_IP)) {
1279 struct ip *tsoh_iph = (struct ip *)(header + tso->nh_off);
1280 tsoh_iph->ip_len = htons(ip_length);
1281 /* XXX We should increment ip_id, but FreeBSD doesn't
1282 * currently allocate extra IDs for multiple segments.
1285 struct ip6_hdr *tsoh_iph =
1286 (struct ip6_hdr *)(header + tso->nh_off);
1287 tsoh_iph->ip6_plen = htons(ip_length - sizeof(*tsoh_iph));
1290 /* Make the header visible to the hardware. */
1291 bus_dmamap_sync(txq->packet_dma_tag, map, BUS_DMASYNC_PREWRITE);
1293 /* Form a descriptor for this header. */
1294 desc = &txq->pend_desc[txq->n_pend_desc++];
1295 efx_tx_qdesc_dma_create(txq->common,
1300 id = (id + 1) & txq->ptr_mask;
1302 tso->segs_space = UINT_MAX;
1304 tso->packet_space = tso->seg_size;
1312 sfxge_tx_queue_tso(struct sfxge_txq *txq, struct mbuf *mbuf,
1313 const bus_dma_segment_t *dma_seg, int n_dma_seg,
1316 struct sfxge_tso_state tso;
1318 unsigned skipped = 0;
1320 tso_start(txq, &tso, dma_seg, mbuf);
1322 while (dma_seg->ds_len + skipped <= tso.header_len) {
1323 skipped += dma_seg->ds_len;
1325 KASSERT(n_dma_seg, ("no payload found in TSO packet"));
1328 tso.in_len = dma_seg->ds_len - (tso.header_len - skipped);
1329 tso.dma_addr = dma_seg->ds_addr + (tso.header_len - skipped);
1331 id = (txq->added + vlan_tagged) & txq->ptr_mask;
1332 if (__predict_false(tso_start_new_packet(txq, &tso, &id)))
1336 tso_fill_packet_with_fragment(txq, &tso);
1337 /* Exactly one DMA descriptor is added */
1338 KASSERT(txq->stmp[id].flags == 0, ("stmp flags are not 0"));
1339 id = (id + 1) & txq->ptr_mask;
1341 /* Move onto the next fragment? */
1342 if (tso.in_len == 0) {
1347 tso.in_len = dma_seg->ds_len;
1348 tso.dma_addr = dma_seg->ds_addr;
1351 /* End of packet? */
1352 if ((tso.packet_space == 0) | (tso.segs_space == 0)) {
1353 unsigned int n_fatso_opt_desc =
1354 (tso.fw_assisted & SFXGE_FATSOV2) ?
1355 EFX_TX_FATSOV2_OPT_NDESCS :
1356 (tso.fw_assisted & SFXGE_FATSOV1) ? 1 : 0;
1358 /* If the queue is now full due to tiny MSS,
1359 * or we can't create another header, discard
1360 * the remainder of the input mbuf but do not
1361 * roll back the work we have done.
1363 if (txq->n_pend_desc + n_fatso_opt_desc +
1364 1 /* header */ + n_dma_seg > txq->max_pkt_desc) {
1365 txq->tso_pdrop_too_many++;
1368 if (__predict_false(tso_start_new_packet(txq, &tso,
1370 txq->tso_pdrop_no_rsrc++;
1381 sfxge_tx_qunblock(struct sfxge_txq *txq)
1383 struct sfxge_softc *sc;
1384 struct sfxge_evq *evq;
1387 evq = sc->evq[txq->evq_index];
1389 SFXGE_EVQ_LOCK_ASSERT_OWNED(evq);
1391 if (__predict_false(txq->init_state != SFXGE_TXQ_STARTED))
1394 SFXGE_TXQ_LOCK(txq);
1399 level = txq->added - txq->completed;
1400 if (level <= SFXGE_TXQ_UNBLOCK_LEVEL(txq->entries)) {
1401 /* reaped must be in sync with blocked */
1402 sfxge_tx_qreap(txq);
1407 sfxge_tx_qdpl_service(txq);
1408 /* note: lock has been dropped */
1412 sfxge_tx_qflush_done(struct sfxge_txq *txq)
1415 txq->flush_state = SFXGE_FLUSH_DONE;
1419 sfxge_tx_qstop(struct sfxge_softc *sc, unsigned int index)
1421 struct sfxge_txq *txq;
1422 struct sfxge_evq *evq;
1425 SFXGE_ADAPTER_LOCK_ASSERT_OWNED(sc);
1427 txq = sc->txq[index];
1428 evq = sc->evq[txq->evq_index];
1430 SFXGE_EVQ_LOCK(evq);
1431 SFXGE_TXQ_LOCK(txq);
1433 KASSERT(txq->init_state == SFXGE_TXQ_STARTED,
1434 ("txq->init_state != SFXGE_TXQ_STARTED"));
1436 txq->init_state = SFXGE_TXQ_INITIALIZED;
1438 if (txq->flush_state != SFXGE_FLUSH_DONE) {
1439 txq->flush_state = SFXGE_FLUSH_PENDING;
1441 SFXGE_EVQ_UNLOCK(evq);
1442 SFXGE_TXQ_UNLOCK(txq);
1444 /* Flush the transmit queue. */
1445 if (efx_tx_qflush(txq->common) != 0) {
1446 log(LOG_ERR, "%s: Flushing Tx queue %u failed\n",
1447 device_get_nameunit(sc->dev), index);
1448 txq->flush_state = SFXGE_FLUSH_DONE;
1452 /* Spin for 100ms. */
1454 if (txq->flush_state != SFXGE_FLUSH_PENDING)
1456 } while (++count < 20);
1458 SFXGE_EVQ_LOCK(evq);
1459 SFXGE_TXQ_LOCK(txq);
1461 KASSERT(txq->flush_state != SFXGE_FLUSH_FAILED,
1462 ("txq->flush_state == SFXGE_FLUSH_FAILED"));
1464 if (txq->flush_state != SFXGE_FLUSH_DONE) {
1466 log(LOG_ERR, "%s: Cannot flush Tx queue %u\n",
1467 device_get_nameunit(sc->dev), index);
1468 txq->flush_state = SFXGE_FLUSH_DONE;
1473 txq->pending = txq->added;
1475 sfxge_tx_qcomplete(txq, evq);
1476 KASSERT(txq->completed == txq->added,
1477 ("txq->completed != txq->added"));
1479 sfxge_tx_qreap(txq);
1480 KASSERT(txq->reaped == txq->completed,
1481 ("txq->reaped != txq->completed"));
1488 /* Destroy the common code transmit queue. */
1489 efx_tx_qdestroy(txq->common);
1492 efx_sram_buf_tbl_clear(sc->enp, txq->buf_base_id,
1493 EFX_TXQ_NBUFS(sc->txq_entries));
1495 SFXGE_EVQ_UNLOCK(evq);
1496 SFXGE_TXQ_UNLOCK(txq);
1500 * Estimate maximum number of Tx descriptors required for TSO packet.
1501 * With minimum MSS and maximum mbuf length we might need more (even
1502 * than a ring-ful of descriptors), but this should not happen in
1503 * practice except due to deliberate attack. In that case we will
1504 * truncate the output at a packet boundary.
1507 sfxge_tx_max_pkt_desc(const struct sfxge_softc *sc, enum sfxge_txq_type type,
1508 unsigned int tso_fw_assisted)
1510 /* One descriptor for every input fragment */
1511 unsigned int max_descs = SFXGE_TX_MAPPING_MAX_SEG;
1512 unsigned int sw_tso_max_descs;
1513 unsigned int fa_tso_v1_max_descs = 0;
1514 unsigned int fa_tso_v2_max_descs = 0;
1516 /* VLAN tagging Tx option descriptor may be required */
1517 if (efx_nic_cfg_get(sc->enp)->enc_hw_tx_insert_vlan_enabled)
1520 if (type == SFXGE_TXQ_IP_TCP_UDP_CKSUM) {
1522 * Plus header and payload descriptor for each output segment.
1523 * Minus one since header fragment is already counted.
1524 * Even if FATSO is used, we should be ready to fallback
1525 * to do it in the driver.
1527 sw_tso_max_descs = SFXGE_TSO_MAX_SEGS * 2 - 1;
1529 /* FW assisted TSOv1 requires one more descriptor per segment
1530 * in comparison to SW TSO */
1531 if (tso_fw_assisted & SFXGE_FATSOV1)
1532 fa_tso_v1_max_descs =
1533 sw_tso_max_descs + SFXGE_TSO_MAX_SEGS;
1535 /* FW assisted TSOv2 requires 3 (2 FATSO plus header) extra
1536 * descriptors per superframe limited by number of DMA fetches
1537 * per packet. The first packet header is already counted.
1539 if (tso_fw_assisted & SFXGE_FATSOV2) {
1540 fa_tso_v2_max_descs =
1541 howmany(SFXGE_TX_MAPPING_MAX_SEG,
1542 EFX_TX_FATSOV2_DMA_SEGS_PER_PKT_MAX - 1) *
1543 (EFX_TX_FATSOV2_OPT_NDESCS + 1) - 1;
1546 max_descs += MAX(sw_tso_max_descs,
1547 MAX(fa_tso_v1_max_descs, fa_tso_v2_max_descs));
1554 sfxge_tx_qstart(struct sfxge_softc *sc, unsigned int index)
1556 struct sfxge_txq *txq;
1559 unsigned int tso_fw_assisted;
1560 struct sfxge_evq *evq;
1561 unsigned int desc_index;
1564 SFXGE_ADAPTER_LOCK_ASSERT_OWNED(sc);
1566 txq = sc->txq[index];
1568 evq = sc->evq[txq->evq_index];
1570 KASSERT(txq->init_state == SFXGE_TXQ_INITIALIZED,
1571 ("txq->init_state != SFXGE_TXQ_INITIALIZED"));
1572 KASSERT(evq->init_state == SFXGE_EVQ_STARTED,
1573 ("evq->init_state != SFXGE_EVQ_STARTED"));
1575 /* Program the buffer table. */
1576 if ((rc = efx_sram_buf_tbl_set(sc->enp, txq->buf_base_id, esmp,
1577 EFX_TXQ_NBUFS(sc->txq_entries))) != 0)
1580 /* Determine the kind of queue we are creating. */
1581 tso_fw_assisted = 0;
1582 switch (txq->type) {
1583 case SFXGE_TXQ_NON_CKSUM:
1586 case SFXGE_TXQ_IP_CKSUM:
1587 flags = EFX_TXQ_CKSUM_IPV4;
1589 case SFXGE_TXQ_IP_TCP_UDP_CKSUM:
1590 flags = EFX_TXQ_CKSUM_IPV4 | EFX_TXQ_CKSUM_TCPUDP;
1591 tso_fw_assisted = sc->tso_fw_assisted;
1592 if (tso_fw_assisted & SFXGE_FATSOV2)
1593 flags |= EFX_TXQ_FATSOV2;
1596 KASSERT(0, ("Impossible TX queue"));
1601 /* Create the common code transmit queue. */
1602 if ((rc = efx_tx_qcreate(sc->enp, index, txq->type, esmp,
1603 sc->txq_entries, txq->buf_base_id, flags, evq->common,
1604 &txq->common, &desc_index)) != 0) {
1605 /* Retry if no FATSOv2 resources, otherwise fail */
1606 if ((rc != ENOSPC) || (~flags & EFX_TXQ_FATSOV2))
1609 /* Looks like all FATSOv2 contexts are used */
1610 flags &= ~EFX_TXQ_FATSOV2;
1611 tso_fw_assisted &= ~SFXGE_FATSOV2;
1612 if ((rc = efx_tx_qcreate(sc->enp, index, txq->type, esmp,
1613 sc->txq_entries, txq->buf_base_id, flags, evq->common,
1614 &txq->common, &desc_index)) != 0)
1618 /* Initialise queue descriptor indexes */
1619 txq->added = txq->pending = txq->completed = txq->reaped = desc_index;
1621 SFXGE_TXQ_LOCK(txq);
1623 /* Enable the transmit queue. */
1624 efx_tx_qenable(txq->common);
1626 txq->init_state = SFXGE_TXQ_STARTED;
1627 txq->flush_state = SFXGE_FLUSH_REQUIRED;
1628 txq->tso_fw_assisted = tso_fw_assisted;
1630 txq->max_pkt_desc = sfxge_tx_max_pkt_desc(sc, txq->type,
1633 txq->hw_vlan_tci = 0;
1635 SFXGE_TXQ_UNLOCK(txq);
1640 efx_sram_buf_tbl_clear(sc->enp, txq->buf_base_id,
1641 EFX_TXQ_NBUFS(sc->txq_entries));
1646 sfxge_tx_stop(struct sfxge_softc *sc)
1650 index = sc->txq_count;
1651 while (--index >= 0)
1652 sfxge_tx_qstop(sc, index);
1654 /* Tear down the transmit module */
1655 efx_tx_fini(sc->enp);
1659 sfxge_tx_start(struct sfxge_softc *sc)
1664 /* Initialize the common code transmit module. */
1665 if ((rc = efx_tx_init(sc->enp)) != 0)
1668 for (index = 0; index < sc->txq_count; index++) {
1669 if ((rc = sfxge_tx_qstart(sc, index)) != 0)
1676 while (--index >= 0)
1677 sfxge_tx_qstop(sc, index);
1679 efx_tx_fini(sc->enp);
1685 sfxge_txq_stat_init(struct sfxge_txq *txq, struct sysctl_oid *txq_node)
1687 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(txq->sc->dev);
1688 struct sysctl_oid *stat_node;
1691 stat_node = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(txq_node), OID_AUTO,
1692 "stats", CTLFLAG_RD, NULL,
1693 "Tx queue statistics");
1694 if (stat_node == NULL)
1697 for (id = 0; id < nitems(sfxge_tx_stats); id++) {
1699 ctx, SYSCTL_CHILDREN(stat_node), OID_AUTO,
1700 sfxge_tx_stats[id].name, CTLFLAG_RD | CTLFLAG_STATS,
1701 (unsigned long *)((caddr_t)txq + sfxge_tx_stats[id].offset),
1709 * Destroy a transmit queue.
1712 sfxge_tx_qfini(struct sfxge_softc *sc, unsigned int index)
1714 struct sfxge_txq *txq;
1717 txq = sc->txq[index];
1719 KASSERT(txq->init_state == SFXGE_TXQ_INITIALIZED,
1720 ("txq->init_state != SFXGE_TXQ_INITIALIZED"));
1722 if (txq->type == SFXGE_TXQ_IP_TCP_UDP_CKSUM)
1725 /* Free the context arrays. */
1726 free(txq->pend_desc, M_SFXGE);
1727 nmaps = sc->txq_entries;
1728 while (nmaps-- != 0)
1729 bus_dmamap_destroy(txq->packet_dma_tag, txq->stmp[nmaps].map);
1730 free(txq->stmp, M_SFXGE);
1732 /* Release DMA memory mapping. */
1733 sfxge_dma_free(&txq->mem);
1735 sc->txq[index] = NULL;
1737 SFXGE_TXQ_LOCK_DESTROY(txq);
1743 sfxge_tx_qinit(struct sfxge_softc *sc, unsigned int txq_index,
1744 enum sfxge_txq_type type, unsigned int evq_index)
1746 const efx_nic_cfg_t *encp = efx_nic_cfg_get(sc->enp);
1748 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev);
1749 struct sysctl_oid *txq_node;
1750 struct sfxge_txq *txq;
1751 struct sfxge_evq *evq;
1752 struct sfxge_tx_dpl *stdp;
1753 struct sysctl_oid *dpl_node;
1758 txq = malloc(sizeof(struct sfxge_txq), M_SFXGE, M_ZERO | M_WAITOK);
1760 txq->entries = sc->txq_entries;
1761 txq->ptr_mask = txq->entries - 1;
1763 sc->txq[txq_index] = txq;
1766 evq = sc->evq[evq_index];
1768 /* Allocate and zero DMA space for the descriptor ring. */
1769 if ((rc = sfxge_dma_alloc(sc, EFX_TXQ_SIZE(sc->txq_entries), esmp)) != 0)
1772 /* Allocate buffer table entries. */
1773 sfxge_sram_buf_tbl_alloc(sc, EFX_TXQ_NBUFS(sc->txq_entries),
1776 /* Create a DMA tag for packet mappings. */
1777 if (bus_dma_tag_create(sc->parent_dma_tag, 1,
1778 encp->enc_tx_dma_desc_boundary,
1779 MIN(0x3FFFFFFFFFFFUL, BUS_SPACE_MAXADDR), BUS_SPACE_MAXADDR, NULL,
1780 NULL, 0x11000, SFXGE_TX_MAPPING_MAX_SEG,
1781 encp->enc_tx_dma_desc_size_max, 0, NULL, NULL,
1782 &txq->packet_dma_tag) != 0) {
1783 device_printf(sc->dev, "Couldn't allocate txq DMA tag\n");
1788 /* Allocate pending descriptor array for batching writes. */
1789 txq->pend_desc = malloc(sizeof(efx_desc_t) * sc->txq_entries,
1790 M_SFXGE, M_ZERO | M_WAITOK);
1792 /* Allocate and initialise mbuf DMA mapping array. */
1793 txq->stmp = malloc(sizeof(struct sfxge_tx_mapping) * sc->txq_entries,
1794 M_SFXGE, M_ZERO | M_WAITOK);
1795 for (nmaps = 0; nmaps < sc->txq_entries; nmaps++) {
1796 rc = bus_dmamap_create(txq->packet_dma_tag, 0,
1797 &txq->stmp[nmaps].map);
1802 snprintf(name, sizeof(name), "%u", txq_index);
1803 txq_node = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(sc->txqs_node),
1804 OID_AUTO, name, CTLFLAG_RD, NULL, "");
1805 if (txq_node == NULL) {
1810 if (type == SFXGE_TXQ_IP_TCP_UDP_CKSUM &&
1811 (rc = tso_init(txq)) != 0)
1814 /* Initialize the deferred packet list. */
1816 stdp->std_put_max = sfxge_tx_dpl_put_max;
1817 stdp->std_get_max = sfxge_tx_dpl_get_max;
1818 stdp->std_get_non_tcp_max = sfxge_tx_dpl_get_non_tcp_max;
1819 stdp->std_getp = &stdp->std_get;
1821 SFXGE_TXQ_LOCK_INIT(txq, device_get_nameunit(sc->dev), txq_index);
1823 dpl_node = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(txq_node), OID_AUTO,
1824 "dpl", CTLFLAG_RD, NULL,
1825 "Deferred packet list statistics");
1826 if (dpl_node == NULL) {
1831 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(dpl_node), OID_AUTO,
1832 "get_count", CTLFLAG_RD | CTLFLAG_STATS,
1833 &stdp->std_get_count, 0, "");
1834 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(dpl_node), OID_AUTO,
1835 "get_non_tcp_count", CTLFLAG_RD | CTLFLAG_STATS,
1836 &stdp->std_get_non_tcp_count, 0, "");
1837 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(dpl_node), OID_AUTO,
1838 "get_hiwat", CTLFLAG_RD | CTLFLAG_STATS,
1839 &stdp->std_get_hiwat, 0, "");
1840 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(dpl_node), OID_AUTO,
1841 "put_hiwat", CTLFLAG_RD | CTLFLAG_STATS,
1842 &stdp->std_put_hiwat, 0, "");
1844 rc = sfxge_txq_stat_init(txq, txq_node);
1846 goto fail_txq_stat_init;
1849 txq->evq_index = evq_index;
1850 txq->init_state = SFXGE_TXQ_INITIALIZED;
1858 free(txq->pend_desc, M_SFXGE);
1860 while (nmaps-- != 0)
1861 bus_dmamap_destroy(txq->packet_dma_tag, txq->stmp[nmaps].map);
1862 free(txq->stmp, M_SFXGE);
1863 bus_dma_tag_destroy(txq->packet_dma_tag);
1866 sfxge_dma_free(esmp);
1872 sfxge_tx_stat_handler(SYSCTL_HANDLER_ARGS)
1874 struct sfxge_softc *sc = arg1;
1875 unsigned int id = arg2;
1879 /* Sum across all TX queues */
1881 for (index = 0; index < sc->txq_count; index++)
1882 sum += *(unsigned long *)((caddr_t)sc->txq[index] +
1883 sfxge_tx_stats[id].offset);
1885 return (SYSCTL_OUT(req, &sum, sizeof(sum)));
1889 sfxge_tx_stat_init(struct sfxge_softc *sc)
1891 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev);
1892 struct sysctl_oid_list *stat_list;
1895 stat_list = SYSCTL_CHILDREN(sc->stats_node);
1897 for (id = 0; id < nitems(sfxge_tx_stats); id++) {
1900 OID_AUTO, sfxge_tx_stats[id].name,
1901 CTLTYPE_ULONG|CTLFLAG_RD,
1902 sc, id, sfxge_tx_stat_handler, "LU",
1908 sfxge_tx_get_drops(struct sfxge_softc *sc)
1912 struct sfxge_txq *txq;
1914 /* Sum across all TX queues */
1915 for (index = 0; index < sc->txq_count; index++) {
1916 txq = sc->txq[index];
1918 * In theory, txq->put_overflow and txq->netdown_drops
1919 * should use atomic operation and other should be
1920 * obtained under txq lock, but it is just statistics.
1922 drops += txq->drops + txq->get_overflow +
1923 txq->get_non_tcp_overflow +
1924 txq->put_overflow + txq->netdown_drops +
1925 txq->tso_pdrop_too_many + txq->tso_pdrop_no_rsrc;
1931 sfxge_tx_fini(struct sfxge_softc *sc)
1935 index = sc->txq_count;
1936 while (--index >= 0)
1937 sfxge_tx_qfini(sc, index);
1944 sfxge_tx_init(struct sfxge_softc *sc)
1946 const efx_nic_cfg_t *encp = efx_nic_cfg_get(sc->enp);
1947 struct sfxge_intr *intr;
1953 KASSERT(intr->state == SFXGE_INTR_INITIALIZED,
1954 ("intr->state != SFXGE_INTR_INITIALIZED"));
1956 if (sfxge_tx_dpl_get_max <= 0) {
1957 log(LOG_ERR, "%s=%d must be greater than 0",
1958 SFXGE_PARAM_TX_DPL_GET_MAX, sfxge_tx_dpl_get_max);
1960 goto fail_tx_dpl_get_max;
1962 if (sfxge_tx_dpl_get_non_tcp_max <= 0) {
1963 log(LOG_ERR, "%s=%d must be greater than 0",
1964 SFXGE_PARAM_TX_DPL_GET_NON_TCP_MAX,
1965 sfxge_tx_dpl_get_non_tcp_max);
1967 goto fail_tx_dpl_get_non_tcp_max;
1969 if (sfxge_tx_dpl_put_max < 0) {
1970 log(LOG_ERR, "%s=%d must be greater or equal to 0",
1971 SFXGE_PARAM_TX_DPL_PUT_MAX, sfxge_tx_dpl_put_max);
1973 goto fail_tx_dpl_put_max;
1976 sc->txq_count = SFXGE_TXQ_NTYPES - 1 + sc->intr.n_alloc;
1978 sc->tso_fw_assisted = sfxge_tso_fw_assisted;
1979 if ((~encp->enc_features & EFX_FEATURE_FW_ASSISTED_TSO) ||
1980 (!encp->enc_fw_assisted_tso_enabled))
1981 sc->tso_fw_assisted &= ~SFXGE_FATSOV1;
1982 if ((~encp->enc_features & EFX_FEATURE_FW_ASSISTED_TSO_V2) ||
1983 (!encp->enc_fw_assisted_tso_v2_enabled))
1984 sc->tso_fw_assisted &= ~SFXGE_FATSOV2;
1986 sc->txqs_node = SYSCTL_ADD_NODE(
1987 device_get_sysctl_ctx(sc->dev),
1988 SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)),
1989 OID_AUTO, "txq", CTLFLAG_RD, NULL, "Tx queues");
1990 if (sc->txqs_node == NULL) {
1995 /* Initialize the transmit queues */
1996 if ((rc = sfxge_tx_qinit(sc, SFXGE_TXQ_NON_CKSUM,
1997 SFXGE_TXQ_NON_CKSUM, 0)) != 0)
2000 if ((rc = sfxge_tx_qinit(sc, SFXGE_TXQ_IP_CKSUM,
2001 SFXGE_TXQ_IP_CKSUM, 0)) != 0)
2005 index < sc->txq_count - SFXGE_TXQ_NTYPES + 1;
2007 if ((rc = sfxge_tx_qinit(sc, SFXGE_TXQ_NTYPES - 1 + index,
2008 SFXGE_TXQ_IP_TCP_UDP_CKSUM, index)) != 0)
2012 sfxge_tx_stat_init(sc);
2017 while (--index >= 0)
2018 sfxge_tx_qfini(sc, SFXGE_TXQ_IP_TCP_UDP_CKSUM + index);
2020 sfxge_tx_qfini(sc, SFXGE_TXQ_IP_CKSUM);
2023 sfxge_tx_qfini(sc, SFXGE_TXQ_NON_CKSUM);
2028 fail_tx_dpl_put_max:
2029 fail_tx_dpl_get_non_tcp_max:
2030 fail_tx_dpl_get_max: