aboutsummaryrefslogtreecommitdiffstats
path: root/drivers
diff options
context:
space:
mode:
authorRusty Russell <rusty@rustcorp.com.au>2008-02-04 23:49:56 -0500
committerRusty Russell <rusty@rustcorp.com.au>2008-02-04 07:49:57 -0500
commita586d4f6016f7139d8c26df0e6927131168d3b5b (patch)
tree1c47e1a6b6b8fb18baa42f32980f29c4ae9cbbdc /drivers
parentf35d9d8aae08940b7fdd1bb8110619da2ece6b28 (diff)
virtio: simplify config mechanism.
Previously we used a type/len pair within the config space, but this seems overkill. We now simply define a structure which represents the layout in the config space: the config space can now only be extended at the end. The main driver-visible changes: 1) We indicate what fields are present with an explicit feature bit. 2) Virtqueues are explicitly numbered, and not in the config space. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Diffstat (limited to 'drivers')
-rw-r--r--drivers/block/virtio_blk.c35
-rw-r--r--drivers/char/virtio_console.c4
-rw-r--r--drivers/lguest/lguest_device.c132
-rw-r--r--drivers/net/virtio_net.c25
-rw-r--r--drivers/virtio/virtio.c45
5 files changed, 100 insertions, 141 deletions
diff --git a/drivers/block/virtio_blk.c b/drivers/block/virtio_blk.c
index 924ddd8bccd2..1c63d5b64c20 100644
--- a/drivers/block/virtio_blk.c
+++ b/drivers/block/virtio_blk.c
@@ -162,8 +162,6 @@ static int virtblk_probe(struct virtio_device *vdev)
162{ 162{
163 struct virtio_blk *vblk; 163 struct virtio_blk *vblk;
164 int err, major; 164 int err, major;
165 void *token;
166 unsigned int len;
167 u64 cap; 165 u64 cap;
168 u32 v; 166 u32 v;
169 167
@@ -178,7 +176,7 @@ static int virtblk_probe(struct virtio_device *vdev)
178 vblk->vdev = vdev; 176 vblk->vdev = vdev;
179 177
180 /* We expect one virtqueue, for output. */ 178 /* We expect one virtqueue, for output. */
181 vblk->vq = vdev->config->find_vq(vdev, blk_done); 179 vblk->vq = vdev->config->find_vq(vdev, 0, blk_done);
182 if (IS_ERR(vblk->vq)) { 180 if (IS_ERR(vblk->vq)) {
183 err = PTR_ERR(vblk->vq); 181 err = PTR_ERR(vblk->vq);
184 goto out_free_vblk; 182 goto out_free_vblk;
@@ -216,15 +214,12 @@ static int virtblk_probe(struct virtio_device *vdev)
216 vblk->disk->fops = &virtblk_fops; 214 vblk->disk->fops = &virtblk_fops;
217 215
218 /* If barriers are supported, tell block layer that queue is ordered */ 216 /* If barriers are supported, tell block layer that queue is ordered */
219 token = vdev->config->find(vdev, VIRTIO_CONFIG_BLK_F, &len); 217 if (vdev->config->feature(vdev, VIRTIO_BLK_F_BARRIER))
220 if (virtio_use_bit(vdev, token, len, VIRTIO_BLK_F_BARRIER))
221 blk_queue_ordered(vblk->disk->queue, QUEUE_ORDERED_TAG, NULL); 218 blk_queue_ordered(vblk->disk->queue, QUEUE_ORDERED_TAG, NULL);
222 219
223 err = virtio_config_val(vdev, VIRTIO_CONFIG_BLK_F_CAPACITY, &cap); 220 /* Host must always specify the capacity. */
224 if (err) { 221 __virtio_config_val(vdev, offsetof(struct virtio_blk_config, capacity),
225 dev_err(&vdev->dev, "Bad/missing capacity in config\n"); 222 &cap);
226 goto out_cleanup_queue;
227 }
228 223
229 /* If capacity is too big, truncate with warning. */ 224 /* If capacity is too big, truncate with warning. */
230 if ((sector_t)cap != cap) { 225 if ((sector_t)cap != cap) {
@@ -234,27 +229,23 @@ static int virtblk_probe(struct virtio_device *vdev)
234 } 229 }
235 set_capacity(vblk->disk, cap); 230 set_capacity(vblk->disk, cap);
236 231
237 err = virtio_config_val(vdev, VIRTIO_CONFIG_BLK_F_SIZE_MAX, &v); 232 /* Host can optionally specify maximum segment size and number of
233 * segments. */
234 err = virtio_config_val(vdev, VIRTIO_BLK_F_SIZE_MAX,
235 offsetof(struct virtio_blk_config, size_max),
236 &v);
238 if (!err) 237 if (!err)
239 blk_queue_max_segment_size(vblk->disk->queue, v); 238 blk_queue_max_segment_size(vblk->disk->queue, v);
240 else if (err != -ENOENT) {
241 dev_err(&vdev->dev, "Bad SIZE_MAX in config\n");
242 goto out_cleanup_queue;
243 }
244 239
245 err = virtio_config_val(vdev, VIRTIO_CONFIG_BLK_F_SEG_MAX, &v); 240 err = virtio_config_val(vdev, VIRTIO_BLK_F_SEG_MAX,
241 offsetof(struct virtio_blk_config, seg_max),
242 &v);
246 if (!err) 243 if (!err)
247 blk_queue_max_hw_segments(vblk->disk->queue, v); 244 blk_queue_max_hw_segments(vblk->disk->queue, v);
248 else if (err != -ENOENT) {
249 dev_err(&vdev->dev, "Bad SEG_MAX in config\n");
250 goto out_cleanup_queue;
251 }
252 245
253 add_disk(vblk->disk); 246 add_disk(vblk->disk);
254 return 0; 247 return 0;
255 248
256out_cleanup_queue:
257 blk_cleanup_queue(vblk->disk->queue);
258out_put_disk: 249out_put_disk:
259 put_disk(vblk->disk); 250 put_disk(vblk->disk);
260out_unregister_blkdev: 251out_unregister_blkdev:
diff --git a/drivers/char/virtio_console.c b/drivers/char/virtio_console.c
index e34da5c97196..dc17fe3a88bc 100644
--- a/drivers/char/virtio_console.c
+++ b/drivers/char/virtio_console.c
@@ -158,13 +158,13 @@ static int __devinit virtcons_probe(struct virtio_device *dev)
158 /* Find the input queue. */ 158 /* Find the input queue. */
159 /* FIXME: This is why we want to wean off hvc: we do nothing 159 /* FIXME: This is why we want to wean off hvc: we do nothing
160 * when input comes in. */ 160 * when input comes in. */
161 in_vq = vdev->config->find_vq(vdev, NULL); 161 in_vq = vdev->config->find_vq(vdev, 0, NULL);
162 if (IS_ERR(in_vq)) { 162 if (IS_ERR(in_vq)) {
163 err = PTR_ERR(in_vq); 163 err = PTR_ERR(in_vq);
164 goto free; 164 goto free;
165 } 165 }
166 166
167 out_vq = vdev->config->find_vq(vdev, NULL); 167 out_vq = vdev->config->find_vq(vdev, 1, NULL);
168 if (IS_ERR(out_vq)) { 168 if (IS_ERR(out_vq)) {
169 err = PTR_ERR(out_vq); 169 err = PTR_ERR(out_vq);
170 goto free_in_vq; 170 goto free_in_vq;
diff --git a/drivers/lguest/lguest_device.c b/drivers/lguest/lguest_device.c
index e2eec38c83c2..07f57a53658b 100644
--- a/drivers/lguest/lguest_device.c
+++ b/drivers/lguest/lguest_device.c
@@ -52,57 +52,82 @@ struct lguest_device {
52/*D:130 52/*D:130
53 * Device configurations 53 * Device configurations
54 * 54 *
55 * The configuration information for a device consists of a series of fields. 55 * The configuration information for a device consists of one or more
56 * We don't really care what they are: the Launcher set them up, and the driver 56 * virtqueues, a feature bitmaks, and some configuration bytes. The
57 * will look at them during setup. 57 * configuration bytes don't really matter to us: the Launcher set them up, and
58 * the driver will look at them during setup.
58 * 59 *
59 * For us these fields come immediately after that device's descriptor in the 60 * A convenient routine to return the device's virtqueue config array:
60 * lguest_devices page. 61 * immediately after the descriptor. */
61 * 62static struct lguest_vqconfig *lg_vq(const struct lguest_device_desc *desc)
62 * Each field starts with a "type" byte, a "length" byte, then that number of 63{
63 * bytes of configuration information. The device descriptor tells us the 64 return (void *)(desc + 1);
64 * total configuration length so we know when we've reached the last field. */ 65}
65 66
66/* type + length bytes */ 67/* The features come immediately after the virtqueues. */
67#define FHDR_LEN 2 68static u8 *lg_features(const struct lguest_device_desc *desc)
69{
70 return (void *)(lg_vq(desc) + desc->num_vq);
71}
68 72
69/* This finds the first field of a given type for a device's configuration. */ 73/* The config space comes after the two feature bitmasks. */
70static void *lg_find(struct virtio_device *vdev, u8 type, unsigned int *len) 74static u8 *lg_config(const struct lguest_device_desc *desc)
71{ 75{
72 struct lguest_device_desc *desc = to_lgdev(vdev)->desc; 76 return lg_features(desc) + desc->feature_len * 2;
73 int i; 77}
74
75 for (i = 0; i < desc->config_len; i += FHDR_LEN + desc->config[i+1]) {
76 if (desc->config[i] == type) {
77 /* Mark it used, so Host can know we looked at it, and
78 * also so we won't find the same one twice. */
79 desc->config[i] |= 0x80;
80 /* Remember, the second byte is the length. */
81 *len = desc->config[i+1];
82 /* We return a pointer to the field header. */
83 return desc->config + i;
84 }
85 }
86 78
87 /* Not found: return NULL for failure. */ 79/* The total size of the config page used by this device (incl. desc) */
88 return NULL; 80static unsigned desc_size(const struct lguest_device_desc *desc)
81{
82 return sizeof(*desc)
83 + desc->num_vq * sizeof(struct lguest_vqconfig)
84 + desc->feature_len * 2
85 + desc->config_len;
86}
87
88/* This tests (and acknowleges) a feature bit. */
89static bool lg_feature(struct virtio_device *vdev, unsigned fbit)
90{
91 struct lguest_device_desc *desc = to_lgdev(vdev)->desc;
92 u8 *features;
93
94 /* Obviously if they ask for a feature off the end of our feature
95 * bitmap, it's not set. */
96 if (fbit / 8 > desc->feature_len)
97 return false;
98
99 /* The feature bitmap comes after the virtqueues. */
100 features = lg_features(desc);
101 if (!(features[fbit / 8] & (1 << (fbit % 8))))
102 return false;
103
104 /* We set the matching bit in the other half of the bitmap to tell the
105 * Host we want to use this feature. We don't use this yet, but we
106 * could in future. */
107 features[desc->feature_len + fbit / 8] |= (1 << (fbit % 8));
108 return true;
89} 109}
90 110
91/* Once they've found a field, getting a copy of it is easy. */ 111/* Once they've found a field, getting a copy of it is easy. */
92static void lg_get(struct virtio_device *vdev, void *token, 112static void lg_get(struct virtio_device *vdev, unsigned int offset,
93 void *buf, unsigned len) 113 void *buf, unsigned len)
94{ 114{
95 /* Check they didn't ask for more than the length of the field! */ 115 struct lguest_device_desc *desc = to_lgdev(vdev)->desc;
96 BUG_ON(len > ((u8 *)token)[1]); 116
97 memcpy(buf, token + FHDR_LEN, len); 117 /* Check they didn't ask for more than the length of the config! */
118 BUG_ON(offset + len > desc->config_len);
119 memcpy(buf, lg_config(desc) + offset, len);
98} 120}
99 121
100/* Setting the contents is also trivial. */ 122/* Setting the contents is also trivial. */
101static void lg_set(struct virtio_device *vdev, void *token, 123static void lg_set(struct virtio_device *vdev, unsigned int offset,
102 const void *buf, unsigned len) 124 const void *buf, unsigned len)
103{ 125{
104 BUG_ON(len > ((u8 *)token)[1]); 126 struct lguest_device_desc *desc = to_lgdev(vdev)->desc;
105 memcpy(token + FHDR_LEN, buf, len); 127
128 /* Check they didn't ask for more than the length of the config! */
129 BUG_ON(offset + len > desc->config_len);
130 memcpy(lg_config(desc) + offset, buf, len);
106} 131}
107 132
108/* The operations to get and set the status word just access the status field 133/* The operations to get and set the status word just access the status field
@@ -165,39 +190,29 @@ static void lg_notify(struct virtqueue *vq)
165 * 190 *
166 * So we provide devices with a "find virtqueue and set it up" function. */ 191 * So we provide devices with a "find virtqueue and set it up" function. */
167static struct virtqueue *lg_find_vq(struct virtio_device *vdev, 192static struct virtqueue *lg_find_vq(struct virtio_device *vdev,
193 unsigned index,
168 bool (*callback)(struct virtqueue *vq)) 194 bool (*callback)(struct virtqueue *vq))
169{ 195{
196 struct lguest_device *ldev = to_lgdev(vdev);
170 struct lguest_vq_info *lvq; 197 struct lguest_vq_info *lvq;
171 struct virtqueue *vq; 198 struct virtqueue *vq;
172 unsigned int len;
173 void *token;
174 int err; 199 int err;
175 200
176 /* Look for a field of the correct type to mark a virtqueue. Note that 201 /* We must have this many virtqueues. */
177 * if this succeeds, then the type will be changed so it won't be found 202 if (index >= ldev->desc->num_vq)
178 * again, and future lg_find_vq() calls will find the next
179 * virtqueue (if any). */
180 token = vdev->config->find(vdev, VIRTIO_CONFIG_F_VIRTQUEUE, &len);
181 if (!token)
182 return ERR_PTR(-ENOENT); 203 return ERR_PTR(-ENOENT);
183 204
184 lvq = kmalloc(sizeof(*lvq), GFP_KERNEL); 205 lvq = kmalloc(sizeof(*lvq), GFP_KERNEL);
185 if (!lvq) 206 if (!lvq)
186 return ERR_PTR(-ENOMEM); 207 return ERR_PTR(-ENOMEM);
187 208
188 /* Note: we could use a configuration space inside here, just like we 209 /* Make a copy of the "struct lguest_vqconfig" entry, which sits after
189 * do for the device. This would allow expansion in future, because 210 * the descriptor. We need a copy because the config space might not
190 * our configuration system is designed to be expansible. But this is 211 * be aligned correctly. */
191 * way easier. */ 212 memcpy(&lvq->config, lg_vq(ldev->desc)+index, sizeof(lvq->config));
192 if (len != sizeof(lvq->config)) {
193 dev_err(&vdev->dev, "Unexpected virtio config len %u\n", len);
194 err = -EIO;
195 goto free_lvq;
196 }
197 /* Make a copy of the "struct lguest_vqconfig" field. We need a copy
198 * because the config space might not be aligned correctly. */
199 vdev->config->get(vdev, token, &lvq->config, sizeof(lvq->config));
200 213
214 printk("Mapping virtqueue %i addr %lx\n", index,
215 (unsigned long)lvq->config.pfn << PAGE_SHIFT);
201 /* Figure out how many pages the ring will take, and map that memory */ 216 /* Figure out how many pages the ring will take, and map that memory */
202 lvq->pages = lguest_map((unsigned long)lvq->config.pfn << PAGE_SHIFT, 217 lvq->pages = lguest_map((unsigned long)lvq->config.pfn << PAGE_SHIFT,
203 DIV_ROUND_UP(vring_size(lvq->config.num, 218 DIV_ROUND_UP(vring_size(lvq->config.num,
@@ -259,7 +274,7 @@ static void lg_del_vq(struct virtqueue *vq)
259 274
260/* The ops structure which hooks everything together. */ 275/* The ops structure which hooks everything together. */
261static struct virtio_config_ops lguest_config_ops = { 276static struct virtio_config_ops lguest_config_ops = {
262 .find = lg_find, 277 .feature = lg_feature,
263 .get = lg_get, 278 .get = lg_get,
264 .set = lg_set, 279 .set = lg_set,
265 .get_status = lg_get_status, 280 .get_status = lg_get_status,
@@ -329,13 +344,14 @@ static void scan_devices(void)
329 struct lguest_device_desc *d; 344 struct lguest_device_desc *d;
330 345
331 /* We start at the page beginning, and skip over each entry. */ 346 /* We start at the page beginning, and skip over each entry. */
332 for (i = 0; i < PAGE_SIZE; i += sizeof(*d) + d->config_len) { 347 for (i = 0; i < PAGE_SIZE; i += desc_size(d)) {
333 d = lguest_devices + i; 348 d = lguest_devices + i;
334 349
335 /* Once we hit a zero, stop. */ 350 /* Once we hit a zero, stop. */
336 if (d->type == 0) 351 if (d->type == 0)
337 break; 352 break;
338 353
354 printk("Device at %i has size %u\n", i, desc_size(d));
339 add_lguest_device(d); 355 add_lguest_device(d);
340 } 356 }
341} 357}
diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
index a60505c8f82a..4b8138312750 100644
--- a/drivers/net/virtio_net.c
+++ b/drivers/net/virtio_net.c
@@ -311,10 +311,8 @@ static int virtnet_close(struct net_device *dev)
311static int virtnet_probe(struct virtio_device *vdev) 311static int virtnet_probe(struct virtio_device *vdev)
312{ 312{
313 int err; 313 int err;
314 unsigned int len;
315 struct net_device *dev; 314 struct net_device *dev;
316 struct virtnet_info *vi; 315 struct virtnet_info *vi;
317 void *token;
318 316
319 /* Allocate ourselves a network device with room for our info */ 317 /* Allocate ourselves a network device with room for our info */
320 dev = alloc_etherdev(sizeof(struct virtnet_info)); 318 dev = alloc_etherdev(sizeof(struct virtnet_info));
@@ -330,25 +328,24 @@ static int virtnet_probe(struct virtio_device *vdev)
330 SET_NETDEV_DEV(dev, &vdev->dev); 328 SET_NETDEV_DEV(dev, &vdev->dev);
331 329
332 /* Do we support "hardware" checksums? */ 330 /* Do we support "hardware" checksums? */
333 token = vdev->config->find(vdev, VIRTIO_CONFIG_NET_F, &len); 331 if (vdev->config->feature(vdev, VIRTIO_NET_F_NO_CSUM)) {
334 if (virtio_use_bit(vdev, token, len, VIRTIO_NET_F_NO_CSUM)) {
335 /* This opens up the world of extra features. */ 332 /* This opens up the world of extra features. */
336 dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST; 333 dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
337 if (virtio_use_bit(vdev, token, len, VIRTIO_NET_F_TSO4)) 334 if (vdev->config->feature(vdev, VIRTIO_NET_F_TSO4))
338 dev->features |= NETIF_F_TSO; 335 dev->features |= NETIF_F_TSO;
339 if (virtio_use_bit(vdev, token, len, VIRTIO_NET_F_UFO)) 336 if (vdev->config->feature(vdev, VIRTIO_NET_F_UFO))
340 dev->features |= NETIF_F_UFO; 337 dev->features |= NETIF_F_UFO;
341 if (virtio_use_bit(vdev, token, len, VIRTIO_NET_F_TSO4_ECN)) 338 if (vdev->config->feature(vdev, VIRTIO_NET_F_TSO4_ECN))
342 dev->features |= NETIF_F_TSO_ECN; 339 dev->features |= NETIF_F_TSO_ECN;
343 if (virtio_use_bit(vdev, token, len, VIRTIO_NET_F_TSO6)) 340 if (vdev->config->feature(vdev, VIRTIO_NET_F_TSO6))
344 dev->features |= NETIF_F_TSO6; 341 dev->features |= NETIF_F_TSO6;
345 } 342 }
346 343
347 /* Configuration may specify what MAC to use. Otherwise random. */ 344 /* Configuration may specify what MAC to use. Otherwise random. */
348 token = vdev->config->find(vdev, VIRTIO_CONFIG_NET_MAC_F, &len); 345 if (vdev->config->feature(vdev, VIRTIO_NET_F_MAC)) {
349 if (token) { 346 vdev->config->get(vdev,
350 dev->addr_len = len; 347 offsetof(struct virtio_net_config, mac),
351 vdev->config->get(vdev, token, dev->dev_addr, len); 348 dev->dev_addr, dev->addr_len);
352 } else 349 } else
353 random_ether_addr(dev->dev_addr); 350 random_ether_addr(dev->dev_addr);
354 351
@@ -359,13 +356,13 @@ static int virtnet_probe(struct virtio_device *vdev)
359 vi->vdev = vdev; 356 vi->vdev = vdev;
360 357
361 /* We expect two virtqueues, receive then send. */ 358 /* We expect two virtqueues, receive then send. */
362 vi->rvq = vdev->config->find_vq(vdev, skb_recv_done); 359 vi->rvq = vdev->config->find_vq(vdev, 0, skb_recv_done);
363 if (IS_ERR(vi->rvq)) { 360 if (IS_ERR(vi->rvq)) {
364 err = PTR_ERR(vi->rvq); 361 err = PTR_ERR(vi->rvq);
365 goto free; 362 goto free;
366 } 363 }
367 364
368 vi->svq = vdev->config->find_vq(vdev, skb_xmit_done); 365 vi->svq = vdev->config->find_vq(vdev, 1, skb_xmit_done);
369 if (IS_ERR(vi->svq)) { 366 if (IS_ERR(vi->svq)) {
370 err = PTR_ERR(vi->svq); 367 err = PTR_ERR(vi->svq);
371 goto free_recv; 368 goto free_recv;
diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c
index 69d7ea02cd48..303cb6f90108 100644
--- a/drivers/virtio/virtio.c
+++ b/drivers/virtio/virtio.c
@@ -148,51 +148,6 @@ void unregister_virtio_device(struct virtio_device *dev)
148} 148}
149EXPORT_SYMBOL_GPL(unregister_virtio_device); 149EXPORT_SYMBOL_GPL(unregister_virtio_device);
150 150
151int __virtio_config_val(struct virtio_device *vdev,
152 u8 type, void *val, size_t size)
153{
154 void *token;
155 unsigned int len;
156
157 token = vdev->config->find(vdev, type, &len);
158 if (!token)
159 return -ENOENT;
160
161 if (len != size)
162 return -EIO;
163
164 vdev->config->get(vdev, token, val, size);
165 return 0;
166}
167EXPORT_SYMBOL_GPL(__virtio_config_val);
168
169int virtio_use_bit(struct virtio_device *vdev,
170 void *token, unsigned int len, unsigned int bitnum)
171{
172 unsigned long bits[16];
173
174 /* This makes it convenient to pass-through find() results. */
175 if (!token)
176 return 0;
177
178 /* bit not in range of this bitfield? */
179 if (bitnum * 8 >= len / 2)
180 return 0;
181
182 /* Giant feature bitfields are silly. */
183 BUG_ON(len > sizeof(bits));
184 vdev->config->get(vdev, token, bits, len);
185
186 if (!test_bit(bitnum, bits))
187 return 0;
188
189 /* Set acknowledge bit, and write it back. */
190 set_bit(bitnum + len * 8 / 2, bits);
191 vdev->config->set(vdev, token, bits, len);
192 return 1;
193}
194EXPORT_SYMBOL_GPL(virtio_use_bit);
195
196static int virtio_init(void) 151static int virtio_init(void)
197{ 152{
198 if (bus_register(&virtio_bus) != 0) 153 if (bus_register(&virtio_bus) != 0)