]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/sfxge/sfxge_tx.h
sfxge: implement if_get_counter callback
[FreeBSD/FreeBSD.git] / sys / dev / sfxge / sfxge_tx.h
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  * $FreeBSD$
30  */
31
32 #ifndef _SFXGE_TX_H
33 #define _SFXGE_TX_H
34
35 #include <netinet/in.h>
36 #include <netinet/ip.h>
37 #include <netinet/tcp.h>
38
39 /* Maximum size of TSO packet */
40 #define SFXGE_TSO_MAX_SIZE              (65535)
41
42 /*
43  * Maximum number of segments to be created for a TSO packet.
44  * Allow for a reasonable minimum MSS of 512.
45  */
46 #define SFXGE_TSO_MAX_SEGS              howmany(SFXGE_TSO_MAX_SIZE, 512)
47
48 /* Maximum number of DMA segments needed to map an mbuf chain.  With
49  * TSO, the mbuf length may be just over 64K, divided into 2K mbuf
50  * clusters.  (The chain could be longer than this initially, but can
51  * be shortened with m_collapse().)
52  */
53 #define SFXGE_TX_MAPPING_MAX_SEG                                        \
54         (1 + howmany(SFXGE_TSO_MAX_SIZE, MCLBYTES))
55
56 /*
57  * Buffer mapping flags.
58  *
59  * Buffers and DMA mappings must be freed when the last descriptor
60  * referring to them is completed.  Set the TX_BUF_UNMAP and
61  * TX_BUF_MBUF flags on the last descriptor generated for an mbuf
62  * chain.  Set only the TX_BUF_UNMAP flag on a descriptor referring to
63  * a heap buffer.
64  */
65 enum sfxge_tx_buf_flags {
66         TX_BUF_UNMAP = 1,
67         TX_BUF_MBUF = 2,
68 };
69
70 /*
71  * Buffer mapping information for descriptors in flight.
72  */
73 struct sfxge_tx_mapping {
74         union {
75                 struct mbuf     *mbuf;
76                 caddr_t         heap_buf;
77         }                       u;
78         bus_dmamap_t            map;
79         enum sfxge_tx_buf_flags flags;
80 };
81
82 #define SFXGE_TX_DPL_GET_PKT_LIMIT_DEFAULT              (64 * 1024)
83 #define SFXGE_TX_DPL_GET_NON_TCP_PKT_LIMIT_DEFAULT      1024
84 #define SFXGE_TX_DPL_PUT_PKT_LIMIT_DEFAULT              64
85
86 /*
87  * Deferred packet list.
88  */
89 struct sfxge_tx_dpl {
90         unsigned int    std_get_max;            /* Maximum number  of packets
91                                                  * in get list */
92         unsigned int    std_get_non_tcp_max;    /* Maximum number
93                                                  * of non-TCP packets
94                                                  * in get list */
95         unsigned int    std_put_max;            /* Maximum number of packets
96                                                  * in put list */
97         uintptr_t       std_put;                /* Head of put list. */
98         struct mbuf     *std_get;               /* Head of get list. */
99         struct mbuf     **std_getp;             /* Tail of get list. */
100         unsigned int    std_get_count;          /* Packets in get list. */
101         unsigned int    std_get_non_tcp_count;  /* Non-TCP packets
102                                                  * in get list */
103         unsigned int    std_get_hiwat;          /* Packets in get list
104                                                  * high watermark */
105 };
106
107
108 #define SFXGE_TX_BUFFER_SIZE    0x400
109 #define SFXGE_TX_HEADER_SIZE    0x100
110 #define SFXGE_TX_COPY_THRESHOLD 0x200
111
112 enum sfxge_txq_state {
113         SFXGE_TXQ_UNINITIALIZED = 0,
114         SFXGE_TXQ_INITIALIZED,
115         SFXGE_TXQ_STARTED
116 };
117
118 enum sfxge_txq_type {
119         SFXGE_TXQ_NON_CKSUM = 0,
120         SFXGE_TXQ_IP_CKSUM,
121         SFXGE_TXQ_IP_TCP_UDP_CKSUM,
122         SFXGE_TXQ_NTYPES
123 };
124
125 #define SFXGE_TXQ_UNBLOCK_LEVEL(_entries)       (EFX_TXQ_LIMIT(_entries) / 4)
126
127 #define SFXGE_TX_BATCH  64
128
129 #ifdef SFXGE_HAVE_MQ
130 #define SFXGE_TX_LOCK(txq)              (&(txq)->lock)
131 #else
132 #define SFXGE_TX_LOCK(txq)              (&(txq)->sc->tx_lock)
133 #endif
134
135 #define SFXGE_TXQ_LOCK_INIT(_txq, _ifname, _txq_index)                  \
136         do {                                                            \
137                 struct sfxge_txq  *__txq = (_txq);                      \
138                                                                         \
139                 snprintf((__txq)->lock_name,                            \
140                          sizeof((__txq)->lock_name),                    \
141                          "%s:txq%u", (_ifname), (_txq_index));          \
142                 mtx_init(&(__txq)->lock, (__txq)->lock_name,            \
143                          NULL, MTX_DEF);                                \
144         } while (B_FALSE)
145 #define SFXGE_TXQ_LOCK_DESTROY(_txq)                                    \
146         mtx_destroy(&(_txq)->lock)
147 #define SFXGE_TXQ_LOCK(_txq)                                            \
148         mtx_lock(SFXGE_TX_LOCK(_txq))
149 #define SFXGE_TXQ_TRYLOCK(_txq)                                         \
150         mtx_trylock(SFXGE_TX_LOCK(_txq))
151 #define SFXGE_TXQ_UNLOCK(_txq)                                          \
152         mtx_unlock(SFXGE_TX_LOCK(_txq))
153 #define SFXGE_TXQ_LOCK_ASSERT_OWNED(_txq)                               \
154         mtx_assert(SFXGE_TX_LOCK(_txq), MA_OWNED)
155
156
157 struct sfxge_txq {
158         /* The following fields should be written very rarely */
159         struct sfxge_softc              *sc;
160         enum sfxge_txq_state            init_state;
161         enum sfxge_flush_state          flush_state;
162         enum sfxge_txq_type             type;
163         unsigned int                    txq_index;
164         unsigned int                    evq_index;
165         efsys_mem_t                     mem;
166         unsigned int                    buf_base_id;
167         unsigned int                    entries;
168         unsigned int                    ptr_mask;
169
170         struct sfxge_tx_mapping         *stmp;  /* Packets in flight. */
171         bus_dma_tag_t                   packet_dma_tag;
172         efx_buffer_t                    *pend_desc;
173         efx_txq_t                       *common;
174
175         efsys_mem_t                     *tsoh_buffer;
176
177         char                            lock_name[SFXGE_LOCK_NAME_MAX];
178
179         /* This field changes more often and is read regularly on both
180          * the initiation and completion paths
181          */
182         int                             blocked __aligned(CACHE_LINE_SIZE);
183
184         /* The following fields change more often, and are used mostly
185          * on the initiation path
186          */
187 #ifdef SFXGE_HAVE_MQ
188         struct mtx                      lock __aligned(CACHE_LINE_SIZE);
189         struct sfxge_tx_dpl             dpl;    /* Deferred packet list. */
190         unsigned int                    n_pend_desc;
191 #else
192         unsigned int                    n_pend_desc __aligned(CACHE_LINE_SIZE);
193 #endif
194         unsigned int                    added;
195         unsigned int                    reaped;
196         /* Statistics */
197         unsigned long                   tso_bursts;
198         unsigned long                   tso_packets;
199         unsigned long                   tso_long_headers;
200         unsigned long                   collapses;
201         unsigned long                   drops;
202         unsigned long                   get_overflow;
203         unsigned long                   get_non_tcp_overflow;
204         unsigned long                   put_overflow;
205         unsigned long                   netdown_drops;
206         unsigned long                   tso_pdrop_too_many;
207         unsigned long                   tso_pdrop_no_rsrc;
208
209         /* The following fields change more often, and are used mostly
210          * on the completion path
211          */
212         unsigned int                    pending __aligned(CACHE_LINE_SIZE);
213         unsigned int                    completed;
214         struct sfxge_txq                *next;
215 };
216
217 struct sfxge_evq;
218
219 extern int sfxge_tx_packet_add(struct sfxge_txq *, struct mbuf *);
220 extern uint64_t sfxge_tx_get_drops(struct sfxge_softc *sc);
221
222 extern int sfxge_tx_init(struct sfxge_softc *sc);
223 extern void sfxge_tx_fini(struct sfxge_softc *sc);
224 extern int sfxge_tx_start(struct sfxge_softc *sc);
225 extern void sfxge_tx_stop(struct sfxge_softc *sc);
226 extern void sfxge_tx_qcomplete(struct sfxge_txq *txq, struct sfxge_evq *evq);
227 extern void sfxge_tx_qflush_done(struct sfxge_txq *txq);
228 #ifdef SFXGE_HAVE_MQ
229 extern void sfxge_if_qflush(struct ifnet *ifp);
230 extern int sfxge_if_transmit(struct ifnet *ifp, struct mbuf *m);
231 #else
232 extern void sfxge_if_start(struct ifnet *ifp);
233 #endif
234
235 #endif