7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi/*
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * CDDL HEADER START
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi *
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * The contents of this file are subject to the terms of the
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * Common Development and Distribution License (the "License").
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * You may not use this file except in compliance with the License.
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi *
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * or http://www.opensolaris.org/os/licensing.
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * See the License for the specific language governing permissions
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * and limitations under the License.
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi *
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * When distributing Covered Code, include this CDDL HEADER in each
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * If applicable, add the following below this CDDL HEADER, with the
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * fields enclosed by brackets "[]" replaced with your own identifying
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * information: Portions Copyright [yyyy] [name of copyright owner]
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi *
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * CDDL HEADER END
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi */
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi/*
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * Use is subject to license terms.
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi */
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#pragma ident "%Z%%M% %I% %E% SMI"
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi#include <fm/topo_hc.h>
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi#include <hb_sun4.h>
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi#include <hostbridge.h>
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi#include <pcibus.h>
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi#include <util.h>
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindiint
0eb822a1c0c2bea495647510b75f77f0e57633ebcindicount_busorrc(topo_mod_t *mod, busorrc_t *list, int *hbc, int *bph)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi{
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi ulong_t start;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi busorrc_t *p;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi int bt;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi start = list->br_ba_ac;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi p = list->br_nextbus;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi bt = *hbc = 1;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi while (p != NULL) {
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi if (p->br_ba_ac == start)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi (*hbc)++;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi bt++;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi p = p->br_nextbus;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi }
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi /*
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * sanity check that we have the correct number of buses/root
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * complexes in the list to have the same number of buses on
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * each hostbridge
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi */
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi if (bt % *hbc != 0) {
724365f7556fc4201fdb11766ebc6bd918523130sethg topo_mod_dprintf(mod,
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi "Imbalance between bus/root complex count and "
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi "the number of hostbridges.\n");
724365f7556fc4201fdb11766ebc6bd918523130sethg return (topo_mod_seterrno(mod, EMOD_PARTIAL_ENUM));
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi }
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi *bph = bt / *hbc;
724365f7556fc4201fdb11766ebc6bd918523130sethg topo_mod_dprintf(mod,
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi "%d hostbridge%s\n", *hbc, (*hbc > 1) ? "s." : ".");
724365f7556fc4201fdb11766ebc6bd918523130sethg topo_mod_dprintf(mod, "%d buses total.\n", bt);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi return (0);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi}
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindistatic int
0eb822a1c0c2bea495647510b75f77f0e57633ebcindibusorrc_process(topo_mod_t *mod, busorrc_t *list, int isrc, tnode_t *ptn)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi{
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi int hbc, busper;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi if (list == NULL) {
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi if (isrc == 1)
724365f7556fc4201fdb11766ebc6bd918523130sethg topo_mod_dprintf(mod, "No root complexes found.\n");
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi else
724365f7556fc4201fdb11766ebc6bd918523130sethg topo_mod_dprintf(mod, "No pci buses found.\n");
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi return (0);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi }
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi /*
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * At this point we've looked through all the top-level device
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * tree nodes for instances of drivers that represent logical
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * PCI buses or root complexes. We've sorted them into a
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * list, ordered by "bus address". We retrieved "bus address"
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * using di_bus_addr(). That gave us a string that contains
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * either a single hex number or a pair of them separated by a
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * comma. If there was a single number, we've assumed the
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * second number to be zero.
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi *
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * So, we always have a pair of numbers describing a bus/root
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * complex, X1 and X2, with X1 being the number before the
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * comma, and X2 being the number after (or the assumed zero).
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * As each node was examined, we then sorted these buses/root
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * complexes, first by the value of X2, and using X1 to order
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * amongst buses/root complexes with the same value for X2.
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi *
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * We infer the existence of hostbridges by observing a
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * pattern that X2 is recycled for different hostbridges, and
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * that sorting by X1 within buses/root complexes with equal
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * values of X2 maintains the correct associations of
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * buses/root complexes and bridges.
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi */
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi if (count_busorrc(mod, list, &hbc, &busper) < 0)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi return (-1);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi if (isrc == 1)
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi return (declare_exbuses(mod, list, ptn, hbc, busper));
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi else
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi return (declare_buses(mod, list, ptn, hbc));
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi}
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindistatic int
0eb822a1c0c2bea495647510b75f77f0e57633ebcindipci_hostbridges_find(topo_mod_t *mod, tnode_t *ptn)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi{
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi busorrc_t *buses = NULL;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi busorrc_t *rcs = NULL;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi di_node_t devtree;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi di_node_t pnode;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi /* Scan for buses, top-level devinfo nodes with the right driver */
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi devtree = topo_mod_devinfo(mod);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi if (devtree == DI_NODE_NIL) {
724365f7556fc4201fdb11766ebc6bd918523130sethg topo_mod_dprintf(mod, "devinfo init failed.");
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi topo_node_range_destroy(ptn, HOSTBRIDGE);
724365f7556fc4201fdb11766ebc6bd918523130sethg return (topo_mod_seterrno(mod, EMOD_PARTIAL_ENUM));
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi }
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi pnode = di_drv_first_node(PCI, devtree);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi while (pnode != DI_NODE_NIL) {
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi if (busorrc_add(mod, &buses, pnode) < 0) {
724365f7556fc4201fdb11766ebc6bd918523130sethg return (topo_mod_seterrno(mod, EMOD_PARTIAL_ENUM));
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi }
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi pnode = di_drv_next_node(pnode);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi }
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi pnode = di_drv_first_node(PSYCHO, devtree);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi while (pnode != DI_NODE_NIL) {
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi if (busorrc_add(mod, &buses, pnode) < 0) {
724365f7556fc4201fdb11766ebc6bd918523130sethg return (topo_mod_seterrno(mod, EMOD_PARTIAL_ENUM));
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi }
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi pnode = di_drv_next_node(pnode);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi }
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi pnode = di_drv_first_node(SCHIZO, devtree);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi while (pnode != DI_NODE_NIL) {
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi if (busorrc_add(mod, &buses, pnode) < 0) {
724365f7556fc4201fdb11766ebc6bd918523130sethg return (topo_mod_seterrno(mod, EMOD_PARTIAL_ENUM));
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi }
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi pnode = di_drv_next_node(pnode);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi }
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi pnode = di_drv_first_node(PX, devtree);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi while (pnode != DI_NODE_NIL) {
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi if (busorrc_add(mod, &rcs, pnode) < 0) {
724365f7556fc4201fdb11766ebc6bd918523130sethg return (topo_mod_seterrno(mod, EMOD_PARTIAL_ENUM));
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi }
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi pnode = di_drv_next_node(pnode);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi }
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi if (busorrc_process(mod, buses, 0, ptn) < 0)
724365f7556fc4201fdb11766ebc6bd918523130sethg return (topo_mod_seterrno(mod, EMOD_PARTIAL_ENUM));
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi if (busorrc_process(mod, rcs, 1, ptn) < 0)
724365f7556fc4201fdb11766ebc6bd918523130sethg return (topo_mod_seterrno(mod, EMOD_PARTIAL_ENUM));
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi busorrc_free(mod, buses);
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi busorrc_free(mod, rcs);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi return (0);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi}
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi/*ARGSUSED*/
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindiint
0eb822a1c0c2bea495647510b75f77f0e57633ebcindiplatform_hb_enum(topo_mod_t *mod, tnode_t *parent, const char *name,
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi topo_instance_t imin, topo_instance_t imax)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi{
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi return (pci_hostbridges_find(mod, parent));
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi}
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi/*ARGSUSED*/
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindiint
0eb822a1c0c2bea495647510b75f77f0e57633ebcindiplatform_hb_label(topo_mod_t *mod, tnode_t *node, nvlist_t *in, nvlist_t **out)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi{
724365f7556fc4201fdb11766ebc6bd918523130sethg return (labelmethod_inherit(mod, node, in, out));
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi}