103831d35Sstevel /*
203831d35Sstevel  * CDDL HEADER START
303831d35Sstevel  *
403831d35Sstevel  * The contents of this file are subject to the terms of the
5*0d63ce2bSvenki  * Common Development and Distribution License (the "License").
6*0d63ce2bSvenki  * You may not use this file except in compliance with the License.
703831d35Sstevel  *
803831d35Sstevel  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
903831d35Sstevel  * or http://www.opensolaris.org/os/licensing.
1003831d35Sstevel  * See the License for the specific language governing permissions
1103831d35Sstevel  * and limitations under the License.
1203831d35Sstevel  *
1303831d35Sstevel  * When distributing Covered Code, include this CDDL HEADER in each
1403831d35Sstevel  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1503831d35Sstevel  * If applicable, add the following below this CDDL HEADER, with the
1603831d35Sstevel  * fields enclosed by brackets "[]" replaced with your own identifying
1703831d35Sstevel  * information: Portions Copyright [yyyy] [name of copyright owner]
1803831d35Sstevel  *
1903831d35Sstevel  * CDDL HEADER END
2003831d35Sstevel  */
2103831d35Sstevel /*
22*0d63ce2bSvenki  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
2303831d35Sstevel  * Use is subject to license terms.
2403831d35Sstevel  */
2503831d35Sstevel 
2603831d35Sstevel #include <stdio.h>
2703831d35Sstevel #include <stdlib.h>
2803831d35Sstevel #include <string.h>
2903831d35Sstevel #include <fcntl.h>
3003831d35Sstevel #include <dirent.h>
3103831d35Sstevel #include <varargs.h>
3203831d35Sstevel #include <errno.h>
3303831d35Sstevel #include <unistd.h>
3403831d35Sstevel #include <alloca.h>
3503831d35Sstevel #include <sys/systeminfo.h>
3603831d35Sstevel #include <sys/utsname.h>
3703831d35Sstevel #include <sys/openpromio.h>
3803831d35Sstevel #include <kstat.h>
3903831d35Sstevel #include <libintl.h>
4003831d35Sstevel #include "pdevinfo.h"
4103831d35Sstevel #include "display.h"
4203831d35Sstevel #include "display_sun4v.h"
4303831d35Sstevel #include "libprtdiag.h"
4403831d35Sstevel 
4503831d35Sstevel #if !defined(TEXT_DOMAIN)
4603831d35Sstevel #define	TEXT_DOMAIN	"SYS_TEST"
4703831d35Sstevel #endif
4803831d35Sstevel 
4903831d35Sstevel /*
5003831d35Sstevel  * Global variables
5103831d35Sstevel  */
5203831d35Sstevel char	*progname;
5303831d35Sstevel char	*promdev = "/dev/openprom";
5403831d35Sstevel int	print_flag = 1;
5503831d35Sstevel int	logging = 0;
5603831d35Sstevel 
5703831d35Sstevel /*
5803831d35Sstevel  * This file represents the splitting out of some functionality
5903831d35Sstevel  * of prtdiag due to the port to the sun4v platform. The PROM
6003831d35Sstevel  * tree-walking functions which contain sun4v specifics were moved
6103831d35Sstevel  * into this module.
6203831d35Sstevel  */
6303831d35Sstevel 
6403831d35Sstevel extern int get_id(Prom_node *);
6503831d35Sstevel 
6603831d35Sstevel /* Function prototypes */
6703831d35Sstevel Prom_node *sun4v_walk(Sys_tree *, Prom_node *, int);
68*0d63ce2bSvenki picl_errno_t sun4v_get_node_by_name(picl_nodehdl_t, char *, picl_nodehdl_t *);
69*0d63ce2bSvenki 
7003831d35Sstevel /*
7103831d35Sstevel  * do_prominfo() is called from main() in usr/src/cmd/prtdiag/main.c
7203831d35Sstevel  *
7303831d35Sstevel  * This is the starting point for all platforms. However, this function
7403831d35Sstevel  * can be overlayed by writing a do_prominfo() function
7503831d35Sstevel  * in the libprtdiag_psr for a particular platform.
7603831d35Sstevel  *
7703831d35Sstevel  */
7803831d35Sstevel int
do_prominfo(int syserrlog,char * pgname,int log_flag,int prt_flag)7903831d35Sstevel do_prominfo(int syserrlog, char *pgname, int log_flag, int prt_flag)
8003831d35Sstevel {
8103831d35Sstevel 	Sys_tree sys_tree;		/* system information */
8203831d35Sstevel 	Prom_node *root_node;		/* root node of OBP device tree */
8303831d35Sstevel 	picl_nodehdl_t	rooth;		/* root PICL node for IO display */
8403831d35Sstevel 	picl_nodehdl_t plafh;		/* Platform PICL node for IO display */
8503831d35Sstevel 
86*0d63ce2bSvenki 	picl_errno_t err;
8703831d35Sstevel 
8803831d35Sstevel 	err = picl_initialize();
8903831d35Sstevel 	if (err != PICL_SUCCESS) {
9003831d35Sstevel 		(void) fprintf(stderr, EM_INIT_FAIL, picl_strerror(err));
9103831d35Sstevel 		exit(1);
9203831d35Sstevel 	}
9303831d35Sstevel 
9403831d35Sstevel 	/* set the global flags */
9503831d35Sstevel 	progname = pgname;
9603831d35Sstevel 	logging = log_flag;
9703831d35Sstevel 	print_flag = prt_flag;
9803831d35Sstevel 
9903831d35Sstevel 	/* set the the system tree fields */
10003831d35Sstevel 	sys_tree.sys_mem = NULL;
10103831d35Sstevel 	sys_tree.boards = NULL;
10203831d35Sstevel 	sys_tree.bd_list = NULL;
10303831d35Sstevel 	sys_tree.board_cnt = 0;
10403831d35Sstevel 
10503831d35Sstevel 	if (promopen(O_RDONLY))  {
10603831d35Sstevel 		exit(_error(dgettext(TEXT_DOMAIN, "openeepr device "
10703831d35Sstevel 			"open failed")));
10803831d35Sstevel 	}
10903831d35Sstevel 
11003831d35Sstevel 	if (is_openprom() == 0)  {
11103831d35Sstevel 		(void) fprintf(stderr, "%s",
11203831d35Sstevel 			dgettext(TEXT_DOMAIN, "System architecture "
11303831d35Sstevel 			    "does not support this option of this "
11403831d35Sstevel 			    "command.\n"));
11503831d35Sstevel 		return (2);
11603831d35Sstevel 	}
11703831d35Sstevel 
11803831d35Sstevel 	if (next(0) == 0) {
11903831d35Sstevel 		return (2);
12003831d35Sstevel 	}
12103831d35Sstevel 
12203831d35Sstevel 	root_node = sun4v_walk(&sys_tree, NULL, next(0));
12303831d35Sstevel 	promclose();
12403831d35Sstevel 
12503831d35Sstevel 	err = picl_get_root(&rooth);
12603831d35Sstevel 	if (err != PICL_SUCCESS) {
12703831d35Sstevel 		(void) fprintf(stderr, EM_GET_ROOT_FAIL, picl_strerror(err));
12803831d35Sstevel 		exit(1);
12903831d35Sstevel 	}
13003831d35Sstevel 
13103831d35Sstevel 	err = sun4v_get_node_by_name(rooth, PICL_NODE_PLATFORM, &plafh);
13203831d35Sstevel 	if (err != PICL_SUCCESS)
13303831d35Sstevel 		return (err);
13403831d35Sstevel 
13503831d35Sstevel 	return (sun4v_display(&sys_tree, root_node, syserrlog, plafh));
13603831d35Sstevel 
13703831d35Sstevel }
13803831d35Sstevel 
13903831d35Sstevel /*
14003831d35Sstevel  * sun4v_Walk the PROM device tree and build the system tree and root tree.
14103831d35Sstevel  * Nodes that have a board number property are placed in the board
14203831d35Sstevel  * structures for easier processing later. Child nodes are placed
14303831d35Sstevel  * under their parents.
14403831d35Sstevel  */
14503831d35Sstevel Prom_node *
sun4v_walk(Sys_tree * tree,Prom_node * root,int id)14603831d35Sstevel sun4v_walk(Sys_tree *tree, Prom_node *root, int id)
14703831d35Sstevel {
14803831d35Sstevel 	register int curnode;
14903831d35Sstevel 	Prom_node *pnode;
15003831d35Sstevel 	char *name;
15103831d35Sstevel 	char *type;
15203831d35Sstevel 	char *compatible;
15303831d35Sstevel 	int board_node = 0;
15403831d35Sstevel 
15503831d35Sstevel 	/* allocate a node for this level */
15603831d35Sstevel 	if ((pnode = (Prom_node *) malloc(sizeof (struct prom_node))) ==
15703831d35Sstevel 	    NULL) {
15803831d35Sstevel 		perror("malloc");
15903831d35Sstevel 		exit(2);	/* program errors cause exit 2 */
16003831d35Sstevel 	}
16103831d35Sstevel 
16203831d35Sstevel 	/* assign parent Prom_node */
16303831d35Sstevel 	pnode->parent = root;
16403831d35Sstevel 	pnode->sibling = NULL;
16503831d35Sstevel 	pnode->child = NULL;
16603831d35Sstevel 
16703831d35Sstevel 	/* read properties for this node */
16803831d35Sstevel 	dump_node(pnode);
16903831d35Sstevel 
17003831d35Sstevel 	/*
17103831d35Sstevel 	 * Place a node in a 'board' if it has 'board'-ness. The definition
17203831d35Sstevel 	 * is that all nodes that are children of root should have a
17303831d35Sstevel 	 * board# property. But the PROM tree does not exactly follow
17403831d35Sstevel 	 * this. This is where we start hacking.
17503831d35Sstevel 	 *
17603831d35Sstevel 	 * PCI to PCI bridges also have the name "pci", but with different
17703831d35Sstevel 	 * model property values.  They should not be put under 'board'.
17803831d35Sstevel 	 */
17903831d35Sstevel 	name = get_node_name(pnode);
18003831d35Sstevel 	type = get_node_type(pnode);
18103831d35Sstevel 	compatible = (char *)get_prop_val(find_prop(pnode, "compatible"));
18203831d35Sstevel 
18303831d35Sstevel #ifdef DEBUG
18403831d35Sstevel 	if (name != NULL)
18503831d35Sstevel 		printf("name=%s ", name);
18603831d35Sstevel 	if (type != NULL)
18703831d35Sstevel 		printf("type=%s ", type);
18803831d35Sstevel 	printf("\n");
18903831d35Sstevel #endif
19003831d35Sstevel 	if (compatible == NULL)
19103831d35Sstevel 		compatible = "";
19203831d35Sstevel 	if (type == NULL)
19303831d35Sstevel 		type = "";
19403831d35Sstevel 	if (name != NULL) {
19503831d35Sstevel 		if (has_board_num(pnode)) {
19603831d35Sstevel 			add_node(tree, pnode);
19703831d35Sstevel 			board_node = 1;
19803831d35Sstevel #ifdef DEBUG
19903831d35Sstevel 			printf("ADDED BOARD name=%s type=%s compatible=%s\n",
20003831d35Sstevel 				name, type, compatible);
20103831d35Sstevel #endif
20203831d35Sstevel 		} else if (strcmp(type, "cpu") == 0) {
20303831d35Sstevel 			add_node(tree, pnode);
20403831d35Sstevel 			board_node = 1;
20503831d35Sstevel #ifdef DEBUG
20603831d35Sstevel 			printf("ADDED BOARD name=%s type=%s compatible=%s\n",
20703831d35Sstevel 				name, type, compatible);
20803831d35Sstevel #endif
20903831d35Sstevel 		}
21003831d35Sstevel #ifdef DEBUG
21103831d35Sstevel 		else
21203831d35Sstevel 			printf("node not added: name=%s type=%s\n", name, type);
21303831d35Sstevel #endif
21403831d35Sstevel 	}
21503831d35Sstevel 
21603831d35Sstevel 	if (curnode = child(id)) {
21703831d35Sstevel 		pnode->child = sun4v_walk(tree, pnode, curnode);
21803831d35Sstevel 	}
21903831d35Sstevel 
22003831d35Sstevel 	if (curnode = next(id)) {
22103831d35Sstevel 		if (board_node) {
22203831d35Sstevel 			return (sun4v_walk(tree, root, curnode));
22303831d35Sstevel 		} else {
22403831d35Sstevel 			pnode->sibling = sun4v_walk(tree, root, curnode);
22503831d35Sstevel 		}
22603831d35Sstevel 	}
22703831d35Sstevel 
22803831d35Sstevel 	if (board_node) {
22903831d35Sstevel 		return (NULL);
23003831d35Sstevel 	} else {
23103831d35Sstevel 		return (pnode);
23203831d35Sstevel 	}
23303831d35Sstevel }
23403831d35Sstevel 
23503831d35Sstevel /*
23603831d35Sstevel  * search children to get the node by the nodename
23703831d35Sstevel  */
238*0d63ce2bSvenki picl_errno_t
sun4v_get_node_by_name(picl_nodehdl_t rooth,char * name,picl_nodehdl_t * nodeh)23903831d35Sstevel sun4v_get_node_by_name(picl_nodehdl_t rooth, char *name,
24003831d35Sstevel     picl_nodehdl_t *nodeh)
24103831d35Sstevel {
24203831d35Sstevel 	picl_nodehdl_t	childh;
24303831d35Sstevel 	int		err;
24403831d35Sstevel 	char		*nodename;
24503831d35Sstevel 
24603831d35Sstevel 	nodename = alloca(strlen(name) + 1);
24703831d35Sstevel 	if (nodename == NULL)
24803831d35Sstevel 		return (PICL_FAILURE);
24903831d35Sstevel 
25003831d35Sstevel 	err = picl_get_propval_by_name(rooth, PICL_PROP_CHILD, &childh,
25103831d35Sstevel 	    sizeof (picl_nodehdl_t));
25203831d35Sstevel 
25303831d35Sstevel 	while (err == PICL_SUCCESS) {
25403831d35Sstevel 		err = picl_get_propval_by_name(childh, PICL_PROP_NAME,
25503831d35Sstevel 		    nodename, (strlen(name) + 1));
25603831d35Sstevel 		if (err != PICL_SUCCESS) {
25703831d35Sstevel 			err = picl_get_propval_by_name(childh, PICL_PROP_PEER,
25803831d35Sstevel 				&childh, sizeof (picl_nodehdl_t));
25903831d35Sstevel 			continue;
26003831d35Sstevel 		}
26103831d35Sstevel 
26203831d35Sstevel 		if (strcmp(nodename, name) == 0) {
26303831d35Sstevel 			*nodeh = childh;
26403831d35Sstevel 			return (PICL_SUCCESS);
26503831d35Sstevel 		}
26603831d35Sstevel 
26703831d35Sstevel 		err = picl_get_propval_by_name(childh, PICL_PROP_PEER,
26803831d35Sstevel 		    &childh, sizeof (picl_nodehdl_t));
26903831d35Sstevel 	}
27003831d35Sstevel 
27103831d35Sstevel 	return (err);
27203831d35Sstevel }
27303831d35Sstevel 
27403831d35Sstevel int
get_id(Prom_node * node)27503831d35Sstevel get_id(Prom_node *node)
27603831d35Sstevel {
27703831d35Sstevel #ifdef	lint
27803831d35Sstevel 	node = node;
27903831d35Sstevel #endif
28003831d35Sstevel 
28103831d35Sstevel 	/*
28203831d35Sstevel 	 * This function is intentionally empty
28303831d35Sstevel 	 */
28403831d35Sstevel 	return (0);
28503831d35Sstevel }
286