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