[BACK]Return to ld_virtio.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sys / dev / pci

Annotation of src/sys/dev/pci/ld_virtio.c, Revision 1.10.2.2

1.10.2.2! pgoyette    1: /*     $NetBSD: ld_virtio.c,v 1.13 2016/11/30 01:36:38 christos Exp $  */
1.1       hannken     2:
                      3: /*
                      4:  * Copyright (c) 2010 Minoura Makoto.
                      5:  * All rights reserved.
                      6:  *
                      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. Redistributions in binary form must reproduce the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer in the
                     14:  *    documentation and/or other materials provided with the distribution.
                     15:  *
                     16:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     17:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     18:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     19:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     20:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     21:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     22:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     23:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     24:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     25:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     26:  */
                     27:
                     28: #include <sys/cdefs.h>
1.10.2.2! pgoyette   29: __KERNEL_RCSID(0, "$NetBSD: ld_virtio.c,v 1.13 2016/11/30 01:36:38 christos Exp $");
1.1       hannken    30:
                     31: #include <sys/param.h>
                     32: #include <sys/systm.h>
                     33: #include <sys/kernel.h>
                     34: #include <sys/buf.h>
1.10.2.1  pgoyette   35: #include <sys/bufq.h>
1.1       hannken    36: #include <sys/bus.h>
                     37: #include <sys/device.h>
                     38: #include <sys/disk.h>
                     39: #include <sys/mutex.h>
1.10.2.1  pgoyette   40: #include <sys/module.h>
1.1       hannken    41:
                     42: #include <dev/pci/pcidevs.h>
                     43: #include <dev/pci/pcireg.h>
                     44: #include <dev/pci/pcivar.h>
                     45:
                     46: #include <dev/ldvar.h>
                     47: #include <dev/pci/virtioreg.h>
                     48: #include <dev/pci/virtiovar.h>
                     49:
1.10.2.1  pgoyette   50: #include "ioconf.h"
                     51:
1.1       hannken    52: /*
                     53:  * ld_virtioreg:
                     54:  */
                     55: /* Configuration registers */
                     56: #define VIRTIO_BLK_CONFIG_CAPACITY     0 /* 64bit */
                     57: #define VIRTIO_BLK_CONFIG_SIZE_MAX     8 /* 32bit */
                     58: #define VIRTIO_BLK_CONFIG_SEG_MAX      12 /* 32bit */
                     59: #define VIRTIO_BLK_CONFIG_GEOMETRY_C   16 /* 16bit */
                     60: #define VIRTIO_BLK_CONFIG_GEOMETRY_H   18 /* 8bit */
                     61: #define VIRTIO_BLK_CONFIG_GEOMETRY_S   19 /* 8bit */
                     62: #define VIRTIO_BLK_CONFIG_BLK_SIZE     20 /* 32bit */
                     63:
                     64: /* Feature bits */
                     65: #define VIRTIO_BLK_F_BARRIER   (1<<0)
                     66: #define VIRTIO_BLK_F_SIZE_MAX  (1<<1)
                     67: #define VIRTIO_BLK_F_SEG_MAX   (1<<2)
                     68: #define VIRTIO_BLK_F_GEOMETRY  (1<<4)
                     69: #define VIRTIO_BLK_F_RO                (1<<5)
                     70: #define VIRTIO_BLK_F_BLK_SIZE  (1<<6)
                     71: #define VIRTIO_BLK_F_SCSI      (1<<7)
                     72: #define VIRTIO_BLK_F_FLUSH     (1<<9)
                     73:
1.9       christos   74: /*
                     75:  * Each block request uses at least two segments - one for the header
                     76:  * and one for the status.
                     77: */
                     78: #define        VIRTIO_BLK_MIN_SEGMENTS 2
                     79:
                     80: #define VIRTIO_BLK_FLAG_BITS \
                     81:        VIRTIO_COMMON_FLAG_BITS \
                     82:        "\x0a""FLUSH" \
                     83:        "\x08""SCSI" \
                     84:        "\x07""BLK_SIZE" \
                     85:        "\x06""RO" \
                     86:        "\x05""GEOMETRY" \
                     87:        "\x03""SEG_MAX" \
                     88:        "\x02""SIZE_MAX" \
                     89:        "\x01""BARRIER"
                     90:
