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