virtio_pci_modern.c 29.8 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
M
Michael S. Tsirkin 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Virtio PCI driver - modern (virtio 1.0) device support
 *
 * This module allows virtio devices to be used over a virtual PCI device.
 * This can be used with QEMU based VMMs like KVM or Xen.
 *
 * Copyright IBM Corp. 2007
 * Copyright Red Hat, Inc. 2014
 *
 * Authors:
 *  Anthony Liguori  <aliguori@us.ibm.com>
 *  Rusty Russell <rusty@rustcorp.com.au>
 *  Michael S. Tsirkin <mst@redhat.com>
 */

17
#include <linux/delay.h>
M
Michael S. Tsirkin 已提交
18
#define VIRTIO_PCI_NO_LEGACY
M
Matej Genci 已提交
19
#define VIRTIO_RING_NO_LEGACY
M
Michael S. Tsirkin 已提交
20 21
#include "virtio_pci_common.h"

22 23 24 25 26 27 28 29
/*
 * Type-safe wrappers for io accesses.
 * Use these to enforce at compile time the following spec requirement:
 *
 * The driver MUST access each field using the “natural” access
 * method, i.e. 32-bit accesses for 32-bit fields, 16-bit accesses
 * for 16-bit fields and 8-bit accesses for 8-bit fields.
 */
30
static inline u8 vp_ioread8(const u8 __iomem *addr)
31 32 33
{
	return ioread8(addr);
}
34
static inline u16 vp_ioread16 (const __le16 __iomem *addr)
35 36 37 38
{
	return ioread16(addr);
}

39
static inline u32 vp_ioread32(const __le32 __iomem *addr)
40 41 42 43 44 45 46 47 48
{
	return ioread32(addr);
}

static inline void vp_iowrite8(u8 value, u8 __iomem *addr)
{
	iowrite8(value, addr);
}

49
static inline void vp_iowrite16(u16 value, __le16 __iomem *addr)
50 51 52 53
{
	iowrite16(value, addr);
}

54
static inline void vp_iowrite32(u32 value, __le32 __iomem *addr)
55 56 57 58
{
	iowrite32(value, addr);
}

59 60 61 62 63 64 65
static void vp_iowrite64_twopart(u64 val,
				 __le32 __iomem *lo, __le32 __iomem *hi)
{
	vp_iowrite32((u32)val, lo);
	vp_iowrite32(val >> 32, hi);
}

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
/*
 * vp_modern_map_capability - map a part of virtio pci capability
 * @mdev: the modern virtio-pci device
 * @off: offset of the capability
 * @minlen: minimal length of the capability
 * @align: align requirement
 * @start: start from the capability
 * @size: map size
 * @len: the length that is actually mapped
 *
 * Returns the io address of for the part of the capability
 */
void __iomem *vp_modern_map_capability(struct virtio_pci_modern_device *mdev, int off,
				       size_t minlen,
				       u32 align,
				       u32 start, u32 size,
				       size_t *len)
M
Michael S. Tsirkin 已提交
83
{
84
	struct pci_dev *dev = mdev->pci_dev;
M
Michael S. Tsirkin 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
	u8 bar;
	u32 offset, length;
	void __iomem *p;

	pci_read_config_byte(dev, off + offsetof(struct virtio_pci_cap,
						 bar),
			     &bar);
	pci_read_config_dword(dev, off + offsetof(struct virtio_pci_cap, offset),
			     &offset);
	pci_read_config_dword(dev, off + offsetof(struct virtio_pci_cap, length),
			      &length);

	if (length <= start) {
		dev_err(&dev->dev,
			"virtio_pci: bad capability len %u (>%u expected)\n",
			length, start);
		return NULL;
	}

	if (length - start < minlen) {
		dev_err(&dev->dev,
			"virtio_pci: bad capability len %u (>=%zu expected)\n",
			length, minlen);
		return NULL;
	}

	length -= start;

	if (start + offset < offset) {
		dev_err(&dev->dev,
			"virtio_pci: map wrap-around %u+%u\n",
			start, offset);
		return NULL;
	}

	offset += start;

	if (offset & (align - 1)) {
		dev_err(&dev->dev,
			"virtio_pci: offset %u not aligned to %u\n",
			offset, align);
		return NULL;
	}

	if (length > size)
		length = size;

	if (len)
		*len = length;

	if (minlen + offset < minlen ||
	    minlen + offset > pci_resource_len(dev, bar)) {
		dev_err(&dev->dev,
			"virtio_pci: map virtio %zu@%u "
			"out of range on bar %i length %lu\n",
			minlen, offset,
			bar, (unsigned long)pci_resource_len(dev, bar));
		return NULL;
	}

	p = pci_iomap_range(dev, bar, offset, length);
	if (!p)
		dev_err(&dev->dev,
			"virtio_pci: unable to map virtio %u@%u on bar %i\n",
			length, offset, bar);
	return p;
}

153 154 155 156 157 158 159
/*
 * vp_modern_get_features - get features from device
 * @mdev: the modern virtio-pci device
 *
 * Returns the features read from the device
 */
static u64 vp_modern_get_features(struct virtio_pci_modern_device *mdev)
M
Michael S. Tsirkin 已提交
160
{
J
Jason Wang 已提交
161
	struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
162

M
Michael S. Tsirkin 已提交
163 164
	u64 features;

165 166 167 168
	vp_iowrite32(0, &cfg->device_feature_select);
	features = vp_ioread32(&cfg->device_feature);
	vp_iowrite32(1, &cfg->device_feature_select);
	features |= ((u64)vp_ioread32(&cfg->device_feature) << 32);
M
Michael S. Tsirkin 已提交
169 170 171 172

	return features;
}

