blob: 4608fd9aaa6c3491967c690c44e7fa8ad0a365cb [file] [log] [blame]
Anthony Liguori33436602007-11-12 21:30:26 -06001/*
Michael S. Tsirkina90fdce2014-12-08 12:31:02 +02002 * Virtio PCI driver - common functionality for all device versions
Anthony Liguori33436602007-11-12 21:30:26 -06003 *
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
Michael S. Tsirkina90fdce2014-12-08 12:31:02 +02008 * Copyright Red Hat, Inc. 2014
Anthony Liguori33436602007-11-12 21:30:26 -06009 *
10 * Authors:
11 * Anthony Liguori <aliguori@us.ibm.com>
Michael S. Tsirkina90fdce2014-12-08 12:31:02 +020012 * Rusty Russell <rusty@rustcorp.com.au>
13 * Michael S. Tsirkin <mst@redhat.com>
Anthony Liguori33436602007-11-12 21:30:26 -060014 *
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
Michael S. Tsirkin5f4c9762014-12-08 16:39:45 +020020#include "virtio_pci_common.h"
Anthony Liguori33436602007-11-12 21:30:26 -060021
Michael S. Tsirkinac399d82015-01-15 17:54:13 +020022static 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
Michael S. Tsirkine6af5782011-11-17 17:41:15 +020030/* wait for pending irq handlers */
Michael S. Tsirkin38eb4a22014-12-07 18:41:16 +020031void vp_synchronize_vectors(struct virtio_device *vdev)
Michael S. Tsirkine6af5782011-11-17 17:41:15 +020032{
33 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
34 int i;
35
Christoph Hellwig07ec5142017-02-05 18:15:19 +010036 synchronize_irq(pci_irq_vector(vp_dev->pci_dev, 0));
37 for (i = 1; i < vp_dev->msix_vectors; i++)
Christoph Hellwigfa3a3272016-11-17 11:43:13 +010038 synchronize_irq(pci_irq_vector(vp_dev->pci_dev, i));
Michael S. Tsirkine6af5782011-11-17 17:41:15 +020039}
40
Anthony Liguori33436602007-11-12 21:30:26 -060041/* the notify function used when creating a virt queue */
Michael S. Tsirkin38eb4a22014-12-07 18:41:16 +020042bool vp_notify(struct virtqueue *vq)
Anthony Liguori33436602007-11-12 21:30:26 -060043{
Anthony Liguori33436602007-11-12 21:30:26 -060044 /* we write the queue's selector into the notification register to
45 * signal the other end */
Michael S. Tsirkinf30eaf42014-12-03 18:01:58 +020046 iowrite16(vq->index, (void __iomem *)vq->priv);
Heinz Graalfs46f9c2b2013-10-29 09:38:50 +103047 return true;
Anthony Liguori33436602007-11-12 21:30:26 -060048}
49
Michael S. Tsirkin77cf52462009-05-14 13:55:31 +030050/* 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;
Michael S. Tsirkin77cf52462009-05-14 13:55:31 +030054
Michael S. Tsirkin016c98c2014-10-14 10:40:34 +103055 virtio_config_changed(&vp_dev->vdev);
Michael S. Tsirkin77cf52462009-05-14 13:55:31 +030056 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;
Michael S. Tsirkin77cf52462009-05-14 13:55:31 +030063 irqreturn_t ret = IRQ_NONE;
Christoph Hellwig5c34d002017-02-05 18:15:18 +010064 struct virtqueue *vq;
Michael S. Tsirkin77cf52462009-05-14 13:55:31 +030065
Christoph Hellwig5c34d002017-02-05 18:15:18 +010066 list_for_each_entry(vq, &vp_dev->vdev.vqs, list) {
67 if (vq->callback && vring_interrupt(irq, vq) == IRQ_HANDLED)
Michael S. Tsirkin77cf52462009-05-14 13:55:31 +030068 ret = IRQ_HANDLED;
69 }
Michael S. Tsirkin77cf52462009-05-14 13:55:31 +030070
71 return ret;
72}
73
Anthony Liguori33436602007-11-12 21:30:26 -060074/* 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;
Anthony Liguori33436602007-11-12 21:30:26 -060083 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. */
Michael S. Tsirkinaf535722014-12-02 14:35:27 +020087 isr = ioread8(vp_dev->isr);
Anthony Liguori33436602007-11-12 21:30:26 -060088
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. */
Michael S. Tsirkin77cf52462009-05-14 13:55:31 +030094 if (isr & VIRTIO_PCI_ISR_CONFIG)
95 vp_config_changed(irq, opaque);
Anthony Liguori33436602007-11-12 21:30:26 -060096
Michael S. Tsirkin77cf52462009-05-14 13:55:31 +030097 return vp_vring_interrupt(irq, opaque);
Anthony Liguori33436602007-11-12 21:30:26 -060098}
99
Christoph Hellwig07ec5142017-02-05 18:15:19 +0100100static void vp_remove_vqs(struct virtio_device *vdev)
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600101{
Michael S. Tsirkine969fed2009-07-26 15:48:08 +0300102 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600103 struct virtqueue *vq, *n;
104
Michael S. Tsirkine969fed2009-07-26 15:48:08 +0300105 list_for_each_entry_safe(vq, n, &vdev->vqs, list) {
Christoph Hellwig5c34d002017-02-05 18:15:18 +0100106 if (vp_dev->msix_vector_map) {
107 int v = vp_dev->msix_vector_map[vq->index];
Christoph Hellwigfa3a3272016-11-17 11:43:13 +0100108
109 if (v != VIRTIO_MSI_NO_VECTOR)
110 free_irq(pci_irq_vector(vp_dev->pci_dev, v),
111 vq);
112 }
Christoph Hellwig5c34d002017-02-05 18:15:18 +0100113 vp_dev->del_vq(vq);
Michael S. Tsirkine969fed2009-07-26 15:48:08 +0300114 }
Christoph Hellwig07ec5142017-02-05 18:15:19 +0100115}
Michael S. Tsirkin82af8ce2009-05-14 13:55:41 +0300116
Christoph Hellwig07ec5142017-02-05 18:15:19 +0100117/* 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;
Christoph Hellwig66f2f552016-11-17 11:43:15 +0100122
Christoph Hellwig07ec5142017-02-05 18:15:19 +0100123 if (WARN_ON_ONCE(list_empty_careful(&vdev->vqs)))
124 return;
Christoph Hellwig66f2f552016-11-17 11:43:15 +0100125
Christoph Hellwig07ec5142017-02-05 18:15:19 +0100126 vp_remove_vqs(vdev);
Christoph Hellwig66f2f552016-11-17 11:43:15 +0100127
Christoph Hellwig53a020c2017-02-05 18:15:20 +0100128 if (vp_dev->pci_dev->msix_enabled) {
Christoph Hellwig07ec5142017-02-05 18:15:19 +0100129 for (i = 0; i < vp_dev->msix_vectors; i++)
130 free_cpumask_var(vp_dev->msix_affinity_masks[i]);
131
Christoph Hellwig66f2f552016-11-17 11:43:15 +0100132 /* Disable the vector used for configuration */
133 vp_dev->config_vector(vp_dev, VIRTIO_MSI_NO_VECTOR);
134
Christoph Hellwig07ec5142017-02-05 18:15:19 +0100135 kfree(vp_dev->msix_affinity_masks);
136 kfree(vp_dev->msix_names);
137 kfree(vp_dev->msix_vector_map);
Christoph Hellwig66f2f552016-11-17 11:43:15 +0100138 }
139
Christoph Hellwig07ec5142017-02-05 18:15:19 +0100140 free_irq(pci_irq_vector(vp_dev->pci_dev, 0), vp_dev);
141 pci_free_irq_vectors(vp_dev->pci_dev);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600142}
143
Christoph Hellwiga3cbec62016-11-17 11:43:16 +0100144static int vp_find_vqs_msix(struct virtio_device *vdev, unsigned nvqs,
Christoph Hellwig52a61512017-02-05 18:15:21 +0100145 struct virtqueue *vqs[], vq_callback_t *callbacks[],
Michael S. Tsirkinbf951b12017-04-04 21:08:54 +0300146 const char * const names[], bool per_vq_vectors,
147 struct irq_affinity *desc)
Michael S. Tsirkine969fed2009-07-26 15:48:08 +0300148{
149 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
Christoph Hellwig07ec5142017-02-05 18:15:19 +0100150 const char *name = dev_name(&vp_dev->vdev.dev);
Michael S. Tsirkin8f10d012017-04-04 21:04:23 +0300151 int i, err = -ENOMEM, allocated_vectors, nvectors;
Christoph Hellwigfb5e31d2017-02-05 18:15:22 +0100152 unsigned flags = PCI_IRQ_MSIX;
Rusty Russellf68d2402009-09-23 22:26:29 -0600153 u16 msix_vec;
Christoph Hellwig07ec5142017-02-05 18:15:19 +0100154
Christoph Hellwigfb5e31d2017-02-05 18:15:22 +0100155 if (desc) {
156 flags |= PCI_IRQ_AFFINITY;
157 desc->pre_vectors++; /* virtio config vector */
158 }
159
Christoph Hellwig07ec5142017-02-05 18:15:19 +0100160 nvectors = 1;
161 for (i = 0; i < nvqs; i++)
162 if (callbacks[i])
163 nvectors++;
Michael S. Tsirkine969fed2009-07-26 15:48:08 +0300164
Michael S. Tsirkinbf951b12017-04-04 21:08:54 +0300165 if (per_vq_vectors) {
166 err = pci_alloc_irq_vectors_affinity(vp_dev->pci_dev, nvectors,
167 nvectors, flags, desc);
168 } else {
Christoph Hellwig07ec5142017-02-05 18:15:19 +0100169 err = pci_alloc_irq_vectors(vp_dev->pci_dev, 2, 2,
170 PCI_IRQ_MSIX);
171 }
172 if (err < 0)
173 return err;
174
175 vp_dev->msix_vectors = nvectors;
176 vp_dev->msix_names = kmalloc_array(nvectors,
177 sizeof(*vp_dev->msix_names), GFP_KERNEL);
178 if (!vp_dev->msix_names)
179 goto out_free_irq_vectors;
180
181 vp_dev->msix_affinity_masks = kcalloc(nvectors,
182 sizeof(*vp_dev->msix_affinity_masks), GFP_KERNEL);
183 if (!vp_dev->msix_affinity_masks)
184 goto out_free_msix_names;
185
186 for (i = 0; i < nvectors; ++i) {
187 if (!alloc_cpumask_var(&vp_dev->msix_affinity_masks[i],
188 GFP_KERNEL))
189 goto out_free_msix_affinity_masks;
Rusty Russellf68d2402009-09-23 22:26:29 -0600190 }
Michael S. Tsirkine969fed2009-07-26 15:48:08 +0300191
Christoph Hellwig07ec5142017-02-05 18:15:19 +0100192 /* Set the vector used for configuration */
193 snprintf(vp_dev->msix_names[0], sizeof(*vp_dev->msix_names),
194 "%s-config", name);
195 err = request_irq(pci_irq_vector(vp_dev->pci_dev, 0), vp_config_changed,
196 0, vp_dev->msix_names[0], vp_dev);
Christoph Hellwiga3cbec62016-11-17 11:43:16 +0100197 if (err)
Michael S. Tsirkinbf951b12017-04-04 21:08:54 +0300198 goto out_free_irq_vectors;
Christoph Hellwiga3cbec62016-11-17 11:43:16 +0100199
Christoph Hellwig07ec5142017-02-05 18:15:19 +0100200 /* Verify we had enough resources to assign the vector */
201 if (vp_dev->config_vector(vp_dev, 0) == VIRTIO_MSI_NO_VECTOR) {
202 err = -EBUSY;
203 goto out_free_config_irq;
Christoph Hellwig5c34d002017-02-05 18:15:18 +0100204 }
205
Christoph Hellwig07ec5142017-02-05 18:15:19 +0100206 vp_dev->msix_vector_map = kmalloc_array(nvqs,
207 sizeof(*vp_dev->msix_vector_map), GFP_KERNEL);
208 if (!vp_dev->msix_vector_map)
209 goto out_disable_config_irq;
210
Michael S. Tsirkin8f10d012017-04-04 21:04:23 +0300211 allocated_vectors = 1; /* vector 0 is the config interrupt */
Michael S. Tsirkine969fed2009-07-26 15:48:08 +0300212 for (i = 0; i < nvqs; ++i) {
Michael S. Tsirkin6457f122012-09-05 21:47:45 +0300213 if (!names[i]) {
214 vqs[i] = NULL;
215 continue;
Christoph Hellwiga3cbec62016-11-17 11:43:16 +0100216 }
217
Christoph Hellwig07ec5142017-02-05 18:15:19 +0100218 if (callbacks[i])
219 msix_vec = allocated_vectors;
Michael S. Tsirkine969fed2009-07-26 15:48:08 +0300220 else
Christoph Hellwig07ec5142017-02-05 18:15:19 +0100221 msix_vec = VIRTIO_MSI_NO_VECTOR;
222
Christoph Hellwig5c34d002017-02-05 18:15:18 +0100223 vqs[i] = vp_dev->setup_vq(vp_dev, i, callbacks[i], names[i],
224 msix_vec);
Michael S. Tsirkine969fed2009-07-26 15:48:08 +0300225 if (IS_ERR(vqs[i])) {
226 err = PTR_ERR(vqs[i]);
Christoph Hellwig07ec5142017-02-05 18:15:19 +0100227 goto out_remove_vqs;
Michael S. Tsirkine969fed2009-07-26 15:48:08 +0300228 }
Michael S. Tsirkin0b22bd02009-10-22 15:06:06 +0200229
Christoph Hellwig5c34d002017-02-05 18:15:18 +0100230 if (msix_vec == VIRTIO_MSI_NO_VECTOR) {
231 vp_dev->msix_vector_map[i] = VIRTIO_MSI_NO_VECTOR;
232 continue;
233 }
234
Michael S. Tsirkin8f10d012017-04-04 21:04:23 +0300235 snprintf(vp_dev->msix_names[i + 1],
Christoph Hellwig07ec5142017-02-05 18:15:19 +0100236 sizeof(*vp_dev->msix_names), "%s-%s",
Michael S. Tsirkin0b22bd02009-10-22 15:06:06 +0200237 dev_name(&vp_dev->vdev.dev), names[i]);
Christoph Hellwigfa3a3272016-11-17 11:43:13 +0100238 err = request_irq(pci_irq_vector(vp_dev->pci_dev, msix_vec),
Christoph Hellwig07ec5142017-02-05 18:15:19 +0100239 vring_interrupt, IRQF_SHARED,
Michael S. Tsirkin8f10d012017-04-04 21:04:23 +0300240 vp_dev->msix_names[i + 1], vqs[i]);
Christoph Hellwig5c34d002017-02-05 18:15:18 +0100241 if (err) {
242 /* don't free this irq on error */
243 vp_dev->msix_vector_map[i] = VIRTIO_MSI_NO_VECTOR;
Christoph Hellwig07ec5142017-02-05 18:15:19 +0100244 goto out_remove_vqs;
Christoph Hellwig5c34d002017-02-05 18:15:18 +0100245 }
246 vp_dev->msix_vector_map[i] = msix_vec;
Christoph Hellwig07ec5142017-02-05 18:15:19 +0100247
Michael S. Tsirkinbf951b12017-04-04 21:08:54 +0300248 if (per_vq_vectors)
Christoph Hellwig07ec5142017-02-05 18:15:19 +0100249 allocated_vectors++;
Michael S. Tsirkine969fed2009-07-26 15:48:08 +0300250 }
Christoph Hellwig07ec5142017-02-05 18:15:19 +0100251
Michael S. Tsirkine969fed2009-07-26 15:48:08 +0300252 return 0;
253
Christoph Hellwig07ec5142017-02-05 18:15:19 +0100254out_remove_vqs:
255 vp_remove_vqs(vdev);
256 kfree(vp_dev->msix_vector_map);
257out_disable_config_irq:
258 vp_dev->config_vector(vp_dev, VIRTIO_MSI_NO_VECTOR);
259out_free_config_irq:
260 free_irq(pci_irq_vector(vp_dev->pci_dev, 0), vp_dev);
261out_free_msix_affinity_masks:
262 for (i = 0; i < nvectors; i++) {
263 if (vp_dev->msix_affinity_masks[i])
264 free_cpumask_var(vp_dev->msix_affinity_masks[i]);
265 }
266 kfree(vp_dev->msix_affinity_masks);
267out_free_msix_names:
268 kfree(vp_dev->msix_names);
269out_free_irq_vectors:
270 pci_free_irq_vectors(vp_dev->pci_dev);
Michael S. Tsirkine969fed2009-07-26 15:48:08 +0300271 return err;
272}
273
Christoph Hellwiga3cbec62016-11-17 11:43:16 +0100274static int vp_find_vqs_intx(struct virtio_device *vdev, unsigned nvqs,
275 struct virtqueue *vqs[], vq_callback_t *callbacks[],
276 const char * const names[])
277{
278 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
279 int i, err;
280
Christoph Hellwiga3cbec62016-11-17 11:43:16 +0100281 err = request_irq(vp_dev->pci_dev->irq, vp_interrupt, IRQF_SHARED,
282 dev_name(&vdev->dev), vp_dev);
283 if (err)
Christoph Hellwig07ec5142017-02-05 18:15:19 +0100284 return err;
Christoph Hellwiga3cbec62016-11-17 11:43:16 +0100285
Christoph Hellwiga3cbec62016-11-17 11:43:16 +0100286 for (i = 0; i < nvqs; ++i) {
287 if (!names[i]) {
288 vqs[i] = NULL;
289 continue;
290 }
Christoph Hellwig5c34d002017-02-05 18:15:18 +0100291 vqs[i] = vp_dev->setup_vq(vp_dev, i, callbacks[i], names[i],
Christoph Hellwiga3cbec62016-11-17 11:43:16 +0100292 VIRTIO_MSI_NO_VECTOR);
293 if (IS_ERR(vqs[i])) {
294 err = PTR_ERR(vqs[i]);
Christoph Hellwig07ec5142017-02-05 18:15:19 +0100295 goto out_remove_vqs;
Christoph Hellwiga3cbec62016-11-17 11:43:16 +0100296 }
297 }
298
299 return 0;
Christoph Hellwig07ec5142017-02-05 18:15:19 +0100300
301out_remove_vqs:
302 vp_remove_vqs(vdev);
303 free_irq(pci_irq_vector(vp_dev->pci_dev, 0), vp_dev);
Christoph Hellwiga3cbec62016-11-17 11:43:16 +0100304 return err;
305}
306
Michael S. Tsirkin82af8ce2009-05-14 13:55:41 +0300307/* the config->find_vqs() implementation */
Michael S. Tsirkin38eb4a22014-12-07 18:41:16 +0200308int vp_find_vqs(struct virtio_device *vdev, unsigned nvqs,
Christoph Hellwigfb5e31d2017-02-05 18:15:22 +0100309 struct virtqueue *vqs[], vq_callback_t *callbacks[],
310 const char * const names[], struct irq_affinity *desc)
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600311{
Rusty Russellf68d2402009-09-23 22:26:29 -0600312 int err;
Michael S. Tsirkin82af8ce2009-05-14 13:55:41 +0300313
Michael S. Tsirkinbf951b12017-04-04 21:08:54 +0300314 /* Try MSI-X with one vector per queue. */
315 err = vp_find_vqs_msix(vdev, nvqs, vqs, callbacks, names, true, desc);
Michael S. Tsirkine969fed2009-07-26 15:48:08 +0300316 if (!err)
317 return 0;
Michael S. Tsirkinbf951b12017-04-04 21:08:54 +0300318 /* Fallback: MSI-X with one vector for config, one shared for queues. */
319 err = vp_find_vqs_msix(vdev, nvqs, vqs, callbacks, names, false, desc);
320 if (!err)
321 return 0;
322 /* Finally fall back to regular interrupts. */
Christoph Hellwiga3cbec62016-11-17 11:43:16 +0100323 return vp_find_vqs_intx(vdev, nvqs, vqs, callbacks, names);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600324}
325
Michael S. Tsirkin38eb4a22014-12-07 18:41:16 +0200326const char *vp_bus_name(struct virtio_device *vdev)
Rick Jones66846042011-11-14 14:17:08 +0000327{
328 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
329
330 return pci_name(vp_dev->pci_dev);
331}
332
Jason Wang75a0a522012-08-28 13:54:14 +0200333/* Setup the affinity for a virtqueue:
334 * - force the affinity for per vq vector
335 * - OR over all affinities for shared MSI
336 * - ignore the affinity request if we're using INTX
337 */
Michael S. Tsirkin38eb4a22014-12-07 18:41:16 +0200338int vp_set_vq_affinity(struct virtqueue *vq, int cpu)
Jason Wang75a0a522012-08-28 13:54:14 +0200339{
340 struct virtio_device *vdev = vq->vdev;
341 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
Jason Wang75a0a522012-08-28 13:54:14 +0200342
343 if (!vq->callback)
344 return -EINVAL;
345
Christoph Hellwig53a020c2017-02-05 18:15:20 +0100346 if (vp_dev->pci_dev->msix_enabled) {
Christoph Hellwig5c34d002017-02-05 18:15:18 +0100347 int vec = vp_dev->msix_vector_map[vq->index];
348 struct cpumask *mask = vp_dev->msix_affinity_masks[vec];
349 unsigned int irq = pci_irq_vector(vp_dev->pci_dev, vec);
350
Jason Wang75a0a522012-08-28 13:54:14 +0200351 if (cpu == -1)
352 irq_set_affinity_hint(irq, NULL);
353 else {
Jiang Liu210d1502015-06-04 16:41:44 +0800354 cpumask_clear(mask);
Jason Wang75a0a522012-08-28 13:54:14 +0200355 cpumask_set_cpu(cpu, mask);
356 irq_set_affinity_hint(irq, mask);
357 }
358 }
359 return 0;
360}
361
Christoph Hellwigbbaba472017-02-05 18:15:23 +0100362const struct cpumask *vp_get_vq_affinity(struct virtio_device *vdev, int index)
363{
364 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
365 unsigned int *map = vp_dev->msix_vector_map;
366
367 if (!map || map[index] == VIRTIO_MSI_NO_VECTOR)
368 return NULL;
369
370 return pci_irq_get_affinity(vp_dev->pci_dev, map[index]);
371}
372
Aaron Lu9e266ec2013-09-09 09:57:12 +0930373#ifdef CONFIG_PM_SLEEP
Amit Shahf0fe6f12011-12-22 16:58:26 +0530374static int virtio_pci_freeze(struct device *dev)
375{
376 struct pci_dev *pci_dev = to_pci_dev(dev);
377 struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
Amit Shahf0fe6f12011-12-22 16:58:26 +0530378 int ret;
379
Michael S. Tsirkinc6716ba2014-10-14 10:40:35 +1030380 ret = virtio_device_freeze(&vp_dev->vdev);
Amit Shahf0fe6f12011-12-22 16:58:26 +0530381
382 if (!ret)
383 pci_disable_device(pci_dev);
384 return ret;
385}
386
Amit Shahf0fe6f12011-12-22 16:58:26 +0530387static int virtio_pci_restore(struct device *dev)
388{
389 struct pci_dev *pci_dev = to_pci_dev(dev);
390 struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
Amit Shahf0fe6f12011-12-22 16:58:26 +0530391 int ret;
392
Amit Shah0517fdd2012-03-29 12:54:43 +0530393 ret = pci_enable_device(pci_dev);
394 if (ret)
395 return ret;
396
397 pci_set_master(pci_dev);
Michael S. Tsirkinc6716ba2014-10-14 10:40:35 +1030398 return virtio_device_restore(&vp_dev->vdev);
Amit Shahf0fe6f12011-12-22 16:58:26 +0530399}
400
Michael S. Tsirkin9a4253d2014-12-11 21:47:49 +0200401static const struct dev_pm_ops virtio_pci_pm_ops = {
Amit Shahf878d0b2012-03-29 12:58:05 +0530402 SET_SYSTEM_SLEEP_PM_OPS(virtio_pci_freeze, virtio_pci_restore)
Amit Shahd0775362011-12-22 16:58:25 +0530403};
Anthony Liguori33436602007-11-12 21:30:26 -0600404#endif
Michael S. Tsirkin9a4253d2014-12-11 21:47:49 +0200405
406
407/* Qumranet donated their vendor ID for devices 0x1000 thru 0x10FF. */
408static const struct pci_device_id virtio_pci_id_table[] = {
Robin H. Johnsoncaf02ab2016-03-06 22:02:30 +0000409 { PCI_DEVICE(PCI_VENDOR_ID_REDHAT_QUMRANET, PCI_ANY_ID) },
Michael S. Tsirkin9a4253d2014-12-11 21:47:49 +0200410 { 0 }
411};
412
413MODULE_DEVICE_TABLE(pci, virtio_pci_id_table);
414
Michael S. Tsirkinff31d2e2015-01-13 11:23:32 +0200415static void virtio_pci_release_dev(struct device *_d)
416{
417 struct virtio_device *vdev = dev_to_virtio(_d);
418 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
419
420 /* As struct device is a kobject, it's not safe to
421 * free the memory (including the reference counter itself)
422 * until it's release callback. */
423 kfree(vp_dev);
424}
425
Michael S. Tsirkin9a4253d2014-12-11 21:47:49 +0200426static int virtio_pci_probe(struct pci_dev *pci_dev,
427 const struct pci_device_id *id)
428{
Michael S. Tsirkinff31d2e2015-01-13 11:23:32 +0200429 struct virtio_pci_device *vp_dev;
430 int rc;
431
432 /* allocate our structure and fill it out */
433 vp_dev = kzalloc(sizeof(struct virtio_pci_device), GFP_KERNEL);
434 if (!vp_dev)
435 return -ENOMEM;
436
437 pci_set_drvdata(pci_dev, vp_dev);
438 vp_dev->vdev.dev.parent = &pci_dev->dev;
439 vp_dev->vdev.dev.release = virtio_pci_release_dev;
440 vp_dev->pci_dev = pci_dev;
Michael S. Tsirkinff31d2e2015-01-13 11:23:32 +0200441
Michael S. Tsirkinff31d2e2015-01-13 11:23:32 +0200442 /* enable the device */
443 rc = pci_enable_device(pci_dev);
444 if (rc)
445 goto err_enable_device;
446
Michael S. Tsirkinac399d82015-01-15 17:54:13 +0200447 if (force_legacy) {
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200448 rc = virtio_pci_legacy_probe(vp_dev);
Michael S. Tsirkinac399d82015-01-15 17:54:13 +0200449 /* Also try modern mode if we can't map BAR0 (no IO space). */
450 if (rc == -ENODEV || rc == -ENOMEM)
451 rc = virtio_pci_modern_probe(vp_dev);
452 if (rc)
453 goto err_probe;
454 } else {
455 rc = virtio_pci_modern_probe(vp_dev);
456 if (rc == -ENODEV)
457 rc = virtio_pci_legacy_probe(vp_dev);
458 if (rc)
459 goto err_probe;
460 }
Michael S. Tsirkinff31d2e2015-01-13 11:23:32 +0200461
462 pci_set_master(pci_dev);
463
464 rc = register_virtio_device(&vp_dev->vdev);
465 if (rc)
466 goto err_register;
467
468 return 0;
469
470err_register:
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200471 if (vp_dev->ioaddr)
472 virtio_pci_legacy_remove(vp_dev);
473 else
474 virtio_pci_modern_remove(vp_dev);
Michael S. Tsirkinff31d2e2015-01-13 11:23:32 +0200475err_probe:
Michael S. Tsirkinff31d2e2015-01-13 11:23:32 +0200476 pci_disable_device(pci_dev);
477err_enable_device:
478 kfree(vp_dev);
479 return rc;
Michael S. Tsirkin9a4253d2014-12-11 21:47:49 +0200480}
481
482static void virtio_pci_remove(struct pci_dev *pci_dev)
483{
Michael S. Tsirkinff31d2e2015-01-13 11:23:32 +0200484 struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
Michael S. Tsirkin2989be02016-01-14 16:00:41 +0200485 struct device *dev = get_device(&vp_dev->vdev.dev);
Michael S. Tsirkinff31d2e2015-01-13 11:23:32 +0200486
487 unregister_virtio_device(&vp_dev->vdev);
488
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200489 if (vp_dev->ioaddr)
490 virtio_pci_legacy_remove(vp_dev);
491 else
492 virtio_pci_modern_remove(vp_dev);
Michael S. Tsirkinff31d2e2015-01-13 11:23:32 +0200493
Michael S. Tsirkinff31d2e2015-01-13 11:23:32 +0200494 pci_disable_device(pci_dev);
Michael S. Tsirkin2989be02016-01-14 16:00:41 +0200495 put_device(dev);
Michael S. Tsirkin9a4253d2014-12-11 21:47:49 +0200496}
497
498static struct pci_driver virtio_pci_driver = {
499 .name = "virtio-pci",
500 .id_table = virtio_pci_id_table,
501 .probe = virtio_pci_probe,
502 .remove = virtio_pci_remove,
503#ifdef CONFIG_PM_SLEEP
504 .driver.pm = &virtio_pci_pm_ops,
505#endif
506};
507
508module_pci_driver(virtio_pci_driver);
Herbert Xu5ff16112014-12-17 00:54:03 +0200509
510MODULE_AUTHOR("Anthony Liguori <aliguori@us.ibm.com>");
511MODULE_DESCRIPTION("virtio-pci");
512MODULE_LICENSE("GPL");
513MODULE_VERSION("1");