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