Home | History | Annotate | Line # | Download | only in pci
virtio_pci.c revision 1.20
      1  1.20  christos /* $NetBSD: virtio_pci.c,v 1.20 2021/01/24 01:44:11 christos Exp $ */
      2   1.1    cherry 
      3   1.1    cherry /*
      4  1.15   reinoud  * Copyright (c) 2020 The NetBSD Foundation, Inc.
      5  1.15   reinoud  * Copyright (c) 2012 Stefan Fritsch.
      6   1.1    cherry  * Copyright (c) 2010 Minoura Makoto.
      7   1.1    cherry  * All rights reserved.
      8   1.1    cherry  *
      9   1.1    cherry  * Redistribution and use in source and binary forms, with or without
     10   1.1    cherry  * modification, are permitted provided that the following conditions
     11   1.1    cherry  * are met:
     12   1.1    cherry  * 1. Redistributions of source code must retain the above copyright
     13   1.1    cherry  *    notice, this list of conditions and the following disclaimer.
     14   1.1    cherry  * 2. Redistributions in binary form must reproduce the above copyright
     15   1.1    cherry  *    notice, this list of conditions and the following disclaimer in the
     16   1.1    cherry  *    documentation and/or other materials provided with the distribution.
     17   1.1    cherry  *
     18   1.1    cherry  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19   1.1    cherry  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20   1.1    cherry  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21   1.1    cherry  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22   1.1    cherry  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23   1.1    cherry  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24   1.1    cherry  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25   1.1    cherry  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26   1.1    cherry  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27   1.1    cherry  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28   1.1    cherry  */
     29   1.1    cherry 
     30   1.1    cherry #include <sys/cdefs.h>
     31  1.20  christos __KERNEL_RCSID(0, "$NetBSD: virtio_pci.c,v 1.20 2021/01/24 01:44:11 christos Exp $");
     32   1.1    cherry 
     33   1.1    cherry #include <sys/param.h>
     34   1.1    cherry #include <sys/systm.h>
     35   1.4  jakllsch #include <sys/kmem.h>
     36   1.5  jakllsch #include <sys/module.h>
     37  1.19  christos #include <sys/endian.h>
     38   1.6  yamaguch #include <sys/interrupt.h>
     39   1.1    cherry 
     40   1.1    cherry #include <sys/device.h>
     41   1.1    cherry 
     42   1.1    cherry #include <dev/pci/pcidevs.h>
     43   1.1    cherry #include <dev/pci/pcireg.h>
     44   1.1    cherry #include <dev/pci/pcivar.h>
     45   1.1    cherry 
     46  1.15   reinoud #include <dev/pci/virtioreg.h> /* XXX: move to non-pci */
     47  1.15   reinoud #include <dev/pci/virtio_pcireg.h>
     48  1.15   reinoud 
     49   1.1    cherry #define VIRTIO_PRIVATE
     50  1.15   reinoud #include <dev/pci/virtiovar.h> /* XXX: move to non-pci */
     51   1.1    cherry 
     52   1.1    cherry 
     53   1.4  jakllsch static int	virtio_pci_match(device_t, cfdata_t, void *);
     54   1.4  jakllsch static void	virtio_pci_attach(device_t, device_t, void *);
     55   1.4  jakllsch static int	virtio_pci_rescan(device_t, const char *, const int *);
     56   1.4  jakllsch static int	virtio_pci_detach(device_t, int);
     57   1.4  jakllsch 
     58   1.4  jakllsch struct virtio_pci_softc {
     59   1.4  jakllsch 	struct virtio_softc	sc_sc;
     60  1.15   reinoud 
     61  1.15   reinoud 	/* IO space */
     62   1.4  jakllsch 	bus_space_tag_t		sc_iot;
     63   1.4  jakllsch 	bus_space_handle_t	sc_ioh;
     64   1.4  jakllsch 	bus_size_t		sc_iosize;
     65  1.15   reinoud 	bus_size_t		sc_mapped_iosize;
     66  1.15   reinoud 
     67  1.15   reinoud 	/* BARs */
     68  1.15   reinoud 	bus_space_tag_t		sc_bars_iot[4];
     69  1.15   reinoud 	bus_space_handle_t	sc_bars_ioh[4];
     70  1.15   reinoud 	bus_size_t		sc_bars_iosize[4];
     71  1.15   reinoud 
     72  1.15   reinoud 	/* notify space */
     73  1.15   reinoud 	bus_space_tag_t		sc_notify_iot;
     74  1.15   reinoud 	bus_space_handle_t	sc_notify_ioh;
     75  1.15   reinoud 	bus_size_t		sc_notify_iosize;
     76  1.15   reinoud 	uint32_t		sc_notify_off_multiplier;
     77  1.15   reinoud 
     78  1.15   reinoud 	/* isr space */
     79  1.15   reinoud 	bus_space_tag_t		sc_isr_iot;
     80  1.15   reinoud 	bus_space_handle_t	sc_isr_ioh;
     81  1.15   reinoud 	bus_size_t		sc_isr_iosize;
     82  1.15   reinoud 
     83  1.15   reinoud 	/* generic */
     84   1.4  jakllsch 	struct pci_attach_args	sc_pa;
     85   1.4  jakllsch 	pci_intr_handle_t	*sc_ihp;
     86   1.4  jakllsch 	void			**sc_ihs;
     87   1.4  jakllsch 	int			sc_ihs_num;
     88  1.15   reinoud 	int			sc_devcfg_offset;	/* for 0.9 */
     89   1.4  jakllsch };
     90   1.4  jakllsch 
     91  1.15   reinoud static int	virtio_pci_attach_09(device_t, void *);
     92  1.15   reinoud static void	virtio_pci_kick_09(struct virtio_softc *, uint16_t);
     93  1.15   reinoud static uint16_t	virtio_pci_read_queue_size_09(struct virtio_softc *, uint16_t);
     94  1.15   reinoud static void	virtio_pci_setup_queue_09(struct virtio_softc *, uint16_t, uint64_t);
     95  1.15   reinoud static void	virtio_pci_set_status_09(struct virtio_softc *, int);
     96  1.15   reinoud static void	virtio_pci_negotiate_features_09(struct virtio_softc *, uint64_t);
     97  1.15   reinoud 
     98  1.15   reinoud static int	virtio_pci_attach_10(device_t, void *);
     99  1.15   reinoud static void	virtio_pci_kick_10(struct virtio_softc *, uint16_t);
    100  1.15   reinoud static uint16_t	virtio_pci_read_queue_size_10(struct virtio_softc *, uint16_t);
    101  1.15   reinoud static void	virtio_pci_setup_queue_10(struct virtio_softc *, uint16_t, uint64_t);
    102  1.15   reinoud static void	virtio_pci_set_status_10(struct virtio_softc *, int);
    103  1.15   reinoud static void	virtio_pci_negotiate_features_10(struct virtio_softc *, uint64_t);
    104  1.15   reinoud static int	virtio_pci_find_cap(struct virtio_pci_softc *psc, int cfg_type, void *buf, int buflen);
    105  1.15   reinoud 
    106   1.4  jakllsch static uint8_t	virtio_pci_read_device_config_1(struct virtio_softc *, int);
    107   1.4  jakllsch static uint16_t	virtio_pci_read_device_config_2(struct virtio_softc *, int);
    108   1.4  jakllsch static uint32_t	virtio_pci_read_device_config_4(struct virtio_softc *, int);
    109   1.4  jakllsch static uint64_t	virtio_pci_read_device_config_8(struct virtio_softc *, int);
    110   1.4  jakllsch static void 	virtio_pci_write_device_config_1(struct virtio_softc *, int, uint8_t);
    111   1.4  jakllsch static void	virtio_pci_write_device_config_2(struct virtio_softc *, int, uint16_t);
    112   1.4  jakllsch static void	virtio_pci_write_device_config_4(struct virtio_softc *, int, uint32_t);
    113   1.4  jakllsch static void	virtio_pci_write_device_config_8(struct virtio_softc *, int, uint64_t);
    114  1.15   reinoud 
    115   1.4  jakllsch static int	virtio_pci_setup_interrupts(struct virtio_softc *);
    116   1.4  jakllsch static void	virtio_pci_free_interrupts(struct virtio_softc *);
    117  1.15   reinoud static int	virtio_pci_adjust_config_region(struct virtio_pci_softc *psc);
    118   1.4  jakllsch static int	virtio_pci_intr(void *arg);
    119   1.4  jakllsch static int	virtio_pci_msix_queue_intr(void *);
    120   1.4  jakllsch static int	virtio_pci_msix_config_intr(void *);
    121  1.15   reinoud static int	virtio_pci_setup_msix_vectors_09(struct virtio_softc *);
    122  1.15   reinoud static int	virtio_pci_setup_msix_vectors_10(struct virtio_softc *);
    123   1.4  jakllsch static int	virtio_pci_setup_msix_interrupts(struct virtio_softc *,
    124   1.4  jakllsch 		    struct pci_attach_args *);
    125   1.4  jakllsch static int	virtio_pci_setup_intx_interrupt(struct virtio_softc *,
    126   1.4  jakllsch 		    struct pci_attach_args *);
    127   1.4  jakllsch 
    128   1.4  jakllsch #define VIRTIO_MSIX_CONFIG_VECTOR_INDEX	0
    129   1.4  jakllsch #define VIRTIO_MSIX_QUEUE_VECTOR_INDEX	1
    130   1.4  jakllsch 
    131  1.15   reinoud #if 0
    132   1.4  jakllsch /* we use the legacy virtio spec, so the PCI registers are host native
    133   1.4  jakllsch  * byte order, not PCI (i.e. LE) byte order */
    134   1.4  jakllsch #if BYTE_ORDER == BIG_ENDIAN
    135   1.4  jakllsch #define REG_HI_OFF      0
    136   1.4  jakllsch #define REG_LO_OFF      4
    137   1.4  jakllsch #ifndef __BUS_SPACE_HAS_STREAM_METHODS
    138   1.4  jakllsch #define bus_space_read_stream_1 bus_space_read_1
    139   1.4  jakllsch #define bus_space_write_stream_1 bus_space_write_1
    140   1.4  jakllsch static inline uint16_t
    141   1.4  jakllsch bus_space_read_stream_2(bus_space_tag_t t, bus_space_handle_t h,
    142   1.4  jakllsch     bus_size_t o)
    143   1.4  jakllsch {
    144   1.4  jakllsch 	return le16toh(bus_space_read_2(t, h, o));
    145   1.4  jakllsch }
    146   1.4  jakllsch static inline void
    147   1.4  jakllsch bus_space_write_stream_2(bus_space_tag_t t, bus_space_handle_t h,
    148   1.4  jakllsch     bus_size_t o, uint16_t v)
    149   1.4  jakllsch {
    150   1.4  jakllsch 	bus_space_write_2(t, h, o, htole16(v));
    151   1.4  jakllsch }
    152   1.4  jakllsch static inline uint32_t
    153   1.4  jakllsch bus_space_read_stream_4(bus_space_tag_t t, bus_space_handle_t h,
    154   1.4  jakllsch     bus_size_t o)
    155   1.4  jakllsch {
    156   1.4  jakllsch 	return le32toh(bus_space_read_4(t, h, o));
    157   1.4  jakllsch }
    158   1.4  jakllsch static inline void
    159   1.4  jakllsch bus_space_write_stream_4(bus_space_tag_t t, bus_space_handle_t h,
    160   1.4  jakllsch     bus_size_t o, uint32_t v)
    161   1.4  jakllsch {
    162   1.4  jakllsch 	bus_space_write_4(t, h, o, htole32(v));
    163   1.4  jakllsch }
    164   1.4  jakllsch #endif
    165   1.4  jakllsch #else
    166   1.4  jakllsch #define REG_HI_OFF	4
    167   1.4  jakllsch #define REG_LO_OFF	0
    168   1.4  jakllsch #ifndef __BUS_SPACE_HAS_STREAM_METHODS
    169   1.4  jakllsch #define bus_space_read_stream_1 bus_space_read_1
    170   1.4  jakllsch #define bus_space_read_stream_2 bus_space_read_2
    171   1.4  jakllsch #define bus_space_read_stream_4 bus_space_read_4
    172   1.4  jakllsch #define bus_space_write_stream_1 bus_space_write_1
    173   1.4  jakllsch #define bus_space_write_stream_2 bus_space_write_2
    174   1.4  jakllsch #define bus_space_write_stream_4 bus_space_write_4
    175   1.4  jakllsch #endif
    176   1.4  jakllsch #endif
    177  1.15   reinoud #endif
    178   1.4  jakllsch 
    179   1.1    cherry 
    180  1.15   reinoud #if BYTE_ORDER == LITTLE_ENDIAN
    181  1.15   reinoud #	define VIODEVRW_SWAP_09 false
    182  1.15   reinoud #	define VIODEVRW_SWAP_10 false
    183  1.15   reinoud #else /* big endian */
    184  1.15   reinoud #	define VIODEVRW_SWAP_09 false
    185  1.15   reinoud #	define VIODEVRW_SWAP_10 true
    186  1.15   reinoud #endif
    187  1.15   reinoud 
    188   1.1    cherry 
    189   1.4  jakllsch CFATTACH_DECL3_NEW(virtio_pci, sizeof(struct virtio_pci_softc),
    190   1.4  jakllsch     virtio_pci_match, virtio_pci_attach, virtio_pci_detach, NULL,
    191   1.4  jakllsch     virtio_pci_rescan, NULL, DVF_DETACH_SHUTDOWN);
    192   1.4  jakllsch 
    193  1.15   reinoud static const struct virtio_ops virtio_pci_ops_09 = {
    194  1.15   reinoud 	.kick = virtio_pci_kick_09,
    195  1.15   reinoud 
    196  1.15   reinoud 	.read_dev_cfg_1 = virtio_pci_read_device_config_1,
    197  1.15   reinoud 	.read_dev_cfg_2 = virtio_pci_read_device_config_2,
    198  1.15   reinoud 	.read_dev_cfg_4 = virtio_pci_read_device_config_4,
    199  1.15   reinoud 	.read_dev_cfg_8 = virtio_pci_read_device_config_8,
    200  1.15   reinoud 	.write_dev_cfg_1 = virtio_pci_write_device_config_1,
    201  1.15   reinoud 	.write_dev_cfg_2 = virtio_pci_write_device_config_2,
    202  1.15   reinoud 	.write_dev_cfg_4 = virtio_pci_write_device_config_4,
    203  1.15   reinoud 	.write_dev_cfg_8 = virtio_pci_write_device_config_8,
    204  1.15   reinoud 
    205  1.15   reinoud 	.read_queue_size = virtio_pci_read_queue_size_09,
    206  1.15   reinoud 	.setup_queue = virtio_pci_setup_queue_09,
    207  1.15   reinoud 	.set_status = virtio_pci_set_status_09,
    208  1.15   reinoud 	.neg_features = virtio_pci_negotiate_features_09,
    209  1.15   reinoud 	.setup_interrupts = virtio_pci_setup_interrupts,
    210  1.15   reinoud 	.free_interrupts = virtio_pci_free_interrupts,
    211  1.15   reinoud };
    212  1.15   reinoud 
    213  1.15   reinoud static const struct virtio_ops virtio_pci_ops_10 = {
    214  1.15   reinoud 	.kick = virtio_pci_kick_10,
    215  1.15   reinoud 
    216   1.4  jakllsch 	.read_dev_cfg_1 = virtio_pci_read_device_config_1,
    217   1.4  jakllsch 	.read_dev_cfg_2 = virtio_pci_read_device_config_2,
    218   1.4  jakllsch 	.read_dev_cfg_4 = virtio_pci_read_device_config_4,
    219   1.4  jakllsch 	.read_dev_cfg_8 = virtio_pci_read_device_config_8,
    220   1.4  jakllsch 	.write_dev_cfg_1 = virtio_pci_write_device_config_1,
    221   1.4  jakllsch 	.write_dev_cfg_2 = virtio_pci_write_device_config_2,
    222   1.4  jakllsch 	.write_dev_cfg_4 = virtio_pci_write_device_config_4,
    223   1.4  jakllsch 	.write_dev_cfg_8 = virtio_pci_write_device_config_8,
    224  1.15   reinoud 
    225  1.15   reinoud 	.read_queue_size = virtio_pci_read_queue_size_10,
    226  1.15   reinoud 	.setup_queue = virtio_pci_setup_queue_10,
    227  1.15   reinoud 	.set_status = virtio_pci_set_status_10,
    228  1.15   reinoud 	.neg_features = virtio_pci_negotiate_features_10,
    229   1.4  jakllsch 	.setup_interrupts = virtio_pci_setup_interrupts,
    230   1.4  jakllsch 	.free_interrupts = virtio_pci_free_interrupts,
    231   1.4  jakllsch };
    232   1.1    cherry 
    233   1.1    cherry static int
    234   1.4  jakllsch virtio_pci_match(device_t parent, cfdata_t match, void *aux)
    235   1.1    cherry {
    236   1.1    cherry 	struct pci_attach_args *pa;
    237   1.1    cherry 
    238   1.1    cherry 	pa = (struct pci_attach_args *)aux;
    239   1.1    cherry 	switch (PCI_VENDOR(pa->pa_id)) {
    240   1.1    cherry 	case PCI_VENDOR_QUMRANET:
    241  1.15   reinoud 		if (((PCI_PRODUCT_QUMRANET_VIRTIO_1000 <=
    242  1.15   reinoud 		      PCI_PRODUCT(pa->pa_id)) &&
    243  1.15   reinoud 		     (PCI_PRODUCT(pa->pa_id) <=
    244  1.15   reinoud 		      PCI_PRODUCT_QUMRANET_VIRTIO_103F)) &&
    245  1.15   reinoud 	              PCI_REVISION(pa->pa_class) == 0)
    246  1.15   reinoud 			return 1;
    247  1.15   reinoud 		if (((PCI_PRODUCT_QUMRANET_VIRTIO_1040 <=
    248  1.15   reinoud 		      PCI_PRODUCT(pa->pa_id)) &&
    249  1.15   reinoud 		     (PCI_PRODUCT(pa->pa_id) <=
    250  1.15   reinoud 		      PCI_PRODUCT_QUMRANET_VIRTIO_107F)) &&
    251  1.15   reinoud 		      PCI_REVISION(pa->pa_class) == 1)
    252   1.1    cherry 			return 1;
    253   1.1    cherry 		break;
    254   1.1    cherry 	}
    255   1.1    cherry 
    256   1.1    cherry 	return 0;
    257   1.1    cherry }
    258   1.1    cherry 
    259   1.1    cherry static void
    260   1.4  jakllsch virtio_pci_attach(device_t parent, device_t self, void *aux)
    261   1.1    cherry {
    262   1.4  jakllsch 	struct virtio_pci_softc * const psc = device_private(self);
    263   1.4  jakllsch 	struct virtio_softc * const sc = &psc->sc_sc;
    264   1.1    cherry 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
    265   1.1    cherry 	pci_chipset_tag_t pc = pa->pa_pc;
    266   1.1    cherry 	pcitag_t tag = pa->pa_tag;
    267   1.1    cherry 	int revision;
    268  1.15   reinoud 	int ret;
    269   1.1    cherry 	pcireg_t id;
    270   1.2       uwe 	pcireg_t csr;
    271   1.1    cherry 
    272   1.1    cherry 	revision = PCI_REVISION(pa->pa_class);
    273  1.15   reinoud 	switch (revision) {
    274  1.15   reinoud 	case 0:
    275  1.15   reinoud 		/* subsystem ID shows what I am */
    276  1.15   reinoud 		id = PCI_SUBSYS_ID(pci_conf_read(pc, tag, PCI_SUBSYS_ID_REG));
    277  1.15   reinoud 		break;
    278  1.15   reinoud 	case 1:
    279  1.15   reinoud 		/* pci product number shows what I am */
    280  1.15   reinoud 		id = PCI_PRODUCT(pa->pa_id) - PCI_PRODUCT_QUMRANET_VIRTIO_1040;
    281  1.15   reinoud 		break;
    282  1.15   reinoud 	default:
    283   1.1    cherry 		aprint_normal(": unknown revision 0x%02x; giving up\n",
    284   1.1    cherry 			      revision);
    285   1.1    cherry 		return;
    286   1.1    cherry 	}
    287  1.15   reinoud 
    288   1.1    cherry 	aprint_normal("\n");
    289   1.1    cherry 	aprint_naive("\n");
    290  1.15   reinoud 	virtio_print_device_type(self, id, revision);
    291   1.1    cherry 
    292   1.2       uwe 	csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
    293   1.2       uwe 	csr |= PCI_COMMAND_MASTER_ENABLE | PCI_COMMAND_IO_ENABLE;
    294   1.2       uwe 	pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, csr);
    295   1.2       uwe 
    296   1.1    cherry 	sc->sc_dev = self;
    297   1.4  jakllsch 	psc->sc_pa = *pa;
    298   1.4  jakllsch 	psc->sc_iot = pa->pa_iot;
    299  1.15   reinoud 
    300  1.15   reinoud 	sc->sc_dmat = pa->pa_dmat;
    301   1.1    cherry 	if (pci_dma64_available(pa))
    302   1.1    cherry 		sc->sc_dmat = pa->pa_dmat64;
    303   1.1    cherry 
    304  1.15   reinoud 	/* attach is dependent on revision */
    305  1.15   reinoud 	ret = 0;
    306  1.15   reinoud 	if (revision == 1) {
    307  1.15   reinoud 		/* try to attach 1.0 */
    308  1.15   reinoud 		ret = virtio_pci_attach_10(self, aux);
    309  1.15   reinoud 	}
    310  1.15   reinoud 	if (ret == 0 && revision == 0) {
    311  1.15   reinoud 		/* revision 0 means 0.9 only or both 0.9 and 1.0 */
    312  1.15   reinoud 		ret = virtio_pci_attach_09(self, aux);
    313  1.15   reinoud 	}
    314  1.15   reinoud 	if (ret) {
    315  1.15   reinoud 		aprint_error_dev(self, "cannot attach (%d)\n", ret);
    316   1.1    cherry 		return;
    317   1.1    cherry 	}
    318  1.15   reinoud 	KASSERT(sc->sc_ops);
    319  1.15   reinoud 
    320  1.15   reinoud 	/* preset config region */
    321  1.15   reinoud 	psc->sc_devcfg_offset = VIRTIO_CONFIG_DEVICE_CONFIG_NOMSI;
    322  1.15   reinoud 	if (virtio_pci_adjust_config_region(psc))
    323  1.15   reinoud 		return;
    324   1.1    cherry 
    325  1.15   reinoud 	/* generic */
    326   1.1    cherry 	virtio_device_reset(sc);
    327   1.1    cherry 	virtio_set_status(sc, VIRTIO_CONFIG_DEVICE_STATUS_ACK);
    328   1.1    cherry 	virtio_set_status(sc, VIRTIO_CONFIG_DEVICE_STATUS_DRIVER);
    329   1.1    cherry 
    330  1.15   reinoud 	sc->sc_childdevid = id;
    331   1.1    cherry 	sc->sc_child = NULL;
    332   1.4  jakllsch 	virtio_pci_rescan(self, "virtio", 0);
    333   1.1    cherry 	return;
    334   1.1    cherry }
    335   1.1    cherry 
    336   1.1    cherry /* ARGSUSED */
    337   1.1    cherry static int
    338   1.4  jakllsch virtio_pci_rescan(device_t self, const char *attr, const int *scan_flags)
    339   1.1    cherry {
    340   1.4  jakllsch 	struct virtio_pci_softc * const psc = device_private(self);
    341   1.4  jakllsch 	struct virtio_softc * const sc = &psc->sc_sc;
    342   1.1    cherry 	struct virtio_attach_args va;
    343   1.1    cherry 
    344   1.1    cherry 	if (sc->sc_child)	/* Child already attached? */
    345   1.1    cherry 		return 0;
    346   1.1    cherry 
    347   1.1    cherry 	memset(&va, 0, sizeof(va));
    348   1.1    cherry 	va.sc_childdevid = sc->sc_childdevid;
    349   1.1    cherry 
    350   1.1    cherry 	config_found_ia(self, attr, &va, NULL);
    351   1.1    cherry 
    352  1.15   reinoud 	if (virtio_attach_failed(sc))
    353   1.1    cherry 		return 0;
    354   1.1    cherry 
    355   1.1    cherry 	return 0;
    356   1.1    cherry }
    357   1.1    cherry 
    358   1.1    cherry 
    359   1.1    cherry static int
    360   1.4  jakllsch virtio_pci_detach(device_t self, int flags)
    361   1.1    cherry {
    362   1.4  jakllsch 	struct virtio_pci_softc * const psc = device_private(self);
    363   1.4  jakllsch 	struct virtio_softc * const sc = &psc->sc_sc;
    364   1.1    cherry 	int r;
    365   1.1    cherry 
    366   1.1    cherry 	if (sc->sc_child != NULL) {
    367   1.1    cherry 		r = config_detach(sc->sc_child, flags);
    368   1.1    cherry 		if (r)
    369   1.1    cherry 			return r;
    370   1.1    cherry 	}
    371   1.1    cherry 
    372   1.1    cherry 	/* Check that child detached properly */
    373   1.1    cherry 	KASSERT(sc->sc_child == NULL);
    374   1.1    cherry 	KASSERT(sc->sc_vqs == NULL);
    375   1.4  jakllsch 	KASSERT(psc->sc_ihs_num == 0);
    376   1.1    cherry 
    377   1.4  jakllsch 	if (psc->sc_iosize)
    378  1.15   reinoud 		bus_space_unmap(psc->sc_iot, psc->sc_ioh,
    379  1.15   reinoud 			psc->sc_mapped_iosize);
    380   1.4  jakllsch 	psc->sc_iosize = 0;
    381   1.1    cherry 
    382   1.1    cherry 	return 0;
    383   1.1    cherry }
    384   1.4  jakllsch 
    385  1.15   reinoud 
    386  1.15   reinoud static int
    387  1.15   reinoud virtio_pci_attach_09(device_t self, void *aux)
    388  1.15   reinoud 	//struct virtio_pci_softc *psc, struct pci_attach_args *pa)
    389  1.15   reinoud {
    390  1.15   reinoud 	struct virtio_pci_softc * const psc = device_private(self);
    391  1.15   reinoud 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
    392  1.15   reinoud 	struct virtio_softc * const sc = &psc->sc_sc;
    393  1.15   reinoud //	pci_chipset_tag_t pc = pa->pa_pc;
    394  1.15   reinoud //	pcitag_t tag = pa->pa_tag;
    395  1.15   reinoud 
    396  1.15   reinoud 	/* complete IO region */
    397  1.15   reinoud 	if (pci_mapreg_map(pa, PCI_MAPREG_START, PCI_MAPREG_TYPE_IO, 0,
    398  1.15   reinoud 			   &psc->sc_iot, &psc->sc_ioh, NULL, &psc->sc_iosize)) {
    399  1.15   reinoud 		aprint_error_dev(self, "can't map i/o space\n");
    400  1.15   reinoud 		return EIO;
    401  1.15   reinoud 	}
    402  1.15   reinoud 	psc->sc_mapped_iosize = psc->sc_iosize;
    403  1.15   reinoud 
    404  1.15   reinoud 	/* queue space */
    405  1.15   reinoud 	if (bus_space_subregion(psc->sc_iot, psc->sc_ioh,
    406  1.15   reinoud 			VIRTIO_CONFIG_QUEUE_NOTIFY, 2, &psc->sc_notify_ioh)) {
    407  1.15   reinoud 		aprint_error_dev(self, "can't map notify i/o space\n");
    408  1.15   reinoud 		return EIO;
    409  1.15   reinoud 	}
    410  1.15   reinoud 	psc->sc_notify_iosize = 2;
    411  1.15   reinoud 	psc->sc_notify_iot = psc->sc_iot;
    412  1.15   reinoud 
    413  1.15   reinoud 	/* ISR space */
    414  1.15   reinoud 	if (bus_space_subregion(psc->sc_iot, psc->sc_ioh,
    415  1.15   reinoud 			VIRTIO_CONFIG_ISR_STATUS, 1, &psc->sc_isr_ioh)) {
    416  1.15   reinoud 		aprint_error_dev(self, "can't map isr i/o space\n");
    417  1.15   reinoud 		return EIO;
    418  1.15   reinoud 	}
    419  1.15   reinoud 	psc->sc_isr_iosize = 1;
    420  1.15   reinoud 	psc->sc_isr_iot = psc->sc_iot;
    421  1.15   reinoud 
    422  1.15   reinoud 	/* set our version 0.9 ops */
    423  1.15   reinoud 	sc->sc_ops = &virtio_pci_ops_09;
    424  1.15   reinoud 	sc->sc_devcfg_swap = VIODEVRW_SWAP_09;
    425  1.15   reinoud 	return 0;
    426  1.15   reinoud }
    427  1.15   reinoud 
    428  1.15   reinoud 
    429  1.15   reinoud #define NMAPREG		((PCI_MAPREG_END - PCI_MAPREG_START) / \
    430  1.15   reinoud 				sizeof(pcireg_t))
    431  1.15   reinoud static int
    432  1.15   reinoud virtio_pci_attach_10(device_t self, void *aux)
    433   1.4  jakllsch {
    434  1.15   reinoud 	struct virtio_pci_softc * const psc = device_private(self);
    435  1.15   reinoud 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
    436  1.15   reinoud 	struct virtio_softc * const sc = &psc->sc_sc;
    437  1.15   reinoud 	pci_chipset_tag_t pc = pa->pa_pc;
    438  1.15   reinoud 	pcitag_t tag = pa->pa_tag;
    439  1.15   reinoud 
    440  1.15   reinoud 	struct virtio_pci_cap common, isr, device;
    441  1.15   reinoud 	struct virtio_pci_notify_cap notify;
    442  1.15   reinoud 	int have_device_cfg = 0;
    443  1.15   reinoud 	bus_size_t bars[NMAPREG] = { 0 };
    444  1.15   reinoud 	int bars_idx[NMAPREG] = { 0 };
    445  1.15   reinoud 	struct virtio_pci_cap *caps[] = { &common, &isr, &device, &notify.cap };
    446  1.15   reinoud 	int i, j = 0, ret = 0;
    447  1.15   reinoud 
    448  1.15   reinoud 	if (virtio_pci_find_cap(psc, VIRTIO_PCI_CAP_COMMON_CFG,
    449  1.15   reinoud 			&common, sizeof(common)))
    450  1.15   reinoud 		return ENODEV;
    451  1.15   reinoud 	if (virtio_pci_find_cap(psc, VIRTIO_PCI_CAP_NOTIFY_CFG,
    452  1.15   reinoud 			&notify, sizeof(notify)))
    453  1.15   reinoud 		return ENODEV;
    454  1.15   reinoud 	if (virtio_pci_find_cap(psc, VIRTIO_PCI_CAP_ISR_CFG,
    455  1.15   reinoud 			&isr, sizeof(isr)))
    456  1.15   reinoud 		return ENODEV;
    457  1.15   reinoud 	if (virtio_pci_find_cap(psc, VIRTIO_PCI_CAP_DEVICE_CFG,
    458  1.15   reinoud 			&device, sizeof(device)))
    459  1.15   reinoud 		memset(&device, 0, sizeof(device));
    460  1.15   reinoud 	else
    461  1.15   reinoud 		have_device_cfg = 1;
    462  1.15   reinoud 
    463  1.15   reinoud 	/*
    464  1.15   reinoud 	 * XXX Maybe there are devices that offer the pci caps but not the
    465  1.15   reinoud 	 * XXX VERSION_1 feature bit? Then we should check the feature bit
    466  1.15   reinoud 	 * XXX here and fall back to 0.9 out if not present.
    467  1.15   reinoud 	 */
    468  1.15   reinoud 
    469  1.15   reinoud 	/* Figure out which bars we need to map */
    470  1.15   reinoud 	for (i = 0; i < __arraycount(caps); i++) {
    471  1.15   reinoud 		int bar = caps[i]->bar;
    472  1.15   reinoud 		bus_size_t len = caps[i]->offset + caps[i]->length;
    473  1.15   reinoud 		if (caps[i]->length == 0)
    474  1.15   reinoud 			continue;
    475  1.15   reinoud 		if (bars[bar] < len)
    476  1.15   reinoud 			bars[bar] = len;
    477  1.15   reinoud 	}
    478  1.15   reinoud 
    479  1.15   reinoud 	for (i = 0; i < __arraycount(bars); i++) {
    480  1.15   reinoud 		int reg;
    481  1.15   reinoud 		pcireg_t type;
    482  1.15   reinoud 		if (bars[i] == 0)
    483  1.15   reinoud 			continue;
    484  1.15   reinoud 		reg = PCI_MAPREG_START + i * 4;
    485  1.15   reinoud 		type = pci_mapreg_type(pc, tag, reg);
    486  1.15   reinoud 		if (pci_mapreg_map(pa, reg, type, 0,
    487  1.15   reinoud 				&psc->sc_bars_iot[j], &psc->sc_bars_ioh[j],
    488  1.15   reinoud 				NULL, &psc->sc_bars_iosize[j])) {
    489  1.15   reinoud 			aprint_error_dev(self, "can't map bar %u \n", i);
    490  1.15   reinoud 			ret = EIO;
    491  1.15   reinoud 			goto err;
    492  1.15   reinoud 		}
    493  1.17    martin 		aprint_debug_dev(self,
    494  1.17    martin 		    "bar[%d]: iot %p, size 0x%" PRIxBUSSIZE "\n",
    495  1.17    martin 		    j, psc->sc_bars_iot[j], psc->sc_bars_iosize[j]);
    496  1.15   reinoud 		bars_idx[i] = j;
    497  1.15   reinoud 		j++;
    498  1.15   reinoud 	}
    499  1.15   reinoud 
    500  1.15   reinoud 	i = bars_idx[notify.cap.bar];
    501  1.15   reinoud 	if (bus_space_subregion(psc->sc_bars_iot[i], psc->sc_bars_ioh[i],
    502  1.15   reinoud 			notify.cap.offset, notify.cap.length,
    503  1.15   reinoud 			&psc->sc_notify_ioh)) {
    504  1.15   reinoud 		aprint_error_dev(self, "can't map notify i/o space\n");
    505  1.15   reinoud 		ret = EIO;
    506  1.15   reinoud 		goto err;
    507  1.15   reinoud 	}
    508  1.15   reinoud 	psc->sc_notify_iosize = notify.cap.length;
    509  1.15   reinoud 	psc->sc_notify_iot = psc->sc_bars_iot[i];
    510  1.15   reinoud 	psc->sc_notify_off_multiplier = le32toh(notify.notify_off_multiplier);
    511  1.15   reinoud 
    512  1.15   reinoud 	if (have_device_cfg) {
    513  1.15   reinoud 		i = bars_idx[device.bar];
    514  1.15   reinoud 		if (bus_space_subregion(psc->sc_bars_iot[i], psc->sc_bars_ioh[i],
    515  1.15   reinoud 				device.offset, device.length,
    516  1.15   reinoud 				&sc->sc_devcfg_ioh)) {
    517  1.15   reinoud 			aprint_error_dev(self, "can't map devcfg i/o space\n");
    518  1.15   reinoud 			ret = EIO;
    519  1.15   reinoud 			goto err;
    520  1.15   reinoud 		}
    521  1.15   reinoud 		aprint_debug_dev(self,
    522  1.15   reinoud 			"device.offset = 0x%x, device.length = 0x%x\n",
    523  1.15   reinoud 			device.offset, device.length);
    524  1.15   reinoud 		sc->sc_devcfg_iosize = device.length;
    525  1.15   reinoud 		sc->sc_devcfg_iot = psc->sc_bars_iot[i];
    526  1.15   reinoud 	}
    527  1.15   reinoud 
    528  1.15   reinoud 	i = bars_idx[isr.bar];
    529  1.15   reinoud 	if (bus_space_subregion(psc->sc_bars_iot[i], psc->sc_bars_ioh[i],
    530  1.15   reinoud 			isr.offset, isr.length, &psc->sc_isr_ioh)) {
    531  1.15   reinoud 		aprint_error_dev(self, "can't map isr i/o space\n");
    532  1.15   reinoud 		ret = EIO;
    533  1.15   reinoud 		goto err;
    534  1.15   reinoud 	}
    535  1.15   reinoud 	psc->sc_isr_iosize = isr.length;
    536  1.15   reinoud 	psc->sc_isr_iot = psc->sc_bars_iot[i];
    537  1.15   reinoud 
    538  1.15   reinoud 	i = bars_idx[common.bar];
    539  1.15   reinoud 	if (bus_space_subregion(psc->sc_bars_iot[i], psc->sc_bars_ioh[i],
    540  1.15   reinoud 			common.offset, common.length, &psc->sc_ioh)) {
    541  1.15   reinoud 		aprint_error_dev(self, "can't map common i/o space\n");
    542  1.15   reinoud 		ret = EIO;
    543  1.15   reinoud 		goto err;
    544  1.15   reinoud 	}
    545  1.15   reinoud 	psc->sc_iosize = common.length;
    546  1.15   reinoud 	psc->sc_iot = psc->sc_bars_iot[i];
    547  1.15   reinoud 	psc->sc_mapped_iosize = psc->sc_bars_iosize[i];
    548  1.15   reinoud 
    549  1.15   reinoud 	psc->sc_sc.sc_version_1 = 1;
    550  1.15   reinoud 
    551  1.15   reinoud 	/* set our version 1.0 ops */
    552  1.15   reinoud 	sc->sc_ops = &virtio_pci_ops_10;
    553  1.15   reinoud 	sc->sc_devcfg_swap = VIODEVRW_SWAP_10;
    554  1.15   reinoud 	return 0;
    555   1.4  jakllsch 
    556  1.15   reinoud err:
    557  1.15   reinoud 	/* there is no pci_mapreg_unmap() */
    558  1.15   reinoud 	return ret;
    559   1.4  jakllsch }
    560   1.4  jakllsch 
    561  1.15   reinoud /* v1.0 attach helper */
    562  1.15   reinoud static int
    563  1.15   reinoud virtio_pci_find_cap(struct virtio_pci_softc *psc, int cfg_type, void *buf, int buflen)
    564   1.4  jakllsch {
    565  1.15   reinoud 	device_t self = psc->sc_sc.sc_dev;
    566  1.15   reinoud 	pci_chipset_tag_t pc = psc->sc_pa.pa_pc;
    567  1.15   reinoud 	pcitag_t tag = psc->sc_pa.pa_tag;
    568  1.15   reinoud 	unsigned int offset, i, len;
    569  1.15   reinoud 	union {
    570  1.15   reinoud 		pcireg_t reg[8];
    571  1.15   reinoud 		struct virtio_pci_cap vcap;
    572  1.15   reinoud 	} *v = buf;
    573  1.15   reinoud 
    574  1.15   reinoud 	if (buflen < sizeof(struct virtio_pci_cap))
    575  1.15   reinoud 		return ERANGE;
    576  1.15   reinoud 
    577  1.15   reinoud 	if (!pci_get_capability(pc, tag, PCI_CAP_VENDSPEC, &offset, &v->reg[0]))
    578  1.15   reinoud 		return ENOENT;
    579  1.15   reinoud 
    580  1.15   reinoud 	do {
    581  1.15   reinoud 		for (i = 0; i < 4; i++)
    582  1.15   reinoud 			v->reg[i] =
    583  1.15   reinoud 				le32toh(pci_conf_read(pc, tag, offset + i * 4));
    584  1.15   reinoud 		if (v->vcap.cfg_type == cfg_type)
    585  1.15   reinoud 			break;
    586  1.15   reinoud 		offset = v->vcap.cap_next;
    587  1.15   reinoud 	} while (offset != 0);
    588  1.15   reinoud 
    589  1.15   reinoud 	if (offset == 0)
    590  1.15   reinoud 		return ENOENT;
    591  1.15   reinoud 
    592  1.15   reinoud 	if (v->vcap.cap_len > sizeof(struct virtio_pci_cap)) {
    593  1.15   reinoud 		len = roundup(v->vcap.cap_len, sizeof(pcireg_t));
    594  1.15   reinoud 		if (len > buflen) {
    595  1.15   reinoud 			aprint_error_dev(self, "%s cap too large\n", __func__);
    596  1.15   reinoud 			return ERANGE;
    597  1.15   reinoud 		}
    598  1.15   reinoud 		for (i = 4; i < len / sizeof(pcireg_t);  i++)
    599  1.15   reinoud 			v->reg[i] =
    600  1.15   reinoud 				le32toh(pci_conf_read(pc, tag, offset + i * 4));
    601  1.15   reinoud 	}
    602  1.15   reinoud 
    603  1.15   reinoud 	/* endian fixup */
    604  1.15   reinoud 	v->vcap.offset = le32toh(v->vcap.offset);
    605  1.15   reinoud 	v->vcap.length = le32toh(v->vcap.length);
    606  1.15   reinoud 	return 0;
    607   1.4  jakllsch }
    608   1.4  jakllsch 
    609  1.15   reinoud 
    610  1.15   reinoud /* -------------------------------------
    611  1.15   reinoud  * Version 0.9 support
    612  1.15   reinoud  * -------------------------------------*/
    613  1.15   reinoud 
    614  1.15   reinoud static void
    615  1.15   reinoud virtio_pci_kick_09(struct virtio_softc *sc, uint16_t idx)
    616   1.4  jakllsch {
    617   1.4  jakllsch 	struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
    618  1.15   reinoud 
    619  1.15   reinoud 	bus_space_write_2(psc->sc_notify_iot, psc->sc_notify_ioh, 0, idx);
    620   1.4  jakllsch }
    621   1.4  jakllsch 
    622  1.15   reinoud /* only applicable for v 0.9 but also called for 1.0 */
    623  1.15   reinoud static int
    624  1.15   reinoud virtio_pci_adjust_config_region(struct virtio_pci_softc *psc)
    625   1.4  jakllsch {
    626  1.15   reinoud 	struct virtio_softc * const sc = (struct virtio_softc *) psc;
    627  1.15   reinoud 	device_t self = psc->sc_sc.sc_dev;
    628  1.15   reinoud 
    629  1.15   reinoud 	if (psc->sc_sc.sc_version_1)
    630  1.15   reinoud 		return 0;
    631  1.15   reinoud 
    632  1.15   reinoud 	sc->sc_devcfg_iosize = psc->sc_iosize - psc->sc_devcfg_offset;
    633  1.15   reinoud 	sc->sc_devcfg_iot = psc->sc_iot;
    634  1.15   reinoud 	if (bus_space_subregion(psc->sc_iot, psc->sc_ioh,
    635  1.15   reinoud 			psc->sc_devcfg_offset, sc->sc_devcfg_iosize,
    636  1.15   reinoud 			&sc->sc_devcfg_ioh)) {
    637  1.15   reinoud 		aprint_error_dev(self, "can't map config i/o space\n");
    638  1.15   reinoud 		return EIO;
    639  1.15   reinoud 	}
    640  1.15   reinoud 
    641  1.15   reinoud 	return 0;
    642   1.4  jakllsch }
    643   1.4  jakllsch 
    644  1.15   reinoud static uint16_t
    645  1.15   reinoud virtio_pci_read_queue_size_09(struct virtio_softc *sc, uint16_t idx)
    646   1.4  jakllsch {
    647   1.4  jakllsch 	struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
    648   1.4  jakllsch 
    649  1.15   reinoud 	bus_space_write_2(psc->sc_iot, psc->sc_ioh,
    650  1.15   reinoud 	    VIRTIO_CONFIG_QUEUE_SELECT, idx);
    651  1.15   reinoud 	return bus_space_read_2(psc->sc_iot, psc->sc_ioh,
    652  1.15   reinoud 	    VIRTIO_CONFIG_QUEUE_SIZE);
    653   1.4  jakllsch }
    654   1.4  jakllsch 
    655   1.4  jakllsch static void
    656  1.15   reinoud virtio_pci_setup_queue_09(struct virtio_softc *sc, uint16_t idx, uint64_t addr)
    657   1.4  jakllsch {
    658   1.4  jakllsch 	struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
    659   1.4  jakllsch 
    660  1.15   reinoud 	bus_space_write_2(psc->sc_iot, psc->sc_ioh,
    661  1.15   reinoud 	    VIRTIO_CONFIG_QUEUE_SELECT, idx);
    662  1.15   reinoud 	bus_space_write_4(psc->sc_iot, psc->sc_ioh,
    663  1.15   reinoud 	    VIRTIO_CONFIG_QUEUE_ADDRESS, addr / VIRTIO_PAGE_SIZE);
    664  1.15   reinoud 
    665  1.15   reinoud 	if (psc->sc_ihs_num > 1) {
    666  1.15   reinoud 		int vec = VIRTIO_MSIX_QUEUE_VECTOR_INDEX;
    667  1.15   reinoud 		if (sc->sc_child_mq)
    668  1.15   reinoud 			vec += idx;
    669  1.15   reinoud 		bus_space_write_2(psc->sc_iot, psc->sc_ioh,
    670  1.15   reinoud 		    VIRTIO_CONFIG_MSI_QUEUE_VECTOR, vec);
    671  1.15   reinoud 	}
    672   1.4  jakllsch }
    673   1.4  jakllsch 
    674   1.4  jakllsch static void
    675  1.15   reinoud virtio_pci_set_status_09(struct virtio_softc *sc, int status)
    676   1.4  jakllsch {
    677   1.4  jakllsch 	struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
    678  1.15   reinoud 	int old = 0;
    679   1.4  jakllsch 
    680  1.15   reinoud 	if (status != 0) {
    681  1.15   reinoud 	    old = bus_space_read_1(psc->sc_iot, psc->sc_ioh,
    682  1.15   reinoud 		VIRTIO_CONFIG_DEVICE_STATUS);
    683  1.15   reinoud 	}
    684  1.15   reinoud 	bus_space_write_1(psc->sc_iot, psc->sc_ioh,
    685  1.15   reinoud 	    VIRTIO_CONFIG_DEVICE_STATUS, status|old);
    686   1.4  jakllsch }
    687   1.4  jakllsch 
    688   1.4  jakllsch static void
    689  1.15   reinoud virtio_pci_negotiate_features_09(struct virtio_softc *sc, uint64_t guest_features)
    690   1.4  jakllsch {
    691   1.4  jakllsch 	struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
    692  1.15   reinoud 	uint32_t r;
    693  1.15   reinoud 
    694  1.15   reinoud 	r = bus_space_read_4(psc->sc_iot, psc->sc_ioh,
    695  1.15   reinoud 	    VIRTIO_CONFIG_DEVICE_FEATURES);
    696  1.15   reinoud 
    697  1.15   reinoud 	r &= guest_features;
    698  1.15   reinoud 
    699  1.15   reinoud 	bus_space_write_4(psc->sc_iot, psc->sc_ioh,
    700  1.15   reinoud 	    VIRTIO_CONFIG_GUEST_FEATURES, r);
    701   1.4  jakllsch 
    702  1.15   reinoud 	sc->sc_active_features = r;
    703   1.4  jakllsch }
    704   1.4  jakllsch 
    705  1.15   reinoud /* -------------------------------------
    706  1.15   reinoud  * Version 1.0 support
    707  1.15   reinoud  * -------------------------------------*/
    708  1.15   reinoud 
    709   1.4  jakllsch static void
    710  1.15   reinoud virtio_pci_kick_10(struct virtio_softc *sc, uint16_t idx)
    711   1.4  jakllsch {
    712   1.4  jakllsch 	struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
    713  1.15   reinoud 	unsigned offset = sc->sc_vqs[idx].vq_notify_off *
    714  1.15   reinoud 		psc->sc_notify_off_multiplier;
    715   1.4  jakllsch 
    716  1.15   reinoud 	bus_space_write_2(psc->sc_notify_iot, psc->sc_notify_ioh, offset, idx);
    717   1.4  jakllsch }
    718   1.4  jakllsch 
    719  1.15   reinoud 
    720   1.4  jakllsch static uint16_t
    721  1.15   reinoud virtio_pci_read_queue_size_10(struct virtio_softc *sc, uint16_t idx)
    722   1.4  jakllsch {
    723   1.4  jakllsch 	struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
    724  1.15   reinoud 	bus_space_tag_t	   iot = psc->sc_iot;
    725  1.15   reinoud 	bus_space_handle_t ioh = psc->sc_ioh;
    726   1.4  jakllsch 
    727  1.15   reinoud 	bus_space_write_2(iot, ioh, VIRTIO_CONFIG1_QUEUE_SELECT, idx);
    728  1.15   reinoud 	return bus_space_read_2(iot, ioh, VIRTIO_CONFIG1_QUEUE_SIZE);
    729   1.4  jakllsch }
    730   1.4  jakllsch 
    731  1.18   reinoud /*
    732  1.18   reinoud  * By definition little endian only in v1.0 and 8 byters are allowed to be
    733  1.18   reinoud  * written as two 4 byters
    734  1.18   reinoud  */
    735  1.19  christos #ifndef __HAVE_BUS_SPACE_8
    736  1.20  christos /*
    737  1.20  christos  * This is not a general purpose function that can be used in any
    738  1.20  christos  * driver. Virtio specifically allows the 8 byte bus transaction
    739  1.20  christos  * to be split into two 4 byte transactions. Do not copy/use it
    740  1.20  christos  * in other device drivers unless you know that the device accepts it.
    741  1.20  christos  */
    742  1.19  christos static __inline void
    743  1.19  christos bus_space_write_8(bus_space_tag_t iot, bus_space_handle_t ioh,
    744  1.19  christos      bus_size_t offset, uint64_t value)
    745  1.19  christos {
    746  1.19  christos #if _QUAD_HIGHWORD
    747  1.19  christos 	bus_space_write_4(iot, ioh, offset, BUS_ADDR_LO32(value));
    748  1.19  christos 	bus_space_write_4(iot, ioh, offset + 4, BUS_ADDR_HI32(value));
    749  1.19  christos #else
    750  1.19  christos 	bus_space_write_4(iot, ioh, offset, BUS_ADDR_HI32(value));
    751  1.19  christos 	bus_space_write_4(iot, ioh, offset + 4, BUS_ADDR_LO32(value));
    752  1.19  christos #endif
    753  1.19  christos }
    754  1.19  christos #endif
    755  1.18   reinoud 
    756   1.4  jakllsch static void
    757  1.15   reinoud virtio_pci_setup_queue_10(struct virtio_softc *sc, uint16_t idx, uint64_t addr)
    758   1.4  jakllsch {
    759   1.4  jakllsch 	struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
    760  1.15   reinoud 	struct virtqueue *vq = &sc->sc_vqs[idx];
    761  1.15   reinoud 	bus_space_tag_t	   iot = psc->sc_iot;
    762  1.15   reinoud 	bus_space_handle_t ioh = psc->sc_ioh;
    763  1.15   reinoud 	KASSERT(vq->vq_index == idx);
    764  1.15   reinoud 
    765  1.15   reinoud 	bus_space_write_2(iot, ioh, VIRTIO_CONFIG1_QUEUE_SELECT, vq->vq_index);
    766  1.15   reinoud 	if (addr == 0) {
    767  1.15   reinoud 		bus_space_write_2(iot, ioh, VIRTIO_CONFIG1_QUEUE_ENABLE, 0);
    768  1.19  christos 		bus_space_write_8(iot, ioh, VIRTIO_CONFIG1_QUEUE_DESC,   0);
    769  1.19  christos 		bus_space_write_8(iot, ioh, VIRTIO_CONFIG1_QUEUE_AVAIL,  0);
    770  1.19  christos 		bus_space_write_8(iot, ioh, VIRTIO_CONFIG1_QUEUE_USED,   0);
    771  1.15   reinoud 	} else {
    772  1.19  christos 		bus_space_write_8(iot, ioh,
    773  1.15   reinoud 			VIRTIO_CONFIG1_QUEUE_DESC, addr);
    774  1.19  christos 		bus_space_write_8(iot, ioh,
    775  1.15   reinoud 			VIRTIO_CONFIG1_QUEUE_AVAIL, addr + vq->vq_availoffset);
    776  1.19  christos 		bus_space_write_8(iot, ioh,
    777  1.15   reinoud 			VIRTIO_CONFIG1_QUEUE_USED, addr + vq->vq_usedoffset);
    778  1.15   reinoud 		bus_space_write_2(iot, ioh,
    779  1.15   reinoud 			VIRTIO_CONFIG1_QUEUE_ENABLE, 1);
    780  1.15   reinoud 		vq->vq_notify_off = bus_space_read_2(iot, ioh,
    781  1.15   reinoud 			VIRTIO_CONFIG1_QUEUE_NOTIFY_OFF);
    782  1.15   reinoud 	}
    783   1.4  jakllsch 
    784   1.4  jakllsch 	if (psc->sc_ihs_num > 1) {
    785   1.4  jakllsch 		int vec = VIRTIO_MSIX_QUEUE_VECTOR_INDEX;
    786   1.6  yamaguch 		if (sc->sc_child_mq)
    787   1.4  jakllsch 			vec += idx;
    788  1.15   reinoud 		bus_space_write_2(iot, ioh,
    789  1.15   reinoud 			VIRTIO_CONFIG1_QUEUE_MSIX_VECTOR, vec);
    790   1.4  jakllsch 	}
    791   1.4  jakllsch }
    792   1.4  jakllsch 
    793   1.4  jakllsch static void
    794  1.15   reinoud virtio_pci_set_status_10(struct virtio_softc *sc, int status)
    795   1.4  jakllsch {
    796   1.4  jakllsch 	struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
    797  1.15   reinoud 	bus_space_tag_t	   iot = psc->sc_iot;
    798  1.15   reinoud 	bus_space_handle_t ioh = psc->sc_ioh;
    799   1.4  jakllsch 	int old = 0;
    800   1.4  jakllsch 
    801  1.15   reinoud 	if (status)
    802  1.15   reinoud 		old = bus_space_read_1(iot, ioh, VIRTIO_CONFIG1_DEVICE_STATUS);
    803  1.15   reinoud 	bus_space_write_1(iot, ioh, VIRTIO_CONFIG1_DEVICE_STATUS, status | old);
    804  1.15   reinoud }
    805  1.15   reinoud 
    806  1.15   reinoud void
    807  1.15   reinoud virtio_pci_negotiate_features_10(struct virtio_softc *sc, uint64_t guest_features)
    808  1.15   reinoud {
    809  1.15   reinoud 	struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
    810  1.15   reinoud 	device_t self          =  sc->sc_dev;
    811  1.15   reinoud 	bus_space_tag_t	   iot = psc->sc_iot;
    812  1.15   reinoud 	bus_space_handle_t ioh = psc->sc_ioh;
    813  1.15   reinoud 	uint64_t host, negotiated, device_status;
    814  1.15   reinoud 
    815  1.15   reinoud 	guest_features |= VIRTIO_F_VERSION_1;
    816  1.15   reinoud 	/* notify on empty is 0.9 only */
    817  1.15   reinoud 	guest_features &= ~VIRTIO_F_NOTIFY_ON_EMPTY;
    818  1.15   reinoud 	sc->sc_active_features = 0;
    819  1.15   reinoud 
    820  1.15   reinoud 	bus_space_write_4(iot, ioh, VIRTIO_CONFIG1_DEVICE_FEATURE_SELECT, 0);
    821  1.15   reinoud 	host = bus_space_read_4(iot, ioh, VIRTIO_CONFIG1_DEVICE_FEATURE);
    822  1.15   reinoud 	bus_space_write_4(iot, ioh, VIRTIO_CONFIG1_DEVICE_FEATURE_SELECT, 1);
    823  1.15   reinoud 	host |= (uint64_t)
    824  1.15   reinoud 		bus_space_read_4(iot, ioh, VIRTIO_CONFIG1_DEVICE_FEATURE) << 32;
    825  1.15   reinoud 
    826  1.15   reinoud 	negotiated = host & guest_features;
    827  1.15   reinoud 
    828  1.15   reinoud 	bus_space_write_4(iot, ioh, VIRTIO_CONFIG1_DRIVER_FEATURE_SELECT, 0);
    829  1.15   reinoud 	bus_space_write_4(iot, ioh, VIRTIO_CONFIG1_DRIVER_FEATURE,
    830  1.15   reinoud 			negotiated & 0xffffffff);
    831  1.15   reinoud 	bus_space_write_4(iot, ioh, VIRTIO_CONFIG1_DRIVER_FEATURE_SELECT, 1);
    832  1.15   reinoud 	bus_space_write_4(iot, ioh, VIRTIO_CONFIG1_DRIVER_FEATURE,
    833  1.15   reinoud 			negotiated >> 32);
    834  1.15   reinoud 	virtio_pci_set_status_10(sc, VIRTIO_CONFIG_DEVICE_STATUS_FEATURES_OK);
    835  1.15   reinoud 
    836  1.15   reinoud 	device_status = bus_space_read_1(iot, ioh, VIRTIO_CONFIG1_DEVICE_STATUS);
    837  1.15   reinoud 	if ((device_status & VIRTIO_CONFIG_DEVICE_STATUS_FEATURES_OK) == 0) {
    838  1.15   reinoud 		aprint_error_dev(self, "feature negotiation failed\n");
    839  1.15   reinoud 		bus_space_write_1(iot, ioh, VIRTIO_CONFIG1_DEVICE_STATUS,
    840  1.15   reinoud 				VIRTIO_CONFIG_DEVICE_STATUS_FAILED);
    841  1.15   reinoud 		return;
    842  1.15   reinoud 	}
    843  1.15   reinoud 
    844  1.15   reinoud 	if ((negotiated & VIRTIO_F_VERSION_1) == 0) {
    845  1.15   reinoud 		aprint_error_dev(self, "host rejected version 1\n");
    846  1.15   reinoud 		bus_space_write_1(iot, ioh, VIRTIO_CONFIG1_DEVICE_STATUS,
    847  1.15   reinoud 				VIRTIO_CONFIG_DEVICE_STATUS_FAILED);
    848  1.15   reinoud 		return;
    849   1.4  jakllsch 	}
    850  1.15   reinoud 
    851  1.15   reinoud 	sc->sc_active_features = negotiated;
    852  1.15   reinoud 	return;
    853  1.15   reinoud }
    854  1.15   reinoud 
    855  1.15   reinoud /* -------------------------------------
    856  1.15   reinoud  * Read/write device config code
    857  1.15   reinoud  * -------------------------------------*/
    858  1.15   reinoud 
    859  1.15   reinoud static uint8_t
    860  1.15   reinoud virtio_pci_read_device_config_1(struct virtio_softc *vsc, int index)
    861  1.15   reinoud {
    862  1.15   reinoud 	bus_space_tag_t	   iot = vsc->sc_devcfg_iot;
    863  1.15   reinoud 	bus_space_handle_t ioh = vsc->sc_devcfg_ioh;
    864  1.15   reinoud 
    865  1.15   reinoud 	return bus_space_read_1(iot, ioh, index);
    866  1.15   reinoud }
    867  1.15   reinoud 
    868  1.15   reinoud static uint16_t
    869  1.15   reinoud virtio_pci_read_device_config_2(struct virtio_softc *vsc, int index)
    870  1.15   reinoud {
    871  1.15   reinoud 	bus_space_tag_t	   iot = vsc->sc_devcfg_iot;
    872  1.15   reinoud 	bus_space_handle_t ioh = vsc->sc_devcfg_ioh;
    873  1.15   reinoud 	uint16_t val;
    874  1.15   reinoud 
    875  1.15   reinoud #if defined(__aarch64__) && BYTE_ORDER == BIG_ENDIAN
    876  1.15   reinoud 	val = bus_space_read_2(iot, ioh, index);
    877  1.15   reinoud 	return val;
    878  1.15   reinoud #else
    879  1.15   reinoud 	val = bus_space_read_stream_2(iot, ioh, index);
    880  1.15   reinoud 	if (vsc->sc_devcfg_swap)
    881  1.15   reinoud 		return bswap16(val);
    882  1.15   reinoud 	return val;
    883  1.15   reinoud #endif
    884   1.4  jakllsch }
    885   1.4  jakllsch 
    886   1.4  jakllsch static uint32_t
    887  1.15   reinoud virtio_pci_read_device_config_4(struct virtio_softc *vsc, int index)
    888  1.15   reinoud {
    889  1.15   reinoud 	bus_space_tag_t	   iot = vsc->sc_devcfg_iot;
    890  1.15   reinoud 	bus_space_handle_t ioh = vsc->sc_devcfg_ioh;
    891  1.15   reinoud 	uint32_t val;
    892  1.15   reinoud 
    893  1.15   reinoud #if defined(__aarch64__) && BYTE_ORDER == BIG_ENDIAN
    894  1.15   reinoud 	val = bus_space_read_4(iot, ioh, index);
    895  1.15   reinoud 	return val;
    896  1.15   reinoud #else
    897  1.15   reinoud 	val = bus_space_read_stream_4(iot, ioh, index);
    898  1.15   reinoud 	if (vsc->sc_devcfg_swap)
    899  1.15   reinoud 		return bswap32(val);
    900  1.15   reinoud 	return val;
    901  1.15   reinoud #endif
    902  1.15   reinoud }
    903  1.15   reinoud 
    904  1.15   reinoud static uint64_t
    905  1.15   reinoud virtio_pci_read_device_config_8(struct virtio_softc *vsc, int index)
    906  1.15   reinoud {
    907  1.15   reinoud 	bus_space_tag_t	   iot = vsc->sc_devcfg_iot;
    908  1.15   reinoud 	bus_space_handle_t ioh = vsc->sc_devcfg_ioh;
    909  1.15   reinoud 	uint64_t val, val_h, val_l;
    910  1.15   reinoud 
    911  1.15   reinoud #if defined(__aarch64__) && BYTE_ORDER == BIG_ENDIAN
    912  1.15   reinoud 	if (vsc->sc_devcfg_swap) {
    913  1.15   reinoud 		val_l = bus_space_read_4(iot, ioh, index);
    914  1.15   reinoud 		val_h = bus_space_read_4(iot, ioh, index + 4);
    915  1.15   reinoud 	} else {
    916  1.15   reinoud 		val_h = bus_space_read_4(iot, ioh, index);
    917  1.15   reinoud 		val_l = bus_space_read_4(iot, ioh, index + 4);
    918  1.15   reinoud 	}
    919  1.15   reinoud 	val = val_h << 32;
    920  1.15   reinoud 	val |= val_l;
    921  1.15   reinoud 	return val;
    922  1.15   reinoud #elif BYTE_ORDER == BIG_ENDIAN
    923  1.15   reinoud 	val_h = bus_space_read_stream_4(iot, ioh, index);
    924  1.15   reinoud 	val_l = bus_space_read_stream_4(iot, ioh, index + 4);
    925  1.15   reinoud 	val = val_h << 32;
    926  1.15   reinoud 	val |= val_l;
    927  1.15   reinoud 	if (vsc->sc_devcfg_swap)
    928  1.15   reinoud 		return bswap64(val);
    929  1.15   reinoud 	return val;
    930  1.15   reinoud #else
    931  1.15   reinoud 	val_l = bus_space_read_4(iot, ioh, index);
    932  1.15   reinoud 	val_h = bus_space_read_4(iot, ioh, index + 4);
    933  1.15   reinoud 	val = val_h << 32;
    934  1.15   reinoud 	val |= val_l;
    935  1.15   reinoud 
    936  1.15   reinoud 	return val;
    937  1.15   reinoud #endif
    938  1.15   reinoud }
    939  1.15   reinoud 
    940  1.15   reinoud static void
    941  1.15   reinoud virtio_pci_write_device_config_1(struct virtio_softc *vsc,
    942  1.15   reinoud 			     int index, uint8_t value)
    943  1.15   reinoud {
    944  1.15   reinoud 	bus_space_tag_t	   iot = vsc->sc_devcfg_iot;
    945  1.15   reinoud 	bus_space_handle_t ioh = vsc->sc_devcfg_ioh;
    946  1.15   reinoud 
    947  1.15   reinoud 	bus_space_write_1(iot, ioh, index, value);
    948  1.15   reinoud }
    949  1.15   reinoud 
    950  1.15   reinoud static void
    951  1.15   reinoud virtio_pci_write_device_config_2(struct virtio_softc *vsc,
    952  1.15   reinoud 			     int index, uint16_t value)
    953  1.15   reinoud {
    954  1.15   reinoud 	bus_space_tag_t	   iot = vsc->sc_devcfg_iot;
    955  1.15   reinoud 	bus_space_handle_t ioh = vsc->sc_devcfg_ioh;
    956  1.15   reinoud 
    957  1.15   reinoud #if defined(__aarch64__) && BYTE_ORDER == BIG_ENDIAN
    958  1.15   reinoud 	bus_space_write_2(iot, ioh, index, value);
    959  1.15   reinoud #else
    960  1.15   reinoud 	if (vsc->sc_devcfg_swap)
    961  1.15   reinoud 		value = bswap16(value);
    962  1.15   reinoud 	bus_space_write_stream_2(iot, ioh, index, value);
    963  1.15   reinoud #endif
    964  1.15   reinoud }
    965  1.15   reinoud 
    966  1.15   reinoud static void
    967  1.15   reinoud virtio_pci_write_device_config_4(struct virtio_softc *vsc,
    968  1.15   reinoud 			     int index, uint32_t value)
    969  1.15   reinoud {
    970  1.15   reinoud 	bus_space_tag_t	   iot = vsc->sc_devcfg_iot;
    971  1.15   reinoud 	bus_space_handle_t ioh = vsc->sc_devcfg_ioh;
    972  1.15   reinoud 
    973  1.15   reinoud #if defined(__aarch64__) && BYTE_ORDER == BIG_ENDIAN
    974  1.15   reinoud 	bus_space_write_4(iot, ioh, index, value);
    975  1.15   reinoud #else
    976  1.15   reinoud 	if (vsc->sc_devcfg_swap)
    977  1.15   reinoud 		value = bswap32(value);
    978  1.15   reinoud 	bus_space_write_stream_4(iot, ioh, index, value);
    979  1.15   reinoud #endif
    980  1.15   reinoud }
    981  1.15   reinoud 
    982  1.15   reinoud static void
    983  1.15   reinoud virtio_pci_write_device_config_8(struct virtio_softc *vsc,
    984  1.15   reinoud 			     int index, uint64_t value)
    985  1.15   reinoud {
    986  1.15   reinoud 	bus_space_tag_t	   iot = vsc->sc_devcfg_iot;
    987  1.15   reinoud 	bus_space_handle_t ioh = vsc->sc_devcfg_ioh;
    988  1.15   reinoud 	uint64_t val_h, val_l;
    989  1.15   reinoud 
    990  1.15   reinoud #if defined(__aarch64__) && BYTE_ORDER == BIG_ENDIAN
    991  1.15   reinoud 	val_l = value & 0xffffffff;
    992  1.15   reinoud 	val_h = value >> 32;
    993  1.15   reinoud 	if (vsc->sc_devcfg_swap) {
    994  1.15   reinoud 		bus_space_write_4(iot, ioh, index, val_l);
    995  1.15   reinoud 		bus_space_write_4(iot, ioh, index + 4, val_h);
    996  1.15   reinoud 	} else {
    997  1.15   reinoud 		bus_space_write_4(iot, ioh, index, val_h);
    998  1.15   reinoud 		bus_space_write_4(iot, ioh, index + 4, val_l);
    999  1.15   reinoud 	}
   1000  1.15   reinoud #elif BYTE_ORDER == BIG_ENDIAN
   1001  1.15   reinoud 	if (vsc->sc_devcfg_swap)
   1002  1.15   reinoud 		value = bswap64(value);
   1003  1.15   reinoud 	val_l = value & 0xffffffff;
   1004  1.15   reinoud 	val_h = value >> 32;
   1005  1.15   reinoud 
   1006  1.15   reinoud 	bus_space_write_stream_4(iot, ioh, index, val_h);
   1007  1.15   reinoud 	bus_space_write_stream_4(iot, ioh, index + 4, val_l);
   1008  1.15   reinoud #else
   1009  1.15   reinoud 	val_l = value & 0xffffffff;
   1010  1.15   reinoud 	val_h = value >> 32;
   1011  1.15   reinoud 	bus_space_write_stream_4(iot, ioh, index, val_l);
   1012  1.15   reinoud 	bus_space_write_stream_4(iot, ioh, index + 4, val_h);
   1013  1.15   reinoud #endif
   1014  1.15   reinoud }
   1015  1.15   reinoud 
   1016  1.15   reinoud /* -------------------------------------
   1017  1.15   reinoud  * Generic PCI interrupt code
   1018  1.15   reinoud  * -------------------------------------*/
   1019  1.15   reinoud 
   1020  1.15   reinoud static int
   1021  1.15   reinoud virtio_pci_setup_msix_vectors_10(struct virtio_softc *sc)
   1022   1.4  jakllsch {
   1023   1.4  jakllsch 	struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
   1024  1.15   reinoud 	device_t self          =  sc->sc_dev;
   1025  1.15   reinoud 	bus_space_tag_t	   iot = psc->sc_iot;
   1026  1.15   reinoud 	bus_space_handle_t ioh = psc->sc_ioh;
   1027  1.15   reinoud 	int vector, ret, qid;
   1028  1.15   reinoud 
   1029  1.15   reinoud 	vector = VIRTIO_MSIX_CONFIG_VECTOR_INDEX;
   1030  1.15   reinoud 	bus_space_write_2(iot, ioh,
   1031  1.15   reinoud 		VIRTIO_CONFIG1_CONFIG_MSIX_VECTOR, vector);
   1032  1.15   reinoud 	ret = bus_space_read_2(iot, ioh, VIRTIO_CONFIG1_CONFIG_MSIX_VECTOR);
   1033  1.15   reinoud 	if (ret != vector) {
   1034  1.15   reinoud 		aprint_error_dev(self, "can't set config msix vector\n");
   1035  1.15   reinoud 		return -1;
   1036  1.15   reinoud 	}
   1037  1.15   reinoud 
   1038  1.15   reinoud 	for (qid = 0; qid < sc->sc_nvqs; qid++) {
   1039  1.15   reinoud 		vector = VIRTIO_MSIX_QUEUE_VECTOR_INDEX;
   1040   1.4  jakllsch 
   1041  1.15   reinoud 		if (sc->sc_child_mq)
   1042  1.15   reinoud 			vector += qid;
   1043  1.15   reinoud 		bus_space_write_2(iot, ioh, VIRTIO_CONFIG1_QUEUE_SELECT, qid);
   1044  1.15   reinoud 		bus_space_write_2(iot, ioh, VIRTIO_CONFIG1_QUEUE_MSIX_VECTOR,
   1045  1.15   reinoud 			vector);
   1046  1.15   reinoud 		ret = bus_space_read_2(iot, ioh,
   1047  1.15   reinoud 			VIRTIO_CONFIG1_QUEUE_MSIX_VECTOR);
   1048  1.15   reinoud 		if (ret != vector) {
   1049  1.15   reinoud 			aprint_error_dev(self, "can't set queue %d "
   1050  1.15   reinoud 				"msix vector\n", qid);
   1051  1.15   reinoud 			return -1;
   1052  1.15   reinoud 		}
   1053  1.15   reinoud 	}
   1054   1.4  jakllsch 
   1055  1.15   reinoud 	return 0;
   1056   1.4  jakllsch }
   1057   1.4  jakllsch 
   1058   1.4  jakllsch static int
   1059  1.15   reinoud virtio_pci_setup_msix_vectors_09(struct virtio_softc *sc)
   1060   1.4  jakllsch {
   1061   1.4  jakllsch 	struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
   1062  1.15   reinoud 	device_t self = sc->sc_dev;
   1063   1.4  jakllsch 	int offset, vector, ret, qid;
   1064   1.4  jakllsch 
   1065   1.4  jakllsch 	offset = VIRTIO_CONFIG_MSI_CONFIG_VECTOR;
   1066   1.4  jakllsch 	vector = VIRTIO_MSIX_CONFIG_VECTOR_INDEX;
   1067   1.4  jakllsch 
   1068  1.15   reinoud 	bus_space_write_2(psc->sc_iot, psc->sc_ioh, offset, vector);
   1069  1.15   reinoud 	ret = bus_space_read_2(psc->sc_iot, psc->sc_ioh, offset);
   1070   1.4  jakllsch 	aprint_debug_dev(sc->sc_dev, "expected=%d, actual=%d\n",
   1071   1.4  jakllsch 	    vector, ret);
   1072  1.15   reinoud 	if (ret != vector) {
   1073  1.15   reinoud 		aprint_error_dev(self, "can't set config msix vector\n");
   1074   1.4  jakllsch 		return -1;
   1075  1.15   reinoud 	}
   1076   1.4  jakllsch 
   1077   1.4  jakllsch 	for (qid = 0; qid < sc->sc_nvqs; qid++) {
   1078   1.4  jakllsch 		offset = VIRTIO_CONFIG_QUEUE_SELECT;
   1079  1.15   reinoud 		bus_space_write_2(psc->sc_iot, psc->sc_ioh, offset, qid);
   1080   1.4  jakllsch 
   1081   1.4  jakllsch 		offset = VIRTIO_CONFIG_MSI_QUEUE_VECTOR;
   1082   1.4  jakllsch 		vector = VIRTIO_MSIX_QUEUE_VECTOR_INDEX;
   1083   1.4  jakllsch 
   1084   1.6  yamaguch 		if (sc->sc_child_mq)
   1085   1.6  yamaguch 			vector += qid;
   1086   1.6  yamaguch 
   1087  1.15   reinoud 		bus_space_write_2(psc->sc_iot, psc->sc_ioh, offset, vector);
   1088  1.15   reinoud 		ret = bus_space_read_2(psc->sc_iot, psc->sc_ioh, offset);
   1089   1.4  jakllsch 		aprint_debug_dev(sc->sc_dev, "expected=%d, actual=%d\n",
   1090   1.4  jakllsch 		    vector, ret);
   1091  1.15   reinoud 		if (ret != vector) {
   1092  1.15   reinoud 			aprint_error_dev(self, "can't set queue %d "
   1093  1.15   reinoud 				"msix vector\n", qid);
   1094   1.4  jakllsch 			return -1;
   1095  1.15   reinoud 		}
   1096   1.4  jakllsch 	}
   1097   1.4  jakllsch 
   1098   1.4  jakllsch 	return 0;
   1099   1.4  jakllsch }
   1100   1.4  jakllsch 
   1101   1.4  jakllsch static int
   1102   1.4  jakllsch virtio_pci_setup_msix_interrupts(struct virtio_softc *sc,
   1103   1.4  jakllsch     struct pci_attach_args *pa)
   1104   1.4  jakllsch {
   1105   1.4  jakllsch 	struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
   1106   1.4  jakllsch 	device_t self = sc->sc_dev;
   1107   1.4  jakllsch 	pci_chipset_tag_t pc = pa->pa_pc;
   1108   1.9  yamaguch 	struct virtqueue *vq;
   1109   1.4  jakllsch 	char intrbuf[PCI_INTRSTR_LEN];
   1110   1.6  yamaguch 	char intr_xname[INTRDEVNAMEBUF];
   1111   1.4  jakllsch 	char const *intrstr;
   1112   1.6  yamaguch 	int idx, qid, n;
   1113  1.15   reinoud 	int ret;
   1114   1.4  jakllsch 
   1115   1.4  jakllsch 	idx = VIRTIO_MSIX_CONFIG_VECTOR_INDEX;
   1116  1.15   reinoud 	if (sc->sc_flags & VIRTIO_F_INTR_MPSAFE)
   1117   1.4  jakllsch 		pci_intr_setattr(pc, &psc->sc_ihp[idx], PCI_INTR_MPSAFE, true);
   1118   1.4  jakllsch 
   1119   1.6  yamaguch 	snprintf(intr_xname, sizeof(intr_xname), "%s config",
   1120   1.6  yamaguch 	    device_xname(sc->sc_dev));
   1121   1.6  yamaguch 
   1122   1.4  jakllsch 	psc->sc_ihs[idx] = pci_intr_establish_xname(pc, psc->sc_ihp[idx],
   1123   1.6  yamaguch 	    sc->sc_ipl, virtio_pci_msix_config_intr, sc, intr_xname);
   1124   1.4  jakllsch 	if (psc->sc_ihs[idx] == NULL) {
   1125   1.4  jakllsch 		aprint_error_dev(self, "couldn't establish MSI-X for config\n");
   1126   1.4  jakllsch 		goto error;
   1127   1.4  jakllsch 	}
   1128   1.4  jakllsch 
   1129   1.4  jakllsch 	idx = VIRTIO_MSIX_QUEUE_VECTOR_INDEX;
   1130   1.6  yamaguch 	if (sc->sc_child_mq) {
   1131   1.6  yamaguch 		for (qid = 0; qid < sc->sc_nvqs; qid++) {
   1132   1.6  yamaguch 			n = idx + qid;
   1133   1.9  yamaguch 			vq = &sc->sc_vqs[qid];
   1134   1.6  yamaguch 
   1135   1.6  yamaguch 			snprintf(intr_xname, sizeof(intr_xname), "%s vq#%d",
   1136   1.6  yamaguch 			    device_xname(sc->sc_dev), qid);
   1137   1.6  yamaguch 
   1138  1.15   reinoud 			if (sc->sc_flags & VIRTIO_F_INTR_MPSAFE) {
   1139   1.6  yamaguch 				pci_intr_setattr(pc, &psc->sc_ihp[n],
   1140   1.6  yamaguch 				    PCI_INTR_MPSAFE, true);
   1141   1.6  yamaguch 			}
   1142   1.6  yamaguch 
   1143   1.6  yamaguch 			psc->sc_ihs[n] = pci_intr_establish_xname(pc, psc->sc_ihp[n],
   1144  1.10  yamaguch 			    sc->sc_ipl, vq->vq_intrhand, vq->vq_intrhand_arg, intr_xname);
   1145   1.6  yamaguch 			if (psc->sc_ihs[n] == NULL) {
   1146   1.6  yamaguch 				aprint_error_dev(self, "couldn't establish MSI-X for a vq\n");
   1147   1.6  yamaguch 				goto error;
   1148   1.6  yamaguch 			}
   1149   1.6  yamaguch 		}
   1150   1.6  yamaguch 	} else {
   1151  1.15   reinoud 		if (sc->sc_flags & VIRTIO_F_INTR_MPSAFE)
   1152   1.6  yamaguch 			pci_intr_setattr(pc, &psc->sc_ihp[idx], PCI_INTR_MPSAFE, true);
   1153   1.4  jakllsch 
   1154   1.6  yamaguch 		snprintf(intr_xname, sizeof(intr_xname), "%s queues",
   1155   1.6  yamaguch 		    device_xname(sc->sc_dev));
   1156   1.6  yamaguch 		psc->sc_ihs[idx] = pci_intr_establish_xname(pc, psc->sc_ihp[idx],
   1157   1.6  yamaguch 		    sc->sc_ipl, virtio_pci_msix_queue_intr, sc, intr_xname);
   1158   1.6  yamaguch 		if (psc->sc_ihs[idx] == NULL) {
   1159   1.6  yamaguch 			aprint_error_dev(self, "couldn't establish MSI-X for queues\n");
   1160   1.6  yamaguch 			goto error;
   1161   1.6  yamaguch 		}
   1162   1.4  jakllsch 	}
   1163   1.4  jakllsch 
   1164  1.15   reinoud 	if (sc->sc_version_1) {
   1165  1.15   reinoud 		ret = virtio_pci_setup_msix_vectors_10(sc);
   1166  1.15   reinoud 	} else {
   1167  1.15   reinoud 		ret = virtio_pci_setup_msix_vectors_09(sc);
   1168  1.15   reinoud 	}
   1169  1.15   reinoud 	if (ret) {
   1170   1.4  jakllsch 		aprint_error_dev(self, "couldn't setup MSI-X vectors\n");
   1171   1.4  jakllsch 		goto error;
   1172   1.4  jakllsch 	}
   1173   1.4  jakllsch 
   1174   1.4  jakllsch 	idx = VIRTIO_MSIX_CONFIG_VECTOR_INDEX;
   1175   1.4  jakllsch 	intrstr = pci_intr_string(pc, psc->sc_ihp[idx], intrbuf, sizeof(intrbuf));
   1176   1.4  jakllsch 	aprint_normal_dev(self, "config interrupting at %s\n", intrstr);
   1177   1.4  jakllsch 	idx = VIRTIO_MSIX_QUEUE_VECTOR_INDEX;
   1178   1.6  yamaguch 	if (sc->sc_child_mq) {
   1179   1.6  yamaguch 		kcpuset_t *affinity;
   1180   1.6  yamaguch 		int affinity_to, r;
   1181   1.6  yamaguch 
   1182   1.6  yamaguch 		kcpuset_create(&affinity, false);
   1183   1.6  yamaguch 
   1184   1.6  yamaguch 		for (qid = 0; qid < sc->sc_nvqs; qid++) {
   1185   1.6  yamaguch 			n = idx + qid;
   1186   1.6  yamaguch 			affinity_to = (qid / 2) % ncpu;
   1187   1.6  yamaguch 
   1188   1.6  yamaguch 			intrstr = pci_intr_string(pc, psc->sc_ihp[n],
   1189   1.6  yamaguch 			    intrbuf, sizeof(intrbuf));
   1190   1.6  yamaguch 
   1191   1.6  yamaguch 			kcpuset_zero(affinity);
   1192   1.6  yamaguch 			kcpuset_set(affinity, affinity_to);
   1193   1.6  yamaguch 			r = interrupt_distribute(psc->sc_ihs[n], affinity, NULL);
   1194   1.6  yamaguch 			if (r == 0) {
   1195   1.6  yamaguch 				aprint_normal_dev(self,
   1196   1.6  yamaguch 				    "for vq #%d interrupting at %s affinity to %u\n",
   1197   1.6  yamaguch 				    qid, intrstr, affinity_to);
   1198   1.6  yamaguch 			} else {
   1199   1.6  yamaguch 				aprint_normal_dev(self,
   1200   1.6  yamaguch 				    "for vq #%d interrupting at %s\n",
   1201   1.6  yamaguch 				    qid, intrstr);
   1202   1.6  yamaguch 			}
   1203   1.6  yamaguch 		}
   1204   1.6  yamaguch 
   1205   1.6  yamaguch 		kcpuset_destroy(affinity);
   1206   1.6  yamaguch 	} else {
   1207   1.6  yamaguch 		intrstr = pci_intr_string(pc, psc->sc_ihp[idx], intrbuf, sizeof(intrbuf));
   1208   1.6  yamaguch 		aprint_normal_dev(self, "queues interrupting at %s\n", intrstr);
   1209   1.6  yamaguch 	}
   1210   1.4  jakllsch 
   1211   1.4  jakllsch 	return 0;
   1212   1.4  jakllsch 
   1213   1.4  jakllsch error:
   1214   1.4  jakllsch 	idx = VIRTIO_MSIX_CONFIG_VECTOR_INDEX;
   1215   1.4  jakllsch 	if (psc->sc_ihs[idx] != NULL)
   1216   1.4  jakllsch 		pci_intr_disestablish(psc->sc_pa.pa_pc, psc->sc_ihs[idx]);
   1217   1.4  jakllsch 	idx = VIRTIO_MSIX_QUEUE_VECTOR_INDEX;
   1218   1.6  yamaguch 	if (sc->sc_child_mq) {
   1219   1.6  yamaguch 		for (qid = 0; qid < sc->sc_nvqs; qid++) {
   1220   1.6  yamaguch 			n = idx + qid;
   1221   1.6  yamaguch 			if (psc->sc_ihs[n] == NULL)
   1222   1.6  yamaguch 				continue;
   1223   1.6  yamaguch 			pci_intr_disestablish(psc->sc_pa.pa_pc, psc->sc_ihs[n]);
   1224   1.6  yamaguch 		}
   1225   1.6  yamaguch 
   1226   1.6  yamaguch 	} else {
   1227   1.6  yamaguch 		if (psc->sc_ihs[idx] != NULL)
   1228   1.6  yamaguch 			pci_intr_disestablish(psc->sc_pa.pa_pc, psc->sc_ihs[idx]);
   1229   1.6  yamaguch 	}
   1230   1.4  jakllsch 
   1231   1.4  jakllsch 	return -1;
   1232   1.4  jakllsch }
   1233   1.4  jakllsch 
   1234   1.4  jakllsch static int
   1235   1.4  jakllsch virtio_pci_setup_intx_interrupt(struct virtio_softc *sc,
   1236   1.4  jakllsch     struct pci_attach_args *pa)
   1237   1.4  jakllsch {
   1238   1.4  jakllsch 	struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
   1239   1.4  jakllsch 	device_t self = sc->sc_dev;
   1240   1.4  jakllsch 	pci_chipset_tag_t pc = pa->pa_pc;
   1241   1.4  jakllsch 	char intrbuf[PCI_INTRSTR_LEN];
   1242   1.4  jakllsch 	char const *intrstr;
   1243   1.4  jakllsch 
   1244  1.15   reinoud 	if (sc->sc_flags & VIRTIO_F_INTR_MPSAFE)
   1245   1.4  jakllsch 		pci_intr_setattr(pc, &psc->sc_ihp[0], PCI_INTR_MPSAFE, true);
   1246   1.4  jakllsch 
   1247   1.4  jakllsch 	psc->sc_ihs[0] = pci_intr_establish_xname(pc, psc->sc_ihp[0],
   1248   1.4  jakllsch 	    sc->sc_ipl, virtio_pci_intr, sc, device_xname(sc->sc_dev));
   1249   1.4  jakllsch 	if (psc->sc_ihs[0] == NULL) {
   1250   1.4  jakllsch 		aprint_error_dev(self, "couldn't establish INTx\n");
   1251   1.4  jakllsch 		return -1;
   1252   1.4  jakllsch 	}
   1253   1.4  jakllsch 
   1254   1.4  jakllsch 	intrstr = pci_intr_string(pc, psc->sc_ihp[0], intrbuf, sizeof(intrbuf));
   1255   1.4  jakllsch 	aprint_normal_dev(self, "interrupting at %s\n", intrstr);
   1256   1.4  jakllsch 
   1257   1.4  jakllsch 	return 0;
   1258   1.4  jakllsch }
   1259   1.4  jakllsch 
   1260   1.4  jakllsch static int
   1261   1.4  jakllsch virtio_pci_setup_interrupts(struct virtio_softc *sc)
   1262   1.4  jakllsch {
   1263   1.4  jakllsch 	struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
   1264   1.4  jakllsch 	device_t self = sc->sc_dev;
   1265   1.4  jakllsch 	pci_chipset_tag_t pc = psc->sc_pa.pa_pc;
   1266  1.13  jakllsch 	pcitag_t tag = psc->sc_pa.pa_tag;
   1267   1.4  jakllsch 	int error;
   1268   1.4  jakllsch 	int nmsix;
   1269  1.13  jakllsch 	int off;
   1270   1.4  jakllsch 	int counts[PCI_INTR_TYPE_SIZE];
   1271   1.4  jakllsch 	pci_intr_type_t max_type;
   1272  1.13  jakllsch 	pcireg_t ctl;
   1273   1.4  jakllsch 
   1274   1.4  jakllsch 	nmsix = pci_msix_count(psc->sc_pa.pa_pc, psc->sc_pa.pa_tag);
   1275   1.4  jakllsch 	aprint_debug_dev(self, "pci_msix_count=%d\n", nmsix);
   1276   1.4  jakllsch 
   1277   1.4  jakllsch 	/* We need at least two: one for config and the other for queues */
   1278  1.15   reinoud 	if ((sc->sc_flags & VIRTIO_F_INTR_MSIX) == 0 || nmsix < 2) {
   1279   1.4  jakllsch 		/* Try INTx only */
   1280   1.4  jakllsch 		max_type = PCI_INTR_TYPE_INTX;
   1281   1.4  jakllsch 		counts[PCI_INTR_TYPE_INTX] = 1;
   1282   1.4  jakllsch 	} else {
   1283   1.4  jakllsch 		/* Try MSI-X first and INTx second */
   1284  1.11  yamaguch 		if (sc->sc_nvqs + VIRTIO_MSIX_QUEUE_VECTOR_INDEX <= nmsix) {
   1285  1.11  yamaguch 			nmsix = sc->sc_nvqs + VIRTIO_MSIX_QUEUE_VECTOR_INDEX;
   1286  1.11  yamaguch 		} else {
   1287  1.11  yamaguch 			sc->sc_child_mq = false;
   1288  1.11  yamaguch 		}
   1289  1.11  yamaguch 
   1290  1.11  yamaguch 		if (sc->sc_child_mq == false) {
   1291   1.6  yamaguch 			nmsix = 2;
   1292   1.6  yamaguch 		}
   1293   1.6  yamaguch 
   1294   1.4  jakllsch 		max_type = PCI_INTR_TYPE_MSIX;
   1295   1.6  yamaguch 		counts[PCI_INTR_TYPE_MSIX] = nmsix;
   1296   1.4  jakllsch 		counts[PCI_INTR_TYPE_MSI] = 0;
   1297   1.4  jakllsch 		counts[PCI_INTR_TYPE_INTX] = 1;
   1298   1.4  jakllsch 	}
   1299   1.4  jakllsch 
   1300   1.4  jakllsch retry:
   1301   1.4  jakllsch 	error = pci_intr_alloc(&psc->sc_pa, &psc->sc_ihp, counts, max_type);
   1302   1.4  jakllsch 	if (error != 0) {
   1303   1.4  jakllsch 		aprint_error_dev(self, "couldn't map interrupt\n");
   1304   1.4  jakllsch 		return -1;
   1305   1.4  jakllsch 	}
   1306   1.4  jakllsch 
   1307   1.4  jakllsch 	if (pci_intr_type(pc, psc->sc_ihp[0]) == PCI_INTR_TYPE_MSIX) {
   1308  1.12  jakllsch 		psc->sc_ihs = kmem_zalloc(sizeof(*psc->sc_ihs) * nmsix,
   1309   1.4  jakllsch 		    KM_SLEEP);
   1310   1.4  jakllsch 
   1311   1.4  jakllsch 		error = virtio_pci_setup_msix_interrupts(sc, &psc->sc_pa);
   1312   1.4  jakllsch 		if (error != 0) {
   1313   1.6  yamaguch 			kmem_free(psc->sc_ihs, sizeof(*psc->sc_ihs) * nmsix);
   1314   1.6  yamaguch 			pci_intr_release(pc, psc->sc_ihp, nmsix);
   1315   1.4  jakllsch 
   1316   1.4  jakllsch 			/* Retry INTx */
   1317   1.4  jakllsch 			max_type = PCI_INTR_TYPE_INTX;
   1318   1.4  jakllsch 			counts[PCI_INTR_TYPE_INTX] = 1;
   1319   1.4  jakllsch 			goto retry;
   1320   1.4  jakllsch 		}
   1321   1.4  jakllsch 
   1322   1.6  yamaguch 		psc->sc_ihs_num = nmsix;
   1323  1.15   reinoud 		psc->sc_devcfg_offset = VIRTIO_CONFIG_DEVICE_CONFIG_MSI;
   1324  1.15   reinoud 		virtio_pci_adjust_config_region(psc);
   1325   1.4  jakllsch 	} else if (pci_intr_type(pc, psc->sc_ihp[0]) == PCI_INTR_TYPE_INTX) {
   1326  1.12  jakllsch 		psc->sc_ihs = kmem_zalloc(sizeof(*psc->sc_ihs) * 1,
   1327   1.4  jakllsch 		    KM_SLEEP);
   1328   1.4  jakllsch 
   1329   1.4  jakllsch 		error = virtio_pci_setup_intx_interrupt(sc, &psc->sc_pa);
   1330   1.4  jakllsch 		if (error != 0) {
   1331   1.4  jakllsch 			kmem_free(psc->sc_ihs, sizeof(*psc->sc_ihs) * 1);
   1332   1.4  jakllsch 			pci_intr_release(pc, psc->sc_ihp, 1);
   1333   1.4  jakllsch 			return -1;
   1334   1.4  jakllsch 		}
   1335   1.4  jakllsch 
   1336   1.4  jakllsch 		psc->sc_ihs_num = 1;
   1337  1.15   reinoud 		psc->sc_devcfg_offset = VIRTIO_CONFIG_DEVICE_CONFIG_NOMSI;
   1338  1.15   reinoud 		virtio_pci_adjust_config_region(psc);
   1339  1.13  jakllsch 
   1340  1.14  jakllsch 		error = pci_get_capability(pc, tag, PCI_CAP_MSIX, &off, NULL);
   1341  1.13  jakllsch 		if (error != 0) {
   1342  1.13  jakllsch 			ctl = pci_conf_read(pc, tag, off + PCI_MSIX_CTL);
   1343  1.13  jakllsch 			ctl &= ~PCI_MSIX_CTL_ENABLE;
   1344  1.13  jakllsch 			pci_conf_write(pc, tag, off + PCI_MSIX_CTL, ctl);
   1345  1.13  jakllsch 		}
   1346   1.4  jakllsch 	}
   1347   1.4  jakllsch 
   1348   1.4  jakllsch 	return 0;
   1349   1.4  jakllsch }
   1350   1.4  jakllsch 
   1351   1.4  jakllsch static void
   1352   1.4  jakllsch virtio_pci_free_interrupts(struct virtio_softc *sc)
   1353   1.4  jakllsch {
   1354   1.4  jakllsch 	struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
   1355   1.4  jakllsch 
   1356   1.4  jakllsch 	for (int i = 0; i < psc->sc_ihs_num; i++) {
   1357   1.4  jakllsch 		if (psc->sc_ihs[i] == NULL)
   1358   1.4  jakllsch 			continue;
   1359   1.4  jakllsch 		pci_intr_disestablish(psc->sc_pa.pa_pc, psc->sc_ihs[i]);
   1360   1.4  jakllsch 		psc->sc_ihs[i] = NULL;
   1361   1.4  jakllsch 	}
   1362   1.4  jakllsch 
   1363   1.4  jakllsch 	if (psc->sc_ihs_num > 0)
   1364   1.4  jakllsch 		pci_intr_release(psc->sc_pa.pa_pc, psc->sc_ihp, psc->sc_ihs_num);
   1365   1.4  jakllsch 
   1366   1.4  jakllsch 	if (psc->sc_ihs != NULL) {
   1367   1.4  jakllsch 		kmem_free(psc->sc_ihs, sizeof(*psc->sc_ihs) * psc->sc_ihs_num);
   1368   1.4  jakllsch 		psc->sc_ihs = NULL;
   1369   1.4  jakllsch 	}
   1370   1.4  jakllsch 	psc->sc_ihs_num = 0;
   1371   1.4  jakllsch }
   1372   1.4  jakllsch 
   1373   1.4  jakllsch /*
   1374   1.4  jakllsch  * Interrupt handler.
   1375   1.4  jakllsch  */
   1376   1.4  jakllsch static int
   1377   1.4  jakllsch virtio_pci_intr(void *arg)
   1378   1.4  jakllsch {
   1379   1.4  jakllsch 	struct virtio_softc *sc = arg;
   1380   1.4  jakllsch 	struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
   1381   1.4  jakllsch 	int isr, r = 0;
   1382   1.4  jakllsch 
   1383   1.4  jakllsch 	/* check and ack the interrupt */
   1384  1.15   reinoud 	isr = bus_space_read_1(psc->sc_isr_iot, psc->sc_isr_ioh, 0);
   1385   1.4  jakllsch 	if (isr == 0)
   1386   1.4  jakllsch 		return 0;
   1387   1.4  jakllsch 	if ((isr & VIRTIO_CONFIG_ISR_CONFIG_CHANGE) &&
   1388   1.4  jakllsch 	    (sc->sc_config_change != NULL))
   1389   1.4  jakllsch 		r = (sc->sc_config_change)(sc);
   1390   1.4  jakllsch 	if (sc->sc_intrhand != NULL) {
   1391   1.4  jakllsch 		if (sc->sc_soft_ih != NULL)
   1392   1.4  jakllsch 			softint_schedule(sc->sc_soft_ih);
   1393   1.4  jakllsch 		else
   1394   1.4  jakllsch 			r |= (sc->sc_intrhand)(sc);
   1395   1.4  jakllsch 	}
   1396   1.4  jakllsch 
   1397   1.4  jakllsch 	return r;
   1398   1.4  jakllsch }
   1399   1.4  jakllsch 
   1400   1.4  jakllsch static int
   1401   1.4  jakllsch virtio_pci_msix_queue_intr(void *arg)
   1402   1.4  jakllsch {
   1403   1.4  jakllsch 	struct virtio_softc *sc = arg;
   1404   1.4  jakllsch 	int r = 0;
   1405   1.4  jakllsch 
   1406   1.4  jakllsch 	if (sc->sc_intrhand != NULL) {
   1407   1.4  jakllsch 		if (sc->sc_soft_ih != NULL)
   1408   1.4  jakllsch 			softint_schedule(sc->sc_soft_ih);
   1409   1.4  jakllsch 		else
   1410   1.4  jakllsch 			r |= (sc->sc_intrhand)(sc);
   1411   1.4  jakllsch 	}
   1412   1.4  jakllsch 
   1413   1.4  jakllsch 	return r;
   1414   1.4  jakllsch }
   1415   1.4  jakllsch 
   1416   1.4  jakllsch static int
   1417   1.4  jakllsch virtio_pci_msix_config_intr(void *arg)
   1418   1.4  jakllsch {
   1419   1.4  jakllsch 	struct virtio_softc *sc = arg;
   1420   1.4  jakllsch 	int r = 0;
   1421   1.4  jakllsch 
   1422   1.4  jakllsch 	if (sc->sc_config_change != NULL)
   1423   1.4  jakllsch 		r = (sc->sc_config_change)(sc);
   1424   1.4  jakllsch 	return r;
   1425   1.4  jakllsch }
   1426   1.5  jakllsch 
   1427   1.5  jakllsch MODULE(MODULE_CLASS_DRIVER, virtio_pci, "pci,virtio");
   1428   1.5  jakllsch 
   1429   1.5  jakllsch #ifdef _MODULE
   1430   1.5  jakllsch #include "ioconf.c"
   1431   1.5  jakllsch #endif
   1432   1.5  jakllsch 
   1433   1.5  jakllsch static int
   1434   1.5  jakllsch virtio_pci_modcmd(modcmd_t cmd, void *opaque)
   1435   1.5  jakllsch {
   1436   1.5  jakllsch 	int error = 0;
   1437   1.5  jakllsch 
   1438   1.5  jakllsch #ifdef _MODULE
   1439   1.5  jakllsch 	switch (cmd) {
   1440   1.5  jakllsch 	case MODULE_CMD_INIT:
   1441   1.5  jakllsch 		error = config_init_component(cfdriver_ioconf_virtio_pci,
   1442   1.5  jakllsch 		    cfattach_ioconf_virtio_pci, cfdata_ioconf_virtio_pci);
   1443   1.5  jakllsch 		break;
   1444   1.5  jakllsch 	case MODULE_CMD_FINI:
   1445   1.5  jakllsch 		error = config_fini_component(cfdriver_ioconf_virtio_pci,
   1446   1.5  jakllsch 		    cfattach_ioconf_virtio_pci, cfdata_ioconf_virtio_pci);
   1447   1.5  jakllsch 		break;
   1448   1.5  jakllsch 	default:
   1449   1.5  jakllsch 		error = ENOTTY;
   1450   1.5  jakllsch 		break;
   1451   1.5  jakllsch 	}
   1452   1.5  jakllsch #endif
   1453   1.5  jakllsch 
   1454   1.5  jakllsch 	return error;
   1455   1.5  jakllsch }
   1456