173 174 175 176 177 178 179 180
/* virtio config->get_features() implementation */
static u64 vp_get_features(struct virtio_device *vdev)
{
	struct virtio_pci_device *vp_dev = to_vp_device(vdev);

	return vp_modern_get_features(&vp_dev->mdev);
}

T
Tiwei Bie 已提交
181 182 183 184 185 186 187 188 189 190
static void vp_transport_features(struct virtio_device *vdev, u64 features)
{
	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
	struct pci_dev *pci_dev = vp_dev->pci_dev;

	if ((features & BIT_ULL(VIRTIO_F_SR_IOV)) &&
			pci_find_ext_capability(pci_dev, PCI_EXT_CAP_ID_SRIOV))
		__virtio_set_bit(vdev, VIRTIO_F_SR_IOV);
}

191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
/*
 * vp_modern_set_features - set features to device
 * @mdev: the modern virtio-pci device
 * @features: the features set to device
 */
static void vp_modern_set_features(struct virtio_pci_modern_device *mdev,
				   u64 features)
{
	struct virtio_pci_common_cfg __iomem *cfg = mdev->common;

	vp_iowrite32(0, &cfg->guest_feature_select);
	vp_iowrite32((u32)features, &cfg->guest_feature);
	vp_iowrite32(1, &cfg->guest_feature_select);
	vp_iowrite32(features >> 32, &cfg->guest_feature);
}

207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
/*
 * vp_modern_queue_vector - set the MSIX vector for a specific virtqueue
 * @mdev: the modern virtio-pci device
 * @index: queue index
 * @vector: the config vector
 *
 * Returns the config vector read from the device
 */
static u16 vp_modern_queue_vector(struct virtio_pci_modern_device *mdev,
				  u16 index, u16 vector)
{
	struct virtio_pci_common_cfg __iomem *cfg = mdev->common;

	vp_iowrite16(index, &cfg->queue_select);
	vp_iowrite16(vector, &cfg->queue_msix_vector);
	/* Flush the write out to device */
	return vp_ioread16(&cfg->queue_msix_vector);
}

226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
/*
 * vp_modern_queue_address - set the virtqueue address
 * @mdev: the modern virtio-pci device
 * @index: the queue index
 * @desc_addr: address of the descriptor area
 * @driver_addr: address of the driver area
 * @device_addr: address of the device area
 */
static void vp_modern_queue_address(struct virtio_pci_modern_device *mdev,
				    u16 index, u64 desc_addr, u64 driver_addr,
				    u64 device_addr)
{
	struct virtio_pci_common_cfg __iomem *cfg = mdev->common;

	vp_iowrite16(index, &cfg->queue_select);

	vp_iowrite64_twopart(desc_addr, &cfg->queue_desc_lo,
			     &cfg->queue_desc_hi);
	vp_iowrite64_twopart(driver_addr, &cfg->queue_avail_lo,
			     &cfg->queue_avail_hi);
	vp_iowrite64_twopart(device_addr, &cfg->queue_used_lo,
			     &cfg->queue_used_hi);
}

250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
/*
 * vp_modern_set_queue_enable - enable a virtqueue
 * @mdev: the modern virtio-pci device
 * @index: the queue index
 * @enable: whether the virtqueue is enable or not
 */
static void vp_modern_set_queue_enable(struct virtio_pci_modern_device *mdev,
				       u16 index, bool enable)
{
	vp_iowrite16(index, &mdev->common->queue_select);
	vp_iowrite16(enable, &mdev->common->queue_enable);
}

/*
 * vp_modern_get_queue_enable - enable a virtqueue
 * @mdev: the modern virtio-pci device
 * @index: the queue index
 *
 * Returns whether a virtqueue is enabled or not
 */
static bool vp_modern_get_queue_enable(struct virtio_pci_modern_device *mdev,
				       u16 index)
{
	vp_iowrite16(index, &mdev->common->queue_select);

	return vp_ioread16(&mdev->common->queue_enable);
}

278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
/*
 * vp_modern_set_queue_size - set size for a virtqueue
 * @mdev: the modern virtio-pci device
 * @index: the queue index
 * @size: the size of the virtqueue
 */
static void vp_modern_set_queue_size(struct virtio_pci_modern_device *mdev,
				     u16 index, u16 size)
{
	vp_iowrite16(index, &mdev->common->queue_select);
	vp_iowrite16(size, &mdev->common->queue_size);

}

/*
 * vp_modern_get_queue_size - get size for a virtqueue
 * @mdev: the modern virtio-pci device
 * @index: the queue index
 *
 * Returns the size of the virtqueue
 */
static u16 vp_modern_get_queue_size(struct virtio_pci_modern_device *mdev,
				    u16 index)
{
	vp_iowrite16(index, &mdev->common->queue_select);

	return vp_ioread16(&mdev->common->queue_size);

}

308 309 310 311 312 313 314 315 316 317 318
/*
 * vp_modern_get_num_queues - get the number of virtqueues
 * @mdev: the modern virtio-pci device
 *
 * Returns the number of virtqueues
 */
static u16 vp_modern_get_num_queues(struct virtio_pci_modern_device *mdev)
{
	return vp_ioread16(&mdev->common->num_queues);
}

319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
/*
 * vp_modern_get_queue_notify_off - get notification offset for a virtqueue
 * @mdev: the modern virtio-pci device
 * @index: the queue index
 *
 * Returns the notification offset for a virtqueue
 */
static u16 vp_modern_get_queue_notify_off(struct virtio_pci_modern_device *mdev,
					  u16 index)
{
	vp_iowrite16(index, &mdev->common->queue_select);

	return vp_ioread16(&mdev->common->queue_notify_off);
}

