2 * Copyright (c) 2011, Bryan Venteicher <bryanv@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 /* Driver for VirtIO block devices. */
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
38 #include <sys/sglist.h>
39 #include <sys/sysctl.h>
41 #include <sys/mutex.h>
42 #include <sys/queue.h>
44 #include <geom/geom_disk.h>
46 #include <machine/bus.h>
47 #include <machine/resource.h>
51 #include <dev/virtio/virtio.h>
52 #include <dev/virtio/virtqueue.h>
53 #include <dev/virtio/block/virtio_blk.h>
55 #include "virtio_if.h"
57 struct vtblk_request {
58 struct virtio_blk_outhdr vbr_hdr;
61 TAILQ_ENTRY(vtblk_request) vbr_link;
64 enum vtblk_cache_mode {
65 VTBLK_CACHE_WRITETHROUGH,
66 VTBLK_CACHE_WRITEBACK,
73 uint64_t vtblk_features;
75 #define VTBLK_FLAG_INDIRECT 0x0001
76 #define VTBLK_FLAG_READONLY 0x0002
77 #define VTBLK_FLAG_DETACH 0x0004
78 #define VTBLK_FLAG_SUSPEND 0x0008
79 #define VTBLK_FLAG_BARRIER 0x0010
80 #define VTBLK_FLAG_WC_CONFIG 0x0020
82 struct virtqueue *vtblk_vq;
83 struct sglist *vtblk_sglist;
84 struct disk *vtblk_disk;
86 struct bio_queue_head vtblk_bioq;
87 TAILQ_HEAD(, vtblk_request)
89 TAILQ_HEAD(, vtblk_request)
91 struct vtblk_request *vtblk_req_ordered;
94 int vtblk_request_count;
95 enum vtblk_cache_mode vtblk_write_cache;
97 struct bio_queue vtblk_dump_queue;
98 struct vtblk_request vtblk_dump_request;
101 static struct virtio_feature_desc vtblk_feature_desc[] = {
102 { VIRTIO_BLK_F_BARRIER, "HostBarrier" },
103 { VIRTIO_BLK_F_SIZE_MAX, "MaxSegSize" },
104 { VIRTIO_BLK_F_SEG_MAX, "MaxNumSegs" },
105 { VIRTIO_BLK_F_GEOMETRY, "DiskGeometry" },
106 { VIRTIO_BLK_F_RO, "ReadOnly" },
107 { VIRTIO_BLK_F_BLK_SIZE, "BlockSize" },
108 { VIRTIO_BLK_F_SCSI, "SCSICmds" },
109 { VIRTIO_BLK_F_WCE, "WriteCache" },
110 { VIRTIO_BLK_F_TOPOLOGY, "Topology" },
111 { VIRTIO_BLK_F_CONFIG_WCE, "ConfigWCE" },
116 static int vtblk_modevent(module_t, int, void *);
118 static int vtblk_probe(device_t);
119 static int vtblk_attach(device_t);
120 static int vtblk_detach(device_t);
121 static int vtblk_suspend(device_t);
122 static int vtblk_resume(device_t);
123 static int vtblk_shutdown(device_t);
124 static int vtblk_config_change(device_t);
126 static int vtblk_open(struct disk *);
127 static int vtblk_close(struct disk *);
128 static int vtblk_ioctl(struct disk *, u_long, void *, int,
130 static int vtblk_dump(void *, void *, vm_offset_t, off_t, size_t);
131 static void vtblk_strategy(struct bio *);
133 static void vtblk_negotiate_features(struct vtblk_softc *);
134 static void vtblk_setup_features(struct vtblk_softc *);
135 static int vtblk_maximum_segments(struct vtblk_softc *,
136 struct virtio_blk_config *);
137 static int vtblk_alloc_virtqueue(struct vtblk_softc *);
138 static void vtblk_resize_disk(struct vtblk_softc *, uint64_t);
139 static void vtblk_alloc_disk(struct vtblk_softc *,
140 struct virtio_blk_config *);
141 static void vtblk_create_disk(struct vtblk_softc *);
143 static int vtblk_request_prealloc(struct vtblk_softc *);
144 static void vtblk_request_free(struct vtblk_softc *);
145 static struct vtblk_request *
146 vtblk_request_dequeue(struct vtblk_softc *);
147 static void vtblk_request_enqueue(struct vtblk_softc *,
148 struct vtblk_request *);
149 static struct vtblk_request *
150 vtblk_request_next_ready(struct vtblk_softc *);
151 static void vtblk_request_requeue_ready(struct vtblk_softc *,
152 struct vtblk_request *);
153 static struct vtblk_request *
154 vtblk_request_next(struct vtblk_softc *);
155 static struct vtblk_request *
156 vtblk_request_bio(struct vtblk_softc *);
157 static int vtblk_request_execute(struct vtblk_softc *,
158 struct vtblk_request *);
159 static int vtblk_request_error(struct vtblk_request *);
161 static void vtblk_queue_completed(struct vtblk_softc *,
163 static void vtblk_done_completed(struct vtblk_softc *,
165 static void vtblk_drain_vq(struct vtblk_softc *);
166 static void vtblk_drain(struct vtblk_softc *);
168 static void vtblk_startio(struct vtblk_softc *);
169 static void vtblk_bio_done(struct vtblk_softc *, struct bio *, int);
171 static void vtblk_read_config(struct vtblk_softc *,
172 struct virtio_blk_config *);
173 static void vtblk_ident(struct vtblk_softc *);
174 static int vtblk_poll_request(struct vtblk_softc *,
175 struct vtblk_request *);
176 static int vtblk_quiesce(struct vtblk_softc *);
177 static void vtblk_vq_intr(void *);
178 static void vtblk_stop(struct vtblk_softc *);
180 static void vtblk_dump_quiesce(struct vtblk_softc *);
181 static int vtblk_dump_write(struct vtblk_softc *, void *, off_t, size_t);
182 static int vtblk_dump_flush(struct vtblk_softc *);
183 static void vtblk_dump_complete(struct vtblk_softc *);
185 static void vtblk_set_write_cache(struct vtblk_softc *, int);
186 static int vtblk_write_cache_enabled(struct vtblk_softc *sc,
187 struct virtio_blk_config *);
188 static int vtblk_write_cache_sysctl(SYSCTL_HANDLER_ARGS);
190 static void vtblk_setup_sysctl(struct vtblk_softc *);
191 static int vtblk_tunable_int(struct vtblk_softc *, const char *, int);
194 static int vtblk_no_ident = 0;
195 TUNABLE_INT("hw.vtblk.no_ident", &vtblk_no_ident);
196 static int vtblk_writecache_mode = -1;
197 TUNABLE_INT("hw.vtblk.writecache_mode", &vtblk_writecache_mode);
199 /* Features desired/implemented by this driver. */
200 #define VTBLK_FEATURES \
201 (VIRTIO_BLK_F_BARRIER | \
202 VIRTIO_BLK_F_SIZE_MAX | \
203 VIRTIO_BLK_F_SEG_MAX | \
204 VIRTIO_BLK_F_GEOMETRY | \
206 VIRTIO_BLK_F_BLK_SIZE | \
208 VIRTIO_BLK_F_TOPOLOGY | \
209 VIRTIO_BLK_F_CONFIG_WCE | \
210 VIRTIO_RING_F_INDIRECT_DESC)
212 #define VTBLK_MTX(_sc) &(_sc)->vtblk_mtx
213 #define VTBLK_LOCK_INIT(_sc, _name) \
214 mtx_init(VTBLK_MTX((_sc)), (_name), \
215 "VirtIO Block Lock", MTX_DEF)
216 #define VTBLK_LOCK(_sc) mtx_lock(VTBLK_MTX((_sc)))
217 #define VTBLK_UNLOCK(_sc) mtx_unlock(VTBLK_MTX((_sc)))
218 #define VTBLK_LOCK_DESTROY(_sc) mtx_destroy(VTBLK_MTX((_sc)))
219 #define VTBLK_LOCK_ASSERT(_sc) mtx_assert(VTBLK_MTX((_sc)), MA_OWNED)
220 #define VTBLK_LOCK_ASSERT_NOTOWNED(_sc) \
221 mtx_assert(VTBLK_MTX((_sc)), MA_NOTOWNED)
223 #define VTBLK_DISK_NAME "vtbd"
224 #define VTBLK_QUIESCE_TIMEOUT (30 * hz)
227 * Each block request uses at least two segments - one for the header
228 * and one for the status.
230 #define VTBLK_MIN_SEGMENTS 2
232 static device_method_t vtblk_methods[] = {
233 /* Device methods. */
234 DEVMETHOD(device_probe, vtblk_probe),
235 DEVMETHOD(device_attach, vtblk_attach),
236 DEVMETHOD(device_detach, vtblk_detach),
237 DEVMETHOD(device_suspend, vtblk_suspend),
238 DEVMETHOD(device_resume, vtblk_resume),
239 DEVMETHOD(device_shutdown, vtblk_shutdown),
241 /* VirtIO methods. */
242 DEVMETHOD(virtio_config_change, vtblk_config_change),
247 static driver_t vtblk_driver = {
250 sizeof(struct vtblk_softc)
252 static devclass_t vtblk_devclass;
254 DRIVER_MODULE(virtio_blk, virtio_pci, vtblk_driver, vtblk_devclass,
256 MODULE_VERSION(virtio_blk, 1);
257 MODULE_DEPEND(virtio_blk, virtio, 1, 1, 1);
260 vtblk_modevent(module_t mod, int type, void *unused)
281 vtblk_probe(device_t dev)
284 if (virtio_get_device_type(dev) != VIRTIO_ID_BLOCK)
287 device_set_desc(dev, "VirtIO Block Adapter");
289 return (BUS_PROBE_DEFAULT);
293 vtblk_attach(device_t dev)
295 struct vtblk_softc *sc;
296 struct virtio_blk_config blkcfg;
299 virtio_set_feature_desc(dev, vtblk_feature_desc);
301 sc = device_get_softc(dev);
303 VTBLK_LOCK_INIT(sc, device_get_nameunit(dev));
304 bioq_init(&sc->vtblk_bioq);
305 TAILQ_INIT(&sc->vtblk_dump_queue);
306 TAILQ_INIT(&sc->vtblk_req_free);
307 TAILQ_INIT(&sc->vtblk_req_ready);
309 vtblk_setup_sysctl(sc);
310 vtblk_setup_features(sc);
312 vtblk_read_config(sc, &blkcfg);
315 * With the current sglist(9) implementation, it is not easy
316 * for us to support a maximum segment size as adjacent
317 * segments are coalesced. For now, just make sure it's larger
318 * than the maximum supported transfer size.
320 if (virtio_with_feature(dev, VIRTIO_BLK_F_SIZE_MAX)) {
321 if (blkcfg.size_max < MAXPHYS) {
323 device_printf(dev, "host requires unsupported "
324 "maximum segment size feature\n");
329 sc->vtblk_max_nsegs = vtblk_maximum_segments(sc, &blkcfg);
330 if (sc->vtblk_max_nsegs <= VTBLK_MIN_SEGMENTS) {
332 device_printf(dev, "fewer than minimum number of segments "
333 "allowed: %d\n", sc->vtblk_max_nsegs);
337 sc->vtblk_sglist = sglist_alloc(sc->vtblk_max_nsegs, M_NOWAIT);
338 if (sc->vtblk_sglist == NULL) {
340 device_printf(dev, "cannot allocate sglist\n");
344 error = vtblk_alloc_virtqueue(sc);
346 device_printf(dev, "cannot allocate virtqueue\n");
350 error = vtblk_request_prealloc(sc);
352 device_printf(dev, "cannot preallocate requests\n");
356 vtblk_alloc_disk(sc, &blkcfg);
358 error = virtio_setup_intr(dev, INTR_TYPE_BIO | INTR_ENTROPY);
360 device_printf(dev, "cannot setup virtqueue interrupt\n");
364 vtblk_create_disk(sc);
366 virtqueue_enable_intr(sc->vtblk_vq);
376 vtblk_detach(device_t dev)
378 struct vtblk_softc *sc;
380 sc = device_get_softc(dev);
383 sc->vtblk_flags |= VTBLK_FLAG_DETACH;
384 if (device_is_attached(dev))
390 if (sc->vtblk_disk != NULL) {
391 disk_destroy(sc->vtblk_disk);
392 sc->vtblk_disk = NULL;
395 if (sc->vtblk_sglist != NULL) {
396 sglist_free(sc->vtblk_sglist);
397 sc->vtblk_sglist = NULL;
400 VTBLK_LOCK_DESTROY(sc);
406 vtblk_suspend(device_t dev)
408 struct vtblk_softc *sc;
411 sc = device_get_softc(dev);
414 sc->vtblk_flags |= VTBLK_FLAG_SUSPEND;
415 /* XXX BMV: virtio_stop(), etc needed here? */
416 error = vtblk_quiesce(sc);
418 sc->vtblk_flags &= ~VTBLK_FLAG_SUSPEND;
425 vtblk_resume(device_t dev)
427 struct vtblk_softc *sc;
429 sc = device_get_softc(dev);
432 /* XXX BMV: virtio_reinit(), etc needed here? */
433 sc->vtblk_flags &= ~VTBLK_FLAG_SUSPEND;
441 vtblk_shutdown(device_t dev)
448 vtblk_config_change(device_t dev)
450 struct vtblk_softc *sc;
451 struct virtio_blk_config blkcfg;
454 sc = device_get_softc(dev);
456 vtblk_read_config(sc, &blkcfg);
458 /* Capacity is always in 512-byte units. */
459 capacity = blkcfg.capacity * 512;
461 if (sc->vtblk_disk->d_mediasize != capacity)
462 vtblk_resize_disk(sc, capacity);
468 vtblk_open(struct disk *dp)
470 struct vtblk_softc *sc;
472 if ((sc = dp->d_drv1) == NULL)
475 return (sc->vtblk_flags & VTBLK_FLAG_DETACH ? ENXIO : 0);
479 vtblk_close(struct disk *dp)
481 struct vtblk_softc *sc;
483 if ((sc = dp->d_drv1) == NULL)
490 vtblk_ioctl(struct disk *dp, u_long cmd, void *addr, int flag,
493 struct vtblk_softc *sc;
495 if ((sc = dp->d_drv1) == NULL)
502 vtblk_dump(void *arg, void *virtual, vm_offset_t physical, off_t offset,
506 struct vtblk_softc *sc;
512 if ((sc = dp->d_drv1) == NULL)
517 vtblk_dump_quiesce(sc);
520 error = vtblk_dump_write(sc, virtual, offset, length);
521 if (error || (virtual == NULL && offset == 0))
522 vtblk_dump_complete(sc);
530 vtblk_strategy(struct bio *bp)
532 struct vtblk_softc *sc;
534 if ((sc = bp->bio_disk->d_drv1) == NULL) {
535 vtblk_bio_done(NULL, bp, EINVAL);
540 * Fail any write if RO. Unfortunately, there does not seem to
541 * be a better way to report our readonly'ness to GEOM above.
543 if (sc->vtblk_flags & VTBLK_FLAG_READONLY &&
544 (bp->bio_cmd == BIO_WRITE || bp->bio_cmd == BIO_FLUSH)) {
545 vtblk_bio_done(sc, bp, EROFS);
551 if (sc->vtblk_flags & VTBLK_FLAG_DETACH) {
553 vtblk_bio_done(sc, bp, ENXIO);
557 bioq_insert_tail(&sc->vtblk_bioq, bp);
564 vtblk_negotiate_features(struct vtblk_softc *sc)
570 features = VTBLK_FEATURES;
572 sc->vtblk_features = virtio_negotiate_features(dev, features);
576 vtblk_setup_features(struct vtblk_softc *sc)
582 vtblk_negotiate_features(sc);
584 if (virtio_with_feature(dev, VIRTIO_RING_F_INDIRECT_DESC))
585 sc->vtblk_flags |= VTBLK_FLAG_INDIRECT;
586 if (virtio_with_feature(dev, VIRTIO_BLK_F_RO))
587 sc->vtblk_flags |= VTBLK_FLAG_READONLY;
588 if (virtio_with_feature(dev, VIRTIO_BLK_F_BARRIER))
589 sc->vtblk_flags |= VTBLK_FLAG_BARRIER;
590 if (virtio_with_feature(dev, VIRTIO_BLK_F_CONFIG_WCE))
591 sc->vtblk_flags |= VTBLK_FLAG_WC_CONFIG;
595 vtblk_maximum_segments(struct vtblk_softc *sc,
596 struct virtio_blk_config *blkcfg)
602 nsegs = VTBLK_MIN_SEGMENTS;
604 if (virtio_with_feature(dev, VIRTIO_BLK_F_SEG_MAX)) {
605 nsegs += MIN(blkcfg->seg_max, MAXPHYS / PAGE_SIZE + 1);
606 if (sc->vtblk_flags & VTBLK_FLAG_INDIRECT)
607 nsegs = MIN(nsegs, VIRTIO_MAX_INDIRECT);
615 vtblk_alloc_virtqueue(struct vtblk_softc *sc)
618 struct vq_alloc_info vq_info;
622 VQ_ALLOC_INFO_INIT(&vq_info, sc->vtblk_max_nsegs,
623 vtblk_vq_intr, sc, &sc->vtblk_vq,
624 "%s request", device_get_nameunit(dev));
626 return (virtio_alloc_virtqueues(dev, 0, 1, &vq_info));
630 vtblk_resize_disk(struct vtblk_softc *sc, uint64_t new_capacity)
639 dp->d_mediasize = new_capacity;
641 device_printf(dev, "resized to %juMB (%ju %u byte sectors)\n",
642 (uintmax_t) dp->d_mediasize >> 20,
643 (uintmax_t) dp->d_mediasize / dp->d_sectorsize,
647 error = disk_resize(dp, M_NOWAIT);
650 "disk_resize(9) failed, error: %d\n", error);
655 vtblk_alloc_disk(struct vtblk_softc *sc, struct virtio_blk_config *blkcfg)
662 sc->vtblk_disk = dp = disk_alloc();
663 dp->d_open = vtblk_open;
664 dp->d_close = vtblk_close;
665 dp->d_ioctl = vtblk_ioctl;
666 dp->d_strategy = vtblk_strategy;
667 dp->d_name = VTBLK_DISK_NAME;
668 dp->d_unit = device_get_unit(dev);
670 dp->d_flags = DISKFLAG_CANFLUSHCACHE | DISKFLAG_UNMAPPED_BIO |
671 DISKFLAG_DIRECT_COMPLETION;
672 dp->d_hba_vendor = virtio_get_vendor(dev);
673 dp->d_hba_device = virtio_get_device(dev);
674 dp->d_hba_subvendor = virtio_get_subvendor(dev);
675 dp->d_hba_subdevice = virtio_get_subdevice(dev);
677 if ((sc->vtblk_flags & VTBLK_FLAG_READONLY) == 0)
678 dp->d_dump = vtblk_dump;
680 /* Capacity is always in 512-byte units. */
681 dp->d_mediasize = blkcfg->capacity * 512;
683 if (virtio_with_feature(dev, VIRTIO_BLK_F_BLK_SIZE))
684 dp->d_sectorsize = blkcfg->blk_size;
686 dp->d_sectorsize = 512;
689 * The VirtIO maximum I/O size is given in terms of segments.
690 * However, FreeBSD limits I/O size by logical buffer size, not
691 * by physically contiguous pages. Therefore, we have to assume
692 * no pages are contiguous. This may impose an artificially low
693 * maximum I/O size. But in practice, since QEMU advertises 128
694 * segments, this gives us a maximum IO size of 125 * PAGE_SIZE,
695 * which is typically greater than MAXPHYS. Eventually we should
696 * just advertise MAXPHYS and split buffers that are too big.
698 * Note we must subtract one additional segment in case of non
699 * page aligned buffers.
701 dp->d_maxsize = (sc->vtblk_max_nsegs - VTBLK_MIN_SEGMENTS - 1) *
703 if (dp->d_maxsize < PAGE_SIZE)
704 dp->d_maxsize = PAGE_SIZE; /* XXX */
706 if (virtio_with_feature(dev, VIRTIO_BLK_F_GEOMETRY)) {
707 dp->d_fwsectors = blkcfg->geometry.sectors;
708 dp->d_fwheads = blkcfg->geometry.heads;
711 if (virtio_with_feature(dev, VIRTIO_BLK_F_TOPOLOGY) &&
712 blkcfg->topology.physical_block_exp > 0) {
713 dp->d_stripesize = dp->d_sectorsize *
714 (1 << blkcfg->topology.physical_block_exp);
715 dp->d_stripeoffset = (dp->d_stripesize -
716 blkcfg->topology.alignment_offset * dp->d_sectorsize) %
720 if (vtblk_write_cache_enabled(sc, blkcfg) != 0)
721 sc->vtblk_write_cache = VTBLK_CACHE_WRITEBACK;
723 sc->vtblk_write_cache = VTBLK_CACHE_WRITETHROUGH;
727 vtblk_create_disk(struct vtblk_softc *sc)
735 device_printf(sc->vtblk_dev, "%juMB (%ju %u byte sectors)\n",
736 (uintmax_t) dp->d_mediasize >> 20,
737 (uintmax_t) dp->d_mediasize / dp->d_sectorsize,
740 disk_create(dp, DISK_VERSION);
744 vtblk_request_prealloc(struct vtblk_softc *sc)
746 struct vtblk_request *req;
749 nreqs = virtqueue_size(sc->vtblk_vq);
752 * Preallocate sufficient requests to keep the virtqueue full. Each
753 * request consumes VTBLK_MIN_SEGMENTS or more descriptors so reduce
754 * the number allocated when indirect descriptors are not available.
756 if ((sc->vtblk_flags & VTBLK_FLAG_INDIRECT) == 0)
757 nreqs /= VTBLK_MIN_SEGMENTS;
759 for (i = 0; i < nreqs; i++) {
760 req = malloc(sizeof(struct vtblk_request), M_DEVBUF, M_NOWAIT);
764 MPASS(sglist_count(&req->vbr_hdr, sizeof(req->vbr_hdr)) == 1);
765 MPASS(sglist_count(&req->vbr_ack, sizeof(req->vbr_ack)) == 1);
767 sc->vtblk_request_count++;
768 vtblk_request_enqueue(sc, req);
775 vtblk_request_free(struct vtblk_softc *sc)
777 struct vtblk_request *req;
779 MPASS(TAILQ_EMPTY(&sc->vtblk_req_ready));
781 while ((req = vtblk_request_dequeue(sc)) != NULL) {
782 sc->vtblk_request_count--;
786 KASSERT(sc->vtblk_request_count == 0,
787 ("%s: leaked %d requests", __func__, sc->vtblk_request_count));
790 static struct vtblk_request *
791 vtblk_request_dequeue(struct vtblk_softc *sc)
793 struct vtblk_request *req;
795 req = TAILQ_FIRST(&sc->vtblk_req_free);
797 TAILQ_REMOVE(&sc->vtblk_req_free, req, vbr_link);
798 bzero(req, sizeof(struct vtblk_request));
805 vtblk_request_enqueue(struct vtblk_softc *sc, struct vtblk_request *req)
808 TAILQ_INSERT_HEAD(&sc->vtblk_req_free, req, vbr_link);
811 static struct vtblk_request *
812 vtblk_request_next_ready(struct vtblk_softc *sc)
814 struct vtblk_request *req;
816 req = TAILQ_FIRST(&sc->vtblk_req_ready);
818 TAILQ_REMOVE(&sc->vtblk_req_ready, req, vbr_link);
824 vtblk_request_requeue_ready(struct vtblk_softc *sc, struct vtblk_request *req)
827 /* NOTE: Currently, there will be at most one request in the queue. */
828 TAILQ_INSERT_HEAD(&sc->vtblk_req_ready, req, vbr_link);
831 static struct vtblk_request *
832 vtblk_request_next(struct vtblk_softc *sc)
834 struct vtblk_request *req;
836 req = vtblk_request_next_ready(sc);
840 return (vtblk_request_bio(sc));
843 static struct vtblk_request *
844 vtblk_request_bio(struct vtblk_softc *sc)
846 struct bio_queue_head *bioq;
847 struct vtblk_request *req;
850 bioq = &sc->vtblk_bioq;
852 if (bioq_first(bioq) == NULL)
855 req = vtblk_request_dequeue(sc);
859 bp = bioq_takefirst(bioq);
862 req->vbr_hdr.ioprio = 1;
864 switch (bp->bio_cmd) {
866 req->vbr_hdr.type = VIRTIO_BLK_T_FLUSH;
869 req->vbr_hdr.type = VIRTIO_BLK_T_IN;
870 req->vbr_hdr.sector = bp->bio_offset / 512;
873 req->vbr_hdr.type = VIRTIO_BLK_T_OUT;
874 req->vbr_hdr.sector = bp->bio_offset / 512;
877 panic("%s: bio with unhandled cmd: %d", __func__, bp->bio_cmd);
880 if (bp->bio_flags & BIO_ORDERED)
881 req->vbr_hdr.type |= VIRTIO_BLK_T_BARRIER;
887 vtblk_request_execute(struct vtblk_softc *sc, struct vtblk_request *req)
889 struct virtqueue *vq;
892 int ordered, readable, writable, error;
895 sg = sc->vtblk_sglist;
901 * Some hosts (such as bhyve) do not implement the barrier feature,
902 * so we emulate it in the driver by allowing the barrier request
903 * to be the only one in flight.
905 if ((sc->vtblk_flags & VTBLK_FLAG_BARRIER) == 0) {
906 if (sc->vtblk_req_ordered != NULL)
908 if (bp->bio_flags & BIO_ORDERED) {
909 if (!virtqueue_empty(vq))
912 req->vbr_hdr.type &= ~VIRTIO_BLK_T_BARRIER;
917 sglist_append(sg, &req->vbr_hdr, sizeof(struct virtio_blk_outhdr));
919 if (bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE) {
920 error = sglist_append_bio(sg, bp);
921 if (error || sg->sg_nseg == sg->sg_maxseg) {
922 panic("%s: bio %p data buffer too big %d",
923 __func__, bp, error);
926 /* BIO_READ means the host writes into our buffer. */
927 if (bp->bio_cmd == BIO_READ)
928 writable = sg->sg_nseg - 1;
932 sglist_append(sg, &req->vbr_ack, sizeof(uint8_t));
933 readable = sg->sg_nseg - writable;
935 error = virtqueue_enqueue(vq, req, sg, readable, writable);
936 if (error == 0 && ordered)
937 sc->vtblk_req_ordered = req;
943 vtblk_request_error(struct vtblk_request *req)
947 switch (req->vbr_ack) {
948 case VIRTIO_BLK_S_OK:
951 case VIRTIO_BLK_S_UNSUPP:
963 vtblk_queue_completed(struct vtblk_softc *sc, struct bio_queue *queue)
965 struct vtblk_request *req;
968 while ((req = virtqueue_dequeue(sc->vtblk_vq, NULL)) != NULL) {
969 if (sc->vtblk_req_ordered != NULL) {
970 MPASS(sc->vtblk_req_ordered == req);
971 sc->vtblk_req_ordered = NULL;
975 bp->bio_error = vtblk_request_error(req);
976 TAILQ_INSERT_TAIL(queue, bp, bio_queue);
978 vtblk_request_enqueue(sc, req);
983 vtblk_done_completed(struct vtblk_softc *sc, struct bio_queue *queue)
985 struct bio *bp, *tmp;
987 TAILQ_FOREACH_SAFE(bp, queue, bio_queue, tmp) {
988 if (bp->bio_error != 0)
989 disk_err(bp, "hard error", -1, 1);
990 vtblk_bio_done(sc, bp, bp->bio_error);
995 vtblk_drain_vq(struct vtblk_softc *sc)
997 struct virtqueue *vq;
998 struct vtblk_request *req;
1004 while ((req = virtqueue_drain(vq, &last)) != NULL) {
1005 vtblk_bio_done(sc, req->vbr_bp, ENXIO);
1006 vtblk_request_enqueue(sc, req);
1009 sc->vtblk_req_ordered = NULL;
1010 KASSERT(virtqueue_empty(vq), ("virtqueue not empty"));
1014 vtblk_drain(struct vtblk_softc *sc)
1016 struct bio_queue queue;
1017 struct bio_queue_head *bioq;
1018 struct vtblk_request *req;
1021 bioq = &sc->vtblk_bioq;
1024 if (sc->vtblk_vq != NULL) {
1025 vtblk_queue_completed(sc, &queue);
1026 vtblk_done_completed(sc, &queue);
1031 while ((req = vtblk_request_next_ready(sc)) != NULL) {
1032 vtblk_bio_done(sc, req->vbr_bp, ENXIO);
1033 vtblk_request_enqueue(sc, req);
1036 while (bioq_first(bioq) != NULL) {
1037 bp = bioq_takefirst(bioq);
1038 vtblk_bio_done(sc, bp, ENXIO);
1041 vtblk_request_free(sc);
1045 vtblk_startio(struct vtblk_softc *sc)
1047 struct virtqueue *vq;
1048 struct vtblk_request *req;
1051 VTBLK_LOCK_ASSERT(sc);
1055 if (sc->vtblk_flags & VTBLK_FLAG_SUSPEND)
1058 while (!virtqueue_full(vq)) {
1059 req = vtblk_request_next(sc);
1063 if (vtblk_request_execute(sc, req) != 0) {
1064 vtblk_request_requeue_ready(sc, req);
1072 virtqueue_notify(vq);
1076 vtblk_bio_done(struct vtblk_softc *sc, struct bio *bp, int error)
1079 /* Because of GEOM direct dispatch, we cannot hold any locks. */
1081 VTBLK_LOCK_ASSERT_NOTOWNED(sc);
1084 bp->bio_resid = bp->bio_bcount;
1085 bp->bio_error = error;
1086 bp->bio_flags |= BIO_ERROR;
1092 #define VTBLK_GET_CONFIG(_dev, _feature, _field, _cfg) \
1093 if (virtio_with_feature(_dev, _feature)) { \
1094 virtio_read_device_config(_dev, \
1095 offsetof(struct virtio_blk_config, _field), \
1096 &(_cfg)->_field, sizeof((_cfg)->_field)); \
1100 vtblk_read_config(struct vtblk_softc *sc, struct virtio_blk_config *blkcfg)
1104 dev = sc->vtblk_dev;
1106 bzero(blkcfg, sizeof(struct virtio_blk_config));
1108 /* The capacity is always available. */
1109 virtio_read_device_config(dev, offsetof(struct virtio_blk_config,
1110 capacity), &blkcfg->capacity, sizeof(blkcfg->capacity));
1112 /* Read the configuration if the feature was negotiated. */
1113 VTBLK_GET_CONFIG(dev, VIRTIO_BLK_F_SIZE_MAX, size_max, blkcfg);
1114 VTBLK_GET_CONFIG(dev, VIRTIO_BLK_F_SEG_MAX, seg_max, blkcfg);
1115 VTBLK_GET_CONFIG(dev, VIRTIO_BLK_F_GEOMETRY, geometry, blkcfg);
1116 VTBLK_GET_CONFIG(dev, VIRTIO_BLK_F_BLK_SIZE, blk_size, blkcfg);
1117 VTBLK_GET_CONFIG(dev, VIRTIO_BLK_F_TOPOLOGY, topology, blkcfg);
1118 VTBLK_GET_CONFIG(dev, VIRTIO_BLK_F_CONFIG_WCE, writeback, blkcfg);
1121 #undef VTBLK_GET_CONFIG
1124 vtblk_ident(struct vtblk_softc *sc)
1128 struct vtblk_request *req;
1131 dp = sc->vtblk_disk;
1132 len = MIN(VIRTIO_BLK_ID_BYTES, DISK_IDENT_SIZE);
1134 if (vtblk_tunable_int(sc, "no_ident", vtblk_no_ident) != 0)
1137 req = vtblk_request_dequeue(sc);
1142 req->vbr_hdr.type = VIRTIO_BLK_T_GET_ID;
1143 req->vbr_hdr.ioprio = 1;
1144 req->vbr_hdr.sector = 0;
1147 bzero(&buf, sizeof(struct bio));
1149 buf.bio_cmd = BIO_READ;
1150 buf.bio_data = dp->d_ident;
1151 buf.bio_bcount = len;
1154 error = vtblk_poll_request(sc, req);
1157 vtblk_request_enqueue(sc, req);
1160 device_printf(sc->vtblk_dev,
1161 "error getting device identifier: %d\n", error);
1166 vtblk_poll_request(struct vtblk_softc *sc, struct vtblk_request *req)
1168 struct virtqueue *vq;
1173 if (!virtqueue_empty(vq))
1176 error = vtblk_request_execute(sc, req);
1180 virtqueue_notify(vq);
1181 virtqueue_poll(vq, NULL);
1183 error = vtblk_request_error(req);
1184 if (error && bootverbose) {
1185 device_printf(sc->vtblk_dev,
1186 "%s: IO error: %d\n", __func__, error);
1193 vtblk_quiesce(struct vtblk_softc *sc)
1197 VTBLK_LOCK_ASSERT(sc);
1200 while (!virtqueue_empty(sc->vtblk_vq)) {
1201 if (mtx_sleep(&sc->vtblk_vq, VTBLK_MTX(sc), PRIBIO, "vtblkq",
1202 VTBLK_QUIESCE_TIMEOUT) == EWOULDBLOCK) {
1212 vtblk_vq_intr(void *xsc)
1214 struct vtblk_softc *sc;
1215 struct virtqueue *vq;
1216 struct bio_queue queue;
1225 if (sc->vtblk_flags & VTBLK_FLAG_DETACH)
1228 vtblk_queue_completed(sc, &queue);
1231 if (virtqueue_enable_intr(vq) != 0) {
1232 virtqueue_disable_intr(vq);
1236 if (sc->vtblk_flags & VTBLK_FLAG_SUSPEND)
1237 wakeup(&sc->vtblk_vq);
1241 vtblk_done_completed(sc, &queue);
1245 vtblk_stop(struct vtblk_softc *sc)
1248 virtqueue_disable_intr(sc->vtblk_vq);
1249 virtio_stop(sc->vtblk_dev);
1253 vtblk_dump_quiesce(struct vtblk_softc *sc)
1257 * Spin here until all the requests in-flight at the time of the
1258 * dump are completed and queued. The queued requests will be
1259 * biodone'd once the dump is finished.
1261 while (!virtqueue_empty(sc->vtblk_vq))
1262 vtblk_queue_completed(sc, &sc->vtblk_dump_queue);
1266 vtblk_dump_write(struct vtblk_softc *sc, void *virtual, off_t offset,
1270 struct vtblk_request *req;
1272 req = &sc->vtblk_dump_request;
1274 req->vbr_hdr.type = VIRTIO_BLK_T_OUT;
1275 req->vbr_hdr.ioprio = 1;
1276 req->vbr_hdr.sector = offset / 512;
1279 bzero(&buf, sizeof(struct bio));
1281 buf.bio_cmd = BIO_WRITE;
1282 buf.bio_data = virtual;
1283 buf.bio_bcount = length;
1285 return (vtblk_poll_request(sc, req));
1289 vtblk_dump_flush(struct vtblk_softc *sc)
1292 struct vtblk_request *req;
1294 req = &sc->vtblk_dump_request;
1296 req->vbr_hdr.type = VIRTIO_BLK_T_FLUSH;
1297 req->vbr_hdr.ioprio = 1;
1298 req->vbr_hdr.sector = 0;
1301 bzero(&buf, sizeof(struct bio));
1303 buf.bio_cmd = BIO_FLUSH;
1305 return (vtblk_poll_request(sc, req));
1309 vtblk_dump_complete(struct vtblk_softc *sc)
1312 vtblk_dump_flush(sc);
1315 vtblk_done_completed(sc, &sc->vtblk_dump_queue);
1320 vtblk_set_write_cache(struct vtblk_softc *sc, int wc)
1323 /* Set either writeback (1) or writethrough (0) mode. */
1324 virtio_write_dev_config_1(sc->vtblk_dev,
1325 offsetof(struct virtio_blk_config, writeback), wc);
1329 vtblk_write_cache_enabled(struct vtblk_softc *sc,
1330 struct virtio_blk_config *blkcfg)
1334 if (sc->vtblk_flags & VTBLK_FLAG_WC_CONFIG) {
1335 wc = vtblk_tunable_int(sc, "writecache_mode",
1336 vtblk_writecache_mode);
1337 if (wc >= 0 && wc < VTBLK_CACHE_MAX)
1338 vtblk_set_write_cache(sc, wc);
1340 wc = blkcfg->writeback;
1342 wc = virtio_with_feature(sc->vtblk_dev, VIRTIO_BLK_F_WCE);
1348 vtblk_write_cache_sysctl(SYSCTL_HANDLER_ARGS)
1350 struct vtblk_softc *sc;
1353 sc = oidp->oid_arg1;
1354 wc = sc->vtblk_write_cache;
1356 error = sysctl_handle_int(oidp, &wc, 0, req);
1357 if (error || req->newptr == NULL)
1359 if ((sc->vtblk_flags & VTBLK_FLAG_WC_CONFIG) == 0)
1361 if (wc < 0 || wc >= VTBLK_CACHE_MAX)
1365 sc->vtblk_write_cache = wc;
1366 vtblk_set_write_cache(sc, sc->vtblk_write_cache);
1373 vtblk_setup_sysctl(struct vtblk_softc *sc)
1376 struct sysctl_ctx_list *ctx;
1377 struct sysctl_oid *tree;
1378 struct sysctl_oid_list *child;
1380 dev = sc->vtblk_dev;
1381 ctx = device_get_sysctl_ctx(dev);
1382 tree = device_get_sysctl_tree(dev);
1383 child = SYSCTL_CHILDREN(tree);
1385 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "writecache_mode",
1386 CTLTYPE_INT | CTLFLAG_RW, sc, 0, vtblk_write_cache_sysctl,
1387 "I", "Write cache mode (writethrough (0) or writeback (1))");
1391 vtblk_tunable_int(struct vtblk_softc *sc, const char *knob, int def)
1395 snprintf(path, sizeof(path),
1396 "hw.vtblk.%d.%s", device_get_unit(sc->vtblk_dev), knob);
1397 TUNABLE_INT_FETCH(path, &def);