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 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 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> 35 #include <sys/sbuf.h> 36 37 #include <machine/bus.h> 38 #include <machine/resource.h> 39 #include <machine/_inttypes.h> 40 #include <sys/bus.h> 41 #include <sys/rman.h> 42 43 #include <dev/virtio/virtio.h> 44 #include <dev/virtio/virtqueue.h> 45 46 #include "virtio_bus_if.h" 47 48 static int virtio_modevent(module_t, int, void *); 49 static const char *virtio_feature_name(uint64_t, struct virtio_feature_desc *); 50 51 static struct virtio_ident { 52 uint16_t devid; 53 char *name; 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_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_RING_F_INDIRECT_DESC, "RingIndirect" }, 70 { VIRTIO_RING_F_EVENT_IDX, "EventIdx" }, 71 { VIRTIO_F_BAD_FEATURE, "BadFeature" }, 72 73 { 0, NULL } 74 }; 75 76 const char * 77 virtio_device_name(uint16_t devid) 78 { 79 struct virtio_ident *ident; 80 81 for (ident = virtio_ident_table; ident->name != NULL; ident++) { 82 if (ident->devid == devid) 83 return (ident->name); 84 } 85 86 return (NULL); 87 } 88 89 void 90 virtio_describe(device_t dev, const char *msg, 91 uint64_t features, struct virtio_feature_desc *feature_desc) 92 { 93 struct sbuf sb; 94 uint64_t val; 95 char *buf; 96 const char *name; 97 int n; 98 99 if ((buf = malloc(512, M_TEMP, M_NOWAIT)) == NULL) { 100 device_printf(dev, "%s features: 0x%"PRIx64"\n", msg, 101 features); 102 return; 103 } 104 105 sbuf_new(&sb, buf, 512, SBUF_FIXEDLEN); 106 sbuf_printf(&sb, "%s features: 0x%"PRIx64, msg, features); 107 108 for (n = 0, val = 1ULL << 63; val != 0; val >>= 1) { 109 /* 110 * BAD_FEATURE is used to detect broken Linux clients 111 * and therefore is not applicable to FreeBSD. 112 */ 113 if (((features & val) == 0) || val == VIRTIO_F_BAD_FEATURE) 114 continue; 115 116 if (n++ == 0) 117 sbuf_cat(&sb, " <"); 118 else 119 sbuf_cat(&sb, ","); 120 121 name = NULL; 122 if (feature_desc != NULL) 123 name = virtio_feature_name(val, feature_desc); 124 if (name == NULL) 125 name = virtio_feature_name(val, 126 virtio_common_feature_desc); 127 128 if (name == NULL) 129 sbuf_printf(&sb, "0x%"PRIx64, val); 130 else 131 sbuf_cat(&sb, name); 132 } 133 134 if (n > 0) 135 sbuf_cat(&sb, ">"); 136 137 #if __FreeBSD_version < 900020 138 sbuf_finish(&sb); 139 if (sbuf_overflowed(&sb) == 0) 140 #else 141 if (sbuf_finish(&sb) == 0) 142 #endif 143 device_printf(dev, "%s\n", sbuf_data(&sb)); 144 145 sbuf_delete(&sb); 146 free(buf, M_TEMP); 147 } 148 149 static const char * 150 virtio_feature_name(uint64_t val, struct virtio_feature_desc *feature_desc) 151 { 152 int i; 153 154 for (i = 0; feature_desc[i].vfd_val != 0; i++) 155 if (val == feature_desc[i].vfd_val) 156 return (feature_desc[i].vfd_str); 157 158 return (NULL); 159 } 160 161 /* 162 * VirtIO bus method wrappers. 163 */ 164 165 void 166 virtio_read_ivar(device_t dev, int ivar, uintptr_t *val) 167 { 168 169 *val = -1; 170 BUS_READ_IVAR(device_get_parent(dev), dev, ivar, val); 171 } 172 173 void 174 virtio_write_ivar(device_t dev, int ivar, uintptr_t val) 175 { 176 177 BUS_WRITE_IVAR(device_get_parent(dev), dev, ivar, val); 178 } 179 180 uint64_t 181 virtio_negotiate_features(device_t dev, uint64_t child_features) 182 { 183 184 return (VIRTIO_BUS_NEGOTIATE_FEATURES(device_get_parent(dev), 185 child_features)); 186 } 187 188 int 189 virtio_alloc_virtqueues(device_t dev, int flags, int nvqs, 190 struct vq_alloc_info *info) 191 { 192 193 return (VIRTIO_BUS_ALLOC_VIRTQUEUES(device_get_parent(dev), flags, 194 nvqs, info)); 195 } 196 197 int 198 virtio_setup_intr(device_t dev, enum intr_type type) 199 { 200 201 return (VIRTIO_BUS_SETUP_INTR(device_get_parent(dev), type)); 202 } 203 204 int 205 virtio_with_feature(device_t dev, uint64_t feature) 206 { 207 208 return (VIRTIO_BUS_WITH_FEATURE(device_get_parent(dev), feature)); 209 } 210 211 void 212 virtio_stop(device_t dev) 213 { 214 215 VIRTIO_BUS_STOP(device_get_parent(dev)); 216 } 217 218 int 219 virtio_reinit(device_t dev, uint64_t features) 220 { 221 222 return (VIRTIO_BUS_REINIT(device_get_parent(dev), features)); 223 } 224 225 void 226 virtio_reinit_complete(device_t dev) 227 { 228 229 VIRTIO_BUS_REINIT_COMPLETE(device_get_parent(dev)); 230 } 231 232 void 233 virtio_read_device_config(device_t dev, bus_size_t offset, void *dst, int len) 234 { 235 236 VIRTIO_BUS_READ_DEVICE_CONFIG(device_get_parent(dev), 237 offset, dst, len); 238 } 239 240 void 241 virtio_write_device_config(device_t dev, bus_size_t offset, void *dst, int len) 242 { 243 244 VIRTIO_BUS_WRITE_DEVICE_CONFIG(device_get_parent(dev), 245 offset, dst, len); 246 } 247 248 static int 249 virtio_modevent(module_t mod, int type, void *unused) 250 { 251 int error; 252 253 error = 0; 254 255 switch (type) { 256 case MOD_LOAD: 257 case MOD_QUIESCE: 258 case MOD_UNLOAD: 259 case MOD_SHUTDOWN: 260 break; 261 default: 262 error = EOPNOTSUPP; 263 break; 264 } 265 266 return (error); 267 } 268 269 static moduledata_t virtio_mod = { 270 "virtio", 271 virtio_modevent, 272 0 273 }; 274 275 DECLARE_MODULE(virtio, virtio_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST); 276 MODULE_VERSION(virtio, 1); 277