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 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <fm/topo_mod.h>
28 #include <fm/topo_hc.h>
29 #include <libdevinfo.h>
30 #include <strings.h>
31 #include <pcibus.h>
32 #include <hostbridge.h>
33 #include <did.h>
34 #include <util.h>
35 
36 static int
hb_process(topo_mod_t * mod,tnode_t * ptn,topo_instance_t hbi,di_node_t bn)37 hb_process(topo_mod_t *mod, tnode_t *ptn, topo_instance_t hbi, di_node_t bn)
38 {
39 	tnode_t *hb;
40 	did_t *hbdid;
41 
42 	if ((hbdid = did_create(mod, bn, 0, hbi, NO_RC, TRUST_BDF)) == NULL)
43 		return (-1);
44 	if ((hb = pcihostbridge_declare(mod, ptn, bn, hbi)) == NULL)
45 		return (-1);
46 	if (topo_mod_enumerate(mod,
47 	    hb, PCI_BUS, PCI_BUS, 0, MAX_HB_BUSES, (void *)hbdid) < 0) {
48 		topo_node_unbind(hb);
49 		return (-1);
50 	}
51 
52 	return (0);
53 }
54 
55 static int
rc_process(topo_mod_t * mod,tnode_t * ptn,topo_instance_t hbi,di_node_t bn)56 rc_process(topo_mod_t *mod, tnode_t *ptn, topo_instance_t hbi, di_node_t bn)
57 {
58 	tnode_t *hb;
59 	tnode_t *rc;
60 	did_t *hbdid;
61 
62 	if ((hbdid = did_create(mod, bn, 0, hbi, hbi, TRUST_BDF)) == NULL)
63 		return (-1);
64 	if ((hb = pciexhostbridge_declare(mod, ptn, bn, hbi)) == NULL)
65 		return (-1);
66 	if ((rc = pciexrc_declare(mod, hb, bn, hbi)) == NULL)
67 		return (-1);
68 	if (topo_mod_enumerate(mod,
69 	    rc, PCI_BUS, PCIEX_BUS, 0, MAX_HB_BUSES, (void *)hbdid) < 0) {
70 		topo_node_unbind(hb);
71 		topo_node_unbind(rc);
72 		return (-1);
73 	}
74 
75 	return (0);
76 }
77 
78 
79 int
pci_hostbridges_find(topo_mod_t * mod,tnode_t * ptn)80 pci_hostbridges_find(topo_mod_t *mod, tnode_t *ptn)
81 {
82 	di_node_t devtree;
83 	di_node_t pnode, cnode;
84 	int hbcnt = 0;
85 
86 	/* Scan for buses, top-level devinfo nodes with the right driver */
87 	devtree = topo_mod_devinfo(mod);
88 	if (devtree == DI_NODE_NIL) {
89 		topo_mod_dprintf(mod, "devinfo init failed.");
90 		topo_node_range_destroy(ptn, HOSTBRIDGE);
91 		return (0);
92 	}
93 
94 	pnode = di_drv_first_node(PCI, devtree);
95 	while (pnode != DI_NODE_NIL) {
96 		/*
97 		 * We've seen cases where certain phantom PCI hostbridges have
98 		 * appeared on systems. If we encounter a host bridge without a
99 		 * bus address assigned to it, then we should skip processing it
100 		 * here as that indicates that it generally doesn't have any
101 		 * devices under it and we'll otherwise blow up in devinfo.
102 		 */
103 		if (di_bus_addr(pnode) == NULL) {
104 			pnode = di_drv_next_node(pnode);
105 			continue;
106 		}
107 
108 		if (hb_process(mod, ptn, hbcnt, pnode) < 0) {
109 			if (hbcnt == 0)
110 				topo_node_range_destroy(ptn, HOSTBRIDGE);
111 			return (topo_mod_seterrno(mod, EMOD_PARTIAL_ENUM));
112 		}
113 		hbcnt++;
114 		pnode = di_drv_next_node(pnode);
115 	}
116 
117 	pnode = di_drv_first_node(NPE, devtree);
118 	while (pnode != DI_NODE_NIL) {
119 		for (cnode = di_child_node(pnode); cnode != DI_NODE_NIL;
120 		    cnode = di_sibling_node(cnode)) {
121 			if (di_driver_name(cnode) == NULL)
122 				continue;
123 			if (strcmp(di_driver_name(cnode), PCI_PCI) == 0) {
124 				if (hb_process(mod, ptn, hbcnt, cnode) < 0) {
125 					if (hbcnt == 0)
126 						topo_node_range_destroy(ptn,
127 						    HOSTBRIDGE);
128 					return (topo_mod_seterrno(mod,
129 					    EMOD_PARTIAL_ENUM));
130 				}
131 				hbcnt++;
132 			}
133 			if (strcmp(di_driver_name(cnode), PCIEB) == 0) {
134 				if (rc_process(mod, ptn, hbcnt, cnode) < 0) {
135 					if (hbcnt == 0)
136 						topo_node_range_destroy(ptn,
137 						    HOSTBRIDGE);
138 					return (topo_mod_seterrno(mod,
139 					    EMOD_PARTIAL_ENUM));
140 				}
141 				hbcnt++;
142 			}
143 		}
144 		pnode = di_drv_next_node(pnode);
145 	}
146 	return (0);
147 }
148 
149 /*ARGSUSED*/
150 int
platform_hb_enum(topo_mod_t * mod,tnode_t * parent,const char * name,topo_instance_t imin,topo_instance_t imax)151 platform_hb_enum(topo_mod_t *mod, tnode_t *parent, const char *name,
152     topo_instance_t imin, topo_instance_t imax)
153 {
154 	return (pci_hostbridges_find(mod, parent));
155 }
156 
157 /*ARGSUSED*/
158 int
platform_hb_label(topo_mod_t * mod,tnode_t * node,nvlist_t * in,nvlist_t ** out)159 platform_hb_label(topo_mod_t *mod, tnode_t *node, nvlist_t *in, nvlist_t **out)
160 {
161 	return (labelmethod_inherit(mod, node, in, out));
162 }
163