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