]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/dev/hyperv/netvsc/if_hn.c
MFC 323175
[FreeBSD/stable/10.git] / sys / dev / hyperv / netvsc / if_hn.c
1 /*-
2  * Copyright (c) 2010-2012 Citrix Inc.
3  * Copyright (c) 2009-2012,2016-2017 Microsoft Corp.
4  * Copyright (c) 2012 NetApp Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice unmodified, this list of conditions, and the following
12  *    disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 /*-
30  * Copyright (c) 2004-2006 Kip Macy
31  * All rights reserved.
32  *
33  * Redistribution and use in source and binary forms, with or without
34  * modification, are permitted provided that the following conditions
35  * are met:
36  * 1. Redistributions of source code must retain the above copyright
37  *    notice, this list of conditions and the following disclaimer.
38  * 2. Redistributions in binary form must reproduce the above copyright
39  *    notice, this list of conditions and the following disclaimer in the
40  *    documentation and/or other materials provided with the distribution.
41  *
42  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
43  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
46  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52  * SUCH DAMAGE.
53  */
54
55 #include <sys/cdefs.h>
56 __FBSDID("$FreeBSD$");
57
58 #include "opt_inet6.h"
59 #include "opt_inet.h"
60 #include "opt_hn.h"
61
62 #include <sys/param.h>
63 #include <sys/bus.h>
64 #include <sys/kernel.h>
65 #include <sys/limits.h>
66 #include <sys/malloc.h>
67 #include <sys/mbuf.h>
68 #include <sys/module.h>
69 #include <sys/proc.h>
70 #include <sys/queue.h>
71 #include <sys/lock.h>
72 #include <sys/rmlock.h>
73 #include <sys/sbuf.h>
74 #include <sys/smp.h>
75 #include <sys/socket.h>
76 #include <sys/sockio.h>
77 #include <sys/sx.h>
78 #include <sys/sysctl.h>
79 #include <sys/systm.h>
80 #include <sys/taskqueue.h>
81 #include <sys/buf_ring.h>
82 #include <sys/eventhandler.h>
83
84 #include <machine/atomic.h>
85 #include <machine/in_cksum.h>
86
87 #include <net/bpf.h>
88 #include <net/ethernet.h>
89 #include <net/if.h>
90 #include <net/if_arp.h>
91 #include <net/if_dl.h>
92 #include <net/if_media.h>
93 #include <net/if_types.h>
94 #include <net/if_var.h>
95 #include <net/if_vlan_var.h>
96 #include <net/rndis.h>
97
98 #include <netinet/in_systm.h>
99 #include <netinet/in.h>
100 #include <netinet/ip.h>
101 #include <netinet/ip6.h>
102 #include <netinet/tcp.h>
103 #include <netinet/tcp_lro.h>
104 #include <netinet/udp.h>
105
106 #include <dev/hyperv/include/hyperv.h>
107 #include <dev/hyperv/include/hyperv_busdma.h>
108 #include <dev/hyperv/include/vmbus.h>
109 #include <dev/hyperv/include/vmbus_xact.h>
110
111 #include <dev/hyperv/netvsc/ndis.h>
112 #include <dev/hyperv/netvsc/if_hnreg.h>
113 #include <dev/hyperv/netvsc/if_hnvar.h>
114 #include <dev/hyperv/netvsc/hn_nvs.h>
115 #include <dev/hyperv/netvsc/hn_rndis.h>
116
117 #include "vmbus_if.h"
118
119 #define HN_IFSTART_SUPPORT
120
121 #define HN_RING_CNT_DEF_MAX             8
122
123 #define HN_VFMAP_SIZE_DEF               8
124
125 #define HN_XPNT_VF_ATTWAIT_MIN          2       /* seconds */
126
127 /* YYY should get it from the underlying channel */
128 #define HN_TX_DESC_CNT                  512
129
130 #define HN_RNDIS_PKT_LEN                                        \
131         (sizeof(struct rndis_packet_msg) +                      \
132          HN_RNDIS_PKTINFO_SIZE(HN_NDIS_HASH_VALUE_SIZE) +       \
133          HN_RNDIS_PKTINFO_SIZE(NDIS_VLAN_INFO_SIZE) +           \
134          HN_RNDIS_PKTINFO_SIZE(NDIS_LSO2_INFO_SIZE) +           \
135          HN_RNDIS_PKTINFO_SIZE(NDIS_TXCSUM_INFO_SIZE))
136 #define HN_RNDIS_PKT_BOUNDARY           PAGE_SIZE
137 #define HN_RNDIS_PKT_ALIGN              CACHE_LINE_SIZE
138
139 #define HN_TX_DATA_BOUNDARY             PAGE_SIZE
140 #define HN_TX_DATA_MAXSIZE              IP_MAXPACKET
141 #define HN_TX_DATA_SEGSIZE              PAGE_SIZE
142 /* -1 for RNDIS packet message */
143 #define HN_TX_DATA_SEGCNT_MAX           (HN_GPACNT_MAX - 1)
144
145 #define HN_DIRECT_TX_SIZE_DEF           128
146
147 #define HN_EARLY_TXEOF_THRESH           8
148
149 #define HN_PKTBUF_LEN_DEF               (16 * 1024)
150
151 #define HN_LROENT_CNT_DEF               128
152
153 #define HN_LRO_LENLIM_MULTIRX_DEF       (12 * ETHERMTU)
154 #define HN_LRO_LENLIM_DEF               (25 * ETHERMTU)
155 /* YYY 2*MTU is a bit rough, but should be good enough. */
156 #define HN_LRO_LENLIM_MIN(ifp)          (2 * (ifp)->if_mtu)
157
158 #define HN_LRO_ACKCNT_DEF               1
159
160 #define HN_LOCK_INIT(sc)                \
161         sx_init(&(sc)->hn_lock, device_get_nameunit((sc)->hn_dev))
162 #define HN_LOCK_DESTROY(sc)             sx_destroy(&(sc)->hn_lock)
163 #define HN_LOCK_ASSERT(sc)              sx_assert(&(sc)->hn_lock, SA_XLOCKED)
164 #define HN_LOCK(sc)                                     \
165 do {                                                    \
166         while (sx_try_xlock(&(sc)->hn_lock) == 0)       \
167                 DELAY(1000);                            \
168 } while (0)
169 #define HN_UNLOCK(sc)                   sx_xunlock(&(sc)->hn_lock)
170
171 #define HN_CSUM_IP_MASK                 (CSUM_IP | CSUM_IP_TCP | CSUM_IP_UDP)
172 #define HN_CSUM_IP6_MASK                (CSUM_IP6_TCP | CSUM_IP6_UDP)
173 #define HN_CSUM_IP_HWASSIST(sc)         \
174         ((sc)->hn_tx_ring[0].hn_csum_assist & HN_CSUM_IP_MASK)
175 #define HN_CSUM_IP6_HWASSIST(sc)        \
176         ((sc)->hn_tx_ring[0].hn_csum_assist & HN_CSUM_IP6_MASK)
177
178 #define HN_PKTSIZE_MIN(align)           \
179         roundup2(ETHER_MIN_LEN + ETHER_VLAN_ENCAP_LEN - ETHER_CRC_LEN + \
180             HN_RNDIS_PKT_LEN, (align))
181 #define HN_PKTSIZE(m, align)            \
182         roundup2((m)->m_pkthdr.len + HN_RNDIS_PKT_LEN, (align))
183
184 #define HN_RING_IDX2CPU(sc, idx)        (((sc)->hn_cpu + (idx)) % mp_ncpus)
185
186 struct hn_txdesc {
187 #ifndef HN_USE_TXDESC_BUFRING
188         SLIST_ENTRY(hn_txdesc)          link;
189 #endif
190         STAILQ_ENTRY(hn_txdesc)         agg_link;
191
192         /* Aggregated txdescs, in sending order. */
193         STAILQ_HEAD(, hn_txdesc)        agg_list;
194
195         /* The oldest packet, if transmission aggregation happens. */
196         struct mbuf                     *m;
197         struct hn_tx_ring               *txr;
198         int                             refs;
199         uint32_t                        flags;  /* HN_TXD_FLAG_ */
200         struct hn_nvs_sendctx           send_ctx;
201         uint32_t                        chim_index;
202         int                             chim_size;
203
204         bus_dmamap_t                    data_dmap;
205
206         bus_addr_t                      rndis_pkt_paddr;
207         struct rndis_packet_msg         *rndis_pkt;
208         bus_dmamap_t                    rndis_pkt_dmap;
209 };
210
211 #define HN_TXD_FLAG_ONLIST              0x0001
212 #define HN_TXD_FLAG_DMAMAP              0x0002
213 #define HN_TXD_FLAG_ONAGG               0x0004
214
215 struct hn_rxinfo {
216         uint32_t                        vlan_info;
217         uint32_t                        csum_info;
218         uint32_t                        hash_info;
219         uint32_t                        hash_value;
220 };
221
222 struct hn_rxvf_setarg {
223         struct hn_rx_ring       *rxr;
224         struct ifnet            *vf_ifp;
225 };
226
227 #define HN_RXINFO_VLAN                  0x0001
228 #define HN_RXINFO_CSUM                  0x0002
229 #define HN_RXINFO_HASHINF               0x0004
230 #define HN_RXINFO_HASHVAL               0x0008
231 #define HN_RXINFO_ALL                   \
232         (HN_RXINFO_VLAN |               \
233          HN_RXINFO_CSUM |               \
234          HN_RXINFO_HASHINF |            \
235          HN_RXINFO_HASHVAL)
236
237 #define HN_NDIS_VLAN_INFO_INVALID       0xffffffff
238 #define HN_NDIS_RXCSUM_INFO_INVALID     0
239 #define HN_NDIS_HASH_INFO_INVALID       0
240
241 static int                      hn_probe(device_t);
242 static int                      hn_attach(device_t);
243 static int                      hn_detach(device_t);
244 static int                      hn_shutdown(device_t);
245 static void                     hn_chan_callback(struct vmbus_channel *,
246                                     void *);
247
248 static void                     hn_init(void *);
249 static int                      hn_ioctl(struct ifnet *, u_long, caddr_t);
250 #ifdef HN_IFSTART_SUPPORT
251 static void                     hn_start(struct ifnet *);
252 #endif
253 static int                      hn_transmit(struct ifnet *, struct mbuf *);
254 static void                     hn_xmit_qflush(struct ifnet *);
255 static int                      hn_ifmedia_upd(struct ifnet *);
256 static void                     hn_ifmedia_sts(struct ifnet *,
257                                     struct ifmediareq *);
258
259 static void                     hn_ifnet_event(void *, struct ifnet *, int);
260 static void                     hn_ifaddr_event(void *, struct ifnet *);
261 static void                     hn_ifnet_attevent(void *, struct ifnet *);
262 static void                     hn_ifnet_detevent(void *, struct ifnet *);
263 static void                     hn_ifnet_lnkevent(void *, struct ifnet *, int);
264
265 static bool                     hn_ismyvf(const struct hn_softc *,
266                                     const struct ifnet *);
267 static void                     hn_rxvf_change(struct hn_softc *,
268                                     struct ifnet *, bool);
269 static void                     hn_rxvf_set(struct hn_softc *, struct ifnet *);
270 static void                     hn_rxvf_set_task(void *, int);
271 static void                     hn_xpnt_vf_input(struct ifnet *, struct mbuf *);
272 static int                      hn_xpnt_vf_iocsetflags(struct hn_softc *);
273 static int                      hn_xpnt_vf_iocsetcaps(struct hn_softc *,
274                                     struct ifreq *);
275 static void                     hn_xpnt_vf_saveifflags(struct hn_softc *);
276 static bool                     hn_xpnt_vf_isready(struct hn_softc *);
277 static void                     hn_xpnt_vf_setready(struct hn_softc *);
278 static void                     hn_xpnt_vf_init_taskfunc(void *, int);
279 static void                     hn_xpnt_vf_init(struct hn_softc *);
280 static void                     hn_xpnt_vf_setenable(struct hn_softc *);
281 static void                     hn_xpnt_vf_setdisable(struct hn_softc *, bool);
282
283 static int                      hn_rndis_rxinfo(const void *, int,
284                                     struct hn_rxinfo *);
285 static void                     hn_rndis_rx_data(struct hn_rx_ring *,
286                                     const void *, int);
287 static void                     hn_rndis_rx_status(struct hn_softc *,
288                                     const void *, int);
289 static void                     hn_rndis_init_fixat(struct hn_softc *, int);
290
291 static void                     hn_nvs_handle_notify(struct hn_softc *,
292                                     const struct vmbus_chanpkt_hdr *);
293 static void                     hn_nvs_handle_comp(struct hn_softc *,
294                                     struct vmbus_channel *,
295                                     const struct vmbus_chanpkt_hdr *);
296 static void                     hn_nvs_handle_rxbuf(struct hn_rx_ring *,
297                                     struct vmbus_channel *,
298                                     const struct vmbus_chanpkt_hdr *);
299 static void                     hn_nvs_ack_rxbuf(struct hn_rx_ring *,
300                                     struct vmbus_channel *, uint64_t);
301
302 #if __FreeBSD_version >= 1100099
303 static int                      hn_lro_lenlim_sysctl(SYSCTL_HANDLER_ARGS);
304 static int                      hn_lro_ackcnt_sysctl(SYSCTL_HANDLER_ARGS);
305 #endif
306 static int                      hn_trust_hcsum_sysctl(SYSCTL_HANDLER_ARGS);
307 static int                      hn_chim_size_sysctl(SYSCTL_HANDLER_ARGS);
308 #if __FreeBSD_version < 1100095
309 static int                      hn_rx_stat_int_sysctl(SYSCTL_HANDLER_ARGS);
310 #else
311 static int                      hn_rx_stat_u64_sysctl(SYSCTL_HANDLER_ARGS);
312 #endif
313 static int                      hn_rx_stat_ulong_sysctl(SYSCTL_HANDLER_ARGS);
314 static int                      hn_tx_stat_ulong_sysctl(SYSCTL_HANDLER_ARGS);
315 static int                      hn_tx_conf_int_sysctl(SYSCTL_HANDLER_ARGS);
316 static int                      hn_ndis_version_sysctl(SYSCTL_HANDLER_ARGS);
317 static int                      hn_caps_sysctl(SYSCTL_HANDLER_ARGS);
318 static int                      hn_hwassist_sysctl(SYSCTL_HANDLER_ARGS);
319 static int                      hn_rxfilter_sysctl(SYSCTL_HANDLER_ARGS);
320 static int                      hn_rss_key_sysctl(SYSCTL_HANDLER_ARGS);
321 static int                      hn_rss_ind_sysctl(SYSCTL_HANDLER_ARGS);
322 static int                      hn_rss_hash_sysctl(SYSCTL_HANDLER_ARGS);
323 static int                      hn_txagg_size_sysctl(SYSCTL_HANDLER_ARGS);
324 static int                      hn_txagg_pkts_sysctl(SYSCTL_HANDLER_ARGS);
325 static int                      hn_txagg_pktmax_sysctl(SYSCTL_HANDLER_ARGS);
326 static int                      hn_txagg_align_sysctl(SYSCTL_HANDLER_ARGS);
327 static int                      hn_polling_sysctl(SYSCTL_HANDLER_ARGS);
328 static int                      hn_vf_sysctl(SYSCTL_HANDLER_ARGS);
329 static int                      hn_rxvf_sysctl(SYSCTL_HANDLER_ARGS);
330 static int                      hn_vflist_sysctl(SYSCTL_HANDLER_ARGS);
331 static int                      hn_vfmap_sysctl(SYSCTL_HANDLER_ARGS);
332 static int                      hn_xpnt_vf_accbpf_sysctl(SYSCTL_HANDLER_ARGS);
333 static int                      hn_xpnt_vf_enabled_sysctl(SYSCTL_HANDLER_ARGS);
334
335 static void                     hn_stop(struct hn_softc *, bool);
336 static void                     hn_init_locked(struct hn_softc *);
337 static int                      hn_chan_attach(struct hn_softc *,
338                                     struct vmbus_channel *);
339 static void                     hn_chan_detach(struct hn_softc *,
340                                     struct vmbus_channel *);
341 static int                      hn_attach_subchans(struct hn_softc *);
342 static void                     hn_detach_allchans(struct hn_softc *);
343 static void                     hn_chan_rollup(struct hn_rx_ring *,
344                                     struct hn_tx_ring *);
345 static void                     hn_set_ring_inuse(struct hn_softc *, int);
346 static int                      hn_synth_attach(struct hn_softc *, int);
347 static void                     hn_synth_detach(struct hn_softc *);
348 static int                      hn_synth_alloc_subchans(struct hn_softc *,
349                                     int *);
350 static bool                     hn_synth_attachable(const struct hn_softc *);
351 static void                     hn_suspend(struct hn_softc *);
352 static void                     hn_suspend_data(struct hn_softc *);
353 static void                     hn_suspend_mgmt(struct hn_softc *);
354 static void                     hn_resume(struct hn_softc *);
355 static void                     hn_resume_data(struct hn_softc *);
356 static void                     hn_resume_mgmt(struct hn_softc *);
357 static void                     hn_suspend_mgmt_taskfunc(void *, int);
358 static void                     hn_chan_drain(struct hn_softc *,
359                                     struct vmbus_channel *);
360 static void                     hn_disable_rx(struct hn_softc *);
361 static void                     hn_drain_rxtx(struct hn_softc *, int);
362 static void                     hn_polling(struct hn_softc *, u_int);
363 static void                     hn_chan_polling(struct vmbus_channel *, u_int);
364 static void                     hn_mtu_change_fixup(struct hn_softc *);
365
366 static void                     hn_update_link_status(struct hn_softc *);
367 static void                     hn_change_network(struct hn_softc *);
368 static void                     hn_link_taskfunc(void *, int);
369 static void                     hn_netchg_init_taskfunc(void *, int);
370 static void                     hn_netchg_status_taskfunc(void *, int);
371 static void                     hn_link_status(struct hn_softc *);
372
373 static int                      hn_create_rx_data(struct hn_softc *, int);
374 static void                     hn_destroy_rx_data(struct hn_softc *);
375 static int                      hn_check_iplen(const struct mbuf *, int);
376 static int                      hn_set_rxfilter(struct hn_softc *, uint32_t);
377 static int                      hn_rxfilter_config(struct hn_softc *);
378 static int                      hn_rss_reconfig(struct hn_softc *);
379 static void                     hn_rss_ind_fixup(struct hn_softc *);
380 static int                      hn_rxpkt(struct hn_rx_ring *, const void *,
381                                     int, const struct hn_rxinfo *);
382
383 static int                      hn_tx_ring_create(struct hn_softc *, int);
384 static void                     hn_tx_ring_destroy(struct hn_tx_ring *);
385 static int                      hn_create_tx_data(struct hn_softc *, int);
386 static void                     hn_fixup_tx_data(struct hn_softc *);
387 static void                     hn_destroy_tx_data(struct hn_softc *);
388 static void                     hn_txdesc_dmamap_destroy(struct hn_txdesc *);
389 static void                     hn_txdesc_gc(struct hn_tx_ring *,
390                                     struct hn_txdesc *);
391 static int                      hn_encap(struct ifnet *, struct hn_tx_ring *,
392                                     struct hn_txdesc *, struct mbuf **);
393 static int                      hn_txpkt(struct ifnet *, struct hn_tx_ring *,
394                                     struct hn_txdesc *);
395 static void                     hn_set_chim_size(struct hn_softc *, int);
396 static void                     hn_set_tso_maxsize(struct hn_softc *, int, int);
397 static bool                     hn_tx_ring_pending(struct hn_tx_ring *);
398 static void                     hn_tx_ring_qflush(struct hn_tx_ring *);
399 static void                     hn_resume_tx(struct hn_softc *, int);
400 static void                     hn_set_txagg(struct hn_softc *);
401 static void                     *hn_try_txagg(struct ifnet *,
402                                     struct hn_tx_ring *, struct hn_txdesc *,
403                                     int);
404 static int                      hn_get_txswq_depth(const struct hn_tx_ring *);
405 static void                     hn_txpkt_done(struct hn_nvs_sendctx *,
406                                     struct hn_softc *, struct vmbus_channel *,
407                                     const void *, int);
408 static int                      hn_txpkt_sglist(struct hn_tx_ring *,
409                                     struct hn_txdesc *);
410 static int                      hn_txpkt_chim(struct hn_tx_ring *,
411                                     struct hn_txdesc *);
412 static int                      hn_xmit(struct hn_tx_ring *, int);
413 static void                     hn_xmit_taskfunc(void *, int);
414 static void                     hn_xmit_txeof(struct hn_tx_ring *);
415 static void                     hn_xmit_txeof_taskfunc(void *, int);
416 #ifdef HN_IFSTART_SUPPORT
417 static int                      hn_start_locked(struct hn_tx_ring *, int);
418 static void                     hn_start_taskfunc(void *, int);
419 static void                     hn_start_txeof(struct hn_tx_ring *);
420 static void                     hn_start_txeof_taskfunc(void *, int);
421 #endif
422
423 SYSCTL_NODE(_hw, OID_AUTO, hn, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
424     "Hyper-V network interface");
425
426 /* Trust tcp segements verification on host side. */
427 static int                      hn_trust_hosttcp = 1;
428 SYSCTL_INT(_hw_hn, OID_AUTO, trust_hosttcp, CTLFLAG_RDTUN,
429     &hn_trust_hosttcp, 0,
430     "Trust tcp segement verification on host side, "
431     "when csum info is missing (global setting)");
432
433 /* Trust udp datagrams verification on host side. */
434 static int                      hn_trust_hostudp = 1;
435 SYSCTL_INT(_hw_hn, OID_AUTO, trust_hostudp, CTLFLAG_RDTUN,
436     &hn_trust_hostudp, 0,
437     "Trust udp datagram verification on host side, "
438     "when csum info is missing (global setting)");
439
440 /* Trust ip packets verification on host side. */
441 static int                      hn_trust_hostip = 1;
442 SYSCTL_INT(_hw_hn, OID_AUTO, trust_hostip, CTLFLAG_RDTUN,
443     &hn_trust_hostip, 0,
444     "Trust ip packet verification on host side, "
445     "when csum info is missing (global setting)");
446
447 /* Limit TSO burst size */
448 static int                      hn_tso_maxlen = IP_MAXPACKET;
449 SYSCTL_INT(_hw_hn, OID_AUTO, tso_maxlen, CTLFLAG_RDTUN,
450     &hn_tso_maxlen, 0, "TSO burst limit");
451
452 /* Limit chimney send size */
453 static int                      hn_tx_chimney_size = 0;
454 SYSCTL_INT(_hw_hn, OID_AUTO, tx_chimney_size, CTLFLAG_RDTUN,
455     &hn_tx_chimney_size, 0, "Chimney send packet size limit");
456
457 /* Limit the size of packet for direct transmission */
458 static int                      hn_direct_tx_size = HN_DIRECT_TX_SIZE_DEF;
459 SYSCTL_INT(_hw_hn, OID_AUTO, direct_tx_size, CTLFLAG_RDTUN,
460     &hn_direct_tx_size, 0, "Size of the packet for direct transmission");
461
462 /* # of LRO entries per RX ring */
463 #if defined(INET) || defined(INET6)
464 #if __FreeBSD_version >= 1100095
465 static int                      hn_lro_entry_count = HN_LROENT_CNT_DEF;
466 SYSCTL_INT(_hw_hn, OID_AUTO, lro_entry_count, CTLFLAG_RDTUN,
467     &hn_lro_entry_count, 0, "LRO entry count");
468 #endif
469 #endif
470
471 static int                      hn_tx_taskq_cnt = 1;
472 SYSCTL_INT(_hw_hn, OID_AUTO, tx_taskq_cnt, CTLFLAG_RDTUN,
473     &hn_tx_taskq_cnt, 0, "# of TX taskqueues");
474
475 #define HN_TX_TASKQ_M_INDEP     0
476 #define HN_TX_TASKQ_M_GLOBAL    1
477 #define HN_TX_TASKQ_M_EVTTQ     2
478
479 static int                      hn_tx_taskq_mode = HN_TX_TASKQ_M_INDEP;
480 SYSCTL_INT(_hw_hn, OID_AUTO, tx_taskq_mode, CTLFLAG_RDTUN,
481     &hn_tx_taskq_mode, 0, "TX taskqueue modes: "
482     "0 - independent, 1 - share global tx taskqs, 2 - share event taskqs");
483
484 #ifndef HN_USE_TXDESC_BUFRING
485 static int                      hn_use_txdesc_bufring = 0;
486 #else
487 static int                      hn_use_txdesc_bufring = 1;
488 #endif
489 SYSCTL_INT(_hw_hn, OID_AUTO, use_txdesc_bufring, CTLFLAG_RD,
490     &hn_use_txdesc_bufring, 0, "Use buf_ring for TX descriptors");
491
492 #ifdef HN_IFSTART_SUPPORT
493 /* Use ifnet.if_start instead of ifnet.if_transmit */
494 static int                      hn_use_if_start = 0;
495 SYSCTL_INT(_hw_hn, OID_AUTO, use_if_start, CTLFLAG_RDTUN,
496     &hn_use_if_start, 0, "Use if_start TX method");
497 #endif
498
499 /* # of channels to use */
500 static int                      hn_chan_cnt = 0;
501 SYSCTL_INT(_hw_hn, OID_AUTO, chan_cnt, CTLFLAG_RDTUN,
502     &hn_chan_cnt, 0,
503     "# of channels to use; each channel has one RX ring and one TX ring");
504
505 /* # of transmit rings to use */
506 static int                      hn_tx_ring_cnt = 0;
507 SYSCTL_INT(_hw_hn, OID_AUTO, tx_ring_cnt, CTLFLAG_RDTUN,
508     &hn_tx_ring_cnt, 0, "# of TX rings to use");
509
510 /* Software TX ring deptch */
511 static int                      hn_tx_swq_depth = 0;
512 SYSCTL_INT(_hw_hn, OID_AUTO, tx_swq_depth, CTLFLAG_RDTUN,
513     &hn_tx_swq_depth, 0, "Depth of IFQ or BUFRING");
514
515 /* Enable sorted LRO, and the depth of the per-channel mbuf queue */
516 #if __FreeBSD_version >= 1100095
517 static u_int                    hn_lro_mbufq_depth = 0;
518 SYSCTL_UINT(_hw_hn, OID_AUTO, lro_mbufq_depth, CTLFLAG_RDTUN,
519     &hn_lro_mbufq_depth, 0, "Depth of LRO mbuf queue");
520 #endif
521
522 /* Packet transmission aggregation size limit */
523 static int                      hn_tx_agg_size = -1;
524 SYSCTL_INT(_hw_hn, OID_AUTO, tx_agg_size, CTLFLAG_RDTUN,
525     &hn_tx_agg_size, 0, "Packet transmission aggregation size limit");
526
527 /* Packet transmission aggregation count limit */
528 static int                      hn_tx_agg_pkts = -1;
529 SYSCTL_INT(_hw_hn, OID_AUTO, tx_agg_pkts, CTLFLAG_RDTUN,
530     &hn_tx_agg_pkts, 0, "Packet transmission aggregation packet limit");
531
532 /* VF list */
533 SYSCTL_PROC(_hw_hn, OID_AUTO, vflist, CTLFLAG_RD | CTLTYPE_STRING,
534     0, 0, hn_vflist_sysctl, "A", "VF list");
535
536 /* VF mapping */
537 SYSCTL_PROC(_hw_hn, OID_AUTO, vfmap, CTLFLAG_RD | CTLTYPE_STRING,
538     0, 0, hn_vfmap_sysctl, "A", "VF mapping");
539
540 /* Transparent VF */
541 static int                      hn_xpnt_vf = 0;
542 SYSCTL_INT(_hw_hn, OID_AUTO, vf_transparent, CTLFLAG_RDTUN,
543     &hn_xpnt_vf, 0, "Transparent VF mod");
544
545 /* Accurate BPF support for Transparent VF */
546 static int                      hn_xpnt_vf_accbpf = 0;
547 SYSCTL_INT(_hw_hn, OID_AUTO, vf_xpnt_accbpf, CTLFLAG_RDTUN,
548     &hn_xpnt_vf_accbpf, 0, "Accurate BPF for transparent VF");
549
550 /* Extra wait for transparent VF attach routing; unit seconds. */
551 static int                      hn_xpnt_vf_attwait = HN_XPNT_VF_ATTWAIT_MIN;
552 SYSCTL_INT(_hw_hn, OID_AUTO, vf_xpnt_attwait, CTLFLAG_RWTUN,
553     &hn_xpnt_vf_attwait, 0,
554     "Extra wait for transparent VF attach routing; unit: seconds");
555
556 static u_int                    hn_cpu_index;   /* next CPU for channel */
557 static struct taskqueue         **hn_tx_taskque;/* shared TX taskqueues */
558
559 static struct rmlock            hn_vfmap_lock;
560 static int                      hn_vfmap_size;
561 static struct ifnet             **hn_vfmap;
562
563 static const uint8_t
564 hn_rss_key_default[NDIS_HASH_KEYSIZE_TOEPLITZ] = {
565         0x6d, 0x5a, 0x56, 0xda, 0x25, 0x5b, 0x0e, 0xc2,
566         0x41, 0x67, 0x25, 0x3d, 0x43, 0xa3, 0x8f, 0xb0,
567         0xd0, 0xca, 0x2b, 0xcb, 0xae, 0x7b, 0x30, 0xb4,
568         0x77, 0xcb, 0x2d, 0xa3, 0x80, 0x30, 0xf2, 0x0c,
569         0x6a, 0x42, 0xb7, 0x3b, 0xbe, 0xac, 0x01, 0xfa
570 };
571
572 static const struct hyperv_guid hn_guid = {
573         .hv_guid = {
574             0x63, 0x51, 0x61, 0xf8, 0x3e, 0xdf, 0xc5, 0x46,
575             0x91, 0x3f, 0xf2, 0xd2, 0xf9, 0x65, 0xed, 0x0e }
576 };
577
578 static device_method_t hn_methods[] = {
579         /* Device interface */
580         DEVMETHOD(device_probe,         hn_probe),
581         DEVMETHOD(device_attach,        hn_attach),
582         DEVMETHOD(device_detach,        hn_detach),
583         DEVMETHOD(device_shutdown,      hn_shutdown),
584         DEVMETHOD_END
585 };
586
587 static driver_t hn_driver = {
588         "hn",
589         hn_methods,
590         sizeof(struct hn_softc)
591 };
592
593 static devclass_t hn_devclass;
594
595 DRIVER_MODULE(hn, vmbus, hn_driver, hn_devclass, 0, 0);
596 MODULE_VERSION(hn, 1);
597 MODULE_DEPEND(hn, vmbus, 1, 1, 1);
598
599 #if __FreeBSD_version >= 1100099
600 static void
601 hn_set_lro_lenlim(struct hn_softc *sc, int lenlim)
602 {
603         int i;
604
605         for (i = 0; i < sc->hn_rx_ring_cnt; ++i)
606                 sc->hn_rx_ring[i].hn_lro.lro_length_lim = lenlim;
607 }
608 #endif
609
610 static int
611 hn_txpkt_sglist(struct hn_tx_ring *txr, struct hn_txdesc *txd)
612 {
613
614         KASSERT(txd->chim_index == HN_NVS_CHIM_IDX_INVALID &&
615             txd->chim_size == 0, ("invalid rndis sglist txd"));
616         return (hn_nvs_send_rndis_sglist(txr->hn_chan, HN_NVS_RNDIS_MTYPE_DATA,
617             &txd->send_ctx, txr->hn_gpa, txr->hn_gpa_cnt));
618 }
619
620 static int
621 hn_txpkt_chim(struct hn_tx_ring *txr, struct hn_txdesc *txd)
622 {
623         struct hn_nvs_rndis rndis;
624
625         KASSERT(txd->chim_index != HN_NVS_CHIM_IDX_INVALID &&
626             txd->chim_size > 0, ("invalid rndis chim txd"));
627
628         rndis.nvs_type = HN_NVS_TYPE_RNDIS;
629         rndis.nvs_rndis_mtype = HN_NVS_RNDIS_MTYPE_DATA;
630         rndis.nvs_chim_idx = txd->chim_index;
631         rndis.nvs_chim_sz = txd->chim_size;
632
633         return (hn_nvs_send(txr->hn_chan, VMBUS_CHANPKT_FLAG_RC,
634             &rndis, sizeof(rndis), &txd->send_ctx));
635 }
636
637 static __inline uint32_t
638 hn_chim_alloc(struct hn_softc *sc)
639 {
640         int i, bmap_cnt = sc->hn_chim_bmap_cnt;
641         u_long *bmap = sc->hn_chim_bmap;
642         uint32_t ret = HN_NVS_CHIM_IDX_INVALID;
643
644         for (i = 0; i < bmap_cnt; ++i) {
645                 int idx;
646
647                 idx = ffsl(~bmap[i]);
648                 if (idx == 0)
649                         continue;
650
651                 --idx; /* ffsl is 1-based */
652                 KASSERT(i * LONG_BIT + idx < sc->hn_chim_cnt,
653                     ("invalid i %d and idx %d", i, idx));
654
655                 if (atomic_testandset_long(&bmap[i], idx))
656                         continue;
657
658                 ret = i * LONG_BIT + idx;
659                 break;
660         }
661         return (ret);
662 }
663
664 static __inline void
665 hn_chim_free(struct hn_softc *sc, uint32_t chim_idx)
666 {
667         u_long mask;
668         uint32_t idx;
669
670         idx = chim_idx / LONG_BIT;
671         KASSERT(idx < sc->hn_chim_bmap_cnt,
672             ("invalid chimney index 0x%x", chim_idx));
673
674         mask = 1UL << (chim_idx % LONG_BIT);
675         KASSERT(sc->hn_chim_bmap[idx] & mask,
676             ("index bitmap 0x%lx, chimney index %u, "
677              "bitmap idx %d, bitmask 0x%lx",
678              sc->hn_chim_bmap[idx], chim_idx, idx, mask));
679
680         atomic_clear_long(&sc->hn_chim_bmap[idx], mask);
681 }
682
683 #if defined(INET6) || defined(INET)
684
685 #define PULLUP_HDR(m, len)                              \
686 do {                                                    \
687         if (__predict_false((m)->m_len < (len))) {      \
688                 (m) = m_pullup((m), (len));             \
689                 if ((m) == NULL)                        \
690                         return (NULL);                  \
691         }                                               \
692 } while (0)
693
694 /*
695  * NOTE: If this function failed, the m_head would be freed.
696  */
697 static __inline struct mbuf *
698 hn_tso_fixup(struct mbuf *m_head)
699 {
700         struct ether_vlan_header *evl;
701         struct tcphdr *th;
702         int ehlen;
703
704         KASSERT(M_WRITABLE(m_head), ("TSO mbuf not writable"));
705
706         PULLUP_HDR(m_head, sizeof(*evl));
707         evl = mtod(m_head, struct ether_vlan_header *);
708         if (evl->evl_encap_proto == ntohs(ETHERTYPE_VLAN))
709                 ehlen = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
710         else
711                 ehlen = ETHER_HDR_LEN;
712
713 #ifdef INET
714         if (m_head->m_pkthdr.csum_flags & CSUM_IP_TSO) {
715                 struct ip *ip;
716                 int iphlen;
717
718                 PULLUP_HDR(m_head, ehlen + sizeof(*ip));
719                 ip = mtodo(m_head, ehlen);
720                 iphlen = ip->ip_hl << 2;
721
722                 PULLUP_HDR(m_head, ehlen + iphlen + sizeof(*th));
723                 th = mtodo(m_head, ehlen + iphlen);
724
725                 ip->ip_len = 0;
726                 ip->ip_sum = 0;
727                 th->th_sum = in_pseudo(ip->ip_src.s_addr,
728                     ip->ip_dst.s_addr, htons(IPPROTO_TCP));
729         }
730 #endif
731 #if defined(INET6) && defined(INET)
732         else
733 #endif
734 #ifdef INET6
735         {
736                 struct ip6_hdr *ip6;
737
738                 PULLUP_HDR(m_head, ehlen + sizeof(*ip6));
739                 ip6 = mtodo(m_head, ehlen);
740                 if (ip6->ip6_nxt != IPPROTO_TCP) {
741                         m_freem(m_head);
742                         return (NULL);
743                 }
744
745                 PULLUP_HDR(m_head, ehlen + sizeof(*ip6) + sizeof(*th));
746                 th = mtodo(m_head, ehlen + sizeof(*ip6));
747
748                 ip6->ip6_plen = 0;
749                 th->th_sum = in6_cksum_pseudo(ip6, 0, IPPROTO_TCP, 0);
750         }
751 #endif
752         return (m_head);
753
754 }
755
756 /*
757  * NOTE: If this function failed, the m_head would be freed.
758  */
759 static __inline struct mbuf *
760 hn_check_tcpsyn(struct mbuf *m_head, int *tcpsyn)
761 {
762         const struct ether_vlan_header *evl;
763         const struct tcphdr *th;
764         int ehlen;
765
766         *tcpsyn = 0;
767
768         PULLUP_HDR(m_head, sizeof(*evl));
769         evl = mtod(m_head, const struct ether_vlan_header *);
770         if (evl->evl_encap_proto == ntohs(ETHERTYPE_VLAN))
771                 ehlen = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
772         else
773                 ehlen = ETHER_HDR_LEN;
774
775 #ifdef INET
776         if (m_head->m_pkthdr.csum_flags & CSUM_IP_TCP) {
777                 const struct ip *ip;
778                 int iphlen;
779
780                 PULLUP_HDR(m_head, ehlen + sizeof(*ip));
781                 ip = mtodo(m_head, ehlen);
782                 iphlen = ip->ip_hl << 2;
783
784                 PULLUP_HDR(m_head, ehlen + iphlen + sizeof(*th));
785                 th = mtodo(m_head, ehlen + iphlen);
786                 if (th->th_flags & TH_SYN)
787                         *tcpsyn = 1;
788         }
789 #endif
790 #if defined(INET6) && defined(INET)
791         else
792 #endif
793 #ifdef INET6
794         {
795                 const struct ip6_hdr *ip6;
796
797                 PULLUP_HDR(m_head, ehlen + sizeof(*ip6));
798                 ip6 = mtodo(m_head, ehlen);
799                 if (ip6->ip6_nxt != IPPROTO_TCP)
800                         return (m_head);
801
802                 PULLUP_HDR(m_head, ehlen + sizeof(*ip6) + sizeof(*th));
803                 th = mtodo(m_head, ehlen + sizeof(*ip6));
804                 if (th->th_flags & TH_SYN)
805                         *tcpsyn = 1;
806         }
807 #endif
808         return (m_head);
809 }
810
811 #undef PULLUP_HDR
812
813 #endif  /* INET6 || INET */
814
815 static int
816 hn_set_rxfilter(struct hn_softc *sc, uint32_t filter)
817 {
818         int error = 0;
819
820         HN_LOCK_ASSERT(sc);
821
822         if (sc->hn_rx_filter != filter) {
823                 error = hn_rndis_set_rxfilter(sc, filter);
824                 if (!error)
825                         sc->hn_rx_filter = filter;
826         }
827         return (error);
828 }
829
830 static int
831 hn_rxfilter_config(struct hn_softc *sc)
832 {
833         struct ifnet *ifp = sc->hn_ifp;
834         uint32_t filter;
835
836         HN_LOCK_ASSERT(sc);
837
838         /*
839          * If the non-transparent mode VF is activated, we don't know how
840          * its RX filter is configured, so stick the synthetic device in
841          * the promiscous mode.
842          */
843         if ((ifp->if_flags & IFF_PROMISC) || (sc->hn_flags & HN_FLAG_RXVF)) {
844                 filter = NDIS_PACKET_TYPE_PROMISCUOUS;
845         } else {
846                 filter = NDIS_PACKET_TYPE_DIRECTED;
847                 if (ifp->if_flags & IFF_BROADCAST)
848                         filter |= NDIS_PACKET_TYPE_BROADCAST;
849                 /* TODO: support multicast list */
850                 if ((ifp->if_flags & IFF_ALLMULTI) ||
851                     !TAILQ_EMPTY(&ifp->if_multiaddrs))
852                         filter |= NDIS_PACKET_TYPE_ALL_MULTICAST;
853         }
854         return (hn_set_rxfilter(sc, filter));
855 }
856
857 static void
858 hn_set_txagg(struct hn_softc *sc)
859 {
860         uint32_t size, pkts;
861         int i;
862
863         /*
864          * Setup aggregation size.
865          */
866         if (sc->hn_agg_size < 0)
867                 size = UINT32_MAX;
868         else
869                 size = sc->hn_agg_size;
870
871         if (sc->hn_rndis_agg_size < size)
872                 size = sc->hn_rndis_agg_size;
873
874         /* NOTE: We only aggregate packets using chimney sending buffers. */
875         if (size > (uint32_t)sc->hn_chim_szmax)
876                 size = sc->hn_chim_szmax;
877
878         if (size <= 2 * HN_PKTSIZE_MIN(sc->hn_rndis_agg_align)) {
879                 /* Disable */
880                 size = 0;
881                 pkts = 0;
882                 goto done;
883         }
884
885         /* NOTE: Type of the per TX ring setting is 'int'. */
886         if (size > INT_MAX)
887                 size = INT_MAX;
888
889         /*
890          * Setup aggregation packet count.
891          */
892         if (sc->hn_agg_pkts < 0)
893                 pkts = UINT32_MAX;
894         else
895                 pkts = sc->hn_agg_pkts;
896
897         if (sc->hn_rndis_agg_pkts < pkts)
898                 pkts = sc->hn_rndis_agg_pkts;
899
900         if (pkts <= 1) {
901                 /* Disable */
902                 size = 0;
903                 pkts = 0;
904                 goto done;
905         }
906
907         /* NOTE: Type of the per TX ring setting is 'short'. */
908         if (pkts > SHRT_MAX)
909                 pkts = SHRT_MAX;
910
911 done:
912         /* NOTE: Type of the per TX ring setting is 'short'. */
913         if (sc->hn_rndis_agg_align > SHRT_MAX) {
914                 /* Disable */
915                 size = 0;
916                 pkts = 0;
917         }
918
919         if (bootverbose) {
920                 if_printf(sc->hn_ifp, "TX agg size %u, pkts %u, align %u\n",
921                     size, pkts, sc->hn_rndis_agg_align);
922         }
923
924         for (i = 0; i < sc->hn_tx_ring_cnt; ++i) {
925                 struct hn_tx_ring *txr = &sc->hn_tx_ring[i];
926
927                 mtx_lock(&txr->hn_tx_lock);
928                 txr->hn_agg_szmax = size;
929                 txr->hn_agg_pktmax = pkts;
930                 txr->hn_agg_align = sc->hn_rndis_agg_align;
931                 mtx_unlock(&txr->hn_tx_lock);
932         }
933 }
934
935 static int
936 hn_get_txswq_depth(const struct hn_tx_ring *txr)
937 {
938
939         KASSERT(txr->hn_txdesc_cnt > 0, ("tx ring is not setup yet"));
940         if (hn_tx_swq_depth < txr->hn_txdesc_cnt)
941                 return txr->hn_txdesc_cnt;
942         return hn_tx_swq_depth;
943 }
944
945 static int
946 hn_rss_reconfig(struct hn_softc *sc)
947 {
948         int error;
949
950         HN_LOCK_ASSERT(sc);
951
952         if ((sc->hn_flags & HN_FLAG_SYNTH_ATTACHED) == 0)
953                 return (ENXIO);
954
955         /*
956          * Disable RSS first.
957          *
958          * NOTE:
959          * Direct reconfiguration by setting the UNCHG flags does
960          * _not_ work properly.
961          */
962         if (bootverbose)
963                 if_printf(sc->hn_ifp, "disable RSS\n");
964         error = hn_rndis_conf_rss(sc, NDIS_RSS_FLAG_DISABLE);
965         if (error) {
966                 if_printf(sc->hn_ifp, "RSS disable failed\n");
967                 return (error);
968         }
969
970         /*
971          * Reenable the RSS w/ the updated RSS key or indirect
972          * table.
973          */
974         if (bootverbose)
975                 if_printf(sc->hn_ifp, "reconfig RSS\n");
976         error = hn_rndis_conf_rss(sc, NDIS_RSS_FLAG_NONE);
977         if (error) {
978                 if_printf(sc->hn_ifp, "RSS reconfig failed\n");
979                 return (error);
980         }
981         return (0);
982 }
983
984 static void
985 hn_rss_ind_fixup(struct hn_softc *sc)
986 {
987         struct ndis_rssprm_toeplitz *rss = &sc->hn_rss;
988         int i, nchan;
989
990         nchan = sc->hn_rx_ring_inuse;
991         KASSERT(nchan > 1, ("invalid # of channels %d", nchan));
992
993         /*
994          * Check indirect table to make sure that all channels in it
995          * can be used.
996          */
997         for (i = 0; i < NDIS_HASH_INDCNT; ++i) {
998                 if (rss->rss_ind[i] >= nchan) {
999                         if_printf(sc->hn_ifp,
1000                             "RSS indirect table %d fixup: %u -> %d\n",
1001                             i, rss->rss_ind[i], nchan - 1);
1002                         rss->rss_ind[i] = nchan - 1;
1003                 }
1004         }
1005 }
1006
1007 static int
1008 hn_ifmedia_upd(struct ifnet *ifp __unused)
1009 {
1010
1011         return EOPNOTSUPP;
1012 }
1013
1014 static void
1015 hn_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1016 {
1017         struct hn_softc *sc = ifp->if_softc;
1018
1019         ifmr->ifm_status = IFM_AVALID;
1020         ifmr->ifm_active = IFM_ETHER;
1021
1022         if ((sc->hn_link_flags & HN_LINK_FLAG_LINKUP) == 0) {
1023                 ifmr->ifm_active |= IFM_NONE;
1024                 return;
1025         }
1026         ifmr->ifm_status |= IFM_ACTIVE;
1027         ifmr->ifm_active |= IFM_10G_T | IFM_FDX;
1028 }
1029
1030 static void
1031 hn_rxvf_set_task(void *xarg, int pending __unused)
1032 {
1033         struct hn_rxvf_setarg *arg = xarg;
1034
1035         arg->rxr->hn_rxvf_ifp = arg->vf_ifp;
1036 }
1037
1038 static void
1039 hn_rxvf_set(struct hn_softc *sc, struct ifnet *vf_ifp)
1040 {
1041         struct hn_rx_ring *rxr;
1042         struct hn_rxvf_setarg arg;
1043         struct task task;
1044         int i;
1045
1046         HN_LOCK_ASSERT(sc);
1047
1048         TASK_INIT(&task, 0, hn_rxvf_set_task, &arg);
1049
1050         for (i = 0; i < sc->hn_rx_ring_cnt; ++i) {
1051                 rxr = &sc->hn_rx_ring[i];
1052
1053                 if (i < sc->hn_rx_ring_inuse) {
1054                         arg.rxr = rxr;
1055                         arg.vf_ifp = vf_ifp;
1056                         vmbus_chan_run_task(rxr->hn_chan, &task);
1057                 } else {
1058                         rxr->hn_rxvf_ifp = vf_ifp;
1059                 }
1060         }
1061 }
1062
1063 static bool
1064 hn_ismyvf(const struct hn_softc *sc, const struct ifnet *ifp)
1065 {
1066         const struct ifnet *hn_ifp;
1067
1068         hn_ifp = sc->hn_ifp;
1069
1070         if (ifp == hn_ifp)
1071                 return (false);
1072
1073         if (ifp->if_alloctype != IFT_ETHER)
1074                 return (false);
1075
1076         /* Ignore lagg/vlan interfaces */
1077         if (strcmp(ifp->if_dname, "lagg") == 0 ||
1078             strcmp(ifp->if_dname, "vlan") == 0)
1079                 return (false);
1080
1081         if (bcmp(IF_LLADDR(ifp), IF_LLADDR(hn_ifp), ETHER_ADDR_LEN) != 0)
1082                 return (false);
1083
1084         return (true);
1085 }
1086
1087 static void
1088 hn_rxvf_change(struct hn_softc *sc, struct ifnet *ifp, bool rxvf)
1089 {
1090         struct ifnet *hn_ifp;
1091
1092         HN_LOCK(sc);
1093
1094         if (!(sc->hn_flags & HN_FLAG_SYNTH_ATTACHED))
1095                 goto out;
1096
1097         if (!hn_ismyvf(sc, ifp))
1098                 goto out;
1099         hn_ifp = sc->hn_ifp;
1100
1101         if (rxvf) {
1102                 if (sc->hn_flags & HN_FLAG_RXVF)
1103                         goto out;
1104
1105                 sc->hn_flags |= HN_FLAG_RXVF;
1106                 hn_rxfilter_config(sc);
1107         } else {
1108                 if (!(sc->hn_flags & HN_FLAG_RXVF))
1109                         goto out;
1110
1111                 sc->hn_flags &= ~HN_FLAG_RXVF;
1112                 if (hn_ifp->if_drv_flags & IFF_DRV_RUNNING)
1113                         hn_rxfilter_config(sc);
1114                 else
1115                         hn_set_rxfilter(sc, NDIS_PACKET_TYPE_NONE);
1116         }
1117
1118         hn_nvs_set_datapath(sc,
1119             rxvf ? HN_NVS_DATAPATH_VF : HN_NVS_DATAPATH_SYNTH);
1120
1121         hn_rxvf_set(sc, rxvf ? ifp : NULL);
1122
1123         if (rxvf) {
1124                 hn_suspend_mgmt(sc);
1125                 sc->hn_link_flags &=
1126                     ~(HN_LINK_FLAG_LINKUP | HN_LINK_FLAG_NETCHG);
1127                 if_link_state_change(hn_ifp, LINK_STATE_DOWN);
1128         } else {
1129                 hn_resume_mgmt(sc);
1130         }
1131
1132         devctl_notify("HYPERV_NIC_VF", hn_ifp->if_xname,
1133             rxvf ? "VF_UP" : "VF_DOWN", NULL);
1134
1135         if (bootverbose) {
1136                 if_printf(hn_ifp, "datapath is switched %s %s\n",
1137                     rxvf ? "to" : "from", ifp->if_xname);
1138         }
1139 out:
1140         HN_UNLOCK(sc);
1141 }
1142
1143 static void
1144 hn_ifnet_event(void *arg, struct ifnet *ifp, int event)
1145 {
1146
1147         if (event != IFNET_EVENT_UP && event != IFNET_EVENT_DOWN)
1148                 return;
1149         hn_rxvf_change(arg, ifp, event == IFNET_EVENT_UP);
1150 }
1151
1152 static void
1153 hn_ifaddr_event(void *arg, struct ifnet *ifp)
1154 {
1155
1156         hn_rxvf_change(arg, ifp, ifp->if_flags & IFF_UP);
1157 }
1158
1159 static int
1160 hn_xpnt_vf_iocsetcaps(struct hn_softc *sc, struct ifreq *ifr)
1161 {
1162         struct ifnet *ifp, *vf_ifp;
1163         uint64_t tmp;
1164         int error;
1165
1166         HN_LOCK_ASSERT(sc);
1167         ifp = sc->hn_ifp;
1168         vf_ifp = sc->hn_vf_ifp;
1169
1170         /*
1171          * Fix up requested capabilities w/ supported capabilities,
1172          * since the supported capabilities could have been changed.
1173          */
1174         ifr->ifr_reqcap &= ifp->if_capabilities;
1175         /* Pass SIOCSIFCAP to VF. */
1176         error = vf_ifp->if_ioctl(vf_ifp, SIOCSIFCAP, (caddr_t)ifr);
1177
1178         /*
1179          * NOTE:
1180          * The error will be propagated to the callers, however, it
1181          * is _not_ useful here.
1182          */
1183
1184         /*
1185          * Merge VF's enabled capabilities.
1186          */
1187         ifp->if_capenable = vf_ifp->if_capenable & ifp->if_capabilities;
1188
1189         tmp = vf_ifp->if_hwassist & HN_CSUM_IP_HWASSIST(sc);
1190         if (ifp->if_capenable & IFCAP_TXCSUM)
1191                 ifp->if_hwassist |= tmp;
1192         else
1193                 ifp->if_hwassist &= ~tmp;
1194
1195         tmp = vf_ifp->if_hwassist & HN_CSUM_IP6_HWASSIST(sc);
1196         if (ifp->if_capenable & IFCAP_TXCSUM_IPV6)
1197                 ifp->if_hwassist |= tmp;
1198         else
1199                 ifp->if_hwassist &= ~tmp;
1200
1201         tmp = vf_ifp->if_hwassist & CSUM_IP_TSO;
1202         if (ifp->if_capenable & IFCAP_TSO4)
1203                 ifp->if_hwassist |= tmp;
1204         else
1205                 ifp->if_hwassist &= ~tmp;
1206
1207         tmp = vf_ifp->if_hwassist & CSUM_IP6_TSO;
1208         if (ifp->if_capenable & IFCAP_TSO6)
1209                 ifp->if_hwassist |= tmp;
1210         else
1211                 ifp->if_hwassist &= ~tmp;
1212
1213         return (error);
1214 }
1215
1216 static int
1217 hn_xpnt_vf_iocsetflags(struct hn_softc *sc)
1218 {
1219         struct ifnet *vf_ifp;
1220         struct ifreq ifr;
1221
1222         HN_LOCK_ASSERT(sc);
1223         vf_ifp = sc->hn_vf_ifp;
1224
1225         memset(&ifr, 0, sizeof(ifr));
1226         strlcpy(ifr.ifr_name, vf_ifp->if_xname, sizeof(ifr.ifr_name));
1227         ifr.ifr_flags = vf_ifp->if_flags & 0xffff;
1228         ifr.ifr_flagshigh = vf_ifp->if_flags >> 16;
1229         return (vf_ifp->if_ioctl(vf_ifp, SIOCSIFFLAGS, (caddr_t)&ifr));
1230 }
1231
1232 static void
1233 hn_xpnt_vf_saveifflags(struct hn_softc *sc)
1234 {
1235         struct ifnet *ifp = sc->hn_ifp;
1236         int allmulti = 0;
1237
1238         HN_LOCK_ASSERT(sc);
1239
1240         /* XXX vlan(4) style mcast addr maintenance */
1241         if (!TAILQ_EMPTY(&ifp->if_multiaddrs))
1242                 allmulti = IFF_ALLMULTI;
1243
1244         /* Always set the VF's if_flags */
1245         sc->hn_vf_ifp->if_flags = ifp->if_flags | allmulti;
1246 }
1247
1248 static void
1249 hn_xpnt_vf_input(struct ifnet *vf_ifp, struct mbuf *m)
1250 {
1251         struct rm_priotracker pt;
1252         struct ifnet *hn_ifp = NULL;
1253         struct mbuf *mn;
1254
1255         /*
1256          * XXX racy, if hn(4) ever detached.
1257          */
1258         rm_rlock(&hn_vfmap_lock, &pt);
1259         if (vf_ifp->if_index < hn_vfmap_size)
1260                 hn_ifp = hn_vfmap[vf_ifp->if_index];
1261         rm_runlock(&hn_vfmap_lock, &pt);
1262
1263         if (hn_ifp != NULL) {
1264                 for (mn = m; mn != NULL; mn = mn->m_nextpkt) {
1265                         /*
1266                          * Allow tapping on the VF.
1267                          */
1268                         ETHER_BPF_MTAP(vf_ifp, mn);
1269
1270                         /*
1271                          * Update VF stats.
1272                          */
1273                         if ((vf_ifp->if_capenable & IFCAP_HWSTATS) == 0) {
1274                                 if_inc_counter(vf_ifp, IFCOUNTER_IBYTES,
1275                                     mn->m_pkthdr.len);
1276                         }
1277                         /*
1278                          * XXX IFCOUNTER_IMCAST
1279                          * This stat updating is kinda invasive, since it
1280                          * requires two checks on the mbuf: the length check
1281                          * and the ethernet header check.  As of this write,
1282                          * all multicast packets go directly to hn(4), which
1283                          * makes imcast stat updating in the VF a try in vian.
1284                          */
1285
1286                         /*
1287                          * Fix up rcvif and increase hn(4)'s ipackets.
1288                          */
1289                         mn->m_pkthdr.rcvif = hn_ifp;
1290                         if_inc_counter(hn_ifp, IFCOUNTER_IPACKETS, 1);
1291                 }
1292                 /*
1293                  * Go through hn(4)'s if_input.
1294                  */
1295                 hn_ifp->if_input(hn_ifp, m);
1296         } else {
1297                 /*
1298                  * In the middle of the transition; free this
1299                  * mbuf chain.
1300                  */
1301                 while (m != NULL) {
1302                         mn = m->m_nextpkt;
1303                         m->m_nextpkt = NULL;
1304                         m_freem(m);
1305                         m = mn;
1306                 }
1307         }
1308 }
1309
1310 static void
1311 hn_mtu_change_fixup(struct hn_softc *sc)
1312 {
1313         struct ifnet *ifp;
1314
1315         HN_LOCK_ASSERT(sc);
1316         ifp = sc->hn_ifp;
1317
1318         hn_set_tso_maxsize(sc, hn_tso_maxlen, ifp->if_mtu);
1319 #if __FreeBSD_version >= 1100099
1320         if (sc->hn_rx_ring[0].hn_lro.lro_length_lim < HN_LRO_LENLIM_MIN(ifp))
1321                 hn_set_lro_lenlim(sc, HN_LRO_LENLIM_MIN(ifp));
1322 #endif
1323 }
1324
1325 static void
1326 hn_xpnt_vf_setready(struct hn_softc *sc)
1327 {
1328         struct ifnet *ifp, *vf_ifp;
1329         struct ifreq ifr;
1330
1331         HN_LOCK_ASSERT(sc);
1332         ifp = sc->hn_ifp;
1333         vf_ifp = sc->hn_vf_ifp;
1334
1335         /*
1336          * Mark the VF ready.
1337          */
1338         sc->hn_vf_rdytick = 0;
1339
1340         /*
1341          * Save information for restoration.
1342          */
1343         sc->hn_saved_caps = ifp->if_capabilities;
1344         sc->hn_saved_tsomax = ifp->if_hw_tsomax;
1345         sc->hn_saved_tsosegcnt = ifp->if_hw_tsomaxsegcount;
1346         sc->hn_saved_tsosegsz = ifp->if_hw_tsomaxsegsize;
1347
1348         /*
1349          * Intersect supported/enabled capabilities.
1350          *
1351          * NOTE:
1352          * if_hwassist is not changed here.
1353          */
1354         ifp->if_capabilities &= vf_ifp->if_capabilities;
1355         ifp->if_capenable &= ifp->if_capabilities;
1356
1357         /*
1358          * Fix TSO settings.
1359          */
1360         if (ifp->if_hw_tsomax > vf_ifp->if_hw_tsomax)
1361                 ifp->if_hw_tsomax = vf_ifp->if_hw_tsomax;
1362         if (ifp->if_hw_tsomaxsegcount > vf_ifp->if_hw_tsomaxsegcount)
1363                 ifp->if_hw_tsomaxsegcount = vf_ifp->if_hw_tsomaxsegcount;
1364         if (ifp->if_hw_tsomaxsegsize > vf_ifp->if_hw_tsomaxsegsize)
1365                 ifp->if_hw_tsomaxsegsize = vf_ifp->if_hw_tsomaxsegsize;
1366
1367         /*
1368          * Change VF's enabled capabilities.
1369          */
1370         memset(&ifr, 0, sizeof(ifr));
1371         strlcpy(ifr.ifr_name, vf_ifp->if_xname, sizeof(ifr.ifr_name));
1372         ifr.ifr_reqcap = ifp->if_capenable;
1373         hn_xpnt_vf_iocsetcaps(sc, &ifr);
1374
1375         if (ifp->if_mtu != ETHERMTU) {
1376                 int error;
1377
1378                 /*
1379                  * Change VF's MTU.
1380                  */
1381                 memset(&ifr, 0, sizeof(ifr));
1382                 strlcpy(ifr.ifr_name, vf_ifp->if_xname, sizeof(ifr.ifr_name));
1383                 ifr.ifr_mtu = ifp->if_mtu;
1384                 error = vf_ifp->if_ioctl(vf_ifp, SIOCSIFMTU, (caddr_t)&ifr);
1385                 if (error) {
1386                         if_printf(ifp, "%s SIOCSIFMTU %lu failed\n",
1387                             vf_ifp->if_xname, ifp->if_mtu);
1388                         if (ifp->if_mtu > ETHERMTU) {
1389                                 if_printf(ifp, "change MTU to %d\n", ETHERMTU);
1390
1391                                 /*
1392                                  * XXX
1393                                  * No need to adjust the synthetic parts' MTU;
1394                                  * failure of the adjustment will cause us
1395                                  * infinite headache.
1396                                  */
1397                                 ifp->if_mtu = ETHERMTU;
1398                                 hn_mtu_change_fixup(sc);
1399                         }
1400                 }
1401         }
1402 }
1403
1404 static bool
1405 hn_xpnt_vf_isready(struct hn_softc *sc)
1406 {
1407
1408         HN_LOCK_ASSERT(sc);
1409
1410         if (!hn_xpnt_vf || sc->hn_vf_ifp == NULL)
1411                 return (false);
1412
1413         if (sc->hn_vf_rdytick == 0)
1414                 return (true);
1415
1416         if (sc->hn_vf_rdytick > ticks)
1417                 return (false);
1418
1419         /* Mark VF as ready. */
1420         hn_xpnt_vf_setready(sc);
1421         return (true);
1422 }
1423
1424 static void
1425 hn_xpnt_vf_setenable(struct hn_softc *sc)
1426 {
1427         int i;
1428
1429         HN_LOCK_ASSERT(sc);
1430
1431         /* NOTE: hn_vf_lock for hn_transmit()/hn_qflush() */
1432         rm_wlock(&sc->hn_vf_lock);
1433         sc->hn_xvf_flags |= HN_XVFFLAG_ENABLED;
1434         rm_wunlock(&sc->hn_vf_lock);
1435
1436         for (i = 0; i < sc->hn_rx_ring_cnt; ++i)
1437                 sc->hn_rx_ring[i].hn_rx_flags |= HN_RX_FLAG_XPNT_VF;
1438 }
1439
1440 static void
1441 hn_xpnt_vf_setdisable(struct hn_softc *sc, bool clear_vf)
1442 {
1443         int i;
1444
1445         HN_LOCK_ASSERT(sc);
1446
1447         /* NOTE: hn_vf_lock for hn_transmit()/hn_qflush() */
1448         rm_wlock(&sc->hn_vf_lock);
1449         sc->hn_xvf_flags &= ~HN_XVFFLAG_ENABLED;
1450         if (clear_vf)
1451                 sc->hn_vf_ifp = NULL;
1452         rm_wunlock(&sc->hn_vf_lock);
1453
1454         for (i = 0; i < sc->hn_rx_ring_cnt; ++i)
1455                 sc->hn_rx_ring[i].hn_rx_flags &= ~HN_RX_FLAG_XPNT_VF;
1456 }
1457
1458 static void
1459 hn_xpnt_vf_init(struct hn_softc *sc)
1460 {
1461         int error;
1462
1463         HN_LOCK_ASSERT(sc);
1464
1465         KASSERT((sc->hn_xvf_flags & HN_XVFFLAG_ENABLED) == 0,
1466             ("%s: transparent VF was enabled", sc->hn_ifp->if_xname));
1467
1468         if (bootverbose) {
1469                 if_printf(sc->hn_ifp, "try bringing up %s\n",
1470                     sc->hn_vf_ifp->if_xname);
1471         }
1472
1473         /*
1474          * Bring the VF up.
1475          */
1476         hn_xpnt_vf_saveifflags(sc);
1477         sc->hn_vf_ifp->if_flags |= IFF_UP;
1478         error = hn_xpnt_vf_iocsetflags(sc);
1479         if (error) {
1480                 if_printf(sc->hn_ifp, "bringing up %s failed: %d\n",
1481                     sc->hn_vf_ifp->if_xname, error);
1482                 return;
1483         }
1484
1485         /*
1486          * NOTE:
1487          * Datapath setting must happen _after_ bringing the VF up.
1488          */
1489         hn_nvs_set_datapath(sc, HN_NVS_DATAPATH_VF);
1490
1491         /* Mark transparent mode VF as enabled. */
1492         hn_xpnt_vf_setenable(sc);
1493 }
1494
1495 static void
1496 hn_xpnt_vf_init_taskfunc(void *xsc, int pending __unused)
1497 {
1498         struct hn_softc *sc = xsc;
1499
1500         HN_LOCK(sc);
1501
1502         if ((sc->hn_flags & HN_FLAG_SYNTH_ATTACHED) == 0)
1503                 goto done;
1504         if (sc->hn_vf_ifp == NULL)
1505                 goto done;
1506         if (sc->hn_xvf_flags & HN_XVFFLAG_ENABLED)
1507                 goto done;
1508
1509         if (sc->hn_vf_rdytick != 0) {
1510                 /* Mark VF as ready. */
1511                 hn_xpnt_vf_setready(sc);
1512         }
1513
1514         if (sc->hn_ifp->if_drv_flags & IFF_DRV_RUNNING) {
1515                 /*
1516                  * Delayed VF initialization.
1517                  */
1518                 if (bootverbose) {
1519                         if_printf(sc->hn_ifp, "delayed initialize %s\n",
1520                             sc->hn_vf_ifp->if_xname);
1521                 }
1522                 hn_xpnt_vf_init(sc);
1523         }
1524 done:
1525         HN_UNLOCK(sc);
1526 }
1527
1528 static void
1529 hn_ifnet_attevent(void *xsc, struct ifnet *ifp)
1530 {
1531         struct hn_softc *sc = xsc;
1532
1533         HN_LOCK(sc);
1534
1535         if (!(sc->hn_flags & HN_FLAG_SYNTH_ATTACHED))
1536                 goto done;
1537
1538         if (!hn_ismyvf(sc, ifp))
1539                 goto done;
1540
1541         if (sc->hn_vf_ifp != NULL) {
1542                 if_printf(sc->hn_ifp, "%s was attached as VF\n",
1543                     sc->hn_vf_ifp->if_xname);
1544                 goto done;
1545         }
1546
1547         if (hn_xpnt_vf && ifp->if_start != NULL) {
1548                 /*
1549                  * ifnet.if_start is _not_ supported by transparent
1550                  * mode VF; mainly due to the IFF_DRV_OACTIVE flag.
1551                  */
1552                 if_printf(sc->hn_ifp, "%s uses if_start, which is unsupported "
1553                     "in transparent VF mode.\n", ifp->if_xname);
1554                 goto done;
1555         }
1556
1557         rm_wlock(&hn_vfmap_lock);
1558
1559         if (ifp->if_index >= hn_vfmap_size) {
1560                 struct ifnet **newmap;
1561                 int newsize;
1562
1563                 newsize = ifp->if_index + HN_VFMAP_SIZE_DEF;
1564                 newmap = malloc(sizeof(struct ifnet *) * newsize, M_DEVBUF,
1565                     M_WAITOK | M_ZERO);
1566
1567                 memcpy(newmap, hn_vfmap,
1568                     sizeof(struct ifnet *) * hn_vfmap_size);
1569                 free(hn_vfmap, M_DEVBUF);
1570                 hn_vfmap = newmap;
1571                 hn_vfmap_size = newsize;
1572         }
1573         KASSERT(hn_vfmap[ifp->if_index] == NULL,
1574             ("%s: ifindex %d was mapped to %s",
1575              ifp->if_xname, ifp->if_index, hn_vfmap[ifp->if_index]->if_xname));
1576         hn_vfmap[ifp->if_index] = sc->hn_ifp;
1577
1578         rm_wunlock(&hn_vfmap_lock);
1579
1580         /* NOTE: hn_vf_lock for hn_transmit()/hn_qflush() */
1581         rm_wlock(&sc->hn_vf_lock);
1582         KASSERT((sc->hn_xvf_flags & HN_XVFFLAG_ENABLED) == 0,
1583             ("%s: transparent VF was enabled", sc->hn_ifp->if_xname));
1584         sc->hn_vf_ifp = ifp;
1585         rm_wunlock(&sc->hn_vf_lock);
1586
1587         if (hn_xpnt_vf) {
1588                 int wait_ticks;
1589
1590                 /*
1591                  * Install if_input for vf_ifp, which does vf_ifp -> hn_ifp.
1592                  * Save vf_ifp's current if_input for later restoration.
1593                  */
1594                 sc->hn_vf_input = ifp->if_input;
1595                 ifp->if_input = hn_xpnt_vf_input;
1596
1597                 /*
1598                  * Stop link status management; use the VF's.
1599                  */
1600                 hn_suspend_mgmt(sc);
1601
1602                 /*
1603                  * Give VF sometime to complete its attach routing.
1604                  */
1605                 wait_ticks = hn_xpnt_vf_attwait * hz;
1606                 sc->hn_vf_rdytick = ticks + wait_ticks;
1607
1608                 taskqueue_enqueue_timeout(sc->hn_vf_taskq, &sc->hn_vf_init,
1609                     wait_ticks);
1610         }
1611 done:
1612         HN_UNLOCK(sc);
1613 }
1614
1615 static void
1616 hn_ifnet_detevent(void *xsc, struct ifnet *ifp)
1617 {
1618         struct hn_softc *sc = xsc;
1619
1620         HN_LOCK(sc);
1621
1622         if (sc->hn_vf_ifp == NULL)
1623                 goto done;
1624
1625         if (!hn_ismyvf(sc, ifp))
1626                 goto done;
1627
1628         if (hn_xpnt_vf) {
1629                 /*
1630                  * Make sure that the delayed initialization is not running.
1631                  *
1632                  * NOTE:
1633                  * - This lock _must_ be released, since the hn_vf_init task
1634                  *   will try holding this lock.
1635                  * - It is safe to release this lock here, since the
1636                  *   hn_ifnet_attevent() is interlocked by the hn_vf_ifp.
1637                  *
1638                  * XXX racy, if hn(4) ever detached.
1639                  */
1640                 HN_UNLOCK(sc);
1641                 taskqueue_drain_timeout(sc->hn_vf_taskq, &sc->hn_vf_init);
1642                 HN_LOCK(sc);
1643
1644                 KASSERT(sc->hn_vf_input != NULL, ("%s VF input is not saved",
1645                     sc->hn_ifp->if_xname));
1646                 ifp->if_input = sc->hn_vf_input;
1647                 sc->hn_vf_input = NULL;
1648
1649                 if (sc->hn_xvf_flags & HN_XVFFLAG_ENABLED)
1650                         hn_nvs_set_datapath(sc, HN_NVS_DATAPATH_SYNTH);
1651
1652                 if (sc->hn_vf_rdytick == 0) {
1653                         /*
1654                          * The VF was ready; restore some settings.
1655                          */
1656                         sc->hn_ifp->if_capabilities = sc->hn_saved_caps;
1657                         /*
1658                          * NOTE:
1659                          * There is _no_ need to fixup if_capenable and
1660                          * if_hwassist, since the if_capabilities before
1661                          * restoration was an intersection of the VF's
1662                          * if_capabilites and the synthetic device's
1663                          * if_capabilites.
1664                          */
1665                         sc->hn_ifp->if_hw_tsomax = sc->hn_saved_tsomax;
1666                         sc->hn_ifp->if_hw_tsomaxsegcount =
1667                             sc->hn_saved_tsosegcnt;
1668                         sc->hn_ifp->if_hw_tsomaxsegsize = sc->hn_saved_tsosegsz;
1669                 }
1670
1671                 /*
1672                  * Resume link status management, which was suspended
1673                  * by hn_ifnet_attevent().
1674                  */
1675                 hn_resume_mgmt(sc);
1676         }
1677
1678         /* Mark transparent mode VF as disabled. */
1679         hn_xpnt_vf_setdisable(sc, true /* clear hn_vf_ifp */);
1680
1681         rm_wlock(&hn_vfmap_lock);
1682
1683         KASSERT(ifp->if_index < hn_vfmap_size,
1684             ("ifindex %d, vfmapsize %d", ifp->if_index, hn_vfmap_size));
1685         if (hn_vfmap[ifp->if_index] != NULL) {
1686                 KASSERT(hn_vfmap[ifp->if_index] == sc->hn_ifp,
1687                     ("%s: ifindex %d was mapped to %s",
1688                      ifp->if_xname, ifp->if_index,
1689                      hn_vfmap[ifp->if_index]->if_xname));
1690                 hn_vfmap[ifp->if_index] = NULL;
1691         }
1692
1693         rm_wunlock(&hn_vfmap_lock);
1694 done:
1695         HN_UNLOCK(sc);
1696 }
1697
1698 static void
1699 hn_ifnet_lnkevent(void *xsc, struct ifnet *ifp, int link_state)
1700 {
1701         struct hn_softc *sc = xsc;
1702
1703         if (sc->hn_vf_ifp == ifp)
1704                 if_link_state_change(sc->hn_ifp, link_state);
1705 }
1706
1707 static int
1708 hn_probe(device_t dev)
1709 {
1710
1711         if (VMBUS_PROBE_GUID(device_get_parent(dev), dev, &hn_guid) == 0) {
1712                 device_set_desc(dev, "Hyper-V Network Interface");
1713                 return BUS_PROBE_DEFAULT;
1714         }
1715         return ENXIO;
1716 }
1717
1718 static int
1719 hn_attach(device_t dev)
1720 {
1721         struct hn_softc *sc = device_get_softc(dev);
1722         struct sysctl_oid_list *child;
1723         struct sysctl_ctx_list *ctx;
1724         uint8_t eaddr[ETHER_ADDR_LEN];
1725         struct ifnet *ifp = NULL;
1726         int error, ring_cnt, tx_ring_cnt;
1727
1728         sc->hn_dev = dev;
1729         sc->hn_prichan = vmbus_get_channel(dev);
1730         HN_LOCK_INIT(sc);
1731         rm_init(&sc->hn_vf_lock, "hnvf");
1732         if (hn_xpnt_vf && hn_xpnt_vf_accbpf)
1733                 sc->hn_xvf_flags |= HN_XVFFLAG_ACCBPF;
1734
1735         /*
1736          * Initialize these tunables once.
1737          */
1738         sc->hn_agg_size = hn_tx_agg_size;
1739         sc->hn_agg_pkts = hn_tx_agg_pkts;
1740
1741         /*
1742          * Setup taskqueue for transmission.
1743          */
1744         if (hn_tx_taskq_mode == HN_TX_TASKQ_M_INDEP) {
1745                 int i;
1746
1747                 sc->hn_tx_taskqs =
1748                     malloc(hn_tx_taskq_cnt * sizeof(struct taskqueue *),
1749                     M_DEVBUF, M_WAITOK);
1750                 for (i = 0; i < hn_tx_taskq_cnt; ++i) {
1751                         sc->hn_tx_taskqs[i] = taskqueue_create("hn_tx",
1752                             M_WAITOK, taskqueue_thread_enqueue,
1753                             &sc->hn_tx_taskqs[i]);
1754                         taskqueue_start_threads(&sc->hn_tx_taskqs[i], 1, PI_NET,
1755                             "%s tx%d", device_get_nameunit(dev), i);
1756                 }
1757         } else if (hn_tx_taskq_mode == HN_TX_TASKQ_M_GLOBAL) {
1758                 sc->hn_tx_taskqs = hn_tx_taskque;
1759         }
1760
1761         /*
1762          * Setup taskqueue for mangement tasks, e.g. link status.
1763          */
1764         sc->hn_mgmt_taskq0 = taskqueue_create("hn_mgmt", M_WAITOK,
1765             taskqueue_thread_enqueue, &sc->hn_mgmt_taskq0);
1766         taskqueue_start_threads(&sc->hn_mgmt_taskq0, 1, PI_NET, "%s mgmt",
1767             device_get_nameunit(dev));
1768         TASK_INIT(&sc->hn_link_task, 0, hn_link_taskfunc, sc);
1769         TASK_INIT(&sc->hn_netchg_init, 0, hn_netchg_init_taskfunc, sc);
1770         TIMEOUT_TASK_INIT(sc->hn_mgmt_taskq0, &sc->hn_netchg_status, 0,
1771             hn_netchg_status_taskfunc, sc);
1772
1773         if (hn_xpnt_vf) {
1774                 /*
1775                  * Setup taskqueue for VF tasks, e.g. delayed VF bringing up.
1776                  */
1777                 sc->hn_vf_taskq = taskqueue_create("hn_vf", M_WAITOK,
1778                     taskqueue_thread_enqueue, &sc->hn_vf_taskq);
1779                 taskqueue_start_threads(&sc->hn_vf_taskq, 1, PI_NET, "%s vf",
1780                     device_get_nameunit(dev));
1781                 TIMEOUT_TASK_INIT(sc->hn_vf_taskq, &sc->hn_vf_init, 0,
1782                     hn_xpnt_vf_init_taskfunc, sc);
1783         }
1784
1785         /*
1786          * Allocate ifnet and setup its name earlier, so that if_printf
1787          * can be used by functions, which will be called after
1788          * ether_ifattach().
1789          */
1790         ifp = sc->hn_ifp = sc->arpcom.ac_ifp = if_alloc(IFT_ETHER);
1791         ifp->if_softc = sc;
1792         if_initname(ifp, device_get_name(dev), device_get_unit(dev));
1793
1794         /*
1795          * Initialize ifmedia earlier so that it can be unconditionally
1796          * destroyed, if error happened later on.
1797          */
1798         ifmedia_init(&sc->hn_media, 0, hn_ifmedia_upd, hn_ifmedia_sts);
1799
1800         /*
1801          * Figure out the # of RX rings (ring_cnt) and the # of TX rings
1802          * to use (tx_ring_cnt).
1803          *
1804          * NOTE:
1805          * The # of RX rings to use is same as the # of channels to use.
1806          */
1807         ring_cnt = hn_chan_cnt;
1808         if (ring_cnt <= 0) {
1809                 /* Default */
1810                 ring_cnt = mp_ncpus;
1811                 if (ring_cnt > HN_RING_CNT_DEF_MAX)
1812                         ring_cnt = HN_RING_CNT_DEF_MAX;
1813         } else if (ring_cnt > mp_ncpus) {
1814                 ring_cnt = mp_ncpus;
1815         }
1816
1817         tx_ring_cnt = hn_tx_ring_cnt;
1818         if (tx_ring_cnt <= 0 || tx_ring_cnt > ring_cnt)
1819                 tx_ring_cnt = ring_cnt;
1820 #ifdef HN_IFSTART_SUPPORT
1821         if (hn_use_if_start) {
1822                 /* ifnet.if_start only needs one TX ring. */
1823                 tx_ring_cnt = 1;
1824         }
1825 #endif
1826
1827         /*
1828          * Set the leader CPU for channels.
1829          */
1830         sc->hn_cpu = atomic_fetchadd_int(&hn_cpu_index, ring_cnt) % mp_ncpus;
1831
1832         /*
1833          * Create enough TX/RX rings, even if only limited number of
1834          * channels can be allocated.
1835          */
1836         error = hn_create_tx_data(sc, tx_ring_cnt);
1837         if (error)
1838                 goto failed;
1839         error = hn_create_rx_data(sc, ring_cnt);
1840         if (error)
1841                 goto failed;
1842
1843         /*
1844          * Create transaction context for NVS and RNDIS transactions.
1845          */
1846         sc->hn_xact = vmbus_xact_ctx_create(bus_get_dma_tag(dev),
1847             HN_XACT_REQ_SIZE, HN_XACT_RESP_SIZE, 0);
1848         if (sc->hn_xact == NULL) {
1849                 error = ENXIO;
1850                 goto failed;
1851         }
1852
1853         /*
1854          * Install orphan handler for the revocation of this device's
1855          * primary channel.
1856          *
1857          * NOTE:
1858          * The processing order is critical here:
1859          * Install the orphan handler, _before_ testing whether this
1860          * device's primary channel has been revoked or not.
1861          */
1862         vmbus_chan_set_orphan(sc->hn_prichan, sc->hn_xact);
1863         if (vmbus_chan_is_revoked(sc->hn_prichan)) {
1864                 error = ENXIO;
1865                 goto failed;
1866         }
1867
1868         /*
1869          * Attach the synthetic parts, i.e. NVS and RNDIS.
1870          */
1871         error = hn_synth_attach(sc, ETHERMTU);
1872         if (error)
1873                 goto failed;
1874
1875         error = hn_rndis_get_eaddr(sc, eaddr);
1876         if (error)
1877                 goto failed;
1878
1879 #if __FreeBSD_version >= 1100099
1880         if (sc->hn_rx_ring_inuse > 1) {
1881                 /*
1882                  * Reduce TCP segment aggregation limit for multiple
1883                  * RX rings to increase ACK timeliness.
1884                  */
1885                 hn_set_lro_lenlim(sc, HN_LRO_LENLIM_MULTIRX_DEF);
1886         }
1887 #endif
1888
1889         /*
1890          * Fixup TX stuffs after synthetic parts are attached.
1891          */
1892         hn_fixup_tx_data(sc);
1893
1894         ctx = device_get_sysctl_ctx(dev);
1895         child = SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
1896         SYSCTL_ADD_UINT(ctx, child, OID_AUTO, "nvs_version", CTLFLAG_RD,
1897             &sc->hn_nvs_ver, 0, "NVS version");
1898         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "ndis_version",
1899             CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
1900             hn_ndis_version_sysctl, "A", "NDIS version");
1901         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "caps",
1902             CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
1903             hn_caps_sysctl, "A", "capabilities");
1904         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "hwassist",
1905             CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
1906             hn_hwassist_sysctl, "A", "hwassist");
1907         SYSCTL_ADD_UINT(ctx, child, OID_AUTO, "tso_max",
1908             CTLFLAG_RD, &ifp->if_hw_tsomax, 0, "max TSO size");
1909         SYSCTL_ADD_UINT(ctx, child, OID_AUTO, "tso_maxsegcnt",
1910             CTLFLAG_RD, &ifp->if_hw_tsomaxsegcount, 0,
1911             "max # of TSO segments");
1912         SYSCTL_ADD_UINT(ctx, child, OID_AUTO, "tso_maxsegsz",
1913             CTLFLAG_RD, &ifp->if_hw_tsomaxsegsize, 0,
1914             "max size of TSO segment");
1915         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "rxfilter",
1916             CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
1917             hn_rxfilter_sysctl, "A", "rxfilter");
1918         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "rss_hash",
1919             CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
1920             hn_rss_hash_sysctl, "A", "RSS hash");
1921         SYSCTL_ADD_INT(ctx, child, OID_AUTO, "rss_ind_size",
1922             CTLFLAG_RD, &sc->hn_rss_ind_size, 0, "RSS indirect entry count");
1923         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "rss_key",
1924             CTLTYPE_OPAQUE | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, 0,
1925             hn_rss_key_sysctl, "IU", "RSS key");
1926         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "rss_ind",
1927             CTLTYPE_OPAQUE | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, 0,
1928             hn_rss_ind_sysctl, "IU", "RSS indirect table");
1929         SYSCTL_ADD_UINT(ctx, child, OID_AUTO, "rndis_agg_size",
1930             CTLFLAG_RD, &sc->hn_rndis_agg_size, 0,
1931             "RNDIS offered packet transmission aggregation size limit");
1932         SYSCTL_ADD_UINT(ctx, child, OID_AUTO, "rndis_agg_pkts",
1933             CTLFLAG_RD, &sc->hn_rndis_agg_pkts, 0,
1934             "RNDIS offered packet transmission aggregation count limit");
1935         SYSCTL_ADD_UINT(ctx, child, OID_AUTO, "rndis_agg_align",
1936             CTLFLAG_RD, &sc->hn_rndis_agg_align, 0,
1937             "RNDIS packet transmission aggregation alignment");
1938         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "agg_size",
1939             CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, 0,
1940             hn_txagg_size_sysctl, "I",
1941             "Packet transmission aggregation size, 0 -- disable, -1 -- auto");
1942         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "agg_pkts",
1943             CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, 0,
1944             hn_txagg_pkts_sysctl, "I",
1945             "Packet transmission aggregation packets, "
1946             "0 -- disable, -1 -- auto");
1947         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "polling",
1948             CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, 0,
1949             hn_polling_sysctl, "I",
1950             "Polling frequency: [100,1000000], 0 disable polling");
1951         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "vf",
1952             CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
1953             hn_vf_sysctl, "A", "Virtual Function's name");
1954         if (!hn_xpnt_vf) {
1955                 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "rxvf",
1956                     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
1957                     hn_rxvf_sysctl, "A", "activated Virtual Function's name");
1958         } else {
1959                 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "vf_xpnt_enabled",
1960                     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
1961                     hn_xpnt_vf_enabled_sysctl, "I",
1962                     "Transparent VF enabled");
1963                 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "vf_xpnt_accbpf",
1964                     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, 0,
1965                     hn_xpnt_vf_accbpf_sysctl, "I",
1966                     "Accurate BPF for transparent VF");
1967         }
1968
1969         /*
1970          * Setup the ifmedia, which has been initialized earlier.
1971          */
1972         ifmedia_add(&sc->hn_media, IFM_ETHER | IFM_AUTO, 0, NULL);
1973         ifmedia_set(&sc->hn_media, IFM_ETHER | IFM_AUTO);
1974         /* XXX ifmedia_set really should do this for us */
1975         sc->hn_media.ifm_media = sc->hn_media.ifm_cur->ifm_media;
1976
1977         /*
1978          * Setup the ifnet for this interface.
1979          */
1980
1981 #ifdef __LP64__
1982         ifp->if_baudrate = IF_Gbps(10);
1983 #else
1984         /* if_baudrate is 32bits on 32bit system. */
1985         ifp->if_baudrate = IF_Gbps(1);
1986 #endif
1987         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1988         ifp->if_ioctl = hn_ioctl;
1989         ifp->if_init = hn_init;
1990 #ifdef HN_IFSTART_SUPPORT
1991         if (hn_use_if_start) {
1992                 int qdepth = hn_get_txswq_depth(&sc->hn_tx_ring[0]);
1993
1994                 ifp->if_start = hn_start;
1995                 IFQ_SET_MAXLEN(&ifp->if_snd, qdepth);
1996                 ifp->if_snd.ifq_drv_maxlen = qdepth - 1;
1997                 IFQ_SET_READY(&ifp->if_snd);
1998         } else
1999 #endif
2000         {
2001                 ifp->if_transmit = hn_transmit;
2002                 ifp->if_qflush = hn_xmit_qflush;
2003         }
2004
2005         ifp->if_capabilities |= IFCAP_RXCSUM | IFCAP_LRO | IFCAP_LINKSTATE;
2006 #ifdef foo
2007         /* We can't diff IPv6 packets from IPv4 packets on RX path. */
2008         ifp->if_capabilities |= IFCAP_RXCSUM_IPV6;
2009 #endif
2010         if (sc->hn_caps & HN_CAP_VLAN) {
2011                 /* XXX not sure about VLAN_MTU. */
2012                 ifp->if_capabilities |= IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU;
2013         }
2014
2015         ifp->if_hwassist = sc->hn_tx_ring[0].hn_csum_assist;
2016         if (ifp->if_hwassist & HN_CSUM_IP_MASK)
2017                 ifp->if_capabilities |= IFCAP_TXCSUM;
2018         if (ifp->if_hwassist & HN_CSUM_IP6_MASK)
2019                 ifp->if_capabilities |= IFCAP_TXCSUM_IPV6;
2020         if (sc->hn_caps & HN_CAP_TSO4) {
2021                 ifp->if_capabilities |= IFCAP_TSO4;
2022                 ifp->if_hwassist |= CSUM_IP_TSO;
2023         }
2024         if (sc->hn_caps & HN_CAP_TSO6) {
2025                 ifp->if_capabilities |= IFCAP_TSO6;
2026                 ifp->if_hwassist |= CSUM_IP6_TSO;
2027         }
2028
2029         /* Enable all available capabilities by default. */
2030         ifp->if_capenable = ifp->if_capabilities;
2031
2032         /*
2033          * Disable IPv6 TSO and TXCSUM by default, they still can
2034          * be enabled through SIOCSIFCAP.
2035          */
2036         ifp->if_capenable &= ~(IFCAP_TXCSUM_IPV6 | IFCAP_TSO6);
2037         ifp->if_hwassist &= ~(HN_CSUM_IP6_MASK | CSUM_IP6_TSO);
2038
2039         if (ifp->if_capabilities & (IFCAP_TSO6 | IFCAP_TSO4)) {
2040                 /*
2041                  * Lock hn_set_tso_maxsize() to simplify its
2042                  * internal logic.
2043                  */
2044                 HN_LOCK(sc);
2045                 hn_set_tso_maxsize(sc, hn_tso_maxlen, ETHERMTU);
2046                 HN_UNLOCK(sc);
2047                 ifp->if_hw_tsomaxsegcount = HN_TX_DATA_SEGCNT_MAX;
2048                 ifp->if_hw_tsomaxsegsize = PAGE_SIZE;
2049         }
2050
2051         ether_ifattach(ifp, eaddr);
2052
2053         if ((ifp->if_capabilities & (IFCAP_TSO6 | IFCAP_TSO4)) && bootverbose) {
2054                 if_printf(ifp, "TSO segcnt %u segsz %u\n",
2055                     ifp->if_hw_tsomaxsegcount, ifp->if_hw_tsomaxsegsize);
2056         }
2057
2058         /* Inform the upper layer about the long frame support. */
2059         ifp->if_hdrlen = sizeof(struct ether_vlan_header);
2060
2061         /*
2062          * Kick off link status check.
2063          */
2064         sc->hn_mgmt_taskq = sc->hn_mgmt_taskq0;
2065         hn_update_link_status(sc);
2066
2067         if (!hn_xpnt_vf) {
2068                 sc->hn_ifnet_evthand = EVENTHANDLER_REGISTER(ifnet_event,
2069                     hn_ifnet_event, sc, EVENTHANDLER_PRI_ANY);
2070                 sc->hn_ifaddr_evthand = EVENTHANDLER_REGISTER(ifaddr_event,
2071                     hn_ifaddr_event, sc, EVENTHANDLER_PRI_ANY);
2072         } else {
2073                 sc->hn_ifnet_lnkhand = EVENTHANDLER_REGISTER(ifnet_link_event,
2074                     hn_ifnet_lnkevent, sc, EVENTHANDLER_PRI_ANY);
2075         }
2076
2077         /*
2078          * NOTE:
2079          * Subscribe ether_ifattach event, instead of ifnet_arrival event,
2080          * since interface's LLADDR is needed; interface LLADDR is not
2081          * available when ifnet_arrival event is triggered.
2082          */
2083         sc->hn_ifnet_atthand = EVENTHANDLER_REGISTER(ether_ifattach_event,
2084             hn_ifnet_attevent, sc, EVENTHANDLER_PRI_ANY);
2085         sc->hn_ifnet_dethand = EVENTHANDLER_REGISTER(ifnet_departure_event,
2086             hn_ifnet_detevent, sc, EVENTHANDLER_PRI_ANY);
2087
2088         return (0);
2089 failed:
2090         if (sc->hn_flags & HN_FLAG_SYNTH_ATTACHED)
2091                 hn_synth_detach(sc);
2092         hn_detach(dev);
2093         return (error);
2094 }
2095
2096 static int
2097 hn_detach(device_t dev)
2098 {
2099         struct hn_softc *sc = device_get_softc(dev);
2100         struct ifnet *ifp = sc->hn_ifp, *vf_ifp;
2101
2102         if (sc->hn_xact != NULL && vmbus_chan_is_revoked(sc->hn_prichan)) {
2103                 /*
2104                  * In case that the vmbus missed the orphan handler
2105                  * installation.
2106                  */
2107                 vmbus_xact_ctx_orphan(sc->hn_xact);
2108         }
2109
2110         if (sc->hn_ifaddr_evthand != NULL)
2111                 EVENTHANDLER_DEREGISTER(ifaddr_event, sc->hn_ifaddr_evthand);
2112         if (sc->hn_ifnet_evthand != NULL)
2113                 EVENTHANDLER_DEREGISTER(ifnet_event, sc->hn_ifnet_evthand);
2114         if (sc->hn_ifnet_atthand != NULL) {
2115                 EVENTHANDLER_DEREGISTER(ether_ifattach_event,
2116                     sc->hn_ifnet_atthand);
2117         }
2118         if (sc->hn_ifnet_dethand != NULL) {
2119                 EVENTHANDLER_DEREGISTER(ifnet_departure_event,
2120                     sc->hn_ifnet_dethand);
2121         }
2122         if (sc->hn_ifnet_lnkhand != NULL)
2123                 EVENTHANDLER_DEREGISTER(ifnet_link_event, sc->hn_ifnet_lnkhand);
2124
2125         vf_ifp = sc->hn_vf_ifp;
2126         __compiler_membar();
2127         if (vf_ifp != NULL)
2128                 hn_ifnet_detevent(sc, vf_ifp);
2129
2130         if (device_is_attached(dev)) {
2131                 HN_LOCK(sc);
2132                 if (sc->hn_flags & HN_FLAG_SYNTH_ATTACHED) {
2133                         if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2134                                 hn_stop(sc, true);
2135                         /*
2136                          * NOTE:
2137                          * hn_stop() only suspends data, so managment
2138                          * stuffs have to be suspended manually here.
2139                          */
2140                         hn_suspend_mgmt(sc);
2141                         hn_synth_detach(sc);
2142                 }
2143                 HN_UNLOCK(sc);
2144                 ether_ifdetach(ifp);
2145         }
2146
2147         ifmedia_removeall(&sc->hn_media);
2148         hn_destroy_rx_data(sc);
2149         hn_destroy_tx_data(sc);
2150
2151         if (sc->hn_tx_taskqs != NULL && sc->hn_tx_taskqs != hn_tx_taskque) {
2152                 int i;
2153
2154                 for (i = 0; i < hn_tx_taskq_cnt; ++i)
2155                         taskqueue_free(sc->hn_tx_taskqs[i]);
2156                 free(sc->hn_tx_taskqs, M_DEVBUF);
2157         }
2158         taskqueue_free(sc->hn_mgmt_taskq0);
2159         if (sc->hn_vf_taskq != NULL)
2160                 taskqueue_free(sc->hn_vf_taskq);
2161
2162         if (sc->hn_xact != NULL) {
2163                 /*
2164                  * Uninstall the orphan handler _before_ the xact is
2165                  * destructed.
2166                  */
2167                 vmbus_chan_unset_orphan(sc->hn_prichan);
2168                 vmbus_xact_ctx_destroy(sc->hn_xact);
2169         }
2170
2171         if_free(ifp);
2172
2173         HN_LOCK_DESTROY(sc);
2174         rm_destroy(&sc->hn_vf_lock);
2175         return (0);
2176 }
2177
2178 static int
2179 hn_shutdown(device_t dev)
2180 {
2181
2182         return (0);
2183 }
2184
2185 static void
2186 hn_link_status(struct hn_softc *sc)
2187 {
2188         uint32_t link_status;
2189         int error;
2190
2191         error = hn_rndis_get_linkstatus(sc, &link_status);
2192         if (error) {
2193                 /* XXX what to do? */
2194                 return;
2195         }
2196
2197         if (link_status == NDIS_MEDIA_STATE_CONNECTED)
2198                 sc->hn_link_flags |= HN_LINK_FLAG_LINKUP;
2199         else
2200                 sc->hn_link_flags &= ~HN_LINK_FLAG_LINKUP;
2201         if_link_state_change(sc->hn_ifp,
2202             (sc->hn_link_flags & HN_LINK_FLAG_LINKUP) ?
2203             LINK_STATE_UP : LINK_STATE_DOWN);
2204 }
2205
2206 static void
2207 hn_link_taskfunc(void *xsc, int pending __unused)
2208 {
2209         struct hn_softc *sc = xsc;
2210
2211         if (sc->hn_link_flags & HN_LINK_FLAG_NETCHG)
2212                 return;
2213         hn_link_status(sc);
2214 }
2215
2216 static void
2217 hn_netchg_init_taskfunc(void *xsc, int pending __unused)
2218 {
2219         struct hn_softc *sc = xsc;
2220
2221         /* Prevent any link status checks from running. */
2222         sc->hn_link_flags |= HN_LINK_FLAG_NETCHG;
2223
2224         /*
2225          * Fake up a [link down --> link up] state change; 5 seconds
2226          * delay is used, which closely simulates miibus reaction
2227          * upon link down event.
2228          */
2229         sc->hn_link_flags &= ~HN_LINK_FLAG_LINKUP;
2230         if_link_state_change(sc->hn_ifp, LINK_STATE_DOWN);
2231         taskqueue_enqueue_timeout(sc->hn_mgmt_taskq0,
2232             &sc->hn_netchg_status, 5 * hz);
2233 }
2234
2235 static void
2236 hn_netchg_status_taskfunc(void *xsc, int pending __unused)
2237 {
2238         struct hn_softc *sc = xsc;
2239
2240         /* Re-allow link status checks. */
2241         sc->hn_link_flags &= ~HN_LINK_FLAG_NETCHG;
2242         hn_link_status(sc);
2243 }
2244
2245 static void
2246 hn_update_link_status(struct hn_softc *sc)
2247 {
2248
2249         if (sc->hn_mgmt_taskq != NULL)
2250                 taskqueue_enqueue(sc->hn_mgmt_taskq, &sc->hn_link_task);
2251 }
2252
2253 static void
2254 hn_change_network(struct hn_softc *sc)
2255 {
2256
2257         if (sc->hn_mgmt_taskq != NULL)
2258                 taskqueue_enqueue(sc->hn_mgmt_taskq, &sc->hn_netchg_init);
2259 }
2260
2261 static __inline int
2262 hn_txdesc_dmamap_load(struct hn_tx_ring *txr, struct hn_txdesc *txd,
2263     struct mbuf **m_head, bus_dma_segment_t *segs, int *nsegs)
2264 {
2265         struct mbuf *m = *m_head;
2266         int error;
2267
2268         KASSERT(txd->chim_index == HN_NVS_CHIM_IDX_INVALID, ("txd uses chim"));
2269
2270         error = bus_dmamap_load_mbuf_sg(txr->hn_tx_data_dtag, txd->data_dmap,
2271             m, segs, nsegs, BUS_DMA_NOWAIT);
2272         if (error == EFBIG) {
2273                 struct mbuf *m_new;
2274
2275                 m_new = m_collapse(m, M_NOWAIT, HN_TX_DATA_SEGCNT_MAX);
2276                 if (m_new == NULL)
2277                         return ENOBUFS;
2278                 else
2279                         *m_head = m = m_new;
2280                 txr->hn_tx_collapsed++;
2281
2282                 error = bus_dmamap_load_mbuf_sg(txr->hn_tx_data_dtag,
2283                     txd->data_dmap, m, segs, nsegs, BUS_DMA_NOWAIT);
2284         }
2285         if (!error) {
2286                 bus_dmamap_sync(txr->hn_tx_data_dtag, txd->data_dmap,
2287                     BUS_DMASYNC_PREWRITE);
2288                 txd->flags |= HN_TXD_FLAG_DMAMAP;
2289         }
2290         return error;
2291 }
2292
2293 static __inline int
2294 hn_txdesc_put(struct hn_tx_ring *txr, struct hn_txdesc *txd)
2295 {
2296
2297         KASSERT((txd->flags & HN_TXD_FLAG_ONLIST) == 0,
2298             ("put an onlist txd %#x", txd->flags));
2299         KASSERT((txd->flags & HN_TXD_FLAG_ONAGG) == 0,
2300             ("put an onagg txd %#x", txd->flags));
2301
2302         KASSERT(txd->refs > 0, ("invalid txd refs %d", txd->refs));
2303         if (atomic_fetchadd_int(&txd->refs, -1) != 1)
2304                 return 0;
2305
2306         if (!STAILQ_EMPTY(&txd->agg_list)) {
2307                 struct hn_txdesc *tmp_txd;
2308
2309                 while ((tmp_txd = STAILQ_FIRST(&txd->agg_list)) != NULL) {
2310                         int freed;
2311
2312                         KASSERT(STAILQ_EMPTY(&tmp_txd->agg_list),
2313                             ("resursive aggregation on aggregated txdesc"));
2314                         KASSERT((tmp_txd->flags & HN_TXD_FLAG_ONAGG),
2315                             ("not aggregated txdesc"));
2316                         KASSERT((tmp_txd->flags & HN_TXD_FLAG_DMAMAP) == 0,
2317                             ("aggregated txdesc uses dmamap"));
2318                         KASSERT(tmp_txd->chim_index == HN_NVS_CHIM_IDX_INVALID,
2319                             ("aggregated txdesc consumes "
2320                              "chimney sending buffer"));
2321                         KASSERT(tmp_txd->chim_size == 0,
2322                             ("aggregated txdesc has non-zero "
2323                              "chimney sending size"));
2324
2325                         STAILQ_REMOVE_HEAD(&txd->agg_list, agg_link);
2326                         tmp_txd->flags &= ~HN_TXD_FLAG_ONAGG;
2327                         freed = hn_txdesc_put(txr, tmp_txd);
2328                         KASSERT(freed, ("failed to free aggregated txdesc"));
2329                 }
2330         }
2331
2332         if (txd->chim_index != HN_NVS_CHIM_IDX_INVALID) {
2333                 KASSERT((txd->flags & HN_TXD_FLAG_DMAMAP) == 0,
2334                     ("chim txd uses dmamap"));
2335                 hn_chim_free(txr->hn_sc, txd->chim_index);
2336                 txd->chim_index = HN_NVS_CHIM_IDX_INVALID;
2337                 txd->chim_size = 0;
2338         } else if (txd->flags & HN_TXD_FLAG_DMAMAP) {
2339                 bus_dmamap_sync(txr->hn_tx_data_dtag,
2340                     txd->data_dmap, BUS_DMASYNC_POSTWRITE);
2341                 bus_dmamap_unload(txr->hn_tx_data_dtag,
2342                     txd->data_dmap);
2343                 txd->flags &= ~HN_TXD_FLAG_DMAMAP;
2344         }
2345
2346         if (txd->m != NULL) {
2347                 m_freem(txd->m);
2348                 txd->m = NULL;
2349         }
2350
2351         txd->flags |= HN_TXD_FLAG_ONLIST;
2352 #ifndef HN_USE_TXDESC_BUFRING
2353         mtx_lock_spin(&txr->hn_txlist_spin);
2354         KASSERT(txr->hn_txdesc_avail >= 0 &&
2355             txr->hn_txdesc_avail < txr->hn_txdesc_cnt,
2356             ("txdesc_put: invalid txd avail %d", txr->hn_txdesc_avail));
2357         txr->hn_txdesc_avail++;
2358         SLIST_INSERT_HEAD(&txr->hn_txlist, txd, link);
2359         mtx_unlock_spin(&txr->hn_txlist_spin);
2360 #else   /* HN_USE_TXDESC_BUFRING */
2361 #ifdef HN_DEBUG
2362         atomic_add_int(&txr->hn_txdesc_avail, 1);
2363 #endif
2364         buf_ring_enqueue(txr->hn_txdesc_br, txd);
2365 #endif  /* !HN_USE_TXDESC_BUFRING */
2366
2367         return 1;
2368 }
2369
2370 static __inline struct hn_txdesc *
2371 hn_txdesc_get(struct hn_tx_ring *txr)
2372 {
2373         struct hn_txdesc *txd;
2374
2375 #ifndef HN_USE_TXDESC_BUFRING
2376         mtx_lock_spin(&txr->hn_txlist_spin);
2377         txd = SLIST_FIRST(&txr->hn_txlist);
2378         if (txd != NULL) {
2379                 KASSERT(txr->hn_txdesc_avail > 0,
2380                     ("txdesc_get: invalid txd avail %d", txr->hn_txdesc_avail));
2381                 txr->hn_txdesc_avail--;
2382                 SLIST_REMOVE_HEAD(&txr->hn_txlist, link);
2383         }
2384         mtx_unlock_spin(&txr->hn_txlist_spin);
2385 #else
2386         txd = buf_ring_dequeue_sc(txr->hn_txdesc_br);
2387 #endif
2388
2389         if (txd != NULL) {
2390 #ifdef HN_USE_TXDESC_BUFRING
2391 #ifdef HN_DEBUG
2392                 atomic_subtract_int(&txr->hn_txdesc_avail, 1);
2393 #endif
2394 #endif  /* HN_USE_TXDESC_BUFRING */
2395                 KASSERT(txd->m == NULL && txd->refs == 0 &&
2396                     STAILQ_EMPTY(&txd->agg_list) &&
2397                     txd->chim_index == HN_NVS_CHIM_IDX_INVALID &&
2398                     txd->chim_size == 0 &&
2399                     (txd->flags & HN_TXD_FLAG_ONLIST) &&
2400                     (txd->flags & HN_TXD_FLAG_ONAGG) == 0 &&
2401                     (txd->flags & HN_TXD_FLAG_DMAMAP) == 0, ("invalid txd"));
2402                 txd->flags &= ~HN_TXD_FLAG_ONLIST;
2403                 txd->refs = 1;
2404         }
2405         return txd;
2406 }
2407
2408 static __inline void
2409 hn_txdesc_hold(struct hn_txdesc *txd)
2410 {
2411
2412         /* 0->1 transition will never work */
2413         KASSERT(txd->refs > 0, ("invalid txd refs %d", txd->refs));
2414         atomic_add_int(&txd->refs, 1);
2415 }
2416
2417 static __inline void
2418 hn_txdesc_agg(struct hn_txdesc *agg_txd, struct hn_txdesc *txd)
2419 {
2420
2421         KASSERT((agg_txd->flags & HN_TXD_FLAG_ONAGG) == 0,
2422             ("recursive aggregation on aggregating txdesc"));
2423
2424         KASSERT((txd->flags & HN_TXD_FLAG_ONAGG) == 0,
2425             ("already aggregated"));
2426         KASSERT(STAILQ_EMPTY(&txd->agg_list),
2427             ("recursive aggregation on to-be-aggregated txdesc"));
2428
2429         txd->flags |= HN_TXD_FLAG_ONAGG;
2430         STAILQ_INSERT_TAIL(&agg_txd->agg_list, txd, agg_link);
2431 }
2432
2433 static bool
2434 hn_tx_ring_pending(struct hn_tx_ring *txr)
2435 {
2436         bool pending = false;
2437
2438 #ifndef HN_USE_TXDESC_BUFRING
2439         mtx_lock_spin(&txr->hn_txlist_spin);
2440         if (txr->hn_txdesc_avail != txr->hn_txdesc_cnt)
2441                 pending = true;
2442         mtx_unlock_spin(&txr->hn_txlist_spin);
2443 #else
2444         if (!buf_ring_full(txr->hn_txdesc_br))
2445                 pending = true;
2446 #endif
2447         return (pending);
2448 }
2449
2450 static __inline void
2451 hn_txeof(struct hn_tx_ring *txr)
2452 {
2453         txr->hn_has_txeof = 0;
2454         txr->hn_txeof(txr);
2455 }
2456
2457 static void
2458 hn_txpkt_done(struct hn_nvs_sendctx *sndc, struct hn_softc *sc,
2459     struct vmbus_channel *chan, const void *data __unused, int dlen __unused)
2460 {
2461         struct hn_txdesc *txd = sndc->hn_cbarg;
2462         struct hn_tx_ring *txr;
2463
2464         txr = txd->txr;
2465         KASSERT(txr->hn_chan == chan,
2466             ("channel mismatch, on chan%u, should be chan%u",
2467              vmbus_chan_id(chan), vmbus_chan_id(txr->hn_chan)));
2468
2469         txr->hn_has_txeof = 1;
2470         hn_txdesc_put(txr, txd);
2471
2472         ++txr->hn_txdone_cnt;
2473         if (txr->hn_txdone_cnt >= HN_EARLY_TXEOF_THRESH) {
2474                 txr->hn_txdone_cnt = 0;
2475                 if (txr->hn_oactive)
2476                         hn_txeof(txr);
2477         }
2478 }
2479
2480 static void
2481 hn_chan_rollup(struct hn_rx_ring *rxr, struct hn_tx_ring *txr)
2482 {
2483 #if defined(INET) || defined(INET6)
2484         struct lro_ctrl *lro = &rxr->hn_lro;
2485         struct lro_entry *queued;
2486
2487         while ((queued = SLIST_FIRST(&lro->lro_active)) != NULL) {
2488                 SLIST_REMOVE_HEAD(&lro->lro_active, next);
2489                 tcp_lro_flush(lro, queued);
2490         }
2491 #endif
2492
2493         /*
2494          * NOTE:
2495          * 'txr' could be NULL, if multiple channels and
2496          * ifnet.if_start method are enabled.
2497          */
2498         if (txr == NULL || !txr->hn_has_txeof)
2499                 return;
2500
2501         txr->hn_txdone_cnt = 0;
2502         hn_txeof(txr);
2503 }
2504
2505 static __inline uint32_t
2506 hn_rndis_pktmsg_offset(uint32_t ofs)
2507 {
2508
2509         KASSERT(ofs >= sizeof(struct rndis_packet_msg),
2510             ("invalid RNDIS packet msg offset %u", ofs));
2511         return (ofs - __offsetof(struct rndis_packet_msg, rm_dataoffset));
2512 }
2513
2514 static __inline void *
2515 hn_rndis_pktinfo_append(struct rndis_packet_msg *pkt, size_t pktsize,
2516     size_t pi_dlen, uint32_t pi_type)
2517 {
2518         const size_t pi_size = HN_RNDIS_PKTINFO_SIZE(pi_dlen);
2519         struct rndis_pktinfo *pi;
2520
2521         KASSERT((pi_size & RNDIS_PACKET_MSG_OFFSET_ALIGNMASK) == 0,
2522             ("unaligned pktinfo size %zu, pktinfo dlen %zu", pi_size, pi_dlen));
2523
2524         /*
2525          * Per-packet-info does not move; it only grows.
2526          *
2527          * NOTE:
2528          * rm_pktinfooffset in this phase counts from the beginning
2529          * of rndis_packet_msg.
2530          */
2531         KASSERT(pkt->rm_pktinfooffset + pkt->rm_pktinfolen + pi_size <= pktsize,
2532             ("%u pktinfo overflows RNDIS packet msg", pi_type));
2533         pi = (struct rndis_pktinfo *)((uint8_t *)pkt + pkt->rm_pktinfooffset +
2534             pkt->rm_pktinfolen);
2535         pkt->rm_pktinfolen += pi_size;
2536
2537         pi->rm_size = pi_size;
2538         pi->rm_type = pi_type;
2539         pi->rm_pktinfooffset = RNDIS_PKTINFO_OFFSET;
2540
2541         return (pi->rm_data);
2542 }
2543
2544 static __inline int
2545 hn_flush_txagg(struct ifnet *ifp, struct hn_tx_ring *txr)
2546 {
2547         struct hn_txdesc *txd;
2548         struct mbuf *m;
2549         int error, pkts;
2550
2551         txd = txr->hn_agg_txd;
2552         KASSERT(txd != NULL, ("no aggregate txdesc"));
2553
2554         /*
2555          * Since hn_txpkt() will reset this temporary stat, save
2556          * it now, so that oerrors can be updated properly, if
2557          * hn_txpkt() ever fails.
2558          */
2559         pkts = txr->hn_stat_pkts;
2560
2561         /*
2562          * Since txd's mbuf will _not_ be freed upon hn_txpkt()
2563          * failure, save it for later freeing, if hn_txpkt() ever
2564          * fails.
2565          */
2566         m = txd->m;
2567         error = hn_txpkt(ifp, txr, txd);
2568         if (__predict_false(error)) {
2569                 /* txd is freed, but m is not. */
2570                 m_freem(m);
2571
2572                 txr->hn_flush_failed++;
2573                 if_inc_counter(ifp, IFCOUNTER_OERRORS, pkts);
2574         }
2575
2576         /* Reset all aggregation states. */
2577         txr->hn_agg_txd = NULL;
2578         txr->hn_agg_szleft = 0;
2579         txr->hn_agg_pktleft = 0;
2580         txr->hn_agg_prevpkt = NULL;
2581
2582         return (error);
2583 }
2584
2585 static void *
2586 hn_try_txagg(struct ifnet *ifp, struct hn_tx_ring *txr, struct hn_txdesc *txd,
2587     int pktsize)
2588 {
2589         void *chim;
2590
2591         if (txr->hn_agg_txd != NULL) {
2592                 if (txr->hn_agg_pktleft >= 1 && txr->hn_agg_szleft > pktsize) {
2593                         struct hn_txdesc *agg_txd = txr->hn_agg_txd;
2594                         struct rndis_packet_msg *pkt = txr->hn_agg_prevpkt;
2595                         int olen;
2596
2597                         /*
2598                          * Update the previous RNDIS packet's total length,
2599                          * it can be increased due to the mandatory alignment
2600                          * padding for this RNDIS packet.  And update the
2601                          * aggregating txdesc's chimney sending buffer size
2602                          * accordingly.
2603                          *
2604                          * XXX
2605                          * Zero-out the padding, as required by the RNDIS spec.
2606                          */
2607                         olen = pkt->rm_len;
2608                         pkt->rm_len = roundup2(olen, txr->hn_agg_align);
2609                         agg_txd->chim_size += pkt->rm_len - olen;
2610
2611                         /* Link this txdesc to the parent. */
2612                         hn_txdesc_agg(agg_txd, txd);
2613
2614                         chim = (uint8_t *)pkt + pkt->rm_len;
2615                         /* Save the current packet for later fixup. */
2616                         txr->hn_agg_prevpkt = chim;
2617
2618                         txr->hn_agg_pktleft--;
2619                         txr->hn_agg_szleft -= pktsize;
2620                         if (txr->hn_agg_szleft <=
2621                             HN_PKTSIZE_MIN(txr->hn_agg_align)) {
2622                                 /*
2623                                  * Probably can't aggregate more packets,
2624                                  * flush this aggregating txdesc proactively.
2625                                  */
2626                                 txr->hn_agg_pktleft = 0;
2627                         }
2628                         /* Done! */
2629                         return (chim);
2630                 }
2631                 hn_flush_txagg(ifp, txr);
2632         }
2633         KASSERT(txr->hn_agg_txd == NULL, ("lingering aggregating txdesc"));
2634
2635         txr->hn_tx_chimney_tried++;
2636         txd->chim_index = hn_chim_alloc(txr->hn_sc);
2637         if (txd->chim_index == HN_NVS_CHIM_IDX_INVALID)
2638                 return (NULL);
2639         txr->hn_tx_chimney++;
2640
2641         chim = txr->hn_sc->hn_chim +
2642             (txd->chim_index * txr->hn_sc->hn_chim_szmax);
2643
2644         if (txr->hn_agg_pktmax > 1 &&
2645             txr->hn_agg_szmax > pktsize + HN_PKTSIZE_MIN(txr->hn_agg_align)) {
2646                 txr->hn_agg_txd = txd;
2647                 txr->hn_agg_pktleft = txr->hn_agg_pktmax - 1;
2648                 txr->hn_agg_szleft = txr->hn_agg_szmax - pktsize;
2649                 txr->hn_agg_prevpkt = chim;
2650         }
2651         return (chim);
2652 }
2653
2654 /*
2655  * NOTE:
2656  * If this function fails, then both txd and m_head0 will be freed.
2657  */
2658 static int
2659 hn_encap(struct ifnet *ifp, struct hn_tx_ring *txr, struct hn_txdesc *txd,
2660     struct mbuf **m_head0)
2661 {
2662         bus_dma_segment_t segs[HN_TX_DATA_SEGCNT_MAX];
2663         int error, nsegs, i;
2664         struct mbuf *m_head = *m_head0;
2665         struct rndis_packet_msg *pkt;
2666         uint32_t *pi_data;
2667         void *chim = NULL;
2668         int pkt_hlen, pkt_size;
2669
2670         pkt = txd->rndis_pkt;
2671         pkt_size = HN_PKTSIZE(m_head, txr->hn_agg_align);
2672         if (pkt_size < txr->hn_chim_size) {
2673                 chim = hn_try_txagg(ifp, txr, txd, pkt_size);
2674                 if (chim != NULL)
2675                         pkt = chim;
2676         } else {
2677                 if (txr->hn_agg_txd != NULL)
2678                         hn_flush_txagg(ifp, txr);
2679         }
2680
2681         pkt->rm_type = REMOTE_NDIS_PACKET_MSG;
2682         pkt->rm_len = m_head->m_pkthdr.len;
2683         pkt->rm_dataoffset = 0;
2684         pkt->rm_datalen = m_head->m_pkthdr.len;
2685         pkt->rm_oobdataoffset = 0;
2686         pkt->rm_oobdatalen = 0;
2687         pkt->rm_oobdataelements = 0;
2688         pkt->rm_pktinfooffset = sizeof(*pkt);
2689         pkt->rm_pktinfolen = 0;
2690         pkt->rm_vchandle = 0;
2691         pkt->rm_reserved = 0;
2692
2693         if (txr->hn_tx_flags & HN_TX_FLAG_HASHVAL) {
2694                 /*
2695                  * Set the hash value for this packet, so that the host could
2696                  * dispatch the TX done event for this packet back to this TX
2697                  * ring's channel.
2698                  */
2699                 pi_data = hn_rndis_pktinfo_append(pkt, HN_RNDIS_PKT_LEN,
2700                     HN_NDIS_HASH_VALUE_SIZE, HN_NDIS_PKTINFO_TYPE_HASHVAL);
2701                 *pi_data = txr->hn_tx_idx;
2702         }
2703
2704         if (m_head->m_flags & M_VLANTAG) {
2705                 pi_data = hn_rndis_pktinfo_append(pkt, HN_RNDIS_PKT_LEN,
2706                     NDIS_VLAN_INFO_SIZE, NDIS_PKTINFO_TYPE_VLAN);
2707                 *pi_data = NDIS_VLAN_INFO_MAKE(
2708                     EVL_VLANOFTAG(m_head->m_pkthdr.ether_vtag),
2709                     EVL_PRIOFTAG(m_head->m_pkthdr.ether_vtag),
2710                     EVL_CFIOFTAG(m_head->m_pkthdr.ether_vtag));
2711         }
2712
2713         if (m_head->m_pkthdr.csum_flags & CSUM_TSO) {
2714 #if defined(INET6) || defined(INET)
2715                 pi_data = hn_rndis_pktinfo_append(pkt, HN_RNDIS_PKT_LEN,
2716                     NDIS_LSO2_INFO_SIZE, NDIS_PKTINFO_TYPE_LSO);
2717 #ifdef INET
2718                 if (m_head->m_pkthdr.csum_flags & CSUM_IP_TSO) {
2719                         *pi_data = NDIS_LSO2_INFO_MAKEIPV4(0,
2720                             m_head->m_pkthdr.tso_segsz);
2721                 }
2722 #endif
2723 #if defined(INET6) && defined(INET)
2724                 else
2725 #endif
2726 #ifdef INET6
2727                 {
2728                         *pi_data = NDIS_LSO2_INFO_MAKEIPV6(0,
2729                             m_head->m_pkthdr.tso_segsz);
2730                 }
2731 #endif
2732 #endif  /* INET6 || INET */
2733         } else if (m_head->m_pkthdr.csum_flags & txr->hn_csum_assist) {
2734                 pi_data = hn_rndis_pktinfo_append(pkt, HN_RNDIS_PKT_LEN,
2735                     NDIS_TXCSUM_INFO_SIZE, NDIS_PKTINFO_TYPE_CSUM);
2736                 if (m_head->m_pkthdr.csum_flags &
2737                     (CSUM_IP6_TCP | CSUM_IP6_UDP)) {
2738                         *pi_data = NDIS_TXCSUM_INFO_IPV6;
2739                 } else {
2740                         *pi_data = NDIS_TXCSUM_INFO_IPV4;
2741                         if (m_head->m_pkthdr.csum_flags & CSUM_IP)
2742                                 *pi_data |= NDIS_TXCSUM_INFO_IPCS;
2743                 }
2744
2745                 if (m_head->m_pkthdr.csum_flags & (CSUM_IP_TCP | CSUM_IP6_TCP))
2746                         *pi_data |= NDIS_TXCSUM_INFO_TCPCS;
2747                 else if (m_head->m_pkthdr.csum_flags &
2748                     (CSUM_IP_UDP | CSUM_IP6_UDP))
2749                         *pi_data |= NDIS_TXCSUM_INFO_UDPCS;
2750         }
2751
2752         pkt_hlen = pkt->rm_pktinfooffset + pkt->rm_pktinfolen;
2753         /* Fixup RNDIS packet message total length */
2754         pkt->rm_len += pkt_hlen;
2755         /* Convert RNDIS packet message offsets */
2756         pkt->rm_dataoffset = hn_rndis_pktmsg_offset(pkt_hlen);
2757         pkt->rm_pktinfooffset = hn_rndis_pktmsg_offset(pkt->rm_pktinfooffset);
2758
2759         /*
2760          * Fast path: Chimney sending.
2761          */
2762         if (chim != NULL) {
2763                 struct hn_txdesc *tgt_txd = txd;
2764
2765                 if (txr->hn_agg_txd != NULL) {
2766                         tgt_txd = txr->hn_agg_txd;
2767 #ifdef INVARIANTS
2768                         *m_head0 = NULL;
2769 #endif
2770                 }
2771
2772                 KASSERT(pkt == chim,
2773                     ("RNDIS pkt not in chimney sending buffer"));
2774                 KASSERT(tgt_txd->chim_index != HN_NVS_CHIM_IDX_INVALID,
2775                     ("chimney sending buffer is not used"));
2776                 tgt_txd->chim_size += pkt->rm_len;
2777
2778                 m_copydata(m_head, 0, m_head->m_pkthdr.len,
2779                     ((uint8_t *)chim) + pkt_hlen);
2780
2781                 txr->hn_gpa_cnt = 0;
2782                 txr->hn_sendpkt = hn_txpkt_chim;
2783                 goto done;
2784         }
2785
2786         KASSERT(txr->hn_agg_txd == NULL, ("aggregating sglist txdesc"));
2787         KASSERT(txd->chim_index == HN_NVS_CHIM_IDX_INVALID,
2788             ("chimney buffer is used"));
2789         KASSERT(pkt == txd->rndis_pkt, ("RNDIS pkt not in txdesc"));
2790
2791         error = hn_txdesc_dmamap_load(txr, txd, &m_head, segs, &nsegs);
2792         if (__predict_false(error)) {
2793                 int freed;
2794
2795                 /*
2796                  * This mbuf is not linked w/ the txd yet, so free it now.
2797                  */
2798                 m_freem(m_head);
2799                 *m_head0 = NULL;
2800
2801                 freed = hn_txdesc_put(txr, txd);
2802                 KASSERT(freed != 0,
2803                     ("fail to free txd upon txdma error"));
2804
2805                 txr->hn_txdma_failed++;
2806                 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
2807                 return error;
2808         }
2809         *m_head0 = m_head;
2810
2811         /* +1 RNDIS packet message */
2812         txr->hn_gpa_cnt = nsegs + 1;
2813
2814         /* send packet with page buffer */
2815         txr->hn_gpa[0].gpa_page = atop(txd->rndis_pkt_paddr);
2816         txr->hn_gpa[0].gpa_ofs = txd->rndis_pkt_paddr & PAGE_MASK;
2817         txr->hn_gpa[0].gpa_len = pkt_hlen;
2818
2819         /*
2820          * Fill the page buffers with mbuf info after the page
2821          * buffer for RNDIS packet message.
2822          */
2823         for (i = 0; i < nsegs; ++i) {
2824                 struct vmbus_gpa *gpa = &txr->hn_gpa[i + 1];
2825
2826                 gpa->gpa_page = atop(segs[i].ds_addr);
2827                 gpa->gpa_ofs = segs[i].ds_addr & PAGE_MASK;
2828                 gpa->gpa_len = segs[i].ds_len;
2829         }
2830
2831         txd->chim_index = HN_NVS_CHIM_IDX_INVALID;
2832         txd->chim_size = 0;
2833         txr->hn_sendpkt = hn_txpkt_sglist;
2834 done:
2835         txd->m = m_head;
2836
2837         /* Set the completion routine */
2838         hn_nvs_sendctx_init(&txd->send_ctx, hn_txpkt_done, txd);
2839
2840         /* Update temporary stats for later use. */
2841         txr->hn_stat_pkts++;
2842         txr->hn_stat_size += m_head->m_pkthdr.len;
2843         if (m_head->m_flags & M_MCAST)
2844                 txr->hn_stat_mcasts++;
2845
2846         return 0;
2847 }
2848
2849 /*
2850  * NOTE:
2851  * If this function fails, then txd will be freed, but the mbuf
2852  * associated w/ the txd will _not_ be freed.
2853  */
2854 static int
2855 hn_txpkt(struct ifnet *ifp, struct hn_tx_ring *txr, struct hn_txdesc *txd)
2856 {
2857         int error, send_failed = 0, has_bpf;
2858
2859 again:
2860         has_bpf = bpf_peers_present(ifp->if_bpf);
2861         if (has_bpf) {
2862                 /*
2863                  * Make sure that this txd and any aggregated txds are not
2864                  * freed before ETHER_BPF_MTAP.
2865                  */
2866                 hn_txdesc_hold(txd);
2867         }
2868         error = txr->hn_sendpkt(txr, txd);
2869         if (!error) {
2870                 if (has_bpf) {
2871                         const struct hn_txdesc *tmp_txd;
2872
2873                         ETHER_BPF_MTAP(ifp, txd->m);
2874                         STAILQ_FOREACH(tmp_txd, &txd->agg_list, agg_link)
2875                                 ETHER_BPF_MTAP(ifp, tmp_txd->m);
2876                 }
2877
2878                 if_inc_counter(ifp, IFCOUNTER_OPACKETS, txr->hn_stat_pkts);
2879 #ifdef HN_IFSTART_SUPPORT
2880                 if (!hn_use_if_start)
2881 #endif
2882                 {
2883                         if_inc_counter(ifp, IFCOUNTER_OBYTES,
2884                             txr->hn_stat_size);
2885                         if (txr->hn_stat_mcasts != 0) {
2886                                 if_inc_counter(ifp, IFCOUNTER_OMCASTS,
2887                                     txr->hn_stat_mcasts);
2888                         }
2889                 }
2890                 txr->hn_pkts += txr->hn_stat_pkts;
2891                 txr->hn_sends++;
2892         }
2893         if (has_bpf)
2894                 hn_txdesc_put(txr, txd);
2895
2896         if (__predict_false(error)) {
2897                 int freed;
2898
2899                 /*
2900                  * This should "really rarely" happen.
2901                  *
2902                  * XXX Too many RX to be acked or too many sideband
2903                  * commands to run?  Ask netvsc_channel_rollup()
2904                  * to kick start later.
2905                  */
2906                 txr->hn_has_txeof = 1;
2907                 if (!send_failed) {
2908                         txr->hn_send_failed++;
2909                         send_failed = 1;
2910                         /*
2911                          * Try sending again after set hn_has_txeof;
2912                          * in case that we missed the last
2913                          * netvsc_channel_rollup().
2914                          */
2915                         goto again;
2916                 }
2917                 if_printf(ifp, "send failed\n");
2918
2919                 /*
2920                  * Caller will perform further processing on the
2921                  * associated mbuf, so don't free it in hn_txdesc_put();
2922                  * only unload it from the DMA map in hn_txdesc_put(),
2923                  * if it was loaded.
2924                  */
2925                 txd->m = NULL;
2926                 freed = hn_txdesc_put(txr, txd);
2927                 KASSERT(freed != 0,
2928                     ("fail to free txd upon send error"));
2929
2930                 txr->hn_send_failed++;
2931         }
2932
2933         /* Reset temporary stats, after this sending is done. */
2934         txr->hn_stat_size = 0;
2935         txr->hn_stat_pkts = 0;
2936         txr->hn_stat_mcasts = 0;
2937
2938         return (error);
2939 }
2940
2941 /*
2942  * Append the specified data to the indicated mbuf chain,
2943  * Extend the mbuf chain if the new data does not fit in
2944  * existing space.
2945  *
2946  * This is a minor rewrite of m_append() from sys/kern/uipc_mbuf.c.
2947  * There should be an equivalent in the kernel mbuf code,
2948  * but there does not appear to be one yet.
2949  *
2950  * Differs from m_append() in that additional mbufs are
2951  * allocated with cluster size MJUMPAGESIZE, and filled
2952  * accordingly.
2953  *
2954  * Return 1 if able to complete the job; otherwise 0.
2955  */
2956 static int
2957 hv_m_append(struct mbuf *m0, int len, c_caddr_t cp)
2958 {
2959         struct mbuf *m, *n;
2960         int remainder, space;
2961
2962         for (m = m0; m->m_next != NULL; m = m->m_next)
2963                 ;
2964         remainder = len;
2965         space = M_TRAILINGSPACE(m);
2966         if (space > 0) {
2967                 /*
2968                  * Copy into available space.
2969                  */
2970                 if (space > remainder)
2971                         space = remainder;
2972                 bcopy(cp, mtod(m, caddr_t) + m->m_len, space);
2973                 m->m_len += space;
2974                 cp += space;
2975                 remainder -= space;
2976         }
2977         while (remainder > 0) {
2978                 /*
2979                  * Allocate a new mbuf; could check space
2980                  * and allocate a cluster instead.
2981                  */
2982                 n = m_getjcl(M_DONTWAIT, m->m_type, 0, MJUMPAGESIZE);
2983                 if (n == NULL)
2984                         break;
2985                 n->m_len = min(MJUMPAGESIZE, remainder);
2986                 bcopy(cp, mtod(n, caddr_t), n->m_len);
2987                 cp += n->m_len;
2988                 remainder -= n->m_len;
2989                 m->m_next = n;
2990                 m = n;
2991         }
2992         if (m0->m_flags & M_PKTHDR)
2993                 m0->m_pkthdr.len += len - remainder;
2994
2995         return (remainder == 0);
2996 }
2997
2998 #if defined(INET) || defined(INET6)
2999 static __inline int
3000 hn_lro_rx(struct lro_ctrl *lc, struct mbuf *m)
3001 {
3002 #if __FreeBSD_version >= 1100095
3003         if (hn_lro_mbufq_depth) {
3004                 tcp_lro_queue_mbuf(lc, m);
3005                 return 0;
3006         }
3007 #endif
3008         return tcp_lro_rx(lc, m, 0);
3009 }
3010 #endif
3011
3012 static int
3013 hn_rxpkt(struct hn_rx_ring *rxr, const void *data, int dlen,
3014     const struct hn_rxinfo *info)
3015 {
3016         struct ifnet *ifp, *hn_ifp = rxr->hn_ifp;
3017         struct mbuf *m_new;
3018         int size, do_lro = 0, do_csum = 1;
3019         int hash_type = M_HASHTYPE_OPAQUE;
3020
3021         /*
3022          * If the non-transparent mode VF is active, inject this packet
3023          * into the VF.
3024          */
3025         ifp = rxr->hn_rxvf_ifp ? rxr->hn_rxvf_ifp : hn_ifp;
3026
3027         if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
3028                 /*
3029                  * NOTE:
3030                  * See the NOTE of hn_rndis_init_fixat().  This
3031                  * function can be reached, immediately after the
3032                  * RNDIS is initialized but before the ifnet is
3033                  * setup on the hn_attach() path; drop the unexpected
3034                  * packets.
3035                  */
3036                 return (0);
3037         }
3038
3039         if (__predict_false(dlen < ETHER_HDR_LEN)) {
3040                 if_inc_counter(hn_ifp, IFCOUNTER_IERRORS, 1);
3041                 return (0);
3042         }
3043
3044         if (dlen <= MHLEN) {
3045                 m_new = m_gethdr(M_NOWAIT, MT_DATA);
3046                 if (m_new == NULL) {
3047                         if_inc_counter(hn_ifp, IFCOUNTER_IQDROPS, 1);
3048                         return (0);
3049                 }
3050                 memcpy(mtod(m_new, void *), data, dlen);
3051                 m_new->m_pkthdr.len = m_new->m_len = dlen;
3052                 rxr->hn_small_pkts++;
3053         } else {
3054                 /*
3055                  * Get an mbuf with a cluster.  For packets 2K or less,
3056                  * get a standard 2K cluster.  For anything larger, get a
3057                  * 4K cluster.  Any buffers larger than 4K can cause problems
3058                  * if looped around to the Hyper-V TX channel, so avoid them.
3059                  */
3060                 size = MCLBYTES;
3061                 if (dlen > MCLBYTES) {
3062                         /* 4096 */
3063                         size = MJUMPAGESIZE;
3064                 }
3065
3066                 m_new = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, size);
3067                 if (m_new == NULL) {
3068                         if_inc_counter(hn_ifp, IFCOUNTER_IQDROPS, 1);
3069                         return (0);
3070                 }
3071
3072                 hv_m_append(m_new, dlen, data);
3073         }
3074         m_new->m_pkthdr.rcvif = ifp;
3075
3076         if (__predict_false((hn_ifp->if_capenable & IFCAP_RXCSUM) == 0))
3077                 do_csum = 0;
3078
3079         /* receive side checksum offload */
3080         if (info->csum_info != HN_NDIS_RXCSUM_INFO_INVALID) {
3081                 /* IP csum offload */
3082                 if ((info->csum_info & NDIS_RXCSUM_INFO_IPCS_OK) && do_csum) {
3083                         m_new->m_pkthdr.csum_flags |=
3084                             (CSUM_IP_CHECKED | CSUM_IP_VALID);
3085                         rxr->hn_csum_ip++;
3086                 }
3087
3088                 /* TCP/UDP csum offload */
3089                 if ((info->csum_info & (NDIS_RXCSUM_INFO_UDPCS_OK |
3090                      NDIS_RXCSUM_INFO_TCPCS_OK)) && do_csum) {
3091                         m_new->m_pkthdr.csum_flags |=
3092                             (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
3093                         m_new->m_pkthdr.csum_data = 0xffff;
3094                         if (info->csum_info & NDIS_RXCSUM_INFO_TCPCS_OK)
3095                                 rxr->hn_csum_tcp++;
3096                         else
3097                                 rxr->hn_csum_udp++;
3098                 }
3099
3100                 /*
3101                  * XXX
3102                  * As of this write (Oct 28th, 2016), host side will turn
3103                  * on only TCPCS_OK and IPCS_OK even for UDP datagrams, so
3104                  * the do_lro setting here is actually _not_ accurate.  We
3105                  * depend on the RSS hash type check to reset do_lro.
3106                  */
3107                 if ((info->csum_info &
3108                      (NDIS_RXCSUM_INFO_TCPCS_OK | NDIS_RXCSUM_INFO_IPCS_OK)) ==
3109                     (NDIS_RXCSUM_INFO_TCPCS_OK | NDIS_RXCSUM_INFO_IPCS_OK))
3110                         do_lro = 1;
3111         } else {
3112                 const struct ether_header *eh;
3113                 uint16_t etype;
3114                 int hoff;
3115
3116                 hoff = sizeof(*eh);
3117                 /* Checked at the beginning of this function. */
3118                 KASSERT(m_new->m_len >= hoff, ("not ethernet frame"));
3119
3120                 eh = mtod(m_new, struct ether_header *);
3121                 etype = ntohs(eh->ether_type);
3122                 if (etype == ETHERTYPE_VLAN) {
3123                         const struct ether_vlan_header *evl;
3124
3125                         hoff = sizeof(*evl);
3126                         if (m_new->m_len < hoff)
3127                                 goto skip;
3128                         evl = mtod(m_new, struct ether_vlan_header *);
3129                         etype = ntohs(evl->evl_proto);
3130                 }
3131
3132                 if (etype == ETHERTYPE_IP) {
3133                         int pr;
3134
3135                         pr = hn_check_iplen(m_new, hoff);
3136                         if (pr == IPPROTO_TCP) {
3137                                 if (do_csum &&
3138                                     (rxr->hn_trust_hcsum &
3139                                      HN_TRUST_HCSUM_TCP)) {
3140                                         rxr->hn_csum_trusted++;
3141                                         m_new->m_pkthdr.csum_flags |=
3142                                            (CSUM_IP_CHECKED | CSUM_IP_VALID |
3143                                             CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
3144                                         m_new->m_pkthdr.csum_data = 0xffff;
3145                                 }
3146                                 do_lro = 1;
3147                         } else if (pr == IPPROTO_UDP) {
3148                                 if (do_csum &&
3149                                     (rxr->hn_trust_hcsum &
3150                                      HN_TRUST_HCSUM_UDP)) {
3151                                         rxr->hn_csum_trusted++;
3152                                         m_new->m_pkthdr.csum_flags |=
3153                                            (CSUM_IP_CHECKED | CSUM_IP_VALID |
3154                                             CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
3155                                         m_new->m_pkthdr.csum_data = 0xffff;
3156                                 }
3157                         } else if (pr != IPPROTO_DONE && do_csum &&
3158                             (rxr->hn_trust_hcsum & HN_TRUST_HCSUM_IP)) {
3159                                 rxr->hn_csum_trusted++;
3160                                 m_new->m_pkthdr.csum_flags |=
3161                                     (CSUM_IP_CHECKED | CSUM_IP_VALID);
3162                         }
3163                 }
3164         }
3165 skip:
3166         if (info->vlan_info != HN_NDIS_VLAN_INFO_INVALID) {
3167                 m_new->m_pkthdr.ether_vtag = EVL_MAKETAG(
3168                     NDIS_VLAN_INFO_ID(info->vlan_info),
3169                     NDIS_VLAN_INFO_PRI(info->vlan_info),
3170                     NDIS_VLAN_INFO_CFI(info->vlan_info));
3171                 m_new->m_flags |= M_VLANTAG;
3172         }
3173
3174         /*
3175          * If VF is activated (tranparent/non-transparent mode does not
3176          * matter here).
3177          *
3178          * - Don't setup mbuf hash, if 'options RSS' is set.
3179          *
3180          *   In Azure, when VF is activated, TCP SYN and SYN|ACK go
3181          *   through hn(4) while the rest of segments and ACKs belonging
3182          *   to the same TCP 4-tuple go through the VF.  So don't setup
3183          *   mbuf hash, if a VF is activated and 'options RSS' is not
3184          *   enabled.  hn(4) and the VF may use neither the same RSS
3185          *   hash key nor the same RSS hash function, so the hash value
3186          *   for packets belonging to the same flow could be different!
3187          *
3188          * - Disable LRO
3189          *
3190          *   hn(4) will only receive broadcast packets, multicast packets,
3191          *   TCP SYN and SYN|ACK (in Azure), LRO is useless for these
3192          *   packet types.
3193          *
3194          *   For non-transparent, we definitely _cannot_ enable LRO at
3195          *   all, since the LRO flush will use hn(4) as the receiving
3196          *   interface; i.e. hn_ifp->if_input(hn_ifp, m).
3197          */
3198         if (hn_ifp != ifp || (rxr->hn_rx_flags & HN_RX_FLAG_XPNT_VF)) {
3199                 do_lro = 0;     /* disable LRO. */
3200 #ifndef RSS
3201                 goto skip_hash; /* skip mbuf hash setup */
3202 #endif
3203         }
3204
3205         if (info->hash_info != HN_NDIS_HASH_INFO_INVALID) {
3206                 rxr->hn_rss_pkts++;
3207                 m_new->m_pkthdr.flowid = info->hash_value;
3208                 if ((info->hash_info & NDIS_HASH_FUNCTION_MASK) ==
3209                     NDIS_HASH_FUNCTION_TOEPLITZ) {
3210                         uint32_t type = (info->hash_info & NDIS_HASH_TYPE_MASK);
3211
3212                         /*
3213                          * NOTE:
3214                          * do_lro is resetted, if the hash types are not TCP
3215                          * related.  See the comment in the above csum_flags
3216                          * setup section.
3217                          */
3218                         switch (type) {
3219                         case NDIS_HASH_IPV4:
3220                                 hash_type = M_HASHTYPE_RSS_IPV4;
3221                                 do_lro = 0;
3222                                 break;
3223
3224                         case NDIS_HASH_TCP_IPV4:
3225                                 hash_type = M_HASHTYPE_RSS_TCP_IPV4;
3226                                 break;
3227
3228                         case NDIS_HASH_IPV6:
3229                                 hash_type = M_HASHTYPE_RSS_IPV6;
3230                                 do_lro = 0;
3231                                 break;
3232
3233                         case NDIS_HASH_IPV6_EX:
3234                                 hash_type = M_HASHTYPE_RSS_IPV6_EX;
3235                                 do_lro = 0;
3236                                 break;
3237
3238                         case NDIS_HASH_TCP_IPV6:
3239                                 hash_type = M_HASHTYPE_RSS_TCP_IPV6;
3240                                 break;
3241
3242                         case NDIS_HASH_TCP_IPV6_EX:
3243                                 hash_type = M_HASHTYPE_RSS_TCP_IPV6_EX;
3244                                 break;
3245                         }
3246                 }
3247         } else {
3248                 m_new->m_pkthdr.flowid = rxr->hn_rx_idx;
3249         }
3250         M_HASHTYPE_SET(m_new, hash_type);
3251
3252 #ifndef RSS
3253 skip_hash:
3254 #endif
3255         if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
3256         if (hn_ifp != ifp) {
3257                 const struct ether_header *eh;
3258
3259                 /*
3260                  * Non-transparent mode VF is activated.
3261                  */
3262
3263                 /*
3264                  * Allow tapping on hn(4).
3265                  */
3266                 ETHER_BPF_MTAP(hn_ifp, m_new);
3267
3268                 /*
3269                  * Update hn(4)'s stats.
3270                  */
3271                 if_inc_counter(hn_ifp, IFCOUNTER_IPACKETS, 1);
3272                 if_inc_counter(hn_ifp, IFCOUNTER_IBYTES, m_new->m_pkthdr.len);
3273                 /* Checked at the beginning of this function. */
3274                 KASSERT(m_new->m_len >= ETHER_HDR_LEN, ("not ethernet frame"));
3275                 eh = mtod(m_new, struct ether_header *);
3276                 if (ETHER_IS_MULTICAST(eh->ether_dhost))
3277                         if_inc_counter(hn_ifp, IFCOUNTER_IMCASTS, 1);
3278         }
3279         rxr->hn_pkts++;
3280
3281         if ((hn_ifp->if_capenable & IFCAP_LRO) && do_lro) {
3282 #if defined(INET) || defined(INET6)
3283                 struct lro_ctrl *lro = &rxr->hn_lro;
3284
3285                 if (lro->lro_cnt) {
3286                         rxr->hn_lro_tried++;
3287                         if (hn_lro_rx(lro, m_new) == 0) {
3288                                 /* DONE! */
3289                                 return 0;
3290                         }
3291                 }
3292 #endif
3293         }
3294         ifp->if_input(ifp, m_new);
3295
3296         return (0);
3297 }
3298
3299 static int
3300 hn_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
3301 {
3302         struct hn_softc *sc = ifp->if_softc;
3303         struct ifreq *ifr = (struct ifreq *)data, ifr_vf;
3304         struct ifnet *vf_ifp;
3305         int mask, error = 0;
3306         struct ifrsskey *ifrk;
3307         struct ifrsshash *ifrh;
3308
3309         switch (cmd) {
3310         case SIOCSIFMTU:
3311                 if (ifr->ifr_mtu > HN_MTU_MAX) {
3312                         error = EINVAL;
3313                         break;
3314                 }
3315
3316                 HN_LOCK(sc);
3317
3318                 if ((sc->hn_flags & HN_FLAG_SYNTH_ATTACHED) == 0) {
3319                         HN_UNLOCK(sc);
3320                         break;
3321                 }
3322
3323                 if ((sc->hn_caps & HN_CAP_MTU) == 0) {
3324                         /* Can't change MTU */
3325                         HN_UNLOCK(sc);
3326                         error = EOPNOTSUPP;
3327                         break;
3328                 }
3329
3330                 if (ifp->if_mtu == ifr->ifr_mtu) {
3331                         HN_UNLOCK(sc);
3332                         break;
3333                 }
3334
3335                 if (hn_xpnt_vf_isready(sc)) {
3336                         vf_ifp = sc->hn_vf_ifp;
3337                         ifr_vf = *ifr;
3338                         strlcpy(ifr_vf.ifr_name, vf_ifp->if_xname,
3339                             sizeof(ifr_vf.ifr_name));
3340                         error = vf_ifp->if_ioctl(vf_ifp, SIOCSIFMTU,
3341                             (caddr_t)&ifr_vf);
3342                         if (error) {
3343                                 HN_UNLOCK(sc);
3344                                 if_printf(ifp, "%s SIOCSIFMTU %d failed: %d\n",
3345                                     vf_ifp->if_xname, ifr->ifr_mtu, error);
3346                                 break;
3347                         }
3348                 }
3349
3350                 /*
3351                  * Suspend this interface before the synthetic parts
3352                  * are ripped.
3353                  */
3354                 hn_suspend(sc);
3355
3356                 /*
3357                  * Detach the synthetics parts, i.e. NVS and RNDIS.
3358                  */
3359                 hn_synth_detach(sc);
3360
3361                 /*
3362                  * Reattach the synthetic parts, i.e. NVS and RNDIS,
3363                  * with the new MTU setting.
3364                  */
3365                 error = hn_synth_attach(sc, ifr->ifr_mtu);
3366                 if (error) {
3367                         HN_UNLOCK(sc);
3368                         break;
3369                 }
3370
3371                 /*
3372                  * Commit the requested MTU, after the synthetic parts
3373                  * have been successfully attached.
3374                  */
3375                 ifp->if_mtu = ifr->ifr_mtu;
3376
3377                 /*
3378                  * Synthetic parts' reattach may change the chimney
3379                  * sending size; update it.
3380                  */
3381                 if (sc->hn_tx_ring[0].hn_chim_size > sc->hn_chim_szmax)
3382                         hn_set_chim_size(sc, sc->hn_chim_szmax);
3383
3384                 /*
3385                  * Make sure that various parameters based on MTU are
3386                  * still valid, after the MTU change.
3387                  */
3388                 hn_mtu_change_fixup(sc);
3389
3390                 /*
3391                  * All done!  Resume the interface now.
3392                  */
3393                 hn_resume(sc);
3394
3395                 if ((sc->hn_flags & HN_FLAG_RXVF) ||
3396                     (sc->hn_xvf_flags & HN_XVFFLAG_ENABLED)) {
3397                         /*
3398                          * Since we have reattached the NVS part,
3399                          * change the datapath to VF again; in case
3400                          * that it is lost, after the NVS was detached.
3401                          */
3402                         hn_nvs_set_datapath(sc, HN_NVS_DATAPATH_VF);
3403                 }
3404
3405                 HN_UNLOCK(sc);
3406                 break;
3407
3408         case SIOCSIFFLAGS:
3409                 HN_LOCK(sc);
3410
3411                 if ((sc->hn_flags & HN_FLAG_SYNTH_ATTACHED) == 0) {
3412                         HN_UNLOCK(sc);
3413                         break;
3414                 }
3415
3416                 if (hn_xpnt_vf_isready(sc))
3417                         hn_xpnt_vf_saveifflags(sc);
3418
3419                 if (ifp->if_flags & IFF_UP) {
3420                         if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
3421                                 /*
3422                                  * Caller meight hold mutex, e.g.
3423                                  * bpf; use busy-wait for the RNDIS
3424                                  * reply.
3425                                  */
3426                                 HN_NO_SLEEPING(sc);
3427                                 hn_rxfilter_config(sc);
3428                                 HN_SLEEPING_OK(sc);
3429
3430                                 if (sc->hn_xvf_flags & HN_XVFFLAG_ENABLED)
3431                                         error = hn_xpnt_vf_iocsetflags(sc);
3432                         } else {
3433                                 hn_init_locked(sc);
3434                         }
3435                 } else {
3436                         if (ifp->if_drv_flags & IFF_DRV_RUNNING)
3437                                 hn_stop(sc, false);
3438                 }
3439                 sc->hn_if_flags = ifp->if_flags;
3440
3441                 HN_UNLOCK(sc);
3442                 break;
3443
3444         case SIOCSIFCAP:
3445                 HN_LOCK(sc);
3446
3447                 if (hn_xpnt_vf_isready(sc)) {
3448                         ifr_vf = *ifr;
3449                         strlcpy(ifr_vf.ifr_name, sc->hn_vf_ifp->if_xname,
3450                             sizeof(ifr_vf.ifr_name));
3451                         error = hn_xpnt_vf_iocsetcaps(sc, &ifr_vf);
3452                         HN_UNLOCK(sc);
3453                         break;
3454                 }
3455
3456                 /*
3457                  * Fix up requested capabilities w/ supported capabilities,
3458                  * since the supported capabilities could have been changed.
3459                  */
3460                 mask = (ifr->ifr_reqcap & ifp->if_capabilities) ^
3461                     ifp->if_capenable;
3462
3463                 if (mask & IFCAP_TXCSUM) {
3464                         ifp->if_capenable ^= IFCAP_TXCSUM;
3465                         if (ifp->if_capenable & IFCAP_TXCSUM)
3466                                 ifp->if_hwassist |= HN_CSUM_IP_HWASSIST(sc);
3467                         else
3468                                 ifp->if_hwassist &= ~HN_CSUM_IP_HWASSIST(sc);
3469                 }
3470                 if (mask & IFCAP_TXCSUM_IPV6) {
3471                         ifp->if_capenable ^= IFCAP_TXCSUM_IPV6;
3472                         if (ifp->if_capenable & IFCAP_TXCSUM_IPV6)
3473                                 ifp->if_hwassist |= HN_CSUM_IP6_HWASSIST(sc);
3474                         else
3475                                 ifp->if_hwassist &= ~HN_CSUM_IP6_HWASSIST(sc);
3476                 }
3477
3478                 /* TODO: flip RNDIS offload parameters for RXCSUM. */
3479                 if (mask & IFCAP_RXCSUM)
3480                         ifp->if_capenable ^= IFCAP_RXCSUM;
3481 #ifdef foo
3482                 /* We can't diff IPv6 packets from IPv4 packets on RX path. */
3483                 if (mask & IFCAP_RXCSUM_IPV6)
3484                         ifp->if_capenable ^= IFCAP_RXCSUM_IPV6;
3485 #endif
3486
3487                 if (mask & IFCAP_LRO)
3488                         ifp->if_capenable ^= IFCAP_LRO;
3489
3490                 if (mask & IFCAP_TSO4) {
3491                         ifp->if_capenable ^= IFCAP_TSO4;
3492                         if (ifp->if_capenable & IFCAP_TSO4)
3493                                 ifp->if_hwassist |= CSUM_IP_TSO;
3494                         else
3495                                 ifp->if_hwassist &= ~CSUM_IP_TSO;
3496                 }
3497                 if (mask & IFCAP_TSO6) {
3498                         ifp->if_capenable ^= IFCAP_TSO6;
3499                         if (ifp->if_capenable & IFCAP_TSO6)
3500                                 ifp->if_hwassist |= CSUM_IP6_TSO;
3501                         else
3502                                 ifp->if_hwassist &= ~CSUM_IP6_TSO;
3503                 }
3504
3505                 HN_UNLOCK(sc);
3506                 break;
3507
3508         case SIOCADDMULTI:
3509         case SIOCDELMULTI:
3510                 HN_LOCK(sc);
3511
3512                 if ((sc->hn_flags & HN_FLAG_SYNTH_ATTACHED) == 0) {
3513                         HN_UNLOCK(sc);
3514                         break;
3515                 }
3516                 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
3517                         /*
3518                          * Multicast uses mutex; use busy-wait for
3519                          * the RNDIS reply.
3520                          */
3521                         HN_NO_SLEEPING(sc);
3522                         hn_rxfilter_config(sc);
3523                         HN_SLEEPING_OK(sc);
3524                 }
3525
3526                 /* XXX vlan(4) style mcast addr maintenance */
3527                 if (hn_xpnt_vf_isready(sc)) {
3528                         int old_if_flags;
3529
3530                         old_if_flags = sc->hn_vf_ifp->if_flags;
3531                         hn_xpnt_vf_saveifflags(sc);
3532
3533                         if ((sc->hn_xvf_flags & HN_XVFFLAG_ENABLED) &&
3534                             ((old_if_flags ^ sc->hn_vf_ifp->if_flags) &
3535                              IFF_ALLMULTI))
3536                                 error = hn_xpnt_vf_iocsetflags(sc);
3537                 }
3538
3539                 HN_UNLOCK(sc);
3540                 break;
3541
3542         case SIOCSIFMEDIA:
3543         case SIOCGIFMEDIA:
3544                 HN_LOCK(sc);
3545                 if (hn_xpnt_vf_isready(sc)) {
3546                         /*
3547                          * SIOCGIFMEDIA expects ifmediareq, so don't
3548                          * create and pass ifr_vf to the VF here; just
3549                          * replace the ifr_name.
3550                          */
3551                         vf_ifp = sc->hn_vf_ifp;
3552                         strlcpy(ifr->ifr_name, vf_ifp->if_xname,
3553                             sizeof(ifr->ifr_name));
3554                         error = vf_ifp->if_ioctl(vf_ifp, cmd, data);
3555                         /* Restore the ifr_name. */
3556                         strlcpy(ifr->ifr_name, ifp->if_xname,
3557                             sizeof(ifr->ifr_name));
3558                         HN_UNLOCK(sc);
3559                         break;
3560                 }
3561                 HN_UNLOCK(sc);
3562                 error = ifmedia_ioctl(ifp, ifr, &sc->hn_media, cmd);
3563                 break;
3564
3565         case SIOCGIFRSSHASH:
3566                 ifrh = (struct ifrsshash *)data;
3567                 HN_LOCK(sc);
3568                 if (sc->hn_rx_ring_inuse == 1) {
3569                         HN_UNLOCK(sc);
3570                         ifrh->ifrh_func = RSS_FUNC_NONE;
3571                         ifrh->ifrh_types = 0;
3572                         break;
3573                 }
3574
3575                 if (sc->hn_rss_hash & NDIS_HASH_FUNCTION_TOEPLITZ)
3576                         ifrh->ifrh_func = RSS_FUNC_TOEPLITZ;
3577                 else
3578                         ifrh->ifrh_func = RSS_FUNC_PRIVATE;
3579
3580                 ifrh->ifrh_types = 0;
3581                 if (sc->hn_rss_hash & NDIS_HASH_IPV4)
3582                         ifrh->ifrh_types |= RSS_TYPE_IPV4;
3583                 if (sc->hn_rss_hash & NDIS_HASH_TCP_IPV4)
3584                         ifrh->ifrh_types |= RSS_TYPE_TCP_IPV4;
3585                 if (sc->hn_rss_hash & NDIS_HASH_IPV6)
3586                         ifrh->ifrh_types |= RSS_TYPE_IPV6;
3587                 if (sc->hn_rss_hash & NDIS_HASH_IPV6_EX)
3588                         ifrh->ifrh_types |= RSS_TYPE_IPV6_EX;
3589                 if (sc->hn_rss_hash & NDIS_HASH_TCP_IPV6)
3590                         ifrh->ifrh_types |= RSS_TYPE_TCP_IPV6;
3591                 if (sc->hn_rss_hash & NDIS_HASH_TCP_IPV6_EX)
3592                         ifrh->ifrh_types |= RSS_TYPE_TCP_IPV6_EX;
3593                 HN_UNLOCK(sc);
3594                 break;
3595
3596         case SIOCGIFRSSKEY:
3597                 ifrk = (struct ifrsskey *)data;
3598                 HN_LOCK(sc);
3599                 if (sc->hn_rx_ring_inuse == 1) {
3600                         HN_UNLOCK(sc);
3601                         ifrk->ifrk_func = RSS_FUNC_NONE;
3602                         ifrk->ifrk_keylen = 0;
3603                         break;
3604                 }
3605                 if (sc->hn_rss_hash & NDIS_HASH_FUNCTION_TOEPLITZ)
3606                         ifrk->ifrk_func = RSS_FUNC_TOEPLITZ;
3607                 else
3608                         ifrk->ifrk_func = RSS_FUNC_PRIVATE;
3609                 ifrk->ifrk_keylen = NDIS_HASH_KEYSIZE_TOEPLITZ;
3610                 memcpy(ifrk->ifrk_key, sc->hn_rss.rss_key,
3611                     NDIS_HASH_KEYSIZE_TOEPLITZ);
3612                 HN_UNLOCK(sc);
3613                 break;
3614
3615         default:
3616                 error = ether_ioctl(ifp, cmd, data);
3617                 break;
3618         }
3619         return (error);
3620 }
3621
3622 static void
3623 hn_stop(struct hn_softc *sc, bool detaching)
3624 {
3625         struct ifnet *ifp = sc->hn_ifp;
3626         int i;
3627
3628         HN_LOCK_ASSERT(sc);
3629
3630         KASSERT(sc->hn_flags & HN_FLAG_SYNTH_ATTACHED,
3631             ("synthetic parts were not attached"));
3632
3633         /* Clear RUNNING bit ASAP. */
3634         atomic_clear_int(&ifp->if_drv_flags, IFF_DRV_RUNNING);
3635
3636         /* Disable polling. */
3637         hn_polling(sc, 0);
3638
3639         if (sc->hn_xvf_flags & HN_XVFFLAG_ENABLED) {
3640                 KASSERT(sc->hn_vf_ifp != NULL,
3641                     ("%s: VF is not attached", ifp->if_xname));
3642
3643                 /* Mark transparent mode VF as disabled. */
3644                 hn_xpnt_vf_setdisable(sc, false /* keep hn_vf_ifp */);
3645
3646                 /*
3647                  * NOTE:
3648                  * Datapath setting must happen _before_ bringing
3649                  * the VF down.
3650                  */
3651                 hn_nvs_set_datapath(sc, HN_NVS_DATAPATH_SYNTH);
3652
3653                 /*
3654                  * Bring the VF down.
3655                  */
3656                 hn_xpnt_vf_saveifflags(sc);
3657                 sc->hn_vf_ifp->if_flags &= ~IFF_UP;
3658                 hn_xpnt_vf_iocsetflags(sc);
3659         }
3660
3661         /* Suspend data transfers. */
3662         hn_suspend_data(sc);
3663
3664         /* Clear OACTIVE bit. */
3665         atomic_clear_int(&ifp->if_drv_flags, IFF_DRV_OACTIVE);
3666         for (i = 0; i < sc->hn_tx_ring_inuse; ++i)
3667                 sc->hn_tx_ring[i].hn_oactive = 0;
3668
3669         /*
3670          * If the non-transparent mode VF is active, make sure
3671          * that the RX filter still allows packet reception.
3672          */
3673         if (!detaching && (sc->hn_flags & HN_FLAG_RXVF))
3674                 hn_rxfilter_config(sc);
3675 }
3676
3677 static void
3678 hn_init_locked(struct hn_softc *sc)
3679 {
3680         struct ifnet *ifp = sc->hn_ifp;
3681         int i;
3682
3683         HN_LOCK_ASSERT(sc);
3684
3685         if ((sc->hn_flags & HN_FLAG_SYNTH_ATTACHED) == 0)
3686                 return;
3687
3688         if (ifp->if_drv_flags & IFF_DRV_RUNNING)
3689                 return;
3690
3691         /* Configure RX filter */
3692         hn_rxfilter_config(sc);
3693
3694         /* Clear OACTIVE bit. */
3695         atomic_clear_int(&ifp->if_drv_flags, IFF_DRV_OACTIVE);
3696         for (i = 0; i < sc->hn_tx_ring_inuse; ++i)
3697                 sc->hn_tx_ring[i].hn_oactive = 0;
3698
3699         /* Clear TX 'suspended' bit. */
3700         hn_resume_tx(sc, sc->hn_tx_ring_inuse);
3701
3702         if (hn_xpnt_vf_isready(sc)) {
3703                 /* Initialize transparent VF. */
3704                 hn_xpnt_vf_init(sc);
3705         }
3706
3707         /* Everything is ready; unleash! */
3708         atomic_set_int(&ifp->if_drv_flags, IFF_DRV_RUNNING);
3709
3710         /* Re-enable polling if requested. */
3711         if (sc->hn_pollhz > 0)
3712                 hn_polling(sc, sc->hn_pollhz);
3713 }
3714
3715 static void
3716 hn_init(void *xsc)
3717 {
3718         struct hn_softc *sc = xsc;
3719
3720         HN_LOCK(sc);
3721         hn_init_locked(sc);
3722         HN_UNLOCK(sc);
3723 }
3724
3725 #if __FreeBSD_version >= 1100099
3726
3727 static int
3728 hn_lro_lenlim_sysctl(SYSCTL_HANDLER_ARGS)
3729 {
3730         struct hn_softc *sc = arg1;
3731         unsigned int lenlim;
3732         int error;
3733
3734         lenlim = sc->hn_rx_ring[0].hn_lro.lro_length_lim;
3735         error = sysctl_handle_int(oidp, &lenlim, 0, req);
3736         if (error || req->newptr == NULL)
3737                 return error;
3738
3739         HN_LOCK(sc);
3740         if (lenlim < HN_LRO_LENLIM_MIN(sc->hn_ifp) ||
3741             lenlim > TCP_LRO_LENGTH_MAX) {
3742                 HN_UNLOCK(sc);
3743                 return EINVAL;
3744         }
3745         hn_set_lro_lenlim(sc, lenlim);
3746         HN_UNLOCK(sc);
3747
3748         return 0;
3749 }
3750
3751 static int
3752 hn_lro_ackcnt_sysctl(SYSCTL_HANDLER_ARGS)
3753 {
3754         struct hn_softc *sc = arg1;
3755         int ackcnt, error, i;
3756
3757         /*
3758          * lro_ackcnt_lim is append count limit,
3759          * +1 to turn it into aggregation limit.
3760          */
3761         ackcnt = sc->hn_rx_ring[0].hn_lro.lro_ackcnt_lim + 1;
3762         error = sysctl_handle_int(oidp, &ackcnt, 0, req);
3763         if (error || req->newptr == NULL)
3764                 return error;
3765
3766         if (ackcnt < 2 || ackcnt > (TCP_LRO_ACKCNT_MAX + 1))
3767                 return EINVAL;
3768
3769         /*
3770          * Convert aggregation limit back to append
3771          * count limit.
3772          */
3773         --ackcnt;
3774         HN_LOCK(sc);
3775         for (i = 0; i < sc->hn_rx_ring_cnt; ++i)
3776                 sc->hn_rx_ring[i].hn_lro.lro_ackcnt_lim = ackcnt;
3777         HN_UNLOCK(sc);
3778         return 0;
3779 }
3780
3781 #endif
3782
3783 static int
3784 hn_trust_hcsum_sysctl(SYSCTL_HANDLER_ARGS)
3785 {
3786         struct hn_softc *sc = arg1;
3787         int hcsum = arg2;
3788         int on, error, i;
3789
3790         on = 0;
3791         if (sc->hn_rx_ring[0].hn_trust_hcsum & hcsum)
3792                 on = 1;
3793
3794         error = sysctl_handle_int(oidp, &on, 0, req);
3795         if (error || req->newptr == NULL)
3796                 return error;
3797
3798         HN_LOCK(sc);
3799         for (i = 0; i < sc->hn_rx_ring_cnt; ++i) {
3800                 struct hn_rx_ring *rxr = &sc->hn_rx_ring[i];
3801
3802                 if (on)
3803                         rxr->hn_trust_hcsum |= hcsum;
3804                 else
3805                         rxr->hn_trust_hcsum &= ~hcsum;
3806         }
3807         HN_UNLOCK(sc);
3808         return 0;
3809 }
3810
3811 static int
3812 hn_chim_size_sysctl(SYSCTL_HANDLER_ARGS)
3813 {
3814         struct hn_softc *sc = arg1;
3815         int chim_size, error;
3816
3817         chim_size = sc->hn_tx_ring[0].hn_chim_size;
3818         error = sysctl_handle_int(oidp, &chim_size, 0, req);
3819         if (error || req->newptr == NULL)
3820                 return error;
3821
3822         if (chim_size > sc->hn_chim_szmax || chim_size <= 0)
3823                 return EINVAL;
3824
3825         HN_LOCK(sc);
3826         hn_set_chim_size(sc, chim_size);
3827         HN_UNLOCK(sc);
3828         return 0;
3829 }
3830
3831 #if __FreeBSD_version < 1100095
3832 static int
3833 hn_rx_stat_int_sysctl(SYSCTL_HANDLER_ARGS)
3834 {
3835         struct hn_softc *sc = arg1;
3836         int ofs = arg2, i, error;
3837         struct hn_rx_ring *rxr;
3838         uint64_t stat;
3839
3840         stat = 0;
3841         for (i = 0; i < sc->hn_rx_ring_inuse; ++i) {
3842                 rxr = &sc->hn_rx_ring[i];
3843                 stat += *((int *)((uint8_t *)rxr + ofs));
3844         }
3845
3846         error = sysctl_handle_64(oidp, &stat, 0, req);
3847         if (error || req->newptr == NULL)
3848                 return error;
3849
3850         /* Zero out this stat. */
3851         for (i = 0; i < sc->hn_rx_ring_inuse; ++i) {
3852                 rxr = &sc->hn_rx_ring[i];
3853                 *((int *)((uint8_t *)rxr + ofs)) = 0;
3854         }
3855         return 0;
3856 }
3857 #else
3858 static int
3859 hn_rx_stat_u64_sysctl(SYSCTL_HANDLER_ARGS)
3860 {
3861         struct hn_softc *sc = arg1;
3862         int ofs = arg2, i, error;
3863         struct hn_rx_ring *rxr;
3864         uint64_t stat;
3865
3866         stat = 0;
3867         for (i = 0; i < sc->hn_rx_ring_cnt; ++i) {
3868                 rxr = &sc->hn_rx_ring[i];
3869                 stat += *((uint64_t *)((uint8_t *)rxr + ofs));
3870         }
3871
3872         error = sysctl_handle_64(oidp, &stat, 0, req);
3873         if (error || req->newptr == NULL)
3874                 return error;
3875
3876         /* Zero out this stat. */
3877         for (i = 0; i < sc->hn_rx_ring_cnt; ++i) {
3878                 rxr = &sc->hn_rx_ring[i];
3879                 *((uint64_t *)((uint8_t *)rxr + ofs)) = 0;
3880         }
3881         return 0;
3882 }
3883
3884 #endif
3885
3886 static int
3887 hn_rx_stat_ulong_sysctl(SYSCTL_HANDLER_ARGS)
3888 {
3889         struct hn_softc *sc = arg1;
3890         int ofs = arg2, i, error;
3891         struct hn_rx_ring *rxr;
3892         u_long stat;
3893
3894         stat = 0;
3895         for (i = 0; i < sc->hn_rx_ring_cnt; ++i) {
3896                 rxr = &sc->hn_rx_ring[i];
3897                 stat += *((u_long *)((uint8_t *)rxr + ofs));
3898         }
3899
3900         error = sysctl_handle_long(oidp, &stat, 0, req);
3901         if (error || req->newptr == NULL)
3902                 return error;
3903
3904         /* Zero out this stat. */
3905         for (i = 0; i < sc->hn_rx_ring_cnt; ++i) {
3906                 rxr = &sc->hn_rx_ring[i];
3907                 *((u_long *)((uint8_t *)rxr + ofs)) = 0;
3908         }
3909         return 0;
3910 }
3911
3912 static int
3913 hn_tx_stat_ulong_sysctl(SYSCTL_HANDLER_ARGS)
3914 {
3915         struct hn_softc *sc = arg1;
3916         int ofs = arg2, i, error;
3917         struct hn_tx_ring *txr;
3918         u_long stat;
3919
3920         stat = 0;
3921         for (i = 0; i < sc->hn_tx_ring_cnt; ++i) {
3922                 txr = &sc->hn_tx_ring[i];
3923                 stat += *((u_long *)((uint8_t *)txr + ofs));
3924         }
3925
3926         error = sysctl_handle_long(oidp, &stat, 0, req);
3927         if (error || req->newptr == NULL)
3928                 return error;
3929
3930         /* Zero out this stat. */
3931         for (i = 0; i < sc->hn_tx_ring_cnt; ++i) {
3932                 txr = &sc->hn_tx_ring[i];
3933                 *((u_long *)((uint8_t *)txr + ofs)) = 0;
3934         }
3935         return 0;
3936 }
3937
3938 static int
3939 hn_tx_conf_int_sysctl(SYSCTL_HANDLER_ARGS)
3940 {
3941         struct hn_softc *sc = arg1;
3942         int ofs = arg2, i, error, conf;
3943         struct hn_tx_ring *txr;
3944
3945         txr = &sc->hn_tx_ring[0];
3946         conf = *((int *)((uint8_t *)txr + ofs));
3947
3948         error = sysctl_handle_int(oidp, &conf, 0, req);
3949         if (error || req->newptr == NULL)
3950                 return error;
3951
3952         HN_LOCK(sc);
3953         for (i = 0; i < sc->hn_tx_ring_cnt; ++i) {
3954                 txr = &sc->hn_tx_ring[i];
3955                 *((int *)((uint8_t *)txr + ofs)) = conf;
3956         }
3957         HN_UNLOCK(sc);
3958
3959         return 0;
3960 }
3961
3962 static int
3963 hn_txagg_size_sysctl(SYSCTL_HANDLER_ARGS)
3964 {
3965         struct hn_softc *sc = arg1;
3966         int error, size;
3967
3968         size = sc->hn_agg_size;
3969         error = sysctl_handle_int(oidp, &size, 0, req);
3970         if (error || req->newptr == NULL)
3971                 return (error);
3972
3973         HN_LOCK(sc);
3974         sc->hn_agg_size = size;
3975         hn_set_txagg(sc);
3976         HN_UNLOCK(sc);
3977
3978         return (0);
3979 }
3980
3981 static int
3982 hn_txagg_pkts_sysctl(SYSCTL_HANDLER_ARGS)
3983 {
3984         struct hn_softc *sc = arg1;
3985         int error, pkts;
3986
3987         pkts = sc->hn_agg_pkts;
3988         error = sysctl_handle_int(oidp, &pkts, 0, req);
3989         if (error || req->newptr == NULL)
3990                 return (error);
3991
3992         HN_LOCK(sc);
3993         sc->hn_agg_pkts = pkts;
3994         hn_set_txagg(sc);
3995         HN_UNLOCK(sc);
3996
3997         return (0);
3998 }
3999
4000 static int
4001 hn_txagg_pktmax_sysctl(SYSCTL_HANDLER_ARGS)
4002 {
4003         struct hn_softc *sc = arg1;
4004         int pkts;
4005
4006         pkts = sc->hn_tx_ring[0].hn_agg_pktmax;
4007         return (sysctl_handle_int(oidp, &pkts, 0, req));
4008 }
4009
4010 static int
4011 hn_txagg_align_sysctl(SYSCTL_HANDLER_ARGS)
4012 {
4013         struct hn_softc *sc = arg1;
4014         int align;
4015
4016         align = sc->hn_tx_ring[0].hn_agg_align;
4017         return (sysctl_handle_int(oidp, &align, 0, req));
4018 }
4019
4020 static void
4021 hn_chan_polling(struct vmbus_channel *chan, u_int pollhz)
4022 {
4023         if (pollhz == 0)
4024                 vmbus_chan_poll_disable(chan);
4025         else
4026                 vmbus_chan_poll_enable(chan, pollhz);
4027 }
4028
4029 static void
4030 hn_polling(struct hn_softc *sc, u_int pollhz)
4031 {
4032         int nsubch = sc->hn_rx_ring_inuse - 1;
4033
4034         HN_LOCK_ASSERT(sc);
4035
4036         if (nsubch > 0) {
4037                 struct vmbus_channel **subch;
4038                 int i;
4039
4040                 subch = vmbus_subchan_get(sc->hn_prichan, nsubch);
4041                 for (i = 0; i < nsubch; ++i)
4042                         hn_chan_polling(subch[i], pollhz);
4043                 vmbus_subchan_rel(subch, nsubch);
4044         }
4045         hn_chan_polling(sc->hn_prichan, pollhz);
4046 }
4047
4048 static int
4049 hn_polling_sysctl(SYSCTL_HANDLER_ARGS)
4050 {
4051         struct hn_softc *sc = arg1;
4052         int pollhz, error;
4053
4054         pollhz = sc->hn_pollhz;
4055         error = sysctl_handle_int(oidp, &pollhz, 0, req);
4056         if (error || req->newptr == NULL)
4057                 return (error);
4058
4059         if (pollhz != 0 &&
4060             (pollhz < VMBUS_CHAN_POLLHZ_MIN || pollhz > VMBUS_CHAN_POLLHZ_MAX))
4061                 return (EINVAL);
4062
4063         HN_LOCK(sc);
4064         if (sc->hn_pollhz != pollhz) {
4065                 sc->hn_pollhz = pollhz;
4066                 if ((sc->hn_ifp->if_drv_flags & IFF_DRV_RUNNING) &&
4067                     (sc->hn_flags & HN_FLAG_SYNTH_ATTACHED))
4068                         hn_polling(sc, sc->hn_pollhz);
4069         }
4070         HN_UNLOCK(sc);
4071
4072         return (0);
4073 }
4074
4075 static int
4076 hn_ndis_version_sysctl(SYSCTL_HANDLER_ARGS)
4077 {
4078         struct hn_softc *sc = arg1;
4079         char verstr[16];
4080
4081         snprintf(verstr, sizeof(verstr), "%u.%u",
4082             HN_NDIS_VERSION_MAJOR(sc->hn_ndis_ver),
4083             HN_NDIS_VERSION_MINOR(sc->hn_ndis_ver));
4084         return sysctl_handle_string(oidp, verstr, sizeof(verstr), req);
4085 }
4086
4087 static int
4088 hn_caps_sysctl(SYSCTL_HANDLER_ARGS)
4089 {
4090         struct hn_softc *sc = arg1;
4091         char caps_str[128];
4092         uint32_t caps;
4093
4094         HN_LOCK(sc);
4095         caps = sc->hn_caps;
4096         HN_UNLOCK(sc);
4097         snprintf(caps_str, sizeof(caps_str), "%b", caps, HN_CAP_BITS);
4098         return sysctl_handle_string(oidp, caps_str, sizeof(caps_str), req);
4099 }
4100
4101 static int
4102 hn_hwassist_sysctl(SYSCTL_HANDLER_ARGS)
4103 {
4104         struct hn_softc *sc = arg1;
4105         char assist_str[128];
4106         uint32_t hwassist;
4107
4108         HN_LOCK(sc);
4109         hwassist = sc->hn_ifp->if_hwassist;
4110         HN_UNLOCK(sc);
4111         snprintf(assist_str, sizeof(assist_str), "%b", hwassist, CSUM_BITS);
4112         return sysctl_handle_string(oidp, assist_str, sizeof(assist_str), req);
4113 }
4114
4115 static int
4116 hn_rxfilter_sysctl(SYSCTL_HANDLER_ARGS)
4117 {
4118         struct hn_softc *sc = arg1;
4119         char filter_str[128];
4120         uint32_t filter;
4121
4122         HN_LOCK(sc);
4123         filter = sc->hn_rx_filter;
4124         HN_UNLOCK(sc);
4125         snprintf(filter_str, sizeof(filter_str), "%b", filter,
4126             NDIS_PACKET_TYPES);
4127         return sysctl_handle_string(oidp, filter_str, sizeof(filter_str), req);
4128 }
4129
4130 static int
4131 hn_rss_key_sysctl(SYSCTL_HANDLER_ARGS)
4132 {
4133         struct hn_softc *sc = arg1;
4134         int error;
4135
4136         HN_LOCK(sc);
4137
4138         error = SYSCTL_OUT(req, sc->hn_rss.rss_key, sizeof(sc->hn_rss.rss_key));
4139         if (error || req->newptr == NULL)
4140                 goto back;
4141
4142         error = SYSCTL_IN(req, sc->hn_rss.rss_key, sizeof(sc->hn_rss.rss_key));
4143         if (error)
4144                 goto back;
4145         sc->hn_flags |= HN_FLAG_HAS_RSSKEY;
4146
4147         if (sc->hn_rx_ring_inuse > 1) {
4148                 error = hn_rss_reconfig(sc);
4149         } else {
4150                 /* Not RSS capable, at least for now; just save the RSS key. */
4151                 error = 0;
4152         }
4153 back:
4154         HN_UNLOCK(sc);
4155         return (error);
4156 }
4157
4158 static int
4159 hn_rss_ind_sysctl(SYSCTL_HANDLER_ARGS)
4160 {
4161         struct hn_softc *sc = arg1;
4162         int error;
4163
4164         HN_LOCK(sc);
4165
4166         error = SYSCTL_OUT(req, sc->hn_rss.rss_ind, sizeof(sc->hn_rss.rss_ind));
4167         if (error || req->newptr == NULL)
4168                 goto back;
4169
4170         /*
4171          * Don't allow RSS indirect table change, if this interface is not
4172          * RSS capable currently.
4173          */
4174         if (sc->hn_rx_ring_inuse == 1) {
4175                 error = EOPNOTSUPP;
4176                 goto back;
4177         }
4178
4179         error = SYSCTL_IN(req, sc->hn_rss.rss_ind, sizeof(sc->hn_rss.rss_ind));
4180         if (error)
4181                 goto back;
4182         sc->hn_flags |= HN_FLAG_HAS_RSSIND;
4183
4184         hn_rss_ind_fixup(sc);
4185         error = hn_rss_reconfig(sc);
4186 back:
4187         HN_UNLOCK(sc);
4188         return (error);
4189 }
4190
4191 static int
4192 hn_rss_hash_sysctl(SYSCTL_HANDLER_ARGS)
4193 {
4194         struct hn_softc *sc = arg1;
4195         char hash_str[128];
4196         uint32_t hash;
4197
4198         HN_LOCK(sc);
4199         hash = sc->hn_rss_hash;
4200         HN_UNLOCK(sc);
4201         snprintf(hash_str, sizeof(hash_str), "%b", hash, NDIS_HASH_BITS);
4202         return sysctl_handle_string(oidp, hash_str, sizeof(hash_str), req);
4203 }
4204
4205 static int
4206 hn_vf_sysctl(SYSCTL_HANDLER_ARGS)
4207 {
4208         struct hn_softc *sc = arg1;
4209         char vf_name[IFNAMSIZ + 1];
4210         struct ifnet *vf_ifp;
4211
4212         HN_LOCK(sc);
4213         vf_name[0] = '\0';
4214         vf_ifp = sc->hn_vf_ifp;
4215         if (vf_ifp != NULL)
4216                 snprintf(vf_name, sizeof(vf_name), "%s", vf_ifp->if_xname);
4217         HN_UNLOCK(sc);
4218         return sysctl_handle_string(oidp, vf_name, sizeof(vf_name), req);
4219 }
4220
4221 static int
4222 hn_rxvf_sysctl(SYSCTL_HANDLER_ARGS)
4223 {
4224         struct hn_softc *sc = arg1;
4225         char vf_name[IFNAMSIZ + 1];
4226         struct ifnet *vf_ifp;
4227
4228         HN_LOCK(sc);
4229         vf_name[0] = '\0';
4230         vf_ifp = sc->hn_rx_ring[0].hn_rxvf_ifp;
4231         if (vf_ifp != NULL)
4232                 snprintf(vf_name, sizeof(vf_name), "%s", vf_ifp->if_xname);
4233         HN_UNLOCK(sc);
4234         return sysctl_handle_string(oidp, vf_name, sizeof(vf_name), req);
4235 }
4236
4237 static int
4238 hn_vflist_sysctl(SYSCTL_HANDLER_ARGS)
4239 {
4240         struct rm_priotracker pt;
4241         struct sbuf *sb;
4242         int error, i;
4243         bool first;
4244
4245         error = sysctl_wire_old_buffer(req, 0);
4246         if (error != 0)
4247                 return (error);
4248
4249         sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
4250         if (sb == NULL)
4251                 return (ENOMEM);
4252
4253         rm_rlock(&hn_vfmap_lock, &pt);
4254
4255         first = true;
4256         for (i = 0; i < hn_vfmap_size; ++i) {
4257                 struct ifnet *ifp;
4258
4259                 if (hn_vfmap[i] == NULL)
4260                         continue;
4261
4262                 ifp = ifnet_byindex(i);
4263                 if (ifp != NULL) {
4264                         if (first)
4265                                 sbuf_printf(sb, "%s", ifp->if_xname);
4266                         else
4267                                 sbuf_printf(sb, " %s", ifp->if_xname);
4268                         first = false;
4269                 }
4270         }
4271
4272         rm_runlock(&hn_vfmap_lock, &pt);
4273
4274         error = sbuf_finish(sb);
4275         sbuf_delete(sb);
4276         return (error);
4277 }
4278
4279 static int
4280 hn_vfmap_sysctl(SYSCTL_HANDLER_ARGS)
4281 {
4282         struct rm_priotracker pt;
4283         struct sbuf *sb;
4284         int error, i;
4285         bool first;
4286
4287         error = sysctl_wire_old_buffer(req, 0);
4288         if (error != 0)
4289                 return (error);
4290
4291         sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
4292         if (sb == NULL)
4293                 return (ENOMEM);
4294
4295         rm_rlock(&hn_vfmap_lock, &pt);
4296
4297         first = true;
4298         for (i = 0; i < hn_vfmap_size; ++i) {
4299                 struct ifnet *ifp, *hn_ifp;
4300
4301                 hn_ifp = hn_vfmap[i];
4302                 if (hn_ifp == NULL)
4303                         continue;
4304
4305                 ifp = ifnet_byindex(i);
4306                 if (ifp != NULL) {
4307                         if (first) {
4308                                 sbuf_printf(sb, "%s:%s", ifp->if_xname,
4309                                     hn_ifp->if_xname);
4310                         } else {
4311                                 sbuf_printf(sb, " %s:%s", ifp->if_xname,
4312                                     hn_ifp->if_xname);
4313                         }
4314                         first = false;
4315                 }
4316         }
4317
4318         rm_runlock(&hn_vfmap_lock, &pt);
4319
4320         error = sbuf_finish(sb);
4321         sbuf_delete(sb);
4322         return (error);
4323 }
4324
4325 static int
4326 hn_xpnt_vf_accbpf_sysctl(SYSCTL_HANDLER_ARGS)
4327 {
4328         struct hn_softc *sc = arg1;
4329         int error, onoff = 0;
4330
4331         if (sc->hn_xvf_flags & HN_XVFFLAG_ACCBPF)
4332                 onoff = 1;
4333         error = sysctl_handle_int(oidp, &onoff, 0, req);
4334         if (error || req->newptr == NULL)
4335                 return (error);
4336
4337         HN_LOCK(sc);
4338         /* NOTE: hn_vf_lock for hn_transmit() */
4339         rm_wlock(&sc->hn_vf_lock);
4340         if (onoff)
4341                 sc->hn_xvf_flags |= HN_XVFFLAG_ACCBPF;
4342         else
4343                 sc->hn_xvf_flags &= ~HN_XVFFLAG_ACCBPF;
4344         rm_wunlock(&sc->hn_vf_lock);
4345         HN_UNLOCK(sc);
4346
4347         return (0);
4348 }
4349
4350 static int
4351 hn_xpnt_vf_enabled_sysctl(SYSCTL_HANDLER_ARGS)
4352 {
4353         struct hn_softc *sc = arg1;
4354         int enabled = 0;
4355
4356         if (sc->hn_xvf_flags & HN_XVFFLAG_ENABLED)
4357                 enabled = 1;
4358         return (sysctl_handle_int(oidp, &enabled, 0, req));
4359 }
4360
4361 static int
4362 hn_check_iplen(const struct mbuf *m, int hoff)
4363 {
4364         const struct ip *ip;
4365         int len, iphlen, iplen;
4366         const struct tcphdr *th;
4367         int thoff;                              /* TCP data offset */
4368
4369         len = hoff + sizeof(struct ip);
4370
4371         /* The packet must be at least the size of an IP header. */
4372         if (m->m_pkthdr.len < len)
4373                 return IPPROTO_DONE;
4374
4375         /* The fixed IP header must reside completely in the first mbuf. */
4376         if (m->m_len < len)
4377                 return IPPROTO_DONE;
4378
4379         ip = mtodo(m, hoff);
4380
4381         /* Bound check the packet's stated IP header length. */
4382         iphlen = ip->ip_hl << 2;
4383         if (iphlen < sizeof(struct ip))         /* minimum header length */
4384                 return IPPROTO_DONE;
4385
4386         /* The full IP header must reside completely in the one mbuf. */
4387         if (m->m_len < hoff + iphlen)
4388                 return IPPROTO_DONE;
4389
4390         iplen = ntohs(ip->ip_len);
4391
4392         /*
4393          * Check that the amount of data in the buffers is as
4394          * at least much as the IP header would have us expect.
4395          */
4396         if (m->m_pkthdr.len < hoff + iplen)
4397                 return IPPROTO_DONE;
4398
4399         /*
4400          * Ignore IP fragments.
4401          */
4402         if (ntohs(ip->ip_off) & (IP_OFFMASK | IP_MF))
4403                 return IPPROTO_DONE;
4404
4405         /*
4406          * The TCP/IP or UDP/IP header must be entirely contained within
4407          * the first fragment of a packet.
4408          */
4409         switch (ip->ip_p) {
4410         case IPPROTO_TCP:
4411                 if (iplen < iphlen + sizeof(struct tcphdr))
4412                         return IPPROTO_DONE;
4413                 if (m->m_len < hoff + iphlen + sizeof(struct tcphdr))
4414                         return IPPROTO_DONE;
4415                 th = (const struct tcphdr *)((const uint8_t *)ip + iphlen);
4416                 thoff = th->th_off << 2;
4417                 if (thoff < sizeof(struct tcphdr) || thoff + iphlen > iplen)
4418                         return IPPROTO_DONE;
4419                 if (m->m_len < hoff + iphlen + thoff)
4420                         return IPPROTO_DONE;
4421                 break;
4422         case IPPROTO_UDP:
4423                 if (iplen < iphlen + sizeof(struct udphdr))
4424                         return IPPROTO_DONE;
4425                 if (m->m_len < hoff + iphlen + sizeof(struct udphdr))
4426                         return IPPROTO_DONE;
4427                 break;
4428         default:
4429                 if (iplen < iphlen)
4430                         return IPPROTO_DONE;
4431                 break;
4432         }
4433         return ip->ip_p;
4434 }
4435
4436 static int
4437 hn_create_rx_data(struct hn_softc *sc, int ring_cnt)
4438 {
4439         struct sysctl_oid_list *child;
4440         struct sysctl_ctx_list *ctx;
4441         device_t dev = sc->hn_dev;
4442 #if defined(INET) || defined(INET6)
4443 #if __FreeBSD_version >= 1100095
4444         int lroent_cnt;
4445 #endif
4446 #endif
4447         int i;
4448
4449         /*
4450          * Create RXBUF for reception.
4451          *
4452          * NOTE:
4453          * - It is shared by all channels.
4454          * - A large enough buffer is allocated, certain version of NVSes
4455          *   may further limit the usable space.
4456          */
4457         sc->hn_rxbuf = hyperv_dmamem_alloc(bus_get_dma_tag(dev),
4458             PAGE_SIZE, 0, HN_RXBUF_SIZE, &sc->hn_rxbuf_dma,
4459             BUS_DMA_WAITOK | BUS_DMA_ZERO);
4460         if (sc->hn_rxbuf == NULL) {
4461                 device_printf(sc->hn_dev, "allocate rxbuf failed\n");
4462                 return (ENOMEM);
4463         }
4464
4465         sc->hn_rx_ring_cnt = ring_cnt;
4466         sc->hn_rx_ring_inuse = sc->hn_rx_ring_cnt;
4467
4468         sc->hn_rx_ring = malloc(sizeof(struct hn_rx_ring) * sc->hn_rx_ring_cnt,
4469             M_DEVBUF, M_WAITOK | M_ZERO);
4470
4471 #if defined(INET) || defined(INET6)
4472 #if __FreeBSD_version >= 1100095
4473         lroent_cnt = hn_lro_entry_count;
4474         if (lroent_cnt < TCP_LRO_ENTRIES)
4475                 lroent_cnt = TCP_LRO_ENTRIES;
4476         if (bootverbose)
4477                 device_printf(dev, "LRO: entry count %d\n", lroent_cnt);
4478 #endif
4479 #endif  /* INET || INET6 */
4480
4481         ctx = device_get_sysctl_ctx(dev);
4482         child = SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
4483
4484         /* Create dev.hn.UNIT.rx sysctl tree */
4485         sc->hn_rx_sysctl_tree = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, "rx",
4486             CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
4487
4488         for (i = 0; i < sc->hn_rx_ring_cnt; ++i) {
4489                 struct hn_rx_ring *rxr = &sc->hn_rx_ring[i];
4490
4491                 rxr->hn_br = hyperv_dmamem_alloc(bus_get_dma_tag(dev),
4492                     PAGE_SIZE, 0, HN_TXBR_SIZE + HN_RXBR_SIZE,
4493                     &rxr->hn_br_dma, BUS_DMA_WAITOK);
4494                 if (rxr->hn_br == NULL) {
4495                         device_printf(dev, "allocate bufring failed\n");
4496                         return (ENOMEM);
4497                 }
4498
4499                 if (hn_trust_hosttcp)
4500                         rxr->hn_trust_hcsum |= HN_TRUST_HCSUM_TCP;
4501                 if (hn_trust_hostudp)
4502                         rxr->hn_trust_hcsum |= HN_TRUST_HCSUM_UDP;
4503                 if (hn_trust_hostip)
4504                         rxr->hn_trust_hcsum |= HN_TRUST_HCSUM_IP;
4505                 rxr->hn_ifp = sc->hn_ifp;
4506                 if (i < sc->hn_tx_ring_cnt)
4507                         rxr->hn_txr = &sc->hn_tx_ring[i];
4508                 rxr->hn_pktbuf_len = HN_PKTBUF_LEN_DEF;
4509                 rxr->hn_pktbuf = malloc(rxr->hn_pktbuf_len, M_DEVBUF, M_WAITOK);
4510                 rxr->hn_rx_idx = i;
4511                 rxr->hn_rxbuf = sc->hn_rxbuf;
4512
4513                 /*
4514                  * Initialize LRO.
4515                  */
4516 #if defined(INET) || defined(INET6)
4517 #if __FreeBSD_version >= 1100095
4518                 tcp_lro_init_args(&rxr->hn_lro, sc->hn_ifp, lroent_cnt,
4519                     hn_lro_mbufq_depth);
4520 #else
4521                 tcp_lro_init(&rxr->hn_lro);
4522                 rxr->hn_lro.ifp = sc->hn_ifp;
4523 #endif
4524 #if __FreeBSD_version >= 1100099
4525                 rxr->hn_lro.lro_length_lim = HN_LRO_LENLIM_DEF;
4526                 rxr->hn_lro.lro_ackcnt_lim = HN_LRO_ACKCNT_DEF;
4527 #endif
4528 #endif  /* INET || INET6 */
4529
4530                 if (sc->hn_rx_sysctl_tree != NULL) {
4531                         char name[16];
4532
4533                         /*
4534                          * Create per RX ring sysctl tree:
4535                          * dev.hn.UNIT.rx.RINGID
4536                          */
4537                         snprintf(name, sizeof(name), "%d", i);
4538                         rxr->hn_rx_sysctl_tree = SYSCTL_ADD_NODE(ctx,
4539                             SYSCTL_CHILDREN(sc->hn_rx_sysctl_tree),
4540                             OID_AUTO, name, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
4541
4542                         if (rxr->hn_rx_sysctl_tree != NULL) {
4543                                 SYSCTL_ADD_ULONG(ctx,
4544                                     SYSCTL_CHILDREN(rxr->hn_rx_sysctl_tree),
4545                                     OID_AUTO, "packets", CTLFLAG_RW,
4546                                     &rxr->hn_pkts, "# of packets received");
4547                                 SYSCTL_ADD_ULONG(ctx,
4548                                     SYSCTL_CHILDREN(rxr->hn_rx_sysctl_tree),
4549                                     OID_AUTO, "rss_pkts", CTLFLAG_RW,
4550                                     &rxr->hn_rss_pkts,
4551                                     "# of packets w/ RSS info received");
4552                                 SYSCTL_ADD_INT(ctx,
4553                                     SYSCTL_CHILDREN(rxr->hn_rx_sysctl_tree),
4554                                     OID_AUTO, "pktbuf_len", CTLFLAG_RD,
4555                                     &rxr->hn_pktbuf_len, 0,
4556                                     "Temporary channel packet buffer length");
4557                         }
4558                 }
4559         }
4560
4561         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "lro_queued",
4562             CTLTYPE_U64 | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
4563             __offsetof(struct hn_rx_ring, hn_lro.lro_queued),
4564 #if __FreeBSD_version < 1100095
4565             hn_rx_stat_int_sysctl,
4566 #else
4567             hn_rx_stat_u64_sysctl,
4568 #endif
4569             "LU", "LRO queued");
4570         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "lro_flushed",
4571             CTLTYPE_U64 | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
4572             __offsetof(struct hn_rx_ring, hn_lro.lro_flushed),
4573 #if __FreeBSD_version < 1100095
4574             hn_rx_stat_int_sysctl,
4575 #else
4576             hn_rx_stat_u64_sysctl,
4577 #endif
4578             "LU", "LRO flushed");
4579         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "lro_tried",
4580             CTLTYPE_ULONG | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
4581             __offsetof(struct hn_rx_ring, hn_lro_tried),
4582             hn_rx_stat_ulong_sysctl, "LU", "# of LRO tries");
4583 #if __FreeBSD_version >= 1100099
4584         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "lro_length_lim",
4585             CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, 0,
4586             hn_lro_lenlim_sysctl, "IU",
4587             "Max # of data bytes to be aggregated by LRO");
4588         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "lro_ackcnt_lim",
4589             CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, 0,
4590             hn_lro_ackcnt_sysctl, "I",
4591             "Max # of ACKs to be aggregated by LRO");
4592 #endif
4593         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "trust_hosttcp",
4594             CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, HN_TRUST_HCSUM_TCP,
4595             hn_trust_hcsum_sysctl, "I",
4596             "Trust tcp segement verification on host side, "
4597             "when csum info is missing");
4598         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "trust_hostudp",
4599             CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, HN_TRUST_HCSUM_UDP,
4600             hn_trust_hcsum_sysctl, "I",
4601             "Trust udp datagram verification on host side, "
4602             "when csum info is missing");
4603         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "trust_hostip",
4604             CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, HN_TRUST_HCSUM_IP,
4605             hn_trust_hcsum_sysctl, "I",
4606             "Trust ip packet verification on host side, "
4607             "when csum info is missing");
4608         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "csum_ip",
4609             CTLTYPE_ULONG | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
4610             __offsetof(struct hn_rx_ring, hn_csum_ip),
4611             hn_rx_stat_ulong_sysctl, "LU", "RXCSUM IP");
4612         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "csum_tcp",
4613             CTLTYPE_ULONG | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
4614             __offsetof(struct hn_rx_ring, hn_csum_tcp),
4615             hn_rx_stat_ulong_sysctl, "LU", "RXCSUM TCP");
4616         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "csum_udp",
4617             CTLTYPE_ULONG | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
4618             __offsetof(struct hn_rx_ring, hn_csum_udp),
4619             hn_rx_stat_ulong_sysctl, "LU", "RXCSUM UDP");
4620         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "csum_trusted",
4621             CTLTYPE_ULONG | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
4622             __offsetof(struct hn_rx_ring, hn_csum_trusted),
4623             hn_rx_stat_ulong_sysctl, "LU",
4624             "# of packets that we trust host's csum verification");
4625         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "small_pkts",
4626             CTLTYPE_ULONG | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
4627             __offsetof(struct hn_rx_ring, hn_small_pkts),
4628             hn_rx_stat_ulong_sysctl, "LU", "# of small packets received");
4629         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "rx_ack_failed",
4630             CTLTYPE_ULONG | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
4631             __offsetof(struct hn_rx_ring, hn_ack_failed),
4632             hn_rx_stat_ulong_sysctl, "LU", "# of RXBUF ack failures");
4633         SYSCTL_ADD_INT(ctx, child, OID_AUTO, "rx_ring_cnt",
4634             CTLFLAG_RD, &sc->hn_rx_ring_cnt, 0, "# created RX rings");
4635         SYSCTL_ADD_INT(ctx, child, OID_AUTO, "rx_ring_inuse",
4636             CTLFLAG_RD, &sc->hn_rx_ring_inuse, 0, "# used RX rings");
4637
4638         return (0);
4639 }
4640
4641 static void
4642 hn_destroy_rx_data(struct hn_softc *sc)
4643 {
4644         int i;
4645
4646         if (sc->hn_rxbuf != NULL) {
4647                 if ((sc->hn_flags & HN_FLAG_RXBUF_REF) == 0)
4648                         hyperv_dmamem_free(&sc->hn_rxbuf_dma, sc->hn_rxbuf);
4649                 else
4650                         device_printf(sc->hn_dev, "RXBUF is referenced\n");
4651                 sc->hn_rxbuf = NULL;
4652         }
4653
4654         if (sc->hn_rx_ring_cnt == 0)
4655                 return;
4656
4657         for (i = 0; i < sc->hn_rx_ring_cnt; ++i) {
4658                 struct hn_rx_ring *rxr = &sc->hn_rx_ring[i];
4659
4660                 if (rxr->hn_br == NULL)
4661                         continue;
4662                 if ((rxr->hn_rx_flags & HN_RX_FLAG_BR_REF) == 0) {
4663                         hyperv_dmamem_free(&rxr->hn_br_dma, rxr->hn_br);
4664                 } else {
4665                         device_printf(sc->hn_dev,
4666                             "%dth channel bufring is referenced", i);
4667                 }
4668                 rxr->hn_br = NULL;
4669
4670 #if defined(INET) || defined(INET6)
4671                 tcp_lro_free(&rxr->hn_lro);
4672 #endif
4673                 free(rxr->hn_pktbuf, M_DEVBUF);
4674         }
4675         free(sc->hn_rx_ring, M_DEVBUF);
4676         sc->hn_rx_ring = NULL;
4677
4678         sc->hn_rx_ring_cnt = 0;
4679         sc->hn_rx_ring_inuse = 0;
4680 }
4681
4682 static int
4683 hn_tx_ring_create(struct hn_softc *sc, int id)
4684 {
4685         struct hn_tx_ring *txr = &sc->hn_tx_ring[id];
4686         device_t dev = sc->hn_dev;
4687         bus_dma_tag_t parent_dtag;
4688         int error, i;
4689
4690         txr->hn_sc = sc;
4691         txr->hn_tx_idx = id;
4692
4693 #ifndef HN_USE_TXDESC_BUFRING
4694         mtx_init(&txr->hn_txlist_spin, "hn txlist", NULL, MTX_SPIN);
4695 #endif
4696         mtx_init(&txr->hn_tx_lock, "hn tx", NULL, MTX_DEF);
4697
4698         txr->hn_txdesc_cnt = HN_TX_DESC_CNT;
4699         txr->hn_txdesc = malloc(sizeof(struct hn_txdesc) * txr->hn_txdesc_cnt,
4700             M_DEVBUF, M_WAITOK | M_ZERO);
4701 #ifndef HN_USE_TXDESC_BUFRING
4702         SLIST_INIT(&txr->hn_txlist);
4703 #else
4704         txr->hn_txdesc_br = buf_ring_alloc(txr->hn_txdesc_cnt, M_DEVBUF,
4705             M_WAITOK, &txr->hn_tx_lock);
4706 #endif
4707
4708         if (hn_tx_taskq_mode == HN_TX_TASKQ_M_EVTTQ) {
4709                 txr->hn_tx_taskq = VMBUS_GET_EVENT_TASKQ(
4710                     device_get_parent(dev), dev, HN_RING_IDX2CPU(sc, id));
4711         } else {
4712                 txr->hn_tx_taskq = sc->hn_tx_taskqs[id % hn_tx_taskq_cnt];
4713         }
4714
4715 #ifdef HN_IFSTART_SUPPORT
4716         if (hn_use_if_start) {
4717                 txr->hn_txeof = hn_start_txeof;
4718                 TASK_INIT(&txr->hn_tx_task, 0, hn_start_taskfunc, txr);
4719                 TASK_INIT(&txr->hn_txeof_task, 0, hn_start_txeof_taskfunc, txr);
4720         } else
4721 #endif
4722         {
4723                 int br_depth;
4724
4725                 txr->hn_txeof = hn_xmit_txeof;
4726                 TASK_INIT(&txr->hn_tx_task, 0, hn_xmit_taskfunc, txr);
4727                 TASK_INIT(&txr->hn_txeof_task, 0, hn_xmit_txeof_taskfunc, txr);
4728
4729                 br_depth = hn_get_txswq_depth(txr);
4730                 txr->hn_mbuf_br = buf_ring_alloc(br_depth, M_DEVBUF,
4731                     M_WAITOK, &txr->hn_tx_lock);
4732         }
4733
4734         txr->hn_direct_tx_size = hn_direct_tx_size;
4735
4736         /*
4737          * Always schedule transmission instead of trying to do direct
4738          * transmission.  This one gives the best performance so far.
4739          */
4740         txr->hn_sched_tx = 1;
4741
4742         parent_dtag = bus_get_dma_tag(dev);
4743
4744         /* DMA tag for RNDIS packet messages. */
4745         error = bus_dma_tag_create(parent_dtag, /* parent */
4746             HN_RNDIS_PKT_ALIGN,         /* alignment */
4747             HN_RNDIS_PKT_BOUNDARY,      /* boundary */
4748             BUS_SPACE_MAXADDR,          /* lowaddr */
4749             BUS_SPACE_MAXADDR,          /* highaddr */
4750             NULL, NULL,                 /* filter, filterarg */
4751             HN_RNDIS_PKT_LEN,           /* maxsize */
4752             1,                          /* nsegments */
4753             HN_RNDIS_PKT_LEN,           /* maxsegsize */
4754             0,                          /* flags */
4755             NULL,                       /* lockfunc */
4756             NULL,                       /* lockfuncarg */
4757             &txr->hn_tx_rndis_dtag);
4758         if (error) {
4759                 device_printf(dev, "failed to create rndis dmatag\n");
4760                 return error;
4761         }
4762
4763         /* DMA tag for data. */
4764         error = bus_dma_tag_create(parent_dtag, /* parent */
4765             1,                          /* alignment */
4766             HN_TX_DATA_BOUNDARY,        /* boundary */
4767             BUS_SPACE_MAXADDR,          /* lowaddr */
4768             BUS_SPACE_MAXADDR,          /* highaddr */
4769             NULL, NULL,                 /* filter, filterarg */
4770             HN_TX_DATA_MAXSIZE,         /* maxsize */
4771             HN_TX_DATA_SEGCNT_MAX,      /* nsegments */
4772             HN_TX_DATA_SEGSIZE,         /* maxsegsize */
4773             0,                          /* flags */
4774             NULL,                       /* lockfunc */
4775             NULL,                       /* lockfuncarg */
4776             &txr->hn_tx_data_dtag);
4777         if (error) {
4778                 device_printf(dev, "failed to create data dmatag\n");
4779                 return error;
4780         }
4781
4782         for (i = 0; i < txr->hn_txdesc_cnt; ++i) {
4783                 struct hn_txdesc *txd = &txr->hn_txdesc[i];
4784
4785                 txd->txr = txr;
4786                 txd->chim_index = HN_NVS_CHIM_IDX_INVALID;
4787                 STAILQ_INIT(&txd->agg_list);
4788
4789                 /*
4790                  * Allocate and load RNDIS packet message.
4791                  */
4792                 error = bus_dmamem_alloc(txr->hn_tx_rndis_dtag,
4793                     (void **)&txd->rndis_pkt,
4794                     BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO,
4795                     &txd->rndis_pkt_dmap);
4796                 if (error) {
4797                         device_printf(dev,
4798                             "failed to allocate rndis_packet_msg, %d\n", i);
4799                         return error;
4800                 }
4801
4802                 error = bus_dmamap_load(txr->hn_tx_rndis_dtag,
4803                     txd->rndis_pkt_dmap,
4804                     txd->rndis_pkt, HN_RNDIS_PKT_LEN,
4805                     hyperv_dma_map_paddr, &txd->rndis_pkt_paddr,
4806                     BUS_DMA_NOWAIT);
4807                 if (error) {
4808                         device_printf(dev,
4809                             "failed to load rndis_packet_msg, %d\n", i);
4810                         bus_dmamem_free(txr->hn_tx_rndis_dtag,
4811                             txd->rndis_pkt, txd->rndis_pkt_dmap);
4812                         return error;
4813                 }
4814
4815                 /* DMA map for TX data. */
4816                 error = bus_dmamap_create(txr->hn_tx_data_dtag, 0,
4817                     &txd->data_dmap);
4818                 if (error) {
4819                         device_printf(dev,
4820                             "failed to allocate tx data dmamap\n");
4821                         bus_dmamap_unload(txr->hn_tx_rndis_dtag,
4822                             txd->rndis_pkt_dmap);
4823                         bus_dmamem_free(txr->hn_tx_rndis_dtag,
4824                             txd->rndis_pkt, txd->rndis_pkt_dmap);
4825                         return error;
4826                 }
4827
4828                 /* All set, put it to list */
4829                 txd->flags |= HN_TXD_FLAG_ONLIST;
4830 #ifndef HN_USE_TXDESC_BUFRING
4831                 SLIST_INSERT_HEAD(&txr->hn_txlist, txd, link);
4832 #else
4833                 buf_ring_enqueue(txr->hn_txdesc_br, txd);
4834 #endif
4835         }
4836         txr->hn_txdesc_avail = txr->hn_txdesc_cnt;
4837
4838         if (sc->hn_tx_sysctl_tree != NULL) {
4839                 struct sysctl_oid_list *child;
4840                 struct sysctl_ctx_list *ctx;
4841                 char name[16];
4842
4843                 /*
4844                  * Create per TX ring sysctl tree:
4845                  * dev.hn.UNIT.tx.RINGID
4846                  */
4847                 ctx = device_get_sysctl_ctx(dev);
4848                 child = SYSCTL_CHILDREN(sc->hn_tx_sysctl_tree);
4849
4850                 snprintf(name, sizeof(name), "%d", id);
4851                 txr->hn_tx_sysctl_tree = SYSCTL_ADD_NODE(ctx, child, OID_AUTO,
4852                     name, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
4853
4854                 if (txr->hn_tx_sysctl_tree != NULL) {
4855                         child = SYSCTL_CHILDREN(txr->hn_tx_sysctl_tree);
4856
4857 #ifdef HN_DEBUG
4858                         SYSCTL_ADD_INT(ctx, child, OID_AUTO, "txdesc_avail",
4859                             CTLFLAG_RD, &txr->hn_txdesc_avail, 0,
4860                             "# of available TX descs");
4861 #endif
4862 #ifdef HN_IFSTART_SUPPORT
4863                         if (!hn_use_if_start)
4864 #endif
4865                         {
4866                                 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "oactive",
4867                                     CTLFLAG_RD, &txr->hn_oactive, 0,
4868                                     "over active");
4869                         }
4870                         SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "packets",
4871                             CTLFLAG_RW, &txr->hn_pkts,
4872                             "# of packets transmitted");
4873                         SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "sends",
4874                             CTLFLAG_RW, &txr->hn_sends, "# of sends");
4875                 }
4876         }
4877
4878         return 0;
4879 }
4880
4881 static void
4882 hn_txdesc_dmamap_destroy(struct hn_txdesc *txd)
4883 {
4884         struct hn_tx_ring *txr = txd->txr;
4885
4886         KASSERT(txd->m == NULL, ("still has mbuf installed"));
4887         KASSERT((txd->flags & HN_TXD_FLAG_DMAMAP) == 0, ("still dma mapped"));
4888
4889         bus_dmamap_unload(txr->hn_tx_rndis_dtag, txd->rndis_pkt_dmap);
4890         bus_dmamem_free(txr->hn_tx_rndis_dtag, txd->rndis_pkt,
4891             txd->rndis_pkt_dmap);
4892         bus_dmamap_destroy(txr->hn_tx_data_dtag, txd->data_dmap);
4893 }
4894
4895 static void
4896 hn_txdesc_gc(struct hn_tx_ring *txr, struct hn_txdesc *txd)
4897 {
4898
4899         KASSERT(txd->refs == 0 || txd->refs == 1,
4900             ("invalid txd refs %d", txd->refs));
4901
4902         /* Aggregated txds will be freed by their aggregating txd. */
4903         if (txd->refs > 0 && (txd->flags & HN_TXD_FLAG_ONAGG) == 0) {
4904                 int freed;
4905
4906                 freed = hn_txdesc_put(txr, txd);
4907                 KASSERT(freed, ("can't free txdesc"));
4908         }
4909 }
4910
4911 static void
4912 hn_tx_ring_destroy(struct hn_tx_ring *txr)
4913 {
4914         int i;
4915
4916         if (txr->hn_txdesc == NULL)
4917                 return;
4918
4919         /*
4920          * NOTE:
4921          * Because the freeing of aggregated txds will be deferred
4922          * to the aggregating txd, two passes are used here:
4923          * - The first pass GCes any pending txds.  This GC is necessary,
4924          *   since if the channels are revoked, hypervisor will not
4925          *   deliver send-done for all pending txds.
4926          * - The second pass frees the busdma stuffs, i.e. after all txds
4927          *   were freed.
4928          */
4929         for (i = 0; i < txr->hn_txdesc_cnt; ++i)
4930                 hn_txdesc_gc(txr, &txr->hn_txdesc[i]);
4931         for (i = 0; i < txr->hn_txdesc_cnt; ++i)
4932                 hn_txdesc_dmamap_destroy(&txr->hn_txdesc[i]);
4933
4934         if (txr->hn_tx_data_dtag != NULL)
4935                 bus_dma_tag_destroy(txr->hn_tx_data_dtag);
4936         if (txr->hn_tx_rndis_dtag != NULL)
4937                 bus_dma_tag_destroy(txr->hn_tx_rndis_dtag);
4938
4939 #ifdef HN_USE_TXDESC_BUFRING
4940         buf_ring_free(txr->hn_txdesc_br, M_DEVBUF);
4941 #endif
4942
4943         free(txr->hn_txdesc, M_DEVBUF);
4944         txr->hn_txdesc = NULL;
4945
4946         if (txr->hn_mbuf_br != NULL)
4947                 buf_ring_free(txr->hn_mbuf_br, M_DEVBUF);
4948
4949 #ifndef HN_USE_TXDESC_BUFRING
4950         mtx_destroy(&txr->hn_txlist_spin);
4951 #endif
4952         mtx_destroy(&txr->hn_tx_lock);
4953 }
4954
4955 static int
4956 hn_create_tx_data(struct hn_softc *sc, int ring_cnt)
4957 {
4958         struct sysctl_oid_list *child;
4959         struct sysctl_ctx_list *ctx;
4960         int i;
4961
4962         /*
4963          * Create TXBUF for chimney sending.
4964          *
4965          * NOTE: It is shared by all channels.
4966          */
4967         sc->hn_chim = hyperv_dmamem_alloc(bus_get_dma_tag(sc->hn_dev),
4968             PAGE_SIZE, 0, HN_CHIM_SIZE, &sc->hn_chim_dma,
4969             BUS_DMA_WAITOK | BUS_DMA_ZERO);
4970         if (sc->hn_chim == NULL) {
4971                 device_printf(sc->hn_dev, "allocate txbuf failed\n");
4972                 return (ENOMEM);
4973         }
4974
4975         sc->hn_tx_ring_cnt = ring_cnt;
4976         sc->hn_tx_ring_inuse = sc->hn_tx_ring_cnt;
4977
4978         sc->hn_tx_ring = malloc(sizeof(struct hn_tx_ring) * sc->hn_tx_ring_cnt,
4979             M_DEVBUF, M_WAITOK | M_ZERO);
4980
4981         ctx = device_get_sysctl_ctx(sc->hn_dev);
4982         child = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->hn_dev));
4983
4984         /* Create dev.hn.UNIT.tx sysctl tree */
4985         sc->hn_tx_sysctl_tree = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, "tx",
4986             CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
4987
4988         for (i = 0; i < sc->hn_tx_ring_cnt; ++i) {
4989                 int error;
4990
4991                 error = hn_tx_ring_create(sc, i);
4992                 if (error)
4993                         return error;
4994         }
4995
4996         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "no_txdescs",
4997             CTLTYPE_ULONG | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
4998             __offsetof(struct hn_tx_ring, hn_no_txdescs),
4999             hn_tx_stat_ulong_sysctl, "LU", "# of times short of TX descs");
5000         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "send_failed",
5001             CTLTYPE_ULONG | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
5002             __offsetof(struct hn_tx_ring, hn_send_failed),
5003             hn_tx_stat_ulong_sysctl, "LU", "# of hyper-v sending failure");
5004         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "txdma_failed",
5005             CTLTYPE_ULONG | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
5006             __offsetof(struct hn_tx_ring, hn_txdma_failed),
5007             hn_tx_stat_ulong_sysctl, "LU", "# of TX DMA failure");
5008         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "agg_flush_failed",
5009             CTLTYPE_ULONG | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
5010             __offsetof(struct hn_tx_ring, hn_flush_failed),
5011             hn_tx_stat_ulong_sysctl, "LU",
5012             "# of packet transmission aggregation flush failure");
5013         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "tx_collapsed",
5014             CTLTYPE_ULONG | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
5015             __offsetof(struct hn_tx_ring, hn_tx_collapsed),
5016             hn_tx_stat_ulong_sysctl, "LU", "# of TX mbuf collapsed");
5017         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "tx_chimney",
5018             CTLTYPE_ULONG | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
5019             __offsetof(struct hn_tx_ring, hn_tx_chimney),
5020             hn_tx_stat_ulong_sysctl, "LU", "# of chimney send");
5021         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "tx_chimney_tried",
5022             CTLTYPE_ULONG | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
5023             __offsetof(struct hn_tx_ring, hn_tx_chimney_tried),
5024             hn_tx_stat_ulong_sysctl, "LU", "# of chimney send tries");
5025         SYSCTL_ADD_INT(ctx, child, OID_AUTO, "txdesc_cnt",
5026             CTLFLAG_RD, &sc->hn_tx_ring[0].hn_txdesc_cnt, 0,
5027             "# of total TX descs");
5028         SYSCTL_ADD_INT(ctx, child, OID_AUTO, "tx_chimney_max",
5029             CTLFLAG_RD, &sc->hn_chim_szmax, 0,
5030             "Chimney send packet size upper boundary");
5031         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "tx_chimney_size",
5032             CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, 0,
5033             hn_chim_size_sysctl, "I", "Chimney send packet size limit");
5034         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "direct_tx_size",
5035             CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
5036             __offsetof(struct hn_tx_ring, hn_direct_tx_size),
5037             hn_tx_conf_int_sysctl, "I",
5038             "Size of the packet for direct transmission");
5039         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "sched_tx",
5040             CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
5041             __offsetof(struct hn_tx_ring, hn_sched_tx),
5042             hn_tx_conf_int_sysctl, "I",
5043             "Always schedule transmission "
5044             "instead of doing direct transmission");
5045         SYSCTL_ADD_INT(ctx, child, OID_AUTO, "tx_ring_cnt",
5046             CTLFLAG_RD, &sc->hn_tx_ring_cnt, 0, "# created TX rings");
5047         SYSCTL_ADD_INT(ctx, child, OID_AUTO, "tx_ring_inuse",
5048             CTLFLAG_RD, &sc->hn_tx_ring_inuse, 0, "# used TX rings");
5049         SYSCTL_ADD_INT(ctx, child, OID_AUTO, "agg_szmax",
5050             CTLFLAG_RD, &sc->hn_tx_ring[0].hn_agg_szmax, 0,
5051             "Applied packet transmission aggregation size");
5052         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "agg_pktmax",
5053             CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
5054             hn_txagg_pktmax_sysctl, "I",
5055             "Applied packet transmission aggregation packets");
5056         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "agg_align",
5057             CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
5058             hn_txagg_align_sysctl, "I",
5059             "Applied packet transmission aggregation alignment");
5060
5061         return 0;
5062 }
5063
5064 static void
5065 hn_set_chim_size(struct hn_softc *sc, int chim_size)
5066 {
5067         int i;
5068
5069         for (i = 0; i < sc->hn_tx_ring_cnt; ++i)
5070                 sc->hn_tx_ring[i].hn_chim_size = chim_size;
5071 }
5072
5073 static void
5074 hn_set_tso_maxsize(struct hn_softc *sc, int tso_maxlen, int mtu)
5075 {
5076         struct ifnet *ifp = sc->hn_ifp;
5077         u_int hw_tsomax;
5078         int tso_minlen;
5079
5080         HN_LOCK_ASSERT(sc);
5081
5082         if ((ifp->if_capabilities & (IFCAP_TSO4 | IFCAP_TSO6)) == 0)
5083                 return;
5084
5085         KASSERT(sc->hn_ndis_tso_sgmin >= 2,
5086             ("invalid NDIS tso sgmin %d", sc->hn_ndis_tso_sgmin));
5087         tso_minlen = sc->hn_ndis_tso_sgmin * mtu;
5088
5089         KASSERT(sc->hn_ndis_tso_szmax >= tso_minlen &&
5090             sc->hn_ndis_tso_szmax <= IP_MAXPACKET,
5091             ("invalid NDIS tso szmax %d", sc->hn_ndis_tso_szmax));
5092
5093         if (tso_maxlen < tso_minlen)
5094                 tso_maxlen = tso_minlen;
5095         else if (tso_maxlen > IP_MAXPACKET)
5096                 tso_maxlen = IP_MAXPACKET;
5097         if (tso_maxlen > sc->hn_ndis_tso_szmax)
5098                 tso_maxlen = sc->hn_ndis_tso_szmax;
5099         hw_tsomax = tso_maxlen - (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN);
5100
5101         if (hn_xpnt_vf_isready(sc)) {
5102                 if (hw_tsomax > sc->hn_vf_ifp->if_hw_tsomax)
5103                         hw_tsomax = sc->hn_vf_ifp->if_hw_tsomax;
5104         }
5105         ifp->if_hw_tsomax = hw_tsomax;
5106         if (bootverbose)
5107                 if_printf(ifp, "TSO size max %u\n", ifp->if_hw_tsomax);
5108 }
5109
5110 static void
5111 hn_fixup_tx_data(struct hn_softc *sc)
5112 {
5113         uint64_t csum_assist;
5114         int i;
5115
5116         hn_set_chim_size(sc, sc->hn_chim_szmax);
5117         if (hn_tx_chimney_size > 0 &&
5118             hn_tx_chimney_size < sc->hn_chim_szmax)
5119                 hn_set_chim_size(sc, hn_tx_chimney_size);
5120
5121         csum_assist = 0;
5122         if (sc->hn_caps & HN_CAP_IPCS)
5123                 csum_assist |= CSUM_IP;
5124         if (sc->hn_caps & HN_CAP_TCP4CS)
5125                 csum_assist |= CSUM_IP_TCP;
5126         if (sc->hn_caps & HN_CAP_UDP4CS)
5127                 csum_assist |= CSUM_IP_UDP;
5128         if (sc->hn_caps & HN_CAP_TCP6CS)
5129                 csum_assist |= CSUM_IP6_TCP;
5130         if (sc->hn_caps & HN_CAP_UDP6CS)
5131                 csum_assist |= CSUM_IP6_UDP;
5132         for (i = 0; i < sc->hn_tx_ring_cnt; ++i)
5133                 sc->hn_tx_ring[i].hn_csum_assist = csum_assist;
5134
5135         if (sc->hn_caps & HN_CAP_HASHVAL) {
5136                 /*
5137                  * Support HASHVAL pktinfo on TX path.
5138                  */
5139                 if (bootverbose)
5140                         if_printf(sc->hn_ifp, "support HASHVAL pktinfo\n");
5141                 for (i = 0; i < sc->hn_tx_ring_cnt; ++i)
5142                         sc->hn_tx_ring[i].hn_tx_flags |= HN_TX_FLAG_HASHVAL;
5143         }
5144 }
5145
5146 static void
5147 hn_destroy_tx_data(struct hn_softc *sc)
5148 {
5149         int i;
5150
5151         if (sc->hn_chim != NULL) {
5152                 if ((sc->hn_flags & HN_FLAG_CHIM_REF) == 0) {
5153                         hyperv_dmamem_free(&sc->hn_chim_dma, sc->hn_chim);
5154                 } else {
5155                         device_printf(sc->hn_dev,
5156                             "chimney sending buffer is referenced");
5157                 }
5158                 sc->hn_chim = NULL;
5159         }
5160
5161         if (sc->hn_tx_ring_cnt == 0)
5162                 return;
5163
5164         for (i = 0; i < sc->hn_tx_ring_cnt; ++i)
5165                 hn_tx_ring_destroy(&sc->hn_tx_ring[i]);
5166
5167         free(sc->hn_tx_ring, M_DEVBUF);
5168         sc->hn_tx_ring = NULL;
5169
5170         sc->hn_tx_ring_cnt = 0;
5171         sc->hn_tx_ring_inuse = 0;
5172 }
5173
5174 #ifdef HN_IFSTART_SUPPORT
5175
5176 static void
5177 hn_start_taskfunc(void *xtxr, int pending __unused)
5178 {
5179         struct hn_tx_ring *txr = xtxr;
5180
5181         mtx_lock(&txr->hn_tx_lock);
5182         hn_start_locked(txr, 0);
5183         mtx_unlock(&txr->hn_tx_lock);
5184 }
5185
5186 static int
5187 hn_start_locked(struct hn_tx_ring *txr, int len)
5188 {
5189         struct hn_softc *sc = txr->hn_sc;
5190         struct ifnet *ifp = sc->hn_ifp;
5191         int sched = 0;
5192
5193         KASSERT(hn_use_if_start,
5194             ("hn_start_locked is called, when if_start is disabled"));
5195         KASSERT(txr == &sc->hn_tx_ring[0], ("not the first TX ring"));
5196         mtx_assert(&txr->hn_tx_lock, MA_OWNED);
5197         KASSERT(txr->hn_agg_txd == NULL, ("lingering aggregating txdesc"));
5198
5199         if (__predict_false(txr->hn_suspended))
5200                 return (0);
5201
5202         if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
5203             IFF_DRV_RUNNING)
5204                 return (0);
5205
5206         while (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) {
5207                 struct hn_txdesc *txd;
5208                 struct mbuf *m_head;
5209                 int error;
5210
5211                 IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
5212                 if (m_head == NULL)
5213                         break;
5214
5215                 if (len > 0 && m_head->m_pkthdr.len > len) {
5216                         /*
5217                          * This sending could be time consuming; let callers
5218                          * dispatch this packet sending (and sending of any
5219                          * following up packets) to tx taskqueue.
5220                          */
5221                         IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
5222                         sched = 1;
5223                         break;
5224                 }
5225
5226 #if defined(INET6) || defined(INET)
5227                 if (m_head->m_pkthdr.csum_flags & CSUM_TSO) {
5228                         m_head = hn_tso_fixup(m_head);
5229                         if (__predict_false(m_head == NULL)) {
5230                                 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
5231                                 continue;
5232                         }
5233                 }
5234 #endif
5235
5236                 txd = hn_txdesc_get(txr);
5237                 if (txd == NULL) {
5238                         txr->hn_no_txdescs++;
5239                         IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
5240                         atomic_set_int(&ifp->if_drv_flags, IFF_DRV_OACTIVE);
5241                         break;
5242                 }
5243
5244                 error = hn_encap(ifp, txr, txd, &m_head);
5245                 if (error) {
5246                         /* Both txd and m_head are freed */
5247                         KASSERT(txr->hn_agg_txd == NULL,
5248                             ("encap failed w/ pending aggregating txdesc"));
5249                         continue;
5250                 }
5251
5252                 if (txr->hn_agg_pktleft == 0) {
5253                         if (txr->hn_agg_txd != NULL) {
5254                                 KASSERT(m_head == NULL,
5255                                     ("pending mbuf for aggregating txdesc"));
5256                                 error = hn_flush_txagg(ifp, txr);
5257                                 if (__predict_false(error)) {
5258                                         atomic_set_int(&ifp->if_drv_flags,
5259                                             IFF_DRV_OACTIVE);
5260                                         break;
5261                                 }
5262                         } else {
5263                                 KASSERT(m_head != NULL, ("mbuf was freed"));
5264                                 error = hn_txpkt(ifp, txr, txd);
5265                                 if (__predict_false(error)) {
5266                                         /* txd is freed, but m_head is not */
5267                                         IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
5268                                         atomic_set_int(&ifp->if_drv_flags,
5269                                             IFF_DRV_OACTIVE);
5270                                         break;
5271                                 }
5272                         }
5273                 }
5274 #ifdef INVARIANTS
5275                 else {
5276                         KASSERT(txr->hn_agg_txd != NULL,
5277                             ("no aggregating txdesc"));
5278                         KASSERT(m_head == NULL,
5279                             ("pending mbuf for aggregating txdesc"));
5280                 }
5281 #endif
5282         }
5283
5284         /* Flush pending aggerated transmission. */
5285         if (txr->hn_agg_txd != NULL)
5286                 hn_flush_txagg(ifp, txr);
5287         return (sched);
5288 }
5289
5290 static void
5291 hn_start(struct ifnet *ifp)
5292 {
5293         struct hn_softc *sc = ifp->if_softc;
5294         struct hn_tx_ring *txr = &sc->hn_tx_ring[0];
5295
5296         if (txr->hn_sched_tx)
5297                 goto do_sched;
5298
5299         if (mtx_trylock(&txr->hn_tx_lock)) {
5300                 int sched;
5301
5302                 sched = hn_start_locked(txr, txr->hn_direct_tx_size);
5303                 mtx_unlock(&txr->hn_tx_lock);
5304                 if (!sched)
5305                         return;
5306         }
5307 do_sched:
5308         taskqueue_enqueue(txr->hn_tx_taskq, &txr->hn_tx_task);
5309 }
5310
5311 static void
5312 hn_start_txeof_taskfunc(void *xtxr, int pending __unused)
5313 {
5314         struct hn_tx_ring *txr = xtxr;
5315
5316         mtx_lock(&txr->hn_tx_lock);
5317         atomic_clear_int(&txr->hn_sc->hn_ifp->if_drv_flags, IFF_DRV_OACTIVE);
5318         hn_start_locked(txr, 0);
5319         mtx_unlock(&txr->hn_tx_lock);
5320 }
5321
5322 static void
5323 hn_start_txeof(struct hn_tx_ring *txr)
5324 {
5325         struct hn_softc *sc = txr->hn_sc;
5326         struct ifnet *ifp = sc->hn_ifp;
5327
5328         KASSERT(txr == &sc->hn_tx_ring[0], ("not the first TX ring"));
5329
5330         if (txr->hn_sched_tx)
5331                 goto do_sched;
5332
5333         if (mtx_trylock(&txr->hn_tx_lock)) {
5334                 int sched;
5335
5336                 atomic_clear_int(&ifp->if_drv_flags, IFF_DRV_OACTIVE);
5337                 sched = hn_start_locked(txr, txr->hn_direct_tx_size);
5338                 mtx_unlock(&txr->hn_tx_lock);
5339                 if (sched) {
5340                         taskqueue_enqueue(txr->hn_tx_taskq,
5341                             &txr->hn_tx_task);
5342                 }
5343         } else {
5344 do_sched:
5345                 /*
5346                  * Release the OACTIVE earlier, with the hope, that
5347                  * others could catch up.  The task will clear the
5348                  * flag again with the hn_tx_lock to avoid possible
5349                  * races.
5350                  */
5351                 atomic_clear_int(&ifp->if_drv_flags, IFF_DRV_OACTIVE);
5352                 taskqueue_enqueue(txr->hn_tx_taskq, &txr->hn_txeof_task);
5353         }
5354 }
5355
5356 #endif  /* HN_IFSTART_SUPPORT */
5357
5358 static int
5359 hn_xmit(struct hn_tx_ring *txr, int len)
5360 {
5361         struct hn_softc *sc = txr->hn_sc;
5362         struct ifnet *ifp = sc->hn_ifp;
5363         struct mbuf *m_head;
5364         int sched = 0;
5365
5366         mtx_assert(&txr->hn_tx_lock, MA_OWNED);
5367 #ifdef HN_IFSTART_SUPPORT
5368         KASSERT(hn_use_if_start == 0,
5369             ("hn_xmit is called, when if_start is enabled"));
5370 #endif
5371         KASSERT(txr->hn_agg_txd == NULL, ("lingering aggregating txdesc"));
5372
5373         if (__predict_false(txr->hn_suspended))
5374                 return (0);
5375
5376         if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || txr->hn_oactive)
5377                 return (0);
5378
5379         while ((m_head = drbr_peek(ifp, txr->hn_mbuf_br)) != NULL) {
5380                 struct hn_txdesc *txd;
5381                 int error;
5382
5383                 if (len > 0 && m_head->m_pkthdr.len > len) {
5384                         /*
5385                          * This sending could be time consuming; let callers
5386                          * dispatch this packet sending (and sending of any
5387                          * following up packets) to tx taskqueue.
5388                          */
5389                         drbr_putback(ifp, txr->hn_mbuf_br, m_head);
5390                         sched = 1;
5391                         break;
5392                 }
5393
5394                 txd = hn_txdesc_get(txr);
5395                 if (txd == NULL) {
5396                         txr->hn_no_txdescs++;
5397                         drbr_putback(ifp, txr->hn_mbuf_br, m_head);
5398                         txr->hn_oactive = 1;
5399                         break;
5400                 }
5401
5402                 error = hn_encap(ifp, txr, txd, &m_head);
5403                 if (error) {
5404                         /* Both txd and m_head are freed; discard */
5405                         KASSERT(txr->hn_agg_txd == NULL,
5406                             ("encap failed w/ pending aggregating txdesc"));
5407                         drbr_advance(ifp, txr->hn_mbuf_br);
5408                         continue;
5409                 }
5410
5411                 if (txr->hn_agg_pktleft == 0) {
5412                         if (txr->hn_agg_txd != NULL) {
5413                                 KASSERT(m_head == NULL,
5414                                     ("pending mbuf for aggregating txdesc"));
5415                                 error = hn_flush_txagg(ifp, txr);
5416                                 if (__predict_false(error)) {
5417                                         txr->hn_oactive = 1;
5418                                         break;
5419                                 }
5420                         } else {
5421                                 KASSERT(m_head != NULL, ("mbuf was freed"));
5422                                 error = hn_txpkt(ifp, txr, txd);
5423                                 if (__predict_false(error)) {
5424                                         /* txd is freed, but m_head is not */
5425                                         drbr_putback(ifp, txr->hn_mbuf_br,
5426                                             m_head);
5427                                         txr->hn_oactive = 1;
5428                                         break;
5429                                 }
5430                         }
5431                 }
5432 #ifdef INVARIANTS
5433                 else {
5434                         KASSERT(txr->hn_agg_txd != NULL,
5435                             ("no aggregating txdesc"));
5436                         KASSERT(m_head == NULL,
5437                             ("pending mbuf for aggregating txdesc"));
5438                 }
5439 #endif
5440
5441                 /* Sent */
5442                 drbr_advance(ifp, txr->hn_mbuf_br);
5443         }
5444
5445         /* Flush pending aggerated transmission. */
5446         if (txr->hn_agg_txd != NULL)
5447                 hn_flush_txagg(ifp, txr);
5448         return (sched);
5449 }
5450
5451 static int
5452 hn_transmit(struct ifnet *ifp, struct mbuf *m)
5453 {
5454         struct hn_softc *sc = ifp->if_softc;
5455         struct hn_tx_ring *txr;
5456         int error, idx = 0;
5457
5458         if (sc->hn_xvf_flags & HN_XVFFLAG_ENABLED) {
5459                 struct rm_priotracker pt;
5460
5461                 rm_rlock(&sc->hn_vf_lock, &pt);
5462                 if (__predict_true(sc->hn_xvf_flags & HN_XVFFLAG_ENABLED)) {
5463                         struct mbuf *m_bpf = NULL;
5464                         int obytes, omcast;
5465
5466                         obytes = m->m_pkthdr.len;
5467                         if (m->m_flags & M_MCAST)
5468                                 omcast = 1;
5469
5470                         if (sc->hn_xvf_flags & HN_XVFFLAG_ACCBPF) {
5471                                 if (bpf_peers_present(ifp->if_bpf)) {
5472                                         m_bpf = m_copypacket(m, M_NOWAIT);
5473                                         if (m_bpf == NULL) {
5474                                                 /*
5475                                                  * Failed to grab a shallow
5476                                                  * copy; tap now.
5477                                                  */
5478                                                 ETHER_BPF_MTAP(ifp, m);
5479                                         }
5480                                 }
5481                         } else {
5482                                 ETHER_BPF_MTAP(ifp, m);
5483                         }
5484
5485                         error = sc->hn_vf_ifp->if_transmit(sc->hn_vf_ifp, m);
5486                         rm_runlock(&sc->hn_vf_lock, &pt);
5487
5488                         if (m_bpf != NULL) {
5489                                 if (!error)
5490                                         ETHER_BPF_MTAP(ifp, m_bpf);
5491                                 m_freem(m_bpf);
5492                         }
5493
5494                         if (error == ENOBUFS) {
5495                                 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
5496                         } else if (error) {
5497                                 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
5498                         } else {
5499                                 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
5500                                 if_inc_counter(ifp, IFCOUNTER_OBYTES, obytes);
5501                                 if (omcast) {
5502                                         if_inc_counter(ifp, IFCOUNTER_OMCASTS,
5503                                             omcast);
5504                                 }
5505                         }
5506                         return (error);
5507                 }
5508                 rm_runlock(&sc->hn_vf_lock, &pt);
5509         }
5510
5511 #if defined(INET6) || defined(INET)
5512         /*
5513          * Perform TSO packet header fixup now, since the TSO
5514          * packet header should be cache-hot.
5515          */
5516         if (m->m_pkthdr.csum_flags & CSUM_TSO) {
5517                 m = hn_tso_fixup(m);
5518                 if (__predict_false(m == NULL)) {
5519                         if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
5520                         return EIO;
5521                 }
5522         }
5523 #endif
5524
5525         /*
5526          * Select the TX ring based on flowid
5527          */
5528         if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) {
5529 #if defined(INET6) || defined(INET)
5530                 int tcpsyn = 0;
5531
5532                 if (m->m_pkthdr.len < 128 &&
5533                     (m->m_pkthdr.csum_flags & (CSUM_IP_TCP | CSUM_IP6_TCP)) &&
5534                     (m->m_pkthdr.csum_flags & CSUM_TSO) == 0) {
5535                         m = hn_check_tcpsyn(m, &tcpsyn);
5536                         if (__predict_false(m == NULL)) {
5537                                 if_inc_counter(ifp,
5538                                     IFCOUNTER_OERRORS, 1);
5539                                 return (EIO);
5540                         }
5541                 }
5542 #else
5543                 const int tcpsyn = 0;
5544 #endif
5545                 if (tcpsyn)
5546                         idx = 0;
5547                 else
5548                         idx = m->m_pkthdr.flowid % sc->hn_tx_ring_inuse;
5549         }
5550         txr = &sc->hn_tx_ring[idx];
5551
5552         error = drbr_enqueue(ifp, txr->hn_mbuf_br, m);
5553         if (error) {
5554                 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
5555                 return error;
5556         }
5557
5558         if (txr->hn_oactive)
5559                 return 0;
5560
5561         if (txr->hn_sched_tx)
5562                 goto do_sched;
5563
5564         if (mtx_trylock(&txr->hn_tx_lock)) {
5565                 int sched;
5566
5567                 sched = hn_xmit(txr, txr->hn_direct_tx_size);
5568                 mtx_unlock(&txr->hn_tx_lock);
5569                 if (!sched)
5570                         return 0;
5571         }
5572 do_sched:
5573         taskqueue_enqueue(txr->hn_tx_taskq, &txr->hn_tx_task);
5574         return 0;
5575 }
5576
5577 static void
5578 hn_tx_ring_qflush(struct hn_tx_ring *txr)
5579 {
5580         struct mbuf *m;
5581
5582         mtx_lock(&txr->hn_tx_lock);
5583         while ((m = buf_ring_dequeue_sc(txr->hn_mbuf_br)) != NULL)
5584                 m_freem(m);
5585         mtx_unlock(&txr->hn_tx_lock);
5586 }
5587
5588 static void
5589 hn_xmit_qflush(struct ifnet *ifp)
5590 {
5591         struct hn_softc *sc = ifp->if_softc;
5592         struct rm_priotracker pt;
5593         int i;
5594
5595         for (i = 0; i < sc->hn_tx_ring_inuse; ++i)
5596                 hn_tx_ring_qflush(&sc->hn_tx_ring[i]);
5597         if_qflush(ifp);
5598
5599         rm_rlock(&sc->hn_vf_lock, &pt);
5600         if (sc->hn_xvf_flags & HN_XVFFLAG_ENABLED)
5601                 sc->hn_vf_ifp->if_qflush(sc->hn_vf_ifp);
5602         rm_runlock(&sc->hn_vf_lock, &pt);
5603 }
5604
5605 static void
5606 hn_xmit_txeof(struct hn_tx_ring *txr)
5607 {
5608
5609         if (txr->hn_sched_tx)
5610                 goto do_sched;
5611
5612         if (mtx_trylock(&txr->hn_tx_lock)) {
5613                 int sched;
5614
5615                 txr->hn_oactive = 0;
5616                 sched = hn_xmit(txr, txr->hn_direct_tx_size);
5617                 mtx_unlock(&txr->hn_tx_lock);
5618                 if (sched) {
5619                         taskqueue_enqueue(txr->hn_tx_taskq,
5620                             &txr->hn_tx_task);
5621                 }
5622         } else {
5623 do_sched:
5624                 /*
5625                  * Release the oactive earlier, with the hope, that
5626                  * others could catch up.  The task will clear the
5627                  * oactive again with the hn_tx_lock to avoid possible
5628                  * races.
5629                  */
5630                 txr->hn_oactive = 0;
5631                 taskqueue_enqueue(txr->hn_tx_taskq, &txr->hn_txeof_task);
5632         }
5633 }
5634
5635 static void
5636 hn_xmit_taskfunc(void *xtxr, int pending __unused)
5637 {
5638         struct hn_tx_ring *txr = xtxr;
5639
5640         mtx_lock(&txr->hn_tx_lock);
5641         hn_xmit(txr, 0);
5642         mtx_unlock(&txr->hn_tx_lock);
5643 }
5644
5645 static void
5646 hn_xmit_txeof_taskfunc(void *xtxr, int pending __unused)
5647 {
5648         struct hn_tx_ring *txr = xtxr;
5649
5650         mtx_lock(&txr->hn_tx_lock);
5651         txr->hn_oactive = 0;
5652         hn_xmit(txr, 0);
5653         mtx_unlock(&txr->hn_tx_lock);
5654 }
5655
5656 static int
5657 hn_chan_attach(struct hn_softc *sc, struct vmbus_channel *chan)
5658 {
5659         struct vmbus_chan_br cbr;
5660         struct hn_rx_ring *rxr;
5661         struct hn_tx_ring *txr = NULL;
5662         int idx, error;
5663
5664         idx = vmbus_chan_subidx(chan);
5665
5666         /*
5667          * Link this channel to RX/TX ring.
5668          */
5669         KASSERT(idx >= 0 && idx < sc->hn_rx_ring_inuse,
5670             ("invalid channel index %d, should > 0 && < %d",
5671              idx, sc->hn_rx_ring_inuse));
5672         rxr = &sc->hn_rx_ring[idx];
5673         KASSERT((rxr->hn_rx_flags & HN_RX_FLAG_ATTACHED) == 0,
5674             ("RX ring %d already attached", idx));
5675         rxr->hn_rx_flags |= HN_RX_FLAG_ATTACHED;
5676         rxr->hn_chan = chan;
5677
5678         if (bootverbose) {
5679                 if_printf(sc->hn_ifp, "link RX ring %d to chan%u\n",
5680                     idx, vmbus_chan_id(chan));
5681         }
5682
5683         if (idx < sc->hn_tx_ring_inuse) {
5684                 txr = &sc->hn_tx_ring[idx];
5685                 KASSERT((txr->hn_tx_flags & HN_TX_FLAG_ATTACHED) == 0,
5686                     ("TX ring %d already attached", idx));
5687                 txr->hn_tx_flags |= HN_TX_FLAG_ATTACHED;
5688
5689                 txr->hn_chan = chan;
5690                 if (bootverbose) {
5691                         if_printf(sc->hn_ifp, "link TX ring %d to chan%u\n",
5692                             idx, vmbus_chan_id(chan));
5693                 }
5694         }
5695
5696         /* Bind this channel to a proper CPU. */
5697         vmbus_chan_cpu_set(chan, HN_RING_IDX2CPU(sc, idx));
5698
5699         /*
5700          * Open this channel
5701          */
5702         cbr.cbr = rxr->hn_br;
5703         cbr.cbr_paddr = rxr->hn_br_dma.hv_paddr;
5704         cbr.cbr_txsz = HN_TXBR_SIZE;
5705         cbr.cbr_rxsz = HN_RXBR_SIZE;
5706         error = vmbus_chan_open_br(chan, &cbr, NULL, 0, hn_chan_callback, rxr);
5707         if (error) {
5708                 if (error == EISCONN) {
5709                         if_printf(sc->hn_ifp, "bufring is connected after "
5710                             "chan%u open failure\n", vmbus_chan_id(chan));
5711                         rxr->hn_rx_flags |= HN_RX_FLAG_BR_REF;
5712                 } else {
5713                         if_printf(sc->hn_ifp, "open chan%u failed: %d\n",
5714                             vmbus_chan_id(chan), error);
5715                 }
5716         }
5717         return (error);
5718 }
5719
5720 static void
5721 hn_chan_detach(struct hn_softc *sc, struct vmbus_channel *chan)
5722 {
5723         struct hn_rx_ring *rxr;
5724         int idx, error;
5725
5726         idx = vmbus_chan_subidx(chan);
5727
5728         /*
5729          * Link this channel to RX/TX ring.
5730          */
5731         KASSERT(idx >= 0 && idx < sc->hn_rx_ring_inuse,
5732             ("invalid channel index %d, should > 0 && < %d",
5733              idx, sc->hn_rx_ring_inuse));
5734         rxr = &sc->hn_rx_ring[idx];
5735         KASSERT((rxr->hn_rx_flags & HN_RX_FLAG_ATTACHED),
5736             ("RX ring %d is not attached", idx));
5737         rxr->hn_rx_flags &= ~HN_RX_FLAG_ATTACHED;
5738
5739         if (idx < sc->hn_tx_ring_inuse) {
5740                 struct hn_tx_ring *txr = &sc->hn_tx_ring[idx];
5741
5742                 KASSERT((txr->hn_tx_flags & HN_TX_FLAG_ATTACHED),
5743                     ("TX ring %d is not attached attached", idx));
5744                 txr->hn_tx_flags &= ~HN_TX_FLAG_ATTACHED;
5745         }
5746
5747         /*
5748          * Close this channel.
5749          *
5750          * NOTE:
5751          * Channel closing does _not_ destroy the target channel.
5752          */
5753         error = vmbus_chan_close_direct(chan);
5754         if (error == EISCONN) {
5755                 if_printf(sc->hn_ifp, "chan%u bufring is connected "
5756                     "after being closed\n", vmbus_chan_id(chan));
5757                 rxr->hn_rx_flags |= HN_RX_FLAG_BR_REF;
5758         } else if (error) {
5759                 if_printf(sc->hn_ifp, "chan%u close failed: %d\n",
5760                     vmbus_chan_id(chan), error);
5761         }
5762 }
5763
5764 static int
5765 hn_attach_subchans(struct hn_softc *sc)
5766 {
5767         struct vmbus_channel **subchans;
5768         int subchan_cnt = sc->hn_rx_ring_inuse - 1;
5769         int i, error = 0;
5770
5771         KASSERT(subchan_cnt > 0, ("no sub-channels"));
5772
5773         /* Attach the sub-channels. */
5774         subchans = vmbus_subchan_get(sc->hn_prichan, subchan_cnt);
5775         for (i = 0; i < subchan_cnt; ++i) {
5776                 int error1;
5777
5778                 error1 = hn_chan_attach(sc, subchans[i]);
5779                 if (error1) {
5780                         error = error1;
5781                         /* Move on; all channels will be detached later. */
5782                 }
5783         }
5784         vmbus_subchan_rel(subchans, subchan_cnt);
5785
5786         if (error) {
5787                 if_printf(sc->hn_ifp, "sub-channels attach failed: %d\n", error);
5788         } else {
5789                 if (bootverbose) {
5790                         if_printf(sc->hn_ifp, "%d sub-channels attached\n",
5791                             subchan_cnt);
5792                 }
5793         }
5794         return (error);
5795 }
5796
5797 static void
5798 hn_detach_allchans(struct hn_softc *sc)
5799 {
5800         struct vmbus_channel **subchans;
5801         int subchan_cnt = sc->hn_rx_ring_inuse - 1;
5802         int i;
5803
5804         if (subchan_cnt == 0)
5805                 goto back;
5806
5807         /* Detach the sub-channels. */
5808         subchans = vmbus_subchan_get(sc->hn_prichan, subchan_cnt);
5809         for (i = 0; i < subchan_cnt; ++i)
5810                 hn_chan_detach(sc, subchans[i]);
5811         vmbus_subchan_rel(subchans, subchan_cnt);
5812
5813 back:
5814         /*
5815          * Detach the primary channel, _after_ all sub-channels
5816          * are detached.
5817          */
5818         hn_chan_detach(sc, sc->hn_prichan);
5819
5820         /* Wait for sub-channels to be destroyed, if any. */
5821         vmbus_subchan_drain(sc->hn_prichan);
5822
5823 #ifdef INVARIANTS
5824         for (i = 0; i < sc->hn_rx_ring_cnt; ++i) {
5825                 KASSERT((sc->hn_rx_ring[i].hn_rx_flags &
5826                     HN_RX_FLAG_ATTACHED) == 0,
5827                     ("%dth RX ring is still attached", i));
5828         }
5829         for (i = 0; i < sc->hn_tx_ring_cnt; ++i) {
5830                 KASSERT((sc->hn_tx_ring[i].hn_tx_flags &
5831                     HN_TX_FLAG_ATTACHED) == 0,
5832                     ("%dth TX ring is still attached", i));
5833         }
5834 #endif
5835 }
5836
5837 static int
5838 hn_synth_alloc_subchans(struct hn_softc *sc, int *nsubch)
5839 {
5840         struct vmbus_channel **subchans;
5841         int nchan, rxr_cnt, error;
5842
5843         nchan = *nsubch + 1;
5844         if (nchan == 1) {
5845                 /*
5846                  * Multiple RX/TX rings are not requested.
5847                  */
5848                 *nsubch = 0;
5849                 return (0);
5850         }
5851
5852         /*
5853          * Query RSS capabilities, e.g. # of RX rings, and # of indirect
5854          * table entries.
5855          */
5856         error = hn_rndis_query_rsscaps(sc, &rxr_cnt);
5857         if (error) {
5858                 /* No RSS; this is benign. */
5859                 *nsubch = 0;
5860                 return (0);
5861         }
5862         if (bootverbose) {
5863                 if_printf(sc->hn_ifp, "RX rings offered %u, requested %d\n",
5864                     rxr_cnt, nchan);
5865         }
5866
5867         if (nchan > rxr_cnt)
5868                 nchan = rxr_cnt;
5869         if (nchan == 1) {
5870                 if_printf(sc->hn_ifp, "only 1 channel is supported, no vRSS\n");
5871                 *nsubch = 0;
5872                 return (0);
5873         }
5874
5875         /*
5876          * Allocate sub-channels from NVS.
5877          */
5878         *nsubch = nchan - 1;
5879         error = hn_nvs_alloc_subchans(sc, nsubch);
5880         if (error || *nsubch == 0) {
5881                 /* Failed to allocate sub-channels. */
5882                 *nsubch = 0;
5883                 return (0);
5884         }
5885
5886         /*
5887          * Wait for all sub-channels to become ready before moving on.
5888          */
5889         subchans = vmbus_subchan_get(sc->hn_prichan, *nsubch);
5890         vmbus_subchan_rel(subchans, *nsubch);
5891         return (0);
5892 }
5893
5894 static bool
5895 hn_synth_attachable(const struct hn_softc *sc)
5896 {
5897         int i;
5898
5899         if (sc->hn_flags & HN_FLAG_ERRORS)
5900                 return (false);
5901
5902         for (i = 0; i < sc->hn_rx_ring_cnt; ++i) {
5903                 const struct hn_rx_ring *rxr = &sc->hn_rx_ring[i];
5904
5905                 if (rxr->hn_rx_flags & HN_RX_FLAG_BR_REF)
5906                         return (false);
5907         }
5908         return (true);
5909 }
5910
5911 /*
5912  * Make sure that the RX filter is zero after the successful
5913  * RNDIS initialization.
5914  *
5915  * NOTE:
5916  * Under certain conditions on certain versions of Hyper-V,
5917  * the RNDIS rxfilter is _not_ zero on the hypervisor side
5918  * after the successful RNDIS initialization, which breaks
5919  * the assumption of any following code (well, it breaks the
5920  * RNDIS API contract actually).  Clear the RNDIS rxfilter
5921  * explicitly, drain packets sneaking through, and drain the
5922  * interrupt taskqueues scheduled due to the stealth packets.
5923  */
5924 static void
5925 hn_rndis_init_fixat(struct hn_softc *sc, int nchan)
5926 {
5927
5928         hn_disable_rx(sc);
5929         hn_drain_rxtx(sc, nchan);
5930 }
5931
5932 static int
5933 hn_synth_attach(struct hn_softc *sc, int mtu)
5934 {
5935 #define ATTACHED_NVS            0x0002
5936 #define ATTACHED_RNDIS          0x0004
5937
5938         struct ndis_rssprm_toeplitz *rss = &sc->hn_rss;
5939         int error, nsubch, nchan = 1, i, rndis_inited;
5940         uint32_t old_caps, attached = 0;
5941
5942         KASSERT((sc->hn_flags & HN_FLAG_SYNTH_ATTACHED) == 0,
5943             ("synthetic parts were attached"));
5944
5945         if (!hn_synth_attachable(sc))
5946                 return (ENXIO);
5947
5948         /* Save capabilities for later verification. */
5949         old_caps = sc->hn_caps;
5950         sc->hn_caps = 0;
5951
5952         /* Clear RSS stuffs. */
5953         sc->hn_rss_ind_size = 0;
5954         sc->hn_rss_hash = 0;
5955
5956         /*
5957          * Attach the primary channel _before_ attaching NVS and RNDIS.
5958          */
5959         error = hn_chan_attach(sc, sc->hn_prichan);
5960         if (error)
5961                 goto failed;
5962
5963         /*
5964          * Attach NVS.
5965          */
5966         error = hn_nvs_attach(sc, mtu);
5967         if (error)
5968                 goto failed;
5969         attached |= ATTACHED_NVS;
5970
5971         /*
5972          * Attach RNDIS _after_ NVS is attached.
5973          */
5974         error = hn_rndis_attach(sc, mtu, &rndis_inited);
5975         if (rndis_inited)
5976                 attached |= ATTACHED_RNDIS;
5977         if (error)
5978                 goto failed;
5979
5980         /*
5981          * Make sure capabilities are not changed.
5982          */
5983         if (device_is_attached(sc->hn_dev) && old_caps != sc->hn_caps) {
5984                 if_printf(sc->hn_ifp, "caps mismatch old 0x%08x, new 0x%08x\n",
5985                     old_caps, sc->hn_caps);
5986                 error = ENXIO;
5987                 goto failed;
5988         }
5989
5990         /*
5991          * Allocate sub-channels for multi-TX/RX rings.
5992          *
5993          * NOTE:
5994          * The # of RX rings that can be used is equivalent to the # of
5995          * channels to be requested.
5996          */
5997         nsubch = sc->hn_rx_ring_cnt - 1;
5998         error = hn_synth_alloc_subchans(sc, &nsubch);
5999         if (error)
6000                 goto failed;
6001         /* NOTE: _Full_ synthetic parts detach is required now. */
6002         sc->hn_flags |= HN_FLAG_SYNTH_ATTACHED;
6003
6004         /*
6005          * Set the # of TX/RX rings that could be used according to
6006          * the # of channels that NVS offered.
6007          */
6008         nchan = nsubch + 1;
6009         hn_set_ring_inuse(sc, nchan);
6010         if (nchan == 1) {
6011                 /* Only the primary channel can be used; done */
6012                 goto back;
6013         }
6014
6015         /*
6016          * Attach the sub-channels.
6017          *
6018          * NOTE: hn_set_ring_inuse() _must_ have been called.
6019          */
6020         error = hn_attach_subchans(sc);
6021         if (error)
6022                 goto failed;
6023
6024         /*
6025          * Configure RSS key and indirect table _after_ all sub-channels
6026          * are attached.
6027          */
6028         if ((sc->hn_flags & HN_FLAG_HAS_RSSKEY) == 0) {
6029                 /*
6030                  * RSS key is not set yet; set it to the default RSS key.
6031                  */
6032                 if (bootverbose)
6033                         if_printf(sc->hn_ifp, "setup default RSS key\n");
6034                 memcpy(rss->rss_key, hn_rss_key_default, sizeof(rss->rss_key));
6035                 sc->hn_flags |= HN_FLAG_HAS_RSSKEY;
6036         }
6037
6038         if ((sc->hn_flags & HN_FLAG_HAS_RSSIND) == 0) {
6039                 /*
6040                  * RSS indirect table is not set yet; set it up in round-
6041                  * robin fashion.
6042                  */
6043                 if (bootverbose) {
6044                         if_printf(sc->hn_ifp, "setup default RSS indirect "
6045                             "table\n");
6046                 }
6047                 for (i = 0; i < NDIS_HASH_INDCNT; ++i)
6048                         rss->rss_ind[i] = i % nchan;
6049                 sc->hn_flags |= HN_FLAG_HAS_RSSIND;
6050         } else {
6051                 /*
6052                  * # of usable channels may be changed, so we have to
6053                  * make sure that all entries in RSS indirect table
6054                  * are valid.
6055                  *
6056                  * NOTE: hn_set_ring_inuse() _must_ have been called.
6057                  */
6058                 hn_rss_ind_fixup(sc);
6059         }
6060
6061         error = hn_rndis_conf_rss(sc, NDIS_RSS_FLAG_NONE);
6062         if (error)
6063                 goto failed;
6064 back:
6065         /*
6066          * Fixup transmission aggregation setup.
6067          */
6068         hn_set_txagg(sc);
6069         hn_rndis_init_fixat(sc, nchan);
6070         return (0);
6071
6072 failed:
6073         if (sc->hn_flags & HN_FLAG_SYNTH_ATTACHED) {
6074                 hn_rndis_init_fixat(sc, nchan);
6075                 hn_synth_detach(sc);
6076         } else {
6077                 if (attached & ATTACHED_RNDIS) {
6078                         hn_rndis_init_fixat(sc, nchan);
6079                         hn_rndis_detach(sc);
6080                 }
6081                 if (attached & ATTACHED_NVS)
6082                         hn_nvs_detach(sc);
6083                 hn_chan_detach(sc, sc->hn_prichan);
6084                 /* Restore old capabilities. */
6085                 sc->hn_caps = old_caps;
6086         }
6087         return (error);
6088
6089 #undef ATTACHED_RNDIS
6090 #undef ATTACHED_NVS
6091 }
6092
6093 /*
6094  * NOTE:
6095  * The interface must have been suspended though hn_suspend(), before
6096  * this function get called.
6097  */
6098 static void
6099 hn_synth_detach(struct hn_softc *sc)
6100 {
6101
6102         KASSERT(sc->hn_flags & HN_FLAG_SYNTH_ATTACHED,
6103             ("synthetic parts were not attached"));
6104
6105         /* Detach the RNDIS first. */
6106         hn_rndis_detach(sc);
6107
6108         /* Detach NVS. */
6109         hn_nvs_detach(sc);
6110
6111         /* Detach all of the channels. */
6112         hn_detach_allchans(sc);
6113
6114         sc->hn_flags &= ~HN_FLAG_SYNTH_ATTACHED;
6115 }
6116
6117 static void
6118 hn_set_ring_inuse(struct hn_softc *sc, int ring_cnt)
6119 {
6120         KASSERT(ring_cnt > 0 && ring_cnt <= sc->hn_rx_ring_cnt,
6121             ("invalid ring count %d", ring_cnt));
6122
6123         if (sc->hn_tx_ring_cnt > ring_cnt)
6124                 sc->hn_tx_ring_inuse = ring_cnt;
6125         else
6126                 sc->hn_tx_ring_inuse = sc->hn_tx_ring_cnt;
6127         sc->hn_rx_ring_inuse = ring_cnt;
6128
6129         if (bootverbose) {
6130                 if_printf(sc->hn_ifp, "%d TX ring, %d RX ring\n",
6131                     sc->hn_tx_ring_inuse, sc->hn_rx_ring_inuse);
6132         }
6133 }
6134
6135 static void
6136 hn_chan_drain(struct hn_softc *sc, struct vmbus_channel *chan)
6137 {
6138
6139         /*
6140          * NOTE:
6141          * The TX bufring will not be drained by the hypervisor,
6142          * if the primary channel is revoked.
6143          */
6144         while (!vmbus_chan_rx_empty(chan) ||
6145             (!vmbus_chan_is_revoked(sc->hn_prichan) &&
6146              !vmbus_chan_tx_empty(chan)))
6147                 pause("waitch", 1);
6148         vmbus_chan_intr_drain(chan);
6149 }
6150
6151 static void
6152 hn_disable_rx(struct hn_softc *sc)
6153 {
6154
6155         /*
6156          * Disable RX by clearing RX filter forcefully.
6157          */
6158         sc->hn_rx_filter = NDIS_PACKET_TYPE_NONE;
6159         hn_rndis_set_rxfilter(sc, sc->hn_rx_filter); /* ignore error */
6160
6161         /*
6162          * Give RNDIS enough time to flush all pending data packets.
6163          */
6164         pause("waitrx", (200 * hz) / 1000);
6165 }
6166
6167 /*
6168  * NOTE:
6169  * RX/TX _must_ have been suspended/disabled, before this function
6170  * is called.
6171  */
6172 static void
6173 hn_drain_rxtx(struct hn_softc *sc, int nchan)
6174 {
6175         struct vmbus_channel **subch = NULL;
6176         int nsubch;
6177
6178         /*
6179          * Drain RX/TX bufrings and interrupts.
6180          */
6181         nsubch = nchan - 1;
6182         if (nsubch > 0)
6183                 subch = vmbus_subchan_get(sc->hn_prichan, nsubch);
6184
6185         if (subch != NULL) {
6186                 int i;
6187
6188                 for (i = 0; i < nsubch; ++i)
6189                         hn_chan_drain(sc, subch[i]);
6190         }
6191         hn_chan_drain(sc, sc->hn_prichan);
6192
6193         if (subch != NULL)
6194                 vmbus_subchan_rel(subch, nsubch);
6195 }
6196
6197 static void
6198 hn_suspend_data(struct hn_softc *sc)
6199 {
6200         struct hn_tx_ring *txr;
6201         int i;
6202
6203         HN_LOCK_ASSERT(sc);
6204
6205         /*
6206          * Suspend TX.
6207          */
6208         for (i = 0; i < sc->hn_tx_ring_inuse; ++i) {
6209                 txr = &sc->hn_tx_ring[i];
6210
6211                 mtx_lock(&txr->hn_tx_lock);
6212                 txr->hn_suspended = 1;
6213                 mtx_unlock(&txr->hn_tx_lock);
6214                 /* No one is able send more packets now. */
6215
6216                 /*
6217                  * Wait for all pending sends to finish.
6218                  *
6219                  * NOTE:
6220                  * We will _not_ receive all pending send-done, if the
6221                  * primary channel is revoked.
6222                  */
6223                 while (hn_tx_ring_pending(txr) &&
6224                     !vmbus_chan_is_revoked(sc->hn_prichan))
6225                         pause("hnwtx", 1 /* 1 tick */);
6226         }
6227
6228         /*
6229          * Disable RX.
6230          */
6231         hn_disable_rx(sc);
6232
6233         /*
6234          * Drain RX/TX.
6235          */
6236         hn_drain_rxtx(sc, sc->hn_rx_ring_inuse);
6237
6238         /*
6239          * Drain any pending TX tasks.
6240          *
6241          * NOTE:
6242          * The above hn_drain_rxtx() can dispatch TX tasks, so the TX
6243          * tasks will have to be drained _after_ the above hn_drain_rxtx().
6244          */
6245         for (i = 0; i < sc->hn_tx_ring_inuse; ++i) {
6246                 txr = &sc->hn_tx_ring[i];
6247
6248                 taskqueue_drain(txr->hn_tx_taskq, &txr->hn_tx_task);
6249                 taskqueue_drain(txr->hn_tx_taskq, &txr->hn_txeof_task);
6250         }
6251 }
6252
6253 static void
6254 hn_suspend_mgmt_taskfunc(void *xsc, int pending __unused)
6255 {
6256
6257         ((struct hn_softc *)xsc)->hn_mgmt_taskq = NULL;
6258 }
6259
6260 static void
6261 hn_suspend_mgmt(struct hn_softc *sc)
6262 {
6263         struct task task;
6264
6265         HN_LOCK_ASSERT(sc);
6266
6267         /*
6268          * Make sure that hn_mgmt_taskq0 can nolonger be accessed
6269          * through hn_mgmt_taskq.
6270          */
6271         TASK_INIT(&task, 0, hn_suspend_mgmt_taskfunc, sc);
6272         vmbus_chan_run_task(sc->hn_prichan, &task);
6273
6274         /*
6275          * Make sure that all pending management tasks are completed.
6276          */
6277         taskqueue_drain(sc->hn_mgmt_taskq0, &sc->hn_netchg_init);
6278         taskqueue_drain_timeout(sc->hn_mgmt_taskq0, &sc->hn_netchg_status);
6279         taskqueue_drain_all(sc->hn_mgmt_taskq0);
6280 }
6281
6282 static void
6283 hn_suspend(struct hn_softc *sc)
6284 {
6285
6286         /* Disable polling. */
6287         hn_polling(sc, 0);
6288
6289         /*
6290          * If the non-transparent mode VF is activated, the synthetic
6291          * device is receiving packets, so the data path of the
6292          * synthetic device must be suspended.
6293          */
6294         if ((sc->hn_ifp->if_drv_flags & IFF_DRV_RUNNING) ||
6295             (sc->hn_flags & HN_FLAG_RXVF))
6296                 hn_suspend_data(sc);
6297         hn_suspend_mgmt(sc);
6298 }
6299
6300 static void
6301 hn_resume_tx(struct hn_softc *sc, int tx_ring_cnt)
6302 {
6303         int i;
6304
6305         KASSERT(tx_ring_cnt <= sc->hn_tx_ring_cnt,
6306             ("invalid TX ring count %d", tx_ring_cnt));
6307
6308         for (i = 0; i < tx_ring_cnt; ++i) {
6309                 struct hn_tx_ring *txr = &sc->hn_tx_ring[i];
6310
6311                 mtx_lock(&txr->hn_tx_lock);
6312                 txr->hn_suspended = 0;
6313                 mtx_unlock(&txr->hn_tx_lock);
6314         }
6315 }
6316
6317 static void
6318 hn_resume_data(struct hn_softc *sc)
6319 {
6320         int i;
6321
6322         HN_LOCK_ASSERT(sc);
6323
6324         /*
6325          * Re-enable RX.
6326          */
6327         hn_rxfilter_config(sc);
6328
6329         /*
6330          * Make sure to clear suspend status on "all" TX rings,
6331          * since hn_tx_ring_inuse can be changed after
6332          * hn_suspend_data().
6333          */
6334         hn_resume_tx(sc, sc->hn_tx_ring_cnt);
6335
6336 #ifdef HN_IFSTART_SUPPORT
6337         if (!hn_use_if_start)
6338 #endif
6339         {
6340                 /*
6341                  * Flush unused drbrs, since hn_tx_ring_inuse may be
6342                  * reduced.
6343                  */
6344                 for (i = sc->hn_tx_ring_inuse; i < sc->hn_tx_ring_cnt; ++i)
6345                         hn_tx_ring_qflush(&sc->hn_tx_ring[i]);
6346         }
6347
6348         /*
6349          * Kick start TX.
6350          */
6351         for (i = 0; i < sc->hn_tx_ring_inuse; ++i) {
6352                 struct hn_tx_ring *txr = &sc->hn_tx_ring[i];
6353
6354                 /*
6355                  * Use txeof task, so that any pending oactive can be
6356                  * cleared properly.
6357                  */
6358                 taskqueue_enqueue(txr->hn_tx_taskq, &txr->hn_txeof_task);
6359         }
6360 }
6361
6362 static void
6363 hn_resume_mgmt(struct hn_softc *sc)
6364 {
6365
6366         sc->hn_mgmt_taskq = sc->hn_mgmt_taskq0;
6367
6368         /*
6369          * Kick off network change detection, if it was pending.
6370          * If no network change was pending, start link status
6371          * checks, which is more lightweight than network change
6372          * detection.
6373          */
6374         if (sc->hn_link_flags & HN_LINK_FLAG_NETCHG)
6375                 hn_change_network(sc);
6376         else
6377                 hn_update_link_status(sc);
6378 }
6379
6380 static void
6381 hn_resume(struct hn_softc *sc)
6382 {
6383
6384         /*
6385          * If the non-transparent mode VF is activated, the synthetic
6386          * device have to receive packets, so the data path of the
6387          * synthetic device must be resumed.
6388          */
6389         if ((sc->hn_ifp->if_drv_flags & IFF_DRV_RUNNING) ||
6390             (sc->hn_flags & HN_FLAG_RXVF))
6391                 hn_resume_data(sc);
6392
6393         /*
6394          * Don't resume link status change if VF is attached/activated.
6395          * - In the non-transparent VF mode, the synthetic device marks
6396          *   link down until the VF is deactivated; i.e. VF is down.
6397          * - In transparent VF mode, VF's media status is used until
6398          *   the VF is detached.
6399          */
6400         if ((sc->hn_flags & HN_FLAG_RXVF) == 0 &&
6401             !(hn_xpnt_vf && sc->hn_vf_ifp != NULL))
6402                 hn_resume_mgmt(sc);
6403
6404         /*
6405          * Re-enable polling if this interface is running and
6406          * the polling is requested.
6407          */
6408         if ((sc->hn_ifp->if_drv_flags & IFF_DRV_RUNNING) && sc->hn_pollhz > 0)
6409                 hn_polling(sc, sc->hn_pollhz);
6410 }
6411
6412 static void 
6413 hn_rndis_rx_status(struct hn_softc *sc, const void *data, int dlen)
6414 {
6415         const struct rndis_status_msg *msg;
6416         int ofs;
6417
6418         if (dlen < sizeof(*msg)) {
6419                 if_printf(sc->hn_ifp, "invalid RNDIS status\n");
6420                 return;
6421         }
6422         msg = data;
6423
6424         switch (msg->rm_status) {
6425         case RNDIS_STATUS_MEDIA_CONNECT:
6426         case RNDIS_STATUS_MEDIA_DISCONNECT:
6427                 hn_update_link_status(sc);
6428                 break;
6429
6430         case RNDIS_STATUS_TASK_OFFLOAD_CURRENT_CONFIG:
6431         case RNDIS_STATUS_LINK_SPEED_CHANGE:
6432                 /* Not really useful; ignore. */
6433                 break;
6434
6435         case RNDIS_STATUS_NETWORK_CHANGE:
6436                 ofs = RNDIS_STBUFOFFSET_ABS(msg->rm_stbufoffset);
6437                 if (dlen < ofs + msg->rm_stbuflen ||
6438                     msg->rm_stbuflen < sizeof(uint32_t)) {
6439                         if_printf(sc->hn_ifp, "network changed\n");
6440                 } else {
6441                         uint32_t change;
6442
6443                         memcpy(&change, ((const uint8_t *)msg) + ofs,
6444                             sizeof(change));
6445                         if_printf(sc->hn_ifp, "network changed, change %u\n",
6446                             change);
6447                 }
6448                 hn_change_network(sc);
6449                 break;
6450
6451         default:
6452                 if_printf(sc->hn_ifp, "unknown RNDIS status 0x%08x\n",
6453                     msg->rm_status);
6454                 break;
6455         }
6456 }
6457
6458 static int
6459 hn_rndis_rxinfo(const void *info_data, int info_dlen, struct hn_rxinfo *info)
6460 {
6461         const struct rndis_pktinfo *pi = info_data;
6462         uint32_t mask = 0;
6463
6464         while (info_dlen != 0) {
6465                 const void *data;
6466                 uint32_t dlen;
6467
6468                 if (__predict_false(info_dlen < sizeof(*pi)))
6469                         return (EINVAL);
6470                 if (__predict_false(info_dlen < pi->rm_size))
6471                         return (EINVAL);
6472                 info_dlen -= pi->rm_size;
6473
6474                 if (__predict_false(pi->rm_size & RNDIS_PKTINFO_SIZE_ALIGNMASK))
6475                         return (EINVAL);
6476                 if (__predict_false(pi->rm_size < pi->rm_pktinfooffset))
6477                         return (EINVAL);
6478                 dlen = pi->rm_size - pi->rm_pktinfooffset;
6479                 data = pi->rm_data;
6480
6481                 switch (pi->rm_type) {
6482                 case NDIS_PKTINFO_TYPE_VLAN:
6483                         if (__predict_false(dlen < NDIS_VLAN_INFO_SIZE))
6484                                 return (EINVAL);
6485                         info->vlan_info = *((const uint32_t *)data);
6486                         mask |= HN_RXINFO_VLAN;
6487                         break;
6488
6489                 case NDIS_PKTINFO_TYPE_CSUM:
6490                         if (__predict_false(dlen < NDIS_RXCSUM_INFO_SIZE))
6491                                 return (EINVAL);
6492                         info->csum_info = *((const uint32_t *)data);
6493                         mask |= HN_RXINFO_CSUM;
6494                         break;
6495
6496                 case HN_NDIS_PKTINFO_TYPE_HASHVAL:
6497                         if (__predict_false(dlen < HN_NDIS_HASH_VALUE_SIZE))
6498                                 return (EINVAL);
6499                         info->hash_value = *((const uint32_t *)data);
6500                         mask |= HN_RXINFO_HASHVAL;
6501                         break;
6502
6503                 case HN_NDIS_PKTINFO_TYPE_HASHINF:
6504                         if (__predict_false(dlen < HN_NDIS_HASH_INFO_SIZE))
6505                                 return (EINVAL);
6506                         info->hash_info = *((const uint32_t *)data);
6507                         mask |= HN_RXINFO_HASHINF;
6508                         break;
6509
6510                 default:
6511                         goto next;
6512                 }
6513
6514                 if (mask == HN_RXINFO_ALL) {
6515                         /* All found; done */
6516                         break;
6517                 }
6518 next:
6519                 pi = (const struct rndis_pktinfo *)
6520                     ((const uint8_t *)pi + pi->rm_size);
6521         }
6522
6523         /*
6524          * Final fixup.
6525          * - If there is no hash value, invalidate the hash info.
6526          */
6527         if ((mask & HN_RXINFO_HASHVAL) == 0)
6528                 info->hash_info = HN_NDIS_HASH_INFO_INVALID;
6529         return (0);
6530 }
6531
6532 static __inline bool
6533 hn_rndis_check_overlap(int off, int len, int check_off, int check_len)
6534 {
6535
6536         if (off < check_off) {
6537                 if (__predict_true(off + len <= check_off))
6538                         return (false);
6539         } else if (off > check_off) {
6540                 if (__predict_true(check_off + check_len <= off))
6541                         return (false);
6542         }
6543         return (true);
6544 }
6545
6546 static void
6547 hn_rndis_rx_data(struct hn_rx_ring *rxr, const void *data, int dlen)
6548 {
6549         const struct rndis_packet_msg *pkt;
6550         struct hn_rxinfo info;
6551         int data_off, pktinfo_off, data_len, pktinfo_len;
6552
6553         /*
6554          * Check length.
6555          */
6556         if (__predict_false(dlen < sizeof(*pkt))) {
6557                 if_printf(rxr->hn_ifp, "invalid RNDIS packet msg\n");
6558                 return;
6559         }
6560         pkt = data;
6561
6562         if (__predict_false(dlen < pkt->rm_len)) {
6563                 if_printf(rxr->hn_ifp, "truncated RNDIS packet msg, "
6564                     "dlen %d, msglen %u\n", dlen, pkt->rm_len);
6565                 return;
6566         }
6567         if (__predict_false(pkt->rm_len <
6568             pkt->rm_datalen + pkt->rm_oobdatalen + pkt->rm_pktinfolen)) {
6569                 if_printf(rxr->hn_ifp, "invalid RNDIS packet msglen, "
6570                     "msglen %u, data %u, oob %u, pktinfo %u\n",
6571                     pkt->rm_len, pkt->rm_datalen, pkt->rm_oobdatalen,
6572                     pkt->rm_pktinfolen);
6573                 return;
6574         }
6575         if (__predict_false(pkt->rm_datalen == 0)) {
6576                 if_printf(rxr->hn_ifp, "invalid RNDIS packet msg, no data\n");
6577                 return;
6578         }
6579
6580         /*
6581          * Check offests.
6582          */
6583 #define IS_OFFSET_INVALID(ofs)                  \
6584         ((ofs) < RNDIS_PACKET_MSG_OFFSET_MIN || \
6585          ((ofs) & RNDIS_PACKET_MSG_OFFSET_ALIGNMASK))
6586
6587         /* XXX Hyper-V does not meet data offset alignment requirement */
6588         if (__predict_false(pkt->rm_dataoffset < RNDIS_PACKET_MSG_OFFSET_MIN)) {
6589                 if_printf(rxr->hn_ifp, "invalid RNDIS packet msg, "
6590                     "data offset %u\n", pkt->rm_dataoffset);
6591                 return;
6592         }
6593         if (__predict_false(pkt->rm_oobdataoffset > 0 &&
6594             IS_OFFSET_INVALID(pkt->rm_oobdataoffset))) {
6595                 if_printf(rxr->hn_ifp, "invalid RNDIS packet msg, "
6596                     "oob offset %u\n", pkt->rm_oobdataoffset);
6597                 return;
6598         }
6599         if (__predict_true(pkt->rm_pktinfooffset > 0) &&
6600             __predict_false(IS_OFFSET_INVALID(pkt->rm_pktinfooffset))) {
6601                 if_printf(rxr->hn_ifp, "invalid RNDIS packet msg, "
6602                     "pktinfo offset %u\n", pkt->rm_pktinfooffset);
6603                 return;
6604         }
6605
6606 #undef IS_OFFSET_INVALID
6607
6608         data_off = RNDIS_PACKET_MSG_OFFSET_ABS(pkt->rm_dataoffset);
6609         data_len = pkt->rm_datalen;
6610         pktinfo_off = RNDIS_PACKET_MSG_OFFSET_ABS(pkt->rm_pktinfooffset);
6611         pktinfo_len = pkt->rm_pktinfolen;
6612
6613         /*
6614          * Check OOB coverage.
6615          */
6616         if (__predict_false(pkt->rm_oobdatalen != 0)) {
6617                 int oob_off, oob_len;
6618
6619                 if_printf(rxr->hn_ifp, "got oobdata\n");
6620                 oob_off = RNDIS_PACKET_MSG_OFFSET_ABS(pkt->rm_oobdataoffset);
6621                 oob_len = pkt->rm_oobdatalen;
6622
6623                 if (__predict_false(oob_off + oob_len > pkt->rm_len)) {
6624                         if_printf(rxr->hn_ifp, "invalid RNDIS packet msg, "
6625                             "oob overflow, msglen %u, oob abs %d len %d\n",
6626                             pkt->rm_len, oob_off, oob_len);
6627                         return;
6628                 }
6629
6630                 /*
6631                  * Check against data.
6632                  */
6633                 if (hn_rndis_check_overlap(oob_off, oob_len,
6634                     data_off, data_len)) {
6635                         if_printf(rxr->hn_ifp, "invalid RNDIS packet msg, "
6636                             "oob overlaps data, oob abs %d len %d, "
6637                             "data abs %d len %d\n",
6638                             oob_off, oob_len, data_off, data_len);
6639                         return;
6640                 }
6641
6642                 /*
6643                  * Check against pktinfo.
6644                  */
6645                 if (pktinfo_len != 0 &&
6646                     hn_rndis_check_overlap(oob_off, oob_len,
6647                     pktinfo_off, pktinfo_len)) {
6648                         if_printf(rxr->hn_ifp, "invalid RNDIS packet msg, "
6649                             "oob overlaps pktinfo, oob abs %d len %d, "
6650                             "pktinfo abs %d len %d\n",
6651                             oob_off, oob_len, pktinfo_off, pktinfo_len);
6652                         return;
6653                 }
6654         }
6655
6656         /*
6657          * Check per-packet-info coverage and find useful per-packet-info.
6658          */
6659         info.vlan_info = HN_NDIS_VLAN_INFO_INVALID;
6660         info.csum_info = HN_NDIS_RXCSUM_INFO_INVALID;
6661         info.hash_info = HN_NDIS_HASH_INFO_INVALID;
6662         if (__predict_true(pktinfo_len != 0)) {
6663                 bool overlap;
6664                 int error;
6665
6666                 if (__predict_false(pktinfo_off + pktinfo_len > pkt->rm_len)) {
6667                         if_printf(rxr->hn_ifp, "invalid RNDIS packet msg, "
6668                             "pktinfo overflow, msglen %u, "
6669                             "pktinfo abs %d len %d\n",
6670                             pkt->rm_len, pktinfo_off, pktinfo_len);
6671                         return;
6672                 }
6673
6674                 /*
6675                  * Check packet info coverage.
6676                  */
6677                 overlap = hn_rndis_check_overlap(pktinfo_off, pktinfo_len,
6678                     data_off, data_len);
6679                 if (__predict_false(overlap)) {
6680                         if_printf(rxr->hn_ifp, "invalid RNDIS packet msg, "
6681                             "pktinfo overlap data, pktinfo abs %d len %d, "
6682                             "data abs %d len %d\n",
6683                             pktinfo_off, pktinfo_len, data_off, data_len);
6684                         return;
6685                 }
6686
6687                 /*
6688                  * Find useful per-packet-info.
6689                  */
6690                 error = hn_rndis_rxinfo(((const uint8_t *)pkt) + pktinfo_off,
6691                     pktinfo_len, &info);
6692                 if (__predict_false(error)) {
6693                         if_printf(rxr->hn_ifp, "invalid RNDIS packet msg "
6694                             "pktinfo\n");
6695                         return;
6696                 }
6697         }
6698
6699         if (__predict_false(data_off + data_len > pkt->rm_len)) {
6700                 if_printf(rxr->hn_ifp, "invalid RNDIS packet msg, "
6701                     "data overflow, msglen %u, data abs %d len %d\n",
6702                     pkt->rm_len, data_off, data_len);
6703                 return;
6704         }
6705         hn_rxpkt(rxr, ((const uint8_t *)pkt) + data_off, data_len, &info);
6706 }
6707
6708 static __inline void
6709 hn_rndis_rxpkt(struct hn_rx_ring *rxr, const void *data, int dlen)
6710 {
6711         const struct rndis_msghdr *hdr;
6712
6713         if (__predict_false(dlen < sizeof(*hdr))) {
6714                 if_printf(rxr->hn_ifp, "invalid RNDIS msg\n");
6715                 return;
6716         }
6717         hdr = data;
6718
6719         if (__predict_true(hdr->rm_type == REMOTE_NDIS_PACKET_MSG)) {
6720                 /* Hot data path. */
6721                 hn_rndis_rx_data(rxr, data, dlen);
6722                 /* Done! */
6723                 return;
6724         }
6725
6726         if (hdr->rm_type == REMOTE_NDIS_INDICATE_STATUS_MSG)
6727                 hn_rndis_rx_status(rxr->hn_ifp->if_softc, data, dlen);
6728         else
6729                 hn_rndis_rx_ctrl(rxr->hn_ifp->if_softc, data, dlen);
6730 }
6731
6732 static void
6733 hn_nvs_handle_notify(struct hn_softc *sc, const struct vmbus_chanpkt_hdr *pkt)
6734 {
6735         const struct hn_nvs_hdr *hdr;
6736
6737         if (VMBUS_CHANPKT_DATALEN(pkt) < sizeof(*hdr)) {
6738                 if_printf(sc->hn_ifp, "invalid nvs notify\n");
6739                 return;
6740         }
6741         hdr = VMBUS_CHANPKT_CONST_DATA(pkt);
6742
6743         if (hdr->nvs_type == HN_NVS_TYPE_TXTBL_NOTE) {
6744                 /* Useless; ignore */
6745                 return;
6746         }
6747         if_printf(sc->hn_ifp, "got notify, nvs type %u\n", hdr->nvs_type);
6748 }
6749
6750 static void
6751 hn_nvs_handle_comp(struct hn_softc *sc, struct vmbus_channel *chan,
6752     const struct vmbus_chanpkt_hdr *pkt)
6753 {
6754         struct hn_nvs_sendctx *sndc;
6755
6756         sndc = (struct hn_nvs_sendctx *)(uintptr_t)pkt->cph_xactid;
6757         sndc->hn_cb(sndc, sc, chan, VMBUS_CHANPKT_CONST_DATA(pkt),
6758             VMBUS_CHANPKT_DATALEN(pkt));
6759         /*
6760          * NOTE:
6761          * 'sndc' CAN NOT be accessed anymore, since it can be freed by
6762          * its callback.
6763          */
6764 }
6765
6766 static void
6767 hn_nvs_handle_rxbuf(struct hn_rx_ring *rxr, struct vmbus_channel *chan,
6768     const struct vmbus_chanpkt_hdr *pkthdr)
6769 {
6770         const struct vmbus_chanpkt_rxbuf *pkt;
6771         const struct hn_nvs_hdr *nvs_hdr;
6772         int count, i, hlen;
6773
6774         if (__predict_false(VMBUS_CHANPKT_DATALEN(pkthdr) < sizeof(*nvs_hdr))) {
6775                 if_printf(rxr->hn_ifp, "invalid nvs RNDIS\n");
6776                 return;
6777         }
6778         nvs_hdr = VMBUS_CHANPKT_CONST_DATA(pkthdr);
6779
6780         /* Make sure that this is a RNDIS message. */
6781         if (__predict_false(nvs_hdr->nvs_type != HN_NVS_TYPE_RNDIS)) {
6782                 if_printf(rxr->hn_ifp, "nvs type %u, not RNDIS\n",
6783                     nvs_hdr->nvs_type);
6784                 return;
6785         }
6786
6787         hlen = VMBUS_CHANPKT_GETLEN(pkthdr->cph_hlen);
6788         if (__predict_false(hlen < sizeof(*pkt))) {
6789                 if_printf(rxr->hn_ifp, "invalid rxbuf chanpkt\n");
6790                 return;
6791         }
6792         pkt = (const struct vmbus_chanpkt_rxbuf *)pkthdr;
6793
6794         if (__predict_false(pkt->cp_rxbuf_id != HN_NVS_RXBUF_SIG)) {
6795                 if_printf(rxr->hn_ifp, "invalid rxbuf_id 0x%08x\n",
6796                     pkt->cp_rxbuf_id);
6797                 return;
6798         }
6799
6800         count = pkt->cp_rxbuf_cnt;
6801         if (__predict_false(hlen <
6802             __offsetof(struct vmbus_chanpkt_rxbuf, cp_rxbuf[count]))) {
6803                 if_printf(rxr->hn_ifp, "invalid rxbuf_cnt %d\n", count);
6804                 return;
6805         }
6806
6807         /* Each range represents 1 RNDIS pkt that contains 1 Ethernet frame */
6808         for (i = 0; i < count; ++i) {
6809                 int ofs, len;
6810
6811                 ofs = pkt->cp_rxbuf[i].rb_ofs;
6812                 len = pkt->cp_rxbuf[i].rb_len;
6813                 if (__predict_false(ofs + len > HN_RXBUF_SIZE)) {
6814                         if_printf(rxr->hn_ifp, "%dth RNDIS msg overflow rxbuf, "
6815                             "ofs %d, len %d\n", i, ofs, len);
6816                         continue;
6817                 }
6818                 hn_rndis_rxpkt(rxr, rxr->hn_rxbuf + ofs, len);
6819         }
6820
6821         /*
6822          * Ack the consumed RXBUF associated w/ this channel packet,
6823          * so that this RXBUF can be recycled by the hypervisor.
6824          */
6825         hn_nvs_ack_rxbuf(rxr, chan, pkt->cp_hdr.cph_xactid);
6826 }
6827
6828 static void
6829 hn_nvs_ack_rxbuf(struct hn_rx_ring *rxr, struct vmbus_channel *chan,
6830     uint64_t tid)
6831 {
6832         struct hn_nvs_rndis_ack ack;
6833         int retries, error;
6834         
6835         ack.nvs_type = HN_NVS_TYPE_RNDIS_ACK;
6836         ack.nvs_status = HN_NVS_STATUS_OK;
6837
6838         retries = 0;
6839 again:
6840         error = vmbus_chan_send(chan, VMBUS_CHANPKT_TYPE_COMP,
6841             VMBUS_CHANPKT_FLAG_NONE, &ack, sizeof(ack), tid);
6842         if (__predict_false(error == EAGAIN)) {
6843                 /*
6844                  * NOTE:
6845                  * This should _not_ happen in real world, since the
6846                  * consumption of the TX bufring from the TX path is
6847                  * controlled.
6848                  */
6849                 if (rxr->hn_ack_failed == 0)
6850                         if_printf(rxr->hn_ifp, "RXBUF ack retry\n");
6851                 rxr->hn_ack_failed++;
6852                 retries++;
6853                 if (retries < 10) {
6854                         DELAY(100);
6855                         goto again;
6856                 }
6857                 /* RXBUF leaks! */
6858                 if_printf(rxr->hn_ifp, "RXBUF ack failed\n");
6859         }
6860 }
6861
6862 static void
6863 hn_chan_callback(struct vmbus_channel *chan, void *xrxr)
6864 {
6865         struct hn_rx_ring *rxr = xrxr;
6866         struct hn_softc *sc = rxr->hn_ifp->if_softc;
6867
6868         for (;;) {
6869                 struct vmbus_chanpkt_hdr *pkt = rxr->hn_pktbuf;
6870                 int error, pktlen;
6871
6872                 pktlen = rxr->hn_pktbuf_len;
6873                 error = vmbus_chan_recv_pkt(chan, pkt, &pktlen);
6874                 if (__predict_false(error == ENOBUFS)) {
6875                         void *nbuf;
6876                         int nlen;
6877
6878                         /*
6879                          * Expand channel packet buffer.
6880                          *
6881                          * XXX
6882                          * Use M_WAITOK here, since allocation failure
6883                          * is fatal.
6884                          */
6885                         nlen = rxr->hn_pktbuf_len * 2;
6886                         while (nlen < pktlen)
6887                                 nlen *= 2;
6888                         nbuf = malloc(nlen, M_DEVBUF, M_WAITOK);
6889
6890                         if_printf(rxr->hn_ifp, "expand pktbuf %d -> %d\n",
6891                             rxr->hn_pktbuf_len, nlen);
6892
6893                         free(rxr->hn_pktbuf, M_DEVBUF);
6894                         rxr->hn_pktbuf = nbuf;
6895                         rxr->hn_pktbuf_len = nlen;
6896                         /* Retry! */
6897                         continue;
6898                 } else if (__predict_false(error == EAGAIN)) {
6899                         /* No more channel packets; done! */
6900                         break;
6901                 }
6902                 KASSERT(!error, ("vmbus_chan_recv_pkt failed: %d", error));
6903
6904                 switch (pkt->cph_type) {
6905                 case VMBUS_CHANPKT_TYPE_COMP:
6906                         hn_nvs_handle_comp(sc, chan, pkt);
6907                         break;
6908
6909                 case VMBUS_CHANPKT_TYPE_RXBUF:
6910                         hn_nvs_handle_rxbuf(rxr, chan, pkt);
6911                         break;
6912
6913                 case VMBUS_CHANPKT_TYPE_INBAND:
6914                         hn_nvs_handle_notify(sc, pkt);
6915                         break;
6916
6917                 default:
6918                         if_printf(rxr->hn_ifp, "unknown chan pkt %u\n",
6919                             pkt->cph_type);
6920                         break;
6921                 }
6922         }
6923         hn_chan_rollup(rxr, rxr->hn_txr);
6924 }
6925
6926 static void
6927 hn_sysinit(void *arg __unused)
6928 {
6929         int i;
6930
6931 #ifdef HN_IFSTART_SUPPORT
6932         /*
6933          * Don't use ifnet.if_start if transparent VF mode is requested;
6934          * mainly due to the IFF_DRV_OACTIVE flag.
6935          */
6936         if (hn_xpnt_vf && hn_use_if_start) {
6937                 hn_use_if_start = 0;
6938                 printf("hn: tranparent VF mode, if_transmit will be used, "
6939                     "instead of if_start\n");
6940         }
6941 #endif
6942         if (hn_xpnt_vf_attwait < HN_XPNT_VF_ATTWAIT_MIN) {
6943                 printf("hn: invalid transparent VF attach routing "
6944                     "wait timeout %d, reset to %d\n",
6945                     hn_xpnt_vf_attwait, HN_XPNT_VF_ATTWAIT_MIN);
6946                 hn_xpnt_vf_attwait = HN_XPNT_VF_ATTWAIT_MIN;
6947         }
6948
6949         /*
6950          * Initialize VF map.
6951          */
6952         rm_init_flags(&hn_vfmap_lock, "hn_vfmap", RM_SLEEPABLE);
6953         hn_vfmap_size = HN_VFMAP_SIZE_DEF;
6954         hn_vfmap = malloc(sizeof(struct ifnet *) * hn_vfmap_size, M_DEVBUF,
6955             M_WAITOK | M_ZERO);
6956
6957         /*
6958          * Fix the # of TX taskqueues.
6959          */
6960         if (hn_tx_taskq_cnt <= 0)
6961                 hn_tx_taskq_cnt = 1;
6962         else if (hn_tx_taskq_cnt > mp_ncpus)
6963                 hn_tx_taskq_cnt = mp_ncpus;
6964
6965         /*
6966          * Fix the TX taskqueue mode.
6967          */
6968         switch (hn_tx_taskq_mode) {
6969         case HN_TX_TASKQ_M_INDEP:
6970         case HN_TX_TASKQ_M_GLOBAL:
6971         case HN_TX_TASKQ_M_EVTTQ:
6972                 break;
6973         default:
6974                 hn_tx_taskq_mode = HN_TX_TASKQ_M_INDEP;
6975                 break;
6976         }
6977
6978         if (vm_guest != VM_GUEST_HV)
6979                 return;
6980
6981         if (hn_tx_taskq_mode != HN_TX_TASKQ_M_GLOBAL)
6982                 return;
6983
6984         hn_tx_taskque = malloc(hn_tx_taskq_cnt * sizeof(struct taskqueue *),
6985             M_DEVBUF, M_WAITOK);
6986         for (i = 0; i < hn_tx_taskq_cnt; ++i) {
6987                 hn_tx_taskque[i] = taskqueue_create("hn_tx", M_WAITOK,
6988                     taskqueue_thread_enqueue, &hn_tx_taskque[i]);
6989                 taskqueue_start_threads(&hn_tx_taskque[i], 1, PI_NET,
6990                     "hn tx%d", i);
6991         }
6992 }
6993 SYSINIT(hn_sysinit, SI_SUB_DRIVERS, SI_ORDER_SECOND, hn_sysinit, NULL);
6994
6995 static void
6996 hn_sysuninit(void *arg __unused)
6997 {
6998
6999         if (hn_tx_taskque != NULL) {
7000                 int i;
7001
7002                 for (i = 0; i < hn_tx_taskq_cnt; ++i)
7003                         taskqueue_free(hn_tx_taskque[i]);
7004                 free(hn_tx_taskque, M_DEVBUF);
7005         }
7006
7007         if (hn_vfmap != NULL)
7008                 free(hn_vfmap, M_DEVBUF);
7009         rm_destroy(&hn_vfmap_lock);
7010 }
7011 SYSUNINIT(hn_sysuninit, SI_SUB_DRIVERS, SI_ORDER_SECOND, hn_sysuninit, NULL);