xref: /illumos-gate/usr/src/lib/fm/topo/modules/common/pcibus/util.c (revision 8c69cc8fbe729fa7b091e901c4b50508ccc6bb33)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <stdlib.h>
28 #include <string.h>
29 #include <strings.h>
30 #include <sys/fm/protocol.h>
31 #include <fm/topo_mod.h>
32 #include <fm/topo_hc.h>
33 #include <fm/libtopo.h>
34 
35 int
36 child_range_add(topo_mod_t *mp, tnode_t *tn, const char *cnm,
37     topo_instance_t imin, topo_instance_t imax)
38 {
39 	int e;
40 
41 	e = topo_node_range_create(mp, tn, cnm, imin, imax);
42 	if (e != 0) {
43 		topo_mod_dprintf(mp, "add child range (%s) failed: %s\n",
44 		    cnm, topo_strerror(topo_mod_errno(mp)));
45 		return (-1);
46 	}
47 	return (0);
48 }
49 
50 ulong_t
51 fm_strtonum(topo_mod_t *mp, char *str, int *err)
52 {
53 	ulong_t r;
54 	char *e;
55 
56 	r = strtoul(str, &e, 16);
57 	if (e == str) {
58 		topo_mod_dprintf(mp,
59 		    "Trouble converting %s to a number!\n", str);
60 		*err = -1;
61 		return (0);
62 	}
63 	*err = 0;
64 	return (r);
65 }
66 
67 static int
68 get_pci_vpd_sn_pn(topo_mod_t *mp, di_node_t dn, char **serial, char **part)
69 {
70 	char *s = NULL, *p = NULL;
71 	di_prom_handle_t promtree = DI_PROM_HANDLE_NIL;
72 
73 	if ((promtree = topo_mod_prominfo(mp)) == DI_PROM_HANDLE_NIL) {
74 		topo_mod_dprintf(mp,
75 		    "get vpd data: di_prom_handle_init failed.\n");
76 		return (-1);
77 	}
78 
79 	/* Get Serial Number and Part Number */
80 	if ((di_prom_prop_lookup_bytes(promtree, dn, "vpd-serial-number",
81 	    (uchar_t **)&s) > 0) && (s != NULL))
82 		*serial = topo_mod_strdup(mp, s);
83 
84 	if ((di_prom_prop_lookup_bytes(promtree, dn, "vpd-part-number",
85 	    (uchar_t **)&p) > 0) && (p != NULL))
86 		*part = topo_mod_strdup(mp, p);
87 
88 	return (0);
89 }
90 
91 tnode_t *
92 tnode_create(topo_mod_t *mp, tnode_t *parent,
93     const char *name, topo_instance_t i, void *priv)
94 {
95 	nvlist_t *fmri;
96 	tnode_t *ntn;
97 	nvlist_t *auth;
98 	char *serial = NULL, *part = NULL;
99 
100 	auth = topo_mod_auth(mp, parent);
101 	/*
102 	 * Get PCI/PCIEX Device Serial Number and Part Number
103 	 * from PCI VPD
104 	 */
105 	if ((strcmp(name, PCI_DEVICE) == 0) ||
106 	    (strcmp(name, PCIEX_DEVICE) == 0))
107 		(void) get_pci_vpd_sn_pn(mp, priv, &serial, &part);
108 
109 	fmri = topo_mod_hcfmri(mp, parent, FM_HC_SCHEME_VERSION, name, i, NULL,
110 	    auth, part, NULL, serial);
111 	nvlist_free(auth);
112 	topo_mod_strfree(mp, serial);
113 	topo_mod_strfree(mp, part);
114 
115 	if (fmri == NULL) {
116 		topo_mod_dprintf(mp,
117 		    "Unable to make nvlist for %s bind.\n", name);
118 		return (NULL);
119 	}
120 
121 	ntn = topo_node_bind(mp, parent, name, i, fmri);
122 	if (ntn == NULL) {
123 		topo_mod_dprintf(mp,
124 		    "topo_node_bind (%s%d/%s%d) failed: %s\n",
125 		    topo_node_name(parent), topo_node_instance(parent),
126 		    name, i,
127 		    topo_strerror(topo_mod_errno(mp)));
128 		nvlist_free(fmri);
129 		return (NULL);
130 	}
131 	nvlist_free(fmri);
132 	topo_node_setspecific(ntn, priv);
133 
134 	return (ntn);
135 }
136 
137 /*ARGSUSED*/
138 int
139 labelmethod_inherit(topo_mod_t *mp, tnode_t *tn, nvlist_t *in, nvlist_t **out)
140 {
141 	int err;
142 
143 	/*
144 	 * Ignore the input and output nvlists and directly set the
145 	 * label as inheritance from the parent
146 	 */
147 	*out = NULL;
148 	if (topo_node_label_set(tn, NULL, &err) < 0) {
149 		if (err != ETOPO_PROP_NOENT)
150 			return (topo_mod_seterrno(mp, err));
151 	}
152 	return (0);
153 }
154