M
Michael S. Tsirkin 已提交
334 335 336 337
/* virtio config->finalize_features() implementation */
static int vp_finalize_features(struct virtio_device *vdev)
{
	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
T
Tiwei Bie 已提交
338
	u64 features = vdev->features;
M
Michael S. Tsirkin 已提交
339 340 341 342

	/* Give virtio_ring a chance to accept features. */
	vring_transport_features(vdev);

T
Tiwei Bie 已提交
343 344 345
	/* Give virtio_pci a chance to accept features. */
	vp_transport_features(vdev, features);

M
Michael S. Tsirkin 已提交
346 347 348 349 350 351
	if (!__virtio_test_bit(vdev, VIRTIO_F_VERSION_1)) {
		dev_err(&vdev->dev, "virtio: device uses modern interface "
			"but does not have VIRTIO_F_VERSION_1\n");
		return -EINVAL;
	}

352
	vp_modern_set_features(&vp_dev->mdev, vdev->features);
M
Michael S. Tsirkin 已提交
353 354 355 356 357 358 359 360 361

	return 0;
}

/* virtio config->get() implementation */
static void vp_get(struct virtio_device *vdev, unsigned offset,
		   void *buf, unsigned len)
{
	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
J
Jason Wang 已提交
362 363
	struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
	void __iomem *device = mdev->device;
M
Michael S. Tsirkin 已提交
364 365 366 367
	u8 b;
	__le16 w;
	__le32 l;

J
Jason Wang 已提交
368
	BUG_ON(offset + len > mdev->device_len);
M
Michael S. Tsirkin 已提交
369 370 371

	switch (len) {
	case 1:
372
		b = ioread8(device + offset);
M
Michael S. Tsirkin 已提交
373 374 375
		memcpy(buf, &b, sizeof b);
		break;
	case 2:
376
		w = cpu_to_le16(ioread16(device + offset));
M
Michael S. Tsirkin 已提交
377 378 379
		memcpy(buf, &w, sizeof w);
		break;
	case 4:
380
		l = cpu_to_le32(ioread32(device + offset));
M
Michael S. Tsirkin 已提交
381 382 383
		memcpy(buf, &l, sizeof l);
		break;
	case 8:
384
		l = cpu_to_le32(ioread32(device + offset));
M
Michael S. Tsirkin 已提交
385
		memcpy(buf, &l, sizeof l);
386
		l = cpu_to_le32(ioread32(device + offset + sizeof l));
M
Michael S. Tsirkin 已提交
387 388 389 390 391 392 393 394 395 396 397 398 399
		memcpy(buf + sizeof l, &l, sizeof l);
		break;
	default:
		BUG();
	}
}

/* the config->set() implementation.  it's symmetric to the config->get()
 * implementation */
static void vp_set(struct virtio_device *vdev, unsigned offset,
		   const void *buf, unsigned len)
{
	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
J
Jason Wang 已提交
400 401
	struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
	void __iomem *device = mdev->device;
M
Michael S. Tsirkin 已提交
402 403 404 405
	u8 b;
	__le16 w;
	__le32 l;

J
Jason Wang 已提交
406
	BUG_ON(offset + len > mdev->device_len);
M
Michael S. Tsirkin 已提交
407 408 409 410

	switch (len) {
	case 1:
		memcpy(&b, buf, sizeof b);
411
		iowrite8(b, device + offset);
M
Michael S. Tsirkin 已提交
412 413 414
		break;
	case 2:
		memcpy(&w, buf, sizeof w);
415
		iowrite16(le16_to_cpu(w), device + offset);
M
Michael S. Tsirkin 已提交
416 417 418
		break;
	case 4:
		memcpy(&l, buf, sizeof l);
419
		iowrite32(le32_to_cpu(l), device + offset);
M
Michael S. Tsirkin 已提交
420 421 422
		break;
	case 8:
		memcpy(&l, buf, sizeof l);
423
		iowrite32(le32_to_cpu(l), device + offset);
M
Michael S. Tsirkin 已提交
424
		memcpy(&l, buf + sizeof l, sizeof l);
425
		iowrite32(le32_to_cpu(l), device + offset + sizeof l);
M
Michael S. Tsirkin 已提交
426 427 428 429 430 431
		break;
	default:
		BUG();
	}
}

432 433 434 435 436 437 438
/*
 * vp_modern_generation - get the device genreation
 * @mdev: the modern virtio-pci device
 *
 * Returns the genreation read from device
 */
static u32 vp_modern_generation(struct virtio_pci_modern_device *mdev)
M
Michael S. Tsirkin 已提交
439
{
J
Jason Wang 已提交
440
	struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
441 442

	return vp_ioread8(&cfg->config_generation);
443 444 445 446 447 448 449
}

static u32 vp_generation(struct virtio_device *vdev)
{
	struct virtio_pci_device *vp_dev = to_vp_device(vdev);

	return vp_modern_generation(&vp_dev->mdev);
M
Michael S. Tsirkin 已提交
450 451
}

452 453 454 455 456 457 458 459 460 461 462 463 464
/*
 * vp_modern_get_status - get the device status
 * @mdev: the modern virtio-pci device
 *
 * Returns the status read from device
 */
static u8 vp_modern_get_status(struct virtio_pci_modern_device *mdev)
{
	struct virtio_pci_common_cfg __iomem *cfg = mdev->common;

	return vp_ioread8(&cfg->device_status);
}

