xref: /dragonfly/sys/dev/virtual/virtio/virtio/virtio.c (revision 8afeec5dc8ef4e8086f91ca5927c18100bc1b73f)
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_bus_if.h"
45 
46 static int virtio_modevent(module_t, int, void *);
47 static const char *virtio_feature_name(uint64_t, struct virtio_feature_desc *);
48 
49 static struct virtio_ident {
50           uint16_t devid;
51           const char *name;
52 } virtio_ident_table[] = {
53           { VIRTIO_ID_NETWORK,          "Network" },
54           { VIRTIO_ID_BLOCK,  "Block"             },
55           { VIRTIO_ID_CONSOLE,          "Console" },
56           { VIRTIO_ID_ENTROPY,          "Entropy" },
57           { VIRTIO_ID_BALLOON,          "Balloon" },
58           { VIRTIO_ID_IOMEMORY,         "IOMemory"          },
59           { VIRTIO_ID_SCSI,   "SCSI"              },
60           { VIRTIO_ID_9P,               "9P Transport"      },
61 
62           { 0, NULL }
63 };
64 
65 /* Device independent features. */
66 static struct virtio_feature_desc virtio_common_feature_desc[] = {
67           { VIRTIO_F_NOTIFY_ON_EMPTY,   "NotifyOnEmpty"     },
68           { VIRTIO_F_ANY_LAYOUT,                  "AnyLayout"         },
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 *
virtio_device_name(uint16_t devid)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 int
virtio_get_device_type(device_t dev)90 virtio_get_device_type(device_t dev)
91 {
92           uintptr_t devtype;
93 
94           devtype = -1;
95 
96           BUS_READ_IVAR(device_get_parent(dev), dev,
97               VIRTIO_IVAR_DEVTYPE, &devtype);
98 
99           return ((int) devtype);
100 }
101 
102 void
virtio_set_feature_desc(device_t dev,struct virtio_feature_desc * feature_desc)103 virtio_set_feature_desc(device_t dev,
104     struct virtio_feature_desc *feature_desc)
105 {
106 
107           BUS_WRITE_IVAR(device_get_parent(dev), dev,
108               VIRTIO_IVAR_FEATURE_DESC, (uintptr_t) feature_desc);
109 }
110 
111 void
virtio_describe(device_t dev,const char * msg,uint64_t features,struct virtio_feature_desc * desc)112 virtio_describe(device_t dev, const char *msg,
113     uint64_t features, struct virtio_feature_desc *desc)
114 {
115           struct sbuf sb;
116           uint64_t val;
117           char *buf;
118           const char *name;
119           int n;
120 
121           if ((buf = kmalloc(512, M_TEMP, M_INTWAIT)) == NULL) {
122                     device_printf(dev, "%s features: 0x%"PRIx64"\n", msg,
123                         features);
124                     return;
125           }
126 
127           sbuf_new(&sb, buf, 512, SBUF_FIXEDLEN);
128           sbuf_printf(&sb, "%s features: 0x%"PRIx64, msg, 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 (sbuf_finish(&sb) == 0)
154                     device_printf(dev, "%s\n", sbuf_data(&sb));
155 
156           sbuf_delete(&sb);
157           kfree(buf, M_TEMP);
158 }
159 
160 static const char *
virtio_feature_name(uint64_t val,struct virtio_feature_desc * desc)161 virtio_feature_name(uint64_t val, struct virtio_feature_desc *desc)
162 {
163           int i, j;
164           struct virtio_feature_desc *descs[2] = { desc,
165               virtio_common_feature_desc };
166 
167           for (i = 0; i < 2; i++) {
168                     if (descs[i] == NULL)
169                               continue;
170 
171                     for (j = 0; descs[i][j].vfd_val != 0; j++) {
172                               if (val == descs[i][j].vfd_val)
173                                         return (descs[i][j].vfd_str);
174                     }
175           }
176 
177           return (NULL);
178 }
179 
180 /*
181  * VirtIO bus method wrappers.
182  */
183 
184 uint64_t
virtio_negotiate_features(device_t dev,uint64_t child_features)185 virtio_negotiate_features(device_t dev, uint64_t child_features)
186 {
187           return (VIRTIO_BUS_NEGOTIATE_FEATURES(device_get_parent(dev),
188                     child_features));
189 }
190 
191 int
virtio_alloc_virtqueues(device_t dev,int nvqs,struct vq_alloc_info * info)192 virtio_alloc_virtqueues(device_t dev, int nvqs, struct vq_alloc_info *info)
193 {
194           return (VIRTIO_BUS_ALLOC_VIRTQUEUES(device_get_parent(dev),
195                     nvqs, info));
196 }
197 
198 int
virtio_setup_intr(device_t dev,uint irq,lwkt_serialize_t slz)199 virtio_setup_intr(device_t dev, uint irq, lwkt_serialize_t slz)
200 {
201           return (VIRTIO_BUS_SETUP_INTR(device_get_parent(dev), irq, slz));
202 }
203 
204 int
virtio_teardown_intr(device_t dev,uint irq)205 virtio_teardown_intr(device_t dev, uint irq)
206 {
207           return (VIRTIO_BUS_TEARDOWN_INTR(device_get_parent(dev), irq));
208 }
209 
210 int
virtio_intr_count(device_t dev)211 virtio_intr_count(device_t dev)
212 {
213           return (VIRTIO_BUS_INTR_COUNT(device_get_parent(dev)));
214 }
215 
216 int
virtio_intr_alloc(device_t dev,int * cnt,int use_config,int * cpus)217 virtio_intr_alloc(device_t dev, int *cnt, int use_config, int *cpus)
218 {
219           return (VIRTIO_BUS_INTR_ALLOC(device_get_parent(dev), cnt, use_config,
220                                               cpus));
221 }
222 
223 int
virtio_intr_release(device_t dev)224 virtio_intr_release(device_t dev)
225 {
226           return (VIRTIO_BUS_INTR_RELEASE(device_get_parent(dev)));
227 }
228 
229 int
virtio_bind_intr(device_t dev,uint irq,int what,driver_intr_t handler,void * arg)230 virtio_bind_intr(device_t dev, uint irq, int what,
231     driver_intr_t handler, void *arg)
232 {
233           return (VIRTIO_BUS_BIND_INTR(device_get_parent(dev), irq, what,
234                                              handler, arg));
235 }
236 
237 int
virtio_unbind_intr(device_t dev,int what)238 virtio_unbind_intr(device_t dev, int what)
239 {
240           return (VIRTIO_BUS_UNBIND_INTR(device_get_parent(dev), what));
241 }
242 
243 int
virtio_with_feature(device_t dev,uint64_t feature)244 virtio_with_feature(device_t dev, uint64_t feature)
245 {
246           return (VIRTIO_BUS_WITH_FEATURE(device_get_parent(dev), feature));
247 }
248 
249 void
virtio_stop(device_t dev)250 virtio_stop(device_t dev)
251 {
252           VIRTIO_BUS_STOP(device_get_parent(dev));
253 }
254 
255 int
virtio_reinit(device_t dev,uint64_t features)256 virtio_reinit(device_t dev, uint64_t features)
257 {
258           return (VIRTIO_BUS_REINIT(device_get_parent(dev), features));
259 }
260 
261 void
virtio_reinit_complete(device_t dev)262 virtio_reinit_complete(device_t dev)
263 {
264           VIRTIO_BUS_REINIT_COMPLETE(device_get_parent(dev));
265 }
266 
267 void
virtio_read_device_config(device_t dev,bus_size_t offset,void * dst,int len)268 virtio_read_device_config(device_t dev, bus_size_t offset, void *dst, int len)
269 {
270           VIRTIO_BUS_READ_DEVICE_CONFIG(device_get_parent(dev),
271                                               offset, dst, len);
272 }
273 
274 void
virtio_write_device_config(device_t dev,bus_size_t offset,void * dst,int len)275 virtio_write_device_config(device_t dev, bus_size_t offset, void *dst, int len)
276 {
277           VIRTIO_BUS_WRITE_DEVICE_CONFIG(device_get_parent(dev),
278                                                offset, dst, len);
279 }
280 
281 static int
virtio_modevent(module_t mod,int type,void * unused)282 virtio_modevent(module_t mod, int type, void *unused)
283 {
284           int error;
285 
286           error = 0;
287 
288           switch (type) {
289           case MOD_LOAD:
290           case MOD_UNLOAD:
291           case MOD_SHUTDOWN:
292                     break;
293           default:
294                     error = EOPNOTSUPP;
295                     break;
296           }
297 
298           return (error);
299 }
300 
301 static moduledata_t virtio_mod = {
302           "virtio",
303           virtio_modevent,
304           0
305 };
306 
307 DECLARE_MODULE(virtio, virtio_mod, SI_SUB_DRIVERS, SI_ORDER_EARLIER);
308 MODULE_VERSION(virtio, 1);
309