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_DUMPING 0x0010
80 #define VTBLK_FLAG_BARRIER 0x0020
81 #define VTBLK_FLAG_WC_CONFIG 0x0040
83 struct virtqueue *vtblk_vq;
84 struct sglist *vtblk_sglist;
85 struct disk *vtblk_disk;
87 struct bio_queue_head vtblk_bioq;
88 TAILQ_HEAD(, vtblk_request)
90 TAILQ_HEAD(, vtblk_request)
92 struct vtblk_request *vtblk_req_ordered;
95 int vtblk_request_count;
96 enum vtblk_cache_mode vtblk_write_cache;
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 *, int);
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_prepare(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 *);
184 static void vtblk_set_write_cache(struct vtblk_softc *, int);
185 static int vtblk_write_cache_enabled(struct vtblk_softc *sc,
186 struct virtio_blk_config *);
187 static int vtblk_write_cache_sysctl(SYSCTL_HANDLER_ARGS);
189 static void vtblk_setup_sysctl(struct vtblk_softc *);
190 static int vtblk_tunable_int(struct vtblk_softc *, const char *, int);
193 static int vtblk_no_ident = 0;
194 TUNABLE_INT("hw.vtblk.no_ident", &vtblk_no_ident);
195 static int vtblk_writecache_mode = -1;
196 TUNABLE_INT("hw.vtblk.writecache_mode", &vtblk_writecache_mode);
198 /* Features desired/implemented by this driver. */
199 #define VTBLK_FEATURES \
200 (VIRTIO_BLK_F_BARRIER | \
201 VIRTIO_BLK_F_SIZE_MAX | \
202 VIRTIO_BLK_F_SEG_MAX | \
203 VIRTIO_BLK_F_GEOMETRY | \
205 VIRTIO_BLK_F_BLK_SIZE | \
207 VIRTIO_BLK_F_TOPOLOGY | \
208 VIRTIO_BLK_F_CONFIG_WCE | \
209 VIRTIO_RING_F_INDIRECT_DESC)
211 #define VTBLK_MTX(_sc) &(_sc)->vtblk_mtx
212 #define VTBLK_LOCK_INIT(_sc, _name) \
213 mtx_init(VTBLK_MTX((_sc)), (_name), \
214 "VirtIO Block Lock", MTX_DEF)
215 #define VTBLK_LOCK(_sc) mtx_lock(VTBLK_MTX((_sc)))
216 #define VTBLK_UNLOCK(_sc) mtx_unlock(VTBLK_MTX((_sc)))
217 #define VTBLK_LOCK_DESTROY(_sc) mtx_destroy(VTBLK_MTX((_sc)))
218 #define VTBLK_LOCK_ASSERT(_sc) mtx_assert(VTBLK_MTX((_sc)), MA_OWNED)
219 #define VTBLK_LOCK_ASSERT_NOTOWNED(_sc) \
220 mtx_assert(VTBLK_MTX((_sc)), MA_NOTOWNED)
222 #define VTBLK_DISK_NAME "vtbd"
223 #define VTBLK_QUIESCE_TIMEOUT (30 * hz)
226 * Each block request uses at least two segments - one for the header
227 * and one for the status.
229 #define VTBLK_MIN_SEGMENTS 2
231 static device_method_t vtblk_methods[] = {
232 /* Device methods. */
233 DEVMETHOD(device_probe, vtblk_probe),
234 DEVMETHOD(device_attach, vtblk_attach),
235 DEVMETHOD(device_detach, vtblk_detach),
236 DEVMETHOD(device_suspend, vtblk_suspend),
237 DEVMETHOD(device_resume, vtblk_resume),
238 DEVMETHOD(device_shutdown, vtblk_shutdown),
240 /* VirtIO methods. */
241 DEVMETHOD(virtio_config_change, vtblk_config_change),
246 static driver_t vtblk_driver = {
249 sizeof(struct vtblk_softc)
251 static devclass_t vtblk_devclass;
253 DRIVER_MODULE(virtio_blk, virtio_pci, vtblk_driver, vtblk_devclass,
255 MODULE_VERSION(virtio_blk, 1);
256 MODULE_DEPEND(virtio_blk, virtio, 1, 1, 1);
259 vtblk_modevent(module_t mod, int type, void *unused)
280 vtblk_probe(device_t dev)
283 if (virtio_get_device_type(dev) != VIRTIO_ID_BLOCK)
286 device_set_desc(dev, "VirtIO Block Adapter");
288 return (BUS_PROBE_DEFAULT);
292 vtblk_attach(device_t dev)
294 struct vtblk_softc *sc;
295 struct virtio_blk_config blkcfg;
298 virtio_set_feature_desc(dev, vtblk_feature_desc);
300 sc = device_get_softc(dev);
302 VTBLK_LOCK_INIT(sc, device_get_nameunit(dev));
303 bioq_init(&sc->vtblk_bioq);
304 TAILQ_INIT(&sc->vtblk_req_free);
305 TAILQ_INIT(&sc->vtblk_req_ready);
307 vtblk_setup_sysctl(sc);
308 vtblk_setup_features(sc);
310 vtblk_read_config(sc, &blkcfg);
313 * With the current sglist(9) implementation, it is not easy
314 * for us to support a maximum segment size as adjacent
315 * segments are coalesced. For now, just make sure it's larger
316 * than the maximum supported transfer size.
318 if (virtio_with_feature(dev, VIRTIO_BLK_F_SIZE_MAX)) {
319 if (blkcfg.size_max < MAXPHYS) {
321 device_printf(dev, "host requires unsupported "
322 "maximum segment size feature\n");
327 sc->vtblk_max_nsegs = vtblk_maximum_segments(sc, &blkcfg);
328 if (sc->vtblk_max_nsegs <= VTBLK_MIN_SEGMENTS) {
330 device_printf(dev, "fewer than minimum number of segments "
331 "allowed: %d\n", sc->vtblk_max_nsegs);
335 sc->vtblk_sglist = sglist_alloc(sc->vtblk_max_nsegs, M_NOWAIT);
336 if (sc->vtblk_sglist == NULL) {
338 device_printf(dev, "cannot allocate sglist\n");
342 error = vtblk_alloc_virtqueue(sc);
344 device_printf(dev, "cannot allocate virtqueue\n");
348 error = vtblk_request_prealloc(sc);
350 device_printf(dev, "cannot preallocate requests\n");
354 vtblk_alloc_disk(sc, &blkcfg);
356 error = virtio_setup_intr(dev, INTR_TYPE_BIO | INTR_ENTROPY);
358 device_printf(dev, "cannot setup virtqueue interrupt\n");
362 vtblk_create_disk(sc);
364 virtqueue_enable_intr(sc->vtblk_vq);
374 vtblk_detach(device_t dev)
376 struct vtblk_softc *sc;
378 sc = device_get_softc(dev);
381 sc->vtblk_flags |= VTBLK_FLAG_DETACH;
382 if (device_is_attached(dev))
388 if (sc->vtblk_disk != NULL) {
389 disk_destroy(sc->vtblk_disk);
390 sc->vtblk_disk = NULL;
393 if (sc->vtblk_sglist != NULL) {
394 sglist_free(sc->vtblk_sglist);
395 sc->vtblk_sglist = NULL;
398 VTBLK_LOCK_DESTROY(sc);
404 vtblk_suspend(device_t dev)
406 struct vtblk_softc *sc;
409 sc = device_get_softc(dev);
412 sc->vtblk_flags |= VTBLK_FLAG_SUSPEND;
413 /* XXX BMV: virtio_stop(), etc needed here? */
414 error = vtblk_quiesce(sc);
416 sc->vtblk_flags &= ~VTBLK_FLAG_SUSPEND;
423 vtblk_resume(device_t dev)
425 struct vtblk_softc *sc;
427 sc = device_get_softc(dev);
430 /* XXX BMV: virtio_reinit(), etc needed here? */
431 sc->vtblk_flags &= ~VTBLK_FLAG_SUSPEND;
439 vtblk_shutdown(device_t dev)
446 vtblk_config_change(device_t dev)
448 struct vtblk_softc *sc;
449 struct virtio_blk_config blkcfg;
452 sc = device_get_softc(dev);
454 vtblk_read_config(sc, &blkcfg);
456 /* Capacity is always in 512-byte units. */
457 capacity = blkcfg.capacity * 512;
459 if (sc->vtblk_disk->d_mediasize != capacity)
460 vtblk_resize_disk(sc, capacity);
466 vtblk_open(struct disk *dp)
468 struct vtblk_softc *sc;
470 if ((sc = dp->d_drv1) == NULL)
473 return (sc->vtblk_flags & VTBLK_FLAG_DETACH ? ENXIO : 0);
477 vtblk_close(struct disk *dp)
479 struct vtblk_softc *sc;
481 if ((sc = dp->d_drv1) == NULL)
488 vtblk_ioctl(struct disk *dp, u_long cmd, void *addr, int flag,
491 struct vtblk_softc *sc;
493 if ((sc = dp->d_drv1) == NULL)
500 vtblk_dump(void *arg, void *virtual, vm_offset_t physical, off_t offset,
504 struct vtblk_softc *sc;
509 if ((sc = dp->d_drv1) == NULL)
514 if ((sc->vtblk_flags & VTBLK_FLAG_DUMPING) == 0) {
515 vtblk_dump_prepare(sc);
516 sc->vtblk_flags |= VTBLK_FLAG_DUMPING;
520 error = vtblk_dump_write(sc, virtual, offset, length);
521 else if (virtual == NULL && offset == 0)
522 error = vtblk_dump_flush(sc);
525 sc->vtblk_flags &= ~VTBLK_FLAG_DUMPING;
534 vtblk_strategy(struct bio *bp)
536 struct vtblk_softc *sc;
538 if ((sc = bp->bio_disk->d_drv1) == NULL) {
539 vtblk_bio_done(NULL, bp, EINVAL);
544 * Fail any write if RO. Unfortunately, there does not seem to
545 * be a better way to report our readonly'ness to GEOM above.
547 if (sc->vtblk_flags & VTBLK_FLAG_READONLY &&
548 (bp->bio_cmd == BIO_WRITE || bp->bio_cmd == BIO_FLUSH)) {
549 vtblk_bio_done(sc, bp, EROFS);
555 if (sc->vtblk_flags & VTBLK_FLAG_DETACH) {
557 vtblk_bio_done(sc, bp, ENXIO);
561 bioq_insert_tail(&sc->vtblk_bioq, bp);
568 vtblk_negotiate_features(struct vtblk_softc *sc)
574 features = VTBLK_FEATURES;
576 sc->vtblk_features = virtio_negotiate_features(dev, features);
580 vtblk_setup_features(struct vtblk_softc *sc)
586 vtblk_negotiate_features(sc);
588 if (virtio_with_feature(dev, VIRTIO_RING_F_INDIRECT_DESC))
589 sc->vtblk_flags |= VTBLK_FLAG_INDIRECT;
590 if (virtio_with_feature(dev, VIRTIO_BLK_F_RO))
591 sc->vtblk_flags |= VTBLK_FLAG_READONLY;
592 if (virtio_with_feature(dev, VIRTIO_BLK_F_BARRIER))
593 sc->vtblk_flags |= VTBLK_FLAG_BARRIER;
594 if (virtio_with_feature(dev, VIRTIO_BLK_F_CONFIG_WCE))
595 sc->vtblk_flags |= VTBLK_FLAG_WC_CONFIG;
599 vtblk_maximum_segments(struct vtblk_softc *sc,
600 struct virtio_blk_config *blkcfg)
606 nsegs = VTBLK_MIN_SEGMENTS;
608 if (virtio_with_feature(dev, VIRTIO_BLK_F_SEG_MAX)) {
609 nsegs += MIN(blkcfg->seg_max, MAXPHYS / PAGE_SIZE + 1);
610 if (sc->vtblk_flags & VTBLK_FLAG_INDIRECT)
611 nsegs = MIN(nsegs, VIRTIO_MAX_INDIRECT);
619 vtblk_alloc_virtqueue(struct vtblk_softc *sc)
622 struct vq_alloc_info vq_info;
626 VQ_ALLOC_INFO_INIT(&vq_info, sc->vtblk_max_nsegs,
627 vtblk_vq_intr, sc, &sc->vtblk_vq,
628 "%s request", device_get_nameunit(dev));
630 return (virtio_alloc_virtqueues(dev, 0, 1, &vq_info));
634 vtblk_resize_disk(struct vtblk_softc *sc, uint64_t new_capacity)
643 dp->d_mediasize = new_capacity;
645 device_printf(dev, "resized to %juMB (%ju %u byte sectors)\n",
646 (uintmax_t) dp->d_mediasize >> 20,
647 (uintmax_t) dp->d_mediasize / dp->d_sectorsize,
651 error = disk_resize(dp, M_NOWAIT);
654 "disk_resize(9) failed, error: %d\n", error);
659 vtblk_alloc_disk(struct vtblk_softc *sc, struct virtio_blk_config *blkcfg)
666 sc->vtblk_disk = dp = disk_alloc();
667 dp->d_open = vtblk_open;
668 dp->d_close = vtblk_close;
669 dp->d_ioctl = vtblk_ioctl;
670 dp->d_strategy = vtblk_strategy;
671 dp->d_name = VTBLK_DISK_NAME;
672 dp->d_unit = device_get_unit(dev);
674 dp->d_flags = DISKFLAG_CANFLUSHCACHE | DISKFLAG_UNMAPPED_BIO |
675 DISKFLAG_DIRECT_COMPLETION;
676 dp->d_hba_vendor = virtio_get_vendor(dev);
677 dp->d_hba_device = virtio_get_device(dev);
678 dp->d_hba_subvendor = virtio_get_subvendor(dev);
679 dp->d_hba_subdevice = virtio_get_subdevice(dev);
681 if ((sc->vtblk_flags & VTBLK_FLAG_READONLY) == 0)
682 dp->d_dump = vtblk_dump;
684 /* Capacity is always in 512-byte units. */
685 dp->d_mediasize = blkcfg->capacity * 512;
687 if (virtio_with_feature(dev, VIRTIO_BLK_F_BLK_SIZE))
688 dp->d_sectorsize = blkcfg->blk_size;
690 dp->d_sectorsize = 512;
693 * The VirtIO maximum I/O size is given in terms of segments.
694 * However, FreeBSD limits I/O size by logical buffer size, not
695 * by physically contiguous pages. Therefore, we have to assume
696 * no pages are contiguous. This may impose an artificially low
697 * maximum I/O size. But in practice, since QEMU advertises 128
698 * segments, this gives us a maximum IO size of 125 * PAGE_SIZE,
699 * which is typically greater than MAXPHYS. Eventually we should
700 * just advertise MAXPHYS and split buffers that are too big.
702 * Note we must subtract one additional segment in case of non
703 * page aligned buffers.
705 dp->d_maxsize = (sc->vtblk_max_nsegs - VTBLK_MIN_SEGMENTS - 1) *
707 if (dp->d_maxsize < PAGE_SIZE)
708 dp->d_maxsize = PAGE_SIZE; /* XXX */
710 if (virtio_with_feature(dev, VIRTIO_BLK_F_GEOMETRY)) {
711 dp->d_fwsectors = blkcfg->geometry.sectors;
712 dp->d_fwheads = blkcfg->geometry.heads;
715 if (virtio_with_feature(dev, VIRTIO_BLK_F_TOPOLOGY)) {
716 dp->d_stripesize = dp->d_sectorsize *
717 (1 << blkcfg->topology.physical_block_exp);
718 dp->d_stripeoffset = (dp->d_stripesize -
719 blkcfg->topology.alignment_offset * dp->d_sectorsize) %
723 if (vtblk_write_cache_enabled(sc, blkcfg) != 0)
724 sc->vtblk_write_cache = VTBLK_CACHE_WRITEBACK;
726 sc->vtblk_write_cache = VTBLK_CACHE_WRITETHROUGH;
730 vtblk_create_disk(struct vtblk_softc *sc)
738 device_printf(sc->vtblk_dev, "%juMB (%ju %u byte sectors)\n",
739 (uintmax_t) dp->d_mediasize >> 20,
740 (uintmax_t) dp->d_mediasize / dp->d_sectorsize,
743 disk_create(dp, DISK_VERSION);
747 vtblk_request_prealloc(struct vtblk_softc *sc)
749 struct vtblk_request *req;
752 nreqs = virtqueue_size(sc->vtblk_vq);
755 * Preallocate sufficient requests to keep the virtqueue full. Each
756 * request consumes VTBLK_MIN_SEGMENTS or more descriptors so reduce
757 * the number allocated when indirect descriptors are not available.
759 if ((sc->vtblk_flags & VTBLK_FLAG_INDIRECT) == 0)
760 nreqs /= VTBLK_MIN_SEGMENTS;
762 for (i = 0; i < nreqs; i++) {
763 req = malloc(sizeof(struct vtblk_request), M_DEVBUF, M_NOWAIT);
767 MPASS(sglist_count(&req->vbr_hdr, sizeof(req->vbr_hdr)) == 1);
768 MPASS(sglist_count(&req->vbr_ack, sizeof(req->vbr_ack)) == 1);
770 sc->vtblk_request_count++;
771 vtblk_request_enqueue(sc, req);
778 vtblk_request_free(struct vtblk_softc *sc)
780 struct vtblk_request *req;
782 MPASS(TAILQ_EMPTY(&sc->vtblk_req_ready));
784 while ((req = vtblk_request_dequeue(sc)) != NULL) {
785 sc->vtblk_request_count--;
789 KASSERT(sc->vtblk_request_count == 0,
790 ("%s: leaked %d requests", __func__, sc->vtblk_request_count));
793 static struct vtblk_request *
794 vtblk_request_dequeue(struct vtblk_softc *sc)
796 struct vtblk_request *req;
798 req = TAILQ_FIRST(&sc->vtblk_req_free);
800 TAILQ_REMOVE(&sc->vtblk_req_free, req, vbr_link);
801 bzero(req, sizeof(struct vtblk_request));
808 vtblk_request_enqueue(struct vtblk_softc *sc, struct vtblk_request *req)
811 TAILQ_INSERT_HEAD(&sc->vtblk_req_free, req, vbr_link);
814 static struct vtblk_request *
815 vtblk_request_next_ready(struct vtblk_softc *sc)
817 struct vtblk_request *req;
819 req = TAILQ_FIRST(&sc->vtblk_req_ready);
821 TAILQ_REMOVE(&sc->vtblk_req_ready, req, vbr_link);
827 vtblk_request_requeue_ready(struct vtblk_softc *sc, struct vtblk_request *req)
830 /* NOTE: Currently, there will be at most one request in the queue. */
831 TAILQ_INSERT_HEAD(&sc->vtblk_req_ready, req, vbr_link);
834 static struct vtblk_request *
835 vtblk_request_next(struct vtblk_softc *sc)
837 struct vtblk_request *req;
839 req = vtblk_request_next_ready(sc);
843 return (vtblk_request_bio(sc));
846 static struct vtblk_request *
847 vtblk_request_bio(struct vtblk_softc *sc)
849 struct bio_queue_head *bioq;
850 struct vtblk_request *req;
853 bioq = &sc->vtblk_bioq;
855 if (bioq_first(bioq) == NULL)
858 req = vtblk_request_dequeue(sc);
862 bp = bioq_takefirst(bioq);
865 req->vbr_hdr.ioprio = 1;
867 switch (bp->bio_cmd) {
869 req->vbr_hdr.type = VIRTIO_BLK_T_FLUSH;
872 req->vbr_hdr.type = VIRTIO_BLK_T_IN;
873 req->vbr_hdr.sector = bp->bio_offset / 512;
876 req->vbr_hdr.type = VIRTIO_BLK_T_OUT;
877 req->vbr_hdr.sector = bp->bio_offset / 512;
880 panic("%s: bio with unhandled cmd: %d", __func__, bp->bio_cmd);
883 if (bp->bio_flags & BIO_ORDERED)
884 req->vbr_hdr.type |= VIRTIO_BLK_T_BARRIER;
890 vtblk_request_execute(struct vtblk_softc *sc, struct vtblk_request *req)
892 struct virtqueue *vq;
895 int ordered, readable, writable, error;
898 sg = sc->vtblk_sglist;
904 * Some hosts (such as bhyve) do not implement the barrier feature,
905 * so we emulate it in the driver by allowing the barrier request
906 * to be the only one in flight.
908 if ((sc->vtblk_flags & VTBLK_FLAG_BARRIER) == 0) {
909 if (sc->vtblk_req_ordered != NULL)
911 if (bp->bio_flags & BIO_ORDERED) {
912 if (!virtqueue_empty(vq))
915 req->vbr_hdr.type &= ~VIRTIO_BLK_T_BARRIER;
920 sglist_append(sg, &req->vbr_hdr, sizeof(struct virtio_blk_outhdr));
922 if (bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE) {
923 error = sglist_append_bio(sg, bp);
924 if (error || sg->sg_nseg == sg->sg_maxseg) {
925 panic("%s: bio %p data buffer too big %d",
926 __func__, bp, error);
929 /* BIO_READ means the host writes into our buffer. */
930 if (bp->bio_cmd == BIO_READ)
931 writable = sg->sg_nseg - 1;
935 sglist_append(sg, &req->vbr_ack, sizeof(uint8_t));
936 readable = sg->sg_nseg - writable;
938 error = virtqueue_enqueue(vq, req, sg, readable, writable);
939 if (error == 0 && ordered)
940 sc->vtblk_req_ordered = req;
946 vtblk_request_error(struct vtblk_request *req)
950 switch (req->vbr_ack) {
951 case VIRTIO_BLK_S_OK:
954 case VIRTIO_BLK_S_UNSUPP:
966 vtblk_queue_completed(struct vtblk_softc *sc, struct bio_queue *queue)
968 struct vtblk_request *req;
971 while ((req = virtqueue_dequeue(sc->vtblk_vq, NULL)) != NULL) {
972 if (sc->vtblk_req_ordered != NULL) {
973 MPASS(sc->vtblk_req_ordered == req);
974 sc->vtblk_req_ordered = NULL;
978 bp->bio_error = vtblk_request_error(req);
979 TAILQ_INSERT_TAIL(queue, bp, bio_queue);
981 vtblk_request_enqueue(sc, req);
986 vtblk_done_completed(struct vtblk_softc *sc, struct bio_queue *queue)
988 struct bio *bp, *tmp;
990 TAILQ_FOREACH_SAFE(bp, queue, bio_queue, tmp) {
991 if (bp->bio_error != 0)
992 disk_err(bp, "hard error", -1, 1);
993 vtblk_bio_done(sc, bp, bp->bio_error);
998 vtblk_drain_vq(struct vtblk_softc *sc, int skip_done)
1000 struct virtqueue *vq;
1001 struct vtblk_request *req;
1007 while ((req = virtqueue_drain(vq, &last)) != NULL) {
1009 vtblk_bio_done(sc, req->vbr_bp, ENXIO);
1011 vtblk_request_enqueue(sc, req);
1014 sc->vtblk_req_ordered = NULL;
1015 KASSERT(virtqueue_empty(vq), ("virtqueue not empty"));
1019 vtblk_drain(struct vtblk_softc *sc)
1021 struct bio_queue queue;
1022 struct bio_queue_head *bioq;
1023 struct vtblk_request *req;
1026 bioq = &sc->vtblk_bioq;
1029 if (sc->vtblk_vq != NULL) {
1030 vtblk_queue_completed(sc, &queue);
1031 vtblk_done_completed(sc, &queue);
1033 vtblk_drain_vq(sc, 0);
1036 while ((req = vtblk_request_next_ready(sc)) != NULL) {
1037 vtblk_bio_done(sc, req->vbr_bp, ENXIO);
1038 vtblk_request_enqueue(sc, req);
1041 while (bioq_first(bioq) != NULL) {
1042 bp = bioq_takefirst(bioq);
1043 vtblk_bio_done(sc, bp, ENXIO);
1046 vtblk_request_free(sc);
1050 vtblk_startio(struct vtblk_softc *sc)
1052 struct virtqueue *vq;
1053 struct vtblk_request *req;
1056 VTBLK_LOCK_ASSERT(sc);
1060 if (sc->vtblk_flags & VTBLK_FLAG_SUSPEND)
1063 while (!virtqueue_full(vq)) {
1064 req = vtblk_request_next(sc);
1068 if (vtblk_request_execute(sc, req) != 0) {
1069 vtblk_request_requeue_ready(sc, req);
1077 virtqueue_notify(vq);
1081 vtblk_bio_done(struct vtblk_softc *sc, struct bio *bp, int error)
1084 /* Because of GEOM direct dispatch, we cannot hold any locks. */
1086 VTBLK_LOCK_ASSERT_NOTOWNED(sc);
1089 bp->bio_resid = bp->bio_bcount;
1090 bp->bio_error = error;
1091 bp->bio_flags |= BIO_ERROR;
1097 #define VTBLK_GET_CONFIG(_dev, _feature, _field, _cfg) \
1098 if (virtio_with_feature(_dev, _feature)) { \
1099 virtio_read_device_config(_dev, \
1100 offsetof(struct virtio_blk_config, _field), \
1101 &(_cfg)->_field, sizeof((_cfg)->_field)); \
1105 vtblk_read_config(struct vtblk_softc *sc, struct virtio_blk_config *blkcfg)
1109 dev = sc->vtblk_dev;
1111 bzero(blkcfg, sizeof(struct virtio_blk_config));
1113 /* The capacity is always available. */
1114 virtio_read_device_config(dev, offsetof(struct virtio_blk_config,
1115 capacity), &blkcfg->capacity, sizeof(blkcfg->capacity));
1117 /* Read the configuration if the feature was negotiated. */
1118 VTBLK_GET_CONFIG(dev, VIRTIO_BLK_F_SIZE_MAX, size_max, blkcfg);
1119 VTBLK_GET_CONFIG(dev, VIRTIO_BLK_F_SEG_MAX, seg_max, blkcfg);
1120 VTBLK_GET_CONFIG(dev, VIRTIO_BLK_F_GEOMETRY, geometry, blkcfg);
1121 VTBLK_GET_CONFIG(dev, VIRTIO_BLK_F_BLK_SIZE, blk_size, blkcfg);
1122 VTBLK_GET_CONFIG(dev, VIRTIO_BLK_F_TOPOLOGY, topology, blkcfg);
1123 VTBLK_GET_CONFIG(dev, VIRTIO_BLK_F_CONFIG_WCE, writeback, blkcfg);
1126 #undef VTBLK_GET_CONFIG
1129 vtblk_ident(struct vtblk_softc *sc)
1133 struct vtblk_request *req;
1136 dp = sc->vtblk_disk;
1137 len = MIN(VIRTIO_BLK_ID_BYTES, DISK_IDENT_SIZE);
1139 if (vtblk_tunable_int(sc, "no_ident", vtblk_no_ident) != 0)
1142 req = vtblk_request_dequeue(sc);
1147 req->vbr_hdr.type = VIRTIO_BLK_T_GET_ID;
1148 req->vbr_hdr.ioprio = 1;
1149 req->vbr_hdr.sector = 0;
1152 bzero(&buf, sizeof(struct bio));
1154 buf.bio_cmd = BIO_READ;
1155 buf.bio_data = dp->d_ident;
1156 buf.bio_bcount = len;
1159 error = vtblk_poll_request(sc, req);
1162 vtblk_request_enqueue(sc, req);
1165 device_printf(sc->vtblk_dev,
1166 "error getting device identifier: %d\n", error);
1171 vtblk_poll_request(struct vtblk_softc *sc, struct vtblk_request *req)
1173 struct virtqueue *vq;
1178 if (!virtqueue_empty(vq))
1181 error = vtblk_request_execute(sc, req);
1185 virtqueue_notify(vq);
1186 virtqueue_poll(vq, NULL);
1188 error = vtblk_request_error(req);
1189 if (error && bootverbose) {
1190 device_printf(sc->vtblk_dev,
1191 "%s: IO error: %d\n", __func__, error);
1198 vtblk_quiesce(struct vtblk_softc *sc)
1202 VTBLK_LOCK_ASSERT(sc);
1205 while (!virtqueue_empty(sc->vtblk_vq)) {
1206 if (mtx_sleep(&sc->vtblk_vq, VTBLK_MTX(sc), PRIBIO, "vtblkq",
1207 VTBLK_QUIESCE_TIMEOUT) == EWOULDBLOCK) {
1217 vtblk_vq_intr(void *xsc)
1219 struct vtblk_softc *sc;
1220 struct virtqueue *vq;
1221 struct bio_queue queue;
1230 if (sc->vtblk_flags & VTBLK_FLAG_DETACH)
1233 vtblk_queue_completed(sc, &queue);
1236 if (virtqueue_enable_intr(vq) != 0) {
1237 virtqueue_disable_intr(vq);
1241 if (sc->vtblk_flags & VTBLK_FLAG_SUSPEND)
1242 wakeup(&sc->vtblk_vq);
1246 vtblk_done_completed(sc, &queue);
1250 vtblk_stop(struct vtblk_softc *sc)
1253 virtqueue_disable_intr(sc->vtblk_vq);
1254 virtio_stop(sc->vtblk_dev);
1258 vtblk_dump_prepare(struct vtblk_softc *sc)
1261 struct virtqueue *vq;
1263 dev = sc->vtblk_dev;
1269 * Drain all requests caught in-flight in the virtqueue,
1270 * skipping biodone(). When dumping, only one request is
1271 * outstanding at a time, and we just poll the virtqueue
1274 vtblk_drain_vq(sc, 1);
1276 if (virtio_reinit(dev, sc->vtblk_features) != 0) {
1277 panic("%s: cannot reinit VirtIO block device during dump",
1278 device_get_nameunit(dev));
1281 virtqueue_disable_intr(vq);
1282 virtio_reinit_complete(dev);
1286 vtblk_dump_write(struct vtblk_softc *sc, void *virtual, off_t offset,
1290 struct vtblk_request *req;
1292 req = &sc->vtblk_dump_request;
1294 req->vbr_hdr.type = VIRTIO_BLK_T_OUT;
1295 req->vbr_hdr.ioprio = 1;
1296 req->vbr_hdr.sector = offset / 512;
1299 bzero(&buf, sizeof(struct bio));
1301 buf.bio_cmd = BIO_WRITE;
1302 buf.bio_data = virtual;
1303 buf.bio_bcount = length;
1305 return (vtblk_poll_request(sc, req));
1309 vtblk_dump_flush(struct vtblk_softc *sc)
1312 struct vtblk_request *req;
1314 req = &sc->vtblk_dump_request;
1316 req->vbr_hdr.type = VIRTIO_BLK_T_FLUSH;
1317 req->vbr_hdr.ioprio = 1;
1318 req->vbr_hdr.sector = 0;
1321 bzero(&buf, sizeof(struct bio));
1323 buf.bio_cmd = BIO_FLUSH;
1325 return (vtblk_poll_request(sc, req));
1329 vtblk_set_write_cache(struct vtblk_softc *sc, int wc)
1332 /* Set either writeback (1) or writethrough (0) mode. */
1333 virtio_write_dev_config_1(sc->vtblk_dev,
1334 offsetof(struct virtio_blk_config, writeback), wc);
1338 vtblk_write_cache_enabled(struct vtblk_softc *sc,
1339 struct virtio_blk_config *blkcfg)
1343 if (sc->vtblk_flags & VTBLK_FLAG_WC_CONFIG) {
1344 wc = vtblk_tunable_int(sc, "writecache_mode",
1345 vtblk_writecache_mode);
1346 if (wc >= 0 && wc < VTBLK_CACHE_MAX)
1347 vtblk_set_write_cache(sc, wc);
1349 wc = blkcfg->writeback;
1351 wc = virtio_with_feature(sc->vtblk_dev, VIRTIO_BLK_F_WCE);
1357 vtblk_write_cache_sysctl(SYSCTL_HANDLER_ARGS)
1359 struct vtblk_softc *sc;
1362 sc = oidp->oid_arg1;
1363 wc = sc->vtblk_write_cache;
1365 error = sysctl_handle_int(oidp, &wc, 0, req);
1366 if (error || req->newptr == NULL)
1368 if ((sc->vtblk_flags & VTBLK_FLAG_WC_CONFIG) == 0)
1370 if (wc < 0 || wc >= VTBLK_CACHE_MAX)
1374 sc->vtblk_write_cache = wc;
1375 vtblk_set_write_cache(sc, sc->vtblk_write_cache);
1382 vtblk_setup_sysctl(struct vtblk_softc *sc)
1385 struct sysctl_ctx_list *ctx;
1386 struct sysctl_oid *tree;
1387 struct sysctl_oid_list *child;
1389 dev = sc->vtblk_dev;
1390 ctx = device_get_sysctl_ctx(dev);
1391 tree = device_get_sysctl_tree(dev);
1392 child = SYSCTL_CHILDREN(tree);
1394 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "writecache_mode",
1395 CTLTYPE_INT | CTLFLAG_RW, sc, 0, vtblk_write_cache_sysctl,
1396 "I", "Write cache mode (writethrough (0) or writeback (1))");
1400 vtblk_tunable_int(struct vtblk_softc *sc, const char *knob, int def)
1404 snprintf(path, sizeof(path),
1405 "hw.vtblk.%d.%s", device_get_unit(sc->vtblk_dev), knob);
1406 TUNABLE_INT_FETCH(path, &def);