xref: /freebsd/sys/dev/hyperv/netvsc/if_hnvar.h (revision 3ff01b231dfa83d518854c63e7c9cd1debd1139e)
1 /*-
2  * Copyright (c) 2016-2017 Microsoft Corp.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #ifndef _IF_HNVAR_H_
30 #define _IF_HNVAR_H_
31 
32 #define HN_USE_TXDESC_BUFRING
33 
34 #define HN_CHIM_SIZE			(15 * 1024 * 1024)
35 
36 #define HN_RXBUF_SIZE			(31 * 1024 * 1024)
37 #define HN_RXBUF_SIZE_COMPAT		(15 * 1024 * 1024)
38 
39 #define HN_MTU_MAX			(65535 - ETHER_ADDR_LEN)
40 
41 #define HN_TXBR_SIZE			(128 * PAGE_SIZE)
42 #define HN_RXBR_SIZE			(128 * PAGE_SIZE)
43 
44 #define HN_XACT_REQ_PGCNT		2
45 #define HN_XACT_RESP_PGCNT		2
46 #define HN_XACT_REQ_SIZE		(HN_XACT_REQ_PGCNT * PAGE_SIZE)
47 #define HN_XACT_RESP_SIZE		(HN_XACT_RESP_PGCNT * PAGE_SIZE)
48 
49 #define HN_GPACNT_MAX			32
50 
51 struct hn_txdesc;
52 #ifndef HN_USE_TXDESC_BUFRING
53 SLIST_HEAD(hn_txdesc_list, hn_txdesc);
54 #else
55 struct buf_ring;
56 #endif
57 struct hn_tx_ring;
58 
59 #define	HN_NVS_RSC_MAX		562	/* Max RSC frags in one vmbus packet */
60 
61 struct hn_rx_rsc {
62 	const uint32_t		*vlan_info;
63 	const uint32_t		*csum_info;
64 	const uint32_t		*hash_info;
65 	const uint32_t		*hash_value;
66 	uint32_t		cnt;		/* fragment count */
67 	uint32_t		pktlen;		/* full packet length */
68 	uint8_t			is_last;	/* last fragment */
69 	const void		*frag_data[HN_NVS_RSC_MAX];
70 	uint32_t		frag_len[HN_NVS_RSC_MAX];
71 };
72 
73 struct hn_rx_ring {
74 	struct ifnet	*hn_ifp;
75 	struct ifnet	*hn_rxvf_ifp;	/* SR-IOV VF for RX */
76 	struct hn_tx_ring *hn_txr;
77 	void		*hn_pktbuf;
78 	int		hn_pktbuf_len;
79 	int		hn_rx_flags;	/* HN_RX_FLAG_ */
80 	uint32_t	hn_mbuf_hash;	/* NDIS_HASH_ */
81 	uint8_t		*hn_rxbuf;	/* shadow sc->hn_rxbuf */
82 	int		hn_rx_idx;
83 	struct hn_rx_rsc rsc;
84 
85 	/* Trust csum verification on host side */
86 	int		hn_trust_hcsum;	/* HN_TRUST_HCSUM_ */
87 	struct lro_ctrl	hn_lro;
88 
89 	u_long		hn_csum_ip;
90 	u_long		hn_csum_tcp;
91 	u_long		hn_csum_udp;
92 	u_long		hn_csum_trusted;
93 	u_long		hn_lro_tried;
94 	u_long		hn_small_pkts;
95 	u_long		hn_pkts;
96 	u_long		hn_rss_pkts;
97 	u_long		hn_ack_failed;
98 	u_long		hn_rsc_pkts;
99 	u_long		hn_rsc_drop;
100 
101 	/* Rarely used stuffs */
102 	struct sysctl_oid *hn_rx_sysctl_tree;
103 
104 	void		*hn_br;		/* TX/RX bufring */
105 	struct hyperv_dma hn_br_dma;
106 
107 	struct vmbus_channel *hn_chan;
108 } __aligned(CACHE_LINE_SIZE);
109 
110 #define HN_TRUST_HCSUM_IP	0x0001
111 #define HN_TRUST_HCSUM_TCP	0x0002
112 #define HN_TRUST_HCSUM_UDP	0x0004
113 
114 #define HN_RX_FLAG_ATTACHED	0x0001
115 #define HN_RX_FLAG_BR_REF	0x0002
116 #define HN_RX_FLAG_XPNT_VF	0x0004
117 #define HN_RX_FLAG_UDP_HASH	0x0008
118 
119 struct hn_tx_ring {
120 #ifndef HN_USE_TXDESC_BUFRING
121 	struct mtx	hn_txlist_spin;
122 	struct hn_txdesc_list hn_txlist;
123 #else
124 	struct buf_ring	*hn_txdesc_br;
125 #endif
126 	int		hn_txdesc_cnt;
127 	int		hn_txdesc_avail;
128 	u_short		hn_has_txeof;
129 	u_short		hn_txdone_cnt;
130 
131 	int		hn_sched_tx;
132 	void		(*hn_txeof)(struct hn_tx_ring *);
133 	struct taskqueue *hn_tx_taskq;
134 	struct task	hn_tx_task;
135 	struct task	hn_txeof_task;
136 
137 	struct buf_ring	*hn_mbuf_br;
138 	int		hn_oactive;
139 	int		hn_tx_idx;
140 	int		hn_tx_flags;
141 
142 	struct mtx	hn_tx_lock;
143 	struct hn_softc	*hn_sc;
144 	struct vmbus_channel *hn_chan;
145 
146 	int		hn_direct_tx_size;
147 	int		hn_chim_size;
148 	bus_dma_tag_t	hn_tx_data_dtag;
149 	uint64_t	hn_csum_assist;
150 
151 	/* Applied packet transmission aggregation limits. */
152 	int		hn_agg_szmax;
153 	short		hn_agg_pktmax;
154 	short		hn_agg_align;
155 
156 	/* Packet transmission aggregation states. */
157 	struct hn_txdesc *hn_agg_txd;
158 	int		hn_agg_szleft;
159 	short		hn_agg_pktleft;
160 	struct rndis_packet_msg *hn_agg_prevpkt;
161 
162 	/* Temporary stats for each sends. */
163 	int		hn_stat_size;
164 	short		hn_stat_pkts;
165 	short		hn_stat_mcasts;
166 
167 	int		(*hn_sendpkt)(struct hn_tx_ring *, struct hn_txdesc *);
168 	int		hn_suspended;
169 	int		hn_gpa_cnt;
170 	struct vmbus_gpa hn_gpa[HN_GPACNT_MAX];
171 
172 	u_long		hn_no_txdescs;
173 	u_long		hn_send_failed;
174 	u_long		hn_txdma_failed;
175 	u_long		hn_tx_collapsed;
176 	u_long		hn_tx_chimney_tried;
177 	u_long		hn_tx_chimney;
178 	u_long		hn_pkts;
179 	u_long		hn_sends;
180 	u_long		hn_flush_failed;
181 
182 	/* Rarely used stuffs */
183 	struct hn_txdesc *hn_txdesc;
184 	bus_dma_tag_t	hn_tx_rndis_dtag;
185 	struct sysctl_oid *hn_tx_sysctl_tree;
186 } __aligned(CACHE_LINE_SIZE);
187 
188 #define HN_TX_FLAG_ATTACHED	0x0001
189 #define HN_TX_FLAG_HASHVAL	0x0002	/* support HASHVAL pktinfo */
190 
191 /*
192  * Device-specific softc structure
193  */
194 struct hn_softc {
195 	struct ifnet    *hn_ifp;
196 	struct ifmedia	hn_media;
197 	device_t        hn_dev;
198 	int             hn_if_flags;
199 	struct sx	hn_lock;
200 	struct vmbus_channel *hn_prichan;
201 
202 	int		hn_rx_ring_cnt;
203 	int		hn_rx_ring_inuse;
204 	struct hn_rx_ring *hn_rx_ring;
205 
206 	struct rmlock	hn_vf_lock;
207 	struct ifnet	*hn_vf_ifp;	/* SR-IOV VF */
208 	uint32_t	hn_xvf_flags;	/* transparent VF flags */
209 
210 	int		hn_tx_ring_cnt;
211 	int		hn_tx_ring_inuse;
212 	struct hn_tx_ring *hn_tx_ring;
213 
214 	uint8_t		*hn_chim;
215 	u_long		*hn_chim_bmap;
216 	int		hn_chim_bmap_cnt;
217 	int		hn_chim_cnt;
218 	int		hn_chim_szmax;
219 
220 	int		hn_cpu;
221 	struct taskqueue **hn_tx_taskqs;
222 	struct sysctl_oid *hn_tx_sysctl_tree;
223 	struct sysctl_oid *hn_rx_sysctl_tree;
224 	struct vmbus_xact_ctx *hn_xact;
225 	uint32_t	hn_nvs_ver;
226 	uint32_t	hn_rx_filter;
227 
228 	/* Packet transmission aggregation user settings. */
229 	int			hn_agg_size;
230 	int			hn_agg_pkts;
231 
232 	struct taskqueue	*hn_mgmt_taskq;
233 	struct taskqueue	*hn_mgmt_taskq0;
234 	struct task		hn_link_task;
235 	struct task		hn_netchg_init;
236 	struct timeout_task	hn_netchg_status;
237 	uint32_t		hn_link_flags;	/* HN_LINK_FLAG_ */
238 
239 	uint32_t		hn_caps;	/* HN_CAP_ */
240 	uint32_t		hn_flags;	/* HN_FLAG_ */
241 	u_int			hn_pollhz;
242 
243 	void			*hn_rxbuf;
244 	uint32_t		hn_rxbuf_gpadl;
245 	struct hyperv_dma	hn_rxbuf_dma;
246 
247 	uint32_t		hn_chim_gpadl;
248 	struct hyperv_dma	hn_chim_dma;
249 
250 	uint32_t		hn_rndis_rid;
251 	uint32_t		hn_ndis_ver;
252 	int			hn_ndis_tso_szmax;
253 	int			hn_ndis_tso_sgmin;
254 	uint32_t		hn_rndis_agg_size;
255 	uint32_t		hn_rndis_agg_pkts;
256 	uint32_t		hn_rndis_agg_align;
257 
258 	int			hn_rss_ind_size;
259 	uint32_t		hn_rss_hash;	/* setting, NDIS_HASH_ */
260 	uint32_t		hn_rss_hcap;	/* caps, NDIS_HASH_ */
261 	struct ndis_rssprm_toeplitz hn_rss;
262 
263 	eventhandler_tag	hn_ifaddr_evthand;
264 	eventhandler_tag	hn_ifnet_evthand;
265 	eventhandler_tag	hn_ifnet_atthand;
266 	eventhandler_tag	hn_ifnet_dethand;
267 	eventhandler_tag	hn_ifnet_lnkhand;
268 
269 	/*
270 	 * Transparent VF delayed initialization.
271 	 */
272 	int			hn_vf_rdytick;	/* ticks, 0 == ready */
273 	struct taskqueue	*hn_vf_taskq;
274 	struct timeout_task	hn_vf_init;
275 
276 	/*
277 	 * Saved information for VF under transparent mode.
278 	 */
279 	void			(*hn_vf_input)
280 				(struct ifnet *, struct mbuf *);
281 	int			hn_saved_caps;
282 	u_int			hn_saved_tsomax;
283 	u_int			hn_saved_tsosegcnt;
284 	u_int			hn_saved_tsosegsz;
285 };
286 
287 #define HN_FLAG_RXBUF_CONNECTED		0x0001
288 #define HN_FLAG_CHIM_CONNECTED		0x0002
289 #define HN_FLAG_HAS_RSSKEY		0x0004
290 #define HN_FLAG_HAS_RSSIND		0x0008
291 #define HN_FLAG_SYNTH_ATTACHED		0x0010
292 #define HN_FLAG_NO_SLEEPING		0x0020
293 #define HN_FLAG_RXBUF_REF		0x0040
294 #define HN_FLAG_CHIM_REF		0x0080
295 #define HN_FLAG_RXVF			0x0100
296 
297 #define HN_FLAG_ERRORS			(HN_FLAG_RXBUF_REF | HN_FLAG_CHIM_REF)
298 
299 #define HN_XVFFLAG_ENABLED		0x0001
300 #define HN_XVFFLAG_ACCBPF		0x0002
301 
302 #define HN_NO_SLEEPING(sc)			\
303 do {						\
304 	(sc)->hn_flags |= HN_FLAG_NO_SLEEPING;	\
305 } while (0)
306 
307 #define HN_SLEEPING_OK(sc)			\
308 do {						\
309 	(sc)->hn_flags &= ~HN_FLAG_NO_SLEEPING;	\
310 } while (0)
311 
312 #define HN_CAN_SLEEP(sc)		\
313 	(((sc)->hn_flags & HN_FLAG_NO_SLEEPING) == 0)
314 
315 #define HN_CAP_VLAN			0x0001
316 #define HN_CAP_MTU			0x0002
317 #define HN_CAP_IPCS			0x0004
318 #define HN_CAP_TCP4CS			0x0008
319 #define HN_CAP_TCP6CS			0x0010
320 #define HN_CAP_UDP4CS			0x0020
321 #define HN_CAP_UDP6CS			0x0040
322 #define HN_CAP_TSO4			0x0080
323 #define HN_CAP_TSO6			0x0100
324 #define HN_CAP_HASHVAL			0x0200
325 #define HN_CAP_UDPHASH			0x0400
326 
327 /* Capability description for use with printf(9) %b identifier. */
328 #define HN_CAP_BITS				\
329 	"\020\1VLAN\2MTU\3IPCS\4TCP4CS\5TCP6CS"	\
330 	"\6UDP4CS\7UDP6CS\10TSO4\11TSO6\12HASHVAL\13UDPHASH"
331 
332 #define HN_LINK_FLAG_LINKUP		0x0001
333 #define HN_LINK_FLAG_NETCHG		0x0002
334 
335 #endif	/* !_IF_HNVAR_H_ */
336