]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - sys/dev/virtio/virtio.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / sys / dev / virtio / virtio.c
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_SCSI,       "SCSI"          },
62         { VIRTIO_ID_9P,         "9P Transport"  },
63
64         { 0, NULL }
65 };
66
67 /* Device independent features. */
68 static struct virtio_feature_desc virtio_common_feature_desc[] = {
69         { VIRTIO_F_NOTIFY_ON_EMPTY,     "NotifyOnEmpty" },
70         { VIRTIO_RING_F_INDIRECT_DESC,  "RingIndirect"  },
71         { VIRTIO_RING_F_EVENT_IDX,      "EventIdx"      },
72         { VIRTIO_F_BAD_FEATURE,         "BadFeature"    },
73
74         { 0, NULL }
75 };
76
77 const char *
78 virtio_device_name(uint16_t devid)
79 {
80         struct virtio_ident *ident;
81
82         for (ident = virtio_ident_table; ident->name != NULL; ident++) {
83                 if (ident->devid == devid)
84                         return (ident->name);
85         }
86
87         return (NULL);
88 }
89
90 static const char *
91 virtio_feature_name(uint64_t val, struct virtio_feature_desc *desc)
92 {
93         int i, j;
94         struct virtio_feature_desc *descs[2] = { desc,
95             virtio_common_feature_desc };
96
97         for (i = 0; i < 2; i++) {
98                 if (descs[i] == NULL)
99                         continue;
100
101                 for (j = 0; descs[i][j].vfd_val != 0; j++) {
102                         if (val == descs[i][j].vfd_val)
103                                 return (descs[i][j].vfd_str);
104                 }
105         }
106
107         return (NULL);
108 }
109
110 void
111 virtio_describe(device_t dev, const char *msg,
112     uint64_t features, struct virtio_feature_desc *desc)
113 {
114         struct sbuf sb;
115         uint64_t val;
116         char *buf;
117         const char *name;
118         int n;
119
120         if ((buf = malloc(512, M_TEMP, M_NOWAIT)) == NULL) {
121                 device_printf(dev, "%s features: %#jx\n", msg, (uintmax_t) features);
122                 return;
123         }
124
125         sbuf_new(&sb, buf, 512, SBUF_FIXEDLEN);
126         sbuf_printf(&sb, "%s features: %#jx", msg, (uintmax_t) 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 = virtio_feature_name(val, desc);
142                 if (name == NULL)
143                         sbuf_printf(&sb, "%#jx", (uintmax_t) val);
144                 else
145                         sbuf_cat(&sb, name);
146         }
147
148         if (n > 0)
149                 sbuf_cat(&sb, ">");
150
151 #if __FreeBSD_version < 900020
152         sbuf_finish(&sb);
153         if (sbuf_overflowed(&sb) == 0)
154 #else
155         if (sbuf_finish(&sb) == 0)
156 #endif
157                 device_printf(dev, "%s\n", sbuf_data(&sb));
158
159         sbuf_delete(&sb);
160         free(buf, M_TEMP);
161 }
162
163 /*
164  * VirtIO bus method wrappers.
165  */
166
167 void
168 virtio_read_ivar(device_t dev, int ivar, uintptr_t *val)
169 {
170
171         *val = -1;
172         BUS_READ_IVAR(device_get_parent(dev), dev, ivar, val);
173 }
174
175 void
176 virtio_write_ivar(device_t dev, int ivar, uintptr_t val)
177 {
178
179         BUS_WRITE_IVAR(device_get_parent(dev), dev, ivar, val);
180 }
181
182 uint64_t
183 virtio_negotiate_features(device_t dev, uint64_t child_features)
184 {
185
186         return (VIRTIO_BUS_NEGOTIATE_FEATURES(device_get_parent(dev),
187             child_features));
188 }
189
190 int
191 virtio_alloc_virtqueues(device_t dev, int flags, int nvqs,
192     struct vq_alloc_info *info)
193 {
194
195         return (VIRTIO_BUS_ALLOC_VIRTQUEUES(device_get_parent(dev), flags,
196             nvqs, info));
197 }
198
199 int
200 virtio_setup_intr(device_t dev, enum intr_type type)
201 {
202
203         return (VIRTIO_BUS_SETUP_INTR(device_get_parent(dev), type));
204 }
205
206 int
207 virtio_with_feature(device_t dev, uint64_t feature)
208 {
209
210         return (VIRTIO_BUS_WITH_FEATURE(device_get_parent(dev), feature));
211 }
212
213 void
214 virtio_stop(device_t dev)
215 {
216
217         VIRTIO_BUS_STOP(device_get_parent(dev));
218 }
219
220 int
221 virtio_reinit(device_t dev, uint64_t features)
222 {
223
224         return (VIRTIO_BUS_REINIT(device_get_parent(dev), features));
225 }
226
227 void
228 virtio_reinit_complete(device_t dev)
229 {
230
231         VIRTIO_BUS_REINIT_COMPLETE(device_get_parent(dev));
232 }
233
234 void
235 virtio_read_device_config(device_t dev, bus_size_t offset, void *dst, int len)
236 {
237
238         VIRTIO_BUS_READ_DEVICE_CONFIG(device_get_parent(dev),
239             offset, dst, len);
240 }
241
242 void
243 virtio_write_device_config(device_t dev, bus_size_t offset, void *dst, int len)
244 {
245
246         VIRTIO_BUS_WRITE_DEVICE_CONFIG(device_get_parent(dev),
247             offset, dst, len);
248 }
249
250 static int
251 virtio_modevent(module_t mod, int type, void *unused)
252 {
253         int error;
254
255         switch (type) {
256         case MOD_LOAD:
257         case MOD_QUIESCE:
258         case MOD_UNLOAD:
259         case MOD_SHUTDOWN:
260                 error = 0;
261                 break;
262         default:
263                 error = EOPNOTSUPP;
264                 break;
265         }
266
267         return (error);
268 }
269
270 static moduledata_t virtio_mod = {
271         "virtio",
272         virtio_modevent,
273         0
274 };
275
276 DECLARE_MODULE(virtio, virtio_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
277 MODULE_VERSION(virtio, 1);