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