blob: 7c385af071a66bb3c0b43e85f62a080ed483613e [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. */
76static struct device virtio_pci_root = {
77 .parent = NULL,
78 .bus_id = "virtio-pci",
79};
80
Anthony Liguori33436602007-11-12 21:30:26 -060081/* Convert a generic virtio device to our structure */
82static struct virtio_pci_device *to_vp_device(struct virtio_device *vdev)
83{
84 return container_of(vdev, struct virtio_pci_device, vdev);
85}
86
Rusty Russellc45a6812008-05-02 21:50:50 -050087/* virtio config->get_features() implementation */
88static u32 vp_get_features(struct virtio_device *vdev)
Anthony Liguori33436602007-11-12 21:30:26 -060089{
90 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
Anthony Liguori33436602007-11-12 21:30:26 -060091
Rusty Russellc45a6812008-05-02 21:50:50 -050092 /* When someone needs more than 32 feature bits, we'll need to
93 * steal a bit to indicate that the rest are somewhere else. */
94 return ioread32(vp_dev->ioaddr + VIRTIO_PCI_HOST_FEATURES);
95}
Anthony Liguori33436602007-11-12 21:30:26 -060096
Rusty Russellc6248962008-07-25 12:06:07 -050097/* virtio config->finalize_features() implementation */
98static void vp_finalize_features(struct virtio_device *vdev)
Rusty Russellc45a6812008-05-02 21:50:50 -050099{
100 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
101
Rusty Russelle34f8722008-07-25 12:06:13 -0500102 /* Give virtio_ring a chance to accept features. */
103 vring_transport_features(vdev);
104
Rusty Russellc6248962008-07-25 12:06:07 -0500105 /* We only support 32 feature bits. */
106 BUILD_BUG_ON(ARRAY_SIZE(vdev->features) != 1);
107 iowrite32(vdev->features[0], vp_dev->ioaddr+VIRTIO_PCI_GUEST_FEATURES);
Anthony Liguori33436602007-11-12 21:30:26 -0600108}
109
110/* virtio config->get() implementation */
111static void vp_get(struct virtio_device *vdev, unsigned offset,
112 void *buf, unsigned len)
113{
114 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
Al Viro97968352008-03-29 03:09:48 +0000115 void __iomem *ioaddr = vp_dev->ioaddr + VIRTIO_PCI_CONFIG + offset;
Anthony Liguori33436602007-11-12 21:30:26 -0600116 u8 *ptr = buf;
117 int i;
118
119 for (i = 0; i < len; i++)
120 ptr[i] = ioread8(ioaddr + i);
121}
122
123/* the config->set() implementation. it's symmetric to the config->get()
124 * implementation */
125static void vp_set(struct virtio_device *vdev, unsigned offset,
126 const void *buf, unsigned len)
127{
128 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
Al Viro97968352008-03-29 03:09:48 +0000129 void __iomem *ioaddr = vp_dev->ioaddr + VIRTIO_PCI_CONFIG + offset;
Anthony Liguori33436602007-11-12 21:30:26 -0600130 const u8 *ptr = buf;
131 int i;
132
133 for (i = 0; i < len; i++)
134 iowrite8(ptr[i], ioaddr + i);
135}
136
137/* config->{get,set}_status() implementations */
138static u8 vp_get_status(struct virtio_device *vdev)
139{
140 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
141 return ioread8(vp_dev->ioaddr + VIRTIO_PCI_STATUS);
142}
143
144static void vp_set_status(struct virtio_device *vdev, u8 status)
145{
146 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
147 /* We should never be setting status to 0. */
148 BUG_ON(status == 0);
Harvey Harrison597d56e2008-03-31 17:53:55 -0700149 iowrite8(status, vp_dev->ioaddr + VIRTIO_PCI_STATUS);
Anthony Liguori33436602007-11-12 21:30:26 -0600150}
151
152static void vp_reset(struct virtio_device *vdev)
153{
154 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
155 /* 0 status means a reset. */
Harvey Harrison597d56e2008-03-31 17:53:55 -0700156 iowrite8(0, vp_dev->ioaddr + VIRTIO_PCI_STATUS);
Anthony Liguori33436602007-11-12 21:30:26 -0600157}
158
159/* the notify function used when creating a virt queue */
160static void vp_notify(struct virtqueue *vq)
161{
162 struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
163 struct virtio_pci_vq_info *info = vq->priv;
164
165 /* we write the queue's selector into the notification register to
166 * signal the other end */
167 iowrite16(info->queue_index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_NOTIFY);
168}
169
170/* A small wrapper to also acknowledge the interrupt when it's handled.
171 * I really need an EIO hook for the vring so I can ack the interrupt once we
172 * know that we'll be handling the IRQ but before we invoke the callback since
173 * the callback may notify the host which results in the host attempting to
174 * raise an interrupt that we would then mask once we acknowledged the
175 * interrupt. */
176static irqreturn_t vp_interrupt(int irq, void *opaque)
177{
178 struct virtio_pci_device *vp_dev = opaque;
179 struct virtio_pci_vq_info *info;
180 irqreturn_t ret = IRQ_NONE;
Anthony Liguori27ebe302008-03-02 16:37:48 -0600181 unsigned long flags;
Anthony Liguori33436602007-11-12 21:30:26 -0600182 u8 isr;
183
184 /* reading the ISR has the effect of also clearing it so it's very
185 * important to save off the value. */
186 isr = ioread8(vp_dev->ioaddr + VIRTIO_PCI_ISR);
187
188 /* It's definitely not us if the ISR was not high */
189 if (!isr)
190 return IRQ_NONE;
191
192 /* Configuration change? Tell driver if it wants to know. */
193 if (isr & VIRTIO_PCI_ISR_CONFIG) {
194 struct virtio_driver *drv;
195 drv = container_of(vp_dev->vdev.dev.driver,
196 struct virtio_driver, driver);
197
198 if (drv->config_changed)
199 drv->config_changed(&vp_dev->vdev);
200 }
201
Anthony Liguori27ebe302008-03-02 16:37:48 -0600202 spin_lock_irqsave(&vp_dev->lock, flags);
Anthony Liguori33436602007-11-12 21:30:26 -0600203 list_for_each_entry(info, &vp_dev->virtqueues, node) {
204 if (vring_interrupt(irq, info->vq) == IRQ_HANDLED)
205 ret = IRQ_HANDLED;
206 }
Anthony Liguori27ebe302008-03-02 16:37:48 -0600207 spin_unlock_irqrestore(&vp_dev->lock, flags);
Anthony Liguori33436602007-11-12 21:30:26 -0600208
209 return ret;
210}
211
212/* the config->find_vq() implementation */
213static struct virtqueue *vp_find_vq(struct virtio_device *vdev, unsigned index,
214 void (*callback)(struct virtqueue *vq))
215{
216 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
217 struct virtio_pci_vq_info *info;
218 struct virtqueue *vq;
Hollis Blanchard13b1eb32008-12-02 16:24:40 -0600219 unsigned long flags, size;
Anthony Liguori33436602007-11-12 21:30:26 -0600220 u16 num;
221 int err;
222
223 /* Select the queue we're interested in */
224 iowrite16(index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_SEL);
225
226 /* Check if queue is either not available or already active. */
227 num = ioread16(vp_dev->ioaddr + VIRTIO_PCI_QUEUE_NUM);
228 if (!num || ioread32(vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN))
229 return ERR_PTR(-ENOENT);
230
231 /* allocate and fill out our structure the represents an active
232 * queue */
233 info = kmalloc(sizeof(struct virtio_pci_vq_info), GFP_KERNEL);
234 if (!info)
235 return ERR_PTR(-ENOMEM);
236
237 info->queue_index = index;
238 info->num = num;
239
Hollis Blanchard13b1eb32008-12-02 16:24:40 -0600240 size = PAGE_ALIGN(vring_size(num, PAGE_SIZE));
241 info->queue = alloc_pages_exact(size, GFP_KERNEL|__GFP_ZERO);
Anthony Liguori33436602007-11-12 21:30:26 -0600242 if (info->queue == NULL) {
243 err = -ENOMEM;
244 goto out_info;
245 }
246
247 /* activate the queue */
248 iowrite32(virt_to_phys(info->queue) >> PAGE_SHIFT,
249 vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN);
250
251 /* create the vring */
252 vq = vring_new_virtqueue(info->num, vdev, info->queue,
253 vp_notify, callback);
254 if (!vq) {
255 err = -ENOMEM;
256 goto out_activate_queue;
257 }
258
259 vq->priv = info;
260 info->vq = vq;
261
Anthony Liguori27ebe302008-03-02 16:37:48 -0600262 spin_lock_irqsave(&vp_dev->lock, flags);
Anthony Liguori33436602007-11-12 21:30:26 -0600263 list_add(&info->node, &vp_dev->virtqueues);
Anthony Liguori27ebe302008-03-02 16:37:48 -0600264 spin_unlock_irqrestore(&vp_dev->lock, flags);
Anthony Liguori33436602007-11-12 21:30:26 -0600265
266 return vq;
267
268out_activate_queue:
269 iowrite32(0, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN);
Hollis Blanchard13b1eb32008-12-02 16:24:40 -0600270 free_pages_exact(info->queue, size);
Anthony Liguori33436602007-11-12 21:30:26 -0600271out_info:
272 kfree(info);
273 return ERR_PTR(err);
274}
275
276/* the config->del_vq() implementation */
277static void vp_del_vq(struct virtqueue *vq)
278{
279 struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
280 struct virtio_pci_vq_info *info = vq->priv;
Hollis Blanchard13b1eb32008-12-02 16:24:40 -0600281 unsigned long flags, size;
Anthony Liguori33436602007-11-12 21:30:26 -0600282
Anthony Liguori27ebe302008-03-02 16:37:48 -0600283 spin_lock_irqsave(&vp_dev->lock, flags);
Anthony Liguori33436602007-11-12 21:30:26 -0600284 list_del(&info->node);
Anthony Liguori27ebe302008-03-02 16:37:48 -0600285 spin_unlock_irqrestore(&vp_dev->lock, flags);
Anthony Liguori33436602007-11-12 21:30:26 -0600286
287 vring_del_virtqueue(vq);
288
289 /* Select and deactivate the queue */
290 iowrite16(info->queue_index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_SEL);
291 iowrite32(0, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN);
292
Hollis Blanchard13b1eb32008-12-02 16:24:40 -0600293 size = PAGE_ALIGN(vring_size(info->num, PAGE_SIZE));
294 free_pages_exact(info->queue, size);
Anthony Liguori33436602007-11-12 21:30:26 -0600295 kfree(info);
296}
297
298static struct virtio_config_ops virtio_pci_config_ops = {
Anthony Liguori33436602007-11-12 21:30:26 -0600299 .get = vp_get,
300 .set = vp_set,
301 .get_status = vp_get_status,
302 .set_status = vp_set_status,
303 .reset = vp_reset,
304 .find_vq = vp_find_vq,
305 .del_vq = vp_del_vq,
Rusty Russellc45a6812008-05-02 21:50:50 -0500306 .get_features = vp_get_features,
Rusty Russellc6248962008-07-25 12:06:07 -0500307 .finalize_features = vp_finalize_features,
Anthony Liguori33436602007-11-12 21:30:26 -0600308};
309
310/* the PCI probing function */
311static int __devinit virtio_pci_probe(struct pci_dev *pci_dev,
312 const struct pci_device_id *id)
313{
314 struct virtio_pci_device *vp_dev;
315 int err;
316
317 /* We only own devices >= 0x1000 and <= 0x103f: leave the rest. */
318 if (pci_dev->device < 0x1000 || pci_dev->device > 0x103f)
319 return -ENODEV;
320
Anthony Liguori55a7c062008-01-28 09:59:59 -0600321 if (pci_dev->revision != VIRTIO_PCI_ABI_VERSION) {
322 printk(KERN_ERR "virtio_pci: expected ABI version %d, got %d\n",
323 VIRTIO_PCI_ABI_VERSION, pci_dev->revision);
324 return -ENODEV;
325 }
326
Anthony Liguori33436602007-11-12 21:30:26 -0600327 /* allocate our structure and fill it out */
328 vp_dev = kzalloc(sizeof(struct virtio_pci_device), GFP_KERNEL);
329 if (vp_dev == NULL)
330 return -ENOMEM;
331
Anthony Liguori33436602007-11-12 21:30:26 -0600332 vp_dev->vdev.dev.parent = &virtio_pci_root;
333 vp_dev->vdev.config = &virtio_pci_config_ops;
334 vp_dev->pci_dev = pci_dev;
335 INIT_LIST_HEAD(&vp_dev->virtqueues);
336 spin_lock_init(&vp_dev->lock);
337
338 /* enable the device */
339 err = pci_enable_device(pci_dev);
340 if (err)
341 goto out;
342
343 err = pci_request_regions(pci_dev, "virtio-pci");
344 if (err)
345 goto out_enable_device;
346
347 vp_dev->ioaddr = pci_iomap(pci_dev, 0, 0);
348 if (vp_dev->ioaddr == NULL)
349 goto out_req_regions;
350
351 pci_set_drvdata(pci_dev, vp_dev);
352
353 /* we use the subsystem vendor/device id as the virtio vendor/device
354 * id. this allows us to use the same PCI vendor/device id for all
355 * virtio devices and to identify the particular virtio driver by
356 * the subsytem ids */
357 vp_dev->vdev.id.vendor = pci_dev->subsystem_vendor;
358 vp_dev->vdev.id.device = pci_dev->subsystem_device;
359
360 /* register a handler for the queue with the PCI device's interrupt */
361 err = request_irq(vp_dev->pci_dev->irq, vp_interrupt, IRQF_SHARED,
362 vp_dev->vdev.dev.bus_id, vp_dev);
363 if (err)
364 goto out_set_drvdata;
365
366 /* finally register the virtio device */
367 err = register_virtio_device(&vp_dev->vdev);
368 if (err)
369 goto out_req_irq;
370
371 return 0;
372
373out_req_irq:
374 free_irq(pci_dev->irq, vp_dev);
375out_set_drvdata:
376 pci_set_drvdata(pci_dev, NULL);
377 pci_iounmap(pci_dev, vp_dev->ioaddr);
378out_req_regions:
379 pci_release_regions(pci_dev);
380out_enable_device:
381 pci_disable_device(pci_dev);
382out:
383 kfree(vp_dev);
384 return err;
385}
386
387static void __devexit virtio_pci_remove(struct pci_dev *pci_dev)
388{
389 struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
390
Anthony Liguoribd6c2692008-03-19 20:35:04 -0500391 unregister_virtio_device(&vp_dev->vdev);
Anthony Liguori33436602007-11-12 21:30:26 -0600392 free_irq(pci_dev->irq, vp_dev);
393 pci_set_drvdata(pci_dev, NULL);
394 pci_iounmap(pci_dev, vp_dev->ioaddr);
395 pci_release_regions(pci_dev);
396 pci_disable_device(pci_dev);
397 kfree(vp_dev);
398}
399
400#ifdef CONFIG_PM
401static int virtio_pci_suspend(struct pci_dev *pci_dev, pm_message_t state)
402{
403 pci_save_state(pci_dev);
404 pci_set_power_state(pci_dev, PCI_D3hot);
405 return 0;
406}
407
408static int virtio_pci_resume(struct pci_dev *pci_dev)
409{
410 pci_restore_state(pci_dev);
411 pci_set_power_state(pci_dev, PCI_D0);
412 return 0;
413}
414#endif
415
416static struct pci_driver virtio_pci_driver = {
417 .name = "virtio-pci",
418 .id_table = virtio_pci_id_table,
419 .probe = virtio_pci_probe,
420 .remove = virtio_pci_remove,
421#ifdef CONFIG_PM
422 .suspend = virtio_pci_suspend,
423 .resume = virtio_pci_resume,
424#endif
425};
426
427static int __init virtio_pci_init(void)
428{
429 int err;
430
431 err = device_register(&virtio_pci_root);
432 if (err)
433 return err;
434
435 err = pci_register_driver(&virtio_pci_driver);
436 if (err)
437 device_unregister(&virtio_pci_root);
438
439 return err;
440}
441
442module_init(virtio_pci_init);
443
444static void __exit virtio_pci_exit(void)
445{
446 device_unregister(&virtio_pci_root);
447 pci_unregister_driver(&virtio_pci_driver);
448}
449
450module_exit(virtio_pci_exit);