xref: /illumos-gate/usr/src/lib/fm/topo/modules/common/pcibus/pcibus.c (revision 12cc75c814f0c017004a9bbc96429911e008601b)
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 2007 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 <sys/fm/protocol.h>
30 #include <assert.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <strings.h>
35 #include <alloca.h>
36 #include <sys/param.h>
37 #include <sys/pci.h>
38 #include <sys/pcie.h>
39 #include <libdevinfo.h>
40 #include <libnvpair.h>
41 #include <fm/topo_mod.h>
42 #include <fm/topo_hc.h>
43 
44 #include <hostbridge.h>
45 #include <pcibus.h>
46 #include <did.h>
47 #include <did_props.h>
48 #include <util.h>
49 
50 extern txprop_t Bus_common_props[];
51 extern txprop_t Dev_common_props[];
52 extern txprop_t Fn_common_props[];
53 extern int Bus_propcnt;
54 extern int Dev_propcnt;
55 extern int Fn_propcnt;
56 
57 extern int platform_pci_label(topo_mod_t *mod, tnode_t *, nvlist_t *,
58     nvlist_t **);
59 extern int platform_pci_fru(topo_mod_t *mod, tnode_t *, nvlist_t *,
60     nvlist_t **);
61 static void pci_release(topo_mod_t *, tnode_t *);
62 static int pci_enum(topo_mod_t *, tnode_t *, const char *, topo_instance_t,
63     topo_instance_t, void *, void *);
64 static int pci_label(topo_mod_t *, tnode_t *, topo_version_t, nvlist_t *,
65     nvlist_t **);
66 static int pci_fru(topo_mod_t *, tnode_t *, topo_version_t, nvlist_t *,
67     nvlist_t **);
68 
69 static const topo_modops_t Pci_ops =
70 	{ pci_enum, pci_release };
71 static const topo_modinfo_t Pci_info =
72 	{ PCI_BUS, FM_FMRI_SCHEME_HC, PCI_ENUMR_VERS, &Pci_ops };
73 
74 static const topo_method_t Pci_methods[] = {
75 	{ TOPO_METH_LABEL, TOPO_METH_LABEL_DESC,
76 	    TOPO_METH_LABEL_VERSION, TOPO_STABILITY_INTERNAL, pci_label },
77 	{ TOPO_METH_FRU_COMPUTE, TOPO_METH_FRU_COMPUTE_DESC,
78 	    TOPO_METH_FRU_COMPUTE_VERSION, TOPO_STABILITY_INTERNAL, pci_fru },
79 	{ NULL }
80 };
81 
82 int
83 _topo_init(topo_mod_t *modhdl, topo_version_t version)
84 {
85 	/*
86 	 * Turn on module debugging output
87 	 */
88 	if (getenv("TOPOPCIDBG") != NULL)
89 		topo_mod_setdebug(modhdl);
90 	topo_mod_dprintf(modhdl, "initializing pcibus builtin\n");
91 
92 	if (version != PCI_ENUMR_VERS)
93 		return (topo_mod_seterrno(modhdl, EMOD_VER_NEW));
94 
95 	topo_mod_register(modhdl, &Pci_info, TOPO_VERSION);
96 	topo_mod_dprintf(modhdl, "PCI Enumr initd\n");
97 
98 	return (0);
99 }
100 
101 void
102 _topo_fini(topo_mod_t *modhdl)
103 {
104 	topo_mod_unregister(modhdl);
105 }
106 
107 static int
108 pci_label(topo_mod_t *mp, tnode_t *node, topo_version_t version,
109     nvlist_t *in, nvlist_t **out)
110 {
111 	if (version > TOPO_METH_LABEL_VERSION)
112 		return (topo_mod_seterrno(mp, EMOD_VER_NEW));
113 	return (platform_pci_label(mp, node, in, out));
114 }
115 static int
116 pci_fru(topo_mod_t *mp, tnode_t *node, topo_version_t version,
117     nvlist_t *in, nvlist_t **out)
118 {
119 	if (version > TOPO_METH_FRU_COMPUTE_VERSION)
120 		return (topo_mod_seterrno(mp, EMOD_VER_NEW));
121 	return (platform_pci_fru(mp, node, in, out));
122 }
123 static tnode_t *
124 pci_tnode_create(topo_mod_t *mod, tnode_t *parent,
125     const char *name, topo_instance_t i, void *priv)
126 {
127 	tnode_t *ntn;
128 
129 	if ((ntn = tnode_create(mod, parent, name, i, priv)) == NULL)
130 		return (NULL);
131 	if (topo_method_register(mod, ntn, Pci_methods) < 0) {
132 		topo_mod_dprintf(mod, "topo_method_register failed: %s\n",
133 		    topo_strerror(topo_mod_errno(mod)));
134 		topo_node_unbind(ntn);
135 		return (NULL);
136 	}
137 	return (ntn);
138 }
139 
140 /*ARGSUSED*/
141 static int
142 hostbridge_asdevice(topo_mod_t *mod, tnode_t *bus)
143 {
144 	di_node_t di;
145 	tnode_t *dev32;
146 
147 	di = topo_node_getspecific(bus);
148 	assert(di != DI_NODE_NIL);
149 
150 	if ((dev32 = pcidev_declare(mod, bus, di, 32)) == NULL)
151 		return (-1);
152 	if (pcifn_declare(mod, dev32, di, 0) == NULL) {
153 		topo_node_unbind(dev32);
154 		return (-1);
155 	}
156 	return (0);
157 }
158 
159 tnode_t *
160 pciexfn_declare(topo_mod_t *mod, tnode_t *parent, di_node_t dn,
161     topo_instance_t i)
162 {
163 	did_t *pd;
164 	tnode_t *ntn;
165 
166 	if ((pd = did_find(mod, dn)) == NULL)
167 		return (NULL);
168 	if ((ntn = pci_tnode_create(mod, parent, PCIEX_FUNCTION, i, dn))
169 	    == NULL)
170 		return (NULL);
171 	if (did_props_set(ntn, pd, Fn_common_props, Fn_propcnt) < 0) {
172 		topo_node_unbind(ntn);
173 		return (NULL);
174 	}
175 	/*
176 	 * We may find pci-express buses or plain-pci buses beneath a function
177 	 */
178 	if (child_range_add(mod, ntn, PCIEX_BUS, 0, MAX_HB_BUSES) < 0) {
179 		topo_node_unbind(ntn);
180 		return (NULL);
181 	}
182 	if (child_range_add(mod, ntn, PCI_BUS, 0, MAX_HB_BUSES) < 0) {
183 		topo_node_range_destroy(ntn, PCIEX_BUS);
184 		topo_node_unbind(ntn);
185 		return (NULL);
186 	}
187 	return (ntn);
188 }
189 
190 tnode_t *
191 pciexdev_declare(topo_mod_t *mod, tnode_t *parent, di_node_t dn,
192     topo_instance_t i)
193 {
194 	did_t *pd;
195 	tnode_t *ntn;
196 
197 	if ((pd = did_find(mod, dn)) == NULL)
198 		return (NULL);
199 	did_settnode(pd, parent);
200 
201 	if ((ntn = pci_tnode_create(mod, parent, PCIEX_DEVICE, i, dn)) == NULL)
202 		return (NULL);
203 	if (did_props_set(ntn, pd, Dev_common_props, Dev_propcnt) < 0) {
204 		topo_node_unbind(ntn);
205 		return (NULL);
206 	}
207 
208 	/*
209 	 * We can expect to find pci-express functions beneath the device
210 	 */
211 	if (child_range_add(mod,
212 	    ntn, PCIEX_FUNCTION, 0, MAX_PCIDEV_FNS) < 0) {
213 		topo_node_unbind(ntn);
214 		return (NULL);
215 	}
216 	return (ntn);
217 }
218 
219 tnode_t *
220 pciexbus_declare(topo_mod_t *mod, tnode_t *parent, di_node_t dn,
221     topo_instance_t i)
222 {
223 	did_t *pd;
224 	tnode_t *ntn;
225 
226 	if ((pd = did_find(mod, dn)) == NULL)
227 		return (NULL);
228 	if ((ntn = pci_tnode_create(mod, parent, PCIEX_BUS, i, dn)) == NULL)
229 		return (NULL);
230 	if (did_props_set(ntn, pd, Bus_common_props, Bus_propcnt) < 0) {
231 		topo_node_unbind(ntn);
232 		return (NULL);
233 	}
234 	/*
235 	 * We can expect to find pci-express devices beneath the bus
236 	 */
237 	if (child_range_add(mod,
238 	    ntn, PCIEX_DEVICE, 0, MAX_PCIBUS_DEVS) < 0) {
239 		topo_node_unbind(ntn);
240 		return (NULL);
241 	}
242 	return (ntn);
243 }
244 
245 tnode_t *
246 pcifn_declare(topo_mod_t *mod, tnode_t *parent, di_node_t dn,
247     topo_instance_t i)
248 {
249 	did_t *pd;
250 	tnode_t *ntn;
251 
252 	if ((pd = did_find(mod, dn)) == NULL)
253 		return (NULL);
254 	if ((ntn = pci_tnode_create(mod, parent, PCI_FUNCTION, i, dn)) == NULL)
255 		return (NULL);
256 	if (did_props_set(ntn, pd, Fn_common_props, Fn_propcnt) < 0) {
257 		topo_node_unbind(ntn);
258 		return (NULL);
259 	}
260 	/*
261 	 * We may find pci buses beneath a function
262 	 */
263 	if (child_range_add(mod, ntn, PCI_BUS, 0, MAX_HB_BUSES) < 0) {
264 		topo_node_unbind(ntn);
265 		return (NULL);
266 	}
267 	return (ntn);
268 }
269 
270 tnode_t *
271 pcidev_declare(topo_mod_t *mod, tnode_t *parent, di_node_t dn,
272     topo_instance_t i)
273 {
274 	did_t *pd;
275 	did_t *ppd;
276 	di_node_t pdn;
277 	tnode_t *ntn;
278 
279 	if ((pdn = topo_node_getspecific(parent)) == DI_NODE_NIL)
280 		return (NULL);
281 	if ((ppd = did_find(mod, pdn)) == NULL)
282 		return (NULL);
283 	if ((pd = did_find(mod, dn)) == NULL)
284 		return (NULL);
285 	/* remember parent tnode */
286 	did_settnode(pd, parent);
287 
288 	if ((ntn = pci_tnode_create(mod, parent, PCI_DEVICE, i, dn)) == NULL)
289 		return (NULL);
290 	/*
291 	 * If our devinfo node is lacking certain information of its
292 	 * own, we may need/want to inherit the information available
293 	 * from our parent node's private data.
294 	 */
295 
296 	did_inherit(ppd, pd);
297 	if (did_props_set(ntn, pd, Dev_common_props, Dev_propcnt) < 0) {
298 		topo_node_unbind(ntn);
299 		return (NULL);
300 	}
301 
302 	/*
303 	 * We can expect to find pci functions beneath the device
304 	 */
305 	if (child_range_add(mod, ntn, PCI_FUNCTION, 0, MAX_PCIDEV_FNS) < 0) {
306 		topo_node_unbind(ntn);
307 		return (NULL);
308 	}
309 	return (ntn);
310 }
311 
312 tnode_t *
313 pcibus_declare(topo_mod_t *mod, tnode_t *parent, di_node_t dn,
314     topo_instance_t i)
315 {
316 	did_t *pd;
317 	tnode_t *ntn;
318 	int hbchild = 0;
319 
320 	if ((pd = did_find(mod, dn)) == NULL)
321 		return (NULL);
322 	if ((ntn = pci_tnode_create(mod, parent, PCI_BUS, i, dn)) == NULL)
323 		return (NULL);
324 	/*
325 	 * If our devinfo node is lacking certain information of its
326 	 * own, and our parent topology node is a hostbridge, we may
327 	 * need/want to inherit information available in the
328 	 * hostbridge node's private data.
329 	 */
330 	if (strcmp(topo_node_name(parent), HOSTBRIDGE) == 0)
331 		hbchild = 1;
332 	if (did_props_set(ntn, pd, Bus_common_props, Bus_propcnt) < 0) {
333 		topo_node_unbind(ntn);
334 		return (NULL);
335 	}
336 	/*
337 	 * We can expect to find pci devices beneath the bus
338 	 */
339 	if (child_range_add(mod, ntn, PCI_DEVICE, 0, MAX_PCIBUS_DEVS) < 0) {
340 		topo_node_unbind(ntn);
341 		return (NULL);
342 	}
343 	/*
344 	 * On each bus child of the hostbridge, we represent the
345 	 * hostbridge as a device outside the range of legal device
346 	 * numbers.
347 	 */
348 	if (hbchild == 1) {
349 		if (hostbridge_asdevice(mod, ntn) < 0) {
350 			topo_node_range_destroy(ntn, PCI_DEVICE);
351 			topo_node_unbind(ntn);
352 			return (NULL);
353 		}
354 	}
355 	return (ntn);
356 }
357 
358 static int
359 pci_bridge_declare(topo_mod_t *mod, tnode_t *fn, di_node_t din, int board,
360     int bridge, int rc, int depth)
361 {
362 	int err, excap, extyp;
363 
364 	excap = pciex_cap_get(mod, din);
365 	extyp = excap & PCIE_PCIECAP_DEV_TYPE_MASK;
366 	if (excap <= 0 ||
367 	    extyp != PCIE_PCIECAP_DEV_TYPE_PCIE2PCI)
368 		err = pci_children_instantiate(mod, fn, din, board, bridge,
369 		    rc, TRUST_BDF, depth + 1);
370 	else
371 		err = pci_children_instantiate(mod, fn, din, board, bridge,
372 		    rc - TO_PCI, TRUST_BDF, depth + 1);
373 	return (err);
374 }
375 
376 static void
377 declare_dev_and_fn(topo_mod_t *mod, tnode_t *bus, tnode_t **dev, di_node_t din,
378     int board, int bridge, int rc, int devno, int fnno, int depth)
379 {
380 	int dcnt = 0;
381 	tnode_t *fn;
382 	uint_t class, subclass;
383 
384 	if (*dev == NULL) {
385 		if (rc >= 0)
386 			*dev = pciexdev_declare(mod, bus, din, devno);
387 		else
388 			*dev = pcidev_declare(mod, bus, din, devno);
389 		if (*dev == NULL)
390 			return;
391 		++dcnt;
392 	}
393 	if (rc >= 0)
394 		fn = pciexfn_declare(mod, *dev, din, fnno);
395 	else
396 		fn = pcifn_declare(mod, *dev, din, fnno);
397 
398 	if (fn == NULL) {
399 		if (dcnt) {
400 			topo_node_unbind(*dev);
401 			*dev = NULL;
402 		}
403 		return;
404 	}
405 
406 	if (pci_classcode_get(mod, din, &class, &subclass) < 0) {
407 		topo_node_unbind(fn);
408 		if (dcnt)
409 			topo_node_unbind(*dev);
410 		return;
411 	}
412 
413 	/*
414 	 * This function may be a bridge.  If not, check for a possible
415 	 * topology map file and kick off its enumeration of lower-level
416 	 * devices.
417 	 */
418 	if (class == PCI_CLASS_BRIDGE && subclass == PCI_BRIDGE_PCI)
419 		(void) pci_bridge_declare(mod, fn, din, board, bridge, rc,
420 		    depth);
421 	else if (class == PCI_CLASS_MASS)
422 		(void) topo_mod_enummap(mod, fn, "storage", FM_FMRI_SCHEME_HC);
423 }
424 
425 int
426 pci_children_instantiate(topo_mod_t *mod, tnode_t *parent, di_node_t pn,
427     int board, int bridge, int rc, int bover, int depth)
428 {
429 	did_t *pps[MAX_PCIBUS_DEVS][MAX_PCIDEV_FNS];
430 	did_t *bp = NULL;
431 	did_t *np;
432 	di_node_t sib;
433 	di_node_t din;
434 	tnode_t *bn = NULL;
435 	tnode_t *dn = NULL;
436 	int pb = -1;
437 	int b, d, f;
438 
439 	for (d = 0; d < MAX_PCIBUS_DEVS; d++)
440 		for (f = 0; f < MAX_PCIDEV_FNS; f++)
441 			pps[d][f] = NULL;
442 
443 	/* start at the parent's first sibling */
444 	sib = di_child_node(pn);
445 	while (sib != DI_NODE_NIL) {
446 		np = did_create(mod, sib, board, bridge, rc, bover);
447 		if (np == NULL)
448 			return (-1);
449 		did_BDF(np, &b, &d, &f);
450 		pps[d][f] = np;
451 		if (bp == NULL)
452 			bp = np;
453 		if (pb < 0)
454 			pb = ((bover == TRUST_BDF) ? b : bover);
455 		sib = di_sibling_node(sib);
456 	}
457 	if (pb < 0 && bover < 0)
458 		return (0);
459 	if (rc >= 0)
460 		bn = pciexbus_declare(mod, parent, pn, ((pb < 0) ? bover : pb));
461 	else
462 		bn = pcibus_declare(mod, parent, pn, ((pb < 0) ? bover : pb));
463 	if (bn == NULL)
464 		return (-1);
465 	if (pb < 0)
466 		return (0);
467 
468 	for (d = 0; d < MAX_PCIBUS_DEVS; d++) {
469 		for (f = 0; f < MAX_PCIDEV_FNS; f++) {
470 			if (pps[d][f] == NULL)
471 				continue;
472 			din = did_dinode(pps[d][f]);
473 
474 			/*
475 			 * Try to enumerate as many devices and functions as
476 			 * possible.  If we fail to declare a device, break
477 			 * out of the function loop.
478 			 */
479 			declare_dev_and_fn(mod, bn,
480 			    &dn, din, board, bridge, rc, d, f, depth);
481 			did_rele(pps[d][f]);
482 
483 			if (dn == NULL)
484 				break;
485 		}
486 		dn = NULL;
487 	}
488 	return (0);
489 }
490 
491 static int
492 pciexbus_enum(topo_mod_t *mp, tnode_t *ptn, char *pnm, topo_instance_t min,
493     topo_instance_t max)
494 {
495 	di_node_t pdn;
496 	int rc;
497 	int retval;
498 
499 	/*
500 	 * PCI-Express; root complex shares the hostbridge's instance
501 	 * number.  Parent node's private data is a simple di_node_t
502 	 * and we have to construct our own did hash and did_t.
503 	 */
504 	rc = topo_node_instance(ptn);
505 
506 	if ((pdn = topo_node_getspecific(ptn)) == DI_NODE_NIL) {
507 		topo_mod_dprintf(mp,
508 		    "Parent %s node missing private data.\n"
509 		    "Unable to proceed with %s enumeration.\n", pnm, PCIEX_BUS);
510 		return (0);
511 	}
512 	did_hash_init(mp);
513 	if ((did_create(mp, pdn, 0, 0, rc, TRUST_BDF)) == NULL)
514 		return (-1);	/* errno already set */
515 
516 	retval = pci_children_instantiate(mp, ptn, pdn, 0, 0, rc,
517 	    (min == max) ? min : TRUST_BDF, 0);
518 	did_hash_fini(mp);
519 
520 	return (retval);
521 }
522 
523 static int
524 pcibus_enum(topo_mod_t *mp, tnode_t *ptn, char *pnm, topo_instance_t min,
525     topo_instance_t max, void *data)
526 {
527 	did_t *didp, *hbdid = (did_t *)data;
528 	int retval;
529 
530 	/*
531 	 * XXTOPO: we should not be sharing private node data with another
532 	 * module. PCI Bus; Parent node's private data is a did_t.  We'll
533 	 * use the did hash established by the parent.
534 	 */
535 	did_setspecific(mp, data);
536 
537 	/*
538 	 * If we're looking for a specific bus-instance, find the right
539 	 * did_t in the chain, otherwise, there should be only one did_t.
540 	 */
541 	if (min == max) {
542 		int b;
543 		didp = hbdid;
544 		while (didp != NULL) {
545 			did_BDF(didp, &b, NULL, NULL);
546 			if (b == min)
547 				break;
548 			didp = did_link_get(didp);
549 		}
550 		if (didp == NULL) {
551 			topo_mod_dprintf(mp,
552 			    "Parent %s node missing private data related\n"
553 			    "to %s instance %d.\n", pnm, PCI_BUS, min);
554 			topo_mod_setspecific(mp, NULL);
555 			return (0);
556 		}
557 	} else {
558 		assert(did_link_get(hbdid) == NULL);
559 		didp = hbdid;
560 	}
561 	retval = pci_children_instantiate(mp, ptn, did_dinode(didp),
562 	    did_board(didp), did_bridge(didp), did_rc(didp),
563 	    (min == max) ? min : TRUST_BDF, 0);
564 
565 	topo_mod_setspecific(mp, NULL);
566 
567 	return (retval);
568 }
569 
570 /*ARGSUSED*/
571 static int
572 pci_enum(topo_mod_t *mod, tnode_t *ptn, const char *name,
573     topo_instance_t min, topo_instance_t max, void *notused, void *data)
574 {
575 	int retval;
576 	char *pname;
577 
578 	topo_mod_dprintf(mod, "Enumerating pci!\n");
579 
580 	if (strcmp(name, PCI_BUS) != 0 && strcmp(name, PCIEX_BUS) != 0) {
581 		topo_mod_dprintf(mod,
582 		    "Currently only know how to enumerate %s or %s.\n",
583 		    PCI_BUS, PCIEX_BUS);
584 		return (0);
585 	}
586 	pname = topo_node_name(ptn);
587 	if (strcmp(pname, HOSTBRIDGE) != 0 && strcmp(pname, PCIEX_ROOT) != 0) {
588 		topo_mod_dprintf(mod,
589 		    "Currently can only enumerate a %s or %s directly\n",
590 		    PCI_BUS, PCIEX_BUS);
591 		topo_mod_dprintf(mod,
592 		    "descended from a %s or %s node.\n",
593 		    HOSTBRIDGE, PCIEX_ROOT);
594 		return (0);
595 	}
596 
597 	if (strcmp(name, PCI_BUS) == 0) {
598 		retval = pcibus_enum(mod, ptn, pname, min, max, data);
599 	} else if (strcmp(name, PCIEX_BUS) == 0) {
600 		retval = pciexbus_enum(mod, ptn, pname, min, max);
601 	} else {
602 		topo_mod_dprintf(mod,
603 		    "Currently only know how to enumerate %s or %s not %s.\n",
604 		    PCI_BUS, PCIEX_BUS, name);
605 		return (0);
606 	}
607 
608 	return (retval);
609 }
610 
611 /*ARGSUSED*/
612 static void
613 pci_release(topo_mod_t *mp, tnode_t *node)
614 {
615 	topo_method_unregister_all(mp, node);
616 
617 	/*
618 	 * node private data (did_t) for this node is destroyed in
619 	 * did_hash_destroy()
620 	 */
621 
622 	topo_node_unbind(node);
623 }
624