Home | History | Annotate | Line # | Download | only in pci
virtio.c revision 1.63.2.6
      1  1.63.2.6    martin /*	$NetBSD: virtio.c,v 1.63.2.6 2024/10/02 18:20:48 martin Exp $	*/
      2       1.1   hannken 
      3       1.1   hannken /*
      4      1.43   reinoud  * Copyright (c) 2020 The NetBSD Foundation, Inc.
      5      1.43   reinoud  * Copyright (c) 2012 Stefan Fritsch, Alexander Fiveg.
      6       1.1   hannken  * Copyright (c) 2010 Minoura Makoto.
      7       1.1   hannken  * All rights reserved.
      8       1.1   hannken  *
      9       1.1   hannken  * Redistribution and use in source and binary forms, with or without
     10       1.1   hannken  * modification, are permitted provided that the following conditions
     11       1.1   hannken  * are met:
     12       1.1   hannken  * 1. Redistributions of source code must retain the above copyright
     13       1.1   hannken  *    notice, this list of conditions and the following disclaimer.
     14       1.1   hannken  * 2. Redistributions in binary form must reproduce the above copyright
     15       1.1   hannken  *    notice, this list of conditions and the following disclaimer in the
     16       1.1   hannken  *    documentation and/or other materials provided with the distribution.
     17       1.1   hannken  *
     18       1.1   hannken  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19       1.1   hannken  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20       1.1   hannken  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21       1.1   hannken  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22       1.1   hannken  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23       1.1   hannken  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24       1.1   hannken  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25       1.1   hannken  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26       1.1   hannken  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27       1.1   hannken  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28       1.1   hannken  */
     29       1.1   hannken 
     30       1.1   hannken #include <sys/cdefs.h>
     31  1.63.2.6    martin __KERNEL_RCSID(0, "$NetBSD: virtio.c,v 1.63.2.6 2024/10/02 18:20:48 martin Exp $");
     32       1.1   hannken 
     33       1.1   hannken #include <sys/param.h>
     34       1.1   hannken #include <sys/systm.h>
     35       1.1   hannken #include <sys/kernel.h>
     36       1.1   hannken #include <sys/atomic.h>
     37       1.1   hannken #include <sys/bus.h>
     38       1.1   hannken #include <sys/device.h>
     39       1.1   hannken #include <sys/kmem.h>
     40      1.18  pgoyette #include <sys/module.h>
     41       1.1   hannken 
     42      1.22  jdolecek #define VIRTIO_PRIVATE
     43      1.22  jdolecek 
     44      1.29    cherry #include <dev/pci/virtioreg.h> /* XXX: move to non-pci */
     45      1.29    cherry #include <dev/pci/virtiovar.h> /* XXX: move to non-pci */
     46       1.1   hannken 
     47       1.1   hannken #define MINSEG_INDIRECT		2 /* use indirect if nsegs >= this value */
     48       1.1   hannken 
     49  1.63.2.4    martin /*
     50  1.63.2.4    martin  * The maximum descriptor size is 2^15. Use that value as the end of
     51  1.63.2.4    martin  * descriptor chain terminator since it will never be a valid index
     52  1.63.2.4    martin  * in the descriptor table.
     53  1.63.2.4    martin  */
     54  1.63.2.4    martin #define VRING_DESC_CHAIN_END		32768
     55  1.63.2.4    martin 
     56      1.43   reinoud /* incomplete list */
     57      1.43   reinoud static const char *virtio_device_name[] = {
     58      1.43   reinoud 	"unknown (0)",			/*  0 */
     59      1.43   reinoud 	"network",			/*  1 */
     60      1.43   reinoud 	"block",			/*  2 */
     61      1.43   reinoud 	"console",			/*  3 */
     62      1.43   reinoud 	"entropy",			/*  4 */
     63      1.43   reinoud 	"memory balloon",		/*  5 */
     64      1.43   reinoud 	"I/O memory",			/*  6 */
     65      1.43   reinoud 	"remote processor messaging",	/*  7 */
     66      1.43   reinoud 	"SCSI",				/*  8 */
     67      1.43   reinoud 	"9P transport",			/*  9 */
     68      1.43   reinoud };
     69      1.43   reinoud #define NDEVNAMES	__arraycount(virtio_device_name)
     70      1.43   reinoud 
     71  1.63.2.4    martin static void	virtio_reset_vq(struct virtio_softc *,
     72  1.63.2.4    martin 		    struct virtqueue *);
     73       1.1   hannken 
     74      1.29    cherry void
     75       1.1   hannken virtio_set_status(struct virtio_softc *sc, int status)
     76       1.1   hannken {
     77      1.31  jakllsch 	sc->sc_ops->set_status(sc, status);
     78      1.11     ozaki }
     79      1.11     ozaki 
     80       1.1   hannken /*
     81       1.1   hannken  * Reset the device.
     82       1.1   hannken  */
     83       1.1   hannken /*
     84       1.1   hannken  * To reset the device to a known state, do following:
     85       1.1   hannken  *	virtio_reset(sc);	     // this will stop the device activity
     86       1.1   hannken  *	<dequeue finished requests>; // virtio_dequeue() still can be called
     87       1.1   hannken  *	<revoke pending requests in the vqs if any>;
     88  1.63.2.6    martin  *	virtio_reinit_start(sc);     // dequeue prohibited
     89       1.1   hannken  *	newfeatures = virtio_negotiate_features(sc, requestedfeatures);
     90       1.1   hannken  *	<some other initialization>;
     91       1.1   hannken  *	virtio_reinit_end(sc);	     // device activated; enqueue allowed
     92       1.1   hannken  * Once attached, feature negotiation can only be allowed after virtio_reset.
     93       1.1   hannken  */
     94       1.1   hannken void
     95       1.1   hannken virtio_reset(struct virtio_softc *sc)
     96       1.1   hannken {
     97       1.1   hannken 	virtio_device_reset(sc);
     98       1.1   hannken }
     99       1.1   hannken 
    100      1.53  yamaguch int
    101       1.1   hannken virtio_reinit_start(struct virtio_softc *sc)
    102       1.1   hannken {
    103      1.51  yamaguch 	int i, r;
    104       1.1   hannken 
    105       1.1   hannken 	virtio_set_status(sc, VIRTIO_CONFIG_DEVICE_STATUS_ACK);
    106       1.1   hannken 	virtio_set_status(sc, VIRTIO_CONFIG_DEVICE_STATUS_DRIVER);
    107       1.1   hannken 	for (i = 0; i < sc->sc_nvqs; i++) {
    108       1.1   hannken 		int n;
    109       1.1   hannken 		struct virtqueue *vq = &sc->sc_vqs[i];
    110      1.31  jakllsch 		n = sc->sc_ops->read_queue_size(sc, vq->vq_index);
    111       1.1   hannken 		if (n == 0)	/* vq disappeared */
    112       1.1   hannken 			continue;
    113       1.1   hannken 		if (n != vq->vq_num) {
    114       1.1   hannken 			panic("%s: virtqueue size changed, vq index %d\n",
    115      1.59  riastrad 			    device_xname(sc->sc_dev),
    116      1.59  riastrad 			    vq->vq_index);
    117       1.1   hannken 		}
    118  1.63.2.4    martin 		virtio_reset_vq(sc, vq);
    119      1.31  jakllsch 		sc->sc_ops->setup_queue(sc, vq->vq_index,
    120      1.43   reinoud 		    vq->vq_dmamap->dm_segs[0].ds_addr);
    121      1.11     ozaki 	}
    122      1.51  yamaguch 
    123      1.51  yamaguch 	r = sc->sc_ops->setup_interrupts(sc, 1);
    124      1.53  yamaguch 	if (r != 0)
    125      1.53  yamaguch 		goto fail;
    126      1.53  yamaguch 
    127      1.53  yamaguch 	return 0;
    128      1.53  yamaguch 
    129      1.53  yamaguch fail:
    130      1.53  yamaguch 	virtio_set_status(sc, VIRTIO_CONFIG_DEVICE_STATUS_FAILED);
    131      1.53  yamaguch 
    132      1.53  yamaguch 	return 1;
    133       1.1   hannken }
    134       1.1   hannken 
    135       1.1   hannken void
    136       1.1   hannken virtio_reinit_end(struct virtio_softc *sc)
    137       1.1   hannken {
    138       1.1   hannken 	virtio_set_status(sc, VIRTIO_CONFIG_DEVICE_STATUS_DRIVER_OK);
    139       1.1   hannken }
    140       1.1   hannken 
    141       1.1   hannken /*
    142       1.1   hannken  * Feature negotiation.
    143       1.1   hannken  */
    144      1.43   reinoud void
    145      1.43   reinoud virtio_negotiate_features(struct virtio_softc *sc, uint64_t guest_features)
    146       1.1   hannken {
    147       1.1   hannken 	if (!(device_cfdata(sc->sc_dev)->cf_flags & 1) &&
    148       1.1   hannken 	    !(device_cfdata(sc->sc_child)->cf_flags & 1)) /* XXX */
    149       1.1   hannken 		guest_features |= VIRTIO_F_RING_INDIRECT_DESC;
    150      1.43   reinoud 	sc->sc_ops->neg_features(sc, guest_features);
    151      1.43   reinoud 	if (sc->sc_active_features & VIRTIO_F_RING_INDIRECT_DESC)
    152       1.1   hannken 		sc->sc_indirect = true;
    153       1.1   hannken 	else
    154       1.1   hannken 		sc->sc_indirect = false;
    155      1.43   reinoud }
    156       1.1   hannken 
    157       1.1   hannken 
    158       1.1   hannken /*
    159      1.43   reinoud  * Device configuration registers readers/writers
    160       1.1   hannken  */
    161      1.43   reinoud #if 0
    162      1.43   reinoud #define DPRINTFR(n, fmt, val, index, num) \
    163      1.43   reinoud 	printf("\n%s (", n); \
    164      1.43   reinoud 	for (int i = 0; i < num; i++) \
    165      1.43   reinoud 		printf("%02x ", bus_space_read_1(sc->sc_devcfg_iot, sc->sc_devcfg_ioh, index+i)); \
    166      1.43   reinoud 	printf(") -> "); printf(fmt, val); printf("\n");
    167      1.45   reinoud #define DPRINTFR2(n, fmt, val_s, val_n) \
    168      1.45   reinoud 	printf("%s ", n); \
    169      1.45   reinoud 	printf("\n        stream "); printf(fmt, val_s); printf(" norm "); printf(fmt, val_n); printf("\n");
    170      1.43   reinoud #else
    171      1.43   reinoud #define DPRINTFR(n, fmt, val, index, num)
    172      1.45   reinoud #define DPRINTFR2(n, fmt, val_s, val_n)
    173      1.43   reinoud #endif
    174      1.43   reinoud 
    175      1.45   reinoud 
    176       1.1   hannken uint8_t
    177      1.59  riastrad virtio_read_device_config_1(struct virtio_softc *sc, int index)
    178      1.59  riastrad {
    179      1.45   reinoud 	bus_space_tag_t	   iot = sc->sc_devcfg_iot;
    180      1.45   reinoud 	bus_space_handle_t ioh = sc->sc_devcfg_ioh;
    181      1.43   reinoud 	uint8_t val;
    182      1.45   reinoud 
    183      1.45   reinoud 	val = bus_space_read_1(iot, ioh, index);
    184      1.45   reinoud 
    185      1.43   reinoud 	DPRINTFR("read_1", "%02x", val, index, 1);
    186      1.43   reinoud 	return val;
    187      1.43   reinoud }
    188      1.43   reinoud 
    189      1.43   reinoud uint16_t
    190      1.59  riastrad virtio_read_device_config_2(struct virtio_softc *sc, int index)
    191      1.59  riastrad {
    192      1.45   reinoud 	bus_space_tag_t	   iot = sc->sc_devcfg_iot;
    193      1.45   reinoud 	bus_space_handle_t ioh = sc->sc_devcfg_ioh;
    194      1.43   reinoud 	uint16_t val;
    195      1.45   reinoud 
    196      1.45   reinoud 	val = bus_space_read_2(iot, ioh, index);
    197      1.45   reinoud 	if (BYTE_ORDER != sc->sc_bus_endian)
    198      1.45   reinoud 		val = bswap16(val);
    199      1.45   reinoud 
    200      1.43   reinoud 	DPRINTFR("read_2", "%04x", val, index, 2);
    201      1.45   reinoud 	DPRINTFR2("read_2", "%04x",
    202      1.59  riastrad 	    bus_space_read_stream_2(sc->sc_devcfg_iot, sc->sc_devcfg_ioh,
    203      1.59  riastrad 		index),
    204      1.59  riastrad 	    bus_space_read_2(sc->sc_devcfg_iot, sc->sc_devcfg_ioh, index));
    205      1.43   reinoud 	return val;
    206      1.43   reinoud }
    207      1.43   reinoud 
    208      1.43   reinoud uint32_t
    209      1.59  riastrad virtio_read_device_config_4(struct virtio_softc *sc, int index)
    210      1.59  riastrad {
    211      1.45   reinoud 	bus_space_tag_t	   iot = sc->sc_devcfg_iot;
    212      1.45   reinoud 	bus_space_handle_t ioh = sc->sc_devcfg_ioh;
    213      1.43   reinoud 	uint32_t val;
    214      1.45   reinoud 
    215      1.45   reinoud 	val = bus_space_read_4(iot, ioh, index);
    216      1.45   reinoud 	if (BYTE_ORDER != sc->sc_bus_endian)
    217      1.45   reinoud 		val = bswap32(val);
    218      1.45   reinoud 
    219      1.43   reinoud 	DPRINTFR("read_4", "%08x", val, index, 4);
    220      1.45   reinoud 	DPRINTFR2("read_4", "%08x",
    221      1.59  riastrad 	    bus_space_read_stream_4(sc->sc_devcfg_iot, sc->sc_devcfg_ioh,
    222      1.59  riastrad 		index),
    223      1.59  riastrad 	    bus_space_read_4(sc->sc_devcfg_iot, sc->sc_devcfg_ioh, index));
    224      1.43   reinoud 	return val;
    225      1.43   reinoud }
    226      1.43   reinoud 
    227      1.46   reinoud /*
    228      1.46   reinoud  * The Virtio spec explicitly tells that reading and writing 8 bytes are not
    229      1.46   reinoud  * considered atomic and no triggers may be connected to reading or writing
    230      1.47   reinoud  * it. We access it using two 32 reads. See virtio spec 4.1.3.1.
    231      1.46   reinoud  */
    232      1.43   reinoud uint64_t
    233      1.59  riastrad virtio_read_device_config_8(struct virtio_softc *sc, int index)
    234      1.59  riastrad {
    235      1.45   reinoud 	bus_space_tag_t	   iot = sc->sc_devcfg_iot;
    236      1.45   reinoud 	bus_space_handle_t ioh = sc->sc_devcfg_ioh;
    237      1.46   reinoud 	union {
    238      1.46   reinoud 		uint64_t u64;
    239      1.47   reinoud 		uint32_t l[2];
    240      1.46   reinoud 	} v;
    241      1.46   reinoud 	uint64_t val;
    242      1.46   reinoud 
    243      1.47   reinoud 	v.l[0] = bus_space_read_4(iot, ioh, index);
    244      1.47   reinoud 	v.l[1] = bus_space_read_4(iot, ioh, index + 4);
    245      1.47   reinoud 	if (sc->sc_bus_endian != sc->sc_struct_endian) {
    246      1.47   reinoud 		v.l[0] = bswap32(v.l[0]);
    247      1.47   reinoud 		v.l[1] = bswap32(v.l[1]);
    248      1.47   reinoud 	}
    249      1.46   reinoud 	val = v.u64;
    250      1.45   reinoud 
    251      1.46   reinoud 	if (BYTE_ORDER != sc->sc_struct_endian)
    252      1.46   reinoud 		val = bswap64(val);
    253      1.45   reinoud 
    254      1.63    simonb 	DPRINTFR("read_8", "%08"PRIx64, val, index, 8);
    255      1.45   reinoud 	DPRINTFR2("read_8 low ", "%08x",
    256      1.59  riastrad 	    bus_space_read_stream_4(sc->sc_devcfg_iot, sc->sc_devcfg_ioh,
    257      1.59  riastrad 		index),
    258      1.59  riastrad 	    bus_space_read_4(sc->sc_devcfg_iot, sc->sc_devcfg_ioh, index));
    259      1.45   reinoud 	DPRINTFR2("read_8 high ", "%08x",
    260      1.59  riastrad 	    bus_space_read_stream_4(sc->sc_devcfg_iot, sc->sc_devcfg_ioh,
    261      1.59  riastrad 		index + 4),
    262      1.59  riastrad 	    bus_space_read_4(sc->sc_devcfg_iot, sc->sc_devcfg_ioh, index + 4));
    263      1.43   reinoud 	return val;
    264       1.1   hannken }
    265       1.1   hannken 
    266      1.43   reinoud /*
    267      1.43   reinoud  * In the older virtio spec, device config registers are host endian. On newer
    268      1.45   reinoud  * they are little endian. Some newer devices however explicitly specify their
    269      1.55    andvar  * register to always be little endian. These functions cater for these.
    270      1.43   reinoud  */
    271       1.1   hannken uint16_t
    272      1.59  riastrad virtio_read_device_config_le_2(struct virtio_softc *sc, int index)
    273      1.59  riastrad {
    274      1.45   reinoud 	bus_space_tag_t	   iot = sc->sc_devcfg_iot;
    275      1.45   reinoud 	bus_space_handle_t ioh = sc->sc_devcfg_ioh;
    276      1.43   reinoud 	uint16_t val;
    277      1.43   reinoud 
    278      1.45   reinoud 	val = bus_space_read_2(iot, ioh, index);
    279  1.63.2.6    martin #if !defined(__aarch64__) && !defined(__arm__)
    280  1.63.2.6    martin 	/*
    281  1.63.2.6    martin 	 * For big-endian aarch64/armv7, bus endian is always LSB, but
    282  1.63.2.6    martin 	 * byte-order is automatically swapped by bus_space(9) (see also
    283  1.63.2.6    martin 	 * comments in virtio_pci.c). Therefore, no need to swap here.
    284  1.63.2.6    martin 	 */
    285      1.45   reinoud 	if (sc->sc_bus_endian != LITTLE_ENDIAN)
    286      1.45   reinoud 		val = bswap16(val);
    287  1.63.2.6    martin #endif
    288      1.45   reinoud 
    289      1.45   reinoud 	DPRINTFR("read_le_2", "%04x", val, index, 2);
    290      1.45   reinoud 	DPRINTFR2("read_le_2", "%04x",
    291      1.59  riastrad 	    bus_space_read_stream_2(sc->sc_devcfg_iot, sc->sc_devcfg_ioh, 0),
    292      1.59  riastrad 	    bus_space_read_2(sc->sc_devcfg_iot, sc->sc_devcfg_ioh, 0));
    293      1.43   reinoud 	return val;
    294       1.1   hannken }
    295       1.1   hannken 
    296       1.1   hannken uint32_t
    297      1.59  riastrad virtio_read_device_config_le_4(struct virtio_softc *sc, int index)
    298      1.59  riastrad {
    299      1.45   reinoud 	bus_space_tag_t	   iot = sc->sc_devcfg_iot;
    300      1.45   reinoud 	bus_space_handle_t ioh = sc->sc_devcfg_ioh;
    301      1.43   reinoud 	uint32_t val;
    302      1.43   reinoud 
    303      1.45   reinoud 	val = bus_space_read_4(iot, ioh, index);
    304  1.63.2.6    martin #if !defined(__aarch64__) && !defined(__arm__)
    305  1.63.2.6    martin 	/* See virtio_read_device_config_le_2() above. */
    306      1.45   reinoud 	if (sc->sc_bus_endian != LITTLE_ENDIAN)
    307      1.45   reinoud 		val = bswap32(val);
    308  1.63.2.6    martin #endif
    309      1.45   reinoud 
    310      1.43   reinoud 	DPRINTFR("read_le_4", "%08x", val, index, 4);
    311      1.45   reinoud 	DPRINTFR2("read_le_4", "%08x",
    312      1.59  riastrad 	    bus_space_read_stream_4(sc->sc_devcfg_iot, sc->sc_devcfg_ioh, 0),
    313      1.59  riastrad 	    bus_space_read_4(sc->sc_devcfg_iot, sc->sc_devcfg_ioh, 0));
    314      1.43   reinoud 	return val;
    315      1.43   reinoud }
    316      1.43   reinoud 
    317      1.43   reinoud void
    318      1.43   reinoud virtio_write_device_config_1(struct virtio_softc *sc, int index, uint8_t value)
    319       1.1   hannken {
    320      1.45   reinoud 	bus_space_tag_t	   iot = sc->sc_devcfg_iot;
    321      1.45   reinoud 	bus_space_handle_t ioh = sc->sc_devcfg_ioh;
    322      1.45   reinoud 
    323      1.45   reinoud 	bus_space_write_1(iot, ioh, index, value);
    324       1.1   hannken }
    325       1.1   hannken 
    326      1.43   reinoud void
    327      1.59  riastrad virtio_write_device_config_2(struct virtio_softc *sc, int index,
    328      1.59  riastrad     uint16_t value)
    329       1.1   hannken {
    330      1.45   reinoud 	bus_space_tag_t	   iot = sc->sc_devcfg_iot;
    331      1.45   reinoud 	bus_space_handle_t ioh = sc->sc_devcfg_ioh;
    332      1.45   reinoud 
    333      1.45   reinoud 	if (BYTE_ORDER != sc->sc_bus_endian)
    334      1.45   reinoud 		value = bswap16(value);
    335      1.45   reinoud 	bus_space_write_2(iot, ioh, index, value);
    336       1.1   hannken }
    337       1.1   hannken 
    338       1.1   hannken void
    339      1.59  riastrad virtio_write_device_config_4(struct virtio_softc *sc, int index,
    340      1.59  riastrad     uint32_t value)
    341       1.1   hannken {
    342      1.45   reinoud 	bus_space_tag_t	   iot = sc->sc_devcfg_iot;
    343      1.45   reinoud 	bus_space_handle_t ioh = sc->sc_devcfg_ioh;
    344      1.45   reinoud 
    345      1.45   reinoud 	if (BYTE_ORDER != sc->sc_bus_endian)
    346      1.45   reinoud 		value = bswap32(value);
    347      1.45   reinoud 	bus_space_write_4(iot, ioh, index, value);
    348       1.1   hannken }
    349       1.1   hannken 
    350      1.46   reinoud /*
    351      1.46   reinoud  * The Virtio spec explicitly tells that reading and writing 8 bytes are not
    352      1.46   reinoud  * considered atomic and no triggers may be connected to reading or writing
    353      1.47   reinoud  * it. We access it using two 32 bit writes. For good measure it is stated to
    354      1.47   reinoud  * always write lsb first just in case of a hypervisor bug. See See virtio
    355      1.47   reinoud  * spec 4.1.3.1.
    356      1.46   reinoud  */
    357       1.1   hannken void
    358      1.59  riastrad virtio_write_device_config_8(struct virtio_softc *sc, int index,
    359      1.59  riastrad     uint64_t value)
    360       1.1   hannken {
    361      1.45   reinoud 	bus_space_tag_t	   iot = sc->sc_devcfg_iot;
    362      1.45   reinoud 	bus_space_handle_t ioh = sc->sc_devcfg_ioh;
    363      1.46   reinoud 	union {
    364      1.46   reinoud 		uint64_t u64;
    365      1.47   reinoud 		uint32_t l[2];
    366      1.46   reinoud 	} v;
    367      1.46   reinoud 
    368      1.46   reinoud 	if (BYTE_ORDER != sc->sc_struct_endian)
    369      1.46   reinoud 		value = bswap64(value);
    370      1.46   reinoud 
    371      1.46   reinoud 	v.u64 = value;
    372      1.47   reinoud 	if (sc->sc_bus_endian != sc->sc_struct_endian) {
    373      1.47   reinoud 		v.l[0] = bswap32(v.l[0]);
    374      1.47   reinoud 		v.l[1] = bswap32(v.l[1]);
    375      1.47   reinoud 	}
    376      1.47   reinoud 
    377      1.47   reinoud 	if (sc->sc_struct_endian == LITTLE_ENDIAN) {
    378      1.47   reinoud 		bus_space_write_4(iot, ioh, index,     v.l[0]);
    379      1.47   reinoud 		bus_space_write_4(iot, ioh, index + 4, v.l[1]);
    380      1.47   reinoud 	} else {
    381      1.47   reinoud 		bus_space_write_4(iot, ioh, index + 4, v.l[1]);
    382      1.47   reinoud 		bus_space_write_4(iot, ioh, index,     v.l[0]);
    383      1.47   reinoud 	}
    384       1.1   hannken }
    385       1.1   hannken 
    386      1.43   reinoud /*
    387      1.43   reinoud  * In the older virtio spec, device config registers are host endian. On newer
    388      1.45   reinoud  * they are little endian. Some newer devices however explicitly specify their
    389      1.55    andvar  * register to always be little endian. These functions cater for these.
    390      1.43   reinoud  */
    391       1.1   hannken void
    392      1.59  riastrad virtio_write_device_config_le_2(struct virtio_softc *sc, int index,
    393      1.59  riastrad     uint16_t value)
    394       1.1   hannken {
    395      1.45   reinoud 	bus_space_tag_t	   iot = sc->sc_devcfg_iot;
    396      1.45   reinoud 	bus_space_handle_t ioh = sc->sc_devcfg_ioh;
    397      1.45   reinoud 
    398      1.45   reinoud 	if (sc->sc_bus_endian != LITTLE_ENDIAN)
    399      1.45   reinoud 		value = bswap16(value);
    400      1.45   reinoud 	bus_space_write_2(iot, ioh, index, value);
    401       1.1   hannken }
    402       1.1   hannken 
    403       1.1   hannken void
    404      1.59  riastrad virtio_write_device_config_le_4(struct virtio_softc *sc, int index,
    405      1.59  riastrad     uint32_t value)
    406      1.43   reinoud {
    407      1.45   reinoud 	bus_space_tag_t	   iot = sc->sc_devcfg_iot;
    408      1.45   reinoud 	bus_space_handle_t ioh = sc->sc_devcfg_ioh;
    409      1.45   reinoud 
    410      1.45   reinoud 	if (sc->sc_bus_endian != LITTLE_ENDIAN)
    411      1.45   reinoud 		value = bswap32(value);
    412      1.45   reinoud 	bus_space_write_4(iot, ioh, index, value);
    413      1.43   reinoud }
    414      1.43   reinoud 
    415      1.45   reinoud 
    416      1.43   reinoud /*
    417      1.43   reinoud  * data structures endian helpers
    418      1.43   reinoud  */
    419      1.59  riastrad uint16_t
    420      1.59  riastrad virtio_rw16(struct virtio_softc *sc, uint16_t val)
    421       1.1   hannken {
    422      1.43   reinoud 	KASSERT(sc);
    423      1.45   reinoud 	return BYTE_ORDER != sc->sc_struct_endian ? bswap16(val) : val;
    424       1.1   hannken }
    425       1.1   hannken 
    426      1.59  riastrad uint32_t
    427      1.59  riastrad virtio_rw32(struct virtio_softc *sc, uint32_t val)
    428      1.43   reinoud {
    429      1.43   reinoud 	KASSERT(sc);
    430      1.45   reinoud 	return BYTE_ORDER != sc->sc_struct_endian ? bswap32(val) : val;
    431      1.43   reinoud }
    432      1.43   reinoud 
    433      1.59  riastrad uint64_t
    434      1.59  riastrad virtio_rw64(struct virtio_softc *sc, uint64_t val)
    435      1.43   reinoud {
    436      1.43   reinoud 	KASSERT(sc);
    437      1.45   reinoud 	return BYTE_ORDER != sc->sc_struct_endian ? bswap64(val) : val;
    438      1.43   reinoud }
    439      1.43   reinoud 
    440      1.43   reinoud 
    441       1.1   hannken /*
    442       1.1   hannken  * Interrupt handler.
    443       1.1   hannken  */
    444       1.8     ozaki static void
    445       1.8     ozaki virtio_soft_intr(void *arg)
    446       1.8     ozaki {
    447       1.8     ozaki 	struct virtio_softc *sc = arg;
    448       1.8     ozaki 
    449       1.8     ozaki 	KASSERT(sc->sc_intrhand != NULL);
    450       1.8     ozaki 
    451      1.54       uwe 	(*sc->sc_intrhand)(sc);
    452       1.8     ozaki }
    453       1.8     ozaki 
    454  1.63.2.4    martin /* set to vq->vq_intrhand in virtio_init_vq_vqdone() */
    455  1.63.2.4    martin static int
    456  1.63.2.4    martin virtio_vq_done(void *xvq)
    457  1.63.2.4    martin {
    458  1.63.2.4    martin 	struct virtqueue *vq = xvq;
    459  1.63.2.4    martin 
    460  1.63.2.4    martin 	return vq->vq_done(vq);
    461  1.63.2.4    martin }
    462  1.63.2.4    martin 
    463  1.63.2.4    martin static int
    464  1.63.2.4    martin virtio_vq_intr(struct virtio_softc *sc)
    465  1.63.2.4    martin {
    466  1.63.2.4    martin 	struct virtqueue *vq;
    467  1.63.2.4    martin 	int i, r = 0;
    468  1.63.2.4    martin 
    469  1.63.2.4    martin 	for (i = 0; i < sc->sc_nvqs; i++) {
    470  1.63.2.4    martin 		vq = &sc->sc_vqs[i];
    471  1.63.2.4    martin 		if (virtio_vq_is_enqueued(sc, vq) == 1) {
    472  1.63.2.4    martin 			r |= (*vq->vq_intrhand)(vq->vq_intrhand_arg);
    473  1.63.2.4    martin 		}
    474  1.63.2.4    martin 	}
    475  1.63.2.4    martin 
    476  1.63.2.4    martin 	return r;
    477  1.63.2.4    martin }
    478  1.63.2.4    martin 
    479       1.1   hannken /*
    480       1.1   hannken  * dmamap sync operations for a virtqueue.
    481       1.1   hannken  */
    482       1.1   hannken static inline void
    483       1.1   hannken vq_sync_descs(struct virtio_softc *sc, struct virtqueue *vq, int ops)
    484       1.1   hannken {
    485      1.57  riastrad 
    486      1.62     skrll 	/* availoffset == sizeof(vring_desc) * vq_num */
    487       1.1   hannken 	bus_dmamap_sync(sc->sc_dmat, vq->vq_dmamap, 0, vq->vq_availoffset,
    488      1.57  riastrad 	    ops);
    489       1.1   hannken }
    490       1.1   hannken 
    491       1.1   hannken static inline void
    492      1.57  riastrad vq_sync_aring_all(struct virtio_softc *sc, struct virtqueue *vq, int ops)
    493       1.1   hannken {
    494      1.43   reinoud 	uint16_t hdrlen = offsetof(struct vring_avail, ring);
    495  1.63.2.1    martin 	size_t payloadlen = vq->vq_num * sizeof(uint16_t);
    496      1.57  riastrad 	size_t usedlen = 0;
    497      1.57  riastrad 
    498      1.43   reinoud 	if (sc->sc_active_features & VIRTIO_F_RING_EVENT_IDX)
    499      1.57  riastrad 		usedlen = sizeof(uint16_t);
    500      1.57  riastrad 	bus_dmamap_sync(sc->sc_dmat, vq->vq_dmamap,
    501      1.57  riastrad 	    vq->vq_availoffset, hdrlen + payloadlen + usedlen, ops);
    502      1.57  riastrad }
    503      1.57  riastrad 
    504      1.57  riastrad static inline void
    505      1.57  riastrad vq_sync_aring_header(struct virtio_softc *sc, struct virtqueue *vq, int ops)
    506      1.57  riastrad {
    507      1.57  riastrad 	uint16_t hdrlen = offsetof(struct vring_avail, ring);
    508      1.57  riastrad 
    509      1.57  riastrad 	bus_dmamap_sync(sc->sc_dmat, vq->vq_dmamap,
    510      1.57  riastrad 	    vq->vq_availoffset, hdrlen, ops);
    511      1.57  riastrad }
    512      1.57  riastrad 
    513      1.57  riastrad static inline void
    514      1.57  riastrad vq_sync_aring_payload(struct virtio_softc *sc, struct virtqueue *vq, int ops)
    515      1.57  riastrad {
    516      1.57  riastrad 	uint16_t hdrlen = offsetof(struct vring_avail, ring);
    517  1.63.2.1    martin 	size_t payloadlen = vq->vq_num * sizeof(uint16_t);
    518      1.43   reinoud 
    519       1.1   hannken 	bus_dmamap_sync(sc->sc_dmat, vq->vq_dmamap,
    520      1.57  riastrad 	    vq->vq_availoffset + hdrlen, payloadlen, ops);
    521       1.1   hannken }
    522       1.1   hannken 
    523       1.1   hannken static inline void
    524      1.57  riastrad vq_sync_aring_used(struct virtio_softc *sc, struct virtqueue *vq, int ops)
    525      1.57  riastrad {
    526      1.57  riastrad 	uint16_t hdrlen = offsetof(struct vring_avail, ring);
    527  1.63.2.1    martin 	size_t payloadlen = vq->vq_num * sizeof(uint16_t);
    528      1.57  riastrad 	size_t usedlen = sizeof(uint16_t);
    529      1.57  riastrad 
    530      1.57  riastrad 	if ((sc->sc_active_features & VIRTIO_F_RING_EVENT_IDX) == 0)
    531      1.57  riastrad 		return;
    532      1.57  riastrad 	bus_dmamap_sync(sc->sc_dmat, vq->vq_dmamap,
    533      1.57  riastrad 	    vq->vq_availoffset + hdrlen + payloadlen, usedlen, ops);
    534      1.57  riastrad }
    535      1.57  riastrad 
    536      1.57  riastrad static inline void
    537      1.57  riastrad vq_sync_uring_all(struct virtio_softc *sc, struct virtqueue *vq, int ops)
    538       1.1   hannken {
    539      1.43   reinoud 	uint16_t hdrlen = offsetof(struct vring_used, ring);
    540  1.63.2.1    martin 	size_t payloadlen = vq->vq_num * sizeof(struct vring_used_elem);
    541      1.57  riastrad 	size_t availlen = 0;
    542      1.57  riastrad 
    543      1.43   reinoud 	if (sc->sc_active_features & VIRTIO_F_RING_EVENT_IDX)
    544      1.57  riastrad 		availlen = sizeof(uint16_t);
    545      1.57  riastrad 	bus_dmamap_sync(sc->sc_dmat, vq->vq_dmamap,
    546      1.57  riastrad 	    vq->vq_usedoffset, hdrlen + payloadlen + availlen, ops);
    547      1.57  riastrad }
    548      1.57  riastrad 
    549      1.57  riastrad static inline void
    550      1.57  riastrad vq_sync_uring_header(struct virtio_softc *sc, struct virtqueue *vq, int ops)
    551      1.57  riastrad {
    552      1.57  riastrad 	uint16_t hdrlen = offsetof(struct vring_used, ring);
    553      1.43   reinoud 
    554       1.1   hannken 	bus_dmamap_sync(sc->sc_dmat, vq->vq_dmamap,
    555      1.57  riastrad 	    vq->vq_usedoffset, hdrlen, ops);
    556      1.57  riastrad }
    557      1.57  riastrad 
    558      1.57  riastrad static inline void
    559      1.57  riastrad vq_sync_uring_payload(struct virtio_softc *sc, struct virtqueue *vq, int ops)
    560      1.57  riastrad {
    561      1.57  riastrad 	uint16_t hdrlen = offsetof(struct vring_used, ring);
    562  1.63.2.1    martin 	size_t payloadlen = vq->vq_num * sizeof(struct vring_used_elem);
    563      1.57  riastrad 
    564      1.57  riastrad 	bus_dmamap_sync(sc->sc_dmat, vq->vq_dmamap,
    565      1.57  riastrad 	    vq->vq_usedoffset + hdrlen, payloadlen, ops);
    566      1.57  riastrad }
    567      1.57  riastrad 
    568      1.57  riastrad static inline void
    569      1.57  riastrad vq_sync_uring_avail(struct virtio_softc *sc, struct virtqueue *vq, int ops)
    570      1.57  riastrad {
    571      1.57  riastrad 	uint16_t hdrlen = offsetof(struct vring_used, ring);
    572  1.63.2.1    martin 	size_t payloadlen = vq->vq_num * sizeof(struct vring_used_elem);
    573      1.57  riastrad 	size_t availlen = sizeof(uint16_t);
    574      1.57  riastrad 
    575      1.57  riastrad 	if ((sc->sc_active_features & VIRTIO_F_RING_EVENT_IDX) == 0)
    576      1.57  riastrad 		return;
    577      1.57  riastrad 	bus_dmamap_sync(sc->sc_dmat, vq->vq_dmamap,
    578      1.57  riastrad 	    vq->vq_usedoffset + hdrlen + payloadlen, availlen, ops);
    579       1.1   hannken }
    580       1.1   hannken 
    581       1.1   hannken static inline void
    582       1.1   hannken vq_sync_indirect(struct virtio_softc *sc, struct virtqueue *vq, int slot,
    583      1.57  riastrad     int ops)
    584       1.1   hannken {
    585      1.57  riastrad 	int offset = vq->vq_indirectoffset +
    586      1.57  riastrad 	    sizeof(struct vring_desc) * vq->vq_maxnsegs * slot;
    587       1.1   hannken 
    588       1.1   hannken 	bus_dmamap_sync(sc->sc_dmat, vq->vq_dmamap,
    589      1.57  riastrad 	    offset, sizeof(struct vring_desc) * vq->vq_maxnsegs, ops);
    590       1.1   hannken }
    591       1.1   hannken 
    592      1.41  yamaguch bool
    593      1.41  yamaguch virtio_vq_is_enqueued(struct virtio_softc *sc, struct virtqueue *vq)
    594      1.37  yamaguch {
    595      1.37  yamaguch 
    596      1.37  yamaguch 	if (vq->vq_queued) {
    597      1.37  yamaguch 		vq->vq_queued = 0;
    598      1.57  riastrad 		vq_sync_aring_all(sc, vq, BUS_DMASYNC_POSTWRITE);
    599      1.37  yamaguch 	}
    600      1.37  yamaguch 
    601      1.57  riastrad 	vq_sync_uring_header(sc, vq, BUS_DMASYNC_POSTREAD);
    602      1.57  riastrad 	if (vq->vq_used_idx == virtio_rw16(sc, vq->vq_used->idx))
    603      1.57  riastrad 		return 0;
    604      1.57  riastrad 	vq_sync_uring_payload(sc, vq, BUS_DMASYNC_POSTREAD);
    605      1.57  riastrad 	return 1;
    606      1.37  yamaguch }
    607      1.37  yamaguch 
    608      1.56  riastrad /*
    609      1.43   reinoud  * Increase the event index in order to delay interrupts.
    610      1.43   reinoud  */
    611      1.43   reinoud int
    612      1.43   reinoud virtio_postpone_intr(struct virtio_softc *sc, struct virtqueue *vq,
    613      1.59  riastrad     uint16_t nslots)
    614      1.43   reinoud {
    615      1.43   reinoud 	uint16_t	idx, nused;
    616      1.43   reinoud 
    617      1.43   reinoud 	idx = vq->vq_used_idx + nslots;
    618      1.43   reinoud 
    619      1.43   reinoud 	/* set the new event index: avail_ring->used_event = idx */
    620      1.43   reinoud 	*vq->vq_used_event = virtio_rw16(sc, idx);
    621      1.57  riastrad 	vq_sync_aring_used(vq->vq_owner, vq, BUS_DMASYNC_PREWRITE);
    622      1.43   reinoud 	vq->vq_queued++;
    623      1.43   reinoud 
    624      1.43   reinoud 	nused = (uint16_t)
    625      1.59  riastrad 	    (virtio_rw16(sc, vq->vq_used->idx) - vq->vq_used_idx);
    626      1.43   reinoud 	KASSERT(nused <= vq->vq_num);
    627      1.43   reinoud 
    628      1.43   reinoud 	return nslots < nused;
    629      1.43   reinoud }
    630      1.43   reinoud 
    631      1.43   reinoud /*
    632      1.43   reinoud  * Postpone interrupt until 3/4 of the available descriptors have been
    633      1.43   reinoud  * consumed.
    634      1.43   reinoud  */
    635      1.43   reinoud int
    636      1.43   reinoud virtio_postpone_intr_smart(struct virtio_softc *sc, struct virtqueue *vq)
    637      1.43   reinoud {
    638      1.43   reinoud 	uint16_t	nslots;
    639      1.43   reinoud 
    640      1.43   reinoud 	nslots = (uint16_t)
    641      1.59  riastrad 	    (virtio_rw16(sc, vq->vq_avail->idx) - vq->vq_used_idx) * 3 / 4;
    642      1.43   reinoud 
    643      1.43   reinoud 	return virtio_postpone_intr(sc, vq, nslots);
    644      1.43   reinoud }
    645      1.43   reinoud 
    646      1.43   reinoud /*
    647      1.43   reinoud  * Postpone interrupt until all of the available descriptors have been
    648      1.43   reinoud  * consumed.
    649      1.43   reinoud  */
    650      1.43   reinoud int
    651      1.43   reinoud virtio_postpone_intr_far(struct virtio_softc *sc, struct virtqueue *vq)
    652      1.43   reinoud {
    653      1.43   reinoud 	uint16_t	nslots;
    654      1.43   reinoud 
    655      1.43   reinoud 	nslots = (uint16_t)
    656      1.59  riastrad 	    (virtio_rw16(sc, vq->vq_avail->idx) - vq->vq_used_idx);
    657      1.43   reinoud 
    658      1.43   reinoud 	return virtio_postpone_intr(sc, vq, nslots);
    659      1.43   reinoud }
    660      1.43   reinoud 
    661       1.1   hannken /*
    662       1.1   hannken  * Start/stop vq interrupt.  No guarantee.
    663       1.1   hannken  */
    664       1.1   hannken void
    665       1.1   hannken virtio_stop_vq_intr(struct virtio_softc *sc, struct virtqueue *vq)
    666       1.1   hannken {
    667      1.57  riastrad 
    668      1.43   reinoud 	if (sc->sc_active_features & VIRTIO_F_RING_EVENT_IDX) {
    669      1.43   reinoud 		/*
    670      1.43   reinoud 		 * No way to disable the interrupt completely with
    671      1.43   reinoud 		 * RingEventIdx. Instead advance used_event by half the
    672      1.43   reinoud 		 * possible value. This won't happen soon and is far enough in
    673  1.63.2.6    martin 		 * the past to not trigger a spurious interrupt.
    674      1.43   reinoud 		 */
    675      1.43   reinoud 		*vq->vq_used_event = virtio_rw16(sc, vq->vq_used_idx + 0x8000);
    676      1.57  riastrad 		vq_sync_aring_used(sc, vq, BUS_DMASYNC_PREWRITE);
    677      1.43   reinoud 	} else {
    678      1.57  riastrad 		vq->vq_avail->flags |=
    679      1.57  riastrad 		    virtio_rw16(sc, VRING_AVAIL_F_NO_INTERRUPT);
    680      1.57  riastrad 		vq_sync_aring_header(sc, vq, BUS_DMASYNC_PREWRITE);
    681      1.43   reinoud 	}
    682       1.1   hannken 	vq->vq_queued++;
    683       1.1   hannken }
    684       1.1   hannken 
    685      1.43   reinoud int
    686       1.1   hannken virtio_start_vq_intr(struct virtio_softc *sc, struct virtqueue *vq)
    687       1.1   hannken {
    688      1.57  riastrad 
    689      1.43   reinoud 	if (sc->sc_active_features & VIRTIO_F_RING_EVENT_IDX) {
    690      1.43   reinoud 		/*
    691      1.43   reinoud 		 * If event index feature is negotiated, enabling interrupts
    692      1.43   reinoud 		 * is done through setting the latest consumed index in the
    693      1.43   reinoud 		 * used_event field
    694      1.43   reinoud 		 */
    695      1.43   reinoud 		*vq->vq_used_event = virtio_rw16(sc, vq->vq_used_idx);
    696      1.57  riastrad 		vq_sync_aring_used(sc, vq, BUS_DMASYNC_PREWRITE);
    697      1.43   reinoud 	} else {
    698      1.57  riastrad 		vq->vq_avail->flags &=
    699      1.57  riastrad 		    ~virtio_rw16(sc, VRING_AVAIL_F_NO_INTERRUPT);
    700      1.57  riastrad 		vq_sync_aring_header(sc, vq, BUS_DMASYNC_PREWRITE);
    701      1.43   reinoud 	}
    702       1.1   hannken 	vq->vq_queued++;
    703      1.43   reinoud 
    704      1.57  riastrad 	vq_sync_uring_header(sc, vq, BUS_DMASYNC_POSTREAD);
    705      1.57  riastrad 	if (vq->vq_used_idx == virtio_rw16(sc, vq->vq_used->idx))
    706      1.57  riastrad 		return 0;
    707      1.57  riastrad 	vq_sync_uring_payload(sc, vq, BUS_DMASYNC_POSTREAD);
    708      1.57  riastrad 	return 1;
    709       1.1   hannken }
    710       1.1   hannken 
    711       1.1   hannken /*
    712       1.1   hannken  * Initialize vq structure.
    713       1.1   hannken  */
    714  1.63.2.4    martin /*
    715  1.63.2.4    martin  * Reset virtqueue parameters
    716  1.63.2.4    martin  */
    717       1.1   hannken static void
    718  1.63.2.4    martin virtio_reset_vq(struct virtio_softc *sc, struct virtqueue *vq)
    719       1.1   hannken {
    720  1.63.2.4    martin 	struct vring_desc *vds;
    721       1.1   hannken 	int i, j;
    722       1.1   hannken 	int vq_size = vq->vq_num;
    723       1.1   hannken 
    724       1.1   hannken 	memset(vq->vq_vaddr, 0, vq->vq_bytesize);
    725       1.1   hannken 
    726  1.63.2.4    martin 	/* build the descriptor chain for free slot management */
    727  1.63.2.4    martin 	vds = vq->vq_desc;
    728  1.63.2.4    martin 	for (i = 0; i < vq_size - 1; i++) {
    729  1.63.2.4    martin 		vds[i].next = virtio_rw16(sc, i + 1);
    730  1.63.2.4    martin 	}
    731  1.63.2.4    martin 	vds[i].next = virtio_rw16(sc, VRING_DESC_CHAIN_END);
    732  1.63.2.4    martin 	vq->vq_free_idx = 0;
    733  1.63.2.4    martin 
    734       1.1   hannken 	/* build the indirect descriptor chain */
    735       1.1   hannken 	if (vq->vq_indirect != NULL) {
    736       1.1   hannken 		struct vring_desc *vd;
    737       1.1   hannken 
    738       1.1   hannken 		for (i = 0; i < vq_size; i++) {
    739       1.1   hannken 			vd = vq->vq_indirect;
    740       1.1   hannken 			vd += vq->vq_maxnsegs * i;
    741      1.59  riastrad 			for (j = 0; j < vq->vq_maxnsegs - 1; j++) {
    742      1.43   reinoud 				vd[j].next = virtio_rw16(sc, j + 1);
    743      1.23    martin 			}
    744       1.1   hannken 		}
    745       1.1   hannken 	}
    746       1.1   hannken 
    747       1.1   hannken 	/* enqueue/dequeue status */
    748       1.1   hannken 	vq->vq_avail_idx = 0;
    749       1.1   hannken 	vq->vq_used_idx = 0;
    750       1.1   hannken 	vq->vq_queued = 0;
    751      1.57  riastrad 	vq_sync_uring_all(sc, vq, BUS_DMASYNC_PREREAD);
    752       1.1   hannken 	vq->vq_queued++;
    753       1.1   hannken }
    754      1.48     skrll 
    755  1.63.2.4    martin /* Initialize vq */
    756  1.63.2.4    martin void
    757  1.63.2.4    martin virtio_init_vq_vqdone(struct virtio_softc *sc, struct virtqueue *vq,
    758  1.63.2.4    martin     int index, int (*vq_done)(struct virtqueue *))
    759  1.63.2.4    martin {
    760  1.63.2.4    martin 
    761  1.63.2.4    martin 	virtio_init_vq(sc, vq, index, virtio_vq_done, vq);
    762  1.63.2.4    martin 	vq->vq_done = vq_done;
    763  1.63.2.4    martin }
    764  1.63.2.4    martin 
    765  1.63.2.4    martin void
    766  1.63.2.4    martin virtio_init_vq(struct virtio_softc *sc, struct virtqueue *vq, int index,
    767  1.63.2.4    martin    int (*func)(void *), void *arg)
    768  1.63.2.4    martin {
    769  1.63.2.4    martin 
    770  1.63.2.4    martin 	memset(vq, 0, sizeof(*vq));
    771  1.63.2.4    martin 
    772  1.63.2.4    martin 	vq->vq_owner = sc;
    773  1.63.2.4    martin 	vq->vq_num = sc->sc_ops->read_queue_size(sc, index);
    774  1.63.2.4    martin 	vq->vq_index = index;
    775  1.63.2.4    martin 	vq->vq_intrhand = func;
    776  1.63.2.4    martin 	vq->vq_intrhand_arg = arg;
    777  1.63.2.4    martin }
    778  1.63.2.4    martin 
    779       1.1   hannken /*
    780       1.1   hannken  * Allocate/free a vq.
    781       1.1   hannken  */
    782       1.1   hannken int
    783  1.63.2.4    martin virtio_alloc_vq(struct virtio_softc *sc, struct virtqueue *vq,
    784      1.15   msaitoh     int maxsegsize, int maxnsegs, const char *name)
    785       1.1   hannken {
    786  1.63.2.4    martin 	bus_size_t size_desc, size_avail, size_used, size_indirect;
    787  1.63.2.4    martin 	bus_size_t allocsize = 0, size_desc_avail;
    788      1.43   reinoud 	int rsegs, r, hdrlen;
    789  1.63.2.4    martin 	unsigned int vq_num;
    790      1.61     skrll #define VIRTQUEUE_ALIGN(n)	roundup(n, VIRTIO_PAGE_SIZE)
    791       1.1   hannken 
    792  1.63.2.4    martin 	vq_num = vq->vq_num;
    793       1.1   hannken 
    794  1.63.2.4    martin 	if (vq_num == 0) {
    795       1.1   hannken 		aprint_error_dev(sc->sc_dev,
    796      1.59  riastrad 		    "virtqueue not exist, index %d for %s\n",
    797  1.63.2.4    martin 		    vq->vq_index, name);
    798       1.1   hannken 		goto err;
    799       1.1   hannken 	}
    800      1.43   reinoud 
    801      1.43   reinoud 	hdrlen = sc->sc_active_features & VIRTIO_F_RING_EVENT_IDX ? 3 : 2;
    802      1.43   reinoud 
    803  1.63.2.4    martin 	size_desc = sizeof(vq->vq_desc[0]) * vq_num;
    804  1.63.2.4    martin 	size_avail = sizeof(uint16_t) * hdrlen
    805  1.63.2.4    martin 	    + sizeof(vq->vq_avail[0].ring[0]) * vq_num;
    806  1.63.2.4    martin 	size_used = sizeof(uint16_t) *hdrlen
    807  1.63.2.4    martin 	    + sizeof(vq->vq_used[0].ring[0]) * vq_num;
    808  1.63.2.4    martin 	size_indirect = (sc->sc_indirect && maxnsegs >= MINSEG_INDIRECT) ?
    809  1.63.2.4    martin 	    sizeof(struct vring_desc) * maxnsegs * vq_num : 0;
    810  1.63.2.4    martin 
    811  1.63.2.4    martin 	size_desc_avail = VIRTQUEUE_ALIGN(size_desc + size_avail);
    812  1.63.2.4    martin 	size_used = VIRTQUEUE_ALIGN(size_used);
    813  1.63.2.4    martin 
    814  1.63.2.4    martin 	allocsize = size_desc_avail + size_used + size_indirect;
    815       1.1   hannken 
    816       1.1   hannken 	/* alloc and map the memory */
    817       1.1   hannken 	r = bus_dmamem_alloc(sc->sc_dmat, allocsize, VIRTIO_PAGE_SIZE, 0,
    818      1.59  riastrad 	    &vq->vq_segs[0], 1, &rsegs, BUS_DMA_WAITOK);
    819       1.1   hannken 	if (r != 0) {
    820       1.1   hannken 		aprint_error_dev(sc->sc_dev,
    821      1.59  riastrad 		    "virtqueue %d for %s allocation failed, "
    822  1.63.2.4    martin 		    "error code %d\n", vq->vq_index, name, r);
    823       1.1   hannken 		goto err;
    824       1.1   hannken 	}
    825  1.63.2.4    martin 
    826      1.43   reinoud 	r = bus_dmamem_map(sc->sc_dmat, &vq->vq_segs[0], rsegs, allocsize,
    827      1.59  riastrad 	    &vq->vq_vaddr, BUS_DMA_WAITOK);
    828       1.1   hannken 	if (r != 0) {
    829       1.1   hannken 		aprint_error_dev(sc->sc_dev,
    830      1.59  riastrad 		    "virtqueue %d for %s map failed, "
    831  1.63.2.4    martin 		    "error code %d\n", vq->vq_index, name, r);
    832       1.1   hannken 		goto err;
    833       1.1   hannken 	}
    834  1.63.2.4    martin 
    835       1.1   hannken 	r = bus_dmamap_create(sc->sc_dmat, allocsize, 1, allocsize, 0,
    836      1.59  riastrad 	    BUS_DMA_WAITOK, &vq->vq_dmamap);
    837       1.1   hannken 	if (r != 0) {
    838       1.1   hannken 		aprint_error_dev(sc->sc_dev,
    839      1.59  riastrad 		    "virtqueue %d for %s dmamap creation failed, "
    840  1.63.2.4    martin 		    "error code %d\n", vq->vq_index, name, r);
    841       1.1   hannken 		goto err;
    842       1.1   hannken 	}
    843  1.63.2.4    martin 
    844       1.1   hannken 	r = bus_dmamap_load(sc->sc_dmat, vq->vq_dmamap,
    845      1.59  riastrad 	    vq->vq_vaddr, allocsize, NULL, BUS_DMA_WAITOK);
    846       1.1   hannken 	if (r != 0) {
    847       1.1   hannken 		aprint_error_dev(sc->sc_dev,
    848      1.59  riastrad 		    "virtqueue %d for %s dmamap load failed, "
    849  1.63.2.4    martin 		    "error code %d\n", vq->vq_index, name, r);
    850       1.1   hannken 		goto err;
    851       1.1   hannken 	}
    852       1.1   hannken 
    853       1.1   hannken 	vq->vq_bytesize = allocsize;
    854       1.1   hannken 	vq->vq_maxsegsize = maxsegsize;
    855       1.1   hannken 	vq->vq_maxnsegs = maxnsegs;
    856       1.1   hannken 
    857  1.63.2.4    martin #define VIRTIO_PTR(base, offset)	(void *)((intptr_t)(base) + (offset))
    858  1.63.2.4    martin 	/* initialize vring pointers */
    859  1.63.2.4    martin 	vq->vq_desc = VIRTIO_PTR(vq->vq_vaddr, 0);
    860  1.63.2.4    martin 	vq->vq_availoffset = size_desc;
    861  1.63.2.4    martin 	vq->vq_avail = VIRTIO_PTR(vq->vq_vaddr, vq->vq_availoffset);
    862  1.63.2.4    martin 	vq->vq_used_event = VIRTIO_PTR(vq->vq_avail,
    863  1.63.2.4    martin 	    offsetof(struct vring_avail, ring[vq_num]));
    864  1.63.2.4    martin 	vq->vq_usedoffset = size_desc_avail;
    865  1.63.2.4    martin 	vq->vq_used = VIRTIO_PTR(vq->vq_vaddr, vq->vq_usedoffset);
    866  1.63.2.4    martin 	vq->vq_avail_event = VIRTIO_PTR(vq->vq_used,
    867  1.63.2.4    martin 	    offsetof(struct vring_used, ring[vq_num]));
    868  1.63.2.4    martin 
    869  1.63.2.4    martin 	if (size_indirect > 0) {
    870  1.63.2.4    martin 		vq->vq_indirectoffset = size_desc_avail + size_used;
    871  1.63.2.4    martin 		vq->vq_indirect = VIRTIO_PTR(vq->vq_vaddr,
    872  1.63.2.4    martin 		    vq->vq_indirectoffset);
    873  1.63.2.4    martin 	}
    874  1.63.2.4    martin #undef VIRTIO_PTR
    875  1.63.2.4    martin 
    876  1.63.2.4    martin 	vq->vq_descx = kmem_zalloc(sizeof(vq->vq_descx[0]) * vq_num,
    877      1.59  riastrad 	    KM_SLEEP);
    878       1.1   hannken 
    879  1.63.2.4    martin 	mutex_init(&vq->vq_freedesc_lock, MUTEX_SPIN, sc->sc_ipl);
    880  1.63.2.4    martin 	mutex_init(&vq->vq_aring_lock, MUTEX_SPIN, sc->sc_ipl);
    881  1.63.2.4    martin 	mutex_init(&vq->vq_uring_lock, MUTEX_SPIN, sc->sc_ipl);
    882  1.63.2.4    martin 
    883  1.63.2.4    martin 	virtio_reset_vq(sc, vq);
    884      1.43   reinoud 
    885       1.1   hannken 	aprint_verbose_dev(sc->sc_dev,
    886  1.63.2.4    martin 	    "allocated %" PRIuBUSSIZE " byte for virtqueue %d for %s, "
    887  1.63.2.4    martin 	    "size %d\n", allocsize, vq->vq_index, name, vq_num);
    888  1.63.2.4    martin 	if (size_indirect > 0)
    889       1.1   hannken 		aprint_verbose_dev(sc->sc_dev,
    890  1.63.2.4    martin 		    "using %" PRIuBUSSIZE " byte (%d entries) indirect "
    891  1.63.2.4    martin 		    "descriptors\n", size_indirect, maxnsegs * vq_num);
    892      1.22  jdolecek 
    893       1.1   hannken 	return 0;
    894       1.1   hannken 
    895       1.1   hannken err:
    896  1.63.2.4    martin 	sc->sc_ops->setup_queue(sc, vq->vq_index, 0);
    897       1.1   hannken 	if (vq->vq_dmamap)
    898       1.1   hannken 		bus_dmamap_destroy(sc->sc_dmat, vq->vq_dmamap);
    899       1.1   hannken 	if (vq->vq_vaddr)
    900       1.1   hannken 		bus_dmamem_unmap(sc->sc_dmat, vq->vq_vaddr, allocsize);
    901       1.1   hannken 	if (vq->vq_segs[0].ds_addr)
    902       1.1   hannken 		bus_dmamem_free(sc->sc_dmat, &vq->vq_segs[0], 1);
    903       1.1   hannken 	memset(vq, 0, sizeof(*vq));
    904       1.1   hannken 
    905       1.1   hannken 	return -1;
    906       1.1   hannken }
    907       1.1   hannken 
    908       1.1   hannken int
    909       1.1   hannken virtio_free_vq(struct virtio_softc *sc, struct virtqueue *vq)
    910       1.1   hannken {
    911  1.63.2.4    martin 	uint16_t s;
    912  1.63.2.4    martin 	size_t i;
    913  1.63.2.4    martin 
    914  1.63.2.4    martin 	if (vq->vq_vaddr == NULL)
    915  1.63.2.4    martin 		return 0;
    916       1.1   hannken 
    917       1.1   hannken 	/* device must be already deactivated */
    918       1.1   hannken 	/* confirm the vq is empty */
    919  1.63.2.4    martin 	s = vq->vq_free_idx;
    920  1.63.2.4    martin 	i = 0;
    921  1.63.2.4    martin 	while (s != virtio_rw16(sc, VRING_DESC_CHAIN_END)) {
    922  1.63.2.4    martin 		s = vq->vq_desc[s].next;
    923       1.1   hannken 		i++;
    924       1.1   hannken 	}
    925       1.1   hannken 	if (i != vq->vq_num) {
    926       1.1   hannken 		printf("%s: freeing non-empty vq, index %d\n",
    927      1.59  riastrad 		    device_xname(sc->sc_dev), vq->vq_index);
    928       1.1   hannken 		return EBUSY;
    929       1.1   hannken 	}
    930       1.1   hannken 
    931       1.1   hannken 	/* tell device that there's no virtqueue any longer */
    932      1.31  jakllsch 	sc->sc_ops->setup_queue(sc, vq->vq_index, 0);
    933       1.1   hannken 
    934      1.57  riastrad 	vq_sync_aring_all(sc, vq, BUS_DMASYNC_POSTWRITE);
    935      1.57  riastrad 
    936  1.63.2.4    martin 	kmem_free(vq->vq_descx, sizeof(vq->vq_descx[0]) * vq->vq_num);
    937       1.1   hannken 	bus_dmamap_unload(sc->sc_dmat, vq->vq_dmamap);
    938       1.1   hannken 	bus_dmamap_destroy(sc->sc_dmat, vq->vq_dmamap);
    939       1.1   hannken 	bus_dmamem_unmap(sc->sc_dmat, vq->vq_vaddr, vq->vq_bytesize);
    940       1.1   hannken 	bus_dmamem_free(sc->sc_dmat, &vq->vq_segs[0], 1);
    941  1.63.2.4    martin 	mutex_destroy(&vq->vq_freedesc_lock);
    942       1.1   hannken 	mutex_destroy(&vq->vq_uring_lock);
    943       1.1   hannken 	mutex_destroy(&vq->vq_aring_lock);
    944       1.1   hannken 	memset(vq, 0, sizeof(*vq));
    945       1.1   hannken 
    946       1.1   hannken 	return 0;
    947       1.1   hannken }
    948       1.1   hannken 
    949       1.1   hannken /*
    950       1.1   hannken  * Free descriptor management.
    951       1.1   hannken  */
    952  1.63.2.4    martin static int
    953  1.63.2.4    martin vq_alloc_slot_locked(struct virtio_softc *sc, struct virtqueue *vq,
    954  1.63.2.4    martin     size_t nslots)
    955       1.1   hannken {
    956  1.63.2.4    martin 	struct vring_desc *vd;
    957  1.63.2.5    martin 	uint16_t head, tail;
    958  1.63.2.4    martin 	size_t i;
    959  1.63.2.4    martin 
    960  1.63.2.4    martin 	KASSERT(mutex_owned(&vq->vq_freedesc_lock));
    961  1.63.2.4    martin 
    962  1.63.2.5    martin 	head = tail = virtio_rw16(sc, vq->vq_free_idx);
    963  1.63.2.4    martin 	for (i = 0; i < nslots - 1; i++) {
    964  1.63.2.4    martin 		if (tail == VRING_DESC_CHAIN_END)
    965  1.63.2.4    martin 			return VRING_DESC_CHAIN_END;
    966       1.1   hannken 
    967  1.63.2.4    martin 		vd = &vq->vq_desc[tail];
    968  1.63.2.4    martin 		vd->flags = virtio_rw16(sc, VRING_DESC_F_NEXT);
    969  1.63.2.4    martin 		tail = virtio_rw16(sc, vd->next);
    970       1.1   hannken 	}
    971       1.1   hannken 
    972  1.63.2.4    martin 	if (tail == VRING_DESC_CHAIN_END)
    973  1.63.2.4    martin 		return VRING_DESC_CHAIN_END;
    974  1.63.2.4    martin 
    975  1.63.2.4    martin 	vd = &vq->vq_desc[tail];
    976  1.63.2.4    martin 	vd->flags = virtio_rw16(sc, 0);
    977  1.63.2.4    martin 	vq->vq_free_idx = vd->next;
    978  1.63.2.4    martin 
    979  1.63.2.5    martin 	return head;
    980  1.63.2.4    martin }
    981  1.63.2.4    martin static uint16_t
    982  1.63.2.4    martin vq_alloc_slot(struct virtio_softc *sc, struct virtqueue *vq, size_t nslots)
    983  1.63.2.4    martin {
    984  1.63.2.4    martin 	uint16_t rv;
    985  1.63.2.4    martin 
    986  1.63.2.4    martin 	mutex_enter(&vq->vq_freedesc_lock);
    987  1.63.2.4    martin 	rv = vq_alloc_slot_locked(sc, vq, nslots);
    988  1.63.2.4    martin 	mutex_exit(&vq->vq_freedesc_lock);
    989  1.63.2.4    martin 
    990  1.63.2.4    martin 	return rv;
    991       1.1   hannken }
    992       1.1   hannken 
    993       1.1   hannken static void
    994  1.63.2.4    martin vq_free_slot(struct virtio_softc *sc, struct virtqueue *vq, uint16_t slot)
    995       1.1   hannken {
    996  1.63.2.4    martin 	struct vring_desc *vd;
    997  1.63.2.4    martin 	uint16_t s;
    998       1.1   hannken 
    999  1.63.2.4    martin 	mutex_enter(&vq->vq_freedesc_lock);
   1000  1.63.2.4    martin 	vd = &vq->vq_desc[slot];
   1001  1.63.2.4    martin 	while ((vd->flags & virtio_rw16(sc, VRING_DESC_F_NEXT)) != 0) {
   1002  1.63.2.4    martin 		s = virtio_rw16(sc, vd->next);
   1003  1.63.2.4    martin 		vd = &vq->vq_desc[s];
   1004  1.63.2.4    martin 	}
   1005  1.63.2.4    martin 	vd->next = vq->vq_free_idx;
   1006  1.63.2.4    martin 	vq->vq_free_idx = virtio_rw16(sc, slot);
   1007  1.63.2.4    martin 	mutex_exit(&vq->vq_freedesc_lock);
   1008       1.1   hannken }
   1009       1.1   hannken 
   1010       1.1   hannken /*
   1011       1.1   hannken  * Enqueue several dmamaps as a single request.
   1012       1.1   hannken  */
   1013       1.1   hannken /*
   1014       1.1   hannken  * Typical usage:
   1015       1.1   hannken  *  <queue size> number of followings are stored in arrays
   1016       1.1   hannken  *  - command blocks (in dmamem) should be pre-allocated and mapped
   1017       1.1   hannken  *  - dmamaps for command blocks should be pre-allocated and loaded
   1018       1.1   hannken  *  - dmamaps for payload should be pre-allocated
   1019       1.1   hannken  *      r = virtio_enqueue_prep(sc, vq, &slot);		// allocate a slot
   1020       1.1   hannken  *	if (r)		// currently 0 or EAGAIN
   1021      1.59  riastrad  *		return r;
   1022       1.1   hannken  *	r = bus_dmamap_load(dmat, dmamap_payload[slot], data, count, ..);
   1023       1.1   hannken  *	if (r) {
   1024      1.59  riastrad  *		virtio_enqueue_abort(sc, vq, slot);
   1025      1.59  riastrad  *		return r;
   1026       1.1   hannken  *	}
   1027      1.48     skrll  *	r = virtio_enqueue_reserve(sc, vq, slot,
   1028      1.59  riastrad  *	    dmamap_payload[slot]->dm_nsegs + 1);
   1029       1.1   hannken  *							// ^ +1 for command
   1030       1.1   hannken  *	if (r) {	// currently 0 or EAGAIN
   1031      1.59  riastrad  *		bus_dmamap_unload(dmat, dmamap_payload[slot]);
   1032      1.59  riastrad  *		return r;				// do not call abort()
   1033       1.1   hannken  *	}
   1034       1.1   hannken  *	<setup and prepare commands>
   1035       1.1   hannken  *	bus_dmamap_sync(dmat, dmamap_cmd[slot],... BUS_DMASYNC_PREWRITE);
   1036       1.1   hannken  *	bus_dmamap_sync(dmat, dmamap_payload[slot],...);
   1037       1.1   hannken  *	virtio_enqueue(sc, vq, slot, dmamap_cmd[slot], false);
   1038       1.1   hannken  *	virtio_enqueue(sc, vq, slot, dmamap_payload[slot], iswrite);
   1039       1.1   hannken  *	virtio_enqueue_commit(sc, vq, slot, true);
   1040       1.1   hannken  */
   1041       1.1   hannken 
   1042       1.1   hannken /*
   1043       1.1   hannken  * enqueue_prep: allocate a slot number
   1044       1.1   hannken  */
   1045       1.1   hannken int
   1046       1.1   hannken virtio_enqueue_prep(struct virtio_softc *sc, struct virtqueue *vq, int *slotp)
   1047       1.1   hannken {
   1048  1.63.2.4    martin 	uint16_t slot;
   1049       1.1   hannken 
   1050  1.63.2.6    martin 	KASSERT(sc->sc_child_state == VIRTIO_CHILD_ATTACH_FINISHED);
   1051       1.1   hannken 	KASSERT(slotp != NULL);
   1052       1.1   hannken 
   1053  1.63.2.4    martin 	slot = vq_alloc_slot(sc, vq, 1);
   1054  1.63.2.4    martin 	if (slot == VRING_DESC_CHAIN_END)
   1055       1.1   hannken 		return EAGAIN;
   1056  1.63.2.4    martin 
   1057  1.63.2.4    martin 	*slotp = slot;
   1058       1.1   hannken 
   1059       1.1   hannken 	return 0;
   1060       1.1   hannken }
   1061       1.1   hannken 
   1062       1.1   hannken /*
   1063       1.1   hannken  * enqueue_reserve: allocate remaining slots and build the descriptor chain.
   1064       1.1   hannken  */
   1065       1.1   hannken int
   1066       1.1   hannken virtio_enqueue_reserve(struct virtio_softc *sc, struct virtqueue *vq,
   1067      1.59  riastrad     int slot, int nsegs)
   1068       1.1   hannken {
   1069  1.63.2.4    martin 	struct vring_desc *vd;
   1070  1.63.2.4    martin 	struct vring_desc_extra *vdx;
   1071  1.63.2.4    martin 	int i;
   1072       1.1   hannken 
   1073  1.63.2.6    martin 	KASSERT(1 <= nsegs);
   1074  1.63.2.6    martin 	KASSERT(nsegs <= vq->vq_num);
   1075       1.1   hannken 
   1076  1.63.2.4    martin 	vdx = &vq->vq_descx[slot];
   1077  1.63.2.4    martin 	vd = &vq->vq_desc[slot];
   1078  1.63.2.4    martin 
   1079  1.63.2.4    martin 	KASSERT((vd->flags & virtio_rw16(sc, VRING_DESC_F_NEXT)) == 0);
   1080  1.63.2.4    martin 
   1081       1.1   hannken 	if ((vq->vq_indirect != NULL) &&
   1082       1.1   hannken 	    (nsegs >= MINSEG_INDIRECT) &&
   1083       1.1   hannken 	    (nsegs <= vq->vq_maxnsegs))
   1084  1.63.2.4    martin 		vdx->use_indirect = true;
   1085       1.1   hannken 	else
   1086  1.63.2.4    martin 		vdx->use_indirect = false;
   1087       1.1   hannken 
   1088  1.63.2.4    martin 	if (vdx->use_indirect) {
   1089      1.43   reinoud 		uint64_t addr;
   1090       1.1   hannken 
   1091      1.43   reinoud 		addr = vq->vq_dmamap->dm_segs[0].ds_addr
   1092      1.59  riastrad 		    + vq->vq_indirectoffset;
   1093      1.43   reinoud 		addr += sizeof(struct vring_desc)
   1094  1.63.2.4    martin 		    * vq->vq_maxnsegs * slot;
   1095  1.63.2.4    martin 
   1096      1.43   reinoud 		vd->addr  = virtio_rw64(sc, addr);
   1097      1.43   reinoud 		vd->len   = virtio_rw32(sc, sizeof(struct vring_desc) * nsegs);
   1098      1.43   reinoud 		vd->flags = virtio_rw16(sc, VRING_DESC_F_INDIRECT);
   1099       1.1   hannken 
   1100  1.63.2.4    martin 		vd = &vq->vq_indirect[vq->vq_maxnsegs * slot];
   1101  1.63.2.4    martin 		vdx->desc_base = vd;
   1102  1.63.2.4    martin 		vdx->desc_free_idx = 0;
   1103       1.1   hannken 
   1104      1.59  riastrad 		for (i = 0; i < nsegs - 1; i++) {
   1105      1.43   reinoud 			vd[i].flags = virtio_rw16(sc, VRING_DESC_F_NEXT);
   1106       1.1   hannken 		}
   1107      1.43   reinoud 		vd[i].flags  = virtio_rw16(sc, 0);
   1108       1.1   hannken 	} else {
   1109  1.63.2.5    martin 		if (nsegs > 1) {
   1110  1.63.2.5    martin 			uint16_t s;
   1111       1.1   hannken 
   1112  1.63.2.5    martin 			s = vq_alloc_slot(sc, vq, nsegs - 1);
   1113  1.63.2.5    martin 			if (s == VRING_DESC_CHAIN_END) {
   1114  1.63.2.5    martin 				vq_free_slot(sc, vq, slot);
   1115  1.63.2.5    martin 				return EAGAIN;
   1116  1.63.2.5    martin 			}
   1117  1.63.2.5    martin 			vd->next = virtio_rw16(sc, s);
   1118  1.63.2.5    martin 			vd->flags = virtio_rw16(sc, VRING_DESC_F_NEXT);
   1119       1.1   hannken 		}
   1120       1.1   hannken 
   1121  1.63.2.4    martin 		vdx->desc_base = &vq->vq_desc[0];
   1122  1.63.2.4    martin 		vdx->desc_free_idx = slot;
   1123       1.1   hannken 	}
   1124  1.63.2.4    martin 
   1125  1.63.2.4    martin 	return 0;
   1126       1.1   hannken }
   1127       1.1   hannken 
   1128       1.1   hannken /*
   1129       1.1   hannken  * enqueue: enqueue a single dmamap.
   1130       1.1   hannken  */
   1131       1.1   hannken int
   1132       1.1   hannken virtio_enqueue(struct virtio_softc *sc, struct virtqueue *vq, int slot,
   1133      1.59  riastrad     bus_dmamap_t dmamap, bool write)
   1134       1.1   hannken {
   1135  1.63.2.4    martin 	struct vring_desc *vds;
   1136  1.63.2.4    martin 	struct vring_desc_extra *vdx;
   1137  1.63.2.4    martin 	uint16_t s;
   1138       1.1   hannken 	int i;
   1139       1.1   hannken 
   1140       1.1   hannken 	KASSERT(dmamap->dm_nsegs > 0);
   1141       1.1   hannken 
   1142  1.63.2.4    martin 	vdx = &vq->vq_descx[slot];
   1143  1.63.2.4    martin 	vds = vdx->desc_base;
   1144  1.63.2.4    martin 	s = vdx->desc_free_idx;
   1145  1.63.2.4    martin 
   1146  1.63.2.4    martin 	KASSERT(vds != NULL);
   1147  1.63.2.4    martin 
   1148       1.1   hannken 	for (i = 0; i < dmamap->dm_nsegs; i++) {
   1149  1.63.2.4    martin 		KASSERT(s != VRING_DESC_CHAIN_END);
   1150  1.63.2.4    martin 
   1151  1.63.2.4    martin 		vds[s].addr = virtio_rw64(sc, dmamap->dm_segs[i].ds_addr);
   1152  1.63.2.4    martin 		vds[s].len  = virtio_rw32(sc, dmamap->dm_segs[i].ds_len);
   1153       1.1   hannken 		if (!write)
   1154  1.63.2.4    martin 			vds[s].flags |= virtio_rw16(sc, VRING_DESC_F_WRITE);
   1155  1.63.2.4    martin 
   1156  1.63.2.4    martin 		if ((vds[s].flags & virtio_rw16(sc, VRING_DESC_F_NEXT)) == 0) {
   1157  1.63.2.4    martin 			s = VRING_DESC_CHAIN_END;
   1158  1.63.2.4    martin 		} else {
   1159  1.63.2.4    martin 			s = virtio_rw16(sc, vds[s].next);
   1160  1.63.2.4    martin 		}
   1161       1.1   hannken 	}
   1162  1.63.2.4    martin 
   1163  1.63.2.4    martin 	vdx->desc_free_idx = s;
   1164       1.1   hannken 
   1165       1.1   hannken 	return 0;
   1166       1.1   hannken }
   1167       1.1   hannken 
   1168       1.1   hannken int
   1169       1.1   hannken virtio_enqueue_p(struct virtio_softc *sc, struct virtqueue *vq, int slot,
   1170      1.59  riastrad     bus_dmamap_t dmamap, bus_addr_t start, bus_size_t len,
   1171      1.59  riastrad     bool write)
   1172       1.1   hannken {
   1173  1.63.2.4    martin 	struct vring_desc_extra *vdx;
   1174  1.63.2.4    martin 	struct vring_desc *vds;
   1175  1.63.2.4    martin 	uint16_t s;
   1176  1.63.2.4    martin 
   1177  1.63.2.4    martin 	vdx = &vq->vq_descx[slot];
   1178  1.63.2.4    martin 	vds = vdx->desc_base;
   1179  1.63.2.4    martin 	s = vdx->desc_free_idx;
   1180       1.1   hannken 
   1181  1.63.2.4    martin 	KASSERT(s != VRING_DESC_CHAIN_END);
   1182  1.63.2.4    martin 	KASSERT(vds != NULL);
   1183       1.1   hannken 	KASSERT(dmamap->dm_nsegs == 1); /* XXX */
   1184      1.59  riastrad 	KASSERT(dmamap->dm_segs[0].ds_len > start);
   1185      1.59  riastrad 	KASSERT(dmamap->dm_segs[0].ds_len >= start + len);
   1186       1.1   hannken 
   1187  1.63.2.4    martin 	vds[s].addr = virtio_rw64(sc, dmamap->dm_segs[0].ds_addr + start);
   1188  1.63.2.4    martin 	vds[s].len  = virtio_rw32(sc, len);
   1189       1.1   hannken 	if (!write)
   1190  1.63.2.4    martin 		vds[s].flags |= virtio_rw16(sc, VRING_DESC_F_WRITE);
   1191  1.63.2.4    martin 
   1192  1.63.2.4    martin 	if ((vds[s].flags & virtio_rw16(sc, VRING_DESC_F_NEXT)) == 0) {
   1193  1.63.2.4    martin 		s = VRING_DESC_CHAIN_END;
   1194  1.63.2.4    martin 	} else {
   1195  1.63.2.4    martin 		s = virtio_rw16(sc, vds[s].next);
   1196  1.63.2.4    martin 	}
   1197  1.63.2.4    martin 
   1198  1.63.2.4    martin 	vdx->desc_free_idx = s;
   1199       1.1   hannken 
   1200       1.1   hannken 	return 0;
   1201       1.1   hannken }
   1202       1.1   hannken 
   1203       1.1   hannken /*
   1204       1.1   hannken  * enqueue_commit: add it to the aring.
   1205       1.1   hannken  */
   1206       1.1   hannken int
   1207       1.1   hannken virtio_enqueue_commit(struct virtio_softc *sc, struct virtqueue *vq, int slot,
   1208      1.59  riastrad     bool notifynow)
   1209       1.1   hannken {
   1210       1.1   hannken 
   1211       1.1   hannken 	if (slot < 0) {
   1212       1.1   hannken 		mutex_enter(&vq->vq_aring_lock);
   1213       1.1   hannken 		goto notify;
   1214       1.1   hannken 	}
   1215  1.63.2.4    martin 
   1216       1.1   hannken 	vq_sync_descs(sc, vq, BUS_DMASYNC_PREWRITE);
   1217  1.63.2.4    martin 	if (vq->vq_descx[slot].use_indirect)
   1218       1.1   hannken 		vq_sync_indirect(sc, vq, slot, BUS_DMASYNC_PREWRITE);
   1219  1.63.2.4    martin 
   1220       1.1   hannken 	mutex_enter(&vq->vq_aring_lock);
   1221      1.43   reinoud 	vq->vq_avail->ring[(vq->vq_avail_idx++) % vq->vq_num] =
   1222      1.57  riastrad 	    virtio_rw16(sc, slot);
   1223       1.1   hannken 
   1224       1.1   hannken notify:
   1225       1.1   hannken 	if (notifynow) {
   1226      1.43   reinoud 		uint16_t o, n, t;
   1227      1.43   reinoud 		uint16_t flags;
   1228      1.57  riastrad 
   1229  1.63.2.1    martin 		o = virtio_rw16(sc, vq->vq_avail->idx) - 1;
   1230      1.43   reinoud 		n = vq->vq_avail_idx;
   1231      1.43   reinoud 
   1232      1.57  riastrad 		/*
   1233      1.57  riastrad 		 * Prepare for `device->CPU' (host->guest) transfer
   1234      1.57  riastrad 		 * into the buffer.  This must happen before we commit
   1235      1.57  riastrad 		 * the vq->vq_avail->idx update to ensure we're not
   1236      1.57  riastrad 		 * still using the buffer in case program-prior loads
   1237      1.57  riastrad 		 * or stores in it get delayed past the store to
   1238      1.57  riastrad 		 * vq->vq_avail->idx.
   1239      1.57  riastrad 		 */
   1240      1.57  riastrad 		vq_sync_uring_all(sc, vq, BUS_DMASYNC_PREREAD);
   1241      1.57  riastrad 
   1242      1.57  riastrad 		/* ensure payload is published, then avail idx */
   1243      1.57  riastrad 		vq_sync_aring_payload(sc, vq, BUS_DMASYNC_PREWRITE);
   1244      1.43   reinoud 		vq->vq_avail->idx = virtio_rw16(sc, vq->vq_avail_idx);
   1245      1.57  riastrad 		vq_sync_aring_header(sc, vq, BUS_DMASYNC_PREWRITE);
   1246       1.1   hannken 		vq->vq_queued++;
   1247      1.43   reinoud 
   1248      1.43   reinoud 		if (sc->sc_active_features & VIRTIO_F_RING_EVENT_IDX) {
   1249      1.57  riastrad 			vq_sync_uring_avail(sc, vq, BUS_DMASYNC_POSTREAD);
   1250      1.43   reinoud 			t = virtio_rw16(sc, *vq->vq_avail_event) + 1;
   1251      1.43   reinoud 			if ((uint16_t) (n - t) < (uint16_t) (n - o))
   1252      1.43   reinoud 				sc->sc_ops->kick(sc, vq->vq_index);
   1253      1.43   reinoud 		} else {
   1254      1.57  riastrad 			vq_sync_uring_header(sc, vq, BUS_DMASYNC_POSTREAD);
   1255      1.43   reinoud 			flags = virtio_rw16(sc, vq->vq_used->flags);
   1256      1.43   reinoud 			if (!(flags & VRING_USED_F_NO_NOTIFY))
   1257      1.43   reinoud 				sc->sc_ops->kick(sc, vq->vq_index);
   1258      1.43   reinoud 		}
   1259       1.1   hannken 	}
   1260       1.1   hannken 	mutex_exit(&vq->vq_aring_lock);
   1261       1.1   hannken 
   1262       1.1   hannken 	return 0;
   1263       1.1   hannken }
   1264       1.1   hannken 
   1265       1.1   hannken /*
   1266       1.1   hannken  * enqueue_abort: rollback.
   1267       1.1   hannken  */
   1268       1.1   hannken int
   1269       1.1   hannken virtio_enqueue_abort(struct virtio_softc *sc, struct virtqueue *vq, int slot)
   1270       1.1   hannken {
   1271  1.63.2.4    martin 	struct vring_desc_extra *vdx;
   1272       1.1   hannken 
   1273  1.63.2.4    martin 	vdx = &vq->vq_descx[slot];
   1274  1.63.2.4    martin 	vdx->desc_free_idx = VRING_DESC_CHAIN_END;
   1275  1.63.2.4    martin 	vdx->desc_base = NULL;
   1276       1.1   hannken 
   1277  1.63.2.5    martin 	vq_free_slot(sc, vq, slot);
   1278  1.63.2.5    martin 
   1279       1.1   hannken 	return 0;
   1280       1.1   hannken }
   1281       1.1   hannken 
   1282       1.1   hannken /*
   1283       1.1   hannken  * Dequeue a request.
   1284       1.1   hannken  */
   1285       1.1   hannken /*
   1286       1.1   hannken  * dequeue: dequeue a request from uring; dmamap_sync for uring is
   1287       1.1   hannken  *	    already done in the interrupt handler.
   1288       1.1   hannken  */
   1289       1.1   hannken int
   1290       1.1   hannken virtio_dequeue(struct virtio_softc *sc, struct virtqueue *vq,
   1291      1.59  riastrad     int *slotp, int *lenp)
   1292       1.1   hannken {
   1293       1.1   hannken 	uint16_t slot, usedidx;
   1294       1.1   hannken 
   1295      1.43   reinoud 	if (vq->vq_used_idx == virtio_rw16(sc, vq->vq_used->idx))
   1296       1.1   hannken 		return ENOENT;
   1297       1.1   hannken 	mutex_enter(&vq->vq_uring_lock);
   1298       1.1   hannken 	usedidx = vq->vq_used_idx++;
   1299       1.1   hannken 	mutex_exit(&vq->vq_uring_lock);
   1300       1.1   hannken 	usedidx %= vq->vq_num;
   1301      1.43   reinoud 	slot = virtio_rw32(sc, vq->vq_used->ring[usedidx].id);
   1302       1.1   hannken 
   1303  1.63.2.4    martin 	if (vq->vq_descx[slot].use_indirect)
   1304       1.1   hannken 		vq_sync_indirect(sc, vq, slot, BUS_DMASYNC_POSTWRITE);
   1305       1.1   hannken 
   1306       1.1   hannken 	if (slotp)
   1307       1.1   hannken 		*slotp = slot;
   1308       1.1   hannken 	if (lenp)
   1309      1.43   reinoud 		*lenp = virtio_rw32(sc, vq->vq_used->ring[usedidx].len);
   1310       1.1   hannken 
   1311       1.1   hannken 	return 0;
   1312       1.1   hannken }
   1313       1.1   hannken 
   1314       1.1   hannken /*
   1315       1.1   hannken  * dequeue_commit: complete dequeue; the slot is recycled for future use.
   1316       1.1   hannken  *                 if you forget to call this the slot will be leaked.
   1317       1.1   hannken  */
   1318       1.1   hannken int
   1319       1.1   hannken virtio_dequeue_commit(struct virtio_softc *sc, struct virtqueue *vq, int slot)
   1320       1.1   hannken {
   1321  1.63.2.4    martin 	struct vring_desc_extra *vdx;
   1322  1.63.2.4    martin 
   1323  1.63.2.4    martin 	vdx = &vq->vq_descx[slot];
   1324  1.63.2.4    martin 	vdx->desc_base = NULL;
   1325  1.63.2.4    martin 	vdx->desc_free_idx = VRING_DESC_CHAIN_END;
   1326       1.1   hannken 
   1327  1.63.2.5    martin 	vq_free_slot(sc, vq, slot);
   1328  1.63.2.5    martin 
   1329       1.1   hannken 	return 0;
   1330       1.1   hannken }
   1331      1.18  pgoyette 
   1332      1.22  jdolecek /*
   1333      1.22  jdolecek  * Attach a child, fill all the members.
   1334      1.22  jdolecek  */
   1335      1.22  jdolecek void
   1336      1.48     skrll virtio_child_attach_start(struct virtio_softc *sc, device_t child, int ipl,
   1337  1.63.2.4    martin     uint64_t req_features, const char *feat_bits)
   1338      1.22  jdolecek {
   1339      1.43   reinoud 	char buf[1024];
   1340      1.22  jdolecek 
   1341  1.63.2.4    martin 	KASSERT(sc->sc_child == NULL);
   1342  1.63.2.5    martin 	KASSERT(sc->sc_child_state == VIRTIO_NO_CHILD);
   1343  1.63.2.4    martin 
   1344      1.22  jdolecek 	sc->sc_child = child;
   1345      1.22  jdolecek 	sc->sc_ipl = ipl;
   1346      1.22  jdolecek 
   1347      1.43   reinoud 	virtio_negotiate_features(sc, req_features);
   1348      1.43   reinoud 	snprintb(buf, sizeof(buf), feat_bits, sc->sc_active_features);
   1349      1.43   reinoud 	aprint_normal(": features: %s\n", buf);
   1350      1.22  jdolecek 	aprint_naive("\n");
   1351      1.22  jdolecek }
   1352      1.22  jdolecek 
   1353  1.63.2.4    martin int
   1354  1.63.2.4    martin virtio_child_attach_finish(struct virtio_softc *sc,
   1355  1.63.2.4    martin     struct virtqueue *vqs, size_t nvqs,
   1356  1.63.2.4    martin     virtio_callback config_change,
   1357  1.63.2.4    martin     int req_flags)
   1358      1.37  yamaguch {
   1359  1.63.2.4    martin 	size_t i;
   1360  1.63.2.4    martin 	int r;
   1361  1.63.2.4    martin 
   1362  1.63.2.4    martin #ifdef DIAGNOSTIC
   1363  1.63.2.4    martin 	KASSERT(nvqs > 0);
   1364  1.63.2.4    martin #define VIRTIO_ASSERT_FLAGS	(VIRTIO_F_INTR_SOFTINT | VIRTIO_F_INTR_PERVQ)
   1365  1.63.2.4    martin 	KASSERT((req_flags & VIRTIO_ASSERT_FLAGS) != VIRTIO_ASSERT_FLAGS);
   1366  1.63.2.4    martin #undef VIRTIO_ASSERT_FLAGS
   1367  1.63.2.4    martin 
   1368  1.63.2.4    martin 	for (i = 0; i < nvqs; i++){
   1369  1.63.2.4    martin 		KASSERT(vqs[i].vq_index == i);
   1370  1.63.2.4    martin 		KASSERT(vqs[i].vq_intrhand != NULL);
   1371  1.63.2.4    martin 		KASSERT(vqs[i].vq_done == NULL ||
   1372  1.63.2.4    martin 		    vqs[i].vq_intrhand == virtio_vq_done);
   1373  1.63.2.4    martin 	}
   1374  1.63.2.4    martin #endif
   1375      1.39  yamaguch 
   1376      1.37  yamaguch 
   1377      1.37  yamaguch 	sc->sc_vqs = vqs;
   1378  1.63.2.4    martin 	sc->sc_nvqs = nvqs;
   1379  1.63.2.4    martin 	sc->sc_config_change = config_change;
   1380  1.63.2.4    martin 	sc->sc_intrhand = virtio_vq_intr;
   1381  1.63.2.4    martin 	sc->sc_flags = req_flags;
   1382      1.37  yamaguch 
   1383  1.63.2.4    martin 	/* set the vq address */
   1384  1.63.2.4    martin 	for (i = 0; i < nvqs; i++) {
   1385  1.63.2.4    martin 		sc->sc_ops->setup_queue(sc, vqs[i].vq_index,
   1386  1.63.2.4    martin 		    vqs[i].vq_dmamap->dm_segs[0].ds_addr);
   1387  1.63.2.4    martin 	}
   1388      1.22  jdolecek 
   1389      1.50  yamaguch 	r = sc->sc_ops->alloc_interrupts(sc);
   1390      1.50  yamaguch 	if (r != 0) {
   1391      1.59  riastrad 		aprint_error_dev(sc->sc_dev,
   1392      1.59  riastrad 		    "failed to allocate interrupts\n");
   1393      1.50  yamaguch 		goto fail;
   1394      1.50  yamaguch 	}
   1395      1.50  yamaguch 
   1396      1.51  yamaguch 	r = sc->sc_ops->setup_interrupts(sc, 0);
   1397      1.22  jdolecek 	if (r != 0) {
   1398      1.22  jdolecek 		aprint_error_dev(sc->sc_dev, "failed to setup interrupts\n");
   1399      1.52  yamaguch 		goto fail;
   1400      1.31  jakllsch 	}
   1401      1.31  jakllsch 
   1402      1.31  jakllsch 	KASSERT(sc->sc_soft_ih == NULL);
   1403      1.43   reinoud 	if (sc->sc_flags & VIRTIO_F_INTR_SOFTINT) {
   1404      1.48     skrll 		u_int flags = SOFTINT_NET;
   1405      1.43   reinoud 		if (sc->sc_flags & VIRTIO_F_INTR_MPSAFE)
   1406      1.31  jakllsch 			flags |= SOFTINT_MPSAFE;
   1407      1.31  jakllsch 
   1408      1.59  riastrad 		sc->sc_soft_ih = softint_establish(flags, virtio_soft_intr,
   1409      1.59  riastrad 		    sc);
   1410      1.31  jakllsch 		if (sc->sc_soft_ih == NULL) {
   1411      1.31  jakllsch 			sc->sc_ops->free_interrupts(sc);
   1412      1.31  jakllsch 			aprint_error_dev(sc->sc_dev,
   1413      1.31  jakllsch 			    "failed to establish soft interrupt\n");
   1414      1.31  jakllsch 			goto fail;
   1415      1.31  jakllsch 		}
   1416      1.22  jdolecek 	}
   1417      1.22  jdolecek 
   1418  1.63.2.5    martin 	sc->sc_child_state = VIRTIO_CHILD_ATTACH_FINISHED;
   1419      1.22  jdolecek 	virtio_set_status(sc, VIRTIO_CONFIG_DEVICE_STATUS_DRIVER_OK);
   1420      1.31  jakllsch 	return 0;
   1421      1.22  jdolecek 
   1422      1.31  jakllsch fail:
   1423      1.37  yamaguch 	if (sc->sc_soft_ih) {
   1424      1.37  yamaguch 		softint_disestablish(sc->sc_soft_ih);
   1425      1.37  yamaguch 		sc->sc_soft_ih = NULL;
   1426      1.37  yamaguch 	}
   1427      1.37  yamaguch 
   1428      1.52  yamaguch 	sc->sc_ops->free_interrupts(sc);
   1429      1.52  yamaguch 
   1430      1.31  jakllsch 	virtio_set_status(sc, VIRTIO_CONFIG_DEVICE_STATUS_FAILED);
   1431      1.31  jakllsch 	return 1;
   1432      1.22  jdolecek }
   1433      1.22  jdolecek 
   1434      1.22  jdolecek void
   1435      1.22  jdolecek virtio_child_detach(struct virtio_softc *sc)
   1436      1.22  jdolecek {
   1437  1.63.2.4    martin 
   1438  1.63.2.4    martin 	/* already detached */
   1439  1.63.2.5    martin 	if (sc->sc_child == NULL)
   1440  1.63.2.4    martin 		return;
   1441  1.63.2.4    martin 
   1442      1.22  jdolecek 
   1443      1.22  jdolecek 	virtio_device_reset(sc);
   1444      1.22  jdolecek 
   1445      1.31  jakllsch 	sc->sc_ops->free_interrupts(sc);
   1446      1.31  jakllsch 
   1447      1.31  jakllsch 	if (sc->sc_soft_ih) {
   1448      1.31  jakllsch 		softint_disestablish(sc->sc_soft_ih);
   1449      1.31  jakllsch 		sc->sc_soft_ih = NULL;
   1450      1.31  jakllsch 	}
   1451  1.63.2.4    martin 
   1452  1.63.2.5    martin 	sc->sc_vqs = NULL;
   1453  1.63.2.5    martin 	sc->sc_child = NULL;
   1454      1.22  jdolecek }
   1455      1.22  jdolecek 
   1456      1.22  jdolecek void
   1457      1.22  jdolecek virtio_child_attach_failed(struct virtio_softc *sc)
   1458      1.22  jdolecek {
   1459      1.22  jdolecek 	virtio_child_detach(sc);
   1460      1.22  jdolecek 
   1461      1.22  jdolecek 	virtio_set_status(sc, VIRTIO_CONFIG_DEVICE_STATUS_FAILED);
   1462      1.22  jdolecek 
   1463  1.63.2.5    martin 	sc->sc_child_state = VIRTIO_CHILD_ATTACH_FAILED;
   1464      1.22  jdolecek }
   1465      1.22  jdolecek 
   1466      1.22  jdolecek bus_dma_tag_t
   1467      1.22  jdolecek virtio_dmat(struct virtio_softc *sc)
   1468      1.22  jdolecek {
   1469      1.22  jdolecek 	return sc->sc_dmat;
   1470      1.22  jdolecek }
   1471      1.22  jdolecek 
   1472      1.22  jdolecek device_t
   1473      1.22  jdolecek virtio_child(struct virtio_softc *sc)
   1474      1.22  jdolecek {
   1475      1.22  jdolecek 	return sc->sc_child;
   1476      1.22  jdolecek }
   1477      1.22  jdolecek 
   1478      1.22  jdolecek int
   1479      1.22  jdolecek virtio_intrhand(struct virtio_softc *sc)
   1480      1.22  jdolecek {
   1481      1.54       uwe 	return (*sc->sc_intrhand)(sc);
   1482      1.22  jdolecek }
   1483      1.22  jdolecek 
   1484      1.43   reinoud uint64_t
   1485      1.22  jdolecek virtio_features(struct virtio_softc *sc)
   1486      1.22  jdolecek {
   1487      1.43   reinoud 	return sc->sc_active_features;
   1488      1.22  jdolecek }
   1489      1.22  jdolecek 
   1490      1.35  jakllsch int
   1491      1.43   reinoud virtio_attach_failed(struct virtio_softc *sc)
   1492      1.35  jakllsch {
   1493      1.43   reinoud 	device_t self = sc->sc_dev;
   1494      1.35  jakllsch 
   1495      1.43   reinoud 	/* no error if its not connected, but its failed */
   1496      1.43   reinoud 	if (sc->sc_childdevid == 0)
   1497      1.43   reinoud 		return 1;
   1498      1.36  jmcneill 
   1499  1.63.2.4    martin 	if (sc->sc_child == NULL) {
   1500  1.63.2.5    martin 		switch (sc->sc_child_state) {
   1501  1.63.2.5    martin 		case VIRTIO_CHILD_ATTACH_FAILED:
   1502  1.63.2.5    martin 			aprint_error_dev(self,
   1503  1.63.2.5    martin 			    "virtio configuration failed\n");
   1504  1.63.2.5    martin 			break;
   1505  1.63.2.5    martin 		case VIRTIO_NO_CHILD:
   1506  1.63.2.5    martin 			aprint_error_dev(self,
   1507  1.63.2.5    martin 			    "no matching child driver; not configured\n");
   1508  1.63.2.5    martin 			break;
   1509  1.63.2.5    martin 		default:
   1510  1.63.2.5    martin 			/* sanity check */
   1511  1.63.2.5    martin 			aprint_error_dev(self,
   1512  1.63.2.5    martin 			    "virtio internal error, "
   1513  1.63.2.5    martin 			    "child driver is not configured\n");
   1514  1.63.2.5    martin 			break;
   1515  1.63.2.5    martin 		}
   1516  1.63.2.5    martin 
   1517      1.43   reinoud 		return 1;
   1518      1.43   reinoud 	}
   1519      1.44   reinoud 
   1520      1.44   reinoud 	/* sanity check */
   1521  1.63.2.5    martin 	if (sc->sc_child_state != VIRTIO_CHILD_ATTACH_FINISHED) {
   1522      1.44   reinoud 		aprint_error_dev(self, "virtio internal error, child driver "
   1523      1.59  riastrad 		    "signaled OK but didn't initialize interrupts\n");
   1524      1.44   reinoud 		return 1;
   1525      1.44   reinoud 	}
   1526      1.44   reinoud 
   1527      1.43   reinoud 	return 0;
   1528      1.43   reinoud }
   1529      1.43   reinoud 
   1530      1.43   reinoud void
   1531      1.43   reinoud virtio_print_device_type(device_t self, int id, int revision)
   1532      1.43   reinoud {
   1533      1.58  riastrad 	aprint_normal_dev(self, "%s device (id %d, rev. 0x%02x)\n",
   1534      1.58  riastrad 	    (id < NDEVNAMES ? virtio_device_name[id] : "Unknown"),
   1535      1.58  riastrad 	    id,
   1536      1.58  riastrad 	    revision);
   1537      1.35  jakllsch }
   1538      1.35  jakllsch 
   1539      1.43   reinoud 
   1540      1.32  jakllsch MODULE(MODULE_CLASS_DRIVER, virtio, NULL);
   1541      1.48     skrll 
   1542      1.18  pgoyette #ifdef _MODULE
   1543      1.18  pgoyette #include "ioconf.c"
   1544      1.18  pgoyette #endif
   1545      1.48     skrll 
   1546      1.18  pgoyette static int
   1547      1.18  pgoyette virtio_modcmd(modcmd_t cmd, void *opaque)
   1548      1.18  pgoyette {
   1549      1.18  pgoyette 	int error = 0;
   1550      1.48     skrll 
   1551      1.18  pgoyette #ifdef _MODULE
   1552      1.18  pgoyette 	switch (cmd) {
   1553      1.18  pgoyette 	case MODULE_CMD_INIT:
   1554      1.48     skrll 		error = config_init_component(cfdriver_ioconf_virtio,
   1555      1.48     skrll 		    cfattach_ioconf_virtio, cfdata_ioconf_virtio);
   1556      1.18  pgoyette 		break;
   1557      1.18  pgoyette 	case MODULE_CMD_FINI:
   1558      1.48     skrll 		error = config_fini_component(cfdriver_ioconf_virtio,
   1559      1.18  pgoyette 		    cfattach_ioconf_virtio, cfdata_ioconf_virtio);
   1560      1.18  pgoyette 		break;
   1561      1.18  pgoyette 	default:
   1562      1.18  pgoyette 		error = ENOTTY;
   1563      1.18  pgoyette 		break;
   1564      1.18  pgoyette 	}
   1565      1.18  pgoyette #endif
   1566      1.48     skrll 
   1567      1.48     skrll 	return error;
   1568      1.18  pgoyette }
   1569