1.1       hannken    91: /* Command */
                     92: #define VIRTIO_BLK_T_IN                0
                     93: #define VIRTIO_BLK_T_OUT       1
                     94: #define VIRTIO_BLK_T_BARRIER   0x80000000
                     95:
                     96: /* Status */
                     97: #define VIRTIO_BLK_S_OK                0
                     98: #define VIRTIO_BLK_S_IOERR     1
                     99:
                    100: /* Request header structure */
                    101: struct virtio_blk_req_hdr {
                    102:        uint32_t        type;   /* VIRTIO_BLK_T_* */
                    103:        uint32_t        ioprio;
                    104:        uint64_t        sector;
                    105: } __packed;
                    106: /* 512*virtio_blk_req_hdr.sector byte payload and 1 byte status follows */
                    107:
                    108:
                    109: /*
                    110:  * ld_virtiovar:
                    111:  */
                    112: struct virtio_blk_req {
                    113:        struct virtio_blk_req_hdr       vr_hdr;
                    114:        uint8_t                         vr_status;
                    115:        struct buf                      *vr_bp;
                    116:        bus_dmamap_t                    vr_cmdsts;
                    117:        bus_dmamap_t                    vr_payload;
                    118: };
                    119:
                    120: struct ld_virtio_softc {
                    121:        struct ld_softc         sc_ld;
                    122:        device_t                sc_dev;
                    123:
                    124:        struct virtio_softc     *sc_virtio;
1.9       christos  125:        struct virtqueue        sc_vq;
1.1       hannken   126:
                    127:        struct virtio_blk_req   *sc_reqs;
1.9       christos  128:        bus_dma_segment_t       sc_reqs_seg;
1.1       hannken   129:
                    130:        int                     sc_readonly;
                    131: };
                    132:
                    133: static int     ld_virtio_match(device_t, cfdata_t, void *);
                    134: static void    ld_virtio_attach(device_t, device_t, void *);
                    135: static int     ld_virtio_detach(device_t, int);
                    136:
                    137: CFATTACH_DECL_NEW(ld_virtio, sizeof(struct ld_virtio_softc),
                    138:     ld_virtio_match, ld_virtio_attach, ld_virtio_detach, NULL);
                    139:
                    140: static int
                    141: ld_virtio_match(device_t parent, cfdata_t match, void *aux)
                    142: {
                    143:        struct virtio_softc *va = aux;
                    144:
                    145:        if (va->sc_childdevid == PCI_PRODUCT_VIRTIO_BLOCK)
                    146:                return 1;
                    147:
                    148:        return 0;
                    149: }
                    150:
                    151: static int ld_virtio_vq_done(struct virtqueue *);
                    152: static int ld_virtio_dump(struct ld_softc *, void *, int, int);
                    153: static int ld_virtio_start(struct ld_softc *, struct buf *);
                    154:
                    155: static int
                    156: ld_virtio_alloc_reqs(struct ld_virtio_softc *sc, int qsize)
                    157: {
                    158:        int allocsize, r, rsegs, i;
                    159:        struct ld_softc *ld = &sc->sc_ld;
                    160:        void *vaddr;
                    161:
                    162:        allocsize = sizeof(struct virtio_blk_req) * qsize;
                    163:        r = bus_dmamem_alloc(sc->sc_virtio->sc_dmat, allocsize, 0, 0,
1.9       christos  164:                             &sc->sc_reqs_seg, 1, &rsegs, BUS_DMA_NOWAIT);
1.1       hannken   165:        if (r != 0) {
                    166:                aprint_error_dev(sc->sc_dev,
                    167:                                 "DMA memory allocation failed, size %d, "
                    168:                                 "error code %d\n", allocsize, r);
                    169:                goto err_none;
                    170:        }
                    171:        r = bus_dmamem_map(sc->sc_virtio->sc_dmat,
1.9       christos  172:                           &sc->sc_reqs_seg, 1, allocsize,
1.1       hannken   173:                           &vaddr, BUS_DMA_NOWAIT);
                    174:        if (r != 0) {
                    175:                aprint_error_dev(sc->sc_dev,
                    176:                                 "DMA memory map failed, "
                    177:                                 "error code %d\n", r);
                    178:                goto err_dmamem_alloc;
                    179:        }
                    180:        sc->sc_reqs = vaddr;
                    181:        memset(vaddr, 0, allocsize);
                    182:        for (i = 0; i < qsize; i++) {
                    183:                struct virtio_blk_req *vr = &sc->sc_reqs[i];
                    184:                r = bus_dmamap_create(sc->sc_virtio->sc_dmat,
                    185:                                      offsetof(struct virtio_blk_req, vr_bp),
                    186:                                      1,
                    187:                                      offsetof(struct virtio_blk_req, vr_bp),
                    188:                                      0,
                    189:                                      BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW,
                    190:                                      &vr->vr_cmdsts);
                    191:                if (r != 0) {
                    192:                        aprint_error_dev(sc->sc_dev,
                    193:                                         "command dmamap creation failed, "
                    194:                                         "error code %d\n", r);
                    195:                        goto err_reqs;
                    196:                }
                    197:                r = bus_dmamap_load(sc->sc_virtio->sc_dmat, vr->vr_cmdsts,
                    198:                                    &vr->vr_hdr,
                    199:                                    offsetof(struct virtio_blk_req, vr_bp),
                    200:                                    NULL, BUS_DMA_NOWAIT);
                    201:                if (r != 0) {
                    202:                        aprint_error_dev(sc->sc_dev,
                    203:                                         "command dmamap load failed, "
                    204:                                         "error code %d\n", r);
                    205:                        goto err_reqs;
                    206:                }
                    207:                r = bus_dmamap_create(sc->sc_virtio->sc_dmat,
                    208:                                      ld->sc_maxxfer,
1.9       christos  209:                                      (ld->sc_maxxfer / NBPG) +
                    210:                                      VIRTIO_BLK_MIN_SEGMENTS,
1.2       hannken   211:                                      ld->sc_maxxfer,
1.1       hannken   212:                                      0,
                    213:                                      BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW,
                    214:                                      &vr->vr_payload);
                    215:                if (r != 0) {
                    216:                        aprint_error_dev(sc->sc_dev,
                    217:                                         "payload dmamap creation failed, "
                    218:                                         "error code %d\n", r);
                    219:                        goto err_reqs;
                    220:                }
                    221:        }
                    222:        return 0;
                    223:
                    224: err_reqs:
                    225:        for (i = 0; i < qsize; i++) {
                    226:                struct virtio_blk_req *vr = &sc->sc_reqs[i];
                    227:                if (vr->vr_cmdsts) {
                    228:                        bus_dmamap_destroy(sc->sc_virtio->sc_dmat,
                    229:                                           vr->vr_cmdsts);
                    230:                        vr->vr_cmdsts = 0;
                    231:                }
                    232:                if (vr->vr_payload) {
                    233:                        bus_dmamap_destroy(sc->sc_virtio->sc_dmat,
                    234:                                           vr->vr_payload);
                    235:                        vr->vr_payload = 0;
                    236:                }
                    237:        }
                    238:        bus_dmamem_unmap(sc->sc_virtio->sc_dmat, sc->sc_reqs, allocsize);
                    239: err_dmamem_alloc:
1.9       christos  240:        bus_dmamem_free(sc->sc_virtio->sc_dmat, &sc->sc_reqs_seg, 1);
1.1       hannken   241: err_none:
                    242:        return -1;
                    243: }
                    244:
                    245: static void
                    246: ld_virtio_attach(device_t parent, device_t self, void *aux)
                    247: {
                    248:        struct ld_virtio_softc *sc = device_private(self);
                    249:        struct ld_softc *ld = &sc->sc_ld;
                    250:        struct virtio_softc *vsc = device_private(parent);
                    251:        uint32_t features;
1.9       christos  252:        char buf[256];
                    253:        int qsize, maxxfersize, maxnsegs;
1.1       hannken   254:
                    255:        if (vsc->sc_child != NULL) {
                    256:                aprint_normal(": child already attached for %s; "
1.10      msaitoh   257:                              "something wrong...\n", device_xname(parent));
1.1       hannken   258:                return;
                    259:        }
                    260:
                    261:        sc->sc_dev = self;
                    262:        sc->sc_virtio = vsc;
                    263:
                    264:        vsc->sc_child = self;
                    265:        vsc->sc_ipl = IPL_BIO;
1.9       christos  266:        vsc->sc_vqs = &sc->sc_vq;
1.1       hannken   267:        vsc->sc_nvqs = 1;
1.8       ozaki-r   268:        vsc->sc_config_change = NULL;
1.1       hannken   269:        vsc->sc_intrhand = virtio_vq_intr;
1.6       ozaki-r   270:        vsc->sc_flags = 0;
1.1       hannken   271:
                    272:        features = virtio_negotiate_features(vsc,
                    273:                                             (VIRTIO_BLK_F_SIZE_MAX |
                    274:                                              VIRTIO_BLK_F_SEG_MAX |
                    275:                                              VIRTIO_BLK_F_GEOMETRY |
                    276:                                              VIRTIO_BLK_F_RO |
1.3       hannken   277:                                              VIRTIO_BLK_F_BLK_SIZE));
1.1       hannken   278:        if (features & VIRTIO_BLK_F_RO)
                    279:                sc->sc_readonly = 1;
                    280:        else
                    281:                sc->sc_readonly = 0;
                    282:
1.9       christos  283:        snprintb(buf, sizeof(buf), VIRTIO_BLK_FLAG_BITS, features);
                    284:        aprint_normal(": Features: %s\n", buf);
                    285:        aprint_naive("\n");
1.3       hannken   286:        if (features & VIRTIO_BLK_F_BLK_SIZE) {
                    287:                ld->sc_secsize = virtio_read_device_config_4(vsc,
                    288:                                        VIRTIO_BLK_CONFIG_BLK_SIZE);
1.9       christos  289:        } else
                    290:                ld->sc_secsize = 512;
                    291:
                    292:        /* At least genfs_io assumes maxxfer == MAXPHYS. */
                    293:        if (features & VIRTIO_BLK_F_SIZE_MAX) {
1.1       hannken   294:                maxxfersize = virtio_read_device_config_4(vsc,
1.9       christos  295:                    VIRTIO_BLK_CONFIG_SIZE_MAX);
                    296:                if (maxxfersize < MAXPHYS) {
                    297:                        aprint_error_dev(sc->sc_dev,
                    298:                            "Too small SIZE_MAX %dK minimum is %dK\n",
                    299:                            maxxfersize / 1024, MAXPHYS / 1024);
                    300:                        // goto err;
                    301:                        maxxfersize = MAXPHYS;
                    302:                } else if (maxxfersize > MAXPHYS) {
                    303:                        aprint_normal_dev(sc->sc_dev,
                    304:                            "Clip SEG_MAX from %dK to %dK\n",
                    305:                            maxxfersize / 1024,
                    306:                            MAXPHYS / 1024);
1.1       hannken   307:                        maxxfersize = MAXPHYS;
1.9       christos  308:                }
                    309:        } else
                    310:                maxxfersize = MAXPHYS;
                    311:
                    312:        if (features & VIRTIO_BLK_F_SEG_MAX) {
                    313:                maxnsegs = virtio_read_device_config_4(vsc,
                    314:                    VIRTIO_BLK_CONFIG_SEG_MAX);
                    315:                if (maxnsegs < VIRTIO_BLK_MIN_SEGMENTS) {
                    316:                        aprint_error_dev(sc->sc_dev,
                    317:                            "Too small SEG_MAX %d minimum is %d\n",
                    318:                            maxnsegs, VIRTIO_BLK_MIN_SEGMENTS);
                    319:                        maxnsegs = maxxfersize / NBPG;
                    320:                        // goto err;
                    321:                }
                    322:        } else
                    323:                maxnsegs = maxxfersize / NBPG;
                    324:
                    325:        /* 2 for the minimum size */
                    326:        maxnsegs += VIRTIO_BLK_MIN_SEGMENTS;
1.1       hannken   327:
1.9       christos  328:        if (virtio_alloc_vq(vsc, &sc->sc_vq, 0, maxxfersize, maxnsegs,
                    329:            "I/O request") != 0) {
1.1       hannken   330:                goto err;
                    331:        }
1.9       christos  332:        qsize = sc->sc_vq.vq_num;
                    333:        sc->sc_vq.vq_done = ld_virtio_vq_done;
1.1       hannken   334:
                    335:        ld->sc_dv = self;
                    336:        ld->sc_secperunit = virtio_read_device_config_8(vsc,
                    337:                                VIRTIO_BLK_CONFIG_CAPACITY);
                    338:        ld->sc_maxxfer = maxxfersize;
                    339:        if (features & VIRTIO_BLK_F_GEOMETRY) {
                    340:                ld->sc_ncylinders = virtio_read_device_config_2(vsc,
                    341:                                        VIRTIO_BLK_CONFIG_GEOMETRY_C);
                    342:                ld->sc_nheads     = virtio_read_device_config_1(vsc,
                    343:                                        VIRTIO_BLK_CONFIG_GEOMETRY_H);
                    344:                ld->sc_nsectors   = virtio_read_device_config_1(vsc,
                    345:                                        VIRTIO_BLK_CONFIG_GEOMETRY_S);
                    346:        }
                    347:        ld->sc_maxqueuecnt = qsize;
                    348:
                    349:        if (ld_virtio_alloc_reqs(sc, qsize) < 0)
                    350:                goto err;
                    351:
                    352:        ld->sc_dump = ld_virtio_dump;
                    353:        ld->sc_flush = NULL;
                    354:        ld->sc_start = ld_virtio_start;
                    355:
                    356:        ld->sc_flags = LDF_ENABLED;
1.10.2.1  pgoyette  357:        ldattach(ld, BUFQ_DISK_DEFAULT_STRAT);
1.1       hannken   358:
                    359:        return;
                    360:
                    361: err:
                    362:        vsc->sc_child = (void*)1;
                    363:        return;
                    364: }
                    365:
                    366: static int
                    367: ld_virtio_start(struct ld_softc *ld, struct buf *bp)
                    368: {
                    369:        /* splbio */
                    370:        struct ld_virtio_softc *sc = device_private(ld->sc_dv);
                    371:        struct virtio_softc *vsc = sc->sc_virtio;
1.9       christos  372:        struct virtqueue *vq = &sc->sc_vq;
1.1       hannken   373:        struct virtio_blk_req *vr;
                    374:        int r;
                    375:        int isread = (bp->b_flags & B_READ);
                    376:        int slot;
                    377:
                    378:        if (sc->sc_readonly && !isread)
                    379:                return EIO;
                    380:
                    381:        r = virtio_enqueue_prep(vsc, vq, &slot);
                    382:        if (r != 0)
                    383:                return r;
1.9       christos  384:
1.1       hannken   385:        vr = &sc->sc_reqs[slot];
1.9       christos  386:        KASSERT(vr->vr_bp == NULL);
                    387:
1.1       hannken   388:        r = bus_dmamap_load(vsc->sc_dmat, vr->vr_payload,
                    389:                            bp->b_data, bp->b_bcount, NULL,
                    390:                            ((isread?BUS_DMA_READ:BUS_DMA_WRITE)
                    391:                             |BUS_DMA_NOWAIT));
1.9       christos  392:        if (r != 0) {
                    393:                aprint_error_dev(sc->sc_dev,
                    394:                    "payload dmamap failed, error code %d\n", r);
                    395:                virtio_enqueue_abort(vsc, vq, slot);
1.1       hannken   396:                return r;
1.9       christos  397:        }
1.1       hannken   398:
1.9       christos  399:        r = virtio_enqueue_reserve(vsc, vq, slot, vr->vr_payload->dm_nsegs +
                    400:            VIRTIO_BLK_MIN_SEGMENTS);
1.1       hannken   401:        if (r != 0) {
                    402:                bus_dmamap_unload(vsc->sc_dmat, vr->vr_payload);
                    403:                return r;
                    404:        }
                    405:
                    406:        vr->vr_bp = bp;
                    407:        vr->vr_hdr.type = isread?VIRTIO_BLK_T_IN:VIRTIO_BLK_T_OUT;
                    408:        vr->vr_hdr.ioprio = 0;
                    409:        vr->vr_hdr.sector = bp->b_rawblkno * sc->sc_ld.sc_secsize / 512;
                    410:
                    411:        bus_dmamap_sync(vsc->sc_dmat, vr->vr_cmdsts,
                    412:                        0, sizeof(struct virtio_blk_req_hdr),
                    413:                        BUS_DMASYNC_PREWRITE);
                    414:        bus_dmamap_sync(vsc->sc_dmat, vr->vr_payload,
                    415:                        0, bp->b_bcount,
                    416:                        isread?BUS_DMASYNC_PREREAD:BUS_DMASYNC_PREWRITE);
                    417:        bus_dmamap_sync(vsc->sc_dmat, vr->vr_cmdsts,
                    418:                        offsetof(struct virtio_blk_req, vr_status),
                    419:                        sizeof(uint8_t),
                    420:                        BUS_DMASYNC_PREREAD);
                    421:
                    422:        virtio_enqueue_p(vsc, vq, slot, vr->vr_cmdsts,
                    423:                         0, sizeof(struct virtio_blk_req_hdr),
                    424:                         true);
                    425:        virtio_enqueue(vsc, vq, slot, vr->vr_payload, !isread);
                    426:        virtio_enqueue_p(vsc, vq, slot, vr->vr_cmdsts,
                    427:                         offsetof(struct virtio_blk_req, vr_status),
                    428:                         sizeof(uint8_t),
                    429:                         false);
                    430:        virtio_enqueue_commit(vsc, vq, slot, true);
                    431:
                    432:        return 0;
                    433: }
                    434:
                    435: static void
                    436: ld_virtio_vq_done1(struct ld_virtio_softc *sc, struct virtio_softc *vsc,
                    437:                   struct virtqueue *vq, int slot)
                    438: {
                    439:        struct virtio_blk_req *vr = &sc->sc_reqs[slot];
                    440:        struct buf *bp = vr->vr_bp;
                    441:
1.9       christos  442:        vr->vr_bp = NULL;
                    443:
1.1       hannken   444:        bus_dmamap_sync(vsc->sc_dmat, vr->vr_cmdsts,
                    445:                        0, sizeof(struct virtio_blk_req_hdr),
                    446:                        BUS_DMASYNC_POSTWRITE);
                    447:        bus_dmamap_sync(vsc->sc_dmat, vr->vr_payload,
                    448:                        0, bp->b_bcount,
                    449:                        (bp->b_flags & B_READ)?BUS_DMASYNC_POSTREAD
                    450:                                              :BUS_DMASYNC_POSTWRITE);
                    451:        bus_dmamap_sync(vsc->sc_dmat, vr->vr_cmdsts,
                    452:                        sizeof(struct virtio_blk_req_hdr), sizeof(uint8_t),
                    453:                        BUS_DMASYNC_POSTREAD);
                    454:
                    455:        if (vr->vr_status != VIRTIO_BLK_S_OK) {
                    456:                bp->b_error = EIO;
                    457:                bp->b_resid = bp->b_bcount;
                    458:        } else {
                    459:                bp->b_error = 0;
                    460:                bp->b_resid = 0;
                    461:        }
                    462:
                    463:        virtio_dequeue_commit(vsc, vq, slot);
                    464:
                    465:        lddone(&sc->sc_ld, bp);
                    466: }
                    467:
                    468: static int
                    469: ld_virtio_vq_done(struct virtqueue *vq)
                    470: {
                    471:        struct virtio_softc *vsc = vq->vq_owner;
                    472:        struct ld_virtio_softc *sc = device_private(vsc->sc_child);
                    473:        int r = 0;
                    474:        int slot;
                    475:
                    476: again:
                    477:        if (virtio_dequeue(vsc, vq, &slot, NULL))
                    478:                return r;
                    479:        r = 1;
                    480:
                    481:        ld_virtio_vq_done1(sc, vsc, vq, slot);
                    482:        goto again;
                    483: }
                    484:
                    485: static int
                    486: ld_virtio_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
                    487: {
                    488:        struct ld_virtio_softc *sc = device_private(ld->sc_dv);
                    489:        struct virtio_softc *vsc = sc->sc_virtio;
1.9       christos  490:        struct virtqueue *vq = &sc->sc_vq;
1.1       hannken   491:        struct virtio_blk_req *vr;
                    492:        int slot, r;
                    493:
                    494:        if (sc->sc_readonly)
                    495:                return EIO;
                    496:
                    497:        r = virtio_enqueue_prep(vsc, vq, &slot);
                    498:        if (r != 0) {
                    499:                if (r == EAGAIN) { /* no free slot; dequeue first */
                    500:                        delay(100);
                    501:                        ld_virtio_vq_done(vq);
                    502:                        r = virtio_enqueue_prep(vsc, vq, &slot);
                    503:                        if (r != 0)
                    504:                                return r;
                    505:                }
                    506:                return r;
                    507:        }
                    508:        vr = &sc->sc_reqs[slot];
                    509:        r = bus_dmamap_load(vsc->sc_dmat, vr->vr_payload,
                    510:                            data, blkcnt*ld->sc_secsize, NULL,
                    511:                            BUS_DMA_WRITE|BUS_DMA_NOWAIT);
                    512:        if (r != 0)
                    513:                return r;
                    514:
1.9       christos  515:        r = virtio_enqueue_reserve(vsc, vq, slot, vr->vr_payload->dm_nsegs +
                    516:            VIRTIO_BLK_MIN_SEGMENTS);
1.1       hannken   517:        if (r != 0) {
                    518:                bus_dmamap_unload(vsc->sc_dmat, vr->vr_payload);
                    519:                return r;
                    520:        }
                    521:
                    522:        vr->vr_bp = (void*)0xdeadbeef;
                    523:        vr->vr_hdr.type = VIRTIO_BLK_T_OUT;
                    524:        vr->vr_hdr.ioprio = 0;
                    525:        vr->vr_hdr.sector = (daddr_t) blkno * ld->sc_secsize / 512;
                    526:
                    527:        bus_dmamap_sync(vsc->sc_dmat, vr->vr_cmdsts,
                    528:                        0, sizeof(struct virtio_blk_req_hdr),
                    529:                        BUS_DMASYNC_PREWRITE);
                    530:        bus_dmamap_sync(vsc->sc_dmat, vr->vr_payload,
                    531:                        0, blkcnt*ld->sc_secsize,
                    532:                        BUS_DMASYNC_PREWRITE);
                    533:        bus_dmamap_sync(vsc->sc_dmat, vr->vr_cmdsts,
                    534:                        offsetof(struct virtio_blk_req, vr_status),
                    535:                        sizeof(uint8_t),
                    536:                        BUS_DMASYNC_PREREAD);
                    537:
                    538:        virtio_enqueue_p(vsc, vq, slot, vr->vr_cmdsts,
                    539:                         0, sizeof(struct virtio_blk_req_hdr),
                    540:                         true);
                    541:        virtio_enqueue(vsc, vq, slot, vr->vr_payload, true);
                    542:        virtio_enqueue_p(vsc, vq, slot, vr->vr_cmdsts,
                    543:                         offsetof(struct virtio_blk_req, vr_status),
                    544:                         sizeof(uint8_t),
                    545:                         false);
                    546:        virtio_enqueue_commit(vsc, vq, slot, true);
                    547:
                    548:        for ( ; ; ) {
                    549:                int dslot;
                    550:
                    551:                r = virtio_dequeue(vsc, vq, &dslot, NULL);
                    552:                if (r != 0)
                    553:                        continue;
                    554:                if (dslot != slot) {
                    555:                        ld_virtio_vq_done1(sc, vsc, vq, dslot);
                    556:                        continue;
                    557:                } else
                    558:                        break;
                    559:        }
                    560:
                    561:        bus_dmamap_sync(vsc->sc_dmat, vr->vr_cmdsts,
                    562:                        0, sizeof(struct virtio_blk_req_hdr),
                    563:                        BUS_DMASYNC_POSTWRITE);
                    564:        bus_dmamap_sync(vsc->sc_dmat, vr->vr_payload,
                    565:                        0, blkcnt*ld->sc_secsize,
                    566:                        BUS_DMASYNC_POSTWRITE);
                    567:        bus_dmamap_sync(vsc->sc_dmat, vr->vr_cmdsts,
                    568:                        offsetof(struct virtio_blk_req, vr_status),
                    569:                        sizeof(uint8_t),
                    570:                        BUS_DMASYNC_POSTREAD);
                    571:        if (vr->vr_status == VIRTIO_BLK_S_OK)
                    572:                r = 0;
                    573:        else
                    574:                r = EIO;
                    575:        virtio_dequeue_commit(vsc, vq, slot);
                    576:
                    577:        return r;
                    578: }
                    579:
                    580: static int
                    581: ld_virtio_detach(device_t self, int flags)
                    582: {
                    583:        struct ld_virtio_softc *sc = device_private(self);
                    584:        struct ld_softc *ld = &sc->sc_ld;
                    585:        bus_dma_tag_t dmat = sc->sc_virtio->sc_dmat;
                    586:        int r, i, qsize;
                    587:
1.9       christos  588:        qsize = sc->sc_vq.vq_num;
1.1       hannken   589:        r = ldbegindetach(ld, flags);
                    590:        if (r != 0)
                    591:                return r;
                    592:        virtio_reset(sc->sc_virtio);
1.9       christos  593:        virtio_free_vq(sc->sc_virtio, &sc->sc_vq);
1.1       hannken   594:
                    595:        for (i = 0; i < qsize; i++) {
                    596:                bus_dmamap_destroy(dmat,
                    597:                                   sc->sc_reqs[i].vr_cmdsts);
                    598:                bus_dmamap_destroy(dmat,
                    599:                                   sc->sc_reqs[i].vr_payload);
                    600:        }
                    601:        bus_dmamem_unmap(dmat, sc->sc_reqs,
                    602:                         sizeof(struct virtio_blk_req) * qsize);
1.9       christos  603:        bus_dmamem_free(dmat, &sc->sc_reqs_seg, 1);
1.1       hannken   604:
                    605:        ldenddetach(ld);
                    606:
                    607:        return 0;
                    608: }
