Michael S. Tsirkin | 38eb4a2 | 2014-12-07 18:41:16 +0200 | [diff] [blame] | 1 | /* |
Michael S. Tsirkin | a90fdce | 2014-12-08 12:31:02 +0200 | [diff] [blame] | 2 | * Virtio PCI driver - legacy device support |
Michael S. Tsirkin | 38eb4a2 | 2014-12-07 18:41:16 +0200 | [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 |
Michael S. Tsirkin | 38eb4a2 | 2014-12-07 18:41:16 +0200 | [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> |
Michael S. Tsirkin | 38eb4a2 | 2014-12-07 18:41:16 +0200 | [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" |
Michael S. Tsirkin | 38eb4a2 | 2014-12-07 18:41:16 +0200 | [diff] [blame] | 21 | |
| 22 | /* Qumranet donated their vendor ID for devices 0x1000 thru 0x10FF. */ |
| 23 | static const struct pci_device_id virtio_pci_id_table[] = { |
| 24 | { PCI_DEVICE(0x1af4, PCI_ANY_ID) }, |
| 25 | { 0 } |
| 26 | }; |
| 27 | |
| 28 | MODULE_DEVICE_TABLE(pci, virtio_pci_id_table); |
| 29 | |
| 30 | /* virtio config->get_features() implementation */ |
| 31 | static u64 vp_get_features(struct virtio_device *vdev) |
| 32 | { |
| 33 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
| 34 | |
| 35 | /* When someone needs more than 32 feature bits, we'll need to |
| 36 | * steal a bit to indicate that the rest are somewhere else. */ |
| 37 | return ioread32(vp_dev->ioaddr + VIRTIO_PCI_HOST_FEATURES); |
| 38 | } |
| 39 | |
| 40 | /* virtio config->finalize_features() implementation */ |
| 41 | static int vp_finalize_features(struct virtio_device *vdev) |
| 42 | { |
| 43 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
| 44 | |
| 45 | /* Give virtio_ring a chance to accept features. */ |
| 46 | vring_transport_features(vdev); |
| 47 | |
| 48 | /* Make sure we don't have any features > 32 bits! */ |
| 49 | BUG_ON((u32)vdev->features != vdev->features); |
| 50 | |
| 51 | /* We only support 32 feature bits. */ |
| 52 | iowrite32(vdev->features, vp_dev->ioaddr + VIRTIO_PCI_GUEST_FEATURES); |
| 53 | |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | /* virtio config->get() implementation */ |
| 58 | static void vp_get(struct virtio_device *vdev, unsigned offset, |
| 59 | void *buf, unsigned len) |
| 60 | { |
| 61 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
| 62 | void __iomem *ioaddr = vp_dev->ioaddr + |
| 63 | VIRTIO_PCI_CONFIG(vp_dev) + offset; |
| 64 | u8 *ptr = buf; |
| 65 | int i; |
| 66 | |
| 67 | for (i = 0; i < len; i++) |
| 68 | ptr[i] = ioread8(ioaddr + i); |
| 69 | } |
| 70 | |
| 71 | /* the config->set() implementation. it's symmetric to the config->get() |
| 72 | * implementation */ |
| 73 | static void vp_set(struct virtio_device *vdev, unsigned offset, |
| 74 | const void *buf, unsigned len) |
| 75 | { |
| 76 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
| 77 | void __iomem *ioaddr = vp_dev->ioaddr + |
| 78 | VIRTIO_PCI_CONFIG(vp_dev) + offset; |
| 79 | const u8 *ptr = buf; |
| 80 | int i; |
| 81 | |
| 82 | for (i = 0; i < len; i++) |
| 83 | iowrite8(ptr[i], ioaddr + i); |
| 84 | } |
| 85 | |
| 86 | /* config->{get,set}_status() implementations */ |
| 87 | static u8 vp_get_status(struct virtio_device *vdev) |
| 88 | { |
| 89 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
| 90 | return ioread8(vp_dev->ioaddr + VIRTIO_PCI_STATUS); |
| 91 | } |
| 92 | |
| 93 | static void vp_set_status(struct virtio_device *vdev, u8 status) |
| 94 | { |
| 95 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
| 96 | /* We should never be setting status to 0. */ |
| 97 | BUG_ON(status == 0); |
| 98 | iowrite8(status, vp_dev->ioaddr + VIRTIO_PCI_STATUS); |
| 99 | } |
| 100 | |
| 101 | static void vp_reset(struct virtio_device *vdev) |
| 102 | { |
| 103 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
| 104 | /* 0 status means a reset. */ |
| 105 | iowrite8(0, vp_dev->ioaddr + VIRTIO_PCI_STATUS); |
| 106 | /* Flush out the status write, and flush in device writes, |
| 107 | * including MSi-X interrupts, if any. */ |
| 108 | ioread8(vp_dev->ioaddr + VIRTIO_PCI_STATUS); |
| 109 | /* Flush pending VQ/configuration callbacks. */ |
| 110 | vp_synchronize_vectors(vdev); |
| 111 | } |
| 112 | |
| 113 | static u16 vp_config_vector(struct virtio_pci_device *vp_dev, u16 vector) |
| 114 | { |
| 115 | /* Setup the vector used for configuration events */ |
| 116 | iowrite16(vector, vp_dev->ioaddr + VIRTIO_MSI_CONFIG_VECTOR); |
| 117 | /* Verify we had enough resources to assign the vector */ |
| 118 | /* Will also flush the write out to device */ |
| 119 | return ioread16(vp_dev->ioaddr + VIRTIO_MSI_CONFIG_VECTOR); |
| 120 | } |
| 121 | |
| 122 | static struct virtqueue *setup_vq(struct virtio_pci_device *vp_dev, |
| 123 | struct virtio_pci_vq_info *info, |
| 124 | unsigned index, |
| 125 | void (*callback)(struct virtqueue *vq), |
| 126 | const char *name, |
| 127 | u16 msix_vec) |
| 128 | { |
| 129 | struct virtqueue *vq; |
| 130 | unsigned long size; |
| 131 | u16 num; |
| 132 | int err; |
| 133 | |
| 134 | /* Select the queue we're interested in */ |
| 135 | iowrite16(index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_SEL); |
| 136 | |
| 137 | /* Check if queue is either not available or already active. */ |
| 138 | num = ioread16(vp_dev->ioaddr + VIRTIO_PCI_QUEUE_NUM); |
| 139 | if (!num || ioread32(vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN)) |
| 140 | return ERR_PTR(-ENOENT); |
| 141 | |
| 142 | info->num = num; |
| 143 | info->msix_vector = msix_vec; |
| 144 | |
| 145 | size = PAGE_ALIGN(vring_size(num, VIRTIO_PCI_VRING_ALIGN)); |
| 146 | info->queue = alloc_pages_exact(size, GFP_KERNEL|__GFP_ZERO); |
| 147 | if (info->queue == NULL) |
| 148 | return ERR_PTR(-ENOMEM); |
| 149 | |
| 150 | /* activate the queue */ |
| 151 | iowrite32(virt_to_phys(info->queue) >> VIRTIO_PCI_QUEUE_ADDR_SHIFT, |
| 152 | vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN); |
| 153 | |
| 154 | /* create the vring */ |
| 155 | vq = vring_new_virtqueue(index, info->num, |
| 156 | VIRTIO_PCI_VRING_ALIGN, &vp_dev->vdev, |
| 157 | true, info->queue, vp_notify, callback, name); |
| 158 | if (!vq) { |
| 159 | err = -ENOMEM; |
| 160 | goto out_activate_queue; |
| 161 | } |
| 162 | |
| 163 | vq->priv = (void __force *)vp_dev->ioaddr + VIRTIO_PCI_QUEUE_NOTIFY; |
| 164 | |
| 165 | if (msix_vec != VIRTIO_MSI_NO_VECTOR) { |
| 166 | iowrite16(msix_vec, vp_dev->ioaddr + VIRTIO_MSI_QUEUE_VECTOR); |
| 167 | msix_vec = ioread16(vp_dev->ioaddr + VIRTIO_MSI_QUEUE_VECTOR); |
| 168 | if (msix_vec == VIRTIO_MSI_NO_VECTOR) { |
| 169 | err = -EBUSY; |
| 170 | goto out_assign; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | return vq; |
| 175 | |
| 176 | out_assign: |
| 177 | vring_del_virtqueue(vq); |
| 178 | out_activate_queue: |
| 179 | iowrite32(0, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN); |
| 180 | free_pages_exact(info->queue, size); |
| 181 | return ERR_PTR(err); |
| 182 | } |
| 183 | |
| 184 | static void del_vq(struct virtio_pci_vq_info *info) |
| 185 | { |
| 186 | struct virtqueue *vq = info->vq; |
| 187 | struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev); |
| 188 | unsigned long size; |
| 189 | |
| 190 | iowrite16(vq->index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_SEL); |
| 191 | |
| 192 | if (vp_dev->msix_enabled) { |
| 193 | iowrite16(VIRTIO_MSI_NO_VECTOR, |
| 194 | vp_dev->ioaddr + VIRTIO_MSI_QUEUE_VECTOR); |
| 195 | /* Flush the write out to device */ |
| 196 | ioread8(vp_dev->ioaddr + VIRTIO_PCI_ISR); |
| 197 | } |
| 198 | |
| 199 | vring_del_virtqueue(vq); |
| 200 | |
| 201 | /* Select and deactivate the queue */ |
| 202 | iowrite32(0, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN); |
| 203 | |
| 204 | size = PAGE_ALIGN(vring_size(info->num, VIRTIO_PCI_VRING_ALIGN)); |
| 205 | free_pages_exact(info->queue, size); |
| 206 | } |
| 207 | |
| 208 | static const struct virtio_config_ops virtio_pci_config_ops = { |
| 209 | .get = vp_get, |
| 210 | .set = vp_set, |
| 211 | .get_status = vp_get_status, |
| 212 | .set_status = vp_set_status, |
| 213 | .reset = vp_reset, |
| 214 | .find_vqs = vp_find_vqs, |
| 215 | .del_vqs = vp_del_vqs, |
| 216 | .get_features = vp_get_features, |
| 217 | .finalize_features = vp_finalize_features, |
| 218 | .bus_name = vp_bus_name, |
| 219 | .set_vq_affinity = vp_set_vq_affinity, |
| 220 | }; |
| 221 | |
| 222 | /* the PCI probing function */ |
| 223 | static int virtio_pci_probe(struct pci_dev *pci_dev, |
| 224 | const struct pci_device_id *id) |
| 225 | { |
| 226 | struct virtio_pci_device *vp_dev; |
| 227 | int err; |
| 228 | |
| 229 | /* We only own devices >= 0x1000 and <= 0x103f: leave the rest. */ |
| 230 | if (pci_dev->device < 0x1000 || pci_dev->device > 0x103f) |
| 231 | return -ENODEV; |
| 232 | |
| 233 | if (pci_dev->revision != VIRTIO_PCI_ABI_VERSION) { |
| 234 | printk(KERN_ERR "virtio_pci: expected ABI version %d, got %d\n", |
| 235 | VIRTIO_PCI_ABI_VERSION, pci_dev->revision); |
| 236 | return -ENODEV; |
| 237 | } |
| 238 | |
| 239 | /* allocate our structure and fill it out */ |
| 240 | vp_dev = kzalloc(sizeof(struct virtio_pci_device), GFP_KERNEL); |
| 241 | if (vp_dev == NULL) |
| 242 | return -ENOMEM; |
| 243 | |
| 244 | vp_dev->vdev.dev.parent = &pci_dev->dev; |
| 245 | vp_dev->vdev.dev.release = virtio_pci_release_dev; |
| 246 | vp_dev->vdev.config = &virtio_pci_config_ops; |
| 247 | vp_dev->pci_dev = pci_dev; |
| 248 | INIT_LIST_HEAD(&vp_dev->virtqueues); |
| 249 | spin_lock_init(&vp_dev->lock); |
| 250 | |
| 251 | /* Disable MSI/MSIX to bring device to a known good state. */ |
| 252 | pci_msi_off(pci_dev); |
| 253 | |
| 254 | /* enable the device */ |
| 255 | err = pci_enable_device(pci_dev); |
| 256 | if (err) |
| 257 | goto out; |
| 258 | |
| 259 | err = pci_request_regions(pci_dev, "virtio-pci"); |
| 260 | if (err) |
| 261 | goto out_enable_device; |
| 262 | |
| 263 | vp_dev->ioaddr = pci_iomap(pci_dev, 0, 0); |
| 264 | if (vp_dev->ioaddr == NULL) { |
| 265 | err = -ENOMEM; |
| 266 | goto out_req_regions; |
| 267 | } |
| 268 | |
| 269 | vp_dev->isr = vp_dev->ioaddr + VIRTIO_PCI_ISR; |
| 270 | |
| 271 | pci_set_drvdata(pci_dev, vp_dev); |
| 272 | pci_set_master(pci_dev); |
| 273 | |
| 274 | /* we use the subsystem vendor/device id as the virtio vendor/device |
| 275 | * id. this allows us to use the same PCI vendor/device id for all |
| 276 | * virtio devices and to identify the particular virtio driver by |
| 277 | * the subsystem ids */ |
| 278 | vp_dev->vdev.id.vendor = pci_dev->subsystem_vendor; |
| 279 | vp_dev->vdev.id.device = pci_dev->subsystem_device; |
| 280 | |
| 281 | vp_dev->config_vector = vp_config_vector; |
| 282 | vp_dev->setup_vq = setup_vq; |
| 283 | vp_dev->del_vq = del_vq; |
| 284 | |
| 285 | /* finally register the virtio device */ |
| 286 | err = register_virtio_device(&vp_dev->vdev); |
| 287 | if (err) |
| 288 | goto out_set_drvdata; |
| 289 | |
| 290 | return 0; |
| 291 | |
| 292 | out_set_drvdata: |
| 293 | pci_iounmap(pci_dev, vp_dev->ioaddr); |
| 294 | out_req_regions: |
| 295 | pci_release_regions(pci_dev); |
| 296 | out_enable_device: |
| 297 | pci_disable_device(pci_dev); |
| 298 | out: |
| 299 | kfree(vp_dev); |
| 300 | return err; |
| 301 | } |
| 302 | |
| 303 | static void virtio_pci_remove(struct pci_dev *pci_dev) |
| 304 | { |
| 305 | struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev); |
| 306 | |
| 307 | unregister_virtio_device(&vp_dev->vdev); |
| 308 | |
| 309 | vp_del_vqs(&vp_dev->vdev); |
| 310 | pci_iounmap(pci_dev, vp_dev->ioaddr); |
| 311 | pci_release_regions(pci_dev); |
| 312 | pci_disable_device(pci_dev); |
| 313 | kfree(vp_dev); |
| 314 | } |
| 315 | |
| 316 | static struct pci_driver virtio_pci_driver = { |
| 317 | .name = "virtio-pci", |
| 318 | .id_table = virtio_pci_id_table, |
| 319 | .probe = virtio_pci_probe, |
| 320 | .remove = virtio_pci_remove, |
| 321 | #ifdef CONFIG_PM_SLEEP |
| 322 | .driver.pm = &virtio_pci_pm_ops, |
| 323 | #endif |
| 324 | }; |
| 325 | |
| 326 | module_pci_driver(virtio_pci_driver); |