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