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