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