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