M
Michael S. Tsirkin 已提交
465 466 467 468
/* config->{get,set}_status() implementations */
static u8 vp_get_status(struct virtio_device *vdev)
{
	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
469 470 471 472 473 474 475 476 477 478 479 480

	return vp_modern_get_status(&vp_dev->mdev);
}

/*
 * vp_modern_set_status - set status to device
 * @mdev: the modern virtio-pci device
 * @status: the status set to device
 */
static void vp_modern_set_status(struct virtio_pci_modern_device *mdev,
				 u8 status)
{
J
Jason Wang 已提交
481
	struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
482

483
	vp_iowrite8(status, &cfg->device_status);
M
Michael S. Tsirkin 已提交
484 485 486 487 488
}

static void vp_set_status(struct virtio_device *vdev, u8 status)
{
	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
489

M
Michael S. Tsirkin 已提交
490 491
	/* We should never be setting status to 0. */
	BUG_ON(status == 0);
492
	vp_modern_set_status(&vp_dev->mdev, status);
M
Michael S. Tsirkin 已提交
493 494 495 496 497
}

static void vp_reset(struct virtio_device *vdev)
{
	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
J
Jason Wang 已提交
498
	struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
499

M
Michael S. Tsirkin 已提交
500
	/* 0 status means a reset. */
501
	vp_modern_set_status(mdev, 0);
502 503 504 505 506
	/* After writing 0 to device_status, the driver MUST wait for a read of
	 * device_status to return 0 before reinitializing the device.
	 * This will flush out the status write, and flush in device writes,
	 * including MSI-X interrupts, if any.
	 */
507
	while (vp_modern_get_status(mdev))
508
		msleep(1);
M
Michael S. Tsirkin 已提交
509 510 511 512
	/* Flush pending VQ/configuration callbacks. */
	vp_synchronize_vectors(vdev);
}

513 514 515 516 517 518 519 520 521
/*
 * vp_modern_config_vector - set the vector for config interrupt
 * @mdev: the modern virtio-pci device
 * @vector: the config vector
 *
 * Returns the config vector read from the device
 */
static u16 vp_modern_config_vector(struct virtio_pci_modern_device *mdev,
				   u16 vector)
M
Michael S. Tsirkin 已提交
522
{
J
Jason Wang 已提交
523
	struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
524

M
Michael S. Tsirkin 已提交
525
	/* Setup the vector used for configuration events */
526
	vp_iowrite16(vector, &cfg->msix_config);
M
Michael S. Tsirkin 已提交
527 528
	/* Verify we had enough resources to assign the vector */
	/* Will also flush the write out to device */
529
	return vp_ioread16(&cfg->msix_config);
M
Michael S. Tsirkin 已提交
530 531
}

532 533 534 535 536
static u16 vp_config_vector(struct virtio_pci_device *vp_dev, u16 vector)
{
	return vp_modern_config_vector(&vp_dev->mdev, vector);
}

M
Michael S. Tsirkin 已提交
537
static struct virtqueue *setup_vq(struct virtio_pci_device *vp_dev,
538
				  struct virtio_pci_vq_info *info,
M
Michael S. Tsirkin 已提交
539 540 541
				  unsigned index,
				  void (*callback)(struct virtqueue *vq),
				  const char *name,
542
				  bool ctx,
M
Michael S. Tsirkin 已提交
543 544
				  u16 msix_vec)
{
J
Jason Wang 已提交
545 546

	struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
M
Michael S. Tsirkin 已提交
547 548 549 550
	struct virtqueue *vq;
	u16 num, off;
	int err;

551
	if (index >= vp_modern_get_num_queues(mdev))
M
Michael S. Tsirkin 已提交
552 553 554
		return ERR_PTR(-ENOENT);

	/* Check if queue is either not available or already active. */
555
	num = vp_modern_get_queue_size(mdev, index);
556
	if (!num || vp_modern_get_queue_enable(mdev, index))
M
Michael S. Tsirkin 已提交
557 558 559 560 561 562 563 564
		return ERR_PTR(-ENOENT);

	if (num & (num - 1)) {
		dev_warn(&vp_dev->pci_dev->dev, "bad queue size %u", num);
		return ERR_PTR(-EINVAL);
	}

	/* get offset of notification word for this vq */
565
	off = vp_modern_get_queue_notify_off(mdev, index);
M
Michael S. Tsirkin 已提交
566

567 568
	info->msix_vector = msix_vec;

M
Michael S. Tsirkin 已提交
569
	/* create the vring */
570 571
	vq = vring_create_virtqueue(index, num,
				    SMP_CACHE_BYTES, &vp_dev->vdev,
572 573
				    true, true, ctx,
				    vp_notify, callback, name);
574 575
	if (!vq)
		return ERR_PTR(-ENOMEM);
M
Michael S. Tsirkin 已提交
576 577

	/* activate the queue */
578
	vp_modern_set_queue_size(mdev, index, virtqueue_get_vring_size(vq));
579 580 581
	vp_modern_queue_address(mdev, index, virtqueue_get_desc_addr(vq),
				virtqueue_get_avail_addr(vq),
				virtqueue_get_used_addr(vq));
M
Michael S. Tsirkin 已提交
582

J
Jason Wang 已提交
583
	if (mdev->notify_base) {
584
		/* offset should not wrap */
J
Jason Wang 已提交
585 586 587
		if ((u64)off * mdev->notify_offset_multiplier + 2
		    > mdev->notify_len) {
			dev_warn(&mdev->pci_dev->dev,
588 589
				 "bad notification offset %u (x %u) "
				 "for queue %u > %zd",
J
Jason Wang 已提交
590 591
				 off, mdev->notify_offset_multiplier,
				 index, mdev->notify_len);
592 593 594
			err = -EINVAL;
			goto err_map_notify;
		}
J
Jason Wang 已提交
595 596
		vq->priv = (void __force *)mdev->notify_base +
			off * mdev->notify_offset_multiplier;
597
	} else {
598
		vq->priv = (void __force *)vp_modern_map_capability(mdev,
J
Jason Wang 已提交
599 600 601
							  mdev->notify_map_cap, 2, 2,
							  off * mdev->notify_offset_multiplier, 2,
							  NULL);
602
	}
M
Michael S. Tsirkin 已提交
603 604 605 606 607 608 609

	if (!vq->priv) {
		err = -ENOMEM;
		goto err_map_notify;
	}

	if (msix_vec != VIRTIO_MSI_NO_VECTOR) {
610
		msix_vec = vp_modern_queue_vector(mdev, index, msix_vec);
M
Michael S. Tsirkin 已提交
611 612 613 614 615 616 617 618 619
		if (msix_vec == VIRTIO_MSI_NO_VECTOR) {
			err = -EBUSY;
			goto err_assign_vector;
		}
	}

	return vq;

err_assign_vector:
J
Jason Wang 已提交
620 621
	if (!mdev->notify_base)
		pci_iounmap(mdev->pci_dev, (void __iomem __force *)vq->priv);
M
Michael S. Tsirkin 已提交
622 623 624 625 626 627
err_map_notify:
	vring_del_virtqueue(vq);
	return ERR_PTR(err);
}

