]>
Commit | Line | Data |
---|---|---|
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> | |
5a0e3ad6 | 20 | #include <linux/slab.h> |
3343660d AL |
21 | #include <linux/interrupt.h> |
22 | #include <linux/virtio.h> | |
23 | #include <linux/virtio_config.h> | |
24 | #include <linux/virtio_ring.h> | |
25 | #include <linux/virtio_pci.h> | |
26 | #include <linux/highmem.h> | |
27 | #include <linux/spinlock.h> | |
28 | ||
29 | MODULE_AUTHOR("Anthony Liguori <aliguori@us.ibm.com>"); | |
30 | MODULE_DESCRIPTION("virtio-pci"); | |
31 | MODULE_LICENSE("GPL"); | |
32 | MODULE_VERSION("1"); | |
33 | ||
34 | /* Our device structure */ | |
35 | struct virtio_pci_device | |
36 | { | |
37 | struct virtio_device vdev; | |
38 | struct pci_dev *pci_dev; | |
39 | ||
40 | /* the IO mapping for the PCI config space */ | |
97968358 | 41 | void __iomem *ioaddr; |
3343660d AL |
42 | |
43 | /* a list of queues so we can dispatch IRQs */ | |
44 | spinlock_t lock; | |
45 | struct list_head virtqueues; | |
82af8ce8 MT |
46 | |
47 | /* MSI-X support */ | |
48 | int msix_enabled; | |
49 | int intx_enabled; | |
50 | struct msix_entry *msix_entries; | |
75a0a52b | 51 | cpumask_var_t *msix_affinity_masks; |
82af8ce8 MT |
52 | /* Name strings for interrupts. This size should be enough, |
53 | * and I'm too lazy to allocate each name separately. */ | |
54 | char (*msix_names)[256]; | |
55 | /* Number of available vectors */ | |
56 | unsigned msix_vectors; | |
e969fed5 | 57 | /* Vectors allocated, excluding per-vq vectors if any */ |
82af8ce8 | 58 | unsigned msix_used_vectors; |
f0fe6f11 | 59 | |
e969fed5 MT |
60 | /* Whether we have vector per vq */ |
61 | bool per_vq_vectors; | |
82af8ce8 MT |
62 | }; |
63 | ||
64 | /* Constants for MSI-X */ | |
65 | /* Use first vector for configuration changes, second and the rest for | |
66 | * virtqueues Thus, we need at least 2 vectors for MSI. */ | |
67 | enum { | |
68 | VP_MSIX_CONFIG_VECTOR = 0, | |
69 | VP_MSIX_VQ_VECTOR = 1, | |
3343660d AL |
70 | }; |
71 | ||
72 | struct virtio_pci_vq_info | |
73 | { | |
74 | /* the actual virtqueue */ | |
75 | struct virtqueue *vq; | |
76 | ||
77 | /* the number of entries in the queue */ | |
78 | int num; | |
79 | ||
3343660d AL |
80 | /* the virtual address of the ring queue */ |
81 | void *queue; | |
82 | ||
83 | /* the list node for the virtqueues list */ | |
84 | struct list_head node; | |
82af8ce8 MT |
85 | |
86 | /* MSI-X vector (or none) */ | |
f68d2408 | 87 | unsigned msix_vector; |
3343660d AL |
88 | }; |
89 | ||
90 | /* Qumranet donated their vendor ID for devices 0x1000 thru 0x10FF. */ | |
cef340e6 | 91 | static const struct pci_device_id virtio_pci_id_table[] = { |
35cdc9eb SH |
92 | { PCI_DEVICE(0x1af4, PCI_ANY_ID) }, |
93 | { 0 } | |
3343660d AL |
94 | }; |
95 | ||
96 | MODULE_DEVICE_TABLE(pci, virtio_pci_id_table); | |
97 | ||
3343660d AL |
98 | /* Convert a generic virtio device to our structure */ |
99 | static struct virtio_pci_device *to_vp_device(struct virtio_device *vdev) | |
100 | { | |
101 | return container_of(vdev, struct virtio_pci_device, vdev); | |
102 | } | |
103 | ||
c45a6816 | 104 | /* virtio config->get_features() implementation */ |
d0254773 | 105 | static u64 vp_get_features(struct virtio_device *vdev) |
c45a6816 RR |
106 | { |
107 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); | |
108 | ||
109 | /* When someone needs more than 32 feature bits, we'll need to | |
110 | * steal a bit to indicate that the rest are somewhere else. */ | |
111 | return ioread32(vp_dev->ioaddr + VIRTIO_PCI_HOST_FEATURES); | |
112 | } | |
113 | ||
c624896e | 114 | /* virtio config->finalize_features() implementation */ |
5c609a5e | 115 | static int vp_finalize_features(struct virtio_device *vdev) |
3343660d AL |
116 | { |
117 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); | |
3343660d | 118 | |
e34f8725 RR |
119 | /* Give virtio_ring a chance to accept features. */ |
120 | vring_transport_features(vdev); | |
121 | ||
93d389f8 MT |
122 | /* Make sure we don't have any features > 32 bits! */ |
123 | BUG_ON((u32)vdev->features != vdev->features); | |
124 | ||
c624896e | 125 | /* We only support 32 feature bits. */ |
e16e12be | 126 | iowrite32(vdev->features, vp_dev->ioaddr + VIRTIO_PCI_GUEST_FEATURES); |
5c609a5e MT |
127 | |
128 | return 0; | |
3343660d AL |
129 | } |
130 | ||
131 | /* virtio config->get() implementation */ | |
132 | static void vp_get(struct virtio_device *vdev, unsigned offset, | |
133 | void *buf, unsigned len) | |
134 | { | |
135 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); | |
82af8ce8 MT |
136 | void __iomem *ioaddr = vp_dev->ioaddr + |
137 | VIRTIO_PCI_CONFIG(vp_dev) + offset; | |
3343660d AL |
138 | u8 *ptr = buf; |
139 | int i; | |
140 | ||
141 | for (i = 0; i < len; i++) | |
142 | ptr[i] = ioread8(ioaddr + i); | |
143 | } | |
144 | ||
145 | /* the config->set() implementation. it's symmetric to the config->get() | |
146 | * implementation */ | |
147 | static void vp_set(struct virtio_device *vdev, unsigned offset, | |
148 | const void *buf, unsigned len) | |
149 | { | |
150 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); | |
82af8ce8 MT |
151 | void __iomem *ioaddr = vp_dev->ioaddr + |
152 | VIRTIO_PCI_CONFIG(vp_dev) + offset; | |
3343660d AL |
153 | const u8 *ptr = buf; |
154 | int i; | |
155 | ||
156 | for (i = 0; i < len; i++) | |
157 | iowrite8(ptr[i], ioaddr + i); | |
158 | } | |
159 | ||
160 | /* config->{get,set}_status() implementations */ | |
161 | static u8 vp_get_status(struct virtio_device *vdev) | |
162 | { | |
163 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); | |
164 | return ioread8(vp_dev->ioaddr + VIRTIO_PCI_STATUS); | |
165 | } | |
166 | ||
167 | static void vp_set_status(struct virtio_device *vdev, u8 status) | |
168 | { | |
169 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); | |
170 | /* We should never be setting status to 0. */ | |
171 | BUG_ON(status == 0); | |
597d56e4 | 172 | iowrite8(status, vp_dev->ioaddr + VIRTIO_PCI_STATUS); |
3343660d AL |
173 | } |
174 | ||
e6af578c MT |
175 | /* wait for pending irq handlers */ |
176 | static void vp_synchronize_vectors(struct virtio_device *vdev) | |
177 | { | |
178 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); | |
179 | int i; | |
180 | ||
181 | if (vp_dev->intx_enabled) | |
182 | synchronize_irq(vp_dev->pci_dev->irq); | |
183 | ||
184 | for (i = 0; i < vp_dev->msix_vectors; ++i) | |
185 | synchronize_irq(vp_dev->msix_entries[i].vector); | |
186 | } | |
187 | ||
3343660d AL |
188 | static void vp_reset(struct virtio_device *vdev) |
189 | { | |
190 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); | |
191 | /* 0 status means a reset. */ | |
597d56e4 | 192 | iowrite8(0, vp_dev->ioaddr + VIRTIO_PCI_STATUS); |
e6af578c MT |
193 | /* Flush out the status write, and flush in device writes, |
194 | * including MSi-X interrupts, if any. */ | |
195 | ioread8(vp_dev->ioaddr + VIRTIO_PCI_STATUS); | |
196 | /* Flush pending VQ/configuration callbacks. */ | |
197 | vp_synchronize_vectors(vdev); | |
3343660d AL |
198 | } |
199 | ||
200 | /* the notify function used when creating a virt queue */ | |
46f9c2b9 | 201 | static bool vp_notify(struct virtqueue *vq) |
3343660d AL |
202 | { |
203 | struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev); | |
3343660d AL |
204 | |
205 | /* we write the queue's selector into the notification register to | |
206 | * signal the other end */ | |
06ca287d | 207 | iowrite16(vq->index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_NOTIFY); |
46f9c2b9 | 208 | return true; |
3343660d AL |
209 | } |
210 | ||
77cf5246 MT |
211 | /* Handle a configuration change: Tell driver if it wants to know. */ |
212 | static irqreturn_t vp_config_changed(int irq, void *opaque) | |
213 | { | |
214 | struct virtio_pci_device *vp_dev = opaque; | |
77cf5246 | 215 | |
016c98c6 | 216 | virtio_config_changed(&vp_dev->vdev); |
77cf5246 MT |
217 | return IRQ_HANDLED; |
218 | } | |
219 | ||
220 | /* Notify all virtqueues on an interrupt. */ | |
221 | static irqreturn_t vp_vring_interrupt(int irq, void *opaque) | |
222 | { | |
223 | struct virtio_pci_device *vp_dev = opaque; | |
224 | struct virtio_pci_vq_info *info; | |
225 | irqreturn_t ret = IRQ_NONE; | |
226 | unsigned long flags; | |
227 | ||
228 | spin_lock_irqsave(&vp_dev->lock, flags); | |
229 | list_for_each_entry(info, &vp_dev->virtqueues, node) { | |
230 | if (vring_interrupt(irq, info->vq) == IRQ_HANDLED) | |
231 | ret = IRQ_HANDLED; | |
232 | } | |
233 | spin_unlock_irqrestore(&vp_dev->lock, flags); | |
234 | ||
235 | return ret; | |
236 | } | |
237 | ||
3343660d AL |
238 | /* A small wrapper to also acknowledge the interrupt when it's handled. |
239 | * I really need an EIO hook for the vring so I can ack the interrupt once we | |
240 | * know that we'll be handling the IRQ but before we invoke the callback since | |
241 | * the callback may notify the host which results in the host attempting to | |
242 | * raise an interrupt that we would then mask once we acknowledged the | |
243 | * interrupt. */ | |
244 | static irqreturn_t vp_interrupt(int irq, void *opaque) | |
245 | { | |
246 | struct virtio_pci_device *vp_dev = opaque; | |
3343660d AL |
247 | u8 isr; |
248 | ||
249 | /* reading the ISR has the effect of also clearing it so it's very | |
250 | * important to save off the value. */ | |
251 | isr = ioread8(vp_dev->ioaddr + VIRTIO_PCI_ISR); | |
252 | ||
253 | /* It's definitely not us if the ISR was not high */ | |
254 | if (!isr) | |
255 | return IRQ_NONE; | |
256 | ||
257 | /* Configuration change? Tell driver if it wants to know. */ | |
77cf5246 MT |
258 | if (isr & VIRTIO_PCI_ISR_CONFIG) |
259 | vp_config_changed(irq, opaque); | |
3343660d | 260 | |
77cf5246 | 261 | return vp_vring_interrupt(irq, opaque); |
3343660d AL |
262 | } |
263 | ||
82af8ce8 MT |
264 | static void vp_free_vectors(struct virtio_device *vdev) |
265 | { | |
266 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); | |
267 | int i; | |
268 | ||
269 | if (vp_dev->intx_enabled) { | |
270 | free_irq(vp_dev->pci_dev->irq, vp_dev); | |
271 | vp_dev->intx_enabled = 0; | |
272 | } | |
273 | ||
274 | for (i = 0; i < vp_dev->msix_used_vectors; ++i) | |
275 | free_irq(vp_dev->msix_entries[i].vector, vp_dev); | |
82af8ce8 | 276 | |
75a0a52b JW |
277 | for (i = 0; i < vp_dev->msix_vectors; i++) |
278 | if (vp_dev->msix_affinity_masks[i]) | |
279 | free_cpumask_var(vp_dev->msix_affinity_masks[i]); | |
280 | ||
82af8ce8 MT |
281 | if (vp_dev->msix_enabled) { |
282 | /* Disable the vector used for configuration */ | |
283 | iowrite16(VIRTIO_MSI_NO_VECTOR, | |
284 | vp_dev->ioaddr + VIRTIO_MSI_CONFIG_VECTOR); | |
285 | /* Flush the write out to device */ | |
286 | ioread16(vp_dev->ioaddr + VIRTIO_MSI_CONFIG_VECTOR); | |
287 | ||
82af8ce8 | 288 | pci_disable_msix(vp_dev->pci_dev); |
ff52c3fc | 289 | vp_dev->msix_enabled = 0; |
82af8ce8 | 290 | } |
ff52c3fc | 291 | |
f11335db | 292 | vp_dev->msix_vectors = 0; |
ff52c3fc MT |
293 | vp_dev->msix_used_vectors = 0; |
294 | kfree(vp_dev->msix_names); | |
295 | vp_dev->msix_names = NULL; | |
296 | kfree(vp_dev->msix_entries); | |
297 | vp_dev->msix_entries = NULL; | |
75a0a52b JW |
298 | kfree(vp_dev->msix_affinity_masks); |
299 | vp_dev->msix_affinity_masks = NULL; | |
82af8ce8 MT |
300 | } |
301 | ||
f68d2408 RR |
302 | static int vp_request_msix_vectors(struct virtio_device *vdev, int nvectors, |
303 | bool per_vq_vectors) | |
82af8ce8 MT |
304 | { |
305 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); | |
306 | const char *name = dev_name(&vp_dev->vdev.dev); | |
307 | unsigned i, v; | |
308 | int err = -ENOMEM; | |
e969fed5 | 309 | |
f11335db AV |
310 | vp_dev->msix_vectors = nvectors; |
311 | ||
82af8ce8 MT |
312 | vp_dev->msix_entries = kmalloc(nvectors * sizeof *vp_dev->msix_entries, |
313 | GFP_KERNEL); | |
314 | if (!vp_dev->msix_entries) | |
ff52c3fc | 315 | goto error; |
82af8ce8 MT |
316 | vp_dev->msix_names = kmalloc(nvectors * sizeof *vp_dev->msix_names, |
317 | GFP_KERNEL); | |
318 | if (!vp_dev->msix_names) | |
ff52c3fc | 319 | goto error; |
75a0a52b JW |
320 | vp_dev->msix_affinity_masks |
321 | = kzalloc(nvectors * sizeof *vp_dev->msix_affinity_masks, | |
322 | GFP_KERNEL); | |
323 | if (!vp_dev->msix_affinity_masks) | |
324 | goto error; | |
325 | for (i = 0; i < nvectors; ++i) | |
326 | if (!alloc_cpumask_var(&vp_dev->msix_affinity_masks[i], | |
327 | GFP_KERNEL)) | |
328 | goto error; | |
82af8ce8 MT |
329 | |
330 | for (i = 0; i < nvectors; ++i) | |
331 | vp_dev->msix_entries[i].entry = i; | |
332 | ||
5e37f670 AG |
333 | err = pci_enable_msix_exact(vp_dev->pci_dev, |
334 | vp_dev->msix_entries, nvectors); | |
e969fed5 MT |
335 | if (err) |
336 | goto error; | |
e969fed5 MT |
337 | vp_dev->msix_enabled = 1; |
338 | ||
339 | /* Set the vector used for configuration */ | |
340 | v = vp_dev->msix_used_vectors; | |
341 | snprintf(vp_dev->msix_names[v], sizeof *vp_dev->msix_names, | |
342 | "%s-config", name); | |
343 | err = request_irq(vp_dev->msix_entries[v].vector, | |
344 | vp_config_changed, 0, vp_dev->msix_names[v], | |
345 | vp_dev); | |
346 | if (err) | |
347 | goto error; | |
348 | ++vp_dev->msix_used_vectors; | |
82af8ce8 | 349 | |
e969fed5 MT |
350 | iowrite16(v, vp_dev->ioaddr + VIRTIO_MSI_CONFIG_VECTOR); |
351 | /* Verify we had enough resources to assign the vector */ | |
352 | v = ioread16(vp_dev->ioaddr + VIRTIO_MSI_CONFIG_VECTOR); | |
353 | if (v == VIRTIO_MSI_NO_VECTOR) { | |
354 | err = -EBUSY; | |
355 | goto error; | |
82af8ce8 MT |
356 | } |
357 | ||
e969fed5 | 358 | if (!per_vq_vectors) { |
82af8ce8 MT |
359 | /* Shared vector for all VQs */ |
360 | v = vp_dev->msix_used_vectors; | |
361 | snprintf(vp_dev->msix_names[v], sizeof *vp_dev->msix_names, | |
362 | "%s-virtqueues", name); | |
363 | err = request_irq(vp_dev->msix_entries[v].vector, | |
364 | vp_vring_interrupt, 0, vp_dev->msix_names[v], | |
365 | vp_dev); | |
366 | if (err) | |
ff52c3fc | 367 | goto error; |
82af8ce8 MT |
368 | ++vp_dev->msix_used_vectors; |
369 | } | |
370 | return 0; | |
ff52c3fc | 371 | error: |
82af8ce8 | 372 | vp_free_vectors(vdev); |
82af8ce8 MT |
373 | return err; |
374 | } | |
375 | ||
f68d2408 RR |
376 | static int vp_request_intx(struct virtio_device *vdev) |
377 | { | |
378 | int err; | |
379 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); | |
380 | ||
381 | err = request_irq(vp_dev->pci_dev->irq, vp_interrupt, | |
382 | IRQF_SHARED, dev_name(&vdev->dev), vp_dev); | |
383 | if (!err) | |
384 | vp_dev->intx_enabled = 1; | |
385 | return err; | |
386 | } | |
387 | ||
388 | static struct virtqueue *setup_vq(struct virtio_device *vdev, unsigned index, | |
389 | void (*callback)(struct virtqueue *vq), | |
390 | const char *name, | |
391 | u16 msix_vec) | |
3343660d AL |
392 | { |
393 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); | |
394 | struct virtio_pci_vq_info *info; | |
395 | struct virtqueue *vq; | |
13b1eb33 | 396 | unsigned long flags, size; |
e969fed5 | 397 | u16 num; |
3343660d AL |
398 | int err; |
399 | ||
400 | /* Select the queue we're interested in */ | |
401 | iowrite16(index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_SEL); | |
402 | ||
403 | /* Check if queue is either not available or already active. */ | |
404 | num = ioread16(vp_dev->ioaddr + VIRTIO_PCI_QUEUE_NUM); | |
405 | if (!num || ioread32(vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN)) | |
406 | return ERR_PTR(-ENOENT); | |
407 | ||
408 | /* allocate and fill out our structure the represents an active | |
409 | * queue */ | |
410 | info = kmalloc(sizeof(struct virtio_pci_vq_info), GFP_KERNEL); | |
411 | if (!info) | |
412 | return ERR_PTR(-ENOMEM); | |
413 | ||
3343660d | 414 | info->num = num; |
f68d2408 | 415 | info->msix_vector = msix_vec; |
3343660d | 416 | |
498af147 | 417 | size = PAGE_ALIGN(vring_size(num, VIRTIO_PCI_VRING_ALIGN)); |
13b1eb33 | 418 | info->queue = alloc_pages_exact(size, GFP_KERNEL|__GFP_ZERO); |
3343660d AL |
419 | if (info->queue == NULL) { |
420 | err = -ENOMEM; | |
421 | goto out_info; | |
422 | } | |
423 | ||
424 | /* activate the queue */ | |
480daab4 | 425 | iowrite32(virt_to_phys(info->queue) >> VIRTIO_PCI_QUEUE_ADDR_SHIFT, |
3343660d AL |
426 | vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN); |
427 | ||
428 | /* create the vring */ | |
17bb6d40 | 429 | vq = vring_new_virtqueue(index, info->num, VIRTIO_PCI_VRING_ALIGN, vdev, |
7b21e34f | 430 | true, info->queue, vp_notify, callback, name); |
3343660d AL |
431 | if (!vq) { |
432 | err = -ENOMEM; | |
433 | goto out_activate_queue; | |
434 | } | |
435 | ||
436 | vq->priv = info; | |
437 | info->vq = vq; | |
438 | ||
f68d2408 RR |
439 | if (msix_vec != VIRTIO_MSI_NO_VECTOR) { |
440 | iowrite16(msix_vec, vp_dev->ioaddr + VIRTIO_MSI_QUEUE_VECTOR); | |
441 | msix_vec = ioread16(vp_dev->ioaddr + VIRTIO_MSI_QUEUE_VECTOR); | |
442 | if (msix_vec == VIRTIO_MSI_NO_VECTOR) { | |
82af8ce8 MT |
443 | err = -EBUSY; |
444 | goto out_assign; | |
445 | } | |
446 | } | |
447 | ||
005b20a8 KK |
448 | if (callback) { |
449 | spin_lock_irqsave(&vp_dev->lock, flags); | |
450 | list_add(&info->node, &vp_dev->virtqueues); | |
451 | spin_unlock_irqrestore(&vp_dev->lock, flags); | |
452 | } else { | |
453 | INIT_LIST_HEAD(&info->node); | |
454 | } | |
3343660d AL |
455 | |
456 | return vq; | |
457 | ||
82af8ce8 | 458 | out_assign: |
82af8ce8 | 459 | vring_del_virtqueue(vq); |
3343660d AL |
460 | out_activate_queue: |
461 | iowrite32(0, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN); | |
13b1eb33 | 462 | free_pages_exact(info->queue, size); |
3343660d AL |
463 | out_info: |
464 | kfree(info); | |
465 | return ERR_PTR(err); | |
466 | } | |
467 | ||
3343660d AL |
468 | static void vp_del_vq(struct virtqueue *vq) |
469 | { | |
470 | struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev); | |
471 | struct virtio_pci_vq_info *info = vq->priv; | |
f6c82507 MT |
472 | unsigned long flags, size; |
473 | ||
474 | spin_lock_irqsave(&vp_dev->lock, flags); | |
475 | list_del(&info->node); | |
476 | spin_unlock_irqrestore(&vp_dev->lock, flags); | |
3343660d | 477 | |
06ca287d | 478 | iowrite16(vq->index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_SEL); |
82af8ce8 | 479 | |
82af8ce8 MT |
480 | if (vp_dev->msix_enabled) { |
481 | iowrite16(VIRTIO_MSI_NO_VECTOR, | |
482 | vp_dev->ioaddr + VIRTIO_MSI_QUEUE_VECTOR); | |
483 | /* Flush the write out to device */ | |
484 | ioread8(vp_dev->ioaddr + VIRTIO_PCI_ISR); | |
485 | } | |
486 | ||
3343660d AL |
487 | vring_del_virtqueue(vq); |
488 | ||
489 | /* Select and deactivate the queue */ | |
3343660d AL |
490 | iowrite32(0, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN); |
491 | ||
498af147 | 492 | size = PAGE_ALIGN(vring_size(info->num, VIRTIO_PCI_VRING_ALIGN)); |
13b1eb33 | 493 | free_pages_exact(info->queue, size); |
3343660d AL |
494 | kfree(info); |
495 | } | |
496 | ||
82af8ce8 | 497 | /* the config->del_vqs() implementation */ |
d2a7ddda MT |
498 | static void vp_del_vqs(struct virtio_device *vdev) |
499 | { | |
e969fed5 | 500 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
d2a7ddda | 501 | struct virtqueue *vq, *n; |
e969fed5 | 502 | struct virtio_pci_vq_info *info; |
d2a7ddda | 503 | |
e969fed5 MT |
504 | list_for_each_entry_safe(vq, n, &vdev->vqs, list) { |
505 | info = vq->priv; | |
31198159 MT |
506 | if (vp_dev->per_vq_vectors && |
507 | info->msix_vector != VIRTIO_MSI_NO_VECTOR) | |
f68d2408 RR |
508 | free_irq(vp_dev->msix_entries[info->msix_vector].vector, |
509 | vq); | |
d2a7ddda | 510 | vp_del_vq(vq); |
e969fed5 MT |
511 | } |
512 | vp_dev->per_vq_vectors = false; | |
82af8ce8 MT |
513 | |
514 | vp_free_vectors(vdev); | |
d2a7ddda MT |
515 | } |
516 | ||
e969fed5 MT |
517 | static int vp_try_to_find_vqs(struct virtio_device *vdev, unsigned nvqs, |
518 | struct virtqueue *vqs[], | |
519 | vq_callback_t *callbacks[], | |
520 | const char *names[], | |
f68d2408 | 521 | bool use_msix, |
e969fed5 | 522 | bool per_vq_vectors) |
d2a7ddda | 523 | { |
e969fed5 | 524 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
f68d2408 RR |
525 | u16 msix_vec; |
526 | int i, err, nvectors, allocated_vectors; | |
82af8ce8 | 527 | |
f68d2408 RR |
528 | if (!use_msix) { |
529 | /* Old style: one normal interrupt for change and all vqs. */ | |
530 | err = vp_request_intx(vdev); | |
531 | if (err) | |
532 | goto error_request; | |
533 | } else { | |
534 | if (per_vq_vectors) { | |
535 | /* Best option: one for change interrupt, one per vq. */ | |
536 | nvectors = 1; | |
537 | for (i = 0; i < nvqs; ++i) | |
538 | if (callbacks[i]) | |
539 | ++nvectors; | |
540 | } else { | |
541 | /* Second best: one for change, shared for all vqs. */ | |
542 | nvectors = 2; | |
543 | } | |
544 | ||
545 | err = vp_request_msix_vectors(vdev, nvectors, per_vq_vectors); | |
546 | if (err) | |
547 | goto error_request; | |
548 | } | |
d2a7ddda | 549 | |
e969fed5 MT |
550 | vp_dev->per_vq_vectors = per_vq_vectors; |
551 | allocated_vectors = vp_dev->msix_used_vectors; | |
d2a7ddda | 552 | for (i = 0; i < nvqs; ++i) { |
6457f126 MT |
553 | if (!names[i]) { |
554 | vqs[i] = NULL; | |
555 | continue; | |
556 | } else if (!callbacks[i] || !vp_dev->msix_enabled) | |
f68d2408 | 557 | msix_vec = VIRTIO_MSI_NO_VECTOR; |
e969fed5 | 558 | else if (vp_dev->per_vq_vectors) |
f68d2408 | 559 | msix_vec = allocated_vectors++; |
e969fed5 | 560 | else |
f68d2408 RR |
561 | msix_vec = VP_MSIX_VQ_VECTOR; |
562 | vqs[i] = setup_vq(vdev, i, callbacks[i], names[i], msix_vec); | |
e969fed5 MT |
563 | if (IS_ERR(vqs[i])) { |
564 | err = PTR_ERR(vqs[i]); | |
82af8ce8 | 565 | goto error_find; |
e969fed5 | 566 | } |
0b22bd0b MT |
567 | |
568 | if (!vp_dev->per_vq_vectors || msix_vec == VIRTIO_MSI_NO_VECTOR) | |
569 | continue; | |
570 | ||
e969fed5 | 571 | /* allocate per-vq irq if available and necessary */ |
0b22bd0b MT |
572 | snprintf(vp_dev->msix_names[msix_vec], |
573 | sizeof *vp_dev->msix_names, | |
574 | "%s-%s", | |
575 | dev_name(&vp_dev->vdev.dev), names[i]); | |
576 | err = request_irq(vp_dev->msix_entries[msix_vec].vector, | |
577 | vring_interrupt, 0, | |
578 | vp_dev->msix_names[msix_vec], | |
579 | vqs[i]); | |
580 | if (err) { | |
581 | vp_del_vq(vqs[i]); | |
582 | goto error_find; | |
e969fed5 | 583 | } |
d2a7ddda MT |
584 | } |
585 | return 0; | |
586 | ||
82af8ce8 | 587 | error_find: |
d2a7ddda | 588 | vp_del_vqs(vdev); |
82af8ce8 MT |
589 | |
590 | error_request: | |
e969fed5 MT |
591 | return err; |
592 | } | |
593 | ||
594 | /* the config->find_vqs() implementation */ | |
595 | static int vp_find_vqs(struct virtio_device *vdev, unsigned nvqs, | |
596 | struct virtqueue *vqs[], | |
597 | vq_callback_t *callbacks[], | |
598 | const char *names[]) | |
599 | { | |
f68d2408 | 600 | int err; |
e969fed5 | 601 | |
f68d2408 RR |
602 | /* Try MSI-X with one vector per queue. */ |
603 | err = vp_try_to_find_vqs(vdev, nvqs, vqs, callbacks, names, true, true); | |
e969fed5 MT |
604 | if (!err) |
605 | return 0; | |
f68d2408 | 606 | /* Fallback: MSI-X with one vector for config, one shared for queues. */ |
e969fed5 | 607 | err = vp_try_to_find_vqs(vdev, nvqs, vqs, callbacks, names, |
f68d2408 | 608 | true, false); |
e969fed5 MT |
609 | if (!err) |
610 | return 0; | |
611 | /* Finally fall back to regular interrupts. */ | |
f68d2408 RR |
612 | return vp_try_to_find_vqs(vdev, nvqs, vqs, callbacks, names, |
613 | false, false); | |
d2a7ddda MT |
614 | } |
615 | ||
66846048 RJ |
616 | static const char *vp_bus_name(struct virtio_device *vdev) |
617 | { | |
618 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); | |
619 | ||
620 | return pci_name(vp_dev->pci_dev); | |
621 | } | |
622 | ||
75a0a52b JW |
623 | /* Setup the affinity for a virtqueue: |
624 | * - force the affinity for per vq vector | |
625 | * - OR over all affinities for shared MSI | |
626 | * - ignore the affinity request if we're using INTX | |
627 | */ | |
628 | static int vp_set_vq_affinity(struct virtqueue *vq, int cpu) | |
629 | { | |
630 | struct virtio_device *vdev = vq->vdev; | |
631 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); | |
632 | struct virtio_pci_vq_info *info = vq->priv; | |
633 | struct cpumask *mask; | |
634 | unsigned int irq; | |
635 | ||
636 | if (!vq->callback) | |
637 | return -EINVAL; | |
638 | ||
639 | if (vp_dev->msix_enabled) { | |
640 | mask = vp_dev->msix_affinity_masks[info->msix_vector]; | |
641 | irq = vp_dev->msix_entries[info->msix_vector].vector; | |
642 | if (cpu == -1) | |
643 | irq_set_affinity_hint(irq, NULL); | |
644 | else { | |
645 | cpumask_set_cpu(cpu, mask); | |
646 | irq_set_affinity_hint(irq, mask); | |
647 | } | |
648 | } | |
649 | return 0; | |
650 | } | |
651 | ||
93503932 | 652 | static const struct virtio_config_ops virtio_pci_config_ops = { |
3343660d AL |
653 | .get = vp_get, |
654 | .set = vp_set, | |
655 | .get_status = vp_get_status, | |
656 | .set_status = vp_set_status, | |
657 | .reset = vp_reset, | |
d2a7ddda MT |
658 | .find_vqs = vp_find_vqs, |
659 | .del_vqs = vp_del_vqs, | |
c45a6816 | 660 | .get_features = vp_get_features, |
c624896e | 661 | .finalize_features = vp_finalize_features, |
66846048 | 662 | .bus_name = vp_bus_name, |
75a0a52b | 663 | .set_vq_affinity = vp_set_vq_affinity, |
3343660d AL |
664 | }; |
665 | ||
29f9f12e MM |
666 | static void virtio_pci_release_dev(struct device *_d) |
667 | { | |
72103bd1 MT |
668 | /* |
669 | * No need for a release method as we allocate/free | |
670 | * all devices together with the pci devices. | |
671 | * Provide an empty one to avoid getting a warning from core. | |
672 | */ | |
29f9f12e MM |
673 | } |
674 | ||
3343660d | 675 | /* the PCI probing function */ |
8590dbc7 GKH |
676 | static int virtio_pci_probe(struct pci_dev *pci_dev, |
677 | const struct pci_device_id *id) | |
3343660d AL |
678 | { |
679 | struct virtio_pci_device *vp_dev; | |
680 | int err; | |
681 | ||
682 | /* We only own devices >= 0x1000 and <= 0x103f: leave the rest. */ | |
683 | if (pci_dev->device < 0x1000 || pci_dev->device > 0x103f) | |
684 | return -ENODEV; | |
685 | ||
55a7c066 AL |
686 | if (pci_dev->revision != VIRTIO_PCI_ABI_VERSION) { |
687 | printk(KERN_ERR "virtio_pci: expected ABI version %d, got %d\n", | |
688 | VIRTIO_PCI_ABI_VERSION, pci_dev->revision); | |
689 | return -ENODEV; | |
690 | } | |
691 | ||
3343660d AL |
692 | /* allocate our structure and fill it out */ |
693 | vp_dev = kzalloc(sizeof(struct virtio_pci_device), GFP_KERNEL); | |
694 | if (vp_dev == NULL) | |
695 | return -ENOMEM; | |
696 | ||
8b3bb3ec | 697 | vp_dev->vdev.dev.parent = &pci_dev->dev; |
29f9f12e | 698 | vp_dev->vdev.dev.release = virtio_pci_release_dev; |
3343660d AL |
699 | vp_dev->vdev.config = &virtio_pci_config_ops; |
700 | vp_dev->pci_dev = pci_dev; | |
701 | INIT_LIST_HEAD(&vp_dev->virtqueues); | |
702 | spin_lock_init(&vp_dev->lock); | |
703 | ||
b03214d5 MT |
704 | /* Disable MSI/MSIX to bring device to a known good state. */ |
705 | pci_msi_off(pci_dev); | |
706 | ||
3343660d AL |
707 | /* enable the device */ |
708 | err = pci_enable_device(pci_dev); | |
709 | if (err) | |
710 | goto out; | |
711 | ||
712 | err = pci_request_regions(pci_dev, "virtio-pci"); | |
713 | if (err) | |
714 | goto out_enable_device; | |
715 | ||
716 | vp_dev->ioaddr = pci_iomap(pci_dev, 0, 0); | |
74a74b37 PST |
717 | if (vp_dev->ioaddr == NULL) { |
718 | err = -ENOMEM; | |
3343660d | 719 | goto out_req_regions; |
74a74b37 | 720 | } |
3343660d AL |
721 | |
722 | pci_set_drvdata(pci_dev, vp_dev); | |
bc505f37 | 723 | pci_set_master(pci_dev); |
3343660d AL |
724 | |
725 | /* we use the subsystem vendor/device id as the virtio vendor/device | |
726 | * id. this allows us to use the same PCI vendor/device id for all | |
727 | * virtio devices and to identify the particular virtio driver by | |
88393161 | 728 | * the subsystem ids */ |
3343660d AL |
729 | vp_dev->vdev.id.vendor = pci_dev->subsystem_vendor; |
730 | vp_dev->vdev.id.device = pci_dev->subsystem_device; | |
731 | ||
3343660d AL |
732 | /* finally register the virtio device */ |
733 | err = register_virtio_device(&vp_dev->vdev); | |
734 | if (err) | |
82af8ce8 | 735 | goto out_set_drvdata; |
3343660d AL |
736 | |
737 | return 0; | |
738 | ||
3343660d | 739 | out_set_drvdata: |
3343660d AL |
740 | pci_iounmap(pci_dev, vp_dev->ioaddr); |
741 | out_req_regions: | |
742 | pci_release_regions(pci_dev); | |
743 | out_enable_device: | |
744 | pci_disable_device(pci_dev); | |
745 | out: | |
746 | kfree(vp_dev); | |
747 | return err; | |
748 | } | |
749 | ||
8590dbc7 | 750 | static void virtio_pci_remove(struct pci_dev *pci_dev) |
3343660d AL |
751 | { |
752 | struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev); | |
753 | ||
bd6c2690 | 754 | unregister_virtio_device(&vp_dev->vdev); |
31a3ddda AS |
755 | |
756 | vp_del_vqs(&vp_dev->vdev); | |
31a3ddda AS |
757 | pci_iounmap(pci_dev, vp_dev->ioaddr); |
758 | pci_release_regions(pci_dev); | |
759 | pci_disable_device(pci_dev); | |
72103bd1 | 760 | kfree(vp_dev); |
3343660d AL |
761 | } |
762 | ||
9e266ece | 763 | #ifdef CONFIG_PM_SLEEP |
f0fe6f11 AS |
764 | static int virtio_pci_freeze(struct device *dev) |
765 | { | |
766 | struct pci_dev *pci_dev = to_pci_dev(dev); | |
767 | struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev); | |
f0fe6f11 AS |
768 | int ret; |
769 | ||
c6716bae | 770 | ret = virtio_device_freeze(&vp_dev->vdev); |
f0fe6f11 AS |
771 | |
772 | if (!ret) | |
773 | pci_disable_device(pci_dev); | |
774 | return ret; | |
775 | } | |
776 | ||
0517fdd1 | 777 | static int virtio_pci_restore(struct device *dev) |
f0fe6f11 AS |
778 | { |
779 | struct pci_dev *pci_dev = to_pci_dev(dev); | |
780 | struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev); | |
781 | int ret; | |
782 | ||
783 | ret = pci_enable_device(pci_dev); | |
784 | if (ret) | |
785 | return ret; | |
0517fdd1 | 786 | |
f0fe6f11 | 787 | pci_set_master(pci_dev); |
c6716bae | 788 | return virtio_device_restore(&vp_dev->vdev); |
f0fe6f11 AS |
789 | } |
790 | ||
d0775363 | 791 | static const struct dev_pm_ops virtio_pci_pm_ops = { |
f878d0be | 792 | SET_SYSTEM_SLEEP_PM_OPS(virtio_pci_freeze, virtio_pci_restore) |
d0775363 | 793 | }; |
3343660d AL |
794 | #endif |
795 | ||
796 | static struct pci_driver virtio_pci_driver = { | |
797 | .name = "virtio-pci", | |
798 | .id_table = virtio_pci_id_table, | |
799 | .probe = virtio_pci_probe, | |
8590dbc7 | 800 | .remove = virtio_pci_remove, |
9e266ece | 801 | #ifdef CONFIG_PM_SLEEP |
d0775363 | 802 | .driver.pm = &virtio_pci_pm_ops, |
3343660d AL |
803 | #endif |
804 | }; | |
805 | ||
1ce6853a | 806 | module_pci_driver(virtio_pci_driver); |