Home | History | Annotate | Line # | Download | only in pci
if_vioif.c revision 1.51.2.1
      1  1.51.2.1        ad /*	$NetBSD: if_vioif.c,v 1.51.2.1 2020/02/29 20:19:11 ad Exp $	*/
      2       1.1   hannken 
      3       1.1   hannken /*
      4       1.1   hannken  * Copyright (c) 2010 Minoura Makoto.
      5       1.1   hannken  * All rights reserved.
      6       1.1   hannken  *
      7       1.1   hannken  * Redistribution and use in source and binary forms, with or without
      8       1.1   hannken  * modification, are permitted provided that the following conditions
      9       1.1   hannken  * are met:
     10       1.1   hannken  * 1. Redistributions of source code must retain the above copyright
     11       1.1   hannken  *    notice, this list of conditions and the following disclaimer.
     12       1.1   hannken  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1   hannken  *    notice, this list of conditions and the following disclaimer in the
     14       1.1   hannken  *    documentation and/or other materials provided with the distribution.
     15       1.1   hannken  *
     16       1.1   hannken  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17       1.1   hannken  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18       1.1   hannken  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19       1.1   hannken  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20       1.1   hannken  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21       1.1   hannken  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22       1.1   hannken  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23       1.1   hannken  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24       1.1   hannken  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25       1.1   hannken  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26       1.1   hannken  */
     27       1.1   hannken 
     28       1.1   hannken #include <sys/cdefs.h>
     29  1.51.2.1        ad __KERNEL_RCSID(0, "$NetBSD: if_vioif.c,v 1.51.2.1 2020/02/29 20:19:11 ad Exp $");
     30      1.15     ozaki 
     31      1.15     ozaki #ifdef _KERNEL_OPT
     32      1.15     ozaki #include "opt_net_mpsafe.h"
     33      1.15     ozaki #endif
     34       1.1   hannken 
     35       1.1   hannken #include <sys/param.h>
     36       1.1   hannken #include <sys/systm.h>
     37       1.1   hannken #include <sys/kernel.h>
     38       1.1   hannken #include <sys/bus.h>
     39       1.1   hannken #include <sys/condvar.h>
     40       1.1   hannken #include <sys/device.h>
     41       1.1   hannken #include <sys/intr.h>
     42       1.1   hannken #include <sys/kmem.h>
     43       1.1   hannken #include <sys/mbuf.h>
     44       1.1   hannken #include <sys/mutex.h>
     45       1.1   hannken #include <sys/sockio.h>
     46      1.12     ozaki #include <sys/cpu.h>
     47      1.26  pgoyette #include <sys/module.h>
     48      1.46  yamaguch #include <sys/pcq.h>
     49       1.1   hannken 
     50       1.1   hannken #include <dev/pci/virtioreg.h>
     51       1.1   hannken #include <dev/pci/virtiovar.h>
     52       1.1   hannken 
     53       1.1   hannken #include <net/if.h>
     54       1.1   hannken #include <net/if_media.h>
     55       1.1   hannken #include <net/if_ether.h>
     56       1.1   hannken 
     57       1.1   hannken #include <net/bpf.h>
     58       1.1   hannken 
     59      1.26  pgoyette #include "ioconf.h"
     60       1.1   hannken 
     61       1.7     ozaki #ifdef NET_MPSAFE
     62       1.7     ozaki #define VIOIF_MPSAFE	1
     63      1.46  yamaguch #define VIOIF_MULTIQ	1
     64       1.7     ozaki #endif
     65       1.7     ozaki 
     66      1.12     ozaki #ifdef SOFTINT_INTR
     67      1.12     ozaki #define VIOIF_SOFTINT_INTR	1
     68      1.12     ozaki #endif
     69      1.12     ozaki 
     70       1.1   hannken /*
     71       1.1   hannken  * if_vioifreg.h:
     72       1.1   hannken  */
     73       1.1   hannken /* Configuration registers */
     74       1.1   hannken #define VIRTIO_NET_CONFIG_MAC		0 /* 8bit x 6byte */
     75       1.1   hannken #define VIRTIO_NET_CONFIG_STATUS	6 /* 16bit */
     76      1.46  yamaguch #define VIRTIO_NET_CONFIG_MAX_VQ_PAIRS	8 /* 16bit */
     77       1.1   hannken 
     78       1.1   hannken /* Feature bits */
     79      1.46  yamaguch #define VIRTIO_NET_F_CSUM		__BIT(0)
     80      1.46  yamaguch #define VIRTIO_NET_F_GUEST_CSUM		__BIT(1)
     81      1.46  yamaguch #define VIRTIO_NET_F_MAC		__BIT(5)
     82      1.46  yamaguch #define VIRTIO_NET_F_GSO		__BIT(6)
     83      1.46  yamaguch #define VIRTIO_NET_F_GUEST_TSO4		__BIT(7)
     84      1.46  yamaguch #define VIRTIO_NET_F_GUEST_TSO6		__BIT(8)
     85      1.46  yamaguch #define VIRTIO_NET_F_GUEST_ECN		__BIT(9)
     86      1.46  yamaguch #define VIRTIO_NET_F_GUEST_UFO		__BIT(10)
     87      1.46  yamaguch #define VIRTIO_NET_F_HOST_TSO4		__BIT(11)
     88      1.46  yamaguch #define VIRTIO_NET_F_HOST_TSO6		__BIT(12)
     89      1.46  yamaguch #define VIRTIO_NET_F_HOST_ECN		__BIT(13)
     90      1.46  yamaguch #define VIRTIO_NET_F_HOST_UFO		__BIT(14)
     91      1.46  yamaguch #define VIRTIO_NET_F_MRG_RXBUF		__BIT(15)
     92      1.46  yamaguch #define VIRTIO_NET_F_STATUS		__BIT(16)
     93      1.46  yamaguch #define VIRTIO_NET_F_CTRL_VQ		__BIT(17)
     94      1.46  yamaguch #define VIRTIO_NET_F_CTRL_RX		__BIT(18)
     95      1.46  yamaguch #define VIRTIO_NET_F_CTRL_VLAN		__BIT(19)
     96      1.46  yamaguch #define VIRTIO_NET_F_CTRL_RX_EXTRA	__BIT(20)
     97      1.46  yamaguch #define VIRTIO_NET_F_GUEST_ANNOUNCE	__BIT(21)
     98      1.46  yamaguch #define VIRTIO_NET_F_MQ			__BIT(22)
     99       1.1   hannken 
    100      1.18  christos #define VIRTIO_NET_FLAG_BITS \
    101      1.18  christos 	VIRTIO_COMMON_FLAG_BITS \
    102      1.46  yamaguch 	"\x17""MQ" \
    103      1.46  yamaguch 	"\x16""GUEST_ANNOUNCE" \
    104      1.46  yamaguch 	"\x15""CTRL_RX_EXTRA" \
    105      1.18  christos 	"\x14""CTRL_VLAN" \
    106      1.18  christos 	"\x13""CTRL_RX" \
    107      1.18  christos 	"\x12""CTRL_VQ" \
    108      1.18  christos 	"\x11""STATUS" \
    109      1.18  christos 	"\x10""MRG_RXBUF" \
    110      1.18  christos 	"\x0f""HOST_UFO" \
    111      1.18  christos 	"\x0e""HOST_ECN" \
    112      1.18  christos 	"\x0d""HOST_TSO6" \
    113      1.18  christos 	"\x0c""HOST_TSO4" \
    114      1.18  christos 	"\x0b""GUEST_UFO" \
    115      1.18  christos 	"\x0a""GUEST_ECN" \
    116      1.18  christos 	"\x09""GUEST_TSO6" \
    117      1.18  christos 	"\x08""GUEST_TSO4" \
    118      1.18  christos 	"\x07""GSO" \
    119      1.18  christos 	"\x06""MAC" \
    120      1.18  christos 	"\x02""GUEST_CSUM" \
    121      1.18  christos 	"\x01""CSUM"
    122      1.18  christos 
    123       1.1   hannken /* Status */
    124       1.1   hannken #define VIRTIO_NET_S_LINK_UP	1
    125       1.1   hannken 
    126       1.1   hannken /* Packet header structure */
    127       1.1   hannken struct virtio_net_hdr {
    128       1.1   hannken 	uint8_t		flags;
    129       1.1   hannken 	uint8_t		gso_type;
    130       1.1   hannken 	uint16_t	hdr_len;
    131       1.1   hannken 	uint16_t	gso_size;
    132       1.1   hannken 	uint16_t	csum_start;
    133       1.1   hannken 	uint16_t	csum_offset;
    134       1.1   hannken #if 0
    135       1.1   hannken 	uint16_t	num_buffers; /* if VIRTIO_NET_F_MRG_RXBUF enabled */
    136       1.1   hannken #endif
    137       1.1   hannken } __packed;
    138       1.1   hannken 
    139       1.1   hannken #define VIRTIO_NET_HDR_F_NEEDS_CSUM	1 /* flags */
    140       1.1   hannken #define VIRTIO_NET_HDR_GSO_NONE		0 /* gso_type */
    141       1.1   hannken #define VIRTIO_NET_HDR_GSO_TCPV4	1 /* gso_type */
    142       1.1   hannken #define VIRTIO_NET_HDR_GSO_UDP		3 /* gso_type */
    143       1.1   hannken #define VIRTIO_NET_HDR_GSO_TCPV6	4 /* gso_type */
    144       1.1   hannken #define VIRTIO_NET_HDR_GSO_ECN		0x80 /* gso_type, |'ed */
    145       1.1   hannken 
    146       1.1   hannken #define VIRTIO_NET_MAX_GSO_LEN		(65536+ETHER_HDR_LEN)
    147       1.1   hannken 
    148       1.1   hannken /* Control virtqueue */
    149       1.1   hannken struct virtio_net_ctrl_cmd {
    150       1.1   hannken 	uint8_t	class;
    151       1.1   hannken 	uint8_t	command;
    152       1.1   hannken } __packed;
    153       1.1   hannken #define VIRTIO_NET_CTRL_RX		0
    154       1.1   hannken # define VIRTIO_NET_CTRL_RX_PROMISC	0
    155       1.1   hannken # define VIRTIO_NET_CTRL_RX_ALLMULTI	1
    156       1.1   hannken 
    157       1.1   hannken #define VIRTIO_NET_CTRL_MAC		1
    158       1.1   hannken # define VIRTIO_NET_CTRL_MAC_TABLE_SET	0
    159       1.1   hannken 
    160       1.1   hannken #define VIRTIO_NET_CTRL_VLAN		2
    161       1.1   hannken # define VIRTIO_NET_CTRL_VLAN_ADD	0
    162       1.1   hannken # define VIRTIO_NET_CTRL_VLAN_DEL	1
    163       1.1   hannken 
    164      1.46  yamaguch #define VIRTIO_NET_CTRL_MQ			4
    165      1.46  yamaguch # define VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET	0
    166      1.46  yamaguch # define VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN	1
    167      1.46  yamaguch # define VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX	0x8000
    168      1.46  yamaguch 
    169       1.1   hannken struct virtio_net_ctrl_status {
    170       1.1   hannken 	uint8_t	ack;
    171       1.1   hannken } __packed;
    172       1.1   hannken #define VIRTIO_NET_OK			0
    173       1.1   hannken #define VIRTIO_NET_ERR			1
    174       1.1   hannken 
    175       1.1   hannken struct virtio_net_ctrl_rx {
    176       1.1   hannken 	uint8_t	onoff;
    177       1.1   hannken } __packed;
    178       1.1   hannken 
    179       1.1   hannken struct virtio_net_ctrl_mac_tbl {
    180       1.1   hannken 	uint32_t nentries;
    181       1.1   hannken 	uint8_t macs[][ETHER_ADDR_LEN];
    182       1.1   hannken } __packed;
    183       1.1   hannken 
    184       1.1   hannken struct virtio_net_ctrl_vlan {
    185       1.1   hannken 	uint16_t id;
    186       1.1   hannken } __packed;
    187       1.1   hannken 
    188      1.46  yamaguch struct virtio_net_ctrl_mq {
    189      1.46  yamaguch 	uint16_t virtqueue_pairs;
    190      1.46  yamaguch } __packed;
    191      1.46  yamaguch 
    192      1.44  yamaguch struct vioif_ctrl_cmdspec {
    193      1.44  yamaguch 	bus_dmamap_t	dmamap;
    194      1.44  yamaguch 	void		*buf;
    195      1.44  yamaguch 	bus_size_t	bufsize;
    196      1.44  yamaguch };
    197       1.1   hannken 
    198       1.1   hannken /*
    199       1.1   hannken  * if_vioifvar.h:
    200       1.1   hannken  */
    201      1.43  yamaguch 
    202      1.43  yamaguch /*
    203      1.43  yamaguch  * Locking notes:
    204      1.43  yamaguch  * + a field in vioif_txqueue is protected by txq_lock (a spin mutex), and
    205      1.43  yamaguch  *   a filds in vioif_rxqueue is protected by rxq_lock (a spin mutex).
    206      1.43  yamaguch  *      - more than one lock cannot be held at onece
    207      1.43  yamaguch  * + ctrlq_inuse is protected by ctrlq_wait_lock.
    208      1.43  yamaguch  *      - other fields in vioif_ctrlqueue are protected by ctrlq_inuse
    209      1.43  yamaguch  *      - txq_lock or rxq_lock cannot be held along with ctrlq_wait_lock
    210      1.43  yamaguch  */
    211      1.43  yamaguch 
    212      1.43  yamaguch struct vioif_txqueue {
    213      1.43  yamaguch 	kmutex_t		*txq_lock;	/* lock for tx operations */
    214      1.43  yamaguch 
    215      1.43  yamaguch 	struct virtqueue	*txq_vq;
    216      1.43  yamaguch 	bool			txq_stopping;
    217      1.43  yamaguch 	bool			txq_link_active;
    218      1.46  yamaguch 	pcq_t			*txq_intrq;
    219      1.43  yamaguch 
    220      1.43  yamaguch 	struct virtio_net_hdr	*txq_hdrs;
    221      1.43  yamaguch 	bus_dmamap_t		*txq_hdr_dmamaps;
    222      1.43  yamaguch 
    223      1.43  yamaguch 	struct mbuf		**txq_mbufs;
    224      1.43  yamaguch 	bus_dmamap_t		*txq_dmamaps;
    225      1.46  yamaguch 
    226      1.46  yamaguch 	void			*txq_deferred_transmit;
    227      1.43  yamaguch };
    228      1.43  yamaguch 
    229      1.43  yamaguch struct vioif_rxqueue {
    230      1.43  yamaguch 	kmutex_t		*rxq_lock;	/* lock for rx operations */
    231      1.43  yamaguch 
    232      1.43  yamaguch 	struct virtqueue	*rxq_vq;
    233      1.43  yamaguch 	bool			rxq_stopping;
    234      1.43  yamaguch 
    235      1.43  yamaguch 	struct virtio_net_hdr	*rxq_hdrs;
    236      1.43  yamaguch 	bus_dmamap_t		*rxq_hdr_dmamaps;
    237      1.43  yamaguch 
    238      1.43  yamaguch 	struct mbuf		**rxq_mbufs;
    239      1.43  yamaguch 	bus_dmamap_t		*rxq_dmamaps;
    240      1.43  yamaguch 
    241      1.43  yamaguch 	void			*rxq_softint;
    242      1.43  yamaguch };
    243      1.43  yamaguch 
    244      1.43  yamaguch struct vioif_ctrlqueue {
    245      1.43  yamaguch 	struct virtqueue		*ctrlq_vq;
    246      1.43  yamaguch 	enum {
    247      1.43  yamaguch 		FREE, INUSE, DONE
    248      1.43  yamaguch 	}				ctrlq_inuse;
    249      1.43  yamaguch 	kcondvar_t			ctrlq_wait;
    250      1.43  yamaguch 	kmutex_t			ctrlq_wait_lock;
    251      1.44  yamaguch 	struct lwp			*ctrlq_owner;
    252      1.43  yamaguch 
    253      1.43  yamaguch 	struct virtio_net_ctrl_cmd	*ctrlq_cmd;
    254      1.43  yamaguch 	struct virtio_net_ctrl_status	*ctrlq_status;
    255      1.43  yamaguch 	struct virtio_net_ctrl_rx	*ctrlq_rx;
    256      1.43  yamaguch 	struct virtio_net_ctrl_mac_tbl	*ctrlq_mac_tbl_uc;
    257      1.43  yamaguch 	struct virtio_net_ctrl_mac_tbl	*ctrlq_mac_tbl_mc;
    258      1.46  yamaguch 	struct virtio_net_ctrl_mq	*ctrlq_mq;
    259      1.43  yamaguch 
    260      1.43  yamaguch 	bus_dmamap_t			ctrlq_cmd_dmamap;
    261      1.43  yamaguch 	bus_dmamap_t			ctrlq_status_dmamap;
    262      1.43  yamaguch 	bus_dmamap_t			ctrlq_rx_dmamap;
    263      1.43  yamaguch 	bus_dmamap_t			ctrlq_tbl_uc_dmamap;
    264      1.43  yamaguch 	bus_dmamap_t			ctrlq_tbl_mc_dmamap;
    265      1.46  yamaguch 	bus_dmamap_t			ctrlq_mq_dmamap;
    266      1.43  yamaguch };
    267      1.43  yamaguch 
    268       1.1   hannken struct vioif_softc {
    269       1.1   hannken 	device_t		sc_dev;
    270       1.1   hannken 
    271       1.1   hannken 	struct virtio_softc	*sc_virtio;
    272      1.46  yamaguch 	struct virtqueue	*sc_vqs;
    273      1.46  yamaguch 
    274      1.46  yamaguch 	int			sc_max_nvq_pairs;
    275      1.46  yamaguch 	int			sc_req_nvq_pairs;
    276      1.46  yamaguch 	int			sc_act_nvq_pairs;
    277       1.1   hannken 
    278       1.1   hannken 	uint8_t			sc_mac[ETHER_ADDR_LEN];
    279       1.1   hannken 	struct ethercom		sc_ethercom;
    280       1.8     pooka 	short			sc_deferred_init_done;
    281      1.34     ozaki 	bool			sc_link_active;
    282       1.1   hannken 
    283      1.46  yamaguch 	struct vioif_txqueue	*sc_txq;
    284      1.46  yamaguch 	struct vioif_rxqueue	*sc_rxq;
    285      1.43  yamaguch 
    286      1.43  yamaguch 	bool			sc_has_ctrl;
    287      1.43  yamaguch 	struct vioif_ctrlqueue	sc_ctrlq;
    288      1.43  yamaguch 
    289       1.1   hannken 	bus_dma_segment_t	sc_hdr_segs[1];
    290      1.43  yamaguch 	void			*sc_dmamem;
    291      1.43  yamaguch 	void			*sc_kmem;
    292       1.1   hannken 
    293      1.34     ozaki 	void			*sc_ctl_softint;
    294       1.1   hannken };
    295       1.1   hannken #define VIRTIO_NET_TX_MAXNSEGS		(16) /* XXX */
    296       1.1   hannken #define VIRTIO_NET_CTRL_MAC_MAXENTRIES	(64) /* XXX */
    297       1.1   hannken 
    298       1.1   hannken /* cfattach interface functions */
    299       1.1   hannken static int	vioif_match(device_t, cfdata_t, void *);
    300       1.1   hannken static void	vioif_attach(device_t, device_t, void *);
    301       1.1   hannken static void	vioif_deferred_init(device_t);
    302       1.1   hannken 
    303       1.1   hannken /* ifnet interface functions */
    304       1.1   hannken static int	vioif_init(struct ifnet *);
    305       1.1   hannken static void	vioif_stop(struct ifnet *, int);
    306       1.1   hannken static void	vioif_start(struct ifnet *);
    307      1.46  yamaguch static void	vioif_start_locked(struct ifnet *, struct vioif_txqueue *);
    308      1.46  yamaguch static int	vioif_transmit(struct ifnet *, struct mbuf *);
    309      1.46  yamaguch static void	vioif_transmit_locked(struct ifnet *, struct vioif_txqueue *);
    310       1.1   hannken static int	vioif_ioctl(struct ifnet *, u_long, void *);
    311       1.1   hannken static void	vioif_watchdog(struct ifnet *);
    312       1.1   hannken 
    313       1.1   hannken /* rx */
    314      1.46  yamaguch static int	vioif_add_rx_mbuf(struct vioif_rxqueue *, int);
    315      1.46  yamaguch static void	vioif_free_rx_mbuf(struct vioif_rxqueue *, int);
    316      1.46  yamaguch static void	vioif_populate_rx_mbufs(struct vioif_rxqueue *);
    317      1.46  yamaguch static void	vioif_populate_rx_mbufs_locked(struct vioif_rxqueue *);
    318      1.46  yamaguch static int	vioif_rx_deq(struct vioif_rxqueue *);
    319      1.46  yamaguch static int	vioif_rx_deq_locked(struct vioif_rxqueue *);
    320       1.1   hannken static int	vioif_rx_vq_done(struct virtqueue *);
    321       1.1   hannken static void	vioif_rx_softint(void *);
    322      1.46  yamaguch static void	vioif_rx_drain(struct vioif_rxqueue *);
    323       1.1   hannken 
    324       1.1   hannken /* tx */
    325       1.1   hannken static int	vioif_tx_vq_done(struct virtqueue *);
    326       1.7     ozaki static int	vioif_tx_vq_done_locked(struct virtqueue *);
    327      1.46  yamaguch static void	vioif_tx_drain(struct vioif_txqueue *);
    328      1.46  yamaguch static void	vioif_deferred_transmit(void *);
    329       1.1   hannken 
    330       1.1   hannken /* other control */
    331      1.34     ozaki static bool	vioif_is_link_up(struct vioif_softc *);
    332      1.34     ozaki static void	vioif_update_link_status(struct vioif_softc *);
    333       1.1   hannken static int	vioif_ctrl_rx(struct vioif_softc *, int, bool);
    334       1.1   hannken static int	vioif_set_promisc(struct vioif_softc *, bool);
    335       1.1   hannken static int	vioif_set_allmulti(struct vioif_softc *, bool);
    336       1.1   hannken static int	vioif_set_rx_filter(struct vioif_softc *);
    337       1.1   hannken static int	vioif_rx_filter(struct vioif_softc *);
    338       1.1   hannken static int	vioif_ctrl_vq_done(struct virtqueue *);
    339      1.34     ozaki static int	vioif_config_change(struct virtio_softc *);
    340      1.34     ozaki static void	vioif_ctl_softint(void *);
    341      1.46  yamaguch static int	vioif_ctrl_mq_vq_pairs_set(struct vioif_softc *, int);
    342      1.46  yamaguch static void	vioif_enable_interrupt_vqpairs(struct vioif_softc *);
    343      1.46  yamaguch static void	vioif_disable_interrupt_vqpairs(struct vioif_softc *);
    344       1.1   hannken 
    345       1.1   hannken CFATTACH_DECL_NEW(vioif, sizeof(struct vioif_softc),
    346       1.1   hannken 		  vioif_match, vioif_attach, NULL, NULL);
    347       1.1   hannken 
    348       1.1   hannken static int
    349       1.1   hannken vioif_match(device_t parent, cfdata_t match, void *aux)
    350       1.1   hannken {
    351      1.32  jdolecek 	struct virtio_attach_args *va = aux;
    352       1.1   hannken 
    353       1.1   hannken 	if (va->sc_childdevid == PCI_PRODUCT_VIRTIO_NETWORK)
    354       1.1   hannken 		return 1;
    355       1.1   hannken 
    356       1.1   hannken 	return 0;
    357       1.1   hannken }
    358       1.1   hannken 
    359      1.51       chs static void
    360      1.46  yamaguch vioif_alloc_queues(struct vioif_softc *sc)
    361      1.46  yamaguch {
    362      1.46  yamaguch 	int nvq_pairs = sc->sc_max_nvq_pairs;
    363      1.46  yamaguch 	int nvqs = nvq_pairs * 2;
    364      1.46  yamaguch 	int i;
    365      1.46  yamaguch 
    366      1.46  yamaguch 	KASSERT(nvq_pairs <= VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX);
    367      1.46  yamaguch 
    368      1.46  yamaguch 	sc->sc_rxq = kmem_zalloc(sizeof(sc->sc_rxq[0]) * nvq_pairs,
    369      1.51       chs 	    KM_SLEEP);
    370      1.46  yamaguch 	sc->sc_txq = kmem_zalloc(sizeof(sc->sc_txq[0]) * nvq_pairs,
    371      1.51       chs 	    KM_SLEEP);
    372      1.46  yamaguch 
    373      1.46  yamaguch 	if (sc->sc_has_ctrl)
    374      1.46  yamaguch 		nvqs++;
    375      1.46  yamaguch 
    376      1.51       chs 	sc->sc_vqs = kmem_zalloc(sizeof(sc->sc_vqs[0]) * nvqs, KM_SLEEP);
    377      1.46  yamaguch 	nvqs = 0;
    378      1.46  yamaguch 	for (i = 0; i < nvq_pairs; i++) {
    379      1.46  yamaguch 		sc->sc_rxq[i].rxq_vq = &sc->sc_vqs[nvqs++];
    380      1.46  yamaguch 		sc->sc_txq[i].txq_vq = &sc->sc_vqs[nvqs++];
    381      1.46  yamaguch 	}
    382      1.46  yamaguch 
    383      1.46  yamaguch 	if (sc->sc_has_ctrl)
    384      1.46  yamaguch 		sc->sc_ctrlq.ctrlq_vq = &sc->sc_vqs[nvqs++];
    385      1.46  yamaguch }
    386      1.46  yamaguch 
    387      1.46  yamaguch static void
    388      1.46  yamaguch vioif_free_queues(struct vioif_softc *sc)
    389      1.46  yamaguch {
    390      1.46  yamaguch 	int nvq_pairs = sc->sc_max_nvq_pairs;
    391      1.46  yamaguch 	int nvqs = nvq_pairs * 2;
    392      1.46  yamaguch 
    393      1.46  yamaguch 	if (sc->sc_ctrlq.ctrlq_vq)
    394      1.46  yamaguch 		nvqs++;
    395      1.46  yamaguch 
    396      1.46  yamaguch 	if (sc->sc_txq) {
    397      1.46  yamaguch 		kmem_free(sc->sc_txq, sizeof(sc->sc_txq[0]) * nvq_pairs);
    398      1.46  yamaguch 		sc->sc_txq = NULL;
    399      1.46  yamaguch 	}
    400      1.46  yamaguch 
    401      1.46  yamaguch 	if (sc->sc_rxq) {
    402      1.46  yamaguch 		kmem_free(sc->sc_rxq, sizeof(sc->sc_rxq[0]) * nvq_pairs);
    403      1.46  yamaguch 		sc->sc_rxq = NULL;
    404      1.46  yamaguch 	}
    405      1.46  yamaguch 
    406      1.46  yamaguch 	if (sc->sc_vqs) {
    407      1.46  yamaguch 		kmem_free(sc->sc_vqs, sizeof(sc->sc_vqs[0]) * nvqs);
    408      1.46  yamaguch 		sc->sc_vqs = NULL;
    409      1.46  yamaguch 	}
    410      1.46  yamaguch }
    411      1.46  yamaguch 
    412       1.1   hannken /* allocate memory */
    413       1.1   hannken /*
    414       1.1   hannken  * dma memory is used for:
    415      1.43  yamaguch  *   rxq_hdrs[slot]:	 metadata array for received frames (READ)
    416      1.43  yamaguch  *   txq_hdrs[slot]:	 metadata array for frames to be sent (WRITE)
    417      1.43  yamaguch  *   ctrlq_cmd:		 command to be sent via ctrl vq (WRITE)
    418      1.43  yamaguch  *   ctrlq_status:	 return value for a command via ctrl vq (READ)
    419      1.43  yamaguch  *   ctrlq_rx:		 parameter for a VIRTIO_NET_CTRL_RX class command
    420       1.1   hannken  *			 (WRITE)
    421      1.43  yamaguch  *   ctrlq_mac_tbl_uc:	 unicast MAC address filter for a VIRTIO_NET_CTRL_MAC
    422       1.1   hannken  *			 class command (WRITE)
    423      1.43  yamaguch  *   ctrlq_mac_tbl_mc:	 multicast MAC address filter for a VIRTIO_NET_CTRL_MAC
    424       1.1   hannken  *			 class command (WRITE)
    425      1.43  yamaguch  * ctrlq_* structures are allocated only one each; they are protected by
    426      1.43  yamaguch  * ctrlq_inuse variable and ctrlq_wait condvar.
    427       1.1   hannken  */
    428       1.1   hannken /*
    429       1.1   hannken  * dynamically allocated memory is used for:
    430      1.43  yamaguch  *   rxq_hdr_dmamaps[slot]:	bus_dmamap_t array for sc_rx_hdrs[slot]
    431      1.43  yamaguch  *   txq_hdr_dmamaps[slot]:	bus_dmamap_t array for sc_tx_hdrs[slot]
    432      1.43  yamaguch  *   rxq_dmamaps[slot]:		bus_dmamap_t array for received payload
    433      1.43  yamaguch  *   txq_dmamaps[slot]:		bus_dmamap_t array for sent payload
    434      1.43  yamaguch  *   rxq_mbufs[slot]:		mbuf pointer array for received frames
    435      1.43  yamaguch  *   txq_mbufs[slot]:		mbuf pointer array for sent frames
    436       1.1   hannken  */
    437       1.1   hannken static int
    438       1.1   hannken vioif_alloc_mems(struct vioif_softc *sc)
    439       1.1   hannken {
    440       1.1   hannken 	struct virtio_softc *vsc = sc->sc_virtio;
    441      1.46  yamaguch 	struct vioif_txqueue *txq;
    442      1.46  yamaguch 	struct vioif_rxqueue *rxq;
    443      1.43  yamaguch 	struct vioif_ctrlqueue *ctrlq = &sc->sc_ctrlq;
    444      1.46  yamaguch 	int allocsize, allocsize2, r, rsegs, i, qid;
    445       1.1   hannken 	void *vaddr;
    446       1.1   hannken 	intptr_t p;
    447       1.1   hannken 
    448      1.46  yamaguch 	allocsize = 0;
    449      1.46  yamaguch 	for (qid = 0; qid < sc->sc_max_nvq_pairs; qid++) {
    450      1.46  yamaguch 		rxq = &sc->sc_rxq[qid];
    451      1.46  yamaguch 		txq = &sc->sc_txq[qid];
    452      1.46  yamaguch 
    453      1.46  yamaguch 		allocsize +=
    454      1.46  yamaguch 		    sizeof(struct virtio_net_hdr) * rxq->rxq_vq->vq_num;
    455      1.46  yamaguch 		allocsize +=
    456      1.46  yamaguch 		    sizeof(struct virtio_net_hdr) * txq->txq_vq->vq_num;
    457      1.46  yamaguch 	}
    458      1.32  jdolecek 	if (sc->sc_has_ctrl) {
    459       1.1   hannken 		allocsize += sizeof(struct virtio_net_ctrl_cmd) * 1;
    460       1.1   hannken 		allocsize += sizeof(struct virtio_net_ctrl_status) * 1;
    461       1.1   hannken 		allocsize += sizeof(struct virtio_net_ctrl_rx) * 1;
    462       1.1   hannken 		allocsize += sizeof(struct virtio_net_ctrl_mac_tbl)
    463      1.50  christos 		    + sizeof(struct virtio_net_ctrl_mac_tbl)
    464      1.50  christos 		    + ETHER_ADDR_LEN * VIRTIO_NET_CTRL_MAC_MAXENTRIES;
    465      1.46  yamaguch 		allocsize += sizeof(struct virtio_net_ctrl_mq) * 1;
    466       1.1   hannken 	}
    467      1.32  jdolecek 	r = bus_dmamem_alloc(virtio_dmat(vsc), allocsize, 0, 0,
    468      1.50  christos 	    &sc->sc_hdr_segs[0], 1, &rsegs, BUS_DMA_NOWAIT);
    469       1.1   hannken 	if (r != 0) {
    470       1.1   hannken 		aprint_error_dev(sc->sc_dev,
    471      1.50  christos 		    "DMA memory allocation failed, size %d, "
    472      1.50  christos 		    "error code %d\n", allocsize, r);
    473       1.1   hannken 		goto err_none;
    474       1.1   hannken 	}
    475      1.32  jdolecek 	r = bus_dmamem_map(virtio_dmat(vsc),
    476      1.50  christos 	    &sc->sc_hdr_segs[0], 1, allocsize, &vaddr, BUS_DMA_NOWAIT);
    477       1.1   hannken 	if (r != 0) {
    478       1.1   hannken 		aprint_error_dev(sc->sc_dev,
    479      1.50  christos 		    "DMA memory map failed, error code %d\n", r);
    480       1.1   hannken 		goto err_dmamem_alloc;
    481       1.1   hannken 	}
    482      1.42  yamaguch 
    483      1.42  yamaguch #define P(p, p0, p0size)	do { p0 = (void *) p;		\
    484      1.42  yamaguch 				     p += p0size; } while (0)
    485       1.1   hannken 	memset(vaddr, 0, allocsize);
    486      1.43  yamaguch 	sc->sc_dmamem = vaddr;
    487       1.1   hannken 	p = (intptr_t) vaddr;
    488      1.42  yamaguch 
    489      1.46  yamaguch 	for (qid = 0; qid < sc->sc_max_nvq_pairs; qid++) {
    490      1.46  yamaguch 		rxq = &sc->sc_rxq[qid];
    491      1.46  yamaguch 		txq = &sc->sc_txq[qid];
    492      1.46  yamaguch 
    493      1.46  yamaguch 		P(p, rxq->rxq_hdrs,
    494      1.46  yamaguch 		    sizeof(rxq->rxq_hdrs[0]) * rxq->rxq_vq->vq_num);
    495      1.46  yamaguch 		P(p, txq->txq_hdrs,
    496      1.46  yamaguch 		    sizeof(txq->txq_hdrs[0]) * txq->txq_vq->vq_num);
    497      1.46  yamaguch 	}
    498      1.32  jdolecek 	if (sc->sc_has_ctrl) {
    499      1.43  yamaguch 		P(p, ctrlq->ctrlq_cmd, sizeof(*ctrlq->ctrlq_cmd));
    500      1.43  yamaguch 		P(p, ctrlq->ctrlq_status, sizeof(*ctrlq->ctrlq_status));
    501      1.43  yamaguch 		P(p, ctrlq->ctrlq_rx, sizeof(*ctrlq->ctrlq_rx));
    502      1.50  christos 		P(p, ctrlq->ctrlq_mac_tbl_uc, sizeof(*ctrlq->ctrlq_mac_tbl_uc));
    503      1.50  christos 		P(p, ctrlq->ctrlq_mac_tbl_mc, sizeof(*ctrlq->ctrlq_mac_tbl_mc)
    504      1.50  christos 		    + ETHER_ADDR_LEN * VIRTIO_NET_CTRL_MAC_MAXENTRIES);
    505      1.46  yamaguch 		P(p, ctrlq->ctrlq_mq, sizeof(*ctrlq->ctrlq_mq));
    506       1.1   hannken 	}
    507       1.1   hannken 
    508      1.46  yamaguch 	allocsize2 = 0;
    509      1.46  yamaguch 	for (qid = 0; qid < sc->sc_max_nvq_pairs; qid++) {
    510      1.46  yamaguch 		int rxqsize, txqsize;
    511      1.46  yamaguch 
    512      1.46  yamaguch 		rxq = &sc->sc_rxq[qid];
    513      1.46  yamaguch 		txq = &sc->sc_txq[qid];
    514      1.46  yamaguch 		rxqsize = rxq->rxq_vq->vq_num;
    515      1.46  yamaguch 		txqsize = txq->txq_vq->vq_num;
    516      1.46  yamaguch 
    517      1.46  yamaguch 		allocsize2 += sizeof(rxq->rxq_dmamaps[0]) * rxqsize;
    518      1.46  yamaguch 		allocsize2 += sizeof(rxq->rxq_hdr_dmamaps[0]) * rxqsize;
    519      1.46  yamaguch 		allocsize2 += sizeof(rxq->rxq_mbufs[0]) * rxqsize;
    520      1.46  yamaguch 
    521      1.46  yamaguch 		allocsize2 += sizeof(txq->txq_dmamaps[0]) * txqsize;
    522      1.46  yamaguch 		allocsize2 += sizeof(txq->txq_hdr_dmamaps[0]) * txqsize;
    523      1.46  yamaguch 		allocsize2 += sizeof(txq->txq_mbufs[0]) * txqsize;
    524      1.46  yamaguch 	}
    525      1.42  yamaguch 	vaddr = kmem_zalloc(allocsize2, KM_SLEEP);
    526      1.43  yamaguch 	sc->sc_kmem = vaddr;
    527      1.42  yamaguch 	p = (intptr_t) vaddr;
    528      1.42  yamaguch 
    529      1.46  yamaguch 	for (qid = 0; qid < sc->sc_max_nvq_pairs; qid++) {
    530      1.46  yamaguch 		int rxqsize, txqsize;
    531      1.46  yamaguch 		rxq = &sc->sc_rxq[qid];
    532      1.46  yamaguch 		txq = &sc->sc_txq[qid];
    533      1.46  yamaguch 		rxqsize = rxq->rxq_vq->vq_num;
    534      1.46  yamaguch 		txqsize = txq->txq_vq->vq_num;
    535      1.46  yamaguch 
    536      1.48   msaitoh 		P(p, rxq->rxq_hdr_dmamaps,
    537      1.48   msaitoh 		    sizeof(rxq->rxq_hdr_dmamaps[0]) * rxqsize);
    538      1.48   msaitoh 		P(p, txq->txq_hdr_dmamaps,
    539      1.48   msaitoh 		    sizeof(txq->txq_hdr_dmamaps[0]) * txqsize);
    540      1.46  yamaguch 		P(p, rxq->rxq_dmamaps, sizeof(rxq->rxq_dmamaps[0]) * rxqsize);
    541      1.46  yamaguch 		P(p, txq->txq_dmamaps, sizeof(txq->txq_dmamaps[0]) * txqsize);
    542      1.46  yamaguch 		P(p, rxq->rxq_mbufs, sizeof(rxq->rxq_mbufs[0]) * rxqsize);
    543      1.46  yamaguch 		P(p, txq->txq_mbufs, sizeof(txq->txq_mbufs[0]) * txqsize);
    544      1.46  yamaguch 	}
    545      1.42  yamaguch #undef P
    546       1.1   hannken 
    547      1.48   msaitoh #define C(map, size, nsegs, usage)					      \
    548      1.48   msaitoh 	do {								      \
    549      1.48   msaitoh 		r = bus_dmamap_create(virtio_dmat(vsc), size, nsegs, size, 0, \
    550      1.48   msaitoh 		    BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,			      \
    551      1.48   msaitoh 		    &map);						      \
    552      1.48   msaitoh 		if (r != 0) {						      \
    553      1.48   msaitoh 			aprint_error_dev(sc->sc_dev,			      \
    554      1.50  christos 			    usage " dmamap creation failed, "		      \
    555      1.50  christos 			    "error code %d\n", r);			      \
    556      1.48   msaitoh 			goto err_reqs;					      \
    557      1.48   msaitoh 		}							      \
    558       1.1   hannken 	} while (0)
    559      1.42  yamaguch #define C_L(map, buf, size, nsegs, rw, usage)				\
    560      1.42  yamaguch 	C(map, size, nsegs, usage);					\
    561       1.1   hannken 	do {								\
    562      1.42  yamaguch 		r = bus_dmamap_load(virtio_dmat(vsc), map,		\
    563      1.42  yamaguch 				    buf, size, NULL,			\
    564      1.42  yamaguch 				    rw | BUS_DMA_NOWAIT);		\
    565       1.1   hannken 		if (r != 0) {						\
    566       1.1   hannken 			aprint_error_dev(sc->sc_dev,			\
    567      1.50  christos 			    usage " dmamap load failed, "		\
    568      1.50  christos 			    "error code %d\n", r);			\
    569       1.1   hannken 			goto err_reqs;					\
    570       1.1   hannken 		}							\
    571       1.1   hannken 	} while (0)
    572       1.1   hannken 
    573      1.46  yamaguch 	for (qid = 0; qid < sc->sc_max_nvq_pairs; qid++) {
    574      1.46  yamaguch 		rxq = &sc->sc_rxq[qid];
    575      1.46  yamaguch 		txq = &sc->sc_txq[qid];
    576      1.46  yamaguch 
    577      1.46  yamaguch 		for (i = 0; i < rxq->rxq_vq->vq_num; i++) {
    578      1.46  yamaguch 			C_L(rxq->rxq_hdr_dmamaps[i], &rxq->rxq_hdrs[i],
    579      1.46  yamaguch 			    sizeof(rxq->rxq_hdrs[0]), 1,
    580      1.46  yamaguch 			    BUS_DMA_READ, "rx header");
    581      1.46  yamaguch 			C(rxq->rxq_dmamaps[i], MCLBYTES, 1, "rx payload");
    582      1.46  yamaguch 		}
    583      1.46  yamaguch 
    584      1.46  yamaguch 		for (i = 0; i < txq->txq_vq->vq_num; i++) {
    585      1.46  yamaguch 			C_L(txq->txq_hdr_dmamaps[i], &txq->txq_hdrs[i],
    586      1.46  yamaguch 			    sizeof(txq->txq_hdrs[0]), 1,
    587      1.46  yamaguch 			    BUS_DMA_READ, "tx header");
    588      1.46  yamaguch 			C(txq->txq_dmamaps[i], ETHER_MAX_LEN,
    589      1.46  yamaguch 			    VIRTIO_NET_TX_MAXNSEGS, "tx payload");
    590      1.46  yamaguch 		}
    591       1.1   hannken 	}
    592       1.1   hannken 
    593      1.32  jdolecek 	if (sc->sc_has_ctrl) {
    594       1.1   hannken 		/* control vq class & command */
    595      1.43  yamaguch 		C_L(ctrlq->ctrlq_cmd_dmamap,
    596      1.43  yamaguch 		    ctrlq->ctrlq_cmd, sizeof(*ctrlq->ctrlq_cmd), 1,
    597      1.42  yamaguch 		    BUS_DMA_WRITE, "control command");
    598      1.43  yamaguch 		C_L(ctrlq->ctrlq_status_dmamap,
    599      1.43  yamaguch 		    ctrlq->ctrlq_status, sizeof(*ctrlq->ctrlq_status), 1,
    600      1.42  yamaguch 		    BUS_DMA_READ, "control status");
    601       1.1   hannken 
    602       1.1   hannken 		/* control vq rx mode command parameter */
    603      1.43  yamaguch 		C_L(ctrlq->ctrlq_rx_dmamap,
    604      1.43  yamaguch 		    ctrlq->ctrlq_rx, sizeof(*ctrlq->ctrlq_rx), 1,
    605      1.42  yamaguch 		    BUS_DMA_WRITE, "rx mode control command");
    606       1.1   hannken 
    607      1.46  yamaguch 		/* multiqueue set command */
    608      1.46  yamaguch 		C_L(ctrlq->ctrlq_mq_dmamap,
    609      1.46  yamaguch 		    ctrlq->ctrlq_mq, sizeof(*ctrlq->ctrlq_mq), 1,
    610      1.46  yamaguch 		    BUS_DMA_WRITE, "multiqueue set command");
    611      1.46  yamaguch 
    612       1.1   hannken 		/* control vq MAC filter table for unicast */
    613       1.1   hannken 		/* do not load now since its length is variable */
    614      1.43  yamaguch 		C(ctrlq->ctrlq_tbl_uc_dmamap,
    615      1.43  yamaguch 		    sizeof(*ctrlq->ctrlq_mac_tbl_uc) + 0, 1,
    616      1.42  yamaguch 		    "unicast MAC address filter command");
    617       1.1   hannken 
    618       1.1   hannken 		/* control vq MAC filter table for multicast */
    619      1.43  yamaguch 		C(ctrlq->ctrlq_tbl_mc_dmamap,
    620      1.43  yamaguch 		    sizeof(*ctrlq->ctrlq_mac_tbl_mc)
    621      1.42  yamaguch 		    + ETHER_ADDR_LEN * VIRTIO_NET_CTRL_MAC_MAXENTRIES, 1,
    622      1.42  yamaguch 		    "multicast MAC address filter command");
    623       1.1   hannken 	}
    624      1.42  yamaguch #undef C_L
    625       1.1   hannken #undef C
    626       1.1   hannken 
    627       1.1   hannken 	return 0;
    628       1.1   hannken 
    629       1.1   hannken err_reqs:
    630       1.1   hannken #define D(map)								\
    631       1.1   hannken 	do {								\
    632      1.42  yamaguch 		if (map) {						\
    633      1.42  yamaguch 			bus_dmamap_destroy(virtio_dmat(vsc), map);	\
    634      1.42  yamaguch 			map = NULL;					\
    635       1.1   hannken 		}							\
    636       1.1   hannken 	} while (0)
    637      1.43  yamaguch 	D(ctrlq->ctrlq_tbl_mc_dmamap);
    638      1.43  yamaguch 	D(ctrlq->ctrlq_tbl_uc_dmamap);
    639      1.43  yamaguch 	D(ctrlq->ctrlq_rx_dmamap);
    640      1.43  yamaguch 	D(ctrlq->ctrlq_status_dmamap);
    641      1.43  yamaguch 	D(ctrlq->ctrlq_cmd_dmamap);
    642      1.46  yamaguch 	for (qid = 0; qid < sc->sc_max_nvq_pairs; qid++) {
    643      1.46  yamaguch 		rxq = &sc->sc_rxq[qid];
    644      1.46  yamaguch 		txq = &sc->sc_txq[qid];
    645      1.46  yamaguch 
    646      1.46  yamaguch 		for (i = 0; i < txq->txq_vq->vq_num; i++) {
    647      1.46  yamaguch 			D(txq->txq_dmamaps[i]);
    648      1.46  yamaguch 			D(txq->txq_hdr_dmamaps[i]);
    649      1.46  yamaguch 		}
    650      1.46  yamaguch 		for (i = 0; i < rxq->rxq_vq->vq_num; i++) {
    651      1.46  yamaguch 			D(rxq->rxq_dmamaps[i]);
    652      1.46  yamaguch 			D(rxq->rxq_hdr_dmamaps[i]);
    653      1.46  yamaguch 		}
    654       1.1   hannken 	}
    655       1.1   hannken #undef D
    656      1.43  yamaguch 	if (sc->sc_kmem) {
    657      1.43  yamaguch 		kmem_free(sc->sc_kmem, allocsize2);
    658      1.43  yamaguch 		sc->sc_kmem = NULL;
    659       1.1   hannken 	}
    660      1.43  yamaguch 	bus_dmamem_unmap(virtio_dmat(vsc), sc->sc_dmamem, allocsize);
    661       1.1   hannken err_dmamem_alloc:
    662      1.32  jdolecek 	bus_dmamem_free(virtio_dmat(vsc), &sc->sc_hdr_segs[0], 1);
    663       1.1   hannken err_none:
    664       1.1   hannken 	return -1;
    665       1.1   hannken }
    666       1.1   hannken 
    667       1.1   hannken static void
    668       1.1   hannken vioif_attach(device_t parent, device_t self, void *aux)
    669       1.1   hannken {
    670       1.1   hannken 	struct vioif_softc *sc = device_private(self);
    671       1.1   hannken 	struct virtio_softc *vsc = device_private(parent);
    672      1.43  yamaguch 	struct vioif_ctrlqueue *ctrlq = &sc->sc_ctrlq;
    673      1.46  yamaguch 	struct vioif_txqueue *txq;
    674      1.46  yamaguch 	struct vioif_rxqueue *rxq;
    675      1.46  yamaguch 	uint32_t features, req_features;
    676       1.1   hannken 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    677      1.43  yamaguch 	u_int softint_flags;
    678      1.46  yamaguch 	int r, i, nvqs=0, req_flags;
    679       1.1   hannken 
    680      1.32  jdolecek 	if (virtio_child(vsc) != NULL) {
    681       1.1   hannken 		aprint_normal(": child already attached for %s; "
    682      1.50  christos 		    "something wrong...\n", device_xname(parent));
    683       1.1   hannken 		return;
    684       1.1   hannken 	}
    685       1.1   hannken 
    686       1.1   hannken 	sc->sc_dev = self;
    687       1.1   hannken 	sc->sc_virtio = vsc;
    688      1.34     ozaki 	sc->sc_link_active = false;
    689       1.1   hannken 
    690      1.46  yamaguch 	sc->sc_max_nvq_pairs = 1;
    691      1.46  yamaguch 	sc->sc_req_nvq_pairs = 1;
    692      1.46  yamaguch 	sc->sc_act_nvq_pairs = 1;
    693      1.46  yamaguch 
    694      1.32  jdolecek 	req_flags = 0;
    695       1.1   hannken 
    696       1.7     ozaki #ifdef VIOIF_MPSAFE
    697      1.32  jdolecek 	req_flags |= VIRTIO_F_PCI_INTR_MPSAFE;
    698       1.7     ozaki #endif
    699      1.12     ozaki #ifdef VIOIF_SOFTINT_INTR
    700      1.32  jdolecek 	req_flags |= VIRTIO_F_PCI_INTR_SOFTINT;
    701      1.12     ozaki #endif
    702      1.32  jdolecek 	req_flags |= VIRTIO_F_PCI_INTR_MSIX;
    703      1.32  jdolecek 
    704      1.46  yamaguch 	req_features =
    705      1.46  yamaguch 	    VIRTIO_NET_F_MAC | VIRTIO_NET_F_STATUS | VIRTIO_NET_F_CTRL_VQ |
    706      1.46  yamaguch 	    VIRTIO_NET_F_CTRL_RX | VIRTIO_F_NOTIFY_ON_EMPTY;
    707      1.46  yamaguch #ifdef VIOIF_MULTIQ
    708      1.46  yamaguch 	req_features |= VIRTIO_NET_F_MQ;
    709      1.46  yamaguch #endif
    710      1.46  yamaguch 	virtio_child_attach_start(vsc, self, IPL_NET, NULL,
    711      1.34     ozaki 	    vioif_config_change, virtio_vq_intr, req_flags,
    712      1.46  yamaguch 	    req_features, VIRTIO_NET_FLAG_BITS);
    713      1.32  jdolecek 
    714      1.32  jdolecek 	features = virtio_features(vsc);
    715       1.7     ozaki 
    716       1.1   hannken 	if (features & VIRTIO_NET_F_MAC) {
    717      1.50  christos 		for (i = 0; i < __arraycount(sc->sc_mac); i++) {
    718      1.50  christos 			sc->sc_mac[i] = virtio_read_device_config_1(vsc,
    719      1.50  christos 			    VIRTIO_NET_CONFIG_MAC + i);
    720      1.50  christos 		}
    721       1.1   hannken 	} else {
    722       1.1   hannken 		/* code stolen from sys/net/if_tap.c */
    723       1.1   hannken 		struct timeval tv;
    724       1.1   hannken 		uint32_t ui;
    725       1.1   hannken 		getmicrouptime(&tv);
    726       1.1   hannken 		ui = (tv.tv_sec ^ tv.tv_usec) & 0xffffff;
    727       1.1   hannken 		memcpy(sc->sc_mac+3, (uint8_t *)&ui, 3);
    728      1.50  christos 		for (i = 0; i < __arraycount(sc->sc_mac); i++) {
    729      1.50  christos 			virtio_write_device_config_1(vsc,
    730      1.50  christos 			    VIRTIO_NET_CONFIG_MAC + i, sc->sc_mac[i]);
    731      1.50  christos 		}
    732       1.1   hannken 	}
    733      1.32  jdolecek 
    734      1.50  christos 	aprint_normal_dev(self, "Ethernet address %s\n",
    735      1.50  christos 	    ether_sprintf(sc->sc_mac));
    736       1.1   hannken 
    737      1.46  yamaguch 	if ((features & VIRTIO_NET_F_CTRL_VQ) &&
    738      1.46  yamaguch 	    (features & VIRTIO_NET_F_CTRL_RX)) {
    739      1.46  yamaguch 		sc->sc_has_ctrl = true;
    740      1.46  yamaguch 
    741      1.46  yamaguch 		cv_init(&ctrlq->ctrlq_wait, "ctrl_vq");
    742      1.46  yamaguch 		mutex_init(&ctrlq->ctrlq_wait_lock, MUTEX_DEFAULT, IPL_NET);
    743      1.46  yamaguch 		ctrlq->ctrlq_inuse = FREE;
    744      1.46  yamaguch 	} else {
    745      1.46  yamaguch 		sc->sc_has_ctrl = false;
    746      1.46  yamaguch 	}
    747      1.46  yamaguch 
    748      1.46  yamaguch 	if (sc->sc_has_ctrl && (features & VIRTIO_NET_F_MQ)) {
    749      1.46  yamaguch 		sc->sc_max_nvq_pairs = virtio_read_device_config_2(vsc,
    750      1.46  yamaguch 		    VIRTIO_NET_CONFIG_MAX_VQ_PAIRS);
    751      1.46  yamaguch 
    752      1.46  yamaguch 		if (sc->sc_max_nvq_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX)
    753      1.46  yamaguch 			goto err;
    754      1.46  yamaguch 
    755      1.46  yamaguch 		/* Limit the number of queue pairs to use */
    756      1.46  yamaguch 		sc->sc_req_nvq_pairs = MIN(sc->sc_max_nvq_pairs, ncpu);
    757      1.46  yamaguch 	}
    758      1.46  yamaguch 
    759      1.51       chs 	vioif_alloc_queues(sc);
    760      1.46  yamaguch 	virtio_child_attach_set_vqs(vsc, sc->sc_vqs, sc->sc_req_nvq_pairs);
    761      1.46  yamaguch 
    762      1.43  yamaguch #ifdef VIOIF_MPSAFE
    763      1.43  yamaguch 	softint_flags = SOFTINT_NET | SOFTINT_MPSAFE;
    764      1.43  yamaguch #else
    765      1.43  yamaguch 	softint_flags = SOFTINT_NET;
    766      1.43  yamaguch #endif
    767       1.7     ozaki 
    768      1.17     ozaki 	/*
    769      1.46  yamaguch 	 * Allocating a virtqueues
    770      1.17     ozaki 	 */
    771      1.46  yamaguch 	for (i = 0; i < sc->sc_max_nvq_pairs; i++) {
    772      1.46  yamaguch 		rxq = &sc->sc_rxq[i];
    773      1.46  yamaguch 		txq = &sc->sc_txq[i];
    774      1.46  yamaguch 		char qname[32];
    775      1.46  yamaguch 
    776      1.46  yamaguch 		rxq->rxq_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NET);
    777      1.46  yamaguch 
    778      1.50  christos 		rxq->rxq_softint = softint_establish(softint_flags,
    779      1.50  christos 		    vioif_rx_softint, rxq);
    780      1.46  yamaguch 		if (rxq->rxq_softint == NULL) {
    781      1.46  yamaguch 			aprint_error_dev(self, "cannot establish rx softint\n");
    782      1.46  yamaguch 			goto err;
    783      1.46  yamaguch 		}
    784      1.46  yamaguch 		snprintf(qname, sizeof(qname), "rx%d", i);
    785      1.46  yamaguch 		r = virtio_alloc_vq(vsc, rxq->rxq_vq, nvqs,
    786      1.46  yamaguch 		    MCLBYTES+sizeof(struct virtio_net_hdr), nvqs, qname);
    787      1.46  yamaguch 		if (r != 0)
    788      1.46  yamaguch 			goto err;
    789      1.46  yamaguch 		nvqs++;
    790      1.46  yamaguch 		rxq->rxq_vq->vq_done = vioif_rx_vq_done;
    791      1.46  yamaguch 		rxq->rxq_vq->vq_done_ctx = (void *)rxq;
    792      1.46  yamaguch 		rxq->rxq_stopping = true;
    793      1.46  yamaguch 
    794      1.46  yamaguch 		txq->txq_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NET);
    795      1.46  yamaguch 		txq->txq_deferred_transmit = softint_establish(softint_flags,
    796      1.46  yamaguch 		    vioif_deferred_transmit, txq);
    797      1.46  yamaguch 		if (txq->txq_deferred_transmit == NULL) {
    798      1.46  yamaguch 			aprint_error_dev(self, "cannot establish tx softint\n");
    799      1.46  yamaguch 			goto err;
    800      1.46  yamaguch 		}
    801      1.46  yamaguch 		snprintf(qname, sizeof(qname), "tx%d", i);
    802      1.46  yamaguch 		r = virtio_alloc_vq(vsc, txq->txq_vq, nvqs,
    803      1.50  christos 		    sizeof(struct virtio_net_hdr)
    804      1.50  christos 		    + (ETHER_MAX_LEN - ETHER_HDR_LEN),
    805      1.46  yamaguch 		    VIRTIO_NET_TX_MAXNSEGS + 1, qname);
    806      1.46  yamaguch 		if (r != 0)
    807      1.46  yamaguch 			goto err;
    808      1.46  yamaguch 		nvqs++;
    809      1.46  yamaguch 		txq->txq_vq->vq_done = vioif_tx_vq_done;
    810      1.46  yamaguch 		txq->txq_vq->vq_done_ctx = (void *)txq;
    811      1.46  yamaguch 		txq->txq_link_active = sc->sc_link_active;
    812      1.46  yamaguch 		txq->txq_stopping = false;
    813      1.51       chs 		txq->txq_intrq = pcq_create(txq->txq_vq->vq_num, KM_SLEEP);
    814      1.43  yamaguch 	}
    815      1.17     ozaki 
    816      1.46  yamaguch 	if (sc->sc_has_ctrl) {
    817      1.17     ozaki 		/*
    818      1.17     ozaki 		 * Allocating a virtqueue for control channel
    819      1.17     ozaki 		 */
    820      1.46  yamaguch 		r = virtio_alloc_vq(vsc, ctrlq->ctrlq_vq, nvqs,
    821      1.17     ozaki 		    NBPG, 1, "control");
    822      1.17     ozaki 		if (r != 0) {
    823      1.17     ozaki 			aprint_error_dev(self, "failed to allocate "
    824      1.50  christos 			    "a virtqueue for control channel, error code %d\n",
    825      1.50  christos 			    r);
    826      1.46  yamaguch 
    827      1.46  yamaguch 			sc->sc_has_ctrl = false;
    828      1.46  yamaguch 			cv_destroy(&ctrlq->ctrlq_wait);
    829      1.46  yamaguch 			mutex_destroy(&ctrlq->ctrlq_wait_lock);
    830      1.46  yamaguch 		} else {
    831      1.46  yamaguch 			nvqs++;
    832      1.46  yamaguch 			ctrlq->ctrlq_vq->vq_done = vioif_ctrl_vq_done;
    833      1.46  yamaguch 			ctrlq->ctrlq_vq->vq_done_ctx = (void *) ctrlq;
    834       1.1   hannken 		}
    835       1.1   hannken 	}
    836      1.34     ozaki 
    837      1.48   msaitoh 	sc->sc_ctl_softint = softint_establish(softint_flags,
    838      1.48   msaitoh 	    vioif_ctl_softint, sc);
    839      1.34     ozaki 	if (sc->sc_ctl_softint == NULL) {
    840      1.34     ozaki 		aprint_error_dev(self, "cannot establish ctl softint\n");
    841       1.1   hannken 		goto err;
    842       1.1   hannken 	}
    843       1.1   hannken 
    844       1.1   hannken 	if (vioif_alloc_mems(sc) < 0)
    845       1.1   hannken 		goto err;
    846       1.1   hannken 
    847      1.32  jdolecek 	if (virtio_child_attach_finish(vsc) != 0)
    848      1.32  jdolecek 		goto err;
    849      1.32  jdolecek 
    850       1.1   hannken 	strlcpy(ifp->if_xname, device_xname(self), IFNAMSIZ);
    851       1.1   hannken 	ifp->if_softc = sc;
    852       1.1   hannken 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    853      1.45  yamaguch #ifdef VIOIF_MPSAFE
    854      1.45  yamaguch 	ifp->if_extflags = IFEF_MPSAFE;
    855      1.45  yamaguch #endif
    856       1.1   hannken 	ifp->if_start = vioif_start;
    857      1.46  yamaguch 	if (sc->sc_req_nvq_pairs > 1)
    858      1.46  yamaguch 		ifp->if_transmit = vioif_transmit;
    859       1.1   hannken 	ifp->if_ioctl = vioif_ioctl;
    860       1.1   hannken 	ifp->if_init = vioif_init;
    861       1.1   hannken 	ifp->if_stop = vioif_stop;
    862       1.1   hannken 	ifp->if_capabilities = 0;
    863       1.1   hannken 	ifp->if_watchdog = vioif_watchdog;
    864      1.46  yamaguch 	txq = &sc->sc_txq[0];
    865      1.43  yamaguch 	IFQ_SET_MAXLEN(&ifp->if_snd, MAX(txq->txq_vq->vq_num, IFQ_MAXLEN));
    866      1.36  jdolecek 	IFQ_SET_READY(&ifp->if_snd);
    867       1.1   hannken 
    868      1.11     ozaki 	sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
    869      1.11     ozaki 
    870       1.1   hannken 	if_attach(ifp);
    871      1.28     ozaki 	if_deferred_start_init(ifp, NULL);
    872       1.1   hannken 	ether_ifattach(ifp, sc->sc_mac);
    873       1.1   hannken 
    874       1.1   hannken 	return;
    875       1.1   hannken 
    876       1.1   hannken err:
    877      1.46  yamaguch 	for (i = 0; i < sc->sc_max_nvq_pairs; i++) {
    878      1.46  yamaguch 		rxq = &sc->sc_rxq[i];
    879      1.46  yamaguch 		txq = &sc->sc_txq[i];
    880      1.46  yamaguch 
    881      1.46  yamaguch 		if (rxq->rxq_lock) {
    882      1.46  yamaguch 			mutex_obj_free(rxq->rxq_lock);
    883      1.46  yamaguch 			rxq->rxq_lock = NULL;
    884      1.46  yamaguch 		}
    885      1.46  yamaguch 
    886      1.46  yamaguch 		if (rxq->rxq_softint) {
    887      1.46  yamaguch 			softint_disestablish(rxq->rxq_softint);
    888      1.46  yamaguch 			rxq->rxq_softint = NULL;
    889      1.46  yamaguch 		}
    890      1.46  yamaguch 
    891      1.46  yamaguch 		if (txq->txq_lock) {
    892      1.46  yamaguch 			mutex_obj_free(txq->txq_lock);
    893      1.46  yamaguch 			txq->txq_lock = NULL;
    894      1.46  yamaguch 		}
    895      1.43  yamaguch 
    896      1.46  yamaguch 		if (txq->txq_deferred_transmit) {
    897      1.46  yamaguch 			softint_disestablish(txq->txq_deferred_transmit);
    898      1.46  yamaguch 			txq->txq_deferred_transmit = NULL;
    899      1.46  yamaguch 		}
    900      1.43  yamaguch 
    901      1.46  yamaguch 		if (txq->txq_intrq) {
    902      1.46  yamaguch 			pcq_destroy(txq->txq_intrq);
    903      1.46  yamaguch 			txq->txq_intrq = NULL;
    904      1.46  yamaguch 		}
    905      1.43  yamaguch 	}
    906       1.7     ozaki 
    907      1.32  jdolecek 	if (sc->sc_has_ctrl) {
    908      1.43  yamaguch 		cv_destroy(&ctrlq->ctrlq_wait);
    909      1.43  yamaguch 		mutex_destroy(&ctrlq->ctrlq_wait_lock);
    910       1.1   hannken 	}
    911      1.20  christos 
    912      1.32  jdolecek 	while (nvqs > 0)
    913      1.46  yamaguch 		virtio_free_vq(vsc, &sc->sc_vqs[--nvqs]);
    914      1.46  yamaguch 
    915      1.46  yamaguch 	vioif_free_queues(sc);
    916      1.20  christos 
    917      1.32  jdolecek 	virtio_child_attach_failed(vsc);
    918       1.1   hannken 	return;
    919       1.1   hannken }
    920       1.1   hannken 
    921       1.1   hannken /* we need interrupts to make promiscuous mode off */
    922       1.1   hannken static void
    923       1.1   hannken vioif_deferred_init(device_t self)
    924       1.1   hannken {
    925       1.1   hannken 	struct vioif_softc *sc = device_private(self);
    926       1.1   hannken 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    927       1.1   hannken 	int r;
    928       1.1   hannken 
    929       1.9     ozaki 	if (ifp->if_flags & IFF_PROMISC)
    930      1.10     ozaki 		return;
    931       1.9     ozaki 
    932       1.1   hannken 	r =  vioif_set_promisc(sc, false);
    933       1.1   hannken 	if (r != 0)
    934       1.1   hannken 		aprint_error_dev(self, "resetting promisc mode failed, "
    935      1.50  christos 		    "error code %d\n", r);
    936       1.1   hannken }
    937       1.1   hannken 
    938      1.46  yamaguch static void
    939      1.46  yamaguch vioif_enable_interrupt_vqpairs(struct vioif_softc *sc)
    940      1.46  yamaguch {
    941      1.46  yamaguch 	struct virtio_softc *vsc = sc->sc_virtio;
    942      1.46  yamaguch 	struct vioif_txqueue *txq;
    943      1.46  yamaguch 	struct vioif_rxqueue *rxq;
    944      1.46  yamaguch 	int i;
    945      1.46  yamaguch 
    946      1.46  yamaguch 	for (i = 0; i < sc->sc_act_nvq_pairs; i++) {
    947      1.46  yamaguch 		txq = &sc->sc_txq[i];
    948      1.46  yamaguch 		rxq = &sc->sc_rxq[i];
    949      1.46  yamaguch 
    950      1.46  yamaguch 		virtio_start_vq_intr(vsc, txq->txq_vq);
    951      1.46  yamaguch 		virtio_start_vq_intr(vsc, rxq->rxq_vq);
    952      1.46  yamaguch 	}
    953      1.46  yamaguch }
    954      1.46  yamaguch 
    955      1.46  yamaguch static void
    956      1.46  yamaguch vioif_disable_interrupt_vqpairs(struct vioif_softc *sc)
    957      1.46  yamaguch {
    958      1.46  yamaguch 	struct virtio_softc *vsc = sc->sc_virtio;
    959      1.46  yamaguch 	struct vioif_txqueue *txq;
    960      1.46  yamaguch 	struct vioif_rxqueue *rxq;
    961      1.46  yamaguch 	int i;
    962      1.46  yamaguch 
    963      1.46  yamaguch 	for (i = 0; i < sc->sc_act_nvq_pairs; i++) {
    964      1.46  yamaguch 		txq = &sc->sc_txq[i];
    965      1.46  yamaguch 		rxq = &sc->sc_rxq[i];
    966      1.46  yamaguch 
    967      1.46  yamaguch 		virtio_stop_vq_intr(vsc, txq->txq_vq);
    968      1.46  yamaguch 		virtio_stop_vq_intr(vsc, rxq->rxq_vq);
    969      1.46  yamaguch 	}
    970      1.46  yamaguch }
    971      1.46  yamaguch 
    972       1.1   hannken /*
    973       1.1   hannken  * Interface functions for ifnet
    974       1.1   hannken  */
    975       1.1   hannken static int
    976       1.1   hannken vioif_init(struct ifnet *ifp)
    977       1.1   hannken {
    978       1.1   hannken 	struct vioif_softc *sc = ifp->if_softc;
    979      1.33     ozaki 	struct virtio_softc *vsc = sc->sc_virtio;
    980      1.46  yamaguch 	struct vioif_rxqueue *rxq;
    981      1.43  yamaguch 	struct vioif_ctrlqueue *ctrlq = &sc->sc_ctrlq;
    982      1.46  yamaguch 	int r, i;
    983       1.1   hannken 
    984       1.1   hannken 	vioif_stop(ifp, 0);
    985       1.7     ozaki 
    986      1.33     ozaki 	virtio_reinit_start(vsc);
    987      1.33     ozaki 	virtio_negotiate_features(vsc, virtio_features(vsc));
    988      1.46  yamaguch 
    989      1.46  yamaguch 	for (i = 0; i < sc->sc_req_nvq_pairs; i++) {
    990      1.46  yamaguch 		rxq = &sc->sc_rxq[i];
    991      1.46  yamaguch 
    992      1.46  yamaguch 		/* Have to set false before vioif_populate_rx_mbufs */
    993      1.46  yamaguch 		rxq->rxq_stopping = false;
    994      1.46  yamaguch 		vioif_populate_rx_mbufs(rxq);
    995      1.46  yamaguch 	}
    996      1.46  yamaguch 
    997      1.46  yamaguch 	virtio_reinit_end(vsc);
    998      1.47  yamaguch 
    999      1.47  yamaguch 	if (sc->sc_has_ctrl)
   1000      1.43  yamaguch 		virtio_start_vq_intr(vsc, ctrlq->ctrlq_vq);
   1001      1.46  yamaguch 
   1002      1.46  yamaguch 	r = vioif_ctrl_mq_vq_pairs_set(sc, sc->sc_req_nvq_pairs);
   1003      1.46  yamaguch 	if (r == 0)
   1004      1.46  yamaguch 		sc->sc_act_nvq_pairs = sc->sc_req_nvq_pairs;
   1005      1.46  yamaguch 	else
   1006      1.46  yamaguch 		sc->sc_act_nvq_pairs = 1;
   1007      1.46  yamaguch 
   1008      1.46  yamaguch 	for (i = 0; i < sc->sc_act_nvq_pairs; i++)
   1009      1.46  yamaguch 		sc->sc_txq[i].txq_stopping = false;
   1010      1.46  yamaguch 
   1011      1.46  yamaguch 	vioif_enable_interrupt_vqpairs(sc);
   1012      1.33     ozaki 
   1013       1.8     pooka 	if (!sc->sc_deferred_init_done) {
   1014       1.8     pooka 		sc->sc_deferred_init_done = 1;
   1015      1.32  jdolecek 		if (sc->sc_has_ctrl)
   1016       1.8     pooka 			vioif_deferred_init(sc->sc_dev);
   1017       1.8     pooka 	}
   1018       1.8     pooka 
   1019      1.34     ozaki 	vioif_update_link_status(sc);
   1020       1.1   hannken 	ifp->if_flags |= IFF_RUNNING;
   1021       1.1   hannken 	ifp->if_flags &= ~IFF_OACTIVE;
   1022       1.1   hannken 	vioif_rx_filter(sc);
   1023       1.1   hannken 
   1024       1.1   hannken 	return 0;
   1025       1.1   hannken }
   1026       1.1   hannken 
   1027       1.1   hannken static void
   1028       1.1   hannken vioif_stop(struct ifnet *ifp, int disable)
   1029       1.1   hannken {
   1030       1.1   hannken 	struct vioif_softc *sc = ifp->if_softc;
   1031       1.1   hannken 	struct virtio_softc *vsc = sc->sc_virtio;
   1032      1.46  yamaguch 	struct vioif_txqueue *txq;
   1033      1.46  yamaguch 	struct vioif_rxqueue *rxq;
   1034      1.43  yamaguch 	struct vioif_ctrlqueue *ctrlq = &sc->sc_ctrlq;
   1035      1.46  yamaguch 	int i;
   1036       1.1   hannken 
   1037      1.13     ozaki 	/* Take the locks to ensure that ongoing TX/RX finish */
   1038      1.46  yamaguch 	for (i = 0; i < sc->sc_act_nvq_pairs; i++) {
   1039      1.46  yamaguch 		txq = &sc->sc_txq[i];
   1040      1.46  yamaguch 		rxq = &sc->sc_rxq[i];
   1041      1.46  yamaguch 
   1042      1.46  yamaguch 		mutex_enter(txq->txq_lock);
   1043      1.46  yamaguch 		txq->txq_stopping = true;
   1044      1.46  yamaguch 		mutex_exit(txq->txq_lock);
   1045      1.46  yamaguch 
   1046      1.46  yamaguch 		mutex_enter(rxq->rxq_lock);
   1047      1.46  yamaguch 		rxq->rxq_stopping = true;
   1048      1.46  yamaguch 		mutex_exit(rxq->rxq_lock);
   1049      1.46  yamaguch 	}
   1050       1.7     ozaki 
   1051      1.33     ozaki 	/* disable interrupts */
   1052      1.46  yamaguch 	vioif_disable_interrupt_vqpairs(sc);
   1053      1.46  yamaguch 
   1054      1.33     ozaki 	if (sc->sc_has_ctrl)
   1055      1.43  yamaguch 		virtio_stop_vq_intr(vsc, ctrlq->ctrlq_vq);
   1056      1.33     ozaki 
   1057       1.1   hannken 	/* only way to stop I/O and DMA is resetting... */
   1058       1.1   hannken 	virtio_reset(vsc);
   1059      1.46  yamaguch 	for (i = 0; i < sc->sc_act_nvq_pairs; i++)
   1060      1.46  yamaguch 		vioif_rx_deq(&sc->sc_rxq[i]);
   1061      1.46  yamaguch 
   1062       1.1   hannken 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1063      1.34     ozaki 	sc->sc_link_active = false;
   1064       1.1   hannken 
   1065      1.46  yamaguch 	for (i = 0; i < sc->sc_act_nvq_pairs; i++) {
   1066      1.46  yamaguch 		txq = &sc->sc_txq[i];
   1067      1.46  yamaguch 		rxq = &sc->sc_rxq[i];
   1068      1.46  yamaguch 
   1069      1.46  yamaguch 		txq->txq_link_active = false;
   1070      1.46  yamaguch 
   1071      1.46  yamaguch 		if (disable)
   1072      1.46  yamaguch 			vioif_rx_drain(rxq);
   1073      1.46  yamaguch 
   1074      1.46  yamaguch 		vioif_tx_drain(txq);
   1075      1.46  yamaguch 	}
   1076       1.1   hannken }
   1077       1.1   hannken 
   1078       1.1   hannken static void
   1079      1.48   msaitoh vioif_send_common_locked(struct ifnet *ifp, struct vioif_txqueue *txq,
   1080      1.48   msaitoh     bool is_transmit)
   1081       1.1   hannken {
   1082       1.1   hannken 	struct vioif_softc *sc = ifp->if_softc;
   1083       1.1   hannken 	struct virtio_softc *vsc = sc->sc_virtio;
   1084      1.43  yamaguch 	struct virtqueue *vq = txq->txq_vq;
   1085       1.1   hannken 	struct mbuf *m;
   1086      1.36  jdolecek 	int queued = 0;
   1087       1.1   hannken 
   1088      1.46  yamaguch 	KASSERT(mutex_owned(txq->txq_lock));
   1089      1.46  yamaguch 
   1090      1.46  yamaguch 	if ((ifp->if_flags & IFF_RUNNING) == 0)
   1091      1.46  yamaguch 		return;
   1092       1.7     ozaki 
   1093      1.46  yamaguch 	if (!txq->txq_link_active || txq->txq_stopping)
   1094      1.46  yamaguch 		return;
   1095       1.7     ozaki 
   1096      1.46  yamaguch 	if ((ifp->if_flags & IFF_OACTIVE) != 0 && !is_transmit)
   1097      1.46  yamaguch 		return;
   1098       1.1   hannken 
   1099       1.2  jmcneill 	for (;;) {
   1100       1.1   hannken 		int slot, r;
   1101       1.1   hannken 
   1102      1.46  yamaguch 		if (is_transmit)
   1103      1.46  yamaguch 			m = pcq_get(txq->txq_intrq);
   1104      1.46  yamaguch 		else
   1105      1.46  yamaguch 			IFQ_DEQUEUE(&ifp->if_snd, m);
   1106      1.46  yamaguch 
   1107       1.2  jmcneill 		if (m == NULL)
   1108       1.2  jmcneill 			break;
   1109       1.2  jmcneill 
   1110       1.1   hannken 		r = virtio_enqueue_prep(vsc, vq, &slot);
   1111       1.1   hannken 		if (r == EAGAIN) {
   1112       1.1   hannken 			ifp->if_flags |= IFF_OACTIVE;
   1113      1.36  jdolecek 			m_freem(m);
   1114      1.36  jdolecek 			break;
   1115       1.1   hannken 		}
   1116       1.1   hannken 		if (r != 0)
   1117       1.1   hannken 			panic("enqueue_prep for a tx buffer");
   1118      1.36  jdolecek 
   1119      1.32  jdolecek 		r = bus_dmamap_load_mbuf(virtio_dmat(vsc),
   1120      1.50  christos 		    txq->txq_dmamaps[slot], m, BUS_DMA_WRITE | BUS_DMA_NOWAIT);
   1121       1.1   hannken 		if (r != 0) {
   1122      1.36  jdolecek 			/* maybe just too fragmented */
   1123      1.36  jdolecek 			struct mbuf *newm;
   1124      1.36  jdolecek 
   1125      1.36  jdolecek 			newm = m_defrag(m, M_NOWAIT);
   1126      1.36  jdolecek 			if (newm == NULL) {
   1127      1.36  jdolecek 				aprint_error_dev(sc->sc_dev,
   1128      1.36  jdolecek 				    "m_defrag() failed\n");
   1129      1.36  jdolecek 				goto skip;
   1130      1.36  jdolecek 			}
   1131      1.36  jdolecek 
   1132      1.37  jdolecek 			m = newm;
   1133      1.36  jdolecek 			r = bus_dmamap_load_mbuf(virtio_dmat(vsc),
   1134      1.50  christos 			    txq->txq_dmamaps[slot], m,
   1135      1.50  christos 			    BUS_DMA_WRITE | BUS_DMA_NOWAIT);
   1136      1.36  jdolecek 			if (r != 0) {
   1137      1.36  jdolecek 				aprint_error_dev(sc->sc_dev,
   1138      1.49   msaitoh 				    "tx dmamap load failed, error code %d\n",
   1139      1.36  jdolecek 				    r);
   1140      1.36  jdolecek skip:
   1141      1.37  jdolecek 				m_freem(m);
   1142      1.36  jdolecek 				virtio_enqueue_abort(vsc, vq, slot);
   1143      1.36  jdolecek 				continue;
   1144      1.36  jdolecek 			}
   1145       1.1   hannken 		}
   1146      1.36  jdolecek 
   1147      1.36  jdolecek 		/* This should actually never fail */
   1148       1.1   hannken 		r = virtio_enqueue_reserve(vsc, vq, slot,
   1149      1.43  yamaguch 					txq->txq_dmamaps[slot]->dm_nsegs + 1);
   1150       1.1   hannken 		if (r != 0) {
   1151      1.36  jdolecek 			aprint_error_dev(sc->sc_dev,
   1152      1.49   msaitoh 			    "virtio_enqueue_reserve failed, error code %d\n",
   1153      1.36  jdolecek 			    r);
   1154      1.32  jdolecek 			bus_dmamap_unload(virtio_dmat(vsc),
   1155      1.43  yamaguch 					  txq->txq_dmamaps[slot]);
   1156      1.36  jdolecek 			/* slot already freed by virtio_enqueue_reserve */
   1157      1.37  jdolecek 			m_freem(m);
   1158      1.36  jdolecek 			continue;
   1159       1.1   hannken 		}
   1160       1.7     ozaki 
   1161      1.43  yamaguch 		txq->txq_mbufs[slot] = m;
   1162       1.1   hannken 
   1163      1.43  yamaguch 		memset(&txq->txq_hdrs[slot], 0, sizeof(struct virtio_net_hdr));
   1164      1.43  yamaguch 		bus_dmamap_sync(virtio_dmat(vsc), txq->txq_dmamaps[slot],
   1165      1.50  christos 		    0, txq->txq_dmamaps[slot]->dm_mapsize,
   1166      1.50  christos 		    BUS_DMASYNC_PREWRITE);
   1167      1.43  yamaguch 		bus_dmamap_sync(virtio_dmat(vsc), txq->txq_hdr_dmamaps[slot],
   1168      1.50  christos 		    0, txq->txq_hdr_dmamaps[slot]->dm_mapsize,
   1169      1.50  christos 		    BUS_DMASYNC_PREWRITE);
   1170      1.43  yamaguch 		virtio_enqueue(vsc, vq, slot, txq->txq_hdr_dmamaps[slot], true);
   1171      1.43  yamaguch 		virtio_enqueue(vsc, vq, slot, txq->txq_dmamaps[slot], true);
   1172       1.1   hannken 		virtio_enqueue_commit(vsc, vq, slot, false);
   1173      1.36  jdolecek 
   1174       1.1   hannken 		queued++;
   1175      1.41   msaitoh 		bpf_mtap(ifp, m, BPF_D_OUT);
   1176       1.1   hannken 	}
   1177       1.1   hannken 
   1178       1.1   hannken 	if (queued > 0) {
   1179       1.1   hannken 		virtio_enqueue_commit(vsc, vq, -1, true);
   1180       1.1   hannken 		ifp->if_timer = 5;
   1181       1.1   hannken 	}
   1182      1.46  yamaguch }
   1183      1.46  yamaguch 
   1184      1.46  yamaguch static void
   1185      1.46  yamaguch vioif_start_locked(struct ifnet *ifp, struct vioif_txqueue *txq)
   1186      1.46  yamaguch {
   1187      1.46  yamaguch 
   1188      1.46  yamaguch 	/*
   1189      1.46  yamaguch 	 * ifp->if_obytes and ifp->if_omcasts are added in if_transmit()@if.c.
   1190      1.46  yamaguch 	 */
   1191      1.46  yamaguch 	vioif_send_common_locked(ifp, txq, false);
   1192      1.46  yamaguch 
   1193      1.46  yamaguch }
   1194      1.46  yamaguch 
   1195      1.46  yamaguch static void
   1196      1.46  yamaguch vioif_start(struct ifnet *ifp)
   1197      1.46  yamaguch {
   1198      1.46  yamaguch 	struct vioif_softc *sc = ifp->if_softc;
   1199      1.46  yamaguch 	struct vioif_txqueue *txq = &sc->sc_txq[0];
   1200      1.46  yamaguch 
   1201      1.46  yamaguch #ifdef VIOIF_MPSAFE
   1202      1.46  yamaguch 	KASSERT(if_is_mpsafe(ifp));
   1203      1.46  yamaguch #endif
   1204      1.46  yamaguch 
   1205      1.46  yamaguch 	mutex_enter(txq->txq_lock);
   1206      1.46  yamaguch 	if (!txq->txq_stopping)
   1207      1.46  yamaguch 		vioif_start_locked(ifp, txq);
   1208      1.46  yamaguch 	mutex_exit(txq->txq_lock);
   1209      1.46  yamaguch }
   1210      1.46  yamaguch 
   1211      1.46  yamaguch static inline int
   1212      1.46  yamaguch vioif_select_txqueue(struct ifnet *ifp, struct mbuf *m)
   1213      1.46  yamaguch {
   1214      1.46  yamaguch 	struct vioif_softc *sc = ifp->if_softc;
   1215      1.46  yamaguch 	u_int cpuid = cpu_index(curcpu());
   1216      1.46  yamaguch 
   1217      1.46  yamaguch 	return cpuid % sc->sc_act_nvq_pairs;
   1218      1.46  yamaguch }
   1219      1.46  yamaguch 
   1220      1.46  yamaguch static void
   1221      1.46  yamaguch vioif_transmit_locked(struct ifnet *ifp, struct vioif_txqueue *txq)
   1222      1.46  yamaguch {
   1223      1.46  yamaguch 
   1224      1.46  yamaguch 	vioif_send_common_locked(ifp, txq, true);
   1225      1.46  yamaguch }
   1226      1.46  yamaguch 
   1227      1.46  yamaguch static int
   1228      1.46  yamaguch vioif_transmit(struct ifnet *ifp, struct mbuf *m)
   1229      1.46  yamaguch {
   1230      1.46  yamaguch 	struct vioif_softc *sc = ifp->if_softc;
   1231      1.46  yamaguch 	struct vioif_txqueue *txq;
   1232      1.46  yamaguch 	int qid;
   1233      1.46  yamaguch 
   1234      1.46  yamaguch 	qid = vioif_select_txqueue(ifp, m);
   1235      1.46  yamaguch 	txq = &sc->sc_txq[qid];
   1236      1.46  yamaguch 
   1237      1.46  yamaguch 	if (__predict_false(!pcq_put(txq->txq_intrq, m))) {
   1238      1.46  yamaguch 		m_freem(m);
   1239      1.46  yamaguch 		return ENOBUFS;
   1240      1.46  yamaguch 	}
   1241      1.46  yamaguch 
   1242  1.51.2.1        ad 	net_stat_ref_t nsr = IF_STAT_GETREF(ifp);
   1243  1.51.2.1        ad 	if_statadd_ref(nsr, if_obytes, m->m_pkthdr.len);
   1244      1.46  yamaguch 	if (m->m_flags & M_MCAST)
   1245  1.51.2.1        ad 		if_statinc_ref(nsr, if_omcasts);
   1246  1.51.2.1        ad 	IF_STAT_PUTREF(ifp);
   1247      1.46  yamaguch 
   1248      1.46  yamaguch 	if (mutex_tryenter(txq->txq_lock)) {
   1249      1.46  yamaguch 		if (!txq->txq_stopping)
   1250      1.46  yamaguch 			vioif_transmit_locked(ifp, txq);
   1251      1.46  yamaguch 		mutex_exit(txq->txq_lock);
   1252      1.46  yamaguch 	}
   1253      1.46  yamaguch 
   1254      1.46  yamaguch 	return 0;
   1255      1.46  yamaguch }
   1256      1.46  yamaguch 
   1257      1.46  yamaguch static void
   1258      1.46  yamaguch vioif_deferred_transmit(void *arg)
   1259      1.46  yamaguch {
   1260      1.46  yamaguch 	struct vioif_txqueue *txq = arg;
   1261      1.46  yamaguch 	struct virtio_softc *vsc = txq->txq_vq->vq_owner;
   1262      1.46  yamaguch 	struct vioif_softc *sc = device_private(virtio_child(vsc));
   1263      1.46  yamaguch 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1264       1.7     ozaki 
   1265      1.46  yamaguch 	if (mutex_tryenter(txq->txq_lock)) {
   1266      1.46  yamaguch 		vioif_send_common_locked(ifp, txq, true);
   1267      1.46  yamaguch 		mutex_exit(txq->txq_lock);
   1268      1.46  yamaguch 	}
   1269       1.1   hannken }
   1270       1.1   hannken 
   1271       1.1   hannken static int
   1272       1.1   hannken vioif_ioctl(struct ifnet *ifp, u_long cmd, void *data)
   1273       1.1   hannken {
   1274       1.1   hannken 	int s, r;
   1275       1.1   hannken 
   1276       1.1   hannken 	s = splnet();
   1277       1.1   hannken 
   1278       1.1   hannken 	r = ether_ioctl(ifp, cmd, data);
   1279       1.1   hannken 	if ((r == 0 && cmd == SIOCSIFFLAGS) ||
   1280       1.1   hannken 	    (r == ENETRESET && (cmd == SIOCADDMULTI || cmd == SIOCDELMULTI))) {
   1281       1.1   hannken 		if (ifp->if_flags & IFF_RUNNING)
   1282       1.1   hannken 			r = vioif_rx_filter(ifp->if_softc);
   1283       1.1   hannken 		else
   1284       1.1   hannken 			r = 0;
   1285       1.1   hannken 	}
   1286       1.1   hannken 
   1287       1.1   hannken 	splx(s);
   1288       1.1   hannken 
   1289       1.1   hannken 	return r;
   1290       1.1   hannken }
   1291       1.1   hannken 
   1292       1.1   hannken void
   1293       1.1   hannken vioif_watchdog(struct ifnet *ifp)
   1294       1.1   hannken {
   1295       1.1   hannken 	struct vioif_softc *sc = ifp->if_softc;
   1296      1.46  yamaguch 	int i;
   1297       1.1   hannken 
   1298      1.46  yamaguch 	if (ifp->if_flags & IFF_RUNNING) {
   1299      1.46  yamaguch 		for (i = 0; i < sc->sc_act_nvq_pairs; i++)
   1300      1.46  yamaguch 			vioif_tx_vq_done(sc->sc_txq[i].txq_vq);
   1301      1.46  yamaguch 	}
   1302       1.1   hannken }
   1303       1.1   hannken 
   1304       1.1   hannken 
   1305       1.1   hannken /*
   1306      1.39  dholland  * Receive implementation
   1307       1.1   hannken  */
   1308      1.39  dholland /* allocate and initialize a mbuf for receive */
   1309       1.1   hannken static int
   1310      1.46  yamaguch vioif_add_rx_mbuf(struct vioif_rxqueue *rxq, int i)
   1311       1.1   hannken {
   1312      1.46  yamaguch 	struct virtio_softc *vsc = rxq->rxq_vq->vq_owner;
   1313       1.1   hannken 	struct mbuf *m;
   1314       1.1   hannken 	int r;
   1315       1.1   hannken 
   1316       1.1   hannken 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1317       1.1   hannken 	if (m == NULL)
   1318       1.1   hannken 		return ENOBUFS;
   1319       1.1   hannken 	MCLGET(m, M_DONTWAIT);
   1320       1.1   hannken 	if ((m->m_flags & M_EXT) == 0) {
   1321       1.1   hannken 		m_freem(m);
   1322       1.1   hannken 		return ENOBUFS;
   1323       1.1   hannken 	}
   1324      1.43  yamaguch 	rxq->rxq_mbufs[i] = m;
   1325       1.1   hannken 	m->m_len = m->m_pkthdr.len = m->m_ext.ext_size;
   1326      1.46  yamaguch 	r = bus_dmamap_load_mbuf(virtio_dmat(vsc),
   1327      1.50  christos 	    rxq->rxq_dmamaps[i], m, BUS_DMA_READ | BUS_DMA_NOWAIT);
   1328       1.1   hannken 	if (r) {
   1329       1.1   hannken 		m_freem(m);
   1330      1.46  yamaguch 		rxq->rxq_mbufs[i] = NULL;
   1331       1.1   hannken 		return r;
   1332       1.1   hannken 	}
   1333       1.1   hannken 
   1334       1.1   hannken 	return 0;
   1335       1.1   hannken }
   1336       1.1   hannken 
   1337      1.39  dholland /* free a mbuf for receive */
   1338       1.1   hannken static void
   1339      1.46  yamaguch vioif_free_rx_mbuf(struct vioif_rxqueue *rxq, int i)
   1340       1.1   hannken {
   1341      1.46  yamaguch 	struct virtio_softc *vsc = rxq->rxq_vq->vq_owner;
   1342      1.43  yamaguch 
   1343      1.46  yamaguch 	bus_dmamap_unload(virtio_dmat(vsc), rxq->rxq_dmamaps[i]);
   1344      1.43  yamaguch 	m_freem(rxq->rxq_mbufs[i]);
   1345      1.43  yamaguch 	rxq->rxq_mbufs[i] = NULL;
   1346       1.1   hannken }
   1347       1.1   hannken 
   1348      1.39  dholland /* add mbufs for all the empty receive slots */
   1349       1.1   hannken static void
   1350      1.46  yamaguch vioif_populate_rx_mbufs(struct vioif_rxqueue *rxq)
   1351       1.1   hannken {
   1352      1.43  yamaguch 
   1353      1.46  yamaguch 	mutex_enter(rxq->rxq_lock);
   1354      1.46  yamaguch 	vioif_populate_rx_mbufs_locked(rxq);
   1355      1.46  yamaguch 	mutex_exit(rxq->rxq_lock);
   1356      1.12     ozaki }
   1357      1.12     ozaki 
   1358      1.12     ozaki static void
   1359      1.46  yamaguch vioif_populate_rx_mbufs_locked(struct vioif_rxqueue *rxq)
   1360      1.12     ozaki {
   1361      1.46  yamaguch 	struct virtqueue *vq = rxq->rxq_vq;
   1362      1.46  yamaguch 	struct virtio_softc *vsc = vq->vq_owner;
   1363      1.46  yamaguch 	struct vioif_softc *sc = device_private(virtio_child(vsc));
   1364       1.1   hannken 	int i, r, ndone = 0;
   1365       1.1   hannken 
   1366      1.46  yamaguch 	KASSERT(mutex_owned(rxq->rxq_lock));
   1367       1.7     ozaki 
   1368      1.43  yamaguch 	if (rxq->rxq_stopping)
   1369      1.12     ozaki 		return;
   1370       1.7     ozaki 
   1371       1.1   hannken 	for (i = 0; i < vq->vq_num; i++) {
   1372       1.1   hannken 		int slot;
   1373       1.1   hannken 		r = virtio_enqueue_prep(vsc, vq, &slot);
   1374       1.1   hannken 		if (r == EAGAIN)
   1375       1.1   hannken 			break;
   1376       1.1   hannken 		if (r != 0)
   1377       1.1   hannken 			panic("enqueue_prep for rx buffers");
   1378      1.43  yamaguch 		if (rxq->rxq_mbufs[slot] == NULL) {
   1379      1.46  yamaguch 			r = vioif_add_rx_mbuf(rxq, slot);
   1380       1.1   hannken 			if (r != 0) {
   1381      1.50  christos 				aprint_error_dev(sc->sc_dev,
   1382      1.50  christos 				    "rx mbuf allocation failed, "
   1383      1.50  christos 				    "error code %d\n", r);
   1384       1.1   hannken 				break;
   1385       1.1   hannken 			}
   1386       1.1   hannken 		}
   1387       1.1   hannken 		r = virtio_enqueue_reserve(vsc, vq, slot,
   1388      1.50  christos 		    rxq->rxq_dmamaps[slot]->dm_nsegs + 1);
   1389       1.1   hannken 		if (r != 0) {
   1390      1.46  yamaguch 			vioif_free_rx_mbuf(rxq, slot);
   1391       1.1   hannken 			break;
   1392       1.1   hannken 		}
   1393      1.43  yamaguch 		bus_dmamap_sync(virtio_dmat(vsc), rxq->rxq_hdr_dmamaps[slot],
   1394      1.50  christos 		    0, sizeof(struct virtio_net_hdr), BUS_DMASYNC_PREREAD);
   1395      1.43  yamaguch 		bus_dmamap_sync(virtio_dmat(vsc), rxq->rxq_dmamaps[slot],
   1396      1.50  christos 		    0, MCLBYTES, BUS_DMASYNC_PREREAD);
   1397      1.50  christos 		virtio_enqueue(vsc, vq, slot, rxq->rxq_hdr_dmamaps[slot],
   1398      1.50  christos 		    false);
   1399      1.43  yamaguch 		virtio_enqueue(vsc, vq, slot, rxq->rxq_dmamaps[slot], false);
   1400       1.1   hannken 		virtio_enqueue_commit(vsc, vq, slot, false);
   1401       1.1   hannken 		ndone++;
   1402       1.1   hannken 	}
   1403       1.1   hannken 	if (ndone > 0)
   1404       1.1   hannken 		virtio_enqueue_commit(vsc, vq, -1, true);
   1405       1.1   hannken }
   1406       1.1   hannken 
   1407      1.39  dholland /* dequeue received packets */
   1408       1.1   hannken static int
   1409      1.46  yamaguch vioif_rx_deq(struct vioif_rxqueue *rxq)
   1410       1.1   hannken {
   1411       1.7     ozaki 	int r;
   1412       1.7     ozaki 
   1413      1.43  yamaguch 	KASSERT(rxq->rxq_stopping);
   1414       1.7     ozaki 
   1415      1.46  yamaguch 	mutex_enter(rxq->rxq_lock);
   1416      1.46  yamaguch 	r = vioif_rx_deq_locked(rxq);
   1417      1.46  yamaguch 	mutex_exit(rxq->rxq_lock);
   1418       1.7     ozaki 
   1419       1.7     ozaki 	return r;
   1420       1.7     ozaki }
   1421       1.7     ozaki 
   1422      1.39  dholland /* dequeue received packets */
   1423       1.7     ozaki static int
   1424      1.46  yamaguch vioif_rx_deq_locked(struct vioif_rxqueue *rxq)
   1425       1.7     ozaki {
   1426      1.43  yamaguch 	struct virtqueue *vq = rxq->rxq_vq;
   1427      1.46  yamaguch 	struct virtio_softc *vsc = vq->vq_owner;
   1428      1.46  yamaguch 	struct vioif_softc *sc = device_private(virtio_child(vsc));
   1429       1.1   hannken 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1430       1.1   hannken 	struct mbuf *m;
   1431       1.1   hannken 	int r = 0;
   1432       1.1   hannken 	int slot, len;
   1433       1.1   hannken 
   1434      1.46  yamaguch 	KASSERT(mutex_owned(rxq->rxq_lock));
   1435       1.7     ozaki 
   1436       1.1   hannken 	while (virtio_dequeue(vsc, vq, &slot, &len) == 0) {
   1437       1.1   hannken 		len -= sizeof(struct virtio_net_hdr);
   1438       1.1   hannken 		r = 1;
   1439      1.43  yamaguch 		bus_dmamap_sync(virtio_dmat(vsc), rxq->rxq_hdr_dmamaps[slot],
   1440      1.50  christos 		    0, sizeof(struct virtio_net_hdr), BUS_DMASYNC_POSTREAD);
   1441      1.43  yamaguch 		bus_dmamap_sync(virtio_dmat(vsc), rxq->rxq_dmamaps[slot],
   1442      1.50  christos 		    0, MCLBYTES, BUS_DMASYNC_POSTREAD);
   1443      1.43  yamaguch 		m = rxq->rxq_mbufs[slot];
   1444       1.1   hannken 		KASSERT(m != NULL);
   1445      1.43  yamaguch 		bus_dmamap_unload(virtio_dmat(vsc), rxq->rxq_dmamaps[slot]);
   1446      1.46  yamaguch 		rxq->rxq_mbufs[slot] = NULL;
   1447       1.1   hannken 		virtio_dequeue_commit(vsc, vq, slot);
   1448      1.24     ozaki 		m_set_rcvif(m, ifp);
   1449       1.1   hannken 		m->m_len = m->m_pkthdr.len = len;
   1450       1.7     ozaki 
   1451      1.46  yamaguch 		mutex_exit(rxq->rxq_lock);
   1452      1.22     ozaki 		if_percpuq_enqueue(ifp->if_percpuq, m);
   1453      1.46  yamaguch 		mutex_enter(rxq->rxq_lock);
   1454       1.7     ozaki 
   1455      1.43  yamaguch 		if (rxq->rxq_stopping)
   1456       1.7     ozaki 			break;
   1457       1.1   hannken 	}
   1458       1.3  christos 
   1459       1.1   hannken 	return r;
   1460       1.1   hannken }
   1461       1.1   hannken 
   1462       1.1   hannken /* rx interrupt; call _dequeue above and schedule a softint */
   1463       1.1   hannken static int
   1464       1.1   hannken vioif_rx_vq_done(struct virtqueue *vq)
   1465       1.1   hannken {
   1466      1.46  yamaguch 	struct vioif_rxqueue *rxq = vq->vq_done_ctx;
   1467       1.7     ozaki 	int r = 0;
   1468       1.7     ozaki 
   1469      1.12     ozaki #ifdef VIOIF_SOFTINT_INTR
   1470      1.12     ozaki 	KASSERT(!cpu_intr_p());
   1471      1.12     ozaki #endif
   1472      1.12     ozaki 
   1473      1.46  yamaguch 	mutex_enter(rxq->rxq_lock);
   1474       1.7     ozaki 
   1475      1.43  yamaguch 	if (rxq->rxq_stopping)
   1476       1.7     ozaki 		goto out;
   1477       1.1   hannken 
   1478      1.46  yamaguch 	r = vioif_rx_deq_locked(rxq);
   1479       1.1   hannken 	if (r)
   1480      1.12     ozaki #ifdef VIOIF_SOFTINT_INTR
   1481      1.46  yamaguch 		vioif_populate_rx_mbufs_locked(rxq);
   1482      1.12     ozaki #else
   1483      1.43  yamaguch 		softint_schedule(rxq->rxq_softint);
   1484      1.12     ozaki #endif
   1485       1.1   hannken 
   1486       1.7     ozaki out:
   1487      1.46  yamaguch 	mutex_exit(rxq->rxq_lock);
   1488       1.1   hannken 	return r;
   1489       1.1   hannken }
   1490       1.1   hannken 
   1491      1.39  dholland /* softint: enqueue receive requests for new incoming packets */
   1492       1.1   hannken static void
   1493       1.1   hannken vioif_rx_softint(void *arg)
   1494       1.1   hannken {
   1495      1.46  yamaguch 	struct vioif_rxqueue *rxq = arg;
   1496       1.1   hannken 
   1497      1.46  yamaguch 	vioif_populate_rx_mbufs(rxq);
   1498       1.1   hannken }
   1499       1.1   hannken 
   1500       1.1   hannken /* free all the mbufs; called from if_stop(disable) */
   1501       1.1   hannken static void
   1502      1.46  yamaguch vioif_rx_drain(struct vioif_rxqueue *rxq)
   1503       1.1   hannken {
   1504      1.43  yamaguch 	struct virtqueue *vq = rxq->rxq_vq;
   1505       1.1   hannken 	int i;
   1506       1.1   hannken 
   1507       1.1   hannken 	for (i = 0; i < vq->vq_num; i++) {
   1508      1.43  yamaguch 		if (rxq->rxq_mbufs[i] == NULL)
   1509       1.1   hannken 			continue;
   1510      1.46  yamaguch 		vioif_free_rx_mbuf(rxq, i);
   1511       1.1   hannken 	}
   1512       1.1   hannken }
   1513       1.1   hannken 
   1514       1.1   hannken 
   1515       1.1   hannken /*
   1516       1.1   hannken  * Transmition implementation
   1517       1.1   hannken  */
   1518       1.1   hannken /* actual transmission is done in if_start */
   1519       1.1   hannken /* tx interrupt; dequeue and free mbufs */
   1520       1.1   hannken /*
   1521       1.1   hannken  * tx interrupt is actually disabled; this should be called upon
   1522       1.1   hannken  * tx vq full and watchdog
   1523       1.1   hannken  */
   1524       1.1   hannken static int
   1525       1.1   hannken vioif_tx_vq_done(struct virtqueue *vq)
   1526       1.1   hannken {
   1527       1.1   hannken 	struct virtio_softc *vsc = vq->vq_owner;
   1528      1.32  jdolecek 	struct vioif_softc *sc = device_private(virtio_child(vsc));
   1529      1.23     pooka 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1530      1.46  yamaguch 	struct vioif_txqueue *txq = vq->vq_done_ctx;
   1531       1.7     ozaki 	int r = 0;
   1532       1.7     ozaki 
   1533      1.46  yamaguch 	mutex_enter(txq->txq_lock);
   1534       1.7     ozaki 
   1535      1.43  yamaguch 	if (txq->txq_stopping)
   1536       1.7     ozaki 		goto out;
   1537       1.7     ozaki 
   1538       1.7     ozaki 	r = vioif_tx_vq_done_locked(vq);
   1539       1.7     ozaki 
   1540       1.7     ozaki out:
   1541      1.46  yamaguch 	mutex_exit(txq->txq_lock);
   1542      1.46  yamaguch 	if (r) {
   1543      1.28     ozaki 		if_schedule_deferred_start(ifp);
   1544      1.46  yamaguch 
   1545      1.46  yamaguch 		KASSERT(txq->txq_deferred_transmit != NULL);
   1546      1.46  yamaguch 		softint_schedule(txq->txq_deferred_transmit);
   1547      1.46  yamaguch 	}
   1548       1.7     ozaki 	return r;
   1549       1.7     ozaki }
   1550       1.7     ozaki 
   1551       1.7     ozaki static int
   1552       1.7     ozaki vioif_tx_vq_done_locked(struct virtqueue *vq)
   1553       1.7     ozaki {
   1554       1.7     ozaki 	struct virtio_softc *vsc = vq->vq_owner;
   1555      1.32  jdolecek 	struct vioif_softc *sc = device_private(virtio_child(vsc));
   1556      1.46  yamaguch 	struct vioif_txqueue *txq = vq->vq_done_ctx;
   1557       1.1   hannken 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1558       1.1   hannken 	struct mbuf *m;
   1559       1.1   hannken 	int r = 0;
   1560       1.1   hannken 	int slot, len;
   1561       1.1   hannken 
   1562      1.46  yamaguch 	KASSERT(mutex_owned(txq->txq_lock));
   1563       1.7     ozaki 
   1564       1.1   hannken 	while (virtio_dequeue(vsc, vq, &slot, &len) == 0) {
   1565       1.1   hannken 		r++;
   1566      1.43  yamaguch 		bus_dmamap_sync(virtio_dmat(vsc), txq->txq_hdr_dmamaps[slot],
   1567      1.50  christos 		    0, sizeof(struct virtio_net_hdr), BUS_DMASYNC_POSTWRITE);
   1568      1.43  yamaguch 		bus_dmamap_sync(virtio_dmat(vsc), txq->txq_dmamaps[slot],
   1569      1.50  christos 		    0, txq->txq_dmamaps[slot]->dm_mapsize,
   1570      1.50  christos 		    BUS_DMASYNC_POSTWRITE);
   1571      1.43  yamaguch 		m = txq->txq_mbufs[slot];
   1572      1.43  yamaguch 		bus_dmamap_unload(virtio_dmat(vsc), txq->txq_dmamaps[slot]);
   1573      1.46  yamaguch 		txq->txq_mbufs[slot] = NULL;
   1574       1.1   hannken 		virtio_dequeue_commit(vsc, vq, slot);
   1575  1.51.2.1        ad 		if_statinc(ifp, if_opackets);
   1576       1.1   hannken 		m_freem(m);
   1577       1.1   hannken 	}
   1578       1.1   hannken 
   1579       1.1   hannken 	if (r)
   1580       1.1   hannken 		ifp->if_flags &= ~IFF_OACTIVE;
   1581       1.1   hannken 	return r;
   1582       1.1   hannken }
   1583       1.1   hannken 
   1584       1.1   hannken /* free all the mbufs already put on vq; called from if_stop(disable) */
   1585       1.1   hannken static void
   1586      1.46  yamaguch vioif_tx_drain(struct vioif_txqueue *txq)
   1587       1.1   hannken {
   1588      1.43  yamaguch 	struct virtqueue *vq = txq->txq_vq;
   1589      1.46  yamaguch 	struct virtio_softc *vsc = vq->vq_owner;
   1590       1.1   hannken 	int i;
   1591       1.1   hannken 
   1592      1.43  yamaguch 	KASSERT(txq->txq_stopping);
   1593       1.7     ozaki 
   1594       1.1   hannken 	for (i = 0; i < vq->vq_num; i++) {
   1595      1.43  yamaguch 		if (txq->txq_mbufs[i] == NULL)
   1596       1.1   hannken 			continue;
   1597      1.43  yamaguch 		bus_dmamap_unload(virtio_dmat(vsc), txq->txq_dmamaps[i]);
   1598      1.43  yamaguch 		m_freem(txq->txq_mbufs[i]);
   1599      1.43  yamaguch 		txq->txq_mbufs[i] = NULL;
   1600       1.1   hannken 	}
   1601       1.1   hannken }
   1602       1.1   hannken 
   1603       1.1   hannken /*
   1604       1.1   hannken  * Control vq
   1605       1.1   hannken  */
   1606       1.1   hannken /* issue a VIRTIO_NET_CTRL_RX class command and wait for completion */
   1607      1.44  yamaguch static void
   1608      1.44  yamaguch vioif_ctrl_acquire(struct vioif_softc *sc)
   1609       1.1   hannken {
   1610      1.43  yamaguch 	struct vioif_ctrlqueue *ctrlq = &sc->sc_ctrlq;
   1611       1.1   hannken 
   1612      1.43  yamaguch 	mutex_enter(&ctrlq->ctrlq_wait_lock);
   1613      1.43  yamaguch 	while (ctrlq->ctrlq_inuse != FREE)
   1614      1.43  yamaguch 		cv_wait(&ctrlq->ctrlq_wait, &ctrlq->ctrlq_wait_lock);
   1615      1.43  yamaguch 	ctrlq->ctrlq_inuse = INUSE;
   1616      1.44  yamaguch 	ctrlq->ctrlq_owner = curlwp;
   1617      1.44  yamaguch 	mutex_exit(&ctrlq->ctrlq_wait_lock);
   1618      1.44  yamaguch }
   1619      1.44  yamaguch 
   1620      1.44  yamaguch static void
   1621      1.44  yamaguch vioif_ctrl_release(struct vioif_softc *sc)
   1622      1.44  yamaguch {
   1623      1.44  yamaguch 	struct vioif_ctrlqueue *ctrlq = &sc->sc_ctrlq;
   1624      1.44  yamaguch 
   1625      1.44  yamaguch 	KASSERT(ctrlq->ctrlq_inuse != FREE);
   1626      1.44  yamaguch 	KASSERT(ctrlq->ctrlq_owner == curlwp);
   1627      1.44  yamaguch 
   1628      1.44  yamaguch 	mutex_enter(&ctrlq->ctrlq_wait_lock);
   1629      1.44  yamaguch 	ctrlq->ctrlq_inuse = FREE;
   1630      1.46  yamaguch 	ctrlq->ctrlq_owner = NULL;
   1631      1.44  yamaguch 	cv_signal(&ctrlq->ctrlq_wait);
   1632      1.43  yamaguch 	mutex_exit(&ctrlq->ctrlq_wait_lock);
   1633      1.44  yamaguch }
   1634      1.44  yamaguch 
   1635      1.44  yamaguch static int
   1636      1.44  yamaguch vioif_ctrl_load_cmdspec(struct vioif_softc *sc,
   1637      1.44  yamaguch     struct vioif_ctrl_cmdspec *specs, int nspecs)
   1638      1.44  yamaguch {
   1639      1.44  yamaguch 	struct virtio_softc *vsc = sc->sc_virtio;
   1640      1.44  yamaguch 	int i, r, loaded;
   1641      1.44  yamaguch 
   1642      1.44  yamaguch 	loaded = 0;
   1643      1.44  yamaguch 	for (i = 0; i < nspecs; i++) {
   1644      1.44  yamaguch 		r = bus_dmamap_load(virtio_dmat(vsc),
   1645      1.44  yamaguch 		    specs[i].dmamap, specs[i].buf, specs[i].bufsize,
   1646      1.48   msaitoh 		    NULL, BUS_DMA_WRITE | BUS_DMA_NOWAIT);
   1647      1.44  yamaguch 		if (r) {
   1648      1.50  christos 			aprint_error_dev(sc->sc_dev, "control command dmamap"
   1649      1.50  christos 			    " load failed, error code %d\n", r);
   1650      1.44  yamaguch 			goto err;
   1651      1.44  yamaguch 		}
   1652      1.44  yamaguch 		loaded++;
   1653      1.44  yamaguch 
   1654      1.44  yamaguch 	}
   1655      1.44  yamaguch 
   1656      1.44  yamaguch 	return r;
   1657      1.44  yamaguch 
   1658      1.44  yamaguch err:
   1659      1.44  yamaguch 	for (i = 0; i < loaded; i++) {
   1660      1.44  yamaguch 		bus_dmamap_unload(virtio_dmat(vsc), specs[i].dmamap);
   1661      1.44  yamaguch 	}
   1662      1.44  yamaguch 
   1663      1.44  yamaguch 	return r;
   1664      1.44  yamaguch }
   1665      1.44  yamaguch 
   1666      1.44  yamaguch static void
   1667      1.44  yamaguch vioif_ctrl_unload_cmdspec(struct vioif_softc *sc,
   1668      1.44  yamaguch     struct vioif_ctrl_cmdspec *specs, int nspecs)
   1669      1.44  yamaguch {
   1670      1.44  yamaguch 	struct virtio_softc *vsc = sc->sc_virtio;
   1671      1.44  yamaguch 	int i;
   1672      1.44  yamaguch 
   1673      1.44  yamaguch 	for (i = 0; i < nspecs; i++) {
   1674      1.44  yamaguch 		bus_dmamap_unload(virtio_dmat(vsc), specs[i].dmamap);
   1675      1.44  yamaguch 	}
   1676      1.44  yamaguch }
   1677      1.44  yamaguch 
   1678      1.44  yamaguch static int
   1679      1.44  yamaguch vioif_ctrl_send_command(struct vioif_softc *sc, uint8_t class, uint8_t cmd,
   1680      1.44  yamaguch     struct vioif_ctrl_cmdspec *specs, int nspecs)
   1681      1.44  yamaguch {
   1682      1.44  yamaguch 	struct vioif_ctrlqueue *ctrlq = &sc->sc_ctrlq;
   1683      1.44  yamaguch 	struct virtqueue *vq = ctrlq->ctrlq_vq;
   1684      1.44  yamaguch 	struct virtio_softc *vsc = sc->sc_virtio;
   1685      1.44  yamaguch 	int i, r, slot;
   1686      1.43  yamaguch 
   1687      1.44  yamaguch 	ctrlq->ctrlq_cmd->class = class;
   1688      1.43  yamaguch 	ctrlq->ctrlq_cmd->command = cmd;
   1689       1.1   hannken 
   1690      1.43  yamaguch 	bus_dmamap_sync(virtio_dmat(vsc), ctrlq->ctrlq_cmd_dmamap,
   1691      1.50  christos 	    0, sizeof(struct virtio_net_ctrl_cmd), BUS_DMASYNC_PREWRITE);
   1692      1.44  yamaguch 	for (i = 0; i < nspecs; i++) {
   1693      1.44  yamaguch 		bus_dmamap_sync(virtio_dmat(vsc), specs[i].dmamap,
   1694      1.50  christos 		    0, specs[i].bufsize, BUS_DMASYNC_PREWRITE);
   1695      1.44  yamaguch 	}
   1696      1.43  yamaguch 	bus_dmamap_sync(virtio_dmat(vsc), ctrlq->ctrlq_status_dmamap,
   1697      1.50  christos 	    0, sizeof(struct virtio_net_ctrl_status), BUS_DMASYNC_PREREAD);
   1698       1.1   hannken 
   1699       1.1   hannken 	r = virtio_enqueue_prep(vsc, vq, &slot);
   1700       1.1   hannken 	if (r != 0)
   1701       1.1   hannken 		panic("%s: control vq busy!?", device_xname(sc->sc_dev));
   1702      1.44  yamaguch 	r = virtio_enqueue_reserve(vsc, vq, slot, nspecs + 2);
   1703       1.1   hannken 	if (r != 0)
   1704       1.1   hannken 		panic("%s: control vq busy!?", device_xname(sc->sc_dev));
   1705      1.43  yamaguch 	virtio_enqueue(vsc, vq, slot, ctrlq->ctrlq_cmd_dmamap, true);
   1706      1.44  yamaguch 	for (i = 0; i < nspecs; i++) {
   1707      1.44  yamaguch 		virtio_enqueue(vsc, vq, slot, specs[i].dmamap, true);
   1708      1.44  yamaguch 	}
   1709      1.43  yamaguch 	virtio_enqueue(vsc, vq, slot, ctrlq->ctrlq_status_dmamap, false);
   1710       1.1   hannken 	virtio_enqueue_commit(vsc, vq, slot, true);
   1711       1.1   hannken 
   1712       1.1   hannken 	/* wait for done */
   1713      1.43  yamaguch 	mutex_enter(&ctrlq->ctrlq_wait_lock);
   1714      1.43  yamaguch 	while (ctrlq->ctrlq_inuse != DONE)
   1715      1.43  yamaguch 		cv_wait(&ctrlq->ctrlq_wait, &ctrlq->ctrlq_wait_lock);
   1716      1.43  yamaguch 	mutex_exit(&ctrlq->ctrlq_wait_lock);
   1717       1.1   hannken 	/* already dequeueued */
   1718       1.1   hannken 
   1719      1.43  yamaguch 	bus_dmamap_sync(virtio_dmat(vsc), ctrlq->ctrlq_cmd_dmamap, 0,
   1720      1.50  christos 	    sizeof(struct virtio_net_ctrl_cmd), BUS_DMASYNC_POSTWRITE);
   1721      1.44  yamaguch 	for (i = 0; i < nspecs; i++) {
   1722      1.44  yamaguch 		bus_dmamap_sync(virtio_dmat(vsc), specs[i].dmamap, 0,
   1723      1.50  christos 		    specs[i].bufsize, BUS_DMASYNC_POSTWRITE);
   1724      1.44  yamaguch 	}
   1725      1.43  yamaguch 	bus_dmamap_sync(virtio_dmat(vsc), ctrlq->ctrlq_status_dmamap, 0,
   1726      1.50  christos 	    sizeof(struct virtio_net_ctrl_status), BUS_DMASYNC_POSTREAD);
   1727       1.1   hannken 
   1728      1.43  yamaguch 	if (ctrlq->ctrlq_status->ack == VIRTIO_NET_OK)
   1729       1.1   hannken 		r = 0;
   1730       1.1   hannken 	else {
   1731      1.50  christos 		aprint_error_dev(sc->sc_dev, "failed setting rx mode\n");
   1732       1.1   hannken 		r = EIO;
   1733       1.1   hannken 	}
   1734       1.1   hannken 
   1735      1.44  yamaguch 	return r;
   1736      1.44  yamaguch }
   1737      1.44  yamaguch 
   1738      1.44  yamaguch static int
   1739      1.44  yamaguch vioif_ctrl_rx(struct vioif_softc *sc, int cmd, bool onoff)
   1740      1.44  yamaguch {
   1741      1.44  yamaguch 	struct virtio_net_ctrl_rx *rx = sc->sc_ctrlq.ctrlq_rx;
   1742      1.44  yamaguch 	struct vioif_ctrl_cmdspec specs[1];
   1743      1.44  yamaguch 	int r;
   1744      1.44  yamaguch 
   1745      1.44  yamaguch 	if (!sc->sc_has_ctrl)
   1746      1.44  yamaguch 		return ENOTSUP;
   1747      1.44  yamaguch 
   1748      1.44  yamaguch 	vioif_ctrl_acquire(sc);
   1749      1.44  yamaguch 
   1750      1.44  yamaguch 	rx->onoff = onoff;
   1751      1.44  yamaguch 	specs[0].dmamap = sc->sc_ctrlq.ctrlq_rx_dmamap;
   1752      1.44  yamaguch 	specs[0].buf = rx;
   1753      1.44  yamaguch 	specs[0].bufsize = sizeof(*rx);
   1754      1.44  yamaguch 
   1755      1.44  yamaguch 	r = vioif_ctrl_send_command(sc, VIRTIO_NET_CTRL_RX, cmd,
   1756      1.44  yamaguch 	    specs, __arraycount(specs));
   1757       1.3  christos 
   1758      1.44  yamaguch 	vioif_ctrl_release(sc);
   1759       1.1   hannken 	return r;
   1760       1.1   hannken }
   1761       1.1   hannken 
   1762       1.1   hannken static int
   1763       1.1   hannken vioif_set_promisc(struct vioif_softc *sc, bool onoff)
   1764       1.1   hannken {
   1765      1.50  christos 	return vioif_ctrl_rx(sc, VIRTIO_NET_CTRL_RX_PROMISC, onoff);
   1766       1.1   hannken }
   1767       1.1   hannken 
   1768       1.1   hannken static int
   1769       1.1   hannken vioif_set_allmulti(struct vioif_softc *sc, bool onoff)
   1770       1.1   hannken {
   1771      1.50  christos 	return vioif_ctrl_rx(sc, VIRTIO_NET_CTRL_RX_ALLMULTI, onoff);
   1772       1.1   hannken }
   1773       1.1   hannken 
   1774       1.1   hannken /* issue VIRTIO_NET_CTRL_MAC_TABLE_SET command and wait for completion */
   1775       1.1   hannken static int
   1776       1.1   hannken vioif_set_rx_filter(struct vioif_softc *sc)
   1777       1.1   hannken {
   1778      1.43  yamaguch 	/* filter already set in ctrlq->ctrlq_mac_tbl */
   1779      1.44  yamaguch 	struct virtio_net_ctrl_mac_tbl *mac_tbl_uc, *mac_tbl_mc;
   1780      1.44  yamaguch 	struct vioif_ctrl_cmdspec specs[2];
   1781      1.44  yamaguch 	int nspecs = __arraycount(specs);
   1782      1.44  yamaguch 	int r;
   1783      1.44  yamaguch 
   1784      1.44  yamaguch 	mac_tbl_uc = sc->sc_ctrlq.ctrlq_mac_tbl_uc;
   1785      1.44  yamaguch 	mac_tbl_mc = sc->sc_ctrlq.ctrlq_mac_tbl_mc;
   1786       1.1   hannken 
   1787      1.32  jdolecek 	if (!sc->sc_has_ctrl)
   1788       1.1   hannken 		return ENOTSUP;
   1789       1.1   hannken 
   1790      1.44  yamaguch 	vioif_ctrl_acquire(sc);
   1791       1.1   hannken 
   1792      1.44  yamaguch 	specs[0].dmamap = sc->sc_ctrlq.ctrlq_tbl_uc_dmamap;
   1793      1.44  yamaguch 	specs[0].buf = mac_tbl_uc;
   1794      1.44  yamaguch 	specs[0].bufsize = sizeof(*mac_tbl_uc)
   1795      1.44  yamaguch 	    + (ETHER_ADDR_LEN * mac_tbl_uc->nentries);
   1796      1.44  yamaguch 
   1797      1.44  yamaguch 	specs[1].dmamap = sc->sc_ctrlq.ctrlq_tbl_mc_dmamap;
   1798      1.44  yamaguch 	specs[1].buf = mac_tbl_mc;
   1799      1.44  yamaguch 	specs[1].bufsize = sizeof(*mac_tbl_mc)
   1800      1.44  yamaguch 	    + (ETHER_ADDR_LEN * mac_tbl_mc->nentries);
   1801       1.1   hannken 
   1802      1.44  yamaguch 	r = vioif_ctrl_load_cmdspec(sc, specs, nspecs);
   1803      1.44  yamaguch 	if (r != 0)
   1804       1.1   hannken 		goto out;
   1805       1.1   hannken 
   1806      1.44  yamaguch 	r = vioif_ctrl_send_command(sc,
   1807      1.44  yamaguch 	    VIRTIO_NET_CTRL_MAC, VIRTIO_NET_CTRL_MAC_TABLE_SET,
   1808      1.44  yamaguch 	    specs, nspecs);
   1809       1.1   hannken 
   1810      1.44  yamaguch 	vioif_ctrl_unload_cmdspec(sc, specs, nspecs);
   1811       1.1   hannken 
   1812       1.1   hannken out:
   1813      1.44  yamaguch 	vioif_ctrl_release(sc);
   1814       1.3  christos 
   1815       1.1   hannken 	return r;
   1816       1.1   hannken }
   1817       1.1   hannken 
   1818      1.46  yamaguch static int
   1819      1.46  yamaguch vioif_ctrl_mq_vq_pairs_set(struct vioif_softc *sc, int nvq_pairs)
   1820      1.46  yamaguch {
   1821      1.46  yamaguch 	struct virtio_net_ctrl_mq *mq = sc->sc_ctrlq.ctrlq_mq;
   1822      1.46  yamaguch 	struct vioif_ctrl_cmdspec specs[1];
   1823      1.46  yamaguch 	int r;
   1824      1.46  yamaguch 
   1825      1.46  yamaguch 	if (!sc->sc_has_ctrl)
   1826      1.46  yamaguch 		return ENOTSUP;
   1827      1.46  yamaguch 
   1828      1.46  yamaguch 	if (nvq_pairs <= 1)
   1829      1.46  yamaguch 		return EINVAL;
   1830      1.46  yamaguch 
   1831      1.46  yamaguch 	vioif_ctrl_acquire(sc);
   1832      1.46  yamaguch 
   1833      1.46  yamaguch 	mq->virtqueue_pairs = nvq_pairs;
   1834      1.46  yamaguch 	specs[0].dmamap = sc->sc_ctrlq.ctrlq_mq_dmamap;
   1835      1.46  yamaguch 	specs[0].buf = mq;
   1836      1.46  yamaguch 	specs[0].bufsize = sizeof(*mq);
   1837      1.46  yamaguch 
   1838      1.46  yamaguch 	r = vioif_ctrl_send_command(sc,
   1839      1.46  yamaguch 	    VIRTIO_NET_CTRL_MQ, VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET,
   1840      1.46  yamaguch 	    specs, __arraycount(specs));
   1841      1.46  yamaguch 
   1842      1.46  yamaguch 	vioif_ctrl_release(sc);
   1843      1.46  yamaguch 
   1844      1.46  yamaguch 	return r;
   1845      1.46  yamaguch }
   1846      1.46  yamaguch 
   1847       1.1   hannken /* ctrl vq interrupt; wake up the command issuer */
   1848       1.1   hannken static int
   1849       1.1   hannken vioif_ctrl_vq_done(struct virtqueue *vq)
   1850       1.1   hannken {
   1851       1.1   hannken 	struct virtio_softc *vsc = vq->vq_owner;
   1852      1.46  yamaguch 	struct vioif_ctrlqueue *ctrlq = vq->vq_done_ctx;
   1853       1.1   hannken 	int r, slot;
   1854       1.1   hannken 
   1855       1.1   hannken 	r = virtio_dequeue(vsc, vq, &slot, NULL);
   1856       1.1   hannken 	if (r == ENOENT)
   1857       1.1   hannken 		return 0;
   1858       1.1   hannken 	virtio_dequeue_commit(vsc, vq, slot);
   1859       1.1   hannken 
   1860      1.43  yamaguch 	mutex_enter(&ctrlq->ctrlq_wait_lock);
   1861      1.43  yamaguch 	ctrlq->ctrlq_inuse = DONE;
   1862      1.43  yamaguch 	cv_signal(&ctrlq->ctrlq_wait);
   1863      1.43  yamaguch 	mutex_exit(&ctrlq->ctrlq_wait_lock);
   1864       1.1   hannken 
   1865       1.1   hannken 	return 1;
   1866       1.1   hannken }
   1867       1.1   hannken 
   1868       1.1   hannken /*
   1869       1.1   hannken  * If IFF_PROMISC requested,  set promiscuous
   1870       1.1   hannken  * If multicast filter small enough (<=MAXENTRIES) set rx filter
   1871       1.1   hannken  * If large multicast filter exist use ALLMULTI
   1872       1.1   hannken  */
   1873       1.1   hannken /*
   1874       1.1   hannken  * If setting rx filter fails fall back to ALLMULTI
   1875       1.1   hannken  * If ALLMULTI fails fall back to PROMISC
   1876       1.1   hannken  */
   1877       1.1   hannken static int
   1878       1.1   hannken vioif_rx_filter(struct vioif_softc *sc)
   1879       1.1   hannken {
   1880      1.48   msaitoh 	struct ethercom *ec = &sc->sc_ethercom;
   1881      1.48   msaitoh 	struct ifnet *ifp = &ec->ec_if;
   1882       1.1   hannken 	struct ether_multi *enm;
   1883       1.1   hannken 	struct ether_multistep step;
   1884      1.43  yamaguch 	struct vioif_ctrlqueue *ctrlq = &sc->sc_ctrlq;
   1885       1.1   hannken 	int nentries;
   1886       1.1   hannken 	int promisc = 0, allmulti = 0, rxfilter = 0;
   1887       1.1   hannken 	int r;
   1888       1.1   hannken 
   1889      1.32  jdolecek 	if (!sc->sc_has_ctrl) {	/* no ctrl vq; always promisc */
   1890       1.1   hannken 		ifp->if_flags |= IFF_PROMISC;
   1891       1.1   hannken 		return 0;
   1892       1.1   hannken 	}
   1893       1.1   hannken 
   1894       1.1   hannken 	if (ifp->if_flags & IFF_PROMISC) {
   1895       1.1   hannken 		promisc = 1;
   1896       1.1   hannken 		goto set;
   1897       1.1   hannken 	}
   1898       1.1   hannken 
   1899       1.1   hannken 	nentries = -1;
   1900      1.48   msaitoh 	ETHER_LOCK(ec);
   1901      1.48   msaitoh 	ETHER_FIRST_MULTI(step, ec, enm);
   1902       1.1   hannken 	while (nentries++, enm != NULL) {
   1903       1.1   hannken 		if (nentries >= VIRTIO_NET_CTRL_MAC_MAXENTRIES) {
   1904       1.1   hannken 			allmulti = 1;
   1905      1.31     ozaki 			goto set_unlock;
   1906       1.1   hannken 		}
   1907      1.50  christos 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
   1908       1.1   hannken 			allmulti = 1;
   1909      1.31     ozaki 			goto set_unlock;
   1910       1.1   hannken 		}
   1911      1.43  yamaguch 		memcpy(ctrlq->ctrlq_mac_tbl_mc->macs[nentries],
   1912      1.50  christos 		    enm->enm_addrlo, ETHER_ADDR_LEN);
   1913       1.1   hannken 		ETHER_NEXT_MULTI(step, enm);
   1914       1.1   hannken 	}
   1915       1.1   hannken 	rxfilter = 1;
   1916       1.1   hannken 
   1917      1.31     ozaki set_unlock:
   1918      1.48   msaitoh 	ETHER_UNLOCK(ec);
   1919      1.30     ozaki 
   1920      1.31     ozaki set:
   1921       1.1   hannken 	if (rxfilter) {
   1922      1.43  yamaguch 		ctrlq->ctrlq_mac_tbl_uc->nentries = 0;
   1923      1.43  yamaguch 		ctrlq->ctrlq_mac_tbl_mc->nentries = nentries;
   1924       1.1   hannken 		r = vioif_set_rx_filter(sc);
   1925       1.1   hannken 		if (r != 0) {
   1926       1.1   hannken 			rxfilter = 0;
   1927       1.1   hannken 			allmulti = 1; /* fallback */
   1928       1.1   hannken 		}
   1929       1.1   hannken 	} else {
   1930       1.1   hannken 		/* remove rx filter */
   1931      1.43  yamaguch 		ctrlq->ctrlq_mac_tbl_uc->nentries = 0;
   1932      1.43  yamaguch 		ctrlq->ctrlq_mac_tbl_mc->nentries = 0;
   1933       1.1   hannken 		r = vioif_set_rx_filter(sc);
   1934       1.1   hannken 		/* what to do on failure? */
   1935       1.1   hannken 	}
   1936       1.1   hannken 	if (allmulti) {
   1937       1.1   hannken 		r = vioif_set_allmulti(sc, true);
   1938       1.1   hannken 		if (r != 0) {
   1939       1.1   hannken 			allmulti = 0;
   1940       1.1   hannken 			promisc = 1; /* fallback */
   1941       1.1   hannken 		}
   1942       1.1   hannken 	} else {
   1943       1.1   hannken 		r = vioif_set_allmulti(sc, false);
   1944       1.1   hannken 		/* what to do on failure? */
   1945       1.1   hannken 	}
   1946       1.1   hannken 	if (promisc) {
   1947       1.1   hannken 		r = vioif_set_promisc(sc, true);
   1948       1.1   hannken 	} else {
   1949       1.1   hannken 		r = vioif_set_promisc(sc, false);
   1950       1.1   hannken 	}
   1951       1.1   hannken 
   1952       1.1   hannken 	return r;
   1953       1.1   hannken }
   1954       1.1   hannken 
   1955      1.34     ozaki static bool
   1956      1.34     ozaki vioif_is_link_up(struct vioif_softc *sc)
   1957      1.34     ozaki {
   1958      1.34     ozaki 	struct virtio_softc *vsc = sc->sc_virtio;
   1959      1.34     ozaki 	uint16_t status;
   1960      1.34     ozaki 
   1961      1.34     ozaki 	if (virtio_features(vsc) & VIRTIO_NET_F_STATUS)
   1962      1.34     ozaki 		status = virtio_read_device_config_2(vsc,
   1963      1.34     ozaki 		    VIRTIO_NET_CONFIG_STATUS);
   1964      1.34     ozaki 	else
   1965      1.34     ozaki 		status = VIRTIO_NET_S_LINK_UP;
   1966      1.34     ozaki 
   1967      1.34     ozaki 	return ((status & VIRTIO_NET_S_LINK_UP) != 0);
   1968      1.34     ozaki }
   1969      1.34     ozaki 
   1970      1.34     ozaki /* change link status */
   1971      1.34     ozaki static void
   1972      1.34     ozaki vioif_update_link_status(struct vioif_softc *sc)
   1973      1.34     ozaki {
   1974      1.34     ozaki 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1975      1.46  yamaguch 	struct vioif_txqueue *txq;
   1976      1.34     ozaki 	bool active, changed;
   1977      1.46  yamaguch 	int link, i;
   1978      1.34     ozaki 
   1979      1.34     ozaki 	active = vioif_is_link_up(sc);
   1980      1.34     ozaki 	changed = false;
   1981      1.34     ozaki 
   1982      1.34     ozaki 	if (active) {
   1983      1.34     ozaki 		if (!sc->sc_link_active)
   1984      1.34     ozaki 			changed = true;
   1985      1.34     ozaki 
   1986      1.34     ozaki 		link = LINK_STATE_UP;
   1987      1.34     ozaki 		sc->sc_link_active = true;
   1988      1.34     ozaki 	} else {
   1989      1.34     ozaki 		if (sc->sc_link_active)
   1990      1.34     ozaki 			changed = true;
   1991      1.34     ozaki 
   1992      1.34     ozaki 		link = LINK_STATE_DOWN;
   1993      1.34     ozaki 		sc->sc_link_active = false;
   1994      1.34     ozaki 	}
   1995      1.34     ozaki 
   1996      1.43  yamaguch 	if (changed) {
   1997      1.46  yamaguch 		for (i = 0; i < sc->sc_act_nvq_pairs; i++) {
   1998      1.46  yamaguch 			txq = &sc->sc_txq[i];
   1999      1.46  yamaguch 
   2000      1.46  yamaguch 			mutex_enter(txq->txq_lock);
   2001      1.46  yamaguch 			txq->txq_link_active = sc->sc_link_active;
   2002      1.46  yamaguch 			mutex_exit(txq->txq_lock);
   2003      1.46  yamaguch 		}
   2004      1.43  yamaguch 
   2005      1.34     ozaki 		if_link_state_change(ifp, link);
   2006      1.43  yamaguch 	}
   2007      1.34     ozaki }
   2008      1.34     ozaki 
   2009      1.34     ozaki static int
   2010      1.34     ozaki vioif_config_change(struct virtio_softc *vsc)
   2011      1.34     ozaki {
   2012      1.34     ozaki 	struct vioif_softc *sc = device_private(virtio_child(vsc));
   2013      1.34     ozaki 
   2014      1.34     ozaki #ifdef VIOIF_SOFTINT_INTR
   2015      1.34     ozaki 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   2016      1.34     ozaki #endif
   2017      1.34     ozaki 
   2018      1.34     ozaki #ifdef VIOIF_SOFTINT_INTR
   2019      1.34     ozaki 	KASSERT(!cpu_intr_p());
   2020      1.34     ozaki 	vioif_update_link_status(sc);
   2021      1.34     ozaki 	vioif_start(ifp);
   2022      1.34     ozaki #else
   2023      1.34     ozaki 	softint_schedule(sc->sc_ctl_softint);
   2024      1.34     ozaki #endif
   2025      1.34     ozaki 
   2026      1.34     ozaki 	return 0;
   2027      1.34     ozaki }
   2028      1.34     ozaki 
   2029      1.34     ozaki static void
   2030      1.34     ozaki vioif_ctl_softint(void *arg)
   2031      1.34     ozaki {
   2032      1.34     ozaki 	struct vioif_softc *sc = arg;
   2033      1.34     ozaki 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   2034      1.34     ozaki 
   2035      1.34     ozaki 	vioif_update_link_status(sc);
   2036      1.34     ozaki 	vioif_start(ifp);
   2037      1.34     ozaki }
   2038      1.34     ozaki 
   2039      1.26  pgoyette MODULE(MODULE_CLASS_DRIVER, if_vioif, "virtio");
   2040      1.48   msaitoh 
   2041      1.26  pgoyette #ifdef _MODULE
   2042      1.26  pgoyette #include "ioconf.c"
   2043      1.26  pgoyette #endif
   2044      1.48   msaitoh 
   2045      1.48   msaitoh static int
   2046      1.26  pgoyette if_vioif_modcmd(modcmd_t cmd, void *opaque)
   2047      1.26  pgoyette {
   2048      1.26  pgoyette 	int error = 0;
   2049      1.48   msaitoh 
   2050      1.26  pgoyette #ifdef _MODULE
   2051      1.26  pgoyette 	switch (cmd) {
   2052      1.26  pgoyette 	case MODULE_CMD_INIT:
   2053      1.48   msaitoh 		error = config_init_component(cfdriver_ioconf_if_vioif,
   2054      1.48   msaitoh 		    cfattach_ioconf_if_vioif, cfdata_ioconf_if_vioif);
   2055      1.26  pgoyette 		break;
   2056      1.26  pgoyette 	case MODULE_CMD_FINI:
   2057      1.26  pgoyette 		error = config_fini_component(cfdriver_ioconf_if_vioif,
   2058      1.26  pgoyette 		    cfattach_ioconf_if_vioif, cfdata_ioconf_if_vioif);
   2059      1.26  pgoyette 		break;
   2060      1.26  pgoyette 	default:
   2061      1.26  pgoyette 		error = ENOTTY;
   2062      1.48   msaitoh 		break;
   2063      1.26  pgoyette 	}
   2064      1.26  pgoyette #endif
   2065      1.48   msaitoh 
   2066      1.26  pgoyette 	return error;
   2067      1.26  pgoyette }
   2068