1 /* vio.c: Virtual I/O channel devices probing infrastructure.
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>
9 * Adapted to sparc64 by David S. Miller davem@davemloft.net
12 #include <linux/kernel.h>
13 #include <linux/irq.h>
14 #include <linux/init.h>
16 #include <asm/mdesc.h>
19 static inline int find_in_proplist(const char *list, const char *match,
25 if (!strcmp(list, match))
34 static const struct vio_device_id *vio_match_device(
35 const struct vio_device_id *matches,
36 const struct vio_dev *dev)
38 const char *type, *compat;
43 len = dev->compat_len;
45 while (matches->type[0] || matches->compat[0]) {
47 if (matches->type[0]) {
49 && !strcmp(matches->type, type);
51 if (matches->compat[0]) {
53 find_in_proplist(compat, matches->compat, len);
62 static int vio_bus_match(struct device *dev, struct device_driver *drv)
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;
71 return vio_match_device(matches, vio_dev) != NULL;
74 static int vio_device_probe(struct device *dev)
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;
82 id = vio_match_device(drv->id_table, vdev);
84 error = drv->probe(vdev, id);
90 static int vio_device_remove(struct device *dev)
92 struct vio_dev *vdev = to_vio_dev(dev);
93 struct vio_driver *drv = to_vio_driver(dev->driver);
96 return drv->remove(vdev);
101 static ssize_t devspec_show(struct device *dev,
102 struct device_attribute *attr, char *buf)
104 struct vio_dev *vdev = to_vio_dev(dev);
105 const char *str = "none";
107 if (!strcmp(vdev->type, "network"))
109 else if (!strcmp(vdev->type, "block"))
112 return sprintf(buf, "%s\n", str);
115 static ssize_t type_show(struct device *dev,
116 struct device_attribute *attr, char *buf)
118 struct vio_dev *vdev = to_vio_dev(dev);
119 return sprintf(buf, "%s\n", vdev->type);
122 static struct device_attribute vio_dev_attrs[] = {
128 static struct bus_type vio_bus_type = {
130 .dev_attrs = vio_dev_attrs,
131 .match = vio_bus_match,
132 .probe = vio_device_probe,
133 .remove = vio_device_remove,
136 int vio_register_driver(struct vio_driver *viodrv)
138 viodrv->driver.bus = &vio_bus_type;
140 return driver_register(&viodrv->driver);
142 EXPORT_SYMBOL(vio_register_driver);
144 void vio_unregister_driver(struct vio_driver *viodrv)
146 driver_unregister(&viodrv->driver);
148 EXPORT_SYMBOL(vio_unregister_driver);
150 struct mdesc_node *vio_find_endpoint(struct vio_dev *vdev)
152 struct mdesc_node *endp, *mp = vdev->mp;
156 for (i = 0; i < mp->num_arcs; i++) {
157 struct mdesc_node *t;
159 if (strcmp(mp->arcs[i].name, "fwd"))
163 if (strcmp(t->name, "channel-endpoint"))
172 EXPORT_SYMBOL(vio_find_endpoint);
174 static void __devinit vio_dev_release(struct device *dev)
176 kfree(to_vio_dev(dev));
180 show_pciobppath_attr(struct device *dev, struct device_attribute *attr,
183 struct vio_dev *vdev;
184 struct device_node *dp;
186 vdev = to_vio_dev(dev);
189 return snprintf (buf, PAGE_SIZE, "%s\n", dp->full_name);
192 static DEVICE_ATTR(obppath, S_IRUSR | S_IRGRP | S_IROTH,
193 show_pciobppath_attr, NULL);
195 struct device_node *cdev_node;
197 static struct vio_dev *root_vdev;
198 static u64 cdev_cfg_handle;
200 static struct vio_dev *vio_create_one(struct mdesc_node *mp,
201 struct device *parent)
203 const char *type, *compat;
204 struct device_node *dp;
205 struct vio_dev *vdev;
209 type = md_get_property(mp, "device-type", NULL);
211 type = md_get_property(mp, "name", NULL);
215 compat = md_get_property(mp, "device-type", &clen);
217 vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
219 printk(KERN_ERR "VIO: Could not allocate vio_dev\n");
225 vdev->compat = compat;
226 vdev->compat_len = clen;
228 irq = md_get_property(mp, "tx-ino", NULL);
230 mp->irqs[0] = sun4v_build_virq(cdev_cfg_handle, *irq);
232 irq = md_get_property(mp, "rx-ino", NULL);
234 mp->irqs[1] = sun4v_build_virq(cdev_cfg_handle, *irq);
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;
241 if (parent == NULL) {
243 } else if (to_vio_dev(parent) == root_vdev) {
244 dp = of_get_next_child(cdev_node, NULL);
246 if (!strcmp(dp->type, type))
249 dp = of_get_next_child(cdev_node, dp);
252 dp = to_vio_dev(parent)->dp;
256 err = device_register(&vdev->dev);
258 printk(KERN_ERR "VIO: Could not register device %s, err=%d\n",
259 vdev->dev.bus_id, err);
264 err = sysfs_create_file(&vdev->dev.kobj,
265 &dev_attr_obppath.attr);
270 static void walk_tree(struct mdesc_node *n, struct vio_dev *parent)
274 for (i = 0; i < n->num_arcs; i++) {
275 struct mdesc_node *mp;
276 struct vio_dev *vdev;
278 if (strcmp(n->arcs[i].name, "fwd"))
283 vdev = vio_create_one(mp, &parent->dev);
284 if (vdev && mp->num_arcs)
289 static void create_devices(struct mdesc_node *root)
291 struct mdesc_node *mp;
293 root_vdev = vio_create_one(root, NULL);
295 printk(KERN_ERR "VIO: Coult not create root device.\n");
299 walk_tree(root, root_vdev);
301 /* Domain services is odd as it doesn't sit underneath the
302 * channel-devices node, so we plug it in manually.
304 mp = md_find_node_by_name(NULL, "domain-services");
306 struct vio_dev *parent = vio_create_one(mp, &root_vdev->dev);
309 walk_tree(mp, parent);
313 const char *channel_devices_node = "channel-devices";
314 const char *channel_devices_compat = "SUNW,sun4v-channel-devices";
315 const char *cfg_handle_prop = "cfg-handle";
317 static int __init vio_init(void)
319 struct mdesc_node *root;
321 const u64 *cfg_handle;
324 root = md_find_node_by_name(NULL, channel_devices_node);
326 printk(KERN_INFO "VIO: No channel-devices MDESC node.\n");
330 cdev_node = of_find_node_by_name(NULL, "channel-devices");
332 printk(KERN_INFO "VIO: No channel-devices OBP node.\n");
336 compat = md_get_property(root, "compatible", &len);
338 printk(KERN_ERR "VIO: Channel devices lacks compatible "
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);
348 cfg_handle = md_get_property(root, cfg_handle_prop, NULL);
350 printk(KERN_ERR "VIO: Channel devices lacks %s property\n",
355 cdev_cfg_handle = *cfg_handle;
357 err = bus_register(&vio_bus_type);
359 printk(KERN_ERR "VIO: Could not register bus type err=%d\n",
364 create_devices(root);
369 postcore_initcall(vio_init);