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