David S. Miller | e53e97c | 2007-07-09 22:22:44 -0700 | [diff] [blame^] | 1 | /* 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 | |
| 19 | static 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 | |
| 34 | static 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 | |
| 62 | static 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 | |
| 74 | static 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 | |
| 90 | static 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 | |
| 101 | static 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 | |
| 107 | if (vdev->type) { |
| 108 | if (!strcmp(vdev->type, "network")) |
| 109 | str = "vnet"; |
| 110 | else if (!strcmp(vdev->type, "block")) |
| 111 | str = "vdisk"; |
| 112 | } |
| 113 | |
| 114 | return sprintf(buf, "%s\n", str); |
| 115 | } |
| 116 | |
| 117 | static struct device_attribute vio_dev_attrs[] = { |
| 118 | __ATTR_RO(devspec), |
| 119 | __ATTR_NULL |
| 120 | }; |
| 121 | |
| 122 | static struct bus_type vio_bus_type = { |
| 123 | .name = "vio", |
| 124 | .dev_attrs = vio_dev_attrs, |
| 125 | .match = vio_bus_match, |
| 126 | .probe = vio_device_probe, |
| 127 | .remove = vio_device_remove, |
| 128 | }; |
| 129 | |
| 130 | int vio_register_driver(struct vio_driver *viodrv) |
| 131 | { |
| 132 | viodrv->driver.bus = &vio_bus_type; |
| 133 | |
| 134 | return driver_register(&viodrv->driver); |
| 135 | } |
| 136 | EXPORT_SYMBOL(vio_register_driver); |
| 137 | |
| 138 | void vio_unregister_driver(struct vio_driver *viodrv) |
| 139 | { |
| 140 | driver_unregister(&viodrv->driver); |
| 141 | } |
| 142 | EXPORT_SYMBOL(vio_unregister_driver); |
| 143 | |
| 144 | struct mdesc_node *vio_find_endpoint(struct vio_dev *vdev) |
| 145 | { |
| 146 | struct mdesc_node *endp, *mp = vdev->mp; |
| 147 | int i; |
| 148 | |
| 149 | endp = NULL; |
| 150 | for (i = 0; i < mp->num_arcs; i++) { |
| 151 | struct mdesc_node *t; |
| 152 | |
| 153 | if (strcmp(mp->arcs[i].name, "fwd")) |
| 154 | continue; |
| 155 | |
| 156 | t = mp->arcs[i].arc; |
| 157 | if (strcmp(t->name, "channel-endpoint")) |
| 158 | continue; |
| 159 | |
| 160 | endp = t; |
| 161 | break; |
| 162 | } |
| 163 | |
| 164 | return endp; |
| 165 | } |
| 166 | EXPORT_SYMBOL(vio_find_endpoint); |
| 167 | |
| 168 | static void __devinit vio_dev_release(struct device *dev) |
| 169 | { |
| 170 | kfree(to_vio_dev(dev)); |
| 171 | } |
| 172 | |
| 173 | static ssize_t |
| 174 | show_pciobppath_attr(struct device *dev, struct device_attribute *attr, |
| 175 | char *buf) |
| 176 | { |
| 177 | struct vio_dev *vdev; |
| 178 | struct device_node *dp; |
| 179 | |
| 180 | vdev = to_vio_dev(dev); |
| 181 | dp = vdev->dp; |
| 182 | |
| 183 | return snprintf (buf, PAGE_SIZE, "%s\n", dp->full_name); |
| 184 | } |
| 185 | |
| 186 | static DEVICE_ATTR(obppath, S_IRUSR | S_IRGRP | S_IROTH, |
| 187 | show_pciobppath_attr, NULL); |
| 188 | |
| 189 | struct device_node *cdev_node; |
| 190 | |
| 191 | static struct vio_dev *root_vdev; |
| 192 | static u64 cdev_cfg_handle; |
| 193 | |
| 194 | static struct vio_dev *vio_create_one(struct mdesc_node *mp, |
| 195 | struct device *parent) |
| 196 | { |
| 197 | const char *type, *compat; |
| 198 | struct device_node *dp; |
| 199 | struct vio_dev *vdev; |
| 200 | const u64 *irq; |
| 201 | int err, clen; |
| 202 | |
| 203 | type = md_get_property(mp, "device-type", NULL); |
| 204 | if (!type) |
| 205 | type = md_get_property(mp, "name", NULL); |
| 206 | compat = md_get_property(mp, "device-type", &clen); |
| 207 | |
| 208 | vdev = kzalloc(sizeof(*vdev), GFP_KERNEL); |
| 209 | if (!vdev) { |
| 210 | printk(KERN_ERR "VIO: Could not allocate vio_dev\n"); |
| 211 | return NULL; |
| 212 | } |
| 213 | |
| 214 | vdev->mp = mp; |
| 215 | vdev->type = type; |
| 216 | vdev->compat = compat; |
| 217 | vdev->compat_len = clen; |
| 218 | |
| 219 | irq = md_get_property(mp, "tx-ino", NULL); |
| 220 | if (irq) |
| 221 | mp->irqs[0] = sun4v_build_virq(cdev_cfg_handle, *irq); |
| 222 | |
| 223 | irq = md_get_property(mp, "rx-ino", NULL); |
| 224 | if (irq) |
| 225 | mp->irqs[1] = sun4v_build_virq(cdev_cfg_handle, *irq); |
| 226 | |
| 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; |
| 231 | |
| 232 | if (parent == NULL) { |
| 233 | dp = cdev_node; |
| 234 | } else if (to_vio_dev(parent) == root_vdev) { |
| 235 | dp = of_get_next_child(cdev_node, NULL); |
| 236 | while (dp) { |
| 237 | if (!strcmp(dp->type, type)) |
| 238 | break; |
| 239 | |
| 240 | dp = of_get_next_child(cdev_node, dp); |
| 241 | } |
| 242 | } else { |
| 243 | dp = to_vio_dev(parent)->dp; |
| 244 | } |
| 245 | vdev->dp = dp; |
| 246 | |
| 247 | err = device_register(&vdev->dev); |
| 248 | if (err) { |
| 249 | printk(KERN_ERR "VIO: Could not register device %s, err=%d\n", |
| 250 | vdev->dev.bus_id, err); |
| 251 | kfree(vdev); |
| 252 | return NULL; |
| 253 | } |
| 254 | if (vdev->dp) |
| 255 | err = sysfs_create_file(&vdev->dev.kobj, |
| 256 | &dev_attr_obppath.attr); |
| 257 | |
| 258 | return vdev; |
| 259 | } |
| 260 | |
| 261 | static void walk_tree(struct mdesc_node *n, struct vio_dev *parent) |
| 262 | { |
| 263 | int i; |
| 264 | |
| 265 | for (i = 0; i < n->num_arcs; i++) { |
| 266 | struct mdesc_node *mp; |
| 267 | struct vio_dev *vdev; |
| 268 | |
| 269 | if (strcmp(n->arcs[i].name, "fwd")) |
| 270 | continue; |
| 271 | |
| 272 | mp = n->arcs[i].arc; |
| 273 | |
| 274 | vdev = vio_create_one(mp, &parent->dev); |
| 275 | if (vdev && mp->num_arcs) |
| 276 | walk_tree(mp, vdev); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | static void create_devices(struct mdesc_node *root) |
| 281 | { |
| 282 | root_vdev = vio_create_one(root, NULL); |
| 283 | if (!root_vdev) { |
| 284 | printk(KERN_ERR "VIO: Coult not create root device.\n"); |
| 285 | return; |
| 286 | } |
| 287 | |
| 288 | walk_tree(root, root_vdev); |
| 289 | } |
| 290 | |
| 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"; |
| 294 | |
| 295 | static int __init vio_init(void) |
| 296 | { |
| 297 | struct mdesc_node *root; |
| 298 | const char *compat; |
| 299 | const u64 *cfg_handle; |
| 300 | int err, len; |
| 301 | |
| 302 | root = md_find_node_by_name(NULL, channel_devices_node); |
| 303 | if (!root) { |
| 304 | printk(KERN_INFO "VIO: No channel-devices MDESC node.\n"); |
| 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | cdev_node = of_find_node_by_name(NULL, "channel-devices"); |
| 309 | if (!cdev_node) { |
| 310 | printk(KERN_INFO "VIO: No channel-devices OBP node.\n"); |
| 311 | return -ENODEV; |
| 312 | } |
| 313 | |
| 314 | compat = md_get_property(root, "compatible", &len); |
| 315 | if (!compat) { |
| 316 | printk(KERN_ERR "VIO: Channel devices lacks compatible " |
| 317 | "property\n"); |
| 318 | return -ENODEV; |
| 319 | } |
| 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); |
| 323 | return -ENODEV; |
| 324 | } |
| 325 | |
| 326 | cfg_handle = md_get_property(root, cfg_handle_prop, NULL); |
| 327 | if (!cfg_handle) { |
| 328 | printk(KERN_ERR "VIO: Channel devices lacks %s property\n", |
| 329 | cfg_handle_prop); |
| 330 | return -ENODEV; |
| 331 | } |
| 332 | |
| 333 | cdev_cfg_handle = *cfg_handle; |
| 334 | |
| 335 | err = bus_register(&vio_bus_type); |
| 336 | if (err) { |
| 337 | printk(KERN_ERR "VIO: Could not register bus type err=%d\n", |
| 338 | err); |
| 339 | return err; |
| 340 | } |
| 341 | |
| 342 | create_devices(root); |
| 343 | |
| 344 | return 0; |
| 345 | } |
| 346 | |
| 347 | postcore_initcall(vio_init); |