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