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