]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/hyperv/netvsc/hv_netvsc_drv_freebsd.c
MFV r298691:
[FreeBSD/FreeBSD.git] / sys / dev / hyperv / netvsc / hv_netvsc_drv_freebsd.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/systm.h>
63 #include <sys/sockio.h>
64 #include <sys/mbuf.h>
65 #include <sys/malloc.h>
66 #include <sys/module.h>
67 #include <sys/kernel.h>
68 #include <sys/socket.h>
69 #include <sys/queue.h>
70 #include <sys/lock.h>
71 #include <sys/sx.h>
72 #include <sys/sysctl.h>
73 #include <sys/buf_ring.h>
74
75 #include <net/if.h>
76 #include <net/if_arp.h>
77 #include <net/ethernet.h>
78 #include <net/if_dl.h>
79 #include <net/if_media.h>
80
81 #include <net/bpf.h>
82
83 #include <net/if_var.h>
84 #include <net/if_types.h>
85 #include <net/if_vlan_var.h>
86
87 #include <netinet/in_systm.h>
88 #include <netinet/in.h>
89 #include <netinet/ip.h>
90 #include <netinet/if_ether.h>
91 #include <netinet/tcp.h>
92 #include <netinet/udp.h>
93 #include <netinet/ip6.h>
94
95 #include <vm/vm.h>
96 #include <vm/vm_param.h>
97 #include <vm/vm_kern.h>
98 #include <vm/pmap.h>
99
100 #include <machine/bus.h>
101 #include <machine/resource.h>
102 #include <machine/frame.h>
103
104 #include <sys/bus.h>
105 #include <sys/rman.h>
106 #include <sys/mutex.h>
107 #include <sys/errno.h>
108 #include <sys/types.h>
109 #include <machine/atomic.h>
110
111 #include <machine/intr_machdep.h>
112
113 #include <machine/in_cksum.h>
114
115 #include <dev/hyperv/include/hyperv.h>
116 #include "hv_net_vsc.h"
117 #include "hv_rndis.h"
118 #include "hv_rndis_filter.h"
119
120 #define hv_chan_rxr     hv_chan_priv1
121 #define hv_chan_txr     hv_chan_priv2
122
123 /* Short for Hyper-V network interface */
124 #define NETVSC_DEVNAME    "hn"
125
126 /*
127  * It looks like offset 0 of buf is reserved to hold the softc pointer.
128  * The sc pointer evidently not needed, and is not presently populated.
129  * The packet offset is where the netvsc_packet starts in the buffer.
130  */
131 #define HV_NV_SC_PTR_OFFSET_IN_BUF         0
132 #define HV_NV_PACKET_OFFSET_IN_BUF         16
133
134 /* YYY should get it from the underlying channel */
135 #define HN_TX_DESC_CNT                  512
136
137 #define HN_LROENT_CNT_DEF               128
138
139 #define HN_RING_CNT_DEF_MAX             8
140
141 #define HN_RNDIS_MSG_LEN                \
142     (sizeof(rndis_msg) +                \
143      RNDIS_HASH_PPI_SIZE +              \
144      RNDIS_VLAN_PPI_SIZE +              \
145      RNDIS_TSO_PPI_SIZE +               \
146      RNDIS_CSUM_PPI_SIZE)
147 #define HN_RNDIS_MSG_BOUNDARY           PAGE_SIZE
148 #define HN_RNDIS_MSG_ALIGN              CACHE_LINE_SIZE
149
150 #define HN_TX_DATA_BOUNDARY             PAGE_SIZE
151 #define HN_TX_DATA_MAXSIZE              IP_MAXPACKET
152 #define HN_TX_DATA_SEGSIZE              PAGE_SIZE
153 #define HN_TX_DATA_SEGCNT_MAX           \
154     (NETVSC_PACKET_MAXPAGE - HV_RF_NUM_TX_RESERVED_PAGE_BUFS)
155
156 #define HN_DIRECT_TX_SIZE_DEF           128
157
158 #define HN_EARLY_TXEOF_THRESH           8
159
160 struct hn_txdesc {
161 #ifndef HN_USE_TXDESC_BUFRING
162         SLIST_ENTRY(hn_txdesc) link;
163 #endif
164         struct mbuf     *m;
165         struct hn_tx_ring *txr;
166         int             refs;
167         uint32_t        flags;          /* HN_TXD_FLAG_ */
168         netvsc_packet   netvsc_pkt;     /* XXX to be removed */
169
170         bus_dmamap_t    data_dmap;
171
172         bus_addr_t      rndis_msg_paddr;
173         rndis_msg       *rndis_msg;
174         bus_dmamap_t    rndis_msg_dmap;
175 };
176
177 #define HN_TXD_FLAG_ONLIST      0x1
178 #define HN_TXD_FLAG_DMAMAP      0x2
179
180 /*
181  * Only enable UDP checksum offloading when it is on 2012R2 or
182  * later.  UDP checksum offloading doesn't work on earlier
183  * Windows releases.
184  */
185 #define HN_CSUM_ASSIST_WIN8     (CSUM_IP | CSUM_TCP)
186 #define HN_CSUM_ASSIST          (CSUM_IP | CSUM_UDP | CSUM_TCP)
187
188 #define HN_LRO_LENLIM_MULTIRX_DEF       (12 * ETHERMTU)
189 #define HN_LRO_LENLIM_DEF               (25 * ETHERMTU)
190 /* YYY 2*MTU is a bit rough, but should be good enough. */
191 #define HN_LRO_LENLIM_MIN(ifp)          (2 * (ifp)->if_mtu)
192
193 #define HN_LRO_ACKCNT_DEF               1
194
195 /*
196  * Be aware that this sleepable mutex will exhibit WITNESS errors when
197  * certain TCP and ARP code paths are taken.  This appears to be a
198  * well-known condition, as all other drivers checked use a sleeping
199  * mutex to protect their transmit paths.
200  * Also Be aware that mutexes do not play well with semaphores, and there
201  * is a conflicting semaphore in a certain channel code path.
202  */
203 #define NV_LOCK_INIT(_sc, _name) \
204             mtx_init(&(_sc)->hn_lock, _name, MTX_NETWORK_LOCK, MTX_DEF)
205 #define NV_LOCK(_sc)            mtx_lock(&(_sc)->hn_lock)
206 #define NV_LOCK_ASSERT(_sc)     mtx_assert(&(_sc)->hn_lock, MA_OWNED)
207 #define NV_UNLOCK(_sc)          mtx_unlock(&(_sc)->hn_lock)
208 #define NV_LOCK_DESTROY(_sc)    mtx_destroy(&(_sc)->hn_lock)
209
210
211 /*
212  * Globals
213  */
214
215 int hv_promisc_mode = 0;    /* normal mode by default */
216
217 SYSCTL_NODE(_hw, OID_AUTO, hn, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
218     "Hyper-V network interface");
219
220 /* Trust tcp segements verification on host side. */
221 static int hn_trust_hosttcp = 1;
222 SYSCTL_INT(_hw_hn, OID_AUTO, trust_hosttcp, CTLFLAG_RDTUN,
223     &hn_trust_hosttcp, 0,
224     "Trust tcp segement verification on host side, "
225     "when csum info is missing (global setting)");
226
227 /* Trust udp datagrams verification on host side. */
228 static int hn_trust_hostudp = 1;
229 SYSCTL_INT(_hw_hn, OID_AUTO, trust_hostudp, CTLFLAG_RDTUN,
230     &hn_trust_hostudp, 0,
231     "Trust udp datagram verification on host side, "
232     "when csum info is missing (global setting)");
233
234 /* Trust ip packets verification on host side. */
235 static int hn_trust_hostip = 1;
236 SYSCTL_INT(_hw_hn, OID_AUTO, trust_hostip, CTLFLAG_RDTUN,
237     &hn_trust_hostip, 0,
238     "Trust ip packet verification on host side, "
239     "when csum info is missing (global setting)");
240
241 #if __FreeBSD_version >= 1100045
242 /* Limit TSO burst size */
243 static int hn_tso_maxlen = 0;
244 SYSCTL_INT(_hw_hn, OID_AUTO, tso_maxlen, CTLFLAG_RDTUN,
245     &hn_tso_maxlen, 0, "TSO burst limit");
246 #endif
247
248 /* Limit chimney send size */
249 static int hn_tx_chimney_size = 0;
250 SYSCTL_INT(_hw_hn, OID_AUTO, tx_chimney_size, CTLFLAG_RDTUN,
251     &hn_tx_chimney_size, 0, "Chimney send packet size limit");
252
253 /* Limit the size of packet for direct transmission */
254 static int hn_direct_tx_size = HN_DIRECT_TX_SIZE_DEF;
255 SYSCTL_INT(_hw_hn, OID_AUTO, direct_tx_size, CTLFLAG_RDTUN,
256     &hn_direct_tx_size, 0, "Size of the packet for direct transmission");
257
258 #if defined(INET) || defined(INET6)
259 #if __FreeBSD_version >= 1100095
260 static int hn_lro_entry_count = HN_LROENT_CNT_DEF;
261 SYSCTL_INT(_hw_hn, OID_AUTO, lro_entry_count, CTLFLAG_RDTUN,
262     &hn_lro_entry_count, 0, "LRO entry count");
263 #endif
264 #endif
265
266 static int hn_share_tx_taskq = 0;
267 SYSCTL_INT(_hw_hn, OID_AUTO, share_tx_taskq, CTLFLAG_RDTUN,
268     &hn_share_tx_taskq, 0, "Enable shared TX taskqueue");
269
270 static struct taskqueue *hn_tx_taskq;
271
272 #ifndef HN_USE_TXDESC_BUFRING
273 static int hn_use_txdesc_bufring = 0;
274 #else
275 static int hn_use_txdesc_bufring = 1;
276 #endif
277 SYSCTL_INT(_hw_hn, OID_AUTO, use_txdesc_bufring, CTLFLAG_RD,
278     &hn_use_txdesc_bufring, 0, "Use buf_ring for TX descriptors");
279
280 static int hn_bind_tx_taskq = -1;
281 SYSCTL_INT(_hw_hn, OID_AUTO, bind_tx_taskq, CTLFLAG_RDTUN,
282     &hn_bind_tx_taskq, 0, "Bind TX taskqueue to the specified cpu");
283
284 static int hn_use_if_start = 0;
285 SYSCTL_INT(_hw_hn, OID_AUTO, use_if_start, CTLFLAG_RDTUN,
286     &hn_use_if_start, 0, "Use if_start TX method");
287
288 static int hn_chan_cnt = 0;
289 SYSCTL_INT(_hw_hn, OID_AUTO, chan_cnt, CTLFLAG_RDTUN,
290     &hn_chan_cnt, 0,
291     "# of channels to use; each channel has one RX ring and one TX ring");
292
293 static int hn_tx_ring_cnt = 0;
294 SYSCTL_INT(_hw_hn, OID_AUTO, tx_ring_cnt, CTLFLAG_RDTUN,
295     &hn_tx_ring_cnt, 0, "# of TX rings to use");
296
297 static int hn_tx_swq_depth = 0;
298 SYSCTL_INT(_hw_hn, OID_AUTO, tx_swq_depth, CTLFLAG_RDTUN,
299     &hn_tx_swq_depth, 0, "Depth of IFQ or BUFRING");
300
301 static u_int hn_cpu_index;
302
303 /*
304  * Forward declarations
305  */
306 static void hn_stop(hn_softc_t *sc);
307 static void hn_ifinit_locked(hn_softc_t *sc);
308 static void hn_ifinit(void *xsc);
309 static int  hn_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
310 static int hn_start_locked(struct hn_tx_ring *txr, int len);
311 static void hn_start(struct ifnet *ifp);
312 static void hn_start_txeof(struct hn_tx_ring *);
313 static int hn_ifmedia_upd(struct ifnet *ifp);
314 static void hn_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr);
315 #if __FreeBSD_version >= 1100099
316 static int hn_lro_lenlim_sysctl(SYSCTL_HANDLER_ARGS);
317 static int hn_lro_ackcnt_sysctl(SYSCTL_HANDLER_ARGS);
318 #endif
319 static int hn_trust_hcsum_sysctl(SYSCTL_HANDLER_ARGS);
320 static int hn_tx_chimney_size_sysctl(SYSCTL_HANDLER_ARGS);
321 static int hn_rx_stat_ulong_sysctl(SYSCTL_HANDLER_ARGS);
322 static int hn_rx_stat_u64_sysctl(SYSCTL_HANDLER_ARGS);
323 static int hn_tx_stat_ulong_sysctl(SYSCTL_HANDLER_ARGS);
324 static int hn_tx_conf_int_sysctl(SYSCTL_HANDLER_ARGS);
325 static int hn_check_iplen(const struct mbuf *, int);
326 static int hn_create_tx_ring(struct hn_softc *, int);
327 static void hn_destroy_tx_ring(struct hn_tx_ring *);
328 static int hn_create_tx_data(struct hn_softc *, int);
329 static void hn_destroy_tx_data(struct hn_softc *);
330 static void hn_start_taskfunc(void *, int);
331 static void hn_start_txeof_taskfunc(void *, int);
332 static void hn_stop_tx_tasks(struct hn_softc *);
333 static int hn_encap(struct hn_tx_ring *, struct hn_txdesc *, struct mbuf **);
334 static void hn_create_rx_data(struct hn_softc *sc, int);
335 static void hn_destroy_rx_data(struct hn_softc *sc);
336 static void hn_set_tx_chimney_size(struct hn_softc *, int);
337 static void hn_channel_attach(struct hn_softc *, struct hv_vmbus_channel *);
338 static void hn_subchan_attach(struct hn_softc *, struct hv_vmbus_channel *);
339
340 static int hn_transmit(struct ifnet *, struct mbuf *);
341 static void hn_xmit_qflush(struct ifnet *);
342 static int hn_xmit(struct hn_tx_ring *, int);
343 static void hn_xmit_txeof(struct hn_tx_ring *);
344 static void hn_xmit_taskfunc(void *, int);
345 static void hn_xmit_txeof_taskfunc(void *, int);
346
347 #if __FreeBSD_version >= 1100099
348 static void
349 hn_set_lro_lenlim(struct hn_softc *sc, int lenlim)
350 {
351         int i;
352
353         for (i = 0; i < sc->hn_rx_ring_inuse; ++i)
354                 sc->hn_rx_ring[i].hn_lro.lro_length_lim = lenlim;
355 }
356 #endif
357
358 static int
359 hn_get_txswq_depth(const struct hn_tx_ring *txr)
360 {
361
362         KASSERT(txr->hn_txdesc_cnt > 0, ("tx ring is not setup yet"));
363         if (hn_tx_swq_depth < txr->hn_txdesc_cnt)
364                 return txr->hn_txdesc_cnt;
365         return hn_tx_swq_depth;
366 }
367
368 static int
369 hn_ifmedia_upd(struct ifnet *ifp __unused)
370 {
371
372         return EOPNOTSUPP;
373 }
374
375 static void
376 hn_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
377 {
378         struct hn_softc *sc = ifp->if_softc;
379
380         ifmr->ifm_status = IFM_AVALID;
381         ifmr->ifm_active = IFM_ETHER;
382
383         if (!sc->hn_carrier) {
384                 ifmr->ifm_active |= IFM_NONE;
385                 return;
386         }
387         ifmr->ifm_status |= IFM_ACTIVE;
388         ifmr->ifm_active |= IFM_10G_T | IFM_FDX;
389 }
390
391 /* {F8615163-DF3E-46c5-913F-F2D2F965ED0E} */
392 static const hv_guid g_net_vsc_device_type = {
393         .data = {0x63, 0x51, 0x61, 0xF8, 0x3E, 0xDF, 0xc5, 0x46,
394                 0x91, 0x3F, 0xF2, 0xD2, 0xF9, 0x65, 0xED, 0x0E}
395 };
396
397 /*
398  * Standard probe entry point.
399  *
400  */
401 static int
402 netvsc_probe(device_t dev)
403 {
404         const char *p;
405
406         p = vmbus_get_type(dev);
407         if (!memcmp(p, &g_net_vsc_device_type.data, sizeof(hv_guid))) {
408                 device_set_desc(dev, "Hyper-V Network Interface");
409                 if (bootverbose)
410                         printf("Netvsc probe... DONE \n");
411
412                 return (BUS_PROBE_DEFAULT);
413         }
414
415         return (ENXIO);
416 }
417
418 /*
419  * Standard attach entry point.
420  *
421  * Called when the driver is loaded.  It allocates needed resources,
422  * and initializes the "hardware" and software.
423  */
424 static int
425 netvsc_attach(device_t dev)
426 {
427         struct hv_device *device_ctx = vmbus_get_devctx(dev);
428         struct hv_vmbus_channel *pri_chan;
429         netvsc_device_info device_info;
430         hn_softc_t *sc;
431         int unit = device_get_unit(dev);
432         struct ifnet *ifp = NULL;
433         int error, ring_cnt, tx_ring_cnt;
434 #if __FreeBSD_version >= 1100045
435         int tso_maxlen;
436 #endif
437
438         sc = device_get_softc(dev);
439
440         sc->hn_unit = unit;
441         sc->hn_dev = dev;
442
443         if (hn_tx_taskq == NULL) {
444                 sc->hn_tx_taskq = taskqueue_create("hn_tx", M_WAITOK,
445                     taskqueue_thread_enqueue, &sc->hn_tx_taskq);
446                 if (hn_bind_tx_taskq >= 0) {
447                         int cpu = hn_bind_tx_taskq;
448                         cpuset_t cpu_set;
449
450                         if (cpu > mp_ncpus - 1)
451                                 cpu = mp_ncpus - 1;
452                         CPU_SETOF(cpu, &cpu_set);
453                         taskqueue_start_threads_cpuset(&sc->hn_tx_taskq, 1,
454                             PI_NET, &cpu_set, "%s tx",
455                             device_get_nameunit(dev));
456                 } else {
457                         taskqueue_start_threads(&sc->hn_tx_taskq, 1, PI_NET,
458                             "%s tx", device_get_nameunit(dev));
459                 }
460         } else {
461                 sc->hn_tx_taskq = hn_tx_taskq;
462         }
463         NV_LOCK_INIT(sc, "NetVSCLock");
464
465         sc->hn_dev_obj = device_ctx;
466
467         ifp = sc->hn_ifp = if_alloc(IFT_ETHER);
468         ifp->if_softc = sc;
469         if_initname(ifp, device_get_name(dev), device_get_unit(dev));
470
471         /*
472          * Figure out the # of RX rings (ring_cnt) and the # of TX rings
473          * to use (tx_ring_cnt).
474          *
475          * NOTE:
476          * The # of RX rings to use is same as the # of channels to use.
477          */
478         ring_cnt = hn_chan_cnt;
479         if (ring_cnt <= 0) {
480                 /* Default */
481                 ring_cnt = mp_ncpus;
482                 if (ring_cnt > HN_RING_CNT_DEF_MAX)
483                         ring_cnt = HN_RING_CNT_DEF_MAX;
484         } else if (ring_cnt > mp_ncpus) {
485                 ring_cnt = mp_ncpus;
486         }
487
488         tx_ring_cnt = hn_tx_ring_cnt;
489         if (tx_ring_cnt <= 0 || tx_ring_cnt > ring_cnt)
490                 tx_ring_cnt = ring_cnt;
491         if (hn_use_if_start) {
492                 /* ifnet.if_start only needs one TX ring. */
493                 tx_ring_cnt = 1;
494         }
495
496         /*
497          * Set the leader CPU for channels.
498          */
499         sc->hn_cpu = atomic_fetchadd_int(&hn_cpu_index, ring_cnt) % mp_ncpus;
500
501         error = hn_create_tx_data(sc, tx_ring_cnt);
502         if (error)
503                 goto failed;
504         hn_create_rx_data(sc, ring_cnt);
505
506         /*
507          * Associate the first TX/RX ring w/ the primary channel.
508          */
509         pri_chan = device_ctx->channel;
510         KASSERT(HV_VMBUS_CHAN_ISPRIMARY(pri_chan), ("not primary channel"));
511         KASSERT(pri_chan->offer_msg.offer.sub_channel_index == 0,
512             ("primary channel subidx %u",
513              pri_chan->offer_msg.offer.sub_channel_index));
514         hn_channel_attach(sc, pri_chan);
515
516         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
517         ifp->if_ioctl = hn_ioctl;
518         ifp->if_init = hn_ifinit;
519         /* needed by hv_rf_on_device_add() code */
520         ifp->if_mtu = ETHERMTU;
521         if (hn_use_if_start) {
522                 int qdepth = hn_get_txswq_depth(&sc->hn_tx_ring[0]);
523
524                 ifp->if_start = hn_start;
525                 IFQ_SET_MAXLEN(&ifp->if_snd, qdepth);
526                 ifp->if_snd.ifq_drv_maxlen = qdepth - 1;
527                 IFQ_SET_READY(&ifp->if_snd);
528         } else {
529                 ifp->if_transmit = hn_transmit;
530                 ifp->if_qflush = hn_xmit_qflush;
531         }
532
533         ifmedia_init(&sc->hn_media, 0, hn_ifmedia_upd, hn_ifmedia_sts);
534         ifmedia_add(&sc->hn_media, IFM_ETHER | IFM_AUTO, 0, NULL);
535         ifmedia_set(&sc->hn_media, IFM_ETHER | IFM_AUTO);
536         /* XXX ifmedia_set really should do this for us */
537         sc->hn_media.ifm_media = sc->hn_media.ifm_cur->ifm_media;
538
539         /*
540          * Tell upper layers that we support full VLAN capability.
541          */
542         ifp->if_hdrlen = sizeof(struct ether_vlan_header);
543         ifp->if_capabilities |=
544             IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU | IFCAP_HWCSUM | IFCAP_TSO |
545             IFCAP_LRO;
546         ifp->if_capenable |=
547             IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU | IFCAP_HWCSUM | IFCAP_TSO |
548             IFCAP_LRO;
549         ifp->if_hwassist = sc->hn_tx_ring[0].hn_csum_assist | CSUM_TSO;
550
551         error = hv_rf_on_device_add(device_ctx, &device_info, ring_cnt);
552         if (error)
553                 goto failed;
554         KASSERT(sc->net_dev->num_channel > 0 &&
555             sc->net_dev->num_channel <= sc->hn_rx_ring_inuse,
556             ("invalid channel count %u, should be less than %d",
557              sc->net_dev->num_channel, sc->hn_rx_ring_inuse));
558
559         /*
560          * Set the # of TX/RX rings that could be used according to
561          * the # of channels that host offered.
562          */
563         if (sc->hn_tx_ring_inuse > sc->net_dev->num_channel)
564                 sc->hn_tx_ring_inuse = sc->net_dev->num_channel;
565         sc->hn_rx_ring_inuse = sc->net_dev->num_channel;
566         device_printf(dev, "%d TX ring, %d RX ring\n",
567             sc->hn_tx_ring_inuse, sc->hn_rx_ring_inuse);
568
569         if (sc->net_dev->num_channel > 1) {
570                 struct hv_vmbus_channel **subchan;
571                 int subchan_cnt = sc->net_dev->num_channel - 1;
572                 int i;
573
574                 /* Wait for sub-channels setup to complete. */
575                 subchan = vmbus_get_subchan(pri_chan, subchan_cnt);
576
577                 /* Attach the sub-channels. */
578                 for (i = 0; i < subchan_cnt; ++i) {
579                         /* NOTE: Calling order is critical. */
580                         hn_subchan_attach(sc, subchan[i]);
581                         hv_nv_subchan_attach(subchan[i]);
582                 }
583
584                 /* Release the sub-channels */
585                 vmbus_rel_subchan(subchan, subchan_cnt);
586                 device_printf(dev, "%d sub-channels setup done\n", subchan_cnt);
587         }
588
589 #if __FreeBSD_version >= 1100099
590         if (sc->hn_rx_ring_inuse > 1) {
591                 /*
592                  * Reduce TCP segment aggregation limit for multiple
593                  * RX rings to increase ACK timeliness.
594                  */
595                 hn_set_lro_lenlim(sc, HN_LRO_LENLIM_MULTIRX_DEF);
596         }
597 #endif
598
599         if (device_info.link_state == 0) {
600                 sc->hn_carrier = 1;
601         }
602
603 #if __FreeBSD_version >= 1100045
604         tso_maxlen = hn_tso_maxlen;
605         if (tso_maxlen <= 0 || tso_maxlen > IP_MAXPACKET)
606                 tso_maxlen = IP_MAXPACKET;
607
608         ifp->if_hw_tsomaxsegcount = HN_TX_DATA_SEGCNT_MAX;
609         ifp->if_hw_tsomaxsegsize = PAGE_SIZE;
610         ifp->if_hw_tsomax = tso_maxlen -
611             (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN);
612 #endif
613
614         ether_ifattach(ifp, device_info.mac_addr);
615
616 #if __FreeBSD_version >= 1100045
617         if_printf(ifp, "TSO: %u/%u/%u\n", ifp->if_hw_tsomax,
618             ifp->if_hw_tsomaxsegcount, ifp->if_hw_tsomaxsegsize);
619 #endif
620
621         sc->hn_tx_chimney_max = sc->net_dev->send_section_size;
622         hn_set_tx_chimney_size(sc, sc->hn_tx_chimney_max);
623         if (hn_tx_chimney_size > 0 &&
624             hn_tx_chimney_size < sc->hn_tx_chimney_max)
625                 hn_set_tx_chimney_size(sc, hn_tx_chimney_size);
626
627         return (0);
628 failed:
629         hn_destroy_tx_data(sc);
630         if (ifp != NULL)
631                 if_free(ifp);
632         return (error);
633 }
634
635 /*
636  * Standard detach entry point
637  */
638 static int
639 netvsc_detach(device_t dev)
640 {
641         struct hn_softc *sc = device_get_softc(dev);
642         struct hv_device *hv_device = vmbus_get_devctx(dev); 
643
644         if (bootverbose)
645                 printf("netvsc_detach\n");
646
647         /*
648          * XXXKYS:  Need to clean up all our
649          * driver state; this is the driver
650          * unloading.
651          */
652
653         /*
654          * XXXKYS:  Need to stop outgoing traffic and unregister
655          * the netdevice.
656          */
657
658         hv_rf_on_device_remove(hv_device, HV_RF_NV_DESTROY_CHANNEL);
659
660         hn_stop_tx_tasks(sc);
661
662         ifmedia_removeall(&sc->hn_media);
663         hn_destroy_rx_data(sc);
664         hn_destroy_tx_data(sc);
665
666         if (sc->hn_tx_taskq != hn_tx_taskq)
667                 taskqueue_free(sc->hn_tx_taskq);
668
669         return (0);
670 }
671
672 /*
673  * Standard shutdown entry point
674  */
675 static int
676 netvsc_shutdown(device_t dev)
677 {
678         return (0);
679 }
680
681 static __inline int
682 hn_txdesc_dmamap_load(struct hn_tx_ring *txr, struct hn_txdesc *txd,
683     struct mbuf **m_head, bus_dma_segment_t *segs, int *nsegs)
684 {
685         struct mbuf *m = *m_head;
686         int error;
687
688         error = bus_dmamap_load_mbuf_sg(txr->hn_tx_data_dtag, txd->data_dmap,
689             m, segs, nsegs, BUS_DMA_NOWAIT);
690         if (error == EFBIG) {
691                 struct mbuf *m_new;
692
693                 m_new = m_collapse(m, M_NOWAIT, HN_TX_DATA_SEGCNT_MAX);
694                 if (m_new == NULL)
695                         return ENOBUFS;
696                 else
697                         *m_head = m = m_new;
698                 txr->hn_tx_collapsed++;
699
700                 error = bus_dmamap_load_mbuf_sg(txr->hn_tx_data_dtag,
701                     txd->data_dmap, m, segs, nsegs, BUS_DMA_NOWAIT);
702         }
703         if (!error) {
704                 bus_dmamap_sync(txr->hn_tx_data_dtag, txd->data_dmap,
705                     BUS_DMASYNC_PREWRITE);
706                 txd->flags |= HN_TXD_FLAG_DMAMAP;
707         }
708         return error;
709 }
710
711 static __inline void
712 hn_txdesc_dmamap_unload(struct hn_tx_ring *txr, struct hn_txdesc *txd)
713 {
714
715         if (txd->flags & HN_TXD_FLAG_DMAMAP) {
716                 bus_dmamap_sync(txr->hn_tx_data_dtag,
717                     txd->data_dmap, BUS_DMASYNC_POSTWRITE);
718                 bus_dmamap_unload(txr->hn_tx_data_dtag,
719                     txd->data_dmap);
720                 txd->flags &= ~HN_TXD_FLAG_DMAMAP;
721         }
722 }
723
724 static __inline int
725 hn_txdesc_put(struct hn_tx_ring *txr, struct hn_txdesc *txd)
726 {
727
728         KASSERT((txd->flags & HN_TXD_FLAG_ONLIST) == 0,
729             ("put an onlist txd %#x", txd->flags));
730
731         KASSERT(txd->refs > 0, ("invalid txd refs %d", txd->refs));
732         if (atomic_fetchadd_int(&txd->refs, -1) != 1)
733                 return 0;
734
735         hn_txdesc_dmamap_unload(txr, txd);
736         if (txd->m != NULL) {
737                 m_freem(txd->m);
738                 txd->m = NULL;
739         }
740
741         txd->flags |= HN_TXD_FLAG_ONLIST;
742
743 #ifndef HN_USE_TXDESC_BUFRING
744         mtx_lock_spin(&txr->hn_txlist_spin);
745         KASSERT(txr->hn_txdesc_avail >= 0 &&
746             txr->hn_txdesc_avail < txr->hn_txdesc_cnt,
747             ("txdesc_put: invalid txd avail %d", txr->hn_txdesc_avail));
748         txr->hn_txdesc_avail++;
749         SLIST_INSERT_HEAD(&txr->hn_txlist, txd, link);
750         mtx_unlock_spin(&txr->hn_txlist_spin);
751 #else
752         atomic_add_int(&txr->hn_txdesc_avail, 1);
753         buf_ring_enqueue(txr->hn_txdesc_br, txd);
754 #endif
755
756         return 1;
757 }
758
759 static __inline struct hn_txdesc *
760 hn_txdesc_get(struct hn_tx_ring *txr)
761 {
762         struct hn_txdesc *txd;
763
764 #ifndef HN_USE_TXDESC_BUFRING
765         mtx_lock_spin(&txr->hn_txlist_spin);
766         txd = SLIST_FIRST(&txr->hn_txlist);
767         if (txd != NULL) {
768                 KASSERT(txr->hn_txdesc_avail > 0,
769                     ("txdesc_get: invalid txd avail %d", txr->hn_txdesc_avail));
770                 txr->hn_txdesc_avail--;
771                 SLIST_REMOVE_HEAD(&txr->hn_txlist, link);
772         }
773         mtx_unlock_spin(&txr->hn_txlist_spin);
774 #else
775         txd = buf_ring_dequeue_sc(txr->hn_txdesc_br);
776 #endif
777
778         if (txd != NULL) {
779 #ifdef HN_USE_TXDESC_BUFRING
780                 atomic_subtract_int(&txr->hn_txdesc_avail, 1);
781 #endif
782                 KASSERT(txd->m == NULL && txd->refs == 0 &&
783                     (txd->flags & HN_TXD_FLAG_ONLIST), ("invalid txd"));
784                 txd->flags &= ~HN_TXD_FLAG_ONLIST;
785                 txd->refs = 1;
786         }
787         return txd;
788 }
789
790 static __inline void
791 hn_txdesc_hold(struct hn_txdesc *txd)
792 {
793
794         /* 0->1 transition will never work */
795         KASSERT(txd->refs > 0, ("invalid refs %d", txd->refs));
796         atomic_add_int(&txd->refs, 1);
797 }
798
799 static __inline void
800 hn_txeof(struct hn_tx_ring *txr)
801 {
802         txr->hn_has_txeof = 0;
803         txr->hn_txeof(txr);
804 }
805
806 static void
807 hn_tx_done(struct hv_vmbus_channel *chan, void *xpkt)
808 {
809         netvsc_packet *packet = xpkt;
810         struct hn_txdesc *txd;
811         struct hn_tx_ring *txr;
812
813         txd = (struct hn_txdesc *)(uintptr_t)
814             packet->compl.send.send_completion_tid;
815
816         txr = txd->txr;
817         KASSERT(txr->hn_chan == chan,
818             ("channel mismatch, on channel%u, should be channel%u",
819              chan->offer_msg.offer.sub_channel_index,
820              txr->hn_chan->offer_msg.offer.sub_channel_index));
821
822         txr->hn_has_txeof = 1;
823         hn_txdesc_put(txr, txd);
824
825         ++txr->hn_txdone_cnt;
826         if (txr->hn_txdone_cnt >= HN_EARLY_TXEOF_THRESH) {
827                 txr->hn_txdone_cnt = 0;
828                 if (txr->hn_oactive)
829                         hn_txeof(txr);
830         }
831 }
832
833 void
834 netvsc_channel_rollup(struct hv_vmbus_channel *chan)
835 {
836         struct hn_tx_ring *txr = chan->hv_chan_txr;
837 #if defined(INET) || defined(INET6)
838         struct hn_rx_ring *rxr = chan->hv_chan_rxr;
839
840         tcp_lro_flush_all(&rxr->hn_lro);
841 #endif
842
843         /*
844          * NOTE:
845          * 'txr' could be NULL, if multiple channels and
846          * ifnet.if_start method are enabled.
847          */
848         if (txr == NULL || !txr->hn_has_txeof)
849                 return;
850
851         txr->hn_txdone_cnt = 0;
852         hn_txeof(txr);
853 }
854
855 /*
856  * NOTE:
857  * If this function fails, then both txd and m_head0 will be freed.
858  */
859 static int
860 hn_encap(struct hn_tx_ring *txr, struct hn_txdesc *txd, struct mbuf **m_head0)
861 {
862         bus_dma_segment_t segs[HN_TX_DATA_SEGCNT_MAX];
863         int error, nsegs, i;
864         struct mbuf *m_head = *m_head0;
865         netvsc_packet *packet;
866         rndis_msg *rndis_mesg;
867         rndis_packet *rndis_pkt;
868         rndis_per_packet_info *rppi;
869         struct ndis_hash_info *hash_info;
870         uint32_t rndis_msg_size;
871
872         packet = &txd->netvsc_pkt;
873         packet->is_data_pkt = TRUE;
874         packet->tot_data_buf_len = m_head->m_pkthdr.len;
875
876         /*
877          * extension points to the area reserved for the
878          * rndis_filter_packet, which is placed just after
879          * the netvsc_packet (and rppi struct, if present;
880          * length is updated later).
881          */
882         rndis_mesg = txd->rndis_msg;
883         /* XXX not necessary */
884         memset(rndis_mesg, 0, HN_RNDIS_MSG_LEN);
885         rndis_mesg->ndis_msg_type = REMOTE_NDIS_PACKET_MSG;
886
887         rndis_pkt = &rndis_mesg->msg.packet;
888         rndis_pkt->data_offset = sizeof(rndis_packet);
889         rndis_pkt->data_length = packet->tot_data_buf_len;
890         rndis_pkt->per_pkt_info_offset = sizeof(rndis_packet);
891
892         rndis_msg_size = RNDIS_MESSAGE_SIZE(rndis_packet);
893
894         /*
895          * Set the hash info for this packet, so that the host could
896          * dispatch the TX done event for this packet back to this TX
897          * ring's channel.
898          */
899         rndis_msg_size += RNDIS_HASH_PPI_SIZE;
900         rppi = hv_set_rppi_data(rndis_mesg, RNDIS_HASH_PPI_SIZE,
901             nbl_hash_value);
902         hash_info = (struct ndis_hash_info *)((uint8_t *)rppi +
903             rppi->per_packet_info_offset);
904         hash_info->hash = txr->hn_tx_idx;
905
906         if (m_head->m_flags & M_VLANTAG) {
907                 ndis_8021q_info *rppi_vlan_info;
908
909                 rndis_msg_size += RNDIS_VLAN_PPI_SIZE;
910                 rppi = hv_set_rppi_data(rndis_mesg, RNDIS_VLAN_PPI_SIZE,
911                     ieee_8021q_info);
912
913                 rppi_vlan_info = (ndis_8021q_info *)((uint8_t *)rppi +
914                     rppi->per_packet_info_offset);
915                 rppi_vlan_info->u1.s1.vlan_id =
916                     m_head->m_pkthdr.ether_vtag & 0xfff;
917         }
918
919         if (m_head->m_pkthdr.csum_flags & CSUM_TSO) {
920                 rndis_tcp_tso_info *tso_info;   
921                 struct ether_vlan_header *eh;
922                 int ether_len;
923
924                 /*
925                  * XXX need m_pullup and use mtodo
926                  */
927                 eh = mtod(m_head, struct ether_vlan_header*);
928                 if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN))
929                         ether_len = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
930                 else
931                         ether_len = ETHER_HDR_LEN;
932
933                 rndis_msg_size += RNDIS_TSO_PPI_SIZE;
934                 rppi = hv_set_rppi_data(rndis_mesg, RNDIS_TSO_PPI_SIZE,
935                     tcp_large_send_info);
936
937                 tso_info = (rndis_tcp_tso_info *)((uint8_t *)rppi +
938                     rppi->per_packet_info_offset);
939                 tso_info->lso_v2_xmit.type =
940                     RNDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE;
941
942 #ifdef INET
943                 if (m_head->m_pkthdr.csum_flags & CSUM_IP_TSO) {
944                         struct ip *ip =
945                             (struct ip *)(m_head->m_data + ether_len);
946                         unsigned long iph_len = ip->ip_hl << 2;
947                         struct tcphdr *th =
948                             (struct tcphdr *)((caddr_t)ip + iph_len);
949
950                         tso_info->lso_v2_xmit.ip_version =
951                             RNDIS_TCP_LARGE_SEND_OFFLOAD_IPV4;
952                         ip->ip_len = 0;
953                         ip->ip_sum = 0;
954
955                         th->th_sum = in_pseudo(ip->ip_src.s_addr,
956                             ip->ip_dst.s_addr, htons(IPPROTO_TCP));
957                 }
958 #endif
959 #if defined(INET6) && defined(INET)
960                 else
961 #endif
962 #ifdef INET6
963                 {
964                         struct ip6_hdr *ip6 = (struct ip6_hdr *)
965                             (m_head->m_data + ether_len);
966                         struct tcphdr *th = (struct tcphdr *)(ip6 + 1);
967
968                         tso_info->lso_v2_xmit.ip_version =
969                             RNDIS_TCP_LARGE_SEND_OFFLOAD_IPV6;
970                         ip6->ip6_plen = 0;
971                         th->th_sum = in6_cksum_pseudo(ip6, 0, IPPROTO_TCP, 0);
972                 }
973 #endif
974                 tso_info->lso_v2_xmit.tcp_header_offset = 0;
975                 tso_info->lso_v2_xmit.mss = m_head->m_pkthdr.tso_segsz;
976         } else if (m_head->m_pkthdr.csum_flags & txr->hn_csum_assist) {
977                 rndis_tcp_ip_csum_info *csum_info;
978
979                 rndis_msg_size += RNDIS_CSUM_PPI_SIZE;
980                 rppi = hv_set_rppi_data(rndis_mesg, RNDIS_CSUM_PPI_SIZE,
981                     tcpip_chksum_info);
982                 csum_info = (rndis_tcp_ip_csum_info *)((uint8_t *)rppi +
983                     rppi->per_packet_info_offset);
984
985                 csum_info->xmit.is_ipv4 = 1;
986                 if (m_head->m_pkthdr.csum_flags & CSUM_IP)
987                         csum_info->xmit.ip_header_csum = 1;
988
989                 if (m_head->m_pkthdr.csum_flags & CSUM_TCP) {
990                         csum_info->xmit.tcp_csum = 1;
991                         csum_info->xmit.tcp_header_offset = 0;
992                 } else if (m_head->m_pkthdr.csum_flags & CSUM_UDP) {
993                         csum_info->xmit.udp_csum = 1;
994                 }
995         }
996
997         rndis_mesg->msg_len = packet->tot_data_buf_len + rndis_msg_size;
998         packet->tot_data_buf_len = rndis_mesg->msg_len;
999
1000         /*
1001          * Chimney send, if the packet could fit into one chimney buffer.
1002          */
1003         if (packet->tot_data_buf_len < txr->hn_tx_chimney_size) {
1004                 netvsc_dev *net_dev = txr->hn_sc->net_dev;
1005                 uint32_t send_buf_section_idx;
1006
1007                 txr->hn_tx_chimney_tried++;
1008                 send_buf_section_idx =
1009                     hv_nv_get_next_send_section(net_dev);
1010                 if (send_buf_section_idx !=
1011                     NVSP_1_CHIMNEY_SEND_INVALID_SECTION_INDEX) {
1012                         uint8_t *dest = ((uint8_t *)net_dev->send_buf +
1013                             (send_buf_section_idx *
1014                              net_dev->send_section_size));
1015
1016                         memcpy(dest, rndis_mesg, rndis_msg_size);
1017                         dest += rndis_msg_size;
1018                         m_copydata(m_head, 0, m_head->m_pkthdr.len, dest);
1019
1020                         packet->send_buf_section_idx = send_buf_section_idx;
1021                         packet->send_buf_section_size =
1022                             packet->tot_data_buf_len;
1023                         packet->page_buf_count = 0;
1024                         txr->hn_tx_chimney++;
1025                         goto done;
1026                 }
1027         }
1028
1029         error = hn_txdesc_dmamap_load(txr, txd, &m_head, segs, &nsegs);
1030         if (error) {
1031                 int freed;
1032
1033                 /*
1034                  * This mbuf is not linked w/ the txd yet, so free it now.
1035                  */
1036                 m_freem(m_head);
1037                 *m_head0 = NULL;
1038
1039                 freed = hn_txdesc_put(txr, txd);
1040                 KASSERT(freed != 0,
1041                     ("fail to free txd upon txdma error"));
1042
1043                 txr->hn_txdma_failed++;
1044                 if_inc_counter(txr->hn_sc->hn_ifp, IFCOUNTER_OERRORS, 1);
1045                 return error;
1046         }
1047         *m_head0 = m_head;
1048
1049         packet->page_buf_count = nsegs + HV_RF_NUM_TX_RESERVED_PAGE_BUFS;
1050
1051         /* send packet with page buffer */
1052         packet->page_buffers[0].pfn = atop(txd->rndis_msg_paddr);
1053         packet->page_buffers[0].offset = txd->rndis_msg_paddr & PAGE_MASK;
1054         packet->page_buffers[0].length = rndis_msg_size;
1055
1056         /*
1057          * Fill the page buffers with mbuf info starting at index
1058          * HV_RF_NUM_TX_RESERVED_PAGE_BUFS.
1059          */
1060         for (i = 0; i < nsegs; ++i) {
1061                 hv_vmbus_page_buffer *pb = &packet->page_buffers[
1062                     i + HV_RF_NUM_TX_RESERVED_PAGE_BUFS];
1063
1064                 pb->pfn = atop(segs[i].ds_addr);
1065                 pb->offset = segs[i].ds_addr & PAGE_MASK;
1066                 pb->length = segs[i].ds_len;
1067         }
1068
1069         packet->send_buf_section_idx =
1070             NVSP_1_CHIMNEY_SEND_INVALID_SECTION_INDEX;
1071         packet->send_buf_section_size = 0;
1072 done:
1073         txd->m = m_head;
1074
1075         /* Set the completion routine */
1076         packet->compl.send.on_send_completion = hn_tx_done;
1077         packet->compl.send.send_completion_context = packet;
1078         packet->compl.send.send_completion_tid = (uint64_t)(uintptr_t)txd;
1079
1080         return 0;
1081 }
1082
1083 /*
1084  * NOTE:
1085  * If this function fails, then txd will be freed, but the mbuf
1086  * associated w/ the txd will _not_ be freed.
1087  */
1088 static int
1089 hn_send_pkt(struct ifnet *ifp, struct hn_tx_ring *txr, struct hn_txdesc *txd)
1090 {
1091         int error, send_failed = 0;
1092
1093 again:
1094         /*
1095          * Make sure that txd is not freed before ETHER_BPF_MTAP.
1096          */
1097         hn_txdesc_hold(txd);
1098         error = hv_nv_on_send(txr->hn_chan, &txd->netvsc_pkt);
1099         if (!error) {
1100                 ETHER_BPF_MTAP(ifp, txd->m);
1101                 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1102                 if (!hn_use_if_start) {
1103                         if_inc_counter(ifp, IFCOUNTER_OBYTES,
1104                             txd->m->m_pkthdr.len);
1105                         if (txd->m->m_flags & M_MCAST)
1106                                 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
1107                 }
1108                 txr->hn_pkts++;
1109         }
1110         hn_txdesc_put(txr, txd);
1111
1112         if (__predict_false(error)) {
1113                 int freed;
1114
1115                 /*
1116                  * This should "really rarely" happen.
1117                  *
1118                  * XXX Too many RX to be acked or too many sideband
1119                  * commands to run?  Ask netvsc_channel_rollup()
1120                  * to kick start later.
1121                  */
1122                 txr->hn_has_txeof = 1;
1123                 if (!send_failed) {
1124                         txr->hn_send_failed++;
1125                         send_failed = 1;
1126                         /*
1127                          * Try sending again after set hn_has_txeof;
1128                          * in case that we missed the last
1129                          * netvsc_channel_rollup().
1130                          */
1131                         goto again;
1132                 }
1133                 if_printf(ifp, "send failed\n");
1134
1135                 /*
1136                  * Caller will perform further processing on the
1137                  * associated mbuf, so don't free it in hn_txdesc_put();
1138                  * only unload it from the DMA map in hn_txdesc_put(),
1139                  * if it was loaded.
1140                  */
1141                 txd->m = NULL;
1142                 freed = hn_txdesc_put(txr, txd);
1143                 KASSERT(freed != 0,
1144                     ("fail to free txd upon send error"));
1145
1146                 txr->hn_send_failed++;
1147         }
1148         return error;
1149 }
1150
1151 /*
1152  * Start a transmit of one or more packets
1153  */
1154 static int
1155 hn_start_locked(struct hn_tx_ring *txr, int len)
1156 {
1157         struct hn_softc *sc = txr->hn_sc;
1158         struct ifnet *ifp = sc->hn_ifp;
1159
1160         KASSERT(hn_use_if_start,
1161             ("hn_start_locked is called, when if_start is disabled"));
1162         KASSERT(txr == &sc->hn_tx_ring[0], ("not the first TX ring"));
1163         mtx_assert(&txr->hn_tx_lock, MA_OWNED);
1164
1165         if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
1166             IFF_DRV_RUNNING)
1167                 return 0;
1168
1169         while (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) {
1170                 struct hn_txdesc *txd;
1171                 struct mbuf *m_head;
1172                 int error;
1173
1174                 IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
1175                 if (m_head == NULL)
1176                         break;
1177
1178                 if (len > 0 && m_head->m_pkthdr.len > len) {
1179                         /*
1180                          * This sending could be time consuming; let callers
1181                          * dispatch this packet sending (and sending of any
1182                          * following up packets) to tx taskqueue.
1183                          */
1184                         IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
1185                         return 1;
1186                 }
1187
1188                 txd = hn_txdesc_get(txr);
1189                 if (txd == NULL) {
1190                         txr->hn_no_txdescs++;
1191                         IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
1192                         atomic_set_int(&ifp->if_drv_flags, IFF_DRV_OACTIVE);
1193                         break;
1194                 }
1195
1196                 error = hn_encap(txr, txd, &m_head);
1197                 if (error) {
1198                         /* Both txd and m_head are freed */
1199                         continue;
1200                 }
1201
1202                 error = hn_send_pkt(ifp, txr, txd);
1203                 if (__predict_false(error)) {
1204                         /* txd is freed, but m_head is not */
1205                         IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
1206                         atomic_set_int(&ifp->if_drv_flags, IFF_DRV_OACTIVE);
1207                         break;
1208                 }
1209         }
1210         return 0;
1211 }
1212
1213 /*
1214  * Link up/down notification
1215  */
1216 void
1217 netvsc_linkstatus_callback(struct hv_device *device_obj, uint32_t status)
1218 {
1219         hn_softc_t *sc = device_get_softc(device_obj->device);
1220
1221         if (status == 1) {
1222                 sc->hn_carrier = 1;
1223         } else {
1224                 sc->hn_carrier = 0;
1225         }
1226 }
1227
1228 /*
1229  * Append the specified data to the indicated mbuf chain,
1230  * Extend the mbuf chain if the new data does not fit in
1231  * existing space.
1232  *
1233  * This is a minor rewrite of m_append() from sys/kern/uipc_mbuf.c.
1234  * There should be an equivalent in the kernel mbuf code,
1235  * but there does not appear to be one yet.
1236  *
1237  * Differs from m_append() in that additional mbufs are
1238  * allocated with cluster size MJUMPAGESIZE, and filled
1239  * accordingly.
1240  *
1241  * Return 1 if able to complete the job; otherwise 0.
1242  */
1243 static int
1244 hv_m_append(struct mbuf *m0, int len, c_caddr_t cp)
1245 {
1246         struct mbuf *m, *n;
1247         int remainder, space;
1248
1249         for (m = m0; m->m_next != NULL; m = m->m_next)
1250                 ;
1251         remainder = len;
1252         space = M_TRAILINGSPACE(m);
1253         if (space > 0) {
1254                 /*
1255                  * Copy into available space.
1256                  */
1257                 if (space > remainder)
1258                         space = remainder;
1259                 bcopy(cp, mtod(m, caddr_t) + m->m_len, space);
1260                 m->m_len += space;
1261                 cp += space;
1262                 remainder -= space;
1263         }
1264         while (remainder > 0) {
1265                 /*
1266                  * Allocate a new mbuf; could check space
1267                  * and allocate a cluster instead.
1268                  */
1269                 n = m_getjcl(M_NOWAIT, m->m_type, 0, MJUMPAGESIZE);
1270                 if (n == NULL)
1271                         break;
1272                 n->m_len = min(MJUMPAGESIZE, remainder);
1273                 bcopy(cp, mtod(n, caddr_t), n->m_len);
1274                 cp += n->m_len;
1275                 remainder -= n->m_len;
1276                 m->m_next = n;
1277                 m = n;
1278         }
1279         if (m0->m_flags & M_PKTHDR)
1280                 m0->m_pkthdr.len += len - remainder;
1281
1282         return (remainder == 0);
1283 }
1284
1285
1286 /*
1287  * Called when we receive a data packet from the "wire" on the
1288  * specified device
1289  *
1290  * Note:  This is no longer used as a callback
1291  */
1292 int
1293 netvsc_recv(struct hv_vmbus_channel *chan, netvsc_packet *packet,
1294     rndis_tcp_ip_csum_info *csum_info)
1295 {
1296         struct hn_rx_ring *rxr = chan->hv_chan_rxr;
1297         struct ifnet *ifp = rxr->hn_ifp;
1298         struct mbuf *m_new;
1299         int size, do_lro = 0, do_csum = 1;
1300
1301         if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
1302                 return (0);
1303
1304         /*
1305          * Bail out if packet contains more data than configured MTU.
1306          */
1307         if (packet->tot_data_buf_len > (ifp->if_mtu + ETHER_HDR_LEN)) {
1308                 return (0);
1309         } else if (packet->tot_data_buf_len <= MHLEN) {
1310                 m_new = m_gethdr(M_NOWAIT, MT_DATA);
1311                 if (m_new == NULL) {
1312                         if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
1313                         return (0);
1314                 }
1315                 memcpy(mtod(m_new, void *), packet->data,
1316                     packet->tot_data_buf_len);
1317                 m_new->m_pkthdr.len = m_new->m_len = packet->tot_data_buf_len;
1318                 rxr->hn_small_pkts++;
1319         } else {
1320                 /*
1321                  * Get an mbuf with a cluster.  For packets 2K or less,
1322                  * get a standard 2K cluster.  For anything larger, get a
1323                  * 4K cluster.  Any buffers larger than 4K can cause problems
1324                  * if looped around to the Hyper-V TX channel, so avoid them.
1325                  */
1326                 size = MCLBYTES;
1327                 if (packet->tot_data_buf_len > MCLBYTES) {
1328                         /* 4096 */
1329                         size = MJUMPAGESIZE;
1330                 }
1331
1332                 m_new = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, size);
1333                 if (m_new == NULL) {
1334                         if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
1335                         return (0);
1336                 }
1337
1338                 hv_m_append(m_new, packet->tot_data_buf_len, packet->data);
1339         }
1340         m_new->m_pkthdr.rcvif = ifp;
1341
1342         if (__predict_false((ifp->if_capenable & IFCAP_RXCSUM) == 0))
1343                 do_csum = 0;
1344
1345         /* receive side checksum offload */
1346         if (csum_info != NULL) {
1347                 /* IP csum offload */
1348                 if (csum_info->receive.ip_csum_succeeded && do_csum) {
1349                         m_new->m_pkthdr.csum_flags |=
1350                             (CSUM_IP_CHECKED | CSUM_IP_VALID);
1351                         rxr->hn_csum_ip++;
1352                 }
1353
1354                 /* TCP/UDP csum offload */
1355                 if ((csum_info->receive.tcp_csum_succeeded ||
1356                      csum_info->receive.udp_csum_succeeded) && do_csum) {
1357                         m_new->m_pkthdr.csum_flags |=
1358                             (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
1359                         m_new->m_pkthdr.csum_data = 0xffff;
1360                         if (csum_info->receive.tcp_csum_succeeded)
1361                                 rxr->hn_csum_tcp++;
1362                         else
1363                                 rxr->hn_csum_udp++;
1364                 }
1365
1366                 if (csum_info->receive.ip_csum_succeeded &&
1367                     csum_info->receive.tcp_csum_succeeded)
1368                         do_lro = 1;
1369         } else {
1370                 const struct ether_header *eh;
1371                 uint16_t etype;
1372                 int hoff;
1373
1374                 hoff = sizeof(*eh);
1375                 if (m_new->m_len < hoff)
1376                         goto skip;
1377                 eh = mtod(m_new, struct ether_header *);
1378                 etype = ntohs(eh->ether_type);
1379                 if (etype == ETHERTYPE_VLAN) {
1380                         const struct ether_vlan_header *evl;
1381
1382                         hoff = sizeof(*evl);
1383                         if (m_new->m_len < hoff)
1384                                 goto skip;
1385                         evl = mtod(m_new, struct ether_vlan_header *);
1386                         etype = ntohs(evl->evl_proto);
1387                 }
1388
1389                 if (etype == ETHERTYPE_IP) {
1390                         int pr;
1391
1392                         pr = hn_check_iplen(m_new, hoff);
1393                         if (pr == IPPROTO_TCP) {
1394                                 if (do_csum &&
1395                                     (rxr->hn_trust_hcsum &
1396                                      HN_TRUST_HCSUM_TCP)) {
1397                                         rxr->hn_csum_trusted++;
1398                                         m_new->m_pkthdr.csum_flags |=
1399                                            (CSUM_IP_CHECKED | CSUM_IP_VALID |
1400                                             CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
1401                                         m_new->m_pkthdr.csum_data = 0xffff;
1402                                 }
1403                                 /* Rely on SW csum verification though... */
1404                                 do_lro = 1;
1405                         } else if (pr == IPPROTO_UDP) {
1406                                 if (do_csum &&
1407                                     (rxr->hn_trust_hcsum &
1408                                      HN_TRUST_HCSUM_UDP)) {
1409                                         rxr->hn_csum_trusted++;
1410                                         m_new->m_pkthdr.csum_flags |=
1411                                            (CSUM_IP_CHECKED | CSUM_IP_VALID |
1412                                             CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
1413                                         m_new->m_pkthdr.csum_data = 0xffff;
1414                                 }
1415                         } else if (pr != IPPROTO_DONE && do_csum &&
1416                             (rxr->hn_trust_hcsum & HN_TRUST_HCSUM_IP)) {
1417                                 rxr->hn_csum_trusted++;
1418                                 m_new->m_pkthdr.csum_flags |=
1419                                     (CSUM_IP_CHECKED | CSUM_IP_VALID);
1420                         }
1421                 }
1422         }
1423 skip:
1424         if ((packet->vlan_tci != 0) &&
1425             (ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0) {
1426                 m_new->m_pkthdr.ether_vtag = packet->vlan_tci;
1427                 m_new->m_flags |= M_VLANTAG;
1428         }
1429
1430         m_new->m_pkthdr.flowid = rxr->hn_rx_idx;
1431         M_HASHTYPE_SET(m_new, M_HASHTYPE_OPAQUE);
1432
1433         /*
1434          * Note:  Moved RX completion back to hv_nv_on_receive() so all
1435          * messages (not just data messages) will trigger a response.
1436          */
1437
1438         if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
1439         rxr->hn_pkts++;
1440
1441         if ((ifp->if_capenable & IFCAP_LRO) && do_lro) {
1442 #if defined(INET) || defined(INET6)
1443                 struct lro_ctrl *lro = &rxr->hn_lro;
1444
1445                 if (lro->lro_cnt) {
1446                         rxr->hn_lro_tried++;
1447                         if (tcp_lro_rx(lro, m_new, 0) == 0) {
1448                                 /* DONE! */
1449                                 return 0;
1450                         }
1451                 }
1452 #endif
1453         }
1454
1455         /* We're not holding the lock here, so don't release it */
1456         (*ifp->if_input)(ifp, m_new);
1457
1458         return (0);
1459 }
1460
1461 /*
1462  * Rules for using sc->temp_unusable:
1463  * 1.  sc->temp_unusable can only be read or written while holding NV_LOCK()
1464  * 2.  code reading sc->temp_unusable under NV_LOCK(), and finding 
1465  *     sc->temp_unusable set, must release NV_LOCK() and exit
1466  * 3.  to retain exclusive control of the interface,
1467  *     sc->temp_unusable must be set by code before releasing NV_LOCK()
1468  * 4.  only code setting sc->temp_unusable can clear sc->temp_unusable
1469  * 5.  code setting sc->temp_unusable must eventually clear sc->temp_unusable
1470  */
1471
1472 /*
1473  * Standard ioctl entry point.  Called when the user wants to configure
1474  * the interface.
1475  */
1476 static int
1477 hn_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1478 {
1479         hn_softc_t *sc = ifp->if_softc;
1480         struct ifreq *ifr = (struct ifreq *)data;
1481 #ifdef INET
1482         struct ifaddr *ifa = (struct ifaddr *)data;
1483 #endif
1484         netvsc_device_info device_info;
1485         struct hv_device *hn_dev;
1486         int mask, error = 0;
1487         int retry_cnt = 500;
1488         
1489         switch(cmd) {
1490
1491         case SIOCSIFADDR:
1492 #ifdef INET
1493                 if (ifa->ifa_addr->sa_family == AF_INET) {
1494                         ifp->if_flags |= IFF_UP;
1495                         if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
1496                                 hn_ifinit(sc);
1497                         arp_ifinit(ifp, ifa);
1498                 } else
1499 #endif
1500                 error = ether_ioctl(ifp, cmd, data);
1501                 break;
1502         case SIOCSIFMTU:
1503                 hn_dev = vmbus_get_devctx(sc->hn_dev);
1504
1505                 /* Check MTU value change */
1506                 if (ifp->if_mtu == ifr->ifr_mtu)
1507                         break;
1508
1509                 if (ifr->ifr_mtu > NETVSC_MAX_CONFIGURABLE_MTU) {
1510                         error = EINVAL;
1511                         break;
1512                 }
1513
1514                 /* Obtain and record requested MTU */
1515                 ifp->if_mtu = ifr->ifr_mtu;
1516
1517 #if __FreeBSD_version >= 1100099
1518                 /*
1519                  * Make sure that LRO aggregation length limit is still
1520                  * valid, after the MTU change.
1521                  */
1522                 NV_LOCK(sc);
1523                 if (sc->hn_rx_ring[0].hn_lro.lro_length_lim <
1524                     HN_LRO_LENLIM_MIN(ifp))
1525                         hn_set_lro_lenlim(sc, HN_LRO_LENLIM_MIN(ifp));
1526                 NV_UNLOCK(sc);
1527 #endif
1528
1529                 do {
1530                         NV_LOCK(sc);
1531                         if (!sc->temp_unusable) {
1532                                 sc->temp_unusable = TRUE;
1533                                 retry_cnt = -1;
1534                         }
1535                         NV_UNLOCK(sc);
1536                         if (retry_cnt > 0) {
1537                                 retry_cnt--;
1538                                 DELAY(5 * 1000);
1539                         }
1540                 } while (retry_cnt > 0);
1541
1542                 if (retry_cnt == 0) {
1543                         error = EINVAL;
1544                         break;
1545                 }
1546
1547                 /* We must remove and add back the device to cause the new
1548                  * MTU to take effect.  This includes tearing down, but not
1549                  * deleting the channel, then bringing it back up.
1550                  */
1551                 error = hv_rf_on_device_remove(hn_dev, HV_RF_NV_RETAIN_CHANNEL);
1552                 if (error) {
1553                         NV_LOCK(sc);
1554                         sc->temp_unusable = FALSE;
1555                         NV_UNLOCK(sc);
1556                         break;
1557                 }
1558                 error = hv_rf_on_device_add(hn_dev, &device_info,
1559                     sc->hn_rx_ring_inuse);
1560                 if (error) {
1561                         NV_LOCK(sc);
1562                         sc->temp_unusable = FALSE;
1563                         NV_UNLOCK(sc);
1564                         break;
1565                 }
1566
1567                 sc->hn_tx_chimney_max = sc->net_dev->send_section_size;
1568                 if (sc->hn_tx_ring[0].hn_tx_chimney_size >
1569                     sc->hn_tx_chimney_max)
1570                         hn_set_tx_chimney_size(sc, sc->hn_tx_chimney_max);
1571
1572                 hn_ifinit_locked(sc);
1573
1574                 NV_LOCK(sc);
1575                 sc->temp_unusable = FALSE;
1576                 NV_UNLOCK(sc);
1577                 break;
1578         case SIOCSIFFLAGS:
1579                 do {
1580                        NV_LOCK(sc);
1581                        if (!sc->temp_unusable) {
1582                                sc->temp_unusable = TRUE;
1583                                retry_cnt = -1;
1584                        }
1585                        NV_UNLOCK(sc);
1586                        if (retry_cnt > 0) {
1587                                 retry_cnt--;
1588                                 DELAY(5 * 1000);
1589                        }
1590                 } while (retry_cnt > 0);
1591
1592                 if (retry_cnt == 0) {
1593                        error = EINVAL;
1594                        break;
1595                 }
1596
1597                 if (ifp->if_flags & IFF_UP) {
1598                         /*
1599                          * If only the state of the PROMISC flag changed,
1600                          * then just use the 'set promisc mode' command
1601                          * instead of reinitializing the entire NIC. Doing
1602                          * a full re-init means reloading the firmware and
1603                          * waiting for it to start up, which may take a
1604                          * second or two.
1605                          */
1606 #ifdef notyet
1607                         /* Fixme:  Promiscuous mode? */
1608                         if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
1609                             ifp->if_flags & IFF_PROMISC &&
1610                             !(sc->hn_if_flags & IFF_PROMISC)) {
1611                                 /* do something here for Hyper-V */
1612                         } else if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
1613                             !(ifp->if_flags & IFF_PROMISC) &&
1614                             sc->hn_if_flags & IFF_PROMISC) {
1615                                 /* do something here for Hyper-V */
1616                         } else
1617 #endif
1618                                 hn_ifinit_locked(sc);
1619                 } else {
1620                         if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1621                                 hn_stop(sc);
1622                         }
1623                 }
1624                 NV_LOCK(sc);
1625                 sc->temp_unusable = FALSE;
1626                 NV_UNLOCK(sc);
1627                 sc->hn_if_flags = ifp->if_flags;
1628                 error = 0;
1629                 break;
1630         case SIOCSIFCAP:
1631                 NV_LOCK(sc);
1632
1633                 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1634                 if (mask & IFCAP_TXCSUM) {
1635                         ifp->if_capenable ^= IFCAP_TXCSUM;
1636                         if (ifp->if_capenable & IFCAP_TXCSUM) {
1637                                 ifp->if_hwassist |=
1638                                     sc->hn_tx_ring[0].hn_csum_assist;
1639                         } else {
1640                                 ifp->if_hwassist &=
1641                                     ~sc->hn_tx_ring[0].hn_csum_assist;
1642                         }
1643                 }
1644
1645                 if (mask & IFCAP_RXCSUM)
1646                         ifp->if_capenable ^= IFCAP_RXCSUM;
1647
1648                 if (mask & IFCAP_LRO)
1649                         ifp->if_capenable ^= IFCAP_LRO;
1650
1651                 if (mask & IFCAP_TSO4) {
1652                         ifp->if_capenable ^= IFCAP_TSO4;
1653                         if (ifp->if_capenable & IFCAP_TSO4)
1654                                 ifp->if_hwassist |= CSUM_IP_TSO;
1655                         else
1656                                 ifp->if_hwassist &= ~CSUM_IP_TSO;
1657                 }
1658
1659                 if (mask & IFCAP_TSO6) {
1660                         ifp->if_capenable ^= IFCAP_TSO6;
1661                         if (ifp->if_capenable & IFCAP_TSO6)
1662                                 ifp->if_hwassist |= CSUM_IP6_TSO;
1663                         else
1664                                 ifp->if_hwassist &= ~CSUM_IP6_TSO;
1665                 }
1666
1667                 NV_UNLOCK(sc);
1668                 error = 0;
1669                 break;
1670         case SIOCADDMULTI:
1671         case SIOCDELMULTI:
1672 #ifdef notyet
1673                 /* Fixme:  Multicast mode? */
1674                 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1675                         NV_LOCK(sc);
1676                         netvsc_setmulti(sc);
1677                         NV_UNLOCK(sc);
1678                         error = 0;
1679                 }
1680 #endif
1681                 error = EINVAL;
1682                 break;
1683         case SIOCSIFMEDIA:
1684         case SIOCGIFMEDIA:
1685                 error = ifmedia_ioctl(ifp, ifr, &sc->hn_media, cmd);
1686                 break;
1687         default:
1688                 error = ether_ioctl(ifp, cmd, data);
1689                 break;
1690         }
1691
1692         return (error);
1693 }
1694
1695 /*
1696  *
1697  */
1698 static void
1699 hn_stop(hn_softc_t *sc)
1700 {
1701         struct ifnet *ifp;
1702         int ret, i;
1703         struct hv_device *device_ctx = vmbus_get_devctx(sc->hn_dev);
1704
1705         ifp = sc->hn_ifp;
1706
1707         if (bootverbose)
1708                 printf(" Closing Device ...\n");
1709
1710         atomic_clear_int(&ifp->if_drv_flags,
1711             (IFF_DRV_RUNNING | IFF_DRV_OACTIVE));
1712         for (i = 0; i < sc->hn_tx_ring_inuse; ++i)
1713                 sc->hn_tx_ring[i].hn_oactive = 0;
1714
1715         if_link_state_change(ifp, LINK_STATE_DOWN);
1716         sc->hn_initdone = 0;
1717
1718         ret = hv_rf_on_close(device_ctx);
1719 }
1720
1721 /*
1722  * FreeBSD transmit entry point
1723  */
1724 static void
1725 hn_start(struct ifnet *ifp)
1726 {
1727         struct hn_softc *sc = ifp->if_softc;
1728         struct hn_tx_ring *txr = &sc->hn_tx_ring[0];
1729
1730         if (txr->hn_sched_tx)
1731                 goto do_sched;
1732
1733         if (mtx_trylock(&txr->hn_tx_lock)) {
1734                 int sched;
1735
1736                 sched = hn_start_locked(txr, txr->hn_direct_tx_size);
1737                 mtx_unlock(&txr->hn_tx_lock);
1738                 if (!sched)
1739                         return;
1740         }
1741 do_sched:
1742         taskqueue_enqueue(txr->hn_tx_taskq, &txr->hn_tx_task);
1743 }
1744
1745 static void
1746 hn_start_txeof(struct hn_tx_ring *txr)
1747 {
1748         struct hn_softc *sc = txr->hn_sc;
1749         struct ifnet *ifp = sc->hn_ifp;
1750
1751         KASSERT(txr == &sc->hn_tx_ring[0], ("not the first TX ring"));
1752
1753         if (txr->hn_sched_tx)
1754                 goto do_sched;
1755
1756         if (mtx_trylock(&txr->hn_tx_lock)) {
1757                 int sched;
1758
1759                 atomic_clear_int(&ifp->if_drv_flags, IFF_DRV_OACTIVE);
1760                 sched = hn_start_locked(txr, txr->hn_direct_tx_size);
1761                 mtx_unlock(&txr->hn_tx_lock);
1762                 if (sched) {
1763                         taskqueue_enqueue(txr->hn_tx_taskq,
1764                             &txr->hn_tx_task);
1765                 }
1766         } else {
1767 do_sched:
1768                 /*
1769                  * Release the OACTIVE earlier, with the hope, that
1770                  * others could catch up.  The task will clear the
1771                  * flag again with the hn_tx_lock to avoid possible
1772                  * races.
1773                  */
1774                 atomic_clear_int(&ifp->if_drv_flags, IFF_DRV_OACTIVE);
1775                 taskqueue_enqueue(txr->hn_tx_taskq, &txr->hn_txeof_task);
1776         }
1777 }
1778
1779 /*
1780  *
1781  */
1782 static void
1783 hn_ifinit_locked(hn_softc_t *sc)
1784 {
1785         struct ifnet *ifp;
1786         struct hv_device *device_ctx = vmbus_get_devctx(sc->hn_dev);
1787         int ret, i;
1788
1789         ifp = sc->hn_ifp;
1790
1791         if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1792                 return;
1793         }
1794
1795         hv_promisc_mode = 1;
1796
1797         ret = hv_rf_on_open(device_ctx);
1798         if (ret != 0) {
1799                 return;
1800         } else {
1801                 sc->hn_initdone = 1;
1802         }
1803
1804         atomic_clear_int(&ifp->if_drv_flags, IFF_DRV_OACTIVE);
1805         for (i = 0; i < sc->hn_tx_ring_inuse; ++i)
1806                 sc->hn_tx_ring[i].hn_oactive = 0;
1807
1808         atomic_set_int(&ifp->if_drv_flags, IFF_DRV_RUNNING);
1809         if_link_state_change(ifp, LINK_STATE_UP);
1810 }
1811
1812 /*
1813  *
1814  */
1815 static void
1816 hn_ifinit(void *xsc)
1817 {
1818         hn_softc_t *sc = xsc;
1819
1820         NV_LOCK(sc);
1821         if (sc->temp_unusable) {
1822                 NV_UNLOCK(sc);
1823                 return;
1824         }
1825         sc->temp_unusable = TRUE;
1826         NV_UNLOCK(sc);
1827
1828         hn_ifinit_locked(sc);
1829
1830         NV_LOCK(sc);
1831         sc->temp_unusable = FALSE;
1832         NV_UNLOCK(sc);
1833 }
1834
1835 #ifdef LATER
1836 /*
1837  *
1838  */
1839 static void
1840 hn_watchdog(struct ifnet *ifp)
1841 {
1842         hn_softc_t *sc;
1843         sc = ifp->if_softc;
1844
1845         printf("hn%d: watchdog timeout -- resetting\n", sc->hn_unit);
1846         hn_ifinit(sc);    /*???*/
1847         if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1848 }
1849 #endif
1850
1851 #if __FreeBSD_version >= 1100099
1852
1853 static int
1854 hn_lro_lenlim_sysctl(SYSCTL_HANDLER_ARGS)
1855 {
1856         struct hn_softc *sc = arg1;
1857         unsigned int lenlim;
1858         int error;
1859
1860         lenlim = sc->hn_rx_ring[0].hn_lro.lro_length_lim;
1861         error = sysctl_handle_int(oidp, &lenlim, 0, req);
1862         if (error || req->newptr == NULL)
1863                 return error;
1864
1865         if (lenlim < HN_LRO_LENLIM_MIN(sc->hn_ifp) ||
1866             lenlim > TCP_LRO_LENGTH_MAX)
1867                 return EINVAL;
1868
1869         NV_LOCK(sc);
1870         hn_set_lro_lenlim(sc, lenlim);
1871         NV_UNLOCK(sc);
1872         return 0;
1873 }
1874
1875 static int
1876 hn_lro_ackcnt_sysctl(SYSCTL_HANDLER_ARGS)
1877 {
1878         struct hn_softc *sc = arg1;
1879         int ackcnt, error, i;
1880
1881         /*
1882          * lro_ackcnt_lim is append count limit,
1883          * +1 to turn it into aggregation limit.
1884          */
1885         ackcnt = sc->hn_rx_ring[0].hn_lro.lro_ackcnt_lim + 1;
1886         error = sysctl_handle_int(oidp, &ackcnt, 0, req);
1887         if (error || req->newptr == NULL)
1888                 return error;
1889
1890         if (ackcnt < 2 || ackcnt > (TCP_LRO_ACKCNT_MAX + 1))
1891                 return EINVAL;
1892
1893         /*
1894          * Convert aggregation limit back to append
1895          * count limit.
1896          */
1897         --ackcnt;
1898         NV_LOCK(sc);
1899         for (i = 0; i < sc->hn_rx_ring_inuse; ++i)
1900                 sc->hn_rx_ring[i].hn_lro.lro_ackcnt_lim = ackcnt;
1901         NV_UNLOCK(sc);
1902         return 0;
1903 }
1904
1905 #endif
1906
1907 static int
1908 hn_trust_hcsum_sysctl(SYSCTL_HANDLER_ARGS)
1909 {
1910         struct hn_softc *sc = arg1;
1911         int hcsum = arg2;
1912         int on, error, i;
1913
1914         on = 0;
1915         if (sc->hn_rx_ring[0].hn_trust_hcsum & hcsum)
1916                 on = 1;
1917
1918         error = sysctl_handle_int(oidp, &on, 0, req);
1919         if (error || req->newptr == NULL)
1920                 return error;
1921
1922         NV_LOCK(sc);
1923         for (i = 0; i < sc->hn_rx_ring_inuse; ++i) {
1924                 struct hn_rx_ring *rxr = &sc->hn_rx_ring[i];
1925
1926                 if (on)
1927                         rxr->hn_trust_hcsum |= hcsum;
1928                 else
1929                         rxr->hn_trust_hcsum &= ~hcsum;
1930         }
1931         NV_UNLOCK(sc);
1932         return 0;
1933 }
1934
1935 static int
1936 hn_tx_chimney_size_sysctl(SYSCTL_HANDLER_ARGS)
1937 {
1938         struct hn_softc *sc = arg1;
1939         int chimney_size, error;
1940
1941         chimney_size = sc->hn_tx_ring[0].hn_tx_chimney_size;
1942         error = sysctl_handle_int(oidp, &chimney_size, 0, req);
1943         if (error || req->newptr == NULL)
1944                 return error;
1945
1946         if (chimney_size > sc->hn_tx_chimney_max || chimney_size <= 0)
1947                 return EINVAL;
1948
1949         hn_set_tx_chimney_size(sc, chimney_size);
1950         return 0;
1951 }
1952
1953 static int
1954 hn_rx_stat_ulong_sysctl(SYSCTL_HANDLER_ARGS)
1955 {
1956         struct hn_softc *sc = arg1;
1957         int ofs = arg2, i, error;
1958         struct hn_rx_ring *rxr;
1959         u_long stat;
1960
1961         stat = 0;
1962         for (i = 0; i < sc->hn_rx_ring_inuse; ++i) {
1963                 rxr = &sc->hn_rx_ring[i];
1964                 stat += *((u_long *)((uint8_t *)rxr + ofs));
1965         }
1966
1967         error = sysctl_handle_long(oidp, &stat, 0, req);
1968         if (error || req->newptr == NULL)
1969                 return error;
1970
1971         /* Zero out this stat. */
1972         for (i = 0; i < sc->hn_rx_ring_inuse; ++i) {
1973                 rxr = &sc->hn_rx_ring[i];
1974                 *((u_long *)((uint8_t *)rxr + ofs)) = 0;
1975         }
1976         return 0;
1977 }
1978
1979 static int
1980 hn_rx_stat_u64_sysctl(SYSCTL_HANDLER_ARGS)
1981 {
1982         struct hn_softc *sc = arg1;
1983         int ofs = arg2, i, error;
1984         struct hn_rx_ring *rxr;
1985         uint64_t stat;
1986
1987         stat = 0;
1988         for (i = 0; i < sc->hn_rx_ring_inuse; ++i) {
1989                 rxr = &sc->hn_rx_ring[i];
1990                 stat += *((uint64_t *)((uint8_t *)rxr + ofs));
1991         }
1992
1993         error = sysctl_handle_64(oidp, &stat, 0, req);
1994         if (error || req->newptr == NULL)
1995                 return error;
1996
1997         /* Zero out this stat. */
1998         for (i = 0; i < sc->hn_rx_ring_inuse; ++i) {
1999                 rxr = &sc->hn_rx_ring[i];
2000                 *((uint64_t *)((uint8_t *)rxr + ofs)) = 0;
2001         }
2002         return 0;
2003 }
2004
2005 static int
2006 hn_tx_stat_ulong_sysctl(SYSCTL_HANDLER_ARGS)
2007 {
2008         struct hn_softc *sc = arg1;
2009         int ofs = arg2, i, error;
2010         struct hn_tx_ring *txr;
2011         u_long stat;
2012
2013         stat = 0;
2014         for (i = 0; i < sc->hn_tx_ring_inuse; ++i) {
2015                 txr = &sc->hn_tx_ring[i];
2016                 stat += *((u_long *)((uint8_t *)txr + ofs));
2017         }
2018
2019         error = sysctl_handle_long(oidp, &stat, 0, req);
2020         if (error || req->newptr == NULL)
2021                 return error;
2022
2023         /* Zero out this stat. */
2024         for (i = 0; i < sc->hn_tx_ring_inuse; ++i) {
2025                 txr = &sc->hn_tx_ring[i];
2026                 *((u_long *)((uint8_t *)txr + ofs)) = 0;
2027         }
2028         return 0;
2029 }
2030
2031 static int
2032 hn_tx_conf_int_sysctl(SYSCTL_HANDLER_ARGS)
2033 {
2034         struct hn_softc *sc = arg1;
2035         int ofs = arg2, i, error, conf;
2036         struct hn_tx_ring *txr;
2037
2038         txr = &sc->hn_tx_ring[0];
2039         conf = *((int *)((uint8_t *)txr + ofs));
2040
2041         error = sysctl_handle_int(oidp, &conf, 0, req);
2042         if (error || req->newptr == NULL)
2043                 return error;
2044
2045         NV_LOCK(sc);
2046         for (i = 0; i < sc->hn_tx_ring_inuse; ++i) {
2047                 txr = &sc->hn_tx_ring[i];
2048                 *((int *)((uint8_t *)txr + ofs)) = conf;
2049         }
2050         NV_UNLOCK(sc);
2051
2052         return 0;
2053 }
2054
2055 static int
2056 hn_check_iplen(const struct mbuf *m, int hoff)
2057 {
2058         const struct ip *ip;
2059         int len, iphlen, iplen;
2060         const struct tcphdr *th;
2061         int thoff;                              /* TCP data offset */
2062
2063         len = hoff + sizeof(struct ip);
2064
2065         /* The packet must be at least the size of an IP header. */
2066         if (m->m_pkthdr.len < len)
2067                 return IPPROTO_DONE;
2068
2069         /* The fixed IP header must reside completely in the first mbuf. */
2070         if (m->m_len < len)
2071                 return IPPROTO_DONE;
2072
2073         ip = mtodo(m, hoff);
2074
2075         /* Bound check the packet's stated IP header length. */
2076         iphlen = ip->ip_hl << 2;
2077         if (iphlen < sizeof(struct ip))         /* minimum header length */
2078                 return IPPROTO_DONE;
2079
2080         /* The full IP header must reside completely in the one mbuf. */
2081         if (m->m_len < hoff + iphlen)
2082                 return IPPROTO_DONE;
2083
2084         iplen = ntohs(ip->ip_len);
2085
2086         /*
2087          * Check that the amount of data in the buffers is as
2088          * at least much as the IP header would have us expect.
2089          */
2090         if (m->m_pkthdr.len < hoff + iplen)
2091                 return IPPROTO_DONE;
2092
2093         /*
2094          * Ignore IP fragments.
2095          */
2096         if (ntohs(ip->ip_off) & (IP_OFFMASK | IP_MF))
2097                 return IPPROTO_DONE;
2098
2099         /*
2100          * The TCP/IP or UDP/IP header must be entirely contained within
2101          * the first fragment of a packet.
2102          */
2103         switch (ip->ip_p) {
2104         case IPPROTO_TCP:
2105                 if (iplen < iphlen + sizeof(struct tcphdr))
2106                         return IPPROTO_DONE;
2107                 if (m->m_len < hoff + iphlen + sizeof(struct tcphdr))
2108                         return IPPROTO_DONE;
2109                 th = (const struct tcphdr *)((const uint8_t *)ip + iphlen);
2110                 thoff = th->th_off << 2;
2111                 if (thoff < sizeof(struct tcphdr) || thoff + iphlen > iplen)
2112                         return IPPROTO_DONE;
2113                 if (m->m_len < hoff + iphlen + thoff)
2114                         return IPPROTO_DONE;
2115                 break;
2116         case IPPROTO_UDP:
2117                 if (iplen < iphlen + sizeof(struct udphdr))
2118                         return IPPROTO_DONE;
2119                 if (m->m_len < hoff + iphlen + sizeof(struct udphdr))
2120                         return IPPROTO_DONE;
2121                 break;
2122         default:
2123                 if (iplen < iphlen)
2124                         return IPPROTO_DONE;
2125                 break;
2126         }
2127         return ip->ip_p;
2128 }
2129
2130 static void
2131 hn_dma_map_paddr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
2132 {
2133         bus_addr_t *paddr = arg;
2134
2135         if (error)
2136                 return;
2137
2138         KASSERT(nseg == 1, ("too many segments %d!", nseg));
2139         *paddr = segs->ds_addr;
2140 }
2141
2142 static void
2143 hn_create_rx_data(struct hn_softc *sc, int ring_cnt)
2144 {
2145         struct sysctl_oid_list *child;
2146         struct sysctl_ctx_list *ctx;
2147         device_t dev = sc->hn_dev;
2148 #if defined(INET) || defined(INET6)
2149 #if __FreeBSD_version >= 1100095
2150         int lroent_cnt;
2151 #endif
2152 #endif
2153         int i;
2154
2155         sc->hn_rx_ring_cnt = ring_cnt;
2156         sc->hn_rx_ring_inuse = sc->hn_rx_ring_cnt;
2157
2158         sc->hn_rx_ring = malloc(sizeof(struct hn_rx_ring) * sc->hn_rx_ring_cnt,
2159             M_NETVSC, M_WAITOK | M_ZERO);
2160
2161 #if defined(INET) || defined(INET6)
2162 #if __FreeBSD_version >= 1100095
2163         lroent_cnt = hn_lro_entry_count;
2164         if (lroent_cnt < TCP_LRO_ENTRIES)
2165                 lroent_cnt = TCP_LRO_ENTRIES;
2166         device_printf(dev, "LRO: entry count %d\n", lroent_cnt);
2167 #endif
2168 #endif  /* INET || INET6 */
2169
2170         ctx = device_get_sysctl_ctx(dev);
2171         child = SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
2172
2173         /* Create dev.hn.UNIT.rx sysctl tree */
2174         sc->hn_rx_sysctl_tree = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, "rx",
2175             CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
2176
2177         for (i = 0; i < sc->hn_rx_ring_cnt; ++i) {
2178                 struct hn_rx_ring *rxr = &sc->hn_rx_ring[i];
2179
2180                 if (hn_trust_hosttcp)
2181                         rxr->hn_trust_hcsum |= HN_TRUST_HCSUM_TCP;
2182                 if (hn_trust_hostudp)
2183                         rxr->hn_trust_hcsum |= HN_TRUST_HCSUM_UDP;
2184                 if (hn_trust_hostip)
2185                         rxr->hn_trust_hcsum |= HN_TRUST_HCSUM_IP;
2186                 rxr->hn_ifp = sc->hn_ifp;
2187                 rxr->hn_rx_idx = i;
2188
2189                 /*
2190                  * Initialize LRO.
2191                  */
2192 #if defined(INET) || defined(INET6)
2193 #if __FreeBSD_version >= 1100095
2194                 tcp_lro_init_args(&rxr->hn_lro, sc->hn_ifp, lroent_cnt, 0);
2195 #else
2196                 tcp_lro_init(&rxr->hn_lro);
2197                 rxr->hn_lro.ifp = sc->hn_ifp;
2198 #endif
2199 #if __FreeBSD_version >= 1100099
2200                 rxr->hn_lro.lro_length_lim = HN_LRO_LENLIM_DEF;
2201                 rxr->hn_lro.lro_ackcnt_lim = HN_LRO_ACKCNT_DEF;
2202 #endif
2203 #endif  /* INET || INET6 */
2204
2205                 if (sc->hn_rx_sysctl_tree != NULL) {
2206                         char name[16];
2207
2208                         /*
2209                          * Create per RX ring sysctl tree:
2210                          * dev.hn.UNIT.rx.RINGID
2211                          */
2212                         snprintf(name, sizeof(name), "%d", i);
2213                         rxr->hn_rx_sysctl_tree = SYSCTL_ADD_NODE(ctx,
2214                             SYSCTL_CHILDREN(sc->hn_rx_sysctl_tree),
2215                             OID_AUTO, name, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
2216
2217                         if (rxr->hn_rx_sysctl_tree != NULL) {
2218                                 SYSCTL_ADD_ULONG(ctx,
2219                                     SYSCTL_CHILDREN(rxr->hn_rx_sysctl_tree),
2220                                     OID_AUTO, "packets", CTLFLAG_RW,
2221                                     &rxr->hn_pkts, "# of packets received");
2222                         }
2223                 }
2224         }
2225
2226         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "lro_queued",
2227             CTLTYPE_U64 | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
2228             __offsetof(struct hn_rx_ring, hn_lro.lro_queued),
2229             hn_rx_stat_u64_sysctl, "LU", "LRO queued");
2230         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "lro_flushed",
2231             CTLTYPE_U64 | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
2232             __offsetof(struct hn_rx_ring, hn_lro.lro_flushed),
2233             hn_rx_stat_u64_sysctl, "LU", "LRO flushed");
2234         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "lro_tried",
2235             CTLTYPE_ULONG | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
2236             __offsetof(struct hn_rx_ring, hn_lro_tried),
2237             hn_rx_stat_ulong_sysctl, "LU", "# of LRO tries");
2238 #if __FreeBSD_version >= 1100099
2239         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "lro_length_lim",
2240             CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, 0,
2241             hn_lro_lenlim_sysctl, "IU",
2242             "Max # of data bytes to be aggregated by LRO");
2243         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "lro_ackcnt_lim",
2244             CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, 0,
2245             hn_lro_ackcnt_sysctl, "I",
2246             "Max # of ACKs to be aggregated by LRO");
2247 #endif
2248         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "trust_hosttcp",
2249             CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, HN_TRUST_HCSUM_TCP,
2250             hn_trust_hcsum_sysctl, "I",
2251             "Trust tcp segement verification on host side, "
2252             "when csum info is missing");
2253         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "trust_hostudp",
2254             CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, HN_TRUST_HCSUM_UDP,
2255             hn_trust_hcsum_sysctl, "I",
2256             "Trust udp datagram verification on host side, "
2257             "when csum info is missing");
2258         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "trust_hostip",
2259             CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, HN_TRUST_HCSUM_IP,
2260             hn_trust_hcsum_sysctl, "I",
2261             "Trust ip packet verification on host side, "
2262             "when csum info is missing");
2263         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "csum_ip",
2264             CTLTYPE_ULONG | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
2265             __offsetof(struct hn_rx_ring, hn_csum_ip),
2266             hn_rx_stat_ulong_sysctl, "LU", "RXCSUM IP");
2267         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "csum_tcp",
2268             CTLTYPE_ULONG | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
2269             __offsetof(struct hn_rx_ring, hn_csum_tcp),
2270             hn_rx_stat_ulong_sysctl, "LU", "RXCSUM TCP");
2271         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "csum_udp",
2272             CTLTYPE_ULONG | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
2273             __offsetof(struct hn_rx_ring, hn_csum_udp),
2274             hn_rx_stat_ulong_sysctl, "LU", "RXCSUM UDP");
2275         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "csum_trusted",
2276             CTLTYPE_ULONG | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
2277             __offsetof(struct hn_rx_ring, hn_csum_trusted),
2278             hn_rx_stat_ulong_sysctl, "LU",
2279             "# of packets that we trust host's csum verification");
2280         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "small_pkts",
2281             CTLTYPE_ULONG | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
2282             __offsetof(struct hn_rx_ring, hn_small_pkts),
2283             hn_rx_stat_ulong_sysctl, "LU", "# of small packets received");
2284         SYSCTL_ADD_INT(ctx, child, OID_AUTO, "rx_ring_cnt",
2285             CTLFLAG_RD, &sc->hn_rx_ring_cnt, 0, "# created RX rings");
2286         SYSCTL_ADD_INT(ctx, child, OID_AUTO, "rx_ring_inuse",
2287             CTLFLAG_RD, &sc->hn_rx_ring_inuse, 0, "# used RX rings");
2288 }
2289
2290 static void
2291 hn_destroy_rx_data(struct hn_softc *sc)
2292 {
2293 #if defined(INET) || defined(INET6)
2294         int i;
2295 #endif
2296
2297         if (sc->hn_rx_ring_cnt == 0)
2298                 return;
2299
2300 #if defined(INET) || defined(INET6)
2301         for (i = 0; i < sc->hn_rx_ring_cnt; ++i)
2302                 tcp_lro_free(&sc->hn_rx_ring[i].hn_lro);
2303 #endif
2304         free(sc->hn_rx_ring, M_NETVSC);
2305         sc->hn_rx_ring = NULL;
2306
2307         sc->hn_rx_ring_cnt = 0;
2308         sc->hn_rx_ring_inuse = 0;
2309 }
2310
2311 static int
2312 hn_create_tx_ring(struct hn_softc *sc, int id)
2313 {
2314         struct hn_tx_ring *txr = &sc->hn_tx_ring[id];
2315         bus_dma_tag_t parent_dtag;
2316         int error, i;
2317
2318         txr->hn_sc = sc;
2319         txr->hn_tx_idx = id;
2320
2321 #ifndef HN_USE_TXDESC_BUFRING
2322         mtx_init(&txr->hn_txlist_spin, "hn txlist", NULL, MTX_SPIN);
2323 #endif
2324         mtx_init(&txr->hn_tx_lock, "hn tx", NULL, MTX_DEF);
2325
2326         txr->hn_txdesc_cnt = HN_TX_DESC_CNT;
2327         txr->hn_txdesc = malloc(sizeof(struct hn_txdesc) * txr->hn_txdesc_cnt,
2328             M_NETVSC, M_WAITOK | M_ZERO);
2329 #ifndef HN_USE_TXDESC_BUFRING
2330         SLIST_INIT(&txr->hn_txlist);
2331 #else
2332         txr->hn_txdesc_br = buf_ring_alloc(txr->hn_txdesc_cnt, M_NETVSC,
2333             M_WAITOK, &txr->hn_tx_lock);
2334 #endif
2335
2336         txr->hn_tx_taskq = sc->hn_tx_taskq;
2337
2338         if (hn_use_if_start) {
2339                 txr->hn_txeof = hn_start_txeof;
2340                 TASK_INIT(&txr->hn_tx_task, 0, hn_start_taskfunc, txr);
2341                 TASK_INIT(&txr->hn_txeof_task, 0, hn_start_txeof_taskfunc, txr);
2342         } else {
2343                 int br_depth;
2344
2345                 txr->hn_txeof = hn_xmit_txeof;
2346                 TASK_INIT(&txr->hn_tx_task, 0, hn_xmit_taskfunc, txr);
2347                 TASK_INIT(&txr->hn_txeof_task, 0, hn_xmit_txeof_taskfunc, txr);
2348
2349                 br_depth = hn_get_txswq_depth(txr);
2350                 txr->hn_mbuf_br = buf_ring_alloc(br_depth, M_NETVSC,
2351                     M_WAITOK, &txr->hn_tx_lock);
2352         }
2353
2354         txr->hn_direct_tx_size = hn_direct_tx_size;
2355         if (hv_vmbus_protocal_version >= HV_VMBUS_VERSION_WIN8_1)
2356                 txr->hn_csum_assist = HN_CSUM_ASSIST;
2357         else
2358                 txr->hn_csum_assist = HN_CSUM_ASSIST_WIN8;
2359
2360         /*
2361          * Always schedule transmission instead of trying to do direct
2362          * transmission.  This one gives the best performance so far.
2363          */
2364         txr->hn_sched_tx = 1;
2365
2366         parent_dtag = bus_get_dma_tag(sc->hn_dev);
2367
2368         /* DMA tag for RNDIS messages. */
2369         error = bus_dma_tag_create(parent_dtag, /* parent */
2370             HN_RNDIS_MSG_ALIGN,         /* alignment */
2371             HN_RNDIS_MSG_BOUNDARY,      /* boundary */
2372             BUS_SPACE_MAXADDR,          /* lowaddr */
2373             BUS_SPACE_MAXADDR,          /* highaddr */
2374             NULL, NULL,                 /* filter, filterarg */
2375             HN_RNDIS_MSG_LEN,           /* maxsize */
2376             1,                          /* nsegments */
2377             HN_RNDIS_MSG_LEN,           /* maxsegsize */
2378             0,                          /* flags */
2379             NULL,                       /* lockfunc */
2380             NULL,                       /* lockfuncarg */
2381             &txr->hn_tx_rndis_dtag);
2382         if (error) {
2383                 device_printf(sc->hn_dev, "failed to create rndis dmatag\n");
2384                 return error;
2385         }
2386
2387         /* DMA tag for data. */
2388         error = bus_dma_tag_create(parent_dtag, /* parent */
2389             1,                          /* alignment */
2390             HN_TX_DATA_BOUNDARY,        /* boundary */
2391             BUS_SPACE_MAXADDR,          /* lowaddr */
2392             BUS_SPACE_MAXADDR,          /* highaddr */
2393             NULL, NULL,                 /* filter, filterarg */
2394             HN_TX_DATA_MAXSIZE,         /* maxsize */
2395             HN_TX_DATA_SEGCNT_MAX,      /* nsegments */
2396             HN_TX_DATA_SEGSIZE,         /* maxsegsize */
2397             0,                          /* flags */
2398             NULL,                       /* lockfunc */
2399             NULL,                       /* lockfuncarg */
2400             &txr->hn_tx_data_dtag);
2401         if (error) {
2402                 device_printf(sc->hn_dev, "failed to create data dmatag\n");
2403                 return error;
2404         }
2405
2406         for (i = 0; i < txr->hn_txdesc_cnt; ++i) {
2407                 struct hn_txdesc *txd = &txr->hn_txdesc[i];
2408
2409                 txd->txr = txr;
2410
2411                 /*
2412                  * Allocate and load RNDIS messages.
2413                  */
2414                 error = bus_dmamem_alloc(txr->hn_tx_rndis_dtag,
2415                     (void **)&txd->rndis_msg,
2416                     BUS_DMA_WAITOK | BUS_DMA_COHERENT,
2417                     &txd->rndis_msg_dmap);
2418                 if (error) {
2419                         device_printf(sc->hn_dev,
2420                             "failed to allocate rndis_msg, %d\n", i);
2421                         return error;
2422                 }
2423
2424                 error = bus_dmamap_load(txr->hn_tx_rndis_dtag,
2425                     txd->rndis_msg_dmap,
2426                     txd->rndis_msg, HN_RNDIS_MSG_LEN,
2427                     hn_dma_map_paddr, &txd->rndis_msg_paddr,
2428                     BUS_DMA_NOWAIT);
2429                 if (error) {
2430                         device_printf(sc->hn_dev,
2431                             "failed to load rndis_msg, %d\n", i);
2432                         bus_dmamem_free(txr->hn_tx_rndis_dtag,
2433                             txd->rndis_msg, txd->rndis_msg_dmap);
2434                         return error;
2435                 }
2436
2437                 /* DMA map for TX data. */
2438                 error = bus_dmamap_create(txr->hn_tx_data_dtag, 0,
2439                     &txd->data_dmap);
2440                 if (error) {
2441                         device_printf(sc->hn_dev,
2442                             "failed to allocate tx data dmamap\n");
2443                         bus_dmamap_unload(txr->hn_tx_rndis_dtag,
2444                             txd->rndis_msg_dmap);
2445                         bus_dmamem_free(txr->hn_tx_rndis_dtag,
2446                             txd->rndis_msg, txd->rndis_msg_dmap);
2447                         return error;
2448                 }
2449
2450                 /* All set, put it to list */
2451                 txd->flags |= HN_TXD_FLAG_ONLIST;
2452 #ifndef HN_USE_TXDESC_BUFRING
2453                 SLIST_INSERT_HEAD(&txr->hn_txlist, txd, link);
2454 #else
2455                 buf_ring_enqueue(txr->hn_txdesc_br, txd);
2456 #endif
2457         }
2458         txr->hn_txdesc_avail = txr->hn_txdesc_cnt;
2459
2460         if (sc->hn_tx_sysctl_tree != NULL) {
2461                 struct sysctl_oid_list *child;
2462                 struct sysctl_ctx_list *ctx;
2463                 char name[16];
2464
2465                 /*
2466                  * Create per TX ring sysctl tree:
2467                  * dev.hn.UNIT.tx.RINGID
2468                  */
2469                 ctx = device_get_sysctl_ctx(sc->hn_dev);
2470                 child = SYSCTL_CHILDREN(sc->hn_tx_sysctl_tree);
2471
2472                 snprintf(name, sizeof(name), "%d", id);
2473                 txr->hn_tx_sysctl_tree = SYSCTL_ADD_NODE(ctx, child, OID_AUTO,
2474                     name, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
2475
2476                 if (txr->hn_tx_sysctl_tree != NULL) {
2477                         child = SYSCTL_CHILDREN(txr->hn_tx_sysctl_tree);
2478
2479                         SYSCTL_ADD_INT(ctx, child, OID_AUTO, "txdesc_avail",
2480                             CTLFLAG_RD, &txr->hn_txdesc_avail, 0,
2481                             "# of available TX descs");
2482                         if (!hn_use_if_start) {
2483                                 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "oactive",
2484                                     CTLFLAG_RD, &txr->hn_oactive, 0,
2485                                     "over active");
2486                         }
2487                         SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "packets",
2488                             CTLFLAG_RW, &txr->hn_pkts,
2489                             "# of packets transmitted");
2490                 }
2491         }
2492
2493         return 0;
2494 }
2495
2496 static void
2497 hn_txdesc_dmamap_destroy(struct hn_txdesc *txd)
2498 {
2499         struct hn_tx_ring *txr = txd->txr;
2500
2501         KASSERT(txd->m == NULL, ("still has mbuf installed"));
2502         KASSERT((txd->flags & HN_TXD_FLAG_DMAMAP) == 0, ("still dma mapped"));
2503
2504         bus_dmamap_unload(txr->hn_tx_rndis_dtag, txd->rndis_msg_dmap);
2505         bus_dmamem_free(txr->hn_tx_rndis_dtag, txd->rndis_msg,
2506             txd->rndis_msg_dmap);
2507         bus_dmamap_destroy(txr->hn_tx_data_dtag, txd->data_dmap);
2508 }
2509
2510 static void
2511 hn_destroy_tx_ring(struct hn_tx_ring *txr)
2512 {
2513         struct hn_txdesc *txd;
2514
2515         if (txr->hn_txdesc == NULL)
2516                 return;
2517
2518 #ifndef HN_USE_TXDESC_BUFRING
2519         while ((txd = SLIST_FIRST(&txr->hn_txlist)) != NULL) {
2520                 SLIST_REMOVE_HEAD(&txr->hn_txlist, link);
2521                 hn_txdesc_dmamap_destroy(txd);
2522         }
2523 #else
2524         mtx_lock(&txr->hn_tx_lock);
2525         while ((txd = buf_ring_dequeue_sc(txr->hn_txdesc_br)) != NULL)
2526                 hn_txdesc_dmamap_destroy(txd);
2527         mtx_unlock(&txr->hn_tx_lock);
2528 #endif
2529
2530         if (txr->hn_tx_data_dtag != NULL)
2531                 bus_dma_tag_destroy(txr->hn_tx_data_dtag);
2532         if (txr->hn_tx_rndis_dtag != NULL)
2533                 bus_dma_tag_destroy(txr->hn_tx_rndis_dtag);
2534
2535 #ifdef HN_USE_TXDESC_BUFRING
2536         buf_ring_free(txr->hn_txdesc_br, M_NETVSC);
2537 #endif
2538
2539         free(txr->hn_txdesc, M_NETVSC);
2540         txr->hn_txdesc = NULL;
2541
2542         if (txr->hn_mbuf_br != NULL)
2543                 buf_ring_free(txr->hn_mbuf_br, M_NETVSC);
2544
2545 #ifndef HN_USE_TXDESC_BUFRING
2546         mtx_destroy(&txr->hn_txlist_spin);
2547 #endif
2548         mtx_destroy(&txr->hn_tx_lock);
2549 }
2550
2551 static int
2552 hn_create_tx_data(struct hn_softc *sc, int ring_cnt)
2553 {
2554         struct sysctl_oid_list *child;
2555         struct sysctl_ctx_list *ctx;
2556         int i;
2557
2558         sc->hn_tx_ring_cnt = ring_cnt;
2559         sc->hn_tx_ring_inuse = sc->hn_tx_ring_cnt;
2560
2561         sc->hn_tx_ring = malloc(sizeof(struct hn_tx_ring) * sc->hn_tx_ring_cnt,
2562             M_NETVSC, M_WAITOK | M_ZERO);
2563
2564         ctx = device_get_sysctl_ctx(sc->hn_dev);
2565         child = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->hn_dev));
2566
2567         /* Create dev.hn.UNIT.tx sysctl tree */
2568         sc->hn_tx_sysctl_tree = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, "tx",
2569             CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
2570
2571         for (i = 0; i < sc->hn_tx_ring_cnt; ++i) {
2572                 int error;
2573
2574                 error = hn_create_tx_ring(sc, i);
2575                 if (error)
2576                         return error;
2577         }
2578
2579         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "no_txdescs",
2580             CTLTYPE_ULONG | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
2581             __offsetof(struct hn_tx_ring, hn_no_txdescs),
2582             hn_tx_stat_ulong_sysctl, "LU", "# of times short of TX descs");
2583         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "send_failed",
2584             CTLTYPE_ULONG | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
2585             __offsetof(struct hn_tx_ring, hn_send_failed),
2586             hn_tx_stat_ulong_sysctl, "LU", "# of hyper-v sending failure");
2587         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "txdma_failed",
2588             CTLTYPE_ULONG | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
2589             __offsetof(struct hn_tx_ring, hn_txdma_failed),
2590             hn_tx_stat_ulong_sysctl, "LU", "# of TX DMA failure");
2591         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "tx_collapsed",
2592             CTLTYPE_ULONG | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
2593             __offsetof(struct hn_tx_ring, hn_tx_collapsed),
2594             hn_tx_stat_ulong_sysctl, "LU", "# of TX mbuf collapsed");
2595         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "tx_chimney",
2596             CTLTYPE_ULONG | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
2597             __offsetof(struct hn_tx_ring, hn_tx_chimney),
2598             hn_tx_stat_ulong_sysctl, "LU", "# of chimney send");
2599         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "tx_chimney_tried",
2600             CTLTYPE_ULONG | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
2601             __offsetof(struct hn_tx_ring, hn_tx_chimney_tried),
2602             hn_tx_stat_ulong_sysctl, "LU", "# of chimney send tries");
2603         SYSCTL_ADD_INT(ctx, child, OID_AUTO, "txdesc_cnt",
2604             CTLFLAG_RD, &sc->hn_tx_ring[0].hn_txdesc_cnt, 0,
2605             "# of total TX descs");
2606         SYSCTL_ADD_INT(ctx, child, OID_AUTO, "tx_chimney_max",
2607             CTLFLAG_RD, &sc->hn_tx_chimney_max, 0,
2608             "Chimney send packet size upper boundary");
2609         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "tx_chimney_size",
2610             CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, 0,
2611             hn_tx_chimney_size_sysctl,
2612             "I", "Chimney send packet size limit");
2613         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "direct_tx_size",
2614             CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
2615             __offsetof(struct hn_tx_ring, hn_direct_tx_size),
2616             hn_tx_conf_int_sysctl, "I",
2617             "Size of the packet for direct transmission");
2618         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "sched_tx",
2619             CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc,
2620             __offsetof(struct hn_tx_ring, hn_sched_tx),
2621             hn_tx_conf_int_sysctl, "I",
2622             "Always schedule transmission "
2623             "instead of doing direct transmission");
2624         SYSCTL_ADD_INT(ctx, child, OID_AUTO, "tx_ring_cnt",
2625             CTLFLAG_RD, &sc->hn_tx_ring_cnt, 0, "# created TX rings");
2626         SYSCTL_ADD_INT(ctx, child, OID_AUTO, "tx_ring_inuse",
2627             CTLFLAG_RD, &sc->hn_tx_ring_inuse, 0, "# used TX rings");
2628
2629         return 0;
2630 }
2631
2632 static void
2633 hn_set_tx_chimney_size(struct hn_softc *sc, int chimney_size)
2634 {
2635         int i;
2636
2637         NV_LOCK(sc);
2638         for (i = 0; i < sc->hn_tx_ring_inuse; ++i)
2639                 sc->hn_tx_ring[i].hn_tx_chimney_size = chimney_size;
2640         NV_UNLOCK(sc);
2641 }
2642
2643 static void
2644 hn_destroy_tx_data(struct hn_softc *sc)
2645 {
2646         int i;
2647
2648         if (sc->hn_tx_ring_cnt == 0)
2649                 return;
2650
2651         for (i = 0; i < sc->hn_tx_ring_cnt; ++i)
2652                 hn_destroy_tx_ring(&sc->hn_tx_ring[i]);
2653
2654         free(sc->hn_tx_ring, M_NETVSC);
2655         sc->hn_tx_ring = NULL;
2656
2657         sc->hn_tx_ring_cnt = 0;
2658         sc->hn_tx_ring_inuse = 0;
2659 }
2660
2661 static void
2662 hn_start_taskfunc(void *xtxr, int pending __unused)
2663 {
2664         struct hn_tx_ring *txr = xtxr;
2665
2666         mtx_lock(&txr->hn_tx_lock);
2667         hn_start_locked(txr, 0);
2668         mtx_unlock(&txr->hn_tx_lock);
2669 }
2670
2671 static void
2672 hn_start_txeof_taskfunc(void *xtxr, int pending __unused)
2673 {
2674         struct hn_tx_ring *txr = xtxr;
2675
2676         mtx_lock(&txr->hn_tx_lock);
2677         atomic_clear_int(&txr->hn_sc->hn_ifp->if_drv_flags, IFF_DRV_OACTIVE);
2678         hn_start_locked(txr, 0);
2679         mtx_unlock(&txr->hn_tx_lock);
2680 }
2681
2682 static void
2683 hn_stop_tx_tasks(struct hn_softc *sc)
2684 {
2685         int i;
2686
2687         for (i = 0; i < sc->hn_tx_ring_inuse; ++i) {
2688                 struct hn_tx_ring *txr = &sc->hn_tx_ring[i];
2689
2690                 taskqueue_drain(txr->hn_tx_taskq, &txr->hn_tx_task);
2691                 taskqueue_drain(txr->hn_tx_taskq, &txr->hn_txeof_task);
2692         }
2693 }
2694
2695 static int
2696 hn_xmit(struct hn_tx_ring *txr, int len)
2697 {
2698         struct hn_softc *sc = txr->hn_sc;
2699         struct ifnet *ifp = sc->hn_ifp;
2700         struct mbuf *m_head;
2701
2702         mtx_assert(&txr->hn_tx_lock, MA_OWNED);
2703         KASSERT(hn_use_if_start == 0,
2704             ("hn_xmit is called, when if_start is enabled"));
2705
2706         if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || txr->hn_oactive)
2707                 return 0;
2708
2709         while ((m_head = drbr_peek(ifp, txr->hn_mbuf_br)) != NULL) {
2710                 struct hn_txdesc *txd;
2711                 int error;
2712
2713                 if (len > 0 && m_head->m_pkthdr.len > len) {
2714                         /*
2715                          * This sending could be time consuming; let callers
2716                          * dispatch this packet sending (and sending of any
2717                          * following up packets) to tx taskqueue.
2718                          */
2719                         drbr_putback(ifp, txr->hn_mbuf_br, m_head);
2720                         return 1;
2721                 }
2722
2723                 txd = hn_txdesc_get(txr);
2724                 if (txd == NULL) {
2725                         txr->hn_no_txdescs++;
2726                         drbr_putback(ifp, txr->hn_mbuf_br, m_head);
2727                         txr->hn_oactive = 1;
2728                         break;
2729                 }
2730
2731                 error = hn_encap(txr, txd, &m_head);
2732                 if (error) {
2733                         /* Both txd and m_head are freed; discard */
2734                         drbr_advance(ifp, txr->hn_mbuf_br);
2735                         continue;
2736                 }
2737
2738                 error = hn_send_pkt(ifp, txr, txd);
2739                 if (__predict_false(error)) {
2740                         /* txd is freed, but m_head is not */
2741                         drbr_putback(ifp, txr->hn_mbuf_br, m_head);
2742                         txr->hn_oactive = 1;
2743                         break;
2744                 }
2745
2746                 /* Sent */
2747                 drbr_advance(ifp, txr->hn_mbuf_br);
2748         }
2749         return 0;
2750 }
2751
2752 static int
2753 hn_transmit(struct ifnet *ifp, struct mbuf *m)
2754 {
2755         struct hn_softc *sc = ifp->if_softc;
2756         struct hn_tx_ring *txr;
2757         int error, idx = 0;
2758
2759         /*
2760          * Select the TX ring based on flowid
2761          */
2762         if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE)
2763                 idx = m->m_pkthdr.flowid % sc->hn_tx_ring_inuse;
2764         txr = &sc->hn_tx_ring[idx];
2765
2766         error = drbr_enqueue(ifp, txr->hn_mbuf_br, m);
2767         if (error) {
2768                 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
2769                 return error;
2770         }
2771
2772         if (txr->hn_oactive)
2773                 return 0;
2774
2775         if (txr->hn_sched_tx)
2776                 goto do_sched;
2777
2778         if (mtx_trylock(&txr->hn_tx_lock)) {
2779                 int sched;
2780
2781                 sched = hn_xmit(txr, txr->hn_direct_tx_size);
2782                 mtx_unlock(&txr->hn_tx_lock);
2783                 if (!sched)
2784                         return 0;
2785         }
2786 do_sched:
2787         taskqueue_enqueue(txr->hn_tx_taskq, &txr->hn_tx_task);
2788         return 0;
2789 }
2790
2791 static void
2792 hn_xmit_qflush(struct ifnet *ifp)
2793 {
2794         struct hn_softc *sc = ifp->if_softc;
2795         int i;
2796
2797         for (i = 0; i < sc->hn_tx_ring_inuse; ++i) {
2798                 struct hn_tx_ring *txr = &sc->hn_tx_ring[i];
2799                 struct mbuf *m;
2800
2801                 mtx_lock(&txr->hn_tx_lock);
2802                 while ((m = buf_ring_dequeue_sc(txr->hn_mbuf_br)) != NULL)
2803                         m_freem(m);
2804                 mtx_unlock(&txr->hn_tx_lock);
2805         }
2806         if_qflush(ifp);
2807 }
2808
2809 static void
2810 hn_xmit_txeof(struct hn_tx_ring *txr)
2811 {
2812
2813         if (txr->hn_sched_tx)
2814                 goto do_sched;
2815
2816         if (mtx_trylock(&txr->hn_tx_lock)) {
2817                 int sched;
2818
2819                 txr->hn_oactive = 0;
2820                 sched = hn_xmit(txr, txr->hn_direct_tx_size);
2821                 mtx_unlock(&txr->hn_tx_lock);
2822                 if (sched) {
2823                         taskqueue_enqueue(txr->hn_tx_taskq,
2824                             &txr->hn_tx_task);
2825                 }
2826         } else {
2827 do_sched:
2828                 /*
2829                  * Release the oactive earlier, with the hope, that
2830                  * others could catch up.  The task will clear the
2831                  * oactive again with the hn_tx_lock to avoid possible
2832                  * races.
2833                  */
2834                 txr->hn_oactive = 0;
2835                 taskqueue_enqueue(txr->hn_tx_taskq, &txr->hn_txeof_task);
2836         }
2837 }
2838
2839 static void
2840 hn_xmit_taskfunc(void *xtxr, int pending __unused)
2841 {
2842         struct hn_tx_ring *txr = xtxr;
2843
2844         mtx_lock(&txr->hn_tx_lock);
2845         hn_xmit(txr, 0);
2846         mtx_unlock(&txr->hn_tx_lock);
2847 }
2848
2849 static void
2850 hn_xmit_txeof_taskfunc(void *xtxr, int pending __unused)
2851 {
2852         struct hn_tx_ring *txr = xtxr;
2853
2854         mtx_lock(&txr->hn_tx_lock);
2855         txr->hn_oactive = 0;
2856         hn_xmit(txr, 0);
2857         mtx_unlock(&txr->hn_tx_lock);
2858 }
2859
2860 static void
2861 hn_channel_attach(struct hn_softc *sc, struct hv_vmbus_channel *chan)
2862 {
2863         struct hn_rx_ring *rxr;
2864         int idx;
2865
2866         idx = chan->offer_msg.offer.sub_channel_index;
2867
2868         KASSERT(idx >= 0 && idx < sc->hn_rx_ring_inuse,
2869             ("invalid channel index %d, should > 0 && < %d",
2870              idx, sc->hn_rx_ring_inuse));
2871         rxr = &sc->hn_rx_ring[idx];
2872         KASSERT((rxr->hn_rx_flags & HN_RX_FLAG_ATTACHED) == 0,
2873             ("RX ring %d already attached", idx));
2874         rxr->hn_rx_flags |= HN_RX_FLAG_ATTACHED;
2875
2876         chan->hv_chan_rxr = rxr;
2877         if (bootverbose) {
2878                 if_printf(sc->hn_ifp, "link RX ring %d to channel%u\n",
2879                     idx, chan->offer_msg.child_rel_id);
2880         }
2881
2882         if (idx < sc->hn_tx_ring_inuse) {
2883                 struct hn_tx_ring *txr = &sc->hn_tx_ring[idx];
2884
2885                 KASSERT((txr->hn_tx_flags & HN_TX_FLAG_ATTACHED) == 0,
2886                     ("TX ring %d already attached", idx));
2887                 txr->hn_tx_flags |= HN_TX_FLAG_ATTACHED;
2888
2889                 chan->hv_chan_txr = txr;
2890                 txr->hn_chan = chan;
2891                 if (bootverbose) {
2892                         if_printf(sc->hn_ifp, "link TX ring %d to channel%u\n",
2893                             idx, chan->offer_msg.child_rel_id);
2894                 }
2895         }
2896
2897         /* Bind channel to a proper CPU */
2898         vmbus_channel_cpu_set(chan, (sc->hn_cpu + idx) % mp_ncpus);
2899 }
2900
2901 static void
2902 hn_subchan_attach(struct hn_softc *sc, struct hv_vmbus_channel *chan)
2903 {
2904
2905         KASSERT(!HV_VMBUS_CHAN_ISPRIMARY(chan),
2906             ("subchannel callback on primary channel"));
2907         KASSERT(chan->offer_msg.offer.sub_channel_index > 0,
2908             ("invalid channel subidx %u",
2909              chan->offer_msg.offer.sub_channel_index));
2910         hn_channel_attach(sc, chan);
2911 }
2912
2913 static void
2914 hn_tx_taskq_create(void *arg __unused)
2915 {
2916         if (!hn_share_tx_taskq)
2917                 return;
2918
2919         hn_tx_taskq = taskqueue_create("hn_tx", M_WAITOK,
2920             taskqueue_thread_enqueue, &hn_tx_taskq);
2921         if (hn_bind_tx_taskq >= 0) {
2922                 int cpu = hn_bind_tx_taskq;
2923                 cpuset_t cpu_set;
2924
2925                 if (cpu > mp_ncpus - 1)
2926                         cpu = mp_ncpus - 1;
2927                 CPU_SETOF(cpu, &cpu_set);
2928                 taskqueue_start_threads_cpuset(&hn_tx_taskq, 1, PI_NET,
2929                     &cpu_set, "hn tx");
2930         } else {
2931                 taskqueue_start_threads(&hn_tx_taskq, 1, PI_NET, "hn tx");
2932         }
2933 }
2934 SYSINIT(hn_txtq_create, SI_SUB_DRIVERS, SI_ORDER_FIRST,
2935     hn_tx_taskq_create, NULL);
2936
2937 static void
2938 hn_tx_taskq_destroy(void *arg __unused)
2939 {
2940         if (hn_tx_taskq != NULL)
2941                 taskqueue_free(hn_tx_taskq);
2942 }
2943 SYSUNINIT(hn_txtq_destroy, SI_SUB_DRIVERS, SI_ORDER_FIRST,
2944     hn_tx_taskq_destroy, NULL);
2945
2946 static device_method_t netvsc_methods[] = {
2947         /* Device interface */
2948         DEVMETHOD(device_probe,         netvsc_probe),
2949         DEVMETHOD(device_attach,        netvsc_attach),
2950         DEVMETHOD(device_detach,        netvsc_detach),
2951         DEVMETHOD(device_shutdown,      netvsc_shutdown),
2952
2953         { 0, 0 }
2954 };
2955
2956 static driver_t netvsc_driver = {
2957         NETVSC_DEVNAME,
2958         netvsc_methods,
2959         sizeof(hn_softc_t)
2960 };
2961
2962 static devclass_t netvsc_devclass;
2963
2964 DRIVER_MODULE(hn, vmbus, netvsc_driver, netvsc_devclass, 0, 0);
2965 MODULE_VERSION(hn, 1);
2966 MODULE_DEPEND(hn, vmbus, 1, 1, 1);