blob: 7eccc91cd59d7b3b7bee3358c2dc97131af0c30c [file] [log] [blame]
David S. Millere53e97c2007-07-09 22:22:44 -07001/* vio.c: Virtual I/O channel devices probing infrastructure.
2 *
3 * Copyright (c) 2003-2005 IBM Corp.
4 * Dave Engebretsen engebret@us.ibm.com
5 * Santiago Leon santil@us.ibm.com
6 * Hollis Blanchard <hollisb@us.ibm.com>
7 * Stephen Rothwell
8 *
9 * Adapted to sparc64 by David S. Miller davem@davemloft.net
10 */
11
12#include <linux/kernel.h>
13#include <linux/irq.h>
14#include <linux/init.h>
15
16#include <asm/mdesc.h>
17#include <asm/vio.h>
18
19static inline int find_in_proplist(const char *list, const char *match,
20 int len)
21{
22 while (len > 0) {
23 int l;
24
25 if (!strcmp(list, match))
26 return 1;
27 l = strlen(list) + 1;
28 list += l;
29 len -= l;
30 }
31 return 0;
32}
33
34static const struct vio_device_id *vio_match_device(
35 const struct vio_device_id *matches,
36 const struct vio_dev *dev)
37{
38 const char *type, *compat;
39 int len;
40
41 type = dev->type;
42 compat = dev->compat;
43 len = dev->compat_len;
44
45 while (matches->type[0] || matches->compat[0]) {
46 int match = 1;
47 if (matches->type[0]) {
48 match &= type
49 && !strcmp(matches->type, type);
50 }
51 if (matches->compat[0]) {
52 match &= compat &&
53 find_in_proplist(compat, matches->compat, len);
54 }
55 if (match)
56 return matches;
57 matches++;
58 }
59 return NULL;
60}
61
62static int vio_bus_match(struct device *dev, struct device_driver *drv)
63{
64 struct vio_dev *vio_dev = to_vio_dev(dev);
65 struct vio_driver *vio_drv = to_vio_driver(drv);
66 const struct vio_device_id *matches = vio_drv->id_table;
67
68 if (!matches)
69 return 0;
70
71 return vio_match_device(matches, vio_dev) != NULL;
72}
73
74static int vio_device_probe(struct device *dev)
75{
76 struct vio_dev *vdev = to_vio_dev(dev);
77 struct vio_driver *drv = to_vio_driver(dev->driver);
78 const struct vio_device_id *id;
79 int error = -ENODEV;
80
81 if (drv->probe) {
82 id = vio_match_device(drv->id_table, vdev);
83 if (id)
84 error = drv->probe(vdev, id);
85 }
86
87 return error;
88}
89
90static int vio_device_remove(struct device *dev)
91{
92 struct vio_dev *vdev = to_vio_dev(dev);
93 struct vio_driver *drv = to_vio_driver(dev->driver);
94
95 if (drv->remove)
96 return drv->remove(vdev);
97
98 return 1;
99}
100
101static ssize_t devspec_show(struct device *dev,
102 struct device_attribute *attr, char *buf)
103{
104 struct vio_dev *vdev = to_vio_dev(dev);
105 const char *str = "none";
106
David S. Miller2c4f4ec2007-07-11 18:15:59 -0700107 if (!strcmp(vdev->type, "network"))
108 str = "vnet";
109 else if (!strcmp(vdev->type, "block"))
110 str = "vdisk";
David S. Millere53e97c2007-07-09 22:22:44 -0700111
112 return sprintf(buf, "%s\n", str);
113}
114
David S. Miller2c4f4ec2007-07-11 18:15:59 -0700115static ssize_t type_show(struct device *dev,
116 struct device_attribute *attr, char *buf)
117{
118 struct vio_dev *vdev = to_vio_dev(dev);
119 return sprintf(buf, "%s\n", vdev->type);
120}
121
David S. Millere53e97c2007-07-09 22:22:44 -0700122static struct device_attribute vio_dev_attrs[] = {
123 __ATTR_RO(devspec),
David S. Miller2c4f4ec2007-07-11 18:15:59 -0700124 __ATTR_RO(type),
David S. Millere53e97c2007-07-09 22:22:44 -0700125 __ATTR_NULL
126};
127
128static struct bus_type vio_bus_type = {
129 .name = "vio",
130 .dev_attrs = vio_dev_attrs,
131 .match = vio_bus_match,
132 .probe = vio_device_probe,
133 .remove = vio_device_remove,
134};
135
136int vio_register_driver(struct vio_driver *viodrv)
137{
138 viodrv->driver.bus = &vio_bus_type;
139
140 return driver_register(&viodrv->driver);
141}
142EXPORT_SYMBOL(vio_register_driver);
143
144void vio_unregister_driver(struct vio_driver *viodrv)
145{
146 driver_unregister(&viodrv->driver);
147}
148EXPORT_SYMBOL(vio_unregister_driver);
149
150struct mdesc_node *vio_find_endpoint(struct vio_dev *vdev)
151{
152 struct mdesc_node *endp, *mp = vdev->mp;
153 int i;
154
155 endp = NULL;
156 for (i = 0; i < mp->num_arcs; i++) {
157 struct mdesc_node *t;
158
159 if (strcmp(mp->arcs[i].name, "fwd"))
160 continue;
161
162 t = mp->arcs[i].arc;
163 if (strcmp(t->name, "channel-endpoint"))
164 continue;
165
166 endp = t;
167 break;
168 }
169
170 return endp;
171}
172EXPORT_SYMBOL(vio_find_endpoint);
173
174static void __devinit vio_dev_release(struct device *dev)
175{
176 kfree(to_vio_dev(dev));
177}
178
179static ssize_t
180show_pciobppath_attr(struct device *dev, struct device_attribute *attr,
181 char *buf)
182{
183 struct vio_dev *vdev;
184 struct device_node *dp;
185
186 vdev = to_vio_dev(dev);
187 dp = vdev->dp;
188
189 return snprintf (buf, PAGE_SIZE, "%s\n", dp->full_name);
190}
191
192static DEVICE_ATTR(obppath, S_IRUSR | S_IRGRP | S_IROTH,
193 show_pciobppath_attr, NULL);
194
195struct device_node *cdev_node;
196
197static struct vio_dev *root_vdev;
198static u64 cdev_cfg_handle;
199
200static struct vio_dev *vio_create_one(struct mdesc_node *mp,
201 struct device *parent)
202{
203 const char *type, *compat;
204 struct device_node *dp;
205 struct vio_dev *vdev;
206 const u64 *irq;
207 int err, clen;
208
209 type = md_get_property(mp, "device-type", NULL);
David S. Miller2c4f4ec2007-07-11 18:15:59 -0700210 if (!type) {
David S. Millere53e97c2007-07-09 22:22:44 -0700211 type = md_get_property(mp, "name", NULL);
David S. Miller2c4f4ec2007-07-11 18:15:59 -0700212 if (!type)
213 type = mp->name;
214 }
David S. Millere53e97c2007-07-09 22:22:44 -0700215 compat = md_get_property(mp, "device-type", &clen);
216
217 vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
218 if (!vdev) {
219 printk(KERN_ERR "VIO: Could not allocate vio_dev\n");
220 return NULL;
221 }
222
223 vdev->mp = mp;
224 vdev->type = type;
225 vdev->compat = compat;
226 vdev->compat_len = clen;
227
228 irq = md_get_property(mp, "tx-ino", NULL);
229 if (irq)
230 mp->irqs[0] = sun4v_build_virq(cdev_cfg_handle, *irq);
231
232 irq = md_get_property(mp, "rx-ino", NULL);
233 if (irq)
234 mp->irqs[1] = sun4v_build_virq(cdev_cfg_handle, *irq);
235
236 snprintf(vdev->dev.bus_id, BUS_ID_SIZE, "%lx", mp->node);
237 vdev->dev.parent = parent;
238 vdev->dev.bus = &vio_bus_type;
239 vdev->dev.release = vio_dev_release;
240
241 if (parent == NULL) {
242 dp = cdev_node;
243 } else if (to_vio_dev(parent) == root_vdev) {
244 dp = of_get_next_child(cdev_node, NULL);
245 while (dp) {
246 if (!strcmp(dp->type, type))
247 break;
248
249 dp = of_get_next_child(cdev_node, dp);
250 }
251 } else {
252 dp = to_vio_dev(parent)->dp;
253 }
254 vdev->dp = dp;
255
256 err = device_register(&vdev->dev);
257 if (err) {
258 printk(KERN_ERR "VIO: Could not register device %s, err=%d\n",
259 vdev->dev.bus_id, err);
260 kfree(vdev);
261 return NULL;
262 }
263 if (vdev->dp)
264 err = sysfs_create_file(&vdev->dev.kobj,
265 &dev_attr_obppath.attr);
266
267 return vdev;
268}
269
270static void walk_tree(struct mdesc_node *n, struct vio_dev *parent)
271{
272 int i;
273
274 for (i = 0; i < n->num_arcs; i++) {
275 struct mdesc_node *mp;
276 struct vio_dev *vdev;
277
278 if (strcmp(n->arcs[i].name, "fwd"))
279 continue;
280
281 mp = n->arcs[i].arc;
282
283 vdev = vio_create_one(mp, &parent->dev);
284 if (vdev && mp->num_arcs)
285 walk_tree(mp, vdev);
286 }
287}
288
289static void create_devices(struct mdesc_node *root)
290{
David S. Miller2c4f4ec2007-07-11 18:15:59 -0700291 struct mdesc_node *mp;
292
David S. Millere53e97c2007-07-09 22:22:44 -0700293 root_vdev = vio_create_one(root, NULL);
294 if (!root_vdev) {
295 printk(KERN_ERR "VIO: Coult not create root device.\n");
296 return;
297 }
298
299 walk_tree(root, root_vdev);
David S. Miller2c4f4ec2007-07-11 18:15:59 -0700300
301 /* Domain services is odd as it doesn't sit underneath the
302 * channel-devices node, so we plug it in manually.
303 */
304 mp = md_find_node_by_name(NULL, "domain-services");
305 if (mp) {
306 struct vio_dev *parent = vio_create_one(mp, &root_vdev->dev);
307
308 if (parent)
309 walk_tree(mp, parent);
310 }
David S. Millere53e97c2007-07-09 22:22:44 -0700311}
312
313const char *channel_devices_node = "channel-devices";
314const char *channel_devices_compat = "SUNW,sun4v-channel-devices";
315const char *cfg_handle_prop = "cfg-handle";
316
317static int __init vio_init(void)
318{
319 struct mdesc_node *root;
320 const char *compat;
321 const u64 *cfg_handle;
322 int err, len;
323
324 root = md_find_node_by_name(NULL, channel_devices_node);
325 if (!root) {
326 printk(KERN_INFO "VIO: No channel-devices MDESC node.\n");
327 return 0;
328 }
329
330 cdev_node = of_find_node_by_name(NULL, "channel-devices");
331 if (!cdev_node) {
332 printk(KERN_INFO "VIO: No channel-devices OBP node.\n");
333 return -ENODEV;
334 }
335
336 compat = md_get_property(root, "compatible", &len);
337 if (!compat) {
338 printk(KERN_ERR "VIO: Channel devices lacks compatible "
339 "property\n");
340 return -ENODEV;
341 }
342 if (!find_in_proplist(compat, channel_devices_compat, len)) {
343 printk(KERN_ERR "VIO: Channel devices node lacks (%s) "
344 "compat entry.\n", channel_devices_compat);
345 return -ENODEV;
346 }
347
348 cfg_handle = md_get_property(root, cfg_handle_prop, NULL);
349 if (!cfg_handle) {
350 printk(KERN_ERR "VIO: Channel devices lacks %s property\n",
351 cfg_handle_prop);
352 return -ENODEV;
353 }
354
355 cdev_cfg_handle = *cfg_handle;
356
357 err = bus_register(&vio_bus_type);
358 if (err) {
359 printk(KERN_ERR "VIO: Could not register bus type err=%d\n",
360 err);
361 return err;
362 }
363
364 create_devices(root);
365
366 return 0;
367}
368
369postcore_initcall(vio_init);