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");
277 static int hn_use_if_start = 1;
278 SYSCTL_INT(_hw_hn, OID_AUTO, use_if_start, CTLFLAG_RDTUN,
279 &hn_use_if_start, 0, "Use if_start TX method");
282 * Forward declarations
284 static void hn_stop(hn_softc_t *sc);
285 static void hn_ifinit_locked(hn_softc_t *sc);
286 static void hn_ifinit(void *xsc);
287 static int hn_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
288 static int hn_start_locked(struct hn_tx_ring *txr, int len);
289 static void hn_start(struct ifnet *ifp);
290 static void hn_start_txeof(struct hn_tx_ring *);
291 static int hn_ifmedia_upd(struct ifnet *ifp);
292 static void hn_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr);
293 #if __FreeBSD_version >= 1100099
294 static int hn_lro_lenlim_sysctl(SYSCTL_HANDLER_ARGS);
295 static int hn_lro_ackcnt_sysctl(SYSCTL_HANDLER_ARGS);
297 static int hn_trust_hcsum_sysctl(SYSCTL_HANDLER_ARGS);
298 static int hn_tx_chimney_size_sysctl(SYSCTL_HANDLER_ARGS);
299 #if __FreeBSD_version < 1100095
300 static int hn_rx_stat_int_sysctl(SYSCTL_HANDLER_ARGS);
302 static int hn_rx_stat_u64_sysctl(SYSCTL_HANDLER_ARGS);
304 static int hn_rx_stat_ulong_sysctl(SYSCTL_HANDLER_ARGS);
305 static int hn_tx_stat_ulong_sysctl(SYSCTL_HANDLER_ARGS);
306 static int hn_tx_conf_int_sysctl(SYSCTL_HANDLER_ARGS);
307 static int hn_check_iplen(const struct mbuf *, int);
308 static int hn_create_tx_ring(struct hn_softc *, int);
309 static void hn_destroy_tx_ring(struct hn_tx_ring *);
310 static int hn_create_tx_data(struct hn_softc *);
311 static void hn_destroy_tx_data(struct hn_softc *);
312 static void hn_start_taskfunc(void *, int);
313 static void hn_start_txeof_taskfunc(void *, int);
314 static void hn_stop_tx_tasks(struct hn_softc *);
315 static int hn_encap(struct hn_tx_ring *, struct hn_txdesc *, struct mbuf **);
316 static void hn_create_rx_data(struct hn_softc *sc);
317 static void hn_destroy_rx_data(struct hn_softc *sc);
318 static void hn_set_tx_chimney_size(struct hn_softc *, int);
320 static int hn_transmit(struct ifnet *, struct mbuf *);
321 static void hn_xmit_qflush(struct ifnet *);
322 static int hn_xmit(struct hn_tx_ring *, int);
323 static void hn_xmit_txeof(struct hn_tx_ring *);
324 static void hn_xmit_taskfunc(void *, int);
325 static void hn_xmit_txeof_taskfunc(void *, int);
328 hn_ifmedia_upd(struct ifnet *ifp __unused)
335 hn_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
337 struct hn_softc *sc = ifp->if_softc;
339 ifmr->ifm_status = IFM_AVALID;
340 ifmr->ifm_active = IFM_ETHER;
342 if (!sc->hn_carrier) {
343 ifmr->ifm_active |= IFM_NONE;
346 ifmr->ifm_status |= IFM_ACTIVE;
347 ifmr->ifm_active |= IFM_10G_T | IFM_FDX;
350 /* {F8615163-DF3E-46c5-913F-F2D2F965ED0E} */
351 static const hv_guid g_net_vsc_device_type = {
352 .data = {0x63, 0x51, 0x61, 0xF8, 0x3E, 0xDF, 0xc5, 0x46,
353 0x91, 0x3F, 0xF2, 0xD2, 0xF9, 0x65, 0xED, 0x0E}
357 * Standard probe entry point.
361 netvsc_probe(device_t dev)
365 p = vmbus_get_type(dev);
366 if (!memcmp(p, &g_net_vsc_device_type.data, sizeof(hv_guid))) {
367 device_set_desc(dev, "Synthetic Network Interface");
369 printf("Netvsc probe... DONE \n");
371 return (BUS_PROBE_DEFAULT);
378 hn_cpuset_setthread_task(void *xmask, int pending __unused)
380 cpuset_t *mask = xmask;
383 error = cpuset_setthread(curthread->td_tid, mask);
385 panic("curthread=%ju: can't pin; error=%d",
386 (uintmax_t)curthread->td_tid, error);
391 * Standard attach entry point.
393 * Called when the driver is loaded. It allocates needed resources,
394 * and initializes the "hardware" and software.
397 netvsc_attach(device_t dev)
399 struct hv_device *device_ctx = vmbus_get_devctx(dev);
400 netvsc_device_info device_info;
402 int unit = device_get_unit(dev);
403 struct ifnet *ifp = NULL;
405 #if __FreeBSD_version >= 1100045
409 sc = device_get_softc(dev);
414 bzero(sc, sizeof(hn_softc_t));
418 if (hn_tx_taskq == NULL) {
419 sc->hn_tx_taskq = taskqueue_create("hn_tx", M_WAITOK,
420 taskqueue_thread_enqueue, &sc->hn_tx_taskq);
421 taskqueue_start_threads(&sc->hn_tx_taskq, 1, PI_NET, "%s tx",
422 device_get_nameunit(dev));
423 if (hn_bind_tx_taskq >= 0) {
424 int cpu = hn_bind_tx_taskq;
425 struct task cpuset_task;
428 if (cpu > mp_ncpus - 1)
430 CPU_SETOF(cpu, &cpu_set);
431 TASK_INIT(&cpuset_task, 0, hn_cpuset_setthread_task,
433 taskqueue_enqueue(sc->hn_tx_taskq, &cpuset_task);
434 taskqueue_drain(sc->hn_tx_taskq, &cpuset_task);
437 sc->hn_tx_taskq = hn_tx_taskq;
439 NV_LOCK_INIT(sc, "NetVSCLock");
441 sc->hn_dev_obj = device_ctx;
443 ifp = sc->hn_ifp = sc->arpcom.ac_ifp = if_alloc(IFT_ETHER);
446 error = hn_create_tx_data(sc);
450 hn_create_rx_data(sc);
452 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
453 ifp->if_dunit = unit;
454 ifp->if_dname = NETVSC_DEVNAME;
456 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
457 ifp->if_ioctl = hn_ioctl;
458 ifp->if_init = hn_ifinit;
459 /* needed by hv_rf_on_device_add() code */
460 ifp->if_mtu = ETHERMTU;
461 if (hn_use_if_start) {
462 ifp->if_start = hn_start;
463 IFQ_SET_MAXLEN(&ifp->if_snd, 512);
464 ifp->if_snd.ifq_drv_maxlen = 511;
465 IFQ_SET_READY(&ifp->if_snd);
467 ifp->if_transmit = hn_transmit;
468 ifp->if_qflush = hn_xmit_qflush;
471 ifmedia_init(&sc->hn_media, 0, hn_ifmedia_upd, hn_ifmedia_sts);
472 ifmedia_add(&sc->hn_media, IFM_ETHER | IFM_AUTO, 0, NULL);
473 ifmedia_set(&sc->hn_media, IFM_ETHER | IFM_AUTO);
474 /* XXX ifmedia_set really should do this for us */
475 sc->hn_media.ifm_media = sc->hn_media.ifm_cur->ifm_media;
478 * Tell upper layers that we support full VLAN capability.
480 ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
481 ifp->if_capabilities |=
482 IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU | IFCAP_HWCSUM | IFCAP_TSO |
485 IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU | IFCAP_HWCSUM | IFCAP_TSO |
487 ifp->if_hwassist = sc->hn_tx_ring[0].hn_csum_assist | CSUM_TSO;
489 error = hv_rf_on_device_add(device_ctx, &device_info);
493 if (device_info.link_state == 0) {
497 #if __FreeBSD_version >= 1100045
498 tso_maxlen = hn_tso_maxlen;
499 if (tso_maxlen <= 0 || tso_maxlen > IP_MAXPACKET)
500 tso_maxlen = IP_MAXPACKET;
502 ifp->if_hw_tsomaxsegcount = HN_TX_DATA_SEGCNT_MAX;
503 ifp->if_hw_tsomaxsegsize = PAGE_SIZE;
504 ifp->if_hw_tsomax = tso_maxlen -
505 (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN);
508 ether_ifattach(ifp, device_info.mac_addr);
510 #if __FreeBSD_version >= 1100045
511 if_printf(ifp, "TSO: %u/%u/%u\n", ifp->if_hw_tsomax,
512 ifp->if_hw_tsomaxsegcount, ifp->if_hw_tsomaxsegsize);
515 sc->hn_tx_chimney_max = sc->net_dev->send_section_size;
516 hn_set_tx_chimney_size(sc, sc->hn_tx_chimney_max);
517 if (hn_tx_chimney_size > 0 &&
518 hn_tx_chimney_size < sc->hn_tx_chimney_max)
519 hn_set_tx_chimney_size(sc, hn_tx_chimney_size);
523 hn_destroy_tx_data(sc);
530 * Standard detach entry point
533 netvsc_detach(device_t dev)
535 struct hn_softc *sc = device_get_softc(dev);
536 struct hv_device *hv_device = vmbus_get_devctx(dev);
539 printf("netvsc_detach\n");
542 * XXXKYS: Need to clean up all our
543 * driver state; this is the driver
548 * XXXKYS: Need to stop outgoing traffic and unregister
552 hv_rf_on_device_remove(hv_device, HV_RF_NV_DESTROY_CHANNEL);
554 hn_stop_tx_tasks(sc);
556 ifmedia_removeall(&sc->hn_media);
557 hn_destroy_rx_data(sc);
558 hn_destroy_tx_data(sc);
560 if (sc->hn_tx_taskq != hn_tx_taskq)
561 taskqueue_free(sc->hn_tx_taskq);
567 * Standard shutdown entry point
570 netvsc_shutdown(device_t dev)
576 hn_txdesc_dmamap_load(struct hn_tx_ring *txr, struct hn_txdesc *txd,
577 struct mbuf **m_head, bus_dma_segment_t *segs, int *nsegs)
579 struct mbuf *m = *m_head;
582 error = bus_dmamap_load_mbuf_sg(txr->hn_tx_data_dtag, txd->data_dmap,
583 m, segs, nsegs, BUS_DMA_NOWAIT);
584 if (error == EFBIG) {
587 m_new = m_collapse(m, M_NOWAIT, HN_TX_DATA_SEGCNT_MAX);
592 txr->hn_tx_collapsed++;
594 error = bus_dmamap_load_mbuf_sg(txr->hn_tx_data_dtag,
595 txd->data_dmap, m, segs, nsegs, BUS_DMA_NOWAIT);
598 bus_dmamap_sync(txr->hn_tx_data_dtag, txd->data_dmap,
599 BUS_DMASYNC_PREWRITE);
600 txd->flags |= HN_TXD_FLAG_DMAMAP;
606 hn_txdesc_dmamap_unload(struct hn_tx_ring *txr, struct hn_txdesc *txd)
609 if (txd->flags & HN_TXD_FLAG_DMAMAP) {
610 bus_dmamap_sync(txr->hn_tx_data_dtag,
611 txd->data_dmap, BUS_DMASYNC_POSTWRITE);
612 bus_dmamap_unload(txr->hn_tx_data_dtag,
614 txd->flags &= ~HN_TXD_FLAG_DMAMAP;
619 hn_txdesc_put(struct hn_tx_ring *txr, struct hn_txdesc *txd)
622 KASSERT((txd->flags & HN_TXD_FLAG_ONLIST) == 0,
623 ("put an onlist txd %#x", txd->flags));
625 KASSERT(txd->refs > 0, ("invalid txd refs %d", txd->refs));
626 if (atomic_fetchadd_int(&txd->refs, -1) != 1)
629 hn_txdesc_dmamap_unload(txr, txd);
630 if (txd->m != NULL) {
635 txd->flags |= HN_TXD_FLAG_ONLIST;
637 #ifndef HN_USE_TXDESC_BUFRING
638 mtx_lock_spin(&txr->hn_txlist_spin);
639 KASSERT(txr->hn_txdesc_avail >= 0 &&
640 txr->hn_txdesc_avail < txr->hn_txdesc_cnt,
641 ("txdesc_put: invalid txd avail %d", txr->hn_txdesc_avail));
642 txr->hn_txdesc_avail++;
643 SLIST_INSERT_HEAD(&txr->hn_txlist, txd, link);
644 mtx_unlock_spin(&txr->hn_txlist_spin);
646 atomic_add_int(&txr->hn_txdesc_avail, 1);
647 buf_ring_enqueue(txr->hn_txdesc_br, txd);
653 static __inline struct hn_txdesc *
654 hn_txdesc_get(struct hn_tx_ring *txr)
656 struct hn_txdesc *txd;
658 #ifndef HN_USE_TXDESC_BUFRING
659 mtx_lock_spin(&txr->hn_txlist_spin);
660 txd = SLIST_FIRST(&txr->hn_txlist);
662 KASSERT(txr->hn_txdesc_avail > 0,
663 ("txdesc_get: invalid txd avail %d", txr->hn_txdesc_avail));
664 txr->hn_txdesc_avail--;
665 SLIST_REMOVE_HEAD(&txr->hn_txlist, link);
667 mtx_unlock_spin(&txr->hn_txlist_spin);
669 txd = buf_ring_dequeue_sc(txr->hn_txdesc_br);
673 #ifdef HN_USE_TXDESC_BUFRING
674 atomic_subtract_int(&txr->hn_txdesc_avail, 1);
676 KASSERT(txd->m == NULL && txd->refs == 0 &&
677 (txd->flags & HN_TXD_FLAG_ONLIST), ("invalid txd"));
678 txd->flags &= ~HN_TXD_FLAG_ONLIST;
685 hn_txdesc_hold(struct hn_txdesc *txd)
688 /* 0->1 transition will never work */
689 KASSERT(txd->refs > 0, ("invalid refs %d", txd->refs));
690 atomic_add_int(&txd->refs, 1);
694 hn_tx_done(void *xpkt)
696 netvsc_packet *packet = xpkt;
697 struct hn_txdesc *txd;
698 struct hn_tx_ring *txr;
700 txd = (struct hn_txdesc *)(uintptr_t)
701 packet->compl.send.send_completion_tid;
704 txr->hn_has_txeof = 1;
705 hn_txdesc_put(txr, txd);
709 netvsc_channel_rollup(struct hv_device *device_ctx)
711 struct hn_softc *sc = device_get_softc(device_ctx->device);
712 struct hn_tx_ring *txr = &sc->hn_tx_ring[0]; /* TODO: vRSS */
713 #if defined(INET) || defined(INET6)
714 struct hn_rx_ring *rxr = &sc->hn_rx_ring[0]; /* TODO: vRSS */
715 struct lro_ctrl *lro = &rxr->hn_lro;
716 struct lro_entry *queued;
718 while ((queued = SLIST_FIRST(&lro->lro_active)) != NULL) {
719 SLIST_REMOVE_HEAD(&lro->lro_active, next);
720 tcp_lro_flush(lro, queued);
724 if (!txr->hn_has_txeof)
727 txr->hn_has_txeof = 0;
733 * If this function fails, then both txd and m_head0 will be freed.
736 hn_encap(struct hn_tx_ring *txr, struct hn_txdesc *txd, struct mbuf **m_head0)
738 bus_dma_segment_t segs[HN_TX_DATA_SEGCNT_MAX];
740 struct mbuf *m_head = *m_head0;
741 netvsc_packet *packet;
742 rndis_msg *rndis_mesg;
743 rndis_packet *rndis_pkt;
744 rndis_per_packet_info *rppi;
745 uint32_t rndis_msg_size;
747 packet = &txd->netvsc_pkt;
748 packet->is_data_pkt = TRUE;
749 packet->tot_data_buf_len = m_head->m_pkthdr.len;
752 * extension points to the area reserved for the
753 * rndis_filter_packet, which is placed just after
754 * the netvsc_packet (and rppi struct, if present;
755 * length is updated later).
757 rndis_mesg = txd->rndis_msg;
758 /* XXX not necessary */
759 memset(rndis_mesg, 0, HN_RNDIS_MSG_LEN);
760 rndis_mesg->ndis_msg_type = REMOTE_NDIS_PACKET_MSG;
762 rndis_pkt = &rndis_mesg->msg.packet;
763 rndis_pkt->data_offset = sizeof(rndis_packet);
764 rndis_pkt->data_length = packet->tot_data_buf_len;
765 rndis_pkt->per_pkt_info_offset = sizeof(rndis_packet);
767 rndis_msg_size = RNDIS_MESSAGE_SIZE(rndis_packet);
769 if (m_head->m_flags & M_VLANTAG) {
770 ndis_8021q_info *rppi_vlan_info;
772 rndis_msg_size += RNDIS_VLAN_PPI_SIZE;
773 rppi = hv_set_rppi_data(rndis_mesg, RNDIS_VLAN_PPI_SIZE,
776 rppi_vlan_info = (ndis_8021q_info *)((uint8_t *)rppi +
777 rppi->per_packet_info_offset);
778 rppi_vlan_info->u1.s1.vlan_id =
779 m_head->m_pkthdr.ether_vtag & 0xfff;
782 if (m_head->m_pkthdr.csum_flags & CSUM_TSO) {
783 rndis_tcp_tso_info *tso_info;
784 struct ether_vlan_header *eh;
788 * XXX need m_pullup and use mtodo
790 eh = mtod(m_head, struct ether_vlan_header*);
791 if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN))
792 ether_len = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
794 ether_len = ETHER_HDR_LEN;
796 rndis_msg_size += RNDIS_TSO_PPI_SIZE;
797 rppi = hv_set_rppi_data(rndis_mesg, RNDIS_TSO_PPI_SIZE,
798 tcp_large_send_info);
800 tso_info = (rndis_tcp_tso_info *)((uint8_t *)rppi +
801 rppi->per_packet_info_offset);
802 tso_info->lso_v2_xmit.type =
803 RNDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE;
806 if (m_head->m_pkthdr.csum_flags & CSUM_IP_TSO) {
808 (struct ip *)(m_head->m_data + ether_len);
809 unsigned long iph_len = ip->ip_hl << 2;
811 (struct tcphdr *)((caddr_t)ip + iph_len);
813 tso_info->lso_v2_xmit.ip_version =
814 RNDIS_TCP_LARGE_SEND_OFFLOAD_IPV4;
818 th->th_sum = in_pseudo(ip->ip_src.s_addr,
819 ip->ip_dst.s_addr, htons(IPPROTO_TCP));
822 #if defined(INET6) && defined(INET)
827 struct ip6_hdr *ip6 = (struct ip6_hdr *)
828 (m_head->m_data + ether_len);
829 struct tcphdr *th = (struct tcphdr *)(ip6 + 1);
831 tso_info->lso_v2_xmit.ip_version =
832 RNDIS_TCP_LARGE_SEND_OFFLOAD_IPV6;
834 th->th_sum = in6_cksum_pseudo(ip6, 0, IPPROTO_TCP, 0);
837 tso_info->lso_v2_xmit.tcp_header_offset = 0;
838 tso_info->lso_v2_xmit.mss = m_head->m_pkthdr.tso_segsz;
839 } else if (m_head->m_pkthdr.csum_flags & txr->hn_csum_assist) {
840 rndis_tcp_ip_csum_info *csum_info;
842 rndis_msg_size += RNDIS_CSUM_PPI_SIZE;
843 rppi = hv_set_rppi_data(rndis_mesg, RNDIS_CSUM_PPI_SIZE,
845 csum_info = (rndis_tcp_ip_csum_info *)((uint8_t *)rppi +
846 rppi->per_packet_info_offset);
848 csum_info->xmit.is_ipv4 = 1;
849 if (m_head->m_pkthdr.csum_flags & CSUM_IP)
850 csum_info->xmit.ip_header_csum = 1;
852 if (m_head->m_pkthdr.csum_flags & CSUM_TCP) {
853 csum_info->xmit.tcp_csum = 1;
854 csum_info->xmit.tcp_header_offset = 0;
855 } else if (m_head->m_pkthdr.csum_flags & CSUM_UDP) {
856 csum_info->xmit.udp_csum = 1;
860 rndis_mesg->msg_len = packet->tot_data_buf_len + rndis_msg_size;
861 packet->tot_data_buf_len = rndis_mesg->msg_len;
864 * Chimney send, if the packet could fit into one chimney buffer.
866 if (packet->tot_data_buf_len < txr->hn_tx_chimney_size) {
867 netvsc_dev *net_dev = txr->hn_sc->net_dev;
868 uint32_t send_buf_section_idx;
870 send_buf_section_idx =
871 hv_nv_get_next_send_section(net_dev);
872 if (send_buf_section_idx !=
873 NVSP_1_CHIMNEY_SEND_INVALID_SECTION_INDEX) {
874 uint8_t *dest = ((uint8_t *)net_dev->send_buf +
875 (send_buf_section_idx *
876 net_dev->send_section_size));
878 memcpy(dest, rndis_mesg, rndis_msg_size);
879 dest += rndis_msg_size;
880 m_copydata(m_head, 0, m_head->m_pkthdr.len, dest);
882 packet->send_buf_section_idx = send_buf_section_idx;
883 packet->send_buf_section_size =
884 packet->tot_data_buf_len;
885 packet->page_buf_count = 0;
886 txr->hn_tx_chimney++;
891 error = hn_txdesc_dmamap_load(txr, txd, &m_head, segs, &nsegs);
896 * This mbuf is not linked w/ the txd yet, so free it now.
901 freed = hn_txdesc_put(txr, txd);
903 ("fail to free txd upon txdma error"));
905 txr->hn_txdma_failed++;
906 if_inc_counter(txr->hn_sc->hn_ifp, IFCOUNTER_OERRORS, 1);
911 packet->page_buf_count = nsegs + HV_RF_NUM_TX_RESERVED_PAGE_BUFS;
913 /* send packet with page buffer */
914 packet->page_buffers[0].pfn = atop(txd->rndis_msg_paddr);
915 packet->page_buffers[0].offset = txd->rndis_msg_paddr & PAGE_MASK;
916 packet->page_buffers[0].length = rndis_msg_size;
919 * Fill the page buffers with mbuf info starting at index
920 * HV_RF_NUM_TX_RESERVED_PAGE_BUFS.
922 for (i = 0; i < nsegs; ++i) {
923 hv_vmbus_page_buffer *pb = &packet->page_buffers[
924 i + HV_RF_NUM_TX_RESERVED_PAGE_BUFS];
926 pb->pfn = atop(segs[i].ds_addr);
927 pb->offset = segs[i].ds_addr & PAGE_MASK;
928 pb->length = segs[i].ds_len;
931 packet->send_buf_section_idx =
932 NVSP_1_CHIMNEY_SEND_INVALID_SECTION_INDEX;
933 packet->send_buf_section_size = 0;
937 /* Set the completion routine */
938 packet->compl.send.on_send_completion = hn_tx_done;
939 packet->compl.send.send_completion_context = packet;
940 packet->compl.send.send_completion_tid = (uint64_t)(uintptr_t)txd;
947 * If this function fails, then txd will be freed, but the mbuf
948 * associated w/ the txd will _not_ be freed.
951 hn_send_pkt(struct ifnet *ifp, struct hv_device *device_ctx,
952 struct hn_tx_ring *txr, struct hn_txdesc *txd)
954 int error, send_failed = 0;
958 * Make sure that txd is not freed before ETHER_BPF_MTAP.
961 error = hv_nv_on_send(device_ctx, &txd->netvsc_pkt);
963 ETHER_BPF_MTAP(ifp, txd->m);
964 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
965 if (!hn_use_if_start) {
966 if_inc_counter(ifp, IFCOUNTER_OBYTES,
967 txd->m->m_pkthdr.len);
968 if (txd->m->m_flags & M_MCAST)
969 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
972 hn_txdesc_put(txr, txd);
974 if (__predict_false(error)) {
978 * This should "really rarely" happen.
980 * XXX Too many RX to be acked or too many sideband
981 * commands to run? Ask netvsc_channel_rollup()
982 * to kick start later.
984 txr->hn_has_txeof = 1;
986 txr->hn_send_failed++;
989 * Try sending again after set hn_has_txeof;
990 * in case that we missed the last
991 * netvsc_channel_rollup().
995 if_printf(ifp, "send failed\n");
998 * Caller will perform further processing on the
999 * associated mbuf, so don't free it in hn_txdesc_put();
1000 * only unload it from the DMA map in hn_txdesc_put(),
1004 freed = hn_txdesc_put(txr, txd);
1006 ("fail to free txd upon send error"));
1008 txr->hn_send_failed++;
1014 * Start a transmit of one or more packets
1017 hn_start_locked(struct hn_tx_ring *txr, int len)
1019 struct hn_softc *sc = txr->hn_sc;
1020 struct ifnet *ifp = sc->hn_ifp;
1021 struct hv_device *device_ctx = vmbus_get_devctx(sc->hn_dev);
1023 KASSERT(hn_use_if_start,
1024 ("hn_start_locked is called, when if_start is disabled"));
1025 KASSERT(txr == &sc->hn_tx_ring[0], ("not the first TX ring"));
1026 mtx_assert(&txr->hn_tx_lock, MA_OWNED);
1028 if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
1032 while (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) {
1033 struct hn_txdesc *txd;
1034 struct mbuf *m_head;
1037 IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
1041 if (len > 0 && m_head->m_pkthdr.len > len) {
1043 * This sending could be time consuming; let callers
1044 * dispatch this packet sending (and sending of any
1045 * following up packets) to tx taskqueue.
1047 IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
1051 txd = hn_txdesc_get(txr);
1053 txr->hn_no_txdescs++;
1054 IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
1055 atomic_set_int(&ifp->if_drv_flags, IFF_DRV_OACTIVE);
1059 error = hn_encap(txr, txd, &m_head);
1061 /* Both txd and m_head are freed */
1065 error = hn_send_pkt(ifp, device_ctx, txr, txd);
1066 if (__predict_false(error)) {
1067 /* txd is freed, but m_head is not */
1068 IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
1069 atomic_set_int(&ifp->if_drv_flags, IFF_DRV_OACTIVE);
1077 * Link up/down notification
1080 netvsc_linkstatus_callback(struct hv_device *device_obj, uint32_t status)
1082 hn_softc_t *sc = device_get_softc(device_obj->device);
1096 * Append the specified data to the indicated mbuf chain,
1097 * Extend the mbuf chain if the new data does not fit in
1100 * This is a minor rewrite of m_append() from sys/kern/uipc_mbuf.c.
1101 * There should be an equivalent in the kernel mbuf code,
1102 * but there does not appear to be one yet.
1104 * Differs from m_append() in that additional mbufs are
1105 * allocated with cluster size MJUMPAGESIZE, and filled
1108 * Return 1 if able to complete the job; otherwise 0.
1111 hv_m_append(struct mbuf *m0, int len, c_caddr_t cp)
1114 int remainder, space;
1116 for (m = m0; m->m_next != NULL; m = m->m_next)
1119 space = M_TRAILINGSPACE(m);
1122 * Copy into available space.
1124 if (space > remainder)
1126 bcopy(cp, mtod(m, caddr_t) + m->m_len, space);
1131 while (remainder > 0) {
1133 * Allocate a new mbuf; could check space
1134 * and allocate a cluster instead.
1136 n = m_getjcl(M_DONTWAIT, m->m_type, 0, MJUMPAGESIZE);
1139 n->m_len = min(MJUMPAGESIZE, remainder);
1140 bcopy(cp, mtod(n, caddr_t), n->m_len);
1142 remainder -= n->m_len;
1146 if (m0->m_flags & M_PKTHDR)
1147 m0->m_pkthdr.len += len - remainder;
1149 return (remainder == 0);
1154 * Called when we receive a data packet from the "wire" on the
1157 * Note: This is no longer used as a callback
1160 netvsc_recv(struct hv_device *device_ctx, netvsc_packet *packet,
1161 rndis_tcp_ip_csum_info *csum_info)
1163 struct hn_softc *sc = device_get_softc(device_ctx->device);
1164 struct hn_rx_ring *rxr = &sc->hn_rx_ring[0]; /* TODO: vRSS */
1167 int size, do_lro = 0, do_csum = 1;
1170 return (0); /* TODO: KYS how can this be! */
1175 ifp = sc->arpcom.ac_ifp;
1177 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1182 * Bail out if packet contains more data than configured MTU.
1184 if (packet->tot_data_buf_len > (ifp->if_mtu + ETHER_HDR_LEN)) {
1186 } else if (packet->tot_data_buf_len <= MHLEN) {
1187 m_new = m_gethdr(M_NOWAIT, MT_DATA);
1190 memcpy(mtod(m_new, void *), packet->data,
1191 packet->tot_data_buf_len);
1192 m_new->m_pkthdr.len = m_new->m_len = packet->tot_data_buf_len;
1193 rxr->hn_small_pkts++;
1196 * Get an mbuf with a cluster. For packets 2K or less,
1197 * get a standard 2K cluster. For anything larger, get a
1198 * 4K cluster. Any buffers larger than 4K can cause problems
1199 * if looped around to the Hyper-V TX channel, so avoid them.
1202 if (packet->tot_data_buf_len > MCLBYTES) {
1204 size = MJUMPAGESIZE;
1207 m_new = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, size);
1208 if (m_new == NULL) {
1209 if_printf(ifp, "alloc mbuf failed.\n");
1213 hv_m_append(m_new, packet->tot_data_buf_len, packet->data);
1215 m_new->m_pkthdr.rcvif = ifp;
1217 if (__predict_false((ifp->if_capenable & IFCAP_RXCSUM) == 0))
1220 /* receive side checksum offload */
1221 if (csum_info != NULL) {
1222 /* IP csum offload */
1223 if (csum_info->receive.ip_csum_succeeded && do_csum) {
1224 m_new->m_pkthdr.csum_flags |=
1225 (CSUM_IP_CHECKED | CSUM_IP_VALID);
1229 /* TCP/UDP csum offload */
1230 if ((csum_info->receive.tcp_csum_succeeded ||
1231 csum_info->receive.udp_csum_succeeded) && do_csum) {
1232 m_new->m_pkthdr.csum_flags |=
1233 (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
1234 m_new->m_pkthdr.csum_data = 0xffff;
1235 if (csum_info->receive.tcp_csum_succeeded)
1241 if (csum_info->receive.ip_csum_succeeded &&
1242 csum_info->receive.tcp_csum_succeeded)
1245 const struct ether_header *eh;
1250 if (m_new->m_len < hoff)
1252 eh = mtod(m_new, struct ether_header *);
1253 etype = ntohs(eh->ether_type);
1254 if (etype == ETHERTYPE_VLAN) {
1255 const struct ether_vlan_header *evl;
1257 hoff = sizeof(*evl);
1258 if (m_new->m_len < hoff)
1260 evl = mtod(m_new, struct ether_vlan_header *);
1261 etype = ntohs(evl->evl_proto);
1264 if (etype == ETHERTYPE_IP) {
1267 pr = hn_check_iplen(m_new, hoff);
1268 if (pr == IPPROTO_TCP) {
1270 (rxr->hn_trust_hcsum &
1271 HN_TRUST_HCSUM_TCP)) {
1272 rxr->hn_csum_trusted++;
1273 m_new->m_pkthdr.csum_flags |=
1274 (CSUM_IP_CHECKED | CSUM_IP_VALID |
1275 CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
1276 m_new->m_pkthdr.csum_data = 0xffff;
1278 /* Rely on SW csum verification though... */
1280 } else if (pr == IPPROTO_UDP) {
1282 (rxr->hn_trust_hcsum &
1283 HN_TRUST_HCSUM_UDP)) {
1284 rxr->hn_csum_trusted++;
1285 m_new->m_pkthdr.csum_flags |=
1286 (CSUM_IP_CHECKED | CSUM_IP_VALID |
1287 CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
1288 m_new->m_pkthdr.csum_data = 0xffff;
1290 } else if (pr != IPPROTO_DONE && do_csum &&
1291 (rxr->hn_trust_hcsum & HN_TRUST_HCSUM_IP)) {
1292 rxr->hn_csum_trusted++;
1293 m_new->m_pkthdr.csum_flags |=
1294 (CSUM_IP_CHECKED | CSUM_IP_VALID);
1299 if ((packet->vlan_tci != 0) &&
1300 (ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0) {
1301 m_new->m_pkthdr.ether_vtag = packet->vlan_tci;
1302 m_new->m_flags |= M_VLANTAG;
1306 * Note: Moved RX completion back to hv_nv_on_receive() so all
1307 * messages (not just data messages) will trigger a response.
1312 if ((ifp->if_capenable & IFCAP_LRO) && do_lro) {
1313 #if defined(INET) || defined(INET6)
1314 struct lro_ctrl *lro = &rxr->hn_lro;
1317 rxr->hn_lro_tried++;
1318 if (tcp_lro_rx(lro, m_new, 0) == 0) {
1326 /* We're not holding the lock here, so don't release it */
1327 (*ifp->if_input)(ifp, m_new);
1333 netvsc_recv_rollup(struct hv_device *device_ctx __unused)
1338 * Rules for using sc->temp_unusable:
1339 * 1. sc->temp_unusable can only be read or written while holding NV_LOCK()
1340 * 2. code reading sc->temp_unusable under NV_LOCK(), and finding
1341 * sc->temp_unusable set, must release NV_LOCK() and exit
1342 * 3. to retain exclusive control of the interface,
1343 * sc->temp_unusable must be set by code before releasing NV_LOCK()
1344 * 4. only code setting sc->temp_unusable can clear sc->temp_unusable
1345 * 5. code setting sc->temp_unusable must eventually clear sc->temp_unusable
1349 * Standard ioctl entry point. Called when the user wants to configure
1353 hn_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1355 hn_softc_t *sc = ifp->if_softc;
1356 struct ifreq *ifr = (struct ifreq *)data;
1358 struct ifaddr *ifa = (struct ifaddr *)data;
1360 netvsc_device_info device_info;
1361 struct hv_device *hn_dev;
1362 int mask, error = 0;
1363 int retry_cnt = 500;
1369 if (ifa->ifa_addr->sa_family == AF_INET) {
1370 ifp->if_flags |= IFF_UP;
1371 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
1373 arp_ifinit(ifp, ifa);
1376 error = ether_ioctl(ifp, cmd, data);
1379 hn_dev = vmbus_get_devctx(sc->hn_dev);
1381 /* Check MTU value change */
1382 if (ifp->if_mtu == ifr->ifr_mtu)
1385 if (ifr->ifr_mtu > NETVSC_MAX_CONFIGURABLE_MTU) {
1390 /* Obtain and record requested MTU */
1391 ifp->if_mtu = ifr->ifr_mtu;
1393 #if __FreeBSD_version >= 1100099
1395 * Make sure that LRO aggregation length limit is still
1396 * valid, after the MTU change.
1399 if (sc->hn_rx_ring[0].hn_lro.lro_length_lim <
1400 HN_LRO_LENLIM_MIN(ifp)) {
1402 for (i = 0; i < sc->hn_rx_ring_cnt; ++i) {
1403 sc->hn_rx_ring[i].hn_lro.lro_length_lim =
1404 HN_LRO_LENLIM_MIN(ifp);
1412 if (!sc->temp_unusable) {
1413 sc->temp_unusable = TRUE;
1417 if (retry_cnt > 0) {
1421 } while (retry_cnt > 0);
1423 if (retry_cnt == 0) {
1428 /* We must remove and add back the device to cause the new
1429 * MTU to take effect. This includes tearing down, but not
1430 * deleting the channel, then bringing it back up.
1432 error = hv_rf_on_device_remove(hn_dev, HV_RF_NV_RETAIN_CHANNEL);
1435 sc->temp_unusable = FALSE;
1439 error = hv_rf_on_device_add(hn_dev, &device_info);
1442 sc->temp_unusable = FALSE;
1447 sc->hn_tx_chimney_max = sc->net_dev->send_section_size;
1448 if (sc->hn_tx_ring[0].hn_tx_chimney_size >
1449 sc->hn_tx_chimney_max)
1450 hn_set_tx_chimney_size(sc, sc->hn_tx_chimney_max);
1452 hn_ifinit_locked(sc);
1455 sc->temp_unusable = FALSE;
1461 if (!sc->temp_unusable) {
1462 sc->temp_unusable = TRUE;
1466 if (retry_cnt > 0) {
1470 } while (retry_cnt > 0);
1472 if (retry_cnt == 0) {
1477 if (ifp->if_flags & IFF_UP) {
1479 * If only the state of the PROMISC flag changed,
1480 * then just use the 'set promisc mode' command
1481 * instead of reinitializing the entire NIC. Doing
1482 * a full re-init means reloading the firmware and
1483 * waiting for it to start up, which may take a
1487 /* Fixme: Promiscuous mode? */
1488 if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
1489 ifp->if_flags & IFF_PROMISC &&
1490 !(sc->hn_if_flags & IFF_PROMISC)) {
1491 /* do something here for Hyper-V */
1492 } else if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
1493 !(ifp->if_flags & IFF_PROMISC) &&
1494 sc->hn_if_flags & IFF_PROMISC) {
1495 /* do something here for Hyper-V */
1498 hn_ifinit_locked(sc);
1500 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1505 sc->temp_unusable = FALSE;
1507 sc->hn_if_flags = ifp->if_flags;
1513 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1514 if (mask & IFCAP_TXCSUM) {
1515 ifp->if_capenable ^= IFCAP_TXCSUM;
1516 if (ifp->if_capenable & IFCAP_TXCSUM) {
1518 sc->hn_tx_ring[0].hn_csum_assist;
1521 ~sc->hn_tx_ring[0].hn_csum_assist;
1525 if (mask & IFCAP_RXCSUM)
1526 ifp->if_capenable ^= IFCAP_RXCSUM;
1528 if (mask & IFCAP_LRO)
1529 ifp->if_capenable ^= IFCAP_LRO;
1531 if (mask & IFCAP_TSO4) {
1532 ifp->if_capenable ^= IFCAP_TSO4;
1533 if (ifp->if_capenable & IFCAP_TSO4)
1534 ifp->if_hwassist |= CSUM_IP_TSO;
1536 ifp->if_hwassist &= ~CSUM_IP_TSO;
1539 if (mask & IFCAP_TSO6) {
1540 ifp->if_capenable ^= IFCAP_TSO6;
1541 if (ifp->if_capenable & IFCAP_TSO6)
1542 ifp->if_hwassist |= CSUM_IP6_TSO;
1544 ifp->if_hwassist &= ~CSUM_IP6_TSO;
1553 /* Fixme: Multicast mode? */
1554 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1556 netvsc_setmulti(sc);
1565 error = ifmedia_ioctl(ifp, ifr, &sc->hn_media, cmd);
1568 error = ether_ioctl(ifp, cmd, data);
1579 hn_stop(hn_softc_t *sc)
1583 struct hv_device *device_ctx = vmbus_get_devctx(sc->hn_dev);
1588 printf(" Closing Device ...\n");
1590 atomic_clear_int(&ifp->if_drv_flags,
1591 (IFF_DRV_RUNNING | IFF_DRV_OACTIVE));
1592 for (i = 0; i < sc->hn_tx_ring_cnt; ++i)
1593 sc->hn_tx_ring[i].hn_oactive = 0;
1595 if_link_state_change(ifp, LINK_STATE_DOWN);
1596 sc->hn_initdone = 0;
1598 ret = hv_rf_on_close(device_ctx);
1602 * FreeBSD transmit entry point
1605 hn_start(struct ifnet *ifp)
1607 struct hn_softc *sc = ifp->if_softc;
1608 struct hn_tx_ring *txr = &sc->hn_tx_ring[0];
1610 if (txr->hn_sched_tx)
1613 if (mtx_trylock(&txr->hn_tx_lock)) {
1616 sched = hn_start_locked(txr, txr->hn_direct_tx_size);
1617 mtx_unlock(&txr->hn_tx_lock);
1622 taskqueue_enqueue(txr->hn_tx_taskq, &txr->hn_tx_task);
1626 hn_start_txeof(struct hn_tx_ring *txr)
1628 struct hn_softc *sc = txr->hn_sc;
1629 struct ifnet *ifp = sc->hn_ifp;
1631 KASSERT(txr == &sc->hn_tx_ring[0], ("not the first TX ring"));
1633 if (txr->hn_sched_tx)
1636 if (mtx_trylock(&txr->hn_tx_lock)) {
1639 atomic_clear_int(&ifp->if_drv_flags, IFF_DRV_OACTIVE);
1640 sched = hn_start_locked(txr, txr->hn_direct_tx_size);
1641 mtx_unlock(&txr->hn_tx_lock);
1643 taskqueue_enqueue(txr->hn_tx_taskq,
1649 * Release the OACTIVE earlier, with the hope, that
1650 * others could catch up. The task will clear the
1651 * flag again with the hn_tx_lock to avoid possible
1654 atomic_clear_int(&ifp->if_drv_flags, IFF_DRV_OACTIVE);
1655 taskqueue_enqueue(txr->hn_tx_taskq, &txr->hn_txeof_task);
1663 hn_ifinit_locked(hn_softc_t *sc)
1666 struct hv_device *device_ctx = vmbus_get_devctx(sc->hn_dev);
1671 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1675 hv_promisc_mode = 1;
1677 ret = hv_rf_on_open(device_ctx);
1681 sc->hn_initdone = 1;
1684 atomic_clear_int(&ifp->if_drv_flags, IFF_DRV_OACTIVE);
1685 for (i = 0; i < sc->hn_tx_ring_cnt; ++i)
1686 sc->hn_tx_ring[i].hn_oactive = 0;
1688 atomic_set_int(&ifp->if_drv_flags, IFF_DRV_RUNNING);
1689 if_link_state_change(ifp, LINK_STATE_UP);
1696 hn_ifinit(void *xsc)
1698 hn_softc_t *sc = xsc;
1701 if (sc->temp_unusable) {
1705 sc->temp_unusable = TRUE;
1708 hn_ifinit_locked(sc);
1711 sc->temp_unusable = FALSE;
1720 hn_watchdog(struct ifnet *ifp)
1725 printf("hn%d: watchdog timeout -- resetting\n", sc->hn_unit);
1726 hn_ifinit(sc); /*???*/
1731 #if __FreeBSD_version >= 1100099
1734 hn_lro_lenlim_sysctl(SYSCTL_HANDLER_ARGS)
1736 struct hn_softc *sc = arg1;
1737 unsigned int lenlim;
1740 lenlim = sc->hn_rx_ring[0].hn_lro.lro_length_lim;
1741 error = sysctl_handle_int(oidp, &lenlim, 0, req);
1742 if (error || req->newptr == NULL)
1745 if (lenlim < HN_LRO_LENLIM_MIN(sc->hn_ifp) ||
1746 lenlim > TCP_LRO_LENGTH_MAX)
1750 for (i = 0; i < sc->hn_rx_ring_cnt; ++i)
1751 sc->hn_rx_ring[i].hn_lro.lro_length_lim = lenlim;
1757 hn_lro_ackcnt_sysctl(SYSCTL_HANDLER_ARGS)
1759 struct hn_softc *sc = arg1;
1760 int ackcnt, error, i;
1763 * lro_ackcnt_lim is append count limit,
1764 * +1 to turn it into aggregation limit.
1766 ackcnt = sc->hn_rx_ring[0].hn_lro.lro_ackcnt_lim + 1;
1767 error = sysctl_handle_int(oidp, &ackcnt, 0, req);
1768 if (error || req->newptr == NULL)
1771 if (ackcnt < 2 || ackcnt > (TCP_LRO_ACKCNT_MAX + 1))
1775 * Convert aggregation limit back to append
1780 for (i = 0; i < sc->hn_rx_ring_cnt; ++i)
1781 sc->hn_rx_ring[i].hn_lro.lro_ackcnt_lim = ackcnt;
1789 hn_trust_hcsum_sysctl(SYSCTL_HANDLER_ARGS)
1791 struct hn_softc *sc = arg1;
1796 if (sc->hn_rx_ring[0].hn_trust_hcsum & hcsum)
1799 error = sysctl_handle_int(oidp, &on, 0, req);
1800 if (error || req->newptr == NULL)
1804 for (i = 0; i < sc->hn_rx_ring_cnt; ++i) {
1805 struct hn_rx_ring *rxr = &sc->hn_rx_ring[i];
1808 rxr->hn_trust_hcsum |= hcsum;
1810 rxr->hn_trust_hcsum &= ~hcsum;
1817 hn_tx_chimney_size_sysctl(SYSCTL_HANDLER_ARGS)
1819 struct hn_softc *sc = arg1;
1820 int chimney_size, error;
1822 chimney_size = sc->hn_tx_ring[0].hn_tx_chimney_size;
1823 error = sysctl_handle_int(oidp, &chimney_size, 0, req);
1824 if (error || req->newptr == NULL)
1827 if (chimney_size > sc->hn_tx_chimney_max || chimney_size <= 0)
1830 hn_set_tx_chimney_size(sc, chimney_size);
1834 #if __FreeBSD_version < 1100095
1836 hn_rx_stat_int_sysctl(SYSCTL_HANDLER_ARGS)
1838 struct hn_softc *sc = arg1;
1839 int ofs = arg2, i, error;
1840 struct hn_rx_ring *rxr;
1844 for (i = 0; i < sc->hn_rx_ring_cnt; ++i) {
1845 rxr = &sc->hn_rx_ring[i];
1846 stat += *((int *)((uint8_t *)rxr + ofs));
1849 error = sysctl_handle_64(oidp, &stat, 0, req);
1850 if (error || req->newptr == NULL)
1853 /* Zero out this stat. */
1854 for (i = 0; i < sc->hn_rx_ring_cnt; ++i) {
1855 rxr = &sc->hn_rx_ring[i];
1856 *((int *)((uint8_t *)rxr + ofs)) = 0;
1862 hn_rx_stat_u64_sysctl(SYSCTL_HANDLER_ARGS)
1864 struct hn_softc *sc = arg1;
1865 int ofs = arg2, i, error;
1866 struct hn_rx_ring *rxr;
1870 for (i = 0; i < sc->hn_rx_ring_cnt; ++i) {
1871 rxr = &sc->hn_rx_ring[i];
1872 stat += *((uint64_t *)((uint8_t *)rxr + ofs));
1875 error = sysctl_handle_64(oidp, &stat, 0, req);
1876 if (error || req->newptr == NULL)
1879 /* Zero out this stat. */
1880 for (i = 0; i < sc->hn_rx_ring_cnt; ++i) {
1881 rxr = &sc->hn_rx_ring[i];
1882 *((uint64_t *)((uint8_t *)rxr + ofs)) = 0;
1890 hn_rx_stat_ulong_sysctl(SYSCTL_HANDLER_ARGS)
1892 struct hn_softc *sc = arg1;
1893 int ofs = arg2, i, error;
1894 struct hn_rx_ring *rxr;
1898 for (i = 0; i < sc->hn_rx_ring_cnt; ++i) {
1899 rxr = &sc->hn_rx_ring[i];
1900 stat += *((u_long *)((uint8_t *)rxr + ofs));
1903 error = sysctl_handle_long(oidp, &stat, 0, req);
1904 if (error || req->newptr == NULL)
1907 /* Zero out this stat. */
1908 for (i = 0; i < sc->hn_rx_ring_cnt; ++i) {
1909 rxr = &sc->hn_rx_ring[i];
1910 *((u_long *)((uint8_t *)rxr + ofs)) = 0;
1916 hn_tx_stat_ulong_sysctl(SYSCTL_HANDLER_ARGS)
1918 struct hn_softc *sc = arg1;
1919 int ofs = arg2, i, error;
1920 struct hn_tx_ring *txr;
1924 for (i = 0; i < sc->hn_tx_ring_cnt; ++i) {
1925 txr = &sc->hn_tx_ring[i];
1926 stat += *((u_long *)((uint8_t *)txr + ofs));
1929 error = sysctl_handle_long(oidp, &stat, 0, req);
1930 if (error || req->newptr == NULL)
1933 /* Zero out this stat. */
1934 for (i = 0; i < sc->hn_tx_ring_cnt; ++i) {
1935 txr = &sc->hn_tx_ring[i];
1936 *((u_long *)((uint8_t *)txr + ofs)) = 0;
1942 hn_tx_conf_int_sysctl(SYSCTL_HANDLER_ARGS)
1944 struct hn_softc *sc = arg1;
1945 int ofs = arg2, i, error, conf;
1946 struct hn_tx_ring *txr;
1948 txr = &sc->hn_tx_ring[0];
1949 conf = *((int *)((uint8_t *)txr + ofs));
1951 error = sysctl_handle_int(oidp, &conf, 0, req);
1952 if (error || req->newptr == NULL)
1956 for (i = 0; i < sc->hn_tx_ring_cnt; ++i) {
1957 txr = &sc->hn_tx_ring[i];
1958 *((int *)((uint8_t *)txr + ofs)) = conf;
1966 hn_check_iplen(const struct mbuf *m, int hoff)
1968 const struct ip *ip;
1969 int len, iphlen, iplen;
1970 const struct tcphdr *th;
1971 int thoff; /* TCP data offset */
1973 len = hoff + sizeof(struct ip);
1975 /* The packet must be at least the size of an IP header. */
1976 if (m->m_pkthdr.len < len)
1977 return IPPROTO_DONE;
1979 /* The fixed IP header must reside completely in the first mbuf. */
1981 return IPPROTO_DONE;
1983 ip = mtodo(m, hoff);
1985 /* Bound check the packet's stated IP header length. */
1986 iphlen = ip->ip_hl << 2;
1987 if (iphlen < sizeof(struct ip)) /* minimum header length */
1988 return IPPROTO_DONE;
1990 /* The full IP header must reside completely in the one mbuf. */
1991 if (m->m_len < hoff + iphlen)
1992 return IPPROTO_DONE;
1994 iplen = ntohs(ip->ip_len);
1997 * Check that the amount of data in the buffers is as
1998 * at least much as the IP header would have us expect.
2000 if (m->m_pkthdr.len < hoff + iplen)
2001 return IPPROTO_DONE;
2004 * Ignore IP fragments.
2006 if (ntohs(ip->ip_off) & (IP_OFFMASK | IP_MF))
2007 return IPPROTO_DONE;
2010 * The TCP/IP or UDP/IP header must be entirely contained within
2011 * the first fragment of a packet.
2015 if (iplen < iphlen + sizeof(struct tcphdr))
2016 return IPPROTO_DONE;
2017 if (m->m_len < hoff + iphlen + sizeof(struct tcphdr))
2018 return IPPROTO_DONE;
2019 th = (const struct tcphdr *)((const uint8_t *)ip + iphlen);
2020 thoff = th->th_off << 2;
2021 if (thoff < sizeof(struct tcphdr) || thoff + iphlen > iplen)
2022 return IPPROTO_DONE;
2023 if (m->m_len < hoff + iphlen + thoff)
2024 return IPPROTO_DONE;
2027 if (iplen < iphlen + sizeof(struct udphdr))
2028 return IPPROTO_DONE;
2029 if (m->m_len < hoff + iphlen + sizeof(struct udphdr))
2030 return IPPROTO_DONE;
2034 return IPPROTO_DONE;
2041 hn_dma_map_paddr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
2043 bus_addr_t *paddr = arg;
2048 KASSERT(nseg == 1, ("too many segments %d!", nseg));
2049 *paddr = segs->ds_addr;
2053 hn_create_rx_data(struct hn_softc *sc)
2055 struct sysctl_oid_list *child;
2056 struct sysctl_ctx_list *ctx;
2057 device_t dev = sc->hn_dev;
2058 #if defined(INET) || defined(INET6)
2059 #if __FreeBSD_version >= 1100095
2065 sc->hn_rx_ring_cnt = 1; /* TODO: vRSS */
2066 sc->hn_rx_ring = malloc(sizeof(struct hn_rx_ring) * sc->hn_rx_ring_cnt,
2067 M_NETVSC, M_WAITOK | M_ZERO);
2069 #if defined(INET) || defined(INET6)
2070 #if __FreeBSD_version >= 1100095
2071 lroent_cnt = hn_lro_entry_count;
2072 if (lroent_cnt < TCP_LRO_ENTRIES)
2073 lroent_cnt = TCP_LRO_ENTRIES;
2074 device_printf(dev, "LRO: entry count %d\n", lroent_cnt);
2076 #endif /* INET || INET6 */
2078 for (i = 0; i < sc->hn_rx_ring_cnt; ++i) {
2079 struct hn_rx_ring *rxr = &sc->hn_rx_ring[i];
2081 if (hn_trust_hosttcp)
2082 rxr->hn_trust_hcsum |= HN_TRUST_HCSUM_TCP;
2083 if (hn_trust_hostudp)
2084 rxr->hn_trust_hcsum |= HN_TRUST_HCSUM_UDP;
2085 if (hn_trust_hostip)
2086 rxr->hn_trust_hcsum |= HN_TRUST_HCSUM_IP;
2091 #if defined(INET) || defined(INET6)
2092 #if __FreeBSD_version >= 1100095
2093 tcp_lro_init_args(&rxr->hn_lro, sc->hn_ifp, lroent_cnt, 0);
2095 tcp_lro_init(&rxr->hn_lro);
2096 rxr->hn_lro.ifp = sc->hn_ifp;
2098 #if __FreeBSD_version >= 1100099
2099 rxr->hn_lro.lro_length_lim = HN_LRO_LENLIM_DEF;
2100 rxr->hn_lro.lro_ackcnt_lim = HN_LRO_ACKCNT_DEF;
2102 #endif /* INET || INET6 */
2105 ctx = device_get_sysctl_ctx(dev);
2106 child = SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
2108 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "lro_queued",
2109 CTLTYPE_U64 | CTLFLAG_RW, sc,
2110 __offsetof(struct hn_rx_ring, hn_lro.lro_queued),
2111 #if __FreeBSD_version < 1100095
2112 hn_rx_stat_int_sysctl,
2114 hn_rx_stat_u64_sysctl,
2116 "LU", "LRO queued");
2117 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "lro_flushed",
2118 CTLTYPE_U64 | CTLFLAG_RW, sc,
2119 __offsetof(struct hn_rx_ring, hn_lro.lro_flushed),
2120 #if __FreeBSD_version < 1100095
2121 hn_rx_stat_int_sysctl,
2123 hn_rx_stat_u64_sysctl,
2125 "LU", "LRO flushed");
2126 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "lro_tried",
2127 CTLTYPE_ULONG | CTLFLAG_RW, sc,
2128 __offsetof(struct hn_rx_ring, hn_lro_tried),
2129 hn_rx_stat_ulong_sysctl, "LU", "# of LRO tries");
2130 #if __FreeBSD_version >= 1100099
2131 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "lro_length_lim",
2132 CTLTYPE_UINT | CTLFLAG_RW, sc, 0, hn_lro_lenlim_sysctl, "IU",
2133 "Max # of data bytes to be aggregated by LRO");
2134 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "lro_ackcnt_lim",
2135 CTLTYPE_INT | CTLFLAG_RW, sc, 0, hn_lro_ackcnt_sysctl, "I",
2136 "Max # of ACKs to be aggregated by LRO");
2138 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "trust_hosttcp",
2139 CTLTYPE_INT | CTLFLAG_RW, sc, HN_TRUST_HCSUM_TCP,
2140 hn_trust_hcsum_sysctl, "I",
2141 "Trust tcp segement verification on host side, "
2142 "when csum info is missing");
2143 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "trust_hostudp",
2144 CTLTYPE_INT | CTLFLAG_RW, sc, HN_TRUST_HCSUM_UDP,
2145 hn_trust_hcsum_sysctl, "I",
2146 "Trust udp datagram verification on host side, "
2147 "when csum info is missing");
2148 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "trust_hostip",
2149 CTLTYPE_INT | CTLFLAG_RW, sc, HN_TRUST_HCSUM_IP,
2150 hn_trust_hcsum_sysctl, "I",
2151 "Trust ip packet verification on host side, "
2152 "when csum info is missing");
2153 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "csum_ip",
2154 CTLTYPE_ULONG | CTLFLAG_RW, sc,
2155 __offsetof(struct hn_rx_ring, hn_csum_ip),
2156 hn_rx_stat_ulong_sysctl, "LU", "RXCSUM IP");
2157 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "csum_tcp",
2158 CTLTYPE_ULONG | CTLFLAG_RW, sc,
2159 __offsetof(struct hn_rx_ring, hn_csum_tcp),
2160 hn_rx_stat_ulong_sysctl, "LU", "RXCSUM TCP");
2161 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "csum_udp",
2162 CTLTYPE_ULONG | CTLFLAG_RW, sc,
2163 __offsetof(struct hn_rx_ring, hn_csum_udp),
2164 hn_rx_stat_ulong_sysctl, "LU", "RXCSUM UDP");
2165 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "csum_trusted",
2166 CTLTYPE_ULONG | CTLFLAG_RW, sc,
2167 __offsetof(struct hn_rx_ring, hn_csum_trusted),
2168 hn_rx_stat_ulong_sysctl, "LU",
2169 "# of packets that we trust host's csum verification");
2170 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "small_pkts",
2171 CTLTYPE_ULONG | CTLFLAG_RW, sc,
2172 __offsetof(struct hn_rx_ring, hn_small_pkts),
2173 hn_rx_stat_ulong_sysctl, "LU", "# of small packets received");
2177 hn_destroy_rx_data(struct hn_softc *sc)
2179 #if defined(INET) || defined(INET6)
2183 if (sc->hn_rx_ring_cnt == 0)
2186 #if defined(INET) || defined(INET6)
2187 for (i = 0; i < sc->hn_rx_ring_cnt; ++i)
2188 tcp_lro_free(&sc->hn_rx_ring[i].hn_lro);
2190 free(sc->hn_rx_ring, M_NETVSC);
2191 sc->hn_rx_ring = NULL;
2193 sc->hn_rx_ring_cnt = 0;
2197 hn_create_tx_ring(struct hn_softc *sc, int id)
2199 struct hn_tx_ring *txr = &sc->hn_tx_ring[id];
2200 bus_dma_tag_t parent_dtag;
2205 #ifndef HN_USE_TXDESC_BUFRING
2206 mtx_init(&txr->hn_txlist_spin, "hn txlist", NULL, MTX_SPIN);
2208 mtx_init(&txr->hn_tx_lock, "hn tx", NULL, MTX_DEF);
2210 txr->hn_txdesc_cnt = HN_TX_DESC_CNT;
2211 txr->hn_txdesc = malloc(sizeof(struct hn_txdesc) * txr->hn_txdesc_cnt,
2212 M_NETVSC, M_WAITOK | M_ZERO);
2213 #ifndef HN_USE_TXDESC_BUFRING
2214 SLIST_INIT(&txr->hn_txlist);
2216 txr->hn_txdesc_br = buf_ring_alloc(txr->hn_txdesc_cnt, M_NETVSC,
2217 M_WAITOK, &txr->hn_tx_lock);
2220 txr->hn_tx_taskq = sc->hn_tx_taskq;
2222 if (hn_use_if_start) {
2223 txr->hn_txeof = hn_start_txeof;
2224 TASK_INIT(&txr->hn_tx_task, 0, hn_start_taskfunc, txr);
2225 TASK_INIT(&txr->hn_txeof_task, 0, hn_start_txeof_taskfunc, txr);
2227 txr->hn_txeof = hn_xmit_txeof;
2228 TASK_INIT(&txr->hn_tx_task, 0, hn_xmit_taskfunc, txr);
2229 TASK_INIT(&txr->hn_txeof_task, 0, hn_xmit_txeof_taskfunc, txr);
2230 txr->hn_mbuf_br = buf_ring_alloc(txr->hn_txdesc_cnt, M_NETVSC,
2231 M_WAITOK, &txr->hn_tx_lock);
2234 txr->hn_direct_tx_size = hn_direct_tx_size;
2235 if (hv_vmbus_protocal_version >= HV_VMBUS_VERSION_WIN8_1)
2236 txr->hn_csum_assist = HN_CSUM_ASSIST;
2238 txr->hn_csum_assist = HN_CSUM_ASSIST_WIN8;
2241 * Always schedule transmission instead of trying to do direct
2242 * transmission. This one gives the best performance so far.
2244 txr->hn_sched_tx = 1;
2246 parent_dtag = bus_get_dma_tag(sc->hn_dev);
2248 /* DMA tag for RNDIS messages. */
2249 error = bus_dma_tag_create(parent_dtag, /* parent */
2250 HN_RNDIS_MSG_ALIGN, /* alignment */
2251 HN_RNDIS_MSG_BOUNDARY, /* boundary */
2252 BUS_SPACE_MAXADDR, /* lowaddr */
2253 BUS_SPACE_MAXADDR, /* highaddr */
2254 NULL, NULL, /* filter, filterarg */
2255 HN_RNDIS_MSG_LEN, /* maxsize */
2257 HN_RNDIS_MSG_LEN, /* maxsegsize */
2259 NULL, /* lockfunc */
2260 NULL, /* lockfuncarg */
2261 &txr->hn_tx_rndis_dtag);
2263 device_printf(sc->hn_dev, "failed to create rndis dmatag\n");
2267 /* DMA tag for data. */
2268 error = bus_dma_tag_create(parent_dtag, /* parent */
2270 HN_TX_DATA_BOUNDARY, /* boundary */
2271 BUS_SPACE_MAXADDR, /* lowaddr */
2272 BUS_SPACE_MAXADDR, /* highaddr */
2273 NULL, NULL, /* filter, filterarg */
2274 HN_TX_DATA_MAXSIZE, /* maxsize */
2275 HN_TX_DATA_SEGCNT_MAX, /* nsegments */
2276 HN_TX_DATA_SEGSIZE, /* maxsegsize */
2278 NULL, /* lockfunc */
2279 NULL, /* lockfuncarg */
2280 &txr->hn_tx_data_dtag);
2282 device_printf(sc->hn_dev, "failed to create data dmatag\n");
2286 for (i = 0; i < txr->hn_txdesc_cnt; ++i) {
2287 struct hn_txdesc *txd = &txr->hn_txdesc[i];
2292 * Allocate and load RNDIS messages.
2294 error = bus_dmamem_alloc(txr->hn_tx_rndis_dtag,
2295 (void **)&txd->rndis_msg,
2296 BUS_DMA_WAITOK | BUS_DMA_COHERENT,
2297 &txd->rndis_msg_dmap);
2299 device_printf(sc->hn_dev,
2300 "failed to allocate rndis_msg, %d\n", i);
2304 error = bus_dmamap_load(txr->hn_tx_rndis_dtag,
2305 txd->rndis_msg_dmap,
2306 txd->rndis_msg, HN_RNDIS_MSG_LEN,
2307 hn_dma_map_paddr, &txd->rndis_msg_paddr,
2310 device_printf(sc->hn_dev,
2311 "failed to load rndis_msg, %d\n", i);
2312 bus_dmamem_free(txr->hn_tx_rndis_dtag,
2313 txd->rndis_msg, txd->rndis_msg_dmap);
2317 /* DMA map for TX data. */
2318 error = bus_dmamap_create(txr->hn_tx_data_dtag, 0,
2321 device_printf(sc->hn_dev,
2322 "failed to allocate tx data dmamap\n");
2323 bus_dmamap_unload(txr->hn_tx_rndis_dtag,
2324 txd->rndis_msg_dmap);
2325 bus_dmamem_free(txr->hn_tx_rndis_dtag,
2326 txd->rndis_msg, txd->rndis_msg_dmap);
2330 /* All set, put it to list */
2331 txd->flags |= HN_TXD_FLAG_ONLIST;
2332 #ifndef HN_USE_TXDESC_BUFRING
2333 SLIST_INSERT_HEAD(&txr->hn_txlist, txd, link);
2335 buf_ring_enqueue(txr->hn_txdesc_br, txd);
2338 txr->hn_txdesc_avail = txr->hn_txdesc_cnt;
2340 if (sc->hn_tx_sysctl_tree != NULL) {
2341 struct sysctl_oid_list *child;
2342 struct sysctl_ctx_list *ctx;
2346 * Create per TX ring sysctl tree:
2347 * dev.hn.UNIT.tx.RINGID
2349 ctx = device_get_sysctl_ctx(sc->hn_dev);
2350 child = SYSCTL_CHILDREN(sc->hn_tx_sysctl_tree);
2352 snprintf(name, sizeof(name), "%d", id);
2353 txr->hn_tx_sysctl_tree = SYSCTL_ADD_NODE(ctx, child, OID_AUTO,
2354 name, CTLFLAG_RD, 0, "");
2356 if (txr->hn_tx_sysctl_tree != NULL) {
2357 child = SYSCTL_CHILDREN(txr->hn_tx_sysctl_tree);
2359 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "txdesc_avail",
2360 CTLFLAG_RD, &txr->hn_txdesc_avail, 0,
2361 "# of available TX descs");
2362 if (!hn_use_if_start) {
2363 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "oactive",
2364 CTLFLAG_RD, &txr->hn_oactive, 0,
2374 hn_txdesc_dmamap_destroy(struct hn_txdesc *txd)
2376 struct hn_tx_ring *txr = txd->txr;
2378 KASSERT(txd->m == NULL, ("still has mbuf installed"));
2379 KASSERT((txd->flags & HN_TXD_FLAG_DMAMAP) == 0, ("still dma mapped"));
2381 bus_dmamap_unload(txr->hn_tx_rndis_dtag, txd->rndis_msg_dmap);
2382 bus_dmamem_free(txr->hn_tx_rndis_dtag, txd->rndis_msg,
2383 txd->rndis_msg_dmap);
2384 bus_dmamap_destroy(txr->hn_tx_data_dtag, txd->data_dmap);
2388 hn_destroy_tx_ring(struct hn_tx_ring *txr)
2390 struct hn_txdesc *txd;
2392 if (txr->hn_txdesc == NULL)
2395 #ifndef HN_USE_TXDESC_BUFRING
2396 while ((txd = SLIST_FIRST(&txr->hn_txlist)) != NULL) {
2397 SLIST_REMOVE_HEAD(&txr->hn_txlist, link);
2398 hn_txdesc_dmamap_destroy(txd);
2401 mtx_lock(&txr->hn_tx_lock);
2402 while ((txd = buf_ring_dequeue_sc(txr->hn_txdesc_br)) != NULL)
2403 hn_txdesc_dmamap_destroy(txd);
2404 mtx_unlock(&txr->hn_tx_lock);
2407 if (txr->hn_tx_data_dtag != NULL)
2408 bus_dma_tag_destroy(txr->hn_tx_data_dtag);
2409 if (txr->hn_tx_rndis_dtag != NULL)
2410 bus_dma_tag_destroy(txr->hn_tx_rndis_dtag);
2412 #ifdef HN_USE_TXDESC_BUFRING
2413 buf_ring_free(txr->hn_txdesc_br, M_NETVSC);
2416 free(txr->hn_txdesc, M_NETVSC);
2417 txr->hn_txdesc = NULL;
2419 if (txr->hn_mbuf_br != NULL)
2420 buf_ring_free(txr->hn_mbuf_br, M_NETVSC);
2422 #ifndef HN_USE_TXDESC_BUFRING
2423 mtx_destroy(&txr->hn_txlist_spin);
2425 mtx_destroy(&txr->hn_tx_lock);
2429 hn_create_tx_data(struct hn_softc *sc)
2431 struct sysctl_oid_list *child;
2432 struct sysctl_ctx_list *ctx;
2435 if (hn_use_if_start) {
2436 /* ifnet.if_start only needs one TX ring */
2437 sc->hn_tx_ring_cnt = 1;
2439 sc->hn_tx_ring_cnt = 1; /* TODO: vRSS */
2441 sc->hn_tx_ring = malloc(sizeof(struct hn_tx_ring) * sc->hn_tx_ring_cnt,
2442 M_NETVSC, M_WAITOK | M_ZERO);
2444 ctx = device_get_sysctl_ctx(sc->hn_dev);
2445 child = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->hn_dev));
2447 /* Create dev.hn.UNIT.tx sysctl tree */
2448 sc->hn_tx_sysctl_tree = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, "tx",
2451 for (i = 0; i < sc->hn_tx_ring_cnt; ++i) {
2454 error = hn_create_tx_ring(sc, i);
2459 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "no_txdescs",
2460 CTLTYPE_ULONG | CTLFLAG_RW, sc,
2461 __offsetof(struct hn_tx_ring, hn_no_txdescs),
2462 hn_tx_stat_ulong_sysctl, "LU", "# of times short of TX descs");
2463 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "send_failed",
2464 CTLTYPE_ULONG | CTLFLAG_RW, sc,
2465 __offsetof(struct hn_tx_ring, hn_send_failed),
2466 hn_tx_stat_ulong_sysctl, "LU", "# of hyper-v sending failure");
2467 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "txdma_failed",
2468 CTLTYPE_ULONG | CTLFLAG_RW, sc,
2469 __offsetof(struct hn_tx_ring, hn_txdma_failed),
2470 hn_tx_stat_ulong_sysctl, "LU", "# of TX DMA failure");
2471 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "tx_collapsed",
2472 CTLTYPE_ULONG | CTLFLAG_RW, sc,
2473 __offsetof(struct hn_tx_ring, hn_tx_collapsed),
2474 hn_tx_stat_ulong_sysctl, "LU", "# of TX mbuf collapsed");
2475 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "tx_chimney",
2476 CTLTYPE_ULONG | CTLFLAG_RW, sc,
2477 __offsetof(struct hn_tx_ring, hn_tx_chimney),
2478 hn_tx_stat_ulong_sysctl, "LU", "# of chimney send");
2479 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "txdesc_cnt",
2480 CTLFLAG_RD, &sc->hn_tx_ring[0].hn_txdesc_cnt, 0,
2481 "# of total TX descs");
2482 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "tx_chimney_max",
2483 CTLFLAG_RD, &sc->hn_tx_chimney_max, 0,
2484 "Chimney send packet size upper boundary");
2485 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "tx_chimney_size",
2486 CTLTYPE_INT | CTLFLAG_RW, sc, 0, hn_tx_chimney_size_sysctl,
2487 "I", "Chimney send packet size limit");
2488 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "direct_tx_size",
2489 CTLTYPE_INT | CTLFLAG_RW, sc,
2490 __offsetof(struct hn_tx_ring, hn_direct_tx_size),
2491 hn_tx_conf_int_sysctl, "I",
2492 "Size of the packet for direct transmission");
2493 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "sched_tx",
2494 CTLTYPE_INT | CTLFLAG_RW, sc,
2495 __offsetof(struct hn_tx_ring, hn_sched_tx),
2496 hn_tx_conf_int_sysctl, "I",
2497 "Always schedule transmission "
2498 "instead of doing direct transmission");
2504 hn_set_tx_chimney_size(struct hn_softc *sc, int chimney_size)
2509 for (i = 0; i < sc->hn_tx_ring_cnt; ++i)
2510 sc->hn_tx_ring[i].hn_tx_chimney_size = chimney_size;
2515 hn_destroy_tx_data(struct hn_softc *sc)
2519 if (sc->hn_tx_ring_cnt == 0)
2522 for (i = 0; i < sc->hn_tx_ring_cnt; ++i)
2523 hn_destroy_tx_ring(&sc->hn_tx_ring[i]);
2525 free(sc->hn_tx_ring, M_NETVSC);
2526 sc->hn_tx_ring = NULL;
2528 sc->hn_tx_ring_cnt = 0;
2532 hn_start_taskfunc(void *xtxr, int pending __unused)
2534 struct hn_tx_ring *txr = xtxr;
2536 mtx_lock(&txr->hn_tx_lock);
2537 hn_start_locked(txr, 0);
2538 mtx_unlock(&txr->hn_tx_lock);
2542 hn_start_txeof_taskfunc(void *xtxr, int pending __unused)
2544 struct hn_tx_ring *txr = xtxr;
2546 mtx_lock(&txr->hn_tx_lock);
2547 atomic_clear_int(&txr->hn_sc->hn_ifp->if_drv_flags, IFF_DRV_OACTIVE);
2548 hn_start_locked(txr, 0);
2549 mtx_unlock(&txr->hn_tx_lock);
2553 hn_stop_tx_tasks(struct hn_softc *sc)
2557 for (i = 0; i < sc->hn_tx_ring_cnt; ++i) {
2558 struct hn_tx_ring *txr = &sc->hn_tx_ring[i];
2560 taskqueue_drain(txr->hn_tx_taskq, &txr->hn_tx_task);
2561 taskqueue_drain(txr->hn_tx_taskq, &txr->hn_txeof_task);
2566 hn_xmit(struct hn_tx_ring *txr, int len)
2568 struct hn_softc *sc = txr->hn_sc;
2569 struct ifnet *ifp = sc->hn_ifp;
2570 struct hv_device *device_ctx = vmbus_get_devctx(sc->hn_dev);
2571 struct mbuf *m_head;
2573 mtx_assert(&txr->hn_tx_lock, MA_OWNED);
2574 KASSERT(hn_use_if_start == 0,
2575 ("hn_xmit is called, when if_start is enabled"));
2577 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || txr->hn_oactive)
2580 while ((m_head = drbr_peek(ifp, txr->hn_mbuf_br)) != NULL) {
2581 struct hn_txdesc *txd;
2584 if (len > 0 && m_head->m_pkthdr.len > len) {
2586 * This sending could be time consuming; let callers
2587 * dispatch this packet sending (and sending of any
2588 * following up packets) to tx taskqueue.
2590 drbr_putback(ifp, txr->hn_mbuf_br, m_head);
2594 txd = hn_txdesc_get(txr);
2596 txr->hn_no_txdescs++;
2597 drbr_putback(ifp, txr->hn_mbuf_br, m_head);
2598 txr->hn_oactive = 1;
2602 error = hn_encap(txr, txd, &m_head);
2604 /* Both txd and m_head are freed; discard */
2605 drbr_advance(ifp, txr->hn_mbuf_br);
2609 error = hn_send_pkt(ifp, device_ctx, txr, txd);
2610 if (__predict_false(error)) {
2611 /* txd is freed, but m_head is not */
2612 drbr_putback(ifp, txr->hn_mbuf_br, m_head);
2613 txr->hn_oactive = 1;
2618 drbr_advance(ifp, txr->hn_mbuf_br);
2624 hn_transmit(struct ifnet *ifp, struct mbuf *m)
2626 struct hn_softc *sc = ifp->if_softc;
2627 struct hn_tx_ring *txr;
2630 /* TODO: vRSS, TX ring selection */
2631 txr = &sc->hn_tx_ring[0];
2633 error = drbr_enqueue(ifp, txr->hn_mbuf_br, m);
2637 if (txr->hn_oactive)
2640 if (txr->hn_sched_tx)
2643 if (mtx_trylock(&txr->hn_tx_lock)) {
2646 sched = hn_xmit(txr, txr->hn_direct_tx_size);
2647 mtx_unlock(&txr->hn_tx_lock);
2652 taskqueue_enqueue(txr->hn_tx_taskq, &txr->hn_tx_task);
2657 hn_xmit_qflush(struct ifnet *ifp)
2659 struct hn_softc *sc = ifp->if_softc;
2662 for (i = 0; i < sc->hn_tx_ring_cnt; ++i) {
2663 struct hn_tx_ring *txr = &sc->hn_tx_ring[i];
2666 mtx_lock(&txr->hn_tx_lock);
2667 while ((m = buf_ring_dequeue_sc(txr->hn_mbuf_br)) != NULL)
2669 mtx_unlock(&txr->hn_tx_lock);
2675 hn_xmit_txeof(struct hn_tx_ring *txr)
2678 if (txr->hn_sched_tx)
2681 if (mtx_trylock(&txr->hn_tx_lock)) {
2684 txr->hn_oactive = 0;
2685 sched = hn_xmit(txr, txr->hn_direct_tx_size);
2686 mtx_unlock(&txr->hn_tx_lock);
2688 taskqueue_enqueue(txr->hn_tx_taskq,
2694 * Release the oactive earlier, with the hope, that
2695 * others could catch up. The task will clear the
2696 * oactive again with the hn_tx_lock to avoid possible
2699 txr->hn_oactive = 0;
2700 taskqueue_enqueue(txr->hn_tx_taskq, &txr->hn_txeof_task);
2705 hn_xmit_taskfunc(void *xtxr, int pending __unused)
2707 struct hn_tx_ring *txr = xtxr;
2709 mtx_lock(&txr->hn_tx_lock);
2711 mtx_unlock(&txr->hn_tx_lock);
2715 hn_xmit_txeof_taskfunc(void *xtxr, int pending __unused)
2717 struct hn_tx_ring *txr = xtxr;
2719 mtx_lock(&txr->hn_tx_lock);
2720 txr->hn_oactive = 0;
2722 mtx_unlock(&txr->hn_tx_lock);
2726 hn_tx_taskq_create(void *arg __unused)
2728 if (!hn_share_tx_taskq)
2731 hn_tx_taskq = taskqueue_create("hn_tx", M_WAITOK,
2732 taskqueue_thread_enqueue, &hn_tx_taskq);
2733 taskqueue_start_threads(&hn_tx_taskq, 1, PI_NET, "hn tx");
2734 if (hn_bind_tx_taskq >= 0) {
2735 int cpu = hn_bind_tx_taskq;
2736 struct task cpuset_task;
2739 if (cpu > mp_ncpus - 1)
2741 CPU_SETOF(cpu, &cpu_set);
2742 TASK_INIT(&cpuset_task, 0, hn_cpuset_setthread_task, &cpu_set);
2743 taskqueue_enqueue(hn_tx_taskq, &cpuset_task);
2744 taskqueue_drain(hn_tx_taskq, &cpuset_task);
2747 SYSINIT(hn_txtq_create, SI_SUB_DRIVERS, SI_ORDER_FIRST,
2748 hn_tx_taskq_create, NULL);
2751 hn_tx_taskq_destroy(void *arg __unused)
2753 if (hn_tx_taskq != NULL)
2754 taskqueue_free(hn_tx_taskq);
2756 SYSUNINIT(hn_txtq_destroy, SI_SUB_DRIVERS, SI_ORDER_FIRST,
2757 hn_tx_taskq_destroy, NULL);
2759 static device_method_t netvsc_methods[] = {
2760 /* Device interface */
2761 DEVMETHOD(device_probe, netvsc_probe),
2762 DEVMETHOD(device_attach, netvsc_attach),
2763 DEVMETHOD(device_detach, netvsc_detach),
2764 DEVMETHOD(device_shutdown, netvsc_shutdown),
2769 static driver_t netvsc_driver = {
2775 static devclass_t netvsc_devclass;
2777 DRIVER_MODULE(hn, vmbus, netvsc_driver, netvsc_devclass, 0, 0);
2778 MODULE_VERSION(hn, 1);
2779 MODULE_DEPEND(hn, vmbus, 1, 1, 1);