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