static int vp_modern_find_vqs(struct virtio_device *vdev, unsigned nvqs,
628 629 630 631
			      struct virtqueue *vqs[],
			      vq_callback_t *callbacks[],
			      const char * const names[], const bool *ctx,
			      struct irq_affinity *desc)
M
Michael S. Tsirkin 已提交
632 633 634
{
	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
	struct virtqueue *vq;
635
	int rc = vp_find_vqs(vdev, nvqs, vqs, callbacks, names, ctx, desc);
M
Michael S. Tsirkin 已提交
636 637 638 639 640 641 642

	if (rc)
		return rc;

	/* Select and activate all queues. Has to be done last: once we do
	 * this, there's no way to go back except reset.
	 */
643 644
	list_for_each_entry(vq, &vdev->vqs, list)
		vp_modern_set_queue_enable(&vp_dev->mdev, vq->index, true);
M
Michael S. Tsirkin 已提交
645 646 647 648

	return 0;
}

649
static void del_vq(struct virtio_pci_vq_info *info)
M
Michael S. Tsirkin 已提交
650
{
651
	struct virtqueue *vq = info->vq;
M
Michael S. Tsirkin 已提交
652
	struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
J
Jason Wang 已提交
653
	struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
M
Michael S. Tsirkin 已提交
654

655 656 657
	if (vp_dev->msix_enabled)
		vp_modern_queue_vector(mdev, vq->index,
				       VIRTIO_MSI_NO_VECTOR);
M
Michael S. Tsirkin 已提交
658

J
Jason Wang 已提交
659 660
	if (!mdev->notify_base)
		pci_iounmap(mdev->pci_dev, (void __force __iomem *)vq->priv);
M
Michael S. Tsirkin 已提交
661 662 663 664

	vring_del_virtqueue(vq);
}

665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
static int virtio_pci_find_shm_cap(struct pci_dev *dev, u8 required_id,
				   u8 *bar, u64 *offset, u64 *len)
{
	int pos;

	for (pos = pci_find_capability(dev, PCI_CAP_ID_VNDR); pos > 0;
	     pos = pci_find_next_capability(dev, pos, PCI_CAP_ID_VNDR)) {
		u8 type, cap_len, id;
		u32 tmp32;
		u64 res_offset, res_length;

		pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
							 cfg_type), &type);
		if (type != VIRTIO_PCI_CAP_SHARED_MEMORY_CFG)
			continue;

		pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
							 cap_len), &cap_len);
		if (cap_len != sizeof(struct virtio_pci_cap64)) {
			dev_err(&dev->dev, "%s: shm cap with bad size offset:"
				" %d size: %d\n", __func__, pos, cap_len);
			continue;
		}

		pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
							 id), &id);
		if (id != required_id)
			continue;

		/* Type, and ID match, looks good */
		pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
							 bar), bar);

		/* Read the lower 32bit of length and offset */
		pci_read_config_dword(dev, pos + offsetof(struct virtio_pci_cap,
							  offset), &tmp32);
		res_offset = tmp32;
		pci_read_config_dword(dev, pos + offsetof(struct virtio_pci_cap,
							  length), &tmp32);
		res_length = tmp32;

		/* and now the top half */
		pci_read_config_dword(dev,
				      pos + offsetof(struct virtio_pci_cap64,
						     offset_hi), &tmp32);
		res_offset |= ((u64)tmp32) << 32;
		pci_read_config_dword(dev,
				      pos + offsetof(struct virtio_pci_cap64,
						     length_hi), &tmp32);
		res_length |= ((u64)tmp32) << 32;

		*offset = res_offset;
		*len = res_length;

		return pos;
	}
	return 0;
}

static bool vp_get_shm_region(struct virtio_device *vdev,
			      struct virtio_shm_region *region, u8 id)
{
	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
	struct pci_dev *pci_dev = vp_dev->pci_dev;
	u8 bar;
	u64 offset, len;
	phys_addr_t phys_addr;
	size_t bar_len;

	if (!virtio_pci_find_shm_cap(pci_dev, id, &bar, &offset, &len))
		return false;

	phys_addr = pci_resource_start(pci_dev, bar);
	bar_len = pci_resource_len(pci_dev, bar);

