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