vfio_ccw_ops.c 15.3 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2 3 4 5
/*
 * Physical device callbacks for vfio_ccw
 *
 * Copyright IBM Corp. 2017
6
 * Copyright Red Hat, Inc. 2019
7 8 9
 *
 * Author(s): Dong Jia Shi <bjsdjshi@linux.vnet.ibm.com>
 *            Xiao Feng Ren <renxiaof@linux.vnet.ibm.com>
10
 *            Cornelia Huck <cohuck@redhat.com>
11 12 13
 */

#include <linux/vfio.h>
14 15
#include <linux/nospec.h>
#include <linux/slab.h>
16 17 18

#include "vfio_ccw_private.h"

19 20
static const struct vfio_device_ops vfio_ccw_dev_ops;

21
static int vfio_ccw_mdev_reset(struct vfio_ccw_private *private)
22 23
{
	/*
24 25
	 * If the FSM state is seen as Not Operational after closing
	 * and re-opening the mdev, return an error.
26
	 */
27
	vfio_ccw_fsm_event(private, VFIO_CCW_EVENT_CLOSE);
28 29 30
	vfio_ccw_fsm_event(private, VFIO_CCW_EVENT_OPEN);
	if (private->state == VFIO_CCW_STATE_NOT_OPER)
		return -EINVAL;
31

32
	return 0;
33 34
}

35
static void vfio_ccw_dma_unmap(struct vfio_device *vdev, u64 iova, u64 length)
36 37
{
	struct vfio_ccw_private *private =
38
		container_of(vdev, struct vfio_ccw_private, vdev);
39

40
	/* Drivers MUST unpin pages in response to an invalidation. */
41
	if (!cp_iova_pinned(&private->cp, iova, length))
42
		return;
43

44
	vfio_ccw_mdev_reset(private);
45 46
}

47 48
static ssize_t name_show(struct mdev_type *mtype,
			 struct mdev_type_attribute *attr, char *buf)
49 50 51
{
	return sprintf(buf, "I/O subchannel (Non-QDIO)\n");
}
52
static MDEV_TYPE_ATTR_RO(name);
53

54 55
static ssize_t device_api_show(struct mdev_type *mtype,
			       struct mdev_type_attribute *attr, char *buf)
56 57 58
{
	return sprintf(buf, "%s\n", VFIO_DEVICE_API_CCW_STRING);
}
59
static MDEV_TYPE_ATTR_RO(device_api);
60

61 62 63
static ssize_t available_instances_show(struct mdev_type *mtype,
					struct mdev_type_attribute *attr,
					char *buf)
64
{
65 66
	struct vfio_ccw_private *private =
		dev_get_drvdata(mtype_get_parent_dev(mtype));
67 68 69

	return sprintf(buf, "%d\n", atomic_read(&private->avail));
}
70
static MDEV_TYPE_ATTR_RO(available_instances);
71 72 73 74 75 76 77 78 79 80 81 82 83

static struct attribute *mdev_types_attrs[] = {
	&mdev_type_attr_name.attr,
	&mdev_type_attr_device_api.attr,
	&mdev_type_attr_available_instances.attr,
	NULL,
};

static struct attribute_group mdev_type_group = {
	.name  = "io",
	.attrs = mdev_types_attrs,
};

84
static struct attribute_group *mdev_type_groups[] = {
85 86 87 88
	&mdev_type_group,
	NULL,
};

89 90 91 92 93 94 95 96 97
static int vfio_ccw_mdev_init_dev(struct vfio_device *vdev)
{
	struct vfio_ccw_private *private =
		container_of(vdev, struct vfio_ccw_private, vdev);

	init_completion(&private->release_comp);
	return 0;
}

98
static int vfio_ccw_mdev_probe(struct mdev_device *mdev)
99
{
100 101
	struct vfio_ccw_private *private = dev_get_drvdata(mdev->dev.parent);
	int ret;
102

103 104 105
	if (private->state == VFIO_CCW_STATE_NOT_OPER)
		return -ENODEV;

106 107 108
	if (atomic_dec_if_positive(&private->avail) < 0)
		return -EPERM;

109 110 111
	ret = vfio_init_device(&private->vdev, &mdev->dev, &vfio_ccw_dev_ops);
	if (ret)
		return ret;
112

113 114
	VFIO_CCW_MSG_EVENT(2, "sch %x.%x.%04x: create\n",
			   private->sch->schid.cssid,
Cornelia Huck's avatar
Cornelia Huck committed
115 116 117
			   private->sch->schid.ssid,
			   private->sch->schid.sch_no);

118 119
	ret = vfio_register_emulated_iommu_dev(&private->vdev);
	if (ret)
120
		goto err_put_vdev;
121
	dev_set_drvdata(&mdev->dev, private);
122
	return 0;
123

124 125
err_put_vdev:
	vfio_put_device(&private->vdev);
126 127
	atomic_inc(&private->avail);
	return ret;
128 129
}

