1074bb90dSTom Pothier /*
2074bb90dSTom Pothier  * CDDL HEADER START
3074bb90dSTom Pothier  *
4074bb90dSTom Pothier  * The contents of this file are subject to the terms of the
5074bb90dSTom Pothier  * Common Development and Distribution License (the "License").
6074bb90dSTom Pothier  * You may not use this file except in compliance with the License.
7074bb90dSTom Pothier  *
8074bb90dSTom Pothier  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9074bb90dSTom Pothier  * or http://www.opensolaris.org/os/licensing.
10074bb90dSTom Pothier  * See the License for the specific language governing permissions
11074bb90dSTom Pothier  * and limitations under the License.
12074bb90dSTom Pothier  *
13074bb90dSTom Pothier  * When distributing Covered Code, include this CDDL HEADER in each
14074bb90dSTom Pothier  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15074bb90dSTom Pothier  * If applicable, add the following below this CDDL HEADER, with the
16074bb90dSTom Pothier  * fields enclosed by brackets "[]" replaced with your own identifying
17074bb90dSTom Pothier  * information: Portions Copyright [yyyy] [name of copyright owner]
18074bb90dSTom Pothier  *
19074bb90dSTom Pothier  * CDDL HEADER END
20074bb90dSTom Pothier  */
21074bb90dSTom Pothier 
22074bb90dSTom Pothier /*
235677a1bfSCheng Sean Ye  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
24074bb90dSTom Pothier  */
25074bb90dSTom Pothier 
26074bb90dSTom Pothier /*
27074bb90dSTom Pothier  * i86pc Generic hostbridge/pciex/pci enumerator
28074bb90dSTom Pothier  *
29074bb90dSTom Pothier  * hostbridge/pciexrc/pcibus topo nodes are created per SMBIOS type 138
30074bb90dSTom Pothier  * (SUN_OEM_PCIEXRC) records.   Each type 138 record can either represent
31074bb90dSTom Pothier  * a hostbridge or a pciexrc/pcibus determined by whether it points to
32074bb90dSTom Pothier  * a baseboard record or another type 138 record.
33074bb90dSTom Pothier  *
34074bb90dSTom Pothier  * x86pi_gen_hbr() is called when a new hostbridge node needs to be created..
35074bb90dSTom Pothier  * It then searches all the type 138 records that connected to it.  For each
36074bb90dSTom Pothier  * of the records, bdf is compared to find a matching di_node.  If the
37074bb90dSTom Pothier  * di_node is a pciex root port, a pciexrc (bad name!) node will be created.
38074bb90dSTom Pothier  * When pciexrc creation is done, or the di_node is a pcibus, in either
39074bb90dSTom Pothier  * case the pcibus module will loaded to enumerate pciexbus/pcibus etc.
40074bb90dSTom Pothier  *
41074bb90dSTom Pothier  * The enumeration uses did routines heavily, which requires a did hash
42074bb90dSTom Pothier  * pointer stored in x86pi's module-specific area.
43074bb90dSTom Pothier  */
44074bb90dSTom Pothier 
45074bb90dSTom Pothier #include <sys/types.h>
46074bb90dSTom Pothier #include <strings.h>
47074bb90dSTom Pothier #include <fm/topo_mod.h>
48074bb90dSTom Pothier #include <fm/topo_hc.h>
49074bb90dSTom Pothier #include <sys/systeminfo.h>
50074bb90dSTom Pothier #include <sys/smbios_impl.h>
51074bb90dSTom Pothier #include <sys/fm/protocol.h>
52074bb90dSTom Pothier #include <x86pi_impl.h>
53074bb90dSTom Pothier #include <did.h>
54074bb90dSTom Pothier #include <did_impl.h>
55074bb90dSTom Pothier #include <did_props.h>
56074bb90dSTom Pothier #include <hostbridge.h>
57074bb90dSTom Pothier 
58074bb90dSTom Pothier #define	PCI_ENUM	"pcibus"
59074bb90dSTom Pothier #define	PCI_ENUMR_VERS	1
60074bb90dSTom Pothier #define	MAX_HB_BUSES	255
61074bb90dSTom Pothier 
62074bb90dSTom Pothier extern txprop_t RC_common_props[], HB_common_props[], ExHB_common_props[];
63074bb90dSTom Pothier extern int RC_propcnt, HB_propcnt, ExHB_propcnt;
64074bb90dSTom Pothier 
65074bb90dSTom Pothier static topo_mod_t *pcimp = NULL;
66074bb90dSTom Pothier 
67074bb90dSTom Pothier int
x86pi_hbr_enum_init(topo_mod_t * mod)68074bb90dSTom Pothier x86pi_hbr_enum_init(topo_mod_t *mod)
69074bb90dSTom Pothier {
70074bb90dSTom Pothier 	const char *f = "x86pi_hbr_enum_init";
71074bb90dSTom Pothier 
725677a1bfSCheng Sean Ye 	if (did_hash_init(mod) < 0) {
73074bb90dSTom Pothier 		topo_mod_dprintf(mod, "%s: did_hash_init() failed.\n", f);
74074bb90dSTom Pothier 		return (-1);
75074bb90dSTom Pothier 	}
76074bb90dSTom Pothier 
775677a1bfSCheng Sean Ye 	if ((pcimp = topo_mod_load(mod, PCI_ENUM, PCI_ENUMR_VERS)) == NULL) {
78074bb90dSTom Pothier 		topo_mod_dprintf(mod,
79074bb90dSTom Pothier 		    "%s: %s enumerator could not load %s.\n",
80074bb90dSTom Pothier 		    f, HOSTBRIDGE, PCI_ENUM);
81074bb90dSTom Pothier 		did_hash_fini(mod);
82074bb90dSTom Pothier 		return (-1);
83074bb90dSTom Pothier 	}
84074bb90dSTom Pothier 
85074bb90dSTom Pothier 	return (0);
86074bb90dSTom Pothier }
87074bb90dSTom Pothier 
88074bb90dSTom Pothier void
x86pi_hbr_enum_fini(topo_mod_t * mod)89074bb90dSTom Pothier x86pi_hbr_enum_fini(topo_mod_t *mod)
90074bb90dSTom Pothier {
91074bb90dSTom Pothier 	did_hash_fini(mod);
925677a1bfSCheng Sean Ye 	topo_mod_unload(pcimp);
935677a1bfSCheng Sean Ye 	pcimp = NULL;
94074bb90dSTom Pothier }
95074bb90dSTom Pothier 
96074bb90dSTom Pothier static int
pciex_process(topo_mod_t * mod,tnode_t * tn_hbr,di_node_t rcn,topo_instance_t rci)97074bb90dSTom Pothier pciex_process(topo_mod_t *mod, tnode_t *tn_hbr, di_node_t rcn,
98074bb90dSTom Pothier     topo_instance_t rci)
99074bb90dSTom Pothier {
100074bb90dSTom Pothier 	did_t		*did;
101074bb90dSTom Pothier 	int		rv;
102074bb90dSTom Pothier 	tnode_t		*tn_rc;
103074bb90dSTom Pothier 	x86pi_hcfmri_t	hcfmri = {0};
104074bb90dSTom Pothier 	tnode_t		*tn_bb = topo_node_parent(tn_hbr);
105074bb90dSTom Pothier 	const char	*f = "pciexrc_process";
106074bb90dSTom Pothier 
107074bb90dSTom Pothier 	if ((did = did_create(mod, rcn, topo_node_instance(tn_bb),
108074bb90dSTom Pothier 	    topo_node_instance(tn_hbr), rci, TRUST_BDF)) == NULL)
1095cc5d5ceSToomas Soome 		return (-1);
110074bb90dSTom Pothier 
111074bb90dSTom Pothier 	did_markrc(did);
112074bb90dSTom Pothier 
113074bb90dSTom Pothier 	/*
114074bb90dSTom Pothier 	 * Let did set the hostbridge properties excluding FRU and label.
115074bb90dSTom Pothier 	 */
116074bb90dSTom Pothier 	(void) did_props_set(tn_hbr, did, ExHB_common_props, ExHB_propcnt - 2);
117074bb90dSTom Pothier 
118074bb90dSTom Pothier 	if (topo_node_range_create(mod, tn_hbr, PCIEX_ROOT, 0,
119074bb90dSTom Pothier 	    MAX_HB_BUSES) != 0 && topo_mod_errno(mod) != EMOD_NODE_DUP) {
120074bb90dSTom Pothier 		topo_mod_dprintf(mod,
121074bb90dSTom Pothier 		    "%s: create child range for %s failed: %s\n",
122074bb90dSTom Pothier 		    f, PCIEX_ROOT, topo_mod_errmsg(mod));
123074bb90dSTom Pothier 		return (-1);
124074bb90dSTom Pothier 	}
125074bb90dSTom Pothier 
126074bb90dSTom Pothier 	hcfmri.hc_name = PCIEX_ROOT;
127074bb90dSTom Pothier 	hcfmri.instance = rci;
128074bb90dSTom Pothier 	rv = x86pi_enum_generic(mod, &hcfmri, tn_hbr, tn_hbr, &tn_rc, 0);
129074bb90dSTom Pothier 	if (rv != 0) {
130*6597d6fcSRobert Mustacchi 		topo_mod_dprintf(mod, "%s: failed to create %s = %" PRIu64 "\n",
131074bb90dSTom Pothier 		    f, PCIEX_ROOT, rci);
132074bb90dSTom Pothier 		return (-1);
133074bb90dSTom Pothier 	}
134074bb90dSTom Pothier 
135074bb90dSTom Pothier 	/*
136074bb90dSTom Pothier 	 * pcibus enumerator requires di_node_t be set in node specific
137074bb90dSTom Pothier 	 */
138074bb90dSTom Pothier 	topo_node_setspecific(tn_rc, rcn);
139074bb90dSTom Pothier 
140074bb90dSTom Pothier 	/*
141074bb90dSTom Pothier 	 * Let did set the RC properties excluding FRU, and label.
142074bb90dSTom Pothier 	 */
143074bb90dSTom Pothier 	if (did_props_set(tn_rc, did, RC_common_props, RC_propcnt - 2) < 0) {
144*6597d6fcSRobert Mustacchi 		topo_mod_dprintf(mod, "%s: did_props_set failed for %s = %"
145*6597d6fcSRobert Mustacchi 		    PRIu64 "\n", f, PCIEX_ROOT, rci);
146074bb90dSTom Pothier 		topo_node_unbind(tn_rc);
147074bb90dSTom Pothier 		return (-1);
148074bb90dSTom Pothier 	}
149074bb90dSTom Pothier 
150074bb90dSTom Pothier 	if (topo_node_range_create(mod, tn_rc, PCIEX_BUS, 0,
151074bb90dSTom Pothier 	    MAX_HB_BUSES) != 0 && topo_mod_errno(mod) != EMOD_NODE_DUP) {
152074bb90dSTom Pothier 		topo_mod_dprintf(mod,
153074bb90dSTom Pothier 		    "%s: create child range for %s failed: %s\n",
154074bb90dSTom Pothier 		    f, PCIEX_BUS, topo_mod_errmsg(mod));
155074bb90dSTom Pothier 		return (-1);
156074bb90dSTom Pothier 	}
157074bb90dSTom Pothier 
158074bb90dSTom Pothier 	return (topo_mod_enumerate(mod, tn_rc, PCI_BUS, PCIEX_BUS,
159074bb90dSTom Pothier 	    0, MAX_HB_BUSES, did));
160074bb90dSTom Pothier }
161074bb90dSTom Pothier 
162074bb90dSTom Pothier static int
pci_process(topo_mod_t * mod,tnode_t * tn_hbr,di_node_t bn)163074bb90dSTom Pothier pci_process(topo_mod_t *mod, tnode_t *tn_hbr, di_node_t bn)
164074bb90dSTom Pothier {
165074bb90dSTom Pothier 	did_t *did;
166074bb90dSTom Pothier 	tnode_t *tn_bb = topo_node_parent(tn_hbr);
167074bb90dSTom Pothier 
168074bb90dSTom Pothier 	if ((did = did_create(mod, bn, topo_node_instance(tn_bb),
169074bb90dSTom Pothier 	    topo_node_instance(tn_hbr), NO_RC, TRUST_BDF)) == NULL)
170074bb90dSTom Pothier 		return (-1);
171074bb90dSTom Pothier 
172074bb90dSTom Pothier 	/*
173074bb90dSTom Pothier 	 * Let did set the hostbridge properties excluding FRU and label.
174074bb90dSTom Pothier 	 */
175074bb90dSTom Pothier 	(void) did_props_set(tn_hbr, did, HB_common_props, HB_propcnt - 2);
176074bb90dSTom Pothier 
177074bb90dSTom Pothier 	if (topo_node_range_create(mod, tn_hbr, PCI_BUS, 0,
178074bb90dSTom Pothier 	    MAX_HB_BUSES) != 0 && topo_mod_errno(mod) != EMOD_NODE_DUP) {
179074bb90dSTom Pothier 		topo_mod_dprintf(mod, "create child range for %s failed: %s\n",
180074bb90dSTom Pothier 		    PCI_BUS, topo_mod_errmsg(mod));
181074bb90dSTom Pothier 		return (-1);
182074bb90dSTom Pothier 	}
183074bb90dSTom Pothier 
184074bb90dSTom Pothier 	return (topo_mod_enumerate(mod, tn_hbr, PCI_BUS, PCI_BUS,
185074bb90dSTom Pothier 	    0, MAX_HB_BUSES, did));
186074bb90dSTom Pothier }
187074bb90dSTom Pothier 
188074bb90dSTom Pothier static int
x86pi_gen_pci_pciexrc(topo_mod_t * mod,tnode_t * tn_hbr,uint16_t bdf,topo_instance_t * rcip)189074bb90dSTom Pothier x86pi_gen_pci_pciexrc(topo_mod_t *mod, tnode_t *tn_hbr, uint16_t bdf,
190074bb90dSTom Pothier     topo_instance_t *rcip)
191074bb90dSTom Pothier {
192074bb90dSTom Pothier 	di_node_t devtree, pnode, cnode;
193074bb90dSTom Pothier 
194074bb90dSTom Pothier 	topo_mod_dprintf(mod, "creating pci/pciexrc node bdf = %#x\n",
195074bb90dSTom Pothier 	    (int)bdf);
196074bb90dSTom Pothier 
197074bb90dSTom Pothier 	devtree = topo_mod_devinfo(mod);
198074bb90dSTom Pothier 	if (devtree == DI_NODE_NIL) {
199074bb90dSTom Pothier 		topo_mod_dprintf(mod, "devinfo init failed.\n");
200074bb90dSTom Pothier 		return (-1);
201074bb90dSTom Pothier 	}
202074bb90dSTom Pothier 
203074bb90dSTom Pothier 	for (pnode = di_drv_first_node(PCI, devtree);
204074bb90dSTom Pothier 	    pnode != DI_NODE_NIL; pnode = di_drv_next_node(pnode))
205074bb90dSTom Pothier 		if (x86pi_bdf(mod, pnode) == bdf)
206074bb90dSTom Pothier 			return (pci_process(mod, tn_hbr, pnode));
207074bb90dSTom Pothier 
208074bb90dSTom Pothier 	pnode = di_drv_first_node(NPE, devtree);
209074bb90dSTom Pothier 	while (pnode != DI_NODE_NIL) {
210074bb90dSTom Pothier 		for (cnode = di_child_node(pnode); cnode != DI_NODE_NIL;
211074bb90dSTom Pothier 		    cnode = di_sibling_node(cnode)) {
212074bb90dSTom Pothier 			if (di_driver_name(cnode) == NULL ||
213074bb90dSTom Pothier 			    x86pi_bdf(mod, cnode) != bdf)
214074bb90dSTom Pothier 				continue;
215074bb90dSTom Pothier 
216074bb90dSTom Pothier 			if (strcmp(di_driver_name(cnode), PCI_PCI) == 0)
217074bb90dSTom Pothier 				return (pci_process(mod, tn_hbr, cnode));
218074bb90dSTom Pothier 
219074bb90dSTom Pothier 			if (strcmp(di_driver_name(cnode), PCIEB) == 0)
220074bb90dSTom Pothier 				return (pciex_process(mod, tn_hbr,
221074bb90dSTom Pothier 				    cnode, (*rcip)++));
222074bb90dSTom Pothier 
223074bb90dSTom Pothier 			topo_mod_dprintf(mod, "no matching driver found: "
224074bb90dSTom Pothier 			    "bdf = %#x\n", (int)bdf);
225074bb90dSTom Pothier 		}
226074bb90dSTom Pothier 		pnode = di_drv_next_node(pnode);
227074bb90dSTom Pothier 	}
228074bb90dSTom Pothier 
229074bb90dSTom Pothier 	topo_mod_dprintf(mod, "no matching bdf found: bdf = %#x\n", (int)bdf);
230074bb90dSTom Pothier 
231074bb90dSTom Pothier 	return (0);
232074bb90dSTom Pothier }
233074bb90dSTom Pothier 
234074bb90dSTom Pothier int
x86pi_gen_hbr(topo_mod_t * mod,tnode_t * tn_bb,int hbr_smbid,topo_instance_t hbri,topo_instance_t * rcip)235efd31e1dSTrang Do x86pi_gen_hbr(topo_mod_t *mod, tnode_t *tn_bb,
236074bb90dSTom Pothier     int hbr_smbid, topo_instance_t hbri, topo_instance_t *rcip)
237074bb90dSTom Pothier {
238074bb90dSTom Pothier 	x86pi_hcfmri_t	hcfmri = {0};
239074bb90dSTom Pothier 	tnode_t		*tn_hbr;
240074bb90dSTom Pothier 	smbs_cnt_t	*smbc = &stypes[SUN_OEM_PCIEXRC];
241074bb90dSTom Pothier 	smbios_pciexrc_t smb_rc;
242074bb90dSTom Pothier 	int		i, rv, err = 0;
243074bb90dSTom Pothier 	const char	*f = "x86pi_gen_hbr";
2445cc5d5ceSToomas Soome 	smbios_hdl_t	*shp;
245efd31e1dSTrang Do 
246efd31e1dSTrang Do 	shp = topo_mod_smbios(mod);
247efd31e1dSTrang Do 	if (shp == NULL)
248efd31e1dSTrang Do 		return (topo_mod_seterrno(mod, EMOD_PARTIAL_ENUM));
249074bb90dSTom Pothier 
250074bb90dSTom Pothier 	hcfmri.hc_name = HOSTBRIDGE;
251074bb90dSTom Pothier 	hcfmri.instance = hbri;
252074bb90dSTom Pothier 
253074bb90dSTom Pothier 	/* create and bind the "hostbridge" node */
254074bb90dSTom Pothier 	rv = x86pi_enum_generic(mod, &hcfmri, tn_bb, tn_bb, &tn_hbr, 0);
255074bb90dSTom Pothier 	if (rv != 0) {
256*6597d6fcSRobert Mustacchi 		topo_mod_dprintf(mod, "%s: failed to create %s = %" PRIu64 "\n",
257074bb90dSTom Pothier 		    f, HOSTBRIDGE, hbri);
258074bb90dSTom Pothier 		return (topo_mod_seterrno(mod, EMOD_PARTIAL_ENUM));
259074bb90dSTom Pothier 	}
260074bb90dSTom Pothier 
261074bb90dSTom Pothier 	/*
262074bb90dSTom Pothier 	 * Walk the smbios records and create the pci/pciexrc nodes
263074bb90dSTom Pothier 	 */
264074bb90dSTom Pothier 	for (i = 0; i < smbc->count; i++) {
265074bb90dSTom Pothier 		if (smbios_info_pciexrc(shp, smbc->ids[i].id, &smb_rc) != 0)
266074bb90dSTom Pothier 			topo_mod_dprintf(mod,
267074bb90dSTom Pothier 			    "%s: failed: id = %d\n", f, (int)smbc->ids[i].id);
268074bb90dSTom Pothier 		else if (smb_rc.smbpcie_bb == hbr_smbid &&
269074bb90dSTom Pothier 		    x86pi_gen_pci_pciexrc(mod, tn_hbr, smb_rc.smbpcie_bdf,
270074bb90dSTom Pothier 		    rcip) != 0)
271074bb90dSTom Pothier 			err++;
272074bb90dSTom Pothier 	}
273074bb90dSTom Pothier 
274074bb90dSTom Pothier 	return (err == 0 ? 0 : topo_mod_seterrno(mod, EMOD_PARTIAL_ENUM));
275074bb90dSTom Pothier }
276