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