blob: 2ef9529809d8bd198455a1af19151c22fe4ca715 [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. Tsirkine6af5782011-11-17 17:41:15 +020022/* wait for pending irq handlers */
Michael S. Tsirkin38eb4a22014-12-07 18:41:16 +020023void vp_synchronize_vectors(struct virtio_device *vdev)
Michael S. Tsirkine6af5782011-11-17 17:41:15 +020024{
25 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
26 int i;
27
28 if (vp_dev->intx_enabled)
29 synchronize_irq(vp_dev->pci_dev->irq);
30
31 for (i = 0; i < vp_dev->msix_vectors; ++i)
32 synchronize_irq(vp_dev->msix_entries[i].vector);
33}
34
Anthony Liguori33436602007-11-12 21:30:26 -060035/* the notify function used when creating a virt queue */
Michael S. Tsirkin38eb4a22014-12-07 18:41:16 +020036bool vp_notify(struct virtqueue *vq)
Anthony Liguori33436602007-11-12 21:30:26 -060037{
Anthony Liguori33436602007-11-12 21:30:26 -060038 /* we write the queue's selector into the notification register to
39 * signal the other end */
Michael S. Tsirkinf30eaf42014-12-03 18:01:58 +020040 iowrite16(vq->index, (void __iomem *)vq->priv);
Heinz Graalfs46f9c2b2013-10-29 09:38:50 +103041 return true;
Anthony Liguori33436602007-11-12 21:30:26 -060042}
43
Michael S. Tsirkin77cf52462009-05-14 13:55:31 +030044/* Handle a configuration change: Tell driver if it wants to know. */
45static irqreturn_t vp_config_changed(int irq, void *opaque)
46{
47 struct virtio_pci_device *vp_dev = opaque;
Michael S. Tsirkin77cf52462009-05-14 13:55:31 +030048
Michael S. Tsirkin016c98c2014-10-14 10:40:34 +103049 virtio_config_changed(&vp_dev->vdev);
Michael S. Tsirkin77cf52462009-05-14 13:55:31 +030050 return IRQ_HANDLED;
51}
52
53/* Notify all virtqueues on an interrupt. */
54static irqreturn_t vp_vring_interrupt(int irq, void *opaque)
55{
56 struct virtio_pci_device *vp_dev = opaque;
57 struct virtio_pci_vq_info *info;
58 irqreturn_t ret = IRQ_NONE;
59 unsigned long flags;
60
61 spin_lock_irqsave(&vp_dev->lock, flags);
62 list_for_each_entry(info, &vp_dev->virtqueues, node) {
63 if (vring_interrupt(irq, info->vq) == IRQ_HANDLED)
64 ret = IRQ_HANDLED;
65 }
66 spin_unlock_irqrestore(&vp_dev->lock, flags);
67
68 return ret;
69}
70
Anthony Liguori33436602007-11-12 21:30:26 -060071/* A small wrapper to also acknowledge the interrupt when it's handled.
72 * I really need an EIO hook for the vring so I can ack the interrupt once we
73 * know that we'll be handling the IRQ but before we invoke the callback since
74 * the callback may notify the host which results in the host attempting to
75 * raise an interrupt that we would then mask once we acknowledged the
76 * interrupt. */
77static irqreturn_t vp_interrupt(int irq, void *opaque)
78{
79 struct virtio_pci_device *vp_dev = opaque;
Anthony Liguori33436602007-11-12 21:30:26 -060080 u8 isr;
81
82 /* reading the ISR has the effect of also clearing it so it's very
83 * important to save off the value. */
Michael S. Tsirkinaf535722014-12-02 14:35:27 +020084 isr = ioread8(vp_dev->isr);
Anthony Liguori33436602007-11-12 21:30:26 -060085
86 /* It's definitely not us if the ISR was not high */
87 if (!isr)
88 return IRQ_NONE;
89
90 /* Configuration change? Tell driver if it wants to know. */
Michael S. Tsirkin77cf52462009-05-14 13:55:31 +030091 if (isr & VIRTIO_PCI_ISR_CONFIG)
92 vp_config_changed(irq, opaque);
Anthony Liguori33436602007-11-12 21:30:26 -060093
Michael S. Tsirkin77cf52462009-05-14 13:55:31 +030094 return vp_vring_interrupt(irq, opaque);
Anthony Liguori33436602007-11-12 21:30:26 -060095}
96
Michael S. Tsirkin82af8ce2009-05-14 13:55:41 +030097static void vp_free_vectors(struct virtio_device *vdev)
98{
99 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
100 int i;
101
102 if (vp_dev->intx_enabled) {
103 free_irq(vp_dev->pci_dev->irq, vp_dev);
104 vp_dev->intx_enabled = 0;
105 }
106
107 for (i = 0; i < vp_dev->msix_used_vectors; ++i)
108 free_irq(vp_dev->msix_entries[i].vector, vp_dev);
Michael S. Tsirkin82af8ce2009-05-14 13:55:41 +0300109
Jason Wang75a0a522012-08-28 13:54:14 +0200110 for (i = 0; i < vp_dev->msix_vectors; i++)
111 if (vp_dev->msix_affinity_masks[i])
112 free_cpumask_var(vp_dev->msix_affinity_masks[i]);
113
Michael S. Tsirkin82af8ce2009-05-14 13:55:41 +0300114 if (vp_dev->msix_enabled) {
115 /* Disable the vector used for configuration */
Michael S. Tsirkin6f8f23d2014-12-07 18:18:09 +0200116 vp_dev->config_vector(vp_dev, VIRTIO_MSI_NO_VECTOR);
Michael S. Tsirkin82af8ce2009-05-14 13:55:41 +0300117
Michael S. Tsirkin82af8ce2009-05-14 13:55:41 +0300118 pci_disable_msix(vp_dev->pci_dev);
Michael S. Tsirkinff52c3f2009-07-23 14:57:37 +0300119 vp_dev->msix_enabled = 0;
Michael S. Tsirkin82af8ce2009-05-14 13:55:41 +0300120 }
Michael S. Tsirkinff52c3f2009-07-23 14:57:37 +0300121
Andrew Vaginf11335d2013-07-02 15:35:13 +0930122 vp_dev->msix_vectors = 0;
Michael S. Tsirkinff52c3f2009-07-23 14:57:37 +0300123 vp_dev->msix_used_vectors = 0;
124 kfree(vp_dev->msix_names);
125 vp_dev->msix_names = NULL;
126 kfree(vp_dev->msix_entries);
127 vp_dev->msix_entries = NULL;
Jason Wang75a0a522012-08-28 13:54:14 +0200128 kfree(vp_dev->msix_affinity_masks);
129 vp_dev->msix_affinity_masks = NULL;
Michael S. Tsirkin82af8ce2009-05-14 13:55:41 +0300130}
131
Rusty Russellf68d2402009-09-23 22:26:29 -0600132static int vp_request_msix_vectors(struct virtio_device *vdev, int nvectors,
133 bool per_vq_vectors)
Michael S. Tsirkin82af8ce2009-05-14 13:55:41 +0300134{
135 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
136 const char *name = dev_name(&vp_dev->vdev.dev);
137 unsigned i, v;
138 int err = -ENOMEM;
Michael S. Tsirkine969fed2009-07-26 15:48:08 +0300139
Andrew Vaginf11335d2013-07-02 15:35:13 +0930140 vp_dev->msix_vectors = nvectors;
141
Michael S. Tsirkin82af8ce2009-05-14 13:55:41 +0300142 vp_dev->msix_entries = kmalloc(nvectors * sizeof *vp_dev->msix_entries,
143 GFP_KERNEL);
144 if (!vp_dev->msix_entries)
Michael S. Tsirkinff52c3f2009-07-23 14:57:37 +0300145 goto error;
Michael S. Tsirkin82af8ce2009-05-14 13:55:41 +0300146 vp_dev->msix_names = kmalloc(nvectors * sizeof *vp_dev->msix_names,
147 GFP_KERNEL);
148 if (!vp_dev->msix_names)
Michael S. Tsirkinff52c3f2009-07-23 14:57:37 +0300149 goto error;
Jason Wang75a0a522012-08-28 13:54:14 +0200150 vp_dev->msix_affinity_masks
151 = kzalloc(nvectors * sizeof *vp_dev->msix_affinity_masks,
152 GFP_KERNEL);
153 if (!vp_dev->msix_affinity_masks)
154 goto error;
155 for (i = 0; i < nvectors; ++i)
156 if (!alloc_cpumask_var(&vp_dev->msix_affinity_masks[i],
157 GFP_KERNEL))
158 goto error;
Michael S. Tsirkin82af8ce2009-05-14 13:55:41 +0300159
160 for (i = 0; i < nvectors; ++i)
161 vp_dev->msix_entries[i].entry = i;
162
Alexander Gordeev5e37f672014-03-13 11:23:37 +1030163 err = pci_enable_msix_exact(vp_dev->pci_dev,
164 vp_dev->msix_entries, nvectors);
Michael S. Tsirkine969fed2009-07-26 15:48:08 +0300165 if (err)
166 goto error;
Michael S. Tsirkine969fed2009-07-26 15:48:08 +0300167 vp_dev->msix_enabled = 1;
Michael S. Tsirkin82af8ce2009-05-14 13:55:41 +0300168
Michael S. Tsirkine969fed2009-07-26 15:48:08 +0300169 /* Set the vector used for configuration */
170 v = vp_dev->msix_used_vectors;
171 snprintf(vp_dev->msix_names[v], sizeof *vp_dev->msix_names,
172 "%s-config", name);
173 err = request_irq(vp_dev->msix_entries[v].vector,
174 vp_config_changed, 0, vp_dev->msix_names[v],
175 vp_dev);
176 if (err)
177 goto error;
178 ++vp_dev->msix_used_vectors;
Michael S. Tsirkin82af8ce2009-05-14 13:55:41 +0300179
Michael S. Tsirkin6f8f23d2014-12-07 18:18:09 +0200180 v = vp_dev->config_vector(vp_dev, v);
Michael S. Tsirkine969fed2009-07-26 15:48:08 +0300181 /* Verify we had enough resources to assign the vector */
Michael S. Tsirkine969fed2009-07-26 15:48:08 +0300182 if (v == VIRTIO_MSI_NO_VECTOR) {
183 err = -EBUSY;
184 goto error;
Michael S. Tsirkin82af8ce2009-05-14 13:55:41 +0300185 }
186
Michael S. Tsirkine969fed2009-07-26 15:48:08 +0300187 if (!per_vq_vectors) {
Michael S. Tsirkin82af8ce2009-05-14 13:55:41 +0300188 /* Shared vector for all VQs */
189 v = vp_dev->msix_used_vectors;
190 snprintf(vp_dev->msix_names[v], sizeof *vp_dev->msix_names,
191 "%s-virtqueues", name);
192 err = request_irq(vp_dev->msix_entries[v].vector,
193 vp_vring_interrupt, 0, vp_dev->msix_names[v],
194 vp_dev);
195 if (err)
Michael S. Tsirkinff52c3f2009-07-23 14:57:37 +0300196 goto error;
Michael S. Tsirkin82af8ce2009-05-14 13:55:41 +0300197 ++vp_dev->msix_used_vectors;
198 }
199 return 0;
Michael S. Tsirkinff52c3f2009-07-23 14:57:37 +0300200error:
Michael S. Tsirkin82af8ce2009-05-14 13:55:41 +0300201 vp_free_vectors(vdev);
Michael S. Tsirkin82af8ce2009-05-14 13:55:41 +0300202 return err;
203}
204
Rusty Russellf68d2402009-09-23 22:26:29 -0600205static int vp_request_intx(struct virtio_device *vdev)
206{
207 int err;
208 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
209
210 err = request_irq(vp_dev->pci_dev->irq, vp_interrupt,
211 IRQF_SHARED, dev_name(&vdev->dev), vp_dev);
212 if (!err)
213 vp_dev->intx_enabled = 1;
214 return err;
215}
216
Michael S. Tsirkinb09f00b2014-12-07 18:03:49 +0200217static struct virtqueue *vp_setup_vq(struct virtio_device *vdev, unsigned index,
218 void (*callback)(struct virtqueue *vq),
219 const char *name,
220 u16 msix_vec)
221{
222 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
223 struct virtio_pci_vq_info *info = kmalloc(sizeof *info, GFP_KERNEL);
224 struct virtqueue *vq;
225 unsigned long flags;
226
227 /* fill out our structure that represents an active queue */
228 if (!info)
229 return ERR_PTR(-ENOMEM);
230
231 vq = vp_dev->setup_vq(vp_dev, info, index, callback, name, msix_vec);
232 if (IS_ERR(vq))
233 goto out_info;
234
235 info->vq = vq;
Krishna Kumar005b20a2011-10-05 11:08:59 +0530236 if (callback) {
237 spin_lock_irqsave(&vp_dev->lock, flags);
238 list_add(&info->node, &vp_dev->virtqueues);
239 spin_unlock_irqrestore(&vp_dev->lock, flags);
240 } else {
241 INIT_LIST_HEAD(&info->node);
242 }
Anthony Liguori33436602007-11-12 21:30:26 -0600243
Michael S. Tsirkin3ec7a772014-12-03 17:50:50 +0200244 vp_dev->vqs[index] = info;
Anthony Liguori33436602007-11-12 21:30:26 -0600245 return vq;
246
Anthony Liguori33436602007-11-12 21:30:26 -0600247out_info:
248 kfree(info);
Michael S. Tsirkinb09f00b2014-12-07 18:03:49 +0200249 return vq;
Anthony Liguori33436602007-11-12 21:30:26 -0600250}
251
Michael S. Tsirkin5386cef2014-12-07 18:03:49 +0200252static void vp_del_vq(struct virtqueue *vq)
253{
254 struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
255 struct virtio_pci_vq_info *info = vp_dev->vqs[vq->index];
256 unsigned long flags;
257
258 spin_lock_irqsave(&vp_dev->lock, flags);
259 list_del(&info->node);
260 spin_unlock_irqrestore(&vp_dev->lock, flags);
261
262 vp_dev->del_vq(info);
Anthony Liguori33436602007-11-12 21:30:26 -0600263 kfree(info);
264}
265
Michael S. Tsirkin82af8ce2009-05-14 13:55:41 +0300266/* the config->del_vqs() implementation */
Michael S. Tsirkin38eb4a22014-12-07 18:41:16 +0200267void vp_del_vqs(struct virtio_device *vdev)
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600268{
Michael S. Tsirkine969fed2009-07-26 15:48:08 +0300269 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600270 struct virtqueue *vq, *n;
Michael S. Tsirkine969fed2009-07-26 15:48:08 +0300271 struct virtio_pci_vq_info *info;
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600272
Michael S. Tsirkine969fed2009-07-26 15:48:08 +0300273 list_for_each_entry_safe(vq, n, &vdev->vqs, list) {
Michael S. Tsirkin3ec7a772014-12-03 17:50:50 +0200274 info = vp_dev->vqs[vq->index];
Michael S. Tsirkin31198152010-02-25 19:08:55 +0200275 if (vp_dev->per_vq_vectors &&
276 info->msix_vector != VIRTIO_MSI_NO_VECTOR)
Rusty Russellf68d2402009-09-23 22:26:29 -0600277 free_irq(vp_dev->msix_entries[info->msix_vector].vector,
278 vq);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600279 vp_del_vq(vq);
Michael S. Tsirkine969fed2009-07-26 15:48:08 +0300280 }
281 vp_dev->per_vq_vectors = false;
Michael S. Tsirkin82af8ce2009-05-14 13:55:41 +0300282
283 vp_free_vectors(vdev);
Michael S. Tsirkin3ec7a772014-12-03 17:50:50 +0200284 kfree(vp_dev->vqs);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600285}
286
Michael S. Tsirkine969fed2009-07-26 15:48:08 +0300287static int vp_try_to_find_vqs(struct virtio_device *vdev, unsigned nvqs,
288 struct virtqueue *vqs[],
289 vq_callback_t *callbacks[],
290 const char *names[],
Rusty Russellf68d2402009-09-23 22:26:29 -0600291 bool use_msix,
Michael S. Tsirkine969fed2009-07-26 15:48:08 +0300292 bool per_vq_vectors)
293{
294 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
Rusty Russellf68d2402009-09-23 22:26:29 -0600295 u16 msix_vec;
296 int i, err, nvectors, allocated_vectors;
Michael S. Tsirkine969fed2009-07-26 15:48:08 +0300297
Michael S. Tsirkin3ec7a772014-12-03 17:50:50 +0200298 vp_dev->vqs = kmalloc(nvqs * sizeof *vp_dev->vqs, GFP_KERNEL);
299 if (!vp_dev->vqs)
300 return -ENOMEM;
301
Rusty Russellf68d2402009-09-23 22:26:29 -0600302 if (!use_msix) {
303 /* Old style: one normal interrupt for change and all vqs. */
304 err = vp_request_intx(vdev);
305 if (err)
Michael S. Tsirkin3ec7a772014-12-03 17:50:50 +0200306 goto error_find;
Rusty Russellf68d2402009-09-23 22:26:29 -0600307 } else {
308 if (per_vq_vectors) {
309 /* Best option: one for change interrupt, one per vq. */
310 nvectors = 1;
311 for (i = 0; i < nvqs; ++i)
312 if (callbacks[i])
313 ++nvectors;
314 } else {
315 /* Second best: one for change, shared for all vqs. */
316 nvectors = 2;
317 }
318
319 err = vp_request_msix_vectors(vdev, nvectors, per_vq_vectors);
320 if (err)
Michael S. Tsirkin3ec7a772014-12-03 17:50:50 +0200321 goto error_find;
Rusty Russellf68d2402009-09-23 22:26:29 -0600322 }
Michael S. Tsirkine969fed2009-07-26 15:48:08 +0300323
324 vp_dev->per_vq_vectors = per_vq_vectors;
325 allocated_vectors = vp_dev->msix_used_vectors;
326 for (i = 0; i < nvqs; ++i) {
Michael S. Tsirkin6457f122012-09-05 21:47:45 +0300327 if (!names[i]) {
328 vqs[i] = NULL;
329 continue;
330 } else if (!callbacks[i] || !vp_dev->msix_enabled)
Rusty Russellf68d2402009-09-23 22:26:29 -0600331 msix_vec = VIRTIO_MSI_NO_VECTOR;
Michael S. Tsirkine969fed2009-07-26 15:48:08 +0300332 else if (vp_dev->per_vq_vectors)
Rusty Russellf68d2402009-09-23 22:26:29 -0600333 msix_vec = allocated_vectors++;
Michael S. Tsirkine969fed2009-07-26 15:48:08 +0300334 else
Rusty Russellf68d2402009-09-23 22:26:29 -0600335 msix_vec = VP_MSIX_VQ_VECTOR;
Michael S. Tsirkinb09f00b2014-12-07 18:03:49 +0200336 vqs[i] = vp_setup_vq(vdev, i, callbacks[i], names[i], msix_vec);
Michael S. Tsirkine969fed2009-07-26 15:48:08 +0300337 if (IS_ERR(vqs[i])) {
338 err = PTR_ERR(vqs[i]);
339 goto error_find;
340 }
Michael S. Tsirkin0b22bd02009-10-22 15:06:06 +0200341
342 if (!vp_dev->per_vq_vectors || msix_vec == VIRTIO_MSI_NO_VECTOR)
343 continue;
344
Michael S. Tsirkine969fed2009-07-26 15:48:08 +0300345 /* allocate per-vq irq if available and necessary */
Michael S. Tsirkin0b22bd02009-10-22 15:06:06 +0200346 snprintf(vp_dev->msix_names[msix_vec],
347 sizeof *vp_dev->msix_names,
348 "%s-%s",
349 dev_name(&vp_dev->vdev.dev), names[i]);
350 err = request_irq(vp_dev->msix_entries[msix_vec].vector,
351 vring_interrupt, 0,
352 vp_dev->msix_names[msix_vec],
353 vqs[i]);
354 if (err) {
355 vp_del_vq(vqs[i]);
356 goto error_find;
Michael S. Tsirkine969fed2009-07-26 15:48:08 +0300357 }
358 }
359 return 0;
360
361error_find:
362 vp_del_vqs(vdev);
Michael S. Tsirkine969fed2009-07-26 15:48:08 +0300363 return err;
364}
365
Michael S. Tsirkin82af8ce2009-05-14 13:55:41 +0300366/* the config->find_vqs() implementation */
Michael S. Tsirkin38eb4a22014-12-07 18:41:16 +0200367int vp_find_vqs(struct virtio_device *vdev, unsigned nvqs,
368 struct virtqueue *vqs[],
369 vq_callback_t *callbacks[],
370 const char *names[])
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600371{
Rusty Russellf68d2402009-09-23 22:26:29 -0600372 int err;
Michael S. Tsirkin82af8ce2009-05-14 13:55:41 +0300373
Rusty Russellf68d2402009-09-23 22:26:29 -0600374 /* Try MSI-X with one vector per queue. */
375 err = vp_try_to_find_vqs(vdev, nvqs, vqs, callbacks, names, true, true);
Michael S. Tsirkine969fed2009-07-26 15:48:08 +0300376 if (!err)
377 return 0;
Rusty Russellf68d2402009-09-23 22:26:29 -0600378 /* Fallback: MSI-X with one vector for config, one shared for queues. */
Michael S. Tsirkine969fed2009-07-26 15:48:08 +0300379 err = vp_try_to_find_vqs(vdev, nvqs, vqs, callbacks, names,
Rusty Russellf68d2402009-09-23 22:26:29 -0600380 true, false);
Michael S. Tsirkine969fed2009-07-26 15:48:08 +0300381 if (!err)
382 return 0;
383 /* Finally fall back to regular interrupts. */
Rusty Russellf68d2402009-09-23 22:26:29 -0600384 return vp_try_to_find_vqs(vdev, nvqs, vqs, callbacks, names,
385 false, false);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600386}
387
Michael S. Tsirkin38eb4a22014-12-07 18:41:16 +0200388const char *vp_bus_name(struct virtio_device *vdev)
Rick Jones66846042011-11-14 14:17:08 +0000389{
390 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
391
392 return pci_name(vp_dev->pci_dev);
393}
394
Jason Wang75a0a522012-08-28 13:54:14 +0200395/* Setup the affinity for a virtqueue:
396 * - force the affinity for per vq vector
397 * - OR over all affinities for shared MSI
398 * - ignore the affinity request if we're using INTX
399 */
Michael S. Tsirkin38eb4a22014-12-07 18:41:16 +0200400int vp_set_vq_affinity(struct virtqueue *vq, int cpu)
Jason Wang75a0a522012-08-28 13:54:14 +0200401{
402 struct virtio_device *vdev = vq->vdev;
403 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
Michael S. Tsirkin3ec7a772014-12-03 17:50:50 +0200404 struct virtio_pci_vq_info *info = vp_dev->vqs[vq->index];
Jason Wang75a0a522012-08-28 13:54:14 +0200405 struct cpumask *mask;
406 unsigned int irq;
407
408 if (!vq->callback)
409 return -EINVAL;
410
411 if (vp_dev->msix_enabled) {
412 mask = vp_dev->msix_affinity_masks[info->msix_vector];
413 irq = vp_dev->msix_entries[info->msix_vector].vector;
414 if (cpu == -1)
415 irq_set_affinity_hint(irq, NULL);
416 else {
417 cpumask_set_cpu(cpu, mask);
418 irq_set_affinity_hint(irq, mask);
419 }
420 }
421 return 0;
422}
423
Michael S. Tsirkin38eb4a22014-12-07 18:41:16 +0200424void virtio_pci_release_dev(struct device *_d)
Mark McLoughlin29f9f122008-12-10 17:45:34 +0000425{
Michael S. Tsirkin72103bd2011-11-07 18:37:05 +0200426 /*
427 * No need for a release method as we allocate/free
428 * all devices together with the pci devices.
429 * Provide an empty one to avoid getting a warning from core.
430 */
Mark McLoughlin29f9f122008-12-10 17:45:34 +0000431}
432
Aaron Lu9e266ec2013-09-09 09:57:12 +0930433#ifdef CONFIG_PM_SLEEP
Amit Shahf0fe6f12011-12-22 16:58:26 +0530434static int virtio_pci_freeze(struct device *dev)
435{
436 struct pci_dev *pci_dev = to_pci_dev(dev);
437 struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
Amit Shahf0fe6f12011-12-22 16:58:26 +0530438 int ret;
439
Michael S. Tsirkinc6716ba2014-10-14 10:40:35 +1030440 ret = virtio_device_freeze(&vp_dev->vdev);
Amit Shahf0fe6f12011-12-22 16:58:26 +0530441
442 if (!ret)
443 pci_disable_device(pci_dev);
444 return ret;
445}
446
Amit Shahf0fe6f12011-12-22 16:58:26 +0530447static int virtio_pci_restore(struct device *dev)
448{
449 struct pci_dev *pci_dev = to_pci_dev(dev);
450 struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
Amit Shahf0fe6f12011-12-22 16:58:26 +0530451 int ret;
452
Amit Shah0517fdd2012-03-29 12:54:43 +0530453 ret = pci_enable_device(pci_dev);
454 if (ret)
455 return ret;
456
457 pci_set_master(pci_dev);
Michael S. Tsirkinc6716ba2014-10-14 10:40:35 +1030458 return virtio_device_restore(&vp_dev->vdev);
Amit Shahf0fe6f12011-12-22 16:58:26 +0530459}
460
Michael S. Tsirkin9a4253d2014-12-11 21:47:49 +0200461static const struct dev_pm_ops virtio_pci_pm_ops = {
Amit Shahf878d0b2012-03-29 12:58:05 +0530462 SET_SYSTEM_SLEEP_PM_OPS(virtio_pci_freeze, virtio_pci_restore)
Amit Shahd0775362011-12-22 16:58:25 +0530463};
Anthony Liguori33436602007-11-12 21:30:26 -0600464#endif
Michael S. Tsirkin9a4253d2014-12-11 21:47:49 +0200465
466
467/* Qumranet donated their vendor ID for devices 0x1000 thru 0x10FF. */
468static const struct pci_device_id virtio_pci_id_table[] = {
469 { PCI_DEVICE(0x1af4, PCI_ANY_ID) },
470 { 0 }
471};
472
473MODULE_DEVICE_TABLE(pci, virtio_pci_id_table);
474
475static int virtio_pci_probe(struct pci_dev *pci_dev,
476 const struct pci_device_id *id)
477{
478 return virtio_pci_legacy_probe(pci_dev, id);
479}
480
481static void virtio_pci_remove(struct pci_dev *pci_dev)
482{
483 virtio_pci_legacy_remove(pci_dev);
484}
485
486static struct pci_driver virtio_pci_driver = {
487 .name = "virtio-pci",
488 .id_table = virtio_pci_id_table,
489 .probe = virtio_pci_probe,
490 .remove = virtio_pci_remove,
491#ifdef CONFIG_PM_SLEEP
492 .driver.pm = &virtio_pci_pm_ops,
493#endif
494};
495
496module_pci_driver(virtio_pci_driver);
Herbert Xu5ff16112014-12-17 00:54:03 +0200497
498MODULE_AUTHOR("Anthony Liguori <aliguori@us.ibm.com>");
499MODULE_DESCRIPTION("virtio-pci");
500MODULE_LICENSE("GPL");
501MODULE_VERSION("1");