2 * Copyright (c) 2010-2011 Solarflare Communications, Inc.
5 * This software was developed in part by Philip Paeps under contract for
6 * Solarflare Communications, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 /* Theory of operation:
32 * Tx queues allocation and mapping
34 * One Tx queue with enabled checksum offload is allocated per Rx channel
35 * (event queue). Also 2 Tx queues (one without checksum offload and one
36 * with IP checksum offload only) are allocated and bound to event queue 0.
37 * sfxge_txq_type is used as Tx queue label.
39 * So, event queue plus label mapping to Tx queue index is:
40 * if event queue index is 0, TxQ-index = TxQ-label * [0..SFXGE_TXQ_NTYPES)
41 * else TxQ-index = SFXGE_TXQ_NTYPES + EvQ-index - 1
42 * See sfxge_get_txq_by_label() sfxge_ev.c
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
48 #include <sys/types.h>
51 #include <sys/socket.h>
52 #include <sys/sysctl.h>
55 #include <net/ethernet.h>
57 #include <net/if_vlan_var.h>
59 #include <netinet/in.h>
60 #include <netinet/ip.h>
61 #include <netinet/ip6.h>
62 #include <netinet/tcp.h>
64 #include "common/efx.h"
69 /* Set the block level to ensure there is space to generate a
70 * large number of descriptors for TSO. With minimum MSS and
71 * maximum mbuf length we might need more than a ring-ful of
72 * descriptors, but this should not happen in practice except
73 * due to deliberate attack. In that case we will truncate
74 * the output at a packet boundary. Allow for a reasonable
77 #define SFXGE_TSO_MAX_DESC ((65535 / 512) * 2 + SFXGE_TX_MAPPING_MAX_SEG - 1)
78 #define SFXGE_TXQ_BLOCK_LEVEL (SFXGE_NDESCS - SFXGE_TSO_MAX_DESC)
80 /* Forward declarations. */
81 static inline void sfxge_tx_qdpl_service(struct sfxge_txq *txq);
82 static void sfxge_tx_qlist_post(struct sfxge_txq *txq);
83 static void sfxge_tx_qunblock(struct sfxge_txq *txq);
84 static int sfxge_tx_queue_tso(struct sfxge_txq *txq, struct mbuf *mbuf,
85 const bus_dma_segment_t *dma_seg, int n_dma_seg);
88 sfxge_tx_qcomplete(struct sfxge_txq *txq)
90 struct sfxge_softc *sc;
91 struct sfxge_evq *evq;
92 unsigned int completed;
95 evq = sc->evq[txq->evq_index];
97 mtx_assert(&evq->lock, MA_OWNED);
99 completed = txq->completed;
100 while (completed != txq->pending) {
101 struct sfxge_tx_mapping *stmp;
104 id = completed++ & (SFXGE_NDESCS - 1);
106 stmp = &txq->stmp[id];
107 if (stmp->flags & TX_BUF_UNMAP) {
108 bus_dmamap_unload(txq->packet_dma_tag, stmp->map);
109 if (stmp->flags & TX_BUF_MBUF) {
110 struct mbuf *m = stmp->u.mbuf;
115 free(stmp->u.heap_buf, M_SFXGE);
120 txq->completed = completed;
122 /* Check whether we need to unblock the queue. */
127 level = txq->added - txq->completed;
128 if (level <= SFXGE_TXQ_UNBLOCK_LEVEL)
129 sfxge_tx_qunblock(txq);
136 * Reorder the put list and append it to the get list.
139 sfxge_tx_qdpl_swizzle(struct sfxge_txq *txq)
141 struct sfxge_tx_dpl *stdp;
142 struct mbuf *mbuf, *get_next, **get_tailp;
143 volatile uintptr_t *putp;
147 mtx_assert(&txq->lock, MA_OWNED);
151 /* Acquire the put list. */
152 putp = &stdp->std_put;
153 put = atomic_readandclear_ptr(putp);
159 /* Reverse the put list. */
160 get_tailp = &mbuf->m_nextpkt;
165 struct mbuf *put_next;
167 put_next = mbuf->m_nextpkt;
168 mbuf->m_nextpkt = get_next;
173 } while (mbuf != NULL);
175 /* Append the reversed put list to the get list. */
176 KASSERT(*get_tailp == NULL, ("*get_tailp != NULL"));
177 *stdp->std_getp = get_next;
178 stdp->std_getp = get_tailp;
179 stdp->std_count += count;
182 #endif /* SFXGE_HAVE_MQ */
185 sfxge_tx_qreap(struct sfxge_txq *txq)
187 mtx_assert(SFXGE_TXQ_LOCK(txq), MA_OWNED);
189 txq->reaped = txq->completed;
193 sfxge_tx_qlist_post(struct sfxge_txq *txq)
195 unsigned int old_added;
199 mtx_assert(SFXGE_TXQ_LOCK(txq), MA_OWNED);
201 KASSERT(txq->n_pend_desc != 0, ("txq->n_pend_desc == 0"));
202 KASSERT(txq->n_pend_desc <= SFXGE_TSO_MAX_DESC,
203 ("txq->n_pend_desc too large"));
204 KASSERT(!txq->blocked, ("txq->blocked"));
206 old_added = txq->added;
208 /* Post the fragment list. */
209 rc = efx_tx_qpost(txq->common, txq->pend_desc, txq->n_pend_desc,
210 txq->reaped, &txq->added);
211 KASSERT(rc == 0, ("efx_tx_qpost() failed"));
213 /* If efx_tx_qpost() had to refragment, our information about
214 * buffers to free may be associated with the wrong
217 KASSERT(txq->added - old_added == txq->n_pend_desc,
218 ("efx_tx_qpost() refragmented descriptors"));
220 level = txq->added - txq->reaped;
221 KASSERT(level <= SFXGE_NDESCS, ("overfilled TX queue"));
223 /* Clear the fragment list. */
224 txq->n_pend_desc = 0;
226 /* Have we reached the block level? */
227 if (level < SFXGE_TXQ_BLOCK_LEVEL)
230 /* Reap, and check again */
232 level = txq->added - txq->reaped;
233 if (level < SFXGE_TXQ_BLOCK_LEVEL)
239 * Avoid a race with completion interrupt handling that could leave
244 level = txq->added - txq->reaped;
245 if (level < SFXGE_TXQ_BLOCK_LEVEL) {
251 static int sfxge_tx_queue_mbuf(struct sfxge_txq *txq, struct mbuf *mbuf)
253 bus_dmamap_t *used_map;
255 bus_dma_segment_t dma_seg[SFXGE_TX_MAPPING_MAX_SEG];
257 struct sfxge_tx_mapping *stmp;
263 KASSERT(!txq->blocked, ("txq->blocked"));
265 if (mbuf->m_pkthdr.csum_flags & CSUM_TSO)
266 prefetch_read_many(mbuf->m_data);
268 if (txq->init_state != SFXGE_TXQ_STARTED) {
273 /* Load the packet for DMA. */
274 id = txq->added & (SFXGE_NDESCS - 1);
275 stmp = &txq->stmp[id];
276 rc = bus_dmamap_load_mbuf_sg(txq->packet_dma_tag, stmp->map,
277 mbuf, dma_seg, &n_dma_seg, 0);
280 struct mbuf *new_mbuf = m_collapse(mbuf, M_NOWAIT,
281 SFXGE_TX_MAPPING_MAX_SEG);
282 if (new_mbuf == NULL)
286 rc = bus_dmamap_load_mbuf_sg(txq->packet_dma_tag,
288 dma_seg, &n_dma_seg, 0);
293 /* Make the packet visible to the hardware. */
294 bus_dmamap_sync(txq->packet_dma_tag, stmp->map, BUS_DMASYNC_PREWRITE);
296 used_map = &stmp->map;
298 if (mbuf->m_pkthdr.csum_flags & CSUM_TSO) {
299 rc = sfxge_tx_queue_tso(txq, mbuf, dma_seg, n_dma_seg);
302 stmp = &txq->stmp[rc];
304 /* Add the mapping to the fragment list, and set flags
309 desc = &txq->pend_desc[i];
310 desc->eb_addr = dma_seg[i].ds_addr;
311 desc->eb_size = dma_seg[i].ds_len;
312 if (i == n_dma_seg - 1) {
320 if (__predict_false(stmp ==
321 &txq->stmp[SFXGE_NDESCS - 1]))
322 stmp = &txq->stmp[0];
326 txq->n_pend_desc = n_dma_seg;
330 * If the mapping required more than one descriptor
331 * then we need to associate the DMA map with the last
332 * descriptor, not the first.
334 if (used_map != &stmp->map) {
336 stmp->map = *used_map;
341 stmp->flags = TX_BUF_UNMAP | TX_BUF_MBUF;
343 /* Post the fragment list. */
344 sfxge_tx_qlist_post(txq);
349 bus_dmamap_unload(txq->packet_dma_tag, *used_map);
351 /* Drop the packet on the floor. */
361 * Drain the deferred packet list into the transmit queue.
364 sfxge_tx_qdpl_drain(struct sfxge_txq *txq)
366 struct sfxge_softc *sc;
367 struct sfxge_tx_dpl *stdp;
368 struct mbuf *mbuf, *next;
373 mtx_assert(&txq->lock, MA_OWNED);
379 prefetch_read_many(sc->enp);
380 prefetch_read_many(txq->common);
382 mbuf = stdp->std_get;
383 count = stdp->std_count;
386 KASSERT(mbuf != NULL, ("mbuf == NULL"));
388 next = mbuf->m_nextpkt;
389 mbuf->m_nextpkt = NULL;
391 ETHER_BPF_MTAP(sc->ifnet, mbuf); /* packet capture */
394 prefetch_read_many(next);
396 rc = sfxge_tx_queue_mbuf(txq, mbuf);
405 /* Push the fragments to the hardware in batches. */
406 if (txq->added - pushed >= SFXGE_TX_BATCH) {
407 efx_tx_qpush(txq->common, txq->added);
413 KASSERT(mbuf == NULL, ("mbuf != NULL"));
414 stdp->std_get = NULL;
416 stdp->std_getp = &stdp->std_get;
418 stdp->std_get = mbuf;
419 stdp->std_count = count;
422 if (txq->added != pushed)
423 efx_tx_qpush(txq->common, txq->added);
425 KASSERT(txq->blocked || stdp->std_count == 0,
426 ("queue unblocked but count is non-zero"));
429 #define SFXGE_TX_QDPL_PENDING(_txq) \
430 ((_txq)->dpl.std_put != 0)
433 * Service the deferred packet list.
435 * NOTE: drops the txq mutex!
438 sfxge_tx_qdpl_service(struct sfxge_txq *txq)
440 mtx_assert(&txq->lock, MA_OWNED);
443 if (SFXGE_TX_QDPL_PENDING(txq))
444 sfxge_tx_qdpl_swizzle(txq);
447 sfxge_tx_qdpl_drain(txq);
449 mtx_unlock(&txq->lock);
450 } while (SFXGE_TX_QDPL_PENDING(txq) &&
451 mtx_trylock(&txq->lock));
455 * Put a packet on the deferred packet list.
457 * If we are called with the txq lock held, we put the packet on the "get
458 * list", otherwise we atomically push it on the "put list". The swizzle
459 * function takes care of ordering.
461 * The length of the put list is bounded by SFXGE_TX_MAX_DEFFERED. We
462 * overload the csum_data field in the mbuf to keep track of this length
463 * because there is no cheap alternative to avoid races.
466 sfxge_tx_qdpl_put(struct sfxge_txq *txq, struct mbuf *mbuf, int locked)
468 struct sfxge_tx_dpl *stdp;
472 KASSERT(mbuf->m_nextpkt == NULL, ("mbuf->m_nextpkt != NULL"));
475 mtx_assert(&txq->lock, MA_OWNED);
477 sfxge_tx_qdpl_swizzle(txq);
479 *(stdp->std_getp) = mbuf;
480 stdp->std_getp = &mbuf->m_nextpkt;
483 volatile uintptr_t *putp;
488 putp = &stdp->std_put;
489 new = (uintptr_t)mbuf;
494 struct mbuf *mp = (struct mbuf *)old;
495 old_len = mp->m_pkthdr.csum_data;
498 if (old_len >= SFXGE_TX_MAX_DEFERRED)
500 mbuf->m_pkthdr.csum_data = old_len + 1;
501 mbuf->m_nextpkt = (void *)old;
502 } while (atomic_cmpset_ptr(putp, old, new) == 0);
509 * Called from if_transmit - will try to grab the txq lock and enqueue to the
510 * put list if it succeeds, otherwise will push onto the defer list.
513 sfxge_tx_packet_add(struct sfxge_txq *txq, struct mbuf *m)
519 * Try to grab the txq lock. If we are able to get the lock,
520 * the packet will be appended to the "get list" of the deferred
521 * packet list. Otherwise, it will be pushed on the "put list".
523 locked = mtx_trylock(&txq->lock);
526 * Can only fail if we weren't able to get the lock.
528 if (sfxge_tx_qdpl_put(txq, m, locked) != 0) {
530 ("sfxge_tx_qdpl_put() failed locked"));
536 * Try to grab the lock again.
538 * If we are able to get the lock, we need to process the deferred
539 * packet list. If we are not able to get the lock, another thread
540 * is processing the list.
543 locked = mtx_trylock(&txq->lock);
546 /* Try to service the list. */
547 sfxge_tx_qdpl_service(txq);
548 /* Lock has been dropped. */
560 sfxge_tx_qdpl_flush(struct sfxge_txq *txq)
562 struct sfxge_tx_dpl *stdp = &txq->dpl;
563 struct mbuf *mbuf, *next;
565 mtx_lock(&txq->lock);
567 sfxge_tx_qdpl_swizzle(txq);
568 for (mbuf = stdp->std_get; mbuf != NULL; mbuf = next) {
569 next = mbuf->m_nextpkt;
572 stdp->std_get = NULL;
574 stdp->std_getp = &stdp->std_get;
576 mtx_unlock(&txq->lock);
580 sfxge_if_qflush(struct ifnet *ifp)
582 struct sfxge_softc *sc;
587 for (i = 0; i < SFXGE_TX_SCALE(sc); i++)
588 sfxge_tx_qdpl_flush(sc->txq[i]);
592 * TX start -- called by the stack.
595 sfxge_if_transmit(struct ifnet *ifp, struct mbuf *m)
597 struct sfxge_softc *sc;
598 struct sfxge_txq *txq;
601 sc = (struct sfxge_softc *)ifp->if_softc;
603 KASSERT(ifp->if_flags & IFF_UP, ("interface not up"));
605 if (!SFXGE_LINK_UP(sc)) {
610 /* Pick the desired transmit queue. */
611 if (m->m_pkthdr.csum_flags & (CSUM_DELAY_DATA | CSUM_TSO)) {
614 if (m->m_flags & M_FLOWID) {
615 uint32_t hash = m->m_pkthdr.flowid;
617 index = sc->rx_indir_table[hash % SFXGE_RX_SCALE_MAX];
619 txq = sc->txq[SFXGE_TXQ_IP_TCP_UDP_CKSUM + index];
620 } else if (m->m_pkthdr.csum_flags & CSUM_DELAY_IP) {
621 txq = sc->txq[SFXGE_TXQ_IP_CKSUM];
623 txq = sc->txq[SFXGE_TXQ_NON_CKSUM];
626 rc = sfxge_tx_packet_add(txq, m);
631 #else /* !SFXGE_HAVE_MQ */
633 static void sfxge_if_start_locked(struct ifnet *ifp)
635 struct sfxge_softc *sc = ifp->if_softc;
636 struct sfxge_txq *txq;
638 unsigned int pushed[SFXGE_TXQ_NTYPES];
639 unsigned int q_index;
641 if ((ifp->if_drv_flags & (IFF_DRV_RUNNING|IFF_DRV_OACTIVE)) !=
645 if (!sc->port.link_up)
648 for (q_index = 0; q_index < SFXGE_TXQ_NTYPES; q_index++) {
649 txq = sc->txq[q_index];
650 pushed[q_index] = txq->added;
653 while (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) {
654 IFQ_DRV_DEQUEUE(&ifp->if_snd, mbuf);
658 ETHER_BPF_MTAP(ifp, mbuf); /* packet capture */
660 /* Pick the desired transmit queue. */
661 if (mbuf->m_pkthdr.csum_flags & (CSUM_DELAY_DATA | CSUM_TSO))
662 q_index = SFXGE_TXQ_IP_TCP_UDP_CKSUM;
663 else if (mbuf->m_pkthdr.csum_flags & CSUM_DELAY_IP)
664 q_index = SFXGE_TXQ_IP_CKSUM;
666 q_index = SFXGE_TXQ_NON_CKSUM;
667 txq = sc->txq[q_index];
669 if (sfxge_tx_queue_mbuf(txq, mbuf) != 0)
673 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
677 /* Push the fragments to the hardware in batches. */
678 if (txq->added - pushed[q_index] >= SFXGE_TX_BATCH) {
679 efx_tx_qpush(txq->common, txq->added);
680 pushed[q_index] = txq->added;
684 for (q_index = 0; q_index < SFXGE_TXQ_NTYPES; q_index++) {
685 txq = sc->txq[q_index];
686 if (txq->added != pushed[q_index])
687 efx_tx_qpush(txq->common, txq->added);
691 void sfxge_if_start(struct ifnet *ifp)
693 struct sfxge_softc *sc = ifp->if_softc;
695 mtx_lock(&sc->tx_lock);
696 sfxge_if_start_locked(ifp);
697 mtx_unlock(&sc->tx_lock);
701 sfxge_tx_qdpl_service(struct sfxge_txq *txq)
703 struct sfxge_softc *sc = txq->sc;
704 struct ifnet *ifp = sc->ifnet;
706 mtx_assert(&sc->tx_lock, MA_OWNED);
707 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
708 sfxge_if_start_locked(ifp);
709 mtx_unlock(&sc->tx_lock);
712 #endif /* SFXGE_HAVE_MQ */
715 * Software "TSO". Not quite as good as doing it in hardware, but
716 * still faster than segmenting in the stack.
719 struct sfxge_tso_state {
720 /* Output position */
721 unsigned out_len; /* Remaining length in current segment */
722 unsigned seqnum; /* Current sequence number */
723 unsigned packet_space; /* Remaining space in current packet */
726 unsigned dma_seg_i; /* Current DMA segment number */
727 uint64_t dma_addr; /* DMA address of current position */
728 unsigned in_len; /* Remaining length in current mbuf */
730 const struct mbuf *mbuf; /* Input mbuf (head of chain) */
731 u_short protocol; /* Network protocol (after VLAN decap) */
732 ssize_t nh_off; /* Offset of network header */
733 ssize_t tcph_off; /* Offset of TCP header */
734 unsigned header_len; /* Number of bytes of header */
735 int full_packet_size; /* Number of bytes to put in each outgoing
739 static inline const struct ip *tso_iph(const struct sfxge_tso_state *tso)
741 KASSERT(tso->protocol == htons(ETHERTYPE_IP),
742 ("tso_iph() in non-IPv4 state"));
743 return (const struct ip *)(tso->mbuf->m_data + tso->nh_off);
745 static inline const struct ip6_hdr *tso_ip6h(const struct sfxge_tso_state *tso)
747 KASSERT(tso->protocol == htons(ETHERTYPE_IPV6),
748 ("tso_ip6h() in non-IPv6 state"));
749 return (const struct ip6_hdr *)(tso->mbuf->m_data + tso->nh_off);
751 static inline const struct tcphdr *tso_tcph(const struct sfxge_tso_state *tso)
753 return (const struct tcphdr *)(tso->mbuf->m_data + tso->tcph_off);
756 /* Size of preallocated TSO header buffers. Larger blocks must be
757 * allocated from the heap.
759 #define TSOH_STD_SIZE 128
761 /* At most half the descriptors in the queue at any time will refer to
762 * a TSO header buffer, since they must always be followed by a
763 * payload descriptor referring to an mbuf.
765 #define TSOH_COUNT (SFXGE_NDESCS / 2u)
766 #define TSOH_PER_PAGE (PAGE_SIZE / TSOH_STD_SIZE)
767 #define TSOH_PAGE_COUNT ((TSOH_COUNT + TSOH_PER_PAGE - 1) / TSOH_PER_PAGE)
769 static int tso_init(struct sfxge_txq *txq)
771 struct sfxge_softc *sc = txq->sc;
774 /* Allocate TSO header buffers */
775 txq->tsoh_buffer = malloc(TSOH_PAGE_COUNT * sizeof(txq->tsoh_buffer[0]),
778 for (i = 0; i < TSOH_PAGE_COUNT; i++) {
779 rc = sfxge_dma_alloc(sc, PAGE_SIZE, &txq->tsoh_buffer[i]);
788 sfxge_dma_free(&txq->tsoh_buffer[i]);
789 free(txq->tsoh_buffer, M_SFXGE);
790 txq->tsoh_buffer = NULL;
794 static void tso_fini(struct sfxge_txq *txq)
798 if (txq->tsoh_buffer) {
799 for (i = 0; i < TSOH_PAGE_COUNT; i++)
800 sfxge_dma_free(&txq->tsoh_buffer[i]);
801 free(txq->tsoh_buffer, M_SFXGE);
805 static void tso_start(struct sfxge_tso_state *tso, struct mbuf *mbuf)
807 struct ether_header *eh = mtod(mbuf, struct ether_header *);
811 /* Find network protocol and header */
812 tso->protocol = eh->ether_type;
813 if (tso->protocol == htons(ETHERTYPE_VLAN)) {
814 struct ether_vlan_header *veh =
815 mtod(mbuf, struct ether_vlan_header *);
816 tso->protocol = veh->evl_proto;
817 tso->nh_off = sizeof(*veh);
819 tso->nh_off = sizeof(*eh);
822 /* Find TCP header */
823 if (tso->protocol == htons(ETHERTYPE_IP)) {
824 KASSERT(tso_iph(tso)->ip_p == IPPROTO_TCP,
825 ("TSO required on non-TCP packet"));
826 tso->tcph_off = tso->nh_off + 4 * tso_iph(tso)->ip_hl;
828 KASSERT(tso->protocol == htons(ETHERTYPE_IPV6),
829 ("TSO required on non-IP packet"));
830 KASSERT(tso_ip6h(tso)->ip6_nxt == IPPROTO_TCP,
831 ("TSO required on non-TCP packet"));
832 tso->tcph_off = tso->nh_off + sizeof(struct ip6_hdr);
835 /* We assume all headers are linear in the head mbuf */
836 tso->header_len = tso->tcph_off + 4 * tso_tcph(tso)->th_off;
837 KASSERT(tso->header_len <= mbuf->m_len, ("packet headers fragmented"));
838 tso->full_packet_size = tso->header_len + mbuf->m_pkthdr.tso_segsz;
840 tso->seqnum = ntohl(tso_tcph(tso)->th_seq);
842 /* These flags must not be duplicated */
843 KASSERT(!(tso_tcph(tso)->th_flags & (TH_URG | TH_SYN | TH_RST)),
844 ("incompatible TCP flag on TSO packet"));
846 tso->out_len = mbuf->m_pkthdr.len - tso->header_len;
850 * tso_fill_packet_with_fragment - form descriptors for the current fragment
852 * Form descriptors for the current fragment, until we reach the end
853 * of fragment or end-of-packet. Return 0 on success, 1 if not enough
856 static void tso_fill_packet_with_fragment(struct sfxge_txq *txq,
857 struct sfxge_tso_state *tso)
862 if (tso->in_len == 0 || tso->packet_space == 0)
865 KASSERT(tso->in_len > 0, ("TSO input length went negative"));
866 KASSERT(tso->packet_space > 0, ("TSO packet space went negative"));
868 n = min(tso->in_len, tso->packet_space);
870 tso->packet_space -= n;
874 desc = &txq->pend_desc[txq->n_pend_desc++];
875 desc->eb_addr = tso->dma_addr;
877 desc->eb_eop = tso->out_len == 0 || tso->packet_space == 0;
882 /* Callback from bus_dmamap_load() for long TSO headers. */
883 static void tso_map_long_header(void *dma_addr_ret,
884 bus_dma_segment_t *segs, int nseg,
887 *(uint64_t *)dma_addr_ret = ((__predict_true(error == 0) &&
888 __predict_true(nseg == 1)) ?
893 * tso_start_new_packet - generate a new header and prepare for the new packet
895 * Generate a new header and prepare for the new packet. Return 0 on
896 * success, or an error code if failed to alloc header.
898 static int tso_start_new_packet(struct sfxge_txq *txq,
899 struct sfxge_tso_state *tso,
902 struct sfxge_tx_mapping *stmp = &txq->stmp[id];
903 struct tcphdr *tsoh_th;
911 /* Allocate a DMA-mapped header buffer. */
912 if (__predict_true(tso->header_len <= TSOH_STD_SIZE)) {
913 unsigned int page_index = (id / 2) / TSOH_PER_PAGE;
914 unsigned int buf_index = (id / 2) % TSOH_PER_PAGE;
916 header = (txq->tsoh_buffer[page_index].esm_base +
917 buf_index * TSOH_STD_SIZE);
918 dma_addr = (txq->tsoh_buffer[page_index].esm_addr +
919 buf_index * TSOH_STD_SIZE);
920 map = txq->tsoh_buffer[page_index].esm_map;
924 /* We cannot use bus_dmamem_alloc() as that may sleep */
925 header = malloc(tso->header_len, M_SFXGE, M_NOWAIT);
926 if (__predict_false(!header))
928 rc = bus_dmamap_load(txq->packet_dma_tag, stmp->map,
929 header, tso->header_len,
930 tso_map_long_header, &dma_addr,
932 if (__predict_false(dma_addr == 0)) {
934 /* Succeeded but got >1 segment */
935 bus_dmamap_unload(txq->packet_dma_tag,
939 free(header, M_SFXGE);
944 txq->tso_long_headers++;
945 stmp->u.heap_buf = header;
946 stmp->flags = TX_BUF_UNMAP;
949 tsoh_th = (struct tcphdr *)(header + tso->tcph_off);
951 /* Copy and update the headers. */
952 memcpy(header, tso->mbuf->m_data, tso->header_len);
954 tsoh_th->th_seq = htonl(tso->seqnum);
955 tso->seqnum += tso->mbuf->m_pkthdr.tso_segsz;
956 if (tso->out_len > tso->mbuf->m_pkthdr.tso_segsz) {
957 /* This packet will not finish the TSO burst. */
958 ip_length = tso->full_packet_size - tso->nh_off;
959 tsoh_th->th_flags &= ~(TH_FIN | TH_PUSH);
961 /* This packet will be the last in the TSO burst. */
962 ip_length = tso->header_len - tso->nh_off + tso->out_len;
965 if (tso->protocol == htons(ETHERTYPE_IP)) {
966 struct ip *tsoh_iph = (struct ip *)(header + tso->nh_off);
967 tsoh_iph->ip_len = htons(ip_length);
968 /* XXX We should increment ip_id, but FreeBSD doesn't
969 * currently allocate extra IDs for multiple segments.
972 struct ip6_hdr *tsoh_iph =
973 (struct ip6_hdr *)(header + tso->nh_off);
974 tsoh_iph->ip6_plen = htons(ip_length - sizeof(*tsoh_iph));
977 /* Make the header visible to the hardware. */
978 bus_dmamap_sync(txq->packet_dma_tag, map, BUS_DMASYNC_PREWRITE);
980 tso->packet_space = tso->mbuf->m_pkthdr.tso_segsz;
983 /* Form a descriptor for this header. */
984 desc = &txq->pend_desc[txq->n_pend_desc++];
985 desc->eb_addr = dma_addr;
986 desc->eb_size = tso->header_len;
993 sfxge_tx_queue_tso(struct sfxge_txq *txq, struct mbuf *mbuf,
994 const bus_dma_segment_t *dma_seg, int n_dma_seg)
996 struct sfxge_tso_state tso;
997 unsigned int id, next_id;
999 tso_start(&tso, mbuf);
1001 /* Grab the first payload fragment. */
1002 if (dma_seg->ds_len == tso.header_len) {
1004 KASSERT(n_dma_seg, ("no payload found in TSO packet"));
1006 tso.in_len = dma_seg->ds_len;
1007 tso.dma_addr = dma_seg->ds_addr;
1009 tso.in_len = dma_seg->ds_len - tso.header_len;
1010 tso.dma_addr = dma_seg->ds_addr + tso.header_len;
1013 id = txq->added & (SFXGE_NDESCS - 1);
1014 if (__predict_false(tso_start_new_packet(txq, &tso, id)))
1018 id = (id + 1) & (SFXGE_NDESCS - 1);
1019 tso_fill_packet_with_fragment(txq, &tso);
1021 /* Move onto the next fragment? */
1022 if (tso.in_len == 0) {
1027 tso.in_len = dma_seg->ds_len;
1028 tso.dma_addr = dma_seg->ds_addr;
1031 /* End of packet? */
1032 if (tso.packet_space == 0) {
1033 /* If the queue is now full due to tiny MSS,
1034 * or we can't create another header, discard
1035 * the remainder of the input mbuf but do not
1036 * roll back the work we have done.
1038 if (txq->n_pend_desc >
1039 SFXGE_TSO_MAX_DESC - (1 + SFXGE_TX_MAPPING_MAX_SEG))
1041 next_id = (id + 1) & (SFXGE_NDESCS - 1);
1042 if (__predict_false(tso_start_new_packet(txq, &tso,
1054 sfxge_tx_qunblock(struct sfxge_txq *txq)
1056 struct sfxge_softc *sc;
1057 struct sfxge_evq *evq;
1060 evq = sc->evq[txq->evq_index];
1062 mtx_assert(&evq->lock, MA_OWNED);
1064 if (txq->init_state != SFXGE_TXQ_STARTED)
1067 mtx_lock(SFXGE_TXQ_LOCK(txq));
1072 level = txq->added - txq->completed;
1073 if (level <= SFXGE_TXQ_UNBLOCK_LEVEL)
1077 sfxge_tx_qdpl_service(txq);
1078 /* note: lock has been dropped */
1082 sfxge_tx_qflush_done(struct sfxge_txq *txq)
1085 txq->flush_state = SFXGE_FLUSH_DONE;
1089 sfxge_tx_qstop(struct sfxge_softc *sc, unsigned int index)
1091 struct sfxge_txq *txq;
1092 struct sfxge_evq *evq;
1095 txq = sc->txq[index];
1096 evq = sc->evq[txq->evq_index];
1098 mtx_lock(SFXGE_TXQ_LOCK(txq));
1100 KASSERT(txq->init_state == SFXGE_TXQ_STARTED,
1101 ("txq->init_state != SFXGE_TXQ_STARTED"));
1103 txq->init_state = SFXGE_TXQ_INITIALIZED;
1104 txq->flush_state = SFXGE_FLUSH_PENDING;
1106 /* Flush the transmit queue. */
1107 efx_tx_qflush(txq->common);
1109 mtx_unlock(SFXGE_TXQ_LOCK(txq));
1113 /* Spin for 100ms. */
1116 if (txq->flush_state != SFXGE_FLUSH_PENDING)
1118 } while (++count < 20);
1120 mtx_lock(&evq->lock);
1121 mtx_lock(SFXGE_TXQ_LOCK(txq));
1123 KASSERT(txq->flush_state != SFXGE_FLUSH_FAILED,
1124 ("txq->flush_state == SFXGE_FLUSH_FAILED"));
1126 txq->flush_state = SFXGE_FLUSH_DONE;
1129 txq->pending = txq->added;
1131 sfxge_tx_qcomplete(txq);
1132 KASSERT(txq->completed == txq->added,
1133 ("txq->completed != txq->added"));
1135 sfxge_tx_qreap(txq);
1136 KASSERT(txq->reaped == txq->completed,
1137 ("txq->reaped != txq->completed"));
1144 /* Destroy the common code transmit queue. */
1145 efx_tx_qdestroy(txq->common);
1148 efx_sram_buf_tbl_clear(sc->enp, txq->buf_base_id,
1149 EFX_TXQ_NBUFS(SFXGE_NDESCS));
1151 mtx_unlock(&evq->lock);
1152 mtx_unlock(SFXGE_TXQ_LOCK(txq));
1156 sfxge_tx_qstart(struct sfxge_softc *sc, unsigned int index)
1158 struct sfxge_txq *txq;
1161 struct sfxge_evq *evq;
1164 txq = sc->txq[index];
1166 evq = sc->evq[txq->evq_index];
1168 KASSERT(txq->init_state == SFXGE_TXQ_INITIALIZED,
1169 ("txq->init_state != SFXGE_TXQ_INITIALIZED"));
1170 KASSERT(evq->init_state == SFXGE_EVQ_STARTED,
1171 ("evq->init_state != SFXGE_EVQ_STARTED"));
1173 /* Program the buffer table. */
1174 if ((rc = efx_sram_buf_tbl_set(sc->enp, txq->buf_base_id, esmp,
1175 EFX_TXQ_NBUFS(SFXGE_NDESCS))) != 0)
1178 /* Determine the kind of queue we are creating. */
1179 switch (txq->type) {
1180 case SFXGE_TXQ_NON_CKSUM:
1183 case SFXGE_TXQ_IP_CKSUM:
1184 flags = EFX_CKSUM_IPV4;
1186 case SFXGE_TXQ_IP_TCP_UDP_CKSUM:
1187 flags = EFX_CKSUM_IPV4 | EFX_CKSUM_TCPUDP;
1190 KASSERT(0, ("Impossible TX queue"));
1195 /* Create the common code transmit queue. */
1196 if ((rc = efx_tx_qcreate(sc->enp, index, txq->type, esmp,
1197 SFXGE_NDESCS, txq->buf_base_id, flags, evq->common,
1198 &txq->common)) != 0)
1201 mtx_lock(SFXGE_TXQ_LOCK(txq));
1203 /* Enable the transmit queue. */
1204 efx_tx_qenable(txq->common);
1206 txq->init_state = SFXGE_TXQ_STARTED;
1208 mtx_unlock(SFXGE_TXQ_LOCK(txq));
1213 efx_sram_buf_tbl_clear(sc->enp, txq->buf_base_id,
1214 EFX_TXQ_NBUFS(SFXGE_NDESCS));
1219 sfxge_tx_stop(struct sfxge_softc *sc)
1221 const efx_nic_cfg_t *encp;
1224 index = SFXGE_TX_SCALE(sc);
1225 while (--index >= 0)
1226 sfxge_tx_qstop(sc, SFXGE_TXQ_IP_TCP_UDP_CKSUM + index);
1228 sfxge_tx_qstop(sc, SFXGE_TXQ_IP_CKSUM);
1230 encp = efx_nic_cfg_get(sc->enp);
1231 sfxge_tx_qstop(sc, SFXGE_TXQ_NON_CKSUM);
1233 /* Tear down the transmit module */
1234 efx_tx_fini(sc->enp);
1238 sfxge_tx_start(struct sfxge_softc *sc)
1243 /* Initialize the common code transmit module. */
1244 if ((rc = efx_tx_init(sc->enp)) != 0)
1247 if ((rc = sfxge_tx_qstart(sc, SFXGE_TXQ_NON_CKSUM)) != 0)
1250 if ((rc = sfxge_tx_qstart(sc, SFXGE_TXQ_IP_CKSUM)) != 0)
1253 for (index = 0; index < SFXGE_TX_SCALE(sc); index++) {
1254 if ((rc = sfxge_tx_qstart(sc, SFXGE_TXQ_IP_TCP_UDP_CKSUM +
1262 while (--index >= 0)
1263 sfxge_tx_qstop(sc, SFXGE_TXQ_IP_TCP_UDP_CKSUM + index);
1265 sfxge_tx_qstop(sc, SFXGE_TXQ_IP_CKSUM);
1268 sfxge_tx_qstop(sc, SFXGE_TXQ_NON_CKSUM);
1271 efx_tx_fini(sc->enp);
1277 * Destroy a transmit queue.
1280 sfxge_tx_qfini(struct sfxge_softc *sc, unsigned int index)
1282 struct sfxge_txq *txq;
1283 unsigned int nmaps = SFXGE_NDESCS;
1285 txq = sc->txq[index];
1287 KASSERT(txq->init_state == SFXGE_TXQ_INITIALIZED,
1288 ("txq->init_state != SFXGE_TXQ_INITIALIZED"));
1290 if (txq->type == SFXGE_TXQ_IP_TCP_UDP_CKSUM)
1293 /* Free the context arrays. */
1294 free(txq->pend_desc, M_SFXGE);
1296 bus_dmamap_destroy(txq->packet_dma_tag, txq->stmp[nmaps].map);
1297 free(txq->stmp, M_SFXGE);
1299 /* Release DMA memory mapping. */
1300 sfxge_dma_free(&txq->mem);
1302 sc->txq[index] = NULL;
1304 #ifdef SFXGE_HAVE_MQ
1305 mtx_destroy(&txq->lock);
1312 sfxge_tx_qinit(struct sfxge_softc *sc, unsigned int txq_index,
1313 enum sfxge_txq_type type, unsigned int evq_index)
1315 struct sfxge_txq *txq;
1316 struct sfxge_evq *evq;
1317 #ifdef SFXGE_HAVE_MQ
1318 struct sfxge_tx_dpl *stdp;
1324 txq = malloc(sizeof(struct sfxge_txq), M_SFXGE, M_ZERO | M_WAITOK);
1327 sc->txq[txq_index] = txq;
1330 evq = sc->evq[evq_index];
1332 /* Allocate and zero DMA space for the descriptor ring. */
1333 if ((rc = sfxge_dma_alloc(sc, EFX_TXQ_SIZE(SFXGE_NDESCS), esmp)) != 0)
1335 (void)memset(esmp->esm_base, 0, EFX_TXQ_SIZE(SFXGE_NDESCS));
1337 /* Allocate buffer table entries. */
1338 sfxge_sram_buf_tbl_alloc(sc, EFX_TXQ_NBUFS(SFXGE_NDESCS),
1341 /* Create a DMA tag for packet mappings. */
1342 if (bus_dma_tag_create(sc->parent_dma_tag, 1, 0x1000,
1343 MIN(0x3FFFFFFFFFFFUL, BUS_SPACE_MAXADDR), BUS_SPACE_MAXADDR, NULL,
1344 NULL, 0x11000, SFXGE_TX_MAPPING_MAX_SEG, 0x1000, 0, NULL, NULL,
1345 &txq->packet_dma_tag) != 0) {
1346 device_printf(sc->dev, "Couldn't allocate txq DMA tag\n");
1351 /* Allocate pending descriptor array for batching writes. */
1352 txq->pend_desc = malloc(sizeof(efx_buffer_t) * SFXGE_NDESCS,
1353 M_SFXGE, M_ZERO | M_WAITOK);
1355 /* Allocate and initialise mbuf DMA mapping array. */
1356 txq->stmp = malloc(sizeof(struct sfxge_tx_mapping) * SFXGE_NDESCS,
1357 M_SFXGE, M_ZERO | M_WAITOK);
1358 for (nmaps = 0; nmaps < SFXGE_NDESCS; nmaps++) {
1359 rc = bus_dmamap_create(txq->packet_dma_tag, 0,
1360 &txq->stmp[nmaps].map);
1365 if (type == SFXGE_TXQ_IP_TCP_UDP_CKSUM &&
1366 (rc = tso_init(txq)) != 0)
1369 #ifdef SFXGE_HAVE_MQ
1370 /* Initialize the deferred packet list. */
1372 stdp->std_getp = &stdp->std_get;
1374 mtx_init(&txq->lock, "txq", NULL, MTX_DEF);
1378 txq->evq_index = evq_index;
1379 txq->txq_index = txq_index;
1380 txq->init_state = SFXGE_TXQ_INITIALIZED;
1385 free(txq->pend_desc, M_SFXGE);
1388 bus_dmamap_destroy(txq->packet_dma_tag, txq->stmp[nmaps].map);
1389 free(txq->stmp, M_SFXGE);
1390 bus_dma_tag_destroy(txq->packet_dma_tag);
1393 sfxge_dma_free(esmp);
1398 static const struct {
1401 } sfxge_tx_stats[] = {
1402 #define SFXGE_TX_STAT(name, member) \
1403 { #name, offsetof(struct sfxge_txq, member) }
1404 SFXGE_TX_STAT(tso_bursts, tso_bursts),
1405 SFXGE_TX_STAT(tso_packets, tso_packets),
1406 SFXGE_TX_STAT(tso_long_headers, tso_long_headers),
1407 SFXGE_TX_STAT(tx_collapses, collapses),
1408 SFXGE_TX_STAT(tx_drops, drops),
1412 sfxge_tx_stat_handler(SYSCTL_HANDLER_ARGS)
1414 struct sfxge_softc *sc = arg1;
1415 unsigned int id = arg2;
1419 /* Sum across all TX queues */
1422 index < SFXGE_TXQ_IP_TCP_UDP_CKSUM + SFXGE_TX_SCALE(sc);
1424 sum += *(unsigned long *)((caddr_t)sc->txq[index] +
1425 sfxge_tx_stats[id].offset);
1427 return SYSCTL_OUT(req, &sum, sizeof(sum));
1431 sfxge_tx_stat_init(struct sfxge_softc *sc)
1433 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev);
1434 struct sysctl_oid_list *stat_list;
1437 stat_list = SYSCTL_CHILDREN(sc->stats_node);
1440 id < sizeof(sfxge_tx_stats) / sizeof(sfxge_tx_stats[0]);
1444 OID_AUTO, sfxge_tx_stats[id].name,
1445 CTLTYPE_ULONG|CTLFLAG_RD,
1446 sc, id, sfxge_tx_stat_handler, "LU",
1452 sfxge_tx_fini(struct sfxge_softc *sc)
1456 index = SFXGE_TX_SCALE(sc);
1457 while (--index >= 0)
1458 sfxge_tx_qfini(sc, SFXGE_TXQ_IP_TCP_UDP_CKSUM + index);
1460 sfxge_tx_qfini(sc, SFXGE_TXQ_IP_CKSUM);
1461 sfxge_tx_qfini(sc, SFXGE_TXQ_NON_CKSUM);
1466 sfxge_tx_init(struct sfxge_softc *sc)
1468 struct sfxge_intr *intr;
1474 KASSERT(intr->state == SFXGE_INTR_INITIALIZED,
1475 ("intr->state != SFXGE_INTR_INITIALIZED"));
1477 /* Initialize the transmit queues */
1478 if ((rc = sfxge_tx_qinit(sc, SFXGE_TXQ_NON_CKSUM,
1479 SFXGE_TXQ_NON_CKSUM, 0)) != 0)
1482 if ((rc = sfxge_tx_qinit(sc, SFXGE_TXQ_IP_CKSUM,
1483 SFXGE_TXQ_IP_CKSUM, 0)) != 0)
1486 for (index = 0; index < SFXGE_TX_SCALE(sc); index++) {
1487 if ((rc = sfxge_tx_qinit(sc, SFXGE_TXQ_IP_TCP_UDP_CKSUM + index,
1488 SFXGE_TXQ_IP_TCP_UDP_CKSUM, index)) != 0)
1492 sfxge_tx_stat_init(sc);
1497 sfxge_tx_qfini(sc, SFXGE_TXQ_IP_CKSUM);
1499 while (--index >= 0)
1500 sfxge_tx_qfini(sc, SFXGE_TXQ_IP_TCP_UDP_CKSUM + index);
1503 sfxge_tx_qfini(sc, SFXGE_TXQ_NON_CKSUM);