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