1.10.2.1  pgoyette  609:
                    610: MODULE(MODULE_CLASS_DRIVER, ld_virtio, "ld,virtio");
                    611:
                    612: #ifdef _MODULE
                    613: /*
                    614:  * XXX Don't allow ioconf.c to redefine the "struct cfdriver ld_cd"
                    615:  * XXX it will be defined in the common-code module
                    616:  */
                    617: #undef  CFDRIVER_DECL
                    618: #define CFDRIVER_DECL(name, class, attr)
                    619: #include "ioconf.c"
                    620: #endif
                    621:
                    622: static int
                    623: ld_virtio_modcmd(modcmd_t cmd, void *opaque)
                    624: {
                    625: #ifdef _MODULE
                    626:        /*
                    627:         * We ignore the cfdriver_vec[] that ioconf provides, since
                    628:         * the cfdrivers are attached already.
                    629:         */
                    630:        static struct cfdriver * const no_cfdriver_vec[] = { NULL };
                    631: #endif
                    632:        int error = 0;
                    633:
                    634: #ifdef _MODULE
                    635:        switch (cmd) {
                    636:        case MODULE_CMD_INIT:
                    637:                error = config_init_component(no_cfdriver_vec,
                    638:                    cfattach_ioconf_ld_virtio, cfdata_ioconf_ld_virtio);
                    639:                break;
                    640:        case MODULE_CMD_FINI:
                    641:                error = config_fini_component(no_cfdriver_vec,
                    642:                    cfattach_ioconf_ld_virtio, cfdata_ioconf_ld_virtio);
                    643:                break;
                    644:        default:
                    645:                error = ENOTTY;
                    646:                break;
                    647:        }
                    648: #endif
                    649:
                    650:        return error;
                    651: }

CVSweb <webmaster@jp.NetBSD.org>