virtio.c revision 330897
1/*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2011, Bryan Venteicher <bryanv@FreeBSD.org> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice unmodified, this list of conditions, and the following 12 * disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29#include <sys/cdefs.h> 30__FBSDID("$FreeBSD: stable/11/sys/dev/virtio/virtio.c 330897 2018-03-14 03:19:51Z eadler $"); 31 32#include <sys/param.h> 33#include <sys/systm.h> 34#include <sys/kernel.h> 35#include <sys/malloc.h> 36#include <sys/module.h> 37#include <sys/sbuf.h> 38 39#include <machine/bus.h> 40#include <machine/resource.h> 41#include <sys/bus.h> 42#include <sys/rman.h> 43 44#include <dev/virtio/virtio.h> 45#include <dev/virtio/virtio_config.h> 46#include <dev/virtio/virtqueue.h> 47 48#include "virtio_bus_if.h" 49 50static int virtio_modevent(module_t, int, void *); 51static const char *virtio_feature_name(uint64_t, struct virtio_feature_desc *); 52 53static struct virtio_ident { 54 uint16_t devid; 55 const char *name; 56} virtio_ident_table[] = { 57 { VIRTIO_ID_NETWORK, "Network" }, 58 { VIRTIO_ID_BLOCK, "Block" }, 59 { VIRTIO_ID_CONSOLE, "Console" }, 60 { VIRTIO_ID_ENTROPY, "Entropy" }, 61 { VIRTIO_ID_BALLOON, "Balloon" }, 62 { VIRTIO_ID_IOMEMORY, "IOMemory" }, 63 { VIRTIO_ID_SCSI, "SCSI" }, 64 { VIRTIO_ID_9P, "9P Transport" }, 65 66 { 0, NULL } 67}; 68 69/* Device independent features. */ 70static struct virtio_feature_desc virtio_common_feature_desc[] = { 71 { VIRTIO_F_NOTIFY_ON_EMPTY, "NotifyOnEmpty" }, 72 { VIRTIO_RING_F_INDIRECT_DESC, "RingIndirect" }, 73 { VIRTIO_RING_F_EVENT_IDX, "EventIdx" }, 74 { VIRTIO_F_BAD_FEATURE, "BadFeature" }, 75 76 { 0, NULL } 77}; 78 79const char * 80virtio_device_name(uint16_t devid) 81{ 82 struct virtio_ident *ident; 83 84 for (ident = virtio_ident_table; ident->name != NULL; ident++) { 85 if (ident->devid == devid) 86 return (ident->name); 87 } 88 89 return (NULL); 90} 91 92static const char * 93virtio_feature_name(uint64_t val, struct virtio_feature_desc *desc) 94{ 95 int i, j; 96 struct virtio_feature_desc *descs[2] = { desc, 97 virtio_common_feature_desc }; 98 99 for (i = 0; i < 2; i++) { 100 if (descs[i] == NULL) 101 continue; 102 103 for (j = 0; descs[i][j].vfd_val != 0; j++) { 104 if (val == descs[i][j].vfd_val) 105 return (descs[i][j].vfd_str); 106 } 107 } 108 109 return (NULL); 110} 111 112void 113virtio_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 = malloc(512, M_TEMP, M_NOWAIT)) == NULL) { 123 device_printf(dev, "%s features: %#jx\n", msg, (uintmax_t) features); 124 return; 125 } 126 127 sbuf_new(&sb, buf, 512, SBUF_FIXEDLEN); 128 sbuf_printf(&sb, "%s features: %#jx", msg, (uintmax_t) features); 129 130 for (n = 0, val = 1ULL << 63; val != 0; val >>= 1) { 131 /* 132 * BAD_FEATURE is used to detect broken Linux clients 133 * and therefore is not applicable to FreeBSD. 134 */ 135 if (((features & val) == 0) || val == VIRTIO_F_BAD_FEATURE) 136 continue; 137 138 if (n++ == 0) 139 sbuf_cat(&sb, " <"); 140 else 141 sbuf_cat(&sb, ","); 142 143 name = virtio_feature_name(val, desc); 144 if (name == NULL) 145 sbuf_printf(&sb, "%#jx", (uintmax_t) val); 146 else 147 sbuf_cat(&sb, name); 148 } 149 150 if (n > 0) 151 sbuf_cat(&sb, ">"); 152 153#if __FreeBSD_version < 900020 154 sbuf_finish(&sb); 155 if (sbuf_overflowed(&sb) == 0) 156#else 157 if (sbuf_finish(&sb) == 0) 158#endif 159 device_printf(dev, "%s\n", sbuf_data(&sb)); 160 161 sbuf_delete(&sb); 162 free(buf, M_TEMP); 163} 164 165/* 166 * VirtIO bus method wrappers. 167 */ 168 169void 170virtio_read_ivar(device_t dev, int ivar, uintptr_t *val) 171{ 172 173 *val = -1; 174 BUS_READ_IVAR(device_get_parent(dev), dev, ivar, val); 175} 176 177void 178virtio_write_ivar(device_t dev, int ivar, uintptr_t val) 179{ 180 181 BUS_WRITE_IVAR(device_get_parent(dev), dev, ivar, val); 182} 183 184uint64_t 185virtio_negotiate_features(device_t dev, uint64_t child_features) 186{ 187 188 return (VIRTIO_BUS_NEGOTIATE_FEATURES(device_get_parent(dev), 189 child_features)); 190} 191 192int 193virtio_alloc_virtqueues(device_t dev, int flags, int nvqs, 194 struct vq_alloc_info *info) 195{ 196 197 return (VIRTIO_BUS_ALLOC_VIRTQUEUES(device_get_parent(dev), flags, 198 nvqs, info)); 199} 200 201int 202virtio_setup_intr(device_t dev, enum intr_type type) 203{ 204 205 return (VIRTIO_BUS_SETUP_INTR(device_get_parent(dev), type)); 206} 207 208int 209virtio_with_feature(device_t dev, uint64_t feature) 210{ 211 212 return (VIRTIO_BUS_WITH_FEATURE(device_get_parent(dev), feature)); 213} 214 215void 216virtio_stop(device_t dev) 217{ 218 219 VIRTIO_BUS_STOP(device_get_parent(dev)); 220} 221 222int 223virtio_reinit(device_t dev, uint64_t features) 224{ 225 226 return (VIRTIO_BUS_REINIT(device_get_parent(dev), features)); 227} 228 229void 230virtio_reinit_complete(device_t dev) 231{ 232 233 VIRTIO_BUS_REINIT_COMPLETE(device_get_parent(dev)); 234} 235 236void 237virtio_read_device_config(device_t dev, bus_size_t offset, void *dst, int len) 238{ 239 240 VIRTIO_BUS_READ_DEVICE_CONFIG(device_get_parent(dev), 241 offset, dst, len); 242} 243 244void 245virtio_write_device_config(device_t dev, bus_size_t offset, void *dst, int len) 246{ 247 248 VIRTIO_BUS_WRITE_DEVICE_CONFIG(device_get_parent(dev), 249 offset, dst, len); 250} 251 252static int 253virtio_modevent(module_t mod, int type, void *unused) 254{ 255 int error; 256 257 switch (type) { 258 case MOD_LOAD: 259 case MOD_QUIESCE: 260 case MOD_UNLOAD: 261 case MOD_SHUTDOWN: 262 error = 0; 263 break; 264 default: 265 error = EOPNOTSUPP; 266 break; 267 } 268 269 return (error); 270} 271 272static moduledata_t virtio_mod = { 273 "virtio", 274 virtio_modevent, 275 0 276}; 277 278DECLARE_MODULE(virtio, virtio_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST); 279MODULE_VERSION(virtio, 1); 280