9be59d9f2f19eaac5db30b8a63fe5a3c1c83db7e
[deliverable/linux.git] / drivers / virtio / virtio_pci.c
1 /*
2 * Virtio PCI driver
3 *
4 * This module allows virtio devices to be used over a virtual PCI device.
5 * This can be used with QEMU based VMMs like KVM or Xen.
6 *
7 * Copyright IBM Corp. 2007
8 *
9 * Authors:
10 * Anthony Liguori <aliguori@us.ibm.com>
11 *
12 * This work is licensed under the terms of the GNU GPL, version 2 or later.
13 * See the COPYING file in the top-level directory.
14 *
15 */
16
17 #include <linux/module.h>
18 #include <linux/list.h>
19 #include <linux/pci.h>
20 #include <linux/slab.h>
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 */
41 void __iomem *ioaddr;
42
43 /* a list of queues so we can dispatch IRQs */
44 spinlock_t lock;
45 struct list_head virtqueues;
46
47 /* MSI-X support */
48 int msix_enabled;
49 int intx_enabled;
50 struct msix_entry *msix_entries;
51 cpumask_var_t *msix_affinity_masks;
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;
57 /* Vectors allocated, excluding per-vq vectors if any */
58 unsigned msix_used_vectors;
59
60 /* Whether we have vector per vq */
61 bool per_vq_vectors;
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,
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
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;
85
86 /* MSI-X vector (or none) */
87 unsigned msix_vector;
88 };
89
90 /* Qumranet donated their vendor ID for devices 0x1000 thru 0x10FF. */
91 static const struct pci_device_id virtio_pci_id_table[] = {
92 { PCI_DEVICE(0x1af4, PCI_ANY_ID) },
93 { 0 }
94 };
95
96 MODULE_DEVICE_TABLE(pci, virtio_pci_id_table);
97
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
104 /* virtio config->get_features() implementation */
105 static u64 vp_get_features(struct virtio_device *vdev)
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
114 /* virtio config->finalize_features() implementation */
115 static int vp_finalize_features(struct virtio_device *vdev)
116 {
117 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
118
119 /* Give virtio_ring a chance to accept features. */
120 vring_transport_features(vdev);
121
122 /* Make sure we don't have any features > 32 bits! */
123 BUG_ON((u32)vdev->features != vdev->features);
124
125 /* We only support 32 feature bits. */
126 iowrite32(vdev->features, vp_dev->ioaddr + VIRTIO_PCI_GUEST_FEATURES);
127
128 return 0;
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);
136 void __iomem *ioaddr = vp_dev->ioaddr +
137 VIRTIO_PCI_CONFIG(vp_dev) + offset;
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);
151 void __iomem *ioaddr = vp_dev->ioaddr +
152 VIRTIO_PCI_CONFIG(vp_dev) + offset;
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);
172 iowrite8(status, vp_dev->ioaddr + VIRTIO_PCI_STATUS);
173 }
174
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
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. */
192 iowrite8(0, vp_dev->ioaddr + VIRTIO_PCI_STATUS);
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);
198 }
199
200 /* the notify function used when creating a virt queue */
201 static bool vp_notify(struct virtqueue *vq)
202 {
203 struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
204
205 /* we write the queue's selector into the notification register to
206 * signal the other end */
207 iowrite16(vq->index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_NOTIFY);
208 return true;
209 }
210
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;
215
216 virtio_config_changed(&vp_dev->vdev);
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
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;
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. */
258 if (isr & VIRTIO_PCI_ISR_CONFIG)
259 vp_config_changed(irq, opaque);
260
261 return vp_vring_interrupt(irq, opaque);
262 }
263
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);
276
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
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
288 pci_disable_msix(vp_dev->pci_dev);
289 vp_dev->msix_enabled = 0;
290 }
291
292 vp_dev->msix_vectors = 0;
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;
298 kfree(vp_dev->msix_affinity_masks);
299 vp_dev->msix_affinity_masks = NULL;
300 }
301
302 static int vp_request_msix_vectors(struct virtio_device *vdev, int nvectors,
303 bool per_vq_vectors)
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;
309
310 vp_dev->msix_vectors = nvectors;
311
312 vp_dev->msix_entries = kmalloc(nvectors * sizeof *vp_dev->msix_entries,
313 GFP_KERNEL);
314 if (!vp_dev->msix_entries)
315 goto error;
316 vp_dev->msix_names = kmalloc(nvectors * sizeof *vp_dev->msix_names,
317 GFP_KERNEL);
318 if (!vp_dev->msix_names)
319 goto error;
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;
329
330 for (i = 0; i < nvectors; ++i)
331 vp_dev->msix_entries[i].entry = i;
332
333 err = pci_enable_msix_exact(vp_dev->pci_dev,
334 vp_dev->msix_entries, nvectors);
335 if (err)
336 goto error;
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;
349
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;
356 }
357
358 if (!per_vq_vectors) {
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)
367 goto error;
368 ++vp_dev->msix_used_vectors;
369 }
370 return 0;
371 error:
372 vp_free_vectors(vdev);
373 return err;
374 }
375
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)
392 {
393 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
394 struct virtio_pci_vq_info *info;
395 struct virtqueue *vq;
396 unsigned long flags, size;
397 u16 num;
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
414 info->num = num;
415 info->msix_vector = msix_vec;
416
417 size = PAGE_ALIGN(vring_size(num, VIRTIO_PCI_VRING_ALIGN));
418 info->queue = alloc_pages_exact(size, GFP_KERNEL|__GFP_ZERO);
419 if (info->queue == NULL) {
420 err = -ENOMEM;
421 goto out_info;
422 }
423
424 /* activate the queue */
425 iowrite32(virt_to_phys(info->queue) >> VIRTIO_PCI_QUEUE_ADDR_SHIFT,
426 vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN);
427
428 /* create the vring */
429 vq = vring_new_virtqueue(index, info->num, VIRTIO_PCI_VRING_ALIGN, vdev,
430 true, info->queue, vp_notify, callback, name);
431 if (!vq) {
432 err = -ENOMEM;
433 goto out_activate_queue;
434 }
435
436 vq->priv = info;
437 info->vq = vq;
438
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) {
443 err = -EBUSY;
444 goto out_assign;
445 }
446 }
447
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 }
455
456 return vq;
457
458 out_assign:
459 vring_del_virtqueue(vq);
460 out_activate_queue:
461 iowrite32(0, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN);
462 free_pages_exact(info->queue, size);
463 out_info:
464 kfree(info);
465 return ERR_PTR(err);
466 }
467
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;
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);
477
478 iowrite16(vq->index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_SEL);
479
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
487 vring_del_virtqueue(vq);
488
489 /* Select and deactivate the queue */
490 iowrite32(0, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN);
491
492 size = PAGE_ALIGN(vring_size(info->num, VIRTIO_PCI_VRING_ALIGN));
493 free_pages_exact(info->queue, size);
494 kfree(info);
495 }
496
497 /* the config->del_vqs() implementation */
498 static void vp_del_vqs(struct virtio_device *vdev)
499 {
500 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
501 struct virtqueue *vq, *n;
502 struct virtio_pci_vq_info *info;
503
504 list_for_each_entry_safe(vq, n, &vdev->vqs, list) {
505 info = vq->priv;
506 if (vp_dev->per_vq_vectors &&
507 info->msix_vector != VIRTIO_MSI_NO_VECTOR)
508 free_irq(vp_dev->msix_entries[info->msix_vector].vector,
509 vq);
510 vp_del_vq(vq);
511 }
512 vp_dev->per_vq_vectors = false;
513
514 vp_free_vectors(vdev);
515 }
516
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[],
521 bool use_msix,
522 bool per_vq_vectors)
523 {
524 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
525 u16 msix_vec;
526 int i, err, nvectors, allocated_vectors;
527
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 }
549
550 vp_dev->per_vq_vectors = per_vq_vectors;
551 allocated_vectors = vp_dev->msix_used_vectors;
552 for (i = 0; i < nvqs; ++i) {
553 if (!names[i]) {
554 vqs[i] = NULL;
555 continue;
556 } else if (!callbacks[i] || !vp_dev->msix_enabled)
557 msix_vec = VIRTIO_MSI_NO_VECTOR;
558 else if (vp_dev->per_vq_vectors)
559 msix_vec = allocated_vectors++;
560 else
561 msix_vec = VP_MSIX_VQ_VECTOR;
562 vqs[i] = setup_vq(vdev, i, callbacks[i], names[i], msix_vec);
563 if (IS_ERR(vqs[i])) {
564 err = PTR_ERR(vqs[i]);
565 goto error_find;
566 }
567
568 if (!vp_dev->per_vq_vectors || msix_vec == VIRTIO_MSI_NO_VECTOR)
569 continue;
570
571 /* allocate per-vq irq if available and necessary */
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;
583 }
584 }
585 return 0;
586
587 error_find:
588 vp_del_vqs(vdev);
589
590 error_request:
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 {
600 int err;
601
602 /* Try MSI-X with one vector per queue. */
603 err = vp_try_to_find_vqs(vdev, nvqs, vqs, callbacks, names, true, true);
604 if (!err)
605 return 0;
606 /* Fallback: MSI-X with one vector for config, one shared for queues. */
607 err = vp_try_to_find_vqs(vdev, nvqs, vqs, callbacks, names,
608 true, false);
609 if (!err)
610 return 0;
611 /* Finally fall back to regular interrupts. */
612 return vp_try_to_find_vqs(vdev, nvqs, vqs, callbacks, names,
613 false, false);
614 }
615
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
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
652 static const struct virtio_config_ops virtio_pci_config_ops = {
653 .get = vp_get,
654 .set = vp_set,
655 .get_status = vp_get_status,
656 .set_status = vp_set_status,
657 .reset = vp_reset,
658 .find_vqs = vp_find_vqs,
659 .del_vqs = vp_del_vqs,
660 .get_features = vp_get_features,
661 .finalize_features = vp_finalize_features,
662 .bus_name = vp_bus_name,
663 .set_vq_affinity = vp_set_vq_affinity,
664 };
665
666 static void virtio_pci_release_dev(struct device *_d)
667 {
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 */
673 }
674
675 /* the PCI probing function */
676 static int virtio_pci_probe(struct pci_dev *pci_dev,
677 const struct pci_device_id *id)
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
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
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
697 vp_dev->vdev.dev.parent = &pci_dev->dev;
698 vp_dev->vdev.dev.release = virtio_pci_release_dev;
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
704 /* Disable MSI/MSIX to bring device to a known good state. */
705 pci_msi_off(pci_dev);
706
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);
717 if (vp_dev->ioaddr == NULL) {
718 err = -ENOMEM;
719 goto out_req_regions;
720 }
721
722 pci_set_drvdata(pci_dev, vp_dev);
723 pci_set_master(pci_dev);
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
728 * the subsystem ids */
729 vp_dev->vdev.id.vendor = pci_dev->subsystem_vendor;
730 vp_dev->vdev.id.device = pci_dev->subsystem_device;
731
732 /* finally register the virtio device */
733 err = register_virtio_device(&vp_dev->vdev);
734 if (err)
735 goto out_set_drvdata;
736
737 return 0;
738
739 out_set_drvdata:
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
750 static void virtio_pci_remove(struct pci_dev *pci_dev)
751 {
752 struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
753
754 unregister_virtio_device(&vp_dev->vdev);
755
756 vp_del_vqs(&vp_dev->vdev);
757 pci_iounmap(pci_dev, vp_dev->ioaddr);
758 pci_release_regions(pci_dev);
759 pci_disable_device(pci_dev);
760 kfree(vp_dev);
761 }
762
763 #ifdef CONFIG_PM_SLEEP
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);
768 int ret;
769
770 ret = virtio_device_freeze(&vp_dev->vdev);
771
772 if (!ret)
773 pci_disable_device(pci_dev);
774 return ret;
775 }
776
777 static int virtio_pci_restore(struct device *dev)
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;
786
787 pci_set_master(pci_dev);
788 return virtio_device_restore(&vp_dev->vdev);
789 }
790
791 static const struct dev_pm_ops virtio_pci_pm_ops = {
792 SET_SYSTEM_SLEEP_PM_OPS(virtio_pci_freeze, virtio_pci_restore)
793 };
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,
800 .remove = virtio_pci_remove,
801 #ifdef CONFIG_PM_SLEEP
802 .driver.pm = &virtio_pci_pm_ops,
803 #endif
804 };
805
806 module_pci_driver(virtio_pci_driver);
This page took 0.04705 seconds and 4 git commands to generate.