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