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/slab.h>
14 #include <linux/irq.h>
15 #include <linux/export.h>
16 #include <linux/init.h>
18 #include <asm/mdesc.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
;
30 len
= dev
->compat_len
;
32 while (matches
->type
[0] || matches
->compat
[0]) {
35 match
&= !strcmp(matches
->type
, type
);
37 if (matches
->compat
[0]) {
39 of_find_in_proplist(compat
, matches
->compat
, len
);
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
);
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
;
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
;
77 id
= vio_match_device(drv
->id_table
, vdev
);
81 /* alloc irqs (unless the driver specified not to) */
83 if (vdev
->tx_irq
== 0 && vdev
->tx_ino
!= ~0UL)
84 vdev
->tx_irq
= sun4v_build_virq(vdev
->cdev_handle
,
87 if (vdev
->rx_irq
== 0 && vdev
->rx_ino
!= ~0UL)
88 vdev
->rx_irq
= sun4v_build_virq(vdev
->cdev_handle
,
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
);
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
);
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"))
121 else if (!strcmp(vdev
->type
, "vdc-port"))
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
,
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
,
148 &dev_attr_modalias
.attr
,
151 ATTRIBUTE_GROUPS(vio_dev
);
153 static struct bus_type vio_bus_type
= {
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
));
186 show_pciobppath_attr(struct device
*dev
, struct device_attribute
*attr
,
189 struct vio_dev
*vdev
;
190 struct device_node
*dp
;
192 vdev
= to_vio_dev(dev
);
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
;
211 mdesc_for_each_arc(a
, hp
, node
, MDESC_ARC_TYPE_BACK
) {
214 target
= mdesc_arc_target(hp
, a
);
215 cfg_handle
= mdesc_get_property(hp
, target
,
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
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
)
242 return MDESC_NODE_NULL
;
244 node
= mdesc_get_node(hp
, (const char *)vdev
->node_name
,
245 &vdev
->md_node_info
);
250 static void vio_fill_channel_info(struct mdesc_handle
*hp
, u64 mp
,
251 struct vio_dev
*vdev
)
257 vdev
->channel_id
= ~0UL;
258 mdesc_for_each_arc(a
, hp
, mp
, MDESC_ARC_TYPE_FWD
) {
263 target
= mdesc_arc_target(hp
, a
);
265 irq
= mdesc_get_property(hp
, target
, "tx-ino", NULL
);
269 irq
= mdesc_get_property(hp
, target
, "rx-ino", NULL
);
273 chan_id
= mdesc_get_property(hp
, target
, "id", NULL
);
275 vdev
->channel_id
= *chan_id
;
278 vdev
->cdev_handle
= cdev_cfg_handle
;
281 int vio_set_intr(unsigned long dev_ino
, int state
)
285 err
= sun4v_vintr_set_valid(cdev_cfg_handle
, dev_ino
, state
);
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
;
298 const u64
*id
, *cfg_handle
;
300 type
= mdesc_get_property(hp
, mp
, "device-type", &tlen
);
302 type
= mdesc_get_property(hp
, mp
, "name", &tlen
);
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",
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
);
321 } else if (clen
> VIO_MAX_COMPAT_LEN
) {
322 printk(KERN_ERR
"VIO: Compat len %d for [%s] is too long.\n",
327 vdev
= kzalloc(sizeof(*vdev
), GFP_KERNEL
);
329 printk(KERN_ERR
"VIO: Could not allocate vio_dev\n");
334 memcpy(vdev
->type
, type
, tlen
);
336 memcpy(vdev
->compat
, compat
, clen
);
338 memset(vdev
->compat
, 0, sizeof(vdev
->compat
));
339 vdev
->compat_len
= clen
;
341 vdev
->port_id
= ~0UL;
345 vio_fill_channel_info(hp
, mp
, vdev
);
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
);
354 dev_set_name(&vdev
->dev
, "%s-%llu-%llu", type
,
356 vdev
->dev_no
= *cfg_handle
;
360 vdev
->dev
.parent
= parent
;
361 vdev
->dev
.bus
= &vio_bus_type
;
362 vdev
->dev
.release
= vio_dev_release
;
364 if (parent
== NULL
) {
366 } else if (to_vio_dev(parent
) == root_vdev
) {
367 dp
= of_get_next_child(cdev_node
, NULL
);
369 if (!strcmp(dp
->type
, type
))
372 dp
= of_get_next_child(cdev_node
, dp
);
375 dp
= to_vio_dev(parent
)->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",
387 err
= mdesc_get_node_info(hp
, mp
, node_name
,
388 &vdev
->md_node_info
);
390 pr_err("VIO: Could not get MD node info %s, err=%d\n",
391 dev_name(&vdev
->dev
), err
);
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
);
402 printk(KERN_ERR
"VIO: Could not register device %s, err=%d\n",
403 dev_name(&vdev
->dev
), err
);
408 err
= sysfs_create_file(&vdev
->dev
.kobj
,
409 &dev_attr_obppath
.attr
);
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
;
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
;
431 node_data
= (struct vio_remove_node_data
*)arg
;
433 node
= vio_vdev_node(node_data
->hp
, vdev
);
435 if (node
== node_data
->node
)
441 static void vio_remove(struct mdesc_handle
*hp
, u64 node
, const char *node_name
)
443 struct vio_remove_node_data node_data
;
447 node_data
.node
= node
;
449 dev
= device_find_child(&root_vdev
->dev
, (void *)&node_data
,
452 printk(KERN_INFO
"VIO: Removing device %s\n", dev_name(dev
));
454 device_unregister(dev
);
457 pr_err("VIO: %s node not found in MDESC\n", node_name
);
461 static struct mdesc_notifier_client vio_device_notifier
= {
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
)
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")) {
490 (void) vio_create_one(hp
, node
, node_name
, &root_vdev
->dev
);
493 static struct mdesc_notifier_client vio_ds_notifier
= {
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
;
507 const u64
*cfg_handle
;
511 err
= bus_register(&vio_bus_type
);
513 printk(KERN_ERR
"VIO: Could not register bus type err=%d\n",
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");
529 cdev_node
= of_find_node_by_name(NULL
, "channel-devices");
532 printk(KERN_INFO
"VIO: No channel-devices OBP node.\n");
536 compat
= mdesc_get_property(hp
, root
, "compatible", &len
);
538 printk(KERN_ERR
"VIO: Channel devices lacks compatible "
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
);
548 cfg_handle
= mdesc_get_property(hp
, root
, cfg_handle_prop
, NULL
);
550 printk(KERN_ERR
"VIO: Channel devices lacks %s property\n",
555 cdev_cfg_handle
= *cfg_handle
;
557 root_vdev
= vio_create_one(hp
, root
, NULL
, NULL
);
560 printk(KERN_ERR
"VIO: Could not create root device.\n");
564 mdesc_register_notifier(&vio_device_notifier
);
565 mdesc_register_notifier(&vio_ds_notifier
);
576 postcore_initcall(vio_init
);