130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
static void vfio_ccw_mdev_release_dev(struct vfio_device *vdev)
{
	struct vfio_ccw_private *private =
		container_of(vdev, struct vfio_ccw_private, vdev);

	/*
	 * We cannot free vfio_ccw_private here because it includes
	 * parent info which must be free'ed by css driver.
	 *
	 * Use a workaround by memset'ing the core device part and
	 * then notifying the remove path that all active references
	 * to this device have been released.
	 */
	memset(vdev, 0, sizeof(*vdev));
	complete(&private->release_comp);
}

147
static void vfio_ccw_mdev_remove(struct mdev_device *mdev)
148
{
149
	struct vfio_ccw_private *private = dev_get_drvdata(mdev->dev.parent);
150

151 152
	VFIO_CCW_MSG_EVENT(2, "sch %x.%x.%04x: remove\n",
			   private->sch->schid.cssid,
Cornelia Huck's avatar
Cornelia Huck committed
153 154 155
			   private->sch->schid.ssid,
			   private->sch->schid.sch_no);

156 157
	vfio_unregister_group_dev(&private->vdev);

158 159 160 161 162 163 164 165 166 167 168
	vfio_put_device(&private->vdev);
	/*
	 * Wait for all active references on mdev are released so it
	 * is safe to defer kfree() to a later point.
	 *
	 * TODO: the clean fix is to split parent/mdev info from ccw
	 * private structure so each can be managed in its own life
	 * cycle.
	 */
	wait_for_completion(&private->release_comp);

169 170 171
	atomic_inc(&private->avail);
}

172
static int vfio_ccw_mdev_open_device(struct vfio_device *vdev)
173 174
{
	struct vfio_ccw_private *private =
175
		container_of(vdev, struct vfio_ccw_private, vdev);
176
	int ret;
177

178 179 180 181
	/* Device cannot simply be opened again from this state */
	if (private->state == VFIO_CCW_STATE_NOT_OPER)
		return -EINVAL;

182 183
	ret = vfio_ccw_register_async_dev_regions(private);
	if (ret)
184
		return ret;
185 186 187 188 189

	ret = vfio_ccw_register_schib_dev_regions(private);
	if (ret)
		goto out_unregister;

190 191 192 193
	ret = vfio_ccw_register_crw_dev_regions(private);
	if (ret)
		goto out_unregister;

194 195 196 197 198 199
	vfio_ccw_fsm_event(private, VFIO_CCW_EVENT_OPEN);
	if (private->state == VFIO_CCW_STATE_NOT_OPER) {
		ret = -EINVAL;
		goto out_unregister;
	}

200 201 202 203
	return ret;

out_unregister:
	vfio_ccw_unregister_dev_regions(private);
204
	return ret;
205 206
}

207
static void vfio_ccw_mdev_close_device(struct vfio_device *vdev)
208 209
{
	struct vfio_ccw_private *private =
210
		container_of(vdev, struct vfio_ccw_private, vdev);
211

212
	vfio_ccw_fsm_event(private, VFIO_CCW_EVENT_CLOSE);
213
	vfio_ccw_unregister_dev_regions(private);
214 215
}

216 217 218
static ssize_t vfio_ccw_mdev_read_io_region(struct vfio_ccw_private *private,
					    char __user *buf, size_t count,
					    loff_t *ppos)
219
{
220
	loff_t pos = *ppos & VFIO_CCW_OFFSET_MASK;
221
	struct ccw_io_region *region;
222
	int ret;
223

224
	if (pos + count > sizeof(*region))
225 226
		return -EINVAL;

227
	mutex_lock(&private->io_mutex);
228
	region = private->io_region;
229
	if (copy_to_user(buf, (void *)region + pos, count))
230 231 232 233 234
		ret = -EFAULT;
	else
		ret = count;
	mutex_unlock(&private->io_mutex);
	return ret;
235 236
}

237
static ssize_t vfio_ccw_mdev_read(struct vfio_device *vdev,
238 239 240
				  char __user *buf,
				  size_t count,
				  loff_t *ppos)
