summaryrefslogtreecommitdiff
path: root/usr/src/lib/fm/topo/modules/common/hostbridge/hostbridge.c
blob: 12904761ee297e6c5995146fd1e17b2d286d8157 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#include <string.h>
#include <fm/topo_mod.h>
#include <fm/topo_hc.h>
#include <libdevinfo.h>
#include <limits.h>
#include <sys/fm/protocol.h>
#include <sys/param.h>
#include <sys/systeminfo.h>
#include <assert.h>

#include <hostbridge.h>
#include <pcibus.h>
#include <did.h>
#include <did_props.h>
#include <util.h>

/*
 * hostbridge.c
 *	Generic code shared by all the hostbridge enumerators
 */
static void hb_release(topo_mod_t *, tnode_t *);
static int hb_label(topo_mod_t *, tnode_t *, topo_version_t, nvlist_t *,
    nvlist_t **);
static int hb_enum(topo_mod_t *, tnode_t *, const char *, topo_instance_t,
    topo_instance_t, void *, void *);

extern int platform_hb_label(topo_mod_t *, tnode_t *, nvlist_t *, nvlist_t **);
extern int platform_hb_enum(topo_mod_t *, tnode_t *,
    const char *, topo_instance_t, topo_instance_t);

extern txprop_t ExHB_common_props[];
extern txprop_t HB_common_props[];
extern txprop_t RC_common_props[];
extern int ExHB_propcnt;
extern int HB_propcnt;
extern int RC_propcnt;

static int specific_hb_enum(topo_mod_t *, tnode_t *, const char *,
    topo_instance_t, topo_instance_t, void *);

static const topo_modops_t Hb_ops =
	{ hb_enum, hb_release };
static const topo_modinfo_t Hb_info =
	{ HOSTBRIDGE, FM_FMRI_SCHEME_HC, HB_ENUMR_VERS, &Hb_ops };

static const topo_method_t Hb_methods[] = {
	{ TOPO_METH_LABEL, TOPO_METH_LABEL_DESC,
	    TOPO_METH_LABEL_VERSION, TOPO_STABILITY_INTERNAL, hb_label },
	{ NULL }
};

static const topo_pgroup_info_t hb_auth_pgroup = {
	FM_FMRI_AUTHORITY,
	TOPO_STABILITY_PRIVATE,
	TOPO_STABILITY_PRIVATE,
	1
};

int
_topo_init(topo_mod_t *modhdl, topo_version_t version)
{
	/*
	 * Turn on module debugging output
	 */
	if (getenv("TOPOHBDBG") != NULL)
		topo_mod_setdebug(modhdl);
	topo_mod_dprintf(modhdl, "initializing hostbridge enumerator\n");

	if (version != HB_ENUMR_VERS)
		return (topo_mod_seterrno(modhdl, EMOD_VER_NEW));

	if (topo_mod_register(modhdl, &Hb_info, TOPO_VERSION) < 0) {
		topo_mod_dprintf(modhdl, "hostbridge registration failed: %s\n",
		    topo_mod_errmsg(modhdl));
		return (-1); /* mod errno already set */
	}

	topo_mod_dprintf(modhdl, "Hostbridge enumr initd\n");

	return (0);
}

void
_topo_fini(topo_mod_t *modhdl)
{
	topo_mod_unregister(modhdl);
}

static int
hb_label(topo_mod_t *mp, tnode_t *node, topo_version_t version,
    nvlist_t *in, nvlist_t **out)
{
	if (version > TOPO_METH_LABEL_VERSION)
		return (topo_mod_seterrno(mp, EMOD_VER_NEW));
	return (platform_hb_label(mp, node, in, out));
}

static topo_mod_t *
pci_enumr_load(topo_mod_t *mp)
{
	topo_mod_t *rp = NULL;

	if ((rp = topo_mod_load(mp, PCI_ENUM, PCI_ENUMR_VERS)) == NULL) {
		topo_mod_dprintf(mp,
		    "%s enumerator could not load %s.\n", HOSTBRIDGE, PCI_ENUM);
	}
	return (rp);
}

/*ARGSUSED*/
static int
hb_enum(topo_mod_t *mp, tnode_t *pn, const char *name, topo_instance_t imin,
    topo_instance_t imax, void *notused, void *data)
{
	int rv;
	topo_mod_t *pcimod;

	if (strcmp(name, HOSTBRIDGE) != 0) {
		topo_mod_dprintf(mp,
		    "Currently only know how to enumerate %s components.\n",
		    HOSTBRIDGE);
		return (0);
	}
	/*
	 * Load the pcibus enumerator
	 */
	if ((pcimod = pci_enumr_load(mp)) == NULL)
		return (-1);

	/*
	 * If we're asked to enumerate a whole range of hostbridges, then
	 * we need to find them all.  If we're just asked to enumerate a
	 * single hostbridge, we expect our caller to have passed us linked
	 * did_t structures we can use to enumerate the singled out hostbridge.
	 */
	if (imin != imax) {

		if (did_hash_init(mp) < 0) {
			topo_mod_dprintf(mp,
			    "Hash initialization for hostbridge "
			    "enumerator failed.\n");
			topo_mod_unload(pcimod);
			return (-1);
		}
		rv = platform_hb_enum(mp, pn, name, imin, imax);
		did_hash_fini(mp);
	} else {
		rv = specific_hb_enum(mp, pn, name, imin, imax, data);
	}

	return (rv);
}

/*ARGSUSED*/
static void
hb_release(topo_mod_t *mp, tnode_t *node)
{
	topo_method_unregister_all(mp, node);
}

