blob: f3300ddded3fa7d9c1031c24b29bb41d0d233bc9 [file] [log] [blame]
Dong Jia Shi84cd8fc2017-03-17 04:17:33 +01001/*
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
15static 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 Shi84cd8fc2017-03-17 04:17:33 +010026 * Vendor drivers MUST unpin pages in response to an
27 * invalidation.
28 */
Dong Jia Shi4e149e42017-03-17 04:17:35 +010029 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 Shi84cd8fc2017-03-17 04:17:33 +010045
46 return NOTIFY_DONE;
47}
48
49static ssize_t name_show(struct kobject *kobj, struct device *dev, char *buf)
50{
51 return sprintf(buf, "I/O subchannel (Non-QDIO)\n");
52}
53MDEV_TYPE_ATTR_RO(name);
54
55static 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}
60MDEV_TYPE_ATTR_RO(device_api);
61
62static 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}
69MDEV_TYPE_ATTR_RO(available_instances);
70
71static 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
78static struct attribute_group mdev_type_group = {
79 .name = "io",
80 .attrs = mdev_types_attrs,
81};
82
83struct attribute_group *mdev_type_groups[] = {
84 &mdev_type_group,
85 NULL,
86};
87
88static 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
101static 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
122static 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
134void 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 Shi060d2b52017-03-17 04:17:34 +0100143static 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
165static 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 Shi4e149e42017-03-17 04:17:35 +0100183
184 region->ret_code = vfio_ccw_sch_cmd_request(private);
185 if (region->ret_code != 0)
186 return region->ret_code;
Dong Jia Shi060d2b52017-03-17 04:17:34 +0100187
188 return count;
189}
190
Dong Jia Shie01bcdd2017-03-17 04:17:36 +0100191static int vfio_ccw_mdev_get_device_info(struct vfio_device_info *info)
192{
193 info->flags = VFIO_DEVICE_FLAGS_CCW;
194 info->num_regions = VFIO_CCW_NUM_REGIONS;
195 info->num_irqs = 0;
196
197 return 0;
198}
199
200static int vfio_ccw_mdev_get_region_info(struct vfio_region_info *info,
201 u16 *cap_type_id,
202 void **cap_type)
203{
204 switch (info->index) {
205 case VFIO_CCW_CONFIG_REGION_INDEX:
206 info->offset = 0;
207 info->size = sizeof(struct ccw_io_region);
208 info->flags = VFIO_REGION_INFO_FLAG_READ
209 | VFIO_REGION_INFO_FLAG_WRITE;
210 return 0;
211 default:
212 return -EINVAL;
213 }
214}
215
216static ssize_t vfio_ccw_mdev_ioctl(struct mdev_device *mdev,
217 unsigned int cmd,
218 unsigned long arg)
219{
220 int ret = 0;
221 unsigned long minsz;
222
223 switch (cmd) {
224 case VFIO_DEVICE_GET_INFO:
225 {
226 struct vfio_device_info info;
227
228 minsz = offsetofend(struct vfio_device_info, num_irqs);
229
230 if (copy_from_user(&info, (void __user *)arg, minsz))
231 return -EFAULT;
232
233 if (info.argsz < minsz)
234 return -EINVAL;
235
236 ret = vfio_ccw_mdev_get_device_info(&info);
237 if (ret)
238 return ret;
239
240 return copy_to_user((void __user *)arg, &info, minsz);
241 }
242 case VFIO_DEVICE_GET_REGION_INFO:
243 {
244 struct vfio_region_info info;
245 u16 cap_type_id = 0;
246 void *cap_type = NULL;
247
248 minsz = offsetofend(struct vfio_region_info, offset);
249
250 if (copy_from_user(&info, (void __user *)arg, minsz))
251 return -EFAULT;
252
253 if (info.argsz < minsz)
254 return -EINVAL;
255
256 ret = vfio_ccw_mdev_get_region_info(&info, &cap_type_id,
257 &cap_type);
258 if (ret)
259 return ret;
260
261 return copy_to_user((void __user *)arg, &info, minsz);
262 }
263 default:
264 return -ENOTTY;
265 }
266}
267
Dong Jia Shi84cd8fc2017-03-17 04:17:33 +0100268static const struct mdev_parent_ops vfio_ccw_mdev_ops = {
269 .owner = THIS_MODULE,
270 .supported_type_groups = mdev_type_groups,
271 .create = vfio_ccw_mdev_create,
272 .remove = vfio_ccw_mdev_remove,
273 .open = vfio_ccw_mdev_open,
274 .release = vfio_ccw_mdev_release,
Dong Jia Shi060d2b52017-03-17 04:17:34 +0100275 .read = vfio_ccw_mdev_read,
276 .write = vfio_ccw_mdev_write,
Dong Jia Shie01bcdd2017-03-17 04:17:36 +0100277 .ioctl = vfio_ccw_mdev_ioctl,
Dong Jia Shi84cd8fc2017-03-17 04:17:33 +0100278};
279
280int vfio_ccw_mdev_reg(struct subchannel *sch)
281{
282 return mdev_register_device(&sch->dev, &vfio_ccw_mdev_ops);
283}
284
285void vfio_ccw_mdev_unreg(struct subchannel *sch)
286{
287 mdev_unregister_device(&sch->dev);
288}