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