2 * Copyright (c) 2010-2012 Citrix Inc.
3 * Copyright (c) 2009-2012 Microsoft Corp.
4 * Copyright (c) 2012 NetApp Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
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.
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.
30 * Copyright (c) 2004-2006 Kip Macy
31 * All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
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.
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
55 #include <sys/cdefs.h>
56 __FBSDID("$FreeBSD$");
58 #include "opt_inet6.h"
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/sockio.h>
65 #include <sys/malloc.h>
66 #include <sys/module.h>
67 #include <sys/kernel.h>
68 #include <sys/socket.h>
70 #include <sys/queue.h>
73 #include <sys/sysctl.h>
74 #include <sys/buf_ring.h>
77 #include <net/if_arp.h>
78 #include <net/ethernet.h>
79 #include <net/if_dl.h>
80 #include <net/if_media.h>
84 #include <net/if_types.h>
85 #include <net/if_vlan_var.h>
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>
97 #include <vm/vm_param.h>
98 #include <vm/vm_kern.h>
101 #include <machine/bus.h>
102 #include <machine/resource.h>
103 #include <machine/frame.h>
104 #include <machine/vmparam.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>
113 #include <machine/intr_machdep.h>
115 #include <machine/in_cksum.h>
117 #include <dev/hyperv/include/hyperv.h>
118 #include "hv_net_vsc.h"
119 #include "hv_rndis.h"
120 #include "hv_rndis_filter.h"
123 /* Short for Hyper-V network interface */
124 #define NETVSC_DEVNAME "hn"
127 * It looks like offset 0 of buf is reserved to hold the softc pointer.
128 * The sc pointer evidently not needed, and is not presently populated.
129 * The packet offset is where the netvsc_packet starts in the buffer.
131 #define HV_NV_SC_PTR_OFFSET_IN_BUF 0
132 #define HV_NV_PACKET_OFFSET_IN_BUF 16
134 /* YYY should get it from the underlying channel */
135 #define HN_TX_DESC_CNT 512
137 #define HN_LROENT_CNT_DEF 128
139 #define HN_RNDIS_MSG_LEN \
140 (sizeof(rndis_msg) + \
141 RNDIS_VLAN_PPI_SIZE + \
142 RNDIS_TSO_PPI_SIZE + \
144 #define HN_RNDIS_MSG_BOUNDARY PAGE_SIZE
145 #define HN_RNDIS_MSG_ALIGN CACHE_LINE_SIZE
147 #define HN_TX_DATA_BOUNDARY PAGE_SIZE
148 #define HN_TX_DATA_MAXSIZE IP_MAXPACKET
149 #define HN_TX_DATA_SEGSIZE PAGE_SIZE
150 #define HN_TX_DATA_SEGCNT_MAX \
151 (NETVSC_PACKET_MAXPAGE - HV_RF_NUM_TX_RESERVED_PAGE_BUFS)
153 #define HN_DIRECT_TX_SIZE_DEF 128
156 #ifndef HN_USE_TXDESC_BUFRING
157 SLIST_ENTRY(hn_txdesc) link;
160 struct hn_tx_ring *txr;
162 uint32_t flags; /* HN_TXD_FLAG_ */
163 netvsc_packet netvsc_pkt; /* XXX to be removed */
165 bus_dmamap_t data_dmap;
167 bus_addr_t rndis_msg_paddr;
168 rndis_msg *rndis_msg;
169 bus_dmamap_t rndis_msg_dmap;
172 #define HN_TXD_FLAG_ONLIST 0x1
173 #define HN_TXD_FLAG_DMAMAP 0x2
176 * Only enable UDP checksum offloading when it is on 2012R2 or
177 * later. UDP checksum offloading doesn't work on earlier
180 #define HN_CSUM_ASSIST_WIN8 (CSUM_IP | CSUM_TCP)
181 #define HN_CSUM_ASSIST (CSUM_IP | CSUM_UDP | CSUM_TCP)
183 #define HN_LRO_LENLIM_DEF (25 * ETHERMTU)
184 /* YYY 2*MTU is a bit rough, but should be good enough. */
185 #define HN_LRO_LENLIM_MIN(ifp) (2 * (ifp)->if_mtu)
187 #define HN_LRO_ACKCNT_DEF 1
190 * Be aware that this sleepable mutex will exhibit WITNESS errors when
191 * certain TCP and ARP code paths are taken. This appears to be a
192 * well-known condition, as all other drivers checked use a sleeping
193 * mutex to protect their transmit paths.
194 * Also Be aware that mutexes do not play well with semaphores, and there
195 * is a conflicting semaphore in a certain channel code path.
197 #define NV_LOCK_INIT(_sc, _name) \
198 mtx_init(&(_sc)->hn_lock, _name, MTX_NETWORK_LOCK, MTX_DEF)
199 #define NV_LOCK(_sc) mtx_lock(&(_sc)->hn_lock)
200 #define NV_LOCK_ASSERT(_sc) mtx_assert(&(_sc)->hn_lock, MA_OWNED)
201 #define NV_UNLOCK(_sc) mtx_unlock(&(_sc)->hn_lock)
202 #define NV_LOCK_DESTROY(_sc) mtx_destroy(&(_sc)->hn_lock)
209 int hv_promisc_mode = 0; /* normal mode by default */
211 SYSCTL_NODE(_hw, OID_AUTO, hn, CTLFLAG_RD, NULL, "Hyper-V network interface");
213 /* Trust tcp segements verification on host side. */
214 static int hn_trust_hosttcp = 1;
215 SYSCTL_INT(_hw_hn, OID_AUTO, trust_hosttcp, CTLFLAG_RDTUN,
216 &hn_trust_hosttcp, 0,
217 "Trust tcp segement verification on host side, "
218 "when csum info is missing (global setting)");
220 /* Trust udp datagrams verification on host side. */
221 static int hn_trust_hostudp = 1;
222 SYSCTL_INT(_hw_hn, OID_AUTO, trust_hostudp, CTLFLAG_RDTUN,
223 &hn_trust_hostudp, 0,
224 "Trust udp datagram verification on host side, "
225 "when csum info is missing (global setting)");
227 /* Trust ip packets verification on host side. */
228 static int hn_trust_hostip = 1;
229 SYSCTL_INT(_hw_hn, OID_AUTO, trust_hostip, CTLFLAG_RDTUN,
231 "Trust ip packet verification on host side, "
232 "when csum info is missing (global setting)");
234 #if __FreeBSD_version >= 1100045
235 /* Limit TSO burst size */
236 static int hn_tso_maxlen = 0;
237 SYSCTL_INT(_hw_hn, OID_AUTO, tso_maxlen, CTLFLAG_RDTUN,
238 &hn_tso_maxlen, 0, "TSO burst limit");
241 /* Limit chimney send size */
242 static int hn_tx_chimney_size = 0;
243 SYSCTL_INT(_hw_hn, OID_AUTO, tx_chimney_size, CTLFLAG_RDTUN,
244 &hn_tx_chimney_size, 0, "Chimney send packet size limit");
246 /* Limit the size of packet for direct transmission */
247 static int hn_direct_tx_size = HN_DIRECT_TX_SIZE_DEF;
248 SYSCTL_INT(_hw_hn, OID_AUTO, direct_tx_size, CTLFLAG_RDTUN,
249 &hn_direct_tx_size, 0, "Size of the packet for direct transmission");
251 #if defined(INET) || defined(INET6)
252 #if __FreeBSD_version >= 1100095
253 static int hn_lro_entry_count = HN_LROENT_CNT_DEF;
254 SYSCTL_INT(_hw_hn, OID_AUTO, lro_entry_count, CTLFLAG_RDTUN,
255 &hn_lro_entry_count, 0, "LRO entry count");
259 static int hn_share_tx_taskq = 0;
260 SYSCTL_INT(_hw_hn, OID_AUTO, share_tx_taskq, CTLFLAG_RDTUN,
261 &hn_share_tx_taskq, 0, "Enable shared TX taskqueue");
263 static struct taskqueue *hn_tx_taskq;
265 #ifndef HN_USE_TXDESC_BUFRING
266 static int hn_use_txdesc_bufring = 0;
268 static int hn_use_txdesc_bufring = 1;
270 SYSCTL_INT(_hw_hn, OID_AUTO, use_txdesc_bufring, CTLFLAG_RD,
271 &hn_use_txdesc_bufring, 0, "Use buf_ring for TX descriptors");
273 static int hn_bind_tx_taskq = -1;
274 SYSCTL_INT(_hw_hn, OID_AUTO, bind_tx_taskq, CTLFLAG_RDTUN,
275 &hn_bind_tx_taskq, 0, "Bind TX taskqueue to the specified cpu");
278 * Forward declarations
280 static void hn_stop(hn_softc_t *sc);
281 static void hn_ifinit_locked(hn_softc_t *sc);
282 static void hn_ifinit(void *xsc);
283 static int hn_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
284 static int hn_start_locked(struct hn_tx_ring *txr, int len);
285 static void hn_start(struct ifnet *ifp);
286 static void hn_start_txeof(struct hn_tx_ring *);
287 static int hn_ifmedia_upd(struct ifnet *ifp);
288 static void hn_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr);
289 #if __FreeBSD_version >= 1100099
290 static int hn_lro_lenlim_sysctl(SYSCTL_HANDLER_ARGS);
291 static int hn_lro_ackcnt_sysctl(SYSCTL_HANDLER_ARGS);
293 static int hn_trust_hcsum_sysctl(SYSCTL_HANDLER_ARGS);
294 static int hn_tx_chimney_size_sysctl(SYSCTL_HANDLER_ARGS);
295 static int hn_rx_stat_ulong_sysctl(SYSCTL_HANDLER_ARGS);
296 static int hn_rx_stat_u64_sysctl(SYSCTL_HANDLER_ARGS);
297 static int hn_tx_stat_ulong_sysctl(SYSCTL_HANDLER_ARGS);
298 static int hn_tx_conf_int_sysctl(SYSCTL_HANDLER_ARGS);
299 static int hn_check_iplen(const struct mbuf *, int);
300 static int hn_create_tx_ring(struct hn_softc *, int);
301 static void hn_destroy_tx_ring(struct hn_tx_ring *);
302 static int hn_create_tx_data(struct hn_softc *);
303 static void hn_destroy_tx_data(struct hn_softc *);
304 static void hn_start_taskfunc(void *xsc, int pending);
305 static void hn_txeof_taskfunc(void *xsc, int pending);
306 static void hn_stop_tx_tasks(struct hn_softc *);
307 static int hn_encap(struct hn_tx_ring *, struct hn_txdesc *, struct mbuf **);
308 static void hn_create_rx_data(struct hn_softc *sc);
309 static void hn_destroy_rx_data(struct hn_softc *sc);
310 static void hn_set_tx_chimney_size(struct hn_softc *, int);
313 hn_ifmedia_upd(struct ifnet *ifp __unused)
320 hn_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
322 struct hn_softc *sc = ifp->if_softc;
324 ifmr->ifm_status = IFM_AVALID;
325 ifmr->ifm_active = IFM_ETHER;
327 if (!sc->hn_carrier) {
328 ifmr->ifm_active |= IFM_NONE;
331 ifmr->ifm_status |= IFM_ACTIVE;
332 ifmr->ifm_active |= IFM_10G_T | IFM_FDX;
335 /* {F8615163-DF3E-46c5-913F-F2D2F965ED0E} */
336 static const hv_guid g_net_vsc_device_type = {
337 .data = {0x63, 0x51, 0x61, 0xF8, 0x3E, 0xDF, 0xc5, 0x46,
338 0x91, 0x3F, 0xF2, 0xD2, 0xF9, 0x65, 0xED, 0x0E}
342 * Standard probe entry point.
346 netvsc_probe(device_t dev)
350 p = vmbus_get_type(dev);
351 if (!memcmp(p, &g_net_vsc_device_type.data, sizeof(hv_guid))) {
352 device_set_desc(dev, "Synthetic Network Interface");
354 printf("Netvsc probe... DONE \n");
356 return (BUS_PROBE_DEFAULT);
363 hn_cpuset_setthread_task(void *xmask, int pending __unused)
365 cpuset_t *mask = xmask;
368 error = cpuset_setthread(curthread->td_tid, mask);
370 panic("curthread=%ju: can't pin; error=%d",
371 (uintmax_t)curthread->td_tid, error);
376 * Standard attach entry point.
378 * Called when the driver is loaded. It allocates needed resources,
379 * and initializes the "hardware" and software.
382 netvsc_attach(device_t dev)
384 struct hv_device *device_ctx = vmbus_get_devctx(dev);
385 netvsc_device_info device_info;
387 int unit = device_get_unit(dev);
388 struct ifnet *ifp = NULL;
390 #if __FreeBSD_version >= 1100045
394 sc = device_get_softc(dev);
399 bzero(sc, sizeof(hn_softc_t));
403 if (hn_tx_taskq == NULL) {
404 sc->hn_tx_taskq = taskqueue_create("hn_tx", M_WAITOK,
405 taskqueue_thread_enqueue, &sc->hn_tx_taskq);
406 taskqueue_start_threads(&sc->hn_tx_taskq, 1, PI_NET, "%s tx",
407 device_get_nameunit(dev));
408 if (hn_bind_tx_taskq >= 0) {
409 int cpu = hn_bind_tx_taskq;
410 struct task cpuset_task;
413 if (cpu > mp_ncpus - 1)
415 CPU_SETOF(cpu, &cpu_set);
416 TASK_INIT(&cpuset_task, 0, hn_cpuset_setthread_task,
418 taskqueue_enqueue(sc->hn_tx_taskq, &cpuset_task);
419 taskqueue_drain(sc->hn_tx_taskq, &cpuset_task);
422 sc->hn_tx_taskq = hn_tx_taskq;
424 NV_LOCK_INIT(sc, "NetVSCLock");
426 sc->hn_dev_obj = device_ctx;
428 ifp = sc->hn_ifp = sc->arpcom.ac_ifp = if_alloc(IFT_ETHER);
431 error = hn_create_tx_data(sc);
435 hn_create_rx_data(sc);
437 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
438 ifp->if_dunit = unit;
439 ifp->if_dname = NETVSC_DEVNAME;
441 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
442 ifp->if_ioctl = hn_ioctl;
443 ifp->if_start = hn_start;
444 ifp->if_init = hn_ifinit;
445 /* needed by hv_rf_on_device_add() code */
446 ifp->if_mtu = ETHERMTU;
447 IFQ_SET_MAXLEN(&ifp->if_snd, 512);
448 ifp->if_snd.ifq_drv_maxlen = 511;
449 IFQ_SET_READY(&ifp->if_snd);
451 ifmedia_init(&sc->hn_media, 0, hn_ifmedia_upd, hn_ifmedia_sts);
452 ifmedia_add(&sc->hn_media, IFM_ETHER | IFM_AUTO, 0, NULL);
453 ifmedia_set(&sc->hn_media, IFM_ETHER | IFM_AUTO);
454 /* XXX ifmedia_set really should do this for us */
455 sc->hn_media.ifm_media = sc->hn_media.ifm_cur->ifm_media;
458 * Tell upper layers that we support full VLAN capability.
460 ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
461 ifp->if_capabilities |=
462 IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU | IFCAP_HWCSUM | IFCAP_TSO |
465 IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU | IFCAP_HWCSUM | IFCAP_TSO |
467 ifp->if_hwassist = sc->hn_tx_ring[0].hn_csum_assist | CSUM_TSO;
469 error = hv_rf_on_device_add(device_ctx, &device_info);
473 if (device_info.link_state == 0) {
477 #if __FreeBSD_version >= 1100045
478 tso_maxlen = hn_tso_maxlen;
479 if (tso_maxlen <= 0 || tso_maxlen > IP_MAXPACKET)
480 tso_maxlen = IP_MAXPACKET;
482 ifp->if_hw_tsomaxsegcount = HN_TX_DATA_SEGCNT_MAX;
483 ifp->if_hw_tsomaxsegsize = PAGE_SIZE;
484 ifp->if_hw_tsomax = tso_maxlen -
485 (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN);
488 ether_ifattach(ifp, device_info.mac_addr);
490 #if __FreeBSD_version >= 1100045
491 if_printf(ifp, "TSO: %u/%u/%u\n", ifp->if_hw_tsomax,
492 ifp->if_hw_tsomaxsegcount, ifp->if_hw_tsomaxsegsize);
495 sc->hn_tx_chimney_max = sc->net_dev->send_section_size;
496 hn_set_tx_chimney_size(sc, sc->hn_tx_chimney_max);
497 if (hn_tx_chimney_size > 0 &&
498 hn_tx_chimney_size < sc->hn_tx_chimney_max)
499 hn_set_tx_chimney_size(sc, hn_tx_chimney_size);
503 hn_destroy_tx_data(sc);
510 * Standard detach entry point
513 netvsc_detach(device_t dev)
515 struct hn_softc *sc = device_get_softc(dev);
516 struct hv_device *hv_device = vmbus_get_devctx(dev);
519 printf("netvsc_detach\n");
522 * XXXKYS: Need to clean up all our
523 * driver state; this is the driver
528 * XXXKYS: Need to stop outgoing traffic and unregister
532 hv_rf_on_device_remove(hv_device, HV_RF_NV_DESTROY_CHANNEL);
534 hn_stop_tx_tasks(sc);
536 ifmedia_removeall(&sc->hn_media);
537 hn_destroy_rx_data(sc);
538 hn_destroy_tx_data(sc);
540 if (sc->hn_tx_taskq != hn_tx_taskq)
541 taskqueue_free(sc->hn_tx_taskq);
547 * Standard shutdown entry point
550 netvsc_shutdown(device_t dev)
556 hn_txdesc_dmamap_load(struct hn_tx_ring *txr, struct hn_txdesc *txd,
557 struct mbuf **m_head, bus_dma_segment_t *segs, int *nsegs)
559 struct mbuf *m = *m_head;
562 error = bus_dmamap_load_mbuf_sg(txr->hn_tx_data_dtag, txd->data_dmap,
563 m, segs, nsegs, BUS_DMA_NOWAIT);
564 if (error == EFBIG) {
567 m_new = m_collapse(m, M_NOWAIT, HN_TX_DATA_SEGCNT_MAX);
572 txr->hn_tx_collapsed++;
574 error = bus_dmamap_load_mbuf_sg(txr->hn_tx_data_dtag,
575 txd->data_dmap, m, segs, nsegs, BUS_DMA_NOWAIT);
578 bus_dmamap_sync(txr->hn_tx_data_dtag, txd->data_dmap,
579 BUS_DMASYNC_PREWRITE);
580 txd->flags |= HN_TXD_FLAG_DMAMAP;
586 hn_txdesc_dmamap_unload(struct hn_tx_ring *txr, struct hn_txdesc *txd)
589 if (txd->flags & HN_TXD_FLAG_DMAMAP) {
590 bus_dmamap_sync(txr->hn_tx_data_dtag,
591 txd->data_dmap, BUS_DMASYNC_POSTWRITE);
592 bus_dmamap_unload(txr->hn_tx_data_dtag,
594 txd->flags &= ~HN_TXD_FLAG_DMAMAP;
599 hn_txdesc_put(struct hn_tx_ring *txr, struct hn_txdesc *txd)
602 KASSERT((txd->flags & HN_TXD_FLAG_ONLIST) == 0,
603 ("put an onlist txd %#x", txd->flags));
605 KASSERT(txd->refs > 0, ("invalid txd refs %d", txd->refs));
606 if (atomic_fetchadd_int(&txd->refs, -1) != 1)
609 hn_txdesc_dmamap_unload(txr, txd);
610 if (txd->m != NULL) {
615 txd->flags |= HN_TXD_FLAG_ONLIST;
617 #ifndef HN_USE_TXDESC_BUFRING
618 mtx_lock_spin(&txr->hn_txlist_spin);
619 KASSERT(txr->hn_txdesc_avail >= 0 &&
620 txr->hn_txdesc_avail < txr->hn_txdesc_cnt,
621 ("txdesc_put: invalid txd avail %d", txr->hn_txdesc_avail));
622 txr->hn_txdesc_avail++;
623 SLIST_INSERT_HEAD(&txr->hn_txlist, txd, link);
624 mtx_unlock_spin(&txr->hn_txlist_spin);
626 atomic_add_int(&txr->hn_txdesc_avail, 1);
627 buf_ring_enqueue(txr->hn_txdesc_br, txd);
633 static __inline struct hn_txdesc *
634 hn_txdesc_get(struct hn_tx_ring *txr)
636 struct hn_txdesc *txd;
638 #ifndef HN_USE_TXDESC_BUFRING
639 mtx_lock_spin(&txr->hn_txlist_spin);
640 txd = SLIST_FIRST(&txr->hn_txlist);
642 KASSERT(txr->hn_txdesc_avail > 0,
643 ("txdesc_get: invalid txd avail %d", txr->hn_txdesc_avail));
644 txr->hn_txdesc_avail--;
645 SLIST_REMOVE_HEAD(&txr->hn_txlist, link);
647 mtx_unlock_spin(&txr->hn_txlist_spin);
649 txd = buf_ring_dequeue_sc(txr->hn_txdesc_br);
653 #ifdef HN_USE_TXDESC_BUFRING
654 atomic_subtract_int(&txr->hn_txdesc_avail, 1);
656 KASSERT(txd->m == NULL && txd->refs == 0 &&
657 (txd->flags & HN_TXD_FLAG_ONLIST), ("invalid txd"));
658 txd->flags &= ~HN_TXD_FLAG_ONLIST;
665 hn_txdesc_hold(struct hn_txdesc *txd)
668 /* 0->1 transition will never work */
669 KASSERT(txd->refs > 0, ("invalid refs %d", txd->refs));
670 atomic_add_int(&txd->refs, 1);
674 * Send completion processing
676 * Note: It looks like offset 0 of buf is reserved to hold the softc
677 * pointer. The sc pointer is not currently needed in this function, and
678 * it is not presently populated by the TX function.
681 netvsc_xmit_completion(void *context)
683 netvsc_packet *packet = context;
684 struct hn_txdesc *txd;
685 struct hn_tx_ring *txr;
687 txd = (struct hn_txdesc *)(uintptr_t)
688 packet->compl.send.send_completion_tid;
692 hn_txdesc_put(txr, txd);
696 netvsc_channel_rollup(struct hv_device *device_ctx)
698 struct hn_softc *sc = device_get_softc(device_ctx->device);
699 struct hn_tx_ring *txr = &sc->hn_tx_ring[0]; /* TODO: vRSS */
700 #if defined(INET) || defined(INET6)
701 struct hn_rx_ring *rxr = &sc->hn_rx_ring[0]; /* TODO: vRSS */
702 struct lro_ctrl *lro = &rxr->hn_lro;
703 struct lro_entry *queued;
705 while ((queued = SLIST_FIRST(&lro->lro_active)) != NULL) {
706 SLIST_REMOVE_HEAD(&lro->lro_active, next);
707 tcp_lro_flush(lro, queued);
720 * If this function fails, then both txd and m_head0 will be freed.
723 hn_encap(struct hn_tx_ring *txr, struct hn_txdesc *txd, struct mbuf **m_head0)
725 bus_dma_segment_t segs[HN_TX_DATA_SEGCNT_MAX];
727 struct mbuf *m_head = *m_head0;
728 netvsc_packet *packet;
729 rndis_msg *rndis_mesg;
730 rndis_packet *rndis_pkt;
731 rndis_per_packet_info *rppi;
732 uint32_t rndis_msg_size;
734 packet = &txd->netvsc_pkt;
735 packet->is_data_pkt = TRUE;
736 packet->tot_data_buf_len = m_head->m_pkthdr.len;
739 * extension points to the area reserved for the
740 * rndis_filter_packet, which is placed just after
741 * the netvsc_packet (and rppi struct, if present;
742 * length is updated later).
744 rndis_mesg = txd->rndis_msg;
745 /* XXX not necessary */
746 memset(rndis_mesg, 0, HN_RNDIS_MSG_LEN);
747 rndis_mesg->ndis_msg_type = REMOTE_NDIS_PACKET_MSG;
749 rndis_pkt = &rndis_mesg->msg.packet;
750 rndis_pkt->data_offset = sizeof(rndis_packet);
751 rndis_pkt->data_length = packet->tot_data_buf_len;
752 rndis_pkt->per_pkt_info_offset = sizeof(rndis_packet);
754 rndis_msg_size = RNDIS_MESSAGE_SIZE(rndis_packet);
756 if (m_head->m_flags & M_VLANTAG) {
757 ndis_8021q_info *rppi_vlan_info;
759 rndis_msg_size += RNDIS_VLAN_PPI_SIZE;
760 rppi = hv_set_rppi_data(rndis_mesg, RNDIS_VLAN_PPI_SIZE,
763 rppi_vlan_info = (ndis_8021q_info *)((uint8_t *)rppi +
764 rppi->per_packet_info_offset);
765 rppi_vlan_info->u1.s1.vlan_id =
766 m_head->m_pkthdr.ether_vtag & 0xfff;
769 if (m_head->m_pkthdr.csum_flags & CSUM_TSO) {
770 rndis_tcp_tso_info *tso_info;
771 struct ether_vlan_header *eh;
775 * XXX need m_pullup and use mtodo
777 eh = mtod(m_head, struct ether_vlan_header*);
778 if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN))
779 ether_len = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
781 ether_len = ETHER_HDR_LEN;
783 rndis_msg_size += RNDIS_TSO_PPI_SIZE;
784 rppi = hv_set_rppi_data(rndis_mesg, RNDIS_TSO_PPI_SIZE,
785 tcp_large_send_info);
787 tso_info = (rndis_tcp_tso_info *)((uint8_t *)rppi +
788 rppi->per_packet_info_offset);
789 tso_info->lso_v2_xmit.type =
790 RNDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE;
793 if (m_head->m_pkthdr.csum_flags & CSUM_IP_TSO) {
795 (struct ip *)(m_head->m_data + ether_len);
796 unsigned long iph_len = ip->ip_hl << 2;
798 (struct tcphdr *)((caddr_t)ip + iph_len);
800 tso_info->lso_v2_xmit.ip_version =
801 RNDIS_TCP_LARGE_SEND_OFFLOAD_IPV4;
805 th->th_sum = in_pseudo(ip->ip_src.s_addr,
806 ip->ip_dst.s_addr, htons(IPPROTO_TCP));
809 #if defined(INET6) && defined(INET)
814 struct ip6_hdr *ip6 = (struct ip6_hdr *)
815 (m_head->m_data + ether_len);
816 struct tcphdr *th = (struct tcphdr *)(ip6 + 1);
818 tso_info->lso_v2_xmit.ip_version =
819 RNDIS_TCP_LARGE_SEND_OFFLOAD_IPV6;
821 th->th_sum = in6_cksum_pseudo(ip6, 0, IPPROTO_TCP, 0);
824 tso_info->lso_v2_xmit.tcp_header_offset = 0;
825 tso_info->lso_v2_xmit.mss = m_head->m_pkthdr.tso_segsz;
826 } else if (m_head->m_pkthdr.csum_flags & txr->hn_csum_assist) {
827 rndis_tcp_ip_csum_info *csum_info;
829 rndis_msg_size += RNDIS_CSUM_PPI_SIZE;
830 rppi = hv_set_rppi_data(rndis_mesg, RNDIS_CSUM_PPI_SIZE,
832 csum_info = (rndis_tcp_ip_csum_info *)((uint8_t *)rppi +
833 rppi->per_packet_info_offset);
835 csum_info->xmit.is_ipv4 = 1;
836 if (m_head->m_pkthdr.csum_flags & CSUM_IP)
837 csum_info->xmit.ip_header_csum = 1;
839 if (m_head->m_pkthdr.csum_flags & CSUM_TCP) {
840 csum_info->xmit.tcp_csum = 1;
841 csum_info->xmit.tcp_header_offset = 0;
842 } else if (m_head->m_pkthdr.csum_flags & CSUM_UDP) {
843 csum_info->xmit.udp_csum = 1;
847 rndis_mesg->msg_len = packet->tot_data_buf_len + rndis_msg_size;
848 packet->tot_data_buf_len = rndis_mesg->msg_len;
851 * Chimney send, if the packet could fit into one chimney buffer.
853 if (packet->tot_data_buf_len < txr->hn_tx_chimney_size) {
854 netvsc_dev *net_dev = txr->hn_sc->net_dev;
855 uint32_t send_buf_section_idx;
857 send_buf_section_idx =
858 hv_nv_get_next_send_section(net_dev);
859 if (send_buf_section_idx !=
860 NVSP_1_CHIMNEY_SEND_INVALID_SECTION_INDEX) {
861 uint8_t *dest = ((uint8_t *)net_dev->send_buf +
862 (send_buf_section_idx *
863 net_dev->send_section_size));
865 memcpy(dest, rndis_mesg, rndis_msg_size);
866 dest += rndis_msg_size;
867 m_copydata(m_head, 0, m_head->m_pkthdr.len, dest);
869 packet->send_buf_section_idx = send_buf_section_idx;
870 packet->send_buf_section_size =
871 packet->tot_data_buf_len;
872 packet->page_buf_count = 0;
873 txr->hn_tx_chimney++;
878 error = hn_txdesc_dmamap_load(txr, txd, &m_head, segs, &nsegs);
883 * This mbuf is not linked w/ the txd yet, so free it now.
888 freed = hn_txdesc_put(txr, txd);
890 ("fail to free txd upon txdma error"));
892 txr->hn_txdma_failed++;
893 if_inc_counter(txr->hn_sc->hn_ifp, IFCOUNTER_OERRORS, 1);
898 packet->page_buf_count = nsegs + HV_RF_NUM_TX_RESERVED_PAGE_BUFS;
900 /* send packet with page buffer */
901 packet->page_buffers[0].pfn = atop(txd->rndis_msg_paddr);
902 packet->page_buffers[0].offset = txd->rndis_msg_paddr & PAGE_MASK;
903 packet->page_buffers[0].length = rndis_msg_size;
906 * Fill the page buffers with mbuf info starting at index
907 * HV_RF_NUM_TX_RESERVED_PAGE_BUFS.
909 for (i = 0; i < nsegs; ++i) {
910 hv_vmbus_page_buffer *pb = &packet->page_buffers[
911 i + HV_RF_NUM_TX_RESERVED_PAGE_BUFS];
913 pb->pfn = atop(segs[i].ds_addr);
914 pb->offset = segs[i].ds_addr & PAGE_MASK;
915 pb->length = segs[i].ds_len;
918 packet->send_buf_section_idx =
919 NVSP_1_CHIMNEY_SEND_INVALID_SECTION_INDEX;
920 packet->send_buf_section_size = 0;
924 /* Set the completion routine */
925 packet->compl.send.on_send_completion = netvsc_xmit_completion;
926 packet->compl.send.send_completion_context = packet;
927 packet->compl.send.send_completion_tid = (uint64_t)(uintptr_t)txd;
933 * Start a transmit of one or more packets
936 hn_start_locked(struct hn_tx_ring *txr, int len)
938 struct hn_softc *sc = txr->hn_sc;
939 struct ifnet *ifp = sc->hn_ifp;
940 struct hv_device *device_ctx = vmbus_get_devctx(sc->hn_dev);
942 KASSERT(txr == &sc->hn_tx_ring[0], ("not the first TX ring"));
943 mtx_assert(&txr->hn_tx_lock, MA_OWNED);
945 if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
949 while (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) {
950 int error, send_failed = 0;
951 struct hn_txdesc *txd;
954 IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
958 if (len > 0 && m_head->m_pkthdr.len > len) {
960 * This sending could be time consuming; let callers
961 * dispatch this packet sending (and sending of any
962 * following up packets) to tx taskqueue.
964 IF_PREPEND(&ifp->if_snd, m_head);
968 txd = hn_txdesc_get(txr);
970 txr->hn_no_txdescs++;
971 IF_PREPEND(&ifp->if_snd, m_head);
972 atomic_set_int(&ifp->if_drv_flags, IFF_DRV_OACTIVE);
976 error = hn_encap(txr, txd, &m_head);
978 /* Both txd and m_head are freed */
983 * Make sure that txd is not freed before ETHER_BPF_MTAP.
986 error = hv_nv_on_send(device_ctx, &txd->netvsc_pkt);
988 ETHER_BPF_MTAP(ifp, m_head);
989 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
991 hn_txdesc_put(txr, txd);
993 if (__predict_false(error)) {
997 * This should "really rarely" happen.
999 * XXX Too many RX to be acked or too many sideband
1000 * commands to run? Ask netvsc_channel_rollup()
1001 * to kick start later.
1005 txr->hn_send_failed++;
1008 * Try sending again after set hn_txeof;
1009 * in case that we missed the last
1010 * netvsc_channel_rollup().
1014 if_printf(ifp, "send failed\n");
1017 * This mbuf will be prepended, don't free it
1018 * in hn_txdesc_put(); only unload it from the
1019 * DMA map in hn_txdesc_put(), if it was loaded.
1022 freed = hn_txdesc_put(txr, txd);
1024 ("fail to free txd upon send error"));
1026 txr->hn_send_failed++;
1027 IF_PREPEND(&ifp->if_snd, m_head);
1028 atomic_set_int(&ifp->if_drv_flags, IFF_DRV_OACTIVE);
1036 * Link up/down notification
1039 netvsc_linkstatus_callback(struct hv_device *device_obj, uint32_t status)
1041 hn_softc_t *sc = device_get_softc(device_obj->device);
1055 * Append the specified data to the indicated mbuf chain,
1056 * Extend the mbuf chain if the new data does not fit in
1059 * This is a minor rewrite of m_append() from sys/kern/uipc_mbuf.c.
1060 * There should be an equivalent in the kernel mbuf code,
1061 * but there does not appear to be one yet.
1063 * Differs from m_append() in that additional mbufs are
1064 * allocated with cluster size MJUMPAGESIZE, and filled
1067 * Return 1 if able to complete the job; otherwise 0.
1070 hv_m_append(struct mbuf *m0, int len, c_caddr_t cp)
1073 int remainder, space;
1075 for (m = m0; m->m_next != NULL; m = m->m_next)
1078 space = M_TRAILINGSPACE(m);
1081 * Copy into available space.
1083 if (space > remainder)
1085 bcopy(cp, mtod(m, caddr_t) + m->m_len, space);
1090 while (remainder > 0) {
1092 * Allocate a new mbuf; could check space
1093 * and allocate a cluster instead.
1095 n = m_getjcl(M_DONTWAIT, m->m_type, 0, MJUMPAGESIZE);
1098 n->m_len = min(MJUMPAGESIZE, remainder);
1099 bcopy(cp, mtod(n, caddr_t), n->m_len);
1101 remainder -= n->m_len;
1105 if (m0->m_flags & M_PKTHDR)
1106 m0->m_pkthdr.len += len - remainder;
1108 return (remainder == 0);
1113 * Called when we receive a data packet from the "wire" on the
1116 * Note: This is no longer used as a callback
1119 netvsc_recv(struct hv_device *device_ctx, netvsc_packet *packet,
1120 rndis_tcp_ip_csum_info *csum_info)
1122 struct hn_softc *sc = device_get_softc(device_ctx->device);
1123 struct hn_rx_ring *rxr = &sc->hn_rx_ring[0]; /* TODO: vRSS */
1126 int size, do_lro = 0, do_csum = 1;
1129 return (0); /* TODO: KYS how can this be! */
1134 ifp = sc->arpcom.ac_ifp;
1136 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1141 * Bail out if packet contains more data than configured MTU.
1143 if (packet->tot_data_buf_len > (ifp->if_mtu + ETHER_HDR_LEN)) {
1145 } else if (packet->tot_data_buf_len <= MHLEN) {
1146 m_new = m_gethdr(M_NOWAIT, MT_DATA);
1149 memcpy(mtod(m_new, void *), packet->data,
1150 packet->tot_data_buf_len);
1151 m_new->m_pkthdr.len = m_new->m_len = packet->tot_data_buf_len;
1152 rxr->hn_small_pkts++;
1155 * Get an mbuf with a cluster. For packets 2K or less,
1156 * get a standard 2K cluster. For anything larger, get a
1157 * 4K cluster. Any buffers larger than 4K can cause problems
1158 * if looped around to the Hyper-V TX channel, so avoid them.
1161 if (packet->tot_data_buf_len > MCLBYTES) {
1163 size = MJUMPAGESIZE;
1166 m_new = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, size);
1167 if (m_new == NULL) {
1168 if_printf(ifp, "alloc mbuf failed.\n");
1172 hv_m_append(m_new, packet->tot_data_buf_len, packet->data);
1174 m_new->m_pkthdr.rcvif = ifp;
1176 if (__predict_false((ifp->if_capenable & IFCAP_RXCSUM) == 0))
1179 /* receive side checksum offload */
1180 if (csum_info != NULL) {
1181 /* IP csum offload */
1182 if (csum_info->receive.ip_csum_succeeded && do_csum) {
1183 m_new->m_pkthdr.csum_flags |=
1184 (CSUM_IP_CHECKED | CSUM_IP_VALID);
1188 /* TCP/UDP csum offload */
1189 if ((csum_info->receive.tcp_csum_succeeded ||
1190 csum_info->receive.udp_csum_succeeded) && do_csum) {
1191 m_new->m_pkthdr.csum_flags |=
1192 (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
1193 m_new->m_pkthdr.csum_data = 0xffff;
1194 if (csum_info->receive.tcp_csum_succeeded)
1200 if (csum_info->receive.ip_csum_succeeded &&
1201 csum_info->receive.tcp_csum_succeeded)
1204 const struct ether_header *eh;
1209 if (m_new->m_len < hoff)
1211 eh = mtod(m_new, struct ether_header *);
1212 etype = ntohs(eh->ether_type);
1213 if (etype == ETHERTYPE_VLAN) {
1214 const struct ether_vlan_header *evl;
1216 hoff = sizeof(*evl);
1217 if (m_new->m_len < hoff)
1219 evl = mtod(m_new, struct ether_vlan_header *);
1220 etype = ntohs(evl->evl_proto);
1223 if (etype == ETHERTYPE_IP) {
1226 pr = hn_check_iplen(m_new, hoff);
1227 if (pr == IPPROTO_TCP) {
1229 (rxr->hn_trust_hcsum &
1230 HN_TRUST_HCSUM_TCP)) {
1231 rxr->hn_csum_trusted++;
1232 m_new->m_pkthdr.csum_flags |=
1233 (CSUM_IP_CHECKED | CSUM_IP_VALID |
1234 CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
1235 m_new->m_pkthdr.csum_data = 0xffff;
1237 /* Rely on SW csum verification though... */
1239 } else if (pr == IPPROTO_UDP) {
1241 (rxr->hn_trust_hcsum &
1242 HN_TRUST_HCSUM_UDP)) {
1243 rxr->hn_csum_trusted++;
1244 m_new->m_pkthdr.csum_flags |=
1245 (CSUM_IP_CHECKED | CSUM_IP_VALID |
1246 CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
1247 m_new->m_pkthdr.csum_data = 0xffff;
1249 } else if (pr != IPPROTO_DONE && do_csum &&
1250 (rxr->hn_trust_hcsum & HN_TRUST_HCSUM_IP)) {
1251 rxr->hn_csum_trusted++;
1252 m_new->m_pkthdr.csum_flags |=
1253 (CSUM_IP_CHECKED | CSUM_IP_VALID);
1258 if ((packet->vlan_tci != 0) &&
1259 (ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0) {
1260 m_new->m_pkthdr.ether_vtag = packet->vlan_tci;
1261 m_new->m_flags |= M_VLANTAG;
1265 * Note: Moved RX completion back to hv_nv_on_receive() so all
1266 * messages (not just data messages) will trigger a response.
1271 if ((ifp->if_capenable & IFCAP_LRO) && do_lro) {
1272 #if defined(INET) || defined(INET6)
1273 struct lro_ctrl *lro = &rxr->hn_lro;
1276 rxr->hn_lro_tried++;
1277 if (tcp_lro_rx(lro, m_new, 0) == 0) {
1285 /* We're not holding the lock here, so don't release it */
1286 (*ifp->if_input)(ifp, m_new);
1292 netvsc_recv_rollup(struct hv_device *device_ctx __unused)
1297 * Rules for using sc->temp_unusable:
1298 * 1. sc->temp_unusable can only be read or written while holding NV_LOCK()
1299 * 2. code reading sc->temp_unusable under NV_LOCK(), and finding
1300 * sc->temp_unusable set, must release NV_LOCK() and exit
1301 * 3. to retain exclusive control of the interface,
1302 * sc->temp_unusable must be set by code before releasing NV_LOCK()
1303 * 4. only code setting sc->temp_unusable can clear sc->temp_unusable
1304 * 5. code setting sc->temp_unusable must eventually clear sc->temp_unusable
1308 * Standard ioctl entry point. Called when the user wants to configure
1312 hn_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1314 hn_softc_t *sc = ifp->if_softc;
1315 struct ifreq *ifr = (struct ifreq *)data;
1317 struct ifaddr *ifa = (struct ifaddr *)data;
1319 netvsc_device_info device_info;
1320 struct hv_device *hn_dev;
1321 int mask, error = 0;
1322 int retry_cnt = 500;
1328 if (ifa->ifa_addr->sa_family == AF_INET) {
1329 ifp->if_flags |= IFF_UP;
1330 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
1332 arp_ifinit(ifp, ifa);
1335 error = ether_ioctl(ifp, cmd, data);
1338 hn_dev = vmbus_get_devctx(sc->hn_dev);
1340 /* Check MTU value change */
1341 if (ifp->if_mtu == ifr->ifr_mtu)
1344 if (ifr->ifr_mtu > NETVSC_MAX_CONFIGURABLE_MTU) {
1349 /* Obtain and record requested MTU */
1350 ifp->if_mtu = ifr->ifr_mtu;
1352 #if __FreeBSD_version >= 1100099
1354 * Make sure that LRO aggregation length limit is still
1355 * valid, after the MTU change.
1358 if (sc->hn_rx_ring[0].hn_lro.lro_length_lim <
1359 HN_LRO_LENLIM_MIN(ifp)) {
1361 for (i = 0; i < sc->hn_rx_ring_cnt; ++i) {
1362 sc->hn_rx_ring[i].hn_lro.lro_length_lim =
1363 HN_LRO_LENLIM_MIN(ifp);
1371 if (!sc->temp_unusable) {
1372 sc->temp_unusable = TRUE;
1376 if (retry_cnt > 0) {
1380 } while (retry_cnt > 0);
1382 if (retry_cnt == 0) {
1387 /* We must remove and add back the device to cause the new
1388 * MTU to take effect. This includes tearing down, but not
1389 * deleting the channel, then bringing it back up.
1391 error = hv_rf_on_device_remove(hn_dev, HV_RF_NV_RETAIN_CHANNEL);
1394 sc->temp_unusable = FALSE;
1398 error = hv_rf_on_device_add(hn_dev, &device_info);
1401 sc->temp_unusable = FALSE;
1406 sc->hn_tx_chimney_max = sc->net_dev->send_section_size;
1407 if (sc->hn_tx_ring[0].hn_tx_chimney_size >
1408 sc->hn_tx_chimney_max)
1409 hn_set_tx_chimney_size(sc, sc->hn_tx_chimney_max);
1411 hn_ifinit_locked(sc);
1414 sc->temp_unusable = FALSE;
1420 if (!sc->temp_unusable) {
1421 sc->temp_unusable = TRUE;
1425 if (retry_cnt > 0) {
1429 } while (retry_cnt > 0);
1431 if (retry_cnt == 0) {
1436 if (ifp->if_flags & IFF_UP) {
1438 * If only the state of the PROMISC flag changed,
1439 * then just use the 'set promisc mode' command
1440 * instead of reinitializing the entire NIC. Doing
1441 * a full re-init means reloading the firmware and
1442 * waiting for it to start up, which may take a
1446 /* Fixme: Promiscuous mode? */
1447 if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
1448 ifp->if_flags & IFF_PROMISC &&
1449 !(sc->hn_if_flags & IFF_PROMISC)) {
1450 /* do something here for Hyper-V */
1451 } else if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
1452 !(ifp->if_flags & IFF_PROMISC) &&
1453 sc->hn_if_flags & IFF_PROMISC) {
1454 /* do something here for Hyper-V */
1457 hn_ifinit_locked(sc);
1459 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1464 sc->temp_unusable = FALSE;
1466 sc->hn_if_flags = ifp->if_flags;
1472 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1473 if (mask & IFCAP_TXCSUM) {
1474 ifp->if_capenable ^= IFCAP_TXCSUM;
1475 if (ifp->if_capenable & IFCAP_TXCSUM) {
1477 sc->hn_tx_ring[0].hn_csum_assist;
1480 ~sc->hn_tx_ring[0].hn_csum_assist;
1484 if (mask & IFCAP_RXCSUM)
1485 ifp->if_capenable ^= IFCAP_RXCSUM;
1487 if (mask & IFCAP_LRO)
1488 ifp->if_capenable ^= IFCAP_LRO;
1490 if (mask & IFCAP_TSO4) {
1491 ifp->if_capenable ^= IFCAP_TSO4;
1492 if (ifp->if_capenable & IFCAP_TSO4)
1493 ifp->if_hwassist |= CSUM_IP_TSO;
1495 ifp->if_hwassist &= ~CSUM_IP_TSO;
1498 if (mask & IFCAP_TSO6) {
1499 ifp->if_capenable ^= IFCAP_TSO6;
1500 if (ifp->if_capenable & IFCAP_TSO6)
1501 ifp->if_hwassist |= CSUM_IP6_TSO;
1503 ifp->if_hwassist &= ~CSUM_IP6_TSO;
1512 /* Fixme: Multicast mode? */
1513 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1515 netvsc_setmulti(sc);
1524 error = ifmedia_ioctl(ifp, ifr, &sc->hn_media, cmd);
1527 error = ether_ioctl(ifp, cmd, data);
1538 hn_stop(hn_softc_t *sc)
1542 struct hv_device *device_ctx = vmbus_get_devctx(sc->hn_dev);
1547 printf(" Closing Device ...\n");
1549 atomic_clear_int(&ifp->if_drv_flags,
1550 (IFF_DRV_RUNNING | IFF_DRV_OACTIVE));
1551 if_link_state_change(ifp, LINK_STATE_DOWN);
1552 sc->hn_initdone = 0;
1554 ret = hv_rf_on_close(device_ctx);
1558 * FreeBSD transmit entry point
1561 hn_start(struct ifnet *ifp)
1563 struct hn_softc *sc = ifp->if_softc;
1564 struct hn_tx_ring *txr = &sc->hn_tx_ring[0];
1566 if (txr->hn_sched_tx)
1569 if (mtx_trylock(&txr->hn_tx_lock)) {
1572 sched = hn_start_locked(txr, txr->hn_direct_tx_size);
1573 mtx_unlock(&txr->hn_tx_lock);
1578 taskqueue_enqueue(txr->hn_tx_taskq, &txr->hn_start_task);
1582 hn_start_txeof(struct hn_tx_ring *txr)
1584 struct hn_softc *sc = txr->hn_sc;
1585 struct ifnet *ifp = sc->hn_ifp;
1587 KASSERT(txr == &sc->hn_tx_ring[0], ("not the first TX ring"));
1589 if (txr->hn_sched_tx)
1592 if (mtx_trylock(&txr->hn_tx_lock)) {
1595 atomic_clear_int(&ifp->if_drv_flags, IFF_DRV_OACTIVE);
1596 sched = hn_start_locked(txr, txr->hn_direct_tx_size);
1597 mtx_unlock(&txr->hn_tx_lock);
1599 taskqueue_enqueue(txr->hn_tx_taskq,
1600 &txr->hn_start_task);
1605 * Release the OACTIVE earlier, with the hope, that
1606 * others could catch up. The task will clear the
1607 * flag again with the hn_tx_lock to avoid possible
1610 atomic_clear_int(&ifp->if_drv_flags, IFF_DRV_OACTIVE);
1611 taskqueue_enqueue(txr->hn_tx_taskq, &txr->hn_txeof_task);
1619 hn_ifinit_locked(hn_softc_t *sc)
1622 struct hv_device *device_ctx = vmbus_get_devctx(sc->hn_dev);
1627 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1631 hv_promisc_mode = 1;
1633 ret = hv_rf_on_open(device_ctx);
1637 sc->hn_initdone = 1;
1639 atomic_clear_int(&ifp->if_drv_flags, IFF_DRV_OACTIVE);
1640 atomic_set_int(&ifp->if_drv_flags, IFF_DRV_RUNNING);
1641 if_link_state_change(ifp, LINK_STATE_UP);
1648 hn_ifinit(void *xsc)
1650 hn_softc_t *sc = xsc;
1653 if (sc->temp_unusable) {
1657 sc->temp_unusable = TRUE;
1660 hn_ifinit_locked(sc);
1663 sc->temp_unusable = FALSE;
1672 hn_watchdog(struct ifnet *ifp)
1677 printf("hn%d: watchdog timeout -- resetting\n", sc->hn_unit);
1678 hn_ifinit(sc); /*???*/
1683 #if __FreeBSD_version >= 1100099
1686 hn_lro_lenlim_sysctl(SYSCTL_HANDLER_ARGS)
1688 struct hn_softc *sc = arg1;
1689 unsigned int lenlim;
1692 lenlim = sc->hn_rx_ring[0].hn_lro.lro_length_lim;
1693 error = sysctl_handle_int(oidp, &lenlim, 0, req);
1694 if (error || req->newptr == NULL)
1697 if (lenlim < HN_LRO_LENLIM_MIN(sc->hn_ifp) ||
1698 lenlim > TCP_LRO_LENGTH_MAX)
1702 for (i = 0; i < sc->hn_rx_ring_cnt; ++i)
1703 sc->hn_rx_ring[i].hn_lro.lro_length_lim = lenlim;
1709 hn_lro_ackcnt_sysctl(SYSCTL_HANDLER_ARGS)
1711 struct hn_softc *sc = arg1;
1712 int ackcnt, error, i;
1715 * lro_ackcnt_lim is append count limit,
1716 * +1 to turn it into aggregation limit.
1718 ackcnt = sc->hn_rx_ring[0].hn_lro.lro_ackcnt_lim + 1;
1719 error = sysctl_handle_int(oidp, &ackcnt, 0, req);
1720 if (error || req->newptr == NULL)
1723 if (ackcnt < 2 || ackcnt > (TCP_LRO_ACKCNT_MAX + 1))
1727 * Convert aggregation limit back to append
1732 for (i = 0; i < sc->hn_rx_ring_cnt; ++i)
1733 sc->hn_rx_ring[i].hn_lro.lro_ackcnt_lim = ackcnt;
1741 hn_trust_hcsum_sysctl(SYSCTL_HANDLER_ARGS)
1743 struct hn_softc *sc = arg1;
1748 if (sc->hn_rx_ring[0].hn_trust_hcsum & hcsum)
1751 error = sysctl_handle_int(oidp, &on, 0, req);
1752 if (error || req->newptr == NULL)
1756 for (i = 0; i < sc->hn_rx_ring_cnt; ++i) {
1757 struct hn_rx_ring *rxr = &sc->hn_rx_ring[i];
1760 rxr->hn_trust_hcsum |= hcsum;
1762 rxr->hn_trust_hcsum &= ~hcsum;
1769 hn_tx_chimney_size_sysctl(SYSCTL_HANDLER_ARGS)
1771 struct hn_softc *sc = arg1;
1772 int chimney_size, error;
1774 chimney_size = sc->hn_tx_ring[0].hn_tx_chimney_size;
1775 error = sysctl_handle_int(oidp, &chimney_size, 0, req);
1776 if (error || req->newptr == NULL)
1779 if (chimney_size > sc->hn_tx_chimney_max || chimney_size <= 0)
1782 hn_set_tx_chimney_size(sc, chimney_size);
1787 hn_rx_stat_ulong_sysctl(SYSCTL_HANDLER_ARGS)
1789 struct hn_softc *sc = arg1;
1790 int ofs = arg2, i, error;
1791 struct hn_rx_ring *rxr;
1795 for (i = 0; i < sc->hn_rx_ring_cnt; ++i) {
1796 rxr = &sc->hn_rx_ring[i];
1797 stat += *((u_long *)((uint8_t *)rxr + ofs));
1800 error = sysctl_handle_long(oidp, &stat, 0, req);
1801 if (error || req->newptr == NULL)
1804 /* Zero out this stat. */
1805 for (i = 0; i < sc->hn_rx_ring_cnt; ++i) {
1806 rxr = &sc->hn_rx_ring[i];
1807 *((u_long *)((uint8_t *)rxr + ofs)) = 0;
1813 hn_rx_stat_u64_sysctl(SYSCTL_HANDLER_ARGS)
1815 struct hn_softc *sc = arg1;
1816 int ofs = arg2, i, error;
1817 struct hn_rx_ring *rxr;
1821 for (i = 0; i < sc->hn_rx_ring_cnt; ++i) {
1822 rxr = &sc->hn_rx_ring[i];
1823 stat += *((uint64_t *)((uint8_t *)rxr + ofs));
1826 error = sysctl_handle_64(oidp, &stat, 0, req);
1827 if (error || req->newptr == NULL)
1830 /* Zero out this stat. */
1831 for (i = 0; i < sc->hn_rx_ring_cnt; ++i) {
1832 rxr = &sc->hn_rx_ring[i];
1833 *((uint64_t *)((uint8_t *)rxr + ofs)) = 0;
1839 hn_tx_stat_ulong_sysctl(SYSCTL_HANDLER_ARGS)
1841 struct hn_softc *sc = arg1;
1842 int ofs = arg2, i, error;
1843 struct hn_tx_ring *txr;
1847 for (i = 0; i < sc->hn_tx_ring_cnt; ++i) {
1848 txr = &sc->hn_tx_ring[i];
1849 stat += *((u_long *)((uint8_t *)txr + ofs));
1852 error = sysctl_handle_long(oidp, &stat, 0, req);
1853 if (error || req->newptr == NULL)
1856 /* Zero out this stat. */
1857 for (i = 0; i < sc->hn_tx_ring_cnt; ++i) {
1858 txr = &sc->hn_tx_ring[i];
1859 *((u_long *)((uint8_t *)txr + ofs)) = 0;
1865 hn_tx_conf_int_sysctl(SYSCTL_HANDLER_ARGS)
1867 struct hn_softc *sc = arg1;
1868 int ofs = arg2, i, error, conf;
1869 struct hn_tx_ring *txr;
1871 txr = &sc->hn_tx_ring[0];
1872 conf = *((int *)((uint8_t *)txr + ofs));
1874 error = sysctl_handle_int(oidp, &conf, 0, req);
1875 if (error || req->newptr == NULL)
1879 for (i = 0; i < sc->hn_tx_ring_cnt; ++i) {
1880 txr = &sc->hn_tx_ring[i];
1881 *((int *)((uint8_t *)txr + ofs)) = conf;
1889 hn_check_iplen(const struct mbuf *m, int hoff)
1891 const struct ip *ip;
1892 int len, iphlen, iplen;
1893 const struct tcphdr *th;
1894 int thoff; /* TCP data offset */
1896 len = hoff + sizeof(struct ip);
1898 /* The packet must be at least the size of an IP header. */
1899 if (m->m_pkthdr.len < len)
1900 return IPPROTO_DONE;
1902 /* The fixed IP header must reside completely in the first mbuf. */
1904 return IPPROTO_DONE;
1906 ip = mtodo(m, hoff);
1908 /* Bound check the packet's stated IP header length. */
1909 iphlen = ip->ip_hl << 2;
1910 if (iphlen < sizeof(struct ip)) /* minimum header length */
1911 return IPPROTO_DONE;
1913 /* The full IP header must reside completely in the one mbuf. */
1914 if (m->m_len < hoff + iphlen)
1915 return IPPROTO_DONE;
1917 iplen = ntohs(ip->ip_len);
1920 * Check that the amount of data in the buffers is as
1921 * at least much as the IP header would have us expect.
1923 if (m->m_pkthdr.len < hoff + iplen)
1924 return IPPROTO_DONE;
1927 * Ignore IP fragments.
1929 if (ntohs(ip->ip_off) & (IP_OFFMASK | IP_MF))
1930 return IPPROTO_DONE;
1933 * The TCP/IP or UDP/IP header must be entirely contained within
1934 * the first fragment of a packet.
1938 if (iplen < iphlen + sizeof(struct tcphdr))
1939 return IPPROTO_DONE;
1940 if (m->m_len < hoff + iphlen + sizeof(struct tcphdr))
1941 return IPPROTO_DONE;
1942 th = (const struct tcphdr *)((const uint8_t *)ip + iphlen);
1943 thoff = th->th_off << 2;
1944 if (thoff < sizeof(struct tcphdr) || thoff + iphlen > iplen)
1945 return IPPROTO_DONE;
1946 if (m->m_len < hoff + iphlen + thoff)
1947 return IPPROTO_DONE;
1950 if (iplen < iphlen + sizeof(struct udphdr))
1951 return IPPROTO_DONE;
1952 if (m->m_len < hoff + iphlen + sizeof(struct udphdr))
1953 return IPPROTO_DONE;
1957 return IPPROTO_DONE;
1964 hn_dma_map_paddr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1966 bus_addr_t *paddr = arg;
1971 KASSERT(nseg == 1, ("too many segments %d!", nseg));
1972 *paddr = segs->ds_addr;
1976 hn_create_rx_data(struct hn_softc *sc)
1978 struct sysctl_oid_list *child;
1979 struct sysctl_ctx_list *ctx;
1980 device_t dev = sc->hn_dev;
1981 #if defined(INET) || defined(INET6)
1982 #if __FreeBSD_version >= 1100095
1988 sc->hn_rx_ring_cnt = 1; /* TODO: vRSS */
1989 sc->hn_rx_ring = malloc(sizeof(struct hn_rx_ring) * sc->hn_rx_ring_cnt,
1990 M_NETVSC, M_WAITOK | M_ZERO);
1992 #if defined(INET) || defined(INET6)
1993 #if __FreeBSD_version >= 1100095
1994 lroent_cnt = hn_lro_entry_count;
1995 if (lroent_cnt < TCP_LRO_ENTRIES)
1996 lroent_cnt = TCP_LRO_ENTRIES;
1997 device_printf(dev, "LRO: entry count %d\n", lroent_cnt);
1999 #endif /* INET || INET6 */
2001 for (i = 0; i < sc->hn_rx_ring_cnt; ++i) {
2002 struct hn_rx_ring *rxr = &sc->hn_rx_ring[i];
2004 if (hn_trust_hosttcp)
2005 rxr->hn_trust_hcsum |= HN_TRUST_HCSUM_TCP;
2006 if (hn_trust_hostudp)
2007 rxr->hn_trust_hcsum |= HN_TRUST_HCSUM_UDP;
2008 if (hn_trust_hostip)
2009 rxr->hn_trust_hcsum |= HN_TRUST_HCSUM_IP;
2014 #if defined(INET) || defined(INET6)
2015 #if __FreeBSD_version >= 1100095
2016 tcp_lro_init_args(&rxr->hn_lro, sc->hn_ifp, lroent_cnt, 0);
2018 tcp_lro_init(&rxr->hn_lro);
2019 rxr->hn_lro.ifp = sc->hn_ifp;
2021 #if __FreeBSD_version >= 1100099
2022 rxr->hn_lro.lro_length_lim = HN_LRO_LENLIM_DEF;
2023 rxr->hn_lro.lro_ackcnt_lim = HN_LRO_ACKCNT_DEF;
2025 #endif /* INET || INET6 */
2028 ctx = device_get_sysctl_ctx(dev);
2029 child = SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
2031 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "lro_queued",
2032 CTLTYPE_U64 | CTLFLAG_RW, sc,
2033 __offsetof(struct hn_rx_ring, hn_lro.lro_queued),
2034 hn_rx_stat_u64_sysctl, "LU", "LRO queued");
2035 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "lro_flushed",
2036 CTLTYPE_U64 | CTLFLAG_RW, sc,
2037 __offsetof(struct hn_rx_ring, hn_lro.lro_flushed),
2038 hn_rx_stat_u64_sysctl, "LU", "LRO flushed");
2039 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "lro_tried",
2040 CTLTYPE_ULONG | CTLFLAG_RW, sc,
2041 __offsetof(struct hn_rx_ring, hn_lro_tried),
2042 hn_rx_stat_ulong_sysctl, "LU", "# of LRO tries");
2043 #if __FreeBSD_version >= 1100099
2044 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "lro_length_lim",
2045 CTLTYPE_UINT | CTLFLAG_RW, sc, 0, hn_lro_lenlim_sysctl, "IU",
2046 "Max # of data bytes to be aggregated by LRO");
2047 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "lro_ackcnt_lim",
2048 CTLTYPE_INT | CTLFLAG_RW, sc, 0, hn_lro_ackcnt_sysctl, "I",
2049 "Max # of ACKs to be aggregated by LRO");
2051 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "trust_hosttcp",
2052 CTLTYPE_INT | CTLFLAG_RW, sc, HN_TRUST_HCSUM_TCP,
2053 hn_trust_hcsum_sysctl, "I",
2054 "Trust tcp segement verification on host side, "
2055 "when csum info is missing");
2056 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "trust_hostudp",
2057 CTLTYPE_INT | CTLFLAG_RW, sc, HN_TRUST_HCSUM_UDP,
2058 hn_trust_hcsum_sysctl, "I",
2059 "Trust udp datagram verification on host side, "
2060 "when csum info is missing");
2061 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "trust_hostip",
2062 CTLTYPE_INT | CTLFLAG_RW, sc, HN_TRUST_HCSUM_IP,
2063 hn_trust_hcsum_sysctl, "I",
2064 "Trust ip packet verification on host side, "
2065 "when csum info is missing");
2066 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "csum_ip",
2067 CTLTYPE_ULONG | CTLFLAG_RW, sc,
2068 __offsetof(struct hn_rx_ring, hn_csum_ip),
2069 hn_rx_stat_ulong_sysctl, "LU", "RXCSUM IP");
2070 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "csum_tcp",
2071 CTLTYPE_ULONG | CTLFLAG_RW, sc,
2072 __offsetof(struct hn_rx_ring, hn_csum_tcp),
2073 hn_rx_stat_ulong_sysctl, "LU", "RXCSUM TCP");
2074 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "csum_udp",
2075 CTLTYPE_ULONG | CTLFLAG_RW, sc,
2076 __offsetof(struct hn_rx_ring, hn_csum_udp),
2077 hn_rx_stat_ulong_sysctl, "LU", "RXCSUM UDP");
2078 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "csum_trusted",
2079 CTLTYPE_ULONG | CTLFLAG_RW, sc,
2080 __offsetof(struct hn_rx_ring, hn_csum_trusted),
2081 hn_rx_stat_ulong_sysctl, "LU",
2082 "# of packets that we trust host's csum verification");
2083 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "small_pkts",
2084 CTLTYPE_ULONG | CTLFLAG_RW, sc,
2085 __offsetof(struct hn_rx_ring, hn_small_pkts),
2086 hn_rx_stat_ulong_sysctl, "LU", "# of small packets received");
2090 hn_destroy_rx_data(struct hn_softc *sc)
2092 #if defined(INET) || defined(INET6)
2096 if (sc->hn_rx_ring_cnt == 0)
2099 #if defined(INET) || defined(INET6)
2100 for (i = 0; i < sc->hn_rx_ring_cnt; ++i)
2101 tcp_lro_free(&sc->hn_rx_ring[i].hn_lro);
2103 free(sc->hn_rx_ring, M_NETVSC);
2104 sc->hn_rx_ring = NULL;
2106 sc->hn_rx_ring_cnt = 0;
2110 hn_create_tx_ring(struct hn_softc *sc, int id)
2112 struct hn_tx_ring *txr = &sc->hn_tx_ring[id];
2113 bus_dma_tag_t parent_dtag;
2118 #ifndef HN_USE_TXDESC_BUFRING
2119 mtx_init(&txr->hn_txlist_spin, "hn txlist", NULL, MTX_SPIN);
2121 mtx_init(&txr->hn_tx_lock, "hn tx", NULL, MTX_DEF);
2123 txr->hn_txdesc_cnt = HN_TX_DESC_CNT;
2124 txr->hn_txdesc = malloc(sizeof(struct hn_txdesc) * txr->hn_txdesc_cnt,
2125 M_NETVSC, M_WAITOK | M_ZERO);
2126 #ifndef HN_USE_TXDESC_BUFRING
2127 SLIST_INIT(&txr->hn_txlist);
2129 txr->hn_txdesc_br = buf_ring_alloc(txr->hn_txdesc_cnt, M_NETVSC,
2130 M_WAITOK, &txr->hn_tx_lock);
2133 txr->hn_tx_taskq = sc->hn_tx_taskq;
2134 TASK_INIT(&txr->hn_start_task, 0, hn_start_taskfunc, txr);
2135 TASK_INIT(&txr->hn_txeof_task, 0, hn_txeof_taskfunc, txr);
2137 txr->hn_direct_tx_size = hn_direct_tx_size;
2138 if (hv_vmbus_protocal_version >= HV_VMBUS_VERSION_WIN8_1)
2139 txr->hn_csum_assist = HN_CSUM_ASSIST;
2141 txr->hn_csum_assist = HN_CSUM_ASSIST_WIN8;
2144 * Always schedule transmission instead of trying to do direct
2145 * transmission. This one gives the best performance so far.
2147 txr->hn_sched_tx = 1;
2149 parent_dtag = bus_get_dma_tag(sc->hn_dev);
2151 /* DMA tag for RNDIS messages. */
2152 error = bus_dma_tag_create(parent_dtag, /* parent */
2153 HN_RNDIS_MSG_ALIGN, /* alignment */
2154 HN_RNDIS_MSG_BOUNDARY, /* boundary */
2155 BUS_SPACE_MAXADDR, /* lowaddr */
2156 BUS_SPACE_MAXADDR, /* highaddr */
2157 NULL, NULL, /* filter, filterarg */
2158 HN_RNDIS_MSG_LEN, /* maxsize */
2160 HN_RNDIS_MSG_LEN, /* maxsegsize */
2162 NULL, /* lockfunc */
2163 NULL, /* lockfuncarg */
2164 &txr->hn_tx_rndis_dtag);
2166 device_printf(sc->hn_dev, "failed to create rndis dmatag\n");
2170 /* DMA tag for data. */
2171 error = bus_dma_tag_create(parent_dtag, /* parent */
2173 HN_TX_DATA_BOUNDARY, /* boundary */
2174 BUS_SPACE_MAXADDR, /* lowaddr */
2175 BUS_SPACE_MAXADDR, /* highaddr */
2176 NULL, NULL, /* filter, filterarg */
2177 HN_TX_DATA_MAXSIZE, /* maxsize */
2178 HN_TX_DATA_SEGCNT_MAX, /* nsegments */
2179 HN_TX_DATA_SEGSIZE, /* maxsegsize */
2181 NULL, /* lockfunc */
2182 NULL, /* lockfuncarg */
2183 &txr->hn_tx_data_dtag);
2185 device_printf(sc->hn_dev, "failed to create data dmatag\n");
2189 for (i = 0; i < txr->hn_txdesc_cnt; ++i) {
2190 struct hn_txdesc *txd = &txr->hn_txdesc[i];
2195 * Allocate and load RNDIS messages.
2197 error = bus_dmamem_alloc(txr->hn_tx_rndis_dtag,
2198 (void **)&txd->rndis_msg,
2199 BUS_DMA_WAITOK | BUS_DMA_COHERENT,
2200 &txd->rndis_msg_dmap);
2202 device_printf(sc->hn_dev,
2203 "failed to allocate rndis_msg, %d\n", i);
2207 error = bus_dmamap_load(txr->hn_tx_rndis_dtag,
2208 txd->rndis_msg_dmap,
2209 txd->rndis_msg, HN_RNDIS_MSG_LEN,
2210 hn_dma_map_paddr, &txd->rndis_msg_paddr,
2213 device_printf(sc->hn_dev,
2214 "failed to load rndis_msg, %d\n", i);
2215 bus_dmamem_free(txr->hn_tx_rndis_dtag,
2216 txd->rndis_msg, txd->rndis_msg_dmap);
2220 /* DMA map for TX data. */
2221 error = bus_dmamap_create(txr->hn_tx_data_dtag, 0,
2224 device_printf(sc->hn_dev,
2225 "failed to allocate tx data dmamap\n");
2226 bus_dmamap_unload(txr->hn_tx_rndis_dtag,
2227 txd->rndis_msg_dmap);
2228 bus_dmamem_free(txr->hn_tx_rndis_dtag,
2229 txd->rndis_msg, txd->rndis_msg_dmap);
2233 /* All set, put it to list */
2234 txd->flags |= HN_TXD_FLAG_ONLIST;
2235 #ifndef HN_USE_TXDESC_BUFRING
2236 SLIST_INSERT_HEAD(&txr->hn_txlist, txd, link);
2238 buf_ring_enqueue(txr->hn_txdesc_br, txd);
2241 txr->hn_txdesc_avail = txr->hn_txdesc_cnt;
2243 if (sc->hn_tx_sysctl_tree != NULL) {
2244 struct sysctl_oid_list *child;
2245 struct sysctl_ctx_list *ctx;
2249 * Create per TX ring sysctl tree:
2250 * dev.hn.UNIT.tx.RINGID
2252 ctx = device_get_sysctl_ctx(sc->hn_dev);
2253 child = SYSCTL_CHILDREN(sc->hn_tx_sysctl_tree);
2255 snprintf(name, sizeof(name), "%d", id);
2256 txr->hn_tx_sysctl_tree = SYSCTL_ADD_NODE(ctx, child, OID_AUTO,
2257 name, CTLFLAG_RD, 0, "");
2259 if (txr->hn_tx_sysctl_tree != NULL) {
2260 child = SYSCTL_CHILDREN(txr->hn_tx_sysctl_tree);
2262 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "txdesc_avail",
2263 CTLFLAG_RD, &txr->hn_txdesc_avail, 0,
2264 "# of available TX descs");
2272 hn_txdesc_dmamap_destroy(struct hn_txdesc *txd)
2274 struct hn_tx_ring *txr = txd->txr;
2276 KASSERT(txd->m == NULL, ("still has mbuf installed"));
2277 KASSERT((txd->flags & HN_TXD_FLAG_DMAMAP) == 0, ("still dma mapped"));
2279 bus_dmamap_unload(txr->hn_tx_rndis_dtag, txd->rndis_msg_dmap);
2280 bus_dmamem_free(txr->hn_tx_rndis_dtag, txd->rndis_msg,
2281 txd->rndis_msg_dmap);
2282 bus_dmamap_destroy(txr->hn_tx_data_dtag, txd->data_dmap);
2286 hn_destroy_tx_ring(struct hn_tx_ring *txr)
2288 struct hn_txdesc *txd;
2290 if (txr->hn_txdesc == NULL)
2293 #ifndef HN_USE_TXDESC_BUFRING
2294 while ((txd = SLIST_FIRST(&txr->hn_txlist)) != NULL) {
2295 SLIST_REMOVE_HEAD(&txr->hn_txlist, link);
2296 hn_txdesc_dmamap_destroy(txd);
2299 while ((txd = buf_ring_dequeue_sc(txr->hn_txdesc_br)) != NULL)
2300 hn_txdesc_dmamap_destroy(txd);
2303 if (txr->hn_tx_data_dtag != NULL)
2304 bus_dma_tag_destroy(txr->hn_tx_data_dtag);
2305 if (txr->hn_tx_rndis_dtag != NULL)
2306 bus_dma_tag_destroy(txr->hn_tx_rndis_dtag);
2308 #ifdef HN_USE_TXDESC_BUFRING
2309 buf_ring_free(txr->hn_txdesc_br, M_NETVSC);
2312 free(txr->hn_txdesc, M_NETVSC);
2313 txr->hn_txdesc = NULL;
2315 #ifndef HN_USE_TXDESC_BUFRING
2316 mtx_destroy(&txr->hn_txlist_spin);
2318 mtx_destroy(&txr->hn_tx_lock);
2322 hn_create_tx_data(struct hn_softc *sc)
2324 struct sysctl_oid_list *child;
2325 struct sysctl_ctx_list *ctx;
2328 sc->hn_tx_ring_cnt = 1; /* TODO: vRSS */
2329 sc->hn_tx_ring = malloc(sizeof(struct hn_tx_ring) * sc->hn_tx_ring_cnt,
2330 M_NETVSC, M_WAITOK | M_ZERO);
2332 ctx = device_get_sysctl_ctx(sc->hn_dev);
2333 child = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->hn_dev));
2335 /* Create dev.hn.UNIT.tx sysctl tree */
2336 sc->hn_tx_sysctl_tree = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, "tx",
2339 for (i = 0; i < sc->hn_tx_ring_cnt; ++i) {
2342 error = hn_create_tx_ring(sc, i);
2347 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "no_txdescs",
2348 CTLTYPE_ULONG | CTLFLAG_RW, sc,
2349 __offsetof(struct hn_tx_ring, hn_no_txdescs),
2350 hn_tx_stat_ulong_sysctl, "LU", "# of times short of TX descs");
2351 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "send_failed",
2352 CTLTYPE_ULONG | CTLFLAG_RW, sc,
2353 __offsetof(struct hn_tx_ring, hn_send_failed),
2354 hn_tx_stat_ulong_sysctl, "LU", "# of hyper-v sending failure");
2355 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "txdma_failed",
2356 CTLTYPE_ULONG | CTLFLAG_RW, sc,
2357 __offsetof(struct hn_tx_ring, hn_txdma_failed),
2358 hn_tx_stat_ulong_sysctl, "LU", "# of TX DMA failure");
2359 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "tx_collapsed",
2360 CTLTYPE_ULONG | CTLFLAG_RW, sc,
2361 __offsetof(struct hn_tx_ring, hn_tx_collapsed),
2362 hn_tx_stat_ulong_sysctl, "LU", "# of TX mbuf collapsed");
2363 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "tx_chimney",
2364 CTLTYPE_ULONG | CTLFLAG_RW, sc,
2365 __offsetof(struct hn_tx_ring, hn_tx_chimney),
2366 hn_tx_stat_ulong_sysctl, "LU", "# of chimney send");
2367 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "txdesc_cnt",
2368 CTLFLAG_RD, &sc->hn_tx_ring[0].hn_txdesc_cnt, 0,
2369 "# of total TX descs");
2370 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "tx_chimney_max",
2371 CTLFLAG_RD, &sc->hn_tx_chimney_max, 0,
2372 "Chimney send packet size upper boundary");
2373 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "tx_chimney_size",
2374 CTLTYPE_INT | CTLFLAG_RW, sc, 0, hn_tx_chimney_size_sysctl,
2375 "I", "Chimney send packet size limit");
2376 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "direct_tx_size",
2377 CTLTYPE_INT | CTLFLAG_RW, sc,
2378 __offsetof(struct hn_tx_ring, hn_direct_tx_size),
2379 hn_tx_conf_int_sysctl, "I",
2380 "Size of the packet for direct transmission");
2381 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "sched_tx",
2382 CTLTYPE_INT | CTLFLAG_RW, sc,
2383 __offsetof(struct hn_tx_ring, hn_sched_tx),
2384 hn_tx_conf_int_sysctl, "I",
2385 "Always schedule transmission "
2386 "instead of doing direct transmission");
2392 hn_set_tx_chimney_size(struct hn_softc *sc, int chimney_size)
2397 for (i = 0; i < sc->hn_tx_ring_cnt; ++i)
2398 sc->hn_tx_ring[i].hn_tx_chimney_size = chimney_size;
2403 hn_destroy_tx_data(struct hn_softc *sc)
2407 if (sc->hn_tx_ring_cnt == 0)
2410 for (i = 0; i < sc->hn_tx_ring_cnt; ++i)
2411 hn_destroy_tx_ring(&sc->hn_tx_ring[i]);
2413 free(sc->hn_tx_ring, M_NETVSC);
2414 sc->hn_tx_ring = NULL;
2416 sc->hn_tx_ring_cnt = 0;
2420 hn_start_taskfunc(void *xtxr, int pending __unused)
2422 struct hn_tx_ring *txr = xtxr;
2424 mtx_lock(&txr->hn_tx_lock);
2425 hn_start_locked(txr, 0);
2426 mtx_unlock(&txr->hn_tx_lock);
2430 hn_txeof_taskfunc(void *xtxr, int pending __unused)
2432 struct hn_tx_ring *txr = xtxr;
2434 mtx_lock(&txr->hn_tx_lock);
2435 atomic_clear_int(&txr->hn_sc->hn_ifp->if_drv_flags, IFF_DRV_OACTIVE);
2436 hn_start_locked(txr, 0);
2437 mtx_unlock(&txr->hn_tx_lock);
2441 hn_stop_tx_tasks(struct hn_softc *sc)
2445 for (i = 0; i < sc->hn_tx_ring_cnt; ++i) {
2446 struct hn_tx_ring *txr = &sc->hn_tx_ring[i];
2448 taskqueue_drain(txr->hn_tx_taskq, &txr->hn_start_task);
2449 taskqueue_drain(txr->hn_tx_taskq, &txr->hn_txeof_task);
2454 hn_tx_taskq_create(void *arg __unused)
2456 if (!hn_share_tx_taskq)
2459 hn_tx_taskq = taskqueue_create("hn_tx", M_WAITOK,
2460 taskqueue_thread_enqueue, &hn_tx_taskq);
2461 taskqueue_start_threads(&hn_tx_taskq, 1, PI_NET, "hn tx");
2462 if (hn_bind_tx_taskq >= 0) {
2463 int cpu = hn_bind_tx_taskq;
2464 struct task cpuset_task;
2467 if (cpu > mp_ncpus - 1)
2469 CPU_SETOF(cpu, &cpu_set);
2470 TASK_INIT(&cpuset_task, 0, hn_cpuset_setthread_task, &cpu_set);
2471 taskqueue_enqueue(hn_tx_taskq, &cpuset_task);
2472 taskqueue_drain(hn_tx_taskq, &cpuset_task);
2475 SYSINIT(hn_txtq_create, SI_SUB_DRIVERS, SI_ORDER_FIRST,
2476 hn_tx_taskq_create, NULL);
2479 hn_tx_taskq_destroy(void *arg __unused)
2481 if (hn_tx_taskq != NULL)
2482 taskqueue_free(hn_tx_taskq);
2484 SYSUNINIT(hn_txtq_destroy, SI_SUB_DRIVERS, SI_ORDER_FIRST,
2485 hn_tx_taskq_destroy, NULL);
2487 static device_method_t netvsc_methods[] = {
2488 /* Device interface */
2489 DEVMETHOD(device_probe, netvsc_probe),
2490 DEVMETHOD(device_attach, netvsc_attach),
2491 DEVMETHOD(device_detach, netvsc_detach),
2492 DEVMETHOD(device_shutdown, netvsc_shutdown),
2497 static driver_t netvsc_driver = {
2503 static devclass_t netvsc_devclass;
2505 DRIVER_MODULE(hn, vmbus, netvsc_driver, netvsc_devclass, 0, 0);
2506 MODULE_VERSION(hn, 1);
2507 MODULE_DEPEND(hn, vmbus, 1, 1, 1);