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>
69 #include <sys/queue.h>
72 #include <sys/sysctl.h>
75 #include <net/if_arp.h>
76 #include <net/ethernet.h>
77 #include <net/if_dl.h>
78 #include <net/if_media.h>
82 #include <net/if_types.h>
83 #include <net/if_vlan_var.h>
86 #include <netinet/in_systm.h>
87 #include <netinet/in.h>
88 #include <netinet/ip.h>
89 #include <netinet/if_ether.h>
90 #include <netinet/tcp.h>
91 #include <netinet/udp.h>
92 #include <netinet/ip6.h>
95 #include <vm/vm_param.h>
96 #include <vm/vm_kern.h>
99 #include <machine/bus.h>
100 #include <machine/resource.h>
101 #include <machine/frame.h>
102 #include <machine/vmparam.h>
105 #include <sys/rman.h>
106 #include <sys/mutex.h>
107 #include <sys/errno.h>
108 #include <sys/types.h>
109 #include <machine/atomic.h>
111 #include <machine/intr_machdep.h>
113 #include <machine/in_cksum.h>
115 #include <dev/hyperv/include/hyperv.h>
116 #include "hv_net_vsc.h"
117 #include "hv_rndis.h"
118 #include "hv_rndis_filter.h"
121 /* Short for Hyper-V network interface */
122 #define NETVSC_DEVNAME "hn"
125 * It looks like offset 0 of buf is reserved to hold the softc pointer.
126 * The sc pointer evidently not needed, and is not presently populated.
127 * The packet offset is where the netvsc_packet starts in the buffer.
129 #define HV_NV_SC_PTR_OFFSET_IN_BUF 0
130 #define HV_NV_PACKET_OFFSET_IN_BUF 16
132 /* YYY should get it from the underlying channel */
133 #define HN_TX_DESC_CNT 512
135 #define HN_LROENT_CNT_DEF 128
137 #define HN_RNDIS_MSG_LEN \
138 (sizeof(rndis_msg) + \
139 RNDIS_VLAN_PPI_SIZE + \
140 RNDIS_TSO_PPI_SIZE + \
142 #define HN_RNDIS_MSG_BOUNDARY PAGE_SIZE
143 #define HN_RNDIS_MSG_ALIGN CACHE_LINE_SIZE
145 #define HN_TX_DATA_BOUNDARY PAGE_SIZE
146 #define HN_TX_DATA_MAXSIZE IP_MAXPACKET
147 #define HN_TX_DATA_SEGSIZE PAGE_SIZE
148 #define HN_TX_DATA_SEGCNT_MAX \
149 (NETVSC_PACKET_MAXPAGE - HV_RF_NUM_TX_RESERVED_PAGE_BUFS)
151 #define HN_DIRECT_TX_SIZE_DEF 128
154 SLIST_ENTRY(hn_txdesc) link;
156 struct hn_tx_ring *txr;
158 uint32_t flags; /* HN_TXD_FLAG_ */
159 netvsc_packet netvsc_pkt; /* XXX to be removed */
161 bus_dmamap_t data_dmap;
163 bus_addr_t rndis_msg_paddr;
164 rndis_msg *rndis_msg;
165 bus_dmamap_t rndis_msg_dmap;
168 #define HN_TXD_FLAG_ONLIST 0x1
169 #define HN_TXD_FLAG_DMAMAP 0x2
172 * Only enable UDP checksum offloading when it is on 2012R2 or
173 * later. UDP checksum offloading doesn't work on earlier
176 #define HN_CSUM_ASSIST_WIN8 (CSUM_TCP)
177 #define HN_CSUM_ASSIST (CSUM_IP | CSUM_UDP | CSUM_TCP)
179 #define HN_LRO_LENLIM_DEF (25 * ETHERMTU)
180 /* YYY 2*MTU is a bit rough, but should be good enough. */
181 #define HN_LRO_LENLIM_MIN(ifp) (2 * (ifp)->if_mtu)
183 #define HN_LRO_ACKCNT_DEF 1
186 * Be aware that this sleepable mutex will exhibit WITNESS errors when
187 * certain TCP and ARP code paths are taken. This appears to be a
188 * well-known condition, as all other drivers checked use a sleeping
189 * mutex to protect their transmit paths.
190 * Also Be aware that mutexes do not play well with semaphores, and there
191 * is a conflicting semaphore in a certain channel code path.
193 #define NV_LOCK_INIT(_sc, _name) \
194 mtx_init(&(_sc)->hn_lock, _name, MTX_NETWORK_LOCK, MTX_DEF)
195 #define NV_LOCK(_sc) mtx_lock(&(_sc)->hn_lock)
196 #define NV_LOCK_ASSERT(_sc) mtx_assert(&(_sc)->hn_lock, MA_OWNED)
197 #define NV_UNLOCK(_sc) mtx_unlock(&(_sc)->hn_lock)
198 #define NV_LOCK_DESTROY(_sc) mtx_destroy(&(_sc)->hn_lock)
205 int hv_promisc_mode = 0; /* normal mode by default */
207 SYSCTL_NODE(_hw, OID_AUTO, hn, CTLFLAG_RD, NULL, "Hyper-V network interface");
209 /* Trust tcp segements verification on host side. */
210 static int hn_trust_hosttcp = 1;
211 SYSCTL_INT(_hw_hn, OID_AUTO, trust_hosttcp, CTLFLAG_RDTUN,
212 &hn_trust_hosttcp, 0,
213 "Trust tcp segement verification on host side, "
214 "when csum info is missing (global setting)");
216 /* Trust udp datagrams verification on host side. */
217 static int hn_trust_hostudp = 1;
218 SYSCTL_INT(_hw_hn, OID_AUTO, trust_hostudp, CTLFLAG_RDTUN,
219 &hn_trust_hostudp, 0,
220 "Trust udp datagram verification on host side, "
221 "when csum info is missing (global setting)");
223 /* Trust ip packets verification on host side. */
224 static int hn_trust_hostip = 1;
225 SYSCTL_INT(_hw_hn, OID_AUTO, trust_hostip, CTLFLAG_RDTUN,
227 "Trust ip packet verification on host side, "
228 "when csum info is missing (global setting)");
230 #if __FreeBSD_version >= 1100045
231 /* Limit TSO burst size */
232 static int hn_tso_maxlen = 0;
233 SYSCTL_INT(_hw_hn, OID_AUTO, tso_maxlen, CTLFLAG_RDTUN,
234 &hn_tso_maxlen, 0, "TSO burst limit");
237 /* Limit chimney send size */
238 static int hn_tx_chimney_size = 0;
239 SYSCTL_INT(_hw_hn, OID_AUTO, tx_chimney_size, CTLFLAG_RDTUN,
240 &hn_tx_chimney_size, 0, "Chimney send packet size limit");
242 /* Limit the size of packet for direct transmission */
243 static int hn_direct_tx_size = HN_DIRECT_TX_SIZE_DEF;
244 SYSCTL_INT(_hw_hn, OID_AUTO, direct_tx_size, CTLFLAG_RDTUN,
245 &hn_direct_tx_size, 0, "Size of the packet for direct transmission");
247 #if defined(INET) || defined(INET6)
248 #if __FreeBSD_version >= 1100095
249 static int hn_lro_entry_count = HN_LROENT_CNT_DEF;
250 SYSCTL_INT(_hw_hn, OID_AUTO, lro_entry_count, CTLFLAG_RDTUN,
251 &hn_lro_entry_count, 0, "LRO entry count");
255 static int hn_share_tx_taskq = 0;
256 SYSCTL_INT(_hw_hn, OID_AUTO, share_tx_taskq, CTLFLAG_RDTUN,
257 &hn_share_tx_taskq, 0, "Enable shared TX taskqueue");
259 static struct taskqueue *hn_tx_taskq;
262 * Forward declarations
264 static void hn_stop(hn_softc_t *sc);
265 static void hn_ifinit_locked(hn_softc_t *sc);
266 static void hn_ifinit(void *xsc);
267 static int hn_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
268 static int hn_start_locked(struct hn_tx_ring *txr, int len);
269 static void hn_start(struct ifnet *ifp);
270 static void hn_start_txeof(struct hn_tx_ring *);
271 static int hn_ifmedia_upd(struct ifnet *ifp);
272 static void hn_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr);
273 #if __FreeBSD_version >= 1100099
274 static int hn_lro_lenlim_sysctl(SYSCTL_HANDLER_ARGS);
275 static int hn_lro_ackcnt_sysctl(SYSCTL_HANDLER_ARGS);
277 static int hn_trust_hcsum_sysctl(SYSCTL_HANDLER_ARGS);
278 static int hn_tx_chimney_size_sysctl(SYSCTL_HANDLER_ARGS);
279 static int hn_rx_stat_ulong_sysctl(SYSCTL_HANDLER_ARGS);
280 static int hn_rx_stat_u64_sysctl(SYSCTL_HANDLER_ARGS);
281 static int hn_tx_stat_ulong_sysctl(SYSCTL_HANDLER_ARGS);
282 static int hn_tx_conf_int_sysctl(SYSCTL_HANDLER_ARGS);
283 static int hn_check_iplen(const struct mbuf *, int);
284 static int hn_create_tx_ring(struct hn_softc *, int);
285 static void hn_destroy_tx_ring(struct hn_tx_ring *);
286 static int hn_create_tx_data(struct hn_softc *);
287 static void hn_destroy_tx_data(struct hn_softc *);
288 static void hn_start_taskfunc(void *xsc, int pending);
289 static void hn_txeof_taskfunc(void *xsc, int pending);
290 static void hn_stop_tx_tasks(struct hn_softc *);
291 static int hn_encap(struct hn_tx_ring *, struct hn_txdesc *, struct mbuf **);
292 static void hn_create_rx_data(struct hn_softc *sc);
293 static void hn_destroy_rx_data(struct hn_softc *sc);
294 static void hn_set_tx_chimney_size(struct hn_softc *, int);
297 hn_ifmedia_upd(struct ifnet *ifp __unused)
304 hn_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
306 struct hn_softc *sc = ifp->if_softc;
308 ifmr->ifm_status = IFM_AVALID;
309 ifmr->ifm_active = IFM_ETHER;
311 if (!sc->hn_carrier) {
312 ifmr->ifm_active |= IFM_NONE;
315 ifmr->ifm_status |= IFM_ACTIVE;
316 ifmr->ifm_active |= IFM_10G_T | IFM_FDX;
319 /* {F8615163-DF3E-46c5-913F-F2D2F965ED0E} */
320 static const hv_guid g_net_vsc_device_type = {
321 .data = {0x63, 0x51, 0x61, 0xF8, 0x3E, 0xDF, 0xc5, 0x46,
322 0x91, 0x3F, 0xF2, 0xD2, 0xF9, 0x65, 0xED, 0x0E}
326 * Standard probe entry point.
330 netvsc_probe(device_t dev)
334 p = vmbus_get_type(dev);
335 if (!memcmp(p, &g_net_vsc_device_type.data, sizeof(hv_guid))) {
336 device_set_desc(dev, "Synthetic Network Interface");
338 printf("Netvsc probe... DONE \n");
340 return (BUS_PROBE_DEFAULT);
347 * Standard attach entry point.
349 * Called when the driver is loaded. It allocates needed resources,
350 * and initializes the "hardware" and software.
353 netvsc_attach(device_t dev)
355 struct hv_device *device_ctx = vmbus_get_devctx(dev);
356 netvsc_device_info device_info;
358 int unit = device_get_unit(dev);
359 struct ifnet *ifp = NULL;
361 #if __FreeBSD_version >= 1100045
365 sc = device_get_softc(dev);
370 bzero(sc, sizeof(hn_softc_t));
374 if (hn_tx_taskq == NULL) {
375 sc->hn_tx_taskq = taskqueue_create("hn_tx", M_WAITOK,
376 taskqueue_thread_enqueue, &sc->hn_tx_taskq);
377 taskqueue_start_threads(&sc->hn_tx_taskq, 1, PI_NET, "%s tx",
378 device_get_nameunit(dev));
380 sc->hn_tx_taskq = hn_tx_taskq;
382 NV_LOCK_INIT(sc, "NetVSCLock");
384 sc->hn_dev_obj = device_ctx;
386 ifp = sc->hn_ifp = sc->arpcom.ac_ifp = if_alloc(IFT_ETHER);
389 error = hn_create_tx_data(sc);
393 hn_create_rx_data(sc);
395 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
396 ifp->if_dunit = unit;
397 ifp->if_dname = NETVSC_DEVNAME;
399 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
400 ifp->if_ioctl = hn_ioctl;
401 ifp->if_start = hn_start;
402 ifp->if_init = hn_ifinit;
403 /* needed by hv_rf_on_device_add() code */
404 ifp->if_mtu = ETHERMTU;
405 IFQ_SET_MAXLEN(&ifp->if_snd, 512);
406 ifp->if_snd.ifq_drv_maxlen = 511;
407 IFQ_SET_READY(&ifp->if_snd);
409 ifmedia_init(&sc->hn_media, 0, hn_ifmedia_upd, hn_ifmedia_sts);
410 ifmedia_add(&sc->hn_media, IFM_ETHER | IFM_AUTO, 0, NULL);
411 ifmedia_set(&sc->hn_media, IFM_ETHER | IFM_AUTO);
412 /* XXX ifmedia_set really should do this for us */
413 sc->hn_media.ifm_media = sc->hn_media.ifm_cur->ifm_media;
416 * Tell upper layers that we support full VLAN capability.
418 ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
419 ifp->if_capabilities |=
420 IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU | IFCAP_HWCSUM | IFCAP_TSO |
423 IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU | IFCAP_HWCSUM | IFCAP_TSO |
425 ifp->if_hwassist = sc->hn_tx_ring[0].hn_csum_assist | CSUM_TSO;
427 error = hv_rf_on_device_add(device_ctx, &device_info);
431 if (device_info.link_state == 0) {
435 #if __FreeBSD_version >= 1100045
436 tso_maxlen = hn_tso_maxlen;
437 if (tso_maxlen <= 0 || tso_maxlen > IP_MAXPACKET)
438 tso_maxlen = IP_MAXPACKET;
440 ifp->if_hw_tsomaxsegcount = HN_TX_DATA_SEGCNT_MAX;
441 ifp->if_hw_tsomaxsegsize = PAGE_SIZE;
442 ifp->if_hw_tsomax = tso_maxlen -
443 (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN);
446 ether_ifattach(ifp, device_info.mac_addr);
448 #if __FreeBSD_version >= 1100045
449 if_printf(ifp, "TSO: %u/%u/%u\n", ifp->if_hw_tsomax,
450 ifp->if_hw_tsomaxsegcount, ifp->if_hw_tsomaxsegsize);
453 sc->hn_tx_chimney_max = sc->net_dev->send_section_size;
454 hn_set_tx_chimney_size(sc, sc->hn_tx_chimney_max);
455 if (hn_tx_chimney_size > 0 &&
456 hn_tx_chimney_size < sc->hn_tx_chimney_max)
457 hn_set_tx_chimney_size(sc, hn_tx_chimney_size);
461 hn_destroy_tx_data(sc);
468 * Standard detach entry point
471 netvsc_detach(device_t dev)
473 struct hn_softc *sc = device_get_softc(dev);
474 struct hv_device *hv_device = vmbus_get_devctx(dev);
477 printf("netvsc_detach\n");
480 * XXXKYS: Need to clean up all our
481 * driver state; this is the driver
486 * XXXKYS: Need to stop outgoing traffic and unregister
490 hv_rf_on_device_remove(hv_device, HV_RF_NV_DESTROY_CHANNEL);
492 hn_stop_tx_tasks(sc);
494 ifmedia_removeall(&sc->hn_media);
495 hn_destroy_rx_data(sc);
496 hn_destroy_tx_data(sc);
498 if (sc->hn_tx_taskq != hn_tx_taskq)
499 taskqueue_free(sc->hn_tx_taskq);
505 * Standard shutdown entry point
508 netvsc_shutdown(device_t dev)
514 hn_txdesc_dmamap_load(struct hn_tx_ring *txr, struct hn_txdesc *txd,
515 struct mbuf **m_head, bus_dma_segment_t *segs, int *nsegs)
517 struct mbuf *m = *m_head;
520 error = bus_dmamap_load_mbuf_sg(txr->hn_tx_data_dtag, txd->data_dmap,
521 m, segs, nsegs, BUS_DMA_NOWAIT);
522 if (error == EFBIG) {
525 m_new = m_collapse(m, M_NOWAIT, HN_TX_DATA_SEGCNT_MAX);
530 txr->hn_tx_collapsed++;
532 error = bus_dmamap_load_mbuf_sg(txr->hn_tx_data_dtag,
533 txd->data_dmap, m, segs, nsegs, BUS_DMA_NOWAIT);
536 bus_dmamap_sync(txr->hn_tx_data_dtag, txd->data_dmap,
537 BUS_DMASYNC_PREWRITE);
538 txd->flags |= HN_TXD_FLAG_DMAMAP;
544 hn_txdesc_dmamap_unload(struct hn_tx_ring *txr, struct hn_txdesc *txd)
547 if (txd->flags & HN_TXD_FLAG_DMAMAP) {
548 bus_dmamap_sync(txr->hn_tx_data_dtag,
549 txd->data_dmap, BUS_DMASYNC_POSTWRITE);
550 bus_dmamap_unload(txr->hn_tx_data_dtag,
552 txd->flags &= ~HN_TXD_FLAG_DMAMAP;
557 hn_txdesc_put(struct hn_tx_ring *txr, struct hn_txdesc *txd)
560 KASSERT((txd->flags & HN_TXD_FLAG_ONLIST) == 0,
561 ("put an onlist txd %#x", txd->flags));
563 KASSERT(txd->refs > 0, ("invalid txd refs %d", txd->refs));
564 if (atomic_fetchadd_int(&txd->refs, -1) != 1)
567 hn_txdesc_dmamap_unload(txr, txd);
568 if (txd->m != NULL) {
573 txd->flags |= HN_TXD_FLAG_ONLIST;
575 mtx_lock_spin(&txr->hn_txlist_spin);
576 KASSERT(txr->hn_txdesc_avail >= 0 &&
577 txr->hn_txdesc_avail < txr->hn_txdesc_cnt,
578 ("txdesc_put: invalid txd avail %d", txr->hn_txdesc_avail));
579 txr->hn_txdesc_avail++;
580 SLIST_INSERT_HEAD(&txr->hn_txlist, txd, link);
581 mtx_unlock_spin(&txr->hn_txlist_spin);
586 static __inline struct hn_txdesc *
587 hn_txdesc_get(struct hn_tx_ring *txr)
589 struct hn_txdesc *txd;
591 mtx_lock_spin(&txr->hn_txlist_spin);
592 txd = SLIST_FIRST(&txr->hn_txlist);
594 KASSERT(txr->hn_txdesc_avail > 0,
595 ("txdesc_get: invalid txd avail %d", txr->hn_txdesc_avail));
596 txr->hn_txdesc_avail--;
597 SLIST_REMOVE_HEAD(&txr->hn_txlist, link);
599 mtx_unlock_spin(&txr->hn_txlist_spin);
602 KASSERT(txd->m == NULL && txd->refs == 0 &&
603 (txd->flags & HN_TXD_FLAG_ONLIST), ("invalid txd"));
604 txd->flags &= ~HN_TXD_FLAG_ONLIST;
611 hn_txdesc_hold(struct hn_txdesc *txd)
614 /* 0->1 transition will never work */
615 KASSERT(txd->refs > 0, ("invalid refs %d", txd->refs));
616 atomic_add_int(&txd->refs, 1);
620 * Send completion processing
622 * Note: It looks like offset 0 of buf is reserved to hold the softc
623 * pointer. The sc pointer is not currently needed in this function, and
624 * it is not presently populated by the TX function.
627 netvsc_xmit_completion(void *context)
629 netvsc_packet *packet = context;
630 struct hn_txdesc *txd;
631 struct hn_tx_ring *txr;
633 txd = (struct hn_txdesc *)(uintptr_t)
634 packet->compl.send.send_completion_tid;
638 hn_txdesc_put(txr, txd);
642 netvsc_channel_rollup(struct hv_device *device_ctx)
644 struct hn_softc *sc = device_get_softc(device_ctx->device);
645 struct hn_tx_ring *txr = &sc->hn_tx_ring[0]; /* TODO: vRSS */
646 #if defined(INET) || defined(INET6)
647 struct hn_rx_ring *rxr = &sc->hn_rx_ring[0]; /* TODO: vRSS */
648 struct lro_ctrl *lro = &rxr->hn_lro;
649 struct lro_entry *queued;
651 while ((queued = SLIST_FIRST(&lro->lro_active)) != NULL) {
652 SLIST_REMOVE_HEAD(&lro->lro_active, next);
653 tcp_lro_flush(lro, queued);
666 * If this function fails, then both txd and m_head0 will be freed.
669 hn_encap(struct hn_tx_ring *txr, struct hn_txdesc *txd, struct mbuf **m_head0)
671 bus_dma_segment_t segs[HN_TX_DATA_SEGCNT_MAX];
673 struct mbuf *m_head = *m_head0;
674 netvsc_packet *packet;
675 rndis_msg *rndis_mesg;
676 rndis_packet *rndis_pkt;
677 rndis_per_packet_info *rppi;
678 uint32_t rndis_msg_size;
680 packet = &txd->netvsc_pkt;
681 packet->is_data_pkt = TRUE;
682 packet->tot_data_buf_len = m_head->m_pkthdr.len;
685 * extension points to the area reserved for the
686 * rndis_filter_packet, which is placed just after
687 * the netvsc_packet (and rppi struct, if present;
688 * length is updated later).
690 rndis_mesg = txd->rndis_msg;
691 /* XXX not necessary */
692 memset(rndis_mesg, 0, HN_RNDIS_MSG_LEN);
693 rndis_mesg->ndis_msg_type = REMOTE_NDIS_PACKET_MSG;
695 rndis_pkt = &rndis_mesg->msg.packet;
696 rndis_pkt->data_offset = sizeof(rndis_packet);
697 rndis_pkt->data_length = packet->tot_data_buf_len;
698 rndis_pkt->per_pkt_info_offset = sizeof(rndis_packet);
700 rndis_msg_size = RNDIS_MESSAGE_SIZE(rndis_packet);
702 if (m_head->m_flags & M_VLANTAG) {
703 ndis_8021q_info *rppi_vlan_info;
705 rndis_msg_size += RNDIS_VLAN_PPI_SIZE;
706 rppi = hv_set_rppi_data(rndis_mesg, RNDIS_VLAN_PPI_SIZE,
709 rppi_vlan_info = (ndis_8021q_info *)((uint8_t *)rppi +
710 rppi->per_packet_info_offset);
711 rppi_vlan_info->u1.s1.vlan_id =
712 m_head->m_pkthdr.ether_vtag & 0xfff;
715 if (m_head->m_pkthdr.csum_flags & CSUM_TSO) {
716 rndis_tcp_tso_info *tso_info;
717 struct ether_vlan_header *eh;
721 * XXX need m_pullup and use mtodo
723 eh = mtod(m_head, struct ether_vlan_header*);
724 if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN))
725 ether_len = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
727 ether_len = ETHER_HDR_LEN;
729 rndis_msg_size += RNDIS_TSO_PPI_SIZE;
730 rppi = hv_set_rppi_data(rndis_mesg, RNDIS_TSO_PPI_SIZE,
731 tcp_large_send_info);
733 tso_info = (rndis_tcp_tso_info *)((uint8_t *)rppi +
734 rppi->per_packet_info_offset);
735 tso_info->lso_v2_xmit.type =
736 RNDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE;
739 if (m_head->m_pkthdr.csum_flags & CSUM_IP_TSO) {
741 (struct ip *)(m_head->m_data + ether_len);
742 unsigned long iph_len = ip->ip_hl << 2;
744 (struct tcphdr *)((caddr_t)ip + iph_len);
746 tso_info->lso_v2_xmit.ip_version =
747 RNDIS_TCP_LARGE_SEND_OFFLOAD_IPV4;
751 th->th_sum = in_pseudo(ip->ip_src.s_addr,
752 ip->ip_dst.s_addr, htons(IPPROTO_TCP));
755 #if defined(INET6) && defined(INET)
760 struct ip6_hdr *ip6 = (struct ip6_hdr *)
761 (m_head->m_data + ether_len);
762 struct tcphdr *th = (struct tcphdr *)(ip6 + 1);
764 tso_info->lso_v2_xmit.ip_version =
765 RNDIS_TCP_LARGE_SEND_OFFLOAD_IPV6;
767 th->th_sum = in6_cksum_pseudo(ip6, 0, IPPROTO_TCP, 0);
770 tso_info->lso_v2_xmit.tcp_header_offset = 0;
771 tso_info->lso_v2_xmit.mss = m_head->m_pkthdr.tso_segsz;
772 } else if (m_head->m_pkthdr.csum_flags & txr->hn_csum_assist) {
773 rndis_tcp_ip_csum_info *csum_info;
775 rndis_msg_size += RNDIS_CSUM_PPI_SIZE;
776 rppi = hv_set_rppi_data(rndis_mesg, RNDIS_CSUM_PPI_SIZE,
778 csum_info = (rndis_tcp_ip_csum_info *)((uint8_t *)rppi +
779 rppi->per_packet_info_offset);
781 csum_info->xmit.is_ipv4 = 1;
782 if (m_head->m_pkthdr.csum_flags & CSUM_IP)
783 csum_info->xmit.ip_header_csum = 1;
785 if (m_head->m_pkthdr.csum_flags & CSUM_TCP) {
786 csum_info->xmit.tcp_csum = 1;
787 csum_info->xmit.tcp_header_offset = 0;
788 } else if (m_head->m_pkthdr.csum_flags & CSUM_UDP) {
789 csum_info->xmit.udp_csum = 1;
793 rndis_mesg->msg_len = packet->tot_data_buf_len + rndis_msg_size;
794 packet->tot_data_buf_len = rndis_mesg->msg_len;
797 * Chimney send, if the packet could fit into one chimney buffer.
799 if (packet->tot_data_buf_len < txr->hn_tx_chimney_size) {
800 netvsc_dev *net_dev = txr->hn_sc->net_dev;
801 uint32_t send_buf_section_idx;
803 send_buf_section_idx =
804 hv_nv_get_next_send_section(net_dev);
805 if (send_buf_section_idx !=
806 NVSP_1_CHIMNEY_SEND_INVALID_SECTION_INDEX) {
807 uint8_t *dest = ((uint8_t *)net_dev->send_buf +
808 (send_buf_section_idx *
809 net_dev->send_section_size));
811 memcpy(dest, rndis_mesg, rndis_msg_size);
812 dest += rndis_msg_size;
813 m_copydata(m_head, 0, m_head->m_pkthdr.len, dest);
815 packet->send_buf_section_idx = send_buf_section_idx;
816 packet->send_buf_section_size =
817 packet->tot_data_buf_len;
818 packet->page_buf_count = 0;
819 txr->hn_tx_chimney++;
824 error = hn_txdesc_dmamap_load(txr, txd, &m_head, segs, &nsegs);
829 * This mbuf is not linked w/ the txd yet, so free it now.
834 freed = hn_txdesc_put(txr, txd);
836 ("fail to free txd upon txdma error"));
838 txr->hn_txdma_failed++;
839 if_inc_counter(txr->hn_sc->hn_ifp, IFCOUNTER_OERRORS, 1);
844 packet->page_buf_count = nsegs + HV_RF_NUM_TX_RESERVED_PAGE_BUFS;
846 /* send packet with page buffer */
847 packet->page_buffers[0].pfn = atop(txd->rndis_msg_paddr);
848 packet->page_buffers[0].offset = txd->rndis_msg_paddr & PAGE_MASK;
849 packet->page_buffers[0].length = rndis_msg_size;
852 * Fill the page buffers with mbuf info starting at index
853 * HV_RF_NUM_TX_RESERVED_PAGE_BUFS.
855 for (i = 0; i < nsegs; ++i) {
856 hv_vmbus_page_buffer *pb = &packet->page_buffers[
857 i + HV_RF_NUM_TX_RESERVED_PAGE_BUFS];
859 pb->pfn = atop(segs[i].ds_addr);
860 pb->offset = segs[i].ds_addr & PAGE_MASK;
861 pb->length = segs[i].ds_len;
864 packet->send_buf_section_idx =
865 NVSP_1_CHIMNEY_SEND_INVALID_SECTION_INDEX;
866 packet->send_buf_section_size = 0;
870 /* Set the completion routine */
871 packet->compl.send.on_send_completion = netvsc_xmit_completion;
872 packet->compl.send.send_completion_context = packet;
873 packet->compl.send.send_completion_tid = (uint64_t)(uintptr_t)txd;
879 * Start a transmit of one or more packets
882 hn_start_locked(struct hn_tx_ring *txr, int len)
884 struct hn_softc *sc = txr->hn_sc;
885 struct ifnet *ifp = sc->hn_ifp;
886 struct hv_device *device_ctx = vmbus_get_devctx(sc->hn_dev);
888 KASSERT(txr == &sc->hn_tx_ring[0], ("not the first TX ring"));
889 mtx_assert(&txr->hn_tx_lock, MA_OWNED);
891 if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
895 while (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) {
896 int error, send_failed = 0;
897 struct hn_txdesc *txd;
900 IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
904 if (len > 0 && m_head->m_pkthdr.len > len) {
906 * This sending could be time consuming; let callers
907 * dispatch this packet sending (and sending of any
908 * following up packets) to tx taskqueue.
910 IF_PREPEND(&ifp->if_snd, m_head);
914 txd = hn_txdesc_get(txr);
916 txr->hn_no_txdescs++;
917 IF_PREPEND(&ifp->if_snd, m_head);
918 atomic_set_int(&ifp->if_drv_flags, IFF_DRV_OACTIVE);
922 error = hn_encap(txr, txd, &m_head);
924 /* Both txd and m_head are freed */
929 * Make sure that txd is not freed before ETHER_BPF_MTAP.
932 error = hv_nv_on_send(device_ctx, &txd->netvsc_pkt);
934 ETHER_BPF_MTAP(ifp, m_head);
935 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
937 hn_txdesc_put(txr, txd);
939 if (__predict_false(error)) {
943 * This should "really rarely" happen.
945 * XXX Too many RX to be acked or too many sideband
946 * commands to run? Ask netvsc_channel_rollup()
947 * to kick start later.
951 txr->hn_send_failed++;
954 * Try sending again after set hn_txeof;
955 * in case that we missed the last
956 * netvsc_channel_rollup().
960 if_printf(ifp, "send failed\n");
963 * This mbuf will be prepended, don't free it
964 * in hn_txdesc_put(); only unload it from the
965 * DMA map in hn_txdesc_put(), if it was loaded.
968 freed = hn_txdesc_put(txr, txd);
970 ("fail to free txd upon send error"));
972 txr->hn_send_failed++;
973 IF_PREPEND(&ifp->if_snd, m_head);
974 atomic_set_int(&ifp->if_drv_flags, IFF_DRV_OACTIVE);
982 * Link up/down notification
985 netvsc_linkstatus_callback(struct hv_device *device_obj, uint32_t status)
987 hn_softc_t *sc = device_get_softc(device_obj->device);
1001 * Append the specified data to the indicated mbuf chain,
1002 * Extend the mbuf chain if the new data does not fit in
1005 * This is a minor rewrite of m_append() from sys/kern/uipc_mbuf.c.
1006 * There should be an equivalent in the kernel mbuf code,
1007 * but there does not appear to be one yet.
1009 * Differs from m_append() in that additional mbufs are
1010 * allocated with cluster size MJUMPAGESIZE, and filled
1013 * Return 1 if able to complete the job; otherwise 0.
1016 hv_m_append(struct mbuf *m0, int len, c_caddr_t cp)
1019 int remainder, space;
1021 for (m = m0; m->m_next != NULL; m = m->m_next)
1024 space = M_TRAILINGSPACE(m);
1027 * Copy into available space.
1029 if (space > remainder)
1031 bcopy(cp, mtod(m, caddr_t) + m->m_len, space);
1036 while (remainder > 0) {
1038 * Allocate a new mbuf; could check space
1039 * and allocate a cluster instead.
1041 n = m_getjcl(M_DONTWAIT, m->m_type, 0, MJUMPAGESIZE);
1044 n->m_len = min(MJUMPAGESIZE, remainder);
1045 bcopy(cp, mtod(n, caddr_t), n->m_len);
1047 remainder -= n->m_len;
1051 if (m0->m_flags & M_PKTHDR)
1052 m0->m_pkthdr.len += len - remainder;
1054 return (remainder == 0);
1059 * Called when we receive a data packet from the "wire" on the
1062 * Note: This is no longer used as a callback
1065 netvsc_recv(struct hv_device *device_ctx, netvsc_packet *packet,
1066 rndis_tcp_ip_csum_info *csum_info)
1068 struct hn_softc *sc = device_get_softc(device_ctx->device);
1069 struct hn_rx_ring *rxr = &sc->hn_rx_ring[0]; /* TODO: vRSS */
1072 int size, do_lro = 0, do_csum = 1;
1075 return (0); /* TODO: KYS how can this be! */
1080 ifp = sc->arpcom.ac_ifp;
1082 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1087 * Bail out if packet contains more data than configured MTU.
1089 if (packet->tot_data_buf_len > (ifp->if_mtu + ETHER_HDR_LEN)) {
1091 } else if (packet->tot_data_buf_len <= MHLEN) {
1092 m_new = m_gethdr(M_NOWAIT, MT_DATA);
1095 memcpy(mtod(m_new, void *), packet->data,
1096 packet->tot_data_buf_len);
1097 m_new->m_pkthdr.len = m_new->m_len = packet->tot_data_buf_len;
1098 rxr->hn_small_pkts++;
1101 * Get an mbuf with a cluster. For packets 2K or less,
1102 * get a standard 2K cluster. For anything larger, get a
1103 * 4K cluster. Any buffers larger than 4K can cause problems
1104 * if looped around to the Hyper-V TX channel, so avoid them.
1107 if (packet->tot_data_buf_len > MCLBYTES) {
1109 size = MJUMPAGESIZE;
1112 m_new = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, size);
1113 if (m_new == NULL) {
1114 if_printf(ifp, "alloc mbuf failed.\n");
1118 hv_m_append(m_new, packet->tot_data_buf_len, packet->data);
1120 m_new->m_pkthdr.rcvif = ifp;
1122 if (__predict_false((ifp->if_capenable & IFCAP_RXCSUM) == 0))
1125 /* receive side checksum offload */
1126 if (csum_info != NULL) {
1127 /* IP csum offload */
1128 if (csum_info->receive.ip_csum_succeeded && do_csum) {
1129 m_new->m_pkthdr.csum_flags |=
1130 (CSUM_IP_CHECKED | CSUM_IP_VALID);
1134 /* TCP/UDP csum offload */
1135 if ((csum_info->receive.tcp_csum_succeeded ||
1136 csum_info->receive.udp_csum_succeeded) && do_csum) {
1137 m_new->m_pkthdr.csum_flags |=
1138 (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
1139 m_new->m_pkthdr.csum_data = 0xffff;
1140 if (csum_info->receive.tcp_csum_succeeded)
1146 if (csum_info->receive.ip_csum_succeeded &&
1147 csum_info->receive.tcp_csum_succeeded)
1150 const struct ether_header *eh;
1155 if (m_new->m_len < hoff)
1157 eh = mtod(m_new, struct ether_header *);
1158 etype = ntohs(eh->ether_type);
1159 if (etype == ETHERTYPE_VLAN) {
1160 const struct ether_vlan_header *evl;
1162 hoff = sizeof(*evl);
1163 if (m_new->m_len < hoff)
1165 evl = mtod(m_new, struct ether_vlan_header *);
1166 etype = ntohs(evl->evl_proto);
1169 if (etype == ETHERTYPE_IP) {
1172 pr = hn_check_iplen(m_new, hoff);
1173 if (pr == IPPROTO_TCP) {
1175 (rxr->hn_trust_hcsum &
1176 HN_TRUST_HCSUM_TCP)) {
1177 rxr->hn_csum_trusted++;
1178 m_new->m_pkthdr.csum_flags |=
1179 (CSUM_IP_CHECKED | CSUM_IP_VALID |
1180 CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
1181 m_new->m_pkthdr.csum_data = 0xffff;
1183 /* Rely on SW csum verification though... */
1185 } else if (pr == IPPROTO_UDP) {
1187 (rxr->hn_trust_hcsum &
1188 HN_TRUST_HCSUM_UDP)) {
1189 rxr->hn_csum_trusted++;
1190 m_new->m_pkthdr.csum_flags |=
1191 (CSUM_IP_CHECKED | CSUM_IP_VALID |
1192 CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
1193 m_new->m_pkthdr.csum_data = 0xffff;
1195 } else if (pr != IPPROTO_DONE && do_csum &&
1196 (rxr->hn_trust_hcsum & HN_TRUST_HCSUM_IP)) {
1197 rxr->hn_csum_trusted++;
1198 m_new->m_pkthdr.csum_flags |=
1199 (CSUM_IP_CHECKED | CSUM_IP_VALID);
1204 if ((packet->vlan_tci != 0) &&
1205 (ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0) {
1206 m_new->m_pkthdr.ether_vtag = packet->vlan_tci;
1207 m_new->m_flags |= M_VLANTAG;
1211 * Note: Moved RX completion back to hv_nv_on_receive() so all
1212 * messages (not just data messages) will trigger a response.
1217 if ((ifp->if_capenable & IFCAP_LRO) && do_lro) {
1218 #if defined(INET) || defined(INET6)
1219 struct lro_ctrl *lro = &rxr->hn_lro;
1222 rxr->hn_lro_tried++;
1223 if (tcp_lro_rx(lro, m_new, 0) == 0) {
1231 /* We're not holding the lock here, so don't release it */
1232 (*ifp->if_input)(ifp, m_new);
1238 netvsc_recv_rollup(struct hv_device *device_ctx __unused)
1243 * Rules for using sc->temp_unusable:
1244 * 1. sc->temp_unusable can only be read or written while holding NV_LOCK()
1245 * 2. code reading sc->temp_unusable under NV_LOCK(), and finding
1246 * sc->temp_unusable set, must release NV_LOCK() and exit
1247 * 3. to retain exclusive control of the interface,
1248 * sc->temp_unusable must be set by code before releasing NV_LOCK()
1249 * 4. only code setting sc->temp_unusable can clear sc->temp_unusable
1250 * 5. code setting sc->temp_unusable must eventually clear sc->temp_unusable
1254 * Standard ioctl entry point. Called when the user wants to configure
1258 hn_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1260 hn_softc_t *sc = ifp->if_softc;
1261 struct ifreq *ifr = (struct ifreq *)data;
1263 struct ifaddr *ifa = (struct ifaddr *)data;
1265 netvsc_device_info device_info;
1266 struct hv_device *hn_dev;
1267 int mask, error = 0;
1268 int retry_cnt = 500;
1274 if (ifa->ifa_addr->sa_family == AF_INET) {
1275 ifp->if_flags |= IFF_UP;
1276 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
1278 arp_ifinit(ifp, ifa);
1281 error = ether_ioctl(ifp, cmd, data);
1284 hn_dev = vmbus_get_devctx(sc->hn_dev);
1286 /* Check MTU value change */
1287 if (ifp->if_mtu == ifr->ifr_mtu)
1290 if (ifr->ifr_mtu > NETVSC_MAX_CONFIGURABLE_MTU) {
1295 /* Obtain and record requested MTU */
1296 ifp->if_mtu = ifr->ifr_mtu;
1298 #if __FreeBSD_version >= 1100099
1300 * Make sure that LRO aggregation length limit is still
1301 * valid, after the MTU change.
1304 if (sc->hn_rx_ring[0].hn_lro.lro_length_lim <
1305 HN_LRO_LENLIM_MIN(ifp)) {
1307 for (i = 0; i < sc->hn_rx_ring_cnt; ++i) {
1308 sc->hn_rx_ring[i].hn_lro.lro_length_lim =
1309 HN_LRO_LENLIM_MIN(ifp);
1317 if (!sc->temp_unusable) {
1318 sc->temp_unusable = TRUE;
1322 if (retry_cnt > 0) {
1326 } while (retry_cnt > 0);
1328 if (retry_cnt == 0) {
1333 /* We must remove and add back the device to cause the new
1334 * MTU to take effect. This includes tearing down, but not
1335 * deleting the channel, then bringing it back up.
1337 error = hv_rf_on_device_remove(hn_dev, HV_RF_NV_RETAIN_CHANNEL);
1340 sc->temp_unusable = FALSE;
1344 error = hv_rf_on_device_add(hn_dev, &device_info);
1347 sc->temp_unusable = FALSE;
1352 sc->hn_tx_chimney_max = sc->net_dev->send_section_size;
1353 if (sc->hn_tx_ring[0].hn_tx_chimney_size >
1354 sc->hn_tx_chimney_max)
1355 hn_set_tx_chimney_size(sc, sc->hn_tx_chimney_max);
1357 hn_ifinit_locked(sc);
1360 sc->temp_unusable = FALSE;
1366 if (!sc->temp_unusable) {
1367 sc->temp_unusable = TRUE;
1371 if (retry_cnt > 0) {
1375 } while (retry_cnt > 0);
1377 if (retry_cnt == 0) {
1382 if (ifp->if_flags & IFF_UP) {
1384 * If only the state of the PROMISC flag changed,
1385 * then just use the 'set promisc mode' command
1386 * instead of reinitializing the entire NIC. Doing
1387 * a full re-init means reloading the firmware and
1388 * waiting for it to start up, which may take a
1392 /* Fixme: Promiscuous mode? */
1393 if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
1394 ifp->if_flags & IFF_PROMISC &&
1395 !(sc->hn_if_flags & IFF_PROMISC)) {
1396 /* do something here for Hyper-V */
1397 } else if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
1398 !(ifp->if_flags & IFF_PROMISC) &&
1399 sc->hn_if_flags & IFF_PROMISC) {
1400 /* do something here for Hyper-V */
1403 hn_ifinit_locked(sc);
1405 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1410 sc->temp_unusable = FALSE;
1412 sc->hn_if_flags = ifp->if_flags;
1418 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1419 if (mask & IFCAP_TXCSUM) {
1420 ifp->if_capenable ^= IFCAP_TXCSUM;
1421 if (ifp->if_capenable & IFCAP_TXCSUM) {
1423 sc->hn_tx_ring[0].hn_csum_assist;
1426 ~sc->hn_tx_ring[0].hn_csum_assist;
1430 if (mask & IFCAP_RXCSUM)
1431 ifp->if_capenable ^= IFCAP_RXCSUM;
1433 if (mask & IFCAP_LRO)
1434 ifp->if_capenable ^= IFCAP_LRO;
1436 if (mask & IFCAP_TSO4) {
1437 ifp->if_capenable ^= IFCAP_TSO4;
1438 if (ifp->if_capenable & IFCAP_TSO4)
1439 ifp->if_hwassist |= CSUM_IP_TSO;
1441 ifp->if_hwassist &= ~CSUM_IP_TSO;
1444 if (mask & IFCAP_TSO6) {
1445 ifp->if_capenable ^= IFCAP_TSO6;
1446 if (ifp->if_capenable & IFCAP_TSO6)
1447 ifp->if_hwassist |= CSUM_IP6_TSO;
1449 ifp->if_hwassist &= ~CSUM_IP6_TSO;
1458 /* Fixme: Multicast mode? */
1459 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1461 netvsc_setmulti(sc);
1470 error = ifmedia_ioctl(ifp, ifr, &sc->hn_media, cmd);
1473 error = ether_ioctl(ifp, cmd, data);
1484 hn_stop(hn_softc_t *sc)
1488 struct hv_device *device_ctx = vmbus_get_devctx(sc->hn_dev);
1493 printf(" Closing Device ...\n");
1495 atomic_clear_int(&ifp->if_drv_flags,
1496 (IFF_DRV_RUNNING | IFF_DRV_OACTIVE));
1497 if_link_state_change(ifp, LINK_STATE_DOWN);
1498 sc->hn_initdone = 0;
1500 ret = hv_rf_on_close(device_ctx);
1504 * FreeBSD transmit entry point
1507 hn_start(struct ifnet *ifp)
1509 struct hn_softc *sc = ifp->if_softc;
1510 struct hn_tx_ring *txr = &sc->hn_tx_ring[0];
1512 if (txr->hn_sched_tx)
1515 if (mtx_trylock(&txr->hn_tx_lock)) {
1518 sched = hn_start_locked(txr, txr->hn_direct_tx_size);
1519 mtx_unlock(&txr->hn_tx_lock);
1524 taskqueue_enqueue(txr->hn_tx_taskq, &txr->hn_start_task);
1528 hn_start_txeof(struct hn_tx_ring *txr)
1530 struct hn_softc *sc = txr->hn_sc;
1531 struct ifnet *ifp = sc->hn_ifp;
1533 KASSERT(txr == &sc->hn_tx_ring[0], ("not the first TX ring"));
1535 if (txr->hn_sched_tx)
1538 if (mtx_trylock(&txr->hn_tx_lock)) {
1541 atomic_clear_int(&ifp->if_drv_flags, IFF_DRV_OACTIVE);
1542 sched = hn_start_locked(txr, txr->hn_direct_tx_size);
1543 mtx_unlock(&txr->hn_tx_lock);
1545 taskqueue_enqueue(txr->hn_tx_taskq,
1546 &txr->hn_start_task);
1551 * Release the OACTIVE earlier, with the hope, that
1552 * others could catch up. The task will clear the
1553 * flag again with the hn_tx_lock to avoid possible
1556 atomic_clear_int(&ifp->if_drv_flags, IFF_DRV_OACTIVE);
1557 taskqueue_enqueue(txr->hn_tx_taskq, &txr->hn_txeof_task);
1565 hn_ifinit_locked(hn_softc_t *sc)
1568 struct hv_device *device_ctx = vmbus_get_devctx(sc->hn_dev);
1573 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1577 hv_promisc_mode = 1;
1579 ret = hv_rf_on_open(device_ctx);
1583 sc->hn_initdone = 1;
1585 atomic_clear_int(&ifp->if_drv_flags, IFF_DRV_OACTIVE);
1586 atomic_set_int(&ifp->if_drv_flags, IFF_DRV_RUNNING);
1587 if_link_state_change(ifp, LINK_STATE_UP);
1594 hn_ifinit(void *xsc)
1596 hn_softc_t *sc = xsc;
1599 if (sc->temp_unusable) {
1603 sc->temp_unusable = TRUE;
1606 hn_ifinit_locked(sc);
1609 sc->temp_unusable = FALSE;
1618 hn_watchdog(struct ifnet *ifp)
1623 printf("hn%d: watchdog timeout -- resetting\n", sc->hn_unit);
1624 hn_ifinit(sc); /*???*/
1629 #if __FreeBSD_version >= 1100099
1632 hn_lro_lenlim_sysctl(SYSCTL_HANDLER_ARGS)
1634 struct hn_softc *sc = arg1;
1635 unsigned int lenlim;
1638 lenlim = sc->hn_rx_ring[0].hn_lro.lro_length_lim;
1639 error = sysctl_handle_int(oidp, &lenlim, 0, req);
1640 if (error || req->newptr == NULL)
1643 if (lenlim < HN_LRO_LENLIM_MIN(sc->hn_ifp) ||
1644 lenlim > TCP_LRO_LENGTH_MAX)
1648 for (i = 0; i < sc->hn_rx_ring_cnt; ++i)
1649 sc->hn_rx_ring[i].hn_lro.lro_length_lim = lenlim;
1655 hn_lro_ackcnt_sysctl(SYSCTL_HANDLER_ARGS)
1657 struct hn_softc *sc = arg1;
1658 int ackcnt, error, i;
1661 * lro_ackcnt_lim is append count limit,
1662 * +1 to turn it into aggregation limit.
1664 ackcnt = sc->hn_rx_ring[0].hn_lro.lro_ackcnt_lim + 1;
1665 error = sysctl_handle_int(oidp, &ackcnt, 0, req);
1666 if (error || req->newptr == NULL)
1669 if (ackcnt < 2 || ackcnt > (TCP_LRO_ACKCNT_MAX + 1))
1673 * Convert aggregation limit back to append
1678 for (i = 0; i < sc->hn_rx_ring_cnt; ++i)
1679 sc->hn_rx_ring[i].hn_lro.lro_ackcnt_lim = ackcnt;
1687 hn_trust_hcsum_sysctl(SYSCTL_HANDLER_ARGS)
1689 struct hn_softc *sc = arg1;
1694 if (sc->hn_rx_ring[0].hn_trust_hcsum & hcsum)
1697 error = sysctl_handle_int(oidp, &on, 0, req);
1698 if (error || req->newptr == NULL)
1702 for (i = 0; i < sc->hn_rx_ring_cnt; ++i) {
1703 struct hn_rx_ring *rxr = &sc->hn_rx_ring[i];
1706 rxr->hn_trust_hcsum |= hcsum;
1708 rxr->hn_trust_hcsum &= ~hcsum;
1715 hn_tx_chimney_size_sysctl(SYSCTL_HANDLER_ARGS)
1717 struct hn_softc *sc = arg1;
1718 int chimney_size, error;
1720 chimney_size = sc->hn_tx_ring[0].hn_tx_chimney_size;
1721 error = sysctl_handle_int(oidp, &chimney_size, 0, req);
1722 if (error || req->newptr == NULL)
1725 if (chimney_size > sc->hn_tx_chimney_max || chimney_size <= 0)
1728 hn_set_tx_chimney_size(sc, chimney_size);
1733 hn_rx_stat_ulong_sysctl(SYSCTL_HANDLER_ARGS)
1735 struct hn_softc *sc = arg1;
1736 int ofs = arg2, i, error;
1737 struct hn_rx_ring *rxr;
1741 for (i = 0; i < sc->hn_rx_ring_cnt; ++i) {
1742 rxr = &sc->hn_rx_ring[i];
1743 stat += *((u_long *)((uint8_t *)rxr + ofs));
1746 error = sysctl_handle_long(oidp, &stat, 0, req);
1747 if (error || req->newptr == NULL)
1750 /* Zero out this stat. */
1751 for (i = 0; i < sc->hn_rx_ring_cnt; ++i) {
1752 rxr = &sc->hn_rx_ring[i];
1753 *((u_long *)((uint8_t *)rxr + ofs)) = 0;
1759 hn_rx_stat_u64_sysctl(SYSCTL_HANDLER_ARGS)
1761 struct hn_softc *sc = arg1;
1762 int ofs = arg2, i, error;
1763 struct hn_rx_ring *rxr;
1767 for (i = 0; i < sc->hn_rx_ring_cnt; ++i) {
1768 rxr = &sc->hn_rx_ring[i];
1769 stat += *((uint64_t *)((uint8_t *)rxr + ofs));
1772 error = sysctl_handle_64(oidp, &stat, 0, req);
1773 if (error || req->newptr == NULL)
1776 /* Zero out this stat. */
1777 for (i = 0; i < sc->hn_rx_ring_cnt; ++i) {
1778 rxr = &sc->hn_rx_ring[i];
1779 *((uint64_t *)((uint8_t *)rxr + ofs)) = 0;
1785 hn_tx_stat_ulong_sysctl(SYSCTL_HANDLER_ARGS)
1787 struct hn_softc *sc = arg1;
1788 int ofs = arg2, i, error;
1789 struct hn_tx_ring *txr;
1793 for (i = 0; i < sc->hn_tx_ring_cnt; ++i) {
1794 txr = &sc->hn_tx_ring[i];
1795 stat += *((u_long *)((uint8_t *)txr + ofs));
1798 error = sysctl_handle_long(oidp, &stat, 0, req);
1799 if (error || req->newptr == NULL)
1802 /* Zero out this stat. */
1803 for (i = 0; i < sc->hn_tx_ring_cnt; ++i) {
1804 txr = &sc->hn_tx_ring[i];
1805 *((u_long *)((uint8_t *)txr + ofs)) = 0;
1811 hn_tx_conf_int_sysctl(SYSCTL_HANDLER_ARGS)
1813 struct hn_softc *sc = arg1;
1814 int ofs = arg2, i, error, conf;
1815 struct hn_tx_ring *txr;
1817 txr = &sc->hn_tx_ring[0];
1818 conf = *((int *)((uint8_t *)txr + ofs));
1820 error = sysctl_handle_int(oidp, &conf, 0, req);
1821 if (error || req->newptr == NULL)
1825 for (i = 0; i < sc->hn_tx_ring_cnt; ++i) {
1826 txr = &sc->hn_tx_ring[i];
1827 *((int *)((uint8_t *)txr + ofs)) = conf;
1835 hn_check_iplen(const struct mbuf *m, int hoff)
1837 const struct ip *ip;
1838 int len, iphlen, iplen;
1839 const struct tcphdr *th;
1840 int thoff; /* TCP data offset */
1842 len = hoff + sizeof(struct ip);
1844 /* The packet must be at least the size of an IP header. */
1845 if (m->m_pkthdr.len < len)
1846 return IPPROTO_DONE;
1848 /* The fixed IP header must reside completely in the first mbuf. */
1850 return IPPROTO_DONE;
1852 ip = mtodo(m, hoff);
1854 /* Bound check the packet's stated IP header length. */
1855 iphlen = ip->ip_hl << 2;
1856 if (iphlen < sizeof(struct ip)) /* minimum header length */
1857 return IPPROTO_DONE;
1859 /* The full IP header must reside completely in the one mbuf. */
1860 if (m->m_len < hoff + iphlen)
1861 return IPPROTO_DONE;
1863 iplen = ntohs(ip->ip_len);
1866 * Check that the amount of data in the buffers is as
1867 * at least much as the IP header would have us expect.
1869 if (m->m_pkthdr.len < hoff + iplen)
1870 return IPPROTO_DONE;
1873 * Ignore IP fragments.
1875 if (ntohs(ip->ip_off) & (IP_OFFMASK | IP_MF))
1876 return IPPROTO_DONE;
1879 * The TCP/IP or UDP/IP header must be entirely contained within
1880 * the first fragment of a packet.
1884 if (iplen < iphlen + sizeof(struct tcphdr))
1885 return IPPROTO_DONE;
1886 if (m->m_len < hoff + iphlen + sizeof(struct tcphdr))
1887 return IPPROTO_DONE;
1888 th = (const struct tcphdr *)((const uint8_t *)ip + iphlen);
1889 thoff = th->th_off << 2;
1890 if (thoff < sizeof(struct tcphdr) || thoff + iphlen > iplen)
1891 return IPPROTO_DONE;
1892 if (m->m_len < hoff + iphlen + thoff)
1893 return IPPROTO_DONE;
1896 if (iplen < iphlen + sizeof(struct udphdr))
1897 return IPPROTO_DONE;
1898 if (m->m_len < hoff + iphlen + sizeof(struct udphdr))
1899 return IPPROTO_DONE;
1903 return IPPROTO_DONE;
1910 hn_dma_map_paddr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1912 bus_addr_t *paddr = arg;
1917 KASSERT(nseg == 1, ("too many segments %d!", nseg));
1918 *paddr = segs->ds_addr;
1922 hn_create_rx_data(struct hn_softc *sc)
1924 struct sysctl_oid_list *child;
1925 struct sysctl_ctx_list *ctx;
1926 device_t dev = sc->hn_dev;
1927 #if defined(INET) || defined(INET6)
1928 #if __FreeBSD_version >= 1100095
1934 sc->hn_rx_ring_cnt = 1; /* TODO: vRSS */
1935 sc->hn_rx_ring = malloc(sizeof(struct hn_rx_ring) * sc->hn_rx_ring_cnt,
1936 M_NETVSC, M_WAITOK | M_ZERO);
1938 #if defined(INET) || defined(INET6)
1939 #if __FreeBSD_version >= 1100095
1940 lroent_cnt = hn_lro_entry_count;
1941 if (lroent_cnt < TCP_LRO_ENTRIES)
1942 lroent_cnt = TCP_LRO_ENTRIES;
1943 device_printf(dev, "LRO: entry count %d\n", lroent_cnt);
1945 #endif /* INET || INET6 */
1947 for (i = 0; i < sc->hn_rx_ring_cnt; ++i) {
1948 struct hn_rx_ring *rxr = &sc->hn_rx_ring[i];
1950 if (hn_trust_hosttcp)
1951 rxr->hn_trust_hcsum |= HN_TRUST_HCSUM_TCP;
1952 if (hn_trust_hostudp)
1953 rxr->hn_trust_hcsum |= HN_TRUST_HCSUM_UDP;
1954 if (hn_trust_hostip)
1955 rxr->hn_trust_hcsum |= HN_TRUST_HCSUM_IP;
1960 #if defined(INET) || defined(INET6)
1961 #if __FreeBSD_version >= 1100095
1962 tcp_lro_init_args(&rxr->hn_lro, sc->hn_ifp, lroent_cnt, 0);
1964 tcp_lro_init(&rxr->hn_lro);
1965 rxr->hn_lro.ifp = sc->hn_ifp;
1967 #if __FreeBSD_version >= 1100099
1968 rxr->hn_lro.lro_length_lim = HN_LRO_LENLIM_DEF;
1969 rxr->hn_lro.lro_ackcnt_lim = HN_LRO_ACKCNT_DEF;
1971 #endif /* INET || INET6 */
1974 ctx = device_get_sysctl_ctx(dev);
1975 child = SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
1977 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "lro_queued",
1978 CTLTYPE_U64 | CTLFLAG_RW, sc,
1979 __offsetof(struct hn_rx_ring, hn_lro.lro_queued),
1980 hn_rx_stat_u64_sysctl, "LU", "LRO queued");
1981 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "lro_flushed",
1982 CTLTYPE_U64 | CTLFLAG_RW, sc,
1983 __offsetof(struct hn_rx_ring, hn_lro.lro_flushed),
1984 hn_rx_stat_u64_sysctl, "LU", "LRO flushed");
1985 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "lro_tried",
1986 CTLTYPE_ULONG | CTLFLAG_RW, sc,
1987 __offsetof(struct hn_rx_ring, hn_lro_tried),
1988 hn_rx_stat_ulong_sysctl, "LU", "# of LRO tries");
1989 #if __FreeBSD_version >= 1100099
1990 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "lro_length_lim",
1991 CTLTYPE_UINT | CTLFLAG_RW, sc, 0, hn_lro_lenlim_sysctl, "IU",
1992 "Max # of data bytes to be aggregated by LRO");
1993 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "lro_ackcnt_lim",
1994 CTLTYPE_INT | CTLFLAG_RW, sc, 0, hn_lro_ackcnt_sysctl, "I",
1995 "Max # of ACKs to be aggregated by LRO");
1997 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "trust_hosttcp",
1998 CTLTYPE_INT | CTLFLAG_RW, sc, HN_TRUST_HCSUM_TCP,
1999 hn_trust_hcsum_sysctl, "I",
2000 "Trust tcp segement verification on host side, "
2001 "when csum info is missing");
2002 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "trust_hostudp",
2003 CTLTYPE_INT | CTLFLAG_RW, sc, HN_TRUST_HCSUM_UDP,
2004 hn_trust_hcsum_sysctl, "I",
2005 "Trust udp datagram verification on host side, "
2006 "when csum info is missing");
2007 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "trust_hostip",
2008 CTLTYPE_INT | CTLFLAG_RW, sc, HN_TRUST_HCSUM_IP,
2009 hn_trust_hcsum_sysctl, "I",
2010 "Trust ip packet verification on host side, "
2011 "when csum info is missing");
2012 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "csum_ip",
2013 CTLTYPE_ULONG | CTLFLAG_RW, sc,
2014 __offsetof(struct hn_rx_ring, hn_csum_ip),
2015 hn_rx_stat_ulong_sysctl, "LU", "RXCSUM IP");
2016 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "csum_tcp",
2017 CTLTYPE_ULONG | CTLFLAG_RW, sc,
2018 __offsetof(struct hn_rx_ring, hn_csum_tcp),
2019 hn_rx_stat_ulong_sysctl, "LU", "RXCSUM TCP");
2020 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "csum_udp",
2021 CTLTYPE_ULONG | CTLFLAG_RW, sc,
2022 __offsetof(struct hn_rx_ring, hn_csum_udp),
2023 hn_rx_stat_ulong_sysctl, "LU", "RXCSUM UDP");
2024 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "csum_trusted",
2025 CTLTYPE_ULONG | CTLFLAG_RW, sc,
2026 __offsetof(struct hn_rx_ring, hn_csum_trusted),
2027 hn_rx_stat_ulong_sysctl, "LU",
2028 "# of packets that we trust host's csum verification");
2029 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "small_pkts",
2030 CTLTYPE_ULONG | CTLFLAG_RW, sc,
2031 __offsetof(struct hn_rx_ring, hn_small_pkts),
2032 hn_rx_stat_ulong_sysctl, "LU", "# of small packets received");
2036 hn_destroy_rx_data(struct hn_softc *sc)
2038 #if defined(INET) || defined(INET6)
2042 if (sc->hn_rx_ring_cnt == 0)
2045 #if defined(INET) || defined(INET6)
2046 for (i = 0; i < sc->hn_rx_ring_cnt; ++i)
2047 tcp_lro_free(&sc->hn_rx_ring[i].hn_lro);
2049 free(sc->hn_rx_ring, M_NETVSC);
2050 sc->hn_rx_ring = NULL;
2052 sc->hn_rx_ring_cnt = 0;
2056 hn_create_tx_ring(struct hn_softc *sc, int id)
2058 struct hn_tx_ring *txr = &sc->hn_tx_ring[id];
2059 bus_dma_tag_t parent_dtag;
2064 mtx_init(&txr->hn_txlist_spin, "hn txlist", NULL, MTX_SPIN);
2065 mtx_init(&txr->hn_tx_lock, "hn tx", NULL, MTX_DEF);
2067 txr->hn_txdesc_cnt = HN_TX_DESC_CNT;
2068 txr->hn_txdesc = malloc(sizeof(struct hn_txdesc) * txr->hn_txdesc_cnt,
2069 M_NETVSC, M_WAITOK | M_ZERO);
2070 SLIST_INIT(&txr->hn_txlist);
2072 txr->hn_tx_taskq = sc->hn_tx_taskq;
2073 TASK_INIT(&txr->hn_start_task, 0, hn_start_taskfunc, txr);
2074 TASK_INIT(&txr->hn_txeof_task, 0, hn_txeof_taskfunc, txr);
2076 txr->hn_direct_tx_size = hn_direct_tx_size;
2077 if (hv_vmbus_protocal_version >= HV_VMBUS_VERSION_WIN8_1)
2078 txr->hn_csum_assist = HN_CSUM_ASSIST;
2080 txr->hn_csum_assist = HN_CSUM_ASSIST_WIN8;
2083 * Always schedule transmission instead of trying to do direct
2084 * transmission. This one gives the best performance so far.
2086 txr->hn_sched_tx = 1;
2088 parent_dtag = bus_get_dma_tag(sc->hn_dev);
2090 /* DMA tag for RNDIS messages. */
2091 error = bus_dma_tag_create(parent_dtag, /* parent */
2092 HN_RNDIS_MSG_ALIGN, /* alignment */
2093 HN_RNDIS_MSG_BOUNDARY, /* boundary */
2094 BUS_SPACE_MAXADDR, /* lowaddr */
2095 BUS_SPACE_MAXADDR, /* highaddr */
2096 NULL, NULL, /* filter, filterarg */
2097 HN_RNDIS_MSG_LEN, /* maxsize */
2099 HN_RNDIS_MSG_LEN, /* maxsegsize */
2101 NULL, /* lockfunc */
2102 NULL, /* lockfuncarg */
2103 &txr->hn_tx_rndis_dtag);
2105 device_printf(sc->hn_dev, "failed to create rndis dmatag\n");
2109 /* DMA tag for data. */
2110 error = bus_dma_tag_create(parent_dtag, /* parent */
2112 HN_TX_DATA_BOUNDARY, /* boundary */
2113 BUS_SPACE_MAXADDR, /* lowaddr */
2114 BUS_SPACE_MAXADDR, /* highaddr */
2115 NULL, NULL, /* filter, filterarg */
2116 HN_TX_DATA_MAXSIZE, /* maxsize */
2117 HN_TX_DATA_SEGCNT_MAX, /* nsegments */
2118 HN_TX_DATA_SEGSIZE, /* maxsegsize */
2120 NULL, /* lockfunc */
2121 NULL, /* lockfuncarg */
2122 &txr->hn_tx_data_dtag);
2124 device_printf(sc->hn_dev, "failed to create data dmatag\n");
2128 for (i = 0; i < txr->hn_txdesc_cnt; ++i) {
2129 struct hn_txdesc *txd = &txr->hn_txdesc[i];
2134 * Allocate and load RNDIS messages.
2136 error = bus_dmamem_alloc(txr->hn_tx_rndis_dtag,
2137 (void **)&txd->rndis_msg,
2138 BUS_DMA_WAITOK | BUS_DMA_COHERENT,
2139 &txd->rndis_msg_dmap);
2141 device_printf(sc->hn_dev,
2142 "failed to allocate rndis_msg, %d\n", i);
2146 error = bus_dmamap_load(txr->hn_tx_rndis_dtag,
2147 txd->rndis_msg_dmap,
2148 txd->rndis_msg, HN_RNDIS_MSG_LEN,
2149 hn_dma_map_paddr, &txd->rndis_msg_paddr,
2152 device_printf(sc->hn_dev,
2153 "failed to load rndis_msg, %d\n", i);
2154 bus_dmamem_free(txr->hn_tx_rndis_dtag,
2155 txd->rndis_msg, txd->rndis_msg_dmap);
2159 /* DMA map for TX data. */
2160 error = bus_dmamap_create(txr->hn_tx_data_dtag, 0,
2163 device_printf(sc->hn_dev,
2164 "failed to allocate tx data dmamap\n");
2165 bus_dmamap_unload(txr->hn_tx_rndis_dtag,
2166 txd->rndis_msg_dmap);
2167 bus_dmamem_free(txr->hn_tx_rndis_dtag,
2168 txd->rndis_msg, txd->rndis_msg_dmap);
2172 /* All set, put it to list */
2173 txd->flags |= HN_TXD_FLAG_ONLIST;
2174 SLIST_INSERT_HEAD(&txr->hn_txlist, txd, link);
2176 txr->hn_txdesc_avail = txr->hn_txdesc_cnt;
2178 if (sc->hn_tx_sysctl_tree != NULL) {
2179 struct sysctl_oid_list *child;
2180 struct sysctl_ctx_list *ctx;
2184 * Create per TX ring sysctl tree:
2185 * dev.hn.UNIT.tx.RINGID
2187 ctx = device_get_sysctl_ctx(sc->hn_dev);
2188 child = SYSCTL_CHILDREN(sc->hn_tx_sysctl_tree);
2190 snprintf(name, sizeof(name), "%d", id);
2191 txr->hn_tx_sysctl_tree = SYSCTL_ADD_NODE(ctx, child, OID_AUTO,
2192 name, CTLFLAG_RD, 0, "");
2194 if (txr->hn_tx_sysctl_tree != NULL) {
2195 child = SYSCTL_CHILDREN(txr->hn_tx_sysctl_tree);
2197 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "txdesc_avail",
2198 CTLFLAG_RD, &txr->hn_txdesc_avail, 0,
2199 "# of available TX descs");
2207 hn_destroy_tx_ring(struct hn_tx_ring *txr)
2209 struct hn_txdesc *txd;
2211 if (txr->hn_txdesc == NULL)
2214 while ((txd = SLIST_FIRST(&txr->hn_txlist)) != NULL) {
2215 KASSERT(txd->m == NULL, ("still has mbuf installed"));
2216 KASSERT((txd->flags & HN_TXD_FLAG_DMAMAP) == 0,
2217 ("still dma mapped"));
2218 SLIST_REMOVE_HEAD(&txr->hn_txlist, link);
2220 bus_dmamap_unload(txr->hn_tx_rndis_dtag,
2221 txd->rndis_msg_dmap);
2222 bus_dmamem_free(txr->hn_tx_rndis_dtag,
2223 txd->rndis_msg, txd->rndis_msg_dmap);
2225 bus_dmamap_destroy(txr->hn_tx_data_dtag, txd->data_dmap);
2228 if (txr->hn_tx_data_dtag != NULL)
2229 bus_dma_tag_destroy(txr->hn_tx_data_dtag);
2230 if (txr->hn_tx_rndis_dtag != NULL)
2231 bus_dma_tag_destroy(txr->hn_tx_rndis_dtag);
2232 free(txr->hn_txdesc, M_NETVSC);
2233 txr->hn_txdesc = NULL;
2235 mtx_destroy(&txr->hn_txlist_spin);
2236 mtx_destroy(&txr->hn_tx_lock);
2240 hn_create_tx_data(struct hn_softc *sc)
2242 struct sysctl_oid_list *child;
2243 struct sysctl_ctx_list *ctx;
2246 sc->hn_tx_ring_cnt = 1; /* TODO: vRSS */
2247 sc->hn_tx_ring = malloc(sizeof(struct hn_tx_ring) * sc->hn_tx_ring_cnt,
2248 M_NETVSC, M_WAITOK | M_ZERO);
2250 ctx = device_get_sysctl_ctx(sc->hn_dev);
2251 child = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->hn_dev));
2253 /* Create dev.hn.UNIT.tx sysctl tree */
2254 sc->hn_tx_sysctl_tree = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, "tx",
2257 for (i = 0; i < sc->hn_tx_ring_cnt; ++i) {
2260 error = hn_create_tx_ring(sc, i);
2265 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "no_txdescs",
2266 CTLTYPE_ULONG | CTLFLAG_RW, sc,
2267 __offsetof(struct hn_tx_ring, hn_no_txdescs),
2268 hn_tx_stat_ulong_sysctl, "LU", "# of times short of TX descs");
2269 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "send_failed",
2270 CTLTYPE_ULONG | CTLFLAG_RW, sc,
2271 __offsetof(struct hn_tx_ring, hn_send_failed),
2272 hn_tx_stat_ulong_sysctl, "LU", "# of hyper-v sending failure");
2273 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "txdma_failed",
2274 CTLTYPE_ULONG | CTLFLAG_RW, sc,
2275 __offsetof(struct hn_tx_ring, hn_txdma_failed),
2276 hn_tx_stat_ulong_sysctl, "LU", "# of TX DMA failure");
2277 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "tx_collapsed",
2278 CTLTYPE_ULONG | CTLFLAG_RW, sc,
2279 __offsetof(struct hn_tx_ring, hn_tx_collapsed),
2280 hn_tx_stat_ulong_sysctl, "LU", "# of TX mbuf collapsed");
2281 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "tx_chimney",
2282 CTLTYPE_ULONG | CTLFLAG_RW, sc,
2283 __offsetof(struct hn_tx_ring, hn_tx_chimney),
2284 hn_tx_stat_ulong_sysctl, "LU", "# of chimney send");
2285 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "txdesc_cnt",
2286 CTLFLAG_RD, &sc->hn_tx_ring[0].hn_txdesc_cnt, 0,
2287 "# of total TX descs");
2288 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "tx_chimney_max",
2289 CTLFLAG_RD, &sc->hn_tx_chimney_max, 0,
2290 "Chimney send packet size upper boundary");
2291 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "tx_chimney_size",
2292 CTLTYPE_INT | CTLFLAG_RW, sc, 0, hn_tx_chimney_size_sysctl,
2293 "I", "Chimney send packet size limit");
2294 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "direct_tx_size",
2295 CTLTYPE_INT | CTLFLAG_RW, sc,
2296 __offsetof(struct hn_tx_ring, hn_direct_tx_size),
2297 hn_tx_conf_int_sysctl, "I",
2298 "Size of the packet for direct transmission");
2299 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "sched_tx",
2300 CTLTYPE_INT | CTLFLAG_RW, sc,
2301 __offsetof(struct hn_tx_ring, hn_sched_tx),
2302 hn_tx_conf_int_sysctl, "I",
2303 "Always schedule transmission "
2304 "instead of doing direct transmission");
2310 hn_set_tx_chimney_size(struct hn_softc *sc, int chimney_size)
2315 for (i = 0; i < sc->hn_tx_ring_cnt; ++i)
2316 sc->hn_tx_ring[i].hn_tx_chimney_size = chimney_size;
2321 hn_destroy_tx_data(struct hn_softc *sc)
2325 if (sc->hn_tx_ring_cnt == 0)
2328 for (i = 0; i < sc->hn_tx_ring_cnt; ++i)
2329 hn_destroy_tx_ring(&sc->hn_tx_ring[i]);
2331 free(sc->hn_tx_ring, M_NETVSC);
2332 sc->hn_tx_ring = NULL;
2334 sc->hn_tx_ring_cnt = 0;
2338 hn_start_taskfunc(void *xtxr, int pending __unused)
2340 struct hn_tx_ring *txr = xtxr;
2342 mtx_lock(&txr->hn_tx_lock);
2343 hn_start_locked(txr, 0);
2344 mtx_unlock(&txr->hn_tx_lock);
2348 hn_txeof_taskfunc(void *xtxr, int pending __unused)
2350 struct hn_tx_ring *txr = xtxr;
2352 mtx_lock(&txr->hn_tx_lock);
2353 atomic_clear_int(&txr->hn_sc->hn_ifp->if_drv_flags, IFF_DRV_OACTIVE);
2354 hn_start_locked(txr, 0);
2355 mtx_unlock(&txr->hn_tx_lock);
2359 hn_stop_tx_tasks(struct hn_softc *sc)
2363 for (i = 0; i < sc->hn_tx_ring_cnt; ++i) {
2364 struct hn_tx_ring *txr = &sc->hn_tx_ring[i];
2366 taskqueue_drain(txr->hn_tx_taskq, &txr->hn_start_task);
2367 taskqueue_drain(txr->hn_tx_taskq, &txr->hn_txeof_task);
2372 hn_tx_taskq_create(void *arg __unused)
2374 if (!hn_share_tx_taskq)
2377 hn_tx_taskq = taskqueue_create("hn_tx", M_WAITOK,
2378 taskqueue_thread_enqueue, &hn_tx_taskq);
2379 taskqueue_start_threads(&hn_tx_taskq, 1, PI_NET, "hn tx");
2381 SYSINIT(hn_txtq_create, SI_SUB_DRIVERS, SI_ORDER_FIRST,
2382 hn_tx_taskq_create, NULL);
2385 hn_tx_taskq_destroy(void *arg __unused)
2387 if (hn_tx_taskq != NULL)
2388 taskqueue_free(hn_tx_taskq);
2390 SYSUNINIT(hn_txtq_destroy, SI_SUB_DRIVERS, SI_ORDER_FIRST,
2391 hn_tx_taskq_destroy, NULL);
2393 static device_method_t netvsc_methods[] = {
2394 /* Device interface */
2395 DEVMETHOD(device_probe, netvsc_probe),
2396 DEVMETHOD(device_attach, netvsc_attach),
2397 DEVMETHOD(device_detach, netvsc_detach),
2398 DEVMETHOD(device_shutdown, netvsc_shutdown),
2403 static driver_t netvsc_driver = {
2409 static devclass_t netvsc_devclass;
2411 DRIVER_MODULE(hn, vmbus, netvsc_driver, netvsc_devclass, 0, 0);
2412 MODULE_VERSION(hn, 1);
2413 MODULE_DEPEND(hn, vmbus, 1, 1, 1);