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