xref: /freebsd/sys/dev/hyperv/netvsc/if_hnvar.h (revision e6bfd18d21b225af6a0ed67ceeaf1293b7b9eba5)
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 	if_t		hn_ifp;
75 	if_t		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 
106 	struct vmbus_channel *hn_chan;
107 } __aligned(CACHE_LINE_SIZE);
108 
109 #define HN_TRUST_HCSUM_IP	0x0001
110 #define HN_TRUST_HCSUM_TCP	0x0002
111 #define HN_TRUST_HCSUM_UDP	0x0004
112 
113 #define HN_RX_FLAG_ATTACHED	0x0001
114 #define HN_RX_FLAG_BR_REF	0x0002
115 #define HN_RX_FLAG_XPNT_VF	0x0004
116 #define HN_RX_FLAG_UDP_HASH	0x0008
117 
118 struct hn_tx_ring {
119 #ifndef HN_USE_TXDESC_BUFRING
120 	struct mtx	hn_txlist_spin;
121 	struct hn_txdesc_list hn_txlist;
122 #else
123 	struct buf_ring	*hn_txdesc_br;
124 #endif
125 	int		hn_txdesc_cnt;
126 	int		hn_txdesc_avail;
127 	u_short		hn_has_txeof;
128 	u_short		hn_txdone_cnt;
129 
130 	int		hn_sched_tx;
131 	void		(*hn_txeof)(struct hn_tx_ring *);
132 	struct taskqueue *hn_tx_taskq;
133 	struct task	hn_tx_task;
134 	struct task	hn_txeof_task;
135 
136 	struct buf_ring	*hn_mbuf_br;
137 	int		hn_oactive;
138 	int		hn_tx_idx;
139 	int		hn_tx_flags;
140 
141 	struct mtx	hn_tx_lock;
142 	struct hn_softc	*hn_sc;
143 	struct vmbus_channel *hn_chan;
144 
145 	int		hn_direct_tx_size;
146 	int		hn_chim_size;
147 	bus_dma_tag_t	hn_tx_data_dtag;
148 	uint64_t	hn_csum_assist;
149 
150 	/* Applied packet transmission aggregation limits. */
151 	int		hn_agg_szmax;
152 	short		hn_agg_pktmax;
153 	short		hn_agg_align;
154 
155 	/* Packet transmission aggregation states. */
156 	struct hn_txdesc *hn_agg_txd;
157 	int		hn_agg_szleft;
158 	short		hn_agg_pktleft;
159 	struct rndis_packet_msg *hn_agg_prevpkt;
160 
161 	/* Temporary stats for each sends. */
162 	int		hn_stat_size;
163 	short		hn_stat_pkts;
164 	short		hn_stat_mcasts;
165 
166 	int		(*hn_sendpkt)(struct hn_tx_ring *, struct hn_txdesc *);
167 	int		hn_suspended;
168 	int		hn_gpa_cnt;
169 	struct vmbus_gpa hn_gpa[HN_GPACNT_MAX];
170 
171 	u_long		hn_no_txdescs;
172 	u_long		hn_send_failed;
173 	u_long		hn_txdma_failed;
174 	u_long		hn_tx_collapsed;
175 	u_long		hn_tx_chimney_tried;
176 	u_long		hn_tx_chimney;
177 	u_long		hn_pkts;
178 	u_long		hn_sends;
179 	u_long		hn_flush_failed;
180 
181 	/* Rarely used stuffs */
182 	struct hn_txdesc *hn_txdesc;
183 	bus_dma_tag_t	hn_tx_rndis_dtag;
184 	struct sysctl_oid *hn_tx_sysctl_tree;
185 } __aligned(CACHE_LINE_SIZE);
186 
187 #define HN_TX_FLAG_ATTACHED	0x0001
188 #define HN_TX_FLAG_HASHVAL	0x0002	/* support HASHVAL pktinfo */
189 
190 /*
191  * Device-specific softc structure
192  */
193 struct hn_softc {
194 	if_t		hn_ifp;
195 	struct ifmedia	hn_media;
196 	device_t        hn_dev;
197 	int             hn_if_flags;
198 	struct sx	hn_lock;
199 	struct vmbus_channel *hn_prichan;
200 
201 	int		hn_rx_ring_cnt;
202 	int		hn_rx_ring_inuse;
203 	struct hn_rx_ring *hn_rx_ring;
204 
205 	struct rmlock	hn_vf_lock;
206 	if_t		hn_vf_ifp;	/* SR-IOV VF */
207 	uint32_t	hn_xvf_flags;	/* transparent VF flags */
208 
209 	int		hn_tx_ring_cnt;
210 	int		hn_tx_ring_inuse;
211 	struct hn_tx_ring *hn_tx_ring;
212 
213 	uint8_t		*hn_chim;
214 	u_long		*hn_chim_bmap;
215 	int		hn_chim_bmap_cnt;
216 	int		hn_chim_cnt;
217 	int		hn_chim_szmax;
218 
219 	int		hn_cpu;
220 	struct taskqueue **hn_tx_taskqs;
221 	struct sysctl_oid *hn_tx_sysctl_tree;
222 	struct sysctl_oid *hn_rx_sysctl_tree;
223 	struct vmbus_xact_ctx *hn_xact;
224 	uint32_t	hn_nvs_ver;
225 	uint32_t	hn_rx_filter;
226 
227 	/* Packet transmission aggregation user settings. */
228 	int			hn_agg_size;
229 	int			hn_agg_pkts;
230 
231 	struct taskqueue	*hn_mgmt_taskq;
232 	struct taskqueue	*hn_mgmt_taskq0;
233 	struct task		hn_link_task;
234 	struct task		hn_netchg_init;
235 	struct timeout_task	hn_netchg_status;
236 	uint32_t		hn_link_flags;	/* HN_LINK_FLAG_ */
237 
238 	uint32_t		hn_caps;	/* HN_CAP_ */
239 	uint32_t		hn_flags;	/* HN_FLAG_ */
240 	u_int			hn_pollhz;
241 
242 	void			*hn_rxbuf;
243 	uint32_t		hn_rxbuf_gpadl;
244 
245 	uint32_t		hn_chim_gpadl;
246 
247 	uint32_t		hn_rndis_rid;
248 	uint32_t		hn_ndis_ver;
249 	int			hn_ndis_tso_szmax;
250 	int			hn_ndis_tso_sgmin;
251 	uint32_t		hn_rndis_agg_size;
252 	uint32_t		hn_rndis_agg_pkts;
253 	uint32_t		hn_rndis_agg_align;
254 
255 	int			hn_rss_ind_size;
256 	uint32_t		hn_rss_hash;	/* setting, NDIS_HASH_ */
257 	uint32_t		hn_rss_hcap;	/* caps, NDIS_HASH_ */
258 	struct ndis_rssprm_toeplitz hn_rss;
259 
260 	eventhandler_tag	hn_ifaddr_evthand;
261 	eventhandler_tag	hn_ifnet_evthand;
262 	eventhandler_tag	hn_ifnet_atthand;
263 	eventhandler_tag	hn_ifnet_dethand;
264 	eventhandler_tag	hn_ifnet_lnkhand;
265 
266 	/*
267 	 * Transparent VF delayed initialization.
268 	 */
269 	int			hn_vf_rdytick;	/* ticks, 0 == ready */
270 	struct taskqueue	*hn_vf_taskq;
271 	struct timeout_task	hn_vf_init;
272 
273 	/*
274 	 * Saved information for VF under transparent mode.
275 	 */
276 	void			(*hn_vf_input)
277 				(if_t, struct mbuf *);
278 	int			hn_saved_caps;
279 	u_int			hn_saved_tsomax;
280 	u_int			hn_saved_tsosegcnt;
281 	u_int			hn_saved_tsosegsz;
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