Home | History | Annotate | Line # | Download | only in dev
vnet.c revision 1.5.4.2
      1  1.5.4.2  thorpej /*	$NetBSD: vnet.c,v 1.5.4.2 2021/04/03 22:28:38 thorpej Exp $	*/
      2  1.5.4.2  thorpej /*	$OpenBSD: vnet.c,v 1.62 2020/07/10 13:26:36 patrick Exp $	*/
      3  1.5.4.2  thorpej /*
      4  1.5.4.2  thorpej  * Copyright (c) 2009, 2015 Mark Kettenis
      5  1.5.4.2  thorpej  *
      6  1.5.4.2  thorpej  * Permission to use, copy, modify, and distribute this software for any
      7  1.5.4.2  thorpej  * purpose with or without fee is hereby granted, provided that the above
      8  1.5.4.2  thorpej  * copyright notice and this permission notice appear in all copies.
      9  1.5.4.2  thorpej  *
     10  1.5.4.2  thorpej  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     11  1.5.4.2  thorpej  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     12  1.5.4.2  thorpej  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     13  1.5.4.2  thorpej  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     14  1.5.4.2  thorpej  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     15  1.5.4.2  thorpej  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     16  1.5.4.2  thorpej  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     17  1.5.4.2  thorpej  */
     18  1.5.4.2  thorpej 
     19  1.5.4.2  thorpej #include <sys/kmem.h>
     20  1.5.4.2  thorpej #include <sys/param.h>
     21  1.5.4.2  thorpej #include <sys/atomic.h>
     22  1.5.4.2  thorpej #include <sys/callout.h>
     23  1.5.4.2  thorpej #include <sys/device.h>
     24  1.5.4.2  thorpej #include <sys/malloc.h>
     25  1.5.4.2  thorpej #include <sys/pool.h>
     26  1.5.4.2  thorpej #include <sys/mbuf.h>
     27  1.5.4.2  thorpej #include <sys/socket.h>
     28  1.5.4.2  thorpej #include <sys/sockio.h>
     29  1.5.4.2  thorpej #include <sys/systm.h>
     30  1.5.4.2  thorpej 
     31  1.5.4.2  thorpej #include <machine/autoconf.h>
     32  1.5.4.2  thorpej #include <machine/hypervisor.h>
     33  1.5.4.2  thorpej #include <machine/openfirm.h>
     34  1.5.4.2  thorpej 
     35  1.5.4.2  thorpej #include <net/if.h>
     36  1.5.4.2  thorpej #include <net/if_media.h>
     37  1.5.4.2  thorpej 
     38  1.5.4.2  thorpej #include <netinet/in.h>
     39  1.5.4.2  thorpej #include <net/if_ether.h>
     40  1.5.4.2  thorpej 
     41  1.5.4.2  thorpej #if NBPFILTER > 0
     42  1.5.4.2  thorpej #include <net/bpf.h>
     43  1.5.4.2  thorpej #endif
     44  1.5.4.2  thorpej 
     45  1.5.4.2  thorpej #include <uvm/uvm_extern.h>
     46  1.5.4.2  thorpej 
     47  1.5.4.2  thorpej #include <sparc64/dev/cbusvar.h>
     48  1.5.4.2  thorpej #include <sparc64/dev/ldcvar.h>
     49  1.5.4.2  thorpej #include <sparc64/dev/viovar.h>
     50  1.5.4.2  thorpej 
     51  1.5.4.2  thorpej #ifdef VNET_DEBUG
     52  1.5.4.2  thorpej #define DPRINTF(x)	printf x
     53  1.5.4.2  thorpej #else
     54  1.5.4.2  thorpej #define DPRINTF(x)
     55  1.5.4.2  thorpej #endif
     56  1.5.4.2  thorpej 
     57  1.5.4.2  thorpej #define VNET_TX_ENTRIES		32
     58  1.5.4.2  thorpej #define VNET_RX_ENTRIES		32
     59  1.5.4.2  thorpej 
     60  1.5.4.2  thorpej struct vnet_attr_info {
     61  1.5.4.2  thorpej 	struct vio_msg_tag	tag;
     62  1.5.4.2  thorpej 	uint8_t			xfer_mode;
     63  1.5.4.2  thorpej 	uint8_t			addr_type;
     64  1.5.4.2  thorpej 	uint16_t		ack_freq;
     65  1.5.4.2  thorpej 	uint32_t		_reserved1;
     66  1.5.4.2  thorpej 	uint64_t		addr;
     67  1.5.4.2  thorpej 	uint64_t		mtu;
     68  1.5.4.2  thorpej 	uint64_t		_reserved2[3];
     69  1.5.4.2  thorpej };
     70  1.5.4.2  thorpej 
     71  1.5.4.2  thorpej /* Address types. */
     72  1.5.4.2  thorpej #define VNET_ADDR_ETHERMAC	0x01
     73  1.5.4.2  thorpej 
     74  1.5.4.2  thorpej /* Sub-Type envelopes. */
     75  1.5.4.2  thorpej #define VNET_MCAST_INFO		0x0101
     76  1.5.4.2  thorpej 
     77  1.5.4.2  thorpej #define VNET_NUM_MCAST		7
     78  1.5.4.2  thorpej 
     79  1.5.4.2  thorpej struct vnet_mcast_info {
     80  1.5.4.2  thorpej 	struct vio_msg_tag	tag;
     81  1.5.4.2  thorpej 	uint8_t			set;
     82  1.5.4.2  thorpej 	uint8_t			count;
     83  1.5.4.2  thorpej 	uint8_t			mcast_addr[VNET_NUM_MCAST][ETHER_ADDR_LEN];
     84  1.5.4.2  thorpej 	uint32_t		_reserved;
     85  1.5.4.2  thorpej };
     86  1.5.4.2  thorpej 
     87  1.5.4.2  thorpej struct vnet_desc {
     88  1.5.4.2  thorpej 	struct vio_dring_hdr	hdr;
     89  1.5.4.2  thorpej 	uint32_t		nbytes;
     90  1.5.4.2  thorpej 	uint32_t		ncookies;
     91  1.5.4.2  thorpej 	struct ldc_cookie	cookie[2];
     92  1.5.4.2  thorpej };
     93  1.5.4.2  thorpej 
     94  1.5.4.2  thorpej struct vnet_desc_msg {
     95  1.5.4.2  thorpej 	struct vio_msg_tag	tag;
     96  1.5.4.2  thorpej 	uint64_t		seq_no;
     97  1.5.4.2  thorpej 	uint64_t		desc_handle;
     98  1.5.4.2  thorpej 	uint32_t		nbytes;
     99  1.5.4.2  thorpej 	uint32_t		ncookies;
    100  1.5.4.2  thorpej 	struct ldc_cookie	cookie[1];
    101  1.5.4.2  thorpej };
    102  1.5.4.2  thorpej 
    103  1.5.4.2  thorpej struct vnet_dring {
    104  1.5.4.2  thorpej 	bus_dmamap_t		vd_map;
    105  1.5.4.2  thorpej 	bus_dma_segment_t	vd_seg;
    106  1.5.4.2  thorpej 	struct vnet_desc	*vd_desc;
    107  1.5.4.2  thorpej 	int			vd_nentries;
    108  1.5.4.2  thorpej };
    109  1.5.4.2  thorpej 
    110  1.5.4.2  thorpej struct vnet_dring *vnet_dring_alloc(bus_dma_tag_t, int);
    111  1.5.4.2  thorpej void	vnet_dring_free(bus_dma_tag_t, struct vnet_dring *);
    112  1.5.4.2  thorpej 
    113  1.5.4.2  thorpej /*
    114  1.5.4.2  thorpej  * For now, we only support vNet 1.0.
    115  1.5.4.2  thorpej  */
    116  1.5.4.2  thorpej #define VNET_MAJOR	1
    117  1.5.4.2  thorpej #define VNET_MINOR	0
    118  1.5.4.2  thorpej 
    119  1.5.4.2  thorpej /*
    120  1.5.4.2  thorpej  * The vNet protocol wants the IP header to be 64-bit aligned, so
    121  1.5.4.2  thorpej  * define out own variant of ETHER_ALIGN.
    122  1.5.4.2  thorpej  */
    123  1.5.4.2  thorpej #define VNET_ETHER_ALIGN	6
    124  1.5.4.2  thorpej 
    125  1.5.4.2  thorpej struct vnet_soft_desc {
    126  1.5.4.2  thorpej 	int		vsd_map_idx;
    127  1.5.4.2  thorpej 	unsigned char *vsd_buf;
    128  1.5.4.2  thorpej };
    129  1.5.4.2  thorpej 
    130  1.5.4.2  thorpej struct vnet_softc {
    131  1.5.4.2  thorpej 	struct device	sc_dv;
    132  1.5.4.2  thorpej 	bus_space_tag_t	sc_bustag;
    133  1.5.4.2  thorpej 	bus_dma_tag_t	sc_dmatag;
    134  1.5.4.2  thorpej 
    135  1.5.4.2  thorpej 	uint64_t	sc_tx_ino;
    136  1.5.4.2  thorpej 	uint64_t	sc_rx_ino;
    137  1.5.4.2  thorpej 	void		*sc_tx_ih;
    138  1.5.4.2  thorpej 	void		*sc_rx_ih;
    139  1.5.4.2  thorpej 
    140  1.5.4.2  thorpej 	struct ldc_conn	sc_lc;
    141  1.5.4.2  thorpej 
    142  1.5.4.2  thorpej 	uint16_t	sc_vio_state;
    143  1.5.4.2  thorpej #define VIO_SND_VER_INFO	0x0001
    144  1.5.4.2  thorpej #define VIO_ACK_VER_INFO	0x0002
    145  1.5.4.2  thorpej #define VIO_RCV_VER_INFO	0x0004
    146  1.5.4.2  thorpej #define VIO_SND_ATTR_INFO	0x0008
    147  1.5.4.2  thorpej #define VIO_ACK_ATTR_INFO	0x0010
    148  1.5.4.2  thorpej #define VIO_RCV_ATTR_INFO	0x0020
    149  1.5.4.2  thorpej #define VIO_SND_DRING_REG	0x0040
    150  1.5.4.2  thorpej #define VIO_ACK_DRING_REG	0x0080
    151  1.5.4.2  thorpej #define VIO_RCV_DRING_REG	0x0100
    152  1.5.4.2  thorpej #define VIO_SND_RDX		0x0200
    153  1.5.4.2  thorpej #define VIO_ACK_RDX		0x0400
    154  1.5.4.2  thorpej #define VIO_RCV_RDX		0x0800
    155  1.5.4.2  thorpej 
    156  1.5.4.2  thorpej 	struct callout	sc_handshake_co;
    157  1.5.4.2  thorpej 
    158  1.5.4.2  thorpej 	uint8_t		sc_xfer_mode;
    159  1.5.4.2  thorpej 
    160  1.5.4.2  thorpej 	uint32_t	sc_local_sid;
    161  1.5.4.2  thorpej 	uint64_t	sc_dring_ident;
    162  1.5.4.2  thorpej 	uint64_t	sc_seq_no;
    163  1.5.4.2  thorpej 
    164  1.5.4.2  thorpej 	u_int		sc_tx_prod;
    165  1.5.4.2  thorpej 	u_int		sc_tx_cons;
    166  1.5.4.2  thorpej 
    167  1.5.4.2  thorpej 	u_int		sc_peer_state;
    168  1.5.4.2  thorpej 
    169  1.5.4.2  thorpej 	struct ldc_map	*sc_lm;
    170  1.5.4.2  thorpej 	struct vnet_dring *sc_vd;
    171  1.5.4.2  thorpej 	struct vnet_soft_desc *sc_vsd;
    172  1.5.4.2  thorpej #define VNET_NUM_SOFT_DESC	128
    173  1.5.4.2  thorpej 
    174  1.5.4.2  thorpej 	size_t		sc_peer_desc_size;
    175  1.5.4.2  thorpej 	struct ldc_cookie sc_peer_dring_cookie;
    176  1.5.4.2  thorpej 	int		sc_peer_dring_nentries;
    177  1.5.4.2  thorpej 
    178  1.5.4.2  thorpej 	struct pool	sc_pool;
    179  1.5.4.2  thorpej 
    180  1.5.4.2  thorpej 	struct ethercom	sc_ethercom;
    181  1.5.4.2  thorpej 	struct ifmedia	sc_media;
    182  1.5.4.2  thorpej 	u_int8_t sc_macaddr[ETHER_ADDR_LEN];
    183  1.5.4.2  thorpej };
    184  1.5.4.2  thorpej 
    185  1.5.4.2  thorpej int vnet_match (device_t, cfdata_t, void *);
    186  1.5.4.2  thorpej void vnet_attach (device_t, device_t, void *);
    187  1.5.4.2  thorpej 
    188  1.5.4.2  thorpej CFATTACH_DECL_NEW(vnet, sizeof(struct vnet_softc),
    189  1.5.4.2  thorpej     vnet_match, vnet_attach, NULL, NULL);
    190  1.5.4.2  thorpej 
    191  1.5.4.2  thorpej int	vnet_tx_intr(void *);
    192  1.5.4.2  thorpej int	vnet_rx_intr(void *);
    193  1.5.4.2  thorpej void	vnet_handshake(void *);
    194  1.5.4.2  thorpej 
    195  1.5.4.2  thorpej void	vio_rx_data(struct ldc_conn *, struct ldc_pkt *);
    196  1.5.4.2  thorpej void	vnet_rx_vio_ctrl(struct vnet_softc *, struct vio_msg *);
    197  1.5.4.2  thorpej void	vnet_rx_vio_ver_info(struct vnet_softc *, struct vio_msg_tag *);
    198  1.5.4.2  thorpej void	vnet_rx_vio_attr_info(struct vnet_softc *, struct vio_msg_tag *);
    199  1.5.4.2  thorpej void	vnet_rx_vio_dring_reg(struct vnet_softc *, struct vio_msg_tag *);
    200  1.5.4.2  thorpej void	vnet_rx_vio_rdx(struct vnet_softc *sc, struct vio_msg_tag *);
    201  1.5.4.2  thorpej void	vnet_rx_vio_mcast_info(struct vnet_softc *sc, struct vio_msg_tag *);
    202  1.5.4.2  thorpej void	vnet_rx_vio_data(struct vnet_softc *sc, struct vio_msg *);
    203  1.5.4.2  thorpej void	vnet_rx_vio_desc_data(struct vnet_softc *sc, struct vio_msg_tag *);
    204  1.5.4.2  thorpej void	vnet_rx_vio_dring_data(struct vnet_softc *sc, struct vio_msg_tag *);
    205  1.5.4.2  thorpej 
    206  1.5.4.2  thorpej void	vnet_ldc_reset(struct ldc_conn *);
    207  1.5.4.2  thorpej void	vnet_ldc_start(struct ldc_conn *);
    208  1.5.4.2  thorpej 
    209  1.5.4.2  thorpej void	vnet_sendmsg(struct vnet_softc *, void *, size_t);
    210  1.5.4.2  thorpej void	vnet_send_ver_info(struct vnet_softc *, uint16_t, uint16_t);
    211  1.5.4.2  thorpej void	vnet_send_attr_info(struct vnet_softc *);
    212  1.5.4.2  thorpej void	vnet_send_dring_reg(struct vnet_softc *);
    213  1.5.4.2  thorpej void	vio_send_rdx(struct vnet_softc *);
    214  1.5.4.2  thorpej void	vnet_send_dring_data(struct vnet_softc *, uint32_t);
    215  1.5.4.2  thorpej 
    216  1.5.4.2  thorpej void	vnet_start(struct ifnet *);
    217  1.5.4.2  thorpej void	vnet_start_desc(struct ifnet *);
    218  1.5.4.2  thorpej int		vnet_ioctl(struct ifnet *, u_long, void *);
    219  1.5.4.2  thorpej void	vnet_watchdog(struct ifnet *);
    220  1.5.4.2  thorpej 
    221  1.5.4.2  thorpej int		vnet_media_change(struct ifnet *);
    222  1.5.4.2  thorpej void	vnet_media_status(struct ifnet *, struct ifmediareq *);
    223  1.5.4.2  thorpej 
    224  1.5.4.2  thorpej void	vnet_link_state(struct vnet_softc *sc);
    225  1.5.4.2  thorpej 
    226  1.5.4.2  thorpej void	vnet_setmulti(struct vnet_softc *, int);
    227  1.5.4.2  thorpej 
    228  1.5.4.2  thorpej int		vnet_init(struct ifnet *);
    229  1.5.4.2  thorpej void	vnet_stop(struct ifnet *, int);
    230  1.5.4.2  thorpej 
    231  1.5.4.2  thorpej int vnet_match(device_t parent, cfdata_t match, void *aux)
    232  1.5.4.2  thorpej {
    233  1.5.4.2  thorpej 
    234  1.5.4.2  thorpej 	struct cbus_attach_args *ca = aux;
    235  1.5.4.2  thorpej 
    236  1.5.4.2  thorpej 	if (strcmp(ca->ca_name, "network") == 0)
    237  1.5.4.2  thorpej 		return (1);
    238  1.5.4.2  thorpej 
    239  1.5.4.2  thorpej 	return (0);
    240  1.5.4.2  thorpej }
    241  1.5.4.2  thorpej 
    242  1.5.4.2  thorpej void
    243  1.5.4.2  thorpej vnet_attach(struct device *parent, struct device *self, void *aux)
    244  1.5.4.2  thorpej {
    245  1.5.4.2  thorpej 	struct vnet_softc *sc = device_private(self);
    246  1.5.4.2  thorpej 	struct cbus_attach_args *ca = aux;
    247  1.5.4.2  thorpej 	struct ldc_conn *lc;
    248  1.5.4.2  thorpej 	struct ifnet *ifp;
    249  1.5.4.2  thorpej 
    250  1.5.4.2  thorpej 	sc->sc_bustag = ca->ca_bustag;
    251  1.5.4.2  thorpej 	sc->sc_dmatag = ca->ca_dmatag;
    252  1.5.4.2  thorpej 	sc->sc_tx_ino = ca->ca_tx_ino;
    253  1.5.4.2  thorpej 	sc->sc_rx_ino = ca->ca_rx_ino;
    254  1.5.4.2  thorpej 
    255  1.5.4.2  thorpej 	printf(": ivec 0x%" PRIx64 ", 0x%" PRIx64, sc->sc_tx_ino, sc->sc_rx_ino);
    256  1.5.4.2  thorpej 
    257  1.5.4.2  thorpej 	/*
    258  1.5.4.2  thorpej 	 * Un-configure queues before registering interrupt handlers,
    259  1.5.4.2  thorpej 	 * such that we dont get any stale LDC packets or events.
    260  1.5.4.2  thorpej 	 */
    261  1.5.4.2  thorpej 	hv_ldc_tx_qconf(ca->ca_id, 0, 0);
    262  1.5.4.2  thorpej 	hv_ldc_rx_qconf(ca->ca_id, 0, 0);
    263  1.5.4.2  thorpej 
    264  1.5.4.2  thorpej 	sc->sc_tx_ih = bus_intr_establish(ca->ca_bustag, sc->sc_tx_ino,
    265  1.5.4.2  thorpej 	    IPL_NET, vnet_tx_intr, sc);
    266  1.5.4.2  thorpej 	sc->sc_rx_ih = bus_intr_establish(ca->ca_bustag, sc->sc_rx_ino,
    267  1.5.4.2  thorpej 	    IPL_NET, vnet_rx_intr, sc);
    268  1.5.4.2  thorpej 	if (sc->sc_tx_ih == NULL || sc->sc_rx_ih == NULL) {
    269  1.5.4.2  thorpej 		printf(", can't establish interrupts\n");
    270  1.5.4.2  thorpej 		return;
    271  1.5.4.2  thorpej 	}
    272  1.5.4.2  thorpej 
    273  1.5.4.2  thorpej 	lc = &sc->sc_lc;
    274  1.5.4.2  thorpej 	lc->lc_id = ca->ca_id;
    275  1.5.4.2  thorpej 	lc->lc_sc = sc;
    276  1.5.4.2  thorpej 	lc->lc_reset = vnet_ldc_reset;
    277  1.5.4.2  thorpej 	lc->lc_start = vnet_ldc_start;
    278  1.5.4.2  thorpej 	lc->lc_rx_data = vio_rx_data;
    279  1.5.4.2  thorpej 
    280  1.5.4.2  thorpej 	callout_init(&sc->sc_handshake_co, 0);
    281  1.5.4.2  thorpej 
    282  1.5.4.2  thorpej 	sc->sc_peer_state = VIO_DP_STOPPED;
    283  1.5.4.2  thorpej 
    284  1.5.4.2  thorpej 	lc->lc_txq = ldc_queue_alloc(VNET_TX_ENTRIES);
    285  1.5.4.2  thorpej 	if (lc->lc_txq == NULL) {
    286  1.5.4.2  thorpej 		printf(", can't allocate tx queue\n");
    287  1.5.4.2  thorpej 		return;
    288  1.5.4.2  thorpej 	}
    289  1.5.4.2  thorpej 
    290  1.5.4.2  thorpej 	lc->lc_rxq = ldc_queue_alloc(VNET_RX_ENTRIES);
    291  1.5.4.2  thorpej 	if (lc->lc_rxq == NULL) {
    292  1.5.4.2  thorpej 		printf(", can't allocate rx queue\n");
    293  1.5.4.2  thorpej 		goto free_txqueue;
    294  1.5.4.2  thorpej 	}
    295  1.5.4.2  thorpej 
    296  1.5.4.2  thorpej 	if (OF_getprop(ca->ca_node, "local-mac-address",
    297  1.5.4.2  thorpej 				   sc->sc_macaddr, ETHER_ADDR_LEN) > 0) {
    298  1.5.4.2  thorpej 		printf(", address %s", ether_sprintf(sc->sc_macaddr));
    299  1.5.4.2  thorpej 	} else {
    300  1.5.4.2  thorpej 		printf(", cannot retrieve local mac address\n");
    301  1.5.4.2  thorpej 		return;
    302  1.5.4.2  thorpej 	}
    303  1.5.4.2  thorpej 
    304  1.5.4.2  thorpej 	/*
    305  1.5.4.2  thorpej 	 * Each interface gets its own pool.
    306  1.5.4.2  thorpej 	 */
    307  1.5.4.2  thorpej 	pool_init(&sc->sc_pool, 2048, 0, 0, 0, sc->sc_dv.dv_xname, NULL, IPL_NET);
    308  1.5.4.2  thorpej 
    309  1.5.4.2  thorpej 	ifp = &sc->sc_ethercom.ec_if;
    310  1.5.4.2  thorpej 	ifp->if_softc = sc;
    311  1.5.4.2  thorpej 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    312  1.5.4.2  thorpej 	ifp->if_init = vnet_init;
    313  1.5.4.2  thorpej 	ifp->if_ioctl = vnet_ioctl;
    314  1.5.4.2  thorpej 	ifp->if_start = vnet_start;
    315  1.5.4.2  thorpej 	ifp->if_stop = vnet_stop;
    316  1.5.4.2  thorpej 	ifp->if_watchdog = vnet_watchdog;
    317  1.5.4.2  thorpej 	strlcpy(ifp->if_xname, device_xname(self), IFNAMSIZ);
    318  1.5.4.2  thorpej 	IFQ_SET_MAXLEN(&ifp->if_snd, 31); /* XXX */
    319  1.5.4.2  thorpej 
    320  1.5.4.2  thorpej 	ifmedia_init(&sc->sc_media, 0, vnet_media_change, vnet_media_status);
    321  1.5.4.2  thorpej 	ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_AUTO, 0, NULL);
    322  1.5.4.2  thorpej 	ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_AUTO);
    323  1.5.4.2  thorpej 
    324  1.5.4.2  thorpej 	if_attach(ifp);
    325  1.5.4.2  thorpej 	ether_ifattach(ifp, sc->sc_macaddr);
    326  1.5.4.2  thorpej 
    327  1.5.4.2  thorpej 	printf("\n");
    328  1.5.4.2  thorpej 	return;
    329  1.5.4.2  thorpej free_txqueue:
    330  1.5.4.2  thorpej 	ldc_queue_free(lc->lc_txq);
    331  1.5.4.2  thorpej }
    332  1.5.4.2  thorpej 
    333  1.5.4.2  thorpej int
    334  1.5.4.2  thorpej vnet_tx_intr(void *arg)
    335  1.5.4.2  thorpej {
    336  1.5.4.2  thorpej 	struct vnet_softc *sc = arg;
    337  1.5.4.2  thorpej 	struct ldc_conn *lc = &sc->sc_lc;
    338  1.5.4.2  thorpej 	uint64_t tx_head, tx_tail, tx_state;
    339  1.5.4.2  thorpej 
    340  1.5.4.2  thorpej 	hv_ldc_tx_get_state(lc->lc_id, &tx_head, &tx_tail, &tx_state);
    341  1.5.4.2  thorpej 	if (tx_state != lc->lc_tx_state) {
    342  1.5.4.2  thorpej 		switch (tx_state) {
    343  1.5.4.2  thorpej 		case LDC_CHANNEL_DOWN:
    344  1.5.4.2  thorpej 			DPRINTF(("%s: Tx link down\n", __func__));
    345  1.5.4.2  thorpej 			break;
    346  1.5.4.2  thorpej 		case LDC_CHANNEL_UP:
    347  1.5.4.2  thorpej 			DPRINTF(("%s: Tx link up\n", __func__));
    348  1.5.4.2  thorpej 			break;
    349  1.5.4.2  thorpej 		case LDC_CHANNEL_RESET:
    350  1.5.4.2  thorpej 			DPRINTF(("%s: Tx link reset\n", __func__));
    351  1.5.4.2  thorpej 			break;
    352  1.5.4.2  thorpej 		}
    353  1.5.4.2  thorpej 		lc->lc_tx_state = tx_state;
    354  1.5.4.2  thorpej 	}
    355  1.5.4.2  thorpej 
    356  1.5.4.2  thorpej 	return (1);
    357  1.5.4.2  thorpej }
    358  1.5.4.2  thorpej 
    359  1.5.4.2  thorpej int
    360  1.5.4.2  thorpej vnet_rx_intr(void *arg)
    361  1.5.4.2  thorpej {
    362  1.5.4.2  thorpej 	struct vnet_softc *sc = arg;
    363  1.5.4.2  thorpej 	struct ldc_conn *lc = &sc->sc_lc;
    364  1.5.4.2  thorpej 	uint64_t rx_head, rx_tail, rx_state;
    365  1.5.4.2  thorpej 	struct ldc_pkt *lp;
    366  1.5.4.2  thorpej 	int err;
    367  1.5.4.2  thorpej 
    368  1.5.4.2  thorpej 	err = hv_ldc_rx_get_state(lc->lc_id, &rx_head, &rx_tail, &rx_state);
    369  1.5.4.2  thorpej 	if (err == H_EINVAL) {
    370  1.5.4.2  thorpej 		printf("hv_ldc_rx_get_state failed\n");
    371  1.5.4.2  thorpej 		return (0);
    372  1.5.4.2  thorpej 	}
    373  1.5.4.2  thorpej 	if (err != H_EOK) {
    374  1.5.4.2  thorpej 		printf("hv_ldc_rx_get_state %d\n", err);
    375  1.5.4.2  thorpej 		return (0);
    376  1.5.4.2  thorpej 	}
    377  1.5.4.2  thorpej 
    378  1.5.4.2  thorpej 	if (rx_state != lc->lc_rx_state) {
    379  1.5.4.2  thorpej 		switch (rx_state) {
    380  1.5.4.2  thorpej 		case LDC_CHANNEL_DOWN:
    381  1.5.4.2  thorpej 			lc->lc_tx_seqid = 0;
    382  1.5.4.2  thorpej 			lc->lc_state = 0;
    383  1.5.4.2  thorpej 			lc->lc_reset(lc);
    384  1.5.4.2  thorpej 			if (rx_head == rx_tail)
    385  1.5.4.2  thorpej 				break;
    386  1.5.4.2  thorpej 			/* Discard and ack pending I/O. */
    387  1.5.4.2  thorpej 			DPRINTF(("setting rx qhead to %" PRId64 "\n", rx_tail));
    388  1.5.4.2  thorpej 			err = hv_ldc_rx_set_qhead(lc->lc_id, rx_tail);
    389  1.5.4.2  thorpej 			if (err == H_EOK)
    390  1.5.4.2  thorpej 				break;
    391  1.5.4.2  thorpej 			printf("%s: hv_ldc_rx_set_qhead %d\n", __func__, err);
    392  1.5.4.2  thorpej 			break;
    393  1.5.4.2  thorpej 		case LDC_CHANNEL_UP:
    394  1.5.4.2  thorpej 			callout_reset(&sc->sc_handshake_co, hz / 2, vnet_handshake, sc);
    395  1.5.4.2  thorpej 			break;
    396  1.5.4.2  thorpej 		case LDC_CHANNEL_RESET:
    397  1.5.4.2  thorpej 			DPRINTF(("%s: Rx link reset\n", __func__));
    398  1.5.4.2  thorpej 			lc->lc_tx_seqid = 0;
    399  1.5.4.2  thorpej 			lc->lc_state = 0;
    400  1.5.4.2  thorpej 			lc->lc_reset(lc);
    401  1.5.4.2  thorpej 			callout_reset(&sc->sc_handshake_co, hz / 2, vnet_handshake, sc);
    402  1.5.4.2  thorpej 			if (rx_head == rx_tail) {
    403  1.5.4.2  thorpej 				break;
    404  1.5.4.2  thorpej 			}
    405  1.5.4.2  thorpej 			/* Discard and ack pending I/O. */
    406  1.5.4.2  thorpej 			DPRINTF(("setting rx qhead to %" PRId64 "\n", rx_tail));
    407  1.5.4.2  thorpej 			err = hv_ldc_rx_set_qhead(lc->lc_id, rx_tail);
    408  1.5.4.2  thorpej 			if (err == H_EOK)
    409  1.5.4.2  thorpej 				break;
    410  1.5.4.2  thorpej 			printf("%s: hv_ldc_rx_set_qhead %d\n", __func__, err);
    411  1.5.4.2  thorpej 			break;
    412  1.5.4.2  thorpej 		default:
    413  1.5.4.2  thorpej 			DPRINTF(("%s: unhandled rx_state %" PRIx64 "\n", __func__, rx_state));
    414  1.5.4.2  thorpej 			break;
    415  1.5.4.2  thorpej 		}
    416  1.5.4.2  thorpej 		lc->lc_rx_state = rx_state;
    417  1.5.4.2  thorpej 		return (1);
    418  1.5.4.2  thorpej 	} else {
    419  1.5.4.2  thorpej 	}
    420  1.5.4.2  thorpej 
    421  1.5.4.2  thorpej 	if (rx_head == rx_tail)
    422  1.5.4.2  thorpej 	{
    423  1.5.4.2  thorpej 		DPRINTF(("%s: head eq tail\n", __func__));
    424  1.5.4.2  thorpej 		return (0);
    425  1.5.4.2  thorpej 	}
    426  1.5.4.2  thorpej 	lp = (struct ldc_pkt *)(uintptr_t)(lc->lc_rxq->lq_va + rx_head);
    427  1.5.4.2  thorpej 	switch (lp->type) {
    428  1.5.4.2  thorpej 	case LDC_CTRL:
    429  1.5.4.2  thorpej 		DPRINTF(("%s: LDC_CTRL\n", __func__));
    430  1.5.4.2  thorpej 		ldc_rx_ctrl(lc, lp);
    431  1.5.4.2  thorpej 		break;
    432  1.5.4.2  thorpej 
    433  1.5.4.2  thorpej 	case LDC_DATA:
    434  1.5.4.2  thorpej 		DPRINTF(("%s: LDC_DATA\n", __func__));
    435  1.5.4.2  thorpej 		ldc_rx_data(lc, lp);
    436  1.5.4.2  thorpej 		break;
    437  1.5.4.2  thorpej 
    438  1.5.4.2  thorpej 	default:
    439  1.5.4.2  thorpej 		DPRINTF(("%s: unhandled type %0x02/%0x02/%0x02\n",
    440  1.5.4.2  thorpej 				 __func__, lp->type, lp->stype, lp->ctrl));
    441  1.5.4.2  thorpej 		Debugger();
    442  1.5.4.2  thorpej 		ldc_reset(lc);
    443  1.5.4.2  thorpej 		break;
    444  1.5.4.2  thorpej 	}
    445  1.5.4.2  thorpej 
    446  1.5.4.2  thorpej 	if (lc->lc_state == 0)
    447  1.5.4.2  thorpej 		return (1);
    448  1.5.4.2  thorpej 
    449  1.5.4.2  thorpej 	rx_head += sizeof(*lp);
    450  1.5.4.2  thorpej 	rx_head &= ((lc->lc_rxq->lq_nentries * sizeof(*lp)) - 1);
    451  1.5.4.2  thorpej 	err = hv_ldc_rx_set_qhead(lc->lc_id, rx_head);
    452  1.5.4.2  thorpej 	if (err != H_EOK)
    453  1.5.4.2  thorpej 		printf("%s: hv_ldc_rx_set_qhead %d\n", __func__, err);
    454  1.5.4.2  thorpej 	return (1);
    455  1.5.4.2  thorpej }
    456  1.5.4.2  thorpej 
    457  1.5.4.2  thorpej void
    458  1.5.4.2  thorpej vnet_handshake(void *arg)
    459  1.5.4.2  thorpej {
    460  1.5.4.2  thorpej 	struct vnet_softc *sc = arg;
    461  1.5.4.2  thorpej 
    462  1.5.4.2  thorpej 	ldc_send_vers(&sc->sc_lc);
    463  1.5.4.2  thorpej }
    464  1.5.4.2  thorpej 
    465  1.5.4.2  thorpej void
    466  1.5.4.2  thorpej vio_rx_data(struct ldc_conn *lc, struct ldc_pkt *lp)
    467  1.5.4.2  thorpej {
    468  1.5.4.2  thorpej 	struct vio_msg *vm = (struct vio_msg *)lp;
    469  1.5.4.2  thorpej 
    470  1.5.4.2  thorpej 	switch (vm->type) {
    471  1.5.4.2  thorpej 	case VIO_TYPE_CTRL:
    472  1.5.4.2  thorpej 		if ((lp->env & LDC_FRAG_START) == 0 &&
    473  1.5.4.2  thorpej 		    (lp->env & LDC_FRAG_STOP) == 0) {
    474  1.5.4.2  thorpej 			DPRINTF(("%s: FRAG_START==0 and FRAG_STOP==0\n", __func__));
    475  1.5.4.2  thorpej 			return;
    476  1.5.4.2  thorpej 		}
    477  1.5.4.2  thorpej 		vnet_rx_vio_ctrl(lc->lc_sc, vm);
    478  1.5.4.2  thorpej 		break;
    479  1.5.4.2  thorpej 
    480  1.5.4.2  thorpej 	case VIO_TYPE_DATA:
    481  1.5.4.2  thorpej 		if((lp->env & LDC_FRAG_START) == 0) {
    482  1.5.4.2  thorpej 			DPRINTF(("%s: FRAG_START==0\n", __func__));
    483  1.5.4.2  thorpej 			return;
    484  1.5.4.2  thorpej 		}
    485  1.5.4.2  thorpej 		vnet_rx_vio_data(lc->lc_sc, vm);
    486  1.5.4.2  thorpej 		break;
    487  1.5.4.2  thorpej 
    488  1.5.4.2  thorpej 	default:
    489  1.5.4.2  thorpej 		DPRINTF(("Unhandled packet type 0x%02x\n", vm->type));
    490  1.5.4.2  thorpej 		ldc_reset(lc);
    491  1.5.4.2  thorpej 		break;
    492  1.5.4.2  thorpej 	}
    493  1.5.4.2  thorpej }
    494  1.5.4.2  thorpej 
    495  1.5.4.2  thorpej void
    496  1.5.4.2  thorpej vnet_rx_vio_ctrl(struct vnet_softc *sc, struct vio_msg *vm)
    497  1.5.4.2  thorpej {
    498  1.5.4.2  thorpej 	struct vio_msg_tag *tag = (struct vio_msg_tag *)&vm->type;
    499  1.5.4.2  thorpej 
    500  1.5.4.2  thorpej 	switch (tag->stype_env) {
    501  1.5.4.2  thorpej 	case VIO_VER_INFO:
    502  1.5.4.2  thorpej 		vnet_rx_vio_ver_info(sc, tag);
    503  1.5.4.2  thorpej 		break;
    504  1.5.4.2  thorpej 	case VIO_ATTR_INFO:
    505  1.5.4.2  thorpej 		vnet_rx_vio_attr_info(sc, tag);
    506  1.5.4.2  thorpej 		break;
    507  1.5.4.2  thorpej 	case VIO_DRING_REG:
    508  1.5.4.2  thorpej 		vnet_rx_vio_dring_reg(sc, tag);
    509  1.5.4.2  thorpej 		break;
    510  1.5.4.2  thorpej 	case VIO_RDX:
    511  1.5.4.2  thorpej 		vnet_rx_vio_rdx(sc, tag);
    512  1.5.4.2  thorpej 		break;
    513  1.5.4.2  thorpej 	case VNET_MCAST_INFO:
    514  1.5.4.2  thorpej 		vnet_rx_vio_mcast_info(sc, tag);
    515  1.5.4.2  thorpej 		break;
    516  1.5.4.2  thorpej 	default:
    517  1.5.4.2  thorpej 		printf("%s: CTRL/0x%02x/0x%04x FIXME\n",
    518  1.5.4.2  thorpej 				 __func__, tag->stype, tag->stype_env);
    519  1.5.4.2  thorpej 		break;
    520  1.5.4.2  thorpej 	}
    521  1.5.4.2  thorpej }
    522  1.5.4.2  thorpej 
    523  1.5.4.2  thorpej void
    524  1.5.4.2  thorpej vnet_rx_vio_ver_info(struct vnet_softc *sc, struct vio_msg_tag *tag)
    525  1.5.4.2  thorpej {
    526  1.5.4.2  thorpej 	struct vio_ver_info *vi = (struct vio_ver_info *)tag;
    527  1.5.4.2  thorpej 
    528  1.5.4.2  thorpej 	switch (vi->tag.stype) {
    529  1.5.4.2  thorpej 	case VIO_SUBTYPE_INFO:
    530  1.5.4.2  thorpej 		DPRINTF(("CTRL/INFO/VER_INFO\n"));
    531  1.5.4.2  thorpej 
    532  1.5.4.2  thorpej 		/* Make sure we're talking to a virtual network device. */
    533  1.5.4.2  thorpej 		if (vi->dev_class != VDEV_NETWORK &&
    534  1.5.4.2  thorpej 		    vi->dev_class != VDEV_NETWORK_SWITCH) {
    535  1.5.4.2  thorpej 			DPRINTF(("Class is not network or network switch\n"));
    536  1.5.4.2  thorpej 			/* Huh, we're not talking to a network device? */
    537  1.5.4.2  thorpej 			printf("Not a network device\n");
    538  1.5.4.2  thorpej 			vi->tag.stype = VIO_SUBTYPE_NACK;
    539  1.5.4.2  thorpej 			vnet_sendmsg(sc, vi, sizeof(*vi));
    540  1.5.4.2  thorpej 			return;
    541  1.5.4.2  thorpej 		}
    542  1.5.4.2  thorpej 
    543  1.5.4.2  thorpej 		if (vi->major != VNET_MAJOR) {
    544  1.5.4.2  thorpej 			DPRINTF(("Major mismatch %" PRId8 " vs %" PRId8 "\n",
    545  1.5.4.2  thorpej 					 vi->major, VNET_MAJOR));
    546  1.5.4.2  thorpej 			vi->tag.stype = VIO_SUBTYPE_NACK;
    547  1.5.4.2  thorpej 			vi->major = VNET_MAJOR;
    548  1.5.4.2  thorpej 			vi->minor = VNET_MINOR;
    549  1.5.4.2  thorpej 			vnet_sendmsg(sc, vi, sizeof(*vi));
    550  1.5.4.2  thorpej 			return;
    551  1.5.4.2  thorpej 		}
    552  1.5.4.2  thorpej 
    553  1.5.4.2  thorpej 		vi->tag.stype = VIO_SUBTYPE_ACK;
    554  1.5.4.2  thorpej 		vi->tag.sid = sc->sc_local_sid;
    555  1.5.4.2  thorpej 		vi->minor = VNET_MINOR;
    556  1.5.4.2  thorpej 		vnet_sendmsg(sc, vi, sizeof(*vi));
    557  1.5.4.2  thorpej 		sc->sc_vio_state |= VIO_RCV_VER_INFO;
    558  1.5.4.2  thorpej 		break;
    559  1.5.4.2  thorpej 
    560  1.5.4.2  thorpej 	case VIO_SUBTYPE_ACK:
    561  1.5.4.2  thorpej 		DPRINTF(("CTRL/ACK/VER_INFO\n"));
    562  1.5.4.2  thorpej 		if (!ISSET(sc->sc_vio_state, VIO_SND_VER_INFO)) {
    563  1.5.4.2  thorpej 			ldc_reset(&sc->sc_lc);
    564  1.5.4.2  thorpej 			break;
    565  1.5.4.2  thorpej 		}
    566  1.5.4.2  thorpej 		sc->sc_vio_state |= VIO_ACK_VER_INFO;
    567  1.5.4.2  thorpej 		break;
    568  1.5.4.2  thorpej 
    569  1.5.4.2  thorpej 	default:
    570  1.5.4.2  thorpej 		DPRINTF(("CTRL/0x%02x/VER_INFO\n", vi->tag.stype));
    571  1.5.4.2  thorpej 		break;
    572  1.5.4.2  thorpej 	}
    573  1.5.4.2  thorpej 
    574  1.5.4.2  thorpej 	if (ISSET(sc->sc_vio_state, VIO_RCV_VER_INFO) &&
    575  1.5.4.2  thorpej 	    ISSET(sc->sc_vio_state, VIO_ACK_VER_INFO))
    576  1.5.4.2  thorpej 		vnet_send_attr_info(sc);
    577  1.5.4.2  thorpej }
    578  1.5.4.2  thorpej 
    579  1.5.4.2  thorpej void
    580  1.5.4.2  thorpej vnet_rx_vio_attr_info(struct vnet_softc *sc, struct vio_msg_tag *tag)
    581  1.5.4.2  thorpej {
    582  1.5.4.2  thorpej 	struct vnet_attr_info *ai = (struct vnet_attr_info *)tag;
    583  1.5.4.2  thorpej 
    584  1.5.4.2  thorpej 	switch (ai->tag.stype) {
    585  1.5.4.2  thorpej 	case VIO_SUBTYPE_INFO:
    586  1.5.4.2  thorpej 		DPRINTF(("CTRL/INFO/ATTR_INFO\n"));
    587  1.5.4.2  thorpej 		sc->sc_xfer_mode = ai->xfer_mode;
    588  1.5.4.2  thorpej 		ai->tag.stype = VIO_SUBTYPE_ACK;
    589  1.5.4.2  thorpej 		ai->tag.sid = sc->sc_local_sid;
    590  1.5.4.2  thorpej 		vnet_sendmsg(sc, ai, sizeof(*ai));
    591  1.5.4.2  thorpej 		sc->sc_vio_state |= VIO_RCV_ATTR_INFO;
    592  1.5.4.2  thorpej 		break;
    593  1.5.4.2  thorpej 
    594  1.5.4.2  thorpej 	case VIO_SUBTYPE_ACK:
    595  1.5.4.2  thorpej 		DPRINTF(("CTRL/ACK/ATTR_INFO\n"));
    596  1.5.4.2  thorpej 		if (!ISSET(sc->sc_vio_state, VIO_SND_ATTR_INFO)) {
    597  1.5.4.2  thorpej 			ldc_reset(&sc->sc_lc);
    598  1.5.4.2  thorpej 			break;
    599  1.5.4.2  thorpej 		}
    600  1.5.4.2  thorpej 		sc->sc_vio_state |= VIO_ACK_ATTR_INFO;
    601  1.5.4.2  thorpej 		break;
    602  1.5.4.2  thorpej 
    603  1.5.4.2  thorpej 	default:
    604  1.5.4.2  thorpej 		DPRINTF(("CTRL/0x%02x/ATTR_INFO\n", ai->tag.stype));
    605  1.5.4.2  thorpej 		break;
    606  1.5.4.2  thorpej 	}
    607  1.5.4.2  thorpej 
    608  1.5.4.2  thorpej 	if (ISSET(sc->sc_vio_state, VIO_RCV_ATTR_INFO) &&
    609  1.5.4.2  thorpej 	    ISSET(sc->sc_vio_state, VIO_ACK_ATTR_INFO)) {
    610  1.5.4.2  thorpej 		if (sc->sc_xfer_mode == VIO_DRING_MODE)
    611  1.5.4.2  thorpej 			vnet_send_dring_reg(sc);
    612  1.5.4.2  thorpej 		else
    613  1.5.4.2  thorpej 			vio_send_rdx(sc);
    614  1.5.4.2  thorpej 	}
    615  1.5.4.2  thorpej }
    616  1.5.4.2  thorpej 
    617  1.5.4.2  thorpej void
    618  1.5.4.2  thorpej vnet_rx_vio_dring_reg(struct vnet_softc *sc, struct vio_msg_tag *tag)
    619  1.5.4.2  thorpej {
    620  1.5.4.2  thorpej 	struct vio_dring_reg *dr = (struct vio_dring_reg *)tag;
    621  1.5.4.2  thorpej 
    622  1.5.4.2  thorpej 	switch (dr->tag.stype) {
    623  1.5.4.2  thorpej 	case VIO_SUBTYPE_INFO:
    624  1.5.4.2  thorpej 		DPRINTF(("CTRL/INFO/DRING_REG\n"));
    625  1.5.4.2  thorpej 		sc->sc_peer_dring_nentries = dr->num_descriptors;
    626  1.5.4.2  thorpej 		sc->sc_peer_desc_size = dr->descriptor_size;
    627  1.5.4.2  thorpej 		sc->sc_peer_dring_cookie = dr->cookie[0];
    628  1.5.4.2  thorpej 
    629  1.5.4.2  thorpej 		dr->tag.stype = VIO_SUBTYPE_ACK;
    630  1.5.4.2  thorpej 		dr->tag.sid = sc->sc_local_sid;
    631  1.5.4.2  thorpej 		vnet_sendmsg(sc, dr, sizeof(*dr));
    632  1.5.4.2  thorpej 		sc->sc_vio_state |= VIO_RCV_DRING_REG;
    633  1.5.4.2  thorpej 		break;
    634  1.5.4.2  thorpej 
    635  1.5.4.2  thorpej 	case VIO_SUBTYPE_ACK:
    636  1.5.4.2  thorpej 		DPRINTF(("CTRL/ACK/DRING_REG\n"));
    637  1.5.4.2  thorpej 		if (!ISSET(sc->sc_vio_state, VIO_SND_DRING_REG)) {
    638  1.5.4.2  thorpej 			ldc_reset(&sc->sc_lc);
    639  1.5.4.2  thorpej 			break;
    640  1.5.4.2  thorpej 		}
    641  1.5.4.2  thorpej 
    642  1.5.4.2  thorpej 		sc->sc_dring_ident = dr->dring_ident;
    643  1.5.4.2  thorpej 		sc->sc_seq_no = 1;
    644  1.5.4.2  thorpej 
    645  1.5.4.2  thorpej 		sc->sc_vio_state |= VIO_ACK_DRING_REG;
    646  1.5.4.2  thorpej 		break;
    647  1.5.4.2  thorpej 
    648  1.5.4.2  thorpej 	default:
    649  1.5.4.2  thorpej 		DPRINTF(("CTRL/0x%02x/DRING_REG\n", dr->tag.stype));
    650  1.5.4.2  thorpej 		break;
    651  1.5.4.2  thorpej 	}
    652  1.5.4.2  thorpej 
    653  1.5.4.2  thorpej 	if (ISSET(sc->sc_vio_state, VIO_RCV_DRING_REG) &&
    654  1.5.4.2  thorpej 	    ISSET(sc->sc_vio_state, VIO_ACK_DRING_REG))
    655  1.5.4.2  thorpej 		vio_send_rdx(sc);
    656  1.5.4.2  thorpej }
    657  1.5.4.2  thorpej 
    658  1.5.4.2  thorpej void
    659  1.5.4.2  thorpej vnet_rx_vio_rdx(struct vnet_softc *sc, struct vio_msg_tag *tag)
    660  1.5.4.2  thorpej {
    661  1.5.4.2  thorpej 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    662  1.5.4.2  thorpej 
    663  1.5.4.2  thorpej 	switch(tag->stype) {
    664  1.5.4.2  thorpej 	case VIO_SUBTYPE_INFO:
    665  1.5.4.2  thorpej 		DPRINTF(("CTRL/INFO/RDX\n"));
    666  1.5.4.2  thorpej 		tag->stype = VIO_SUBTYPE_ACK;
    667  1.5.4.2  thorpej 		tag->sid = sc->sc_local_sid;
    668  1.5.4.2  thorpej 		vnet_sendmsg(sc, tag, sizeof(*tag));
    669  1.5.4.2  thorpej 		sc->sc_vio_state |= VIO_RCV_RDX;
    670  1.5.4.2  thorpej 		break;
    671  1.5.4.2  thorpej 
    672  1.5.4.2  thorpej 	case VIO_SUBTYPE_ACK:
    673  1.5.4.2  thorpej 		DPRINTF(("CTRL/ACK/RDX\n"));
    674  1.5.4.2  thorpej 		if (!ISSET(sc->sc_vio_state, VIO_SND_RDX)) {
    675  1.5.4.2  thorpej 			ldc_reset(&sc->sc_lc);
    676  1.5.4.2  thorpej 			break;
    677  1.5.4.2  thorpej 		}
    678  1.5.4.2  thorpej 		sc->sc_vio_state |= VIO_ACK_RDX;
    679  1.5.4.2  thorpej 		break;
    680  1.5.4.2  thorpej 
    681  1.5.4.2  thorpej 	default:
    682  1.5.4.2  thorpej 		DPRINTF(("CTRL/0x%02x/RDX (VIO)\n", tag->stype));
    683  1.5.4.2  thorpej 		break;
    684  1.5.4.2  thorpej 	}
    685  1.5.4.2  thorpej 
    686  1.5.4.2  thorpej 	if (ISSET(sc->sc_vio_state, VIO_RCV_RDX) &&
    687  1.5.4.2  thorpej 	    ISSET(sc->sc_vio_state, VIO_ACK_RDX)) {
    688  1.5.4.2  thorpej 		/* Link is up! */
    689  1.5.4.2  thorpej 		vnet_link_state(sc);
    690  1.5.4.2  thorpej 
    691  1.5.4.2  thorpej 		/* Configure multicast now that we can. */
    692  1.5.4.2  thorpej 		vnet_setmulti(sc, 1);
    693  1.5.4.2  thorpej 
    694  1.5.4.2  thorpej 		KERNEL_LOCK(1, curlwp);
    695  1.5.4.2  thorpej 		ifp->if_flags &= ~IFF_OACTIVE;
    696  1.5.4.2  thorpej 		vnet_start(ifp);
    697  1.5.4.2  thorpej 		KERNEL_UNLOCK_ONE(curlwp);
    698  1.5.4.2  thorpej 	}
    699  1.5.4.2  thorpej }
    700  1.5.4.2  thorpej 
    701  1.5.4.2  thorpej void
    702  1.5.4.2  thorpej vnet_rx_vio_mcast_info(struct vnet_softc *sc, struct vio_msg_tag *tag)
    703  1.5.4.2  thorpej {
    704  1.5.4.2  thorpej 	switch(tag->stype) {
    705  1.5.4.2  thorpej 
    706  1.5.4.2  thorpej 		case VIO_SUBTYPE_INFO:
    707  1.5.4.2  thorpej 			DPRINTF(("CTRL/INFO/MCAST_INFO\n"));
    708  1.5.4.2  thorpej 			break;
    709  1.5.4.2  thorpej 
    710  1.5.4.2  thorpej 		case VIO_SUBTYPE_ACK:
    711  1.5.4.2  thorpej 			DPRINTF(("CTRL/ACK/MCAST_INFO\n"));
    712  1.5.4.2  thorpej 			break;
    713  1.5.4.2  thorpej 
    714  1.5.4.2  thorpej 		case VIO_SUBTYPE_NACK:
    715  1.5.4.2  thorpej 			DPRINTF(("CTRL/NACK/MCAST_INFO\n"));
    716  1.5.4.2  thorpej 			break;
    717  1.5.4.2  thorpej 
    718  1.5.4.2  thorpej 		default:
    719  1.5.4.2  thorpej 			printf("%s: CTRL/0x%02x/0x%04x\n",
    720  1.5.4.2  thorpej 				   __func__, tag->stype, tag->stype_env);
    721  1.5.4.2  thorpej 			break;
    722  1.5.4.2  thorpej 	}
    723  1.5.4.2  thorpej }
    724  1.5.4.2  thorpej 
    725  1.5.4.2  thorpej void
    726  1.5.4.2  thorpej vnet_rx_vio_data(struct vnet_softc *sc, struct vio_msg *vm)
    727  1.5.4.2  thorpej {
    728  1.5.4.2  thorpej 	struct vio_msg_tag *tag = (struct vio_msg_tag *)&vm->type;
    729  1.5.4.2  thorpej 
    730  1.5.4.2  thorpej 	if (!ISSET(sc->sc_vio_state, VIO_RCV_RDX) ||
    731  1.5.4.2  thorpej 	    !ISSET(sc->sc_vio_state, VIO_ACK_RDX)) {
    732  1.5.4.2  thorpej 		DPRINTF(("Spurious DATA/0x%02x/0x%04x\n", tag->stype,
    733  1.5.4.2  thorpej 		    tag->stype_env));
    734  1.5.4.2  thorpej 		return;
    735  1.5.4.2  thorpej 	}
    736  1.5.4.2  thorpej 
    737  1.5.4.2  thorpej 	switch(tag->stype_env) {
    738  1.5.4.2  thorpej 	case VIO_DESC_DATA:
    739  1.5.4.2  thorpej 		vnet_rx_vio_desc_data(sc, tag);
    740  1.5.4.2  thorpej 		break;
    741  1.5.4.2  thorpej 
    742  1.5.4.2  thorpej 	case VIO_DRING_DATA:
    743  1.5.4.2  thorpej 		vnet_rx_vio_dring_data(sc, tag);
    744  1.5.4.2  thorpej 		break;
    745  1.5.4.2  thorpej 
    746  1.5.4.2  thorpej 	default:
    747  1.5.4.2  thorpej 		DPRINTF(("DATA/0x%02x/0x%04x\n", tag->stype, tag->stype_env));
    748  1.5.4.2  thorpej 		break;
    749  1.5.4.2  thorpej 	}
    750  1.5.4.2  thorpej }
    751  1.5.4.2  thorpej 
    752  1.5.4.2  thorpej void
    753  1.5.4.2  thorpej vnet_rx_vio_desc_data(struct vnet_softc *sc, struct vio_msg_tag *tag)
    754  1.5.4.2  thorpej {
    755  1.5.4.2  thorpej 
    756  1.5.4.2  thorpej 	struct vnet_desc_msg *dm = (struct vnet_desc_msg *)tag;
    757  1.5.4.2  thorpej 	struct ldc_conn *lc = &sc->sc_lc;
    758  1.5.4.2  thorpej 	struct ldc_map *map = sc->sc_lm;
    759  1.5.4.2  thorpej 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    760  1.5.4.2  thorpej 	struct mbuf *m;
    761  1.5.4.2  thorpej 	unsigned char *buf;
    762  1.5.4.2  thorpej 	paddr_t pa;
    763  1.5.4.2  thorpej 	psize_t nbytes;
    764  1.5.4.2  thorpej 	u_int cons;
    765  1.5.4.2  thorpej 	int err;
    766  1.5.4.2  thorpej 
    767  1.5.4.2  thorpej 	switch(tag->stype) {
    768  1.5.4.2  thorpej 	case VIO_SUBTYPE_INFO:
    769  1.5.4.2  thorpej 		buf = pool_get(&sc->sc_pool, PR_NOWAIT|PR_ZERO);
    770  1.5.4.2  thorpej 		if (buf == NULL) {
    771  1.5.4.2  thorpej 			if_statinc(ifp, if_ierrors);
    772  1.5.4.2  thorpej 			goto skip;
    773  1.5.4.2  thorpej 		}
    774  1.5.4.2  thorpej 		nbytes = roundup(dm->nbytes, 8);
    775  1.5.4.2  thorpej 
    776  1.5.4.2  thorpej 		if (dm->nbytes > (ETHER_MAX_LEN - ETHER_CRC_LEN)) {
    777  1.5.4.2  thorpej 			if_statinc(ifp, if_ierrors);
    778  1.5.4.2  thorpej 			goto skip;
    779  1.5.4.2  thorpej 		}
    780  1.5.4.2  thorpej 
    781  1.5.4.2  thorpej 		pmap_extract(pmap_kernel(), (vaddr_t)buf, &pa);
    782  1.5.4.2  thorpej 		err = hv_ldc_copy(lc->lc_id, LDC_COPY_IN,
    783  1.5.4.2  thorpej 		    dm->cookie[0].addr, pa, nbytes, &nbytes);
    784  1.5.4.2  thorpej 		if (err != H_EOK) {
    785  1.5.4.2  thorpej 			pool_put(&sc->sc_pool, buf);
    786  1.5.4.2  thorpej 			if_statinc(ifp, if_ierrors);
    787  1.5.4.2  thorpej 			goto skip;
    788  1.5.4.2  thorpej 		}
    789  1.5.4.2  thorpej 
    790  1.5.4.2  thorpej 		/* Stupid OBP doesn't align properly. */
    791  1.5.4.2  thorpej 		m = m_devget(buf, dm->nbytes, 0, ifp);
    792  1.5.4.2  thorpej 		pool_put(&sc->sc_pool, buf);
    793  1.5.4.2  thorpej 		if (m == NULL) {
    794  1.5.4.2  thorpej 			if_statinc(ifp, if_ierrors);
    795  1.5.4.2  thorpej 			goto skip;
    796  1.5.4.2  thorpej 		}
    797  1.5.4.2  thorpej 
    798  1.5.4.2  thorpej 		/* Pass it on. */
    799  1.5.4.2  thorpej 		if_percpuq_enqueue(ifp->if_percpuq, m);
    800  1.5.4.2  thorpej 	skip:
    801  1.5.4.2  thorpej 		dm->tag.stype = VIO_SUBTYPE_ACK;
    802  1.5.4.2  thorpej 		dm->tag.sid = sc->sc_local_sid;
    803  1.5.4.2  thorpej 		vnet_sendmsg(sc, dm, sizeof(*dm));
    804  1.5.4.2  thorpej 		break;
    805  1.5.4.2  thorpej 
    806  1.5.4.2  thorpej 	case VIO_SUBTYPE_ACK:
    807  1.5.4.2  thorpej 		DPRINTF(("DATA/ACK/DESC_DATA\n"));
    808  1.5.4.2  thorpej 
    809  1.5.4.2  thorpej 		if (dm->desc_handle != sc->sc_tx_cons) {
    810  1.5.4.2  thorpej 			printf("out of order\n");
    811  1.5.4.2  thorpej 			return;
    812  1.5.4.2  thorpej 		}
    813  1.5.4.2  thorpej 
    814  1.5.4.2  thorpej 		cons = sc->sc_tx_cons & (sc->sc_vd->vd_nentries - 1);
    815  1.5.4.2  thorpej 
    816  1.5.4.2  thorpej 		map->lm_slot[sc->sc_vsd[cons].vsd_map_idx].entry = 0;
    817  1.5.4.2  thorpej 		atomic_dec_32(&map->lm_count);
    818  1.5.4.2  thorpej 
    819  1.5.4.2  thorpej 		pool_put(&sc->sc_pool, sc->sc_vsd[cons].vsd_buf);
    820  1.5.4.2  thorpej 		sc->sc_vsd[cons].vsd_buf = NULL;
    821  1.5.4.2  thorpej 
    822  1.5.4.2  thorpej 		sc->sc_tx_cons++;
    823  1.5.4.2  thorpej 		break;
    824  1.5.4.2  thorpej 
    825  1.5.4.2  thorpej 	case VIO_SUBTYPE_NACK:
    826  1.5.4.2  thorpej 		DPRINTF(("DATA/NACK/DESC_DATA\n"));
    827  1.5.4.2  thorpej 		break;
    828  1.5.4.2  thorpej 
    829  1.5.4.2  thorpej 	default:
    830  1.5.4.2  thorpej 		DPRINTF(("DATA/0x%02x/DESC_DATA\n", tag->stype));
    831  1.5.4.2  thorpej 		break;
    832  1.5.4.2  thorpej 	}
    833  1.5.4.2  thorpej }
    834  1.5.4.2  thorpej 
    835  1.5.4.2  thorpej void
    836  1.5.4.2  thorpej vnet_rx_vio_dring_data(struct vnet_softc *sc, struct vio_msg_tag *tag)
    837  1.5.4.2  thorpej {
    838  1.5.4.2  thorpej 	struct vio_dring_msg *dm = (struct vio_dring_msg *)tag;
    839  1.5.4.2  thorpej 	struct ldc_conn *lc = &sc->sc_lc;
    840  1.5.4.2  thorpej 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    841  1.5.4.2  thorpej 	struct mbuf *m = NULL;
    842  1.5.4.2  thorpej 	paddr_t pa;
    843  1.5.4.2  thorpej 	psize_t nbytes;
    844  1.5.4.2  thorpej 	int err;
    845  1.5.4.2  thorpej 
    846  1.5.4.2  thorpej 	switch(tag->stype) {
    847  1.5.4.2  thorpej 	case VIO_SUBTYPE_INFO:
    848  1.5.4.2  thorpej 	{
    849  1.5.4.2  thorpej 		DPRINTF(("%s: VIO_SUBTYPE_INFO\n", __func__));
    850  1.5.4.2  thorpej 		struct vnet_desc desc;
    851  1.5.4.2  thorpej 		uint64_t cookie;
    852  1.5.4.2  thorpej 		paddr_t desc_pa;
    853  1.5.4.2  thorpej 		int idx, ack_end_idx = -1;
    854  1.5.4.2  thorpej 
    855  1.5.4.2  thorpej 		idx = dm->start_idx;
    856  1.5.4.2  thorpej 		for (;;) {
    857  1.5.4.2  thorpej 			cookie = sc->sc_peer_dring_cookie.addr;
    858  1.5.4.2  thorpej 			cookie += idx * sc->sc_peer_desc_size;
    859  1.5.4.2  thorpej 			nbytes = sc->sc_peer_desc_size;
    860  1.5.4.2  thorpej 			pmap_extract(pmap_kernel(), (vaddr_t)&desc, &desc_pa);
    861  1.5.4.2  thorpej 			err = hv_ldc_copy(lc->lc_id, LDC_COPY_IN, cookie,
    862  1.5.4.2  thorpej 			    desc_pa, nbytes, &nbytes);
    863  1.5.4.2  thorpej 			if (err != H_EOK) {
    864  1.5.4.2  thorpej 				printf("hv_ldc_copy_in %d\n", err);
    865  1.5.4.2  thorpej 				break;
    866  1.5.4.2  thorpej 			}
    867  1.5.4.2  thorpej 
    868  1.5.4.2  thorpej 			if (desc.hdr.dstate != VIO_DESC_READY)
    869  1.5.4.2  thorpej 				break;
    870  1.5.4.2  thorpej 
    871  1.5.4.2  thorpej 			if (desc.nbytes > (ETHER_MAX_LEN - ETHER_CRC_LEN)) {
    872  1.5.4.2  thorpej 				if_statinc(ifp, if_ierrors);
    873  1.5.4.2  thorpej 				goto skip;
    874  1.5.4.2  thorpej 			}
    875  1.5.4.2  thorpej 
    876  1.5.4.2  thorpej 			MGETHDR(m, M_DONTWAIT, MT_DATA);
    877  1.5.4.2  thorpej 			if (m == NULL) {
    878  1.5.4.2  thorpej 				DPRINTF(("%s: MGETHDR failed\n", __func__));
    879  1.5.4.2  thorpej 				if_statinc(ifp, if_ierrors);
    880  1.5.4.2  thorpej 				goto skip;
    881  1.5.4.2  thorpej 			}
    882  1.5.4.2  thorpej 			MCLGET(m, M_DONTWAIT);
    883  1.5.4.2  thorpej 			if ((m->m_flags & M_EXT) == 0)
    884  1.5.4.2  thorpej 				break;
    885  1.5.4.2  thorpej 			m->m_len = m->m_pkthdr.len = desc.nbytes;
    886  1.5.4.2  thorpej 			nbytes = roundup(desc.nbytes + VNET_ETHER_ALIGN, 8);
    887  1.5.4.2  thorpej 
    888  1.5.4.2  thorpej 			pmap_extract(pmap_kernel(), (vaddr_t)m->m_data, &pa);
    889  1.5.4.2  thorpej 			err = hv_ldc_copy(lc->lc_id, LDC_COPY_IN,
    890  1.5.4.2  thorpej 			    desc.cookie[0].addr, pa, nbytes, &nbytes);
    891  1.5.4.2  thorpej 			if (err != H_EOK) {
    892  1.5.4.2  thorpej 				m_freem(m);
    893  1.5.4.2  thorpej 				goto skip;
    894  1.5.4.2  thorpej 			}
    895  1.5.4.2  thorpej 			m->m_data += VNET_ETHER_ALIGN;
    896  1.5.4.2  thorpej 			m_set_rcvif(m, ifp);
    897  1.5.4.2  thorpej 
    898  1.5.4.2  thorpej 			if_percpuq_enqueue(ifp->if_percpuq, m);
    899  1.5.4.2  thorpej 
    900  1.5.4.2  thorpej 		skip:
    901  1.5.4.2  thorpej 			desc.hdr.dstate = VIO_DESC_DONE;
    902  1.5.4.2  thorpej 			nbytes = sc->sc_peer_desc_size;
    903  1.5.4.2  thorpej 			err = hv_ldc_copy(lc->lc_id, LDC_COPY_OUT, cookie,
    904  1.5.4.2  thorpej 			    desc_pa, nbytes, &nbytes);
    905  1.5.4.2  thorpej 			if (err != H_EOK)
    906  1.5.4.2  thorpej 				printf("hv_ldc_copy_out %d\n", err);
    907  1.5.4.2  thorpej 
    908  1.5.4.2  thorpej 			ack_end_idx = idx;
    909  1.5.4.2  thorpej 			if (++idx == sc->sc_peer_dring_nentries)
    910  1.5.4.2  thorpej 				idx = 0;
    911  1.5.4.2  thorpej 		}
    912  1.5.4.2  thorpej 
    913  1.5.4.2  thorpej 		if (ack_end_idx == -1) {
    914  1.5.4.2  thorpej 			dm->tag.stype = VIO_SUBTYPE_NACK;
    915  1.5.4.2  thorpej 		} else {
    916  1.5.4.2  thorpej 			dm->tag.stype = VIO_SUBTYPE_ACK;
    917  1.5.4.2  thorpej 			dm->end_idx = ack_end_idx;
    918  1.5.4.2  thorpej 		}
    919  1.5.4.2  thorpej 		dm->tag.sid = sc->sc_local_sid;
    920  1.5.4.2  thorpej 		dm->proc_state = VIO_DP_STOPPED;
    921  1.5.4.2  thorpej 		vnet_sendmsg(sc, dm, sizeof(*dm));
    922  1.5.4.2  thorpej 		break;
    923  1.5.4.2  thorpej 	}
    924  1.5.4.2  thorpej 
    925  1.5.4.2  thorpej 	case VIO_SUBTYPE_ACK:
    926  1.5.4.2  thorpej 	{
    927  1.5.4.2  thorpej 		DPRINTF(("%s: VIO_SUBTYPE_ACK\n", __func__));
    928  1.5.4.2  thorpej 		struct ldc_map *map = sc->sc_lm;
    929  1.5.4.2  thorpej 		u_int cons, count;
    930  1.5.4.2  thorpej 
    931  1.5.4.2  thorpej 		sc->sc_peer_state = dm->proc_state;
    932  1.5.4.2  thorpej 
    933  1.5.4.2  thorpej 		cons = sc->sc_tx_cons & (sc->sc_vd->vd_nentries - 1);
    934  1.5.4.2  thorpej 		while (sc->sc_vd->vd_desc[cons].hdr.dstate == VIO_DESC_DONE) {
    935  1.5.4.2  thorpej 			map->lm_slot[sc->sc_vsd[cons].vsd_map_idx].entry = 0;
    936  1.5.4.2  thorpej 			atomic_dec_32(&map->lm_count);
    937  1.5.4.2  thorpej 
    938  1.5.4.2  thorpej 			pool_put(&sc->sc_pool, sc->sc_vsd[cons].vsd_buf);
    939  1.5.4.2  thorpej 			sc->sc_vsd[cons].vsd_buf = NULL;
    940  1.5.4.2  thorpej 
    941  1.5.4.2  thorpej 			sc->sc_vd->vd_desc[cons].hdr.dstate = VIO_DESC_FREE;
    942  1.5.4.2  thorpej 			sc->sc_tx_cons++;
    943  1.5.4.2  thorpej 			cons = sc->sc_tx_cons & (sc->sc_vd->vd_nentries - 1);
    944  1.5.4.2  thorpej 		}
    945  1.5.4.2  thorpej 
    946  1.5.4.2  thorpej 		count = sc->sc_tx_prod - sc->sc_tx_cons;
    947  1.5.4.2  thorpej 		if (count > 0 && sc->sc_peer_state != VIO_DP_ACTIVE)
    948  1.5.4.2  thorpej 			vnet_send_dring_data(sc, cons);
    949  1.5.4.2  thorpej 
    950  1.5.4.2  thorpej 		KERNEL_LOCK(1, curlwp);
    951  1.5.4.2  thorpej 		if (count < (sc->sc_vd->vd_nentries - 1))
    952  1.5.4.2  thorpej 			ifp->if_flags &= ~IFF_OACTIVE;
    953  1.5.4.2  thorpej 		if (count == 0)
    954  1.5.4.2  thorpej 			ifp->if_timer = 0;
    955  1.5.4.2  thorpej 
    956  1.5.4.2  thorpej 		vnet_start(ifp);
    957  1.5.4.2  thorpej 		KERNEL_UNLOCK_ONE(curlwp);
    958  1.5.4.2  thorpej 		break;
    959  1.5.4.2  thorpej 	}
    960  1.5.4.2  thorpej 
    961  1.5.4.2  thorpej 	case VIO_SUBTYPE_NACK:
    962  1.5.4.2  thorpej 		DPRINTF(("DATA/NACK/DRING_DATA\n"));
    963  1.5.4.2  thorpej 		sc->sc_peer_state = VIO_DP_STOPPED;
    964  1.5.4.2  thorpej 		break;
    965  1.5.4.2  thorpej 
    966  1.5.4.2  thorpej 	default:
    967  1.5.4.2  thorpej 		DPRINTF(("DATA/0x%02x/DRING_DATA\n", tag->stype));
    968  1.5.4.2  thorpej 		break;
    969  1.5.4.2  thorpej 	}
    970  1.5.4.2  thorpej }
    971  1.5.4.2  thorpej 
    972  1.5.4.2  thorpej void
    973  1.5.4.2  thorpej vnet_ldc_reset(struct ldc_conn *lc)
    974  1.5.4.2  thorpej {
    975  1.5.4.2  thorpej 
    976  1.5.4.2  thorpej 	struct vnet_softc *sc = lc->lc_sc;
    977  1.5.4.2  thorpej 	int i;
    978  1.5.4.2  thorpej 
    979  1.5.4.2  thorpej 	callout_stop(&sc->sc_handshake_co);
    980  1.5.4.2  thorpej 	sc->sc_tx_prod = sc->sc_tx_cons = 0;
    981  1.5.4.2  thorpej 	sc->sc_peer_state = VIO_DP_STOPPED;
    982  1.5.4.2  thorpej 	sc->sc_vio_state = 0;
    983  1.5.4.2  thorpej 	vnet_link_state(sc);
    984  1.5.4.2  thorpej 
    985  1.5.4.2  thorpej 	sc->sc_lm->lm_next = 1;
    986  1.5.4.2  thorpej 	sc->sc_lm->lm_count = 1;
    987  1.5.4.2  thorpej 	for (i = 1; i < sc->sc_lm->lm_nentries; i++)
    988  1.5.4.2  thorpej 		sc->sc_lm->lm_slot[i].entry = 0;
    989  1.5.4.2  thorpej 
    990  1.5.4.2  thorpej 	for (i = 0; i < sc->sc_vd->vd_nentries; i++) {
    991  1.5.4.2  thorpej 		if (sc->sc_vsd[i].vsd_buf) {
    992  1.5.4.2  thorpej 			pool_put(&sc->sc_pool, sc->sc_vsd[i].vsd_buf);
    993  1.5.4.2  thorpej 			sc->sc_vsd[i].vsd_buf = NULL;
    994  1.5.4.2  thorpej 		}
    995  1.5.4.2  thorpej 		sc->sc_vd->vd_desc[i].hdr.dstate = VIO_DESC_FREE;
    996  1.5.4.2  thorpej 	}
    997  1.5.4.2  thorpej }
    998  1.5.4.2  thorpej 
    999  1.5.4.2  thorpej void
   1000  1.5.4.2  thorpej vnet_ldc_start(struct ldc_conn *lc)
   1001  1.5.4.2  thorpej {
   1002  1.5.4.2  thorpej 	struct vnet_softc *sc = lc->lc_sc;
   1003  1.5.4.2  thorpej 	callout_stop(&sc->sc_handshake_co);
   1004  1.5.4.2  thorpej 	vnet_send_ver_info(sc, VNET_MAJOR, VNET_MINOR);
   1005  1.5.4.2  thorpej }
   1006  1.5.4.2  thorpej 
   1007  1.5.4.2  thorpej void
   1008  1.5.4.2  thorpej vnet_sendmsg(struct vnet_softc *sc, void *msg, size_t len)
   1009  1.5.4.2  thorpej {
   1010  1.5.4.2  thorpej 	struct ldc_conn *lc = &sc->sc_lc;
   1011  1.5.4.2  thorpej 	int err;
   1012  1.5.4.2  thorpej 
   1013  1.5.4.2  thorpej 	err = ldc_send_unreliable(lc, msg, len);
   1014  1.5.4.2  thorpej 	if (err)
   1015  1.5.4.2  thorpej 		printf("%s: ldc_send_unreliable: %d\n", __func__, err);
   1016  1.5.4.2  thorpej }
   1017  1.5.4.2  thorpej 
   1018  1.5.4.2  thorpej void
   1019  1.5.4.2  thorpej vnet_send_ver_info(struct vnet_softc *sc, uint16_t major, uint16_t minor)
   1020  1.5.4.2  thorpej {
   1021  1.5.4.2  thorpej 	struct vio_ver_info vi;
   1022  1.5.4.2  thorpej 
   1023  1.5.4.2  thorpej 	bzero(&vi, sizeof(vi));
   1024  1.5.4.2  thorpej 	vi.tag.type = VIO_TYPE_CTRL;
   1025  1.5.4.2  thorpej 	vi.tag.stype = VIO_SUBTYPE_INFO;
   1026  1.5.4.2  thorpej 	vi.tag.stype_env = VIO_VER_INFO;
   1027  1.5.4.2  thorpej 	vi.tag.sid = sc->sc_local_sid;
   1028  1.5.4.2  thorpej 	vi.major = major;
   1029  1.5.4.2  thorpej 	vi.minor = minor;
   1030  1.5.4.2  thorpej 	vi.dev_class = VDEV_NETWORK;
   1031  1.5.4.2  thorpej 	vnet_sendmsg(sc, &vi, sizeof(vi));
   1032  1.5.4.2  thorpej 
   1033  1.5.4.2  thorpej 	sc->sc_vio_state |= VIO_SND_VER_INFO;
   1034  1.5.4.2  thorpej }
   1035  1.5.4.2  thorpej 
   1036  1.5.4.2  thorpej void
   1037  1.5.4.2  thorpej vnet_send_attr_info(struct vnet_softc *sc)
   1038  1.5.4.2  thorpej {
   1039  1.5.4.2  thorpej 	struct vnet_attr_info ai;
   1040  1.5.4.2  thorpej 	int i;
   1041  1.5.4.2  thorpej 
   1042  1.5.4.2  thorpej 	bzero(&ai, sizeof(ai));
   1043  1.5.4.2  thorpej 	ai.tag.type = VIO_TYPE_CTRL;
   1044  1.5.4.2  thorpej 	ai.tag.stype = VIO_SUBTYPE_INFO;
   1045  1.5.4.2  thorpej 	ai.tag.stype_env = VIO_ATTR_INFO;
   1046  1.5.4.2  thorpej 	ai.tag.sid = sc->sc_local_sid;
   1047  1.5.4.2  thorpej 	ai.xfer_mode = VIO_DRING_MODE;
   1048  1.5.4.2  thorpej 	ai.addr_type = VNET_ADDR_ETHERMAC;
   1049  1.5.4.2  thorpej 	ai.ack_freq = 0;
   1050  1.5.4.2  thorpej 	ai.addr = 0;
   1051  1.5.4.2  thorpej 	for (i = 0; i < ETHER_ADDR_LEN; i++) {
   1052  1.5.4.2  thorpej 		ai.addr <<= 8;
   1053  1.5.4.2  thorpej 		ai.addr |= sc->sc_macaddr[i];
   1054  1.5.4.2  thorpej 	}
   1055  1.5.4.2  thorpej 	ai.mtu = ETHER_MAX_LEN - ETHER_CRC_LEN;
   1056  1.5.4.2  thorpej 	vnet_sendmsg(sc, &ai, sizeof(ai));
   1057  1.5.4.2  thorpej 
   1058  1.5.4.2  thorpej 	sc->sc_vio_state |= VIO_SND_ATTR_INFO;
   1059  1.5.4.2  thorpej }
   1060  1.5.4.2  thorpej 
   1061  1.5.4.2  thorpej void
   1062  1.5.4.2  thorpej vnet_send_dring_reg(struct vnet_softc *sc)
   1063  1.5.4.2  thorpej {
   1064  1.5.4.2  thorpej 	struct vio_dring_reg dr;
   1065  1.5.4.2  thorpej 
   1066  1.5.4.2  thorpej 	bzero(&dr, sizeof(dr));
   1067  1.5.4.2  thorpej 	dr.tag.type = VIO_TYPE_CTRL;
   1068  1.5.4.2  thorpej 	dr.tag.stype = VIO_SUBTYPE_INFO;
   1069  1.5.4.2  thorpej 	dr.tag.stype_env = VIO_DRING_REG;
   1070  1.5.4.2  thorpej 	dr.tag.sid = sc->sc_local_sid;
   1071  1.5.4.2  thorpej 	dr.dring_ident = 0;
   1072  1.5.4.2  thorpej 	dr.num_descriptors = sc->sc_vd->vd_nentries;
   1073  1.5.4.2  thorpej 	dr.descriptor_size = sizeof(struct vnet_desc);
   1074  1.5.4.2  thorpej 	dr.options = VIO_TX_RING;
   1075  1.5.4.2  thorpej 	dr.ncookies = 1;
   1076  1.5.4.2  thorpej 	dr.cookie[0].addr = 0;
   1077  1.5.4.2  thorpej 	dr.cookie[0].size = PAGE_SIZE;
   1078  1.5.4.2  thorpej 	vnet_sendmsg(sc, &dr, sizeof(dr));
   1079  1.5.4.2  thorpej 
   1080  1.5.4.2  thorpej 	sc->sc_vio_state |= VIO_SND_DRING_REG;
   1081  1.5.4.2  thorpej };
   1082  1.5.4.2  thorpej 
   1083  1.5.4.2  thorpej void
   1084  1.5.4.2  thorpej vio_send_rdx(struct vnet_softc *sc)
   1085  1.5.4.2  thorpej {
   1086  1.5.4.2  thorpej 	struct vio_msg_tag tag;
   1087  1.5.4.2  thorpej 
   1088  1.5.4.2  thorpej 	tag.type = VIO_TYPE_CTRL;
   1089  1.5.4.2  thorpej 	tag.stype = VIO_SUBTYPE_INFO;
   1090  1.5.4.2  thorpej 	tag.stype_env = VIO_RDX;
   1091  1.5.4.2  thorpej 	tag.sid = sc->sc_local_sid;
   1092  1.5.4.2  thorpej 	vnet_sendmsg(sc, &tag, sizeof(tag));
   1093  1.5.4.2  thorpej 
   1094  1.5.4.2  thorpej 	sc->sc_vio_state |= VIO_SND_RDX;
   1095  1.5.4.2  thorpej }
   1096  1.5.4.2  thorpej 
   1097  1.5.4.2  thorpej void
   1098  1.5.4.2  thorpej vnet_send_dring_data(struct vnet_softc *sc, uint32_t start_idx)
   1099  1.5.4.2  thorpej {
   1100  1.5.4.2  thorpej 	struct vio_dring_msg dm;
   1101  1.5.4.2  thorpej 	u_int peer_state;
   1102  1.5.4.2  thorpej 
   1103  1.5.4.2  thorpej 	peer_state = atomic_swap_uint(&sc->sc_peer_state, VIO_DP_ACTIVE);
   1104  1.5.4.2  thorpej 	if (peer_state == VIO_DP_ACTIVE) {
   1105  1.5.4.2  thorpej 		DPRINTF(("%s: peer_state == VIO_DP_ACTIVE\n", __func__));
   1106  1.5.4.2  thorpej 		return;
   1107  1.5.4.2  thorpej 	}
   1108  1.5.4.2  thorpej 
   1109  1.5.4.2  thorpej 	bzero(&dm, sizeof(dm));
   1110  1.5.4.2  thorpej 	dm.tag.type = VIO_TYPE_DATA;
   1111  1.5.4.2  thorpej 	dm.tag.stype = VIO_SUBTYPE_INFO;
   1112  1.5.4.2  thorpej 	dm.tag.stype_env = VIO_DRING_DATA;
   1113  1.5.4.2  thorpej 	dm.tag.sid = sc->sc_local_sid;
   1114  1.5.4.2  thorpej 	dm.seq_no = sc->sc_seq_no++;
   1115  1.5.4.2  thorpej 	dm.dring_ident = sc->sc_dring_ident;
   1116  1.5.4.2  thorpej 	dm.start_idx = start_idx;
   1117  1.5.4.2  thorpej 	dm.end_idx = -1;
   1118  1.5.4.2  thorpej 	vnet_sendmsg(sc, &dm, sizeof(dm));
   1119  1.5.4.2  thorpej }
   1120  1.5.4.2  thorpej 
   1121  1.5.4.2  thorpej void
   1122  1.5.4.2  thorpej vnet_start(struct ifnet *ifp)
   1123  1.5.4.2  thorpej {
   1124  1.5.4.2  thorpej 	struct vnet_softc *sc = ifp->if_softc;
   1125  1.5.4.2  thorpej 	struct ldc_conn *lc = &sc->sc_lc;
   1126  1.5.4.2  thorpej 	struct ldc_map *map = sc->sc_lm;
   1127  1.5.4.2  thorpej 	struct mbuf *m;
   1128  1.5.4.2  thorpej 	paddr_t pa;
   1129  1.5.4.2  thorpej 	unsigned char *buf;
   1130  1.5.4.2  thorpej 	uint64_t tx_head, tx_tail, tx_state;
   1131  1.5.4.2  thorpej 	u_int start, prod, count;
   1132  1.5.4.2  thorpej 	int err;
   1133  1.5.4.2  thorpej 	if (!(ifp->if_flags & IFF_RUNNING))
   1134  1.5.4.2  thorpej 	{
   1135  1.5.4.2  thorpej 		DPRINTF(("%s: not in RUNNING state\n", __func__));
   1136  1.5.4.2  thorpej 		return;
   1137  1.5.4.2  thorpej 	}
   1138  1.5.4.2  thorpej 	if (ifp->if_flags & IFF_OACTIVE)
   1139  1.5.4.2  thorpej 	{
   1140  1.5.4.2  thorpej 		DPRINTF(("%s: already active\n", __func__));
   1141  1.5.4.2  thorpej 		return;
   1142  1.5.4.2  thorpej 	}
   1143  1.5.4.2  thorpej 
   1144  1.5.4.2  thorpej 	if (IFQ_IS_EMPTY(&ifp->if_snd))
   1145  1.5.4.2  thorpej 	{
   1146  1.5.4.2  thorpej 		DPRINTF(("%s: queue is empty\n", __func__));
   1147  1.5.4.2  thorpej 		return;
   1148  1.5.4.2  thorpej 	} else {
   1149  1.5.4.2  thorpej 		DPRINTF(("%s: queue size %d\n", __func__, ifp->if_snd.ifq_len));
   1150  1.5.4.2  thorpej 	}
   1151  1.5.4.2  thorpej 
   1152  1.5.4.2  thorpej 	/*
   1153  1.5.4.2  thorpej 	 * We cannot transmit packets until a VIO connection has been
   1154  1.5.4.2  thorpej 	 * established.
   1155  1.5.4.2  thorpej 	 */
   1156  1.5.4.2  thorpej 	if (!ISSET(sc->sc_vio_state, VIO_RCV_RDX) ||
   1157  1.5.4.2  thorpej 	    !ISSET(sc->sc_vio_state, VIO_ACK_RDX))
   1158  1.5.4.2  thorpej 	{
   1159  1.5.4.2  thorpej 		DPRINTF(("%s: vio connection not established yet\n", __func__));
   1160  1.5.4.2  thorpej 		return;
   1161  1.5.4.2  thorpej 	}
   1162  1.5.4.2  thorpej 
   1163  1.5.4.2  thorpej 	/*
   1164  1.5.4.2  thorpej 	 * Make sure there is room in the LDC transmit queue to send a
   1165  1.5.4.2  thorpej 	 * DRING_DATA message.
   1166  1.5.4.2  thorpej 	 */
   1167  1.5.4.2  thorpej 	err = hv_ldc_tx_get_state(lc->lc_id, &tx_head, &tx_tail, &tx_state);
   1168  1.5.4.2  thorpej 	if (err != H_EOK) {
   1169  1.5.4.2  thorpej 		DPRINTF(("%s: no room in ldc transmit queue\n", __func__));
   1170  1.5.4.2  thorpej 		return;
   1171  1.5.4.2  thorpej 	}
   1172  1.5.4.2  thorpej 	tx_tail += sizeof(struct ldc_pkt);
   1173  1.5.4.2  thorpej 	tx_tail &= ((lc->lc_txq->lq_nentries * sizeof(struct ldc_pkt)) - 1);
   1174  1.5.4.2  thorpej 	if (tx_tail == tx_head) {
   1175  1.5.4.2  thorpej 		ifp->if_flags |= IFF_OACTIVE;
   1176  1.5.4.2  thorpej 		{
   1177  1.5.4.2  thorpej 			DPRINTF(("%s: tail equals head\n", __func__));
   1178  1.5.4.2  thorpej 			return;
   1179  1.5.4.2  thorpej 		}
   1180  1.5.4.2  thorpej 	}
   1181  1.5.4.2  thorpej 
   1182  1.5.4.2  thorpej 	if (sc->sc_xfer_mode == VIO_DESC_MODE) {
   1183  1.5.4.2  thorpej 		DPRINTF(("%s: vio_desc_mode\n", __func__));
   1184  1.5.4.2  thorpej 		vnet_start_desc(ifp);
   1185  1.5.4.2  thorpej 		return;
   1186  1.5.4.2  thorpej 	}
   1187  1.5.4.2  thorpej 
   1188  1.5.4.2  thorpej 	start = prod = sc->sc_tx_prod & (sc->sc_vd->vd_nentries - 1);
   1189  1.5.4.2  thorpej 	while (sc->sc_vd->vd_desc[prod].hdr.dstate == VIO_DESC_FREE) {
   1190  1.5.4.2  thorpej 		count = sc->sc_tx_prod - sc->sc_tx_cons;
   1191  1.5.4.2  thorpej 		if (count >= (sc->sc_vd->vd_nentries - 1) ||
   1192  1.5.4.2  thorpej 		    map->lm_count >= map->lm_nentries) {
   1193  1.5.4.2  thorpej 			DPRINTF(("%s: count issue\n", __func__));
   1194  1.5.4.2  thorpej 			ifp->if_flags |= IFF_OACTIVE;
   1195  1.5.4.2  thorpej 			break;
   1196  1.5.4.2  thorpej 		}
   1197  1.5.4.2  thorpej 
   1198  1.5.4.2  thorpej 		buf = pool_get(&sc->sc_pool, PR_NOWAIT|PR_ZERO);
   1199  1.5.4.2  thorpej 		if (buf == NULL) {
   1200  1.5.4.2  thorpej 			DPRINTF(("%s: buff is NULL\n", __func__));
   1201  1.5.4.2  thorpej 			ifp->if_flags |= IFF_OACTIVE;
   1202  1.5.4.2  thorpej 			break;
   1203  1.5.4.2  thorpej 		}
   1204  1.5.4.2  thorpej 		IFQ_DEQUEUE(&ifp->if_snd, m);
   1205  1.5.4.2  thorpej 		if (m == NULL) {
   1206  1.5.4.2  thorpej 			pool_put(&sc->sc_pool, buf);
   1207  1.5.4.2  thorpej 			break;
   1208  1.5.4.2  thorpej 		}
   1209  1.5.4.2  thorpej 
   1210  1.5.4.2  thorpej 		m_copydata(m, 0, m->m_pkthdr.len, buf + VNET_ETHER_ALIGN);
   1211  1.5.4.2  thorpej 
   1212  1.5.4.2  thorpej #if NBPFILTER > 0
   1213  1.5.4.2  thorpej 		/*
   1214  1.5.4.2  thorpej 		 * If BPF is listening on this interface, let it see the
   1215  1.5.4.2  thorpej 		 * packet before we commit it to the wire.
   1216  1.5.4.2  thorpej 		 */
   1217  1.5.4.2  thorpej 		if (ifp->if_bpf)
   1218  1.5.4.2  thorpej 		{
   1219  1.5.4.2  thorpej 			DPRINTF(("%s: before bpf\n", __func__));
   1220  1.5.4.2  thorpej 			bpf_mtap(ifp->if_bpf, m, BPF_DIRECTION_OUT);
   1221  1.5.4.2  thorpej 			DPRINTF(("%s: after bpf\n", __func__));
   1222  1.5.4.2  thorpej 		}
   1223  1.5.4.2  thorpej #endif
   1224  1.5.4.2  thorpej 
   1225  1.5.4.2  thorpej 		pmap_extract(pmap_kernel(), (vaddr_t)buf, &pa);
   1226  1.5.4.2  thorpej 		KASSERT((pa & ~PAGE_MASK) == (pa & LDC_MTE_RA_MASK));
   1227  1.5.4.2  thorpej 		while (map->lm_slot[map->lm_next].entry != 0) {
   1228  1.5.4.2  thorpej 			map->lm_next++;
   1229  1.5.4.2  thorpej 			map->lm_next &= (map->lm_nentries - 1);
   1230  1.5.4.2  thorpej 		}
   1231  1.5.4.2  thorpej 		map->lm_slot[map->lm_next].entry = (pa & LDC_MTE_RA_MASK);
   1232  1.5.4.2  thorpej 		map->lm_slot[map->lm_next].entry |= LDC_MTE_CPR;
   1233  1.5.4.2  thorpej 		atomic_inc_32(&map->lm_count);
   1234  1.5.4.2  thorpej 		sc->sc_vd->vd_desc[prod].nbytes = MAX(m->m_pkthdr.len, 60);
   1235  1.5.4.2  thorpej 		sc->sc_vd->vd_desc[prod].ncookies = 1;
   1236  1.5.4.2  thorpej 		sc->sc_vd->vd_desc[prod].cookie[0].addr =
   1237  1.5.4.2  thorpej 		    map->lm_next << PAGE_SHIFT | (pa & PAGE_MASK);
   1238  1.5.4.2  thorpej 		sc->sc_vd->vd_desc[prod].cookie[0].size = 2048;
   1239  1.5.4.2  thorpej 		membar_producer();
   1240  1.5.4.2  thorpej 		sc->sc_vd->vd_desc[prod].hdr.dstate = VIO_DESC_READY;
   1241  1.5.4.2  thorpej 
   1242  1.5.4.2  thorpej 		sc->sc_vsd[prod].vsd_map_idx = map->lm_next;
   1243  1.5.4.2  thorpej 		sc->sc_vsd[prod].vsd_buf = buf;
   1244  1.5.4.2  thorpej 
   1245  1.5.4.2  thorpej 		sc->sc_tx_prod++;
   1246  1.5.4.2  thorpej 		prod = sc->sc_tx_prod & (sc->sc_vd->vd_nentries - 1);
   1247  1.5.4.2  thorpej 
   1248  1.5.4.2  thorpej 		m_freem(m);
   1249  1.5.4.2  thorpej 	}
   1250  1.5.4.2  thorpej 
   1251  1.5.4.2  thorpej 	membar_producer();
   1252  1.5.4.2  thorpej 
   1253  1.5.4.2  thorpej 	if (start != prod && sc->sc_peer_state != VIO_DP_ACTIVE) {
   1254  1.5.4.2  thorpej 		vnet_send_dring_data(sc, start);
   1255  1.5.4.2  thorpej 		ifp->if_timer = 5;
   1256  1.5.4.2  thorpej 	}
   1257  1.5.4.2  thorpej 
   1258  1.5.4.2  thorpej }
   1259  1.5.4.2  thorpej 
   1260  1.5.4.2  thorpej void
   1261  1.5.4.2  thorpej vnet_start_desc(struct ifnet *ifp)
   1262  1.5.4.2  thorpej {
   1263  1.5.4.2  thorpej 	struct vnet_softc *sc = ifp->if_softc;
   1264  1.5.4.2  thorpej 	struct ldc_map *map = sc->sc_lm;
   1265  1.5.4.2  thorpej 	struct vnet_desc_msg dm;
   1266  1.5.4.2  thorpej 	struct mbuf *m;
   1267  1.5.4.2  thorpej 	paddr_t pa;
   1268  1.5.4.2  thorpej 	unsigned char *buf;
   1269  1.5.4.2  thorpej 	u_int prod, count;
   1270  1.5.4.2  thorpej 
   1271  1.5.4.2  thorpej 	for (;;) {
   1272  1.5.4.2  thorpej 		count = sc->sc_tx_prod - sc->sc_tx_cons;
   1273  1.5.4.2  thorpej 		if (count >= (sc->sc_vd->vd_nentries - 1) ||
   1274  1.5.4.2  thorpej 		    map->lm_count >= map->lm_nentries) {
   1275  1.5.4.2  thorpej 			ifp->if_flags |= IFF_OACTIVE;
   1276  1.5.4.2  thorpej 			return;
   1277  1.5.4.2  thorpej 		}
   1278  1.5.4.2  thorpej 
   1279  1.5.4.2  thorpej 		buf = pool_get(&sc->sc_pool, PR_NOWAIT|PR_ZERO);
   1280  1.5.4.2  thorpej 		if (buf == NULL) {
   1281  1.5.4.2  thorpej 			ifp->if_flags |= IFF_OACTIVE;
   1282  1.5.4.2  thorpej 			return;
   1283  1.5.4.2  thorpej 		}
   1284  1.5.4.2  thorpej 
   1285  1.5.4.2  thorpej 		IFQ_DEQUEUE(&ifp->if_snd, m);
   1286  1.5.4.2  thorpej 
   1287  1.5.4.2  thorpej 		if (m == NULL) {
   1288  1.5.4.2  thorpej 			pool_put(&sc->sc_pool, buf);
   1289  1.5.4.2  thorpej 			return;
   1290  1.5.4.2  thorpej 		}
   1291  1.5.4.2  thorpej 
   1292  1.5.4.2  thorpej 		m_copydata(m, 0, m->m_pkthdr.len, buf);
   1293  1.5.4.2  thorpej 
   1294  1.5.4.2  thorpej #if NBPFILTER > 0
   1295  1.5.4.2  thorpej 		/*
   1296  1.5.4.2  thorpej 		 * If BPF is listening on this interface, let it see the
   1297  1.5.4.2  thorpej 		 * packet before we commit it to the wire.
   1298  1.5.4.2  thorpej 		 */
   1299  1.5.4.2  thorpej 		if (ifp->if_bpf)
   1300  1.5.4.2  thorpej 			bpf_mtap(ifp->if_bpf, m, BPF_DIRECTION_OUT);
   1301  1.5.4.2  thorpej #endif
   1302  1.5.4.2  thorpej 
   1303  1.5.4.2  thorpej 		pmap_extract(pmap_kernel(), (vaddr_t)buf, &pa);
   1304  1.5.4.2  thorpej 		KASSERT((pa & ~PAGE_MASK) == (pa & LDC_MTE_RA_MASK));
   1305  1.5.4.2  thorpej 		while (map->lm_slot[map->lm_next].entry != 0) {
   1306  1.5.4.2  thorpej 			map->lm_next++;
   1307  1.5.4.2  thorpej 			map->lm_next &= (map->lm_nentries - 1);
   1308  1.5.4.2  thorpej 		}
   1309  1.5.4.2  thorpej 		map->lm_slot[map->lm_next].entry = (pa & LDC_MTE_RA_MASK);
   1310  1.5.4.2  thorpej 		map->lm_slot[map->lm_next].entry |= LDC_MTE_CPR;
   1311  1.5.4.2  thorpej 		atomic_inc_32(&map->lm_count);
   1312  1.5.4.2  thorpej 
   1313  1.5.4.2  thorpej 		prod = sc->sc_tx_prod & (sc->sc_vd->vd_nentries - 1);
   1314  1.5.4.2  thorpej 		sc->sc_vsd[prod].vsd_map_idx = map->lm_next;
   1315  1.5.4.2  thorpej 		sc->sc_vsd[prod].vsd_buf = buf;
   1316  1.5.4.2  thorpej 
   1317  1.5.4.2  thorpej 		bzero(&dm, sizeof(dm));
   1318  1.5.4.2  thorpej 		dm.tag.type = VIO_TYPE_DATA;
   1319  1.5.4.2  thorpej 		dm.tag.stype = VIO_SUBTYPE_INFO;
   1320  1.5.4.2  thorpej 		dm.tag.stype_env = VIO_DESC_DATA;
   1321  1.5.4.2  thorpej 		dm.tag.sid = sc->sc_local_sid;
   1322  1.5.4.2  thorpej 		dm.seq_no = sc->sc_seq_no++;
   1323  1.5.4.2  thorpej 		dm.desc_handle = sc->sc_tx_prod;
   1324  1.5.4.2  thorpej 		dm.nbytes = MAX(m->m_pkthdr.len, 60);
   1325  1.5.4.2  thorpej 		dm.ncookies = 1;
   1326  1.5.4.2  thorpej 		dm.cookie[0].addr =
   1327  1.5.4.2  thorpej 			map->lm_next << PAGE_SHIFT | (pa & PAGE_MASK);
   1328  1.5.4.2  thorpej 		dm.cookie[0].size = 2048;
   1329  1.5.4.2  thorpej 		vnet_sendmsg(sc, &dm, sizeof(dm));
   1330  1.5.4.2  thorpej 
   1331  1.5.4.2  thorpej 		sc->sc_tx_prod++;
   1332  1.5.4.2  thorpej 		sc->sc_tx_prod &= (sc->sc_vd->vd_nentries - 1);
   1333  1.5.4.2  thorpej 
   1334  1.5.4.2  thorpej 		m_freem(m);
   1335  1.5.4.2  thorpej 	}
   1336  1.5.4.2  thorpej }
   1337  1.5.4.2  thorpej 
   1338  1.5.4.2  thorpej int
   1339  1.5.4.2  thorpej vnet_ioctl(struct ifnet *ifp, u_long cmd, void* data)
   1340  1.5.4.2  thorpej {
   1341  1.5.4.2  thorpej 	struct vnet_softc *sc = ifp->if_softc;
   1342  1.5.4.2  thorpej 	struct ifreq *ifr = (struct ifreq *)data;
   1343  1.5.4.2  thorpej 	int s, error = 0;
   1344  1.5.4.2  thorpej 
   1345  1.5.4.2  thorpej 	s = splnet();
   1346  1.5.4.2  thorpej 
   1347  1.5.4.2  thorpej 	switch (cmd) {
   1348  1.5.4.2  thorpej 
   1349  1.5.4.2  thorpej 		case SIOCSIFADDR:
   1350  1.5.4.2  thorpej 			ifp->if_flags |= IFF_UP;
   1351  1.5.4.2  thorpej 			/* FALLTHROUGH */
   1352  1.5.4.2  thorpej 		case SIOCSIFFLAGS:
   1353  1.5.4.2  thorpej 			if (ifp->if_flags & IFF_UP) {
   1354  1.5.4.2  thorpej 				if ((ifp->if_flags & IFF_RUNNING) == 0)
   1355  1.5.4.2  thorpej 					vnet_init(ifp);
   1356  1.5.4.2  thorpej 			} else {
   1357  1.5.4.2  thorpej 				if (ifp->if_flags & IFF_RUNNING)
   1358  1.5.4.2  thorpej 					vnet_stop(ifp, 0);
   1359  1.5.4.2  thorpej 			}
   1360  1.5.4.2  thorpej 		break;
   1361  1.5.4.2  thorpej 
   1362  1.5.4.2  thorpej 		case SIOCGIFMEDIA:
   1363  1.5.4.2  thorpej 		case SIOCSIFMEDIA:
   1364  1.5.4.2  thorpej 			error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
   1365  1.5.4.2  thorpej 			break;
   1366  1.5.4.2  thorpej 
   1367  1.5.4.2  thorpej 		case SIOCADDMULTI:
   1368  1.5.4.2  thorpej 		case SIOCDELMULTI:
   1369  1.5.4.2  thorpej 			/*
   1370  1.5.4.2  thorpej 			 * XXX Removing all multicast addresses and adding
   1371  1.5.4.2  thorpej 			 * most of them back, is somewhat retarded.
   1372  1.5.4.2  thorpej 			 */
   1373  1.5.4.2  thorpej 			vnet_setmulti(sc, 0);
   1374  1.5.4.2  thorpej 			error = ether_ioctl(ifp, cmd, data);
   1375  1.5.4.2  thorpej 			vnet_setmulti(sc, 1);
   1376  1.5.4.2  thorpej 			if (error == ENETRESET)
   1377  1.5.4.2  thorpej 				error = 0;
   1378  1.5.4.2  thorpej 			break;
   1379  1.5.4.2  thorpej 
   1380  1.5.4.2  thorpej 		default:
   1381  1.5.4.2  thorpej 			error = ether_ioctl(ifp, cmd, data);
   1382  1.5.4.2  thorpej 	}
   1383  1.5.4.2  thorpej 
   1384  1.5.4.2  thorpej 	splx(s);
   1385  1.5.4.2  thorpej 
   1386  1.5.4.2  thorpej 	return (error);
   1387  1.5.4.2  thorpej }
   1388  1.5.4.2  thorpej 
   1389  1.5.4.2  thorpej void
   1390  1.5.4.2  thorpej vnet_watchdog(struct ifnet *ifp)
   1391  1.5.4.2  thorpej {
   1392  1.5.4.2  thorpej 
   1393  1.5.4.2  thorpej 	struct vnet_softc *sc = ifp->if_softc;
   1394  1.5.4.2  thorpej 
   1395  1.5.4.2  thorpej 	printf("%s: watchdog timeout\n", sc->sc_dv.dv_xname);
   1396  1.5.4.2  thorpej }
   1397  1.5.4.2  thorpej 
   1398  1.5.4.2  thorpej int
   1399  1.5.4.2  thorpej vnet_media_change(struct ifnet *ifp)
   1400  1.5.4.2  thorpej {
   1401  1.5.4.2  thorpej 	return (0);
   1402  1.5.4.2  thorpej }
   1403  1.5.4.2  thorpej 
   1404  1.5.4.2  thorpej void
   1405  1.5.4.2  thorpej vnet_media_status(struct ifnet *ifp, struct ifmediareq *imr)
   1406  1.5.4.2  thorpej {
   1407  1.5.4.2  thorpej 	imr->ifm_active = IFM_ETHER | IFM_AUTO;
   1408  1.5.4.2  thorpej 	imr->ifm_status = IFM_AVALID;
   1409  1.5.4.2  thorpej 	if (ifp->if_link_state == LINK_STATE_UP &&
   1410  1.5.4.2  thorpej 	    ifp->if_flags & IFF_UP)
   1411  1.5.4.2  thorpej 		imr->ifm_status |= IFM_ACTIVE;
   1412  1.5.4.2  thorpej }
   1413  1.5.4.2  thorpej 
   1414  1.5.4.2  thorpej void
   1415  1.5.4.2  thorpej vnet_link_state(struct vnet_softc *sc)
   1416  1.5.4.2  thorpej {
   1417  1.5.4.2  thorpej 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1418  1.5.4.2  thorpej 	int link_state = LINK_STATE_DOWN;
   1419  1.5.4.2  thorpej 
   1420  1.5.4.2  thorpej 	KERNEL_LOCK(1, curlwp);
   1421  1.5.4.2  thorpej 	if (ISSET(sc->sc_vio_state, VIO_RCV_RDX) &&
   1422  1.5.4.2  thorpej 	    ISSET(sc->sc_vio_state, VIO_ACK_RDX))
   1423  1.5.4.2  thorpej 		link_state = LINK_STATE_UP;
   1424  1.5.4.2  thorpej 	if (ifp->if_link_state != link_state) {
   1425  1.5.4.2  thorpej 		if_link_state_change(ifp, link_state);
   1426  1.5.4.2  thorpej 	}
   1427  1.5.4.2  thorpej 	KERNEL_UNLOCK_ONE(curlwp);
   1428  1.5.4.2  thorpej }
   1429  1.5.4.2  thorpej 
   1430  1.5.4.2  thorpej void
   1431  1.5.4.2  thorpej vnet_setmulti(struct vnet_softc *sc, int set)
   1432  1.5.4.2  thorpej {
   1433  1.5.4.2  thorpej 	struct ethercom *ec = &sc->sc_ethercom;
   1434  1.5.4.2  thorpej 	struct ether_multi *enm;
   1435  1.5.4.2  thorpej 	struct ether_multistep step;
   1436  1.5.4.2  thorpej 	struct vnet_mcast_info mi;
   1437  1.5.4.2  thorpej 	int count = 0;
   1438  1.5.4.2  thorpej 
   1439  1.5.4.2  thorpej 	if (!ISSET(sc->sc_vio_state, VIO_RCV_RDX) ||
   1440  1.5.4.2  thorpej 	    !ISSET(sc->sc_vio_state, VIO_ACK_RDX))
   1441  1.5.4.2  thorpej 		return;
   1442  1.5.4.2  thorpej 
   1443  1.5.4.2  thorpej 	bzero(&mi, sizeof(mi));
   1444  1.5.4.2  thorpej 	mi.tag.type = VIO_TYPE_CTRL;
   1445  1.5.4.2  thorpej 	mi.tag.stype = VIO_SUBTYPE_INFO;
   1446  1.5.4.2  thorpej 	mi.tag.stype_env = VNET_MCAST_INFO;
   1447  1.5.4.2  thorpej 	mi.tag.sid = sc->sc_local_sid;
   1448  1.5.4.2  thorpej 	mi.set = set ? 1 : 0;
   1449  1.5.4.2  thorpej 	KERNEL_LOCK(1, curlwp);
   1450  1.5.4.2  thorpej 	ETHER_FIRST_MULTI(step, ec, enm);
   1451  1.5.4.2  thorpej 	while (enm != NULL) {
   1452  1.5.4.2  thorpej 		/* XXX What about multicast ranges? */
   1453  1.5.4.2  thorpej 		bcopy(enm->enm_addrlo, mi.mcast_addr[count], ETHER_ADDR_LEN);
   1454  1.5.4.2  thorpej 		ETHER_NEXT_MULTI(step, enm);
   1455  1.5.4.2  thorpej 
   1456  1.5.4.2  thorpej 		count++;
   1457  1.5.4.2  thorpej 		if (count < VNET_NUM_MCAST)
   1458  1.5.4.2  thorpej 			continue;
   1459  1.5.4.2  thorpej 
   1460  1.5.4.2  thorpej 		mi.count = VNET_NUM_MCAST;
   1461  1.5.4.2  thorpej 		vnet_sendmsg(sc, &mi, sizeof(mi));
   1462  1.5.4.2  thorpej 		count = 0;
   1463  1.5.4.2  thorpej 	}
   1464  1.5.4.2  thorpej 
   1465  1.5.4.2  thorpej 	if (count > 0) {
   1466  1.5.4.2  thorpej 		mi.count = count;
   1467  1.5.4.2  thorpej 		vnet_sendmsg(sc, &mi, sizeof(mi));
   1468  1.5.4.2  thorpej 	}
   1469  1.5.4.2  thorpej 	KERNEL_UNLOCK_ONE(curlwp);
   1470  1.5.4.2  thorpej }
   1471  1.5.4.2  thorpej 
   1472  1.5.4.2  thorpej 
   1473  1.5.4.2  thorpej int
   1474  1.5.4.2  thorpej vnet_init(struct ifnet *ifp)
   1475  1.5.4.2  thorpej {
   1476  1.5.4.2  thorpej 	struct vnet_softc *sc = ifp->if_softc;
   1477  1.5.4.2  thorpej 	struct ldc_conn *lc = &sc->sc_lc;
   1478  1.5.4.2  thorpej 	int err;
   1479  1.5.4.2  thorpej 	vaddr_t va;
   1480  1.5.4.2  thorpej 	paddr_t pa;
   1481  1.5.4.2  thorpej 	sc->sc_lm = ldc_map_alloc(2048);
   1482  1.5.4.2  thorpej 	if (sc->sc_lm == NULL)
   1483  1.5.4.2  thorpej 		return ENOMEM;
   1484  1.5.4.2  thorpej 
   1485  1.5.4.2  thorpej 	va = (vaddr_t)sc->sc_lm->lm_slot;
   1486  1.5.4.2  thorpej 	pa = 0;
   1487  1.5.4.2  thorpej 	if (pmap_extract(pmap_kernel(), va, &pa) == FALSE)
   1488  1.5.4.2  thorpej 		panic("pmap_extract failed %lx\n", va);
   1489  1.5.4.2  thorpej 	err = hv_ldc_set_map_table(lc->lc_id, pa, 2048);
   1490  1.5.4.2  thorpej 	if (err != H_EOK) {
   1491  1.5.4.2  thorpej 		printf("hv_ldc_set_map_table %d\n", err);
   1492  1.5.4.2  thorpej 		return EINVAL;
   1493  1.5.4.2  thorpej 	}
   1494  1.5.4.2  thorpej 
   1495  1.5.4.2  thorpej 	sc->sc_vd = vnet_dring_alloc(sc->sc_dmatag, VNET_NUM_SOFT_DESC);
   1496  1.5.4.2  thorpej 	if (sc->sc_vd == NULL)
   1497  1.5.4.2  thorpej 		return ENOMEM;
   1498  1.5.4.2  thorpej 	sc->sc_vsd = malloc(VNET_NUM_SOFT_DESC * sizeof(*sc->sc_vsd), M_DEVBUF,
   1499  1.5.4.2  thorpej 	    M_NOWAIT|M_ZERO);
   1500  1.5.4.2  thorpej 	if (sc->sc_vsd == NULL)
   1501  1.5.4.2  thorpej 		return ENOMEM;
   1502  1.5.4.2  thorpej 
   1503  1.5.4.2  thorpej 	va = (vaddr_t)sc->sc_vd->vd_desc;
   1504  1.5.4.2  thorpej 	pa = 0;
   1505  1.5.4.2  thorpej 	if (pmap_extract(pmap_kernel(), va, &pa) == FALSE)
   1506  1.5.4.2  thorpej 		panic("pmap_extract failed %lx\n", va);
   1507  1.5.4.2  thorpej 	sc->sc_lm->lm_slot[0].entry = pa;
   1508  1.5.4.2  thorpej 	sc->sc_lm->lm_slot[0].entry &= LDC_MTE_RA_MASK;
   1509  1.5.4.2  thorpej 	sc->sc_lm->lm_slot[0].entry |= LDC_MTE_CPR | LDC_MTE_CPW;
   1510  1.5.4.2  thorpej 	sc->sc_lm->lm_next = 1;
   1511  1.5.4.2  thorpej 	sc->sc_lm->lm_count = 1;
   1512  1.5.4.2  thorpej 
   1513  1.5.4.2  thorpej 	va = lc->lc_txq->lq_va;
   1514  1.5.4.2  thorpej 	pa = 0;
   1515  1.5.4.2  thorpej 	if (pmap_extract(pmap_kernel(), va, &pa) == FALSE)
   1516  1.5.4.2  thorpej 		panic("pmap_extract failed %lx\n", va);
   1517  1.5.4.2  thorpej 	err = hv_ldc_tx_qconf(lc->lc_id, pa, lc->lc_txq->lq_nentries);
   1518  1.5.4.2  thorpej 	if (err != H_EOK)
   1519  1.5.4.2  thorpej 		printf("hv_ldc_tx_qconf %d\n", err);
   1520  1.5.4.2  thorpej 
   1521  1.5.4.2  thorpej 	va = (vaddr_t)lc->lc_rxq->lq_va;
   1522  1.5.4.2  thorpej 	pa = 0;
   1523  1.5.4.2  thorpej 	if (pmap_extract(pmap_kernel(), va, &pa) == FALSE)
   1524  1.5.4.2  thorpej 	  panic("pmap_extract failed %lx\n", va);
   1525  1.5.4.2  thorpej 
   1526  1.5.4.2  thorpej 	err = hv_ldc_rx_qconf(lc->lc_id, pa, lc->lc_rxq->lq_nentries);
   1527  1.5.4.2  thorpej 	if (err != H_EOK)
   1528  1.5.4.2  thorpej 		printf("hv_ldc_rx_qconf %d\n", err);
   1529  1.5.4.2  thorpej 
   1530  1.5.4.2  thorpej 	cbus_intr_setenabled(sc->sc_bustag, sc->sc_tx_ino, INTR_ENABLED);
   1531  1.5.4.2  thorpej 	cbus_intr_setenabled(sc->sc_bustag, sc->sc_rx_ino, INTR_ENABLED);
   1532  1.5.4.2  thorpej 
   1533  1.5.4.2  thorpej 	ldc_send_vers(lc);
   1534  1.5.4.2  thorpej 
   1535  1.5.4.2  thorpej 	ifp->if_flags |= IFF_RUNNING;
   1536  1.5.4.2  thorpej 
   1537  1.5.4.2  thorpej 	return 0;
   1538  1.5.4.2  thorpej }
   1539  1.5.4.2  thorpej 
   1540  1.5.4.2  thorpej void
   1541  1.5.4.2  thorpej vnet_stop(struct ifnet *ifp, int disable)
   1542  1.5.4.2  thorpej 
   1543  1.5.4.2  thorpej {
   1544  1.5.4.2  thorpej 	struct vnet_softc *sc = ifp->if_softc;
   1545  1.5.4.2  thorpej 	struct ldc_conn *lc = &sc->sc_lc;
   1546  1.5.4.2  thorpej 
   1547  1.5.4.2  thorpej 	ifp->if_flags &= ~IFF_RUNNING;
   1548  1.5.4.2  thorpej 	ifp->if_flags &= ~IFF_OACTIVE;
   1549  1.5.4.2  thorpej 	ifp->if_timer = 0;
   1550  1.5.4.2  thorpej 
   1551  1.5.4.2  thorpej 	cbus_intr_setenabled(sc->sc_bustag, sc->sc_tx_ino, INTR_DISABLED);
   1552  1.5.4.2  thorpej 	cbus_intr_setenabled(sc->sc_bustag, sc->sc_rx_ino, INTR_DISABLED);
   1553  1.5.4.2  thorpej 
   1554  1.5.4.2  thorpej #if 0
   1555  1.5.4.2  thorpej openbsd XXX
   1556  1.5.4.2  thorpej 	intr_barrier(sc->sc_tx_ih);
   1557  1.5.4.2  thorpej 	intr_barrier(sc->sc_rx_ih);
   1558  1.5.4.2  thorpej #else
   1559  1.5.4.2  thorpej 	printf("vnet_stop() intr_barrier() not available\n");
   1560  1.5.4.2  thorpej #endif
   1561  1.5.4.2  thorpej 
   1562  1.5.4.2  thorpej 	hv_ldc_tx_qconf(lc->lc_id, 0, 0);
   1563  1.5.4.2  thorpej 	hv_ldc_rx_qconf(lc->lc_id, 0, 0);
   1564  1.5.4.2  thorpej 	lc->lc_tx_seqid = 0;
   1565  1.5.4.2  thorpej 	lc->lc_state = 0;
   1566  1.5.4.2  thorpej 	lc->lc_tx_state = lc->lc_rx_state = LDC_CHANNEL_DOWN;
   1567  1.5.4.2  thorpej 	vnet_ldc_reset(lc);
   1568  1.5.4.2  thorpej 
   1569  1.5.4.2  thorpej 	free(sc->sc_vsd, M_DEVBUF);
   1570  1.5.4.2  thorpej 
   1571  1.5.4.2  thorpej 	vnet_dring_free(sc->sc_dmatag, sc->sc_vd);
   1572  1.5.4.2  thorpej 
   1573  1.5.4.2  thorpej 	hv_ldc_set_map_table(lc->lc_id, 0, 0);
   1574  1.5.4.2  thorpej 	ldc_map_free(sc->sc_lm);
   1575  1.5.4.2  thorpej }
   1576  1.5.4.2  thorpej 
   1577  1.5.4.2  thorpej struct vnet_dring *
   1578  1.5.4.2  thorpej vnet_dring_alloc(bus_dma_tag_t t, int nentries)
   1579  1.5.4.2  thorpej {
   1580  1.5.4.2  thorpej 	struct vnet_dring *vd;
   1581  1.5.4.2  thorpej 	bus_size_t size;
   1582  1.5.4.2  thorpej 	vaddr_t va;
   1583  1.5.4.2  thorpej 	int i;
   1584  1.5.4.2  thorpej 
   1585  1.5.4.2  thorpej 	vd = kmem_zalloc(sizeof(struct vnet_dring), KM_SLEEP);
   1586  1.5.4.2  thorpej 	if (vd == NULL)
   1587  1.5.4.2  thorpej 		return NULL;
   1588  1.5.4.2  thorpej 
   1589  1.5.4.2  thorpej 	size = roundup(nentries * sizeof(struct vnet_desc), PAGE_SIZE);
   1590  1.5.4.2  thorpej 
   1591  1.5.4.2  thorpej 	va = (vaddr_t)kmem_zalloc(size, KM_SLEEP);
   1592  1.5.4.2  thorpej 	vd->vd_desc = (struct vnet_desc *)va;
   1593  1.5.4.2  thorpej 	vd->vd_nentries = nentries;
   1594  1.5.4.2  thorpej 	bzero(vd->vd_desc, nentries * sizeof(struct vnet_desc));
   1595  1.5.4.2  thorpej 	for (i = 0; i < vd->vd_nentries; i++)
   1596  1.5.4.2  thorpej 		vd->vd_desc[i].hdr.dstate = VIO_DESC_FREE;
   1597  1.5.4.2  thorpej 	return (vd);
   1598  1.5.4.2  thorpej 
   1599  1.5.4.2  thorpej 	return (NULL);
   1600  1.5.4.2  thorpej }
   1601  1.5.4.2  thorpej 
   1602  1.5.4.2  thorpej void
   1603  1.5.4.2  thorpej vnet_dring_free(bus_dma_tag_t t, struct vnet_dring *vd)
   1604  1.5.4.2  thorpej {
   1605  1.5.4.2  thorpej 
   1606  1.5.4.2  thorpej 	bus_size_t size;
   1607  1.5.4.2  thorpej 
   1608  1.5.4.2  thorpej 	size = vd->vd_nentries * sizeof(struct vnet_desc);
   1609  1.5.4.2  thorpej 	size = roundup(size, PAGE_SIZE);
   1610  1.5.4.2  thorpej 
   1611  1.5.4.2  thorpej 	kmem_free(vd->vd_desc, size);
   1612  1.5.4.2  thorpej 	kmem_free(vd, size);
   1613  1.5.4.2  thorpej }
   1614  1.5.4.2  thorpej 
   1615