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 #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 	return (topo_mod_enumerate(mod,
49 	    hb, PCI_BUS, PCI_BUS, 0, MAX_HB_BUSES, (void *)hbdid));
50 }
51 
52 static int
53 rc_process(topo_mod_t *mod, tnode_t *ptn, topo_instance_t hbi, di_node_t bn)
54 {
55 	tnode_t *hb;
56 	tnode_t *rc;
57 	did_t *hbdid;
58 
59 	if ((hbdid = did_create(mod, bn, 0, hbi, hbi, TRUST_BDF)) == NULL)
60 		return (-1);
61 	if ((hb = pciexhostbridge_declare(mod, ptn, bn, hbi)) == NULL)
62 		return (-1);
63 	if ((rc = pciexrc_declare(mod, hb, bn, hbi)) == NULL)
64 		return (-1);
65 	return (topo_mod_enumerate(mod,
66 	    rc, PCI_BUS, PCIEX_BUS, 0, MAX_HB_BUSES, (void *)hbdid));
67 }
68 
69 
70 int
71 pci_hostbridges_find(topo_mod_t *mod, tnode_t *ptn)
72 {
73 	di_node_t devtree;
74 	di_node_t pnode, cnode;
75 	int hbcnt = 0;
76 
77 	/* Scan for buses, top-level devinfo nodes with the right driver */
78 	devtree = topo_mod_devinfo(mod);
79 	if (devtree == DI_NODE_NIL) {
80 		topo_mod_dprintf(mod, "devinfo init failed.");
81 		topo_node_range_destroy(ptn, HOSTBRIDGE);
82 		return (0);
83 	}
84 
85 	pnode = di_drv_first_node(PCI, devtree);
86 	while (pnode != DI_NODE_NIL) {
87 		if (hb_process(mod, ptn, hbcnt++, pnode)
88 		    < 0) {
89 			topo_node_range_destroy(ptn, HOSTBRIDGE);
90 			return (topo_mod_seterrno(mod, EMOD_PARTIAL_ENUM));
91 		}
92 		pnode = di_drv_next_node(pnode);
93 	}
94 
95 	pnode = di_drv_first_node(NPE, devtree);
96 	while (pnode != DI_NODE_NIL) {
97 		for (cnode = di_child_node(pnode); cnode != DI_NODE_NIL;
98 		    cnode = di_sibling_node(cnode)) {
99 			if (di_driver_name(cnode) == NULL)
100 				continue;
101 			if (strcmp(di_driver_name(cnode), PCI_PCI) == 0) {
102 				if (hb_process(mod, ptn, hbcnt++, cnode) < 0) {
103 					topo_node_range_destroy(ptn,
104 					    HOSTBRIDGE);
105 					return (topo_mod_seterrno(mod,
106 					    EMOD_PARTIAL_ENUM));
107 				}
108 			}
109 			if (strcmp(di_driver_name(cnode), PCIE_PCI) == 0) {
110 				if (rc_process(mod, ptn, hbcnt++, cnode) < 0) {
111 					topo_node_range_destroy(ptn,
112 					    HOSTBRIDGE);
113 					return (topo_mod_seterrno(mod,
114 					    EMOD_PARTIAL_ENUM));
115 				}
116 			}
117 		}
118 		pnode = di_drv_next_node(pnode);
119 	}
120 	return (0);
121 }
122 
123 /*ARGSUSED*/
124 int
125 platform_hb_enum(topo_mod_t *mod, tnode_t *parent, const char *name,
126     topo_instance_t imin, topo_instance_t imax)
127 {
128 	return (pci_hostbridges_find(mod, parent));
129 }
130 
131 /*ARGSUSED*/
132 int
133 platform_hb_label(topo_mod_t *mod, tnode_t *node, nvlist_t *in, nvlist_t **out)
134 {
135 	return (labelmethod_inherit(mod, node, in, out));
136 }
137