static tnode_t *
hb_tnode_create(topo_mod_t *mod, tnode_t *parent,
    const char *name, topo_instance_t i, void *priv)
{
	int err;
	nvlist_t *fmri;
	tnode_t *ntn;
	nvlist_t *auth = topo_mod_auth(mod, parent);

	fmri = topo_mod_hcfmri(mod, parent, FM_HC_SCHEME_VERSION, name, i,
	    NULL, auth, NULL, NULL, NULL);
	nvlist_free(auth);
	if (fmri == NULL) {
		topo_mod_dprintf(mod,
		    "Unable to make nvlist for %s bind: %s.\n",
		    name, topo_mod_errmsg(mod));
		return (NULL);
	}

	ntn = topo_node_bind(mod, parent, name, i, fmri);
	if (ntn == NULL) {
		topo_mod_dprintf(mod,
		    "topo_node_bind (%s%d/%s%d) failed: %s\n",
		    topo_node_name(parent), topo_node_instance(parent),
		    name, i,
		    topo_strerror(topo_mod_errno(mod)));
		nvlist_free(fmri);
		return (NULL);
	}
	nvlist_free(fmri);
	topo_node_setspecific(ntn, priv);

	if (topo_pgroup_create(ntn, &hb_auth_pgroup, &err) == 0) {
		(void) topo_prop_inherit(ntn, FM_FMRI_AUTHORITY,
		    FM_FMRI_AUTH_PRODUCT, &err);
		(void) topo_prop_inherit(ntn, FM_FMRI_AUTHORITY,
		    FM_FMRI_AUTH_PRODUCT_SN, &err);
		(void) topo_prop_inherit(ntn, FM_FMRI_AUTHORITY,
		    FM_FMRI_AUTH_CHASSIS, &err);
		(void) topo_prop_inherit(ntn, FM_FMRI_AUTHORITY,
		    FM_FMRI_AUTH_SERVER, &err);
	}

	if (topo_method_register(mod, ntn, Hb_methods) < 0) {
		topo_mod_dprintf(mod, "topo_method_register failed: %s\n",
		    topo_strerror(topo_mod_errno(mod)));
		topo_node_unbind(ntn);
		return (NULL);
	}
	return (ntn);
}

tnode_t *
pcihostbridge_declare(topo_mod_t *mod, tnode_t *parent, di_node_t din,
    topo_instance_t i)
{
	did_t *pd;
	tnode_t *ntn;

	if ((pd = did_find(mod, din)) == NULL)
		return (NULL);
	if ((ntn = hb_tnode_create(mod, parent, HOSTBRIDGE, i, din)) == NULL)
		return (NULL);
	if (did_props_set(ntn, pd, HB_common_props, HB_propcnt) < 0) {
		topo_node_unbind(ntn);
		return (NULL);
	}
	/*
	 * We expect to find pci buses beneath the hostbridge.
	 */
	if (child_range_add(mod, ntn, PCI_BUS, 0, MAX_HB_BUSES) < 0) {
		topo_node_unbind(ntn);
		return (NULL);
	}
	return (ntn);
}

tnode_t *
pciexhostbridge_declare(topo_mod_t *mod, tnode_t *parent, di_node_t din,
    topo_instance_t hi)
{
	did_t *pd;
	tnode_t *ntn;

	if ((pd = did_find(mod, din)) == NULL)
		return (NULL);
	if ((ntn = hb_tnode_create(mod, parent, HOSTBRIDGE, hi, din)) == NULL)
		return (NULL);
	if (did_props_set(ntn, pd, ExHB_common_props, ExHB_propcnt) < 0) {
		topo_node_unbind(ntn);
		return (NULL);
	}
	/*
	 * We expect to find root complexes beneath the hostbridge.
	 */
	if (child_range_add(mod, ntn, PCIEX_ROOT, 0, MAX_HB_BUSES) < 0) {
		topo_node_unbind(ntn);
		return (NULL);
	}
	return (ntn);
}

tnode_t *
pciexrc_declare(topo_mod_t *mod, tnode_t *parent, di_node_t din,
    topo_instance_t ri)
{
	did_t *pd;
	tnode_t *ntn;

	if ((pd = did_find(mod, din)) == NULL)
		return (NULL);
	did_markrc(pd);
	if ((ntn = hb_tnode_create(mod, parent, PCIEX_ROOT, ri, din)) == NULL)
		return (NULL);
	if (did_props_set(ntn, pd, RC_common_props, RC_propcnt) < 0) {
		topo_node_unbind(ntn);
		return (NULL);
	}
	/*
	 * We expect to find pci-express buses beneath a root complex
	 */
	if (child_range_add(mod, ntn, PCIEX_BUS, 0, MAX_HB_BUSES) < 0) {
		topo_node_range_destroy(ntn, PCIEX_BUS);
		return (NULL);
	}
	return (ntn);
}

/*ARGSUSED*/
static int
specific_hb_enum(topo_mod_t *mod, tnode_t *pn, const char *name,
    topo_instance_t imin, topo_instance_t imax, void *priv)
{
	tnode_t *hb;
	did_t *iodid = (did_t *)priv;
	did_t *didp;
	int brc = 0;
	int bus;

	did_setspecific(mod, priv);

	/*
	 * Find the hostbridge of interest
	 */
	didp = iodid;
	for (brc = 0; brc < imin; brc++)
		didp = did_chain_get(didp);
	assert(didp != NULL);

	if ((hb = pcihostbridge_declare(mod, pn, did_dinode(didp), imin))
	    == NULL) {
		return (-1);
	}
	while (didp != NULL) {
		did_BDF(didp, &bus, NULL, NULL);
		if (topo_mod_enumerate(mod,
		    hb, PCI_BUS, PCI_BUS, bus, bus, didp) != 0) {
			return (topo_mod_seterrno(mod, EMOD_PARTIAL_ENUM));
		}
		didp = did_link_get(didp);
	}

	return (0);
}