xref: /linux/arch/sparc/kernel/vio.c (revision 69050f8d6d075dc01af7a5f2f550a8067510366f)
1 // SPDX-License-Identifier: GPL-2.0
2 /* vio.c: Virtual I/O channel devices probing infrastructure.
3  *
4  *    Copyright (c) 2003-2005 IBM Corp.
5  *     Dave Engebretsen engebret@us.ibm.com
6  *     Santiago Leon santil@us.ibm.com
7  *     Hollis Blanchard <hollisb@us.ibm.com>
8  *     Stephen Rothwell
9  *
10  * Adapted to sparc64 by David S. Miller davem@davemloft.net
11  */
12 
13 #include <linux/kernel.h>
14 #include <linux/slab.h>
15 #include <linux/string.h>
16 #include <linux/irq.h>
17 #include <linux/export.h>
18 #include <linux/init.h>
19 
20 #include <asm/mdesc.h>
21 #include <asm/vio.h>
22 
23 static const struct vio_device_id *vio_match_device(
24 	const struct vio_device_id *matches,
25 	const struct vio_dev *dev)
26 {
27 	const char *type, *compat;
28 	int len;
29 
30 	type = dev->type;
31 	compat = dev->compat;
32 	len = dev->compat_len;
33 
34 	while (matches->type[0] || matches->compat[0]) {
35 		int match = 1;
36 		if (matches->type[0])
37 			match &= !strcmp(matches->type, type);
38 
39 		if (matches->compat[0]) {
40 			match &= len &&
41 				of_find_in_proplist(compat, matches->compat, len);
42 		}
43 		if (match)
44 			return matches;
45 		matches++;
46 	}
47 	return NULL;
48 }
49 
50 static int vio_hotplug(const struct device *dev, struct kobj_uevent_env *env)
51 {
52 	const struct vio_dev *vio_dev = to_vio_dev(dev);
53 
54 	add_uevent_var(env, "MODALIAS=vio:T%sS%s", vio_dev->type, vio_dev->compat);
55 	return 0;
56 }
57 
58 static int vio_bus_match(struct device *dev, const struct device_driver *drv)
59 {
60 	struct vio_dev *vio_dev = to_vio_dev(dev);
61 	const struct vio_driver *vio_drv = to_vio_driver(drv);
62 	const struct vio_device_id *matches = vio_drv->id_table;
63 
64 	if (!matches)
65 		return 0;
66 
67 	return vio_match_device(matches, vio_dev) != NULL;
68 }
69 
70 static int vio_device_probe(struct device *dev)
71 {
72 	struct vio_dev *vdev = to_vio_dev(dev);
73 	struct vio_driver *drv = to_vio_driver(dev->driver);
74 	const struct vio_device_id *id;
75 
76 	if (!drv->probe)
77 		return -ENODEV;
78 
79 	id = vio_match_device(drv->id_table, vdev);
80 	if (!id)
81 		return -ENODEV;
82 
83 	/* alloc irqs (unless the driver specified not to) */
84 	if (!drv->no_irq) {
85 		if (vdev->tx_irq == 0 && vdev->tx_ino != ~0UL)
86 			vdev->tx_irq = sun4v_build_virq(vdev->cdev_handle,
87 							vdev->tx_ino);
88 
89 		if (vdev->rx_irq == 0 && vdev->rx_ino != ~0UL)
90 			vdev->rx_irq = sun4v_build_virq(vdev->cdev_handle,
91 							vdev->rx_ino);
92 	}
93 
94 	return drv->probe(vdev, id);
95 }
96 
97 static void vio_device_remove(struct device *dev)
98 {
99 	struct vio_dev *vdev = to_vio_dev(dev);
100 	struct vio_driver *drv = to_vio_driver(dev->driver);
101 
102 	if (drv->remove) {
103 		/*
104 		 * Ideally, we would remove/deallocate tx/rx virqs
105 		 * here - however, there are currently no support
106 		 * routines to do so at the moment. TBD
107 		 */
108 
109 		drv->remove(vdev);
110 	}
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 const 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 scnprintf(buf, PAGE_SIZE, "%pOF\n", dp);
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 EXPORT_SYMBOL(vio_vdev_node);
250 
251 static void vio_fill_channel_info(struct mdesc_handle *hp, u64 mp,
252 				  struct vio_dev *vdev)
253 {
254 	u64 a;
255 
256 	vdev->tx_ino = ~0UL;
257 	vdev->rx_ino = ~0UL;
258 	vdev->channel_id = ~0UL;
259 	mdesc_for_each_arc(a, hp, mp, MDESC_ARC_TYPE_FWD) {
260 		const u64 *chan_id;
261 		const u64 *irq;
262 		u64 target;
263 
264 		target = mdesc_arc_target(hp, a);
265 
266 		irq = mdesc_get_property(hp, target, "tx-ino", NULL);
267 		if (irq)
268 			vdev->tx_ino = *irq;
269 
270 		irq = mdesc_get_property(hp, target, "rx-ino", NULL);
271 		if (irq)
272 			vdev->rx_ino = *irq;
273 
274 		chan_id = mdesc_get_property(hp, target, "id", NULL);
275 		if (chan_id)
276 			vdev->channel_id = *chan_id;
277 	}
278 
279 	vdev->cdev_handle = cdev_cfg_handle;
280 }
281 
282 int vio_set_intr(unsigned long dev_ino, int state)
283 {
284 	int err;
285 
286 	err = sun4v_vintr_set_valid(cdev_cfg_handle, dev_ino, state);
287 	return err;
288 }
289 EXPORT_SYMBOL(vio_set_intr);
290 
291 static struct vio_dev *vio_create_one(struct mdesc_handle *hp, u64 mp,
292 				      const char *node_name,
293 				      struct device *parent)
294 {
295 	const char *type, *compat;
296 	struct device_node *dp;
297 	struct vio_dev *vdev;
298 	int err, tlen, clen;
299 	const u64 *id, *cfg_handle;
300 
301 	type = mdesc_get_property(hp, mp, "device-type", &tlen);
302 	if (!type) {
303 		type = mdesc_get_property(hp, mp, "name", &tlen);
304 		if (!type) {
305 			type = mdesc_node_name(hp, mp);
306 			tlen = strlen(type) + 1;
307 		}
308 	}
309 	if (tlen > VIO_MAX_TYPE_LEN || strlen(type) >= VIO_MAX_TYPE_LEN) {
310 		printk(KERN_ERR "VIO: Type string [%s] is too long.\n",
311 		       type);
312 		return NULL;
313 	}
314 
315 	id = mdesc_get_property(hp, mp, "id", NULL);
316 
317 	cfg_handle = vio_cfg_handle(hp, mp);
318 
319 	compat = mdesc_get_property(hp, mp, "device-type", &clen);
320 	if (!compat) {
321 		clen = 0;
322 	} else if (clen > VIO_MAX_COMPAT_LEN) {
323 		printk(KERN_ERR "VIO: Compat len %d for [%s] is too long.\n",
324 		       clen, type);
325 		return NULL;
326 	}
327 
328 	vdev = kzalloc_obj(*vdev, GFP_KERNEL);
329 	if (!vdev) {
330 		printk(KERN_ERR "VIO: Could not allocate vio_dev\n");
331 		return NULL;
332 	}
333 
334 	vdev->mp = mp;
335 	memcpy(vdev->type, type, tlen);
336 	if (compat)
337 		memcpy(vdev->compat, compat, clen);
338 	else
339 		memset(vdev->compat, 0, sizeof(vdev->compat));
340 	vdev->compat_len = clen;
341 
342 	vdev->port_id = ~0UL;
343 	vdev->tx_irq = 0;
344 	vdev->rx_irq = 0;
345 
346 	vio_fill_channel_info(hp, mp, vdev);
347 
348 	if (!id) {
349 		dev_set_name(&vdev->dev, "%s", type);
350 		vdev->dev_no = ~(u64)0;
351 	} else if (!cfg_handle) {
352 		dev_set_name(&vdev->dev, "%s-%llu", type, *id);
353 		vdev->dev_no = *id;
354 	} else {
355 		dev_set_name(&vdev->dev, "%s-%llu-%llu", type,
356 			     *cfg_handle, *id);
357 		vdev->dev_no = *cfg_handle;
358 		vdev->port_id = *id;
359 	}
360 
361 	vdev->dev.parent = parent;
362 	vdev->dev.bus = &vio_bus_type;
363 	vdev->dev.release = vio_dev_release;
364 
365 	if (parent == NULL) {
366 		dp = cdev_node;
367 	} else if (to_vio_dev(parent) == root_vdev) {
368 		for_each_child_of_node(cdev_node, dp) {
369 			if (of_node_is_type(dp, type))
370 				break;
371 		}
372 	} else {
373 		dp = to_vio_dev(parent)->dp;
374 	}
375 	vdev->dp = dp;
376 
377 	/*
378 	 * node_name is NULL for the parent/channel-devices node and
379 	 * the parent doesn't require the MD node info.
380 	 */
381 	if (node_name != NULL) {
382 		strscpy(vdev->node_name, node_name);
383 
384 		err = mdesc_get_node_info(hp, mp, node_name,
385 					  &vdev->md_node_info);
386 		if (err) {
387 			pr_err("VIO: Could not get MD node info %s, err=%d\n",
388 			       dev_name(&vdev->dev), err);
389 			kfree(vdev);
390 			return NULL;
391 		}
392 	}
393 
394 	pr_info("VIO: Adding device %s (tx_ino = %llx, rx_ino = %llx)\n",
395 		dev_name(&vdev->dev), vdev->tx_ino, vdev->rx_ino);
396 
397 	err = device_register(&vdev->dev);
398 	if (err) {
399 		printk(KERN_ERR "VIO: Could not register device %s, err=%d\n",
400 		       dev_name(&vdev->dev), err);
401 		put_device(&vdev->dev);
402 		return NULL;
403 	}
404 	if (vdev->dp)
405 		err = sysfs_create_file(&vdev->dev.kobj,
406 					&dev_attr_obppath.attr);
407 
408 	return vdev;
409 }
410 
411 static void vio_add(struct mdesc_handle *hp, u64 node,
412 		    const char *node_name)
413 {
414 	(void) vio_create_one(hp, node, node_name, &root_vdev->dev);
415 }
416 
417 struct vio_remove_node_data {
418 	struct mdesc_handle *hp;
419 	u64 node;
420 };
421 
422 static int vio_md_node_match(struct device *dev, const void *arg)
423 {
424 	struct vio_dev *vdev = to_vio_dev(dev);
425 	const struct vio_remove_node_data *node_data;
426 	u64 node;
427 
428 	node_data = (const struct vio_remove_node_data *)arg;
429 
430 	node = vio_vdev_node(node_data->hp, vdev);
431 
432 	if (node == node_data->node)
433 		return 1;
434 	else
435 		return 0;
436 }
437 
438 static void vio_remove(struct mdesc_handle *hp, u64 node, const char *node_name)
439 {
440 	struct vio_remove_node_data node_data;
441 	struct device *dev;
442 
443 	node_data.hp = hp;
444 	node_data.node = node;
445 
446 	dev = device_find_child(&root_vdev->dev, (void *)&node_data,
447 				vio_md_node_match);
448 	if (dev) {
449 		printk(KERN_INFO "VIO: Removing device %s\n", dev_name(dev));
450 
451 		device_unregister(dev);
452 		put_device(dev);
453 	} else {
454 		pr_err("VIO: %s node not found in MDESC\n", node_name);
455 	}
456 }
457 
458 static struct mdesc_notifier_client vio_device_notifier = {
459 	.add		= vio_add,
460 	.remove		= vio_remove,
461 	.node_name	= "virtual-device-port",
462 };
463 
464 /* We are only interested in domain service ports under the
465  * "domain-services" node.  On control nodes there is another port
466  * under "openboot" that we should not mess with as aparently that is
467  * reserved exclusively for OBP use.
468  */
469 static void vio_add_ds(struct mdesc_handle *hp, u64 node,
470 		       const char *node_name)
471 {
472 	int found;
473 	u64 a;
474 
475 	found = 0;
476 	mdesc_for_each_arc(a, hp, node, MDESC_ARC_TYPE_BACK) {
477 		u64 target = mdesc_arc_target(hp, a);
478 		const char *name = mdesc_node_name(hp, target);
479 
480 		if (!strcmp(name, "domain-services")) {
481 			found = 1;
482 			break;
483 		}
484 	}
485 
486 	if (found)
487 		(void) vio_create_one(hp, node, node_name, &root_vdev->dev);
488 }
489 
490 static struct mdesc_notifier_client vio_ds_notifier = {
491 	.add		= vio_add_ds,
492 	.remove		= vio_remove,
493 	.node_name	= "domain-services-port",
494 };
495 
496 static const char *channel_devices_node = "channel-devices";
497 static const char *channel_devices_compat = "SUNW,sun4v-channel-devices";
498 static const char *cfg_handle_prop = "cfg-handle";
499 
500 static int __init vio_init(void)
501 {
502 	struct mdesc_handle *hp;
503 	const char *compat;
504 	const u64 *cfg_handle;
505 	int err, len;
506 	u64 root;
507 
508 	err = bus_register(&vio_bus_type);
509 	if (err) {
510 		printk(KERN_ERR "VIO: Could not register bus type err=%d\n",
511 		       err);
512 		return err;
513 	}
514 
515 	hp = mdesc_grab();
516 	if (!hp)
517 		return 0;
518 
519 	root = mdesc_node_by_name(hp, MDESC_NODE_NULL, channel_devices_node);
520 	if (root == MDESC_NODE_NULL) {
521 		printk(KERN_INFO "VIO: No channel-devices MDESC node.\n");
522 		mdesc_release(hp);
523 		return 0;
524 	}
525 
526 	cdev_node = of_find_node_by_name(NULL, "channel-devices");
527 	err = -ENODEV;
528 	if (!cdev_node) {
529 		printk(KERN_INFO "VIO: No channel-devices OBP node.\n");
530 		goto out_release;
531 	}
532 
533 	compat = mdesc_get_property(hp, root, "compatible", &len);
534 	if (!compat) {
535 		printk(KERN_ERR "VIO: Channel devices lacks compatible "
536 		       "property\n");
537 		goto out_release;
538 	}
539 	if (!of_find_in_proplist(compat, channel_devices_compat, len)) {
540 		printk(KERN_ERR "VIO: Channel devices node lacks (%s) "
541 		       "compat entry.\n", channel_devices_compat);
542 		goto out_release;
543 	}
544 
545 	cfg_handle = mdesc_get_property(hp, root, cfg_handle_prop, NULL);
546 	if (!cfg_handle) {
547 		printk(KERN_ERR "VIO: Channel devices lacks %s property\n",
548 		       cfg_handle_prop);
549 		goto out_release;
550 	}
551 
552 	cdev_cfg_handle = *cfg_handle;
553 
554 	root_vdev = vio_create_one(hp, root, NULL, NULL);
555 	err = -ENODEV;
556 	if (!root_vdev) {
557 		printk(KERN_ERR "VIO: Could not create root device.\n");
558 		goto out_release;
559 	}
560 
561 	mdesc_register_notifier(&vio_device_notifier);
562 	mdesc_register_notifier(&vio_ds_notifier);
563 
564 	mdesc_release(hp);
565 
566 	return err;
567 
568 out_release:
569 	mdesc_release(hp);
570 	return err;
571 }
572 
573 postcore_initcall(vio_init);
574