blob: 878c88239fc802dd6a56b2d5e4387d1b247fe3ef [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 Shi84cd8fc2017-03-17 04:17:33 +0100191static 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 Shi060d2b52017-03-17 04:17:34 +0100198 .read = vfio_ccw_mdev_read,
199 .write = vfio_ccw_mdev_write,
Dong Jia Shi84cd8fc2017-03-17 04:17:33 +0100200};
201
202int vfio_ccw_mdev_reg(struct subchannel *sch)
203{
204 return mdev_register_device(&sch->dev, &vfio_ccw_mdev_ops);
205}
206
207void vfio_ccw_mdev_unreg(struct subchannel *sch)
208{
209 mdev_unregister_device(&sch->dev);
210}