Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 1 | /* |
Michael S. Tsirkin | a90fdce | 2014-12-08 12:31:02 +0200 | [diff] [blame] | 2 | * Virtio PCI driver - common functionality for all device versions |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 3 | * |
| 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. Tsirkin | a90fdce | 2014-12-08 12:31:02 +0200 | [diff] [blame] | 8 | * Copyright Red Hat, Inc. 2014 |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 9 | * |
| 10 | * Authors: |
| 11 | * Anthony Liguori <aliguori@us.ibm.com> |
Michael S. Tsirkin | a90fdce | 2014-12-08 12:31:02 +0200 | [diff] [blame] | 12 | * Rusty Russell <rusty@rustcorp.com.au> |
| 13 | * Michael S. Tsirkin <mst@redhat.com> |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 14 | * |
| 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. Tsirkin | 5f4c976 | 2014-12-08 16:39:45 +0200 | [diff] [blame] | 20 | #include "virtio_pci_common.h" |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 21 | |
Michael S. Tsirkin | ac399d8 | 2015-01-15 17:54:13 +0200 | [diff] [blame] | 22 | static bool force_legacy = false; |
| 23 | |
| 24 | #if IS_ENABLED(CONFIG_VIRTIO_PCI_LEGACY) |
| 25 | module_param(force_legacy, bool, 0444); |
| 26 | MODULE_PARM_DESC(force_legacy, |
| 27 | "Force legacy mode for transitional virtio 1 devices"); |
| 28 | #endif |
| 29 | |
Michael S. Tsirkin | e6af578 | 2011-11-17 17:41:15 +0200 | [diff] [blame] | 30 | /* wait for pending irq handlers */ |
Michael S. Tsirkin | 38eb4a2 | 2014-12-07 18:41:16 +0200 | [diff] [blame] | 31 | void vp_synchronize_vectors(struct virtio_device *vdev) |
Michael S. Tsirkin | e6af578 | 2011-11-17 17:41:15 +0200 | [diff] [blame] | 32 | { |
| 33 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
| 34 | int i; |
| 35 | |
Christoph Hellwig | 07ec5148 | 2017-02-05 18:15:19 +0100 | [diff] [blame] | 36 | synchronize_irq(pci_irq_vector(vp_dev->pci_dev, 0)); |
| 37 | for (i = 1; i < vp_dev->msix_vectors; i++) |
Christoph Hellwig | fa3a327 | 2016-11-17 11:43:13 +0100 | [diff] [blame] | 38 | synchronize_irq(pci_irq_vector(vp_dev->pci_dev, i)); |
Michael S. Tsirkin | e6af578 | 2011-11-17 17:41:15 +0200 | [diff] [blame] | 39 | } |
| 40 | |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 41 | /* the notify function used when creating a virt queue */ |
Michael S. Tsirkin | 38eb4a2 | 2014-12-07 18:41:16 +0200 | [diff] [blame] | 42 | bool vp_notify(struct virtqueue *vq) |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 43 | { |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 44 | /* we write the queue's selector into the notification register to |
| 45 | * signal the other end */ |
Michael S. Tsirkin | f30eaf4 | 2014-12-03 18:01:58 +0200 | [diff] [blame] | 46 | iowrite16(vq->index, (void __iomem *)vq->priv); |
Heinz Graalfs | 46f9c2b | 2013-10-29 09:38:50 +1030 | [diff] [blame] | 47 | return true; |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 48 | } |
| 49 | |
Michael S. Tsirkin | 77cf5246 | 2009-05-14 13:55:31 +0300 | [diff] [blame] | 50 | /* Handle a configuration change: Tell driver if it wants to know. */ |
| 51 | static irqreturn_t vp_config_changed(int irq, void *opaque) |
| 52 | { |
| 53 | struct virtio_pci_device *vp_dev = opaque; |
Michael S. Tsirkin | 77cf5246 | 2009-05-14 13:55:31 +0300 | [diff] [blame] | 54 | |
Michael S. Tsirkin | 016c98c | 2014-10-14 10:40:34 +1030 | [diff] [blame] | 55 | virtio_config_changed(&vp_dev->vdev); |
Michael S. Tsirkin | 77cf5246 | 2009-05-14 13:55:31 +0300 | [diff] [blame] | 56 | return IRQ_HANDLED; |
| 57 | } |
| 58 | |
| 59 | /* Notify all virtqueues on an interrupt. */ |
| 60 | static irqreturn_t vp_vring_interrupt(int irq, void *opaque) |
| 61 | { |
| 62 | struct virtio_pci_device *vp_dev = opaque; |
Michael S. Tsirkin | 77cf5246 | 2009-05-14 13:55:31 +0300 | [diff] [blame] | 63 | irqreturn_t ret = IRQ_NONE; |
Christoph Hellwig | 5c34d00 | 2017-02-05 18:15:18 +0100 | [diff] [blame] | 64 | struct virtqueue *vq; |
Michael S. Tsirkin | 77cf5246 | 2009-05-14 13:55:31 +0300 | [diff] [blame] | 65 | |
Christoph Hellwig | 5c34d00 | 2017-02-05 18:15:18 +0100 | [diff] [blame] | 66 | list_for_each_entry(vq, &vp_dev->vdev.vqs, list) { |
| 67 | if (vq->callback && vring_interrupt(irq, vq) == IRQ_HANDLED) |
Michael S. Tsirkin | 77cf5246 | 2009-05-14 13:55:31 +0300 | [diff] [blame] | 68 | ret = IRQ_HANDLED; |
| 69 | } |
Michael S. Tsirkin | 77cf5246 | 2009-05-14 13:55:31 +0300 | [diff] [blame] | 70 | |
| 71 | return ret; |
| 72 | } |
| 73 | |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 74 | /* A small wrapper to also acknowledge the interrupt when it's handled. |
| 75 | * I really need an EIO hook for the vring so I can ack the interrupt once we |
| 76 | * know that we'll be handling the IRQ but before we invoke the callback since |
| 77 | * the callback may notify the host which results in the host attempting to |
| 78 | * raise an interrupt that we would then mask once we acknowledged the |
| 79 | * interrupt. */ |
| 80 | static irqreturn_t vp_interrupt(int irq, void *opaque) |
| 81 | { |
| 82 | struct virtio_pci_device *vp_dev = opaque; |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 83 | u8 isr; |
| 84 | |
| 85 | /* reading the ISR has the effect of also clearing it so it's very |
| 86 | * important to save off the value. */ |
Michael S. Tsirkin | af53572 | 2014-12-02 14:35:27 +0200 | [diff] [blame] | 87 | isr = ioread8(vp_dev->isr); |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 88 | |
| 89 | /* It's definitely not us if the ISR was not high */ |
| 90 | if (!isr) |
| 91 | return IRQ_NONE; |
| 92 | |
| 93 | /* Configuration change? Tell driver if it wants to know. */ |
Michael S. Tsirkin | 77cf5246 | 2009-05-14 13:55:31 +0300 | [diff] [blame] | 94 | if (isr & VIRTIO_PCI_ISR_CONFIG) |
| 95 | vp_config_changed(irq, opaque); |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 96 | |
Michael S. Tsirkin | 77cf5246 | 2009-05-14 13:55:31 +0300 | [diff] [blame] | 97 | return vp_vring_interrupt(irq, opaque); |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 98 | } |
| 99 | |
Christoph Hellwig | 07ec5148 | 2017-02-05 18:15:19 +0100 | [diff] [blame] | 100 | static void vp_remove_vqs(struct virtio_device *vdev) |
Michael S. Tsirkin | d2a7ddd | 2009-06-12 22:16:36 -0600 | [diff] [blame] | 101 | { |
Michael S. Tsirkin | e969fed | 2009-07-26 15:48:08 +0300 | [diff] [blame] | 102 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
Michael S. Tsirkin | d2a7ddd | 2009-06-12 22:16:36 -0600 | [diff] [blame] | 103 | struct virtqueue *vq, *n; |
| 104 | |
Michael S. Tsirkin | e969fed | 2009-07-26 15:48:08 +0300 | [diff] [blame] | 105 | list_for_each_entry_safe(vq, n, &vdev->vqs, list) { |
Christoph Hellwig | 5c34d00 | 2017-02-05 18:15:18 +0100 | [diff] [blame] | 106 | if (vp_dev->msix_vector_map) { |
| 107 | int v = vp_dev->msix_vector_map[vq->index]; |
Christoph Hellwig | fa3a327 | 2016-11-17 11:43:13 +0100 | [diff] [blame] | 108 | |
| 109 | if (v != VIRTIO_MSI_NO_VECTOR) |
| 110 | free_irq(pci_irq_vector(vp_dev->pci_dev, v), |
| 111 | vq); |
| 112 | } |
Christoph Hellwig | 5c34d00 | 2017-02-05 18:15:18 +0100 | [diff] [blame] | 113 | vp_dev->del_vq(vq); |
Michael S. Tsirkin | e969fed | 2009-07-26 15:48:08 +0300 | [diff] [blame] | 114 | } |
Christoph Hellwig | 07ec5148 | 2017-02-05 18:15:19 +0100 | [diff] [blame] | 115 | } |
Michael S. Tsirkin | 82af8ce | 2009-05-14 13:55:41 +0300 | [diff] [blame] | 116 | |
Christoph Hellwig | 07ec5148 | 2017-02-05 18:15:19 +0100 | [diff] [blame] | 117 | /* the config->del_vqs() implementation */ |
| 118 | void vp_del_vqs(struct virtio_device *vdev) |
| 119 | { |
| 120 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
| 121 | int i; |
Christoph Hellwig | 66f2f55 | 2016-11-17 11:43:15 +0100 | [diff] [blame] | 122 | |
Christoph Hellwig | 07ec5148 | 2017-02-05 18:15:19 +0100 | [diff] [blame] | 123 | if (WARN_ON_ONCE(list_empty_careful(&vdev->vqs))) |
| 124 | return; |
Christoph Hellwig | 66f2f55 | 2016-11-17 11:43:15 +0100 | [diff] [blame] | 125 | |
Christoph Hellwig | 07ec5148 | 2017-02-05 18:15:19 +0100 | [diff] [blame] | 126 | vp_remove_vqs(vdev); |
Christoph Hellwig | 66f2f55 | 2016-11-17 11:43:15 +0100 | [diff] [blame] | 127 | |
Michael S. Tsirkin | 2008c15 | 2017-04-04 21:09:20 +0300 | [diff] [blame^] | 128 | if (vp_dev->msix_enabled) { |
Christoph Hellwig | 07ec5148 | 2017-02-05 18:15:19 +0100 | [diff] [blame] | 129 | for (i = 0; i < vp_dev->msix_vectors; i++) |
| 130 | free_cpumask_var(vp_dev->msix_affinity_masks[i]); |
| 131 | |
Christoph Hellwig | 66f2f55 | 2016-11-17 11:43:15 +0100 | [diff] [blame] | 132 | /* Disable the vector used for configuration */ |
| 133 | vp_dev->config_vector(vp_dev, VIRTIO_MSI_NO_VECTOR); |
| 134 | |
Christoph Hellwig | 07ec5148 | 2017-02-05 18:15:19 +0100 | [diff] [blame] | 135 | kfree(vp_dev->msix_affinity_masks); |
| 136 | kfree(vp_dev->msix_names); |
| 137 | kfree(vp_dev->msix_vector_map); |
Christoph Hellwig | 66f2f55 | 2016-11-17 11:43:15 +0100 | [diff] [blame] | 138 | } |
| 139 | |
Christoph Hellwig | 07ec5148 | 2017-02-05 18:15:19 +0100 | [diff] [blame] | 140 | free_irq(pci_irq_vector(vp_dev->pci_dev, 0), vp_dev); |
| 141 | pci_free_irq_vectors(vp_dev->pci_dev); |
Michael S. Tsirkin | d2a7ddd | 2009-06-12 22:16:36 -0600 | [diff] [blame] | 142 | } |
| 143 | |
Christoph Hellwig | a3cbec6 | 2016-11-17 11:43:16 +0100 | [diff] [blame] | 144 | static int vp_find_vqs_msix(struct virtio_device *vdev, unsigned nvqs, |
Christoph Hellwig | 52a6151 | 2017-02-05 18:15:21 +0100 | [diff] [blame] | 145 | struct virtqueue *vqs[], vq_callback_t *callbacks[], |
Michael S. Tsirkin | bf951b1 | 2017-04-04 21:08:54 +0300 | [diff] [blame] | 146 | const char * const names[], bool per_vq_vectors, |
| 147 | struct irq_affinity *desc) |
Michael S. Tsirkin | e969fed | 2009-07-26 15:48:08 +0300 | [diff] [blame] | 148 | { |
| 149 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
Christoph Hellwig | 07ec5148 | 2017-02-05 18:15:19 +0100 | [diff] [blame] | 150 | const char *name = dev_name(&vp_dev->vdev.dev); |
Michael S. Tsirkin | 8f10d01 | 2017-04-04 21:04:23 +0300 | [diff] [blame] | 151 | int i, err = -ENOMEM, allocated_vectors, nvectors; |
Christoph Hellwig | fb5e31d | 2017-02-05 18:15:22 +0100 | [diff] [blame] | 152 | unsigned flags = PCI_IRQ_MSIX; |
Rusty Russell | f68d240 | 2009-09-23 22:26:29 -0600 | [diff] [blame] | 153 | u16 msix_vec; |
Christoph Hellwig | 07ec5148 | 2017-02-05 18:15:19 +0100 | [diff] [blame] | 154 | |
Christoph Hellwig | fb5e31d | 2017-02-05 18:15:22 +0100 | [diff] [blame] | 155 | if (desc) { |
| 156 | flags |= PCI_IRQ_AFFINITY; |
| 157 | desc->pre_vectors++; /* virtio config vector */ |
| 158 | } |
| 159 | |
Christoph Hellwig | 07ec5148 | 2017-02-05 18:15:19 +0100 | [diff] [blame] | 160 | nvectors = 1; |
| 161 | for (i = 0; i < nvqs; i++) |
| 162 | if (callbacks[i]) |
| 163 | nvectors++; |
Michael S. Tsirkin | e969fed | 2009-07-26 15:48:08 +0300 | [diff] [blame] | 164 | |
Michael S. Tsirkin | bf951b1 | 2017-04-04 21:08:54 +0300 | [diff] [blame] | 165 | if (per_vq_vectors) { |
| 166 | err = pci_alloc_irq_vectors_affinity(vp_dev->pci_dev, nvectors, |
| 167 | nvectors, flags, desc); |
| 168 | } else { |
Christoph Hellwig | 07ec5148 | 2017-02-05 18:15:19 +0100 | [diff] [blame] | 169 | err = pci_alloc_irq_vectors(vp_dev->pci_dev, 2, 2, |
| 170 | PCI_IRQ_MSIX); |
| 171 | } |
| 172 | if (err < 0) |
| 173 | return err; |
| 174 | |
| 175 | vp_dev->msix_vectors = nvectors; |
| 176 | vp_dev->msix_names = kmalloc_array(nvectors, |
| 177 | sizeof(*vp_dev->msix_names), GFP_KERNEL); |
| 178 | if (!vp_dev->msix_names) |
| 179 | goto out_free_irq_vectors; |
| 180 | |
| 181 | vp_dev->msix_affinity_masks = kcalloc(nvectors, |
| 182 | sizeof(*vp_dev->msix_affinity_masks), GFP_KERNEL); |
| 183 | if (!vp_dev->msix_affinity_masks) |
| 184 | goto out_free_msix_names; |
| 185 | |
| 186 | for (i = 0; i < nvectors; ++i) { |
| 187 | if (!alloc_cpumask_var(&vp_dev->msix_affinity_masks[i], |
| 188 | GFP_KERNEL)) |
| 189 | goto out_free_msix_affinity_masks; |
Rusty Russell | f68d240 | 2009-09-23 22:26:29 -0600 | [diff] [blame] | 190 | } |
Michael S. Tsirkin | e969fed | 2009-07-26 15:48:08 +0300 | [diff] [blame] | 191 | |
Christoph Hellwig | 07ec5148 | 2017-02-05 18:15:19 +0100 | [diff] [blame] | 192 | /* Set the vector used for configuration */ |
| 193 | snprintf(vp_dev->msix_names[0], sizeof(*vp_dev->msix_names), |
| 194 | "%s-config", name); |
| 195 | err = request_irq(pci_irq_vector(vp_dev->pci_dev, 0), vp_config_changed, |
| 196 | 0, vp_dev->msix_names[0], vp_dev); |
Christoph Hellwig | a3cbec6 | 2016-11-17 11:43:16 +0100 | [diff] [blame] | 197 | if (err) |
Michael S. Tsirkin | bf951b1 | 2017-04-04 21:08:54 +0300 | [diff] [blame] | 198 | goto out_free_irq_vectors; |
Christoph Hellwig | a3cbec6 | 2016-11-17 11:43:16 +0100 | [diff] [blame] | 199 | |
Christoph Hellwig | 07ec5148 | 2017-02-05 18:15:19 +0100 | [diff] [blame] | 200 | /* Verify we had enough resources to assign the vector */ |
| 201 | if (vp_dev->config_vector(vp_dev, 0) == VIRTIO_MSI_NO_VECTOR) { |
| 202 | err = -EBUSY; |
| 203 | goto out_free_config_irq; |
Christoph Hellwig | 5c34d00 | 2017-02-05 18:15:18 +0100 | [diff] [blame] | 204 | } |
| 205 | |
Christoph Hellwig | 07ec5148 | 2017-02-05 18:15:19 +0100 | [diff] [blame] | 206 | vp_dev->msix_vector_map = kmalloc_array(nvqs, |
| 207 | sizeof(*vp_dev->msix_vector_map), GFP_KERNEL); |
| 208 | if (!vp_dev->msix_vector_map) |
| 209 | goto out_disable_config_irq; |
| 210 | |
Michael S. Tsirkin | 8f10d01 | 2017-04-04 21:04:23 +0300 | [diff] [blame] | 211 | allocated_vectors = 1; /* vector 0 is the config interrupt */ |
Michael S. Tsirkin | e969fed | 2009-07-26 15:48:08 +0300 | [diff] [blame] | 212 | for (i = 0; i < nvqs; ++i) { |
Michael S. Tsirkin | 6457f12 | 2012-09-05 21:47:45 +0300 | [diff] [blame] | 213 | if (!names[i]) { |
| 214 | vqs[i] = NULL; |
| 215 | continue; |
Christoph Hellwig | a3cbec6 | 2016-11-17 11:43:16 +0100 | [diff] [blame] | 216 | } |
| 217 | |
Christoph Hellwig | 07ec5148 | 2017-02-05 18:15:19 +0100 | [diff] [blame] | 218 | if (callbacks[i]) |
| 219 | msix_vec = allocated_vectors; |
Michael S. Tsirkin | e969fed | 2009-07-26 15:48:08 +0300 | [diff] [blame] | 220 | else |
Christoph Hellwig | 07ec5148 | 2017-02-05 18:15:19 +0100 | [diff] [blame] | 221 | msix_vec = VIRTIO_MSI_NO_VECTOR; |
| 222 | |
Christoph Hellwig | 5c34d00 | 2017-02-05 18:15:18 +0100 | [diff] [blame] | 223 | vqs[i] = vp_dev->setup_vq(vp_dev, i, callbacks[i], names[i], |
| 224 | msix_vec); |
Michael S. Tsirkin | e969fed | 2009-07-26 15:48:08 +0300 | [diff] [blame] | 225 | if (IS_ERR(vqs[i])) { |
| 226 | err = PTR_ERR(vqs[i]); |
Christoph Hellwig | 07ec5148 | 2017-02-05 18:15:19 +0100 | [diff] [blame] | 227 | goto out_remove_vqs; |
Michael S. Tsirkin | e969fed | 2009-07-26 15:48:08 +0300 | [diff] [blame] | 228 | } |
Michael S. Tsirkin | 0b22bd0 | 2009-10-22 15:06:06 +0200 | [diff] [blame] | 229 | |
Christoph Hellwig | 5c34d00 | 2017-02-05 18:15:18 +0100 | [diff] [blame] | 230 | if (msix_vec == VIRTIO_MSI_NO_VECTOR) { |
| 231 | vp_dev->msix_vector_map[i] = VIRTIO_MSI_NO_VECTOR; |
| 232 | continue; |
| 233 | } |
| 234 | |
Michael S. Tsirkin | 8f10d01 | 2017-04-04 21:04:23 +0300 | [diff] [blame] | 235 | snprintf(vp_dev->msix_names[i + 1], |
Christoph Hellwig | 07ec5148 | 2017-02-05 18:15:19 +0100 | [diff] [blame] | 236 | sizeof(*vp_dev->msix_names), "%s-%s", |
Michael S. Tsirkin | 0b22bd0 | 2009-10-22 15:06:06 +0200 | [diff] [blame] | 237 | dev_name(&vp_dev->vdev.dev), names[i]); |
Christoph Hellwig | fa3a327 | 2016-11-17 11:43:13 +0100 | [diff] [blame] | 238 | err = request_irq(pci_irq_vector(vp_dev->pci_dev, msix_vec), |
Christoph Hellwig | 07ec5148 | 2017-02-05 18:15:19 +0100 | [diff] [blame] | 239 | vring_interrupt, IRQF_SHARED, |
Michael S. Tsirkin | 8f10d01 | 2017-04-04 21:04:23 +0300 | [diff] [blame] | 240 | vp_dev->msix_names[i + 1], vqs[i]); |
Christoph Hellwig | 5c34d00 | 2017-02-05 18:15:18 +0100 | [diff] [blame] | 241 | if (err) { |
| 242 | /* don't free this irq on error */ |
| 243 | vp_dev->msix_vector_map[i] = VIRTIO_MSI_NO_VECTOR; |
Christoph Hellwig | 07ec5148 | 2017-02-05 18:15:19 +0100 | [diff] [blame] | 244 | goto out_remove_vqs; |
Christoph Hellwig | 5c34d00 | 2017-02-05 18:15:18 +0100 | [diff] [blame] | 245 | } |
| 246 | vp_dev->msix_vector_map[i] = msix_vec; |
Christoph Hellwig | 07ec5148 | 2017-02-05 18:15:19 +0100 | [diff] [blame] | 247 | |
Michael S. Tsirkin | bf951b1 | 2017-04-04 21:08:54 +0300 | [diff] [blame] | 248 | if (per_vq_vectors) |
Christoph Hellwig | 07ec5148 | 2017-02-05 18:15:19 +0100 | [diff] [blame] | 249 | allocated_vectors++; |
Michael S. Tsirkin | e969fed | 2009-07-26 15:48:08 +0300 | [diff] [blame] | 250 | } |
Christoph Hellwig | 07ec5148 | 2017-02-05 18:15:19 +0100 | [diff] [blame] | 251 | |
Michael S. Tsirkin | 2008c15 | 2017-04-04 21:09:20 +0300 | [diff] [blame^] | 252 | vp_dev->msix_enabled = 1; |
Michael S. Tsirkin | e969fed | 2009-07-26 15:48:08 +0300 | [diff] [blame] | 253 | return 0; |
| 254 | |
Christoph Hellwig | 07ec5148 | 2017-02-05 18:15:19 +0100 | [diff] [blame] | 255 | out_remove_vqs: |
| 256 | vp_remove_vqs(vdev); |
| 257 | kfree(vp_dev->msix_vector_map); |
| 258 | out_disable_config_irq: |
| 259 | vp_dev->config_vector(vp_dev, VIRTIO_MSI_NO_VECTOR); |
| 260 | out_free_config_irq: |
| 261 | free_irq(pci_irq_vector(vp_dev->pci_dev, 0), vp_dev); |
| 262 | out_free_msix_affinity_masks: |
| 263 | for (i = 0; i < nvectors; i++) { |
| 264 | if (vp_dev->msix_affinity_masks[i]) |
| 265 | free_cpumask_var(vp_dev->msix_affinity_masks[i]); |
| 266 | } |
| 267 | kfree(vp_dev->msix_affinity_masks); |
| 268 | out_free_msix_names: |
| 269 | kfree(vp_dev->msix_names); |
| 270 | out_free_irq_vectors: |
| 271 | pci_free_irq_vectors(vp_dev->pci_dev); |
Michael S. Tsirkin | e969fed | 2009-07-26 15:48:08 +0300 | [diff] [blame] | 272 | return err; |
| 273 | } |
| 274 | |
Christoph Hellwig | a3cbec6 | 2016-11-17 11:43:16 +0100 | [diff] [blame] | 275 | static int vp_find_vqs_intx(struct virtio_device *vdev, unsigned nvqs, |
| 276 | struct virtqueue *vqs[], vq_callback_t *callbacks[], |
| 277 | const char * const names[]) |
| 278 | { |
| 279 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
| 280 | int i, err; |
| 281 | |
Christoph Hellwig | a3cbec6 | 2016-11-17 11:43:16 +0100 | [diff] [blame] | 282 | err = request_irq(vp_dev->pci_dev->irq, vp_interrupt, IRQF_SHARED, |
| 283 | dev_name(&vdev->dev), vp_dev); |
| 284 | if (err) |
Christoph Hellwig | 07ec5148 | 2017-02-05 18:15:19 +0100 | [diff] [blame] | 285 | return err; |
Christoph Hellwig | a3cbec6 | 2016-11-17 11:43:16 +0100 | [diff] [blame] | 286 | |
Christoph Hellwig | a3cbec6 | 2016-11-17 11:43:16 +0100 | [diff] [blame] | 287 | for (i = 0; i < nvqs; ++i) { |
| 288 | if (!names[i]) { |
| 289 | vqs[i] = NULL; |
| 290 | continue; |
| 291 | } |
Christoph Hellwig | 5c34d00 | 2017-02-05 18:15:18 +0100 | [diff] [blame] | 292 | vqs[i] = vp_dev->setup_vq(vp_dev, i, callbacks[i], names[i], |
Christoph Hellwig | a3cbec6 | 2016-11-17 11:43:16 +0100 | [diff] [blame] | 293 | VIRTIO_MSI_NO_VECTOR); |
| 294 | if (IS_ERR(vqs[i])) { |
| 295 | err = PTR_ERR(vqs[i]); |
Christoph Hellwig | 07ec5148 | 2017-02-05 18:15:19 +0100 | [diff] [blame] | 296 | goto out_remove_vqs; |
Christoph Hellwig | a3cbec6 | 2016-11-17 11:43:16 +0100 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | |
| 300 | return 0; |
Christoph Hellwig | 07ec5148 | 2017-02-05 18:15:19 +0100 | [diff] [blame] | 301 | |
| 302 | out_remove_vqs: |
| 303 | vp_remove_vqs(vdev); |
| 304 | free_irq(pci_irq_vector(vp_dev->pci_dev, 0), vp_dev); |
Christoph Hellwig | a3cbec6 | 2016-11-17 11:43:16 +0100 | [diff] [blame] | 305 | return err; |
| 306 | } |
| 307 | |
Michael S. Tsirkin | 82af8ce | 2009-05-14 13:55:41 +0300 | [diff] [blame] | 308 | /* the config->find_vqs() implementation */ |
Michael S. Tsirkin | 38eb4a2 | 2014-12-07 18:41:16 +0200 | [diff] [blame] | 309 | int vp_find_vqs(struct virtio_device *vdev, unsigned nvqs, |
Christoph Hellwig | fb5e31d | 2017-02-05 18:15:22 +0100 | [diff] [blame] | 310 | struct virtqueue *vqs[], vq_callback_t *callbacks[], |
| 311 | const char * const names[], struct irq_affinity *desc) |
Michael S. Tsirkin | d2a7ddd | 2009-06-12 22:16:36 -0600 | [diff] [blame] | 312 | { |
Rusty Russell | f68d240 | 2009-09-23 22:26:29 -0600 | [diff] [blame] | 313 | int err; |
Michael S. Tsirkin | 82af8ce | 2009-05-14 13:55:41 +0300 | [diff] [blame] | 314 | |
Michael S. Tsirkin | bf951b1 | 2017-04-04 21:08:54 +0300 | [diff] [blame] | 315 | /* Try MSI-X with one vector per queue. */ |
| 316 | err = vp_find_vqs_msix(vdev, nvqs, vqs, callbacks, names, true, desc); |
Michael S. Tsirkin | e969fed | 2009-07-26 15:48:08 +0300 | [diff] [blame] | 317 | if (!err) |
| 318 | return 0; |
Michael S. Tsirkin | bf951b1 | 2017-04-04 21:08:54 +0300 | [diff] [blame] | 319 | /* Fallback: MSI-X with one vector for config, one shared for queues. */ |
| 320 | err = vp_find_vqs_msix(vdev, nvqs, vqs, callbacks, names, false, desc); |
| 321 | if (!err) |
| 322 | return 0; |
| 323 | /* Finally fall back to regular interrupts. */ |
Christoph Hellwig | a3cbec6 | 2016-11-17 11:43:16 +0100 | [diff] [blame] | 324 | return vp_find_vqs_intx(vdev, nvqs, vqs, callbacks, names); |
Michael S. Tsirkin | d2a7ddd | 2009-06-12 22:16:36 -0600 | [diff] [blame] | 325 | } |
| 326 | |
Michael S. Tsirkin | 38eb4a2 | 2014-12-07 18:41:16 +0200 | [diff] [blame] | 327 | const char *vp_bus_name(struct virtio_device *vdev) |
Rick Jones | 6684604 | 2011-11-14 14:17:08 +0000 | [diff] [blame] | 328 | { |
| 329 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
| 330 | |
| 331 | return pci_name(vp_dev->pci_dev); |
| 332 | } |
| 333 | |
Jason Wang | 75a0a52 | 2012-08-28 13:54:14 +0200 | [diff] [blame] | 334 | /* Setup the affinity for a virtqueue: |
| 335 | * - force the affinity for per vq vector |
| 336 | * - OR over all affinities for shared MSI |
| 337 | * - ignore the affinity request if we're using INTX |
| 338 | */ |
Michael S. Tsirkin | 38eb4a2 | 2014-12-07 18:41:16 +0200 | [diff] [blame] | 339 | int vp_set_vq_affinity(struct virtqueue *vq, int cpu) |
Jason Wang | 75a0a52 | 2012-08-28 13:54:14 +0200 | [diff] [blame] | 340 | { |
| 341 | struct virtio_device *vdev = vq->vdev; |
| 342 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
Jason Wang | 75a0a52 | 2012-08-28 13:54:14 +0200 | [diff] [blame] | 343 | |
| 344 | if (!vq->callback) |
| 345 | return -EINVAL; |
| 346 | |
Michael S. Tsirkin | 2008c15 | 2017-04-04 21:09:20 +0300 | [diff] [blame^] | 347 | if (vp_dev->msix_enabled) { |
Christoph Hellwig | 5c34d00 | 2017-02-05 18:15:18 +0100 | [diff] [blame] | 348 | int vec = vp_dev->msix_vector_map[vq->index]; |
| 349 | struct cpumask *mask = vp_dev->msix_affinity_masks[vec]; |
| 350 | unsigned int irq = pci_irq_vector(vp_dev->pci_dev, vec); |
| 351 | |
Jason Wang | 75a0a52 | 2012-08-28 13:54:14 +0200 | [diff] [blame] | 352 | if (cpu == -1) |
| 353 | irq_set_affinity_hint(irq, NULL); |
| 354 | else { |
Jiang Liu | 210d150 | 2015-06-04 16:41:44 +0800 | [diff] [blame] | 355 | cpumask_clear(mask); |
Jason Wang | 75a0a52 | 2012-08-28 13:54:14 +0200 | [diff] [blame] | 356 | cpumask_set_cpu(cpu, mask); |
| 357 | irq_set_affinity_hint(irq, mask); |
| 358 | } |
| 359 | } |
| 360 | return 0; |
| 361 | } |
| 362 | |
Christoph Hellwig | bbaba47 | 2017-02-05 18:15:23 +0100 | [diff] [blame] | 363 | const struct cpumask *vp_get_vq_affinity(struct virtio_device *vdev, int index) |
| 364 | { |
| 365 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
| 366 | unsigned int *map = vp_dev->msix_vector_map; |
| 367 | |
| 368 | if (!map || map[index] == VIRTIO_MSI_NO_VECTOR) |
| 369 | return NULL; |
| 370 | |
| 371 | return pci_irq_get_affinity(vp_dev->pci_dev, map[index]); |
| 372 | } |
| 373 | |
Aaron Lu | 9e266ec | 2013-09-09 09:57:12 +0930 | [diff] [blame] | 374 | #ifdef CONFIG_PM_SLEEP |
Amit Shah | f0fe6f1 | 2011-12-22 16:58:26 +0530 | [diff] [blame] | 375 | static int virtio_pci_freeze(struct device *dev) |
| 376 | { |
| 377 | struct pci_dev *pci_dev = to_pci_dev(dev); |
| 378 | struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev); |
Amit Shah | f0fe6f1 | 2011-12-22 16:58:26 +0530 | [diff] [blame] | 379 | int ret; |
| 380 | |
Michael S. Tsirkin | c6716ba | 2014-10-14 10:40:35 +1030 | [diff] [blame] | 381 | ret = virtio_device_freeze(&vp_dev->vdev); |
Amit Shah | f0fe6f1 | 2011-12-22 16:58:26 +0530 | [diff] [blame] | 382 | |
| 383 | if (!ret) |
| 384 | pci_disable_device(pci_dev); |
| 385 | return ret; |
| 386 | } |
| 387 | |
Amit Shah | f0fe6f1 | 2011-12-22 16:58:26 +0530 | [diff] [blame] | 388 | static int virtio_pci_restore(struct device *dev) |
| 389 | { |
| 390 | struct pci_dev *pci_dev = to_pci_dev(dev); |
| 391 | struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev); |
Amit Shah | f0fe6f1 | 2011-12-22 16:58:26 +0530 | [diff] [blame] | 392 | int ret; |
| 393 | |
Amit Shah | 0517fdd | 2012-03-29 12:54:43 +0530 | [diff] [blame] | 394 | ret = pci_enable_device(pci_dev); |
| 395 | if (ret) |
| 396 | return ret; |
| 397 | |
| 398 | pci_set_master(pci_dev); |
Michael S. Tsirkin | c6716ba | 2014-10-14 10:40:35 +1030 | [diff] [blame] | 399 | return virtio_device_restore(&vp_dev->vdev); |
Amit Shah | f0fe6f1 | 2011-12-22 16:58:26 +0530 | [diff] [blame] | 400 | } |
| 401 | |
Michael S. Tsirkin | 9a4253d | 2014-12-11 21:47:49 +0200 | [diff] [blame] | 402 | static const struct dev_pm_ops virtio_pci_pm_ops = { |
Amit Shah | f878d0b | 2012-03-29 12:58:05 +0530 | [diff] [blame] | 403 | SET_SYSTEM_SLEEP_PM_OPS(virtio_pci_freeze, virtio_pci_restore) |
Amit Shah | d077536 | 2011-12-22 16:58:25 +0530 | [diff] [blame] | 404 | }; |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 405 | #endif |
Michael S. Tsirkin | 9a4253d | 2014-12-11 21:47:49 +0200 | [diff] [blame] | 406 | |
| 407 | |
| 408 | /* Qumranet donated their vendor ID for devices 0x1000 thru 0x10FF. */ |
| 409 | static const struct pci_device_id virtio_pci_id_table[] = { |
Robin H. Johnson | caf02ab | 2016-03-06 22:02:30 +0000 | [diff] [blame] | 410 | { PCI_DEVICE(PCI_VENDOR_ID_REDHAT_QUMRANET, PCI_ANY_ID) }, |
Michael S. Tsirkin | 9a4253d | 2014-12-11 21:47:49 +0200 | [diff] [blame] | 411 | { 0 } |
| 412 | }; |
| 413 | |
| 414 | MODULE_DEVICE_TABLE(pci, virtio_pci_id_table); |
| 415 | |
Michael S. Tsirkin | ff31d2e | 2015-01-13 11:23:32 +0200 | [diff] [blame] | 416 | static void virtio_pci_release_dev(struct device *_d) |
| 417 | { |
| 418 | struct virtio_device *vdev = dev_to_virtio(_d); |
| 419 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
| 420 | |
| 421 | /* As struct device is a kobject, it's not safe to |
| 422 | * free the memory (including the reference counter itself) |
| 423 | * until it's release callback. */ |
| 424 | kfree(vp_dev); |
| 425 | } |
| 426 | |
Michael S. Tsirkin | 9a4253d | 2014-12-11 21:47:49 +0200 | [diff] [blame] | 427 | static int virtio_pci_probe(struct pci_dev *pci_dev, |
| 428 | const struct pci_device_id *id) |
| 429 | { |
Michael S. Tsirkin | ff31d2e | 2015-01-13 11:23:32 +0200 | [diff] [blame] | 430 | struct virtio_pci_device *vp_dev; |
| 431 | int rc; |
| 432 | |
| 433 | /* allocate our structure and fill it out */ |
| 434 | vp_dev = kzalloc(sizeof(struct virtio_pci_device), GFP_KERNEL); |
| 435 | if (!vp_dev) |
| 436 | return -ENOMEM; |
| 437 | |
| 438 | pci_set_drvdata(pci_dev, vp_dev); |
| 439 | vp_dev->vdev.dev.parent = &pci_dev->dev; |
| 440 | vp_dev->vdev.dev.release = virtio_pci_release_dev; |
| 441 | vp_dev->pci_dev = pci_dev; |
Michael S. Tsirkin | ff31d2e | 2015-01-13 11:23:32 +0200 | [diff] [blame] | 442 | |
Michael S. Tsirkin | ff31d2e | 2015-01-13 11:23:32 +0200 | [diff] [blame] | 443 | /* enable the device */ |
| 444 | rc = pci_enable_device(pci_dev); |
| 445 | if (rc) |
| 446 | goto err_enable_device; |
| 447 | |
Michael S. Tsirkin | ac399d8 | 2015-01-15 17:54:13 +0200 | [diff] [blame] | 448 | if (force_legacy) { |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 449 | rc = virtio_pci_legacy_probe(vp_dev); |
Michael S. Tsirkin | ac399d8 | 2015-01-15 17:54:13 +0200 | [diff] [blame] | 450 | /* Also try modern mode if we can't map BAR0 (no IO space). */ |
| 451 | if (rc == -ENODEV || rc == -ENOMEM) |
| 452 | rc = virtio_pci_modern_probe(vp_dev); |
| 453 | if (rc) |
| 454 | goto err_probe; |
| 455 | } else { |
| 456 | rc = virtio_pci_modern_probe(vp_dev); |
| 457 | if (rc == -ENODEV) |
| 458 | rc = virtio_pci_legacy_probe(vp_dev); |
| 459 | if (rc) |
| 460 | goto err_probe; |
| 461 | } |
Michael S. Tsirkin | ff31d2e | 2015-01-13 11:23:32 +0200 | [diff] [blame] | 462 | |
| 463 | pci_set_master(pci_dev); |
| 464 | |
| 465 | rc = register_virtio_device(&vp_dev->vdev); |
| 466 | if (rc) |
| 467 | goto err_register; |
| 468 | |
| 469 | return 0; |
| 470 | |
| 471 | err_register: |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 472 | if (vp_dev->ioaddr) |
| 473 | virtio_pci_legacy_remove(vp_dev); |
| 474 | else |
| 475 | virtio_pci_modern_remove(vp_dev); |
Michael S. Tsirkin | ff31d2e | 2015-01-13 11:23:32 +0200 | [diff] [blame] | 476 | err_probe: |
Michael S. Tsirkin | ff31d2e | 2015-01-13 11:23:32 +0200 | [diff] [blame] | 477 | pci_disable_device(pci_dev); |
| 478 | err_enable_device: |
| 479 | kfree(vp_dev); |
| 480 | return rc; |
Michael S. Tsirkin | 9a4253d | 2014-12-11 21:47:49 +0200 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | static void virtio_pci_remove(struct pci_dev *pci_dev) |
| 484 | { |
Michael S. Tsirkin | ff31d2e | 2015-01-13 11:23:32 +0200 | [diff] [blame] | 485 | struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev); |
Michael S. Tsirkin | 2989be0 | 2016-01-14 16:00:41 +0200 | [diff] [blame] | 486 | struct device *dev = get_device(&vp_dev->vdev.dev); |
Michael S. Tsirkin | ff31d2e | 2015-01-13 11:23:32 +0200 | [diff] [blame] | 487 | |
| 488 | unregister_virtio_device(&vp_dev->vdev); |
| 489 | |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 490 | if (vp_dev->ioaddr) |
| 491 | virtio_pci_legacy_remove(vp_dev); |
| 492 | else |
| 493 | virtio_pci_modern_remove(vp_dev); |
Michael S. Tsirkin | ff31d2e | 2015-01-13 11:23:32 +0200 | [diff] [blame] | 494 | |
Michael S. Tsirkin | ff31d2e | 2015-01-13 11:23:32 +0200 | [diff] [blame] | 495 | pci_disable_device(pci_dev); |
Michael S. Tsirkin | 2989be0 | 2016-01-14 16:00:41 +0200 | [diff] [blame] | 496 | put_device(dev); |
Michael S. Tsirkin | 9a4253d | 2014-12-11 21:47:49 +0200 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | static struct pci_driver virtio_pci_driver = { |
| 500 | .name = "virtio-pci", |
| 501 | .id_table = virtio_pci_id_table, |
| 502 | .probe = virtio_pci_probe, |
| 503 | .remove = virtio_pci_remove, |
| 504 | #ifdef CONFIG_PM_SLEEP |
| 505 | .driver.pm = &virtio_pci_pm_ops, |
| 506 | #endif |
| 507 | }; |
| 508 | |
| 509 | module_pci_driver(virtio_pci_driver); |
Herbert Xu | 5ff1611 | 2014-12-17 00:54:03 +0200 | [diff] [blame] | 510 | |
| 511 | MODULE_AUTHOR("Anthony Liguori <aliguori@us.ibm.com>"); |
| 512 | MODULE_DESCRIPTION("virtio-pci"); |
| 513 | MODULE_LICENSE("GPL"); |
| 514 | MODULE_VERSION("1"); |