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>
74 #include <net/if_arp.h>
75 #include <net/ethernet.h>
76 #include <net/if_dl.h>
77 #include <net/if_media.h>
81 #include <net/if_var.h>
82 #include <net/if_types.h>
83 #include <net/if_vlan_var.h>
85 #include <netinet/in_systm.h>
86 #include <netinet/in.h>
87 #include <netinet/ip.h>
88 #include <netinet/if_ether.h>
89 #include <netinet/tcp.h>
90 #include <netinet/udp.h>
91 #include <netinet/ip6.h>
94 #include <vm/vm_param.h>
95 #include <vm/vm_kern.h>
98 #include <machine/bus.h>
99 #include <machine/resource.h>
100 #include <machine/frame.h>
101 #include <machine/vmparam.h>
104 #include <sys/rman.h>
105 #include <sys/mutex.h>
106 #include <sys/errno.h>
107 #include <sys/types.h>
108 #include <machine/atomic.h>
110 #include <machine/intr_machdep.h>
112 #include <machine/in_cksum.h>
114 #include <dev/hyperv/include/hyperv.h>
115 #include "hv_net_vsc.h"
116 #include "hv_rndis.h"
117 #include "hv_rndis_filter.h"
120 /* Short for Hyper-V network interface */
121 #define NETVSC_DEVNAME "hn"
124 * It looks like offset 0 of buf is reserved to hold the softc pointer.
125 * The sc pointer evidently not needed, and is not presently populated.
126 * The packet offset is where the netvsc_packet starts in the buffer.
128 #define HV_NV_SC_PTR_OFFSET_IN_BUF 0
129 #define HV_NV_PACKET_OFFSET_IN_BUF 16
136 struct hv_netvsc_driver_context {
141 * Be aware that this sleepable mutex will exhibit WITNESS errors when
142 * certain TCP and ARP code paths are taken. This appears to be a
143 * well-known condition, as all other drivers checked use a sleeping
144 * mutex to protect their transmit paths.
145 * Also Be aware that mutexes do not play well with semaphores, and there
146 * is a conflicting semaphore in a certain channel code path.
148 #define NV_LOCK_INIT(_sc, _name) \
149 mtx_init(&(_sc)->hn_lock, _name, MTX_NETWORK_LOCK, MTX_DEF)
150 #define NV_LOCK(_sc) mtx_lock(&(_sc)->hn_lock)
151 #define NV_LOCK_ASSERT(_sc) mtx_assert(&(_sc)->hn_lock, MA_OWNED)
152 #define NV_UNLOCK(_sc) mtx_unlock(&(_sc)->hn_lock)
153 #define NV_LOCK_DESTROY(_sc) mtx_destroy(&(_sc)->hn_lock)
160 int hv_promisc_mode = 0; /* normal mode by default */
162 /* The one and only one */
163 static struct hv_netvsc_driver_context g_netvsc_drv;
167 * Forward declarations
169 static void hn_stop(hn_softc_t *sc);
170 static void hn_ifinit_locked(hn_softc_t *sc);
171 static void hn_ifinit(void *xsc);
172 static int hn_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
173 static int hn_start_locked(struct ifnet *ifp);
174 static void hn_start(struct ifnet *ifp);
177 * NetVsc get message transport protocol type
179 static uint32_t get_transport_proto_type(struct mbuf *m_head)
181 uint32_t ret_val = TRANSPORT_TYPE_NOT_IP;
182 uint16_t ether_type = 0;
184 struct ether_vlan_header *eh;
192 eh = mtod(m_head, struct ether_vlan_header*);
193 if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
194 ether_len = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
195 ether_type = eh->evl_proto;
197 ether_len = ETHER_HDR_LEN;
198 ether_type = eh->evl_encap_proto;
201 switch (ntohs(ether_type)) {
204 ip6 = (struct ip6_hdr *)(m_head->m_data + ether_len);
206 if (IPPROTO_TCP == ip6->ip6_nxt) {
207 ret_val = TRANSPORT_TYPE_IPV6_TCP;
208 } else if (IPPROTO_UDP == ip6->ip6_nxt) {
209 ret_val = TRANSPORT_TYPE_IPV6_UDP;
215 iph = (struct ip *)(m_head->m_data + ether_len);
217 if (IPPROTO_TCP == iph->ip_p) {
218 ret_val = TRANSPORT_TYPE_IPV4_TCP;
219 } else if (IPPROTO_UDP == iph->ip_p) {
220 ret_val = TRANSPORT_TYPE_IPV4_UDP;
225 ret_val = TRANSPORT_TYPE_NOT_IP;
233 * NetVsc driver initialization
234 * Note: Filter init is no longer required
237 netvsc_drv_init(void)
243 * NetVsc global initialization entry point
249 printf("Netvsc initializing... ");
252 * XXXKYS: cleanup initialization
254 if (!cold && !g_netvsc_drv.drv_inited) {
255 g_netvsc_drv.drv_inited = 1;
259 } else if (bootverbose)
260 printf("Already initialized!\n");
263 /* {F8615163-DF3E-46c5-913F-F2D2F965ED0E} */
264 static const hv_guid g_net_vsc_device_type = {
265 .data = {0x63, 0x51, 0x61, 0xF8, 0x3E, 0xDF, 0xc5, 0x46,
266 0x91, 0x3F, 0xF2, 0xD2, 0xF9, 0x65, 0xED, 0x0E}
270 * Standard probe entry point.
274 netvsc_probe(device_t dev)
278 p = vmbus_get_type(dev);
279 if (!memcmp(p, &g_net_vsc_device_type.data, sizeof(hv_guid))) {
280 device_set_desc(dev, "Synthetic Network Interface");
282 printf("Netvsc probe... DONE \n");
284 return (BUS_PROBE_DEFAULT);
291 * Standard attach entry point.
293 * Called when the driver is loaded. It allocates needed resources,
294 * and initializes the "hardware" and software.
297 netvsc_attach(device_t dev)
299 struct hv_device *device_ctx = vmbus_get_devctx(dev);
300 netvsc_device_info device_info;
302 int unit = device_get_unit(dev);
308 sc = device_get_softc(dev);
313 bzero(sc, sizeof(hn_softc_t));
317 NV_LOCK_INIT(sc, "NetVSCLock");
319 sc->hn_dev_obj = device_ctx;
321 ifp = sc->hn_ifp = if_alloc(IFT_ETHER);
324 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
325 ifp->if_dunit = unit;
326 ifp->if_dname = NETVSC_DEVNAME;
328 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
329 ifp->if_ioctl = hn_ioctl;
330 ifp->if_start = hn_start;
331 ifp->if_init = hn_ifinit;
332 /* needed by hv_rf_on_device_add() code */
333 ifp->if_mtu = ETHERMTU;
334 IFQ_SET_MAXLEN(&ifp->if_snd, 512);
335 ifp->if_snd.ifq_drv_maxlen = 511;
336 IFQ_SET_READY(&ifp->if_snd);
339 * Tell upper layers that we support full VLAN capability.
341 ifp->if_hdrlen = sizeof(struct ether_vlan_header);
342 ifp->if_capabilities |=
343 IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU | IFCAP_HWCSUM | IFCAP_TSO;
345 IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU | IFCAP_HWCSUM | IFCAP_TSO;
346 ifp->if_hwassist = CSUM_TCP | CSUM_UDP | CSUM_TSO;
348 ret = hv_rf_on_device_add(device_ctx, &device_info);
354 if (device_info.link_state == 0) {
358 ether_ifattach(ifp, device_info.mac_addr);
364 * Standard detach entry point
367 netvsc_detach(device_t dev)
369 struct hv_device *hv_device = vmbus_get_devctx(dev);
372 printf("netvsc_detach\n");
375 * XXXKYS: Need to clean up all our
376 * driver state; this is the driver
381 * XXXKYS: Need to stop outgoing traffic and unregister
385 hv_rf_on_device_remove(hv_device, HV_RF_NV_DESTROY_CHANNEL);
391 * Standard shutdown entry point
394 netvsc_shutdown(device_t dev)
400 * Send completion processing
402 * Note: It looks like offset 0 of buf is reserved to hold the softc
403 * pointer. The sc pointer is not currently needed in this function, and
404 * it is not presently populated by the TX function.
407 netvsc_xmit_completion(void *context)
409 netvsc_packet *packet = (netvsc_packet *)context;
413 mb = (struct mbuf *)(uintptr_t)packet->compl.send.send_completion_tid;
414 buf = ((uint8_t *)packet) - HV_NV_PACKET_OFFSET_IN_BUF;
424 * Start a transmit of one or more packets
427 hn_start_locked(struct ifnet *ifp)
429 hn_softc_t *sc = ifp->if_softc;
430 struct hv_device *device_ctx = vmbus_get_devctx(sc->hn_dev);
431 netvsc_dev *net_dev = sc->net_dev;
432 device_t dev = device_ctx->device;
434 netvsc_packet *packet;
435 struct mbuf *m_head, *m;
436 struct mbuf *mc_head = NULL;
437 struct ether_vlan_header *eh;
438 rndis_msg *rndis_mesg;
439 rndis_packet *rndis_pkt;
440 rndis_per_packet_info *rppi;
441 ndis_8021q_info *rppi_vlan_info;
442 rndis_tcp_ip_csum_info *csum_info;
443 rndis_tcp_tso_info *tso_info;
450 uint32_t rndis_msg_size = 0;
451 uint32_t trans_proto_type;
452 uint32_t send_buf_section_idx =
453 NVSP_1_CHIMNEY_SEND_INVALID_SECTION_INDEX;
455 while (!IFQ_DRV_IS_EMPTY(&sc->hn_ifp->if_snd)) {
456 IFQ_DRV_DEQUEUE(&sc->hn_ifp->if_snd, m_head);
457 if (m_head == NULL) {
464 /* Walk the mbuf list computing total length and num frags */
465 for (m = m_head; m != NULL; m = m->m_next) {
473 * Reserve the number of pages requested. Currently,
474 * one page is reserved for the message in the RNDIS
477 num_frags += HV_RF_NUM_TX_RESERVED_PAGE_BUFS;
479 /* If exceeds # page_buffers in netvsc_packet */
480 if (num_frags > NETVSC_PACKET_MAXPAGE) {
481 device_printf(dev, "exceed max page buffers,%d,%d\n",
482 num_frags, NETVSC_PACKET_MAXPAGE);
484 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
489 * Allocate a buffer with space for a netvsc packet plus a
490 * number of reserved areas. First comes a (currently 16
491 * bytes, currently unused) reserved data area. Second is
492 * the netvsc_packet. Third is an area reserved for an
493 * rndis_filter_packet struct. Fourth (optional) is a
494 * rndis_per_packet_info struct.
495 * Changed malloc to M_NOWAIT to avoid sleep under spin lock.
496 * No longer reserving extra space for page buffers, as they
497 * are already part of the netvsc_packet.
499 buf = malloc(HV_NV_PACKET_OFFSET_IN_BUF +
500 sizeof(netvsc_packet) +
502 RNDIS_VLAN_PPI_SIZE +
505 M_NETVSC, M_ZERO | M_NOWAIT);
507 device_printf(dev, "hn:malloc packet failed\n");
509 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
513 packet = (netvsc_packet *)(buf + HV_NV_PACKET_OFFSET_IN_BUF);
514 *(vm_offset_t *)buf = HV_NV_SC_PTR_OFFSET_IN_BUF;
516 packet->is_data_pkt = TRUE;
518 /* Set up the rndis header */
519 packet->page_buf_count = num_frags;
521 /* Initialize it from the mbuf */
522 packet->tot_data_buf_len = len;
525 * extension points to the area reserved for the
526 * rndis_filter_packet, which is placed just after
527 * the netvsc_packet (and rppi struct, if present;
528 * length is updated later).
530 packet->rndis_mesg = packet + 1;
531 rndis_mesg = (rndis_msg *)packet->rndis_mesg;
532 rndis_mesg->ndis_msg_type = REMOTE_NDIS_PACKET_MSG;
534 rndis_pkt = &rndis_mesg->msg.packet;
535 rndis_pkt->data_offset = sizeof(rndis_packet);
536 rndis_pkt->data_length = packet->tot_data_buf_len;
537 rndis_pkt->per_pkt_info_offset = sizeof(rndis_packet);
539 rndis_msg_size = RNDIS_MESSAGE_SIZE(rndis_packet);
542 * If the Hyper-V infrastructure needs to embed a VLAN tag,
543 * initialize netvsc_packet and rppi struct values as needed.
545 if (m_head->m_flags & M_VLANTAG) {
547 * set up some additional fields so the Hyper-V infrastructure will stuff the VLAN tag
550 packet->vlan_tci = m_head->m_pkthdr.ether_vtag;
552 rndis_msg_size += RNDIS_VLAN_PPI_SIZE;
554 rppi = hv_set_rppi_data(rndis_mesg, RNDIS_VLAN_PPI_SIZE,
557 /* VLAN info immediately follows rppi struct */
558 rppi_vlan_info = (ndis_8021q_info *)((char*)rppi +
559 rppi->per_packet_info_offset);
560 /* FreeBSD does not support CFI or priority */
561 rppi_vlan_info->u1.s1.vlan_id =
562 packet->vlan_tci & 0xfff;
565 if (0 == m_head->m_pkthdr.csum_flags) {
569 eh = mtod(m_head, struct ether_vlan_header*);
570 if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
571 ether_len = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
573 ether_len = ETHER_HDR_LEN;
576 trans_proto_type = get_transport_proto_type(m_head);
577 if (TRANSPORT_TYPE_NOT_IP == trans_proto_type) {
582 * TSO packet needless to setup the send side checksum
585 if (m_head->m_pkthdr.csum_flags & CSUM_TSO) {
589 /* setup checksum offload */
590 rndis_msg_size += RNDIS_CSUM_PPI_SIZE;
591 rppi = hv_set_rppi_data(rndis_mesg, RNDIS_CSUM_PPI_SIZE,
593 csum_info = (rndis_tcp_ip_csum_info *)((char*)rppi +
594 rppi->per_packet_info_offset);
596 if (trans_proto_type & (TYPE_IPV4 << 16)) {
597 csum_info->xmit.is_ipv4 = 1;
599 csum_info->xmit.is_ipv6 = 1;
602 if (trans_proto_type & TYPE_TCP) {
603 csum_info->xmit.tcp_csum = 1;
604 csum_info->xmit.tcp_header_offset = 0;
605 } else if (trans_proto_type & TYPE_UDP) {
606 csum_info->xmit.udp_csum = 1;
612 /* setup TCP segmentation offload */
613 rndis_msg_size += RNDIS_TSO_PPI_SIZE;
614 rppi = hv_set_rppi_data(rndis_mesg, RNDIS_TSO_PPI_SIZE,
615 tcp_large_send_info);
617 tso_info = (rndis_tcp_tso_info *)((char *)rppi +
618 rppi->per_packet_info_offset);
619 tso_info->lso_v2_xmit.type =
620 RNDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE;
623 if (trans_proto_type & (TYPE_IPV4 << 16)) {
625 (struct ip *)(m_head->m_data + ether_len);
626 unsigned long iph_len = ip->ip_hl << 2;
628 (struct tcphdr *)((caddr_t)ip + iph_len);
630 tso_info->lso_v2_xmit.ip_version =
631 RNDIS_TCP_LARGE_SEND_OFFLOAD_IPV4;
635 th->th_sum = in_pseudo(ip->ip_src.s_addr,
640 #if defined(INET6) && defined(INET)
645 struct ip6_hdr *ip6 =
646 (struct ip6_hdr *)(m_head->m_data + ether_len);
647 struct tcphdr *th = (struct tcphdr *)(ip6 + 1);
649 tso_info->lso_v2_xmit.ip_version =
650 RNDIS_TCP_LARGE_SEND_OFFLOAD_IPV6;
652 th->th_sum = in6_cksum_pseudo(ip6, 0, IPPROTO_TCP, 0);
655 tso_info->lso_v2_xmit.tcp_header_offset = 0;
656 tso_info->lso_v2_xmit.mss = m_head->m_pkthdr.tso_segsz;
659 rndis_mesg->msg_len = packet->tot_data_buf_len + rndis_msg_size;
660 packet->tot_data_buf_len = rndis_mesg->msg_len;
662 /* send packet with send buffer */
663 if (packet->tot_data_buf_len < net_dev->send_section_size) {
664 send_buf_section_idx =
665 hv_nv_get_next_send_section(net_dev);
666 if (send_buf_section_idx !=
667 NVSP_1_CHIMNEY_SEND_INVALID_SECTION_INDEX) {
668 char *dest = ((char *)net_dev->send_buf +
669 send_buf_section_idx *
670 net_dev->send_section_size);
672 memcpy(dest, rndis_mesg, rndis_msg_size);
673 dest += rndis_msg_size;
674 for (m = m_head; m != NULL; m = m->m_next) {
677 (void *)mtod(m, vm_offset_t),
683 packet->send_buf_section_idx =
684 send_buf_section_idx;
685 packet->send_buf_section_size =
686 packet->tot_data_buf_len;
687 packet->page_buf_count = 0;
692 /* send packet with page buffer */
693 packet->page_buffers[0].pfn =
694 atop(hv_get_phys_addr(rndis_mesg));
695 packet->page_buffers[0].offset =
696 (unsigned long)rndis_mesg & PAGE_MASK;
697 packet->page_buffers[0].length = rndis_msg_size;
700 * Fill the page buffers with mbuf info starting at index
701 * HV_RF_NUM_TX_RESERVED_PAGE_BUFS.
703 i = HV_RF_NUM_TX_RESERVED_PAGE_BUFS;
704 for (m = m_head; m != NULL; m = m->m_next) {
707 vtophys(mtod(m, vm_offset_t));
708 packet->page_buffers[i].pfn =
710 packet->page_buffers[i].offset =
711 paddr & (PAGE_SIZE - 1);
712 packet->page_buffers[i].length = m->m_len;
717 packet->send_buf_section_idx =
718 NVSP_1_CHIMNEY_SEND_INVALID_SECTION_INDEX;
719 packet->send_buf_section_size = 0;
724 * If bpf, copy the mbuf chain. This is less expensive than
725 * it appears; the mbuf clusters are not copied, only their
726 * reference counts are incremented.
727 * Needed to avoid a race condition where the completion
728 * callback is invoked, freeing the mbuf chain, before the
729 * bpf_mtap code has a chance to run.
732 mc_head = m_copypacket(m_head, M_NOWAIT);
735 /* Set the completion routine */
736 packet->compl.send.on_send_completion = netvsc_xmit_completion;
737 packet->compl.send.send_completion_context = packet;
738 packet->compl.send.send_completion_tid = (uint64_t)(uintptr_t)m_head;
740 /* Removed critical_enter(), does not appear necessary */
741 ret = hv_nv_on_send(device_ctx, packet);
743 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
744 /* if bpf && mc_head, call bpf_mtap code */
746 ETHER_BPF_MTAP(ifp, mc_head);
754 IF_PREPEND(&ifp->if_snd, m_head);
755 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
758 * Null the mbuf pointer so the completion function
759 * does not free the mbuf chain. We just pushed the
760 * mbuf chain back on the if_snd queue.
762 packet->compl.send.send_completion_tid = 0;
765 * Release the resources since we will not get any
768 netvsc_xmit_completion(packet);
769 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
772 /* if bpf && mc_head, free the mbuf chain copy */
782 * Link up/down notification
785 netvsc_linkstatus_callback(struct hv_device *device_obj, uint32_t status)
787 hn_softc_t *sc = device_get_softc(device_obj->device);
801 * Append the specified data to the indicated mbuf chain,
802 * Extend the mbuf chain if the new data does not fit in
805 * This is a minor rewrite of m_append() from sys/kern/uipc_mbuf.c.
806 * There should be an equivalent in the kernel mbuf code,
807 * but there does not appear to be one yet.
809 * Differs from m_append() in that additional mbufs are
810 * allocated with cluster size MJUMPAGESIZE, and filled
813 * Return 1 if able to complete the job; otherwise 0.
816 hv_m_append(struct mbuf *m0, int len, c_caddr_t cp)
819 int remainder, space;
821 for (m = m0; m->m_next != NULL; m = m->m_next)
824 space = M_TRAILINGSPACE(m);
827 * Copy into available space.
829 if (space > remainder)
831 bcopy(cp, mtod(m, caddr_t) + m->m_len, space);
836 while (remainder > 0) {
838 * Allocate a new mbuf; could check space
839 * and allocate a cluster instead.
841 n = m_getjcl(M_NOWAIT, m->m_type, 0, MJUMPAGESIZE);
844 n->m_len = min(MJUMPAGESIZE, remainder);
845 bcopy(cp, mtod(n, caddr_t), n->m_len);
847 remainder -= n->m_len;
851 if (m0->m_flags & M_PKTHDR)
852 m0->m_pkthdr.len += len - remainder;
854 return (remainder == 0);
859 * Called when we receive a data packet from the "wire" on the
862 * Note: This is no longer used as a callback
865 netvsc_recv(struct hv_device *device_ctx, netvsc_packet *packet,
866 rndis_tcp_ip_csum_info *csum_info)
868 hn_softc_t *sc = (hn_softc_t *)device_get_softc(device_ctx->device);
871 device_t dev = device_ctx->device;
875 return (0); /* TODO: KYS how can this be! */
880 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
885 * Bail out if packet contains more data than configured MTU.
887 if (packet->tot_data_buf_len > (ifp->if_mtu + ETHER_HDR_LEN)) {
892 * Get an mbuf with a cluster. For packets 2K or less,
893 * get a standard 2K cluster. For anything larger, get a
894 * 4K cluster. Any buffers larger than 4K can cause problems
895 * if looped around to the Hyper-V TX channel, so avoid them.
899 if (packet->tot_data_buf_len > MCLBYTES) {
904 m_new = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, size);
907 device_printf(dev, "alloc mbuf failed.\n");
911 hv_m_append(m_new, packet->tot_data_buf_len,
914 m_new->m_pkthdr.rcvif = ifp;
916 /* receive side checksum offload */
917 m_new->m_pkthdr.csum_flags = 0;
918 if (NULL != csum_info) {
919 /* IP csum offload */
920 if (csum_info->receive.ip_csum_succeeded) {
921 m_new->m_pkthdr.csum_flags |=
922 (CSUM_IP_CHECKED | CSUM_IP_VALID);
925 /* TCP csum offload */
926 if (csum_info->receive.tcp_csum_succeeded) {
927 m_new->m_pkthdr.csum_flags |=
928 (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
929 m_new->m_pkthdr.csum_data = 0xffff;
933 if ((packet->vlan_tci != 0) &&
934 (ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0) {
935 m_new->m_pkthdr.ether_vtag = packet->vlan_tci;
936 m_new->m_flags |= M_VLANTAG;
940 * Note: Moved RX completion back to hv_nv_on_receive() so all
941 * messages (not just data messages) will trigger a response.
944 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
946 /* We're not holding the lock here, so don't release it */
947 (*ifp->if_input)(ifp, m_new);
953 * Rules for using sc->temp_unusable:
954 * 1. sc->temp_unusable can only be read or written while holding NV_LOCK()
955 * 2. code reading sc->temp_unusable under NV_LOCK(), and finding
956 * sc->temp_unusable set, must release NV_LOCK() and exit
957 * 3. to retain exclusive control of the interface,
958 * sc->temp_unusable must be set by code before releasing NV_LOCK()
959 * 4. only code setting sc->temp_unusable can clear sc->temp_unusable
960 * 5. code setting sc->temp_unusable must eventually clear sc->temp_unusable
964 * Standard ioctl entry point. Called when the user wants to configure
968 hn_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
970 hn_softc_t *sc = ifp->if_softc;
971 struct ifreq *ifr = (struct ifreq *)data;
973 struct ifaddr *ifa = (struct ifaddr *)data;
975 netvsc_device_info device_info;
976 struct hv_device *hn_dev;
984 if (ifa->ifa_addr->sa_family == AF_INET) {
985 ifp->if_flags |= IFF_UP;
986 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
988 arp_ifinit(ifp, ifa);
991 error = ether_ioctl(ifp, cmd, data);
994 hn_dev = vmbus_get_devctx(sc->hn_dev);
996 /* Check MTU value change */
997 if (ifp->if_mtu == ifr->ifr_mtu)
1000 if (ifr->ifr_mtu > NETVSC_MAX_CONFIGURABLE_MTU) {
1005 /* Obtain and record requested MTU */
1006 ifp->if_mtu = ifr->ifr_mtu;
1010 if (!sc->temp_unusable) {
1011 sc->temp_unusable = TRUE;
1015 if (retry_cnt > 0) {
1019 } while (retry_cnt > 0);
1021 if (retry_cnt == 0) {
1026 /* We must remove and add back the device to cause the new
1027 * MTU to take effect. This includes tearing down, but not
1028 * deleting the channel, then bringing it back up.
1030 error = hv_rf_on_device_remove(hn_dev, HV_RF_NV_RETAIN_CHANNEL);
1033 sc->temp_unusable = FALSE;
1037 error = hv_rf_on_device_add(hn_dev, &device_info);
1040 sc->temp_unusable = FALSE;
1045 hn_ifinit_locked(sc);
1048 sc->temp_unusable = FALSE;
1054 if (!sc->temp_unusable) {
1055 sc->temp_unusable = TRUE;
1059 if (retry_cnt > 0) {
1063 } while (retry_cnt > 0);
1065 if (retry_cnt == 0) {
1070 if (ifp->if_flags & IFF_UP) {
1072 * If only the state of the PROMISC flag changed,
1073 * then just use the 'set promisc mode' command
1074 * instead of reinitializing the entire NIC. Doing
1075 * a full re-init means reloading the firmware and
1076 * waiting for it to start up, which may take a
1080 /* Fixme: Promiscuous mode? */
1081 if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
1082 ifp->if_flags & IFF_PROMISC &&
1083 !(sc->hn_if_flags & IFF_PROMISC)) {
1084 /* do something here for Hyper-V */
1085 } else if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
1086 !(ifp->if_flags & IFF_PROMISC) &&
1087 sc->hn_if_flags & IFF_PROMISC) {
1088 /* do something here for Hyper-V */
1091 hn_ifinit_locked(sc);
1093 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1098 sc->temp_unusable = FALSE;
1100 sc->hn_if_flags = ifp->if_flags;
1104 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1105 if (mask & IFCAP_TXCSUM) {
1106 if (IFCAP_TXCSUM & ifp->if_capenable) {
1107 ifp->if_capenable &= ~IFCAP_TXCSUM;
1108 ifp->if_hwassist &= ~(CSUM_TCP | CSUM_UDP);
1110 ifp->if_capenable |= IFCAP_TXCSUM;
1111 ifp->if_hwassist |= (CSUM_TCP | CSUM_UDP);
1115 if (mask & IFCAP_RXCSUM) {
1116 if (IFCAP_RXCSUM & ifp->if_capenable) {
1117 ifp->if_capenable &= ~IFCAP_RXCSUM;
1119 ifp->if_capenable |= IFCAP_RXCSUM;
1123 if (mask & IFCAP_TSO4) {
1124 ifp->if_capenable ^= IFCAP_TSO4;
1125 ifp->if_hwassist ^= CSUM_IP_TSO;
1128 if (mask & IFCAP_TSO6) {
1129 ifp->if_capenable ^= IFCAP_TSO6;
1130 ifp->if_hwassist ^= CSUM_IP6_TSO;
1138 /* Fixme: Multicast mode? */
1139 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1141 netvsc_setmulti(sc);
1152 error = ether_ioctl(ifp, cmd, data);
1163 hn_stop(hn_softc_t *sc)
1167 struct hv_device *device_ctx = vmbus_get_devctx(sc->hn_dev);
1172 printf(" Closing Device ...\n");
1174 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1175 if_link_state_change(ifp, LINK_STATE_DOWN);
1176 sc->hn_initdone = 0;
1178 ret = hv_rf_on_close(device_ctx);
1182 * FreeBSD transmit entry point
1185 hn_start(struct ifnet *ifp)
1191 if (sc->temp_unusable) {
1195 hn_start_locked(ifp);
1203 hn_ifinit_locked(hn_softc_t *sc)
1206 struct hv_device *device_ctx = vmbus_get_devctx(sc->hn_dev);
1211 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1215 hv_promisc_mode = 1;
1217 ret = hv_rf_on_open(device_ctx);
1221 sc->hn_initdone = 1;
1223 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1224 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1225 if_link_state_change(ifp, LINK_STATE_UP);
1232 hn_ifinit(void *xsc)
1234 hn_softc_t *sc = xsc;
1237 if (sc->temp_unusable) {
1241 sc->temp_unusable = TRUE;
1244 hn_ifinit_locked(sc);
1247 sc->temp_unusable = FALSE;
1256 hn_watchdog(struct ifnet *ifp)
1261 printf("hn%d: watchdog timeout -- resetting\n", sc->hn_unit);
1262 hn_ifinit(sc); /*???*/
1263 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1267 static device_method_t netvsc_methods[] = {
1268 /* Device interface */
1269 DEVMETHOD(device_probe, netvsc_probe),
1270 DEVMETHOD(device_attach, netvsc_attach),
1271 DEVMETHOD(device_detach, netvsc_detach),
1272 DEVMETHOD(device_shutdown, netvsc_shutdown),
1277 static driver_t netvsc_driver = {
1283 static devclass_t netvsc_devclass;
1285 DRIVER_MODULE(hn, vmbus, netvsc_driver, netvsc_devclass, 0, 0);
1286 MODULE_VERSION(hn, 1);
1287 MODULE_DEPEND(hn, vmbus, 1, 1, 1);
1288 SYSINIT(netvsc_initx, SI_SUB_KTHREAD_IDLE, SI_ORDER_MIDDLE + 1, netvsc_init,