Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Virtio PCI driver - modern (virtio 1.0) device support |
| 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 |
| 8 | * Copyright Red Hat, Inc. 2014 |
| 9 | * |
| 10 | * Authors: |
| 11 | * Anthony Liguori <aliguori@us.ibm.com> |
| 12 | * Rusty Russell <rusty@rustcorp.com.au> |
| 13 | * Michael S. Tsirkin <mst@redhat.com> |
| 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 | |
| 20 | #define VIRTIO_PCI_NO_LEGACY |
| 21 | #include "virtio_pci_common.h" |
| 22 | |
Michael S. Tsirkin | c5d4c2c | 2015-04-01 13:33:20 +1030 | [diff] [blame^] | 23 | /* |
| 24 | * Type-safe wrappers for io accesses. |
| 25 | * Use these to enforce at compile time the following spec requirement: |
| 26 | * |
| 27 | * The driver MUST access each field using the “natural” access |
| 28 | * method, i.e. 32-bit accesses for 32-bit fields, 16-bit accesses |
| 29 | * for 16-bit fields and 8-bit accesses for 8-bit fields. |
| 30 | */ |
| 31 | static inline u8 vp_ioread8(u8 __iomem *addr) |
| 32 | { |
| 33 | return ioread8(addr); |
| 34 | } |
| 35 | static inline u16 vp_ioread16 (u16 __iomem *addr) |
| 36 | { |
| 37 | return ioread16(addr); |
| 38 | } |
| 39 | |
| 40 | static inline u32 vp_ioread32(u32 __iomem *addr) |
| 41 | { |
| 42 | return ioread32(addr); |
| 43 | } |
| 44 | |
| 45 | static inline void vp_iowrite8(u8 value, u8 __iomem *addr) |
| 46 | { |
| 47 | iowrite8(value, addr); |
| 48 | } |
| 49 | |
| 50 | static inline void vp_iowrite16(u16 value, u16 __iomem *addr) |
| 51 | { |
| 52 | iowrite16(value, addr); |
| 53 | } |
| 54 | |
| 55 | static inline void vp_iowrite32(u32 value, u32 __iomem *addr) |
| 56 | { |
| 57 | iowrite32(value, addr); |
| 58 | } |
| 59 | |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 60 | static void __iomem *map_capability(struct pci_dev *dev, int off, |
| 61 | size_t minlen, |
| 62 | u32 align, |
| 63 | u32 start, u32 size, |
| 64 | size_t *len) |
| 65 | { |
| 66 | u8 bar; |
| 67 | u32 offset, length; |
| 68 | void __iomem *p; |
| 69 | |
| 70 | pci_read_config_byte(dev, off + offsetof(struct virtio_pci_cap, |
| 71 | bar), |
| 72 | &bar); |
| 73 | pci_read_config_dword(dev, off + offsetof(struct virtio_pci_cap, offset), |
| 74 | &offset); |
| 75 | pci_read_config_dword(dev, off + offsetof(struct virtio_pci_cap, length), |
| 76 | &length); |
| 77 | |
| 78 | if (length <= start) { |
| 79 | dev_err(&dev->dev, |
| 80 | "virtio_pci: bad capability len %u (>%u expected)\n", |
| 81 | length, start); |
| 82 | return NULL; |
| 83 | } |
| 84 | |
| 85 | if (length - start < minlen) { |
| 86 | dev_err(&dev->dev, |
| 87 | "virtio_pci: bad capability len %u (>=%zu expected)\n", |
| 88 | length, minlen); |
| 89 | return NULL; |
| 90 | } |
| 91 | |
| 92 | length -= start; |
| 93 | |
| 94 | if (start + offset < offset) { |
| 95 | dev_err(&dev->dev, |
| 96 | "virtio_pci: map wrap-around %u+%u\n", |
| 97 | start, offset); |
| 98 | return NULL; |
| 99 | } |
| 100 | |
| 101 | offset += start; |
| 102 | |
| 103 | if (offset & (align - 1)) { |
| 104 | dev_err(&dev->dev, |
| 105 | "virtio_pci: offset %u not aligned to %u\n", |
| 106 | offset, align); |
| 107 | return NULL; |
| 108 | } |
| 109 | |
| 110 | if (length > size) |
| 111 | length = size; |
| 112 | |
| 113 | if (len) |
| 114 | *len = length; |
| 115 | |
| 116 | if (minlen + offset < minlen || |
| 117 | minlen + offset > pci_resource_len(dev, bar)) { |
| 118 | dev_err(&dev->dev, |
| 119 | "virtio_pci: map virtio %zu@%u " |
| 120 | "out of range on bar %i length %lu\n", |
| 121 | minlen, offset, |
| 122 | bar, (unsigned long)pci_resource_len(dev, bar)); |
| 123 | return NULL; |
| 124 | } |
| 125 | |
| 126 | p = pci_iomap_range(dev, bar, offset, length); |
| 127 | if (!p) |
| 128 | dev_err(&dev->dev, |
| 129 | "virtio_pci: unable to map virtio %u@%u on bar %i\n", |
| 130 | length, offset, bar); |
| 131 | return p; |
| 132 | } |
| 133 | |
| 134 | static void iowrite64_twopart(u64 val, __le32 __iomem *lo, __le32 __iomem *hi) |
| 135 | { |
| 136 | iowrite32((u32)val, lo); |
| 137 | iowrite32(val >> 32, hi); |
| 138 | } |
| 139 | |
| 140 | /* virtio config->get_features() implementation */ |
| 141 | static u64 vp_get_features(struct virtio_device *vdev) |
| 142 | { |
| 143 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
| 144 | u64 features; |
| 145 | |
| 146 | iowrite32(0, &vp_dev->common->device_feature_select); |
| 147 | features = ioread32(&vp_dev->common->device_feature); |
| 148 | iowrite32(1, &vp_dev->common->device_feature_select); |
| 149 | features |= ((u64)ioread32(&vp_dev->common->device_feature) << 32); |
| 150 | |
| 151 | return features; |
| 152 | } |
| 153 | |
| 154 | /* virtio config->finalize_features() implementation */ |
| 155 | static int vp_finalize_features(struct virtio_device *vdev) |
| 156 | { |
| 157 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
| 158 | |
| 159 | /* Give virtio_ring a chance to accept features. */ |
| 160 | vring_transport_features(vdev); |
| 161 | |
| 162 | if (!__virtio_test_bit(vdev, VIRTIO_F_VERSION_1)) { |
| 163 | dev_err(&vdev->dev, "virtio: device uses modern interface " |
| 164 | "but does not have VIRTIO_F_VERSION_1\n"); |
| 165 | return -EINVAL; |
| 166 | } |
| 167 | |
| 168 | iowrite32(0, &vp_dev->common->guest_feature_select); |
| 169 | iowrite32((u32)vdev->features, &vp_dev->common->guest_feature); |
| 170 | iowrite32(1, &vp_dev->common->guest_feature_select); |
| 171 | iowrite32(vdev->features >> 32, &vp_dev->common->guest_feature); |
| 172 | |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | /* virtio config->get() implementation */ |
| 177 | static void vp_get(struct virtio_device *vdev, unsigned offset, |
| 178 | void *buf, unsigned len) |
| 179 | { |
| 180 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
| 181 | u8 b; |
| 182 | __le16 w; |
| 183 | __le32 l; |
| 184 | |
| 185 | BUG_ON(offset + len > vp_dev->device_len); |
| 186 | |
| 187 | switch (len) { |
| 188 | case 1: |
| 189 | b = ioread8(vp_dev->device + offset); |
| 190 | memcpy(buf, &b, sizeof b); |
| 191 | break; |
| 192 | case 2: |
| 193 | w = cpu_to_le16(ioread16(vp_dev->device + offset)); |
| 194 | memcpy(buf, &w, sizeof w); |
| 195 | break; |
| 196 | case 4: |
| 197 | l = cpu_to_le32(ioread32(vp_dev->device + offset)); |
| 198 | memcpy(buf, &l, sizeof l); |
| 199 | break; |
| 200 | case 8: |
| 201 | l = cpu_to_le32(ioread32(vp_dev->device + offset)); |
| 202 | memcpy(buf, &l, sizeof l); |
| 203 | l = cpu_to_le32(ioread32(vp_dev->device + offset + sizeof l)); |
| 204 | memcpy(buf + sizeof l, &l, sizeof l); |
| 205 | break; |
| 206 | default: |
| 207 | BUG(); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | /* the config->set() implementation. it's symmetric to the config->get() |
| 212 | * implementation */ |
| 213 | static void vp_set(struct virtio_device *vdev, unsigned offset, |
| 214 | const void *buf, unsigned len) |
| 215 | { |
| 216 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
| 217 | u8 b; |
| 218 | __le16 w; |
| 219 | __le32 l; |
| 220 | |
| 221 | BUG_ON(offset + len > vp_dev->device_len); |
| 222 | |
| 223 | switch (len) { |
| 224 | case 1: |
| 225 | memcpy(&b, buf, sizeof b); |
| 226 | iowrite8(b, vp_dev->device + offset); |
| 227 | break; |
| 228 | case 2: |
| 229 | memcpy(&w, buf, sizeof w); |
| 230 | iowrite16(le16_to_cpu(w), vp_dev->device + offset); |
| 231 | break; |
| 232 | case 4: |
| 233 | memcpy(&l, buf, sizeof l); |
| 234 | iowrite32(le32_to_cpu(l), vp_dev->device + offset); |
| 235 | break; |
| 236 | case 8: |
| 237 | memcpy(&l, buf, sizeof l); |
| 238 | iowrite32(le32_to_cpu(l), vp_dev->device + offset); |
| 239 | memcpy(&l, buf + sizeof l, sizeof l); |
| 240 | iowrite32(le32_to_cpu(l), vp_dev->device + offset + sizeof l); |
| 241 | break; |
| 242 | default: |
| 243 | BUG(); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | static u32 vp_generation(struct virtio_device *vdev) |
| 248 | { |
| 249 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
| 250 | return ioread8(&vp_dev->common->config_generation); |
| 251 | } |
| 252 | |
| 253 | /* config->{get,set}_status() implementations */ |
| 254 | static u8 vp_get_status(struct virtio_device *vdev) |
| 255 | { |
| 256 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
| 257 | return ioread8(&vp_dev->common->device_status); |
| 258 | } |
| 259 | |
| 260 | static void vp_set_status(struct virtio_device *vdev, u8 status) |
| 261 | { |
| 262 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
| 263 | /* We should never be setting status to 0. */ |
| 264 | BUG_ON(status == 0); |
| 265 | iowrite8(status, &vp_dev->common->device_status); |
| 266 | } |
| 267 | |
| 268 | static void vp_reset(struct virtio_device *vdev) |
| 269 | { |
| 270 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
| 271 | /* 0 status means a reset. */ |
| 272 | iowrite8(0, &vp_dev->common->device_status); |
| 273 | /* Flush out the status write, and flush in device writes, |
| 274 | * including MSI-X interrupts, if any. */ |
| 275 | ioread8(&vp_dev->common->device_status); |
| 276 | /* Flush pending VQ/configuration callbacks. */ |
| 277 | vp_synchronize_vectors(vdev); |
| 278 | } |
| 279 | |
| 280 | static u16 vp_config_vector(struct virtio_pci_device *vp_dev, u16 vector) |
| 281 | { |
| 282 | /* Setup the vector used for configuration events */ |
| 283 | iowrite16(vector, &vp_dev->common->msix_config); |
| 284 | /* Verify we had enough resources to assign the vector */ |
| 285 | /* Will also flush the write out to device */ |
| 286 | return ioread16(&vp_dev->common->msix_config); |
| 287 | } |
| 288 | |
| 289 | static size_t vring_pci_size(u16 num) |
| 290 | { |
| 291 | /* We only need a cacheline separation. */ |
| 292 | return PAGE_ALIGN(vring_size(num, SMP_CACHE_BYTES)); |
| 293 | } |
| 294 | |
| 295 | static void *alloc_virtqueue_pages(int *num) |
| 296 | { |
| 297 | void *pages; |
| 298 | |
| 299 | /* TODO: allocate each queue chunk individually */ |
| 300 | for (; *num && vring_pci_size(*num) > PAGE_SIZE; *num /= 2) { |
| 301 | pages = alloc_pages_exact(vring_pci_size(*num), |
| 302 | GFP_KERNEL|__GFP_ZERO|__GFP_NOWARN); |
| 303 | if (pages) |
| 304 | return pages; |
| 305 | } |
| 306 | |
| 307 | if (!*num) |
| 308 | return NULL; |
| 309 | |
| 310 | /* Try to get a single page. You are my only hope! */ |
| 311 | return alloc_pages_exact(vring_pci_size(*num), GFP_KERNEL|__GFP_ZERO); |
| 312 | } |
| 313 | |
| 314 | static struct virtqueue *setup_vq(struct virtio_pci_device *vp_dev, |
| 315 | struct virtio_pci_vq_info *info, |
| 316 | unsigned index, |
| 317 | void (*callback)(struct virtqueue *vq), |
| 318 | const char *name, |
| 319 | u16 msix_vec) |
| 320 | { |
| 321 | struct virtio_pci_common_cfg __iomem *cfg = vp_dev->common; |
| 322 | struct virtqueue *vq; |
| 323 | u16 num, off; |
| 324 | int err; |
| 325 | |
| 326 | if (index >= ioread16(&cfg->num_queues)) |
| 327 | return ERR_PTR(-ENOENT); |
| 328 | |
| 329 | /* Select the queue we're interested in */ |
| 330 | iowrite16(index, &cfg->queue_select); |
| 331 | |
| 332 | /* Check if queue is either not available or already active. */ |
| 333 | num = ioread16(&cfg->queue_size); |
Rusty Russell | 7abb568 | 2015-02-11 15:01:14 +1030 | [diff] [blame] | 334 | if (!num || ioread16(&cfg->queue_enable)) |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 335 | return ERR_PTR(-ENOENT); |
| 336 | |
| 337 | if (num & (num - 1)) { |
| 338 | dev_warn(&vp_dev->pci_dev->dev, "bad queue size %u", num); |
| 339 | return ERR_PTR(-EINVAL); |
| 340 | } |
| 341 | |
| 342 | /* get offset of notification word for this vq */ |
| 343 | off = ioread16(&cfg->queue_notify_off); |
| 344 | |
| 345 | info->num = num; |
| 346 | info->msix_vector = msix_vec; |
| 347 | |
| 348 | info->queue = alloc_virtqueue_pages(&info->num); |
| 349 | if (info->queue == NULL) |
| 350 | return ERR_PTR(-ENOMEM); |
| 351 | |
| 352 | /* create the vring */ |
| 353 | vq = vring_new_virtqueue(index, info->num, |
| 354 | SMP_CACHE_BYTES, &vp_dev->vdev, |
| 355 | true, info->queue, vp_notify, callback, name); |
| 356 | if (!vq) { |
| 357 | err = -ENOMEM; |
| 358 | goto err_new_queue; |
| 359 | } |
| 360 | |
| 361 | /* activate the queue */ |
| 362 | iowrite16(num, &cfg->queue_size); |
| 363 | iowrite64_twopart(virt_to_phys(info->queue), |
| 364 | &cfg->queue_desc_lo, &cfg->queue_desc_hi); |
| 365 | iowrite64_twopart(virt_to_phys(virtqueue_get_avail(vq)), |
| 366 | &cfg->queue_avail_lo, &cfg->queue_avail_hi); |
| 367 | iowrite64_twopart(virt_to_phys(virtqueue_get_used(vq)), |
| 368 | &cfg->queue_used_lo, &cfg->queue_used_hi); |
| 369 | |
Michael S. Tsirkin | 3909213 | 2015-01-14 18:50:55 +0200 | [diff] [blame] | 370 | if (vp_dev->notify_base) { |
| 371 | /* offset should not wrap */ |
| 372 | if ((u64)off * vp_dev->notify_offset_multiplier + 2 |
| 373 | > vp_dev->notify_len) { |
| 374 | dev_warn(&vp_dev->pci_dev->dev, |
| 375 | "bad notification offset %u (x %u) " |
| 376 | "for queue %u > %zd", |
| 377 | off, vp_dev->notify_offset_multiplier, |
| 378 | index, vp_dev->notify_len); |
| 379 | err = -EINVAL; |
| 380 | goto err_map_notify; |
| 381 | } |
| 382 | vq->priv = (void __force *)vp_dev->notify_base + |
| 383 | off * vp_dev->notify_offset_multiplier; |
| 384 | } else { |
| 385 | vq->priv = (void __force *)map_capability(vp_dev->pci_dev, |
| 386 | vp_dev->notify_map_cap, 2, 2, |
| 387 | off * vp_dev->notify_offset_multiplier, 2, |
| 388 | NULL); |
| 389 | } |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 390 | |
| 391 | if (!vq->priv) { |
| 392 | err = -ENOMEM; |
| 393 | goto err_map_notify; |
| 394 | } |
| 395 | |
| 396 | if (msix_vec != VIRTIO_MSI_NO_VECTOR) { |
| 397 | iowrite16(msix_vec, &cfg->queue_msix_vector); |
| 398 | msix_vec = ioread16(&cfg->queue_msix_vector); |
| 399 | if (msix_vec == VIRTIO_MSI_NO_VECTOR) { |
| 400 | err = -EBUSY; |
| 401 | goto err_assign_vector; |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | return vq; |
| 406 | |
| 407 | err_assign_vector: |
Michael S. Tsirkin | 3909213 | 2015-01-14 18:50:55 +0200 | [diff] [blame] | 408 | if (!vp_dev->notify_base) |
| 409 | pci_iounmap(vp_dev->pci_dev, (void __iomem __force *)vq->priv); |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 410 | err_map_notify: |
| 411 | vring_del_virtqueue(vq); |
| 412 | err_new_queue: |
| 413 | free_pages_exact(info->queue, vring_pci_size(info->num)); |
| 414 | return ERR_PTR(err); |
| 415 | } |
| 416 | |
| 417 | static int vp_modern_find_vqs(struct virtio_device *vdev, unsigned nvqs, |
| 418 | struct virtqueue *vqs[], |
| 419 | vq_callback_t *callbacks[], |
| 420 | const char *names[]) |
| 421 | { |
| 422 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
| 423 | struct virtqueue *vq; |
| 424 | int rc = vp_find_vqs(vdev, nvqs, vqs, callbacks, names); |
| 425 | |
| 426 | if (rc) |
| 427 | return rc; |
| 428 | |
| 429 | /* Select and activate all queues. Has to be done last: once we do |
| 430 | * this, there's no way to go back except reset. |
| 431 | */ |
| 432 | list_for_each_entry(vq, &vdev->vqs, list) { |
| 433 | iowrite16(vq->index, &vp_dev->common->queue_select); |
Rusty Russell | 7abb568 | 2015-02-11 15:01:14 +1030 | [diff] [blame] | 434 | iowrite16(1, &vp_dev->common->queue_enable); |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | return 0; |
| 438 | } |
| 439 | |
| 440 | static void del_vq(struct virtio_pci_vq_info *info) |
| 441 | { |
| 442 | struct virtqueue *vq = info->vq; |
| 443 | struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev); |
| 444 | |
| 445 | iowrite16(vq->index, &vp_dev->common->queue_select); |
| 446 | |
| 447 | if (vp_dev->msix_enabled) { |
| 448 | iowrite16(VIRTIO_MSI_NO_VECTOR, |
| 449 | &vp_dev->common->queue_msix_vector); |
| 450 | /* Flush the write out to device */ |
| 451 | ioread16(&vp_dev->common->queue_msix_vector); |
| 452 | } |
| 453 | |
Michael S. Tsirkin | 3909213 | 2015-01-14 18:50:55 +0200 | [diff] [blame] | 454 | if (!vp_dev->notify_base) |
| 455 | pci_iounmap(vp_dev->pci_dev, (void __force __iomem *)vq->priv); |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 456 | |
| 457 | vring_del_virtqueue(vq); |
| 458 | |
| 459 | free_pages_exact(info->queue, vring_pci_size(info->num)); |
| 460 | } |
| 461 | |
Michael S. Tsirkin | d3f5f06 | 2015-01-13 16:34:58 +0200 | [diff] [blame] | 462 | static const struct virtio_config_ops virtio_pci_config_nodev_ops = { |
| 463 | .get = NULL, |
| 464 | .set = NULL, |
| 465 | .generation = vp_generation, |
| 466 | .get_status = vp_get_status, |
| 467 | .set_status = vp_set_status, |
| 468 | .reset = vp_reset, |
| 469 | .find_vqs = vp_modern_find_vqs, |
| 470 | .del_vqs = vp_del_vqs, |
| 471 | .get_features = vp_get_features, |
| 472 | .finalize_features = vp_finalize_features, |
| 473 | .bus_name = vp_bus_name, |
| 474 | .set_vq_affinity = vp_set_vq_affinity, |
| 475 | }; |
| 476 | |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 477 | static const struct virtio_config_ops virtio_pci_config_ops = { |
| 478 | .get = vp_get, |
| 479 | .set = vp_set, |
| 480 | .generation = vp_generation, |
| 481 | .get_status = vp_get_status, |
| 482 | .set_status = vp_set_status, |
| 483 | .reset = vp_reset, |
| 484 | .find_vqs = vp_modern_find_vqs, |
| 485 | .del_vqs = vp_del_vqs, |
| 486 | .get_features = vp_get_features, |
| 487 | .finalize_features = vp_finalize_features, |
| 488 | .bus_name = vp_bus_name, |
| 489 | .set_vq_affinity = vp_set_vq_affinity, |
| 490 | }; |
| 491 | |
| 492 | /** |
| 493 | * virtio_pci_find_capability - walk capabilities to find device info. |
| 494 | * @dev: the pci device |
| 495 | * @cfg_type: the VIRTIO_PCI_CAP_* value we seek |
| 496 | * @ioresource_types: IORESOURCE_MEM and/or IORESOURCE_IO. |
| 497 | * |
| 498 | * Returns offset of the capability, or 0. |
| 499 | */ |
| 500 | static inline int virtio_pci_find_capability(struct pci_dev *dev, u8 cfg_type, |
| 501 | u32 ioresource_types) |
| 502 | { |
| 503 | int pos; |
| 504 | |
| 505 | for (pos = pci_find_capability(dev, PCI_CAP_ID_VNDR); |
| 506 | pos > 0; |
| 507 | pos = pci_find_next_capability(dev, pos, PCI_CAP_ID_VNDR)) { |
| 508 | u8 type, bar; |
| 509 | pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap, |
| 510 | cfg_type), |
| 511 | &type); |
| 512 | pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap, |
| 513 | bar), |
| 514 | &bar); |
| 515 | |
| 516 | /* Ignore structures with reserved BAR values */ |
| 517 | if (bar > 0x5) |
| 518 | continue; |
| 519 | |
| 520 | if (type == cfg_type) { |
| 521 | if (pci_resource_len(dev, bar) && |
| 522 | pci_resource_flags(dev, bar) & ioresource_types) |
| 523 | return pos; |
| 524 | } |
| 525 | } |
| 526 | return 0; |
| 527 | } |
| 528 | |
Rusty Russell | 89461c4 | 2013-05-30 16:29:32 +0930 | [diff] [blame] | 529 | /* This is part of the ABI. Don't screw with it. */ |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 530 | static inline void check_offsets(void) |
| 531 | { |
Rusty Russell | 89461c4 | 2013-05-30 16:29:32 +0930 | [diff] [blame] | 532 | /* Note: disk space was harmed in compilation of this function. */ |
| 533 | BUILD_BUG_ON(VIRTIO_PCI_CAP_VNDR != |
| 534 | offsetof(struct virtio_pci_cap, cap_vndr)); |
| 535 | BUILD_BUG_ON(VIRTIO_PCI_CAP_NEXT != |
| 536 | offsetof(struct virtio_pci_cap, cap_next)); |
| 537 | BUILD_BUG_ON(VIRTIO_PCI_CAP_LEN != |
| 538 | offsetof(struct virtio_pci_cap, cap_len)); |
| 539 | BUILD_BUG_ON(VIRTIO_PCI_CAP_CFG_TYPE != |
| 540 | offsetof(struct virtio_pci_cap, cfg_type)); |
| 541 | BUILD_BUG_ON(VIRTIO_PCI_CAP_BAR != |
| 542 | offsetof(struct virtio_pci_cap, bar)); |
| 543 | BUILD_BUG_ON(VIRTIO_PCI_CAP_OFFSET != |
| 544 | offsetof(struct virtio_pci_cap, offset)); |
| 545 | BUILD_BUG_ON(VIRTIO_PCI_CAP_LENGTH != |
| 546 | offsetof(struct virtio_pci_cap, length)); |
| 547 | BUILD_BUG_ON(VIRTIO_PCI_NOTIFY_CAP_MULT != |
| 548 | offsetof(struct virtio_pci_notify_cap, |
| 549 | notify_off_multiplier)); |
| 550 | BUILD_BUG_ON(VIRTIO_PCI_COMMON_DFSELECT != |
| 551 | offsetof(struct virtio_pci_common_cfg, |
| 552 | device_feature_select)); |
| 553 | BUILD_BUG_ON(VIRTIO_PCI_COMMON_DF != |
| 554 | offsetof(struct virtio_pci_common_cfg, device_feature)); |
| 555 | BUILD_BUG_ON(VIRTIO_PCI_COMMON_GFSELECT != |
| 556 | offsetof(struct virtio_pci_common_cfg, |
| 557 | guest_feature_select)); |
| 558 | BUILD_BUG_ON(VIRTIO_PCI_COMMON_GF != |
| 559 | offsetof(struct virtio_pci_common_cfg, guest_feature)); |
| 560 | BUILD_BUG_ON(VIRTIO_PCI_COMMON_MSIX != |
| 561 | offsetof(struct virtio_pci_common_cfg, msix_config)); |
| 562 | BUILD_BUG_ON(VIRTIO_PCI_COMMON_NUMQ != |
| 563 | offsetof(struct virtio_pci_common_cfg, num_queues)); |
| 564 | BUILD_BUG_ON(VIRTIO_PCI_COMMON_STATUS != |
| 565 | offsetof(struct virtio_pci_common_cfg, device_status)); |
| 566 | BUILD_BUG_ON(VIRTIO_PCI_COMMON_CFGGENERATION != |
| 567 | offsetof(struct virtio_pci_common_cfg, config_generation)); |
| 568 | BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_SELECT != |
| 569 | offsetof(struct virtio_pci_common_cfg, queue_select)); |
| 570 | BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_SIZE != |
| 571 | offsetof(struct virtio_pci_common_cfg, queue_size)); |
| 572 | BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_MSIX != |
| 573 | offsetof(struct virtio_pci_common_cfg, queue_msix_vector)); |
| 574 | BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_ENABLE != |
| 575 | offsetof(struct virtio_pci_common_cfg, queue_enable)); |
| 576 | BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_NOFF != |
| 577 | offsetof(struct virtio_pci_common_cfg, queue_notify_off)); |
| 578 | BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_DESCLO != |
| 579 | offsetof(struct virtio_pci_common_cfg, queue_desc_lo)); |
| 580 | BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_DESCHI != |
| 581 | offsetof(struct virtio_pci_common_cfg, queue_desc_hi)); |
| 582 | BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_AVAILLO != |
| 583 | offsetof(struct virtio_pci_common_cfg, queue_avail_lo)); |
| 584 | BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_AVAILHI != |
| 585 | offsetof(struct virtio_pci_common_cfg, queue_avail_hi)); |
| 586 | BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_USEDLO != |
| 587 | offsetof(struct virtio_pci_common_cfg, queue_used_lo)); |
| 588 | BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_USEDHI != |
| 589 | offsetof(struct virtio_pci_common_cfg, queue_used_hi)); |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | /* the PCI probing function */ |
| 593 | int virtio_pci_modern_probe(struct virtio_pci_device *vp_dev) |
| 594 | { |
| 595 | struct pci_dev *pci_dev = vp_dev->pci_dev; |
| 596 | int err, common, isr, notify, device; |
| 597 | u32 notify_length; |
Michael S. Tsirkin | 3909213 | 2015-01-14 18:50:55 +0200 | [diff] [blame] | 598 | u32 notify_offset; |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 599 | |
| 600 | check_offsets(); |
| 601 | |
| 602 | /* We only own devices >= 0x1000 and <= 0x107f: leave the rest. */ |
| 603 | if (pci_dev->device < 0x1000 || pci_dev->device > 0x107f) |
| 604 | return -ENODEV; |
| 605 | |
| 606 | if (pci_dev->device < 0x1040) { |
| 607 | /* Transitional devices: use the PCI subsystem device id as |
| 608 | * virtio device id, same as legacy driver always did. |
| 609 | */ |
| 610 | vp_dev->vdev.id.device = pci_dev->subsystem_device; |
| 611 | } else { |
| 612 | /* Modern devices: simply use PCI device id, but start from 0x1040. */ |
| 613 | vp_dev->vdev.id.device = pci_dev->device - 0x1040; |
| 614 | } |
| 615 | vp_dev->vdev.id.vendor = pci_dev->subsystem_vendor; |
| 616 | |
| 617 | if (virtio_device_is_legacy_only(vp_dev->vdev.id)) |
| 618 | return -ENODEV; |
| 619 | |
| 620 | /* check for a common config: if not, use legacy mode (bar 0). */ |
| 621 | common = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_COMMON_CFG, |
| 622 | IORESOURCE_IO | IORESOURCE_MEM); |
| 623 | if (!common) { |
| 624 | dev_info(&pci_dev->dev, |
| 625 | "virtio_pci: leaving for legacy driver\n"); |
| 626 | return -ENODEV; |
| 627 | } |
| 628 | |
| 629 | /* If common is there, these should be too... */ |
| 630 | isr = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_ISR_CFG, |
| 631 | IORESOURCE_IO | IORESOURCE_MEM); |
| 632 | notify = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_NOTIFY_CFG, |
| 633 | IORESOURCE_IO | IORESOURCE_MEM); |
| 634 | if (!isr || !notify) { |
| 635 | dev_err(&pci_dev->dev, |
| 636 | "virtio_pci: missing capabilities %i/%i/%i\n", |
| 637 | common, isr, notify); |
| 638 | return -EINVAL; |
| 639 | } |
| 640 | |
| 641 | /* Device capability is only mandatory for devices that have |
| 642 | * device-specific configuration. |
| 643 | */ |
| 644 | device = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_DEVICE_CFG, |
| 645 | IORESOURCE_IO | IORESOURCE_MEM); |
| 646 | |
| 647 | err = -EINVAL; |
| 648 | vp_dev->common = map_capability(pci_dev, common, |
| 649 | sizeof(struct virtio_pci_common_cfg), 4, |
| 650 | 0, sizeof(struct virtio_pci_common_cfg), |
| 651 | NULL); |
| 652 | if (!vp_dev->common) |
| 653 | goto err_map_common; |
| 654 | vp_dev->isr = map_capability(pci_dev, isr, sizeof(u8), 1, |
| 655 | 0, 1, |
| 656 | NULL); |
| 657 | if (!vp_dev->isr) |
| 658 | goto err_map_isr; |
| 659 | |
| 660 | /* Read notify_off_multiplier from config space. */ |
| 661 | pci_read_config_dword(pci_dev, |
| 662 | notify + offsetof(struct virtio_pci_notify_cap, |
| 663 | notify_off_multiplier), |
| 664 | &vp_dev->notify_offset_multiplier); |
Michael S. Tsirkin | 3909213 | 2015-01-14 18:50:55 +0200 | [diff] [blame] | 665 | /* Read notify length and offset from config space. */ |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 666 | pci_read_config_dword(pci_dev, |
| 667 | notify + offsetof(struct virtio_pci_notify_cap, |
| 668 | cap.length), |
| 669 | ¬ify_length); |
| 670 | |
Michael S. Tsirkin | 3909213 | 2015-01-14 18:50:55 +0200 | [diff] [blame] | 671 | pci_read_config_dword(pci_dev, |
| 672 | notify + offsetof(struct virtio_pci_notify_cap, |
| 673 | cap.length), |
| 674 | ¬ify_offset); |
| 675 | |
| 676 | /* We don't know how many VQs we'll map, ahead of the time. |
| 677 | * If notify length is small, map it all now. |
| 678 | * Otherwise, map each VQ individually later. |
| 679 | */ |
| 680 | if ((u64)notify_length + (notify_offset % PAGE_SIZE) <= PAGE_SIZE) { |
| 681 | vp_dev->notify_base = map_capability(pci_dev, notify, 2, 2, |
| 682 | 0, notify_length, |
| 683 | &vp_dev->notify_len); |
| 684 | if (!vp_dev->notify_base) |
| 685 | goto err_map_notify; |
| 686 | } else { |
| 687 | vp_dev->notify_map_cap = notify; |
| 688 | } |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 689 | |
| 690 | /* Again, we don't know how much we should map, but PAGE_SIZE |
| 691 | * is more than enough for all existing devices. |
| 692 | */ |
| 693 | if (device) { |
| 694 | vp_dev->device = map_capability(pci_dev, device, 0, 4, |
| 695 | 0, PAGE_SIZE, |
| 696 | &vp_dev->device_len); |
| 697 | if (!vp_dev->device) |
| 698 | goto err_map_device; |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 699 | |
Michael S. Tsirkin | d3f5f06 | 2015-01-13 16:34:58 +0200 | [diff] [blame] | 700 | vp_dev->vdev.config = &virtio_pci_config_ops; |
| 701 | } else { |
| 702 | vp_dev->vdev.config = &virtio_pci_config_nodev_ops; |
| 703 | } |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 704 | |
| 705 | vp_dev->config_vector = vp_config_vector; |
| 706 | vp_dev->setup_vq = setup_vq; |
| 707 | vp_dev->del_vq = del_vq; |
| 708 | |
| 709 | return 0; |
| 710 | |
| 711 | err_map_device: |
Michael S. Tsirkin | 3909213 | 2015-01-14 18:50:55 +0200 | [diff] [blame] | 712 | if (vp_dev->notify_base) |
| 713 | pci_iounmap(pci_dev, vp_dev->notify_base); |
| 714 | err_map_notify: |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 715 | pci_iounmap(pci_dev, vp_dev->isr); |
| 716 | err_map_isr: |
| 717 | pci_iounmap(pci_dev, vp_dev->common); |
| 718 | err_map_common: |
| 719 | return err; |
| 720 | } |
| 721 | |
| 722 | void virtio_pci_modern_remove(struct virtio_pci_device *vp_dev) |
| 723 | { |
| 724 | struct pci_dev *pci_dev = vp_dev->pci_dev; |
| 725 | |
| 726 | if (vp_dev->device) |
| 727 | pci_iounmap(pci_dev, vp_dev->device); |
Michael S. Tsirkin | 3909213 | 2015-01-14 18:50:55 +0200 | [diff] [blame] | 728 | if (vp_dev->notify_base) |
| 729 | pci_iounmap(pci_dev, vp_dev->notify_base); |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 730 | pci_iounmap(pci_dev, vp_dev->isr); |
| 731 | pci_iounmap(pci_dev, vp_dev->common); |
| 732 | } |