	if ((offset + len) < offset) {
		dev_err(&pci_dev->dev, "%s: cap offset+len overflow detected\n",
			__func__);
		return false;
	}

	if (offset + len > bar_len) {
		dev_err(&pci_dev->dev, "%s: bar shorter than cap offset+len\n",
			__func__);
		return false;
	}

	region->len = len;
	region->addr = (u64) phys_addr + offset;

	return true;
}

758 759 760 761 762 763 764 765 766 767 768 769 770
static const struct virtio_config_ops virtio_pci_config_nodev_ops = {
	.get		= NULL,
	.set		= NULL,
	.generation	= vp_generation,
	.get_status	= vp_get_status,
	.set_status	= vp_set_status,
	.reset		= vp_reset,
	.find_vqs	= vp_modern_find_vqs,
	.del_vqs	= vp_del_vqs,
	.get_features	= vp_get_features,
	.finalize_features = vp_finalize_features,
	.bus_name	= vp_bus_name,
	.set_vq_affinity = vp_set_vq_affinity,
771
	.get_vq_affinity = vp_get_vq_affinity,
772
	.get_shm_region  = vp_get_shm_region,
773 774
};

M
Michael S. Tsirkin 已提交
775 776 777 778 779 780 781 782 783 784 785 786 787
static const struct virtio_config_ops virtio_pci_config_ops = {
	.get		= vp_get,
	.set		= vp_set,
	.generation	= vp_generation,
	.get_status	= vp_get_status,
	.set_status	= vp_set_status,
	.reset		= vp_reset,
	.find_vqs	= vp_modern_find_vqs,
	.del_vqs	= vp_del_vqs,
	.get_features	= vp_get_features,
	.finalize_features = vp_finalize_features,
	.bus_name	= vp_bus_name,
	.set_vq_affinity = vp_set_vq_affinity,
788
	.get_vq_affinity = vp_get_vq_affinity,
789
	.get_shm_region  = vp_get_shm_region,
M
Michael S. Tsirkin 已提交
790 791 792 793 794 795 796
};

/**
 * virtio_pci_find_capability - walk capabilities to find device info.
 * @dev: the pci device
 * @cfg_type: the VIRTIO_PCI_CAP_* value we seek
 * @ioresource_types: IORESOURCE_MEM and/or IORESOURCE_IO.
797
 * @bars: the bitmask of BARs
M
Michael S. Tsirkin 已提交
798 799 800 801
 *
 * Returns offset of the capability, or 0.
 */
static inline int virtio_pci_find_capability(struct pci_dev *dev, u8 cfg_type,
802
					     u32 ioresource_types, int *bars)
M
Michael S. Tsirkin 已提交
803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
{
	int pos;

	for (pos = pci_find_capability(dev, PCI_CAP_ID_VNDR);
	     pos > 0;
	     pos = pci_find_next_capability(dev, pos, PCI_CAP_ID_VNDR)) {
		u8 type, bar;
		pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
							 cfg_type),
				     &type);
		pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
							 bar),
				     &bar);

		/* Ignore structures with reserved BAR values */
		if (bar > 0x5)
			continue;

		if (type == cfg_type) {
			if (pci_resource_len(dev, bar) &&
823 824
			    pci_resource_flags(dev, bar) & ioresource_types) {
				*bars |= (1 << bar);
M
Michael S. Tsirkin 已提交
825
				return pos;
826
			}
M
Michael S. Tsirkin 已提交
827 828 829 830 831
		}
	}
	return 0;
}

832
/* This is part of the ABI.  Don't screw with it. */
M
Michael S. Tsirkin 已提交
833 834
static inline void check_offsets(void)
{
835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
	/* Note: disk space was harmed in compilation of this function. */
	BUILD_BUG_ON(VIRTIO_PCI_CAP_VNDR !=
		     offsetof(struct virtio_pci_cap, cap_vndr));
	BUILD_BUG_ON(VIRTIO_PCI_CAP_NEXT !=
		     offsetof(struct virtio_pci_cap, cap_next));
	BUILD_BUG_ON(VIRTIO_PCI_CAP_LEN !=
		     offsetof(struct virtio_pci_cap, cap_len));
	BUILD_BUG_ON(VIRTIO_PCI_CAP_CFG_TYPE !=
		     offsetof(struct virtio_pci_cap, cfg_type));
	BUILD_BUG_ON(VIRTIO_PCI_CAP_BAR !=
		     offsetof(struct virtio_pci_cap, bar));
	BUILD_BUG_ON(VIRTIO_PCI_CAP_OFFSET !=
		     offsetof(struct virtio_pci_cap, offset));
	BUILD_BUG_ON(VIRTIO_PCI_CAP_LENGTH !=
		     offsetof(struct virtio_pci_cap, length));
	BUILD_BUG_ON(VIRTIO_PCI_NOTIFY_CAP_MULT !=
		     offsetof(struct virtio_pci_notify_cap,
			      notify_off_multiplier));
	BUILD_BUG_ON(VIRTIO_PCI_COMMON_DFSELECT !=
		     offsetof(struct virtio_pci_common_cfg,
			      device_feature_select));
	BUILD_BUG_ON(VIRTIO_PCI_COMMON_DF !=
		     offsetof(struct virtio_pci_common_cfg, device_feature));
	BUILD_BUG_ON(VIRTIO_PCI_COMMON_GFSELECT !=
		     offsetof(struct virtio_pci_common_cfg,
			      guest_feature_select));
	BUILD_BUG_ON(VIRTIO_PCI_COMMON_GF !=
		     offsetof(struct virtio_pci_common_cfg, guest_feature));
	BUILD_BUG_ON(VIRTIO_PCI_COMMON_MSIX !=
		     offsetof(struct virtio_pci_common_cfg, msix_config));
	BUILD_BUG_ON(VIRTIO_PCI_COMMON_NUMQ !=
		     offsetof(struct virtio_pci_common_cfg, num_queues));
	BUILD_BUG_ON(VIRTIO_PCI_COMMON_STATUS !=
		     offsetof(struct virtio_pci_common_cfg, device_status));
	BUILD_BUG_ON(VIRTIO_PCI_COMMON_CFGGENERATION !=
		     offsetof(struct virtio_pci_common_cfg, config_generation));
	BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_SELECT !=
		     offsetof(struct virtio_pci_common_cfg, queue_select));
	BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_SIZE !=
		     offsetof(struct virtio_pci_common_cfg, queue_size));
	BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_MSIX !=
		     offsetof(struct virtio_pci_common_cfg, queue_msix_vector));
	BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_ENABLE !=
		     offsetof(struct virtio_pci_common_cfg, queue_enable));
	BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_NOFF !=
		     offsetof(struct virtio_pci_common_cfg, queue_notify_off));
	BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_DESCLO !=
		     offsetof(struct virtio_pci_common_cfg, queue_desc_lo));
	BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_DESCHI !=
		     offsetof(struct virtio_pci_common_cfg, queue_desc_hi));
	BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_AVAILLO !=
		     offsetof(struct virtio_pci_common_cfg, queue_avail_lo));
	BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_AVAILHI !=
		     offsetof(struct virtio_pci_common_cfg, queue_avail_hi));
	BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_USEDLO !=
		     offsetof(struct virtio_pci_common_cfg, queue_used_lo));
	BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_USEDHI !=
		     offsetof(struct virtio_pci_common_cfg, queue_used_hi));
