Thomas Gleixner | f33f5fe | 2019-05-22 09:51:24 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 2 | /* |
Michael S. Tsirkin | a90fdce | 2014-12-08 12:31:02 +0200 | [diff] [blame] | 3 | * Virtio PCI driver - common functionality for all device versions |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 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 |
Michael S. Tsirkin | a90fdce | 2014-12-08 12:31:02 +0200 | [diff] [blame] | 9 | * Copyright Red Hat, Inc. 2014 |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 10 | * |
| 11 | * Authors: |
| 12 | * Anthony Liguori <aliguori@us.ibm.com> |
Michael S. Tsirkin | a90fdce | 2014-12-08 12:31:02 +0200 | [diff] [blame] | 13 | * Rusty Russell <rusty@rustcorp.com.au> |
| 14 | * Michael S. Tsirkin <mst@redhat.com> |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 15 | */ |
| 16 | |
Michael S. Tsirkin | 5f4c976 | 2014-12-08 16:39:45 +0200 | [diff] [blame] | 17 | #include "virtio_pci_common.h" |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 18 | |
Michael S. Tsirkin | ac399d8 | 2015-01-15 17:54:13 +0200 | [diff] [blame] | 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 | |
Jason Wang | 9e35276 | 2021-10-19 15:01:46 +0800 | [diff] [blame] | 27 | /* disable irq handlers */ |
| 28 | void vp_disable_cbs(struct virtio_device *vdev) |
Michael S. Tsirkin | e6af578 | 2011-11-17 17:41:15 +0200 | [diff] [blame] | 29 | { |
| 30 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
| 31 | int i; |
| 32 | |
Jason Wang | 080cd7c | 2021-10-19 15:01:47 +0800 | [diff] [blame] | 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); |
Michael S. Tsirkin | 0b0f9dc | 2017-04-04 21:15:41 +0300 | [diff] [blame] | 41 | synchronize_irq(vp_dev->pci_dev->irq); |
Jason Wang | 080cd7c | 2021-10-19 15:01:47 +0800 | [diff] [blame] | 42 | } |
Michael S. Tsirkin | 0b0f9dc | 2017-04-04 21:15:41 +0300 | [diff] [blame] | 43 | |
| 44 | for (i = 0; i < vp_dev->msix_vectors; ++i) |
Jason Wang | 9e35276 | 2021-10-19 15:01:46 +0800 | [diff] [blame] | 45 | disable_irq(pci_irq_vector(vp_dev->pci_dev, i)); |
| 46 | } |
| 47 | |
| 48 | /* enable irq handlers */ |
| 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 | |
Jason Wang | 080cd7c | 2021-10-19 15:01:47 +0800 | [diff] [blame] | 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); |
Jason Wang | 9e35276 | 2021-10-19 15:01:46 +0800 | [diff] [blame] | 62 | return; |
Jason Wang | 080cd7c | 2021-10-19 15:01:47 +0800 | [diff] [blame] | 63 | } |
Jason Wang | 9e35276 | 2021-10-19 15:01:46 +0800 | [diff] [blame] | 64 | |
| 65 | for (i = 0; i < vp_dev->msix_vectors; ++i) |
| 66 | enable_irq(pci_irq_vector(vp_dev->pci_dev, i)); |
Michael S. Tsirkin | e6af578 | 2011-11-17 17:41:15 +0200 | [diff] [blame] | 67 | } |
| 68 | |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 69 | /* the notify function used when creating a virt queue */ |
Michael S. Tsirkin | 38eb4a2 | 2014-12-07 18:41:16 +0200 | [diff] [blame] | 70 | bool vp_notify(struct virtqueue *vq) |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 71 | { |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 72 | /* we write the queue's selector into the notification register to |
| 73 | * signal the other end */ |
Michael S. Tsirkin | f30eaf4 | 2014-12-03 18:01:58 +0200 | [diff] [blame] | 74 | iowrite16(vq->index, (void __iomem *)vq->priv); |
Heinz Graalfs | 46f9c2b | 2013-10-29 09:38:50 +1030 | [diff] [blame] | 75 | return true; |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 76 | } |
| 77 | |
Michael S. Tsirkin | 77cf5246 | 2009-05-14 13:55:31 +0300 | [diff] [blame] | 78 | /* Handle a configuration change: Tell driver if it wants to know. */ |
| 79 | static irqreturn_t vp_config_changed(int irq, void *opaque) |
| 80 | { |
| 81 | struct virtio_pci_device *vp_dev = opaque; |
Michael S. Tsirkin | 77cf5246 | 2009-05-14 13:55:31 +0300 | [diff] [blame] | 82 | |
Michael S. Tsirkin | 016c98c | 2014-10-14 10:40:34 +1030 | [diff] [blame] | 83 | virtio_config_changed(&vp_dev->vdev); |
Michael S. Tsirkin | 77cf5246 | 2009-05-14 13:55:31 +0300 | [diff] [blame] | 84 | return IRQ_HANDLED; |
| 85 | } |
| 86 | |
| 87 | /* Notify all virtqueues on an interrupt. */ |
| 88 | static irqreturn_t vp_vring_interrupt(int irq, void *opaque) |
| 89 | { |
| 90 | struct virtio_pci_device *vp_dev = opaque; |
Michael S. Tsirkin | 0a9b3f4 | 2017-04-04 21:44:44 +0300 | [diff] [blame] | 91 | struct virtio_pci_vq_info *info; |
Michael S. Tsirkin | 77cf5246 | 2009-05-14 13:55:31 +0300 | [diff] [blame] | 92 | irqreturn_t ret = IRQ_NONE; |
Michael S. Tsirkin | 0a9b3f4 | 2017-04-04 21:44:44 +0300 | [diff] [blame] | 93 | unsigned long flags; |
Michael S. Tsirkin | 77cf5246 | 2009-05-14 13:55:31 +0300 | [diff] [blame] | 94 | |
Michael S. Tsirkin | 0a9b3f4 | 2017-04-04 21:44:44 +0300 | [diff] [blame] | 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) |
Michael S. Tsirkin | 77cf5246 | 2009-05-14 13:55:31 +0300 | [diff] [blame] | 98 | ret = IRQ_HANDLED; |
| 99 | } |
Michael S. Tsirkin | 0a9b3f4 | 2017-04-04 21:44:44 +0300 | [diff] [blame] | 100 | spin_unlock_irqrestore(&vp_dev->lock, flags); |
Michael S. Tsirkin | 77cf5246 | 2009-05-14 13:55:31 +0300 | [diff] [blame] | 101 | |
| 102 | return ret; |
| 103 | } |
| 104 | |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 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. */ |
| 111 | static irqreturn_t vp_interrupt(int irq, void *opaque) |
| 112 | { |
| 113 | struct virtio_pci_device *vp_dev = opaque; |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 114 | u8 isr; |
| 115 | |
Jason Wang | 080cd7c | 2021-10-19 15:01:47 +0800 | [diff] [blame] | 116 | if (!READ_ONCE(vp_dev->intx_soft_enabled)) |
| 117 | return IRQ_NONE; |
| 118 | |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 119 | /* reading the ISR has the effect of also clearing it so it's very |
| 120 | * important to save off the value. */ |
Michael S. Tsirkin | af53572 | 2014-12-02 14:35:27 +0200 | [diff] [blame] | 121 | isr = ioread8(vp_dev->isr); |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 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. */ |
Michael S. Tsirkin | 77cf5246 | 2009-05-14 13:55:31 +0300 | [diff] [blame] | 128 | if (isr & VIRTIO_PCI_ISR_CONFIG) |
| 129 | vp_config_changed(irq, opaque); |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 130 | |
Michael S. Tsirkin | 77cf5246 | 2009-05-14 13:55:31 +0300 | [diff] [blame] | 131 | return vp_vring_interrupt(irq, opaque); |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 132 | } |
| 133 | |
Michael S. Tsirkin | 0b0f9dc | 2017-04-04 21:15:41 +0300 | [diff] [blame] | 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); |
Christoph Hellwig | ba74b6f | 2017-08-24 18:07:02 +0200 | [diff] [blame] | 139 | unsigned flags = PCI_IRQ_MSIX; |
Michael S. Tsirkin | 0b0f9dc | 2017-04-04 21:15:41 +0300 | [diff] [blame] | 140 | unsigned i, v; |
| 141 | int err = -ENOMEM; |
| 142 | |
| 143 | vp_dev->msix_vectors = nvectors; |
| 144 | |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 145 | vp_dev->msix_names = kmalloc_array(nvectors, |
| 146 | sizeof(*vp_dev->msix_names), |
| 147 | GFP_KERNEL); |
Michael S. Tsirkin | 0b0f9dc | 2017-04-04 21:15:41 +0300 | [diff] [blame] | 148 | if (!vp_dev->msix_names) |
| 149 | goto error; |
| 150 | vp_dev->msix_affinity_masks |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 151 | = kcalloc(nvectors, sizeof(*vp_dev->msix_affinity_masks), |
Michael S. Tsirkin | 0b0f9dc | 2017-04-04 21:15:41 +0300 | [diff] [blame] | 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 | |
Christoph Hellwig | ba74b6f | 2017-08-24 18:07:02 +0200 | [diff] [blame] | 160 | if (desc) { |
| 161 | flags |= PCI_IRQ_AFFINITY; |
| 162 | desc->pre_vectors++; /* virtio config vector */ |
| 163 | } |
| 164 | |
Michael S. Tsirkin | 0b0f9dc | 2017-04-04 21:15:41 +0300 | [diff] [blame] | 165 | err = pci_alloc_irq_vectors_affinity(vp_dev->pci_dev, nvectors, |
Christoph Hellwig | ba74b6f | 2017-08-24 18:07:02 +0200 | [diff] [blame] | 166 | nvectors, flags, desc); |
Michael S. Tsirkin | 0b0f9dc | 2017-04-04 21:15:41 +0300 | [diff] [blame] | 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), |
Jason Wang | 9e35276 | 2021-10-19 15:01:46 +0800 | [diff] [blame] | 176 | vp_config_changed, IRQF_NO_AUTOEN, |
| 177 | vp_dev->msix_names[v], |
Michael S. Tsirkin | 0b0f9dc | 2017-04-04 21:15:41 +0300 | [diff] [blame] | 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), |
Jason Wang | 9e35276 | 2021-10-19 15:01:46 +0800 | [diff] [blame] | 196 | vp_vring_interrupt, IRQF_NO_AUTOEN, |
| 197 | vp_dev->msix_names[v], |
Michael S. Tsirkin | 0b0f9dc | 2017-04-04 21:15:41 +0300 | [diff] [blame] | 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 | |
Michael S. Tsirkin | 0a9b3f4 | 2017-04-04 21:44:44 +0300 | [diff] [blame] | 208 | static struct virtqueue *vp_setup_vq(struct virtio_device *vdev, unsigned index, |
| 209 | void (*callback)(struct virtqueue *vq), |
| 210 | const char *name, |
Michael S. Tsirkin | f94682d | 2017-03-06 18:32:29 +0200 | [diff] [blame] | 211 | bool ctx, |
Michael S. Tsirkin | 0a9b3f4 | 2017-04-04 21:44:44 +0300 | [diff] [blame] | 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 | |
Michael S. Tsirkin | f94682d | 2017-03-06 18:32:29 +0200 | [diff] [blame] | 223 | vq = vp_dev->setup_vq(vp_dev, info, index, callback, name, ctx, |
Michael S. Tsirkin | 0a9b3f4 | 2017-04-04 21:44:44 +0300 | [diff] [blame] | 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 | |
| 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 | |
Michael S. Tsirkin | 0b0f9dc | 2017-04-04 21:15:41 +0300 | [diff] [blame] | 259 | /* the config->del_vqs() implementation */ |
| 260 | void vp_del_vqs(struct virtio_device *vdev) |
Michael S. Tsirkin | d2a7ddd | 2009-06-12 22:16:36 -0600 | [diff] [blame] | 261 | { |
Michael S. Tsirkin | e969fed | 2009-07-26 15:48:08 +0300 | [diff] [blame] | 262 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
Michael S. Tsirkin | d2a7ddd | 2009-06-12 22:16:36 -0600 | [diff] [blame] | 263 | struct virtqueue *vq, *n; |
Michael S. Tsirkin | 0b0f9dc | 2017-04-04 21:15:41 +0300 | [diff] [blame] | 264 | int i; |
Michael S. Tsirkin | d2a7ddd | 2009-06-12 22:16:36 -0600 | [diff] [blame] | 265 | |
Michael S. Tsirkin | e969fed | 2009-07-26 15:48:08 +0300 | [diff] [blame] | 266 | list_for_each_entry_safe(vq, n, &vdev->vqs, list) { |
Michael S. Tsirkin | 0a9b3f4 | 2017-04-04 21:44:44 +0300 | [diff] [blame] | 267 | if (vp_dev->per_vq_vectors) { |
| 268 | int v = vp_dev->vqs[vq->index]->msix_vector; |
Christoph Hellwig | fa3a327 | 2016-11-17 11:43:13 +0100 | [diff] [blame] | 269 | |
Marc Zyngier | 2f8dc3a | 2017-03-08 08:09:27 +0000 | [diff] [blame] | 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 | } |
Christoph Hellwig | fa3a327 | 2016-11-17 11:43:13 +0100 | [diff] [blame] | 276 | } |
Michael S. Tsirkin | 0a9b3f4 | 2017-04-04 21:44:44 +0300 | [diff] [blame] | 277 | vp_del_vq(vq); |
Michael S. Tsirkin | e969fed | 2009-07-26 15:48:08 +0300 | [diff] [blame] | 278 | } |
Michael S. Tsirkin | 0a9b3f4 | 2017-04-04 21:44:44 +0300 | [diff] [blame] | 279 | vp_dev->per_vq_vectors = false; |
Michael S. Tsirkin | 82af8ce | 2009-05-14 13:55:41 +0300 | [diff] [blame] | 280 | |
Michael S. Tsirkin | 0b0f9dc | 2017-04-04 21:15:41 +0300 | [diff] [blame] | 281 | if (vp_dev->intx_enabled) { |
| 282 | free_irq(vp_dev->pci_dev->irq, vp_dev); |
| 283 | vp_dev->intx_enabled = 0; |
| 284 | } |
Christoph Hellwig | 66f2f55 | 2016-11-17 11:43:15 +0100 | [diff] [blame] | 285 | |
Michael S. Tsirkin | 0b0f9dc | 2017-04-04 21:15:41 +0300 | [diff] [blame] | 286 | for (i = 0; i < vp_dev->msix_used_vectors; ++i) |
| 287 | free_irq(pci_irq_vector(vp_dev->pci_dev, i), vp_dev); |
Christoph Hellwig | 66f2f55 | 2016-11-17 11:43:15 +0100 | [diff] [blame] | 288 | |
Longpeng | 6a8aae6 | 2019-03-09 15:17:40 +0800 | [diff] [blame] | 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 | } |
Christoph Hellwig | 07ec5148 | 2017-02-05 18:15:19 +0100 | [diff] [blame] | 294 | |
Michael S. Tsirkin | 0b0f9dc | 2017-04-04 21:15:41 +0300 | [diff] [blame] | 295 | if (vp_dev->msix_enabled) { |
Christoph Hellwig | 66f2f55 | 2016-11-17 11:43:15 +0100 | [diff] [blame] | 296 | /* Disable the vector used for configuration */ |
| 297 | vp_dev->config_vector(vp_dev, VIRTIO_MSI_NO_VECTOR); |
| 298 | |
Michael S. Tsirkin | 0b0f9dc | 2017-04-04 21:15:41 +0300 | [diff] [blame] | 299 | pci_free_irq_vectors(vp_dev->pci_dev); |
| 300 | vp_dev->msix_enabled = 0; |
Christoph Hellwig | 66f2f55 | 2016-11-17 11:43:15 +0100 | [diff] [blame] | 301 | } |
| 302 | |
Michael S. Tsirkin | 0b0f9dc | 2017-04-04 21:15:41 +0300 | [diff] [blame] | 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; |
Michael S. Tsirkin | 0a9b3f4 | 2017-04-04 21:44:44 +0300 | [diff] [blame] | 309 | kfree(vp_dev->vqs); |
| 310 | vp_dev->vqs = NULL; |
Michael S. Tsirkin | d2a7ddd | 2009-06-12 22:16:36 -0600 | [diff] [blame] | 311 | } |
| 312 | |
Christoph Hellwig | a3cbec6 | 2016-11-17 11:43:16 +0100 | [diff] [blame] | 313 | static int vp_find_vqs_msix(struct virtio_device *vdev, unsigned nvqs, |
Christoph Hellwig | 52a6151 | 2017-02-05 18:15:21 +0100 | [diff] [blame] | 314 | struct virtqueue *vqs[], vq_callback_t *callbacks[], |
Michael S. Tsirkin | bf951b1 | 2017-04-04 21:08:54 +0300 | [diff] [blame] | 315 | const char * const names[], bool per_vq_vectors, |
Michael S. Tsirkin | f94682d | 2017-03-06 18:32:29 +0200 | [diff] [blame] | 316 | const bool *ctx, |
Michael S. Tsirkin | bf951b1 | 2017-04-04 21:08:54 +0300 | [diff] [blame] | 317 | struct irq_affinity *desc) |
Michael S. Tsirkin | e969fed | 2009-07-26 15:48:08 +0300 | [diff] [blame] | 318 | { |
| 319 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
Rusty Russell | f68d240 | 2009-09-23 22:26:29 -0600 | [diff] [blame] | 320 | u16 msix_vec; |
Wei Wang | ddbeac0 | 2018-12-28 10:26:25 +0800 | [diff] [blame] | 321 | int i, err, nvectors, allocated_vectors, queue_idx = 0; |
Michael S. Tsirkin | e969fed | 2009-07-26 15:48:08 +0300 | [diff] [blame] | 322 | |
Michael S. Tsirkin | 0a9b3f4 | 2017-04-04 21:44:44 +0300 | [diff] [blame] | 323 | vp_dev->vqs = kcalloc(nvqs, sizeof(*vp_dev->vqs), GFP_KERNEL); |
| 324 | if (!vp_dev->vqs) |
| 325 | return -ENOMEM; |
| 326 | |
Michael S. Tsirkin | bf951b1 | 2017-04-04 21:08:54 +0300 | [diff] [blame] | 327 | if (per_vq_vectors) { |
Michael S. Tsirkin | 0b0f9dc | 2017-04-04 21:15:41 +0300 | [diff] [blame] | 328 | /* Best option: one for change interrupt, one per vq. */ |
| 329 | nvectors = 1; |
| 330 | for (i = 0; i < nvqs; ++i) |
Daniel Verkamp | 303090b | 2020-01-03 10:40:45 -0800 | [diff] [blame] | 331 | if (names[i] && callbacks[i]) |
Michael S. Tsirkin | 0b0f9dc | 2017-04-04 21:15:41 +0300 | [diff] [blame] | 332 | ++nvectors; |
Michael S. Tsirkin | bf951b1 | 2017-04-04 21:08:54 +0300 | [diff] [blame] | 333 | } else { |
Michael S. Tsirkin | 0b0f9dc | 2017-04-04 21:15:41 +0300 | [diff] [blame] | 334 | /* Second best: one for change, shared for all vqs. */ |
| 335 | nvectors = 2; |
Rusty Russell | f68d240 | 2009-09-23 22:26:29 -0600 | [diff] [blame] | 336 | } |
Michael S. Tsirkin | e969fed | 2009-07-26 15:48:08 +0300 | [diff] [blame] | 337 | |
Michael S. Tsirkin | 0b0f9dc | 2017-04-04 21:15:41 +0300 | [diff] [blame] | 338 | err = vp_request_msix_vectors(vdev, nvectors, per_vq_vectors, |
| 339 | per_vq_vectors ? desc : NULL); |
Christoph Hellwig | a3cbec6 | 2016-11-17 11:43:16 +0100 | [diff] [blame] | 340 | if (err) |
Michael S. Tsirkin | 0b0f9dc | 2017-04-04 21:15:41 +0300 | [diff] [blame] | 341 | goto error_find; |
Christoph Hellwig | a3cbec6 | 2016-11-17 11:43:16 +0100 | [diff] [blame] | 342 | |
Michael S. Tsirkin | 0a9b3f4 | 2017-04-04 21:44:44 +0300 | [diff] [blame] | 343 | vp_dev->per_vq_vectors = per_vq_vectors; |
Michael S. Tsirkin | 0b0f9dc | 2017-04-04 21:15:41 +0300 | [diff] [blame] | 344 | allocated_vectors = vp_dev->msix_used_vectors; |
Michael S. Tsirkin | e969fed | 2009-07-26 15:48:08 +0300 | [diff] [blame] | 345 | for (i = 0; i < nvqs; ++i) { |
Michael S. Tsirkin | 6457f12 | 2012-09-05 21:47:45 +0300 | [diff] [blame] | 346 | if (!names[i]) { |
| 347 | vqs[i] = NULL; |
| 348 | continue; |
Christoph Hellwig | a3cbec6 | 2016-11-17 11:43:16 +0100 | [diff] [blame] | 349 | } |
| 350 | |
Michael S. Tsirkin | 0b0f9dc | 2017-04-04 21:15:41 +0300 | [diff] [blame] | 351 | if (!callbacks[i]) |
Christoph Hellwig | 07ec5148 | 2017-02-05 18:15:19 +0100 | [diff] [blame] | 352 | msix_vec = VIRTIO_MSI_NO_VECTOR; |
Michael S. Tsirkin | 0a9b3f4 | 2017-04-04 21:44:44 +0300 | [diff] [blame] | 353 | else if (vp_dev->per_vq_vectors) |
Michael S. Tsirkin | 0b0f9dc | 2017-04-04 21:15:41 +0300 | [diff] [blame] | 354 | msix_vec = allocated_vectors++; |
| 355 | else |
| 356 | msix_vec = VP_MSIX_VQ_VECTOR; |
Wei Wang | ddbeac0 | 2018-12-28 10:26:25 +0800 | [diff] [blame] | 357 | vqs[i] = vp_setup_vq(vdev, queue_idx++, callbacks[i], names[i], |
Michael S. Tsirkin | f94682d | 2017-03-06 18:32:29 +0200 | [diff] [blame] | 358 | ctx ? ctx[i] : false, |
Michael S. Tsirkin | 0a9b3f4 | 2017-04-04 21:44:44 +0300 | [diff] [blame] | 359 | msix_vec); |
Michael S. Tsirkin | e969fed | 2009-07-26 15:48:08 +0300 | [diff] [blame] | 360 | if (IS_ERR(vqs[i])) { |
| 361 | err = PTR_ERR(vqs[i]); |
Michael S. Tsirkin | 0b0f9dc | 2017-04-04 21:15:41 +0300 | [diff] [blame] | 362 | goto error_find; |
Michael S. Tsirkin | e969fed | 2009-07-26 15:48:08 +0300 | [diff] [blame] | 363 | } |
Michael S. Tsirkin | 0b22bd0 | 2009-10-22 15:06:06 +0200 | [diff] [blame] | 364 | |
Michael S. Tsirkin | 0a9b3f4 | 2017-04-04 21:44:44 +0300 | [diff] [blame] | 365 | if (!vp_dev->per_vq_vectors || msix_vec == VIRTIO_MSI_NO_VECTOR) |
Michael S. Tsirkin | 0b0f9dc | 2017-04-04 21:15:41 +0300 | [diff] [blame] | 366 | continue; |
| 367 | |
Michael S. Tsirkin | 0b0f9dc | 2017-04-04 21:15:41 +0300 | [diff] [blame] | 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", |
Michael S. Tsirkin | 0b22bd0 | 2009-10-22 15:06:06 +0200 | [diff] [blame] | 372 | dev_name(&vp_dev->vdev.dev), names[i]); |
Christoph Hellwig | fa3a327 | 2016-11-17 11:43:13 +0100 | [diff] [blame] | 373 | err = request_irq(pci_irq_vector(vp_dev->pci_dev, msix_vec), |
Jason Wang | 9e35276 | 2021-10-19 15:01:46 +0800 | [diff] [blame] | 374 | vring_interrupt, IRQF_NO_AUTOEN, |
Michael S. Tsirkin | 0b0f9dc | 2017-04-04 21:15:41 +0300 | [diff] [blame] | 375 | vp_dev->msix_names[msix_vec], |
| 376 | vqs[i]); |
Michael S. Tsirkin | 0a9b3f4 | 2017-04-04 21:44:44 +0300 | [diff] [blame] | 377 | if (err) |
Michael S. Tsirkin | 0b0f9dc | 2017-04-04 21:15:41 +0300 | [diff] [blame] | 378 | goto error_find; |
Michael S. Tsirkin | e969fed | 2009-07-26 15:48:08 +0300 | [diff] [blame] | 379 | } |
| 380 | return 0; |
| 381 | |
Michael S. Tsirkin | 0b0f9dc | 2017-04-04 21:15:41 +0300 | [diff] [blame] | 382 | error_find: |
| 383 | vp_del_vqs(vdev); |
Michael S. Tsirkin | e969fed | 2009-07-26 15:48:08 +0300 | [diff] [blame] | 384 | return err; |
| 385 | } |
| 386 | |
Christoph Hellwig | a3cbec6 | 2016-11-17 11:43:16 +0100 | [diff] [blame] | 387 | static int vp_find_vqs_intx(struct virtio_device *vdev, unsigned nvqs, |
| 388 | struct virtqueue *vqs[], vq_callback_t *callbacks[], |
Michael S. Tsirkin | f94682d | 2017-03-06 18:32:29 +0200 | [diff] [blame] | 389 | const char * const names[], const bool *ctx) |
Christoph Hellwig | a3cbec6 | 2016-11-17 11:43:16 +0100 | [diff] [blame] | 390 | { |
| 391 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
Wei Wang | ddbeac0 | 2018-12-28 10:26:25 +0800 | [diff] [blame] | 392 | int i, err, queue_idx = 0; |
Christoph Hellwig | a3cbec6 | 2016-11-17 11:43:16 +0100 | [diff] [blame] | 393 | |
Michael S. Tsirkin | 0a9b3f4 | 2017-04-04 21:44:44 +0300 | [diff] [blame] | 394 | vp_dev->vqs = kcalloc(nvqs, sizeof(*vp_dev->vqs), GFP_KERNEL); |
| 395 | if (!vp_dev->vqs) |
| 396 | return -ENOMEM; |
| 397 | |
Christoph Hellwig | a3cbec6 | 2016-11-17 11:43:16 +0100 | [diff] [blame] | 398 | err = request_irq(vp_dev->pci_dev->irq, vp_interrupt, IRQF_SHARED, |
| 399 | dev_name(&vdev->dev), vp_dev); |
| 400 | if (err) |
Michael S. Tsirkin | 0b0f9dc | 2017-04-04 21:15:41 +0300 | [diff] [blame] | 401 | goto out_del_vqs; |
Christoph Hellwig | a3cbec6 | 2016-11-17 11:43:16 +0100 | [diff] [blame] | 402 | |
Michael S. Tsirkin | 0b0f9dc | 2017-04-04 21:15:41 +0300 | [diff] [blame] | 403 | vp_dev->intx_enabled = 1; |
Michael S. Tsirkin | 0a9b3f4 | 2017-04-04 21:44:44 +0300 | [diff] [blame] | 404 | vp_dev->per_vq_vectors = false; |
Christoph Hellwig | a3cbec6 | 2016-11-17 11:43:16 +0100 | [diff] [blame] | 405 | for (i = 0; i < nvqs; ++i) { |
| 406 | if (!names[i]) { |
| 407 | vqs[i] = NULL; |
| 408 | continue; |
| 409 | } |
Wei Wang | ddbeac0 | 2018-12-28 10:26:25 +0800 | [diff] [blame] | 410 | vqs[i] = vp_setup_vq(vdev, queue_idx++, callbacks[i], names[i], |
Michael S. Tsirkin | f94682d | 2017-03-06 18:32:29 +0200 | [diff] [blame] | 411 | ctx ? ctx[i] : false, |
Michael S. Tsirkin | 0a9b3f4 | 2017-04-04 21:44:44 +0300 | [diff] [blame] | 412 | VIRTIO_MSI_NO_VECTOR); |
Christoph Hellwig | a3cbec6 | 2016-11-17 11:43:16 +0100 | [diff] [blame] | 413 | if (IS_ERR(vqs[i])) { |
| 414 | err = PTR_ERR(vqs[i]); |
Michael S. Tsirkin | 0b0f9dc | 2017-04-04 21:15:41 +0300 | [diff] [blame] | 415 | goto out_del_vqs; |
Christoph Hellwig | a3cbec6 | 2016-11-17 11:43:16 +0100 | [diff] [blame] | 416 | } |
| 417 | } |
| 418 | |
| 419 | return 0; |
Michael S. Tsirkin | 0b0f9dc | 2017-04-04 21:15:41 +0300 | [diff] [blame] | 420 | out_del_vqs: |
| 421 | vp_del_vqs(vdev); |
Christoph Hellwig | a3cbec6 | 2016-11-17 11:43:16 +0100 | [diff] [blame] | 422 | return err; |
| 423 | } |
| 424 | |
Michael S. Tsirkin | 82af8ce | 2009-05-14 13:55:41 +0300 | [diff] [blame] | 425 | /* the config->find_vqs() implementation */ |
Michael S. Tsirkin | 38eb4a2 | 2014-12-07 18:41:16 +0200 | [diff] [blame] | 426 | int vp_find_vqs(struct virtio_device *vdev, unsigned nvqs, |
Christoph Hellwig | fb5e31d | 2017-02-05 18:15:22 +0100 | [diff] [blame] | 427 | struct virtqueue *vqs[], vq_callback_t *callbacks[], |
Michael S. Tsirkin | f94682d | 2017-03-06 18:32:29 +0200 | [diff] [blame] | 428 | const char * const names[], const bool *ctx, |
| 429 | struct irq_affinity *desc) |
Michael S. Tsirkin | d2a7ddd | 2009-06-12 22:16:36 -0600 | [diff] [blame] | 430 | { |
Rusty Russell | f68d240 | 2009-09-23 22:26:29 -0600 | [diff] [blame] | 431 | int err; |
Michael S. Tsirkin | 82af8ce | 2009-05-14 13:55:41 +0300 | [diff] [blame] | 432 | |
Michael S. Tsirkin | bf951b1 | 2017-04-04 21:08:54 +0300 | [diff] [blame] | 433 | /* Try MSI-X with one vector per queue. */ |
Michael S. Tsirkin | f94682d | 2017-03-06 18:32:29 +0200 | [diff] [blame] | 434 | err = vp_find_vqs_msix(vdev, nvqs, vqs, callbacks, names, true, ctx, desc); |
Michael S. Tsirkin | e969fed | 2009-07-26 15:48:08 +0300 | [diff] [blame] | 435 | if (!err) |
| 436 | return 0; |
Michael S. Tsirkin | bf951b1 | 2017-04-04 21:08:54 +0300 | [diff] [blame] | 437 | /* Fallback: MSI-X with one vector for config, one shared for queues. */ |
Michael S. Tsirkin | f94682d | 2017-03-06 18:32:29 +0200 | [diff] [blame] | 438 | err = vp_find_vqs_msix(vdev, nvqs, vqs, callbacks, names, false, ctx, desc); |
Michael S. Tsirkin | bf951b1 | 2017-04-04 21:08:54 +0300 | [diff] [blame] | 439 | if (!err) |
| 440 | return 0; |
| 441 | /* Finally fall back to regular interrupts. */ |
Michael S. Tsirkin | f94682d | 2017-03-06 18:32:29 +0200 | [diff] [blame] | 442 | return vp_find_vqs_intx(vdev, nvqs, vqs, callbacks, names, ctx); |
Michael S. Tsirkin | d2a7ddd | 2009-06-12 22:16:36 -0600 | [diff] [blame] | 443 | } |
| 444 | |
Michael S. Tsirkin | 38eb4a2 | 2014-12-07 18:41:16 +0200 | [diff] [blame] | 445 | const char *vp_bus_name(struct virtio_device *vdev) |
Rick Jones | 6684604 | 2011-11-14 14:17:08 +0000 | [diff] [blame] | 446 | { |
| 447 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
| 448 | |
| 449 | return pci_name(vp_dev->pci_dev); |
| 450 | } |
| 451 | |
Jason Wang | 75a0a52 | 2012-08-28 13:54:14 +0200 | [diff] [blame] | 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 | */ |
Caleb Raitto | 19e226e | 2018-08-09 18:18:28 -0700 | [diff] [blame] | 457 | int vp_set_vq_affinity(struct virtqueue *vq, const struct cpumask *cpu_mask) |
Jason Wang | 75a0a52 | 2012-08-28 13:54:14 +0200 | [diff] [blame] | 458 | { |
| 459 | struct virtio_device *vdev = vq->vdev; |
| 460 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
Michael S. Tsirkin | 0a9b3f4 | 2017-04-04 21:44:44 +0300 | [diff] [blame] | 461 | struct virtio_pci_vq_info *info = vp_dev->vqs[vq->index]; |
| 462 | struct cpumask *mask; |
| 463 | unsigned int irq; |
Jason Wang | 75a0a52 | 2012-08-28 13:54:14 +0200 | [diff] [blame] | 464 | |
| 465 | if (!vq->callback) |
| 466 | return -EINVAL; |
| 467 | |
Michael S. Tsirkin | 2008c15 | 2017-04-04 21:09:20 +0300 | [diff] [blame] | 468 | if (vp_dev->msix_enabled) { |
Michael S. Tsirkin | 0a9b3f4 | 2017-04-04 21:44:44 +0300 | [diff] [blame] | 469 | mask = vp_dev->msix_affinity_masks[info->msix_vector]; |
| 470 | irq = pci_irq_vector(vp_dev->pci_dev, info->msix_vector); |
Caleb Raitto | 19e226e | 2018-08-09 18:18:28 -0700 | [diff] [blame] | 471 | if (!cpu_mask) |
Jason Wang | 75a0a52 | 2012-08-28 13:54:14 +0200 | [diff] [blame] | 472 | irq_set_affinity_hint(irq, NULL); |
| 473 | else { |
Caleb Raitto | 19e226e | 2018-08-09 18:18:28 -0700 | [diff] [blame] | 474 | cpumask_copy(mask, cpu_mask); |
Jason Wang | 75a0a52 | 2012-08-28 13:54:14 +0200 | [diff] [blame] | 475 | irq_set_affinity_hint(irq, mask); |
| 476 | } |
| 477 | } |
| 478 | return 0; |
| 479 | } |
| 480 | |
Christoph Hellwig | bbaba47 | 2017-02-05 18:15:23 +0100 | [diff] [blame] | 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); |
Christoph Hellwig | bbaba47 | 2017-02-05 18:15:23 +0100 | [diff] [blame] | 484 | |
Michael S. Tsirkin | 0a9b3f4 | 2017-04-04 21:44:44 +0300 | [diff] [blame] | 485 | if (!vp_dev->per_vq_vectors || |
| 486 | vp_dev->vqs[index]->msix_vector == VIRTIO_MSI_NO_VECTOR) |
Christoph Hellwig | bbaba47 | 2017-02-05 18:15:23 +0100 | [diff] [blame] | 487 | return NULL; |
| 488 | |
Michael S. Tsirkin | 0a9b3f4 | 2017-04-04 21:44:44 +0300 | [diff] [blame] | 489 | return pci_irq_get_affinity(vp_dev->pci_dev, |
| 490 | vp_dev->vqs[index]->msix_vector); |
Christoph Hellwig | bbaba47 | 2017-02-05 18:15:23 +0100 | [diff] [blame] | 491 | } |
| 492 | |
Aaron Lu | 9e266ec | 2013-09-09 09:57:12 +0930 | [diff] [blame] | 493 | #ifdef CONFIG_PM_SLEEP |
Amit Shah | f0fe6f1 | 2011-12-22 16:58:26 +0530 | [diff] [blame] | 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); |
Amit Shah | f0fe6f1 | 2011-12-22 16:58:26 +0530 | [diff] [blame] | 498 | int ret; |
| 499 | |
Michael S. Tsirkin | c6716ba | 2014-10-14 10:40:35 +1030 | [diff] [blame] | 500 | ret = virtio_device_freeze(&vp_dev->vdev); |
Amit Shah | f0fe6f1 | 2011-12-22 16:58:26 +0530 | [diff] [blame] | 501 | |
| 502 | if (!ret) |
| 503 | pci_disable_device(pci_dev); |
| 504 | return ret; |
| 505 | } |
| 506 | |
Amit Shah | f0fe6f1 | 2011-12-22 16:58:26 +0530 | [diff] [blame] | 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); |
Amit Shah | f0fe6f1 | 2011-12-22 16:58:26 +0530 | [diff] [blame] | 511 | int ret; |
| 512 | |
Amit Shah | 0517fdd | 2012-03-29 12:54:43 +0530 | [diff] [blame] | 513 | ret = pci_enable_device(pci_dev); |
| 514 | if (ret) |
| 515 | return ret; |
| 516 | |
| 517 | pci_set_master(pci_dev); |
Michael S. Tsirkin | c6716ba | 2014-10-14 10:40:35 +1030 | [diff] [blame] | 518 | return virtio_device_restore(&vp_dev->vdev); |
Amit Shah | f0fe6f1 | 2011-12-22 16:58:26 +0530 | [diff] [blame] | 519 | } |
| 520 | |
Michael S. Tsirkin | 9a4253d | 2014-12-11 21:47:49 +0200 | [diff] [blame] | 521 | static const struct dev_pm_ops virtio_pci_pm_ops = { |
Amit Shah | f878d0b | 2012-03-29 12:58:05 +0530 | [diff] [blame] | 522 | SET_SYSTEM_SLEEP_PM_OPS(virtio_pci_freeze, virtio_pci_restore) |
Amit Shah | d077536 | 2011-12-22 16:58:25 +0530 | [diff] [blame] | 523 | }; |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 524 | #endif |
Michael S. Tsirkin | 9a4253d | 2014-12-11 21:47:49 +0200 | [diff] [blame] | 525 | |
| 526 | |
| 527 | /* Qumranet donated their vendor ID for devices 0x1000 thru 0x10FF. */ |
| 528 | static const struct pci_device_id virtio_pci_id_table[] = { |
Robin H. Johnson | caf02ab | 2016-03-06 22:02:30 +0000 | [diff] [blame] | 529 | { PCI_DEVICE(PCI_VENDOR_ID_REDHAT_QUMRANET, PCI_ANY_ID) }, |
Michael S. Tsirkin | 9a4253d | 2014-12-11 21:47:49 +0200 | [diff] [blame] | 530 | { 0 } |
| 531 | }; |
| 532 | |
| 533 | MODULE_DEVICE_TABLE(pci, virtio_pci_id_table); |
| 534 | |
Michael S. Tsirkin | ff31d2e | 2015-01-13 11:23:32 +0200 | [diff] [blame] | 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 | |
Michael S. Tsirkin | 9a4253d | 2014-12-11 21:47:49 +0200 | [diff] [blame] | 546 | static int virtio_pci_probe(struct pci_dev *pci_dev, |
| 547 | const struct pci_device_id *id) |
| 548 | { |
weiping zhang | 33635bd | 2017-12-21 20:40:24 +0800 | [diff] [blame] | 549 | struct virtio_pci_device *vp_dev, *reg_dev = NULL; |
Michael S. Tsirkin | ff31d2e | 2015-01-13 11:23:32 +0200 | [diff] [blame] | 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; |
Michael S. Tsirkin | 0a9b3f4 | 2017-04-04 21:44:44 +0300 | [diff] [blame] | 561 | INIT_LIST_HEAD(&vp_dev->virtqueues); |
| 562 | spin_lock_init(&vp_dev->lock); |
Michael S. Tsirkin | ff31d2e | 2015-01-13 11:23:32 +0200 | [diff] [blame] | 563 | |
Michael S. Tsirkin | ff31d2e | 2015-01-13 11:23:32 +0200 | [diff] [blame] | 564 | /* enable the device */ |
| 565 | rc = pci_enable_device(pci_dev); |
| 566 | if (rc) |
| 567 | goto err_enable_device; |
| 568 | |
Michael S. Tsirkin | ac399d8 | 2015-01-15 17:54:13 +0200 | [diff] [blame] | 569 | if (force_legacy) { |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 570 | rc = virtio_pci_legacy_probe(vp_dev); |
Michael S. Tsirkin | ac399d8 | 2015-01-15 17:54:13 +0200 | [diff] [blame] | 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 | } |
Michael S. Tsirkin | ff31d2e | 2015-01-13 11:23:32 +0200 | [diff] [blame] | 583 | |
| 584 | pci_set_master(pci_dev); |
| 585 | |
Wu Zongyong | d89c8169 | 2021-10-29 17:14:42 +0800 | [diff] [blame] | 586 | vp_dev->is_legacy = vp_dev->ldev.ioaddr ? true : false; |
| 587 | |
Michael S. Tsirkin | ff31d2e | 2015-01-13 11:23:32 +0200 | [diff] [blame] | 588 | rc = register_virtio_device(&vp_dev->vdev); |
weiping zhang | 33635bd | 2017-12-21 20:40:24 +0800 | [diff] [blame] | 589 | reg_dev = vp_dev; |
Michael S. Tsirkin | ff31d2e | 2015-01-13 11:23:32 +0200 | [diff] [blame] | 590 | if (rc) |
| 591 | goto err_register; |
| 592 | |
| 593 | return 0; |
| 594 | |
| 595 | err_register: |
Wu Zongyong | d89c8169 | 2021-10-29 17:14:42 +0800 | [diff] [blame] | 596 | if (vp_dev->is_legacy) |
| 597 | virtio_pci_legacy_remove(vp_dev); |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 598 | else |
Wu Zongyong | d89c8169 | 2021-10-29 17:14:42 +0800 | [diff] [blame] | 599 | virtio_pci_modern_remove(vp_dev); |
Michael S. Tsirkin | ff31d2e | 2015-01-13 11:23:32 +0200 | [diff] [blame] | 600 | err_probe: |
Michael S. Tsirkin | ff31d2e | 2015-01-13 11:23:32 +0200 | [diff] [blame] | 601 | pci_disable_device(pci_dev); |
| 602 | err_enable_device: |
weiping zhang | 33635bd | 2017-12-21 20:40:24 +0800 | [diff] [blame] | 603 | if (reg_dev) |
| 604 | put_device(&vp_dev->vdev.dev); |
| 605 | else |
| 606 | kfree(vp_dev); |
Michael S. Tsirkin | ff31d2e | 2015-01-13 11:23:32 +0200 | [diff] [blame] | 607 | return rc; |
Michael S. Tsirkin | 9a4253d | 2014-12-11 21:47:49 +0200 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | static void virtio_pci_remove(struct pci_dev *pci_dev) |
| 611 | { |
Michael S. Tsirkin | ff31d2e | 2015-01-13 11:23:32 +0200 | [diff] [blame] | 612 | struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev); |
Michael S. Tsirkin | 2989be0 | 2016-01-14 16:00:41 +0200 | [diff] [blame] | 613 | struct device *dev = get_device(&vp_dev->vdev.dev); |
Michael S. Tsirkin | ff31d2e | 2015-01-13 11:23:32 +0200 | [diff] [blame] | 614 | |
Parav Pandit | 43bb40c5 | 2021-07-21 17:26:48 +0300 | [diff] [blame] | 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 | |
Tiwei Bie | cfecc29 | 2018-06-01 12:02:39 +0800 | [diff] [blame] | 622 | pci_disable_sriov(pci_dev); |
| 623 | |
Michael S. Tsirkin | ff31d2e | 2015-01-13 11:23:32 +0200 | [diff] [blame] | 624 | unregister_virtio_device(&vp_dev->vdev); |
| 625 | |
Wu Zongyong | d89c8169 | 2021-10-29 17:14:42 +0800 | [diff] [blame] | 626 | if (vp_dev->is_legacy) |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 627 | virtio_pci_legacy_remove(vp_dev); |
| 628 | else |
| 629 | virtio_pci_modern_remove(vp_dev); |
Michael S. Tsirkin | ff31d2e | 2015-01-13 11:23:32 +0200 | [diff] [blame] | 630 | |
Michael S. Tsirkin | ff31d2e | 2015-01-13 11:23:32 +0200 | [diff] [blame] | 631 | pci_disable_device(pci_dev); |
Michael S. Tsirkin | 2989be0 | 2016-01-14 16:00:41 +0200 | [diff] [blame] | 632 | put_device(dev); |
Michael S. Tsirkin | 9a4253d | 2014-12-11 21:47:49 +0200 | [diff] [blame] | 633 | } |
| 634 | |
Tiwei Bie | cfecc29 | 2018-06-01 12:02:39 +0800 | [diff] [blame] | 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 | |
Michael S. Tsirkin | 9a4253d | 2014-12-11 21:47:49 +0200 | [diff] [blame] | 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 |
Tiwei Bie | cfecc29 | 2018-06-01 12:02:39 +0800 | [diff] [blame] | 670 | .sriov_configure = virtio_pci_sriov_configure, |
Michael S. Tsirkin | 9a4253d | 2014-12-11 21:47:49 +0200 | [diff] [blame] | 671 | }; |
| 672 | |
| 673 | module_pci_driver(virtio_pci_driver); |
Herbert Xu | 5ff1611 | 2014-12-17 00:54:03 +0200 | [diff] [blame] | 674 | |
| 675 | MODULE_AUTHOR("Anthony Liguori <aliguori@us.ibm.com>"); |
| 676 | MODULE_DESCRIPTION("virtio-pci"); |
| 677 | MODULE_LICENSE("GPL"); |
| 678 | MODULE_VERSION("1"); |