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