xref: /onnv-gate/usr/src/lib/fm/topo/modules/common/ses/ses.c (revision 12830:30a30d80e478)
16869Seschrock /*
26869Seschrock  * CDDL HEADER START
36869Seschrock  *
46869Seschrock  * The contents of this file are subject to the terms of the
56869Seschrock  * Common Development and Distribution License (the "License").
66869Seschrock  * You may not use this file except in compliance with the License.
76869Seschrock  *
86869Seschrock  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
96869Seschrock  * or http://www.opensolaris.org/os/licensing.
106869Seschrock  * See the License for the specific language governing permissions
116869Seschrock  * and limitations under the License.
126869Seschrock  *
136869Seschrock  * When distributing Covered Code, include this CDDL HEADER in each
146869Seschrock  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
156869Seschrock  * If applicable, add the following below this CDDL HEADER, with the
166869Seschrock  * fields enclosed by brackets "[]" replaced with your own identifying
176869Seschrock  * information: Portions Copyright [yyyy] [name of copyright owner]
186869Seschrock  *
196869Seschrock  * CDDL HEADER END
206869Seschrock  */
216869Seschrock 
226869Seschrock /*
2312106SStephen.Hanson@Sun.COM  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
246869Seschrock  */
256869Seschrock 
269296SEric.Schrock@Sun.COM #include <alloca.h>
276869Seschrock #include <dirent.h>
286869Seschrock #include <devid.h>
296869Seschrock #include <fm/libdiskstatus.h>
306869Seschrock #include <inttypes.h>
316869Seschrock #include <pthread.h>
326869Seschrock #include <strings.h>
3312126SHyon.Kim@Sun.COM #include <string.h>
346869Seschrock #include <unistd.h>
356869Seschrock #include <sys/dkio.h>
366869Seschrock #include <sys/fm/protocol.h>
3712106SStephen.Hanson@Sun.COM #include <sys/libdevid.h>
386869Seschrock #include <sys/scsi/scsi_types.h>
3912126SHyon.Kim@Sun.COM #include <sys/byteorder.h>
4012357SStephen.Hanson@Sun.COM #include <pthread.h>
4112357SStephen.Hanson@Sun.COM #include <signal.h>
4212357SStephen.Hanson@Sun.COM #include <fcntl.h>
4312357SStephen.Hanson@Sun.COM #include <sys/ctfs.h>
4412357SStephen.Hanson@Sun.COM #include <libcontract.h>
4512357SStephen.Hanson@Sun.COM #include <poll.h>
4612357SStephen.Hanson@Sun.COM #include <sys/contract/device.h>
4712357SStephen.Hanson@Sun.COM #include <libsysevent.h>
4812357SStephen.Hanson@Sun.COM #include <sys/sysevent/eventdefs.h>
4912632SHyon.Kim@Sun.COM #include <scsi/plugins/ses/vendor/sun.h>
507269Seschrock 
516869Seschrock #include "disk.h"
527269Seschrock #include "ses.h"
536869Seschrock 
546869Seschrock #define	SES_VERSION	1
556869Seschrock 
5612126SHyon.Kim@Sun.COM #define	SES_STARTING_SUBCHASSIS 256	/* valid subchassis IDs are uint8_t */
5712126SHyon.Kim@Sun.COM #define	NO_SUBCHASSIS	((uint64_t)-1)
5812126SHyon.Kim@Sun.COM 
5910527SEric.Schrock@Sun.COM static int ses_snap_freq = 250;		/* in milliseconds */
606869Seschrock 
617269Seschrock #define	SES_STATUS_UNAVAIL(s)	\
628344SEric.Schrock@Sun.COM 	((s) == SES_ESC_UNSUPPORTED || (s) >= SES_ESC_NOT_INSTALLED)
636869Seschrock 
6412632SHyon.Kim@Sun.COM #define	HR_SECOND   1000000000
6512632SHyon.Kim@Sun.COM 
666869Seschrock /*
676869Seschrock  * Because multiple SES targets can be part of a single chassis, we construct
686869Seschrock  * our own hierarchy that takes this into account.  These SES targets may refer
696869Seschrock  * to the same devices (multiple paths) or to different devices (managing
706869Seschrock  * different portions of the space).  We arrange things into a
716869Seschrock  * ses_enum_enclosure_t, which contains a set of ses targets, and a list of all
726869Seschrock  * nodes found so far.
736869Seschrock  */
748344SEric.Schrock@Sun.COM typedef struct ses_alt_node {
758344SEric.Schrock@Sun.COM 	topo_list_t		san_link;
768344SEric.Schrock@Sun.COM 	ses_node_t		*san_node;
778344SEric.Schrock@Sun.COM } ses_alt_node_t;
786869Seschrock 
796869Seschrock typedef struct ses_enum_node {
806869Seschrock 	topo_list_t		sen_link;
816869Seschrock 	ses_node_t		*sen_node;
828344SEric.Schrock@Sun.COM 	topo_list_t		sen_alt_nodes;
836869Seschrock 	uint64_t		sen_type;
846869Seschrock 	uint64_t		sen_instance;
856869Seschrock 	ses_enum_target_t	*sen_target;
866869Seschrock } ses_enum_node_t;
876869Seschrock 
886869Seschrock typedef struct ses_enum_chassis {
896869Seschrock 	topo_list_t		sec_link;
9010519SSundeep.Panicker@Sun.COM 	topo_list_t		sec_subchassis;
916869Seschrock 	topo_list_t		sec_nodes;
926869Seschrock 	topo_list_t		sec_targets;
936869Seschrock 	const char		*sec_csn;
946869Seschrock 	ses_node_t		*sec_enclosure;
956869Seschrock 	ses_enum_target_t	*sec_target;
966869Seschrock 	topo_instance_t		sec_instance;
9710519SSundeep.Panicker@Sun.COM 	topo_instance_t		sec_scinstance;
9812126SHyon.Kim@Sun.COM 	topo_instance_t		sec_maxinstance;
996869Seschrock 	boolean_t		sec_hasdev;
1007269Seschrock 	boolean_t		sec_internal;
1016869Seschrock } ses_enum_chassis_t;
1026869Seschrock 
1036869Seschrock typedef struct ses_enum_data {
10412126SHyon.Kim@Sun.COM 	topo_list_t		sed_devs;
1056869Seschrock 	topo_list_t		sed_chassis;
1066869Seschrock 	ses_enum_chassis_t	*sed_current;
1076869Seschrock 	ses_enum_target_t	*sed_target;
1086869Seschrock 	int			sed_errno;
1096869Seschrock 	char			*sed_name;
1106869Seschrock 	topo_mod_t		*sed_mod;
1116869Seschrock 	topo_instance_t		sed_instance;
1126869Seschrock } ses_enum_data_t;
1136869Seschrock 
11412126SHyon.Kim@Sun.COM typedef struct sas_connector_phy_data {
11512632SHyon.Kim@Sun.COM 	uint64_t    scpd_index;
11612632SHyon.Kim@Sun.COM 	uint64_t    scpd_pm;
11712126SHyon.Kim@Sun.COM } sas_connector_phy_data_t;
11812126SHyon.Kim@Sun.COM 
11912126SHyon.Kim@Sun.COM typedef struct sas_connector_type {
12012632SHyon.Kim@Sun.COM 	uint64_t    sct_type;
12112632SHyon.Kim@Sun.COM 	char	    *sct_name;
12212126SHyon.Kim@Sun.COM } sas_connector_type_t;
12312126SHyon.Kim@Sun.COM 
12412126SHyon.Kim@Sun.COM static const sas_connector_type_t sas_connector_type_list[] = {
12512126SHyon.Kim@Sun.COM 	{   0x0, "Information unknown"  },
12612126SHyon.Kim@Sun.COM 	{   0x1, "External SAS 4x receptacle (see SAS-2 and SFF-8470)"	},
12712126SHyon.Kim@Sun.COM 	{   0x2, "Exteranl Mini SAS 4x receptacle (see SAS-2 and SFF-8088)" },
12812126SHyon.Kim@Sun.COM 	{   0xF, "Vendor-specific external connector"	},
12912126SHyon.Kim@Sun.COM 	{   0x10, "Internal wide SAS 4i plug (see SAS-2 and SFF-8484)"	},
13012126SHyon.Kim@Sun.COM 	{   0x11,
13112126SHyon.Kim@Sun.COM 	"Internal wide Mini SAS 4i receptacle (see SAS-2 and SFF-8087)"	},
13212126SHyon.Kim@Sun.COM 	{   0x20, "Internal SAS Drive receptacle (see SAS-2 and SFF-8482)"  },
13312126SHyon.Kim@Sun.COM 	{   0x21, "Internal SATA host plug (see SAS-2 and SATA-2)"  },
13412126SHyon.Kim@Sun.COM 	{   0x22, "Internal SAS Drive plug (see SAS-2 and SFF-8482)"	},
13512126SHyon.Kim@Sun.COM 	{   0x23, "Internal SATA device plug (see SAS-2 and SATA-2)"	},
13612126SHyon.Kim@Sun.COM 	{   0x2F, "Internal SAS virtual connector"  },
13712126SHyon.Kim@Sun.COM 	{   0x3F, "Vendor-specific internal connector"	},
13812126SHyon.Kim@Sun.COM 	{   0x70, "Other Vendor-specific connector"	},
13912126SHyon.Kim@Sun.COM 	{   0x71, "Other Vendor-specific connector"	},
14012126SHyon.Kim@Sun.COM 	{   0x72, "Other Vendor-specific connector"	},
14112126SHyon.Kim@Sun.COM 	{   0x73, "Other Vendor-specific connector"	},
14212126SHyon.Kim@Sun.COM 	{   0x74, "Other Vendor-specific connector"	},
14312126SHyon.Kim@Sun.COM 	{   0x75, "Other Vendor-specific connector"	},
14412126SHyon.Kim@Sun.COM 	{   0x76, "Other Vendor-specific connector"	},
14512126SHyon.Kim@Sun.COM 	{   0x77, "Other Vendor-specific connector"	},
14612126SHyon.Kim@Sun.COM 	{   0x78, "Other Vendor-specific connector"	},
14712126SHyon.Kim@Sun.COM 	{   0x79, "Other Vendor-specific connector"	},
14812126SHyon.Kim@Sun.COM 	{   0x7A, "Other Vendor-specific connector"	},
14912126SHyon.Kim@Sun.COM 	{   0x7B, "Other Vendor-specific connector"	},
15012126SHyon.Kim@Sun.COM 	{   0x7C, "Other Vendor-specific connector"	},
15112126SHyon.Kim@Sun.COM 	{   0x7D, "Other Vendor-specific connector"	},
15212126SHyon.Kim@Sun.COM 	{   0x7E, "Other Vendor-specific connector"	},
15312126SHyon.Kim@Sun.COM 	{   0x7F, "Other Vendor-specific connector"	},
15412126SHyon.Kim@Sun.COM 	{   0x80, "Not Defined"	}
15512126SHyon.Kim@Sun.COM };
15612126SHyon.Kim@Sun.COM 
15712126SHyon.Kim@Sun.COM #define	SAS_CONNECTOR_TYPE_CODE_NOT_DEFINED  0x80
15812126SHyon.Kim@Sun.COM #define	SAS_CONNECTOR_TYPE_NOT_DEFINED \
15912126SHyon.Kim@Sun.COM 	"Connector type not definedi by SES-2 standard"
16012126SHyon.Kim@Sun.COM #define	SAS_CONNECTOR_TYPE_RESERVED \
16112126SHyon.Kim@Sun.COM 	"Connector type reserved by SES-2 standard"
16212126SHyon.Kim@Sun.COM 
16312632SHyon.Kim@Sun.COM typedef struct phys_enum_type {
16412632SHyon.Kim@Sun.COM 	uint64_t    pet_type;
16512632SHyon.Kim@Sun.COM 	char	    *pet_nodename;
16612632SHyon.Kim@Sun.COM 	char	    *pet_defaultlabel;
16712632SHyon.Kim@Sun.COM 	boolean_t   pet_dorange;
16812632SHyon.Kim@Sun.COM } phys_enum_type_t;
16912632SHyon.Kim@Sun.COM 
17012632SHyon.Kim@Sun.COM static const phys_enum_type_t phys_enum_type_list[] = {
17112632SHyon.Kim@Sun.COM 	{   SES_ET_ARRAY_DEVICE, BAY, "BAY", B_TRUE  },
17212632SHyon.Kim@Sun.COM 	{   SES_ET_COOLING, FAN, "FAN", B_TRUE  },
17312632SHyon.Kim@Sun.COM 	{   SES_ET_DEVICE, BAY, "BAY", B_TRUE  },
17412632SHyon.Kim@Sun.COM 	{   SES_ET_ESC_ELECTRONICS, CONTROLLER, "CONTROLLER", B_TRUE  },
17512632SHyon.Kim@Sun.COM 	{   SES_ET_POWER_SUPPLY, PSU, "PSU", B_TRUE  },
17612632SHyon.Kim@Sun.COM 	{   SES_ET_SUNW_FANBOARD, FANBOARD, "FANBOARD", B_TRUE  },
17712632SHyon.Kim@Sun.COM 	{   SES_ET_SUNW_FANMODULE, FANMODULE, "FANMODULE", B_TRUE  },
17812632SHyon.Kim@Sun.COM 	{   SES_ET_SUNW_POWERBOARD, POWERBOARD, "POWERBOARD", B_TRUE  },
17912632SHyon.Kim@Sun.COM 	{   SES_ET_SUNW_POWERMODULE, POWERMODULE, "POWERMODULE", B_TRUE  }
18012632SHyon.Kim@Sun.COM };
18112632SHyon.Kim@Sun.COM 
18212632SHyon.Kim@Sun.COM #define	N_PHYS_ENUM_TYPES (sizeof (phys_enum_type_list) / \
18312632SHyon.Kim@Sun.COM 	sizeof (phys_enum_type_list[0]))
18412632SHyon.Kim@Sun.COM 
18512632SHyon.Kim@Sun.COM /*
18612632SHyon.Kim@Sun.COM  * Structure for the hierarchical tree for element nodes.
18712632SHyon.Kim@Sun.COM  */
18812632SHyon.Kim@Sun.COM typedef struct ses_phys_tree {
18912632SHyon.Kim@Sun.COM     ses_node_t	*spt_snode;
19012632SHyon.Kim@Sun.COM     ses_enum_node_t	*spt_senumnode;
19112632SHyon.Kim@Sun.COM     boolean_t	spt_isfru;
19212632SHyon.Kim@Sun.COM     uint64_t	spt_eonlyindex;
19312632SHyon.Kim@Sun.COM     uint64_t	spt_cindex;
19412632SHyon.Kim@Sun.COM     uint64_t	spt_pindex;
19512632SHyon.Kim@Sun.COM     uint64_t	spt_maxinst;
19612632SHyon.Kim@Sun.COM     struct ses_phys_tree    *spt_parent;
19712632SHyon.Kim@Sun.COM     struct ses_phys_tree    *spt_child;
19812632SHyon.Kim@Sun.COM     struct ses_phys_tree    *spt_sibling;
19912632SHyon.Kim@Sun.COM     tnode_t	*spt_tnode;
20012632SHyon.Kim@Sun.COM } ses_phys_tree_t;
20112632SHyon.Kim@Sun.COM 
20210519SSundeep.Panicker@Sun.COM typedef enum {
20310519SSundeep.Panicker@Sun.COM 	SES_NEW_CHASSIS		= 0x1,
20410519SSundeep.Panicker@Sun.COM 	SES_NEW_SUBCHASSIS	= 0x2,
20510519SSundeep.Panicker@Sun.COM 	SES_DUP_CHASSIS		= 0x4,
20610519SSundeep.Panicker@Sun.COM 	SES_DUP_SUBCHASSIS	= 0x8
20710519SSundeep.Panicker@Sun.COM } ses_chassis_type_e;
20810519SSundeep.Panicker@Sun.COM 
20912632SHyon.Kim@Sun.COM 
21012477SHyon.Kim@Sun.COM static const topo_pgroup_info_t storage_pgroup = {
21112477SHyon.Kim@Sun.COM 	TOPO_PGROUP_STORAGE,
21212126SHyon.Kim@Sun.COM 	TOPO_STABILITY_PRIVATE,
21312126SHyon.Kim@Sun.COM 	TOPO_STABILITY_PRIVATE,
21412126SHyon.Kim@Sun.COM 	1
21512126SHyon.Kim@Sun.COM };
21612126SHyon.Kim@Sun.COM 
21712477SHyon.Kim@Sun.COM static const topo_pgroup_info_t smp_pgroup = {
21812477SHyon.Kim@Sun.COM 	TOPO_PGROUP_SMP,
21912126SHyon.Kim@Sun.COM 	TOPO_STABILITY_PRIVATE,
22012126SHyon.Kim@Sun.COM 	TOPO_STABILITY_PRIVATE,
22112126SHyon.Kim@Sun.COM 	1
22212126SHyon.Kim@Sun.COM };
22312126SHyon.Kim@Sun.COM 
22412563SHyon.Kim@Sun.COM static const topo_pgroup_info_t ses_pgroup = {
22512563SHyon.Kim@Sun.COM 	TOPO_PGROUP_SES,
22612563SHyon.Kim@Sun.COM 	TOPO_STABILITY_PRIVATE,
22712563SHyon.Kim@Sun.COM 	TOPO_STABILITY_PRIVATE,
22812563SHyon.Kim@Sun.COM 	1
22912563SHyon.Kim@Sun.COM };
23012563SHyon.Kim@Sun.COM 
2316869Seschrock static int ses_present(topo_mod_t *, tnode_t *, topo_version_t, nvlist_t *,
2326869Seschrock     nvlist_t **);
2336869Seschrock static int ses_contains(topo_mod_t *, tnode_t *, topo_version_t, nvlist_t *,
2346869Seschrock     nvlist_t **);
2356869Seschrock 
2366869Seschrock static const topo_method_t ses_component_methods[] = {
2376869Seschrock 	{ TOPO_METH_PRESENT, TOPO_METH_PRESENT_DESC,
2386869Seschrock 	    TOPO_METH_PRESENT_VERSION0, TOPO_STABILITY_INTERNAL, ses_present },
2397269Seschrock 	{ TOPO_METH_FAC_ENUM, TOPO_METH_FAC_ENUM_DESC, 0,
2407269Seschrock 	    TOPO_STABILITY_INTERNAL, ses_node_enum_facility },
24110234SRobert.Johnston@Sun.COM 	{ TOPO_METH_SENSOR_FAILURE, TOPO_METH_SENSOR_FAILURE_DESC,
24210234SRobert.Johnston@Sun.COM 	    TOPO_METH_SENSOR_FAILURE_VERSION, TOPO_STABILITY_INTERNAL,
24310234SRobert.Johnston@Sun.COM 	    topo_method_sensor_failure },
2447269Seschrock 	{ NULL }
2457269Seschrock };
2467269Seschrock 
2477269Seschrock static const topo_method_t ses_bay_methods[] = {
2487269Seschrock 	{ TOPO_METH_FAC_ENUM, TOPO_METH_FAC_ENUM_DESC, 0,
2497269Seschrock 	    TOPO_STABILITY_INTERNAL, ses_node_enum_facility },
2506869Seschrock 	{ NULL }
2516869Seschrock };
2526869Seschrock 
2536869Seschrock static const topo_method_t ses_enclosure_methods[] = {
2546869Seschrock 	{ TOPO_METH_CONTAINS, TOPO_METH_CONTAINS_DESC,
2556869Seschrock 	    TOPO_METH_CONTAINS_VERSION, TOPO_STABILITY_INTERNAL, ses_contains },
2567269Seschrock 	{ TOPO_METH_FAC_ENUM, TOPO_METH_FAC_ENUM_DESC, 0,
2577269Seschrock 	    TOPO_STABILITY_INTERNAL, ses_enc_enum_facility },
2586869Seschrock 	{ NULL }
2596869Seschrock };
2606869Seschrock 
26112357SStephen.Hanson@Sun.COM /*
26212357SStephen.Hanson@Sun.COM  * Functions for tracking ses devices which we were unable to open. We retry
26312357SStephen.Hanson@Sun.COM  * these at regular intervals using ses_recheck_dir() and if we find that we
26412357SStephen.Hanson@Sun.COM  * can now open any of them then we send a sysevent to indicate that a new topo
26512357SStephen.Hanson@Sun.COM  * snapshot should be taken.
26612357SStephen.Hanson@Sun.COM  */
26712357SStephen.Hanson@Sun.COM typedef struct ses_open_fail_list {
26812357SStephen.Hanson@Sun.COM 	struct ses_open_fail_list	*sof_next;
26912357SStephen.Hanson@Sun.COM 	char				*sof_path;
27012357SStephen.Hanson@Sun.COM } ses_open_fail_list_t;
27112357SStephen.Hanson@Sun.COM 
27212357SStephen.Hanson@Sun.COM static ses_open_fail_list_t *ses_sofh;
27312357SStephen.Hanson@Sun.COM static pthread_mutex_t ses_sofmt;
27412502SStephen.Hanson@Sun.COM static void ses_ct_print(char *ptr);
27512357SStephen.Hanson@Sun.COM 
27612357SStephen.Hanson@Sun.COM static void
ses_recheck_dir()27712502SStephen.Hanson@Sun.COM ses_recheck_dir()
27812357SStephen.Hanson@Sun.COM {
27912357SStephen.Hanson@Sun.COM 	ses_target_t *target;
28012357SStephen.Hanson@Sun.COM 	sysevent_id_t eid;
28112502SStephen.Hanson@Sun.COM 	char buf[80];
28212357SStephen.Hanson@Sun.COM 	ses_open_fail_list_t *sof;
28312357SStephen.Hanson@Sun.COM 
28412357SStephen.Hanson@Sun.COM 	/*
28512357SStephen.Hanson@Sun.COM 	 * check list of "unable to open" devices
28612357SStephen.Hanson@Sun.COM 	 */
28712357SStephen.Hanson@Sun.COM 	(void) pthread_mutex_lock(&ses_sofmt);
28812357SStephen.Hanson@Sun.COM 	for (sof = ses_sofh; sof != NULL; sof = sof->sof_next) {
28912357SStephen.Hanson@Sun.COM 		/*
29012357SStephen.Hanson@Sun.COM 		 * see if we can open it now
29112357SStephen.Hanson@Sun.COM 		 */
29212357SStephen.Hanson@Sun.COM 		if ((target = ses_open(LIBSES_VERSION,
29312357SStephen.Hanson@Sun.COM 		    sof->sof_path)) == NULL) {
29412502SStephen.Hanson@Sun.COM 			(void) snprintf(buf, sizeof (buf),
29512357SStephen.Hanson@Sun.COM 			    "recheck_dir - still can't open %s", sof->sof_path);
29612502SStephen.Hanson@Sun.COM 			ses_ct_print(buf);
29712357SStephen.Hanson@Sun.COM 			continue;
29812357SStephen.Hanson@Sun.COM 		}
29912357SStephen.Hanson@Sun.COM 
30012357SStephen.Hanson@Sun.COM 		/*
30112357SStephen.Hanson@Sun.COM 		 * ok - better force a new snapshot
30212357SStephen.Hanson@Sun.COM 		 */
30312502SStephen.Hanson@Sun.COM 		(void) snprintf(buf, sizeof (buf),
30412357SStephen.Hanson@Sun.COM 		    "recheck_dir - can now open %s", sof->sof_path);
30512502SStephen.Hanson@Sun.COM 		ses_ct_print(buf);
30612357SStephen.Hanson@Sun.COM 		(void) sysevent_post_event(EC_PLATFORM, ESC_PLATFORM_SP_RESET,
30712357SStephen.Hanson@Sun.COM 		    SUNW_VENDOR, "fmd", NULL, &eid);
30812357SStephen.Hanson@Sun.COM 		ses_close(target);
30912357SStephen.Hanson@Sun.COM 		break;
31012357SStephen.Hanson@Sun.COM 	}
31112357SStephen.Hanson@Sun.COM 	(void) pthread_mutex_unlock(&ses_sofmt);
31212357SStephen.Hanson@Sun.COM }
31312357SStephen.Hanson@Sun.COM 
31412357SStephen.Hanson@Sun.COM static void
ses_sof_alloc(topo_mod_t * mod,char * path)31512357SStephen.Hanson@Sun.COM ses_sof_alloc(topo_mod_t *mod, char *path)
31612357SStephen.Hanson@Sun.COM {
31712357SStephen.Hanson@Sun.COM 	ses_open_fail_list_t *sof;
31812357SStephen.Hanson@Sun.COM 
31912357SStephen.Hanson@Sun.COM 	(void) pthread_mutex_lock(&ses_sofmt);
32012357SStephen.Hanson@Sun.COM 	sof = topo_mod_zalloc(mod, sizeof (*sof));
32112357SStephen.Hanson@Sun.COM 	topo_mod_dprintf(mod, "sof_alloc %s", path);
32212357SStephen.Hanson@Sun.COM 	sof->sof_path = path;
32312357SStephen.Hanson@Sun.COM 	sof->sof_next = ses_sofh;
32412357SStephen.Hanson@Sun.COM 	ses_sofh = sof;
32512357SStephen.Hanson@Sun.COM 	(void) pthread_mutex_unlock(&ses_sofmt);
32612357SStephen.Hanson@Sun.COM }
32712357SStephen.Hanson@Sun.COM 
32812357SStephen.Hanson@Sun.COM static void
ses_sof_freeall(topo_mod_t * mod)32912357SStephen.Hanson@Sun.COM ses_sof_freeall(topo_mod_t *mod)
33012357SStephen.Hanson@Sun.COM {
33112357SStephen.Hanson@Sun.COM 	ses_open_fail_list_t *sof, *next_sof;
33212357SStephen.Hanson@Sun.COM 
33312357SStephen.Hanson@Sun.COM 	(void) pthread_mutex_lock(&ses_sofmt);
33412357SStephen.Hanson@Sun.COM 	for (sof = ses_sofh; sof != NULL; sof = next_sof) {
33512357SStephen.Hanson@Sun.COM 		next_sof = sof->sof_next;
33612357SStephen.Hanson@Sun.COM 		topo_mod_dprintf(mod, "sof_freeall %s", sof->sof_path);
33712357SStephen.Hanson@Sun.COM 		topo_mod_strfree(mod, sof->sof_path);
33812357SStephen.Hanson@Sun.COM 		topo_mod_free(mod, sof, sizeof (*sof));
33912357SStephen.Hanson@Sun.COM 	}
34012357SStephen.Hanson@Sun.COM 	ses_sofh = NULL;
34112357SStephen.Hanson@Sun.COM 	(void) pthread_mutex_unlock(&ses_sofmt);
34212357SStephen.Hanson@Sun.COM }
34312357SStephen.Hanson@Sun.COM 
34412357SStephen.Hanson@Sun.COM /*
34512357SStephen.Hanson@Sun.COM  * functions for verifying that the ses_enum_target_t held in a device
34612357SStephen.Hanson@Sun.COM  * contract's cookie field is still valid (it may have been freed by
34712357SStephen.Hanson@Sun.COM  * ses_release()).
34812357SStephen.Hanson@Sun.COM  */
34912357SStephen.Hanson@Sun.COM typedef struct ses_stp_list {
35012357SStephen.Hanson@Sun.COM 	struct ses_stp_list	*ssl_next;
35112357SStephen.Hanson@Sun.COM 	ses_enum_target_t	*ssl_tgt;
35212357SStephen.Hanson@Sun.COM } ses_stp_list_t;
35312357SStephen.Hanson@Sun.COM 
35412357SStephen.Hanson@Sun.COM static ses_stp_list_t *ses_sslh;
35512357SStephen.Hanson@Sun.COM static pthread_mutex_t ses_sslmt;
35612357SStephen.Hanson@Sun.COM 
35712357SStephen.Hanson@Sun.COM static void
ses_ssl_alloc(topo_mod_t * mod,ses_enum_target_t * stp)35812357SStephen.Hanson@Sun.COM ses_ssl_alloc(topo_mod_t *mod, ses_enum_target_t *stp)
35912357SStephen.Hanson@Sun.COM {
36012357SStephen.Hanson@Sun.COM 	ses_stp_list_t *ssl;
36112357SStephen.Hanson@Sun.COM 
36212357SStephen.Hanson@Sun.COM 	(void) pthread_mutex_lock(&ses_sslmt);
36312357SStephen.Hanson@Sun.COM 	ssl = topo_mod_zalloc(mod, sizeof (*ssl));
36412357SStephen.Hanson@Sun.COM 	topo_mod_dprintf(mod, "ssl_alloc %p", stp);
36512357SStephen.Hanson@Sun.COM 	ssl->ssl_tgt = stp;
36612357SStephen.Hanson@Sun.COM 	ssl->ssl_next = ses_sslh;
36712357SStephen.Hanson@Sun.COM 	ses_sslh = ssl;
36812357SStephen.Hanson@Sun.COM 	(void) pthread_mutex_unlock(&ses_sslmt);
36912357SStephen.Hanson@Sun.COM }
37012357SStephen.Hanson@Sun.COM 
37112357SStephen.Hanson@Sun.COM static void
ses_ssl_free(topo_mod_t * mod,ses_enum_target_t * stp)37212357SStephen.Hanson@Sun.COM ses_ssl_free(topo_mod_t *mod, ses_enum_target_t *stp)
37312357SStephen.Hanson@Sun.COM {
37412357SStephen.Hanson@Sun.COM 	ses_stp_list_t *ssl, *prev_ssl;
37512357SStephen.Hanson@Sun.COM 
37612357SStephen.Hanson@Sun.COM 	(void) pthread_mutex_lock(&ses_sslmt);
37712357SStephen.Hanson@Sun.COM 	prev_ssl = NULL;
37812357SStephen.Hanson@Sun.COM 	for (ssl = ses_sslh; ssl != NULL; ssl = ssl->ssl_next) {
37912357SStephen.Hanson@Sun.COM 		if (ssl->ssl_tgt == stp) {
38012357SStephen.Hanson@Sun.COM 			topo_mod_dprintf(mod, "ssl_free %p", ssl->ssl_tgt);
38112357SStephen.Hanson@Sun.COM 			if (prev_ssl == NULL)
38212357SStephen.Hanson@Sun.COM 				ses_sslh = ssl->ssl_next;
38312357SStephen.Hanson@Sun.COM 			else
38412357SStephen.Hanson@Sun.COM 				prev_ssl->ssl_next = ssl->ssl_next;
38512357SStephen.Hanson@Sun.COM 			topo_mod_free(mod, ssl, sizeof (*ssl));
38612357SStephen.Hanson@Sun.COM 			break;
38712357SStephen.Hanson@Sun.COM 		}
38812357SStephen.Hanson@Sun.COM 		prev_ssl = ssl;
38912357SStephen.Hanson@Sun.COM 	}
39012357SStephen.Hanson@Sun.COM 	(void) pthread_mutex_unlock(&ses_sslmt);
39112357SStephen.Hanson@Sun.COM }
39212357SStephen.Hanson@Sun.COM 
39312357SStephen.Hanson@Sun.COM static int
ses_ssl_valid(ses_enum_target_t * stp)39412357SStephen.Hanson@Sun.COM ses_ssl_valid(ses_enum_target_t *stp)
39512357SStephen.Hanson@Sun.COM {
39612357SStephen.Hanson@Sun.COM 	ses_stp_list_t *ssl;
39712357SStephen.Hanson@Sun.COM 
39812357SStephen.Hanson@Sun.COM 	for (ssl = ses_sslh; ssl != NULL; ssl = ssl->ssl_next)
39912357SStephen.Hanson@Sun.COM 		if (ssl->ssl_tgt == stp)
40012357SStephen.Hanson@Sun.COM 			return (1);
40112357SStephen.Hanson@Sun.COM 	return (0);
40212357SStephen.Hanson@Sun.COM }
40312357SStephen.Hanson@Sun.COM 
40412357SStephen.Hanson@Sun.COM /*
40512357SStephen.Hanson@Sun.COM  * Functions for creating and destroying a background thread
40612357SStephen.Hanson@Sun.COM  * (ses_contract_thread) used for detecting when ses devices have been
40712357SStephen.Hanson@Sun.COM  * retired/unretired.
40812357SStephen.Hanson@Sun.COM  */
40912357SStephen.Hanson@Sun.COM static struct ses_thread_s {
41012357SStephen.Hanson@Sun.COM 	pthread_mutex_t mt;
41112357SStephen.Hanson@Sun.COM 	pthread_t tid;
41212357SStephen.Hanson@Sun.COM 	int thr_sig;
41312357SStephen.Hanson@Sun.COM 	int doexit;
41412357SStephen.Hanson@Sun.COM 	int count;
41512357SStephen.Hanson@Sun.COM } sesthread = {
41612357SStephen.Hanson@Sun.COM 	PTHREAD_MUTEX_INITIALIZER,
41712357SStephen.Hanson@Sun.COM 	0,
41812357SStephen.Hanson@Sun.COM 	SIGTERM,
41912357SStephen.Hanson@Sun.COM 	0,
42012357SStephen.Hanson@Sun.COM 	0
42112357SStephen.Hanson@Sun.COM };
42212357SStephen.Hanson@Sun.COM 
42312502SStephen.Hanson@Sun.COM typedef struct ses_mod_list {
42412502SStephen.Hanson@Sun.COM 	struct ses_mod_list	*smod_next;
42512502SStephen.Hanson@Sun.COM 	topo_mod_t		*smod_mod;
42612502SStephen.Hanson@Sun.COM } ses_mod_list_t;
42712502SStephen.Hanson@Sun.COM 
42812502SStephen.Hanson@Sun.COM static ses_mod_list_t *ses_smod;
42912502SStephen.Hanson@Sun.COM 
43012502SStephen.Hanson@Sun.COM static void
ses_ct_print(char * ptr)43112502SStephen.Hanson@Sun.COM ses_ct_print(char *ptr)
43212502SStephen.Hanson@Sun.COM {
43312502SStephen.Hanson@Sun.COM 	(void) pthread_mutex_lock(&sesthread.mt);
43412705SStephen.Hanson@Sun.COM 	if (ses_smod != NULL && ses_smod->smod_mod != NULL)
43512502SStephen.Hanson@Sun.COM 		topo_mod_dprintf(ses_smod->smod_mod, ptr);
43612502SStephen.Hanson@Sun.COM 	(void) pthread_mutex_unlock(&sesthread.mt);
43712502SStephen.Hanson@Sun.COM }
43812502SStephen.Hanson@Sun.COM 
43912502SStephen.Hanson@Sun.COM /*ARGSUSED*/
44012357SStephen.Hanson@Sun.COM static void *
ses_contract_thread(void * arg)44112357SStephen.Hanson@Sun.COM ses_contract_thread(void *arg)
44212357SStephen.Hanson@Sun.COM {
44312357SStephen.Hanson@Sun.COM 	int efd, ctlfd, statfd;
44412357SStephen.Hanson@Sun.COM 	ct_evthdl_t ev;
44512357SStephen.Hanson@Sun.COM 	ctevid_t evid;
44612357SStephen.Hanson@Sun.COM 	uint_t event;
44712357SStephen.Hanson@Sun.COM 	char path[PATH_MAX];
44812502SStephen.Hanson@Sun.COM 	char buf[80];
44912357SStephen.Hanson@Sun.COM 	ses_enum_target_t *stp;
45012357SStephen.Hanson@Sun.COM 	ct_stathdl_t stathdl;
45112357SStephen.Hanson@Sun.COM 	ctid_t ctid;
45212357SStephen.Hanson@Sun.COM 	struct pollfd fds;
45312357SStephen.Hanson@Sun.COM 	int pollret;
45412357SStephen.Hanson@Sun.COM 
45512502SStephen.Hanson@Sun.COM 	ses_ct_print("start contract event thread");
45612357SStephen.Hanson@Sun.COM 	efd = open64(CTFS_ROOT "/device/pbundle", O_RDONLY);
45712357SStephen.Hanson@Sun.COM 	fds.fd = efd;
45812357SStephen.Hanson@Sun.COM 	fds.events = POLLIN;
45912357SStephen.Hanson@Sun.COM 	fds.revents = 0;
46012357SStephen.Hanson@Sun.COM 	for (;;) {
46112357SStephen.Hanson@Sun.COM 		/* check if we've been asked to exit */
46212357SStephen.Hanson@Sun.COM 		(void) pthread_mutex_lock(&sesthread.mt);
46312357SStephen.Hanson@Sun.COM 		if (sesthread.doexit) {
46412357SStephen.Hanson@Sun.COM 			(void) pthread_mutex_unlock(&sesthread.mt);
46512357SStephen.Hanson@Sun.COM 			break;
46612357SStephen.Hanson@Sun.COM 		}
46712357SStephen.Hanson@Sun.COM 		(void) pthread_mutex_unlock(&sesthread.mt);
46812357SStephen.Hanson@Sun.COM 
46912357SStephen.Hanson@Sun.COM 		/* poll until an event arrives */
47012357SStephen.Hanson@Sun.COM 		if ((pollret = poll(&fds, 1, 10000)) <= 0) {
47112357SStephen.Hanson@Sun.COM 			if (pollret == 0)
47212502SStephen.Hanson@Sun.COM 				ses_recheck_dir();
47312357SStephen.Hanson@Sun.COM 			continue;
47412357SStephen.Hanson@Sun.COM 		}
47512357SStephen.Hanson@Sun.COM 
47612357SStephen.Hanson@Sun.COM 		/* read the event */
47712357SStephen.Hanson@Sun.COM 		(void) pthread_mutex_lock(&ses_sslmt);
47812502SStephen.Hanson@Sun.COM 		ses_ct_print("read contract event");
47912357SStephen.Hanson@Sun.COM 		if (ct_event_read(efd, &ev) != 0) {
48012357SStephen.Hanson@Sun.COM 			(void) pthread_mutex_unlock(&ses_sslmt);
48112357SStephen.Hanson@Sun.COM 			continue;
48212357SStephen.Hanson@Sun.COM 		}
48312357SStephen.Hanson@Sun.COM 
48412357SStephen.Hanson@Sun.COM 		/* see if it is an event we are expecting */
48512357SStephen.Hanson@Sun.COM 		ctid = ct_event_get_ctid(ev);
48612502SStephen.Hanson@Sun.COM 		(void) snprintf(buf, sizeof (buf),
48712502SStephen.Hanson@Sun.COM 		    "got contract event ctid=%d", ctid);
48812502SStephen.Hanson@Sun.COM 		ses_ct_print(buf);
48912357SStephen.Hanson@Sun.COM 		event = ct_event_get_type(ev);
49012357SStephen.Hanson@Sun.COM 		if (event != CT_DEV_EV_OFFLINE && event != CT_EV_NEGEND) {
49112502SStephen.Hanson@Sun.COM 			snprintf(buf, sizeof (buf),
49212502SStephen.Hanson@Sun.COM 			    "bad contract event %x", event);
49312502SStephen.Hanson@Sun.COM 			ses_ct_print(buf);
49412357SStephen.Hanson@Sun.COM 			ct_event_free(ev);
49512357SStephen.Hanson@Sun.COM 			(void) pthread_mutex_unlock(&ses_sslmt);
49612357SStephen.Hanson@Sun.COM 			continue;
49712357SStephen.Hanson@Sun.COM 		}
49812357SStephen.Hanson@Sun.COM 
49912357SStephen.Hanson@Sun.COM 		/* find target pointer saved in cookie */
50012357SStephen.Hanson@Sun.COM 		evid = ct_event_get_evid(ev);
50112357SStephen.Hanson@Sun.COM 		(void) snprintf(path, PATH_MAX, CTFS_ROOT "/device/%ld/status",
50212357SStephen.Hanson@Sun.COM 		    ctid);
50312357SStephen.Hanson@Sun.COM 		statfd = open64(path, O_RDONLY);
50412357SStephen.Hanson@Sun.COM 		ct_status_read(statfd, CTD_COMMON, &stathdl);
50512357SStephen.Hanson@Sun.COM 		stp = (ses_enum_target_t *)(uintptr_t)
50612357SStephen.Hanson@Sun.COM 		    ct_status_get_cookie(stathdl);
50712357SStephen.Hanson@Sun.COM 		ct_status_free(stathdl);
50812357SStephen.Hanson@Sun.COM 		close(statfd);
50912357SStephen.Hanson@Sun.COM 
51012357SStephen.Hanson@Sun.COM 		/* check if target pointer is still valid */
51112357SStephen.Hanson@Sun.COM 		if (ses_ssl_valid(stp) == 0) {
51212502SStephen.Hanson@Sun.COM 			snprintf(buf, sizeof (buf),
51312502SStephen.Hanson@Sun.COM 			    "contract already abandoned %x", event);
51412502SStephen.Hanson@Sun.COM 			ses_ct_print(buf);
51512357SStephen.Hanson@Sun.COM 			(void) snprintf(path, PATH_MAX,
51612357SStephen.Hanson@Sun.COM 			    CTFS_ROOT "/device/%ld/ctl", ctid);
51712357SStephen.Hanson@Sun.COM 			ctlfd = open64(path, O_WRONLY);
51812357SStephen.Hanson@Sun.COM 			if (event != CT_EV_NEGEND)
51912357SStephen.Hanson@Sun.COM 				ct_ctl_ack(ctlfd, evid);
52012357SStephen.Hanson@Sun.COM 			else
52112357SStephen.Hanson@Sun.COM 				ct_ctl_abandon(ctlfd);
52212357SStephen.Hanson@Sun.COM 			close(ctlfd);
52312357SStephen.Hanson@Sun.COM 			ct_event_free(ev);
52412357SStephen.Hanson@Sun.COM 			(void) pthread_mutex_unlock(&ses_sslmt);
52512357SStephen.Hanson@Sun.COM 			continue;
52612357SStephen.Hanson@Sun.COM 		}
52712357SStephen.Hanson@Sun.COM 
52812357SStephen.Hanson@Sun.COM 		/* find control device for ack/abandon */
52912357SStephen.Hanson@Sun.COM 		(void) pthread_mutex_lock(&stp->set_lock);
53012357SStephen.Hanson@Sun.COM 		(void) snprintf(path, PATH_MAX, CTFS_ROOT "/device/%ld/ctl",
53112357SStephen.Hanson@Sun.COM 		    ctid);
53212357SStephen.Hanson@Sun.COM 		ctlfd = open64(path, O_WRONLY);
53312357SStephen.Hanson@Sun.COM 		if (event != CT_EV_NEGEND) {
53412357SStephen.Hanson@Sun.COM 			/* if this is an offline event, do the offline */
53512502SStephen.Hanson@Sun.COM 			ses_ct_print("got contract offline event");
53612357SStephen.Hanson@Sun.COM 			if (stp->set_target) {
53712502SStephen.Hanson@Sun.COM 				ses_ct_print("contract thread rele");
53812357SStephen.Hanson@Sun.COM 				ses_snap_rele(stp->set_snap);
53912357SStephen.Hanson@Sun.COM 				ses_close(stp->set_target);
54012357SStephen.Hanson@Sun.COM 				stp->set_target = NULL;
54112357SStephen.Hanson@Sun.COM 			}
54212357SStephen.Hanson@Sun.COM 			ct_ctl_ack(ctlfd, evid);
54312357SStephen.Hanson@Sun.COM 		} else {
54412357SStephen.Hanson@Sun.COM 			/* if this is the negend, then abandon the contract */
54512502SStephen.Hanson@Sun.COM 			ses_ct_print("got contract negend");
54612357SStephen.Hanson@Sun.COM 			if (stp->set_ctid) {
54712502SStephen.Hanson@Sun.COM 				snprintf(buf, sizeof (buf),
54812502SStephen.Hanson@Sun.COM 				    "abandon old contract %d", stp->set_ctid);
54912502SStephen.Hanson@Sun.COM 				ses_ct_print(buf);
55012357SStephen.Hanson@Sun.COM 				stp->set_ctid = NULL;
55112357SStephen.Hanson@Sun.COM 			}
55212357SStephen.Hanson@Sun.COM 			ct_ctl_abandon(ctlfd);
55312357SStephen.Hanson@Sun.COM 		}
55412357SStephen.Hanson@Sun.COM 		close(ctlfd);
55512357SStephen.Hanson@Sun.COM 		(void) pthread_mutex_unlock(&stp->set_lock);
55612357SStephen.Hanson@Sun.COM 		ct_event_free(ev);
55712357SStephen.Hanson@Sun.COM 		(void) pthread_mutex_unlock(&ses_sslmt);
55812357SStephen.Hanson@Sun.COM 	}
55912357SStephen.Hanson@Sun.COM 	close(efd);
56012357SStephen.Hanson@Sun.COM 	return (NULL);
56112357SStephen.Hanson@Sun.COM }
56212357SStephen.Hanson@Sun.COM 
56312357SStephen.Hanson@Sun.COM int
find_thr_sig(void)56412357SStephen.Hanson@Sun.COM find_thr_sig(void)
56512357SStephen.Hanson@Sun.COM {
56612357SStephen.Hanson@Sun.COM 	int i;
56712357SStephen.Hanson@Sun.COM 	sigset_t oset, rset;
56812357SStephen.Hanson@Sun.COM 	int sig[] = {SIGTERM, SIGUSR1, SIGUSR2};
56912357SStephen.Hanson@Sun.COM 	int sig_sz = sizeof (sig) / sizeof (int);
57012357SStephen.Hanson@Sun.COM 	int rc = SIGTERM;
57112357SStephen.Hanson@Sun.COM 
57212357SStephen.Hanson@Sun.COM 	/* prefered set of signals that are likely used to terminate threads */
57312357SStephen.Hanson@Sun.COM 	(void) sigemptyset(&oset);
57412357SStephen.Hanson@Sun.COM 	(void) pthread_sigmask(SIG_SETMASK, NULL, &oset);
57512357SStephen.Hanson@Sun.COM 	for (i = 0; i < sig_sz; i++) {
57612357SStephen.Hanson@Sun.COM 		if (sigismember(&oset, sig[i]) == 0) {
57712357SStephen.Hanson@Sun.COM 			return (sig[i]);
57812357SStephen.Hanson@Sun.COM 		}
57912357SStephen.Hanson@Sun.COM 	}
58012357SStephen.Hanson@Sun.COM 
58112357SStephen.Hanson@Sun.COM 	/* reserved set of signals that are not allowed to terminate thread */
58212357SStephen.Hanson@Sun.COM 	(void) sigemptyset(&rset);
58312357SStephen.Hanson@Sun.COM 	(void) sigaddset(&rset, SIGABRT);
58412357SStephen.Hanson@Sun.COM 	(void) sigaddset(&rset, SIGKILL);
58512357SStephen.Hanson@Sun.COM 	(void) sigaddset(&rset, SIGSTOP);
58612357SStephen.Hanson@Sun.COM 	(void) sigaddset(&rset, SIGCANCEL);
58712357SStephen.Hanson@Sun.COM 
58812357SStephen.Hanson@Sun.COM 	/* Find signal that is not masked and not in the reserved list. */
58912357SStephen.Hanson@Sun.COM 	for (i = 1; i < MAXSIG; i++) {
59012357SStephen.Hanson@Sun.COM 		if (sigismember(&rset, i) == 1) {
59112357SStephen.Hanson@Sun.COM 			continue;
59212357SStephen.Hanson@Sun.COM 		}
59312357SStephen.Hanson@Sun.COM 		if (sigismember(&oset, i) == 0) {
59412357SStephen.Hanson@Sun.COM 			return (i);
59512357SStephen.Hanson@Sun.COM 		}
59612357SStephen.Hanson@Sun.COM 	}
59712357SStephen.Hanson@Sun.COM 
59812357SStephen.Hanson@Sun.COM 	return (rc);
59912357SStephen.Hanson@Sun.COM }
60012357SStephen.Hanson@Sun.COM 
60112357SStephen.Hanson@Sun.COM /*ARGSUSED*/
60212357SStephen.Hanson@Sun.COM static void
ses_handler(int sig)60312357SStephen.Hanson@Sun.COM ses_handler(int sig)
60412357SStephen.Hanson@Sun.COM {
60512357SStephen.Hanson@Sun.COM }
60612357SStephen.Hanson@Sun.COM 
60712357SStephen.Hanson@Sun.COM static void
ses_thread_init(topo_mod_t * mod)60812502SStephen.Hanson@Sun.COM ses_thread_init(topo_mod_t *mod)
60912357SStephen.Hanson@Sun.COM {
61012357SStephen.Hanson@Sun.COM 	pthread_attr_t *attr = NULL;
61112357SStephen.Hanson@Sun.COM 	struct sigaction act;
61212502SStephen.Hanson@Sun.COM 	ses_mod_list_t *smod;
61312357SStephen.Hanson@Sun.COM 
61412357SStephen.Hanson@Sun.COM 	(void) pthread_mutex_lock(&sesthread.mt);
61512357SStephen.Hanson@Sun.COM 	sesthread.count++;
61612502SStephen.Hanson@Sun.COM 	smod = topo_mod_zalloc(mod, sizeof (*smod));
61712502SStephen.Hanson@Sun.COM 	smod->smod_mod = mod;
61812502SStephen.Hanson@Sun.COM 	smod->smod_next = ses_smod;
61912502SStephen.Hanson@Sun.COM 	ses_smod = smod;
62012357SStephen.Hanson@Sun.COM 	if (sesthread.tid == 0) {
62112357SStephen.Hanson@Sun.COM 		/* find a suitable signal to use for killing the thread below */
62212357SStephen.Hanson@Sun.COM 		sesthread.thr_sig = find_thr_sig();
62312357SStephen.Hanson@Sun.COM 
62412357SStephen.Hanson@Sun.COM 		/* if don't have a handler for this signal, create one */
62512357SStephen.Hanson@Sun.COM 		(void) sigaction(sesthread.thr_sig, NULL, &act);
62612357SStephen.Hanson@Sun.COM 		if (act.sa_handler == SIG_DFL || act.sa_handler == SIG_IGN)
62712357SStephen.Hanson@Sun.COM 			act.sa_handler = ses_handler;
62812357SStephen.Hanson@Sun.COM 		(void) sigaction(sesthread.thr_sig, &act, NULL);
62912357SStephen.Hanson@Sun.COM 
63012357SStephen.Hanson@Sun.COM 		/* create a thread to listen for offline events */
63112357SStephen.Hanson@Sun.COM 		(void) pthread_create(&sesthread.tid,
63212502SStephen.Hanson@Sun.COM 		    attr, ses_contract_thread, NULL);
63312357SStephen.Hanson@Sun.COM 	}
63412357SStephen.Hanson@Sun.COM 	(void) pthread_mutex_unlock(&sesthread.mt);
63512357SStephen.Hanson@Sun.COM }
63612357SStephen.Hanson@Sun.COM 
63712357SStephen.Hanson@Sun.COM static void
ses_thread_fini(topo_mod_t * mod)63812502SStephen.Hanson@Sun.COM ses_thread_fini(topo_mod_t *mod)
63912357SStephen.Hanson@Sun.COM {
64012502SStephen.Hanson@Sun.COM 	ses_mod_list_t *smod, *prev_smod;
64112502SStephen.Hanson@Sun.COM 
64212357SStephen.Hanson@Sun.COM 	(void) pthread_mutex_lock(&sesthread.mt);
64312502SStephen.Hanson@Sun.COM 	prev_smod = NULL;
64412502SStephen.Hanson@Sun.COM 	for (smod = ses_smod; smod != NULL; smod = smod->smod_next) {
64512502SStephen.Hanson@Sun.COM 		if (smod->smod_mod == mod) {
64612502SStephen.Hanson@Sun.COM 			if (prev_smod == NULL)
64712502SStephen.Hanson@Sun.COM 				ses_smod = smod->smod_next;
64812502SStephen.Hanson@Sun.COM 			else
64912502SStephen.Hanson@Sun.COM 				prev_smod->smod_next = smod->smod_next;
65012502SStephen.Hanson@Sun.COM 			topo_mod_free(mod, smod, sizeof (*smod));
65112502SStephen.Hanson@Sun.COM 			break;
65212502SStephen.Hanson@Sun.COM 		}
65312502SStephen.Hanson@Sun.COM 		prev_smod = smod;
65412502SStephen.Hanson@Sun.COM 	}
65512357SStephen.Hanson@Sun.COM 	if (--sesthread.count > 0) {
65612357SStephen.Hanson@Sun.COM 		(void) pthread_mutex_unlock(&sesthread.mt);
65712357SStephen.Hanson@Sun.COM 		return;
65812357SStephen.Hanson@Sun.COM 	}
65912357SStephen.Hanson@Sun.COM 	sesthread.doexit = 1;
66012357SStephen.Hanson@Sun.COM 	(void) pthread_mutex_unlock(&sesthread.mt);
66112357SStephen.Hanson@Sun.COM 	(void) pthread_kill(sesthread.tid, sesthread.thr_sig);
66212357SStephen.Hanson@Sun.COM 	(void) pthread_join(sesthread.tid, NULL);
66312357SStephen.Hanson@Sun.COM 	sesthread.tid = 0;
66412357SStephen.Hanson@Sun.COM }
66512357SStephen.Hanson@Sun.COM 
66612357SStephen.Hanson@Sun.COM static void
ses_create_contract(topo_mod_t * mod,ses_enum_target_t * stp)66712357SStephen.Hanson@Sun.COM ses_create_contract(topo_mod_t *mod, ses_enum_target_t *stp)
66812357SStephen.Hanson@Sun.COM {
66912357SStephen.Hanson@Sun.COM 	int tfd, len, rval;
67012357SStephen.Hanson@Sun.COM 	char link_path[PATH_MAX];
67112357SStephen.Hanson@Sun.COM 
67212357SStephen.Hanson@Sun.COM 	stp->set_ctid = NULL;
67312357SStephen.Hanson@Sun.COM 
67412357SStephen.Hanson@Sun.COM 	/* convert "/dev" path into "/devices" path */
67512357SStephen.Hanson@Sun.COM 	if ((len = readlink(stp->set_devpath, link_path, PATH_MAX)) < 0) {
67612357SStephen.Hanson@Sun.COM 		topo_mod_dprintf(mod, "readlink failed");
67712357SStephen.Hanson@Sun.COM 		return;
67812357SStephen.Hanson@Sun.COM 	}
67912357SStephen.Hanson@Sun.COM 	link_path[len] = '\0';
68012357SStephen.Hanson@Sun.COM 
68112357SStephen.Hanson@Sun.COM 	/* set up template to create new contract */
68212357SStephen.Hanson@Sun.COM 	tfd = open64(CTFS_ROOT "/device/template", O_RDWR);
68312357SStephen.Hanson@Sun.COM 	ct_tmpl_set_critical(tfd, CT_DEV_EV_OFFLINE);
68412357SStephen.Hanson@Sun.COM 	ct_tmpl_set_cookie(tfd, (uint64_t)(uintptr_t)stp);
68512357SStephen.Hanson@Sun.COM 
68612357SStephen.Hanson@Sun.COM 	/* strip "../../devices" off the front and create the contract */
68712357SStephen.Hanson@Sun.COM 	if ((rval = ct_dev_tmpl_set_minor(tfd, &link_path[13])) != 0)
68812357SStephen.Hanson@Sun.COM 		topo_mod_dprintf(mod, "failed to set minor %s rval = %d",
68912357SStephen.Hanson@Sun.COM 		    &link_path[13], rval);
69012357SStephen.Hanson@Sun.COM 	else if ((rval = ct_tmpl_create(tfd, &stp->set_ctid)) != 0)
69112357SStephen.Hanson@Sun.COM 		topo_mod_dprintf(mod, "failed to create ctid rval = %d", rval);
69212357SStephen.Hanson@Sun.COM 	else
69312357SStephen.Hanson@Sun.COM 		topo_mod_dprintf(mod, "created ctid=%d", stp->set_ctid);
69412357SStephen.Hanson@Sun.COM 	close(tfd);
69512357SStephen.Hanson@Sun.COM }
69612357SStephen.Hanson@Sun.COM 
6976869Seschrock static void
ses_target_free(topo_mod_t * mod,ses_enum_target_t * stp)6986869Seschrock ses_target_free(topo_mod_t *mod, ses_enum_target_t *stp)
6996869Seschrock {
7006869Seschrock 	if (--stp->set_refcount == 0) {
70112357SStephen.Hanson@Sun.COM 		/* check if already closed due to contract offline request */
70212357SStephen.Hanson@Sun.COM 		(void) pthread_mutex_lock(&stp->set_lock);
70312357SStephen.Hanson@Sun.COM 		if (stp->set_target) {
70412357SStephen.Hanson@Sun.COM 			ses_snap_rele(stp->set_snap);
70512357SStephen.Hanson@Sun.COM 			ses_close(stp->set_target);
70612357SStephen.Hanson@Sun.COM 			stp->set_target = NULL;
70712357SStephen.Hanson@Sun.COM 		}
70812357SStephen.Hanson@Sun.COM 		if (stp->set_ctid) {
70912357SStephen.Hanson@Sun.COM 			int ctlfd;
71012357SStephen.Hanson@Sun.COM 			char path[PATH_MAX];
71112357SStephen.Hanson@Sun.COM 
71212357SStephen.Hanson@Sun.COM 			topo_mod_dprintf(mod, "abandon old contract %d",
71312357SStephen.Hanson@Sun.COM 			    stp->set_ctid);
71412357SStephen.Hanson@Sun.COM 			(void) snprintf(path, PATH_MAX,
71512357SStephen.Hanson@Sun.COM 			    CTFS_ROOT "/device/%ld/ctl", stp->set_ctid);
71612357SStephen.Hanson@Sun.COM 			ctlfd = open64(path, O_WRONLY);
71712357SStephen.Hanson@Sun.COM 			ct_ctl_abandon(ctlfd);
71812357SStephen.Hanson@Sun.COM 			close(ctlfd);
71912357SStephen.Hanson@Sun.COM 			stp->set_ctid = NULL;
72012357SStephen.Hanson@Sun.COM 		}
72112357SStephen.Hanson@Sun.COM 		(void) pthread_mutex_unlock(&stp->set_lock);
72212357SStephen.Hanson@Sun.COM 		ses_ssl_free(mod, stp);
7236869Seschrock 		topo_mod_strfree(mod, stp->set_devpath);
7246869Seschrock 		topo_mod_free(mod, stp, sizeof (ses_enum_target_t));
7256869Seschrock 	}
7266869Seschrock }
7276869Seschrock 
7286869Seschrock static void
ses_data_free(ses_enum_data_t * sdp,ses_enum_chassis_t * pcp)72910519SSundeep.Panicker@Sun.COM ses_data_free(ses_enum_data_t *sdp, ses_enum_chassis_t *pcp)
7306869Seschrock {
7316869Seschrock 	topo_mod_t *mod = sdp->sed_mod;
7326869Seschrock 	ses_enum_chassis_t *cp;
7336869Seschrock 	ses_enum_node_t *np;
7346869Seschrock 	ses_enum_target_t *tp;
7358344SEric.Schrock@Sun.COM 	ses_alt_node_t *ap;
73610519SSundeep.Panicker@Sun.COM 	topo_list_t *cpl;
7376869Seschrock 
73810519SSundeep.Panicker@Sun.COM 
73910519SSundeep.Panicker@Sun.COM 	if (pcp != NULL)
74010519SSundeep.Panicker@Sun.COM 		cpl = &pcp->sec_subchassis;
74110519SSundeep.Panicker@Sun.COM 	else
74210519SSundeep.Panicker@Sun.COM 		cpl = &sdp->sed_chassis;
74310519SSundeep.Panicker@Sun.COM 
74410519SSundeep.Panicker@Sun.COM 	while ((cp = topo_list_next(cpl)) != NULL) {
74510519SSundeep.Panicker@Sun.COM 		topo_list_delete(cpl, cp);
7466869Seschrock 
7476869Seschrock 		while ((np = topo_list_next(&cp->sec_nodes)) != NULL) {
7488344SEric.Schrock@Sun.COM 			while ((ap = topo_list_next(&np->sen_alt_nodes)) !=
7498344SEric.Schrock@Sun.COM 			    NULL) {
7508344SEric.Schrock@Sun.COM 				topo_list_delete(&np->sen_alt_nodes, ap);
7518344SEric.Schrock@Sun.COM 				topo_mod_free(mod, ap, sizeof (ses_alt_node_t));
7528344SEric.Schrock@Sun.COM 			}
7536869Seschrock 			topo_list_delete(&cp->sec_nodes, np);
7546869Seschrock 			topo_mod_free(mod, np, sizeof (ses_enum_node_t));
7556869Seschrock 		}
7566869Seschrock 
7576869Seschrock 		while ((tp = topo_list_next(&cp->sec_targets)) != NULL) {
7586869Seschrock 			topo_list_delete(&cp->sec_targets, tp);
7596869Seschrock 			ses_target_free(mod, tp);
7606869Seschrock 		}
7616869Seschrock 
7626869Seschrock 		topo_mod_free(mod, cp, sizeof (ses_enum_chassis_t));
7636869Seschrock 	}
7646869Seschrock 
76510519SSundeep.Panicker@Sun.COM 	if (pcp == NULL) {
76612126SHyon.Kim@Sun.COM 		dev_list_free(mod, &sdp->sed_devs);
76710519SSundeep.Panicker@Sun.COM 		topo_mod_free(mod, sdp, sizeof (ses_enum_data_t));
76810519SSundeep.Panicker@Sun.COM 	}
7696869Seschrock }
7706869Seschrock 
7716869Seschrock /*
7726869Seschrock  * For enclosure nodes, we have a special contains method.  By default, the hc
7736869Seschrock  * walker will compare the node name and instance number to determine if an
7746869Seschrock  * FMRI matches.  For enclosures where the enumeration order is impossible to
7756869Seschrock  * predict, we instead use the chassis-id as a unique identifier, and ignore
7766869Seschrock  * the instance number.
7776869Seschrock  */
7786869Seschrock static int
fmri_contains(topo_mod_t * mod,nvlist_t * nv1,nvlist_t * nv2)7796869Seschrock fmri_contains(topo_mod_t *mod, nvlist_t *nv1, nvlist_t *nv2)
7806869Seschrock {
7816869Seschrock 	uint8_t v1, v2;
7826869Seschrock 	nvlist_t **hcp1, **hcp2;
7836869Seschrock 	int err, i;
7846869Seschrock 	uint_t nhcp1, nhcp2;
7856869Seschrock 	nvlist_t *a1, *a2;
7866869Seschrock 	char *c1, *c2;
7876869Seschrock 	int mindepth;
7886869Seschrock 
7896869Seschrock 	if (nvlist_lookup_uint8(nv1, FM_VERSION, &v1) != 0 ||
7906869Seschrock 	    nvlist_lookup_uint8(nv2, FM_VERSION, &v2) != 0 ||
7916869Seschrock 	    v1 > FM_HC_SCHEME_VERSION || v2 > FM_HC_SCHEME_VERSION)
7926869Seschrock 		return (topo_mod_seterrno(mod, EMOD_FMRI_VERSION));
7936869Seschrock 
7946869Seschrock 	err = nvlist_lookup_nvlist_array(nv1, FM_FMRI_HC_LIST, &hcp1, &nhcp1);
7956869Seschrock 	err |= nvlist_lookup_nvlist_array(nv2, FM_FMRI_HC_LIST, &hcp2, &nhcp2);
7966869Seschrock 	if (err != 0)
7976869Seschrock 		return (topo_mod_seterrno(mod, EMOD_FMRI_NVL));
7986869Seschrock 
7996869Seschrock 	/*
8006869Seschrock 	 * If the chassis-id doesn't match, then these FMRIs are not
8016869Seschrock 	 * equivalent.  If one of the FMRIs doesn't have a chassis ID, then we
8026869Seschrock 	 * have no choice but to fall back to the instance ID.
8036869Seschrock 	 */
8046869Seschrock 	if (nvlist_lookup_nvlist(nv1, FM_FMRI_AUTHORITY, &a1) == 0 &&
8056869Seschrock 	    nvlist_lookup_nvlist(nv2, FM_FMRI_AUTHORITY, &a2) == 0 &&
8066869Seschrock 	    nvlist_lookup_string(a1, FM_FMRI_AUTH_CHASSIS, &c1) == 0 &&
8076869Seschrock 	    nvlist_lookup_string(a2, FM_FMRI_AUTH_CHASSIS, &c2) == 0) {
8086869Seschrock 		if (strcmp(c1, c2) != 0)
8096869Seschrock 			return (0);
8106869Seschrock 
8116869Seschrock 		mindepth = 1;
8126869Seschrock 	} else {
8136869Seschrock 		mindepth = 0;
8146869Seschrock 	}
8156869Seschrock 
8166869Seschrock 	if (nhcp2 < nhcp1)
8176869Seschrock 		return (0);
8186869Seschrock 
8196869Seschrock 	for (i = 0; i < nhcp1; i++) {
8206869Seschrock 		char *nm1 = NULL;
8216869Seschrock 		char *nm2 = NULL;
8226869Seschrock 		char *id1 = NULL;
8236869Seschrock 		char *id2 = NULL;
8246869Seschrock 
8256869Seschrock 		(void) nvlist_lookup_string(hcp1[i], FM_FMRI_HC_NAME, &nm1);
8266869Seschrock 		(void) nvlist_lookup_string(hcp2[i], FM_FMRI_HC_NAME, &nm2);
8276869Seschrock 		(void) nvlist_lookup_string(hcp1[i], FM_FMRI_HC_ID, &id1);
8286869Seschrock 		(void) nvlist_lookup_string(hcp2[i], FM_FMRI_HC_ID, &id2);
8296869Seschrock 		if (nm1 == NULL || nm2 == NULL || id1 == NULL || id2 == NULL)
8306869Seschrock 			return (topo_mod_seterrno(mod, EMOD_FMRI_NVL));
8316869Seschrock 
8326869Seschrock 		if (strcmp(nm1, nm2) == 0 &&
8336869Seschrock 		    (i < mindepth || strcmp(id1, id2) == 0))
8346869Seschrock 			continue;
8356869Seschrock 
8366869Seschrock 		return (0);
8376869Seschrock 	}
8386869Seschrock 
8396869Seschrock 	return (1);
8406869Seschrock }
8416869Seschrock 
8426869Seschrock /*ARGSUSED*/
8436869Seschrock static int
ses_contains(topo_mod_t * mod,tnode_t * tn,topo_version_t version,nvlist_t * in,nvlist_t ** out)8446869Seschrock ses_contains(topo_mod_t *mod, tnode_t *tn, topo_version_t version,
8456869Seschrock     nvlist_t *in, nvlist_t **out)
8466869Seschrock {
8476869Seschrock 	int ret;
8486869Seschrock 	nvlist_t *nv1, *nv2;
8496869Seschrock 
8506869Seschrock 	if (version > TOPO_METH_CONTAINS_VERSION)
8516869Seschrock 		return (topo_mod_seterrno(mod, EMOD_VER_NEW));
8526869Seschrock 
8536869Seschrock 	if (nvlist_lookup_nvlist(in, TOPO_METH_FMRI_ARG_FMRI, &nv1) != 0 ||
8546869Seschrock 	    nvlist_lookup_nvlist(in, TOPO_METH_FMRI_ARG_SUBFMRI, &nv2) != 0)
8556869Seschrock 		return (topo_mod_seterrno(mod, EMOD_METHOD_INVAL));
8566869Seschrock 
8576869Seschrock 	ret = fmri_contains(mod, nv1, nv2);
8586869Seschrock 	if (ret < 0)
8596869Seschrock 		return (-1);
8606869Seschrock 
8616869Seschrock 	if (topo_mod_nvalloc(mod, out, NV_UNIQUE_NAME) == 0) {
8627732SRobert.Johnston@Sun.COM 		if (nvlist_add_uint32(*out, TOPO_METH_CONTAINS_RET,
8636869Seschrock 		    ret) == 0)
8646869Seschrock 			return (0);
8656869Seschrock 		else
8666869Seschrock 			nvlist_free(*out);
8676869Seschrock 	}
8686869Seschrock 
8696869Seschrock 	return (-1);
8706869Seschrock 
8716869Seschrock }
8726869Seschrock 
8736869Seschrock /*
8747269Seschrock  * Return a current instance of the node.  This is somewhat complicated because
8757269Seschrock  * we need to take a new snapshot in order to get the new data, but we don't
8766869Seschrock  * want to be constantly taking SES snapshots if the consumer is going to do a
8776869Seschrock  * series of queries.  So we adopt the strategy of assuming that the SES state
8787269Seschrock  * is not going to be rapidly changing, and limit our snapshot frequency to
8797269Seschrock  * some defined bounds.
8806869Seschrock  */
8817269Seschrock ses_node_t *
ses_node_lock(topo_mod_t * mod,tnode_t * tn)8828344SEric.Schrock@Sun.COM ses_node_lock(topo_mod_t *mod, tnode_t *tn)
8836869Seschrock {
8846869Seschrock 	ses_enum_target_t *tp = topo_node_getspecific(tn);
88510527SEric.Schrock@Sun.COM 	hrtime_t now;
8866869Seschrock 	ses_snap_t *snap;
8876869Seschrock 	int err;
8887269Seschrock 	uint64_t nodeid;
8896869Seschrock 	ses_node_t *np;
8906869Seschrock 
8917269Seschrock 	if (tp == NULL) {
8927269Seschrock 		(void) topo_mod_seterrno(mod, EMOD_METHOD_NOTSUP);
8937269Seschrock 		return (NULL);
8947269Seschrock 	}
8956869Seschrock 
8968344SEric.Schrock@Sun.COM 	(void) pthread_mutex_lock(&tp->set_lock);
8978344SEric.Schrock@Sun.COM 
8986869Seschrock 	/*
8996869Seschrock 	 * Determine if we need to take a new snapshot.
9006869Seschrock 	 */
90110527SEric.Schrock@Sun.COM 	now = gethrtime();
9026869Seschrock 
90312357SStephen.Hanson@Sun.COM 	if (tp->set_target == NULL) {
90412357SStephen.Hanson@Sun.COM 		/*
90512357SStephen.Hanson@Sun.COM 		 * We may have closed the device but not yet abandoned the
90612357SStephen.Hanson@Sun.COM 		 * contract (ie we've had the offline event but not yet the
90712357SStephen.Hanson@Sun.COM 		 * negend). If so, just return failure.
90812357SStephen.Hanson@Sun.COM 		 */
90912357SStephen.Hanson@Sun.COM 		if (tp->set_ctid != NULL) {
91012357SStephen.Hanson@Sun.COM 			(void) topo_mod_seterrno(mod, EMOD_METHOD_NOTSUP);
91112357SStephen.Hanson@Sun.COM 			(void) pthread_mutex_unlock(&tp->set_lock);
91212357SStephen.Hanson@Sun.COM 			return (NULL);
91312357SStephen.Hanson@Sun.COM 		}
91412357SStephen.Hanson@Sun.COM 
91512357SStephen.Hanson@Sun.COM 		/*
91612357SStephen.Hanson@Sun.COM 		 * The device has been closed due to a contract offline
91712357SStephen.Hanson@Sun.COM 		 * request, then we need to reopen it and create a new contract.
91812357SStephen.Hanson@Sun.COM 		 */
91912357SStephen.Hanson@Sun.COM 		if ((tp->set_target =
92012357SStephen.Hanson@Sun.COM 		    ses_open(LIBSES_VERSION, tp->set_devpath)) == NULL) {
92112357SStephen.Hanson@Sun.COM 			sysevent_id_t eid;
92212357SStephen.Hanson@Sun.COM 
92312357SStephen.Hanson@Sun.COM 			(void) topo_mod_seterrno(mod, EMOD_METHOD_NOTSUP);
92412357SStephen.Hanson@Sun.COM 			(void) pthread_mutex_unlock(&tp->set_lock);
92512357SStephen.Hanson@Sun.COM 			topo_mod_dprintf(mod, "recheck_dir - "
92612357SStephen.Hanson@Sun.COM 			    "can no longer open %s", tp->set_devpath);
92712357SStephen.Hanson@Sun.COM 			(void) sysevent_post_event(EC_PLATFORM,
92812357SStephen.Hanson@Sun.COM 			    ESC_PLATFORM_SP_RESET, SUNW_VENDOR, "fmd", NULL,
92912357SStephen.Hanson@Sun.COM 			    &eid);
93012357SStephen.Hanson@Sun.COM 			return (NULL);
93112357SStephen.Hanson@Sun.COM 		}
93212357SStephen.Hanson@Sun.COM 		topo_mod_dprintf(mod, "reopen contract");
93312357SStephen.Hanson@Sun.COM 		ses_create_contract(mod, tp);
93412357SStephen.Hanson@Sun.COM 		tp->set_snap = ses_snap_hold(tp->set_target);
93512357SStephen.Hanson@Sun.COM 		tp->set_snaptime = gethrtime();
93612357SStephen.Hanson@Sun.COM 	} else if (now - tp->set_snaptime > (ses_snap_freq * 1000 * 1000) &&
9376869Seschrock 	    (snap = ses_snap_new(tp->set_target)) != NULL) {
9386869Seschrock 		if (ses_snap_generation(snap) !=
9396869Seschrock 		    ses_snap_generation(tp->set_snap)) {
9406869Seschrock 			/*
9416869Seschrock 			 * If we find ourselves in this situation, we're in
9426869Seschrock 			 * trouble.  The generation count has changed, which
9436869Seschrock 			 * indicates that our current topology is out of date.
9446869Seschrock 			 * But we need to consult the new topology in order to
9456869Seschrock 			 * determine presence at this moment in time.  We can't
9466869Seschrock 			 * go back and change the topo snapshot in situ, so
9477269Seschrock 			 * we'll just have to fail the call in this unlikely
9487269Seschrock 			 * scenario.
9496869Seschrock 			 */
9506869Seschrock 			ses_snap_rele(snap);
9517269Seschrock 			(void) topo_mod_seterrno(mod, EMOD_METHOD_NOTSUP);
9528344SEric.Schrock@Sun.COM 			(void) pthread_mutex_unlock(&tp->set_lock);
9537269Seschrock 			return (NULL);
9546869Seschrock 		} else {
9556869Seschrock 			ses_snap_rele(tp->set_snap);
9566869Seschrock 			tp->set_snap = snap;
9576869Seschrock 		}
95810527SEric.Schrock@Sun.COM 		tp->set_snaptime = gethrtime();
9596869Seschrock 	}
9606869Seschrock 
9616869Seschrock 	snap = tp->set_snap;
9626869Seschrock 
9636869Seschrock 	verify(topo_prop_get_uint64(tn, TOPO_PGROUP_SES,
9646869Seschrock 	    TOPO_PROP_NODE_ID, &nodeid, &err) == 0);
9656869Seschrock 	verify((np = ses_node_lookup(snap, nodeid)) != NULL);
9667269Seschrock 
9677269Seschrock 	return (np);
9687269Seschrock }
9697269Seschrock 
9708344SEric.Schrock@Sun.COM /*ARGSUSED*/
9718344SEric.Schrock@Sun.COM void
ses_node_unlock(topo_mod_t * mod,tnode_t * tn)9728344SEric.Schrock@Sun.COM ses_node_unlock(topo_mod_t *mod, tnode_t *tn)
9738344SEric.Schrock@Sun.COM {
9748344SEric.Schrock@Sun.COM 	ses_enum_target_t *tp = topo_node_getspecific(tn);
9758344SEric.Schrock@Sun.COM 
9768344SEric.Schrock@Sun.COM 	verify(tp != NULL);
9778344SEric.Schrock@Sun.COM 
9788344SEric.Schrock@Sun.COM 	(void) pthread_mutex_unlock(&tp->set_lock);
9798344SEric.Schrock@Sun.COM }
9808344SEric.Schrock@Sun.COM 
9817269Seschrock /*
9827269Seschrock  * Determine if the element is present.
9837269Seschrock  */
9847269Seschrock /*ARGSUSED*/
9857269Seschrock static int
ses_present(topo_mod_t * mod,tnode_t * tn,topo_version_t version,nvlist_t * in,nvlist_t ** out)9867269Seschrock ses_present(topo_mod_t *mod, tnode_t *tn, topo_version_t version,
9877269Seschrock     nvlist_t *in, nvlist_t **out)
9887269Seschrock {
9897269Seschrock 	boolean_t present;
9907269Seschrock 	ses_node_t *np;
9917269Seschrock 	nvlist_t *props, *nvl;
9927269Seschrock 	uint64_t status;
9937269Seschrock 
9948344SEric.Schrock@Sun.COM 	if ((np = ses_node_lock(mod, tn)) == NULL)
9957269Seschrock 		return (-1);
9967269Seschrock 
9976869Seschrock 	verify((props = ses_node_props(np)) != NULL);
9986869Seschrock 	verify(nvlist_lookup_uint64(props,
9996869Seschrock 	    SES_PROP_STATUS_CODE, &status) == 0);
10006869Seschrock 
10018344SEric.Schrock@Sun.COM 	ses_node_unlock(mod, tn);
10028344SEric.Schrock@Sun.COM 
10036869Seschrock 	present = (status != SES_ESC_NOT_INSTALLED);
10046869Seschrock 
10056869Seschrock 	if (topo_mod_nvalloc(mod, &nvl, NV_UNIQUE_NAME) != 0)
10066869Seschrock 		return (topo_mod_seterrno(mod, EMOD_FMRI_NVL));
10076869Seschrock 
10086869Seschrock 	if (nvlist_add_uint32(nvl, TOPO_METH_PRESENT_RET,
10096869Seschrock 	    present) != 0) {
10106869Seschrock 		nvlist_free(nvl);
10116869Seschrock 		return (topo_mod_seterrno(mod, EMOD_FMRI_NVL));
10126869Seschrock 	}
10136869Seschrock 
10146869Seschrock 	*out = nvl;
10156869Seschrock 
10166869Seschrock 	return (0);
10176869Seschrock }
10186869Seschrock 
10196869Seschrock /*
102012126SHyon.Kim@Sun.COM  * Sets standard properties for a ses node (enclosure, bay, controller
102112126SHyon.Kim@Sun.COM  * or expander).
102212126SHyon.Kim@Sun.COM  * This includes setting the FRU, as well as setting the
102312126SHyon.Kim@Sun.COM  * authority information.  When  the fru topo node(frutn) is not NULL
102412126SHyon.Kim@Sun.COM  * its resouce should be used as FRU.
10256869Seschrock  */
10266869Seschrock static int
ses_set_standard_props(topo_mod_t * mod,tnode_t * frutn,tnode_t * tn,nvlist_t * auth,uint64_t nodeid,const char * path)102712126SHyon.Kim@Sun.COM ses_set_standard_props(topo_mod_t *mod, tnode_t *frutn, tnode_t *tn,
102812126SHyon.Kim@Sun.COM     nvlist_t *auth, uint64_t nodeid, const char *path)
10296869Seschrock {
10306869Seschrock 	int err;
10316869Seschrock 	char *product, *chassis;
10326869Seschrock 	nvlist_t *fmri;
10336869Seschrock 
10346869Seschrock 	/*
10356869Seschrock 	 * Set the authority explicitly if specified.
10366869Seschrock 	 */
10376869Seschrock 	if (auth) {
10386869Seschrock 		verify(nvlist_lookup_string(auth, FM_FMRI_AUTH_PRODUCT,
10396869Seschrock 		    &product) == 0);
10406869Seschrock 		verify(nvlist_lookup_string(auth, FM_FMRI_AUTH_CHASSIS,
10416869Seschrock 		    &chassis) == 0);
10426869Seschrock 		if (topo_prop_set_string(tn, FM_FMRI_AUTHORITY,
10436869Seschrock 		    FM_FMRI_AUTH_PRODUCT, TOPO_PROP_IMMUTABLE, product,
10446869Seschrock 		    &err) != 0 ||
10456869Seschrock 		    topo_prop_set_string(tn, FM_FMRI_AUTHORITY,
10466869Seschrock 		    FM_FMRI_AUTH_CHASSIS, TOPO_PROP_IMMUTABLE, chassis,
10476869Seschrock 		    &err) != 0 ||
10486869Seschrock 		    topo_prop_set_string(tn, FM_FMRI_AUTHORITY,
10496869Seschrock 		    FM_FMRI_AUTH_SERVER, TOPO_PROP_IMMUTABLE, "",
10506869Seschrock 		    &err) != 0) {
10516869Seschrock 			topo_mod_dprintf(mod, "failed to add authority "
10527269Seschrock 			    "properties: %s\n", topo_strerror(err));
10536869Seschrock 			return (topo_mod_seterrno(mod, err));
10546869Seschrock 		}
10556869Seschrock 	}
10566869Seschrock 
10576869Seschrock 	/*
10586869Seschrock 	 * Copy the resource and set that as the FRU.
10596869Seschrock 	 */
106012126SHyon.Kim@Sun.COM 	if (frutn != NULL) {
106112126SHyon.Kim@Sun.COM 		if (topo_node_resource(frutn, &fmri, &err) != 0) {
106212126SHyon.Kim@Sun.COM 			topo_mod_dprintf(mod,
106312126SHyon.Kim@Sun.COM 			    "topo_node_resource() failed : %s\n",
106412126SHyon.Kim@Sun.COM 			    topo_strerror(err));
106512126SHyon.Kim@Sun.COM 			return (topo_mod_seterrno(mod, err));
106612126SHyon.Kim@Sun.COM 		}
106712126SHyon.Kim@Sun.COM 	} else {
106812126SHyon.Kim@Sun.COM 		if (topo_node_resource(tn, &fmri, &err) != 0) {
106912126SHyon.Kim@Sun.COM 			topo_mod_dprintf(mod,
107012126SHyon.Kim@Sun.COM 			    "topo_node_resource() failed : %s\n",
107112126SHyon.Kim@Sun.COM 			    topo_strerror(err));
107212126SHyon.Kim@Sun.COM 			return (topo_mod_seterrno(mod, err));
107312126SHyon.Kim@Sun.COM 		}
10746869Seschrock 	}
10756869Seschrock 
10766869Seschrock 	if (topo_node_fru_set(tn, fmri, 0, &err) != 0) {
10776869Seschrock 		topo_mod_dprintf(mod,
10786869Seschrock 		    "topo_node_fru_set() failed : %s\n",
10796869Seschrock 		    topo_strerror(err));
10806869Seschrock 		nvlist_free(fmri);
10816869Seschrock 		return (topo_mod_seterrno(mod, err));
10826869Seschrock 	}
10836869Seschrock 
10846869Seschrock 	nvlist_free(fmri);
10856869Seschrock 
10866869Seschrock 	/*
10876869Seschrock 	 * Set the SES-specific properties so that consumers can query
10886869Seschrock 	 * additional information about the particular SES element.
10896869Seschrock 	 */
109012563SHyon.Kim@Sun.COM 	if (topo_pgroup_create(tn, &ses_pgroup, &err) != 0) {
10916869Seschrock 		topo_mod_dprintf(mod, "failed to create propgroup "
10926869Seschrock 		    "%s: %s\n", TOPO_PGROUP_SES, topo_strerror(err));
10936869Seschrock 		return (-1);
10946869Seschrock 	}
10956869Seschrock 
10966869Seschrock 	if (topo_prop_set_uint64(tn, TOPO_PGROUP_SES,
10976869Seschrock 	    TOPO_PROP_NODE_ID, TOPO_PROP_IMMUTABLE,
10986869Seschrock 	    nodeid, &err) != 0) {
10996869Seschrock 		topo_mod_dprintf(mod,
11006869Seschrock 		    "failed to create property %s: %s\n",
11016869Seschrock 		    TOPO_PROP_NODE_ID, topo_strerror(err));
11026869Seschrock 		return (-1);
11036869Seschrock 	}
11046869Seschrock 
11056869Seschrock 	if (topo_prop_set_string(tn, TOPO_PGROUP_SES,
11066869Seschrock 	    TOPO_PROP_TARGET_PATH, TOPO_PROP_IMMUTABLE,
11076869Seschrock 	    path, &err) != 0) {
11086869Seschrock 		topo_mod_dprintf(mod,
11096869Seschrock 		    "failed to create property %s: %s\n",
11106869Seschrock 		    TOPO_PROP_TARGET_PATH, topo_strerror(err));
11116869Seschrock 		return (-1);
11126869Seschrock 	}
11136869Seschrock 
11146869Seschrock 	return (0);
11156869Seschrock }
11166869Seschrock 
11176869Seschrock /*
11186869Seschrock  * Callback to add a disk to a given bay.  We first check the status-code to
11196869Seschrock  * determine if a disk is present, ignoring those that aren't in an appropriate
11208344SEric.Schrock@Sun.COM  * state.  We then scan the parent bay node's SAS address array to determine
11218344SEric.Schrock@Sun.COM  * possible attached SAS addresses.  We create a disk node if the disk is not
11228344SEric.Schrock@Sun.COM  * SAS or the SES target does not support the necessary pages for this; if we
11238344SEric.Schrock@Sun.COM  * find the SAS address, we create a disk node and also correlate it with
11248344SEric.Schrock@Sun.COM  * the corresponding Solaris device node to fill in the rest of the data.
11256869Seschrock  */
11266869Seschrock static int
ses_create_disk(ses_enum_data_t * sdp,tnode_t * pnode,nvlist_t * props)11276869Seschrock ses_create_disk(ses_enum_data_t *sdp, tnode_t *pnode, nvlist_t *props)
11286869Seschrock {
11296869Seschrock 	topo_mod_t *mod = sdp->sed_mod;
11306869Seschrock 	uint64_t status;
11316869Seschrock 	uint_t s, nsas;
11328344SEric.Schrock@Sun.COM 	char **paths;
113312106SStephen.Hanson@Sun.COM 	int err, ret;
113412106SStephen.Hanson@Sun.COM 	tnode_t *child = NULL;
11356869Seschrock 
11366869Seschrock 	/*
11376869Seschrock 	 * Skip devices that are not in a present (and possibly damaged) state.
11386869Seschrock 	 */
11396869Seschrock 	if (nvlist_lookup_uint64(props, SES_PROP_STATUS_CODE, &status) != 0)
11406869Seschrock 		return (0);
11416869Seschrock 
114212126SHyon.Kim@Sun.COM 	if (status != SES_ESC_UNSUPPORTED &&
114312126SHyon.Kim@Sun.COM 	    status != SES_ESC_OK &&
11446869Seschrock 	    status != SES_ESC_CRITICAL &&
11456869Seschrock 	    status != SES_ESC_NONCRITICAL &&
11466869Seschrock 	    status != SES_ESC_UNRECOVERABLE &&
11476869Seschrock 	    status != SES_ESC_NO_ACCESS)
11486869Seschrock 		return (0);
11496869Seschrock 
11506869Seschrock 	topo_mod_dprintf(mod, "found attached disk");
11516869Seschrock 
11526869Seschrock 	/*
11536869Seschrock 	 * Create the disk range.
11546869Seschrock 	 */
11558344SEric.Schrock@Sun.COM 	if (topo_node_range_create(mod, pnode, DISK, 0, 0) != 0) {
11566869Seschrock 		topo_mod_dprintf(mod,
11576869Seschrock 		    "topo_node_create_range() failed: %s",
11586869Seschrock 		    topo_mod_errmsg(mod));
11596869Seschrock 		return (-1);
11606869Seschrock 	}
11616869Seschrock 
11626869Seschrock 	/*
11636869Seschrock 	 * Look through all SAS addresses and attempt to correlate them to a
11646869Seschrock 	 * known Solaris device.  If we don't find a matching node, then we
11656869Seschrock 	 * don't enumerate the disk node.
116612654SHyon.Kim@Sun.COM 	 * Note that TOPO_PROP_SAS_ADDR prop includes SAS address from
116712654SHyon.Kim@Sun.COM 	 * alternate elements that represent the same device.
11686869Seschrock 	 */
11698344SEric.Schrock@Sun.COM 	if (topo_prop_get_string_array(pnode, TOPO_PGROUP_SES,
11708344SEric.Schrock@Sun.COM 	    TOPO_PROP_SAS_ADDR, &paths, &nsas, &err) != 0)
11718344SEric.Schrock@Sun.COM 		return (0);
11728344SEric.Schrock@Sun.COM 
11738344SEric.Schrock@Sun.COM 	err = 0;
11748344SEric.Schrock@Sun.COM 
11756869Seschrock 	for (s = 0; s < nsas; s++) {
117612126SHyon.Kim@Sun.COM 		ret = disk_declare_addr(mod, pnode, &sdp->sed_devs, paths[s],
117712106SStephen.Hanson@Sun.COM 		    &child);
117812106SStephen.Hanson@Sun.COM 		if (ret == 0) {
117912106SStephen.Hanson@Sun.COM 			break;
118012106SStephen.Hanson@Sun.COM 		} else if (ret < 0) {
11818344SEric.Schrock@Sun.COM 			err = -1;
11828344SEric.Schrock@Sun.COM 			break;
11838344SEric.Schrock@Sun.COM 		}
11848344SEric.Schrock@Sun.COM 	}
11858344SEric.Schrock@Sun.COM 
118612106SStephen.Hanson@Sun.COM 	if (s == nsas)
118712106SStephen.Hanson@Sun.COM 		disk_declare_non_enumerated(mod, pnode, &child);
118812106SStephen.Hanson@Sun.COM 
118912106SStephen.Hanson@Sun.COM 	/* copy sas_addresses (target-ports) from parent (with 'w'added) */
119012106SStephen.Hanson@Sun.COM 	if (child != NULL) {
119112106SStephen.Hanson@Sun.COM 		int i;
119212106SStephen.Hanson@Sun.COM 		char **tports;
119312106SStephen.Hanson@Sun.COM 		uint64_t wwn;
119412106SStephen.Hanson@Sun.COM 
119512106SStephen.Hanson@Sun.COM 		tports = topo_mod_zalloc(mod, sizeof (char *) * nsas);
119612106SStephen.Hanson@Sun.COM 		if (tports != NULL) {
119712106SStephen.Hanson@Sun.COM 			for (i = 0; i < nsas; i++) {
119812106SStephen.Hanson@Sun.COM 				if (scsi_wwnstr_to_wwn(paths[i], &wwn) !=
119912106SStephen.Hanson@Sun.COM 				    DDI_SUCCESS)
120012106SStephen.Hanson@Sun.COM 					break;
120112106SStephen.Hanson@Sun.COM 				tports[i] = scsi_wwn_to_wwnstr(wwn, 1, NULL);
120212106SStephen.Hanson@Sun.COM 				if (tports[i] == NULL)
120312106SStephen.Hanson@Sun.COM 					break;
120412106SStephen.Hanson@Sun.COM 			}
120512106SStephen.Hanson@Sun.COM 			/* if they all worked then create the property */
120612106SStephen.Hanson@Sun.COM 			if (i == nsas)
120712106SStephen.Hanson@Sun.COM 				(void) topo_prop_set_string_array(child,
120812106SStephen.Hanson@Sun.COM 				    TOPO_PGROUP_STORAGE,
120912106SStephen.Hanson@Sun.COM 				    TOPO_STORAGE_TARGET_PORT_L0IDS,
121012106SStephen.Hanson@Sun.COM 				    TOPO_PROP_IMMUTABLE, (const char **)tports,
121112106SStephen.Hanson@Sun.COM 				    nsas, &err);
121212106SStephen.Hanson@Sun.COM 
121312106SStephen.Hanson@Sun.COM 			for (i = 0; i < nsas; i++)
121412106SStephen.Hanson@Sun.COM 				if (tports[i] != NULL)
121512106SStephen.Hanson@Sun.COM 					scsi_free_wwnstr(tports[i]);
121612106SStephen.Hanson@Sun.COM 			topo_mod_free(mod, tports, sizeof (char *) * nsas);
121712106SStephen.Hanson@Sun.COM 		}
121812106SStephen.Hanson@Sun.COM 	}
121912106SStephen.Hanson@Sun.COM 
12208344SEric.Schrock@Sun.COM 	for (s = 0; s < nsas; s++)
12218344SEric.Schrock@Sun.COM 		topo_mod_free(mod, paths[s], strlen(paths[s]) + 1);
12228344SEric.Schrock@Sun.COM 	topo_mod_free(mod, paths, nsas * sizeof (char *));
12238344SEric.Schrock@Sun.COM 
12248344SEric.Schrock@Sun.COM 	return (err);
12258344SEric.Schrock@Sun.COM }
12268344SEric.Schrock@Sun.COM 
12278344SEric.Schrock@Sun.COM static int
ses_add_bay_props(topo_mod_t * mod,tnode_t * tn,ses_enum_node_t * snp)12288344SEric.Schrock@Sun.COM ses_add_bay_props(topo_mod_t *mod, tnode_t *tn, ses_enum_node_t *snp)
12298344SEric.Schrock@Sun.COM {
12308344SEric.Schrock@Sun.COM 	ses_alt_node_t *ap;
12318344SEric.Schrock@Sun.COM 	ses_node_t *np;
12328344SEric.Schrock@Sun.COM 	nvlist_t *props;
12338344SEric.Schrock@Sun.COM 
12348344SEric.Schrock@Sun.COM 	nvlist_t **phys;
12358344SEric.Schrock@Sun.COM 	uint_t i, j, n_phys, all_phys = 0;
12368344SEric.Schrock@Sun.COM 	char **paths;
12378344SEric.Schrock@Sun.COM 	uint64_t addr;
12388344SEric.Schrock@Sun.COM 	size_t len;
12398344SEric.Schrock@Sun.COM 	int terr, err = -1;
12408344SEric.Schrock@Sun.COM 
12418344SEric.Schrock@Sun.COM 	for (ap = topo_list_next(&snp->sen_alt_nodes); ap != NULL;
12428344SEric.Schrock@Sun.COM 	    ap = topo_list_next(ap)) {
12438344SEric.Schrock@Sun.COM 		np = ap->san_node;
12448344SEric.Schrock@Sun.COM 		props = ses_node_props(np);
12458344SEric.Schrock@Sun.COM 
12468344SEric.Schrock@Sun.COM 		if (nvlist_lookup_nvlist_array(props, SES_SAS_PROP_PHYS,
12478344SEric.Schrock@Sun.COM 		    &phys, &n_phys) != 0)
12486869Seschrock 			continue;
12496869Seschrock 
12508344SEric.Schrock@Sun.COM 		all_phys += n_phys;
12516869Seschrock 	}
12526869Seschrock 
12538344SEric.Schrock@Sun.COM 	if (all_phys == 0)
12548344SEric.Schrock@Sun.COM 		return (0);
12558344SEric.Schrock@Sun.COM 
12568344SEric.Schrock@Sun.COM 	if ((paths = topo_mod_zalloc(mod, all_phys * sizeof (char *))) == NULL)
12578344SEric.Schrock@Sun.COM 		return (-1);
12588344SEric.Schrock@Sun.COM 
12598344SEric.Schrock@Sun.COM 	for (i = 0, ap = topo_list_next(&snp->sen_alt_nodes); ap != NULL;
12608344SEric.Schrock@Sun.COM 	    ap = topo_list_next(ap)) {
12618344SEric.Schrock@Sun.COM 		np = ap->san_node;
12628344SEric.Schrock@Sun.COM 		props = ses_node_props(np);
12638344SEric.Schrock@Sun.COM 
12648344SEric.Schrock@Sun.COM 		if (nvlist_lookup_nvlist_array(props, SES_SAS_PROP_PHYS,
12658344SEric.Schrock@Sun.COM 		    &phys, &n_phys) != 0)
12668344SEric.Schrock@Sun.COM 			continue;
12678344SEric.Schrock@Sun.COM 
12688344SEric.Schrock@Sun.COM 		for (j = 0; j < n_phys; j++) {
12698344SEric.Schrock@Sun.COM 			if (nvlist_lookup_uint64(phys[j], SES_SAS_PROP_ADDR,
12708344SEric.Schrock@Sun.COM 			    &addr) != 0)
12718344SEric.Schrock@Sun.COM 				continue;
12728344SEric.Schrock@Sun.COM 
12738344SEric.Schrock@Sun.COM 			len = snprintf(NULL, 0, "%016llx", addr) + 1;
12748344SEric.Schrock@Sun.COM 			if ((paths[i] = topo_mod_alloc(mod, len)) == NULL)
12758344SEric.Schrock@Sun.COM 				goto error;
12768344SEric.Schrock@Sun.COM 
12778344SEric.Schrock@Sun.COM 			(void) snprintf(paths[i], len, "%016llx", addr);
12788344SEric.Schrock@Sun.COM 
12798344SEric.Schrock@Sun.COM 			++i;
12808344SEric.Schrock@Sun.COM 		}
12818344SEric.Schrock@Sun.COM 	}
12828344SEric.Schrock@Sun.COM 
12838344SEric.Schrock@Sun.COM 	err = topo_prop_set_string_array(tn, TOPO_PGROUP_SES,
12848344SEric.Schrock@Sun.COM 	    TOPO_PROP_SAS_ADDR, TOPO_PROP_IMMUTABLE,
12858344SEric.Schrock@Sun.COM 	    (const char **)paths, i, &terr);
12868344SEric.Schrock@Sun.COM 	if (err != 0)
12878344SEric.Schrock@Sun.COM 		err = topo_mod_seterrno(mod, terr);
12888344SEric.Schrock@Sun.COM 
12898344SEric.Schrock@Sun.COM error:
12908344SEric.Schrock@Sun.COM 	for (i = 0; i < all_phys && paths[i] != NULL; i++)
12918344SEric.Schrock@Sun.COM 		topo_mod_free(mod, paths[i], strlen(paths[i]) + 1);
12928344SEric.Schrock@Sun.COM 	topo_mod_free(mod, paths, all_phys * sizeof (char *));
12938344SEric.Schrock@Sun.COM 
12948344SEric.Schrock@Sun.COM 	return (err);
12956869Seschrock }
12966869Seschrock 
12976869Seschrock /*
129812126SHyon.Kim@Sun.COM  * Callback to create a basic node (bay, psu, fan, or controller and expander).
12996869Seschrock  */
13006869Seschrock static int
ses_create_generic(ses_enum_data_t * sdp,ses_enum_node_t * snp,tnode_t * pnode,tnode_t * frutn,const char * nodename,const char * labelname,tnode_t ** node)130112632SHyon.Kim@Sun.COM ses_create_generic(ses_enum_data_t *sdp, ses_enum_node_t *snp, tnode_t *pnode,
130212632SHyon.Kim@Sun.COM     tnode_t *frutn, const char *nodename, const char *labelname,
130312632SHyon.Kim@Sun.COM     tnode_t **node)
13046869Seschrock {
13056869Seschrock 	ses_node_t *np = snp->sen_node;
13067269Seschrock 	ses_node_t *parent;
13076869Seschrock 	uint64_t instance = snp->sen_instance;
13086869Seschrock 	topo_mod_t *mod = sdp->sed_mod;
13096869Seschrock 	nvlist_t *props, *aprops;
13106869Seschrock 	nvlist_t *auth = NULL, *fmri = NULL;
131112632SHyon.Kim@Sun.COM 	tnode_t *tn = NULL;
13126869Seschrock 	char label[128];
13136869Seschrock 	int err;
13147269Seschrock 	char *part = NULL, *serial = NULL, *revision = NULL;
13157269Seschrock 	char *desc;
13167269Seschrock 	boolean_t report;
13176869Seschrock 
13186869Seschrock 	props = ses_node_props(np);
13196869Seschrock 
13206869Seschrock 	(void) nvlist_lookup_string(props, LIBSES_PROP_PART, &part);
13216869Seschrock 	(void) nvlist_lookup_string(props, LIBSES_PROP_SERIAL, &serial);
13226869Seschrock 
13236869Seschrock 	topo_mod_dprintf(mod, "adding %s %llu", nodename, instance);
13246869Seschrock 
13256869Seschrock 	/*
13266869Seschrock 	 * Create the node.  The interesting information is all copied from the
13276869Seschrock 	 * parent enclosure node, so there is not much to do.
13286869Seschrock 	 */
13296869Seschrock 	if ((auth = topo_mod_auth(mod, pnode)) == NULL)
13306869Seschrock 		goto error;
13316869Seschrock 
13327269Seschrock 	/*
13337269Seschrock 	 * We want to report revision information for the controller nodes, but
13347269Seschrock 	 * we do not get per-element revision information.  However, we do have
13357269Seschrock 	 * revision information for the entire enclosure, and we can use the
13367269Seschrock 	 * 'reported-via' property to know that this controller corresponds to
13377269Seschrock 	 * the given revision information.  This means we cannot get revision
13387269Seschrock 	 * information for targets we are not explicitly connected to, but
13397269Seschrock 	 * there is little we can do about the situation.
13407269Seschrock 	 */
13417269Seschrock 	if (strcmp(nodename, CONTROLLER) == 0 &&
13427269Seschrock 	    nvlist_lookup_boolean_value(props, SES_PROP_REPORT, &report) == 0 &&
13437269Seschrock 	    report) {
13447269Seschrock 		for (parent = ses_node_parent(np); parent != NULL;
13457269Seschrock 		    parent = ses_node_parent(parent)) {
13467269Seschrock 			if (ses_node_type(parent) == SES_NODE_ENCLOSURE) {
13477269Seschrock 				(void) nvlist_lookup_string(
13487269Seschrock 				    ses_node_props(parent),
13497269Seschrock 				    SES_EN_PROP_REV, &revision);
13507269Seschrock 				break;
13517269Seschrock 			}
13527269Seschrock 		}
13537269Seschrock 	}
13547269Seschrock 
13556869Seschrock 	if ((fmri = topo_mod_hcfmri(mod, pnode, FM_HC_SCHEME_VERSION,
13567269Seschrock 	    nodename, (topo_instance_t)instance, NULL, auth, part, revision,
13576869Seschrock 	    serial)) == NULL) {
13586869Seschrock 		topo_mod_dprintf(mod, "topo_mod_hcfmri() failed: %s",
13596869Seschrock 		    topo_mod_errmsg(mod));
13606869Seschrock 		goto error;
13616869Seschrock 	}
13626869Seschrock 
13636869Seschrock 	if ((tn = topo_node_bind(mod, pnode, nodename,
13646869Seschrock 	    instance, fmri)) == NULL) {
13656869Seschrock 		topo_mod_dprintf(mod, "topo_node_bind() failed: %s",
13666869Seschrock 		    topo_mod_errmsg(mod));
13676869Seschrock 		goto error;
13686869Seschrock 	}
13696869Seschrock 
13706869Seschrock 	/*
13717269Seschrock 	 * For the node label, we look for the following in order:
13727269Seschrock 	 *
13737269Seschrock 	 * 	<ses-description>
13747269Seschrock 	 * 	<ses-class-description> <instance>
13757269Seschrock 	 * 	<default-type-label> <instance>
13766869Seschrock 	 */
13777269Seschrock 	if (nvlist_lookup_string(props, SES_PROP_DESCRIPTION, &desc) != 0 ||
13787269Seschrock 	    desc[0] == '\0') {
13797269Seschrock 		parent = ses_node_parent(np);
13807269Seschrock 		aprops = ses_node_props(parent);
13817269Seschrock 		if (nvlist_lookup_string(aprops, SES_PROP_CLASS_DESCRIPTION,
138212126SHyon.Kim@Sun.COM 		    &desc) != 0 || desc[0] == '\0')
138312126SHyon.Kim@Sun.COM 			desc = (char *)labelname;
13847269Seschrock 		(void) snprintf(label, sizeof (label), "%s %llu", desc,
13857269Seschrock 		    instance);
13867269Seschrock 		desc = label;
13877269Seschrock 	}
13886869Seschrock 
13897269Seschrock 	if (topo_node_label_set(tn, desc, &err) != 0)
13906869Seschrock 		goto error;
13916869Seschrock 
139212126SHyon.Kim@Sun.COM 	if (ses_set_standard_props(mod, frutn, tn, NULL, ses_node_id(np),
13936869Seschrock 	    snp->sen_target->set_devpath) != 0)
13946869Seschrock 		goto error;
13956869Seschrock 
139612126SHyon.Kim@Sun.COM 	if (strcmp(nodename, BAY) == 0) {
13978344SEric.Schrock@Sun.COM 		if (ses_add_bay_props(mod, tn, snp) != 0)
13988344SEric.Schrock@Sun.COM 			goto error;
13998344SEric.Schrock@Sun.COM 
14006869Seschrock 		if (ses_create_disk(sdp, tn, props) != 0)
14016869Seschrock 			goto error;
14027269Seschrock 
14037269Seschrock 		if (topo_method_register(mod, tn, ses_bay_methods) != 0) {
14047269Seschrock 			topo_mod_dprintf(mod,
14057269Seschrock 			    "topo_method_register() failed: %s",
14067269Seschrock 			    topo_mod_errmsg(mod));
14077269Seschrock 			goto error;
14087269Seschrock 		}
140912126SHyon.Kim@Sun.COM 	} else if ((strcmp(nodename, FAN) == 0) ||
141012126SHyon.Kim@Sun.COM 	    (strcmp(nodename, PSU) == 0) ||
141112126SHyon.Kim@Sun.COM 	    (strcmp(nodename, CONTROLLER) == 0)) {
14126869Seschrock 		/*
14137269Seschrock 		 * Only fan, psu, and controller nodes have a 'present' method.
14147269Seschrock 		 * Bay nodes are always present, and disk nodes are present by
141512126SHyon.Kim@Sun.COM 		 * virtue of being enumerated and SAS expander nodes and
141612126SHyon.Kim@Sun.COM 		 * SAS connector nodes are also always present once
141712126SHyon.Kim@Sun.COM 		 * the parent controller is found.
14186869Seschrock 		 */
14196869Seschrock 		if (topo_method_register(mod, tn, ses_component_methods) != 0) {
14206869Seschrock 			topo_mod_dprintf(mod,
14216869Seschrock 			    "topo_method_register() failed: %s",
14226869Seschrock 			    topo_mod_errmsg(mod));
14236869Seschrock 			goto error;
14246869Seschrock 		}
14256869Seschrock 
14266869Seschrock 	}
14276869Seschrock 
14287269Seschrock 	snp->sen_target->set_refcount++;
14297269Seschrock 	topo_node_setspecific(tn, snp->sen_target);
14307269Seschrock 
14316869Seschrock 	nvlist_free(auth);
14326869Seschrock 	nvlist_free(fmri);
143312126SHyon.Kim@Sun.COM 	if (node != NULL) *node = tn;
143412126SHyon.Kim@Sun.COM 	return (0);
143512126SHyon.Kim@Sun.COM 
143612126SHyon.Kim@Sun.COM error:
143712126SHyon.Kim@Sun.COM 	nvlist_free(auth);
143812126SHyon.Kim@Sun.COM 	nvlist_free(fmri);
143912126SHyon.Kim@Sun.COM 	return (-1);
144012126SHyon.Kim@Sun.COM }
144112126SHyon.Kim@Sun.COM 
144212126SHyon.Kim@Sun.COM /*
144312126SHyon.Kim@Sun.COM  * Create SAS expander specific props.
144412126SHyon.Kim@Sun.COM  */
144512126SHyon.Kim@Sun.COM /*ARGSUSED*/
144612126SHyon.Kim@Sun.COM static int
ses_set_expander_props(ses_enum_data_t * sdp,ses_enum_node_t * snp,tnode_t * ptnode,tnode_t * tnode,int * phycount,int64_t * connlist)144712126SHyon.Kim@Sun.COM ses_set_expander_props(ses_enum_data_t *sdp, ses_enum_node_t *snp,
144812126SHyon.Kim@Sun.COM     tnode_t *ptnode, tnode_t *tnode, int *phycount, int64_t *connlist)
144912126SHyon.Kim@Sun.COM {
145012126SHyon.Kim@Sun.COM 	ses_node_t *np = snp->sen_node;
145112126SHyon.Kim@Sun.COM 	topo_mod_t *mod = sdp->sed_mod;
145212126SHyon.Kim@Sun.COM 	nvlist_t *auth = NULL, *fmri = NULL;
145312126SHyon.Kim@Sun.COM 	nvlist_t *props, **phylist;
145412126SHyon.Kim@Sun.COM 	int err, i;
145512126SHyon.Kim@Sun.COM 	uint_t pcount;
145612126SHyon.Kim@Sun.COM 	uint64_t sasaddr, connidx;
145712126SHyon.Kim@Sun.COM 	char sasaddr_str[17];
145812477SHyon.Kim@Sun.COM 	boolean_t found = B_FALSE, ses_found = B_FALSE;
145912477SHyon.Kim@Sun.COM 	dev_di_node_t *dnode, *sesdnode;
146012126SHyon.Kim@Sun.COM 
146112126SHyon.Kim@Sun.COM 	props = ses_node_props(np);
146212126SHyon.Kim@Sun.COM 
146312126SHyon.Kim@Sun.COM 	/*
146412126SHyon.Kim@Sun.COM 	 * the uninstalled expander is not enumerated by checking
146512126SHyon.Kim@Sun.COM 	 * the element status code.  No present present' method provided.
146612126SHyon.Kim@Sun.COM 	 */
146712126SHyon.Kim@Sun.COM 	/*
146812126SHyon.Kim@Sun.COM 	 * Get the Expander SAS address.  It should exist.
146912126SHyon.Kim@Sun.COM 	 */
147012126SHyon.Kim@Sun.COM 	if (nvlist_lookup_uint64(props, SES_EXP_PROP_SAS_ADDR,
147112126SHyon.Kim@Sun.COM 	    &sasaddr) != 0) {
147212126SHyon.Kim@Sun.COM 		topo_mod_dprintf(mod,
147312126SHyon.Kim@Sun.COM 		    "Failed to get prop %s.", SES_EXP_PROP_SAS_ADDR);
147412126SHyon.Kim@Sun.COM 		goto error;
147512126SHyon.Kim@Sun.COM 	}
147612126SHyon.Kim@Sun.COM 
147712126SHyon.Kim@Sun.COM 	(void) sprintf(sasaddr_str, "%llx", sasaddr);
147812126SHyon.Kim@Sun.COM 
147912126SHyon.Kim@Sun.COM 	/* search matching dev_di_node. */
148012126SHyon.Kim@Sun.COM 	for (dnode = topo_list_next(&sdp->sed_devs); dnode != NULL;
148112126SHyon.Kim@Sun.COM 	    dnode = topo_list_next(dnode)) {
148212477SHyon.Kim@Sun.COM 		for (i = 0; i < dnode->ddn_ppath_count; i++) {
148312477SHyon.Kim@Sun.COM 			if ((dnode->ddn_target_port[i] != NULL) &&
148412477SHyon.Kim@Sun.COM 			    (strstr(dnode->ddn_target_port[i],
148512477SHyon.Kim@Sun.COM 			    sasaddr_str) != NULL)) {
148612477SHyon.Kim@Sun.COM 				found = B_TRUE;
148712477SHyon.Kim@Sun.COM 				break;
148812477SHyon.Kim@Sun.COM 			}
148912477SHyon.Kim@Sun.COM 		}
149012477SHyon.Kim@Sun.COM 		if (found)
149112126SHyon.Kim@Sun.COM 			break;
149212126SHyon.Kim@Sun.COM 	}
149312126SHyon.Kim@Sun.COM 
149412126SHyon.Kim@Sun.COM 	if (!found) {
149512126SHyon.Kim@Sun.COM 		topo_mod_dprintf(mod,
149612126SHyon.Kim@Sun.COM 		    "ses_set_expander_props: Failed to find matching "
149712126SHyon.Kim@Sun.COM 		    "devinfo node for Exapnder SAS address %s",
149812126SHyon.Kim@Sun.COM 		    SES_EXP_PROP_SAS_ADDR);
149912126SHyon.Kim@Sun.COM 		/* continue on to get storage group props. */
150012126SHyon.Kim@Sun.COM 	} else {
150112477SHyon.Kim@Sun.COM 		/* create/set the devfs-path and devid in the smp group */
150212477SHyon.Kim@Sun.COM 		if (topo_pgroup_create(tnode, &smp_pgroup, &err) != 0) {
150312126SHyon.Kim@Sun.COM 			topo_mod_dprintf(mod, "ses_set_expander_props: "
150412477SHyon.Kim@Sun.COM 			    "failed to create smp property group %s\n",
150512477SHyon.Kim@Sun.COM 			    topo_strerror(err));
150612126SHyon.Kim@Sun.COM 			goto error;
150712126SHyon.Kim@Sun.COM 		} else {
150812477SHyon.Kim@Sun.COM 			if (topo_prop_set_string(tnode, TOPO_PGROUP_SMP,
150912477SHyon.Kim@Sun.COM 			    TOPO_PROP_SMP_TARGET_PORT, TOPO_PROP_IMMUTABLE,
151012477SHyon.Kim@Sun.COM 			    dnode->ddn_target_port[i], &err) != 0) {
151112477SHyon.Kim@Sun.COM 				topo_mod_dprintf(mod, "ses_set_expander_props: "
151212477SHyon.Kim@Sun.COM 				    "set %S error %s\n", TOPO_PROP_SAS_ADDR,
151312477SHyon.Kim@Sun.COM 				    topo_strerror(err));
151412477SHyon.Kim@Sun.COM 			}
151512477SHyon.Kim@Sun.COM 			if (topo_prop_set_string(tnode, TOPO_PGROUP_SMP,
151612477SHyon.Kim@Sun.COM 			    TOPO_PROP_SMP_DEV_PATH, TOPO_PROP_IMMUTABLE,
151712126SHyon.Kim@Sun.COM 			    dnode->ddn_dpath, &err) != 0) {
151812126SHyon.Kim@Sun.COM 				topo_mod_dprintf(mod, "ses_set_expander_props: "
151912126SHyon.Kim@Sun.COM 				    "set dev error %s\n", topo_strerror(err));
152012126SHyon.Kim@Sun.COM 			}
152112477SHyon.Kim@Sun.COM 			if (topo_prop_set_string(tnode, TOPO_PGROUP_SMP,
152212477SHyon.Kim@Sun.COM 			    TOPO_PROP_SMP_DEVID, TOPO_PROP_IMMUTABLE,
152312126SHyon.Kim@Sun.COM 			    dnode->ddn_devid, &err) != 0) {
152412126SHyon.Kim@Sun.COM 				topo_mod_dprintf(mod, "ses_set_expander_props: "
152512126SHyon.Kim@Sun.COM 				    "set devid error %s\n", topo_strerror(err));
152612126SHyon.Kim@Sun.COM 			}
152712126SHyon.Kim@Sun.COM 			if (dnode->ddn_ppath_count != 0 &&
152812477SHyon.Kim@Sun.COM 			    topo_prop_set_string_array(tnode, TOPO_PGROUP_SMP,
152912477SHyon.Kim@Sun.COM 			    TOPO_PROP_SMP_PHYS_PATH, TOPO_PROP_IMMUTABLE,
153012126SHyon.Kim@Sun.COM 			    (const char **)dnode->ddn_ppath,
153112126SHyon.Kim@Sun.COM 			    dnode->ddn_ppath_count, &err) != 0) {
153212126SHyon.Kim@Sun.COM 				topo_mod_dprintf(mod, "ses_set_expander_props: "
153312126SHyon.Kim@Sun.COM 				    "set phys-path error %s\n",
153412126SHyon.Kim@Sun.COM 				    topo_strerror(err));
153512126SHyon.Kim@Sun.COM 			}
153612126SHyon.Kim@Sun.COM 		}
153712126SHyon.Kim@Sun.COM 	}
153812126SHyon.Kim@Sun.COM 
153912477SHyon.Kim@Sun.COM 	/* update the ses property group with SES target info */
154012632SHyon.Kim@Sun.COM 	if ((topo_pgroup_create(tnode, &ses_pgroup, &err) != 0) &&
154112563SHyon.Kim@Sun.COM 	    (err != ETOPO_PROP_DEFD)) {
154212563SHyon.Kim@Sun.COM 		/* SES prop group doesn't exist but failed to be created. */
154312477SHyon.Kim@Sun.COM 		topo_mod_dprintf(mod, "ses_set_expander_props: "
154412563SHyon.Kim@Sun.COM 		    "ses pgroup create error %s\n", topo_strerror(err));
154512477SHyon.Kim@Sun.COM 		goto error;
154612477SHyon.Kim@Sun.COM 	} else {
154712477SHyon.Kim@Sun.COM 		/* locate assciated enclosure dev_di_node. */
154812477SHyon.Kim@Sun.COM 		for (sesdnode = topo_list_next(&sdp->sed_devs);
154912477SHyon.Kim@Sun.COM 		    sesdnode != NULL; sesdnode = topo_list_next(sesdnode)) {
155012477SHyon.Kim@Sun.COM 			for (i = 0; i < sesdnode->ddn_ppath_count; i++) {
155112477SHyon.Kim@Sun.COM 				/*
155212477SHyon.Kim@Sun.COM 				 * check if attached port exists and
155312477SHyon.Kim@Sun.COM 				 * its node type is enclosure and
155412477SHyon.Kim@Sun.COM 				 * attached port is same as sas address of
155512477SHyon.Kim@Sun.COM 				 * the expander and
155612477SHyon.Kim@Sun.COM 				 * bridge port for virtual phy indication
155712477SHyon.Kim@Sun.COM 				 * exist.
155812477SHyon.Kim@Sun.COM 				 */
155912477SHyon.Kim@Sun.COM 				if ((sesdnode->ddn_attached_port[i] != NULL) &&
156012477SHyon.Kim@Sun.COM 				    (sesdnode->ddn_dtype == DTYPE_ESI) &&
156112477SHyon.Kim@Sun.COM 				    (strstr(sesdnode->ddn_attached_port[i],
156212477SHyon.Kim@Sun.COM 				    sasaddr_str) != NULL) &&
156312477SHyon.Kim@Sun.COM 				    (sesdnode->ddn_bridge_port[i] != NULL)) {
156412477SHyon.Kim@Sun.COM 					ses_found = B_TRUE;
156512477SHyon.Kim@Sun.COM 					break;
156612477SHyon.Kim@Sun.COM 				}
156712477SHyon.Kim@Sun.COM 			}
156812477SHyon.Kim@Sun.COM 			if (ses_found) break;
156912477SHyon.Kim@Sun.COM 		}
157012477SHyon.Kim@Sun.COM 
157112477SHyon.Kim@Sun.COM 		if (ses_found) {
157212477SHyon.Kim@Sun.COM 			if (topo_prop_set_string(tnode, TOPO_PGROUP_SES,
157312477SHyon.Kim@Sun.COM 			    TOPO_PROP_SES_TARGET_PORT, TOPO_PROP_IMMUTABLE,
157412477SHyon.Kim@Sun.COM 			    sesdnode->ddn_target_port[i], &err) != 0) {
157512477SHyon.Kim@Sun.COM 				topo_mod_dprintf(mod, "ses_set_expander_props: "
157612477SHyon.Kim@Sun.COM 				    "set ses %S error %s\n", TOPO_PROP_SAS_ADDR,
157712477SHyon.Kim@Sun.COM 				    topo_strerror(err));
157812477SHyon.Kim@Sun.COM 			}
157912477SHyon.Kim@Sun.COM 			if (topo_prop_set_string(tnode, TOPO_PGROUP_SES,
158012477SHyon.Kim@Sun.COM 			    TOPO_PROP_SES_DEV_PATH, TOPO_PROP_IMMUTABLE,
158112477SHyon.Kim@Sun.COM 			    sesdnode->ddn_dpath, &err) != 0) {
158212477SHyon.Kim@Sun.COM 				topo_mod_dprintf(mod, "ses_set_expander_props: "
158312477SHyon.Kim@Sun.COM 				    "set ses dev error %s\n",
158412477SHyon.Kim@Sun.COM 				    topo_strerror(err));
158512477SHyon.Kim@Sun.COM 			}
158612477SHyon.Kim@Sun.COM 			if (topo_prop_set_string(tnode, TOPO_PGROUP_SES,
158712477SHyon.Kim@Sun.COM 			    TOPO_PROP_SES_DEVID, TOPO_PROP_IMMUTABLE,
158812477SHyon.Kim@Sun.COM 			    sesdnode->ddn_devid, &err) != 0) {
158912477SHyon.Kim@Sun.COM 				topo_mod_dprintf(mod, "ses_set_expander_props: "
159012477SHyon.Kim@Sun.COM 				    "set ses devid error %s\n",
159112477SHyon.Kim@Sun.COM 				    topo_strerror(err));
159212477SHyon.Kim@Sun.COM 			}
159312477SHyon.Kim@Sun.COM 			if (sesdnode->ddn_ppath_count != 0 &&
159412477SHyon.Kim@Sun.COM 			    topo_prop_set_string_array(tnode, TOPO_PGROUP_SES,
159512477SHyon.Kim@Sun.COM 			    TOPO_PROP_SES_PHYS_PATH, TOPO_PROP_IMMUTABLE,
159612477SHyon.Kim@Sun.COM 			    (const char **)sesdnode->ddn_ppath,
159712477SHyon.Kim@Sun.COM 			    sesdnode->ddn_ppath_count, &err) != 0) {
159812477SHyon.Kim@Sun.COM 				topo_mod_dprintf(mod, "ses_set_expander_props: "
159912477SHyon.Kim@Sun.COM 				    "set ses phys-path error %s\n",
160012477SHyon.Kim@Sun.COM 				    topo_strerror(err));
160112477SHyon.Kim@Sun.COM 			}
160212477SHyon.Kim@Sun.COM 
160312477SHyon.Kim@Sun.COM 		}
160412477SHyon.Kim@Sun.COM 	}
160512477SHyon.Kim@Sun.COM 
160612126SHyon.Kim@Sun.COM 	/* create the storage group */
160712126SHyon.Kim@Sun.COM 	if (topo_pgroup_create(tnode, &storage_pgroup, &err) != 0) {
160812126SHyon.Kim@Sun.COM 		topo_mod_dprintf(mod, "ses_set_expander_props: "
160912126SHyon.Kim@Sun.COM 		    "create storage error %s\n", topo_strerror(err));
161012126SHyon.Kim@Sun.COM 		goto error;
161112126SHyon.Kim@Sun.COM 	} else {
161212477SHyon.Kim@Sun.COM 		/* set the SAS address prop out of expander element status. */
161312126SHyon.Kim@Sun.COM 		if (topo_prop_set_string(tnode, TOPO_PGROUP_STORAGE,
161412126SHyon.Kim@Sun.COM 		    TOPO_PROP_SAS_ADDR, TOPO_PROP_IMMUTABLE, sasaddr_str,
161512126SHyon.Kim@Sun.COM 		    &err) != 0) {
161612126SHyon.Kim@Sun.COM 			topo_mod_dprintf(mod, "ses_set_expander_props: "
161712126SHyon.Kim@Sun.COM 			    "set %S error %s\n", TOPO_PROP_SAS_ADDR,
161812126SHyon.Kim@Sun.COM 			    topo_strerror(err));
161912126SHyon.Kim@Sun.COM 		}
162012126SHyon.Kim@Sun.COM 
162112126SHyon.Kim@Sun.COM 		/* Get the phy information for the expander */
162212126SHyon.Kim@Sun.COM 		if (nvlist_lookup_nvlist_array(props, SES_SAS_PROP_PHYS,
162312126SHyon.Kim@Sun.COM 		    &phylist, &pcount) != 0) {
162412126SHyon.Kim@Sun.COM 			topo_mod_dprintf(mod,
162512126SHyon.Kim@Sun.COM 			    "Failed to get prop %s.", SES_SAS_PROP_PHYS);
162612126SHyon.Kim@Sun.COM 		} else {
162712126SHyon.Kim@Sun.COM 			/*
162812126SHyon.Kim@Sun.COM 			 * For each phy, get the connector element index and
162912126SHyon.Kim@Sun.COM 			 * stores into connector element index array.
163012126SHyon.Kim@Sun.COM 			 */
163112126SHyon.Kim@Sun.COM 			*phycount = pcount;
163212126SHyon.Kim@Sun.COM 			for (i = 0; i < pcount; i++) {
163312126SHyon.Kim@Sun.COM 				if (nvlist_lookup_uint64(phylist[i],
163412126SHyon.Kim@Sun.COM 				    SES_PROP_CE_IDX, &connidx) == 0) {
163512126SHyon.Kim@Sun.COM 					if (connidx != 0xff) {
163612126SHyon.Kim@Sun.COM 						connlist[i] = connidx;
163712126SHyon.Kim@Sun.COM 					} else {
163812126SHyon.Kim@Sun.COM 						connlist[i] = -1;
163912126SHyon.Kim@Sun.COM 					}
164012126SHyon.Kim@Sun.COM 				} else {
164112126SHyon.Kim@Sun.COM 					/* Fail to get the index. set to -1. */
164212126SHyon.Kim@Sun.COM 					connlist[i] = -1;
164312126SHyon.Kim@Sun.COM 				}
164412126SHyon.Kim@Sun.COM 			}
164512126SHyon.Kim@Sun.COM 
164612126SHyon.Kim@Sun.COM 			/* set the phy count prop of the expander. */
164712126SHyon.Kim@Sun.COM 			if (topo_prop_set_uint64(tnode, TOPO_PGROUP_STORAGE,
164812126SHyon.Kim@Sun.COM 			    TOPO_PROP_PHY_COUNT, TOPO_PROP_IMMUTABLE, pcount,
164912126SHyon.Kim@Sun.COM 			    &err) != 0) {
165012126SHyon.Kim@Sun.COM 				topo_mod_dprintf(mod, "ses_set_expander_props: "
165112126SHyon.Kim@Sun.COM 				    "set %S error %s\n", TOPO_PROP_PHY_COUNT,
165212126SHyon.Kim@Sun.COM 				    topo_strerror(err));
165312126SHyon.Kim@Sun.COM 			}
165412126SHyon.Kim@Sun.COM 
165512126SHyon.Kim@Sun.COM 			/*
165612126SHyon.Kim@Sun.COM 			 * set the connector element index of
165712126SHyon.Kim@Sun.COM 			 * the expander phys.
165812126SHyon.Kim@Sun.COM 			 */
165912126SHyon.Kim@Sun.COM 		}
166012126SHyon.Kim@Sun.COM 
166112126SHyon.Kim@Sun.COM 		/* populate other misc storage group properties */
166212126SHyon.Kim@Sun.COM 		if (found) {
166312126SHyon.Kim@Sun.COM 			if (dnode->ddn_mfg && (topo_prop_set_string(tnode,
166412126SHyon.Kim@Sun.COM 			    TOPO_PGROUP_STORAGE, TOPO_STORAGE_MANUFACTURER,
166512126SHyon.Kim@Sun.COM 			    TOPO_PROP_IMMUTABLE, dnode->ddn_mfg, &err) != 0)) {
166612126SHyon.Kim@Sun.COM 				topo_mod_dprintf(mod, "ses_set_expander_props: "
166712126SHyon.Kim@Sun.COM 				    "set mfg error %s\n", topo_strerror(err));
166812126SHyon.Kim@Sun.COM 			}
166912126SHyon.Kim@Sun.COM 
167012126SHyon.Kim@Sun.COM 			if (dnode->ddn_model && (topo_prop_set_string(tnode,
167112126SHyon.Kim@Sun.COM 			    TOPO_PGROUP_STORAGE, TOPO_STORAGE_MODEL,
167212126SHyon.Kim@Sun.COM 			    TOPO_PROP_IMMUTABLE,
167312126SHyon.Kim@Sun.COM 			    dnode->ddn_model, &err) != 0)) {
167412126SHyon.Kim@Sun.COM 				topo_mod_dprintf(mod, "ses_set_expander_props: "
167512126SHyon.Kim@Sun.COM 				    "set model error %s\n", topo_strerror(err));
167612126SHyon.Kim@Sun.COM 			}
167712126SHyon.Kim@Sun.COM 
167812126SHyon.Kim@Sun.COM 			if (dnode->ddn_serial && (topo_prop_set_string(tnode,
167912126SHyon.Kim@Sun.COM 			    TOPO_PGROUP_STORAGE, TOPO_STORAGE_SERIAL_NUM,
168012126SHyon.Kim@Sun.COM 			    TOPO_PROP_IMMUTABLE,
168112126SHyon.Kim@Sun.COM 			    dnode->ddn_serial, &err) != 0)) {
168212126SHyon.Kim@Sun.COM 				topo_mod_dprintf(mod, "ses_set_expander_props: "
168312126SHyon.Kim@Sun.COM 				    "set serial error %s\n",
168412126SHyon.Kim@Sun.COM 				    topo_strerror(err));
168512126SHyon.Kim@Sun.COM 			}
168612126SHyon.Kim@Sun.COM 
168712126SHyon.Kim@Sun.COM 			if (dnode->ddn_firm && (topo_prop_set_string(tnode,
168812126SHyon.Kim@Sun.COM 			    TOPO_PGROUP_STORAGE,
168912126SHyon.Kim@Sun.COM 			    TOPO_STORAGE_FIRMWARE_REV, TOPO_PROP_IMMUTABLE,
169012126SHyon.Kim@Sun.COM 			    dnode->ddn_firm, &err) != 0)) {
169112126SHyon.Kim@Sun.COM 				topo_mod_dprintf(mod, "ses_set_expander_props: "
169212126SHyon.Kim@Sun.COM 				    "set firm error %s\n", topo_strerror(err));
169312126SHyon.Kim@Sun.COM 			}
169412126SHyon.Kim@Sun.COM 		}
169512126SHyon.Kim@Sun.COM 	}
169612126SHyon.Kim@Sun.COM 
16976869Seschrock 	return (0);
16986869Seschrock 
16996869Seschrock error:
17006869Seschrock 	nvlist_free(auth);
17016869Seschrock 	nvlist_free(fmri);
17026869Seschrock 	return (-1);
17036869Seschrock }
17046869Seschrock 
17056869Seschrock /*
170612126SHyon.Kim@Sun.COM  * Create SAS expander specific props.
170712126SHyon.Kim@Sun.COM  */
170812126SHyon.Kim@Sun.COM /*ARGSUSED*/
170912126SHyon.Kim@Sun.COM static int
ses_set_connector_props(ses_enum_data_t * sdp,ses_enum_node_t * snp,tnode_t * tnode,int64_t phy_mask)171012126SHyon.Kim@Sun.COM ses_set_connector_props(ses_enum_data_t *sdp, ses_enum_node_t *snp,
171112126SHyon.Kim@Sun.COM     tnode_t *tnode, int64_t phy_mask)
171212126SHyon.Kim@Sun.COM {
171312126SHyon.Kim@Sun.COM 	ses_node_t *np = snp->sen_node;
171412126SHyon.Kim@Sun.COM 	topo_mod_t *mod = sdp->sed_mod;
171512126SHyon.Kim@Sun.COM 	nvlist_t *props;
171612126SHyon.Kim@Sun.COM 	int err, i;
171712126SHyon.Kim@Sun.COM 	uint64_t conntype;
171812126SHyon.Kim@Sun.COM 	char phymask_str[17], *conntype_str;
171912126SHyon.Kim@Sun.COM 	boolean_t   found;
172012126SHyon.Kim@Sun.COM 
172112126SHyon.Kim@Sun.COM 	props = ses_node_props(np);
172212126SHyon.Kim@Sun.COM 
172312126SHyon.Kim@Sun.COM 	/*
172412126SHyon.Kim@Sun.COM 	 * convert phy mask to string.
172512126SHyon.Kim@Sun.COM 	 */
172612126SHyon.Kim@Sun.COM 	(void) snprintf(phymask_str, 17, "%llx", phy_mask);
172712126SHyon.Kim@Sun.COM 
172812126SHyon.Kim@Sun.COM 	/* create the storage group */
172912126SHyon.Kim@Sun.COM 	if (topo_pgroup_create(tnode, &storage_pgroup, &err) != 0) {
173012126SHyon.Kim@Sun.COM 		topo_mod_dprintf(mod, "ses_set_expander_props: "
173112126SHyon.Kim@Sun.COM 		    "create storage error %s\n", topo_strerror(err));
173212126SHyon.Kim@Sun.COM 		return (-1);
173312126SHyon.Kim@Sun.COM 	} else {
173412126SHyon.Kim@Sun.COM 		/* set the SAS address prop of the expander. */
173512126SHyon.Kim@Sun.COM 		if (topo_prop_set_string(tnode, TOPO_PGROUP_STORAGE,
173612126SHyon.Kim@Sun.COM 		    TOPO_STORAGE_SAS_PHY_MASK, TOPO_PROP_IMMUTABLE,
173712126SHyon.Kim@Sun.COM 		    phymask_str, &err) != 0) {
173812126SHyon.Kim@Sun.COM 			topo_mod_dprintf(mod, "ses_set_expander_props: "
173912126SHyon.Kim@Sun.COM 			    "set %S error %s\n", TOPO_STORAGE_SAS_PHY_MASK,
174012126SHyon.Kim@Sun.COM 			    topo_strerror(err));
174112126SHyon.Kim@Sun.COM 		}
174212126SHyon.Kim@Sun.COM 
174312126SHyon.Kim@Sun.COM 		/* Get the connector type information for the expander */
174412126SHyon.Kim@Sun.COM 		if (nvlist_lookup_uint64(props,
174512126SHyon.Kim@Sun.COM 		    SES_SC_PROP_CONNECTOR_TYPE, &conntype) != 0) {
174612126SHyon.Kim@Sun.COM 			topo_mod_dprintf(mod, "Failed to get prop %s.",
174712126SHyon.Kim@Sun.COM 			    TOPO_STORAGE_SAS_PHY_MASK);
174812126SHyon.Kim@Sun.COM 		} else {
174912126SHyon.Kim@Sun.COM 			found = B_FALSE;
175012126SHyon.Kim@Sun.COM 			for (i = 0; ; i++) {
175112632SHyon.Kim@Sun.COM 				if (sas_connector_type_list[i].sct_type ==
175212126SHyon.Kim@Sun.COM 				    SAS_CONNECTOR_TYPE_CODE_NOT_DEFINED) {
175312126SHyon.Kim@Sun.COM 					break;
175412126SHyon.Kim@Sun.COM 				}
175512632SHyon.Kim@Sun.COM 				if (sas_connector_type_list[i].sct_type ==
175612126SHyon.Kim@Sun.COM 				    conntype) {
175712126SHyon.Kim@Sun.COM 					conntype_str =
175812632SHyon.Kim@Sun.COM 					    sas_connector_type_list[i].sct_name;
175912126SHyon.Kim@Sun.COM 					found = B_TRUE;
176012126SHyon.Kim@Sun.COM 					break;
176112126SHyon.Kim@Sun.COM 				}
176212126SHyon.Kim@Sun.COM 			}
176312126SHyon.Kim@Sun.COM 
176412126SHyon.Kim@Sun.COM 			if (!found) {
176512126SHyon.Kim@Sun.COM 				if (conntype <
176612126SHyon.Kim@Sun.COM 				    SAS_CONNECTOR_TYPE_CODE_NOT_DEFINED) {
176712126SHyon.Kim@Sun.COM 					conntype_str =
176812126SHyon.Kim@Sun.COM 					    SAS_CONNECTOR_TYPE_RESERVED;
176912126SHyon.Kim@Sun.COM 				} else {
177012126SHyon.Kim@Sun.COM 					conntype_str =
177112126SHyon.Kim@Sun.COM 					    SAS_CONNECTOR_TYPE_NOT_DEFINED;
177212126SHyon.Kim@Sun.COM 				}
177312126SHyon.Kim@Sun.COM 			}
177412126SHyon.Kim@Sun.COM 
177512126SHyon.Kim@Sun.COM 			/* set the phy count prop of the expander. */
177612126SHyon.Kim@Sun.COM 			if (topo_prop_set_string(tnode, TOPO_PGROUP_STORAGE,
177712126SHyon.Kim@Sun.COM 			    TOPO_STORAGE_SAS_CONNECTOR_TYPE,
177812126SHyon.Kim@Sun.COM 			    TOPO_PROP_IMMUTABLE, conntype_str, &err) != 0) {
177912126SHyon.Kim@Sun.COM 				topo_mod_dprintf(mod, "ses_set_expander_props: "
178012126SHyon.Kim@Sun.COM 				    "set %S error %s\n", TOPO_PROP_PHY_COUNT,
178112126SHyon.Kim@Sun.COM 				    topo_strerror(err));
178212126SHyon.Kim@Sun.COM 			}
178312126SHyon.Kim@Sun.COM 		}
178412126SHyon.Kim@Sun.COM 	}
178512126SHyon.Kim@Sun.COM 
178612126SHyon.Kim@Sun.COM 	return (0);
178712126SHyon.Kim@Sun.COM }
178812126SHyon.Kim@Sun.COM 
178912126SHyon.Kim@Sun.COM /*
179012126SHyon.Kim@Sun.COM  * Instantiate SAS expander nodes for a given ESC Electronics node(controller)
179112126SHyon.Kim@Sun.COM  * nodes.
179212126SHyon.Kim@Sun.COM  */
179312126SHyon.Kim@Sun.COM /*ARGSUSED*/
179412126SHyon.Kim@Sun.COM static int
ses_create_esc_sasspecific(ses_enum_data_t * sdp,ses_enum_node_t * snp,tnode_t * pnode,ses_enum_chassis_t * cp,boolean_t dorange)179512126SHyon.Kim@Sun.COM ses_create_esc_sasspecific(ses_enum_data_t *sdp, ses_enum_node_t *snp,
179612126SHyon.Kim@Sun.COM     tnode_t *pnode, ses_enum_chassis_t *cp,
179712126SHyon.Kim@Sun.COM     boolean_t dorange)
179812126SHyon.Kim@Sun.COM {
179912126SHyon.Kim@Sun.COM 	topo_mod_t *mod = sdp->sed_mod;
180012126SHyon.Kim@Sun.COM 	tnode_t	*exptn, *contn;
180112126SHyon.Kim@Sun.COM 	boolean_t found;
180212126SHyon.Kim@Sun.COM 	sas_connector_phy_data_t connectors[64] = {NULL};
180312126SHyon.Kim@Sun.COM 	uint64_t max;
180412126SHyon.Kim@Sun.COM 	ses_enum_node_t *ctlsnp, *xsnp, *consnp;
180512126SHyon.Kim@Sun.COM 	ses_node_t *np = snp->sen_node;
180612126SHyon.Kim@Sun.COM 	nvlist_t *props, *psprops;
180712126SHyon.Kim@Sun.COM 	uint64_t index, psindex, conindex, psstatus, i, j, count;
180812126SHyon.Kim@Sun.COM 	int64_t cidxlist[256] = {NULL};
180912126SHyon.Kim@Sun.COM 	int phycount;
181012126SHyon.Kim@Sun.COM 
181112126SHyon.Kim@Sun.COM 	props = ses_node_props(np);
181212126SHyon.Kim@Sun.COM 
181312126SHyon.Kim@Sun.COM 	if (nvlist_lookup_uint64(props, SES_PROP_ELEMENT_ONLY_INDEX,
181412126SHyon.Kim@Sun.COM 	    &index) != 0)
181512126SHyon.Kim@Sun.COM 		return (-1);
181612126SHyon.Kim@Sun.COM 
181712126SHyon.Kim@Sun.COM 	/*
181812126SHyon.Kim@Sun.COM 	 * For SES constroller node, check to see if there are
181912126SHyon.Kim@Sun.COM 	 * associated SAS expanders.
182012126SHyon.Kim@Sun.COM 	 */
182112126SHyon.Kim@Sun.COM 	found = B_FALSE;
182212126SHyon.Kim@Sun.COM 	max = 0;
182312126SHyon.Kim@Sun.COM 	for (ctlsnp = topo_list_next(&cp->sec_nodes); ctlsnp != NULL;
182412126SHyon.Kim@Sun.COM 	    ctlsnp = topo_list_next(ctlsnp)) {
182512126SHyon.Kim@Sun.COM 		if (ctlsnp->sen_type == SES_ET_SAS_EXPANDER) {
182612126SHyon.Kim@Sun.COM 			found = B_TRUE;
182712126SHyon.Kim@Sun.COM 			if (ctlsnp->sen_instance > max)
182812126SHyon.Kim@Sun.COM 				max = ctlsnp->sen_instance;
182912126SHyon.Kim@Sun.COM 		}
183012126SHyon.Kim@Sun.COM 	}
183112126SHyon.Kim@Sun.COM 
183212126SHyon.Kim@Sun.COM 	/*
183312126SHyon.Kim@Sun.COM 	 * No SAS expander found notthing to process.
183412126SHyon.Kim@Sun.COM 	 */
183512126SHyon.Kim@Sun.COM 	if (!found)
183612126SHyon.Kim@Sun.COM 		return (0);
183712126SHyon.Kim@Sun.COM 
183812126SHyon.Kim@Sun.COM 	topo_mod_dprintf(mod, "%s Controller %d: creating "
183912126SHyon.Kim@Sun.COM 	    "%llu %s nodes", cp->sec_csn, index, max + 1, SASEXPANDER);
184012126SHyon.Kim@Sun.COM 
184112126SHyon.Kim@Sun.COM 	/*
184212126SHyon.Kim@Sun.COM 	 * The max number represent the number of elements
184312126SHyon.Kim@Sun.COM 	 * deducted from the highest SES_PROP_ELEMENT_CLASS_INDEX
184412126SHyon.Kim@Sun.COM 	 * of SET_ET_SAS_EXPANDER type element.
184512126SHyon.Kim@Sun.COM 	 *
184612126SHyon.Kim@Sun.COM 	 * There may be multiple ESC Electronics element(controllers)
184712126SHyon.Kim@Sun.COM 	 * within JBOD(typicall two for redundancy) and SAS expander
184812126SHyon.Kim@Sun.COM 	 * elements are associated with only one of them.  We are
184912126SHyon.Kim@Sun.COM 	 * still creating the range based max number here.
185012126SHyon.Kim@Sun.COM 	 * That will cover the case that all expanders are associated
185112126SHyon.Kim@Sun.COM 	 * with one SES controller.
185212126SHyon.Kim@Sun.COM 	 */
185312126SHyon.Kim@Sun.COM 	if (dorange && topo_node_range_create(mod, pnode,
185412126SHyon.Kim@Sun.COM 	    SASEXPANDER, 0, max) != 0) {
185512126SHyon.Kim@Sun.COM 		topo_mod_dprintf(mod,
185612126SHyon.Kim@Sun.COM 		    "topo_node_create_range() failed: %s",
185712126SHyon.Kim@Sun.COM 		    topo_mod_errmsg(mod));
185812126SHyon.Kim@Sun.COM 		return (-1);
185912126SHyon.Kim@Sun.COM 	}
186012126SHyon.Kim@Sun.COM 
186112126SHyon.Kim@Sun.COM 	/*
186212126SHyon.Kim@Sun.COM 	 * Search exapnders with the parent index matching with
186312126SHyon.Kim@Sun.COM 	 * ESC Electronics element index.
186412126SHyon.Kim@Sun.COM 	 * Note the index used here is a global index across
186512126SHyon.Kim@Sun.COM 	 * SES elements.
186612126SHyon.Kim@Sun.COM 	 */
186712126SHyon.Kim@Sun.COM 	for (xsnp = topo_list_next(&cp->sec_nodes); xsnp != NULL;
186812126SHyon.Kim@Sun.COM 	    xsnp = topo_list_next(xsnp)) {
186912126SHyon.Kim@Sun.COM 		if (xsnp->sen_type == SES_ET_SAS_EXPANDER) {
187012126SHyon.Kim@Sun.COM 			/*
187112126SHyon.Kim@Sun.COM 			 * get the parent ESC controller.
187212126SHyon.Kim@Sun.COM 			 */
187312126SHyon.Kim@Sun.COM 			psprops = ses_node_props(xsnp->sen_node);
187412126SHyon.Kim@Sun.COM 			if (nvlist_lookup_uint64(psprops,
187512126SHyon.Kim@Sun.COM 			    SES_PROP_STATUS_CODE, &psstatus) == 0) {
187612126SHyon.Kim@Sun.COM 				if (psstatus == SES_ESC_NOT_INSTALLED) {
187712126SHyon.Kim@Sun.COM 					/*
187812126SHyon.Kim@Sun.COM 					 * Not installed.
187912126SHyon.Kim@Sun.COM 					 * Don't create a ndoe.
188012126SHyon.Kim@Sun.COM 					 */
188112126SHyon.Kim@Sun.COM 					continue;
188212126SHyon.Kim@Sun.COM 				}
188312126SHyon.Kim@Sun.COM 			} else {
188412126SHyon.Kim@Sun.COM 				/*
188512126SHyon.Kim@Sun.COM 				 * The element should have status code.
188612126SHyon.Kim@Sun.COM 				 * If not there is no way to find
188712126SHyon.Kim@Sun.COM 				 * out if the expander element exist or
188812126SHyon.Kim@Sun.COM 				 * not.
188912126SHyon.Kim@Sun.COM 				 */
189012126SHyon.Kim@Sun.COM 				continue;
189112126SHyon.Kim@Sun.COM 			}
189212126SHyon.Kim@Sun.COM 
189312126SHyon.Kim@Sun.COM 			/* Get the physical parent index to compare. */
189412126SHyon.Kim@Sun.COM 			if (nvlist_lookup_uint64(psprops,
189512126SHyon.Kim@Sun.COM 			    LIBSES_PROP_PHYS_PARENT, &psindex) == 0) {
189612126SHyon.Kim@Sun.COM 				if (index == psindex) {
189712126SHyon.Kim@Sun.COM 		/* indentation moved forward */
189812126SHyon.Kim@Sun.COM 		/*
189912126SHyon.Kim@Sun.COM 		 * Handle basic node information of SAS expander
190012126SHyon.Kim@Sun.COM 		 * element - binding to parent node and
190112126SHyon.Kim@Sun.COM 		 * allocating FMRI...
190212126SHyon.Kim@Sun.COM 		 */
190312632SHyon.Kim@Sun.COM 		if (ses_create_generic(sdp, xsnp, pnode, pnode, SASEXPANDER,
190412126SHyon.Kim@Sun.COM 		    "SAS-EXPANDER", &exptn) != 0)
190512126SHyon.Kim@Sun.COM 			continue;
190612126SHyon.Kim@Sun.COM 		/*
190712126SHyon.Kim@Sun.COM 		 * Now handle SAS expander unique portion of node creation.
190812126SHyon.Kim@Sun.COM 		 * The max nubmer of the phy count is 256 since SES-2
190912126SHyon.Kim@Sun.COM 		 * defines as 1 byte field.  The cidxlist has the same
191012126SHyon.Kim@Sun.COM 		 * number of elements.
191112126SHyon.Kim@Sun.COM 		 *
191212126SHyon.Kim@Sun.COM 		 * We use size 64 array to store the connectors.
191312126SHyon.Kim@Sun.COM 		 * Typically a connectors associated with 4 phys so that
191412126SHyon.Kim@Sun.COM 		 * matches with the max number of connecters associated
191512126SHyon.Kim@Sun.COM 		 * with an expander.
191612126SHyon.Kim@Sun.COM 		 * The phy count goes up to 38 for Sun supported
191712126SHyon.Kim@Sun.COM 		 * JBOD.
191812126SHyon.Kim@Sun.COM 		 */
191912126SHyon.Kim@Sun.COM 		memset(cidxlist, 0, sizeof (int64_t) * 64);
192012126SHyon.Kim@Sun.COM 		if (ses_set_expander_props(sdp, xsnp, pnode, exptn, &phycount,
192112126SHyon.Kim@Sun.COM 		    cidxlist) != 0) {
192212126SHyon.Kim@Sun.COM 			/*
192312126SHyon.Kim@Sun.COM 			 * error on getting specific prop failed.
192412126SHyon.Kim@Sun.COM 			 * continue on.  Note that the node is
192512126SHyon.Kim@Sun.COM 			 * left bound.
192612126SHyon.Kim@Sun.COM 			 */
192712126SHyon.Kim@Sun.COM 			continue;
192812126SHyon.Kim@Sun.COM 		}
192912126SHyon.Kim@Sun.COM 
193012126SHyon.Kim@Sun.COM 		/*
193112126SHyon.Kim@Sun.COM 		 * count represetns the number of connectors discovered so far.
193212126SHyon.Kim@Sun.COM 		 */
193312126SHyon.Kim@Sun.COM 		count = 0;
193412126SHyon.Kim@Sun.COM 		memset(connectors, 0, sizeof (sas_connector_phy_data_t) * 64);
193512126SHyon.Kim@Sun.COM 		for (i = 0; i < phycount; i++) {
193612126SHyon.Kim@Sun.COM 			if (cidxlist[i] != -1) {
193712126SHyon.Kim@Sun.COM 				/* connector index is valid. */
193812126SHyon.Kim@Sun.COM 				for (j = 0; j < count; j++) {
193912632SHyon.Kim@Sun.COM 					if (connectors[j].scpd_index ==
194012126SHyon.Kim@Sun.COM 					    cidxlist[i]) {
194112126SHyon.Kim@Sun.COM 						/*
194212126SHyon.Kim@Sun.COM 						 * Just update phy mask.
194312126SHyon.Kim@Sun.COM 						 * The postion for connector
194412126SHyon.Kim@Sun.COM 						 * index lists(cidxlist index)
194512126SHyon.Kim@Sun.COM 						 * is set.
194612126SHyon.Kim@Sun.COM 						 */
194712632SHyon.Kim@Sun.COM 						connectors[j].scpd_pm =
194812632SHyon.Kim@Sun.COM 						    connectors[j].scpd_pm |
194912126SHyon.Kim@Sun.COM 						    (1ULL << i);
195012126SHyon.Kim@Sun.COM 						break;
195112126SHyon.Kim@Sun.COM 					}
195212126SHyon.Kim@Sun.COM 				}
195312126SHyon.Kim@Sun.COM 				/*
195412126SHyon.Kim@Sun.COM 				 * If j and count matche a  new connector
195512126SHyon.Kim@Sun.COM 				 * index is found.
195612126SHyon.Kim@Sun.COM 				 */
195712126SHyon.Kim@Sun.COM 				if (j == count) {
195812126SHyon.Kim@Sun.COM 					/* add a new index and phy mask. */
195912632SHyon.Kim@Sun.COM 					connectors[count].scpd_index =
196012632SHyon.Kim@Sun.COM 					    cidxlist[i];
196112632SHyon.Kim@Sun.COM 					connectors[count].scpd_pm =
196212632SHyon.Kim@Sun.COM 					    connectors[count].scpd_pm |
196312126SHyon.Kim@Sun.COM 					    (1ULL << i);
196412126SHyon.Kim@Sun.COM 					count++;
196512126SHyon.Kim@Sun.COM 				}
196612126SHyon.Kim@Sun.COM 			}
196712126SHyon.Kim@Sun.COM 		}
196812126SHyon.Kim@Sun.COM 
196912126SHyon.Kim@Sun.COM 		/*
197012126SHyon.Kim@Sun.COM 		 * create range for the connector nodes.
197112126SHyon.Kim@Sun.COM 		 * The class index of the ses connector element
197212126SHyon.Kim@Sun.COM 		 * is set as the instance nubmer for the node.
197312126SHyon.Kim@Sun.COM 		 * Even though one expander may not have all connectors
197412126SHyon.Kim@Sun.COM 		 * are associated with we are creating the range with
197512126SHyon.Kim@Sun.COM 		 * max possible instance number.
197612126SHyon.Kim@Sun.COM 		 */
197712126SHyon.Kim@Sun.COM 		found = B_FALSE;
197812126SHyon.Kim@Sun.COM 		max = 0;
197912126SHyon.Kim@Sun.COM 		for (consnp = topo_list_next(&cp->sec_nodes);
198012126SHyon.Kim@Sun.COM 		    consnp != NULL; consnp = topo_list_next(consnp)) {
198112126SHyon.Kim@Sun.COM 			if (consnp->sen_type == SES_ET_SAS_CONNECTOR) {
198212126SHyon.Kim@Sun.COM 				psprops = ses_node_props(consnp->sen_node);
198312126SHyon.Kim@Sun.COM 				found = B_TRUE;
198412126SHyon.Kim@Sun.COM 				if (consnp->sen_instance > max)
198512126SHyon.Kim@Sun.COM 					max = consnp->sen_instance;
198612126SHyon.Kim@Sun.COM 			}
198712126SHyon.Kim@Sun.COM 		}
198812126SHyon.Kim@Sun.COM 
198912126SHyon.Kim@Sun.COM 		/*
199012126SHyon.Kim@Sun.COM 		 * No SAS connector found nothing to process.
199112126SHyon.Kim@Sun.COM 		 */
199212126SHyon.Kim@Sun.COM 		if (!found)
199312126SHyon.Kim@Sun.COM 			return (0);
199412126SHyon.Kim@Sun.COM 
199512126SHyon.Kim@Sun.COM 		if (dorange && topo_node_range_create(mod, exptn,
199612126SHyon.Kim@Sun.COM 		    RECEPTACLE, 0, max) != 0) {
199712126SHyon.Kim@Sun.COM 			topo_mod_dprintf(mod,
199812126SHyon.Kim@Sun.COM 			    "topo_node_create_range() failed: %s",
199912126SHyon.Kim@Sun.COM 			    topo_mod_errmsg(mod));
200012126SHyon.Kim@Sun.COM 			return (-1);
200112126SHyon.Kim@Sun.COM 		}
200212126SHyon.Kim@Sun.COM 
200312126SHyon.Kim@Sun.COM 		/* search matching connector element using the index. */
200412126SHyon.Kim@Sun.COM 		for (i = 0; i < count; i++) {
200512126SHyon.Kim@Sun.COM 			found = B_FALSE;
200612126SHyon.Kim@Sun.COM 			for (consnp = topo_list_next(&cp->sec_nodes);
200712126SHyon.Kim@Sun.COM 			    consnp != NULL; consnp = topo_list_next(consnp)) {
200812126SHyon.Kim@Sun.COM 				if (consnp->sen_type == SES_ET_SAS_CONNECTOR) {
200912126SHyon.Kim@Sun.COM 					psprops = ses_node_props(
201012126SHyon.Kim@Sun.COM 					    consnp->sen_node);
201112126SHyon.Kim@Sun.COM 					/*
201212126SHyon.Kim@Sun.COM 					 * Get the physical parent index to
201312126SHyon.Kim@Sun.COM 					 * compare.
201412126SHyon.Kim@Sun.COM 					 * The connector elements are children
201512126SHyon.Kim@Sun.COM 					 * of ESC Electronics element even
201612126SHyon.Kim@Sun.COM 					 * though we enumerate them under
201712126SHyon.Kim@Sun.COM 					 * an expander in libtopo.
201812126SHyon.Kim@Sun.COM 					 */
201912126SHyon.Kim@Sun.COM 					if (nvlist_lookup_uint64(psprops,
202012126SHyon.Kim@Sun.COM 					    SES_PROP_ELEMENT_ONLY_INDEX,
202112126SHyon.Kim@Sun.COM 					    &conindex) == 0) {
202212126SHyon.Kim@Sun.COM 						if (conindex ==
202312632SHyon.Kim@Sun.COM 						    connectors[i].scpd_index) {
202412126SHyon.Kim@Sun.COM 							found = B_TRUE;
202512126SHyon.Kim@Sun.COM 							break;
202612126SHyon.Kim@Sun.COM 						}
202712126SHyon.Kim@Sun.COM 					}
202812126SHyon.Kim@Sun.COM 				}
202912126SHyon.Kim@Sun.COM 			}
203012126SHyon.Kim@Sun.COM 
203112126SHyon.Kim@Sun.COM 			/* now create a libtopo node. */
203212126SHyon.Kim@Sun.COM 			if (found) {
203312126SHyon.Kim@Sun.COM 				/* Create generic props. */
203412126SHyon.Kim@Sun.COM 				if (ses_create_generic(sdp, consnp, exptn,
203512632SHyon.Kim@Sun.COM 				    topo_node_parent(exptn),
203612126SHyon.Kim@Sun.COM 				    RECEPTACLE, "RECEPTACLE", &contn) !=
203712126SHyon.Kim@Sun.COM 				    0) {
203812126SHyon.Kim@Sun.COM 					continue;
203912126SHyon.Kim@Sun.COM 				}
204012126SHyon.Kim@Sun.COM 				/* Create connector specific props. */
204112126SHyon.Kim@Sun.COM 				if (ses_set_connector_props(sdp, consnp,
204212632SHyon.Kim@Sun.COM 				    contn, connectors[i].scpd_pm) != 0) {
204312126SHyon.Kim@Sun.COM 					continue;
204412126SHyon.Kim@Sun.COM 				}
204512126SHyon.Kim@Sun.COM 			}
204612126SHyon.Kim@Sun.COM 		}
204712126SHyon.Kim@Sun.COM 		/* end indentation change */
204812126SHyon.Kim@Sun.COM 				}
204912126SHyon.Kim@Sun.COM 			}
205012126SHyon.Kim@Sun.COM 		}
205112126SHyon.Kim@Sun.COM 	}
205212126SHyon.Kim@Sun.COM 
205312126SHyon.Kim@Sun.COM 	return (0);
205412126SHyon.Kim@Sun.COM }
205512126SHyon.Kim@Sun.COM 
205612126SHyon.Kim@Sun.COM /*
205712126SHyon.Kim@Sun.COM  * Instantiate any protocol specific portion of a node.
205812126SHyon.Kim@Sun.COM  */
205912126SHyon.Kim@Sun.COM /*ARGSUSED*/
206012126SHyon.Kim@Sun.COM static int
ses_create_protocol_specific(ses_enum_data_t * sdp,ses_enum_node_t * snp,tnode_t * pnode,uint64_t type,ses_enum_chassis_t * cp,boolean_t dorange)206112126SHyon.Kim@Sun.COM ses_create_protocol_specific(ses_enum_data_t *sdp, ses_enum_node_t *snp,
206212126SHyon.Kim@Sun.COM     tnode_t *pnode, uint64_t type, ses_enum_chassis_t *cp,
206312126SHyon.Kim@Sun.COM     boolean_t dorange)
206412126SHyon.Kim@Sun.COM {
206512126SHyon.Kim@Sun.COM 
206612126SHyon.Kim@Sun.COM 	if (type == SES_ET_ESC_ELECTRONICS) {
206712126SHyon.Kim@Sun.COM 		/* create SAS specific children(expanders and connectors. */
206812126SHyon.Kim@Sun.COM 		return (ses_create_esc_sasspecific(sdp, snp, pnode, cp,
206912126SHyon.Kim@Sun.COM 		    dorange));
207012126SHyon.Kim@Sun.COM 	}
207112126SHyon.Kim@Sun.COM 
207212126SHyon.Kim@Sun.COM 	return (0);
207312126SHyon.Kim@Sun.COM }
207412126SHyon.Kim@Sun.COM 
207512126SHyon.Kim@Sun.COM /*
20766869Seschrock  * Instantiate any children of a given type.
20776869Seschrock  */
20786869Seschrock static int
ses_create_children(ses_enum_data_t * sdp,tnode_t * pnode,uint64_t type,const char * nodename,const char * defaultlabel,ses_enum_chassis_t * cp,boolean_t dorange)20796869Seschrock ses_create_children(ses_enum_data_t *sdp, tnode_t *pnode, uint64_t type,
20807269Seschrock     const char *nodename, const char *defaultlabel, ses_enum_chassis_t *cp,
20817269Seschrock     boolean_t dorange)
20826869Seschrock {
20836869Seschrock 	topo_mod_t *mod = sdp->sed_mod;
20846869Seschrock 	boolean_t found;
20856869Seschrock 	uint64_t max;
20866869Seschrock 	ses_enum_node_t *snp;
208712126SHyon.Kim@Sun.COM 	tnode_t	*tn;
20886869Seschrock 
20896869Seschrock 	/*
20906869Seschrock 	 * First go through and count how many matching nodes we have.
20916869Seschrock 	 */
20926869Seschrock 	max = 0;
20936869Seschrock 	found = B_FALSE;
20946869Seschrock 	for (snp = topo_list_next(&cp->sec_nodes); snp != NULL;
20956869Seschrock 	    snp = topo_list_next(snp)) {
20966869Seschrock 		if (snp->sen_type == type) {
20976869Seschrock 			found = B_TRUE;
20986869Seschrock 			if (snp->sen_instance > max)
20996869Seschrock 				max = snp->sen_instance;
21006869Seschrock 		}
21016869Seschrock 	}
21026869Seschrock 
21036869Seschrock 	/*
21046869Seschrock 	 * No enclosure should export both DEVICE and ARRAY_DEVICE elements.
21056869Seschrock 	 * Since we map both of these to 'disk', if an enclosure does this, we
21066869Seschrock 	 * just ignore the array elements.
21076869Seschrock 	 */
21086869Seschrock 	if (!found ||
21096869Seschrock 	    (type == SES_ET_ARRAY_DEVICE && cp->sec_hasdev))
21106869Seschrock 		return (0);
21116869Seschrock 
21126869Seschrock 	topo_mod_dprintf(mod, "%s: creating %llu %s nodes",
211310519SSundeep.Panicker@Sun.COM 	    cp->sec_csn, max + 1, nodename);
21146869Seschrock 
21157269Seschrock 	if (dorange && topo_node_range_create(mod, pnode,
21166869Seschrock 	    nodename, 0, max) != 0) {
21176869Seschrock 		topo_mod_dprintf(mod,
21186869Seschrock 		    "topo_node_create_range() failed: %s",
21196869Seschrock 		    topo_mod_errmsg(mod));
21206869Seschrock 		return (-1);
21216869Seschrock 	}
21226869Seschrock 
21236869Seschrock 	for (snp = topo_list_next(&cp->sec_nodes); snp != NULL;
21246869Seschrock 	    snp = topo_list_next(snp)) {
21256869Seschrock 		if (snp->sen_type == type) {
212612632SHyon.Kim@Sun.COM 			/*
212712632SHyon.Kim@Sun.COM 			 * With flat layout of ses nodes there is no
212812632SHyon.Kim@Sun.COM 			 * way to find out the direct FRU for a node.
212912632SHyon.Kim@Sun.COM 			 * Passing NULL for fru topo node.  Note that
213012632SHyon.Kim@Sun.COM 			 * ses_create_children_from_phys_tree() provides
213112632SHyon.Kim@Sun.COM 			 * the actual direct FRU for a node.
213212632SHyon.Kim@Sun.COM 			 */
213312632SHyon.Kim@Sun.COM 			if (ses_create_generic(sdp, snp, pnode, NULL,
213412126SHyon.Kim@Sun.COM 			    nodename, defaultlabel, &tn) != 0)
21356869Seschrock 				return (-1);
213612126SHyon.Kim@Sun.COM 			/*
213712126SHyon.Kim@Sun.COM 			 * For some SES element there may be protocol specific
213812126SHyon.Kim@Sun.COM 			 * information to process.   Here we are processing
213912126SHyon.Kim@Sun.COM 			 * the association between enclosure controller and
214012126SHyon.Kim@Sun.COM 			 * SAS expanders.
214112126SHyon.Kim@Sun.COM 			 */
214212126SHyon.Kim@Sun.COM 			if (type == SES_ET_ESC_ELECTRONICS) {
214312126SHyon.Kim@Sun.COM 				/* create SAS expander node */
214412126SHyon.Kim@Sun.COM 				if (ses_create_protocol_specific(sdp, snp,
214512126SHyon.Kim@Sun.COM 				    tn, type, cp, dorange) != 0) {
214612126SHyon.Kim@Sun.COM 					return (-1);
214712126SHyon.Kim@Sun.COM 				}
214812126SHyon.Kim@Sun.COM 			}
214912126SHyon.Kim@Sun.COM 
21506869Seschrock 		}
21516869Seschrock 	}
21526869Seschrock 
21536869Seschrock 	return (0);
21546869Seschrock }
21556869Seschrock 
21566869Seschrock /*
215710519SSundeep.Panicker@Sun.COM  * Instantiate a new subchassis instance in the topology.
215810519SSundeep.Panicker@Sun.COM  */
215910519SSundeep.Panicker@Sun.COM static int
ses_create_subchassis(ses_enum_data_t * sdp,tnode_t * pnode,ses_enum_chassis_t * scp)216010519SSundeep.Panicker@Sun.COM ses_create_subchassis(ses_enum_data_t *sdp, tnode_t *pnode,
216110519SSundeep.Panicker@Sun.COM     ses_enum_chassis_t *scp)
216210519SSundeep.Panicker@Sun.COM {
216310519SSundeep.Panicker@Sun.COM 	topo_mod_t *mod = sdp->sed_mod;
216410519SSundeep.Panicker@Sun.COM 	tnode_t *tn;
216510519SSundeep.Panicker@Sun.COM 	nvlist_t *props;
216610519SSundeep.Panicker@Sun.COM 	nvlist_t *auth = NULL, *fmri = NULL;
216710519SSundeep.Panicker@Sun.COM 	uint64_t instance = scp->sec_instance;
216810519SSundeep.Panicker@Sun.COM 	char *desc;
216910519SSundeep.Panicker@Sun.COM 	char label[128];
217010519SSundeep.Panicker@Sun.COM 	char **paths;
217110519SSundeep.Panicker@Sun.COM 	int i, err;
217210519SSundeep.Panicker@Sun.COM 	ses_enum_target_t *stp;
217310519SSundeep.Panicker@Sun.COM 	int ret = -1;
217410519SSundeep.Panicker@Sun.COM 
217510519SSundeep.Panicker@Sun.COM 	/*
217610519SSundeep.Panicker@Sun.COM 	 * Copy authority information from parent enclosure node
217710519SSundeep.Panicker@Sun.COM 	 */
217810519SSundeep.Panicker@Sun.COM 	if ((auth = topo_mod_auth(mod, pnode)) == NULL)
217910519SSundeep.Panicker@Sun.COM 		goto error;
218010519SSundeep.Panicker@Sun.COM 
218110519SSundeep.Panicker@Sun.COM 	/*
218210519SSundeep.Panicker@Sun.COM 	 * Record the subchassis serial number in the FMRI.
218310519SSundeep.Panicker@Sun.COM 	 * For now, we assume that logical id is the subchassis serial number.
218410519SSundeep.Panicker@Sun.COM 	 * If this assumption changes in future, then the following
218510519SSundeep.Panicker@Sun.COM 	 * piece of code will need to be updated via an RFE.
218610519SSundeep.Panicker@Sun.COM 	 */
218710519SSundeep.Panicker@Sun.COM 	if ((fmri = topo_mod_hcfmri(mod, pnode, FM_HC_SCHEME_VERSION,
218812126SHyon.Kim@Sun.COM 	    SUBCHASSIS, (topo_instance_t)instance, NULL, auth, NULL, NULL,
218912126SHyon.Kim@Sun.COM 	    NULL)) == NULL) {
219010519SSundeep.Panicker@Sun.COM 		topo_mod_dprintf(mod, "topo_mod_hcfmri() failed: %s",
219110519SSundeep.Panicker@Sun.COM 		    topo_mod_errmsg(mod));
219210519SSundeep.Panicker@Sun.COM 		goto error;
219310519SSundeep.Panicker@Sun.COM 	}
219410519SSundeep.Panicker@Sun.COM 
219510519SSundeep.Panicker@Sun.COM 	if ((tn = topo_node_bind(mod, pnode, SUBCHASSIS,
219610519SSundeep.Panicker@Sun.COM 	    instance, fmri)) == NULL) {
219710519SSundeep.Panicker@Sun.COM 		topo_mod_dprintf(mod, "topo_node_bind() failed: %s",
219810519SSundeep.Panicker@Sun.COM 		    topo_mod_errmsg(mod));
219910519SSundeep.Panicker@Sun.COM 		goto error;
220010519SSundeep.Panicker@Sun.COM 	}
220110519SSundeep.Panicker@Sun.COM 
220210519SSundeep.Panicker@Sun.COM 	props = ses_node_props(scp->sec_enclosure);
220310519SSundeep.Panicker@Sun.COM 
220410519SSundeep.Panicker@Sun.COM 	/*
220510519SSundeep.Panicker@Sun.COM 	 * Look for the subchassis label in the following order:
220610519SSundeep.Panicker@Sun.COM 	 *	<ses-description>
220710519SSundeep.Panicker@Sun.COM 	 *	<ses-class-description> <instance>
220810519SSundeep.Panicker@Sun.COM 	 *	<default-type-label> <instance>
220910519SSundeep.Panicker@Sun.COM 	 *
221010519SSundeep.Panicker@Sun.COM 	 * For subchassis, the default label is "SUBCHASSIS"
221110519SSundeep.Panicker@Sun.COM 	 */
221210519SSundeep.Panicker@Sun.COM 	if (nvlist_lookup_string(props, SES_PROP_DESCRIPTION, &desc) != 0 ||
221310519SSundeep.Panicker@Sun.COM 	    desc[0] == '\0') {
221410519SSundeep.Panicker@Sun.COM 		if (nvlist_lookup_string(props, SES_PROP_CLASS_DESCRIPTION,
221510519SSundeep.Panicker@Sun.COM 		    &desc) == 0 && desc[0] != '\0')
221610519SSundeep.Panicker@Sun.COM 			(void) snprintf(label, sizeof (label), "%s %llu", desc,
221710519SSundeep.Panicker@Sun.COM 			    instance);
221810519SSundeep.Panicker@Sun.COM 		else
221910519SSundeep.Panicker@Sun.COM 			(void) snprintf(label, sizeof (label),
222010519SSundeep.Panicker@Sun.COM 			    "SUBCHASSIS %llu", instance);
222110519SSundeep.Panicker@Sun.COM 		desc = label;
222210519SSundeep.Panicker@Sun.COM 	}
222310519SSundeep.Panicker@Sun.COM 
222410519SSundeep.Panicker@Sun.COM 	if (topo_node_label_set(tn, desc, &err) != 0)
222510519SSundeep.Panicker@Sun.COM 		goto error;
222610519SSundeep.Panicker@Sun.COM 
222712126SHyon.Kim@Sun.COM 	if (ses_set_standard_props(mod, NULL, tn, NULL,
222810519SSundeep.Panicker@Sun.COM 	    ses_node_id(scp->sec_enclosure), scp->sec_target->set_devpath) != 0)
222910519SSundeep.Panicker@Sun.COM 		goto error;
223010519SSundeep.Panicker@Sun.COM 
223110519SSundeep.Panicker@Sun.COM 	/*
223212126SHyon.Kim@Sun.COM 	 * Set the 'chassis-type' property for this subchassis.  This is either
223312126SHyon.Kim@Sun.COM 	 * 'ses-class-description' or 'subchassis'.
223412126SHyon.Kim@Sun.COM 	 */
223512126SHyon.Kim@Sun.COM 	if (nvlist_lookup_string(props, SES_PROP_CLASS_DESCRIPTION, &desc) != 0)
223612126SHyon.Kim@Sun.COM 		desc = "subchassis";
223712126SHyon.Kim@Sun.COM 
223812126SHyon.Kim@Sun.COM 	if (topo_prop_set_string(tn, TOPO_PGROUP_SES,
223912126SHyon.Kim@Sun.COM 	    TOPO_PROP_CHASSIS_TYPE, TOPO_PROP_IMMUTABLE, desc, &err) != 0) {
224012126SHyon.Kim@Sun.COM 		topo_mod_dprintf(mod, "failed to create property %s: %s\n",
224112126SHyon.Kim@Sun.COM 		    TOPO_PROP_CHASSIS_TYPE, topo_strerror(err));
224212126SHyon.Kim@Sun.COM 		goto error;
224312126SHyon.Kim@Sun.COM 	}
224412126SHyon.Kim@Sun.COM 
224512126SHyon.Kim@Sun.COM 	/*
224610519SSundeep.Panicker@Sun.COM 	 * For enclosures, we want to include all possible targets (for upgrade
224710519SSundeep.Panicker@Sun.COM 	 * purposes).
224810519SSundeep.Panicker@Sun.COM 	 */
224910519SSundeep.Panicker@Sun.COM 	for (i = 0, stp = topo_list_next(&scp->sec_targets); stp != NULL;
225010519SSundeep.Panicker@Sun.COM 	    stp = topo_list_next(stp), i++)
225110519SSundeep.Panicker@Sun.COM 		;
225210519SSundeep.Panicker@Sun.COM 
225310519SSundeep.Panicker@Sun.COM 	verify(i != 0);
225410519SSundeep.Panicker@Sun.COM 	paths = alloca(i * sizeof (char *));
225510519SSundeep.Panicker@Sun.COM 
225610519SSundeep.Panicker@Sun.COM 	for (i = 0, stp = topo_list_next(&scp->sec_targets); stp != NULL;
225710519SSundeep.Panicker@Sun.COM 	    stp = topo_list_next(stp), i++)
225810519SSundeep.Panicker@Sun.COM 		paths[i] = stp->set_devpath;
225910519SSundeep.Panicker@Sun.COM 
226010519SSundeep.Panicker@Sun.COM 	if (topo_prop_set_string_array(tn, TOPO_PGROUP_SES,
226110519SSundeep.Panicker@Sun.COM 	    TOPO_PROP_PATHS, TOPO_PROP_IMMUTABLE, (const char **)paths,
226210519SSundeep.Panicker@Sun.COM 	    i, &err) != 0) {
226310519SSundeep.Panicker@Sun.COM 		topo_mod_dprintf(mod, "failed to create property %s: %s\n",
226410519SSundeep.Panicker@Sun.COM 		    TOPO_PROP_PATHS, topo_strerror(err));
226510519SSundeep.Panicker@Sun.COM 		goto error;
226610519SSundeep.Panicker@Sun.COM 	}
226710519SSundeep.Panicker@Sun.COM 
226810519SSundeep.Panicker@Sun.COM 	if (topo_method_register(mod, tn, ses_enclosure_methods) != 0) {
226910519SSundeep.Panicker@Sun.COM 		topo_mod_dprintf(mod, "topo_method_register() failed: %s",
227010519SSundeep.Panicker@Sun.COM 		    topo_mod_errmsg(mod));
227110519SSundeep.Panicker@Sun.COM 		goto error;
227210519SSundeep.Panicker@Sun.COM 	}
227310519SSundeep.Panicker@Sun.COM 
227410519SSundeep.Panicker@Sun.COM 	/*
227510519SSundeep.Panicker@Sun.COM 	 * Create the nodes for controllers and bays.
227610519SSundeep.Panicker@Sun.COM 	 */
227710519SSundeep.Panicker@Sun.COM 	if (ses_create_children(sdp, tn, SES_ET_ESC_ELECTRONICS,
227810519SSundeep.Panicker@Sun.COM 	    CONTROLLER, "CONTROLLER", scp, B_TRUE) != 0 ||
227910519SSundeep.Panicker@Sun.COM 	    ses_create_children(sdp, tn, SES_ET_DEVICE,
228010519SSundeep.Panicker@Sun.COM 	    BAY, "BAY", scp, B_TRUE) != 0 ||
228110519SSundeep.Panicker@Sun.COM 	    ses_create_children(sdp, tn, SES_ET_ARRAY_DEVICE,
228210519SSundeep.Panicker@Sun.COM 	    BAY, "BAY", scp, B_TRUE) != 0)
228310519SSundeep.Panicker@Sun.COM 		goto error;
228410519SSundeep.Panicker@Sun.COM 
228510519SSundeep.Panicker@Sun.COM 	ret = 0;
228610519SSundeep.Panicker@Sun.COM 
228710519SSundeep.Panicker@Sun.COM error:
228810519SSundeep.Panicker@Sun.COM 	nvlist_free(auth);
228910519SSundeep.Panicker@Sun.COM 	nvlist_free(fmri);
229010519SSundeep.Panicker@Sun.COM 	return (ret);
229110519SSundeep.Panicker@Sun.COM }
229210519SSundeep.Panicker@Sun.COM 
229310519SSundeep.Panicker@Sun.COM /*
229412632SHyon.Kim@Sun.COM  * Function we use to insert a node.
229512632SHyon.Kim@Sun.COM  */
229612632SHyon.Kim@Sun.COM static int
ses_phys_tree_insert(topo_mod_t * mod,ses_phys_tree_t ** sproot,ses_phys_tree_t * child)229712632SHyon.Kim@Sun.COM ses_phys_tree_insert(topo_mod_t *mod, ses_phys_tree_t **sproot,
229812632SHyon.Kim@Sun.COM     ses_phys_tree_t *child)
229912632SHyon.Kim@Sun.COM {
230012632SHyon.Kim@Sun.COM 	uint64_t ppindex, eindex, pindex;
230112632SHyon.Kim@Sun.COM 	ses_phys_tree_t *node_ptr;
230212632SHyon.Kim@Sun.COM 	int ret = 0;
230312632SHyon.Kim@Sun.COM 
230412632SHyon.Kim@Sun.COM 	assert(sproot != NULL);
230512632SHyon.Kim@Sun.COM 	assert(child != NULL);
230612632SHyon.Kim@Sun.COM 
230712632SHyon.Kim@Sun.COM 	if (*sproot == NULL) {
230812632SHyon.Kim@Sun.COM 		*sproot = child;
230912632SHyon.Kim@Sun.COM 		return (0);
231012632SHyon.Kim@Sun.COM 	}
231112632SHyon.Kim@Sun.COM 
231212632SHyon.Kim@Sun.COM 	pindex = child->spt_pindex;
231312632SHyon.Kim@Sun.COM 	ppindex = (*sproot)->spt_pindex;
231412632SHyon.Kim@Sun.COM 	eindex = (*sproot)->spt_eonlyindex;
231512632SHyon.Kim@Sun.COM 
231612632SHyon.Kim@Sun.COM 	/*
231712632SHyon.Kim@Sun.COM 	 * If the element only index of the root is same as the physical
231812632SHyon.Kim@Sun.COM 	 * parent index of a node to be added, add the node as a child of
231912632SHyon.Kim@Sun.COM 	 * the current root.
232012632SHyon.Kim@Sun.COM 	 */
232112632SHyon.Kim@Sun.COM 	if (eindex == pindex) {
232212632SHyon.Kim@Sun.COM 		(void) ses_phys_tree_insert(mod, &(*sproot)->spt_child, child);
232312632SHyon.Kim@Sun.COM 		child->spt_parent = *sproot;
232412632SHyon.Kim@Sun.COM 	} else if (ppindex == pindex) {
232512632SHyon.Kim@Sun.COM 		/*
232612632SHyon.Kim@Sun.COM 		 * if the physical parent of the current root and the child
232712632SHyon.Kim@Sun.COM 		 * is same, then this should be a sibling node.
232812632SHyon.Kim@Sun.COM 		 * Siblings can be different element types and arrange
232912632SHyon.Kim@Sun.COM 		 * them by group.
233012632SHyon.Kim@Sun.COM 		 */
233112632SHyon.Kim@Sun.COM 		if ((*sproot)->spt_senumnode->sen_type ==
233212632SHyon.Kim@Sun.COM 		    child->spt_senumnode->sen_type) {
233312632SHyon.Kim@Sun.COM 			child->spt_sibling = *sproot;
233412632SHyon.Kim@Sun.COM 			*sproot = child;
233512632SHyon.Kim@Sun.COM 		} else {
233612632SHyon.Kim@Sun.COM 			/* add a node in front of matching element type. */
233712632SHyon.Kim@Sun.COM 			node_ptr = *sproot;
233812632SHyon.Kim@Sun.COM 			while (node_ptr->spt_sibling != NULL) {
233912632SHyon.Kim@Sun.COM 				if (node_ptr->spt_sibling->
234012632SHyon.Kim@Sun.COM 				    spt_senumnode->sen_type ==
234112632SHyon.Kim@Sun.COM 				    child->spt_senumnode->sen_type) {
234212632SHyon.Kim@Sun.COM 					child->spt_sibling =
234312632SHyon.Kim@Sun.COM 					    node_ptr->spt_sibling;
234412632SHyon.Kim@Sun.COM 					node_ptr->spt_sibling = child;
234512632SHyon.Kim@Sun.COM 					break;
234612632SHyon.Kim@Sun.COM 				}
234712632SHyon.Kim@Sun.COM 				node_ptr = node_ptr->spt_sibling;
234812632SHyon.Kim@Sun.COM 			}
234912632SHyon.Kim@Sun.COM 			/* no matching.  Add the child at the end. */
235012632SHyon.Kim@Sun.COM 			if (node_ptr->spt_sibling == NULL) {
235112632SHyon.Kim@Sun.COM 				node_ptr->spt_sibling = child;
235212632SHyon.Kim@Sun.COM 			}
235312632SHyon.Kim@Sun.COM 		}
235412632SHyon.Kim@Sun.COM 		child->spt_parent = (*sproot)->spt_parent;
235512632SHyon.Kim@Sun.COM 	} else {
235612632SHyon.Kim@Sun.COM 		/*
235712632SHyon.Kim@Sun.COM 		 * The root and the node is not directly related.
235812632SHyon.Kim@Sun.COM 		 * Try to insert to the child sub-tree first and then try to
235912632SHyon.Kim@Sun.COM 		 * insert to the sibling sub-trees.  If fails for both
236012632SHyon.Kim@Sun.COM 		 * the caller will retry insertion later.
236112632SHyon.Kim@Sun.COM 		 */
236212632SHyon.Kim@Sun.COM 		if ((*sproot)->spt_child) {
236312632SHyon.Kim@Sun.COM 			ret = ses_phys_tree_insert(mod, &(*sproot)->spt_child,
236412632SHyon.Kim@Sun.COM 			    child);
236512632SHyon.Kim@Sun.COM 		}
236612632SHyon.Kim@Sun.COM 		if ((*sproot)->spt_child == NULL || ret != 0) {
236712632SHyon.Kim@Sun.COM 			if ((*sproot)->spt_sibling) {
236812632SHyon.Kim@Sun.COM 				ret = ses_phys_tree_insert(mod,
236912632SHyon.Kim@Sun.COM 				    &(*sproot)->spt_sibling, child);
237012632SHyon.Kim@Sun.COM 			} else {
237112632SHyon.Kim@Sun.COM 				ret = 1;
237212632SHyon.Kim@Sun.COM 			}
237312632SHyon.Kim@Sun.COM 		}
237412632SHyon.Kim@Sun.COM 		return (ret);
237512632SHyon.Kim@Sun.COM 	}
237612632SHyon.Kim@Sun.COM 	return (0);
237712632SHyon.Kim@Sun.COM }
237812632SHyon.Kim@Sun.COM 
237912632SHyon.Kim@Sun.COM /*
238012632SHyon.Kim@Sun.COM  * Construct tree view of ses elements through parent phyiscal element index.
238112632SHyon.Kim@Sun.COM  * The root of tree is already constructed using the enclosure element.
238212632SHyon.Kim@Sun.COM  */
238312632SHyon.Kim@Sun.COM static int
ses_construct_phys_tree(ses_enum_data_t * sdp,ses_enum_chassis_t * cp,ses_phys_tree_t * sproot)238412632SHyon.Kim@Sun.COM ses_construct_phys_tree(ses_enum_data_t *sdp, ses_enum_chassis_t *cp,
238512632SHyon.Kim@Sun.COM     ses_phys_tree_t *sproot)
238612632SHyon.Kim@Sun.COM {
238712632SHyon.Kim@Sun.COM 	ses_enum_node_t *snp;
238812632SHyon.Kim@Sun.COM 	ses_phys_tree_t	*child;
238912632SHyon.Kim@Sun.COM 	ses_phys_tree_t	*u_watch = NULL;
239012632SHyon.Kim@Sun.COM 	ses_phys_tree_t	*u_head = NULL;
239112632SHyon.Kim@Sun.COM 	ses_phys_tree_t	*u_tail = NULL;
239212632SHyon.Kim@Sun.COM 	int u_inserted = 0, u_left = 0;
239312632SHyon.Kim@Sun.COM 	nvlist_t *props;
239412632SHyon.Kim@Sun.COM 	topo_mod_t *mod = sdp->sed_mod;
239512632SHyon.Kim@Sun.COM 
239612632SHyon.Kim@Sun.COM 	for (snp = topo_list_next(&cp->sec_nodes); snp != NULL;
239712632SHyon.Kim@Sun.COM 	    snp = topo_list_next(snp)) {
239812632SHyon.Kim@Sun.COM 		if ((child = topo_mod_zalloc(mod,
239912632SHyon.Kim@Sun.COM 		    sizeof (ses_phys_tree_t))) == NULL) {
240012632SHyon.Kim@Sun.COM 			topo_mod_dprintf(mod,
240112632SHyon.Kim@Sun.COM 			    "failed to allocate root.");
240212632SHyon.Kim@Sun.COM 			return (-1);
240312632SHyon.Kim@Sun.COM 		}
240412632SHyon.Kim@Sun.COM 		child->spt_snode = snp->sen_node;
240512632SHyon.Kim@Sun.COM 		props = ses_node_props(snp->sen_node);
240612632SHyon.Kim@Sun.COM 		if (nvlist_lookup_uint64(props,
240712632SHyon.Kim@Sun.COM 		    LIBSES_PROP_PHYS_PARENT, &child->spt_pindex) != 0) {
240812632SHyon.Kim@Sun.COM 			/*
240912632SHyon.Kim@Sun.COM 			 * the prop should exist. continue to see if
241012632SHyon.Kim@Sun.COM 			 * we can build a partial tree with other elements.
241112632SHyon.Kim@Sun.COM 			 */
241212632SHyon.Kim@Sun.COM 			topo_mod_dprintf(mod,
241312632SHyon.Kim@Sun.COM 			    "ses_construct_phys_tree(): Failed to find prop %s "
241412632SHyon.Kim@Sun.COM 			    "on ses element type %d and instance %d "
241512632SHyon.Kim@Sun.COM 			    "(CSN %s).", LIBSES_PROP_PHYS_PARENT,
241612632SHyon.Kim@Sun.COM 			    snp->sen_type, snp->sen_instance, cp->sec_csn);
241712632SHyon.Kim@Sun.COM 			topo_mod_free(mod, child, sizeof (ses_phys_tree_t));
241812632SHyon.Kim@Sun.COM 			continue;
241912632SHyon.Kim@Sun.COM 		} else {
242012632SHyon.Kim@Sun.COM 			if (nvlist_lookup_boolean_value(props,
242112632SHyon.Kim@Sun.COM 			    LIBSES_PROP_FRU, &child->spt_isfru) != 0) {
242212632SHyon.Kim@Sun.COM 				topo_mod_dprintf(mod,
242312632SHyon.Kim@Sun.COM 				    "ses_construct_phys_tree(): Failed to "
242412632SHyon.Kim@Sun.COM 				    "find prop %s on ses element type %d "
242512632SHyon.Kim@Sun.COM 				    "and instance %d (CSN %s).",
242612632SHyon.Kim@Sun.COM 				    LIBSES_PROP_FRU,
242712632SHyon.Kim@Sun.COM 				    snp->sen_type, snp->sen_instance,
242812632SHyon.Kim@Sun.COM 				    cp->sec_csn);
242912632SHyon.Kim@Sun.COM 				/*
243012632SHyon.Kim@Sun.COM 				 * Ignore if the prop doesn't exist.
243112632SHyon.Kim@Sun.COM 				 * Note that the enclosure itself should be
243212632SHyon.Kim@Sun.COM 				 * a FRU so if no FRU found the enclosure FRU
243312632SHyon.Kim@Sun.COM 				 * can be a direct FRU.
243412632SHyon.Kim@Sun.COM 				 */
243512632SHyon.Kim@Sun.COM 			}
243612632SHyon.Kim@Sun.COM 			verify(nvlist_lookup_uint64(props,
243712632SHyon.Kim@Sun.COM 			    SES_PROP_ELEMENT_ONLY_INDEX,
243812632SHyon.Kim@Sun.COM 			    &child->spt_eonlyindex) == 0);
243912632SHyon.Kim@Sun.COM 			verify(nvlist_lookup_uint64(props,
244012632SHyon.Kim@Sun.COM 			    SES_PROP_ELEMENT_CLASS_INDEX,
244112632SHyon.Kim@Sun.COM 			    &child->spt_cindex) == 0);
244212632SHyon.Kim@Sun.COM 		}
244312632SHyon.Kim@Sun.COM 		child->spt_senumnode = snp;
244412632SHyon.Kim@Sun.COM 		if (ses_phys_tree_insert(mod, &sproot, child) != 0) {
244512632SHyon.Kim@Sun.COM 			/* collect unresolved element to process later. */
244612632SHyon.Kim@Sun.COM 			if (u_head == NULL) {
244712632SHyon.Kim@Sun.COM 				u_head = child;
244812632SHyon.Kim@Sun.COM 				u_tail = child;
244912632SHyon.Kim@Sun.COM 			} else {
245012632SHyon.Kim@Sun.COM 				child->spt_sibling = u_head;
245112632SHyon.Kim@Sun.COM 				u_head = child;
245212632SHyon.Kim@Sun.COM 			}
245312632SHyon.Kim@Sun.COM 		}
245412632SHyon.Kim@Sun.COM 	}
245512632SHyon.Kim@Sun.COM 
245612632SHyon.Kim@Sun.COM 	/*
245712632SHyon.Kim@Sun.COM 	 * The parent of a child node may not be inserted yet.
245812632SHyon.Kim@Sun.COM 	 * Trying to insert the child until no child is left or
245912632SHyon.Kim@Sun.COM 	 * no child is not added further.  For the latter
246012632SHyon.Kim@Sun.COM 	 * the hierarchical relationship between elements
246112632SHyon.Kim@Sun.COM 	 * should be checked through SUNW,FRUID page.
246212632SHyon.Kim@Sun.COM 	 * u_watch is a watch dog to check the prgress of unresolved
246312632SHyon.Kim@Sun.COM 	 * node.
246412632SHyon.Kim@Sun.COM 	 */
246512632SHyon.Kim@Sun.COM 	u_watch = u_tail;
246612632SHyon.Kim@Sun.COM 	while (u_head) {
246712632SHyon.Kim@Sun.COM 		child = u_head;
246812632SHyon.Kim@Sun.COM 		u_head = u_head->spt_sibling;
246912632SHyon.Kim@Sun.COM 		if (u_head == NULL)
247012632SHyon.Kim@Sun.COM 			u_tail = NULL;
247112632SHyon.Kim@Sun.COM 		child->spt_sibling = NULL;
247212632SHyon.Kim@Sun.COM 		if (ses_phys_tree_insert(mod, &sproot, child) != 0) {
247312632SHyon.Kim@Sun.COM 			u_tail->spt_sibling = child;
247412632SHyon.Kim@Sun.COM 			u_tail = child;
247512632SHyon.Kim@Sun.COM 			if (child == u_watch) {
247612632SHyon.Kim@Sun.COM 				/*
247712632SHyon.Kim@Sun.COM 				 * We just scanned one round for the
247812632SHyon.Kim@Sun.COM 				 * unresolved list. Check to see whether we
247912632SHyon.Kim@Sun.COM 				 * have nodes inserted, if none, we should
248012632SHyon.Kim@Sun.COM 				 * break in case of an indefinite loop.
248112632SHyon.Kim@Sun.COM 				 */
248212632SHyon.Kim@Sun.COM 				if (u_inserted == 0) {
248312632SHyon.Kim@Sun.COM 					/*
248412632SHyon.Kim@Sun.COM 					 * Indicate there is unhandled node.
248512632SHyon.Kim@Sun.COM 					 * Chain free the whole unsolved
248612632SHyon.Kim@Sun.COM 					 * list here.
248712632SHyon.Kim@Sun.COM 					 */
248812632SHyon.Kim@Sun.COM 					u_left++;
248912632SHyon.Kim@Sun.COM 					break;
249012632SHyon.Kim@Sun.COM 				} else {
249112632SHyon.Kim@Sun.COM 					u_inserted = 0;
249212632SHyon.Kim@Sun.COM 					u_watch = u_tail;
249312632SHyon.Kim@Sun.COM 				}
249412632SHyon.Kim@Sun.COM 			}
249512632SHyon.Kim@Sun.COM 		} else {
249612632SHyon.Kim@Sun.COM 			/*
249712632SHyon.Kim@Sun.COM 			 * We just inserted one rpnode, increment the
249812632SHyon.Kim@Sun.COM 			 * unsolved_inserted counter. We will utilize this
249912632SHyon.Kim@Sun.COM 			 * counter to detect an indefinite insertion loop.
250012632SHyon.Kim@Sun.COM 			 */
250112632SHyon.Kim@Sun.COM 			u_inserted++;
2502*12830SHyon.Kim@Sun.COM 			if (child == u_watch) {
2503*12830SHyon.Kim@Sun.COM 				/*
2504*12830SHyon.Kim@Sun.COM 				 * watch dog node itself is inserted.
2505*12830SHyon.Kim@Sun.COM 				 * Set it to the tail and refresh the watching.
2506*12830SHyon.Kim@Sun.COM 				 */
2507*12830SHyon.Kim@Sun.COM 				u_watch = u_tail;
2508*12830SHyon.Kim@Sun.COM 				u_inserted = 0;
2509*12830SHyon.Kim@Sun.COM 				u_left = 0;
2510*12830SHyon.Kim@Sun.COM 			}
251112632SHyon.Kim@Sun.COM 		}
251212632SHyon.Kim@Sun.COM 	}
251312632SHyon.Kim@Sun.COM 
251412632SHyon.Kim@Sun.COM 	/* check if there is left out unresolved nodes. */
251512632SHyon.Kim@Sun.COM 	if (u_left) {
251612632SHyon.Kim@Sun.COM 		topo_mod_dprintf(mod, "ses_construct_phys_tree(): "
251712632SHyon.Kim@Sun.COM 		    "Failed to construct physical view of the following "
251812632SHyon.Kim@Sun.COM 		    "ses elements of Chassis CSN %s.", cp->sec_csn);
251912632SHyon.Kim@Sun.COM 		while (u_head) {
252012632SHyon.Kim@Sun.COM 			u_tail = u_head->spt_sibling;
252112632SHyon.Kim@Sun.COM 			topo_mod_dprintf(mod,
252212632SHyon.Kim@Sun.COM 			    "\telement type (%d) and instance (%d)",
252312632SHyon.Kim@Sun.COM 			    u_head->spt_senumnode->sen_type,
252412632SHyon.Kim@Sun.COM 			    u_head->spt_senumnode->sen_instance);
252512632SHyon.Kim@Sun.COM 			topo_mod_free(mod, u_head, sizeof (ses_phys_tree_t));
252612632SHyon.Kim@Sun.COM 			u_head = u_tail;
252712632SHyon.Kim@Sun.COM 		}
252812632SHyon.Kim@Sun.COM 		return (-1);
252912632SHyon.Kim@Sun.COM 	}
253012632SHyon.Kim@Sun.COM 
253112632SHyon.Kim@Sun.COM 	return (0);
253212632SHyon.Kim@Sun.COM }
253312632SHyon.Kim@Sun.COM 
253412632SHyon.Kim@Sun.COM /*
253512632SHyon.Kim@Sun.COM  * Free the whole phys tree.
253612632SHyon.Kim@Sun.COM  */
ses_phys_tree_free(topo_mod_t * mod,ses_phys_tree_t * sproot)253712632SHyon.Kim@Sun.COM static void ses_phys_tree_free(topo_mod_t *mod, ses_phys_tree_t *sproot)
253812632SHyon.Kim@Sun.COM {
253912632SHyon.Kim@Sun.COM 	if (sproot == NULL)
254012632SHyon.Kim@Sun.COM 		return;
254112632SHyon.Kim@Sun.COM 
254212632SHyon.Kim@Sun.COM 	/* Free child tree. */
254312632SHyon.Kim@Sun.COM 	if (sproot->spt_child) {
254412632SHyon.Kim@Sun.COM 		ses_phys_tree_free(mod, sproot->spt_child);
254512632SHyon.Kim@Sun.COM 	}
254612632SHyon.Kim@Sun.COM 
254712632SHyon.Kim@Sun.COM 	/* Free sibling trees. */
254812632SHyon.Kim@Sun.COM 	if (sproot->spt_sibling) {
254912632SHyon.Kim@Sun.COM 		ses_phys_tree_free(mod, sproot->spt_sibling);
255012632SHyon.Kim@Sun.COM 	}
255112632SHyon.Kim@Sun.COM 
255212632SHyon.Kim@Sun.COM 	/* Free root node itself. */
255312632SHyon.Kim@Sun.COM 	topo_mod_free(mod, sproot, sizeof (ses_phys_tree_t));
255412632SHyon.Kim@Sun.COM }
255512632SHyon.Kim@Sun.COM 
255612632SHyon.Kim@Sun.COM /*
255712632SHyon.Kim@Sun.COM  * Parses phys_enum_type table to get the index of the given type.
255812632SHyon.Kim@Sun.COM  */
255912632SHyon.Kim@Sun.COM static boolean_t
is_type_enumerated(ses_phys_tree_t * node,int * index)256012632SHyon.Kim@Sun.COM is_type_enumerated(ses_phys_tree_t *node, int *index)
256112632SHyon.Kim@Sun.COM {
256212632SHyon.Kim@Sun.COM 	int i;
256312632SHyon.Kim@Sun.COM 
256412632SHyon.Kim@Sun.COM 	for (i = 0; i < N_PHYS_ENUM_TYPES; i++) {
256512632SHyon.Kim@Sun.COM 		if (node->spt_senumnode->sen_type ==
256612632SHyon.Kim@Sun.COM 		    phys_enum_type_list[i].pet_type) {
256712632SHyon.Kim@Sun.COM 			*index = i;
256812632SHyon.Kim@Sun.COM 			return (B_TRUE);
256912632SHyon.Kim@Sun.COM 		}
257012632SHyon.Kim@Sun.COM 	}
257112632SHyon.Kim@Sun.COM 	return (B_FALSE);
257212632SHyon.Kim@Sun.COM }
257312632SHyon.Kim@Sun.COM 
257412632SHyon.Kim@Sun.COM /*
257512632SHyon.Kim@Sun.COM  * Recusrive routine for top-down enumeration of the tree.
257612632SHyon.Kim@Sun.COM  */
257712632SHyon.Kim@Sun.COM static int
ses_enumerate_node(ses_enum_data_t * sdp,tnode_t * pnode,ses_enum_chassis_t * cp,ses_phys_tree_t * parent,int mrange[])257812632SHyon.Kim@Sun.COM ses_enumerate_node(ses_enum_data_t *sdp, tnode_t *pnode, ses_enum_chassis_t *cp,
257912632SHyon.Kim@Sun.COM     ses_phys_tree_t *parent, int mrange[])
258012632SHyon.Kim@Sun.COM {
258112632SHyon.Kim@Sun.COM 	topo_mod_t *mod = sdp->sed_mod;
258212632SHyon.Kim@Sun.COM 	ses_phys_tree_t *child = NULL;
258312632SHyon.Kim@Sun.COM 	int i, ret = 0, ret_ch;
258412632SHyon.Kim@Sun.COM 	uint64_t prevtype = SES_ET_UNSPECIFIED;
258512632SHyon.Kim@Sun.COM 	ses_phys_tree_t *dirfru = NULL;
258612632SHyon.Kim@Sun.COM 	tnode_t *tn = NULL, *frutn = NULL;
258712632SHyon.Kim@Sun.COM 
258812632SHyon.Kim@Sun.COM 	if (parent == NULL) {
258912632SHyon.Kim@Sun.COM 		return (0);
259012632SHyon.Kim@Sun.COM 	}
259112632SHyon.Kim@Sun.COM 
259212632SHyon.Kim@Sun.COM 	for (child = parent->spt_child; child != NULL;
259312632SHyon.Kim@Sun.COM 	    child = child->spt_sibling) {
259412632SHyon.Kim@Sun.COM 		if (is_type_enumerated(child, &i)) {
259512632SHyon.Kim@Sun.COM 			if (prevtype != phys_enum_type_list[i].pet_type) {
259612632SHyon.Kim@Sun.COM 				/* check if range needs to be created. */
259712632SHyon.Kim@Sun.COM 				if (phys_enum_type_list[i].pet_dorange &&
259812632SHyon.Kim@Sun.COM 				    topo_node_range_create(mod, pnode,
259912632SHyon.Kim@Sun.COM 				    phys_enum_type_list[i].pet_nodename, 0,
260012632SHyon.Kim@Sun.COM 				    mrange[i]) != 0) {
260112632SHyon.Kim@Sun.COM 					topo_mod_dprintf(mod,
260212632SHyon.Kim@Sun.COM 					    "topo_node_create_range() failed: "
260312632SHyon.Kim@Sun.COM 					    "%s", topo_mod_errmsg(mod));
260412632SHyon.Kim@Sun.COM 					return (-1);
260512632SHyon.Kim@Sun.COM 				}
260612632SHyon.Kim@Sun.COM 				prevtype = phys_enum_type_list[i].pet_type;
260712632SHyon.Kim@Sun.COM 			}
260812632SHyon.Kim@Sun.COM 
260912632SHyon.Kim@Sun.COM 			if (!(child->spt_isfru)) {
261012632SHyon.Kim@Sun.COM 				for (dirfru = parent; dirfru != NULL;
261112632SHyon.Kim@Sun.COM 				    dirfru = dirfru->spt_parent) {
261212632SHyon.Kim@Sun.COM 					if (dirfru->spt_isfru) {
261312632SHyon.Kim@Sun.COM 						break;
261412632SHyon.Kim@Sun.COM 					}
261512632SHyon.Kim@Sun.COM 				}
261612632SHyon.Kim@Sun.COM 				/* found direct FRU node. */
261712632SHyon.Kim@Sun.COM 				if (dirfru) {
261812632SHyon.Kim@Sun.COM 					frutn = dirfru->spt_tnode;
261912632SHyon.Kim@Sun.COM 				} else {
262012632SHyon.Kim@Sun.COM 					frutn = NULL;
262112632SHyon.Kim@Sun.COM 				}
262212632SHyon.Kim@Sun.COM 			} else {
262312632SHyon.Kim@Sun.COM 				frutn = NULL;
262412632SHyon.Kim@Sun.COM 			}
262512632SHyon.Kim@Sun.COM 
262612632SHyon.Kim@Sun.COM 			if (ses_create_generic(sdp, child->spt_senumnode,
262712632SHyon.Kim@Sun.COM 			    pnode, frutn, phys_enum_type_list[i].pet_nodename,
262812632SHyon.Kim@Sun.COM 			    phys_enum_type_list[i].pet_defaultlabel, &tn) != 0)
262912632SHyon.Kim@Sun.COM 				return (-1);
263012632SHyon.Kim@Sun.COM 
263112632SHyon.Kim@Sun.COM 			child->spt_tnode = tn;
263212632SHyon.Kim@Sun.COM 			/*
263312632SHyon.Kim@Sun.COM 			 * For some SES element there may be protocol specific
263412632SHyon.Kim@Sun.COM 			 * information to process.   Here we are processing
263512632SHyon.Kim@Sun.COM 			 * the association between enclosure controller and
263612632SHyon.Kim@Sun.COM 			 * SAS expanders.
263712632SHyon.Kim@Sun.COM 			 */
263812632SHyon.Kim@Sun.COM 			if (phys_enum_type_list[i].pet_type ==
263912632SHyon.Kim@Sun.COM 			    SES_ET_ESC_ELECTRONICS) {
264012632SHyon.Kim@Sun.COM 				/* create SAS expander node */
264112632SHyon.Kim@Sun.COM 				if (ses_create_protocol_specific(sdp,
264212632SHyon.Kim@Sun.COM 				    child->spt_senumnode, tn,
264312632SHyon.Kim@Sun.COM 				    phys_enum_type_list[i].pet_type,
264412632SHyon.Kim@Sun.COM 				    cp, phys_enum_type_list[i].pet_dorange) !=
264512632SHyon.Kim@Sun.COM 				    0) {
264612632SHyon.Kim@Sun.COM 					return (-1);
264712632SHyon.Kim@Sun.COM 				}
264812632SHyon.Kim@Sun.COM 			}
264912632SHyon.Kim@Sun.COM 		} else {
265012632SHyon.Kim@Sun.COM 			continue;
265112632SHyon.Kim@Sun.COM 		}
265212632SHyon.Kim@Sun.COM 		ret_ch = ses_enumerate_node(sdp, tn, cp, child, mrange);
265312632SHyon.Kim@Sun.COM 		if (ret_ch)
265412632SHyon.Kim@Sun.COM 			ret = ret_ch; /* there was an error and set the ret. */
265512632SHyon.Kim@Sun.COM 	}
265612632SHyon.Kim@Sun.COM 
265712632SHyon.Kim@Sun.COM 	return (ret);
265812632SHyon.Kim@Sun.COM }
265912632SHyon.Kim@Sun.COM 
266012632SHyon.Kim@Sun.COM /*
266112632SHyon.Kim@Sun.COM  * Instantiate types of nodes that are specified in the hierarchy
266212632SHyon.Kim@Sun.COM  * element type list.
266312632SHyon.Kim@Sun.COM  */
266412632SHyon.Kim@Sun.COM static int
ses_create_children_from_phys_tree(ses_enum_data_t * sdp,tnode_t * pnode,ses_enum_chassis_t * cp,ses_phys_tree_t * phys_tree)266512632SHyon.Kim@Sun.COM ses_create_children_from_phys_tree(ses_enum_data_t *sdp, tnode_t *pnode,
266612632SHyon.Kim@Sun.COM     ses_enum_chassis_t *cp, ses_phys_tree_t *phys_tree)
266712632SHyon.Kim@Sun.COM {
266812632SHyon.Kim@Sun.COM 	topo_mod_t *mod = sdp->sed_mod;
266912632SHyon.Kim@Sun.COM 	int mrange[N_PHYS_ENUM_TYPES] = { 0 };
267012632SHyon.Kim@Sun.COM 	ses_enum_node_t *snp;
267112632SHyon.Kim@Sun.COM 	int i, ret;
267212632SHyon.Kim@Sun.COM 
267312632SHyon.Kim@Sun.COM 	/*
267412632SHyon.Kim@Sun.COM 	 * First get max range for each type of element to be enumerated.
267512632SHyon.Kim@Sun.COM 	 */
267612632SHyon.Kim@Sun.COM 	for (i = 0; i < N_PHYS_ENUM_TYPES; i++) {
267712632SHyon.Kim@Sun.COM 		if (phys_enum_type_list[i].pet_dorange) {
267812632SHyon.Kim@Sun.COM 			for (snp = topo_list_next(&cp->sec_nodes); snp != NULL;
267912632SHyon.Kim@Sun.COM 			    snp = topo_list_next(snp)) {
268012632SHyon.Kim@Sun.COM 				if (snp->sen_type ==
268112632SHyon.Kim@Sun.COM 				    phys_enum_type_list[i].pet_type) {
268212632SHyon.Kim@Sun.COM 					if (snp->sen_instance > mrange[i])
268312632SHyon.Kim@Sun.COM 						mrange[i] =
268412632SHyon.Kim@Sun.COM 						    snp->sen_instance;
268512632SHyon.Kim@Sun.COM 				}
268612632SHyon.Kim@Sun.COM 			}
268712632SHyon.Kim@Sun.COM 		}
268812632SHyon.Kim@Sun.COM 	}
268912632SHyon.Kim@Sun.COM 
269012632SHyon.Kim@Sun.COM 	topo_mod_dprintf(mod, "%s: creating nodes from FRU hierarchy tree.",
269112632SHyon.Kim@Sun.COM 	    cp->sec_csn);
269212632SHyon.Kim@Sun.COM 
269312632SHyon.Kim@Sun.COM 	if ((ret = ses_enumerate_node(sdp, pnode, cp, phys_tree, mrange)) !=
269412632SHyon.Kim@Sun.COM 	    0) {
269512632SHyon.Kim@Sun.COM 		topo_mod_dprintf(mod,
269612632SHyon.Kim@Sun.COM 		    "ses_create_children_from_phys_tree() failed: ");
269712632SHyon.Kim@Sun.COM 		return (ret);
269812632SHyon.Kim@Sun.COM 	}
269912632SHyon.Kim@Sun.COM 
270012632SHyon.Kim@Sun.COM 	return (0);
270112632SHyon.Kim@Sun.COM }
270212632SHyon.Kim@Sun.COM 
270312632SHyon.Kim@Sun.COM /*
27046869Seschrock  * Instantiate a new chassis instance in the topology.
27056869Seschrock  */
27066869Seschrock static int
ses_create_chassis(ses_enum_data_t * sdp,tnode_t * pnode,ses_enum_chassis_t * cp)27076869Seschrock ses_create_chassis(ses_enum_data_t *sdp, tnode_t *pnode, ses_enum_chassis_t *cp)
27086869Seschrock {
27096869Seschrock 	topo_mod_t *mod = sdp->sed_mod;
27106869Seschrock 	nvlist_t *props;
27116869Seschrock 	char *raw_manufacturer, *raw_model, *raw_revision;
27126869Seschrock 	char *manufacturer = NULL, *model = NULL, *product = NULL;
27136869Seschrock 	char *revision = NULL;
27146869Seschrock 	char *serial;
27159296SEric.Schrock@Sun.COM 	char **paths;
27166869Seschrock 	size_t prodlen;
27176869Seschrock 	tnode_t *tn;
27186869Seschrock 	nvlist_t *fmri = NULL, *auth = NULL;
27196869Seschrock 	int ret = -1;
27206869Seschrock 	ses_enum_node_t *snp;
27219296SEric.Schrock@Sun.COM 	ses_enum_target_t *stp;
272210519SSundeep.Panicker@Sun.COM 	ses_enum_chassis_t *scp;
27239296SEric.Schrock@Sun.COM 	int i, err;
272412632SHyon.Kim@Sun.COM 	uint64_t sc_count = 0, pindex;
272512632SHyon.Kim@Sun.COM 	ses_phys_tree_t	*sproot = NULL;
272612632SHyon.Kim@Sun.COM 	hrtime_t start;
272712632SHyon.Kim@Sun.COM 	hrtime_t end;
272812632SHyon.Kim@Sun.COM 	double duration;
27296869Seschrock 
27306869Seschrock 	/*
27317269Seschrock 	 * Ignore any internal enclosures.
27327269Seschrock 	 */
27337269Seschrock 	if (cp->sec_internal)
27347269Seschrock 		return (0);
27357269Seschrock 
27367269Seschrock 	/*
27376869Seschrock 	 * Check to see if there are any devices presennt in the chassis.  If
27386869Seschrock 	 * not, ignore the chassis alltogether.  This is most useful for
27396869Seschrock 	 * ignoring internal HBAs that present a SES target but don't actually
27406869Seschrock 	 * manage any of the devices.
27416869Seschrock 	 */
27426869Seschrock 	for (snp = topo_list_next(&cp->sec_nodes); snp != NULL;
27436869Seschrock 	    snp = topo_list_next(snp)) {
27446869Seschrock 		if (snp->sen_type == SES_ET_DEVICE ||
27456869Seschrock 		    snp->sen_type == SES_ET_ARRAY_DEVICE)
27466869Seschrock 			break;
27476869Seschrock 	}
27486869Seschrock 
27496869Seschrock 	if (snp == NULL)
27506869Seschrock 		return (0);
27516869Seschrock 
27526869Seschrock 	props = ses_node_props(cp->sec_enclosure);
27536869Seschrock 
27546869Seschrock 	/*
27556869Seschrock 	 * We use the following property mappings:
27566869Seschrock 	 *
27576869Seschrock 	 * 	manufacturer		vendor-id
27586869Seschrock 	 * 	model			product-id
27596869Seschrock 	 * 	serial-number		libses-chassis-serial
27606869Seschrock 	 */
27616869Seschrock 	verify(nvlist_lookup_string(props, SES_EN_PROP_VID,
27626869Seschrock 	    &raw_manufacturer) == 0);
27636869Seschrock 	verify(nvlist_lookup_string(props, SES_EN_PROP_PID, &raw_model) == 0);
27646869Seschrock 	verify(nvlist_lookup_string(props, SES_EN_PROP_REV,
27656869Seschrock 	    &raw_revision) == 0);
27666869Seschrock 	verify(nvlist_lookup_string(props, LIBSES_EN_PROP_CSN, &serial) == 0);
27676869Seschrock 
27686869Seschrock 	/*
27696869Seschrock 	 * To construct the authority information, we 'clean' each string by
27706869Seschrock 	 * removing any offensive characters and trimmming whitespace.  For the
27716869Seschrock 	 * 'product-id', we use a concatenation of 'manufacturer-model'.  We
27726869Seschrock 	 * also take the numerical serial number and convert it to a string.
27736869Seschrock 	 */
27746869Seschrock 	if ((manufacturer = disk_auth_clean(mod, raw_manufacturer)) == NULL ||
27756869Seschrock 	    (model = disk_auth_clean(mod, raw_model)) == NULL ||
27766869Seschrock 	    (revision = disk_auth_clean(mod, raw_revision)) == NULL) {
27776869Seschrock 		goto error;
27786869Seschrock 	}
27796869Seschrock 
27806869Seschrock 	prodlen = strlen(manufacturer) + strlen(model) + 2;
27816869Seschrock 	if ((product = topo_mod_alloc(mod, prodlen)) == NULL)
27826869Seschrock 		goto error;
27836869Seschrock 
27846869Seschrock 	(void) snprintf(product, prodlen, "%s-%s", manufacturer, model);
27856869Seschrock 
27866869Seschrock 	/*
27876869Seschrock 	 * Construct the topo node and bind it to our parent.
27886869Seschrock 	 */
27896869Seschrock 	if (topo_mod_nvalloc(mod, &auth, NV_UNIQUE_NAME) != 0)
27906869Seschrock 		goto error;
27916869Seschrock 
27926869Seschrock 	if (nvlist_add_string(auth, FM_FMRI_AUTH_PRODUCT, product) != 0 ||
27936869Seschrock 	    nvlist_add_string(auth, FM_FMRI_AUTH_CHASSIS, serial) != 0) {
27946869Seschrock 		(void) topo_mod_seterrno(mod, EMOD_NVL_INVAL);
27956869Seschrock 		goto error;
27966869Seschrock 	}
27976869Seschrock 
27986869Seschrock 	/*
27996869Seschrock 	 * We pass NULL for the parent FMRI because there is no resource
28006869Seschrock 	 * associated with it.  For the toplevel enclosure, we leave the
28016869Seschrock 	 * serial/part/revision portions empty, which are reserved for
28026869Seschrock 	 * individual components within the chassis.
28036869Seschrock 	 */
28046869Seschrock 	if ((fmri = topo_mod_hcfmri(mod, NULL, FM_HC_SCHEME_VERSION,
28056869Seschrock 	    SES_ENCLOSURE, cp->sec_instance, NULL, auth,
28066869Seschrock 	    model, revision, serial)) == NULL) {
28076869Seschrock 		topo_mod_dprintf(mod, "topo_mod_hcfmri() failed: %s",
28086869Seschrock 		    topo_mod_errmsg(mod));
28096869Seschrock 		goto error;
28106869Seschrock 	}
28116869Seschrock 
28126869Seschrock 	if ((tn = topo_node_bind(mod, pnode, SES_ENCLOSURE,
28136869Seschrock 	    cp->sec_instance, fmri)) == NULL) {
28146869Seschrock 		topo_mod_dprintf(mod, "topo_node_bind() failed: %s",
28156869Seschrock 		    topo_mod_errmsg(mod));
28166869Seschrock 		goto error;
28176869Seschrock 	}
28186869Seschrock 
28196869Seschrock 	if (topo_method_register(mod, tn, ses_enclosure_methods) != 0) {
28206869Seschrock 		topo_mod_dprintf(mod,
28216869Seschrock 		    "topo_method_register() failed: %s",
28226869Seschrock 		    topo_mod_errmsg(mod));
28236869Seschrock 		goto error;
28246869Seschrock 	}
28256869Seschrock 
282612126SHyon.Kim@Sun.COM 	if (ses_set_standard_props(mod, NULL, tn, auth,
28276869Seschrock 	    ses_node_id(cp->sec_enclosure), cp->sec_target->set_devpath) != 0)
28286869Seschrock 		goto error;
28296869Seschrock 
28306869Seschrock 	/*
28319296SEric.Schrock@Sun.COM 	 * For enclosures, we want to include all possible targets (for upgrade
28329296SEric.Schrock@Sun.COM 	 * purposes).
28339296SEric.Schrock@Sun.COM 	 */
28349296SEric.Schrock@Sun.COM 	for (i = 0, stp = topo_list_next(&cp->sec_targets); stp != NULL;
28359296SEric.Schrock@Sun.COM 	    stp = topo_list_next(stp), i++)
28369296SEric.Schrock@Sun.COM 		;
28379296SEric.Schrock@Sun.COM 
28389296SEric.Schrock@Sun.COM 	verify(i != 0);
28399296SEric.Schrock@Sun.COM 	paths = alloca(i * sizeof (char *));
28409296SEric.Schrock@Sun.COM 
28419296SEric.Schrock@Sun.COM 	for (i = 0, stp = topo_list_next(&cp->sec_targets); stp != NULL;
28429296SEric.Schrock@Sun.COM 	    stp = topo_list_next(stp), i++)
28439296SEric.Schrock@Sun.COM 		paths[i] = stp->set_devpath;
28449296SEric.Schrock@Sun.COM 
28459296SEric.Schrock@Sun.COM 
28469296SEric.Schrock@Sun.COM 	if (topo_prop_set_string_array(tn, TOPO_PGROUP_SES,
28479296SEric.Schrock@Sun.COM 	    TOPO_PROP_PATHS, TOPO_PROP_IMMUTABLE, (const char **)paths,
28489296SEric.Schrock@Sun.COM 	    i, &err) != 0) {
28499296SEric.Schrock@Sun.COM 		topo_mod_dprintf(mod,
28509296SEric.Schrock@Sun.COM 		    "failed to create property %s: %s\n",
28519296SEric.Schrock@Sun.COM 		    TOPO_PROP_PATHS, topo_strerror(err));
28529296SEric.Schrock@Sun.COM 		goto error;
28539296SEric.Schrock@Sun.COM 	}
28549296SEric.Schrock@Sun.COM 
285512632SHyon.Kim@Sun.COM 	if (nvlist_lookup_uint64(props,
285612632SHyon.Kim@Sun.COM 	    LIBSES_PROP_PHYS_PARENT, &pindex) == 0) {
285712632SHyon.Kim@Sun.COM 		start = gethrtime(); /* to mearusre performance */
285812632SHyon.Kim@Sun.COM 		/*
285912632SHyon.Kim@Sun.COM 		 * The enclosure is supported through SUNW,FRUID.
286012632SHyon.Kim@Sun.COM 		 * Need to enumerate the nodes through hierarchical order.
286112632SHyon.Kim@Sun.COM 		 */
286212632SHyon.Kim@Sun.COM 		if ((sproot = topo_mod_zalloc(mod,
286312632SHyon.Kim@Sun.COM 		    sizeof (ses_phys_tree_t))) == NULL) {
286412632SHyon.Kim@Sun.COM 			topo_mod_dprintf(mod,
286512632SHyon.Kim@Sun.COM 			    "failed to allocate root: %s\n",
286612632SHyon.Kim@Sun.COM 			    topo_strerror(err));
286712632SHyon.Kim@Sun.COM 			goto error;
286812632SHyon.Kim@Sun.COM 		}
286912632SHyon.Kim@Sun.COM 		sproot->spt_pindex = pindex;
287012632SHyon.Kim@Sun.COM 		if (nvlist_lookup_boolean_value(props,
287112632SHyon.Kim@Sun.COM 		    LIBSES_PROP_FRU, &sproot->spt_isfru) != 0) {
287212632SHyon.Kim@Sun.COM 			topo_mod_dprintf(mod,
287312632SHyon.Kim@Sun.COM 			    "ses_create_chassis(): Failed to find prop %s "
287412632SHyon.Kim@Sun.COM 			    "on enclosure element (CSN %s).",
287512632SHyon.Kim@Sun.COM 			    LIBSES_PROP_FRU, cp->sec_csn);
287612632SHyon.Kim@Sun.COM 			/* an enclosure should be a FRU. continue to process. */
287712632SHyon.Kim@Sun.COM 			sproot->spt_isfru = B_TRUE;
287812632SHyon.Kim@Sun.COM 		}
287912632SHyon.Kim@Sun.COM 		if (nvlist_lookup_uint64(props,
288012632SHyon.Kim@Sun.COM 		    SES_PROP_ELEMENT_ONLY_INDEX,
288112632SHyon.Kim@Sun.COM 		    &sproot->spt_eonlyindex) != 0) {
288212632SHyon.Kim@Sun.COM 			topo_mod_dprintf(mod,
288312632SHyon.Kim@Sun.COM 			    "ses_create_chassis(): Failed to find prop %s "
288412632SHyon.Kim@Sun.COM 			    "on enclosure element (CSN %s).",
288512632SHyon.Kim@Sun.COM 			    LIBSES_PROP_PHYS_PARENT, cp->sec_csn);
288612632SHyon.Kim@Sun.COM 			topo_mod_free(mod, sproot, sizeof (ses_phys_tree_t));
288712632SHyon.Kim@Sun.COM 			goto error;
288812632SHyon.Kim@Sun.COM 		}
288912632SHyon.Kim@Sun.COM 		if (sproot->spt_pindex != sproot->spt_eonlyindex) {
289012632SHyon.Kim@Sun.COM 			topo_mod_dprintf(mod, "ses_create_chassis(): "
289112632SHyon.Kim@Sun.COM 			    "Enclosure element(CSN %s) should have "
289212632SHyon.Kim@Sun.COM 			    "itself as the parent to be the root node "
289312632SHyon.Kim@Sun.COM 			    "of FRU hierarchical tree.)", cp->sec_csn);
289412632SHyon.Kim@Sun.COM 			topo_mod_free(mod, sproot, sizeof (ses_phys_tree_t));
289512632SHyon.Kim@Sun.COM 			goto error;
289612632SHyon.Kim@Sun.COM 		} else {
289712632SHyon.Kim@Sun.COM 			sproot->spt_snode = cp->sec_enclosure;
289812632SHyon.Kim@Sun.COM 			sproot->spt_tnode = tn;
289912632SHyon.Kim@Sun.COM 			/* construct a tree. */
290012632SHyon.Kim@Sun.COM 			if (ses_construct_phys_tree(sdp, cp, sproot) != 0) {
290112632SHyon.Kim@Sun.COM 				topo_mod_dprintf(mod, "ses_create_chassis(): "
290212632SHyon.Kim@Sun.COM 				    "Failed to construct FRU hierarchical "
290312632SHyon.Kim@Sun.COM 				    "tree on enclosure (CSN %s.)",
290412632SHyon.Kim@Sun.COM 				    cp->sec_csn);
290512632SHyon.Kim@Sun.COM 			}
290612632SHyon.Kim@Sun.COM 
290712632SHyon.Kim@Sun.COM 			/* enumerate elements from the tree. */
290812632SHyon.Kim@Sun.COM 			if (ses_create_children_from_phys_tree(sdp, tn, cp,
290912632SHyon.Kim@Sun.COM 			    sproot) != 0) {
291012632SHyon.Kim@Sun.COM 				topo_mod_dprintf(mod, "ses_create_chassis(): "
291112632SHyon.Kim@Sun.COM 				    "Failed to create children topo nodes out "
291212632SHyon.Kim@Sun.COM 				    "of FRU hierarchical tree on enclosure "
291312632SHyon.Kim@Sun.COM 				    "(CSN %s).", cp->sec_csn);
291412632SHyon.Kim@Sun.COM 			}
291512632SHyon.Kim@Sun.COM 			/* destroy the phys tree. */
291612632SHyon.Kim@Sun.COM 			ses_phys_tree_free(mod, sproot);
291712632SHyon.Kim@Sun.COM 		}
291812632SHyon.Kim@Sun.COM 
291912632SHyon.Kim@Sun.COM 		end = gethrtime();
292012632SHyon.Kim@Sun.COM 		duration = end - start;
292112632SHyon.Kim@Sun.COM 		duration /= HR_SECOND;
292212632SHyon.Kim@Sun.COM 		topo_mod_dprintf(mod,
292312632SHyon.Kim@Sun.COM 		    "FRU boundary tree based enumeration: %.6f seconds",
292412632SHyon.Kim@Sun.COM 		    duration);
292512632SHyon.Kim@Sun.COM 	} else {
292612632SHyon.Kim@Sun.COM 		/*
292712632SHyon.Kim@Sun.COM 		 * Create the nodes for power supplies, fans, controllers and
292812632SHyon.Kim@Sun.COM 		 * devices.  Note that SAS exopander nodes and connector nodes
292912632SHyon.Kim@Sun.COM 		 * are handled through protocol specific processing of
293012632SHyon.Kim@Sun.COM 		 * controllers.
293112632SHyon.Kim@Sun.COM 		 */
293212632SHyon.Kim@Sun.COM 		if (ses_create_children(sdp, tn, SES_ET_POWER_SUPPLY,
293312632SHyon.Kim@Sun.COM 		    PSU, "PSU", cp, B_TRUE) != 0 ||
293412632SHyon.Kim@Sun.COM 		    ses_create_children(sdp, tn, SES_ET_COOLING,
293512632SHyon.Kim@Sun.COM 		    FAN, "FAN", cp, B_TRUE) != 0 ||
293612632SHyon.Kim@Sun.COM 		    ses_create_children(sdp, tn, SES_ET_ESC_ELECTRONICS,
293712632SHyon.Kim@Sun.COM 		    CONTROLLER, "CONTROLLER", cp, B_TRUE) != 0 ||
293812632SHyon.Kim@Sun.COM 		    ses_create_children(sdp, tn, SES_ET_DEVICE,
293912632SHyon.Kim@Sun.COM 		    BAY, "BAY", cp, B_TRUE) != 0 ||
294012632SHyon.Kim@Sun.COM 		    ses_create_children(sdp, tn, SES_ET_ARRAY_DEVICE,
294112632SHyon.Kim@Sun.COM 		    BAY, "BAY", cp, B_TRUE) != 0)
294212632SHyon.Kim@Sun.COM 			goto error;
294312632SHyon.Kim@Sun.COM 	}
29446869Seschrock 
294512126SHyon.Kim@Sun.COM 	if (cp->sec_maxinstance >= 0 &&
294612126SHyon.Kim@Sun.COM 	    (topo_node_range_create(mod, tn, SUBCHASSIS, 0,
294712126SHyon.Kim@Sun.COM 	    cp->sec_maxinstance) != 0)) {
294810519SSundeep.Panicker@Sun.COM 		topo_mod_dprintf(mod, "topo_node_create_range() failed: %s",
294910519SSundeep.Panicker@Sun.COM 		    topo_mod_errmsg(mod));
295010519SSundeep.Panicker@Sun.COM 		goto error;
295110519SSundeep.Panicker@Sun.COM 	}
295210519SSundeep.Panicker@Sun.COM 
295310519SSundeep.Panicker@Sun.COM 	for (scp = topo_list_next(&cp->sec_subchassis); scp != NULL;
295410519SSundeep.Panicker@Sun.COM 	    scp = topo_list_next(scp)) {
295510519SSundeep.Panicker@Sun.COM 
295610519SSundeep.Panicker@Sun.COM 		if (ses_create_subchassis(sdp, tn, scp) != 0)
295710519SSundeep.Panicker@Sun.COM 			goto error;
295810519SSundeep.Panicker@Sun.COM 
295910519SSundeep.Panicker@Sun.COM 		topo_mod_dprintf(mod, "created Subchassis node with "
296012126SHyon.Kim@Sun.COM 		    "instance %u\nand target (%s) under Chassis with CSN %s",
296112126SHyon.Kim@Sun.COM 		    scp->sec_instance, scp->sec_target->set_devpath,
296212126SHyon.Kim@Sun.COM 		    cp->sec_csn);
296310519SSundeep.Panicker@Sun.COM 
296410519SSundeep.Panicker@Sun.COM 		sc_count++;
296510519SSundeep.Panicker@Sun.COM 	}
296610519SSundeep.Panicker@Sun.COM 
296710519SSundeep.Panicker@Sun.COM 	topo_mod_dprintf(mod, "%s: created %llu %s nodes",
296810519SSundeep.Panicker@Sun.COM 	    cp->sec_csn, sc_count, SUBCHASSIS);
296910519SSundeep.Panicker@Sun.COM 
29709296SEric.Schrock@Sun.COM 	cp->sec_target->set_refcount++;
29719296SEric.Schrock@Sun.COM 	topo_node_setspecific(tn, cp->sec_target);
29727269Seschrock 
29736869Seschrock 	ret = 0;
29746869Seschrock error:
29756869Seschrock 	topo_mod_strfree(mod, manufacturer);
29766869Seschrock 	topo_mod_strfree(mod, model);
29776869Seschrock 	topo_mod_strfree(mod, revision);
29786869Seschrock 	topo_mod_strfree(mod, product);
29796869Seschrock 
29806869Seschrock 	nvlist_free(fmri);
29816869Seschrock 	nvlist_free(auth);
29826869Seschrock 	return (ret);
29836869Seschrock }
29846869Seschrock 
29856869Seschrock /*
29867269Seschrock  * Create a bay node explicitly enumerated via XML.
29877269Seschrock  */
29887269Seschrock static int
ses_create_bays(ses_enum_data_t * sdp,tnode_t * pnode)29897269Seschrock ses_create_bays(ses_enum_data_t *sdp, tnode_t *pnode)
29907269Seschrock {
29917269Seschrock 	topo_mod_t *mod = sdp->sed_mod;
29927269Seschrock 	ses_enum_chassis_t *cp;
29937269Seschrock 
29947269Seschrock 	/*
29957269Seschrock 	 * Iterate over chassis looking for an internal enclosure.  This
29967269Seschrock 	 * property is set via a vendor-specific plugin, and there should only
29977269Seschrock 	 * ever be a single internal chassis in a system.
29987269Seschrock 	 */
29997269Seschrock 	for (cp = topo_list_next(&sdp->sed_chassis); cp != NULL;
30007269Seschrock 	    cp = topo_list_next(cp)) {
30017269Seschrock 		if (cp->sec_internal)
30027269Seschrock 			break;
30037269Seschrock 	}
30047269Seschrock 
30057269Seschrock 	if (cp == NULL) {
30067269Seschrock 		topo_mod_dprintf(mod, "failed to find internal chassis\n");
30077269Seschrock 		return (-1);
30087269Seschrock 	}
30097269Seschrock 
30107269Seschrock 	if (ses_create_children(sdp, pnode, SES_ET_DEVICE,
30117269Seschrock 	    BAY, "BAY", cp, B_FALSE) != 0 ||
30127269Seschrock 	    ses_create_children(sdp, pnode, SES_ET_ARRAY_DEVICE,
30137269Seschrock 	    BAY, "BAY", cp, B_FALSE) != 0)
30147269Seschrock 		return (-1);
30157269Seschrock 
30167269Seschrock 	return (0);
30177269Seschrock }
301810519SSundeep.Panicker@Sun.COM 
301910519SSundeep.Panicker@Sun.COM /*
302010519SSundeep.Panicker@Sun.COM  * Initialize chassis or subchassis.
302110519SSundeep.Panicker@Sun.COM  */
302210519SSundeep.Panicker@Sun.COM static int
ses_init_chassis(topo_mod_t * mod,ses_enum_data_t * sdp,ses_enum_chassis_t * pcp,ses_enum_chassis_t * cp,ses_node_t * np,nvlist_t * props,uint64_t subchassis,ses_chassis_type_e flags)302310519SSundeep.Panicker@Sun.COM ses_init_chassis(topo_mod_t *mod, ses_enum_data_t *sdp, ses_enum_chassis_t *pcp,
302410519SSundeep.Panicker@Sun.COM     ses_enum_chassis_t *cp, ses_node_t *np, nvlist_t *props,
302512126SHyon.Kim@Sun.COM     uint64_t subchassis, ses_chassis_type_e flags)
302610519SSundeep.Panicker@Sun.COM {
302710519SSundeep.Panicker@Sun.COM 	boolean_t internal, ident;
302810519SSundeep.Panicker@Sun.COM 
302910519SSundeep.Panicker@Sun.COM 	assert((flags & (SES_NEW_CHASSIS | SES_NEW_SUBCHASSIS |
303010519SSundeep.Panicker@Sun.COM 	    SES_DUP_CHASSIS | SES_DUP_SUBCHASSIS)) != 0);
303110519SSundeep.Panicker@Sun.COM 
303212126SHyon.Kim@Sun.COM 	assert(cp != NULL);
303312126SHyon.Kim@Sun.COM 	assert(np != NULL);
303412126SHyon.Kim@Sun.COM 	assert(props != NULL);
303510519SSundeep.Panicker@Sun.COM 
303610519SSundeep.Panicker@Sun.COM 	if (flags & (SES_NEW_SUBCHASSIS | SES_DUP_SUBCHASSIS))
303710519SSundeep.Panicker@Sun.COM 		assert(pcp != NULL);
303810519SSundeep.Panicker@Sun.COM 
303912126SHyon.Kim@Sun.COM 	topo_mod_dprintf(mod, "ses_init_chassis: %s: index %llu, flags (%d)",
304012126SHyon.Kim@Sun.COM 	    sdp->sed_name, subchassis, flags);
304110519SSundeep.Panicker@Sun.COM 
304210519SSundeep.Panicker@Sun.COM 	if (flags & (SES_NEW_CHASSIS | SES_NEW_SUBCHASSIS)) {
304310519SSundeep.Panicker@Sun.COM 
304410519SSundeep.Panicker@Sun.COM 		topo_mod_dprintf(mod, "new chassis/subchassis");
304510519SSundeep.Panicker@Sun.COM 		if (nvlist_lookup_boolean_value(props,
304610519SSundeep.Panicker@Sun.COM 		    LIBSES_EN_PROP_INTERNAL, &internal) == 0)
304710519SSundeep.Panicker@Sun.COM 			cp->sec_internal = internal;
304810519SSundeep.Panicker@Sun.COM 
304910519SSundeep.Panicker@Sun.COM 		cp->sec_enclosure = np;
305010519SSundeep.Panicker@Sun.COM 		cp->sec_target = sdp->sed_target;
305110519SSundeep.Panicker@Sun.COM 
305210519SSundeep.Panicker@Sun.COM 		if (flags & SES_NEW_CHASSIS) {
305312126SHyon.Kim@Sun.COM 			if (!cp->sec_internal)
305412126SHyon.Kim@Sun.COM 				cp->sec_instance = sdp->sed_instance++;
305510519SSundeep.Panicker@Sun.COM 			topo_list_append(&sdp->sed_chassis, cp);
305610519SSundeep.Panicker@Sun.COM 		} else {
305712126SHyon.Kim@Sun.COM 			if (subchassis != NO_SUBCHASSIS)
305812126SHyon.Kim@Sun.COM 				cp->sec_instance = subchassis;
305912126SHyon.Kim@Sun.COM 			else
306012126SHyon.Kim@Sun.COM 				cp->sec_instance = pcp->sec_scinstance++;
306112126SHyon.Kim@Sun.COM 
306212126SHyon.Kim@Sun.COM 			if (cp->sec_instance > pcp->sec_maxinstance)
306312126SHyon.Kim@Sun.COM 				pcp->sec_maxinstance = cp->sec_instance;
306412126SHyon.Kim@Sun.COM 
306510519SSundeep.Panicker@Sun.COM 			topo_list_append(&pcp->sec_subchassis, cp);
306610519SSundeep.Panicker@Sun.COM 		}
306710519SSundeep.Panicker@Sun.COM 
306810519SSundeep.Panicker@Sun.COM 	} else {
306910519SSundeep.Panicker@Sun.COM 		topo_mod_dprintf(mod, "dup chassis/subchassis");
307010519SSundeep.Panicker@Sun.COM 		if (nvlist_lookup_boolean_value(props,
307110519SSundeep.Panicker@Sun.COM 		    SES_PROP_IDENT, &ident) == 0) {
307210519SSundeep.Panicker@Sun.COM 			topo_mod_dprintf(mod,  "overriding enclosure node");
307310519SSundeep.Panicker@Sun.COM 
307410519SSundeep.Panicker@Sun.COM 			cp->sec_enclosure = np;
307510519SSundeep.Panicker@Sun.COM 			cp->sec_target = sdp->sed_target;
307610519SSundeep.Panicker@Sun.COM 		}
307710519SSundeep.Panicker@Sun.COM 	}
307810519SSundeep.Panicker@Sun.COM 
307910519SSundeep.Panicker@Sun.COM 	topo_list_append(&cp->sec_targets, sdp->sed_target);
308010519SSundeep.Panicker@Sun.COM 	sdp->sed_current = cp;
308110519SSundeep.Panicker@Sun.COM 
308210519SSundeep.Panicker@Sun.COM 	return (0);
308310519SSundeep.Panicker@Sun.COM }
308410519SSundeep.Panicker@Sun.COM 
30857269Seschrock /*
30866869Seschrock  * Gather nodes from the current SES target into our chassis list, merging the
30876869Seschrock  * results if necessary.
30886869Seschrock  */
30896869Seschrock static ses_walk_action_t
ses_enum_gather(ses_node_t * np,void * data)30906869Seschrock ses_enum_gather(ses_node_t *np, void *data)
30916869Seschrock {
30926869Seschrock 	nvlist_t *props = ses_node_props(np);
30936869Seschrock 	ses_enum_data_t *sdp = data;
30946869Seschrock 	topo_mod_t *mod = sdp->sed_mod;
309510519SSundeep.Panicker@Sun.COM 	ses_enum_chassis_t *cp, *scp;
30966869Seschrock 	ses_enum_node_t *snp;
30978344SEric.Schrock@Sun.COM 	ses_alt_node_t *sap;
30986869Seschrock 	char *csn;
30996869Seschrock 	uint64_t instance, type;
31007269Seschrock 	uint64_t prevstatus, status;
310110519SSundeep.Panicker@Sun.COM 	boolean_t report;
310212126SHyon.Kim@Sun.COM 	uint64_t subchassis = NO_SUBCHASSIS;
31036869Seschrock 
31046869Seschrock 	if (ses_node_type(np) == SES_NODE_ENCLOSURE) {
31056869Seschrock 		/*
31066869Seschrock 		 * If we have already identified the chassis for this target,
31076869Seschrock 		 * then this is a secondary enclosure and we should ignore it,
31086869Seschrock 		 * along with the rest of the tree (since this is depth-first).
31096869Seschrock 		 */
31106869Seschrock 		if (sdp->sed_current != NULL)
31116869Seschrock 			return (SES_WALK_ACTION_TERMINATE);
31126869Seschrock 
31136869Seschrock 		/*
31146869Seschrock 		 * Go through the list of chassis we have seen so far and see
31156869Seschrock 		 * if this serial number matches one of the known values.
311610519SSundeep.Panicker@Sun.COM 		 * If so, check whether this enclosure is a subchassis.
31176869Seschrock 		 */
31186869Seschrock 		if (nvlist_lookup_string(props, LIBSES_EN_PROP_CSN,
31196869Seschrock 		    &csn) != 0)
31206869Seschrock 			return (SES_WALK_ACTION_TERMINATE);
31216869Seschrock 
312212126SHyon.Kim@Sun.COM 		(void) nvlist_lookup_uint64(props, LIBSES_EN_PROP_SUBCHASSIS_ID,
312312126SHyon.Kim@Sun.COM 		    &subchassis);
31246869Seschrock 
312510519SSundeep.Panicker@Sun.COM 		topo_mod_dprintf(mod, "ses_enum_gather: Enclosure Node (%s) "
312612126SHyon.Kim@Sun.COM 		    "CSN (%s), subchassis (%llu)", sdp->sed_name, csn,
312712126SHyon.Kim@Sun.COM 		    subchassis);
312810519SSundeep.Panicker@Sun.COM 
312910519SSundeep.Panicker@Sun.COM 		/*
313010519SSundeep.Panicker@Sun.COM 		 * We need to determine whether this enclosure node
313110519SSundeep.Panicker@Sun.COM 		 * represents a chassis or a subchassis. Since we may
313210519SSundeep.Panicker@Sun.COM 		 * receive the enclosure nodes in a non-deterministic
313310519SSundeep.Panicker@Sun.COM 		 * manner, we need to account for all possible combinations:
313410519SSundeep.Panicker@Sun.COM 		 *	1. Chassis for the current CSN has not yet been
313510519SSundeep.Panicker@Sun.COM 		 *	   allocated
313610519SSundeep.Panicker@Sun.COM 		 *		1.1 This is a new chassis:
313710519SSundeep.Panicker@Sun.COM 		 *			allocate and instantiate the chassis
313810519SSundeep.Panicker@Sun.COM 		 *		1.2 This is a new subchassis:
313910519SSundeep.Panicker@Sun.COM 		 *			allocate a placeholder chassis
314010519SSundeep.Panicker@Sun.COM 		 *			allocate and instantiate the subchassis
314110519SSundeep.Panicker@Sun.COM 		 *			link the subchassis to the chassis
314210519SSundeep.Panicker@Sun.COM 		 *	2. Chassis for the current CSN has been allocated
314310519SSundeep.Panicker@Sun.COM 		 *		2.1 This is a duplicate chassis enclosure
314410519SSundeep.Panicker@Sun.COM 		 *			check whether to override old chassis
314510519SSundeep.Panicker@Sun.COM 		 *			append to chassis' target list
314610519SSundeep.Panicker@Sun.COM 		 *		2.2 Only placeholder chassis exists
314710519SSundeep.Panicker@Sun.COM 		 *			fill in the chassis fields
314810519SSundeep.Panicker@Sun.COM 		 *		2.3 This is a new subchassis
314910519SSundeep.Panicker@Sun.COM 		 *			allocate and instantiate the subchassis
315010519SSundeep.Panicker@Sun.COM 		 *			link the subchassis to the chassis
315110519SSundeep.Panicker@Sun.COM 		 *		2.4 This is a duplicate subchassis enclosure
315210519SSundeep.Panicker@Sun.COM 		 *			 check whether to override old chassis
315310519SSundeep.Panicker@Sun.COM 		 *			 append to chassis' target list
315410519SSundeep.Panicker@Sun.COM 		 */
315510519SSundeep.Panicker@Sun.COM 
315610519SSundeep.Panicker@Sun.COM 		for (cp = topo_list_next(&sdp->sed_chassis); cp != NULL;
315710519SSundeep.Panicker@Sun.COM 		    cp = topo_list_next(cp))
315810519SSundeep.Panicker@Sun.COM 			if (strcmp(cp->sec_csn, csn) == 0)
315910519SSundeep.Panicker@Sun.COM 				break;
316010519SSundeep.Panicker@Sun.COM 
31616869Seschrock 		if (cp == NULL) {
316210519SSundeep.Panicker@Sun.COM 			/* 1. Haven't seen a chassis with this CSN before */
31636869Seschrock 
31646869Seschrock 			if ((cp = topo_mod_zalloc(mod,
31656869Seschrock 			    sizeof (ses_enum_chassis_t))) == NULL)
31666869Seschrock 				goto error;
31676869Seschrock 
316812126SHyon.Kim@Sun.COM 			cp->sec_scinstance = SES_STARTING_SUBCHASSIS;
316912126SHyon.Kim@Sun.COM 			cp->sec_maxinstance = -1;
317010519SSundeep.Panicker@Sun.COM 			cp->sec_csn = csn;
317110519SSundeep.Panicker@Sun.COM 
317212126SHyon.Kim@Sun.COM 			if (subchassis == NO_SUBCHASSIS) {
317310519SSundeep.Panicker@Sun.COM 				/* 1.1 This is a new chassis */
317410519SSundeep.Panicker@Sun.COM 
317510519SSundeep.Panicker@Sun.COM 				topo_mod_dprintf(mod, "%s: Initialize new "
317612126SHyon.Kim@Sun.COM 				    "chassis with CSN %s", sdp->sed_name, csn);
317710519SSundeep.Panicker@Sun.COM 
317810519SSundeep.Panicker@Sun.COM 				if (ses_init_chassis(mod, sdp, NULL, cp,
317912126SHyon.Kim@Sun.COM 				    np, props, NO_SUBCHASSIS,
318012126SHyon.Kim@Sun.COM 				    SES_NEW_CHASSIS) < 0)
318110519SSundeep.Panicker@Sun.COM 					goto error;
318210519SSundeep.Panicker@Sun.COM 			} else {
318310519SSundeep.Panicker@Sun.COM 				/* 1.2 This is a new subchassis */
31847269Seschrock 
318510519SSundeep.Panicker@Sun.COM 				topo_mod_dprintf(mod, "%s: Initialize new "
318612126SHyon.Kim@Sun.COM 				    "subchassis with CSN %s and index %llu",
318712126SHyon.Kim@Sun.COM 				    sdp->sed_name, csn, subchassis);
318810519SSundeep.Panicker@Sun.COM 
318910519SSundeep.Panicker@Sun.COM 				if ((scp = topo_mod_zalloc(mod,
319010519SSundeep.Panicker@Sun.COM 				    sizeof (ses_enum_chassis_t))) == NULL)
319110519SSundeep.Panicker@Sun.COM 					goto error;
319210519SSundeep.Panicker@Sun.COM 
319310519SSundeep.Panicker@Sun.COM 				scp->sec_csn = csn;
319410519SSundeep.Panicker@Sun.COM 
319512126SHyon.Kim@Sun.COM 				if (ses_init_chassis(mod, sdp, cp, scp, np,
319612126SHyon.Kim@Sun.COM 				    props, subchassis, SES_NEW_SUBCHASSIS) < 0)
319710519SSundeep.Panicker@Sun.COM 					goto error;
319810519SSundeep.Panicker@Sun.COM 			}
31999296SEric.Schrock@Sun.COM 		} else {
320012126SHyon.Kim@Sun.COM 			/*
320112126SHyon.Kim@Sun.COM 			 * We have a chassis or subchassis with this CSN.  If
320212126SHyon.Kim@Sun.COM 			 * it's a chassis, we must check to see whether it is
320312126SHyon.Kim@Sun.COM 			 * a placeholder previously created because we found a
320412126SHyon.Kim@Sun.COM 			 * subchassis with this CSN.  We will know that because
320512126SHyon.Kim@Sun.COM 			 * the sec_target value will not be set; it is set only
320612126SHyon.Kim@Sun.COM 			 * in ses_init_chassis().  In that case, initialise it
320712126SHyon.Kim@Sun.COM 			 * as a new chassis; otherwise, it's a duplicate and we
320812126SHyon.Kim@Sun.COM 			 * need to append only.
320912126SHyon.Kim@Sun.COM 			 */
321012126SHyon.Kim@Sun.COM 			if (subchassis == NO_SUBCHASSIS) {
321112126SHyon.Kim@Sun.COM 				if (cp->sec_target != NULL) {
321210519SSundeep.Panicker@Sun.COM 					/* 2.1 This is a duplicate chassis */
321310519SSundeep.Panicker@Sun.COM 
321410519SSundeep.Panicker@Sun.COM 					topo_mod_dprintf(mod, "%s: Append "
321512126SHyon.Kim@Sun.COM 					    "duplicate chassis with CSN (%s)",
321612126SHyon.Kim@Sun.COM 					    sdp->sed_name, csn);
321710519SSundeep.Panicker@Sun.COM 
321810519SSundeep.Panicker@Sun.COM 					if (ses_init_chassis(mod, sdp, NULL, cp,
321912126SHyon.Kim@Sun.COM 					    np, props, NO_SUBCHASSIS,
322010519SSundeep.Panicker@Sun.COM 					    SES_DUP_CHASSIS) < 0)
322110519SSundeep.Panicker@Sun.COM 						goto error;
322210519SSundeep.Panicker@Sun.COM 				} else {
322312126SHyon.Kim@Sun.COM 					/* Placeholder chassis - init it up */
322410519SSundeep.Panicker@Sun.COM 					topo_mod_dprintf(mod, "%s: Initialize"
322512126SHyon.Kim@Sun.COM 					    "placeholder chassis with CSN %s",
322612126SHyon.Kim@Sun.COM 					    sdp->sed_name, csn);
322710519SSundeep.Panicker@Sun.COM 
322812126SHyon.Kim@Sun.COM 					if (ses_init_chassis(mod, sdp, NULL,
322912126SHyon.Kim@Sun.COM 					    cp, np, props, NO_SUBCHASSIS,
323010519SSundeep.Panicker@Sun.COM 					    SES_NEW_CHASSIS) < 0)
323110519SSundeep.Panicker@Sun.COM 						goto error;
323210519SSundeep.Panicker@Sun.COM 
323310519SSundeep.Panicker@Sun.COM 				}
323410519SSundeep.Panicker@Sun.COM 			} else {
323510519SSundeep.Panicker@Sun.COM 				/* This is a subchassis */
323610519SSundeep.Panicker@Sun.COM 
323710519SSundeep.Panicker@Sun.COM 				for (scp = topo_list_next(&cp->sec_subchassis);
323810519SSundeep.Panicker@Sun.COM 				    scp != NULL; scp = topo_list_next(scp))
323912126SHyon.Kim@Sun.COM 					if (scp->sec_instance == subchassis)
324010519SSundeep.Panicker@Sun.COM 						break;
324110519SSundeep.Panicker@Sun.COM 
324210519SSundeep.Panicker@Sun.COM 				if (scp == NULL) {
324310519SSundeep.Panicker@Sun.COM 					/* 2.3 This is a new subchassis */
324410519SSundeep.Panicker@Sun.COM 
324510519SSundeep.Panicker@Sun.COM 					topo_mod_dprintf(mod, "%s: Initialize "
324610519SSundeep.Panicker@Sun.COM 					    "new subchassis with CSN (%s) "
324710519SSundeep.Panicker@Sun.COM 					    "and LID (%s)",
324812126SHyon.Kim@Sun.COM 					    sdp->sed_name, csn);
324910519SSundeep.Panicker@Sun.COM 
325010519SSundeep.Panicker@Sun.COM 					if ((scp = topo_mod_zalloc(mod,
325110519SSundeep.Panicker@Sun.COM 					    sizeof (ses_enum_chassis_t)))
325210519SSundeep.Panicker@Sun.COM 					    == NULL)
325310519SSundeep.Panicker@Sun.COM 						goto error;
325410519SSundeep.Panicker@Sun.COM 
325510519SSundeep.Panicker@Sun.COM 					scp->sec_csn = csn;
325610519SSundeep.Panicker@Sun.COM 
325710519SSundeep.Panicker@Sun.COM 					if (ses_init_chassis(mod, sdp, cp, scp,
325812126SHyon.Kim@Sun.COM 					    np, props, subchassis,
325910519SSundeep.Panicker@Sun.COM 					    SES_NEW_SUBCHASSIS) < 0)
326010519SSundeep.Panicker@Sun.COM 						goto error;
326110519SSundeep.Panicker@Sun.COM 				} else {
326210519SSundeep.Panicker@Sun.COM 					/* 2.4 This is a duplicate subchassis */
326310519SSundeep.Panicker@Sun.COM 
326410519SSundeep.Panicker@Sun.COM 					topo_mod_dprintf(mod, "%s: Append "
326510519SSundeep.Panicker@Sun.COM 					    "duplicate subchassis with "
326612126SHyon.Kim@Sun.COM 					    "CSN (%s)", sdp->sed_name, csn);
326710519SSundeep.Panicker@Sun.COM 
326810519SSundeep.Panicker@Sun.COM 					if (ses_init_chassis(mod, sdp, cp, scp,
326912126SHyon.Kim@Sun.COM 					    np, props, subchassis,
327010519SSundeep.Panicker@Sun.COM 					    SES_DUP_SUBCHASSIS) < 0)
327110519SSundeep.Panicker@Sun.COM 						goto error;
327210519SSundeep.Panicker@Sun.COM 				}
32739296SEric.Schrock@Sun.COM 			}
32746869Seschrock 		}
32756869Seschrock 	} else if (ses_node_type(np) == SES_NODE_ELEMENT) {
32766869Seschrock 		/*
32776869Seschrock 		 * If we haven't yet seen an enclosure node and identified the
32786869Seschrock 		 * current chassis, something is very wrong; bail out.
32796869Seschrock 		 */
32806869Seschrock 		if (sdp->sed_current == NULL)
32816869Seschrock 			return (SES_WALK_ACTION_TERMINATE);
32826869Seschrock 
32836869Seschrock 		/*
32846869Seschrock 		 * If this isn't one of the element types we care about, then
32856869Seschrock 		 * ignore it.
32866869Seschrock 		 */
32876869Seschrock 		verify(nvlist_lookup_uint64(props, SES_PROP_ELEMENT_TYPE,
32886869Seschrock 		    &type) == 0);
32896869Seschrock 		if (type != SES_ET_DEVICE &&
32906869Seschrock 		    type != SES_ET_ARRAY_DEVICE &&
329112632SHyon.Kim@Sun.COM 		    type != SES_ET_SUNW_FANBOARD &&
329212632SHyon.Kim@Sun.COM 		    type != SES_ET_SUNW_FANMODULE &&
32936869Seschrock 		    type != SES_ET_COOLING &&
329412632SHyon.Kim@Sun.COM 		    type != SES_ET_SUNW_POWERBOARD &&
329512632SHyon.Kim@Sun.COM 		    type != SES_ET_SUNW_POWERMODULE &&
32966869Seschrock 		    type != SES_ET_POWER_SUPPLY &&
329712126SHyon.Kim@Sun.COM 		    type != SES_ET_ESC_ELECTRONICS &&
329812126SHyon.Kim@Sun.COM 		    type != SES_ET_SAS_EXPANDER &&
329912126SHyon.Kim@Sun.COM 		    type != SES_ET_SAS_CONNECTOR)
33006869Seschrock 			return (SES_WALK_ACTION_CONTINUE);
33016869Seschrock 
33026869Seschrock 		/*
33036869Seschrock 		 * Get the current instance number and see if we already know
33046869Seschrock 		 * about this element.  If so, it means we have multiple paths
33056869Seschrock 		 * to the same elements, and we should ignore the current path.
33066869Seschrock 		 */
33076869Seschrock 		verify(nvlist_lookup_uint64(props, SES_PROP_ELEMENT_CLASS_INDEX,
33086869Seschrock 		    &instance) == 0);
33096869Seschrock 		if (type == SES_ET_DEVICE || type == SES_ET_ARRAY_DEVICE)
33106869Seschrock 			(void) nvlist_lookup_uint64(props, SES_PROP_BAY_NUMBER,
33116869Seschrock 			    &instance);
33126869Seschrock 
33136869Seschrock 		cp = sdp->sed_current;
33146869Seschrock 
33156869Seschrock 		for (snp = topo_list_next(&cp->sec_nodes); snp != NULL;
33166869Seschrock 		    snp = topo_list_next(snp)) {
33176869Seschrock 			if (snp->sen_type == type &&
33186869Seschrock 			    snp->sen_instance == instance)
33197269Seschrock 				break;
33207269Seschrock 		}
33217269Seschrock 
33227269Seschrock 		/*
33237269Seschrock 		 * We prefer the new element under the following circumstances:
33247269Seschrock 		 *
33257269Seschrock 		 * - The currently known element's status is unknown or not
33267269Seschrock 		 *   available, but the new element has a known status.  This
33277269Seschrock 		 *   occurs if a given element is only available through a
33287269Seschrock 		 *   particular target.
33297269Seschrock 		 *
33307269Seschrock 		 * - This is an ESC_ELECTRONICS element, and the 'reported-via'
33317269Seschrock 		 *   property is set.  This allows us to get reliable firmware
33327269Seschrock 		 *   revision information from the enclosure node.
33337269Seschrock 		 */
33347269Seschrock 		if (snp != NULL) {
33357269Seschrock 			if (nvlist_lookup_uint64(
33367269Seschrock 			    ses_node_props(snp->sen_node),
33377269Seschrock 			    SES_PROP_STATUS_CODE, &prevstatus) != 0)
33387269Seschrock 				prevstatus = SES_ESC_UNSUPPORTED;
33397269Seschrock 			if (nvlist_lookup_uint64(
33407269Seschrock 			    props, SES_PROP_STATUS_CODE, &status) != 0)
33417269Seschrock 				status = SES_ESC_UNSUPPORTED;
33427269Seschrock 			if (nvlist_lookup_boolean_value(
33437269Seschrock 			    props, SES_PROP_REPORT, &report) != 0)
33447269Seschrock 				report = B_FALSE;
33457269Seschrock 
33467269Seschrock 			if ((SES_STATUS_UNAVAIL(prevstatus) &&
33477269Seschrock 			    !SES_STATUS_UNAVAIL(status)) ||
33487269Seschrock 			    (type == SES_ET_ESC_ELECTRONICS &&
33497269Seschrock 			    report)) {
33507269Seschrock 				snp->sen_node = np;
33517269Seschrock 				snp->sen_target = sdp->sed_target;
33527269Seschrock 			}
33537269Seschrock 
33548344SEric.Schrock@Sun.COM 			if ((sap = topo_mod_zalloc(mod,
33558344SEric.Schrock@Sun.COM 			    sizeof (ses_alt_node_t))) == NULL)
33568344SEric.Schrock@Sun.COM 				goto error;
33578344SEric.Schrock@Sun.COM 
33588344SEric.Schrock@Sun.COM 			sap->san_node = np;
33598344SEric.Schrock@Sun.COM 			topo_list_append(&snp->sen_alt_nodes, sap);
33608344SEric.Schrock@Sun.COM 
33617269Seschrock 			return (SES_WALK_ACTION_CONTINUE);
33626869Seschrock 		}
33636869Seschrock 
33646869Seschrock 		if ((snp = topo_mod_zalloc(mod,
33656869Seschrock 		    sizeof (ses_enum_node_t))) == NULL)
33666869Seschrock 			goto error;
33676869Seschrock 
33688344SEric.Schrock@Sun.COM 		if ((sap = topo_mod_zalloc(mod,
33698344SEric.Schrock@Sun.COM 		    sizeof (ses_alt_node_t))) == NULL) {
33708344SEric.Schrock@Sun.COM 			topo_mod_free(mod, snp, sizeof (ses_enum_node_t));
33718344SEric.Schrock@Sun.COM 			goto error;
33728344SEric.Schrock@Sun.COM 		}
33738344SEric.Schrock@Sun.COM 
33746869Seschrock 		topo_mod_dprintf(mod, "%s: adding node (%llu, %llu)",
33756869Seschrock 		    sdp->sed_name, type, instance);
33766869Seschrock 		snp->sen_node = np;
33776869Seschrock 		snp->sen_type = type;
33786869Seschrock 		snp->sen_instance = instance;
33796869Seschrock 		snp->sen_target = sdp->sed_target;
33808344SEric.Schrock@Sun.COM 		sap->san_node = np;
33818344SEric.Schrock@Sun.COM 		topo_list_append(&snp->sen_alt_nodes, sap);
33826869Seschrock 		topo_list_append(&cp->sec_nodes, snp);
33836869Seschrock 
33846869Seschrock 		if (type == SES_ET_DEVICE)
33856869Seschrock 			cp->sec_hasdev = B_TRUE;
33866869Seschrock 	}
33876869Seschrock 
33886869Seschrock 	return (SES_WALK_ACTION_CONTINUE);
33896869Seschrock 
33906869Seschrock error:
33916869Seschrock 	sdp->sed_errno = -1;
33926869Seschrock 	return (SES_WALK_ACTION_TERMINATE);
33936869Seschrock }
33946869Seschrock 
33956869Seschrock static int
ses_process_dir(const char * dirpath,ses_enum_data_t * sdp)33966869Seschrock ses_process_dir(const char *dirpath, ses_enum_data_t *sdp)
33976869Seschrock {
33986869Seschrock 	topo_mod_t *mod = sdp->sed_mod;
33996869Seschrock 	DIR *dir;
34006869Seschrock 	struct dirent *dp;
34016869Seschrock 	char path[PATH_MAX];
34026869Seschrock 	ses_enum_target_t *stp;
34036869Seschrock 	int err = -1;
34046869Seschrock 
34056869Seschrock 	/*
34066869Seschrock 	 * Open the SES target directory and iterate over any available
34076869Seschrock 	 * targets.
34086869Seschrock 	 */
34096869Seschrock 	if ((dir = opendir(dirpath)) == NULL) {
34106869Seschrock 		/*
34116869Seschrock 		 * If the SES target directory does not exist, then return as if
34126869Seschrock 		 * there are no active targets.
34136869Seschrock 		 */
34146869Seschrock 		topo_mod_dprintf(mod, "failed to open ses "
34156869Seschrock 		    "directory '%s'", dirpath);
34166869Seschrock 		return (0);
34176869Seschrock 	}
34186869Seschrock 
34196869Seschrock 	while ((dp = readdir(dir)) != NULL) {
34206869Seschrock 		if (strcmp(dp->d_name, ".") == 0 ||
34216869Seschrock 		    strcmp(dp->d_name, "..") == 0)
34226869Seschrock 			continue;
34236869Seschrock 
34246869Seschrock 		/*
34256869Seschrock 		 * Create a new target instance and take a snapshot.
34266869Seschrock 		 */
34276869Seschrock 		if ((stp = topo_mod_zalloc(mod,
34286869Seschrock 		    sizeof (ses_enum_target_t))) == NULL)
34296869Seschrock 			goto error;
34306869Seschrock 
34318344SEric.Schrock@Sun.COM 		(void) pthread_mutex_init(&stp->set_lock, NULL);
34328344SEric.Schrock@Sun.COM 
34336869Seschrock 		(void) snprintf(path, sizeof (path), "%s/%s", dirpath,
34346869Seschrock 		    dp->d_name);
34356869Seschrock 
34366869Seschrock 		/*
34376869Seschrock 		 * We keep track of the SES device path and export it on a
34386869Seschrock 		 * per-node basis to allow higher level software to get to the
34396869Seschrock 		 * corresponding SES state.
34406869Seschrock 		 */
34416869Seschrock 		if ((stp->set_devpath = topo_mod_strdup(mod, path)) == NULL) {
34426869Seschrock 			topo_mod_free(mod, stp, sizeof (ses_enum_target_t));
34436869Seschrock 			goto error;
34446869Seschrock 		}
34456869Seschrock 
34466869Seschrock 		if ((stp->set_target =
34476869Seschrock 		    ses_open(LIBSES_VERSION, path)) == NULL) {
34486869Seschrock 			topo_mod_dprintf(mod, "failed to open ses target "
34496869Seschrock 			    "'%s': %s", dp->d_name, ses_errmsg());
345012357SStephen.Hanson@Sun.COM 			ses_sof_alloc(mod, stp->set_devpath);
34516869Seschrock 			topo_mod_free(mod, stp, sizeof (ses_enum_target_t));
34526869Seschrock 			continue;
34536869Seschrock 		}
345412357SStephen.Hanson@Sun.COM 		topo_mod_dprintf(mod, "open contract");
345512357SStephen.Hanson@Sun.COM 		ses_ssl_alloc(mod, stp);
345612357SStephen.Hanson@Sun.COM 		ses_create_contract(mod, stp);
34576869Seschrock 
34586869Seschrock 		stp->set_refcount = 1;
34596869Seschrock 		sdp->sed_target = stp;
34606869Seschrock 		stp->set_snap = ses_snap_hold(stp->set_target);
346110527SEric.Schrock@Sun.COM 		stp->set_snaptime = gethrtime();
34626869Seschrock 
34636869Seschrock 		/*
34646869Seschrock 		 * Enumerate over all SES elements and merge them into the
34656869Seschrock 		 * correct ses_enum_chassis_t.
34666869Seschrock 		 */
34676869Seschrock 		sdp->sed_current = NULL;
34686869Seschrock 		sdp->sed_errno = 0;
34696869Seschrock 		sdp->sed_name = dp->d_name;
34706869Seschrock 		(void) ses_walk(stp->set_snap, ses_enum_gather, sdp);
34716869Seschrock 
34726869Seschrock 		if (sdp->sed_errno != 0)
34736869Seschrock 			goto error;
34746869Seschrock 	}
34756869Seschrock 
34766869Seschrock 	err = 0;
34776869Seschrock error:
34786869Seschrock 	closedir(dir);
34796869Seschrock 	return (err);
34806869Seschrock }
34816869Seschrock 
34826869Seschrock static void
ses_release(topo_mod_t * mod,tnode_t * tn)34836869Seschrock ses_release(topo_mod_t *mod, tnode_t *tn)
34846869Seschrock {
34856869Seschrock 	ses_enum_target_t *stp;
34866869Seschrock 
348712357SStephen.Hanson@Sun.COM 	if ((stp = topo_node_getspecific(tn)) != NULL) {
348812357SStephen.Hanson@Sun.COM 		topo_node_setspecific(tn, NULL);
34896869Seschrock 		ses_target_free(mod, stp);
349012357SStephen.Hanson@Sun.COM 	}
34916869Seschrock }
34926869Seschrock 
34936869Seschrock /*ARGSUSED*/
34946869Seschrock static int
ses_enum(topo_mod_t * mod,tnode_t * rnode,const char * name,topo_instance_t min,topo_instance_t max,void * arg,void * notused)34956869Seschrock ses_enum(topo_mod_t *mod, tnode_t *rnode, const char *name,
34966869Seschrock     topo_instance_t min, topo_instance_t max, void *arg, void *notused)
34976869Seschrock {
34986869Seschrock 	ses_enum_chassis_t *cp;
34997269Seschrock 	ses_enum_data_t *data;
35006869Seschrock 
35016869Seschrock 	/*
35026869Seschrock 	 * Check to make sure we're being invoked sensibly, and that we're not
35036869Seschrock 	 * being invoked as part of a post-processing step.
35046869Seschrock 	 */
35057269Seschrock 	if (strcmp(name, SES_ENCLOSURE) != 0 && strcmp(name, BAY) != 0)
35066869Seschrock 		return (0);
35076869Seschrock 
35086869Seschrock 	/*
35097269Seschrock 	 * If this is the first time we've called our enumeration method, then
35107269Seschrock 	 * gather information about any available enclosures.
35116869Seschrock 	 */
35127269Seschrock 	if ((data = topo_mod_getspecific(mod)) == NULL) {
351312357SStephen.Hanson@Sun.COM 		ses_sof_freeall(mod);
35147269Seschrock 		if ((data = topo_mod_zalloc(mod, sizeof (ses_enum_data_t))) ==
35157269Seschrock 		    NULL)
35167269Seschrock 			return (-1);
35176869Seschrock 
35187269Seschrock 		data->sed_mod = mod;
35197269Seschrock 		topo_mod_setspecific(mod, data);
35207269Seschrock 
352112126SHyon.Kim@Sun.COM 		if (dev_list_gather(mod, &data->sed_devs) != 0)
35227269Seschrock 			goto error;
35237269Seschrock 
35247269Seschrock 		/*
35257269Seschrock 		 * We search both the ses(7D) and sgen(7D) locations, so we are
35267269Seschrock 		 * independent of any particular driver class bindings.
35277269Seschrock 		 */
35287269Seschrock 		if (ses_process_dir("/dev/es", data) != 0 ||
35297269Seschrock 		    ses_process_dir("/dev/scsi/ses", data) != 0)
35306869Seschrock 			goto error;
35316869Seschrock 	}
35326869Seschrock 
35337269Seschrock 	if (strcmp(name, SES_ENCLOSURE) == 0) {
35347269Seschrock 		/*
35357269Seschrock 		 * This is a request to enumerate external enclosures.  Go
35367269Seschrock 		 * through all the targets and create chassis nodes where
35377269Seschrock 		 * necessary.
35387269Seschrock 		 */
35397269Seschrock 		for (cp = topo_list_next(&data->sed_chassis); cp != NULL;
35407269Seschrock 		    cp = topo_list_next(cp)) {
35417269Seschrock 			if (ses_create_chassis(data, rnode, cp) != 0)
35427269Seschrock 				goto error;
35437269Seschrock 		}
35447269Seschrock 	} else {
35457269Seschrock 		/*
35467269Seschrock 		 * This is a request to enumerate a specific bay underneath the
35477269Seschrock 		 * root chassis (for internal disks).
35487269Seschrock 		 */
35497269Seschrock 		if (ses_create_bays(data, rnode) != 0)
35507269Seschrock 			goto error;
35517269Seschrock 	}
35527269Seschrock 
35537269Seschrock 	/*
35547269Seschrock 	 * This is a bit of a kludge.  In order to allow internal disks to be
35557269Seschrock 	 * enumerated and share snapshot-specific information with the external
35567269Seschrock 	 * enclosure enumeration, we rely on the fact that we will be invoked
35577269Seschrock 	 * for the 'ses-enclosure' node last.
35587269Seschrock 	 */
35597269Seschrock 	if (strcmp(name, SES_ENCLOSURE) == 0) {
356010519SSundeep.Panicker@Sun.COM 		for (cp = topo_list_next(&data->sed_chassis); cp != NULL;
356110519SSundeep.Panicker@Sun.COM 		    cp = topo_list_next(cp))
356210519SSundeep.Panicker@Sun.COM 			ses_data_free(data, cp);
356310519SSundeep.Panicker@Sun.COM 		ses_data_free(data, NULL);
35647269Seschrock 		topo_mod_setspecific(mod, NULL);
35657269Seschrock 	}
35666869Seschrock 	return (0);
35676869Seschrock 
35686869Seschrock error:
356910519SSundeep.Panicker@Sun.COM 	for (cp = topo_list_next(&data->sed_chassis); cp != NULL;
357010519SSundeep.Panicker@Sun.COM 	    cp = topo_list_next(cp))
357110519SSundeep.Panicker@Sun.COM 		ses_data_free(data, cp);
357210519SSundeep.Panicker@Sun.COM 	ses_data_free(data, NULL);
35737269Seschrock 	topo_mod_setspecific(mod, NULL);
35746869Seschrock 	return (-1);
35756869Seschrock }
35766869Seschrock 
35776869Seschrock static const topo_modops_t ses_ops =
35786869Seschrock 	{ ses_enum, ses_release };
35796869Seschrock 
35806869Seschrock static topo_modinfo_t ses_info =
35816869Seschrock 	{ SES_ENCLOSURE, FM_FMRI_SCHEME_HC, SES_VERSION, &ses_ops };
35826869Seschrock 
35836869Seschrock /*ARGSUSED*/
35846869Seschrock int
_topo_init(topo_mod_t * mod,topo_version_t version)35856869Seschrock _topo_init(topo_mod_t *mod, topo_version_t version)
35866869Seschrock {
358712357SStephen.Hanson@Sun.COM 	int rval;
358812357SStephen.Hanson@Sun.COM 
35896869Seschrock 	if (getenv("TOPOSESDEBUG") != NULL)
35906869Seschrock 		topo_mod_setdebug(mod);
35916869Seschrock 
35926869Seschrock 	topo_mod_dprintf(mod, "initializing %s enumerator\n",
35936869Seschrock 	    SES_ENCLOSURE);
35946869Seschrock 
359512357SStephen.Hanson@Sun.COM 	if ((rval = topo_mod_register(mod, &ses_info, TOPO_VERSION)) == 0)
359612357SStephen.Hanson@Sun.COM 		ses_thread_init(mod);
359712357SStephen.Hanson@Sun.COM 
359812357SStephen.Hanson@Sun.COM 	return (rval);
35996869Seschrock }
36006869Seschrock 
36016869Seschrock void
_topo_fini(topo_mod_t * mod)36026869Seschrock _topo_fini(topo_mod_t *mod)
36036869Seschrock {
360412502SStephen.Hanson@Sun.COM 	ses_thread_fini(mod);
360512357SStephen.Hanson@Sun.COM 	ses_sof_freeall(mod);
36066869Seschrock 	topo_mod_unregister(mod);
36076869Seschrock }
3608