xref: /freebsd/sys/dev/vmware/vmxnet3/if_vmxvar.h (revision 0572ccaa4543b0abef8ef81e384c1d04de9f3da1)
1 /*-
2  * Copyright (c) 2013 Tsubai Masanari
3  * Copyright (c) 2013 Bryan Venteicher <bryanv@FreeBSD.org>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  *
17  * $FreeBSD$
18  */
19 
20 #ifndef _IF_VMXVAR_H
21 #define _IF_VMXVAR_H
22 
23 struct vmxnet3_softc;
24 
25 struct vmxnet3_dma_alloc {
26 	bus_addr_t		dma_paddr;
27 	caddr_t			dma_vaddr;
28 	bus_dma_tag_t		dma_tag;
29 	bus_dmamap_t		dma_map;
30 	bus_size_t		dma_size;
31 };
32 
33 /*
34  * The number of Rx/Tx queues this driver prefers.
35  */
36 #define VMXNET3_DEF_RX_QUEUES	8
37 #define VMXNET3_DEF_TX_QUEUES	8
38 
39 /*
40  * The number of Rx rings in each Rx queue.
41  */
42 #define VMXNET3_RXRINGS_PERQ	2
43 
44 /*
45  * The number of descriptors in each Rx/Tx ring.
46  */
47 #define VMXNET3_DEF_TX_NDESC		512
48 #define VMXNET3_MAX_TX_NDESC		4096
49 #define VMXNET3_MIN_TX_NDESC		32
50 #define VMXNET3_MASK_TX_NDESC		0x1F
51 #define VMXNET3_DEF_RX_NDESC		256
52 #define VMXNET3_MAX_RX_NDESC		2048
53 #define VMXNET3_MIN_RX_NDESC		32
54 #define VMXNET3_MASK_RX_NDESC		0x1F
55 
56 #define VMXNET3_MAX_TX_NCOMPDESC	VMXNET3_MAX_TX_NDESC
57 #define VMXNET3_MAX_RX_NCOMPDESC \
58     (VMXNET3_MAX_RX_NDESC * VMXNET3_RXRINGS_PERQ)
59 
60 struct vmxnet3_txbuf {
61 	bus_dmamap_t		 vtxb_dmamap;
62 	struct mbuf		*vtxb_m;
63 };
64 
65 struct vmxnet3_txring {
66 	struct vmxnet3_txbuf	*vxtxr_txbuf;
67 	u_int			 vxtxr_head;
68 	u_int			 vxtxr_next;
69 	u_int			 vxtxr_ndesc;
70 	int			 vxtxr_gen;
71 	bus_dma_tag_t		 vxtxr_txtag;
72 	struct vmxnet3_txdesc	*vxtxr_txd;
73 	struct vmxnet3_dma_alloc vxtxr_dma;
74 };
75 
76 static inline int
77 VMXNET3_TXRING_AVAIL(struct vmxnet3_txring *txr)
78 {
79 	int avail = txr->vxtxr_next - txr->vxtxr_head - 1;
80 	return (avail < 0 ? txr->vxtxr_ndesc + avail : avail);
81 }
82 
83 struct vmxnet3_rxbuf {
84 	bus_dmamap_t		 vrxb_dmamap;
85 	struct mbuf		*vrxb_m;
86 };
87 
88 struct vmxnet3_rxring {
89 	struct vmxnet3_rxbuf	*vxrxr_rxbuf;
90 	struct vmxnet3_rxdesc	*vxrxr_rxd;
91 	u_int			 vxrxr_fill;
92 	u_int			 vxrxr_ndesc;
93 	int			 vxrxr_gen;
94 	int			 vxrxr_rid;
95 	bus_dma_tag_t		 vxrxr_rxtag;
96 	struct vmxnet3_dma_alloc vxrxr_dma;
97 	bus_dmamap_t		 vxrxr_spare_dmap;
98 };
99 
100 static inline void
101 vmxnet3_rxr_increment_fill(struct vmxnet3_rxring *rxr)
102 {
103 
104 	if (++rxr->vxrxr_fill == rxr->vxrxr_ndesc) {
105 		rxr->vxrxr_fill = 0;
106 		rxr->vxrxr_gen ^= 1;
107 	}
108 }
109 
110 struct vmxnet3_comp_ring {
111 	union {
112 		struct vmxnet3_txcompdesc *txcd;
113 		struct vmxnet3_rxcompdesc *rxcd;
114 	}			 vxcr_u;
115 	u_int			 vxcr_next;
116 	u_int			 vxcr_ndesc;
117 	int			 vxcr_gen;
118 	struct vmxnet3_dma_alloc vxcr_dma;
119 };
120 
121 struct vmxnet3_txq_stats {
122 	uint64_t		vmtxs_opackets;	/* if_opackets */
123 	uint64_t		vmtxs_obytes;	/* if_obytes */
124 	uint64_t		vmtxs_omcasts;	/* if_omcasts */
125 	uint64_t		vmtxs_csum;
126 	uint64_t		vmtxs_tso;
127 	uint64_t		vmtxs_full;
128 	uint64_t		vmtxs_offload_failed;
129 };
130 
131 struct vmxnet3_txqueue {
132 	struct mtx			 vxtxq_mtx;
133 	struct vmxnet3_softc		*vxtxq_sc;
134 #ifndef VMXNET3_TX_LEGACY
135 	struct buf_ring			*vxtxq_br;
136 #endif
137 	int				 vxtxq_id;
138 	int				 vxtxq_intr_idx;
139 	int				 vxtxq_watchdog;
140 	struct vmxnet3_txring		 vxtxq_cmd_ring;
141 	struct vmxnet3_comp_ring	 vxtxq_comp_ring;
142 	struct vmxnet3_txq_stats	 vxtxq_stats;
143 	struct vmxnet3_txq_shared	*vxtxq_ts;
144 	struct sysctl_oid_list		*vxtxq_sysctl;
145 #ifndef VMXNET3_TX_LEGACY
146 	struct task			 vxtxq_defrtask;
147 #endif
148 	char				 vxtxq_name[16];
149 } __aligned(CACHE_LINE_SIZE);
150 
151 #define VMXNET3_TXQ_LOCK(_txq)		mtx_lock(&(_txq)->vxtxq_mtx)
152 #define VMXNET3_TXQ_TRYLOCK(_txq)	mtx_trylock(&(_txq)->vxtxq_mtx)
153 #define VMXNET3_TXQ_UNLOCK(_txq)	mtx_unlock(&(_txq)->vxtxq_mtx)
154 #define VMXNET3_TXQ_LOCK_ASSERT(_txq)		\
155     mtx_assert(&(_txq)->vxtxq_mtx, MA_OWNED)
156 #define VMXNET3_TXQ_LOCK_ASSERT_NOTOWNED(_txq)	\
157     mtx_assert(&(_txq)->vxtxq_mtx, MA_NOTOWNED)
158 
159 struct vmxnet3_rxq_stats {
160 	uint64_t		vmrxs_ipackets;	/* if_ipackets */
161 	uint64_t		vmrxs_ibytes;	/* if_ibytes */
162 	uint64_t		vmrxs_iqdrops;	/* if_iqdrops */
163 	uint64_t		vmrxs_ierrors;	/* if_ierrors */
164 };
165 
166 struct vmxnet3_rxqueue {
167 	struct mtx			 vxrxq_mtx;
168 	struct vmxnet3_softc		*vxrxq_sc;
169 	int				 vxrxq_id;
170 	int				 vxrxq_intr_idx;
171 	struct vmxnet3_rxring		 vxrxq_cmd_ring[VMXNET3_RXRINGS_PERQ];
172 	struct vmxnet3_comp_ring	 vxrxq_comp_ring;
173 	struct vmxnet3_rxq_stats	 vxrxq_stats;
174 	struct vmxnet3_rxq_shared	*vxrxq_rs;
175 	struct sysctl_oid_list		*vxrxq_sysctl;
176 	char				 vxrxq_name[16];
177 } __aligned(CACHE_LINE_SIZE);
178 
179 #define VMXNET3_RXQ_LOCK(_rxq)		mtx_lock(&(_rxq)->vxrxq_mtx)
180 #define VMXNET3_RXQ_UNLOCK(_rxq)	mtx_unlock(&(_rxq)->vxrxq_mtx)
181 #define VMXNET3_RXQ_LOCK_ASSERT(_rxq)		\
182     mtx_assert(&(_rxq)->vxrxq_mtx, MA_OWNED)
183 #define VMXNET3_RXQ_LOCK_ASSERT_NOTOWNED(_rxq)	\
184     mtx_assert(&(_rxq)->vxrxq_mtx, MA_NOTOWNED)
185 
186 struct vmxnet3_statistics {
187 	uint32_t		vmst_defragged;
188 	uint32_t		vmst_defrag_failed;
189 	uint32_t		vmst_mgetcl_failed;
190 	uint32_t		vmst_mbuf_load_failed;
191 };
192 
193 struct vmxnet3_interrupt {
194 	struct resource		*vmxi_irq;
195 	int			 vmxi_rid;
196 	void			*vmxi_handler;
197 };
198 
199 struct vmxnet3_softc {
200 	device_t			 vmx_dev;
201 	struct ifnet			*vmx_ifp;
202 	struct vmxnet3_driver_shared	*vmx_ds;
203 	uint32_t			 vmx_flags;
204 #define VMXNET3_FLAG_NO_MSIX	0x0001
205 #define VMXNET3_FLAG_RSS	0x0002
206 
207 	struct vmxnet3_rxqueue		*vmx_rxq;
208 	struct vmxnet3_txqueue		*vmx_txq;
209 
210 	struct resource			*vmx_res0;
211 	bus_space_tag_t			 vmx_iot0;
212 	bus_space_handle_t		 vmx_ioh0;
213 	struct resource			*vmx_res1;
214 	bus_space_tag_t			 vmx_iot1;
215 	bus_space_handle_t		 vmx_ioh1;
216 	struct resource			*vmx_msix_res;
217 
218 	int				 vmx_link_active;
219 	int				 vmx_link_speed;
220 	int				 vmx_if_flags;
221 	int				 vmx_ntxqueues;
222 	int				 vmx_nrxqueues;
223 	int				 vmx_ntxdescs;
224 	int				 vmx_nrxdescs;
225 	int				 vmx_max_rxsegs;
226 	int				 vmx_rx_max_chain;
227 
228 	struct vmxnet3_statistics	 vmx_stats;
229 
230 	int				 vmx_intr_type;
231 	int				 vmx_intr_mask_mode;
232 	int				 vmx_event_intr_idx;
233 	int				 vmx_nintrs;
234 	struct vmxnet3_interrupt	 vmx_intrs[VMXNET3_MAX_INTRS];
235 
236 	struct mtx			 vmx_mtx;
237 #ifndef VMXNET3_LEGACY_TX
238 	struct taskqueue		*vmx_tq;
239 #endif
240 	uint8_t				*vmx_mcast;
241 	void				*vmx_qs;
242 	struct vmxnet3_rss_shared	*vmx_rss;
243 	struct callout			 vmx_tick;
244 	struct vmxnet3_dma_alloc	 vmx_ds_dma;
245 	struct vmxnet3_dma_alloc	 vmx_qs_dma;
246 	struct vmxnet3_dma_alloc	 vmx_mcast_dma;
247 	struct vmxnet3_dma_alloc	 vmx_rss_dma;
248 	struct ifmedia			 vmx_media;
249 	int				 vmx_max_ntxqueues;
250 	int				 vmx_max_nrxqueues;
251 	eventhandler_tag		 vmx_vlan_attach;
252 	eventhandler_tag		 vmx_vlan_detach;
253 	uint32_t			 vmx_vlan_filter[4096/32];
254 	uint8_t				 vmx_lladdr[ETHER_ADDR_LEN];
255 };
256 
257 #define VMXNET3_CORE_LOCK_INIT(_sc, _name) \
258     mtx_init(&(_sc)->vmx_mtx, _name, "VMXNET3 Lock", MTX_DEF)
259 #define VMXNET3_CORE_LOCK_DESTROY(_sc)	mtx_destroy(&(_sc)->vmx_mtx)
260 #define VMXNET3_CORE_LOCK(_sc)		mtx_lock(&(_sc)->vmx_mtx)
261 #define VMXNET3_CORE_UNLOCK(_sc)	mtx_unlock(&(_sc)->vmx_mtx)
262 #define VMXNET3_CORE_LOCK_ASSERT(_sc)	mtx_assert(&(_sc)->vmx_mtx, MA_OWNED)
263 #define VMXNET3_CORE_LOCK_ASSERT_NOTOWNED(_sc) \
264     mtx_assert(&(_sc)->vmx_mtx, MA_NOTOWNED)
265 
266 /*
267  * Our driver version we report to the hypervisor; we just keep
268  * this value constant.
269  */
270 #define VMXNET3_DRIVER_VERSION 0x00010000
271 
272 /*
273  * Max descriptors per Tx packet. We must limit the size of the
274  * any TSO packets based on the number of segments.
275  */
276 #define VMXNET3_TX_MAXSEGS		32
277 #define VMXNET3_TX_MAXSIZE		(VMXNET3_TX_MAXSEGS * MCLBYTES)
278 #define VMXNET3_TSO_MAXSIZE \
279     (VMXNET3_TX_MAXSIZE - sizeof(struct ether_vlan_header))
280 
281 /*
282  * Maximum support Tx segments size. The length field in the
283  * Tx descriptor is 14 bits.
284  */
285 #define VMXNET3_TX_MAXSEGSIZE		(1 << 14)
286 
287 /*
288  * The maximum number of Rx segments we accept. When LRO is enabled,
289  * this allows us to receive the maximum sized frame with one MCLBYTES
290  * cluster followed by 16 MJUMPAGESIZE clusters.
291  */
292 #define VMXNET3_MAX_RX_SEGS		17
293 
294 /*
295  * Predetermined size of the multicast MACs filter table. If the
296  * number of multicast addresses exceeds this size, then the
297  * ALL_MULTI mode is use instead.
298  */
299 #define VMXNET3_MULTICAST_MAX		32
300 
301 /*
302  * Our Tx watchdog timeout.
303  */
304 #define VMXNET3_WATCHDOG_TIMEOUT	5
305 
306 /*
307  * Number of slots in the Tx bufrings. This value matches most other
308  * multiqueue drivers.
309  */
310 #define VMXNET3_DEF_BUFRING_SIZE	4096
311 
312 /*
313  * IP protocols that we can perform Tx checksum offloading of.
314  */
315 #define VMXNET3_CSUM_OFFLOAD		(CSUM_TCP | CSUM_UDP)
316 #define VMXNET3_CSUM_OFFLOAD_IPV6	(CSUM_TCP_IPV6 | CSUM_UDP_IPV6)
317 
318 #define VMXNET3_CSUM_ALL_OFFLOAD	\
319     (VMXNET3_CSUM_OFFLOAD | VMXNET3_CSUM_OFFLOAD_IPV6 | CSUM_TSO)
320 
321 /*
322  * Compat macros to keep this driver compiling on old releases.
323  */
324 
325 #if !defined(SYSCTL_ADD_UQUAD)
326 #define SYSCTL_ADD_UQUAD SYSCTL_ADD_QUAD
327 #endif
328 
329 #if !defined(IFCAP_TXCSUM_IPV6)
330 #define IFCAP_TXCSUM_IPV6 0
331 #endif
332 
333 #if !defined(IFCAP_RXCSUM_IPV6)
334 #define IFCAP_RXCSUM_IPV6 0
335 #endif
336 
337 #if !defined(CSUM_TCP_IPV6)
338 #define CSUM_TCP_IPV6 0
339 #endif
340 
341 #if !defined(CSUM_UDP_IPV6)
342 #define CSUM_UDP_IPV6	0
343 #endif
344 
345 #endif /* _IF_VMXVAR_H */
346