1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Virtio PCI driver - common functionality for all device versions
4 *
5 * This module allows virtio devices to be used over a virtual PCI device.
6 * This can be used with QEMU based VMMs like KVM or Xen.
7 *
8 * Copyright IBM Corp. 2007
9 * Copyright Red Hat, Inc. 2014
10 *
11 * Authors:
12 * Anthony Liguori <aliguori@us.ibm.com>
13 * Rusty Russell <rusty@rustcorp.com.au>
14 * Michael S. Tsirkin <mst@redhat.com>
15 */
16
17 #include "virtio_pci_common.h"
18
19 static bool force_legacy = false;
20
21 #if IS_ENABLED(CONFIG_VIRTIO_PCI_LEGACY)
22 module_param(force_legacy, bool, 0444);
23 MODULE_PARM_DESC(force_legacy,
24 "Force legacy mode for transitional virtio 1 devices");
25 #endif
26
27 /* disable irq handlers */
vp_disable_cbs(struct virtio_device * vdev)28 void vp_disable_cbs(struct virtio_device *vdev)
29 {
30 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
31 int i;
32
33 if (vp_dev->intx_enabled) {
34 /*
35 * The below synchronize() guarantees that any
36 * interrupt for this line arriving after
37 * synchronize_irq() has completed is guaranteed to see
38 * intx_soft_enabled == false.
39 */
40 WRITE_ONCE(vp_dev->intx_soft_enabled, false);
41 synchronize_irq(vp_dev->pci_dev->irq);
42 }
43
44 for (i = 0; i < vp_dev->msix_vectors; ++i)
45 disable_irq(pci_irq_vector(vp_dev->pci_dev, i));
46 }
47
48 /* enable irq handlers */
vp_enable_cbs(struct virtio_device * vdev)49 void vp_enable_cbs(struct virtio_device *vdev)
50 {
51 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
52 int i;
53
54 if (vp_dev->intx_enabled) {
55 disable_irq(vp_dev->pci_dev->irq);
56 /*
57 * The above disable_irq() provides TSO ordering and
58 * as such promotes the below store to store-release.
59 */
60 WRITE_ONCE(vp_dev->intx_soft_enabled, true);
61 enable_irq(vp_dev->pci_dev->irq);
62 return;
63 }
64
65 for (i = 0; i < vp_dev->msix_vectors; ++i)
66 enable_irq(pci_irq_vector(vp_dev->pci_dev, i));
67 }
68
69 /* the notify function used when creating a virt queue */
vp_notify(struct virtqueue * vq)70 bool vp_notify(struct virtqueue *vq)
71 {
72 /* we write the queue's selector into the notification register to
73 * signal the other end */
74 iowrite16(vq->index, (void __iomem *)vq->priv);
75 return true;
76 }
77
78 /* Handle a configuration change: Tell driver if it wants to know. */
vp_config_changed(int irq,void * opaque)79 static irqreturn_t vp_config_changed(int irq, void *opaque)
80 {
81 struct virtio_pci_device *vp_dev = opaque;
82
83 virtio_config_changed(&vp_dev->vdev);
84 return IRQ_HANDLED;
85 }
86
87 /* Notify all virtqueues on an interrupt. */
vp_vring_interrupt(int irq,void * opaque)88 static irqreturn_t vp_vring_interrupt(int irq, void *opaque)
89 {
90 struct virtio_pci_device *vp_dev = opaque;
91 struct virtio_pci_vq_info *info;
92 irqreturn_t ret = IRQ_NONE;
93 unsigned long flags;
94
95 spin_lock_irqsave(&vp_dev->lock, flags);
96 list_for_each_entry(info, &vp_dev->virtqueues, node) {
97 if (vring_interrupt(irq, info->vq) == IRQ_HANDLED)
98 ret = IRQ_HANDLED;
99 }
100 spin_unlock_irqrestore(&vp_dev->lock, flags);
101
102 return ret;
103 }
104
105 /* A small wrapper to also acknowledge the interrupt when it's handled.
106 * I really need an EIO hook for the vring so I can ack the interrupt once we
107 * know that we'll be handling the IRQ but before we invoke the callback since
108 * the callback may notify the host which results in the host attempting to
109 * raise an interrupt that we would then mask once we acknowledged the
110 * interrupt. */
vp_interrupt(int irq,void * opaque)111 static irqreturn_t vp_interrupt(int irq, void *opaque)
112 {
113 struct virtio_pci_device *vp_dev = opaque;
114 u8 isr;
115
116 if (!READ_ONCE(vp_dev->intx_soft_enabled))
117 return IRQ_NONE;
118
119 /* reading the ISR has the effect of also clearing it so it's very
120 * important to save off the value. */
121 isr = ioread8(vp_dev->isr);
122
123 /* It's definitely not us if the ISR was not high */
124 if (!isr)
125 return IRQ_NONE;
126
127 /* Configuration change? Tell driver if it wants to know. */
128 if (isr & VIRTIO_PCI_ISR_CONFIG)
129 vp_config_changed(irq, opaque);
130
131 return vp_vring_interrupt(irq, opaque);
132 }
133
vp_request_msix_vectors(struct virtio_device * vdev,int nvectors,bool per_vq_vectors,struct irq_affinity * desc)134 static int vp_request_msix_vectors(struct virtio_device *vdev, int nvectors,
135 bool per_vq_vectors, struct irq_affinity *desc)
136 {
137 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
138 const char *name = dev_name(&vp_dev->vdev.dev);
139 unsigned flags = PCI_IRQ_MSIX;
140 unsigned i, v;
141 int err = -ENOMEM;
142
143 vp_dev->msix_vectors = nvectors;
144
145 vp_dev->msix_names = kmalloc_array(nvectors,
146 sizeof(*vp_dev->msix_names),
147 GFP_KERNEL);
148 if (!vp_dev->msix_names)
149 goto error;
150 vp_dev->msix_affinity_masks
151 = kcalloc(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;
159
160 if (desc) {
161 flags |= PCI_IRQ_AFFINITY;
162 desc->pre_vectors++; /* virtio config vector */
163 }
164
165 err = pci_alloc_irq_vectors_affinity(vp_dev->pci_dev, nvectors,
166 nvectors, flags, desc);
167 if (err < 0)
168 goto error;
169 vp_dev->msix_enabled = 1;
170
171 /* Set the vector used for configuration */
172 v = vp_dev->msix_used_vectors;
173 snprintf(vp_dev->msix_names[v], sizeof *vp_dev->msix_names,
174 "%s-config", name);
175 err = request_irq(pci_irq_vector(vp_dev->pci_dev, v),
176 vp_config_changed, IRQF_NO_AUTOEN,
177 vp_dev->msix_names[v],
178 vp_dev);
179 if (err)
180 goto error;
181 ++vp_dev->msix_used_vectors;
182
183 v = vp_dev->config_vector(vp_dev, v);
184 /* Verify we had enough resources to assign the vector */
185 if (v == VIRTIO_MSI_NO_VECTOR) {
186 err = -EBUSY;
187 goto error;
188 }
189
190 if (!per_vq_vectors) {
191 /* Shared vector for all VQs */
192 v = vp_dev->msix_used_vectors;
193 snprintf(vp_dev->msix_names[v], sizeof *vp_dev->msix_names,
194 "%s-virtqueues", name);
195 err = request_irq(pci_irq_vector(vp_dev->pci_dev, v),
196 vp_vring_interrupt, IRQF_NO_AUTOEN,
197 vp_dev->msix_names[v],
198 vp_dev);
199 if (err)
200 goto error;
201 ++vp_dev->msix_used_vectors;
202 }
203 return 0;
204 error:
205 return err;
206 }
207
vp_setup_vq(struct virtio_device * vdev,unsigned index,void (* callback)(struct virtqueue * vq),const char * name,bool ctx,u16 msix_vec)208 static struct virtqueue *vp_setup_vq(struct virtio_device *vdev, unsigned index,
209 void (*callback)(struct virtqueue *vq),
210 const char *name,
211 bool ctx,
212 u16 msix_vec)
213 {
214 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
215 struct virtio_pci_vq_info *info = kmalloc(sizeof *info, GFP_KERNEL);
216 struct virtqueue *vq;
217 unsigned long flags;
218
219 /* fill out our structure that represents an active queue */
220 if (!info)
221 return ERR_PTR(-ENOMEM);
222
223 vq = vp_dev->setup_vq(vp_dev, info, index, callback, name, ctx,
224 msix_vec);
225 if (IS_ERR(vq))
226 goto out_info;
227
228 info->vq = vq;
229 if (callback) {
230 spin_lock_irqsave(&vp_dev->lock, flags);
231 list_add(&info->node, &vp_dev->virtqueues);
232 spin_unlock_irqrestore(&vp_dev->lock, flags);
233 } else {
234 INIT_LIST_HEAD(&info->node);
235 }
236
237 vp_dev->vqs[index] = info;
238 return vq;
239
240 out_info:
241 kfree(info);
242 return vq;
243 }
244
vp_del_vq(struct virtqueue * vq)245 static void vp_del_vq(struct virtqueue *vq)
246 {
247 struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
248 struct virtio_pci_vq_info *info = vp_dev->vqs[vq->index];
249 unsigned long flags;
250
251 spin_lock_irqsave(&vp_dev->lock, flags);
252 list_del(&info->node);
253 spin_unlock_irqrestore(&vp_dev->lock, flags);
254
255 vp_dev->del_vq(info);
256 kfree(info);
257 }
258
259 /* the config->del_vqs() implementation */
vp_del_vqs(struct virtio_device * vdev)260 void vp_del_vqs(struct virtio_device *vdev)
261 {
262 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
263 struct virtqueue *vq, *n;
264 int i;
265
266 list_for_each_entry_safe(vq, n, &vdev->vqs, list) {
267 if (vp_dev->per_vq_vectors) {
268 int v = vp_dev->vqs[vq->index]->msix_vector;
269
270 if (v != VIRTIO_MSI_NO_VECTOR) {
271 int irq = pci_irq_vector(vp_dev->pci_dev, v);
272
273 irq_set_affinity_hint(irq, NULL);
274 free_irq(irq, vq);
275 }
276 }
277 vp_del_vq(vq);
278 }
279 vp_dev->per_vq_vectors = false;
280
281 if (vp_dev->intx_enabled) {
282 free_irq(vp_dev->pci_dev->irq, vp_dev);
283 vp_dev->intx_enabled = 0;
284 }
285
286 for (i = 0; i < vp_dev->msix_used_vectors; ++i)
287 free_irq(pci_irq_vector(vp_dev->pci_dev, i), vp_dev);
288
289 if (vp_dev->msix_affinity_masks) {
290 for (i = 0; i < vp_dev->msix_vectors; i++)
291 if (vp_dev->msix_affinity_masks[i])
292 free_cpumask_var(vp_dev->msix_affinity_masks[i]);
293 }
294
295 if (vp_dev->msix_enabled) {
296 /* Disable the vector used for configuration */
297 vp_dev->config_vector(vp_dev, VIRTIO_MSI_NO_VECTOR);
298
299 pci_free_irq_vectors(vp_dev->pci_dev);
300 vp_dev->msix_enabled = 0;
301 }
302
303 vp_dev->msix_vectors = 0;
304 vp_dev->msix_used_vectors = 0;
305 kfree(vp_dev->msix_names);
306 vp_dev->msix_names = NULL;
307 kfree(vp_dev->msix_affinity_masks);
308 vp_dev->msix_affinity_masks = NULL;
309 kfree(vp_dev->vqs);
310 vp_dev->vqs = NULL;
311 }
312
vp_find_vqs_msix(struct virtio_device * vdev,unsigned nvqs,struct virtqueue * vqs[],vq_callback_t * callbacks[],const char * const names[],bool per_vq_vectors,const bool * ctx,struct irq_affinity * desc)313 static int vp_find_vqs_msix(struct virtio_device *vdev, unsigned nvqs,
314 struct virtqueue *vqs[], vq_callback_t *callbacks[],
315 const char * const names[], bool per_vq_vectors,
316 const bool *ctx,
317 struct irq_affinity *desc)
318 {
319 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
320 u16 msix_vec;
321 int i, err, nvectors, allocated_vectors, queue_idx = 0;
322
323 vp_dev->vqs = kcalloc(nvqs, sizeof(*vp_dev->vqs), GFP_KERNEL);
324 if (!vp_dev->vqs)
325 return -ENOMEM;
326
327 if (per_vq_vectors) {
328 /* Best option: one for change interrupt, one per vq. */
329 nvectors = 1;
330 for (i = 0; i < nvqs; ++i)
331 if (names[i] && callbacks[i])
332 ++nvectors;
333 } else {
334 /* Second best: one for change, shared for all vqs. */
335 nvectors = 2;
336 }
337
338 err = vp_request_msix_vectors(vdev, nvectors, per_vq_vectors,
339 per_vq_vectors ? desc : NULL);
340 if (err)
341 goto error_find;
342
343 vp_dev->per_vq_vectors = per_vq_vectors;
344 allocated_vectors = vp_dev->msix_used_vectors;
345 for (i = 0; i < nvqs; ++i) {
346 if (!names[i]) {
347 vqs[i] = NULL;
348 continue;
349 }
350
351 if (!callbacks[i])
352 msix_vec = VIRTIO_MSI_NO_VECTOR;
353 else if (vp_dev->per_vq_vectors)
354 msix_vec = allocated_vectors++;
355 else
356 msix_vec = VP_MSIX_VQ_VECTOR;
357 vqs[i] = vp_setup_vq(vdev, queue_idx++, callbacks[i], names[i],
358 ctx ? ctx[i] : false,
359 msix_vec);
360 if (IS_ERR(vqs[i])) {
361 err = PTR_ERR(vqs[i]);
362 goto error_find;
363 }
364
365 if (!vp_dev->per_vq_vectors || msix_vec == VIRTIO_MSI_NO_VECTOR)
366 continue;
367
368 /* allocate per-vq irq if available and necessary */
369 snprintf(vp_dev->msix_names[msix_vec],
370 sizeof *vp_dev->msix_names,
371 "%s-%s",
372 dev_name(&vp_dev->vdev.dev), names[i]);
373 err = request_irq(pci_irq_vector(vp_dev->pci_dev, msix_vec),
374 vring_interrupt, IRQF_NO_AUTOEN,
375 vp_dev->msix_names[msix_vec],
376 vqs[i]);
377 if (err)
378 goto error_find;
379 }
380 return 0;
381
382 error_find:
383 vp_del_vqs(vdev);
384 return err;
385 }
386
vp_find_vqs_intx(struct virtio_device * vdev,unsigned nvqs,struct virtqueue * vqs[],vq_callback_t * callbacks[],const char * const names[],const bool * ctx)387 static int vp_find_vqs_intx(struct virtio_device *vdev, unsigned nvqs,
388 struct virtqueue *vqs[], vq_callback_t *callbacks[],
389 const char * const names[], const bool *ctx)
390 {
391 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
392 int i, err, queue_idx = 0;
393
394 vp_dev->vqs = kcalloc(nvqs, sizeof(*vp_dev->vqs), GFP_KERNEL);
395 if (!vp_dev->vqs)
396 return -ENOMEM;
397
398 err = request_irq(vp_dev->pci_dev->irq, vp_interrupt, IRQF_SHARED,
399 dev_name(&vdev->dev), vp_dev);
400 if (err)
401 goto out_del_vqs;
402
403 vp_dev->intx_enabled = 1;
404 vp_dev->per_vq_vectors = false;
405 for (i = 0; i < nvqs; ++i) {
406 if (!names[i]) {
407 vqs[i] = NULL;
408 continue;
409 }
410 vqs[i] = vp_setup_vq(vdev, queue_idx++, callbacks[i], names[i],
411 ctx ? ctx[i] : false,
412 VIRTIO_MSI_NO_VECTOR);
413 if (IS_ERR(vqs[i])) {
414 err = PTR_ERR(vqs[i]);
415 goto out_del_vqs;
416 }
417 }
418
419 return 0;
420 out_del_vqs:
421 vp_del_vqs(vdev);
422 return err;
423 }
424
425 /* the config->find_vqs() implementation */
vp_find_vqs(struct virtio_device * vdev,unsigned nvqs,struct virtqueue * vqs[],vq_callback_t * callbacks[],const char * const names[],const bool * ctx,struct irq_affinity * desc)426 int vp_find_vqs(struct virtio_device *vdev, unsigned nvqs,
427 struct virtqueue *vqs[], vq_callback_t *callbacks[],
428 const char * const names[], const bool *ctx,
429 struct irq_affinity *desc)
430 {
431 int err;
432
433 /* Try MSI-X with one vector per queue. */
434 err = vp_find_vqs_msix(vdev, nvqs, vqs, callbacks, names, true, ctx, desc);
435 if (!err)
436 return 0;
437 /* Fallback: MSI-X with one vector for config, one shared for queues. */
438 err = vp_find_vqs_msix(vdev, nvqs, vqs, callbacks, names, false, ctx, desc);
439 if (!err)
440 return 0;
441 /* Finally fall back to regular interrupts. */
442 return vp_find_vqs_intx(vdev, nvqs, vqs, callbacks, names, ctx);
443 }
444
vp_bus_name(struct virtio_device * vdev)445 const char *vp_bus_name(struct virtio_device *vdev)
446 {
447 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
448
449 return pci_name(vp_dev->pci_dev);
450 }
451
452 /* Setup the affinity for a virtqueue:
453 * - force the affinity for per vq vector
454 * - OR over all affinities for shared MSI
455 * - ignore the affinity request if we're using INTX
456 */
vp_set_vq_affinity(struct virtqueue * vq,const struct cpumask * cpu_mask)457 int vp_set_vq_affinity(struct virtqueue *vq, const struct cpumask *cpu_mask)
458 {
459 struct virtio_device *vdev = vq->vdev;
460 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
461 struct virtio_pci_vq_info *info = vp_dev->vqs[vq->index];
462 struct cpumask *mask;
463 unsigned int irq;
464
465 if (!vq->callback)
466 return -EINVAL;
467
468 if (vp_dev->msix_enabled) {
469 mask = vp_dev->msix_affinity_masks[info->msix_vector];
470 irq = pci_irq_vector(vp_dev->pci_dev, info->msix_vector);
471 if (!cpu_mask)
472 irq_set_affinity_hint(irq, NULL);
473 else {
474 cpumask_copy(mask, cpu_mask);
475 irq_set_affinity_hint(irq, mask);
476 }
477 }
478 return 0;
479 }
480
vp_get_vq_affinity(struct virtio_device * vdev,int index)481 const struct cpumask *vp_get_vq_affinity(struct virtio_device *vdev, int index)
482 {
483 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
484
485 if (!vp_dev->per_vq_vectors ||
486 vp_dev->vqs[index]->msix_vector == VIRTIO_MSI_NO_VECTOR)
487 return NULL;
488
489 return pci_irq_get_affinity(vp_dev->pci_dev,
490 vp_dev->vqs[index]->msix_vector);
491 }
492
493 #ifdef CONFIG_PM_SLEEP
virtio_pci_freeze(struct device * dev)494 static int virtio_pci_freeze(struct device *dev)
495 {
496 struct pci_dev *pci_dev = to_pci_dev(dev);
497 struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
498 int ret;
499
500 ret = virtio_device_freeze(&vp_dev->vdev);
501
502 if (!ret)
503 pci_disable_device(pci_dev);
504 return ret;
505 }
506
virtio_pci_restore(struct device * dev)507 static int virtio_pci_restore(struct device *dev)
508 {
509 struct pci_dev *pci_dev = to_pci_dev(dev);
510 struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
511 int ret;
512
513 ret = pci_enable_device(pci_dev);
514 if (ret)
515 return ret;
516
517 pci_set_master(pci_dev);
518 return virtio_device_restore(&vp_dev->vdev);
519 }
520
521 static const struct dev_pm_ops virtio_pci_pm_ops = {
522 SET_SYSTEM_SLEEP_PM_OPS(virtio_pci_freeze, virtio_pci_restore)
523 };
524 #endif
525
526
527 /* Qumranet donated their vendor ID for devices 0x1000 thru 0x10FF. */
528 static const struct pci_device_id virtio_pci_id_table[] = {
529 { PCI_DEVICE(PCI_VENDOR_ID_REDHAT_QUMRANET, PCI_ANY_ID) },
530 { 0 }
531 };
532
533 MODULE_DEVICE_TABLE(pci, virtio_pci_id_table);
534
virtio_pci_release_dev(struct device * _d)535 static void virtio_pci_release_dev(struct device *_d)
536 {
537 struct virtio_device *vdev = dev_to_virtio(_d);
538 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
539
540 /* As struct device is a kobject, it's not safe to
541 * free the memory (including the reference counter itself)
542 * until it's release callback. */
543 kfree(vp_dev);
544 }
545
virtio_pci_probe(struct pci_dev * pci_dev,const struct pci_device_id * id)546 static int virtio_pci_probe(struct pci_dev *pci_dev,
547 const struct pci_device_id *id)
548 {
549 struct virtio_pci_device *vp_dev, *reg_dev = NULL;
550 int rc;
551
552 /* allocate our structure and fill it out */
553 vp_dev = kzalloc(sizeof(struct virtio_pci_device), GFP_KERNEL);
554 if (!vp_dev)
555 return -ENOMEM;
556
557 pci_set_drvdata(pci_dev, vp_dev);
558 vp_dev->vdev.dev.parent = &pci_dev->dev;
559 vp_dev->vdev.dev.release = virtio_pci_release_dev;
560 vp_dev->pci_dev = pci_dev;
561 INIT_LIST_HEAD(&vp_dev->virtqueues);
562 spin_lock_init(&vp_dev->lock);
563
564 /* enable the device */
565 rc = pci_enable_device(pci_dev);
566 if (rc)
567 goto err_enable_device;
568
569 if (force_legacy) {
570 rc = virtio_pci_legacy_probe(vp_dev);
571 /* Also try modern mode if we can't map BAR0 (no IO space). */
572 if (rc == -ENODEV || rc == -ENOMEM)
573 rc = virtio_pci_modern_probe(vp_dev);
574 if (rc)
575 goto err_probe;
576 } else {
577 rc = virtio_pci_modern_probe(vp_dev);
578 if (rc == -ENODEV)
579 rc = virtio_pci_legacy_probe(vp_dev);
580 if (rc)
581 goto err_probe;
582 }
583
584 pci_set_master(pci_dev);
585
586 vp_dev->is_legacy = vp_dev->ldev.ioaddr ? true : false;
587
588 rc = register_virtio_device(&vp_dev->vdev);
589 reg_dev = vp_dev;
590 if (rc)
591 goto err_register;
592
593 return 0;
594
595 err_register:
596 if (vp_dev->is_legacy)
597 virtio_pci_legacy_remove(vp_dev);
598 else
599 virtio_pci_modern_remove(vp_dev);
600 err_probe:
601 pci_disable_device(pci_dev);
602 err_enable_device:
603 if (reg_dev)
604 put_device(&vp_dev->vdev.dev);
605 else
606 kfree(vp_dev);
607 return rc;
608 }
609
virtio_pci_remove(struct pci_dev * pci_dev)610 static void virtio_pci_remove(struct pci_dev *pci_dev)
611 {
612 struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
613 struct device *dev = get_device(&vp_dev->vdev.dev);
614
615 /*
616 * Device is marked broken on surprise removal so that virtio upper
617 * layers can abort any ongoing operation.
618 */
619 if (!pci_device_is_present(pci_dev))
620 virtio_break_device(&vp_dev->vdev);
621
622 pci_disable_sriov(pci_dev);
623
624 unregister_virtio_device(&vp_dev->vdev);
625
626 if (vp_dev->is_legacy)
627 virtio_pci_legacy_remove(vp_dev);
628 else
629 virtio_pci_modern_remove(vp_dev);
630
631 pci_disable_device(pci_dev);
632 put_device(dev);
633 }
634
virtio_pci_sriov_configure(struct pci_dev * pci_dev,int num_vfs)635 static int virtio_pci_sriov_configure(struct pci_dev *pci_dev, int num_vfs)
636 {
637 struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
638 struct virtio_device *vdev = &vp_dev->vdev;
639 int ret;
640
641 if (!(vdev->config->get_status(vdev) & VIRTIO_CONFIG_S_DRIVER_OK))
642 return -EBUSY;
643
644 if (!__virtio_test_bit(vdev, VIRTIO_F_SR_IOV))
645 return -EINVAL;
646
647 if (pci_vfs_assigned(pci_dev))
648 return -EPERM;
649
650 if (num_vfs == 0) {
651 pci_disable_sriov(pci_dev);
652 return 0;
653 }
654
655 ret = pci_enable_sriov(pci_dev, num_vfs);
656 if (ret < 0)
657 return ret;
658
659 return num_vfs;
660 }
661
662 static struct pci_driver virtio_pci_driver = {
663 .name = "virtio-pci",
664 .id_table = virtio_pci_id_table,
665 .probe = virtio_pci_probe,
666 .remove = virtio_pci_remove,
667 #ifdef CONFIG_PM_SLEEP
668 .driver.pm = &virtio_pci_pm_ops,
669 #endif
670 .sriov_configure = virtio_pci_sriov_configure,
671 };
672
673 module_pci_driver(virtio_pci_driver);
674
675 MODULE_AUTHOR("Anthony Liguori <aliguori@us.ibm.com>");
676 MODULE_DESCRIPTION("virtio-pci");
677 MODULE_LICENSE("GPL");
678 MODULE_VERSION("1");
679