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