blob: 951e673e50a4d3f2212faa023dc303edf7b3983d [file] [log] [blame]
Anthony Liguori33436602007-11-12 21:30:26 -06001/*
2 * Virtio PCI driver
3 *
4 * This module allows virtio devices to be used over a virtual PCI device.
5 * This can be used with QEMU based VMMs like KVM or Xen.
6 *
7 * Copyright IBM Corp. 2007
8 *
9 * Authors:
10 * Anthony Liguori <aliguori@us.ibm.com>
11 *
12 * This work is licensed under the terms of the GNU GPL, version 2 or later.
13 * See the COPYING file in the top-level directory.
14 *
15 */
16
17#include <linux/module.h>
18#include <linux/list.h>
19#include <linux/pci.h>
20#include <linux/interrupt.h>
21#include <linux/virtio.h>
22#include <linux/virtio_config.h>
23#include <linux/virtio_ring.h>
24#include <linux/virtio_pci.h>
25#include <linux/highmem.h>
26#include <linux/spinlock.h>
27
28MODULE_AUTHOR("Anthony Liguori <aliguori@us.ibm.com>");
29MODULE_DESCRIPTION("virtio-pci");
30MODULE_LICENSE("GPL");
31MODULE_VERSION("1");
32
33/* Our device structure */
34struct virtio_pci_device
35{
36 struct virtio_device vdev;
37 struct pci_dev *pci_dev;
38
39 /* the IO mapping for the PCI config space */
Al Viro97968352008-03-29 03:09:48 +000040 void __iomem *ioaddr;
Anthony Liguori33436602007-11-12 21:30:26 -060041
42 /* a list of queues so we can dispatch IRQs */
43 spinlock_t lock;
44 struct list_head virtqueues;
45};
46
47struct virtio_pci_vq_info
48{
49 /* the actual virtqueue */
50 struct virtqueue *vq;
51
52 /* the number of entries in the queue */
53 int num;
54
55 /* the index of the queue */
56 int queue_index;
57
58 /* the virtual address of the ring queue */
59 void *queue;
60
61 /* the list node for the virtqueues list */
62 struct list_head node;
63};
64
65/* Qumranet donated their vendor ID for devices 0x1000 thru 0x10FF. */
66static struct pci_device_id virtio_pci_id_table[] = {
67 { 0x1af4, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
68 { 0 },
69};
70
71MODULE_DEVICE_TABLE(pci, virtio_pci_id_table);
72
73/* A PCI device has it's own struct device and so does a virtio device so
74 * we create a place for the virtio devices to show up in sysfs. I think it
75 * would make more sense for virtio to not insist on having it's own device. */
Mark McLoughlin63d12552008-12-15 12:58:27 +000076static struct device *virtio_pci_root;
Anthony Liguori33436602007-11-12 21:30:26 -060077
Anthony Liguori33436602007-11-12 21:30:26 -060078/* Convert a generic virtio device to our structure */
79static struct virtio_pci_device *to_vp_device(struct virtio_device *vdev)
80{
81 return container_of(vdev, struct virtio_pci_device, vdev);
82}
83
Rusty Russellc45a6812008-05-02 21:50:50 -050084/* virtio config->get_features() implementation */
85static u32 vp_get_features(struct virtio_device *vdev)
Anthony Liguori33436602007-11-12 21:30:26 -060086{
87 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
Anthony Liguori33436602007-11-12 21:30:26 -060088
Rusty Russellc45a6812008-05-02 21:50:50 -050089 /* When someone needs more than 32 feature bits, we'll need to
90 * steal a bit to indicate that the rest are somewhere else. */
91 return ioread32(vp_dev->ioaddr + VIRTIO_PCI_HOST_FEATURES);
92}
Anthony Liguori33436602007-11-12 21:30:26 -060093
Rusty Russellc6248962008-07-25 12:06:07 -050094/* virtio config->finalize_features() implementation */
95static void vp_finalize_features(struct virtio_device *vdev)
Rusty Russellc45a6812008-05-02 21:50:50 -050096{
97 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
98
Rusty Russelle34f8722008-07-25 12:06:13 -050099 /* Give virtio_ring a chance to accept features. */
100 vring_transport_features(vdev);
101
Rusty Russellc6248962008-07-25 12:06:07 -0500102 /* We only support 32 feature bits. */
103 BUILD_BUG_ON(ARRAY_SIZE(vdev->features) != 1);
104 iowrite32(vdev->features[0], vp_dev->ioaddr+VIRTIO_PCI_GUEST_FEATURES);
Anthony Liguori33436602007-11-12 21:30:26 -0600105}
106
107/* virtio config->get() implementation */
108static void vp_get(struct virtio_device *vdev, unsigned offset,
109 void *buf, unsigned len)
110{
111 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
Al Viro97968352008-03-29 03:09:48 +0000112 void __iomem *ioaddr = vp_dev->ioaddr + VIRTIO_PCI_CONFIG + offset;
Anthony Liguori33436602007-11-12 21:30:26 -0600113 u8 *ptr = buf;
114 int i;
115
116 for (i = 0; i < len; i++)
117 ptr[i] = ioread8(ioaddr + i);
118}
119
120/* the config->set() implementation. it's symmetric to the config->get()
121 * implementation */
122static void vp_set(struct virtio_device *vdev, unsigned offset,
123 const void *buf, unsigned len)
124{
125 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
Al Viro97968352008-03-29 03:09:48 +0000126 void __iomem *ioaddr = vp_dev->ioaddr + VIRTIO_PCI_CONFIG + offset;
Anthony Liguori33436602007-11-12 21:30:26 -0600127 const u8 *ptr = buf;
128 int i;
129
130 for (i = 0; i < len; i++)
131 iowrite8(ptr[i], ioaddr + i);
132}
133
134/* config->{get,set}_status() implementations */
135static u8 vp_get_status(struct virtio_device *vdev)
136{
137 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
138 return ioread8(vp_dev->ioaddr + VIRTIO_PCI_STATUS);
139}
140
141static void vp_set_status(struct virtio_device *vdev, u8 status)
142{
143 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
144 /* We should never be setting status to 0. */
145 BUG_ON(status == 0);
Harvey Harrison597d56e2008-03-31 17:53:55 -0700146 iowrite8(status, vp_dev->ioaddr + VIRTIO_PCI_STATUS);
Anthony Liguori33436602007-11-12 21:30:26 -0600147}
148
149static void vp_reset(struct virtio_device *vdev)
150{
151 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
152 /* 0 status means a reset. */
Harvey Harrison597d56e2008-03-31 17:53:55 -0700153 iowrite8(0, vp_dev->ioaddr + VIRTIO_PCI_STATUS);
Anthony Liguori33436602007-11-12 21:30:26 -0600154}
155
156/* the notify function used when creating a virt queue */
157static void vp_notify(struct virtqueue *vq)
158{
159 struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
160 struct virtio_pci_vq_info *info = vq->priv;
161
162 /* we write the queue's selector into the notification register to
163 * signal the other end */
164 iowrite16(info->queue_index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_NOTIFY);
165}
166
Michael S. Tsirkin77cf52462009-05-14 13:55:31 +0300167/* Handle a configuration change: Tell driver if it wants to know. */
168static irqreturn_t vp_config_changed(int irq, void *opaque)
169{
170 struct virtio_pci_device *vp_dev = opaque;
171 struct virtio_driver *drv;
172 drv = container_of(vp_dev->vdev.dev.driver,
173 struct virtio_driver, driver);
174
175 if (drv && drv->config_changed)
176 drv->config_changed(&vp_dev->vdev);
177 return IRQ_HANDLED;
178}
179
180/* Notify all virtqueues on an interrupt. */
181static irqreturn_t vp_vring_interrupt(int irq, void *opaque)
182{
183 struct virtio_pci_device *vp_dev = opaque;
184 struct virtio_pci_vq_info *info;
185 irqreturn_t ret = IRQ_NONE;
186 unsigned long flags;
187
188 spin_lock_irqsave(&vp_dev->lock, flags);
189 list_for_each_entry(info, &vp_dev->virtqueues, node) {
190 if (vring_interrupt(irq, info->vq) == IRQ_HANDLED)
191 ret = IRQ_HANDLED;
192 }
193 spin_unlock_irqrestore(&vp_dev->lock, flags);
194
195 return ret;
196}
197
Anthony Liguori33436602007-11-12 21:30:26 -0600198/* A small wrapper to also acknowledge the interrupt when it's handled.
199 * I really need an EIO hook for the vring so I can ack the interrupt once we
200 * know that we'll be handling the IRQ but before we invoke the callback since
201 * the callback may notify the host which results in the host attempting to
202 * raise an interrupt that we would then mask once we acknowledged the
203 * interrupt. */
204static irqreturn_t vp_interrupt(int irq, void *opaque)
205{
206 struct virtio_pci_device *vp_dev = opaque;
Anthony Liguori33436602007-11-12 21:30:26 -0600207 u8 isr;
208
209 /* reading the ISR has the effect of also clearing it so it's very
210 * important to save off the value. */
211 isr = ioread8(vp_dev->ioaddr + VIRTIO_PCI_ISR);
212
213 /* It's definitely not us if the ISR was not high */
214 if (!isr)
215 return IRQ_NONE;
216
217 /* Configuration change? Tell driver if it wants to know. */
Michael S. Tsirkin77cf52462009-05-14 13:55:31 +0300218 if (isr & VIRTIO_PCI_ISR_CONFIG)
219 vp_config_changed(irq, opaque);
Anthony Liguori33436602007-11-12 21:30:26 -0600220
Michael S. Tsirkin77cf52462009-05-14 13:55:31 +0300221 return vp_vring_interrupt(irq, opaque);
Anthony Liguori33436602007-11-12 21:30:26 -0600222}
223
224/* the config->find_vq() implementation */
225static struct virtqueue *vp_find_vq(struct virtio_device *vdev, unsigned index,
Rusty Russell9499f5e2009-06-12 22:16:35 -0600226 void (*callback)(struct virtqueue *vq),
227 const char *name)
Anthony Liguori33436602007-11-12 21:30:26 -0600228{
229 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
230 struct virtio_pci_vq_info *info;
231 struct virtqueue *vq;
Hollis Blanchard13b1eb32008-12-02 16:24:40 -0600232 unsigned long flags, size;
Anthony Liguori33436602007-11-12 21:30:26 -0600233 u16 num;
234 int err;
235
236 /* Select the queue we're interested in */
237 iowrite16(index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_SEL);
238
239 /* Check if queue is either not available or already active. */
240 num = ioread16(vp_dev->ioaddr + VIRTIO_PCI_QUEUE_NUM);
241 if (!num || ioread32(vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN))
242 return ERR_PTR(-ENOENT);
243
244 /* allocate and fill out our structure the represents an active
245 * queue */
246 info = kmalloc(sizeof(struct virtio_pci_vq_info), GFP_KERNEL);
247 if (!info)
248 return ERR_PTR(-ENOMEM);
249
250 info->queue_index = index;
251 info->num = num;
252
Rusty Russell498af142008-12-30 09:25:57 -0600253 size = PAGE_ALIGN(vring_size(num, VIRTIO_PCI_VRING_ALIGN));
Hollis Blanchard13b1eb32008-12-02 16:24:40 -0600254 info->queue = alloc_pages_exact(size, GFP_KERNEL|__GFP_ZERO);
Anthony Liguori33436602007-11-12 21:30:26 -0600255 if (info->queue == NULL) {
256 err = -ENOMEM;
257 goto out_info;
258 }
259
260 /* activate the queue */
Rusty Russell480daab2008-12-30 09:25:56 -0600261 iowrite32(virt_to_phys(info->queue) >> VIRTIO_PCI_QUEUE_ADDR_SHIFT,
Anthony Liguori33436602007-11-12 21:30:26 -0600262 vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN);
263
264 /* create the vring */
Rusty Russell87c7d572008-12-30 09:26:03 -0600265 vq = vring_new_virtqueue(info->num, VIRTIO_PCI_VRING_ALIGN,
Rusty Russell9499f5e2009-06-12 22:16:35 -0600266 vdev, info->queue, vp_notify, callback, name);
Anthony Liguori33436602007-11-12 21:30:26 -0600267 if (!vq) {
268 err = -ENOMEM;
269 goto out_activate_queue;
270 }
271
272 vq->priv = info;
273 info->vq = vq;
274
Anthony Liguori27ebe302008-03-02 16:37:48 -0600275 spin_lock_irqsave(&vp_dev->lock, flags);
Anthony Liguori33436602007-11-12 21:30:26 -0600276 list_add(&info->node, &vp_dev->virtqueues);
Anthony Liguori27ebe302008-03-02 16:37:48 -0600277 spin_unlock_irqrestore(&vp_dev->lock, flags);
Anthony Liguori33436602007-11-12 21:30:26 -0600278
279 return vq;
280
281out_activate_queue:
282 iowrite32(0, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN);
Hollis Blanchard13b1eb32008-12-02 16:24:40 -0600283 free_pages_exact(info->queue, size);
Anthony Liguori33436602007-11-12 21:30:26 -0600284out_info:
285 kfree(info);
286 return ERR_PTR(err);
287}
288
289/* the config->del_vq() implementation */
290static void vp_del_vq(struct virtqueue *vq)
291{
292 struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
293 struct virtio_pci_vq_info *info = vq->priv;
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600294 unsigned long size;
Anthony Liguori33436602007-11-12 21:30:26 -0600295
296 vring_del_virtqueue(vq);
297
298 /* Select and deactivate the queue */
299 iowrite16(info->queue_index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_SEL);
300 iowrite32(0, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN);
301
Rusty Russell498af142008-12-30 09:25:57 -0600302 size = PAGE_ALIGN(vring_size(info->num, VIRTIO_PCI_VRING_ALIGN));
Hollis Blanchard13b1eb32008-12-02 16:24:40 -0600303 free_pages_exact(info->queue, size);
Anthony Liguori33436602007-11-12 21:30:26 -0600304 kfree(info);
305}
306
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600307static void vp_del_vqs(struct virtio_device *vdev)
308{
309 struct virtqueue *vq, *n;
310
311 list_for_each_entry_safe(vq, n, &vdev->vqs, list)
312 vp_del_vq(vq);
313}
314
315static int vp_find_vqs(struct virtio_device *vdev, unsigned nvqs,
316 struct virtqueue *vqs[],
317 vq_callback_t *callbacks[],
318 const char *names[])
319{
320 int i;
321
322 for (i = 0; i < nvqs; ++i) {
323 vqs[i] = vp_find_vq(vdev, i, callbacks[i], names[i]);
324 if (IS_ERR(vqs[i]))
325 goto error;
326 }
327 return 0;
328
329error:
330 vp_del_vqs(vdev);
331 return PTR_ERR(vqs[i]);
332}
333
Anthony Liguori33436602007-11-12 21:30:26 -0600334static struct virtio_config_ops virtio_pci_config_ops = {
Anthony Liguori33436602007-11-12 21:30:26 -0600335 .get = vp_get,
336 .set = vp_set,
337 .get_status = vp_get_status,
338 .set_status = vp_set_status,
339 .reset = vp_reset,
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600340 .find_vqs = vp_find_vqs,
341 .del_vqs = vp_del_vqs,
Rusty Russellc45a6812008-05-02 21:50:50 -0500342 .get_features = vp_get_features,
Rusty Russellc6248962008-07-25 12:06:07 -0500343 .finalize_features = vp_finalize_features,
Anthony Liguori33436602007-11-12 21:30:26 -0600344};
345
Mark McLoughlin29f9f122008-12-10 17:45:34 +0000346static void virtio_pci_release_dev(struct device *_d)
347{
348 struct virtio_device *dev = container_of(_d, struct virtio_device, dev);
349 struct virtio_pci_device *vp_dev = to_vp_device(dev);
350 struct pci_dev *pci_dev = vp_dev->pci_dev;
351
352 free_irq(pci_dev->irq, vp_dev);
353 pci_set_drvdata(pci_dev, NULL);
354 pci_iounmap(pci_dev, vp_dev->ioaddr);
355 pci_release_regions(pci_dev);
356 pci_disable_device(pci_dev);
357 kfree(vp_dev);
358}
359
Anthony Liguori33436602007-11-12 21:30:26 -0600360/* the PCI probing function */
361static int __devinit virtio_pci_probe(struct pci_dev *pci_dev,
362 const struct pci_device_id *id)
363{
364 struct virtio_pci_device *vp_dev;
365 int err;
366
367 /* We only own devices >= 0x1000 and <= 0x103f: leave the rest. */
368 if (pci_dev->device < 0x1000 || pci_dev->device > 0x103f)
369 return -ENODEV;
370
Anthony Liguori55a7c062008-01-28 09:59:59 -0600371 if (pci_dev->revision != VIRTIO_PCI_ABI_VERSION) {
372 printk(KERN_ERR "virtio_pci: expected ABI version %d, got %d\n",
373 VIRTIO_PCI_ABI_VERSION, pci_dev->revision);
374 return -ENODEV;
375 }
376
Anthony Liguori33436602007-11-12 21:30:26 -0600377 /* allocate our structure and fill it out */
378 vp_dev = kzalloc(sizeof(struct virtio_pci_device), GFP_KERNEL);
379 if (vp_dev == NULL)
380 return -ENOMEM;
381
Mark McLoughlin63d12552008-12-15 12:58:27 +0000382 vp_dev->vdev.dev.parent = virtio_pci_root;
Mark McLoughlin29f9f122008-12-10 17:45:34 +0000383 vp_dev->vdev.dev.release = virtio_pci_release_dev;
Anthony Liguori33436602007-11-12 21:30:26 -0600384 vp_dev->vdev.config = &virtio_pci_config_ops;
385 vp_dev->pci_dev = pci_dev;
386 INIT_LIST_HEAD(&vp_dev->virtqueues);
387 spin_lock_init(&vp_dev->lock);
388
389 /* enable the device */
390 err = pci_enable_device(pci_dev);
391 if (err)
392 goto out;
393
394 err = pci_request_regions(pci_dev, "virtio-pci");
395 if (err)
396 goto out_enable_device;
397
398 vp_dev->ioaddr = pci_iomap(pci_dev, 0, 0);
399 if (vp_dev->ioaddr == NULL)
400 goto out_req_regions;
401
402 pci_set_drvdata(pci_dev, vp_dev);
403
404 /* we use the subsystem vendor/device id as the virtio vendor/device
405 * id. this allows us to use the same PCI vendor/device id for all
406 * virtio devices and to identify the particular virtio driver by
407 * the subsytem ids */
408 vp_dev->vdev.id.vendor = pci_dev->subsystem_vendor;
409 vp_dev->vdev.id.device = pci_dev->subsystem_device;
410
411 /* register a handler for the queue with the PCI device's interrupt */
412 err = request_irq(vp_dev->pci_dev->irq, vp_interrupt, IRQF_SHARED,
Kay Sievers99e0b6c2008-12-30 09:25:56 -0600413 dev_name(&vp_dev->vdev.dev), vp_dev);
Anthony Liguori33436602007-11-12 21:30:26 -0600414 if (err)
415 goto out_set_drvdata;
416
417 /* finally register the virtio device */
418 err = register_virtio_device(&vp_dev->vdev);
419 if (err)
420 goto out_req_irq;
421
422 return 0;
423
424out_req_irq:
425 free_irq(pci_dev->irq, vp_dev);
426out_set_drvdata:
427 pci_set_drvdata(pci_dev, NULL);
428 pci_iounmap(pci_dev, vp_dev->ioaddr);
429out_req_regions:
430 pci_release_regions(pci_dev);
431out_enable_device:
432 pci_disable_device(pci_dev);
433out:
434 kfree(vp_dev);
435 return err;
436}
437
438static void __devexit virtio_pci_remove(struct pci_dev *pci_dev)
439{
440 struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
441
Anthony Liguoribd6c2692008-03-19 20:35:04 -0500442 unregister_virtio_device(&vp_dev->vdev);
Anthony Liguori33436602007-11-12 21:30:26 -0600443}
444
445#ifdef CONFIG_PM
446static int virtio_pci_suspend(struct pci_dev *pci_dev, pm_message_t state)
447{
448 pci_save_state(pci_dev);
449 pci_set_power_state(pci_dev, PCI_D3hot);
450 return 0;
451}
452
453static int virtio_pci_resume(struct pci_dev *pci_dev)
454{
455 pci_restore_state(pci_dev);
456 pci_set_power_state(pci_dev, PCI_D0);
457 return 0;
458}
459#endif
460
461static struct pci_driver virtio_pci_driver = {
462 .name = "virtio-pci",
463 .id_table = virtio_pci_id_table,
464 .probe = virtio_pci_probe,
465 .remove = virtio_pci_remove,
466#ifdef CONFIG_PM
467 .suspend = virtio_pci_suspend,
468 .resume = virtio_pci_resume,
469#endif
470};
471
472static int __init virtio_pci_init(void)
473{
474 int err;
475
Mark McLoughlin63d12552008-12-15 12:58:27 +0000476 virtio_pci_root = root_device_register("virtio-pci");
477 if (IS_ERR(virtio_pci_root))
478 return PTR_ERR(virtio_pci_root);
Anthony Liguori33436602007-11-12 21:30:26 -0600479
480 err = pci_register_driver(&virtio_pci_driver);
481 if (err)
Mark McLoughlin63d12552008-12-15 12:58:27 +0000482 device_unregister(virtio_pci_root);
Anthony Liguori33436602007-11-12 21:30:26 -0600483
484 return err;
485}
486
487module_init(virtio_pci_init);
488
489static void __exit virtio_pci_exit(void)
490{
Anthony Liguori33436602007-11-12 21:30:26 -0600491 pci_unregister_driver(&virtio_pci_driver);
Mark McLoughlin63d12552008-12-15 12:58:27 +0000492 root_device_unregister(virtio_pci_root);
Anthony Liguori33436602007-11-12 21:30:26 -0600493}
494
495module_exit(virtio_pci_exit);