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