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