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