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