]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/virtio/virtio_pci_common.c
Merge tag 'efi-urgent' of git://git.kernel.org/pub/scm/linux/kernel/git/efi/efi into...
[mirror_ubuntu-artful-kernel.git] / drivers / virtio / virtio_pci_common.c
CommitLineData
3343660d 1/*
a90fdce9 2 * Virtio PCI driver - common functionality for all device versions
3343660d
AL
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
a90fdce9 8 * Copyright Red Hat, Inc. 2014
3343660d
AL
9 *
10 * Authors:
11 * Anthony Liguori <aliguori@us.ibm.com>
a90fdce9
MT
12 * Rusty Russell <rusty@rustcorp.com.au>
13 * Michael S. Tsirkin <mst@redhat.com>
3343660d
AL
14 *
15 * This work is licensed under the terms of the GNU GPL, version 2 or later.
16 * See the COPYING file in the top-level directory.
17 *
18 */
19
5f4c9760 20#include "virtio_pci_common.h"
3343660d 21
ac399d8f
MT
22static bool force_legacy = false;
23
24#if IS_ENABLED(CONFIG_VIRTIO_PCI_LEGACY)
25module_param(force_legacy, bool, 0444);
26MODULE_PARM_DESC(force_legacy,
27 "Force legacy mode for transitional virtio 1 devices");
28#endif
29
e6af578c 30/* wait for pending irq handlers */
38eb4a29 31void vp_synchronize_vectors(struct virtio_device *vdev)
e6af578c
MT
32{
33 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
34 int i;
35
07ec5148
CH
36 synchronize_irq(pci_irq_vector(vp_dev->pci_dev, 0));
37 for (i = 1; i < vp_dev->msix_vectors; i++)
fa3a3279 38 synchronize_irq(pci_irq_vector(vp_dev->pci_dev, i));
e6af578c
MT
39}
40
3343660d 41/* the notify function used when creating a virt queue */
38eb4a29 42bool vp_notify(struct virtqueue *vq)
3343660d 43{
3343660d
AL
44 /* we write the queue's selector into the notification register to
45 * signal the other end */
f30eaf4a 46 iowrite16(vq->index, (void __iomem *)vq->priv);
46f9c2b9 47 return true;
3343660d
AL
48}
49
77cf5246
MT
50/* Handle a configuration change: Tell driver if it wants to know. */
51static irqreturn_t vp_config_changed(int irq, void *opaque)
52{
53 struct virtio_pci_device *vp_dev = opaque;
77cf5246 54
016c98c6 55 virtio_config_changed(&vp_dev->vdev);
77cf5246
MT
56 return IRQ_HANDLED;
57}
58
59/* Notify all virtqueues on an interrupt. */
60static irqreturn_t vp_vring_interrupt(int irq, void *opaque)
61{
62 struct virtio_pci_device *vp_dev = opaque;
77cf5246 63 irqreturn_t ret = IRQ_NONE;
5c34d002 64 struct virtqueue *vq;
77cf5246 65
5c34d002
CH
66 list_for_each_entry(vq, &vp_dev->vdev.vqs, list) {
67 if (vq->callback && vring_interrupt(irq, vq) == IRQ_HANDLED)
77cf5246
MT
68 ret = IRQ_HANDLED;
69 }
77cf5246
MT
70
71 return ret;
72}
73
3343660d
AL
74/* A small wrapper to also acknowledge the interrupt when it's handled.
75 * I really need an EIO hook for the vring so I can ack the interrupt once we
76 * know that we'll be handling the IRQ but before we invoke the callback since
77 * the callback may notify the host which results in the host attempting to
78 * raise an interrupt that we would then mask once we acknowledged the
79 * interrupt. */
80static irqreturn_t vp_interrupt(int irq, void *opaque)
81{
82 struct virtio_pci_device *vp_dev = opaque;
3343660d
AL
83 u8 isr;
84
85 /* reading the ISR has the effect of also clearing it so it's very
86 * important to save off the value. */
af535722 87 isr = ioread8(vp_dev->isr);
3343660d
AL
88
89 /* It's definitely not us if the ISR was not high */
90 if (!isr)
91 return IRQ_NONE;
92
93 /* Configuration change? Tell driver if it wants to know. */
77cf5246
MT
94 if (isr & VIRTIO_PCI_ISR_CONFIG)
95 vp_config_changed(irq, opaque);
3343660d 96
77cf5246 97 return vp_vring_interrupt(irq, opaque);
3343660d
AL
98}
99
07ec5148 100static void vp_remove_vqs(struct virtio_device *vdev)
d2a7ddda 101{
e969fed5 102 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
d2a7ddda
MT
103 struct virtqueue *vq, *n;
104
e969fed5 105 list_for_each_entry_safe(vq, n, &vdev->vqs, list) {
5c34d002
CH
106 if (vp_dev->msix_vector_map) {
107 int v = vp_dev->msix_vector_map[vq->index];
fa3a3279
CH
108
109 if (v != VIRTIO_MSI_NO_VECTOR)
110 free_irq(pci_irq_vector(vp_dev->pci_dev, v),
111 vq);
112 }
5c34d002 113 vp_dev->del_vq(vq);
e969fed5 114 }
07ec5148 115}
82af8ce8 116
07ec5148
CH
117/* the config->del_vqs() implementation */
118void vp_del_vqs(struct virtio_device *vdev)
119{
120 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
121 int i;
66f2f555 122
07ec5148
CH
123 if (WARN_ON_ONCE(list_empty_careful(&vdev->vqs)))
124 return;
66f2f555 125
07ec5148 126 vp_remove_vqs(vdev);
66f2f555 127
53a020c6 128 if (vp_dev->pci_dev->msix_enabled) {
07ec5148
CH
129 for (i = 0; i < vp_dev->msix_vectors; i++)
130 free_cpumask_var(vp_dev->msix_affinity_masks[i]);
131
66f2f555
CH
132 /* Disable the vector used for configuration */
133 vp_dev->config_vector(vp_dev, VIRTIO_MSI_NO_VECTOR);
134
07ec5148
CH
135 kfree(vp_dev->msix_affinity_masks);
136 kfree(vp_dev->msix_names);
137 kfree(vp_dev->msix_vector_map);
66f2f555
CH
138 }
139
07ec5148
CH
140 free_irq(pci_irq_vector(vp_dev->pci_dev, 0), vp_dev);
141 pci_free_irq_vectors(vp_dev->pci_dev);
d2a7ddda
MT
142}
143
a3cbec69 144static int vp_find_vqs_msix(struct virtio_device *vdev, unsigned nvqs,
52a61516 145 struct virtqueue *vqs[], vq_callback_t *callbacks[],
fb5e31d9 146 const char * const names[], struct irq_affinity *desc)
d2a7ddda 147{
e969fed5 148 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
07ec5148
CH
149 const char *name = dev_name(&vp_dev->vdev.dev);
150 int i, err = -ENOMEM, allocated_vectors, nvectors;
fb5e31d9 151 unsigned flags = PCI_IRQ_MSIX;
52a61516 152 bool shared = false;
f68d2408 153 u16 msix_vec;
07ec5148 154
fb5e31d9
CH
155 if (desc) {
156 flags |= PCI_IRQ_AFFINITY;
157 desc->pre_vectors++; /* virtio config vector */
158 }
159
07ec5148
CH
160 nvectors = 1;
161 for (i = 0; i < nvqs; i++)
162 if (callbacks[i])
163 nvectors++;
82af8ce8 164
52a61516 165 /* Try one vector per queue first. */
fb5e31d9
CH
166 err = pci_alloc_irq_vectors_affinity(vp_dev->pci_dev, nvectors,
167 nvectors, flags, desc);
52a61516
CH
168 if (err < 0) {
169 /* Fallback to one vector for config, one shared for queues. */
170 shared = true;
07ec5148
CH
171 err = pci_alloc_irq_vectors(vp_dev->pci_dev, 2, 2,
172 PCI_IRQ_MSIX);
52a61516
CH
173 if (err < 0)
174 return err;
f68d2408 175 }
07ec5148
CH
176 if (err < 0)
177 return err;
d2a7ddda 178
07ec5148
CH
179 vp_dev->msix_vectors = nvectors;
180 vp_dev->msix_names = kmalloc_array(nvectors,
181 sizeof(*vp_dev->msix_names), GFP_KERNEL);
182 if (!vp_dev->msix_names)
183 goto out_free_irq_vectors;
184
185 vp_dev->msix_affinity_masks = kcalloc(nvectors,
186 sizeof(*vp_dev->msix_affinity_masks), GFP_KERNEL);
187 if (!vp_dev->msix_affinity_masks)
188 goto out_free_msix_names;
189
190 for (i = 0; i < nvectors; ++i) {
191 if (!alloc_cpumask_var(&vp_dev->msix_affinity_masks[i],
192 GFP_KERNEL))
193 goto out_free_msix_affinity_masks;
194 }
195
196 /* Set the vector used for configuration */
197 snprintf(vp_dev->msix_names[0], sizeof(*vp_dev->msix_names),
198 "%s-config", name);
199 err = request_irq(pci_irq_vector(vp_dev->pci_dev, 0), vp_config_changed,
200 0, vp_dev->msix_names[0], vp_dev);
a3cbec69 201 if (err)
52a61516 202 goto out_free_msix_affinity_masks;
a3cbec69 203
07ec5148
CH
204 /* Verify we had enough resources to assign the vector */
205 if (vp_dev->config_vector(vp_dev, 0) == VIRTIO_MSI_NO_VECTOR) {
206 err = -EBUSY;
207 goto out_free_config_irq;
5c34d002
CH
208 }
209
07ec5148
CH
210 vp_dev->msix_vector_map = kmalloc_array(nvqs,
211 sizeof(*vp_dev->msix_vector_map), GFP_KERNEL);
212 if (!vp_dev->msix_vector_map)
213 goto out_disable_config_irq;
214
215 allocated_vectors = 1; /* vector 0 is the config interrupt */
d2a7ddda 216 for (i = 0; i < nvqs; ++i) {
6457f126
MT
217 if (!names[i]) {
218 vqs[i] = NULL;
219 continue;
a3cbec69
CH
220 }
221
07ec5148
CH
222 if (callbacks[i])
223 msix_vec = allocated_vectors;
e969fed5 224 else
07ec5148
CH
225 msix_vec = VIRTIO_MSI_NO_VECTOR;
226
5c34d002
CH
227 vqs[i] = vp_dev->setup_vq(vp_dev, i, callbacks[i], names[i],
228 msix_vec);
e969fed5
MT
229 if (IS_ERR(vqs[i])) {
230 err = PTR_ERR(vqs[i]);
07ec5148 231 goto out_remove_vqs;
e969fed5 232 }
0b22bd0b 233
5c34d002
CH
234 if (msix_vec == VIRTIO_MSI_NO_VECTOR) {
235 vp_dev->msix_vector_map[i] = VIRTIO_MSI_NO_VECTOR;
236 continue;
237 }
238
07ec5148
CH
239 snprintf(vp_dev->msix_names[i + 1],
240 sizeof(*vp_dev->msix_names), "%s-%s",
0b22bd0b 241 dev_name(&vp_dev->vdev.dev), names[i]);
fa3a3279 242 err = request_irq(pci_irq_vector(vp_dev->pci_dev, msix_vec),
07ec5148
CH
243 vring_interrupt, IRQF_SHARED,
244 vp_dev->msix_names[i + 1], vqs[i]);
5c34d002
CH
245 if (err) {
246 /* don't free this irq on error */
247 vp_dev->msix_vector_map[i] = VIRTIO_MSI_NO_VECTOR;
07ec5148 248 goto out_remove_vqs;
5c34d002
CH
249 }
250 vp_dev->msix_vector_map[i] = msix_vec;
07ec5148 251
52a61516
CH
252 /*
253 * Use a different vector for each queue if they are available,
254 * else share the same vector for all VQs.
255 */
256 if (!shared)
07ec5148 257 allocated_vectors++;
d2a7ddda 258 }
07ec5148 259
d2a7ddda
MT
260 return 0;
261
07ec5148
CH
262out_remove_vqs:
263 vp_remove_vqs(vdev);
264 kfree(vp_dev->msix_vector_map);
265out_disable_config_irq:
266 vp_dev->config_vector(vp_dev, VIRTIO_MSI_NO_VECTOR);
267out_free_config_irq:
268 free_irq(pci_irq_vector(vp_dev->pci_dev, 0), vp_dev);
269out_free_msix_affinity_masks:
270 for (i = 0; i < nvectors; i++) {
271 if (vp_dev->msix_affinity_masks[i])
272 free_cpumask_var(vp_dev->msix_affinity_masks[i]);
273 }
274 kfree(vp_dev->msix_affinity_masks);
275out_free_msix_names:
276 kfree(vp_dev->msix_names);
277out_free_irq_vectors:
278 pci_free_irq_vectors(vp_dev->pci_dev);
e969fed5
MT
279 return err;
280}
281
a3cbec69
CH
282static int vp_find_vqs_intx(struct virtio_device *vdev, unsigned nvqs,
283 struct virtqueue *vqs[], vq_callback_t *callbacks[],
284 const char * const names[])
285{
286 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
287 int i, err;
288
a3cbec69
CH
289 err = request_irq(vp_dev->pci_dev->irq, vp_interrupt, IRQF_SHARED,
290 dev_name(&vdev->dev), vp_dev);
291 if (err)
07ec5148 292 return err;
a3cbec69 293
a3cbec69
CH
294 for (i = 0; i < nvqs; ++i) {
295 if (!names[i]) {
296 vqs[i] = NULL;
297 continue;
298 }
5c34d002 299 vqs[i] = vp_dev->setup_vq(vp_dev, i, callbacks[i], names[i],
a3cbec69
CH
300 VIRTIO_MSI_NO_VECTOR);
301 if (IS_ERR(vqs[i])) {
302 err = PTR_ERR(vqs[i]);
07ec5148 303 goto out_remove_vqs;
a3cbec69
CH
304 }
305 }
306
307 return 0;
07ec5148
CH
308
309out_remove_vqs:
310 vp_remove_vqs(vdev);
311 free_irq(pci_irq_vector(vp_dev->pci_dev, 0), vp_dev);
a3cbec69
CH
312 return err;
313}
314
e969fed5 315/* the config->find_vqs() implementation */
38eb4a29 316int vp_find_vqs(struct virtio_device *vdev, unsigned nvqs,
fb5e31d9
CH
317 struct virtqueue *vqs[], vq_callback_t *callbacks[],
318 const char * const names[], struct irq_affinity *desc)
e969fed5 319{
f68d2408 320 int err;
e969fed5 321
fb5e31d9 322 err = vp_find_vqs_msix(vdev, nvqs, vqs, callbacks, names, desc);
e969fed5
MT
323 if (!err)
324 return 0;
a3cbec69 325 return vp_find_vqs_intx(vdev, nvqs, vqs, callbacks, names);
d2a7ddda
MT
326}
327
38eb4a29 328const char *vp_bus_name(struct virtio_device *vdev)
66846048
RJ
329{
330 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
331
332 return pci_name(vp_dev->pci_dev);
333}
334
75a0a52b
JW
335/* Setup the affinity for a virtqueue:
336 * - force the affinity for per vq vector
337 * - OR over all affinities for shared MSI
338 * - ignore the affinity request if we're using INTX
339 */
38eb4a29 340int vp_set_vq_affinity(struct virtqueue *vq, int cpu)
75a0a52b
JW
341{
342 struct virtio_device *vdev = vq->vdev;
343 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
75a0a52b
JW
344
345 if (!vq->callback)
346 return -EINVAL;
347
53a020c6 348 if (vp_dev->pci_dev->msix_enabled) {
5c34d002
CH
349 int vec = vp_dev->msix_vector_map[vq->index];
350 struct cpumask *mask = vp_dev->msix_affinity_masks[vec];
351 unsigned int irq = pci_irq_vector(vp_dev->pci_dev, vec);
352
75a0a52b
JW
353 if (cpu == -1)
354 irq_set_affinity_hint(irq, NULL);
355 else {
210d150e 356 cpumask_clear(mask);
75a0a52b
JW
357 cpumask_set_cpu(cpu, mask);
358 irq_set_affinity_hint(irq, mask);
359 }
360 }
361 return 0;
362}
363
bbaba479
CH
364const struct cpumask *vp_get_vq_affinity(struct virtio_device *vdev, int index)
365{
366 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
367 unsigned int *map = vp_dev->msix_vector_map;
368
369 if (!map || map[index] == VIRTIO_MSI_NO_VECTOR)
370 return NULL;
371
372 return pci_irq_get_affinity(vp_dev->pci_dev, map[index]);
373}
374
9e266ece 375#ifdef CONFIG_PM_SLEEP
f0fe6f11
AS
376static int virtio_pci_freeze(struct device *dev)
377{
378 struct pci_dev *pci_dev = to_pci_dev(dev);
379 struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
f0fe6f11
AS
380 int ret;
381
c6716bae 382 ret = virtio_device_freeze(&vp_dev->vdev);
f0fe6f11
AS
383
384 if (!ret)
385 pci_disable_device(pci_dev);
386 return ret;
387}
388
0517fdd1 389static int virtio_pci_restore(struct device *dev)
f0fe6f11
AS
390{
391 struct pci_dev *pci_dev = to_pci_dev(dev);
392 struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
393 int ret;
394
395 ret = pci_enable_device(pci_dev);
396 if (ret)
397 return ret;
0517fdd1 398
f0fe6f11 399 pci_set_master(pci_dev);
c6716bae 400 return virtio_device_restore(&vp_dev->vdev);
f0fe6f11
AS
401}
402
9a4253db 403static const struct dev_pm_ops virtio_pci_pm_ops = {
f878d0be 404 SET_SYSTEM_SLEEP_PM_OPS(virtio_pci_freeze, virtio_pci_restore)
d0775363 405};
3343660d 406#endif
9a4253db
MT
407
408
409/* Qumranet donated their vendor ID for devices 0x1000 thru 0x10FF. */
410static const struct pci_device_id virtio_pci_id_table[] = {
caf02abf 411 { PCI_DEVICE(PCI_VENDOR_ID_REDHAT_QUMRANET, PCI_ANY_ID) },
9a4253db
MT
412 { 0 }
413};
414
415MODULE_DEVICE_TABLE(pci, virtio_pci_id_table);
416
ff31d2e2
MT
417static void virtio_pci_release_dev(struct device *_d)
418{
419 struct virtio_device *vdev = dev_to_virtio(_d);
420 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
421
422 /* As struct device is a kobject, it's not safe to
423 * free the memory (including the reference counter itself)
424 * until it's release callback. */
425 kfree(vp_dev);
426}
427
9a4253db
MT
428static int virtio_pci_probe(struct pci_dev *pci_dev,
429 const struct pci_device_id *id)
430{
ff31d2e2
MT
431 struct virtio_pci_device *vp_dev;
432 int rc;
433
434 /* allocate our structure and fill it out */
435 vp_dev = kzalloc(sizeof(struct virtio_pci_device), GFP_KERNEL);
436 if (!vp_dev)
437 return -ENOMEM;
438
439 pci_set_drvdata(pci_dev, vp_dev);
440 vp_dev->vdev.dev.parent = &pci_dev->dev;
441 vp_dev->vdev.dev.release = virtio_pci_release_dev;
442 vp_dev->pci_dev = pci_dev;
ff31d2e2 443
ff31d2e2
MT
444 /* enable the device */
445 rc = pci_enable_device(pci_dev);
446 if (rc)
447 goto err_enable_device;
448
ac399d8f 449 if (force_legacy) {
1fcf0512 450 rc = virtio_pci_legacy_probe(vp_dev);
ac399d8f
MT
451 /* Also try modern mode if we can't map BAR0 (no IO space). */
452 if (rc == -ENODEV || rc == -ENOMEM)
453 rc = virtio_pci_modern_probe(vp_dev);
454 if (rc)
455 goto err_probe;
456 } else {
457 rc = virtio_pci_modern_probe(vp_dev);
458 if (rc == -ENODEV)
459 rc = virtio_pci_legacy_probe(vp_dev);
460 if (rc)
461 goto err_probe;
462 }
ff31d2e2
MT
463
464 pci_set_master(pci_dev);
465
466 rc = register_virtio_device(&vp_dev->vdev);
467 if (rc)
468 goto err_register;
469
470 return 0;
471
472err_register:
1fcf0512
MT
473 if (vp_dev->ioaddr)
474 virtio_pci_legacy_remove(vp_dev);
475 else
476 virtio_pci_modern_remove(vp_dev);
ff31d2e2 477err_probe:
ff31d2e2
MT
478 pci_disable_device(pci_dev);
479err_enable_device:
480 kfree(vp_dev);
481 return rc;
9a4253db
MT
482}
483
484static void virtio_pci_remove(struct pci_dev *pci_dev)
485{
ff31d2e2 486 struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
2989be09 487 struct device *dev = get_device(&vp_dev->vdev.dev);
ff31d2e2
MT
488
489 unregister_virtio_device(&vp_dev->vdev);
490
1fcf0512
MT
491 if (vp_dev->ioaddr)
492 virtio_pci_legacy_remove(vp_dev);
493 else
494 virtio_pci_modern_remove(vp_dev);
ff31d2e2 495
ff31d2e2 496 pci_disable_device(pci_dev);
2989be09 497 put_device(dev);
9a4253db
MT
498}
499
500static struct pci_driver virtio_pci_driver = {
501 .name = "virtio-pci",
502 .id_table = virtio_pci_id_table,
503 .probe = virtio_pci_probe,
504 .remove = virtio_pci_remove,
505#ifdef CONFIG_PM_SLEEP
506 .driver.pm = &virtio_pci_pm_ops,
507#endif
508};
509
510module_pci_driver(virtio_pci_driver);
5ff16110
HX
511
512MODULE_AUTHOR("Anthony Liguori <aliguori@us.ibm.com>");
513MODULE_DESCRIPTION("virtio-pci");
514MODULE_LICENSE("GPL");
515MODULE_VERSION("1");