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