aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/virtio
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2008-02-04 11:00:54 -0500
committerLinus Torvalds <torvalds@linux-foundation.org>2008-02-04 11:00:54 -0500
commit93890b71a34f9490673a6edd56b61c2124215e46 (patch)
treec5d82620f2cb69f0bf43639e63f54b0c0e2eb744 /drivers/virtio
parentf5bb3a5e9dcdb8435471562b6cada89525cf4df1 (diff)
parent6b35e40767c6c1ac783330109ae8e0c09ea6bc82 (diff)
Merge git://git.kernel.org/pub/scm/linux/kernel/git/rusty/linux-2.6-for-linus
* git://git.kernel.org/pub/scm/linux/kernel/git/rusty/linux-2.6-for-linus: (25 commits) virtio: balloon driver virtio: Use PCI revision field to indicate virtio PCI ABI version virtio: PCI device virtio_blk: implement naming for vda-vdz,vdaa-vdzz,vdaaa-vdzzz virtio_blk: Dont waste major numbers virtio_blk: provide getgeo virtio_net: parametrize the napi_weight for virtio receive queue. virtio: free transmit skbs when notified, not on next xmit. virtio: flush buffers on open virtnet: remove double ether_setup virtio: Allow virtio to be modular and used by modules virtio: Use the sg_phys convenience function. virtio: Put the virtio under the virtualization menu virtio: handle interrupts after callbacks turned off virtio: reset function virtio: populate network rings in the probe routine, not open virtio: Tweak virtio_net defines virtio: Net header needs hdr_len virtio: remove unused id field from struct virtio_blk_outhdr virtio: clarify NO_NOTIFY flag usage ...
Diffstat (limited to 'drivers/virtio')
-rw-r--r--drivers/virtio/Kconfig31
-rw-r--r--drivers/virtio/Makefile2
-rw-r--r--drivers/virtio/virtio.c65
-rw-r--r--drivers/virtio/virtio_balloon.c284
-rw-r--r--drivers/virtio/virtio_pci.c446
-rw-r--r--drivers/virtio/virtio_ring.c51
6 files changed, 810 insertions, 69 deletions
diff --git a/drivers/virtio/Kconfig b/drivers/virtio/Kconfig
index 9e33fc4da875..3dd6294d10b6 100644
--- a/drivers/virtio/Kconfig
+++ b/drivers/virtio/Kconfig
@@ -1,8 +1,35 @@
1# Virtio always gets selected by whoever wants it. 1# Virtio always gets selected by whoever wants it.
2config VIRTIO 2config VIRTIO
3 bool 3 tristate
4 4
5# Similarly the virtio ring implementation. 5# Similarly the virtio ring implementation.
6config VIRTIO_RING 6config VIRTIO_RING
7 bool 7 tristate
8 depends on VIRTIO 8 depends on VIRTIO
9
10config VIRTIO_PCI
11 tristate "PCI driver for virtio devices (EXPERIMENTAL)"
12 depends on PCI && EXPERIMENTAL
13 select VIRTIO
14 select VIRTIO_RING
15 ---help---
16 This drivers provides support for virtio based paravirtual device
17 drivers over PCI. This requires that your VMM has appropriate PCI
18 virtio backends. Most QEMU based VMMs should support these devices
19 (like KVM or Xen).
20
21 Currently, the ABI is not considered stable so there is no guarantee
22 that this version of the driver will work with your VMM.
23
24 If unsure, say M.
25
26config VIRTIO_BALLOON
27 tristate "Virtio balloon driver (EXPERIMENTAL)"
28 select VIRTIO
29 select VIRTIO_RING
30 ---help---
31 This driver supports increasing and decreasing the amount
32 of memory within a KVM guest.
33
34 If unsure, say M.
35
diff --git a/drivers/virtio/Makefile b/drivers/virtio/Makefile
index f70e40971dd9..6738c446c199 100644
--- a/drivers/virtio/Makefile
+++ b/drivers/virtio/Makefile
@@ -1,2 +1,4 @@
1obj-$(CONFIG_VIRTIO) += virtio.o 1obj-$(CONFIG_VIRTIO) += virtio.o
2obj-$(CONFIG_VIRTIO_RING) += virtio_ring.o 2obj-$(CONFIG_VIRTIO_RING) += virtio_ring.o
3obj-$(CONFIG_VIRTIO_PCI) += virtio_pci.o
4obj-$(CONFIG_VIRTIO_BALLOON) += virtio_balloon.o
diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c
index 69d7ea02cd48..b535483bc556 100644
--- a/drivers/virtio/virtio.c
+++ b/drivers/virtio/virtio.c
@@ -102,9 +102,13 @@ static int virtio_dev_remove(struct device *_d)
102 struct virtio_driver *drv = container_of(dev->dev.driver, 102 struct virtio_driver *drv = container_of(dev->dev.driver,
103 struct virtio_driver, driver); 103 struct virtio_driver, driver);
104 104
105 dev->config->set_status(dev, dev->config->get_status(dev)
106 & ~VIRTIO_CONFIG_S_DRIVER);
107 drv->remove(dev); 105 drv->remove(dev);
106
107 /* Driver should have reset device. */
108 BUG_ON(dev->config->get_status(dev));
109
110 /* Acknowledge the device's existence again. */
111 add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
108 return 0; 112 return 0;
109} 113}
110 114
@@ -130,6 +134,10 @@ int register_virtio_device(struct virtio_device *dev)
130 dev->dev.bus = &virtio_bus; 134 dev->dev.bus = &virtio_bus;
131 sprintf(dev->dev.bus_id, "%u", dev->index); 135 sprintf(dev->dev.bus_id, "%u", dev->index);
132 136
137 /* We always start by resetting the device, in case a previous
138 * driver messed it up. This also tests that code path a little. */
139 dev->config->reset(dev);
140
133 /* Acknowledge that we've seen the device. */ 141 /* Acknowledge that we've seen the device. */
134 add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE); 142 add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
135 143
@@ -148,55 +156,18 @@ void unregister_virtio_device(struct virtio_device *dev)
148} 156}
149EXPORT_SYMBOL_GPL(unregister_virtio_device); 157EXPORT_SYMBOL_GPL(unregister_virtio_device);
150 158
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) 159static int virtio_init(void)
197{ 160{
198 if (bus_register(&virtio_bus) != 0) 161 if (bus_register(&virtio_bus) != 0)
199 panic("virtio bus registration failed"); 162 panic("virtio bus registration failed");
200 return 0; 163 return 0;
201} 164}
165
166static void __exit virtio_exit(void)
167{
168 bus_unregister(&virtio_bus);
169}
202core_initcall(virtio_init); 170core_initcall(virtio_init);
171module_exit(virtio_exit);
172
173MODULE_LICENSE("GPL");
diff --git a/drivers/virtio/virtio_balloon.c b/drivers/virtio/virtio_balloon.c
new file mode 100644
index 000000000000..622aece1acce
--- /dev/null
+++ b/drivers/virtio/virtio_balloon.c
@@ -0,0 +1,284 @@
1/* Virtio balloon implementation, inspired by Dor Loar and Marcelo
2 * Tosatti's implementations.
3 *
4 * Copyright 2008 Rusty Russell IBM Corporation
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20//#define DEBUG
21#include <linux/virtio.h>
22#include <linux/virtio_balloon.h>
23#include <linux/swap.h>
24#include <linux/kthread.h>
25#include <linux/freezer.h>
26
27struct virtio_balloon
28{
29 struct virtio_device *vdev;
30 struct virtqueue *inflate_vq, *deflate_vq;
31
32 /* Where the ballooning thread waits for config to change. */
33 wait_queue_head_t config_change;
34
35 /* The thread servicing the balloon. */
36 struct task_struct *thread;
37
38 /* Waiting for host to ack the pages we released. */
39 struct completion acked;
40
41 /* Do we have to tell Host *before* we reuse pages? */
42 bool tell_host_first;
43
44 /* The pages we've told the Host we're not using. */
45 unsigned int num_pages;
46 struct list_head pages;
47
48 /* The array of pfns we tell the Host about. */
49 unsigned int num_pfns;
50 u32 pfns[256];
51};
52
53static struct virtio_device_id id_table[] = {
54 { VIRTIO_ID_BALLOON, VIRTIO_DEV_ANY_ID },
55 { 0 },
56};
57
58static void balloon_ack(struct virtqueue *vq)
59{
60 struct virtio_balloon *vb;
61 unsigned int len;
62
63 vb = vq->vq_ops->get_buf(vq, &len);
64 if (vb)
65 complete(&vb->acked);
66}
67
68static void tell_host(struct virtio_balloon *vb, struct virtqueue *vq)
69{
70 struct scatterlist sg;
71
72 sg_init_one(&sg, vb->pfns, sizeof(vb->pfns[0]) * vb->num_pfns);
73
74 init_completion(&vb->acked);
75
76 /* We should always be able to add one buffer to an empty queue. */
77 if (vq->vq_ops->add_buf(vq, &sg, 1, 0, vb) != 0)
78 BUG();
79 vq->vq_ops->kick(vq);
80
81 /* When host has read buffer, this completes via balloon_ack */
82 wait_for_completion(&vb->acked);
83}
84
85static void fill_balloon(struct virtio_balloon *vb, size_t num)
86{
87 /* We can only do one array worth at a time. */
88 num = min(num, ARRAY_SIZE(vb->pfns));
89
90 for (vb->num_pfns = 0; vb->num_pfns < num; vb->num_pfns++) {
91 struct page *page = alloc_page(GFP_HIGHUSER | __GFP_NORETRY);
92 if (!page) {
93 if (printk_ratelimit())
94 dev_printk(KERN_INFO, &vb->vdev->dev,
95 "Out of puff! Can't get %zu pages\n",
96 num);
97 /* Sleep for at least 1/5 of a second before retry. */
98 msleep(200);
99 break;
100 }
101 vb->pfns[vb->num_pfns] = page_to_pfn(page);
102 totalram_pages--;
103 vb->num_pages++;
104 list_add(&page->lru, &vb->pages);
105 }
106
107 /* Didn't get any? Oh well. */
108 if (vb->num_pfns == 0)
109 return;
110
111 tell_host(vb, vb->inflate_vq);
112}
113
114static void release_pages_by_pfn(const u32 pfns[], unsigned int num)
115{
116 unsigned int i;
117
118 for (i = 0; i < num; i++) {
119 __free_page(pfn_to_page(pfns[i]));
120 totalram_pages++;
121 }
122}
123
124static void leak_balloon(struct virtio_balloon *vb, size_t num)
125{
126 struct page *page;
127
128 /* We can only do one array worth at a time. */
129 num = min(num, ARRAY_SIZE(vb->pfns));
130
131 for (vb->num_pfns = 0; vb->num_pfns < num; vb->num_pfns++) {
132 page = list_first_entry(&vb->pages, struct page, lru);
133 list_del(&page->lru);
134 vb->pfns[vb->num_pfns] = page_to_pfn(page);
135 vb->num_pages--;
136 }
137
138 if (vb->tell_host_first) {
139 tell_host(vb, vb->deflate_vq);
140 release_pages_by_pfn(vb->pfns, vb->num_pfns);
141 } else {
142 release_pages_by_pfn(vb->pfns, vb->num_pfns);
143 tell_host(vb, vb->deflate_vq);
144 }
145}
146
147static void virtballoon_changed(struct virtio_device *vdev)
148{
149 struct virtio_balloon *vb = vdev->priv;
150
151 wake_up(&vb->config_change);
152}
153
154static inline int towards_target(struct virtio_balloon *vb)
155{
156 u32 v;
157 __virtio_config_val(vb->vdev,
158 offsetof(struct virtio_balloon_config, num_pages),
159 &v);
160 return v - vb->num_pages;
161}
162
163static void update_balloon_size(struct virtio_balloon *vb)
164{
165 __le32 actual = cpu_to_le32(vb->num_pages);
166
167 vb->vdev->config->set(vb->vdev,
168 offsetof(struct virtio_balloon_config, actual),
169 &actual, sizeof(actual));
170}
171
172static int balloon(void *_vballoon)
173{
174 struct virtio_balloon *vb = _vballoon;
175
176 set_freezable();
177 while (!kthread_should_stop()) {
178 int diff;
179
180 try_to_freeze();
181 wait_event_interruptible(vb->config_change,
182 (diff = towards_target(vb)) != 0
183 || kthread_should_stop());
184 if (diff > 0)
185 fill_balloon(vb, diff);
186 else if (diff < 0)
187 leak_balloon(vb, -diff);
188 update_balloon_size(vb);
189 }
190 return 0;
191}
192
193static int virtballoon_probe(struct virtio_device *vdev)
194{
195 struct virtio_balloon *vb;
196 int err;
197
198 vdev->priv = vb = kmalloc(sizeof(*vb), GFP_KERNEL);
199 if (!vb) {
200 err = -ENOMEM;
201 goto out;
202 }
203
204 INIT_LIST_HEAD(&vb->pages);
205 vb->num_pages = 0;
206 init_waitqueue_head(&vb->config_change);
207 vb->vdev = vdev;
208
209 /* We expect two virtqueues. */
210 vb->inflate_vq = vdev->config->find_vq(vdev, 0, balloon_ack);
211 if (IS_ERR(vb->inflate_vq)) {
212 err = PTR_ERR(vb->inflate_vq);
213 goto out_free_vb;
214 }
215
216 vb->deflate_vq = vdev->config->find_vq(vdev, 1, balloon_ack);
217 if (IS_ERR(vb->deflate_vq)) {
218 err = PTR_ERR(vb->deflate_vq);
219 goto out_del_inflate_vq;
220 }
221
222 vb->thread = kthread_run(balloon, vb, "vballoon");
223 if (IS_ERR(vb->thread)) {
224 err = PTR_ERR(vb->thread);
225 goto out_del_deflate_vq;
226 }
227
228 vb->tell_host_first
229 = vdev->config->feature(vdev, VIRTIO_BALLOON_F_MUST_TELL_HOST);
230
231 return 0;
232
233out_del_deflate_vq:
234 vdev->config->del_vq(vb->deflate_vq);
235out_del_inflate_vq:
236 vdev->config->del_vq(vb->inflate_vq);
237out_free_vb:
238 kfree(vb);
239out:
240 return err;
241}
242
243static void virtballoon_remove(struct virtio_device *vdev)
244{
245 struct virtio_balloon *vb = vdev->priv;
246
247 kthread_stop(vb->thread);
248
249 /* There might be pages left in the balloon: free them. */
250 while (vb->num_pages)
251 leak_balloon(vb, vb->num_pages);
252
253 /* Now we reset the device so we can clean up the queues. */
254 vdev->config->reset(vdev);
255
256 vdev->config->del_vq(vb->deflate_vq);
257 vdev->config->del_vq(vb->inflate_vq);
258 kfree(vb);
259}
260
261static struct virtio_driver virtio_balloon = {
262 .driver.name = KBUILD_MODNAME,
263 .driver.owner = THIS_MODULE,
264 .id_table = id_table,
265 .probe = virtballoon_probe,
266 .remove = __devexit_p(virtballoon_remove),
267 .config_changed = virtballoon_changed,
268};
269
270static int __init init(void)
271{
272 return register_virtio_driver(&virtio_balloon);
273}
274
275static void __exit fini(void)
276{
277 unregister_virtio_driver(&virtio_balloon);
278}
279module_init(init);
280module_exit(fini);
281
282MODULE_DEVICE_TABLE(virtio, id_table);
283MODULE_DESCRIPTION("Virtio balloon driver");
284MODULE_LICENSE("GPL");
diff --git a/drivers/virtio/virtio_pci.c b/drivers/virtio/virtio_pci.c
new file mode 100644
index 000000000000..26f787ddd5ff
--- /dev/null
+++ b/drivers/virtio/virtio_pci.c
@@ -0,0 +1,446 @@
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 */
40 void *ioaddr;
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
90/* virtio config->feature() implementation */
91static bool vp_feature(struct virtio_device *vdev, unsigned bit)
92{
93 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
94 u32 mask;
95
96 /* Since this function is supposed to have the side effect of
97 * enabling a queried feature, we simulate that by doing a read
98 * from the host feature bitmask and then writing to the guest
99 * feature bitmask */
100 mask = ioread32(vp_dev->ioaddr + VIRTIO_PCI_HOST_FEATURES);
101 if (mask & (1 << bit)) {
102 mask |= (1 << bit);
103 iowrite32(mask, vp_dev->ioaddr + VIRTIO_PCI_GUEST_FEATURES);
104 }
105
106 return !!(mask & (1 << bit));
107}
108
109/* virtio config->get() implementation */
110static void vp_get(struct virtio_device *vdev, unsigned offset,
111 void *buf, unsigned len)
112{
113 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
114 void *ioaddr = vp_dev->ioaddr + VIRTIO_PCI_CONFIG + offset;
115 u8 *ptr = buf;
116 int i;
117
118 for (i = 0; i < len; i++)
119 ptr[i] = ioread8(ioaddr + i);
120}
121
122/* the config->set() implementation. it's symmetric to the config->get()
123 * implementation */
124static void vp_set(struct virtio_device *vdev, unsigned offset,
125 const void *buf, unsigned len)
126{
127 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
128 void *ioaddr = vp_dev->ioaddr + VIRTIO_PCI_CONFIG + offset;
129 const u8 *ptr = buf;
130 int i;
131
132 for (i = 0; i < len; i++)
133 iowrite8(ptr[i], ioaddr + i);
134}
135
136/* config->{get,set}_status() implementations */
137static u8 vp_get_status(struct virtio_device *vdev)
138{
139 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
140 return ioread8(vp_dev->ioaddr + VIRTIO_PCI_STATUS);
141}
142
143static void vp_set_status(struct virtio_device *vdev, u8 status)
144{
145 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
146 /* We should never be setting status to 0. */
147 BUG_ON(status == 0);
148 return iowrite8(status, vp_dev->ioaddr + VIRTIO_PCI_STATUS);
149}
150
151static void vp_reset(struct virtio_device *vdev)
152{
153 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
154 /* 0 status means a reset. */
155 return iowrite8(0, vp_dev->ioaddr + VIRTIO_PCI_STATUS);
156}
157
158/* the notify function used when creating a virt queue */
159static void vp_notify(struct virtqueue *vq)
160{
161 struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
162 struct virtio_pci_vq_info *info = vq->priv;
163
164 /* we write the queue's selector into the notification register to
165 * signal the other end */
166 iowrite16(info->queue_index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_NOTIFY);
167}
168
169/* A small wrapper to also acknowledge the interrupt when it's handled.
170 * I really need an EIO hook for the vring so I can ack the interrupt once we
171 * know that we'll be handling the IRQ but before we invoke the callback since
172 * the callback may notify the host which results in the host attempting to
173 * raise an interrupt that we would then mask once we acknowledged the
174 * interrupt. */
175static irqreturn_t vp_interrupt(int irq, void *opaque)
176{
177 struct virtio_pci_device *vp_dev = opaque;
178 struct virtio_pci_vq_info *info;
179 irqreturn_t ret = IRQ_NONE;
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
200 spin_lock(&vp_dev->lock);
201 list_for_each_entry(info, &vp_dev->virtqueues, node) {
202 if (vring_interrupt(irq, info->vq) == IRQ_HANDLED)
203 ret = IRQ_HANDLED;
204 }
205 spin_unlock(&vp_dev->lock);
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;
217 u16 num;
218 int err;
219
220 /* Select the queue we're interested in */
221 iowrite16(index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_SEL);
222
223 /* Check if queue is either not available or already active. */
224 num = ioread16(vp_dev->ioaddr + VIRTIO_PCI_QUEUE_NUM);
225 if (!num || ioread32(vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN))
226 return ERR_PTR(-ENOENT);
227
228 /* allocate and fill out our structure the represents an active
229 * queue */
230 info = kmalloc(sizeof(struct virtio_pci_vq_info), GFP_KERNEL);
231 if (!info)
232 return ERR_PTR(-ENOMEM);
233
234 info->queue_index = index;
235 info->num = num;
236
237 info->queue = kzalloc(PAGE_ALIGN(vring_size(num,PAGE_SIZE)), GFP_KERNEL);
238 if (info->queue == NULL) {
239 err = -ENOMEM;
240 goto out_info;
241 }
242
243 /* activate the queue */
244 iowrite32(virt_to_phys(info->queue) >> PAGE_SHIFT,
245 vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN);
246
247 /* create the vring */
248 vq = vring_new_virtqueue(info->num, vdev, info->queue,
249 vp_notify, callback);
250 if (!vq) {
251 err = -ENOMEM;
252 goto out_activate_queue;
253 }
254
255 vq->priv = info;
256 info->vq = vq;
257
258 spin_lock(&vp_dev->lock);
259 list_add(&info->node, &vp_dev->virtqueues);
260 spin_unlock(&vp_dev->lock);
261
262 return vq;
263
264out_activate_queue:
265 iowrite32(0, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN);
266 kfree(info->queue);
267out_info:
268 kfree(info);
269 return ERR_PTR(err);
270}
271
272/* the config->del_vq() implementation */
273static void vp_del_vq(struct virtqueue *vq)
274{
275 struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
276 struct virtio_pci_vq_info *info = vq->priv;
277
278 spin_lock(&vp_dev->lock);
279 list_del(&info->node);
280 spin_unlock(&vp_dev->lock);
281
282 vring_del_virtqueue(vq);
283
284 /* Select and deactivate the queue */
285 iowrite16(info->queue_index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_SEL);
286 iowrite32(0, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN);
287
288 kfree(info->queue);
289 kfree(info);
290}
291
292static struct virtio_config_ops virtio_pci_config_ops = {
293 .feature = vp_feature,
294 .get = vp_get,
295 .set = vp_set,
296 .get_status = vp_get_status,
297 .set_status = vp_set_status,
298 .reset = vp_reset,
299 .find_vq = vp_find_vq,
300 .del_vq = vp_del_vq,
301};
302
303/* the PCI probing function */
304static int __devinit virtio_pci_probe(struct pci_dev *pci_dev,
305 const struct pci_device_id *id)
306{
307 struct virtio_pci_device *vp_dev;
308 int err;
309
310 /* We only own devices >= 0x1000 and <= 0x103f: leave the rest. */
311 if (pci_dev->device < 0x1000 || pci_dev->device > 0x103f)
312 return -ENODEV;
313
314 if (pci_dev->revision != VIRTIO_PCI_ABI_VERSION) {
315 printk(KERN_ERR "virtio_pci: expected ABI version %d, got %d\n",
316 VIRTIO_PCI_ABI_VERSION, pci_dev->revision);
317 return -ENODEV;
318 }
319
320 /* allocate our structure and fill it out */
321 vp_dev = kzalloc(sizeof(struct virtio_pci_device), GFP_KERNEL);
322 if (vp_dev == NULL)
323 return -ENOMEM;
324
325 snprintf(vp_dev->vdev.dev.bus_id, BUS_ID_SIZE, "virtio%d", dev_index);
326 vp_dev->vdev.index = dev_index;
327 dev_index++;
328
329 vp_dev->vdev.dev.parent = &virtio_pci_root;
330 vp_dev->vdev.config = &virtio_pci_config_ops;
331 vp_dev->pci_dev = pci_dev;
332 INIT_LIST_HEAD(&vp_dev->virtqueues);
333 spin_lock_init(&vp_dev->lock);
334
335 /* enable the device */
336 err = pci_enable_device(pci_dev);
337 if (err)
338 goto out;
339
340 err = pci_request_regions(pci_dev, "virtio-pci");
341 if (err)
342 goto out_enable_device;
343
344 vp_dev->ioaddr = pci_iomap(pci_dev, 0, 0);
345 if (vp_dev->ioaddr == NULL)
346 goto out_req_regions;
347
348 pci_set_drvdata(pci_dev, vp_dev);
349
350 /* we use the subsystem vendor/device id as the virtio vendor/device
351 * id. this allows us to use the same PCI vendor/device id for all
352 * virtio devices and to identify the particular virtio driver by
353 * the subsytem ids */
354 vp_dev->vdev.id.vendor = pci_dev->subsystem_vendor;
355 vp_dev->vdev.id.device = pci_dev->subsystem_device;
356
357 /* register a handler for the queue with the PCI device's interrupt */
358 err = request_irq(vp_dev->pci_dev->irq, vp_interrupt, IRQF_SHARED,
359 vp_dev->vdev.dev.bus_id, vp_dev);
360 if (err)
361 goto out_set_drvdata;
362
363 /* finally register the virtio device */
364 err = register_virtio_device(&vp_dev->vdev);
365 if (err)
366 goto out_req_irq;
367
368 return 0;
369
370out_req_irq:
371 free_irq(pci_dev->irq, vp_dev);
372out_set_drvdata:
373 pci_set_drvdata(pci_dev, NULL);
374 pci_iounmap(pci_dev, vp_dev->ioaddr);
375out_req_regions:
376 pci_release_regions(pci_dev);
377out_enable_device:
378 pci_disable_device(pci_dev);
379out:
380 kfree(vp_dev);
381 return err;
382}
383
384static void __devexit virtio_pci_remove(struct pci_dev *pci_dev)
385{
386 struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
387
388 free_irq(pci_dev->irq, vp_dev);
389 pci_set_drvdata(pci_dev, NULL);
390 pci_iounmap(pci_dev, vp_dev->ioaddr);
391 pci_release_regions(pci_dev);
392 pci_disable_device(pci_dev);
393 kfree(vp_dev);
394}
395
396#ifdef CONFIG_PM
397static int virtio_pci_suspend(struct pci_dev *pci_dev, pm_message_t state)
398{
399 pci_save_state(pci_dev);
400 pci_set_power_state(pci_dev, PCI_D3hot);
401 return 0;
402}
403
404static int virtio_pci_resume(struct pci_dev *pci_dev)
405{
406 pci_restore_state(pci_dev);
407 pci_set_power_state(pci_dev, PCI_D0);
408 return 0;
409}
410#endif
411
412static struct pci_driver virtio_pci_driver = {
413 .name = "virtio-pci",
414 .id_table = virtio_pci_id_table,
415 .probe = virtio_pci_probe,
416 .remove = virtio_pci_remove,
417#ifdef CONFIG_PM
418 .suspend = virtio_pci_suspend,
419 .resume = virtio_pci_resume,
420#endif
421};
422
423static int __init virtio_pci_init(void)
424{
425 int err;
426
427 err = device_register(&virtio_pci_root);
428 if (err)
429 return err;
430
431 err = pci_register_driver(&virtio_pci_driver);
432 if (err)
433 device_unregister(&virtio_pci_root);
434
435 return err;
436}
437
438module_init(virtio_pci_init);
439
440static void __exit virtio_pci_exit(void)
441{
442 device_unregister(&virtio_pci_root);
443 pci_unregister_driver(&virtio_pci_driver);
444}
445
446module_exit(virtio_pci_exit);
diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c
index 1dc04b6684e6..3a28c1382131 100644
--- a/drivers/virtio/virtio_ring.c
+++ b/drivers/virtio/virtio_ring.c
@@ -87,6 +87,8 @@ static int vring_add_buf(struct virtqueue *_vq,
87 if (vq->num_free < out + in) { 87 if (vq->num_free < out + in) {
88 pr_debug("Can't add buf len %i - avail = %i\n", 88 pr_debug("Can't add buf len %i - avail = %i\n",
89 out + in, vq->num_free); 89 out + in, vq->num_free);
90 /* We notify *even if* VRING_USED_F_NO_NOTIFY is set here. */
91 vq->notify(&vq->vq);
90 END_USE(vq); 92 END_USE(vq);
91 return -ENOSPC; 93 return -ENOSPC;
92 } 94 }
@@ -97,16 +99,14 @@ static int vring_add_buf(struct virtqueue *_vq,
97 head = vq->free_head; 99 head = vq->free_head;
98 for (i = vq->free_head; out; i = vq->vring.desc[i].next, out--) { 100 for (i = vq->free_head; out; i = vq->vring.desc[i].next, out--) {
99 vq->vring.desc[i].flags = VRING_DESC_F_NEXT; 101 vq->vring.desc[i].flags = VRING_DESC_F_NEXT;
100 vq->vring.desc[i].addr = (page_to_pfn(sg_page(sg))<<PAGE_SHIFT) 102 vq->vring.desc[i].addr = sg_phys(sg);
101 + sg->offset;
102 vq->vring.desc[i].len = sg->length; 103 vq->vring.desc[i].len = sg->length;
103 prev = i; 104 prev = i;
104 sg++; 105 sg++;
105 } 106 }
106 for (; in; i = vq->vring.desc[i].next, in--) { 107 for (; in; i = vq->vring.desc[i].next, in--) {
107 vq->vring.desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE; 108 vq->vring.desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE;
108 vq->vring.desc[i].addr = (page_to_pfn(sg_page(sg))<<PAGE_SHIFT) 109 vq->vring.desc[i].addr = sg_phys(sg);
109 + sg->offset;
110 vq->vring.desc[i].len = sg->length; 110 vq->vring.desc[i].len = sg->length;
111 prev = i; 111 prev = i;
112 sg++; 112 sg++;
@@ -171,16 +171,6 @@ static void detach_buf(struct vring_virtqueue *vq, unsigned int head)
171 vq->num_free++; 171 vq->num_free++;
172} 172}
173 173
174/* FIXME: We need to tell other side about removal, to synchronize. */
175static void vring_shutdown(struct virtqueue *_vq)
176{
177 struct vring_virtqueue *vq = to_vvq(_vq);
178 unsigned int i;
179
180 for (i = 0; i < vq->vring.num; i++)
181 detach_buf(vq, i);
182}
183
184static inline bool more_used(const struct vring_virtqueue *vq) 174static inline bool more_used(const struct vring_virtqueue *vq)
185{ 175{
186 return vq->last_used_idx != vq->vring.used->idx; 176 return vq->last_used_idx != vq->vring.used->idx;
@@ -220,7 +210,17 @@ static void *vring_get_buf(struct virtqueue *_vq, unsigned int *len)
220 return ret; 210 return ret;
221} 211}
222 212
223static bool vring_restart(struct virtqueue *_vq) 213static void vring_disable_cb(struct virtqueue *_vq)
214{
215 struct vring_virtqueue *vq = to_vvq(_vq);
216
217 START_USE(vq);
218 BUG_ON(vq->vring.avail->flags & VRING_AVAIL_F_NO_INTERRUPT);
219 vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
220 END_USE(vq);
221}
222
223static bool vring_enable_cb(struct virtqueue *_vq)
224{ 224{
225 struct vring_virtqueue *vq = to_vvq(_vq); 225 struct vring_virtqueue *vq = to_vvq(_vq);
226 226
@@ -253,26 +253,34 @@ irqreturn_t vring_interrupt(int irq, void *_vq)
253 if (unlikely(vq->broken)) 253 if (unlikely(vq->broken))
254 return IRQ_HANDLED; 254 return IRQ_HANDLED;
255 255
256 /* Other side may have missed us turning off the interrupt,
257 * but we should preserve disable semantic for virtio users. */
258 if (unlikely(vq->vring.avail->flags & VRING_AVAIL_F_NO_INTERRUPT)) {
259 pr_debug("virtqueue interrupt after disable for %p\n", vq);
260 return IRQ_HANDLED;
261 }
262
256 pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback); 263 pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback);
257 if (vq->vq.callback && !vq->vq.callback(&vq->vq)) 264 if (vq->vq.callback)
258 vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT; 265 vq->vq.callback(&vq->vq);
259 266
260 return IRQ_HANDLED; 267 return IRQ_HANDLED;
261} 268}
269EXPORT_SYMBOL_GPL(vring_interrupt);
262 270
263static struct virtqueue_ops vring_vq_ops = { 271static struct virtqueue_ops vring_vq_ops = {
264 .add_buf = vring_add_buf, 272 .add_buf = vring_add_buf,
265 .get_buf = vring_get_buf, 273 .get_buf = vring_get_buf,
266 .kick = vring_kick, 274 .kick = vring_kick,
267 .restart = vring_restart, 275 .disable_cb = vring_disable_cb,
268 .shutdown = vring_shutdown, 276 .enable_cb = vring_enable_cb,
269}; 277};
270 278
271struct virtqueue *vring_new_virtqueue(unsigned int num, 279struct virtqueue *vring_new_virtqueue(unsigned int num,
272 struct virtio_device *vdev, 280 struct virtio_device *vdev,
273 void *pages, 281 void *pages,
274 void (*notify)(struct virtqueue *), 282 void (*notify)(struct virtqueue *),
275 bool (*callback)(struct virtqueue *)) 283 void (*callback)(struct virtqueue *))
276{ 284{
277 struct vring_virtqueue *vq; 285 struct vring_virtqueue *vq;
278 unsigned int i; 286 unsigned int i;
@@ -311,9 +319,12 @@ struct virtqueue *vring_new_virtqueue(unsigned int num,
311 319
312 return &vq->vq; 320 return &vq->vq;
313} 321}
322EXPORT_SYMBOL_GPL(vring_new_virtqueue);
314 323
315void vring_del_virtqueue(struct virtqueue *vq) 324void vring_del_virtqueue(struct virtqueue *vq)
316{ 325{
317 kfree(to_vvq(vq)); 326 kfree(to_vvq(vq));
318} 327}
328EXPORT_SYMBOL_GPL(vring_del_virtqueue);
319 329
330MODULE_LICENSE("GPL");