Home | History | Annotate | Line # | Download | only in pci
virtio.c revision 1.42.2.1
      1  1.42.2.1   thorpej /*	$NetBSD: virtio.c,v 1.42.2.1 2021/04/03 22:28:49 thorpej Exp $	*/
      2       1.1   hannken 
      3       1.1   hannken /*
      4  1.42.2.1   thorpej  * Copyright (c) 2020 The NetBSD Foundation, Inc.
      5  1.42.2.1   thorpej  * 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.42.2.1   thorpej __KERNEL_RCSID(0, "$NetBSD: virtio.c,v 1.42.2.1 2021/04/03 22:28:49 thorpej 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.42.2.1   thorpej /* incomplete list */
     50  1.42.2.1   thorpej static const char *virtio_device_name[] = {
     51  1.42.2.1   thorpej 	"unknown (0)",			/*  0 */
     52  1.42.2.1   thorpej 	"network",			/*  1 */
     53  1.42.2.1   thorpej 	"block",			/*  2 */
     54  1.42.2.1   thorpej 	"console",			/*  3 */
     55  1.42.2.1   thorpej 	"entropy",			/*  4 */
     56  1.42.2.1   thorpej 	"memory balloon",		/*  5 */
     57  1.42.2.1   thorpej 	"I/O memory",			/*  6 */
     58  1.42.2.1   thorpej 	"remote processor messaging",	/*  7 */
     59  1.42.2.1   thorpej 	"SCSI",				/*  8 */
     60  1.42.2.1   thorpej 	"9P transport",			/*  9 */
     61  1.42.2.1   thorpej };
     62  1.42.2.1   thorpej #define NDEVNAMES	__arraycount(virtio_device_name)
     63  1.42.2.1   thorpej 
     64       1.1   hannken static void	virtio_init_vq(struct virtio_softc *,
     65       1.1   hannken 		    struct virtqueue *, const bool);
     66       1.1   hannken 
     67      1.29    cherry void
     68       1.1   hannken virtio_set_status(struct virtio_softc *sc, int status)
     69       1.1   hannken {
     70      1.31  jakllsch 	sc->sc_ops->set_status(sc, status);
     71      1.11     ozaki }
     72      1.11     ozaki 
     73       1.1   hannken /*
     74       1.1   hannken  * Reset the device.
     75       1.1   hannken  */
     76       1.1   hannken /*
     77       1.1   hannken  * To reset the device to a known state, do following:
     78       1.1   hannken  *	virtio_reset(sc);	     // this will stop the device activity
     79       1.1   hannken  *	<dequeue finished requests>; // virtio_dequeue() still can be called
     80       1.1   hannken  *	<revoke pending requests in the vqs if any>;
     81      1.42  jakllsch  *	virtio_reinit_start(sc);     // dequeue prohibitted
     82       1.1   hannken  *	newfeatures = virtio_negotiate_features(sc, requestedfeatures);
     83       1.1   hannken  *	<some other initialization>;
     84       1.1   hannken  *	virtio_reinit_end(sc);	     // device activated; enqueue allowed
     85       1.1   hannken  * Once attached, feature negotiation can only be allowed after virtio_reset.
     86       1.1   hannken  */
     87       1.1   hannken void
     88       1.1   hannken virtio_reset(struct virtio_softc *sc)
     89       1.1   hannken {
     90       1.1   hannken 	virtio_device_reset(sc);
     91       1.1   hannken }
     92       1.1   hannken 
     93       1.1   hannken void
     94       1.1   hannken virtio_reinit_start(struct virtio_softc *sc)
     95       1.1   hannken {
     96       1.1   hannken 	int i;
     97       1.1   hannken 
     98       1.1   hannken 	virtio_set_status(sc, VIRTIO_CONFIG_DEVICE_STATUS_ACK);
     99       1.1   hannken 	virtio_set_status(sc, VIRTIO_CONFIG_DEVICE_STATUS_DRIVER);
    100       1.1   hannken 	for (i = 0; i < sc->sc_nvqs; i++) {
    101       1.1   hannken 		int n;
    102       1.1   hannken 		struct virtqueue *vq = &sc->sc_vqs[i];
    103      1.31  jakllsch 		n = sc->sc_ops->read_queue_size(sc, vq->vq_index);
    104       1.1   hannken 		if (n == 0)	/* vq disappeared */
    105       1.1   hannken 			continue;
    106       1.1   hannken 		if (n != vq->vq_num) {
    107       1.1   hannken 			panic("%s: virtqueue size changed, vq index %d\n",
    108       1.1   hannken 			      device_xname(sc->sc_dev),
    109       1.1   hannken 			      vq->vq_index);
    110       1.1   hannken 		}
    111       1.1   hannken 		virtio_init_vq(sc, vq, true);
    112      1.31  jakllsch 		sc->sc_ops->setup_queue(sc, vq->vq_index,
    113  1.42.2.1   thorpej 		    vq->vq_dmamap->dm_segs[0].ds_addr);
    114      1.11     ozaki 	}
    115       1.1   hannken }
    116       1.1   hannken 
    117       1.1   hannken void
    118       1.1   hannken virtio_reinit_end(struct virtio_softc *sc)
    119       1.1   hannken {
    120       1.1   hannken 	virtio_set_status(sc, VIRTIO_CONFIG_DEVICE_STATUS_DRIVER_OK);
    121       1.1   hannken }
    122       1.1   hannken 
    123       1.1   hannken /*
    124       1.1   hannken  * Feature negotiation.
    125       1.1   hannken  */
    126  1.42.2.1   thorpej void
    127  1.42.2.1   thorpej virtio_negotiate_features(struct virtio_softc *sc, uint64_t guest_features)
    128       1.1   hannken {
    129       1.1   hannken 	if (!(device_cfdata(sc->sc_dev)->cf_flags & 1) &&
    130       1.1   hannken 	    !(device_cfdata(sc->sc_child)->cf_flags & 1)) /* XXX */
    131       1.1   hannken 		guest_features |= VIRTIO_F_RING_INDIRECT_DESC;
    132  1.42.2.1   thorpej 	sc->sc_ops->neg_features(sc, guest_features);
    133  1.42.2.1   thorpej 	if (sc->sc_active_features & VIRTIO_F_RING_INDIRECT_DESC)
    134       1.1   hannken 		sc->sc_indirect = true;
    135       1.1   hannken 	else
    136       1.1   hannken 		sc->sc_indirect = false;
    137       1.1   hannken }
    138       1.1   hannken 
    139  1.42.2.1   thorpej 
    140       1.1   hannken /*
    141  1.42.2.1   thorpej  * Device configuration registers readers/writers
    142       1.1   hannken  */
    143  1.42.2.1   thorpej #if 0
    144  1.42.2.1   thorpej #define DPRINTFR(n, fmt, val, index, num) \
    145  1.42.2.1   thorpej 	printf("\n%s (", n); \
    146  1.42.2.1   thorpej 	for (int i = 0; i < num; i++) \
    147  1.42.2.1   thorpej 		printf("%02x ", bus_space_read_1(sc->sc_devcfg_iot, sc->sc_devcfg_ioh, index+i)); \
    148  1.42.2.1   thorpej 	printf(") -> "); printf(fmt, val); printf("\n");
    149  1.42.2.1   thorpej #define DPRINTFR2(n, fmt, val_s, val_n) \
    150  1.42.2.1   thorpej 	printf("%s ", n); \
    151  1.42.2.1   thorpej 	printf("\n        stream "); printf(fmt, val_s); printf(" norm "); printf(fmt, val_n); printf("\n");
    152  1.42.2.1   thorpej #else
    153  1.42.2.1   thorpej #define DPRINTFR(n, fmt, val, index, num)
    154  1.42.2.1   thorpej #define DPRINTFR2(n, fmt, val_s, val_n)
    155  1.42.2.1   thorpej #endif
    156  1.42.2.1   thorpej 
    157  1.42.2.1   thorpej 
    158       1.1   hannken uint8_t
    159  1.42.2.1   thorpej virtio_read_device_config_1(struct virtio_softc *sc, int index) {
    160  1.42.2.1   thorpej 	bus_space_tag_t	   iot = sc->sc_devcfg_iot;
    161  1.42.2.1   thorpej 	bus_space_handle_t ioh = sc->sc_devcfg_ioh;
    162  1.42.2.1   thorpej 	uint8_t val;
    163  1.42.2.1   thorpej 
    164  1.42.2.1   thorpej 	val = bus_space_read_1(iot, ioh, index);
    165  1.42.2.1   thorpej 
    166  1.42.2.1   thorpej 	DPRINTFR("read_1", "%02x", val, index, 1);
    167  1.42.2.1   thorpej 	return val;
    168       1.1   hannken }
    169       1.1   hannken 
    170       1.1   hannken uint16_t
    171  1.42.2.1   thorpej virtio_read_device_config_2(struct virtio_softc *sc, int index) {
    172  1.42.2.1   thorpej 	bus_space_tag_t	   iot = sc->sc_devcfg_iot;
    173  1.42.2.1   thorpej 	bus_space_handle_t ioh = sc->sc_devcfg_ioh;
    174  1.42.2.1   thorpej 	uint16_t val;
    175  1.42.2.1   thorpej 
    176  1.42.2.1   thorpej 	val = bus_space_read_2(iot, ioh, index);
    177  1.42.2.1   thorpej 	if (BYTE_ORDER != sc->sc_bus_endian)
    178  1.42.2.1   thorpej 		val = bswap16(val);
    179  1.42.2.1   thorpej 
    180  1.42.2.1   thorpej 	DPRINTFR("read_2", "%04x", val, index, 2);
    181  1.42.2.1   thorpej 	DPRINTFR2("read_2", "%04x",
    182  1.42.2.1   thorpej 		bus_space_read_stream_2(sc->sc_devcfg_iot, sc->sc_devcfg_ioh, index),
    183  1.42.2.1   thorpej 		bus_space_read_2(sc->sc_devcfg_iot, sc->sc_devcfg_ioh, index));
    184  1.42.2.1   thorpej 	return val;
    185       1.1   hannken }
    186       1.1   hannken 
    187       1.1   hannken uint32_t
    188  1.42.2.1   thorpej virtio_read_device_config_4(struct virtio_softc *sc, int index) {
    189  1.42.2.1   thorpej 	bus_space_tag_t	   iot = sc->sc_devcfg_iot;
    190  1.42.2.1   thorpej 	bus_space_handle_t ioh = sc->sc_devcfg_ioh;
    191  1.42.2.1   thorpej 	uint32_t val;
    192  1.42.2.1   thorpej 
    193  1.42.2.1   thorpej 	val = bus_space_read_4(iot, ioh, index);
    194  1.42.2.1   thorpej 	if (BYTE_ORDER != sc->sc_bus_endian)
    195  1.42.2.1   thorpej 		val = bswap32(val);
    196  1.42.2.1   thorpej 
    197  1.42.2.1   thorpej 	DPRINTFR("read_4", "%08x", val, index, 4);
    198  1.42.2.1   thorpej 	DPRINTFR2("read_4", "%08x",
    199  1.42.2.1   thorpej 		bus_space_read_stream_4(sc->sc_devcfg_iot, sc->sc_devcfg_ioh, index),
    200  1.42.2.1   thorpej 		bus_space_read_4(sc->sc_devcfg_iot, sc->sc_devcfg_ioh, index));
    201  1.42.2.1   thorpej 	return val;
    202       1.1   hannken }
    203       1.1   hannken 
    204  1.42.2.1   thorpej /*
    205  1.42.2.1   thorpej  * The Virtio spec explicitly tells that reading and writing 8 bytes are not
    206  1.42.2.1   thorpej  * considered atomic and no triggers may be connected to reading or writing
    207  1.42.2.1   thorpej  * it. We access it using two 32 reads. See virtio spec 4.1.3.1.
    208  1.42.2.1   thorpej  */
    209       1.1   hannken uint64_t
    210  1.42.2.1   thorpej virtio_read_device_config_8(struct virtio_softc *sc, int index) {
    211  1.42.2.1   thorpej 	bus_space_tag_t	   iot = sc->sc_devcfg_iot;
    212  1.42.2.1   thorpej 	bus_space_handle_t ioh = sc->sc_devcfg_ioh;
    213  1.42.2.1   thorpej 	union {
    214  1.42.2.1   thorpej 		uint64_t u64;
    215  1.42.2.1   thorpej 		uint32_t l[2];
    216  1.42.2.1   thorpej 	} v;
    217  1.42.2.1   thorpej 	uint64_t val;
    218  1.42.2.1   thorpej 
    219  1.42.2.1   thorpej 	v.l[0] = bus_space_read_4(iot, ioh, index);
    220  1.42.2.1   thorpej 	v.l[1] = bus_space_read_4(iot, ioh, index + 4);
    221  1.42.2.1   thorpej 	if (sc->sc_bus_endian != sc->sc_struct_endian) {
    222  1.42.2.1   thorpej 		v.l[0] = bswap32(v.l[0]);
    223  1.42.2.1   thorpej 		v.l[1] = bswap32(v.l[1]);
    224  1.42.2.1   thorpej 	}
    225  1.42.2.1   thorpej 	val = v.u64;
    226  1.42.2.1   thorpej 
    227  1.42.2.1   thorpej 	if (BYTE_ORDER != sc->sc_struct_endian)
    228  1.42.2.1   thorpej 		val = bswap64(val);
    229  1.42.2.1   thorpej 
    230  1.42.2.1   thorpej 	DPRINTFR("read_8", "%08lx", val, index, 8);
    231  1.42.2.1   thorpej 	DPRINTFR2("read_8 low ", "%08x",
    232  1.42.2.1   thorpej 		bus_space_read_stream_4(sc->sc_devcfg_iot, sc->sc_devcfg_ioh, index),
    233  1.42.2.1   thorpej 		bus_space_read_4(sc->sc_devcfg_iot, sc->sc_devcfg_ioh, index));
    234  1.42.2.1   thorpej 	DPRINTFR2("read_8 high ", "%08x",
    235  1.42.2.1   thorpej 		bus_space_read_stream_4(sc->sc_devcfg_iot, sc->sc_devcfg_ioh, index + 4),
    236  1.42.2.1   thorpej 		bus_space_read_4(sc->sc_devcfg_iot, sc->sc_devcfg_ioh, index + 4));
    237  1.42.2.1   thorpej 	return val;
    238  1.42.2.1   thorpej }
    239  1.42.2.1   thorpej 
    240  1.42.2.1   thorpej /*
    241  1.42.2.1   thorpej  * In the older virtio spec, device config registers are host endian. On newer
    242  1.42.2.1   thorpej  * they are little endian. Some newer devices however explicitly specify their
    243  1.42.2.1   thorpej  * register to always be little endian. These fuctions cater for these.
    244  1.42.2.1   thorpej  */
    245  1.42.2.1   thorpej uint16_t
    246  1.42.2.1   thorpej virtio_read_device_config_le_2(struct virtio_softc *sc, int index) {
    247  1.42.2.1   thorpej 	bus_space_tag_t	   iot = sc->sc_devcfg_iot;
    248  1.42.2.1   thorpej 	bus_space_handle_t ioh = sc->sc_devcfg_ioh;
    249  1.42.2.1   thorpej 	uint16_t val;
    250  1.42.2.1   thorpej 
    251  1.42.2.1   thorpej 	val = bus_space_read_2(iot, ioh, index);
    252  1.42.2.1   thorpej 	if (sc->sc_bus_endian != LITTLE_ENDIAN)
    253  1.42.2.1   thorpej 		val = bswap16(val);
    254  1.42.2.1   thorpej 
    255  1.42.2.1   thorpej 	DPRINTFR("read_le_2", "%04x", val, index, 2);
    256  1.42.2.1   thorpej 	DPRINTFR2("read_le_2", "%04x",
    257  1.42.2.1   thorpej 		bus_space_read_stream_2(sc->sc_devcfg_iot, sc->sc_devcfg_ioh, 0),
    258  1.42.2.1   thorpej 		bus_space_read_2(sc->sc_devcfg_iot, sc->sc_devcfg_ioh, 0));
    259  1.42.2.1   thorpej 	return val;
    260  1.42.2.1   thorpej }
    261  1.42.2.1   thorpej 
    262  1.42.2.1   thorpej uint32_t
    263  1.42.2.1   thorpej virtio_read_device_config_le_4(struct virtio_softc *sc, int index) {
    264  1.42.2.1   thorpej 	bus_space_tag_t	   iot = sc->sc_devcfg_iot;
    265  1.42.2.1   thorpej 	bus_space_handle_t ioh = sc->sc_devcfg_ioh;
    266  1.42.2.1   thorpej 	uint32_t val;
    267  1.42.2.1   thorpej 
    268  1.42.2.1   thorpej 	val = bus_space_read_4(iot, ioh, index);
    269  1.42.2.1   thorpej 	if (sc->sc_bus_endian != LITTLE_ENDIAN)
    270  1.42.2.1   thorpej 		val = bswap32(val);
    271  1.42.2.1   thorpej 
    272  1.42.2.1   thorpej 	DPRINTFR("read_le_4", "%08x", val, index, 4);
    273  1.42.2.1   thorpej 	DPRINTFR2("read_le_4", "%08x",
    274  1.42.2.1   thorpej 		bus_space_read_stream_4(sc->sc_devcfg_iot, sc->sc_devcfg_ioh, 0),
    275  1.42.2.1   thorpej 		bus_space_read_4(sc->sc_devcfg_iot, sc->sc_devcfg_ioh, 0));
    276  1.42.2.1   thorpej 	return val;
    277  1.42.2.1   thorpej }
    278  1.42.2.1   thorpej 
    279  1.42.2.1   thorpej void
    280  1.42.2.1   thorpej virtio_write_device_config_1(struct virtio_softc *sc, int index, uint8_t value)
    281  1.42.2.1   thorpej {
    282  1.42.2.1   thorpej 	bus_space_tag_t	   iot = sc->sc_devcfg_iot;
    283  1.42.2.1   thorpej 	bus_space_handle_t ioh = sc->sc_devcfg_ioh;
    284  1.42.2.1   thorpej 
    285  1.42.2.1   thorpej 	bus_space_write_1(iot, ioh, index, value);
    286  1.42.2.1   thorpej }
    287  1.42.2.1   thorpej 
    288  1.42.2.1   thorpej void
    289  1.42.2.1   thorpej virtio_write_device_config_2(struct virtio_softc *sc, int index, uint16_t value)
    290       1.1   hannken {
    291  1.42.2.1   thorpej 	bus_space_tag_t	   iot = sc->sc_devcfg_iot;
    292  1.42.2.1   thorpej 	bus_space_handle_t ioh = sc->sc_devcfg_ioh;
    293  1.42.2.1   thorpej 
    294  1.42.2.1   thorpej 	if (BYTE_ORDER != sc->sc_bus_endian)
    295  1.42.2.1   thorpej 		value = bswap16(value);
    296  1.42.2.1   thorpej 	bus_space_write_2(iot, ioh, index, value);
    297       1.1   hannken }
    298       1.1   hannken 
    299       1.1   hannken void
    300  1.42.2.1   thorpej virtio_write_device_config_4(struct virtio_softc *sc, int index, uint32_t value)
    301       1.1   hannken {
    302  1.42.2.1   thorpej 	bus_space_tag_t	   iot = sc->sc_devcfg_iot;
    303  1.42.2.1   thorpej 	bus_space_handle_t ioh = sc->sc_devcfg_ioh;
    304  1.42.2.1   thorpej 
    305  1.42.2.1   thorpej 	if (BYTE_ORDER != sc->sc_bus_endian)
    306  1.42.2.1   thorpej 		value = bswap32(value);
    307  1.42.2.1   thorpej 	bus_space_write_4(iot, ioh, index, value);
    308       1.1   hannken }
    309       1.1   hannken 
    310  1.42.2.1   thorpej /*
    311  1.42.2.1   thorpej  * The Virtio spec explicitly tells that reading and writing 8 bytes are not
    312  1.42.2.1   thorpej  * considered atomic and no triggers may be connected to reading or writing
    313  1.42.2.1   thorpej  * it. We access it using two 32 bit writes. For good measure it is stated to
    314  1.42.2.1   thorpej  * always write lsb first just in case of a hypervisor bug. See See virtio
    315  1.42.2.1   thorpej  * spec 4.1.3.1.
    316  1.42.2.1   thorpej  */
    317       1.1   hannken void
    318  1.42.2.1   thorpej virtio_write_device_config_8(struct virtio_softc *sc, int index, uint64_t value)
    319       1.1   hannken {
    320  1.42.2.1   thorpej 	bus_space_tag_t	   iot = sc->sc_devcfg_iot;
    321  1.42.2.1   thorpej 	bus_space_handle_t ioh = sc->sc_devcfg_ioh;
    322  1.42.2.1   thorpej 	union {
    323  1.42.2.1   thorpej 		uint64_t u64;
    324  1.42.2.1   thorpej 		uint32_t l[2];
    325  1.42.2.1   thorpej 	} v;
    326  1.42.2.1   thorpej 
    327  1.42.2.1   thorpej 	if (BYTE_ORDER != sc->sc_struct_endian)
    328  1.42.2.1   thorpej 		value = bswap64(value);
    329  1.42.2.1   thorpej 
    330  1.42.2.1   thorpej 	v.u64 = value;
    331  1.42.2.1   thorpej 	if (sc->sc_bus_endian != sc->sc_struct_endian) {
    332  1.42.2.1   thorpej 		v.l[0] = bswap32(v.l[0]);
    333  1.42.2.1   thorpej 		v.l[1] = bswap32(v.l[1]);
    334  1.42.2.1   thorpej 	}
    335  1.42.2.1   thorpej 
    336  1.42.2.1   thorpej 	if (sc->sc_struct_endian == LITTLE_ENDIAN) {
    337  1.42.2.1   thorpej 		bus_space_write_4(iot, ioh, index,     v.l[0]);
    338  1.42.2.1   thorpej 		bus_space_write_4(iot, ioh, index + 4, v.l[1]);
    339  1.42.2.1   thorpej 	} else {
    340  1.42.2.1   thorpej 		bus_space_write_4(iot, ioh, index + 4, v.l[1]);
    341  1.42.2.1   thorpej 		bus_space_write_4(iot, ioh, index,     v.l[0]);
    342  1.42.2.1   thorpej 	}
    343       1.1   hannken }
    344       1.1   hannken 
    345  1.42.2.1   thorpej /*
    346  1.42.2.1   thorpej  * In the older virtio spec, device config registers are host endian. On newer
    347  1.42.2.1   thorpej  * they are little endian. Some newer devices however explicitly specify their
    348  1.42.2.1   thorpej  * register to always be little endian. These fuctions cater for these.
    349  1.42.2.1   thorpej  */
    350       1.1   hannken void
    351  1.42.2.1   thorpej virtio_write_device_config_le_2(struct virtio_softc *sc, int index, uint16_t value)
    352       1.1   hannken {
    353  1.42.2.1   thorpej 	bus_space_tag_t	   iot = sc->sc_devcfg_iot;
    354  1.42.2.1   thorpej 	bus_space_handle_t ioh = sc->sc_devcfg_ioh;
    355  1.42.2.1   thorpej 
    356  1.42.2.1   thorpej 	if (sc->sc_bus_endian != LITTLE_ENDIAN)
    357  1.42.2.1   thorpej 		value = bswap16(value);
    358  1.42.2.1   thorpej 	bus_space_write_2(iot, ioh, index, value);
    359       1.1   hannken }
    360       1.1   hannken 
    361       1.1   hannken void
    362  1.42.2.1   thorpej virtio_write_device_config_le_4(struct virtio_softc *sc, int index, uint32_t value)
    363       1.1   hannken {
    364  1.42.2.1   thorpej 	bus_space_tag_t	   iot = sc->sc_devcfg_iot;
    365  1.42.2.1   thorpej 	bus_space_handle_t ioh = sc->sc_devcfg_ioh;
    366  1.42.2.1   thorpej 
    367  1.42.2.1   thorpej 	if (sc->sc_bus_endian != LITTLE_ENDIAN)
    368  1.42.2.1   thorpej 		value = bswap32(value);
    369  1.42.2.1   thorpej 	bus_space_write_4(iot, ioh, index, value);
    370       1.1   hannken }
    371       1.1   hannken 
    372  1.42.2.1   thorpej 
    373  1.42.2.1   thorpej /*
    374  1.42.2.1   thorpej  * data structures endian helpers
    375  1.42.2.1   thorpej  */
    376  1.42.2.1   thorpej uint16_t virtio_rw16(struct virtio_softc *sc, uint16_t val)
    377  1.42.2.1   thorpej {
    378  1.42.2.1   thorpej 	KASSERT(sc);
    379  1.42.2.1   thorpej 	return BYTE_ORDER != sc->sc_struct_endian ? bswap16(val) : val;
    380  1.42.2.1   thorpej }
    381  1.42.2.1   thorpej 
    382  1.42.2.1   thorpej uint32_t virtio_rw32(struct virtio_softc *sc, uint32_t val)
    383  1.42.2.1   thorpej {
    384  1.42.2.1   thorpej 	KASSERT(sc);
    385  1.42.2.1   thorpej 	return BYTE_ORDER != sc->sc_struct_endian ? bswap32(val) : val;
    386  1.42.2.1   thorpej }
    387  1.42.2.1   thorpej 
    388  1.42.2.1   thorpej uint64_t virtio_rw64(struct virtio_softc *sc, uint64_t val)
    389  1.42.2.1   thorpej {
    390  1.42.2.1   thorpej 	KASSERT(sc);
    391  1.42.2.1   thorpej 	return BYTE_ORDER != sc->sc_struct_endian ? bswap64(val) : val;
    392  1.42.2.1   thorpej }
    393  1.42.2.1   thorpej 
    394  1.42.2.1   thorpej 
    395       1.1   hannken /*
    396       1.1   hannken  * Interrupt handler.
    397       1.1   hannken  */
    398       1.8     ozaki static void
    399       1.8     ozaki virtio_soft_intr(void *arg)
    400       1.8     ozaki {
    401       1.8     ozaki 	struct virtio_softc *sc = arg;
    402       1.8     ozaki 
    403       1.8     ozaki 	KASSERT(sc->sc_intrhand != NULL);
    404       1.8     ozaki 
    405       1.8     ozaki 	(sc->sc_intrhand)(sc);
    406       1.8     ozaki }
    407       1.8     ozaki 
    408       1.1   hannken /*
    409       1.1   hannken  * dmamap sync operations for a virtqueue.
    410       1.1   hannken  */
    411       1.1   hannken static inline void
    412       1.1   hannken vq_sync_descs(struct virtio_softc *sc, struct virtqueue *vq, int ops)
    413       1.1   hannken {
    414       1.1   hannken 	/* availoffset == sizeof(vring_desc)*vq_num */
    415       1.1   hannken 	bus_dmamap_sync(sc->sc_dmat, vq->vq_dmamap, 0, vq->vq_availoffset,
    416       1.1   hannken 			ops);
    417       1.1   hannken }
    418       1.1   hannken 
    419       1.1   hannken static inline void
    420       1.1   hannken vq_sync_aring(struct virtio_softc *sc, struct virtqueue *vq, int ops)
    421       1.1   hannken {
    422  1.42.2.1   thorpej 	uint16_t hdrlen = offsetof(struct vring_avail, ring);
    423  1.42.2.1   thorpej 	if (sc->sc_active_features & VIRTIO_F_RING_EVENT_IDX)
    424  1.42.2.1   thorpej 		hdrlen += sizeof(uint16_t);
    425  1.42.2.1   thorpej 
    426       1.1   hannken 	bus_dmamap_sync(sc->sc_dmat, vq->vq_dmamap,
    427       1.1   hannken 			vq->vq_availoffset,
    428  1.42.2.1   thorpej 			hdrlen + sc->sc_nvqs * sizeof(uint16_t),
    429       1.1   hannken 			ops);
    430       1.1   hannken }
    431       1.1   hannken 
    432       1.1   hannken static inline void
    433       1.1   hannken vq_sync_uring(struct virtio_softc *sc, struct virtqueue *vq, int ops)
    434       1.1   hannken {
    435  1.42.2.1   thorpej 	uint16_t hdrlen = offsetof(struct vring_used, ring);
    436  1.42.2.1   thorpej 	if (sc->sc_active_features & VIRTIO_F_RING_EVENT_IDX)
    437  1.42.2.1   thorpej 		hdrlen += sizeof(uint16_t);
    438  1.42.2.1   thorpej 
    439       1.1   hannken 	bus_dmamap_sync(sc->sc_dmat, vq->vq_dmamap,
    440       1.1   hannken 			vq->vq_usedoffset,
    441  1.42.2.1   thorpej 			hdrlen + sc->sc_nvqs * sizeof(struct vring_used_elem),
    442       1.1   hannken 			ops);
    443       1.1   hannken }
    444       1.1   hannken 
    445       1.1   hannken static inline void
    446       1.1   hannken vq_sync_indirect(struct virtio_softc *sc, struct virtqueue *vq, int slot,
    447       1.1   hannken 		     int ops)
    448       1.1   hannken {
    449       1.1   hannken 	int offset = vq->vq_indirectoffset
    450       1.1   hannken 		      + sizeof(struct vring_desc) * vq->vq_maxnsegs * slot;
    451       1.1   hannken 
    452       1.1   hannken 	bus_dmamap_sync(sc->sc_dmat, vq->vq_dmamap,
    453       1.1   hannken 			offset, sizeof(struct vring_desc) * vq->vq_maxnsegs,
    454       1.1   hannken 			ops);
    455       1.1   hannken }
    456       1.1   hannken 
    457       1.1   hannken /*
    458       1.1   hannken  * Can be used as sc_intrhand.
    459       1.1   hannken  */
    460       1.1   hannken /*
    461       1.1   hannken  * Scan vq, bus_dmamap_sync for the vqs (not for the payload),
    462       1.1   hannken  * and calls (*vq_done)() if some entries are consumed.
    463       1.1   hannken  */
    464      1.41  yamaguch bool
    465      1.41  yamaguch virtio_vq_is_enqueued(struct virtio_softc *sc, struct virtqueue *vq)
    466      1.37  yamaguch {
    467      1.37  yamaguch 
    468      1.37  yamaguch 	if (vq->vq_queued) {
    469      1.37  yamaguch 		vq->vq_queued = 0;
    470      1.37  yamaguch 		vq_sync_aring(sc, vq, BUS_DMASYNC_POSTWRITE);
    471      1.37  yamaguch 	}
    472      1.37  yamaguch 	vq_sync_uring(sc, vq, BUS_DMASYNC_POSTREAD);
    473      1.37  yamaguch 	membar_consumer();
    474      1.37  yamaguch 
    475  1.42.2.1   thorpej 	return (vq->vq_used_idx != virtio_rw16(sc, vq->vq_used->idx)) ? 1 : 0;
    476      1.37  yamaguch }
    477      1.37  yamaguch 
    478       1.1   hannken int
    479       1.1   hannken virtio_vq_intr(struct virtio_softc *sc)
    480       1.1   hannken {
    481       1.1   hannken 	struct virtqueue *vq;
    482       1.1   hannken 	int i, r = 0;
    483       1.1   hannken 
    484       1.1   hannken 	for (i = 0; i < sc->sc_nvqs; i++) {
    485       1.1   hannken 		vq = &sc->sc_vqs[i];
    486      1.41  yamaguch 		if (virtio_vq_is_enqueued(sc, vq) == 1) {
    487      1.41  yamaguch 			if (vq->vq_done)
    488      1.41  yamaguch 				r |= (vq->vq_done)(vq);
    489      1.41  yamaguch 		}
    490       1.1   hannken 	}
    491       1.1   hannken 
    492       1.1   hannken 	return r;
    493       1.1   hannken }
    494       1.1   hannken 
    495      1.41  yamaguch int
    496      1.41  yamaguch virtio_vq_intrhand(struct virtio_softc *sc)
    497      1.37  yamaguch {
    498      1.41  yamaguch 	struct virtqueue *vq;
    499      1.41  yamaguch 	int i, r = 0;
    500      1.41  yamaguch 
    501      1.41  yamaguch 	for (i = 0; i < sc->sc_nvqs; i++) {
    502      1.41  yamaguch 		vq = &sc->sc_vqs[i];
    503      1.41  yamaguch 		r |= (vq->vq_intrhand)(vq->vq_intrhand_arg);
    504      1.41  yamaguch 	}
    505      1.37  yamaguch 
    506      1.41  yamaguch 	return r;
    507      1.37  yamaguch }
    508      1.37  yamaguch 
    509  1.42.2.1   thorpej 
    510  1.42.2.1   thorpej /*
    511  1.42.2.1   thorpej  * Increase the event index in order to delay interrupts.
    512  1.42.2.1   thorpej  */
    513  1.42.2.1   thorpej int
    514  1.42.2.1   thorpej virtio_postpone_intr(struct virtio_softc *sc, struct virtqueue *vq,
    515  1.42.2.1   thorpej 		uint16_t nslots)
    516  1.42.2.1   thorpej {
    517  1.42.2.1   thorpej 	uint16_t	idx, nused;
    518  1.42.2.1   thorpej 
    519  1.42.2.1   thorpej 	idx = vq->vq_used_idx + nslots;
    520  1.42.2.1   thorpej 
    521  1.42.2.1   thorpej 	/* set the new event index: avail_ring->used_event = idx */
    522  1.42.2.1   thorpej 	*vq->vq_used_event = virtio_rw16(sc, idx);
    523  1.42.2.1   thorpej 	membar_producer();
    524  1.42.2.1   thorpej 
    525  1.42.2.1   thorpej 	vq_sync_aring(vq->vq_owner, vq, BUS_DMASYNC_PREWRITE);
    526  1.42.2.1   thorpej 	vq->vq_queued++;
    527  1.42.2.1   thorpej 
    528  1.42.2.1   thorpej 	nused = (uint16_t)
    529  1.42.2.1   thorpej 		(virtio_rw16(sc, vq->vq_used->idx) - vq->vq_used_idx);
    530  1.42.2.1   thorpej 	KASSERT(nused <= vq->vq_num);
    531  1.42.2.1   thorpej 
    532  1.42.2.1   thorpej 	return nslots < nused;
    533  1.42.2.1   thorpej }
    534  1.42.2.1   thorpej 
    535  1.42.2.1   thorpej /*
    536  1.42.2.1   thorpej  * Postpone interrupt until 3/4 of the available descriptors have been
    537  1.42.2.1   thorpej  * consumed.
    538  1.42.2.1   thorpej  */
    539  1.42.2.1   thorpej int
    540  1.42.2.1   thorpej virtio_postpone_intr_smart(struct virtio_softc *sc, struct virtqueue *vq)
    541  1.42.2.1   thorpej {
    542  1.42.2.1   thorpej 	uint16_t	nslots;
    543  1.42.2.1   thorpej 
    544  1.42.2.1   thorpej 	nslots = (uint16_t)
    545  1.42.2.1   thorpej 		(virtio_rw16(sc, vq->vq_avail->idx) - vq->vq_used_idx) * 3 / 4;
    546  1.42.2.1   thorpej 
    547  1.42.2.1   thorpej 	return virtio_postpone_intr(sc, vq, nslots);
    548  1.42.2.1   thorpej }
    549  1.42.2.1   thorpej 
    550  1.42.2.1   thorpej /*
    551  1.42.2.1   thorpej  * Postpone interrupt until all of the available descriptors have been
    552  1.42.2.1   thorpej  * consumed.
    553  1.42.2.1   thorpej  */
    554  1.42.2.1   thorpej int
    555  1.42.2.1   thorpej virtio_postpone_intr_far(struct virtio_softc *sc, struct virtqueue *vq)
    556  1.42.2.1   thorpej {
    557  1.42.2.1   thorpej 	uint16_t	nslots;
    558  1.42.2.1   thorpej 
    559  1.42.2.1   thorpej 	nslots = (uint16_t)
    560  1.42.2.1   thorpej 		(virtio_rw16(sc, vq->vq_avail->idx) - vq->vq_used_idx);
    561  1.42.2.1   thorpej 
    562  1.42.2.1   thorpej 	return virtio_postpone_intr(sc, vq, nslots);
    563  1.42.2.1   thorpej }
    564  1.42.2.1   thorpej 
    565       1.1   hannken /*
    566       1.1   hannken  * Start/stop vq interrupt.  No guarantee.
    567       1.1   hannken  */
    568       1.1   hannken void
    569       1.1   hannken virtio_stop_vq_intr(struct virtio_softc *sc, struct virtqueue *vq)
    570       1.1   hannken {
    571  1.42.2.1   thorpej 	if (sc->sc_active_features & VIRTIO_F_RING_EVENT_IDX) {
    572  1.42.2.1   thorpej 		/*
    573  1.42.2.1   thorpej 		 * No way to disable the interrupt completely with
    574  1.42.2.1   thorpej 		 * RingEventIdx. Instead advance used_event by half the
    575  1.42.2.1   thorpej 		 * possible value. This won't happen soon and is far enough in
    576  1.42.2.1   thorpej 		 * the past to not trigger a spurios interrupt.
    577  1.42.2.1   thorpej 		 */
    578  1.42.2.1   thorpej 		*vq->vq_used_event = virtio_rw16(sc, vq->vq_used_idx + 0x8000);
    579  1.42.2.1   thorpej 	} else {
    580  1.42.2.1   thorpej 		vq->vq_avail->flags |= virtio_rw16(sc, VRING_AVAIL_F_NO_INTERRUPT);
    581  1.42.2.1   thorpej 	}
    582       1.1   hannken 	vq_sync_aring(sc, vq, BUS_DMASYNC_PREWRITE);
    583       1.1   hannken 	vq->vq_queued++;
    584       1.1   hannken }
    585       1.1   hannken 
    586  1.42.2.1   thorpej int
    587       1.1   hannken virtio_start_vq_intr(struct virtio_softc *sc, struct virtqueue *vq)
    588       1.1   hannken {
    589  1.42.2.1   thorpej 	if (sc->sc_active_features & VIRTIO_F_RING_EVENT_IDX) {
    590  1.42.2.1   thorpej 		/*
    591  1.42.2.1   thorpej 		 * If event index feature is negotiated, enabling interrupts
    592  1.42.2.1   thorpej 		 * is done through setting the latest consumed index in the
    593  1.42.2.1   thorpej 		 * used_event field
    594  1.42.2.1   thorpej 		 */
    595  1.42.2.1   thorpej 		*vq->vq_used_event = virtio_rw16(sc, vq->vq_used_idx);
    596  1.42.2.1   thorpej 	} else {
    597  1.42.2.1   thorpej 		vq->vq_avail->flags &= ~virtio_rw16(sc, VRING_AVAIL_F_NO_INTERRUPT);
    598  1.42.2.1   thorpej 	}
    599       1.1   hannken 	vq_sync_aring(sc, vq, BUS_DMASYNC_PREWRITE);
    600       1.1   hannken 	vq->vq_queued++;
    601  1.42.2.1   thorpej 
    602  1.42.2.1   thorpej 	return vq->vq_used_idx != virtio_rw16(sc, vq->vq_used->idx);
    603       1.1   hannken }
    604       1.1   hannken 
    605       1.1   hannken /*
    606       1.1   hannken  * Initialize vq structure.
    607       1.1   hannken  */
    608       1.1   hannken static void
    609      1.15   msaitoh virtio_init_vq(struct virtio_softc *sc, struct virtqueue *vq,
    610      1.15   msaitoh     const bool reinit)
    611       1.1   hannken {
    612       1.1   hannken 	int i, j;
    613       1.1   hannken 	int vq_size = vq->vq_num;
    614       1.1   hannken 
    615       1.1   hannken 	memset(vq->vq_vaddr, 0, vq->vq_bytesize);
    616       1.1   hannken 
    617       1.1   hannken 	/* build the indirect descriptor chain */
    618       1.1   hannken 	if (vq->vq_indirect != NULL) {
    619       1.1   hannken 		struct vring_desc *vd;
    620       1.1   hannken 
    621       1.1   hannken 		for (i = 0; i < vq_size; i++) {
    622       1.1   hannken 			vd = vq->vq_indirect;
    623       1.1   hannken 			vd += vq->vq_maxnsegs * i;
    624      1.23    martin 			for (j = 0; j < vq->vq_maxnsegs-1; j++) {
    625  1.42.2.1   thorpej 				vd[j].next = virtio_rw16(sc, j + 1);
    626      1.23    martin 			}
    627       1.1   hannken 		}
    628       1.1   hannken 	}
    629       1.1   hannken 
    630       1.1   hannken 	/* free slot management */
    631       1.1   hannken 	SIMPLEQ_INIT(&vq->vq_freelist);
    632       1.1   hannken 	for (i = 0; i < vq_size; i++) {
    633       1.1   hannken 		SIMPLEQ_INSERT_TAIL(&vq->vq_freelist,
    634       1.1   hannken 				    &vq->vq_entries[i], qe_list);
    635       1.1   hannken 		vq->vq_entries[i].qe_index = i;
    636       1.1   hannken 	}
    637       1.1   hannken 	if (!reinit)
    638       1.1   hannken 		mutex_init(&vq->vq_freelist_lock, MUTEX_SPIN, sc->sc_ipl);
    639       1.1   hannken 
    640       1.1   hannken 	/* enqueue/dequeue status */
    641       1.1   hannken 	vq->vq_avail_idx = 0;
    642       1.1   hannken 	vq->vq_used_idx = 0;
    643       1.1   hannken 	vq->vq_queued = 0;
    644       1.1   hannken 	if (!reinit) {
    645       1.1   hannken 		mutex_init(&vq->vq_aring_lock, MUTEX_SPIN, sc->sc_ipl);
    646       1.1   hannken 		mutex_init(&vq->vq_uring_lock, MUTEX_SPIN, sc->sc_ipl);
    647       1.1   hannken 	}
    648       1.1   hannken 	vq_sync_aring(sc, vq, BUS_DMASYNC_PREWRITE);
    649       1.1   hannken 	vq_sync_uring(sc, vq, BUS_DMASYNC_PREREAD);
    650       1.1   hannken 	vq->vq_queued++;
    651       1.1   hannken }
    652  1.42.2.1   thorpej 
    653       1.1   hannken /*
    654       1.1   hannken  * Allocate/free a vq.
    655       1.1   hannken  */
    656       1.1   hannken int
    657      1.15   msaitoh virtio_alloc_vq(struct virtio_softc *sc, struct virtqueue *vq, int index,
    658      1.15   msaitoh     int maxsegsize, int maxnsegs, const char *name)
    659       1.1   hannken {
    660       1.1   hannken 	int vq_size, allocsize1, allocsize2, allocsize3, allocsize = 0;
    661  1.42.2.1   thorpej 	int rsegs, r, hdrlen;
    662       1.1   hannken #define VIRTQUEUE_ALIGN(n)	(((n)+(VIRTIO_PAGE_SIZE-1))&	\
    663       1.1   hannken 				 ~(VIRTIO_PAGE_SIZE-1))
    664       1.1   hannken 
    665      1.22  jdolecek 	/* Make sure callers allocate vqs in order */
    666      1.22  jdolecek 	KASSERT(sc->sc_nvqs == index);
    667      1.22  jdolecek 
    668       1.1   hannken 	memset(vq, 0, sizeof(*vq));
    669       1.1   hannken 
    670      1.31  jakllsch 	vq_size = sc->sc_ops->read_queue_size(sc, index);
    671       1.1   hannken 	if (vq_size == 0) {
    672       1.1   hannken 		aprint_error_dev(sc->sc_dev,
    673       1.1   hannken 				 "virtqueue not exist, index %d for %s\n",
    674       1.1   hannken 				 index, name);
    675       1.1   hannken 		goto err;
    676       1.1   hannken 	}
    677  1.42.2.1   thorpej 
    678  1.42.2.1   thorpej 	hdrlen = sc->sc_active_features & VIRTIO_F_RING_EVENT_IDX ? 3 : 2;
    679  1.42.2.1   thorpej 
    680       1.1   hannken 	/* allocsize1: descriptor table + avail ring + pad */
    681       1.1   hannken 	allocsize1 = VIRTQUEUE_ALIGN(sizeof(struct vring_desc)*vq_size
    682  1.42.2.1   thorpej 			     + sizeof(uint16_t)*(hdrlen + vq_size));
    683       1.1   hannken 	/* allocsize2: used ring + pad */
    684  1.42.2.1   thorpej 	allocsize2 = VIRTQUEUE_ALIGN(sizeof(uint16_t) * hdrlen
    685  1.42.2.1   thorpej 			     + sizeof(struct vring_used_elem)*vq_size);
    686       1.1   hannken 	/* allocsize3: indirect table */
    687       1.1   hannken 	if (sc->sc_indirect && maxnsegs >= MINSEG_INDIRECT)
    688       1.1   hannken 		allocsize3 = sizeof(struct vring_desc) * maxnsegs * vq_size;
    689       1.1   hannken 	else
    690       1.1   hannken 		allocsize3 = 0;
    691       1.1   hannken 	allocsize = allocsize1 + allocsize2 + allocsize3;
    692       1.1   hannken 
    693       1.1   hannken 	/* alloc and map the memory */
    694       1.1   hannken 	r = bus_dmamem_alloc(sc->sc_dmat, allocsize, VIRTIO_PAGE_SIZE, 0,
    695  1.42.2.1   thorpej 			     &vq->vq_segs[0], 1, &rsegs, BUS_DMA_WAITOK);
    696       1.1   hannken 	if (r != 0) {
    697       1.1   hannken 		aprint_error_dev(sc->sc_dev,
    698       1.1   hannken 				 "virtqueue %d for %s allocation failed, "
    699       1.1   hannken 				 "error code %d\n", index, name, r);
    700       1.1   hannken 		goto err;
    701       1.1   hannken 	}
    702  1.42.2.1   thorpej 	r = bus_dmamem_map(sc->sc_dmat, &vq->vq_segs[0], rsegs, allocsize,
    703  1.42.2.1   thorpej 			   &vq->vq_vaddr, BUS_DMA_WAITOK);
    704       1.1   hannken 	if (r != 0) {
    705       1.1   hannken 		aprint_error_dev(sc->sc_dev,
    706       1.1   hannken 				 "virtqueue %d for %s map failed, "
    707       1.1   hannken 				 "error code %d\n", index, name, r);
    708       1.1   hannken 		goto err;
    709       1.1   hannken 	}
    710       1.1   hannken 	r = bus_dmamap_create(sc->sc_dmat, allocsize, 1, allocsize, 0,
    711  1.42.2.1   thorpej 			      BUS_DMA_WAITOK, &vq->vq_dmamap);
    712       1.1   hannken 	if (r != 0) {
    713       1.1   hannken 		aprint_error_dev(sc->sc_dev,
    714       1.1   hannken 				 "virtqueue %d for %s dmamap creation failed, "
    715       1.1   hannken 				 "error code %d\n", index, name, r);
    716       1.1   hannken 		goto err;
    717       1.1   hannken 	}
    718       1.1   hannken 	r = bus_dmamap_load(sc->sc_dmat, vq->vq_dmamap,
    719  1.42.2.1   thorpej 			    vq->vq_vaddr, allocsize, NULL, BUS_DMA_WAITOK);
    720       1.1   hannken 	if (r != 0) {
    721       1.1   hannken 		aprint_error_dev(sc->sc_dev,
    722       1.1   hannken 				 "virtqueue %d for %s dmamap load failed, "
    723       1.1   hannken 				 "error code %d\n", index, name, r);
    724       1.1   hannken 		goto err;
    725       1.1   hannken 	}
    726       1.1   hannken 
    727       1.1   hannken 	/* remember addresses and offsets for later use */
    728       1.1   hannken 	vq->vq_owner = sc;
    729       1.1   hannken 	vq->vq_num = vq_size;
    730       1.1   hannken 	vq->vq_index = index;
    731       1.1   hannken 	vq->vq_desc = vq->vq_vaddr;
    732       1.1   hannken 	vq->vq_availoffset = sizeof(struct vring_desc)*vq_size;
    733       1.1   hannken 	vq->vq_avail = (void*)(((char*)vq->vq_desc) + vq->vq_availoffset);
    734  1.42.2.1   thorpej 	vq->vq_used_event = (uint16_t *) ((char *)vq->vq_avail +
    735  1.42.2.1   thorpej 		 offsetof(struct vring_avail, ring[vq->vq_num]));
    736       1.1   hannken 	vq->vq_usedoffset = allocsize1;
    737       1.1   hannken 	vq->vq_used = (void*)(((char*)vq->vq_desc) + vq->vq_usedoffset);
    738  1.42.2.1   thorpej 	vq->vq_avail_event = (uint16_t *)((char *)vq->vq_used +
    739  1.42.2.1   thorpej 		 offsetof(struct vring_used, ring[vq->vq_num]));
    740  1.42.2.1   thorpej 
    741       1.1   hannken 	if (allocsize3 > 0) {
    742       1.1   hannken 		vq->vq_indirectoffset = allocsize1 + allocsize2;
    743       1.1   hannken 		vq->vq_indirect = (void*)(((char*)vq->vq_desc)
    744       1.1   hannken 					  + vq->vq_indirectoffset);
    745       1.1   hannken 	}
    746       1.1   hannken 	vq->vq_bytesize = allocsize;
    747       1.1   hannken 	vq->vq_maxsegsize = maxsegsize;
    748       1.1   hannken 	vq->vq_maxnsegs = maxnsegs;
    749       1.1   hannken 
    750       1.1   hannken 	/* free slot management */
    751       1.1   hannken 	vq->vq_entries = kmem_zalloc(sizeof(struct vq_entry)*vq_size,
    752      1.38       chs 				     KM_SLEEP);
    753       1.1   hannken 	virtio_init_vq(sc, vq, false);
    754       1.1   hannken 
    755  1.42.2.1   thorpej 	/* set the vq address */
    756  1.42.2.1   thorpej 	sc->sc_ops->setup_queue(sc, index,
    757  1.42.2.1   thorpej 	    vq->vq_dmamap->dm_segs[0].ds_addr);
    758  1.42.2.1   thorpej 
    759       1.1   hannken 	aprint_verbose_dev(sc->sc_dev,
    760       1.1   hannken 			   "allocated %u byte for virtqueue %d for %s, "
    761       1.1   hannken 			   "size %d\n", allocsize, index, name, vq_size);
    762       1.1   hannken 	if (allocsize3 > 0)
    763       1.1   hannken 		aprint_verbose_dev(sc->sc_dev,
    764       1.1   hannken 				   "using %d byte (%d entries) "
    765       1.1   hannken 				   "indirect descriptors\n",
    766       1.1   hannken 				   allocsize3, maxnsegs * vq_size);
    767      1.22  jdolecek 
    768      1.22  jdolecek 	sc->sc_nvqs++;
    769      1.22  jdolecek 
    770       1.1   hannken 	return 0;
    771       1.1   hannken 
    772       1.1   hannken err:
    773      1.34  jakllsch 	sc->sc_ops->setup_queue(sc, index, 0);
    774       1.1   hannken 	if (vq->vq_dmamap)
    775       1.1   hannken 		bus_dmamap_destroy(sc->sc_dmat, vq->vq_dmamap);
    776       1.1   hannken 	if (vq->vq_vaddr)
    777       1.1   hannken 		bus_dmamem_unmap(sc->sc_dmat, vq->vq_vaddr, allocsize);
    778       1.1   hannken 	if (vq->vq_segs[0].ds_addr)
    779       1.1   hannken 		bus_dmamem_free(sc->sc_dmat, &vq->vq_segs[0], 1);
    780       1.1   hannken 	memset(vq, 0, sizeof(*vq));
    781       1.1   hannken 
    782       1.1   hannken 	return -1;
    783       1.1   hannken }
    784       1.1   hannken 
    785       1.1   hannken int
    786       1.1   hannken virtio_free_vq(struct virtio_softc *sc, struct virtqueue *vq)
    787       1.1   hannken {
    788       1.1   hannken 	struct vq_entry *qe;
    789       1.1   hannken 	int i = 0;
    790       1.1   hannken 
    791       1.1   hannken 	/* device must be already deactivated */
    792       1.1   hannken 	/* confirm the vq is empty */
    793       1.1   hannken 	SIMPLEQ_FOREACH(qe, &vq->vq_freelist, qe_list) {
    794       1.1   hannken 		i++;
    795       1.1   hannken 	}
    796       1.1   hannken 	if (i != vq->vq_num) {
    797       1.1   hannken 		printf("%s: freeing non-empty vq, index %d\n",
    798       1.1   hannken 		       device_xname(sc->sc_dev), vq->vq_index);
    799       1.1   hannken 		return EBUSY;
    800       1.1   hannken 	}
    801       1.1   hannken 
    802       1.1   hannken 	/* tell device that there's no virtqueue any longer */
    803      1.31  jakllsch 	sc->sc_ops->setup_queue(sc, vq->vq_index, 0);
    804       1.1   hannken 
    805      1.14  christos 	kmem_free(vq->vq_entries, sizeof(*vq->vq_entries) * vq->vq_num);
    806       1.1   hannken 	bus_dmamap_unload(sc->sc_dmat, vq->vq_dmamap);
    807       1.1   hannken 	bus_dmamap_destroy(sc->sc_dmat, vq->vq_dmamap);
    808       1.1   hannken 	bus_dmamem_unmap(sc->sc_dmat, vq->vq_vaddr, vq->vq_bytesize);
    809       1.1   hannken 	bus_dmamem_free(sc->sc_dmat, &vq->vq_segs[0], 1);
    810       1.1   hannken 	mutex_destroy(&vq->vq_freelist_lock);
    811       1.1   hannken 	mutex_destroy(&vq->vq_uring_lock);
    812       1.1   hannken 	mutex_destroy(&vq->vq_aring_lock);
    813       1.1   hannken 	memset(vq, 0, sizeof(*vq));
    814       1.1   hannken 
    815      1.22  jdolecek 	sc->sc_nvqs--;
    816      1.22  jdolecek 
    817       1.1   hannken 	return 0;
    818       1.1   hannken }
    819       1.1   hannken 
    820       1.1   hannken /*
    821       1.1   hannken  * Free descriptor management.
    822       1.1   hannken  */
    823       1.1   hannken static struct vq_entry *
    824       1.1   hannken vq_alloc_entry(struct virtqueue *vq)
    825       1.1   hannken {
    826       1.1   hannken 	struct vq_entry *qe;
    827       1.1   hannken 
    828       1.1   hannken 	mutex_enter(&vq->vq_freelist_lock);
    829       1.1   hannken 	if (SIMPLEQ_EMPTY(&vq->vq_freelist)) {
    830       1.1   hannken 		mutex_exit(&vq->vq_freelist_lock);
    831       1.1   hannken 		return NULL;
    832       1.1   hannken 	}
    833       1.1   hannken 	qe = SIMPLEQ_FIRST(&vq->vq_freelist);
    834       1.1   hannken 	SIMPLEQ_REMOVE_HEAD(&vq->vq_freelist, qe_list);
    835       1.1   hannken 	mutex_exit(&vq->vq_freelist_lock);
    836       1.1   hannken 
    837       1.1   hannken 	return qe;
    838       1.1   hannken }
    839       1.1   hannken 
    840       1.1   hannken static void
    841       1.1   hannken vq_free_entry(struct virtqueue *vq, struct vq_entry *qe)
    842       1.1   hannken {
    843       1.1   hannken 	mutex_enter(&vq->vq_freelist_lock);
    844       1.1   hannken 	SIMPLEQ_INSERT_TAIL(&vq->vq_freelist, qe, qe_list);
    845       1.1   hannken 	mutex_exit(&vq->vq_freelist_lock);
    846       1.1   hannken 
    847       1.1   hannken 	return;
    848       1.1   hannken }
    849       1.1   hannken 
    850       1.1   hannken /*
    851       1.1   hannken  * Enqueue several dmamaps as a single request.
    852       1.1   hannken  */
    853       1.1   hannken /*
    854       1.1   hannken  * Typical usage:
    855       1.1   hannken  *  <queue size> number of followings are stored in arrays
    856       1.1   hannken  *  - command blocks (in dmamem) should be pre-allocated and mapped
    857       1.1   hannken  *  - dmamaps for command blocks should be pre-allocated and loaded
    858       1.1   hannken  *  - dmamaps for payload should be pre-allocated
    859       1.1   hannken  *      r = virtio_enqueue_prep(sc, vq, &slot);		// allocate a slot
    860       1.1   hannken  *	if (r)		// currently 0 or EAGAIN
    861       1.1   hannken  *	  return r;
    862       1.1   hannken  *	r = bus_dmamap_load(dmat, dmamap_payload[slot], data, count, ..);
    863       1.1   hannken  *	if (r) {
    864       1.1   hannken  *	  virtio_enqueue_abort(sc, vq, slot);
    865       1.1   hannken  *	  return r;
    866       1.1   hannken  *	}
    867  1.42.2.1   thorpej  *	r = virtio_enqueue_reserve(sc, vq, slot,
    868       1.1   hannken  *				   dmamap_payload[slot]->dm_nsegs+1);
    869       1.1   hannken  *							// ^ +1 for command
    870       1.1   hannken  *	if (r) {	// currently 0 or EAGAIN
    871       1.1   hannken  *	  bus_dmamap_unload(dmat, dmamap_payload[slot]);
    872       1.1   hannken  *	  return r;					// do not call abort()
    873       1.1   hannken  *	}
    874       1.1   hannken  *	<setup and prepare commands>
    875       1.1   hannken  *	bus_dmamap_sync(dmat, dmamap_cmd[slot],... BUS_DMASYNC_PREWRITE);
    876       1.1   hannken  *	bus_dmamap_sync(dmat, dmamap_payload[slot],...);
    877       1.1   hannken  *	virtio_enqueue(sc, vq, slot, dmamap_cmd[slot], false);
    878       1.1   hannken  *	virtio_enqueue(sc, vq, slot, dmamap_payload[slot], iswrite);
    879       1.1   hannken  *	virtio_enqueue_commit(sc, vq, slot, true);
    880       1.1   hannken  */
    881       1.1   hannken 
    882       1.1   hannken /*
    883       1.1   hannken  * enqueue_prep: allocate a slot number
    884       1.1   hannken  */
    885       1.1   hannken int
    886       1.1   hannken virtio_enqueue_prep(struct virtio_softc *sc, struct virtqueue *vq, int *slotp)
    887       1.1   hannken {
    888       1.1   hannken 	struct vq_entry *qe1;
    889       1.1   hannken 
    890       1.1   hannken 	KASSERT(slotp != NULL);
    891       1.1   hannken 
    892       1.1   hannken 	qe1 = vq_alloc_entry(vq);
    893       1.1   hannken 	if (qe1 == NULL)
    894       1.1   hannken 		return EAGAIN;
    895       1.1   hannken 	/* next slot is not allocated yet */
    896       1.1   hannken 	qe1->qe_next = -1;
    897       1.1   hannken 	*slotp = qe1->qe_index;
    898       1.1   hannken 
    899       1.1   hannken 	return 0;
    900       1.1   hannken }
    901       1.1   hannken 
    902       1.1   hannken /*
    903       1.1   hannken  * enqueue_reserve: allocate remaining slots and build the descriptor chain.
    904       1.1   hannken  */
    905       1.1   hannken int
    906       1.1   hannken virtio_enqueue_reserve(struct virtio_softc *sc, struct virtqueue *vq,
    907       1.1   hannken 		       int slot, int nsegs)
    908       1.1   hannken {
    909       1.1   hannken 	int indirect;
    910       1.1   hannken 	struct vq_entry *qe1 = &vq->vq_entries[slot];
    911       1.1   hannken 
    912       1.1   hannken 	KASSERT(qe1->qe_next == -1);
    913       1.1   hannken 	KASSERT(1 <= nsegs && nsegs <= vq->vq_num);
    914       1.1   hannken 
    915       1.1   hannken 	if ((vq->vq_indirect != NULL) &&
    916       1.1   hannken 	    (nsegs >= MINSEG_INDIRECT) &&
    917       1.1   hannken 	    (nsegs <= vq->vq_maxnsegs))
    918       1.1   hannken 		indirect = 1;
    919       1.1   hannken 	else
    920       1.1   hannken 		indirect = 0;
    921       1.1   hannken 	qe1->qe_indirect = indirect;
    922       1.1   hannken 
    923       1.1   hannken 	if (indirect) {
    924       1.1   hannken 		struct vring_desc *vd;
    925  1.42.2.1   thorpej 		uint64_t addr;
    926       1.1   hannken 		int i;
    927       1.1   hannken 
    928       1.1   hannken 		vd = &vq->vq_desc[qe1->qe_index];
    929  1.42.2.1   thorpej 		addr = vq->vq_dmamap->dm_segs[0].ds_addr
    930       1.1   hannken 			+ vq->vq_indirectoffset;
    931  1.42.2.1   thorpej 		addr += sizeof(struct vring_desc)
    932       1.1   hannken 			* vq->vq_maxnsegs * qe1->qe_index;
    933  1.42.2.1   thorpej 		vd->addr  = virtio_rw64(sc, addr);
    934  1.42.2.1   thorpej 		vd->len   = virtio_rw32(sc, sizeof(struct vring_desc) * nsegs);
    935  1.42.2.1   thorpej 		vd->flags = virtio_rw16(sc, VRING_DESC_F_INDIRECT);
    936       1.1   hannken 
    937       1.1   hannken 		vd = vq->vq_indirect;
    938       1.1   hannken 		vd += vq->vq_maxnsegs * qe1->qe_index;
    939       1.1   hannken 		qe1->qe_desc_base = vd;
    940       1.1   hannken 
    941       1.1   hannken 		for (i = 0; i < nsegs-1; i++) {
    942  1.42.2.1   thorpej 			vd[i].flags = virtio_rw16(sc, VRING_DESC_F_NEXT);
    943       1.1   hannken 		}
    944  1.42.2.1   thorpej 		vd[i].flags  = virtio_rw16(sc, 0);
    945       1.1   hannken 		qe1->qe_next = 0;
    946       1.1   hannken 
    947       1.1   hannken 		return 0;
    948       1.1   hannken 	} else {
    949       1.1   hannken 		struct vring_desc *vd;
    950       1.1   hannken 		struct vq_entry *qe;
    951       1.1   hannken 		int i, s;
    952       1.1   hannken 
    953       1.1   hannken 		vd = &vq->vq_desc[0];
    954       1.1   hannken 		qe1->qe_desc_base = vd;
    955       1.1   hannken 		qe1->qe_next = qe1->qe_index;
    956       1.1   hannken 		s = slot;
    957       1.1   hannken 		for (i = 0; i < nsegs - 1; i++) {
    958       1.1   hannken 			qe = vq_alloc_entry(vq);
    959       1.1   hannken 			if (qe == NULL) {
    960  1.42.2.1   thorpej 				vd[s].flags = virtio_rw16(sc, 0);
    961       1.1   hannken 				virtio_enqueue_abort(sc, vq, slot);
    962       1.1   hannken 				return EAGAIN;
    963       1.1   hannken 			}
    964  1.42.2.1   thorpej 			vd[s].flags = virtio_rw16(sc, VRING_DESC_F_NEXT);
    965  1.42.2.1   thorpej 			vd[s].next  = virtio_rw16(sc, qe->qe_index);
    966       1.1   hannken 			s = qe->qe_index;
    967       1.1   hannken 		}
    968  1.42.2.1   thorpej 		vd[s].flags = virtio_rw16(sc, 0);
    969       1.1   hannken 
    970       1.1   hannken 		return 0;
    971       1.1   hannken 	}
    972       1.1   hannken }
    973       1.1   hannken 
    974       1.1   hannken /*
    975       1.1   hannken  * enqueue: enqueue a single dmamap.
    976       1.1   hannken  */
    977       1.1   hannken int
    978       1.1   hannken virtio_enqueue(struct virtio_softc *sc, struct virtqueue *vq, int slot,
    979       1.1   hannken 	       bus_dmamap_t dmamap, bool write)
    980       1.1   hannken {
    981       1.1   hannken 	struct vq_entry *qe1 = &vq->vq_entries[slot];
    982       1.1   hannken 	struct vring_desc *vd = qe1->qe_desc_base;
    983       1.1   hannken 	int i;
    984       1.1   hannken 	int s = qe1->qe_next;
    985       1.1   hannken 
    986       1.1   hannken 	KASSERT(s >= 0);
    987       1.1   hannken 	KASSERT(dmamap->dm_nsegs > 0);
    988       1.1   hannken 
    989       1.1   hannken 	for (i = 0; i < dmamap->dm_nsegs; i++) {
    990  1.42.2.1   thorpej 		vd[s].addr = virtio_rw64(sc, dmamap->dm_segs[i].ds_addr);
    991  1.42.2.1   thorpej 		vd[s].len  = virtio_rw32(sc, dmamap->dm_segs[i].ds_len);
    992       1.1   hannken 		if (!write)
    993  1.42.2.1   thorpej 			vd[s].flags |= virtio_rw16(sc, VRING_DESC_F_WRITE);
    994  1.42.2.1   thorpej 		s = virtio_rw16(sc, vd[s].next);
    995       1.1   hannken 	}
    996       1.1   hannken 	qe1->qe_next = s;
    997       1.1   hannken 
    998       1.1   hannken 	return 0;
    999       1.1   hannken }
   1000       1.1   hannken 
   1001       1.1   hannken int
   1002       1.1   hannken virtio_enqueue_p(struct virtio_softc *sc, struct virtqueue *vq, int slot,
   1003       1.1   hannken 		 bus_dmamap_t dmamap, bus_addr_t start, bus_size_t len,
   1004       1.1   hannken 		 bool write)
   1005       1.1   hannken {
   1006       1.1   hannken 	struct vq_entry *qe1 = &vq->vq_entries[slot];
   1007       1.1   hannken 	struct vring_desc *vd = qe1->qe_desc_base;
   1008       1.1   hannken 	int s = qe1->qe_next;
   1009       1.1   hannken 
   1010       1.1   hannken 	KASSERT(s >= 0);
   1011       1.1   hannken 	KASSERT(dmamap->dm_nsegs == 1); /* XXX */
   1012       1.1   hannken 	KASSERT((dmamap->dm_segs[0].ds_len > start) &&
   1013       1.1   hannken 		(dmamap->dm_segs[0].ds_len >= start + len));
   1014       1.1   hannken 
   1015  1.42.2.1   thorpej 	vd[s].addr = virtio_rw64(sc, dmamap->dm_segs[0].ds_addr + start);
   1016  1.42.2.1   thorpej 	vd[s].len  = virtio_rw32(sc, len);
   1017       1.1   hannken 	if (!write)
   1018  1.42.2.1   thorpej 		vd[s].flags |= virtio_rw16(sc, VRING_DESC_F_WRITE);
   1019  1.42.2.1   thorpej 	qe1->qe_next = virtio_rw16(sc, vd[s].next);
   1020       1.1   hannken 
   1021       1.1   hannken 	return 0;
   1022       1.1   hannken }
   1023       1.1   hannken 
   1024       1.1   hannken /*
   1025       1.1   hannken  * enqueue_commit: add it to the aring.
   1026       1.1   hannken  */
   1027       1.1   hannken int
   1028       1.1   hannken virtio_enqueue_commit(struct virtio_softc *sc, struct virtqueue *vq, int slot,
   1029       1.1   hannken 		      bool notifynow)
   1030       1.1   hannken {
   1031       1.1   hannken 	struct vq_entry *qe1;
   1032       1.1   hannken 
   1033       1.1   hannken 	if (slot < 0) {
   1034       1.1   hannken 		mutex_enter(&vq->vq_aring_lock);
   1035       1.1   hannken 		goto notify;
   1036       1.1   hannken 	}
   1037       1.1   hannken 	vq_sync_descs(sc, vq, BUS_DMASYNC_PREWRITE);
   1038       1.1   hannken 	qe1 = &vq->vq_entries[slot];
   1039       1.1   hannken 	if (qe1->qe_indirect)
   1040       1.1   hannken 		vq_sync_indirect(sc, vq, slot, BUS_DMASYNC_PREWRITE);
   1041       1.1   hannken 	mutex_enter(&vq->vq_aring_lock);
   1042  1.42.2.1   thorpej 	vq->vq_avail->ring[(vq->vq_avail_idx++) % vq->vq_num] =
   1043  1.42.2.1   thorpej 		virtio_rw16(sc, slot);
   1044       1.1   hannken 
   1045       1.1   hannken notify:
   1046       1.1   hannken 	if (notifynow) {
   1047  1.42.2.1   thorpej 		uint16_t o, n, t;
   1048  1.42.2.1   thorpej 		uint16_t flags;
   1049  1.42.2.1   thorpej 		o = virtio_rw16(sc, vq->vq_avail->idx);
   1050  1.42.2.1   thorpej 		n = vq->vq_avail_idx;
   1051  1.42.2.1   thorpej 
   1052  1.42.2.1   thorpej 		/* publish avail idx */
   1053       1.1   hannken 		membar_producer();
   1054  1.42.2.1   thorpej 		vq->vq_avail->idx = virtio_rw16(sc, vq->vq_avail_idx);
   1055       1.1   hannken 		vq_sync_aring(sc, vq, BUS_DMASYNC_PREWRITE);
   1056       1.1   hannken 		vq->vq_queued++;
   1057  1.42.2.1   thorpej 
   1058       1.1   hannken 		membar_consumer();
   1059  1.42.2.1   thorpej 		vq_sync_uring(sc, vq, BUS_DMASYNC_PREREAD);
   1060  1.42.2.1   thorpej 		if (sc->sc_active_features & VIRTIO_F_RING_EVENT_IDX) {
   1061  1.42.2.1   thorpej 			t = virtio_rw16(sc, *vq->vq_avail_event) + 1;
   1062  1.42.2.1   thorpej 			if ((uint16_t) (n - t) < (uint16_t) (n - o))
   1063  1.42.2.1   thorpej 				sc->sc_ops->kick(sc, vq->vq_index);
   1064  1.42.2.1   thorpej 		} else {
   1065  1.42.2.1   thorpej 			flags = virtio_rw16(sc, vq->vq_used->flags);
   1066  1.42.2.1   thorpej 			if (!(flags & VRING_USED_F_NO_NOTIFY))
   1067  1.42.2.1   thorpej 				sc->sc_ops->kick(sc, vq->vq_index);
   1068  1.42.2.1   thorpej 		}
   1069  1.42.2.1   thorpej 		vq_sync_uring(sc, vq, BUS_DMASYNC_POSTREAD);
   1070  1.42.2.1   thorpej 		vq_sync_aring(sc, vq, BUS_DMASYNC_POSTWRITE);
   1071       1.1   hannken 	}
   1072       1.1   hannken 	mutex_exit(&vq->vq_aring_lock);
   1073       1.1   hannken 
   1074       1.1   hannken 	return 0;
   1075       1.1   hannken }
   1076       1.1   hannken 
   1077       1.1   hannken /*
   1078       1.1   hannken  * enqueue_abort: rollback.
   1079       1.1   hannken  */
   1080       1.1   hannken int
   1081       1.1   hannken virtio_enqueue_abort(struct virtio_softc *sc, struct virtqueue *vq, int slot)
   1082       1.1   hannken {
   1083       1.1   hannken 	struct vq_entry *qe = &vq->vq_entries[slot];
   1084       1.1   hannken 	struct vring_desc *vd;
   1085       1.1   hannken 	int s;
   1086       1.1   hannken 
   1087       1.1   hannken 	if (qe->qe_next < 0) {
   1088       1.1   hannken 		vq_free_entry(vq, qe);
   1089       1.1   hannken 		return 0;
   1090       1.1   hannken 	}
   1091       1.1   hannken 
   1092       1.1   hannken 	s = slot;
   1093       1.1   hannken 	vd = &vq->vq_desc[0];
   1094  1.42.2.1   thorpej 	while (virtio_rw16(sc, vd[s].flags) & VRING_DESC_F_NEXT) {
   1095  1.42.2.1   thorpej 		s = virtio_rw16(sc, vd[s].next);
   1096       1.1   hannken 		vq_free_entry(vq, qe);
   1097       1.1   hannken 		qe = &vq->vq_entries[s];
   1098       1.1   hannken 	}
   1099       1.1   hannken 	vq_free_entry(vq, qe);
   1100       1.1   hannken 	return 0;
   1101       1.1   hannken }
   1102       1.1   hannken 
   1103       1.1   hannken /*
   1104       1.1   hannken  * Dequeue a request.
   1105       1.1   hannken  */
   1106       1.1   hannken /*
   1107       1.1   hannken  * dequeue: dequeue a request from uring; dmamap_sync for uring is
   1108       1.1   hannken  *	    already done in the interrupt handler.
   1109       1.1   hannken  */
   1110       1.1   hannken int
   1111       1.1   hannken virtio_dequeue(struct virtio_softc *sc, struct virtqueue *vq,
   1112       1.1   hannken 	       int *slotp, int *lenp)
   1113       1.1   hannken {
   1114       1.1   hannken 	uint16_t slot, usedidx;
   1115       1.1   hannken 	struct vq_entry *qe;
   1116       1.1   hannken 
   1117  1.42.2.1   thorpej 	if (vq->vq_used_idx == virtio_rw16(sc, vq->vq_used->idx))
   1118       1.1   hannken 		return ENOENT;
   1119       1.1   hannken 	mutex_enter(&vq->vq_uring_lock);
   1120       1.1   hannken 	usedidx = vq->vq_used_idx++;
   1121       1.1   hannken 	mutex_exit(&vq->vq_uring_lock);
   1122       1.1   hannken 	usedidx %= vq->vq_num;
   1123  1.42.2.1   thorpej 	slot = virtio_rw32(sc, vq->vq_used->ring[usedidx].id);
   1124       1.1   hannken 	qe = &vq->vq_entries[slot];
   1125       1.1   hannken 
   1126       1.1   hannken 	if (qe->qe_indirect)
   1127       1.1   hannken 		vq_sync_indirect(sc, vq, slot, BUS_DMASYNC_POSTWRITE);
   1128       1.1   hannken 
   1129       1.1   hannken 	if (slotp)
   1130       1.1   hannken 		*slotp = slot;
   1131       1.1   hannken 	if (lenp)
   1132  1.42.2.1   thorpej 		*lenp = virtio_rw32(sc, vq->vq_used->ring[usedidx].len);
   1133       1.1   hannken 
   1134       1.1   hannken 	return 0;
   1135       1.1   hannken }
   1136       1.1   hannken 
   1137       1.1   hannken /*
   1138       1.1   hannken  * dequeue_commit: complete dequeue; the slot is recycled for future use.
   1139       1.1   hannken  *                 if you forget to call this the slot will be leaked.
   1140       1.1   hannken  */
   1141       1.1   hannken int
   1142       1.1   hannken virtio_dequeue_commit(struct virtio_softc *sc, struct virtqueue *vq, int slot)
   1143       1.1   hannken {
   1144       1.1   hannken 	struct vq_entry *qe = &vq->vq_entries[slot];
   1145       1.1   hannken 	struct vring_desc *vd = &vq->vq_desc[0];
   1146       1.1   hannken 	int s = slot;
   1147       1.1   hannken 
   1148  1.42.2.1   thorpej 	while (virtio_rw16(sc, vd[s].flags) & VRING_DESC_F_NEXT) {
   1149  1.42.2.1   thorpej 		s = virtio_rw16(sc, vd[s].next);
   1150       1.1   hannken 		vq_free_entry(vq, qe);
   1151       1.1   hannken 		qe = &vq->vq_entries[s];
   1152       1.1   hannken 	}
   1153       1.1   hannken 	vq_free_entry(vq, qe);
   1154       1.1   hannken 
   1155       1.1   hannken 	return 0;
   1156       1.1   hannken }
   1157      1.18  pgoyette 
   1158      1.22  jdolecek /*
   1159      1.22  jdolecek  * Attach a child, fill all the members.
   1160      1.22  jdolecek  */
   1161      1.22  jdolecek void
   1162  1.42.2.1   thorpej virtio_child_attach_start(struct virtio_softc *sc, device_t child, int ipl,
   1163      1.22  jdolecek 		    struct virtqueue *vqs,
   1164      1.22  jdolecek 		    virtio_callback config_change,
   1165      1.22  jdolecek 		    virtio_callback intr_hand,
   1166      1.22  jdolecek 		    int req_flags, int req_features, const char *feat_bits)
   1167      1.22  jdolecek {
   1168  1.42.2.1   thorpej 	char buf[1024];
   1169      1.22  jdolecek 
   1170      1.22  jdolecek 	sc->sc_child = child;
   1171      1.22  jdolecek 	sc->sc_ipl = ipl;
   1172      1.22  jdolecek 	sc->sc_vqs = vqs;
   1173      1.22  jdolecek 	sc->sc_config_change = config_change;
   1174      1.22  jdolecek 	sc->sc_intrhand = intr_hand;
   1175      1.22  jdolecek 	sc->sc_flags = req_flags;
   1176      1.22  jdolecek 
   1177  1.42.2.1   thorpej 	virtio_negotiate_features(sc, req_features);
   1178  1.42.2.1   thorpej 	snprintb(buf, sizeof(buf), feat_bits, sc->sc_active_features);
   1179  1.42.2.1   thorpej 	aprint_normal(": features: %s\n", buf);
   1180      1.22  jdolecek 	aprint_naive("\n");
   1181      1.22  jdolecek }
   1182      1.22  jdolecek 
   1183      1.37  yamaguch void
   1184      1.37  yamaguch virtio_child_attach_set_vqs(struct virtio_softc *sc,
   1185      1.37  yamaguch     struct virtqueue *vqs, int nvq_pairs)
   1186      1.37  yamaguch {
   1187      1.39  yamaguch 
   1188      1.39  yamaguch 	KASSERT(nvq_pairs == 1 ||
   1189  1.42.2.1   thorpej 	    (sc->sc_flags & VIRTIO_F_INTR_SOFTINT) == 0);
   1190      1.37  yamaguch 	if (nvq_pairs > 1)
   1191      1.37  yamaguch 		sc->sc_child_mq = true;
   1192      1.37  yamaguch 
   1193      1.37  yamaguch 	sc->sc_vqs = vqs;
   1194      1.37  yamaguch }
   1195      1.37  yamaguch 
   1196      1.22  jdolecek int
   1197      1.22  jdolecek virtio_child_attach_finish(struct virtio_softc *sc)
   1198      1.22  jdolecek {
   1199      1.22  jdolecek 	int r;
   1200      1.22  jdolecek 
   1201  1.42.2.1   thorpej 	sc->sc_finished_called = true;
   1202      1.31  jakllsch 	r = sc->sc_ops->setup_interrupts(sc);
   1203      1.22  jdolecek 	if (r != 0) {
   1204      1.22  jdolecek 		aprint_error_dev(sc->sc_dev, "failed to setup interrupts\n");
   1205      1.31  jakllsch 		goto fail;
   1206      1.31  jakllsch 	}
   1207      1.31  jakllsch 
   1208      1.31  jakllsch 	KASSERT(sc->sc_soft_ih == NULL);
   1209  1.42.2.1   thorpej 	if (sc->sc_flags & VIRTIO_F_INTR_SOFTINT) {
   1210  1.42.2.1   thorpej 		u_int flags = SOFTINT_NET;
   1211  1.42.2.1   thorpej 		if (sc->sc_flags & VIRTIO_F_INTR_MPSAFE)
   1212      1.31  jakllsch 			flags |= SOFTINT_MPSAFE;
   1213      1.31  jakllsch 
   1214      1.31  jakllsch 		sc->sc_soft_ih = softint_establish(flags, virtio_soft_intr, sc);
   1215      1.31  jakllsch 		if (sc->sc_soft_ih == NULL) {
   1216      1.31  jakllsch 			sc->sc_ops->free_interrupts(sc);
   1217      1.31  jakllsch 			aprint_error_dev(sc->sc_dev,
   1218      1.31  jakllsch 			    "failed to establish soft interrupt\n");
   1219      1.31  jakllsch 			goto fail;
   1220      1.31  jakllsch 		}
   1221      1.22  jdolecek 	}
   1222      1.22  jdolecek 
   1223      1.22  jdolecek 	virtio_set_status(sc, VIRTIO_CONFIG_DEVICE_STATUS_DRIVER_OK);
   1224      1.31  jakllsch 	return 0;
   1225      1.22  jdolecek 
   1226      1.31  jakllsch fail:
   1227      1.37  yamaguch 	if (sc->sc_soft_ih) {
   1228      1.37  yamaguch 		softint_disestablish(sc->sc_soft_ih);
   1229      1.37  yamaguch 		sc->sc_soft_ih = NULL;
   1230      1.37  yamaguch 	}
   1231      1.37  yamaguch 
   1232      1.31  jakllsch 	virtio_set_status(sc, VIRTIO_CONFIG_DEVICE_STATUS_FAILED);
   1233      1.31  jakllsch 	return 1;
   1234      1.22  jdolecek }
   1235      1.22  jdolecek 
   1236      1.22  jdolecek void
   1237      1.22  jdolecek virtio_child_detach(struct virtio_softc *sc)
   1238      1.22  jdolecek {
   1239      1.22  jdolecek 	sc->sc_child = NULL;
   1240      1.22  jdolecek 	sc->sc_vqs = NULL;
   1241      1.22  jdolecek 
   1242      1.22  jdolecek 	virtio_device_reset(sc);
   1243      1.22  jdolecek 
   1244      1.31  jakllsch 	sc->sc_ops->free_interrupts(sc);
   1245      1.31  jakllsch 
   1246      1.31  jakllsch 	if (sc->sc_soft_ih) {
   1247      1.31  jakllsch 		softint_disestablish(sc->sc_soft_ih);
   1248      1.31  jakllsch 		sc->sc_soft_ih = NULL;
   1249      1.31  jakllsch 	}
   1250      1.22  jdolecek }
   1251      1.22  jdolecek 
   1252      1.22  jdolecek void
   1253      1.22  jdolecek virtio_child_attach_failed(struct virtio_softc *sc)
   1254      1.22  jdolecek {
   1255      1.22  jdolecek 	virtio_child_detach(sc);
   1256      1.22  jdolecek 
   1257      1.22  jdolecek 	virtio_set_status(sc, VIRTIO_CONFIG_DEVICE_STATUS_FAILED);
   1258      1.22  jdolecek 
   1259      1.22  jdolecek 	sc->sc_child = VIRTIO_CHILD_FAILED;
   1260      1.22  jdolecek }
   1261      1.22  jdolecek 
   1262      1.22  jdolecek bus_dma_tag_t
   1263      1.22  jdolecek virtio_dmat(struct virtio_softc *sc)
   1264      1.22  jdolecek {
   1265      1.22  jdolecek 	return sc->sc_dmat;
   1266      1.22  jdolecek }
   1267      1.22  jdolecek 
   1268      1.22  jdolecek device_t
   1269      1.22  jdolecek virtio_child(struct virtio_softc *sc)
   1270      1.22  jdolecek {
   1271      1.22  jdolecek 	return sc->sc_child;
   1272      1.22  jdolecek }
   1273      1.22  jdolecek 
   1274      1.22  jdolecek int
   1275      1.22  jdolecek virtio_intrhand(struct virtio_softc *sc)
   1276      1.22  jdolecek {
   1277      1.22  jdolecek 	return (sc->sc_intrhand)(sc);
   1278      1.22  jdolecek }
   1279      1.22  jdolecek 
   1280  1.42.2.1   thorpej uint64_t
   1281      1.22  jdolecek virtio_features(struct virtio_softc *sc)
   1282      1.22  jdolecek {
   1283  1.42.2.1   thorpej 	return sc->sc_active_features;
   1284      1.22  jdolecek }
   1285      1.22  jdolecek 
   1286      1.35  jakllsch int
   1287  1.42.2.1   thorpej virtio_attach_failed(struct virtio_softc *sc)
   1288      1.35  jakllsch {
   1289  1.42.2.1   thorpej 	device_t self = sc->sc_dev;
   1290      1.35  jakllsch 
   1291  1.42.2.1   thorpej 	/* no error if its not connected, but its failed */
   1292  1.42.2.1   thorpej 	if (sc->sc_childdevid == 0)
   1293  1.42.2.1   thorpej 		return 1;
   1294      1.36  jmcneill 
   1295  1.42.2.1   thorpej 	if (sc->sc_child == NULL) {
   1296  1.42.2.1   thorpej 		aprint_error_dev(self,
   1297  1.42.2.1   thorpej 			"no matching child driver; not configured\n");
   1298  1.42.2.1   thorpej 		return 1;
   1299  1.42.2.1   thorpej 	}
   1300  1.42.2.1   thorpej 
   1301  1.42.2.1   thorpej 	if (sc->sc_child == VIRTIO_CHILD_FAILED) {
   1302  1.42.2.1   thorpej 		aprint_error_dev(self, "virtio configuration failed\n");
   1303  1.42.2.1   thorpej 		return 1;
   1304  1.42.2.1   thorpej 	}
   1305  1.42.2.1   thorpej 
   1306  1.42.2.1   thorpej 	/* sanity check */
   1307  1.42.2.1   thorpej 	if (!sc->sc_finished_called) {
   1308  1.42.2.1   thorpej 		aprint_error_dev(self, "virtio internal error, child driver "
   1309  1.42.2.1   thorpej 			"signaled OK but didn't initialize interrupts\n");
   1310  1.42.2.1   thorpej 		return 1;
   1311  1.42.2.1   thorpej 	}
   1312  1.42.2.1   thorpej 
   1313  1.42.2.1   thorpej 	return 0;
   1314  1.42.2.1   thorpej }
   1315      1.35  jakllsch 
   1316  1.42.2.1   thorpej void
   1317  1.42.2.1   thorpej virtio_print_device_type(device_t self, int id, int revision)
   1318  1.42.2.1   thorpej {
   1319  1.42.2.1   thorpej 	aprint_normal_dev(self, "%s device (rev. 0x%02x)\n",
   1320  1.42.2.1   thorpej 		  (id < NDEVNAMES ? virtio_device_name[id] : "Unknown"),
   1321  1.42.2.1   thorpej 		  revision);
   1322      1.35  jakllsch }
   1323      1.35  jakllsch 
   1324  1.42.2.1   thorpej 
   1325      1.32  jakllsch MODULE(MODULE_CLASS_DRIVER, virtio, NULL);
   1326  1.42.2.1   thorpej 
   1327      1.18  pgoyette #ifdef _MODULE
   1328      1.18  pgoyette #include "ioconf.c"
   1329      1.18  pgoyette #endif
   1330  1.42.2.1   thorpej 
   1331      1.18  pgoyette static int
   1332      1.18  pgoyette virtio_modcmd(modcmd_t cmd, void *opaque)
   1333      1.18  pgoyette {
   1334      1.18  pgoyette 	int error = 0;
   1335  1.42.2.1   thorpej 
   1336      1.18  pgoyette #ifdef _MODULE
   1337      1.18  pgoyette 	switch (cmd) {
   1338      1.18  pgoyette 	case MODULE_CMD_INIT:
   1339  1.42.2.1   thorpej 		error = config_init_component(cfdriver_ioconf_virtio,
   1340  1.42.2.1   thorpej 		    cfattach_ioconf_virtio, cfdata_ioconf_virtio);
   1341      1.18  pgoyette 		break;
   1342      1.18  pgoyette 	case MODULE_CMD_FINI:
   1343  1.42.2.1   thorpej 		error = config_fini_component(cfdriver_ioconf_virtio,
   1344      1.18  pgoyette 		    cfattach_ioconf_virtio, cfdata_ioconf_virtio);
   1345      1.18  pgoyette 		break;
   1346      1.18  pgoyette 	default:
   1347      1.18  pgoyette 		error = ENOTTY;
   1348      1.18  pgoyette 		break;
   1349      1.18  pgoyette 	}
   1350      1.18  pgoyette #endif
   1351  1.42.2.1   thorpej 
   1352  1.42.2.1   thorpej 	return error;
   1353      1.18  pgoyette }
   1354