]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/dev/hyperv/netvsc/hv_netvsc_drv_freebsd.c
MFC 295740,295741,295742
[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/queue.h>
70 #include <sys/lock.h>
71 #include <sys/sx.h>
72 #include <sys/sysctl.h>
73
74 #include <net/if.h>
75 #include <net/if_arp.h>
76 #include <net/ethernet.h>
77 #include <net/if_dl.h>
78 #include <net/if_media.h>
79
80 #include <net/bpf.h>
81
82 #include <net/if_types.h>
83 #include <net/if_vlan_var.h>
84 #include <net/if.h>
85
86 #include <netinet/in_systm.h>
87 #include <netinet/in.h>
88 #include <netinet/ip.h>
89 #include <netinet/if_ether.h>
90 #include <netinet/tcp.h>
91 #include <netinet/udp.h>
92 #include <netinet/ip6.h>
93
94 #include <vm/vm.h>
95 #include <vm/vm_param.h>
96 #include <vm/vm_kern.h>
97 #include <vm/pmap.h>
98
99 #include <machine/bus.h>
100 #include <machine/resource.h>
101 #include <machine/frame.h>
102 #include <machine/vmparam.h>
103
104 #include <sys/bus.h>
105 #include <sys/rman.h>
106 #include <sys/mutex.h>
107 #include <sys/errno.h>
108 #include <sys/types.h>
109 #include <machine/atomic.h>
110
111 #include <machine/intr_machdep.h>
112
113 #include <machine/in_cksum.h>
114
115 #include <dev/hyperv/include/hyperv.h>
116 #include "hv_net_vsc.h"
117 #include "hv_rndis.h"
118 #include "hv_rndis_filter.h"
119
120
121 /* Short for Hyper-V network interface */
122 #define NETVSC_DEVNAME    "hn"
123
124 /*
125  * It looks like offset 0 of buf is reserved to hold the softc pointer.
126  * The sc pointer evidently not needed, and is not presently populated.
127  * The packet offset is where the netvsc_packet starts in the buffer.
128  */
129 #define HV_NV_SC_PTR_OFFSET_IN_BUF         0
130 #define HV_NV_PACKET_OFFSET_IN_BUF         16
131
132 /* YYY should get it from the underlying channel */
133 #define HN_TX_DESC_CNT                  512
134
135 #define HN_LROENT_CNT_DEF               128
136
137 #define HN_RNDIS_MSG_LEN                \
138     (sizeof(rndis_msg) +                \
139      RNDIS_VLAN_PPI_SIZE +              \
140      RNDIS_TSO_PPI_SIZE +               \
141      RNDIS_CSUM_PPI_SIZE)
142 #define HN_RNDIS_MSG_BOUNDARY           PAGE_SIZE
143 #define HN_RNDIS_MSG_ALIGN              CACHE_LINE_SIZE
144
145 #define HN_TX_DATA_BOUNDARY             PAGE_SIZE
146 #define HN_TX_DATA_MAXSIZE              IP_MAXPACKET
147 #define HN_TX_DATA_SEGSIZE              PAGE_SIZE
148 #define HN_TX_DATA_SEGCNT_MAX           \
149     (NETVSC_PACKET_MAXPAGE - HV_RF_NUM_TX_RESERVED_PAGE_BUFS)
150
151 #define HN_DIRECT_TX_SIZE_DEF           128
152
153 struct hn_txdesc {
154         SLIST_ENTRY(hn_txdesc) link;
155         struct mbuf     *m;
156         struct hn_softc *sc;
157         int             refs;
158         uint32_t        flags;          /* HN_TXD_FLAG_ */
159         netvsc_packet   netvsc_pkt;     /* XXX to be removed */
160
161         bus_dmamap_t    data_dmap;
162
163         bus_addr_t      rndis_msg_paddr;
164         rndis_msg       *rndis_msg;
165         bus_dmamap_t    rndis_msg_dmap;
166 };
167
168 #define HN_TXD_FLAG_ONLIST      0x1
169 #define HN_TXD_FLAG_DMAMAP      0x2
170
171 /*
172  * Only enable UDP checksum offloading when it is on 2012R2 or
173  * later.  UDP checksum offloading doesn't work on earlier
174  * Windows releases.
175  */
176 #define HN_CSUM_ASSIST_WIN8     (CSUM_TCP)
177 #define HN_CSUM_ASSIST          (CSUM_IP | CSUM_UDP | CSUM_TCP)
178
179 #define HN_LRO_LENLIM_DEF               (25 * ETHERMTU)
180 /* YYY 2*MTU is a bit rough, but should be good enough. */
181 #define HN_LRO_LENLIM_MIN(ifp)          (2 * (ifp)->if_mtu)
182
183 #define HN_LRO_ACKCNT_DEF               1
184
185 /*
186  * Be aware that this sleepable mutex will exhibit WITNESS errors when
187  * certain TCP and ARP code paths are taken.  This appears to be a
188  * well-known condition, as all other drivers checked use a sleeping
189  * mutex to protect their transmit paths.
190  * Also Be aware that mutexes do not play well with semaphores, and there
191  * is a conflicting semaphore in a certain channel code path.
192  */
193 #define NV_LOCK_INIT(_sc, _name) \
194             mtx_init(&(_sc)->hn_lock, _name, MTX_NETWORK_LOCK, MTX_DEF)
195 #define NV_LOCK(_sc)            mtx_lock(&(_sc)->hn_lock)
196 #define NV_TRYLOCK(_sc)         mtx_trylock(&(_sc)->hn_lock)
197 #define NV_LOCK_ASSERT(_sc)     mtx_assert(&(_sc)->hn_lock, MA_OWNED)
198 #define NV_UNLOCK(_sc)          mtx_unlock(&(_sc)->hn_lock)
199 #define NV_LOCK_DESTROY(_sc)    mtx_destroy(&(_sc)->hn_lock)
200
201
202 /*
203  * Globals
204  */
205
206 int hv_promisc_mode = 0;    /* normal mode by default */
207
208 /* Trust tcp segements verification on host side. */
209 static int hn_trust_hosttcp = 1;
210 TUNABLE_INT("dev.hn.trust_hosttcp", &hn_trust_hosttcp);
211
212 /* Trust udp datagrams verification on host side. */
213 static int hn_trust_hostudp = 1;
214 TUNABLE_INT("dev.hn.trust_hostudp", &hn_trust_hostudp);
215
216 /* Trust ip packets verification on host side. */
217 static int hn_trust_hostip = 1;
218 TUNABLE_INT("dev.hn.trust_hostip", &hn_trust_hostip);
219
220 #if __FreeBSD_version >= 1100045
221 /* Limit TSO burst size */
222 static int hn_tso_maxlen = 0;
223 TUNABLE_INT("dev.hn.tso_maxlen", &hn_tso_maxlen);
224 #endif
225
226 /* Limit chimney send size */
227 static int hn_tx_chimney_size = 0;
228 TUNABLE_INT("dev.hn.tx_chimney_size", &hn_tx_chimney_size);
229
230 /* Limit the size of packet for direct transmission */
231 static int hn_direct_tx_size = HN_DIRECT_TX_SIZE_DEF;
232 TUNABLE_INT("dev.hn.direct_tx_size", &hn_direct_tx_size);
233
234 #if defined(INET) || defined(INET6)
235 #if __FreeBSD_version >= 1100095
236 static int hn_lro_entry_count = HN_LROENT_CNT_DEF;
237 TUNABLE_INT("dev.hn.lro_entry_count", &hn_lro_entry_count);
238 #endif
239 #endif
240
241 static int hn_share_tx_taskq = 0;
242 TUNABLE_INT("hw.hn.share_tx_taskq", &hn_share_tx_taskq);
243
244 static struct taskqueue *hn_tx_taskq;
245
246 /*
247  * Forward declarations
248  */
249 static void hn_stop(hn_softc_t *sc);
250 static void hn_ifinit_locked(hn_softc_t *sc);
251 static void hn_ifinit(void *xsc);
252 static int  hn_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
253 static int hn_start_locked(struct ifnet *ifp, int len);
254 static void hn_start(struct ifnet *ifp);
255 static void hn_start_txeof(struct ifnet *ifp);
256 static int hn_ifmedia_upd(struct ifnet *ifp);
257 static void hn_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr);
258 #if __FreeBSD_version >= 1100099
259 static int hn_lro_lenlim_sysctl(SYSCTL_HANDLER_ARGS);
260 static int hn_lro_ackcnt_sysctl(SYSCTL_HANDLER_ARGS);
261 #endif
262 static int hn_trust_hcsum_sysctl(SYSCTL_HANDLER_ARGS);
263 static int hn_tx_chimney_size_sysctl(SYSCTL_HANDLER_ARGS);
264 static int hn_check_iplen(const struct mbuf *, int);
265 static int hn_create_tx_ring(struct hn_softc *sc);
266 static void hn_destroy_tx_ring(struct hn_softc *sc);
267 static void hn_start_taskfunc(void *xsc, int pending);
268 static void hn_txeof_taskfunc(void *xsc, int pending);
269 static int hn_encap(struct hn_softc *, struct hn_txdesc *, struct mbuf **);
270
271 static int
272 hn_ifmedia_upd(struct ifnet *ifp __unused)
273 {
274
275         return EOPNOTSUPP;
276 }
277
278 static void
279 hn_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
280 {
281         struct hn_softc *sc = ifp->if_softc;
282
283         ifmr->ifm_status = IFM_AVALID;
284         ifmr->ifm_active = IFM_ETHER;
285
286         if (!sc->hn_carrier) {
287                 ifmr->ifm_active |= IFM_NONE;
288                 return;
289         }
290         ifmr->ifm_status |= IFM_ACTIVE;
291         ifmr->ifm_active |= IFM_10G_T | IFM_FDX;
292 }
293
294 /* {F8615163-DF3E-46c5-913F-F2D2F965ED0E} */
295 static const hv_guid g_net_vsc_device_type = {
296         .data = {0x63, 0x51, 0x61, 0xF8, 0x3E, 0xDF, 0xc5, 0x46,
297                 0x91, 0x3F, 0xF2, 0xD2, 0xF9, 0x65, 0xED, 0x0E}
298 };
299
300 /*
301  * Standard probe entry point.
302  *
303  */
304 static int
305 netvsc_probe(device_t dev)
306 {
307         const char *p;
308
309         p = vmbus_get_type(dev);
310         if (!memcmp(p, &g_net_vsc_device_type.data, sizeof(hv_guid))) {
311                 device_set_desc(dev, "Synthetic Network Interface");
312                 if (bootverbose)
313                         printf("Netvsc probe... DONE \n");
314
315                 return (BUS_PROBE_DEFAULT);
316         }
317
318         return (ENXIO);
319 }
320
321 /*
322  * Standard attach entry point.
323  *
324  * Called when the driver is loaded.  It allocates needed resources,
325  * and initializes the "hardware" and software.
326  */
327 static int
328 netvsc_attach(device_t dev)
329 {
330         struct hv_device *device_ctx = vmbus_get_devctx(dev);
331         netvsc_device_info device_info;
332         hn_softc_t *sc;
333         int unit = device_get_unit(dev);
334         struct ifnet *ifp = NULL;
335         struct sysctl_oid_list *child;
336         struct sysctl_ctx_list *ctx;
337         int error;
338 #if __FreeBSD_version >= 1100045
339         int tso_maxlen;
340 #endif
341 #if defined(INET) || defined(INET6)
342 #if __FreeBSD_version >= 1100095
343         int lroent_cnt;
344 #endif
345 #endif
346
347         sc = device_get_softc(dev);
348         if (sc == NULL) {
349                 return (ENOMEM);
350         }
351
352         bzero(sc, sizeof(hn_softc_t));
353         sc->hn_unit = unit;
354         sc->hn_dev = dev;
355         sc->hn_direct_tx_size = hn_direct_tx_size;
356         if (hn_trust_hosttcp)
357                 sc->hn_trust_hcsum |= HN_TRUST_HCSUM_TCP;
358         if (hn_trust_hostudp)
359                 sc->hn_trust_hcsum |= HN_TRUST_HCSUM_UDP;
360         if (hn_trust_hostip)
361                 sc->hn_trust_hcsum |= HN_TRUST_HCSUM_IP;
362
363         if (hn_tx_taskq == NULL) {
364                 sc->hn_tx_taskq = taskqueue_create_fast("hn_tx", M_WAITOK,
365                     taskqueue_thread_enqueue, &sc->hn_tx_taskq);
366                 taskqueue_start_threads(&sc->hn_tx_taskq, 1, PI_NET, "%s tx",
367                     device_get_nameunit(dev));
368         } else {
369                 sc->hn_tx_taskq = hn_tx_taskq;
370         }
371         TASK_INIT(&sc->hn_start_task, 0, hn_start_taskfunc, sc);
372         TASK_INIT(&sc->hn_txeof_task, 0, hn_txeof_taskfunc, sc);
373
374         error = hn_create_tx_ring(sc);
375         if (error)
376                 goto failed;
377
378         NV_LOCK_INIT(sc, "NetVSCLock");
379
380         sc->hn_dev_obj = device_ctx;
381
382         ifp = sc->hn_ifp = sc->arpcom.ac_ifp = if_alloc(IFT_ETHER);
383         ifp->if_softc = sc;
384
385         if_initname(ifp, device_get_name(dev), device_get_unit(dev));
386         ifp->if_dunit = unit;
387         ifp->if_dname = NETVSC_DEVNAME;
388
389         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
390         ifp->if_ioctl = hn_ioctl;
391         ifp->if_start = hn_start;
392         ifp->if_init = hn_ifinit;
393         /* needed by hv_rf_on_device_add() code */
394         ifp->if_mtu = ETHERMTU;
395         IFQ_SET_MAXLEN(&ifp->if_snd, 512);
396         ifp->if_snd.ifq_drv_maxlen = 511;
397         IFQ_SET_READY(&ifp->if_snd);
398
399         ifmedia_init(&sc->hn_media, 0, hn_ifmedia_upd, hn_ifmedia_sts);
400         ifmedia_add(&sc->hn_media, IFM_ETHER | IFM_AUTO, 0, NULL);
401         ifmedia_set(&sc->hn_media, IFM_ETHER | IFM_AUTO);
402         /* XXX ifmedia_set really should do this for us */
403         sc->hn_media.ifm_media = sc->hn_media.ifm_cur->ifm_media;
404
405         /*
406          * Tell upper layers that we support full VLAN capability.
407          */
408         ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
409         ifp->if_capabilities |=
410             IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU | IFCAP_HWCSUM | IFCAP_TSO |
411             IFCAP_LRO;
412         ifp->if_capenable |=
413             IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU | IFCAP_HWCSUM | IFCAP_TSO |
414             IFCAP_LRO;
415
416         if (hv_vmbus_protocal_version >= HV_VMBUS_VERSION_WIN8_1)
417                 sc->hn_csum_assist = HN_CSUM_ASSIST;
418         else
419                 sc->hn_csum_assist = HN_CSUM_ASSIST_WIN8;
420         ifp->if_hwassist = sc->hn_csum_assist | CSUM_TSO;
421
422         error = hv_rf_on_device_add(device_ctx, &device_info);
423         if (error)
424                 goto failed;
425
426         if (device_info.link_state == 0) {
427                 sc->hn_carrier = 1;
428         }
429
430 #if defined(INET) || defined(INET6)
431 #if __FreeBSD_version >= 1100095
432         lroent_cnt = hn_lro_entry_count;
433         if (lroent_cnt < TCP_LRO_ENTRIES)
434                 lroent_cnt = TCP_LRO_ENTRIES;
435         tcp_lro_init_args(&sc->hn_lro, ifp, lroent_cnt, 0);
436         device_printf(dev, "LRO: entry count %d\n", lroent_cnt);
437 #else
438         tcp_lro_init(&sc->hn_lro);
439         /* Driver private LRO settings */
440         sc->hn_lro.ifp = ifp;
441 #endif
442 #if __FreeBSD_version >= 1100099
443         sc->hn_lro.lro_length_lim = HN_LRO_LENLIM_DEF;
444         sc->hn_lro.lro_ackcnt_lim = HN_LRO_ACKCNT_DEF;
445 #endif
446 #endif  /* INET || INET6 */
447
448 #if __FreeBSD_version >= 1100045
449         tso_maxlen = hn_tso_maxlen;
450         if (tso_maxlen <= 0 || tso_maxlen > IP_MAXPACKET)
451                 tso_maxlen = IP_MAXPACKET;
452
453         ifp->if_hw_tsomaxsegcount = HN_TX_DATA_SEGCNT_MAX;
454         ifp->if_hw_tsomaxsegsize = PAGE_SIZE;
455         ifp->if_hw_tsomax = tso_maxlen -
456             (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN);
457 #endif
458
459         ether_ifattach(ifp, device_info.mac_addr);
460
461 #if __FreeBSD_version >= 1100045
462         if_printf(ifp, "TSO: %u/%u/%u\n", ifp->if_hw_tsomax,
463             ifp->if_hw_tsomaxsegcount, ifp->if_hw_tsomaxsegsize);
464 #endif
465
466         sc->hn_tx_chimney_max = sc->net_dev->send_section_size;
467         sc->hn_tx_chimney_size = sc->hn_tx_chimney_max;
468         if (hn_tx_chimney_size > 0 &&
469             hn_tx_chimney_size < sc->hn_tx_chimney_max)
470                 sc->hn_tx_chimney_size = hn_tx_chimney_size;
471
472         /*
473          * Always schedule transmission instead of trying
474          * to do direct transmission.  This one gives the
475          * best performance so far.
476          */
477         sc->hn_sched_tx = 1;
478
479         ctx = device_get_sysctl_ctx(dev);
480         child = SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
481
482         SYSCTL_ADD_INT(ctx, child, OID_AUTO, "lro_queued",
483             CTLFLAG_RW, &sc->hn_lro.lro_queued, 0, "LRO queued");
484         SYSCTL_ADD_INT(ctx, child, OID_AUTO, "lro_flushed",
485             CTLFLAG_RW, &sc->hn_lro.lro_flushed, 0, "LRO flushed");
486         SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "lro_tried",
487             CTLFLAG_RW, &sc->hn_lro_tried, "# of LRO tries");
488 #if __FreeBSD_version >= 1100099
489         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "lro_length_lim",
490             CTLTYPE_UINT | CTLFLAG_RW, sc, 0, hn_lro_lenlim_sysctl, "IU",
491             "Max # of data bytes to be aggregated by LRO");
492         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "lro_ackcnt_lim",
493             CTLTYPE_INT | CTLFLAG_RW, sc, 0, hn_lro_ackcnt_sysctl, "I",
494             "Max # of ACKs to be aggregated by LRO");
495 #endif
496         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "trust_hosttcp",
497             CTLTYPE_INT | CTLFLAG_RW, sc, HN_TRUST_HCSUM_TCP,
498             hn_trust_hcsum_sysctl, "I",
499             "Trust tcp segement verification on host side, "
500             "when csum info is missing");
501         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "trust_hostudp",
502             CTLTYPE_INT | CTLFLAG_RW, sc, HN_TRUST_HCSUM_UDP,
503             hn_trust_hcsum_sysctl, "I",
504             "Trust udp datagram verification on host side, "
505             "when csum info is missing");
506         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "trust_hostip",
507             CTLTYPE_INT | CTLFLAG_RW, sc, HN_TRUST_HCSUM_IP,
508             hn_trust_hcsum_sysctl, "I",
509             "Trust ip packet verification on host side, "
510             "when csum info is missing");
511         SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "csum_ip",
512             CTLFLAG_RW, &sc->hn_csum_ip, "RXCSUM IP");
513         SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "csum_tcp",
514             CTLFLAG_RW, &sc->hn_csum_tcp, "RXCSUM TCP");
515         SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "csum_udp",
516             CTLFLAG_RW, &sc->hn_csum_udp, "RXCSUM UDP");
517         SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "csum_trusted",
518             CTLFLAG_RW, &sc->hn_csum_trusted,
519             "# of packets that we trust host's csum verification");
520         SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "small_pkts",
521             CTLFLAG_RW, &sc->hn_small_pkts, "# of small packets received");
522         SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "no_txdescs",
523             CTLFLAG_RW, &sc->hn_no_txdescs, "# of times short of TX descs");
524         SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "send_failed",
525             CTLFLAG_RW, &sc->hn_send_failed, "# of hyper-v sending failure");
526         SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "txdma_failed",
527             CTLFLAG_RW, &sc->hn_txdma_failed, "# of TX DMA failure");
528         SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "tx_collapsed",
529             CTLFLAG_RW, &sc->hn_tx_collapsed, "# of TX mbuf collapsed");
530         SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "tx_chimney",
531             CTLFLAG_RW, &sc->hn_tx_chimney, "# of chimney send");
532         SYSCTL_ADD_INT(ctx, child, OID_AUTO, "txdesc_cnt",
533             CTLFLAG_RD, &sc->hn_txdesc_cnt, 0, "# of total TX descs");
534         SYSCTL_ADD_INT(ctx, child, OID_AUTO, "txdesc_avail",
535             CTLFLAG_RD, &sc->hn_txdesc_avail, 0, "# of available TX descs");
536         SYSCTL_ADD_INT(ctx, child, OID_AUTO, "tx_chimney_max",
537             CTLFLAG_RD, &sc->hn_tx_chimney_max, 0,
538             "Chimney send packet size upper boundary");
539         SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "tx_chimney_size",
540             CTLTYPE_INT | CTLFLAG_RW, sc, 0, hn_tx_chimney_size_sysctl,
541             "I", "Chimney send packet size limit");
542         SYSCTL_ADD_INT(ctx, child, OID_AUTO, "direct_tx_size",
543             CTLFLAG_RW, &sc->hn_direct_tx_size, 0,
544             "Size of the packet for direct transmission");
545         SYSCTL_ADD_INT(ctx, child, OID_AUTO, "sched_tx",
546             CTLFLAG_RW, &sc->hn_sched_tx, 0,
547             "Always schedule transmission "
548             "instead of doing direct transmission");
549
550         if (unit == 0) {
551                 struct sysctl_ctx_list *dc_ctx;
552                 struct sysctl_oid_list *dc_child;
553                 devclass_t dc;
554
555                 /*
556                  * Add sysctl nodes for devclass
557                  */
558                 dc = device_get_devclass(dev);
559                 dc_ctx = devclass_get_sysctl_ctx(dc);
560                 dc_child = SYSCTL_CHILDREN(devclass_get_sysctl_tree(dc));
561
562                 SYSCTL_ADD_INT(dc_ctx, dc_child, OID_AUTO, "trust_hosttcp",
563                     CTLFLAG_RD, &hn_trust_hosttcp, 0,
564                     "Trust tcp segement verification on host side, "
565                     "when csum info is missing (global setting)");
566                 SYSCTL_ADD_INT(dc_ctx, dc_child, OID_AUTO, "trust_hostudp",
567                     CTLFLAG_RD, &hn_trust_hostudp, 0,
568                     "Trust udp datagram verification on host side, "
569                     "when csum info is missing (global setting)");
570                 SYSCTL_ADD_INT(dc_ctx, dc_child, OID_AUTO, "trust_hostip",
571                     CTLFLAG_RD, &hn_trust_hostip, 0,
572                     "Trust ip packet verification on host side, "
573                     "when csum info is missing (global setting)");
574                 SYSCTL_ADD_INT(dc_ctx, dc_child, OID_AUTO, "tx_chimney_size",
575                     CTLFLAG_RD, &hn_tx_chimney_size, 0,
576                     "Chimney send packet size limit");
577 #if __FreeBSD_version >= 1100045
578                 SYSCTL_ADD_INT(dc_ctx, dc_child, OID_AUTO, "tso_maxlen",
579                     CTLFLAG_RD, &hn_tso_maxlen, 0, "TSO burst limit");
580 #endif
581                 SYSCTL_ADD_INT(dc_ctx, dc_child, OID_AUTO, "direct_tx_size",
582                     CTLFLAG_RD, &hn_direct_tx_size, 0,
583                     "Size of the packet for direct transmission");
584 #if defined(INET) || defined(INET6)
585 #if __FreeBSD_version >= 1100095
586                 SYSCTL_ADD_INT(dc_ctx, dc_child, OID_AUTO, "lro_entry_count",
587                     CTLFLAG_RD, &hn_lro_entry_count, 0, "LRO entry count");
588 #endif
589 #endif
590         }
591
592         return (0);
593 failed:
594         hn_destroy_tx_ring(sc);
595         if (ifp != NULL)
596                 if_free(ifp);
597         return (error);
598 }
599
600 /*
601  * Standard detach entry point
602  */
603 static int
604 netvsc_detach(device_t dev)
605 {
606         struct hn_softc *sc = device_get_softc(dev);
607         struct hv_device *hv_device = vmbus_get_devctx(dev); 
608
609         if (bootverbose)
610                 printf("netvsc_detach\n");
611
612         /*
613          * XXXKYS:  Need to clean up all our
614          * driver state; this is the driver
615          * unloading.
616          */
617
618         /*
619          * XXXKYS:  Need to stop outgoing traffic and unregister
620          * the netdevice.
621          */
622
623         hv_rf_on_device_remove(hv_device, HV_RF_NV_DESTROY_CHANNEL);
624
625         taskqueue_drain(sc->hn_tx_taskq, &sc->hn_start_task);
626         taskqueue_drain(sc->hn_tx_taskq, &sc->hn_txeof_task);
627         if (sc->hn_tx_taskq != hn_tx_taskq)
628                 taskqueue_free(sc->hn_tx_taskq);
629
630         ifmedia_removeall(&sc->hn_media);
631 #if defined(INET) || defined(INET6)
632         tcp_lro_free(&sc->hn_lro);
633 #endif
634         hn_destroy_tx_ring(sc);
635
636         return (0);
637 }
638
639 /*
640  * Standard shutdown entry point
641  */
642 static int
643 netvsc_shutdown(device_t dev)
644 {
645         return (0);
646 }
647
648 static __inline int
649 hn_txdesc_dmamap_load(struct hn_softc *sc, struct hn_txdesc *txd,
650     struct mbuf **m_head, bus_dma_segment_t *segs, int *nsegs)
651 {
652         struct mbuf *m = *m_head;
653         int error;
654
655         error = bus_dmamap_load_mbuf_sg(sc->hn_tx_data_dtag, txd->data_dmap,
656             m, segs, nsegs, BUS_DMA_NOWAIT);
657         if (error == EFBIG) {
658                 struct mbuf *m_new;
659
660                 m_new = m_collapse(m, M_NOWAIT, HN_TX_DATA_SEGCNT_MAX);
661                 if (m_new == NULL)
662                         return ENOBUFS;
663                 else
664                         *m_head = m = m_new;
665                 sc->hn_tx_collapsed++;
666
667                 error = bus_dmamap_load_mbuf_sg(sc->hn_tx_data_dtag,
668                     txd->data_dmap, m, segs, nsegs, BUS_DMA_NOWAIT);
669         }
670         if (!error) {
671                 bus_dmamap_sync(sc->hn_tx_data_dtag, txd->data_dmap,
672                     BUS_DMASYNC_PREWRITE);
673                 txd->flags |= HN_TXD_FLAG_DMAMAP;
674         }
675         return error;
676 }
677
678 static __inline void
679 hn_txdesc_dmamap_unload(struct hn_softc *sc, struct hn_txdesc *txd)
680 {
681
682         if (txd->flags & HN_TXD_FLAG_DMAMAP) {
683                 bus_dmamap_sync(sc->hn_tx_data_dtag,
684                     txd->data_dmap, BUS_DMASYNC_POSTWRITE);
685                 bus_dmamap_unload(sc->hn_tx_data_dtag,
686                     txd->data_dmap);
687                 txd->flags &= ~HN_TXD_FLAG_DMAMAP;
688         }
689 }
690
691 static __inline int
692 hn_txdesc_put(struct hn_softc *sc, struct hn_txdesc *txd)
693 {
694
695         KASSERT((txd->flags & HN_TXD_FLAG_ONLIST) == 0,
696             ("put an onlist txd %#x", txd->flags));
697
698         KASSERT(txd->refs > 0, ("invalid txd refs %d", txd->refs));
699         if (atomic_fetchadd_int(&txd->refs, -1) != 1)
700                 return 0;
701
702         hn_txdesc_dmamap_unload(sc, txd);
703         if (txd->m != NULL) {
704                 m_freem(txd->m);
705                 txd->m = NULL;
706         }
707
708         txd->flags |= HN_TXD_FLAG_ONLIST;
709
710         mtx_lock_spin(&sc->hn_txlist_spin);
711         KASSERT(sc->hn_txdesc_avail >= 0 &&
712             sc->hn_txdesc_avail < sc->hn_txdesc_cnt,
713             ("txdesc_put: invalid txd avail %d", sc->hn_txdesc_avail));
714         sc->hn_txdesc_avail++;
715         SLIST_INSERT_HEAD(&sc->hn_txlist, txd, link);
716         mtx_unlock_spin(&sc->hn_txlist_spin);
717
718         return 1;
719 }
720
721 static __inline struct hn_txdesc *
722 hn_txdesc_get(struct hn_softc *sc)
723 {
724         struct hn_txdesc *txd;
725
726         mtx_lock_spin(&sc->hn_txlist_spin);
727         txd = SLIST_FIRST(&sc->hn_txlist);
728         if (txd != NULL) {
729                 KASSERT(sc->hn_txdesc_avail > 0,
730                     ("txdesc_get: invalid txd avail %d", sc->hn_txdesc_avail));
731                 sc->hn_txdesc_avail--;
732                 SLIST_REMOVE_HEAD(&sc->hn_txlist, link);
733         }
734         mtx_unlock_spin(&sc->hn_txlist_spin);
735
736         if (txd != NULL) {
737                 KASSERT(txd->m == NULL && txd->refs == 0 &&
738                     (txd->flags & HN_TXD_FLAG_ONLIST), ("invalid txd"));
739                 txd->flags &= ~HN_TXD_FLAG_ONLIST;
740                 txd->refs = 1;
741         }
742         return txd;
743 }
744
745 static __inline void
746 hn_txdesc_hold(struct hn_txdesc *txd)
747 {
748
749         /* 0->1 transition will never work */
750         KASSERT(txd->refs > 0, ("invalid refs %d", txd->refs));
751         atomic_add_int(&txd->refs, 1);
752 }
753
754 /*
755  * Send completion processing
756  *
757  * Note:  It looks like offset 0 of buf is reserved to hold the softc
758  * pointer.  The sc pointer is not currently needed in this function, and
759  * it is not presently populated by the TX function.
760  */
761 void
762 netvsc_xmit_completion(void *context)
763 {
764         netvsc_packet *packet = context;
765         struct hn_txdesc *txd;
766         struct hn_softc *sc;
767
768         txd = (struct hn_txdesc *)(uintptr_t)
769             packet->compl.send.send_completion_tid;
770
771         sc = txd->sc;
772         sc->hn_txeof = 1;
773         hn_txdesc_put(sc, txd);
774 }
775
776 void
777 netvsc_channel_rollup(struct hv_device *device_ctx)
778 {
779         struct hn_softc *sc = device_get_softc(device_ctx->device);
780 #if defined(INET) || defined(INET6)
781         struct lro_ctrl *lro = &sc->hn_lro;
782         struct lro_entry *queued;
783
784         while ((queued = SLIST_FIRST(&lro->lro_active)) != NULL) {
785                 SLIST_REMOVE_HEAD(&lro->lro_active, next);
786                 tcp_lro_flush(lro, queued);
787         }
788 #endif
789
790         if (!sc->hn_txeof)
791                 return;
792
793         sc->hn_txeof = 0;
794         hn_start_txeof(sc->hn_ifp);
795 }
796
797 /*
798  * NOTE:
799  * If this function fails, then both txd and m_head0 will be freed.
800  */
801 static int
802 hn_encap(struct hn_softc *sc, struct hn_txdesc *txd, struct mbuf **m_head0)
803 {
804         bus_dma_segment_t segs[HN_TX_DATA_SEGCNT_MAX];
805         int error, nsegs, i;
806         struct mbuf *m_head = *m_head0;
807         netvsc_packet *packet;
808         rndis_msg *rndis_mesg;
809         rndis_packet *rndis_pkt;
810         rndis_per_packet_info *rppi;
811         uint32_t rndis_msg_size;
812
813         packet = &txd->netvsc_pkt;
814         packet->is_data_pkt = TRUE;
815         packet->tot_data_buf_len = m_head->m_pkthdr.len;
816
817         /*
818          * extension points to the area reserved for the
819          * rndis_filter_packet, which is placed just after
820          * the netvsc_packet (and rppi struct, if present;
821          * length is updated later).
822          */
823         rndis_mesg = txd->rndis_msg;
824         /* XXX not necessary */
825         memset(rndis_mesg, 0, HN_RNDIS_MSG_LEN);
826         rndis_mesg->ndis_msg_type = REMOTE_NDIS_PACKET_MSG;
827
828         rndis_pkt = &rndis_mesg->msg.packet;
829         rndis_pkt->data_offset = sizeof(rndis_packet);
830         rndis_pkt->data_length = packet->tot_data_buf_len;
831         rndis_pkt->per_pkt_info_offset = sizeof(rndis_packet);
832
833         rndis_msg_size = RNDIS_MESSAGE_SIZE(rndis_packet);
834
835         if (m_head->m_flags & M_VLANTAG) {
836                 ndis_8021q_info *rppi_vlan_info;
837
838                 rndis_msg_size += RNDIS_VLAN_PPI_SIZE;
839                 rppi = hv_set_rppi_data(rndis_mesg, RNDIS_VLAN_PPI_SIZE,
840                     ieee_8021q_info);
841
842                 rppi_vlan_info = (ndis_8021q_info *)((uint8_t *)rppi +
843                     rppi->per_packet_info_offset);
844                 rppi_vlan_info->u1.s1.vlan_id =
845                     m_head->m_pkthdr.ether_vtag & 0xfff;
846         }
847
848         if (m_head->m_pkthdr.csum_flags & CSUM_TSO) {
849                 rndis_tcp_tso_info *tso_info;   
850                 struct ether_vlan_header *eh;
851                 int ether_len;
852
853                 /*
854                  * XXX need m_pullup and use mtodo
855                  */
856                 eh = mtod(m_head, struct ether_vlan_header*);
857                 if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN))
858                         ether_len = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
859                 else
860                         ether_len = ETHER_HDR_LEN;
861
862                 rndis_msg_size += RNDIS_TSO_PPI_SIZE;
863                 rppi = hv_set_rppi_data(rndis_mesg, RNDIS_TSO_PPI_SIZE,
864                     tcp_large_send_info);
865
866                 tso_info = (rndis_tcp_tso_info *)((uint8_t *)rppi +
867                     rppi->per_packet_info_offset);
868                 tso_info->lso_v2_xmit.type =
869                     RNDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE;
870
871 #ifdef INET
872                 if (m_head->m_pkthdr.csum_flags & CSUM_IP_TSO) {
873                         struct ip *ip =
874                             (struct ip *)(m_head->m_data + ether_len);
875                         unsigned long iph_len = ip->ip_hl << 2;
876                         struct tcphdr *th =
877                             (struct tcphdr *)((caddr_t)ip + iph_len);
878
879                         tso_info->lso_v2_xmit.ip_version =
880                             RNDIS_TCP_LARGE_SEND_OFFLOAD_IPV4;
881                         ip->ip_len = 0;
882                         ip->ip_sum = 0;
883
884                         th->th_sum = in_pseudo(ip->ip_src.s_addr,
885                             ip->ip_dst.s_addr, htons(IPPROTO_TCP));
886                 }
887 #endif
888 #if defined(INET6) && defined(INET)
889                 else
890 #endif
891 #ifdef INET6
892                 {
893                         struct ip6_hdr *ip6 = (struct ip6_hdr *)
894                             (m_head->m_data + ether_len);
895                         struct tcphdr *th = (struct tcphdr *)(ip6 + 1);
896
897                         tso_info->lso_v2_xmit.ip_version =
898                             RNDIS_TCP_LARGE_SEND_OFFLOAD_IPV6;
899                         ip6->ip6_plen = 0;
900                         th->th_sum = in6_cksum_pseudo(ip6, 0, IPPROTO_TCP, 0);
901                 }
902 #endif
903                 tso_info->lso_v2_xmit.tcp_header_offset = 0;
904                 tso_info->lso_v2_xmit.mss = m_head->m_pkthdr.tso_segsz;
905         } else if (m_head->m_pkthdr.csum_flags & sc->hn_csum_assist) {
906                 rndis_tcp_ip_csum_info *csum_info;
907
908                 rndis_msg_size += RNDIS_CSUM_PPI_SIZE;
909                 rppi = hv_set_rppi_data(rndis_mesg, RNDIS_CSUM_PPI_SIZE,
910                     tcpip_chksum_info);
911                 csum_info = (rndis_tcp_ip_csum_info *)((uint8_t *)rppi +
912                     rppi->per_packet_info_offset);
913
914                 csum_info->xmit.is_ipv4 = 1;
915                 if (m_head->m_pkthdr.csum_flags & CSUM_IP)
916                         csum_info->xmit.ip_header_csum = 1;
917
918                 if (m_head->m_pkthdr.csum_flags & CSUM_TCP) {
919                         csum_info->xmit.tcp_csum = 1;
920                         csum_info->xmit.tcp_header_offset = 0;
921                 } else if (m_head->m_pkthdr.csum_flags & CSUM_UDP) {
922                         csum_info->xmit.udp_csum = 1;
923                 }
924         }
925
926         rndis_mesg->msg_len = packet->tot_data_buf_len + rndis_msg_size;
927         packet->tot_data_buf_len = rndis_mesg->msg_len;
928
929         /*
930          * Chimney send, if the packet could fit into one chimney buffer.
931          */
932         if (packet->tot_data_buf_len < sc->hn_tx_chimney_size) {
933                 netvsc_dev *net_dev = sc->net_dev;
934                 uint32_t send_buf_section_idx;
935
936                 send_buf_section_idx =
937                     hv_nv_get_next_send_section(net_dev);
938                 if (send_buf_section_idx !=
939                     NVSP_1_CHIMNEY_SEND_INVALID_SECTION_INDEX) {
940                         uint8_t *dest = ((uint8_t *)net_dev->send_buf +
941                             (send_buf_section_idx *
942                              net_dev->send_section_size));
943
944                         memcpy(dest, rndis_mesg, rndis_msg_size);
945                         dest += rndis_msg_size;
946                         m_copydata(m_head, 0, m_head->m_pkthdr.len, dest);
947
948                         packet->send_buf_section_idx = send_buf_section_idx;
949                         packet->send_buf_section_size =
950                             packet->tot_data_buf_len;
951                         packet->page_buf_count = 0;
952                         sc->hn_tx_chimney++;
953                         goto done;
954                 }
955         }
956
957         error = hn_txdesc_dmamap_load(sc, txd, &m_head, segs, &nsegs);
958         if (error) {
959                 int freed;
960
961                 /*
962                  * This mbuf is not linked w/ the txd yet, so free it now.
963                  */
964                 m_freem(m_head);
965                 *m_head0 = NULL;
966
967                 freed = hn_txdesc_put(sc, txd);
968                 KASSERT(freed != 0,
969                     ("fail to free txd upon txdma error"));
970
971                 sc->hn_txdma_failed++;
972                 if_inc_counter(sc->hn_ifp, IFCOUNTER_OERRORS, 1);
973                 return error;
974         }
975         *m_head0 = m_head;
976
977         packet->page_buf_count = nsegs + HV_RF_NUM_TX_RESERVED_PAGE_BUFS;
978
979         /* send packet with page buffer */
980         packet->page_buffers[0].pfn = atop(txd->rndis_msg_paddr);
981         packet->page_buffers[0].offset = txd->rndis_msg_paddr & PAGE_MASK;
982         packet->page_buffers[0].length = rndis_msg_size;
983
984         /*
985          * Fill the page buffers with mbuf info starting at index
986          * HV_RF_NUM_TX_RESERVED_PAGE_BUFS.
987          */
988         for (i = 0; i < nsegs; ++i) {
989                 hv_vmbus_page_buffer *pb = &packet->page_buffers[
990                     i + HV_RF_NUM_TX_RESERVED_PAGE_BUFS];
991
992                 pb->pfn = atop(segs[i].ds_addr);
993                 pb->offset = segs[i].ds_addr & PAGE_MASK;
994                 pb->length = segs[i].ds_len;
995         }
996
997         packet->send_buf_section_idx =
998             NVSP_1_CHIMNEY_SEND_INVALID_SECTION_INDEX;
999         packet->send_buf_section_size = 0;
1000 done:
1001         txd->m = m_head;
1002
1003         /* Set the completion routine */
1004         packet->compl.send.on_send_completion = netvsc_xmit_completion;
1005         packet->compl.send.send_completion_context = packet;
1006         packet->compl.send.send_completion_tid = (uint64_t)(uintptr_t)txd;
1007
1008         return 0;
1009 }
1010
1011 /*
1012  * Start a transmit of one or more packets
1013  */
1014 static int
1015 hn_start_locked(struct ifnet *ifp, int len)
1016 {
1017         struct hn_softc *sc = ifp->if_softc;
1018         struct hv_device *device_ctx = vmbus_get_devctx(sc->hn_dev);
1019
1020         if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
1021             IFF_DRV_RUNNING)
1022                 return 0;
1023
1024         while (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) {
1025                 int error, send_failed = 0;
1026                 struct hn_txdesc *txd;
1027                 struct mbuf *m_head;
1028
1029                 IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
1030                 if (m_head == NULL)
1031                         break;
1032
1033                 if (len > 0 && m_head->m_pkthdr.len > len) {
1034                         /*
1035                          * This sending could be time consuming; let callers
1036                          * dispatch this packet sending (and sending of any
1037                          * following up packets) to tx taskqueue.
1038                          */
1039                         IF_PREPEND(&ifp->if_snd, m_head);
1040                         return 1;
1041                 }
1042
1043                 txd = hn_txdesc_get(sc);
1044                 if (txd == NULL) {
1045                         sc->hn_no_txdescs++;
1046                         IF_PREPEND(&ifp->if_snd, m_head);
1047                         atomic_set_int(&ifp->if_drv_flags, IFF_DRV_OACTIVE);
1048                         break;
1049                 }
1050
1051                 error = hn_encap(sc, txd, &m_head);
1052                 if (error) {
1053                         /* Both txd and m_head are freed */
1054                         continue;
1055                 }
1056 again:
1057                 /*
1058                  * Make sure that txd is not freed before ETHER_BPF_MTAP.
1059                  */
1060                 hn_txdesc_hold(txd);
1061                 error = hv_nv_on_send(device_ctx, &txd->netvsc_pkt);
1062                 if (!error) {
1063                         ETHER_BPF_MTAP(ifp, m_head);
1064                         if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1065                 }
1066                 hn_txdesc_put(sc, txd);
1067
1068                 if (__predict_false(error)) {
1069                         int freed;
1070
1071                         /*
1072                          * This should "really rarely" happen.
1073                          *
1074                          * XXX Too many RX to be acked or too many sideband
1075                          * commands to run?  Ask netvsc_channel_rollup()
1076                          * to kick start later.
1077                          */
1078                         sc->hn_txeof = 1;
1079                         if (!send_failed) {
1080                                 sc->hn_send_failed++;
1081                                 send_failed = 1;
1082                                 /*
1083                                  * Try sending again after set hn_txeof;
1084                                  * in case that we missed the last
1085                                  * netvsc_channel_rollup().
1086                                  */
1087                                 goto again;
1088                         }
1089                         if_printf(ifp, "send failed\n");
1090
1091                         /*
1092                          * This mbuf will be prepended, don't free it
1093                          * in hn_txdesc_put(); only unload it from the
1094                          * DMA map in hn_txdesc_put(), if it was loaded.
1095                          */
1096                         txd->m = NULL;
1097                         freed = hn_txdesc_put(sc, txd);
1098                         KASSERT(freed != 0,
1099                             ("fail to free txd upon send error"));
1100
1101                         sc->hn_send_failed++;
1102                         IF_PREPEND(&ifp->if_snd, m_head);
1103                         atomic_set_int(&ifp->if_drv_flags, IFF_DRV_OACTIVE);
1104                         break;
1105                 }
1106         }
1107         return 0;
1108 }
1109
1110 /*
1111  * Link up/down notification
1112  */
1113 void
1114 netvsc_linkstatus_callback(struct hv_device *device_obj, uint32_t status)
1115 {
1116         hn_softc_t *sc = device_get_softc(device_obj->device);
1117
1118         if (sc == NULL) {
1119                 return;
1120         }
1121
1122         if (status == 1) {
1123                 sc->hn_carrier = 1;
1124         } else {
1125                 sc->hn_carrier = 0;
1126         }
1127 }
1128
1129 /*
1130  * Append the specified data to the indicated mbuf chain,
1131  * Extend the mbuf chain if the new data does not fit in
1132  * existing space.
1133  *
1134  * This is a minor rewrite of m_append() from sys/kern/uipc_mbuf.c.
1135  * There should be an equivalent in the kernel mbuf code,
1136  * but there does not appear to be one yet.
1137  *
1138  * Differs from m_append() in that additional mbufs are
1139  * allocated with cluster size MJUMPAGESIZE, and filled
1140  * accordingly.
1141  *
1142  * Return 1 if able to complete the job; otherwise 0.
1143  */
1144 static int
1145 hv_m_append(struct mbuf *m0, int len, c_caddr_t cp)
1146 {
1147         struct mbuf *m, *n;
1148         int remainder, space;
1149
1150         for (m = m0; m->m_next != NULL; m = m->m_next)
1151                 ;
1152         remainder = len;
1153         space = M_TRAILINGSPACE(m);
1154         if (space > 0) {
1155                 /*
1156                  * Copy into available space.
1157                  */
1158                 if (space > remainder)
1159                         space = remainder;
1160                 bcopy(cp, mtod(m, caddr_t) + m->m_len, space);
1161                 m->m_len += space;
1162                 cp += space;
1163                 remainder -= space;
1164         }
1165         while (remainder > 0) {
1166                 /*
1167                  * Allocate a new mbuf; could check space
1168                  * and allocate a cluster instead.
1169                  */
1170                 n = m_getjcl(M_DONTWAIT, m->m_type, 0, MJUMPAGESIZE);
1171                 if (n == NULL)
1172                         break;
1173                 n->m_len = min(MJUMPAGESIZE, remainder);
1174                 bcopy(cp, mtod(n, caddr_t), n->m_len);
1175                 cp += n->m_len;
1176                 remainder -= n->m_len;
1177                 m->m_next = n;
1178                 m = n;
1179         }
1180         if (m0->m_flags & M_PKTHDR)
1181                 m0->m_pkthdr.len += len - remainder;
1182
1183         return (remainder == 0);
1184 }
1185
1186
1187 /*
1188  * Called when we receive a data packet from the "wire" on the
1189  * specified device
1190  *
1191  * Note:  This is no longer used as a callback
1192  */
1193 int
1194 netvsc_recv(struct hv_device *device_ctx, netvsc_packet *packet,
1195     rndis_tcp_ip_csum_info *csum_info)
1196 {
1197         hn_softc_t *sc = (hn_softc_t *)device_get_softc(device_ctx->device);
1198         struct mbuf *m_new;
1199         struct ifnet *ifp;
1200         device_t dev = device_ctx->device;
1201         int size, do_lro = 0, do_csum = 1;
1202
1203         if (sc == NULL) {
1204                 return (0); /* TODO: KYS how can this be! */
1205         }
1206
1207         ifp = sc->hn_ifp;
1208         
1209         ifp = sc->arpcom.ac_ifp;
1210
1211         if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1212                 return (0);
1213         }
1214
1215         /*
1216          * Bail out if packet contains more data than configured MTU.
1217          */
1218         if (packet->tot_data_buf_len > (ifp->if_mtu + ETHER_HDR_LEN)) {
1219                 return (0);
1220         } else if (packet->tot_data_buf_len <= MHLEN) {
1221                 m_new = m_gethdr(M_NOWAIT, MT_DATA);
1222                 if (m_new == NULL)
1223                         return (0);
1224                 memcpy(mtod(m_new, void *), packet->data,
1225                     packet->tot_data_buf_len);
1226                 m_new->m_pkthdr.len = m_new->m_len = packet->tot_data_buf_len;
1227                 sc->hn_small_pkts++;
1228         } else {
1229                 /*
1230                  * Get an mbuf with a cluster.  For packets 2K or less,
1231                  * get a standard 2K cluster.  For anything larger, get a
1232                  * 4K cluster.  Any buffers larger than 4K can cause problems
1233                  * if looped around to the Hyper-V TX channel, so avoid them.
1234                  */
1235                 size = MCLBYTES;
1236                 if (packet->tot_data_buf_len > MCLBYTES) {
1237                         /* 4096 */
1238                         size = MJUMPAGESIZE;
1239                 }
1240
1241                 m_new = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, size);
1242                 if (m_new == NULL) {
1243                         device_printf(dev, "alloc mbuf failed.\n");
1244                         return (0);
1245                 }
1246
1247                 hv_m_append(m_new, packet->tot_data_buf_len, packet->data);
1248         }
1249         m_new->m_pkthdr.rcvif = ifp;
1250
1251         if (__predict_false((ifp->if_capenable & IFCAP_RXCSUM) == 0))
1252                 do_csum = 0;
1253
1254         /* receive side checksum offload */
1255         if (csum_info != NULL) {
1256                 /* IP csum offload */
1257                 if (csum_info->receive.ip_csum_succeeded && do_csum) {
1258                         m_new->m_pkthdr.csum_flags |=
1259                             (CSUM_IP_CHECKED | CSUM_IP_VALID);
1260                         sc->hn_csum_ip++;
1261                 }
1262
1263                 /* TCP/UDP csum offload */
1264                 if ((csum_info->receive.tcp_csum_succeeded ||
1265                      csum_info->receive.udp_csum_succeeded) && do_csum) {
1266                         m_new->m_pkthdr.csum_flags |=
1267                             (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
1268                         m_new->m_pkthdr.csum_data = 0xffff;
1269                         if (csum_info->receive.tcp_csum_succeeded)
1270                                 sc->hn_csum_tcp++;
1271                         else
1272                                 sc->hn_csum_udp++;
1273                 }
1274
1275                 if (csum_info->receive.ip_csum_succeeded &&
1276                     csum_info->receive.tcp_csum_succeeded)
1277                         do_lro = 1;
1278         } else {
1279                 const struct ether_header *eh;
1280                 uint16_t etype;
1281                 int hoff;
1282
1283                 hoff = sizeof(*eh);
1284                 if (m_new->m_len < hoff)
1285                         goto skip;
1286                 eh = mtod(m_new, struct ether_header *);
1287                 etype = ntohs(eh->ether_type);
1288                 if (etype == ETHERTYPE_VLAN) {
1289                         const struct ether_vlan_header *evl;
1290
1291                         hoff = sizeof(*evl);
1292                         if (m_new->m_len < hoff)
1293                                 goto skip;
1294                         evl = mtod(m_new, struct ether_vlan_header *);
1295                         etype = ntohs(evl->evl_proto);
1296                 }
1297
1298                 if (etype == ETHERTYPE_IP) {
1299                         int pr;
1300
1301                         pr = hn_check_iplen(m_new, hoff);
1302                         if (pr == IPPROTO_TCP) {
1303                                 if (do_csum &&
1304                                     (sc->hn_trust_hcsum & HN_TRUST_HCSUM_TCP)) {
1305                                         sc->hn_csum_trusted++;
1306                                         m_new->m_pkthdr.csum_flags |=
1307                                            (CSUM_IP_CHECKED | CSUM_IP_VALID |
1308                                             CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
1309                                         m_new->m_pkthdr.csum_data = 0xffff;
1310                                 }
1311                                 /* Rely on SW csum verification though... */
1312                                 do_lro = 1;
1313                         } else if (pr == IPPROTO_UDP) {
1314                                 if (do_csum &&
1315                                     (sc->hn_trust_hcsum & HN_TRUST_HCSUM_UDP)) {
1316                                         sc->hn_csum_trusted++;
1317                                         m_new->m_pkthdr.csum_flags |=
1318                                            (CSUM_IP_CHECKED | CSUM_IP_VALID |
1319                                             CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
1320                                         m_new->m_pkthdr.csum_data = 0xffff;
1321                                 }
1322                         } else if (pr != IPPROTO_DONE && do_csum &&
1323                             (sc->hn_trust_hcsum & HN_TRUST_HCSUM_IP)) {
1324                                 sc->hn_csum_trusted++;
1325                                 m_new->m_pkthdr.csum_flags |=
1326                                     (CSUM_IP_CHECKED | CSUM_IP_VALID);
1327                         }
1328                 }
1329         }
1330 skip:
1331         if ((packet->vlan_tci != 0) &&
1332             (ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0) {
1333                 m_new->m_pkthdr.ether_vtag = packet->vlan_tci;
1334                 m_new->m_flags |= M_VLANTAG;
1335         }
1336
1337         /*
1338          * Note:  Moved RX completion back to hv_nv_on_receive() so all
1339          * messages (not just data messages) will trigger a response.
1340          */
1341
1342         ifp->if_ipackets++;
1343
1344         if ((ifp->if_capenable & IFCAP_LRO) && do_lro) {
1345 #if defined(INET) || defined(INET6)
1346                 struct lro_ctrl *lro = &sc->hn_lro;
1347
1348                 if (lro->lro_cnt) {
1349                         sc->hn_lro_tried++;
1350                         if (tcp_lro_rx(lro, m_new, 0) == 0) {
1351                                 /* DONE! */
1352                                 return 0;
1353                         }
1354                 }
1355 #endif
1356         }
1357
1358         /* We're not holding the lock here, so don't release it */
1359         (*ifp->if_input)(ifp, m_new);
1360
1361         return (0);
1362 }
1363
1364 void
1365 netvsc_recv_rollup(struct hv_device *device_ctx __unused)
1366 {
1367 }
1368
1369 /*
1370  * Rules for using sc->temp_unusable:
1371  * 1.  sc->temp_unusable can only be read or written while holding NV_LOCK()
1372  * 2.  code reading sc->temp_unusable under NV_LOCK(), and finding 
1373  *     sc->temp_unusable set, must release NV_LOCK() and exit
1374  * 3.  to retain exclusive control of the interface,
1375  *     sc->temp_unusable must be set by code before releasing NV_LOCK()
1376  * 4.  only code setting sc->temp_unusable can clear sc->temp_unusable
1377  * 5.  code setting sc->temp_unusable must eventually clear sc->temp_unusable
1378  */
1379
1380 /*
1381  * Standard ioctl entry point.  Called when the user wants to configure
1382  * the interface.
1383  */
1384 static int
1385 hn_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1386 {
1387         hn_softc_t *sc = ifp->if_softc;
1388         struct ifreq *ifr = (struct ifreq *)data;
1389 #ifdef INET
1390         struct ifaddr *ifa = (struct ifaddr *)data;
1391 #endif
1392         netvsc_device_info device_info;
1393         struct hv_device *hn_dev;
1394         int mask, error = 0;
1395         int retry_cnt = 500;
1396         
1397         switch(cmd) {
1398
1399         case SIOCSIFADDR:
1400 #ifdef INET
1401                 if (ifa->ifa_addr->sa_family == AF_INET) {
1402                         ifp->if_flags |= IFF_UP;
1403                         if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
1404                                 hn_ifinit(sc);
1405                         arp_ifinit(ifp, ifa);
1406                 } else
1407 #endif
1408                 error = ether_ioctl(ifp, cmd, data);
1409                 break;
1410         case SIOCSIFMTU:
1411                 hn_dev = vmbus_get_devctx(sc->hn_dev);
1412
1413                 /* Check MTU value change */
1414                 if (ifp->if_mtu == ifr->ifr_mtu)
1415                         break;
1416
1417                 if (ifr->ifr_mtu > NETVSC_MAX_CONFIGURABLE_MTU) {
1418                         error = EINVAL;
1419                         break;
1420                 }
1421
1422                 /* Obtain and record requested MTU */
1423                 ifp->if_mtu = ifr->ifr_mtu;
1424
1425 #if __FreeBSD_version >= 1100099
1426                 /*
1427                  * Make sure that LRO aggregation length limit is still
1428                  * valid, after the MTU change.
1429                  */
1430                 if (sc->hn_lro.lro_length_lim < HN_LRO_LENLIM_MIN(ifp))
1431                         sc->hn_lro.lro_length_lim = HN_LRO_LENLIM_MIN(ifp);
1432 #endif
1433
1434                 do {
1435                         NV_LOCK(sc);
1436                         if (!sc->temp_unusable) {
1437                                 sc->temp_unusable = TRUE;
1438                                 retry_cnt = -1;
1439                         }
1440                         NV_UNLOCK(sc);
1441                         if (retry_cnt > 0) {
1442                                 retry_cnt--;
1443                                 DELAY(5 * 1000);
1444                         }
1445                 } while (retry_cnt > 0);
1446
1447                 if (retry_cnt == 0) {
1448                         error = EINVAL;
1449                         break;
1450                 }
1451
1452                 /* We must remove and add back the device to cause the new
1453                  * MTU to take effect.  This includes tearing down, but not
1454                  * deleting the channel, then bringing it back up.
1455                  */
1456                 error = hv_rf_on_device_remove(hn_dev, HV_RF_NV_RETAIN_CHANNEL);
1457                 if (error) {
1458                         NV_LOCK(sc);
1459                         sc->temp_unusable = FALSE;
1460                         NV_UNLOCK(sc);
1461                         break;
1462                 }
1463                 error = hv_rf_on_device_add(hn_dev, &device_info);
1464                 if (error) {
1465                         NV_LOCK(sc);
1466                         sc->temp_unusable = FALSE;
1467                         NV_UNLOCK(sc);
1468                         break;
1469                 }
1470
1471                 sc->hn_tx_chimney_max = sc->net_dev->send_section_size;
1472                 if (sc->hn_tx_chimney_size > sc->hn_tx_chimney_max)
1473                         sc->hn_tx_chimney_size = sc->hn_tx_chimney_max;
1474                 hn_ifinit_locked(sc);
1475
1476                 NV_LOCK(sc);
1477                 sc->temp_unusable = FALSE;
1478                 NV_UNLOCK(sc);
1479                 break;
1480         case SIOCSIFFLAGS:
1481                 do {
1482                        NV_LOCK(sc);
1483                        if (!sc->temp_unusable) {
1484                                sc->temp_unusable = TRUE;
1485                                retry_cnt = -1;
1486                        }
1487                        NV_UNLOCK(sc);
1488                        if (retry_cnt > 0) {
1489                                 retry_cnt--;
1490                                 DELAY(5 * 1000);
1491                        }
1492                 } while (retry_cnt > 0);
1493
1494                 if (retry_cnt == 0) {
1495                        error = EINVAL;
1496                        break;
1497                 }
1498
1499                 if (ifp->if_flags & IFF_UP) {
1500                         /*
1501                          * If only the state of the PROMISC flag changed,
1502                          * then just use the 'set promisc mode' command
1503                          * instead of reinitializing the entire NIC. Doing
1504                          * a full re-init means reloading the firmware and
1505                          * waiting for it to start up, which may take a
1506                          * second or two.
1507                          */
1508 #ifdef notyet
1509                         /* Fixme:  Promiscuous mode? */
1510                         if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
1511                             ifp->if_flags & IFF_PROMISC &&
1512                             !(sc->hn_if_flags & IFF_PROMISC)) {
1513                                 /* do something here for Hyper-V */
1514                         } else if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
1515                             !(ifp->if_flags & IFF_PROMISC) &&
1516                             sc->hn_if_flags & IFF_PROMISC) {
1517                                 /* do something here for Hyper-V */
1518                         } else
1519 #endif
1520                                 hn_ifinit_locked(sc);
1521                 } else {
1522                         if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1523                                 hn_stop(sc);
1524                         }
1525                 }
1526                 NV_LOCK(sc);
1527                 sc->temp_unusable = FALSE;
1528                 NV_UNLOCK(sc);
1529                 sc->hn_if_flags = ifp->if_flags;
1530                 error = 0;
1531                 break;
1532         case SIOCSIFCAP:
1533                 NV_LOCK(sc);
1534
1535                 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1536                 if (mask & IFCAP_TXCSUM) {
1537                         ifp->if_capenable ^= IFCAP_TXCSUM;
1538                         if (ifp->if_capenable & IFCAP_TXCSUM)
1539                                 ifp->if_hwassist |= sc->hn_csum_assist;
1540                         else
1541                                 ifp->if_hwassist &= ~sc->hn_csum_assist;
1542                 }
1543
1544                 if (mask & IFCAP_RXCSUM)
1545                         ifp->if_capenable ^= IFCAP_RXCSUM;
1546
1547                 if (mask & IFCAP_LRO)
1548                         ifp->if_capenable ^= IFCAP_LRO;
1549
1550                 if (mask & IFCAP_TSO4) {
1551                         ifp->if_capenable ^= IFCAP_TSO4;
1552                         if (ifp->if_capenable & IFCAP_TSO4)
1553                                 ifp->if_hwassist |= CSUM_IP_TSO;
1554                         else
1555                                 ifp->if_hwassist &= ~CSUM_IP_TSO;
1556                 }
1557
1558                 if (mask & IFCAP_TSO6) {
1559                         ifp->if_capenable ^= IFCAP_TSO6;
1560                         if (ifp->if_capenable & IFCAP_TSO6)
1561                                 ifp->if_hwassist |= CSUM_IP6_TSO;
1562                         else
1563                                 ifp->if_hwassist &= ~CSUM_IP6_TSO;
1564                 }
1565
1566                 NV_UNLOCK(sc);
1567                 error = 0;
1568                 break;
1569         case SIOCADDMULTI:
1570         case SIOCDELMULTI:
1571 #ifdef notyet
1572                 /* Fixme:  Multicast mode? */
1573                 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1574                         NV_LOCK(sc);
1575                         netvsc_setmulti(sc);
1576                         NV_UNLOCK(sc);
1577                         error = 0;
1578                 }
1579 #endif
1580                 error = EINVAL;
1581                 break;
1582         case SIOCSIFMEDIA:
1583         case SIOCGIFMEDIA:
1584                 error = ifmedia_ioctl(ifp, ifr, &sc->hn_media, cmd);
1585                 break;
1586         default:
1587                 error = ether_ioctl(ifp, cmd, data);
1588                 break;
1589         }
1590
1591         return (error);
1592 }
1593
1594 /*
1595  *
1596  */
1597 static void
1598 hn_stop(hn_softc_t *sc)
1599 {
1600         struct ifnet *ifp;
1601         int ret;
1602         struct hv_device *device_ctx = vmbus_get_devctx(sc->hn_dev);
1603
1604         ifp = sc->hn_ifp;
1605
1606         if (bootverbose)
1607                 printf(" Closing Device ...\n");
1608
1609         atomic_clear_int(&ifp->if_drv_flags,
1610             (IFF_DRV_RUNNING | IFF_DRV_OACTIVE));
1611         if_link_state_change(ifp, LINK_STATE_DOWN);
1612         sc->hn_initdone = 0;
1613
1614         ret = hv_rf_on_close(device_ctx);
1615 }
1616
1617 /*
1618  * FreeBSD transmit entry point
1619  */
1620 static void
1621 hn_start(struct ifnet *ifp)
1622 {
1623         struct hn_softc *sc = ifp->if_softc;
1624
1625         if (sc->hn_sched_tx)
1626                 goto do_sched;
1627
1628         if (NV_TRYLOCK(sc)) {
1629                 int sched;
1630
1631                 sched = hn_start_locked(ifp, sc->hn_direct_tx_size);
1632                 NV_UNLOCK(sc);
1633                 if (!sched)
1634                         return;
1635         }
1636 do_sched:
1637         taskqueue_enqueue_fast(sc->hn_tx_taskq, &sc->hn_start_task);
1638 }
1639
1640 static void
1641 hn_start_txeof(struct ifnet *ifp)
1642 {
1643         struct hn_softc *sc = ifp->if_softc;
1644
1645         if (sc->hn_sched_tx)
1646                 goto do_sched;
1647
1648         if (NV_TRYLOCK(sc)) {
1649                 int sched;
1650
1651                 atomic_clear_int(&ifp->if_drv_flags, IFF_DRV_OACTIVE);
1652                 sched = hn_start_locked(ifp, sc->hn_direct_tx_size);
1653                 NV_UNLOCK(sc);
1654                 if (sched) {
1655                         taskqueue_enqueue_fast(sc->hn_tx_taskq,
1656                             &sc->hn_start_task);
1657                 }
1658         } else {
1659 do_sched:
1660                 /*
1661                  * Release the OACTIVE earlier, with the hope, that
1662                  * others could catch up.  The task will clear the
1663                  * flag again with the NV_LOCK to avoid possible
1664                  * races.
1665                  */
1666                 atomic_clear_int(&ifp->if_drv_flags, IFF_DRV_OACTIVE);
1667                 taskqueue_enqueue_fast(sc->hn_tx_taskq, &sc->hn_txeof_task);
1668         }
1669 }
1670
1671 /*
1672  *
1673  */
1674 static void
1675 hn_ifinit_locked(hn_softc_t *sc)
1676 {
1677         struct ifnet *ifp;
1678         struct hv_device *device_ctx = vmbus_get_devctx(sc->hn_dev);
1679         int ret;
1680
1681         ifp = sc->hn_ifp;
1682
1683         if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1684                 return;
1685         }
1686
1687         hv_promisc_mode = 1;
1688
1689         ret = hv_rf_on_open(device_ctx);
1690         if (ret != 0) {
1691                 return;
1692         } else {
1693                 sc->hn_initdone = 1;
1694         }
1695         atomic_clear_int(&ifp->if_drv_flags, IFF_DRV_OACTIVE);
1696         atomic_set_int(&ifp->if_drv_flags, IFF_DRV_RUNNING);
1697         if_link_state_change(ifp, LINK_STATE_UP);
1698 }
1699
1700 /*
1701  *
1702  */
1703 static void
1704 hn_ifinit(void *xsc)
1705 {
1706         hn_softc_t *sc = xsc;
1707
1708         NV_LOCK(sc);
1709         if (sc->temp_unusable) {
1710                 NV_UNLOCK(sc);
1711                 return;
1712         }
1713         sc->temp_unusable = TRUE;
1714         NV_UNLOCK(sc);
1715
1716         hn_ifinit_locked(sc);
1717
1718         NV_LOCK(sc);
1719         sc->temp_unusable = FALSE;
1720         NV_UNLOCK(sc);
1721 }
1722
1723 #ifdef LATER
1724 /*
1725  *
1726  */
1727 static void
1728 hn_watchdog(struct ifnet *ifp)
1729 {
1730         hn_softc_t *sc;
1731         sc = ifp->if_softc;
1732
1733         printf("hn%d: watchdog timeout -- resetting\n", sc->hn_unit);
1734         hn_ifinit(sc);    /*???*/
1735         ifp->if_oerrors++;
1736 }
1737 #endif
1738
1739 #if __FreeBSD_version >= 1100099
1740
1741 static int
1742 hn_lro_lenlim_sysctl(SYSCTL_HANDLER_ARGS)
1743 {
1744         struct hn_softc *sc = arg1;
1745         unsigned int lenlim;
1746         int error;
1747
1748         lenlim = sc->hn_lro.lro_length_lim;
1749         error = sysctl_handle_int(oidp, &lenlim, 0, req);
1750         if (error || req->newptr == NULL)
1751                 return error;
1752
1753         if (lenlim < HN_LRO_LENLIM_MIN(sc->hn_ifp) ||
1754             lenlim > TCP_LRO_LENGTH_MAX)
1755                 return EINVAL;
1756
1757         sc->hn_lro.lro_length_lim = lenlim;
1758         return 0;
1759 }
1760
1761 static int
1762 hn_lro_ackcnt_sysctl(SYSCTL_HANDLER_ARGS)
1763 {
1764         struct hn_softc *sc = arg1;
1765         int ackcnt, error;
1766
1767         /*
1768          * lro_ackcnt_lim is append count limit,
1769          * +1 to turn it into aggregation limit.
1770          */
1771         ackcnt = sc->hn_lro.lro_ackcnt_lim + 1;
1772         error = sysctl_handle_int(oidp, &ackcnt, 0, req);
1773         if (error || req->newptr == NULL)
1774                 return error;
1775
1776         if (ackcnt < 2 || ackcnt > (TCP_LRO_ACKCNT_MAX + 1))
1777                 return EINVAL;
1778
1779         /*
1780          * Convert aggregation limit back to append
1781          * count limit.
1782          */
1783         sc->hn_lro.lro_ackcnt_lim = ackcnt - 1;
1784         return 0;
1785 }
1786
1787 #endif
1788
1789 static int
1790 hn_trust_hcsum_sysctl(SYSCTL_HANDLER_ARGS)
1791 {
1792         struct hn_softc *sc = arg1;
1793         int hcsum = arg2;
1794         int on, error;
1795
1796         on = 0;
1797         if (sc->hn_trust_hcsum & hcsum)
1798                 on = 1;
1799
1800         error = sysctl_handle_int(oidp, &on, 0, req);
1801         if (error || req->newptr == NULL)
1802                 return error;
1803
1804         NV_LOCK(sc);
1805         if (on)
1806                 sc->hn_trust_hcsum |= hcsum;
1807         else
1808                 sc->hn_trust_hcsum &= ~hcsum;
1809         NV_UNLOCK(sc);
1810         return 0;
1811 }
1812
1813 static int
1814 hn_tx_chimney_size_sysctl(SYSCTL_HANDLER_ARGS)
1815 {
1816         struct hn_softc *sc = arg1;
1817         int chimney_size, error;
1818
1819         chimney_size = sc->hn_tx_chimney_size;
1820         error = sysctl_handle_int(oidp, &chimney_size, 0, req);
1821         if (error || req->newptr == NULL)
1822                 return error;
1823
1824         if (chimney_size > sc->hn_tx_chimney_max || chimney_size <= 0)
1825                 return EINVAL;
1826
1827         if (sc->hn_tx_chimney_size != chimney_size)
1828                 sc->hn_tx_chimney_size = chimney_size;
1829         return 0;
1830 }
1831
1832 static int
1833 hn_check_iplen(const struct mbuf *m, int hoff)
1834 {
1835         const struct ip *ip;
1836         int len, iphlen, iplen;
1837         const struct tcphdr *th;
1838         int thoff;                              /* TCP data offset */
1839
1840         len = hoff + sizeof(struct ip);
1841
1842         /* The packet must be at least the size of an IP header. */
1843         if (m->m_pkthdr.len < len)
1844                 return IPPROTO_DONE;
1845
1846         /* The fixed IP header must reside completely in the first mbuf. */
1847         if (m->m_len < len)
1848                 return IPPROTO_DONE;
1849
1850         ip = mtodo(m, hoff);
1851
1852         /* Bound check the packet's stated IP header length. */
1853         iphlen = ip->ip_hl << 2;
1854         if (iphlen < sizeof(struct ip))         /* minimum header length */
1855                 return IPPROTO_DONE;
1856
1857         /* The full IP header must reside completely in the one mbuf. */
1858         if (m->m_len < hoff + iphlen)
1859                 return IPPROTO_DONE;
1860
1861         iplen = ntohs(ip->ip_len);
1862
1863         /*
1864          * Check that the amount of data in the buffers is as
1865          * at least much as the IP header would have us expect.
1866          */
1867         if (m->m_pkthdr.len < hoff + iplen)
1868                 return IPPROTO_DONE;
1869
1870         /*
1871          * Ignore IP fragments.
1872          */
1873         if (ntohs(ip->ip_off) & (IP_OFFMASK | IP_MF))
1874                 return IPPROTO_DONE;
1875
1876         /*
1877          * The TCP/IP or UDP/IP header must be entirely contained within
1878          * the first fragment of a packet.
1879          */
1880         switch (ip->ip_p) {
1881         case IPPROTO_TCP:
1882                 if (iplen < iphlen + sizeof(struct tcphdr))
1883                         return IPPROTO_DONE;
1884                 if (m->m_len < hoff + iphlen + sizeof(struct tcphdr))
1885                         return IPPROTO_DONE;
1886                 th = (const struct tcphdr *)((const uint8_t *)ip + iphlen);
1887                 thoff = th->th_off << 2;
1888                 if (thoff < sizeof(struct tcphdr) || thoff + iphlen > iplen)
1889                         return IPPROTO_DONE;
1890                 if (m->m_len < hoff + iphlen + thoff)
1891                         return IPPROTO_DONE;
1892                 break;
1893         case IPPROTO_UDP:
1894                 if (iplen < iphlen + sizeof(struct udphdr))
1895                         return IPPROTO_DONE;
1896                 if (m->m_len < hoff + iphlen + sizeof(struct udphdr))
1897                         return IPPROTO_DONE;
1898                 break;
1899         default:
1900                 if (iplen < iphlen)
1901                         return IPPROTO_DONE;
1902                 break;
1903         }
1904         return ip->ip_p;
1905 }
1906
1907 static void
1908 hn_dma_map_paddr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1909 {
1910         bus_addr_t *paddr = arg;
1911
1912         if (error)
1913                 return;
1914
1915         KASSERT(nseg == 1, ("too many segments %d!", nseg));
1916         *paddr = segs->ds_addr;
1917 }
1918
1919 static int
1920 hn_create_tx_ring(struct hn_softc *sc)
1921 {
1922         bus_dma_tag_t parent_dtag;
1923         int error, i;
1924
1925         sc->hn_txdesc_cnt = HN_TX_DESC_CNT;
1926         sc->hn_txdesc = malloc(sizeof(struct hn_txdesc) * sc->hn_txdesc_cnt,
1927             M_NETVSC, M_WAITOK | M_ZERO);
1928         SLIST_INIT(&sc->hn_txlist);
1929         mtx_init(&sc->hn_txlist_spin, "hn txlist", NULL, MTX_SPIN);
1930
1931         parent_dtag = bus_get_dma_tag(sc->hn_dev);
1932
1933         /* DMA tag for RNDIS messages. */
1934         error = bus_dma_tag_create(parent_dtag, /* parent */
1935             HN_RNDIS_MSG_ALIGN,         /* alignment */
1936             HN_RNDIS_MSG_BOUNDARY,      /* boundary */
1937             BUS_SPACE_MAXADDR,          /* lowaddr */
1938             BUS_SPACE_MAXADDR,          /* highaddr */
1939             NULL, NULL,                 /* filter, filterarg */
1940             HN_RNDIS_MSG_LEN,           /* maxsize */
1941             1,                          /* nsegments */
1942             HN_RNDIS_MSG_LEN,           /* maxsegsize */
1943             0,                          /* flags */
1944             NULL,                       /* lockfunc */
1945             NULL,                       /* lockfuncarg */
1946             &sc->hn_tx_rndis_dtag);
1947         if (error) {
1948                 device_printf(sc->hn_dev, "failed to create rndis dmatag\n");
1949                 return error;
1950         }
1951
1952         /* DMA tag for data. */
1953         error = bus_dma_tag_create(parent_dtag, /* parent */
1954             1,                          /* alignment */
1955             HN_TX_DATA_BOUNDARY,        /* boundary */
1956             BUS_SPACE_MAXADDR,          /* lowaddr */
1957             BUS_SPACE_MAXADDR,          /* highaddr */
1958             NULL, NULL,                 /* filter, filterarg */
1959             HN_TX_DATA_MAXSIZE,         /* maxsize */
1960             HN_TX_DATA_SEGCNT_MAX,      /* nsegments */
1961             HN_TX_DATA_SEGSIZE,         /* maxsegsize */
1962             0,                          /* flags */
1963             NULL,                       /* lockfunc */
1964             NULL,                       /* lockfuncarg */
1965             &sc->hn_tx_data_dtag);
1966         if (error) {
1967                 device_printf(sc->hn_dev, "failed to create data dmatag\n");
1968                 return error;
1969         }
1970
1971         for (i = 0; i < sc->hn_txdesc_cnt; ++i) {
1972                 struct hn_txdesc *txd = &sc->hn_txdesc[i];
1973
1974                 txd->sc = sc;
1975
1976                 /*
1977                  * Allocate and load RNDIS messages.
1978                  */
1979                 error = bus_dmamem_alloc(sc->hn_tx_rndis_dtag,
1980                     (void **)&txd->rndis_msg,
1981                     BUS_DMA_WAITOK | BUS_DMA_COHERENT,
1982                     &txd->rndis_msg_dmap);
1983                 if (error) {
1984                         device_printf(sc->hn_dev,
1985                             "failed to allocate rndis_msg, %d\n", i);
1986                         return error;
1987                 }
1988
1989                 error = bus_dmamap_load(sc->hn_tx_rndis_dtag,
1990                     txd->rndis_msg_dmap,
1991                     txd->rndis_msg, HN_RNDIS_MSG_LEN,
1992                     hn_dma_map_paddr, &txd->rndis_msg_paddr,
1993                     BUS_DMA_NOWAIT);
1994                 if (error) {
1995                         device_printf(sc->hn_dev,
1996                             "failed to load rndis_msg, %d\n", i);
1997                         bus_dmamem_free(sc->hn_tx_rndis_dtag,
1998                             txd->rndis_msg, txd->rndis_msg_dmap);
1999                         return error;
2000                 }
2001
2002                 /* DMA map for TX data. */
2003                 error = bus_dmamap_create(sc->hn_tx_data_dtag, 0,
2004                     &txd->data_dmap);
2005                 if (error) {
2006                         device_printf(sc->hn_dev,
2007                             "failed to allocate tx data dmamap\n");
2008                         bus_dmamap_unload(sc->hn_tx_rndis_dtag,
2009                             txd->rndis_msg_dmap);
2010                         bus_dmamem_free(sc->hn_tx_rndis_dtag,
2011                             txd->rndis_msg, txd->rndis_msg_dmap);
2012                         return error;
2013                 }
2014
2015                 /* All set, put it to list */
2016                 txd->flags |= HN_TXD_FLAG_ONLIST;
2017                 SLIST_INSERT_HEAD(&sc->hn_txlist, txd, link);
2018         }
2019         sc->hn_txdesc_avail = sc->hn_txdesc_cnt;
2020
2021         return 0;
2022 }
2023
2024 static void
2025 hn_destroy_tx_ring(struct hn_softc *sc)
2026 {
2027         struct hn_txdesc *txd;
2028
2029         while ((txd = SLIST_FIRST(&sc->hn_txlist)) != NULL) {
2030                 KASSERT(txd->m == NULL, ("still has mbuf installed"));
2031                 KASSERT((txd->flags & HN_TXD_FLAG_DMAMAP) == 0,
2032                     ("still dma mapped"));
2033                 SLIST_REMOVE_HEAD(&sc->hn_txlist, link);
2034
2035                 bus_dmamap_unload(sc->hn_tx_rndis_dtag,
2036                     txd->rndis_msg_dmap);
2037                 bus_dmamem_free(sc->hn_tx_rndis_dtag,
2038                     txd->rndis_msg, txd->rndis_msg_dmap);
2039
2040                 bus_dmamap_destroy(sc->hn_tx_data_dtag, txd->data_dmap);
2041         }
2042
2043         if (sc->hn_tx_data_dtag != NULL)
2044                 bus_dma_tag_destroy(sc->hn_tx_data_dtag);
2045         if (sc->hn_tx_rndis_dtag != NULL)
2046                 bus_dma_tag_destroy(sc->hn_tx_rndis_dtag);
2047         free(sc->hn_txdesc, M_NETVSC);
2048         mtx_destroy(&sc->hn_txlist_spin);
2049 }
2050
2051 static void
2052 hn_start_taskfunc(void *xsc, int pending __unused)
2053 {
2054         struct hn_softc *sc = xsc;
2055
2056         NV_LOCK(sc);
2057         hn_start_locked(sc->hn_ifp, 0);
2058         NV_UNLOCK(sc);
2059 }
2060
2061 static void
2062 hn_txeof_taskfunc(void *xsc, int pending __unused)
2063 {
2064         struct hn_softc *sc = xsc;
2065         struct ifnet *ifp = sc->hn_ifp;
2066
2067         NV_LOCK(sc);
2068         atomic_clear_int(&ifp->if_drv_flags, IFF_DRV_OACTIVE);
2069         hn_start_locked(ifp, 0);
2070         NV_UNLOCK(sc);
2071 }
2072
2073 static void
2074 hn_tx_taskq_create(void *arg __unused)
2075 {
2076         if (!hn_share_tx_taskq)
2077                 return;
2078
2079         hn_tx_taskq = taskqueue_create_fast("hn_tx", M_WAITOK,
2080             taskqueue_thread_enqueue, &hn_tx_taskq);
2081         taskqueue_start_threads(&hn_tx_taskq, 1, PI_NET, "hn tx");
2082 }
2083 SYSINIT(hn_txtq_create, SI_SUB_DRIVERS, SI_ORDER_FIRST,
2084     hn_tx_taskq_create, NULL);
2085
2086 static void
2087 hn_tx_taskq_destroy(void *arg __unused)
2088 {
2089         if (hn_tx_taskq != NULL)
2090                 taskqueue_free(hn_tx_taskq);
2091 }
2092 SYSUNINIT(hn_txtq_destroy, SI_SUB_DRIVERS, SI_ORDER_FIRST,
2093     hn_tx_taskq_destroy, NULL);
2094
2095 static device_method_t netvsc_methods[] = {
2096         /* Device interface */
2097         DEVMETHOD(device_probe,         netvsc_probe),
2098         DEVMETHOD(device_attach,        netvsc_attach),
2099         DEVMETHOD(device_detach,        netvsc_detach),
2100         DEVMETHOD(device_shutdown,      netvsc_shutdown),
2101
2102         { 0, 0 }
2103 };
2104
2105 static driver_t netvsc_driver = {
2106         NETVSC_DEVNAME,
2107         netvsc_methods,
2108         sizeof(hn_softc_t)
2109 };
2110
2111 static devclass_t netvsc_devclass;
2112
2113 DRIVER_MODULE(hn, vmbus, netvsc_driver, netvsc_devclass, 0, 0);
2114 MODULE_VERSION(hn, 1);
2115 MODULE_DEPEND(hn, vmbus, 1, 1, 1);