xref: /freebsd-13-stable/sys/dev/hyperv/netvsc/if_hnvar.h (revision f8167e0404dab9ffeaca95853dd237ab7c587f82)
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 	struct ifnet	*hn_ifp;
73 	struct ifnet	*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 	struct hyperv_dma hn_br_dma;
104 
105 	struct vmbus_channel *hn_chan;
106 } __aligned(CACHE_LINE_SIZE);
107 
108 #define HN_TRUST_HCSUM_IP	0x0001
109 #define HN_TRUST_HCSUM_TCP	0x0002
110 #define HN_TRUST_HCSUM_UDP	0x0004
111 
112 #define HN_RX_FLAG_ATTACHED	0x0001
113 #define HN_RX_FLAG_BR_REF	0x0002
114 #define HN_RX_FLAG_XPNT_VF	0x0004
115 #define HN_RX_FLAG_UDP_HASH	0x0008
116 
117 struct hn_tx_ring {
118 #ifndef HN_USE_TXDESC_BUFRING
119 	struct mtx	hn_txlist_spin;
120 	struct hn_txdesc_list hn_txlist;
121 #else
122 	struct buf_ring	*hn_txdesc_br;
123 #endif
124 	int		hn_txdesc_cnt;
125 	int		hn_txdesc_avail;
126 	u_short		hn_has_txeof;
127 	u_short		hn_txdone_cnt;
128 
129 	int		hn_sched_tx;
130 	void		(*hn_txeof)(struct hn_tx_ring *);
131 	struct taskqueue *hn_tx_taskq;
132 	struct task	hn_tx_task;
133 	struct task	hn_txeof_task;
134 
135 	struct buf_ring	*hn_mbuf_br;
136 	int		hn_oactive;
137 	int		hn_tx_idx;
138 	int		hn_tx_flags;
139 
140 	struct mtx	hn_tx_lock;
141 	struct hn_softc	*hn_sc;
142 	struct vmbus_channel *hn_chan;
143 
144 	int		hn_direct_tx_size;
145 	int		hn_chim_size;
146 	bus_dma_tag_t	hn_tx_data_dtag;
147 	uint64_t	hn_csum_assist;
148 
149 	/* Applied packet transmission aggregation limits. */
150 	int		hn_agg_szmax;
151 	short		hn_agg_pktmax;
152 	short		hn_agg_align;
153 
154 	/* Packet transmission aggregation states. */
155 	struct hn_txdesc *hn_agg_txd;
156 	int		hn_agg_szleft;
157 	short		hn_agg_pktleft;
158 	struct rndis_packet_msg *hn_agg_prevpkt;
159 
160 	/* Temporary stats for each sends. */
161 	int		hn_stat_size;
162 	short		hn_stat_pkts;
163 	short		hn_stat_mcasts;
164 
165 	int		(*hn_sendpkt)(struct hn_tx_ring *, struct hn_txdesc *);
166 	int		hn_suspended;
167 	int		hn_gpa_cnt;
168 	struct vmbus_gpa hn_gpa[HN_GPACNT_MAX];
169 
170 	u_long		hn_no_txdescs;
171 	u_long		hn_send_failed;
172 	u_long		hn_txdma_failed;
173 	u_long		hn_tx_collapsed;
174 	u_long		hn_tx_chimney_tried;
175 	u_long		hn_tx_chimney;
176 	u_long		hn_pkts;
177 	u_long		hn_sends;
178 	u_long		hn_flush_failed;
179 
180 	/* Rarely used stuffs */
181 	struct hn_txdesc *hn_txdesc;
182 	bus_dma_tag_t	hn_tx_rndis_dtag;
183 	struct sysctl_oid *hn_tx_sysctl_tree;
184 } __aligned(CACHE_LINE_SIZE);
185 
186 #define HN_TX_FLAG_ATTACHED	0x0001
187 #define HN_TX_FLAG_HASHVAL	0x0002	/* support HASHVAL pktinfo */
188 
189 /*
190  * Device-specific softc structure
191  */
192 struct hn_softc {
193 	struct ifnet    *hn_ifp;
194 	struct ifmedia	hn_media;
195 	device_t        hn_dev;
196 	int             hn_if_flags;
197 	struct sx	hn_lock;
198 	struct vmbus_channel *hn_prichan;
199 
200 	int		hn_rx_ring_cnt;
201 	int		hn_rx_ring_inuse;
202 	struct hn_rx_ring *hn_rx_ring;
203 
204 	struct rmlock	hn_vf_lock;
205 	struct ifnet	*hn_vf_ifp;	/* SR-IOV VF */
206 	uint32_t	hn_xvf_flags;	/* transparent VF flags */
207 
208 	int		hn_tx_ring_cnt;
209 	int		hn_tx_ring_inuse;
210 	struct hn_tx_ring *hn_tx_ring;
211 
212 	uint8_t		*hn_chim;
213 	u_long		*hn_chim_bmap;
214 	int		hn_chim_bmap_cnt;
215 	int		hn_chim_cnt;
216 	int		hn_chim_szmax;
217 
218 	int		hn_cpu;
219 	struct taskqueue **hn_tx_taskqs;
220 	struct sysctl_oid *hn_tx_sysctl_tree;
221 	struct sysctl_oid *hn_rx_sysctl_tree;
222 	struct vmbus_xact_ctx *hn_xact;
223 	uint32_t	hn_nvs_ver;
224 	uint32_t	hn_rx_filter;
225 
226 	/* Packet transmission aggregation user settings. */
227 	int			hn_agg_size;
228 	int			hn_agg_pkts;
229 
230 	struct taskqueue	*hn_mgmt_taskq;
231 	struct taskqueue	*hn_mgmt_taskq0;
232 	struct task		hn_link_task;
233 	struct task		hn_netchg_init;
234 	struct timeout_task	hn_netchg_status;
235 	uint32_t		hn_link_flags;	/* HN_LINK_FLAG_ */
236 
237 	uint32_t		hn_caps;	/* HN_CAP_ */
238 	uint32_t		hn_flags;	/* HN_FLAG_ */
239 	u_int			hn_pollhz;
240 
241 	void			*hn_rxbuf;
242 	uint32_t		hn_rxbuf_gpadl;
243 	struct hyperv_dma	hn_rxbuf_dma;
244 
245 	uint32_t		hn_chim_gpadl;
246 	struct hyperv_dma	hn_chim_dma;
247 
248 	uint32_t		hn_rndis_rid;
249 	uint32_t		hn_ndis_ver;
250 	int			hn_ndis_tso_szmax;
251 	int			hn_ndis_tso_sgmin;
252 	uint32_t		hn_rndis_agg_size;
253 	uint32_t		hn_rndis_agg_pkts;
254 	uint32_t		hn_rndis_agg_align;
255 
256 	int			hn_rss_ind_size;
257 	uint32_t		hn_rss_hash;	/* setting, NDIS_HASH_ */
258 	uint32_t		hn_rss_hcap;	/* caps, NDIS_HASH_ */
259 	struct ndis_rssprm_toeplitz hn_rss;
260 
261 	eventhandler_tag	hn_ifaddr_evthand;
262 	eventhandler_tag	hn_ifnet_evthand;
263 	eventhandler_tag	hn_ifnet_atthand;
264 	eventhandler_tag	hn_ifnet_dethand;
265 	eventhandler_tag	hn_ifnet_lnkhand;
266 
267 	/*
268 	 * Transparent VF delayed initialization.
269 	 */
270 	int			hn_vf_rdytick;	/* ticks, 0 == ready */
271 	struct taskqueue	*hn_vf_taskq;
272 	struct timeout_task	hn_vf_init;
273 
274 	/*
275 	 * Saved information for VF under transparent mode.
276 	 */
277 	void			(*hn_vf_input)
278 				(struct ifnet *, struct mbuf *);
279 	int			hn_saved_caps;
280 	u_int			hn_saved_tsomax;
281 	u_int			hn_saved_tsosegcnt;
282 	u_int			hn_saved_tsosegsz;
283 };
284 
285 #define HN_FLAG_RXBUF_CONNECTED		0x0001
286 #define HN_FLAG_CHIM_CONNECTED		0x0002
287 #define HN_FLAG_HAS_RSSKEY		0x0004
288 #define HN_FLAG_HAS_RSSIND		0x0008
289 #define HN_FLAG_SYNTH_ATTACHED		0x0010
290 #define HN_FLAG_NO_SLEEPING		0x0020
291 #define HN_FLAG_RXBUF_REF		0x0040
292 #define HN_FLAG_CHIM_REF		0x0080
293 #define HN_FLAG_RXVF			0x0100
294 
295 #define HN_FLAG_ERRORS			(HN_FLAG_RXBUF_REF | HN_FLAG_CHIM_REF)
296 
297 #define HN_XVFFLAG_ENABLED		0x0001
298 #define HN_XVFFLAG_ACCBPF		0x0002
299 
300 #define HN_NO_SLEEPING(sc)			\
301 do {						\
302 	(sc)->hn_flags |= HN_FLAG_NO_SLEEPING;	\
303 } while (0)
304 
305 #define HN_SLEEPING_OK(sc)			\
306 do {						\
307 	(sc)->hn_flags &= ~HN_FLAG_NO_SLEEPING;	\
308 } while (0)
309 
310 #define HN_CAN_SLEEP(sc)		\
311 	(((sc)->hn_flags & HN_FLAG_NO_SLEEPING) == 0)
312 
313 #define HN_CAP_VLAN			0x0001
314 #define HN_CAP_MTU			0x0002
315 #define HN_CAP_IPCS			0x0004
316 #define HN_CAP_TCP4CS			0x0008
317 #define HN_CAP_TCP6CS			0x0010
318 #define HN_CAP_UDP4CS			0x0020
319 #define HN_CAP_UDP6CS			0x0040
320 #define HN_CAP_TSO4			0x0080
321 #define HN_CAP_TSO6			0x0100
322 #define HN_CAP_HASHVAL			0x0200
323 #define HN_CAP_UDPHASH			0x0400
324 
325 /* Capability description for use with printf(9) %b identifier. */
326 #define HN_CAP_BITS				\
327 	"\020\1VLAN\2MTU\3IPCS\4TCP4CS\5TCP6CS"	\
328 	"\6UDP4CS\7UDP6CS\10TSO4\11TSO6\12HASHVAL\13UDPHASH"
329 
330 #define HN_LINK_FLAG_LINKUP		0x0001
331 #define HN_LINK_FLAG_NETCHG		0x0002
332 
333 #endif	/* !_IF_HNVAR_H_ */
334