blob: d99029d3892edf3435d4960a63ab46e397b26462 [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
Michael S. Tsirkin0b0f9dc2017-04-04 21:15:41 +030036 if (vp_dev->intx_enabled)
37 synchronize_irq(vp_dev->pci_dev->irq);
38
39 for (i = 0; i < vp_dev->msix_vectors; ++i)
Christoph Hellwigfa3a3272016-11-17 11:43:13 +010040 synchronize_irq(pci_irq_vector(vp_dev->pci_dev, i));
Michael S. Tsirkine6af5782011-11-17 17:41:15 +020041}
42
Anthony Liguori33436602007-11-12 21:30:26 -060043/* the notify function used when creating a virt queue */
Michael S. Tsirkin38eb4a22014-12-07 18:41:16 +020044bool vp_notify(struct virtqueue *vq)
Anthony Liguori33436602007-11-12 21:30:26 -060045{
Anthony Liguori33436602007-11-12 21:30:26 -060046 /* we write the queue's selector into the notification register to
47 * signal the other end */
Michael S. Tsirkinf30eaf42014-12-03 18:01:58 +020048 iowrite16(vq->index, (void __iomem *)vq->priv);
Heinz Graalfs46f9c2b2013-10-29 09:38:50 +103049 return true;
Anthony Liguori33436602007-11-12 21:30:26 -060050}
51
Michael S. Tsirkin77cf52462009-05-14 13:55:31 +030052/* 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;
Michael S. Tsirkin77cf52462009-05-14 13:55:31 +030056
Michael S. Tsirkin016c98c2014-10-14 10:40:34 +103057 virtio_config_changed(&vp_dev->vdev);
Michael S. Tsirkin77cf52462009-05-14 13:55:31 +030058 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;
Michael S. Tsirkin77cf52462009-05-14 13:55:31 +030065 irqreturn_t ret = IRQ_NONE;
Christoph Hellwig5c34d002017-02-05 18:15:18 +010066 struct virtqueue *vq;
Michael S. Tsirkin77cf52462009-05-14 13:55:31 +030067
Christoph Hellwig5c34d002017-02-05 18:15:18 +010068 list_for_each_entry(vq, &vp_dev->vdev.vqs, list) {
69 if (vq->callback && vring_interrupt(irq, vq) == IRQ_HANDLED)
Michael S. Tsirkin77cf52462009-05-14 13:55:31 +030070 ret = IRQ_HANDLED;
71 }
Michael S. Tsirkin77cf52462009-05-14 13:55:31 +030072
73 return ret;
74}
75
Anthony Liguori33436602007-11-12 21:30:26 -060076/* A small wrapper to also acknowledge the interrupt when it's handled.
77 * I really need an EIO hook for the vring so I can ack the interrupt once we
78 * know that we'll be handling the IRQ but before we invoke the callback since
79 * the callback may notify the host which results in the host attempting to
80 * raise an interrupt that we would then mask once we acknowledged the
81 * interrupt. */
82static irqreturn_t vp_interrupt(int irq, void *opaque)
83{
84 struct virtio_pci_device *vp_dev = opaque;
Anthony Liguori33436602007-11-12 21:30:26 -060085 u8 isr;
86
87 /* reading the ISR has the effect of also clearing it so it's very
88 * important to save off the value. */
Michael S. Tsirkinaf535722014-12-02 14:35:27 +020089 isr = ioread8(vp_dev->isr);
Anthony Liguori33436602007-11-12 21:30:26 -060090
91 /* It's definitely not us if the ISR was not high */
92 if (!isr)
93 return IRQ_NONE;
94
95 /* Configuration change? Tell driver if it wants to know. */
Michael S. Tsirkin77cf52462009-05-14 13:55:31 +030096 if (isr & VIRTIO_PCI_ISR_CONFIG)
97 vp_config_changed(irq, opaque);
Anthony Liguori33436602007-11-12 21:30:26 -060098
Michael S. Tsirkin77cf52462009-05-14 13:55:31 +030099 return vp_vring_interrupt(irq, opaque);
Anthony Liguori33436602007-11-12 21:30:26 -0600100}
101
Michael S. Tsirkin0b0f9dc2017-04-04 21:15:41 +0300102static int vp_request_msix_vectors(struct virtio_device *vdev, int nvectors,
103 bool per_vq_vectors, struct irq_affinity *desc)
104{
105 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
106 const char *name = dev_name(&vp_dev->vdev.dev);
107 unsigned i, v;
108 int err = -ENOMEM;
109
110 vp_dev->msix_vectors = nvectors;
111
112 vp_dev->msix_names = kmalloc(nvectors * sizeof *vp_dev->msix_names,
113 GFP_KERNEL);
114 if (!vp_dev->msix_names)
115 goto error;
116 vp_dev->msix_affinity_masks
117 = kzalloc(nvectors * sizeof *vp_dev->msix_affinity_masks,
118 GFP_KERNEL);
119 if (!vp_dev->msix_affinity_masks)
120 goto error;
121 for (i = 0; i < nvectors; ++i)
122 if (!alloc_cpumask_var(&vp_dev->msix_affinity_masks[i],
123 GFP_KERNEL))
124 goto error;
125
126 err = pci_alloc_irq_vectors_affinity(vp_dev->pci_dev, nvectors,
127 nvectors, PCI_IRQ_MSIX |
128 (desc ? PCI_IRQ_AFFINITY : 0),
129 desc);
130 if (err < 0)
131 goto error;
132 vp_dev->msix_enabled = 1;
133
134 /* Set the vector used for configuration */
135 v = vp_dev->msix_used_vectors;
136 snprintf(vp_dev->msix_names[v], sizeof *vp_dev->msix_names,
137 "%s-config", name);
138 err = request_irq(pci_irq_vector(vp_dev->pci_dev, v),
139 vp_config_changed, 0, vp_dev->msix_names[v],
140 vp_dev);
141 if (err)
142 goto error;
143 ++vp_dev->msix_used_vectors;
144
145 v = vp_dev->config_vector(vp_dev, v);
146 /* Verify we had enough resources to assign the vector */
147 if (v == VIRTIO_MSI_NO_VECTOR) {
148 err = -EBUSY;
149 goto error;
150 }
151
152 if (!per_vq_vectors) {
153 /* Shared vector for all VQs */
154 v = vp_dev->msix_used_vectors;
155 snprintf(vp_dev->msix_names[v], sizeof *vp_dev->msix_names,
156 "%s-virtqueues", name);
157 err = request_irq(pci_irq_vector(vp_dev->pci_dev, v),
158 vp_vring_interrupt, 0, vp_dev->msix_names[v],
159 vp_dev);
160 if (err)
161 goto error;
162 ++vp_dev->msix_used_vectors;
163 }
164 return 0;
165error:
166 return err;
167}
168
169/* the config->del_vqs() implementation */
170void vp_del_vqs(struct virtio_device *vdev)
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600171{
Michael S. Tsirkine969fed2009-07-26 15:48:08 +0300172 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600173 struct virtqueue *vq, *n;
Michael S. Tsirkin0b0f9dc2017-04-04 21:15:41 +0300174 int i;
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600175
Michael S. Tsirkine969fed2009-07-26 15:48:08 +0300176 list_for_each_entry_safe(vq, n, &vdev->vqs, list) {
Christoph Hellwig5c34d002017-02-05 18:15:18 +0100177 if (vp_dev->msix_vector_map) {
178 int v = vp_dev->msix_vector_map[vq->index];
Christoph Hellwigfa3a3272016-11-17 11:43:13 +0100179
180 if (v != VIRTIO_MSI_NO_VECTOR)
181 free_irq(pci_irq_vector(vp_dev->pci_dev, v),
182 vq);
183 }
Christoph Hellwig5c34d002017-02-05 18:15:18 +0100184 vp_dev->del_vq(vq);
Michael S. Tsirkine969fed2009-07-26 15:48:08 +0300185 }
Michael S. Tsirkin82af8ce2009-05-14 13:55:41 +0300186
Michael S. Tsirkin0b0f9dc2017-04-04 21:15:41 +0300187 if (vp_dev->intx_enabled) {
188 free_irq(vp_dev->pci_dev->irq, vp_dev);
189 vp_dev->intx_enabled = 0;
190 }
Christoph Hellwig66f2f552016-11-17 11:43:15 +0100191
Michael S. Tsirkin0b0f9dc2017-04-04 21:15:41 +0300192 for (i = 0; i < vp_dev->msix_used_vectors; ++i)
193 free_irq(pci_irq_vector(vp_dev->pci_dev, i), vp_dev);
Christoph Hellwig66f2f552016-11-17 11:43:15 +0100194
Michael S. Tsirkin0b0f9dc2017-04-04 21:15:41 +0300195 for (i = 0; i < vp_dev->msix_vectors; i++)
196 if (vp_dev->msix_affinity_masks[i])
Christoph Hellwig07ec5142017-02-05 18:15:19 +0100197 free_cpumask_var(vp_dev->msix_affinity_masks[i]);
198
Michael S. Tsirkin0b0f9dc2017-04-04 21:15:41 +0300199 if (vp_dev->msix_enabled) {
Christoph Hellwig66f2f552016-11-17 11:43:15 +0100200 /* Disable the vector used for configuration */
201 vp_dev->config_vector(vp_dev, VIRTIO_MSI_NO_VECTOR);
202
Michael S. Tsirkin0b0f9dc2017-04-04 21:15:41 +0300203 pci_free_irq_vectors(vp_dev->pci_dev);
204 vp_dev->msix_enabled = 0;
Christoph Hellwig66f2f552016-11-17 11:43:15 +0100205 }
206
Michael S. Tsirkin0b0f9dc2017-04-04 21:15:41 +0300207 vp_dev->msix_vectors = 0;
208 vp_dev->msix_used_vectors = 0;
209 kfree(vp_dev->msix_names);
210 vp_dev->msix_names = NULL;
211 kfree(vp_dev->msix_affinity_masks);
212 vp_dev->msix_affinity_masks = NULL;
213 kfree(vp_dev->msix_vector_map);
214 vp_dev->msix_vector_map = NULL;
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600215}
216
Christoph Hellwiga3cbec62016-11-17 11:43:16 +0100217static int vp_find_vqs_msix(struct virtio_device *vdev, unsigned nvqs,
Christoph Hellwig52a61512017-02-05 18:15:21 +0100218 struct virtqueue *vqs[], vq_callback_t *callbacks[],
Michael S. Tsirkinbf951b12017-04-04 21:08:54 +0300219 const char * const names[], bool per_vq_vectors,
220 struct irq_affinity *desc)
Michael S. Tsirkine969fed2009-07-26 15:48:08 +0300221{
222 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
Rusty Russellf68d2402009-09-23 22:26:29 -0600223 u16 msix_vec;
Michael S. Tsirkin0b0f9dc2017-04-04 21:15:41 +0300224 int i, err, nvectors, allocated_vectors;
Michael S. Tsirkine969fed2009-07-26 15:48:08 +0300225
Michael S. Tsirkinbf951b12017-04-04 21:08:54 +0300226 if (per_vq_vectors) {
Michael S. Tsirkin0b0f9dc2017-04-04 21:15:41 +0300227 /* Best option: one for change interrupt, one per vq. */
228 nvectors = 1;
229 for (i = 0; i < nvqs; ++i)
230 if (callbacks[i])
231 ++nvectors;
Michael S. Tsirkinbf951b12017-04-04 21:08:54 +0300232 } else {
Michael S. Tsirkin0b0f9dc2017-04-04 21:15:41 +0300233 /* Second best: one for change, shared for all vqs. */
234 nvectors = 2;
Rusty Russellf68d2402009-09-23 22:26:29 -0600235 }
Michael S. Tsirkine969fed2009-07-26 15:48:08 +0300236
Michael S. Tsirkin0b0f9dc2017-04-04 21:15:41 +0300237 err = vp_request_msix_vectors(vdev, nvectors, per_vq_vectors,
238 per_vq_vectors ? desc : NULL);
Christoph Hellwiga3cbec62016-11-17 11:43:16 +0100239 if (err)
Michael S. Tsirkin0b0f9dc2017-04-04 21:15:41 +0300240 goto error_find;
Christoph Hellwiga3cbec62016-11-17 11:43:16 +0100241
Michael S. Tsirkin0b0f9dc2017-04-04 21:15:41 +0300242 if (per_vq_vectors) {
243 vp_dev->msix_vector_map = kmalloc_array(nvqs,
244 sizeof(*vp_dev->msix_vector_map), GFP_KERNEL);
245 if (!vp_dev->msix_vector_map)
246 goto error_find;
Christoph Hellwig5c34d002017-02-05 18:15:18 +0100247 }
248
Michael S. Tsirkin0b0f9dc2017-04-04 21:15:41 +0300249 allocated_vectors = vp_dev->msix_used_vectors;
Michael S. Tsirkine969fed2009-07-26 15:48:08 +0300250 for (i = 0; i < nvqs; ++i) {
Michael S. Tsirkin6457f122012-09-05 21:47:45 +0300251 if (!names[i]) {
252 vqs[i] = NULL;
253 continue;
Christoph Hellwiga3cbec62016-11-17 11:43:16 +0100254 }
255
Michael S. Tsirkin0b0f9dc2017-04-04 21:15:41 +0300256 if (!callbacks[i])
Christoph Hellwig07ec5142017-02-05 18:15:19 +0100257 msix_vec = VIRTIO_MSI_NO_VECTOR;
Michael S. Tsirkin0b0f9dc2017-04-04 21:15:41 +0300258 else if (per_vq_vectors)
259 msix_vec = allocated_vectors++;
260 else
261 msix_vec = VP_MSIX_VQ_VECTOR;
Christoph Hellwig5c34d002017-02-05 18:15:18 +0100262 vqs[i] = vp_dev->setup_vq(vp_dev, i, callbacks[i], names[i],
263 msix_vec);
Michael S. Tsirkine969fed2009-07-26 15:48:08 +0300264 if (IS_ERR(vqs[i])) {
265 err = PTR_ERR(vqs[i]);
Michael S. Tsirkin0b0f9dc2017-04-04 21:15:41 +0300266 goto error_find;
Michael S. Tsirkine969fed2009-07-26 15:48:08 +0300267 }
Michael S. Tsirkin0b22bd02009-10-22 15:06:06 +0200268
Michael S. Tsirkin0b0f9dc2017-04-04 21:15:41 +0300269 if (!per_vq_vectors)
270 continue;
271
Christoph Hellwig5c34d002017-02-05 18:15:18 +0100272 if (msix_vec == VIRTIO_MSI_NO_VECTOR) {
273 vp_dev->msix_vector_map[i] = VIRTIO_MSI_NO_VECTOR;
274 continue;
275 }
276
Michael S. Tsirkin0b0f9dc2017-04-04 21:15:41 +0300277 /* allocate per-vq irq if available and necessary */
278 snprintf(vp_dev->msix_names[msix_vec],
279 sizeof *vp_dev->msix_names,
280 "%s-%s",
Michael S. Tsirkin0b22bd02009-10-22 15:06:06 +0200281 dev_name(&vp_dev->vdev.dev), names[i]);
Christoph Hellwigfa3a3272016-11-17 11:43:13 +0100282 err = request_irq(pci_irq_vector(vp_dev->pci_dev, msix_vec),
Michael S. Tsirkin0b0f9dc2017-04-04 21:15:41 +0300283 vring_interrupt, 0,
284 vp_dev->msix_names[msix_vec],
285 vqs[i]);
Christoph Hellwig5c34d002017-02-05 18:15:18 +0100286 if (err) {
287 /* don't free this irq on error */
288 vp_dev->msix_vector_map[i] = VIRTIO_MSI_NO_VECTOR;
Michael S. Tsirkin0b0f9dc2017-04-04 21:15:41 +0300289 goto error_find;
Christoph Hellwig5c34d002017-02-05 18:15:18 +0100290 }
291 vp_dev->msix_vector_map[i] = msix_vec;
Michael S. Tsirkine969fed2009-07-26 15:48:08 +0300292 }
293 return 0;
294
Michael S. Tsirkin0b0f9dc2017-04-04 21:15:41 +0300295error_find:
296 vp_del_vqs(vdev);
Michael S. Tsirkine969fed2009-07-26 15:48:08 +0300297 return err;
298}
299
Christoph Hellwiga3cbec62016-11-17 11:43:16 +0100300static int vp_find_vqs_intx(struct virtio_device *vdev, unsigned nvqs,
301 struct virtqueue *vqs[], vq_callback_t *callbacks[],
302 const char * const names[])
303{
304 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
305 int i, err;
306
Christoph Hellwiga3cbec62016-11-17 11:43:16 +0100307 err = request_irq(vp_dev->pci_dev->irq, vp_interrupt, IRQF_SHARED,
308 dev_name(&vdev->dev), vp_dev);
309 if (err)
Michael S. Tsirkin0b0f9dc2017-04-04 21:15:41 +0300310 goto out_del_vqs;
Christoph Hellwiga3cbec62016-11-17 11:43:16 +0100311
Michael S. Tsirkin0b0f9dc2017-04-04 21:15:41 +0300312 vp_dev->intx_enabled = 1;
Christoph Hellwiga3cbec62016-11-17 11:43:16 +0100313 for (i = 0; i < nvqs; ++i) {
314 if (!names[i]) {
315 vqs[i] = NULL;
316 continue;
317 }
Christoph Hellwig5c34d002017-02-05 18:15:18 +0100318 vqs[i] = vp_dev->setup_vq(vp_dev, i, callbacks[i], names[i],
Christoph Hellwiga3cbec62016-11-17 11:43:16 +0100319 VIRTIO_MSI_NO_VECTOR);
320 if (IS_ERR(vqs[i])) {
321 err = PTR_ERR(vqs[i]);
Michael S. Tsirkin0b0f9dc2017-04-04 21:15:41 +0300322 goto out_del_vqs;
Christoph Hellwiga3cbec62016-11-17 11:43:16 +0100323 }
324 }
325
326 return 0;
Michael S. Tsirkin0b0f9dc2017-04-04 21:15:41 +0300327out_del_vqs:
328 vp_del_vqs(vdev);
Christoph Hellwiga3cbec62016-11-17 11:43:16 +0100329 return err;
330}
331
Michael S. Tsirkin82af8ce2009-05-14 13:55:41 +0300332/* the config->find_vqs() implementation */
Michael S. Tsirkin38eb4a22014-12-07 18:41:16 +0200333int vp_find_vqs(struct virtio_device *vdev, unsigned nvqs,
Christoph Hellwigfb5e31d2017-02-05 18:15:22 +0100334 struct virtqueue *vqs[], vq_callback_t *callbacks[],
335 const char * const names[], struct irq_affinity *desc)
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600336{
Rusty Russellf68d2402009-09-23 22:26:29 -0600337 int err;
Michael S. Tsirkin82af8ce2009-05-14 13:55:41 +0300338
Michael S. Tsirkinbf951b12017-04-04 21:08:54 +0300339 /* Try MSI-X with one vector per queue. */
340 err = vp_find_vqs_msix(vdev, nvqs, vqs, callbacks, names, true, desc);
Michael S. Tsirkine969fed2009-07-26 15:48:08 +0300341 if (!err)
342 return 0;
Michael S. Tsirkinbf951b12017-04-04 21:08:54 +0300343 /* Fallback: MSI-X with one vector for config, one shared for queues. */
344 err = vp_find_vqs_msix(vdev, nvqs, vqs, callbacks, names, false, desc);
345 if (!err)
346 return 0;
347 /* Finally fall back to regular interrupts. */
Christoph Hellwiga3cbec62016-11-17 11:43:16 +0100348 return vp_find_vqs_intx(vdev, nvqs, vqs, callbacks, names);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600349}
350
Michael S. Tsirkin38eb4a22014-12-07 18:41:16 +0200351const char *vp_bus_name(struct virtio_device *vdev)
Rick Jones66846042011-11-14 14:17:08 +0000352{
353 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
354
355 return pci_name(vp_dev->pci_dev);
356}
357
Jason Wang75a0a522012-08-28 13:54:14 +0200358/* Setup the affinity for a virtqueue:
359 * - force the affinity for per vq vector
360 * - OR over all affinities for shared MSI
361 * - ignore the affinity request if we're using INTX
362 */
Michael S. Tsirkin38eb4a22014-12-07 18:41:16 +0200363int vp_set_vq_affinity(struct virtqueue *vq, int cpu)
Jason Wang75a0a522012-08-28 13:54:14 +0200364{
365 struct virtio_device *vdev = vq->vdev;
366 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
Jason Wang75a0a522012-08-28 13:54:14 +0200367
368 if (!vq->callback)
369 return -EINVAL;
370
Michael S. Tsirkin2008c152017-04-04 21:09:20 +0300371 if (vp_dev->msix_enabled) {
Christoph Hellwig5c34d002017-02-05 18:15:18 +0100372 int vec = vp_dev->msix_vector_map[vq->index];
373 struct cpumask *mask = vp_dev->msix_affinity_masks[vec];
374 unsigned int irq = pci_irq_vector(vp_dev->pci_dev, vec);
375
Jason Wang75a0a522012-08-28 13:54:14 +0200376 if (cpu == -1)
377 irq_set_affinity_hint(irq, NULL);
378 else {
Jiang Liu210d1502015-06-04 16:41:44 +0800379 cpumask_clear(mask);
Jason Wang75a0a522012-08-28 13:54:14 +0200380 cpumask_set_cpu(cpu, mask);
381 irq_set_affinity_hint(irq, mask);
382 }
383 }
384 return 0;
385}
386
Christoph Hellwigbbaba472017-02-05 18:15:23 +0100387const struct cpumask *vp_get_vq_affinity(struct virtio_device *vdev, int index)
388{
389 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
390 unsigned int *map = vp_dev->msix_vector_map;
391
392 if (!map || map[index] == VIRTIO_MSI_NO_VECTOR)
393 return NULL;
394
395 return pci_irq_get_affinity(vp_dev->pci_dev, map[index]);
396}
397
Aaron Lu9e266ec2013-09-09 09:57:12 +0930398#ifdef CONFIG_PM_SLEEP
Amit Shahf0fe6f12011-12-22 16:58:26 +0530399static int virtio_pci_freeze(struct device *dev)
400{
401 struct pci_dev *pci_dev = to_pci_dev(dev);
402 struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
Amit Shahf0fe6f12011-12-22 16:58:26 +0530403 int ret;
404
Michael S. Tsirkinc6716ba2014-10-14 10:40:35 +1030405 ret = virtio_device_freeze(&vp_dev->vdev);
Amit Shahf0fe6f12011-12-22 16:58:26 +0530406
407 if (!ret)
408 pci_disable_device(pci_dev);
409 return ret;
410}
411
Amit Shahf0fe6f12011-12-22 16:58:26 +0530412static int virtio_pci_restore(struct device *dev)
413{
414 struct pci_dev *pci_dev = to_pci_dev(dev);
415 struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
Amit Shahf0fe6f12011-12-22 16:58:26 +0530416 int ret;
417
Amit Shah0517fdd2012-03-29 12:54:43 +0530418 ret = pci_enable_device(pci_dev);
419 if (ret)
420 return ret;
421
422 pci_set_master(pci_dev);
Michael S. Tsirkinc6716ba2014-10-14 10:40:35 +1030423 return virtio_device_restore(&vp_dev->vdev);
Amit Shahf0fe6f12011-12-22 16:58:26 +0530424}
425
Michael S. Tsirkin9a4253d2014-12-11 21:47:49 +0200426static const struct dev_pm_ops virtio_pci_pm_ops = {
Amit Shahf878d0b2012-03-29 12:58:05 +0530427 SET_SYSTEM_SLEEP_PM_OPS(virtio_pci_freeze, virtio_pci_restore)
Amit Shahd0775362011-12-22 16:58:25 +0530428};
Anthony Liguori33436602007-11-12 21:30:26 -0600429#endif
Michael S. Tsirkin9a4253d2014-12-11 21:47:49 +0200430
431
432/* Qumranet donated their vendor ID for devices 0x1000 thru 0x10FF. */
433static const struct pci_device_id virtio_pci_id_table[] = {
Robin H. Johnsoncaf02ab2016-03-06 22:02:30 +0000434 { PCI_DEVICE(PCI_VENDOR_ID_REDHAT_QUMRANET, PCI_ANY_ID) },
Michael S. Tsirkin9a4253d2014-12-11 21:47:49 +0200435 { 0 }
436};
437
438MODULE_DEVICE_TABLE(pci, virtio_pci_id_table);
439
Michael S. Tsirkinff31d2e2015-01-13 11:23:32 +0200440static void virtio_pci_release_dev(struct device *_d)
441{
442 struct virtio_device *vdev = dev_to_virtio(_d);
443 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
444
445 /* As struct device is a kobject, it's not safe to
446 * free the memory (including the reference counter itself)
447 * until it's release callback. */
448 kfree(vp_dev);
449}
450
Michael S. Tsirkin9a4253d2014-12-11 21:47:49 +0200451static int virtio_pci_probe(struct pci_dev *pci_dev,
452 const struct pci_device_id *id)
453{
Michael S. Tsirkinff31d2e2015-01-13 11:23:32 +0200454 struct virtio_pci_device *vp_dev;
455 int rc;
456
457 /* allocate our structure and fill it out */
458 vp_dev = kzalloc(sizeof(struct virtio_pci_device), GFP_KERNEL);
459 if (!vp_dev)
460 return -ENOMEM;
461
462 pci_set_drvdata(pci_dev, vp_dev);
463 vp_dev->vdev.dev.parent = &pci_dev->dev;
464 vp_dev->vdev.dev.release = virtio_pci_release_dev;
465 vp_dev->pci_dev = pci_dev;
Michael S. Tsirkinff31d2e2015-01-13 11:23:32 +0200466
Michael S. Tsirkinff31d2e2015-01-13 11:23:32 +0200467 /* enable the device */
468 rc = pci_enable_device(pci_dev);
469 if (rc)
470 goto err_enable_device;
471
Michael S. Tsirkinac399d82015-01-15 17:54:13 +0200472 if (force_legacy) {
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200473 rc = virtio_pci_legacy_probe(vp_dev);
Michael S. Tsirkinac399d82015-01-15 17:54:13 +0200474 /* Also try modern mode if we can't map BAR0 (no IO space). */
475 if (rc == -ENODEV || rc == -ENOMEM)
476 rc = virtio_pci_modern_probe(vp_dev);
477 if (rc)
478 goto err_probe;
479 } else {
480 rc = virtio_pci_modern_probe(vp_dev);
481 if (rc == -ENODEV)
482 rc = virtio_pci_legacy_probe(vp_dev);
483 if (rc)
484 goto err_probe;
485 }
Michael S. Tsirkinff31d2e2015-01-13 11:23:32 +0200486
487 pci_set_master(pci_dev);
488
489 rc = register_virtio_device(&vp_dev->vdev);
490 if (rc)
491 goto err_register;
492
493 return 0;
494
495err_register:
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200496 if (vp_dev->ioaddr)
497 virtio_pci_legacy_remove(vp_dev);
498 else
499 virtio_pci_modern_remove(vp_dev);
Michael S. Tsirkinff31d2e2015-01-13 11:23:32 +0200500err_probe:
Michael S. Tsirkinff31d2e2015-01-13 11:23:32 +0200501 pci_disable_device(pci_dev);
502err_enable_device:
503 kfree(vp_dev);
504 return rc;
Michael S. Tsirkin9a4253d2014-12-11 21:47:49 +0200505}
506
507static void virtio_pci_remove(struct pci_dev *pci_dev)
508{
Michael S. Tsirkinff31d2e2015-01-13 11:23:32 +0200509 struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
Michael S. Tsirkin2989be02016-01-14 16:00:41 +0200510 struct device *dev = get_device(&vp_dev->vdev.dev);
Michael S. Tsirkinff31d2e2015-01-13 11:23:32 +0200511
512 unregister_virtio_device(&vp_dev->vdev);
513
Michael S. Tsirkin1fcf0512014-12-11 13:59:51 +0200514 if (vp_dev->ioaddr)
515 virtio_pci_legacy_remove(vp_dev);
516 else
517 virtio_pci_modern_remove(vp_dev);
Michael S. Tsirkinff31d2e2015-01-13 11:23:32 +0200518
Michael S. Tsirkinff31d2e2015-01-13 11:23:32 +0200519 pci_disable_device(pci_dev);
Michael S. Tsirkin2989be02016-01-14 16:00:41 +0200520 put_device(dev);
Michael S. Tsirkin9a4253d2014-12-11 21:47:49 +0200521}
522
523static struct pci_driver virtio_pci_driver = {
524 .name = "virtio-pci",
525 .id_table = virtio_pci_id_table,
526 .probe = virtio_pci_probe,
527 .remove = virtio_pci_remove,
528#ifdef CONFIG_PM_SLEEP
529 .driver.pm = &virtio_pci_pm_ops,
530#endif
531};
532
533module_pci_driver(virtio_pci_driver);
Herbert Xu5ff16112014-12-17 00:54:03 +0200534
535MODULE_AUTHOR("Anthony Liguori <aliguori@us.ibm.com>");
536MODULE_DESCRIPTION("virtio-pci");
537MODULE_LICENSE("GPL");
538MODULE_VERSION("1");