]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/virtio/virtio_pci_common.c
virtio_pci: remove the call to vp_free_vectors in vp_request_msix_vectors
[mirror_ubuntu-zesty-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
36 if (vp_dev->intx_enabled)
37 synchronize_irq(vp_dev->pci_dev->irq);
38
39 for (i = 0; i < vp_dev->msix_vectors; ++i)
fa3a3279 40 synchronize_irq(pci_irq_vector(vp_dev->pci_dev, i));
e6af578c
MT
41}
42
3343660d 43/* the notify function used when creating a virt queue */
38eb4a29 44bool vp_notify(struct virtqueue *vq)
3343660d 45{
3343660d
AL
46 /* we write the queue's selector into the notification register to
47 * signal the other end */
f30eaf4a 48 iowrite16(vq->index, (void __iomem *)vq->priv);
46f9c2b9 49 return true;
3343660d
AL
50}
51
77cf5246
MT
52/* Handle a configuration change: Tell driver if it wants to know. */
53static irqreturn_t vp_config_changed(int irq, void *opaque)
54{
55 struct virtio_pci_device *vp_dev = opaque;
77cf5246 56
016c98c6 57 virtio_config_changed(&vp_dev->vdev);
77cf5246
MT
58 return IRQ_HANDLED;
59}
60
61/* Notify all virtqueues on an interrupt. */
62static irqreturn_t vp_vring_interrupt(int irq, void *opaque)
63{
64 struct virtio_pci_device *vp_dev = opaque;
65 struct virtio_pci_vq_info *info;
66 irqreturn_t ret = IRQ_NONE;
67 unsigned long flags;
68
69 spin_lock_irqsave(&vp_dev->lock, flags);
70 list_for_each_entry(info, &vp_dev->virtqueues, node) {
71 if (vring_interrupt(irq, info->vq) == IRQ_HANDLED)
72 ret = IRQ_HANDLED;
73 }
74 spin_unlock_irqrestore(&vp_dev->lock, flags);
75
76 return ret;
77}
78
3343660d
AL
79/* A small wrapper to also acknowledge the interrupt when it's handled.
80 * I really need an EIO hook for the vring so I can ack the interrupt once we
81 * know that we'll be handling the IRQ but before we invoke the callback since
82 * the callback may notify the host which results in the host attempting to
83 * raise an interrupt that we would then mask once we acknowledged the
84 * interrupt. */
85static irqreturn_t vp_interrupt(int irq, void *opaque)
86{
87 struct virtio_pci_device *vp_dev = opaque;
3343660d
AL
88 u8 isr;
89
90 /* reading the ISR has the effect of also clearing it so it's very
91 * important to save off the value. */
af535722 92 isr = ioread8(vp_dev->isr);
3343660d
AL
93
94 /* It's definitely not us if the ISR was not high */
95 if (!isr)
96 return IRQ_NONE;
97
98 /* Configuration change? Tell driver if it wants to know. */
77cf5246
MT
99 if (isr & VIRTIO_PCI_ISR_CONFIG)
100 vp_config_changed(irq, opaque);
3343660d 101
77cf5246 102 return vp_vring_interrupt(irq, opaque);
3343660d
AL
103}
104
82af8ce8
MT
105static void vp_free_vectors(struct virtio_device *vdev)
106{
107 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
108 int i;
109
110 if (vp_dev->intx_enabled) {
111 free_irq(vp_dev->pci_dev->irq, vp_dev);
112 vp_dev->intx_enabled = 0;
113 }
114
115 for (i = 0; i < vp_dev->msix_used_vectors; ++i)
fa3a3279 116 free_irq(pci_irq_vector(vp_dev->pci_dev, i), vp_dev);
82af8ce8 117
75a0a52b
JW
118 for (i = 0; i < vp_dev->msix_vectors; i++)
119 if (vp_dev->msix_affinity_masks[i])
120 free_cpumask_var(vp_dev->msix_affinity_masks[i]);
121
82af8ce8
MT
122 if (vp_dev->msix_enabled) {
123 /* Disable the vector used for configuration */
6f8f23d6 124 vp_dev->config_vector(vp_dev, VIRTIO_MSI_NO_VECTOR);
82af8ce8 125
fa3a3279 126 pci_free_irq_vectors(vp_dev->pci_dev);
ff52c3fc 127 vp_dev->msix_enabled = 0;
82af8ce8 128 }
ff52c3fc 129
f11335db 130 vp_dev->msix_vectors = 0;
ff52c3fc
MT
131 vp_dev->msix_used_vectors = 0;
132 kfree(vp_dev->msix_names);
133 vp_dev->msix_names = NULL;
75a0a52b
JW
134 kfree(vp_dev->msix_affinity_masks);
135 vp_dev->msix_affinity_masks = NULL;
82af8ce8
MT
136}
137
f68d2408
RR
138static int vp_request_msix_vectors(struct virtio_device *vdev, int nvectors,
139 bool per_vq_vectors)
82af8ce8
MT
140{
141 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
142 const char *name = dev_name(&vp_dev->vdev.dev);
143 unsigned i, v;
144 int err = -ENOMEM;
e969fed5 145
f11335db
AV
146 vp_dev->msix_vectors = nvectors;
147
82af8ce8
MT
148 vp_dev->msix_names = kmalloc(nvectors * sizeof *vp_dev->msix_names,
149 GFP_KERNEL);
150 if (!vp_dev->msix_names)
ff52c3fc 151 goto error;
75a0a52b
JW
152 vp_dev->msix_affinity_masks
153 = kzalloc(nvectors * sizeof *vp_dev->msix_affinity_masks,
154 GFP_KERNEL);
155 if (!vp_dev->msix_affinity_masks)
156 goto error;
157 for (i = 0; i < nvectors; ++i)
158 if (!alloc_cpumask_var(&vp_dev->msix_affinity_masks[i],
159 GFP_KERNEL))
160 goto error;
82af8ce8 161
fa3a3279
CH
162 err = pci_alloc_irq_vectors(vp_dev->pci_dev, nvectors, nvectors,
163 PCI_IRQ_MSIX);
164 if (err < 0)
e969fed5 165 goto error;
e969fed5
MT
166 vp_dev->msix_enabled = 1;
167
168 /* Set the vector used for configuration */
169 v = vp_dev->msix_used_vectors;
170 snprintf(vp_dev->msix_names[v], sizeof *vp_dev->msix_names,
171 "%s-config", name);
fa3a3279 172 err = request_irq(pci_irq_vector(vp_dev->pci_dev, v),
e969fed5
MT
173 vp_config_changed, 0, vp_dev->msix_names[v],
174 vp_dev);
175 if (err)
176 goto error;
177 ++vp_dev->msix_used_vectors;
82af8ce8 178
6f8f23d6 179 v = vp_dev->config_vector(vp_dev, v);
e969fed5 180 /* Verify we had enough resources to assign the vector */
e969fed5
MT
181 if (v == VIRTIO_MSI_NO_VECTOR) {
182 err = -EBUSY;
183 goto error;
82af8ce8
MT
184 }
185
e969fed5 186 if (!per_vq_vectors) {
82af8ce8
MT
187 /* Shared vector for all VQs */
188 v = vp_dev->msix_used_vectors;
189 snprintf(vp_dev->msix_names[v], sizeof *vp_dev->msix_names,
190 "%s-virtqueues", name);
fa3a3279 191 err = request_irq(pci_irq_vector(vp_dev->pci_dev, v),
82af8ce8
MT
192 vp_vring_interrupt, 0, vp_dev->msix_names[v],
193 vp_dev);
194 if (err)
ff52c3fc 195 goto error;
82af8ce8
MT
196 ++vp_dev->msix_used_vectors;
197 }
198 return 0;
ff52c3fc 199error:
82af8ce8
MT
200 return err;
201}
202
f68d2408
RR
203static int vp_request_intx(struct virtio_device *vdev)
204{
205 int err;
206 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
207
208 err = request_irq(vp_dev->pci_dev->irq, vp_interrupt,
209 IRQF_SHARED, dev_name(&vdev->dev), vp_dev);
210 if (!err)
211 vp_dev->intx_enabled = 1;
212 return err;
213}
214
b09f00bb
MT
215static struct virtqueue *vp_setup_vq(struct virtio_device *vdev, unsigned index,
216 void (*callback)(struct virtqueue *vq),
217 const char *name,
218 u16 msix_vec)
219{
220 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
221 struct virtio_pci_vq_info *info = kmalloc(sizeof *info, GFP_KERNEL);
222 struct virtqueue *vq;
223 unsigned long flags;
224
225 /* fill out our structure that represents an active queue */
226 if (!info)
227 return ERR_PTR(-ENOMEM);
228
229 vq = vp_dev->setup_vq(vp_dev, info, index, callback, name, msix_vec);
230 if (IS_ERR(vq))
231 goto out_info;
232
233 info->vq = vq;
005b20a8
KK
234 if (callback) {
235 spin_lock_irqsave(&vp_dev->lock, flags);
236 list_add(&info->node, &vp_dev->virtqueues);
237 spin_unlock_irqrestore(&vp_dev->lock, flags);
238 } else {
239 INIT_LIST_HEAD(&info->node);
240 }
3343660d 241
3ec7a77b 242 vp_dev->vqs[index] = info;
3343660d
AL
243 return vq;
244
3343660d
AL
245out_info:
246 kfree(info);
b09f00bb 247 return vq;
3343660d
AL
248}
249
5386cef2
MT
250static void vp_del_vq(struct virtqueue *vq)
251{
252 struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
253 struct virtio_pci_vq_info *info = vp_dev->vqs[vq->index];
254 unsigned long flags;
255
256 spin_lock_irqsave(&vp_dev->lock, flags);
257 list_del(&info->node);
258 spin_unlock_irqrestore(&vp_dev->lock, flags);
259
260 vp_dev->del_vq(info);
3343660d
AL
261 kfree(info);
262}
263
82af8ce8 264/* the config->del_vqs() implementation */
38eb4a29 265void vp_del_vqs(struct virtio_device *vdev)
d2a7ddda 266{
e969fed5 267 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
d2a7ddda
MT
268 struct virtqueue *vq, *n;
269
e969fed5 270 list_for_each_entry_safe(vq, n, &vdev->vqs, list) {
fa3a3279
CH
271 if (vp_dev->per_vq_vectors) {
272 int v = vp_dev->vqs[vq->index]->msix_vector;
273
274 if (v != VIRTIO_MSI_NO_VECTOR)
275 free_irq(pci_irq_vector(vp_dev->pci_dev, v),
276 vq);
277 }
d2a7ddda 278 vp_del_vq(vq);
e969fed5
MT
279 }
280 vp_dev->per_vq_vectors = false;
82af8ce8
MT
281
282 vp_free_vectors(vdev);
3ec7a77b 283 kfree(vp_dev->vqs);
80e9541f 284 vp_dev->vqs = NULL;
d2a7ddda
MT
285}
286
e969fed5
MT
287static int vp_try_to_find_vqs(struct virtio_device *vdev, unsigned nvqs,
288 struct virtqueue *vqs[],
289 vq_callback_t *callbacks[],
f7ad26ff 290 const char * const names[],
f68d2408 291 bool use_msix,
e969fed5 292 bool per_vq_vectors)
d2a7ddda 293{
e969fed5 294 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
f68d2408
RR
295 u16 msix_vec;
296 int i, err, nvectors, allocated_vectors;
82af8ce8 297
3ec7a77b
MT
298 vp_dev->vqs = kmalloc(nvqs * sizeof *vp_dev->vqs, GFP_KERNEL);
299 if (!vp_dev->vqs)
300 return -ENOMEM;
301
f68d2408
RR
302 if (!use_msix) {
303 /* Old style: one normal interrupt for change and all vqs. */
304 err = vp_request_intx(vdev);
305 if (err)
3ec7a77b 306 goto error_find;
f68d2408
RR
307 } else {
308 if (per_vq_vectors) {
309 /* Best option: one for change interrupt, one per vq. */
310 nvectors = 1;
311 for (i = 0; i < nvqs; ++i)
312 if (callbacks[i])
313 ++nvectors;
314 } else {
315 /* Second best: one for change, shared for all vqs. */
316 nvectors = 2;
317 }
318
319 err = vp_request_msix_vectors(vdev, nvectors, per_vq_vectors);
320 if (err)
3ec7a77b 321 goto error_find;
f68d2408 322 }
d2a7ddda 323
e969fed5
MT
324 vp_dev->per_vq_vectors = per_vq_vectors;
325 allocated_vectors = vp_dev->msix_used_vectors;
d2a7ddda 326 for (i = 0; i < nvqs; ++i) {
6457f126
MT
327 if (!names[i]) {
328 vqs[i] = NULL;
329 continue;
330 } else if (!callbacks[i] || !vp_dev->msix_enabled)
f68d2408 331 msix_vec = VIRTIO_MSI_NO_VECTOR;
e969fed5 332 else if (vp_dev->per_vq_vectors)
f68d2408 333 msix_vec = allocated_vectors++;
e969fed5 334 else
f68d2408 335 msix_vec = VP_MSIX_VQ_VECTOR;
b09f00bb 336 vqs[i] = vp_setup_vq(vdev, i, callbacks[i], names[i], msix_vec);
e969fed5
MT
337 if (IS_ERR(vqs[i])) {
338 err = PTR_ERR(vqs[i]);
82af8ce8 339 goto error_find;
e969fed5 340 }
0b22bd0b
MT
341
342 if (!vp_dev->per_vq_vectors || msix_vec == VIRTIO_MSI_NO_VECTOR)
343 continue;
344
e969fed5 345 /* allocate per-vq irq if available and necessary */
0b22bd0b
MT
346 snprintf(vp_dev->msix_names[msix_vec],
347 sizeof *vp_dev->msix_names,
348 "%s-%s",
349 dev_name(&vp_dev->vdev.dev), names[i]);
fa3a3279 350 err = request_irq(pci_irq_vector(vp_dev->pci_dev, msix_vec),
0b22bd0b
MT
351 vring_interrupt, 0,
352 vp_dev->msix_names[msix_vec],
353 vqs[i]);
d4179597 354 if (err)
0b22bd0b 355 goto error_find;
d2a7ddda
MT
356 }
357 return 0;
358
82af8ce8 359error_find:
d2a7ddda 360 vp_del_vqs(vdev);
e969fed5
MT
361 return err;
362}
363
364/* the config->find_vqs() implementation */
38eb4a29
MT
365int vp_find_vqs(struct virtio_device *vdev, unsigned nvqs,
366 struct virtqueue *vqs[],
367 vq_callback_t *callbacks[],
f7ad26ff 368 const char * const names[])
e969fed5 369{
f68d2408 370 int err;
e969fed5 371
f68d2408
RR
372 /* Try MSI-X with one vector per queue. */
373 err = vp_try_to_find_vqs(vdev, nvqs, vqs, callbacks, names, true, true);
e969fed5
MT
374 if (!err)
375 return 0;
f68d2408 376 /* Fallback: MSI-X with one vector for config, one shared for queues. */
e969fed5 377 err = vp_try_to_find_vqs(vdev, nvqs, vqs, callbacks, names,
f68d2408 378 true, false);
e969fed5
MT
379 if (!err)
380 return 0;
381 /* Finally fall back to regular interrupts. */
f68d2408
RR
382 return vp_try_to_find_vqs(vdev, nvqs, vqs, callbacks, names,
383 false, false);
d2a7ddda
MT
384}
385
38eb4a29 386const char *vp_bus_name(struct virtio_device *vdev)
66846048
RJ
387{
388 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
389
390 return pci_name(vp_dev->pci_dev);
391}
392
75a0a52b
JW
393/* Setup the affinity for a virtqueue:
394 * - force the affinity for per vq vector
395 * - OR over all affinities for shared MSI
396 * - ignore the affinity request if we're using INTX
397 */
38eb4a29 398int vp_set_vq_affinity(struct virtqueue *vq, int cpu)
75a0a52b
JW
399{
400 struct virtio_device *vdev = vq->vdev;
401 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
3ec7a77b 402 struct virtio_pci_vq_info *info = vp_dev->vqs[vq->index];
75a0a52b
JW
403 struct cpumask *mask;
404 unsigned int irq;
405
406 if (!vq->callback)
407 return -EINVAL;
408
409 if (vp_dev->msix_enabled) {
410 mask = vp_dev->msix_affinity_masks[info->msix_vector];
fa3a3279 411 irq = pci_irq_vector(vp_dev->pci_dev, info->msix_vector);
75a0a52b
JW
412 if (cpu == -1)
413 irq_set_affinity_hint(irq, NULL);
414 else {
210d150e 415 cpumask_clear(mask);
75a0a52b
JW
416 cpumask_set_cpu(cpu, mask);
417 irq_set_affinity_hint(irq, mask);
418 }
419 }
420 return 0;
421}
422
9e266ece 423#ifdef CONFIG_PM_SLEEP
f0fe6f11
AS
424static int virtio_pci_freeze(struct device *dev)
425{
426 struct pci_dev *pci_dev = to_pci_dev(dev);
427 struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
f0fe6f11
AS
428 int ret;
429
c6716bae 430 ret = virtio_device_freeze(&vp_dev->vdev);
f0fe6f11
AS
431
432 if (!ret)
433 pci_disable_device(pci_dev);
434 return ret;
435}
436
0517fdd1 437static int virtio_pci_restore(struct device *dev)
f0fe6f11
AS
438{
439 struct pci_dev *pci_dev = to_pci_dev(dev);
440 struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
441 int ret;
442
443 ret = pci_enable_device(pci_dev);
444 if (ret)
445 return ret;
0517fdd1 446
f0fe6f11 447 pci_set_master(pci_dev);
c6716bae 448 return virtio_device_restore(&vp_dev->vdev);
f0fe6f11
AS
449}
450
9a4253db 451static const struct dev_pm_ops virtio_pci_pm_ops = {
f878d0be 452 SET_SYSTEM_SLEEP_PM_OPS(virtio_pci_freeze, virtio_pci_restore)
d0775363 453};
3343660d 454#endif
9a4253db
MT
455
456
457/* Qumranet donated their vendor ID for devices 0x1000 thru 0x10FF. */
458static const struct pci_device_id virtio_pci_id_table[] = {
caf02abf 459 { PCI_DEVICE(PCI_VENDOR_ID_REDHAT_QUMRANET, PCI_ANY_ID) },
9a4253db
MT
460 { 0 }
461};
462
463MODULE_DEVICE_TABLE(pci, virtio_pci_id_table);
464
ff31d2e2
MT
465static void virtio_pci_release_dev(struct device *_d)
466{
467 struct virtio_device *vdev = dev_to_virtio(_d);
468 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
469
470 /* As struct device is a kobject, it's not safe to
471 * free the memory (including the reference counter itself)
472 * until it's release callback. */
473 kfree(vp_dev);
474}
475
9a4253db
MT
476static int virtio_pci_probe(struct pci_dev *pci_dev,
477 const struct pci_device_id *id)
478{
ff31d2e2
MT
479 struct virtio_pci_device *vp_dev;
480 int rc;
481
482 /* allocate our structure and fill it out */
483 vp_dev = kzalloc(sizeof(struct virtio_pci_device), GFP_KERNEL);
484 if (!vp_dev)
485 return -ENOMEM;
486
487 pci_set_drvdata(pci_dev, vp_dev);
488 vp_dev->vdev.dev.parent = &pci_dev->dev;
489 vp_dev->vdev.dev.release = virtio_pci_release_dev;
490 vp_dev->pci_dev = pci_dev;
491 INIT_LIST_HEAD(&vp_dev->virtqueues);
492 spin_lock_init(&vp_dev->lock);
493
ff31d2e2
MT
494 /* enable the device */
495 rc = pci_enable_device(pci_dev);
496 if (rc)
497 goto err_enable_device;
498
ac399d8f 499 if (force_legacy) {
1fcf0512 500 rc = virtio_pci_legacy_probe(vp_dev);
ac399d8f
MT
501 /* Also try modern mode if we can't map BAR0 (no IO space). */
502 if (rc == -ENODEV || rc == -ENOMEM)
503 rc = virtio_pci_modern_probe(vp_dev);
504 if (rc)
505 goto err_probe;
506 } else {
507 rc = virtio_pci_modern_probe(vp_dev);
508 if (rc == -ENODEV)
509 rc = virtio_pci_legacy_probe(vp_dev);
510 if (rc)
511 goto err_probe;
512 }
ff31d2e2
MT
513
514 pci_set_master(pci_dev);
515
516 rc = register_virtio_device(&vp_dev->vdev);
517 if (rc)
518 goto err_register;
519
520 return 0;
521
522err_register:
1fcf0512
MT
523 if (vp_dev->ioaddr)
524 virtio_pci_legacy_remove(vp_dev);
525 else
526 virtio_pci_modern_remove(vp_dev);
ff31d2e2 527err_probe:
ff31d2e2
MT
528 pci_disable_device(pci_dev);
529err_enable_device:
530 kfree(vp_dev);
531 return rc;
9a4253db
MT
532}
533
534static void virtio_pci_remove(struct pci_dev *pci_dev)
535{
ff31d2e2 536 struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
2989be09 537 struct device *dev = get_device(&vp_dev->vdev.dev);
ff31d2e2
MT
538
539 unregister_virtio_device(&vp_dev->vdev);
540
1fcf0512
MT
541 if (vp_dev->ioaddr)
542 virtio_pci_legacy_remove(vp_dev);
543 else
544 virtio_pci_modern_remove(vp_dev);
ff31d2e2 545
ff31d2e2 546 pci_disable_device(pci_dev);
2989be09 547 put_device(dev);
9a4253db
MT
548}
549
550static struct pci_driver virtio_pci_driver = {
551 .name = "virtio-pci",
552 .id_table = virtio_pci_id_table,
553 .probe = virtio_pci_probe,
554 .remove = virtio_pci_remove,
555#ifdef CONFIG_PM_SLEEP
556 .driver.pm = &virtio_pci_pm_ops,
557#endif
558};
559
560module_pci_driver(virtio_pci_driver);
5ff16110
HX
561
562MODULE_AUTHOR("Anthony Liguori <aliguori@us.ibm.com>");
563MODULE_DESCRIPTION("virtio-pci");
564MODULE_LICENSE("GPL");
565MODULE_VERSION("1");