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";
108 if (!strcmp(vdev->type, "network"))
110 else if (!strcmp(vdev->type, "block"))
114 return sprintf(buf, "%s\n", str);
117 static struct device_attribute vio_dev_attrs[] = {
122 static struct bus_type vio_bus_type = {
124 .dev_attrs = vio_dev_attrs,
125 .match = vio_bus_match,
126 .probe = vio_device_probe,
127 .remove = vio_device_remove,
130 int vio_register_driver(struct vio_driver *viodrv)
132 viodrv->driver.bus = &vio_bus_type;
134 return driver_register(&viodrv->driver);
136 EXPORT_SYMBOL(vio_register_driver);
138 void vio_unregister_driver(struct vio_driver *viodrv)
140 driver_unregister(&viodrv->driver);
142 EXPORT_SYMBOL(vio_unregister_driver);
144 struct mdesc_node *vio_find_endpoint(struct vio_dev *vdev)
146 struct mdesc_node *endp, *mp = vdev->mp;
150 for (i = 0; i < mp->num_arcs; i++) {
151 struct mdesc_node *t;
153 if (strcmp(mp->arcs[i].name, "fwd"))
157 if (strcmp(t->name, "channel-endpoint"))
166 EXPORT_SYMBOL(vio_find_endpoint);
168 static void __devinit vio_dev_release(struct device *dev)
170 kfree(to_vio_dev(dev));
174 show_pciobppath_attr(struct device *dev, struct device_attribute *attr,
177 struct vio_dev *vdev;
178 struct device_node *dp;
180 vdev = to_vio_dev(dev);
183 return snprintf (buf, PAGE_SIZE, "%s\n", dp->full_name);
186 static DEVICE_ATTR(obppath, S_IRUSR | S_IRGRP | S_IROTH,
187 show_pciobppath_attr, NULL);
189 struct device_node *cdev_node;
191 static struct vio_dev *root_vdev;
192 static u64 cdev_cfg_handle;
194 static struct vio_dev *vio_create_one(struct mdesc_node *mp,
195 struct device *parent)
197 const char *type, *compat;
198 struct device_node *dp;
199 struct vio_dev *vdev;
203 type = md_get_property(mp, "device-type", NULL);
205 type = md_get_property(mp, "name", NULL);
206 compat = md_get_property(mp, "device-type", &clen);
208 vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
210 printk(KERN_ERR "VIO: Could not allocate vio_dev\n");
216 vdev->compat = compat;
217 vdev->compat_len = clen;
219 irq = md_get_property(mp, "tx-ino", NULL);
221 mp->irqs[0] = sun4v_build_virq(cdev_cfg_handle, *irq);
223 irq = md_get_property(mp, "rx-ino", NULL);
225 mp->irqs[1] = sun4v_build_virq(cdev_cfg_handle, *irq);
227 snprintf(vdev->dev.bus_id, BUS_ID_SIZE, "%lx", mp->node);
228 vdev->dev.parent = parent;
229 vdev->dev.bus = &vio_bus_type;
230 vdev->dev.release = vio_dev_release;
232 if (parent == NULL) {
234 } else if (to_vio_dev(parent) == root_vdev) {
235 dp = of_get_next_child(cdev_node, NULL);
237 if (!strcmp(dp->type, type))
240 dp = of_get_next_child(cdev_node, dp);
243 dp = to_vio_dev(parent)->dp;
247 err = device_register(&vdev->dev);
249 printk(KERN_ERR "VIO: Could not register device %s, err=%d\n",
250 vdev->dev.bus_id, err);
255 err = sysfs_create_file(&vdev->dev.kobj,
256 &dev_attr_obppath.attr);
261 static void walk_tree(struct mdesc_node *n, struct vio_dev *parent)
265 for (i = 0; i < n->num_arcs; i++) {
266 struct mdesc_node *mp;
267 struct vio_dev *vdev;
269 if (strcmp(n->arcs[i].name, "fwd"))
274 vdev = vio_create_one(mp, &parent->dev);
275 if (vdev && mp->num_arcs)
280 static void create_devices(struct mdesc_node *root)
282 root_vdev = vio_create_one(root, NULL);
284 printk(KERN_ERR "VIO: Coult not create root device.\n");
288 walk_tree(root, root_vdev);
291 const char *channel_devices_node = "channel-devices";
292 const char *channel_devices_compat = "SUNW,sun4v-channel-devices";
293 const char *cfg_handle_prop = "cfg-handle";
295 static int __init vio_init(void)
297 struct mdesc_node *root;
299 const u64 *cfg_handle;
302 root = md_find_node_by_name(NULL, channel_devices_node);
304 printk(KERN_INFO "VIO: No channel-devices MDESC node.\n");
308 cdev_node = of_find_node_by_name(NULL, "channel-devices");
310 printk(KERN_INFO "VIO: No channel-devices OBP node.\n");
314 compat = md_get_property(root, "compatible", &len);
316 printk(KERN_ERR "VIO: Channel devices lacks compatible "
320 if (!find_in_proplist(compat, channel_devices_compat, len)) {
321 printk(KERN_ERR "VIO: Channel devices node lacks (%s) "
322 "compat entry.\n", channel_devices_compat);
326 cfg_handle = md_get_property(root, cfg_handle_prop, NULL);
328 printk(KERN_ERR "VIO: Channel devices lacks %s property\n",
333 cdev_cfg_handle = *cfg_handle;
335 err = bus_register(&vio_bus_type);
337 printk(KERN_ERR "VIO: Could not register bus type err=%d\n",
342 create_devices(root);
347 postcore_initcall(vio_init);