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 <libdevinfo.h>
31 #include <strings.h>
32 #include "pcibus.h"
33 #include "hostbridge.h"
34 #include "did.h"
35 #include "util.h"
36 
37 extern did_hash_t *Didhash;
38 
39 static int
40 hb_process(tnode_t *ptn, topo_instance_t hbi, di_node_t bn)
41 {
42 	tnode_t *hb;
43 
44 	if (did_create(Didhash, bn, 0, hbi, NO_RC, TRUST_BDF) == NULL)
45 		return (-1);
46 	if ((hb = pcihostbridge_declare(ptn, bn, hbi)) == NULL)
47 		return (-1);
48 	return (topo_mod_enumerate(HbHdl,
49 	    hb, PCI_BUS, PCI_BUS, 0, MAX_HB_BUSES));
50 }
51 
52 static int
53 rc_process(tnode_t *ptn, topo_instance_t hbi, di_node_t bn)
54 {
55 	tnode_t *hb;
56 	tnode_t *rc;
57 
58 	if (did_create(Didhash, bn, 0, hbi, hbi, TRUST_BDF) == NULL)
59 		return (-1);
60 	if ((hb = pciexhostbridge_declare(ptn, bn, hbi)) == NULL)
61 		return (-1);
62 	if ((rc = pciexrc_declare(hb, bn, hbi)) == NULL)
63 		return (-1);
64 	return (topo_mod_enumerate(HbHdl,
65 	    rc, PCI_BUS, PCIEX_BUS, 0, MAX_HB_BUSES));
66 }
67 
68 
69 int
70 pci_hostbridges_find(tnode_t *ptn)
71 {
72 	di_node_t devtree;
73 	di_node_t pnode;
74 	di_node_t cnode;
75 	int hbcnt = 0;
76 
77 	/* Scan for buses, top-level devinfo nodes with the right driver */
78 	devtree = di_init("/", DINFOCPYALL);
79 	if (devtree == DI_NODE_NIL) {
80 		topo_mod_dprintf(HbHdl, "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(ptn, hbcnt++, pnode) < 0) {
88 			di_fini(devtree);
89 			topo_node_range_destroy(ptn, HOSTBRIDGE);
90 			return (topo_mod_seterrno(HbHdl, 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(ptn, hbcnt++, cnode) < 0) {
103 					di_fini(devtree);
104 					topo_node_range_destroy(ptn,
105 					    HOSTBRIDGE);
106 					return (topo_mod_seterrno(HbHdl,
107 					    EMOD_PARTIAL_ENUM));
108 				}
109 			}
110 			if (strcmp(di_driver_name(cnode), PCIE_PCI) == 0) {
111 				if (rc_process(ptn, hbcnt++, cnode) < 0) {
112 					di_fini(devtree);
113 					topo_node_range_destroy(ptn,
114 					    HOSTBRIDGE);
115 					return (topo_mod_seterrno(HbHdl,
116 					    EMOD_PARTIAL_ENUM));
117 				}
118 			}
119 		}
120 		pnode = di_drv_next_node(pnode);
121 	}
122 	di_fini(devtree);
123 	return (0);
124 }
125 
126 /*ARGSUSED*/
127 int
128 platform_hb_enum(tnode_t *parent, const char *name,
129     topo_instance_t imin, topo_instance_t imax)
130 {
131 	return (pci_hostbridges_find(parent));
132 }
133 
134 /*ARGSUSED*/
135 int
136 platform_hb_label(tnode_t *node, nvlist_t *in, nvlist_t **out)
137 {
138 	return (labelmethod_inherit(HbHdl, node, in, out));
139 }
140