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