1 // SPDX-License-Identifier: GPL-2.0
3 * Physical device callbacks for vfio_ccw
5 * Copyright IBM Corp. 2017
6 * Copyright Red Hat, Inc. 2019
8 * Author(s): Dong Jia Shi <bjsdjshi@linux.vnet.ibm.com>
9 * Xiao Feng Ren <renxiaof@linux.vnet.ibm.com>
10 * Cornelia Huck <cohuck@redhat.com>
13 #include <linux/vfio.h>
14 #include <linux/mdev.h>
15 #include <linux/nospec.h>
16 #include <linux/slab.h>
18 #include "vfio_ccw_private.h"
20 static int vfio_ccw_mdev_reset(struct mdev_device *mdev)
22 struct vfio_ccw_private *private;
23 struct subchannel *sch;
26 private = dev_get_drvdata(mdev_parent_dev(mdev));
30 * In the cureent stage, some things like "no I/O running" and "no
31 * interrupt pending" are clear, but we are not sure what other state
32 * we need to care about.
33 * There are still a lot more instructions need to be handled. We
34 * should come back here later.
36 ret = vfio_ccw_sch_quiesce(sch);
40 ret = cio_enable_subchannel(sch, (u32)(unsigned long)sch);
42 private->state = VFIO_CCW_STATE_IDLE;
47 static int vfio_ccw_mdev_notifier(struct notifier_block *nb,
51 struct vfio_ccw_private *private =
52 container_of(nb, struct vfio_ccw_private, nb);
55 * Vendor drivers MUST unpin pages in response to an
58 if (action == VFIO_IOMMU_NOTIFY_DMA_UNMAP) {
59 struct vfio_iommu_type1_dma_unmap *unmap = data;
61 if (!cp_iova_pinned(&private->cp, unmap->iova))
64 if (vfio_ccw_mdev_reset(private->mdev))
67 cp_free(&private->cp);
74 static ssize_t name_show(struct kobject *kobj, struct device *dev, char *buf)
76 return sprintf(buf, "I/O subchannel (Non-QDIO)\n");
78 static MDEV_TYPE_ATTR_RO(name);
80 static ssize_t device_api_show(struct kobject *kobj, struct device *dev,
83 return sprintf(buf, "%s\n", VFIO_DEVICE_API_CCW_STRING);
85 static MDEV_TYPE_ATTR_RO(device_api);
87 static ssize_t available_instances_show(struct kobject *kobj,
88 struct device *dev, char *buf)
90 struct vfio_ccw_private *private = dev_get_drvdata(dev);
92 return sprintf(buf, "%d\n", atomic_read(&private->avail));
94 static MDEV_TYPE_ATTR_RO(available_instances);
96 static struct attribute *mdev_types_attrs[] = {
97 &mdev_type_attr_name.attr,
98 &mdev_type_attr_device_api.attr,
99 &mdev_type_attr_available_instances.attr,
103 static struct attribute_group mdev_type_group = {
105 .attrs = mdev_types_attrs,
108 static struct attribute_group *mdev_type_groups[] = {
113 static int vfio_ccw_mdev_create(struct kobject *kobj, struct mdev_device *mdev)
115 struct vfio_ccw_private *private =
116 dev_get_drvdata(mdev_parent_dev(mdev));
118 if (private->state == VFIO_CCW_STATE_NOT_OPER)
121 if (atomic_dec_if_positive(&private->avail) < 0)
124 private->mdev = mdev;
125 private->state = VFIO_CCW_STATE_IDLE;
127 VFIO_CCW_MSG_EVENT(2, "mdev %pUl, sch %x.%x.%04x: create\n",
128 mdev_uuid(mdev), private->sch->schid.cssid,
129 private->sch->schid.ssid,
130 private->sch->schid.sch_no);
135 static int vfio_ccw_mdev_remove(struct mdev_device *mdev)
137 struct vfio_ccw_private *private =
138 dev_get_drvdata(mdev_parent_dev(mdev));
140 VFIO_CCW_MSG_EVENT(2, "mdev %pUl, sch %x.%x.%04x: remove\n",
141 mdev_uuid(mdev), private->sch->schid.cssid,
142 private->sch->schid.ssid,
143 private->sch->schid.sch_no);
145 if ((private->state != VFIO_CCW_STATE_NOT_OPER) &&
146 (private->state != VFIO_CCW_STATE_STANDBY)) {
147 if (!vfio_ccw_sch_quiesce(private->sch))
148 private->state = VFIO_CCW_STATE_STANDBY;
149 /* The state will be NOT_OPER on error. */
152 cp_free(&private->cp);
153 private->mdev = NULL;
154 atomic_inc(&private->avail);
159 static int vfio_ccw_mdev_open(struct mdev_device *mdev)
161 struct vfio_ccw_private *private =
162 dev_get_drvdata(mdev_parent_dev(mdev));
163 unsigned long events = VFIO_IOMMU_NOTIFY_DMA_UNMAP;
166 private->nb.notifier_call = vfio_ccw_mdev_notifier;
168 ret = vfio_register_notifier(mdev_dev(mdev), VFIO_IOMMU_NOTIFY,
169 &events, &private->nb);
173 ret = vfio_ccw_register_async_dev_regions(private);
177 ret = vfio_ccw_register_schib_dev_regions(private);
181 ret = vfio_ccw_register_crw_dev_regions(private);
188 vfio_ccw_unregister_dev_regions(private);
189 vfio_unregister_notifier(mdev_dev(mdev), VFIO_IOMMU_NOTIFY,
194 static void vfio_ccw_mdev_release(struct mdev_device *mdev)
196 struct vfio_ccw_private *private =
197 dev_get_drvdata(mdev_parent_dev(mdev));
199 if ((private->state != VFIO_CCW_STATE_NOT_OPER) &&
200 (private->state != VFIO_CCW_STATE_STANDBY)) {
201 if (!vfio_ccw_mdev_reset(mdev))
202 private->state = VFIO_CCW_STATE_STANDBY;
203 /* The state will be NOT_OPER on error. */
206 cp_free(&private->cp);
207 vfio_ccw_unregister_dev_regions(private);
208 vfio_unregister_notifier(mdev_dev(mdev), VFIO_IOMMU_NOTIFY,
212 static ssize_t vfio_ccw_mdev_read_io_region(struct vfio_ccw_private *private,
213 char __user *buf, size_t count,
216 loff_t pos = *ppos & VFIO_CCW_OFFSET_MASK;
217 struct ccw_io_region *region;
220 if (pos + count > sizeof(*region))
223 mutex_lock(&private->io_mutex);
224 region = private->io_region;
225 if (copy_to_user(buf, (void *)region + pos, count))
229 mutex_unlock(&private->io_mutex);
233 static ssize_t vfio_ccw_mdev_read(struct mdev_device *mdev,
238 unsigned int index = VFIO_CCW_OFFSET_TO_INDEX(*ppos);
239 struct vfio_ccw_private *private;
241 private = dev_get_drvdata(mdev_parent_dev(mdev));
243 if (index >= VFIO_CCW_NUM_REGIONS + private->num_regions)
247 case VFIO_CCW_CONFIG_REGION_INDEX:
248 return vfio_ccw_mdev_read_io_region(private, buf, count, ppos);
250 index -= VFIO_CCW_NUM_REGIONS;
251 return private->region[index].ops->read(private, buf, count,
258 static ssize_t vfio_ccw_mdev_write_io_region(struct vfio_ccw_private *private,
259 const char __user *buf,
260 size_t count, loff_t *ppos)
262 loff_t pos = *ppos & VFIO_CCW_OFFSET_MASK;
263 struct ccw_io_region *region;
266 if (pos + count > sizeof(*region))
269 if (!mutex_trylock(&private->io_mutex))
272 region = private->io_region;
273 if (copy_from_user((void *)region + pos, buf, count)) {
278 vfio_ccw_fsm_event(private, VFIO_CCW_EVENT_IO_REQ);
279 ret = (region->ret_code != 0) ? region->ret_code : count;
282 mutex_unlock(&private->io_mutex);
286 static ssize_t vfio_ccw_mdev_write(struct mdev_device *mdev,
287 const char __user *buf,
291 unsigned int index = VFIO_CCW_OFFSET_TO_INDEX(*ppos);
292 struct vfio_ccw_private *private;
294 private = dev_get_drvdata(mdev_parent_dev(mdev));
296 if (index >= VFIO_CCW_NUM_REGIONS + private->num_regions)
300 case VFIO_CCW_CONFIG_REGION_INDEX:
301 return vfio_ccw_mdev_write_io_region(private, buf, count, ppos);
303 index -= VFIO_CCW_NUM_REGIONS;
304 return private->region[index].ops->write(private, buf, count,
311 static int vfio_ccw_mdev_get_device_info(struct vfio_device_info *info,
312 struct mdev_device *mdev)
314 struct vfio_ccw_private *private;
316 private = dev_get_drvdata(mdev_parent_dev(mdev));
317 info->flags = VFIO_DEVICE_FLAGS_CCW | VFIO_DEVICE_FLAGS_RESET;
318 info->num_regions = VFIO_CCW_NUM_REGIONS + private->num_regions;
319 info->num_irqs = VFIO_CCW_NUM_IRQS;
324 static int vfio_ccw_mdev_get_region_info(struct vfio_region_info *info,
325 struct mdev_device *mdev,
328 struct vfio_ccw_private *private;
331 private = dev_get_drvdata(mdev_parent_dev(mdev));
332 switch (info->index) {
333 case VFIO_CCW_CONFIG_REGION_INDEX:
335 info->size = sizeof(struct ccw_io_region);
336 info->flags = VFIO_REGION_INFO_FLAG_READ
337 | VFIO_REGION_INFO_FLAG_WRITE;
339 default: /* all other regions are handled via capability chain */
341 struct vfio_info_cap caps = { .buf = NULL, .size = 0 };
342 struct vfio_region_info_cap_type cap_type = {
343 .header.id = VFIO_REGION_INFO_CAP_TYPE,
344 .header.version = 1 };
348 VFIO_CCW_NUM_REGIONS + private->num_regions)
351 info->index = array_index_nospec(info->index,
352 VFIO_CCW_NUM_REGIONS +
353 private->num_regions);
355 i = info->index - VFIO_CCW_NUM_REGIONS;
357 info->offset = VFIO_CCW_INDEX_TO_OFFSET(info->index);
358 info->size = private->region[i].size;
359 info->flags = private->region[i].flags;
361 cap_type.type = private->region[i].type;
362 cap_type.subtype = private->region[i].subtype;
364 ret = vfio_info_add_capability(&caps, &cap_type.header,
369 info->flags |= VFIO_REGION_INFO_FLAG_CAPS;
370 if (info->argsz < sizeof(*info) + caps.size) {
371 info->argsz = sizeof(*info) + caps.size;
372 info->cap_offset = 0;
374 vfio_info_cap_shift(&caps, sizeof(*info));
375 if (copy_to_user((void __user *)arg + sizeof(*info),
376 caps.buf, caps.size)) {
380 info->cap_offset = sizeof(*info);
390 static int vfio_ccw_mdev_get_irq_info(struct vfio_irq_info *info)
392 switch (info->index) {
393 case VFIO_CCW_IO_IRQ_INDEX:
394 case VFIO_CCW_CRW_IRQ_INDEX:
396 info->flags = VFIO_IRQ_INFO_EVENTFD;
405 static int vfio_ccw_mdev_set_irqs(struct mdev_device *mdev,
410 struct vfio_ccw_private *private;
411 struct eventfd_ctx **ctx;
413 if (!(flags & VFIO_IRQ_SET_ACTION_TRIGGER))
416 private = dev_get_drvdata(mdev_parent_dev(mdev));
419 case VFIO_CCW_IO_IRQ_INDEX:
420 ctx = &private->io_trigger;
422 case VFIO_CCW_CRW_IRQ_INDEX:
423 ctx = &private->crw_trigger;
429 switch (flags & VFIO_IRQ_SET_DATA_TYPE_MASK) {
430 case VFIO_IRQ_SET_DATA_NONE:
433 eventfd_signal(*ctx, 1);
436 case VFIO_IRQ_SET_DATA_BOOL:
440 if (get_user(trigger, (uint8_t __user *)data))
444 eventfd_signal(*ctx, 1);
447 case VFIO_IRQ_SET_DATA_EVENTFD:
451 if (get_user(fd, (int32_t __user *)data))
456 eventfd_ctx_put(*ctx);
458 } else if (fd >= 0) {
459 struct eventfd_ctx *efdctx;
461 efdctx = eventfd_ctx_fdget(fd);
463 return PTR_ERR(efdctx);
466 eventfd_ctx_put(*ctx);
479 int vfio_ccw_register_dev_region(struct vfio_ccw_private *private,
480 unsigned int subtype,
481 const struct vfio_ccw_regops *ops,
482 size_t size, u32 flags, void *data)
484 struct vfio_ccw_region *region;
486 region = krealloc(private->region,
487 (private->num_regions + 1) * sizeof(*region),
492 private->region = region;
493 private->region[private->num_regions].type = VFIO_REGION_TYPE_CCW;
494 private->region[private->num_regions].subtype = subtype;
495 private->region[private->num_regions].ops = ops;
496 private->region[private->num_regions].size = size;
497 private->region[private->num_regions].flags = flags;
498 private->region[private->num_regions].data = data;
500 private->num_regions++;
505 void vfio_ccw_unregister_dev_regions(struct vfio_ccw_private *private)
509 for (i = 0; i < private->num_regions; i++)
510 private->region[i].ops->release(private, &private->region[i]);
511 private->num_regions = 0;
512 kfree(private->region);
513 private->region = NULL;
516 static ssize_t vfio_ccw_mdev_ioctl(struct mdev_device *mdev,
524 case VFIO_DEVICE_GET_INFO:
526 struct vfio_device_info info;
528 minsz = offsetofend(struct vfio_device_info, num_irqs);
530 if (copy_from_user(&info, (void __user *)arg, minsz))
533 if (info.argsz < minsz)
536 ret = vfio_ccw_mdev_get_device_info(&info, mdev);
540 return copy_to_user((void __user *)arg, &info, minsz) ? -EFAULT : 0;
542 case VFIO_DEVICE_GET_REGION_INFO:
544 struct vfio_region_info info;
546 minsz = offsetofend(struct vfio_region_info, offset);
548 if (copy_from_user(&info, (void __user *)arg, minsz))
551 if (info.argsz < minsz)
554 ret = vfio_ccw_mdev_get_region_info(&info, mdev, arg);
558 return copy_to_user((void __user *)arg, &info, minsz) ? -EFAULT : 0;
560 case VFIO_DEVICE_GET_IRQ_INFO:
562 struct vfio_irq_info info;
564 minsz = offsetofend(struct vfio_irq_info, count);
566 if (copy_from_user(&info, (void __user *)arg, minsz))
569 if (info.argsz < minsz || info.index >= VFIO_CCW_NUM_IRQS)
572 ret = vfio_ccw_mdev_get_irq_info(&info);
576 if (info.count == -1)
579 return copy_to_user((void __user *)arg, &info, minsz) ? -EFAULT : 0;
581 case VFIO_DEVICE_SET_IRQS:
583 struct vfio_irq_set hdr;
587 minsz = offsetofend(struct vfio_irq_set, count);
589 if (copy_from_user(&hdr, (void __user *)arg, minsz))
592 ret = vfio_set_irqs_validate_and_prepare(&hdr, 1,
598 data = (void __user *)(arg + minsz);
599 return vfio_ccw_mdev_set_irqs(mdev, hdr.flags, hdr.index, data);
601 case VFIO_DEVICE_RESET:
602 return vfio_ccw_mdev_reset(mdev);
608 static const struct mdev_parent_ops vfio_ccw_mdev_ops = {
609 .owner = THIS_MODULE,
610 .supported_type_groups = mdev_type_groups,
611 .create = vfio_ccw_mdev_create,
612 .remove = vfio_ccw_mdev_remove,
613 .open = vfio_ccw_mdev_open,
614 .release = vfio_ccw_mdev_release,
615 .read = vfio_ccw_mdev_read,
616 .write = vfio_ccw_mdev_write,
617 .ioctl = vfio_ccw_mdev_ioctl,
620 int vfio_ccw_mdev_reg(struct subchannel *sch)
622 return mdev_register_device(&sch->dev, &vfio_ccw_mdev_ops);
625 void vfio_ccw_mdev_unreg(struct subchannel *sch)
627 mdev_unregister_device(&sch->dev);