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"
122 #define hv_chan_rxr hv_chan_priv1
123 #define hv_chan_txr hv_chan_priv2
125 /* Short for Hyper-V network interface */
126 #define NETVSC_DEVNAME "hn"
129 * It looks like offset 0 of buf is reserved to hold the softc pointer.
130 * The sc pointer evidently not needed, and is not presently populated.
131 * The packet offset is where the netvsc_packet starts in the buffer.
133 #define HV_NV_SC_PTR_OFFSET_IN_BUF 0
134 #define HV_NV_PACKET_OFFSET_IN_BUF 16
136 /* YYY should get it from the underlying channel */
137 #define HN_TX_DESC_CNT 512
139 #define HN_LROENT_CNT_DEF 128
141 #define HN_RNDIS_MSG_LEN \
142 (sizeof(rndis_msg) + \
143 RNDIS_VLAN_PPI_SIZE + \
144 RNDIS_TSO_PPI_SIZE + \
146 #define HN_RNDIS_MSG_BOUNDARY PAGE_SIZE
147 #define HN_RNDIS_MSG_ALIGN CACHE_LINE_SIZE
149 #define HN_TX_DATA_BOUNDARY PAGE_SIZE
150 #define HN_TX_DATA_MAXSIZE IP_MAXPACKET
151 #define HN_TX_DATA_SEGSIZE PAGE_SIZE
152 #define HN_TX_DATA_SEGCNT_MAX \
153 (NETVSC_PACKET_MAXPAGE - HV_RF_NUM_TX_RESERVED_PAGE_BUFS)
155 #define HN_DIRECT_TX_SIZE_DEF 128
158 #ifndef HN_USE_TXDESC_BUFRING
159 SLIST_ENTRY(hn_txdesc) link;
162 struct hn_tx_ring *txr;
164 uint32_t flags; /* HN_TXD_FLAG_ */
165 netvsc_packet netvsc_pkt; /* XXX to be removed */
167 bus_dmamap_t data_dmap;
169 bus_addr_t rndis_msg_paddr;
170 rndis_msg *rndis_msg;
171 bus_dmamap_t rndis_msg_dmap;
174 #define HN_TXD_FLAG_ONLIST 0x1
175 #define HN_TXD_FLAG_DMAMAP 0x2
178 * Only enable UDP checksum offloading when it is on 2012R2 or
179 * later. UDP checksum offloading doesn't work on earlier
182 #define HN_CSUM_ASSIST_WIN8 (CSUM_IP | CSUM_TCP)
183 #define HN_CSUM_ASSIST (CSUM_IP | CSUM_UDP | CSUM_TCP)
185 #define HN_LRO_LENLIM_DEF (25 * ETHERMTU)
186 /* YYY 2*MTU is a bit rough, but should be good enough. */
187 #define HN_LRO_LENLIM_MIN(ifp) (2 * (ifp)->if_mtu)
189 #define HN_LRO_ACKCNT_DEF 1
192 * Be aware that this sleepable mutex will exhibit WITNESS errors when
193 * certain TCP and ARP code paths are taken. This appears to be a
194 * well-known condition, as all other drivers checked use a sleeping
195 * mutex to protect their transmit paths.
196 * Also Be aware that mutexes do not play well with semaphores, and there
197 * is a conflicting semaphore in a certain channel code path.
199 #define NV_LOCK_INIT(_sc, _name) \
200 mtx_init(&(_sc)->hn_lock, _name, MTX_NETWORK_LOCK, MTX_DEF)
201 #define NV_LOCK(_sc) mtx_lock(&(_sc)->hn_lock)
202 #define NV_LOCK_ASSERT(_sc) mtx_assert(&(_sc)->hn_lock, MA_OWNED)
203 #define NV_UNLOCK(_sc) mtx_unlock(&(_sc)->hn_lock)
204 #define NV_LOCK_DESTROY(_sc) mtx_destroy(&(_sc)->hn_lock)
211 int hv_promisc_mode = 0; /* normal mode by default */
213 SYSCTL_NODE(_hw, OID_AUTO, hn, CTLFLAG_RD, NULL, "Hyper-V network interface");
215 /* Trust tcp segements verification on host side. */
216 static int hn_trust_hosttcp = 1;
217 SYSCTL_INT(_hw_hn, OID_AUTO, trust_hosttcp, CTLFLAG_RDTUN,
218 &hn_trust_hosttcp, 0,
219 "Trust tcp segement verification on host side, "
220 "when csum info is missing (global setting)");
222 /* Trust udp datagrams verification on host side. */
223 static int hn_trust_hostudp = 1;
224 SYSCTL_INT(_hw_hn, OID_AUTO, trust_hostudp, CTLFLAG_RDTUN,
225 &hn_trust_hostudp, 0,
226 "Trust udp datagram verification on host side, "
227 "when csum info is missing (global setting)");
229 /* Trust ip packets verification on host side. */
230 static int hn_trust_hostip = 1;
231 SYSCTL_INT(_hw_hn, OID_AUTO, trust_hostip, CTLFLAG_RDTUN,
233 "Trust ip packet verification on host side, "
234 "when csum info is missing (global setting)");
236 #if __FreeBSD_version >= 1100045
237 /* Limit TSO burst size */
238 static int hn_tso_maxlen = 0;
239 SYSCTL_INT(_hw_hn, OID_AUTO, tso_maxlen, CTLFLAG_RDTUN,
240 &hn_tso_maxlen, 0, "TSO burst limit");
243 /* Limit chimney send size */
244 static int hn_tx_chimney_size = 0;
245 SYSCTL_INT(_hw_hn, OID_AUTO, tx_chimney_size, CTLFLAG_RDTUN,
246 &hn_tx_chimney_size, 0, "Chimney send packet size limit");
248 /* Limit the size of packet for direct transmission */
249 static int hn_direct_tx_size = HN_DIRECT_TX_SIZE_DEF;
250 SYSCTL_INT(_hw_hn, OID_AUTO, direct_tx_size, CTLFLAG_RDTUN,
251 &hn_direct_tx_size, 0, "Size of the packet for direct transmission");
253 #if defined(INET) || defined(INET6)
254 #if __FreeBSD_version >= 1100095
255 static int hn_lro_entry_count = HN_LROENT_CNT_DEF;
256 SYSCTL_INT(_hw_hn, OID_AUTO, lro_entry_count, CTLFLAG_RDTUN,
257 &hn_lro_entry_count, 0, "LRO entry count");
261 static int hn_share_tx_taskq = 0;
262 SYSCTL_INT(_hw_hn, OID_AUTO, share_tx_taskq, CTLFLAG_RDTUN,
263 &hn_share_tx_taskq, 0, "Enable shared TX taskqueue");
265 static struct taskqueue *hn_tx_taskq;
267 #ifndef HN_USE_TXDESC_BUFRING
268 static int hn_use_txdesc_bufring = 0;
270 static int hn_use_txdesc_bufring = 1;
272 SYSCTL_INT(_hw_hn, OID_AUTO, use_txdesc_bufring, CTLFLAG_RD,
273 &hn_use_txdesc_bufring, 0, "Use buf_ring for TX descriptors");
275 static int hn_bind_tx_taskq = -1;
276 SYSCTL_INT(_hw_hn, OID_AUTO, bind_tx_taskq, CTLFLAG_RDTUN,
277 &hn_bind_tx_taskq, 0, "Bind TX taskqueue to the specified cpu");
279 static int hn_use_if_start = 1;
280 SYSCTL_INT(_hw_hn, OID_AUTO, use_if_start, CTLFLAG_RDTUN,
281 &hn_use_if_start, 0, "Use if_start TX method");
284 * Forward declarations
286 static void hn_stop(hn_softc_t *sc);
287 static void hn_ifinit_locked(hn_softc_t *sc);
288 static void hn_ifinit(void *xsc);
289 static int hn_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
290 static int hn_start_locked(struct hn_tx_ring *txr, int len);
291 static void hn_start(struct ifnet *ifp);
292 static void hn_start_txeof(struct hn_tx_ring *);
293 static int hn_ifmedia_upd(struct ifnet *ifp);
294 static void hn_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr);
295 #if __FreeBSD_version >= 1100099
296 static int hn_lro_lenlim_sysctl(SYSCTL_HANDLER_ARGS);
297 static int hn_lro_ackcnt_sysctl(SYSCTL_HANDLER_ARGS);
299 static int hn_trust_hcsum_sysctl(SYSCTL_HANDLER_ARGS);
300 static int hn_tx_chimney_size_sysctl(SYSCTL_HANDLER_ARGS);
301 #if __FreeBSD_version < 1100095
302 static int hn_rx_stat_int_sysctl(SYSCTL_HANDLER_ARGS);
304 static int hn_rx_stat_u64_sysctl(SYSCTL_HANDLER_ARGS);
306 static int hn_rx_stat_ulong_sysctl(SYSCTL_HANDLER_ARGS);
307 static int hn_tx_stat_ulong_sysctl(SYSCTL_HANDLER_ARGS);
308 static int hn_tx_conf_int_sysctl(SYSCTL_HANDLER_ARGS);
309 static int hn_check_iplen(const struct mbuf *, int);
310 static int hn_create_tx_ring(struct hn_softc *, int);
311 static void hn_destroy_tx_ring(struct hn_tx_ring *);
312 static int hn_create_tx_data(struct hn_softc *);
313 static void hn_destroy_tx_data(struct hn_softc *);
314 static void hn_start_taskfunc(void *, int);
315 static void hn_start_txeof_taskfunc(void *, int);
316 static void hn_stop_tx_tasks(struct hn_softc *);
317 static int hn_encap(struct hn_tx_ring *, struct hn_txdesc *, struct mbuf **);
318 static void hn_create_rx_data(struct hn_softc *sc);
319 static void hn_destroy_rx_data(struct hn_softc *sc);
320 static void hn_set_tx_chimney_size(struct hn_softc *, int);
322 static int hn_transmit(struct ifnet *, struct mbuf *);
323 static void hn_xmit_qflush(struct ifnet *);
324 static int hn_xmit(struct hn_tx_ring *, int);
325 static void hn_xmit_txeof(struct hn_tx_ring *);
326 static void hn_xmit_taskfunc(void *, int);
327 static void hn_xmit_txeof_taskfunc(void *, int);
330 hn_ifmedia_upd(struct ifnet *ifp __unused)
337 hn_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
339 struct hn_softc *sc = ifp->if_softc;
341 ifmr->ifm_status = IFM_AVALID;
342 ifmr->ifm_active = IFM_ETHER;
344 if (!sc->hn_carrier) {
345 ifmr->ifm_active |= IFM_NONE;
348 ifmr->ifm_status |= IFM_ACTIVE;
349 ifmr->ifm_active |= IFM_10G_T | IFM_FDX;
352 /* {F8615163-DF3E-46c5-913F-F2D2F965ED0E} */
353 static const hv_guid g_net_vsc_device_type = {
354 .data = {0x63, 0x51, 0x61, 0xF8, 0x3E, 0xDF, 0xc5, 0x46,
355 0x91, 0x3F, 0xF2, 0xD2, 0xF9, 0x65, 0xED, 0x0E}
359 * Standard probe entry point.
363 netvsc_probe(device_t dev)
367 p = vmbus_get_type(dev);
368 if (!memcmp(p, &g_net_vsc_device_type.data, sizeof(hv_guid))) {
369 device_set_desc(dev, "Synthetic Network Interface");
371 printf("Netvsc probe... DONE \n");
373 return (BUS_PROBE_DEFAULT);
380 hn_cpuset_setthread_task(void *xmask, int pending __unused)
382 cpuset_t *mask = xmask;
385 error = cpuset_setthread(curthread->td_tid, mask);
387 panic("curthread=%ju: can't pin; error=%d",
388 (uintmax_t)curthread->td_tid, error);
393 * Standard attach entry point.
395 * Called when the driver is loaded. It allocates needed resources,
396 * and initializes the "hardware" and software.
399 netvsc_attach(device_t dev)
401 struct hv_device *device_ctx = vmbus_get_devctx(dev);
402 struct hv_vmbus_channel *chan;
403 netvsc_device_info device_info;
405 int unit = device_get_unit(dev);
406 struct ifnet *ifp = NULL;
408 #if __FreeBSD_version >= 1100045
412 sc = device_get_softc(dev);
417 bzero(sc, sizeof(hn_softc_t));
421 if (hn_tx_taskq == NULL) {
422 sc->hn_tx_taskq = taskqueue_create("hn_tx", M_WAITOK,
423 taskqueue_thread_enqueue, &sc->hn_tx_taskq);
424 taskqueue_start_threads(&sc->hn_tx_taskq, 1, PI_NET, "%s tx",
425 device_get_nameunit(dev));
426 if (hn_bind_tx_taskq >= 0) {
427 int cpu = hn_bind_tx_taskq;
428 struct task cpuset_task;
431 if (cpu > mp_ncpus - 1)
433 CPU_SETOF(cpu, &cpu_set);
434 TASK_INIT(&cpuset_task, 0, hn_cpuset_setthread_task,
436 taskqueue_enqueue(sc->hn_tx_taskq, &cpuset_task);
437 taskqueue_drain(sc->hn_tx_taskq, &cpuset_task);
440 sc->hn_tx_taskq = hn_tx_taskq;
442 NV_LOCK_INIT(sc, "NetVSCLock");
444 sc->hn_dev_obj = device_ctx;
446 ifp = sc->hn_ifp = sc->arpcom.ac_ifp = if_alloc(IFT_ETHER);
449 error = hn_create_tx_data(sc);
453 hn_create_rx_data(sc);
456 * Associate the first TX/RX ring w/ the primary channel.
458 chan = device_ctx->channel;
459 chan->hv_chan_rxr = &sc->hn_rx_ring[0];
460 chan->hv_chan_txr = &sc->hn_tx_ring[0];
461 sc->hn_tx_ring[0].hn_chan = chan;
463 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
464 ifp->if_dunit = unit;
465 ifp->if_dname = NETVSC_DEVNAME;
467 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
468 ifp->if_ioctl = hn_ioctl;
469 ifp->if_init = hn_ifinit;
470 /* needed by hv_rf_on_device_add() code */
471 ifp->if_mtu = ETHERMTU;
472 if (hn_use_if_start) {
473 ifp->if_start = hn_start;
474 IFQ_SET_MAXLEN(&ifp->if_snd, 512);
475 ifp->if_snd.ifq_drv_maxlen = 511;
476 IFQ_SET_READY(&ifp->if_snd);
478 ifp->if_transmit = hn_transmit;
479 ifp->if_qflush = hn_xmit_qflush;
482 ifmedia_init(&sc->hn_media, 0, hn_ifmedia_upd, hn_ifmedia_sts);
483 ifmedia_add(&sc->hn_media, IFM_ETHER | IFM_AUTO, 0, NULL);
484 ifmedia_set(&sc->hn_media, IFM_ETHER | IFM_AUTO);
485 /* XXX ifmedia_set really should do this for us */
486 sc->hn_media.ifm_media = sc->hn_media.ifm_cur->ifm_media;
489 * Tell upper layers that we support full VLAN capability.
491 ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
492 ifp->if_capabilities |=
493 IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU | IFCAP_HWCSUM | IFCAP_TSO |
496 IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU | IFCAP_HWCSUM | IFCAP_TSO |
498 ifp->if_hwassist = sc->hn_tx_ring[0].hn_csum_assist | CSUM_TSO;
500 error = hv_rf_on_device_add(device_ctx, &device_info);
504 if (device_info.link_state == 0) {
508 #if __FreeBSD_version >= 1100045
509 tso_maxlen = hn_tso_maxlen;
510 if (tso_maxlen <= 0 || tso_maxlen > IP_MAXPACKET)
511 tso_maxlen = IP_MAXPACKET;
513 ifp->if_hw_tsomaxsegcount = HN_TX_DATA_SEGCNT_MAX;
514 ifp->if_hw_tsomaxsegsize = PAGE_SIZE;
515 ifp->if_hw_tsomax = tso_maxlen -
516 (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN);
519 ether_ifattach(ifp, device_info.mac_addr);
521 #if __FreeBSD_version >= 1100045
522 if_printf(ifp, "TSO: %u/%u/%u\n", ifp->if_hw_tsomax,
523 ifp->if_hw_tsomaxsegcount, ifp->if_hw_tsomaxsegsize);
526 sc->hn_tx_chimney_max = sc->net_dev->send_section_size;
527 hn_set_tx_chimney_size(sc, sc->hn_tx_chimney_max);
528 if (hn_tx_chimney_size > 0 &&
529 hn_tx_chimney_size < sc->hn_tx_chimney_max)
530 hn_set_tx_chimney_size(sc, hn_tx_chimney_size);
534 hn_destroy_tx_data(sc);
541 * Standard detach entry point
544 netvsc_detach(device_t dev)
546 struct hn_softc *sc = device_get_softc(dev);
547 struct hv_device *hv_device = vmbus_get_devctx(dev);
550 printf("netvsc_detach\n");
553 * XXXKYS: Need to clean up all our
554 * driver state; this is the driver
559 * XXXKYS: Need to stop outgoing traffic and unregister
563 hv_rf_on_device_remove(hv_device, HV_RF_NV_DESTROY_CHANNEL);
565 hn_stop_tx_tasks(sc);
567 ifmedia_removeall(&sc->hn_media);
568 hn_destroy_rx_data(sc);
569 hn_destroy_tx_data(sc);
571 if (sc->hn_tx_taskq != hn_tx_taskq)
572 taskqueue_free(sc->hn_tx_taskq);
578 * Standard shutdown entry point
581 netvsc_shutdown(device_t dev)
587 hn_txdesc_dmamap_load(struct hn_tx_ring *txr, struct hn_txdesc *txd,
588 struct mbuf **m_head, bus_dma_segment_t *segs, int *nsegs)
590 struct mbuf *m = *m_head;
593 error = bus_dmamap_load_mbuf_sg(txr->hn_tx_data_dtag, txd->data_dmap,
594 m, segs, nsegs, BUS_DMA_NOWAIT);
595 if (error == EFBIG) {
598 m_new = m_collapse(m, M_NOWAIT, HN_TX_DATA_SEGCNT_MAX);
603 txr->hn_tx_collapsed++;
605 error = bus_dmamap_load_mbuf_sg(txr->hn_tx_data_dtag,
606 txd->data_dmap, m, segs, nsegs, BUS_DMA_NOWAIT);
609 bus_dmamap_sync(txr->hn_tx_data_dtag, txd->data_dmap,
610 BUS_DMASYNC_PREWRITE);
611 txd->flags |= HN_TXD_FLAG_DMAMAP;
617 hn_txdesc_dmamap_unload(struct hn_tx_ring *txr, struct hn_txdesc *txd)
620 if (txd->flags & HN_TXD_FLAG_DMAMAP) {
621 bus_dmamap_sync(txr->hn_tx_data_dtag,
622 txd->data_dmap, BUS_DMASYNC_POSTWRITE);
623 bus_dmamap_unload(txr->hn_tx_data_dtag,
625 txd->flags &= ~HN_TXD_FLAG_DMAMAP;
630 hn_txdesc_put(struct hn_tx_ring *txr, struct hn_txdesc *txd)
633 KASSERT((txd->flags & HN_TXD_FLAG_ONLIST) == 0,
634 ("put an onlist txd %#x", txd->flags));
636 KASSERT(txd->refs > 0, ("invalid txd refs %d", txd->refs));
637 if (atomic_fetchadd_int(&txd->refs, -1) != 1)
640 hn_txdesc_dmamap_unload(txr, txd);
641 if (txd->m != NULL) {
646 txd->flags |= HN_TXD_FLAG_ONLIST;
648 #ifndef HN_USE_TXDESC_BUFRING
649 mtx_lock_spin(&txr->hn_txlist_spin);
650 KASSERT(txr->hn_txdesc_avail >= 0 &&
651 txr->hn_txdesc_avail < txr->hn_txdesc_cnt,
652 ("txdesc_put: invalid txd avail %d", txr->hn_txdesc_avail));
653 txr->hn_txdesc_avail++;
654 SLIST_INSERT_HEAD(&txr->hn_txlist, txd, link);
655 mtx_unlock_spin(&txr->hn_txlist_spin);
657 atomic_add_int(&txr->hn_txdesc_avail, 1);
658 buf_ring_enqueue(txr->hn_txdesc_br, txd);
664 static __inline struct hn_txdesc *
665 hn_txdesc_get(struct hn_tx_ring *txr)
667 struct hn_txdesc *txd;
669 #ifndef HN_USE_TXDESC_BUFRING
670 mtx_lock_spin(&txr->hn_txlist_spin);
671 txd = SLIST_FIRST(&txr->hn_txlist);
673 KASSERT(txr->hn_txdesc_avail > 0,
674 ("txdesc_get: invalid txd avail %d", txr->hn_txdesc_avail));
675 txr->hn_txdesc_avail--;
676 SLIST_REMOVE_HEAD(&txr->hn_txlist, link);
678 mtx_unlock_spin(&txr->hn_txlist_spin);
680 txd = buf_ring_dequeue_sc(txr->hn_txdesc_br);
684 #ifdef HN_USE_TXDESC_BUFRING
685 atomic_subtract_int(&txr->hn_txdesc_avail, 1);
687 KASSERT(txd->m == NULL && txd->refs == 0 &&
688 (txd->flags & HN_TXD_FLAG_ONLIST), ("invalid txd"));
689 txd->flags &= ~HN_TXD_FLAG_ONLIST;
696 hn_txdesc_hold(struct hn_txdesc *txd)
699 /* 0->1 transition will never work */
700 KASSERT(txd->refs > 0, ("invalid refs %d", txd->refs));
701 atomic_add_int(&txd->refs, 1);
705 hn_tx_done(void *xpkt)
707 netvsc_packet *packet = xpkt;
708 struct hn_txdesc *txd;
709 struct hn_tx_ring *txr;
711 txd = (struct hn_txdesc *)(uintptr_t)
712 packet->compl.send.send_completion_tid;
715 txr->hn_has_txeof = 1;
716 hn_txdesc_put(txr, txd);
720 netvsc_channel_rollup(struct hv_vmbus_channel *chan)
722 struct hn_tx_ring *txr = chan->hv_chan_txr;
723 #if defined(INET) || defined(INET6)
724 struct hn_rx_ring *rxr = chan->hv_chan_rxr;
725 struct lro_ctrl *lro = &rxr->hn_lro;
726 struct lro_entry *queued;
728 while ((queued = SLIST_FIRST(&lro->lro_active)) != NULL) {
729 SLIST_REMOVE_HEAD(&lro->lro_active, next);
730 tcp_lro_flush(lro, queued);
736 * 'txr' could be NULL, if multiple channels and
737 * ifnet.if_start method are enabled.
739 if (txr == NULL || !txr->hn_has_txeof)
742 txr->hn_has_txeof = 0;
748 * If this function fails, then both txd and m_head0 will be freed.
751 hn_encap(struct hn_tx_ring *txr, struct hn_txdesc *txd, struct mbuf **m_head0)
753 bus_dma_segment_t segs[HN_TX_DATA_SEGCNT_MAX];
755 struct mbuf *m_head = *m_head0;
756 netvsc_packet *packet;
757 rndis_msg *rndis_mesg;
758 rndis_packet *rndis_pkt;
759 rndis_per_packet_info *rppi;
760 uint32_t rndis_msg_size;
762 packet = &txd->netvsc_pkt;
763 packet->is_data_pkt = TRUE;
764 packet->tot_data_buf_len = m_head->m_pkthdr.len;
767 * extension points to the area reserved for the
768 * rndis_filter_packet, which is placed just after
769 * the netvsc_packet (and rppi struct, if present;
770 * length is updated later).
772 rndis_mesg = txd->rndis_msg;
773 /* XXX not necessary */
774 memset(rndis_mesg, 0, HN_RNDIS_MSG_LEN);
775 rndis_mesg->ndis_msg_type = REMOTE_NDIS_PACKET_MSG;
777 rndis_pkt = &rndis_mesg->msg.packet;
778 rndis_pkt->data_offset = sizeof(rndis_packet);
779 rndis_pkt->data_length = packet->tot_data_buf_len;
780 rndis_pkt->per_pkt_info_offset = sizeof(rndis_packet);
782 rndis_msg_size = RNDIS_MESSAGE_SIZE(rndis_packet);
784 if (m_head->m_flags & M_VLANTAG) {
785 ndis_8021q_info *rppi_vlan_info;
787 rndis_msg_size += RNDIS_VLAN_PPI_SIZE;
788 rppi = hv_set_rppi_data(rndis_mesg, RNDIS_VLAN_PPI_SIZE,
791 rppi_vlan_info = (ndis_8021q_info *)((uint8_t *)rppi +
792 rppi->per_packet_info_offset);
793 rppi_vlan_info->u1.s1.vlan_id =
794 m_head->m_pkthdr.ether_vtag & 0xfff;
797 if (m_head->m_pkthdr.csum_flags & CSUM_TSO) {
798 rndis_tcp_tso_info *tso_info;
799 struct ether_vlan_header *eh;
803 * XXX need m_pullup and use mtodo
805 eh = mtod(m_head, struct ether_vlan_header*);
806 if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN))
807 ether_len = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
809 ether_len = ETHER_HDR_LEN;
811 rndis_msg_size += RNDIS_TSO_PPI_SIZE;
812 rppi = hv_set_rppi_data(rndis_mesg, RNDIS_TSO_PPI_SIZE,
813 tcp_large_send_info);
815 tso_info = (rndis_tcp_tso_info *)((uint8_t *)rppi +
816 rppi->per_packet_info_offset);
817 tso_info->lso_v2_xmit.type =
818 RNDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE;
821 if (m_head->m_pkthdr.csum_flags & CSUM_IP_TSO) {
823 (struct ip *)(m_head->m_data + ether_len);
824 unsigned long iph_len = ip->ip_hl << 2;
826 (struct tcphdr *)((caddr_t)ip + iph_len);
828 tso_info->lso_v2_xmit.ip_version =
829 RNDIS_TCP_LARGE_SEND_OFFLOAD_IPV4;
833 th->th_sum = in_pseudo(ip->ip_src.s_addr,
834 ip->ip_dst.s_addr, htons(IPPROTO_TCP));
837 #if defined(INET6) && defined(INET)
842 struct ip6_hdr *ip6 = (struct ip6_hdr *)
843 (m_head->m_data + ether_len);
844 struct tcphdr *th = (struct tcphdr *)(ip6 + 1);
846 tso_info->lso_v2_xmit.ip_version =
847 RNDIS_TCP_LARGE_SEND_OFFLOAD_IPV6;
849 th->th_sum = in6_cksum_pseudo(ip6, 0, IPPROTO_TCP, 0);
852 tso_info->lso_v2_xmit.tcp_header_offset = 0;
853 tso_info->lso_v2_xmit.mss = m_head->m_pkthdr.tso_segsz;
854 } else if (m_head->m_pkthdr.csum_flags & txr->hn_csum_assist) {
855 rndis_tcp_ip_csum_info *csum_info;
857 rndis_msg_size += RNDIS_CSUM_PPI_SIZE;
858 rppi = hv_set_rppi_data(rndis_mesg, RNDIS_CSUM_PPI_SIZE,
860 csum_info = (rndis_tcp_ip_csum_info *)((uint8_t *)rppi +
861 rppi->per_packet_info_offset);
863 csum_info->xmit.is_ipv4 = 1;
864 if (m_head->m_pkthdr.csum_flags & CSUM_IP)
865 csum_info->xmit.ip_header_csum = 1;
867 if (m_head->m_pkthdr.csum_flags & CSUM_TCP) {
868 csum_info->xmit.tcp_csum = 1;
869 csum_info->xmit.tcp_header_offset = 0;
870 } else if (m_head->m_pkthdr.csum_flags & CSUM_UDP) {
871 csum_info->xmit.udp_csum = 1;
875 rndis_mesg->msg_len = packet->tot_data_buf_len + rndis_msg_size;
876 packet->tot_data_buf_len = rndis_mesg->msg_len;
879 * Chimney send, if the packet could fit into one chimney buffer.
881 * TODO: vRSS, chimney buffer should be per-channel.
883 if (packet->tot_data_buf_len < txr->hn_tx_chimney_size) {
884 netvsc_dev *net_dev = txr->hn_sc->net_dev;
885 uint32_t send_buf_section_idx;
887 send_buf_section_idx =
888 hv_nv_get_next_send_section(net_dev);
889 if (send_buf_section_idx !=
890 NVSP_1_CHIMNEY_SEND_INVALID_SECTION_INDEX) {
891 uint8_t *dest = ((uint8_t *)net_dev->send_buf +
892 (send_buf_section_idx *
893 net_dev->send_section_size));
895 memcpy(dest, rndis_mesg, rndis_msg_size);
896 dest += rndis_msg_size;
897 m_copydata(m_head, 0, m_head->m_pkthdr.len, dest);
899 packet->send_buf_section_idx = send_buf_section_idx;
900 packet->send_buf_section_size =
901 packet->tot_data_buf_len;
902 packet->page_buf_count = 0;
903 txr->hn_tx_chimney++;
908 error = hn_txdesc_dmamap_load(txr, txd, &m_head, segs, &nsegs);
913 * This mbuf is not linked w/ the txd yet, so free it now.
918 freed = hn_txdesc_put(txr, txd);
920 ("fail to free txd upon txdma error"));
922 txr->hn_txdma_failed++;
923 if_inc_counter(txr->hn_sc->hn_ifp, IFCOUNTER_OERRORS, 1);
928 packet->page_buf_count = nsegs + HV_RF_NUM_TX_RESERVED_PAGE_BUFS;
930 /* send packet with page buffer */
931 packet->page_buffers[0].pfn = atop(txd->rndis_msg_paddr);
932 packet->page_buffers[0].offset = txd->rndis_msg_paddr & PAGE_MASK;
933 packet->page_buffers[0].length = rndis_msg_size;
936 * Fill the page buffers with mbuf info starting at index
937 * HV_RF_NUM_TX_RESERVED_PAGE_BUFS.
939 for (i = 0; i < nsegs; ++i) {
940 hv_vmbus_page_buffer *pb = &packet->page_buffers[
941 i + HV_RF_NUM_TX_RESERVED_PAGE_BUFS];
943 pb->pfn = atop(segs[i].ds_addr);
944 pb->offset = segs[i].ds_addr & PAGE_MASK;
945 pb->length = segs[i].ds_len;
948 packet->send_buf_section_idx =
949 NVSP_1_CHIMNEY_SEND_INVALID_SECTION_INDEX;
950 packet->send_buf_section_size = 0;
954 /* Set the completion routine */
955 packet->compl.send.on_send_completion = hn_tx_done;
956 packet->compl.send.send_completion_context = packet;
957 packet->compl.send.send_completion_tid = (uint64_t)(uintptr_t)txd;
964 * If this function fails, then txd will be freed, but the mbuf
965 * associated w/ the txd will _not_ be freed.
968 hn_send_pkt(struct ifnet *ifp, struct hn_tx_ring *txr, struct hn_txdesc *txd)
970 int error, send_failed = 0;
974 * Make sure that txd is not freed before ETHER_BPF_MTAP.
977 error = hv_nv_on_send(txr->hn_chan, &txd->netvsc_pkt);
979 ETHER_BPF_MTAP(ifp, txd->m);
980 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
981 if (!hn_use_if_start) {
982 if_inc_counter(ifp, IFCOUNTER_OBYTES,
983 txd->m->m_pkthdr.len);
984 if (txd->m->m_flags & M_MCAST)
985 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
988 hn_txdesc_put(txr, txd);
990 if (__predict_false(error)) {
994 * This should "really rarely" happen.
996 * XXX Too many RX to be acked or too many sideband
997 * commands to run? Ask netvsc_channel_rollup()
998 * to kick start later.
1000 txr->hn_has_txeof = 1;
1002 txr->hn_send_failed++;
1005 * Try sending again after set hn_has_txeof;
1006 * in case that we missed the last
1007 * netvsc_channel_rollup().
1011 if_printf(ifp, "send failed\n");
1014 * Caller will perform further processing on the
1015 * associated mbuf, so don't free it in hn_txdesc_put();
1016 * only unload it from the DMA map in hn_txdesc_put(),
1020 freed = hn_txdesc_put(txr, txd);
1022 ("fail to free txd upon send error"));
1024 txr->hn_send_failed++;
1030 * Start a transmit of one or more packets
1033 hn_start_locked(struct hn_tx_ring *txr, int len)
1035 struct hn_softc *sc = txr->hn_sc;
1036 struct ifnet *ifp = sc->hn_ifp;
1038 KASSERT(hn_use_if_start,
1039 ("hn_start_locked is called, when if_start is disabled"));
1040 KASSERT(txr == &sc->hn_tx_ring[0], ("not the first TX ring"));
1041 mtx_assert(&txr->hn_tx_lock, MA_OWNED);
1043 if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
1047 while (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) {
1048 struct hn_txdesc *txd;
1049 struct mbuf *m_head;
1052 IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
1056 if (len > 0 && m_head->m_pkthdr.len > len) {
1058 * This sending could be time consuming; let callers
1059 * dispatch this packet sending (and sending of any
1060 * following up packets) to tx taskqueue.
1062 IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
1066 txd = hn_txdesc_get(txr);
1068 txr->hn_no_txdescs++;
1069 IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
1070 atomic_set_int(&ifp->if_drv_flags, IFF_DRV_OACTIVE);
1074 error = hn_encap(txr, txd, &m_head);
1076 /* Both txd and m_head are freed */
1080 error = hn_send_pkt(ifp, txr, txd);
1081 if (__predict_false(error)) {
1082 /* txd is freed, but m_head is not */
1083 IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
1084 atomic_set_int(&ifp->if_drv_flags, IFF_DRV_OACTIVE);
1092 * Link up/down notification
1095 netvsc_linkstatus_callback(struct hv_device *device_obj, uint32_t status)
1097 hn_softc_t *sc = device_get_softc(device_obj->device);
1111 * Append the specified data to the indicated mbuf chain,
1112 * Extend the mbuf chain if the new data does not fit in
1115 * This is a minor rewrite of m_append() from sys/kern/uipc_mbuf.c.
1116 * There should be an equivalent in the kernel mbuf code,
1117 * but there does not appear to be one yet.
1119 * Differs from m_append() in that additional mbufs are
1120 * allocated with cluster size MJUMPAGESIZE, and filled
1123 * Return 1 if able to complete the job; otherwise 0.
1126 hv_m_append(struct mbuf *m0, int len, c_caddr_t cp)
1129 int remainder, space;
1131 for (m = m0; m->m_next != NULL; m = m->m_next)
1134 space = M_TRAILINGSPACE(m);
1137 * Copy into available space.
1139 if (space > remainder)
1141 bcopy(cp, mtod(m, caddr_t) + m->m_len, space);
1146 while (remainder > 0) {
1148 * Allocate a new mbuf; could check space
1149 * and allocate a cluster instead.
1151 n = m_getjcl(M_DONTWAIT, m->m_type, 0, MJUMPAGESIZE);
1154 n->m_len = min(MJUMPAGESIZE, remainder);
1155 bcopy(cp, mtod(n, caddr_t), n->m_len);
1157 remainder -= n->m_len;
1161 if (m0->m_flags & M_PKTHDR)
1162 m0->m_pkthdr.len += len - remainder;
1164 return (remainder == 0);
1169 * Called when we receive a data packet from the "wire" on the
1172 * Note: This is no longer used as a callback
1175 netvsc_recv(struct hv_vmbus_channel *chan, netvsc_packet *packet,
1176 rndis_tcp_ip_csum_info *csum_info)
1178 struct hn_rx_ring *rxr = chan->hv_chan_rxr;
1179 struct ifnet *ifp = rxr->hn_ifp;
1181 int size, do_lro = 0, do_csum = 1;
1183 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
1187 * Bail out if packet contains more data than configured MTU.
1189 if (packet->tot_data_buf_len > (ifp->if_mtu + ETHER_HDR_LEN)) {
1191 } else if (packet->tot_data_buf_len <= MHLEN) {
1192 m_new = m_gethdr(M_NOWAIT, MT_DATA);
1195 memcpy(mtod(m_new, void *), packet->data,
1196 packet->tot_data_buf_len);
1197 m_new->m_pkthdr.len = m_new->m_len = packet->tot_data_buf_len;
1198 rxr->hn_small_pkts++;
1201 * Get an mbuf with a cluster. For packets 2K or less,
1202 * get a standard 2K cluster. For anything larger, get a
1203 * 4K cluster. Any buffers larger than 4K can cause problems
1204 * if looped around to the Hyper-V TX channel, so avoid them.
1207 if (packet->tot_data_buf_len > MCLBYTES) {
1209 size = MJUMPAGESIZE;
1212 m_new = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, size);
1213 if (m_new == NULL) {
1214 if_printf(ifp, "alloc mbuf failed.\n");
1218 hv_m_append(m_new, packet->tot_data_buf_len, packet->data);
1220 m_new->m_pkthdr.rcvif = ifp;
1222 if (__predict_false((ifp->if_capenable & IFCAP_RXCSUM) == 0))
1225 /* receive side checksum offload */
1226 if (csum_info != NULL) {
1227 /* IP csum offload */
1228 if (csum_info->receive.ip_csum_succeeded && do_csum) {
1229 m_new->m_pkthdr.csum_flags |=
1230 (CSUM_IP_CHECKED | CSUM_IP_VALID);
1234 /* TCP/UDP csum offload */
1235 if ((csum_info->receive.tcp_csum_succeeded ||
1236 csum_info->receive.udp_csum_succeeded) && do_csum) {
1237 m_new->m_pkthdr.csum_flags |=
1238 (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
1239 m_new->m_pkthdr.csum_data = 0xffff;
1240 if (csum_info->receive.tcp_csum_succeeded)
1246 if (csum_info->receive.ip_csum_succeeded &&
1247 csum_info->receive.tcp_csum_succeeded)
1250 const struct ether_header *eh;
1255 if (m_new->m_len < hoff)
1257 eh = mtod(m_new, struct ether_header *);
1258 etype = ntohs(eh->ether_type);
1259 if (etype == ETHERTYPE_VLAN) {
1260 const struct ether_vlan_header *evl;
1262 hoff = sizeof(*evl);
1263 if (m_new->m_len < hoff)
1265 evl = mtod(m_new, struct ether_vlan_header *);
1266 etype = ntohs(evl->evl_proto);
1269 if (etype == ETHERTYPE_IP) {
1272 pr = hn_check_iplen(m_new, hoff);
1273 if (pr == IPPROTO_TCP) {
1275 (rxr->hn_trust_hcsum &
1276 HN_TRUST_HCSUM_TCP)) {
1277 rxr->hn_csum_trusted++;
1278 m_new->m_pkthdr.csum_flags |=
1279 (CSUM_IP_CHECKED | CSUM_IP_VALID |
1280 CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
1281 m_new->m_pkthdr.csum_data = 0xffff;
1283 /* Rely on SW csum verification though... */
1285 } else if (pr == IPPROTO_UDP) {
1287 (rxr->hn_trust_hcsum &
1288 HN_TRUST_HCSUM_UDP)) {
1289 rxr->hn_csum_trusted++;
1290 m_new->m_pkthdr.csum_flags |=
1291 (CSUM_IP_CHECKED | CSUM_IP_VALID |
1292 CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
1293 m_new->m_pkthdr.csum_data = 0xffff;
1295 } else if (pr != IPPROTO_DONE && do_csum &&
1296 (rxr->hn_trust_hcsum & HN_TRUST_HCSUM_IP)) {
1297 rxr->hn_csum_trusted++;
1298 m_new->m_pkthdr.csum_flags |=
1299 (CSUM_IP_CHECKED | CSUM_IP_VALID);
1304 if ((packet->vlan_tci != 0) &&
1305 (ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0) {
1306 m_new->m_pkthdr.ether_vtag = packet->vlan_tci;
1307 m_new->m_flags |= M_VLANTAG;
1311 * Note: Moved RX completion back to hv_nv_on_receive() so all
1312 * messages (not just data messages) will trigger a response.
1317 if ((ifp->if_capenable & IFCAP_LRO) && do_lro) {
1318 #if defined(INET) || defined(INET6)
1319 struct lro_ctrl *lro = &rxr->hn_lro;
1322 rxr->hn_lro_tried++;
1323 if (tcp_lro_rx(lro, m_new, 0) == 0) {
1331 /* We're not holding the lock here, so don't release it */
1332 (*ifp->if_input)(ifp, m_new);
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;
2087 rxr->hn_ifp = sc->hn_ifp;
2092 #if defined(INET) || defined(INET6)
2093 #if __FreeBSD_version >= 1100095
2094 tcp_lro_init_args(&rxr->hn_lro, sc->hn_ifp, lroent_cnt, 0);
2096 tcp_lro_init(&rxr->hn_lro);
2097 rxr->hn_lro.ifp = sc->hn_ifp;
2099 #if __FreeBSD_version >= 1100099
2100 rxr->hn_lro.lro_length_lim = HN_LRO_LENLIM_DEF;
2101 rxr->hn_lro.lro_ackcnt_lim = HN_LRO_ACKCNT_DEF;
2103 #endif /* INET || INET6 */
2106 ctx = device_get_sysctl_ctx(dev);
2107 child = SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
2109 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "lro_queued",
2110 CTLTYPE_U64 | CTLFLAG_RW, sc,
2111 __offsetof(struct hn_rx_ring, hn_lro.lro_queued),
2112 #if __FreeBSD_version < 1100095
2113 hn_rx_stat_int_sysctl,
2115 hn_rx_stat_u64_sysctl,
2117 "LU", "LRO queued");
2118 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "lro_flushed",
2119 CTLTYPE_U64 | CTLFLAG_RW, sc,
2120 __offsetof(struct hn_rx_ring, hn_lro.lro_flushed),
2121 #if __FreeBSD_version < 1100095
2122 hn_rx_stat_int_sysctl,
2124 hn_rx_stat_u64_sysctl,
2126 "LU", "LRO flushed");
2127 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "lro_tried",
2128 CTLTYPE_ULONG | CTLFLAG_RW, sc,
2129 __offsetof(struct hn_rx_ring, hn_lro_tried),
2130 hn_rx_stat_ulong_sysctl, "LU", "# of LRO tries");
2131 #if __FreeBSD_version >= 1100099
2132 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "lro_length_lim",
2133 CTLTYPE_UINT | CTLFLAG_RW, sc, 0, hn_lro_lenlim_sysctl, "IU",
2134 "Max # of data bytes to be aggregated by LRO");
2135 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "lro_ackcnt_lim",
2136 CTLTYPE_INT | CTLFLAG_RW, sc, 0, hn_lro_ackcnt_sysctl, "I",
2137 "Max # of ACKs to be aggregated by LRO");
2139 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "trust_hosttcp",
2140 CTLTYPE_INT | CTLFLAG_RW, sc, HN_TRUST_HCSUM_TCP,
2141 hn_trust_hcsum_sysctl, "I",
2142 "Trust tcp segement verification on host side, "
2143 "when csum info is missing");
2144 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "trust_hostudp",
2145 CTLTYPE_INT | CTLFLAG_RW, sc, HN_TRUST_HCSUM_UDP,
2146 hn_trust_hcsum_sysctl, "I",
2147 "Trust udp datagram verification on host side, "
2148 "when csum info is missing");
2149 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "trust_hostip",
2150 CTLTYPE_INT | CTLFLAG_RW, sc, HN_TRUST_HCSUM_IP,
2151 hn_trust_hcsum_sysctl, "I",
2152 "Trust ip packet verification on host side, "
2153 "when csum info is missing");
2154 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "csum_ip",
2155 CTLTYPE_ULONG | CTLFLAG_RW, sc,
2156 __offsetof(struct hn_rx_ring, hn_csum_ip),
2157 hn_rx_stat_ulong_sysctl, "LU", "RXCSUM IP");
2158 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "csum_tcp",
2159 CTLTYPE_ULONG | CTLFLAG_RW, sc,
2160 __offsetof(struct hn_rx_ring, hn_csum_tcp),
2161 hn_rx_stat_ulong_sysctl, "LU", "RXCSUM TCP");
2162 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "csum_udp",
2163 CTLTYPE_ULONG | CTLFLAG_RW, sc,
2164 __offsetof(struct hn_rx_ring, hn_csum_udp),
2165 hn_rx_stat_ulong_sysctl, "LU", "RXCSUM UDP");
2166 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "csum_trusted",
2167 CTLTYPE_ULONG | CTLFLAG_RW, sc,
2168 __offsetof(struct hn_rx_ring, hn_csum_trusted),
2169 hn_rx_stat_ulong_sysctl, "LU",
2170 "# of packets that we trust host's csum verification");
2171 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "small_pkts",
2172 CTLTYPE_ULONG | CTLFLAG_RW, sc,
2173 __offsetof(struct hn_rx_ring, hn_small_pkts),
2174 hn_rx_stat_ulong_sysctl, "LU", "# of small packets received");
2178 hn_destroy_rx_data(struct hn_softc *sc)
2180 #if defined(INET) || defined(INET6)
2184 if (sc->hn_rx_ring_cnt == 0)
2187 #if defined(INET) || defined(INET6)
2188 for (i = 0; i < sc->hn_rx_ring_cnt; ++i)
2189 tcp_lro_free(&sc->hn_rx_ring[i].hn_lro);
2191 free(sc->hn_rx_ring, M_NETVSC);
2192 sc->hn_rx_ring = NULL;
2194 sc->hn_rx_ring_cnt = 0;
2198 hn_create_tx_ring(struct hn_softc *sc, int id)
2200 struct hn_tx_ring *txr = &sc->hn_tx_ring[id];
2201 bus_dma_tag_t parent_dtag;
2206 #ifndef HN_USE_TXDESC_BUFRING
2207 mtx_init(&txr->hn_txlist_spin, "hn txlist", NULL, MTX_SPIN);
2209 mtx_init(&txr->hn_tx_lock, "hn tx", NULL, MTX_DEF);
2211 txr->hn_txdesc_cnt = HN_TX_DESC_CNT;
2212 txr->hn_txdesc = malloc(sizeof(struct hn_txdesc) * txr->hn_txdesc_cnt,
2213 M_NETVSC, M_WAITOK | M_ZERO);
2214 #ifndef HN_USE_TXDESC_BUFRING
2215 SLIST_INIT(&txr->hn_txlist);
2217 txr->hn_txdesc_br = buf_ring_alloc(txr->hn_txdesc_cnt, M_NETVSC,
2218 M_WAITOK, &txr->hn_tx_lock);
2221 txr->hn_tx_taskq = sc->hn_tx_taskq;
2223 if (hn_use_if_start) {
2224 txr->hn_txeof = hn_start_txeof;
2225 TASK_INIT(&txr->hn_tx_task, 0, hn_start_taskfunc, txr);
2226 TASK_INIT(&txr->hn_txeof_task, 0, hn_start_txeof_taskfunc, txr);
2228 txr->hn_txeof = hn_xmit_txeof;
2229 TASK_INIT(&txr->hn_tx_task, 0, hn_xmit_taskfunc, txr);
2230 TASK_INIT(&txr->hn_txeof_task, 0, hn_xmit_txeof_taskfunc, txr);
2231 txr->hn_mbuf_br = buf_ring_alloc(txr->hn_txdesc_cnt, M_NETVSC,
2232 M_WAITOK, &txr->hn_tx_lock);
2235 txr->hn_direct_tx_size = hn_direct_tx_size;
2236 if (hv_vmbus_protocal_version >= HV_VMBUS_VERSION_WIN8_1)
2237 txr->hn_csum_assist = HN_CSUM_ASSIST;
2239 txr->hn_csum_assist = HN_CSUM_ASSIST_WIN8;
2242 * Always schedule transmission instead of trying to do direct
2243 * transmission. This one gives the best performance so far.
2245 txr->hn_sched_tx = 1;
2247 parent_dtag = bus_get_dma_tag(sc->hn_dev);
2249 /* DMA tag for RNDIS messages. */
2250 error = bus_dma_tag_create(parent_dtag, /* parent */
2251 HN_RNDIS_MSG_ALIGN, /* alignment */
2252 HN_RNDIS_MSG_BOUNDARY, /* boundary */
2253 BUS_SPACE_MAXADDR, /* lowaddr */
2254 BUS_SPACE_MAXADDR, /* highaddr */
2255 NULL, NULL, /* filter, filterarg */
2256 HN_RNDIS_MSG_LEN, /* maxsize */
2258 HN_RNDIS_MSG_LEN, /* maxsegsize */
2260 NULL, /* lockfunc */
2261 NULL, /* lockfuncarg */
2262 &txr->hn_tx_rndis_dtag);
2264 device_printf(sc->hn_dev, "failed to create rndis dmatag\n");
2268 /* DMA tag for data. */
2269 error = bus_dma_tag_create(parent_dtag, /* parent */
2271 HN_TX_DATA_BOUNDARY, /* boundary */
2272 BUS_SPACE_MAXADDR, /* lowaddr */
2273 BUS_SPACE_MAXADDR, /* highaddr */
2274 NULL, NULL, /* filter, filterarg */
2275 HN_TX_DATA_MAXSIZE, /* maxsize */
2276 HN_TX_DATA_SEGCNT_MAX, /* nsegments */
2277 HN_TX_DATA_SEGSIZE, /* maxsegsize */
2279 NULL, /* lockfunc */
2280 NULL, /* lockfuncarg */
2281 &txr->hn_tx_data_dtag);
2283 device_printf(sc->hn_dev, "failed to create data dmatag\n");
2287 for (i = 0; i < txr->hn_txdesc_cnt; ++i) {
2288 struct hn_txdesc *txd = &txr->hn_txdesc[i];
2293 * Allocate and load RNDIS messages.
2295 error = bus_dmamem_alloc(txr->hn_tx_rndis_dtag,
2296 (void **)&txd->rndis_msg,
2297 BUS_DMA_WAITOK | BUS_DMA_COHERENT,
2298 &txd->rndis_msg_dmap);
2300 device_printf(sc->hn_dev,
2301 "failed to allocate rndis_msg, %d\n", i);
2305 error = bus_dmamap_load(txr->hn_tx_rndis_dtag,
2306 txd->rndis_msg_dmap,
2307 txd->rndis_msg, HN_RNDIS_MSG_LEN,
2308 hn_dma_map_paddr, &txd->rndis_msg_paddr,
2311 device_printf(sc->hn_dev,
2312 "failed to load rndis_msg, %d\n", i);
2313 bus_dmamem_free(txr->hn_tx_rndis_dtag,
2314 txd->rndis_msg, txd->rndis_msg_dmap);
2318 /* DMA map for TX data. */
2319 error = bus_dmamap_create(txr->hn_tx_data_dtag, 0,
2322 device_printf(sc->hn_dev,
2323 "failed to allocate tx data dmamap\n");
2324 bus_dmamap_unload(txr->hn_tx_rndis_dtag,
2325 txd->rndis_msg_dmap);
2326 bus_dmamem_free(txr->hn_tx_rndis_dtag,
2327 txd->rndis_msg, txd->rndis_msg_dmap);
2331 /* All set, put it to list */
2332 txd->flags |= HN_TXD_FLAG_ONLIST;
2333 #ifndef HN_USE_TXDESC_BUFRING
2334 SLIST_INSERT_HEAD(&txr->hn_txlist, txd, link);
2336 buf_ring_enqueue(txr->hn_txdesc_br, txd);
2339 txr->hn_txdesc_avail = txr->hn_txdesc_cnt;
2341 if (sc->hn_tx_sysctl_tree != NULL) {
2342 struct sysctl_oid_list *child;
2343 struct sysctl_ctx_list *ctx;
2347 * Create per TX ring sysctl tree:
2348 * dev.hn.UNIT.tx.RINGID
2350 ctx = device_get_sysctl_ctx(sc->hn_dev);
2351 child = SYSCTL_CHILDREN(sc->hn_tx_sysctl_tree);
2353 snprintf(name, sizeof(name), "%d", id);
2354 txr->hn_tx_sysctl_tree = SYSCTL_ADD_NODE(ctx, child, OID_AUTO,
2355 name, CTLFLAG_RD, 0, "");
2357 if (txr->hn_tx_sysctl_tree != NULL) {
2358 child = SYSCTL_CHILDREN(txr->hn_tx_sysctl_tree);
2360 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "txdesc_avail",
2361 CTLFLAG_RD, &txr->hn_txdesc_avail, 0,
2362 "# of available TX descs");
2363 if (!hn_use_if_start) {
2364 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "oactive",
2365 CTLFLAG_RD, &txr->hn_oactive, 0,
2375 hn_txdesc_dmamap_destroy(struct hn_txdesc *txd)
2377 struct hn_tx_ring *txr = txd->txr;
2379 KASSERT(txd->m == NULL, ("still has mbuf installed"));
2380 KASSERT((txd->flags & HN_TXD_FLAG_DMAMAP) == 0, ("still dma mapped"));
2382 bus_dmamap_unload(txr->hn_tx_rndis_dtag, txd->rndis_msg_dmap);
2383 bus_dmamem_free(txr->hn_tx_rndis_dtag, txd->rndis_msg,
2384 txd->rndis_msg_dmap);
2385 bus_dmamap_destroy(txr->hn_tx_data_dtag, txd->data_dmap);
2389 hn_destroy_tx_ring(struct hn_tx_ring *txr)
2391 struct hn_txdesc *txd;
2393 if (txr->hn_txdesc == NULL)
2396 #ifndef HN_USE_TXDESC_BUFRING
2397 while ((txd = SLIST_FIRST(&txr->hn_txlist)) != NULL) {
2398 SLIST_REMOVE_HEAD(&txr->hn_txlist, link);
2399 hn_txdesc_dmamap_destroy(txd);
2402 mtx_lock(&txr->hn_tx_lock);
2403 while ((txd = buf_ring_dequeue_sc(txr->hn_txdesc_br)) != NULL)
2404 hn_txdesc_dmamap_destroy(txd);
2405 mtx_unlock(&txr->hn_tx_lock);
2408 if (txr->hn_tx_data_dtag != NULL)
2409 bus_dma_tag_destroy(txr->hn_tx_data_dtag);
2410 if (txr->hn_tx_rndis_dtag != NULL)
2411 bus_dma_tag_destroy(txr->hn_tx_rndis_dtag);
2413 #ifdef HN_USE_TXDESC_BUFRING
2414 buf_ring_free(txr->hn_txdesc_br, M_NETVSC);
2417 free(txr->hn_txdesc, M_NETVSC);
2418 txr->hn_txdesc = NULL;
2420 if (txr->hn_mbuf_br != NULL)
2421 buf_ring_free(txr->hn_mbuf_br, M_NETVSC);
2423 #ifndef HN_USE_TXDESC_BUFRING
2424 mtx_destroy(&txr->hn_txlist_spin);
2426 mtx_destroy(&txr->hn_tx_lock);
2430 hn_create_tx_data(struct hn_softc *sc)
2432 struct sysctl_oid_list *child;
2433 struct sysctl_ctx_list *ctx;
2436 if (hn_use_if_start) {
2437 /* ifnet.if_start only needs one TX ring */
2438 sc->hn_tx_ring_cnt = 1;
2440 sc->hn_tx_ring_cnt = 1; /* TODO: vRSS */
2442 sc->hn_tx_ring = malloc(sizeof(struct hn_tx_ring) * sc->hn_tx_ring_cnt,
2443 M_NETVSC, M_WAITOK | M_ZERO);
2445 ctx = device_get_sysctl_ctx(sc->hn_dev);
2446 child = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->hn_dev));
2448 /* Create dev.hn.UNIT.tx sysctl tree */
2449 sc->hn_tx_sysctl_tree = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, "tx",
2452 for (i = 0; i < sc->hn_tx_ring_cnt; ++i) {
2455 error = hn_create_tx_ring(sc, i);
2460 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "no_txdescs",
2461 CTLTYPE_ULONG | CTLFLAG_RW, sc,
2462 __offsetof(struct hn_tx_ring, hn_no_txdescs),
2463 hn_tx_stat_ulong_sysctl, "LU", "# of times short of TX descs");
2464 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "send_failed",
2465 CTLTYPE_ULONG | CTLFLAG_RW, sc,
2466 __offsetof(struct hn_tx_ring, hn_send_failed),
2467 hn_tx_stat_ulong_sysctl, "LU", "# of hyper-v sending failure");
2468 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "txdma_failed",
2469 CTLTYPE_ULONG | CTLFLAG_RW, sc,
2470 __offsetof(struct hn_tx_ring, hn_txdma_failed),
2471 hn_tx_stat_ulong_sysctl, "LU", "# of TX DMA failure");
2472 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "tx_collapsed",
2473 CTLTYPE_ULONG | CTLFLAG_RW, sc,
2474 __offsetof(struct hn_tx_ring, hn_tx_collapsed),
2475 hn_tx_stat_ulong_sysctl, "LU", "# of TX mbuf collapsed");
2476 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "tx_chimney",
2477 CTLTYPE_ULONG | CTLFLAG_RW, sc,
2478 __offsetof(struct hn_tx_ring, hn_tx_chimney),
2479 hn_tx_stat_ulong_sysctl, "LU", "# of chimney send");
2480 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "txdesc_cnt",
2481 CTLFLAG_RD, &sc->hn_tx_ring[0].hn_txdesc_cnt, 0,
2482 "# of total TX descs");
2483 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "tx_chimney_max",
2484 CTLFLAG_RD, &sc->hn_tx_chimney_max, 0,
2485 "Chimney send packet size upper boundary");
2486 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "tx_chimney_size",
2487 CTLTYPE_INT | CTLFLAG_RW, sc, 0, hn_tx_chimney_size_sysctl,
2488 "I", "Chimney send packet size limit");
2489 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "direct_tx_size",
2490 CTLTYPE_INT | CTLFLAG_RW, sc,
2491 __offsetof(struct hn_tx_ring, hn_direct_tx_size),
2492 hn_tx_conf_int_sysctl, "I",
2493 "Size of the packet for direct transmission");
2494 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "sched_tx",
2495 CTLTYPE_INT | CTLFLAG_RW, sc,
2496 __offsetof(struct hn_tx_ring, hn_sched_tx),
2497 hn_tx_conf_int_sysctl, "I",
2498 "Always schedule transmission "
2499 "instead of doing direct transmission");
2505 hn_set_tx_chimney_size(struct hn_softc *sc, int chimney_size)
2510 for (i = 0; i < sc->hn_tx_ring_cnt; ++i)
2511 sc->hn_tx_ring[i].hn_tx_chimney_size = chimney_size;
2516 hn_destroy_tx_data(struct hn_softc *sc)
2520 if (sc->hn_tx_ring_cnt == 0)
2523 for (i = 0; i < sc->hn_tx_ring_cnt; ++i)
2524 hn_destroy_tx_ring(&sc->hn_tx_ring[i]);
2526 free(sc->hn_tx_ring, M_NETVSC);
2527 sc->hn_tx_ring = NULL;
2529 sc->hn_tx_ring_cnt = 0;
2533 hn_start_taskfunc(void *xtxr, int pending __unused)
2535 struct hn_tx_ring *txr = xtxr;
2537 mtx_lock(&txr->hn_tx_lock);
2538 hn_start_locked(txr, 0);
2539 mtx_unlock(&txr->hn_tx_lock);
2543 hn_start_txeof_taskfunc(void *xtxr, int pending __unused)
2545 struct hn_tx_ring *txr = xtxr;
2547 mtx_lock(&txr->hn_tx_lock);
2548 atomic_clear_int(&txr->hn_sc->hn_ifp->if_drv_flags, IFF_DRV_OACTIVE);
2549 hn_start_locked(txr, 0);
2550 mtx_unlock(&txr->hn_tx_lock);
2554 hn_stop_tx_tasks(struct hn_softc *sc)
2558 for (i = 0; i < sc->hn_tx_ring_cnt; ++i) {
2559 struct hn_tx_ring *txr = &sc->hn_tx_ring[i];
2561 taskqueue_drain(txr->hn_tx_taskq, &txr->hn_tx_task);
2562 taskqueue_drain(txr->hn_tx_taskq, &txr->hn_txeof_task);
2567 hn_xmit(struct hn_tx_ring *txr, int len)
2569 struct hn_softc *sc = txr->hn_sc;
2570 struct ifnet *ifp = sc->hn_ifp;
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, 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);