241
{
242 243
	struct vfio_ccw_private *private =
		container_of(vdev, struct vfio_ccw_private, vdev);
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
	unsigned int index = VFIO_CCW_OFFSET_TO_INDEX(*ppos);

	if (index >= VFIO_CCW_NUM_REGIONS + private->num_regions)
		return -EINVAL;

	switch (index) {
	case VFIO_CCW_CONFIG_REGION_INDEX:
		return vfio_ccw_mdev_read_io_region(private, buf, count, ppos);
	default:
		index -= VFIO_CCW_NUM_REGIONS;
		return private->region[index].ops->read(private, buf, count,
							ppos);
	}

	return -EINVAL;
}

static ssize_t vfio_ccw_mdev_write_io_region(struct vfio_ccw_private *private,
					     const char __user *buf,
					     size_t count, loff_t *ppos)
{
	loff_t pos = *ppos & VFIO_CCW_OFFSET_MASK;
266
	struct ccw_io_region *region;
267
	int ret;
268

269
	if (pos + count > sizeof(*region))
270 271
		return -EINVAL;

272 273
	if (!mutex_trylock(&private->io_mutex))
		return -EAGAIN;
274

275
	region = private->io_region;
276
	if (copy_from_user((void *)region + pos, buf, count)) {
277 278 279
		ret = -EFAULT;
		goto out_unlock;
	}
280

281
	vfio_ccw_fsm_event(private, VFIO_CCW_EVENT_IO_REQ);
282
	ret = (region->ret_code != 0) ? region->ret_code : count;
283

284 285 286
out_unlock:
	mutex_unlock(&private->io_mutex);
	return ret;
287 288
}

289
static ssize_t vfio_ccw_mdev_write(struct vfio_device *vdev,
290 291 292
				   const char __user *buf,
				   size_t count,
				   loff_t *ppos)
293
{
294 295
	struct vfio_ccw_private *private =
		container_of(vdev, struct vfio_ccw_private, vdev);
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
	unsigned int index = VFIO_CCW_OFFSET_TO_INDEX(*ppos);

	if (index >= VFIO_CCW_NUM_REGIONS + private->num_regions)
		return -EINVAL;

	switch (index) {
	case VFIO_CCW_CONFIG_REGION_INDEX:
		return vfio_ccw_mdev_write_io_region(private, buf, count, ppos);
	default:
		index -= VFIO_CCW_NUM_REGIONS;
		return private->region[index].ops->write(private, buf, count,
							 ppos);
	}

	return -EINVAL;
}

313 314
static int vfio_ccw_mdev_get_device_info(struct vfio_ccw_private *private,
					 struct vfio_device_info *info)
315
{
316
	info->flags = VFIO_DEVICE_FLAGS_CCW | VFIO_DEVICE_FLAGS_RESET;
317
	info->num_regions = VFIO_CCW_NUM_REGIONS + private->num_regions;
318
	info->num_irqs = VFIO_CCW_NUM_IRQS;
319 320 321 322

	return 0;
}

323 324
static int vfio_ccw_mdev_get_region_info(struct vfio_ccw_private *private,
					 struct vfio_region_info *info,
325
					 unsigned long arg)
326
{
327 328
	int i;

329 330 331 332 333 334 335
	switch (info->index) {
	case VFIO_CCW_CONFIG_REGION_INDEX:
		info->offset = 0;
		info->size = sizeof(struct ccw_io_region);
		info->flags = VFIO_REGION_INFO_FLAG_READ
			      | VFIO_REGION_INFO_FLAG_WRITE;
		return 0;
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
	default: /* all other regions are handled via capability chain */
	{
		struct vfio_info_cap caps = { .buf = NULL, .size = 0 };
		struct vfio_region_info_cap_type cap_type = {
			.header.id = VFIO_REGION_INFO_CAP_TYPE,
			.header.version = 1 };
		int ret;

		if (info->index >=
		    VFIO_CCW_NUM_REGIONS + private->num_regions)
			return -EINVAL;

		info->index = array_index_nospec(info->index,
						 VFIO_CCW_NUM_REGIONS +
						 private->num_regions);

		i = info->index - VFIO_CCW_NUM_REGIONS;

		info->offset = VFIO_CCW_INDEX_TO_OFFSET(info->index);
		info->size = private->region[i].size;
		info->flags = private->region[i].flags;

		cap_type.type = private->region[i].type;
		cap_type.subtype = private->region[i].subtype;

		ret = vfio_info_add_capability(&caps, &cap_type.header,
					       sizeof(cap_type));
		if (ret)
			return ret;

		info->flags |= VFIO_REGION_INFO_FLAG_CAPS;
		if (info->argsz < sizeof(*info) + caps.size) {
			info->argsz = sizeof(*info) + caps.size;
			info->cap_offset = 0;
		} else {
			vfio_info_cap_shift(&caps, sizeof(*info));
			if (copy_to_user((void __user *)arg + sizeof(*info),
					 caps.buf, caps.size)) {
				kfree(caps.buf);
				return -EFAULT;
			}
			info->cap_offset = sizeof(*info);
		}

		kfree(caps.buf);

	}
383
	}
384
	return 0;
385 386
}

