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 #if __FreeBSD_version < 1100095
296 static int hn_rx_stat_int_sysctl(SYSCTL_HANDLER_ARGS);
298 static int hn_rx_stat_ulong_sysctl(SYSCTL_HANDLER_ARGS);
299 static int hn_rx_stat_u64_sysctl(SYSCTL_HANDLER_ARGS);
300 static int hn_tx_stat_ulong_sysctl(SYSCTL_HANDLER_ARGS);
301 static int hn_tx_conf_int_sysctl(SYSCTL_HANDLER_ARGS);
302 static int hn_check_iplen(const struct mbuf *, int);
303 static int hn_create_tx_ring(struct hn_softc *, int);
304 static void hn_destroy_tx_ring(struct hn_tx_ring *);
305 static int hn_create_tx_data(struct hn_softc *);
306 static void hn_destroy_tx_data(struct hn_softc *);
307 static void hn_start_taskfunc(void *, int);
308 static void hn_start_txeof_taskfunc(void *, int);
309 static void hn_stop_tx_tasks(struct hn_softc *);
310 static int hn_encap(struct hn_tx_ring *, struct hn_txdesc *, struct mbuf **);
311 static void hn_create_rx_data(struct hn_softc *sc);
312 static void hn_destroy_rx_data(struct hn_softc *sc);
313 static void hn_set_tx_chimney_size(struct hn_softc *, int);
316 hn_ifmedia_upd(struct ifnet *ifp __unused)
323 hn_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
325 struct hn_softc *sc = ifp->if_softc;
327 ifmr->ifm_status = IFM_AVALID;
328 ifmr->ifm_active = IFM_ETHER;
330 if (!sc->hn_carrier) {
331 ifmr->ifm_active |= IFM_NONE;
334 ifmr->ifm_status |= IFM_ACTIVE;
335 ifmr->ifm_active |= IFM_10G_T | IFM_FDX;
338 /* {F8615163-DF3E-46c5-913F-F2D2F965ED0E} */
339 static const hv_guid g_net_vsc_device_type = {
340 .data = {0x63, 0x51, 0x61, 0xF8, 0x3E, 0xDF, 0xc5, 0x46,
341 0x91, 0x3F, 0xF2, 0xD2, 0xF9, 0x65, 0xED, 0x0E}
345 * Standard probe entry point.
349 netvsc_probe(device_t dev)
353 p = vmbus_get_type(dev);
354 if (!memcmp(p, &g_net_vsc_device_type.data, sizeof(hv_guid))) {
355 device_set_desc(dev, "Synthetic Network Interface");
357 printf("Netvsc probe... DONE \n");
359 return (BUS_PROBE_DEFAULT);
366 hn_cpuset_setthread_task(void *xmask, int pending __unused)
368 cpuset_t *mask = xmask;
371 error = cpuset_setthread(curthread->td_tid, mask);
373 panic("curthread=%ju: can't pin; error=%d",
374 (uintmax_t)curthread->td_tid, error);
379 * Standard attach entry point.
381 * Called when the driver is loaded. It allocates needed resources,
382 * and initializes the "hardware" and software.
385 netvsc_attach(device_t dev)
387 struct hv_device *device_ctx = vmbus_get_devctx(dev);
388 netvsc_device_info device_info;
390 int unit = device_get_unit(dev);
391 struct ifnet *ifp = NULL;
393 #if __FreeBSD_version >= 1100045
397 sc = device_get_softc(dev);
402 bzero(sc, sizeof(hn_softc_t));
406 if (hn_tx_taskq == NULL) {
407 sc->hn_tx_taskq = taskqueue_create("hn_tx", M_WAITOK,
408 taskqueue_thread_enqueue, &sc->hn_tx_taskq);
409 taskqueue_start_threads(&sc->hn_tx_taskq, 1, PI_NET, "%s tx",
410 device_get_nameunit(dev));
411 if (hn_bind_tx_taskq >= 0) {
412 int cpu = hn_bind_tx_taskq;
413 struct task cpuset_task;
416 if (cpu > mp_ncpus - 1)
418 CPU_SETOF(cpu, &cpu_set);
419 TASK_INIT(&cpuset_task, 0, hn_cpuset_setthread_task,
421 taskqueue_enqueue(sc->hn_tx_taskq, &cpuset_task);
422 taskqueue_drain(sc->hn_tx_taskq, &cpuset_task);
425 sc->hn_tx_taskq = hn_tx_taskq;
427 NV_LOCK_INIT(sc, "NetVSCLock");
429 sc->hn_dev_obj = device_ctx;
431 ifp = sc->hn_ifp = sc->arpcom.ac_ifp = if_alloc(IFT_ETHER);
434 error = hn_create_tx_data(sc);
438 hn_create_rx_data(sc);
440 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
441 ifp->if_dunit = unit;
442 ifp->if_dname = NETVSC_DEVNAME;
444 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
445 ifp->if_ioctl = hn_ioctl;
446 ifp->if_start = hn_start;
447 ifp->if_init = hn_ifinit;
448 /* needed by hv_rf_on_device_add() code */
449 ifp->if_mtu = ETHERMTU;
450 IFQ_SET_MAXLEN(&ifp->if_snd, 512);
451 ifp->if_snd.ifq_drv_maxlen = 511;
452 IFQ_SET_READY(&ifp->if_snd);
454 ifmedia_init(&sc->hn_media, 0, hn_ifmedia_upd, hn_ifmedia_sts);
455 ifmedia_add(&sc->hn_media, IFM_ETHER | IFM_AUTO, 0, NULL);
456 ifmedia_set(&sc->hn_media, IFM_ETHER | IFM_AUTO);
457 /* XXX ifmedia_set really should do this for us */
458 sc->hn_media.ifm_media = sc->hn_media.ifm_cur->ifm_media;
461 * Tell upper layers that we support full VLAN capability.
463 ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
464 ifp->if_capabilities |=
465 IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU | IFCAP_HWCSUM | IFCAP_TSO |
468 IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU | IFCAP_HWCSUM | IFCAP_TSO |
470 ifp->if_hwassist = sc->hn_tx_ring[0].hn_csum_assist | CSUM_TSO;
472 error = hv_rf_on_device_add(device_ctx, &device_info);
476 if (device_info.link_state == 0) {
480 #if __FreeBSD_version >= 1100045
481 tso_maxlen = hn_tso_maxlen;
482 if (tso_maxlen <= 0 || tso_maxlen > IP_MAXPACKET)
483 tso_maxlen = IP_MAXPACKET;
485 ifp->if_hw_tsomaxsegcount = HN_TX_DATA_SEGCNT_MAX;
486 ifp->if_hw_tsomaxsegsize = PAGE_SIZE;
487 ifp->if_hw_tsomax = tso_maxlen -
488 (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN);
491 ether_ifattach(ifp, device_info.mac_addr);
493 #if __FreeBSD_version >= 1100045
494 if_printf(ifp, "TSO: %u/%u/%u\n", ifp->if_hw_tsomax,
495 ifp->if_hw_tsomaxsegcount, ifp->if_hw_tsomaxsegsize);
498 sc->hn_tx_chimney_max = sc->net_dev->send_section_size;
499 hn_set_tx_chimney_size(sc, sc->hn_tx_chimney_max);
500 if (hn_tx_chimney_size > 0 &&
501 hn_tx_chimney_size < sc->hn_tx_chimney_max)
502 hn_set_tx_chimney_size(sc, hn_tx_chimney_size);
506 hn_destroy_tx_data(sc);
513 * Standard detach entry point
516 netvsc_detach(device_t dev)
518 struct hn_softc *sc = device_get_softc(dev);
519 struct hv_device *hv_device = vmbus_get_devctx(dev);
522 printf("netvsc_detach\n");
525 * XXXKYS: Need to clean up all our
526 * driver state; this is the driver
531 * XXXKYS: Need to stop outgoing traffic and unregister
535 hv_rf_on_device_remove(hv_device, HV_RF_NV_DESTROY_CHANNEL);
537 hn_stop_tx_tasks(sc);
539 ifmedia_removeall(&sc->hn_media);
540 hn_destroy_rx_data(sc);
541 hn_destroy_tx_data(sc);
543 if (sc->hn_tx_taskq != hn_tx_taskq)
544 taskqueue_free(sc->hn_tx_taskq);
550 * Standard shutdown entry point
553 netvsc_shutdown(device_t dev)
559 hn_txdesc_dmamap_load(struct hn_tx_ring *txr, struct hn_txdesc *txd,
560 struct mbuf **m_head, bus_dma_segment_t *segs, int *nsegs)
562 struct mbuf *m = *m_head;
565 error = bus_dmamap_load_mbuf_sg(txr->hn_tx_data_dtag, txd->data_dmap,
566 m, segs, nsegs, BUS_DMA_NOWAIT);
567 if (error == EFBIG) {
570 m_new = m_collapse(m, M_NOWAIT, HN_TX_DATA_SEGCNT_MAX);
575 txr->hn_tx_collapsed++;
577 error = bus_dmamap_load_mbuf_sg(txr->hn_tx_data_dtag,
578 txd->data_dmap, m, segs, nsegs, BUS_DMA_NOWAIT);
581 bus_dmamap_sync(txr->hn_tx_data_dtag, txd->data_dmap,
582 BUS_DMASYNC_PREWRITE);
583 txd->flags |= HN_TXD_FLAG_DMAMAP;
589 hn_txdesc_dmamap_unload(struct hn_tx_ring *txr, struct hn_txdesc *txd)
592 if (txd->flags & HN_TXD_FLAG_DMAMAP) {
593 bus_dmamap_sync(txr->hn_tx_data_dtag,
594 txd->data_dmap, BUS_DMASYNC_POSTWRITE);
595 bus_dmamap_unload(txr->hn_tx_data_dtag,
597 txd->flags &= ~HN_TXD_FLAG_DMAMAP;
602 hn_txdesc_put(struct hn_tx_ring *txr, struct hn_txdesc *txd)
605 KASSERT((txd->flags & HN_TXD_FLAG_ONLIST) == 0,
606 ("put an onlist txd %#x", txd->flags));
608 KASSERT(txd->refs > 0, ("invalid txd refs %d", txd->refs));
609 if (atomic_fetchadd_int(&txd->refs, -1) != 1)
612 hn_txdesc_dmamap_unload(txr, txd);
613 if (txd->m != NULL) {
618 txd->flags |= HN_TXD_FLAG_ONLIST;
620 #ifndef HN_USE_TXDESC_BUFRING
621 mtx_lock_spin(&txr->hn_txlist_spin);
622 KASSERT(txr->hn_txdesc_avail >= 0 &&
623 txr->hn_txdesc_avail < txr->hn_txdesc_cnt,
624 ("txdesc_put: invalid txd avail %d", txr->hn_txdesc_avail));
625 txr->hn_txdesc_avail++;
626 SLIST_INSERT_HEAD(&txr->hn_txlist, txd, link);
627 mtx_unlock_spin(&txr->hn_txlist_spin);
629 atomic_add_int(&txr->hn_txdesc_avail, 1);
630 buf_ring_enqueue(txr->hn_txdesc_br, txd);
636 static __inline struct hn_txdesc *
637 hn_txdesc_get(struct hn_tx_ring *txr)
639 struct hn_txdesc *txd;
641 #ifndef HN_USE_TXDESC_BUFRING
642 mtx_lock_spin(&txr->hn_txlist_spin);
643 txd = SLIST_FIRST(&txr->hn_txlist);
645 KASSERT(txr->hn_txdesc_avail > 0,
646 ("txdesc_get: invalid txd avail %d", txr->hn_txdesc_avail));
647 txr->hn_txdesc_avail--;
648 SLIST_REMOVE_HEAD(&txr->hn_txlist, link);
650 mtx_unlock_spin(&txr->hn_txlist_spin);
652 txd = buf_ring_dequeue_sc(txr->hn_txdesc_br);
656 #ifdef HN_USE_TXDESC_BUFRING
657 atomic_subtract_int(&txr->hn_txdesc_avail, 1);
659 KASSERT(txd->m == NULL && txd->refs == 0 &&
660 (txd->flags & HN_TXD_FLAG_ONLIST), ("invalid txd"));
661 txd->flags &= ~HN_TXD_FLAG_ONLIST;
668 hn_txdesc_hold(struct hn_txdesc *txd)
671 /* 0->1 transition will never work */
672 KASSERT(txd->refs > 0, ("invalid refs %d", txd->refs));
673 atomic_add_int(&txd->refs, 1);
677 hn_tx_done(void *xpkt)
679 netvsc_packet *packet = xpkt;
680 struct hn_txdesc *txd;
681 struct hn_tx_ring *txr;
683 txd = (struct hn_txdesc *)(uintptr_t)
684 packet->compl.send.send_completion_tid;
687 txr->hn_has_txeof = 1;
688 hn_txdesc_put(txr, txd);
692 netvsc_channel_rollup(struct hv_device *device_ctx)
694 struct hn_softc *sc = device_get_softc(device_ctx->device);
695 struct hn_tx_ring *txr = &sc->hn_tx_ring[0]; /* TODO: vRSS */
696 #if defined(INET) || defined(INET6)
697 struct hn_rx_ring *rxr = &sc->hn_rx_ring[0]; /* TODO: vRSS */
698 struct lro_ctrl *lro = &rxr->hn_lro;
699 struct lro_entry *queued;
701 while ((queued = SLIST_FIRST(&lro->lro_active)) != NULL) {
702 SLIST_REMOVE_HEAD(&lro->lro_active, next);
703 tcp_lro_flush(lro, queued);
707 if (!txr->hn_has_txeof)
710 txr->hn_has_txeof = 0;
716 * If this function fails, then both txd and m_head0 will be freed.
719 hn_encap(struct hn_tx_ring *txr, struct hn_txdesc *txd, struct mbuf **m_head0)
721 bus_dma_segment_t segs[HN_TX_DATA_SEGCNT_MAX];
723 struct mbuf *m_head = *m_head0;
724 netvsc_packet *packet;
725 rndis_msg *rndis_mesg;
726 rndis_packet *rndis_pkt;
727 rndis_per_packet_info *rppi;
728 uint32_t rndis_msg_size;
730 packet = &txd->netvsc_pkt;
731 packet->is_data_pkt = TRUE;
732 packet->tot_data_buf_len = m_head->m_pkthdr.len;
735 * extension points to the area reserved for the
736 * rndis_filter_packet, which is placed just after
737 * the netvsc_packet (and rppi struct, if present;
738 * length is updated later).
740 rndis_mesg = txd->rndis_msg;
741 /* XXX not necessary */
742 memset(rndis_mesg, 0, HN_RNDIS_MSG_LEN);
743 rndis_mesg->ndis_msg_type = REMOTE_NDIS_PACKET_MSG;
745 rndis_pkt = &rndis_mesg->msg.packet;
746 rndis_pkt->data_offset = sizeof(rndis_packet);
747 rndis_pkt->data_length = packet->tot_data_buf_len;
748 rndis_pkt->per_pkt_info_offset = sizeof(rndis_packet);
750 rndis_msg_size = RNDIS_MESSAGE_SIZE(rndis_packet);
752 if (m_head->m_flags & M_VLANTAG) {
753 ndis_8021q_info *rppi_vlan_info;
755 rndis_msg_size += RNDIS_VLAN_PPI_SIZE;
756 rppi = hv_set_rppi_data(rndis_mesg, RNDIS_VLAN_PPI_SIZE,
759 rppi_vlan_info = (ndis_8021q_info *)((uint8_t *)rppi +
760 rppi->per_packet_info_offset);
761 rppi_vlan_info->u1.s1.vlan_id =
762 m_head->m_pkthdr.ether_vtag & 0xfff;
765 if (m_head->m_pkthdr.csum_flags & CSUM_TSO) {
766 rndis_tcp_tso_info *tso_info;
767 struct ether_vlan_header *eh;
771 * XXX need m_pullup and use mtodo
773 eh = mtod(m_head, struct ether_vlan_header*);
774 if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN))
775 ether_len = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
777 ether_len = ETHER_HDR_LEN;
779 rndis_msg_size += RNDIS_TSO_PPI_SIZE;
780 rppi = hv_set_rppi_data(rndis_mesg, RNDIS_TSO_PPI_SIZE,
781 tcp_large_send_info);
783 tso_info = (rndis_tcp_tso_info *)((uint8_t *)rppi +
784 rppi->per_packet_info_offset);
785 tso_info->lso_v2_xmit.type =
786 RNDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE;
789 if (m_head->m_pkthdr.csum_flags & CSUM_IP_TSO) {
791 (struct ip *)(m_head->m_data + ether_len);
792 unsigned long iph_len = ip->ip_hl << 2;
794 (struct tcphdr *)((caddr_t)ip + iph_len);
796 tso_info->lso_v2_xmit.ip_version =
797 RNDIS_TCP_LARGE_SEND_OFFLOAD_IPV4;
801 th->th_sum = in_pseudo(ip->ip_src.s_addr,
802 ip->ip_dst.s_addr, htons(IPPROTO_TCP));
805 #if defined(INET6) && defined(INET)
810 struct ip6_hdr *ip6 = (struct ip6_hdr *)
811 (m_head->m_data + ether_len);
812 struct tcphdr *th = (struct tcphdr *)(ip6 + 1);
814 tso_info->lso_v2_xmit.ip_version =
815 RNDIS_TCP_LARGE_SEND_OFFLOAD_IPV6;
817 th->th_sum = in6_cksum_pseudo(ip6, 0, IPPROTO_TCP, 0);
820 tso_info->lso_v2_xmit.tcp_header_offset = 0;
821 tso_info->lso_v2_xmit.mss = m_head->m_pkthdr.tso_segsz;
822 } else if (m_head->m_pkthdr.csum_flags & txr->hn_csum_assist) {
823 rndis_tcp_ip_csum_info *csum_info;
825 rndis_msg_size += RNDIS_CSUM_PPI_SIZE;
826 rppi = hv_set_rppi_data(rndis_mesg, RNDIS_CSUM_PPI_SIZE,
828 csum_info = (rndis_tcp_ip_csum_info *)((uint8_t *)rppi +
829 rppi->per_packet_info_offset);
831 csum_info->xmit.is_ipv4 = 1;
832 if (m_head->m_pkthdr.csum_flags & CSUM_IP)
833 csum_info->xmit.ip_header_csum = 1;
835 if (m_head->m_pkthdr.csum_flags & CSUM_TCP) {
836 csum_info->xmit.tcp_csum = 1;
837 csum_info->xmit.tcp_header_offset = 0;
838 } else if (m_head->m_pkthdr.csum_flags & CSUM_UDP) {
839 csum_info->xmit.udp_csum = 1;
843 rndis_mesg->msg_len = packet->tot_data_buf_len + rndis_msg_size;
844 packet->tot_data_buf_len = rndis_mesg->msg_len;
847 * Chimney send, if the packet could fit into one chimney buffer.
849 if (packet->tot_data_buf_len < txr->hn_tx_chimney_size) {
850 netvsc_dev *net_dev = txr->hn_sc->net_dev;
851 uint32_t send_buf_section_idx;
853 send_buf_section_idx =
854 hv_nv_get_next_send_section(net_dev);
855 if (send_buf_section_idx !=
856 NVSP_1_CHIMNEY_SEND_INVALID_SECTION_INDEX) {
857 uint8_t *dest = ((uint8_t *)net_dev->send_buf +
858 (send_buf_section_idx *
859 net_dev->send_section_size));
861 memcpy(dest, rndis_mesg, rndis_msg_size);
862 dest += rndis_msg_size;
863 m_copydata(m_head, 0, m_head->m_pkthdr.len, dest);
865 packet->send_buf_section_idx = send_buf_section_idx;
866 packet->send_buf_section_size =
867 packet->tot_data_buf_len;
868 packet->page_buf_count = 0;
869 txr->hn_tx_chimney++;
874 error = hn_txdesc_dmamap_load(txr, txd, &m_head, segs, &nsegs);
879 * This mbuf is not linked w/ the txd yet, so free it now.
884 freed = hn_txdesc_put(txr, txd);
886 ("fail to free txd upon txdma error"));
888 txr->hn_txdma_failed++;
889 if_inc_counter(txr->hn_sc->hn_ifp, IFCOUNTER_OERRORS, 1);
894 packet->page_buf_count = nsegs + HV_RF_NUM_TX_RESERVED_PAGE_BUFS;
896 /* send packet with page buffer */
897 packet->page_buffers[0].pfn = atop(txd->rndis_msg_paddr);
898 packet->page_buffers[0].offset = txd->rndis_msg_paddr & PAGE_MASK;
899 packet->page_buffers[0].length = rndis_msg_size;
902 * Fill the page buffers with mbuf info starting at index
903 * HV_RF_NUM_TX_RESERVED_PAGE_BUFS.
905 for (i = 0; i < nsegs; ++i) {
906 hv_vmbus_page_buffer *pb = &packet->page_buffers[
907 i + HV_RF_NUM_TX_RESERVED_PAGE_BUFS];
909 pb->pfn = atop(segs[i].ds_addr);
910 pb->offset = segs[i].ds_addr & PAGE_MASK;
911 pb->length = segs[i].ds_len;
914 packet->send_buf_section_idx =
915 NVSP_1_CHIMNEY_SEND_INVALID_SECTION_INDEX;
916 packet->send_buf_section_size = 0;
920 /* Set the completion routine */
921 packet->compl.send.on_send_completion = hn_tx_done;
922 packet->compl.send.send_completion_context = packet;
923 packet->compl.send.send_completion_tid = (uint64_t)(uintptr_t)txd;
930 * If this function fails, then txd will be freed, but the mbuf
931 * associated w/ the txd will _not_ be freed.
934 hn_send_pkt(struct ifnet *ifp, struct hv_device *device_ctx,
935 struct hn_tx_ring *txr, struct hn_txdesc *txd)
937 int error, send_failed = 0;
941 * Make sure that txd is not freed before ETHER_BPF_MTAP.
944 error = hv_nv_on_send(device_ctx, &txd->netvsc_pkt);
946 ETHER_BPF_MTAP(ifp, txd->m);
947 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
949 hn_txdesc_put(txr, txd);
951 if (__predict_false(error)) {
955 * This should "really rarely" happen.
957 * XXX Too many RX to be acked or too many sideband
958 * commands to run? Ask netvsc_channel_rollup()
959 * to kick start later.
961 txr->hn_has_txeof = 1;
963 txr->hn_send_failed++;
966 * Try sending again after set hn_has_txeof;
967 * in case that we missed the last
968 * netvsc_channel_rollup().
972 if_printf(ifp, "send failed\n");
975 * Caller will perform further processing on the
976 * associated mbuf, so don't free it in hn_txdesc_put();
977 * only unload it from the DMA map in hn_txdesc_put(),
981 freed = hn_txdesc_put(txr, txd);
983 ("fail to free txd upon send error"));
985 txr->hn_send_failed++;
991 * Start a transmit of one or more packets
994 hn_start_locked(struct hn_tx_ring *txr, int len)
996 struct hn_softc *sc = txr->hn_sc;
997 struct ifnet *ifp = sc->hn_ifp;
998 struct hv_device *device_ctx = vmbus_get_devctx(sc->hn_dev);
1000 KASSERT(txr == &sc->hn_tx_ring[0], ("not the first TX ring"));
1001 mtx_assert(&txr->hn_tx_lock, MA_OWNED);
1003 if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
1007 while (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) {
1008 struct hn_txdesc *txd;
1009 struct mbuf *m_head;
1012 IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
1016 if (len > 0 && m_head->m_pkthdr.len > len) {
1018 * This sending could be time consuming; let callers
1019 * dispatch this packet sending (and sending of any
1020 * following up packets) to tx taskqueue.
1022 IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
1026 txd = hn_txdesc_get(txr);
1028 txr->hn_no_txdescs++;
1029 IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
1030 atomic_set_int(&ifp->if_drv_flags, IFF_DRV_OACTIVE);
1034 error = hn_encap(txr, txd, &m_head);
1036 /* Both txd and m_head are freed */
1040 error = hn_send_pkt(ifp, device_ctx, txr, txd);
1041 if (__predict_false(error)) {
1042 /* txd is freed, but m_head is not */
1043 IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
1044 atomic_set_int(&ifp->if_drv_flags, IFF_DRV_OACTIVE);
1052 * Link up/down notification
1055 netvsc_linkstatus_callback(struct hv_device *device_obj, uint32_t status)
1057 hn_softc_t *sc = device_get_softc(device_obj->device);
1071 * Append the specified data to the indicated mbuf chain,
1072 * Extend the mbuf chain if the new data does not fit in
1075 * This is a minor rewrite of m_append() from sys/kern/uipc_mbuf.c.
1076 * There should be an equivalent in the kernel mbuf code,
1077 * but there does not appear to be one yet.
1079 * Differs from m_append() in that additional mbufs are
1080 * allocated with cluster size MJUMPAGESIZE, and filled
1083 * Return 1 if able to complete the job; otherwise 0.
1086 hv_m_append(struct mbuf *m0, int len, c_caddr_t cp)
1089 int remainder, space;
1091 for (m = m0; m->m_next != NULL; m = m->m_next)
1094 space = M_TRAILINGSPACE(m);
1097 * Copy into available space.
1099 if (space > remainder)
1101 bcopy(cp, mtod(m, caddr_t) + m->m_len, space);
1106 while (remainder > 0) {
1108 * Allocate a new mbuf; could check space
1109 * and allocate a cluster instead.
1111 n = m_getjcl(M_DONTWAIT, m->m_type, 0, MJUMPAGESIZE);
1114 n->m_len = min(MJUMPAGESIZE, remainder);
1115 bcopy(cp, mtod(n, caddr_t), n->m_len);
1117 remainder -= n->m_len;
1121 if (m0->m_flags & M_PKTHDR)
1122 m0->m_pkthdr.len += len - remainder;
1124 return (remainder == 0);
1129 * Called when we receive a data packet from the "wire" on the
1132 * Note: This is no longer used as a callback
1135 netvsc_recv(struct hv_device *device_ctx, netvsc_packet *packet,
1136 rndis_tcp_ip_csum_info *csum_info)
1138 struct hn_softc *sc = device_get_softc(device_ctx->device);
1139 struct hn_rx_ring *rxr = &sc->hn_rx_ring[0]; /* TODO: vRSS */
1142 int size, do_lro = 0, do_csum = 1;
1145 return (0); /* TODO: KYS how can this be! */
1150 ifp = sc->arpcom.ac_ifp;
1152 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1157 * Bail out if packet contains more data than configured MTU.
1159 if (packet->tot_data_buf_len > (ifp->if_mtu + ETHER_HDR_LEN)) {
1161 } else if (packet->tot_data_buf_len <= MHLEN) {
1162 m_new = m_gethdr(M_NOWAIT, MT_DATA);
1165 memcpy(mtod(m_new, void *), packet->data,
1166 packet->tot_data_buf_len);
1167 m_new->m_pkthdr.len = m_new->m_len = packet->tot_data_buf_len;
1168 rxr->hn_small_pkts++;
1171 * Get an mbuf with a cluster. For packets 2K or less,
1172 * get a standard 2K cluster. For anything larger, get a
1173 * 4K cluster. Any buffers larger than 4K can cause problems
1174 * if looped around to the Hyper-V TX channel, so avoid them.
1177 if (packet->tot_data_buf_len > MCLBYTES) {
1179 size = MJUMPAGESIZE;
1182 m_new = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, size);
1183 if (m_new == NULL) {
1184 if_printf(ifp, "alloc mbuf failed.\n");
1188 hv_m_append(m_new, packet->tot_data_buf_len, packet->data);
1190 m_new->m_pkthdr.rcvif = ifp;
1192 if (__predict_false((ifp->if_capenable & IFCAP_RXCSUM) == 0))
1195 /* receive side checksum offload */
1196 if (csum_info != NULL) {
1197 /* IP csum offload */
1198 if (csum_info->receive.ip_csum_succeeded && do_csum) {
1199 m_new->m_pkthdr.csum_flags |=
1200 (CSUM_IP_CHECKED | CSUM_IP_VALID);
1204 /* TCP/UDP csum offload */
1205 if ((csum_info->receive.tcp_csum_succeeded ||
1206 csum_info->receive.udp_csum_succeeded) && do_csum) {
1207 m_new->m_pkthdr.csum_flags |=
1208 (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
1209 m_new->m_pkthdr.csum_data = 0xffff;
1210 if (csum_info->receive.tcp_csum_succeeded)
1216 if (csum_info->receive.ip_csum_succeeded &&
1217 csum_info->receive.tcp_csum_succeeded)
1220 const struct ether_header *eh;
1225 if (m_new->m_len < hoff)
1227 eh = mtod(m_new, struct ether_header *);
1228 etype = ntohs(eh->ether_type);
1229 if (etype == ETHERTYPE_VLAN) {
1230 const struct ether_vlan_header *evl;
1232 hoff = sizeof(*evl);
1233 if (m_new->m_len < hoff)
1235 evl = mtod(m_new, struct ether_vlan_header *);
1236 etype = ntohs(evl->evl_proto);
1239 if (etype == ETHERTYPE_IP) {
1242 pr = hn_check_iplen(m_new, hoff);
1243 if (pr == IPPROTO_TCP) {
1245 (rxr->hn_trust_hcsum &
1246 HN_TRUST_HCSUM_TCP)) {
1247 rxr->hn_csum_trusted++;
1248 m_new->m_pkthdr.csum_flags |=
1249 (CSUM_IP_CHECKED | CSUM_IP_VALID |
1250 CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
1251 m_new->m_pkthdr.csum_data = 0xffff;
1253 /* Rely on SW csum verification though... */
1255 } else if (pr == IPPROTO_UDP) {
1257 (rxr->hn_trust_hcsum &
1258 HN_TRUST_HCSUM_UDP)) {
1259 rxr->hn_csum_trusted++;
1260 m_new->m_pkthdr.csum_flags |=
1261 (CSUM_IP_CHECKED | CSUM_IP_VALID |
1262 CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
1263 m_new->m_pkthdr.csum_data = 0xffff;
1265 } else if (pr != IPPROTO_DONE && do_csum &&
1266 (rxr->hn_trust_hcsum & HN_TRUST_HCSUM_IP)) {
1267 rxr->hn_csum_trusted++;
1268 m_new->m_pkthdr.csum_flags |=
1269 (CSUM_IP_CHECKED | CSUM_IP_VALID);
1274 if ((packet->vlan_tci != 0) &&
1275 (ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0) {
1276 m_new->m_pkthdr.ether_vtag = packet->vlan_tci;
1277 m_new->m_flags |= M_VLANTAG;
1281 * Note: Moved RX completion back to hv_nv_on_receive() so all
1282 * messages (not just data messages) will trigger a response.
1287 if ((ifp->if_capenable & IFCAP_LRO) && do_lro) {
1288 #if defined(INET) || defined(INET6)
1289 struct lro_ctrl *lro = &rxr->hn_lro;
1292 rxr->hn_lro_tried++;
1293 if (tcp_lro_rx(lro, m_new, 0) == 0) {
1301 /* We're not holding the lock here, so don't release it */
1302 (*ifp->if_input)(ifp, m_new);
1308 netvsc_recv_rollup(struct hv_device *device_ctx __unused)
1313 * Rules for using sc->temp_unusable:
1314 * 1. sc->temp_unusable can only be read or written while holding NV_LOCK()
1315 * 2. code reading sc->temp_unusable under NV_LOCK(), and finding
1316 * sc->temp_unusable set, must release NV_LOCK() and exit
1317 * 3. to retain exclusive control of the interface,
1318 * sc->temp_unusable must be set by code before releasing NV_LOCK()
1319 * 4. only code setting sc->temp_unusable can clear sc->temp_unusable
1320 * 5. code setting sc->temp_unusable must eventually clear sc->temp_unusable
1324 * Standard ioctl entry point. Called when the user wants to configure
1328 hn_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1330 hn_softc_t *sc = ifp->if_softc;
1331 struct ifreq *ifr = (struct ifreq *)data;
1333 struct ifaddr *ifa = (struct ifaddr *)data;
1335 netvsc_device_info device_info;
1336 struct hv_device *hn_dev;
1337 int mask, error = 0;
1338 int retry_cnt = 500;
1344 if (ifa->ifa_addr->sa_family == AF_INET) {
1345 ifp->if_flags |= IFF_UP;
1346 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
1348 arp_ifinit(ifp, ifa);
1351 error = ether_ioctl(ifp, cmd, data);
1354 hn_dev = vmbus_get_devctx(sc->hn_dev);
1356 /* Check MTU value change */
1357 if (ifp->if_mtu == ifr->ifr_mtu)
1360 if (ifr->ifr_mtu > NETVSC_MAX_CONFIGURABLE_MTU) {
1365 /* Obtain and record requested MTU */
1366 ifp->if_mtu = ifr->ifr_mtu;
1368 #if __FreeBSD_version >= 1100099
1370 * Make sure that LRO aggregation length limit is still
1371 * valid, after the MTU change.
1374 if (sc->hn_rx_ring[0].hn_lro.lro_length_lim <
1375 HN_LRO_LENLIM_MIN(ifp)) {
1377 for (i = 0; i < sc->hn_rx_ring_cnt; ++i) {
1378 sc->hn_rx_ring[i].hn_lro.lro_length_lim =
1379 HN_LRO_LENLIM_MIN(ifp);
1387 if (!sc->temp_unusable) {
1388 sc->temp_unusable = TRUE;
1392 if (retry_cnt > 0) {
1396 } while (retry_cnt > 0);
1398 if (retry_cnt == 0) {
1403 /* We must remove and add back the device to cause the new
1404 * MTU to take effect. This includes tearing down, but not
1405 * deleting the channel, then bringing it back up.
1407 error = hv_rf_on_device_remove(hn_dev, HV_RF_NV_RETAIN_CHANNEL);
1410 sc->temp_unusable = FALSE;
1414 error = hv_rf_on_device_add(hn_dev, &device_info);
1417 sc->temp_unusable = FALSE;
1422 sc->hn_tx_chimney_max = sc->net_dev->send_section_size;
1423 if (sc->hn_tx_ring[0].hn_tx_chimney_size >
1424 sc->hn_tx_chimney_max)
1425 hn_set_tx_chimney_size(sc, sc->hn_tx_chimney_max);
1427 hn_ifinit_locked(sc);
1430 sc->temp_unusable = FALSE;
1436 if (!sc->temp_unusable) {
1437 sc->temp_unusable = TRUE;
1441 if (retry_cnt > 0) {
1445 } while (retry_cnt > 0);
1447 if (retry_cnt == 0) {
1452 if (ifp->if_flags & IFF_UP) {
1454 * If only the state of the PROMISC flag changed,
1455 * then just use the 'set promisc mode' command
1456 * instead of reinitializing the entire NIC. Doing
1457 * a full re-init means reloading the firmware and
1458 * waiting for it to start up, which may take a
1462 /* Fixme: Promiscuous mode? */
1463 if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
1464 ifp->if_flags & IFF_PROMISC &&
1465 !(sc->hn_if_flags & IFF_PROMISC)) {
1466 /* do something here for Hyper-V */
1467 } else if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
1468 !(ifp->if_flags & IFF_PROMISC) &&
1469 sc->hn_if_flags & IFF_PROMISC) {
1470 /* do something here for Hyper-V */
1473 hn_ifinit_locked(sc);
1475 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1480 sc->temp_unusable = FALSE;
1482 sc->hn_if_flags = ifp->if_flags;
1488 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1489 if (mask & IFCAP_TXCSUM) {
1490 ifp->if_capenable ^= IFCAP_TXCSUM;
1491 if (ifp->if_capenable & IFCAP_TXCSUM) {
1493 sc->hn_tx_ring[0].hn_csum_assist;
1496 ~sc->hn_tx_ring[0].hn_csum_assist;
1500 if (mask & IFCAP_RXCSUM)
1501 ifp->if_capenable ^= IFCAP_RXCSUM;
1503 if (mask & IFCAP_LRO)
1504 ifp->if_capenable ^= IFCAP_LRO;
1506 if (mask & IFCAP_TSO4) {
1507 ifp->if_capenable ^= IFCAP_TSO4;
1508 if (ifp->if_capenable & IFCAP_TSO4)
1509 ifp->if_hwassist |= CSUM_IP_TSO;
1511 ifp->if_hwassist &= ~CSUM_IP_TSO;
1514 if (mask & IFCAP_TSO6) {
1515 ifp->if_capenable ^= IFCAP_TSO6;
1516 if (ifp->if_capenable & IFCAP_TSO6)
1517 ifp->if_hwassist |= CSUM_IP6_TSO;
1519 ifp->if_hwassist &= ~CSUM_IP6_TSO;
1528 /* Fixme: Multicast mode? */
1529 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1531 netvsc_setmulti(sc);
1540 error = ifmedia_ioctl(ifp, ifr, &sc->hn_media, cmd);
1543 error = ether_ioctl(ifp, cmd, data);
1554 hn_stop(hn_softc_t *sc)
1558 struct hv_device *device_ctx = vmbus_get_devctx(sc->hn_dev);
1563 printf(" Closing Device ...\n");
1565 atomic_clear_int(&ifp->if_drv_flags,
1566 (IFF_DRV_RUNNING | IFF_DRV_OACTIVE));
1567 if_link_state_change(ifp, LINK_STATE_DOWN);
1568 sc->hn_initdone = 0;
1570 ret = hv_rf_on_close(device_ctx);
1574 * FreeBSD transmit entry point
1577 hn_start(struct ifnet *ifp)
1579 struct hn_softc *sc = ifp->if_softc;
1580 struct hn_tx_ring *txr = &sc->hn_tx_ring[0];
1582 if (txr->hn_sched_tx)
1585 if (mtx_trylock(&txr->hn_tx_lock)) {
1588 sched = hn_start_locked(txr, txr->hn_direct_tx_size);
1589 mtx_unlock(&txr->hn_tx_lock);
1594 taskqueue_enqueue(txr->hn_tx_taskq, &txr->hn_tx_task);
1598 hn_start_txeof(struct hn_tx_ring *txr)
1600 struct hn_softc *sc = txr->hn_sc;
1601 struct ifnet *ifp = sc->hn_ifp;
1603 KASSERT(txr == &sc->hn_tx_ring[0], ("not the first TX ring"));
1605 if (txr->hn_sched_tx)
1608 if (mtx_trylock(&txr->hn_tx_lock)) {
1611 atomic_clear_int(&ifp->if_drv_flags, IFF_DRV_OACTIVE);
1612 sched = hn_start_locked(txr, txr->hn_direct_tx_size);
1613 mtx_unlock(&txr->hn_tx_lock);
1615 taskqueue_enqueue(txr->hn_tx_taskq,
1621 * Release the OACTIVE earlier, with the hope, that
1622 * others could catch up. The task will clear the
1623 * flag again with the hn_tx_lock to avoid possible
1626 atomic_clear_int(&ifp->if_drv_flags, IFF_DRV_OACTIVE);
1627 taskqueue_enqueue(txr->hn_tx_taskq, &txr->hn_txeof_task);
1635 hn_ifinit_locked(hn_softc_t *sc)
1638 struct hv_device *device_ctx = vmbus_get_devctx(sc->hn_dev);
1643 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1647 hv_promisc_mode = 1;
1649 ret = hv_rf_on_open(device_ctx);
1653 sc->hn_initdone = 1;
1655 atomic_clear_int(&ifp->if_drv_flags, IFF_DRV_OACTIVE);
1656 atomic_set_int(&ifp->if_drv_flags, IFF_DRV_RUNNING);
1657 if_link_state_change(ifp, LINK_STATE_UP);
1664 hn_ifinit(void *xsc)
1666 hn_softc_t *sc = xsc;
1669 if (sc->temp_unusable) {
1673 sc->temp_unusable = TRUE;
1676 hn_ifinit_locked(sc);
1679 sc->temp_unusable = FALSE;
1688 hn_watchdog(struct ifnet *ifp)
1693 printf("hn%d: watchdog timeout -- resetting\n", sc->hn_unit);
1694 hn_ifinit(sc); /*???*/
1699 #if __FreeBSD_version >= 1100099
1702 hn_lro_lenlim_sysctl(SYSCTL_HANDLER_ARGS)
1704 struct hn_softc *sc = arg1;
1705 unsigned int lenlim;
1708 lenlim = sc->hn_rx_ring[0].hn_lro.lro_length_lim;
1709 error = sysctl_handle_int(oidp, &lenlim, 0, req);
1710 if (error || req->newptr == NULL)
1713 if (lenlim < HN_LRO_LENLIM_MIN(sc->hn_ifp) ||
1714 lenlim > TCP_LRO_LENGTH_MAX)
1718 for (i = 0; i < sc->hn_rx_ring_cnt; ++i)
1719 sc->hn_rx_ring[i].hn_lro.lro_length_lim = lenlim;
1725 hn_lro_ackcnt_sysctl(SYSCTL_HANDLER_ARGS)
1727 struct hn_softc *sc = arg1;
1728 int ackcnt, error, i;
1731 * lro_ackcnt_lim is append count limit,
1732 * +1 to turn it into aggregation limit.
1734 ackcnt = sc->hn_rx_ring[0].hn_lro.lro_ackcnt_lim + 1;
1735 error = sysctl_handle_int(oidp, &ackcnt, 0, req);
1736 if (error || req->newptr == NULL)
1739 if (ackcnt < 2 || ackcnt > (TCP_LRO_ACKCNT_MAX + 1))
1743 * Convert aggregation limit back to append
1748 for (i = 0; i < sc->hn_rx_ring_cnt; ++i)
1749 sc->hn_rx_ring[i].hn_lro.lro_ackcnt_lim = ackcnt;
1757 hn_trust_hcsum_sysctl(SYSCTL_HANDLER_ARGS)
1759 struct hn_softc *sc = arg1;
1764 if (sc->hn_rx_ring[0].hn_trust_hcsum & hcsum)
1767 error = sysctl_handle_int(oidp, &on, 0, req);
1768 if (error || req->newptr == NULL)
1772 for (i = 0; i < sc->hn_rx_ring_cnt; ++i) {
1773 struct hn_rx_ring *rxr = &sc->hn_rx_ring[i];
1776 rxr->hn_trust_hcsum |= hcsum;
1778 rxr->hn_trust_hcsum &= ~hcsum;
1785 hn_tx_chimney_size_sysctl(SYSCTL_HANDLER_ARGS)
1787 struct hn_softc *sc = arg1;
1788 int chimney_size, error;
1790 chimney_size = sc->hn_tx_ring[0].hn_tx_chimney_size;
1791 error = sysctl_handle_int(oidp, &chimney_size, 0, req);
1792 if (error || req->newptr == NULL)
1795 if (chimney_size > sc->hn_tx_chimney_max || chimney_size <= 0)
1798 hn_set_tx_chimney_size(sc, chimney_size);
1802 #if __FreeBSD_version < 1100095
1804 hn_rx_stat_int_sysctl(SYSCTL_HANDLER_ARGS)
1806 struct hn_softc *sc = arg1;
1807 int ofs = arg2, i, error;
1808 struct hn_rx_ring *rxr;
1812 for (i = 0; i < sc->hn_rx_ring_cnt; ++i) {
1813 rxr = &sc->hn_rx_ring[i];
1814 stat += *((int *)((uint8_t *)rxr + ofs));
1817 error = sysctl_handle_64(oidp, &stat, 0, req);
1818 if (error || req->newptr == NULL)
1821 /* Zero out this stat. */
1822 for (i = 0; i < sc->hn_rx_ring_cnt; ++i) {
1823 rxr = &sc->hn_rx_ring[i];
1824 *((int *)((uint8_t *)rxr + ofs)) = 0;
1831 hn_rx_stat_ulong_sysctl(SYSCTL_HANDLER_ARGS)
1833 struct hn_softc *sc = arg1;
1834 int ofs = arg2, i, error;
1835 struct hn_rx_ring *rxr;
1839 for (i = 0; i < sc->hn_rx_ring_cnt; ++i) {
1840 rxr = &sc->hn_rx_ring[i];
1841 stat += *((u_long *)((uint8_t *)rxr + ofs));
1844 error = sysctl_handle_long(oidp, &stat, 0, req);
1845 if (error || req->newptr == NULL)
1848 /* Zero out this stat. */
1849 for (i = 0; i < sc->hn_rx_ring_cnt; ++i) {
1850 rxr = &sc->hn_rx_ring[i];
1851 *((u_long *)((uint8_t *)rxr + ofs)) = 0;
1857 hn_rx_stat_u64_sysctl(SYSCTL_HANDLER_ARGS)
1859 struct hn_softc *sc = arg1;
1860 int ofs = arg2, i, error;
1861 struct hn_rx_ring *rxr;
1865 for (i = 0; i < sc->hn_rx_ring_cnt; ++i) {
1866 rxr = &sc->hn_rx_ring[i];
1867 stat += *((uint64_t *)((uint8_t *)rxr + ofs));
1870 error = sysctl_handle_64(oidp, &stat, 0, req);
1871 if (error || req->newptr == NULL)
1874 /* Zero out this stat. */
1875 for (i = 0; i < sc->hn_rx_ring_cnt; ++i) {
1876 rxr = &sc->hn_rx_ring[i];
1877 *((uint64_t *)((uint8_t *)rxr + ofs)) = 0;
1883 hn_tx_stat_ulong_sysctl(SYSCTL_HANDLER_ARGS)
1885 struct hn_softc *sc = arg1;
1886 int ofs = arg2, i, error;
1887 struct hn_tx_ring *txr;
1891 for (i = 0; i < sc->hn_tx_ring_cnt; ++i) {
1892 txr = &sc->hn_tx_ring[i];
1893 stat += *((u_long *)((uint8_t *)txr + ofs));
1896 error = sysctl_handle_long(oidp, &stat, 0, req);
1897 if (error || req->newptr == NULL)
1900 /* Zero out this stat. */
1901 for (i = 0; i < sc->hn_tx_ring_cnt; ++i) {
1902 txr = &sc->hn_tx_ring[i];
1903 *((u_long *)((uint8_t *)txr + ofs)) = 0;
1909 hn_tx_conf_int_sysctl(SYSCTL_HANDLER_ARGS)
1911 struct hn_softc *sc = arg1;
1912 int ofs = arg2, i, error, conf;
1913 struct hn_tx_ring *txr;
1915 txr = &sc->hn_tx_ring[0];
1916 conf = *((int *)((uint8_t *)txr + ofs));
1918 error = sysctl_handle_int(oidp, &conf, 0, req);
1919 if (error || req->newptr == NULL)
1923 for (i = 0; i < sc->hn_tx_ring_cnt; ++i) {
1924 txr = &sc->hn_tx_ring[i];
1925 *((int *)((uint8_t *)txr + ofs)) = conf;
1933 hn_check_iplen(const struct mbuf *m, int hoff)
1935 const struct ip *ip;
1936 int len, iphlen, iplen;
1937 const struct tcphdr *th;
1938 int thoff; /* TCP data offset */
1940 len = hoff + sizeof(struct ip);
1942 /* The packet must be at least the size of an IP header. */
1943 if (m->m_pkthdr.len < len)
1944 return IPPROTO_DONE;
1946 /* The fixed IP header must reside completely in the first mbuf. */
1948 return IPPROTO_DONE;
1950 ip = mtodo(m, hoff);
1952 /* Bound check the packet's stated IP header length. */
1953 iphlen = ip->ip_hl << 2;
1954 if (iphlen < sizeof(struct ip)) /* minimum header length */
1955 return IPPROTO_DONE;
1957 /* The full IP header must reside completely in the one mbuf. */
1958 if (m->m_len < hoff + iphlen)
1959 return IPPROTO_DONE;
1961 iplen = ntohs(ip->ip_len);
1964 * Check that the amount of data in the buffers is as
1965 * at least much as the IP header would have us expect.
1967 if (m->m_pkthdr.len < hoff + iplen)
1968 return IPPROTO_DONE;
1971 * Ignore IP fragments.
1973 if (ntohs(ip->ip_off) & (IP_OFFMASK | IP_MF))
1974 return IPPROTO_DONE;
1977 * The TCP/IP or UDP/IP header must be entirely contained within
1978 * the first fragment of a packet.
1982 if (iplen < iphlen + sizeof(struct tcphdr))
1983 return IPPROTO_DONE;
1984 if (m->m_len < hoff + iphlen + sizeof(struct tcphdr))
1985 return IPPROTO_DONE;
1986 th = (const struct tcphdr *)((const uint8_t *)ip + iphlen);
1987 thoff = th->th_off << 2;
1988 if (thoff < sizeof(struct tcphdr) || thoff + iphlen > iplen)
1989 return IPPROTO_DONE;
1990 if (m->m_len < hoff + iphlen + thoff)
1991 return IPPROTO_DONE;
1994 if (iplen < iphlen + sizeof(struct udphdr))
1995 return IPPROTO_DONE;
1996 if (m->m_len < hoff + iphlen + sizeof(struct udphdr))
1997 return IPPROTO_DONE;
2001 return IPPROTO_DONE;
2008 hn_dma_map_paddr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
2010 bus_addr_t *paddr = arg;
2015 KASSERT(nseg == 1, ("too many segments %d!", nseg));
2016 *paddr = segs->ds_addr;
2020 hn_create_rx_data(struct hn_softc *sc)
2022 struct sysctl_oid_list *child;
2023 struct sysctl_ctx_list *ctx;
2024 device_t dev = sc->hn_dev;
2025 #if defined(INET) || defined(INET6)
2026 #if __FreeBSD_version >= 1100095
2032 sc->hn_rx_ring_cnt = 1; /* TODO: vRSS */
2033 sc->hn_rx_ring = malloc(sizeof(struct hn_rx_ring) * sc->hn_rx_ring_cnt,
2034 M_NETVSC, M_WAITOK | M_ZERO);
2036 #if defined(INET) || defined(INET6)
2037 #if __FreeBSD_version >= 1100095
2038 lroent_cnt = hn_lro_entry_count;
2039 if (lroent_cnt < TCP_LRO_ENTRIES)
2040 lroent_cnt = TCP_LRO_ENTRIES;
2041 device_printf(dev, "LRO: entry count %d\n", lroent_cnt);
2043 #endif /* INET || INET6 */
2045 for (i = 0; i < sc->hn_rx_ring_cnt; ++i) {
2046 struct hn_rx_ring *rxr = &sc->hn_rx_ring[i];
2048 if (hn_trust_hosttcp)
2049 rxr->hn_trust_hcsum |= HN_TRUST_HCSUM_TCP;
2050 if (hn_trust_hostudp)
2051 rxr->hn_trust_hcsum |= HN_TRUST_HCSUM_UDP;
2052 if (hn_trust_hostip)
2053 rxr->hn_trust_hcsum |= HN_TRUST_HCSUM_IP;
2058 #if defined(INET) || defined(INET6)
2059 #if __FreeBSD_version >= 1100095
2060 tcp_lro_init_args(&rxr->hn_lro, sc->hn_ifp, lroent_cnt, 0);
2062 tcp_lro_init(&rxr->hn_lro);
2063 rxr->hn_lro.ifp = sc->hn_ifp;
2065 #if __FreeBSD_version >= 1100099
2066 rxr->hn_lro.lro_length_lim = HN_LRO_LENLIM_DEF;
2067 rxr->hn_lro.lro_ackcnt_lim = HN_LRO_ACKCNT_DEF;
2069 #endif /* INET || INET6 */
2072 ctx = device_get_sysctl_ctx(dev);
2073 child = SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
2075 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "lro_queued",
2076 CTLTYPE_U64 | CTLFLAG_RW, sc,
2077 __offsetof(struct hn_rx_ring, hn_lro.lro_queued),
2078 #if __FreeBSD_version < 1100095
2079 hn_rx_stat_int_sysctl,
2081 hn_rx_stat_u64_sysctl,
2083 "LU", "LRO queued");
2084 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "lro_flushed",
2085 CTLTYPE_U64 | CTLFLAG_RW, sc,
2086 __offsetof(struct hn_rx_ring, hn_lro.lro_flushed),
2087 #if __FreeBSD_version < 1100095
2088 hn_rx_stat_int_sysctl,
2090 hn_rx_stat_u64_sysctl,
2092 "LU", "LRO flushed");
2093 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "lro_tried",
2094 CTLTYPE_ULONG | CTLFLAG_RW, sc,
2095 __offsetof(struct hn_rx_ring, hn_lro_tried),
2096 hn_rx_stat_ulong_sysctl, "LU", "# of LRO tries");
2097 #if __FreeBSD_version >= 1100099
2098 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "lro_length_lim",
2099 CTLTYPE_UINT | CTLFLAG_RW, sc, 0, hn_lro_lenlim_sysctl, "IU",
2100 "Max # of data bytes to be aggregated by LRO");
2101 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "lro_ackcnt_lim",
2102 CTLTYPE_INT | CTLFLAG_RW, sc, 0, hn_lro_ackcnt_sysctl, "I",
2103 "Max # of ACKs to be aggregated by LRO");
2105 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "trust_hosttcp",
2106 CTLTYPE_INT | CTLFLAG_RW, sc, HN_TRUST_HCSUM_TCP,
2107 hn_trust_hcsum_sysctl, "I",
2108 "Trust tcp segement verification on host side, "
2109 "when csum info is missing");
2110 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "trust_hostudp",
2111 CTLTYPE_INT | CTLFLAG_RW, sc, HN_TRUST_HCSUM_UDP,
2112 hn_trust_hcsum_sysctl, "I",
2113 "Trust udp datagram verification on host side, "
2114 "when csum info is missing");
2115 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "trust_hostip",
2116 CTLTYPE_INT | CTLFLAG_RW, sc, HN_TRUST_HCSUM_IP,
2117 hn_trust_hcsum_sysctl, "I",
2118 "Trust ip packet verification on host side, "
2119 "when csum info is missing");
2120 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "csum_ip",
2121 CTLTYPE_ULONG | CTLFLAG_RW, sc,
2122 __offsetof(struct hn_rx_ring, hn_csum_ip),
2123 hn_rx_stat_ulong_sysctl, "LU", "RXCSUM IP");
2124 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "csum_tcp",
2125 CTLTYPE_ULONG | CTLFLAG_RW, sc,
2126 __offsetof(struct hn_rx_ring, hn_csum_tcp),
2127 hn_rx_stat_ulong_sysctl, "LU", "RXCSUM TCP");
2128 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "csum_udp",
2129 CTLTYPE_ULONG | CTLFLAG_RW, sc,
2130 __offsetof(struct hn_rx_ring, hn_csum_udp),
2131 hn_rx_stat_ulong_sysctl, "LU", "RXCSUM UDP");
2132 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "csum_trusted",
2133 CTLTYPE_ULONG | CTLFLAG_RW, sc,
2134 __offsetof(struct hn_rx_ring, hn_csum_trusted),
2135 hn_rx_stat_ulong_sysctl, "LU",
2136 "# of packets that we trust host's csum verification");
2137 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "small_pkts",
2138 CTLTYPE_ULONG | CTLFLAG_RW, sc,
2139 __offsetof(struct hn_rx_ring, hn_small_pkts),
2140 hn_rx_stat_ulong_sysctl, "LU", "# of small packets received");
2144 hn_destroy_rx_data(struct hn_softc *sc)
2146 #if defined(INET) || defined(INET6)
2150 if (sc->hn_rx_ring_cnt == 0)
2153 #if defined(INET) || defined(INET6)
2154 for (i = 0; i < sc->hn_rx_ring_cnt; ++i)
2155 tcp_lro_free(&sc->hn_rx_ring[i].hn_lro);
2157 free(sc->hn_rx_ring, M_NETVSC);
2158 sc->hn_rx_ring = NULL;
2160 sc->hn_rx_ring_cnt = 0;
2164 hn_create_tx_ring(struct hn_softc *sc, int id)
2166 struct hn_tx_ring *txr = &sc->hn_tx_ring[id];
2167 bus_dma_tag_t parent_dtag;
2172 #ifndef HN_USE_TXDESC_BUFRING
2173 mtx_init(&txr->hn_txlist_spin, "hn txlist", NULL, MTX_SPIN);
2175 mtx_init(&txr->hn_tx_lock, "hn tx", NULL, MTX_DEF);
2177 txr->hn_txdesc_cnt = HN_TX_DESC_CNT;
2178 txr->hn_txdesc = malloc(sizeof(struct hn_txdesc) * txr->hn_txdesc_cnt,
2179 M_NETVSC, M_WAITOK | M_ZERO);
2180 #ifndef HN_USE_TXDESC_BUFRING
2181 SLIST_INIT(&txr->hn_txlist);
2183 txr->hn_txdesc_br = buf_ring_alloc(txr->hn_txdesc_cnt, M_NETVSC,
2184 M_WAITOK, &txr->hn_tx_lock);
2187 txr->hn_tx_taskq = sc->hn_tx_taskq;
2188 TASK_INIT(&txr->hn_tx_task, 0, hn_start_taskfunc, txr);
2189 TASK_INIT(&txr->hn_txeof_task, 0, hn_start_txeof_taskfunc, txr);
2191 txr->hn_direct_tx_size = hn_direct_tx_size;
2192 if (hv_vmbus_protocal_version >= HV_VMBUS_VERSION_WIN8_1)
2193 txr->hn_csum_assist = HN_CSUM_ASSIST;
2195 txr->hn_csum_assist = HN_CSUM_ASSIST_WIN8;
2198 * Always schedule transmission instead of trying to do direct
2199 * transmission. This one gives the best performance so far.
2201 txr->hn_sched_tx = 1;
2203 txr->hn_txeof = hn_start_txeof; /* TODO: if_transmit */
2205 parent_dtag = bus_get_dma_tag(sc->hn_dev);
2207 /* DMA tag for RNDIS messages. */
2208 error = bus_dma_tag_create(parent_dtag, /* parent */
2209 HN_RNDIS_MSG_ALIGN, /* alignment */
2210 HN_RNDIS_MSG_BOUNDARY, /* boundary */
2211 BUS_SPACE_MAXADDR, /* lowaddr */
2212 BUS_SPACE_MAXADDR, /* highaddr */
2213 NULL, NULL, /* filter, filterarg */
2214 HN_RNDIS_MSG_LEN, /* maxsize */
2216 HN_RNDIS_MSG_LEN, /* maxsegsize */
2218 NULL, /* lockfunc */
2219 NULL, /* lockfuncarg */
2220 &txr->hn_tx_rndis_dtag);
2222 device_printf(sc->hn_dev, "failed to create rndis dmatag\n");
2226 /* DMA tag for data. */
2227 error = bus_dma_tag_create(parent_dtag, /* parent */
2229 HN_TX_DATA_BOUNDARY, /* boundary */
2230 BUS_SPACE_MAXADDR, /* lowaddr */
2231 BUS_SPACE_MAXADDR, /* highaddr */
2232 NULL, NULL, /* filter, filterarg */
2233 HN_TX_DATA_MAXSIZE, /* maxsize */
2234 HN_TX_DATA_SEGCNT_MAX, /* nsegments */
2235 HN_TX_DATA_SEGSIZE, /* maxsegsize */
2237 NULL, /* lockfunc */
2238 NULL, /* lockfuncarg */
2239 &txr->hn_tx_data_dtag);
2241 device_printf(sc->hn_dev, "failed to create data dmatag\n");
2245 for (i = 0; i < txr->hn_txdesc_cnt; ++i) {
2246 struct hn_txdesc *txd = &txr->hn_txdesc[i];
2251 * Allocate and load RNDIS messages.
2253 error = bus_dmamem_alloc(txr->hn_tx_rndis_dtag,
2254 (void **)&txd->rndis_msg,
2255 BUS_DMA_WAITOK | BUS_DMA_COHERENT,
2256 &txd->rndis_msg_dmap);
2258 device_printf(sc->hn_dev,
2259 "failed to allocate rndis_msg, %d\n", i);
2263 error = bus_dmamap_load(txr->hn_tx_rndis_dtag,
2264 txd->rndis_msg_dmap,
2265 txd->rndis_msg, HN_RNDIS_MSG_LEN,
2266 hn_dma_map_paddr, &txd->rndis_msg_paddr,
2269 device_printf(sc->hn_dev,
2270 "failed to load rndis_msg, %d\n", i);
2271 bus_dmamem_free(txr->hn_tx_rndis_dtag,
2272 txd->rndis_msg, txd->rndis_msg_dmap);
2276 /* DMA map for TX data. */
2277 error = bus_dmamap_create(txr->hn_tx_data_dtag, 0,
2280 device_printf(sc->hn_dev,
2281 "failed to allocate tx data dmamap\n");
2282 bus_dmamap_unload(txr->hn_tx_rndis_dtag,
2283 txd->rndis_msg_dmap);
2284 bus_dmamem_free(txr->hn_tx_rndis_dtag,
2285 txd->rndis_msg, txd->rndis_msg_dmap);
2289 /* All set, put it to list */
2290 txd->flags |= HN_TXD_FLAG_ONLIST;
2291 #ifndef HN_USE_TXDESC_BUFRING
2292 SLIST_INSERT_HEAD(&txr->hn_txlist, txd, link);
2294 buf_ring_enqueue(txr->hn_txdesc_br, txd);
2297 txr->hn_txdesc_avail = txr->hn_txdesc_cnt;
2299 if (sc->hn_tx_sysctl_tree != NULL) {
2300 struct sysctl_oid_list *child;
2301 struct sysctl_ctx_list *ctx;
2305 * Create per TX ring sysctl tree:
2306 * dev.hn.UNIT.tx.RINGID
2308 ctx = device_get_sysctl_ctx(sc->hn_dev);
2309 child = SYSCTL_CHILDREN(sc->hn_tx_sysctl_tree);
2311 snprintf(name, sizeof(name), "%d", id);
2312 txr->hn_tx_sysctl_tree = SYSCTL_ADD_NODE(ctx, child, OID_AUTO,
2313 name, CTLFLAG_RD, 0, "");
2315 if (txr->hn_tx_sysctl_tree != NULL) {
2316 child = SYSCTL_CHILDREN(txr->hn_tx_sysctl_tree);
2318 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "txdesc_avail",
2319 CTLFLAG_RD, &txr->hn_txdesc_avail, 0,
2320 "# of available TX descs");
2328 hn_txdesc_dmamap_destroy(struct hn_txdesc *txd)
2330 struct hn_tx_ring *txr = txd->txr;
2332 KASSERT(txd->m == NULL, ("still has mbuf installed"));
2333 KASSERT((txd->flags & HN_TXD_FLAG_DMAMAP) == 0, ("still dma mapped"));
2335 bus_dmamap_unload(txr->hn_tx_rndis_dtag, txd->rndis_msg_dmap);
2336 bus_dmamem_free(txr->hn_tx_rndis_dtag, txd->rndis_msg,
2337 txd->rndis_msg_dmap);
2338 bus_dmamap_destroy(txr->hn_tx_data_dtag, txd->data_dmap);
2342 hn_destroy_tx_ring(struct hn_tx_ring *txr)
2344 struct hn_txdesc *txd;
2346 if (txr->hn_txdesc == NULL)
2349 #ifndef HN_USE_TXDESC_BUFRING
2350 while ((txd = SLIST_FIRST(&txr->hn_txlist)) != NULL) {
2351 SLIST_REMOVE_HEAD(&txr->hn_txlist, link);
2352 hn_txdesc_dmamap_destroy(txd);
2355 while ((txd = buf_ring_dequeue_sc(txr->hn_txdesc_br)) != NULL)
2356 hn_txdesc_dmamap_destroy(txd);
2359 if (txr->hn_tx_data_dtag != NULL)
2360 bus_dma_tag_destroy(txr->hn_tx_data_dtag);
2361 if (txr->hn_tx_rndis_dtag != NULL)
2362 bus_dma_tag_destroy(txr->hn_tx_rndis_dtag);
2364 #ifdef HN_USE_TXDESC_BUFRING
2365 buf_ring_free(txr->hn_txdesc_br, M_NETVSC);
2368 free(txr->hn_txdesc, M_NETVSC);
2369 txr->hn_txdesc = NULL;
2371 #ifndef HN_USE_TXDESC_BUFRING
2372 mtx_destroy(&txr->hn_txlist_spin);
2374 mtx_destroy(&txr->hn_tx_lock);
2378 hn_create_tx_data(struct hn_softc *sc)
2380 struct sysctl_oid_list *child;
2381 struct sysctl_ctx_list *ctx;
2384 sc->hn_tx_ring_cnt = 1; /* TODO: vRSS */
2385 sc->hn_tx_ring = malloc(sizeof(struct hn_tx_ring) * sc->hn_tx_ring_cnt,
2386 M_NETVSC, M_WAITOK | M_ZERO);
2388 ctx = device_get_sysctl_ctx(sc->hn_dev);
2389 child = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->hn_dev));
2391 /* Create dev.hn.UNIT.tx sysctl tree */
2392 sc->hn_tx_sysctl_tree = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, "tx",
2395 for (i = 0; i < sc->hn_tx_ring_cnt; ++i) {
2398 error = hn_create_tx_ring(sc, i);
2403 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "no_txdescs",
2404 CTLTYPE_ULONG | CTLFLAG_RW, sc,
2405 __offsetof(struct hn_tx_ring, hn_no_txdescs),
2406 hn_tx_stat_ulong_sysctl, "LU", "# of times short of TX descs");
2407 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "send_failed",
2408 CTLTYPE_ULONG | CTLFLAG_RW, sc,
2409 __offsetof(struct hn_tx_ring, hn_send_failed),
2410 hn_tx_stat_ulong_sysctl, "LU", "# of hyper-v sending failure");
2411 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "txdma_failed",
2412 CTLTYPE_ULONG | CTLFLAG_RW, sc,
2413 __offsetof(struct hn_tx_ring, hn_txdma_failed),
2414 hn_tx_stat_ulong_sysctl, "LU", "# of TX DMA failure");
2415 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "tx_collapsed",
2416 CTLTYPE_ULONG | CTLFLAG_RW, sc,
2417 __offsetof(struct hn_tx_ring, hn_tx_collapsed),
2418 hn_tx_stat_ulong_sysctl, "LU", "# of TX mbuf collapsed");
2419 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "tx_chimney",
2420 CTLTYPE_ULONG | CTLFLAG_RW, sc,
2421 __offsetof(struct hn_tx_ring, hn_tx_chimney),
2422 hn_tx_stat_ulong_sysctl, "LU", "# of chimney send");
2423 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "txdesc_cnt",
2424 CTLFLAG_RD, &sc->hn_tx_ring[0].hn_txdesc_cnt, 0,
2425 "# of total TX descs");
2426 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "tx_chimney_max",
2427 CTLFLAG_RD, &sc->hn_tx_chimney_max, 0,
2428 "Chimney send packet size upper boundary");
2429 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "tx_chimney_size",
2430 CTLTYPE_INT | CTLFLAG_RW, sc, 0, hn_tx_chimney_size_sysctl,
2431 "I", "Chimney send packet size limit");
2432 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "direct_tx_size",
2433 CTLTYPE_INT | CTLFLAG_RW, sc,
2434 __offsetof(struct hn_tx_ring, hn_direct_tx_size),
2435 hn_tx_conf_int_sysctl, "I",
2436 "Size of the packet for direct transmission");
2437 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "sched_tx",
2438 CTLTYPE_INT | CTLFLAG_RW, sc,
2439 __offsetof(struct hn_tx_ring, hn_sched_tx),
2440 hn_tx_conf_int_sysctl, "I",
2441 "Always schedule transmission "
2442 "instead of doing direct transmission");
2448 hn_set_tx_chimney_size(struct hn_softc *sc, int chimney_size)
2453 for (i = 0; i < sc->hn_tx_ring_cnt; ++i)
2454 sc->hn_tx_ring[i].hn_tx_chimney_size = chimney_size;
2459 hn_destroy_tx_data(struct hn_softc *sc)
2463 if (sc->hn_tx_ring_cnt == 0)
2466 for (i = 0; i < sc->hn_tx_ring_cnt; ++i)
2467 hn_destroy_tx_ring(&sc->hn_tx_ring[i]);
2469 free(sc->hn_tx_ring, M_NETVSC);
2470 sc->hn_tx_ring = NULL;
2472 sc->hn_tx_ring_cnt = 0;
2476 hn_start_taskfunc(void *xtxr, int pending __unused)
2478 struct hn_tx_ring *txr = xtxr;
2480 mtx_lock(&txr->hn_tx_lock);
2481 hn_start_locked(txr, 0);
2482 mtx_unlock(&txr->hn_tx_lock);
2486 hn_start_txeof_taskfunc(void *xtxr, int pending __unused)
2488 struct hn_tx_ring *txr = xtxr;
2490 mtx_lock(&txr->hn_tx_lock);
2491 atomic_clear_int(&txr->hn_sc->hn_ifp->if_drv_flags, IFF_DRV_OACTIVE);
2492 hn_start_locked(txr, 0);
2493 mtx_unlock(&txr->hn_tx_lock);
2497 hn_stop_tx_tasks(struct hn_softc *sc)
2501 for (i = 0; i < sc->hn_tx_ring_cnt; ++i) {
2502 struct hn_tx_ring *txr = &sc->hn_tx_ring[i];
2504 taskqueue_drain(txr->hn_tx_taskq, &txr->hn_tx_task);
2505 taskqueue_drain(txr->hn_tx_taskq, &txr->hn_txeof_task);
2510 hn_tx_taskq_create(void *arg __unused)
2512 if (!hn_share_tx_taskq)
2515 hn_tx_taskq = taskqueue_create("hn_tx", M_WAITOK,
2516 taskqueue_thread_enqueue, &hn_tx_taskq);
2517 taskqueue_start_threads(&hn_tx_taskq, 1, PI_NET, "hn tx");
2518 if (hn_bind_tx_taskq >= 0) {
2519 int cpu = hn_bind_tx_taskq;
2520 struct task cpuset_task;
2523 if (cpu > mp_ncpus - 1)
2525 CPU_SETOF(cpu, &cpu_set);
2526 TASK_INIT(&cpuset_task, 0, hn_cpuset_setthread_task, &cpu_set);
2527 taskqueue_enqueue(hn_tx_taskq, &cpuset_task);
2528 taskqueue_drain(hn_tx_taskq, &cpuset_task);
2531 SYSINIT(hn_txtq_create, SI_SUB_DRIVERS, SI_ORDER_FIRST,
2532 hn_tx_taskq_create, NULL);
2535 hn_tx_taskq_destroy(void *arg __unused)
2537 if (hn_tx_taskq != NULL)
2538 taskqueue_free(hn_tx_taskq);
2540 SYSUNINIT(hn_txtq_destroy, SI_SUB_DRIVERS, SI_ORDER_FIRST,
2541 hn_tx_taskq_destroy, NULL);
2543 static device_method_t netvsc_methods[] = {
2544 /* Device interface */
2545 DEVMETHOD(device_probe, netvsc_probe),
2546 DEVMETHOD(device_attach, netvsc_attach),
2547 DEVMETHOD(device_detach, netvsc_detach),
2548 DEVMETHOD(device_shutdown, netvsc_shutdown),
2553 static driver_t netvsc_driver = {
2559 static devclass_t netvsc_devclass;
2561 DRIVER_MODULE(hn, vmbus, netvsc_driver, netvsc_devclass, 0, 0);
2562 MODULE_VERSION(hn, 1);
2563 MODULE_DEPEND(hn, vmbus, 1, 1, 1);