Deleted Added
full compact
virtio.c (241470) virtio.c (252707)
1/*-
1/*-
2 * Copyright (c) 2011, Bryan Venteicher <bryanv@daemoninthecloset.org>
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>
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: head/sys/dev/virtio/virtio.c 241470 2012-10-11 23:41:18Z grehan $");
28__FBSDID("$FreeBSD: head/sys/dev/virtio/virtio.c 252707 2013-07-04 17:57:26Z bryanv $");
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>
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 <machine/_inttypes.h>
40#include <sys/bus.h>
41#include <sys/rman.h>
42
43#include <dev/virtio/virtio.h>
44#include <dev/virtio/virtqueue.h>
45
46#include "virtio_bus_if.h"
47
48static int virtio_modevent(module_t, int, void *);
49static const char *virtio_feature_name(uint64_t, struct virtio_feature_desc *);
50
51static struct virtio_ident {
39#include <sys/bus.h>
40#include <sys/rman.h>
41
42#include <dev/virtio/virtio.h>
43#include <dev/virtio/virtqueue.h>
44
45#include "virtio_bus_if.h"
46
47static int virtio_modevent(module_t, int, void *);
48static const char *virtio_feature_name(uint64_t, struct virtio_feature_desc *);
49
50static struct virtio_ident {
52 uint16_t devid;
53 char *name;
51 uint16_t devid;
52 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. */
68static 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
77const char *
78virtio_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
53} virtio_ident_table[] = {
54 { VIRTIO_ID_NETWORK, "Network" },
55 { VIRTIO_ID_BLOCK, "Block" },
56 { VIRTIO_ID_CONSOLE, "Console" },
57 { VIRTIO_ID_ENTROPY, "Entropy" },
58 { VIRTIO_ID_BALLOON, "Balloon" },
59 { VIRTIO_ID_IOMEMORY, "IOMemory" },
60 { VIRTIO_ID_SCSI, "SCSI" },
61 { VIRTIO_ID_9P, "9P Transport" },
62
63 { 0, NULL }
64};
65
66/* Device independent features. */
67static struct virtio_feature_desc virtio_common_feature_desc[] = {
68 { VIRTIO_F_NOTIFY_ON_EMPTY, "NotifyOnEmpty" },
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
76const char *
77virtio_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
89static const char *
90virtio_feature_name(uint64_t val, struct virtio_feature_desc *desc)
91{
92 int i, j;
93 struct virtio_feature_desc *descs[2] = { desc,
94 virtio_common_feature_desc };
95
96 for (i = 0; i < 2; i++) {
97 if (descs[i] == NULL)
98 continue;
99
100 for (j = 0; descs[i][j].vfd_val != 0; j++) {
101 if (val == descs[i][j].vfd_val)
102 return (descs[i][j].vfd_str);
103 }
104 }
105
106 return (NULL);
107}
108
90void
91virtio_describe(device_t dev, const char *msg,
109void
110virtio_describe(device_t dev, const char *msg,
92 uint64_t features, struct virtio_feature_desc *feature_desc)
111 uint64_t features, struct virtio_feature_desc *desc)
93{
94 struct sbuf sb;
95 uint64_t val;
96 char *buf;
97 const char *name;
98 int n;
99
100 if ((buf = malloc(512, M_TEMP, M_NOWAIT)) == NULL) {
112{
113 struct sbuf sb;
114 uint64_t val;
115 char *buf;
116 const char *name;
117 int n;
118
119 if ((buf = malloc(512, M_TEMP, M_NOWAIT)) == NULL) {
101 device_printf(dev, "%s features: 0x%"PRIx64"\n", msg,
102 features);
120 device_printf(dev, "%s features: %#jx\n", msg, (uintmax_t) features);
103 return;
104 }
105
106 sbuf_new(&sb, buf, 512, SBUF_FIXEDLEN);
121 return;
122 }
123
124 sbuf_new(&sb, buf, 512, SBUF_FIXEDLEN);
107 sbuf_printf(&sb, "%s features: 0x%"PRIx64, msg, features);
125 sbuf_printf(&sb, "%s features: %#jx", msg, (uintmax_t) features);
108
109 for (n = 0, val = 1ULL << 63; val != 0; val >>= 1) {
110 /*
111 * BAD_FEATURE is used to detect broken Linux clients
112 * and therefore is not applicable to FreeBSD.
113 */
114 if (((features & val) == 0) || val == VIRTIO_F_BAD_FEATURE)
115 continue;
116
117 if (n++ == 0)
118 sbuf_cat(&sb, " <");
119 else
120 sbuf_cat(&sb, ",");
121
126
127 for (n = 0, val = 1ULL << 63; val != 0; val >>= 1) {
128 /*
129 * BAD_FEATURE is used to detect broken Linux clients
130 * and therefore is not applicable to FreeBSD.
131 */
132 if (((features & val) == 0) || val == VIRTIO_F_BAD_FEATURE)
133 continue;
134
135 if (n++ == 0)
136 sbuf_cat(&sb, " <");
137 else
138 sbuf_cat(&sb, ",");
139
122 name = NULL;
123 if (feature_desc != NULL)
124 name = virtio_feature_name(val, feature_desc);
140 name = virtio_feature_name(val, desc);
125 if (name == NULL)
141 if (name == NULL)
126 name = virtio_feature_name(val,
127 virtio_common_feature_desc);
128
129 if (name == NULL)
130 sbuf_printf(&sb, "0x%"PRIx64, val);
142 sbuf_printf(&sb, "%#jx", (uintmax_t) val);
131 else
132 sbuf_cat(&sb, name);
133 }
134
135 if (n > 0)
136 sbuf_cat(&sb, ">");
137
138#if __FreeBSD_version < 900020
139 sbuf_finish(&sb);
140 if (sbuf_overflowed(&sb) == 0)
141#else
142 if (sbuf_finish(&sb) == 0)
143#endif
144 device_printf(dev, "%s\n", sbuf_data(&sb));
145
146 sbuf_delete(&sb);
147 free(buf, M_TEMP);
148}
149
143 else
144 sbuf_cat(&sb, name);
145 }
146
147 if (n > 0)
148 sbuf_cat(&sb, ">");
149
150#if __FreeBSD_version < 900020
151 sbuf_finish(&sb);
152 if (sbuf_overflowed(&sb) == 0)
153#else
154 if (sbuf_finish(&sb) == 0)
155#endif
156 device_printf(dev, "%s\n", sbuf_data(&sb));
157
158 sbuf_delete(&sb);
159 free(buf, M_TEMP);
160}
161
150static const char *
151virtio_feature_name(uint64_t val, struct virtio_feature_desc *feature_desc)
152{
153 int i;
154
155 for (i = 0; feature_desc[i].vfd_val != 0; i++)
156 if (val == feature_desc[i].vfd_val)
157 return (feature_desc[i].vfd_str);
158
159 return (NULL);
160}
161
162/*
163 * VirtIO bus method wrappers.
164 */
165
166void
167virtio_read_ivar(device_t dev, int ivar, uintptr_t *val)
168{
169
170 *val = -1;
171 BUS_READ_IVAR(device_get_parent(dev), dev, ivar, val);
172}
173
174void
175virtio_write_ivar(device_t dev, int ivar, uintptr_t val)
176{
177
178 BUS_WRITE_IVAR(device_get_parent(dev), dev, ivar, val);
179}
180
181uint64_t
182virtio_negotiate_features(device_t dev, uint64_t child_features)
183{
184
185 return (VIRTIO_BUS_NEGOTIATE_FEATURES(device_get_parent(dev),
186 child_features));
187}
188
189int
190virtio_alloc_virtqueues(device_t dev, int flags, int nvqs,
191 struct vq_alloc_info *info)
192{
193
194 return (VIRTIO_BUS_ALLOC_VIRTQUEUES(device_get_parent(dev), flags,
195 nvqs, info));
196}
197
198int
199virtio_setup_intr(device_t dev, enum intr_type type)
200{
201
202 return (VIRTIO_BUS_SETUP_INTR(device_get_parent(dev), type));
203}
204
205int
206virtio_with_feature(device_t dev, uint64_t feature)
207{
208
209 return (VIRTIO_BUS_WITH_FEATURE(device_get_parent(dev), feature));
210}
211
212void
213virtio_stop(device_t dev)
214{
215
216 VIRTIO_BUS_STOP(device_get_parent(dev));
217}
218
219int
220virtio_reinit(device_t dev, uint64_t features)
221{
222
223 return (VIRTIO_BUS_REINIT(device_get_parent(dev), features));
224}
225
226void
227virtio_reinit_complete(device_t dev)
228{
229
230 VIRTIO_BUS_REINIT_COMPLETE(device_get_parent(dev));
231}
232
233void
234virtio_read_device_config(device_t dev, bus_size_t offset, void *dst, int len)
235{
236
237 VIRTIO_BUS_READ_DEVICE_CONFIG(device_get_parent(dev),
238 offset, dst, len);
239}
240
241void
242virtio_write_device_config(device_t dev, bus_size_t offset, void *dst, int len)
243{
244
245 VIRTIO_BUS_WRITE_DEVICE_CONFIG(device_get_parent(dev),
246 offset, dst, len);
247}
248
249static int
250virtio_modevent(module_t mod, int type, void *unused)
251{
252 int error;
253
162/*
163 * VirtIO bus method wrappers.
164 */
165
166void
167virtio_read_ivar(device_t dev, int ivar, uintptr_t *val)
168{
169
170 *val = -1;
171 BUS_READ_IVAR(device_get_parent(dev), dev, ivar, val);
172}
173
174void
175virtio_write_ivar(device_t dev, int ivar, uintptr_t val)
176{
177
178 BUS_WRITE_IVAR(device_get_parent(dev), dev, ivar, val);
179}
180
181uint64_t
182virtio_negotiate_features(device_t dev, uint64_t child_features)
183{
184
185 return (VIRTIO_BUS_NEGOTIATE_FEATURES(device_get_parent(dev),
186 child_features));
187}
188
189int
190virtio_alloc_virtqueues(device_t dev, int flags, int nvqs,
191 struct vq_alloc_info *info)
192{
193
194 return (VIRTIO_BUS_ALLOC_VIRTQUEUES(device_get_parent(dev), flags,
195 nvqs, info));
196}
197
198int
199virtio_setup_intr(device_t dev, enum intr_type type)
200{
201
202 return (VIRTIO_BUS_SETUP_INTR(device_get_parent(dev), type));
203}
204
205int
206virtio_with_feature(device_t dev, uint64_t feature)
207{
208
209 return (VIRTIO_BUS_WITH_FEATURE(device_get_parent(dev), feature));
210}
211
212void
213virtio_stop(device_t dev)
214{
215
216 VIRTIO_BUS_STOP(device_get_parent(dev));
217}
218
219int
220virtio_reinit(device_t dev, uint64_t features)
221{
222
223 return (VIRTIO_BUS_REINIT(device_get_parent(dev), features));
224}
225
226void
227virtio_reinit_complete(device_t dev)
228{
229
230 VIRTIO_BUS_REINIT_COMPLETE(device_get_parent(dev));
231}
232
233void
234virtio_read_device_config(device_t dev, bus_size_t offset, void *dst, int len)
235{
236
237 VIRTIO_BUS_READ_DEVICE_CONFIG(device_get_parent(dev),
238 offset, dst, len);
239}
240
241void
242virtio_write_device_config(device_t dev, bus_size_t offset, void *dst, int len)
243{
244
245 VIRTIO_BUS_WRITE_DEVICE_CONFIG(device_get_parent(dev),
246 offset, dst, len);
247}
248
249static int
250virtio_modevent(module_t mod, int type, void *unused)
251{
252 int error;
253
254 error = 0;
255
256 switch (type) {
257 case MOD_LOAD:
258 case MOD_QUIESCE:
259 case MOD_UNLOAD:
260 case MOD_SHUTDOWN:
254 switch (type) {
255 case MOD_LOAD:
256 case MOD_QUIESCE:
257 case MOD_UNLOAD:
258 case MOD_SHUTDOWN:
259 error = 0;
261 break;
262 default:
263 error = EOPNOTSUPP;
264 break;
265 }
266
267 return (error);
268}
269
270static moduledata_t virtio_mod = {
271 "virtio",
272 virtio_modevent,
273 0
274};
275
276DECLARE_MODULE(virtio, virtio_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
277MODULE_VERSION(virtio, 1);
260 break;
261 default:
262 error = EOPNOTSUPP;
263 break;
264 }
265
266 return (error);
267}
268
269static moduledata_t virtio_mod = {
270 "virtio",
271 virtio_modevent,
272 0
273};
274
275DECLARE_MODULE(virtio, virtio_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
276MODULE_VERSION(virtio, 1);