dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / lib / libprtdiag / common / pdevinfo_funcs.c
blob2d4c36035af56c037072a3dc9f56012a99fad9ac
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
20 * CDDL HEADER END
23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <fcntl.h>
33 #include <stdarg.h>
34 #include <errno.h>
35 #include <unistd.h>
36 #include <sys/utsname.h>
37 #include <sys/openpromio.h>
38 #include <libintl.h>
39 #include "pdevinfo.h"
40 #include "display.h"
41 #include "pdevinfo_sun4u.h"
44 * For machines that support the openprom, fetch and print the list
45 * of devices that the kernel has fetched from the prom or conjured up.
50 static int prom_fd;
51 extern char *progname;
52 extern char *promdev;
53 extern void getppdata();
54 extern void printppdata();
57 * Define DPRINT for run-time debugging printf's...
58 * #define DPRINT 1
61 #ifdef DPRINT
62 static char vdebug_flag = 1;
63 #define dprintf if (vdebug_flag) printf
64 static void dprint_dev_info(caddr_t, dev_info_t *);
65 #endif /* DPRINT */
67 #if !defined(TEXT_DOMAIN)
68 #define TEXT_DOMAIN "SYS_TEST"
69 #endif
71 /*VARARGS1*/
72 int
73 _error(char *fmt, ...)
75 int saved_errno;
76 va_list ap;
77 extern int errno;
78 saved_errno = errno;
80 if (progname)
81 (void) fprintf(stderr, "%s: ", progname);
83 va_start(ap, fmt);
85 (void) vfprintf(stderr, fmt, ap);
87 va_end(ap);
89 (void) fprintf(stderr, ": ");
90 errno = saved_errno;
91 perror("");
93 return (2);
96 int
97 is_openprom(void)
99 Oppbuf oppbuf;
100 register struct openpromio *opp = &(oppbuf.opp);
101 register unsigned int i;
103 opp->oprom_size = MAXVALSIZE;
104 if (ioctl(prom_fd, OPROMGETCONS, opp) < 0)
105 exit(_error("OPROMGETCONS"));
107 i = (unsigned int)((unsigned char)opp->oprom_array[0]);
108 return ((i & OPROMCONS_OPENPROM) == OPROMCONS_OPENPROM);
112 * Read all properties and values from nodes.
113 * Copy the properties read into the prom_node passsed in.
115 void
116 dump_node(Prom_node *node)
118 Oppbuf oppbuf;
119 register struct openpromio *opp = &oppbuf.opp;
120 Prop *prop = NULL; /* tail of properties list */
121 StaticProp *temp;
123 /* clear out pointers in pnode */
124 node->props = NULL;
126 /* get first prop by asking for null string */
127 (void) memset(oppbuf.buf, 0, BUFSIZE);
129 /* allocate space for the property */
130 if ((temp = malloc(sizeof (StaticProp))) == NULL) {
131 perror("malloc");
132 exit(1);
135 opp->oprom_size = MAXPROPSIZE;
136 while (opp->oprom_size != 0) {
137 Prop *new;
138 int i;
139 char *tempp, *newp;
142 * get property
144 opp->oprom_size = MAXPROPSIZE;
146 if (ioctl(prom_fd, OPROMNXTPROP, opp) < 0)
147 exit(_error("OPROMNXTPROP"));
149 if (opp->oprom_size != 0) {
150 temp->name.opp.oprom_size = opp->oprom_size;
151 (void) strcpy(temp->name.opp.oprom_array,
152 opp->oprom_array);
154 (void) strcpy(temp->value.opp.oprom_array,
155 temp->name.opp.oprom_array);
156 getpropval(&temp->value.opp);
157 temp->size = temp->value.opp.oprom_size;
159 /* Now copy over temp's data to new. */
160 if ((new = malloc(sizeof (Prop))) == NULL) {
161 perror("malloc");
162 exit(1);
166 * First copy over temp->name's data. The
167 * temp->name.opp.opio_u union always contains char[]
168 * (as opposed to an int or int []).
170 new->name.opp.oprom_size = temp->name.opp.oprom_size;
172 if ((new->name.opp.oprom_array =
173 malloc(new->name.opp.oprom_size)) == NULL) {
174 perror("malloc");
175 exit(1);
177 (void) strcpy(new->name.opp.oprom_array,
178 temp->name.opp.oprom_array);
180 new->name.opp.holds_array = 1;
183 * Then copy over temp->value's data.
184 * temp->value.opp.opio_u could contain char[], int or
185 * int []. If *(temp->value.opp.oprom_array) is '\0',
186 * this indicates int or int []. int is the norm, but
187 * to be safe we assume int [] and copy over
188 * OPROM_NODE_SIZE int elements.
190 new->value.opp.oprom_size = temp->value.opp.oprom_size;
192 if (*(temp->value.opp.oprom_array) == '\0') {
193 for (i = 0; i < OPROM_NODE_SIZE; i++)
194 new->value.opp.oprom_node[i] =
195 *(&temp->value.opp.oprom_node+i);
197 new->value.opp.holds_array = 0;
198 } else {
199 if ((new->value.opp.oprom_array =
200 malloc(new->value.opp.oprom_size))
201 == NULL) {
202 perror("malloc");
203 exit(1);
207 * temp->value.opp.oprom_array can contain one
208 * or more embedded NULLs. These trip-up the
209 * standard string copying functions, so we do
210 * the copy by hand. temp->value.opp.oprom_array
211 * will be NULL-terminated. oprom_size includes
212 * this terminating NULL.
214 newp = new->value.opp.oprom_array;
215 tempp = temp->value.opp.oprom_array;
216 for (i = new->value.opp.oprom_size; i > 0; i--)
217 *newp++ = *tempp++;
219 new->value.opp.holds_array = 1;
222 new->size = temp->size;
224 /* everything worked so link the property list */
225 if (node->props == NULL)
226 node->props = new;
227 else if (prop != NULL)
228 prop->next = new;
229 prop = new;
230 prop->next = NULL;
233 free(temp);
237 promopen(int oflag)
239 /*CONSTCOND*/
240 while (1) {
241 if ((prom_fd = open(promdev, oflag)) < 0) {
242 if (errno == EAGAIN) {
243 (void) sleep(5);
244 continue;
246 if (errno == ENXIO)
247 return (-1);
248 exit(_error(dgettext(TEXT_DOMAIN, "cannot open %s"),
249 promdev));
250 } else
251 return (0);
253 /*NOTREACHED*/
256 void
257 promclose(void)
259 if (close(prom_fd) < 0)
260 exit(_error(dgettext(TEXT_DOMAIN, "close error on %s"),
261 promdev));
265 * Read the value of the property from the PROM device tree
267 void
268 getpropval(struct openpromio *opp)
270 opp->oprom_size = MAXVALSIZE;
272 if (ioctl(prom_fd, OPROMGETPROP, opp) < 0)
273 exit(_error("OPROMGETPROP"));
277 next(int id)
279 Oppbuf oppbuf;
280 register struct openpromio *opp = &(oppbuf.opp);
281 /* LINTED */
282 int *ip = (int *)(opp->oprom_array);
284 (void) memset(oppbuf.buf, 0, BUFSIZE);
286 opp->oprom_size = MAXVALSIZE;
287 *ip = id;
288 if (ioctl(prom_fd, OPROMNEXT, opp) < 0)
289 return (_error("OPROMNEXT"));
290 /* LINTED */
291 return (*(int *)opp->oprom_array);
295 child(int id)
297 Oppbuf oppbuf;
298 register struct openpromio *opp = &(oppbuf.opp);
299 /* LINTED */
300 int *ip = (int *)(opp->oprom_array);
302 (void) memset(oppbuf.buf, 0, BUFSIZE);
303 opp->oprom_size = MAXVALSIZE;
304 *ip = id;
305 if (ioctl(prom_fd, OPROMCHILD, opp) < 0)
306 return (_error("OPROMCHILD"));
307 /* LINTED */
308 return (*(int *)opp->oprom_array);
312 * Check if the Prom node passed in contains a property called
313 * "board#".
316 has_board_num(Prom_node *node)
318 Prop *prop = node->props;
321 * walk thru all properties in this PROM node and look for
322 * board# prop
324 while (prop != NULL) {
325 if (strcmp(prop->name.opp.oprom_array, "board#") == 0)
326 return (1);
328 prop = prop->next;
331 return (0);
332 } /* end of has_board_num() */
335 * Retrieve the value of the board number property from this Prom
336 * node. It has the type of int.
339 get_board_num(Prom_node *node)
341 Prop *prop = node->props;
344 * walk thru all properties in this PROM node and look for
345 * board# prop
347 while (prop != NULL) {
348 if (strcmp(prop->name.opp.oprom_array, "board#") == 0)
349 return (prop->value.opp.oprom_node[0]);
351 prop = prop->next;
354 return (-1);
355 } /* end of get_board_num() */
358 * Find the requested board struct in the system device tree.
360 Board_node *
361 find_board(Sys_tree *root, int board)
363 Board_node *bnode = root->bd_list;
365 while ((bnode != NULL) && (board != bnode->board_num))
366 bnode = bnode->next;
368 return (bnode);
369 } /* end of find_board() */
372 * Add a board to the system list in order. Initialize all pointer
373 * fields to NULL.
375 Board_node *
376 insert_board(Sys_tree *root, int board)
378 Board_node *bnode;
379 Board_node *temp = root->bd_list;
381 if ((bnode = (Board_node *) malloc(sizeof (Board_node))) == NULL) {
382 perror("malloc");
383 exit(1);
385 bnode->nodes = NULL;
386 bnode->next = NULL;
387 bnode->board_num = board;
389 if (temp == NULL)
390 root->bd_list = bnode;
391 else if (temp->board_num > board) {
392 bnode->next = temp;
393 root->bd_list = bnode;
394 } else {
395 while ((temp->next != NULL) && (board > temp->next->board_num))
396 temp = temp->next;
397 bnode->next = temp->next;
398 temp->next = bnode;
400 root->board_cnt++;
402 return (bnode);
403 } /* end of insert_board() */
406 * This function searches through the properties of the node passed in
407 * and returns a pointer to the value of the name property.
409 char *
410 get_node_name(Prom_node *pnode)
412 Prop *prop;
414 if (pnode == NULL) {
415 return (NULL);
418 prop = pnode->props;
419 while (prop != NULL) {
420 if (strcmp("name", prop->name.opp.oprom_array) == 0)
421 return (prop->value.opp.oprom_array);
422 prop = prop->next;
424 return (NULL);
425 } /* end of get_node_name() */
428 * This function searches through the properties of the node passed in
429 * and returns a pointer to the value of the name property.
431 char *
432 get_node_type(Prom_node *pnode)
434 Prop *prop;
436 if (pnode == NULL) {
437 return (NULL);
440 prop = pnode->props;
441 while (prop != NULL) {
442 if (strcmp("device_type", prop->name.opp.oprom_array) == 0)
443 return (prop->value.opp.oprom_array);
444 prop = prop->next;
446 return (NULL);
447 } /* end of get_node_type() */
450 * Do a depth-first walk of a device tree and
451 * return the first node with the name matching.
454 Prom_node *
455 dev_find_node(Prom_node *root, char *name)
457 Prom_node *node;
459 node = dev_find_node_by_type(root, "name", name);
461 return (node);
464 Prom_node *
465 dev_next_node(Prom_node *root, char *name)
467 Prom_node *node;
469 node = dev_next_node_by_type(root, "name", name);
471 return (node);
475 * Search for and return a node of the required type. If no node is found,
476 * then return NULL.
478 Prom_node *
479 dev_find_type(Prom_node *root, char *type)
481 Prom_node *node;
483 node = dev_find_node_by_type(root, "device_type", type);
485 return (node); /* not found */
489 * Start from the current node and return the next node besides the
490 * current one which has the requested type property.
492 Prom_node *
493 dev_next_type(Prom_node *root, char *type)
495 Prom_node *node;
497 node = dev_next_node_by_type(root, "device_type", type);
499 return (node); /* not found */
503 * Search a device tree and return the first failed node that is found.
504 * (has a 'status' property)
506 Prom_node *
507 find_failed_node(Prom_node * root)
509 Prom_node *pnode;
511 if (root == NULL)
512 return (NULL);
514 if (node_failed(root)) {
515 return (root);
518 /* search the child */
519 if ((pnode = find_failed_node(root->child)) != NULL)
520 return (pnode);
522 /* search the siblings */
523 if ((pnode = find_failed_node(root->sibling)) != NULL)
524 return (pnode);
526 return (NULL);
527 } /* end of find_failed_node() */
530 * Start from the current node and return the next node besides
531 * the current one which is failed. (has a 'status' property)
533 Prom_node *
534 next_failed_node(Prom_node * root)
536 Prom_node *pnode;
537 Prom_node *parent;
539 if (root == NULL)
540 return (NULL);
542 /* search the child */
543 if ((pnode = find_failed_node(root->child)) != NULL) {
544 return (pnode);
547 /* search the siblings */
548 if ((pnode = find_failed_node(root->sibling)) != NULL) {
549 return (pnode);
552 /* backtracking the search up through parents' siblings */
553 parent = root->parent;
554 while (parent != NULL) {
555 if ((pnode = find_failed_node(parent->sibling)) != NULL)
556 return (pnode);
557 else
558 parent = parent->parent;
561 return (NULL);
562 } /* end of find_failed_node() */
565 * node_failed
567 * This function determines if the current Prom node is failed. This
568 * is defined by having a status property containing the token 'fail'.
571 node_failed(Prom_node *node)
573 return (node_status(node, "fail"));
577 node_status(Prom_node *node, char *status)
579 void *value;
581 if (status == NULL)
582 return (0);
584 /* search the local node */
585 if ((value = get_prop_val(find_prop(node, "status"))) != NULL) {
586 if ((value != NULL) && strstr((char *)value, status))
587 return (1);
589 return (0);
593 * Get a property's value. Must be void * since the property can
594 * be any data type. Caller must know the *PROPER* way to use this
595 * data.
597 void *
598 get_prop_val(Prop *prop)
600 if (prop == NULL)
601 return (NULL);
603 if (prop->value.opp.holds_array)
604 return ((void *)(prop->value.opp.oprom_array));
605 else
606 return ((void *)(&prop->value.opp.oprom_node[0]));
607 } /* end of get_prop_val() */
610 * Search a Prom node and retrieve the property with the correct
611 * name.
613 Prop *
614 find_prop(Prom_node *pnode, char *name)
616 Prop *prop;
618 if (pnode == NULL) {
619 return (NULL);
622 if (pnode->props == NULL) {
623 (void) printf("%s", dgettext(TEXT_DOMAIN, "Prom node has "
624 "no properties\n"));
625 return (NULL);
628 prop = pnode->props;
629 while ((prop != NULL) && (strcmp(prop->name.opp.oprom_array, name)))
630 prop = prop->next;
632 return (prop);
636 * This function adds a board node to the board structure where that
637 * that node's physical component lives.
639 void
640 add_node(Sys_tree *root, Prom_node *pnode)
642 int board;
643 Board_node *bnode;
644 Prom_node *p;
646 /* add this node to the Board list of the appropriate board */
647 if ((board = get_board_num(pnode)) == -1) {
648 /* board is 0 if not on Sunfire */
649 board = 0;
652 /* find the node with the same board number */
653 if ((bnode = find_board(root, board)) == NULL) {
654 bnode = insert_board(root, board);
655 bnode->board_type = UNKNOWN_BOARD;
658 /* now attach this prom node to the board list */
659 /* Insert this node at the end of the list */
660 pnode->sibling = NULL;
661 if (bnode->nodes == NULL)
662 bnode->nodes = pnode;
663 else {
664 p = bnode->nodes;
665 while (p->sibling != NULL)
666 p = p->sibling;
667 p->sibling = pnode;
673 * Find the device on the current board with the requested device ID
674 * and name. If this rountine is passed a NULL pointer, it simply returns
675 * NULL.
677 Prom_node *
678 find_device(Board_node *board, int id, char *name)
680 Prom_node *pnode;
681 int mask;
683 /* find the first cpu node */
684 pnode = dev_find_node(board->nodes, name);
686 mask = 0x1F;
687 while (pnode != NULL) {
688 if ((get_id(pnode) & mask) == id)
689 return (pnode);
691 pnode = dev_next_node(pnode, name);
693 return (NULL);
696 Prom_node *
697 dev_find_node_by_type(Prom_node *root, char *type, char *property)
699 Prom_node *node;
700 char *type_prop;
702 if (root == NULL || property == NULL)
703 return (NULL);
705 type_prop = (char *)get_prop_val(find_prop(root, type));
707 if (type_prop != NULL) {
708 if (strcmp(type_prop, property) == 0) {
709 return (root);
713 /* look at your children first */
714 if ((node = dev_find_node_by_type(root->child, type,
715 property)) != NULL)
716 return (node);
718 /* now look at your siblings */
719 if ((node = dev_find_node_by_type(root->sibling, type,
720 property)) != NULL)
721 return (node);
723 return (NULL); /* not found */
726 Prom_node *
727 dev_next_node_by_type(Prom_node *root, char *type, char *property)
729 Prom_node *node;
731 if (root == NULL || property == NULL)
732 return (NULL);
734 /* look at your children first */
735 if ((node = dev_find_node_by_type(root->child, type,
736 property)) != NULL)
737 return (node);
739 /* now look at your siblings */
740 if ((node = dev_find_node_by_type(root->sibling, type,
741 property)) != NULL)
742 return (node);
744 /* now look at papa's siblings */
745 if ((node = dev_find_node_by_type(root->parent->sibling,
746 type, property)) != NULL)
747 return (node);
749 return (NULL); /* not found */
753 * Do a depth-first walk of a device tree and
754 * return the first node with the matching compatible.
756 Prom_node *
757 dev_find_node_by_compatible(Prom_node *root, char *compatible)
759 Prom_node *node;
760 Prop *prop;
761 char *compatible_array;
762 int size, nbytes;
764 if (root == NULL || compatible == NULL)
765 return (NULL);
767 if ((prop = find_prop(root, "compatible")) != NULL &&
768 (compatible_array = (char *)get_prop_val(prop)) != NULL) {
770 * The Prop structure returned by find_prop() is supposed
771 * to contain an indication of how big the value of the
772 * compatible property is. Since it is an array of strings
773 * this is our only means of determining just how many
774 * strings might be in this property. However, this size
775 * is often left as zero even though there is at least one
776 * string present. When this is the case, all we can do
777 * is examine the first string in the compatible property.
780 for (size = prop->size; size >= 0; size -= nbytes) {
781 if (strcmp(compatible_array, compatible) == 0)
782 return (root); /* found a match */
784 nbytes = strlen(compatible_array) + 1;
785 compatible_array += nbytes;
789 node = dev_find_node_by_compatible(root->child, compatible);
790 if (node != NULL)
791 return (node);
794 * Note the very deliberate use of tail recursion here. A good
795 * compiler (such as Sun's) will recognize this and generate code
796 * that does not allocate another stack frame. Instead, it will
797 * overlay the existing stack frame with the new one, the only change
798 * having been to replace the original root with its sibling.
799 * This has the potential to create some confusion for anyone
800 * trying to debug this code from a core dump, since the stack
801 * trace will not reveal recursion on siblings, only on children.
804 return (dev_find_node_by_compatible(root->sibling, compatible));
808 * Start from the current node and return the next node besides
809 * the current one which has the requested compatible property.
811 Prom_node *
812 dev_next_node_by_compatible(Prom_node *root, char *compatible)
814 Prom_node *node;
816 if (root == NULL || compatible == NULL)
817 return (NULL);
819 node = dev_find_node_by_compatible(root->child, compatible);
820 if (node != NULL)
821 return (node);
824 * More tail recursion. Even though it is a different function,
825 * this will overlay the current stack frame. Caveat exterminator.
828 node = dev_find_node_by_compatible(root->sibling, compatible);
829 if (node != NULL)
830 return (node);
832 return (dev_find_node_by_compatible(root->parent->sibling, compatible));