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 | |
Jason Wang | 0b017708 | 2021-01-04 14:54:51 +0800 | [diff] [blame] | 22 | static u64 vp_get_features(struct virtio_device *vdev) |
| 23 | { |
| 24 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
| 25 | |
| 26 | return vp_modern_get_features(&vp_dev->mdev); |
| 27 | } |
| 28 | |
Tiwei Bie | cfecc29 | 2018-06-01 12:02:39 +0800 | [diff] [blame] | 29 | static void vp_transport_features(struct virtio_device *vdev, u64 features) |
| 30 | { |
| 31 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
| 32 | struct pci_dev *pci_dev = vp_dev->pci_dev; |
| 33 | |
| 34 | if ((features & BIT_ULL(VIRTIO_F_SR_IOV)) && |
| 35 | pci_find_ext_capability(pci_dev, PCI_EXT_CAP_ID_SRIOV)) |
| 36 | __virtio_set_bit(vdev, VIRTIO_F_SR_IOV); |
| 37 | } |
| 38 | |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 39 | /* virtio config->finalize_features() implementation */ |
| 40 | static int vp_finalize_features(struct virtio_device *vdev) |
| 41 | { |
| 42 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
Tiwei Bie | cfecc29 | 2018-06-01 12:02:39 +0800 | [diff] [blame] | 43 | u64 features = vdev->features; |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 44 | |
| 45 | /* Give virtio_ring a chance to accept features. */ |
| 46 | vring_transport_features(vdev); |
| 47 | |
Tiwei Bie | cfecc29 | 2018-06-01 12:02:39 +0800 | [diff] [blame] | 48 | /* Give virtio_pci a chance to accept features. */ |
| 49 | vp_transport_features(vdev, features); |
| 50 | |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 51 | if (!__virtio_test_bit(vdev, VIRTIO_F_VERSION_1)) { |
| 52 | dev_err(&vdev->dev, "virtio: device uses modern interface " |
| 53 | "but does not have VIRTIO_F_VERSION_1\n"); |
| 54 | return -EINVAL; |
| 55 | } |
| 56 | |
Jason Wang | 0b017708 | 2021-01-04 14:54:51 +0800 | [diff] [blame] | 57 | vp_modern_set_features(&vp_dev->mdev, vdev->features); |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 58 | |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | /* virtio config->get() implementation */ |
| 63 | static void vp_get(struct virtio_device *vdev, unsigned offset, |
| 64 | void *buf, unsigned len) |
| 65 | { |
| 66 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
Jason Wang | b5d5809 | 2021-01-04 14:54:46 +0800 | [diff] [blame] | 67 | struct virtio_pci_modern_device *mdev = &vp_dev->mdev; |
| 68 | void __iomem *device = mdev->device; |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 69 | u8 b; |
| 70 | __le16 w; |
| 71 | __le32 l; |
| 72 | |
Jason Wang | b5d5809 | 2021-01-04 14:54:46 +0800 | [diff] [blame] | 73 | BUG_ON(offset + len > mdev->device_len); |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 74 | |
| 75 | switch (len) { |
| 76 | case 1: |
Jason Wang | 64f2087 | 2021-01-04 14:54:45 +0800 | [diff] [blame] | 77 | b = ioread8(device + offset); |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 78 | memcpy(buf, &b, sizeof b); |
| 79 | break; |
| 80 | case 2: |
Jason Wang | 64f2087 | 2021-01-04 14:54:45 +0800 | [diff] [blame] | 81 | w = cpu_to_le16(ioread16(device + offset)); |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 82 | memcpy(buf, &w, sizeof w); |
| 83 | break; |
| 84 | case 4: |
Jason Wang | 64f2087 | 2021-01-04 14:54:45 +0800 | [diff] [blame] | 85 | l = cpu_to_le32(ioread32(device + offset)); |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 86 | memcpy(buf, &l, sizeof l); |
| 87 | break; |
| 88 | case 8: |
Jason Wang | 64f2087 | 2021-01-04 14:54:45 +0800 | [diff] [blame] | 89 | l = cpu_to_le32(ioread32(device + offset)); |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 90 | memcpy(buf, &l, sizeof l); |
Jason Wang | 64f2087 | 2021-01-04 14:54:45 +0800 | [diff] [blame] | 91 | l = cpu_to_le32(ioread32(device + offset + sizeof l)); |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 92 | memcpy(buf + sizeof l, &l, sizeof l); |
| 93 | break; |
| 94 | default: |
| 95 | BUG(); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /* the config->set() implementation. it's symmetric to the config->get() |
| 100 | * implementation */ |
| 101 | static void vp_set(struct virtio_device *vdev, unsigned offset, |
| 102 | const void *buf, unsigned len) |
| 103 | { |
| 104 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
Jason Wang | b5d5809 | 2021-01-04 14:54:46 +0800 | [diff] [blame] | 105 | struct virtio_pci_modern_device *mdev = &vp_dev->mdev; |
| 106 | void __iomem *device = mdev->device; |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 107 | u8 b; |
| 108 | __le16 w; |
| 109 | __le32 l; |
| 110 | |
Jason Wang | b5d5809 | 2021-01-04 14:54:46 +0800 | [diff] [blame] | 111 | BUG_ON(offset + len > mdev->device_len); |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 112 | |
| 113 | switch (len) { |
| 114 | case 1: |
| 115 | memcpy(&b, buf, sizeof b); |
Jason Wang | 64f2087 | 2021-01-04 14:54:45 +0800 | [diff] [blame] | 116 | iowrite8(b, device + offset); |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 117 | break; |
| 118 | case 2: |
| 119 | memcpy(&w, buf, sizeof w); |
Jason Wang | 64f2087 | 2021-01-04 14:54:45 +0800 | [diff] [blame] | 120 | iowrite16(le16_to_cpu(w), device + offset); |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 121 | break; |
| 122 | case 4: |
| 123 | memcpy(&l, buf, sizeof l); |
Jason Wang | 64f2087 | 2021-01-04 14:54:45 +0800 | [diff] [blame] | 124 | iowrite32(le32_to_cpu(l), device + offset); |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 125 | break; |
| 126 | case 8: |
| 127 | memcpy(&l, buf, sizeof l); |
Jason Wang | 64f2087 | 2021-01-04 14:54:45 +0800 | [diff] [blame] | 128 | iowrite32(le32_to_cpu(l), device + offset); |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 129 | memcpy(&l, buf + sizeof l, sizeof l); |
Jason Wang | 64f2087 | 2021-01-04 14:54:45 +0800 | [diff] [blame] | 130 | iowrite32(le32_to_cpu(l), device + offset + sizeof l); |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 131 | break; |
| 132 | default: |
| 133 | BUG(); |
| 134 | } |
| 135 | } |
| 136 | |
Jason Wang | ed2a73d | 2021-01-04 14:54:52 +0800 | [diff] [blame] | 137 | static u32 vp_generation(struct virtio_device *vdev) |
| 138 | { |
| 139 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
| 140 | |
| 141 | return vp_modern_generation(&vp_dev->mdev); |
| 142 | } |
| 143 | |
Jason Wang | e366912 | 2021-01-04 14:54:50 +0800 | [diff] [blame] | 144 | /* config->{get,set}_status() implementations */ |
| 145 | static u8 vp_get_status(struct virtio_device *vdev) |
| 146 | { |
| 147 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
| 148 | |
| 149 | return vp_modern_get_status(&vp_dev->mdev); |
| 150 | } |
| 151 | |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 152 | static void vp_set_status(struct virtio_device *vdev, u8 status) |
| 153 | { |
| 154 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
Jason Wang | 64f2087 | 2021-01-04 14:54:45 +0800 | [diff] [blame] | 155 | |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 156 | /* We should never be setting status to 0. */ |
| 157 | BUG_ON(status == 0); |
Jason Wang | e366912 | 2021-01-04 14:54:50 +0800 | [diff] [blame] | 158 | vp_modern_set_status(&vp_dev->mdev, status); |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | static void vp_reset(struct virtio_device *vdev) |
| 162 | { |
| 163 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
Jason Wang | b5d5809 | 2021-01-04 14:54:46 +0800 | [diff] [blame] | 164 | struct virtio_pci_modern_device *mdev = &vp_dev->mdev; |
Jason Wang | 64f2087 | 2021-01-04 14:54:45 +0800 | [diff] [blame] | 165 | |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 166 | /* 0 status means a reset. */ |
Jason Wang | e366912 | 2021-01-04 14:54:50 +0800 | [diff] [blame] | 167 | vp_modern_set_status(mdev, 0); |
Michael S. Tsirkin | 05dbcb4 | 2016-04-03 15:23:37 +0300 | [diff] [blame] | 168 | /* After writing 0 to device_status, the driver MUST wait for a read of |
| 169 | * device_status to return 0 before reinitializing the device. |
| 170 | * This will flush out the status write, and flush in device writes, |
| 171 | * including MSI-X interrupts, if any. |
| 172 | */ |
Jason Wang | e366912 | 2021-01-04 14:54:50 +0800 | [diff] [blame] | 173 | while (vp_modern_get_status(mdev)) |
Michael S. Tsirkin | 05dbcb4 | 2016-04-03 15:23:37 +0300 | [diff] [blame] | 174 | msleep(1); |
Jason Wang | 9e35276 | 2021-10-19 15:01:46 +0800 | [diff] [blame] | 175 | /* Disable VQ/configuration callbacks. */ |
| 176 | vp_disable_cbs(vdev); |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 177 | } |
| 178 | |
Jason Wang | 1a5c85f1 | 2021-01-04 14:54:49 +0800 | [diff] [blame] | 179 | static u16 vp_config_vector(struct virtio_pci_device *vp_dev, u16 vector) |
| 180 | { |
| 181 | return vp_modern_config_vector(&vp_dev->mdev, vector); |
| 182 | } |
| 183 | |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 184 | static struct virtqueue *setup_vq(struct virtio_pci_device *vp_dev, |
Michael S. Tsirkin | 0a9b3f4 | 2017-04-04 21:44:44 +0300 | [diff] [blame] | 185 | struct virtio_pci_vq_info *info, |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 186 | unsigned index, |
| 187 | void (*callback)(struct virtqueue *vq), |
| 188 | const char *name, |
Michael S. Tsirkin | f94682d | 2017-03-06 18:32:29 +0200 | [diff] [blame] | 189 | bool ctx, |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 190 | u16 msix_vec) |
| 191 | { |
Jason Wang | b5d5809 | 2021-01-04 14:54:46 +0800 | [diff] [blame] | 192 | |
| 193 | struct virtio_pci_modern_device *mdev = &vp_dev->mdev; |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 194 | struct virtqueue *vq; |
Jason Wang | 7dca6c0 | 2021-04-15 03:31:42 -0400 | [diff] [blame] | 195 | u16 num; |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 196 | int err; |
| 197 | |
Jason Wang | 6e52fc4 | 2021-01-04 14:54:57 +0800 | [diff] [blame] | 198 | if (index >= vp_modern_get_num_queues(mdev)) |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 199 | return ERR_PTR(-ENOENT); |
| 200 | |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 201 | /* Check if queue is either not available or already active. */ |
Jason Wang | 75658af | 2021-01-04 14:54:56 +0800 | [diff] [blame] | 202 | num = vp_modern_get_queue_size(mdev, index); |
Jason Wang | dc2e648 | 2021-01-04 14:54:55 +0800 | [diff] [blame] | 203 | if (!num || vp_modern_get_queue_enable(mdev, index)) |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 204 | return ERR_PTR(-ENOENT); |
| 205 | |
| 206 | if (num & (num - 1)) { |
| 207 | dev_warn(&vp_dev->pci_dev->dev, "bad queue size %u", num); |
| 208 | return ERR_PTR(-EINVAL); |
| 209 | } |
| 210 | |
Michael S. Tsirkin | 0a9b3f4 | 2017-04-04 21:44:44 +0300 | [diff] [blame] | 211 | info->msix_vector = msix_vec; |
| 212 | |
Andy Lutomirski | 7a5589b | 2016-02-02 21:46:39 -0800 | [diff] [blame] | 213 | /* create the vring */ |
| 214 | vq = vring_create_virtqueue(index, num, |
| 215 | SMP_CACHE_BYTES, &vp_dev->vdev, |
Michael S. Tsirkin | f94682d | 2017-03-06 18:32:29 +0200 | [diff] [blame] | 216 | true, true, ctx, |
| 217 | vp_notify, callback, name); |
Andy Lutomirski | 7a5589b | 2016-02-02 21:46:39 -0800 | [diff] [blame] | 218 | if (!vq) |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 219 | return ERR_PTR(-ENOMEM); |
| 220 | |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 221 | /* activate the queue */ |
Jason Wang | 75658af | 2021-01-04 14:54:56 +0800 | [diff] [blame] | 222 | vp_modern_set_queue_size(mdev, index, virtqueue_get_vring_size(vq)); |
Jason Wang | e1b0fa2 | 2021-01-04 14:54:54 +0800 | [diff] [blame] | 223 | vp_modern_queue_address(mdev, index, virtqueue_get_desc_addr(vq), |
| 224 | virtqueue_get_avail_addr(vq), |
| 225 | virtqueue_get_used_addr(vq)); |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 226 | |
Michael S. Tsirkin | 0f8a0b0 | 2021-05-04 04:12:10 -0400 | [diff] [blame] | 227 | vq->priv = (void __force *)vp_modern_map_vq_notify(mdev, index, NULL); |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 228 | if (!vq->priv) { |
| 229 | err = -ENOMEM; |
| 230 | goto err_map_notify; |
| 231 | } |
| 232 | |
| 233 | if (msix_vec != VIRTIO_MSI_NO_VECTOR) { |
Jason Wang | 3fbda9c | 2021-01-04 14:54:53 +0800 | [diff] [blame] | 234 | msix_vec = vp_modern_queue_vector(mdev, index, msix_vec); |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 235 | if (msix_vec == VIRTIO_MSI_NO_VECTOR) { |
| 236 | err = -EBUSY; |
| 237 | goto err_assign_vector; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | return vq; |
| 242 | |
| 243 | err_assign_vector: |
Jason Wang | b5d5809 | 2021-01-04 14:54:46 +0800 | [diff] [blame] | 244 | if (!mdev->notify_base) |
| 245 | pci_iounmap(mdev->pci_dev, (void __iomem __force *)vq->priv); |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 246 | err_map_notify: |
| 247 | vring_del_virtqueue(vq); |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 248 | return ERR_PTR(err); |
| 249 | } |
| 250 | |
| 251 | 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] | 252 | struct virtqueue *vqs[], |
| 253 | vq_callback_t *callbacks[], |
| 254 | const char * const names[], const bool *ctx, |
| 255 | struct irq_affinity *desc) |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 256 | { |
| 257 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
| 258 | struct virtqueue *vq; |
Michael S. Tsirkin | f94682d | 2017-03-06 18:32:29 +0200 | [diff] [blame] | 259 | 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] | 260 | |
| 261 | if (rc) |
| 262 | return rc; |
| 263 | |
| 264 | /* Select and activate all queues. Has to be done last: once we do |
| 265 | * this, there's no way to go back except reset. |
| 266 | */ |
Jason Wang | dc2e648 | 2021-01-04 14:54:55 +0800 | [diff] [blame] | 267 | list_for_each_entry(vq, &vdev->vqs, list) |
| 268 | vp_modern_set_queue_enable(&vp_dev->mdev, vq->index, true); |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 269 | |
| 270 | return 0; |
| 271 | } |
| 272 | |
Michael S. Tsirkin | 0a9b3f4 | 2017-04-04 21:44:44 +0300 | [diff] [blame] | 273 | static void del_vq(struct virtio_pci_vq_info *info) |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 274 | { |
Michael S. Tsirkin | 0a9b3f4 | 2017-04-04 21:44:44 +0300 | [diff] [blame] | 275 | struct virtqueue *vq = info->vq; |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 276 | struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev); |
Jason Wang | b5d5809 | 2021-01-04 14:54:46 +0800 | [diff] [blame] | 277 | struct virtio_pci_modern_device *mdev = &vp_dev->mdev; |
Jason Wang | b5d5809 | 2021-01-04 14:54:46 +0800 | [diff] [blame] | 278 | |
Jason Wang | 3fbda9c | 2021-01-04 14:54:53 +0800 | [diff] [blame] | 279 | if (vp_dev->msix_enabled) |
| 280 | vp_modern_queue_vector(mdev, vq->index, |
| 281 | VIRTIO_MSI_NO_VECTOR); |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 282 | |
Jason Wang | b5d5809 | 2021-01-04 14:54:46 +0800 | [diff] [blame] | 283 | if (!mdev->notify_base) |
| 284 | pci_iounmap(mdev->pci_dev, (void __force __iomem *)vq->priv); |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 285 | |
| 286 | vring_del_virtqueue(vq); |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 287 | } |
| 288 | |
Sebastien Boeuf | 0dd4ff9 | 2020-08-19 18:19:42 -0400 | [diff] [blame] | 289 | static int virtio_pci_find_shm_cap(struct pci_dev *dev, u8 required_id, |
| 290 | u8 *bar, u64 *offset, u64 *len) |
| 291 | { |
| 292 | int pos; |
| 293 | |
| 294 | for (pos = pci_find_capability(dev, PCI_CAP_ID_VNDR); pos > 0; |
| 295 | pos = pci_find_next_capability(dev, pos, PCI_CAP_ID_VNDR)) { |
| 296 | u8 type, cap_len, id; |
| 297 | u32 tmp32; |
| 298 | u64 res_offset, res_length; |
| 299 | |
| 300 | pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap, |
| 301 | cfg_type), &type); |
| 302 | if (type != VIRTIO_PCI_CAP_SHARED_MEMORY_CFG) |
| 303 | continue; |
| 304 | |
| 305 | pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap, |
| 306 | cap_len), &cap_len); |
| 307 | if (cap_len != sizeof(struct virtio_pci_cap64)) { |
| 308 | dev_err(&dev->dev, "%s: shm cap with bad size offset:" |
| 309 | " %d size: %d\n", __func__, pos, cap_len); |
| 310 | continue; |
| 311 | } |
| 312 | |
| 313 | pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap, |
| 314 | id), &id); |
| 315 | if (id != required_id) |
| 316 | continue; |
| 317 | |
| 318 | /* Type, and ID match, looks good */ |
| 319 | pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap, |
| 320 | bar), bar); |
| 321 | |
| 322 | /* Read the lower 32bit of length and offset */ |
| 323 | pci_read_config_dword(dev, pos + offsetof(struct virtio_pci_cap, |
| 324 | offset), &tmp32); |
| 325 | res_offset = tmp32; |
| 326 | pci_read_config_dword(dev, pos + offsetof(struct virtio_pci_cap, |
| 327 | length), &tmp32); |
| 328 | res_length = tmp32; |
| 329 | |
| 330 | /* and now the top half */ |
| 331 | pci_read_config_dword(dev, |
| 332 | pos + offsetof(struct virtio_pci_cap64, |
| 333 | offset_hi), &tmp32); |
| 334 | res_offset |= ((u64)tmp32) << 32; |
| 335 | pci_read_config_dword(dev, |
| 336 | pos + offsetof(struct virtio_pci_cap64, |
| 337 | length_hi), &tmp32); |
| 338 | res_length |= ((u64)tmp32) << 32; |
| 339 | |
| 340 | *offset = res_offset; |
| 341 | *len = res_length; |
| 342 | |
| 343 | return pos; |
| 344 | } |
| 345 | return 0; |
| 346 | } |
| 347 | |
| 348 | static bool vp_get_shm_region(struct virtio_device *vdev, |
| 349 | struct virtio_shm_region *region, u8 id) |
| 350 | { |
| 351 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
| 352 | struct pci_dev *pci_dev = vp_dev->pci_dev; |
| 353 | u8 bar; |
| 354 | u64 offset, len; |
| 355 | phys_addr_t phys_addr; |
| 356 | size_t bar_len; |
| 357 | |
| 358 | if (!virtio_pci_find_shm_cap(pci_dev, id, &bar, &offset, &len)) |
| 359 | return false; |
| 360 | |
| 361 | phys_addr = pci_resource_start(pci_dev, bar); |
| 362 | bar_len = pci_resource_len(pci_dev, bar); |
| 363 | |
| 364 | if ((offset + len) < offset) { |
| 365 | dev_err(&pci_dev->dev, "%s: cap offset+len overflow detected\n", |
| 366 | __func__); |
| 367 | return false; |
| 368 | } |
| 369 | |
| 370 | if (offset + len > bar_len) { |
| 371 | dev_err(&pci_dev->dev, "%s: bar shorter than cap offset+len\n", |
| 372 | __func__); |
| 373 | return false; |
| 374 | } |
| 375 | |
| 376 | region->len = len; |
| 377 | region->addr = (u64) phys_addr + offset; |
| 378 | |
| 379 | return true; |
| 380 | } |
| 381 | |
Michael S. Tsirkin | d3f5f06 | 2015-01-13 16:34:58 +0200 | [diff] [blame] | 382 | static const struct virtio_config_ops virtio_pci_config_nodev_ops = { |
Jason Wang | 9e35276 | 2021-10-19 15:01:46 +0800 | [diff] [blame] | 383 | .enable_cbs = vp_enable_cbs, |
Michael S. Tsirkin | d3f5f06 | 2015-01-13 16:34:58 +0200 | [diff] [blame] | 384 | .get = NULL, |
| 385 | .set = NULL, |
| 386 | .generation = vp_generation, |
| 387 | .get_status = vp_get_status, |
| 388 | .set_status = vp_set_status, |
| 389 | .reset = vp_reset, |
| 390 | .find_vqs = vp_modern_find_vqs, |
| 391 | .del_vqs = vp_del_vqs, |
| 392 | .get_features = vp_get_features, |
| 393 | .finalize_features = vp_finalize_features, |
| 394 | .bus_name = vp_bus_name, |
| 395 | .set_vq_affinity = vp_set_vq_affinity, |
Christoph Hellwig | bbaba47 | 2017-02-05 18:15:23 +0100 | [diff] [blame] | 396 | .get_vq_affinity = vp_get_vq_affinity, |
Sebastien Boeuf | 0dd4ff9 | 2020-08-19 18:19:42 -0400 | [diff] [blame] | 397 | .get_shm_region = vp_get_shm_region, |
Michael S. Tsirkin | d3f5f06 | 2015-01-13 16:34:58 +0200 | [diff] [blame] | 398 | }; |
| 399 | |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 400 | static const struct virtio_config_ops virtio_pci_config_ops = { |
Jason Wang | 9e35276 | 2021-10-19 15:01:46 +0800 | [diff] [blame] | 401 | .enable_cbs = vp_enable_cbs, |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 402 | .get = vp_get, |
| 403 | .set = vp_set, |
| 404 | .generation = vp_generation, |
| 405 | .get_status = vp_get_status, |
| 406 | .set_status = vp_set_status, |
| 407 | .reset = vp_reset, |
| 408 | .find_vqs = vp_modern_find_vqs, |
| 409 | .del_vqs = vp_del_vqs, |
| 410 | .get_features = vp_get_features, |
| 411 | .finalize_features = vp_finalize_features, |
| 412 | .bus_name = vp_bus_name, |
| 413 | .set_vq_affinity = vp_set_vq_affinity, |
Christoph Hellwig | bbaba47 | 2017-02-05 18:15:23 +0100 | [diff] [blame] | 414 | .get_vq_affinity = vp_get_vq_affinity, |
Sebastien Boeuf | 0dd4ff9 | 2020-08-19 18:19:42 -0400 | [diff] [blame] | 415 | .get_shm_region = vp_get_shm_region, |
Michael S. Tsirkin | 1fcf051 | 2014-12-11 13:59:51 +0200 | [diff] [blame] | 416 | }; |
| 417 | |
Jason Wang | 117a9de | 2021-01-04 14:54:47 +0800 | [diff] [blame] | 418 | /* the PCI probing function */ |
| 419 | int virtio_pci_modern_probe(struct virtio_pci_device *vp_dev) |
| 420 | { |
| 421 | struct virtio_pci_modern_device *mdev = &vp_dev->mdev; |
| 422 | struct pci_dev *pci_dev = vp_dev->pci_dev; |
| 423 | int err; |
| 424 | |
| 425 | mdev->pci_dev = pci_dev; |
| 426 | |
| 427 | err = vp_modern_probe(mdev); |
| 428 | if (err) |
| 429 | return err; |
| 430 | |
| 431 | if (mdev->device) |
| 432 | vp_dev->vdev.config = &virtio_pci_config_ops; |
| 433 | else |
| 434 | vp_dev->vdev.config = &virtio_pci_config_nodev_ops; |
| 435 | |
| 436 | vp_dev->config_vector = vp_config_vector; |
| 437 | vp_dev->setup_vq = setup_vq; |
| 438 | vp_dev->del_vq = del_vq; |
| 439 | vp_dev->isr = mdev->isr; |
| 440 | vp_dev->vdev.id = mdev->id; |
| 441 | |
| 442 | return 0; |
| 443 | } |
| 444 | |
Jason Wang | 3249037 | 2021-01-04 14:54:48 +0800 | [diff] [blame] | 445 | void virtio_pci_modern_remove(struct virtio_pci_device *vp_dev) |
| 446 | { |
| 447 | struct virtio_pci_modern_device *mdev = &vp_dev->mdev; |
| 448 | |
| 449 | vp_modern_remove(mdev); |
| 450 | } |