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 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <fm/topo_mod.h>
30 #include <fm/topo_hc.h>
31 #include <libdevinfo.h>
32 #include <strings.h>
33 #include <pcibus.h>
34 #include <hostbridge.h>
35 #include <did.h>
36 #include <util.h>
37 
38 static int
39 hb_process(topo_mod_t *mod, tnode_t *ptn, topo_instance_t hbi, di_node_t bn)
40 {
41 	tnode_t *hb;
42 	did_t *hbdid;
43 
44 	if ((hbdid = did_create(mod, bn, 0, hbi, NO_RC, TRUST_BDF)) == NULL)
45 		return (-1);
46 	if ((hb = pcihostbridge_declare(mod, ptn, bn, hbi)) == NULL)
47 		return (-1);
48 	if (topo_mod_enumerate(mod,
49 	    hb, PCI_BUS, PCI_BUS, 0, MAX_HB_BUSES, (void *)hbdid) < 0) {
50 		topo_node_unbind(hb);
51 		return (-1);
52 	}
53 
54 	return (0);
55 }
56 
57 static int
58 rc_process(topo_mod_t *mod, tnode_t *ptn, topo_instance_t hbi, di_node_t bn)
59 {
60 	tnode_t *hb;
61 	tnode_t *rc;
62 	did_t *hbdid;
63 
64 	if ((hbdid = did_create(mod, bn, 0, hbi, hbi, TRUST_BDF)) == NULL)
65 		return (-1);
66 	if ((hb = pciexhostbridge_declare(mod, ptn, bn, hbi)) == NULL)
67 		return (-1);
68 	if ((rc = pciexrc_declare(mod, hb, bn, hbi)) == NULL)
69 		return (-1);
70 	if (topo_mod_enumerate(mod,
71 	    rc, PCI_BUS, PCIEX_BUS, 0, MAX_HB_BUSES, (void *)hbdid) < 0) {
72 		topo_node_unbind(hb);
73 		topo_node_unbind(rc);
74 		return (-1);
75 	}
76 
77 	return (0);
78 }
79 
80 
81 int
82 pci_hostbridges_find(topo_mod_t *mod, tnode_t *ptn)
83 {
84 	di_node_t devtree;
85 	di_node_t pnode, cnode;
86 	int hbcnt = 0;
87 
88 	/* Scan for buses, top-level devinfo nodes with the right driver */
89 	devtree = topo_mod_devinfo(mod);
90 	if (devtree == DI_NODE_NIL) {
91 		topo_mod_dprintf(mod, "devinfo init failed.");
92 		topo_node_range_destroy(ptn, HOSTBRIDGE);
93 		return (0);
94 	}
95 
96 	pnode = di_drv_first_node(PCI, devtree);
97 	while (pnode != DI_NODE_NIL) {
98 		if (hb_process(mod, ptn, hbcnt, pnode) < 0) {
99 			if (hbcnt == 0)
100 				topo_node_range_destroy(ptn, HOSTBRIDGE);
101 			return (topo_mod_seterrno(mod, EMOD_PARTIAL_ENUM));
102 		}
103 		hbcnt++;
104 		pnode = di_drv_next_node(pnode);
105 	}
106 
107 	pnode = di_drv_first_node(NPE, devtree);
108 	while (pnode != DI_NODE_NIL) {
109 		for (cnode = di_child_node(pnode); cnode != DI_NODE_NIL;
110 		    cnode = di_sibling_node(cnode)) {
111 			if (di_driver_name(cnode) == NULL)
112 				continue;
113 			if (strcmp(di_driver_name(cnode), PCI_PCI) == 0) {
114 				if (hb_process(mod, ptn, hbcnt, cnode) < 0) {
115 					if (hbcnt == 0)
116 						topo_node_range_destroy(ptn,
117 						    HOSTBRIDGE);
118 					return (topo_mod_seterrno(mod,
119 					    EMOD_PARTIAL_ENUM));
120 				}
121 				hbcnt++;
122 			}
123 			if (strcmp(di_driver_name(cnode), PCIE_PCI) == 0) {
124 				if (rc_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 		}
134 		pnode = di_drv_next_node(pnode);
135 	}
136 	return (0);
137 }
138 
139 /*ARGSUSED*/
140 int
141 platform_hb_enum(topo_mod_t *mod, tnode_t *parent, const char *name,
142     topo_instance_t imin, topo_instance_t imax)
143 {
144 	return (pci_hostbridges_find(mod, parent));
145 }
146 
147 /*ARGSUSED*/
148 int
149 platform_hb_label(topo_mod_t *mod, tnode_t *node, nvlist_t *in, nvlist_t **out)
150 {
151 	return (labelmethod_inherit(mod, node, in, out));
152 }
153