Home | History | Annotate | Line # | Download | only in pci
vioscsi.c revision 1.1
      1  1.1  christos /*	$OpenBSD: vioscsi.c,v 1.3 2015/03/14 03:38:49 jsg Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*
      4  1.1  christos  * Copyright (c) 2013 Google Inc.
      5  1.1  christos  *
      6  1.1  christos  * Permission to use, copy, modify, and distribute this software for any
      7  1.1  christos  * purpose with or without fee is hereby granted, provided that the above
      8  1.1  christos  * copyright notice and this permission notice appear in all copies.
      9  1.1  christos  *
     10  1.1  christos  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     11  1.1  christos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     12  1.1  christos  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     13  1.1  christos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     14  1.1  christos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     15  1.1  christos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     16  1.1  christos  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     17  1.1  christos  */
     18  1.1  christos 
     19  1.1  christos #include <sys/cdefs.h>
     20  1.1  christos __KERNEL_RCSID(0, "$NetBSD: vioscsi.c,v 1.1 2015/10/29 01:56:12 christos Exp $");
     21  1.1  christos 
     22  1.1  christos #include <sys/param.h>
     23  1.1  christos #include <sys/systm.h>
     24  1.1  christos #include <sys/device.h>
     25  1.1  christos #include <sys/bus.h>
     26  1.1  christos #include <sys/buf.h>
     27  1.1  christos 
     28  1.1  christos #include <dev/pci/pcidevs.h>
     29  1.1  christos #include <dev/pci/pcireg.h>
     30  1.1  christos #include <dev/pci/pcivar.h>
     31  1.1  christos 
     32  1.1  christos #include <dev/pci/vioscsireg.h>
     33  1.1  christos #include <dev/pci/virtiovar.h>
     34  1.1  christos 
     35  1.1  christos #include <dev/scsipi/scsi_all.h>
     36  1.1  christos #include <dev/scsipi/scsiconf.h>
     37  1.1  christos 
     38  1.1  christos #define VIOSCSI_DEBUG
     39  1.1  christos 
     40  1.1  christos #ifdef VIOSCSI_DEBUG
     41  1.1  christos static int vioscsi_debug = 1;
     42  1.1  christos #define DPRINTF(f) do { if (vioscsi_debug) printf f; } while (/*CONSTCOND*/0)
     43  1.1  christos #else
     44  1.1  christos #define DPRINTF(f) ((void)0)
     45  1.1  christos #endif
     46  1.1  christos 
     47  1.1  christos struct vioscsi_req {
     48  1.1  christos 	struct virtio_scsi_req_hdr	 vr_req;
     49  1.1  christos 	struct virtio_scsi_res_hdr	 vr_res;
     50  1.1  christos 	struct scsipi_xfer		*vr_xs;
     51  1.1  christos 	bus_dmamap_t			 vr_control;
     52  1.1  christos 	bus_dmamap_t			 vr_data;
     53  1.1  christos };
     54  1.1  christos 
     55  1.1  christos struct vioscsi_softc {
     56  1.1  christos 	device_t 		 sc_dev;
     57  1.1  christos 	struct scsipi_adapter	 sc_adapter;
     58  1.1  christos 	struct scsipi_channel 	 sc_channel;
     59  1.1  christos 
     60  1.1  christos 	struct virtqueue	 sc_vqs[3];
     61  1.1  christos 	struct vioscsi_req	*sc_reqs;
     62  1.1  christos 	bus_dma_segment_t        sc_reqs_segs[1];
     63  1.1  christos 
     64  1.1  christos 	u_int32_t		 sc_seg_max;
     65  1.1  christos };
     66  1.1  christos 
     67  1.1  christos static int	 vioscsi_match(device_t, cfdata_t, void *);
     68  1.1  christos static void	 vioscsi_attach(device_t, device_t, void *);
     69  1.1  christos 
     70  1.1  christos static int	 vioscsi_alloc_reqs(struct vioscsi_softc *,
     71  1.1  christos     struct virtio_softc *, int, uint32_t);
     72  1.1  christos static void	 vioscsi_scsipi_request(struct scsipi_channel *,
     73  1.1  christos     scsipi_adapter_req_t, void *);
     74  1.1  christos static int	 vioscsi_vq_done(struct virtqueue *);
     75  1.1  christos static void	 vioscsi_req_done(struct vioscsi_softc *, struct virtio_softc *,
     76  1.1  christos     struct vioscsi_req *);
     77  1.1  christos static struct vioscsi_req *vioscsi_req_get(struct vioscsi_softc *);
     78  1.1  christos static void	 vioscsi_req_put(struct vioscsi_softc *, struct vioscsi_req *);
     79  1.1  christos 
     80  1.1  christos static const char *const vioscsi_vq_names[] = {
     81  1.1  christos 	"control",
     82  1.1  christos 	"event",
     83  1.1  christos 	"request",
     84  1.1  christos };
     85  1.1  christos 
     86  1.1  christos CFATTACH_DECL_NEW(vioscsi, sizeof(struct vioscsi_softc),
     87  1.1  christos     vioscsi_match, vioscsi_attach, NULL, NULL);
     88  1.1  christos 
     89  1.1  christos static int
     90  1.1  christos vioscsi_match(device_t parent, cfdata_t match, void *aux)
     91  1.1  christos {
     92  1.1  christos 	struct virtio_softc *va = aux;
     93  1.1  christos 
     94  1.1  christos 	if (va->sc_childdevid == PCI_PRODUCT_VIRTIO_SCSI)
     95  1.1  christos 		return 1;
     96  1.1  christos 	return 0;
     97  1.1  christos }
     98  1.1  christos 
     99  1.1  christos static void
    100  1.1  christos vioscsi_attach(device_t parent, device_t self, void *aux)
    101  1.1  christos {
    102  1.1  christos 	struct vioscsi_softc *sc = device_private(self);
    103  1.1  christos 	struct virtio_softc *vsc = device_private(parent);
    104  1.1  christos 	struct scsipi_adapter *adapt = &sc->sc_adapter;
    105  1.1  christos 	struct scsipi_channel *chan = &sc->sc_channel;
    106  1.1  christos 	uint32_t features;
    107  1.1  christos 	char buf[256];
    108  1.1  christos 	int rv;
    109  1.1  christos 
    110  1.1  christos 	if (vsc->sc_child != NULL) {
    111  1.1  christos 		aprint_error(": parent %s already has a child\n",
    112  1.1  christos 		    device_xname(parent));
    113  1.1  christos 		return;
    114  1.1  christos 	}
    115  1.1  christos 
    116  1.1  christos 	sc->sc_dev = self;
    117  1.1  christos 
    118  1.1  christos 	vsc->sc_child = self;
    119  1.1  christos 	vsc->sc_ipl = IPL_BIO;
    120  1.1  christos 	vsc->sc_vqs = sc->sc_vqs;
    121  1.1  christos 	vsc->sc_nvqs = __arraycount(sc->sc_vqs);
    122  1.1  christos 	vsc->sc_config_change = NULL;
    123  1.1  christos 	vsc->sc_intrhand = virtio_vq_intr;
    124  1.1  christos 	vsc->sc_flags = 0;
    125  1.1  christos 
    126  1.1  christos 	features = virtio_negotiate_features(vsc, 0);
    127  1.1  christos 	snprintb(buf, sizeof(buf), VIRTIO_COMMON_FLAG_BITS, features);
    128  1.1  christos 	aprint_normal(": Features: %s\n", buf);
    129  1.1  christos 	aprint_naive("\n");
    130  1.1  christos 
    131  1.1  christos 	uint32_t cmd_per_lun = virtio_read_device_config_4(vsc,
    132  1.1  christos 	    VIRTIO_SCSI_CONFIG_CMD_PER_LUN);
    133  1.1  christos 
    134  1.1  christos 	uint32_t seg_max = virtio_read_device_config_4(vsc,
    135  1.1  christos 	    VIRTIO_SCSI_CONFIG_SEG_MAX);
    136  1.1  christos 
    137  1.1  christos 	uint16_t max_target = virtio_read_device_config_2(vsc,
    138  1.1  christos 	    VIRTIO_SCSI_CONFIG_MAX_TARGET);
    139  1.1  christos 
    140  1.1  christos 	uint16_t max_channel = virtio_read_device_config_2(vsc,
    141  1.1  christos 	    VIRTIO_SCSI_CONFIG_MAX_CHANNEL);
    142  1.1  christos 
    143  1.1  christos 	uint32_t max_lun = virtio_read_device_config_4(vsc,
    144  1.1  christos 	    VIRTIO_SCSI_CONFIG_MAX_LUN);
    145  1.1  christos 
    146  1.1  christos 	sc->sc_seg_max = seg_max;
    147  1.1  christos 
    148  1.1  christos 	for (size_t i = 0; i < __arraycount(sc->sc_vqs); i++) {
    149  1.1  christos 		rv = virtio_alloc_vq(vsc, &sc->sc_vqs[i], i, MAXPHYS,
    150  1.1  christos 		    1 + howmany(MAXPHYS, NBPG), vioscsi_vq_names[i]);
    151  1.1  christos 		if (rv) {
    152  1.1  christos 			aprint_error_dev(sc->sc_dev,
    153  1.1  christos 			    "failed to allocate virtqueue %zu\n", i);
    154  1.1  christos 			return;
    155  1.1  christos 		}
    156  1.1  christos 		sc->sc_vqs[i].vq_done = vioscsi_vq_done;
    157  1.1  christos 	}
    158  1.1  christos 
    159  1.1  christos 	int qsize = sc->sc_vqs[2].vq_num;
    160  1.1  christos 	aprint_normal_dev(sc->sc_dev, "qsize %d\n", qsize);
    161  1.1  christos 	if (vioscsi_alloc_reqs(sc, vsc, qsize, seg_max))
    162  1.1  christos 		return;
    163  1.1  christos 
    164  1.1  christos 	/*
    165  1.1  christos 	 * Fill in the scsipi_adapter.
    166  1.1  christos 	 */
    167  1.1  christos 	memset(adapt, 0, sizeof(*adapt));
    168  1.1  christos 	adapt->adapt_dev = sc->sc_dev;
    169  1.1  christos 	adapt->adapt_nchannels = max_channel;
    170  1.1  christos 	adapt->adapt_openings = cmd_per_lun;
    171  1.1  christos 	adapt->adapt_max_periph = adapt->adapt_openings;
    172  1.1  christos 	adapt->adapt_request = vioscsi_scsipi_request;
    173  1.1  christos 	adapt->adapt_minphys = minphys;
    174  1.1  christos 
    175  1.1  christos 	/*
    176  1.1  christos 	 * Fill in the scsipi_channel.
    177  1.1  christos 	 */
    178  1.1  christos 	memset(chan, 0, sizeof(*chan));
    179  1.1  christos 	chan->chan_adapter = adapt;
    180  1.1  christos 	chan->chan_bustype = &scsi_bustype;
    181  1.1  christos 	chan->chan_channel = 0;
    182  1.1  christos 	chan->chan_ntargets = max_target;
    183  1.1  christos 	chan->chan_nluns = max_lun;
    184  1.1  christos 	chan->chan_id = 0; /*XXX*/
    185  1.1  christos 
    186  1.1  christos 	config_found(sc->sc_dev, &sc->sc_channel, scsiprint);
    187  1.1  christos }
    188  1.1  christos 
    189  1.1  christos #define XS2DMA(xs) \
    190  1.1  christos     ((((xs)->xs_control & XS_CTL_DATA_IN) ? BUS_DMA_READ : BUS_DMA_WRITE) | \
    191  1.1  christos     (((xs)->xs_control & XS_CTL_NOSLEEP) ? BUS_DMA_NOWAIT : BUS_DMA_WAITOK) | \
    192  1.1  christos     BUS_DMA_STREAMING)
    193  1.1  christos 
    194  1.1  christos #define XS2DMAPRE(xs) (((xs)->xs_control & XS_CTL_DATA_IN) ? \
    195  1.1  christos     BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE)
    196  1.1  christos 
    197  1.1  christos #define XS2DMAPOST(xs) (((xs)->xs_control & XS_CTL_DATA_IN) ? \
    198  1.1  christos     BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE)
    199  1.1  christos 
    200  1.1  christos static void
    201  1.1  christos vioscsi_scsipi_request(struct scsipi_channel *chan, scsipi_adapter_req_t
    202  1.1  christos     request, void *arg)
    203  1.1  christos {
    204  1.1  christos 	struct vioscsi_softc *sc =
    205  1.1  christos 	    device_private(chan->chan_adapter->adapt_dev);
    206  1.1  christos 	struct virtio_softc *vsc = device_private(device_parent(sc->sc_dev));
    207  1.1  christos 	struct scsipi_xfer *xs;
    208  1.1  christos 	struct scsipi_periph *periph;
    209  1.1  christos 	struct vioscsi_req *vr;
    210  1.1  christos 	struct virtio_scsi_req_hdr *req;
    211  1.1  christos 	struct virtqueue *vq = &sc->sc_vqs[2];
    212  1.1  christos 	int slot, error;
    213  1.1  christos 
    214  1.1  christos 	DPRINTF(("%s: enter\n", __func__));
    215  1.1  christos 
    216  1.1  christos 	if (request != ADAPTER_REQ_RUN_XFER) {
    217  1.1  christos 		DPRINTF(("%s: unhandled %d\n", __func__, request));
    218  1.1  christos 		return;
    219  1.1  christos 	}
    220  1.1  christos 
    221  1.1  christos 
    222  1.1  christos 	xs = arg;
    223  1.1  christos 	periph = xs->xs_periph;
    224  1.1  christos 
    225  1.1  christos         KASSERT((xs->xs_control & (XS_CTL_DATA_IN|XS_CTL_DATA_OUT)) != 0);
    226  1.1  christos 
    227  1.1  christos 	vr = vioscsi_req_get(sc);
    228  1.1  christos #ifdef DIAGNOSTIC
    229  1.1  christos 	/*
    230  1.1  christos 	 * This should never happen as we track the resources
    231  1.1  christos 	 * in the mid-layer.
    232  1.1  christos 	 */
    233  1.1  christos 	if (vr == NULL) {
    234  1.1  christos 		scsipi_printaddr(xs->xs_periph);
    235  1.1  christos 		panic("%s: unable to allocate request\n", __func__);
    236  1.1  christos 	}
    237  1.1  christos #endif
    238  1.1  christos 	req = &vr->vr_req;
    239  1.1  christos 	slot = vr - sc->sc_reqs;
    240  1.1  christos 
    241  1.1  christos 	vr->vr_xs = xs;
    242  1.1  christos 
    243  1.1  christos 	/*
    244  1.1  christos 	 * "The only supported format for the LUN field is: first byte set to
    245  1.1  christos 	 * 1, second byte set to target, third and fourth byte representing a
    246  1.1  christos 	 * single level LUN structure, followed by four zero bytes."
    247  1.1  christos 	 */
    248  1.1  christos 	if (periph->periph_target >= 256 || periph->periph_lun >= 16384) {
    249  1.1  christos 		DPRINTF(("%s: bad target %u or lun %u\n", __func__,
    250  1.1  christos 		    periph->periph_target, periph->periph_lun));
    251  1.1  christos 		goto stuffup;
    252  1.1  christos 	}
    253  1.1  christos 	req->lun[0] = 1;
    254  1.1  christos 	req->lun[1] = periph->periph_target;
    255  1.1  christos 	req->lun[2] = 0x40 | (periph->periph_lun >> 8);
    256  1.1  christos 	req->lun[3] = periph->periph_lun;
    257  1.1  christos 	memset(req->lun + 4, 0, 4);
    258  1.1  christos 
    259  1.1  christos 	if ((size_t)xs->cmdlen > sizeof(req->cdb))
    260  1.1  christos 		goto stuffup;
    261  1.1  christos 	memset(req->cdb, 0, sizeof(req->cdb));
    262  1.1  christos 	memcpy(req->cdb, xs->cmd, xs->cmdlen);
    263  1.1  christos 
    264  1.1  christos 	error = bus_dmamap_load(vsc->sc_dmat, vr->vr_data,
    265  1.1  christos 	    xs->data, xs->datalen, NULL, XS2DMA(xs));
    266  1.1  christos 	switch (error) {
    267  1.1  christos 	case 0:
    268  1.1  christos 		break;
    269  1.1  christos 	case ENOMEM:
    270  1.1  christos 	case EAGAIN:
    271  1.1  christos 		xs->error = XS_RESOURCE_SHORTAGE;
    272  1.1  christos 		goto nomore;
    273  1.1  christos 	default:
    274  1.1  christos 		aprint_error_dev(sc->sc_dev, "error %d loading DMA map\n",
    275  1.1  christos 		    error);
    276  1.1  christos 	stuffup:
    277  1.1  christos 		xs->error = XS_DRIVER_STUFFUP;
    278  1.1  christos nomore:
    279  1.1  christos 		// XXX: free req?
    280  1.1  christos 		scsipi_done(xs);
    281  1.1  christos 		return;
    282  1.1  christos 	}
    283  1.1  christos 
    284  1.1  christos 	error = virtio_enqueue_reserve(vsc, vq, slot,
    285  1.1  christos 	    vr->vr_data->dm_nsegs + 2);
    286  1.1  christos 	if (error) {
    287  1.1  christos 		DPRINTF(("%s: error reserving %d\n", __func__, error));
    288  1.1  christos 		goto stuffup;
    289  1.1  christos 	}
    290  1.1  christos 
    291  1.1  christos 	bus_dmamap_sync(vsc->sc_dmat, vr->vr_control,
    292  1.1  christos 	    offsetof(struct vioscsi_req, vr_req),
    293  1.1  christos 	    sizeof(struct virtio_scsi_req_hdr),
    294  1.1  christos 	    BUS_DMASYNC_PREWRITE);
    295  1.1  christos 	bus_dmamap_sync(vsc->sc_dmat, vr->vr_control,
    296  1.1  christos 	    offsetof(struct vioscsi_req, vr_res),
    297  1.1  christos             sizeof(struct virtio_scsi_res_hdr),
    298  1.1  christos 	    BUS_DMASYNC_PREREAD);
    299  1.1  christos 	bus_dmamap_sync(vsc->sc_dmat, vr->vr_data, 0, xs->datalen,
    300  1.1  christos 	    XS2DMAPRE(xs));
    301  1.1  christos 
    302  1.1  christos 	virtio_enqueue_p(vsc, vq, slot, vr->vr_control,
    303  1.1  christos 	    offsetof(struct vioscsi_req, vr_req),
    304  1.1  christos             sizeof(struct virtio_scsi_req_hdr), 1);
    305  1.1  christos 	if (xs->xs_control & XS_CTL_DATA_OUT)
    306  1.1  christos 		virtio_enqueue(vsc, vq, slot, vr->vr_data, 1);
    307  1.1  christos 	virtio_enqueue_p(vsc, vq, slot, vr->vr_control,
    308  1.1  christos 	    offsetof(struct vioscsi_req, vr_res),
    309  1.1  christos             sizeof(struct virtio_scsi_res_hdr), 0);
    310  1.1  christos 	if (xs->xs_control & XS_CTL_DATA_IN)
    311  1.1  christos 		virtio_enqueue(vsc, vq, slot, vr->vr_data, 0);
    312  1.1  christos 	virtio_enqueue_commit(vsc, vq, slot, 1);
    313  1.1  christos 
    314  1.1  christos 	if ((xs->xs_control & XS_CTL_POLL) == 0)
    315  1.1  christos 		return;
    316  1.1  christos 
    317  1.1  christos 	DPRINTF(("%s: polling...\n", __func__));
    318  1.1  christos 	// XXX: do this better.
    319  1.1  christos 	int timeout = 1000;
    320  1.1  christos 	do {
    321  1.1  christos 		(*vsc->sc_intrhand)(vsc);
    322  1.1  christos 		if (vr->vr_xs != xs)
    323  1.1  christos 			break;
    324  1.1  christos 		delay(1000);
    325  1.1  christos 	} while (--timeout > 0);
    326  1.1  christos 
    327  1.1  christos 	if (vr->vr_xs == xs) {
    328  1.1  christos 		// XXX: Abort!
    329  1.1  christos 		xs->error = XS_TIMEOUT;
    330  1.1  christos 		xs->resid = xs->datalen;
    331  1.1  christos 		DPRINTF(("%s: polling timeout\n", __func__));
    332  1.1  christos 		scsipi_done(xs);
    333  1.1  christos 	}
    334  1.1  christos 	DPRINTF(("%s: done (timeout=%d)\n", __func__, timeout));
    335  1.1  christos }
    336  1.1  christos 
    337  1.1  christos static void
    338  1.1  christos vioscsi_req_done(struct vioscsi_softc *sc, struct virtio_softc *vsc,
    339  1.1  christos     struct vioscsi_req *vr)
    340  1.1  christos {
    341  1.1  christos 	struct scsipi_xfer *xs = vr->vr_xs;
    342  1.1  christos 
    343  1.1  christos 	DPRINTF(("%s: enter\n", __func__));
    344  1.1  christos 
    345  1.1  christos 	bus_dmamap_sync(vsc->sc_dmat, vr->vr_control,
    346  1.1  christos 	    offsetof(struct vioscsi_req, vr_req),
    347  1.1  christos 	    sizeof(struct virtio_scsi_req_hdr),
    348  1.1  christos 	    BUS_DMASYNC_POSTWRITE);
    349  1.1  christos 	bus_dmamap_sync(vsc->sc_dmat, vr->vr_control,
    350  1.1  christos 	    offsetof(struct vioscsi_req, vr_res),
    351  1.1  christos 	    sizeof(struct virtio_scsi_res_hdr),
    352  1.1  christos 	    BUS_DMASYNC_POSTREAD);
    353  1.1  christos 	bus_dmamap_sync(vsc->sc_dmat, vr->vr_data, 0, xs->datalen,
    354  1.1  christos 	    XS2DMAPOST(xs));
    355  1.1  christos 
    356  1.1  christos 	if (vr->vr_res.response != VIRTIO_SCSI_S_OK) {
    357  1.1  christos 		xs->error = XS_DRIVER_STUFFUP;
    358  1.1  christos 		xs->resid = xs->datalen;
    359  1.1  christos 		DPRINTF(("%s: stuffup: %d\n", __func__, vr->vr_res.response));
    360  1.1  christos 		goto done;
    361  1.1  christos 	}
    362  1.1  christos 
    363  1.1  christos 	size_t sense_len = MIN(sizeof(xs->sense), vr->vr_res.sense_len);
    364  1.1  christos 	memcpy(&xs->sense, vr->vr_res.sense, sense_len);
    365  1.1  christos 	xs->error = (sense_len == 0) ? XS_NOERROR : XS_SENSE;
    366  1.1  christos 
    367  1.1  christos 	xs->status = vr->vr_res.status;
    368  1.1  christos 	xs->resid = vr->vr_res.residual;
    369  1.1  christos 
    370  1.1  christos 	DPRINTF(("%s: done %d, %d, %d\n", __func__,
    371  1.1  christos 	    xs->error, xs->status, xs->resid));
    372  1.1  christos 
    373  1.1  christos done:
    374  1.1  christos 	vr->vr_xs = NULL;
    375  1.1  christos 	vioscsi_req_put(sc, vr);
    376  1.1  christos 	scsipi_done(xs);
    377  1.1  christos }
    378  1.1  christos 
    379  1.1  christos static int
    380  1.1  christos vioscsi_vq_done(struct virtqueue *vq)
    381  1.1  christos {
    382  1.1  christos 	struct virtio_softc *vsc = vq->vq_owner;
    383  1.1  christos 	struct vioscsi_softc *sc = device_private(vsc->sc_child);
    384  1.1  christos 	int ret = 0;
    385  1.1  christos 
    386  1.1  christos 	DPRINTF(("%s: enter\n", __func__));
    387  1.1  christos 
    388  1.1  christos 	for (;;) {
    389  1.1  christos 		int r, slot;
    390  1.1  christos 		r = virtio_dequeue(vsc, vq, &slot, NULL);
    391  1.1  christos 		if (r != 0)
    392  1.1  christos 			break;
    393  1.1  christos 
    394  1.1  christos 		DPRINTF(("%s: slot=%d\n", __func__, slot));
    395  1.1  christos 		vioscsi_req_done(sc, vsc, &sc->sc_reqs[slot]);
    396  1.1  christos 		ret = 1;
    397  1.1  christos 	}
    398  1.1  christos 
    399  1.1  christos 	DPRINTF(("%s: exit %d\n", __func__, ret));
    400  1.1  christos 
    401  1.1  christos 	return ret;
    402  1.1  christos }
    403  1.1  christos 
    404  1.1  christos static struct vioscsi_req *
    405  1.1  christos vioscsi_req_get(struct vioscsi_softc *sc)
    406  1.1  christos {
    407  1.1  christos 	struct virtio_softc *vsc = device_private(device_parent(sc->sc_dev));
    408  1.1  christos 	struct virtqueue *vq = &sc->sc_vqs[2];
    409  1.1  christos 	struct vioscsi_req *vr;
    410  1.1  christos 	int r, slot;
    411  1.1  christos 
    412  1.1  christos 	if ((r = virtio_enqueue_prep(vsc, vq, &slot)) != 0) {
    413  1.1  christos 		DPRINTF(("%s: virtio_enqueue_get error %d\n", __func__, r));
    414  1.1  christos 		goto err1;
    415  1.1  christos 	}
    416  1.1  christos 	vr = &sc->sc_reqs[slot];
    417  1.1  christos 
    418  1.1  christos 	vr->vr_req.id = slot;
    419  1.1  christos 	vr->vr_req.task_attr = VIRTIO_SCSI_S_SIMPLE;
    420  1.1  christos 
    421  1.1  christos 	r = bus_dmamap_create(vsc->sc_dmat,
    422  1.1  christos 	    offsetof(struct vioscsi_req, vr_xs), 1,
    423  1.1  christos 	    offsetof(struct vioscsi_req, vr_xs), 0,
    424  1.1  christos 	    BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW, &vr->vr_control);
    425  1.1  christos 	if (r != 0) {
    426  1.1  christos 		DPRINTF(("%s: bus_dmamap_create xs error %d\n", __func__, r));
    427  1.1  christos 		goto err2;
    428  1.1  christos 	}
    429  1.1  christos 	r = bus_dmamap_create(vsc->sc_dmat, MAXPHYS, sc->sc_seg_max,
    430  1.1  christos 	    MAXPHYS, 0, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW, &vr->vr_data);
    431  1.1  christos 	if (r != 0) {
    432  1.1  christos 		DPRINTF(("%s: bus_dmamap_create data error %d\n", __func__, r));
    433  1.1  christos 		goto err3;
    434  1.1  christos 	}
    435  1.1  christos 	r = bus_dmamap_load(vsc->sc_dmat, vr->vr_control,
    436  1.1  christos 	    vr, offsetof(struct vioscsi_req, vr_xs), NULL,
    437  1.1  christos 	    BUS_DMA_NOWAIT);
    438  1.1  christos 	if (r != 0) {
    439  1.1  christos 		DPRINTF(("%s: bus_dmamap_create ctrl error %d\n", __func__, r));
    440  1.1  christos 		goto err4;
    441  1.1  christos 	}
    442  1.1  christos 
    443  1.1  christos 	DPRINTF(("%s: %p, %d\n", __func__, vr, slot));
    444  1.1  christos 
    445  1.1  christos 	return vr;
    446  1.1  christos 
    447  1.1  christos err4:
    448  1.1  christos 	bus_dmamap_destroy(vsc->sc_dmat, vr->vr_data);
    449  1.1  christos err3:
    450  1.1  christos 	bus_dmamap_destroy(vsc->sc_dmat, vr->vr_control);
    451  1.1  christos err2:
    452  1.1  christos 	virtio_enqueue_abort(vsc, vq, slot);
    453  1.1  christos err1:
    454  1.1  christos 	return NULL;
    455  1.1  christos }
    456  1.1  christos 
    457  1.1  christos static void
    458  1.1  christos vioscsi_req_put(struct vioscsi_softc *sc, struct vioscsi_req *vr)
    459  1.1  christos {
    460  1.1  christos 	struct virtio_softc *vsc = device_private(device_parent(sc->sc_dev));
    461  1.1  christos 	struct virtqueue *vq = &sc->sc_vqs[2];
    462  1.1  christos 	int slot = vr - sc->sc_reqs;
    463  1.1  christos 
    464  1.1  christos 	DPRINTF(("%s: %p, %d\n", __func__, vr, slot));
    465  1.1  christos 
    466  1.1  christos 	bus_dmamap_destroy(vsc->sc_dmat, vr->vr_control);
    467  1.1  christos 	bus_dmamap_destroy(vsc->sc_dmat, vr->vr_data);
    468  1.1  christos 
    469  1.1  christos 	virtio_dequeue_commit(vsc, vq, slot);
    470  1.1  christos }
    471  1.1  christos 
    472  1.1  christos int
    473  1.1  christos vioscsi_alloc_reqs(struct vioscsi_softc *sc, struct virtio_softc *vsc,
    474  1.1  christos     int qsize, uint32_t seg_max)
    475  1.1  christos {
    476  1.1  christos 	size_t allocsize;
    477  1.1  christos 	int r, rsegs;
    478  1.1  christos 	void *vaddr;
    479  1.1  christos 
    480  1.1  christos 	allocsize = qsize * sizeof(struct vioscsi_req);
    481  1.1  christos 	r = bus_dmamem_alloc(vsc->sc_dmat, allocsize, 0, 0,
    482  1.1  christos 	    &sc->sc_reqs_segs[0], 1, &rsegs, BUS_DMA_NOWAIT);
    483  1.1  christos 	if (r != 0) {
    484  1.1  christos 		aprint_error_dev(sc->sc_dev,
    485  1.1  christos 		    "%s: bus_dmamem_alloc, size %zu, error %d\n", __func__,
    486  1.1  christos 		    allocsize, r);
    487  1.1  christos 		return 1;
    488  1.1  christos 	}
    489  1.1  christos 	r = bus_dmamem_map(vsc->sc_dmat, &sc->sc_reqs_segs[0], 1,
    490  1.1  christos 	    allocsize, &vaddr, BUS_DMA_NOWAIT);
    491  1.1  christos 	if (r != 0) {
    492  1.1  christos 		aprint_error_dev(sc->sc_dev,
    493  1.1  christos 		    "%s: bus_dmamem_map failed, error %d\n", __func__, r);
    494  1.1  christos 		bus_dmamem_free(vsc->sc_dmat, &sc->sc_reqs_segs[0], 1);
    495  1.1  christos 		return 1;
    496  1.1  christos 	}
    497  1.1  christos 	sc->sc_reqs = vaddr;
    498  1.1  christos 	memset(vaddr, 0, allocsize);
    499  1.1  christos 	return 0;
    500  1.1  christos }
    501