Home | History | Annotate | Line # | Download | only in hyperv
if_hvn.c revision 1.3.2.2
      1 /*	$NetBSD: if_hvn.c,v 1.3.2.2 2019/06/10 22:07:09 christos Exp $	*/
      2 /*	$OpenBSD: if_hvn.c,v 1.39 2018/03/11 14:31:34 mikeb Exp $	*/
      3 
      4 /*-
      5  * Copyright (c) 2009-2012,2016 Microsoft Corp.
      6  * Copyright (c) 2010-2012 Citrix Inc.
      7  * Copyright (c) 2012 NetApp Inc.
      8  * Copyright (c) 2016 Mike Belopuhov <mike (at) esdenera.com>
      9  * All rights reserved.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice unmodified, this list of conditions, and the following
     16  *    disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * The OpenBSD port was done under funding by Esdenera Networks GmbH.
     35  */
     36 
     37 #include <sys/cdefs.h>
     38 __KERNEL_RCSID(0, "$NetBSD: if_hvn.c,v 1.3.2.2 2019/06/10 22:07:09 christos Exp $");
     39 
     40 #ifdef _KERNEL_OPT
     41 #include "opt_inet.h"
     42 #include "opt_inet6.h"
     43 #include "opt_net_mpsafe.h"
     44 #endif
     45 
     46 #include <sys/param.h>
     47 #include <sys/systm.h>
     48 #include <sys/kernel.h>
     49 #include <sys/device.h>
     50 #include <sys/atomic.h>
     51 #include <sys/bus.h>
     52 #include <sys/intr.h>
     53 #include <sys/kmem.h>
     54 
     55 #include <net/if.h>
     56 #include <net/if_ether.h>
     57 #include <net/if_media.h>
     58 
     59 #include <net/bpf.h>
     60 
     61 #include <dev/ic/ndisreg.h>
     62 #include <dev/ic/rndisreg.h>
     63 
     64 #include <dev/hyperv/vmbusvar.h>
     65 #include <dev/hyperv/if_hvnreg.h>
     66 
     67 #ifndef EVL_PRIO_BITS
     68 #define EVL_PRIO_BITS	13
     69 #endif
     70 
     71 #define HVN_NVS_MSGSIZE			32
     72 #define HVN_NVS_BUFSIZE			PAGE_SIZE
     73 
     74 /*
     75  * RNDIS control interface
     76  */
     77 #define HVN_RNDIS_CTLREQS		4
     78 #define HVN_RNDIS_BUFSIZE		512
     79 
     80 struct rndis_cmd {
     81 	uint32_t			rc_id;
     82 	struct hvn_nvs_rndis		rc_msg;
     83 	void				*rc_req;
     84 	bus_dmamap_t			rc_dmap;
     85 	bus_dma_segment_t		rc_segs;
     86 	int				rc_nsegs;
     87 	uint64_t			rc_gpa;
     88 	struct rndis_packet_msg		rc_cmp;
     89 	uint32_t			rc_cmplen;
     90 	uint8_t				rc_cmpbuf[HVN_RNDIS_BUFSIZE];
     91 	int				rc_done;
     92 	TAILQ_ENTRY(rndis_cmd)		rc_entry;
     93 };
     94 TAILQ_HEAD(rndis_queue, rndis_cmd);
     95 
     96 #define HVN_MAXMTU			(9 * 1024)
     97 
     98 #define HVN_RNDIS_XFER_SIZE		2048
     99 
    100 /*
    101  * Tx ring
    102  */
    103 #define HVN_TX_DESC			256
    104 #define HVN_TX_FRAGS			15		/* 31 is the max */
    105 #define HVN_TX_FRAG_SIZE		PAGE_SIZE
    106 #define HVN_TX_PKT_SIZE			16384
    107 
    108 #define HVN_RNDIS_PKT_LEN					\
    109 	(sizeof(struct rndis_packet_msg) +			\
    110 	 sizeof(struct rndis_pktinfo) + NDIS_VLAN_INFO_SIZE +	\
    111 	 sizeof(struct rndis_pktinfo) + NDIS_TXCSUM_INFO_SIZE)
    112 
    113 struct hvn_tx_desc {
    114 	uint32_t			txd_id;
    115 	int				txd_ready;
    116 	struct vmbus_gpa		txd_sgl[HVN_TX_FRAGS + 1];
    117 	int				txd_nsge;
    118 	struct mbuf			*txd_buf;
    119 	bus_dmamap_t			txd_dmap;
    120 	struct vmbus_gpa		txd_gpa;
    121 	struct rndis_packet_msg		*txd_req;
    122 };
    123 
    124 struct hvn_softc {
    125 	device_t			sc_dev;
    126 
    127 	struct vmbus_softc		*sc_vmbus;
    128 	struct vmbus_channel		*sc_chan;
    129 	bus_dma_tag_t			sc_dmat;
    130 
    131 	struct ethercom			sc_ec;
    132 	struct ifmedia			sc_media;
    133 	struct if_percpuq		*sc_ipq;
    134 	int				sc_link_state;
    135 	int				sc_promisc;
    136 
    137 	uint32_t			sc_flags;
    138 #define	HVN_SCF_ATTACHED	__BIT(0)
    139 
    140 	/* NVS protocol */
    141 	int				sc_proto;
    142 	uint32_t			sc_nvstid;
    143 	uint8_t				sc_nvsrsp[HVN_NVS_MSGSIZE];
    144 	uint8_t				*sc_nvsbuf;
    145 	int				sc_nvsdone;
    146 
    147 	/* RNDIS protocol */
    148 	int				sc_ndisver;
    149 	uint32_t			sc_rndisrid;
    150 	struct rndis_queue		sc_cntl_sq; /* submission queue */
    151 	kmutex_t			sc_cntl_sqlck;
    152 	struct rndis_queue		sc_cntl_cq; /* completion queue */
    153 	kmutex_t			sc_cntl_cqlck;
    154 	struct rndis_queue		sc_cntl_fq; /* free queue */
    155 	kmutex_t			sc_cntl_fqlck;
    156 	struct rndis_cmd		sc_cntl_msgs[HVN_RNDIS_CTLREQS];
    157 	struct hvn_nvs_rndis		sc_data_msg;
    158 
    159 	/* Rx ring */
    160 	uint8_t				*sc_rx_ring;
    161 	int				sc_rx_size;
    162 	uint32_t			sc_rx_hndl;
    163 	struct hyperv_dma		sc_rx_dma;
    164 
    165 	/* Tx ring */
    166 	uint32_t			sc_tx_next;
    167 	uint32_t			sc_tx_avail;
    168 	struct hvn_tx_desc		sc_tx_desc[HVN_TX_DESC];
    169 	bus_dmamap_t			sc_tx_rmap;
    170 	uint8_t				*sc_tx_msgs;
    171 	bus_dma_segment_t		sc_tx_mseg;
    172 };
    173 
    174 #define SC2IFP(_sc_)	(&(_sc_)->sc_ec.ec_if)
    175 #define IFP2SC(_ifp_)	((_ifp_)->if_softc)
    176 
    177 
    178 static int	hvn_match(device_t, cfdata_t, void *);
    179 static void	hvn_attach(device_t, device_t, void *);
    180 static int	hvn_detach(device_t, int);
    181 
    182 CFATTACH_DECL_NEW(hvn, sizeof(struct hvn_softc),
    183     hvn_match, hvn_attach, hvn_detach, NULL);
    184 
    185 static int	hvn_ioctl(struct ifnet *, u_long, void *);
    186 static int	hvn_media_change(struct ifnet *);
    187 static void	hvn_media_status(struct ifnet *, struct ifmediareq *);
    188 static int	hvn_iff(struct hvn_softc *);
    189 static int	hvn_init(struct ifnet *);
    190 static void	hvn_stop(struct ifnet *, int);
    191 static void	hvn_start(struct ifnet *);
    192 static int	hvn_encap(struct hvn_softc *, struct mbuf *,
    193 		    struct hvn_tx_desc **);
    194 static void	hvn_decap(struct hvn_softc *, struct hvn_tx_desc *);
    195 static void	hvn_txeof(struct hvn_softc *, uint64_t);
    196 static int	hvn_rx_ring_create(struct hvn_softc *);
    197 static int	hvn_rx_ring_destroy(struct hvn_softc *);
    198 static int	hvn_tx_ring_create(struct hvn_softc *);
    199 static void	hvn_tx_ring_destroy(struct hvn_softc *);
    200 static int	hvn_set_capabilities(struct hvn_softc *);
    201 static int	hvn_get_lladdr(struct hvn_softc *, uint8_t *);
    202 static void	hvn_get_link_status(struct hvn_softc *);
    203 
    204 /* NSVP */
    205 static int	hvn_nvs_attach(struct hvn_softc *);
    206 static void	hvn_nvs_intr(void *);
    207 static int	hvn_nvs_cmd(struct hvn_softc *, void *, size_t, uint64_t, int);
    208 static int	hvn_nvs_ack(struct hvn_softc *, uint64_t);
    209 static void	hvn_nvs_detach(struct hvn_softc *);
    210 
    211 /* RNDIS */
    212 static int	hvn_rndis_attach(struct hvn_softc *);
    213 static int	hvn_rndis_cmd(struct hvn_softc *, struct rndis_cmd *, int);
    214 static void	hvn_rndis_input(struct hvn_softc *, uint64_t, void *);
    215 static void	hvn_rxeof(struct hvn_softc *, uint8_t *, uint32_t);
    216 static void	hvn_rndis_complete(struct hvn_softc *, uint8_t *, uint32_t);
    217 static int	hvn_rndis_output(struct hvn_softc *, struct hvn_tx_desc *);
    218 static void	hvn_rndis_status(struct hvn_softc *, uint8_t *, uint32_t);
    219 static int	hvn_rndis_query(struct hvn_softc *, uint32_t, void *, size_t *);
    220 static int	hvn_rndis_set(struct hvn_softc *, uint32_t, void *, size_t);
    221 static int	hvn_rndis_open(struct hvn_softc *);
    222 static int	hvn_rndis_close(struct hvn_softc *);
    223 static void	hvn_rndis_detach(struct hvn_softc *);
    224 
    225 static int
    226 hvn_match(device_t parent, cfdata_t match, void *aux)
    227 {
    228 	struct vmbus_attach_args *aa = aux;
    229 
    230 	if (memcmp(aa->aa_type, &hyperv_guid_network, sizeof(*aa->aa_type)))
    231 		return 0;
    232 	return 1;
    233 }
    234 
    235 static void
    236 hvn_attach(device_t parent, device_t self, void *aux)
    237 {
    238 	struct hvn_softc *sc = device_private(self);
    239 	struct vmbus_attach_args *aa = aux;
    240 	struct ifnet *ifp = SC2IFP(sc);
    241 	uint8_t enaddr[ETHER_ADDR_LEN];
    242 	int error;
    243 
    244 	sc->sc_dev = self;
    245 	sc->sc_vmbus = (struct vmbus_softc *)device_private(parent);
    246 	sc->sc_chan = aa->aa_chan;
    247 	sc->sc_dmat = sc->sc_vmbus->sc_dmat;
    248 
    249 	aprint_naive("\n");
    250 	aprint_normal(": Hyper-V NetVSC\n");
    251 
    252 	strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
    253 
    254 	if (hvn_nvs_attach(sc)) {
    255 		aprint_error_dev(self, "failed to init NVSP\n");
    256 		return;
    257 	}
    258 
    259 	if (hvn_rx_ring_create(sc)) {
    260 		aprint_error_dev(self, "failed to create Rx ring\n");
    261 		goto fail1;
    262 	}
    263 
    264 	if (hvn_tx_ring_create(sc)) {
    265 		aprint_error_dev(self, "failed to create Tx ring\n");
    266 		goto fail1;
    267 	}
    268 
    269 	ifp->if_softc = sc;
    270 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    271 	ifp->if_ioctl = hvn_ioctl;
    272 	ifp->if_start = hvn_start;
    273 	ifp->if_init = hvn_init;
    274 	ifp->if_stop = hvn_stop;
    275 	ifp->if_capabilities = IFCAP_CSUM_IPv4_Tx | IFCAP_CSUM_IPv4_Rx;
    276 	ifp->if_capabilities |= IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_TCPv4_Rx;
    277 	ifp->if_capabilities |= IFCAP_CSUM_TCPv6_Tx | IFCAP_CSUM_TCPv6_Rx;
    278 	if (sc->sc_ndisver > NDIS_VERSION_6_30) {
    279 		ifp->if_capabilities |= IFCAP_CSUM_UDPv4_Tx;
    280 		ifp->if_capabilities |= IFCAP_CSUM_UDPv4_Rx;
    281 		ifp->if_capabilities |= IFCAP_CSUM_UDPv6_Tx;
    282 		ifp->if_capabilities |= IFCAP_CSUM_UDPv6_Rx;
    283 	}
    284 	if (sc->sc_proto >= HVN_NVS_PROTO_VERSION_2) {
    285 		sc->sc_ec.ec_capabilities |= ETHERCAP_VLAN_HWTAGGING;
    286 		sc->sc_ec.ec_capabilities |= ETHERCAP_VLAN_MTU;
    287 	}
    288 
    289 	IFQ_SET_MAXLEN(&ifp->if_snd, HVN_TX_DESC - 1);
    290 	IFQ_SET_READY(&ifp->if_snd);
    291 
    292 	/* Initialize ifmedia structures. */
    293 	sc->sc_ec.ec_ifmedia = &sc->sc_media;
    294 	ifmedia_init(&sc->sc_media, IFM_IMASK, hvn_media_change,
    295 	    hvn_media_status);
    296 	ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_MANUAL, 0, NULL);
    297 	ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_MANUAL);
    298 
    299 	error = if_initialize(ifp);
    300 	if (error) {
    301 		aprint_error_dev(self, "if_initialize failed(%d)\n", error);
    302 		goto fail2;
    303 	}
    304 	sc->sc_ipq = if_percpuq_create(ifp);
    305 	if_deferred_start_init(ifp, NULL);
    306 
    307 	if (hvn_rndis_attach(sc)) {
    308 		aprint_error_dev(self, "failed to init RNDIS\n");
    309 		goto fail1;
    310 	}
    311 
    312 	aprint_normal_dev(self, "NVS %d.%d NDIS %d.%d\n",
    313 	    sc->sc_proto >> 16, sc->sc_proto & 0xffff,
    314 	    sc->sc_ndisver >> 16 , sc->sc_ndisver & 0xffff);
    315 
    316 	if (hvn_set_capabilities(sc)) {
    317 		aprint_error_dev(self, "failed to setup offloading\n");
    318 		goto fail2;
    319 	}
    320 
    321 	if (hvn_get_lladdr(sc, enaddr)) {
    322 		aprint_error_dev(self,
    323 		    "failed to obtain an ethernet address\n");
    324 		goto fail2;
    325 	}
    326 	aprint_normal_dev(self, "Ethernet address %s\n", ether_sprintf(enaddr));
    327 
    328 	ether_ifattach(ifp, enaddr);
    329 	if_register(ifp);
    330 
    331 	if (pmf_device_register(self, NULL, NULL))
    332 		pmf_class_network_register(self, ifp);
    333 	else
    334 		aprint_error_dev(self, "couldn't establish power handler\n");
    335 
    336 	SET(sc->sc_flags, HVN_SCF_ATTACHED);
    337 	return;
    338 
    339 fail2:	hvn_rndis_detach(sc);
    340 fail1:	hvn_rx_ring_destroy(sc);
    341 	hvn_tx_ring_destroy(sc);
    342 	hvn_nvs_detach(sc);
    343 }
    344 
    345 static int
    346 hvn_detach(device_t self, int flags)
    347 {
    348 	struct hvn_softc *sc = device_private(self);
    349 	struct ifnet *ifp = SC2IFP(sc);
    350 
    351 	if (!ISSET(sc->sc_flags, HVN_SCF_ATTACHED))
    352 		return 0;
    353 
    354 	hvn_stop(ifp, 1);
    355 
    356 	pmf_device_deregister(self);
    357 
    358 	ether_ifdetach(ifp);
    359 	if_detach(ifp);
    360 	if_percpuq_destroy(sc->sc_ipq);
    361 
    362 	hvn_rndis_detach(sc);
    363 	hvn_rx_ring_destroy(sc);
    364 	hvn_tx_ring_destroy(sc);
    365 	hvn_nvs_detach(sc);
    366 
    367 	return 0;
    368 }
    369 
    370 static int
    371 hvn_ioctl(struct ifnet *ifp, u_long command, void * data)
    372 {
    373 	struct hvn_softc *sc = IFP2SC(ifp);
    374 	int s, error = 0;
    375 
    376 	s = splnet();
    377 
    378 	switch (command) {
    379 	case SIOCSIFFLAGS:
    380 		if (ifp->if_flags & IFF_UP) {
    381 			if (ifp->if_flags & IFF_RUNNING)
    382 				error = ENETRESET;
    383 			else {
    384 				error = hvn_init(ifp);
    385 				if (error)
    386 					ifp->if_flags &= ~IFF_UP;
    387 			}
    388 		} else {
    389 			if (ifp->if_flags & IFF_RUNNING)
    390 				hvn_stop(ifp, 1);
    391 		}
    392 		break;
    393 	default:
    394 		error = ether_ioctl(ifp, command, data);
    395 		break;
    396 	}
    397 
    398 	if (error == ENETRESET) {
    399 		if (ifp->if_flags & IFF_RUNNING)
    400 			hvn_iff(sc);
    401 		error = 0;
    402 	}
    403 
    404 	splx(s);
    405 
    406 	return error;
    407 }
    408 
    409 static int
    410 hvn_media_change(struct ifnet *ifp)
    411 {
    412 
    413 	return 0;
    414 }
    415 
    416 static void
    417 hvn_media_status(struct ifnet *ifp, struct ifmediareq *ifmr)
    418 {
    419 	struct hvn_softc *sc = IFP2SC(ifp);
    420 	int link_state;
    421 
    422 	link_state = sc->sc_link_state;
    423 	hvn_get_link_status(sc);
    424 	if (link_state != sc->sc_link_state)
    425 		if_link_state_change(ifp, sc->sc_link_state);
    426 
    427 	ifmr->ifm_status = IFM_AVALID;
    428 	ifmr->ifm_active = IFM_ETHER | IFM_MANUAL;
    429 	if (sc->sc_link_state == LINK_STATE_UP)
    430 		ifmr->ifm_status |= IFM_ACTIVE;
    431 }
    432 
    433 static int
    434 hvn_iff(struct hvn_softc *sc)
    435 {
    436 
    437 	/* XXX */
    438 	sc->sc_promisc = 0;
    439 
    440 	return 0;
    441 }
    442 
    443 static int
    444 hvn_init(struct ifnet *ifp)
    445 {
    446 	struct hvn_softc *sc = IFP2SC(ifp);
    447 	int error;
    448 
    449 	hvn_stop(ifp, 0);
    450 
    451 	error = hvn_iff(sc);
    452 	if (error)
    453 		return error;
    454 
    455 	error = hvn_rndis_open(sc);
    456 	if (error == 0) {
    457 		ifp->if_flags |= IFF_RUNNING;
    458 		ifp->if_flags &= ~IFF_OACTIVE;
    459 	}
    460 	return error;
    461 }
    462 
    463 static void
    464 hvn_stop(struct ifnet *ifp, int disable)
    465 {
    466 	struct hvn_softc *sc = IFP2SC(ifp);
    467 
    468 	hvn_rndis_close(sc);
    469 
    470 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
    471 }
    472 
    473 static void
    474 hvn_start(struct ifnet *ifp)
    475 {
    476 	struct hvn_softc *sc = IFP2SC(ifp);
    477 	struct hvn_tx_desc *txd;
    478 	struct mbuf *m;
    479 
    480 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
    481 		return;
    482 
    483 	for (;;) {
    484 		if (!sc->sc_tx_avail) {
    485 			/* transient */
    486 			ifp->if_flags |= IFF_OACTIVE;
    487 			break;
    488 		}
    489 
    490 		IFQ_DEQUEUE(&ifp->if_snd, m);
    491 		if (m == NULL)
    492 			break;
    493 
    494 		if (hvn_encap(sc, m, &txd)) {
    495 			/* the chain is too large */
    496 			ifp->if_oerrors++;
    497 			m_freem(m);
    498 			continue;
    499 		}
    500 
    501 		bpf_mtap(ifp, m, BPF_D_OUT);
    502 
    503 		if (hvn_rndis_output(sc, txd)) {
    504 			hvn_decap(sc, txd);
    505 			ifp->if_oerrors++;
    506 			m_freem(m);
    507 			continue;
    508 		}
    509 
    510 		sc->sc_tx_next++;
    511 	}
    512 }
    513 
    514 static inline char *
    515 hvn_rndis_pktinfo_append(struct rndis_packet_msg *pkt, size_t pktsize,
    516     size_t datalen, uint32_t type)
    517 {
    518 	struct rndis_pktinfo *pi;
    519 	size_t pi_size = sizeof(*pi) + datalen;
    520 	char *cp;
    521 
    522 	KASSERT(pkt->rm_pktinfooffset + pkt->rm_pktinfolen + pi_size <=
    523 	    pktsize);
    524 
    525 	cp = (char *)pkt + pkt->rm_pktinfooffset + pkt->rm_pktinfolen;
    526 	pi = (struct rndis_pktinfo *)cp;
    527 	pi->rm_size = pi_size;
    528 	pi->rm_type = type;
    529 	pi->rm_pktinfooffset = sizeof(*pi);
    530 	pkt->rm_pktinfolen += pi_size;
    531 	pkt->rm_dataoffset += pi_size;
    532 	pkt->rm_len += pi_size;
    533 
    534 	return (char *)pi->rm_data;
    535 }
    536 
    537 static int
    538 hvn_encap(struct hvn_softc *sc, struct mbuf *m, struct hvn_tx_desc **txd0)
    539 {
    540 	struct hvn_tx_desc *txd;
    541 	struct rndis_packet_msg *pkt;
    542 	bus_dma_segment_t *seg;
    543 	size_t pktlen;
    544 	int i, rv;
    545 
    546 	do {
    547 		txd = &sc->sc_tx_desc[sc->sc_tx_next % HVN_TX_DESC];
    548 		sc->sc_tx_next++;
    549 	} while (!txd->txd_ready);
    550 	txd->txd_ready = 0;
    551 
    552 	pkt = txd->txd_req;
    553 	memset(pkt, 0, HVN_RNDIS_PKT_LEN);
    554 	pkt->rm_type = REMOTE_NDIS_PACKET_MSG;
    555 	pkt->rm_len = sizeof(*pkt) + m->m_pkthdr.len;
    556 	pkt->rm_dataoffset = RNDIS_DATA_OFFSET;
    557 	pkt->rm_datalen = m->m_pkthdr.len;
    558 	pkt->rm_pktinfooffset = sizeof(*pkt); /* adjusted below */
    559 	pkt->rm_pktinfolen = 0;
    560 
    561 	rv = bus_dmamap_load_mbuf(sc->sc_dmat, txd->txd_dmap, m, BUS_DMA_READ |
    562 	    BUS_DMA_NOWAIT);
    563 	switch (rv) {
    564 	case 0:
    565 		break;
    566 	case EFBIG:
    567 		if (m_defrag(m, M_NOWAIT) == 0 &&
    568 		    bus_dmamap_load_mbuf(sc->sc_dmat, txd->txd_dmap, m,
    569 		      BUS_DMA_READ | BUS_DMA_NOWAIT) == 0)
    570 			break;
    571 		/* FALLTHROUGH */
    572 	default:
    573 		DPRINTF("%s: failed to load mbuf\n", device_xname(sc->sc_dev));
    574 		return -1;
    575 	}
    576 	txd->txd_buf = m;
    577 
    578 	if (m->m_flags & M_VLANTAG) {
    579 		uint32_t vlan;
    580 		char *cp;
    581 
    582 		vlan = NDIS_VLAN_INFO_MAKE(
    583 		    EVL_VLANOFTAG(m->m_pkthdr.ether_vtag),
    584 		    EVL_PRIOFTAG(m->m_pkthdr.ether_vtag), 0);
    585 		cp = hvn_rndis_pktinfo_append(pkt, HVN_RNDIS_PKT_LEN,
    586 		    NDIS_VLAN_INFO_SIZE, NDIS_PKTINFO_TYPE_VLAN);
    587 		memcpy(cp, &vlan, NDIS_VLAN_INFO_SIZE);
    588 	}
    589 
    590 	if (m->m_pkthdr.csum_flags & (M_CSUM_IPv4 | M_CSUM_UDPv4 |
    591 	    M_CSUM_TCPv4)) {
    592 		uint32_t csum = NDIS_TXCSUM_INFO_IPV4;
    593 		char *cp;
    594 
    595 		if (m->m_pkthdr.csum_flags & M_CSUM_IPv4)
    596 			csum |= NDIS_TXCSUM_INFO_IPCS;
    597 		if (m->m_pkthdr.csum_flags & M_CSUM_TCPv4)
    598 			csum |= NDIS_TXCSUM_INFO_TCPCS;
    599 		if (m->m_pkthdr.csum_flags & M_CSUM_UDPv4)
    600 			csum |= NDIS_TXCSUM_INFO_UDPCS;
    601 		cp = hvn_rndis_pktinfo_append(pkt, HVN_RNDIS_PKT_LEN,
    602 		    NDIS_TXCSUM_INFO_SIZE, NDIS_PKTINFO_TYPE_CSUM);
    603 		memcpy(cp, &csum, NDIS_TXCSUM_INFO_SIZE);
    604 	}
    605 
    606 	pktlen = pkt->rm_pktinfooffset + pkt->rm_pktinfolen;
    607 	pkt->rm_pktinfooffset -= RNDIS_HEADER_OFFSET;
    608 
    609 	/* Attach an RNDIS message to the first slot */
    610 	txd->txd_sgl[0].gpa_page = txd->txd_gpa.gpa_page;
    611 	txd->txd_sgl[0].gpa_ofs = txd->txd_gpa.gpa_ofs;
    612 	txd->txd_sgl[0].gpa_len = pktlen;
    613 	txd->txd_nsge = txd->txd_dmap->dm_nsegs + 1;
    614 
    615 	for (i = 0; i < txd->txd_dmap->dm_nsegs; i++) {
    616 		seg = &txd->txd_dmap->dm_segs[i];
    617 		txd->txd_sgl[1 + i].gpa_page = atop(seg->ds_addr);
    618 		txd->txd_sgl[1 + i].gpa_ofs = seg->ds_addr & PAGE_MASK;
    619 		txd->txd_sgl[1 + i].gpa_len = seg->ds_len;
    620 	}
    621 
    622 	*txd0 = txd;
    623 
    624 	atomic_dec_uint(&sc->sc_tx_avail);
    625 
    626 	return 0;
    627 }
    628 
    629 static void
    630 hvn_decap(struct hvn_softc *sc, struct hvn_tx_desc *txd)
    631 {
    632 	struct ifnet *ifp = SC2IFP(sc);
    633 
    634 	bus_dmamap_sync(sc->sc_dmat, txd->txd_dmap, 0, 0,
    635 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
    636 	bus_dmamap_unload(sc->sc_dmat, txd->txd_dmap);
    637 	txd->txd_buf = NULL;
    638 	txd->txd_nsge = 0;
    639 	txd->txd_ready = 1;
    640 	atomic_inc_uint(&sc->sc_tx_avail);
    641 	ifp->if_flags &= ~IFF_OACTIVE;
    642 }
    643 
    644 static void
    645 hvn_txeof(struct hvn_softc *sc, uint64_t tid)
    646 {
    647 	struct ifnet *ifp = SC2IFP(sc);
    648 	struct hvn_tx_desc *txd;
    649 	struct mbuf *m;
    650 	uint32_t id = tid >> 32;
    651 
    652 	if ((tid & 0xffffffffU) != 0)
    653 		return;
    654 
    655 	id -= HVN_NVS_CHIM_SIG;
    656 	if (id >= HVN_TX_DESC) {
    657 		device_printf(sc->sc_dev, "tx packet index too large: %u", id);
    658 		return;
    659 	}
    660 
    661 	txd = &sc->sc_tx_desc[id];
    662 
    663 	if ((m = txd->txd_buf) == NULL) {
    664 		device_printf(sc->sc_dev, "no mbuf @%u\n", id);
    665 		return;
    666 	}
    667 	txd->txd_buf = NULL;
    668 
    669 	bus_dmamap_sync(sc->sc_dmat, txd->txd_dmap, 0, 0,
    670 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
    671 	bus_dmamap_unload(sc->sc_dmat, txd->txd_dmap);
    672 	m_freem(m);
    673 	ifp->if_opackets++;
    674 
    675 	txd->txd_ready = 1;
    676 
    677 	atomic_inc_uint(&sc->sc_tx_avail);
    678 	ifp->if_flags &= ~IFF_OACTIVE;
    679 }
    680 
    681 static int
    682 hvn_rx_ring_create(struct hvn_softc *sc)
    683 {
    684 	struct hvn_nvs_rxbuf_conn cmd;
    685 	struct hvn_nvs_rxbuf_conn_resp *rsp;
    686 	uint64_t tid;
    687 
    688 	if (sc->sc_proto <= HVN_NVS_PROTO_VERSION_2)
    689 		sc->sc_rx_size = 15 * 1024 * 1024;	/* 15MB */
    690 	else
    691 		sc->sc_rx_size = 16 * 1024 * 1024; 	/* 16MB */
    692 	sc->sc_rx_ring = hyperv_dma_alloc(sc->sc_dmat, &sc->sc_rx_dma,
    693 	    sc->sc_rx_size, PAGE_SIZE, PAGE_SIZE, sc->sc_rx_size / PAGE_SIZE);
    694 	if (sc->sc_rx_ring == NULL) {
    695 		DPRINTF("%s: failed to allocate Rx ring buffer\n",
    696 		    device_xname(sc->sc_dev));
    697 		return -1;
    698 	}
    699 	if (vmbus_handle_alloc(sc->sc_chan, &sc->sc_rx_dma, sc->sc_rx_size,
    700 	    &sc->sc_rx_hndl)) {
    701 		DPRINTF("%s: failed to obtain a PA handle\n",
    702 		    device_xname(sc->sc_dev));
    703 		goto errout;
    704 	}
    705 
    706 	memset(&cmd, 0, sizeof(cmd));
    707 	cmd.nvs_type = HVN_NVS_TYPE_RXBUF_CONN;
    708 	cmd.nvs_gpadl = sc->sc_rx_hndl;
    709 	cmd.nvs_sig = HVN_NVS_RXBUF_SIG;
    710 
    711 	tid = atomic_inc_uint_nv(&sc->sc_nvstid);
    712 	if (hvn_nvs_cmd(sc, &cmd, sizeof(cmd), tid, 100))
    713 		goto errout;
    714 
    715 	rsp = (struct hvn_nvs_rxbuf_conn_resp *)&sc->sc_nvsrsp;
    716 	if (rsp->nvs_status != HVN_NVS_STATUS_OK) {
    717 		DPRINTF("%s: failed to set up the Rx ring\n",
    718 		    device_xname(sc->sc_dev));
    719 		goto errout;
    720 	}
    721 	if (rsp->nvs_nsect > 1) {
    722 		DPRINTF("%s: invalid number of Rx ring sections: %u\n",
    723 		    device_xname(sc->sc_dev), rsp->nvs_nsect);
    724 		hvn_rx_ring_destroy(sc);
    725 		return -1;
    726 	}
    727 	return 0;
    728 
    729  errout:
    730 	if (sc->sc_rx_hndl) {
    731 		vmbus_handle_free(sc->sc_chan, sc->sc_rx_hndl);
    732 		sc->sc_rx_hndl = 0;
    733 	}
    734 	if (sc->sc_rx_ring) {
    735 		kmem_free(sc->sc_rx_ring, sc->sc_rx_size);
    736 		sc->sc_rx_ring = NULL;
    737 	}
    738 	return -1;
    739 }
    740 
    741 static int
    742 hvn_rx_ring_destroy(struct hvn_softc *sc)
    743 {
    744 	struct hvn_nvs_rxbuf_disconn cmd;
    745 	uint64_t tid;
    746 
    747 	if (sc->sc_rx_ring == NULL)
    748 		return 0;
    749 
    750 	memset(&cmd, 0, sizeof(cmd));
    751 	cmd.nvs_type = HVN_NVS_TYPE_RXBUF_DISCONN;
    752 	cmd.nvs_sig = HVN_NVS_RXBUF_SIG;
    753 
    754 	tid = atomic_inc_uint_nv(&sc->sc_nvstid);
    755 	if (hvn_nvs_cmd(sc, &cmd, sizeof(cmd), tid, 0))
    756 		return -1;
    757 
    758 	delay(100);
    759 
    760 	vmbus_handle_free(sc->sc_chan, sc->sc_rx_hndl);
    761 
    762 	sc->sc_rx_hndl = 0;
    763 
    764 	kmem_free(sc->sc_rx_ring, sc->sc_rx_size);
    765 	sc->sc_rx_ring = NULL;
    766 
    767 	return 0;
    768 }
    769 
    770 static int
    771 hvn_tx_ring_create(struct hvn_softc *sc)
    772 {
    773 	const int dmaflags = cold ? BUS_DMA_NOWAIT : BUS_DMA_WAITOK;
    774 	struct hvn_tx_desc *txd;
    775 	bus_dma_segment_t *seg;
    776 	size_t msgsize;
    777 	int i, rsegs;
    778 	paddr_t pa;
    779 
    780 	msgsize = roundup(HVN_RNDIS_PKT_LEN, 128);
    781 
    782 	/* Allocate memory to store RNDIS messages */
    783 	if (bus_dmamem_alloc(sc->sc_dmat, msgsize * HVN_TX_DESC, PAGE_SIZE, 0,
    784 	    &sc->sc_tx_mseg, 1, &rsegs, dmaflags)) {
    785 		DPRINTF("%s: failed to allocate memory for RDNIS messages\n",
    786 		    device_xname(sc->sc_dev));
    787 		goto errout;
    788 	}
    789 	if (bus_dmamem_map(sc->sc_dmat, &sc->sc_tx_mseg, 1, msgsize *
    790 	    HVN_TX_DESC, (void **)&sc->sc_tx_msgs, dmaflags)) {
    791 		DPRINTF("%s: failed to establish mapping for RDNIS messages\n",
    792 		    device_xname(sc->sc_dev));
    793 		goto errout;
    794 	}
    795 	memset(sc->sc_tx_msgs, 0, msgsize * HVN_TX_DESC);
    796 	if (bus_dmamap_create(sc->sc_dmat, msgsize * HVN_TX_DESC, 1,
    797 	    msgsize * HVN_TX_DESC, 0, dmaflags, &sc->sc_tx_rmap)) {
    798 		DPRINTF("%s: failed to create map for RDNIS messages\n",
    799 		    device_xname(sc->sc_dev));
    800 		goto errout;
    801 	}
    802 	if (bus_dmamap_load(sc->sc_dmat, sc->sc_tx_rmap, sc->sc_tx_msgs,
    803 	    msgsize * HVN_TX_DESC, NULL, dmaflags)) {
    804 		DPRINTF("%s: failed to create map for RDNIS messages\n",
    805 		    device_xname(sc->sc_dev));
    806 		goto errout;
    807 	}
    808 
    809 	for (i = 0; i < HVN_TX_DESC; i++) {
    810 		txd = &sc->sc_tx_desc[i];
    811 		if (bus_dmamap_create(sc->sc_dmat, HVN_TX_PKT_SIZE,
    812 		    HVN_TX_FRAGS, HVN_TX_FRAG_SIZE, PAGE_SIZE, dmaflags,
    813 		    &txd->txd_dmap)) {
    814 			DPRINTF("%s: failed to create map for TX descriptors\n",
    815 			    device_xname(sc->sc_dev));
    816 			goto errout;
    817 		}
    818 		seg = &sc->sc_tx_rmap->dm_segs[0];
    819 		pa = seg->ds_addr + (msgsize * i);
    820 		txd->txd_gpa.gpa_page = atop(pa);
    821 		txd->txd_gpa.gpa_ofs = pa & PAGE_MASK;
    822 		txd->txd_gpa.gpa_len = msgsize;
    823 		txd->txd_req = (void *)(sc->sc_tx_msgs + (msgsize * i));
    824 		txd->txd_id = i + HVN_NVS_CHIM_SIG;
    825 		txd->txd_ready = 1;
    826 	}
    827 	sc->sc_tx_avail = HVN_TX_DESC;
    828 
    829 	return 0;
    830 
    831  errout:
    832 	hvn_tx_ring_destroy(sc);
    833 	return -1;
    834 }
    835 
    836 static void
    837 hvn_tx_ring_destroy(struct hvn_softc *sc)
    838 {
    839 	struct hvn_tx_desc *txd;
    840 	int i;
    841 
    842 	for (i = 0; i < HVN_TX_DESC; i++) {
    843 		txd = &sc->sc_tx_desc[i];
    844 		if (txd->txd_dmap == NULL)
    845 			continue;
    846 		bus_dmamap_sync(sc->sc_dmat, txd->txd_dmap, 0, 0,
    847 		    BUS_DMASYNC_POSTWRITE);
    848 		bus_dmamap_unload(sc->sc_dmat, txd->txd_dmap);
    849 		bus_dmamap_destroy(sc->sc_dmat, txd->txd_dmap);
    850 		txd->txd_dmap = NULL;
    851 		if (txd->txd_buf == NULL)
    852 			continue;
    853 		m_free(txd->txd_buf);
    854 		txd->txd_buf = NULL;
    855 	}
    856 	if (sc->sc_tx_rmap) {
    857 		bus_dmamap_sync(sc->sc_dmat, sc->sc_tx_rmap, 0, 0,
    858 		    BUS_DMASYNC_POSTWRITE);
    859 		bus_dmamap_unload(sc->sc_dmat, sc->sc_tx_rmap);
    860 		bus_dmamap_destroy(sc->sc_dmat, sc->sc_tx_rmap);
    861 	}
    862 	if (sc->sc_tx_msgs) {
    863 		size_t msgsize = roundup(HVN_RNDIS_PKT_LEN, 128);
    864 
    865 		bus_dmamem_unmap(sc->sc_dmat, sc->sc_tx_msgs,
    866 		    msgsize * HVN_TX_DESC);
    867 		bus_dmamem_free(sc->sc_dmat, &sc->sc_tx_mseg, 1);
    868 	}
    869 	sc->sc_tx_rmap = NULL;
    870 	sc->sc_tx_msgs = NULL;
    871 }
    872 
    873 static int
    874 hvn_get_lladdr(struct hvn_softc *sc, uint8_t *enaddr)
    875 {
    876 	size_t addrlen = ETHER_ADDR_LEN;
    877 	int rv;
    878 
    879 	rv = hvn_rndis_query(sc, OID_802_3_PERMANENT_ADDRESS, enaddr, &addrlen);
    880 	if (rv == 0 && addrlen != ETHER_ADDR_LEN)
    881 		rv = -1;
    882 	return rv;
    883 }
    884 
    885 static void
    886 hvn_get_link_status(struct hvn_softc *sc)
    887 {
    888 	uint32_t state;
    889 	size_t len = sizeof(state);
    890 
    891 	if (hvn_rndis_query(sc, OID_GEN_MEDIA_CONNECT_STATUS,
    892 	    &state, &len) == 0)
    893 		sc->sc_link_state = (state == NDIS_MEDIA_STATE_CONNECTED) ?
    894 		    LINK_STATE_UP : LINK_STATE_DOWN;
    895 }
    896 
    897 static int
    898 hvn_nvs_attach(struct hvn_softc *sc)
    899 {
    900 	static const uint32_t protos[] = {
    901 		HVN_NVS_PROTO_VERSION_5,
    902 		HVN_NVS_PROTO_VERSION_4,
    903 		HVN_NVS_PROTO_VERSION_2,
    904 		HVN_NVS_PROTO_VERSION_1
    905 	};
    906 	const int kmemflags = cold ? KM_NOSLEEP : KM_SLEEP;
    907 	struct hvn_nvs_init cmd;
    908 	struct hvn_nvs_init_resp *rsp;
    909 	struct hvn_nvs_ndis_init ncmd;
    910 	struct hvn_nvs_ndis_conf ccmd;
    911 	uint32_t ndisver, ringsize;
    912 	uint64_t tid;
    913 	int i;
    914 
    915 	sc->sc_nvsbuf = kmem_zalloc(HVN_NVS_BUFSIZE, kmemflags);
    916 	if (sc->sc_nvsbuf == NULL) {
    917 		DPRINTF("%s: failed to allocate channel data buffer\n",
    918 		    device_xname(sc->sc_dev));
    919 		return -1;
    920 	}
    921 
    922 	/* We need to be able to fit all RNDIS control and data messages */
    923 	ringsize = HVN_RNDIS_CTLREQS *
    924 	    (sizeof(struct hvn_nvs_rndis) + sizeof(struct vmbus_gpa)) +
    925 	    HVN_TX_DESC * (sizeof(struct hvn_nvs_rndis) +
    926 	    (HVN_TX_FRAGS + 1) * sizeof(struct vmbus_gpa));
    927 
    928 	sc->sc_chan->ch_flags &= ~CHF_BATCHED;
    929 
    930 	if (vmbus_channel_setdeferred(sc->sc_chan, device_xname(sc->sc_dev))) {
    931 		aprint_error_dev(sc->sc_dev,
    932 		    "failed to create the interrupt thread\n");
    933 		return -1;
    934 	}
    935 
    936 	/* Associate our interrupt handler with the channel */
    937 	if (vmbus_channel_open(sc->sc_chan, ringsize, NULL, 0,
    938 	    hvn_nvs_intr, sc)) {
    939 		DPRINTF("%s: failed to open channel\n",
    940 		    device_xname(sc->sc_dev));
    941 		kmem_free(sc->sc_nvsbuf, HVN_NVS_BUFSIZE);
    942 		return -1;
    943 	}
    944 
    945 	memset(&cmd, 0, sizeof(cmd));
    946 	cmd.nvs_type = HVN_NVS_TYPE_INIT;
    947 	for (i = 0; i < __arraycount(protos); i++) {
    948 		cmd.nvs_ver_min = cmd.nvs_ver_max = protos[i];
    949 		tid = atomic_inc_uint_nv(&sc->sc_nvstid);
    950 		if (hvn_nvs_cmd(sc, &cmd, sizeof(cmd), tid, 100))
    951 			return -1;
    952 
    953 		rsp = (struct hvn_nvs_init_resp *)&sc->sc_nvsrsp;
    954 		if (rsp->nvs_status == HVN_NVS_STATUS_OK) {
    955 			sc->sc_proto = protos[i];
    956 			break;
    957 		}
    958 	}
    959 	if (i == __arraycount(protos)) {
    960 		DPRINTF("%s: failed to negotiate NVSP version\n",
    961 		    device_xname(sc->sc_dev));
    962 		return -1;
    963 	}
    964 
    965 	if (sc->sc_proto >= HVN_NVS_PROTO_VERSION_2) {
    966 		memset(&ccmd, 0, sizeof(ccmd));
    967 		ccmd.nvs_type = HVN_NVS_TYPE_NDIS_CONF;
    968 		ccmd.nvs_mtu = HVN_MAXMTU;
    969 		ccmd.nvs_caps = HVN_NVS_NDIS_CONF_VLAN;
    970 
    971 		tid = atomic_inc_uint_nv(&sc->sc_nvstid);
    972 		if (hvn_nvs_cmd(sc, &ccmd, sizeof(ccmd), tid, 100))
    973 			return -1;
    974 	}
    975 
    976 	memset(&ncmd, 0, sizeof(ncmd));
    977 	ncmd.nvs_type = HVN_NVS_TYPE_NDIS_INIT;
    978 	if (sc->sc_proto <= HVN_NVS_PROTO_VERSION_4)
    979 		ndisver = NDIS_VERSION_6_1;
    980 	else
    981 		ndisver = NDIS_VERSION_6_30;
    982 	ncmd.nvs_ndis_major = (ndisver & 0xffff0000) >> 16;
    983 	ncmd.nvs_ndis_minor = ndisver & 0x0000ffff;
    984 
    985 	tid = atomic_inc_uint_nv(&sc->sc_nvstid);
    986 	if (hvn_nvs_cmd(sc, &ncmd, sizeof(ncmd), tid, 100))
    987 		return -1;
    988 
    989 	sc->sc_ndisver = ndisver;
    990 
    991 	return 0;
    992 }
    993 
    994 static void
    995 hvn_nvs_intr(void *arg)
    996 {
    997 	struct hvn_softc *sc = arg;
    998 	struct ifnet *ifp = SC2IFP(sc);
    999 	struct vmbus_chanpkt_hdr *cph;
   1000 	const struct hvn_nvs_hdr *nvs;
   1001 	uint64_t rid;
   1002 	uint32_t rlen;
   1003 	int rv;
   1004 	bool dotx = false;
   1005 
   1006 	for (;;) {
   1007 		rv = vmbus_channel_recv(sc->sc_chan, sc->sc_nvsbuf,
   1008 		    HVN_NVS_BUFSIZE, &rlen, &rid, 1);
   1009 		if (rv != 0 || rlen == 0) {
   1010 			if (rv != EAGAIN)
   1011 				device_printf(sc->sc_dev,
   1012 				    "failed to receive an NVSP packet\n");
   1013 			break;
   1014 		}
   1015 		cph = (struct vmbus_chanpkt_hdr *)sc->sc_nvsbuf;
   1016 		nvs = (const struct hvn_nvs_hdr *)VMBUS_CHANPKT_CONST_DATA(cph);
   1017 
   1018 		if (cph->cph_type == VMBUS_CHANPKT_TYPE_COMP) {
   1019 			switch (nvs->nvs_type) {
   1020 			case HVN_NVS_TYPE_INIT_RESP:
   1021 			case HVN_NVS_TYPE_RXBUF_CONNRESP:
   1022 			case HVN_NVS_TYPE_CHIM_CONNRESP:
   1023 			case HVN_NVS_TYPE_SUBCH_RESP:
   1024 				/* copy the response back */
   1025 				memcpy(&sc->sc_nvsrsp, nvs, HVN_NVS_MSGSIZE);
   1026 				sc->sc_nvsdone = 1;
   1027 				wakeup(&sc->sc_nvsrsp);
   1028 				break;
   1029 			case HVN_NVS_TYPE_RNDIS_ACK:
   1030 				dotx = true;
   1031 				hvn_txeof(sc, cph->cph_tid);
   1032 				break;
   1033 			default:
   1034 				device_printf(sc->sc_dev,
   1035 				    "unhandled NVSP packet type %u "
   1036 				    "on completion\n", nvs->nvs_type);
   1037 				break;
   1038 			}
   1039 		} else if (cph->cph_type == VMBUS_CHANPKT_TYPE_RXBUF) {
   1040 			switch (nvs->nvs_type) {
   1041 			case HVN_NVS_TYPE_RNDIS:
   1042 				hvn_rndis_input(sc, cph->cph_tid, cph);
   1043 				break;
   1044 			default:
   1045 				device_printf(sc->sc_dev,
   1046 				    "unhandled NVSP packet type %u "
   1047 				    "on receive\n", nvs->nvs_type);
   1048 				break;
   1049 			}
   1050 		} else
   1051 			device_printf(sc->sc_dev,
   1052 			    "unknown NVSP packet type %u\n", cph->cph_type);
   1053 	}
   1054 
   1055 	if (dotx)
   1056 		if_schedule_deferred_start(ifp);
   1057 }
   1058 
   1059 static int
   1060 hvn_nvs_cmd(struct hvn_softc *sc, void *cmd, size_t cmdsize, uint64_t tid,
   1061     int timo)
   1062 {
   1063 	struct hvn_nvs_hdr *hdr = cmd;
   1064 	int tries = 10;
   1065 	int rv, s;
   1066 
   1067 	sc->sc_nvsdone = 0;
   1068 
   1069 	do {
   1070 		rv = vmbus_channel_send(sc->sc_chan, cmd, cmdsize,
   1071 		    tid, VMBUS_CHANPKT_TYPE_INBAND,
   1072 		    timo ? VMBUS_CHANPKT_FLAG_RC : 0);
   1073 		if (rv == EAGAIN) {
   1074 			if (cold)
   1075 				delay(1000);
   1076 			else
   1077 				tsleep(cmd, PRIBIO, "nvsout", 1);
   1078 		} else if (rv) {
   1079 			DPRINTF("%s: NVSP operation %u send error %d\n",
   1080 			    device_xname(sc->sc_dev), hdr->nvs_type, rv);
   1081 			return rv;
   1082 		}
   1083 	} while (rv != 0 && --tries > 0);
   1084 
   1085 	if (tries == 0 && rv != 0) {
   1086 		device_printf(sc->sc_dev,
   1087 		    "NVSP operation %u send error %d\n", hdr->nvs_type, rv);
   1088 		return rv;
   1089 	}
   1090 
   1091 	if (timo == 0)
   1092 		return 0;
   1093 
   1094 	do {
   1095 		if (cold)
   1096 			delay(1000);
   1097 		else
   1098 			tsleep(sc, PRIBIO | PCATCH, "nvscmd", 1);
   1099 		s = splnet();
   1100 		hvn_nvs_intr(sc);
   1101 		splx(s);
   1102 	} while (--timo > 0 && sc->sc_nvsdone != 1);
   1103 
   1104 	if (timo == 0 && sc->sc_nvsdone != 1) {
   1105 		device_printf(sc->sc_dev, "NVSP operation %u timed out\n",
   1106 		    hdr->nvs_type);
   1107 		return ETIMEDOUT;
   1108 	}
   1109 	return 0;
   1110 }
   1111 
   1112 static int
   1113 hvn_nvs_ack(struct hvn_softc *sc, uint64_t tid)
   1114 {
   1115 	struct hvn_nvs_rndis_ack cmd;
   1116 	int tries = 5;
   1117 	int rv;
   1118 
   1119 	cmd.nvs_type = HVN_NVS_TYPE_RNDIS_ACK;
   1120 	cmd.nvs_status = HVN_NVS_STATUS_OK;
   1121 	do {
   1122 		rv = vmbus_channel_send(sc->sc_chan, &cmd, sizeof(cmd),
   1123 		    tid, VMBUS_CHANPKT_TYPE_COMP, 0);
   1124 		if (rv == EAGAIN)
   1125 			delay(10);
   1126 		else if (rv) {
   1127 			DPRINTF("%s: NVSP acknowledgement error %d\n",
   1128 			    device_xname(sc->sc_dev), rv);
   1129 			return rv;
   1130 		}
   1131 	} while (rv != 0 && --tries > 0);
   1132 	return rv;
   1133 }
   1134 
   1135 static void
   1136 hvn_nvs_detach(struct hvn_softc *sc)
   1137 {
   1138 
   1139 	if (vmbus_channel_close(sc->sc_chan) == 0) {
   1140 		kmem_free(sc->sc_nvsbuf, HVN_NVS_BUFSIZE);
   1141 		sc->sc_nvsbuf = NULL;
   1142 	}
   1143 }
   1144 
   1145 static inline struct rndis_cmd *
   1146 hvn_alloc_cmd(struct hvn_softc *sc)
   1147 {
   1148 	struct rndis_cmd *rc;
   1149 
   1150 	mutex_enter(&sc->sc_cntl_fqlck);
   1151 	while ((rc = TAILQ_FIRST(&sc->sc_cntl_fq)) == NULL)
   1152 		/* XXX use condvar(9) instead of mtsleep */
   1153 		mtsleep(&sc->sc_cntl_fq, PRIBIO, "nvsalloc", 1,
   1154 		    &sc->sc_cntl_fqlck);
   1155 	TAILQ_REMOVE(&sc->sc_cntl_fq, rc, rc_entry);
   1156 	mutex_exit(&sc->sc_cntl_fqlck);
   1157 	return rc;
   1158 }
   1159 
   1160 static inline void
   1161 hvn_submit_cmd(struct hvn_softc *sc, struct rndis_cmd *rc)
   1162 {
   1163 
   1164 	mutex_enter(&sc->sc_cntl_sqlck);
   1165 	TAILQ_INSERT_TAIL(&sc->sc_cntl_sq, rc, rc_entry);
   1166 	mutex_exit(&sc->sc_cntl_sqlck);
   1167 }
   1168 
   1169 static inline struct rndis_cmd *
   1170 hvn_complete_cmd(struct hvn_softc *sc, uint32_t id)
   1171 {
   1172 	struct rndis_cmd *rc;
   1173 
   1174 	mutex_enter(&sc->sc_cntl_sqlck);
   1175 	TAILQ_FOREACH(rc, &sc->sc_cntl_sq, rc_entry) {
   1176 		if (rc->rc_id == id) {
   1177 			TAILQ_REMOVE(&sc->sc_cntl_sq, rc, rc_entry);
   1178 			break;
   1179 		}
   1180 	}
   1181 	mutex_exit(&sc->sc_cntl_sqlck);
   1182 	if (rc != NULL) {
   1183 		mutex_enter(&sc->sc_cntl_cqlck);
   1184 		TAILQ_INSERT_TAIL(&sc->sc_cntl_cq, rc, rc_entry);
   1185 		mutex_exit(&sc->sc_cntl_cqlck);
   1186 	}
   1187 	return rc;
   1188 }
   1189 
   1190 static inline void
   1191 hvn_release_cmd(struct hvn_softc *sc, struct rndis_cmd *rc)
   1192 {
   1193 
   1194 	mutex_enter(&sc->sc_cntl_cqlck);
   1195 	TAILQ_REMOVE(&sc->sc_cntl_cq, rc, rc_entry);
   1196 	mutex_exit(&sc->sc_cntl_cqlck);
   1197 }
   1198 
   1199 static inline int
   1200 hvn_rollback_cmd(struct hvn_softc *sc, struct rndis_cmd *rc)
   1201 {
   1202 	struct rndis_cmd *rn;
   1203 
   1204 	mutex_enter(&sc->sc_cntl_sqlck);
   1205 	TAILQ_FOREACH(rn, &sc->sc_cntl_sq, rc_entry) {
   1206 		if (rn == rc) {
   1207 			TAILQ_REMOVE(&sc->sc_cntl_sq, rc, rc_entry);
   1208 			mutex_exit(&sc->sc_cntl_sqlck);
   1209 			return 0;
   1210 		}
   1211 	}
   1212 	mutex_exit(&sc->sc_cntl_sqlck);
   1213 	return -1;
   1214 }
   1215 
   1216 static inline void
   1217 hvn_free_cmd(struct hvn_softc *sc, struct rndis_cmd *rc)
   1218 {
   1219 
   1220 	memset(rc->rc_req, 0, sizeof(struct rndis_packet_msg));
   1221 	memset(&rc->rc_cmp, 0, sizeof(rc->rc_cmp));
   1222 	memset(&rc->rc_msg, 0, sizeof(rc->rc_msg));
   1223 	mutex_enter(&sc->sc_cntl_fqlck);
   1224 	TAILQ_INSERT_TAIL(&sc->sc_cntl_fq, rc, rc_entry);
   1225 	mutex_exit(&sc->sc_cntl_fqlck);
   1226 	wakeup(&sc->sc_cntl_fq);
   1227 }
   1228 
   1229 static int
   1230 hvn_rndis_attach(struct hvn_softc *sc)
   1231 {
   1232 	const int dmaflags = cold ? BUS_DMA_NOWAIT : BUS_DMA_WAITOK;
   1233 	struct rndis_init_req *req;
   1234 	struct rndis_init_comp *cmp;
   1235 	struct rndis_cmd *rc;
   1236 	int i, rv;
   1237 
   1238 	/* RNDIS control message queues */
   1239 	TAILQ_INIT(&sc->sc_cntl_sq);
   1240 	TAILQ_INIT(&sc->sc_cntl_cq);
   1241 	TAILQ_INIT(&sc->sc_cntl_fq);
   1242 	mutex_init(&sc->sc_cntl_sqlck, MUTEX_DEFAULT, IPL_NET);
   1243 	mutex_init(&sc->sc_cntl_cqlck, MUTEX_DEFAULT, IPL_NET);
   1244 	mutex_init(&sc->sc_cntl_fqlck, MUTEX_DEFAULT, IPL_NET);
   1245 
   1246 	for (i = 0; i < HVN_RNDIS_CTLREQS; i++) {
   1247 		rc = &sc->sc_cntl_msgs[i];
   1248 		if (bus_dmamap_create(sc->sc_dmat, PAGE_SIZE, 1, PAGE_SIZE, 0,
   1249 		    dmaflags, &rc->rc_dmap)) {
   1250 			DPRINTF("%s: failed to create RNDIS command map\n",
   1251 			    device_xname(sc->sc_dev));
   1252 			goto errout;
   1253 		}
   1254 		if (bus_dmamem_alloc(sc->sc_dmat, PAGE_SIZE, PAGE_SIZE,
   1255 		    0, &rc->rc_segs, 1, &rc->rc_nsegs, dmaflags)) {
   1256 			DPRINTF("%s: failed to allocate RNDIS command\n",
   1257 			    device_xname(sc->sc_dev));
   1258 			bus_dmamap_destroy(sc->sc_dmat, rc->rc_dmap);
   1259 			goto errout;
   1260 		}
   1261 		if (bus_dmamem_map(sc->sc_dmat, &rc->rc_segs, rc->rc_nsegs,
   1262 		    PAGE_SIZE, (void **)&rc->rc_req, dmaflags)) {
   1263 			DPRINTF("%s: failed to allocate RNDIS command\n",
   1264 			    device_xname(sc->sc_dev));
   1265 			bus_dmamem_free(sc->sc_dmat, &rc->rc_segs,
   1266 			    rc->rc_nsegs);
   1267 			bus_dmamap_destroy(sc->sc_dmat, rc->rc_dmap);
   1268 			goto errout;
   1269 		}
   1270 		memset(rc->rc_req, 0, PAGE_SIZE);
   1271 		if (bus_dmamap_load(sc->sc_dmat, rc->rc_dmap, rc->rc_req,
   1272 		    PAGE_SIZE, NULL, dmaflags)) {
   1273 			DPRINTF("%s: failed to load RNDIS command map\n",
   1274 			    device_xname(sc->sc_dev));
   1275 			bus_dmamem_free(sc->sc_dmat, &rc->rc_segs,
   1276 			    rc->rc_nsegs);
   1277 			bus_dmamap_destroy(sc->sc_dmat, rc->rc_dmap);
   1278 			goto errout;
   1279 		}
   1280 		rc->rc_gpa = atop(rc->rc_dmap->dm_segs[0].ds_addr);
   1281 		TAILQ_INSERT_TAIL(&sc->sc_cntl_fq, rc, rc_entry);
   1282 	}
   1283 
   1284 	rc = hvn_alloc_cmd(sc);
   1285 
   1286 	bus_dmamap_sync(sc->sc_dmat, rc->rc_dmap, 0, PAGE_SIZE,
   1287 	    BUS_DMASYNC_PREREAD);
   1288 
   1289 	rc->rc_id = atomic_inc_uint_nv(&sc->sc_rndisrid);
   1290 
   1291 	req = rc->rc_req;
   1292 	req->rm_type = REMOTE_NDIS_INITIALIZE_MSG;
   1293 	req->rm_len = sizeof(*req);
   1294 	req->rm_rid = rc->rc_id;
   1295 	req->rm_ver_major = RNDIS_VERSION_MAJOR;
   1296 	req->rm_ver_minor = RNDIS_VERSION_MINOR;
   1297 	req->rm_max_xfersz = HVN_RNDIS_XFER_SIZE;
   1298 
   1299 	rc->rc_cmplen = sizeof(*cmp);
   1300 
   1301 	bus_dmamap_sync(sc->sc_dmat, rc->rc_dmap, 0, PAGE_SIZE,
   1302 	    BUS_DMASYNC_PREWRITE);
   1303 
   1304 	if ((rv = hvn_rndis_cmd(sc, rc, 500)) != 0) {
   1305 		DPRINTF("%s: INITIALIZE_MSG failed, error %d\n",
   1306 		    device_xname(sc->sc_dev), rv);
   1307 		hvn_free_cmd(sc, rc);
   1308 		goto errout;
   1309 	}
   1310 	cmp = (struct rndis_init_comp *)&rc->rc_cmp;
   1311 	if (cmp->rm_status != RNDIS_STATUS_SUCCESS) {
   1312 		DPRINTF("%s: failed to init RNDIS, error %#x\n",
   1313 		    device_xname(sc->sc_dev), cmp->rm_status);
   1314 		hvn_free_cmd(sc, rc);
   1315 		goto errout;
   1316 	}
   1317 
   1318 	hvn_free_cmd(sc, rc);
   1319 
   1320 	/* Initialize RNDIS Data command */
   1321 	memset(&sc->sc_data_msg, 0, sizeof(sc->sc_data_msg));
   1322 	sc->sc_data_msg.nvs_type = HVN_NVS_TYPE_RNDIS;
   1323 	sc->sc_data_msg.nvs_rndis_mtype = HVN_NVS_RNDIS_MTYPE_DATA;
   1324 	sc->sc_data_msg.nvs_chim_idx = HVN_NVS_CHIM_IDX_INVALID;
   1325 
   1326 	return 0;
   1327 
   1328 errout:
   1329 	for (i = 0; i < HVN_RNDIS_CTLREQS; i++) {
   1330 		rc = &sc->sc_cntl_msgs[i];
   1331 		if (rc->rc_req == NULL)
   1332 			continue;
   1333 		TAILQ_REMOVE(&sc->sc_cntl_fq, rc, rc_entry);
   1334 		bus_dmamem_free(sc->sc_dmat, &rc->rc_segs, rc->rc_nsegs);
   1335 		rc->rc_req = NULL;
   1336 		bus_dmamap_destroy(sc->sc_dmat, rc->rc_dmap);
   1337 	}
   1338 	return -1;
   1339 }
   1340 
   1341 static int
   1342 hvn_set_capabilities(struct hvn_softc *sc)
   1343 {
   1344 	struct ndis_offload_params params;
   1345 	size_t len = sizeof(params);
   1346 
   1347 	memset(&params, 0, sizeof(params));
   1348 
   1349 	params.ndis_hdr.ndis_type = NDIS_OBJTYPE_DEFAULT;
   1350 	if (sc->sc_ndisver < NDIS_VERSION_6_30) {
   1351 		params.ndis_hdr.ndis_rev = NDIS_OFFLOAD_PARAMS_REV_2;
   1352 		len = params.ndis_hdr.ndis_size = NDIS_OFFLOAD_PARAMS_SIZE_6_1;
   1353 	} else {
   1354 		params.ndis_hdr.ndis_rev = NDIS_OFFLOAD_PARAMS_REV_3;
   1355 		len = params.ndis_hdr.ndis_size = NDIS_OFFLOAD_PARAMS_SIZE;
   1356 	}
   1357 
   1358 	params.ndis_ip4csum = NDIS_OFFLOAD_PARAM_TXRX;
   1359 	params.ndis_tcp4csum = NDIS_OFFLOAD_PARAM_TXRX;
   1360 	params.ndis_tcp6csum = NDIS_OFFLOAD_PARAM_TXRX;
   1361 	if (sc->sc_ndisver >= NDIS_VERSION_6_30) {
   1362 		params.ndis_udp4csum = NDIS_OFFLOAD_PARAM_TXRX;
   1363 		params.ndis_udp6csum = NDIS_OFFLOAD_PARAM_TXRX;
   1364 	}
   1365 
   1366 	return hvn_rndis_set(sc, OID_TCP_OFFLOAD_PARAMETERS, &params, len);
   1367 }
   1368 
   1369 static int
   1370 hvn_rndis_cmd(struct hvn_softc *sc, struct rndis_cmd *rc, int timo)
   1371 {
   1372 	struct hvn_nvs_rndis *msg = &rc->rc_msg;
   1373 	struct rndis_msghdr *hdr = rc->rc_req;
   1374 	struct vmbus_gpa sgl[1];
   1375 	int tries = 10;
   1376 	int rv, s;
   1377 
   1378 	KASSERT(timo > 0);
   1379 
   1380 	msg->nvs_type = HVN_NVS_TYPE_RNDIS;
   1381 	msg->nvs_rndis_mtype = HVN_NVS_RNDIS_MTYPE_CTRL;
   1382 	msg->nvs_chim_idx = HVN_NVS_CHIM_IDX_INVALID;
   1383 
   1384 	sgl[0].gpa_page = rc->rc_gpa;
   1385 	sgl[0].gpa_len = hdr->rm_len;
   1386 	sgl[0].gpa_ofs = 0;
   1387 
   1388 	rc->rc_done = 0;
   1389 
   1390 	hvn_submit_cmd(sc, rc);
   1391 
   1392 	do {
   1393 		rv = vmbus_channel_send_sgl(sc->sc_chan, sgl, 1, &rc->rc_msg,
   1394 		    sizeof(*msg), rc->rc_id);
   1395 		if (rv == EAGAIN) {
   1396 			if (cold)
   1397 				delay(1000);
   1398 			else
   1399 				tsleep(rc, PRIBIO, "rndisout", 1);
   1400 		} else if (rv) {
   1401 			DPRINTF("%s: RNDIS operation %u send error %d\n",
   1402 			    device_xname(sc->sc_dev), hdr->rm_type, rv);
   1403 			hvn_rollback_cmd(sc, rc);
   1404 			return rv;
   1405 		}
   1406 	} while (rv != 0 && --tries > 0);
   1407 
   1408 	if (tries == 0 && rv != 0) {
   1409 		device_printf(sc->sc_dev,
   1410 		    "RNDIS operation %u send error %d\n", hdr->rm_type, rv);
   1411 		return rv;
   1412 	}
   1413 
   1414 	bus_dmamap_sync(sc->sc_dmat, rc->rc_dmap, 0, PAGE_SIZE,
   1415 	    BUS_DMASYNC_POSTWRITE);
   1416 
   1417 	do {
   1418 		if (cold)
   1419 			delay(1000);
   1420 		else
   1421 			tsleep(rc, PRIBIO | PCATCH, "rndiscmd", 1);
   1422 		s = splnet();
   1423 		hvn_nvs_intr(sc);
   1424 		splx(s);
   1425 	} while (--timo > 0 && rc->rc_done != 1);
   1426 
   1427 	bus_dmamap_sync(sc->sc_dmat, rc->rc_dmap, 0, PAGE_SIZE,
   1428 	    BUS_DMASYNC_POSTREAD);
   1429 
   1430 	if (rc->rc_done != 1) {
   1431 		rv = timo == 0 ? ETIMEDOUT : EINTR;
   1432 		if (hvn_rollback_cmd(sc, rc)) {
   1433 			hvn_release_cmd(sc, rc);
   1434 			rv = 0;
   1435 		} else if (rv == ETIMEDOUT) {
   1436 			device_printf(sc->sc_dev,
   1437 			    "RNDIS operation %u timed out\n", hdr->rm_type);
   1438 		}
   1439 		return rv;
   1440 	}
   1441 
   1442 	hvn_release_cmd(sc, rc);
   1443 	return 0;
   1444 }
   1445 
   1446 static void
   1447 hvn_rndis_input(struct hvn_softc *sc, uint64_t tid, void *arg)
   1448 {
   1449 	struct vmbus_chanpkt_prplist *cp = arg;
   1450 	uint32_t off, len, type;
   1451 	int i;
   1452 
   1453 	if (sc->sc_rx_ring == NULL) {
   1454 		DPRINTF("%s: invalid rx ring\n", device_xname(sc->sc_dev));
   1455 		return;
   1456 	}
   1457 
   1458 	for (i = 0; i < cp->cp_range_cnt; i++) {
   1459 		off = cp->cp_range[i].gpa_ofs;
   1460 		len = cp->cp_range[i].gpa_len;
   1461 
   1462 		KASSERT(off + len <= sc->sc_rx_size);
   1463 		KASSERT(len >= RNDIS_HEADER_OFFSET + 4);
   1464 
   1465 		memcpy(&type, sc->sc_rx_ring + off, sizeof(type));
   1466 		switch (type) {
   1467 		/* data message */
   1468 		case REMOTE_NDIS_PACKET_MSG:
   1469 			hvn_rxeof(sc, sc->sc_rx_ring + off, len);
   1470 			break;
   1471 		/* completion messages */
   1472 		case REMOTE_NDIS_INITIALIZE_CMPLT:
   1473 		case REMOTE_NDIS_QUERY_CMPLT:
   1474 		case REMOTE_NDIS_SET_CMPLT:
   1475 		case REMOTE_NDIS_RESET_CMPLT:
   1476 		case REMOTE_NDIS_KEEPALIVE_CMPLT:
   1477 			hvn_rndis_complete(sc, sc->sc_rx_ring + off, len);
   1478 			break;
   1479 		/* notification message */
   1480 		case REMOTE_NDIS_INDICATE_STATUS_MSG:
   1481 			hvn_rndis_status(sc, sc->sc_rx_ring + off, len);
   1482 			break;
   1483 		default:
   1484 			device_printf(sc->sc_dev,
   1485 			    "unhandled RNDIS message type %u\n", type);
   1486 			break;
   1487 		}
   1488 	}
   1489 
   1490 	hvn_nvs_ack(sc, tid);
   1491 }
   1492 
   1493 static inline struct mbuf *
   1494 hvn_devget(struct hvn_softc *sc, void *buf, uint32_t len)
   1495 {
   1496 	struct ifnet *ifp = SC2IFP(sc);
   1497 	struct mbuf *m;
   1498 	size_t size = len + ETHER_ALIGN;
   1499 
   1500 	MGETHDR(m, M_NOWAIT, MT_DATA);
   1501 	if (m == NULL)
   1502 		return NULL;
   1503 
   1504 	if (size > MHLEN) {
   1505 		if (size <= MCLBYTES)
   1506 			MCLGET(m, M_NOWAIT);
   1507 		else
   1508 			MEXTMALLOC(m, size, M_NOWAIT);
   1509 		if ((m->m_flags & M_EXT) == 0) {
   1510 			m_freem(m);
   1511 			return NULL;
   1512 		}
   1513 	}
   1514 
   1515 	m->m_len = m->m_pkthdr.len = size;
   1516 	m_adj(m, ETHER_ALIGN);
   1517 	m_copyback(m, 0, len, buf);
   1518 	m_set_rcvif(m, ifp);
   1519 	return m;
   1520 }
   1521 
   1522 static void
   1523 hvn_rxeof(struct hvn_softc *sc, uint8_t *buf, uint32_t len)
   1524 {
   1525 	struct ifnet *ifp = SC2IFP(sc);
   1526 	struct rndis_packet_msg *pkt;
   1527 	struct rndis_pktinfo *pi;
   1528 	uint32_t csum, vlan;
   1529 	struct mbuf *m;
   1530 
   1531 	if (!(ifp->if_flags & IFF_RUNNING))
   1532 		return;
   1533 
   1534 	if (len < sizeof(*pkt)) {
   1535 		device_printf(sc->sc_dev, "data packet too short: %u\n",
   1536 		    len);
   1537 		return;
   1538 	}
   1539 
   1540 	pkt = (struct rndis_packet_msg *)buf;
   1541 	if (pkt->rm_dataoffset + pkt->rm_datalen > len) {
   1542 		device_printf(sc->sc_dev,
   1543 		    "data packet out of bounds: %u@%u\n", pkt->rm_dataoffset,
   1544 		    pkt->rm_datalen);
   1545 		return;
   1546 	}
   1547 
   1548 	if ((m = hvn_devget(sc, buf + RNDIS_HEADER_OFFSET + pkt->rm_dataoffset,
   1549 	    pkt->rm_datalen)) == NULL) {
   1550 		ifp->if_ierrors++;
   1551 		return;
   1552 	}
   1553 
   1554 	if (pkt->rm_pktinfooffset + pkt->rm_pktinfolen > len) {
   1555 		device_printf(sc->sc_dev,
   1556 		    "pktinfo is out of bounds: %u@%u vs %u\n",
   1557 		    pkt->rm_pktinfolen, pkt->rm_pktinfooffset, len);
   1558 		goto done;
   1559 	}
   1560 
   1561 	pi = (struct rndis_pktinfo *)(buf + RNDIS_HEADER_OFFSET +
   1562 	    pkt->rm_pktinfooffset);
   1563 	while (pkt->rm_pktinfolen > 0) {
   1564 		if (pi->rm_size > pkt->rm_pktinfolen) {
   1565 			device_printf(sc->sc_dev,
   1566 			    "invalid pktinfo size: %u/%u\n", pi->rm_size,
   1567 			    pkt->rm_pktinfolen);
   1568 			break;
   1569 		}
   1570 
   1571 		switch (pi->rm_type) {
   1572 		case NDIS_PKTINFO_TYPE_CSUM:
   1573 			memcpy(&csum, pi->rm_data, sizeof(csum));
   1574 			if (csum & NDIS_RXCSUM_INFO_IPCS_OK)
   1575 				m->m_pkthdr.csum_flags |= M_CSUM_IPv4;
   1576 			if (csum & NDIS_RXCSUM_INFO_TCPCS_OK)
   1577 				m->m_pkthdr.csum_flags |= M_CSUM_TCPv4;
   1578 			if (csum & NDIS_RXCSUM_INFO_UDPCS_OK)
   1579 				m->m_pkthdr.csum_flags |= M_CSUM_UDPv4;
   1580 			break;
   1581 		case NDIS_PKTINFO_TYPE_VLAN:
   1582 			memcpy(&vlan, pi->rm_data, sizeof(vlan));
   1583 			if (vlan != 0xffffffff) {
   1584 				m->m_pkthdr.ether_vtag =
   1585 				    NDIS_VLAN_INFO_ID(vlan) |
   1586 				    (NDIS_VLAN_INFO_PRI(vlan) << EVL_PRIO_BITS);
   1587 				m->m_flags |= M_VLANTAG;
   1588 			}
   1589 			break;
   1590 		default:
   1591 			DPRINTF("%s: unhandled pktinfo type %u\n",
   1592 			    device_xname(sc->sc_dev), pi->rm_type);
   1593 			break;
   1594 		}
   1595 
   1596 		pkt->rm_pktinfolen -= pi->rm_size;
   1597 		pi = (struct rndis_pktinfo *)((char *)pi + pi->rm_size);
   1598 	}
   1599 
   1600  done:
   1601 	if_percpuq_enqueue(sc->sc_ipq, m);
   1602 }
   1603 
   1604 static void
   1605 hvn_rndis_complete(struct hvn_softc *sc, uint8_t *buf, uint32_t len)
   1606 {
   1607 	struct rndis_cmd *rc;
   1608 	uint32_t id;
   1609 
   1610 	memcpy(&id, buf + RNDIS_HEADER_OFFSET, sizeof(id));
   1611 	if ((rc = hvn_complete_cmd(sc, id)) != NULL) {
   1612 		if (len < rc->rc_cmplen)
   1613 			device_printf(sc->sc_dev,
   1614 			    "RNDIS response %u too short: %u\n", id, len);
   1615 		else
   1616 			memcpy(&rc->rc_cmp, buf, rc->rc_cmplen);
   1617 		if (len > rc->rc_cmplen &&
   1618 		    len - rc->rc_cmplen > HVN_RNDIS_BUFSIZE)
   1619 			device_printf(sc->sc_dev,
   1620 			    "RNDIS response %u too large: %u\n", id, len);
   1621 		else if (len > rc->rc_cmplen)
   1622 			memcpy(&rc->rc_cmpbuf, buf + rc->rc_cmplen,
   1623 			    len - rc->rc_cmplen);
   1624 		rc->rc_done = 1;
   1625 		wakeup(rc);
   1626 	} else {
   1627 		DPRINTF("%s: failed to complete RNDIS request id %u\n",
   1628 		    device_xname(sc->sc_dev), id);
   1629 	}
   1630 }
   1631 
   1632 static int
   1633 hvn_rndis_output(struct hvn_softc *sc, struct hvn_tx_desc *txd)
   1634 {
   1635 	uint64_t rid = (uint64_t)txd->txd_id << 32;
   1636 	int rv;
   1637 
   1638 	rv = vmbus_channel_send_sgl(sc->sc_chan, txd->txd_sgl, txd->txd_nsge,
   1639 	    &sc->sc_data_msg, sizeof(sc->sc_data_msg), rid);
   1640 	if (rv) {
   1641 		DPRINTF("%s: RNDIS data send error %d\n",
   1642 		    device_xname(sc->sc_dev), rv);
   1643 		return rv;
   1644 	}
   1645 	return 0;
   1646 }
   1647 
   1648 static void
   1649 hvn_rndis_status(struct hvn_softc *sc, uint8_t *buf, uint32_t len)
   1650 {
   1651 	struct ifnet *ifp = SC2IFP(sc);
   1652 	uint32_t status;
   1653 	int link_state = sc->sc_link_state;
   1654 
   1655 	memcpy(&status, buf + RNDIS_HEADER_OFFSET, sizeof(status));
   1656 	switch (status) {
   1657 	case RNDIS_STATUS_MEDIA_CONNECT:
   1658 		sc->sc_link_state = LINK_STATE_UP;
   1659 		break;
   1660 	case RNDIS_STATUS_MEDIA_DISCONNECT:
   1661 		sc->sc_link_state = LINK_STATE_DOWN;
   1662 		break;
   1663 	/* Ignore these */
   1664 	case RNDIS_STATUS_OFFLOAD_CURRENT_CONFIG:
   1665 		return;
   1666 	default:
   1667 		DPRINTF("%s: unhandled status %#x\n", device_xname(sc->sc_dev),
   1668 		    status);
   1669 		return;
   1670 	}
   1671 	if (link_state != sc->sc_link_state)
   1672 		if_link_state_change(ifp, sc->sc_link_state);
   1673 }
   1674 
   1675 static int
   1676 hvn_rndis_query(struct hvn_softc *sc, uint32_t oid, void *res, size_t *length)
   1677 {
   1678 	struct rndis_cmd *rc;
   1679 	struct rndis_query_req *req;
   1680 	struct rndis_query_comp *cmp;
   1681 	size_t olength = *length;
   1682 	int rv;
   1683 
   1684 	rc = hvn_alloc_cmd(sc);
   1685 
   1686 	bus_dmamap_sync(sc->sc_dmat, rc->rc_dmap, 0, PAGE_SIZE,
   1687 	    BUS_DMASYNC_PREREAD);
   1688 
   1689 	rc->rc_id = atomic_inc_uint_nv(&sc->sc_rndisrid);
   1690 
   1691 	req = rc->rc_req;
   1692 	req->rm_type = REMOTE_NDIS_QUERY_MSG;
   1693 	req->rm_len = sizeof(*req);
   1694 	req->rm_rid = rc->rc_id;
   1695 	req->rm_oid = oid;
   1696 	req->rm_infobufoffset = sizeof(*req) - RNDIS_HEADER_OFFSET;
   1697 
   1698 	rc->rc_cmplen = sizeof(*cmp);
   1699 
   1700 	bus_dmamap_sync(sc->sc_dmat, rc->rc_dmap, 0, PAGE_SIZE,
   1701 	    BUS_DMASYNC_PREWRITE);
   1702 
   1703 	if ((rv = hvn_rndis_cmd(sc, rc, 500)) != 0) {
   1704 		DPRINTF("%s: QUERY_MSG failed, error %d\n",
   1705 		    device_xname(sc->sc_dev), rv);
   1706 		hvn_free_cmd(sc, rc);
   1707 		return rv;
   1708 	}
   1709 
   1710 	cmp = (struct rndis_query_comp *)&rc->rc_cmp;
   1711 	switch (cmp->rm_status) {
   1712 	case RNDIS_STATUS_SUCCESS:
   1713 		if (cmp->rm_infobuflen > olength) {
   1714 			rv = EINVAL;
   1715 			break;
   1716 		}
   1717 		memcpy(res, rc->rc_cmpbuf, cmp->rm_infobuflen);
   1718 		*length = cmp->rm_infobuflen;
   1719 		break;
   1720 	default:
   1721 		*length = 0;
   1722 		rv = EIO;
   1723 		break;
   1724 	}
   1725 
   1726 	hvn_free_cmd(sc, rc);
   1727 	return rv;
   1728 }
   1729 
   1730 static int
   1731 hvn_rndis_set(struct hvn_softc *sc, uint32_t oid, void *data, size_t length)
   1732 {
   1733 	struct rndis_cmd *rc;
   1734 	struct rndis_set_req *req;
   1735 	struct rndis_set_comp *cmp;
   1736 	int rv;
   1737 
   1738 	rc = hvn_alloc_cmd(sc);
   1739 
   1740 	bus_dmamap_sync(sc->sc_dmat, rc->rc_dmap, 0, PAGE_SIZE,
   1741 	    BUS_DMASYNC_PREREAD);
   1742 
   1743 	rc->rc_id = atomic_inc_uint_nv(&sc->sc_rndisrid);
   1744 
   1745 	req = rc->rc_req;
   1746 	req->rm_type = REMOTE_NDIS_SET_MSG;
   1747 	req->rm_len = sizeof(*req) + length;
   1748 	req->rm_rid = rc->rc_id;
   1749 	req->rm_oid = oid;
   1750 	req->rm_infobufoffset = sizeof(*req) - RNDIS_HEADER_OFFSET;
   1751 
   1752 	rc->rc_cmplen = sizeof(*cmp);
   1753 
   1754 	if (length > 0) {
   1755 		KASSERT(sizeof(*req) + length < PAGE_SIZE);
   1756 		req->rm_infobuflen = length;
   1757 		memcpy(req + 1, data, length);
   1758 	}
   1759 
   1760 	bus_dmamap_sync(sc->sc_dmat, rc->rc_dmap, 0, PAGE_SIZE,
   1761 	    BUS_DMASYNC_PREWRITE);
   1762 
   1763 	if ((rv = hvn_rndis_cmd(sc, rc, 500)) != 0) {
   1764 		DPRINTF("%s: SET_MSG failed, error %d\n",
   1765 		    device_xname(sc->sc_dev), rv);
   1766 		hvn_free_cmd(sc, rc);
   1767 		return rv;
   1768 	}
   1769 
   1770 	cmp = (struct rndis_set_comp *)&rc->rc_cmp;
   1771 	if (cmp->rm_status != RNDIS_STATUS_SUCCESS)
   1772 		rv = EIO;
   1773 
   1774 	hvn_free_cmd(sc, rc);
   1775 	return rv;
   1776 }
   1777 
   1778 static int
   1779 hvn_rndis_open(struct hvn_softc *sc)
   1780 {
   1781 	uint32_t filter;
   1782 	int rv;
   1783 
   1784 	if (sc->sc_promisc)
   1785 		filter = RNDIS_PACKET_TYPE_PROMISCUOUS;
   1786 	else
   1787 		filter = RNDIS_PACKET_TYPE_BROADCAST |
   1788 		    RNDIS_PACKET_TYPE_ALL_MULTICAST |
   1789 		    RNDIS_PACKET_TYPE_DIRECTED;
   1790 
   1791 	rv = hvn_rndis_set(sc, OID_GEN_CURRENT_PACKET_FILTER,
   1792 	    &filter, sizeof(filter));
   1793 	if (rv) {
   1794 		DPRINTF("%s: failed to set RNDIS filter to %#x\n",
   1795 		    device_xname(sc->sc_dev), filter);
   1796 	}
   1797 	return rv;
   1798 }
   1799 
   1800 static int
   1801 hvn_rndis_close(struct hvn_softc *sc)
   1802 {
   1803 	uint32_t filter = 0;
   1804 	int rv;
   1805 
   1806 	rv = hvn_rndis_set(sc, OID_GEN_CURRENT_PACKET_FILTER,
   1807 	    &filter, sizeof(filter));
   1808 	if (rv) {
   1809 		DPRINTF("%s: failed to clear RNDIS filter\n",
   1810 		    device_xname(sc->sc_dev));
   1811 	}
   1812 	return rv;
   1813 }
   1814 
   1815 static void
   1816 hvn_rndis_detach(struct hvn_softc *sc)
   1817 {
   1818 	struct rndis_cmd *rc;
   1819 	struct rndis_halt_req *req;
   1820 	int rv;
   1821 
   1822 	rc = hvn_alloc_cmd(sc);
   1823 
   1824 	bus_dmamap_sync(sc->sc_dmat, rc->rc_dmap, 0, PAGE_SIZE,
   1825 	    BUS_DMASYNC_PREREAD);
   1826 
   1827 	rc->rc_id = atomic_inc_uint_nv(&sc->sc_rndisrid);
   1828 
   1829 	req = rc->rc_req;
   1830 	req->rm_type = REMOTE_NDIS_HALT_MSG;
   1831 	req->rm_len = sizeof(*req);
   1832 	req->rm_rid = rc->rc_id;
   1833 
   1834 	bus_dmamap_sync(sc->sc_dmat, rc->rc_dmap, 0, PAGE_SIZE,
   1835 	    BUS_DMASYNC_PREWRITE);
   1836 
   1837 	if ((rv = hvn_rndis_cmd(sc, rc, 500)) != 0) {
   1838 		DPRINTF("%s: HALT_MSG failed, error %d\n",
   1839 		    device_xname(sc->sc_dev), rv);
   1840 	}
   1841 	hvn_free_cmd(sc, rc);
   1842 }
   1843