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