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