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