xref: /freebsd/sys/dev/sfxge/sfxge_tx.c (revision 325151a3)
1 /*-
2  * Copyright (c) 2010-2015 Solarflare Communications Inc.
3  * All rights reserved.
4  *
5  * This software was developed in part by Philip Paeps under contract for
6  * Solarflare Communications, Inc.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright notice,
12  *    this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright notice,
14  *    this list of conditions and the following disclaimer in the documentation
15  *    and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  * The views and conclusions contained in the software and documentation are
30  * those of the authors and should not be interpreted as representing official
31  * policies, either expressed or implied, of the FreeBSD Project.
32  */
33 
34 /* Theory of operation:
35  *
36  * Tx queues allocation and mapping
37  *
38  * One Tx queue with enabled checksum offload is allocated per Rx channel
39  * (event queue).  Also 2 Tx queues (one without checksum offload and one
40  * with IP checksum offload only) are allocated and bound to event queue 0.
41  * sfxge_txq_type is used as Tx queue label.
42  *
43  * So, event queue plus label mapping to Tx queue index is:
44  *	if event queue index is 0, TxQ-index = TxQ-label * [0..SFXGE_TXQ_NTYPES)
45  *	else TxQ-index = SFXGE_TXQ_NTYPES + EvQ-index - 1
46  * See sfxge_get_txq_by_label() sfxge_ev.c
47  */
48 
49 #include <sys/cdefs.h>
50 __FBSDID("$FreeBSD$");
51 
52 #include <sys/types.h>
53 #include <sys/mbuf.h>
54 #include <sys/smp.h>
55 #include <sys/socket.h>
56 #include <sys/sysctl.h>
57 #include <sys/syslog.h>
58 
59 #include <net/bpf.h>
60 #include <net/ethernet.h>
61 #include <net/if.h>
62 #include <net/if_vlan_var.h>
63 
64 #include <netinet/in.h>
65 #include <netinet/ip.h>
66 #include <netinet/ip6.h>
67 #include <netinet/tcp.h>
68 
69 #include "common/efx.h"
70 
71 #include "sfxge.h"
72 #include "sfxge_tx.h"
73 
74 
75 #define	SFXGE_PARAM_TX_DPL_GET_MAX	SFXGE_PARAM(tx_dpl_get_max)
76 static int sfxge_tx_dpl_get_max = SFXGE_TX_DPL_GET_PKT_LIMIT_DEFAULT;
77 TUNABLE_INT(SFXGE_PARAM_TX_DPL_GET_MAX, &sfxge_tx_dpl_get_max);
78 SYSCTL_INT(_hw_sfxge, OID_AUTO, tx_dpl_get_max, CTLFLAG_RDTUN,
79 	   &sfxge_tx_dpl_get_max, 0,
80 	   "Maximum number of any packets in deferred packet get-list");
81 
82 #define	SFXGE_PARAM_TX_DPL_GET_NON_TCP_MAX \
83 	SFXGE_PARAM(tx_dpl_get_non_tcp_max)
84 static int sfxge_tx_dpl_get_non_tcp_max =
85 	SFXGE_TX_DPL_GET_NON_TCP_PKT_LIMIT_DEFAULT;
86 TUNABLE_INT(SFXGE_PARAM_TX_DPL_GET_NON_TCP_MAX, &sfxge_tx_dpl_get_non_tcp_max);
87 SYSCTL_INT(_hw_sfxge, OID_AUTO, tx_dpl_get_non_tcp_max, CTLFLAG_RDTUN,
88 	   &sfxge_tx_dpl_get_non_tcp_max, 0,
89 	   "Maximum number of non-TCP packets in deferred packet get-list");
90 
91 #define	SFXGE_PARAM_TX_DPL_PUT_MAX	SFXGE_PARAM(tx_dpl_put_max)
92 static int sfxge_tx_dpl_put_max = SFXGE_TX_DPL_PUT_PKT_LIMIT_DEFAULT;
93 TUNABLE_INT(SFXGE_PARAM_TX_DPL_PUT_MAX, &sfxge_tx_dpl_put_max);
94 SYSCTL_INT(_hw_sfxge, OID_AUTO, tx_dpl_put_max, CTLFLAG_RDTUN,
95 	   &sfxge_tx_dpl_put_max, 0,
96 	   "Maximum number of any packets in deferred packet put-list");
97 
98 #define	SFXGE_PARAM_TSO_FW_ASSISTED	SFXGE_PARAM(tso_fw_assisted)
99 static int sfxge_tso_fw_assisted = 1;
100 TUNABLE_INT(SFXGE_PARAM_TSO_FW_ASSISTED, &sfxge_tso_fw_assisted);
101 SYSCTL_INT(_hw_sfxge, OID_AUTO, tso_fw_assisted, CTLFLAG_RDTUN,
102 	   &sfxge_tso_fw_assisted, 0,
103 	   "Use FW-assisted TSO if supported by NIC firmware");
104 
105 
106 static const struct {
107 	const char *name;
108 	size_t offset;
109 } sfxge_tx_stats[] = {
110 #define	SFXGE_TX_STAT(name, member) \
111 	{ #name, offsetof(struct sfxge_txq, member) }
112 	SFXGE_TX_STAT(tso_bursts, tso_bursts),
113 	SFXGE_TX_STAT(tso_packets, tso_packets),
114 	SFXGE_TX_STAT(tso_long_headers, tso_long_headers),
115 	SFXGE_TX_STAT(tso_pdrop_too_many, tso_pdrop_too_many),
116 	SFXGE_TX_STAT(tso_pdrop_no_rsrc, tso_pdrop_no_rsrc),
117 	SFXGE_TX_STAT(tx_collapses, collapses),
118 	SFXGE_TX_STAT(tx_drops, drops),
119 	SFXGE_TX_STAT(tx_get_overflow, get_overflow),
120 	SFXGE_TX_STAT(tx_get_non_tcp_overflow, get_non_tcp_overflow),
121 	SFXGE_TX_STAT(tx_put_overflow, put_overflow),
122 	SFXGE_TX_STAT(tx_netdown_drops, netdown_drops),
123 };
124 
125 
126 /* Forward declarations. */
127 static void sfxge_tx_qdpl_service(struct sfxge_txq *txq);
128 static void sfxge_tx_qlist_post(struct sfxge_txq *txq);
129 static void sfxge_tx_qunblock(struct sfxge_txq *txq);
130 static int sfxge_tx_queue_tso(struct sfxge_txq *txq, struct mbuf *mbuf,
131 			      const bus_dma_segment_t *dma_seg, int n_dma_seg,
132 			      int vlan_tagged);
133 
134 static int
135 sfxge_tx_maybe_insert_tag(struct sfxge_txq *txq, struct mbuf *mbuf)
136 {
137 	uint16_t this_tag = ((mbuf->m_flags & M_VLANTAG) ?
138 			     mbuf->m_pkthdr.ether_vtag :
139 			     0);
140 
141 	if (this_tag == txq->hw_vlan_tci)
142 		return (0);
143 
144 	efx_tx_qdesc_vlantci_create(txq->common,
145 				    bswap16(this_tag),
146 				    &txq->pend_desc[0]);
147 	txq->n_pend_desc = 1;
148 	txq->hw_vlan_tci = this_tag;
149 	return (1);
150 }
151 
152 static inline void
153 sfxge_next_stmp(struct sfxge_txq *txq, struct sfxge_tx_mapping **pstmp)
154 {
155 	KASSERT((*pstmp)->flags == 0, ("stmp flags are not 0"));
156 	if (__predict_false(*pstmp ==
157 			    &txq->stmp[txq->ptr_mask]))
158 		*pstmp = &txq->stmp[0];
159 	else
160 		(*pstmp)++;
161 }
162 
163 
164 void
165 sfxge_tx_qcomplete(struct sfxge_txq *txq, struct sfxge_evq *evq)
166 {
167 	unsigned int completed;
168 
169 	SFXGE_EVQ_LOCK_ASSERT_OWNED(evq);
170 
171 	completed = txq->completed;
172 	while (completed != txq->pending) {
173 		struct sfxge_tx_mapping *stmp;
174 		unsigned int id;
175 
176 		id = completed++ & txq->ptr_mask;
177 
178 		stmp = &txq->stmp[id];
179 		if (stmp->flags & TX_BUF_UNMAP) {
180 			bus_dmamap_unload(txq->packet_dma_tag, stmp->map);
181 			if (stmp->flags & TX_BUF_MBUF) {
182 				struct mbuf *m = stmp->u.mbuf;
183 				do
184 					m = m_free(m);
185 				while (m != NULL);
186 			} else {
187 				free(stmp->u.heap_buf, M_SFXGE);
188 			}
189 			stmp->flags = 0;
190 		}
191 	}
192 	txq->completed = completed;
193 
194 	/* Check whether we need to unblock the queue. */
195 	mb();
196 	if (txq->blocked) {
197 		unsigned int level;
198 
199 		level = txq->added - txq->completed;
200 		if (level <= SFXGE_TXQ_UNBLOCK_LEVEL(txq->entries))
201 			sfxge_tx_qunblock(txq);
202 	}
203 }
204 
205 static unsigned int
206 sfxge_is_mbuf_non_tcp(struct mbuf *mbuf)
207 {
208 	/* Absense of TCP checksum flags does not mean that it is non-TCP
209 	 * but it should be true if user wants to achieve high throughput.
210 	 */
211 	return (!(mbuf->m_pkthdr.csum_flags & (CSUM_IP_TCP | CSUM_IP6_TCP)));
212 }
213 
214 /*
215  * Reorder the put list and append it to the get list.
216  */
217 static void
218 sfxge_tx_qdpl_swizzle(struct sfxge_txq *txq)
219 {
220 	struct sfxge_tx_dpl *stdp;
221 	struct mbuf *mbuf, *get_next, **get_tailp;
222 	volatile uintptr_t *putp;
223 	uintptr_t put;
224 	unsigned int count;
225 	unsigned int non_tcp_count;
226 
227 	SFXGE_TXQ_LOCK_ASSERT_OWNED(txq);
228 
229 	stdp = &txq->dpl;
230 
231 	/* Acquire the put list. */
232 	putp = &stdp->std_put;
233 	put = atomic_readandclear_ptr(putp);
234 	mbuf = (void *)put;
235 
236 	if (mbuf == NULL)
237 		return;
238 
239 	/* Reverse the put list. */
240 	get_tailp = &mbuf->m_nextpkt;
241 	get_next = NULL;
242 
243 	count = 0;
244 	non_tcp_count = 0;
245 	do {
246 		struct mbuf *put_next;
247 
248 		non_tcp_count += sfxge_is_mbuf_non_tcp(mbuf);
249 		put_next = mbuf->m_nextpkt;
250 		mbuf->m_nextpkt = get_next;
251 		get_next = mbuf;
252 		mbuf = put_next;
253 
254 		count++;
255 	} while (mbuf != NULL);
256 
257 	if (count > stdp->std_put_hiwat)
258 		stdp->std_put_hiwat = count;
259 
260 	/* Append the reversed put list to the get list. */
261 	KASSERT(*get_tailp == NULL, ("*get_tailp != NULL"));
262 	*stdp->std_getp = get_next;
263 	stdp->std_getp = get_tailp;
264 	stdp->std_get_count += count;
265 	stdp->std_get_non_tcp_count += non_tcp_count;
266 }
267 
268 static void
269 sfxge_tx_qreap(struct sfxge_txq *txq)
270 {
271 	SFXGE_TXQ_LOCK_ASSERT_OWNED(txq);
272 
273 	txq->reaped = txq->completed;
274 }
275 
276 static void
277 sfxge_tx_qlist_post(struct sfxge_txq *txq)
278 {
279 	unsigned int old_added;
280 	unsigned int block_level;
281 	unsigned int level;
282 	int rc;
283 
284 	SFXGE_TXQ_LOCK_ASSERT_OWNED(txq);
285 
286 	KASSERT(txq->n_pend_desc != 0, ("txq->n_pend_desc == 0"));
287 	KASSERT(txq->n_pend_desc <= txq->max_pkt_desc,
288 		("txq->n_pend_desc too large"));
289 	KASSERT(!txq->blocked, ("txq->blocked"));
290 
291 	old_added = txq->added;
292 
293 	/* Post the fragment list. */
294 	rc = efx_tx_qdesc_post(txq->common, txq->pend_desc, txq->n_pend_desc,
295 			  txq->reaped, &txq->added);
296 	KASSERT(rc == 0, ("efx_tx_qdesc_post() failed"));
297 
298 	/* If efx_tx_qdesc_post() had to refragment, our information about
299 	 * buffers to free may be associated with the wrong
300 	 * descriptors.
301 	 */
302 	KASSERT(txq->added - old_added == txq->n_pend_desc,
303 		("efx_tx_qdesc_post() refragmented descriptors"));
304 
305 	level = txq->added - txq->reaped;
306 	KASSERT(level <= txq->entries, ("overfilled TX queue"));
307 
308 	/* Clear the fragment list. */
309 	txq->n_pend_desc = 0;
310 
311 	/*
312 	 * Set the block level to ensure there is space to generate a
313 	 * large number of descriptors for TSO.
314 	 */
315 	block_level = EFX_TXQ_LIMIT(txq->entries) - txq->max_pkt_desc;
316 
317 	/* Have we reached the block level? */
318 	if (level < block_level)
319 		return;
320 
321 	/* Reap, and check again */
322 	sfxge_tx_qreap(txq);
323 	level = txq->added - txq->reaped;
324 	if (level < block_level)
325 		return;
326 
327 	txq->blocked = 1;
328 
329 	/*
330 	 * Avoid a race with completion interrupt handling that could leave
331 	 * the queue blocked.
332 	 */
333 	mb();
334 	sfxge_tx_qreap(txq);
335 	level = txq->added - txq->reaped;
336 	if (level < block_level) {
337 		mb();
338 		txq->blocked = 0;
339 	}
340 }
341 
342 static int sfxge_tx_queue_mbuf(struct sfxge_txq *txq, struct mbuf *mbuf)
343 {
344 	bus_dmamap_t *used_map;
345 	bus_dmamap_t map;
346 	bus_dma_segment_t dma_seg[SFXGE_TX_MAPPING_MAX_SEG];
347 	unsigned int id;
348 	struct sfxge_tx_mapping *stmp;
349 	efx_desc_t *desc;
350 	int n_dma_seg;
351 	int rc;
352 	int i;
353 	int eop;
354 	int vlan_tagged;
355 
356 	KASSERT(!txq->blocked, ("txq->blocked"));
357 
358 	if (mbuf->m_pkthdr.csum_flags & CSUM_TSO)
359 		prefetch_read_many(mbuf->m_data);
360 
361 	if (__predict_false(txq->init_state != SFXGE_TXQ_STARTED)) {
362 		rc = EINTR;
363 		goto reject;
364 	}
365 
366 	/* Load the packet for DMA. */
367 	id = txq->added & txq->ptr_mask;
368 	stmp = &txq->stmp[id];
369 	rc = bus_dmamap_load_mbuf_sg(txq->packet_dma_tag, stmp->map,
370 				     mbuf, dma_seg, &n_dma_seg, 0);
371 	if (rc == EFBIG) {
372 		/* Try again. */
373 		struct mbuf *new_mbuf = m_collapse(mbuf, M_NOWAIT,
374 						   SFXGE_TX_MAPPING_MAX_SEG);
375 		if (new_mbuf == NULL)
376 			goto reject;
377 		++txq->collapses;
378 		mbuf = new_mbuf;
379 		rc = bus_dmamap_load_mbuf_sg(txq->packet_dma_tag,
380 					     stmp->map, mbuf,
381 					     dma_seg, &n_dma_seg, 0);
382 	}
383 	if (rc != 0)
384 		goto reject;
385 
386 	/* Make the packet visible to the hardware. */
387 	bus_dmamap_sync(txq->packet_dma_tag, stmp->map, BUS_DMASYNC_PREWRITE);
388 
389 	used_map = &stmp->map;
390 
391 	vlan_tagged = sfxge_tx_maybe_insert_tag(txq, mbuf);
392 	if (vlan_tagged) {
393 		sfxge_next_stmp(txq, &stmp);
394 	}
395 	if (mbuf->m_pkthdr.csum_flags & CSUM_TSO) {
396 		rc = sfxge_tx_queue_tso(txq, mbuf, dma_seg, n_dma_seg, vlan_tagged);
397 		if (rc < 0)
398 			goto reject_mapped;
399 		stmp = &txq->stmp[(rc - 1) & txq->ptr_mask];
400 	} else {
401 		/* Add the mapping to the fragment list, and set flags
402 		 * for the buffer.
403 		 */
404 
405 		i = 0;
406 		for (;;) {
407 			desc = &txq->pend_desc[i + vlan_tagged];
408 			eop = (i == n_dma_seg - 1);
409 			efx_tx_qdesc_dma_create(txq->common,
410 						dma_seg[i].ds_addr,
411 						dma_seg[i].ds_len,
412 						eop,
413 						desc);
414 			if (eop)
415 				break;
416 			i++;
417 			sfxge_next_stmp(txq, &stmp);
418 		}
419 		txq->n_pend_desc = n_dma_seg + vlan_tagged;
420 	}
421 
422 	/*
423 	 * If the mapping required more than one descriptor
424 	 * then we need to associate the DMA map with the last
425 	 * descriptor, not the first.
426 	 */
427 	if (used_map != &stmp->map) {
428 		map = stmp->map;
429 		stmp->map = *used_map;
430 		*used_map = map;
431 	}
432 
433 	stmp->u.mbuf = mbuf;
434 	stmp->flags = TX_BUF_UNMAP | TX_BUF_MBUF;
435 
436 	/* Post the fragment list. */
437 	sfxge_tx_qlist_post(txq);
438 
439 	return (0);
440 
441 reject_mapped:
442 	bus_dmamap_unload(txq->packet_dma_tag, *used_map);
443 reject:
444 	/* Drop the packet on the floor. */
445 	m_freem(mbuf);
446 	++txq->drops;
447 
448 	return (rc);
449 }
450 
451 /*
452  * Drain the deferred packet list into the transmit queue.
453  */
454 static void
455 sfxge_tx_qdpl_drain(struct sfxge_txq *txq)
456 {
457 	struct sfxge_softc *sc;
458 	struct sfxge_tx_dpl *stdp;
459 	struct mbuf *mbuf, *next;
460 	unsigned int count;
461 	unsigned int non_tcp_count;
462 	unsigned int pushed;
463 	int rc;
464 
465 	SFXGE_TXQ_LOCK_ASSERT_OWNED(txq);
466 
467 	sc = txq->sc;
468 	stdp = &txq->dpl;
469 	pushed = txq->added;
470 
471 	if (__predict_true(txq->init_state == SFXGE_TXQ_STARTED)) {
472 		prefetch_read_many(sc->enp);
473 		prefetch_read_many(txq->common);
474 	}
475 
476 	mbuf = stdp->std_get;
477 	count = stdp->std_get_count;
478 	non_tcp_count = stdp->std_get_non_tcp_count;
479 
480 	if (count > stdp->std_get_hiwat)
481 		stdp->std_get_hiwat = count;
482 
483 	while (count != 0) {
484 		KASSERT(mbuf != NULL, ("mbuf == NULL"));
485 
486 		next = mbuf->m_nextpkt;
487 		mbuf->m_nextpkt = NULL;
488 
489 		ETHER_BPF_MTAP(sc->ifnet, mbuf); /* packet capture */
490 
491 		if (next != NULL)
492 			prefetch_read_many(next);
493 
494 		rc = sfxge_tx_queue_mbuf(txq, mbuf);
495 		--count;
496 		non_tcp_count -= sfxge_is_mbuf_non_tcp(mbuf);
497 		mbuf = next;
498 		if (rc != 0)
499 			continue;
500 
501 		if (txq->blocked)
502 			break;
503 
504 		/* Push the fragments to the hardware in batches. */
505 		if (txq->added - pushed >= SFXGE_TX_BATCH) {
506 			efx_tx_qpush(txq->common, txq->added, pushed);
507 			pushed = txq->added;
508 		}
509 	}
510 
511 	if (count == 0) {
512 		KASSERT(mbuf == NULL, ("mbuf != NULL"));
513 		KASSERT(non_tcp_count == 0,
514 			("inconsistent TCP/non-TCP detection"));
515 		stdp->std_get = NULL;
516 		stdp->std_get_count = 0;
517 		stdp->std_get_non_tcp_count = 0;
518 		stdp->std_getp = &stdp->std_get;
519 	} else {
520 		stdp->std_get = mbuf;
521 		stdp->std_get_count = count;
522 		stdp->std_get_non_tcp_count = non_tcp_count;
523 	}
524 
525 	if (txq->added != pushed)
526 		efx_tx_qpush(txq->common, txq->added, pushed);
527 
528 	KASSERT(txq->blocked || stdp->std_get_count == 0,
529 		("queue unblocked but count is non-zero"));
530 }
531 
532 #define	SFXGE_TX_QDPL_PENDING(_txq)	((_txq)->dpl.std_put != 0)
533 
534 /*
535  * Service the deferred packet list.
536  *
537  * NOTE: drops the txq mutex!
538  */
539 static void
540 sfxge_tx_qdpl_service(struct sfxge_txq *txq)
541 {
542 	SFXGE_TXQ_LOCK_ASSERT_OWNED(txq);
543 
544 	do {
545 		if (SFXGE_TX_QDPL_PENDING(txq))
546 			sfxge_tx_qdpl_swizzle(txq);
547 
548 		if (!txq->blocked)
549 			sfxge_tx_qdpl_drain(txq);
550 
551 		SFXGE_TXQ_UNLOCK(txq);
552 	} while (SFXGE_TX_QDPL_PENDING(txq) &&
553 		 SFXGE_TXQ_TRYLOCK(txq));
554 }
555 
556 /*
557  * Put a packet on the deferred packet get-list.
558  */
559 static int
560 sfxge_tx_qdpl_put_locked(struct sfxge_txq *txq, struct mbuf *mbuf)
561 {
562 	struct sfxge_tx_dpl *stdp;
563 
564 	stdp = &txq->dpl;
565 
566 	KASSERT(mbuf->m_nextpkt == NULL, ("mbuf->m_nextpkt != NULL"));
567 
568 	SFXGE_TXQ_LOCK_ASSERT_OWNED(txq);
569 
570 	if (stdp->std_get_count >= stdp->std_get_max) {
571 		txq->get_overflow++;
572 		return (ENOBUFS);
573 	}
574 	if (sfxge_is_mbuf_non_tcp(mbuf)) {
575 		if (stdp->std_get_non_tcp_count >=
576 		    stdp->std_get_non_tcp_max) {
577 			txq->get_non_tcp_overflow++;
578 			return (ENOBUFS);
579 		}
580 		stdp->std_get_non_tcp_count++;
581 	}
582 
583 	*(stdp->std_getp) = mbuf;
584 	stdp->std_getp = &mbuf->m_nextpkt;
585 	stdp->std_get_count++;
586 
587 	return (0);
588 }
589 
590 /*
591  * Put a packet on the deferred packet put-list.
592  *
593  * We overload the csum_data field in the mbuf to keep track of this length
594  * because there is no cheap alternative to avoid races.
595  */
596 static int
597 sfxge_tx_qdpl_put_unlocked(struct sfxge_txq *txq, struct mbuf *mbuf)
598 {
599 	struct sfxge_tx_dpl *stdp;
600 	volatile uintptr_t *putp;
601 	uintptr_t old;
602 	uintptr_t new;
603 	unsigned old_len;
604 
605 	KASSERT(mbuf->m_nextpkt == NULL, ("mbuf->m_nextpkt != NULL"));
606 
607 	SFXGE_TXQ_LOCK_ASSERT_NOTOWNED(txq);
608 
609 	stdp = &txq->dpl;
610 	putp = &stdp->std_put;
611 	new = (uintptr_t)mbuf;
612 
613 	do {
614 		old = *putp;
615 		if (old != 0) {
616 			struct mbuf *mp = (struct mbuf *)old;
617 			old_len = mp->m_pkthdr.csum_data;
618 		} else
619 			old_len = 0;
620 		if (old_len >= stdp->std_put_max) {
621 			atomic_add_long(&txq->put_overflow, 1);
622 			return (ENOBUFS);
623 		}
624 		mbuf->m_pkthdr.csum_data = old_len + 1;
625 		mbuf->m_nextpkt = (void *)old;
626 	} while (atomic_cmpset_ptr(putp, old, new) == 0);
627 
628 	return (0);
629 }
630 
631 /*
632  * Called from if_transmit - will try to grab the txq lock and enqueue to the
633  * put list if it succeeds, otherwise try to push onto the defer list if space.
634  */
635 static int
636 sfxge_tx_packet_add(struct sfxge_txq *txq, struct mbuf *m)
637 {
638 	int rc;
639 
640 	if (!SFXGE_LINK_UP(txq->sc)) {
641 		atomic_add_long(&txq->netdown_drops, 1);
642 		return (ENETDOWN);
643 	}
644 
645 	/*
646 	 * Try to grab the txq lock.  If we are able to get the lock,
647 	 * the packet will be appended to the "get list" of the deferred
648 	 * packet list.  Otherwise, it will be pushed on the "put list".
649 	 */
650 	if (SFXGE_TXQ_TRYLOCK(txq)) {
651 		/* First swizzle put-list to get-list to keep order */
652 		sfxge_tx_qdpl_swizzle(txq);
653 
654 		rc = sfxge_tx_qdpl_put_locked(txq, m);
655 
656 		/* Try to service the list. */
657 		sfxge_tx_qdpl_service(txq);
658 		/* Lock has been dropped. */
659 	} else {
660 		rc = sfxge_tx_qdpl_put_unlocked(txq, m);
661 
662 		/*
663 		 * Try to grab the lock again.
664 		 *
665 		 * If we are able to get the lock, we need to process
666 		 * the deferred packet list.  If we are not able to get
667 		 * the lock, another thread is processing the list.
668 		 */
669 		if ((rc == 0) && SFXGE_TXQ_TRYLOCK(txq)) {
670 			sfxge_tx_qdpl_service(txq);
671 			/* Lock has been dropped. */
672 		}
673 	}
674 
675 	SFXGE_TXQ_LOCK_ASSERT_NOTOWNED(txq);
676 
677 	return (rc);
678 }
679 
680 static void
681 sfxge_tx_qdpl_flush(struct sfxge_txq *txq)
682 {
683 	struct sfxge_tx_dpl *stdp = &txq->dpl;
684 	struct mbuf *mbuf, *next;
685 
686 	SFXGE_TXQ_LOCK(txq);
687 
688 	sfxge_tx_qdpl_swizzle(txq);
689 	for (mbuf = stdp->std_get; mbuf != NULL; mbuf = next) {
690 		next = mbuf->m_nextpkt;
691 		m_freem(mbuf);
692 	}
693 	stdp->std_get = NULL;
694 	stdp->std_get_count = 0;
695 	stdp->std_get_non_tcp_count = 0;
696 	stdp->std_getp = &stdp->std_get;
697 
698 	SFXGE_TXQ_UNLOCK(txq);
699 }
700 
701 void
702 sfxge_if_qflush(struct ifnet *ifp)
703 {
704 	struct sfxge_softc *sc;
705 	unsigned int i;
706 
707 	sc = ifp->if_softc;
708 
709 	for (i = 0; i < sc->txq_count; i++)
710 		sfxge_tx_qdpl_flush(sc->txq[i]);
711 }
712 
713 /*
714  * TX start -- called by the stack.
715  */
716 int
717 sfxge_if_transmit(struct ifnet *ifp, struct mbuf *m)
718 {
719 	struct sfxge_softc *sc;
720 	struct sfxge_txq *txq;
721 	int rc;
722 
723 	sc = (struct sfxge_softc *)ifp->if_softc;
724 
725 	/*
726 	 * Transmit may be called when interface is up from the kernel
727 	 * point of view, but not yet up (in progress) from the driver
728 	 * point of view. I.e. link aggregation bring up.
729 	 * Transmit may be called when interface is up from the driver
730 	 * point of view, but already down from the kernel point of
731 	 * view. I.e. Rx when interface shutdown is in progress.
732 	 */
733 	KASSERT((ifp->if_flags & IFF_UP) || (sc->if_flags & IFF_UP),
734 		("interface not up"));
735 
736 	/* Pick the desired transmit queue. */
737 	if (m->m_pkthdr.csum_flags &
738 	    (CSUM_DELAY_DATA | CSUM_TCP_IPV6 | CSUM_UDP_IPV6 | CSUM_TSO)) {
739 		int index = 0;
740 
741 		/* check if flowid is set */
742 		if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) {
743 			uint32_t hash = m->m_pkthdr.flowid;
744 
745 			index = sc->rx_indir_table[hash % SFXGE_RX_SCALE_MAX];
746 		}
747 		txq = sc->txq[SFXGE_TXQ_IP_TCP_UDP_CKSUM + index];
748 	} else if (m->m_pkthdr.csum_flags & CSUM_DELAY_IP) {
749 		txq = sc->txq[SFXGE_TXQ_IP_CKSUM];
750 	} else {
751 		txq = sc->txq[SFXGE_TXQ_NON_CKSUM];
752 	}
753 
754 	rc = sfxge_tx_packet_add(txq, m);
755 	if (rc != 0)
756 		m_freem(m);
757 
758 	return (rc);
759 }
760 
761 /*
762  * Software "TSO".  Not quite as good as doing it in hardware, but
763  * still faster than segmenting in the stack.
764  */
765 
766 struct sfxge_tso_state {
767 	/* Output position */
768 	unsigned out_len;	/* Remaining length in current segment */
769 	unsigned seqnum;	/* Current sequence number */
770 	unsigned packet_space;	/* Remaining space in current packet */
771 
772 	/* Input position */
773 	uint64_t dma_addr;	/* DMA address of current position */
774 	unsigned in_len;	/* Remaining length in current mbuf */
775 
776 	const struct mbuf *mbuf; /* Input mbuf (head of chain) */
777 	u_short protocol;	/* Network protocol (after VLAN decap) */
778 	ssize_t nh_off;		/* Offset of network header */
779 	ssize_t tcph_off;	/* Offset of TCP header */
780 	unsigned header_len;	/* Number of bytes of header */
781 	unsigned seg_size;	/* TCP segment size */
782 	int fw_assisted;	/* Use FW-assisted TSO */
783 	u_short packet_id;	/* IPv4 packet ID from the original packet */
784 	efx_desc_t header_desc; /* Precomputed header descriptor for
785 				 * FW-assisted TSO */
786 };
787 
788 static const struct ip *tso_iph(const struct sfxge_tso_state *tso)
789 {
790 	KASSERT(tso->protocol == htons(ETHERTYPE_IP),
791 		("tso_iph() in non-IPv4 state"));
792 	return (const struct ip *)(tso->mbuf->m_data + tso->nh_off);
793 }
794 static __unused const struct ip6_hdr *tso_ip6h(const struct sfxge_tso_state *tso)
795 {
796 	KASSERT(tso->protocol == htons(ETHERTYPE_IPV6),
797 		("tso_ip6h() in non-IPv6 state"));
798 	return (const struct ip6_hdr *)(tso->mbuf->m_data + tso->nh_off);
799 }
800 static const struct tcphdr *tso_tcph(const struct sfxge_tso_state *tso)
801 {
802 	return (const struct tcphdr *)(tso->mbuf->m_data + tso->tcph_off);
803 }
804 
805 /* Size of preallocated TSO header buffers.  Larger blocks must be
806  * allocated from the heap.
807  */
808 #define	TSOH_STD_SIZE	128
809 
810 /* At most half the descriptors in the queue at any time will refer to
811  * a TSO header buffer, since they must always be followed by a
812  * payload descriptor referring to an mbuf.
813  */
814 #define	TSOH_COUNT(_txq_entries)	((_txq_entries) / 2u)
815 #define	TSOH_PER_PAGE	(PAGE_SIZE / TSOH_STD_SIZE)
816 #define	TSOH_PAGE_COUNT(_txq_entries)	\
817 	((TSOH_COUNT(_txq_entries) + TSOH_PER_PAGE - 1) / TSOH_PER_PAGE)
818 
819 static int tso_init(struct sfxge_txq *txq)
820 {
821 	struct sfxge_softc *sc = txq->sc;
822 	unsigned int tsoh_page_count = TSOH_PAGE_COUNT(sc->txq_entries);
823 	int i, rc;
824 
825 	/* Allocate TSO header buffers */
826 	txq->tsoh_buffer = malloc(tsoh_page_count * sizeof(txq->tsoh_buffer[0]),
827 				  M_SFXGE, M_WAITOK);
828 
829 	for (i = 0; i < tsoh_page_count; i++) {
830 		rc = sfxge_dma_alloc(sc, PAGE_SIZE, &txq->tsoh_buffer[i]);
831 		if (rc != 0)
832 			goto fail;
833 	}
834 
835 	return (0);
836 
837 fail:
838 	while (i-- > 0)
839 		sfxge_dma_free(&txq->tsoh_buffer[i]);
840 	free(txq->tsoh_buffer, M_SFXGE);
841 	txq->tsoh_buffer = NULL;
842 	return (rc);
843 }
844 
845 static void tso_fini(struct sfxge_txq *txq)
846 {
847 	int i;
848 
849 	if (txq->tsoh_buffer != NULL) {
850 		for (i = 0; i < TSOH_PAGE_COUNT(txq->sc->txq_entries); i++)
851 			sfxge_dma_free(&txq->tsoh_buffer[i]);
852 		free(txq->tsoh_buffer, M_SFXGE);
853 	}
854 }
855 
856 static void tso_start(struct sfxge_txq *txq, struct sfxge_tso_state *tso,
857 		      const bus_dma_segment_t *hdr_dma_seg,
858 		      struct mbuf *mbuf)
859 {
860 	struct ether_header *eh = mtod(mbuf, struct ether_header *);
861 	const efx_nic_cfg_t *encp = efx_nic_cfg_get(txq->sc->enp);
862 	const struct tcphdr *th;
863 	struct tcphdr th_copy;
864 
865 	tso->fw_assisted = txq->sc->tso_fw_assisted;
866 	tso->mbuf = mbuf;
867 
868 	/* Find network protocol and header */
869 	tso->protocol = eh->ether_type;
870 	if (tso->protocol == htons(ETHERTYPE_VLAN)) {
871 		struct ether_vlan_header *veh =
872 			mtod(mbuf, struct ether_vlan_header *);
873 		tso->protocol = veh->evl_proto;
874 		tso->nh_off = sizeof(*veh);
875 	} else {
876 		tso->nh_off = sizeof(*eh);
877 	}
878 
879 	/* Find TCP header */
880 	if (tso->protocol == htons(ETHERTYPE_IP)) {
881 		KASSERT(tso_iph(tso)->ip_p == IPPROTO_TCP,
882 			("TSO required on non-TCP packet"));
883 		tso->tcph_off = tso->nh_off + 4 * tso_iph(tso)->ip_hl;
884 		tso->packet_id = tso_iph(tso)->ip_id;
885 	} else {
886 		KASSERT(tso->protocol == htons(ETHERTYPE_IPV6),
887 			("TSO required on non-IP packet"));
888 		KASSERT(tso_ip6h(tso)->ip6_nxt == IPPROTO_TCP,
889 			("TSO required on non-TCP packet"));
890 		tso->tcph_off = tso->nh_off + sizeof(struct ip6_hdr);
891 		tso->packet_id = 0;
892 	}
893 	if (tso->fw_assisted &&
894 	    __predict_false(tso->tcph_off >
895 			    encp->enc_tx_tso_tcp_header_offset_limit)) {
896 		tso->fw_assisted = 0;
897 	}
898 
899 	KASSERT(mbuf->m_len >= tso->tcph_off,
900 		("network header is fragmented in mbuf"));
901 	/* We need TCP header including flags (window is the next) */
902 	if (mbuf->m_len < tso->tcph_off + offsetof(struct tcphdr, th_win)) {
903 		m_copydata(tso->mbuf, tso->tcph_off, sizeof(th_copy),
904 			   (caddr_t)&th_copy);
905 		th = &th_copy;
906 	} else {
907 		th = tso_tcph(tso);
908 	}
909 
910 	tso->header_len = tso->tcph_off + 4 * th->th_off;
911 	tso->seg_size = mbuf->m_pkthdr.tso_segsz;
912 
913 	tso->seqnum = ntohl(th->th_seq);
914 
915 	/* These flags must not be duplicated */
916 	/*
917 	 * RST should not be duplicated as well, but FreeBSD kernel
918 	 * generates TSO packets with RST flag. So, do not assert
919 	 * its absence.
920 	 */
921 	KASSERT(!(th->th_flags & (TH_URG | TH_SYN)),
922 		("incompatible TCP flag 0x%x on TSO packet",
923 		 th->th_flags & (TH_URG | TH_SYN)));
924 
925 	tso->out_len = mbuf->m_pkthdr.len - tso->header_len;
926 
927 	if (tso->fw_assisted) {
928 		if (hdr_dma_seg->ds_len >= tso->header_len)
929 			efx_tx_qdesc_dma_create(txq->common,
930 						hdr_dma_seg->ds_addr,
931 						tso->header_len,
932 						B_FALSE,
933 						&tso->header_desc);
934 		else
935 			tso->fw_assisted = 0;
936 	}
937 }
938 
939 /*
940  * tso_fill_packet_with_fragment - form descriptors for the current fragment
941  *
942  * Form descriptors for the current fragment, until we reach the end
943  * of fragment or end-of-packet.  Return 0 on success, 1 if not enough
944  * space.
945  */
946 static void tso_fill_packet_with_fragment(struct sfxge_txq *txq,
947 					  struct sfxge_tso_state *tso)
948 {
949 	efx_desc_t *desc;
950 	int n;
951 
952 	if (tso->in_len == 0 || tso->packet_space == 0)
953 		return;
954 
955 	KASSERT(tso->in_len > 0, ("TSO input length went negative"));
956 	KASSERT(tso->packet_space > 0, ("TSO packet space went negative"));
957 
958 	n = min(tso->in_len, tso->packet_space);
959 
960 	tso->packet_space -= n;
961 	tso->out_len -= n;
962 	tso->in_len -= n;
963 
964 	desc = &txq->pend_desc[txq->n_pend_desc++];
965 	efx_tx_qdesc_dma_create(txq->common,
966 				tso->dma_addr,
967 				n,
968 				tso->out_len == 0 || tso->packet_space == 0,
969 				desc);
970 
971 	tso->dma_addr += n;
972 }
973 
974 /* Callback from bus_dmamap_load() for long TSO headers. */
975 static void tso_map_long_header(void *dma_addr_ret,
976 				bus_dma_segment_t *segs, int nseg,
977 				int error)
978 {
979 	*(uint64_t *)dma_addr_ret = ((__predict_true(error == 0) &&
980 				      __predict_true(nseg == 1)) ?
981 				     segs->ds_addr : 0);
982 }
983 
984 /*
985  * tso_start_new_packet - generate a new header and prepare for the new packet
986  *
987  * Generate a new header and prepare for the new packet.  Return 0 on
988  * success, or an error code if failed to alloc header.
989  */
990 static int tso_start_new_packet(struct sfxge_txq *txq,
991 				struct sfxge_tso_state *tso,
992 				unsigned int *idp)
993 {
994 	unsigned int id = *idp;
995 	struct tcphdr *tsoh_th;
996 	unsigned ip_length;
997 	caddr_t header;
998 	uint64_t dma_addr;
999 	bus_dmamap_t map;
1000 	efx_desc_t *desc;
1001 	int rc;
1002 
1003 	if (tso->fw_assisted) {
1004 		uint8_t tcp_flags = tso_tcph(tso)->th_flags;
1005 
1006 		if (tso->out_len > tso->seg_size)
1007 			tcp_flags &= ~(TH_FIN | TH_PUSH);
1008 
1009 		/* TSO option descriptor */
1010 		desc = &txq->pend_desc[txq->n_pend_desc++];
1011 		efx_tx_qdesc_tso_create(txq->common,
1012 					tso->packet_id,
1013 					tso->seqnum,
1014 					tcp_flags,
1015 					desc++);
1016 		KASSERT(txq->stmp[id].flags == 0, ("stmp flags are not 0"));
1017 		id = (id + 1) & txq->ptr_mask;
1018 
1019 		/* Header DMA descriptor */
1020 		*desc = tso->header_desc;
1021 		txq->n_pend_desc++;
1022 		KASSERT(txq->stmp[id].flags == 0, ("stmp flags are not 0"));
1023 		id = (id + 1) & txq->ptr_mask;
1024 
1025 		tso->seqnum += tso->seg_size;
1026 	} else {
1027 		/* Allocate a DMA-mapped header buffer. */
1028 		if (__predict_true(tso->header_len <= TSOH_STD_SIZE)) {
1029 			unsigned int page_index = (id / 2) / TSOH_PER_PAGE;
1030 			unsigned int buf_index = (id / 2) % TSOH_PER_PAGE;
1031 
1032 			header = (txq->tsoh_buffer[page_index].esm_base +
1033 				  buf_index * TSOH_STD_SIZE);
1034 			dma_addr = (txq->tsoh_buffer[page_index].esm_addr +
1035 				    buf_index * TSOH_STD_SIZE);
1036 			map = txq->tsoh_buffer[page_index].esm_map;
1037 
1038 			KASSERT(txq->stmp[id].flags == 0,
1039 				("stmp flags are not 0"));
1040 		} else {
1041 			struct sfxge_tx_mapping *stmp = &txq->stmp[id];
1042 
1043 			/* We cannot use bus_dmamem_alloc() as that may sleep */
1044 			header = malloc(tso->header_len, M_SFXGE, M_NOWAIT);
1045 			if (__predict_false(!header))
1046 				return (ENOMEM);
1047 			rc = bus_dmamap_load(txq->packet_dma_tag, stmp->map,
1048 					     header, tso->header_len,
1049 					     tso_map_long_header, &dma_addr,
1050 					     BUS_DMA_NOWAIT);
1051 			if (__predict_false(dma_addr == 0)) {
1052 				if (rc == 0) {
1053 					/* Succeeded but got >1 segment */
1054 					bus_dmamap_unload(txq->packet_dma_tag,
1055 							  stmp->map);
1056 					rc = EINVAL;
1057 				}
1058 				free(header, M_SFXGE);
1059 				return (rc);
1060 			}
1061 			map = stmp->map;
1062 
1063 			txq->tso_long_headers++;
1064 			stmp->u.heap_buf = header;
1065 			stmp->flags = TX_BUF_UNMAP;
1066 		}
1067 
1068 		tsoh_th = (struct tcphdr *)(header + tso->tcph_off);
1069 
1070 		/* Copy and update the headers. */
1071 		m_copydata(tso->mbuf, 0, tso->header_len, header);
1072 
1073 		tsoh_th->th_seq = htonl(tso->seqnum);
1074 		tso->seqnum += tso->seg_size;
1075 		if (tso->out_len > tso->seg_size) {
1076 			/* This packet will not finish the TSO burst. */
1077 			ip_length = tso->header_len - tso->nh_off + tso->seg_size;
1078 			tsoh_th->th_flags &= ~(TH_FIN | TH_PUSH);
1079 		} else {
1080 			/* This packet will be the last in the TSO burst. */
1081 			ip_length = tso->header_len - tso->nh_off + tso->out_len;
1082 		}
1083 
1084 		if (tso->protocol == htons(ETHERTYPE_IP)) {
1085 			struct ip *tsoh_iph = (struct ip *)(header + tso->nh_off);
1086 			tsoh_iph->ip_len = htons(ip_length);
1087 			/* XXX We should increment ip_id, but FreeBSD doesn't
1088 			 * currently allocate extra IDs for multiple segments.
1089 			 */
1090 		} else {
1091 			struct ip6_hdr *tsoh_iph =
1092 				(struct ip6_hdr *)(header + tso->nh_off);
1093 			tsoh_iph->ip6_plen = htons(ip_length - sizeof(*tsoh_iph));
1094 		}
1095 
1096 		/* Make the header visible to the hardware. */
1097 		bus_dmamap_sync(txq->packet_dma_tag, map, BUS_DMASYNC_PREWRITE);
1098 
1099 		/* Form a descriptor for this header. */
1100 		desc = &txq->pend_desc[txq->n_pend_desc++];
1101 		efx_tx_qdesc_dma_create(txq->common,
1102 					dma_addr,
1103 					tso->header_len,
1104 					0,
1105 					desc);
1106 		id = (id + 1) & txq->ptr_mask;
1107 	}
1108 	tso->packet_space = tso->seg_size;
1109 	txq->tso_packets++;
1110 	*idp = id;
1111 
1112 	return (0);
1113 }
1114 
1115 static int
1116 sfxge_tx_queue_tso(struct sfxge_txq *txq, struct mbuf *mbuf,
1117 		   const bus_dma_segment_t *dma_seg, int n_dma_seg,
1118 		   int vlan_tagged)
1119 {
1120 	struct sfxge_tso_state tso;
1121 	unsigned int id;
1122 	unsigned skipped = 0;
1123 
1124 	tso_start(txq, &tso, dma_seg, mbuf);
1125 
1126 	while (dma_seg->ds_len + skipped <= tso.header_len) {
1127 		skipped += dma_seg->ds_len;
1128 		--n_dma_seg;
1129 		KASSERT(n_dma_seg, ("no payload found in TSO packet"));
1130 		++dma_seg;
1131 	}
1132 	tso.in_len = dma_seg->ds_len - (tso.header_len - skipped);
1133 	tso.dma_addr = dma_seg->ds_addr + (tso.header_len - skipped);
1134 
1135 	id = (txq->added + vlan_tagged) & txq->ptr_mask;
1136 	if (__predict_false(tso_start_new_packet(txq, &tso, &id)))
1137 		return (-1);
1138 
1139 	while (1) {
1140 		tso_fill_packet_with_fragment(txq, &tso);
1141 		/* Exactly one DMA descriptor is added */
1142 		KASSERT(txq->stmp[id].flags == 0, ("stmp flags are not 0"));
1143 		id = (id + 1) & txq->ptr_mask;
1144 
1145 		/* Move onto the next fragment? */
1146 		if (tso.in_len == 0) {
1147 			--n_dma_seg;
1148 			if (n_dma_seg == 0)
1149 				break;
1150 			++dma_seg;
1151 			tso.in_len = dma_seg->ds_len;
1152 			tso.dma_addr = dma_seg->ds_addr;
1153 		}
1154 
1155 		/* End of packet? */
1156 		if (tso.packet_space == 0) {
1157 			/* If the queue is now full due to tiny MSS,
1158 			 * or we can't create another header, discard
1159 			 * the remainder of the input mbuf but do not
1160 			 * roll back the work we have done.
1161 			 */
1162 			if (txq->n_pend_desc + tso.fw_assisted +
1163 			    1 /* header */ + n_dma_seg >
1164 			    txq->max_pkt_desc) {
1165 				txq->tso_pdrop_too_many++;
1166 				break;
1167 			}
1168 			if (__predict_false(tso_start_new_packet(txq, &tso,
1169 								 &id))) {
1170 				txq->tso_pdrop_no_rsrc++;
1171 				break;
1172 			}
1173 		}
1174 	}
1175 
1176 	txq->tso_bursts++;
1177 	return (id);
1178 }
1179 
1180 static void
1181 sfxge_tx_qunblock(struct sfxge_txq *txq)
1182 {
1183 	struct sfxge_softc *sc;
1184 	struct sfxge_evq *evq;
1185 
1186 	sc = txq->sc;
1187 	evq = sc->evq[txq->evq_index];
1188 
1189 	SFXGE_EVQ_LOCK_ASSERT_OWNED(evq);
1190 
1191 	if (__predict_false(txq->init_state != SFXGE_TXQ_STARTED))
1192 		return;
1193 
1194 	SFXGE_TXQ_LOCK(txq);
1195 
1196 	if (txq->blocked) {
1197 		unsigned int level;
1198 
1199 		level = txq->added - txq->completed;
1200 		if (level <= SFXGE_TXQ_UNBLOCK_LEVEL(txq->entries)) {
1201 			/* reaped must be in sync with blocked */
1202 			sfxge_tx_qreap(txq);
1203 			txq->blocked = 0;
1204 		}
1205 	}
1206 
1207 	sfxge_tx_qdpl_service(txq);
1208 	/* note: lock has been dropped */
1209 }
1210 
1211 void
1212 sfxge_tx_qflush_done(struct sfxge_txq *txq)
1213 {
1214 
1215 	txq->flush_state = SFXGE_FLUSH_DONE;
1216 }
1217 
1218 static void
1219 sfxge_tx_qstop(struct sfxge_softc *sc, unsigned int index)
1220 {
1221 	struct sfxge_txq *txq;
1222 	struct sfxge_evq *evq;
1223 	unsigned int count;
1224 
1225 	SFXGE_ADAPTER_LOCK_ASSERT_OWNED(sc);
1226 
1227 	txq = sc->txq[index];
1228 	evq = sc->evq[txq->evq_index];
1229 
1230 	SFXGE_EVQ_LOCK(evq);
1231 	SFXGE_TXQ_LOCK(txq);
1232 
1233 	KASSERT(txq->init_state == SFXGE_TXQ_STARTED,
1234 	    ("txq->init_state != SFXGE_TXQ_STARTED"));
1235 
1236 	txq->init_state = SFXGE_TXQ_INITIALIZED;
1237 
1238 	if (txq->flush_state != SFXGE_FLUSH_DONE) {
1239 		txq->flush_state = SFXGE_FLUSH_PENDING;
1240 
1241 		SFXGE_EVQ_UNLOCK(evq);
1242 		SFXGE_TXQ_UNLOCK(txq);
1243 
1244 		/* Flush the transmit queue. */
1245 		if (efx_tx_qflush(txq->common) != 0) {
1246 			log(LOG_ERR, "%s: Flushing Tx queue %u failed\n",
1247 			    device_get_nameunit(sc->dev), index);
1248 			txq->flush_state = SFXGE_FLUSH_DONE;
1249 		} else {
1250 			count = 0;
1251 			do {
1252 				/* Spin for 100ms. */
1253 				DELAY(100000);
1254 				if (txq->flush_state != SFXGE_FLUSH_PENDING)
1255 					break;
1256 			} while (++count < 20);
1257 		}
1258 		SFXGE_EVQ_LOCK(evq);
1259 		SFXGE_TXQ_LOCK(txq);
1260 
1261 		KASSERT(txq->flush_state != SFXGE_FLUSH_FAILED,
1262 		    ("txq->flush_state == SFXGE_FLUSH_FAILED"));
1263 
1264 		if (txq->flush_state != SFXGE_FLUSH_DONE) {
1265 			/* Flush timeout */
1266 			log(LOG_ERR, "%s: Cannot flush Tx queue %u\n",
1267 			    device_get_nameunit(sc->dev), index);
1268 			txq->flush_state = SFXGE_FLUSH_DONE;
1269 		}
1270 	}
1271 
1272 	txq->blocked = 0;
1273 	txq->pending = txq->added;
1274 
1275 	sfxge_tx_qcomplete(txq, evq);
1276 	KASSERT(txq->completed == txq->added,
1277 	    ("txq->completed != txq->added"));
1278 
1279 	sfxge_tx_qreap(txq);
1280 	KASSERT(txq->reaped == txq->completed,
1281 	    ("txq->reaped != txq->completed"));
1282 
1283 	txq->added = 0;
1284 	txq->pending = 0;
1285 	txq->completed = 0;
1286 	txq->reaped = 0;
1287 
1288 	/* Destroy the common code transmit queue. */
1289 	efx_tx_qdestroy(txq->common);
1290 	txq->common = NULL;
1291 
1292 	efx_sram_buf_tbl_clear(sc->enp, txq->buf_base_id,
1293 	    EFX_TXQ_NBUFS(sc->txq_entries));
1294 
1295 	SFXGE_EVQ_UNLOCK(evq);
1296 	SFXGE_TXQ_UNLOCK(txq);
1297 }
1298 
1299 static int
1300 sfxge_tx_qstart(struct sfxge_softc *sc, unsigned int index)
1301 {
1302 	struct sfxge_txq *txq;
1303 	efsys_mem_t *esmp;
1304 	uint16_t flags;
1305 	struct sfxge_evq *evq;
1306 	unsigned int desc_index;
1307 	int rc;
1308 
1309 	SFXGE_ADAPTER_LOCK_ASSERT_OWNED(sc);
1310 
1311 	txq = sc->txq[index];
1312 	esmp = &txq->mem;
1313 	evq = sc->evq[txq->evq_index];
1314 
1315 	KASSERT(txq->init_state == SFXGE_TXQ_INITIALIZED,
1316 	    ("txq->init_state != SFXGE_TXQ_INITIALIZED"));
1317 	KASSERT(evq->init_state == SFXGE_EVQ_STARTED,
1318 	    ("evq->init_state != SFXGE_EVQ_STARTED"));
1319 
1320 	/* Program the buffer table. */
1321 	if ((rc = efx_sram_buf_tbl_set(sc->enp, txq->buf_base_id, esmp,
1322 	    EFX_TXQ_NBUFS(sc->txq_entries))) != 0)
1323 		return (rc);
1324 
1325 	/* Determine the kind of queue we are creating. */
1326 	switch (txq->type) {
1327 	case SFXGE_TXQ_NON_CKSUM:
1328 		flags = 0;
1329 		break;
1330 	case SFXGE_TXQ_IP_CKSUM:
1331 		flags = EFX_CKSUM_IPV4;
1332 		break;
1333 	case SFXGE_TXQ_IP_TCP_UDP_CKSUM:
1334 		flags = EFX_CKSUM_IPV4 | EFX_CKSUM_TCPUDP;
1335 		break;
1336 	default:
1337 		KASSERT(0, ("Impossible TX queue"));
1338 		flags = 0;
1339 		break;
1340 	}
1341 
1342 	/* Create the common code transmit queue. */
1343 	if ((rc = efx_tx_qcreate(sc->enp, index, txq->type, esmp,
1344 	    sc->txq_entries, txq->buf_base_id, flags, evq->common,
1345 	    &txq->common, &desc_index)) != 0)
1346 		goto fail;
1347 
1348 	/* Initialise queue descriptor indexes */
1349 	txq->added = txq->pending = txq->completed = txq->reaped = desc_index;
1350 
1351 	SFXGE_TXQ_LOCK(txq);
1352 
1353 	/* Enable the transmit queue. */
1354 	efx_tx_qenable(txq->common);
1355 
1356 	txq->init_state = SFXGE_TXQ_STARTED;
1357 	txq->flush_state = SFXGE_FLUSH_REQUIRED;
1358 
1359 	SFXGE_TXQ_UNLOCK(txq);
1360 
1361 	return (0);
1362 
1363 fail:
1364 	efx_sram_buf_tbl_clear(sc->enp, txq->buf_base_id,
1365 	    EFX_TXQ_NBUFS(sc->txq_entries));
1366 	return (rc);
1367 }
1368 
1369 void
1370 sfxge_tx_stop(struct sfxge_softc *sc)
1371 {
1372 	int index;
1373 
1374 	index = sc->txq_count;
1375 	while (--index >= 0)
1376 		sfxge_tx_qstop(sc, index);
1377 
1378 	/* Tear down the transmit module */
1379 	efx_tx_fini(sc->enp);
1380 }
1381 
1382 int
1383 sfxge_tx_start(struct sfxge_softc *sc)
1384 {
1385 	int index;
1386 	int rc;
1387 
1388 	/* Initialize the common code transmit module. */
1389 	if ((rc = efx_tx_init(sc->enp)) != 0)
1390 		return (rc);
1391 
1392 	for (index = 0; index < sc->txq_count; index++) {
1393 		if ((rc = sfxge_tx_qstart(sc, index)) != 0)
1394 			goto fail;
1395 	}
1396 
1397 	return (0);
1398 
1399 fail:
1400 	while (--index >= 0)
1401 		sfxge_tx_qstop(sc, index);
1402 
1403 	efx_tx_fini(sc->enp);
1404 
1405 	return (rc);
1406 }
1407 
1408 static int
1409 sfxge_txq_stat_init(struct sfxge_txq *txq, struct sysctl_oid *txq_node)
1410 {
1411 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(txq->sc->dev);
1412 	struct sysctl_oid *stat_node;
1413 	unsigned int id;
1414 
1415 	stat_node = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(txq_node), OID_AUTO,
1416 				    "stats", CTLFLAG_RD, NULL,
1417 				    "Tx queue statistics");
1418 	if (stat_node == NULL)
1419 		return (ENOMEM);
1420 
1421 	for (id = 0; id < nitems(sfxge_tx_stats); id++) {
1422 		SYSCTL_ADD_ULONG(
1423 		    ctx, SYSCTL_CHILDREN(stat_node), OID_AUTO,
1424 		    sfxge_tx_stats[id].name, CTLFLAG_RD | CTLFLAG_STATS,
1425 		    (unsigned long *)((caddr_t)txq + sfxge_tx_stats[id].offset),
1426 		    "");
1427 	}
1428 
1429 	return (0);
1430 }
1431 
1432 /**
1433  * Destroy a transmit queue.
1434  */
1435 static void
1436 sfxge_tx_qfini(struct sfxge_softc *sc, unsigned int index)
1437 {
1438 	struct sfxge_txq *txq;
1439 	unsigned int nmaps;
1440 
1441 	txq = sc->txq[index];
1442 
1443 	KASSERT(txq->init_state == SFXGE_TXQ_INITIALIZED,
1444 	    ("txq->init_state != SFXGE_TXQ_INITIALIZED"));
1445 
1446 	if (txq->type == SFXGE_TXQ_IP_TCP_UDP_CKSUM)
1447 		tso_fini(txq);
1448 
1449 	/* Free the context arrays. */
1450 	free(txq->pend_desc, M_SFXGE);
1451 	nmaps = sc->txq_entries;
1452 	while (nmaps-- != 0)
1453 		bus_dmamap_destroy(txq->packet_dma_tag, txq->stmp[nmaps].map);
1454 	free(txq->stmp, M_SFXGE);
1455 
1456 	/* Release DMA memory mapping. */
1457 	sfxge_dma_free(&txq->mem);
1458 
1459 	sc->txq[index] = NULL;
1460 
1461 	SFXGE_TXQ_LOCK_DESTROY(txq);
1462 
1463 	free(txq, M_SFXGE);
1464 }
1465 
1466 /*
1467  * Estimate maximum number of Tx descriptors required for TSO packet.
1468  * With minimum MSS and maximum mbuf length we might need more (even
1469  * than a ring-ful of descriptors), but this should not happen in
1470  * practice except due to deliberate attack.  In that case we will
1471  * truncate the output at a packet boundary.
1472  */
1473 static unsigned int
1474 sfxge_tx_max_pkt_desc(const struct sfxge_softc *sc, enum sfxge_txq_type type)
1475 {
1476 	/* One descriptor for every input fragment */
1477 	unsigned int max_descs = SFXGE_TX_MAPPING_MAX_SEG;
1478 
1479 	/* VLAN tagging Tx option descriptor may be required */
1480 	if (efx_nic_cfg_get(sc->enp)->enc_hw_tx_insert_vlan_enabled)
1481 		max_descs++;
1482 
1483 	if (type == SFXGE_TXQ_IP_TCP_UDP_CKSUM) {
1484 		/*
1485 		 * Plus header and payload descriptor for each output segment.
1486 		 * Minus one since header fragment is already counted.
1487 		 */
1488 		max_descs += SFXGE_TSO_MAX_SEGS * 2 - 1;
1489 
1490 		/* FW assisted TSO requires one more descriptor per segment */
1491 		if (sc->tso_fw_assisted)
1492 			max_descs += SFXGE_TSO_MAX_SEGS;
1493 	}
1494 
1495 	return (max_descs);
1496 }
1497 
1498 static int
1499 sfxge_tx_qinit(struct sfxge_softc *sc, unsigned int txq_index,
1500 	       enum sfxge_txq_type type, unsigned int evq_index)
1501 {
1502 	char name[16];
1503 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev);
1504 	struct sysctl_oid *txq_node;
1505 	struct sfxge_txq *txq;
1506 	struct sfxge_evq *evq;
1507 	struct sfxge_tx_dpl *stdp;
1508 	struct sysctl_oid *dpl_node;
1509 	efsys_mem_t *esmp;
1510 	unsigned int nmaps;
1511 	int rc;
1512 
1513 	txq = malloc(sizeof(struct sfxge_txq), M_SFXGE, M_ZERO | M_WAITOK);
1514 	txq->sc = sc;
1515 	txq->entries = sc->txq_entries;
1516 	txq->ptr_mask = txq->entries - 1;
1517 
1518 	sc->txq[txq_index] = txq;
1519 	esmp = &txq->mem;
1520 
1521 	evq = sc->evq[evq_index];
1522 
1523 	/* Allocate and zero DMA space for the descriptor ring. */
1524 	if ((rc = sfxge_dma_alloc(sc, EFX_TXQ_SIZE(sc->txq_entries), esmp)) != 0)
1525 		return (rc);
1526 
1527 	/* Allocate buffer table entries. */
1528 	sfxge_sram_buf_tbl_alloc(sc, EFX_TXQ_NBUFS(sc->txq_entries),
1529 				 &txq->buf_base_id);
1530 
1531 	/* Create a DMA tag for packet mappings. */
1532 	if (bus_dma_tag_create(sc->parent_dma_tag, 1, 0x1000,
1533 	    MIN(0x3FFFFFFFFFFFUL, BUS_SPACE_MAXADDR), BUS_SPACE_MAXADDR, NULL,
1534 	    NULL, 0x11000, SFXGE_TX_MAPPING_MAX_SEG, 0x1000, 0, NULL, NULL,
1535 	    &txq->packet_dma_tag) != 0) {
1536 		device_printf(sc->dev, "Couldn't allocate txq DMA tag\n");
1537 		rc = ENOMEM;
1538 		goto fail;
1539 	}
1540 
1541 	/* Allocate pending descriptor array for batching writes. */
1542 	txq->pend_desc = malloc(sizeof(efx_desc_t) * sc->txq_entries,
1543 				M_SFXGE, M_ZERO | M_WAITOK);
1544 
1545 	/* Allocate and initialise mbuf DMA mapping array. */
1546 	txq->stmp = malloc(sizeof(struct sfxge_tx_mapping) * sc->txq_entries,
1547 	    M_SFXGE, M_ZERO | M_WAITOK);
1548 	for (nmaps = 0; nmaps < sc->txq_entries; nmaps++) {
1549 		rc = bus_dmamap_create(txq->packet_dma_tag, 0,
1550 				       &txq->stmp[nmaps].map);
1551 		if (rc != 0)
1552 			goto fail2;
1553 	}
1554 
1555 	snprintf(name, sizeof(name), "%u", txq_index);
1556 	txq_node = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(sc->txqs_node),
1557 				   OID_AUTO, name, CTLFLAG_RD, NULL, "");
1558 	if (txq_node == NULL) {
1559 		rc = ENOMEM;
1560 		goto fail_txq_node;
1561 	}
1562 
1563 	if (type == SFXGE_TXQ_IP_TCP_UDP_CKSUM &&
1564 	    (rc = tso_init(txq)) != 0)
1565 		goto fail3;
1566 
1567 	if (sfxge_tx_dpl_get_max <= 0) {
1568 		log(LOG_ERR, "%s=%d must be greater than 0",
1569 		    SFXGE_PARAM_TX_DPL_GET_MAX, sfxge_tx_dpl_get_max);
1570 		rc = EINVAL;
1571 		goto fail_tx_dpl_get_max;
1572 	}
1573 	if (sfxge_tx_dpl_get_non_tcp_max <= 0) {
1574 		log(LOG_ERR, "%s=%d must be greater than 0",
1575 		    SFXGE_PARAM_TX_DPL_GET_NON_TCP_MAX,
1576 		    sfxge_tx_dpl_get_non_tcp_max);
1577 		rc = EINVAL;
1578 		goto fail_tx_dpl_get_max;
1579 	}
1580 	if (sfxge_tx_dpl_put_max < 0) {
1581 		log(LOG_ERR, "%s=%d must be greater or equal to 0",
1582 		    SFXGE_PARAM_TX_DPL_PUT_MAX, sfxge_tx_dpl_put_max);
1583 		rc = EINVAL;
1584 		goto fail_tx_dpl_put_max;
1585 	}
1586 
1587 	/* Initialize the deferred packet list. */
1588 	stdp = &txq->dpl;
1589 	stdp->std_put_max = sfxge_tx_dpl_put_max;
1590 	stdp->std_get_max = sfxge_tx_dpl_get_max;
1591 	stdp->std_get_non_tcp_max = sfxge_tx_dpl_get_non_tcp_max;
1592 	stdp->std_getp = &stdp->std_get;
1593 
1594 	SFXGE_TXQ_LOCK_INIT(txq, device_get_nameunit(sc->dev), txq_index);
1595 
1596 	dpl_node = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(txq_node), OID_AUTO,
1597 				   "dpl", CTLFLAG_RD, NULL,
1598 				   "Deferred packet list statistics");
1599 	if (dpl_node == NULL) {
1600 		rc = ENOMEM;
1601 		goto fail_dpl_node;
1602 	}
1603 
1604 	SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(dpl_node), OID_AUTO,
1605 			"get_count", CTLFLAG_RD | CTLFLAG_STATS,
1606 			&stdp->std_get_count, 0, "");
1607 	SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(dpl_node), OID_AUTO,
1608 			"get_non_tcp_count", CTLFLAG_RD | CTLFLAG_STATS,
1609 			&stdp->std_get_non_tcp_count, 0, "");
1610 	SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(dpl_node), OID_AUTO,
1611 			"get_hiwat", CTLFLAG_RD | CTLFLAG_STATS,
1612 			&stdp->std_get_hiwat, 0, "");
1613 	SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(dpl_node), OID_AUTO,
1614 			"put_hiwat", CTLFLAG_RD | CTLFLAG_STATS,
1615 			&stdp->std_put_hiwat, 0, "");
1616 
1617 	rc = sfxge_txq_stat_init(txq, txq_node);
1618 	if (rc != 0)
1619 		goto fail_txq_stat_init;
1620 
1621 	txq->type = type;
1622 	txq->evq_index = evq_index;
1623 	txq->txq_index = txq_index;
1624 	txq->init_state = SFXGE_TXQ_INITIALIZED;
1625 	txq->hw_vlan_tci = 0;
1626 
1627 	txq->max_pkt_desc = sfxge_tx_max_pkt_desc(sc, type);
1628 
1629 	return (0);
1630 
1631 fail_txq_stat_init:
1632 fail_dpl_node:
1633 fail_tx_dpl_put_max:
1634 fail_tx_dpl_get_max:
1635 fail3:
1636 fail_txq_node:
1637 	free(txq->pend_desc, M_SFXGE);
1638 fail2:
1639 	while (nmaps-- != 0)
1640 		bus_dmamap_destroy(txq->packet_dma_tag, txq->stmp[nmaps].map);
1641 	free(txq->stmp, M_SFXGE);
1642 	bus_dma_tag_destroy(txq->packet_dma_tag);
1643 
1644 fail:
1645 	sfxge_dma_free(esmp);
1646 
1647 	return (rc);
1648 }
1649 
1650 static int
1651 sfxge_tx_stat_handler(SYSCTL_HANDLER_ARGS)
1652 {
1653 	struct sfxge_softc *sc = arg1;
1654 	unsigned int id = arg2;
1655 	unsigned long sum;
1656 	unsigned int index;
1657 
1658 	/* Sum across all TX queues */
1659 	sum = 0;
1660 	for (index = 0; index < sc->txq_count; index++)
1661 		sum += *(unsigned long *)((caddr_t)sc->txq[index] +
1662 					  sfxge_tx_stats[id].offset);
1663 
1664 	return (SYSCTL_OUT(req, &sum, sizeof(sum)));
1665 }
1666 
1667 static void
1668 sfxge_tx_stat_init(struct sfxge_softc *sc)
1669 {
1670 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev);
1671 	struct sysctl_oid_list *stat_list;
1672 	unsigned int id;
1673 
1674 	stat_list = SYSCTL_CHILDREN(sc->stats_node);
1675 
1676 	for (id = 0; id < nitems(sfxge_tx_stats); id++) {
1677 		SYSCTL_ADD_PROC(
1678 			ctx, stat_list,
1679 			OID_AUTO, sfxge_tx_stats[id].name,
1680 			CTLTYPE_ULONG|CTLFLAG_RD,
1681 			sc, id, sfxge_tx_stat_handler, "LU",
1682 			"");
1683 	}
1684 }
1685 
1686 uint64_t
1687 sfxge_tx_get_drops(struct sfxge_softc *sc)
1688 {
1689 	unsigned int index;
1690 	uint64_t drops = 0;
1691 	struct sfxge_txq *txq;
1692 
1693 	/* Sum across all TX queues */
1694 	for (index = 0; index < sc->txq_count; index++) {
1695 		txq = sc->txq[index];
1696 		/*
1697 		 * In theory, txq->put_overflow and txq->netdown_drops
1698 		 * should use atomic operation and other should be
1699 		 * obtained under txq lock, but it is just statistics.
1700 		 */
1701 		drops += txq->drops + txq->get_overflow +
1702 			 txq->get_non_tcp_overflow +
1703 			 txq->put_overflow + txq->netdown_drops +
1704 			 txq->tso_pdrop_too_many + txq->tso_pdrop_no_rsrc;
1705 	}
1706 	return (drops);
1707 }
1708 
1709 void
1710 sfxge_tx_fini(struct sfxge_softc *sc)
1711 {
1712 	int index;
1713 
1714 	index = sc->txq_count;
1715 	while (--index >= 0)
1716 		sfxge_tx_qfini(sc, index);
1717 
1718 	sc->txq_count = 0;
1719 }
1720 
1721 
1722 int
1723 sfxge_tx_init(struct sfxge_softc *sc)
1724 {
1725 	const efx_nic_cfg_t *encp = efx_nic_cfg_get(sc->enp);
1726 	struct sfxge_intr *intr;
1727 	int index;
1728 	int rc;
1729 
1730 	intr = &sc->intr;
1731 
1732 	KASSERT(intr->state == SFXGE_INTR_INITIALIZED,
1733 	    ("intr->state != SFXGE_INTR_INITIALIZED"));
1734 
1735 	sc->txq_count = SFXGE_TXQ_NTYPES - 1 + sc->intr.n_alloc;
1736 
1737 	sc->tso_fw_assisted = sfxge_tso_fw_assisted;
1738 	if (sc->tso_fw_assisted)
1739 		sc->tso_fw_assisted =
1740 		    (encp->enc_features & EFX_FEATURE_FW_ASSISTED_TSO) &&
1741 		    (encp->enc_fw_assisted_tso_enabled);
1742 
1743 	sc->txqs_node = SYSCTL_ADD_NODE(
1744 		device_get_sysctl_ctx(sc->dev),
1745 		SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)),
1746 		OID_AUTO, "txq", CTLFLAG_RD, NULL, "Tx queues");
1747 	if (sc->txqs_node == NULL) {
1748 		rc = ENOMEM;
1749 		goto fail_txq_node;
1750 	}
1751 
1752 	/* Initialize the transmit queues */
1753 	if ((rc = sfxge_tx_qinit(sc, SFXGE_TXQ_NON_CKSUM,
1754 	    SFXGE_TXQ_NON_CKSUM, 0)) != 0)
1755 		goto fail;
1756 
1757 	if ((rc = sfxge_tx_qinit(sc, SFXGE_TXQ_IP_CKSUM,
1758 	    SFXGE_TXQ_IP_CKSUM, 0)) != 0)
1759 		goto fail2;
1760 
1761 	for (index = 0;
1762 	     index < sc->txq_count - SFXGE_TXQ_NTYPES + 1;
1763 	     index++) {
1764 		if ((rc = sfxge_tx_qinit(sc, SFXGE_TXQ_NTYPES - 1 + index,
1765 		    SFXGE_TXQ_IP_TCP_UDP_CKSUM, index)) != 0)
1766 			goto fail3;
1767 	}
1768 
1769 	sfxge_tx_stat_init(sc);
1770 
1771 	return (0);
1772 
1773 fail3:
1774 	while (--index >= 0)
1775 		sfxge_tx_qfini(sc, SFXGE_TXQ_IP_TCP_UDP_CKSUM + index);
1776 
1777 	sfxge_tx_qfini(sc, SFXGE_TXQ_IP_CKSUM);
1778 
1779 fail2:
1780 	sfxge_tx_qfini(sc, SFXGE_TXQ_NON_CKSUM);
1781 
1782 fail:
1783 fail_txq_node:
1784 	sc->txq_count = 0;
1785 	return (rc);
1786 }
1787