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