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