387
static int vfio_ccw_mdev_get_irq_info(struct vfio_irq_info *info)
388
{
Eric Farman's avatar
Eric Farman committed
389 390
	switch (info->index) {
	case VFIO_CCW_IO_IRQ_INDEX:
391
	case VFIO_CCW_CRW_IRQ_INDEX:
392
	case VFIO_CCW_REQ_IRQ_INDEX:
Eric Farman's avatar
Eric Farman committed
393 394 395 396
		info->count = 1;
		info->flags = VFIO_IRQ_INFO_EVENTFD;
		break;
	default:
397
		return -EINVAL;
Eric Farman's avatar
Eric Farman committed
398
	}
399 400 401 402

	return 0;
}

403
static int vfio_ccw_mdev_set_irqs(struct vfio_ccw_private *private,
404
				  uint32_t flags,
Eric Farman's avatar
Eric Farman committed
405
				  uint32_t index,
406 407 408 409 410 411 412
				  void __user *data)
{
	struct eventfd_ctx **ctx;

	if (!(flags & VFIO_IRQ_SET_ACTION_TRIGGER))
		return -EINVAL;

Eric Farman's avatar
Eric Farman committed
413 414 415 416
	switch (index) {
	case VFIO_CCW_IO_IRQ_INDEX:
		ctx = &private->io_trigger;
		break;
417 418 419
	case VFIO_CCW_CRW_IRQ_INDEX:
		ctx = &private->crw_trigger;
		break;
420 421 422
	case VFIO_CCW_REQ_IRQ_INDEX:
		ctx = &private->req_trigger;
		break;
Eric Farman's avatar
Eric Farman committed
423 424 425
	default:
		return -EINVAL;
	}
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476

	switch (flags & VFIO_IRQ_SET_DATA_TYPE_MASK) {
	case VFIO_IRQ_SET_DATA_NONE:
	{
		if (*ctx)
			eventfd_signal(*ctx, 1);
		return 0;
	}
	case VFIO_IRQ_SET_DATA_BOOL:
	{
		uint8_t trigger;

		if (get_user(trigger, (uint8_t __user *)data))
			return -EFAULT;

		if (trigger && *ctx)
			eventfd_signal(*ctx, 1);
		return 0;
	}
	case VFIO_IRQ_SET_DATA_EVENTFD:
	{
		int32_t fd;

		if (get_user(fd, (int32_t __user *)data))
			return -EFAULT;

		if (fd == -1) {
			if (*ctx)
				eventfd_ctx_put(*ctx);
			*ctx = NULL;
		} else if (fd >= 0) {
			struct eventfd_ctx *efdctx;

			efdctx = eventfd_ctx_fdget(fd);
			if (IS_ERR(efdctx))
				return PTR_ERR(efdctx);

			if (*ctx)
				eventfd_ctx_put(*ctx);

			*ctx = efdctx;
		} else
			return -EINVAL;

		return 0;
	}
	default:
		return -EINVAL;
	}
}

477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
int vfio_ccw_register_dev_region(struct vfio_ccw_private *private,
				 unsigned int subtype,
				 const struct vfio_ccw_regops *ops,
				 size_t size, u32 flags, void *data)
{
	struct vfio_ccw_region *region;

	region = krealloc(private->region,
			  (private->num_regions + 1) * sizeof(*region),
			  GFP_KERNEL);
	if (!region)
		return -ENOMEM;

	private->region = region;
	private->region[private->num_regions].type = VFIO_REGION_TYPE_CCW;
	private->region[private->num_regions].subtype = subtype;
	private->region[private->num_regions].ops = ops;
	private->region[private->num_regions].size = size;
	private->region[private->num_regions].flags = flags;
	private->region[private->num_regions].data = data;

	private->num_regions++;

	return 0;
}

503 504 505 506 507 508 509 510 511 512 513
void vfio_ccw_unregister_dev_regions(struct vfio_ccw_private *private)
{
	int i;

	for (i = 0; i < private->num_regions; i++)
		private->region[i].ops->release(private, &private->region[i]);
	private->num_regions = 0;
	kfree(private->region);
	private->region = NULL;
}

