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 | |
Dong Jia Shi | 83d1193 | 2017-03-17 04:17:37 +0100 | [diff] [blame^] | 15 | static int vfio_ccw_mdev_reset(struct mdev_device *mdev) |
| 16 | { |
| 17 | struct vfio_ccw_private *private; |
| 18 | struct subchannel *sch; |
| 19 | int ret; |
| 20 | |
| 21 | private = dev_get_drvdata(mdev_parent_dev(mdev)); |
| 22 | if (!private) |
| 23 | return -ENODEV; |
| 24 | |
| 25 | sch = private->sch; |
| 26 | /* |
| 27 | * TODO: |
| 28 | * In the cureent stage, some things like "no I/O running" and "no |
| 29 | * interrupt pending" are clear, but we are not sure what other state |
| 30 | * we need to care about. |
| 31 | * There are still a lot more instructions need to be handled. We |
| 32 | * should come back here later. |
| 33 | */ |
| 34 | ret = vfio_ccw_sch_quiesce(sch); |
| 35 | if (ret) |
| 36 | return ret; |
| 37 | |
| 38 | return cio_enable_subchannel(sch, (u32)(unsigned long)sch); |
| 39 | } |
| 40 | |
Dong Jia Shi | 84cd8fc | 2017-03-17 04:17:33 +0100 | [diff] [blame] | 41 | static int vfio_ccw_mdev_notifier(struct notifier_block *nb, |
| 42 | unsigned long action, |
| 43 | void *data) |
| 44 | { |
| 45 | struct vfio_ccw_private *private = |
| 46 | container_of(nb, struct vfio_ccw_private, nb); |
| 47 | |
| 48 | if (!private) |
| 49 | return NOTIFY_STOP; |
| 50 | |
| 51 | /* |
Dong Jia Shi | 84cd8fc | 2017-03-17 04:17:33 +0100 | [diff] [blame] | 52 | * Vendor drivers MUST unpin pages in response to an |
| 53 | * invalidation. |
| 54 | */ |
Dong Jia Shi | 4e149e4 | 2017-03-17 04:17:35 +0100 | [diff] [blame] | 55 | if (action == VFIO_IOMMU_NOTIFY_DMA_UNMAP) { |
| 56 | struct vfio_iommu_type1_dma_unmap *unmap = data; |
Dong Jia Shi | 4e149e4 | 2017-03-17 04:17:35 +0100 | [diff] [blame] | 57 | |
| 58 | if (!cp_iova_pinned(&private->cp, unmap->iova)) |
| 59 | return NOTIFY_OK; |
| 60 | |
Dong Jia Shi | 83d1193 | 2017-03-17 04:17:37 +0100 | [diff] [blame^] | 61 | if (vfio_ccw_mdev_reset(private->mdev)) |
Dong Jia Shi | 4e149e4 | 2017-03-17 04:17:35 +0100 | [diff] [blame] | 62 | return NOTIFY_BAD; |
| 63 | |
| 64 | cp_free(&private->cp); |
| 65 | return NOTIFY_OK; |
| 66 | } |
Dong Jia Shi | 84cd8fc | 2017-03-17 04:17:33 +0100 | [diff] [blame] | 67 | |
| 68 | return NOTIFY_DONE; |
| 69 | } |
| 70 | |
| 71 | static ssize_t name_show(struct kobject *kobj, struct device *dev, char *buf) |
| 72 | { |
| 73 | return sprintf(buf, "I/O subchannel (Non-QDIO)\n"); |
| 74 | } |
| 75 | MDEV_TYPE_ATTR_RO(name); |
| 76 | |
| 77 | static ssize_t device_api_show(struct kobject *kobj, struct device *dev, |
| 78 | char *buf) |
| 79 | { |
| 80 | return sprintf(buf, "%s\n", VFIO_DEVICE_API_CCW_STRING); |
| 81 | } |
| 82 | MDEV_TYPE_ATTR_RO(device_api); |
| 83 | |
| 84 | static ssize_t available_instances_show(struct kobject *kobj, |
| 85 | struct device *dev, char *buf) |
| 86 | { |
| 87 | struct vfio_ccw_private *private = dev_get_drvdata(dev); |
| 88 | |
| 89 | return sprintf(buf, "%d\n", atomic_read(&private->avail)); |
| 90 | } |
| 91 | MDEV_TYPE_ATTR_RO(available_instances); |
| 92 | |
| 93 | static struct attribute *mdev_types_attrs[] = { |
| 94 | &mdev_type_attr_name.attr, |
| 95 | &mdev_type_attr_device_api.attr, |
| 96 | &mdev_type_attr_available_instances.attr, |
| 97 | NULL, |
| 98 | }; |
| 99 | |
| 100 | static struct attribute_group mdev_type_group = { |
| 101 | .name = "io", |
| 102 | .attrs = mdev_types_attrs, |
| 103 | }; |
| 104 | |
| 105 | struct attribute_group *mdev_type_groups[] = { |
| 106 | &mdev_type_group, |
| 107 | NULL, |
| 108 | }; |
| 109 | |
| 110 | static int vfio_ccw_mdev_create(struct kobject *kobj, struct mdev_device *mdev) |
| 111 | { |
| 112 | struct vfio_ccw_private *private = |
| 113 | dev_get_drvdata(mdev_parent_dev(mdev)); |
| 114 | |
| 115 | if (atomic_dec_if_positive(&private->avail) < 0) |
| 116 | return -EPERM; |
| 117 | |
| 118 | private->mdev = mdev; |
| 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | static int vfio_ccw_mdev_remove(struct mdev_device *mdev) |
| 124 | { |
Dong Jia Shi | 83d1193 | 2017-03-17 04:17:37 +0100 | [diff] [blame^] | 125 | struct vfio_ccw_private *private = |
| 126 | dev_get_drvdata(mdev_parent_dev(mdev)); |
Dong Jia Shi | 84cd8fc | 2017-03-17 04:17:33 +0100 | [diff] [blame] | 127 | int ret; |
| 128 | |
Dong Jia Shi | 83d1193 | 2017-03-17 04:17:37 +0100 | [diff] [blame^] | 129 | ret = vfio_ccw_mdev_reset(mdev); |
Dong Jia Shi | 84cd8fc | 2017-03-17 04:17:33 +0100 | [diff] [blame] | 130 | if (ret) |
| 131 | return ret; |
| 132 | |
| 133 | private->mdev = NULL; |
| 134 | atomic_inc(&private->avail); |
| 135 | |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | static int vfio_ccw_mdev_open(struct mdev_device *mdev) |
| 140 | { |
| 141 | struct vfio_ccw_private *private = |
| 142 | dev_get_drvdata(mdev_parent_dev(mdev)); |
| 143 | unsigned long events = VFIO_IOMMU_NOTIFY_DMA_UNMAP; |
| 144 | |
| 145 | private->nb.notifier_call = vfio_ccw_mdev_notifier; |
| 146 | |
| 147 | return vfio_register_notifier(mdev_dev(mdev), VFIO_IOMMU_NOTIFY, |
| 148 | &events, &private->nb); |
| 149 | } |
| 150 | |
| 151 | void vfio_ccw_mdev_release(struct mdev_device *mdev) |
| 152 | { |
| 153 | struct vfio_ccw_private *private = |
| 154 | dev_get_drvdata(mdev_parent_dev(mdev)); |
| 155 | |
| 156 | vfio_unregister_notifier(mdev_dev(mdev), VFIO_IOMMU_NOTIFY, |
| 157 | &private->nb); |
| 158 | } |
| 159 | |
Dong Jia Shi | 060d2b5 | 2017-03-17 04:17:34 +0100 | [diff] [blame] | 160 | static ssize_t vfio_ccw_mdev_read(struct mdev_device *mdev, |
| 161 | char __user *buf, |
| 162 | size_t count, |
| 163 | loff_t *ppos) |
| 164 | { |
| 165 | struct vfio_ccw_private *private; |
| 166 | struct ccw_io_region *region; |
| 167 | |
| 168 | if (*ppos + count > sizeof(*region)) |
| 169 | return -EINVAL; |
| 170 | |
| 171 | private = dev_get_drvdata(mdev_parent_dev(mdev)); |
| 172 | if (!private) |
| 173 | return -ENODEV; |
| 174 | |
| 175 | region = &private->io_region; |
| 176 | if (copy_to_user(buf, (void *)region + *ppos, count)) |
| 177 | return -EFAULT; |
| 178 | |
| 179 | return count; |
| 180 | } |
| 181 | |
| 182 | static ssize_t vfio_ccw_mdev_write(struct mdev_device *mdev, |
| 183 | const char __user *buf, |
| 184 | size_t count, |
| 185 | loff_t *ppos) |
| 186 | { |
| 187 | struct vfio_ccw_private *private; |
| 188 | struct ccw_io_region *region; |
| 189 | |
| 190 | if (*ppos + count > sizeof(*region)) |
| 191 | return -EINVAL; |
| 192 | |
| 193 | private = dev_get_drvdata(mdev_parent_dev(mdev)); |
| 194 | if (!private) |
| 195 | return -ENODEV; |
| 196 | |
| 197 | region = &private->io_region; |
| 198 | if (copy_from_user((void *)region + *ppos, buf, count)) |
| 199 | return -EFAULT; |
Dong Jia Shi | 4e149e4 | 2017-03-17 04:17:35 +0100 | [diff] [blame] | 200 | |
| 201 | region->ret_code = vfio_ccw_sch_cmd_request(private); |
| 202 | if (region->ret_code != 0) |
| 203 | return region->ret_code; |
Dong Jia Shi | 060d2b5 | 2017-03-17 04:17:34 +0100 | [diff] [blame] | 204 | |
| 205 | return count; |
| 206 | } |
| 207 | |
Dong Jia Shi | e01bcdd | 2017-03-17 04:17:36 +0100 | [diff] [blame] | 208 | static int vfio_ccw_mdev_get_device_info(struct vfio_device_info *info) |
| 209 | { |
Dong Jia Shi | 83d1193 | 2017-03-17 04:17:37 +0100 | [diff] [blame^] | 210 | info->flags = VFIO_DEVICE_FLAGS_CCW | VFIO_DEVICE_FLAGS_RESET; |
Dong Jia Shi | e01bcdd | 2017-03-17 04:17:36 +0100 | [diff] [blame] | 211 | info->num_regions = VFIO_CCW_NUM_REGIONS; |
| 212 | info->num_irqs = 0; |
| 213 | |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | static int vfio_ccw_mdev_get_region_info(struct vfio_region_info *info, |
| 218 | u16 *cap_type_id, |
| 219 | void **cap_type) |
| 220 | { |
| 221 | switch (info->index) { |
| 222 | case VFIO_CCW_CONFIG_REGION_INDEX: |
| 223 | info->offset = 0; |
| 224 | info->size = sizeof(struct ccw_io_region); |
| 225 | info->flags = VFIO_REGION_INFO_FLAG_READ |
| 226 | | VFIO_REGION_INFO_FLAG_WRITE; |
| 227 | return 0; |
| 228 | default: |
| 229 | return -EINVAL; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | static ssize_t vfio_ccw_mdev_ioctl(struct mdev_device *mdev, |
| 234 | unsigned int cmd, |
| 235 | unsigned long arg) |
| 236 | { |
| 237 | int ret = 0; |
| 238 | unsigned long minsz; |
| 239 | |
| 240 | switch (cmd) { |
| 241 | case VFIO_DEVICE_GET_INFO: |
| 242 | { |
| 243 | struct vfio_device_info info; |
| 244 | |
| 245 | minsz = offsetofend(struct vfio_device_info, num_irqs); |
| 246 | |
| 247 | if (copy_from_user(&info, (void __user *)arg, minsz)) |
| 248 | return -EFAULT; |
| 249 | |
| 250 | if (info.argsz < minsz) |
| 251 | return -EINVAL; |
| 252 | |
| 253 | ret = vfio_ccw_mdev_get_device_info(&info); |
| 254 | if (ret) |
| 255 | return ret; |
| 256 | |
| 257 | return copy_to_user((void __user *)arg, &info, minsz); |
| 258 | } |
| 259 | case VFIO_DEVICE_GET_REGION_INFO: |
| 260 | { |
| 261 | struct vfio_region_info info; |
| 262 | u16 cap_type_id = 0; |
| 263 | void *cap_type = NULL; |
| 264 | |
| 265 | minsz = offsetofend(struct vfio_region_info, offset); |
| 266 | |
| 267 | if (copy_from_user(&info, (void __user *)arg, minsz)) |
| 268 | return -EFAULT; |
| 269 | |
| 270 | if (info.argsz < minsz) |
| 271 | return -EINVAL; |
| 272 | |
| 273 | ret = vfio_ccw_mdev_get_region_info(&info, &cap_type_id, |
| 274 | &cap_type); |
| 275 | if (ret) |
| 276 | return ret; |
| 277 | |
| 278 | return copy_to_user((void __user *)arg, &info, minsz); |
| 279 | } |
Dong Jia Shi | 83d1193 | 2017-03-17 04:17:37 +0100 | [diff] [blame^] | 280 | case VFIO_DEVICE_RESET: |
| 281 | return vfio_ccw_mdev_reset(mdev); |
Dong Jia Shi | e01bcdd | 2017-03-17 04:17:36 +0100 | [diff] [blame] | 282 | default: |
| 283 | return -ENOTTY; |
| 284 | } |
| 285 | } |
| 286 | |
Dong Jia Shi | 84cd8fc | 2017-03-17 04:17:33 +0100 | [diff] [blame] | 287 | static const struct mdev_parent_ops vfio_ccw_mdev_ops = { |
| 288 | .owner = THIS_MODULE, |
| 289 | .supported_type_groups = mdev_type_groups, |
| 290 | .create = vfio_ccw_mdev_create, |
| 291 | .remove = vfio_ccw_mdev_remove, |
| 292 | .open = vfio_ccw_mdev_open, |
| 293 | .release = vfio_ccw_mdev_release, |
Dong Jia Shi | 060d2b5 | 2017-03-17 04:17:34 +0100 | [diff] [blame] | 294 | .read = vfio_ccw_mdev_read, |
| 295 | .write = vfio_ccw_mdev_write, |
Dong Jia Shi | e01bcdd | 2017-03-17 04:17:36 +0100 | [diff] [blame] | 296 | .ioctl = vfio_ccw_mdev_ioctl, |
Dong Jia Shi | 84cd8fc | 2017-03-17 04:17:33 +0100 | [diff] [blame] | 297 | }; |
| 298 | |
| 299 | int vfio_ccw_mdev_reg(struct subchannel *sch) |
| 300 | { |
| 301 | return mdev_register_device(&sch->dev, &vfio_ccw_mdev_ops); |
| 302 | } |
| 303 | |
| 304 | void vfio_ccw_mdev_unreg(struct subchannel *sch) |
| 305 | { |
| 306 | mdev_unregister_device(&sch->dev); |
| 307 | } |