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