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