Dong Jia Shi | 84cd8fc | 2017-03-17 04:17:33 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Physical device callbacks for vfio_ccw |
| 3 | * |
| 4 | * Copyright IBM Corp. 2017 |
| 5 | * |
| 6 | * Author(s): Dong Jia Shi <bjsdjshi@linux.vnet.ibm.com> |
| 7 | * Xiao Feng Ren <renxiaof@linux.vnet.ibm.com> |
| 8 | */ |
| 9 | |
| 10 | #include <linux/vfio.h> |
| 11 | #include <linux/mdev.h> |
| 12 | |
| 13 | #include "vfio_ccw_private.h" |
| 14 | |
| 15 | static int vfio_ccw_mdev_notifier(struct notifier_block *nb, |
| 16 | unsigned long action, |
| 17 | void *data) |
| 18 | { |
| 19 | struct vfio_ccw_private *private = |
| 20 | container_of(nb, struct vfio_ccw_private, nb); |
| 21 | |
| 22 | if (!private) |
| 23 | return NOTIFY_STOP; |
| 24 | |
| 25 | /* |
| 26 | * TODO: |
| 27 | * Vendor drivers MUST unpin pages in response to an |
| 28 | * invalidation. |
| 29 | */ |
| 30 | if (action == VFIO_IOMMU_NOTIFY_DMA_UNMAP) |
| 31 | return NOTIFY_BAD; |
| 32 | |
| 33 | return NOTIFY_DONE; |
| 34 | } |
| 35 | |
| 36 | static ssize_t name_show(struct kobject *kobj, struct device *dev, char *buf) |
| 37 | { |
| 38 | return sprintf(buf, "I/O subchannel (Non-QDIO)\n"); |
| 39 | } |
| 40 | MDEV_TYPE_ATTR_RO(name); |
| 41 | |
| 42 | static ssize_t device_api_show(struct kobject *kobj, struct device *dev, |
| 43 | char *buf) |
| 44 | { |
| 45 | return sprintf(buf, "%s\n", VFIO_DEVICE_API_CCW_STRING); |
| 46 | } |
| 47 | MDEV_TYPE_ATTR_RO(device_api); |
| 48 | |
| 49 | static ssize_t available_instances_show(struct kobject *kobj, |
| 50 | struct device *dev, char *buf) |
| 51 | { |
| 52 | struct vfio_ccw_private *private = dev_get_drvdata(dev); |
| 53 | |
| 54 | return sprintf(buf, "%d\n", atomic_read(&private->avail)); |
| 55 | } |
| 56 | MDEV_TYPE_ATTR_RO(available_instances); |
| 57 | |
| 58 | static struct attribute *mdev_types_attrs[] = { |
| 59 | &mdev_type_attr_name.attr, |
| 60 | &mdev_type_attr_device_api.attr, |
| 61 | &mdev_type_attr_available_instances.attr, |
| 62 | NULL, |
| 63 | }; |
| 64 | |
| 65 | static struct attribute_group mdev_type_group = { |
| 66 | .name = "io", |
| 67 | .attrs = mdev_types_attrs, |
| 68 | }; |
| 69 | |
| 70 | struct attribute_group *mdev_type_groups[] = { |
| 71 | &mdev_type_group, |
| 72 | NULL, |
| 73 | }; |
| 74 | |
| 75 | static int vfio_ccw_mdev_create(struct kobject *kobj, struct mdev_device *mdev) |
| 76 | { |
| 77 | struct vfio_ccw_private *private = |
| 78 | dev_get_drvdata(mdev_parent_dev(mdev)); |
| 79 | |
| 80 | if (atomic_dec_if_positive(&private->avail) < 0) |
| 81 | return -EPERM; |
| 82 | |
| 83 | private->mdev = mdev; |
| 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | static int vfio_ccw_mdev_remove(struct mdev_device *mdev) |
| 89 | { |
| 90 | struct vfio_ccw_private *private; |
| 91 | struct subchannel *sch; |
| 92 | int ret; |
| 93 | |
| 94 | private = dev_get_drvdata(mdev_parent_dev(mdev)); |
| 95 | sch = private->sch; |
| 96 | ret = vfio_ccw_sch_quiesce(sch); |
| 97 | if (ret) |
| 98 | return ret; |
| 99 | ret = cio_enable_subchannel(sch, (u32)(unsigned long)sch); |
| 100 | if (ret) |
| 101 | return ret; |
| 102 | |
| 103 | private->mdev = NULL; |
| 104 | atomic_inc(&private->avail); |
| 105 | |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | static int vfio_ccw_mdev_open(struct mdev_device *mdev) |
| 110 | { |
| 111 | struct vfio_ccw_private *private = |
| 112 | dev_get_drvdata(mdev_parent_dev(mdev)); |
| 113 | unsigned long events = VFIO_IOMMU_NOTIFY_DMA_UNMAP; |
| 114 | |
| 115 | private->nb.notifier_call = vfio_ccw_mdev_notifier; |
| 116 | |
| 117 | return vfio_register_notifier(mdev_dev(mdev), VFIO_IOMMU_NOTIFY, |
| 118 | &events, &private->nb); |
| 119 | } |
| 120 | |
| 121 | void vfio_ccw_mdev_release(struct mdev_device *mdev) |
| 122 | { |
| 123 | struct vfio_ccw_private *private = |
| 124 | dev_get_drvdata(mdev_parent_dev(mdev)); |
| 125 | |
| 126 | vfio_unregister_notifier(mdev_dev(mdev), VFIO_IOMMU_NOTIFY, |
| 127 | &private->nb); |
| 128 | } |
| 129 | |
Dong Jia Shi | 060d2b5 | 2017-03-17 04:17:34 +0100 | [diff] [blame^] | 130 | static ssize_t vfio_ccw_mdev_read(struct mdev_device *mdev, |
| 131 | char __user *buf, |
| 132 | size_t count, |
| 133 | loff_t *ppos) |
| 134 | { |
| 135 | struct vfio_ccw_private *private; |
| 136 | struct ccw_io_region *region; |
| 137 | |
| 138 | if (*ppos + count > sizeof(*region)) |
| 139 | return -EINVAL; |
| 140 | |
| 141 | private = dev_get_drvdata(mdev_parent_dev(mdev)); |
| 142 | if (!private) |
| 143 | return -ENODEV; |
| 144 | |
| 145 | region = &private->io_region; |
| 146 | if (copy_to_user(buf, (void *)region + *ppos, count)) |
| 147 | return -EFAULT; |
| 148 | |
| 149 | return count; |
| 150 | } |
| 151 | |
| 152 | static ssize_t vfio_ccw_mdev_write(struct mdev_device *mdev, |
| 153 | const char __user *buf, |
| 154 | size_t count, |
| 155 | loff_t *ppos) |
| 156 | { |
| 157 | struct vfio_ccw_private *private; |
| 158 | struct ccw_io_region *region; |
| 159 | |
| 160 | if (*ppos + count > sizeof(*region)) |
| 161 | return -EINVAL; |
| 162 | |
| 163 | private = dev_get_drvdata(mdev_parent_dev(mdev)); |
| 164 | if (!private) |
| 165 | return -ENODEV; |
| 166 | |
| 167 | region = &private->io_region; |
| 168 | if (copy_from_user((void *)region + *ppos, buf, count)) |
| 169 | return -EFAULT; |
| 170 | region->ret_code = 0; |
| 171 | |
| 172 | return count; |
| 173 | } |
| 174 | |
Dong Jia Shi | 84cd8fc | 2017-03-17 04:17:33 +0100 | [diff] [blame] | 175 | static const struct mdev_parent_ops vfio_ccw_mdev_ops = { |
| 176 | .owner = THIS_MODULE, |
| 177 | .supported_type_groups = mdev_type_groups, |
| 178 | .create = vfio_ccw_mdev_create, |
| 179 | .remove = vfio_ccw_mdev_remove, |
| 180 | .open = vfio_ccw_mdev_open, |
| 181 | .release = vfio_ccw_mdev_release, |
Dong Jia Shi | 060d2b5 | 2017-03-17 04:17:34 +0100 | [diff] [blame^] | 182 | .read = vfio_ccw_mdev_read, |
| 183 | .write = vfio_ccw_mdev_write, |
Dong Jia Shi | 84cd8fc | 2017-03-17 04:17:33 +0100 | [diff] [blame] | 184 | }; |
| 185 | |
| 186 | int vfio_ccw_mdev_reg(struct subchannel *sch) |
| 187 | { |
| 188 | return mdev_register_device(&sch->dev, &vfio_ccw_mdev_ops); |
| 189 | } |
| 190 | |
| 191 | void vfio_ccw_mdev_unreg(struct subchannel *sch) |
| 192 | { |
| 193 | mdev_unregister_device(&sch->dev); |
| 194 | } |