M
Michael S. Tsirkin 已提交
893 894
}

895 896 897 898 899 900 901 902
/*
 * vp_modern_probe: probe the modern virtio pci device, note that the
 * caller is required to enable PCI device before calling this function.
 * @mdev: the modern virtio-pci device
 *
 * Return 0 on succeed otherwise fail
 */
static int vp_modern_probe(struct virtio_pci_modern_device *mdev)
M
Michael S. Tsirkin 已提交
903
{
904
	struct pci_dev *pci_dev = mdev->pci_dev;
M
Michael S. Tsirkin 已提交
905 906
	int err, common, isr, notify, device;
	u32 notify_length;
907
	u32 notify_offset;
M
Michael S. Tsirkin 已提交
908 909 910

	check_offsets();

J
Jason Wang 已提交
911 912
	mdev->pci_dev = pci_dev;

M
Michael S. Tsirkin 已提交
913 914 915 916 917 918 919 920
	/* We only own devices >= 0x1000 and <= 0x107f: leave the rest. */
	if (pci_dev->device < 0x1000 || pci_dev->device > 0x107f)
		return -ENODEV;

	if (pci_dev->device < 0x1040) {
		/* Transitional devices: use the PCI subsystem device id as
		 * virtio device id, same as legacy driver always did.
		 */
J
Jason Wang 已提交
921
		mdev->id.device = pci_dev->subsystem_device;
M
Michael S. Tsirkin 已提交
922 923
	} else {
		/* Modern devices: simply use PCI device id, but start from 0x1040. */
J
Jason Wang 已提交
924
		mdev->id.device = pci_dev->device - 0x1040;
M
Michael S. Tsirkin 已提交
925
	}
J
Jason Wang 已提交
926
	mdev->id.vendor = pci_dev->subsystem_vendor;
M
Michael S. Tsirkin 已提交
927 928 929

	/* check for a common config: if not, use legacy mode (bar 0). */
	common = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_COMMON_CFG,
930
					    IORESOURCE_IO | IORESOURCE_MEM,
J
Jason Wang 已提交
931
					    &mdev->modern_bars);
M
Michael S. Tsirkin 已提交
932 933 934 935 936 937 938 939
	if (!common) {
		dev_info(&pci_dev->dev,
			 "virtio_pci: leaving for legacy driver\n");
		return -ENODEV;
	}

	/* If common is there, these should be too... */
	isr = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_ISR_CFG,
940
					 IORESOURCE_IO | IORESOURCE_MEM,
J
Jason Wang 已提交
941
					 &mdev->modern_bars);
M
Michael S. Tsirkin 已提交
942
	notify = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_NOTIFY_CFG,
943
					    IORESOURCE_IO | IORESOURCE_MEM,
J
Jason Wang 已提交
944
					    &mdev->modern_bars);
M
Michael S. Tsirkin 已提交
945 946 947 948 949 950 951
	if (!isr || !notify) {
		dev_err(&pci_dev->dev,
			"virtio_pci: missing capabilities %i/%i/%i\n",
			common, isr, notify);
		return -EINVAL;
	}

952 953 954 955 956 957 958
	err = dma_set_mask_and_coherent(&pci_dev->dev, DMA_BIT_MASK(64));
	if (err)
		err = dma_set_mask_and_coherent(&pci_dev->dev,
						DMA_BIT_MASK(32));
	if (err)
		dev_warn(&pci_dev->dev, "Failed to enable 64-bit or 32-bit DMA.  Trying to continue, but this might not work.\n");

M
Michael S. Tsirkin 已提交
959 960 961 962
	/* Device capability is only mandatory for devices that have
	 * device-specific configuration.
	 */
	device = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_DEVICE_CFG,
963
					    IORESOURCE_IO | IORESOURCE_MEM,
