xref: /freebsd/sys/dev/sfxge/sfxge_tx.h (revision f4b37ed0f8b307b1f3f0f630ca725d68f1dff30d)
1 /*-
2  * Copyright (c) 2010-2015 Solarflare Communications Inc.
3  * All rights reserved.
4  *
5  * This software was developed in part by Philip Paeps under contract for
6  * Solarflare Communications, Inc.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright notice,
12  *    this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright notice,
14  *    this list of conditions and the following disclaimer in the documentation
15  *    and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  * The views and conclusions contained in the software and documentation are
30  * those of the authors and should not be interpreted as representing official
31  * policies, either expressed or implied, of the FreeBSD Project.
32  *
33  * $FreeBSD$
34  */
35 
36 #ifndef _SFXGE_TX_H
37 #define	_SFXGE_TX_H
38 
39 #include <netinet/in.h>
40 #include <netinet/ip.h>
41 #include <netinet/tcp.h>
42 
43 /* Maximum size of TSO packet */
44 #define	SFXGE_TSO_MAX_SIZE		(65535)
45 
46 /*
47  * Maximum number of segments to be created for a TSO packet.
48  * Allow for a reasonable minimum MSS of 512.
49  */
50 #define	SFXGE_TSO_MAX_SEGS		howmany(SFXGE_TSO_MAX_SIZE, 512)
51 
52 /* Maximum number of DMA segments needed to map an mbuf chain.  With
53  * TSO, the mbuf length may be just over 64K, divided into 2K mbuf
54  * clusters.  (The chain could be longer than this initially, but can
55  * be shortened with m_collapse().)
56  */
57 #define	SFXGE_TX_MAPPING_MAX_SEG					\
58 	(1 + howmany(SFXGE_TSO_MAX_SIZE, MCLBYTES))
59 
60 /*
61  * Buffer mapping flags.
62  *
63  * Buffers and DMA mappings must be freed when the last descriptor
64  * referring to them is completed.  Set the TX_BUF_UNMAP and
65  * TX_BUF_MBUF flags on the last descriptor generated for an mbuf
66  * chain.  Set only the TX_BUF_UNMAP flag on a descriptor referring to
67  * a heap buffer.
68  */
69 enum sfxge_tx_buf_flags {
70 	TX_BUF_UNMAP = 1,
71 	TX_BUF_MBUF = 2,
72 };
73 
74 /*
75  * Buffer mapping information for descriptors in flight.
76  */
77 struct sfxge_tx_mapping {
78 	union {
79 		struct mbuf	*mbuf;
80 		caddr_t		heap_buf;
81 	}			u;
82 	bus_dmamap_t		map;
83 	enum sfxge_tx_buf_flags	flags;
84 };
85 
86 #define	SFXGE_TX_DPL_GET_PKT_LIMIT_DEFAULT		(64 * 1024)
87 #define	SFXGE_TX_DPL_GET_NON_TCP_PKT_LIMIT_DEFAULT	1024
88 #define	SFXGE_TX_DPL_PUT_PKT_LIMIT_DEFAULT		1024
89 
90 /*
91  * Deferred packet list.
92  */
93 struct sfxge_tx_dpl {
94 	unsigned int	std_get_max;		/* Maximum number  of packets
95 						 * in get list */
96 	unsigned int	std_get_non_tcp_max;	/* Maximum number
97 						 * of non-TCP packets
98 						 * in get list */
99 	unsigned int	std_put_max;		/* Maximum number of packets
100 						 * in put list */
101 	uintptr_t	std_put;		/* Head of put list. */
102 	struct mbuf	*std_get;		/* Head of get list. */
103 	struct mbuf	**std_getp;		/* Tail of get list. */
104 	unsigned int	std_get_count;		/* Packets in get list. */
105 	unsigned int	std_get_non_tcp_count;	/* Non-TCP packets
106 						 * in get list */
107 	unsigned int	std_get_hiwat;		/* Packets in get list
108 						 * high watermark */
109 	unsigned int	std_put_hiwat;		/* Packets in put list
110 						 * high watermark */
111 };
112 
113 
114 #define	SFXGE_TX_BUFFER_SIZE	0x400
115 #define	SFXGE_TX_HEADER_SIZE	0x100
116 #define	SFXGE_TX_COPY_THRESHOLD	0x200
117 
118 enum sfxge_txq_state {
119 	SFXGE_TXQ_UNINITIALIZED = 0,
120 	SFXGE_TXQ_INITIALIZED,
121 	SFXGE_TXQ_STARTED
122 };
123 
124 enum sfxge_txq_type {
125 	SFXGE_TXQ_NON_CKSUM = 0,
126 	SFXGE_TXQ_IP_CKSUM,
127 	SFXGE_TXQ_IP_TCP_UDP_CKSUM,
128 	SFXGE_TXQ_NTYPES
129 };
130 
131 #define	SFXGE_TXQ_UNBLOCK_LEVEL(_entries)	(EFX_TXQ_LIMIT(_entries) / 4)
132 
133 #define	SFXGE_TX_BATCH	64
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(&(_txq)->lock)
149 #define	SFXGE_TXQ_TRYLOCK(_txq)						\
150 	mtx_trylock(&(_txq)->lock)
151 #define	SFXGE_TXQ_UNLOCK(_txq)						\
152 	mtx_unlock(&(_txq)->lock)
153 #define	SFXGE_TXQ_LOCK_ASSERT_OWNED(_txq)				\
154 	mtx_assert(&(_txq)->lock, MA_OWNED)
155 #define	SFXGE_TXQ_LOCK_ASSERT_NOTOWNED(_txq)				\
156 	mtx_assert(&(_txq)->lock, MA_NOTOWNED)
157 
158 
159 struct sfxge_txq {
160 	/* The following fields should be written very rarely */
161 	struct sfxge_softc		*sc;
162 	enum sfxge_txq_state		init_state;
163 	enum sfxge_flush_state		flush_state;
164 	enum sfxge_txq_type		type;
165 	unsigned int			txq_index;
166 	unsigned int			evq_index;
167 	efsys_mem_t			mem;
168 	unsigned int			buf_base_id;
169 	unsigned int			entries;
170 	unsigned int			ptr_mask;
171 	unsigned int			max_pkt_desc;
172 
173 	struct sfxge_tx_mapping		*stmp;	/* Packets in flight. */
174 	bus_dma_tag_t			packet_dma_tag;
175 	efx_desc_t			*pend_desc;
176 	efx_txq_t			*common;
177 
178 	efsys_mem_t			*tsoh_buffer;
179 
180 	char				lock_name[SFXGE_LOCK_NAME_MAX];
181 
182 	/* This field changes more often and is read regularly on both
183 	 * the initiation and completion paths
184 	 */
185 	int				blocked __aligned(CACHE_LINE_SIZE);
186 
187 	/* The following fields change more often, and are used mostly
188 	 * on the initiation path
189 	 */
190 	struct mtx			lock __aligned(CACHE_LINE_SIZE);
191 	struct sfxge_tx_dpl		dpl;	/* Deferred packet list. */
192 	unsigned int			n_pend_desc;
193 	unsigned int			added;
194 	unsigned int			reaped;
195 
196 	/* The last VLAN TCI seen on the queue if FW-assisted tagging is
197 	   used */
198 	uint16_t			hw_vlan_tci;
199 
200 	/* Statistics */
201 	unsigned long			tso_bursts;
202 	unsigned long			tso_packets;
203 	unsigned long			tso_long_headers;
204 	unsigned long			collapses;
205 	unsigned long			drops;
206 	unsigned long			get_overflow;
207 	unsigned long			get_non_tcp_overflow;
208 	unsigned long			put_overflow;
209 	unsigned long			netdown_drops;
210 	unsigned long			tso_pdrop_too_many;
211 	unsigned long			tso_pdrop_no_rsrc;
212 
213 	/* The following fields change more often, and are used mostly
214 	 * on the completion path
215 	 */
216 	unsigned int			pending __aligned(CACHE_LINE_SIZE);
217 	unsigned int			completed;
218 	struct sfxge_txq		*next;
219 };
220 
221 struct sfxge_evq;
222 
223 extern uint64_t sfxge_tx_get_drops(struct sfxge_softc *sc);
224 
225 extern int sfxge_tx_init(struct sfxge_softc *sc);
226 extern void sfxge_tx_fini(struct sfxge_softc *sc);
227 extern int sfxge_tx_start(struct sfxge_softc *sc);
228 extern void sfxge_tx_stop(struct sfxge_softc *sc);
229 extern void sfxge_tx_qcomplete(struct sfxge_txq *txq, struct sfxge_evq *evq);
230 extern void sfxge_tx_qflush_done(struct sfxge_txq *txq);
231 extern void sfxge_if_qflush(struct ifnet *ifp);
232 extern int sfxge_if_transmit(struct ifnet *ifp, struct mbuf *m);
233 
234 #endif
235