1227569Sphilip/*-
2300607Sarybchik * Copyright (c) 2010-2016 Solarflare Communications Inc.
3227569Sphilip * All rights reserved.
4227569Sphilip *
5227569Sphilip * This software was developed in part by Philip Paeps under contract for
6227569Sphilip * Solarflare Communications, Inc.
7227569Sphilip *
8227569Sphilip * Redistribution and use in source and binary forms, with or without
9283514Sarybchik * modification, are permitted provided that the following conditions are met:
10227569Sphilip *
11283514Sarybchik * 1. Redistributions of source code must retain the above copyright notice,
12283514Sarybchik *    this list of conditions and the following disclaimer.
13283514Sarybchik * 2. Redistributions in binary form must reproduce the above copyright notice,
14283514Sarybchik *    this list of conditions and the following disclaimer in the documentation
15283514Sarybchik *    and/or other materials provided with the distribution.
16283514Sarybchik *
17283514Sarybchik * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18283514Sarybchik * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19283514Sarybchik * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20283514Sarybchik * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21283514Sarybchik * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22283514Sarybchik * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23283514Sarybchik * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24283514Sarybchik * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25283514Sarybchik * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26283514Sarybchik * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27283514Sarybchik * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28283514Sarybchik *
29283514Sarybchik * The views and conclusions contained in the software and documentation are
30283514Sarybchik * those of the authors and should not be interpreted as representing official
31283514Sarybchik * policies, either expressed or implied, of the FreeBSD Project.
32227569Sphilip */
33227569Sphilip
34264461Sgnn/* Theory of operation:
35264461Sgnn *
36342455Sarybchik * Tx queues allocation and mapping on Siena
37264461Sgnn *
38264461Sgnn * One Tx queue with enabled checksum offload is allocated per Rx channel
39264461Sgnn * (event queue).  Also 2 Tx queues (one without checksum offload and one
40264461Sgnn * with IP checksum offload only) are allocated and bound to event queue 0.
41264461Sgnn * sfxge_txq_type is used as Tx queue label.
42264461Sgnn *
43264461Sgnn * So, event queue plus label mapping to Tx queue index is:
44264461Sgnn *	if event queue index is 0, TxQ-index = TxQ-label * [0..SFXGE_TXQ_NTYPES)
45264461Sgnn *	else TxQ-index = SFXGE_TXQ_NTYPES + EvQ-index - 1
46264461Sgnn * See sfxge_get_txq_by_label() sfxge_ev.c
47342455Sarybchik *
48342455Sarybchik * Tx queue allocation and mapping on EF10
49342455Sarybchik *
50342455Sarybchik * One Tx queue with enabled checksum offload is allocated per Rx
51342455Sarybchik * channel (event queue). Checksum offload on all Tx queues is enabled or
52342455Sarybchik * disabled dynamically by inserting option descriptors, so the additional
53342455Sarybchik * queues used on Siena are not required.
54342455Sarybchik *
55342455Sarybchik * TxQ label is always set to zero on EF10 hardware.
56342455Sarybchik * So, event queue to Tx queue mapping is simple:
57342455Sarybchik * TxQ-index = EvQ-index
58264461Sgnn */
59264461Sgnn
60227569Sphilip#include <sys/cdefs.h>
61227569Sphilip__FBSDID("$FreeBSD: stable/11/sys/dev/sfxge/sfxge_tx.c 342455 2018-12-25 07:39:34Z arybchik $");
62227569Sphilip
63301494Sarybchik#include "opt_rss.h"
64301494Sarybchik
65295126Sglebius#include <sys/param.h>
66295126Sglebius#include <sys/malloc.h>
67227569Sphilip#include <sys/mbuf.h>
68227569Sphilip#include <sys/smp.h>
69227569Sphilip#include <sys/socket.h>
70227569Sphilip#include <sys/sysctl.h>
71272331Sgnn#include <sys/syslog.h>
72294077Sarybchik#include <sys/limits.h>
73227569Sphilip
74227569Sphilip#include <net/bpf.h>
75227569Sphilip#include <net/ethernet.h>
76227569Sphilip#include <net/if.h>
77227569Sphilip#include <net/if_vlan_var.h>
78227569Sphilip
79227569Sphilip#include <netinet/in.h>
80227569Sphilip#include <netinet/ip.h>
81227569Sphilip#include <netinet/ip6.h>
82227569Sphilip#include <netinet/tcp.h>
83227569Sphilip
84301494Sarybchik#ifdef RSS
85301494Sarybchik#include <net/rss_config.h>
86301494Sarybchik#endif
87301494Sarybchik
88227569Sphilip#include "common/efx.h"
89227569Sphilip
90227569Sphilip#include "sfxge.h"
91227569Sphilip#include "sfxge_tx.h"
92227569Sphilip
93227569Sphilip
94272331Sgnn#define	SFXGE_PARAM_TX_DPL_GET_MAX	SFXGE_PARAM(tx_dpl_get_max)
95272331Sgnnstatic int sfxge_tx_dpl_get_max = SFXGE_TX_DPL_GET_PKT_LIMIT_DEFAULT;
96272331SgnnTUNABLE_INT(SFXGE_PARAM_TX_DPL_GET_MAX, &sfxge_tx_dpl_get_max);
97272331SgnnSYSCTL_INT(_hw_sfxge, OID_AUTO, tx_dpl_get_max, CTLFLAG_RDTUN,
98272331Sgnn	   &sfxge_tx_dpl_get_max, 0,
99277895Sarybchik	   "Maximum number of any packets in deferred packet get-list");
100272331Sgnn
101277895Sarybchik#define	SFXGE_PARAM_TX_DPL_GET_NON_TCP_MAX \
102277895Sarybchik	SFXGE_PARAM(tx_dpl_get_non_tcp_max)
103277895Sarybchikstatic int sfxge_tx_dpl_get_non_tcp_max =
104277895Sarybchik	SFXGE_TX_DPL_GET_NON_TCP_PKT_LIMIT_DEFAULT;
105277895SarybchikTUNABLE_INT(SFXGE_PARAM_TX_DPL_GET_NON_TCP_MAX, &sfxge_tx_dpl_get_non_tcp_max);
106277895SarybchikSYSCTL_INT(_hw_sfxge, OID_AUTO, tx_dpl_get_non_tcp_max, CTLFLAG_RDTUN,
107277895Sarybchik	   &sfxge_tx_dpl_get_non_tcp_max, 0,
108277895Sarybchik	   "Maximum number of non-TCP packets in deferred packet get-list");
109277895Sarybchik
110272331Sgnn#define	SFXGE_PARAM_TX_DPL_PUT_MAX	SFXGE_PARAM(tx_dpl_put_max)
111272331Sgnnstatic int sfxge_tx_dpl_put_max = SFXGE_TX_DPL_PUT_PKT_LIMIT_DEFAULT;
112272331SgnnTUNABLE_INT(SFXGE_PARAM_TX_DPL_PUT_MAX, &sfxge_tx_dpl_put_max);
113272331SgnnSYSCTL_INT(_hw_sfxge, OID_AUTO, tx_dpl_put_max, CTLFLAG_RDTUN,
114272331Sgnn	   &sfxge_tx_dpl_put_max, 0,
115277895Sarybchik	   "Maximum number of any packets in deferred packet put-list");
116272331Sgnn
117283514Sarybchik#define	SFXGE_PARAM_TSO_FW_ASSISTED	SFXGE_PARAM(tso_fw_assisted)
118294077Sarybchikstatic int sfxge_tso_fw_assisted = (SFXGE_FATSOV1 | SFXGE_FATSOV2);
119283514SarybchikTUNABLE_INT(SFXGE_PARAM_TSO_FW_ASSISTED, &sfxge_tso_fw_assisted);
120283514SarybchikSYSCTL_INT(_hw_sfxge, OID_AUTO, tso_fw_assisted, CTLFLAG_RDTUN,
121283514Sarybchik	   &sfxge_tso_fw_assisted, 0,
122294077Sarybchik	   "Bitmask of FW-assisted TSO allowed to use if supported by NIC firmware");
123272331Sgnn
124283514Sarybchik
125280377Sarybchikstatic const struct {
126280377Sarybchik	const char *name;
127280377Sarybchik	size_t offset;
128280377Sarybchik} sfxge_tx_stats[] = {
129280377Sarybchik#define	SFXGE_TX_STAT(name, member) \
130280377Sarybchik	{ #name, offsetof(struct sfxge_txq, member) }
131280377Sarybchik	SFXGE_TX_STAT(tso_bursts, tso_bursts),
132280377Sarybchik	SFXGE_TX_STAT(tso_packets, tso_packets),
133280377Sarybchik	SFXGE_TX_STAT(tso_long_headers, tso_long_headers),
134280377Sarybchik	SFXGE_TX_STAT(tso_pdrop_too_many, tso_pdrop_too_many),
135280377Sarybchik	SFXGE_TX_STAT(tso_pdrop_no_rsrc, tso_pdrop_no_rsrc),
136280377Sarybchik	SFXGE_TX_STAT(tx_collapses, collapses),
137280377Sarybchik	SFXGE_TX_STAT(tx_drops, drops),
138280377Sarybchik	SFXGE_TX_STAT(tx_get_overflow, get_overflow),
139280377Sarybchik	SFXGE_TX_STAT(tx_get_non_tcp_overflow, get_non_tcp_overflow),
140280377Sarybchik	SFXGE_TX_STAT(tx_put_overflow, put_overflow),
141280377Sarybchik	SFXGE_TX_STAT(tx_netdown_drops, netdown_drops),
142280377Sarybchik};
143280377Sarybchik
144280377Sarybchik
145227569Sphilip/* Forward declarations. */
146278837Sarybchikstatic void sfxge_tx_qdpl_service(struct sfxge_txq *txq);
147227569Sphilipstatic void sfxge_tx_qlist_post(struct sfxge_txq *txq);
148227569Sphilipstatic void sfxge_tx_qunblock(struct sfxge_txq *txq);
149227569Sphilipstatic int sfxge_tx_queue_tso(struct sfxge_txq *txq, struct mbuf *mbuf,
150283514Sarybchik			      const bus_dma_segment_t *dma_seg, int n_dma_seg,
151342455Sarybchik			      int n_extra_descs);
152227569Sphilip
153342455Sarybchikstatic inline void
154342455Sarybchiksfxge_next_stmp(struct sfxge_txq *txq, struct sfxge_tx_mapping **pstmp)
155342455Sarybchik{
156342455Sarybchik	KASSERT((*pstmp)->flags == 0, ("stmp flags are not 0"));
157342455Sarybchik	if (__predict_false(*pstmp ==
158342455Sarybchik			    &txq->stmp[txq->ptr_mask]))
159342455Sarybchik		*pstmp = &txq->stmp[0];
160342455Sarybchik	else
161342455Sarybchik		(*pstmp)++;
162342455Sarybchik}
163342455Sarybchik
164283514Sarybchikstatic int
165342455Sarybchiksfxge_tx_maybe_toggle_cksum_offload(struct sfxge_txq *txq, struct mbuf *mbuf,
166342455Sarybchik				    struct sfxge_tx_mapping **pstmp)
167283514Sarybchik{
168342455Sarybchik	uint16_t new_hw_cksum_flags;
169342455Sarybchik	efx_desc_t *desc;
170342455Sarybchik
171342455Sarybchik	if (mbuf->m_pkthdr.csum_flags &
172342455Sarybchik	    (CSUM_DELAY_DATA | CSUM_DELAY_DATA_IPV6 | CSUM_TSO)) {
173342455Sarybchik		/*
174342455Sarybchik		 * We always set EFX_TXQ_CKSUM_IPV4 here because this
175342455Sarybchik		 * configuration is the most useful, and this won't
176342455Sarybchik		 * cause any trouble in case of IPv6 traffic anyway.
177342455Sarybchik		 */
178342455Sarybchik		new_hw_cksum_flags = EFX_TXQ_CKSUM_IPV4 | EFX_TXQ_CKSUM_TCPUDP;
179342455Sarybchik	} else if (mbuf->m_pkthdr.csum_flags & CSUM_DELAY_IP) {
180342455Sarybchik		new_hw_cksum_flags = EFX_TXQ_CKSUM_IPV4;
181342455Sarybchik	} else {
182342455Sarybchik		new_hw_cksum_flags = 0;
183342455Sarybchik	}
184342455Sarybchik
185342455Sarybchik	if (new_hw_cksum_flags == txq->hw_cksum_flags)
186342455Sarybchik		return (0);
187342455Sarybchik
188342455Sarybchik	desc = &txq->pend_desc[txq->n_pend_desc];
189342455Sarybchik	efx_tx_qdesc_checksum_create(txq->common, new_hw_cksum_flags, desc);
190342455Sarybchik	txq->hw_cksum_flags = new_hw_cksum_flags;
191342455Sarybchik	txq->n_pend_desc++;
192342455Sarybchik
193342455Sarybchik	sfxge_next_stmp(txq, pstmp);
194342455Sarybchik
195342455Sarybchik	return (1);
196342455Sarybchik}
197342455Sarybchik
198342455Sarybchikstatic int
199342455Sarybchiksfxge_tx_maybe_insert_tag(struct sfxge_txq *txq, struct mbuf *mbuf,
200342455Sarybchik			  struct sfxge_tx_mapping **pstmp)
201342455Sarybchik{
202283514Sarybchik	uint16_t this_tag = ((mbuf->m_flags & M_VLANTAG) ?
203283514Sarybchik			     mbuf->m_pkthdr.ether_vtag :
204283514Sarybchik			     0);
205342455Sarybchik	efx_desc_t *desc;
206283514Sarybchik
207283514Sarybchik	if (this_tag == txq->hw_vlan_tci)
208283514Sarybchik		return (0);
209283514Sarybchik
210342455Sarybchik	desc = &txq->pend_desc[txq->n_pend_desc];
211342455Sarybchik	efx_tx_qdesc_vlantci_create(txq->common, bswap16(this_tag), desc);
212283514Sarybchik	txq->hw_vlan_tci = this_tag;
213342455Sarybchik	txq->n_pend_desc++;
214342455Sarybchik
215342455Sarybchik	sfxge_next_stmp(txq, pstmp);
216342455Sarybchik
217283514Sarybchik	return (1);
218283514Sarybchik}
219283514Sarybchik
220227569Sphilipvoid
221277889Sarybchiksfxge_tx_qcomplete(struct sfxge_txq *txq, struct sfxge_evq *evq)
222227569Sphilip{
223227569Sphilip	unsigned int completed;
224227569Sphilip
225278221Sarybchik	SFXGE_EVQ_LOCK_ASSERT_OWNED(evq);
226227569Sphilip
227227569Sphilip	completed = txq->completed;
228227569Sphilip	while (completed != txq->pending) {
229227569Sphilip		struct sfxge_tx_mapping *stmp;
230227569Sphilip		unsigned int id;
231227569Sphilip
232272328Sgnn		id = completed++ & txq->ptr_mask;
233227569Sphilip
234227569Sphilip		stmp = &txq->stmp[id];
235227569Sphilip		if (stmp->flags & TX_BUF_UNMAP) {
236227569Sphilip			bus_dmamap_unload(txq->packet_dma_tag, stmp->map);
237227569Sphilip			if (stmp->flags & TX_BUF_MBUF) {
238227569Sphilip				struct mbuf *m = stmp->u.mbuf;
239227569Sphilip				do
240227569Sphilip					m = m_free(m);
241227569Sphilip				while (m != NULL);
242227569Sphilip			} else {
243227569Sphilip				free(stmp->u.heap_buf, M_SFXGE);
244227569Sphilip			}
245227569Sphilip			stmp->flags = 0;
246227569Sphilip		}
247227569Sphilip	}
248227569Sphilip	txq->completed = completed;
249227569Sphilip
250227569Sphilip	/* Check whether we need to unblock the queue. */
251227569Sphilip	mb();
252227569Sphilip	if (txq->blocked) {
253227569Sphilip		unsigned int level;
254227569Sphilip
255227569Sphilip		level = txq->added - txq->completed;
256272328Sgnn		if (level <= SFXGE_TXQ_UNBLOCK_LEVEL(txq->entries))
257227569Sphilip			sfxge_tx_qunblock(txq);
258227569Sphilip	}
259227569Sphilip}
260227569Sphilip
261278837Sarybchikstatic unsigned int
262277895Sarybchiksfxge_is_mbuf_non_tcp(struct mbuf *mbuf)
263277895Sarybchik{
264298955Spfg	/* Absence of TCP checksum flags does not mean that it is non-TCP
265277895Sarybchik	 * but it should be true if user wants to achieve high throughput.
266277895Sarybchik	 */
267277895Sarybchik	return (!(mbuf->m_pkthdr.csum_flags & (CSUM_IP_TCP | CSUM_IP6_TCP)));
268277895Sarybchik}
269277895Sarybchik
270227569Sphilip/*
271227569Sphilip * Reorder the put list and append it to the get list.
272227569Sphilip */
273227569Sphilipstatic void
274227569Sphilipsfxge_tx_qdpl_swizzle(struct sfxge_txq *txq)
275227569Sphilip{
276227569Sphilip	struct sfxge_tx_dpl *stdp;
277227569Sphilip	struct mbuf *mbuf, *get_next, **get_tailp;
278227569Sphilip	volatile uintptr_t *putp;
279227569Sphilip	uintptr_t put;
280227569Sphilip	unsigned int count;
281277895Sarybchik	unsigned int non_tcp_count;
282227569Sphilip
283278221Sarybchik	SFXGE_TXQ_LOCK_ASSERT_OWNED(txq);
284227569Sphilip
285227569Sphilip	stdp = &txq->dpl;
286227569Sphilip
287227569Sphilip	/* Acquire the put list. */
288227569Sphilip	putp = &stdp->std_put;
289227640Smarius	put = atomic_readandclear_ptr(putp);
290227569Sphilip	mbuf = (void *)put;
291227569Sphilip
292227569Sphilip	if (mbuf == NULL)
293227569Sphilip		return;
294227569Sphilip
295227569Sphilip	/* Reverse the put list. */
296227569Sphilip	get_tailp = &mbuf->m_nextpkt;
297227569Sphilip	get_next = NULL;
298227569Sphilip
299227569Sphilip	count = 0;
300277895Sarybchik	non_tcp_count = 0;
301227569Sphilip	do {
302227569Sphilip		struct mbuf *put_next;
303227569Sphilip
304277895Sarybchik		non_tcp_count += sfxge_is_mbuf_non_tcp(mbuf);
305227569Sphilip		put_next = mbuf->m_nextpkt;
306227569Sphilip		mbuf->m_nextpkt = get_next;
307227569Sphilip		get_next = mbuf;
308227569Sphilip		mbuf = put_next;
309227569Sphilip
310227569Sphilip		count++;
311227569Sphilip	} while (mbuf != NULL);
312227569Sphilip
313279231Sarybchik	if (count > stdp->std_put_hiwat)
314279231Sarybchik		stdp->std_put_hiwat = count;
315279231Sarybchik
316227569Sphilip	/* Append the reversed put list to the get list. */
317227569Sphilip	KASSERT(*get_tailp == NULL, ("*get_tailp != NULL"));
318227569Sphilip	*stdp->std_getp = get_next;
319227569Sphilip	stdp->std_getp = get_tailp;
320272330Sgnn	stdp->std_get_count += count;
321277895Sarybchik	stdp->std_get_non_tcp_count += non_tcp_count;
322227569Sphilip}
323227569Sphilip
324227569Sphilipstatic void
325227569Sphilipsfxge_tx_qreap(struct sfxge_txq *txq)
326227569Sphilip{
327278221Sarybchik	SFXGE_TXQ_LOCK_ASSERT_OWNED(txq);
328227569Sphilip
329227569Sphilip	txq->reaped = txq->completed;
330227569Sphilip}
331227569Sphilip
332227569Sphilipstatic void
333227569Sphilipsfxge_tx_qlist_post(struct sfxge_txq *txq)
334227569Sphilip{
335227569Sphilip	unsigned int old_added;
336283514Sarybchik	unsigned int block_level;
337227569Sphilip	unsigned int level;
338227569Sphilip	int rc;
339227569Sphilip
340278221Sarybchik	SFXGE_TXQ_LOCK_ASSERT_OWNED(txq);
341227569Sphilip
342227569Sphilip	KASSERT(txq->n_pend_desc != 0, ("txq->n_pend_desc == 0"));
343283514Sarybchik	KASSERT(txq->n_pend_desc <= txq->max_pkt_desc,
344227569Sphilip		("txq->n_pend_desc too large"));
345227569Sphilip	KASSERT(!txq->blocked, ("txq->blocked"));
346227569Sphilip
347227569Sphilip	old_added = txq->added;
348227569Sphilip
349227569Sphilip	/* Post the fragment list. */
350283514Sarybchik	rc = efx_tx_qdesc_post(txq->common, txq->pend_desc, txq->n_pend_desc,
351227569Sphilip			  txq->reaped, &txq->added);
352283514Sarybchik	KASSERT(rc == 0, ("efx_tx_qdesc_post() failed"));
353227569Sphilip
354283514Sarybchik	/* If efx_tx_qdesc_post() had to refragment, our information about
355227569Sphilip	 * buffers to free may be associated with the wrong
356227569Sphilip	 * descriptors.
357227569Sphilip	 */
358227569Sphilip	KASSERT(txq->added - old_added == txq->n_pend_desc,
359283514Sarybchik		("efx_tx_qdesc_post() refragmented descriptors"));
360227569Sphilip
361227569Sphilip	level = txq->added - txq->reaped;
362272328Sgnn	KASSERT(level <= txq->entries, ("overfilled TX queue"));
363227569Sphilip
364227569Sphilip	/* Clear the fragment list. */
365227569Sphilip	txq->n_pend_desc = 0;
366227569Sphilip
367283514Sarybchik	/*
368283514Sarybchik	 * Set the block level to ensure there is space to generate a
369283514Sarybchik	 * large number of descriptors for TSO.
370283514Sarybchik	 */
371283514Sarybchik	block_level = EFX_TXQ_LIMIT(txq->entries) - txq->max_pkt_desc;
372283514Sarybchik
373227569Sphilip	/* Have we reached the block level? */
374283514Sarybchik	if (level < block_level)
375227569Sphilip		return;
376227569Sphilip
377227569Sphilip	/* Reap, and check again */
378227569Sphilip	sfxge_tx_qreap(txq);
379227569Sphilip	level = txq->added - txq->reaped;
380283514Sarybchik	if (level < block_level)
381227569Sphilip		return;
382227569Sphilip
383227569Sphilip	txq->blocked = 1;
384227569Sphilip
385227569Sphilip	/*
386227569Sphilip	 * Avoid a race with completion interrupt handling that could leave
387227569Sphilip	 * the queue blocked.
388227569Sphilip	 */
389227569Sphilip	mb();
390227569Sphilip	sfxge_tx_qreap(txq);
391227569Sphilip	level = txq->added - txq->reaped;
392283514Sarybchik	if (level < block_level) {
393227569Sphilip		mb();
394227569Sphilip		txq->blocked = 0;
395227569Sphilip	}
396227569Sphilip}
397227569Sphilip
398227569Sphilipstatic int sfxge_tx_queue_mbuf(struct sfxge_txq *txq, struct mbuf *mbuf)
399227569Sphilip{
400227569Sphilip	bus_dmamap_t *used_map;
401227569Sphilip	bus_dmamap_t map;
402227569Sphilip	bus_dma_segment_t dma_seg[SFXGE_TX_MAPPING_MAX_SEG];
403227569Sphilip	unsigned int id;
404227569Sphilip	struct sfxge_tx_mapping *stmp;
405283514Sarybchik	efx_desc_t *desc;
406227569Sphilip	int n_dma_seg;
407227569Sphilip	int rc;
408227569Sphilip	int i;
409283514Sarybchik	int eop;
410342455Sarybchik	uint16_t hw_cksum_flags_prev;
411342451Sarybchik	uint16_t hw_vlan_tci_prev;
412342455Sarybchik	int n_extra_descs;
413227569Sphilip
414227569Sphilip	KASSERT(!txq->blocked, ("txq->blocked"));
415227569Sphilip
416312157Sarybchik#if SFXGE_TX_PARSE_EARLY
417312157Sarybchik	/*
418312157Sarybchik	 * If software TSO is used, we still need to copy packet header,
419312157Sarybchik	 * even if we have already parsed it early before enqueue.
420312157Sarybchik	 */
421312157Sarybchik	if ((mbuf->m_pkthdr.csum_flags & CSUM_TSO) &&
422312157Sarybchik	    (txq->tso_fw_assisted == 0))
423312157Sarybchik		prefetch_read_many(mbuf->m_data);
424312157Sarybchik#else
425312157Sarybchik	/*
426312157Sarybchik	 * Prefetch packet header since we need to parse it and extract
427312157Sarybchik	 * IP ID, TCP sequence number and flags.
428312157Sarybchik	 */
429227569Sphilip	if (mbuf->m_pkthdr.csum_flags & CSUM_TSO)
430227569Sphilip		prefetch_read_many(mbuf->m_data);
431312157Sarybchik#endif
432227569Sphilip
433279351Sarybchik	if (__predict_false(txq->init_state != SFXGE_TXQ_STARTED)) {
434227569Sphilip		rc = EINTR;
435227569Sphilip		goto reject;
436227569Sphilip	}
437227569Sphilip
438227569Sphilip	/* Load the packet for DMA. */
439272328Sgnn	id = txq->added & txq->ptr_mask;
440227569Sphilip	stmp = &txq->stmp[id];
441227569Sphilip	rc = bus_dmamap_load_mbuf_sg(txq->packet_dma_tag, stmp->map,
442227569Sphilip				     mbuf, dma_seg, &n_dma_seg, 0);
443227569Sphilip	if (rc == EFBIG) {
444227569Sphilip		/* Try again. */
445243857Sglebius		struct mbuf *new_mbuf = m_collapse(mbuf, M_NOWAIT,
446227569Sphilip						   SFXGE_TX_MAPPING_MAX_SEG);
447227569Sphilip		if (new_mbuf == NULL)
448227569Sphilip			goto reject;
449227569Sphilip		++txq->collapses;
450227569Sphilip		mbuf = new_mbuf;
451227569Sphilip		rc = bus_dmamap_load_mbuf_sg(txq->packet_dma_tag,
452227569Sphilip					     stmp->map, mbuf,
453227569Sphilip					     dma_seg, &n_dma_seg, 0);
454227569Sphilip	}
455227569Sphilip	if (rc != 0)
456227569Sphilip		goto reject;
457227569Sphilip
458227569Sphilip	/* Make the packet visible to the hardware. */
459227569Sphilip	bus_dmamap_sync(txq->packet_dma_tag, stmp->map, BUS_DMASYNC_PREWRITE);
460227569Sphilip
461227569Sphilip	used_map = &stmp->map;
462227569Sphilip
463342455Sarybchik	hw_cksum_flags_prev = txq->hw_cksum_flags;
464342451Sarybchik	hw_vlan_tci_prev = txq->hw_vlan_tci;
465342451Sarybchik
466342455Sarybchik	/*
467342455Sarybchik	 * The order of option descriptors, which are used to leverage VLAN tag
468342455Sarybchik	 * and checksum offloads, might be important. Changing checksum offload
469342455Sarybchik	 * between VLAN option and packet descriptors probably does not work.
470342455Sarybchik	 */
471342455Sarybchik	n_extra_descs = sfxge_tx_maybe_toggle_cksum_offload(txq, mbuf, &stmp);
472342455Sarybchik	n_extra_descs += sfxge_tx_maybe_insert_tag(txq, mbuf, &stmp);
473342455Sarybchik
474227569Sphilip	if (mbuf->m_pkthdr.csum_flags & CSUM_TSO) {
475342455Sarybchik		rc = sfxge_tx_queue_tso(txq, mbuf, dma_seg, n_dma_seg,
476342455Sarybchik					n_extra_descs);
477227569Sphilip		if (rc < 0)
478227569Sphilip			goto reject_mapped;
479283514Sarybchik		stmp = &txq->stmp[(rc - 1) & txq->ptr_mask];
480227569Sphilip	} else {
481227569Sphilip		/* Add the mapping to the fragment list, and set flags
482227569Sphilip		 * for the buffer.
483227569Sphilip		 */
484283514Sarybchik
485227569Sphilip		i = 0;
486227569Sphilip		for (;;) {
487342455Sarybchik			desc = &txq->pend_desc[i + n_extra_descs];
488283514Sarybchik			eop = (i == n_dma_seg - 1);
489283514Sarybchik			efx_tx_qdesc_dma_create(txq->common,
490283514Sarybchik						dma_seg[i].ds_addr,
491283514Sarybchik						dma_seg[i].ds_len,
492283514Sarybchik						eop,
493283514Sarybchik						desc);
494283514Sarybchik			if (eop)
495227569Sphilip				break;
496227569Sphilip			i++;
497283514Sarybchik			sfxge_next_stmp(txq, &stmp);
498227569Sphilip		}
499342455Sarybchik		txq->n_pend_desc = n_dma_seg + n_extra_descs;
500227569Sphilip	}
501227569Sphilip
502227569Sphilip	/*
503227569Sphilip	 * If the mapping required more than one descriptor
504227569Sphilip	 * then we need to associate the DMA map with the last
505227569Sphilip	 * descriptor, not the first.
506227569Sphilip	 */
507227569Sphilip	if (used_map != &stmp->map) {
508227569Sphilip		map = stmp->map;
509227569Sphilip		stmp->map = *used_map;
510227569Sphilip		*used_map = map;
511227569Sphilip	}
512227569Sphilip
513227569Sphilip	stmp->u.mbuf = mbuf;
514227569Sphilip	stmp->flags = TX_BUF_UNMAP | TX_BUF_MBUF;
515227569Sphilip
516227569Sphilip	/* Post the fragment list. */
517227569Sphilip	sfxge_tx_qlist_post(txq);
518227569Sphilip
519272325Sgnn	return (0);
520227569Sphilip
521227569Sphilipreject_mapped:
522342451Sarybchik	txq->hw_vlan_tci = hw_vlan_tci_prev;
523342455Sarybchik	txq->hw_cksum_flags = hw_cksum_flags_prev;
524227569Sphilip	bus_dmamap_unload(txq->packet_dma_tag, *used_map);
525227569Sphilipreject:
526227569Sphilip	/* Drop the packet on the floor. */
527227569Sphilip	m_freem(mbuf);
528227569Sphilip	++txq->drops;
529227569Sphilip
530272325Sgnn	return (rc);
531227569Sphilip}
532227569Sphilip
533227569Sphilip/*
534227569Sphilip * Drain the deferred packet list into the transmit queue.
535227569Sphilip */
536227569Sphilipstatic void
537227569Sphilipsfxge_tx_qdpl_drain(struct sfxge_txq *txq)
538227569Sphilip{
539227569Sphilip	struct sfxge_softc *sc;
540227569Sphilip	struct sfxge_tx_dpl *stdp;
541227569Sphilip	struct mbuf *mbuf, *next;
542227569Sphilip	unsigned int count;
543277895Sarybchik	unsigned int non_tcp_count;
544227569Sphilip	unsigned int pushed;
545227569Sphilip	int rc;
546227569Sphilip
547278221Sarybchik	SFXGE_TXQ_LOCK_ASSERT_OWNED(txq);
548227569Sphilip
549227569Sphilip	sc = txq->sc;
550227569Sphilip	stdp = &txq->dpl;
551227569Sphilip	pushed = txq->added;
552227569Sphilip
553280163Sarybchik	if (__predict_true(txq->init_state == SFXGE_TXQ_STARTED)) {
554280163Sarybchik		prefetch_read_many(sc->enp);
555280163Sarybchik		prefetch_read_many(txq->common);
556280163Sarybchik	}
557227569Sphilip
558227569Sphilip	mbuf = stdp->std_get;
559272330Sgnn	count = stdp->std_get_count;
560277895Sarybchik	non_tcp_count = stdp->std_get_non_tcp_count;
561227569Sphilip
562277895Sarybchik	if (count > stdp->std_get_hiwat)
563277895Sarybchik		stdp->std_get_hiwat = count;
564277895Sarybchik
565227569Sphilip	while (count != 0) {
566227569Sphilip		KASSERT(mbuf != NULL, ("mbuf == NULL"));
567227569Sphilip
568227569Sphilip		next = mbuf->m_nextpkt;
569227569Sphilip		mbuf->m_nextpkt = NULL;
570227569Sphilip
571227569Sphilip		ETHER_BPF_MTAP(sc->ifnet, mbuf); /* packet capture */
572227569Sphilip
573227569Sphilip		if (next != NULL)
574227569Sphilip			prefetch_read_many(next);
575227569Sphilip
576227569Sphilip		rc = sfxge_tx_queue_mbuf(txq, mbuf);
577227569Sphilip		--count;
578277895Sarybchik		non_tcp_count -= sfxge_is_mbuf_non_tcp(mbuf);
579227569Sphilip		mbuf = next;
580227569Sphilip		if (rc != 0)
581227569Sphilip			continue;
582227569Sphilip
583227569Sphilip		if (txq->blocked)
584227569Sphilip			break;
585227569Sphilip
586227569Sphilip		/* Push the fragments to the hardware in batches. */
587227569Sphilip		if (txq->added - pushed >= SFXGE_TX_BATCH) {
588283514Sarybchik			efx_tx_qpush(txq->common, txq->added, pushed);
589227569Sphilip			pushed = txq->added;
590227569Sphilip		}
591227569Sphilip	}
592227569Sphilip
593227569Sphilip	if (count == 0) {
594227569Sphilip		KASSERT(mbuf == NULL, ("mbuf != NULL"));
595277895Sarybchik		KASSERT(non_tcp_count == 0,
596277895Sarybchik			("inconsistent TCP/non-TCP detection"));
597227569Sphilip		stdp->std_get = NULL;
598272330Sgnn		stdp->std_get_count = 0;
599277895Sarybchik		stdp->std_get_non_tcp_count = 0;
600227569Sphilip		stdp->std_getp = &stdp->std_get;
601227569Sphilip	} else {
602227569Sphilip		stdp->std_get = mbuf;
603272330Sgnn		stdp->std_get_count = count;
604277895Sarybchik		stdp->std_get_non_tcp_count = non_tcp_count;
605227569Sphilip	}
606227569Sphilip
607227569Sphilip	if (txq->added != pushed)
608283514Sarybchik		efx_tx_qpush(txq->common, txq->added, pushed);
609227569Sphilip
610272330Sgnn	KASSERT(txq->blocked || stdp->std_get_count == 0,
611227569Sphilip		("queue unblocked but count is non-zero"));
612227569Sphilip}
613227569Sphilip
614283514Sarybchik#define	SFXGE_TX_QDPL_PENDING(_txq)	((_txq)->dpl.std_put != 0)
615227569Sphilip
616227569Sphilip/*
617227569Sphilip * Service the deferred packet list.
618227569Sphilip *
619227569Sphilip * NOTE: drops the txq mutex!
620227569Sphilip */
621278837Sarybchikstatic void
622227569Sphilipsfxge_tx_qdpl_service(struct sfxge_txq *txq)
623227569Sphilip{
624278221Sarybchik	SFXGE_TXQ_LOCK_ASSERT_OWNED(txq);
625227569Sphilip
626227569Sphilip	do {
627227569Sphilip		if (SFXGE_TX_QDPL_PENDING(txq))
628227569Sphilip			sfxge_tx_qdpl_swizzle(txq);
629227569Sphilip
630227569Sphilip		if (!txq->blocked)
631227569Sphilip			sfxge_tx_qdpl_drain(txq);
632227569Sphilip
633278221Sarybchik		SFXGE_TXQ_UNLOCK(txq);
634227569Sphilip	} while (SFXGE_TX_QDPL_PENDING(txq) &&
635278221Sarybchik		 SFXGE_TXQ_TRYLOCK(txq));
636227569Sphilip}
637227569Sphilip
638227569Sphilip/*
639282942Sarybchik * Put a packet on the deferred packet get-list.
640227569Sphilip */
641278837Sarybchikstatic int
642282942Sarybchiksfxge_tx_qdpl_put_locked(struct sfxge_txq *txq, struct mbuf *mbuf)
643227569Sphilip{
644227569Sphilip	struct sfxge_tx_dpl *stdp;
645227569Sphilip
646227569Sphilip	stdp = &txq->dpl;
647227569Sphilip
648227569Sphilip	KASSERT(mbuf->m_nextpkt == NULL, ("mbuf->m_nextpkt != NULL"));
649227569Sphilip
650282942Sarybchik	SFXGE_TXQ_LOCK_ASSERT_OWNED(txq);
651227569Sphilip
652282942Sarybchik	if (stdp->std_get_count >= stdp->std_get_max) {
653282942Sarybchik		txq->get_overflow++;
654282942Sarybchik		return (ENOBUFS);
655282942Sarybchik	}
656282942Sarybchik	if (sfxge_is_mbuf_non_tcp(mbuf)) {
657282942Sarybchik		if (stdp->std_get_non_tcp_count >=
658282942Sarybchik		    stdp->std_get_non_tcp_max) {
659282942Sarybchik			txq->get_non_tcp_overflow++;
660263649Sglebius			return (ENOBUFS);
661277895Sarybchik		}
662282942Sarybchik		stdp->std_get_non_tcp_count++;
663282942Sarybchik	}
664263649Sglebius
665282942Sarybchik	*(stdp->std_getp) = mbuf;
666282942Sarybchik	stdp->std_getp = &mbuf->m_nextpkt;
667282942Sarybchik	stdp->std_get_count++;
668227569Sphilip
669282942Sarybchik	return (0);
670282942Sarybchik}
671227569Sphilip
672282942Sarybchik/*
673282942Sarybchik * Put a packet on the deferred packet put-list.
674282942Sarybchik *
675282942Sarybchik * We overload the csum_data field in the mbuf to keep track of this length
676282942Sarybchik * because there is no cheap alternative to avoid races.
677282942Sarybchik */
678282942Sarybchikstatic int
679282942Sarybchiksfxge_tx_qdpl_put_unlocked(struct sfxge_txq *txq, struct mbuf *mbuf)
680282942Sarybchik{
681282942Sarybchik	struct sfxge_tx_dpl *stdp;
682282942Sarybchik	volatile uintptr_t *putp;
683282942Sarybchik	uintptr_t old;
684282942Sarybchik	uintptr_t new;
685311028Sarybchik	unsigned int put_count;
686227569Sphilip
687282942Sarybchik	KASSERT(mbuf->m_nextpkt == NULL, ("mbuf->m_nextpkt != NULL"));
688282942Sarybchik
689282942Sarybchik	SFXGE_TXQ_LOCK_ASSERT_NOTOWNED(txq);
690282942Sarybchik
691282942Sarybchik	stdp = &txq->dpl;
692282942Sarybchik	putp = &stdp->std_put;
693282942Sarybchik	new = (uintptr_t)mbuf;
694282942Sarybchik
695282942Sarybchik	do {
696282942Sarybchik		old = *putp;
697282942Sarybchik		if (old != 0) {
698282942Sarybchik			struct mbuf *mp = (struct mbuf *)old;
699311028Sarybchik			put_count = mp->m_pkthdr.csum_data;
700282942Sarybchik		} else
701311028Sarybchik			put_count = 0;
702311028Sarybchik		if (put_count >= stdp->std_put_max) {
703282942Sarybchik			atomic_add_long(&txq->put_overflow, 1);
704282942Sarybchik			return (ENOBUFS);
705282942Sarybchik		}
706311028Sarybchik		mbuf->m_pkthdr.csum_data = put_count + 1;
707282942Sarybchik		mbuf->m_nextpkt = (void *)old;
708282942Sarybchik	} while (atomic_cmpset_ptr(putp, old, new) == 0);
709282942Sarybchik
710227569Sphilip	return (0);
711227569Sphilip}
712227569Sphilip
713227569Sphilip/*
714227569Sphilip * Called from if_transmit - will try to grab the txq lock and enqueue to the
715279141Sarybchik * put list if it succeeds, otherwise try to push onto the defer list if space.
716227569Sphilip */
717283514Sarybchikstatic int
718227569Sphilipsfxge_tx_packet_add(struct sfxge_txq *txq, struct mbuf *m)
719227569Sphilip{
720227569Sphilip	int rc;
721227569Sphilip
722263332Sglebius	if (!SFXGE_LINK_UP(txq->sc)) {
723277895Sarybchik		atomic_add_long(&txq->netdown_drops, 1);
724282998Sarybchik		return (ENETDOWN);
725263332Sglebius	}
726263332Sglebius
727227569Sphilip	/*
728227569Sphilip	 * Try to grab the txq lock.  If we are able to get the lock,
729227569Sphilip	 * the packet will be appended to the "get list" of the deferred
730227569Sphilip	 * packet list.  Otherwise, it will be pushed on the "put list".
731227569Sphilip	 */
732282997Sarybchik	if (SFXGE_TXQ_TRYLOCK(txq)) {
733282942Sarybchik		/* First swizzle put-list to get-list to keep order */
734282942Sarybchik		sfxge_tx_qdpl_swizzle(txq);
735282942Sarybchik
736282942Sarybchik		rc = sfxge_tx_qdpl_put_locked(txq, m);
737282997Sarybchik
738282997Sarybchik		/* Try to service the list. */
739282997Sarybchik		sfxge_tx_qdpl_service(txq);
740282997Sarybchik		/* Lock has been dropped. */
741282942Sarybchik	} else {
742282942Sarybchik		rc = sfxge_tx_qdpl_put_unlocked(txq, m);
743227569Sphilip
744282942Sarybchik		/*
745282942Sarybchik		 * Try to grab the lock again.
746282942Sarybchik		 *
747282942Sarybchik		 * If we are able to get the lock, we need to process
748282942Sarybchik		 * the deferred packet list.  If we are not able to get
749282942Sarybchik		 * the lock, another thread is processing the list.
750282942Sarybchik		 */
751283048Sarybchik		if ((rc == 0) && SFXGE_TXQ_TRYLOCK(txq)) {
752282997Sarybchik			sfxge_tx_qdpl_service(txq);
753282997Sarybchik			/* Lock has been dropped. */
754282997Sarybchik		}
755282942Sarybchik	}
756227569Sphilip
757282997Sarybchik	SFXGE_TXQ_LOCK_ASSERT_NOTOWNED(txq);
758227569Sphilip
759283048Sarybchik	return (rc);
760227569Sphilip}
761227569Sphilip
762227569Sphilipstatic void
763227569Sphilipsfxge_tx_qdpl_flush(struct sfxge_txq *txq)
764227569Sphilip{
765227569Sphilip	struct sfxge_tx_dpl *stdp = &txq->dpl;
766227569Sphilip	struct mbuf *mbuf, *next;
767227569Sphilip
768278221Sarybchik	SFXGE_TXQ_LOCK(txq);
769227569Sphilip
770227569Sphilip	sfxge_tx_qdpl_swizzle(txq);
771227569Sphilip	for (mbuf = stdp->std_get; mbuf != NULL; mbuf = next) {
772227569Sphilip		next = mbuf->m_nextpkt;
773227569Sphilip		m_freem(mbuf);
774227569Sphilip	}
775227569Sphilip	stdp->std_get = NULL;
776272330Sgnn	stdp->std_get_count = 0;
777277895Sarybchik	stdp->std_get_non_tcp_count = 0;
778272325Sgnn	stdp->std_getp = &stdp->std_get;
779227569Sphilip
780278221Sarybchik	SFXGE_TXQ_UNLOCK(txq);
781227569Sphilip}
782227569Sphilip
783227569Sphilipvoid
784227569Sphilipsfxge_if_qflush(struct ifnet *ifp)
785227569Sphilip{
786227569Sphilip	struct sfxge_softc *sc;
787280433Sarybchik	unsigned int i;
788227569Sphilip
789227569Sphilip	sc = ifp->if_softc;
790227569Sphilip
791278938Sarybchik	for (i = 0; i < sc->txq_count; i++)
792227569Sphilip		sfxge_tx_qdpl_flush(sc->txq[i]);
793227569Sphilip}
794227569Sphilip
795291584Sarybchik#if SFXGE_TX_PARSE_EARLY
796291584Sarybchik
797291584Sarybchik/* There is little space for user data in mbuf pkthdr, so we
798291584Sarybchik * use l*hlen fields which are not used by the driver otherwise
799291584Sarybchik * to store header offsets.
800291584Sarybchik * The fields are 8-bit, but it's ok, no header may be longer than 255 bytes.
801291584Sarybchik */
802291584Sarybchik
803291584Sarybchik
804291584Sarybchik#define TSO_MBUF_PROTO(_mbuf)    ((_mbuf)->m_pkthdr.PH_loc.sixteen[0])
805291584Sarybchik/* We abuse l5hlen here because PH_loc can hold only 64 bits of data */
806291584Sarybchik#define TSO_MBUF_FLAGS(_mbuf)    ((_mbuf)->m_pkthdr.l5hlen)
807291584Sarybchik#define TSO_MBUF_PACKETID(_mbuf) ((_mbuf)->m_pkthdr.PH_loc.sixteen[1])
808291584Sarybchik#define TSO_MBUF_SEQNUM(_mbuf)   ((_mbuf)->m_pkthdr.PH_loc.thirtytwo[1])
809291584Sarybchik
810291584Sarybchikstatic void sfxge_parse_tx_packet(struct mbuf *mbuf)
811291584Sarybchik{
812291584Sarybchik	struct ether_header *eh = mtod(mbuf, struct ether_header *);
813291584Sarybchik	const struct tcphdr *th;
814291584Sarybchik	struct tcphdr th_copy;
815291584Sarybchik
816291584Sarybchik	/* Find network protocol and header */
817291584Sarybchik	TSO_MBUF_PROTO(mbuf) = eh->ether_type;
818291584Sarybchik	if (TSO_MBUF_PROTO(mbuf) == htons(ETHERTYPE_VLAN)) {
819291584Sarybchik		struct ether_vlan_header *veh =
820291584Sarybchik			mtod(mbuf, struct ether_vlan_header *);
821291584Sarybchik		TSO_MBUF_PROTO(mbuf) = veh->evl_proto;
822291584Sarybchik		mbuf->m_pkthdr.l2hlen = sizeof(*veh);
823291584Sarybchik	} else {
824291584Sarybchik		mbuf->m_pkthdr.l2hlen = sizeof(*eh);
825291584Sarybchik	}
826291584Sarybchik
827291584Sarybchik	/* Find TCP header */
828291584Sarybchik	if (TSO_MBUF_PROTO(mbuf) == htons(ETHERTYPE_IP)) {
829291584Sarybchik		const struct ip *iph = (const struct ip *)mtodo(mbuf, mbuf->m_pkthdr.l2hlen);
830291584Sarybchik
831291584Sarybchik		KASSERT(iph->ip_p == IPPROTO_TCP,
832291584Sarybchik			("TSO required on non-TCP packet"));
833291584Sarybchik		mbuf->m_pkthdr.l3hlen = mbuf->m_pkthdr.l2hlen + 4 * iph->ip_hl;
834291584Sarybchik		TSO_MBUF_PACKETID(mbuf) = iph->ip_id;
835291584Sarybchik	} else {
836291584Sarybchik		KASSERT(TSO_MBUF_PROTO(mbuf) == htons(ETHERTYPE_IPV6),
837291584Sarybchik			("TSO required on non-IP packet"));
838291584Sarybchik		KASSERT(((const struct ip6_hdr *)mtodo(mbuf, mbuf->m_pkthdr.l2hlen))->ip6_nxt ==
839291584Sarybchik			IPPROTO_TCP,
840291584Sarybchik			("TSO required on non-TCP packet"));
841291584Sarybchik		mbuf->m_pkthdr.l3hlen = mbuf->m_pkthdr.l2hlen + sizeof(struct ip6_hdr);
842291584Sarybchik		TSO_MBUF_PACKETID(mbuf) = 0;
843291584Sarybchik	}
844291584Sarybchik
845291584Sarybchik	KASSERT(mbuf->m_len >= mbuf->m_pkthdr.l3hlen,
846291584Sarybchik		("network header is fragmented in mbuf"));
847291584Sarybchik
848291584Sarybchik	/* We need TCP header including flags (window is the next) */
849291584Sarybchik	if (mbuf->m_len < mbuf->m_pkthdr.l3hlen + offsetof(struct tcphdr, th_win)) {
850291584Sarybchik		m_copydata(mbuf, mbuf->m_pkthdr.l3hlen, sizeof(th_copy),
851291584Sarybchik			   (caddr_t)&th_copy);
852291584Sarybchik		th = &th_copy;
853291584Sarybchik	} else {
854291584Sarybchik		th = (const struct tcphdr *)mtodo(mbuf, mbuf->m_pkthdr.l3hlen);
855291584Sarybchik	}
856291584Sarybchik
857291584Sarybchik	mbuf->m_pkthdr.l4hlen = mbuf->m_pkthdr.l3hlen + 4 * th->th_off;
858291584Sarybchik	TSO_MBUF_SEQNUM(mbuf) = ntohl(th->th_seq);
859291584Sarybchik
860291584Sarybchik	/* These flags must not be duplicated */
861291584Sarybchik	/*
862291584Sarybchik	 * RST should not be duplicated as well, but FreeBSD kernel
863291584Sarybchik	 * generates TSO packets with RST flag. So, do not assert
864291584Sarybchik	 * its absence.
865291584Sarybchik	 */
866291584Sarybchik	KASSERT(!(th->th_flags & (TH_URG | TH_SYN)),
867291584Sarybchik		("incompatible TCP flag 0x%x on TSO packet",
868291584Sarybchik		 th->th_flags & (TH_URG | TH_SYN)));
869291584Sarybchik	TSO_MBUF_FLAGS(mbuf) = th->th_flags;
870291584Sarybchik}
871291584Sarybchik#endif
872291584Sarybchik
873227569Sphilip/*
874227569Sphilip * TX start -- called by the stack.
875227569Sphilip */
876227569Sphilipint
877227569Sphilipsfxge_if_transmit(struct ifnet *ifp, struct mbuf *m)
878272325Sgnn{
879227569Sphilip	struct sfxge_softc *sc;
880227569Sphilip	struct sfxge_txq *txq;
881227569Sphilip	int rc;
882227569Sphilip
883227569Sphilip	sc = (struct sfxge_softc *)ifp->if_softc;
884227569Sphilip
885280374Sarybchik	/*
886280374Sarybchik	 * Transmit may be called when interface is up from the kernel
887280374Sarybchik	 * point of view, but not yet up (in progress) from the driver
888280374Sarybchik	 * point of view. I.e. link aggregation bring up.
889280374Sarybchik	 * Transmit may be called when interface is up from the driver
890280374Sarybchik	 * point of view, but already down from the kernel point of
891280374Sarybchik	 * view. I.e. Rx when interface shutdown is in progress.
892280374Sarybchik	 */
893280374Sarybchik	KASSERT((ifp->if_flags & IFF_UP) || (sc->if_flags & IFF_UP),
894280374Sarybchik		("interface not up"));
895227569Sphilip
896227569Sphilip	/* Pick the desired transmit queue. */
897342455Sarybchik	if (sc->txq_dynamic_cksum_toggle_supported |
898342455Sarybchik	    (m->m_pkthdr.csum_flags &
899342455Sarybchik	     (CSUM_DELAY_DATA | CSUM_TCP_IPV6 | CSUM_UDP_IPV6 | CSUM_TSO))) {
900227569Sphilip		int index = 0;
901227569Sphilip
902301494Sarybchik#ifdef RSS
903301494Sarybchik		uint32_t bucket_id;
904301494Sarybchik
905301494Sarybchik		/*
906301494Sarybchik		 * Select a TX queue which matches the corresponding
907301494Sarybchik		 * RX queue for the hash in order to assign both
908301494Sarybchik		 * TX and RX parts of the flow to the same CPU
909301494Sarybchik		 */
910301494Sarybchik		if (rss_m2bucket(m, &bucket_id) == 0)
911301494Sarybchik			index = bucket_id % (sc->txq_count - (SFXGE_TXQ_NTYPES - 1));
912301494Sarybchik#else
913275358Shselasky		/* check if flowid is set */
914275358Shselasky		if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) {
915227569Sphilip			uint32_t hash = m->m_pkthdr.flowid;
916310831Sarybchik			uint32_t idx = hash % nitems(sc->rx_indir_table);
917227569Sphilip
918310831Sarybchik			index = sc->rx_indir_table[idx];
919227569Sphilip		}
920301494Sarybchik#endif
921291584Sarybchik#if SFXGE_TX_PARSE_EARLY
922291584Sarybchik		if (m->m_pkthdr.csum_flags & CSUM_TSO)
923291584Sarybchik			sfxge_parse_tx_packet(m);
924291584Sarybchik#endif
925342455Sarybchik		index += (sc->txq_dynamic_cksum_toggle_supported == B_FALSE) ?
926342455Sarybchik			 SFXGE_TXQ_IP_TCP_UDP_CKSUM : 0;
927342455Sarybchik		txq = sc->txq[index];
928227569Sphilip	} else if (m->m_pkthdr.csum_flags & CSUM_DELAY_IP) {
929227569Sphilip		txq = sc->txq[SFXGE_TXQ_IP_CKSUM];
930227569Sphilip	} else {
931227569Sphilip		txq = sc->txq[SFXGE_TXQ_NON_CKSUM];
932227569Sphilip	}
933227569Sphilip
934227569Sphilip	rc = sfxge_tx_packet_add(txq, m);
935282998Sarybchik	if (rc != 0)
936282998Sarybchik		m_freem(m);
937227569Sphilip
938227569Sphilip	return (rc);
939227569Sphilip}
940227569Sphilip
941227569Sphilip/*
942227569Sphilip * Software "TSO".  Not quite as good as doing it in hardware, but
943227569Sphilip * still faster than segmenting in the stack.
944227569Sphilip */
945227569Sphilip
946227569Sphilipstruct sfxge_tso_state {
947227569Sphilip	/* Output position */
948227569Sphilip	unsigned out_len;	/* Remaining length in current segment */
949227569Sphilip	unsigned seqnum;	/* Current sequence number */
950227569Sphilip	unsigned packet_space;	/* Remaining space in current packet */
951294077Sarybchik	unsigned segs_space;	/* Remaining number of DMA segments
952294077Sarybchik				   for the packet (FATSOv2 only) */
953227569Sphilip
954227569Sphilip	/* Input position */
955227569Sphilip	uint64_t dma_addr;	/* DMA address of current position */
956227569Sphilip	unsigned in_len;	/* Remaining length in current mbuf */
957227569Sphilip
958227569Sphilip	const struct mbuf *mbuf; /* Input mbuf (head of chain) */
959227569Sphilip	u_short protocol;	/* Network protocol (after VLAN decap) */
960227569Sphilip	ssize_t nh_off;		/* Offset of network header */
961227569Sphilip	ssize_t tcph_off;	/* Offset of TCP header */
962227569Sphilip	unsigned header_len;	/* Number of bytes of header */
963278937Sarybchik	unsigned seg_size;	/* TCP segment size */
964283514Sarybchik	int fw_assisted;	/* Use FW-assisted TSO */
965283514Sarybchik	u_short packet_id;	/* IPv4 packet ID from the original packet */
966291584Sarybchik	uint8_t tcp_flags;	/* TCP flags */
967283514Sarybchik	efx_desc_t header_desc; /* Precomputed header descriptor for
968283514Sarybchik				 * FW-assisted TSO */
969227569Sphilip};
970227569Sphilip
971291584Sarybchik#if !SFXGE_TX_PARSE_EARLY
972278837Sarybchikstatic const struct ip *tso_iph(const struct sfxge_tso_state *tso)
973227569Sphilip{
974227569Sphilip	KASSERT(tso->protocol == htons(ETHERTYPE_IP),
975227569Sphilip		("tso_iph() in non-IPv4 state"));
976227569Sphilip	return (const struct ip *)(tso->mbuf->m_data + tso->nh_off);
977227569Sphilip}
978291584Sarybchik
979278837Sarybchikstatic __unused const struct ip6_hdr *tso_ip6h(const struct sfxge_tso_state *tso)
980227569Sphilip{
981227569Sphilip	KASSERT(tso->protocol == htons(ETHERTYPE_IPV6),
982227569Sphilip		("tso_ip6h() in non-IPv6 state"));
983227569Sphilip	return (const struct ip6_hdr *)(tso->mbuf->m_data + tso->nh_off);
984227569Sphilip}
985291584Sarybchik
986278837Sarybchikstatic const struct tcphdr *tso_tcph(const struct sfxge_tso_state *tso)
987227569Sphilip{
988227569Sphilip	return (const struct tcphdr *)(tso->mbuf->m_data + tso->tcph_off);
989227569Sphilip}
990291584Sarybchik#endif
991227569Sphilip
992291584Sarybchik
993227569Sphilip/* Size of preallocated TSO header buffers.  Larger blocks must be
994227569Sphilip * allocated from the heap.
995227569Sphilip */
996272325Sgnn#define	TSOH_STD_SIZE	128
997227569Sphilip
998227569Sphilip/* At most half the descriptors in the queue at any time will refer to
999227569Sphilip * a TSO header buffer, since they must always be followed by a
1000227569Sphilip * payload descriptor referring to an mbuf.
1001227569Sphilip */
1002272328Sgnn#define	TSOH_COUNT(_txq_entries)	((_txq_entries) / 2u)
1003272325Sgnn#define	TSOH_PER_PAGE	(PAGE_SIZE / TSOH_STD_SIZE)
1004272328Sgnn#define	TSOH_PAGE_COUNT(_txq_entries)	\
1005298646Spfg	howmany(TSOH_COUNT(_txq_entries), TSOH_PER_PAGE)
1006227569Sphilip
1007227569Sphilipstatic int tso_init(struct sfxge_txq *txq)
1008227569Sphilip{
1009227569Sphilip	struct sfxge_softc *sc = txq->sc;
1010272328Sgnn	unsigned int tsoh_page_count = TSOH_PAGE_COUNT(sc->txq_entries);
1011227569Sphilip	int i, rc;
1012227569Sphilip
1013227569Sphilip	/* Allocate TSO header buffers */
1014272328Sgnn	txq->tsoh_buffer = malloc(tsoh_page_count * sizeof(txq->tsoh_buffer[0]),
1015227569Sphilip				  M_SFXGE, M_WAITOK);
1016227569Sphilip
1017272328Sgnn	for (i = 0; i < tsoh_page_count; i++) {
1018227569Sphilip		rc = sfxge_dma_alloc(sc, PAGE_SIZE, &txq->tsoh_buffer[i]);
1019272325Sgnn		if (rc != 0)
1020227569Sphilip			goto fail;
1021227569Sphilip	}
1022227569Sphilip
1023272325Sgnn	return (0);
1024227569Sphilip
1025227569Sphilipfail:
1026227569Sphilip	while (i-- > 0)
1027227569Sphilip		sfxge_dma_free(&txq->tsoh_buffer[i]);
1028227569Sphilip	free(txq->tsoh_buffer, M_SFXGE);
1029227569Sphilip	txq->tsoh_buffer = NULL;
1030272325Sgnn	return (rc);
1031227569Sphilip}
1032227569Sphilip
1033227569Sphilipstatic void tso_fini(struct sfxge_txq *txq)
1034227569Sphilip{
1035227569Sphilip	int i;
1036227569Sphilip
1037272325Sgnn	if (txq->tsoh_buffer != NULL) {
1038272328Sgnn		for (i = 0; i < TSOH_PAGE_COUNT(txq->sc->txq_entries); i++)
1039227569Sphilip			sfxge_dma_free(&txq->tsoh_buffer[i]);
1040227569Sphilip		free(txq->tsoh_buffer, M_SFXGE);
1041227569Sphilip	}
1042227569Sphilip}
1043227569Sphilip
1044283514Sarybchikstatic void tso_start(struct sfxge_txq *txq, struct sfxge_tso_state *tso,
1045283514Sarybchik		      const bus_dma_segment_t *hdr_dma_seg,
1046283514Sarybchik		      struct mbuf *mbuf)
1047227569Sphilip{
1048291584Sarybchik	const efx_nic_cfg_t *encp = efx_nic_cfg_get(txq->sc->enp);
1049291584Sarybchik#if !SFXGE_TX_PARSE_EARLY
1050227569Sphilip	struct ether_header *eh = mtod(mbuf, struct ether_header *);
1051279046Sarybchik	const struct tcphdr *th;
1052279046Sarybchik	struct tcphdr th_copy;
1053291584Sarybchik#endif
1054227569Sphilip
1055294077Sarybchik	tso->fw_assisted = txq->tso_fw_assisted;
1056227569Sphilip	tso->mbuf = mbuf;
1057227569Sphilip
1058227569Sphilip	/* Find network protocol and header */
1059291584Sarybchik#if !SFXGE_TX_PARSE_EARLY
1060227569Sphilip	tso->protocol = eh->ether_type;
1061227569Sphilip	if (tso->protocol == htons(ETHERTYPE_VLAN)) {
1062227569Sphilip		struct ether_vlan_header *veh =
1063227569Sphilip			mtod(mbuf, struct ether_vlan_header *);
1064227569Sphilip		tso->protocol = veh->evl_proto;
1065227569Sphilip		tso->nh_off = sizeof(*veh);
1066227569Sphilip	} else {
1067227569Sphilip		tso->nh_off = sizeof(*eh);
1068227569Sphilip	}
1069291584Sarybchik#else
1070291584Sarybchik	tso->protocol = TSO_MBUF_PROTO(mbuf);
1071291584Sarybchik	tso->nh_off = mbuf->m_pkthdr.l2hlen;
1072291584Sarybchik	tso->tcph_off = mbuf->m_pkthdr.l3hlen;
1073301607Sarybchik	tso->packet_id = ntohs(TSO_MBUF_PACKETID(mbuf));
1074291584Sarybchik#endif
1075227569Sphilip
1076291584Sarybchik#if !SFXGE_TX_PARSE_EARLY
1077227569Sphilip	/* Find TCP header */
1078227569Sphilip	if (tso->protocol == htons(ETHERTYPE_IP)) {
1079227569Sphilip		KASSERT(tso_iph(tso)->ip_p == IPPROTO_TCP,
1080227569Sphilip			("TSO required on non-TCP packet"));
1081227569Sphilip		tso->tcph_off = tso->nh_off + 4 * tso_iph(tso)->ip_hl;
1082301607Sarybchik		tso->packet_id = ntohs(tso_iph(tso)->ip_id);
1083227569Sphilip	} else {
1084227569Sphilip		KASSERT(tso->protocol == htons(ETHERTYPE_IPV6),
1085227569Sphilip			("TSO required on non-IP packet"));
1086227569Sphilip		KASSERT(tso_ip6h(tso)->ip6_nxt == IPPROTO_TCP,
1087227569Sphilip			("TSO required on non-TCP packet"));
1088227569Sphilip		tso->tcph_off = tso->nh_off + sizeof(struct ip6_hdr);
1089283514Sarybchik		tso->packet_id = 0;
1090227569Sphilip	}
1091291584Sarybchik#endif
1092291584Sarybchik
1093291584Sarybchik
1094283514Sarybchik	if (tso->fw_assisted &&
1095283514Sarybchik	    __predict_false(tso->tcph_off >
1096283514Sarybchik			    encp->enc_tx_tso_tcp_header_offset_limit)) {
1097283514Sarybchik		tso->fw_assisted = 0;
1098283514Sarybchik	}
1099227569Sphilip
1100291584Sarybchik
1101291584Sarybchik#if !SFXGE_TX_PARSE_EARLY
1102279046Sarybchik	KASSERT(mbuf->m_len >= tso->tcph_off,
1103279046Sarybchik		("network header is fragmented in mbuf"));
1104279046Sarybchik	/* We need TCP header including flags (window is the next) */
1105279046Sarybchik	if (mbuf->m_len < tso->tcph_off + offsetof(struct tcphdr, th_win)) {
1106279046Sarybchik		m_copydata(tso->mbuf, tso->tcph_off, sizeof(th_copy),
1107279046Sarybchik			   (caddr_t)&th_copy);
1108279046Sarybchik		th = &th_copy;
1109279046Sarybchik	} else {
1110279046Sarybchik		th = tso_tcph(tso);
1111279046Sarybchik	}
1112279046Sarybchik	tso->header_len = tso->tcph_off + 4 * th->th_off;
1113291584Sarybchik#else
1114291584Sarybchik	tso->header_len = mbuf->m_pkthdr.l4hlen;
1115291584Sarybchik#endif
1116278937Sarybchik	tso->seg_size = mbuf->m_pkthdr.tso_segsz;
1117227569Sphilip
1118291584Sarybchik#if !SFXGE_TX_PARSE_EARLY
1119279046Sarybchik	tso->seqnum = ntohl(th->th_seq);
1120227569Sphilip
1121227569Sphilip	/* These flags must not be duplicated */
1122283278Sarybchik	/*
1123283278Sarybchik	 * RST should not be duplicated as well, but FreeBSD kernel
1124283278Sarybchik	 * generates TSO packets with RST flag. So, do not assert
1125283278Sarybchik	 * its absence.
1126283278Sarybchik	 */
1127283278Sarybchik	KASSERT(!(th->th_flags & (TH_URG | TH_SYN)),
1128283278Sarybchik		("incompatible TCP flag 0x%x on TSO packet",
1129283278Sarybchik		 th->th_flags & (TH_URG | TH_SYN)));
1130291584Sarybchik	tso->tcp_flags = th->th_flags;
1131291584Sarybchik#else
1132291584Sarybchik	tso->seqnum = TSO_MBUF_SEQNUM(mbuf);
1133291584Sarybchik	tso->tcp_flags = TSO_MBUF_FLAGS(mbuf);
1134291584Sarybchik#endif
1135227569Sphilip
1136227569Sphilip	tso->out_len = mbuf->m_pkthdr.len - tso->header_len;
1137283514Sarybchik
1138283514Sarybchik	if (tso->fw_assisted) {
1139283514Sarybchik		if (hdr_dma_seg->ds_len >= tso->header_len)
1140283514Sarybchik			efx_tx_qdesc_dma_create(txq->common,
1141283514Sarybchik						hdr_dma_seg->ds_addr,
1142283514Sarybchik						tso->header_len,
1143283514Sarybchik						B_FALSE,
1144283514Sarybchik						&tso->header_desc);
1145283514Sarybchik		else
1146283514Sarybchik			tso->fw_assisted = 0;
1147283514Sarybchik	}
1148227569Sphilip}
1149227569Sphilip
1150227569Sphilip/*
1151227569Sphilip * tso_fill_packet_with_fragment - form descriptors for the current fragment
1152227569Sphilip *
1153227569Sphilip * Form descriptors for the current fragment, until we reach the end
1154227569Sphilip * of fragment or end-of-packet.  Return 0 on success, 1 if not enough
1155227569Sphilip * space.
1156227569Sphilip */
1157227569Sphilipstatic void tso_fill_packet_with_fragment(struct sfxge_txq *txq,
1158227569Sphilip					  struct sfxge_tso_state *tso)
1159227569Sphilip{
1160283514Sarybchik	efx_desc_t *desc;
1161227569Sphilip	int n;
1162294077Sarybchik	uint64_t dma_addr = tso->dma_addr;
1163294077Sarybchik	boolean_t eop;
1164227569Sphilip
1165227569Sphilip	if (tso->in_len == 0 || tso->packet_space == 0)
1166227569Sphilip		return;
1167227569Sphilip
1168227569Sphilip	KASSERT(tso->in_len > 0, ("TSO input length went negative"));
1169227569Sphilip	KASSERT(tso->packet_space > 0, ("TSO packet space went negative"));
1170227569Sphilip
1171294077Sarybchik	if (tso->fw_assisted & SFXGE_FATSOV2) {
1172294077Sarybchik		n = tso->in_len;
1173294077Sarybchik		tso->out_len -= n;
1174294077Sarybchik		tso->seqnum += n;
1175294077Sarybchik		tso->in_len = 0;
1176294077Sarybchik		if (n < tso->packet_space) {
1177294077Sarybchik			tso->packet_space -= n;
1178294077Sarybchik			tso->segs_space--;
1179294077Sarybchik		} else {
1180294077Sarybchik			tso->packet_space = tso->seg_size -
1181294077Sarybchik			    (n - tso->packet_space) % tso->seg_size;
1182294077Sarybchik			tso->segs_space =
1183294077Sarybchik			    EFX_TX_FATSOV2_DMA_SEGS_PER_PKT_MAX - 1 -
1184294077Sarybchik			    (tso->packet_space != tso->seg_size);
1185294077Sarybchik		}
1186294077Sarybchik	} else {
1187294077Sarybchik		n = min(tso->in_len, tso->packet_space);
1188294077Sarybchik		tso->packet_space -= n;
1189294077Sarybchik		tso->out_len -= n;
1190294077Sarybchik		tso->dma_addr += n;
1191294077Sarybchik		tso->in_len -= n;
1192294077Sarybchik	}
1193227569Sphilip
1194294077Sarybchik	/*
1195294077Sarybchik	 * It is OK to use binary OR below to avoid extra branching
1196294077Sarybchik	 * since all conditions may always be checked.
1197294077Sarybchik	 */
1198294077Sarybchik	eop = (tso->out_len == 0) | (tso->packet_space == 0) |
1199294077Sarybchik	    (tso->segs_space == 0);
1200227569Sphilip
1201227569Sphilip	desc = &txq->pend_desc[txq->n_pend_desc++];
1202294077Sarybchik	efx_tx_qdesc_dma_create(txq->common, dma_addr, n, eop, desc);
1203227569Sphilip}
1204227569Sphilip
1205227569Sphilip/* Callback from bus_dmamap_load() for long TSO headers. */
1206227569Sphilipstatic void tso_map_long_header(void *dma_addr_ret,
1207227569Sphilip				bus_dma_segment_t *segs, int nseg,
1208227569Sphilip				int error)
1209227569Sphilip{
1210227569Sphilip	*(uint64_t *)dma_addr_ret = ((__predict_true(error == 0) &&
1211227569Sphilip				      __predict_true(nseg == 1)) ?
1212227569Sphilip				     segs->ds_addr : 0);
1213227569Sphilip}
1214227569Sphilip
1215227569Sphilip/*
1216227569Sphilip * tso_start_new_packet - generate a new header and prepare for the new packet
1217227569Sphilip *
1218227569Sphilip * Generate a new header and prepare for the new packet.  Return 0 on
1219227569Sphilip * success, or an error code if failed to alloc header.
1220227569Sphilip */
1221227569Sphilipstatic int tso_start_new_packet(struct sfxge_txq *txq,
1222227569Sphilip				struct sfxge_tso_state *tso,
1223283514Sarybchik				unsigned int *idp)
1224227569Sphilip{
1225283514Sarybchik	unsigned int id = *idp;
1226227569Sphilip	struct tcphdr *tsoh_th;
1227227569Sphilip	unsigned ip_length;
1228227569Sphilip	caddr_t header;
1229227569Sphilip	uint64_t dma_addr;
1230227569Sphilip	bus_dmamap_t map;
1231283514Sarybchik	efx_desc_t *desc;
1232227569Sphilip	int rc;
1233227569Sphilip
1234283514Sarybchik	if (tso->fw_assisted) {
1235294077Sarybchik		if (tso->fw_assisted & SFXGE_FATSOV2) {
1236294077Sarybchik			/* Add 2 FATSOv2 option descriptors */
1237294077Sarybchik			desc = &txq->pend_desc[txq->n_pend_desc];
1238294077Sarybchik			efx_tx_qdesc_tso2_create(txq->common,
1239294077Sarybchik						 tso->packet_id,
1240294077Sarybchik						 tso->seqnum,
1241294077Sarybchik						 tso->seg_size,
1242294077Sarybchik						 desc,
1243294077Sarybchik						 EFX_TX_FATSOV2_OPT_NDESCS);
1244294077Sarybchik			desc += EFX_TX_FATSOV2_OPT_NDESCS;
1245294077Sarybchik			txq->n_pend_desc += EFX_TX_FATSOV2_OPT_NDESCS;
1246294077Sarybchik			KASSERT(txq->stmp[id].flags == 0, ("stmp flags are not 0"));
1247294077Sarybchik			id = (id + EFX_TX_FATSOV2_OPT_NDESCS) & txq->ptr_mask;
1248227569Sphilip
1249294077Sarybchik			tso->segs_space =
1250294077Sarybchik			    EFX_TX_FATSOV2_DMA_SEGS_PER_PKT_MAX - 1;
1251294077Sarybchik		} else {
1252294077Sarybchik			uint8_t tcp_flags = tso->tcp_flags;
1253227569Sphilip
1254294077Sarybchik			if (tso->out_len > tso->seg_size)
1255294077Sarybchik				tcp_flags &= ~(TH_FIN | TH_PUSH);
1256283514Sarybchik
1257294077Sarybchik			/* Add FATSOv1 option descriptor */
1258294077Sarybchik			desc = &txq->pend_desc[txq->n_pend_desc++];
1259294077Sarybchik			efx_tx_qdesc_tso_create(txq->common,
1260294077Sarybchik						tso->packet_id,
1261294077Sarybchik						tso->seqnum,
1262294077Sarybchik						tcp_flags,
1263294077Sarybchik						desc++);
1264294077Sarybchik			KASSERT(txq->stmp[id].flags == 0, ("stmp flags are not 0"));
1265294077Sarybchik			id = (id + 1) & txq->ptr_mask;
1266294077Sarybchik
1267294077Sarybchik			tso->seqnum += tso->seg_size;
1268294077Sarybchik			tso->segs_space = UINT_MAX;
1269294077Sarybchik		}
1270294077Sarybchik
1271283514Sarybchik		/* Header DMA descriptor */
1272283514Sarybchik		*desc = tso->header_desc;
1273283514Sarybchik		txq->n_pend_desc++;
1274283514Sarybchik		KASSERT(txq->stmp[id].flags == 0, ("stmp flags are not 0"));
1275283514Sarybchik		id = (id + 1) & txq->ptr_mask;
1276227569Sphilip	} else {
1277283514Sarybchik		/* Allocate a DMA-mapped header buffer. */
1278283514Sarybchik		if (__predict_true(tso->header_len <= TSOH_STD_SIZE)) {
1279283514Sarybchik			unsigned int page_index = (id / 2) / TSOH_PER_PAGE;
1280283514Sarybchik			unsigned int buf_index = (id / 2) % TSOH_PER_PAGE;
1281283514Sarybchik
1282283514Sarybchik			header = (txq->tsoh_buffer[page_index].esm_base +
1283283514Sarybchik				  buf_index * TSOH_STD_SIZE);
1284283514Sarybchik			dma_addr = (txq->tsoh_buffer[page_index].esm_addr +
1285283514Sarybchik				    buf_index * TSOH_STD_SIZE);
1286283514Sarybchik			map = txq->tsoh_buffer[page_index].esm_map;
1287283514Sarybchik
1288283514Sarybchik			KASSERT(txq->stmp[id].flags == 0,
1289283514Sarybchik				("stmp flags are not 0"));
1290283514Sarybchik		} else {
1291283514Sarybchik			struct sfxge_tx_mapping *stmp = &txq->stmp[id];
1292283514Sarybchik
1293283514Sarybchik			/* We cannot use bus_dmamem_alloc() as that may sleep */
1294283514Sarybchik			header = malloc(tso->header_len, M_SFXGE, M_NOWAIT);
1295283514Sarybchik			if (__predict_false(!header))
1296283514Sarybchik				return (ENOMEM);
1297283514Sarybchik			rc = bus_dmamap_load(txq->packet_dma_tag, stmp->map,
1298283514Sarybchik					     header, tso->header_len,
1299283514Sarybchik					     tso_map_long_header, &dma_addr,
1300283514Sarybchik					     BUS_DMA_NOWAIT);
1301283514Sarybchik			if (__predict_false(dma_addr == 0)) {
1302283514Sarybchik				if (rc == 0) {
1303283514Sarybchik					/* Succeeded but got >1 segment */
1304283514Sarybchik					bus_dmamap_unload(txq->packet_dma_tag,
1305283514Sarybchik							  stmp->map);
1306283514Sarybchik					rc = EINVAL;
1307283514Sarybchik				}
1308283514Sarybchik				free(header, M_SFXGE);
1309283514Sarybchik				return (rc);
1310227569Sphilip			}
1311283514Sarybchik			map = stmp->map;
1312283514Sarybchik
1313283514Sarybchik			txq->tso_long_headers++;
1314283514Sarybchik			stmp->u.heap_buf = header;
1315283514Sarybchik			stmp->flags = TX_BUF_UNMAP;
1316227569Sphilip		}
1317227569Sphilip
1318283514Sarybchik		tsoh_th = (struct tcphdr *)(header + tso->tcph_off);
1319227569Sphilip
1320283514Sarybchik		/* Copy and update the headers. */
1321283514Sarybchik		m_copydata(tso->mbuf, 0, tso->header_len, header);
1322227569Sphilip
1323283514Sarybchik		tsoh_th->th_seq = htonl(tso->seqnum);
1324283514Sarybchik		tso->seqnum += tso->seg_size;
1325283514Sarybchik		if (tso->out_len > tso->seg_size) {
1326283514Sarybchik			/* This packet will not finish the TSO burst. */
1327283514Sarybchik			ip_length = tso->header_len - tso->nh_off + tso->seg_size;
1328283514Sarybchik			tsoh_th->th_flags &= ~(TH_FIN | TH_PUSH);
1329283514Sarybchik		} else {
1330283514Sarybchik			/* This packet will be the last in the TSO burst. */
1331283514Sarybchik			ip_length = tso->header_len - tso->nh_off + tso->out_len;
1332283514Sarybchik		}
1333227569Sphilip
1334283514Sarybchik		if (tso->protocol == htons(ETHERTYPE_IP)) {
1335283514Sarybchik			struct ip *tsoh_iph = (struct ip *)(header + tso->nh_off);
1336283514Sarybchik			tsoh_iph->ip_len = htons(ip_length);
1337283514Sarybchik			/* XXX We should increment ip_id, but FreeBSD doesn't
1338283514Sarybchik			 * currently allocate extra IDs for multiple segments.
1339283514Sarybchik			 */
1340283514Sarybchik		} else {
1341283514Sarybchik			struct ip6_hdr *tsoh_iph =
1342283514Sarybchik				(struct ip6_hdr *)(header + tso->nh_off);
1343283514Sarybchik			tsoh_iph->ip6_plen = htons(ip_length - sizeof(*tsoh_iph));
1344283514Sarybchik		}
1345227569Sphilip
1346283514Sarybchik		/* Make the header visible to the hardware. */
1347283514Sarybchik		bus_dmamap_sync(txq->packet_dma_tag, map, BUS_DMASYNC_PREWRITE);
1348283514Sarybchik
1349283514Sarybchik		/* Form a descriptor for this header. */
1350283514Sarybchik		desc = &txq->pend_desc[txq->n_pend_desc++];
1351283514Sarybchik		efx_tx_qdesc_dma_create(txq->common,
1352283514Sarybchik					dma_addr,
1353283514Sarybchik					tso->header_len,
1354283514Sarybchik					0,
1355283514Sarybchik					desc);
1356283514Sarybchik		id = (id + 1) & txq->ptr_mask;
1357294077Sarybchik
1358294077Sarybchik		tso->segs_space = UINT_MAX;
1359227569Sphilip	}
1360278937Sarybchik	tso->packet_space = tso->seg_size;
1361227569Sphilip	txq->tso_packets++;
1362283514Sarybchik	*idp = id;
1363227569Sphilip
1364272325Sgnn	return (0);
1365227569Sphilip}
1366227569Sphilip
1367227569Sphilipstatic int
1368227569Sphilipsfxge_tx_queue_tso(struct sfxge_txq *txq, struct mbuf *mbuf,
1369283514Sarybchik		   const bus_dma_segment_t *dma_seg, int n_dma_seg,
1370342455Sarybchik		   int n_extra_descs)
1371227569Sphilip{
1372227569Sphilip	struct sfxge_tso_state tso;
1373283514Sarybchik	unsigned int id;
1374277888Sarybchik	unsigned skipped = 0;
1375227569Sphilip
1376283514Sarybchik	tso_start(txq, &tso, dma_seg, mbuf);
1377227569Sphilip
1378277888Sarybchik	while (dma_seg->ds_len + skipped <= tso.header_len) {
1379277888Sarybchik		skipped += dma_seg->ds_len;
1380227569Sphilip		--n_dma_seg;
1381227569Sphilip		KASSERT(n_dma_seg, ("no payload found in TSO packet"));
1382227569Sphilip		++dma_seg;
1383227569Sphilip	}
1384280807Sarybchik	tso.in_len = dma_seg->ds_len - (tso.header_len - skipped);
1385277888Sarybchik	tso.dma_addr = dma_seg->ds_addr + (tso.header_len - skipped);
1386227569Sphilip
1387342455Sarybchik	id = (txq->added + n_extra_descs) & txq->ptr_mask;
1388283514Sarybchik	if (__predict_false(tso_start_new_packet(txq, &tso, &id)))
1389272328Sgnn		return (-1);
1390227569Sphilip
1391227569Sphilip	while (1) {
1392283514Sarybchik		tso_fill_packet_with_fragment(txq, &tso);
1393283514Sarybchik		/* Exactly one DMA descriptor is added */
1394283514Sarybchik		KASSERT(txq->stmp[id].flags == 0, ("stmp flags are not 0"));
1395272328Sgnn		id = (id + 1) & txq->ptr_mask;
1396227569Sphilip
1397227569Sphilip		/* Move onto the next fragment? */
1398227569Sphilip		if (tso.in_len == 0) {
1399227569Sphilip			--n_dma_seg;
1400227569Sphilip			if (n_dma_seg == 0)
1401227569Sphilip				break;
1402227569Sphilip			++dma_seg;
1403227569Sphilip			tso.in_len = dma_seg->ds_len;
1404227569Sphilip			tso.dma_addr = dma_seg->ds_addr;
1405227569Sphilip		}
1406227569Sphilip
1407227569Sphilip		/* End of packet? */
1408294077Sarybchik		if ((tso.packet_space == 0) | (tso.segs_space == 0)) {
1409294077Sarybchik			unsigned int n_fatso_opt_desc =
1410294077Sarybchik			    (tso.fw_assisted & SFXGE_FATSOV2) ?
1411294077Sarybchik			    EFX_TX_FATSOV2_OPT_NDESCS :
1412294077Sarybchik			    (tso.fw_assisted & SFXGE_FATSOV1) ? 1 : 0;
1413294077Sarybchik
1414227569Sphilip			/* If the queue is now full due to tiny MSS,
1415227569Sphilip			 * or we can't create another header, discard
1416227569Sphilip			 * the remainder of the input mbuf but do not
1417227569Sphilip			 * roll back the work we have done.
1418227569Sphilip			 */
1419294077Sarybchik			if (txq->n_pend_desc + n_fatso_opt_desc +
1420294077Sarybchik			    1 /* header */ + n_dma_seg > txq->max_pkt_desc) {
1421278255Sarybchik				txq->tso_pdrop_too_many++;
1422227569Sphilip				break;
1423278255Sarybchik			}
1424227569Sphilip			if (__predict_false(tso_start_new_packet(txq, &tso,
1425283514Sarybchik								 &id))) {
1426278255Sarybchik				txq->tso_pdrop_no_rsrc++;
1427227569Sphilip				break;
1428278255Sarybchik			}
1429227569Sphilip		}
1430227569Sphilip	}
1431227569Sphilip
1432227569Sphilip	txq->tso_bursts++;
1433272325Sgnn	return (id);
1434227569Sphilip}
1435227569Sphilip
1436227569Sphilipstatic void
1437227569Sphilipsfxge_tx_qunblock(struct sfxge_txq *txq)
1438227569Sphilip{
1439227569Sphilip	struct sfxge_softc *sc;
1440227569Sphilip	struct sfxge_evq *evq;
1441227569Sphilip
1442227569Sphilip	sc = txq->sc;
1443227569Sphilip	evq = sc->evq[txq->evq_index];
1444227569Sphilip
1445278221Sarybchik	SFXGE_EVQ_LOCK_ASSERT_OWNED(evq);
1446227569Sphilip
1447279351Sarybchik	if (__predict_false(txq->init_state != SFXGE_TXQ_STARTED))
1448227569Sphilip		return;
1449227569Sphilip
1450278221Sarybchik	SFXGE_TXQ_LOCK(txq);
1451227569Sphilip
1452227569Sphilip	if (txq->blocked) {
1453227569Sphilip		unsigned int level;
1454227569Sphilip
1455227569Sphilip		level = txq->added - txq->completed;
1456279080Sarybchik		if (level <= SFXGE_TXQ_UNBLOCK_LEVEL(txq->entries)) {
1457279080Sarybchik			/* reaped must be in sync with blocked */
1458279080Sarybchik			sfxge_tx_qreap(txq);
1459227569Sphilip			txq->blocked = 0;
1460279080Sarybchik		}
1461227569Sphilip	}
1462227569Sphilip
1463227569Sphilip	sfxge_tx_qdpl_service(txq);
1464227569Sphilip	/* note: lock has been dropped */
1465227569Sphilip}
1466227569Sphilip
1467227569Sphilipvoid
1468227569Sphilipsfxge_tx_qflush_done(struct sfxge_txq *txq)
1469227569Sphilip{
1470227569Sphilip
1471227569Sphilip	txq->flush_state = SFXGE_FLUSH_DONE;
1472227569Sphilip}
1473227569Sphilip
1474227569Sphilipstatic void
1475227569Sphilipsfxge_tx_qstop(struct sfxge_softc *sc, unsigned int index)
1476227569Sphilip{
1477227569Sphilip	struct sfxge_txq *txq;
1478227569Sphilip	struct sfxge_evq *evq;
1479227569Sphilip	unsigned int count;
1480227569Sphilip
1481283514Sarybchik	SFXGE_ADAPTER_LOCK_ASSERT_OWNED(sc);
1482283514Sarybchik
1483227569Sphilip	txq = sc->txq[index];
1484227569Sphilip	evq = sc->evq[txq->evq_index];
1485227569Sphilip
1486283514Sarybchik	SFXGE_EVQ_LOCK(evq);
1487278221Sarybchik	SFXGE_TXQ_LOCK(txq);
1488227569Sphilip
1489227569Sphilip	KASSERT(txq->init_state == SFXGE_TXQ_STARTED,
1490227569Sphilip	    ("txq->init_state != SFXGE_TXQ_STARTED"));
1491227569Sphilip
1492227569Sphilip	txq->init_state = SFXGE_TXQ_INITIALIZED;
1493227569Sphilip
1494283514Sarybchik	if (txq->flush_state != SFXGE_FLUSH_DONE) {
1495283514Sarybchik		txq->flush_state = SFXGE_FLUSH_PENDING;
1496227569Sphilip
1497283514Sarybchik		SFXGE_EVQ_UNLOCK(evq);
1498283514Sarybchik		SFXGE_TXQ_UNLOCK(txq);
1499227569Sphilip
1500283514Sarybchik		/* Flush the transmit queue. */
1501283514Sarybchik		if (efx_tx_qflush(txq->common) != 0) {
1502283514Sarybchik			log(LOG_ERR, "%s: Flushing Tx queue %u failed\n",
1503283514Sarybchik			    device_get_nameunit(sc->dev), index);
1504283514Sarybchik			txq->flush_state = SFXGE_FLUSH_DONE;
1505283514Sarybchik		} else {
1506283514Sarybchik			count = 0;
1507283514Sarybchik			do {
1508283514Sarybchik				/* Spin for 100ms. */
1509283514Sarybchik				DELAY(100000);
1510283514Sarybchik				if (txq->flush_state != SFXGE_FLUSH_PENDING)
1511283514Sarybchik					break;
1512283514Sarybchik			} while (++count < 20);
1513283514Sarybchik		}
1514283514Sarybchik		SFXGE_EVQ_LOCK(evq);
1515283514Sarybchik		SFXGE_TXQ_LOCK(txq);
1516227569Sphilip
1517283514Sarybchik		KASSERT(txq->flush_state != SFXGE_FLUSH_FAILED,
1518283514Sarybchik		    ("txq->flush_state == SFXGE_FLUSH_FAILED"));
1519227569Sphilip
1520283514Sarybchik		if (txq->flush_state != SFXGE_FLUSH_DONE) {
1521283514Sarybchik			/* Flush timeout */
1522283514Sarybchik			log(LOG_ERR, "%s: Cannot flush Tx queue %u\n",
1523283514Sarybchik			    device_get_nameunit(sc->dev), index);
1524283514Sarybchik			txq->flush_state = SFXGE_FLUSH_DONE;
1525283514Sarybchik		}
1526283514Sarybchik	}
1527227569Sphilip
1528227569Sphilip	txq->blocked = 0;
1529227569Sphilip	txq->pending = txq->added;
1530227569Sphilip
1531277889Sarybchik	sfxge_tx_qcomplete(txq, evq);
1532227569Sphilip	KASSERT(txq->completed == txq->added,
1533227569Sphilip	    ("txq->completed != txq->added"));
1534227569Sphilip
1535227569Sphilip	sfxge_tx_qreap(txq);
1536227569Sphilip	KASSERT(txq->reaped == txq->completed,
1537227569Sphilip	    ("txq->reaped != txq->completed"));
1538227569Sphilip
1539227569Sphilip	txq->added = 0;
1540227569Sphilip	txq->pending = 0;
1541227569Sphilip	txq->completed = 0;
1542227569Sphilip	txq->reaped = 0;
1543227569Sphilip
1544227569Sphilip	/* Destroy the common code transmit queue. */
1545227569Sphilip	efx_tx_qdestroy(txq->common);
1546227569Sphilip	txq->common = NULL;
1547227569Sphilip
1548227569Sphilip	efx_sram_buf_tbl_clear(sc->enp, txq->buf_base_id,
1549272328Sgnn	    EFX_TXQ_NBUFS(sc->txq_entries));
1550227569Sphilip
1551342455Sarybchik	txq->hw_cksum_flags = 0;
1552342455Sarybchik
1553278221Sarybchik	SFXGE_EVQ_UNLOCK(evq);
1554278221Sarybchik	SFXGE_TXQ_UNLOCK(txq);
1555227569Sphilip}
1556227569Sphilip
1557294077Sarybchik/*
1558294077Sarybchik * Estimate maximum number of Tx descriptors required for TSO packet.
1559294077Sarybchik * With minimum MSS and maximum mbuf length we might need more (even
1560294077Sarybchik * than a ring-ful of descriptors), but this should not happen in
1561294077Sarybchik * practice except due to deliberate attack.  In that case we will
1562294077Sarybchik * truncate the output at a packet boundary.
1563294077Sarybchik */
1564294077Sarybchikstatic unsigned int
1565294077Sarybchiksfxge_tx_max_pkt_desc(const struct sfxge_softc *sc, enum sfxge_txq_type type,
1566294077Sarybchik		      unsigned int tso_fw_assisted)
1567294077Sarybchik{
1568294077Sarybchik	/* One descriptor for every input fragment */
1569294077Sarybchik	unsigned int max_descs = SFXGE_TX_MAPPING_MAX_SEG;
1570294077Sarybchik	unsigned int sw_tso_max_descs;
1571294077Sarybchik	unsigned int fa_tso_v1_max_descs = 0;
1572294077Sarybchik	unsigned int fa_tso_v2_max_descs = 0;
1573294077Sarybchik
1574342455Sarybchik	/* Checksum offload Tx option descriptor may be required */
1575342455Sarybchik	if (sc->txq_dynamic_cksum_toggle_supported)
1576342455Sarybchik		max_descs++;
1577342455Sarybchik
1578294077Sarybchik	/* VLAN tagging Tx option descriptor may be required */
1579294077Sarybchik	if (efx_nic_cfg_get(sc->enp)->enc_hw_tx_insert_vlan_enabled)
1580294077Sarybchik		max_descs++;
1581294077Sarybchik
1582294077Sarybchik	if (type == SFXGE_TXQ_IP_TCP_UDP_CKSUM) {
1583294077Sarybchik		/*
1584294077Sarybchik		 * Plus header and payload descriptor for each output segment.
1585294077Sarybchik		 * Minus one since header fragment is already counted.
1586294077Sarybchik		 * Even if FATSO is used, we should be ready to fallback
1587294077Sarybchik		 * to do it in the driver.
1588294077Sarybchik		 */
1589294077Sarybchik		sw_tso_max_descs = SFXGE_TSO_MAX_SEGS * 2 - 1;
1590294077Sarybchik
1591294077Sarybchik		/* FW assisted TSOv1 requires one more descriptor per segment
1592294077Sarybchik		 * in comparison to SW TSO */
1593294077Sarybchik		if (tso_fw_assisted & SFXGE_FATSOV1)
1594294077Sarybchik			fa_tso_v1_max_descs =
1595294077Sarybchik			    sw_tso_max_descs + SFXGE_TSO_MAX_SEGS;
1596294077Sarybchik
1597294077Sarybchik		/* FW assisted TSOv2 requires 3 (2 FATSO plus header) extra
1598294077Sarybchik		 * descriptors per superframe limited by number of DMA fetches
1599294077Sarybchik		 * per packet. The first packet header is already counted.
1600294077Sarybchik		 */
1601294077Sarybchik		if (tso_fw_assisted & SFXGE_FATSOV2) {
1602294077Sarybchik			fa_tso_v2_max_descs =
1603294077Sarybchik			    howmany(SFXGE_TX_MAPPING_MAX_SEG,
1604294077Sarybchik				    EFX_TX_FATSOV2_DMA_SEGS_PER_PKT_MAX - 1) *
1605294077Sarybchik			    (EFX_TX_FATSOV2_OPT_NDESCS + 1) - 1;
1606294077Sarybchik		}
1607294077Sarybchik
1608294077Sarybchik		max_descs += MAX(sw_tso_max_descs,
1609294077Sarybchik				 MAX(fa_tso_v1_max_descs, fa_tso_v2_max_descs));
1610294077Sarybchik	}
1611294077Sarybchik
1612294077Sarybchik	return (max_descs);
1613294077Sarybchik}
1614294077Sarybchik
1615227569Sphilipstatic int
1616227569Sphilipsfxge_tx_qstart(struct sfxge_softc *sc, unsigned int index)
1617227569Sphilip{
1618227569Sphilip	struct sfxge_txq *txq;
1619227569Sphilip	efsys_mem_t *esmp;
1620227569Sphilip	uint16_t flags;
1621294077Sarybchik	unsigned int tso_fw_assisted;
1622342455Sarybchik	unsigned int label;
1623227569Sphilip	struct sfxge_evq *evq;
1624283514Sarybchik	unsigned int desc_index;
1625227569Sphilip	int rc;
1626227569Sphilip
1627283514Sarybchik	SFXGE_ADAPTER_LOCK_ASSERT_OWNED(sc);
1628283514Sarybchik
1629227569Sphilip	txq = sc->txq[index];
1630227569Sphilip	esmp = &txq->mem;
1631227569Sphilip	evq = sc->evq[txq->evq_index];
1632227569Sphilip
1633227569Sphilip	KASSERT(txq->init_state == SFXGE_TXQ_INITIALIZED,
1634227569Sphilip	    ("txq->init_state != SFXGE_TXQ_INITIALIZED"));
1635227569Sphilip	KASSERT(evq->init_state == SFXGE_EVQ_STARTED,
1636227569Sphilip	    ("evq->init_state != SFXGE_EVQ_STARTED"));
1637227569Sphilip
1638227569Sphilip	/* Program the buffer table. */
1639227569Sphilip	if ((rc = efx_sram_buf_tbl_set(sc->enp, txq->buf_base_id, esmp,
1640272328Sgnn	    EFX_TXQ_NBUFS(sc->txq_entries))) != 0)
1641272328Sgnn		return (rc);
1642227569Sphilip
1643227569Sphilip	/* Determine the kind of queue we are creating. */
1644294077Sarybchik	tso_fw_assisted = 0;
1645227569Sphilip	switch (txq->type) {
1646227569Sphilip	case SFXGE_TXQ_NON_CKSUM:
1647227569Sphilip		flags = 0;
1648227569Sphilip		break;
1649227569Sphilip	case SFXGE_TXQ_IP_CKSUM:
1650291924Sarybchik		flags = EFX_TXQ_CKSUM_IPV4;
1651227569Sphilip		break;
1652227569Sphilip	case SFXGE_TXQ_IP_TCP_UDP_CKSUM:
1653291924Sarybchik		flags = EFX_TXQ_CKSUM_IPV4 | EFX_TXQ_CKSUM_TCPUDP;
1654294077Sarybchik		tso_fw_assisted = sc->tso_fw_assisted;
1655294077Sarybchik		if (tso_fw_assisted & SFXGE_FATSOV2)
1656294077Sarybchik			flags |= EFX_TXQ_FATSOV2;
1657227569Sphilip		break;
1658227569Sphilip	default:
1659227569Sphilip		KASSERT(0, ("Impossible TX queue"));
1660227569Sphilip		flags = 0;
1661227569Sphilip		break;
1662227569Sphilip	}
1663227569Sphilip
1664342455Sarybchik	label = (sc->txq_dynamic_cksum_toggle_supported) ? 0 : txq->type;
1665342455Sarybchik
1666227569Sphilip	/* Create the common code transmit queue. */
1667342455Sarybchik	if ((rc = efx_tx_qcreate(sc->enp, index, label, esmp,
1668272328Sgnn	    sc->txq_entries, txq->buf_base_id, flags, evq->common,
1669294077Sarybchik	    &txq->common, &desc_index)) != 0) {
1670294077Sarybchik		/* Retry if no FATSOv2 resources, otherwise fail */
1671294077Sarybchik		if ((rc != ENOSPC) || (~flags & EFX_TXQ_FATSOV2))
1672294077Sarybchik			goto fail;
1673227569Sphilip
1674294077Sarybchik		/* Looks like all FATSOv2 contexts are used */
1675294077Sarybchik		flags &= ~EFX_TXQ_FATSOV2;
1676294077Sarybchik		tso_fw_assisted &= ~SFXGE_FATSOV2;
1677342455Sarybchik		if ((rc = efx_tx_qcreate(sc->enp, index, label, esmp,
1678294077Sarybchik		    sc->txq_entries, txq->buf_base_id, flags, evq->common,
1679294077Sarybchik		    &txq->common, &desc_index)) != 0)
1680294077Sarybchik			goto fail;
1681294077Sarybchik	}
1682294077Sarybchik
1683283514Sarybchik	/* Initialise queue descriptor indexes */
1684283514Sarybchik	txq->added = txq->pending = txq->completed = txq->reaped = desc_index;
1685283514Sarybchik
1686278221Sarybchik	SFXGE_TXQ_LOCK(txq);
1687272325Sgnn
1688227569Sphilip	/* Enable the transmit queue. */
1689227569Sphilip	efx_tx_qenable(txq->common);
1690227569Sphilip
1691227569Sphilip	txq->init_state = SFXGE_TXQ_STARTED;
1692283514Sarybchik	txq->flush_state = SFXGE_FLUSH_REQUIRED;
1693294077Sarybchik	txq->tso_fw_assisted = tso_fw_assisted;
1694227569Sphilip
1695294077Sarybchik	txq->max_pkt_desc = sfxge_tx_max_pkt_desc(sc, txq->type,
1696294077Sarybchik						  tso_fw_assisted);
1697294077Sarybchik
1698342404Sarybchik	txq->hw_vlan_tci = 0;
1699342404Sarybchik
1700342455Sarybchik	txq->hw_cksum_flags = flags &
1701342455Sarybchik			      (EFX_TXQ_CKSUM_IPV4 | EFX_TXQ_CKSUM_TCPUDP);
1702342455Sarybchik
1703278221Sarybchik	SFXGE_TXQ_UNLOCK(txq);
1704227569Sphilip
1705227569Sphilip	return (0);
1706227569Sphilip
1707227569Sphilipfail:
1708227569Sphilip	efx_sram_buf_tbl_clear(sc->enp, txq->buf_base_id,
1709272328Sgnn	    EFX_TXQ_NBUFS(sc->txq_entries));
1710272328Sgnn	return (rc);
1711227569Sphilip}
1712227569Sphilip
1713227569Sphilipvoid
1714227569Sphilipsfxge_tx_stop(struct sfxge_softc *sc)
1715227569Sphilip{
1716227569Sphilip	int index;
1717227569Sphilip
1718278938Sarybchik	index = sc->txq_count;
1719227569Sphilip	while (--index >= 0)
1720278938Sarybchik		sfxge_tx_qstop(sc, index);
1721227569Sphilip
1722227569Sphilip	/* Tear down the transmit module */
1723227569Sphilip	efx_tx_fini(sc->enp);
1724227569Sphilip}
1725227569Sphilip
1726227569Sphilipint
1727227569Sphilipsfxge_tx_start(struct sfxge_softc *sc)
1728227569Sphilip{
1729227569Sphilip	int index;
1730227569Sphilip	int rc;
1731227569Sphilip
1732227569Sphilip	/* Initialize the common code transmit module. */
1733227569Sphilip	if ((rc = efx_tx_init(sc->enp)) != 0)
1734227569Sphilip		return (rc);
1735227569Sphilip
1736278938Sarybchik	for (index = 0; index < sc->txq_count; index++) {
1737278938Sarybchik		if ((rc = sfxge_tx_qstart(sc, index)) != 0)
1738278938Sarybchik			goto fail;
1739227569Sphilip	}
1740227569Sphilip
1741227569Sphilip	return (0);
1742227569Sphilip
1743278938Sarybchikfail:
1744227569Sphilip	while (--index >= 0)
1745278938Sarybchik		sfxge_tx_qstop(sc, index);
1746227569Sphilip
1747227569Sphilip	efx_tx_fini(sc->enp);
1748227569Sphilip
1749227569Sphilip	return (rc);
1750227569Sphilip}
1751227569Sphilip
1752280377Sarybchikstatic int
1753280377Sarybchiksfxge_txq_stat_init(struct sfxge_txq *txq, struct sysctl_oid *txq_node)
1754280377Sarybchik{
1755280377Sarybchik	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(txq->sc->dev);
1756280377Sarybchik	struct sysctl_oid *stat_node;
1757280377Sarybchik	unsigned int id;
1758280377Sarybchik
1759280377Sarybchik	stat_node = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(txq_node), OID_AUTO,
1760280377Sarybchik				    "stats", CTLFLAG_RD, NULL,
1761280377Sarybchik				    "Tx queue statistics");
1762280377Sarybchik	if (stat_node == NULL)
1763280377Sarybchik		return (ENOMEM);
1764280377Sarybchik
1765280377Sarybchik	for (id = 0; id < nitems(sfxge_tx_stats); id++) {
1766280377Sarybchik		SYSCTL_ADD_ULONG(
1767280377Sarybchik		    ctx, SYSCTL_CHILDREN(stat_node), OID_AUTO,
1768280377Sarybchik		    sfxge_tx_stats[id].name, CTLFLAG_RD | CTLFLAG_STATS,
1769280377Sarybchik		    (unsigned long *)((caddr_t)txq + sfxge_tx_stats[id].offset),
1770280377Sarybchik		    "");
1771280377Sarybchik	}
1772280377Sarybchik
1773280377Sarybchik	return (0);
1774280377Sarybchik}
1775280377Sarybchik
1776227569Sphilip/**
1777227569Sphilip * Destroy a transmit queue.
1778227569Sphilip */
1779227569Sphilipstatic void
1780227569Sphilipsfxge_tx_qfini(struct sfxge_softc *sc, unsigned int index)
1781227569Sphilip{
1782227569Sphilip	struct sfxge_txq *txq;
1783272328Sgnn	unsigned int nmaps;
1784227569Sphilip
1785227569Sphilip	txq = sc->txq[index];
1786227569Sphilip
1787227569Sphilip	KASSERT(txq->init_state == SFXGE_TXQ_INITIALIZED,
1788227569Sphilip	    ("txq->init_state != SFXGE_TXQ_INITIALIZED"));
1789227569Sphilip
1790227569Sphilip	if (txq->type == SFXGE_TXQ_IP_TCP_UDP_CKSUM)
1791227569Sphilip		tso_fini(txq);
1792227569Sphilip
1793227569Sphilip	/* Free the context arrays. */
1794227569Sphilip	free(txq->pend_desc, M_SFXGE);
1795272328Sgnn	nmaps = sc->txq_entries;
1796272325Sgnn	while (nmaps-- != 0)
1797227569Sphilip		bus_dmamap_destroy(txq->packet_dma_tag, txq->stmp[nmaps].map);
1798227569Sphilip	free(txq->stmp, M_SFXGE);
1799227569Sphilip
1800227569Sphilip	/* Release DMA memory mapping. */
1801227569Sphilip	sfxge_dma_free(&txq->mem);
1802227569Sphilip
1803227569Sphilip	sc->txq[index] = NULL;
1804227569Sphilip
1805278221Sarybchik	SFXGE_TXQ_LOCK_DESTROY(txq);
1806227569Sphilip
1807227569Sphilip	free(txq, M_SFXGE);
1808227569Sphilip}
1809227569Sphilip
1810227569Sphilipstatic int
1811227569Sphilipsfxge_tx_qinit(struct sfxge_softc *sc, unsigned int txq_index,
1812283514Sarybchik	       enum sfxge_txq_type type, unsigned int evq_index)
1813227569Sphilip{
1814311765Sarybchik	const efx_nic_cfg_t *encp = efx_nic_cfg_get(sc->enp);
1815272330Sgnn	char name[16];
1816280161Sarybchik	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev);
1817272330Sgnn	struct sysctl_oid *txq_node;
1818227569Sphilip	struct sfxge_txq *txq;
1819227569Sphilip	struct sfxge_evq *evq;
1820227569Sphilip	struct sfxge_tx_dpl *stdp;
1821280161Sarybchik	struct sysctl_oid *dpl_node;
1822227569Sphilip	efsys_mem_t *esmp;
1823227569Sphilip	unsigned int nmaps;
1824227569Sphilip	int rc;
1825227569Sphilip
1826227569Sphilip	txq = malloc(sizeof(struct sfxge_txq), M_SFXGE, M_ZERO | M_WAITOK);
1827227569Sphilip	txq->sc = sc;
1828272328Sgnn	txq->entries = sc->txq_entries;
1829272328Sgnn	txq->ptr_mask = txq->entries - 1;
1830227569Sphilip
1831227569Sphilip	sc->txq[txq_index] = txq;
1832227569Sphilip	esmp = &txq->mem;
1833227569Sphilip
1834227569Sphilip	evq = sc->evq[evq_index];
1835227569Sphilip
1836227569Sphilip	/* Allocate and zero DMA space for the descriptor ring. */
1837272328Sgnn	if ((rc = sfxge_dma_alloc(sc, EFX_TXQ_SIZE(sc->txq_entries), esmp)) != 0)
1838227569Sphilip		return (rc);
1839227569Sphilip
1840227569Sphilip	/* Allocate buffer table entries. */
1841272328Sgnn	sfxge_sram_buf_tbl_alloc(sc, EFX_TXQ_NBUFS(sc->txq_entries),
1842227569Sphilip				 &txq->buf_base_id);
1843227569Sphilip
1844227569Sphilip	/* Create a DMA tag for packet mappings. */
1845311765Sarybchik	if (bus_dma_tag_create(sc->parent_dma_tag, 1,
1846311765Sarybchik	    encp->enc_tx_dma_desc_boundary,
1847227640Smarius	    MIN(0x3FFFFFFFFFFFUL, BUS_SPACE_MAXADDR), BUS_SPACE_MAXADDR, NULL,
1848311765Sarybchik	    NULL, 0x11000, SFXGE_TX_MAPPING_MAX_SEG,
1849311765Sarybchik	    encp->enc_tx_dma_desc_size_max, 0, NULL, NULL,
1850227569Sphilip	    &txq->packet_dma_tag) != 0) {
1851227569Sphilip		device_printf(sc->dev, "Couldn't allocate txq DMA tag\n");
1852227569Sphilip		rc = ENOMEM;
1853227569Sphilip		goto fail;
1854227569Sphilip	}
1855227569Sphilip
1856227569Sphilip	/* Allocate pending descriptor array for batching writes. */
1857283514Sarybchik	txq->pend_desc = malloc(sizeof(efx_desc_t) * sc->txq_entries,
1858227569Sphilip				M_SFXGE, M_ZERO | M_WAITOK);
1859227569Sphilip
1860227569Sphilip	/* Allocate and initialise mbuf DMA mapping array. */
1861272328Sgnn	txq->stmp = malloc(sizeof(struct sfxge_tx_mapping) * sc->txq_entries,
1862227569Sphilip	    M_SFXGE, M_ZERO | M_WAITOK);
1863272328Sgnn	for (nmaps = 0; nmaps < sc->txq_entries; nmaps++) {
1864227569Sphilip		rc = bus_dmamap_create(txq->packet_dma_tag, 0,
1865227569Sphilip				       &txq->stmp[nmaps].map);
1866227569Sphilip		if (rc != 0)
1867227569Sphilip			goto fail2;
1868227569Sphilip	}
1869227569Sphilip
1870272330Sgnn	snprintf(name, sizeof(name), "%u", txq_index);
1871280161Sarybchik	txq_node = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(sc->txqs_node),
1872280161Sarybchik				   OID_AUTO, name, CTLFLAG_RD, NULL, "");
1873272330Sgnn	if (txq_node == NULL) {
1874272330Sgnn		rc = ENOMEM;
1875272330Sgnn		goto fail_txq_node;
1876272330Sgnn	}
1877272330Sgnn
1878227569Sphilip	if (type == SFXGE_TXQ_IP_TCP_UDP_CKSUM &&
1879227569Sphilip	    (rc = tso_init(txq)) != 0)
1880227569Sphilip		goto fail3;
1881227569Sphilip
1882227569Sphilip	/* Initialize the deferred packet list. */
1883227569Sphilip	stdp = &txq->dpl;
1884272331Sgnn	stdp->std_put_max = sfxge_tx_dpl_put_max;
1885272331Sgnn	stdp->std_get_max = sfxge_tx_dpl_get_max;
1886277895Sarybchik	stdp->std_get_non_tcp_max = sfxge_tx_dpl_get_non_tcp_max;
1887227569Sphilip	stdp->std_getp = &stdp->std_get;
1888227569Sphilip
1889278250Sarybchik	SFXGE_TXQ_LOCK_INIT(txq, device_get_nameunit(sc->dev), txq_index);
1890272330Sgnn
1891280161Sarybchik	dpl_node = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(txq_node), OID_AUTO,
1892280161Sarybchik				   "dpl", CTLFLAG_RD, NULL,
1893280161Sarybchik				   "Deferred packet list statistics");
1894280161Sarybchik	if (dpl_node == NULL) {
1895280161Sarybchik		rc = ENOMEM;
1896280161Sarybchik		goto fail_dpl_node;
1897280161Sarybchik	}
1898280161Sarybchik
1899280161Sarybchik	SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(dpl_node), OID_AUTO,
1900280161Sarybchik			"get_count", CTLFLAG_RD | CTLFLAG_STATS,
1901272330Sgnn			&stdp->std_get_count, 0, "");
1902280161Sarybchik	SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(dpl_node), OID_AUTO,
1903280161Sarybchik			"get_non_tcp_count", CTLFLAG_RD | CTLFLAG_STATS,
1904277895Sarybchik			&stdp->std_get_non_tcp_count, 0, "");
1905280161Sarybchik	SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(dpl_node), OID_AUTO,
1906280161Sarybchik			"get_hiwat", CTLFLAG_RD | CTLFLAG_STATS,
1907277895Sarybchik			&stdp->std_get_hiwat, 0, "");
1908280161Sarybchik	SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(dpl_node), OID_AUTO,
1909280161Sarybchik			"put_hiwat", CTLFLAG_RD | CTLFLAG_STATS,
1910279231Sarybchik			&stdp->std_put_hiwat, 0, "");
1911227569Sphilip
1912280377Sarybchik	rc = sfxge_txq_stat_init(txq, txq_node);
1913280377Sarybchik	if (rc != 0)
1914280377Sarybchik		goto fail_txq_stat_init;
1915280377Sarybchik
1916227569Sphilip	txq->type = type;
1917227569Sphilip	txq->evq_index = evq_index;
1918227569Sphilip	txq->init_state = SFXGE_TXQ_INITIALIZED;
1919227569Sphilip
1920227569Sphilip	return (0);
1921227569Sphilip
1922280377Sarybchikfail_txq_stat_init:
1923280161Sarybchikfail_dpl_node:
1924227569Sphilipfail3:
1925272330Sgnnfail_txq_node:
1926227569Sphilip	free(txq->pend_desc, M_SFXGE);
1927227569Sphilipfail2:
1928272325Sgnn	while (nmaps-- != 0)
1929227569Sphilip		bus_dmamap_destroy(txq->packet_dma_tag, txq->stmp[nmaps].map);
1930227569Sphilip	free(txq->stmp, M_SFXGE);
1931227569Sphilip	bus_dma_tag_destroy(txq->packet_dma_tag);
1932227569Sphilip
1933227569Sphilipfail:
1934227569Sphilip	sfxge_dma_free(esmp);
1935227569Sphilip
1936227569Sphilip	return (rc);
1937227569Sphilip}
1938227569Sphilip
1939227569Sphilipstatic int
1940227569Sphilipsfxge_tx_stat_handler(SYSCTL_HANDLER_ARGS)
1941227569Sphilip{
1942227569Sphilip	struct sfxge_softc *sc = arg1;
1943227569Sphilip	unsigned int id = arg2;
1944227569Sphilip	unsigned long sum;
1945227569Sphilip	unsigned int index;
1946227569Sphilip
1947227569Sphilip	/* Sum across all TX queues */
1948227569Sphilip	sum = 0;
1949278938Sarybchik	for (index = 0; index < sc->txq_count; index++)
1950227569Sphilip		sum += *(unsigned long *)((caddr_t)sc->txq[index] +
1951227569Sphilip					  sfxge_tx_stats[id].offset);
1952227569Sphilip
1953272325Sgnn	return (SYSCTL_OUT(req, &sum, sizeof(sum)));
1954227569Sphilip}
1955227569Sphilip
1956227569Sphilipstatic void
1957227569Sphilipsfxge_tx_stat_init(struct sfxge_softc *sc)
1958227569Sphilip{
1959227569Sphilip	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev);
1960227569Sphilip	struct sysctl_oid_list *stat_list;
1961227569Sphilip	unsigned int id;
1962227569Sphilip
1963227569Sphilip	stat_list = SYSCTL_CHILDREN(sc->stats_node);
1964227569Sphilip
1965279077Sarybchik	for (id = 0; id < nitems(sfxge_tx_stats); id++) {
1966227569Sphilip		SYSCTL_ADD_PROC(
1967227569Sphilip			ctx, stat_list,
1968227569Sphilip			OID_AUTO, sfxge_tx_stats[id].name,
1969227569Sphilip			CTLTYPE_ULONG|CTLFLAG_RD,
1970227569Sphilip			sc, id, sfxge_tx_stat_handler, "LU",
1971227569Sphilip			"");
1972227569Sphilip	}
1973227569Sphilip}
1974227569Sphilip
1975279184Sarybchikuint64_t
1976279184Sarybchiksfxge_tx_get_drops(struct sfxge_softc *sc)
1977279184Sarybchik{
1978279184Sarybchik	unsigned int index;
1979279184Sarybchik	uint64_t drops = 0;
1980279184Sarybchik	struct sfxge_txq *txq;
1981279184Sarybchik
1982279184Sarybchik	/* Sum across all TX queues */
1983279184Sarybchik	for (index = 0; index < sc->txq_count; index++) {
1984279184Sarybchik		txq = sc->txq[index];
1985279184Sarybchik		/*
1986279184Sarybchik		 * In theory, txq->put_overflow and txq->netdown_drops
1987279184Sarybchik		 * should use atomic operation and other should be
1988279184Sarybchik		 * obtained under txq lock, but it is just statistics.
1989279184Sarybchik		 */
1990279184Sarybchik		drops += txq->drops + txq->get_overflow +
1991279184Sarybchik			 txq->get_non_tcp_overflow +
1992279184Sarybchik			 txq->put_overflow + txq->netdown_drops +
1993279184Sarybchik			 txq->tso_pdrop_too_many + txq->tso_pdrop_no_rsrc;
1994279184Sarybchik	}
1995279184Sarybchik	return (drops);
1996279184Sarybchik}
1997279184Sarybchik
1998227569Sphilipvoid
1999227569Sphilipsfxge_tx_fini(struct sfxge_softc *sc)
2000227569Sphilip{
2001227569Sphilip	int index;
2002227569Sphilip
2003278938Sarybchik	index = sc->txq_count;
2004227569Sphilip	while (--index >= 0)
2005278938Sarybchik		sfxge_tx_qfini(sc, index);
2006227569Sphilip
2007278938Sarybchik	sc->txq_count = 0;
2008227569Sphilip}
2009227569Sphilip
2010227569Sphilip
2011227569Sphilipint
2012227569Sphilipsfxge_tx_init(struct sfxge_softc *sc)
2013227569Sphilip{
2014283514Sarybchik	const efx_nic_cfg_t *encp = efx_nic_cfg_get(sc->enp);
2015227569Sphilip	struct sfxge_intr *intr;
2016227569Sphilip	int index;
2017227569Sphilip	int rc;
2018227569Sphilip
2019227569Sphilip	intr = &sc->intr;
2020227569Sphilip
2021227569Sphilip	KASSERT(intr->state == SFXGE_INTR_INITIALIZED,
2022227569Sphilip	    ("intr->state != SFXGE_INTR_INITIALIZED"));
2023227569Sphilip
2024311029Sarybchik	if (sfxge_tx_dpl_get_max <= 0) {
2025311029Sarybchik		log(LOG_ERR, "%s=%d must be greater than 0",
2026311029Sarybchik		    SFXGE_PARAM_TX_DPL_GET_MAX, sfxge_tx_dpl_get_max);
2027311029Sarybchik		rc = EINVAL;
2028311029Sarybchik		goto fail_tx_dpl_get_max;
2029311029Sarybchik	}
2030311029Sarybchik	if (sfxge_tx_dpl_get_non_tcp_max <= 0) {
2031311029Sarybchik		log(LOG_ERR, "%s=%d must be greater than 0",
2032311029Sarybchik		    SFXGE_PARAM_TX_DPL_GET_NON_TCP_MAX,
2033311029Sarybchik		    sfxge_tx_dpl_get_non_tcp_max);
2034311029Sarybchik		rc = EINVAL;
2035311029Sarybchik		goto fail_tx_dpl_get_non_tcp_max;
2036311029Sarybchik	}
2037311029Sarybchik	if (sfxge_tx_dpl_put_max < 0) {
2038311029Sarybchik		log(LOG_ERR, "%s=%d must be greater or equal to 0",
2039311029Sarybchik		    SFXGE_PARAM_TX_DPL_PUT_MAX, sfxge_tx_dpl_put_max);
2040311029Sarybchik		rc = EINVAL;
2041311029Sarybchik		goto fail_tx_dpl_put_max;
2042311029Sarybchik	}
2043311029Sarybchik
2044342454Sarybchik	sc->txq_count = SFXGE_EVQ0_N_TXQ(sc) - 1 + sc->intr.n_alloc;
2045278938Sarybchik
2046283514Sarybchik	sc->tso_fw_assisted = sfxge_tso_fw_assisted;
2047294077Sarybchik	if ((~encp->enc_features & EFX_FEATURE_FW_ASSISTED_TSO) ||
2048294077Sarybchik	    (!encp->enc_fw_assisted_tso_enabled))
2049294077Sarybchik		sc->tso_fw_assisted &= ~SFXGE_FATSOV1;
2050294077Sarybchik	if ((~encp->enc_features & EFX_FEATURE_FW_ASSISTED_TSO_V2) ||
2051294077Sarybchik	    (!encp->enc_fw_assisted_tso_v2_enabled))
2052294077Sarybchik		sc->tso_fw_assisted &= ~SFXGE_FATSOV2;
2053283514Sarybchik
2054272330Sgnn	sc->txqs_node = SYSCTL_ADD_NODE(
2055272330Sgnn		device_get_sysctl_ctx(sc->dev),
2056272330Sgnn		SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)),
2057272330Sgnn		OID_AUTO, "txq", CTLFLAG_RD, NULL, "Tx queues");
2058272330Sgnn	if (sc->txqs_node == NULL) {
2059272330Sgnn		rc = ENOMEM;
2060272330Sgnn		goto fail_txq_node;
2061272330Sgnn	}
2062272330Sgnn
2063227569Sphilip	/* Initialize the transmit queues */
2064342455Sarybchik	if (sc->txq_dynamic_cksum_toggle_supported == B_FALSE) {
2065342455Sarybchik		if ((rc = sfxge_tx_qinit(sc, SFXGE_TXQ_NON_CKSUM,
2066342455Sarybchik		    SFXGE_TXQ_NON_CKSUM, 0)) != 0)
2067342455Sarybchik			goto fail;
2068227569Sphilip
2069342455Sarybchik		if ((rc = sfxge_tx_qinit(sc, SFXGE_TXQ_IP_CKSUM,
2070342455Sarybchik		    SFXGE_TXQ_IP_CKSUM, 0)) != 0)
2071342455Sarybchik			goto fail2;
2072342455Sarybchik	}
2073227569Sphilip
2074278938Sarybchik	for (index = 0;
2075342454Sarybchik	     index < sc->txq_count - SFXGE_EVQ0_N_TXQ(sc) + 1;
2076278938Sarybchik	     index++) {
2077342454Sarybchik		if ((rc = sfxge_tx_qinit(sc, SFXGE_EVQ0_N_TXQ(sc) - 1 + index,
2078227569Sphilip		    SFXGE_TXQ_IP_TCP_UDP_CKSUM, index)) != 0)
2079227569Sphilip			goto fail3;
2080227569Sphilip	}
2081227569Sphilip
2082227569Sphilip	sfxge_tx_stat_init(sc);
2083227569Sphilip
2084227569Sphilip	return (0);
2085227569Sphilip
2086227569Sphilipfail3:
2087227569Sphilip	while (--index >= 0)
2088227569Sphilip		sfxge_tx_qfini(sc, SFXGE_TXQ_IP_TCP_UDP_CKSUM + index);
2089227569Sphilip
2090278938Sarybchik	sfxge_tx_qfini(sc, SFXGE_TXQ_IP_CKSUM);
2091278938Sarybchik
2092227569Sphilipfail2:
2093227569Sphilip	sfxge_tx_qfini(sc, SFXGE_TXQ_NON_CKSUM);
2094227569Sphilip
2095227569Sphilipfail:
2096272330Sgnnfail_txq_node:
2097278938Sarybchik	sc->txq_count = 0;
2098311029Sarybchikfail_tx_dpl_put_max:
2099311029Sarybchikfail_tx_dpl_get_non_tcp_max:
2100311029Sarybchikfail_tx_dpl_get_max:
2101227569Sphilip	return (rc);
2102227569Sphilip}
2103