xref: /freebsd/sys/dev/sfxge/sfxge_tx.c (revision f080384c)
1e948693eSPhilip Paeps /*-
2e948693eSPhilip Paeps  * Copyright (c) 2010-2011 Solarflare Communications, Inc.
3e948693eSPhilip Paeps  * All rights reserved.
4e948693eSPhilip Paeps  *
5e948693eSPhilip Paeps  * This software was developed in part by Philip Paeps under contract for
6e948693eSPhilip Paeps  * Solarflare Communications, Inc.
7e948693eSPhilip Paeps  *
8e948693eSPhilip Paeps  * Redistribution and use in source and binary forms, with or without
9e948693eSPhilip Paeps  * modification, are permitted provided that the following conditions
10e948693eSPhilip Paeps  * are met:
11e948693eSPhilip Paeps  * 1. Redistributions of source code must retain the above copyright
12e948693eSPhilip Paeps  *    notice, this list of conditions and the following disclaimer.
13e948693eSPhilip Paeps  * 2. Redistributions in binary form must reproduce the above copyright
14e948693eSPhilip Paeps  *    notice, this list of conditions and the following disclaimer in the
15e948693eSPhilip Paeps  *    documentation and/or other materials provided with the distribution.
16e948693eSPhilip Paeps  *
17e948693eSPhilip Paeps  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18e948693eSPhilip Paeps  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19e948693eSPhilip Paeps  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20e948693eSPhilip Paeps  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21e948693eSPhilip Paeps  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22e948693eSPhilip Paeps  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23e948693eSPhilip Paeps  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24e948693eSPhilip Paeps  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25e948693eSPhilip Paeps  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26e948693eSPhilip Paeps  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27e948693eSPhilip Paeps  * SUCH DAMAGE.
28e948693eSPhilip Paeps  */
29e948693eSPhilip Paeps 
30cf07c70dSGeorge V. Neville-Neil /* Theory of operation:
31cf07c70dSGeorge V. Neville-Neil  *
32cf07c70dSGeorge V. Neville-Neil  * Tx queues allocation and mapping
33cf07c70dSGeorge V. Neville-Neil  *
34cf07c70dSGeorge V. Neville-Neil  * One Tx queue with enabled checksum offload is allocated per Rx channel
35cf07c70dSGeorge V. Neville-Neil  * (event queue).  Also 2 Tx queues (one without checksum offload and one
36cf07c70dSGeorge V. Neville-Neil  * with IP checksum offload only) are allocated and bound to event queue 0.
37cf07c70dSGeorge V. Neville-Neil  * sfxge_txq_type is used as Tx queue label.
38cf07c70dSGeorge V. Neville-Neil  *
39cf07c70dSGeorge V. Neville-Neil  * So, event queue plus label mapping to Tx queue index is:
40cf07c70dSGeorge V. Neville-Neil  *	if event queue index is 0, TxQ-index = TxQ-label * [0..SFXGE_TXQ_NTYPES)
41cf07c70dSGeorge V. Neville-Neil  *	else TxQ-index = SFXGE_TXQ_NTYPES + EvQ-index - 1
42cf07c70dSGeorge V. Neville-Neil  * See sfxge_get_txq_by_label() sfxge_ev.c
43cf07c70dSGeorge V. Neville-Neil  */
44cf07c70dSGeorge V. Neville-Neil 
45e948693eSPhilip Paeps #include <sys/cdefs.h>
46e948693eSPhilip Paeps __FBSDID("$FreeBSD$");
47e948693eSPhilip Paeps 
48e948693eSPhilip Paeps #include <sys/types.h>
49e948693eSPhilip Paeps #include <sys/mbuf.h>
50e948693eSPhilip Paeps #include <sys/smp.h>
51e948693eSPhilip Paeps #include <sys/socket.h>
52e948693eSPhilip Paeps #include <sys/sysctl.h>
53060a95efSGeorge V. Neville-Neil #include <sys/syslog.h>
54e948693eSPhilip Paeps 
55e948693eSPhilip Paeps #include <net/bpf.h>
56e948693eSPhilip Paeps #include <net/ethernet.h>
57e948693eSPhilip Paeps #include <net/if.h>
58e948693eSPhilip Paeps #include <net/if_vlan_var.h>
59e948693eSPhilip Paeps 
60e948693eSPhilip Paeps #include <netinet/in.h>
61e948693eSPhilip Paeps #include <netinet/ip.h>
62e948693eSPhilip Paeps #include <netinet/ip6.h>
63e948693eSPhilip Paeps #include <netinet/tcp.h>
64e948693eSPhilip Paeps 
65e948693eSPhilip Paeps #include "common/efx.h"
66e948693eSPhilip Paeps 
67e948693eSPhilip Paeps #include "sfxge.h"
68e948693eSPhilip Paeps #include "sfxge_tx.h"
69e948693eSPhilip Paeps 
7049448887SAndrew Rybchenko /*
7149448887SAndrew Rybchenko  * Estimate maximum number of Tx descriptors required for TSO packet.
7249448887SAndrew Rybchenko  * With minimum MSS and maximum mbuf length we might need more (even
7349448887SAndrew Rybchenko  * than a ring-ful of descriptors), but this should not happen in
7449448887SAndrew Rybchenko  * practice except due to deliberate attack.  In that case we will
7549448887SAndrew Rybchenko  * truncate the output at a packet boundary.
76e948693eSPhilip Paeps  */
7745923e76SAndrew Rybchenko #define	SFXGE_TSO_MAX_DESC						\
7845923e76SAndrew Rybchenko 	(SFXGE_TSO_MAX_SEGS * 2 + SFXGE_TX_MAPPING_MAX_SEG - 1)
7949448887SAndrew Rybchenko 
8049448887SAndrew Rybchenko /*
8149448887SAndrew Rybchenko  * Set the block level to ensure there is space to generate a
8249448887SAndrew Rybchenko  * large number of descriptors for TSO.
8349448887SAndrew Rybchenko  */
8449448887SAndrew Rybchenko #define	SFXGE_TXQ_BLOCK_LEVEL(_entries)					\
8549448887SAndrew Rybchenko 	(EFX_TXQ_LIMIT(_entries) - SFXGE_TSO_MAX_DESC)
86e948693eSPhilip Paeps 
87060a95efSGeorge V. Neville-Neil 
88060a95efSGeorge V. Neville-Neil #define	SFXGE_PARAM_TX_DPL_GET_MAX	SFXGE_PARAM(tx_dpl_get_max)
89060a95efSGeorge V. Neville-Neil static int sfxge_tx_dpl_get_max = SFXGE_TX_DPL_GET_PKT_LIMIT_DEFAULT;
90060a95efSGeorge V. Neville-Neil TUNABLE_INT(SFXGE_PARAM_TX_DPL_GET_MAX, &sfxge_tx_dpl_get_max);
91060a95efSGeorge V. Neville-Neil SYSCTL_INT(_hw_sfxge, OID_AUTO, tx_dpl_get_max, CTLFLAG_RDTUN,
92060a95efSGeorge V. Neville-Neil 	   &sfxge_tx_dpl_get_max, 0,
9393929f25SAndrew Rybchenko 	   "Maximum number of any packets in deferred packet get-list");
9493929f25SAndrew Rybchenko 
9593929f25SAndrew Rybchenko #define	SFXGE_PARAM_TX_DPL_GET_NON_TCP_MAX \
9693929f25SAndrew Rybchenko 	SFXGE_PARAM(tx_dpl_get_non_tcp_max)
9793929f25SAndrew Rybchenko static int sfxge_tx_dpl_get_non_tcp_max =
9893929f25SAndrew Rybchenko 	SFXGE_TX_DPL_GET_NON_TCP_PKT_LIMIT_DEFAULT;
9993929f25SAndrew Rybchenko TUNABLE_INT(SFXGE_PARAM_TX_DPL_GET_NON_TCP_MAX, &sfxge_tx_dpl_get_non_tcp_max);
10093929f25SAndrew Rybchenko SYSCTL_INT(_hw_sfxge, OID_AUTO, tx_dpl_get_non_tcp_max, CTLFLAG_RDTUN,
10193929f25SAndrew Rybchenko 	   &sfxge_tx_dpl_get_non_tcp_max, 0,
10293929f25SAndrew Rybchenko 	   "Maximum number of non-TCP packets in deferred packet get-list");
103060a95efSGeorge V. Neville-Neil 
104060a95efSGeorge V. Neville-Neil #define	SFXGE_PARAM_TX_DPL_PUT_MAX	SFXGE_PARAM(tx_dpl_put_max)
105060a95efSGeorge V. Neville-Neil static int sfxge_tx_dpl_put_max = SFXGE_TX_DPL_PUT_PKT_LIMIT_DEFAULT;
106060a95efSGeorge V. Neville-Neil TUNABLE_INT(SFXGE_PARAM_TX_DPL_PUT_MAX, &sfxge_tx_dpl_put_max);
107060a95efSGeorge V. Neville-Neil SYSCTL_INT(_hw_sfxge, OID_AUTO, tx_dpl_put_max, CTLFLAG_RDTUN,
108060a95efSGeorge V. Neville-Neil 	   &sfxge_tx_dpl_put_max, 0,
10993929f25SAndrew Rybchenko 	   "Maximum number of any packets in deferred packet put-list");
110060a95efSGeorge V. Neville-Neil 
111060a95efSGeorge V. Neville-Neil 
112f6222d7bSAndrew Rybchenko static const struct {
113f6222d7bSAndrew Rybchenko 	const char *name;
114f6222d7bSAndrew Rybchenko 	size_t offset;
115f6222d7bSAndrew Rybchenko } sfxge_tx_stats[] = {
116f6222d7bSAndrew Rybchenko #define	SFXGE_TX_STAT(name, member) \
117f6222d7bSAndrew Rybchenko 	{ #name, offsetof(struct sfxge_txq, member) }
118f6222d7bSAndrew Rybchenko 	SFXGE_TX_STAT(tso_bursts, tso_bursts),
119f6222d7bSAndrew Rybchenko 	SFXGE_TX_STAT(tso_packets, tso_packets),
120f6222d7bSAndrew Rybchenko 	SFXGE_TX_STAT(tso_long_headers, tso_long_headers),
121f6222d7bSAndrew Rybchenko 	SFXGE_TX_STAT(tso_pdrop_too_many, tso_pdrop_too_many),
122f6222d7bSAndrew Rybchenko 	SFXGE_TX_STAT(tso_pdrop_no_rsrc, tso_pdrop_no_rsrc),
123f6222d7bSAndrew Rybchenko 	SFXGE_TX_STAT(tx_collapses, collapses),
124f6222d7bSAndrew Rybchenko 	SFXGE_TX_STAT(tx_drops, drops),
125f6222d7bSAndrew Rybchenko 	SFXGE_TX_STAT(tx_get_overflow, get_overflow),
126f6222d7bSAndrew Rybchenko 	SFXGE_TX_STAT(tx_get_non_tcp_overflow, get_non_tcp_overflow),
127f6222d7bSAndrew Rybchenko 	SFXGE_TX_STAT(tx_put_overflow, put_overflow),
128f6222d7bSAndrew Rybchenko 	SFXGE_TX_STAT(tx_netdown_drops, netdown_drops),
129f6222d7bSAndrew Rybchenko };
130f6222d7bSAndrew Rybchenko 
131f6222d7bSAndrew Rybchenko 
132e948693eSPhilip Paeps /* Forward declarations. */
1330b28bbdcSAndrew Rybchenko static void sfxge_tx_qdpl_service(struct sfxge_txq *txq);
134e948693eSPhilip Paeps static void sfxge_tx_qlist_post(struct sfxge_txq *txq);
135e948693eSPhilip Paeps static void sfxge_tx_qunblock(struct sfxge_txq *txq);
136e948693eSPhilip Paeps static int sfxge_tx_queue_tso(struct sfxge_txq *txq, struct mbuf *mbuf,
137e948693eSPhilip Paeps 			      const bus_dma_segment_t *dma_seg, int n_dma_seg);
138e948693eSPhilip Paeps 
139e948693eSPhilip Paeps void
140cc933626SAndrew Rybchenko sfxge_tx_qcomplete(struct sfxge_txq *txq, struct sfxge_evq *evq)
141e948693eSPhilip Paeps {
142e948693eSPhilip Paeps 	unsigned int completed;
143e948693eSPhilip Paeps 
144763cab71SAndrew Rybchenko 	SFXGE_EVQ_LOCK_ASSERT_OWNED(evq);
145e948693eSPhilip Paeps 
146e948693eSPhilip Paeps 	completed = txq->completed;
147e948693eSPhilip Paeps 	while (completed != txq->pending) {
148e948693eSPhilip Paeps 		struct sfxge_tx_mapping *stmp;
149e948693eSPhilip Paeps 		unsigned int id;
150e948693eSPhilip Paeps 
151385b1d8eSGeorge V. Neville-Neil 		id = completed++ & txq->ptr_mask;
152e948693eSPhilip Paeps 
153e948693eSPhilip Paeps 		stmp = &txq->stmp[id];
154e948693eSPhilip Paeps 		if (stmp->flags & TX_BUF_UNMAP) {
155e948693eSPhilip Paeps 			bus_dmamap_unload(txq->packet_dma_tag, stmp->map);
156e948693eSPhilip Paeps 			if (stmp->flags & TX_BUF_MBUF) {
157e948693eSPhilip Paeps 				struct mbuf *m = stmp->u.mbuf;
158e948693eSPhilip Paeps 				do
159e948693eSPhilip Paeps 					m = m_free(m);
160e948693eSPhilip Paeps 				while (m != NULL);
161e948693eSPhilip Paeps 			} else {
162e948693eSPhilip Paeps 				free(stmp->u.heap_buf, M_SFXGE);
163e948693eSPhilip Paeps 			}
164e948693eSPhilip Paeps 			stmp->flags = 0;
165e948693eSPhilip Paeps 		}
166e948693eSPhilip Paeps 	}
167e948693eSPhilip Paeps 	txq->completed = completed;
168e948693eSPhilip Paeps 
169e948693eSPhilip Paeps 	/* Check whether we need to unblock the queue. */
170e948693eSPhilip Paeps 	mb();
171e948693eSPhilip Paeps 	if (txq->blocked) {
172e948693eSPhilip Paeps 		unsigned int level;
173e948693eSPhilip Paeps 
174e948693eSPhilip Paeps 		level = txq->added - txq->completed;
175385b1d8eSGeorge V. Neville-Neil 		if (level <= SFXGE_TXQ_UNBLOCK_LEVEL(txq->entries))
176e948693eSPhilip Paeps 			sfxge_tx_qunblock(txq);
177e948693eSPhilip Paeps 	}
178e948693eSPhilip Paeps }
179e948693eSPhilip Paeps 
1800b28bbdcSAndrew Rybchenko static unsigned int
18193929f25SAndrew Rybchenko sfxge_is_mbuf_non_tcp(struct mbuf *mbuf)
18293929f25SAndrew Rybchenko {
18393929f25SAndrew Rybchenko 	/* Absense of TCP checksum flags does not mean that it is non-TCP
18493929f25SAndrew Rybchenko 	 * but it should be true if user wants to achieve high throughput.
18593929f25SAndrew Rybchenko 	 */
18693929f25SAndrew Rybchenko 	return (!(mbuf->m_pkthdr.csum_flags & (CSUM_IP_TCP | CSUM_IP6_TCP)));
18793929f25SAndrew Rybchenko }
18893929f25SAndrew Rybchenko 
189e948693eSPhilip Paeps /*
190e948693eSPhilip Paeps  * Reorder the put list and append it to the get list.
191e948693eSPhilip Paeps  */
192e948693eSPhilip Paeps static void
193e948693eSPhilip Paeps sfxge_tx_qdpl_swizzle(struct sfxge_txq *txq)
194e948693eSPhilip Paeps {
195e948693eSPhilip Paeps 	struct sfxge_tx_dpl *stdp;
196e948693eSPhilip Paeps 	struct mbuf *mbuf, *get_next, **get_tailp;
197e948693eSPhilip Paeps 	volatile uintptr_t *putp;
198e948693eSPhilip Paeps 	uintptr_t put;
199e948693eSPhilip Paeps 	unsigned int count;
20093929f25SAndrew Rybchenko 	unsigned int non_tcp_count;
201e948693eSPhilip Paeps 
202763cab71SAndrew Rybchenko 	SFXGE_TXQ_LOCK_ASSERT_OWNED(txq);
203e948693eSPhilip Paeps 
204e948693eSPhilip Paeps 	stdp = &txq->dpl;
205e948693eSPhilip Paeps 
206e948693eSPhilip Paeps 	/* Acquire the put list. */
207e948693eSPhilip Paeps 	putp = &stdp->std_put;
208fb8ccc78SMarius Strobl 	put = atomic_readandclear_ptr(putp);
209e948693eSPhilip Paeps 	mbuf = (void *)put;
210e948693eSPhilip Paeps 
211e948693eSPhilip Paeps 	if (mbuf == NULL)
212e948693eSPhilip Paeps 		return;
213e948693eSPhilip Paeps 
214e948693eSPhilip Paeps 	/* Reverse the put list. */
215e948693eSPhilip Paeps 	get_tailp = &mbuf->m_nextpkt;
216e948693eSPhilip Paeps 	get_next = NULL;
217e948693eSPhilip Paeps 
218e948693eSPhilip Paeps 	count = 0;
21993929f25SAndrew Rybchenko 	non_tcp_count = 0;
220e948693eSPhilip Paeps 	do {
221e948693eSPhilip Paeps 		struct mbuf *put_next;
222e948693eSPhilip Paeps 
22393929f25SAndrew Rybchenko 		non_tcp_count += sfxge_is_mbuf_non_tcp(mbuf);
224e948693eSPhilip Paeps 		put_next = mbuf->m_nextpkt;
225e948693eSPhilip Paeps 		mbuf->m_nextpkt = get_next;
226e948693eSPhilip Paeps 		get_next = mbuf;
227e948693eSPhilip Paeps 		mbuf = put_next;
228e948693eSPhilip Paeps 
229e948693eSPhilip Paeps 		count++;
230e948693eSPhilip Paeps 	} while (mbuf != NULL);
231e948693eSPhilip Paeps 
232bce6d281SAndrew Rybchenko 	if (count > stdp->std_put_hiwat)
233bce6d281SAndrew Rybchenko 		stdp->std_put_hiwat = count;
234bce6d281SAndrew Rybchenko 
235e948693eSPhilip Paeps 	/* Append the reversed put list to the get list. */
236e948693eSPhilip Paeps 	KASSERT(*get_tailp == NULL, ("*get_tailp != NULL"));
237e948693eSPhilip Paeps 	*stdp->std_getp = get_next;
238e948693eSPhilip Paeps 	stdp->std_getp = get_tailp;
239bc85c897SGeorge V. Neville-Neil 	stdp->std_get_count += count;
24093929f25SAndrew Rybchenko 	stdp->std_get_non_tcp_count += non_tcp_count;
241e948693eSPhilip Paeps }
242e948693eSPhilip Paeps 
243e948693eSPhilip Paeps static void
244e948693eSPhilip Paeps sfxge_tx_qreap(struct sfxge_txq *txq)
245e948693eSPhilip Paeps {
246763cab71SAndrew Rybchenko 	SFXGE_TXQ_LOCK_ASSERT_OWNED(txq);
247e948693eSPhilip Paeps 
248e948693eSPhilip Paeps 	txq->reaped = txq->completed;
249e948693eSPhilip Paeps }
250e948693eSPhilip Paeps 
251e948693eSPhilip Paeps static void
252e948693eSPhilip Paeps sfxge_tx_qlist_post(struct sfxge_txq *txq)
253e948693eSPhilip Paeps {
254e948693eSPhilip Paeps 	unsigned int old_added;
255e948693eSPhilip Paeps 	unsigned int level;
256e948693eSPhilip Paeps 	int rc;
257e948693eSPhilip Paeps 
258763cab71SAndrew Rybchenko 	SFXGE_TXQ_LOCK_ASSERT_OWNED(txq);
259e948693eSPhilip Paeps 
260e948693eSPhilip Paeps 	KASSERT(txq->n_pend_desc != 0, ("txq->n_pend_desc == 0"));
261e948693eSPhilip Paeps 	KASSERT(txq->n_pend_desc <= SFXGE_TSO_MAX_DESC,
262e948693eSPhilip Paeps 		("txq->n_pend_desc too large"));
263e948693eSPhilip Paeps 	KASSERT(!txq->blocked, ("txq->blocked"));
264e948693eSPhilip Paeps 
265e948693eSPhilip Paeps 	old_added = txq->added;
266e948693eSPhilip Paeps 
267e948693eSPhilip Paeps 	/* Post the fragment list. */
268e948693eSPhilip Paeps 	rc = efx_tx_qpost(txq->common, txq->pend_desc, txq->n_pend_desc,
269e948693eSPhilip Paeps 			  txq->reaped, &txq->added);
270e948693eSPhilip Paeps 	KASSERT(rc == 0, ("efx_tx_qpost() failed"));
271e948693eSPhilip Paeps 
272e948693eSPhilip Paeps 	/* If efx_tx_qpost() had to refragment, our information about
273e948693eSPhilip Paeps 	 * buffers to free may be associated with the wrong
274e948693eSPhilip Paeps 	 * descriptors.
275e948693eSPhilip Paeps 	 */
276e948693eSPhilip Paeps 	KASSERT(txq->added - old_added == txq->n_pend_desc,
277e948693eSPhilip Paeps 		("efx_tx_qpost() refragmented descriptors"));
278e948693eSPhilip Paeps 
279e948693eSPhilip Paeps 	level = txq->added - txq->reaped;
280385b1d8eSGeorge V. Neville-Neil 	KASSERT(level <= txq->entries, ("overfilled TX queue"));
281e948693eSPhilip Paeps 
282e948693eSPhilip Paeps 	/* Clear the fragment list. */
283e948693eSPhilip Paeps 	txq->n_pend_desc = 0;
284e948693eSPhilip Paeps 
285e948693eSPhilip Paeps 	/* Have we reached the block level? */
286385b1d8eSGeorge V. Neville-Neil 	if (level < SFXGE_TXQ_BLOCK_LEVEL(txq->entries))
287e948693eSPhilip Paeps 		return;
288e948693eSPhilip Paeps 
289e948693eSPhilip Paeps 	/* Reap, and check again */
290e948693eSPhilip Paeps 	sfxge_tx_qreap(txq);
291e948693eSPhilip Paeps 	level = txq->added - txq->reaped;
292385b1d8eSGeorge V. Neville-Neil 	if (level < SFXGE_TXQ_BLOCK_LEVEL(txq->entries))
293e948693eSPhilip Paeps 		return;
294e948693eSPhilip Paeps 
295e948693eSPhilip Paeps 	txq->blocked = 1;
296e948693eSPhilip Paeps 
297e948693eSPhilip Paeps 	/*
298e948693eSPhilip Paeps 	 * Avoid a race with completion interrupt handling that could leave
299e948693eSPhilip Paeps 	 * the queue blocked.
300e948693eSPhilip Paeps 	 */
301e948693eSPhilip Paeps 	mb();
302e948693eSPhilip Paeps 	sfxge_tx_qreap(txq);
303e948693eSPhilip Paeps 	level = txq->added - txq->reaped;
304385b1d8eSGeorge V. Neville-Neil 	if (level < SFXGE_TXQ_BLOCK_LEVEL(txq->entries)) {
305e948693eSPhilip Paeps 		mb();
306e948693eSPhilip Paeps 		txq->blocked = 0;
307e948693eSPhilip Paeps 	}
308e948693eSPhilip Paeps }
309e948693eSPhilip Paeps 
310e948693eSPhilip Paeps static int sfxge_tx_queue_mbuf(struct sfxge_txq *txq, struct mbuf *mbuf)
311e948693eSPhilip Paeps {
312e948693eSPhilip Paeps 	bus_dmamap_t *used_map;
313e948693eSPhilip Paeps 	bus_dmamap_t map;
314e948693eSPhilip Paeps 	bus_dma_segment_t dma_seg[SFXGE_TX_MAPPING_MAX_SEG];
315e948693eSPhilip Paeps 	unsigned int id;
316e948693eSPhilip Paeps 	struct sfxge_tx_mapping *stmp;
317e948693eSPhilip Paeps 	efx_buffer_t *desc;
318e948693eSPhilip Paeps 	int n_dma_seg;
319e948693eSPhilip Paeps 	int rc;
320e948693eSPhilip Paeps 	int i;
321e948693eSPhilip Paeps 
322e948693eSPhilip Paeps 	KASSERT(!txq->blocked, ("txq->blocked"));
323e948693eSPhilip Paeps 
324e948693eSPhilip Paeps 	if (mbuf->m_pkthdr.csum_flags & CSUM_TSO)
325e948693eSPhilip Paeps 		prefetch_read_many(mbuf->m_data);
326e948693eSPhilip Paeps 
327851128b8SAndrew Rybchenko 	if (__predict_false(txq->init_state != SFXGE_TXQ_STARTED)) {
328e948693eSPhilip Paeps 		rc = EINTR;
329e948693eSPhilip Paeps 		goto reject;
330e948693eSPhilip Paeps 	}
331e948693eSPhilip Paeps 
332e948693eSPhilip Paeps 	/* Load the packet for DMA. */
333385b1d8eSGeorge V. Neville-Neil 	id = txq->added & txq->ptr_mask;
334e948693eSPhilip Paeps 	stmp = &txq->stmp[id];
335e948693eSPhilip Paeps 	rc = bus_dmamap_load_mbuf_sg(txq->packet_dma_tag, stmp->map,
336e948693eSPhilip Paeps 				     mbuf, dma_seg, &n_dma_seg, 0);
337e948693eSPhilip Paeps 	if (rc == EFBIG) {
338e948693eSPhilip Paeps 		/* Try again. */
339c6499eccSGleb Smirnoff 		struct mbuf *new_mbuf = m_collapse(mbuf, M_NOWAIT,
340e948693eSPhilip Paeps 						   SFXGE_TX_MAPPING_MAX_SEG);
341e948693eSPhilip Paeps 		if (new_mbuf == NULL)
342e948693eSPhilip Paeps 			goto reject;
343e948693eSPhilip Paeps 		++txq->collapses;
344e948693eSPhilip Paeps 		mbuf = new_mbuf;
345e948693eSPhilip Paeps 		rc = bus_dmamap_load_mbuf_sg(txq->packet_dma_tag,
346e948693eSPhilip Paeps 					     stmp->map, mbuf,
347e948693eSPhilip Paeps 					     dma_seg, &n_dma_seg, 0);
348e948693eSPhilip Paeps 	}
349e948693eSPhilip Paeps 	if (rc != 0)
350e948693eSPhilip Paeps 		goto reject;
351e948693eSPhilip Paeps 
352e948693eSPhilip Paeps 	/* Make the packet visible to the hardware. */
353e948693eSPhilip Paeps 	bus_dmamap_sync(txq->packet_dma_tag, stmp->map, BUS_DMASYNC_PREWRITE);
354e948693eSPhilip Paeps 
355e948693eSPhilip Paeps 	used_map = &stmp->map;
356e948693eSPhilip Paeps 
357e948693eSPhilip Paeps 	if (mbuf->m_pkthdr.csum_flags & CSUM_TSO) {
358e948693eSPhilip Paeps 		rc = sfxge_tx_queue_tso(txq, mbuf, dma_seg, n_dma_seg);
359e948693eSPhilip Paeps 		if (rc < 0)
360e948693eSPhilip Paeps 			goto reject_mapped;
361e948693eSPhilip Paeps 		stmp = &txq->stmp[rc];
362e948693eSPhilip Paeps 	} else {
363e948693eSPhilip Paeps 		/* Add the mapping to the fragment list, and set flags
364e948693eSPhilip Paeps 		 * for the buffer.
365e948693eSPhilip Paeps 		 */
366e948693eSPhilip Paeps 		i = 0;
367e948693eSPhilip Paeps 		for (;;) {
368e948693eSPhilip Paeps 			desc = &txq->pend_desc[i];
369e948693eSPhilip Paeps 			desc->eb_addr = dma_seg[i].ds_addr;
370e948693eSPhilip Paeps 			desc->eb_size = dma_seg[i].ds_len;
371e948693eSPhilip Paeps 			if (i == n_dma_seg - 1) {
372e948693eSPhilip Paeps 				desc->eb_eop = 1;
373e948693eSPhilip Paeps 				break;
374e948693eSPhilip Paeps 			}
375e948693eSPhilip Paeps 			desc->eb_eop = 0;
376e948693eSPhilip Paeps 			i++;
377e948693eSPhilip Paeps 
378e948693eSPhilip Paeps 			stmp->flags = 0;
379e948693eSPhilip Paeps 			if (__predict_false(stmp ==
380385b1d8eSGeorge V. Neville-Neil 					    &txq->stmp[txq->ptr_mask]))
381e948693eSPhilip Paeps 				stmp = &txq->stmp[0];
382e948693eSPhilip Paeps 			else
383e948693eSPhilip Paeps 				stmp++;
384e948693eSPhilip Paeps 		}
385e948693eSPhilip Paeps 		txq->n_pend_desc = n_dma_seg;
386e948693eSPhilip Paeps 	}
387e948693eSPhilip Paeps 
388e948693eSPhilip Paeps 	/*
389e948693eSPhilip Paeps 	 * If the mapping required more than one descriptor
390e948693eSPhilip Paeps 	 * then we need to associate the DMA map with the last
391e948693eSPhilip Paeps 	 * descriptor, not the first.
392e948693eSPhilip Paeps 	 */
393e948693eSPhilip Paeps 	if (used_map != &stmp->map) {
394e948693eSPhilip Paeps 		map = stmp->map;
395e948693eSPhilip Paeps 		stmp->map = *used_map;
396e948693eSPhilip Paeps 		*used_map = map;
397e948693eSPhilip Paeps 	}
398e948693eSPhilip Paeps 
399e948693eSPhilip Paeps 	stmp->u.mbuf = mbuf;
400e948693eSPhilip Paeps 	stmp->flags = TX_BUF_UNMAP | TX_BUF_MBUF;
401e948693eSPhilip Paeps 
402e948693eSPhilip Paeps 	/* Post the fragment list. */
403e948693eSPhilip Paeps 	sfxge_tx_qlist_post(txq);
404e948693eSPhilip Paeps 
405b7b0edd1SGeorge V. Neville-Neil 	return (0);
406e948693eSPhilip Paeps 
407e948693eSPhilip Paeps reject_mapped:
408e948693eSPhilip Paeps 	bus_dmamap_unload(txq->packet_dma_tag, *used_map);
409e948693eSPhilip Paeps reject:
410e948693eSPhilip Paeps 	/* Drop the packet on the floor. */
411e948693eSPhilip Paeps 	m_freem(mbuf);
412e948693eSPhilip Paeps 	++txq->drops;
413e948693eSPhilip Paeps 
414b7b0edd1SGeorge V. Neville-Neil 	return (rc);
415e948693eSPhilip Paeps }
416e948693eSPhilip Paeps 
417e948693eSPhilip Paeps /*
418e948693eSPhilip Paeps  * Drain the deferred packet list into the transmit queue.
419e948693eSPhilip Paeps  */
420e948693eSPhilip Paeps static void
421e948693eSPhilip Paeps sfxge_tx_qdpl_drain(struct sfxge_txq *txq)
422e948693eSPhilip Paeps {
423e948693eSPhilip Paeps 	struct sfxge_softc *sc;
424e948693eSPhilip Paeps 	struct sfxge_tx_dpl *stdp;
425e948693eSPhilip Paeps 	struct mbuf *mbuf, *next;
426e948693eSPhilip Paeps 	unsigned int count;
42793929f25SAndrew Rybchenko 	unsigned int non_tcp_count;
428e948693eSPhilip Paeps 	unsigned int pushed;
429e948693eSPhilip Paeps 	int rc;
430e948693eSPhilip Paeps 
431763cab71SAndrew Rybchenko 	SFXGE_TXQ_LOCK_ASSERT_OWNED(txq);
432e948693eSPhilip Paeps 
433e948693eSPhilip Paeps 	sc = txq->sc;
434e948693eSPhilip Paeps 	stdp = &txq->dpl;
435e948693eSPhilip Paeps 	pushed = txq->added;
436e948693eSPhilip Paeps 
43706824d2cSAndrew Rybchenko 	if (__predict_true(txq->init_state == SFXGE_TXQ_STARTED)) {
438e948693eSPhilip Paeps 		prefetch_read_many(sc->enp);
439e948693eSPhilip Paeps 		prefetch_read_many(txq->common);
44006824d2cSAndrew Rybchenko 	}
441e948693eSPhilip Paeps 
442e948693eSPhilip Paeps 	mbuf = stdp->std_get;
443bc85c897SGeorge V. Neville-Neil 	count = stdp->std_get_count;
44493929f25SAndrew Rybchenko 	non_tcp_count = stdp->std_get_non_tcp_count;
44593929f25SAndrew Rybchenko 
44693929f25SAndrew Rybchenko 	if (count > stdp->std_get_hiwat)
44793929f25SAndrew Rybchenko 		stdp->std_get_hiwat = count;
448e948693eSPhilip Paeps 
449e948693eSPhilip Paeps 	while (count != 0) {
450e948693eSPhilip Paeps 		KASSERT(mbuf != NULL, ("mbuf == NULL"));
451e948693eSPhilip Paeps 
452e948693eSPhilip Paeps 		next = mbuf->m_nextpkt;
453e948693eSPhilip Paeps 		mbuf->m_nextpkt = NULL;
454e948693eSPhilip Paeps 
455e948693eSPhilip Paeps 		ETHER_BPF_MTAP(sc->ifnet, mbuf); /* packet capture */
456e948693eSPhilip Paeps 
457e948693eSPhilip Paeps 		if (next != NULL)
458e948693eSPhilip Paeps 			prefetch_read_many(next);
459e948693eSPhilip Paeps 
460e948693eSPhilip Paeps 		rc = sfxge_tx_queue_mbuf(txq, mbuf);
461e948693eSPhilip Paeps 		--count;
46293929f25SAndrew Rybchenko 		non_tcp_count -= sfxge_is_mbuf_non_tcp(mbuf);
463e948693eSPhilip Paeps 		mbuf = next;
464e948693eSPhilip Paeps 		if (rc != 0)
465e948693eSPhilip Paeps 			continue;
466e948693eSPhilip Paeps 
467e948693eSPhilip Paeps 		if (txq->blocked)
468e948693eSPhilip Paeps 			break;
469e948693eSPhilip Paeps 
470e948693eSPhilip Paeps 		/* Push the fragments to the hardware in batches. */
471e948693eSPhilip Paeps 		if (txq->added - pushed >= SFXGE_TX_BATCH) {
472e948693eSPhilip Paeps 			efx_tx_qpush(txq->common, txq->added);
473e948693eSPhilip Paeps 			pushed = txq->added;
474e948693eSPhilip Paeps 		}
475e948693eSPhilip Paeps 	}
476e948693eSPhilip Paeps 
477e948693eSPhilip Paeps 	if (count == 0) {
478e948693eSPhilip Paeps 		KASSERT(mbuf == NULL, ("mbuf != NULL"));
47993929f25SAndrew Rybchenko 		KASSERT(non_tcp_count == 0,
48093929f25SAndrew Rybchenko 			("inconsistent TCP/non-TCP detection"));
481e948693eSPhilip Paeps 		stdp->std_get = NULL;
482bc85c897SGeorge V. Neville-Neil 		stdp->std_get_count = 0;
48393929f25SAndrew Rybchenko 		stdp->std_get_non_tcp_count = 0;
484e948693eSPhilip Paeps 		stdp->std_getp = &stdp->std_get;
485e948693eSPhilip Paeps 	} else {
486e948693eSPhilip Paeps 		stdp->std_get = mbuf;
487bc85c897SGeorge V. Neville-Neil 		stdp->std_get_count = count;
48893929f25SAndrew Rybchenko 		stdp->std_get_non_tcp_count = non_tcp_count;
489e948693eSPhilip Paeps 	}
490e948693eSPhilip Paeps 
491e948693eSPhilip Paeps 	if (txq->added != pushed)
492e948693eSPhilip Paeps 		efx_tx_qpush(txq->common, txq->added);
493e948693eSPhilip Paeps 
494bc85c897SGeorge V. Neville-Neil 	KASSERT(txq->blocked || stdp->std_get_count == 0,
495e948693eSPhilip Paeps 		("queue unblocked but count is non-zero"));
496e948693eSPhilip Paeps }
497e948693eSPhilip Paeps 
498e948693eSPhilip Paeps #define	SFXGE_TX_QDPL_PENDING(_txq)					\
499e948693eSPhilip Paeps 	((_txq)->dpl.std_put != 0)
500e948693eSPhilip Paeps 
501e948693eSPhilip Paeps /*
502e948693eSPhilip Paeps  * Service the deferred packet list.
503e948693eSPhilip Paeps  *
504e948693eSPhilip Paeps  * NOTE: drops the txq mutex!
505e948693eSPhilip Paeps  */
5060b28bbdcSAndrew Rybchenko static void
507e948693eSPhilip Paeps sfxge_tx_qdpl_service(struct sfxge_txq *txq)
508e948693eSPhilip Paeps {
509763cab71SAndrew Rybchenko 	SFXGE_TXQ_LOCK_ASSERT_OWNED(txq);
510e948693eSPhilip Paeps 
511e948693eSPhilip Paeps 	do {
512e948693eSPhilip Paeps 		if (SFXGE_TX_QDPL_PENDING(txq))
513e948693eSPhilip Paeps 			sfxge_tx_qdpl_swizzle(txq);
514e948693eSPhilip Paeps 
515e948693eSPhilip Paeps 		if (!txq->blocked)
516e948693eSPhilip Paeps 			sfxge_tx_qdpl_drain(txq);
517e948693eSPhilip Paeps 
518763cab71SAndrew Rybchenko 		SFXGE_TXQ_UNLOCK(txq);
519e948693eSPhilip Paeps 	} while (SFXGE_TX_QDPL_PENDING(txq) &&
520763cab71SAndrew Rybchenko 		 SFXGE_TXQ_TRYLOCK(txq));
521e948693eSPhilip Paeps }
522e948693eSPhilip Paeps 
523e948693eSPhilip Paeps /*
524d6e9f736SAndrew Rybchenko  * Put a packet on the deferred packet get-list.
525e948693eSPhilip Paeps  */
5260b28bbdcSAndrew Rybchenko static int
527d6e9f736SAndrew Rybchenko sfxge_tx_qdpl_put_locked(struct sfxge_txq *txq, struct mbuf *mbuf)
528e948693eSPhilip Paeps {
529e948693eSPhilip Paeps 	struct sfxge_tx_dpl *stdp;
530e948693eSPhilip Paeps 
531e948693eSPhilip Paeps 	stdp = &txq->dpl;
532e948693eSPhilip Paeps 
533e948693eSPhilip Paeps 	KASSERT(mbuf->m_nextpkt == NULL, ("mbuf->m_nextpkt != NULL"));
534e948693eSPhilip Paeps 
535763cab71SAndrew Rybchenko 	SFXGE_TXQ_LOCK_ASSERT_OWNED(txq);
536e948693eSPhilip Paeps 
53793929f25SAndrew Rybchenko 	if (stdp->std_get_count >= stdp->std_get_max) {
53893929f25SAndrew Rybchenko 		txq->get_overflow++;
539c1974e29SGleb Smirnoff 		return (ENOBUFS);
54093929f25SAndrew Rybchenko 	}
54193929f25SAndrew Rybchenko 	if (sfxge_is_mbuf_non_tcp(mbuf)) {
54293929f25SAndrew Rybchenko 		if (stdp->std_get_non_tcp_count >=
54393929f25SAndrew Rybchenko 		    stdp->std_get_non_tcp_max) {
54493929f25SAndrew Rybchenko 			txq->get_non_tcp_overflow++;
54593929f25SAndrew Rybchenko 			return (ENOBUFS);
54693929f25SAndrew Rybchenko 		}
54793929f25SAndrew Rybchenko 		stdp->std_get_non_tcp_count++;
54893929f25SAndrew Rybchenko 	}
549c1974e29SGleb Smirnoff 
550e948693eSPhilip Paeps 	*(stdp->std_getp) = mbuf;
551e948693eSPhilip Paeps 	stdp->std_getp = &mbuf->m_nextpkt;
552bc85c897SGeorge V. Neville-Neil 	stdp->std_get_count++;
553d6e9f736SAndrew Rybchenko 
554d6e9f736SAndrew Rybchenko 	return (0);
555d6e9f736SAndrew Rybchenko }
556d6e9f736SAndrew Rybchenko 
557d6e9f736SAndrew Rybchenko /*
558d6e9f736SAndrew Rybchenko  * Put a packet on the deferred packet put-list.
559d6e9f736SAndrew Rybchenko  *
560d6e9f736SAndrew Rybchenko  * We overload the csum_data field in the mbuf to keep track of this length
561d6e9f736SAndrew Rybchenko  * because there is no cheap alternative to avoid races.
562d6e9f736SAndrew Rybchenko  */
563d6e9f736SAndrew Rybchenko static int
564d6e9f736SAndrew Rybchenko sfxge_tx_qdpl_put_unlocked(struct sfxge_txq *txq, struct mbuf *mbuf)
565d6e9f736SAndrew Rybchenko {
566d6e9f736SAndrew Rybchenko 	struct sfxge_tx_dpl *stdp;
567e948693eSPhilip Paeps 	volatile uintptr_t *putp;
568e948693eSPhilip Paeps 	uintptr_t old;
569e948693eSPhilip Paeps 	uintptr_t new;
570e948693eSPhilip Paeps 	unsigned old_len;
571e948693eSPhilip Paeps 
572d6e9f736SAndrew Rybchenko 	KASSERT(mbuf->m_nextpkt == NULL, ("mbuf->m_nextpkt != NULL"));
573d6e9f736SAndrew Rybchenko 
574d6e9f736SAndrew Rybchenko 	SFXGE_TXQ_LOCK_ASSERT_NOTOWNED(txq);
575d6e9f736SAndrew Rybchenko 
576d6e9f736SAndrew Rybchenko 	stdp = &txq->dpl;
577e948693eSPhilip Paeps 	putp = &stdp->std_put;
578e948693eSPhilip Paeps 	new = (uintptr_t)mbuf;
579e948693eSPhilip Paeps 
580e948693eSPhilip Paeps 	do {
581e948693eSPhilip Paeps 		old = *putp;
582b7b0edd1SGeorge V. Neville-Neil 		if (old != 0) {
583e948693eSPhilip Paeps 			struct mbuf *mp = (struct mbuf *)old;
584e948693eSPhilip Paeps 			old_len = mp->m_pkthdr.csum_data;
585e948693eSPhilip Paeps 		} else
586e948693eSPhilip Paeps 			old_len = 0;
58793929f25SAndrew Rybchenko 		if (old_len >= stdp->std_put_max) {
58893929f25SAndrew Rybchenko 			atomic_add_long(&txq->put_overflow, 1);
589c1974e29SGleb Smirnoff 			return (ENOBUFS);
59093929f25SAndrew Rybchenko 		}
591e948693eSPhilip Paeps 		mbuf->m_pkthdr.csum_data = old_len + 1;
592e948693eSPhilip Paeps 		mbuf->m_nextpkt = (void *)old;
593fb8ccc78SMarius Strobl 	} while (atomic_cmpset_ptr(putp, old, new) == 0);
594e948693eSPhilip Paeps 
595e948693eSPhilip Paeps 	return (0);
596e948693eSPhilip Paeps }
597e948693eSPhilip Paeps 
598e948693eSPhilip Paeps /*
599e948693eSPhilip Paeps  * Called from if_transmit - will try to grab the txq lock and enqueue to the
600c071447aSAndrew Rybchenko  * put list if it succeeds, otherwise try to push onto the defer list if space.
601e948693eSPhilip Paeps  */
602e948693eSPhilip Paeps int
603e948693eSPhilip Paeps sfxge_tx_packet_add(struct sfxge_txq *txq, struct mbuf *m)
604e948693eSPhilip Paeps {
605e948693eSPhilip Paeps 	int rc;
606e948693eSPhilip Paeps 
607d7ac87d3SGleb Smirnoff 	if (!SFXGE_LINK_UP(txq->sc)) {
60893929f25SAndrew Rybchenko 		atomic_add_long(&txq->netdown_drops, 1);
6090e4ebe6cSAndrew Rybchenko 		return (ENETDOWN);
610d7ac87d3SGleb Smirnoff 	}
611d7ac87d3SGleb Smirnoff 
612e948693eSPhilip Paeps 	/*
613e948693eSPhilip Paeps 	 * Try to grab the txq lock.  If we are able to get the lock,
614e948693eSPhilip Paeps 	 * the packet will be appended to the "get list" of the deferred
615e948693eSPhilip Paeps 	 * packet list.  Otherwise, it will be pushed on the "put list".
616e948693eSPhilip Paeps 	 */
617deee1de4SAndrew Rybchenko 	if (SFXGE_TXQ_TRYLOCK(txq)) {
618d6e9f736SAndrew Rybchenko 		/* First swizzle put-list to get-list to keep order */
619d6e9f736SAndrew Rybchenko 		sfxge_tx_qdpl_swizzle(txq);
620d6e9f736SAndrew Rybchenko 
621d6e9f736SAndrew Rybchenko 		rc = sfxge_tx_qdpl_put_locked(txq, m);
622deee1de4SAndrew Rybchenko 
623deee1de4SAndrew Rybchenko 		/* Try to service the list. */
624deee1de4SAndrew Rybchenko 		sfxge_tx_qdpl_service(txq);
625deee1de4SAndrew Rybchenko 		/* Lock has been dropped. */
626d6e9f736SAndrew Rybchenko 	} else {
627d6e9f736SAndrew Rybchenko 		rc = sfxge_tx_qdpl_put_unlocked(txq, m);
628e948693eSPhilip Paeps 
629e948693eSPhilip Paeps 		/*
630e948693eSPhilip Paeps 		 * Try to grab the lock again.
631e948693eSPhilip Paeps 		 *
632d6e9f736SAndrew Rybchenko 		 * If we are able to get the lock, we need to process
633d6e9f736SAndrew Rybchenko 		 * the deferred packet list.  If we are not able to get
634d6e9f736SAndrew Rybchenko 		 * the lock, another thread is processing the list.
635e948693eSPhilip Paeps 		 */
636f080384cSAndrew Rybchenko 		if ((rc == 0) && SFXGE_TXQ_TRYLOCK(txq)) {
637e948693eSPhilip Paeps 			sfxge_tx_qdpl_service(txq);
638e948693eSPhilip Paeps 			/* Lock has been dropped. */
639e948693eSPhilip Paeps 		}
640deee1de4SAndrew Rybchenko 	}
641deee1de4SAndrew Rybchenko 
642deee1de4SAndrew Rybchenko 	SFXGE_TXQ_LOCK_ASSERT_NOTOWNED(txq);
643e948693eSPhilip Paeps 
644f080384cSAndrew Rybchenko 	return (rc);
645e948693eSPhilip Paeps }
646e948693eSPhilip Paeps 
647e948693eSPhilip Paeps static void
648e948693eSPhilip Paeps sfxge_tx_qdpl_flush(struct sfxge_txq *txq)
649e948693eSPhilip Paeps {
650e948693eSPhilip Paeps 	struct sfxge_tx_dpl *stdp = &txq->dpl;
651e948693eSPhilip Paeps 	struct mbuf *mbuf, *next;
652e948693eSPhilip Paeps 
653763cab71SAndrew Rybchenko 	SFXGE_TXQ_LOCK(txq);
654e948693eSPhilip Paeps 
655e948693eSPhilip Paeps 	sfxge_tx_qdpl_swizzle(txq);
656e948693eSPhilip Paeps 	for (mbuf = stdp->std_get; mbuf != NULL; mbuf = next) {
657e948693eSPhilip Paeps 		next = mbuf->m_nextpkt;
658e948693eSPhilip Paeps 		m_freem(mbuf);
659e948693eSPhilip Paeps 	}
660e948693eSPhilip Paeps 	stdp->std_get = NULL;
661bc85c897SGeorge V. Neville-Neil 	stdp->std_get_count = 0;
66293929f25SAndrew Rybchenko 	stdp->std_get_non_tcp_count = 0;
663e948693eSPhilip Paeps 	stdp->std_getp = &stdp->std_get;
664e948693eSPhilip Paeps 
665763cab71SAndrew Rybchenko 	SFXGE_TXQ_UNLOCK(txq);
666e948693eSPhilip Paeps }
667e948693eSPhilip Paeps 
668e948693eSPhilip Paeps void
669e948693eSPhilip Paeps sfxge_if_qflush(struct ifnet *ifp)
670e948693eSPhilip Paeps {
671e948693eSPhilip Paeps 	struct sfxge_softc *sc;
672cb552e88SAndrew Rybchenko 	unsigned int i;
673e948693eSPhilip Paeps 
674e948693eSPhilip Paeps 	sc = ifp->if_softc;
675e948693eSPhilip Paeps 
676e2b05fe2SAndrew Rybchenko 	for (i = 0; i < sc->txq_count; i++)
677e948693eSPhilip Paeps 		sfxge_tx_qdpl_flush(sc->txq[i]);
678e948693eSPhilip Paeps }
679e948693eSPhilip Paeps 
680e948693eSPhilip Paeps /*
681e948693eSPhilip Paeps  * TX start -- called by the stack.
682e948693eSPhilip Paeps  */
683e948693eSPhilip Paeps int
684e948693eSPhilip Paeps sfxge_if_transmit(struct ifnet *ifp, struct mbuf *m)
685e948693eSPhilip Paeps {
686e948693eSPhilip Paeps 	struct sfxge_softc *sc;
687e948693eSPhilip Paeps 	struct sfxge_txq *txq;
688e948693eSPhilip Paeps 	int rc;
689e948693eSPhilip Paeps 
690e948693eSPhilip Paeps 	sc = (struct sfxge_softc *)ifp->if_softc;
691e948693eSPhilip Paeps 
69287581ab8SAndrew Rybchenko 	/*
69387581ab8SAndrew Rybchenko 	 * Transmit may be called when interface is up from the kernel
69487581ab8SAndrew Rybchenko 	 * point of view, but not yet up (in progress) from the driver
69587581ab8SAndrew Rybchenko 	 * point of view. I.e. link aggregation bring up.
69687581ab8SAndrew Rybchenko 	 * Transmit may be called when interface is up from the driver
69787581ab8SAndrew Rybchenko 	 * point of view, but already down from the kernel point of
69887581ab8SAndrew Rybchenko 	 * view. I.e. Rx when interface shutdown is in progress.
69987581ab8SAndrew Rybchenko 	 */
70087581ab8SAndrew Rybchenko 	KASSERT((ifp->if_flags & IFF_UP) || (sc->if_flags & IFF_UP),
70187581ab8SAndrew Rybchenko 		("interface not up"));
702e948693eSPhilip Paeps 
703e948693eSPhilip Paeps 	/* Pick the desired transmit queue. */
704db7e53aaSAndrew Rybchenko 	if (m->m_pkthdr.csum_flags &
705db7e53aaSAndrew Rybchenko 	    (CSUM_DELAY_DATA | CSUM_TCP_IPV6 | CSUM_UDP_IPV6 | CSUM_TSO)) {
706e948693eSPhilip Paeps 		int index = 0;
707e948693eSPhilip Paeps 
708c2529042SHans Petter Selasky 		/* check if flowid is set */
709c2529042SHans Petter Selasky 		if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) {
710e948693eSPhilip Paeps 			uint32_t hash = m->m_pkthdr.flowid;
711e948693eSPhilip Paeps 
712e948693eSPhilip Paeps 			index = sc->rx_indir_table[hash % SFXGE_RX_SCALE_MAX];
713e948693eSPhilip Paeps 		}
714e948693eSPhilip Paeps 		txq = sc->txq[SFXGE_TXQ_IP_TCP_UDP_CKSUM + index];
715e948693eSPhilip Paeps 	} else if (m->m_pkthdr.csum_flags & CSUM_DELAY_IP) {
716e948693eSPhilip Paeps 		txq = sc->txq[SFXGE_TXQ_IP_CKSUM];
717e948693eSPhilip Paeps 	} else {
718e948693eSPhilip Paeps 		txq = sc->txq[SFXGE_TXQ_NON_CKSUM];
719e948693eSPhilip Paeps 	}
720e948693eSPhilip Paeps 
721e948693eSPhilip Paeps 	rc = sfxge_tx_packet_add(txq, m);
7220e4ebe6cSAndrew Rybchenko 	if (rc != 0)
7230e4ebe6cSAndrew Rybchenko 		m_freem(m);
724e948693eSPhilip Paeps 
725e948693eSPhilip Paeps 	return (rc);
726e948693eSPhilip Paeps }
727e948693eSPhilip Paeps 
728e948693eSPhilip Paeps /*
729e948693eSPhilip Paeps  * Software "TSO".  Not quite as good as doing it in hardware, but
730e948693eSPhilip Paeps  * still faster than segmenting in the stack.
731e948693eSPhilip Paeps  */
732e948693eSPhilip Paeps 
733e948693eSPhilip Paeps struct sfxge_tso_state {
734e948693eSPhilip Paeps 	/* Output position */
735e948693eSPhilip Paeps 	unsigned out_len;	/* Remaining length in current segment */
736e948693eSPhilip Paeps 	unsigned seqnum;	/* Current sequence number */
737e948693eSPhilip Paeps 	unsigned packet_space;	/* Remaining space in current packet */
738e948693eSPhilip Paeps 
739e948693eSPhilip Paeps 	/* Input position */
740e948693eSPhilip Paeps 	uint64_t dma_addr;	/* DMA address of current position */
741e948693eSPhilip Paeps 	unsigned in_len;	/* Remaining length in current mbuf */
742e948693eSPhilip Paeps 
743e948693eSPhilip Paeps 	const struct mbuf *mbuf; /* Input mbuf (head of chain) */
744e948693eSPhilip Paeps 	u_short protocol;	/* Network protocol (after VLAN decap) */
745e948693eSPhilip Paeps 	ssize_t nh_off;		/* Offset of network header */
746e948693eSPhilip Paeps 	ssize_t tcph_off;	/* Offset of TCP header */
747e948693eSPhilip Paeps 	unsigned header_len;	/* Number of bytes of header */
748d0f73877SAndrew Rybchenko 	unsigned seg_size;	/* TCP segment size */
749e948693eSPhilip Paeps };
750e948693eSPhilip Paeps 
7510b28bbdcSAndrew Rybchenko static const struct ip *tso_iph(const struct sfxge_tso_state *tso)
752e948693eSPhilip Paeps {
753e948693eSPhilip Paeps 	KASSERT(tso->protocol == htons(ETHERTYPE_IP),
754e948693eSPhilip Paeps 		("tso_iph() in non-IPv4 state"));
755e948693eSPhilip Paeps 	return (const struct ip *)(tso->mbuf->m_data + tso->nh_off);
756e948693eSPhilip Paeps }
7570b28bbdcSAndrew Rybchenko static __unused const struct ip6_hdr *tso_ip6h(const struct sfxge_tso_state *tso)
758e948693eSPhilip Paeps {
759e948693eSPhilip Paeps 	KASSERT(tso->protocol == htons(ETHERTYPE_IPV6),
760e948693eSPhilip Paeps 		("tso_ip6h() in non-IPv6 state"));
761e948693eSPhilip Paeps 	return (const struct ip6_hdr *)(tso->mbuf->m_data + tso->nh_off);
762e948693eSPhilip Paeps }
7630b28bbdcSAndrew Rybchenko static const struct tcphdr *tso_tcph(const struct sfxge_tso_state *tso)
764e948693eSPhilip Paeps {
765e948693eSPhilip Paeps 	return (const struct tcphdr *)(tso->mbuf->m_data + tso->tcph_off);
766e948693eSPhilip Paeps }
767e948693eSPhilip Paeps 
768e948693eSPhilip Paeps /* Size of preallocated TSO header buffers.  Larger blocks must be
769e948693eSPhilip Paeps  * allocated from the heap.
770e948693eSPhilip Paeps  */
771e948693eSPhilip Paeps #define	TSOH_STD_SIZE	128
772e948693eSPhilip Paeps 
773e948693eSPhilip Paeps /* At most half the descriptors in the queue at any time will refer to
774e948693eSPhilip Paeps  * a TSO header buffer, since they must always be followed by a
775e948693eSPhilip Paeps  * payload descriptor referring to an mbuf.
776e948693eSPhilip Paeps  */
777385b1d8eSGeorge V. Neville-Neil #define	TSOH_COUNT(_txq_entries)	((_txq_entries) / 2u)
778e948693eSPhilip Paeps #define	TSOH_PER_PAGE	(PAGE_SIZE / TSOH_STD_SIZE)
779385b1d8eSGeorge V. Neville-Neil #define	TSOH_PAGE_COUNT(_txq_entries)	\
780385b1d8eSGeorge V. Neville-Neil 	((TSOH_COUNT(_txq_entries) + TSOH_PER_PAGE - 1) / TSOH_PER_PAGE)
781e948693eSPhilip Paeps 
782e948693eSPhilip Paeps static int tso_init(struct sfxge_txq *txq)
783e948693eSPhilip Paeps {
784e948693eSPhilip Paeps 	struct sfxge_softc *sc = txq->sc;
785385b1d8eSGeorge V. Neville-Neil 	unsigned int tsoh_page_count = TSOH_PAGE_COUNT(sc->txq_entries);
786e948693eSPhilip Paeps 	int i, rc;
787e948693eSPhilip Paeps 
788e948693eSPhilip Paeps 	/* Allocate TSO header buffers */
789385b1d8eSGeorge V. Neville-Neil 	txq->tsoh_buffer = malloc(tsoh_page_count * sizeof(txq->tsoh_buffer[0]),
790e948693eSPhilip Paeps 				  M_SFXGE, M_WAITOK);
791e948693eSPhilip Paeps 
792385b1d8eSGeorge V. Neville-Neil 	for (i = 0; i < tsoh_page_count; i++) {
793e948693eSPhilip Paeps 		rc = sfxge_dma_alloc(sc, PAGE_SIZE, &txq->tsoh_buffer[i]);
794b7b0edd1SGeorge V. Neville-Neil 		if (rc != 0)
795e948693eSPhilip Paeps 			goto fail;
796e948693eSPhilip Paeps 	}
797e948693eSPhilip Paeps 
798b7b0edd1SGeorge V. Neville-Neil 	return (0);
799e948693eSPhilip Paeps 
800e948693eSPhilip Paeps fail:
801e948693eSPhilip Paeps 	while (i-- > 0)
802e948693eSPhilip Paeps 		sfxge_dma_free(&txq->tsoh_buffer[i]);
803e948693eSPhilip Paeps 	free(txq->tsoh_buffer, M_SFXGE);
804e948693eSPhilip Paeps 	txq->tsoh_buffer = NULL;
805b7b0edd1SGeorge V. Neville-Neil 	return (rc);
806e948693eSPhilip Paeps }
807e948693eSPhilip Paeps 
808e948693eSPhilip Paeps static void tso_fini(struct sfxge_txq *txq)
809e948693eSPhilip Paeps {
810e948693eSPhilip Paeps 	int i;
811e948693eSPhilip Paeps 
812b7b0edd1SGeorge V. Neville-Neil 	if (txq->tsoh_buffer != NULL) {
813385b1d8eSGeorge V. Neville-Neil 		for (i = 0; i < TSOH_PAGE_COUNT(txq->sc->txq_entries); i++)
814e948693eSPhilip Paeps 			sfxge_dma_free(&txq->tsoh_buffer[i]);
815e948693eSPhilip Paeps 		free(txq->tsoh_buffer, M_SFXGE);
816e948693eSPhilip Paeps 	}
817e948693eSPhilip Paeps }
818e948693eSPhilip Paeps 
819e948693eSPhilip Paeps static void tso_start(struct sfxge_tso_state *tso, struct mbuf *mbuf)
820e948693eSPhilip Paeps {
821e948693eSPhilip Paeps 	struct ether_header *eh = mtod(mbuf, struct ether_header *);
822b2c43c38SAndrew Rybchenko 	const struct tcphdr *th;
823b2c43c38SAndrew Rybchenko 	struct tcphdr th_copy;
824e948693eSPhilip Paeps 
825e948693eSPhilip Paeps 	tso->mbuf = mbuf;
826e948693eSPhilip Paeps 
827e948693eSPhilip Paeps 	/* Find network protocol and header */
828e948693eSPhilip Paeps 	tso->protocol = eh->ether_type;
829e948693eSPhilip Paeps 	if (tso->protocol == htons(ETHERTYPE_VLAN)) {
830e948693eSPhilip Paeps 		struct ether_vlan_header *veh =
831e948693eSPhilip Paeps 			mtod(mbuf, struct ether_vlan_header *);
832e948693eSPhilip Paeps 		tso->protocol = veh->evl_proto;
833e948693eSPhilip Paeps 		tso->nh_off = sizeof(*veh);
834e948693eSPhilip Paeps 	} else {
835e948693eSPhilip Paeps 		tso->nh_off = sizeof(*eh);
836e948693eSPhilip Paeps 	}
837e948693eSPhilip Paeps 
838e948693eSPhilip Paeps 	/* Find TCP header */
839e948693eSPhilip Paeps 	if (tso->protocol == htons(ETHERTYPE_IP)) {
840e948693eSPhilip Paeps 		KASSERT(tso_iph(tso)->ip_p == IPPROTO_TCP,
841e948693eSPhilip Paeps 			("TSO required on non-TCP packet"));
842e948693eSPhilip Paeps 		tso->tcph_off = tso->nh_off + 4 * tso_iph(tso)->ip_hl;
843e948693eSPhilip Paeps 	} else {
844e948693eSPhilip Paeps 		KASSERT(tso->protocol == htons(ETHERTYPE_IPV6),
845e948693eSPhilip Paeps 			("TSO required on non-IP packet"));
846e948693eSPhilip Paeps 		KASSERT(tso_ip6h(tso)->ip6_nxt == IPPROTO_TCP,
847e948693eSPhilip Paeps 			("TSO required on non-TCP packet"));
848e948693eSPhilip Paeps 		tso->tcph_off = tso->nh_off + sizeof(struct ip6_hdr);
849e948693eSPhilip Paeps 	}
850e948693eSPhilip Paeps 
851b2c43c38SAndrew Rybchenko 	KASSERT(mbuf->m_len >= tso->tcph_off,
852b2c43c38SAndrew Rybchenko 		("network header is fragmented in mbuf"));
853b2c43c38SAndrew Rybchenko 	/* We need TCP header including flags (window is the next) */
854b2c43c38SAndrew Rybchenko 	if (mbuf->m_len < tso->tcph_off + offsetof(struct tcphdr, th_win)) {
855b2c43c38SAndrew Rybchenko 		m_copydata(tso->mbuf, tso->tcph_off, sizeof(th_copy),
856b2c43c38SAndrew Rybchenko 			   (caddr_t)&th_copy);
857b2c43c38SAndrew Rybchenko 		th = &th_copy;
858b2c43c38SAndrew Rybchenko 	} else {
859b2c43c38SAndrew Rybchenko 		th = tso_tcph(tso);
860b2c43c38SAndrew Rybchenko 	}
861b2c43c38SAndrew Rybchenko 
862b2c43c38SAndrew Rybchenko 	tso->header_len = tso->tcph_off + 4 * th->th_off;
863d0f73877SAndrew Rybchenko 	tso->seg_size = mbuf->m_pkthdr.tso_segsz;
864e948693eSPhilip Paeps 
865b2c43c38SAndrew Rybchenko 	tso->seqnum = ntohl(th->th_seq);
866e948693eSPhilip Paeps 
867e948693eSPhilip Paeps 	/* These flags must not be duplicated */
868b2c43c38SAndrew Rybchenko 	KASSERT(!(th->th_flags & (TH_URG | TH_SYN | TH_RST)),
869e948693eSPhilip Paeps 		("incompatible TCP flag on TSO packet"));
870e948693eSPhilip Paeps 
871e948693eSPhilip Paeps 	tso->out_len = mbuf->m_pkthdr.len - tso->header_len;
872e948693eSPhilip Paeps }
873e948693eSPhilip Paeps 
874e948693eSPhilip Paeps /*
875e948693eSPhilip Paeps  * tso_fill_packet_with_fragment - form descriptors for the current fragment
876e948693eSPhilip Paeps  *
877e948693eSPhilip Paeps  * Form descriptors for the current fragment, until we reach the end
878e948693eSPhilip Paeps  * of fragment or end-of-packet.  Return 0 on success, 1 if not enough
879e948693eSPhilip Paeps  * space.
880e948693eSPhilip Paeps  */
881e948693eSPhilip Paeps static void tso_fill_packet_with_fragment(struct sfxge_txq *txq,
882e948693eSPhilip Paeps 					  struct sfxge_tso_state *tso)
883e948693eSPhilip Paeps {
884e948693eSPhilip Paeps 	efx_buffer_t *desc;
885e948693eSPhilip Paeps 	int n;
886e948693eSPhilip Paeps 
887e948693eSPhilip Paeps 	if (tso->in_len == 0 || tso->packet_space == 0)
888e948693eSPhilip Paeps 		return;
889e948693eSPhilip Paeps 
890e948693eSPhilip Paeps 	KASSERT(tso->in_len > 0, ("TSO input length went negative"));
891e948693eSPhilip Paeps 	KASSERT(tso->packet_space > 0, ("TSO packet space went negative"));
892e948693eSPhilip Paeps 
893e948693eSPhilip Paeps 	n = min(tso->in_len, tso->packet_space);
894e948693eSPhilip Paeps 
895e948693eSPhilip Paeps 	tso->packet_space -= n;
896e948693eSPhilip Paeps 	tso->out_len -= n;
897e948693eSPhilip Paeps 	tso->in_len -= n;
898e948693eSPhilip Paeps 
899e948693eSPhilip Paeps 	desc = &txq->pend_desc[txq->n_pend_desc++];
900e948693eSPhilip Paeps 	desc->eb_addr = tso->dma_addr;
901e948693eSPhilip Paeps 	desc->eb_size = n;
902e948693eSPhilip Paeps 	desc->eb_eop = tso->out_len == 0 || tso->packet_space == 0;
903e948693eSPhilip Paeps 
904e948693eSPhilip Paeps 	tso->dma_addr += n;
905e948693eSPhilip Paeps }
906e948693eSPhilip Paeps 
907e948693eSPhilip Paeps /* Callback from bus_dmamap_load() for long TSO headers. */
908e948693eSPhilip Paeps static void tso_map_long_header(void *dma_addr_ret,
909e948693eSPhilip Paeps 				bus_dma_segment_t *segs, int nseg,
910e948693eSPhilip Paeps 				int error)
911e948693eSPhilip Paeps {
912e948693eSPhilip Paeps 	*(uint64_t *)dma_addr_ret = ((__predict_true(error == 0) &&
913e948693eSPhilip Paeps 				      __predict_true(nseg == 1)) ?
914e948693eSPhilip Paeps 				     segs->ds_addr : 0);
915e948693eSPhilip Paeps }
916e948693eSPhilip Paeps 
917e948693eSPhilip Paeps /*
918e948693eSPhilip Paeps  * tso_start_new_packet - generate a new header and prepare for the new packet
919e948693eSPhilip Paeps  *
920e948693eSPhilip Paeps  * Generate a new header and prepare for the new packet.  Return 0 on
921e948693eSPhilip Paeps  * success, or an error code if failed to alloc header.
922e948693eSPhilip Paeps  */
923e948693eSPhilip Paeps static int tso_start_new_packet(struct sfxge_txq *txq,
924e948693eSPhilip Paeps 				struct sfxge_tso_state *tso,
925e948693eSPhilip Paeps 				unsigned int id)
926e948693eSPhilip Paeps {
927e948693eSPhilip Paeps 	struct sfxge_tx_mapping *stmp = &txq->stmp[id];
928e948693eSPhilip Paeps 	struct tcphdr *tsoh_th;
929e948693eSPhilip Paeps 	unsigned ip_length;
930e948693eSPhilip Paeps 	caddr_t header;
931e948693eSPhilip Paeps 	uint64_t dma_addr;
932e948693eSPhilip Paeps 	bus_dmamap_t map;
933e948693eSPhilip Paeps 	efx_buffer_t *desc;
934e948693eSPhilip Paeps 	int rc;
935e948693eSPhilip Paeps 
936e948693eSPhilip Paeps 	/* Allocate a DMA-mapped header buffer. */
937e948693eSPhilip Paeps 	if (__predict_true(tso->header_len <= TSOH_STD_SIZE)) {
938e948693eSPhilip Paeps 		unsigned int page_index = (id / 2) / TSOH_PER_PAGE;
939e948693eSPhilip Paeps 		unsigned int buf_index = (id / 2) % TSOH_PER_PAGE;
940e948693eSPhilip Paeps 
941e948693eSPhilip Paeps 		header = (txq->tsoh_buffer[page_index].esm_base +
942e948693eSPhilip Paeps 			  buf_index * TSOH_STD_SIZE);
943e948693eSPhilip Paeps 		dma_addr = (txq->tsoh_buffer[page_index].esm_addr +
944e948693eSPhilip Paeps 			    buf_index * TSOH_STD_SIZE);
945e948693eSPhilip Paeps 		map = txq->tsoh_buffer[page_index].esm_map;
946e948693eSPhilip Paeps 
947e948693eSPhilip Paeps 		stmp->flags = 0;
948e948693eSPhilip Paeps 	} else {
949e948693eSPhilip Paeps 		/* We cannot use bus_dmamem_alloc() as that may sleep */
950e948693eSPhilip Paeps 		header = malloc(tso->header_len, M_SFXGE, M_NOWAIT);
951e948693eSPhilip Paeps 		if (__predict_false(!header))
952b7b0edd1SGeorge V. Neville-Neil 			return (ENOMEM);
953e948693eSPhilip Paeps 		rc = bus_dmamap_load(txq->packet_dma_tag, stmp->map,
954e948693eSPhilip Paeps 				     header, tso->header_len,
955e948693eSPhilip Paeps 				     tso_map_long_header, &dma_addr,
956e948693eSPhilip Paeps 				     BUS_DMA_NOWAIT);
957e948693eSPhilip Paeps 		if (__predict_false(dma_addr == 0)) {
958e948693eSPhilip Paeps 			if (rc == 0) {
959e948693eSPhilip Paeps 				/* Succeeded but got >1 segment */
960e948693eSPhilip Paeps 				bus_dmamap_unload(txq->packet_dma_tag,
961e948693eSPhilip Paeps 						  stmp->map);
962e948693eSPhilip Paeps 				rc = EINVAL;
963e948693eSPhilip Paeps 			}
964e948693eSPhilip Paeps 			free(header, M_SFXGE);
965b7b0edd1SGeorge V. Neville-Neil 			return (rc);
966e948693eSPhilip Paeps 		}
967e948693eSPhilip Paeps 		map = stmp->map;
968e948693eSPhilip Paeps 
969e948693eSPhilip Paeps 		txq->tso_long_headers++;
970e948693eSPhilip Paeps 		stmp->u.heap_buf = header;
971e948693eSPhilip Paeps 		stmp->flags = TX_BUF_UNMAP;
972e948693eSPhilip Paeps 	}
973e948693eSPhilip Paeps 
974e948693eSPhilip Paeps 	tsoh_th = (struct tcphdr *)(header + tso->tcph_off);
975e948693eSPhilip Paeps 
976e948693eSPhilip Paeps 	/* Copy and update the headers. */
977a35485aaSAndrew Rybchenko 	m_copydata(tso->mbuf, 0, tso->header_len, header);
978e948693eSPhilip Paeps 
979e948693eSPhilip Paeps 	tsoh_th->th_seq = htonl(tso->seqnum);
980d0f73877SAndrew Rybchenko 	tso->seqnum += tso->seg_size;
981d0f73877SAndrew Rybchenko 	if (tso->out_len > tso->seg_size) {
982e948693eSPhilip Paeps 		/* This packet will not finish the TSO burst. */
983d0f73877SAndrew Rybchenko 		ip_length = tso->header_len - tso->nh_off + tso->seg_size;
984e948693eSPhilip Paeps 		tsoh_th->th_flags &= ~(TH_FIN | TH_PUSH);
985e948693eSPhilip Paeps 	} else {
986e948693eSPhilip Paeps 		/* This packet will be the last in the TSO burst. */
987e948693eSPhilip Paeps 		ip_length = tso->header_len - tso->nh_off + tso->out_len;
988e948693eSPhilip Paeps 	}
989e948693eSPhilip Paeps 
990e948693eSPhilip Paeps 	if (tso->protocol == htons(ETHERTYPE_IP)) {
991e948693eSPhilip Paeps 		struct ip *tsoh_iph = (struct ip *)(header + tso->nh_off);
992e948693eSPhilip Paeps 		tsoh_iph->ip_len = htons(ip_length);
993e948693eSPhilip Paeps 		/* XXX We should increment ip_id, but FreeBSD doesn't
994e948693eSPhilip Paeps 		 * currently allocate extra IDs for multiple segments.
995e948693eSPhilip Paeps 		 */
996e948693eSPhilip Paeps 	} else {
997e948693eSPhilip Paeps 		struct ip6_hdr *tsoh_iph =
998e948693eSPhilip Paeps 			(struct ip6_hdr *)(header + tso->nh_off);
999e948693eSPhilip Paeps 		tsoh_iph->ip6_plen = htons(ip_length - sizeof(*tsoh_iph));
1000e948693eSPhilip Paeps 	}
1001e948693eSPhilip Paeps 
1002e948693eSPhilip Paeps 	/* Make the header visible to the hardware. */
1003e948693eSPhilip Paeps 	bus_dmamap_sync(txq->packet_dma_tag, map, BUS_DMASYNC_PREWRITE);
1004e948693eSPhilip Paeps 
1005d0f73877SAndrew Rybchenko 	tso->packet_space = tso->seg_size;
1006e948693eSPhilip Paeps 	txq->tso_packets++;
1007e948693eSPhilip Paeps 
1008e948693eSPhilip Paeps 	/* Form a descriptor for this header. */
1009e948693eSPhilip Paeps 	desc = &txq->pend_desc[txq->n_pend_desc++];
1010e948693eSPhilip Paeps 	desc->eb_addr = dma_addr;
1011e948693eSPhilip Paeps 	desc->eb_size = tso->header_len;
1012e948693eSPhilip Paeps 	desc->eb_eop = 0;
1013e948693eSPhilip Paeps 
1014b7b0edd1SGeorge V. Neville-Neil 	return (0);
1015e948693eSPhilip Paeps }
1016e948693eSPhilip Paeps 
1017e948693eSPhilip Paeps static int
1018e948693eSPhilip Paeps sfxge_tx_queue_tso(struct sfxge_txq *txq, struct mbuf *mbuf,
1019e948693eSPhilip Paeps 		   const bus_dma_segment_t *dma_seg, int n_dma_seg)
1020e948693eSPhilip Paeps {
1021e948693eSPhilip Paeps 	struct sfxge_tso_state tso;
1022e948693eSPhilip Paeps 	unsigned int id, next_id;
1023a35485aaSAndrew Rybchenko 	unsigned skipped = 0;
1024e948693eSPhilip Paeps 
1025e948693eSPhilip Paeps 	tso_start(&tso, mbuf);
1026e948693eSPhilip Paeps 
1027a35485aaSAndrew Rybchenko 	while (dma_seg->ds_len + skipped <= tso.header_len) {
1028a35485aaSAndrew Rybchenko 		skipped += dma_seg->ds_len;
1029e948693eSPhilip Paeps 		--n_dma_seg;
1030e948693eSPhilip Paeps 		KASSERT(n_dma_seg, ("no payload found in TSO packet"));
1031e948693eSPhilip Paeps 		++dma_seg;
1032e948693eSPhilip Paeps 	}
1033cfaf34ffSAndrew Rybchenko 	tso.in_len = dma_seg->ds_len - (tso.header_len - skipped);
1034a35485aaSAndrew Rybchenko 	tso.dma_addr = dma_seg->ds_addr + (tso.header_len - skipped);
1035e948693eSPhilip Paeps 
1036385b1d8eSGeorge V. Neville-Neil 	id = txq->added & txq->ptr_mask;
1037e948693eSPhilip Paeps 	if (__predict_false(tso_start_new_packet(txq, &tso, id)))
1038385b1d8eSGeorge V. Neville-Neil 		return (-1);
1039e948693eSPhilip Paeps 
1040e948693eSPhilip Paeps 	while (1) {
1041385b1d8eSGeorge V. Neville-Neil 		id = (id + 1) & txq->ptr_mask;
1042e948693eSPhilip Paeps 		tso_fill_packet_with_fragment(txq, &tso);
1043e948693eSPhilip Paeps 
1044e948693eSPhilip Paeps 		/* Move onto the next fragment? */
1045e948693eSPhilip Paeps 		if (tso.in_len == 0) {
1046e948693eSPhilip Paeps 			--n_dma_seg;
1047e948693eSPhilip Paeps 			if (n_dma_seg == 0)
1048e948693eSPhilip Paeps 				break;
1049e948693eSPhilip Paeps 			++dma_seg;
1050e948693eSPhilip Paeps 			tso.in_len = dma_seg->ds_len;
1051e948693eSPhilip Paeps 			tso.dma_addr = dma_seg->ds_addr;
1052e948693eSPhilip Paeps 		}
1053e948693eSPhilip Paeps 
1054e948693eSPhilip Paeps 		/* End of packet? */
1055e948693eSPhilip Paeps 		if (tso.packet_space == 0) {
1056e948693eSPhilip Paeps 			/* If the queue is now full due to tiny MSS,
1057e948693eSPhilip Paeps 			 * or we can't create another header, discard
1058e948693eSPhilip Paeps 			 * the remainder of the input mbuf but do not
1059e948693eSPhilip Paeps 			 * roll back the work we have done.
1060e948693eSPhilip Paeps 			 */
10614d0b294bSAndrew Rybchenko 			if (txq->n_pend_desc + 1 /* header */ + n_dma_seg >
10624d0b294bSAndrew Rybchenko 			    SFXGE_TSO_MAX_DESC) {
1063e1a3d10eSAndrew Rybchenko 				txq->tso_pdrop_too_many++;
1064e948693eSPhilip Paeps 				break;
1065e1a3d10eSAndrew Rybchenko 			}
1066385b1d8eSGeorge V. Neville-Neil 			next_id = (id + 1) & txq->ptr_mask;
1067e948693eSPhilip Paeps 			if (__predict_false(tso_start_new_packet(txq, &tso,
1068e1a3d10eSAndrew Rybchenko 								 next_id))) {
1069e1a3d10eSAndrew Rybchenko 				txq->tso_pdrop_no_rsrc++;
1070e948693eSPhilip Paeps 				break;
1071e1a3d10eSAndrew Rybchenko 			}
1072e948693eSPhilip Paeps 			id = next_id;
1073e948693eSPhilip Paeps 		}
1074e948693eSPhilip Paeps 	}
1075e948693eSPhilip Paeps 
1076e948693eSPhilip Paeps 	txq->tso_bursts++;
1077b7b0edd1SGeorge V. Neville-Neil 	return (id);
1078e948693eSPhilip Paeps }
1079e948693eSPhilip Paeps 
1080e948693eSPhilip Paeps static void
1081e948693eSPhilip Paeps sfxge_tx_qunblock(struct sfxge_txq *txq)
1082e948693eSPhilip Paeps {
1083e948693eSPhilip Paeps 	struct sfxge_softc *sc;
1084e948693eSPhilip Paeps 	struct sfxge_evq *evq;
1085e948693eSPhilip Paeps 
1086e948693eSPhilip Paeps 	sc = txq->sc;
1087e948693eSPhilip Paeps 	evq = sc->evq[txq->evq_index];
1088e948693eSPhilip Paeps 
1089763cab71SAndrew Rybchenko 	SFXGE_EVQ_LOCK_ASSERT_OWNED(evq);
1090e948693eSPhilip Paeps 
1091851128b8SAndrew Rybchenko 	if (__predict_false(txq->init_state != SFXGE_TXQ_STARTED))
1092e948693eSPhilip Paeps 		return;
1093e948693eSPhilip Paeps 
1094763cab71SAndrew Rybchenko 	SFXGE_TXQ_LOCK(txq);
1095e948693eSPhilip Paeps 
1096e948693eSPhilip Paeps 	if (txq->blocked) {
1097e948693eSPhilip Paeps 		unsigned int level;
1098e948693eSPhilip Paeps 
1099e948693eSPhilip Paeps 		level = txq->added - txq->completed;
11006d73545eSAndrew Rybchenko 		if (level <= SFXGE_TXQ_UNBLOCK_LEVEL(txq->entries)) {
11016d73545eSAndrew Rybchenko 			/* reaped must be in sync with blocked */
11026d73545eSAndrew Rybchenko 			sfxge_tx_qreap(txq);
1103e948693eSPhilip Paeps 			txq->blocked = 0;
1104e948693eSPhilip Paeps 		}
11056d73545eSAndrew Rybchenko 	}
1106e948693eSPhilip Paeps 
1107e948693eSPhilip Paeps 	sfxge_tx_qdpl_service(txq);
1108e948693eSPhilip Paeps 	/* note: lock has been dropped */
1109e948693eSPhilip Paeps }
1110e948693eSPhilip Paeps 
1111e948693eSPhilip Paeps void
1112e948693eSPhilip Paeps sfxge_tx_qflush_done(struct sfxge_txq *txq)
1113e948693eSPhilip Paeps {
1114e948693eSPhilip Paeps 
1115e948693eSPhilip Paeps 	txq->flush_state = SFXGE_FLUSH_DONE;
1116e948693eSPhilip Paeps }
1117e948693eSPhilip Paeps 
1118e948693eSPhilip Paeps static void
1119e948693eSPhilip Paeps sfxge_tx_qstop(struct sfxge_softc *sc, unsigned int index)
1120e948693eSPhilip Paeps {
1121e948693eSPhilip Paeps 	struct sfxge_txq *txq;
1122e948693eSPhilip Paeps 	struct sfxge_evq *evq;
1123e948693eSPhilip Paeps 	unsigned int count;
1124e948693eSPhilip Paeps 
1125e948693eSPhilip Paeps 	txq = sc->txq[index];
1126e948693eSPhilip Paeps 	evq = sc->evq[txq->evq_index];
1127e948693eSPhilip Paeps 
1128763cab71SAndrew Rybchenko 	SFXGE_TXQ_LOCK(txq);
1129e948693eSPhilip Paeps 
1130e948693eSPhilip Paeps 	KASSERT(txq->init_state == SFXGE_TXQ_STARTED,
1131e948693eSPhilip Paeps 	    ("txq->init_state != SFXGE_TXQ_STARTED"));
1132e948693eSPhilip Paeps 
1133e948693eSPhilip Paeps 	txq->init_state = SFXGE_TXQ_INITIALIZED;
1134e948693eSPhilip Paeps 	txq->flush_state = SFXGE_FLUSH_PENDING;
1135e948693eSPhilip Paeps 
1136e948693eSPhilip Paeps 	/* Flush the transmit queue. */
1137e948693eSPhilip Paeps 	efx_tx_qflush(txq->common);
1138e948693eSPhilip Paeps 
1139763cab71SAndrew Rybchenko 	SFXGE_TXQ_UNLOCK(txq);
1140e948693eSPhilip Paeps 
1141e948693eSPhilip Paeps 	count = 0;
1142e948693eSPhilip Paeps 	do {
1143e948693eSPhilip Paeps 		/* Spin for 100ms. */
1144e948693eSPhilip Paeps 		DELAY(100000);
1145e948693eSPhilip Paeps 
1146e948693eSPhilip Paeps 		if (txq->flush_state != SFXGE_FLUSH_PENDING)
1147e948693eSPhilip Paeps 			break;
1148e948693eSPhilip Paeps 	} while (++count < 20);
1149e948693eSPhilip Paeps 
1150763cab71SAndrew Rybchenko 	SFXGE_EVQ_LOCK(evq);
1151763cab71SAndrew Rybchenko 	SFXGE_TXQ_LOCK(txq);
1152e948693eSPhilip Paeps 
1153e948693eSPhilip Paeps 	KASSERT(txq->flush_state != SFXGE_FLUSH_FAILED,
1154e948693eSPhilip Paeps 	    ("txq->flush_state == SFXGE_FLUSH_FAILED"));
1155e948693eSPhilip Paeps 
1156e948693eSPhilip Paeps 	txq->flush_state = SFXGE_FLUSH_DONE;
1157e948693eSPhilip Paeps 
1158e948693eSPhilip Paeps 	txq->blocked = 0;
1159e948693eSPhilip Paeps 	txq->pending = txq->added;
1160e948693eSPhilip Paeps 
1161cc933626SAndrew Rybchenko 	sfxge_tx_qcomplete(txq, evq);
1162e948693eSPhilip Paeps 	KASSERT(txq->completed == txq->added,
1163e948693eSPhilip Paeps 	    ("txq->completed != txq->added"));
1164e948693eSPhilip Paeps 
1165e948693eSPhilip Paeps 	sfxge_tx_qreap(txq);
1166e948693eSPhilip Paeps 	KASSERT(txq->reaped == txq->completed,
1167e948693eSPhilip Paeps 	    ("txq->reaped != txq->completed"));
1168e948693eSPhilip Paeps 
1169e948693eSPhilip Paeps 	txq->added = 0;
1170e948693eSPhilip Paeps 	txq->pending = 0;
1171e948693eSPhilip Paeps 	txq->completed = 0;
1172e948693eSPhilip Paeps 	txq->reaped = 0;
1173e948693eSPhilip Paeps 
1174e948693eSPhilip Paeps 	/* Destroy the common code transmit queue. */
1175e948693eSPhilip Paeps 	efx_tx_qdestroy(txq->common);
1176e948693eSPhilip Paeps 	txq->common = NULL;
1177e948693eSPhilip Paeps 
1178e948693eSPhilip Paeps 	efx_sram_buf_tbl_clear(sc->enp, txq->buf_base_id,
1179385b1d8eSGeorge V. Neville-Neil 	    EFX_TXQ_NBUFS(sc->txq_entries));
1180e948693eSPhilip Paeps 
1181763cab71SAndrew Rybchenko 	SFXGE_EVQ_UNLOCK(evq);
1182763cab71SAndrew Rybchenko 	SFXGE_TXQ_UNLOCK(txq);
1183e948693eSPhilip Paeps }
1184e948693eSPhilip Paeps 
1185e948693eSPhilip Paeps static int
1186e948693eSPhilip Paeps sfxge_tx_qstart(struct sfxge_softc *sc, unsigned int index)
1187e948693eSPhilip Paeps {
1188e948693eSPhilip Paeps 	struct sfxge_txq *txq;
1189e948693eSPhilip Paeps 	efsys_mem_t *esmp;
1190e948693eSPhilip Paeps 	uint16_t flags;
1191e948693eSPhilip Paeps 	struct sfxge_evq *evq;
1192e948693eSPhilip Paeps 	int rc;
1193e948693eSPhilip Paeps 
1194e948693eSPhilip Paeps 	txq = sc->txq[index];
1195e948693eSPhilip Paeps 	esmp = &txq->mem;
1196e948693eSPhilip Paeps 	evq = sc->evq[txq->evq_index];
1197e948693eSPhilip Paeps 
1198e948693eSPhilip Paeps 	KASSERT(txq->init_state == SFXGE_TXQ_INITIALIZED,
1199e948693eSPhilip Paeps 	    ("txq->init_state != SFXGE_TXQ_INITIALIZED"));
1200e948693eSPhilip Paeps 	KASSERT(evq->init_state == SFXGE_EVQ_STARTED,
1201e948693eSPhilip Paeps 	    ("evq->init_state != SFXGE_EVQ_STARTED"));
1202e948693eSPhilip Paeps 
1203e948693eSPhilip Paeps 	/* Program the buffer table. */
1204e948693eSPhilip Paeps 	if ((rc = efx_sram_buf_tbl_set(sc->enp, txq->buf_base_id, esmp,
1205385b1d8eSGeorge V. Neville-Neil 	    EFX_TXQ_NBUFS(sc->txq_entries))) != 0)
1206385b1d8eSGeorge V. Neville-Neil 		return (rc);
1207e948693eSPhilip Paeps 
1208e948693eSPhilip Paeps 	/* Determine the kind of queue we are creating. */
1209e948693eSPhilip Paeps 	switch (txq->type) {
1210e948693eSPhilip Paeps 	case SFXGE_TXQ_NON_CKSUM:
1211e948693eSPhilip Paeps 		flags = 0;
1212e948693eSPhilip Paeps 		break;
1213e948693eSPhilip Paeps 	case SFXGE_TXQ_IP_CKSUM:
1214e948693eSPhilip Paeps 		flags = EFX_CKSUM_IPV4;
1215e948693eSPhilip Paeps 		break;
1216e948693eSPhilip Paeps 	case SFXGE_TXQ_IP_TCP_UDP_CKSUM:
1217e948693eSPhilip Paeps 		flags = EFX_CKSUM_IPV4 | EFX_CKSUM_TCPUDP;
1218e948693eSPhilip Paeps 		break;
1219e948693eSPhilip Paeps 	default:
1220e948693eSPhilip Paeps 		KASSERT(0, ("Impossible TX queue"));
1221e948693eSPhilip Paeps 		flags = 0;
1222e948693eSPhilip Paeps 		break;
1223e948693eSPhilip Paeps 	}
1224e948693eSPhilip Paeps 
1225e948693eSPhilip Paeps 	/* Create the common code transmit queue. */
1226cf07c70dSGeorge V. Neville-Neil 	if ((rc = efx_tx_qcreate(sc->enp, index, txq->type, esmp,
1227385b1d8eSGeorge V. Neville-Neil 	    sc->txq_entries, txq->buf_base_id, flags, evq->common,
1228e948693eSPhilip Paeps 	    &txq->common)) != 0)
1229e948693eSPhilip Paeps 		goto fail;
1230e948693eSPhilip Paeps 
1231763cab71SAndrew Rybchenko 	SFXGE_TXQ_LOCK(txq);
1232e948693eSPhilip Paeps 
1233e948693eSPhilip Paeps 	/* Enable the transmit queue. */
1234e948693eSPhilip Paeps 	efx_tx_qenable(txq->common);
1235e948693eSPhilip Paeps 
1236e948693eSPhilip Paeps 	txq->init_state = SFXGE_TXQ_STARTED;
1237e948693eSPhilip Paeps 
1238763cab71SAndrew Rybchenko 	SFXGE_TXQ_UNLOCK(txq);
1239e948693eSPhilip Paeps 
1240e948693eSPhilip Paeps 	return (0);
1241e948693eSPhilip Paeps 
1242e948693eSPhilip Paeps fail:
1243e948693eSPhilip Paeps 	efx_sram_buf_tbl_clear(sc->enp, txq->buf_base_id,
1244385b1d8eSGeorge V. Neville-Neil 	    EFX_TXQ_NBUFS(sc->txq_entries));
1245385b1d8eSGeorge V. Neville-Neil 	return (rc);
1246e948693eSPhilip Paeps }
1247e948693eSPhilip Paeps 
1248e948693eSPhilip Paeps void
1249e948693eSPhilip Paeps sfxge_tx_stop(struct sfxge_softc *sc)
1250e948693eSPhilip Paeps {
1251e948693eSPhilip Paeps 	int index;
1252e948693eSPhilip Paeps 
1253e2b05fe2SAndrew Rybchenko 	index = sc->txq_count;
1254e948693eSPhilip Paeps 	while (--index >= 0)
1255e2b05fe2SAndrew Rybchenko 		sfxge_tx_qstop(sc, index);
1256e948693eSPhilip Paeps 
1257e948693eSPhilip Paeps 	/* Tear down the transmit module */
1258e948693eSPhilip Paeps 	efx_tx_fini(sc->enp);
1259e948693eSPhilip Paeps }
1260e948693eSPhilip Paeps 
1261e948693eSPhilip Paeps int
1262e948693eSPhilip Paeps sfxge_tx_start(struct sfxge_softc *sc)
1263e948693eSPhilip Paeps {
1264e948693eSPhilip Paeps 	int index;
1265e948693eSPhilip Paeps 	int rc;
1266e948693eSPhilip Paeps 
1267e948693eSPhilip Paeps 	/* Initialize the common code transmit module. */
1268e948693eSPhilip Paeps 	if ((rc = efx_tx_init(sc->enp)) != 0)
1269e948693eSPhilip Paeps 		return (rc);
1270e948693eSPhilip Paeps 
1271e2b05fe2SAndrew Rybchenko 	for (index = 0; index < sc->txq_count; index++) {
1272e2b05fe2SAndrew Rybchenko 		if ((rc = sfxge_tx_qstart(sc, index)) != 0)
1273e948693eSPhilip Paeps 			goto fail;
1274e948693eSPhilip Paeps 	}
1275e948693eSPhilip Paeps 
1276e948693eSPhilip Paeps 	return (0);
1277e948693eSPhilip Paeps 
1278e948693eSPhilip Paeps fail:
1279e2b05fe2SAndrew Rybchenko 	while (--index >= 0)
1280e2b05fe2SAndrew Rybchenko 		sfxge_tx_qstop(sc, index);
1281e2b05fe2SAndrew Rybchenko 
1282e948693eSPhilip Paeps 	efx_tx_fini(sc->enp);
1283e948693eSPhilip Paeps 
1284e948693eSPhilip Paeps 	return (rc);
1285e948693eSPhilip Paeps }
1286e948693eSPhilip Paeps 
1287f6222d7bSAndrew Rybchenko static int
1288f6222d7bSAndrew Rybchenko sfxge_txq_stat_init(struct sfxge_txq *txq, struct sysctl_oid *txq_node)
1289f6222d7bSAndrew Rybchenko {
1290f6222d7bSAndrew Rybchenko 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(txq->sc->dev);
1291f6222d7bSAndrew Rybchenko 	struct sysctl_oid *stat_node;
1292f6222d7bSAndrew Rybchenko 	unsigned int id;
1293f6222d7bSAndrew Rybchenko 
1294f6222d7bSAndrew Rybchenko 	stat_node = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(txq_node), OID_AUTO,
1295f6222d7bSAndrew Rybchenko 				    "stats", CTLFLAG_RD, NULL,
1296f6222d7bSAndrew Rybchenko 				    "Tx queue statistics");
1297f6222d7bSAndrew Rybchenko 	if (stat_node == NULL)
1298f6222d7bSAndrew Rybchenko 		return (ENOMEM);
1299f6222d7bSAndrew Rybchenko 
1300f6222d7bSAndrew Rybchenko 	for (id = 0; id < nitems(sfxge_tx_stats); id++) {
1301f6222d7bSAndrew Rybchenko 		SYSCTL_ADD_ULONG(
1302f6222d7bSAndrew Rybchenko 		    ctx, SYSCTL_CHILDREN(stat_node), OID_AUTO,
1303f6222d7bSAndrew Rybchenko 		    sfxge_tx_stats[id].name, CTLFLAG_RD | CTLFLAG_STATS,
1304f6222d7bSAndrew Rybchenko 		    (unsigned long *)((caddr_t)txq + sfxge_tx_stats[id].offset),
1305f6222d7bSAndrew Rybchenko 		    "");
1306f6222d7bSAndrew Rybchenko 	}
1307f6222d7bSAndrew Rybchenko 
1308f6222d7bSAndrew Rybchenko 	return (0);
1309f6222d7bSAndrew Rybchenko }
1310f6222d7bSAndrew Rybchenko 
1311e948693eSPhilip Paeps /**
1312e948693eSPhilip Paeps  * Destroy a transmit queue.
1313e948693eSPhilip Paeps  */
1314e948693eSPhilip Paeps static void
1315e948693eSPhilip Paeps sfxge_tx_qfini(struct sfxge_softc *sc, unsigned int index)
1316e948693eSPhilip Paeps {
1317e948693eSPhilip Paeps 	struct sfxge_txq *txq;
1318385b1d8eSGeorge V. Neville-Neil 	unsigned int nmaps;
1319e948693eSPhilip Paeps 
1320e948693eSPhilip Paeps 	txq = sc->txq[index];
1321e948693eSPhilip Paeps 
1322e948693eSPhilip Paeps 	KASSERT(txq->init_state == SFXGE_TXQ_INITIALIZED,
1323e948693eSPhilip Paeps 	    ("txq->init_state != SFXGE_TXQ_INITIALIZED"));
1324e948693eSPhilip Paeps 
1325e948693eSPhilip Paeps 	if (txq->type == SFXGE_TXQ_IP_TCP_UDP_CKSUM)
1326e948693eSPhilip Paeps 		tso_fini(txq);
1327e948693eSPhilip Paeps 
1328e948693eSPhilip Paeps 	/* Free the context arrays. */
1329e948693eSPhilip Paeps 	free(txq->pend_desc, M_SFXGE);
1330385b1d8eSGeorge V. Neville-Neil 	nmaps = sc->txq_entries;
1331b7b0edd1SGeorge V. Neville-Neil 	while (nmaps-- != 0)
1332e948693eSPhilip Paeps 		bus_dmamap_destroy(txq->packet_dma_tag, txq->stmp[nmaps].map);
1333e948693eSPhilip Paeps 	free(txq->stmp, M_SFXGE);
1334e948693eSPhilip Paeps 
1335e948693eSPhilip Paeps 	/* Release DMA memory mapping. */
1336e948693eSPhilip Paeps 	sfxge_dma_free(&txq->mem);
1337e948693eSPhilip Paeps 
1338e948693eSPhilip Paeps 	sc->txq[index] = NULL;
1339e948693eSPhilip Paeps 
1340763cab71SAndrew Rybchenko 	SFXGE_TXQ_LOCK_DESTROY(txq);
1341e948693eSPhilip Paeps 
1342e948693eSPhilip Paeps 	free(txq, M_SFXGE);
1343e948693eSPhilip Paeps }
1344e948693eSPhilip Paeps 
1345e948693eSPhilip Paeps static int
1346e948693eSPhilip Paeps sfxge_tx_qinit(struct sfxge_softc *sc, unsigned int txq_index,
1347e948693eSPhilip Paeps     enum sfxge_txq_type type, unsigned int evq_index)
1348e948693eSPhilip Paeps {
1349bc85c897SGeorge V. Neville-Neil 	char name[16];
135095caaf0fSAndrew Rybchenko 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev);
1351bc85c897SGeorge V. Neville-Neil 	struct sysctl_oid *txq_node;
1352e948693eSPhilip Paeps 	struct sfxge_txq *txq;
1353e948693eSPhilip Paeps 	struct sfxge_evq *evq;
1354e948693eSPhilip Paeps 	struct sfxge_tx_dpl *stdp;
135595caaf0fSAndrew Rybchenko 	struct sysctl_oid *dpl_node;
1356e948693eSPhilip Paeps 	efsys_mem_t *esmp;
1357e948693eSPhilip Paeps 	unsigned int nmaps;
1358e948693eSPhilip Paeps 	int rc;
1359e948693eSPhilip Paeps 
1360e948693eSPhilip Paeps 	txq = malloc(sizeof(struct sfxge_txq), M_SFXGE, M_ZERO | M_WAITOK);
1361e948693eSPhilip Paeps 	txq->sc = sc;
1362385b1d8eSGeorge V. Neville-Neil 	txq->entries = sc->txq_entries;
1363385b1d8eSGeorge V. Neville-Neil 	txq->ptr_mask = txq->entries - 1;
1364e948693eSPhilip Paeps 
1365e948693eSPhilip Paeps 	sc->txq[txq_index] = txq;
1366e948693eSPhilip Paeps 	esmp = &txq->mem;
1367e948693eSPhilip Paeps 
1368e948693eSPhilip Paeps 	evq = sc->evq[evq_index];
1369e948693eSPhilip Paeps 
1370e948693eSPhilip Paeps 	/* Allocate and zero DMA space for the descriptor ring. */
1371385b1d8eSGeorge V. Neville-Neil 	if ((rc = sfxge_dma_alloc(sc, EFX_TXQ_SIZE(sc->txq_entries), esmp)) != 0)
1372e948693eSPhilip Paeps 		return (rc);
1373e948693eSPhilip Paeps 
1374e948693eSPhilip Paeps 	/* Allocate buffer table entries. */
1375385b1d8eSGeorge V. Neville-Neil 	sfxge_sram_buf_tbl_alloc(sc, EFX_TXQ_NBUFS(sc->txq_entries),
1376e948693eSPhilip Paeps 				 &txq->buf_base_id);
1377e948693eSPhilip Paeps 
1378e948693eSPhilip Paeps 	/* Create a DMA tag for packet mappings. */
1379fb8ccc78SMarius Strobl 	if (bus_dma_tag_create(sc->parent_dma_tag, 1, 0x1000,
1380fb8ccc78SMarius Strobl 	    MIN(0x3FFFFFFFFFFFUL, BUS_SPACE_MAXADDR), BUS_SPACE_MAXADDR, NULL,
1381fb8ccc78SMarius Strobl 	    NULL, 0x11000, SFXGE_TX_MAPPING_MAX_SEG, 0x1000, 0, NULL, NULL,
1382e948693eSPhilip Paeps 	    &txq->packet_dma_tag) != 0) {
1383e948693eSPhilip Paeps 		device_printf(sc->dev, "Couldn't allocate txq DMA tag\n");
1384e948693eSPhilip Paeps 		rc = ENOMEM;
1385e948693eSPhilip Paeps 		goto fail;
1386e948693eSPhilip Paeps 	}
1387e948693eSPhilip Paeps 
1388e948693eSPhilip Paeps 	/* Allocate pending descriptor array for batching writes. */
1389385b1d8eSGeorge V. Neville-Neil 	txq->pend_desc = malloc(sizeof(efx_buffer_t) * sc->txq_entries,
1390e948693eSPhilip Paeps 				M_SFXGE, M_ZERO | M_WAITOK);
1391e948693eSPhilip Paeps 
1392e948693eSPhilip Paeps 	/* Allocate and initialise mbuf DMA mapping array. */
1393385b1d8eSGeorge V. Neville-Neil 	txq->stmp = malloc(sizeof(struct sfxge_tx_mapping) * sc->txq_entries,
1394e948693eSPhilip Paeps 	    M_SFXGE, M_ZERO | M_WAITOK);
1395385b1d8eSGeorge V. Neville-Neil 	for (nmaps = 0; nmaps < sc->txq_entries; nmaps++) {
1396e948693eSPhilip Paeps 		rc = bus_dmamap_create(txq->packet_dma_tag, 0,
1397e948693eSPhilip Paeps 				       &txq->stmp[nmaps].map);
1398e948693eSPhilip Paeps 		if (rc != 0)
1399e948693eSPhilip Paeps 			goto fail2;
1400e948693eSPhilip Paeps 	}
1401e948693eSPhilip Paeps 
1402bc85c897SGeorge V. Neville-Neil 	snprintf(name, sizeof(name), "%u", txq_index);
140395caaf0fSAndrew Rybchenko 	txq_node = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(sc->txqs_node),
1404bc85c897SGeorge V. Neville-Neil 				   OID_AUTO, name, CTLFLAG_RD, NULL, "");
1405bc85c897SGeorge V. Neville-Neil 	if (txq_node == NULL) {
1406bc85c897SGeorge V. Neville-Neil 		rc = ENOMEM;
1407bc85c897SGeorge V. Neville-Neil 		goto fail_txq_node;
1408bc85c897SGeorge V. Neville-Neil 	}
1409bc85c897SGeorge V. Neville-Neil 
1410e948693eSPhilip Paeps 	if (type == SFXGE_TXQ_IP_TCP_UDP_CKSUM &&
1411e948693eSPhilip Paeps 	    (rc = tso_init(txq)) != 0)
1412e948693eSPhilip Paeps 		goto fail3;
1413e948693eSPhilip Paeps 
1414060a95efSGeorge V. Neville-Neil 	if (sfxge_tx_dpl_get_max <= 0) {
1415060a95efSGeorge V. Neville-Neil 		log(LOG_ERR, "%s=%d must be greater than 0",
1416060a95efSGeorge V. Neville-Neil 		    SFXGE_PARAM_TX_DPL_GET_MAX, sfxge_tx_dpl_get_max);
1417060a95efSGeorge V. Neville-Neil 		rc = EINVAL;
1418060a95efSGeorge V. Neville-Neil 		goto fail_tx_dpl_get_max;
1419060a95efSGeorge V. Neville-Neil 	}
142093929f25SAndrew Rybchenko 	if (sfxge_tx_dpl_get_non_tcp_max <= 0) {
142193929f25SAndrew Rybchenko 		log(LOG_ERR, "%s=%d must be greater than 0",
142293929f25SAndrew Rybchenko 		    SFXGE_PARAM_TX_DPL_GET_NON_TCP_MAX,
142393929f25SAndrew Rybchenko 		    sfxge_tx_dpl_get_non_tcp_max);
142493929f25SAndrew Rybchenko 		rc = EINVAL;
142593929f25SAndrew Rybchenko 		goto fail_tx_dpl_get_max;
142693929f25SAndrew Rybchenko 	}
1427060a95efSGeorge V. Neville-Neil 	if (sfxge_tx_dpl_put_max < 0) {
1428060a95efSGeorge V. Neville-Neil 		log(LOG_ERR, "%s=%d must be greater or equal to 0",
1429060a95efSGeorge V. Neville-Neil 		    SFXGE_PARAM_TX_DPL_PUT_MAX, sfxge_tx_dpl_put_max);
1430060a95efSGeorge V. Neville-Neil 		rc = EINVAL;
1431060a95efSGeorge V. Neville-Neil 		goto fail_tx_dpl_put_max;
1432060a95efSGeorge V. Neville-Neil 	}
1433060a95efSGeorge V. Neville-Neil 
1434e948693eSPhilip Paeps 	/* Initialize the deferred packet list. */
1435e948693eSPhilip Paeps 	stdp = &txq->dpl;
1436060a95efSGeorge V. Neville-Neil 	stdp->std_put_max = sfxge_tx_dpl_put_max;
1437060a95efSGeorge V. Neville-Neil 	stdp->std_get_max = sfxge_tx_dpl_get_max;
143893929f25SAndrew Rybchenko 	stdp->std_get_non_tcp_max = sfxge_tx_dpl_get_non_tcp_max;
1439e948693eSPhilip Paeps 	stdp->std_getp = &stdp->std_get;
1440e948693eSPhilip Paeps 
144133d45dc5SAndrew Rybchenko 	SFXGE_TXQ_LOCK_INIT(txq, device_get_nameunit(sc->dev), txq_index);
1442bc85c897SGeorge V. Neville-Neil 
144395caaf0fSAndrew Rybchenko 	dpl_node = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(txq_node), OID_AUTO,
144495caaf0fSAndrew Rybchenko 				   "dpl", CTLFLAG_RD, NULL,
144595caaf0fSAndrew Rybchenko 				   "Deferred packet list statistics");
144695caaf0fSAndrew Rybchenko 	if (dpl_node == NULL) {
144795caaf0fSAndrew Rybchenko 		rc = ENOMEM;
144895caaf0fSAndrew Rybchenko 		goto fail_dpl_node;
144995caaf0fSAndrew Rybchenko 	}
145095caaf0fSAndrew Rybchenko 
145195caaf0fSAndrew Rybchenko 	SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(dpl_node), OID_AUTO,
145295caaf0fSAndrew Rybchenko 			"get_count", CTLFLAG_RD | CTLFLAG_STATS,
1453bc85c897SGeorge V. Neville-Neil 			&stdp->std_get_count, 0, "");
145495caaf0fSAndrew Rybchenko 	SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(dpl_node), OID_AUTO,
145595caaf0fSAndrew Rybchenko 			"get_non_tcp_count", CTLFLAG_RD | CTLFLAG_STATS,
145693929f25SAndrew Rybchenko 			&stdp->std_get_non_tcp_count, 0, "");
145795caaf0fSAndrew Rybchenko 	SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(dpl_node), OID_AUTO,
145895caaf0fSAndrew Rybchenko 			"get_hiwat", CTLFLAG_RD | CTLFLAG_STATS,
145993929f25SAndrew Rybchenko 			&stdp->std_get_hiwat, 0, "");
146095caaf0fSAndrew Rybchenko 	SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(dpl_node), OID_AUTO,
146195caaf0fSAndrew Rybchenko 			"put_hiwat", CTLFLAG_RD | CTLFLAG_STATS,
1462bce6d281SAndrew Rybchenko 			&stdp->std_put_hiwat, 0, "");
1463e948693eSPhilip Paeps 
1464f6222d7bSAndrew Rybchenko 	rc = sfxge_txq_stat_init(txq, txq_node);
1465f6222d7bSAndrew Rybchenko 	if (rc != 0)
1466f6222d7bSAndrew Rybchenko 		goto fail_txq_stat_init;
1467f6222d7bSAndrew Rybchenko 
1468e948693eSPhilip Paeps 	txq->type = type;
1469e948693eSPhilip Paeps 	txq->evq_index = evq_index;
1470e948693eSPhilip Paeps 	txq->txq_index = txq_index;
1471e948693eSPhilip Paeps 	txq->init_state = SFXGE_TXQ_INITIALIZED;
1472e948693eSPhilip Paeps 
1473e948693eSPhilip Paeps 	return (0);
1474e948693eSPhilip Paeps 
1475f6222d7bSAndrew Rybchenko fail_txq_stat_init:
147695caaf0fSAndrew Rybchenko fail_dpl_node:
1477060a95efSGeorge V. Neville-Neil fail_tx_dpl_put_max:
1478060a95efSGeorge V. Neville-Neil fail_tx_dpl_get_max:
1479e948693eSPhilip Paeps fail3:
1480bc85c897SGeorge V. Neville-Neil fail_txq_node:
1481e948693eSPhilip Paeps 	free(txq->pend_desc, M_SFXGE);
1482e948693eSPhilip Paeps fail2:
1483b7b0edd1SGeorge V. Neville-Neil 	while (nmaps-- != 0)
1484e948693eSPhilip Paeps 		bus_dmamap_destroy(txq->packet_dma_tag, txq->stmp[nmaps].map);
1485e948693eSPhilip Paeps 	free(txq->stmp, M_SFXGE);
1486e948693eSPhilip Paeps 	bus_dma_tag_destroy(txq->packet_dma_tag);
1487e948693eSPhilip Paeps 
1488e948693eSPhilip Paeps fail:
1489e948693eSPhilip Paeps 	sfxge_dma_free(esmp);
1490e948693eSPhilip Paeps 
1491e948693eSPhilip Paeps 	return (rc);
1492e948693eSPhilip Paeps }
1493e948693eSPhilip Paeps 
1494e948693eSPhilip Paeps static int
1495e948693eSPhilip Paeps sfxge_tx_stat_handler(SYSCTL_HANDLER_ARGS)
1496e948693eSPhilip Paeps {
1497e948693eSPhilip Paeps 	struct sfxge_softc *sc = arg1;
1498e948693eSPhilip Paeps 	unsigned int id = arg2;
1499e948693eSPhilip Paeps 	unsigned long sum;
1500e948693eSPhilip Paeps 	unsigned int index;
1501e948693eSPhilip Paeps 
1502e948693eSPhilip Paeps 	/* Sum across all TX queues */
1503e948693eSPhilip Paeps 	sum = 0;
1504e2b05fe2SAndrew Rybchenko 	for (index = 0; index < sc->txq_count; index++)
1505e948693eSPhilip Paeps 		sum += *(unsigned long *)((caddr_t)sc->txq[index] +
1506e948693eSPhilip Paeps 					  sfxge_tx_stats[id].offset);
1507e948693eSPhilip Paeps 
1508b7b0edd1SGeorge V. Neville-Neil 	return (SYSCTL_OUT(req, &sum, sizeof(sum)));
1509e948693eSPhilip Paeps }
1510e948693eSPhilip Paeps 
1511e948693eSPhilip Paeps static void
1512e948693eSPhilip Paeps sfxge_tx_stat_init(struct sfxge_softc *sc)
1513e948693eSPhilip Paeps {
1514e948693eSPhilip Paeps 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev);
1515e948693eSPhilip Paeps 	struct sysctl_oid_list *stat_list;
1516e948693eSPhilip Paeps 	unsigned int id;
1517e948693eSPhilip Paeps 
1518e948693eSPhilip Paeps 	stat_list = SYSCTL_CHILDREN(sc->stats_node);
1519e948693eSPhilip Paeps 
1520612d8e28SAndrew Rybchenko 	for (id = 0; id < nitems(sfxge_tx_stats); id++) {
1521e948693eSPhilip Paeps 		SYSCTL_ADD_PROC(
1522e948693eSPhilip Paeps 			ctx, stat_list,
1523e948693eSPhilip Paeps 			OID_AUTO, sfxge_tx_stats[id].name,
1524e948693eSPhilip Paeps 			CTLTYPE_ULONG|CTLFLAG_RD,
1525e948693eSPhilip Paeps 			sc, id, sfxge_tx_stat_handler, "LU",
1526e948693eSPhilip Paeps 			"");
1527e948693eSPhilip Paeps 	}
1528e948693eSPhilip Paeps }
1529e948693eSPhilip Paeps 
15303d8fce27SAndrew Rybchenko uint64_t
15313d8fce27SAndrew Rybchenko sfxge_tx_get_drops(struct sfxge_softc *sc)
15323d8fce27SAndrew Rybchenko {
15333d8fce27SAndrew Rybchenko 	unsigned int index;
15343d8fce27SAndrew Rybchenko 	uint64_t drops = 0;
15353d8fce27SAndrew Rybchenko 	struct sfxge_txq *txq;
15363d8fce27SAndrew Rybchenko 
15373d8fce27SAndrew Rybchenko 	/* Sum across all TX queues */
15383d8fce27SAndrew Rybchenko 	for (index = 0; index < sc->txq_count; index++) {
15393d8fce27SAndrew Rybchenko 		txq = sc->txq[index];
15403d8fce27SAndrew Rybchenko 		/*
15413d8fce27SAndrew Rybchenko 		 * In theory, txq->put_overflow and txq->netdown_drops
15423d8fce27SAndrew Rybchenko 		 * should use atomic operation and other should be
15433d8fce27SAndrew Rybchenko 		 * obtained under txq lock, but it is just statistics.
15443d8fce27SAndrew Rybchenko 		 */
15453d8fce27SAndrew Rybchenko 		drops += txq->drops + txq->get_overflow +
15463d8fce27SAndrew Rybchenko 			 txq->get_non_tcp_overflow +
15473d8fce27SAndrew Rybchenko 			 txq->put_overflow + txq->netdown_drops +
15483d8fce27SAndrew Rybchenko 			 txq->tso_pdrop_too_many + txq->tso_pdrop_no_rsrc;
15493d8fce27SAndrew Rybchenko 	}
15503d8fce27SAndrew Rybchenko 	return (drops);
15513d8fce27SAndrew Rybchenko }
15523d8fce27SAndrew Rybchenko 
1553e948693eSPhilip Paeps void
1554e948693eSPhilip Paeps sfxge_tx_fini(struct sfxge_softc *sc)
1555e948693eSPhilip Paeps {
1556e948693eSPhilip Paeps 	int index;
1557e948693eSPhilip Paeps 
1558e2b05fe2SAndrew Rybchenko 	index = sc->txq_count;
1559e948693eSPhilip Paeps 	while (--index >= 0)
1560e2b05fe2SAndrew Rybchenko 		sfxge_tx_qfini(sc, index);
1561e948693eSPhilip Paeps 
1562e2b05fe2SAndrew Rybchenko 	sc->txq_count = 0;
1563e948693eSPhilip Paeps }
1564e948693eSPhilip Paeps 
1565e948693eSPhilip Paeps 
1566e948693eSPhilip Paeps int
1567e948693eSPhilip Paeps sfxge_tx_init(struct sfxge_softc *sc)
1568e948693eSPhilip Paeps {
1569e948693eSPhilip Paeps 	struct sfxge_intr *intr;
1570e948693eSPhilip Paeps 	int index;
1571e948693eSPhilip Paeps 	int rc;
1572e948693eSPhilip Paeps 
1573e948693eSPhilip Paeps 	intr = &sc->intr;
1574e948693eSPhilip Paeps 
1575e948693eSPhilip Paeps 	KASSERT(intr->state == SFXGE_INTR_INITIALIZED,
1576e948693eSPhilip Paeps 	    ("intr->state != SFXGE_INTR_INITIALIZED"));
1577e948693eSPhilip Paeps 
1578e2b05fe2SAndrew Rybchenko 	sc->txq_count = SFXGE_TXQ_NTYPES - 1 + sc->intr.n_alloc;
1579e2b05fe2SAndrew Rybchenko 
1580bc85c897SGeorge V. Neville-Neil 	sc->txqs_node = SYSCTL_ADD_NODE(
1581bc85c897SGeorge V. Neville-Neil 		device_get_sysctl_ctx(sc->dev),
1582bc85c897SGeorge V. Neville-Neil 		SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)),
1583bc85c897SGeorge V. Neville-Neil 		OID_AUTO, "txq", CTLFLAG_RD, NULL, "Tx queues");
1584bc85c897SGeorge V. Neville-Neil 	if (sc->txqs_node == NULL) {
1585bc85c897SGeorge V. Neville-Neil 		rc = ENOMEM;
1586bc85c897SGeorge V. Neville-Neil 		goto fail_txq_node;
1587bc85c897SGeorge V. Neville-Neil 	}
1588bc85c897SGeorge V. Neville-Neil 
1589e948693eSPhilip Paeps 	/* Initialize the transmit queues */
1590e948693eSPhilip Paeps 	if ((rc = sfxge_tx_qinit(sc, SFXGE_TXQ_NON_CKSUM,
1591e948693eSPhilip Paeps 	    SFXGE_TXQ_NON_CKSUM, 0)) != 0)
1592e948693eSPhilip Paeps 		goto fail;
1593e948693eSPhilip Paeps 
1594e948693eSPhilip Paeps 	if ((rc = sfxge_tx_qinit(sc, SFXGE_TXQ_IP_CKSUM,
1595e948693eSPhilip Paeps 	    SFXGE_TXQ_IP_CKSUM, 0)) != 0)
1596e948693eSPhilip Paeps 		goto fail2;
1597e948693eSPhilip Paeps 
1598e2b05fe2SAndrew Rybchenko 	for (index = 0;
1599e2b05fe2SAndrew Rybchenko 	     index < sc->txq_count - SFXGE_TXQ_NTYPES + 1;
1600e2b05fe2SAndrew Rybchenko 	     index++) {
1601e2b05fe2SAndrew Rybchenko 		if ((rc = sfxge_tx_qinit(sc, SFXGE_TXQ_NTYPES - 1 + index,
1602e948693eSPhilip Paeps 		    SFXGE_TXQ_IP_TCP_UDP_CKSUM, index)) != 0)
1603e948693eSPhilip Paeps 			goto fail3;
1604e948693eSPhilip Paeps 	}
1605e948693eSPhilip Paeps 
1606e948693eSPhilip Paeps 	sfxge_tx_stat_init(sc);
1607e948693eSPhilip Paeps 
1608e948693eSPhilip Paeps 	return (0);
1609e948693eSPhilip Paeps 
1610e948693eSPhilip Paeps fail3:
1611e948693eSPhilip Paeps 	while (--index >= 0)
1612e948693eSPhilip Paeps 		sfxge_tx_qfini(sc, SFXGE_TXQ_IP_TCP_UDP_CKSUM + index);
1613e948693eSPhilip Paeps 
1614e2b05fe2SAndrew Rybchenko 	sfxge_tx_qfini(sc, SFXGE_TXQ_IP_CKSUM);
1615e2b05fe2SAndrew Rybchenko 
1616e948693eSPhilip Paeps fail2:
1617e948693eSPhilip Paeps 	sfxge_tx_qfini(sc, SFXGE_TXQ_NON_CKSUM);
1618e948693eSPhilip Paeps 
1619e948693eSPhilip Paeps fail:
1620bc85c897SGeorge V. Neville-Neil fail_txq_node:
1621e2b05fe2SAndrew Rybchenko 	sc->txq_count = 0;
1622e948693eSPhilip Paeps 	return (rc);
1623e948693eSPhilip Paeps }
1624