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 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/malloc.h>
34 #include <sys/module.h>
37 #include <machine/bus.h>
38 #include <machine/resource.h>
42 #include <dev/virtio/virtio.h>
43 #include <dev/virtio/virtio_config.h>
44 #include <dev/virtio/virtqueue.h>
46 #include "virtio_bus_if.h"
48 static int virtio_modevent(module_t, int, void *);
49 static const char *virtio_feature_name(uint64_t, struct virtio_feature_desc *);
51 static struct virtio_ident {
54 } virtio_ident_table[] = {
55 { VIRTIO_ID_NETWORK, "Network" },
56 { VIRTIO_ID_BLOCK, "Block" },
57 { VIRTIO_ID_CONSOLE, "Console" },
58 { VIRTIO_ID_ENTROPY, "Entropy" },
59 { VIRTIO_ID_BALLOON, "Balloon" },
60 { VIRTIO_ID_IOMEMORY, "IOMemory" },
61 { VIRTIO_ID_SCSI, "SCSI" },
62 { VIRTIO_ID_9P, "9P Transport" },
67 /* Device independent features. */
68 static struct virtio_feature_desc virtio_common_feature_desc[] = {
69 { VIRTIO_F_NOTIFY_ON_EMPTY, "NotifyOnEmpty" },
70 { VIRTIO_RING_F_INDIRECT_DESC, "RingIndirect" },
71 { VIRTIO_RING_F_EVENT_IDX, "EventIdx" },
72 { VIRTIO_F_BAD_FEATURE, "BadFeature" },
78 virtio_device_name(uint16_t devid)
80 struct virtio_ident *ident;
82 for (ident = virtio_ident_table; ident->name != NULL; ident++) {
83 if (ident->devid == devid)
91 virtio_feature_name(uint64_t val, struct virtio_feature_desc *desc)
94 struct virtio_feature_desc *descs[2] = { desc,
95 virtio_common_feature_desc };
97 for (i = 0; i < 2; i++) {
101 for (j = 0; descs[i][j].vfd_val != 0; j++) {
102 if (val == descs[i][j].vfd_val)
103 return (descs[i][j].vfd_str);
111 virtio_describe(device_t dev, const char *msg,
112 uint64_t features, struct virtio_feature_desc *desc)
120 if ((buf = malloc(512, M_TEMP, M_NOWAIT)) == NULL) {
121 device_printf(dev, "%s features: %#jx\n", msg, (uintmax_t) features);
125 sbuf_new(&sb, buf, 512, SBUF_FIXEDLEN);
126 sbuf_printf(&sb, "%s features: %#jx", msg, (uintmax_t) features);
128 for (n = 0, val = 1ULL << 63; val != 0; val >>= 1) {
130 * BAD_FEATURE is used to detect broken Linux clients
131 * and therefore is not applicable to FreeBSD.
133 if (((features & val) == 0) || val == VIRTIO_F_BAD_FEATURE)
141 name = virtio_feature_name(val, desc);
143 sbuf_printf(&sb, "%#jx", (uintmax_t) val);
151 #if __FreeBSD_version < 900020
153 if (sbuf_overflowed(&sb) == 0)
155 if (sbuf_finish(&sb) == 0)
157 device_printf(dev, "%s\n", sbuf_data(&sb));
164 * VirtIO bus method wrappers.
168 virtio_read_ivar(device_t dev, int ivar, uintptr_t *val)
172 BUS_READ_IVAR(device_get_parent(dev), dev, ivar, val);
176 virtio_write_ivar(device_t dev, int ivar, uintptr_t val)
179 BUS_WRITE_IVAR(device_get_parent(dev), dev, ivar, val);
183 virtio_negotiate_features(device_t dev, uint64_t child_features)
186 return (VIRTIO_BUS_NEGOTIATE_FEATURES(device_get_parent(dev),
191 virtio_alloc_virtqueues(device_t dev, int flags, int nvqs,
192 struct vq_alloc_info *info)
195 return (VIRTIO_BUS_ALLOC_VIRTQUEUES(device_get_parent(dev), flags,
200 virtio_setup_intr(device_t dev, enum intr_type type)
203 return (VIRTIO_BUS_SETUP_INTR(device_get_parent(dev), type));
207 virtio_with_feature(device_t dev, uint64_t feature)
210 return (VIRTIO_BUS_WITH_FEATURE(device_get_parent(dev), feature));
214 virtio_stop(device_t dev)
217 VIRTIO_BUS_STOP(device_get_parent(dev));
221 virtio_reinit(device_t dev, uint64_t features)
224 return (VIRTIO_BUS_REINIT(device_get_parent(dev), features));
228 virtio_reinit_complete(device_t dev)
231 VIRTIO_BUS_REINIT_COMPLETE(device_get_parent(dev));
235 virtio_read_device_config(device_t dev, bus_size_t offset, void *dst, int len)
238 VIRTIO_BUS_READ_DEVICE_CONFIG(device_get_parent(dev),
243 virtio_write_device_config(device_t dev, bus_size_t offset, void *dst, int len)
246 VIRTIO_BUS_WRITE_DEVICE_CONFIG(device_get_parent(dev),
251 virtio_modevent(module_t mod, int type, void *unused)
270 static moduledata_t virtio_mod = {
276 DECLARE_MODULE(virtio, virtio_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
277 MODULE_VERSION(virtio, 1);