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