Home | History | Annotate | Line # | Download | only in pci
if_vioif.c revision 1.87
      1 /*	$NetBSD: if_vioif.c,v 1.87 2023/03/23 01:36:50 yamaguchi Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2020 The NetBSD Foundation, Inc.
      5  * Copyright (c) 2010 Minoura Makoto.
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: if_vioif.c,v 1.87 2023/03/23 01:36:50 yamaguchi Exp $");
     31 
     32 #ifdef _KERNEL_OPT
     33 #include "opt_net_mpsafe.h"
     34 #endif
     35 
     36 #include <sys/param.h>
     37 #include <sys/systm.h>
     38 #include <sys/kernel.h>
     39 #include <sys/atomic.h>
     40 #include <sys/bus.h>
     41 #include <sys/condvar.h>
     42 #include <sys/device.h>
     43 #include <sys/evcnt.h>
     44 #include <sys/intr.h>
     45 #include <sys/kmem.h>
     46 #include <sys/mbuf.h>
     47 #include <sys/mutex.h>
     48 #include <sys/sockio.h>
     49 #include <sys/syslog.h>
     50 #include <sys/cpu.h>
     51 #include <sys/module.h>
     52 #include <sys/pcq.h>
     53 #include <sys/workqueue.h>
     54 #include <sys/xcall.h>
     55 
     56 #include <dev/pci/virtioreg.h>
     57 #include <dev/pci/virtiovar.h>
     58 
     59 #include <net/if.h>
     60 #include <net/if_dl.h>
     61 #include <net/if_media.h>
     62 #include <net/if_ether.h>
     63 
     64 #include <net/bpf.h>
     65 
     66 #include "ioconf.h"
     67 
     68 #ifdef NET_MPSAFE
     69 #define VIOIF_MPSAFE	1
     70 #define VIOIF_MULTIQ	1
     71 #endif
     72 
     73 /*
     74  * if_vioifreg.h:
     75  */
     76 /* Configuration registers */
     77 #define VIRTIO_NET_CONFIG_MAC		 0 /* 8bit x 6byte */
     78 #define VIRTIO_NET_CONFIG_STATUS	 6 /* 16bit */
     79 #define VIRTIO_NET_CONFIG_MAX_VQ_PAIRS	 8 /* 16bit */
     80 #define VIRTIO_NET_CONFIG_MTU		10 /* 16bit */
     81 
     82 /* Feature bits */
     83 #define VIRTIO_NET_F_CSUM		__BIT(0)
     84 #define VIRTIO_NET_F_GUEST_CSUM		__BIT(1)
     85 #define VIRTIO_NET_F_MAC		__BIT(5)
     86 #define VIRTIO_NET_F_GSO		__BIT(6)
     87 #define VIRTIO_NET_F_GUEST_TSO4		__BIT(7)
     88 #define VIRTIO_NET_F_GUEST_TSO6		__BIT(8)
     89 #define VIRTIO_NET_F_GUEST_ECN		__BIT(9)
     90 #define VIRTIO_NET_F_GUEST_UFO		__BIT(10)
     91 #define VIRTIO_NET_F_HOST_TSO4		__BIT(11)
     92 #define VIRTIO_NET_F_HOST_TSO6		__BIT(12)
     93 #define VIRTIO_NET_F_HOST_ECN		__BIT(13)
     94 #define VIRTIO_NET_F_HOST_UFO		__BIT(14)
     95 #define VIRTIO_NET_F_MRG_RXBUF		__BIT(15)
     96 #define VIRTIO_NET_F_STATUS		__BIT(16)
     97 #define VIRTIO_NET_F_CTRL_VQ		__BIT(17)
     98 #define VIRTIO_NET_F_CTRL_RX		__BIT(18)
     99 #define VIRTIO_NET_F_CTRL_VLAN		__BIT(19)
    100 #define VIRTIO_NET_F_CTRL_RX_EXTRA	__BIT(20)
    101 #define VIRTIO_NET_F_GUEST_ANNOUNCE	__BIT(21)
    102 #define VIRTIO_NET_F_MQ			__BIT(22)
    103 #define VIRTIO_NET_F_CTRL_MAC_ADDR 	__BIT(23)
    104 
    105 #define VIRTIO_NET_FLAG_BITS			\
    106 	VIRTIO_COMMON_FLAG_BITS			\
    107 	"b\x17" "CTRL_MAC\0"			\
    108 	"b\x16" "MQ\0"				\
    109 	"b\x15" "GUEST_ANNOUNCE\0"		\
    110 	"b\x14" "CTRL_RX_EXTRA\0"		\
    111 	"b\x13" "CTRL_VLAN\0"			\
    112 	"b\x12" "CTRL_RX\0"			\
    113 	"b\x11" "CTRL_VQ\0"			\
    114 	"b\x10" "STATUS\0"			\
    115 	"b\x0f" "MRG_RXBUF\0"			\
    116 	"b\x0e" "HOST_UFO\0"			\
    117 	"b\x0d" "HOST_ECN\0"			\
    118 	"b\x0c" "HOST_TSO6\0"			\
    119 	"b\x0b" "HOST_TSO4\0"			\
    120 	"b\x0a" "GUEST_UFO\0"			\
    121 	"b\x09" "GUEST_ECN\0"			\
    122 	"b\x08" "GUEST_TSO6\0"			\
    123 	"b\x07" "GUEST_TSO4\0"			\
    124 	"b\x06" "GSO\0"				\
    125 	"b\x05" "MAC\0"				\
    126 	"b\x01" "GUEST_CSUM\0"			\
    127 	"b\x00" "CSUM\0"
    128 
    129 /* Status */
    130 #define VIRTIO_NET_S_LINK_UP	1
    131 
    132 /* Packet header structure */
    133 struct virtio_net_hdr {
    134 	uint8_t		flags;
    135 	uint8_t		gso_type;
    136 	uint16_t	hdr_len;
    137 	uint16_t	gso_size;
    138 	uint16_t	csum_start;
    139 	uint16_t	csum_offset;
    140 
    141 	uint16_t	num_buffers; /* VIRTIO_NET_F_MRG_RXBUF enabled or v1 */
    142 } __packed;
    143 
    144 #define VIRTIO_NET_HDR_F_NEEDS_CSUM	1 /* flags */
    145 #define VIRTIO_NET_HDR_GSO_NONE		0 /* gso_type */
    146 #define VIRTIO_NET_HDR_GSO_TCPV4	1 /* gso_type */
    147 #define VIRTIO_NET_HDR_GSO_UDP		3 /* gso_type */
    148 #define VIRTIO_NET_HDR_GSO_TCPV6	4 /* gso_type */
    149 #define VIRTIO_NET_HDR_GSO_ECN		0x80 /* gso_type, |'ed */
    150 
    151 #define VIRTIO_NET_MAX_GSO_LEN		(65536+ETHER_HDR_LEN)
    152 
    153 /* Control virtqueue */
    154 struct virtio_net_ctrl_cmd {
    155 	uint8_t	class;
    156 	uint8_t	command;
    157 } __packed;
    158 #define VIRTIO_NET_CTRL_RX		0
    159 # define VIRTIO_NET_CTRL_RX_PROMISC	0
    160 # define VIRTIO_NET_CTRL_RX_ALLMULTI	1
    161 
    162 #define VIRTIO_NET_CTRL_MAC		1
    163 # define VIRTIO_NET_CTRL_MAC_TABLE_SET	0
    164 # define  VIRTIO_NET_CTRL_MAC_ADDR_SET	1
    165 
    166 #define VIRTIO_NET_CTRL_VLAN		2
    167 # define VIRTIO_NET_CTRL_VLAN_ADD	0
    168 # define VIRTIO_NET_CTRL_VLAN_DEL	1
    169 
    170 #define VIRTIO_NET_CTRL_MQ			4
    171 # define VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET	0
    172 # define VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN	1
    173 # define VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX	0x8000
    174 
    175 struct virtio_net_ctrl_status {
    176 	uint8_t	ack;
    177 } __packed;
    178 #define VIRTIO_NET_OK			0
    179 #define VIRTIO_NET_ERR			1
    180 
    181 struct virtio_net_ctrl_rx {
    182 	uint8_t	onoff;
    183 } __packed;
    184 
    185 struct virtio_net_ctrl_mac_tbl {
    186 	uint32_t nentries;
    187 	uint8_t macs[][ETHER_ADDR_LEN];
    188 } __packed;
    189 
    190 struct virtio_net_ctrl_mac_addr {
    191 	uint8_t mac[ETHER_ADDR_LEN];
    192 } __packed;
    193 
    194 struct virtio_net_ctrl_vlan {
    195 	uint16_t id;
    196 } __packed;
    197 
    198 struct virtio_net_ctrl_mq {
    199 	uint16_t virtqueue_pairs;
    200 } __packed;
    201 
    202 /*
    203  * if_vioifvar.h:
    204  */
    205 
    206 /*
    207  * Locking notes:
    208  * + a field in vioif_txqueue is protected by txq_lock (a spin mutex), and
    209  *   a field in vioif_rxqueue is protected by rxq_lock (a spin mutex).
    210  *      - more than one lock cannot be held at onece
    211  * + ctrlq_inuse is protected by ctrlq_wait_lock.
    212  *      - other fields in vioif_ctrlqueue are protected by ctrlq_inuse
    213  *      - txq_lock or rxq_lock cannot be held along with ctrlq_wait_lock
    214  * + fields in vioif_softc except queues are protected by
    215  *   sc->sc_lock(an adaptive mutex)
    216  *      - the lock is held before acquisition of other locks
    217  */
    218 
    219 struct vioif_ctrl_cmdspec {
    220 	bus_dmamap_t	dmamap;
    221 	void		*buf;
    222 	bus_size_t	bufsize;
    223 };
    224 
    225 struct vioif_work {
    226 	struct work	 cookie;
    227 	void		(*func)(void *);
    228 	void		*arg;
    229 	unsigned int	 added;
    230 };
    231 
    232 struct vioif_txqueue {
    233 	kmutex_t		*txq_lock;	/* lock for tx operations */
    234 
    235 	struct virtqueue	*txq_vq;
    236 	bool			txq_stopping;
    237 	bool			txq_link_active;
    238 	pcq_t			*txq_intrq;
    239 
    240 	struct virtio_net_hdr	*txq_hdrs;
    241 	bus_dmamap_t		*txq_hdr_dmamaps;
    242 
    243 	struct mbuf		**txq_mbufs;
    244 	bus_dmamap_t		*txq_dmamaps;
    245 
    246 	void			*txq_deferred_transmit;
    247 	void			*txq_handle_si;
    248 	struct vioif_work	 txq_work;
    249 	bool			 txq_workqueue;
    250 	bool			 txq_running_handle;
    251 
    252 	char			 txq_evgroup[16];
    253 	struct evcnt		 txq_defrag_failed;
    254 	struct evcnt		 txq_mbuf_load_failed;
    255 	struct evcnt		 txq_enqueue_reserve_failed;
    256 };
    257 
    258 struct vioif_rxqueue {
    259 	kmutex_t		*rxq_lock;	/* lock for rx operations */
    260 
    261 	struct virtqueue	*rxq_vq;
    262 	bool			rxq_stopping;
    263 
    264 	struct virtio_net_hdr	*rxq_hdrs;
    265 	bus_dmamap_t		*rxq_hdr_dmamaps;
    266 
    267 	struct mbuf		**rxq_mbufs;
    268 	bus_dmamap_t		*rxq_dmamaps;
    269 
    270 	void			*rxq_handle_si;
    271 	struct vioif_work	 rxq_work;
    272 	bool			 rxq_workqueue;
    273 	bool			 rxq_running_handle;
    274 
    275 	char			 rxq_evgroup[16];
    276 	struct evcnt		 rxq_mbuf_add_failed;
    277 };
    278 
    279 struct vioif_ctrlqueue {
    280 	struct virtqueue		*ctrlq_vq;
    281 	enum {
    282 		FREE, INUSE, DONE
    283 	}				ctrlq_inuse;
    284 	kcondvar_t			ctrlq_wait;
    285 	kmutex_t			ctrlq_wait_lock;
    286 	struct lwp			*ctrlq_owner;
    287 
    288 	struct virtio_net_ctrl_cmd	*ctrlq_cmd;
    289 	struct virtio_net_ctrl_status	*ctrlq_status;
    290 	struct virtio_net_ctrl_rx	*ctrlq_rx;
    291 	struct virtio_net_ctrl_mac_tbl	*ctrlq_mac_tbl_uc;
    292 	struct virtio_net_ctrl_mac_tbl	*ctrlq_mac_tbl_mc;
    293 	struct virtio_net_ctrl_mac_addr	*ctrlq_mac_addr;
    294 	struct virtio_net_ctrl_mq	*ctrlq_mq;
    295 
    296 	bus_dmamap_t			ctrlq_cmd_dmamap;
    297 	bus_dmamap_t			ctrlq_status_dmamap;
    298 	bus_dmamap_t			ctrlq_rx_dmamap;
    299 	bus_dmamap_t			ctrlq_tbl_uc_dmamap;
    300 	bus_dmamap_t			ctrlq_tbl_mc_dmamap;
    301 	bus_dmamap_t			ctrlq_mac_addr_dmamap;
    302 	bus_dmamap_t			ctrlq_mq_dmamap;
    303 
    304 	struct evcnt			ctrlq_cmd_load_failed;
    305 	struct evcnt			ctrlq_cmd_failed;
    306 };
    307 
    308 struct vioif_softc {
    309 	device_t		sc_dev;
    310 	kmutex_t		sc_lock;
    311 	struct sysctllog	*sc_sysctllog;
    312 
    313 	struct virtio_softc	*sc_virtio;
    314 	struct virtqueue	*sc_vqs;
    315 	u_int			 sc_hdr_size;
    316 
    317 	int			sc_max_nvq_pairs;
    318 	int			sc_req_nvq_pairs;
    319 	int			sc_act_nvq_pairs;
    320 
    321 	uint8_t			sc_mac[ETHER_ADDR_LEN];
    322 	struct ethercom		sc_ethercom;
    323 	int			sc_link_state;
    324 
    325 	struct vioif_txqueue	*sc_txq;
    326 	struct vioif_rxqueue	*sc_rxq;
    327 
    328 	bool			sc_has_ctrl;
    329 	struct vioif_ctrlqueue	sc_ctrlq;
    330 
    331 	bus_dma_segment_t	sc_hdr_segs[1];
    332 	void			*sc_dmamem;
    333 	void			*sc_kmem;
    334 
    335 	void			*sc_ctl_softint;
    336 
    337 	struct workqueue	*sc_txrx_workqueue;
    338 	bool			 sc_txrx_workqueue_sysctl;
    339 	u_int			 sc_tx_intr_process_limit;
    340 	u_int			 sc_tx_process_limit;
    341 	u_int			 sc_rx_intr_process_limit;
    342 	u_int			 sc_rx_process_limit;
    343 };
    344 #define VIRTIO_NET_TX_MAXNSEGS		(16) /* XXX */
    345 #define VIRTIO_NET_CTRL_MAC_MAXENTRIES	(64) /* XXX */
    346 
    347 #define VIOIF_TX_INTR_PROCESS_LIMIT	256
    348 #define VIOIF_TX_PROCESS_LIMIT		256
    349 #define VIOIF_RX_INTR_PROCESS_LIMIT	0U
    350 #define VIOIF_RX_PROCESS_LIMIT		256
    351 
    352 #define VIOIF_WORKQUEUE_PRI		PRI_SOFTNET
    353 #define VIOIF_IS_LINK_ACTIVE(_sc)	((_sc)->sc_link_state == LINK_STATE_UP ? \
    354 					    true : false)
    355 
    356 /* cfattach interface functions */
    357 static int	vioif_match(device_t, cfdata_t, void *);
    358 static void	vioif_attach(device_t, device_t, void *);
    359 static int	vioif_finalize_teardown(device_t);
    360 
    361 /* ifnet interface functions */
    362 static int	vioif_init(struct ifnet *);
    363 static void	vioif_stop(struct ifnet *, int);
    364 static void	vioif_start(struct ifnet *);
    365 static void	vioif_start_locked(struct ifnet *, struct vioif_txqueue *);
    366 static int	vioif_transmit(struct ifnet *, struct mbuf *);
    367 static void	vioif_transmit_locked(struct ifnet *, struct vioif_txqueue *);
    368 static int	vioif_ioctl(struct ifnet *, u_long, void *);
    369 static void	vioif_watchdog(struct ifnet *);
    370 static int	vioif_ifflags_cb(struct ethercom *);
    371 
    372 /* rx */
    373 static int	vioif_add_rx_mbuf(struct vioif_rxqueue *, int);
    374 static void	vioif_free_rx_mbuf(struct vioif_rxqueue *, int);
    375 static void	vioif_populate_rx_mbufs_locked(struct vioif_softc *,
    376 		    struct vioif_rxqueue *);
    377 static void	vioif_rx_queue_clear(struct vioif_rxqueue *);
    378 static bool	vioif_rx_deq_locked(struct vioif_softc *, struct virtio_softc *,
    379 		    struct vioif_rxqueue *, u_int);
    380 static int	vioif_rx_intr(void *);
    381 static void	vioif_rx_handle(void *);
    382 static void	vioif_rx_sched_handle(struct vioif_softc *,
    383 		    struct vioif_rxqueue *);
    384 static void	vioif_rx_drain(struct vioif_rxqueue *);
    385 
    386 /* tx */
    387 static int	vioif_tx_intr(void *);
    388 static void	vioif_tx_handle(void *);
    389 static void	vioif_tx_sched_handle(struct vioif_softc *,
    390 		    struct vioif_txqueue *);
    391 static void	vioif_tx_queue_clear(struct vioif_txqueue *);
    392 static bool	vioif_tx_deq_locked(struct vioif_softc *, struct virtio_softc *,
    393 		    struct vioif_txqueue *, u_int);
    394 static void	vioif_tx_drain(struct vioif_txqueue *);
    395 static void	vioif_deferred_transmit(void *);
    396 
    397 /* workqueue */
    398 static struct workqueue*
    399 		vioif_workq_create(const char *, pri_t, int, int);
    400 static void	vioif_workq_destroy(struct workqueue *);
    401 static void	vioif_workq_work(struct work *, void *);
    402 static void	vioif_work_set(struct vioif_work *, void(*)(void *), void *);
    403 static void	vioif_work_add(struct workqueue *, struct vioif_work *);
    404 static void	vioif_work_wait(struct workqueue *, struct vioif_work *);
    405 
    406 /* other control */
    407 static int	vioif_get_link_status(struct vioif_softc *);
    408 static void	vioif_update_link_status(struct vioif_softc *);
    409 static int	vioif_ctrl_rx(struct vioif_softc *, int, bool);
    410 static int	vioif_set_promisc(struct vioif_softc *, bool);
    411 static int	vioif_set_allmulti(struct vioif_softc *, bool);
    412 static int	vioif_set_rx_filter(struct vioif_softc *);
    413 static int	vioif_rx_filter(struct vioif_softc *);
    414 static int	vioif_set_mac_addr(struct vioif_softc *);
    415 static int	vioif_ctrl_intr(void *);
    416 static int	vioif_config_change(struct virtio_softc *);
    417 static void	vioif_ctl_softint(void *);
    418 static int	vioif_ctrl_mq_vq_pairs_set(struct vioif_softc *, int);
    419 static void	vioif_enable_interrupt_vqpairs(struct vioif_softc *);
    420 static void	vioif_disable_interrupt_vqpairs(struct vioif_softc *);
    421 static int	vioif_setup_sysctl(struct vioif_softc *);
    422 static void	vioif_setup_stats(struct vioif_softc *);
    423 static int	vioif_ifflags(struct vioif_softc *);
    424 static void	vioif_intr_barrier(void);
    425 
    426 CFATTACH_DECL_NEW(vioif, sizeof(struct vioif_softc),
    427 		  vioif_match, vioif_attach, NULL, NULL);
    428 
    429 static int
    430 vioif_match(device_t parent, cfdata_t match, void *aux)
    431 {
    432 	struct virtio_attach_args *va = aux;
    433 
    434 	if (va->sc_childdevid == VIRTIO_DEVICE_ID_NETWORK)
    435 		return 1;
    436 
    437 	return 0;
    438 }
    439 
    440 static int
    441 vioif_dmamap_create(struct vioif_softc *sc, bus_dmamap_t *map,
    442     bus_size_t size, int nsegs, const char *usage)
    443 {
    444 	int r;
    445 
    446 	r = bus_dmamap_create(virtio_dmat(sc->sc_virtio), size,
    447 	    nsegs, size, 0, BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, map);
    448 
    449 	if (r != 0) {
    450 		aprint_error_dev(sc->sc_dev, "%s dmamap creation failed, "
    451 		    "error code %d\n", usage, r);
    452 	}
    453 
    454 	return r;
    455 }
    456 
    457 static void
    458 vioif_dmamap_destroy(struct vioif_softc *sc, bus_dmamap_t *map)
    459 {
    460 
    461 	if (*map) {
    462 		bus_dmamap_destroy(virtio_dmat(sc->sc_virtio), *map);
    463 		*map = NULL;
    464 	}
    465 }
    466 
    467 static int
    468 vioif_dmamap_create_load(struct vioif_softc *sc, bus_dmamap_t *map,
    469     void *buf, bus_size_t size, int nsegs, int rw, const char *usage)
    470 {
    471 	int r;
    472 
    473 	r = vioif_dmamap_create(sc, map, size, nsegs, usage);
    474 	if (r != 0)
    475 		return 1;
    476 
    477 	r = bus_dmamap_load(virtio_dmat(sc->sc_virtio), *map, buf,
    478 	    size, NULL, rw | BUS_DMA_NOWAIT);
    479 	if (r != 0) {
    480 		vioif_dmamap_destroy(sc, map);
    481 		aprint_error_dev(sc->sc_dev, "%s dmamap load failed. "
    482 		    "error code %d\n", usage, r);
    483 	}
    484 
    485 	return r;
    486 }
    487 
    488 static void *
    489 vioif_assign_mem(intptr_t *p, size_t size)
    490 {
    491 	intptr_t rv;
    492 
    493 	rv = *p;
    494 	*p += size;
    495 
    496 	return (void *)rv;
    497 }
    498 
    499 static void
    500 vioif_alloc_queues(struct vioif_softc *sc)
    501 {
    502 	int nvq_pairs = sc->sc_max_nvq_pairs;
    503 	int nvqs = nvq_pairs * 2;
    504 	int i;
    505 
    506 	KASSERT(nvq_pairs <= VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX);
    507 
    508 	sc->sc_rxq = kmem_zalloc(sizeof(sc->sc_rxq[0]) * nvq_pairs,
    509 	    KM_SLEEP);
    510 	sc->sc_txq = kmem_zalloc(sizeof(sc->sc_txq[0]) * nvq_pairs,
    511 	    KM_SLEEP);
    512 
    513 	if (sc->sc_has_ctrl)
    514 		nvqs++;
    515 
    516 	sc->sc_vqs = kmem_zalloc(sizeof(sc->sc_vqs[0]) * nvqs, KM_SLEEP);
    517 	nvqs = 0;
    518 	for (i = 0; i < nvq_pairs; i++) {
    519 		sc->sc_rxq[i].rxq_vq = &sc->sc_vqs[nvqs++];
    520 		sc->sc_txq[i].txq_vq = &sc->sc_vqs[nvqs++];
    521 	}
    522 
    523 	if (sc->sc_has_ctrl)
    524 		sc->sc_ctrlq.ctrlq_vq = &sc->sc_vqs[nvqs++];
    525 }
    526 
    527 static void
    528 vioif_free_queues(struct vioif_softc *sc)
    529 {
    530 	int nvq_pairs = sc->sc_max_nvq_pairs;
    531 	int nvqs = nvq_pairs * 2;
    532 
    533 	if (sc->sc_ctrlq.ctrlq_vq)
    534 		nvqs++;
    535 
    536 	if (sc->sc_txq) {
    537 		kmem_free(sc->sc_txq, sizeof(sc->sc_txq[0]) * nvq_pairs);
    538 		sc->sc_txq = NULL;
    539 	}
    540 
    541 	if (sc->sc_rxq) {
    542 		kmem_free(sc->sc_rxq, sizeof(sc->sc_rxq[0]) * nvq_pairs);
    543 		sc->sc_rxq = NULL;
    544 	}
    545 
    546 	if (sc->sc_vqs) {
    547 		kmem_free(sc->sc_vqs, sizeof(sc->sc_vqs[0]) * nvqs);
    548 		sc->sc_vqs = NULL;
    549 	}
    550 }
    551 
    552 /* allocate memory */
    553 /*
    554  * dma memory is used for:
    555  *   rxq_hdrs[slot]:	 metadata array for received frames (READ)
    556  *   txq_hdrs[slot]:	 metadata array for frames to be sent (WRITE)
    557  *   ctrlq_cmd:		 command to be sent via ctrl vq (WRITE)
    558  *   ctrlq_status:	 return value for a command via ctrl vq (READ)
    559  *   ctrlq_rx:		 parameter for a VIRTIO_NET_CTRL_RX class command
    560  *			 (WRITE)
    561  *   ctrlq_mac_tbl_uc:	 unicast MAC address filter for a VIRTIO_NET_CTRL_MAC
    562  *			 class command (WRITE)
    563  *   ctrlq_mac_tbl_mc:	 multicast MAC address filter for a VIRTIO_NET_CTRL_MAC
    564  *			 class command (WRITE)
    565  * ctrlq_* structures are allocated only one each; they are protected by
    566  * ctrlq_inuse variable and ctrlq_wait condvar.
    567  */
    568 /*
    569  * dynamically allocated memory is used for:
    570  *   rxq_hdr_dmamaps[slot]:	bus_dmamap_t array for sc_rx_hdrs[slot]
    571  *   txq_hdr_dmamaps[slot]:	bus_dmamap_t array for sc_tx_hdrs[slot]
    572  *   rxq_dmamaps[slot]:		bus_dmamap_t array for received payload
    573  *   txq_dmamaps[slot]:		bus_dmamap_t array for sent payload
    574  *   rxq_mbufs[slot]:		mbuf pointer array for received frames
    575  *   txq_mbufs[slot]:		mbuf pointer array for sent frames
    576  */
    577 static int
    578 vioif_alloc_mems(struct vioif_softc *sc)
    579 {
    580 	struct virtio_softc *vsc = sc->sc_virtio;
    581 	struct vioif_txqueue *txq;
    582 	struct vioif_rxqueue *rxq;
    583 	struct vioif_ctrlqueue *ctrlq = &sc->sc_ctrlq;
    584 	int allocsize, allocsize2, r, rsegs, i, qid;
    585 	void *vaddr;
    586 	intptr_t p;
    587 
    588 	allocsize = 0;
    589 	for (qid = 0; qid < sc->sc_max_nvq_pairs; qid++) {
    590 		rxq = &sc->sc_rxq[qid];
    591 		txq = &sc->sc_txq[qid];
    592 
    593 		allocsize += sizeof(struct virtio_net_hdr) *
    594 			(rxq->rxq_vq->vq_num + txq->txq_vq->vq_num);
    595 	}
    596 	if (sc->sc_has_ctrl) {
    597 		allocsize += sizeof(struct virtio_net_ctrl_cmd);
    598 		allocsize += sizeof(struct virtio_net_ctrl_status);
    599 		allocsize += sizeof(struct virtio_net_ctrl_rx);
    600 		allocsize += sizeof(struct virtio_net_ctrl_mac_tbl)
    601 		    + ETHER_ADDR_LEN;
    602 		allocsize += sizeof(struct virtio_net_ctrl_mac_tbl)
    603 		    + ETHER_ADDR_LEN * VIRTIO_NET_CTRL_MAC_MAXENTRIES;
    604 		allocsize += sizeof(struct virtio_net_ctrl_mac_addr);
    605 		allocsize += sizeof(struct virtio_net_ctrl_mq);
    606 	}
    607 	r = bus_dmamem_alloc(virtio_dmat(vsc), allocsize, 0, 0,
    608 	    &sc->sc_hdr_segs[0], 1, &rsegs, BUS_DMA_NOWAIT);
    609 	if (r != 0) {
    610 		aprint_error_dev(sc->sc_dev,
    611 		    "DMA memory allocation failed, size %d, "
    612 		    "error code %d\n", allocsize, r);
    613 		goto err_none;
    614 	}
    615 	r = bus_dmamem_map(virtio_dmat(vsc),
    616 	    &sc->sc_hdr_segs[0], 1, allocsize, &vaddr, BUS_DMA_NOWAIT);
    617 	if (r != 0) {
    618 		aprint_error_dev(sc->sc_dev,
    619 		    "DMA memory map failed, error code %d\n", r);
    620 		goto err_dmamem_alloc;
    621 	}
    622 
    623 	memset(vaddr, 0, allocsize);
    624 	sc->sc_dmamem = vaddr;
    625 	p = (intptr_t) vaddr;
    626 
    627 	for (qid = 0; qid < sc->sc_max_nvq_pairs; qid++) {
    628 		rxq = &sc->sc_rxq[qid];
    629 		txq = &sc->sc_txq[qid];
    630 
    631 		rxq->rxq_hdrs = vioif_assign_mem(&p,
    632 		    sizeof(struct virtio_net_hdr) * rxq->rxq_vq->vq_num);
    633 		txq->txq_hdrs = vioif_assign_mem(&p,
    634 		    sizeof(struct virtio_net_hdr) * txq->txq_vq->vq_num);
    635 	}
    636 	if (sc->sc_has_ctrl) {
    637 		ctrlq->ctrlq_cmd = vioif_assign_mem(&p,
    638 		    sizeof(*ctrlq->ctrlq_cmd));
    639 		ctrlq->ctrlq_status = vioif_assign_mem(&p,
    640 		    sizeof(*ctrlq->ctrlq_status));
    641 		ctrlq->ctrlq_rx = vioif_assign_mem(&p,
    642 		    sizeof(*ctrlq->ctrlq_rx));
    643 		ctrlq->ctrlq_mac_tbl_uc = vioif_assign_mem(&p,
    644 		    sizeof(*ctrlq->ctrlq_mac_tbl_uc)
    645 		    + ETHER_ADDR_LEN);
    646 		ctrlq->ctrlq_mac_tbl_mc = vioif_assign_mem(&p,
    647 		    sizeof(*ctrlq->ctrlq_mac_tbl_mc)
    648 		    + ETHER_ADDR_LEN * VIRTIO_NET_CTRL_MAC_MAXENTRIES);
    649 		ctrlq->ctrlq_mac_addr = vioif_assign_mem(&p,
    650 		    sizeof(*ctrlq->ctrlq_mac_addr));
    651 		ctrlq->ctrlq_mq = vioif_assign_mem(&p, sizeof(*ctrlq->ctrlq_mq));
    652 	}
    653 
    654 	allocsize2 = 0;
    655 	for (qid = 0; qid < sc->sc_max_nvq_pairs; qid++) {
    656 		int rxqsize, txqsize;
    657 
    658 		rxq = &sc->sc_rxq[qid];
    659 		txq = &sc->sc_txq[qid];
    660 		rxqsize = rxq->rxq_vq->vq_num;
    661 		txqsize = txq->txq_vq->vq_num;
    662 
    663 		allocsize2 += sizeof(rxq->rxq_dmamaps[0]) * rxqsize;
    664 		allocsize2 += sizeof(rxq->rxq_hdr_dmamaps[0]) * rxqsize;
    665 		allocsize2 += sizeof(rxq->rxq_mbufs[0]) * rxqsize;
    666 
    667 		allocsize2 += sizeof(txq->txq_dmamaps[0]) * txqsize;
    668 		allocsize2 += sizeof(txq->txq_hdr_dmamaps[0]) * txqsize;
    669 		allocsize2 += sizeof(txq->txq_mbufs[0]) * txqsize;
    670 	}
    671 	vaddr = kmem_zalloc(allocsize2, KM_SLEEP);
    672 	sc->sc_kmem = vaddr;
    673 	p = (intptr_t) vaddr;
    674 
    675 	for (qid = 0; qid < sc->sc_max_nvq_pairs; qid++) {
    676 		int rxqsize, txqsize;
    677 		rxq = &sc->sc_rxq[qid];
    678 		txq = &sc->sc_txq[qid];
    679 		rxqsize = rxq->rxq_vq->vq_num;
    680 		txqsize = txq->txq_vq->vq_num;
    681 
    682 		rxq->rxq_hdr_dmamaps = vioif_assign_mem(&p,
    683 		    sizeof(rxq->rxq_hdr_dmamaps[0]) * rxqsize);
    684 		txq->txq_hdr_dmamaps = vioif_assign_mem(&p,
    685 		    sizeof(txq->txq_hdr_dmamaps[0]) * txqsize);
    686 		rxq->rxq_dmamaps = vioif_assign_mem(&p,
    687 		    sizeof(rxq->rxq_dmamaps[0]) * rxqsize);
    688 		txq->txq_dmamaps = vioif_assign_mem(&p,
    689 		    sizeof(txq->txq_dmamaps[0]) * txqsize);
    690 		rxq->rxq_mbufs = vioif_assign_mem(&p,
    691 		    sizeof(rxq->rxq_mbufs[0]) * rxqsize);
    692 		txq->txq_mbufs = vioif_assign_mem(&p,
    693 		    sizeof(txq->txq_mbufs[0]) * txqsize);
    694 	}
    695 
    696 	for (qid = 0; qid < sc->sc_max_nvq_pairs; qid++) {
    697 		rxq = &sc->sc_rxq[qid];
    698 		txq = &sc->sc_txq[qid];
    699 
    700 		for (i = 0; i < rxq->rxq_vq->vq_num; i++) {
    701 			r = vioif_dmamap_create_load(sc, &rxq->rxq_hdr_dmamaps[i],
    702 			    &rxq->rxq_hdrs[i], sc->sc_hdr_size, 1,
    703 			    BUS_DMA_READ, "rx header");
    704 			if (r != 0)
    705 				goto err_reqs;
    706 
    707 			r = vioif_dmamap_create(sc, &rxq->rxq_dmamaps[i],
    708 			    MCLBYTES, 1, "rx payload");
    709 			if (r != 0)
    710 				goto err_reqs;
    711 		}
    712 
    713 		for (i = 0; i < txq->txq_vq->vq_num; i++) {
    714 			r = vioif_dmamap_create_load(sc, &txq->txq_hdr_dmamaps[i],
    715 			    &txq->txq_hdrs[i], sc->sc_hdr_size, 1,
    716 			    BUS_DMA_READ, "tx header");
    717 			if (r != 0)
    718 				goto err_reqs;
    719 
    720 			r = vioif_dmamap_create(sc, &txq->txq_dmamaps[i], ETHER_MAX_LEN,
    721 			    VIRTIO_NET_TX_MAXNSEGS, "tx payload");
    722 			if (r != 0)
    723 				goto err_reqs;
    724 		}
    725 	}
    726 
    727 	if (sc->sc_has_ctrl) {
    728 		/* control vq class & command */
    729 		r = vioif_dmamap_create_load(sc, &ctrlq->ctrlq_cmd_dmamap,
    730 		    ctrlq->ctrlq_cmd, sizeof(*ctrlq->ctrlq_cmd), 1,
    731 		    BUS_DMA_WRITE, "control command");
    732 		if (r != 0)
    733 			goto err_reqs;
    734 
    735 		r = vioif_dmamap_create_load(sc, &ctrlq->ctrlq_status_dmamap,
    736 		    ctrlq->ctrlq_status, sizeof(*ctrlq->ctrlq_status), 1,
    737 		    BUS_DMA_READ, "control status");
    738 		if (r != 0)
    739 			goto err_reqs;
    740 
    741 		/* control vq rx mode command parameter */
    742 		r = vioif_dmamap_create_load(sc, &ctrlq->ctrlq_rx_dmamap,
    743 		    ctrlq->ctrlq_rx, sizeof(*ctrlq->ctrlq_rx), 1,
    744 		    BUS_DMA_WRITE, "rx mode control command");
    745 		if (r != 0)
    746 			goto err_reqs;
    747 
    748 		/* multiqueue set command */
    749 		r = vioif_dmamap_create_load(sc, &ctrlq->ctrlq_mq_dmamap,
    750 		    ctrlq->ctrlq_mq, sizeof(*ctrlq->ctrlq_mq), 1,
    751 		    BUS_DMA_WRITE, "multiqueue set command");
    752 		if (r != 0)
    753 			goto err_reqs;
    754 
    755 		/* control vq MAC filter table for unicast */
    756 		/* do not load now since its length is variable */
    757 		r = vioif_dmamap_create(sc, &ctrlq->ctrlq_tbl_uc_dmamap,
    758 		    sizeof(*ctrlq->ctrlq_mac_tbl_uc)
    759 		    + ETHER_ADDR_LEN, 1,
    760 		    "unicast MAC address filter command");
    761 		if (r != 0)
    762 			goto err_reqs;
    763 
    764 		/* control vq MAC filter table for multicast */
    765 		r = vioif_dmamap_create(sc, &ctrlq->ctrlq_tbl_mc_dmamap,
    766 		    sizeof(*ctrlq->ctrlq_mac_tbl_mc)
    767 		    + ETHER_ADDR_LEN * VIRTIO_NET_CTRL_MAC_MAXENTRIES, 1,
    768 		    "multicast MAC address filter command");
    769 		if (r != 0)
    770 			goto err_reqs;
    771 
    772 		/* control vq MAC address set command */
    773 		r = vioif_dmamap_create_load(sc,
    774 		    &ctrlq->ctrlq_mac_addr_dmamap,
    775 		    ctrlq->ctrlq_mac_addr,
    776 		    sizeof(*ctrlq->ctrlq_mac_addr), 1,
    777 		    BUS_DMA_WRITE, "mac addr set command");
    778 		if (r != 0)
    779 			goto err_reqs;
    780 	}
    781 
    782 	return 0;
    783 
    784 err_reqs:
    785 	vioif_dmamap_destroy(sc, &ctrlq->ctrlq_tbl_mc_dmamap);
    786 	vioif_dmamap_destroy(sc, &ctrlq->ctrlq_tbl_uc_dmamap);
    787 	vioif_dmamap_destroy(sc, &ctrlq->ctrlq_rx_dmamap);
    788 	vioif_dmamap_destroy(sc, &ctrlq->ctrlq_status_dmamap);
    789 	vioif_dmamap_destroy(sc, &ctrlq->ctrlq_cmd_dmamap);
    790 	vioif_dmamap_destroy(sc, &ctrlq->ctrlq_mac_addr_dmamap);
    791 	for (qid = 0; qid < sc->sc_max_nvq_pairs; qid++) {
    792 		rxq = &sc->sc_rxq[qid];
    793 		txq = &sc->sc_txq[qid];
    794 
    795 		for (i = 0; i < txq->txq_vq->vq_num; i++) {
    796 			vioif_dmamap_destroy(sc, &txq->txq_dmamaps[i]);
    797 			vioif_dmamap_destroy(sc, &txq->txq_hdr_dmamaps[i]);
    798 		}
    799 		for (i = 0; i < rxq->rxq_vq->vq_num; i++) {
    800 			vioif_dmamap_destroy(sc, &rxq->rxq_dmamaps[i]);
    801 			vioif_dmamap_destroy(sc, &rxq->rxq_hdr_dmamaps[i]);
    802 		}
    803 	}
    804 	if (sc->sc_kmem) {
    805 		kmem_free(sc->sc_kmem, allocsize2);
    806 		sc->sc_kmem = NULL;
    807 	}
    808 	bus_dmamem_unmap(virtio_dmat(vsc), sc->sc_dmamem, allocsize);
    809 err_dmamem_alloc:
    810 	bus_dmamem_free(virtio_dmat(vsc), &sc->sc_hdr_segs[0], 1);
    811 err_none:
    812 	return -1;
    813 }
    814 
    815 static void
    816 vioif_attach(device_t parent, device_t self, void *aux)
    817 {
    818 	struct vioif_softc *sc = device_private(self);
    819 	struct virtio_softc *vsc = device_private(parent);
    820 	struct vioif_ctrlqueue *ctrlq = &sc->sc_ctrlq;
    821 	struct vioif_txqueue *txq;
    822 	struct vioif_rxqueue *rxq;
    823 	uint64_t features, req_features;
    824 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    825 	u_int softint_flags;
    826 	int r, i, nvqs = 0, req_flags;
    827 	char xnamebuf[MAXCOMLEN];
    828 
    829 	if (virtio_child(vsc) != NULL) {
    830 		aprint_normal(": child already attached for %s; "
    831 		    "something wrong...\n", device_xname(parent));
    832 		return;
    833 	}
    834 
    835 	sc->sc_dev = self;
    836 	sc->sc_virtio = vsc;
    837 	sc->sc_link_state = LINK_STATE_UNKNOWN;
    838 
    839 	sc->sc_max_nvq_pairs = 1;
    840 	sc->sc_req_nvq_pairs = 1;
    841 	sc->sc_act_nvq_pairs = 1;
    842 	sc->sc_txrx_workqueue_sysctl = true;
    843 	sc->sc_tx_intr_process_limit = VIOIF_TX_INTR_PROCESS_LIMIT;
    844 	sc->sc_tx_process_limit = VIOIF_TX_PROCESS_LIMIT;
    845 	sc->sc_rx_intr_process_limit = VIOIF_RX_INTR_PROCESS_LIMIT;
    846 	sc->sc_rx_process_limit = VIOIF_RX_PROCESS_LIMIT;
    847 
    848 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
    849 
    850 	snprintf(xnamebuf, sizeof(xnamebuf), "%s_txrx", device_xname(self));
    851 	sc->sc_txrx_workqueue = vioif_workq_create(xnamebuf, VIOIF_WORKQUEUE_PRI,
    852 	    IPL_NET, WQ_PERCPU | WQ_MPSAFE);
    853 	if (sc->sc_txrx_workqueue == NULL)
    854 		goto err;
    855 
    856 	req_flags = 0;
    857 
    858 #ifdef VIOIF_MPSAFE
    859 	req_flags |= VIRTIO_F_INTR_MPSAFE;
    860 #endif
    861 	req_flags |= VIRTIO_F_INTR_MSIX;
    862 
    863 	req_features =
    864 	    VIRTIO_NET_F_MAC | VIRTIO_NET_F_STATUS | VIRTIO_NET_F_CTRL_VQ |
    865 	    VIRTIO_NET_F_CTRL_RX | VIRTIO_F_NOTIFY_ON_EMPTY;
    866 	req_features |= VIRTIO_F_RING_EVENT_IDX;
    867 	req_features |= VIRTIO_NET_F_CTRL_MAC_ADDR;
    868 #ifdef VIOIF_MULTIQ
    869 	req_features |= VIRTIO_NET_F_MQ;
    870 #endif
    871 	virtio_child_attach_start(vsc, self, IPL_NET, NULL,
    872 	    vioif_config_change, virtio_vq_intrhand, req_flags,
    873 	    req_features, VIRTIO_NET_FLAG_BITS);
    874 
    875 	features = virtio_features(vsc);
    876 	if (features == 0)
    877 		goto err;
    878 
    879 	if (features & VIRTIO_NET_F_MAC) {
    880 		for (i = 0; i < __arraycount(sc->sc_mac); i++) {
    881 			sc->sc_mac[i] = virtio_read_device_config_1(vsc,
    882 			    VIRTIO_NET_CONFIG_MAC + i);
    883 		}
    884 	} else {
    885 		/* code stolen from sys/net/if_tap.c */
    886 		struct timeval tv;
    887 		uint32_t ui;
    888 		getmicrouptime(&tv);
    889 		ui = (tv.tv_sec ^ tv.tv_usec) & 0xffffff;
    890 		memcpy(sc->sc_mac+3, (uint8_t *)&ui, 3);
    891 		for (i = 0; i < __arraycount(sc->sc_mac); i++) {
    892 			virtio_write_device_config_1(vsc,
    893 			    VIRTIO_NET_CONFIG_MAC + i, sc->sc_mac[i]);
    894 		}
    895 	}
    896 
    897 	/* 'Ethernet' with capital follows other ethernet driver attachment */
    898 	aprint_normal_dev(self, "Ethernet address %s\n",
    899 	    ether_sprintf(sc->sc_mac));
    900 
    901 	if (features & (VIRTIO_NET_F_MRG_RXBUF | VIRTIO_F_VERSION_1)) {
    902 		sc->sc_hdr_size = sizeof(struct virtio_net_hdr);
    903 	} else {
    904 		sc->sc_hdr_size = offsetof(struct virtio_net_hdr, num_buffers);
    905 	}
    906 
    907 	if ((features & VIRTIO_NET_F_CTRL_VQ) &&
    908 	    (features & VIRTIO_NET_F_CTRL_RX)) {
    909 		sc->sc_has_ctrl = true;
    910 
    911 		cv_init(&ctrlq->ctrlq_wait, "ctrl_vq");
    912 		mutex_init(&ctrlq->ctrlq_wait_lock, MUTEX_DEFAULT, IPL_NET);
    913 		ctrlq->ctrlq_inuse = FREE;
    914 	} else {
    915 		sc->sc_has_ctrl = false;
    916 	}
    917 
    918 	if (sc->sc_has_ctrl && (features & VIRTIO_NET_F_MQ)) {
    919 		sc->sc_max_nvq_pairs = virtio_read_device_config_2(vsc,
    920 		    VIRTIO_NET_CONFIG_MAX_VQ_PAIRS);
    921 
    922 		if (sc->sc_max_nvq_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX)
    923 			goto err;
    924 
    925 		/* Limit the number of queue pairs to use */
    926 		sc->sc_req_nvq_pairs = MIN(sc->sc_max_nvq_pairs, ncpu);
    927 	}
    928 
    929 	vioif_alloc_queues(sc);
    930 	virtio_child_attach_set_vqs(vsc, sc->sc_vqs, sc->sc_req_nvq_pairs);
    931 
    932 #ifdef VIOIF_MPSAFE
    933 	softint_flags = SOFTINT_NET | SOFTINT_MPSAFE;
    934 #else
    935 	softint_flags = SOFTINT_NET;
    936 #endif
    937 
    938 	/*
    939 	 * Allocating virtqueues
    940 	 */
    941 	for (i = 0; i < sc->sc_max_nvq_pairs; i++) {
    942 		rxq = &sc->sc_rxq[i];
    943 		txq = &sc->sc_txq[i];
    944 		char qname[32];
    945 
    946 		rxq->rxq_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NET);
    947 
    948 		rxq->rxq_handle_si = softint_establish(softint_flags,
    949 		    vioif_rx_handle, rxq);
    950 		if (rxq->rxq_handle_si == NULL) {
    951 			aprint_error_dev(self, "cannot establish rx softint\n");
    952 			goto err;
    953 		}
    954 
    955 		snprintf(qname, sizeof(qname), "rx%d", i);
    956 		r = virtio_alloc_vq(vsc, rxq->rxq_vq, nvqs,
    957 		    MCLBYTES + sc->sc_hdr_size, 2, qname);
    958 		if (r != 0)
    959 			goto err;
    960 		nvqs++;
    961 		rxq->rxq_vq->vq_intrhand = vioif_rx_intr;
    962 		rxq->rxq_vq->vq_intrhand_arg = (void *)rxq;
    963 		rxq->rxq_stopping = false;
    964 		rxq->rxq_running_handle = false;
    965 		vioif_work_set(&rxq->rxq_work, vioif_rx_handle, rxq);
    966 
    967 		txq->txq_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NET);
    968 
    969 		txq->txq_deferred_transmit = softint_establish(softint_flags,
    970 		    vioif_deferred_transmit, txq);
    971 		if (txq->txq_deferred_transmit == NULL) {
    972 			aprint_error_dev(self, "cannot establish tx softint\n");
    973 			goto err;
    974 		}
    975 		txq->txq_handle_si = softint_establish(softint_flags,
    976 		    vioif_tx_handle, txq);
    977 		if (txq->txq_handle_si == NULL) {
    978 			aprint_error_dev(self, "cannot establish tx softint\n");
    979 			goto err;
    980 		}
    981 
    982 		snprintf(qname, sizeof(qname), "tx%d", i);
    983 		r = virtio_alloc_vq(vsc, txq->txq_vq, nvqs,
    984 		    sc->sc_hdr_size + (ETHER_MAX_LEN - ETHER_HDR_LEN),
    985 		    VIRTIO_NET_TX_MAXNSEGS + 1, qname);
    986 		if (r != 0)
    987 			goto err;
    988 		nvqs++;
    989 		txq->txq_vq->vq_intrhand = vioif_tx_intr;
    990 		txq->txq_vq->vq_intrhand_arg = (void *)txq;
    991 		txq->txq_link_active = VIOIF_IS_LINK_ACTIVE(sc);
    992 		txq->txq_stopping = false;
    993 		txq->txq_running_handle = false;
    994 		txq->txq_intrq = pcq_create(txq->txq_vq->vq_num, KM_SLEEP);
    995 		vioif_work_set(&txq->txq_work, vioif_tx_handle, txq);
    996 	}
    997 
    998 	if (sc->sc_has_ctrl) {
    999 		/*
   1000 		 * Allocating a virtqueue for control channel
   1001 		 */
   1002 		r = virtio_alloc_vq(vsc, ctrlq->ctrlq_vq, nvqs,
   1003 		    NBPG, 1, "control");
   1004 		if (r != 0) {
   1005 			aprint_error_dev(self, "failed to allocate "
   1006 			    "a virtqueue for control channel, error code %d\n",
   1007 			    r);
   1008 
   1009 			sc->sc_has_ctrl = false;
   1010 			cv_destroy(&ctrlq->ctrlq_wait);
   1011 			mutex_destroy(&ctrlq->ctrlq_wait_lock);
   1012 		} else {
   1013 			nvqs++;
   1014 			ctrlq->ctrlq_vq->vq_intrhand = vioif_ctrl_intr;
   1015 			ctrlq->ctrlq_vq->vq_intrhand_arg = (void *) ctrlq;
   1016 		}
   1017 	}
   1018 
   1019 	sc->sc_ctl_softint = softint_establish(softint_flags,
   1020 	    vioif_ctl_softint, sc);
   1021 	if (sc->sc_ctl_softint == NULL) {
   1022 		aprint_error_dev(self, "cannot establish ctl softint\n");
   1023 		goto err;
   1024 	}
   1025 
   1026 	if (vioif_alloc_mems(sc) < 0)
   1027 		goto err;
   1028 
   1029 	if (virtio_child_attach_finish(vsc) != 0)
   1030 		goto err;
   1031 
   1032 	if (vioif_setup_sysctl(sc) != 0) {
   1033 		aprint_error_dev(self, "unable to create sysctl node\n");
   1034 		/* continue */
   1035 	}
   1036 
   1037 	vioif_setup_stats(sc);
   1038 
   1039 	strlcpy(ifp->if_xname, device_xname(self), IFNAMSIZ);
   1040 	ifp->if_softc = sc;
   1041 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
   1042 #ifdef VIOIF_MPSAFE
   1043 	ifp->if_extflags = IFEF_MPSAFE;
   1044 #endif
   1045 	ifp->if_start = vioif_start;
   1046 	if (sc->sc_req_nvq_pairs > 1)
   1047 		ifp->if_transmit = vioif_transmit;
   1048 	ifp->if_ioctl = vioif_ioctl;
   1049 	ifp->if_init = vioif_init;
   1050 	ifp->if_stop = vioif_stop;
   1051 	ifp->if_capabilities = 0;
   1052 	ifp->if_watchdog = vioif_watchdog;
   1053 	txq = &sc->sc_txq[0];
   1054 	IFQ_SET_MAXLEN(&ifp->if_snd, MAX(txq->txq_vq->vq_num, IFQ_MAXLEN));
   1055 	IFQ_SET_READY(&ifp->if_snd);
   1056 
   1057 	sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
   1058 
   1059 	if_attach(ifp);
   1060 	if_deferred_start_init(ifp, NULL);
   1061 	ether_ifattach(ifp, sc->sc_mac);
   1062 	ether_set_ifflags_cb(&sc->sc_ethercom, vioif_ifflags_cb);
   1063 
   1064 	return;
   1065 
   1066 err:
   1067 	for (i = 0; i < sc->sc_max_nvq_pairs; i++) {
   1068 		rxq = &sc->sc_rxq[i];
   1069 		txq = &sc->sc_txq[i];
   1070 
   1071 		if (rxq->rxq_lock) {
   1072 			mutex_obj_free(rxq->rxq_lock);
   1073 			rxq->rxq_lock = NULL;
   1074 		}
   1075 
   1076 		if (rxq->rxq_handle_si) {
   1077 			softint_disestablish(rxq->rxq_handle_si);
   1078 			rxq->rxq_handle_si = NULL;
   1079 		}
   1080 
   1081 		if (txq->txq_lock) {
   1082 			mutex_obj_free(txq->txq_lock);
   1083 			txq->txq_lock = NULL;
   1084 		}
   1085 
   1086 		if (txq->txq_handle_si) {
   1087 			softint_disestablish(txq->txq_handle_si);
   1088 			txq->txq_handle_si = NULL;
   1089 		}
   1090 
   1091 		if (txq->txq_deferred_transmit) {
   1092 			softint_disestablish(txq->txq_deferred_transmit);
   1093 			txq->txq_deferred_transmit = NULL;
   1094 		}
   1095 
   1096 		if (txq->txq_intrq) {
   1097 			pcq_destroy(txq->txq_intrq);
   1098 			txq->txq_intrq = NULL;
   1099 		}
   1100 	}
   1101 
   1102 	if (sc->sc_has_ctrl) {
   1103 		cv_destroy(&ctrlq->ctrlq_wait);
   1104 		mutex_destroy(&ctrlq->ctrlq_wait_lock);
   1105 	}
   1106 
   1107 	while (nvqs > 0)
   1108 		virtio_free_vq(vsc, &sc->sc_vqs[--nvqs]);
   1109 
   1110 	vioif_free_queues(sc);
   1111 	mutex_destroy(&sc->sc_lock);
   1112 	virtio_child_attach_failed(vsc);
   1113 	config_finalize_register(self, vioif_finalize_teardown);
   1114 
   1115 	return;
   1116 }
   1117 
   1118 static int
   1119 vioif_finalize_teardown(device_t self)
   1120 {
   1121 	struct vioif_softc *sc = device_private(self);
   1122 
   1123 	if (sc->sc_txrx_workqueue != NULL) {
   1124 		vioif_workq_destroy(sc->sc_txrx_workqueue);
   1125 		sc->sc_txrx_workqueue = NULL;
   1126 	}
   1127 
   1128 	return 0;
   1129 }
   1130 
   1131 static void
   1132 vioif_enable_interrupt_vqpairs(struct vioif_softc *sc)
   1133 {
   1134 	struct virtio_softc *vsc = sc->sc_virtio;
   1135 	struct vioif_txqueue *txq;
   1136 	struct vioif_rxqueue *rxq;
   1137 	int i;
   1138 
   1139 	for (i = 0; i < sc->sc_act_nvq_pairs; i++) {
   1140 		txq = &sc->sc_txq[i];
   1141 		rxq = &sc->sc_rxq[i];
   1142 
   1143 		virtio_start_vq_intr(vsc, txq->txq_vq);
   1144 		virtio_start_vq_intr(vsc, rxq->rxq_vq);
   1145 	}
   1146 }
   1147 
   1148 static void
   1149 vioif_disable_interrupt_vqpairs(struct vioif_softc *sc)
   1150 {
   1151 	struct virtio_softc *vsc = sc->sc_virtio;
   1152 	struct vioif_txqueue *txq;
   1153 	struct vioif_rxqueue *rxq;
   1154 	int i;
   1155 
   1156 	for (i = 0; i < sc->sc_act_nvq_pairs; i++) {
   1157 		rxq = &sc->sc_rxq[i];
   1158 		txq = &sc->sc_txq[i];
   1159 
   1160 		virtio_stop_vq_intr(vsc, rxq->rxq_vq);
   1161 		virtio_stop_vq_intr(vsc, txq->txq_vq);
   1162 	}
   1163 }
   1164 
   1165 /*
   1166  * Interface functions for ifnet
   1167  */
   1168 static int
   1169 vioif_init(struct ifnet *ifp)
   1170 {
   1171 	struct vioif_softc *sc = ifp->if_softc;
   1172 	struct virtio_softc *vsc = sc->sc_virtio;
   1173 	struct vioif_rxqueue *rxq;
   1174 	struct vioif_ctrlqueue *ctrlq = &sc->sc_ctrlq;
   1175 	int r, i;
   1176 
   1177 	vioif_stop(ifp, 0);
   1178 
   1179 	r = virtio_reinit_start(vsc);
   1180 	if (r != 0) {
   1181 		log(LOG_ERR, "%s: reset failed\n", ifp->if_xname);
   1182 		return EIO;
   1183 	}
   1184 
   1185 	virtio_negotiate_features(vsc, virtio_features(vsc));
   1186 
   1187 	for (i = 0; i < sc->sc_req_nvq_pairs; i++) {
   1188 		rxq = &sc->sc_rxq[i];
   1189 
   1190 		mutex_enter(rxq->rxq_lock);
   1191 		vioif_populate_rx_mbufs_locked(sc, rxq);
   1192 		mutex_exit(rxq->rxq_lock);
   1193 
   1194 	}
   1195 
   1196 	virtio_reinit_end(vsc);
   1197 
   1198 	if (sc->sc_has_ctrl)
   1199 		virtio_start_vq_intr(vsc, ctrlq->ctrlq_vq);
   1200 
   1201 	r = vioif_ctrl_mq_vq_pairs_set(sc, sc->sc_req_nvq_pairs);
   1202 	if (r == 0)
   1203 		sc->sc_act_nvq_pairs = sc->sc_req_nvq_pairs;
   1204 	else
   1205 		sc->sc_act_nvq_pairs = 1;
   1206 
   1207 	vioif_enable_interrupt_vqpairs(sc);
   1208 
   1209 	vioif_update_link_status(sc);
   1210 	ifp->if_flags |= IFF_RUNNING;
   1211 	ifp->if_flags &= ~IFF_OACTIVE;
   1212 	r = vioif_rx_filter(sc);
   1213 
   1214 	return r;
   1215 }
   1216 
   1217 static void
   1218 vioif_stop(struct ifnet *ifp, int disable)
   1219 {
   1220 	struct vioif_softc *sc = ifp->if_softc;
   1221 	struct virtio_softc *vsc = sc->sc_virtio;
   1222 	struct vioif_txqueue *txq;
   1223 	struct vioif_rxqueue *rxq;
   1224 	struct vioif_ctrlqueue *ctrlq = &sc->sc_ctrlq;
   1225 	int i;
   1226 
   1227 
   1228 	for (i = 0; i < sc->sc_act_nvq_pairs; i++) {
   1229 		txq = &sc->sc_txq[i];
   1230 		rxq = &sc->sc_rxq[i];
   1231 
   1232 		mutex_enter(rxq->rxq_lock);
   1233 		rxq->rxq_stopping = true;
   1234 		mutex_exit(rxq->rxq_lock);
   1235 
   1236 		mutex_enter(txq->txq_lock);
   1237 		txq->txq_stopping = true;
   1238 		mutex_exit(txq->txq_lock);
   1239 	}
   1240 
   1241 	/* disable interrupts */
   1242 	vioif_disable_interrupt_vqpairs(sc);
   1243 	if (sc->sc_has_ctrl)
   1244 		virtio_stop_vq_intr(vsc, ctrlq->ctrlq_vq);
   1245 
   1246 	/*
   1247 	 * only way to stop interrupt, I/O and DMA is resetting...
   1248 	 *
   1249 	 * NOTE: Devices based on VirtIO draft specification can not
   1250 	 * stop interrupt completely even if virtio_stop_vq_intr() is called.
   1251 	 */
   1252 	virtio_reset(vsc);
   1253 
   1254 	vioif_intr_barrier();
   1255 
   1256 	for (i = 0; i < sc->sc_act_nvq_pairs; i++) {
   1257 		txq = &sc->sc_txq[i];
   1258 		rxq = &sc->sc_rxq[i];
   1259 
   1260 		vioif_work_wait(sc->sc_txrx_workqueue, &rxq->rxq_work);
   1261 		vioif_work_wait(sc->sc_txrx_workqueue, &txq->txq_work);
   1262 	}
   1263 
   1264 	for (i = 0; i < sc->sc_act_nvq_pairs; i++) {
   1265 		vioif_rx_queue_clear(&sc->sc_rxq[i]);
   1266 		vioif_tx_queue_clear(&sc->sc_txq[i]);
   1267 	}
   1268 
   1269 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1270 
   1271 	/* all packet processing is stopped */
   1272 	for (i = 0; i < sc->sc_act_nvq_pairs; i++) {
   1273 		txq = &sc->sc_txq[i];
   1274 		rxq = &sc->sc_rxq[i];
   1275 
   1276 		mutex_enter(rxq->rxq_lock);
   1277 		rxq->rxq_stopping = false;
   1278 		KASSERT(!rxq->rxq_running_handle);
   1279 		mutex_exit(rxq->rxq_lock);
   1280 
   1281 		mutex_enter(txq->txq_lock);
   1282 		txq->txq_stopping = false;
   1283 		KASSERT(!txq->txq_running_handle);
   1284 		mutex_exit(txq->txq_lock);
   1285 	}
   1286 
   1287 	for (i = 0; i < sc->sc_act_nvq_pairs; i++) {
   1288 		txq = &sc->sc_txq[i];
   1289 		rxq = &sc->sc_rxq[i];
   1290 
   1291 		if (disable)
   1292 			vioif_rx_drain(rxq);
   1293 
   1294 		vioif_tx_drain(txq);
   1295 	}
   1296 }
   1297 
   1298 static void
   1299 vioif_send_common_locked(struct ifnet *ifp, struct vioif_txqueue *txq,
   1300     bool is_transmit)
   1301 {
   1302 	struct vioif_softc *sc = ifp->if_softc;
   1303 	struct virtio_softc *vsc = sc->sc_virtio;
   1304 	struct virtqueue *vq = txq->txq_vq;
   1305 	struct virtio_net_hdr *hdr;
   1306 	struct mbuf *m;
   1307 	int queued = 0;
   1308 
   1309 	KASSERT(mutex_owned(txq->txq_lock));
   1310 
   1311 	if ((ifp->if_flags & IFF_RUNNING) == 0)
   1312 		return;
   1313 
   1314 	if (!txq->txq_link_active || txq->txq_stopping)
   1315 		return;
   1316 
   1317 	if ((ifp->if_flags & IFF_OACTIVE) != 0 && !is_transmit)
   1318 		return;
   1319 
   1320 	for (;;) {
   1321 		int slot, r;
   1322 
   1323 		if (is_transmit)
   1324 			m = pcq_get(txq->txq_intrq);
   1325 		else
   1326 			IFQ_DEQUEUE(&ifp->if_snd, m);
   1327 
   1328 		if (m == NULL)
   1329 			break;
   1330 
   1331 		r = virtio_enqueue_prep(vsc, vq, &slot);
   1332 		if (r == EAGAIN) {
   1333 			ifp->if_flags |= IFF_OACTIVE;
   1334 			m_freem(m);
   1335 			break;
   1336 		}
   1337 		if (r != 0)
   1338 			panic("enqueue_prep for a tx buffer");
   1339 
   1340 		r = bus_dmamap_load_mbuf(virtio_dmat(vsc),
   1341 		    txq->txq_dmamaps[slot], m, BUS_DMA_WRITE | BUS_DMA_NOWAIT);
   1342 		if (r != 0) {
   1343 			/* maybe just too fragmented */
   1344 			struct mbuf *newm;
   1345 
   1346 			newm = m_defrag(m, M_NOWAIT);
   1347 			if (newm == NULL) {
   1348 				txq->txq_defrag_failed.ev_count++;
   1349 				goto skip;
   1350 			}
   1351 
   1352 			m = newm;
   1353 			r = bus_dmamap_load_mbuf(virtio_dmat(vsc),
   1354 			    txq->txq_dmamaps[slot], m,
   1355 			    BUS_DMA_WRITE | BUS_DMA_NOWAIT);
   1356 			if (r != 0) {
   1357 				txq->txq_mbuf_load_failed.ev_count++;
   1358 skip:
   1359 				m_freem(m);
   1360 				virtio_enqueue_abort(vsc, vq, slot);
   1361 				continue;
   1362 			}
   1363 		}
   1364 
   1365 		/* This should actually never fail */
   1366 		r = virtio_enqueue_reserve(vsc, vq, slot,
   1367 		    txq->txq_dmamaps[slot]->dm_nsegs + 1);
   1368 		if (r != 0) {
   1369 			txq->txq_enqueue_reserve_failed.ev_count++;
   1370 			bus_dmamap_unload(virtio_dmat(vsc),
   1371 			     txq->txq_dmamaps[slot]);
   1372 			/* slot already freed by virtio_enqueue_reserve */
   1373 			m_freem(m);
   1374 			continue;
   1375 		}
   1376 
   1377 		txq->txq_mbufs[slot] = m;
   1378 
   1379 		hdr = &txq->txq_hdrs[slot];
   1380 		memset(hdr, 0, sc->sc_hdr_size);
   1381 		bus_dmamap_sync(virtio_dmat(vsc), txq->txq_dmamaps[slot],
   1382 		    0, txq->txq_dmamaps[slot]->dm_mapsize,
   1383 		    BUS_DMASYNC_PREWRITE);
   1384 		bus_dmamap_sync(virtio_dmat(vsc), txq->txq_hdr_dmamaps[slot],
   1385 		    0, txq->txq_hdr_dmamaps[slot]->dm_mapsize,
   1386 		    BUS_DMASYNC_PREWRITE);
   1387 		virtio_enqueue(vsc, vq, slot, txq->txq_hdr_dmamaps[slot], true);
   1388 		virtio_enqueue(vsc, vq, slot, txq->txq_dmamaps[slot], true);
   1389 		virtio_enqueue_commit(vsc, vq, slot, false);
   1390 
   1391 		queued++;
   1392 		bpf_mtap(ifp, m, BPF_D_OUT);
   1393 	}
   1394 
   1395 	if (queued > 0) {
   1396 		virtio_enqueue_commit(vsc, vq, -1, true);
   1397 		ifp->if_timer = 5;
   1398 	}
   1399 }
   1400 
   1401 static void
   1402 vioif_start_locked(struct ifnet *ifp, struct vioif_txqueue *txq)
   1403 {
   1404 
   1405 	/*
   1406 	 * ifp->if_obytes and ifp->if_omcasts are added in if_transmit()@if.c.
   1407 	 */
   1408 	vioif_send_common_locked(ifp, txq, false);
   1409 
   1410 }
   1411 
   1412 static void
   1413 vioif_start(struct ifnet *ifp)
   1414 {
   1415 	struct vioif_softc *sc = ifp->if_softc;
   1416 	struct vioif_txqueue *txq = &sc->sc_txq[0];
   1417 
   1418 #ifdef VIOIF_MPSAFE
   1419 	KASSERT(if_is_mpsafe(ifp));
   1420 #endif
   1421 
   1422 	mutex_enter(txq->txq_lock);
   1423 	vioif_start_locked(ifp, txq);
   1424 	mutex_exit(txq->txq_lock);
   1425 }
   1426 
   1427 static inline int
   1428 vioif_select_txqueue(struct ifnet *ifp, struct mbuf *m)
   1429 {
   1430 	struct vioif_softc *sc = ifp->if_softc;
   1431 	u_int cpuid = cpu_index(curcpu());
   1432 
   1433 	return cpuid % sc->sc_act_nvq_pairs;
   1434 }
   1435 
   1436 static void
   1437 vioif_transmit_locked(struct ifnet *ifp, struct vioif_txqueue *txq)
   1438 {
   1439 
   1440 	vioif_send_common_locked(ifp, txq, true);
   1441 }
   1442 
   1443 static int
   1444 vioif_transmit(struct ifnet *ifp, struct mbuf *m)
   1445 {
   1446 	struct vioif_softc *sc = ifp->if_softc;
   1447 	struct vioif_txqueue *txq;
   1448 	int qid;
   1449 
   1450 	qid = vioif_select_txqueue(ifp, m);
   1451 	txq = &sc->sc_txq[qid];
   1452 
   1453 	if (__predict_false(!pcq_put(txq->txq_intrq, m))) {
   1454 		m_freem(m);
   1455 		return ENOBUFS;
   1456 	}
   1457 
   1458 	net_stat_ref_t nsr = IF_STAT_GETREF(ifp);
   1459 	if_statadd_ref(nsr, if_obytes, m->m_pkthdr.len);
   1460 	if (m->m_flags & M_MCAST)
   1461 		if_statinc_ref(nsr, if_omcasts);
   1462 	IF_STAT_PUTREF(ifp);
   1463 
   1464 	if (mutex_tryenter(txq->txq_lock)) {
   1465 		vioif_transmit_locked(ifp, txq);
   1466 		mutex_exit(txq->txq_lock);
   1467 	}
   1468 
   1469 	return 0;
   1470 }
   1471 
   1472 static void
   1473 vioif_deferred_transmit(void *arg)
   1474 {
   1475 	struct vioif_txqueue *txq = arg;
   1476 	struct virtio_softc *vsc = txq->txq_vq->vq_owner;
   1477 	struct vioif_softc *sc = device_private(virtio_child(vsc));
   1478 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1479 
   1480 	mutex_enter(txq->txq_lock);
   1481 	vioif_send_common_locked(ifp, txq, true);
   1482 	mutex_exit(txq->txq_lock);
   1483 }
   1484 
   1485 static int
   1486 vioif_ioctl(struct ifnet *ifp, u_long cmd, void *data)
   1487 {
   1488 	int s, r;
   1489 
   1490 	s = splnet();
   1491 
   1492 	r = ether_ioctl(ifp, cmd, data);
   1493 	if (r == ENETRESET && (cmd == SIOCADDMULTI || cmd == SIOCDELMULTI)) {
   1494 		if (ifp->if_flags & IFF_RUNNING) {
   1495 			r = vioif_rx_filter(ifp->if_softc);
   1496 		} else {
   1497 			r = 0;
   1498 		}
   1499 	}
   1500 
   1501 	splx(s);
   1502 
   1503 	return r;
   1504 }
   1505 
   1506 void
   1507 vioif_watchdog(struct ifnet *ifp)
   1508 {
   1509 	struct vioif_softc *sc = ifp->if_softc;
   1510 	int i;
   1511 
   1512 	if (ifp->if_flags & IFF_RUNNING) {
   1513 		for (i = 0; i < sc->sc_act_nvq_pairs; i++) {
   1514 			vioif_tx_queue_clear(&sc->sc_txq[i]);
   1515 		}
   1516 	}
   1517 }
   1518 
   1519 /*
   1520  * Receive implementation
   1521  */
   1522 /* allocate and initialize a mbuf for receive */
   1523 static int
   1524 vioif_add_rx_mbuf(struct vioif_rxqueue *rxq, int i)
   1525 {
   1526 	struct virtio_softc *vsc = rxq->rxq_vq->vq_owner;
   1527 	struct mbuf *m;
   1528 	int r;
   1529 
   1530 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1531 	if (m == NULL)
   1532 		return ENOBUFS;
   1533 	MCLGET(m, M_DONTWAIT);
   1534 	if ((m->m_flags & M_EXT) == 0) {
   1535 		m_freem(m);
   1536 		return ENOBUFS;
   1537 	}
   1538 	rxq->rxq_mbufs[i] = m;
   1539 	m->m_len = m->m_pkthdr.len = m->m_ext.ext_size;
   1540 	r = bus_dmamap_load_mbuf(virtio_dmat(vsc),
   1541 	    rxq->rxq_dmamaps[i], m, BUS_DMA_READ | BUS_DMA_NOWAIT);
   1542 	if (r) {
   1543 		m_freem(m);
   1544 		rxq->rxq_mbufs[i] = NULL;
   1545 		return r;
   1546 	}
   1547 
   1548 	return 0;
   1549 }
   1550 
   1551 /* free a mbuf for receive */
   1552 static void
   1553 vioif_free_rx_mbuf(struct vioif_rxqueue *rxq, int i)
   1554 {
   1555 	struct virtio_softc *vsc = rxq->rxq_vq->vq_owner;
   1556 
   1557 	bus_dmamap_unload(virtio_dmat(vsc), rxq->rxq_dmamaps[i]);
   1558 	m_freem(rxq->rxq_mbufs[i]);
   1559 	rxq->rxq_mbufs[i] = NULL;
   1560 }
   1561 
   1562 /* add mbufs for all the empty receive slots */
   1563 static void
   1564 vioif_populate_rx_mbufs_locked(struct vioif_softc *sc, struct vioif_rxqueue *rxq)
   1565 {
   1566 	struct virtqueue *vq = rxq->rxq_vq;
   1567 	struct virtio_softc *vsc = vq->vq_owner;
   1568 	int i, r, ndone = 0;
   1569 
   1570 	KASSERT(mutex_owned(rxq->rxq_lock));
   1571 
   1572 	if (rxq->rxq_stopping)
   1573 		return;
   1574 
   1575 	for (i = 0; i < vq->vq_num; i++) {
   1576 		int slot;
   1577 		r = virtio_enqueue_prep(vsc, vq, &slot);
   1578 		if (r == EAGAIN)
   1579 			break;
   1580 		if (r != 0)
   1581 			panic("enqueue_prep for rx buffers");
   1582 		if (rxq->rxq_mbufs[slot] == NULL) {
   1583 			r = vioif_add_rx_mbuf(rxq, slot);
   1584 			if (r != 0) {
   1585 				rxq->rxq_mbuf_add_failed.ev_count++;
   1586 				break;
   1587 			}
   1588 		}
   1589 		r = virtio_enqueue_reserve(vsc, vq, slot,
   1590 		    rxq->rxq_dmamaps[slot]->dm_nsegs + 1);
   1591 		if (r != 0) {
   1592 			vioif_free_rx_mbuf(rxq, slot);
   1593 			break;
   1594 		}
   1595 		bus_dmamap_sync(virtio_dmat(vsc), rxq->rxq_hdr_dmamaps[slot],
   1596 		    0, sc->sc_hdr_size, BUS_DMASYNC_PREREAD);
   1597 		bus_dmamap_sync(virtio_dmat(vsc), rxq->rxq_dmamaps[slot],
   1598 		    0, MCLBYTES, BUS_DMASYNC_PREREAD);
   1599 		virtio_enqueue(vsc, vq, slot, rxq->rxq_hdr_dmamaps[slot],
   1600 		    false);
   1601 		virtio_enqueue(vsc, vq, slot, rxq->rxq_dmamaps[slot], false);
   1602 		virtio_enqueue_commit(vsc, vq, slot, false);
   1603 		ndone++;
   1604 	}
   1605 	if (ndone > 0)
   1606 		virtio_enqueue_commit(vsc, vq, -1, true);
   1607 }
   1608 
   1609 static void
   1610 vioif_rx_queue_clear(struct vioif_rxqueue *rxq)
   1611 {
   1612 	struct virtqueue *vq = rxq->rxq_vq;
   1613 	struct virtio_softc *vsc = vq->vq_owner;
   1614 	struct vioif_softc *sc = device_private(virtio_child(vsc));
   1615 	u_int limit = UINT_MAX;
   1616 	bool more;
   1617 
   1618 	KASSERT(rxq->rxq_stopping);
   1619 
   1620 	mutex_enter(rxq->rxq_lock);
   1621 	for (;;) {
   1622 		more = vioif_rx_deq_locked(sc, vsc, rxq, limit);
   1623 		if (more == false)
   1624 			break;
   1625 	}
   1626 	mutex_exit(rxq->rxq_lock);
   1627 }
   1628 
   1629 /* dequeue received packets */
   1630 static bool
   1631 vioif_rx_deq_locked(struct vioif_softc *sc, struct virtio_softc *vsc,
   1632     struct vioif_rxqueue *rxq, u_int limit)
   1633 {
   1634 	struct virtqueue *vq = rxq->rxq_vq;
   1635 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1636 	struct mbuf *m;
   1637 	int slot, len;
   1638 	bool more = false, dequeued = false;
   1639 
   1640 	KASSERT(mutex_owned(rxq->rxq_lock));
   1641 
   1642 	if (virtio_vq_is_enqueued(vsc, vq) == false)
   1643 		return false;
   1644 
   1645 	for (;;) {
   1646 		if (limit-- == 0) {
   1647 			more = true;
   1648 			break;
   1649 		}
   1650 
   1651 		if (virtio_dequeue(vsc, vq, &slot, &len) != 0)
   1652 			break;
   1653 
   1654 		dequeued = true;
   1655 
   1656 		len -= sc->sc_hdr_size;
   1657 		bus_dmamap_sync(virtio_dmat(vsc), rxq->rxq_hdr_dmamaps[slot],
   1658 		    0, sc->sc_hdr_size, BUS_DMASYNC_POSTREAD);
   1659 		bus_dmamap_sync(virtio_dmat(vsc), rxq->rxq_dmamaps[slot],
   1660 		    0, MCLBYTES, BUS_DMASYNC_POSTREAD);
   1661 		m = rxq->rxq_mbufs[slot];
   1662 		KASSERT(m != NULL);
   1663 		bus_dmamap_unload(virtio_dmat(vsc), rxq->rxq_dmamaps[slot]);
   1664 		rxq->rxq_mbufs[slot] = NULL;
   1665 		virtio_dequeue_commit(vsc, vq, slot);
   1666 		m_set_rcvif(m, ifp);
   1667 		m->m_len = m->m_pkthdr.len = len;
   1668 
   1669 		if_percpuq_enqueue(ifp->if_percpuq, m);
   1670 	}
   1671 
   1672 	if (dequeued)
   1673 		vioif_populate_rx_mbufs_locked(sc, rxq);
   1674 
   1675 	return more;
   1676 }
   1677 
   1678 /* rx interrupt; call _dequeue above and schedule a softint */
   1679 
   1680 static void
   1681 vioif_rx_handle_locked(void *xrxq, u_int limit)
   1682 {
   1683 	struct vioif_rxqueue *rxq = xrxq;
   1684 	struct virtqueue *vq = rxq->rxq_vq;
   1685 	struct virtio_softc *vsc = vq->vq_owner;
   1686 	struct vioif_softc *sc = device_private(virtio_child(vsc));
   1687 	bool more;
   1688 	int enqueued;
   1689 
   1690 	KASSERT(mutex_owned(rxq->rxq_lock));
   1691 	KASSERT(!rxq->rxq_stopping);
   1692 
   1693 	more = vioif_rx_deq_locked(sc, vsc, rxq, limit);
   1694 	if (more) {
   1695 		vioif_rx_sched_handle(sc, rxq);
   1696 		return;
   1697 	}
   1698 
   1699 	enqueued = virtio_start_vq_intr(vsc, rxq->rxq_vq);
   1700 	if (enqueued != 0) {
   1701 		virtio_stop_vq_intr(vsc, rxq->rxq_vq);
   1702 		vioif_rx_sched_handle(sc, rxq);
   1703 		return;
   1704 	}
   1705 
   1706 	rxq->rxq_running_handle = false;
   1707 }
   1708 
   1709 static int
   1710 vioif_rx_intr(void *arg)
   1711 {
   1712 	struct vioif_rxqueue *rxq = arg;
   1713 	struct virtqueue *vq = rxq->rxq_vq;
   1714 	struct virtio_softc *vsc = vq->vq_owner;
   1715 	struct vioif_softc *sc = device_private(virtio_child(vsc));
   1716 	u_int limit;
   1717 
   1718 
   1719 	mutex_enter(rxq->rxq_lock);
   1720 
   1721 	/* rx handler is already running in softint/workqueue */
   1722 	if (rxq->rxq_running_handle)
   1723 		goto done;
   1724 
   1725 	if (rxq->rxq_stopping)
   1726 		goto done;
   1727 
   1728 	rxq->rxq_running_handle = true;
   1729 
   1730 	limit = sc->sc_rx_intr_process_limit;
   1731 	virtio_stop_vq_intr(vsc, vq);
   1732 	vioif_rx_handle_locked(rxq, limit);
   1733 
   1734 done:
   1735 	mutex_exit(rxq->rxq_lock);
   1736 	return 1;
   1737 }
   1738 
   1739 static void
   1740 vioif_rx_handle(void *xrxq)
   1741 {
   1742 	struct vioif_rxqueue *rxq = xrxq;
   1743 	struct virtqueue *vq = rxq->rxq_vq;
   1744 	struct virtio_softc *vsc = vq->vq_owner;
   1745 	struct vioif_softc *sc = device_private(virtio_child(vsc));
   1746 	u_int limit;
   1747 
   1748 	mutex_enter(rxq->rxq_lock);
   1749 
   1750 	KASSERT(rxq->rxq_running_handle);
   1751 
   1752 	if (rxq->rxq_stopping) {
   1753 		rxq->rxq_running_handle = false;
   1754 		goto done;
   1755 	}
   1756 
   1757 	limit = sc->sc_rx_process_limit;
   1758 	vioif_rx_handle_locked(rxq, limit);
   1759 
   1760 done:
   1761 	mutex_exit(rxq->rxq_lock);
   1762 }
   1763 
   1764 static void
   1765 vioif_rx_sched_handle(struct vioif_softc *sc, struct vioif_rxqueue *rxq)
   1766 {
   1767 
   1768 	KASSERT(mutex_owned(rxq->rxq_lock));
   1769 
   1770 	if (rxq->rxq_stopping)
   1771 		return;
   1772 
   1773 	if (rxq->rxq_workqueue)
   1774 		vioif_work_add(sc->sc_txrx_workqueue, &rxq->rxq_work);
   1775 	else
   1776 		softint_schedule(rxq->rxq_handle_si);
   1777 }
   1778 
   1779 /* free all the mbufs; called from if_stop(disable) */
   1780 static void
   1781 vioif_rx_drain(struct vioif_rxqueue *rxq)
   1782 {
   1783 	struct virtqueue *vq = rxq->rxq_vq;
   1784 	int i;
   1785 
   1786 	for (i = 0; i < vq->vq_num; i++) {
   1787 		if (rxq->rxq_mbufs[i] == NULL)
   1788 			continue;
   1789 		vioif_free_rx_mbuf(rxq, i);
   1790 	}
   1791 }
   1792 
   1793 /*
   1794  * Transmition implementation
   1795  */
   1796 /* actual transmission is done in if_start */
   1797 /* tx interrupt; dequeue and free mbufs */
   1798 /*
   1799  * tx interrupt is actually disabled; this should be called upon
   1800  * tx vq full and watchdog
   1801  */
   1802 
   1803 static void
   1804 vioif_tx_handle_locked(struct vioif_txqueue *txq, u_int limit)
   1805 {
   1806 	struct virtqueue *vq = txq->txq_vq;
   1807 	struct virtio_softc *vsc = vq->vq_owner;
   1808 	struct vioif_softc *sc = device_private(virtio_child(vsc));
   1809 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1810 	bool more;
   1811 	int enqueued;
   1812 
   1813 	KASSERT(mutex_owned(txq->txq_lock));
   1814 	KASSERT(!txq->txq_stopping);
   1815 
   1816 	more = vioif_tx_deq_locked(sc, vsc, txq, limit);
   1817 	if (more) {
   1818 		vioif_tx_sched_handle(sc, txq);
   1819 		return;
   1820 	}
   1821 
   1822 	enqueued = (virtio_features(vsc) & VIRTIO_F_RING_EVENT_IDX) ?
   1823 	    virtio_postpone_intr_smart(vsc, vq):
   1824 	    virtio_start_vq_intr(vsc, vq);
   1825 	if (enqueued != 0) {
   1826 		virtio_stop_vq_intr(vsc, vq);
   1827 		vioif_tx_sched_handle(sc, txq);
   1828 		return;
   1829 	}
   1830 
   1831 	txq->txq_running_handle = false;
   1832 
   1833 	/* for ALTQ */
   1834 	if (txq == &sc->sc_txq[0]) {
   1835 		if_schedule_deferred_start(ifp);
   1836 		ifp->if_flags &= ~IFF_OACTIVE;
   1837 	}
   1838 	softint_schedule(txq->txq_deferred_transmit);
   1839 }
   1840 
   1841 
   1842 static int
   1843 vioif_tx_intr(void *arg)
   1844 {
   1845 	struct vioif_txqueue *txq = arg;
   1846 	struct virtqueue *vq = txq->txq_vq;
   1847 	struct virtio_softc *vsc = vq->vq_owner;
   1848 	struct vioif_softc *sc = device_private(virtio_child(vsc));
   1849 	u_int limit;
   1850 
   1851 	limit = sc->sc_tx_intr_process_limit;
   1852 
   1853 	mutex_enter(txq->txq_lock);
   1854 
   1855 	/* tx handler is already running in softint/workqueue */
   1856 	if (txq->txq_running_handle)
   1857 		goto done;
   1858 
   1859 	if (txq->txq_stopping)
   1860 		goto done;
   1861 
   1862 	txq->txq_running_handle = true;
   1863 
   1864 	virtio_stop_vq_intr(vsc, vq);
   1865 	txq->txq_workqueue = sc->sc_txrx_workqueue_sysctl;
   1866 	vioif_tx_handle_locked(txq, limit);
   1867 
   1868 done:
   1869 	mutex_exit(txq->txq_lock);
   1870 	return 1;
   1871 }
   1872 
   1873 static void
   1874 vioif_tx_handle(void *xtxq)
   1875 {
   1876 	struct vioif_txqueue *txq = xtxq;
   1877 	struct virtqueue *vq = txq->txq_vq;
   1878 	struct virtio_softc *vsc = vq->vq_owner;
   1879 	struct vioif_softc *sc = device_private(virtio_child(vsc));
   1880 	u_int limit;
   1881 
   1882 	mutex_enter(txq->txq_lock);
   1883 
   1884 	KASSERT(txq->txq_running_handle);
   1885 
   1886 	if (txq->txq_stopping) {
   1887 		txq->txq_running_handle = false;
   1888 		goto done;
   1889 	}
   1890 
   1891 	limit = sc->sc_tx_process_limit;
   1892 	vioif_tx_handle_locked(txq, limit);
   1893 
   1894 done:
   1895 	mutex_exit(txq->txq_lock);
   1896 }
   1897 
   1898 static void
   1899 vioif_tx_sched_handle(struct vioif_softc *sc, struct vioif_txqueue *txq)
   1900 {
   1901 
   1902 	KASSERT(mutex_owned(txq->txq_lock));
   1903 
   1904 	if (txq->txq_stopping)
   1905 		return;
   1906 
   1907 	if (txq->txq_workqueue)
   1908 		vioif_work_add(sc->sc_txrx_workqueue, &txq->txq_work);
   1909 	else
   1910 		softint_schedule(txq->txq_handle_si);
   1911 }
   1912 
   1913 static void
   1914 vioif_tx_queue_clear(struct vioif_txqueue *txq)
   1915 {
   1916 	struct virtqueue *vq = txq->txq_vq;
   1917 	struct virtio_softc *vsc = vq->vq_owner;
   1918 	struct vioif_softc *sc = device_private(virtio_child(vsc));
   1919 	u_int limit = UINT_MAX;
   1920 	bool more;
   1921 
   1922 	mutex_enter(txq->txq_lock);
   1923 	for (;;) {
   1924 		more = vioif_tx_deq_locked(sc, vsc, txq, limit);
   1925 		if (more == false)
   1926 			break;
   1927 	}
   1928 	mutex_exit(txq->txq_lock);
   1929 }
   1930 
   1931 static bool
   1932 vioif_tx_deq_locked(struct vioif_softc *sc, struct virtio_softc *vsc,
   1933     struct vioif_txqueue *txq, u_int limit)
   1934 {
   1935 	struct virtqueue *vq = txq->txq_vq;
   1936 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1937 	struct mbuf *m;
   1938 	int slot, len;
   1939 	bool more = false;
   1940 
   1941 	KASSERT(mutex_owned(txq->txq_lock));
   1942 
   1943 	if (virtio_vq_is_enqueued(vsc, vq) == false)
   1944 		return false;
   1945 
   1946 	for (;;) {
   1947 		if (limit-- == 0) {
   1948 			more = true;
   1949 			break;
   1950 		}
   1951 
   1952 		if (virtio_dequeue(vsc, vq, &slot, &len) != 0)
   1953 			break;
   1954 
   1955 		bus_dmamap_sync(virtio_dmat(vsc), txq->txq_hdr_dmamaps[slot],
   1956 		    0, sc->sc_hdr_size, BUS_DMASYNC_POSTWRITE);
   1957 		bus_dmamap_sync(virtio_dmat(vsc), txq->txq_dmamaps[slot],
   1958 		    0, txq->txq_dmamaps[slot]->dm_mapsize,
   1959 		    BUS_DMASYNC_POSTWRITE);
   1960 		m = txq->txq_mbufs[slot];
   1961 		bus_dmamap_unload(virtio_dmat(vsc), txq->txq_dmamaps[slot]);
   1962 		txq->txq_mbufs[slot] = NULL;
   1963 		virtio_dequeue_commit(vsc, vq, slot);
   1964 		if_statinc(ifp, if_opackets);
   1965 		m_freem(m);
   1966 	}
   1967 
   1968 	return more;
   1969 }
   1970 
   1971 /* free all the mbufs already put on vq; called from if_stop(disable) */
   1972 static void
   1973 vioif_tx_drain(struct vioif_txqueue *txq)
   1974 {
   1975 	struct virtqueue *vq = txq->txq_vq;
   1976 	struct virtio_softc *vsc = vq->vq_owner;
   1977 	int i;
   1978 
   1979 	for (i = 0; i < vq->vq_num; i++) {
   1980 		if (txq->txq_mbufs[i] == NULL)
   1981 			continue;
   1982 		bus_dmamap_unload(virtio_dmat(vsc), txq->txq_dmamaps[i]);
   1983 		m_freem(txq->txq_mbufs[i]);
   1984 		txq->txq_mbufs[i] = NULL;
   1985 	}
   1986 }
   1987 
   1988 /*
   1989  * Control vq
   1990  */
   1991 /* issue a VIRTIO_NET_CTRL_RX class command and wait for completion */
   1992 static void
   1993 vioif_ctrl_acquire(struct vioif_softc *sc)
   1994 {
   1995 	struct vioif_ctrlqueue *ctrlq = &sc->sc_ctrlq;
   1996 
   1997 	mutex_enter(&ctrlq->ctrlq_wait_lock);
   1998 	while (ctrlq->ctrlq_inuse != FREE)
   1999 		cv_wait(&ctrlq->ctrlq_wait, &ctrlq->ctrlq_wait_lock);
   2000 	ctrlq->ctrlq_inuse = INUSE;
   2001 	ctrlq->ctrlq_owner = curlwp;
   2002 	mutex_exit(&ctrlq->ctrlq_wait_lock);
   2003 }
   2004 
   2005 static void
   2006 vioif_ctrl_release(struct vioif_softc *sc)
   2007 {
   2008 	struct vioif_ctrlqueue *ctrlq = &sc->sc_ctrlq;
   2009 
   2010 	KASSERT(ctrlq->ctrlq_inuse != FREE);
   2011 	KASSERT(ctrlq->ctrlq_owner == curlwp);
   2012 
   2013 	mutex_enter(&ctrlq->ctrlq_wait_lock);
   2014 	ctrlq->ctrlq_inuse = FREE;
   2015 	ctrlq->ctrlq_owner = NULL;
   2016 	cv_signal(&ctrlq->ctrlq_wait);
   2017 	mutex_exit(&ctrlq->ctrlq_wait_lock);
   2018 }
   2019 
   2020 static int
   2021 vioif_ctrl_load_cmdspec(struct vioif_softc *sc,
   2022     struct vioif_ctrl_cmdspec *specs, int nspecs)
   2023 {
   2024 	struct virtio_softc *vsc = sc->sc_virtio;
   2025 	int i, r, loaded;
   2026 
   2027 	loaded = 0;
   2028 	for (i = 0; i < nspecs; i++) {
   2029 		r = bus_dmamap_load(virtio_dmat(vsc),
   2030 		    specs[i].dmamap, specs[i].buf, specs[i].bufsize,
   2031 		    NULL, BUS_DMA_WRITE | BUS_DMA_NOWAIT);
   2032 		if (r) {
   2033 			sc->sc_ctrlq.ctrlq_cmd_load_failed.ev_count++;
   2034 			goto err;
   2035 		}
   2036 		loaded++;
   2037 
   2038 	}
   2039 
   2040 	return r;
   2041 
   2042 err:
   2043 	for (i = 0; i < loaded; i++) {
   2044 		bus_dmamap_unload(virtio_dmat(vsc), specs[i].dmamap);
   2045 	}
   2046 
   2047 	return r;
   2048 }
   2049 
   2050 static void
   2051 vioif_ctrl_unload_cmdspec(struct vioif_softc *sc,
   2052     struct vioif_ctrl_cmdspec *specs, int nspecs)
   2053 {
   2054 	struct virtio_softc *vsc = sc->sc_virtio;
   2055 	int i;
   2056 
   2057 	for (i = 0; i < nspecs; i++) {
   2058 		bus_dmamap_unload(virtio_dmat(vsc), specs[i].dmamap);
   2059 	}
   2060 }
   2061 
   2062 static int
   2063 vioif_ctrl_send_command(struct vioif_softc *sc, uint8_t class, uint8_t cmd,
   2064     struct vioif_ctrl_cmdspec *specs, int nspecs)
   2065 {
   2066 	struct vioif_ctrlqueue *ctrlq = &sc->sc_ctrlq;
   2067 	struct virtqueue *vq = ctrlq->ctrlq_vq;
   2068 	struct virtio_softc *vsc = sc->sc_virtio;
   2069 	int i, r, slot;
   2070 
   2071 	ctrlq->ctrlq_cmd->class = class;
   2072 	ctrlq->ctrlq_cmd->command = cmd;
   2073 
   2074 	bus_dmamap_sync(virtio_dmat(vsc), ctrlq->ctrlq_cmd_dmamap,
   2075 	    0, sizeof(struct virtio_net_ctrl_cmd), BUS_DMASYNC_PREWRITE);
   2076 	for (i = 0; i < nspecs; i++) {
   2077 		bus_dmamap_sync(virtio_dmat(vsc), specs[i].dmamap,
   2078 		    0, specs[i].bufsize, BUS_DMASYNC_PREWRITE);
   2079 	}
   2080 	bus_dmamap_sync(virtio_dmat(vsc), ctrlq->ctrlq_status_dmamap,
   2081 	    0, sizeof(struct virtio_net_ctrl_status), BUS_DMASYNC_PREREAD);
   2082 
   2083 	/* we need to explicitly (re)start vq intr when using RING EVENT IDX */
   2084 	if (virtio_features(vsc) & VIRTIO_F_RING_EVENT_IDX)
   2085 		virtio_start_vq_intr(vsc, ctrlq->ctrlq_vq);
   2086 
   2087 	r = virtio_enqueue_prep(vsc, vq, &slot);
   2088 	if (r != 0)
   2089 		panic("%s: control vq busy!?", device_xname(sc->sc_dev));
   2090 	r = virtio_enqueue_reserve(vsc, vq, slot, nspecs + 2);
   2091 	if (r != 0)
   2092 		panic("%s: control vq busy!?", device_xname(sc->sc_dev));
   2093 	virtio_enqueue(vsc, vq, slot, ctrlq->ctrlq_cmd_dmamap, true);
   2094 	for (i = 0; i < nspecs; i++) {
   2095 		virtio_enqueue(vsc, vq, slot, specs[i].dmamap, true);
   2096 	}
   2097 	virtio_enqueue(vsc, vq, slot, ctrlq->ctrlq_status_dmamap, false);
   2098 	virtio_enqueue_commit(vsc, vq, slot, true);
   2099 
   2100 	/* wait for done */
   2101 	mutex_enter(&ctrlq->ctrlq_wait_lock);
   2102 	while (ctrlq->ctrlq_inuse != DONE)
   2103 		cv_wait(&ctrlq->ctrlq_wait, &ctrlq->ctrlq_wait_lock);
   2104 	mutex_exit(&ctrlq->ctrlq_wait_lock);
   2105 	/* already dequeueued */
   2106 
   2107 	bus_dmamap_sync(virtio_dmat(vsc), ctrlq->ctrlq_cmd_dmamap, 0,
   2108 	    sizeof(struct virtio_net_ctrl_cmd), BUS_DMASYNC_POSTWRITE);
   2109 	for (i = 0; i < nspecs; i++) {
   2110 		bus_dmamap_sync(virtio_dmat(vsc), specs[i].dmamap, 0,
   2111 		    specs[i].bufsize, BUS_DMASYNC_POSTWRITE);
   2112 	}
   2113 	bus_dmamap_sync(virtio_dmat(vsc), ctrlq->ctrlq_status_dmamap, 0,
   2114 	    sizeof(struct virtio_net_ctrl_status), BUS_DMASYNC_POSTREAD);
   2115 
   2116 	if (ctrlq->ctrlq_status->ack == VIRTIO_NET_OK)
   2117 		r = 0;
   2118 	else {
   2119 		device_printf(sc->sc_dev, "failed setting rx mode\n");
   2120 		sc->sc_ctrlq.ctrlq_cmd_failed.ev_count++;
   2121 		r = EIO;
   2122 	}
   2123 
   2124 	return r;
   2125 }
   2126 
   2127 static int
   2128 vioif_ctrl_rx(struct vioif_softc *sc, int cmd, bool onoff)
   2129 {
   2130 	struct virtio_net_ctrl_rx *rx = sc->sc_ctrlq.ctrlq_rx;
   2131 	struct vioif_ctrl_cmdspec specs[1];
   2132 	int r;
   2133 
   2134 	if (!sc->sc_has_ctrl)
   2135 		return ENOTSUP;
   2136 
   2137 	vioif_ctrl_acquire(sc);
   2138 
   2139 	rx->onoff = onoff;
   2140 	specs[0].dmamap = sc->sc_ctrlq.ctrlq_rx_dmamap;
   2141 	specs[0].buf = rx;
   2142 	specs[0].bufsize = sizeof(*rx);
   2143 
   2144 	r = vioif_ctrl_send_command(sc, VIRTIO_NET_CTRL_RX, cmd,
   2145 	    specs, __arraycount(specs));
   2146 
   2147 	vioif_ctrl_release(sc);
   2148 	return r;
   2149 }
   2150 
   2151 static int
   2152 vioif_set_promisc(struct vioif_softc *sc, bool onoff)
   2153 {
   2154 	return vioif_ctrl_rx(sc, VIRTIO_NET_CTRL_RX_PROMISC, onoff);
   2155 }
   2156 
   2157 static int
   2158 vioif_set_allmulti(struct vioif_softc *sc, bool onoff)
   2159 {
   2160 	return vioif_ctrl_rx(sc, VIRTIO_NET_CTRL_RX_ALLMULTI, onoff);
   2161 }
   2162 
   2163 /* issue VIRTIO_NET_CTRL_MAC_TABLE_SET command and wait for completion */
   2164 static int
   2165 vioif_set_rx_filter(struct vioif_softc *sc)
   2166 {
   2167 	/* filter already set in ctrlq->ctrlq_mac_tbl */
   2168 	struct virtio_softc *vsc = sc->sc_virtio;
   2169 	struct virtio_net_ctrl_mac_tbl *mac_tbl_uc, *mac_tbl_mc;
   2170 	struct vioif_ctrl_cmdspec specs[2];
   2171 	int nspecs = __arraycount(specs);
   2172 	int r;
   2173 
   2174 	mac_tbl_uc = sc->sc_ctrlq.ctrlq_mac_tbl_uc;
   2175 	mac_tbl_mc = sc->sc_ctrlq.ctrlq_mac_tbl_mc;
   2176 
   2177 	if (!sc->sc_has_ctrl)
   2178 		return ENOTSUP;
   2179 
   2180 	vioif_ctrl_acquire(sc);
   2181 
   2182 	specs[0].dmamap = sc->sc_ctrlq.ctrlq_tbl_uc_dmamap;
   2183 	specs[0].buf = mac_tbl_uc;
   2184 	specs[0].bufsize = sizeof(*mac_tbl_uc)
   2185 	    + (ETHER_ADDR_LEN * virtio_rw32(vsc, mac_tbl_uc->nentries));
   2186 
   2187 	specs[1].dmamap = sc->sc_ctrlq.ctrlq_tbl_mc_dmamap;
   2188 	specs[1].buf = mac_tbl_mc;
   2189 	specs[1].bufsize = sizeof(*mac_tbl_mc)
   2190 	    + (ETHER_ADDR_LEN * virtio_rw32(vsc, mac_tbl_mc->nentries));
   2191 
   2192 	r = vioif_ctrl_load_cmdspec(sc, specs, nspecs);
   2193 	if (r != 0)
   2194 		goto out;
   2195 
   2196 	r = vioif_ctrl_send_command(sc,
   2197 	    VIRTIO_NET_CTRL_MAC, VIRTIO_NET_CTRL_MAC_TABLE_SET,
   2198 	    specs, nspecs);
   2199 
   2200 	vioif_ctrl_unload_cmdspec(sc, specs, nspecs);
   2201 
   2202 out:
   2203 	vioif_ctrl_release(sc);
   2204 
   2205 	return r;
   2206 }
   2207 
   2208 static int
   2209 vioif_set_mac_addr(struct vioif_softc *sc)
   2210 {
   2211 	struct virtio_net_ctrl_mac_addr *ma =
   2212 	    sc->sc_ctrlq.ctrlq_mac_addr;
   2213 	struct vioif_ctrl_cmdspec specs[1];
   2214 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   2215 	int nspecs = __arraycount(specs);
   2216 	uint64_t features;
   2217 	int r;
   2218 	size_t i;
   2219 
   2220 	if (!sc->sc_has_ctrl)
   2221 		return ENOTSUP;
   2222 
   2223 	if (memcmp(CLLADDR(ifp->if_sadl), sc->sc_mac,
   2224 	    ETHER_ADDR_LEN) == 0) {
   2225 		return 0;
   2226 	}
   2227 
   2228 	memcpy(sc->sc_mac, CLLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
   2229 
   2230 	features = virtio_features(sc->sc_virtio);
   2231 	if (features & VIRTIO_NET_F_CTRL_MAC_ADDR) {
   2232 		vioif_ctrl_acquire(sc);
   2233 
   2234 		memcpy(ma->mac, sc->sc_mac, ETHER_ADDR_LEN);
   2235 		specs[0].dmamap = sc->sc_ctrlq.ctrlq_mac_addr_dmamap;
   2236 		specs[0].buf = ma;
   2237 		specs[0].bufsize = sizeof(*ma);
   2238 
   2239 		r = vioif_ctrl_send_command(sc,
   2240 		    VIRTIO_NET_CTRL_MAC, VIRTIO_NET_CTRL_MAC_ADDR_SET,
   2241 		    specs, nspecs);
   2242 
   2243 		vioif_ctrl_release(sc);
   2244 	} else {
   2245 		for (i = 0; i < __arraycount(sc->sc_mac); i++) {
   2246 			virtio_write_device_config_1(sc->sc_virtio,
   2247 			    VIRTIO_NET_CONFIG_MAC + i, sc->sc_mac[i]);
   2248 		}
   2249 		r = 0;
   2250 	}
   2251 
   2252 	return r;
   2253 }
   2254 
   2255 static int
   2256 vioif_ctrl_mq_vq_pairs_set(struct vioif_softc *sc, int nvq_pairs)
   2257 {
   2258 	struct virtio_net_ctrl_mq *mq = sc->sc_ctrlq.ctrlq_mq;
   2259 	struct vioif_ctrl_cmdspec specs[1];
   2260 	int r;
   2261 
   2262 	if (!sc->sc_has_ctrl)
   2263 		return ENOTSUP;
   2264 
   2265 	if (nvq_pairs <= 1)
   2266 		return EINVAL;
   2267 
   2268 	vioif_ctrl_acquire(sc);
   2269 
   2270 	mq->virtqueue_pairs = virtio_rw16(sc->sc_virtio, nvq_pairs);
   2271 	specs[0].dmamap = sc->sc_ctrlq.ctrlq_mq_dmamap;
   2272 	specs[0].buf = mq;
   2273 	specs[0].bufsize = sizeof(*mq);
   2274 
   2275 	r = vioif_ctrl_send_command(sc,
   2276 	    VIRTIO_NET_CTRL_MQ, VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET,
   2277 	    specs, __arraycount(specs));
   2278 
   2279 	vioif_ctrl_release(sc);
   2280 
   2281 	return r;
   2282 }
   2283 
   2284 /* ctrl vq interrupt; wake up the command issuer */
   2285 static int
   2286 vioif_ctrl_intr(void *arg)
   2287 {
   2288 	struct vioif_ctrlqueue *ctrlq = arg;
   2289 	struct virtqueue *vq = ctrlq->ctrlq_vq;
   2290 	struct virtio_softc *vsc = vq->vq_owner;
   2291 	int r, slot;
   2292 
   2293 	if (virtio_vq_is_enqueued(vsc, vq) == false)
   2294 		return 0;
   2295 
   2296 	r = virtio_dequeue(vsc, vq, &slot, NULL);
   2297 	if (r == ENOENT)
   2298 		return 0;
   2299 	virtio_dequeue_commit(vsc, vq, slot);
   2300 
   2301 	mutex_enter(&ctrlq->ctrlq_wait_lock);
   2302 	ctrlq->ctrlq_inuse = DONE;
   2303 	cv_signal(&ctrlq->ctrlq_wait);
   2304 	mutex_exit(&ctrlq->ctrlq_wait_lock);
   2305 
   2306 	return 1;
   2307 }
   2308 
   2309 static int
   2310 vioif_ifflags(struct vioif_softc *sc)
   2311 {
   2312 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   2313 	bool onoff;
   2314 	int r;
   2315 
   2316 	if (!sc->sc_has_ctrl) {
   2317 		/* no ctrl vq; always promisc and allmulti */
   2318 		ifp->if_flags |= (IFF_PROMISC | IFF_ALLMULTI);
   2319 		return 0;
   2320 	}
   2321 
   2322 	onoff = ifp->if_flags & IFF_ALLMULTI ? true : false;
   2323 	r = vioif_set_allmulti(sc, onoff);
   2324 	if (r != 0) {
   2325 		log(LOG_WARNING,
   2326 		    "%s: couldn't %sable ALLMULTI\n",
   2327 		    ifp->if_xname, onoff ? "en" : "dis");
   2328 		if (onoff == false) {
   2329 			ifp->if_flags |= IFF_ALLMULTI;
   2330 		}
   2331 	}
   2332 
   2333 	onoff = ifp->if_flags & IFF_PROMISC ? true : false;
   2334 	r = vioif_set_promisc(sc, onoff);
   2335 	if (r != 0) {
   2336 		log(LOG_WARNING,
   2337 		    "%s: couldn't %sable PROMISC\n",
   2338 		    ifp->if_xname, onoff ? "en" : "dis");
   2339 		if (onoff == false) {
   2340 			ifp->if_flags |= IFF_PROMISC;
   2341 		}
   2342 	}
   2343 
   2344 	return 0;
   2345 }
   2346 
   2347 static int
   2348 vioif_ifflags_cb(struct ethercom *ec)
   2349 {
   2350 	struct ifnet *ifp = &ec->ec_if;
   2351 	struct vioif_softc *sc = ifp->if_softc;
   2352 
   2353 	return vioif_ifflags(sc);
   2354 }
   2355 
   2356 /*
   2357  * If multicast filter small enough (<=MAXENTRIES) set rx filter
   2358  * If large multicast filter exist use ALLMULTI
   2359  * If setting rx filter fails fall back to ALLMULTI
   2360  */
   2361 static int
   2362 vioif_rx_filter(struct vioif_softc *sc)
   2363 {
   2364 	struct virtio_softc *vsc = sc->sc_virtio;
   2365 	struct ethercom *ec = &sc->sc_ethercom;
   2366 	struct ifnet *ifp = &ec->ec_if;
   2367 	struct ether_multi *enm;
   2368 	struct ether_multistep step;
   2369 	struct vioif_ctrlqueue *ctrlq = &sc->sc_ctrlq;
   2370 	int nentries;
   2371 	bool allmulti = 0;
   2372 	int r;
   2373 
   2374 	if (!sc->sc_has_ctrl) {
   2375 		goto set_ifflags;
   2376 	}
   2377 
   2378 	memcpy(ctrlq->ctrlq_mac_tbl_uc->macs[0],
   2379 	    CLLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
   2380 
   2381 	nentries = 0;
   2382 	allmulti = false;
   2383 
   2384 	ETHER_LOCK(ec);
   2385 	for (ETHER_FIRST_MULTI(step, ec, enm); enm != NULL;
   2386 	    ETHER_NEXT_MULTI(step, enm)) {
   2387 		if (nentries >= VIRTIO_NET_CTRL_MAC_MAXENTRIES) {
   2388 			allmulti = true;
   2389 			break;
   2390 		}
   2391 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
   2392 			allmulti = true;
   2393 			break;
   2394 		}
   2395 
   2396 		memcpy(ctrlq->ctrlq_mac_tbl_mc->macs[nentries],
   2397 		    enm->enm_addrlo, ETHER_ADDR_LEN);
   2398 		nentries++;
   2399 	}
   2400 	ETHER_UNLOCK(ec);
   2401 
   2402 	r = vioif_set_mac_addr(sc);
   2403 	if (r != 0) {
   2404 		log(LOG_WARNING, "%s: couldn't set MAC address\n",
   2405 		    ifp->if_xname);
   2406 	}
   2407 
   2408 	if (!allmulti) {
   2409 		ctrlq->ctrlq_mac_tbl_uc->nentries = virtio_rw32(vsc, 1);
   2410 		ctrlq->ctrlq_mac_tbl_mc->nentries = virtio_rw32(vsc, nentries);
   2411 		r = vioif_set_rx_filter(sc);
   2412 		if (r != 0) {
   2413 			allmulti = true; /* fallback */
   2414 		}
   2415 	}
   2416 
   2417 	if (allmulti) {
   2418 		ctrlq->ctrlq_mac_tbl_uc->nentries = virtio_rw32(vsc, 0);
   2419 		ctrlq->ctrlq_mac_tbl_mc->nentries = virtio_rw32(vsc, 0);
   2420 		r = vioif_set_rx_filter(sc);
   2421 		if (r != 0) {
   2422 			log(LOG_DEBUG, "%s: couldn't clear RX filter\n",
   2423 			    ifp->if_xname);
   2424 			/* what to do on failure? */
   2425 		}
   2426 
   2427 		ifp->if_flags |= IFF_ALLMULTI;
   2428 	}
   2429 
   2430 set_ifflags:
   2431 	r = vioif_ifflags(sc);
   2432 
   2433 	return r;
   2434 }
   2435 
   2436 static int
   2437 vioif_get_link_status(struct vioif_softc *sc)
   2438 {
   2439 	struct virtio_softc *vsc = sc->sc_virtio;
   2440 	uint16_t status;
   2441 
   2442 	if (virtio_features(vsc) & VIRTIO_NET_F_STATUS)
   2443 		status = virtio_read_device_config_2(vsc,
   2444 		    VIRTIO_NET_CONFIG_STATUS);
   2445 	else
   2446 		status = VIRTIO_NET_S_LINK_UP;
   2447 
   2448 	if ((status & VIRTIO_NET_S_LINK_UP) != 0)
   2449 		return LINK_STATE_UP;
   2450 
   2451 	return LINK_STATE_DOWN;
   2452 }
   2453 
   2454 /* change link status */
   2455 static void
   2456 vioif_update_link_status(struct vioif_softc *sc)
   2457 {
   2458 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   2459 	struct vioif_txqueue *txq;
   2460 	bool active;
   2461 	int link, i;
   2462 
   2463 	mutex_enter(&sc->sc_lock);
   2464 
   2465 	link = vioif_get_link_status(sc);
   2466 
   2467 	if (link == sc->sc_link_state)
   2468 		goto done;
   2469 
   2470 	sc->sc_link_state = link;
   2471 
   2472 	active = VIOIF_IS_LINK_ACTIVE(sc);
   2473 	for (i = 0; i < sc->sc_act_nvq_pairs; i++) {
   2474 		txq = &sc->sc_txq[i];
   2475 
   2476 		mutex_enter(txq->txq_lock);
   2477 		txq->txq_link_active = active;
   2478 		mutex_exit(txq->txq_lock);
   2479 	}
   2480 
   2481 	if_link_state_change(ifp, sc->sc_link_state);
   2482 
   2483 done:
   2484 	mutex_exit(&sc->sc_lock);
   2485 }
   2486 
   2487 static int
   2488 vioif_config_change(struct virtio_softc *vsc)
   2489 {
   2490 	struct vioif_softc *sc = device_private(virtio_child(vsc));
   2491 
   2492 	softint_schedule(sc->sc_ctl_softint);
   2493 	return 0;
   2494 }
   2495 
   2496 static void
   2497 vioif_ctl_softint(void *arg)
   2498 {
   2499 	struct vioif_softc *sc = arg;
   2500 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   2501 
   2502 	vioif_update_link_status(sc);
   2503 	vioif_start(ifp);
   2504 }
   2505 
   2506 static struct workqueue *
   2507 vioif_workq_create(const char *name, pri_t prio, int ipl, int flags)
   2508 {
   2509 	struct workqueue *wq;
   2510 	int error;
   2511 
   2512 	error = workqueue_create(&wq, name, vioif_workq_work, NULL,
   2513 	    prio, ipl, flags);
   2514 
   2515 	if (error)
   2516 		return NULL;
   2517 
   2518 	return wq;
   2519 }
   2520 
   2521 static void
   2522 vioif_workq_destroy(struct workqueue *wq)
   2523 {
   2524 
   2525 	workqueue_destroy(wq);
   2526 }
   2527 
   2528 static void
   2529 vioif_workq_work(struct work *wk, void *context)
   2530 {
   2531 	struct vioif_work *work;
   2532 
   2533 	work = container_of(wk, struct vioif_work, cookie);
   2534 
   2535 	atomic_store_relaxed(&work->added, 0);
   2536 	work->func(work->arg);
   2537 }
   2538 
   2539 static void
   2540 vioif_work_set(struct vioif_work *work, void (*func)(void *), void *arg)
   2541 {
   2542 
   2543 	memset(work, 0, sizeof(*work));
   2544 	work->func = func;
   2545 	work->arg = arg;
   2546 }
   2547 
   2548 static void
   2549 vioif_work_add(struct workqueue *wq, struct vioif_work *work)
   2550 {
   2551 
   2552 	if (atomic_load_relaxed(&work->added) != 0)
   2553 		return;
   2554 
   2555 	atomic_store_relaxed(&work->added, 1);
   2556 	kpreempt_disable();
   2557 	workqueue_enqueue(wq, &work->cookie, NULL);
   2558 	kpreempt_enable();
   2559 }
   2560 
   2561 static void
   2562 vioif_work_wait(struct workqueue *wq, struct vioif_work *work)
   2563 {
   2564 
   2565 	workqueue_wait(wq, &work->cookie);
   2566 }
   2567 
   2568 static int
   2569 vioif_setup_sysctl(struct vioif_softc *sc)
   2570 {
   2571 	const char *devname;
   2572 	struct sysctllog **log;
   2573 	const struct sysctlnode *rnode, *rxnode, *txnode;
   2574 	int error;
   2575 
   2576 	log = &sc->sc_sysctllog;
   2577 	devname = device_xname(sc->sc_dev);
   2578 
   2579 	error = sysctl_createv(log, 0, NULL, &rnode,
   2580 	    0, CTLTYPE_NODE, devname,
   2581 	    SYSCTL_DESCR("virtio-net information and settings"),
   2582 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
   2583 	if (error)
   2584 		goto out;
   2585 
   2586 	error = sysctl_createv(log, 0, &rnode, NULL,
   2587 	    CTLFLAG_READWRITE, CTLTYPE_BOOL, "txrx_workqueue",
   2588 	    SYSCTL_DESCR("Use workqueue for packet processing"),
   2589 	    NULL, 0, &sc->sc_txrx_workqueue_sysctl, 0, CTL_CREATE, CTL_EOL);
   2590 	if (error)
   2591 		goto out;
   2592 
   2593 	error = sysctl_createv(log, 0, &rnode, &rxnode,
   2594 	    0, CTLTYPE_NODE, "rx",
   2595 	    SYSCTL_DESCR("virtio-net information and settings for Rx"),
   2596 	    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL);
   2597 	if (error)
   2598 		goto out;
   2599 
   2600 	error = sysctl_createv(log, 0, &rxnode, NULL,
   2601 	    CTLFLAG_READWRITE, CTLTYPE_INT, "intr_process_limit",
   2602 	    SYSCTL_DESCR("max number of Rx packets to process for interrupt processing"),
   2603 	    NULL, 0, &sc->sc_rx_intr_process_limit, 0, CTL_CREATE, CTL_EOL);
   2604 	if (error)
   2605 		goto out;
   2606 
   2607 	error = sysctl_createv(log, 0, &rxnode, NULL,
   2608 	    CTLFLAG_READWRITE, CTLTYPE_INT, "process_limit",
   2609 	    SYSCTL_DESCR("max number of Rx packets to process for deferred processing"),
   2610 	    NULL, 0, &sc->sc_rx_process_limit, 0, CTL_CREATE, CTL_EOL);
   2611 	if (error)
   2612 		goto out;
   2613 
   2614 	error = sysctl_createv(log, 0, &rnode, &txnode,
   2615 	    0, CTLTYPE_NODE, "tx",
   2616 	    SYSCTL_DESCR("virtio-net information and settings for Tx"),
   2617 	    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL);
   2618 	if (error)
   2619 		goto out;
   2620 
   2621 	error = sysctl_createv(log, 0, &txnode, NULL,
   2622 	    CTLFLAG_READWRITE, CTLTYPE_INT, "intr_process_limit",
   2623 	    SYSCTL_DESCR("max number of Tx packets to process for interrupt processing"),
   2624 	    NULL, 0, &sc->sc_tx_intr_process_limit, 0, CTL_CREATE, CTL_EOL);
   2625 	if (error)
   2626 		goto out;
   2627 
   2628 	error = sysctl_createv(log, 0, &txnode, NULL,
   2629 	    CTLFLAG_READWRITE, CTLTYPE_INT, "process_limit",
   2630 	    SYSCTL_DESCR("max number of Tx packets to process for deferred processing"),
   2631 	    NULL, 0, &sc->sc_tx_process_limit, 0, CTL_CREATE, CTL_EOL);
   2632 
   2633 out:
   2634 	if (error)
   2635 		sysctl_teardown(log);
   2636 
   2637 	return error;
   2638 }
   2639 
   2640 static void
   2641 vioif_setup_stats(struct vioif_softc *sc)
   2642 {
   2643 	struct vioif_rxqueue *rxq;
   2644 	struct vioif_txqueue *txq;
   2645 	int i;
   2646 
   2647 	for (i = 0; i < sc->sc_max_nvq_pairs; i++) {
   2648 		rxq = &sc->sc_rxq[i];
   2649 		txq = &sc->sc_txq[i];
   2650 
   2651 		snprintf(txq->txq_evgroup, sizeof(txq->txq_evgroup), "%s-TX%d",
   2652 		    device_xname(sc->sc_dev), i);
   2653 		evcnt_attach_dynamic(&txq->txq_defrag_failed, EVCNT_TYPE_MISC,
   2654 		    NULL, txq->txq_evgroup, "tx m_defrag() failed");
   2655 		evcnt_attach_dynamic(&txq->txq_mbuf_load_failed, EVCNT_TYPE_MISC,
   2656 		    NULL, txq->txq_evgroup, "tx dmamap load failed");
   2657 		evcnt_attach_dynamic(&txq->txq_enqueue_reserve_failed, EVCNT_TYPE_MISC,
   2658 		    NULL, txq->txq_evgroup, "virtio_enqueue_reserve failed");
   2659 
   2660 		snprintf(rxq->rxq_evgroup, sizeof(rxq->rxq_evgroup), "%s-RX%d",
   2661 		    device_xname(sc->sc_dev), i);
   2662 		evcnt_attach_dynamic(&rxq->rxq_mbuf_add_failed, EVCNT_TYPE_MISC,
   2663 		    NULL, rxq->rxq_evgroup, "rx mbuf allocation failed");
   2664 	}
   2665 
   2666 	evcnt_attach_dynamic(&sc->sc_ctrlq.ctrlq_cmd_load_failed, EVCNT_TYPE_MISC,
   2667 	    NULL, device_xname(sc->sc_dev), "control command dmamap load failed");
   2668 	evcnt_attach_dynamic(&sc->sc_ctrlq.ctrlq_cmd_failed, EVCNT_TYPE_MISC,
   2669 	    NULL, device_xname(sc->sc_dev), "control command failed");
   2670 }
   2671 
   2672 static void
   2673 vioif_intr_barrier(void)
   2674 {
   2675 
   2676 	/* wait for finish all interrupt handler */
   2677 	xc_barrier(0);
   2678 }
   2679 
   2680 MODULE(MODULE_CLASS_DRIVER, if_vioif, "virtio");
   2681 
   2682 #ifdef _MODULE
   2683 #include "ioconf.c"
   2684 #endif
   2685 
   2686 static int
   2687 if_vioif_modcmd(modcmd_t cmd, void *opaque)
   2688 {
   2689 	int error = 0;
   2690 
   2691 #ifdef _MODULE
   2692 	switch (cmd) {
   2693 	case MODULE_CMD_INIT:
   2694 		error = config_init_component(cfdriver_ioconf_if_vioif,
   2695 		    cfattach_ioconf_if_vioif, cfdata_ioconf_if_vioif);
   2696 		break;
   2697 	case MODULE_CMD_FINI:
   2698 		error = config_fini_component(cfdriver_ioconf_if_vioif,
   2699 		    cfattach_ioconf_if_vioif, cfdata_ioconf_if_vioif);
   2700 		break;
   2701 	default:
   2702 		error = ENOTTY;
   2703 		break;
   2704 	}
   2705 #endif
   2706 
   2707 	return error;
   2708 }
   2709