1*1708Sstevel /*
2*1708Sstevel * CDDL HEADER START
3*1708Sstevel *
4*1708Sstevel * The contents of this file are subject to the terms of the
5*1708Sstevel * Common Development and Distribution License, Version 1.0 only
6*1708Sstevel * (the "License"). You may not use this file except in compliance
7*1708Sstevel * with the License.
8*1708Sstevel *
9*1708Sstevel * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*1708Sstevel * or http://www.opensolaris.org/os/licensing.
11*1708Sstevel * See the License for the specific language governing permissions
12*1708Sstevel * and limitations under the License.
13*1708Sstevel *
14*1708Sstevel * When distributing Covered Code, include this CDDL HEADER in each
15*1708Sstevel * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*1708Sstevel * If applicable, add the following below this CDDL HEADER, with the
17*1708Sstevel * fields enclosed by brackets "[]" replaced with your own identifying
18*1708Sstevel * information: Portions Copyright [yyyy] [name of copyright owner]
19*1708Sstevel *
20*1708Sstevel * CDDL HEADER END
21*1708Sstevel */
22*1708Sstevel /*
23*1708Sstevel * Copyright 2000-2003 Sun Microsystems, Inc. All rights reserved.
24*1708Sstevel * Use is subject to license terms.
25*1708Sstevel */
26*1708Sstevel
27*1708Sstevel #pragma ident "%Z%%M% %I% %E% SMI"
28*1708Sstevel
29*1708Sstevel #include <stdio.h>
30*1708Sstevel #include <stdlib.h>
31*1708Sstevel #include <string.h>
32*1708Sstevel #include <unistd.h>
33*1708Sstevel #include <sys/systeminfo.h>
34*1708Sstevel #include <sys/utsname.h>
35*1708Sstevel #include <sys/openpromio.h>
36*1708Sstevel #include <libdevinfo.h>
37*1708Sstevel
38*1708Sstevel #include "pdevinfo.h"
39*1708Sstevel #include "pdevinfo_sun4u.h"
40*1708Sstevel #include "display.h"
41*1708Sstevel #include "display_sun4u.h"
42*1708Sstevel #include "libprtdiag.h"
43*1708Sstevel
44*1708Sstevel /*
45*1708Sstevel * This file contains the functions that are to be called when
46*1708Sstevel * a platform wants to use libdevinfo for it's device information
47*1708Sstevel * instead of OBP. This will allow prtdiag to support hot-plug
48*1708Sstevel * events on platforms whose OBP doesn't get updated to reflect
49*1708Sstevel * the hot-plug changes to the system.
50*1708Sstevel */
51*1708Sstevel
52*1708Sstevel int do_devinfo(int syserrlog, char *pgname, int log_flag,
53*1708Sstevel int prt_flag);
54*1708Sstevel static void dump_di_node(Prom_node *pnode, di_node_t di_node);
55*1708Sstevel static Prom_node *walk_di_tree(Sys_tree *tree, Prom_node *root,
56*1708Sstevel di_node_t di_node);
57*1708Sstevel static int match_compatible_name(char *, int, char *);
58*1708Sstevel
59*1708Sstevel /*
60*1708Sstevel * Global variables
61*1708Sstevel */
62*1708Sstevel di_prom_handle_t ph; /* Handle for using di_prom interface */
63*1708Sstevel extern char *progname;
64*1708Sstevel
65*1708Sstevel
66*1708Sstevel
67*1708Sstevel /*
68*1708Sstevel * Used instead of the walk() function when a platform wants to
69*1708Sstevel * walk libdevinfo's device tree instead of walking OBP's
70*1708Sstevel * device tree.
71*1708Sstevel */
72*1708Sstevel static Prom_node*
walk_di_tree(Sys_tree * tree,Prom_node * root,di_node_t di_node)73*1708Sstevel walk_di_tree(Sys_tree *tree, Prom_node *root, di_node_t di_node)
74*1708Sstevel {
75*1708Sstevel di_node_t curnode;
76*1708Sstevel Prom_node *pnode;
77*1708Sstevel char *name, *type, *model, *compatible_array;
78*1708Sstevel int board_node = 0;
79*1708Sstevel int *int_val;
80*1708Sstevel int portid;
81*1708Sstevel int is_schizo = 0, n_names;
82*1708Sstevel
83*1708Sstevel /* allocate a node for this level */
84*1708Sstevel if ((pnode = (Prom_node *) malloc(sizeof (struct prom_node))) ==
85*1708Sstevel NULL) {
86*1708Sstevel perror("malloc");
87*1708Sstevel exit(2);
88*1708Sstevel }
89*1708Sstevel
90*1708Sstevel /* assign parent Prom_node */
91*1708Sstevel pnode->parent = root;
92*1708Sstevel pnode->sibling = NULL;
93*1708Sstevel pnode->child = NULL;
94*1708Sstevel
95*1708Sstevel /* read properties for this node */
96*1708Sstevel dump_di_node(pnode, di_node);
97*1708Sstevel
98*1708Sstevel name = get_node_name(pnode);
99*1708Sstevel type = get_node_type(pnode);
100*1708Sstevel if (type == NULL)
101*1708Sstevel type = "";
102*1708Sstevel model = (char *)get_prop_val(find_prop(pnode, "model"));
103*1708Sstevel if (model == NULL)
104*1708Sstevel model = "";
105*1708Sstevel
106*1708Sstevel /*
107*1708Sstevel * For identifying Schizo nodes we need to check if the
108*1708Sstevel * compatible property contains the string 'pci108e,8001'.
109*1708Sstevel * This property contains an array of strings so we need
110*1708Sstevel * search all strings.
111*1708Sstevel */
112*1708Sstevel if ((n_names = di_compatible_names(di_node, &compatible_array)) > 0) {
113*1708Sstevel if (match_compatible_name(compatible_array, n_names,
114*1708Sstevel "pci108e,8001"))
115*1708Sstevel is_schizo = 1;
116*1708Sstevel }
117*1708Sstevel
118*1708Sstevel if (int_val = (int *)get_prop_val(find_prop(pnode, "portid")))
119*1708Sstevel portid = *int_val;
120*1708Sstevel else if ((strcmp(type, "cpu") == 0) &&
121*1708Sstevel (int_val = (int *)get_prop_val(find_prop(pnode->parent, "portid"))))
122*1708Sstevel portid = *int_val;
123*1708Sstevel else
124*1708Sstevel portid = -1;
125*1708Sstevel
126*1708Sstevel #ifdef DEBUG
127*1708Sstevel if (name != NULL)
128*1708Sstevel printf("name=%s\n", name);
129*1708Sstevel if (type != NULL)
130*1708Sstevel printf("type=%s\n", type);
131*1708Sstevel if (model != NULL)
132*1708Sstevel printf("model=%s\n", model);
133*1708Sstevel printf("portid=%d\n", portid);
134*1708Sstevel #endif
135*1708Sstevel
136*1708Sstevel if (name != NULL) {
137*1708Sstevel if (has_board_num(pnode)) {
138*1708Sstevel add_node(tree, pnode);
139*1708Sstevel board_node = 1;
140*1708Sstevel D_PRINTF("\n---\nnodename = %s [ %s ] \n",
141*1708Sstevel di_node_name(di_node), di_devfs_path(di_node));
142*1708Sstevel D_PRINTF("ADDED BOARD name=%s type=%s model=%s "
143*1708Sstevel "portid =%d\n", name, type, model, portid);
144*1708Sstevel } else if ((strcmp(name, FFB_NAME) == 0) ||
145*1708Sstevel (strcmp(type, "cpu") == 0) ||
146*1708Sstevel
147*1708Sstevel ((strcmp(type, "memory-controller") == 0) &&
148*1708Sstevel (strcmp(name, "ac") != 0)) ||
149*1708Sstevel
150*1708Sstevel ((strcmp(name, "pci") == 0) &&
151*1708Sstevel (strcmp(model, "SUNW,psycho") == 0)) ||
152*1708Sstevel
153*1708Sstevel ((strcmp(name, "pci") == 0) &&
154*1708Sstevel (strcmp(model, "SUNW,sabre") == 0)) ||
155*1708Sstevel
156*1708Sstevel ((strcmp(name, "pci") == 0) && (is_schizo)) ||
157*1708Sstevel
158*1708Sstevel (strcmp(name, "counter-timer") == 0) ||
159*1708Sstevel (strcmp(name, "sbus") == 0)) {
160*1708Sstevel add_node(tree, pnode);
161*1708Sstevel board_node = 1;
162*1708Sstevel D_PRINTF("\n---\nnodename = %s [ %s ] \n",
163*1708Sstevel di_node_name(di_node), di_devfs_path(di_node));
164*1708Sstevel D_PRINTF("ADDED BOARD name=%s type=%s model=%s\n",
165*1708Sstevel name, type, model);
166*1708Sstevel }
167*1708Sstevel } else {
168*1708Sstevel D_PRINTF("node not added: type=%s portid =%d\n", type, portid);
169*1708Sstevel }
170*1708Sstevel
171*1708Sstevel if (curnode = di_child_node(di_node)) {
172*1708Sstevel pnode->child = walk_di_tree(tree, pnode, curnode);
173*1708Sstevel }
174*1708Sstevel
175*1708Sstevel if (curnode = di_sibling_node(di_node)) {
176*1708Sstevel if (board_node) {
177*1708Sstevel return (walk_di_tree(tree, root, curnode));
178*1708Sstevel } else {
179*1708Sstevel pnode->sibling = walk_di_tree(tree, root, curnode);
180*1708Sstevel }
181*1708Sstevel }
182*1708Sstevel
183*1708Sstevel /*
184*1708Sstevel * This check is needed in case the "board node" occurs at the
185*1708Sstevel * end of the sibling chain as opposed to the middle or front.
186*1708Sstevel */
187*1708Sstevel if (board_node)
188*1708Sstevel return (NULL);
189*1708Sstevel
190*1708Sstevel return (pnode);
191*1708Sstevel }
192*1708Sstevel
193*1708Sstevel /*
194*1708Sstevel * Dump all the devinfo properties and then the obp properties for
195*1708Sstevel * the specified devinfo node into the Prom_node structure.
196*1708Sstevel */
197*1708Sstevel static void
dump_di_node(Prom_node * pnode,di_node_t di_node)198*1708Sstevel dump_di_node(Prom_node *pnode, di_node_t di_node)
199*1708Sstevel {
200*1708Sstevel Prop *prop = NULL; /* tail of properties list */
201*1708Sstevel
202*1708Sstevel Prop *temp; /* newly allocated property */
203*1708Sstevel di_prop_t di_prop;
204*1708Sstevel di_prom_prop_t p_prop;
205*1708Sstevel int retval = 0;
206*1708Sstevel int i;
207*1708Sstevel
208*1708Sstevel /* clear out pointers in pnode */
209*1708Sstevel pnode->props = NULL;
210*1708Sstevel
211*1708Sstevel D_PRINTF("\n\n ------- Dumping devinfo properties for node ------\n");
212*1708Sstevel
213*1708Sstevel /*
214*1708Sstevel * get all the devinfo properties first
215*1708Sstevel */
216*1708Sstevel for (di_prop = di_prop_next(di_node, DI_PROP_NIL);
217*1708Sstevel di_prop != DI_PROP_NIL;
218*1708Sstevel di_prop = di_prop_next(di_node, di_prop)) {
219*1708Sstevel
220*1708Sstevel char *di_name;
221*1708Sstevel void *di_data;
222*1708Sstevel int di_ptype;
223*1708Sstevel
224*1708Sstevel di_name = di_prop_name(di_prop);
225*1708Sstevel if (di_name == (char *)NULL)
226*1708Sstevel continue;
227*1708Sstevel
228*1708Sstevel di_ptype = di_prop_type(di_prop);
229*1708Sstevel D_PRINTF("DEVINFO Properties %s: ", di_name);
230*1708Sstevel
231*1708Sstevel switch (di_ptype) {
232*1708Sstevel int *int_val;
233*1708Sstevel char *char_val;
234*1708Sstevel case DI_PROP_TYPE_INT:
235*1708Sstevel retval = di_prop_lookup_ints(DDI_DEV_T_ANY,
236*1708Sstevel di_node, di_name, (int **)&di_data);
237*1708Sstevel if (retval > 0) {
238*1708Sstevel int_val = (int *)di_data;
239*1708Sstevel D_PRINTF("0x%x\n", *int_val);
240*1708Sstevel }
241*1708Sstevel break;
242*1708Sstevel case DI_PROP_TYPE_STRING:
243*1708Sstevel retval = di_prop_lookup_strings(DDI_DEV_T_ANY,
244*1708Sstevel di_node, di_name, (char **)&di_data);
245*1708Sstevel if (retval > 0) {
246*1708Sstevel char_val = (char *)di_data;
247*1708Sstevel D_PRINTF("%s\n", char_val);
248*1708Sstevel }
249*1708Sstevel break;
250*1708Sstevel case DI_PROP_TYPE_BYTE:
251*1708Sstevel retval = di_prop_lookup_bytes(DDI_DEV_T_ANY,
252*1708Sstevel di_node, di_name, (uchar_t **)&di_data);
253*1708Sstevel if (retval > 0) {
254*1708Sstevel char_val = (char *)di_data;
255*1708Sstevel D_PRINTF("%s\n", char_val);
256*1708Sstevel }
257*1708Sstevel break;
258*1708Sstevel case DI_PROP_TYPE_UNKNOWN:
259*1708Sstevel retval = di_prop_lookup_bytes(DDI_DEV_T_ANY,
260*1708Sstevel di_node, di_name, (uchar_t **)&di_data);
261*1708Sstevel if (retval > 0) {
262*1708Sstevel char_val = (char *)di_data;
263*1708Sstevel D_PRINTF("%s\n", char_val);
264*1708Sstevel }
265*1708Sstevel break;
266*1708Sstevel case DI_PROP_TYPE_BOOLEAN:
267*1708Sstevel di_data = NULL;
268*1708Sstevel retval = 1;
269*1708Sstevel break;
270*1708Sstevel default:
271*1708Sstevel D_PRINTF(" Skipping property\n");
272*1708Sstevel retval = -1;
273*1708Sstevel }
274*1708Sstevel
275*1708Sstevel if (retval <= 0)
276*1708Sstevel continue;
277*1708Sstevel
278*1708Sstevel /* allocate space for the property */
279*1708Sstevel if ((temp = (Prop *) malloc(sizeof (Prop))) == NULL) {
280*1708Sstevel perror("malloc");
281*1708Sstevel exit(1);
282*1708Sstevel }
283*1708Sstevel
284*1708Sstevel /*
285*1708Sstevel * Given that we're using libdevinfo rather than OBP,
286*1708Sstevel * the chances are that future accesses to di_name and
287*1708Sstevel * di_data will be via temp->name.val_ptr and
288*1708Sstevel * temp->value.val_ptr respectively. However, this may
289*1708Sstevel * not be the case, so we have to suitably fill in
290*1708Sstevel * temp->name.opp and temp->value.opp.
291*1708Sstevel *
292*1708Sstevel * di_name is char * and non-NULL if we've made it to
293*1708Sstevel * here, so we can simply point
294*1708Sstevel * temp->name.opp.oprom_array to temp->name.val_ptr.
295*1708Sstevel *
296*1708Sstevel * di_data could be NULL, char * or int * at this point.
297*1708Sstevel * If it's non-NULL, a 1st char of '\0' indicates int *.
298*1708Sstevel * We thus set temp->value.opp.oprom_node[] (although
299*1708Sstevel * interest in any element other than 0 is rare, all
300*1708Sstevel * elements must be set to ensure compatibility with
301*1708Sstevel * OBP), and holds_array is set to 0.
302*1708Sstevel *
303*1708Sstevel * If di_data is NULL, or the 1st char is not '\0', we set
304*1708Sstevel * temp->value.opp.oprom_array. If di_ptype is
305*1708Sstevel * DI_PROP_TYPE_BOOLEAN, holds_array is set to 0, else it
306*1708Sstevel * is set to 1.
307*1708Sstevel */
308*1708Sstevel temp->name.val_ptr = (void *)di_name;
309*1708Sstevel temp->name.opp.oprom_array = temp->name.val_ptr;
310*1708Sstevel temp->name.opp.holds_array = 1;
311*1708Sstevel
312*1708Sstevel temp->value.val_ptr = (void *)di_data;
313*1708Sstevel if ((di_data != NULL) && (*((char *)di_data) == '\0')) {
314*1708Sstevel for (i = 0; i < OPROM_NODE_SIZE; i++)
315*1708Sstevel temp->value.opp.oprom_node[i] = *((int *)di_data+i);
316*1708Sstevel
317*1708Sstevel temp->value.opp.holds_array = 0;
318*1708Sstevel } else {
319*1708Sstevel temp->value.opp.oprom_array = temp->value.val_ptr;
320*1708Sstevel if (di_ptype == DI_PROP_TYPE_BOOLEAN)
321*1708Sstevel temp->value.opp.holds_array = 0;
322*1708Sstevel else
323*1708Sstevel temp->value.opp.holds_array = 1;
324*1708Sstevel }
325*1708Sstevel
326*1708Sstevel temp->size = retval;
327*1708Sstevel
328*1708Sstevel /* everything worked so link the property list */
329*1708Sstevel if (pnode->props == NULL)
330*1708Sstevel pnode->props = temp;
331*1708Sstevel else if (prop != NULL)
332*1708Sstevel prop->next = temp;
333*1708Sstevel prop = temp;
334*1708Sstevel prop->next = NULL;
335*1708Sstevel }
336*1708Sstevel
337*1708Sstevel /*
338*1708Sstevel * Then get all the OBP properties.
339*1708Sstevel */
340*1708Sstevel for (p_prop = di_prom_prop_next(ph, di_node, DI_PROM_PROP_NIL);
341*1708Sstevel p_prop != DI_PROM_PROP_NIL;
342*1708Sstevel p_prop = di_prom_prop_next(ph, di_node, p_prop)) {
343*1708Sstevel
344*1708Sstevel char *p_name;
345*1708Sstevel unsigned char *p_data;
346*1708Sstevel
347*1708Sstevel p_name = di_prom_prop_name(p_prop);
348*1708Sstevel if (p_name == (char *)NULL)
349*1708Sstevel retval = -1;
350*1708Sstevel else
351*1708Sstevel retval = di_prom_prop_data(p_prop, &p_data);
352*1708Sstevel
353*1708Sstevel if (retval <= 0)
354*1708Sstevel continue;
355*1708Sstevel
356*1708Sstevel /* allocate space for the property */
357*1708Sstevel if ((temp = (Prop *) malloc(sizeof (Prop))) == NULL) {
358*1708Sstevel perror("malloc");
359*1708Sstevel exit(1);
360*1708Sstevel }
361*1708Sstevel
362*1708Sstevel /*
363*1708Sstevel * As above, p_name is char * and non-NULL if we've made
364*1708Sstevel * it to here, so we can simply point
365*1708Sstevel * temp->name.opp.oprom_array to temp->name.val_ptr.
366*1708Sstevel *
367*1708Sstevel * p_data could be NULL, a character or a number at this
368*1708Sstevel * point. If it's non-NULL, a 1st char of '\0' indicates a
369*1708Sstevel * number, so we set temp->value.opp.oprom_node[] (again
370*1708Sstevel * setting every element to ensure OBP compatibility).
371*1708Sstevel * These assignments create a lint error, hence the LINTED
372*1708Sstevel * comment.
373*1708Sstevel *
374*1708Sstevel * If p_data is NULL, or the 1st char is not '\0', we set
375*1708Sstevel * temp->value.opp.oprom_array.
376*1708Sstevel */
377*1708Sstevel temp->name.val_ptr = (void *)p_name;
378*1708Sstevel temp->name.opp.oprom_array = temp->name.val_ptr;
379*1708Sstevel temp->name.opp.holds_array = 1;
380*1708Sstevel
381*1708Sstevel temp->value.val_ptr = (void *)p_data;
382*1708Sstevel if ((p_data != NULL) && (*p_data == '\0')) {
383*1708Sstevel for (i = 0; i < OPROM_NODE_SIZE; i++)
384*1708Sstevel /* LINTED */
385*1708Sstevel temp->value.opp.oprom_node[i] = *((int *)p_data+i);
386*1708Sstevel
387*1708Sstevel temp->value.opp.holds_array = 0;
388*1708Sstevel } else {
389*1708Sstevel temp->value.opp.oprom_array = temp->value.val_ptr;
390*1708Sstevel temp->value.opp.holds_array = 1;
391*1708Sstevel }
392*1708Sstevel
393*1708Sstevel temp->size = retval;
394*1708Sstevel
395*1708Sstevel /* everything worked so link the property list */
396*1708Sstevel if (pnode->props == NULL) {
397*1708Sstevel pnode->props = temp;
398*1708Sstevel } else if (prop != NULL) {
399*1708Sstevel prop->next = temp;
400*1708Sstevel }
401*1708Sstevel prop = temp;
402*1708Sstevel prop->next = NULL;
403*1708Sstevel }
404*1708Sstevel }
405*1708Sstevel
406*1708Sstevel /*
407*1708Sstevel * Used in place of do_prominfo() when a platform wants to use
408*1708Sstevel * libdevinfo for getting the device tree instead of OBP.
409*1708Sstevel */
410*1708Sstevel int
do_devinfo(int syserrlog,char * pgname,int log_flag,int prt_flag)411*1708Sstevel do_devinfo(int syserrlog, char *pgname, int log_flag, int prt_flag)
412*1708Sstevel {
413*1708Sstevel Sys_tree sys_tree; /* system information */
414*1708Sstevel Prom_node *root_node; /* root node of OBP device tree */
415*1708Sstevel di_node_t di_root_node; /* root of the devinfo tree */
416*1708Sstevel struct system_kstat_data sys_kstat; /* kstats for non-OBP data */
417*1708Sstevel int retval = -1;
418*1708Sstevel
419*1708Sstevel /* set the global flags */
420*1708Sstevel progname = pgname;
421*1708Sstevel logging = log_flag;
422*1708Sstevel print_flag = prt_flag;
423*1708Sstevel
424*1708Sstevel /* set the the system tree fields */
425*1708Sstevel sys_tree.sys_mem = NULL;
426*1708Sstevel sys_tree.boards = NULL;
427*1708Sstevel sys_tree.bd_list = NULL;
428*1708Sstevel sys_tree.board_cnt = 0;
429*1708Sstevel
430*1708Sstevel /*
431*1708Sstevel * create a snapshot of the kernel device tree
432*1708Sstevel * and return a handle to it.
433*1708Sstevel */
434*1708Sstevel if ((di_root_node = di_init("/", DINFOCPYALL)) == DI_NODE_NIL) {
435*1708Sstevel exit(_error("di_init() failed"));
436*1708Sstevel }
437*1708Sstevel
438*1708Sstevel /*
439*1708Sstevel * create a handle to the PROM device tree.
440*1708Sstevel */
441*1708Sstevel if ((ph = di_prom_init()) == NULL) {
442*1708Sstevel exit(_error("di_prom_init() failed"));
443*1708Sstevel }
444*1708Sstevel
445*1708Sstevel /*
446*1708Sstevel * walk the devinfo tree and build up a list of all
447*1708Sstevel * nodes and properties.
448*1708Sstevel */
449*1708Sstevel root_node = walk_di_tree(&sys_tree, NULL, di_root_node);
450*1708Sstevel
451*1708Sstevel /* resolve the board types now */
452*1708Sstevel resolve_board_types(&sys_tree);
453*1708Sstevel
454*1708Sstevel read_sun4u_kstats(&sys_tree, &sys_kstat);
455*1708Sstevel retval = display(&sys_tree, root_node, &sys_kstat, syserrlog);
456*1708Sstevel
457*1708Sstevel di_fini(di_root_node);
458*1708Sstevel di_prom_fini(ph);
459*1708Sstevel return (retval);
460*1708Sstevel }
461*1708Sstevel
462*1708Sstevel /*
463*1708Sstevel * check to see if the name shows up in the compatible array
464*1708Sstevel */
465*1708Sstevel static int
match_compatible_name(char * compatible_array,int n_names,char * name)466*1708Sstevel match_compatible_name(char *compatible_array, int n_names, char *name)
467*1708Sstevel {
468*1708Sstevel int i, ret = 0;
469*1708Sstevel
470*1708Sstevel /* parse the compatible list */
471*1708Sstevel for (i = 0; i < n_names; i++) {
472*1708Sstevel if (strcmp(compatible_array, name) == 0) {
473*1708Sstevel ret = 1;
474*1708Sstevel break;
475*1708Sstevel }
476*1708Sstevel compatible_array += strlen(compatible_array) + 1;
477*1708Sstevel }
478*1708Sstevel return (ret);
479*1708Sstevel }
480