1 /*- 2 * Copyright (c) 2011, Bryan Venteicher <bryanv@daemoninthecloset.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice unmodified, this list of conditions, and the following 10 * disclaimer. 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. 14 * 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. 25 * 26 * $FreeBSD: src/sys/dev/virtio/virtio.c,v 1.1 2011/11/18 05:43:43 grehan Exp $ 27 */ 28 29 #include <sys/param.h> 30 #include <sys/systm.h> 31 #include <sys/kernel.h> 32 #include <sys/malloc.h> 33 #include <sys/module.h> 34 #include <sys/sbuf.h> 35 36 #include <machine/inttypes.h> 37 #include <sys/bus.h> 38 #include <sys/serialize.h> 39 #include <sys/rman.h> 40 41 #include "virtio.h" 42 #include "virtqueue.h" 43 44 #include "virtio_if.h" 45 #include "virtio_bus_if.h" 46 47 static int virtio_modevent(module_t, int, void *); 48 static const char *virtio_feature_name(uint64_t, struct virtio_feature_desc *); 49 50 static struct virtio_ident { 51 uint16_t devid; 52 const char *name; 53 } virtio_ident_table[] = { 54 { VIRTIO_ID_NETWORK, "Network" }, 55 { VIRTIO_ID_BLOCK, "Block" }, 56 { VIRTIO_ID_CONSOLE, "Console" }, 57 { VIRTIO_ID_ENTROPY, "Entropy" }, 58 { VIRTIO_ID_BALLOON, "Balloon" }, 59 { VIRTIO_ID_IOMEMORY, "IOMemory" }, 60 { VIRTIO_ID_SCSI, "SCSI" }, 61 { VIRTIO_ID_9P, "9P Transport" }, 62 63 { 0, NULL } 64 }; 65 66 /* Device independent features. */ 67 static struct virtio_feature_desc virtio_common_feature_desc[] = { 68 { VIRTIO_F_NOTIFY_ON_EMPTY, "NotifyOnEmpty" }, 69 { VIRTIO_F_ANY_LAYOUT, "AnyLayout" }, 70 { VIRTIO_RING_F_INDIRECT_DESC, "RingIndirect" }, 71 { VIRTIO_RING_F_EVENT_IDX, "EventIdx" }, 72 { VIRTIO_F_BAD_FEATURE, "BadFeature" }, 73 74 { 0, NULL } 75 }; 76 77 const char * 78 virtio_device_name(uint16_t devid) 79 { 80 struct virtio_ident *ident; 81 82 for (ident = virtio_ident_table; ident->name != NULL; ident++) { 83 if (ident->devid == devid) 84 return (ident->name); 85 } 86 87 return (NULL); 88 } 89 90 int 91 virtio_get_device_type(device_t dev) 92 { 93 uintptr_t devtype; 94 95 devtype = -1; 96 97 BUS_READ_IVAR(device_get_parent(dev), dev, 98 VIRTIO_IVAR_DEVTYPE, &devtype); 99 100 return ((int) devtype); 101 } 102 103 void 104 virtio_set_feature_desc(device_t dev, 105 struct virtio_feature_desc *feature_desc) 106 { 107 108 BUS_WRITE_IVAR(device_get_parent(dev), dev, 109 VIRTIO_IVAR_FEATURE_DESC, (uintptr_t) feature_desc); 110 } 111 112 void 113 virtio_describe(device_t dev, const char *msg, 114 uint64_t features, struct virtio_feature_desc *desc) 115 { 116 struct sbuf sb; 117 uint64_t val; 118 char *buf; 119 const char *name; 120 int n; 121 122 if ((buf = kmalloc(512, M_TEMP, M_INTWAIT)) == NULL) { 123 device_printf(dev, "%s features: 0x%"PRIx64"\n", msg, 124 features); 125 return; 126 } 127 128 sbuf_new(&sb, buf, 512, SBUF_FIXEDLEN); 129 sbuf_printf(&sb, "%s features: 0x%"PRIx64, msg, features); 130 131 for (n = 0, val = 1ULL << 63; val != 0; val >>= 1) { 132 /* 133 * BAD_FEATURE is used to detect broken Linux clients 134 * and therefore is not applicable to FreeBSD. 135 */ 136 if (((features & val) == 0) || val == VIRTIO_F_BAD_FEATURE) 137 continue; 138 139 if (n++ == 0) 140 sbuf_cat(&sb, " <"); 141 else 142 sbuf_cat(&sb, ","); 143 144 name = virtio_feature_name(val, desc); 145 if (name == NULL) 146 sbuf_printf(&sb, "%#jx", (uintmax_t) val); 147 else 148 sbuf_cat(&sb, name); 149 } 150 151 if (n > 0) 152 sbuf_cat(&sb, ">"); 153 154 if (sbuf_finish(&sb) == 0) 155 device_printf(dev, "%s\n", sbuf_data(&sb)); 156 157 sbuf_delete(&sb); 158 kfree(buf, M_TEMP); 159 } 160 161 static const char * 162 virtio_feature_name(uint64_t val, struct virtio_feature_desc *desc) 163 { 164 int i, j; 165 struct virtio_feature_desc *descs[2] = { desc, 166 virtio_common_feature_desc }; 167 168 for (i = 0; i < 2; i++) { 169 if (descs[i] == NULL) 170 continue; 171 172 for (j = 0; descs[i][j].vfd_val != 0; j++) { 173 if (val == descs[i][j].vfd_val) 174 return (descs[i][j].vfd_str); 175 } 176 } 177 178 return (NULL); 179 } 180 181 /* 182 * VirtIO bus method wrappers. 183 */ 184 185 uint64_t 186 virtio_negotiate_features(device_t dev, uint64_t child_features) 187 { 188 return (VIRTIO_BUS_NEGOTIATE_FEATURES(device_get_parent(dev), 189 child_features)); 190 } 191 192 int 193 virtio_alloc_virtqueues(device_t dev, int flags, int nvqs, 194 struct vq_alloc_info *info) 195 { 196 return (VIRTIO_BUS_ALLOC_VIRTQUEUES(device_get_parent(dev), flags, 197 nvqs, info)); 198 } 199 200 int 201 virtio_setup_intr(device_t dev, lwkt_serialize_t slz) 202 { 203 return (VIRTIO_BUS_SETUP_INTR(device_get_parent(dev), slz)); 204 } 205 206 int 207 virtio_with_feature(device_t dev, uint64_t feature) 208 { 209 return (VIRTIO_BUS_WITH_FEATURE(device_get_parent(dev), feature)); 210 } 211 212 void 213 virtio_stop(device_t dev) 214 { 215 VIRTIO_BUS_STOP(device_get_parent(dev)); 216 } 217 218 int 219 virtio_reinit(device_t dev, uint64_t features) 220 { 221 return (VIRTIO_BUS_REINIT(device_get_parent(dev), features)); 222 } 223 224 void 225 virtio_reinit_complete(device_t dev) 226 { 227 VIRTIO_BUS_REINIT_COMPLETE(device_get_parent(dev)); 228 } 229 230 void 231 virtio_read_device_config(device_t dev, bus_size_t offset, void *dst, int len) 232 { 233 VIRTIO_BUS_READ_DEVICE_CONFIG(device_get_parent(dev), 234 offset, dst, len); 235 } 236 237 void 238 virtio_write_device_config(device_t dev, bus_size_t offset, void *dst, int len) 239 { 240 VIRTIO_BUS_WRITE_DEVICE_CONFIG(device_get_parent(dev), 241 offset, dst, len); 242 } 243 244 static int 245 virtio_modevent(module_t mod, int type, void *unused) 246 { 247 int error; 248 249 error = 0; 250 251 switch (type) { 252 case MOD_LOAD: 253 case MOD_UNLOAD: 254 case MOD_SHUTDOWN: 255 break; 256 default: 257 error = EOPNOTSUPP; 258 break; 259 } 260 261 return (error); 262 } 263 264 static moduledata_t virtio_mod = { 265 "virtio", 266 virtio_modevent, 267 0 268 }; 269 270 DECLARE_MODULE(virtio, virtio_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST); 271 MODULE_VERSION(virtio, 1); 272