J
Jason Wang 已提交
964
					    &mdev->modern_bars);
965

J
Jason Wang 已提交
966
	err = pci_request_selected_regions(pci_dev, mdev->modern_bars,
967 968 969
					   "virtio-pci-modern");
	if (err)
		return err;
M
Michael S. Tsirkin 已提交
970 971

	err = -EINVAL;
972
	mdev->common = vp_modern_map_capability(mdev, common,
J
Jason Wang 已提交
973 974 975 976
				      sizeof(struct virtio_pci_common_cfg), 4,
				      0, sizeof(struct virtio_pci_common_cfg),
				      NULL);
	if (!mdev->common)
M
Michael S. Tsirkin 已提交
977
		goto err_map_common;
978 979 980
	mdev->isr = vp_modern_map_capability(mdev, isr, sizeof(u8), 1,
					     0, 1,
					     NULL);
J
Jason Wang 已提交
981
	if (!mdev->isr)
M
Michael S. Tsirkin 已提交
982 983 984 985 986 987
		goto err_map_isr;

	/* Read notify_off_multiplier from config space. */
	pci_read_config_dword(pci_dev,
			      notify + offsetof(struct virtio_pci_notify_cap,
						notify_off_multiplier),
J
Jason Wang 已提交
988
			      &mdev->notify_offset_multiplier);
989
	/* Read notify length and offset from config space. */
M
Michael S. Tsirkin 已提交
990 991 992 993 994
	pci_read_config_dword(pci_dev,
			      notify + offsetof(struct virtio_pci_notify_cap,
						cap.length),
			      &notify_length);

995 996
	pci_read_config_dword(pci_dev,
			      notify + offsetof(struct virtio_pci_notify_cap,
997
						cap.offset),
998 999 1000 1001 1002 1003 1004
			      &notify_offset);

	/* We don't know how many VQs we'll map, ahead of the time.
	 * If notify length is small, map it all now.
	 * Otherwise, map each VQ individually later.
	 */
	if ((u64)notify_length + (notify_offset % PAGE_SIZE) <= PAGE_SIZE) {
1005 1006 1007 1008
		mdev->notify_base = vp_modern_map_capability(mdev, notify,
							     2, 2,
							     0, notify_length,
							     &mdev->notify_len);
J
Jason Wang 已提交
1009
		if (!mdev->notify_base)
1010 1011
			goto err_map_notify;
	} else {
J
Jason Wang 已提交
1012
		mdev->notify_map_cap = notify;
1013
	}
M
Michael S. Tsirkin 已提交
1014 1015 1016 1017 1018

	/* Again, we don't know how much we should map, but PAGE_SIZE
	 * is more than enough for all existing devices.
	 */
	if (device) {
1019 1020 1021
		mdev->device = vp_modern_map_capability(mdev, device, 0, 4,
							0, PAGE_SIZE,
							&mdev->device_len);
J
Jason Wang 已提交
1022
		if (!mdev->device)
M
Michael S. Tsirkin 已提交
1023
			goto err_map_device;
1024
	}
M
Michael S. Tsirkin 已提交
1025 1026 1027 1028

	return 0;

err_map_device:
J
Jason Wang 已提交
1029 1030
	if (mdev->notify_base)
		pci_iounmap(pci_dev, mdev->notify_base);
1031
err_map_notify:
J
Jason Wang 已提交
1032
	pci_iounmap(pci_dev, mdev->isr);
M
Michael S. Tsirkin 已提交
1033
err_map_isr:
J
Jason Wang 已提交
1034
	pci_iounmap(pci_dev, mdev->common);
M
Michael S. Tsirkin 已提交
1035 1036 1037 1038
err_map_common:
	return err;
}

1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
/* the PCI probing function */
int virtio_pci_modern_probe(struct virtio_pci_device *vp_dev)
{
	struct virtio_pci_modern_device *mdev = &vp_dev->mdev;
	struct pci_dev *pci_dev = vp_dev->pci_dev;
	int err;

	mdev->pci_dev = pci_dev;

	err = vp_modern_probe(mdev);
	if (err)
		return err;

	if (mdev->device)
		vp_dev->vdev.config = &virtio_pci_config_ops;
	else
		vp_dev->vdev.config = &virtio_pci_config_nodev_ops;

	vp_dev->config_vector = vp_config_vector;
	vp_dev->setup_vq = setup_vq;
	vp_dev->del_vq = del_vq;
	vp_dev->isr = mdev->isr;
	vp_dev->vdev.id = mdev->id;

	return 0;
}

1066 1067 1068 1069 1070
/*
 * vp_modern_probe: remove and cleanup the modern virtio pci device
 * @mdev: the modern virtio-pci device
 */
static void vp_modern_remove(struct virtio_pci_modern_device *mdev)
M
Michael S. Tsirkin 已提交
1071
{
J
Jason Wang 已提交
1072 1073 1074 1075 1076 1077 1078 1079 1080
	struct pci_dev *pci_dev = mdev->pci_dev;

	if (mdev->device)
		pci_iounmap(pci_dev, mdev->device);
	if (mdev->notify_base)
		pci_iounmap(pci_dev, mdev->notify_base);
	pci_iounmap(pci_dev, mdev->isr);
	pci_iounmap(pci_dev, mdev->common);
	pci_release_selected_regions(pci_dev, mdev->modern_bars);
M
Michael S. Tsirkin 已提交
1081
}
1082 1083 1084 1085 1086 1087 1088

void virtio_pci_modern_remove(struct virtio_pci_device *vp_dev)
{
	struct virtio_pci_modern_device *mdev = &vp_dev->mdev;

	vp_modern_remove(mdev);
}