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