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;
366 KASSERT(!txq->blocked, ("txq->blocked"));
368 #if SFXGE_TX_PARSE_EARLY
370 * If software TSO is used, we still need to copy packet header,
371 * even if we have already parsed it early before enqueue.
373 if ((mbuf->m_pkthdr.csum_flags & CSUM_TSO) &&
374 (txq->tso_fw_assisted == 0))
375 prefetch_read_many(mbuf->m_data);
378 * Prefetch packet header since we need to parse it and extract
379 * IP ID, TCP sequence number and flags.
381 if (mbuf->m_pkthdr.csum_flags & CSUM_TSO)
382 prefetch_read_many(mbuf->m_data);
385 if (__predict_false(txq->init_state != SFXGE_TXQ_STARTED)) {
390 /* Load the packet for DMA. */
391 id = txq->added & txq->ptr_mask;
392 stmp = &txq->stmp[id];
393 rc = bus_dmamap_load_mbuf_sg(txq->packet_dma_tag, stmp->map,
394 mbuf, dma_seg, &n_dma_seg, 0);
397 struct mbuf *new_mbuf = m_collapse(mbuf, M_NOWAIT,
398 SFXGE_TX_MAPPING_MAX_SEG);
399 if (new_mbuf == NULL)
403 rc = bus_dmamap_load_mbuf_sg(txq->packet_dma_tag,
405 dma_seg, &n_dma_seg, 0);
410 /* Make the packet visible to the hardware. */
411 bus_dmamap_sync(txq->packet_dma_tag, stmp->map, BUS_DMASYNC_PREWRITE);
413 used_map = &stmp->map;
415 vlan_tagged = sfxge_tx_maybe_insert_tag(txq, mbuf);
417 sfxge_next_stmp(txq, &stmp);
419 if (mbuf->m_pkthdr.csum_flags & CSUM_TSO) {
420 rc = sfxge_tx_queue_tso(txq, mbuf, dma_seg, n_dma_seg, vlan_tagged);
423 stmp = &txq->stmp[(rc - 1) & txq->ptr_mask];
425 /* Add the mapping to the fragment list, and set flags
431 desc = &txq->pend_desc[i + vlan_tagged];
432 eop = (i == n_dma_seg - 1);
433 efx_tx_qdesc_dma_create(txq->common,
441 sfxge_next_stmp(txq, &stmp);
443 txq->n_pend_desc = n_dma_seg + vlan_tagged;
447 * If the mapping required more than one descriptor
448 * then we need to associate the DMA map with the last
449 * descriptor, not the first.
451 if (used_map != &stmp->map) {
453 stmp->map = *used_map;
458 stmp->flags = TX_BUF_UNMAP | TX_BUF_MBUF;
460 /* Post the fragment list. */
461 sfxge_tx_qlist_post(txq);
466 bus_dmamap_unload(txq->packet_dma_tag, *used_map);
468 /* Drop the packet on the floor. */
476 * Drain the deferred packet list into the transmit queue.
479 sfxge_tx_qdpl_drain(struct sfxge_txq *txq)
481 struct sfxge_softc *sc;
482 struct sfxge_tx_dpl *stdp;
483 struct mbuf *mbuf, *next;
485 unsigned int non_tcp_count;
489 SFXGE_TXQ_LOCK_ASSERT_OWNED(txq);
495 if (__predict_true(txq->init_state == SFXGE_TXQ_STARTED)) {
496 prefetch_read_many(sc->enp);
497 prefetch_read_many(txq->common);
500 mbuf = stdp->std_get;
501 count = stdp->std_get_count;
502 non_tcp_count = stdp->std_get_non_tcp_count;
504 if (count > stdp->std_get_hiwat)
505 stdp->std_get_hiwat = count;
508 KASSERT(mbuf != NULL, ("mbuf == NULL"));
510 next = mbuf->m_nextpkt;
511 mbuf->m_nextpkt = NULL;
513 ETHER_BPF_MTAP(sc->ifnet, mbuf); /* packet capture */
516 prefetch_read_many(next);
518 rc = sfxge_tx_queue_mbuf(txq, mbuf);
520 non_tcp_count -= sfxge_is_mbuf_non_tcp(mbuf);
528 /* Push the fragments to the hardware in batches. */
529 if (txq->added - pushed >= SFXGE_TX_BATCH) {
530 efx_tx_qpush(txq->common, txq->added, pushed);
536 KASSERT(mbuf == NULL, ("mbuf != NULL"));
537 KASSERT(non_tcp_count == 0,
538 ("inconsistent TCP/non-TCP detection"));
539 stdp->std_get = NULL;
540 stdp->std_get_count = 0;
541 stdp->std_get_non_tcp_count = 0;
542 stdp->std_getp = &stdp->std_get;
544 stdp->std_get = mbuf;
545 stdp->std_get_count = count;
546 stdp->std_get_non_tcp_count = non_tcp_count;
549 if (txq->added != pushed)
550 efx_tx_qpush(txq->common, txq->added, pushed);
552 KASSERT(txq->blocked || stdp->std_get_count == 0,
553 ("queue unblocked but count is non-zero"));
556 #define SFXGE_TX_QDPL_PENDING(_txq) ((_txq)->dpl.std_put != 0)
559 * Service the deferred packet list.
561 * NOTE: drops the txq mutex!
564 sfxge_tx_qdpl_service(struct sfxge_txq *txq)
566 SFXGE_TXQ_LOCK_ASSERT_OWNED(txq);
569 if (SFXGE_TX_QDPL_PENDING(txq))
570 sfxge_tx_qdpl_swizzle(txq);
573 sfxge_tx_qdpl_drain(txq);
575 SFXGE_TXQ_UNLOCK(txq);
576 } while (SFXGE_TX_QDPL_PENDING(txq) &&
577 SFXGE_TXQ_TRYLOCK(txq));
581 * Put a packet on the deferred packet get-list.
584 sfxge_tx_qdpl_put_locked(struct sfxge_txq *txq, struct mbuf *mbuf)
586 struct sfxge_tx_dpl *stdp;
590 KASSERT(mbuf->m_nextpkt == NULL, ("mbuf->m_nextpkt != NULL"));
592 SFXGE_TXQ_LOCK_ASSERT_OWNED(txq);
594 if (stdp->std_get_count >= stdp->std_get_max) {
598 if (sfxge_is_mbuf_non_tcp(mbuf)) {
599 if (stdp->std_get_non_tcp_count >=
600 stdp->std_get_non_tcp_max) {
601 txq->get_non_tcp_overflow++;
604 stdp->std_get_non_tcp_count++;
607 *(stdp->std_getp) = mbuf;
608 stdp->std_getp = &mbuf->m_nextpkt;
609 stdp->std_get_count++;
615 * Put a packet on the deferred packet put-list.
617 * We overload the csum_data field in the mbuf to keep track of this length
618 * because there is no cheap alternative to avoid races.
621 sfxge_tx_qdpl_put_unlocked(struct sfxge_txq *txq, struct mbuf *mbuf)
623 struct sfxge_tx_dpl *stdp;
624 volatile uintptr_t *putp;
627 unsigned int put_count;
629 KASSERT(mbuf->m_nextpkt == NULL, ("mbuf->m_nextpkt != NULL"));
631 SFXGE_TXQ_LOCK_ASSERT_NOTOWNED(txq);
634 putp = &stdp->std_put;
635 new = (uintptr_t)mbuf;
640 struct mbuf *mp = (struct mbuf *)old;
641 put_count = mp->m_pkthdr.csum_data;
644 if (put_count >= stdp->std_put_max) {
645 atomic_add_long(&txq->put_overflow, 1);
648 mbuf->m_pkthdr.csum_data = put_count + 1;
649 mbuf->m_nextpkt = (void *)old;
650 } while (atomic_cmpset_ptr(putp, old, new) == 0);
656 * Called from if_transmit - will try to grab the txq lock and enqueue to the
657 * put list if it succeeds, otherwise try to push onto the defer list if space.
660 sfxge_tx_packet_add(struct sfxge_txq *txq, struct mbuf *m)
664 if (!SFXGE_LINK_UP(txq->sc)) {
665 atomic_add_long(&txq->netdown_drops, 1);
670 * Try to grab the txq lock. If we are able to get the lock,
671 * the packet will be appended to the "get list" of the deferred
672 * packet list. Otherwise, it will be pushed on the "put list".
674 if (SFXGE_TXQ_TRYLOCK(txq)) {
675 /* First swizzle put-list to get-list to keep order */
676 sfxge_tx_qdpl_swizzle(txq);
678 rc = sfxge_tx_qdpl_put_locked(txq, m);
680 /* Try to service the list. */
681 sfxge_tx_qdpl_service(txq);
682 /* Lock has been dropped. */
684 rc = sfxge_tx_qdpl_put_unlocked(txq, m);
687 * Try to grab the lock again.
689 * If we are able to get the lock, we need to process
690 * the deferred packet list. If we are not able to get
691 * the lock, another thread is processing the list.
693 if ((rc == 0) && SFXGE_TXQ_TRYLOCK(txq)) {
694 sfxge_tx_qdpl_service(txq);
695 /* Lock has been dropped. */
699 SFXGE_TXQ_LOCK_ASSERT_NOTOWNED(txq);
705 sfxge_tx_qdpl_flush(struct sfxge_txq *txq)
707 struct sfxge_tx_dpl *stdp = &txq->dpl;
708 struct mbuf *mbuf, *next;
712 sfxge_tx_qdpl_swizzle(txq);
713 for (mbuf = stdp->std_get; mbuf != NULL; mbuf = next) {
714 next = mbuf->m_nextpkt;
717 stdp->std_get = NULL;
718 stdp->std_get_count = 0;
719 stdp->std_get_non_tcp_count = 0;
720 stdp->std_getp = &stdp->std_get;
722 SFXGE_TXQ_UNLOCK(txq);
726 sfxge_if_qflush(struct ifnet *ifp)
728 struct sfxge_softc *sc;
733 for (i = 0; i < sc->txq_count; i++)
734 sfxge_tx_qdpl_flush(sc->txq[i]);
737 #if SFXGE_TX_PARSE_EARLY
739 /* There is little space for user data in mbuf pkthdr, so we
740 * use l*hlen fields which are not used by the driver otherwise
741 * to store header offsets.
742 * The fields are 8-bit, but it's ok, no header may be longer than 255 bytes.
746 #define TSO_MBUF_PROTO(_mbuf) ((_mbuf)->m_pkthdr.PH_loc.sixteen[0])
747 /* We abuse l5hlen here because PH_loc can hold only 64 bits of data */
748 #define TSO_MBUF_FLAGS(_mbuf) ((_mbuf)->m_pkthdr.l5hlen)
749 #define TSO_MBUF_PACKETID(_mbuf) ((_mbuf)->m_pkthdr.PH_loc.sixteen[1])
750 #define TSO_MBUF_SEQNUM(_mbuf) ((_mbuf)->m_pkthdr.PH_loc.thirtytwo[1])
752 static void sfxge_parse_tx_packet(struct mbuf *mbuf)
754 struct ether_header *eh = mtod(mbuf, struct ether_header *);
755 const struct tcphdr *th;
756 struct tcphdr th_copy;
758 /* Find network protocol and header */
759 TSO_MBUF_PROTO(mbuf) = eh->ether_type;
760 if (TSO_MBUF_PROTO(mbuf) == htons(ETHERTYPE_VLAN)) {
761 struct ether_vlan_header *veh =
762 mtod(mbuf, struct ether_vlan_header *);
763 TSO_MBUF_PROTO(mbuf) = veh->evl_proto;
764 mbuf->m_pkthdr.l2hlen = sizeof(*veh);
766 mbuf->m_pkthdr.l2hlen = sizeof(*eh);
769 /* Find TCP header */
770 if (TSO_MBUF_PROTO(mbuf) == htons(ETHERTYPE_IP)) {
771 const struct ip *iph = (const struct ip *)mtodo(mbuf, mbuf->m_pkthdr.l2hlen);
773 KASSERT(iph->ip_p == IPPROTO_TCP,
774 ("TSO required on non-TCP packet"));
775 mbuf->m_pkthdr.l3hlen = mbuf->m_pkthdr.l2hlen + 4 * iph->ip_hl;
776 TSO_MBUF_PACKETID(mbuf) = iph->ip_id;
778 KASSERT(TSO_MBUF_PROTO(mbuf) == htons(ETHERTYPE_IPV6),
779 ("TSO required on non-IP packet"));
780 KASSERT(((const struct ip6_hdr *)mtodo(mbuf, mbuf->m_pkthdr.l2hlen))->ip6_nxt ==
782 ("TSO required on non-TCP packet"));
783 mbuf->m_pkthdr.l3hlen = mbuf->m_pkthdr.l2hlen + sizeof(struct ip6_hdr);
784 TSO_MBUF_PACKETID(mbuf) = 0;
787 KASSERT(mbuf->m_len >= mbuf->m_pkthdr.l3hlen,
788 ("network header is fragmented in mbuf"));
790 /* We need TCP header including flags (window is the next) */
791 if (mbuf->m_len < mbuf->m_pkthdr.l3hlen + offsetof(struct tcphdr, th_win)) {
792 m_copydata(mbuf, mbuf->m_pkthdr.l3hlen, sizeof(th_copy),
796 th = (const struct tcphdr *)mtodo(mbuf, mbuf->m_pkthdr.l3hlen);
799 mbuf->m_pkthdr.l4hlen = mbuf->m_pkthdr.l3hlen + 4 * th->th_off;
800 TSO_MBUF_SEQNUM(mbuf) = ntohl(th->th_seq);
802 /* These flags must not be duplicated */
804 * RST should not be duplicated as well, but FreeBSD kernel
805 * generates TSO packets with RST flag. So, do not assert
808 KASSERT(!(th->th_flags & (TH_URG | TH_SYN)),
809 ("incompatible TCP flag 0x%x on TSO packet",
810 th->th_flags & (TH_URG | TH_SYN)));
811 TSO_MBUF_FLAGS(mbuf) = th->th_flags;
816 * TX start -- called by the stack.
819 sfxge_if_transmit(struct ifnet *ifp, struct mbuf *m)
821 struct sfxge_softc *sc;
822 struct sfxge_txq *txq;
825 sc = (struct sfxge_softc *)ifp->if_softc;
828 * Transmit may be called when interface is up from the kernel
829 * point of view, but not yet up (in progress) from the driver
830 * point of view. I.e. link aggregation bring up.
831 * Transmit may be called when interface is up from the driver
832 * point of view, but already down from the kernel point of
833 * view. I.e. Rx when interface shutdown is in progress.
835 KASSERT((ifp->if_flags & IFF_UP) || (sc->if_flags & IFF_UP),
836 ("interface not up"));
838 /* Pick the desired transmit queue. */
839 if (m->m_pkthdr.csum_flags &
840 (CSUM_DELAY_DATA | CSUM_TCP_IPV6 | CSUM_UDP_IPV6 | CSUM_TSO)) {
847 * Select a TX queue which matches the corresponding
848 * RX queue for the hash in order to assign both
849 * TX and RX parts of the flow to the same CPU
851 if (rss_m2bucket(m, &bucket_id) == 0)
852 index = bucket_id % (sc->txq_count - (SFXGE_TXQ_NTYPES - 1));
854 /* check if flowid is set */
855 if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) {
856 uint32_t hash = m->m_pkthdr.flowid;
857 uint32_t idx = hash % nitems(sc->rx_indir_table);
859 index = sc->rx_indir_table[idx];
862 #if SFXGE_TX_PARSE_EARLY
863 if (m->m_pkthdr.csum_flags & CSUM_TSO)
864 sfxge_parse_tx_packet(m);
866 txq = sc->txq[SFXGE_TXQ_IP_TCP_UDP_CKSUM + index];
867 } else if (m->m_pkthdr.csum_flags & CSUM_DELAY_IP) {
868 txq = sc->txq[SFXGE_TXQ_IP_CKSUM];
870 txq = sc->txq[SFXGE_TXQ_NON_CKSUM];
873 rc = sfxge_tx_packet_add(txq, m);
881 * Software "TSO". Not quite as good as doing it in hardware, but
882 * still faster than segmenting in the stack.
885 struct sfxge_tso_state {
886 /* Output position */
887 unsigned out_len; /* Remaining length in current segment */
888 unsigned seqnum; /* Current sequence number */
889 unsigned packet_space; /* Remaining space in current packet */
890 unsigned segs_space; /* Remaining number of DMA segments
891 for the packet (FATSOv2 only) */
894 uint64_t dma_addr; /* DMA address of current position */
895 unsigned in_len; /* Remaining length in current mbuf */
897 const struct mbuf *mbuf; /* Input mbuf (head of chain) */
898 u_short protocol; /* Network protocol (after VLAN decap) */
899 ssize_t nh_off; /* Offset of network header */
900 ssize_t tcph_off; /* Offset of TCP header */
901 unsigned header_len; /* Number of bytes of header */
902 unsigned seg_size; /* TCP segment size */
903 int fw_assisted; /* Use FW-assisted TSO */
904 u_short packet_id; /* IPv4 packet ID from the original packet */
905 uint8_t tcp_flags; /* TCP flags */
906 efx_desc_t header_desc; /* Precomputed header descriptor for
910 #if !SFXGE_TX_PARSE_EARLY
911 static const struct ip *tso_iph(const struct sfxge_tso_state *tso)
913 KASSERT(tso->protocol == htons(ETHERTYPE_IP),
914 ("tso_iph() in non-IPv4 state"));
915 return (const struct ip *)(tso->mbuf->m_data + tso->nh_off);
918 static __unused const struct ip6_hdr *tso_ip6h(const struct sfxge_tso_state *tso)
920 KASSERT(tso->protocol == htons(ETHERTYPE_IPV6),
921 ("tso_ip6h() in non-IPv6 state"));
922 return (const struct ip6_hdr *)(tso->mbuf->m_data + tso->nh_off);
925 static const struct tcphdr *tso_tcph(const struct sfxge_tso_state *tso)
927 return (const struct tcphdr *)(tso->mbuf->m_data + tso->tcph_off);
932 /* Size of preallocated TSO header buffers. Larger blocks must be
933 * allocated from the heap.
935 #define TSOH_STD_SIZE 128
937 /* At most half the descriptors in the queue at any time will refer to
938 * a TSO header buffer, since they must always be followed by a
939 * payload descriptor referring to an mbuf.
941 #define TSOH_COUNT(_txq_entries) ((_txq_entries) / 2u)
942 #define TSOH_PER_PAGE (PAGE_SIZE / TSOH_STD_SIZE)
943 #define TSOH_PAGE_COUNT(_txq_entries) \
944 howmany(TSOH_COUNT(_txq_entries), TSOH_PER_PAGE)
946 static int tso_init(struct sfxge_txq *txq)
948 struct sfxge_softc *sc = txq->sc;
949 unsigned int tsoh_page_count = TSOH_PAGE_COUNT(sc->txq_entries);
952 /* Allocate TSO header buffers */
953 txq->tsoh_buffer = malloc(tsoh_page_count * sizeof(txq->tsoh_buffer[0]),
956 for (i = 0; i < tsoh_page_count; i++) {
957 rc = sfxge_dma_alloc(sc, PAGE_SIZE, &txq->tsoh_buffer[i]);
966 sfxge_dma_free(&txq->tsoh_buffer[i]);
967 free(txq->tsoh_buffer, M_SFXGE);
968 txq->tsoh_buffer = NULL;
972 static void tso_fini(struct sfxge_txq *txq)
976 if (txq->tsoh_buffer != NULL) {
977 for (i = 0; i < TSOH_PAGE_COUNT(txq->sc->txq_entries); i++)
978 sfxge_dma_free(&txq->tsoh_buffer[i]);
979 free(txq->tsoh_buffer, M_SFXGE);
983 static void tso_start(struct sfxge_txq *txq, struct sfxge_tso_state *tso,
984 const bus_dma_segment_t *hdr_dma_seg,
987 const efx_nic_cfg_t *encp = efx_nic_cfg_get(txq->sc->enp);
988 #if !SFXGE_TX_PARSE_EARLY
989 struct ether_header *eh = mtod(mbuf, struct ether_header *);
990 const struct tcphdr *th;
991 struct tcphdr th_copy;
994 tso->fw_assisted = txq->tso_fw_assisted;
997 /* Find network protocol and header */
998 #if !SFXGE_TX_PARSE_EARLY
999 tso->protocol = eh->ether_type;
1000 if (tso->protocol == htons(ETHERTYPE_VLAN)) {
1001 struct ether_vlan_header *veh =
1002 mtod(mbuf, struct ether_vlan_header *);
1003 tso->protocol = veh->evl_proto;
1004 tso->nh_off = sizeof(*veh);
1006 tso->nh_off = sizeof(*eh);
1009 tso->protocol = TSO_MBUF_PROTO(mbuf);
1010 tso->nh_off = mbuf->m_pkthdr.l2hlen;
1011 tso->tcph_off = mbuf->m_pkthdr.l3hlen;
1012 tso->packet_id = ntohs(TSO_MBUF_PACKETID(mbuf));
1015 #if !SFXGE_TX_PARSE_EARLY
1016 /* Find TCP header */
1017 if (tso->protocol == htons(ETHERTYPE_IP)) {
1018 KASSERT(tso_iph(tso)->ip_p == IPPROTO_TCP,
1019 ("TSO required on non-TCP packet"));
1020 tso->tcph_off = tso->nh_off + 4 * tso_iph(tso)->ip_hl;
1021 tso->packet_id = ntohs(tso_iph(tso)->ip_id);
1023 KASSERT(tso->protocol == htons(ETHERTYPE_IPV6),
1024 ("TSO required on non-IP packet"));
1025 KASSERT(tso_ip6h(tso)->ip6_nxt == IPPROTO_TCP,
1026 ("TSO required on non-TCP packet"));
1027 tso->tcph_off = tso->nh_off + sizeof(struct ip6_hdr);
1033 if (tso->fw_assisted &&
1034 __predict_false(tso->tcph_off >
1035 encp->enc_tx_tso_tcp_header_offset_limit)) {
1036 tso->fw_assisted = 0;
1040 #if !SFXGE_TX_PARSE_EARLY
1041 KASSERT(mbuf->m_len >= tso->tcph_off,
1042 ("network header is fragmented in mbuf"));
1043 /* We need TCP header including flags (window is the next) */
1044 if (mbuf->m_len < tso->tcph_off + offsetof(struct tcphdr, th_win)) {
1045 m_copydata(tso->mbuf, tso->tcph_off, sizeof(th_copy),
1051 tso->header_len = tso->tcph_off + 4 * th->th_off;
1053 tso->header_len = mbuf->m_pkthdr.l4hlen;
1055 tso->seg_size = mbuf->m_pkthdr.tso_segsz;
1057 #if !SFXGE_TX_PARSE_EARLY
1058 tso->seqnum = ntohl(th->th_seq);
1060 /* These flags must not be duplicated */
1062 * RST should not be duplicated as well, but FreeBSD kernel
1063 * generates TSO packets with RST flag. So, do not assert
1066 KASSERT(!(th->th_flags & (TH_URG | TH_SYN)),
1067 ("incompatible TCP flag 0x%x on TSO packet",
1068 th->th_flags & (TH_URG | TH_SYN)));
1069 tso->tcp_flags = th->th_flags;
1071 tso->seqnum = TSO_MBUF_SEQNUM(mbuf);
1072 tso->tcp_flags = TSO_MBUF_FLAGS(mbuf);
1075 tso->out_len = mbuf->m_pkthdr.len - tso->header_len;
1077 if (tso->fw_assisted) {
1078 if (hdr_dma_seg->ds_len >= tso->header_len)
1079 efx_tx_qdesc_dma_create(txq->common,
1080 hdr_dma_seg->ds_addr,
1085 tso->fw_assisted = 0;
1090 * tso_fill_packet_with_fragment - form descriptors for the current fragment
1092 * Form descriptors for the current fragment, until we reach the end
1093 * of fragment or end-of-packet. Return 0 on success, 1 if not enough
1096 static void tso_fill_packet_with_fragment(struct sfxge_txq *txq,
1097 struct sfxge_tso_state *tso)
1101 uint64_t dma_addr = tso->dma_addr;
1104 if (tso->in_len == 0 || tso->packet_space == 0)
1107 KASSERT(tso->in_len > 0, ("TSO input length went negative"));
1108 KASSERT(tso->packet_space > 0, ("TSO packet space went negative"));
1110 if (tso->fw_assisted & SFXGE_FATSOV2) {
1115 if (n < tso->packet_space) {
1116 tso->packet_space -= n;
1119 tso->packet_space = tso->seg_size -
1120 (n - tso->packet_space) % tso->seg_size;
1122 EFX_TX_FATSOV2_DMA_SEGS_PER_PKT_MAX - 1 -
1123 (tso->packet_space != tso->seg_size);
1126 n = min(tso->in_len, tso->packet_space);
1127 tso->packet_space -= n;
1134 * It is OK to use binary OR below to avoid extra branching
1135 * since all conditions may always be checked.
1137 eop = (tso->out_len == 0) | (tso->packet_space == 0) |
1138 (tso->segs_space == 0);
1140 desc = &txq->pend_desc[txq->n_pend_desc++];
1141 efx_tx_qdesc_dma_create(txq->common, dma_addr, n, eop, desc);
1144 /* Callback from bus_dmamap_load() for long TSO headers. */
1145 static void tso_map_long_header(void *dma_addr_ret,
1146 bus_dma_segment_t *segs, int nseg,
1149 *(uint64_t *)dma_addr_ret = ((__predict_true(error == 0) &&
1150 __predict_true(nseg == 1)) ?
1155 * tso_start_new_packet - generate a new header and prepare for the new packet
1157 * Generate a new header and prepare for the new packet. Return 0 on
1158 * success, or an error code if failed to alloc header.
1160 static int tso_start_new_packet(struct sfxge_txq *txq,
1161 struct sfxge_tso_state *tso,
1164 unsigned int id = *idp;
1165 struct tcphdr *tsoh_th;
1173 if (tso->fw_assisted) {
1174 if (tso->fw_assisted & SFXGE_FATSOV2) {
1175 /* Add 2 FATSOv2 option descriptors */
1176 desc = &txq->pend_desc[txq->n_pend_desc];
1177 efx_tx_qdesc_tso2_create(txq->common,
1182 EFX_TX_FATSOV2_OPT_NDESCS);
1183 desc += EFX_TX_FATSOV2_OPT_NDESCS;
1184 txq->n_pend_desc += EFX_TX_FATSOV2_OPT_NDESCS;
1185 KASSERT(txq->stmp[id].flags == 0, ("stmp flags are not 0"));
1186 id = (id + EFX_TX_FATSOV2_OPT_NDESCS) & txq->ptr_mask;
1189 EFX_TX_FATSOV2_DMA_SEGS_PER_PKT_MAX - 1;
1191 uint8_t tcp_flags = tso->tcp_flags;
1193 if (tso->out_len > tso->seg_size)
1194 tcp_flags &= ~(TH_FIN | TH_PUSH);
1196 /* Add FATSOv1 option descriptor */
1197 desc = &txq->pend_desc[txq->n_pend_desc++];
1198 efx_tx_qdesc_tso_create(txq->common,
1203 KASSERT(txq->stmp[id].flags == 0, ("stmp flags are not 0"));
1204 id = (id + 1) & txq->ptr_mask;
1206 tso->seqnum += tso->seg_size;
1207 tso->segs_space = UINT_MAX;
1210 /* Header DMA descriptor */
1211 *desc = tso->header_desc;
1213 KASSERT(txq->stmp[id].flags == 0, ("stmp flags are not 0"));
1214 id = (id + 1) & txq->ptr_mask;
1216 /* Allocate a DMA-mapped header buffer. */
1217 if (__predict_true(tso->header_len <= TSOH_STD_SIZE)) {
1218 unsigned int page_index = (id / 2) / TSOH_PER_PAGE;
1219 unsigned int buf_index = (id / 2) % TSOH_PER_PAGE;
1221 header = (txq->tsoh_buffer[page_index].esm_base +
1222 buf_index * TSOH_STD_SIZE);
1223 dma_addr = (txq->tsoh_buffer[page_index].esm_addr +
1224 buf_index * TSOH_STD_SIZE);
1225 map = txq->tsoh_buffer[page_index].esm_map;
1227 KASSERT(txq->stmp[id].flags == 0,
1228 ("stmp flags are not 0"));
1230 struct sfxge_tx_mapping *stmp = &txq->stmp[id];
1232 /* We cannot use bus_dmamem_alloc() as that may sleep */
1233 header = malloc(tso->header_len, M_SFXGE, M_NOWAIT);
1234 if (__predict_false(!header))
1236 rc = bus_dmamap_load(txq->packet_dma_tag, stmp->map,
1237 header, tso->header_len,
1238 tso_map_long_header, &dma_addr,
1240 if (__predict_false(dma_addr == 0)) {
1242 /* Succeeded but got >1 segment */
1243 bus_dmamap_unload(txq->packet_dma_tag,
1247 free(header, M_SFXGE);
1252 txq->tso_long_headers++;
1253 stmp->u.heap_buf = header;
1254 stmp->flags = TX_BUF_UNMAP;
1257 tsoh_th = (struct tcphdr *)(header + tso->tcph_off);
1259 /* Copy and update the headers. */
1260 m_copydata(tso->mbuf, 0, tso->header_len, header);
1262 tsoh_th->th_seq = htonl(tso->seqnum);
1263 tso->seqnum += tso->seg_size;
1264 if (tso->out_len > tso->seg_size) {
1265 /* This packet will not finish the TSO burst. */
1266 ip_length = tso->header_len - tso->nh_off + tso->seg_size;
1267 tsoh_th->th_flags &= ~(TH_FIN | TH_PUSH);
1269 /* This packet will be the last in the TSO burst. */
1270 ip_length = tso->header_len - tso->nh_off + tso->out_len;
1273 if (tso->protocol == htons(ETHERTYPE_IP)) {
1274 struct ip *tsoh_iph = (struct ip *)(header + tso->nh_off);
1275 tsoh_iph->ip_len = htons(ip_length);
1276 /* XXX We should increment ip_id, but FreeBSD doesn't
1277 * currently allocate extra IDs for multiple segments.
1280 struct ip6_hdr *tsoh_iph =
1281 (struct ip6_hdr *)(header + tso->nh_off);
1282 tsoh_iph->ip6_plen = htons(ip_length - sizeof(*tsoh_iph));
1285 /* Make the header visible to the hardware. */
1286 bus_dmamap_sync(txq->packet_dma_tag, map, BUS_DMASYNC_PREWRITE);
1288 /* Form a descriptor for this header. */
1289 desc = &txq->pend_desc[txq->n_pend_desc++];
1290 efx_tx_qdesc_dma_create(txq->common,
1295 id = (id + 1) & txq->ptr_mask;
1297 tso->segs_space = UINT_MAX;
1299 tso->packet_space = tso->seg_size;
1307 sfxge_tx_queue_tso(struct sfxge_txq *txq, struct mbuf *mbuf,
1308 const bus_dma_segment_t *dma_seg, int n_dma_seg,
1311 struct sfxge_tso_state tso;
1313 unsigned skipped = 0;
1315 tso_start(txq, &tso, dma_seg, mbuf);
1317 while (dma_seg->ds_len + skipped <= tso.header_len) {
1318 skipped += dma_seg->ds_len;
1320 KASSERT(n_dma_seg, ("no payload found in TSO packet"));
1323 tso.in_len = dma_seg->ds_len - (tso.header_len - skipped);
1324 tso.dma_addr = dma_seg->ds_addr + (tso.header_len - skipped);
1326 id = (txq->added + vlan_tagged) & txq->ptr_mask;
1327 if (__predict_false(tso_start_new_packet(txq, &tso, &id)))
1331 tso_fill_packet_with_fragment(txq, &tso);
1332 /* Exactly one DMA descriptor is added */
1333 KASSERT(txq->stmp[id].flags == 0, ("stmp flags are not 0"));
1334 id = (id + 1) & txq->ptr_mask;
1336 /* Move onto the next fragment? */
1337 if (tso.in_len == 0) {
1342 tso.in_len = dma_seg->ds_len;
1343 tso.dma_addr = dma_seg->ds_addr;
1346 /* End of packet? */
1347 if ((tso.packet_space == 0) | (tso.segs_space == 0)) {
1348 unsigned int n_fatso_opt_desc =
1349 (tso.fw_assisted & SFXGE_FATSOV2) ?
1350 EFX_TX_FATSOV2_OPT_NDESCS :
1351 (tso.fw_assisted & SFXGE_FATSOV1) ? 1 : 0;
1353 /* If the queue is now full due to tiny MSS,
1354 * or we can't create another header, discard
1355 * the remainder of the input mbuf but do not
1356 * roll back the work we have done.
1358 if (txq->n_pend_desc + n_fatso_opt_desc +
1359 1 /* header */ + n_dma_seg > txq->max_pkt_desc) {
1360 txq->tso_pdrop_too_many++;
1363 if (__predict_false(tso_start_new_packet(txq, &tso,
1365 txq->tso_pdrop_no_rsrc++;
1376 sfxge_tx_qunblock(struct sfxge_txq *txq)
1378 struct sfxge_softc *sc;
1379 struct sfxge_evq *evq;
1382 evq = sc->evq[txq->evq_index];
1384 SFXGE_EVQ_LOCK_ASSERT_OWNED(evq);
1386 if (__predict_false(txq->init_state != SFXGE_TXQ_STARTED))
1389 SFXGE_TXQ_LOCK(txq);
1394 level = txq->added - txq->completed;
1395 if (level <= SFXGE_TXQ_UNBLOCK_LEVEL(txq->entries)) {
1396 /* reaped must be in sync with blocked */
1397 sfxge_tx_qreap(txq);
1402 sfxge_tx_qdpl_service(txq);
1403 /* note: lock has been dropped */
1407 sfxge_tx_qflush_done(struct sfxge_txq *txq)
1410 txq->flush_state = SFXGE_FLUSH_DONE;
1414 sfxge_tx_qstop(struct sfxge_softc *sc, unsigned int index)
1416 struct sfxge_txq *txq;
1417 struct sfxge_evq *evq;
1420 SFXGE_ADAPTER_LOCK_ASSERT_OWNED(sc);
1422 txq = sc->txq[index];
1423 evq = sc->evq[txq->evq_index];
1425 SFXGE_EVQ_LOCK(evq);
1426 SFXGE_TXQ_LOCK(txq);
1428 KASSERT(txq->init_state == SFXGE_TXQ_STARTED,
1429 ("txq->init_state != SFXGE_TXQ_STARTED"));
1431 txq->init_state = SFXGE_TXQ_INITIALIZED;
1433 if (txq->flush_state != SFXGE_FLUSH_DONE) {
1434 txq->flush_state = SFXGE_FLUSH_PENDING;
1436 SFXGE_EVQ_UNLOCK(evq);
1437 SFXGE_TXQ_UNLOCK(txq);
1439 /* Flush the transmit queue. */
1440 if (efx_tx_qflush(txq->common) != 0) {
1441 log(LOG_ERR, "%s: Flushing Tx queue %u failed\n",
1442 device_get_nameunit(sc->dev), index);
1443 txq->flush_state = SFXGE_FLUSH_DONE;
1447 /* Spin for 100ms. */
1449 if (txq->flush_state != SFXGE_FLUSH_PENDING)
1451 } while (++count < 20);
1453 SFXGE_EVQ_LOCK(evq);
1454 SFXGE_TXQ_LOCK(txq);
1456 KASSERT(txq->flush_state != SFXGE_FLUSH_FAILED,
1457 ("txq->flush_state == SFXGE_FLUSH_FAILED"));
1459 if (txq->flush_state != SFXGE_FLUSH_DONE) {
1461 log(LOG_ERR, "%s: Cannot flush Tx queue %u\n",
1462 device_get_nameunit(sc->dev), index);
1463 txq->flush_state = SFXGE_FLUSH_DONE;
1468 txq->pending = txq->added;
1470 sfxge_tx_qcomplete(txq, evq);
1471 KASSERT(txq->completed == txq->added,
1472 ("txq->completed != txq->added"));
1474 sfxge_tx_qreap(txq);
1475 KASSERT(txq->reaped == txq->completed,
1476 ("txq->reaped != txq->completed"));
1483 /* Destroy the common code transmit queue. */
1484 efx_tx_qdestroy(txq->common);
1487 efx_sram_buf_tbl_clear(sc->enp, txq->buf_base_id,
1488 EFX_TXQ_NBUFS(sc->txq_entries));
1490 SFXGE_EVQ_UNLOCK(evq);
1491 SFXGE_TXQ_UNLOCK(txq);
1495 * Estimate maximum number of Tx descriptors required for TSO packet.
1496 * With minimum MSS and maximum mbuf length we might need more (even
1497 * than a ring-ful of descriptors), but this should not happen in
1498 * practice except due to deliberate attack. In that case we will
1499 * truncate the output at a packet boundary.
1502 sfxge_tx_max_pkt_desc(const struct sfxge_softc *sc, enum sfxge_txq_type type,
1503 unsigned int tso_fw_assisted)
1505 /* One descriptor for every input fragment */
1506 unsigned int max_descs = SFXGE_TX_MAPPING_MAX_SEG;
1507 unsigned int sw_tso_max_descs;
1508 unsigned int fa_tso_v1_max_descs = 0;
1509 unsigned int fa_tso_v2_max_descs = 0;
1511 /* VLAN tagging Tx option descriptor may be required */
1512 if (efx_nic_cfg_get(sc->enp)->enc_hw_tx_insert_vlan_enabled)
1515 if (type == SFXGE_TXQ_IP_TCP_UDP_CKSUM) {
1517 * Plus header and payload descriptor for each output segment.
1518 * Minus one since header fragment is already counted.
1519 * Even if FATSO is used, we should be ready to fallback
1520 * to do it in the driver.
1522 sw_tso_max_descs = SFXGE_TSO_MAX_SEGS * 2 - 1;
1524 /* FW assisted TSOv1 requires one more descriptor per segment
1525 * in comparison to SW TSO */
1526 if (tso_fw_assisted & SFXGE_FATSOV1)
1527 fa_tso_v1_max_descs =
1528 sw_tso_max_descs + SFXGE_TSO_MAX_SEGS;
1530 /* FW assisted TSOv2 requires 3 (2 FATSO plus header) extra
1531 * descriptors per superframe limited by number of DMA fetches
1532 * per packet. The first packet header is already counted.
1534 if (tso_fw_assisted & SFXGE_FATSOV2) {
1535 fa_tso_v2_max_descs =
1536 howmany(SFXGE_TX_MAPPING_MAX_SEG,
1537 EFX_TX_FATSOV2_DMA_SEGS_PER_PKT_MAX - 1) *
1538 (EFX_TX_FATSOV2_OPT_NDESCS + 1) - 1;
1541 max_descs += MAX(sw_tso_max_descs,
1542 MAX(fa_tso_v1_max_descs, fa_tso_v2_max_descs));
1549 sfxge_tx_qstart(struct sfxge_softc *sc, unsigned int index)
1551 struct sfxge_txq *txq;
1554 unsigned int tso_fw_assisted;
1555 struct sfxge_evq *evq;
1556 unsigned int desc_index;
1559 SFXGE_ADAPTER_LOCK_ASSERT_OWNED(sc);
1561 txq = sc->txq[index];
1563 evq = sc->evq[txq->evq_index];
1565 KASSERT(txq->init_state == SFXGE_TXQ_INITIALIZED,
1566 ("txq->init_state != SFXGE_TXQ_INITIALIZED"));
1567 KASSERT(evq->init_state == SFXGE_EVQ_STARTED,
1568 ("evq->init_state != SFXGE_EVQ_STARTED"));
1570 /* Program the buffer table. */
1571 if ((rc = efx_sram_buf_tbl_set(sc->enp, txq->buf_base_id, esmp,
1572 EFX_TXQ_NBUFS(sc->txq_entries))) != 0)
1575 /* Determine the kind of queue we are creating. */
1576 tso_fw_assisted = 0;
1577 switch (txq->type) {
1578 case SFXGE_TXQ_NON_CKSUM:
1581 case SFXGE_TXQ_IP_CKSUM:
1582 flags = EFX_TXQ_CKSUM_IPV4;
1584 case SFXGE_TXQ_IP_TCP_UDP_CKSUM:
1585 flags = EFX_TXQ_CKSUM_IPV4 | EFX_TXQ_CKSUM_TCPUDP;
1586 tso_fw_assisted = sc->tso_fw_assisted;
1587 if (tso_fw_assisted & SFXGE_FATSOV2)
1588 flags |= EFX_TXQ_FATSOV2;
1591 KASSERT(0, ("Impossible TX queue"));
1596 /* Create the common code transmit queue. */
1597 if ((rc = efx_tx_qcreate(sc->enp, index, txq->type, esmp,
1598 sc->txq_entries, txq->buf_base_id, flags, evq->common,
1599 &txq->common, &desc_index)) != 0) {
1600 /* Retry if no FATSOv2 resources, otherwise fail */
1601 if ((rc != ENOSPC) || (~flags & EFX_TXQ_FATSOV2))
1604 /* Looks like all FATSOv2 contexts are used */
1605 flags &= ~EFX_TXQ_FATSOV2;
1606 tso_fw_assisted &= ~SFXGE_FATSOV2;
1607 if ((rc = efx_tx_qcreate(sc->enp, index, txq->type, esmp,
1608 sc->txq_entries, txq->buf_base_id, flags, evq->common,
1609 &txq->common, &desc_index)) != 0)
1613 /* Initialise queue descriptor indexes */
1614 txq->added = txq->pending = txq->completed = txq->reaped = desc_index;
1616 SFXGE_TXQ_LOCK(txq);
1618 /* Enable the transmit queue. */
1619 efx_tx_qenable(txq->common);
1621 txq->init_state = SFXGE_TXQ_STARTED;
1622 txq->flush_state = SFXGE_FLUSH_REQUIRED;
1623 txq->tso_fw_assisted = tso_fw_assisted;
1625 txq->max_pkt_desc = sfxge_tx_max_pkt_desc(sc, txq->type,
1628 txq->hw_vlan_tci = 0;
1630 SFXGE_TXQ_UNLOCK(txq);
1635 efx_sram_buf_tbl_clear(sc->enp, txq->buf_base_id,
1636 EFX_TXQ_NBUFS(sc->txq_entries));
1641 sfxge_tx_stop(struct sfxge_softc *sc)
1645 index = sc->txq_count;
1646 while (--index >= 0)
1647 sfxge_tx_qstop(sc, index);
1649 /* Tear down the transmit module */
1650 efx_tx_fini(sc->enp);
1654 sfxge_tx_start(struct sfxge_softc *sc)
1659 /* Initialize the common code transmit module. */
1660 if ((rc = efx_tx_init(sc->enp)) != 0)
1663 for (index = 0; index < sc->txq_count; index++) {
1664 if ((rc = sfxge_tx_qstart(sc, index)) != 0)
1671 while (--index >= 0)
1672 sfxge_tx_qstop(sc, index);
1674 efx_tx_fini(sc->enp);
1680 sfxge_txq_stat_init(struct sfxge_txq *txq, struct sysctl_oid *txq_node)
1682 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(txq->sc->dev);
1683 struct sysctl_oid *stat_node;
1686 stat_node = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(txq_node), OID_AUTO,
1687 "stats", CTLFLAG_RD, NULL,
1688 "Tx queue statistics");
1689 if (stat_node == NULL)
1692 for (id = 0; id < nitems(sfxge_tx_stats); id++) {
1694 ctx, SYSCTL_CHILDREN(stat_node), OID_AUTO,
1695 sfxge_tx_stats[id].name, CTLFLAG_RD | CTLFLAG_STATS,
1696 (unsigned long *)((caddr_t)txq + sfxge_tx_stats[id].offset),
1704 * Destroy a transmit queue.
1707 sfxge_tx_qfini(struct sfxge_softc *sc, unsigned int index)
1709 struct sfxge_txq *txq;
1712 txq = sc->txq[index];
1714 KASSERT(txq->init_state == SFXGE_TXQ_INITIALIZED,
1715 ("txq->init_state != SFXGE_TXQ_INITIALIZED"));
1717 if (txq->type == SFXGE_TXQ_IP_TCP_UDP_CKSUM)
1720 /* Free the context arrays. */
1721 free(txq->pend_desc, M_SFXGE);
1722 nmaps = sc->txq_entries;
1723 while (nmaps-- != 0)
1724 bus_dmamap_destroy(txq->packet_dma_tag, txq->stmp[nmaps].map);
1725 free(txq->stmp, M_SFXGE);
1727 /* Release DMA memory mapping. */
1728 sfxge_dma_free(&txq->mem);
1730 sc->txq[index] = NULL;
1732 SFXGE_TXQ_LOCK_DESTROY(txq);
1738 sfxge_tx_qinit(struct sfxge_softc *sc, unsigned int txq_index,
1739 enum sfxge_txq_type type, unsigned int evq_index)
1741 const efx_nic_cfg_t *encp = efx_nic_cfg_get(sc->enp);
1743 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev);
1744 struct sysctl_oid *txq_node;
1745 struct sfxge_txq *txq;
1746 struct sfxge_evq *evq;
1747 struct sfxge_tx_dpl *stdp;
1748 struct sysctl_oid *dpl_node;
1753 txq = malloc(sizeof(struct sfxge_txq), M_SFXGE, M_ZERO | M_WAITOK);
1755 txq->entries = sc->txq_entries;
1756 txq->ptr_mask = txq->entries - 1;
1758 sc->txq[txq_index] = txq;
1761 evq = sc->evq[evq_index];
1763 /* Allocate and zero DMA space for the descriptor ring. */
1764 if ((rc = sfxge_dma_alloc(sc, EFX_TXQ_SIZE(sc->txq_entries), esmp)) != 0)
1767 /* Allocate buffer table entries. */
1768 sfxge_sram_buf_tbl_alloc(sc, EFX_TXQ_NBUFS(sc->txq_entries),
1771 /* Create a DMA tag for packet mappings. */
1772 if (bus_dma_tag_create(sc->parent_dma_tag, 1,
1773 encp->enc_tx_dma_desc_boundary,
1774 MIN(0x3FFFFFFFFFFFUL, BUS_SPACE_MAXADDR), BUS_SPACE_MAXADDR, NULL,
1775 NULL, 0x11000, SFXGE_TX_MAPPING_MAX_SEG,
1776 encp->enc_tx_dma_desc_size_max, 0, NULL, NULL,
1777 &txq->packet_dma_tag) != 0) {
1778 device_printf(sc->dev, "Couldn't allocate txq DMA tag\n");
1783 /* Allocate pending descriptor array for batching writes. */
1784 txq->pend_desc = malloc(sizeof(efx_desc_t) * sc->txq_entries,
1785 M_SFXGE, M_ZERO | M_WAITOK);
1787 /* Allocate and initialise mbuf DMA mapping array. */
1788 txq->stmp = malloc(sizeof(struct sfxge_tx_mapping) * sc->txq_entries,
1789 M_SFXGE, M_ZERO | M_WAITOK);
1790 for (nmaps = 0; nmaps < sc->txq_entries; nmaps++) {
1791 rc = bus_dmamap_create(txq->packet_dma_tag, 0,
1792 &txq->stmp[nmaps].map);
1797 snprintf(name, sizeof(name), "%u", txq_index);
1798 txq_node = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(sc->txqs_node),
1799 OID_AUTO, name, CTLFLAG_RD, NULL, "");
1800 if (txq_node == NULL) {
1805 if (type == SFXGE_TXQ_IP_TCP_UDP_CKSUM &&
1806 (rc = tso_init(txq)) != 0)
1809 /* Initialize the deferred packet list. */
1811 stdp->std_put_max = sfxge_tx_dpl_put_max;
1812 stdp->std_get_max = sfxge_tx_dpl_get_max;
1813 stdp->std_get_non_tcp_max = sfxge_tx_dpl_get_non_tcp_max;
1814 stdp->std_getp = &stdp->std_get;
1816 SFXGE_TXQ_LOCK_INIT(txq, device_get_nameunit(sc->dev), txq_index);
1818 dpl_node = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(txq_node), OID_AUTO,
1819 "dpl", CTLFLAG_RD, NULL,
1820 "Deferred packet list statistics");
1821 if (dpl_node == NULL) {
1826 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(dpl_node), OID_AUTO,
1827 "get_count", CTLFLAG_RD | CTLFLAG_STATS,
1828 &stdp->std_get_count, 0, "");
1829 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(dpl_node), OID_AUTO,
1830 "get_non_tcp_count", CTLFLAG_RD | CTLFLAG_STATS,
1831 &stdp->std_get_non_tcp_count, 0, "");
1832 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(dpl_node), OID_AUTO,
1833 "get_hiwat", CTLFLAG_RD | CTLFLAG_STATS,
1834 &stdp->std_get_hiwat, 0, "");
1835 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(dpl_node), OID_AUTO,
1836 "put_hiwat", CTLFLAG_RD | CTLFLAG_STATS,
1837 &stdp->std_put_hiwat, 0, "");
1839 rc = sfxge_txq_stat_init(txq, txq_node);
1841 goto fail_txq_stat_init;
1844 txq->evq_index = evq_index;
1845 txq->init_state = SFXGE_TXQ_INITIALIZED;
1853 free(txq->pend_desc, M_SFXGE);
1855 while (nmaps-- != 0)
1856 bus_dmamap_destroy(txq->packet_dma_tag, txq->stmp[nmaps].map);
1857 free(txq->stmp, M_SFXGE);
1858 bus_dma_tag_destroy(txq->packet_dma_tag);
1861 sfxge_dma_free(esmp);
1867 sfxge_tx_stat_handler(SYSCTL_HANDLER_ARGS)
1869 struct sfxge_softc *sc = arg1;
1870 unsigned int id = arg2;
1874 /* Sum across all TX queues */
1876 for (index = 0; index < sc->txq_count; index++)
1877 sum += *(unsigned long *)((caddr_t)sc->txq[index] +
1878 sfxge_tx_stats[id].offset);
1880 return (SYSCTL_OUT(req, &sum, sizeof(sum)));
1884 sfxge_tx_stat_init(struct sfxge_softc *sc)
1886 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev);
1887 struct sysctl_oid_list *stat_list;
1890 stat_list = SYSCTL_CHILDREN(sc->stats_node);
1892 for (id = 0; id < nitems(sfxge_tx_stats); id++) {
1895 OID_AUTO, sfxge_tx_stats[id].name,
1896 CTLTYPE_ULONG|CTLFLAG_RD,
1897 sc, id, sfxge_tx_stat_handler, "LU",
1903 sfxge_tx_get_drops(struct sfxge_softc *sc)
1907 struct sfxge_txq *txq;
1909 /* Sum across all TX queues */
1910 for (index = 0; index < sc->txq_count; index++) {
1911 txq = sc->txq[index];
1913 * In theory, txq->put_overflow and txq->netdown_drops
1914 * should use atomic operation and other should be
1915 * obtained under txq lock, but it is just statistics.
1917 drops += txq->drops + txq->get_overflow +
1918 txq->get_non_tcp_overflow +
1919 txq->put_overflow + txq->netdown_drops +
1920 txq->tso_pdrop_too_many + txq->tso_pdrop_no_rsrc;
1926 sfxge_tx_fini(struct sfxge_softc *sc)
1930 index = sc->txq_count;
1931 while (--index >= 0)
1932 sfxge_tx_qfini(sc, index);
1939 sfxge_tx_init(struct sfxge_softc *sc)
1941 const efx_nic_cfg_t *encp = efx_nic_cfg_get(sc->enp);
1942 struct sfxge_intr *intr;
1948 KASSERT(intr->state == SFXGE_INTR_INITIALIZED,
1949 ("intr->state != SFXGE_INTR_INITIALIZED"));
1951 if (sfxge_tx_dpl_get_max <= 0) {
1952 log(LOG_ERR, "%s=%d must be greater than 0",
1953 SFXGE_PARAM_TX_DPL_GET_MAX, sfxge_tx_dpl_get_max);
1955 goto fail_tx_dpl_get_max;
1957 if (sfxge_tx_dpl_get_non_tcp_max <= 0) {
1958 log(LOG_ERR, "%s=%d must be greater than 0",
1959 SFXGE_PARAM_TX_DPL_GET_NON_TCP_MAX,
1960 sfxge_tx_dpl_get_non_tcp_max);
1962 goto fail_tx_dpl_get_non_tcp_max;
1964 if (sfxge_tx_dpl_put_max < 0) {
1965 log(LOG_ERR, "%s=%d must be greater or equal to 0",
1966 SFXGE_PARAM_TX_DPL_PUT_MAX, sfxge_tx_dpl_put_max);
1968 goto fail_tx_dpl_put_max;
1971 sc->txq_count = SFXGE_TXQ_NTYPES - 1 + sc->intr.n_alloc;
1973 sc->tso_fw_assisted = sfxge_tso_fw_assisted;
1974 if ((~encp->enc_features & EFX_FEATURE_FW_ASSISTED_TSO) ||
1975 (!encp->enc_fw_assisted_tso_enabled))
1976 sc->tso_fw_assisted &= ~SFXGE_FATSOV1;
1977 if ((~encp->enc_features & EFX_FEATURE_FW_ASSISTED_TSO_V2) ||
1978 (!encp->enc_fw_assisted_tso_v2_enabled))
1979 sc->tso_fw_assisted &= ~SFXGE_FATSOV2;
1981 sc->txqs_node = SYSCTL_ADD_NODE(
1982 device_get_sysctl_ctx(sc->dev),
1983 SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)),
1984 OID_AUTO, "txq", CTLFLAG_RD, NULL, "Tx queues");
1985 if (sc->txqs_node == NULL) {
1990 /* Initialize the transmit queues */
1991 if ((rc = sfxge_tx_qinit(sc, SFXGE_TXQ_NON_CKSUM,
1992 SFXGE_TXQ_NON_CKSUM, 0)) != 0)
1995 if ((rc = sfxge_tx_qinit(sc, SFXGE_TXQ_IP_CKSUM,
1996 SFXGE_TXQ_IP_CKSUM, 0)) != 0)
2000 index < sc->txq_count - SFXGE_TXQ_NTYPES + 1;
2002 if ((rc = sfxge_tx_qinit(sc, SFXGE_TXQ_NTYPES - 1 + index,
2003 SFXGE_TXQ_IP_TCP_UDP_CKSUM, index)) != 0)
2007 sfxge_tx_stat_init(sc);
2012 while (--index >= 0)
2013 sfxge_tx_qfini(sc, SFXGE_TXQ_IP_TCP_UDP_CKSUM + index);
2015 sfxge_tx_qfini(sc, SFXGE_TXQ_IP_CKSUM);
2018 sfxge_tx_qfini(sc, SFXGE_TXQ_NON_CKSUM);
2023 fail_tx_dpl_put_max:
2024 fail_tx_dpl_get_non_tcp_max:
2025 fail_tx_dpl_get_max: