Linux 4.13.16
[linux/fpc-iii.git] / arch / sparc / kernel / vio.c
blob1c8763c9c52b292b85cccdc544e034ba318d564f
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>
7 * Stephen Rothwell
9 * Adapted to sparc64 by David S. Miller davem@davemloft.net
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>
18 #include <asm/mdesc.h>
19 #include <asm/vio.h>
21 static const struct vio_device_id *vio_match_device(
22 const struct vio_device_id *matches,
23 const struct vio_dev *dev)
25 const char *type, *compat;
26 int len;
28 type = dev->type;
29 compat = dev->compat;
30 len = dev->compat_len;
32 while (matches->type[0] || matches->compat[0]) {
33 int match = 1;
34 if (matches->type[0])
35 match &= !strcmp(matches->type, type);
37 if (matches->compat[0]) {
38 match &= len &&
39 of_find_in_proplist(compat, matches->compat, len);
41 if (match)
42 return matches;
43 matches++;
45 return NULL;
48 static int vio_hotplug(struct device *dev, struct kobj_uevent_env *env)
50 const struct vio_dev *vio_dev = to_vio_dev(dev);
52 add_uevent_var(env, "MODALIAS=vio:T%sS%s", vio_dev->type, vio_dev->compat);
53 return 0;
56 static int vio_bus_match(struct device *dev, struct device_driver *drv)
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;
62 if (!matches)
63 return 0;
65 return vio_match_device(matches, vio_dev) != NULL;
68 static int vio_device_probe(struct device *dev)
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;
74 if (!drv->probe)
75 return -ENODEV;
77 id = vio_match_device(drv->id_table, vdev);
78 if (!id)
79 return -ENODEV;
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);
87 if (vdev->rx_irq == 0 && vdev->rx_ino != ~0UL)
88 vdev->rx_irq = sun4v_build_virq(vdev->cdev_handle,
89 vdev->rx_ino);
92 return drv->probe(vdev, id);
95 static int vio_device_remove(struct device *dev)
97 struct vio_dev *vdev = to_vio_dev(dev);
98 struct vio_driver *drv = to_vio_driver(dev->driver);
100 if (drv->remove) {
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
107 return drv->remove(vdev);
110 return 1;
113 static ssize_t devspec_show(struct device *dev,
114 struct device_attribute *attr, char *buf)
116 struct vio_dev *vdev = to_vio_dev(dev);
117 const char *str = "none";
119 if (!strcmp(vdev->type, "vnet-port"))
120 str = "vnet";
121 else if (!strcmp(vdev->type, "vdc-port"))
122 str = "vdisk";
124 return sprintf(buf, "%s\n", str);
126 static DEVICE_ATTR_RO(devspec);
128 static ssize_t type_show(struct device *dev,
129 struct device_attribute *attr, char *buf)
131 struct vio_dev *vdev = to_vio_dev(dev);
132 return sprintf(buf, "%s\n", vdev->type);
134 static DEVICE_ATTR_RO(type);
136 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
137 char *buf)
139 const struct vio_dev *vdev = to_vio_dev(dev);
141 return sprintf(buf, "vio:T%sS%s\n", vdev->type, vdev->compat);
143 static DEVICE_ATTR_RO(modalias);
145 static struct attribute *vio_dev_attrs[] = {
146 &dev_attr_devspec.attr,
147 &dev_attr_type.attr,
148 &dev_attr_modalias.attr,
149 NULL,
151 ATTRIBUTE_GROUPS(vio_dev);
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,
162 int __vio_register_driver(struct vio_driver *viodrv, struct module *owner,
163 const char *mod_name)
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;
170 return driver_register(&viodrv->driver);
172 EXPORT_SYMBOL(__vio_register_driver);
174 void vio_unregister_driver(struct vio_driver *viodrv)
176 driver_unregister(&viodrv->driver);
178 EXPORT_SYMBOL(vio_unregister_driver);
180 static void vio_dev_release(struct device *dev)
182 kfree(to_vio_dev(dev));
185 static ssize_t
186 show_pciobppath_attr(struct device *dev, struct device_attribute *attr,
187 char *buf)
189 struct vio_dev *vdev;
190 struct device_node *dp;
192 vdev = to_vio_dev(dev);
193 dp = vdev->dp;
195 return snprintf (buf, PAGE_SIZE, "%s\n", dp->full_name);
198 static DEVICE_ATTR(obppath, S_IRUSR | S_IRGRP | S_IROTH,
199 show_pciobppath_attr, NULL);
201 static struct device_node *cdev_node;
203 static struct vio_dev *root_vdev;
204 static u64 cdev_cfg_handle;
206 static const u64 *vio_cfg_handle(struct mdesc_handle *hp, u64 node)
208 const u64 *cfg_handle = NULL;
209 u64 a;
211 mdesc_for_each_arc(a, hp, node, MDESC_ARC_TYPE_BACK) {
212 u64 target;
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;
221 return cfg_handle;
225 * vio_vdev_node() - Find VDEV node in MD
226 * @hp: Handle to the MD
227 * @vdev: Pointer to VDEV
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.
233 * NOTE: the MD must be locked, using mdesc_grab(), when calling this routine
235 * Return: The VDEV node in MDESC
237 u64 vio_vdev_node(struct mdesc_handle *hp, struct vio_dev *vdev)
239 u64 node;
241 if (vdev == NULL)
242 return MDESC_NODE_NULL;
244 node = mdesc_get_node(hp, (const char *)vdev->node_name,
245 &vdev->md_node_info);
247 return node;
250 static void vio_fill_channel_info(struct mdesc_handle *hp, u64 mp,
251 struct vio_dev *vdev)
253 u64 a;
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;
263 target = mdesc_arc_target(hp, a);
265 irq = mdesc_get_property(hp, target, "tx-ino", NULL);
266 if (irq)
267 vdev->tx_ino = *irq;
269 irq = mdesc_get_property(hp, target, "rx-ino", NULL);
270 if (irq)
271 vdev->rx_ino = *irq;
273 chan_id = mdesc_get_property(hp, target, "id", NULL);
274 if (chan_id)
275 vdev->channel_id = *chan_id;
278 vdev->cdev_handle = cdev_cfg_handle;
281 int vio_set_intr(unsigned long dev_ino, int state)
283 int err;
285 err = sun4v_vintr_set_valid(cdev_cfg_handle, dev_ino, state);
286 return err;
288 EXPORT_SYMBOL(vio_set_intr);
290 static struct vio_dev *vio_create_one(struct mdesc_handle *hp, u64 mp,
291 const char *node_name,
292 struct device *parent)
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;
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;
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;
314 id = mdesc_get_property(hp, mp, "id", NULL);
316 cfg_handle = vio_cfg_handle(hp, mp);
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;
327 vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
328 if (!vdev) {
329 printk(KERN_ERR "VIO: Could not allocate vio_dev\n");
330 return NULL;
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;
341 vdev->port_id = ~0UL;
342 vdev->tx_irq = 0;
343 vdev->rx_irq = 0;
345 vio_fill_channel_info(hp, mp, vdev);
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;
360 vdev->dev.parent = parent;
361 vdev->dev.bus = &vio_bus_type;
362 vdev->dev.release = vio_dev_release;
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;
372 dp = of_get_next_child(cdev_node, dp);
374 } else {
375 dp = to_vio_dev(parent)->dp;
377 vdev->dp = dp;
380 * node_name is NULL for the parent/channel-devices node and
381 * the parent doesn't require the MD node info.
383 if (node_name != NULL) {
384 (void) snprintf(vdev->node_name, VIO_MAX_NAME_LEN, "%s",
385 node_name);
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;
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);
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;
407 if (vdev->dp)
408 err = sysfs_create_file(&vdev->dev.kobj,
409 &dev_attr_obppath.attr);
411 return vdev;
414 static void vio_add(struct mdesc_handle *hp, u64 node,
415 const char *node_name)
417 (void) vio_create_one(hp, node, node_name, &root_vdev->dev);
420 struct vio_remove_node_data {
421 struct mdesc_handle *hp;
422 u64 node;
425 static int vio_md_node_match(struct device *dev, void *arg)
427 struct vio_dev *vdev = to_vio_dev(dev);
428 struct vio_remove_node_data *node_data;
429 u64 node;
431 node_data = (struct vio_remove_node_data *)arg;
433 node = vio_vdev_node(node_data->hp, vdev);
435 if (node == node_data->node)
436 return 1;
437 else
438 return 0;
441 static void vio_remove(struct mdesc_handle *hp, u64 node, const char *node_name)
443 struct vio_remove_node_data node_data;
444 struct device *dev;
446 node_data.hp = hp;
447 node_data.node = node;
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));
454 device_unregister(dev);
455 put_device(dev);
456 } else {
457 pr_err("VIO: %s node not found in MDESC\n", node_name);
461 static struct mdesc_notifier_client vio_device_notifier = {
462 .add = vio_add,
463 .remove = vio_remove,
464 .node_name = "virtual-device-port",
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.
472 static void vio_add_ds(struct mdesc_handle *hp, u64 node,
473 const char *node_name)
475 int found;
476 u64 a;
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);
483 if (!strcmp(name, "domain-services")) {
484 found = 1;
485 break;
489 if (found)
490 (void) vio_create_one(hp, node, node_name, &root_vdev->dev);
493 static struct mdesc_notifier_client vio_ds_notifier = {
494 .add = vio_add_ds,
495 .remove = vio_remove,
496 .node_name = "domain-services-port",
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";
503 static int __init vio_init(void)
505 struct mdesc_handle *hp;
506 const char *compat;
507 const u64 *cfg_handle;
508 int err, len;
509 u64 root;
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;
518 hp = mdesc_grab();
519 if (!hp)
520 return 0;
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;
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;
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;
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;
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;
555 cdev_cfg_handle = *cfg_handle;
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;
564 mdesc_register_notifier(&vio_device_notifier);
565 mdesc_register_notifier(&vio_ds_notifier);
567 mdesc_release(hp);
569 return err;
571 out_release:
572 mdesc_release(hp);
573 return err;
576 postcore_initcall(vio_init);