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/slab.h> 14 #include <linux/irq.h> 15 #include <linux/export.h> 16 #include <linux/init.h> 17 18 #include <asm/mdesc.h> 19 #include <asm/vio.h> 20 21 static const struct vio_device_id *vio_match_device( 22 const struct vio_device_id *matches, 23 const struct vio_dev *dev) 24 { 25 const char *type, *compat; 26 int len; 27 28 type = dev->type; 29 compat = dev->compat; 30 len = dev->compat_len; 31 32 while (matches->type[0] || matches->compat[0]) { 33 int match = 1; 34 if (matches->type[0]) 35 match &= !strcmp(matches->type, type); 36 37 if (matches->compat[0]) { 38 match &= len && 39 of_find_in_proplist(compat, matches->compat, len); 40 } 41 if (match) 42 return matches; 43 matches++; 44 } 45 return NULL; 46 } 47 48 static int vio_hotplug(struct device *dev, struct kobj_uevent_env *env) 49 { 50 const struct vio_dev *vio_dev = to_vio_dev(dev); 51 52 add_uevent_var(env, "MODALIAS=vio:T%sS%s", vio_dev->type, vio_dev->compat); 53 return 0; 54 } 55 56 static int vio_bus_match(struct device *dev, struct device_driver *drv) 57 { 58 struct vio_dev *vio_dev = to_vio_dev(dev); 59 struct vio_driver *vio_drv = to_vio_driver(drv); 60 const struct vio_device_id *matches = vio_drv->id_table; 61 62 if (!matches) 63 return 0; 64 65 return vio_match_device(matches, vio_dev) != NULL; 66 } 67 68 static int vio_device_probe(struct device *dev) 69 { 70 struct vio_dev *vdev = to_vio_dev(dev); 71 struct vio_driver *drv = to_vio_driver(dev->driver); 72 const struct vio_device_id *id; 73 74 if (!drv->probe) 75 return -ENODEV; 76 77 id = vio_match_device(drv->id_table, vdev); 78 if (!id) 79 return -ENODEV; 80 81 /* alloc irqs (unless the driver specified not to) */ 82 if (!drv->no_irq) { 83 if (vdev->tx_irq == 0 && vdev->tx_ino != ~0UL) 84 vdev->tx_irq = sun4v_build_virq(vdev->cdev_handle, 85 vdev->tx_ino); 86 87 if (vdev->rx_irq == 0 && vdev->rx_ino != ~0UL) 88 vdev->rx_irq = sun4v_build_virq(vdev->cdev_handle, 89 vdev->rx_ino); 90 } 91 92 return drv->probe(vdev, id); 93 } 94 95 static int vio_device_remove(struct device *dev) 96 { 97 struct vio_dev *vdev = to_vio_dev(dev); 98 struct vio_driver *drv = to_vio_driver(dev->driver); 99 100 if (drv->remove) { 101 /* 102 * Ideally, we would remove/deallocate tx/rx virqs 103 * here - however, there are currently no support 104 * routines to do so at the moment. TBD 105 */ 106 107 return drv->remove(vdev); 108 } 109 110 return 1; 111 } 112 113 static ssize_t devspec_show(struct device *dev, 114 struct device_attribute *attr, char *buf) 115 { 116 struct vio_dev *vdev = to_vio_dev(dev); 117 const char *str = "none"; 118 119 if (!strcmp(vdev->type, "vnet-port")) 120 str = "vnet"; 121 else if (!strcmp(vdev->type, "vdc-port")) 122 str = "vdisk"; 123 124 return sprintf(buf, "%s\n", str); 125 } 126 static DEVICE_ATTR_RO(devspec); 127 128 static ssize_t type_show(struct device *dev, 129 struct device_attribute *attr, char *buf) 130 { 131 struct vio_dev *vdev = to_vio_dev(dev); 132 return sprintf(buf, "%s\n", vdev->type); 133 } 134 static DEVICE_ATTR_RO(type); 135 136 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, 137 char *buf) 138 { 139 const struct vio_dev *vdev = to_vio_dev(dev); 140 141 return sprintf(buf, "vio:T%sS%s\n", vdev->type, vdev->compat); 142 } 143 static DEVICE_ATTR_RO(modalias); 144 145 static struct attribute *vio_dev_attrs[] = { 146 &dev_attr_devspec.attr, 147 &dev_attr_type.attr, 148 &dev_attr_modalias.attr, 149 NULL, 150 }; 151 ATTRIBUTE_GROUPS(vio_dev); 152 153 static struct bus_type vio_bus_type = { 154 .name = "vio", 155 .dev_groups = vio_dev_groups, 156 .uevent = vio_hotplug, 157 .match = vio_bus_match, 158 .probe = vio_device_probe, 159 .remove = vio_device_remove, 160 }; 161 162 int __vio_register_driver(struct vio_driver *viodrv, struct module *owner, 163 const char *mod_name) 164 { 165 viodrv->driver.bus = &vio_bus_type; 166 viodrv->driver.name = viodrv->name; 167 viodrv->driver.owner = owner; 168 viodrv->driver.mod_name = mod_name; 169 170 return driver_register(&viodrv->driver); 171 } 172 EXPORT_SYMBOL(__vio_register_driver); 173 174 void vio_unregister_driver(struct vio_driver *viodrv) 175 { 176 driver_unregister(&viodrv->driver); 177 } 178 EXPORT_SYMBOL(vio_unregister_driver); 179 180 static void vio_dev_release(struct device *dev) 181 { 182 kfree(to_vio_dev(dev)); 183 } 184 185 static ssize_t 186 show_pciobppath_attr(struct device *dev, struct device_attribute *attr, 187 char *buf) 188 { 189 struct vio_dev *vdev; 190 struct device_node *dp; 191 192 vdev = to_vio_dev(dev); 193 dp = vdev->dp; 194 195 return snprintf (buf, PAGE_SIZE, "%s\n", dp->full_name); 196 } 197 198 static DEVICE_ATTR(obppath, S_IRUSR | S_IRGRP | S_IROTH, 199 show_pciobppath_attr, NULL); 200 201 static struct device_node *cdev_node; 202 203 static struct vio_dev *root_vdev; 204 static u64 cdev_cfg_handle; 205 206 static const u64 *vio_cfg_handle(struct mdesc_handle *hp, u64 node) 207 { 208 const u64 *cfg_handle = NULL; 209 u64 a; 210 211 mdesc_for_each_arc(a, hp, node, MDESC_ARC_TYPE_BACK) { 212 u64 target; 213 214 target = mdesc_arc_target(hp, a); 215 cfg_handle = mdesc_get_property(hp, target, 216 "cfg-handle", NULL); 217 if (cfg_handle) 218 break; 219 } 220 221 return cfg_handle; 222 } 223 224 /** 225 * vio_vdev_node() - Find VDEV node in MD 226 * @hp: Handle to the MD 227 * @vdev: Pointer to VDEV 228 * 229 * Find the node in the current MD which matches the given vio_dev. This 230 * must be done dynamically since the node value can change if the MD 231 * is updated. 232 * 233 * NOTE: the MD must be locked, using mdesc_grab(), when calling this routine 234 * 235 * Return: The VDEV node in MDESC 236 */ 237 u64 vio_vdev_node(struct mdesc_handle *hp, struct vio_dev *vdev) 238 { 239 u64 node; 240 241 if (vdev == NULL) 242 return MDESC_NODE_NULL; 243 244 node = mdesc_get_node(hp, (const char *)vdev->node_name, 245 &vdev->md_node_info); 246 247 return node; 248 } 249 250 static void vio_fill_channel_info(struct mdesc_handle *hp, u64 mp, 251 struct vio_dev *vdev) 252 { 253 u64 a; 254 255 vdev->tx_ino = ~0UL; 256 vdev->rx_ino = ~0UL; 257 vdev->channel_id = ~0UL; 258 mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_FWD) { 259 const u64 *chan_id; 260 const u64 *irq; 261 u64 target; 262 263 target = mdesc_arc_target(hp, a); 264 265 irq = mdesc_get_property(hp, target, "tx-ino", NULL); 266 if (irq) 267 vdev->tx_ino = *irq; 268 269 irq = mdesc_get_property(hp, target, "rx-ino", NULL); 270 if (irq) 271 vdev->rx_ino = *irq; 272 273 chan_id = mdesc_get_property(hp, target, "id", NULL); 274 if (chan_id) 275 vdev->channel_id = *chan_id; 276 } 277 278 vdev->cdev_handle = cdev_cfg_handle; 279 } 280 281 int vio_set_intr(unsigned long dev_ino, int state) 282 { 283 int err; 284 285 err = sun4v_vintr_set_valid(cdev_cfg_handle, dev_ino, state); 286 return err; 287 } 288 EXPORT_SYMBOL(vio_set_intr); 289 290 static struct vio_dev *vio_create_one(struct mdesc_handle *hp, u64 mp, 291 const char *node_name, 292 struct device *parent) 293 { 294 const char *type, *compat; 295 struct device_node *dp; 296 struct vio_dev *vdev; 297 int err, tlen, clen; 298 const u64 *id, *cfg_handle; 299 300 type = mdesc_get_property(hp, mp, "device-type", &tlen); 301 if (!type) { 302 type = mdesc_get_property(hp, mp, "name", &tlen); 303 if (!type) { 304 type = mdesc_node_name(hp, mp); 305 tlen = strlen(type) + 1; 306 } 307 } 308 if (tlen > VIO_MAX_TYPE_LEN || strlen(type) >= VIO_MAX_TYPE_LEN) { 309 printk(KERN_ERR "VIO: Type string [%s] is too long.\n", 310 type); 311 return NULL; 312 } 313 314 id = mdesc_get_property(hp, mp, "id", NULL); 315 316 cfg_handle = vio_cfg_handle(hp, mp); 317 318 compat = mdesc_get_property(hp, mp, "device-type", &clen); 319 if (!compat) { 320 clen = 0; 321 } else if (clen > VIO_MAX_COMPAT_LEN) { 322 printk(KERN_ERR "VIO: Compat len %d for [%s] is too long.\n", 323 clen, type); 324 return NULL; 325 } 326 327 vdev = kzalloc(sizeof(*vdev), GFP_KERNEL); 328 if (!vdev) { 329 printk(KERN_ERR "VIO: Could not allocate vio_dev\n"); 330 return NULL; 331 } 332 333 vdev->mp = mp; 334 memcpy(vdev->type, type, tlen); 335 if (compat) 336 memcpy(vdev->compat, compat, clen); 337 else 338 memset(vdev->compat, 0, sizeof(vdev->compat)); 339 vdev->compat_len = clen; 340 341 vdev->port_id = ~0UL; 342 vdev->tx_irq = 0; 343 vdev->rx_irq = 0; 344 345 vio_fill_channel_info(hp, mp, vdev); 346 347 if (!id) { 348 dev_set_name(&vdev->dev, "%s", type); 349 vdev->dev_no = ~(u64)0; 350 } else if (!cfg_handle) { 351 dev_set_name(&vdev->dev, "%s-%llu", type, *id); 352 vdev->dev_no = *id; 353 } else { 354 dev_set_name(&vdev->dev, "%s-%llu-%llu", type, 355 *cfg_handle, *id); 356 vdev->dev_no = *cfg_handle; 357 vdev->port_id = *id; 358 } 359 360 vdev->dev.parent = parent; 361 vdev->dev.bus = &vio_bus_type; 362 vdev->dev.release = vio_dev_release; 363 364 if (parent == NULL) { 365 dp = cdev_node; 366 } else if (to_vio_dev(parent) == root_vdev) { 367 dp = of_get_next_child(cdev_node, NULL); 368 while (dp) { 369 if (!strcmp(dp->type, type)) 370 break; 371 372 dp = of_get_next_child(cdev_node, dp); 373 } 374 } else { 375 dp = to_vio_dev(parent)->dp; 376 } 377 vdev->dp = dp; 378 379 /* 380 * node_name is NULL for the parent/channel-devices node and 381 * the parent doesn't require the MD node info. 382 */ 383 if (node_name != NULL) { 384 (void) snprintf(vdev->node_name, VIO_MAX_NAME_LEN, "%s", 385 node_name); 386 387 err = mdesc_get_node_info(hp, mp, node_name, 388 &vdev->md_node_info); 389 if (err) { 390 pr_err("VIO: Could not get MD node info %s, err=%d\n", 391 dev_name(&vdev->dev), err); 392 kfree(vdev); 393 return NULL; 394 } 395 } 396 397 pr_info("VIO: Adding device %s (tx_ino = %llx, rx_ino = %llx)\n", 398 dev_name(&vdev->dev), vdev->tx_ino, vdev->rx_ino); 399 400 err = device_register(&vdev->dev); 401 if (err) { 402 printk(KERN_ERR "VIO: Could not register device %s, err=%d\n", 403 dev_name(&vdev->dev), err); 404 kfree(vdev); 405 return NULL; 406 } 407 if (vdev->dp) 408 err = sysfs_create_file(&vdev->dev.kobj, 409 &dev_attr_obppath.attr); 410 411 return vdev; 412 } 413 414 static void vio_add(struct mdesc_handle *hp, u64 node, 415 const char *node_name) 416 { 417 (void) vio_create_one(hp, node, node_name, &root_vdev->dev); 418 } 419 420 struct vio_remove_node_data { 421 struct mdesc_handle *hp; 422 u64 node; 423 }; 424 425 static int vio_md_node_match(struct device *dev, void *arg) 426 { 427 struct vio_dev *vdev = to_vio_dev(dev); 428 struct vio_remove_node_data *node_data; 429 u64 node; 430 431 node_data = (struct vio_remove_node_data *)arg; 432 433 node = vio_vdev_node(node_data->hp, vdev); 434 435 if (node == node_data->node) 436 return 1; 437 else 438 return 0; 439 } 440 441 static void vio_remove(struct mdesc_handle *hp, u64 node, const char *node_name) 442 { 443 struct vio_remove_node_data node_data; 444 struct device *dev; 445 446 node_data.hp = hp; 447 node_data.node = node; 448 449 dev = device_find_child(&root_vdev->dev, (void *)&node_data, 450 vio_md_node_match); 451 if (dev) { 452 printk(KERN_INFO "VIO: Removing device %s\n", dev_name(dev)); 453 454 device_unregister(dev); 455 put_device(dev); 456 } else { 457 pr_err("VIO: %s node not found in MDESC\n", node_name); 458 } 459 } 460 461 static struct mdesc_notifier_client vio_device_notifier = { 462 .add = vio_add, 463 .remove = vio_remove, 464 .node_name = "virtual-device-port", 465 }; 466 467 /* We are only interested in domain service ports under the 468 * "domain-services" node. On control nodes there is another port 469 * under "openboot" that we should not mess with as aparently that is 470 * reserved exclusively for OBP use. 471 */ 472 static void vio_add_ds(struct mdesc_handle *hp, u64 node, 473 const char *node_name) 474 { 475 int found; 476 u64 a; 477 478 found = 0; 479 mdesc_for_each_arc(a, hp, node, MDESC_ARC_TYPE_BACK) { 480 u64 target = mdesc_arc_target(hp, a); 481 const char *name = mdesc_node_name(hp, target); 482 483 if (!strcmp(name, "domain-services")) { 484 found = 1; 485 break; 486 } 487 } 488 489 if (found) 490 (void) vio_create_one(hp, node, node_name, &root_vdev->dev); 491 } 492 493 static struct mdesc_notifier_client vio_ds_notifier = { 494 .add = vio_add_ds, 495 .remove = vio_remove, 496 .node_name = "domain-services-port", 497 }; 498 499 static const char *channel_devices_node = "channel-devices"; 500 static const char *channel_devices_compat = "SUNW,sun4v-channel-devices"; 501 static const char *cfg_handle_prop = "cfg-handle"; 502 503 static int __init vio_init(void) 504 { 505 struct mdesc_handle *hp; 506 const char *compat; 507 const u64 *cfg_handle; 508 int err, len; 509 u64 root; 510 511 err = bus_register(&vio_bus_type); 512 if (err) { 513 printk(KERN_ERR "VIO: Could not register bus type err=%d\n", 514 err); 515 return err; 516 } 517 518 hp = mdesc_grab(); 519 if (!hp) 520 return 0; 521 522 root = mdesc_node_by_name(hp, MDESC_NODE_NULL, channel_devices_node); 523 if (root == MDESC_NODE_NULL) { 524 printk(KERN_INFO "VIO: No channel-devices MDESC node.\n"); 525 mdesc_release(hp); 526 return 0; 527 } 528 529 cdev_node = of_find_node_by_name(NULL, "channel-devices"); 530 err = -ENODEV; 531 if (!cdev_node) { 532 printk(KERN_INFO "VIO: No channel-devices OBP node.\n"); 533 goto out_release; 534 } 535 536 compat = mdesc_get_property(hp, root, "compatible", &len); 537 if (!compat) { 538 printk(KERN_ERR "VIO: Channel devices lacks compatible " 539 "property\n"); 540 goto out_release; 541 } 542 if (!of_find_in_proplist(compat, channel_devices_compat, len)) { 543 printk(KERN_ERR "VIO: Channel devices node lacks (%s) " 544 "compat entry.\n", channel_devices_compat); 545 goto out_release; 546 } 547 548 cfg_handle = mdesc_get_property(hp, root, cfg_handle_prop, NULL); 549 if (!cfg_handle) { 550 printk(KERN_ERR "VIO: Channel devices lacks %s property\n", 551 cfg_handle_prop); 552 goto out_release; 553 } 554 555 cdev_cfg_handle = *cfg_handle; 556 557 root_vdev = vio_create_one(hp, root, NULL, NULL); 558 err = -ENODEV; 559 if (!root_vdev) { 560 printk(KERN_ERR "VIO: Could not create root device.\n"); 561 goto out_release; 562 } 563 564 mdesc_register_notifier(&vio_device_notifier); 565 mdesc_register_notifier(&vio_ds_notifier); 566 567 mdesc_release(hp); 568 569 return err; 570 571 out_release: 572 mdesc_release(hp); 573 return err; 574 } 575 576 postcore_initcall(vio_init); 577