xref: /freebsd-11-stable/sys/dev/virtio/virtio.c (revision 4ab2e064d7950be84256d671a7ae93f87cc6aa36)
1 /*-
2  * Copyright (c) 2011, Bryan Venteicher <bryanv@FreeBSD.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 <sys/bus.h>
40 #include <sys/rman.h>
41 
42 #include <dev/virtio/virtio.h>
43 #include <dev/virtio/virtio_config.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 	const 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_RPMSG,		"Remote Processor Messaging" 	},
62 	{ VIRTIO_ID_SCSI,		"SCSI"				},
63 	{ VIRTIO_ID_9P,			"9P Transport"			},
64 	{ VIRTIO_ID_RPROC_SERIAL,	"Remote Processor Serial"	},
65 	{ VIRTIO_ID_CAIF,		"CAIF"				},
66 	{ VIRTIO_ID_GPU,		"GPU"				},
67 	{ VIRTIO_ID_INPUT,		"Input" 			},
68 	{ VIRTIO_ID_VSOCK,		"VSOCK Transport" 		},
69 	{ VIRTIO_ID_CRYPTO,		"Crypto" 			},
70 
71 	{ 0, NULL }
72 };
73 
74 /* Device independent features. */
75 static struct virtio_feature_desc virtio_common_feature_desc[] = {
76 	{ VIRTIO_F_NOTIFY_ON_EMPTY,	"NotifyOnEmpty"	},
77 	{ VIRTIO_RING_F_INDIRECT_DESC,	"RingIndirect"	},
78 	{ VIRTIO_RING_F_EVENT_IDX,	"EventIdx"	},
79 	{ VIRTIO_F_BAD_FEATURE,		"BadFeature"	},
80 
81 	{ 0, NULL }
82 };
83 
84 const char *
virtio_device_name(uint16_t devid)85 virtio_device_name(uint16_t devid)
86 {
87 	struct virtio_ident *ident;
88 
89 	for (ident = virtio_ident_table; ident->name != NULL; ident++) {
90 		if (ident->devid == devid)
91 			return (ident->name);
92 	}
93 
94 	return (NULL);
95 }
96 
97 static const char *
virtio_feature_name(uint64_t val,struct virtio_feature_desc * desc)98 virtio_feature_name(uint64_t val, struct virtio_feature_desc *desc)
99 {
100 	int i, j;
101 	struct virtio_feature_desc *descs[2] = { desc,
102 	    virtio_common_feature_desc };
103 
104 	for (i = 0; i < 2; i++) {
105 		if (descs[i] == NULL)
106 			continue;
107 
108 		for (j = 0; descs[i][j].vfd_val != 0; j++) {
109 			if (val == descs[i][j].vfd_val)
110 				return (descs[i][j].vfd_str);
111 		}
112 	}
113 
114 	return (NULL);
115 }
116 
117 void
virtio_describe(device_t dev,const char * msg,uint64_t features,struct virtio_feature_desc * desc)118 virtio_describe(device_t dev, const char *msg,
119     uint64_t features, struct virtio_feature_desc *desc)
120 {
121 	struct sbuf sb;
122 	uint64_t val;
123 	char *buf;
124 	const char *name;
125 	int n;
126 
127 	if ((buf = malloc(512, M_TEMP, M_NOWAIT)) == NULL) {
128 		device_printf(dev, "%s features: %#jx\n", msg, (uintmax_t) features);
129 		return;
130 	}
131 
132 	sbuf_new(&sb, buf, 512, SBUF_FIXEDLEN);
133 	sbuf_printf(&sb, "%s features: %#jx", msg, (uintmax_t) features);
134 
135 	for (n = 0, val = 1ULL << 63; val != 0; val >>= 1) {
136 		/*
137 		 * BAD_FEATURE is used to detect broken Linux clients
138 		 * and therefore is not applicable to FreeBSD.
139 		 */
140 		if (((features & val) == 0) || val == VIRTIO_F_BAD_FEATURE)
141 			continue;
142 
143 		if (n++ == 0)
144 			sbuf_cat(&sb, " <");
145 		else
146 			sbuf_cat(&sb, ",");
147 
148 		name = virtio_feature_name(val, desc);
149 		if (name == NULL)
150 			sbuf_printf(&sb, "%#jx", (uintmax_t) val);
151 		else
152 			sbuf_cat(&sb, name);
153 	}
154 
155 	if (n > 0)
156 		sbuf_cat(&sb, ">");
157 
158 #if __FreeBSD_version < 900020
159 	sbuf_finish(&sb);
160 	if (sbuf_overflowed(&sb) == 0)
161 #else
162 	if (sbuf_finish(&sb) == 0)
163 #endif
164 		device_printf(dev, "%s\n", sbuf_data(&sb));
165 
166 	sbuf_delete(&sb);
167 	free(buf, M_TEMP);
168 }
169 
170 /*
171  * VirtIO bus method wrappers.
172  */
173 
174 void
virtio_read_ivar(device_t dev,int ivar,uintptr_t * val)175 virtio_read_ivar(device_t dev, int ivar, uintptr_t *val)
176 {
177 
178 	*val = -1;
179 	BUS_READ_IVAR(device_get_parent(dev), dev, ivar, val);
180 }
181 
182 void
virtio_write_ivar(device_t dev,int ivar,uintptr_t val)183 virtio_write_ivar(device_t dev, int ivar, uintptr_t val)
184 {
185 
186 	BUS_WRITE_IVAR(device_get_parent(dev), dev, ivar, val);
187 }
188 
189 uint64_t
virtio_negotiate_features(device_t dev,uint64_t child_features)190 virtio_negotiate_features(device_t dev, uint64_t child_features)
191 {
192 
193 	return (VIRTIO_BUS_NEGOTIATE_FEATURES(device_get_parent(dev),
194 	    child_features));
195 }
196 
197 int
virtio_alloc_virtqueues(device_t dev,int flags,int nvqs,struct vq_alloc_info * info)198 virtio_alloc_virtqueues(device_t dev, int flags, int nvqs,
199     struct vq_alloc_info *info)
200 {
201 
202 	return (VIRTIO_BUS_ALLOC_VIRTQUEUES(device_get_parent(dev), flags,
203 	    nvqs, info));
204 }
205 
206 int
virtio_setup_intr(device_t dev,enum intr_type type)207 virtio_setup_intr(device_t dev, enum intr_type type)
208 {
209 
210 	return (VIRTIO_BUS_SETUP_INTR(device_get_parent(dev), type));
211 }
212 
213 int
virtio_with_feature(device_t dev,uint64_t feature)214 virtio_with_feature(device_t dev, uint64_t feature)
215 {
216 
217 	return (VIRTIO_BUS_WITH_FEATURE(device_get_parent(dev), feature));
218 }
219 
220 void
virtio_stop(device_t dev)221 virtio_stop(device_t dev)
222 {
223 
224 	VIRTIO_BUS_STOP(device_get_parent(dev));
225 }
226 
227 int
virtio_reinit(device_t dev,uint64_t features)228 virtio_reinit(device_t dev, uint64_t features)
229 {
230 
231 	return (VIRTIO_BUS_REINIT(device_get_parent(dev), features));
232 }
233 
234 void
virtio_reinit_complete(device_t dev)235 virtio_reinit_complete(device_t dev)
236 {
237 
238 	VIRTIO_BUS_REINIT_COMPLETE(device_get_parent(dev));
239 }
240 
241 int
virtio_config_generation(device_t dev)242 virtio_config_generation(device_t dev)
243 {
244 
245 	return (VIRTIO_BUS_CONFIG_GENERATION(device_get_parent(dev)));
246 }
247 
248 void
virtio_read_device_config(device_t dev,bus_size_t offset,void * dst,int len)249 virtio_read_device_config(device_t dev, bus_size_t offset, void *dst, int len)
250 {
251 
252 	VIRTIO_BUS_READ_DEVICE_CONFIG(device_get_parent(dev),
253 	    offset, dst, len);
254 }
255 
256 void
virtio_write_device_config(device_t dev,bus_size_t offset,void * dst,int len)257 virtio_write_device_config(device_t dev, bus_size_t offset, void *dst, int len)
258 {
259 
260 	VIRTIO_BUS_WRITE_DEVICE_CONFIG(device_get_parent(dev),
261 	    offset, dst, len);
262 }
263 
264 static int
virtio_modevent(module_t mod,int type,void * unused)265 virtio_modevent(module_t mod, int type, void *unused)
266 {
267 	int error;
268 
269 	switch (type) {
270 	case MOD_LOAD:
271 	case MOD_QUIESCE:
272 	case MOD_UNLOAD:
273 	case MOD_SHUTDOWN:
274 		error = 0;
275 		break;
276 	default:
277 		error = EOPNOTSUPP;
278 		break;
279 	}
280 
281 	return (error);
282 }
283 
284 static moduledata_t virtio_mod = {
285 	"virtio",
286 	virtio_modevent,
287 	0
288 };
289 
290 DECLARE_MODULE(virtio, virtio_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
291 MODULE_VERSION(virtio, 1);
292