]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/virtio/virtio_pci.c
virtio: virtio_pci should not set bus_id.
[mirror_ubuntu-artful-kernel.git] / drivers / virtio / virtio_pci.c
CommitLineData
3343660d
AL
1/*
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 */
97968358 40 void __iomem *ioaddr;
3343660d
AL
41
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
81/* Unique numbering for devices under the kvm root */
82static unsigned int dev_index;
83
84/* Convert a generic virtio device to our structure */
85static struct virtio_pci_device *to_vp_device(struct virtio_device *vdev)
86{
87 return container_of(vdev, struct virtio_pci_device, vdev);
88}
89
c45a6816
RR
90/* virtio config->get_features() implementation */
91static u32 vp_get_features(struct virtio_device *vdev)
92{
93 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
94
95 /* When someone needs more than 32 feature bits, we'll need to
96 * steal a bit to indicate that the rest are somewhere else. */
97 return ioread32(vp_dev->ioaddr + VIRTIO_PCI_HOST_FEATURES);
98}
99
100/* virtio config->set_features() implementation */
101static void vp_set_features(struct virtio_device *vdev, u32 features)
3343660d
AL
102{
103 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
3343660d 104
c45a6816 105 iowrite32(features, vp_dev->ioaddr + VIRTIO_PCI_GUEST_FEATURES);
3343660d
AL
106}
107
108/* virtio config->get() implementation */
109static void vp_get(struct virtio_device *vdev, unsigned offset,
110 void *buf, unsigned len)
111{
112 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
97968358 113 void __iomem *ioaddr = vp_dev->ioaddr + VIRTIO_PCI_CONFIG + offset;
3343660d
AL
114 u8 *ptr = buf;
115 int i;
116
117 for (i = 0; i < len; i++)
118 ptr[i] = ioread8(ioaddr + i);
119}
120
121/* the config->set() implementation. it's symmetric to the config->get()
122 * implementation */
123static void vp_set(struct virtio_device *vdev, unsigned offset,
124 const void *buf, unsigned len)
125{
126 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
97968358 127 void __iomem *ioaddr = vp_dev->ioaddr + VIRTIO_PCI_CONFIG + offset;
3343660d
AL
128 const u8 *ptr = buf;
129 int i;
130
131 for (i = 0; i < len; i++)
132 iowrite8(ptr[i], ioaddr + i);
133}
134
135/* config->{get,set}_status() implementations */
136static u8 vp_get_status(struct virtio_device *vdev)
137{
138 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
139 return ioread8(vp_dev->ioaddr + VIRTIO_PCI_STATUS);
140}
141
142static void vp_set_status(struct virtio_device *vdev, u8 status)
143{
144 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
145 /* We should never be setting status to 0. */
146 BUG_ON(status == 0);
597d56e4 147 iowrite8(status, vp_dev->ioaddr + VIRTIO_PCI_STATUS);
3343660d
AL
148}
149
150static void vp_reset(struct virtio_device *vdev)
151{
152 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
153 /* 0 status means a reset. */
597d56e4 154 iowrite8(0, vp_dev->ioaddr + VIRTIO_PCI_STATUS);
3343660d
AL
155}
156
157/* the notify function used when creating a virt queue */
158static void vp_notify(struct virtqueue *vq)
159{
160 struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
161 struct virtio_pci_vq_info *info = vq->priv;
162
163 /* we write the queue's selector into the notification register to
164 * signal the other end */
165 iowrite16(info->queue_index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_NOTIFY);
166}
167
168/* A small wrapper to also acknowledge the interrupt when it's handled.
169 * I really need an EIO hook for the vring so I can ack the interrupt once we
170 * know that we'll be handling the IRQ but before we invoke the callback since
171 * the callback may notify the host which results in the host attempting to
172 * raise an interrupt that we would then mask once we acknowledged the
173 * interrupt. */
174static irqreturn_t vp_interrupt(int irq, void *opaque)
175{
176 struct virtio_pci_device *vp_dev = opaque;
177 struct virtio_pci_vq_info *info;
178 irqreturn_t ret = IRQ_NONE;
27ebe308 179 unsigned long flags;
3343660d
AL
180 u8 isr;
181
182 /* reading the ISR has the effect of also clearing it so it's very
183 * important to save off the value. */
184 isr = ioread8(vp_dev->ioaddr + VIRTIO_PCI_ISR);
185
186 /* It's definitely not us if the ISR was not high */
187 if (!isr)
188 return IRQ_NONE;
189
190 /* Configuration change? Tell driver if it wants to know. */
191 if (isr & VIRTIO_PCI_ISR_CONFIG) {
192 struct virtio_driver *drv;
193 drv = container_of(vp_dev->vdev.dev.driver,
194 struct virtio_driver, driver);
195
196 if (drv->config_changed)
197 drv->config_changed(&vp_dev->vdev);
198 }
199
27ebe308 200 spin_lock_irqsave(&vp_dev->lock, flags);
3343660d
AL
201 list_for_each_entry(info, &vp_dev->virtqueues, node) {
202 if (vring_interrupt(irq, info->vq) == IRQ_HANDLED)
203 ret = IRQ_HANDLED;
204 }
27ebe308 205 spin_unlock_irqrestore(&vp_dev->lock, flags);
3343660d
AL
206
207 return ret;
208}
209
210/* the config->find_vq() implementation */
211static struct virtqueue *vp_find_vq(struct virtio_device *vdev, unsigned index,
212 void (*callback)(struct virtqueue *vq))
213{
214 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
215 struct virtio_pci_vq_info *info;
216 struct virtqueue *vq;
27ebe308 217 unsigned long flags;
3343660d
AL
218 u16 num;
219 int err;
220
221 /* Select the queue we're interested in */
222 iowrite16(index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_SEL);
223
224 /* Check if queue is either not available or already active. */
225 num = ioread16(vp_dev->ioaddr + VIRTIO_PCI_QUEUE_NUM);
226 if (!num || ioread32(vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN))
227 return ERR_PTR(-ENOENT);
228
229 /* allocate and fill out our structure the represents an active
230 * queue */
231 info = kmalloc(sizeof(struct virtio_pci_vq_info), GFP_KERNEL);
232 if (!info)
233 return ERR_PTR(-ENOMEM);
234
235 info->queue_index = index;
236 info->num = num;
237
238 info->queue = kzalloc(PAGE_ALIGN(vring_size(num,PAGE_SIZE)), GFP_KERNEL);
239 if (info->queue == NULL) {
240 err = -ENOMEM;
241 goto out_info;
242 }
243
244 /* activate the queue */
245 iowrite32(virt_to_phys(info->queue) >> PAGE_SHIFT,
246 vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN);
247
248 /* create the vring */
249 vq = vring_new_virtqueue(info->num, vdev, info->queue,
250 vp_notify, callback);
251 if (!vq) {
252 err = -ENOMEM;
253 goto out_activate_queue;
254 }
255
256 vq->priv = info;
257 info->vq = vq;
258
27ebe308 259 spin_lock_irqsave(&vp_dev->lock, flags);
3343660d 260 list_add(&info->node, &vp_dev->virtqueues);
27ebe308 261 spin_unlock_irqrestore(&vp_dev->lock, flags);
3343660d
AL
262
263 return vq;
264
265out_activate_queue:
266 iowrite32(0, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN);
267 kfree(info->queue);
268out_info:
269 kfree(info);
270 return ERR_PTR(err);
271}
272
273/* the config->del_vq() implementation */
274static void vp_del_vq(struct virtqueue *vq)
275{
276 struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
277 struct virtio_pci_vq_info *info = vq->priv;
27ebe308 278 unsigned long flags;
3343660d 279
27ebe308 280 spin_lock_irqsave(&vp_dev->lock, flags);
3343660d 281 list_del(&info->node);
27ebe308 282 spin_unlock_irqrestore(&vp_dev->lock, flags);
3343660d
AL
283
284 vring_del_virtqueue(vq);
285
286 /* Select and deactivate the queue */
287 iowrite16(info->queue_index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_SEL);
288 iowrite32(0, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN);
289
290 kfree(info->queue);
291 kfree(info);
292}
293
294static struct virtio_config_ops virtio_pci_config_ops = {
3343660d
AL
295 .get = vp_get,
296 .set = vp_set,
297 .get_status = vp_get_status,
298 .set_status = vp_set_status,
299 .reset = vp_reset,
300 .find_vq = vp_find_vq,
301 .del_vq = vp_del_vq,
c45a6816
RR
302 .get_features = vp_get_features,
303 .set_features = vp_set_features,
3343660d
AL
304};
305
306/* the PCI probing function */
307static int __devinit virtio_pci_probe(struct pci_dev *pci_dev,
308 const struct pci_device_id *id)
309{
310 struct virtio_pci_device *vp_dev;
311 int err;
312
313 /* We only own devices >= 0x1000 and <= 0x103f: leave the rest. */
314 if (pci_dev->device < 0x1000 || pci_dev->device > 0x103f)
315 return -ENODEV;
316
55a7c066
AL
317 if (pci_dev->revision != VIRTIO_PCI_ABI_VERSION) {
318 printk(KERN_ERR "virtio_pci: expected ABI version %d, got %d\n",
319 VIRTIO_PCI_ABI_VERSION, pci_dev->revision);
320 return -ENODEV;
321 }
322
3343660d
AL
323 /* allocate our structure and fill it out */
324 vp_dev = kzalloc(sizeof(struct virtio_pci_device), GFP_KERNEL);
325 if (vp_dev == NULL)
326 return -ENOMEM;
327
3343660d
AL
328 vp_dev->vdev.index = dev_index;
329 dev_index++;
330
331 vp_dev->vdev.dev.parent = &virtio_pci_root;
332 vp_dev->vdev.config = &virtio_pci_config_ops;
333 vp_dev->pci_dev = pci_dev;
334 INIT_LIST_HEAD(&vp_dev->virtqueues);
335 spin_lock_init(&vp_dev->lock);
336
337 /* enable the device */
338 err = pci_enable_device(pci_dev);
339 if (err)
340 goto out;
341
342 err = pci_request_regions(pci_dev, "virtio-pci");
343 if (err)
344 goto out_enable_device;
345
346 vp_dev->ioaddr = pci_iomap(pci_dev, 0, 0);
347 if (vp_dev->ioaddr == NULL)
348 goto out_req_regions;
349
350 pci_set_drvdata(pci_dev, vp_dev);
351
352 /* we use the subsystem vendor/device id as the virtio vendor/device
353 * id. this allows us to use the same PCI vendor/device id for all
354 * virtio devices and to identify the particular virtio driver by
355 * the subsytem ids */
356 vp_dev->vdev.id.vendor = pci_dev->subsystem_vendor;
357 vp_dev->vdev.id.device = pci_dev->subsystem_device;
358
359 /* register a handler for the queue with the PCI device's interrupt */
360 err = request_irq(vp_dev->pci_dev->irq, vp_interrupt, IRQF_SHARED,
361 vp_dev->vdev.dev.bus_id, vp_dev);
362 if (err)
363 goto out_set_drvdata;
364
365 /* finally register the virtio device */
366 err = register_virtio_device(&vp_dev->vdev);
367 if (err)
368 goto out_req_irq;
369
370 return 0;
371
372out_req_irq:
373 free_irq(pci_dev->irq, vp_dev);
374out_set_drvdata:
375 pci_set_drvdata(pci_dev, NULL);
376 pci_iounmap(pci_dev, vp_dev->ioaddr);
377out_req_regions:
378 pci_release_regions(pci_dev);
379out_enable_device:
380 pci_disable_device(pci_dev);
381out:
382 kfree(vp_dev);
383 return err;
384}
385
386static void __devexit virtio_pci_remove(struct pci_dev *pci_dev)
387{
388 struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
389
bd6c2690 390 unregister_virtio_device(&vp_dev->vdev);
3343660d
AL
391 free_irq(pci_dev->irq, vp_dev);
392 pci_set_drvdata(pci_dev, NULL);
393 pci_iounmap(pci_dev, vp_dev->ioaddr);
394 pci_release_regions(pci_dev);
395 pci_disable_device(pci_dev);
396 kfree(vp_dev);
397}
398
399#ifdef CONFIG_PM
400static int virtio_pci_suspend(struct pci_dev *pci_dev, pm_message_t state)
401{
402 pci_save_state(pci_dev);
403 pci_set_power_state(pci_dev, PCI_D3hot);
404 return 0;
405}
406
407static int virtio_pci_resume(struct pci_dev *pci_dev)
408{
409 pci_restore_state(pci_dev);
410 pci_set_power_state(pci_dev, PCI_D0);
411 return 0;
412}
413#endif
414
415static struct pci_driver virtio_pci_driver = {
416 .name = "virtio-pci",
417 .id_table = virtio_pci_id_table,
418 .probe = virtio_pci_probe,
419 .remove = virtio_pci_remove,
420#ifdef CONFIG_PM
421 .suspend = virtio_pci_suspend,
422 .resume = virtio_pci_resume,
423#endif
424};
425
426static int __init virtio_pci_init(void)
427{
428 int err;
429
430 err = device_register(&virtio_pci_root);
431 if (err)
432 return err;
433
434 err = pci_register_driver(&virtio_pci_driver);
435 if (err)
436 device_unregister(&virtio_pci_root);
437
438 return err;
439}
440
441module_init(virtio_pci_init);
442
443static void __exit virtio_pci_exit(void)
444{
445 device_unregister(&virtio_pci_root);
446 pci_unregister_driver(&virtio_pci_driver);
447}
448
449module_exit(virtio_pci_exit);