Home | History | Annotate | Line # | Download | only in pci
if_vioif.c revision 1.6
      1 /*	$NetBSD: if_vioif.c,v 1.6 2014/07/22 01:55:54 ozaki-r Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2010 Minoura Makoto.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 #include <sys/cdefs.h>
     29 __KERNEL_RCSID(0, "$NetBSD: if_vioif.c,v 1.6 2014/07/22 01:55:54 ozaki-r Exp $");
     30 
     31 #include <sys/param.h>
     32 #include <sys/systm.h>
     33 #include <sys/kernel.h>
     34 #include <sys/bus.h>
     35 #include <sys/condvar.h>
     36 #include <sys/device.h>
     37 #include <sys/intr.h>
     38 #include <sys/kmem.h>
     39 #include <sys/mbuf.h>
     40 #include <sys/mutex.h>
     41 #include <sys/sockio.h>
     42 
     43 #include <dev/pci/pcidevs.h>
     44 #include <dev/pci/pcireg.h>
     45 #include <dev/pci/pcivar.h>
     46 #include <dev/pci/virtioreg.h>
     47 #include <dev/pci/virtiovar.h>
     48 
     49 #include <net/if.h>
     50 #include <net/if_media.h>
     51 #include <net/if_ether.h>
     52 
     53 #include <net/bpf.h>
     54 
     55 
     56 /*
     57  * if_vioifreg.h:
     58  */
     59 /* Configuration registers */
     60 #define VIRTIO_NET_CONFIG_MAC		0 /* 8bit x 6byte */
     61 #define VIRTIO_NET_CONFIG_STATUS	6 /* 16bit */
     62 
     63 /* Feature bits */
     64 #define VIRTIO_NET_F_CSUM	(1<<0)
     65 #define VIRTIO_NET_F_GUEST_CSUM	(1<<1)
     66 #define VIRTIO_NET_F_MAC	(1<<5)
     67 #define VIRTIO_NET_F_GSO	(1<<6)
     68 #define VIRTIO_NET_F_GUEST_TSO4	(1<<7)
     69 #define VIRTIO_NET_F_GUEST_TSO6	(1<<8)
     70 #define VIRTIO_NET_F_GUEST_ECN	(1<<9)
     71 #define VIRTIO_NET_F_GUEST_UFO	(1<<10)
     72 #define VIRTIO_NET_F_HOST_TSO4	(1<<11)
     73 #define VIRTIO_NET_F_HOST_TSO6	(1<<12)
     74 #define VIRTIO_NET_F_HOST_ECN	(1<<13)
     75 #define VIRTIO_NET_F_HOST_UFO	(1<<14)
     76 #define VIRTIO_NET_F_MRG_RXBUF	(1<<15)
     77 #define VIRTIO_NET_F_STATUS	(1<<16)
     78 #define VIRTIO_NET_F_CTRL_VQ	(1<<17)
     79 #define VIRTIO_NET_F_CTRL_RX	(1<<18)
     80 #define VIRTIO_NET_F_CTRL_VLAN	(1<<19)
     81 
     82 /* Status */
     83 #define VIRTIO_NET_S_LINK_UP	1
     84 
     85 /* Packet header structure */
     86 struct virtio_net_hdr {
     87 	uint8_t		flags;
     88 	uint8_t		gso_type;
     89 	uint16_t	hdr_len;
     90 	uint16_t	gso_size;
     91 	uint16_t	csum_start;
     92 	uint16_t	csum_offset;
     93 #if 0
     94 	uint16_t	num_buffers; /* if VIRTIO_NET_F_MRG_RXBUF enabled */
     95 #endif
     96 } __packed;
     97 
     98 #define VIRTIO_NET_HDR_F_NEEDS_CSUM	1 /* flags */
     99 #define VIRTIO_NET_HDR_GSO_NONE		0 /* gso_type */
    100 #define VIRTIO_NET_HDR_GSO_TCPV4	1 /* gso_type */
    101 #define VIRTIO_NET_HDR_GSO_UDP		3 /* gso_type */
    102 #define VIRTIO_NET_HDR_GSO_TCPV6	4 /* gso_type */
    103 #define VIRTIO_NET_HDR_GSO_ECN		0x80 /* gso_type, |'ed */
    104 
    105 #define VIRTIO_NET_MAX_GSO_LEN		(65536+ETHER_HDR_LEN)
    106 
    107 /* Control virtqueue */
    108 struct virtio_net_ctrl_cmd {
    109 	uint8_t	class;
    110 	uint8_t	command;
    111 } __packed;
    112 #define VIRTIO_NET_CTRL_RX		0
    113 # define VIRTIO_NET_CTRL_RX_PROMISC	0
    114 # define VIRTIO_NET_CTRL_RX_ALLMULTI	1
    115 
    116 #define VIRTIO_NET_CTRL_MAC		1
    117 # define VIRTIO_NET_CTRL_MAC_TABLE_SET	0
    118 
    119 #define VIRTIO_NET_CTRL_VLAN		2
    120 # define VIRTIO_NET_CTRL_VLAN_ADD	0
    121 # define VIRTIO_NET_CTRL_VLAN_DEL	1
    122 
    123 struct virtio_net_ctrl_status {
    124 	uint8_t	ack;
    125 } __packed;
    126 #define VIRTIO_NET_OK			0
    127 #define VIRTIO_NET_ERR			1
    128 
    129 struct virtio_net_ctrl_rx {
    130 	uint8_t	onoff;
    131 } __packed;
    132 
    133 struct virtio_net_ctrl_mac_tbl {
    134 	uint32_t nentries;
    135 	uint8_t macs[][ETHER_ADDR_LEN];
    136 } __packed;
    137 
    138 struct virtio_net_ctrl_vlan {
    139 	uint16_t id;
    140 } __packed;
    141 
    142 
    143 /*
    144  * if_vioifvar.h:
    145  */
    146 struct vioif_softc {
    147 	device_t		sc_dev;
    148 
    149 	struct virtio_softc	*sc_virtio;
    150 	struct virtqueue	sc_vq[3];
    151 
    152 	uint8_t			sc_mac[ETHER_ADDR_LEN];
    153 	struct ethercom		sc_ethercom;
    154 	short			sc_ifflags;
    155 
    156 	/* bus_dmamem */
    157 	bus_dma_segment_t	sc_hdr_segs[1];
    158 	struct virtio_net_hdr	*sc_hdrs;
    159 #define sc_rx_hdrs	sc_hdrs
    160 	struct virtio_net_hdr	*sc_tx_hdrs;
    161 	struct virtio_net_ctrl_cmd *sc_ctrl_cmd;
    162 	struct virtio_net_ctrl_status *sc_ctrl_status;
    163 	struct virtio_net_ctrl_rx *sc_ctrl_rx;
    164 	struct virtio_net_ctrl_mac_tbl *sc_ctrl_mac_tbl_uc;
    165 	struct virtio_net_ctrl_mac_tbl *sc_ctrl_mac_tbl_mc;
    166 
    167 	/* kmem */
    168 	bus_dmamap_t		*sc_arrays;
    169 #define sc_rxhdr_dmamaps sc_arrays
    170 	bus_dmamap_t		*sc_txhdr_dmamaps;
    171 	bus_dmamap_t		*sc_rx_dmamaps;
    172 	bus_dmamap_t		*sc_tx_dmamaps;
    173 	struct mbuf		**sc_rx_mbufs;
    174 	struct mbuf		**sc_tx_mbufs;
    175 
    176 	bus_dmamap_t		sc_ctrl_cmd_dmamap;
    177 	bus_dmamap_t		sc_ctrl_status_dmamap;
    178 	bus_dmamap_t		sc_ctrl_rx_dmamap;
    179 	bus_dmamap_t		sc_ctrl_tbl_uc_dmamap;
    180 	bus_dmamap_t		sc_ctrl_tbl_mc_dmamap;
    181 
    182 	void			*sc_rx_softint;
    183 
    184 	enum {
    185 		FREE, INUSE, DONE
    186 	}			sc_ctrl_inuse;
    187 	kcondvar_t		sc_ctrl_wait;
    188 	kmutex_t		sc_ctrl_wait_lock;
    189 };
    190 #define VIRTIO_NET_TX_MAXNSEGS		(16) /* XXX */
    191 #define VIRTIO_NET_CTRL_MAC_MAXENTRIES	(64) /* XXX */
    192 
    193 /* cfattach interface functions */
    194 static int	vioif_match(device_t, cfdata_t, void *);
    195 static void	vioif_attach(device_t, device_t, void *);
    196 static void	vioif_deferred_init(device_t);
    197 
    198 /* ifnet interface functions */
    199 static int	vioif_init(struct ifnet *);
    200 static void	vioif_stop(struct ifnet *, int);
    201 static void	vioif_start(struct ifnet *);
    202 static int	vioif_ioctl(struct ifnet *, u_long, void *);
    203 static void	vioif_watchdog(struct ifnet *);
    204 
    205 /* rx */
    206 static int	vioif_add_rx_mbuf(struct vioif_softc *, int);
    207 static void	vioif_free_rx_mbuf(struct vioif_softc *, int);
    208 static void	vioif_populate_rx_mbufs(struct vioif_softc *);
    209 static int	vioif_rx_deq(struct vioif_softc *);
    210 static int	vioif_rx_vq_done(struct virtqueue *);
    211 static void	vioif_rx_softint(void *);
    212 static void	vioif_rx_drain(struct vioif_softc *);
    213 
    214 /* tx */
    215 static int	vioif_tx_vq_done(struct virtqueue *);
    216 static void	vioif_tx_drain(struct vioif_softc *);
    217 
    218 /* other control */
    219 static int	vioif_updown(struct vioif_softc *, bool);
    220 static int	vioif_ctrl_rx(struct vioif_softc *, int, bool);
    221 static int	vioif_set_promisc(struct vioif_softc *, bool);
    222 static int	vioif_set_allmulti(struct vioif_softc *, bool);
    223 static int	vioif_set_rx_filter(struct vioif_softc *);
    224 static int	vioif_rx_filter(struct vioif_softc *);
    225 static int	vioif_ctrl_vq_done(struct virtqueue *);
    226 
    227 CFATTACH_DECL_NEW(vioif, sizeof(struct vioif_softc),
    228 		  vioif_match, vioif_attach, NULL, NULL);
    229 
    230 static int
    231 vioif_match(device_t parent, cfdata_t match, void *aux)
    232 {
    233 	struct virtio_softc *va = aux;
    234 
    235 	if (va->sc_childdevid == PCI_PRODUCT_VIRTIO_NETWORK)
    236 		return 1;
    237 
    238 	return 0;
    239 }
    240 
    241 /* allocate memory */
    242 /*
    243  * dma memory is used for:
    244  *   sc_rx_hdrs[slot]:	 metadata array for recieved frames (READ)
    245  *   sc_tx_hdrs[slot]:	 metadata array for frames to be sent (WRITE)
    246  *   sc_ctrl_cmd:	 command to be sent via ctrl vq (WRITE)
    247  *   sc_ctrl_status:	 return value for a command via ctrl vq (READ)
    248  *   sc_ctrl_rx:	 parameter for a VIRTIO_NET_CTRL_RX class command
    249  *			 (WRITE)
    250  *   sc_ctrl_mac_tbl_uc: unicast MAC address filter for a VIRTIO_NET_CTRL_MAC
    251  *			 class command (WRITE)
    252  *   sc_ctrl_mac_tbl_mc: multicast MAC address filter for a VIRTIO_NET_CTRL_MAC
    253  *			 class command (WRITE)
    254  * sc_ctrl_* structures are allocated only one each; they are protected by
    255  * sc_ctrl_inuse variable and sc_ctrl_wait condvar.
    256  */
    257 /*
    258  * dynamically allocated memory is used for:
    259  *   sc_rxhdr_dmamaps[slot]:	bus_dmamap_t array for sc_rx_hdrs[slot]
    260  *   sc_txhdr_dmamaps[slot]:	bus_dmamap_t array for sc_tx_hdrs[slot]
    261  *   sc_rx_dmamaps[slot]:	bus_dmamap_t array for recieved payload
    262  *   sc_tx_dmamaps[slot]:	bus_dmamap_t array for sent payload
    263  *   sc_rx_mbufs[slot]:		mbuf pointer array for recieved frames
    264  *   sc_tx_mbufs[slot]:		mbuf pointer array for sent frames
    265  */
    266 static int
    267 vioif_alloc_mems(struct vioif_softc *sc)
    268 {
    269 	struct virtio_softc *vsc = sc->sc_virtio;
    270 	int allocsize, allocsize2, r, rsegs, i;
    271 	void *vaddr;
    272 	intptr_t p;
    273 	int rxqsize, txqsize;
    274 
    275 	rxqsize = vsc->sc_vqs[0].vq_num;
    276 	txqsize = vsc->sc_vqs[1].vq_num;
    277 
    278 	allocsize = sizeof(struct virtio_net_hdr) * rxqsize;
    279 	allocsize += sizeof(struct virtio_net_hdr) * txqsize;
    280 	if (vsc->sc_nvqs == 3) {
    281 		allocsize += sizeof(struct virtio_net_ctrl_cmd) * 1;
    282 		allocsize += sizeof(struct virtio_net_ctrl_status) * 1;
    283 		allocsize += sizeof(struct virtio_net_ctrl_rx) * 1;
    284 		allocsize += sizeof(struct virtio_net_ctrl_mac_tbl)
    285 			+ sizeof(struct virtio_net_ctrl_mac_tbl)
    286 			+ ETHER_ADDR_LEN * VIRTIO_NET_CTRL_MAC_MAXENTRIES;
    287 	}
    288 	r = bus_dmamem_alloc(vsc->sc_dmat, allocsize, 0, 0,
    289 			     &sc->sc_hdr_segs[0], 1, &rsegs, BUS_DMA_NOWAIT);
    290 	if (r != 0) {
    291 		aprint_error_dev(sc->sc_dev,
    292 				 "DMA memory allocation failed, size %d, "
    293 				 "error code %d\n", allocsize, r);
    294 		goto err_none;
    295 	}
    296 	r = bus_dmamem_map(vsc->sc_dmat,
    297 			   &sc->sc_hdr_segs[0], 1, allocsize,
    298 			   &vaddr, BUS_DMA_NOWAIT);
    299 	if (r != 0) {
    300 		aprint_error_dev(sc->sc_dev,
    301 				 "DMA memory map failed, "
    302 				 "error code %d\n", r);
    303 		goto err_dmamem_alloc;
    304 	}
    305 	sc->sc_hdrs = vaddr;
    306 	memset(vaddr, 0, allocsize);
    307 	p = (intptr_t) vaddr;
    308 	p += sizeof(struct virtio_net_hdr) * rxqsize;
    309 #define P(name,size)	do { sc->sc_ ##name = (void*) p;	\
    310 			     p += size; } while (0)
    311 	P(tx_hdrs, sizeof(struct virtio_net_hdr) * txqsize);
    312 	if (vsc->sc_nvqs == 3) {
    313 		P(ctrl_cmd, sizeof(struct virtio_net_ctrl_cmd));
    314 		P(ctrl_status, sizeof(struct virtio_net_ctrl_status));
    315 		P(ctrl_rx, sizeof(struct virtio_net_ctrl_rx));
    316 		P(ctrl_mac_tbl_uc, sizeof(struct virtio_net_ctrl_mac_tbl));
    317 		P(ctrl_mac_tbl_mc,
    318 		  (sizeof(struct virtio_net_ctrl_mac_tbl)
    319 		   + ETHER_ADDR_LEN * VIRTIO_NET_CTRL_MAC_MAXENTRIES));
    320 	}
    321 #undef P
    322 
    323 	allocsize2 = sizeof(bus_dmamap_t) * (rxqsize + txqsize);
    324 	allocsize2 += sizeof(bus_dmamap_t) * (rxqsize + txqsize);
    325 	allocsize2 += sizeof(struct mbuf*) * (rxqsize + txqsize);
    326 	sc->sc_arrays = kmem_zalloc(allocsize2, KM_SLEEP);
    327 	if (sc->sc_arrays == NULL)
    328 		goto err_dmamem_map;
    329 	sc->sc_txhdr_dmamaps = sc->sc_arrays + rxqsize;
    330 	sc->sc_rx_dmamaps = sc->sc_txhdr_dmamaps + txqsize;
    331 	sc->sc_tx_dmamaps = sc->sc_rx_dmamaps + rxqsize;
    332 	sc->sc_rx_mbufs = (void*) (sc->sc_tx_dmamaps + txqsize);
    333 	sc->sc_tx_mbufs = sc->sc_rx_mbufs + rxqsize;
    334 
    335 #define C(map, buf, size, nsegs, rw, usage)				\
    336 	do {								\
    337 		r = bus_dmamap_create(vsc->sc_dmat, size, nsegs, size, 0, \
    338 				      BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW,	\
    339 				      &sc->sc_ ##map);			\
    340 		if (r != 0) {						\
    341 			aprint_error_dev(sc->sc_dev,			\
    342 					 usage " dmamap creation failed, " \
    343 					 "error code %d\n", r);		\
    344 					 goto err_reqs;			\
    345 		}							\
    346 	} while (0)
    347 #define C_L1(map, buf, size, nsegs, rw, usage)				\
    348 	C(map, buf, size, nsegs, rw, usage);				\
    349 	do {								\
    350 		r = bus_dmamap_load(vsc->sc_dmat, sc->sc_ ##map,	\
    351 				    &sc->sc_ ##buf, size, NULL,		\
    352 				    BUS_DMA_ ##rw | BUS_DMA_NOWAIT);	\
    353 		if (r != 0) {						\
    354 			aprint_error_dev(sc->sc_dev,			\
    355 					 usage " dmamap load failed, "	\
    356 					 "error code %d\n", r);		\
    357 			goto err_reqs;					\
    358 		}							\
    359 	} while (0)
    360 #define C_L2(map, buf, size, nsegs, rw, usage)				\
    361 	C(map, buf, size, nsegs, rw, usage);				\
    362 	do {								\
    363 		r = bus_dmamap_load(vsc->sc_dmat, sc->sc_ ##map,	\
    364 				    sc->sc_ ##buf, size, NULL,		\
    365 				    BUS_DMA_ ##rw | BUS_DMA_NOWAIT);	\
    366 		if (r != 0) {						\
    367 			aprint_error_dev(sc->sc_dev,			\
    368 					 usage " dmamap load failed, "	\
    369 					 "error code %d\n", r);		\
    370 			goto err_reqs;					\
    371 		}							\
    372 	} while (0)
    373 	for (i = 0; i < rxqsize; i++) {
    374 		C_L1(rxhdr_dmamaps[i], rx_hdrs[i],
    375 		    sizeof(struct virtio_net_hdr), 1,
    376 		    READ, "rx header");
    377 		C(rx_dmamaps[i], NULL, MCLBYTES, 1, 0, "rx payload");
    378 	}
    379 
    380 	for (i = 0; i < txqsize; i++) {
    381 		C_L1(txhdr_dmamaps[i], rx_hdrs[i],
    382 		    sizeof(struct virtio_net_hdr), 1,
    383 		    WRITE, "tx header");
    384 		C(tx_dmamaps[i], NULL, ETHER_MAX_LEN, 256 /* XXX */, 0,
    385 		  "tx payload");
    386 	}
    387 
    388 	if (vsc->sc_nvqs == 3) {
    389 		/* control vq class & command */
    390 		C_L2(ctrl_cmd_dmamap, ctrl_cmd,
    391 		    sizeof(struct virtio_net_ctrl_cmd), 1, WRITE,
    392 		    "control command");
    393 
    394 		/* control vq status */
    395 		C_L2(ctrl_status_dmamap, ctrl_status,
    396 		    sizeof(struct virtio_net_ctrl_status), 1, READ,
    397 		    "control status");
    398 
    399 		/* control vq rx mode command parameter */
    400 		C_L2(ctrl_rx_dmamap, ctrl_rx,
    401 		    sizeof(struct virtio_net_ctrl_rx), 1, WRITE,
    402 		    "rx mode control command");
    403 
    404 		/* control vq MAC filter table for unicast */
    405 		/* do not load now since its length is variable */
    406 		C(ctrl_tbl_uc_dmamap, NULL,
    407 		  sizeof(struct virtio_net_ctrl_mac_tbl) + 0, 1, WRITE,
    408 		  "unicast MAC address filter command");
    409 
    410 		/* control vq MAC filter table for multicast */
    411 		C(ctrl_tbl_mc_dmamap, NULL,
    412 		  (sizeof(struct virtio_net_ctrl_mac_tbl)
    413 		   + ETHER_ADDR_LEN * VIRTIO_NET_CTRL_MAC_MAXENTRIES),
    414 		  1, WRITE, "multicast MAC address filter command");
    415 	}
    416 #undef C_L2
    417 #undef C_L1
    418 #undef C
    419 
    420 	return 0;
    421 
    422 err_reqs:
    423 #define D(map)								\
    424 	do {								\
    425 		if (sc->sc_ ##map) {					\
    426 			bus_dmamap_destroy(vsc->sc_dmat, sc->sc_ ##map); \
    427 			sc->sc_ ##map = NULL;				\
    428 		}							\
    429 	} while (0)
    430 	D(ctrl_tbl_mc_dmamap);
    431 	D(ctrl_tbl_uc_dmamap);
    432 	D(ctrl_rx_dmamap);
    433 	D(ctrl_status_dmamap);
    434 	D(ctrl_cmd_dmamap);
    435 	for (i = 0; i < txqsize; i++) {
    436 		D(tx_dmamaps[i]);
    437 		D(txhdr_dmamaps[i]);
    438 	}
    439 	for (i = 0; i < rxqsize; i++) {
    440 		D(rx_dmamaps[i]);
    441 		D(rxhdr_dmamaps[i]);
    442 	}
    443 #undef D
    444 	if (sc->sc_arrays) {
    445 		kmem_free(sc->sc_arrays, allocsize2);
    446 		sc->sc_arrays = 0;
    447 	}
    448 err_dmamem_map:
    449 	bus_dmamem_unmap(vsc->sc_dmat, sc->sc_hdrs, allocsize);
    450 err_dmamem_alloc:
    451 	bus_dmamem_free(vsc->sc_dmat, &sc->sc_hdr_segs[0], 1);
    452 err_none:
    453 	return -1;
    454 }
    455 
    456 static void
    457 vioif_attach(device_t parent, device_t self, void *aux)
    458 {
    459 	struct vioif_softc *sc = device_private(self);
    460 	struct virtio_softc *vsc = device_private(parent);
    461 	uint32_t features;
    462 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    463 
    464 	if (vsc->sc_child != NULL) {
    465 		aprint_normal(": child already attached for %s; "
    466 			      "something wrong...\n",
    467 			      device_xname(parent));
    468 		return;
    469 	}
    470 
    471 	sc->sc_dev = self;
    472 	sc->sc_virtio = vsc;
    473 
    474 	vsc->sc_child = self;
    475 	vsc->sc_ipl = IPL_NET;
    476 	vsc->sc_vqs = &sc->sc_vq[0];
    477 	vsc->sc_config_change = 0;
    478 	vsc->sc_intrhand = virtio_vq_intr;
    479 	vsc->sc_flags = 0;
    480 
    481 	features = virtio_negotiate_features(vsc,
    482 					     (VIRTIO_NET_F_MAC |
    483 					      VIRTIO_NET_F_STATUS |
    484 					      VIRTIO_NET_F_CTRL_VQ |
    485 					      VIRTIO_NET_F_CTRL_RX |
    486 					      VIRTIO_F_NOTIFY_ON_EMPTY));
    487 	if (features & VIRTIO_NET_F_MAC) {
    488 		sc->sc_mac[0] = virtio_read_device_config_1(vsc,
    489 						    VIRTIO_NET_CONFIG_MAC+0);
    490 		sc->sc_mac[1] = virtio_read_device_config_1(vsc,
    491 						    VIRTIO_NET_CONFIG_MAC+1);
    492 		sc->sc_mac[2] = virtio_read_device_config_1(vsc,
    493 						    VIRTIO_NET_CONFIG_MAC+2);
    494 		sc->sc_mac[3] = virtio_read_device_config_1(vsc,
    495 						    VIRTIO_NET_CONFIG_MAC+3);
    496 		sc->sc_mac[4] = virtio_read_device_config_1(vsc,
    497 						    VIRTIO_NET_CONFIG_MAC+4);
    498 		sc->sc_mac[5] = virtio_read_device_config_1(vsc,
    499 						    VIRTIO_NET_CONFIG_MAC+5);
    500 	} else {
    501 		/* code stolen from sys/net/if_tap.c */
    502 		struct timeval tv;
    503 		uint32_t ui;
    504 		getmicrouptime(&tv);
    505 		ui = (tv.tv_sec ^ tv.tv_usec) & 0xffffff;
    506 		memcpy(sc->sc_mac+3, (uint8_t *)&ui, 3);
    507 		virtio_write_device_config_1(vsc,
    508 					     VIRTIO_NET_CONFIG_MAC+0,
    509 					     sc->sc_mac[0]);
    510 		virtio_write_device_config_1(vsc,
    511 					     VIRTIO_NET_CONFIG_MAC+1,
    512 					     sc->sc_mac[1]);
    513 		virtio_write_device_config_1(vsc,
    514 					     VIRTIO_NET_CONFIG_MAC+2,
    515 					     sc->sc_mac[2]);
    516 		virtio_write_device_config_1(vsc,
    517 					     VIRTIO_NET_CONFIG_MAC+3,
    518 					     sc->sc_mac[3]);
    519 		virtio_write_device_config_1(vsc,
    520 					     VIRTIO_NET_CONFIG_MAC+4,
    521 					     sc->sc_mac[4]);
    522 		virtio_write_device_config_1(vsc,
    523 					     VIRTIO_NET_CONFIG_MAC+5,
    524 					     sc->sc_mac[5]);
    525 	}
    526 	aprint_normal(": Ethernet address %s\n", ether_sprintf(sc->sc_mac));
    527 	aprint_naive("\n");
    528 
    529 	if (virtio_alloc_vq(vsc, &sc->sc_vq[0], 0,
    530 			    MCLBYTES+sizeof(struct virtio_net_hdr), 2,
    531 			    "rx") != 0) {
    532 		goto err;
    533 	}
    534 	vsc->sc_nvqs = 1;
    535 	sc->sc_vq[0].vq_done = vioif_rx_vq_done;
    536 	if (virtio_alloc_vq(vsc, &sc->sc_vq[1], 1,
    537 			    (sizeof(struct virtio_net_hdr)
    538 			     + (ETHER_MAX_LEN - ETHER_HDR_LEN)),
    539 			    VIRTIO_NET_TX_MAXNSEGS + 1,
    540 			    "tx") != 0) {
    541 		goto err;
    542 	}
    543 	vsc->sc_nvqs = 2;
    544 	sc->sc_vq[1].vq_done = vioif_tx_vq_done;
    545 	virtio_start_vq_intr(vsc, &sc->sc_vq[0]);
    546 	virtio_stop_vq_intr(vsc, &sc->sc_vq[1]); /* not urgent; do it later */
    547 	if ((features & VIRTIO_NET_F_CTRL_VQ)
    548 	    && (features & VIRTIO_NET_F_CTRL_RX)) {
    549 		if (virtio_alloc_vq(vsc, &sc->sc_vq[2], 2,
    550 				    NBPG, 1, "control") == 0) {
    551 			sc->sc_vq[2].vq_done = vioif_ctrl_vq_done;
    552 			cv_init(&sc->sc_ctrl_wait, "ctrl_vq");
    553 			mutex_init(&sc->sc_ctrl_wait_lock,
    554 				   MUTEX_DEFAULT, IPL_NET);
    555 			sc->sc_ctrl_inuse = FREE;
    556 			virtio_start_vq_intr(vsc, &sc->sc_vq[2]);
    557 			vsc->sc_nvqs = 3;
    558 		}
    559 	}
    560 
    561 	sc->sc_rx_softint = softint_establish(SOFTINT_NET,
    562 					      vioif_rx_softint, sc);
    563 	if (sc->sc_rx_softint == NULL) {
    564 		aprint_error_dev(self, "cannot establish softint\n");
    565 		goto err;
    566 	}
    567 
    568 	if (vioif_alloc_mems(sc) < 0)
    569 		goto err;
    570 	if (vsc->sc_nvqs == 3)
    571 		config_interrupts(self, vioif_deferred_init);
    572 
    573 	strlcpy(ifp->if_xname, device_xname(self), IFNAMSIZ);
    574 	ifp->if_softc = sc;
    575 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    576 	ifp->if_start = vioif_start;
    577 	ifp->if_ioctl = vioif_ioctl;
    578 	ifp->if_init = vioif_init;
    579 	ifp->if_stop = vioif_stop;
    580 	ifp->if_capabilities = 0;
    581 	ifp->if_watchdog = vioif_watchdog;
    582 
    583 	if_attach(ifp);
    584 	ether_ifattach(ifp, sc->sc_mac);
    585 
    586 	return;
    587 
    588 err:
    589 	if (vsc->sc_nvqs == 3) {
    590 		virtio_free_vq(vsc, &sc->sc_vq[2]);
    591 		cv_destroy(&sc->sc_ctrl_wait);
    592 		mutex_destroy(&sc->sc_ctrl_wait_lock);
    593 		vsc->sc_nvqs = 2;
    594 	}
    595 	if (vsc->sc_nvqs == 2) {
    596 		virtio_free_vq(vsc, &sc->sc_vq[1]);
    597 		vsc->sc_nvqs = 1;
    598 	}
    599 	if (vsc->sc_nvqs == 1) {
    600 		virtio_free_vq(vsc, &sc->sc_vq[0]);
    601 		vsc->sc_nvqs = 0;
    602 	}
    603 	vsc->sc_child = (void*)1;
    604 	return;
    605 }
    606 
    607 /* we need interrupts to make promiscuous mode off */
    608 static void
    609 vioif_deferred_init(device_t self)
    610 {
    611 	struct vioif_softc *sc = device_private(self);
    612 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    613 	int r;
    614 
    615 	r =  vioif_set_promisc(sc, false);
    616 	if (r != 0)
    617 		aprint_error_dev(self, "resetting promisc mode failed, "
    618 				 "errror code %d\n", r);
    619 	else
    620 		ifp->if_flags &= ~IFF_PROMISC;
    621 }
    622 
    623 /*
    624  * Interface functions for ifnet
    625  */
    626 static int
    627 vioif_init(struct ifnet *ifp)
    628 {
    629 	struct vioif_softc *sc = ifp->if_softc;
    630 
    631 	vioif_stop(ifp, 0);
    632 	vioif_populate_rx_mbufs(sc);
    633 	vioif_updown(sc, true);
    634 	ifp->if_flags |= IFF_RUNNING;
    635 	ifp->if_flags &= ~IFF_OACTIVE;
    636 	vioif_rx_filter(sc);
    637 
    638 	return 0;
    639 }
    640 
    641 static void
    642 vioif_stop(struct ifnet *ifp, int disable)
    643 {
    644 	struct vioif_softc *sc = ifp->if_softc;
    645 	struct virtio_softc *vsc = sc->sc_virtio;
    646 
    647 	/* only way to stop I/O and DMA is resetting... */
    648 	virtio_reset(vsc);
    649 	vioif_rx_deq(sc);
    650 	vioif_tx_drain(sc);
    651 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
    652 
    653 	if (disable)
    654 		vioif_rx_drain(sc);
    655 
    656 	virtio_reinit_start(vsc);
    657 	virtio_negotiate_features(vsc, vsc->sc_features);
    658 	virtio_start_vq_intr(vsc, &sc->sc_vq[0]);
    659 	virtio_stop_vq_intr(vsc, &sc->sc_vq[1]);
    660 	if (vsc->sc_nvqs >= 3)
    661 		virtio_start_vq_intr(vsc, &sc->sc_vq[2]);
    662 	virtio_reinit_end(vsc);
    663 	vioif_updown(sc, false);
    664 }
    665 
    666 static void
    667 vioif_start(struct ifnet *ifp)
    668 {
    669 	struct vioif_softc *sc = ifp->if_softc;
    670 	struct virtio_softc *vsc = sc->sc_virtio;
    671 	struct virtqueue *vq = &sc->sc_vq[1]; /* tx vq */
    672 	struct mbuf *m;
    673 	int queued = 0, retry = 0;
    674 
    675 	if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
    676 		return;
    677 
    678 	for (;;) {
    679 		int slot, r;
    680 
    681 		IFQ_POLL(&ifp->if_snd, m);
    682 		if (m == NULL)
    683 			break;
    684 
    685 		r = virtio_enqueue_prep(vsc, vq, &slot);
    686 		if (r == EAGAIN) {
    687 			ifp->if_flags |= IFF_OACTIVE;
    688 			vioif_tx_vq_done(vq);
    689 			if (retry++ == 0)
    690 				continue;
    691 			else
    692 				break;
    693 		}
    694 		if (r != 0)
    695 			panic("enqueue_prep for a tx buffer");
    696 		r = bus_dmamap_load_mbuf(vsc->sc_dmat,
    697 					 sc->sc_tx_dmamaps[slot],
    698 					 m, BUS_DMA_WRITE|BUS_DMA_NOWAIT);
    699 		if (r != 0) {
    700 			virtio_enqueue_abort(vsc, vq, slot);
    701 			printf("%s: tx dmamap load failed, error code %d\n",
    702 			       device_xname(sc->sc_dev), r);
    703 			break;
    704 		}
    705 		r = virtio_enqueue_reserve(vsc, vq, slot,
    706 					sc->sc_tx_dmamaps[slot]->dm_nsegs + 1);
    707 		if (r != 0) {
    708 			bus_dmamap_unload(vsc->sc_dmat,
    709 					  sc->sc_tx_dmamaps[slot]);
    710 			ifp->if_flags |= IFF_OACTIVE;
    711 			vioif_tx_vq_done(vq);
    712 			if (retry++ == 0)
    713 				continue;
    714 			else
    715 				break;
    716 		}
    717 		IFQ_DEQUEUE(&ifp->if_snd, m);
    718 		sc->sc_tx_mbufs[slot] = m;
    719 
    720 		memset(&sc->sc_tx_hdrs[slot], 0, sizeof(struct virtio_net_hdr));
    721 		bus_dmamap_sync(vsc->sc_dmat, sc->sc_tx_dmamaps[slot],
    722 				0, sc->sc_tx_dmamaps[slot]->dm_mapsize,
    723 				BUS_DMASYNC_PREWRITE);
    724 		bus_dmamap_sync(vsc->sc_dmat, sc->sc_txhdr_dmamaps[slot],
    725 				0, sc->sc_txhdr_dmamaps[slot]->dm_mapsize,
    726 				BUS_DMASYNC_PREWRITE);
    727 		virtio_enqueue(vsc, vq, slot, sc->sc_txhdr_dmamaps[slot], true);
    728 		virtio_enqueue(vsc, vq, slot, sc->sc_tx_dmamaps[slot], true);
    729 		virtio_enqueue_commit(vsc, vq, slot, false);
    730 		queued++;
    731 		bpf_mtap(ifp, m);
    732 	}
    733 
    734 	if (queued > 0) {
    735 		virtio_enqueue_commit(vsc, vq, -1, true);
    736 		ifp->if_timer = 5;
    737 	}
    738 }
    739 
    740 static int
    741 vioif_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    742 {
    743 	int s, r;
    744 
    745 	s = splnet();
    746 
    747 	r = ether_ioctl(ifp, cmd, data);
    748 	if ((r == 0 && cmd == SIOCSIFFLAGS) ||
    749 	    (r == ENETRESET && (cmd == SIOCADDMULTI || cmd == SIOCDELMULTI))) {
    750 		if (ifp->if_flags & IFF_RUNNING)
    751 			r = vioif_rx_filter(ifp->if_softc);
    752 		else
    753 			r = 0;
    754 	}
    755 
    756 	splx(s);
    757 
    758 	return r;
    759 }
    760 
    761 void
    762 vioif_watchdog(struct ifnet *ifp)
    763 {
    764 	struct vioif_softc *sc = ifp->if_softc;
    765 
    766 	if (ifp->if_flags & IFF_RUNNING)
    767 		vioif_tx_vq_done(&sc->sc_vq[1]);
    768 }
    769 
    770 
    771 /*
    772  * Recieve implementation
    773  */
    774 /* allocate and initialize a mbuf for recieve */
    775 static int
    776 vioif_add_rx_mbuf(struct vioif_softc *sc, int i)
    777 {
    778 	struct mbuf *m;
    779 	int r;
    780 
    781 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    782 	if (m == NULL)
    783 		return ENOBUFS;
    784 	MCLGET(m, M_DONTWAIT);
    785 	if ((m->m_flags & M_EXT) == 0) {
    786 		m_freem(m);
    787 		return ENOBUFS;
    788 	}
    789 	sc->sc_rx_mbufs[i] = m;
    790 	m->m_len = m->m_pkthdr.len = m->m_ext.ext_size;
    791 	r = bus_dmamap_load_mbuf(sc->sc_virtio->sc_dmat,
    792 				 sc->sc_rx_dmamaps[i],
    793 				 m, BUS_DMA_READ|BUS_DMA_NOWAIT);
    794 	if (r) {
    795 		m_freem(m);
    796 		sc->sc_rx_mbufs[i] = 0;
    797 		return r;
    798 	}
    799 
    800 	return 0;
    801 }
    802 
    803 /* free a mbuf for recieve */
    804 static void
    805 vioif_free_rx_mbuf(struct vioif_softc *sc, int i)
    806 {
    807 	bus_dmamap_unload(sc->sc_virtio->sc_dmat, sc->sc_rx_dmamaps[i]);
    808 	m_freem(sc->sc_rx_mbufs[i]);
    809 	sc->sc_rx_mbufs[i] = NULL;
    810 }
    811 
    812 /* add mbufs for all the empty recieve slots */
    813 static void
    814 vioif_populate_rx_mbufs(struct vioif_softc *sc)
    815 {
    816 	struct virtio_softc *vsc = sc->sc_virtio;
    817 	int i, r, ndone = 0;
    818 	struct virtqueue *vq = &sc->sc_vq[0]; /* rx vq */
    819 
    820 	for (i = 0; i < vq->vq_num; i++) {
    821 		int slot;
    822 		r = virtio_enqueue_prep(vsc, vq, &slot);
    823 		if (r == EAGAIN)
    824 			break;
    825 		if (r != 0)
    826 			panic("enqueue_prep for rx buffers");
    827 		if (sc->sc_rx_mbufs[slot] == NULL) {
    828 			r = vioif_add_rx_mbuf(sc, slot);
    829 			if (r != 0) {
    830 				printf("%s: rx mbuf allocation failed, "
    831 				       "error code %d\n",
    832 				       device_xname(sc->sc_dev), r);
    833 				break;
    834 			}
    835 		}
    836 		r = virtio_enqueue_reserve(vsc, vq, slot,
    837 					sc->sc_rx_dmamaps[slot]->dm_nsegs + 1);
    838 		if (r != 0) {
    839 			vioif_free_rx_mbuf(sc, slot);
    840 			break;
    841 		}
    842 		bus_dmamap_sync(vsc->sc_dmat, sc->sc_rxhdr_dmamaps[slot],
    843 			0, sizeof(struct virtio_net_hdr), BUS_DMASYNC_PREREAD);
    844 		bus_dmamap_sync(vsc->sc_dmat, sc->sc_rx_dmamaps[slot],
    845 			0, MCLBYTES, BUS_DMASYNC_PREREAD);
    846 		virtio_enqueue(vsc, vq, slot, sc->sc_rxhdr_dmamaps[slot], false);
    847 		virtio_enqueue(vsc, vq, slot, sc->sc_rx_dmamaps[slot], false);
    848 		virtio_enqueue_commit(vsc, vq, slot, false);
    849 		ndone++;
    850 	}
    851 	if (ndone > 0)
    852 		virtio_enqueue_commit(vsc, vq, -1, true);
    853 }
    854 
    855 /* dequeue recieved packets */
    856 static int
    857 vioif_rx_deq(struct vioif_softc *sc)
    858 {
    859 	struct virtio_softc *vsc = sc->sc_virtio;
    860 	struct virtqueue *vq = &sc->sc_vq[0];
    861 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    862 	struct mbuf *m;
    863 	int r = 0;
    864 	int slot, len;
    865 
    866 	while (virtio_dequeue(vsc, vq, &slot, &len) == 0) {
    867 		len -= sizeof(struct virtio_net_hdr);
    868 		r = 1;
    869 		bus_dmamap_sync(vsc->sc_dmat, sc->sc_rxhdr_dmamaps[slot],
    870 				0, sizeof(struct virtio_net_hdr),
    871 				BUS_DMASYNC_POSTREAD);
    872 		bus_dmamap_sync(vsc->sc_dmat, sc->sc_rx_dmamaps[slot],
    873 				0, MCLBYTES,
    874 				BUS_DMASYNC_POSTREAD);
    875 		m = sc->sc_rx_mbufs[slot];
    876 		KASSERT(m != NULL);
    877 		bus_dmamap_unload(vsc->sc_dmat, sc->sc_rx_dmamaps[slot]);
    878 		sc->sc_rx_mbufs[slot] = 0;
    879 		virtio_dequeue_commit(vsc, vq, slot);
    880 		m->m_pkthdr.rcvif = ifp;
    881 		m->m_len = m->m_pkthdr.len = len;
    882 		ifp->if_ipackets++;
    883 		bpf_mtap(ifp, m);
    884 		(*ifp->if_input)(ifp, m);
    885 	}
    886 
    887 	return r;
    888 }
    889 
    890 /* rx interrupt; call _dequeue above and schedule a softint */
    891 static int
    892 vioif_rx_vq_done(struct virtqueue *vq)
    893 {
    894 	struct virtio_softc *vsc = vq->vq_owner;
    895 	struct vioif_softc *sc = device_private(vsc->sc_child);
    896 	int r;
    897 
    898 	r = vioif_rx_deq(sc);
    899 	if (r)
    900 		softint_schedule(sc->sc_rx_softint);
    901 
    902 	return r;
    903 }
    904 
    905 /* softint: enqueue recieve requests for new incoming packets */
    906 static void
    907 vioif_rx_softint(void *arg)
    908 {
    909 	struct vioif_softc *sc = arg;
    910 
    911 	vioif_populate_rx_mbufs(sc);
    912 }
    913 
    914 /* free all the mbufs; called from if_stop(disable) */
    915 static void
    916 vioif_rx_drain(struct vioif_softc *sc)
    917 {
    918 	struct virtqueue *vq = &sc->sc_vq[0];
    919 	int i;
    920 
    921 	for (i = 0; i < vq->vq_num; i++) {
    922 		if (sc->sc_rx_mbufs[i] == NULL)
    923 			continue;
    924 		vioif_free_rx_mbuf(sc, i);
    925 	}
    926 }
    927 
    928 
    929 /*
    930  * Transmition implementation
    931  */
    932 /* actual transmission is done in if_start */
    933 /* tx interrupt; dequeue and free mbufs */
    934 /*
    935  * tx interrupt is actually disabled; this should be called upon
    936  * tx vq full and watchdog
    937  */
    938 static int
    939 vioif_tx_vq_done(struct virtqueue *vq)
    940 {
    941 	struct virtio_softc *vsc = vq->vq_owner;
    942 	struct vioif_softc *sc = device_private(vsc->sc_child);
    943 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    944 	struct mbuf *m;
    945 	int r = 0;
    946 	int slot, len;
    947 
    948 	while (virtio_dequeue(vsc, vq, &slot, &len) == 0) {
    949 		r++;
    950 		bus_dmamap_sync(vsc->sc_dmat, sc->sc_txhdr_dmamaps[slot],
    951 				0, sizeof(struct virtio_net_hdr),
    952 				BUS_DMASYNC_POSTWRITE);
    953 		bus_dmamap_sync(vsc->sc_dmat, sc->sc_tx_dmamaps[slot],
    954 				0, sc->sc_tx_dmamaps[slot]->dm_mapsize,
    955 				BUS_DMASYNC_POSTWRITE);
    956 		m = sc->sc_tx_mbufs[slot];
    957 		bus_dmamap_unload(vsc->sc_dmat, sc->sc_tx_dmamaps[slot]);
    958 		sc->sc_tx_mbufs[slot] = 0;
    959 		virtio_dequeue_commit(vsc, vq, slot);
    960 		ifp->if_opackets++;
    961 		m_freem(m);
    962 	}
    963 
    964 	if (r)
    965 		ifp->if_flags &= ~IFF_OACTIVE;
    966 	return r;
    967 }
    968 
    969 /* free all the mbufs already put on vq; called from if_stop(disable) */
    970 static void
    971 vioif_tx_drain(struct vioif_softc *sc)
    972 {
    973 	struct virtio_softc *vsc = sc->sc_virtio;
    974 	struct virtqueue *vq = &sc->sc_vq[1];
    975 	int i;
    976 
    977 	for (i = 0; i < vq->vq_num; i++) {
    978 		if (sc->sc_tx_mbufs[i] == NULL)
    979 			continue;
    980 		bus_dmamap_unload(vsc->sc_dmat, sc->sc_tx_dmamaps[i]);
    981 		m_freem(sc->sc_tx_mbufs[i]);
    982 		sc->sc_tx_mbufs[i] = NULL;
    983 	}
    984 }
    985 
    986 /*
    987  * Control vq
    988  */
    989 /* issue a VIRTIO_NET_CTRL_RX class command and wait for completion */
    990 static int
    991 vioif_ctrl_rx(struct vioif_softc *sc, int cmd, bool onoff)
    992 {
    993 	struct virtio_softc *vsc = sc->sc_virtio;
    994 	struct virtqueue *vq = &sc->sc_vq[2];
    995 	int r, slot;
    996 
    997 	if (vsc->sc_nvqs < 3)
    998 		return ENOTSUP;
    999 
   1000 	mutex_enter(&sc->sc_ctrl_wait_lock);
   1001 	while (sc->sc_ctrl_inuse != FREE)
   1002 		cv_wait(&sc->sc_ctrl_wait, &sc->sc_ctrl_wait_lock);
   1003 	sc->sc_ctrl_inuse = INUSE;
   1004 	mutex_exit(&sc->sc_ctrl_wait_lock);
   1005 
   1006 	sc->sc_ctrl_cmd->class = VIRTIO_NET_CTRL_RX;
   1007 	sc->sc_ctrl_cmd->command = cmd;
   1008 	sc->sc_ctrl_rx->onoff = onoff;
   1009 
   1010 	bus_dmamap_sync(vsc->sc_dmat, sc->sc_ctrl_cmd_dmamap,
   1011 			0, sizeof(struct virtio_net_ctrl_cmd),
   1012 			BUS_DMASYNC_PREWRITE);
   1013 	bus_dmamap_sync(vsc->sc_dmat, sc->sc_ctrl_rx_dmamap,
   1014 			0, sizeof(struct virtio_net_ctrl_rx),
   1015 			BUS_DMASYNC_PREWRITE);
   1016 	bus_dmamap_sync(vsc->sc_dmat, sc->sc_ctrl_status_dmamap,
   1017 			0, sizeof(struct virtio_net_ctrl_status),
   1018 			BUS_DMASYNC_PREREAD);
   1019 
   1020 	r = virtio_enqueue_prep(vsc, vq, &slot);
   1021 	if (r != 0)
   1022 		panic("%s: control vq busy!?", device_xname(sc->sc_dev));
   1023 	r = virtio_enqueue_reserve(vsc, vq, slot, 3);
   1024 	if (r != 0)
   1025 		panic("%s: control vq busy!?", device_xname(sc->sc_dev));
   1026 	virtio_enqueue(vsc, vq, slot, sc->sc_ctrl_cmd_dmamap, true);
   1027 	virtio_enqueue(vsc, vq, slot, sc->sc_ctrl_rx_dmamap, true);
   1028 	virtio_enqueue(vsc, vq, slot, sc->sc_ctrl_status_dmamap, false);
   1029 	virtio_enqueue_commit(vsc, vq, slot, true);
   1030 
   1031 	/* wait for done */
   1032 	mutex_enter(&sc->sc_ctrl_wait_lock);
   1033 	while (sc->sc_ctrl_inuse != DONE)
   1034 		cv_wait(&sc->sc_ctrl_wait, &sc->sc_ctrl_wait_lock);
   1035 	mutex_exit(&sc->sc_ctrl_wait_lock);
   1036 	/* already dequeueued */
   1037 
   1038 	bus_dmamap_sync(vsc->sc_dmat, sc->sc_ctrl_cmd_dmamap, 0,
   1039 			sizeof(struct virtio_net_ctrl_cmd),
   1040 			BUS_DMASYNC_POSTWRITE);
   1041 	bus_dmamap_sync(vsc->sc_dmat, sc->sc_ctrl_rx_dmamap, 0,
   1042 			sizeof(struct virtio_net_ctrl_rx),
   1043 			BUS_DMASYNC_POSTWRITE);
   1044 	bus_dmamap_sync(vsc->sc_dmat, sc->sc_ctrl_status_dmamap, 0,
   1045 			sizeof(struct virtio_net_ctrl_status),
   1046 			BUS_DMASYNC_POSTREAD);
   1047 
   1048 	if (sc->sc_ctrl_status->ack == VIRTIO_NET_OK)
   1049 		r = 0;
   1050 	else {
   1051 		printf("%s: failed setting rx mode\n",
   1052 		       device_xname(sc->sc_dev));
   1053 		r = EIO;
   1054 	}
   1055 
   1056 	mutex_enter(&sc->sc_ctrl_wait_lock);
   1057 	sc->sc_ctrl_inuse = FREE;
   1058 	cv_signal(&sc->sc_ctrl_wait);
   1059 	mutex_exit(&sc->sc_ctrl_wait_lock);
   1060 
   1061 	return r;
   1062 }
   1063 
   1064 static int
   1065 vioif_set_promisc(struct vioif_softc *sc, bool onoff)
   1066 {
   1067 	int r;
   1068 
   1069 	r = vioif_ctrl_rx(sc, VIRTIO_NET_CTRL_RX_PROMISC, onoff);
   1070 
   1071 	return r;
   1072 }
   1073 
   1074 static int
   1075 vioif_set_allmulti(struct vioif_softc *sc, bool onoff)
   1076 {
   1077 	int r;
   1078 
   1079 	r = vioif_ctrl_rx(sc, VIRTIO_NET_CTRL_RX_ALLMULTI, onoff);
   1080 
   1081 	return r;
   1082 }
   1083 
   1084 /* issue VIRTIO_NET_CTRL_MAC_TABLE_SET command and wait for completion */
   1085 static int
   1086 vioif_set_rx_filter(struct vioif_softc *sc)
   1087 {
   1088 	/* filter already set in sc_ctrl_mac_tbl */
   1089 	struct virtio_softc *vsc = sc->sc_virtio;
   1090 	struct virtqueue *vq = &sc->sc_vq[2];
   1091 	int r, slot;
   1092 
   1093 	if (vsc->sc_nvqs < 3)
   1094 		return ENOTSUP;
   1095 
   1096 	mutex_enter(&sc->sc_ctrl_wait_lock);
   1097 	while (sc->sc_ctrl_inuse != FREE)
   1098 		cv_wait(&sc->sc_ctrl_wait, &sc->sc_ctrl_wait_lock);
   1099 	sc->sc_ctrl_inuse = INUSE;
   1100 	mutex_exit(&sc->sc_ctrl_wait_lock);
   1101 
   1102 	sc->sc_ctrl_cmd->class = VIRTIO_NET_CTRL_MAC;
   1103 	sc->sc_ctrl_cmd->command = VIRTIO_NET_CTRL_MAC_TABLE_SET;
   1104 
   1105 	r = bus_dmamap_load(vsc->sc_dmat, sc->sc_ctrl_tbl_uc_dmamap,
   1106 			    sc->sc_ctrl_mac_tbl_uc,
   1107 			    (sizeof(struct virtio_net_ctrl_mac_tbl)
   1108 			  + ETHER_ADDR_LEN * sc->sc_ctrl_mac_tbl_uc->nentries),
   1109 			    NULL, BUS_DMA_WRITE|BUS_DMA_NOWAIT);
   1110 	if (r) {
   1111 		printf("%s: control command dmamap load failed, "
   1112 		       "error code %d\n", device_xname(sc->sc_dev), r);
   1113 		goto out;
   1114 	}
   1115 	r = bus_dmamap_load(vsc->sc_dmat, sc->sc_ctrl_tbl_mc_dmamap,
   1116 			    sc->sc_ctrl_mac_tbl_mc,
   1117 			    (sizeof(struct virtio_net_ctrl_mac_tbl)
   1118 			  + ETHER_ADDR_LEN * sc->sc_ctrl_mac_tbl_mc->nentries),
   1119 			    NULL, BUS_DMA_WRITE|BUS_DMA_NOWAIT);
   1120 	if (r) {
   1121 		printf("%s: control command dmamap load failed, "
   1122 		       "error code %d\n", device_xname(sc->sc_dev), r);
   1123 		bus_dmamap_unload(vsc->sc_dmat, sc->sc_ctrl_tbl_uc_dmamap);
   1124 		goto out;
   1125 	}
   1126 
   1127 	bus_dmamap_sync(vsc->sc_dmat, sc->sc_ctrl_cmd_dmamap,
   1128 			0, sizeof(struct virtio_net_ctrl_cmd),
   1129 			BUS_DMASYNC_PREWRITE);
   1130 	bus_dmamap_sync(vsc->sc_dmat, sc->sc_ctrl_tbl_uc_dmamap, 0,
   1131 			(sizeof(struct virtio_net_ctrl_mac_tbl)
   1132 			 + ETHER_ADDR_LEN * sc->sc_ctrl_mac_tbl_uc->nentries),
   1133 			BUS_DMASYNC_PREWRITE);
   1134 	bus_dmamap_sync(vsc->sc_dmat, sc->sc_ctrl_tbl_mc_dmamap, 0,
   1135 			(sizeof(struct virtio_net_ctrl_mac_tbl)
   1136 			 + ETHER_ADDR_LEN * sc->sc_ctrl_mac_tbl_mc->nentries),
   1137 			BUS_DMASYNC_PREWRITE);
   1138 	bus_dmamap_sync(vsc->sc_dmat, sc->sc_ctrl_status_dmamap,
   1139 			0, sizeof(struct virtio_net_ctrl_status),
   1140 			BUS_DMASYNC_PREREAD);
   1141 
   1142 	r = virtio_enqueue_prep(vsc, vq, &slot);
   1143 	if (r != 0)
   1144 		panic("%s: control vq busy!?", device_xname(sc->sc_dev));
   1145 	r = virtio_enqueue_reserve(vsc, vq, slot, 4);
   1146 	if (r != 0)
   1147 		panic("%s: control vq busy!?", device_xname(sc->sc_dev));
   1148 	virtio_enqueue(vsc, vq, slot, sc->sc_ctrl_cmd_dmamap, true);
   1149 	virtio_enqueue(vsc, vq, slot, sc->sc_ctrl_tbl_uc_dmamap, true);
   1150 	virtio_enqueue(vsc, vq, slot, sc->sc_ctrl_tbl_mc_dmamap, true);
   1151 	virtio_enqueue(vsc, vq, slot, sc->sc_ctrl_status_dmamap, false);
   1152 	virtio_enqueue_commit(vsc, vq, slot, true);
   1153 
   1154 	/* wait for done */
   1155 	mutex_enter(&sc->sc_ctrl_wait_lock);
   1156 	while (sc->sc_ctrl_inuse != DONE)
   1157 		cv_wait(&sc->sc_ctrl_wait, &sc->sc_ctrl_wait_lock);
   1158 	mutex_exit(&sc->sc_ctrl_wait_lock);
   1159 	/* already dequeueued */
   1160 
   1161 	bus_dmamap_sync(vsc->sc_dmat, sc->sc_ctrl_cmd_dmamap, 0,
   1162 			sizeof(struct virtio_net_ctrl_cmd),
   1163 			BUS_DMASYNC_POSTWRITE);
   1164 	bus_dmamap_sync(vsc->sc_dmat, sc->sc_ctrl_tbl_uc_dmamap, 0,
   1165 			(sizeof(struct virtio_net_ctrl_mac_tbl)
   1166 			 + ETHER_ADDR_LEN * sc->sc_ctrl_mac_tbl_uc->nentries),
   1167 			BUS_DMASYNC_POSTWRITE);
   1168 	bus_dmamap_sync(vsc->sc_dmat, sc->sc_ctrl_tbl_mc_dmamap, 0,
   1169 			(sizeof(struct virtio_net_ctrl_mac_tbl)
   1170 			 + ETHER_ADDR_LEN * sc->sc_ctrl_mac_tbl_mc->nentries),
   1171 			BUS_DMASYNC_POSTWRITE);
   1172 	bus_dmamap_sync(vsc->sc_dmat, sc->sc_ctrl_status_dmamap, 0,
   1173 			sizeof(struct virtio_net_ctrl_status),
   1174 			BUS_DMASYNC_POSTREAD);
   1175 	bus_dmamap_unload(vsc->sc_dmat, sc->sc_ctrl_tbl_uc_dmamap);
   1176 	bus_dmamap_unload(vsc->sc_dmat, sc->sc_ctrl_tbl_mc_dmamap);
   1177 
   1178 	if (sc->sc_ctrl_status->ack == VIRTIO_NET_OK)
   1179 		r = 0;
   1180 	else {
   1181 		printf("%s: failed setting rx filter\n",
   1182 		       device_xname(sc->sc_dev));
   1183 		r = EIO;
   1184 	}
   1185 
   1186 out:
   1187 	mutex_enter(&sc->sc_ctrl_wait_lock);
   1188 	sc->sc_ctrl_inuse = FREE;
   1189 	cv_signal(&sc->sc_ctrl_wait);
   1190 	mutex_exit(&sc->sc_ctrl_wait_lock);
   1191 
   1192 	return r;
   1193 }
   1194 
   1195 /* ctrl vq interrupt; wake up the command issuer */
   1196 static int
   1197 vioif_ctrl_vq_done(struct virtqueue *vq)
   1198 {
   1199 	struct virtio_softc *vsc = vq->vq_owner;
   1200 	struct vioif_softc *sc = device_private(vsc->sc_child);
   1201 	int r, slot;
   1202 
   1203 	r = virtio_dequeue(vsc, vq, &slot, NULL);
   1204 	if (r == ENOENT)
   1205 		return 0;
   1206 	virtio_dequeue_commit(vsc, vq, slot);
   1207 
   1208 	mutex_enter(&sc->sc_ctrl_wait_lock);
   1209 	sc->sc_ctrl_inuse = DONE;
   1210 	cv_signal(&sc->sc_ctrl_wait);
   1211 	mutex_exit(&sc->sc_ctrl_wait_lock);
   1212 
   1213 	return 1;
   1214 }
   1215 
   1216 /*
   1217  * If IFF_PROMISC requested,  set promiscuous
   1218  * If multicast filter small enough (<=MAXENTRIES) set rx filter
   1219  * If large multicast filter exist use ALLMULTI
   1220  */
   1221 /*
   1222  * If setting rx filter fails fall back to ALLMULTI
   1223  * If ALLMULTI fails fall back to PROMISC
   1224  */
   1225 static int
   1226 vioif_rx_filter(struct vioif_softc *sc)
   1227 {
   1228 	struct virtio_softc *vsc = sc->sc_virtio;
   1229 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1230 	struct ether_multi *enm;
   1231 	struct ether_multistep step;
   1232 	int nentries;
   1233 	int promisc = 0, allmulti = 0, rxfilter = 0;
   1234 	int r;
   1235 
   1236 	if (vsc->sc_nvqs < 3) {	/* no ctrl vq; always promisc */
   1237 		ifp->if_flags |= IFF_PROMISC;
   1238 		return 0;
   1239 	}
   1240 
   1241 	if (ifp->if_flags & IFF_PROMISC) {
   1242 		promisc = 1;
   1243 		goto set;
   1244 	}
   1245 
   1246 	nentries = -1;
   1247 	ETHER_FIRST_MULTI(step, &sc->sc_ethercom, enm);
   1248 	while (nentries++, enm != NULL) {
   1249 		if (nentries >= VIRTIO_NET_CTRL_MAC_MAXENTRIES) {
   1250 			allmulti = 1;
   1251 			goto set;
   1252 		}
   1253 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
   1254 			   ETHER_ADDR_LEN)) {
   1255 			allmulti = 1;
   1256 			goto set;
   1257 		}
   1258 		memcpy(sc->sc_ctrl_mac_tbl_mc->macs[nentries],
   1259 		       enm->enm_addrlo, ETHER_ADDR_LEN);
   1260 		ETHER_NEXT_MULTI(step, enm);
   1261 	}
   1262 	rxfilter = 1;
   1263 
   1264 set:
   1265 	if (rxfilter) {
   1266 		sc->sc_ctrl_mac_tbl_uc->nentries = 0;
   1267 		sc->sc_ctrl_mac_tbl_mc->nentries = nentries;
   1268 		r = vioif_set_rx_filter(sc);
   1269 		if (r != 0) {
   1270 			rxfilter = 0;
   1271 			allmulti = 1; /* fallback */
   1272 		}
   1273 	} else {
   1274 		/* remove rx filter */
   1275 		sc->sc_ctrl_mac_tbl_uc->nentries = 0;
   1276 		sc->sc_ctrl_mac_tbl_mc->nentries = 0;
   1277 		r = vioif_set_rx_filter(sc);
   1278 		/* what to do on failure? */
   1279 	}
   1280 	if (allmulti) {
   1281 		r = vioif_set_allmulti(sc, true);
   1282 		if (r != 0) {
   1283 			allmulti = 0;
   1284 			promisc = 1; /* fallback */
   1285 		}
   1286 	} else {
   1287 		r = vioif_set_allmulti(sc, false);
   1288 		/* what to do on failure? */
   1289 	}
   1290 	if (promisc) {
   1291 		r = vioif_set_promisc(sc, true);
   1292 	} else {
   1293 		r = vioif_set_promisc(sc, false);
   1294 	}
   1295 
   1296 	return r;
   1297 }
   1298 
   1299 /* change link status */
   1300 static int
   1301 vioif_updown(struct vioif_softc *sc, bool isup)
   1302 {
   1303 	struct virtio_softc *vsc = sc->sc_virtio;
   1304 
   1305 	if (!(vsc->sc_features & VIRTIO_NET_F_STATUS))
   1306 		return ENODEV;
   1307 	virtio_write_device_config_1(vsc,
   1308 				     VIRTIO_NET_CONFIG_STATUS,
   1309 				     isup?VIRTIO_NET_S_LINK_UP:0);
   1310 	return 0;
   1311 }
   1312