514
static ssize_t vfio_ccw_mdev_ioctl(struct vfio_device *vdev,
515 516 517
				   unsigned int cmd,
				   unsigned long arg)
{
518
	struct vfio_ccw_private *private =
519
		container_of(vdev, struct vfio_ccw_private, vdev);
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
	int ret = 0;
	unsigned long minsz;

	switch (cmd) {
	case VFIO_DEVICE_GET_INFO:
	{
		struct vfio_device_info info;

		minsz = offsetofend(struct vfio_device_info, num_irqs);

		if (copy_from_user(&info, (void __user *)arg, minsz))
			return -EFAULT;

		if (info.argsz < minsz)
			return -EINVAL;

536
		ret = vfio_ccw_mdev_get_device_info(private, &info);
537 538 539
		if (ret)
			return ret;

540
		return copy_to_user((void __user *)arg, &info, minsz) ? -EFAULT : 0;
541 542 543 544 545 546 547 548 549 550 551 552 553
	}
	case VFIO_DEVICE_GET_REGION_INFO:
	{
		struct vfio_region_info info;

		minsz = offsetofend(struct vfio_region_info, offset);

		if (copy_from_user(&info, (void __user *)arg, minsz))
			return -EFAULT;

		if (info.argsz < minsz)
			return -EINVAL;

554
		ret = vfio_ccw_mdev_get_region_info(private, &info, arg);
555 556 557
		if (ret)
			return ret;

558
		return copy_to_user((void __user *)arg, &info, minsz) ? -EFAULT : 0;
559
	}
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
	case VFIO_DEVICE_GET_IRQ_INFO:
	{
		struct vfio_irq_info info;

		minsz = offsetofend(struct vfio_irq_info, count);

		if (copy_from_user(&info, (void __user *)arg, minsz))
			return -EFAULT;

		if (info.argsz < minsz || info.index >= VFIO_CCW_NUM_IRQS)
			return -EINVAL;

		ret = vfio_ccw_mdev_get_irq_info(&info);
		if (ret)
			return ret;

		if (info.count == -1)
			return -EINVAL;

579
		return copy_to_user((void __user *)arg, &info, minsz) ? -EFAULT : 0;
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
	}
	case VFIO_DEVICE_SET_IRQS:
	{
		struct vfio_irq_set hdr;
		size_t data_size;
		void __user *data;

		minsz = offsetofend(struct vfio_irq_set, count);

		if (copy_from_user(&hdr, (void __user *)arg, minsz))
			return -EFAULT;

		ret = vfio_set_irqs_validate_and_prepare(&hdr, 1,
							 VFIO_CCW_NUM_IRQS,
							 &data_size);
		if (ret)
			return ret;

		data = (void __user *)(arg + minsz);
599 600
		return vfio_ccw_mdev_set_irqs(private, hdr.flags, hdr.index,
					      data);
601
	}
602
	case VFIO_DEVICE_RESET:
603
		return vfio_ccw_mdev_reset(private);
604 605 606 607 608
	default:
		return -ENOTTY;
	}
}

609
/* Request removal of the device*/
610
static void vfio_ccw_mdev_request(struct vfio_device *vdev, unsigned int count)
611
{
612 613 614
	struct vfio_ccw_private *private =
		container_of(vdev, struct vfio_ccw_private, vdev);
	struct device *dev = vdev->dev;
615 616 617

	if (private->req_trigger) {
		if (!(count % 10))
618
			dev_notice_ratelimited(dev,
619 620 621 622 623
					       "Relaying device request to user (#%u)\n",
					       count);

		eventfd_signal(private->req_trigger, 1);
	} else if (count == 0) {
624
		dev_notice(dev,
625 626 627 628
			   "No device request channel registered, blocked until released by user\n");
	}
}

629
static const struct vfio_device_ops vfio_ccw_dev_ops = {
630 631
	.init = vfio_ccw_mdev_init_dev,
	.release = vfio_ccw_mdev_release_dev,
632 633 634 635 636 637
	.open_device = vfio_ccw_mdev_open_device,
	.close_device = vfio_ccw_mdev_close_device,
	.read = vfio_ccw_mdev_read,
	.write = vfio_ccw_mdev_write,
	.ioctl = vfio_ccw_mdev_ioctl,
	.request = vfio_ccw_mdev_request,
638
	.dma_unmap = vfio_ccw_dma_unmap,
639 640 641 642 643 644 645 646 647 648
};

struct mdev_driver vfio_ccw_mdev_driver = {
	.driver = {
		.name = "vfio_ccw_mdev",
		.owner = THIS_MODULE,
		.mod_name = KBUILD_MODNAME,
	},
	.probe = vfio_ccw_mdev_probe,
	.remove = vfio_ccw_mdev_remove,
649 650
	.supported_type_groups  = mdev_type_groups,
};