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