xref: /illumos-gate/usr/src/lib/fm/topo/modules/common/ses/ses.c (revision f37b3cbb6f67aaea5eec1c335bdc7bf432867d64)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright 2012 Milan Jurik. All rights reserved.
25  * Copyright 2015 Nexenta Systems, Inc.  All rights reserved.
26  * Copyright 2020 Joyent, Inc.
27  */
28 
29 #include <alloca.h>
30 #include <dirent.h>
31 #include <devid.h>
32 #include <fm/libdiskstatus.h>
33 #include <inttypes.h>
34 #include <pthread.h>
35 #include <strings.h>
36 #include <string.h>
37 #include <unistd.h>
38 #include <sys/dkio.h>
39 #include <sys/fm/protocol.h>
40 #include <sys/libdevid.h>
41 #include <sys/scsi/scsi_types.h>
42 #include <sys/byteorder.h>
43 #include <pthread.h>
44 #include <signal.h>
45 #include <fcntl.h>
46 #include <sys/ctfs.h>
47 #include <libcontract.h>
48 #include <poll.h>
49 #include <sys/contract/device.h>
50 #include <libsysevent.h>
51 #include <sys/sysevent/eventdefs.h>
52 #include <scsi/plugins/ses/vendor/sun.h>
53 
54 #include "disk.h"
55 #include "ses.h"
56 
57 #define	SES_VERSION	1
58 
59 #define	SES_STARTING_SUBCHASSIS 256	/* valid subchassis IDs are uint8_t */
60 #define	NO_SUBCHASSIS	((uint64_t)-1)
61 
62 static int ses_snap_freq = 250;		/* in milliseconds */
63 
64 #define	SES_STATUS_UNAVAIL(s)	\
65 	((s) == SES_ESC_UNSUPPORTED || (s) >= SES_ESC_NOT_INSTALLED)
66 
67 #define	HR_SECOND   1000000000
68 
69 #define	SES_INST_NOTSET		UINT64_MAX
70 
71 /*
72  * Because multiple SES targets can be part of a single chassis, we construct
73  * our own hierarchy that takes this into account.  These SES targets may refer
74  * to the same devices (multiple paths) or to different devices (managing
75  * different portions of the space).  We arrange things into a
76  * ses_enum_enclosure_t, which contains a set of ses targets, and a list of all
77  * nodes found so far.
78  */
79 typedef struct ses_alt_node {
80 	topo_list_t		san_link;
81 	ses_node_t		*san_node;
82 } ses_alt_node_t;
83 
84 typedef struct ses_enum_node {
85 	topo_list_t		sen_link;
86 	ses_node_t		*sen_node;
87 	topo_list_t		sen_alt_nodes;
88 	uint64_t		sen_type;
89 	uint64_t		sen_instance;
90 	ses_enum_target_t	*sen_target;
91 } ses_enum_node_t;
92 
93 typedef struct ses_enum_chassis {
94 	topo_list_t		sec_link;
95 	topo_list_t		sec_subchassis;
96 	topo_list_t		sec_nodes;
97 	topo_list_t		sec_targets;
98 	const char		*sec_csn;
99 	ses_node_t		*sec_enclosure;
100 	ses_enum_target_t	*sec_target;
101 	topo_instance_t		sec_instance;
102 	topo_instance_t		sec_scinstance;
103 	topo_instance_t		sec_maxinstance;
104 	boolean_t		sec_hasdev;
105 	boolean_t		sec_internal;
106 } ses_enum_chassis_t;
107 
108 typedef struct ses_enum_data {
109 	topo_list_t		sed_devs;
110 	topo_list_t		sed_chassis;
111 	ses_enum_chassis_t	*sed_current;
112 	ses_enum_target_t	*sed_target;
113 	int			sed_errno;
114 	char			*sed_name;
115 	topo_mod_t		*sed_mod;
116 	topo_instance_t		sed_instance;
117 } ses_enum_data_t;
118 
119 typedef struct sas_connector_phy_data {
120 	uint64_t    scpd_index;
121 	uint64_t    scpd_pm;
122 } sas_connector_phy_data_t;
123 
124 typedef struct sas_connector_type {
125 	uint64_t    sct_type;
126 	char	    *sct_name;
127 } sas_connector_type_t;
128 
129 static const sas_connector_type_t sas_connector_type_list[] = {
130 	{   0x0, "Information unknown"  },
131 	{   0x1, "External SAS 4x receptacle (see SAS-2 and SFF-8470)"	},
132 	{   0x2, "Exteranl Mini SAS 4x receptacle (see SAS-2 and SFF-8088)" },
133 	{   0x3, "QSFP+ receptacle (see SAS-2.1 and SFF-8436)" },
134 	{   0x4, "Mini SAS 4x active receptacle (see SAS-2.1 and SFF-8088)" },
135 	{   0x5, "Mini SAS HD 4x receptacle (see SAS-2.1 and SFF-8644)" },
136 	{   0x6, "Mini SAS HD 8x receptacle (see SAS-2.1 and SFF-8644)" },
137 	{   0x7, "Mini SAS HD 16x receptacle (see SAS-2.1 and SFF-8644)" },
138 	{   0xF, "Vendor-specific external connector"	},
139 	{   0x10, "Internal wide SAS 4i plug (see SAS-2 and SFF-8484)"	},
140 	{   0x11,
141 	"Internal wide Mini SAS 4i receptacle (see SAS-2 and SFF-8087)"	},
142 	{   0x12, "Mini SAS HD 4i receptacle (see SAS-2.1 and SFF-8643)" },
143 	{   0x20, "Internal SAS Drive receptacle (see SAS-2 and SFF-8482)" },
144 	{   0x21, "Internal SATA host plug (see SAS-2 and SATA-2)"	},
145 	{   0x22, "Internal SAS Drive plug (see SAS-2 and SFF-8482)"	},
146 	{   0x23, "Internal SATA device plug (see SAS-2 and SATA-2)"	},
147 	{   0x24, "Micro SAS receptacle (see SAS-2.14)"	},
148 	{   0x25, "Micro SATA device plug (see SAS-2.1 and SATA)"	},
149 	{   0x26, "Micro SAS plug (see SAS-2.1 and SFF-8486)"	},
150 	{   0x27, "Micro SAS/SATA plug (see SAS-2.1 and SFF-8486)"	},
151 	{   0x28,
152 	"12 Gb/s SAS Drive backplane receptacle (see SAS-34 and SFF-8680)" },
153 	{   0x29, "12Gb/s SAS Drive Plug (see SAS-3 and SFF-8680)"	},
154 	{   0x2A, "Multifunction 12 Gb/s 6x Unshielded receptacle connector "
155 		"receptacle (see SAS-3 and SFF-8639)"	},
156 	{   0x2B, "Multifunction 12 Gb/s 6x Unshielded receptable connector "
157 		"plug (see SAS-3 and SFF-8639)"	},
158 	{   0x2F, "Internal SAS virtual connector"  },
159 	{   0x3F, "Vendor-specific internal connector"	},
160 	{   0x70, "Other Vendor-specific connector"	},
161 	{   0x71, "Other Vendor-specific connector"	},
162 	{   0x72, "Other Vendor-specific connector"	},
163 	{   0x73, "Other Vendor-specific connector"	},
164 	{   0x74, "Other Vendor-specific connector"	},
165 	{   0x75, "Other Vendor-specific connector"	},
166 	{   0x76, "Other Vendor-specific connector"	},
167 	{   0x77, "Other Vendor-specific connector"	},
168 	{   0x78, "Other Vendor-specific connector"	},
169 	{   0x79, "Other Vendor-specific connector"	},
170 	{   0x7A, "Other Vendor-specific connector"	},
171 	{   0x7B, "Other Vendor-specific connector"	},
172 	{   0x7C, "Other Vendor-specific connector"	},
173 	{   0x7D, "Other Vendor-specific connector"	},
174 	{   0x7E, "Other Vendor-specific connector"	},
175 	{   0x7F, "Other Vendor-specific connector"	},
176 	{   0x80, "Not Defined"	}
177 };
178 
179 #define	SAS_CONNECTOR_TYPE_CODE_NOT_DEFINED  0x80
180 #define	SAS_CONNECTOR_TYPE_NOT_DEFINED \
181 	"Connector type not defined by SES-2 standard"
182 #define	SAS_CONNECTOR_TYPE_RESERVED \
183 	"Connector type reserved by SES-2 standard"
184 
185 typedef struct phys_enum_type {
186 	uint64_t    pet_type;
187 	char	    *pet_nodename;
188 	char	    *pet_defaultlabel;
189 	boolean_t   pet_dorange;
190 } phys_enum_type_t;
191 
192 static const phys_enum_type_t phys_enum_type_list[] = {
193 	{   SES_ET_ARRAY_DEVICE, BAY, "BAY", B_TRUE  },
194 	{   SES_ET_COOLING, FAN, "FAN", B_TRUE  },
195 	{   SES_ET_DEVICE, BAY, "BAY", B_TRUE  },
196 	{   SES_ET_ESC_ELECTRONICS, CONTROLLER, "CONTROLLER", B_TRUE  },
197 	{   SES_ET_POWER_SUPPLY, PSU, "PSU", B_TRUE  },
198 	{   SES_ET_SUNW_FANBOARD, FANBOARD, "FANBOARD", B_TRUE  },
199 	{   SES_ET_SUNW_FANMODULE, FANMODULE, "FANMODULE", B_TRUE  },
200 	{   SES_ET_SUNW_POWERBOARD, POWERBOARD, "POWERBOARD", B_TRUE  },
201 	{   SES_ET_SUNW_POWERMODULE, POWERMODULE, "POWERMODULE", B_TRUE  }
202 };
203 
204 #define	N_PHYS_ENUM_TYPES (sizeof (phys_enum_type_list) / \
205 	sizeof (phys_enum_type_list[0]))
206 
207 /*
208  * Structure for the hierarchical tree for element nodes.
209  */
210 typedef struct ses_phys_tree {
211     ses_node_t	*spt_snode;
212     ses_enum_node_t	*spt_senumnode;
213     boolean_t	spt_isfru;
214     uint64_t	spt_eonlyindex;
215     uint64_t	spt_cindex;
216     uint64_t	spt_pindex;
217     uint64_t	spt_maxinst;
218     struct ses_phys_tree    *spt_parent;
219     struct ses_phys_tree    *spt_child;
220     struct ses_phys_tree    *spt_sibling;
221     tnode_t	*spt_tnode;
222 } ses_phys_tree_t;
223 
224 typedef enum {
225 	SES_NEW_CHASSIS		= 0x1,
226 	SES_NEW_SUBCHASSIS	= 0x2,
227 	SES_DUP_CHASSIS		= 0x4,
228 	SES_DUP_SUBCHASSIS	= 0x8
229 } ses_chassis_type_e;
230 
231 static const topo_pgroup_info_t smp_pgroup = {
232 	TOPO_PGROUP_SMP,
233 	TOPO_STABILITY_PRIVATE,
234 	TOPO_STABILITY_PRIVATE,
235 	1
236 };
237 
238 static const topo_pgroup_info_t ses_pgroup = {
239 	TOPO_PGROUP_SES,
240 	TOPO_STABILITY_PRIVATE,
241 	TOPO_STABILITY_PRIVATE,
242 	1
243 };
244 
245 static int ses_present(topo_mod_t *, tnode_t *, topo_version_t, nvlist_t *,
246     nvlist_t **);
247 static int ses_contains(topo_mod_t *, tnode_t *, topo_version_t, nvlist_t *,
248     nvlist_t **);
249 
250 static const topo_method_t ses_component_methods[] = {
251 	{ TOPO_METH_PRESENT, TOPO_METH_PRESENT_DESC,
252 	    TOPO_METH_PRESENT_VERSION0, TOPO_STABILITY_INTERNAL, ses_present },
253 	{ TOPO_METH_FAC_ENUM, TOPO_METH_FAC_ENUM_DESC, 0,
254 	    TOPO_STABILITY_INTERNAL, ses_node_enum_facility },
255 	{ TOPO_METH_SENSOR_FAILURE, TOPO_METH_SENSOR_FAILURE_DESC,
256 	    TOPO_METH_SENSOR_FAILURE_VERSION, TOPO_STABILITY_INTERNAL,
257 	    topo_method_sensor_failure },
258 	{ NULL }
259 };
260 
261 #define	TOPO_METH_SMCI_4U36_LABEL		"smci_4u36_bay_label"
262 #define	TOPO_METH_SMCI_4U36_LABEL_DESC	\
263 	"compute bay labels on SMCI 4U36 storage platform variants"
264 #define	TOPO_METH_SMCI_4U36_LABEL_VERSION	0
265 static int smci_4u36_bay_label(topo_mod_t *, tnode_t *, topo_version_t,
266     nvlist_t *, nvlist_t **);
267 
268 static const topo_method_t ses_bay_methods[] = {
269 	{ TOPO_METH_FAC_ENUM, TOPO_METH_FAC_ENUM_DESC, 0,
270 	    TOPO_STABILITY_INTERNAL, ses_node_enum_facility },
271 	{ TOPO_METH_OCCUPIED, TOPO_METH_OCCUPIED_DESC,
272 	    TOPO_METH_OCCUPIED_VERSION, TOPO_STABILITY_INTERNAL,
273 	    topo_mod_hc_occupied },
274 	{ TOPO_METH_SMCI_4U36_LABEL, TOPO_METH_SMCI_4U36_LABEL_DESC,
275 	    TOPO_METH_SMCI_4U36_LABEL_VERSION, TOPO_STABILITY_INTERNAL,
276 	    smci_4u36_bay_label },
277 	{ NULL }
278 };
279 
280 static const topo_method_t ses_recep_methods[] = {
281 	{ TOPO_METH_OCCUPIED, TOPO_METH_OCCUPIED_DESC,
282 	    TOPO_METH_OCCUPIED_VERSION, TOPO_STABILITY_INTERNAL,
283 	    topo_mod_hc_occupied },
284 	{ NULL }
285 };
286 
287 static const topo_method_t ses_enclosure_methods[] = {
288 	{ TOPO_METH_CONTAINS, TOPO_METH_CONTAINS_DESC,
289 	    TOPO_METH_CONTAINS_VERSION, TOPO_STABILITY_INTERNAL, ses_contains },
290 	{ TOPO_METH_FAC_ENUM, TOPO_METH_FAC_ENUM_DESC, 0,
291 	    TOPO_STABILITY_INTERNAL, ses_enc_enum_facility },
292 	{ NULL }
293 };
294 
295 /*
296  * The bay_label_overrides table can be used to map a server product ID to a
297  * topo method that will be invoked to override the value of the label property
298  * for all bay nodes.  By default the property value is static, derived from
299  * the corresponding SES array device element's descriptor string.
300  */
301 typedef struct ses_label_overrides {
302 	const char *slbl_product;
303 	const char *slbl_mname;
304 } ses_label_overrides_t;
305 
306 /*
307  * This table covers three generations of SMCI's 4U 36-bay storage server
308  * (and the Joyent-branded versions).  There was also an Ivy Bridge variant
309  * which has been omitted due to an inability to find one to test on.
310  */
311 static const ses_label_overrides_t bay_label_overrides[] = {
312 	/* Sandy Bridge variant */
313 	{ "SSG-6047R-E1R36L", TOPO_METH_SMCI_4U36_LABEL },
314 	{ "Joyent-Storage-Platform-5001", TOPO_METH_SMCI_4U36_LABEL },
315 
316 	/* Broadwell variant */
317 	{ "SSG-6048R-E1CR36L", TOPO_METH_SMCI_4U36_LABEL },
318 	{ "Joyent-Storage-Platform-7001", TOPO_METH_SMCI_4U36_LABEL },
319 
320 	/* Skylake variant */
321 	{ "SSG-6049P-E1CR36L", TOPO_METH_SMCI_4U36_LABEL },
322 	{ "Joyent-S10G5", TOPO_METH_SMCI_4U36_LABEL }
323 };
324 
325 #define	N_BAY_LBL_OVERRIDES (sizeof (bay_label_overrides) / \
326 	sizeof (bay_label_overrides[0]))
327 
328 /*
329  * Functions for tracking ses devices which we were unable to open. We retry
330  * these at regular intervals using ses_recheck_dir() and if we find that we
331  * can now open any of them then we send a sysevent to indicate that a new topo
332  * snapshot should be taken.
333  */
334 typedef struct ses_open_fail_list {
335 	struct ses_open_fail_list	*sof_next;
336 	char				*sof_path;
337 } ses_open_fail_list_t;
338 
339 static ses_open_fail_list_t *ses_sofh;
340 static pthread_mutex_t ses_sofmt;
341 static void ses_ct_print(char *ptr);
342 
343 static void
344 ses_recheck_dir()
345 {
346 	ses_target_t *target;
347 	sysevent_id_t eid;
348 	char buf[80];
349 	ses_open_fail_list_t *sof;
350 
351 	/*
352 	 * check list of "unable to open" devices
353 	 */
354 	(void) pthread_mutex_lock(&ses_sofmt);
355 	for (sof = ses_sofh; sof != NULL; sof = sof->sof_next) {
356 		/*
357 		 * see if we can open it now
358 		 */
359 		if ((target = ses_open(LIBSES_VERSION,
360 		    sof->sof_path)) == NULL) {
361 			(void) snprintf(buf, sizeof (buf),
362 			    "recheck_dir - still can't open %s", sof->sof_path);
363 			ses_ct_print(buf);
364 			continue;
365 		}
366 
367 		/*
368 		 * ok - better force a new snapshot
369 		 */
370 		(void) snprintf(buf, sizeof (buf),
371 		    "recheck_dir - can now open %s", sof->sof_path);
372 		ses_ct_print(buf);
373 		(void) sysevent_post_event(EC_PLATFORM, ESC_PLATFORM_SP_RESET,
374 		    SUNW_VENDOR, "fmd", NULL, &eid);
375 		ses_close(target);
376 		break;
377 	}
378 	(void) pthread_mutex_unlock(&ses_sofmt);
379 }
380 
381 static void
382 ses_sof_alloc(topo_mod_t *mod, char *path)
383 {
384 	ses_open_fail_list_t *sof;
385 
386 	(void) pthread_mutex_lock(&ses_sofmt);
387 	sof = topo_mod_zalloc(mod, sizeof (*sof));
388 	topo_mod_dprintf(mod, "sof_alloc %s", path);
389 	sof->sof_path = path;
390 	sof->sof_next = ses_sofh;
391 	ses_sofh = sof;
392 	(void) pthread_mutex_unlock(&ses_sofmt);
393 }
394 
395 static void
396 ses_sof_freeall(topo_mod_t *mod)
397 {
398 	ses_open_fail_list_t *sof, *next_sof;
399 
400 	(void) pthread_mutex_lock(&ses_sofmt);
401 	for (sof = ses_sofh; sof != NULL; sof = next_sof) {
402 		next_sof = sof->sof_next;
403 		topo_mod_dprintf(mod, "sof_freeall %s", sof->sof_path);
404 		topo_mod_strfree(mod, sof->sof_path);
405 		topo_mod_free(mod, sof, sizeof (*sof));
406 	}
407 	ses_sofh = NULL;
408 	(void) pthread_mutex_unlock(&ses_sofmt);
409 }
410 
411 /*
412  * functions for verifying that the ses_enum_target_t held in a device
413  * contract's cookie field is still valid (it may have been freed by
414  * ses_release()).
415  */
416 typedef struct ses_stp_list {
417 	struct ses_stp_list	*ssl_next;
418 	ses_enum_target_t	*ssl_tgt;
419 } ses_stp_list_t;
420 
421 static ses_stp_list_t *ses_sslh;
422 static pthread_mutex_t ses_sslmt;
423 
424 static void
425 ses_ssl_alloc(topo_mod_t *mod, ses_enum_target_t *stp)
426 {
427 	ses_stp_list_t *ssl;
428 
429 	(void) pthread_mutex_lock(&ses_sslmt);
430 	ssl = topo_mod_zalloc(mod, sizeof (*ssl));
431 	topo_mod_dprintf(mod, "ssl_alloc %p", stp);
432 	ssl->ssl_tgt = stp;
433 	ssl->ssl_next = ses_sslh;
434 	ses_sslh = ssl;
435 	(void) pthread_mutex_unlock(&ses_sslmt);
436 }
437 
438 static void
439 ses_ssl_free(topo_mod_t *mod, ses_enum_target_t *stp)
440 {
441 	ses_stp_list_t *ssl, *prev_ssl;
442 
443 	(void) pthread_mutex_lock(&ses_sslmt);
444 	prev_ssl = NULL;
445 	for (ssl = ses_sslh; ssl != NULL; ssl = ssl->ssl_next) {
446 		if (ssl->ssl_tgt == stp) {
447 			topo_mod_dprintf(mod, "ssl_free %p", ssl->ssl_tgt);
448 			if (prev_ssl == NULL)
449 				ses_sslh = ssl->ssl_next;
450 			else
451 				prev_ssl->ssl_next = ssl->ssl_next;
452 			topo_mod_free(mod, ssl, sizeof (*ssl));
453 			break;
454 		}
455 		prev_ssl = ssl;
456 	}
457 	(void) pthread_mutex_unlock(&ses_sslmt);
458 }
459 
460 static int
461 ses_ssl_valid(ses_enum_target_t *stp)
462 {
463 	ses_stp_list_t *ssl;
464 
465 	for (ssl = ses_sslh; ssl != NULL; ssl = ssl->ssl_next)
466 		if (ssl->ssl_tgt == stp)
467 			return (1);
468 	return (0);
469 }
470 
471 /*
472  * Functions for creating and destroying a background thread
473  * (ses_contract_thread) used for detecting when ses devices have been
474  * retired/unretired.
475  */
476 static struct ses_thread_s {
477 	pthread_mutex_t mt;
478 	pthread_t tid;
479 	int thr_sig;
480 	int doexit;
481 	int count;
482 } sesthread = {
483 	PTHREAD_MUTEX_INITIALIZER,
484 	0,
485 	SIGTERM,
486 	0,
487 	0
488 };
489 
490 typedef struct ses_mod_list {
491 	struct ses_mod_list	*smod_next;
492 	topo_mod_t		*smod_mod;
493 } ses_mod_list_t;
494 
495 static ses_mod_list_t *ses_smod;
496 
497 static void
498 ses_ct_print(char *ptr)
499 {
500 	(void) pthread_mutex_lock(&sesthread.mt);
501 	if (ses_smod != NULL && ses_smod->smod_mod != NULL)
502 		topo_mod_dprintf(ses_smod->smod_mod, ptr);
503 	(void) pthread_mutex_unlock(&sesthread.mt);
504 }
505 
506 /*ARGSUSED*/
507 static void *
508 ses_contract_thread(void *arg)
509 {
510 	int efd, ctlfd, statfd;
511 	ct_evthdl_t ev;
512 	ctevid_t evid;
513 	uint_t event;
514 	char path[PATH_MAX];
515 	char buf[80];
516 	ses_enum_target_t *stp;
517 	ct_stathdl_t stathdl;
518 	ctid_t ctid;
519 	struct pollfd fds;
520 	int pollret;
521 	sigset_t sigset;
522 
523 	ses_ct_print("start contract event thread");
524 	efd = open64(CTFS_ROOT "/device/pbundle", O_RDONLY);
525 	fds.fd = efd;
526 	fds.events = POLLIN;
527 	fds.revents = 0;
528 	(void) sigaddset(&sigset, sesthread.thr_sig);
529 	(void) pthread_sigmask(SIG_UNBLOCK, &sigset, NULL);
530 	for (;;) {
531 		/* check if we've been asked to exit */
532 		(void) pthread_mutex_lock(&sesthread.mt);
533 		if (sesthread.doexit) {
534 			(void) pthread_mutex_unlock(&sesthread.mt);
535 			break;
536 		}
537 		(void) pthread_mutex_unlock(&sesthread.mt);
538 
539 		/* poll until an event arrives */
540 		if ((pollret = poll(&fds, 1, 10000)) <= 0) {
541 			if (pollret == 0)
542 				ses_recheck_dir();
543 			continue;
544 		}
545 
546 		/* read the event */
547 		(void) pthread_mutex_lock(&ses_sslmt);
548 		ses_ct_print("read contract event");
549 		if (ct_event_read(efd, &ev) != 0) {
550 			(void) pthread_mutex_unlock(&ses_sslmt);
551 			continue;
552 		}
553 
554 		/* see if it is an event we are expecting */
555 		ctid = ct_event_get_ctid(ev);
556 		(void) snprintf(buf, sizeof (buf),
557 		    "got contract event ctid=%d", ctid);
558 		ses_ct_print(buf);
559 		event = ct_event_get_type(ev);
560 		if (event != CT_DEV_EV_OFFLINE && event != CT_EV_NEGEND) {
561 			(void) snprintf(buf, sizeof (buf),
562 			    "bad contract event %x", event);
563 			ses_ct_print(buf);
564 			ct_event_free(ev);
565 			(void) pthread_mutex_unlock(&ses_sslmt);
566 			continue;
567 		}
568 
569 		/* find target pointer saved in cookie */
570 		evid = ct_event_get_evid(ev);
571 		(void) snprintf(path, PATH_MAX, CTFS_ROOT "/device/%ld/status",
572 		    ctid);
573 		statfd = open64(path, O_RDONLY);
574 		(void) ct_status_read(statfd, CTD_COMMON, &stathdl);
575 		stp = (ses_enum_target_t *)(uintptr_t)
576 		    ct_status_get_cookie(stathdl);
577 		ct_status_free(stathdl);
578 		(void) close(statfd);
579 
580 		/* check if target pointer is still valid */
581 		if (ses_ssl_valid(stp) == 0) {
582 			(void) snprintf(buf, sizeof (buf),
583 			    "contract already abandoned %x", event);
584 			ses_ct_print(buf);
585 			(void) snprintf(path, PATH_MAX,
586 			    CTFS_ROOT "/device/%ld/ctl", ctid);
587 			ctlfd = open64(path, O_WRONLY);
588 			if (event != CT_EV_NEGEND)
589 				(void) ct_ctl_ack(ctlfd, evid);
590 			else
591 				(void) ct_ctl_abandon(ctlfd);
592 			(void) close(ctlfd);
593 			ct_event_free(ev);
594 			(void) pthread_mutex_unlock(&ses_sslmt);
595 			continue;
596 		}
597 
598 		/* find control device for ack/abandon */
599 		(void) pthread_mutex_lock(&stp->set_lock);
600 		(void) snprintf(path, PATH_MAX, CTFS_ROOT "/device/%ld/ctl",
601 		    ctid);
602 		ctlfd = open64(path, O_WRONLY);
603 		if (event != CT_EV_NEGEND) {
604 			/* if this is an offline event, do the offline */
605 			ses_ct_print("got contract offline event");
606 			if (stp->set_target) {
607 				ses_ct_print("contract thread rele");
608 				ses_snap_rele(stp->set_snap);
609 				ses_close(stp->set_target);
610 				stp->set_target = NULL;
611 			}
612 			(void) ct_ctl_ack(ctlfd, evid);
613 		} else {
614 			/* if this is the negend, then abandon the contract */
615 			ses_ct_print("got contract negend");
616 			if (stp->set_ctid) {
617 				(void) snprintf(buf, sizeof (buf),
618 				    "abandon old contract %d", stp->set_ctid);
619 				ses_ct_print(buf);
620 				stp->set_ctid = 0;
621 			}
622 			(void) ct_ctl_abandon(ctlfd);
623 		}
624 		(void) close(ctlfd);
625 		(void) pthread_mutex_unlock(&stp->set_lock);
626 		ct_event_free(ev);
627 		(void) pthread_mutex_unlock(&ses_sslmt);
628 	}
629 	(void) close(efd);
630 	return (NULL);
631 }
632 
633 int
634 find_thr_sig(void)
635 {
636 	int i;
637 	sigset_t oset, rset;
638 	int sig[] = {SIGTERM, SIGUSR1, SIGUSR2};
639 	int sig_sz = sizeof (sig) / sizeof (int);
640 	int rc = SIGTERM;
641 
642 	/* prefered set of signals that are likely used to terminate threads */
643 	(void) sigemptyset(&oset);
644 	(void) pthread_sigmask(SIG_SETMASK, NULL, &oset);
645 	for (i = 0; i < sig_sz; i++) {
646 		if (sigismember(&oset, sig[i]) == 0) {
647 			return (sig[i]);
648 		}
649 	}
650 
651 	/* reserved set of signals that are not allowed to terminate thread */
652 	(void) sigemptyset(&rset);
653 	(void) sigaddset(&rset, SIGABRT);
654 	(void) sigaddset(&rset, SIGKILL);
655 	(void) sigaddset(&rset, SIGSTOP);
656 	(void) sigaddset(&rset, SIGCANCEL);
657 
658 	/* Find signal that is not masked and not in the reserved list. */
659 	for (i = 1; i < MAXSIG; i++) {
660 		if (sigismember(&rset, i) == 1) {
661 			continue;
662 		}
663 		if (sigismember(&oset, i) == 0) {
664 			return (i);
665 		}
666 	}
667 
668 	return (rc);
669 }
670 
671 /*ARGSUSED*/
672 static void
673 ses_handler(int sig)
674 {
675 }
676 
677 static void
678 ses_thread_init(topo_mod_t *mod)
679 {
680 	pthread_attr_t *attr = NULL;
681 	struct sigaction act;
682 	ses_mod_list_t *smod;
683 
684 	(void) pthread_mutex_lock(&sesthread.mt);
685 	sesthread.count++;
686 	smod = topo_mod_zalloc(mod, sizeof (*smod));
687 	smod->smod_mod = mod;
688 	smod->smod_next = ses_smod;
689 	ses_smod = smod;
690 	if (sesthread.tid == 0) {
691 		/* find a suitable signal to use for killing the thread below */
692 		sesthread.thr_sig = find_thr_sig();
693 
694 		/* if don't have a handler for this signal, create one */
695 		(void) sigaction(sesthread.thr_sig, NULL, &act);
696 		if (act.sa_handler == SIG_DFL || act.sa_handler == SIG_IGN)
697 			act.sa_handler = ses_handler;
698 		(void) sigaction(sesthread.thr_sig, &act, NULL);
699 
700 		/* create a thread to listen for offline events */
701 		(void) pthread_create(&sesthread.tid,
702 		    attr, ses_contract_thread, NULL);
703 	}
704 	(void) pthread_mutex_unlock(&sesthread.mt);
705 }
706 
707 static void
708 ses_thread_fini(topo_mod_t *mod)
709 {
710 	ses_mod_list_t *smod, *prev_smod;
711 
712 	(void) pthread_mutex_lock(&sesthread.mt);
713 	prev_smod = NULL;
714 	for (smod = ses_smod; smod != NULL; smod = smod->smod_next) {
715 		if (smod->smod_mod == mod) {
716 			if (prev_smod == NULL)
717 				ses_smod = smod->smod_next;
718 			else
719 				prev_smod->smod_next = smod->smod_next;
720 			topo_mod_free(mod, smod, sizeof (*smod));
721 			break;
722 		}
723 		prev_smod = smod;
724 	}
725 	if (--sesthread.count > 0) {
726 		(void) pthread_mutex_unlock(&sesthread.mt);
727 		return;
728 	}
729 	sesthread.doexit = 1;
730 	(void) pthread_mutex_unlock(&sesthread.mt);
731 	(void) pthread_kill(sesthread.tid, sesthread.thr_sig);
732 	(void) pthread_join(sesthread.tid, NULL);
733 	sesthread.tid = 0;
734 }
735 
736 static void
737 ses_create_contract(topo_mod_t *mod, ses_enum_target_t *stp)
738 {
739 	int tfd, len, rval;
740 	char link_path[PATH_MAX];
741 
742 	stp->set_ctid = 0;
743 
744 	/* convert "/dev" path into "/devices" path */
745 	if ((len = readlink(stp->set_devpath, link_path, PATH_MAX)) < 0) {
746 		topo_mod_dprintf(mod, "readlink failed");
747 		return;
748 	}
749 	link_path[len] = '\0';
750 
751 	/* set up template to create new contract */
752 	tfd = open64(CTFS_ROOT "/device/template", O_RDWR);
753 	(void) ct_tmpl_set_critical(tfd, CT_DEV_EV_OFFLINE);
754 	(void) ct_tmpl_set_cookie(tfd, (uint64_t)(uintptr_t)stp);
755 
756 	/* strip "../../devices" off the front and create the contract */
757 	if ((rval = ct_dev_tmpl_set_minor(tfd, &link_path[13])) != 0)
758 		topo_mod_dprintf(mod, "failed to set minor %s rval = %d",
759 		    &link_path[13], rval);
760 	else if ((rval = ct_tmpl_create(tfd, &stp->set_ctid)) != 0)
761 		topo_mod_dprintf(mod, "failed to create ctid rval = %d", rval);
762 	else
763 		topo_mod_dprintf(mod, "created ctid=%" _PRIdID, stp->set_ctid);
764 	(void) close(tfd);
765 }
766 
767 static void
768 ses_target_free(topo_mod_t *mod, ses_enum_target_t *stp)
769 {
770 	if (--stp->set_refcount == 0) {
771 		/* check if already closed due to contract offline request */
772 		(void) pthread_mutex_lock(&stp->set_lock);
773 		if (stp->set_target) {
774 			ses_snap_rele(stp->set_snap);
775 			ses_close(stp->set_target);
776 			stp->set_target = NULL;
777 		}
778 		if (stp->set_ctid) {
779 			int ctlfd;
780 			char path[PATH_MAX];
781 
782 			topo_mod_dprintf(mod, "abandon old contract %" _PRIdID,
783 			    stp->set_ctid);
784 			(void) snprintf(path, PATH_MAX,
785 			    CTFS_ROOT "/device/%" _PRIdID "/ctl",
786 			    stp->set_ctid);
787 			ctlfd = open64(path, O_WRONLY);
788 			(void) ct_ctl_abandon(ctlfd);
789 			(void) close(ctlfd);
790 			stp->set_ctid = 0;
791 		}
792 		(void) pthread_mutex_unlock(&stp->set_lock);
793 		ses_ssl_free(mod, stp);
794 		topo_mod_strfree(mod, stp->set_devpath);
795 		topo_mod_free(mod, stp, sizeof (ses_enum_target_t));
796 	}
797 }
798 
799 static void
800 ses_data_free(ses_enum_data_t *sdp, ses_enum_chassis_t *pcp)
801 {
802 	topo_mod_t *mod = sdp->sed_mod;
803 	ses_enum_chassis_t *cp;
804 	ses_enum_node_t *np;
805 	ses_enum_target_t *tp;
806 	ses_alt_node_t *ap;
807 	topo_list_t *cpl;
808 
809 
810 	if (pcp != NULL)
811 		cpl = &pcp->sec_subchassis;
812 	else
813 		cpl = &sdp->sed_chassis;
814 
815 	while ((cp = topo_list_next(cpl)) != NULL) {
816 		topo_list_delete(cpl, cp);
817 
818 		while ((np = topo_list_next(&cp->sec_nodes)) != NULL) {
819 			while ((ap = topo_list_next(&np->sen_alt_nodes)) !=
820 			    NULL) {
821 				topo_list_delete(&np->sen_alt_nodes, ap);
822 				topo_mod_free(mod, ap, sizeof (ses_alt_node_t));
823 			}
824 			topo_list_delete(&cp->sec_nodes, np);
825 			topo_mod_free(mod, np, sizeof (ses_enum_node_t));
826 		}
827 
828 		while ((tp = topo_list_next(&cp->sec_targets)) != NULL) {
829 			topo_list_delete(&cp->sec_targets, tp);
830 			ses_target_free(mod, tp);
831 		}
832 
833 		topo_mod_free(mod, cp, sizeof (ses_enum_chassis_t));
834 	}
835 
836 	if (pcp == NULL) {
837 		dev_list_free(mod, &sdp->sed_devs);
838 		topo_mod_free(mod, sdp, sizeof (ses_enum_data_t));
839 	}
840 }
841 
842 /*
843  * For enclosure nodes, we have a special contains method.  By default, the hc
844  * walker will compare the node name and instance number to determine if an
845  * FMRI matches.  For enclosures where the enumeration order is impossible to
846  * predict, we instead use the chassis-id as a unique identifier, and ignore
847  * the instance number.
848  */
849 static int
850 fmri_contains(topo_mod_t *mod, nvlist_t *nv1, nvlist_t *nv2)
851 {
852 	uint8_t v1, v2;
853 	nvlist_t **hcp1, **hcp2;
854 	int err, i;
855 	uint_t nhcp1, nhcp2;
856 	nvlist_t *a1, *a2;
857 	char *c1, *c2;
858 	int mindepth;
859 
860 	if (nvlist_lookup_uint8(nv1, FM_VERSION, &v1) != 0 ||
861 	    nvlist_lookup_uint8(nv2, FM_VERSION, &v2) != 0 ||
862 	    v1 > FM_HC_SCHEME_VERSION || v2 > FM_HC_SCHEME_VERSION)
863 		return (topo_mod_seterrno(mod, EMOD_FMRI_VERSION));
864 
865 	err = nvlist_lookup_nvlist_array(nv1, FM_FMRI_HC_LIST, &hcp1, &nhcp1);
866 	err |= nvlist_lookup_nvlist_array(nv2, FM_FMRI_HC_LIST, &hcp2, &nhcp2);
867 	if (err != 0)
868 		return (topo_mod_seterrno(mod, EMOD_FMRI_NVL));
869 
870 	/*
871 	 * If the chassis-id doesn't match, then these FMRIs are not
872 	 * equivalent.  If one of the FMRIs doesn't have a chassis ID, then we
873 	 * have no choice but to fall back to the instance ID.
874 	 */
875 	if (nvlist_lookup_nvlist(nv1, FM_FMRI_AUTHORITY, &a1) == 0 &&
876 	    nvlist_lookup_nvlist(nv2, FM_FMRI_AUTHORITY, &a2) == 0 &&
877 	    nvlist_lookup_string(a1, FM_FMRI_AUTH_CHASSIS, &c1) == 0 &&
878 	    nvlist_lookup_string(a2, FM_FMRI_AUTH_CHASSIS, &c2) == 0) {
879 		if (strcmp(c1, c2) != 0)
880 			return (0);
881 
882 		mindepth = 1;
883 	} else {
884 		mindepth = 0;
885 	}
886 
887 	if (nhcp2 < nhcp1)
888 		return (0);
889 
890 	for (i = 0; i < nhcp1; i++) {
891 		char *nm1 = NULL;
892 		char *nm2 = NULL;
893 		char *id1 = NULL;
894 		char *id2 = NULL;
895 
896 		(void) nvlist_lookup_string(hcp1[i], FM_FMRI_HC_NAME, &nm1);
897 		(void) nvlist_lookup_string(hcp2[i], FM_FMRI_HC_NAME, &nm2);
898 		(void) nvlist_lookup_string(hcp1[i], FM_FMRI_HC_ID, &id1);
899 		(void) nvlist_lookup_string(hcp2[i], FM_FMRI_HC_ID, &id2);
900 		if (nm1 == NULL || nm2 == NULL || id1 == NULL || id2 == NULL)
901 			return (topo_mod_seterrno(mod, EMOD_FMRI_NVL));
902 
903 		if (strcmp(nm1, nm2) == 0 &&
904 		    (i < mindepth || strcmp(id1, id2) == 0))
905 			continue;
906 
907 		return (0);
908 	}
909 
910 	return (1);
911 }
912 
913 /*ARGSUSED*/
914 static int
915 ses_contains(topo_mod_t *mod, tnode_t *tn, topo_version_t version,
916     nvlist_t *in, nvlist_t **out)
917 {
918 	int ret;
919 	nvlist_t *nv1, *nv2;
920 
921 	if (version > TOPO_METH_CONTAINS_VERSION)
922 		return (topo_mod_seterrno(mod, EMOD_VER_NEW));
923 
924 	if (nvlist_lookup_nvlist(in, TOPO_METH_FMRI_ARG_FMRI, &nv1) != 0 ||
925 	    nvlist_lookup_nvlist(in, TOPO_METH_FMRI_ARG_SUBFMRI, &nv2) != 0)
926 		return (topo_mod_seterrno(mod, EMOD_METHOD_INVAL));
927 
928 	ret = fmri_contains(mod, nv1, nv2);
929 	if (ret < 0)
930 		return (-1);
931 
932 	if (topo_mod_nvalloc(mod, out, NV_UNIQUE_NAME) == 0) {
933 		if (nvlist_add_uint32(*out, TOPO_METH_CONTAINS_RET,
934 		    ret) == 0)
935 			return (0);
936 		else
937 			nvlist_free(*out);
938 	}
939 
940 	return (-1);
941 
942 }
943 
944 /*
945  * Return a current instance of the node.  This is somewhat complicated because
946  * we need to take a new snapshot in order to get the new data, but we don't
947  * want to be constantly taking SES snapshots if the consumer is going to do a
948  * series of queries.  So we adopt the strategy of assuming that the SES state
949  * is not going to be rapidly changing, and limit our snapshot frequency to
950  * some defined bounds.
951  */
952 ses_node_t *
953 ses_node_lock(topo_mod_t *mod, tnode_t *tn)
954 {
955 	ses_enum_target_t *tp = topo_node_getspecific(tn);
956 	hrtime_t now;
957 	ses_snap_t *snap;
958 	int err;
959 	uint64_t nodeid;
960 	ses_node_t *np;
961 
962 	if (tp == NULL) {
963 		(void) topo_mod_seterrno(mod, EMOD_METHOD_NOTSUP);
964 		return (NULL);
965 	}
966 
967 	(void) pthread_mutex_lock(&tp->set_lock);
968 
969 	/*
970 	 * Determine if we need to take a new snapshot.
971 	 */
972 	now = gethrtime();
973 
974 	if (tp->set_target == NULL) {
975 		/*
976 		 * We may have closed the device but not yet abandoned the
977 		 * contract (ie we've had the offline event but not yet the
978 		 * negend). If so, just return failure.
979 		 */
980 		if (tp->set_ctid != 0) {
981 			(void) topo_mod_seterrno(mod, EMOD_METHOD_NOTSUP);
982 			(void) pthread_mutex_unlock(&tp->set_lock);
983 			return (NULL);
984 		}
985 
986 		/*
987 		 * The device has been closed due to a contract offline
988 		 * request, then we need to reopen it and create a new contract.
989 		 */
990 		if ((tp->set_target =
991 		    ses_open(LIBSES_VERSION, tp->set_devpath)) == NULL) {
992 			sysevent_id_t eid;
993 
994 			(void) topo_mod_seterrno(mod, EMOD_METHOD_NOTSUP);
995 			(void) pthread_mutex_unlock(&tp->set_lock);
996 			topo_mod_dprintf(mod, "recheck_dir - "
997 			    "can no longer open %s", tp->set_devpath);
998 			(void) sysevent_post_event(EC_PLATFORM,
999 			    ESC_PLATFORM_SP_RESET, SUNW_VENDOR, "fmd", NULL,
1000 			    &eid);
1001 			return (NULL);
1002 		}
1003 		topo_mod_dprintf(mod, "reopen contract");
1004 		ses_create_contract(mod, tp);
1005 		tp->set_snap = ses_snap_hold(tp->set_target);
1006 		tp->set_snaptime = gethrtime();
1007 	} else if (now - tp->set_snaptime > (ses_snap_freq * 1000 * 1000) &&
1008 	    (snap = ses_snap_new(tp->set_target)) != NULL) {
1009 		if (ses_snap_generation(snap) !=
1010 		    ses_snap_generation(tp->set_snap)) {
1011 			/*
1012 			 * If we find ourselves in this situation, we're in
1013 			 * trouble.  The generation count has changed, which
1014 			 * indicates that our current topology is out of date.
1015 			 * But we need to consult the new topology in order to
1016 			 * determine presence at this moment in time.  We can't
1017 			 * go back and change the topo snapshot in situ, so
1018 			 * we'll just have to fail the call in this unlikely
1019 			 * scenario.
1020 			 */
1021 			ses_snap_rele(snap);
1022 			(void) topo_mod_seterrno(mod, EMOD_METHOD_NOTSUP);
1023 			(void) pthread_mutex_unlock(&tp->set_lock);
1024 			return (NULL);
1025 		} else {
1026 			ses_snap_rele(tp->set_snap);
1027 			tp->set_snap = snap;
1028 		}
1029 		tp->set_snaptime = gethrtime();
1030 	}
1031 
1032 	snap = tp->set_snap;
1033 
1034 	verify(topo_prop_get_uint64(tn, TOPO_PGROUP_SES,
1035 	    TOPO_PROP_NODE_ID, &nodeid, &err) == 0);
1036 	verify((np = ses_node_lookup(snap, nodeid)) != NULL);
1037 
1038 	return (np);
1039 }
1040 
1041 /*ARGSUSED*/
1042 void
1043 ses_node_unlock(topo_mod_t *mod, tnode_t *tn)
1044 {
1045 	ses_enum_target_t *tp = topo_node_getspecific(tn);
1046 
1047 	verify(tp != NULL);
1048 
1049 	(void) pthread_mutex_unlock(&tp->set_lock);
1050 }
1051 
1052 /*
1053  * Determine if the element is present.
1054  */
1055 /*ARGSUSED*/
1056 static int
1057 ses_present(topo_mod_t *mod, tnode_t *tn, topo_version_t version,
1058     nvlist_t *in, nvlist_t **out)
1059 {
1060 	boolean_t present;
1061 	ses_node_t *np;
1062 	nvlist_t *props, *nvl;
1063 	uint64_t status;
1064 
1065 	if ((np = ses_node_lock(mod, tn)) == NULL)
1066 		return (-1);
1067 
1068 	verify((props = ses_node_props(np)) != NULL);
1069 	verify(nvlist_lookup_uint64(props,
1070 	    SES_PROP_STATUS_CODE, &status) == 0);
1071 
1072 	ses_node_unlock(mod, tn);
1073 
1074 	present = (status != SES_ESC_NOT_INSTALLED);
1075 
1076 	if (topo_mod_nvalloc(mod, &nvl, NV_UNIQUE_NAME) != 0)
1077 		return (topo_mod_seterrno(mod, EMOD_FMRI_NVL));
1078 
1079 	if (nvlist_add_uint32(nvl, TOPO_METH_PRESENT_RET,
1080 	    present) != 0) {
1081 		nvlist_free(nvl);
1082 		return (topo_mod_seterrno(mod, EMOD_FMRI_NVL));
1083 	}
1084 
1085 	*out = nvl;
1086 
1087 	return (0);
1088 }
1089 
1090 /*
1091  * Sets standard properties for a ses node (enclosure, bay, controller
1092  * or expander).
1093  * This includes setting the FRU, as well as setting the
1094  * authority information.  When  the fru topo node(frutn) is not NULL
1095  * its resouce should be used as FRU.
1096  */
1097 static int
1098 ses_set_standard_props(topo_mod_t *mod, tnode_t *frutn, tnode_t *tn,
1099     nvlist_t *auth, uint64_t nodeid, const char *path)
1100 {
1101 	int err;
1102 	char *product, *chassis;
1103 	nvlist_t *fmri;
1104 
1105 	/*
1106 	 * Set the authority explicitly if specified.
1107 	 */
1108 	if (auth) {
1109 		verify(nvlist_lookup_string(auth, FM_FMRI_AUTH_PRODUCT,
1110 		    &product) == 0);
1111 		verify(nvlist_lookup_string(auth, FM_FMRI_AUTH_CHASSIS,
1112 		    &chassis) == 0);
1113 		if (topo_prop_set_string(tn, FM_FMRI_AUTHORITY,
1114 		    FM_FMRI_AUTH_PRODUCT, TOPO_PROP_IMMUTABLE, product,
1115 		    &err) != 0 ||
1116 		    topo_prop_set_string(tn, FM_FMRI_AUTHORITY,
1117 		    FM_FMRI_AUTH_CHASSIS, TOPO_PROP_IMMUTABLE, chassis,
1118 		    &err) != 0 ||
1119 		    topo_prop_set_string(tn, FM_FMRI_AUTHORITY,
1120 		    FM_FMRI_AUTH_SERVER, TOPO_PROP_IMMUTABLE, "",
1121 		    &err) != 0) {
1122 			topo_mod_dprintf(mod, "failed to add authority "
1123 			    "properties: %s\n", topo_strerror(err));
1124 			return (topo_mod_seterrno(mod, err));
1125 		}
1126 	}
1127 
1128 	/*
1129 	 * Copy the resource and set that as the FRU.
1130 	 */
1131 	if (frutn != NULL) {
1132 		if (topo_node_resource(frutn, &fmri, &err) != 0) {
1133 			topo_mod_dprintf(mod,
1134 			    "topo_node_resource() failed : %s\n",
1135 			    topo_strerror(err));
1136 			return (topo_mod_seterrno(mod, err));
1137 		}
1138 	} else {
1139 		if (topo_node_resource(tn, &fmri, &err) != 0) {
1140 			topo_mod_dprintf(mod,
1141 			    "topo_node_resource() failed : %s\n",
1142 			    topo_strerror(err));
1143 			return (topo_mod_seterrno(mod, err));
1144 		}
1145 	}
1146 
1147 	if (topo_node_fru_set(tn, fmri, 0, &err) != 0) {
1148 		topo_mod_dprintf(mod,
1149 		    "topo_node_fru_set() failed : %s\n",
1150 		    topo_strerror(err));
1151 		nvlist_free(fmri);
1152 		return (topo_mod_seterrno(mod, err));
1153 	}
1154 
1155 	nvlist_free(fmri);
1156 
1157 	/*
1158 	 * Set the SES-specific properties so that consumers can query
1159 	 * additional information about the particular SES element.
1160 	 */
1161 	if (topo_pgroup_create(tn, &ses_pgroup, &err) != 0) {
1162 		topo_mod_dprintf(mod, "failed to create propgroup "
1163 		    "%s: %s\n", TOPO_PGROUP_SES, topo_strerror(err));
1164 		return (-1);
1165 	}
1166 
1167 	if (topo_prop_set_uint64(tn, TOPO_PGROUP_SES,
1168 	    TOPO_PROP_NODE_ID, TOPO_PROP_IMMUTABLE,
1169 	    nodeid, &err) != 0) {
1170 		topo_mod_dprintf(mod,
1171 		    "failed to create property %s: %s\n",
1172 		    TOPO_PROP_NODE_ID, topo_strerror(err));
1173 		return (-1);
1174 	}
1175 
1176 	if (topo_prop_set_string(tn, TOPO_PGROUP_SES,
1177 	    TOPO_PROP_TARGET_PATH, TOPO_PROP_IMMUTABLE,
1178 	    path, &err) != 0) {
1179 		topo_mod_dprintf(mod,
1180 		    "failed to create property %s: %s\n",
1181 		    TOPO_PROP_TARGET_PATH, topo_strerror(err));
1182 		return (-1);
1183 	}
1184 
1185 	return (0);
1186 }
1187 
1188 /*
1189  * Iterate over the SES phy information. If any of the ports indicates that it's
1190  * a SATA device and we haven't matched any disk devices yet, that means
1191  * that the HBA was able to create a WWN for the SATA device based on its GUID,
1192  * which is good. However, SES includes the WWN for the device's STP bridge. In
1193  * theory, if the driver includes the WWN based on the SATA guid then it should
1194  * also set the bridge-port property indicating the WWN that should match the
1195  * SATA device.
1196  */
1197 static int
1198 ses_create_disk_bridge(ses_enum_data_t *sdp, tnode_t *pnode, nvlist_t *props,
1199     tnode_t **child)
1200 {
1201 	nvlist_t **phys;
1202 	uint_t i, n_phys;
1203 	topo_mod_t *mod = sdp->sed_mod;
1204 
1205 	if (nvlist_lookup_nvlist_array(props, SES_SAS_PROP_PHYS, &phys,
1206 	    &n_phys) != 0)
1207 		return (1);
1208 
1209 	for (i = 0; i < n_phys; i++) {
1210 		uint64_t wwn;
1211 		boolean_t sata;
1212 		char wwnstr[64];
1213 
1214 		if (nvlist_lookup_uint64(phys[i], SES_SAS_PROP_ADDR,
1215 		    &wwn) != 0 || wwn == 0) {
1216 			continue;
1217 		}
1218 
1219 		if (nvlist_lookup_boolean_value(phys[i],
1220 		    SES_SAS_PROP_SATA_DEVICE, &sata) != 0 || !sata) {
1221 			continue;
1222 		}
1223 
1224 		if (scsi_wwn_to_wwnstr(wwn, 0, wwnstr) == NULL)
1225 			continue;
1226 
1227 		if (disk_declare_bridge(mod, pnode, &sdp->sed_devs,
1228 		    wwnstr, child) == 0) {
1229 			return (0);
1230 		}
1231 
1232 	}
1233 
1234 	return (1);
1235 }
1236 
1237 /*
1238  * Callback to add a disk to a given bay.  We first check the status-code to
1239  * determine if a disk is present, ignoring those that aren't in an appropriate
1240  * state.  We then scan the parent bay node's SAS address array to determine
1241  * possible attached SAS addresses.  We create a disk node if the disk is not
1242  * SAS or the SES target does not support the necessary pages for this; if we
1243  * find the SAS address, we create a disk node and also correlate it with
1244  * the corresponding Solaris device node to fill in the rest of the data.
1245  */
1246 static int
1247 ses_create_disk(ses_enum_data_t *sdp, tnode_t *pnode, nvlist_t *props)
1248 {
1249 	topo_mod_t *mod = sdp->sed_mod;
1250 	uint64_t status;
1251 	uint_t s, nsas;
1252 	char **paths;
1253 	int err, ret;
1254 	tnode_t *child = NULL;
1255 
1256 	/*
1257 	 * Skip devices that are not in a present (and possibly damaged) state.
1258 	 * Also, skip devices that this expander is either not fully wired to,
1259 	 * or are hidden due to SAS zoning, as indicated by the
1260 	 * SES_ESC_NO_ACCESS state.
1261 	 */
1262 	if (nvlist_lookup_uint64(props, SES_PROP_STATUS_CODE, &status) != 0)
1263 		return (0);
1264 
1265 	if (status != SES_ESC_UNSUPPORTED &&
1266 	    status != SES_ESC_OK &&
1267 	    status != SES_ESC_CRITICAL &&
1268 	    status != SES_ESC_NONCRITICAL &&
1269 	    status != SES_ESC_UNRECOVERABLE &&
1270 	    status != SES_ESC_UNKNOWN)
1271 		return (0);
1272 
1273 	topo_mod_dprintf(mod, "found attached disk");
1274 
1275 	/*
1276 	 * Create the disk range.
1277 	 */
1278 	if (topo_node_range_create(mod, pnode, DISK, 0, 0) != 0) {
1279 		topo_mod_dprintf(mod,
1280 		    "topo_node_create_range() failed: %s",
1281 		    topo_mod_errmsg(mod));
1282 		return (-1);
1283 	}
1284 
1285 	/*
1286 	 * Look through all SAS addresses and attempt to correlate them to a
1287 	 * known Solaris device.  If we don't find a matching node, then we
1288 	 * don't enumerate the disk node.
1289 	 * Note that TOPO_PROP_SAS_ADDR prop includes SAS address from
1290 	 * alternate elements that represent the same device.
1291 	 */
1292 	if (topo_prop_get_string_array(pnode, TOPO_PGROUP_SES,
1293 	    TOPO_PROP_SAS_ADDR, &paths, &nsas, &err) != 0)
1294 		return (0);
1295 
1296 	err = 0;
1297 
1298 	for (s = 0; s < nsas; s++) {
1299 		ret = disk_declare_addr(mod, pnode, &sdp->sed_devs, paths[s],
1300 		    &child);
1301 		if (ret == 0) {
1302 			break;
1303 		} else if (ret < 0) {
1304 			err = -1;
1305 			break;
1306 		}
1307 	}
1308 
1309 	/*
1310 	 * We need to take another pass through the properties for this bay by
1311 	 * iterating over the phys and noting if any of these are SATA. Note,
1312 	 * this information isn't commonly part of the topo tree at this time,
1313 	 * hence why we end up going back and iterating over the properties
1314 	 * ourselves.
1315 	 */
1316 	if (s == nsas) {
1317 		if (ses_create_disk_bridge(sdp, pnode, props, &child) != 0)
1318 			(void) disk_declare_non_enumerated(mod, pnode, &child);
1319 	}
1320 
1321 	/* copy sas_addresses (target-ports) from parent (with 'w'added) */
1322 	if (child != NULL) {
1323 		int i;
1324 		char **tports;
1325 		uint64_t wwn;
1326 
1327 		tports = topo_mod_zalloc(mod, sizeof (char *) * nsas);
1328 		if (tports != NULL) {
1329 			for (i = 0; i < nsas; i++) {
1330 				if (scsi_wwnstr_to_wwn(paths[i], &wwn) !=
1331 				    DDI_SUCCESS)
1332 					break;
1333 				tports[i] = scsi_wwn_to_wwnstr(wwn, 1, NULL);
1334 				if (tports[i] == NULL)
1335 					break;
1336 			}
1337 			/* if they all worked then create the property */
1338 			if (i == nsas)
1339 				(void) topo_prop_set_string_array(child,
1340 				    TOPO_PGROUP_STORAGE,
1341 				    TOPO_STORAGE_TARGET_PORT_L0IDS,
1342 				    TOPO_PROP_IMMUTABLE, (const char **)tports,
1343 				    nsas, &err);
1344 
1345 			for (i = 0; i < nsas; i++)
1346 				if (tports[i] != NULL)
1347 					scsi_free_wwnstr(tports[i]);
1348 			topo_mod_free(mod, tports, sizeof (char *) * nsas);
1349 		}
1350 	}
1351 
1352 	for (s = 0; s < nsas; s++)
1353 		topo_mod_free(mod, paths[s], strlen(paths[s]) + 1);
1354 	topo_mod_free(mod, paths, nsas * sizeof (char *));
1355 
1356 	return (err);
1357 }
1358 
1359 static int
1360 ses_add_bay_props(topo_mod_t *mod, tnode_t *tn, ses_enum_node_t *snp)
1361 {
1362 	ses_alt_node_t *ap;
1363 	ses_node_t *np;
1364 	nvlist_t *props;
1365 
1366 	nvlist_t **phys;
1367 	uint_t i, j, n_phys, all_phys = 0;
1368 	char **paths;
1369 	uint64_t addr;
1370 	size_t len;
1371 	int terr, err = -1;
1372 
1373 	for (ap = topo_list_next(&snp->sen_alt_nodes); ap != NULL;
1374 	    ap = topo_list_next(ap)) {
1375 		np = ap->san_node;
1376 		props = ses_node_props(np);
1377 
1378 		if (nvlist_lookup_nvlist_array(props, SES_SAS_PROP_PHYS,
1379 		    &phys, &n_phys) != 0)
1380 			continue;
1381 
1382 		all_phys += n_phys;
1383 	}
1384 
1385 	if (all_phys == 0)
1386 		return (0);
1387 
1388 	if ((paths = topo_mod_zalloc(mod, all_phys * sizeof (char *))) == NULL)
1389 		return (-1);
1390 
1391 	for (i = 0, ap = topo_list_next(&snp->sen_alt_nodes); ap != NULL;
1392 	    ap = topo_list_next(ap)) {
1393 		np = ap->san_node;
1394 		props = ses_node_props(np);
1395 
1396 		if (nvlist_lookup_nvlist_array(props, SES_SAS_PROP_PHYS,
1397 		    &phys, &n_phys) != 0)
1398 			continue;
1399 
1400 		for (j = 0; j < n_phys; j++) {
1401 			if (nvlist_lookup_uint64(phys[j], SES_SAS_PROP_ADDR,
1402 			    &addr) != 0)
1403 				continue;
1404 
1405 			len = snprintf(NULL, 0, "%016llx", addr) + 1;
1406 			if ((paths[i] = topo_mod_alloc(mod, len)) == NULL)
1407 				goto error;
1408 
1409 			(void) snprintf(paths[i], len, "%016llx", addr);
1410 
1411 			++i;
1412 		}
1413 	}
1414 
1415 	err = topo_prop_set_string_array(tn, TOPO_PGROUP_SES,
1416 	    TOPO_PROP_SAS_ADDR, TOPO_PROP_IMMUTABLE,
1417 	    (const char **)paths, i, &terr);
1418 	if (err != 0)
1419 		err = topo_mod_seterrno(mod, terr);
1420 
1421 error:
1422 	for (i = 0; i < all_phys && paths[i] != NULL; i++)
1423 		topo_mod_free(mod, paths[i], strlen(paths[i]) + 1);
1424 	topo_mod_free(mod, paths, all_phys * sizeof (char *));
1425 
1426 	return (err);
1427 }
1428 
1429 static const char *
1430 lookup_bay_override(const char *product_id)
1431 {
1432 	for (uint_t i = 0; i < N_BAY_LBL_OVERRIDES; i++) {
1433 		if (strcmp(product_id,
1434 		    bay_label_overrides[i].slbl_product) == 0) {
1435 			return (bay_label_overrides[i].slbl_mname);
1436 		}
1437 	}
1438 	return (NULL);
1439 }
1440 
1441 /*
1442  * Callback to create a basic node (bay, psu, fan, or controller and expander).
1443  */
1444 static int
1445 ses_create_generic(ses_enum_data_t *sdp, ses_enum_node_t *snp, tnode_t *pnode,
1446     tnode_t *frutn, const char *nodename, const char *labelname,
1447     tnode_t **node)
1448 {
1449 	ses_node_t *np = snp->sen_node;
1450 	ses_node_t *parent;
1451 	uint64_t instance = snp->sen_instance;
1452 	topo_mod_t *mod = sdp->sed_mod;
1453 	nvlist_t *props, *aprops;
1454 	nvlist_t *auth = NULL, *fmri = NULL;
1455 	tnode_t *tn = NULL;
1456 	char *clean_label = NULL, label[128];
1457 	int err;
1458 	char *part = NULL, *serial = NULL, *revision = NULL;
1459 	char *desc;
1460 	boolean_t report;
1461 
1462 	props = ses_node_props(np);
1463 
1464 	(void) nvlist_lookup_string(props, LIBSES_PROP_PART, &part);
1465 	(void) nvlist_lookup_string(props, LIBSES_PROP_SERIAL, &serial);
1466 
1467 	topo_mod_dprintf(mod, "adding %s %" PRIu64, nodename, instance);
1468 
1469 	/*
1470 	 * Create the node.  The interesting information is all copied from the
1471 	 * parent enclosure node, so there is not much to do.
1472 	 */
1473 	if ((auth = topo_mod_auth(mod, pnode)) == NULL)
1474 		goto error;
1475 
1476 	/*
1477 	 * We want to report revision information for the controller nodes, but
1478 	 * we do not get per-element revision information.  However, we do have
1479 	 * revision information for the entire enclosure, and we can use the
1480 	 * 'reported-via' property to know that this controller corresponds to
1481 	 * the given revision information.  This means we cannot get revision
1482 	 * information for targets we are not explicitly connected to, but
1483 	 * there is little we can do about the situation.
1484 	 */
1485 	if (strcmp(nodename, CONTROLLER) == 0 &&
1486 	    nvlist_lookup_boolean_value(props, SES_PROP_REPORT, &report) == 0 &&
1487 	    report) {
1488 		for (parent = ses_node_parent(np); parent != NULL;
1489 		    parent = ses_node_parent(parent)) {
1490 			if (ses_node_type(parent) == SES_NODE_ENCLOSURE) {
1491 				(void) nvlist_lookup_string(
1492 				    ses_node_props(parent),
1493 				    SES_EN_PROP_REV, &revision);
1494 				break;
1495 			}
1496 		}
1497 	}
1498 
1499 	if ((fmri = topo_mod_hcfmri(mod, pnode, FM_HC_SCHEME_VERSION,
1500 	    nodename, (topo_instance_t)instance, NULL, auth, part, revision,
1501 	    serial)) == NULL) {
1502 		topo_mod_dprintf(mod, "topo_mod_hcfmri() failed: %s",
1503 		    topo_mod_errmsg(mod));
1504 		goto error;
1505 	}
1506 
1507 	if ((tn = topo_node_bind(mod, pnode, nodename,
1508 	    instance, fmri)) == NULL) {
1509 		topo_mod_dprintf(mod, "topo_node_bind() failed: %s",
1510 		    topo_mod_errmsg(mod));
1511 		goto error;
1512 	}
1513 
1514 	/*
1515 	 * For the node label, we look for the following in order:
1516 	 *
1517 	 * <ses-description>
1518 	 * <ses-class-description> <instance>
1519 	 * <default-type-label> <instance>
1520 	 */
1521 	if (nvlist_lookup_string(props, SES_PROP_DESCRIPTION, &desc) != 0 ||
1522 	    desc[0] == '\0') {
1523 		parent = ses_node_parent(np);
1524 		aprops = ses_node_props(parent);
1525 		if (nvlist_lookup_string(aprops, SES_PROP_CLASS_DESCRIPTION,
1526 		    &desc) != 0 || desc[0] == '\0')
1527 			desc = (char *)labelname;
1528 		(void) snprintf(label, sizeof (label), "%s %llu", desc,
1529 		    instance);
1530 		desc = label;
1531 	}
1532 
1533 	if ((clean_label = topo_mod_clean_str(mod, desc)) == NULL)
1534 		goto error;
1535 
1536 	if (topo_prop_set_string(tn, TOPO_PGROUP_PROTOCOL, TOPO_PROP_LABEL,
1537 	    TOPO_PROP_MUTABLE, clean_label, &err) < 0)
1538 		goto error;
1539 
1540 	if (ses_set_standard_props(mod, frutn, tn, NULL, ses_node_id(np),
1541 	    snp->sen_target->set_devpath) != 0)
1542 		goto error;
1543 
1544 	if (strcmp(nodename, BAY) == 0) {
1545 		const char *label_method;
1546 		char *product;
1547 		nvlist_t *args = NULL;
1548 
1549 		if (ses_add_bay_props(mod, tn, snp) != 0)
1550 			goto error;
1551 
1552 		if (topo_method_register(mod, tn, ses_bay_methods) != 0) {
1553 			topo_mod_dprintf(mod,
1554 			    "topo_method_register() failed: %s",
1555 			    topo_mod_errmsg(mod));
1556 			goto error;
1557 		}
1558 
1559 		/*
1560 		 * Ideally we'd perform this sort of override with a platform
1561 		 * specific XML map file, and that would work here if we only
1562 		 * wanted to override the bay node label.  However, we'd also
1563 		 * like the disk node label (if the bay is occupied) to inherit
1564 		 * the overriden bay label.  So we need to ensure the
1565 		 * propmethod is registered before we create the child disk
1566 		 * node.
1567 		 */
1568 		if ((product = topo_mod_product(mod)) == NULL) {
1569 			(void) topo_mod_seterrno(mod, EMOD_NOMEM);
1570 			goto error;
1571 		}
1572 		if ((label_method = lookup_bay_override(product)) != NULL) {
1573 			if (topo_mod_nvalloc(mod, &args, NV_UNIQUE_NAME) != 0 ||
1574 			    topo_prop_method_register(tn, TOPO_PGROUP_PROTOCOL,
1575 			    TOPO_PROP_LABEL, TOPO_TYPE_STRING, label_method,
1576 			    args, &err)) {
1577 				topo_mod_dprintf(mod,
1578 				    "Failed to register method: %s on %s=%"
1579 				    PRIu64, label_method, BAY,
1580 				    topo_node_instance(tn));
1581 				topo_mod_strfree(mod, product);
1582 				nvlist_free(args);
1583 				goto error;
1584 			}
1585 			nvlist_free(args);
1586 		}
1587 		topo_mod_strfree(mod, product);
1588 
1589 		if (ses_create_disk(sdp, tn, props) != 0)
1590 			goto error;
1591 	} else if ((strcmp(nodename, FAN) == 0) ||
1592 	    (strcmp(nodename, PSU) == 0) ||
1593 	    (strcmp(nodename, CONTROLLER) == 0)) {
1594 		/*
1595 		 * Only fan, psu, and controller nodes have a 'present' method.
1596 		 * Bay nodes are always present, and disk nodes are present by
1597 		 * virtue of being enumerated and SAS expander nodes and
1598 		 * SAS connector nodes are also always present once
1599 		 * the parent controller is found.
1600 		 */
1601 		if (topo_method_register(mod, tn, ses_component_methods) != 0) {
1602 			topo_mod_dprintf(mod,
1603 			    "topo_method_register() failed: %s",
1604 			    topo_mod_errmsg(mod));
1605 			goto error;
1606 		}
1607 
1608 	}
1609 
1610 	snp->sen_target->set_refcount++;
1611 	topo_node_setspecific(tn, snp->sen_target);
1612 
1613 	nvlist_free(auth);
1614 	nvlist_free(fmri);
1615 	topo_mod_strfree(mod, clean_label);
1616 	if (node != NULL) *node = tn;
1617 	return (0);
1618 
1619 error:
1620 	nvlist_free(auth);
1621 	nvlist_free(fmri);
1622 	topo_mod_strfree(mod, clean_label);
1623 	return (-1);
1624 }
1625 
1626 /*
1627  * Create SAS expander specific props.
1628  */
1629 /*ARGSUSED*/
1630 static int
1631 ses_set_expander_props(ses_enum_data_t *sdp, ses_enum_node_t *snp,
1632     tnode_t *ptnode, tnode_t *tnode, int *phycount, int64_t *connlist)
1633 {
1634 	ses_node_t *np = snp->sen_node;
1635 	topo_mod_t *mod = sdp->sed_mod;
1636 	nvlist_t *auth = NULL, *fmri = NULL;
1637 	nvlist_t *props, **phylist;
1638 	int err, i;
1639 	uint_t pcount;
1640 	uint64_t sasaddr, connidx;
1641 	char sasaddr_str[17];
1642 	boolean_t found = B_FALSE, ses_found = B_FALSE;
1643 	dev_di_node_t *dnode, *sesdnode;
1644 
1645 	props = ses_node_props(np);
1646 
1647 	/*
1648 	 * the uninstalled expander is not enumerated by checking
1649 	 * the element status code.  No present present' method provided.
1650 	 */
1651 	/*
1652 	 * Get the Expander SAS address.  It should exist.
1653 	 */
1654 	if (nvlist_lookup_uint64(props, SES_EXP_PROP_SAS_ADDR,
1655 	    &sasaddr) != 0) {
1656 		topo_mod_dprintf(mod,
1657 		    "Failed to get prop %s.", SES_EXP_PROP_SAS_ADDR);
1658 		goto error;
1659 	}
1660 
1661 	(void) sprintf(sasaddr_str, "%llx", sasaddr);
1662 
1663 	/* search matching dev_di_node. */
1664 	for (dnode = topo_list_next(&sdp->sed_devs); dnode != NULL;
1665 	    dnode = topo_list_next(dnode)) {
1666 		for (i = 0; i < dnode->ddn_ppath_count; i++) {
1667 			if ((dnode->ddn_target_port[i] != NULL) &&
1668 			    (strstr(dnode->ddn_target_port[i],
1669 			    sasaddr_str) != NULL)) {
1670 				found = B_TRUE;
1671 				break;
1672 			}
1673 		}
1674 		if (found)
1675 			break;
1676 	}
1677 
1678 	if (!found) {
1679 		topo_mod_dprintf(mod,
1680 		    "ses_set_expander_props: Failed to find matching "
1681 		    "devinfo node for Exapnder SAS address %s",
1682 		    SES_EXP_PROP_SAS_ADDR);
1683 		/* continue on to get storage group props. */
1684 	} else {
1685 		/* create/set the devfs-path and devid in the smp group */
1686 		if (topo_pgroup_create(tnode, &smp_pgroup, &err) != 0) {
1687 			topo_mod_dprintf(mod, "ses_set_expander_props: "
1688 			    "failed to create smp property group %s\n",
1689 			    topo_strerror(err));
1690 			goto error;
1691 		} else {
1692 			if (topo_prop_set_string(tnode, TOPO_PGROUP_SMP,
1693 			    TOPO_PROP_SMP_TARGET_PORT, TOPO_PROP_IMMUTABLE,
1694 			    dnode->ddn_target_port[i], &err) != 0) {
1695 				topo_mod_dprintf(mod, "ses_set_expander_props: "
1696 				    "set %s error %s\n", TOPO_PROP_SAS_ADDR,
1697 				    topo_strerror(err));
1698 			}
1699 			if (topo_prop_set_string(tnode, TOPO_PGROUP_SMP,
1700 			    TOPO_PROP_SMP_DEV_PATH, TOPO_PROP_IMMUTABLE,
1701 			    dnode->ddn_dpath, &err) != 0) {
1702 				topo_mod_dprintf(mod, "ses_set_expander_props: "
1703 				    "set dev error %s\n", topo_strerror(err));
1704 			}
1705 			if (topo_prop_set_string(tnode, TOPO_PGROUP_SMP,
1706 			    TOPO_PROP_SMP_DEVID, TOPO_PROP_IMMUTABLE,
1707 			    dnode->ddn_devid, &err) != 0) {
1708 				topo_mod_dprintf(mod, "ses_set_expander_props: "
1709 				    "set devid error %s\n", topo_strerror(err));
1710 			}
1711 			if (dnode->ddn_ppath_count != 0 &&
1712 			    topo_prop_set_string_array(tnode, TOPO_PGROUP_SMP,
1713 			    TOPO_PROP_SMP_PHYS_PATH, TOPO_PROP_IMMUTABLE,
1714 			    (const char **)dnode->ddn_ppath,
1715 			    dnode->ddn_ppath_count, &err) != 0) {
1716 				topo_mod_dprintf(mod, "ses_set_expander_props: "
1717 				    "set phys-path error %s\n",
1718 				    topo_strerror(err));
1719 			}
1720 		}
1721 	}
1722 
1723 	/* update the ses property group with SES target info */
1724 	if ((topo_pgroup_create(tnode, &ses_pgroup, &err) != 0) &&
1725 	    (err != ETOPO_PROP_DEFD)) {
1726 		/* SES prop group doesn't exist but failed to be created. */
1727 		topo_mod_dprintf(mod, "ses_set_expander_props: "
1728 		    "ses pgroup create error %s\n", topo_strerror(err));
1729 		goto error;
1730 	} else {
1731 		/* locate assciated enclosure dev_di_node. */
1732 		for (sesdnode = topo_list_next(&sdp->sed_devs);
1733 		    sesdnode != NULL; sesdnode = topo_list_next(sesdnode)) {
1734 			for (i = 0; i < sesdnode->ddn_ppath_count; i++) {
1735 				/*
1736 				 * check if attached port exists and
1737 				 * its node type is enclosure and
1738 				 * attached port is same as sas address of
1739 				 * the expander and
1740 				 * bridge port for virtual phy indication
1741 				 * exist.
1742 				 */
1743 				if ((sesdnode->ddn_attached_port[i] != NULL) &&
1744 				    (sesdnode->ddn_dtype == DTYPE_ESI) &&
1745 				    (strstr(sesdnode->ddn_attached_port[i],
1746 				    sasaddr_str) != NULL) &&
1747 				    (sesdnode->ddn_bridge_port[i] != NULL)) {
1748 					ses_found = B_TRUE;
1749 					break;
1750 				}
1751 			}
1752 			if (ses_found) break;
1753 		}
1754 
1755 		if (ses_found) {
1756 			if (topo_prop_set_string(tnode, TOPO_PGROUP_SES,
1757 			    TOPO_PROP_SES_TARGET_PORT, TOPO_PROP_IMMUTABLE,
1758 			    sesdnode->ddn_target_port[i], &err) != 0) {
1759 				topo_mod_dprintf(mod, "ses_set_expander_props: "
1760 				    "set ses %s error %s\n", TOPO_PROP_SAS_ADDR,
1761 				    topo_strerror(err));
1762 			}
1763 			if (topo_prop_set_string(tnode, TOPO_PGROUP_SES,
1764 			    TOPO_PROP_SES_DEV_PATH, TOPO_PROP_IMMUTABLE,
1765 			    sesdnode->ddn_dpath, &err) != 0) {
1766 				topo_mod_dprintf(mod, "ses_set_expander_props: "
1767 				    "set ses dev error %s\n",
1768 				    topo_strerror(err));
1769 			}
1770 			if (topo_prop_set_string(tnode, TOPO_PGROUP_SES,
1771 			    TOPO_PROP_SES_DEVID, TOPO_PROP_IMMUTABLE,
1772 			    sesdnode->ddn_devid, &err) != 0) {
1773 				topo_mod_dprintf(mod, "ses_set_expander_props: "
1774 				    "set ses devid error %s\n",
1775 				    topo_strerror(err));
1776 			}
1777 			if (sesdnode->ddn_ppath_count != 0 &&
1778 			    topo_prop_set_string_array(tnode, TOPO_PGROUP_SES,
1779 			    TOPO_PROP_SES_PHYS_PATH, TOPO_PROP_IMMUTABLE,
1780 			    (const char **)sesdnode->ddn_ppath,
1781 			    sesdnode->ddn_ppath_count, &err) != 0) {
1782 				topo_mod_dprintf(mod, "ses_set_expander_props: "
1783 				    "set ses phys-path error %s\n",
1784 				    topo_strerror(err));
1785 			}
1786 
1787 		}
1788 	}
1789 
1790 	/* create the storage group */
1791 	if (topo_pgroup_create(tnode, &storage_pgroup, &err) != 0) {
1792 		topo_mod_dprintf(mod, "ses_set_expander_props: "
1793 		    "create storage error %s\n", topo_strerror(err));
1794 		goto error;
1795 	} else {
1796 		/* set the SAS address prop out of expander element status. */
1797 		if (topo_prop_set_string(tnode, TOPO_PGROUP_STORAGE,
1798 		    TOPO_PROP_SAS_ADDR, TOPO_PROP_IMMUTABLE, sasaddr_str,
1799 		    &err) != 0) {
1800 			topo_mod_dprintf(mod, "ses_set_expander_props: "
1801 			    "set %s error %s\n", TOPO_PROP_SAS_ADDR,
1802 			    topo_strerror(err));
1803 		}
1804 
1805 		/* Get the phy information for the expander */
1806 		if (nvlist_lookup_nvlist_array(props, SES_SAS_PROP_PHYS,
1807 		    &phylist, &pcount) != 0) {
1808 			topo_mod_dprintf(mod,
1809 			    "Failed to get prop %s.", SES_SAS_PROP_PHYS);
1810 		} else {
1811 			/*
1812 			 * For each phy, get the connector element index and
1813 			 * stores into connector element index array.
1814 			 */
1815 			*phycount = pcount;
1816 			for (i = 0; i < pcount; i++) {
1817 				if (nvlist_lookup_uint64(phylist[i],
1818 				    SES_PROP_CE_IDX, &connidx) == 0) {
1819 					if (connidx != 0xff) {
1820 						connlist[i] = connidx;
1821 					} else {
1822 						connlist[i] = -1;
1823 					}
1824 				} else {
1825 					/* Fail to get the index. set to -1. */
1826 					connlist[i] = -1;
1827 				}
1828 			}
1829 
1830 			/* set the phy count prop of the expander. */
1831 			if (topo_prop_set_uint64(tnode, TOPO_PGROUP_STORAGE,
1832 			    TOPO_PROP_PHY_COUNT, TOPO_PROP_IMMUTABLE, pcount,
1833 			    &err) != 0) {
1834 				topo_mod_dprintf(mod, "ses_set_expander_props: "
1835 				    "set %s error %s\n", TOPO_PROP_PHY_COUNT,
1836 				    topo_strerror(err));
1837 			}
1838 
1839 			/*
1840 			 * set the connector element index of
1841 			 * the expander phys.
1842 			 */
1843 		}
1844 
1845 		/* populate other misc storage group properties */
1846 		if (found) {
1847 			if (dnode->ddn_mfg && (topo_prop_set_string(tnode,
1848 			    TOPO_PGROUP_STORAGE, TOPO_STORAGE_MANUFACTURER,
1849 			    TOPO_PROP_IMMUTABLE, dnode->ddn_mfg, &err) != 0)) {
1850 				topo_mod_dprintf(mod, "ses_set_expander_props: "
1851 				    "set mfg error %s\n", topo_strerror(err));
1852 			}
1853 
1854 			if (dnode->ddn_model && (topo_prop_set_string(tnode,
1855 			    TOPO_PGROUP_STORAGE, TOPO_STORAGE_MODEL,
1856 			    TOPO_PROP_IMMUTABLE,
1857 			    dnode->ddn_model, &err) != 0)) {
1858 				topo_mod_dprintf(mod, "ses_set_expander_props: "
1859 				    "set model error %s\n", topo_strerror(err));
1860 			}
1861 
1862 			if (dnode->ddn_serial && (topo_prop_set_string(tnode,
1863 			    TOPO_PGROUP_STORAGE, TOPO_STORAGE_SERIAL_NUM,
1864 			    TOPO_PROP_IMMUTABLE,
1865 			    dnode->ddn_serial, &err) != 0)) {
1866 				topo_mod_dprintf(mod, "ses_set_expander_props: "
1867 				    "set serial error %s\n",
1868 				    topo_strerror(err));
1869 			}
1870 
1871 			if (dnode->ddn_firm && (topo_prop_set_string(tnode,
1872 			    TOPO_PGROUP_STORAGE,
1873 			    TOPO_STORAGE_FIRMWARE_REV, TOPO_PROP_IMMUTABLE,
1874 			    dnode->ddn_firm, &err) != 0)) {
1875 				topo_mod_dprintf(mod, "ses_set_expander_props: "
1876 				    "set firm error %s\n", topo_strerror(err));
1877 			}
1878 		}
1879 	}
1880 
1881 	return (0);
1882 
1883 error:
1884 	nvlist_free(auth);
1885 	nvlist_free(fmri);
1886 	return (-1);
1887 }
1888 
1889 /*
1890  * Create SAS expander specific props.
1891  */
1892 /*ARGSUSED*/
1893 static int
1894 ses_set_connector_props(ses_enum_data_t *sdp, ses_enum_node_t *snp,
1895     tnode_t *tnode, int64_t phy_mask)
1896 {
1897 	ses_node_t *np = snp->sen_node;
1898 	topo_mod_t *mod = sdp->sed_mod;
1899 	nvlist_t *props;
1900 	int err, i;
1901 	uint64_t conntype;
1902 	char phymask_str[17], *conntype_str;
1903 	boolean_t   found;
1904 
1905 	props = ses_node_props(np);
1906 
1907 	/*
1908 	 * convert phy mask to string.
1909 	 */
1910 	(void) snprintf(phymask_str, 17, "%llx", phy_mask);
1911 
1912 	/* create the storage group */
1913 	if (topo_pgroup_create(tnode, &storage_pgroup, &err) != 0) {
1914 		topo_mod_dprintf(mod, "ses_set_expander_props: "
1915 		    "create storage error %s\n", topo_strerror(err));
1916 		return (-1);
1917 	} else {
1918 		/* set the SAS address prop of the expander. */
1919 		if (topo_prop_set_string(tnode, TOPO_PGROUP_STORAGE,
1920 		    TOPO_STORAGE_SAS_PHY_MASK, TOPO_PROP_IMMUTABLE,
1921 		    phymask_str, &err) != 0) {
1922 			topo_mod_dprintf(mod, "ses_set_expander_props: "
1923 			    "set %s error %s\n", TOPO_STORAGE_SAS_PHY_MASK,
1924 			    topo_strerror(err));
1925 		}
1926 
1927 		/* Get the connector type information for the expander */
1928 		if (nvlist_lookup_uint64(props,
1929 		    SES_SC_PROP_CONNECTOR_TYPE, &conntype) != 0) {
1930 			topo_mod_dprintf(mod, "Failed to get prop %s.",
1931 			    TOPO_STORAGE_SAS_PHY_MASK);
1932 		} else {
1933 			found = B_FALSE;
1934 			for (i = 0; ; i++) {
1935 				if (sas_connector_type_list[i].sct_type ==
1936 				    SAS_CONNECTOR_TYPE_CODE_NOT_DEFINED) {
1937 					break;
1938 				}
1939 				if (sas_connector_type_list[i].sct_type ==
1940 				    conntype) {
1941 					conntype_str =
1942 					    sas_connector_type_list[i].sct_name;
1943 					found = B_TRUE;
1944 					break;
1945 				}
1946 			}
1947 
1948 			if (!found) {
1949 				if (conntype <
1950 				    SAS_CONNECTOR_TYPE_CODE_NOT_DEFINED) {
1951 					conntype_str =
1952 					    SAS_CONNECTOR_TYPE_RESERVED;
1953 				} else {
1954 					conntype_str =
1955 					    SAS_CONNECTOR_TYPE_NOT_DEFINED;
1956 				}
1957 			}
1958 
1959 			/* set the phy count prop of the expander. */
1960 			if (topo_prop_set_string(tnode, TOPO_PGROUP_STORAGE,
1961 			    TOPO_STORAGE_SAS_CONNECTOR_TYPE,
1962 			    TOPO_PROP_IMMUTABLE, conntype_str, &err) != 0) {
1963 				topo_mod_dprintf(mod, "ses_set_expander_props: "
1964 				    "set %s error %s\n", TOPO_PROP_PHY_COUNT,
1965 				    topo_strerror(err));
1966 			}
1967 		}
1968 	}
1969 
1970 	return (0);
1971 }
1972 
1973 /*
1974  * Instantiate SAS expander nodes for a given ESC Electronics node(controller)
1975  * nodes.
1976  */
1977 /*ARGSUSED*/
1978 static int
1979 ses_create_esc_sasspecific(ses_enum_data_t *sdp, ses_enum_node_t *snp,
1980     tnode_t *pnode, ses_enum_chassis_t *cp,
1981     boolean_t dorange)
1982 {
1983 	topo_mod_t *mod = sdp->sed_mod;
1984 	tnode_t	*exptn, *contn;
1985 	boolean_t found;
1986 	sas_connector_phy_data_t connectors[64] = {0};
1987 	uint64_t max;
1988 	ses_enum_node_t *ctlsnp, *xsnp, *consnp;
1989 	ses_node_t *np = snp->sen_node;
1990 	nvlist_t *props, *psprops;
1991 	uint64_t index, psindex, conindex, psstatus, i, j, count;
1992 	int64_t cidxlist[256] = {0};
1993 	int phycount;
1994 
1995 	props = ses_node_props(np);
1996 
1997 	if (nvlist_lookup_uint64(props, SES_PROP_ELEMENT_ONLY_INDEX,
1998 	    &index) != 0)
1999 		return (-1);
2000 
2001 	/*
2002 	 * For SES constroller node, check to see if there are
2003 	 * associated SAS expanders.
2004 	 */
2005 	found = B_FALSE;
2006 	max = 0;
2007 	for (ctlsnp = topo_list_next(&cp->sec_nodes); ctlsnp != NULL;
2008 	    ctlsnp = topo_list_next(ctlsnp)) {
2009 		if (ctlsnp->sen_type == SES_ET_SAS_EXPANDER) {
2010 			found = B_TRUE;
2011 			if (ctlsnp->sen_instance > max)
2012 				max = ctlsnp->sen_instance;
2013 		}
2014 	}
2015 
2016 	/*
2017 	 * No SAS expander found notthing to process.
2018 	 */
2019 	if (!found)
2020 		return (0);
2021 
2022 	topo_mod_dprintf(mod, "%s Controller %" PRIu64 ": creating "
2023 	    "%" PRIu64 " %s nodes", cp->sec_csn, index, max + 1, SASEXPANDER);
2024 
2025 	/*
2026 	 * The max number represent the number of elements
2027 	 * deducted from the highest SES_PROP_ELEMENT_CLASS_INDEX
2028 	 * of SET_ET_SAS_EXPANDER type element.
2029 	 *
2030 	 * There may be multiple ESC Electronics element(controllers)
2031 	 * within JBOD(typicall two for redundancy) and SAS expander
2032 	 * elements are associated with only one of them.  We are
2033 	 * still creating the range based max number here.
2034 	 * That will cover the case that all expanders are associated
2035 	 * with one SES controller.
2036 	 */
2037 	if (dorange && topo_node_range_create(mod, pnode,
2038 	    SASEXPANDER, 0, max) != 0) {
2039 		topo_mod_dprintf(mod,
2040 		    "topo_node_create_range() failed: %s",
2041 		    topo_mod_errmsg(mod));
2042 		return (-1);
2043 	}
2044 
2045 	/*
2046 	 * Search exapnders with the parent index matching with
2047 	 * ESC Electronics element index.
2048 	 * Note the index used here is a global index across
2049 	 * SES elements.
2050 	 */
2051 	for (xsnp = topo_list_next(&cp->sec_nodes); xsnp != NULL;
2052 	    xsnp = topo_list_next(xsnp)) {
2053 		if (xsnp->sen_type == SES_ET_SAS_EXPANDER) {
2054 			/*
2055 			 * get the parent ESC controller.
2056 			 */
2057 			psprops = ses_node_props(xsnp->sen_node);
2058 			if (nvlist_lookup_uint64(psprops,
2059 			    SES_PROP_STATUS_CODE, &psstatus) == 0) {
2060 				if (psstatus == SES_ESC_NOT_INSTALLED) {
2061 					/*
2062 					 * Not installed.
2063 					 * Don't create a ndoe.
2064 					 */
2065 					continue;
2066 				}
2067 			} else {
2068 				/*
2069 				 * The element should have status code.
2070 				 * If not there is no way to find
2071 				 * out if the expander element exist or
2072 				 * not.
2073 				 */
2074 				continue;
2075 			}
2076 
2077 			/* Get the physical parent index to compare. */
2078 			if (nvlist_lookup_uint64(psprops,
2079 			    LIBSES_PROP_PHYS_PARENT, &psindex) == 0) {
2080 				if (index == psindex) {
2081 		/* indentation moved forward */
2082 		/*
2083 		 * Handle basic node information of SAS expander
2084 		 * element - binding to parent node and
2085 		 * allocating FMRI...
2086 		 */
2087 		if (ses_create_generic(sdp, xsnp, pnode, pnode, SASEXPANDER,
2088 		    "SAS-EXPANDER", &exptn) != 0)
2089 			continue;
2090 		/*
2091 		 * Now handle SAS expander unique portion of node creation.
2092 		 * The max nubmer of the phy count is 256 since SES-2
2093 		 * defines as 1 byte field.  The cidxlist has the same
2094 		 * number of elements.
2095 		 *
2096 		 * We use size 64 array to store the connectors.
2097 		 * Typically a connectors associated with 4 phys so that
2098 		 * matches with the max number of connecters associated
2099 		 * with an expander.
2100 		 * The phy count goes up to 38 for Sun supported
2101 		 * JBOD.
2102 		 */
2103 		(void) memset(cidxlist, 0, sizeof (int64_t) * 64);
2104 		if (ses_set_expander_props(sdp, xsnp, pnode, exptn, &phycount,
2105 		    cidxlist) != 0) {
2106 			/*
2107 			 * error on getting specific prop failed.
2108 			 * continue on.  Note that the node is
2109 			 * left bound.
2110 			 */
2111 			continue;
2112 		}
2113 
2114 		/*
2115 		 * count represetns the number of connectors discovered so far.
2116 		 */
2117 		count = 0;
2118 		(void) memset(connectors, 0,
2119 		    sizeof (sas_connector_phy_data_t) * 64);
2120 		for (i = 0; i < phycount; i++) {
2121 			if (cidxlist[i] != -1) {
2122 				/* connector index is valid. */
2123 				for (j = 0; j < count; j++) {
2124 					if (connectors[j].scpd_index ==
2125 					    cidxlist[i]) {
2126 						/*
2127 						 * Just update phy mask.
2128 						 * The postion for connector
2129 						 * index lists(cidxlist index)
2130 						 * is set.
2131 						 */
2132 						connectors[j].scpd_pm =
2133 						    connectors[j].scpd_pm |
2134 						    (1ULL << i);
2135 						break;
2136 					}
2137 				}
2138 				/*
2139 				 * If j and count matche a  new connector
2140 				 * index is found.
2141 				 */
2142 				if (j == count) {
2143 					/* add a new index and phy mask. */
2144 					connectors[count].scpd_index =
2145 					    cidxlist[i];
2146 					connectors[count].scpd_pm =
2147 					    connectors[count].scpd_pm |
2148 					    (1ULL << i);
2149 					count++;
2150 				}
2151 			}
2152 		}
2153 
2154 		/*
2155 		 * create range for the connector nodes.
2156 		 * The class index of the ses connector element
2157 		 * is set as the instance nubmer for the node.
2158 		 * Even though one expander may not have all connectors
2159 		 * are associated with we are creating the range with
2160 		 * max possible instance number.
2161 		 */
2162 		found = B_FALSE;
2163 		max = 0;
2164 		for (consnp = topo_list_next(&cp->sec_nodes);
2165 		    consnp != NULL; consnp = topo_list_next(consnp)) {
2166 			if (consnp->sen_type == SES_ET_SAS_CONNECTOR) {
2167 				psprops = ses_node_props(consnp->sen_node);
2168 				found = B_TRUE;
2169 				if (consnp->sen_instance > max)
2170 					max = consnp->sen_instance;
2171 			}
2172 		}
2173 
2174 		/*
2175 		 * No SAS connector found nothing to process.
2176 		 */
2177 		if (!found)
2178 			return (0);
2179 
2180 		if (dorange && topo_node_range_create(mod, exptn,
2181 		    RECEPTACLE, 0, max) != 0) {
2182 			topo_mod_dprintf(mod,
2183 			    "topo_node_create_range() failed: %s",
2184 			    topo_mod_errmsg(mod));
2185 			return (-1);
2186 		}
2187 
2188 		/* search matching connector element using the index. */
2189 		for (i = 0; i < count; i++) {
2190 			found = B_FALSE;
2191 			for (consnp = topo_list_next(&cp->sec_nodes);
2192 			    consnp != NULL; consnp = topo_list_next(consnp)) {
2193 				if (consnp->sen_type == SES_ET_SAS_CONNECTOR) {
2194 					psprops = ses_node_props(
2195 					    consnp->sen_node);
2196 					/*
2197 					 * Get the physical parent index to
2198 					 * compare.
2199 					 * The connector elements are children
2200 					 * of ESC Electronics element even
2201 					 * though we enumerate them under
2202 					 * an expander in libtopo.
2203 					 */
2204 					if (nvlist_lookup_uint64(psprops,
2205 					    SES_PROP_ELEMENT_ONLY_INDEX,
2206 					    &conindex) == 0) {
2207 						if (conindex ==
2208 						    connectors[i].scpd_index) {
2209 							found = B_TRUE;
2210 							break;
2211 						}
2212 					}
2213 				}
2214 			}
2215 
2216 			/* now create a libtopo node. */
2217 			if (found) {
2218 				/* Create generic props. */
2219 				if (ses_create_generic(sdp, consnp, exptn,
2220 				    topo_node_parent(exptn),
2221 				    RECEPTACLE, "RECEPTACLE", &contn) !=
2222 				    0) {
2223 					continue;
2224 				}
2225 				/* Create connector specific props. */
2226 				if (ses_set_connector_props(sdp, consnp,
2227 				    contn, connectors[i].scpd_pm) != 0) {
2228 					continue;
2229 				}
2230 				if (topo_method_register(mod, contn,
2231 				    ses_recep_methods) != 0) {
2232 					topo_mod_dprintf(mod,
2233 					    "topo_method_register() failed: "
2234 					    "%s",
2235 					    topo_mod_errmsg(mod));
2236 					continue;
2237 				}
2238 
2239 			}
2240 		}
2241 		/* end indentation change */
2242 				}
2243 			}
2244 		}
2245 	}
2246 
2247 	return (0);
2248 }
2249 
2250 /*
2251  * Instantiate any protocol specific portion of a node.
2252  */
2253 /*ARGSUSED*/
2254 static int
2255 ses_create_protocol_specific(ses_enum_data_t *sdp, ses_enum_node_t *snp,
2256     tnode_t *pnode, uint64_t type, ses_enum_chassis_t *cp,
2257     boolean_t dorange)
2258 {
2259 
2260 	if (type == SES_ET_ESC_ELECTRONICS) {
2261 		/* create SAS specific children(expanders and connectors. */
2262 		return (ses_create_esc_sasspecific(sdp, snp, pnode, cp,
2263 		    dorange));
2264 	}
2265 
2266 	return (0);
2267 }
2268 
2269 /*
2270  * Instantiate any children of a given type.
2271  */
2272 static int
2273 ses_create_children(ses_enum_data_t *sdp, tnode_t *pnode, uint64_t type,
2274     const char *nodename, const char *defaultlabel, ses_enum_chassis_t *cp,
2275     boolean_t dorange)
2276 {
2277 	topo_mod_t *mod = sdp->sed_mod;
2278 	boolean_t found;
2279 	uint64_t max;
2280 	ses_enum_node_t *snp;
2281 	tnode_t	*tn;
2282 
2283 	/*
2284 	 * First go through and count how many matching nodes we have.
2285 	 */
2286 	max = 0;
2287 	found = B_FALSE;
2288 	for (snp = topo_list_next(&cp->sec_nodes); snp != NULL;
2289 	    snp = topo_list_next(snp)) {
2290 		if (snp->sen_type == type) {
2291 			found = B_TRUE;
2292 			if (snp->sen_instance > max)
2293 				max = snp->sen_instance;
2294 		}
2295 	}
2296 
2297 	/*
2298 	 * No enclosure should export both DEVICE and ARRAY_DEVICE elements.
2299 	 * Since we map both of these to 'disk', if an enclosure does this, we
2300 	 * just ignore the array elements.
2301 	 */
2302 	if (!found ||
2303 	    (type == SES_ET_ARRAY_DEVICE && cp->sec_hasdev))
2304 		return (0);
2305 
2306 	topo_mod_dprintf(mod, "%s: creating %" PRIu64 " %s nodes",
2307 	    cp->sec_csn, max + 1, nodename);
2308 
2309 	if (dorange && topo_node_range_create(mod, pnode,
2310 	    nodename, 0, max) != 0) {
2311 		topo_mod_dprintf(mod,
2312 		    "topo_node_create_range() failed: %s",
2313 		    topo_mod_errmsg(mod));
2314 		return (-1);
2315 	}
2316 
2317 	for (snp = topo_list_next(&cp->sec_nodes); snp != NULL;
2318 	    snp = topo_list_next(snp)) {
2319 		if (snp->sen_type == type) {
2320 			/*
2321 			 * With flat layout of ses nodes there is no
2322 			 * way to find out the direct FRU for a node.
2323 			 * Passing NULL for fru topo node.  Note that
2324 			 * ses_create_children_from_phys_tree() provides
2325 			 * the actual direct FRU for a node.
2326 			 */
2327 			if (ses_create_generic(sdp, snp, pnode, NULL,
2328 			    nodename, defaultlabel, &tn) != 0)
2329 				return (-1);
2330 			/*
2331 			 * For some SES element there may be protocol specific
2332 			 * information to process.   Here we are processing
2333 			 * the association between enclosure controller and
2334 			 * SAS expanders.
2335 			 */
2336 			if (type == SES_ET_ESC_ELECTRONICS) {
2337 				/* create SAS expander node */
2338 				if (ses_create_protocol_specific(sdp, snp,
2339 				    tn, type, cp, dorange) != 0) {
2340 					return (-1);
2341 				}
2342 			}
2343 
2344 		}
2345 	}
2346 
2347 	return (0);
2348 }
2349 
2350 /*
2351  * Instantiate a new subchassis instance in the topology.
2352  */
2353 static int
2354 ses_create_subchassis(ses_enum_data_t *sdp, tnode_t *pnode,
2355     ses_enum_chassis_t *scp)
2356 {
2357 	topo_mod_t *mod = sdp->sed_mod;
2358 	tnode_t *tn;
2359 	nvlist_t *props;
2360 	nvlist_t *auth = NULL, *fmri = NULL;
2361 	uint64_t instance = scp->sec_instance;
2362 	char *desc;
2363 	char *clean_label = NULL, label[128];
2364 	char **paths;
2365 	int i, err;
2366 	ses_enum_target_t *stp;
2367 	int ret = -1;
2368 
2369 	/*
2370 	 * Copy authority information from parent enclosure node
2371 	 */
2372 	if ((auth = topo_mod_auth(mod, pnode)) == NULL)
2373 		goto error;
2374 
2375 	/*
2376 	 * Record the subchassis serial number in the FMRI.
2377 	 * For now, we assume that logical id is the subchassis serial number.
2378 	 * If this assumption changes in future, then the following
2379 	 * piece of code will need to be updated via an RFE.
2380 	 */
2381 	if ((fmri = topo_mod_hcfmri(mod, pnode, FM_HC_SCHEME_VERSION,
2382 	    SUBCHASSIS, (topo_instance_t)instance, NULL, auth, NULL, NULL,
2383 	    NULL)) == NULL) {
2384 		topo_mod_dprintf(mod, "topo_mod_hcfmri() failed: %s",
2385 		    topo_mod_errmsg(mod));
2386 		goto error;
2387 	}
2388 
2389 	if ((tn = topo_node_bind(mod, pnode, SUBCHASSIS,
2390 	    instance, fmri)) == NULL) {
2391 		topo_mod_dprintf(mod, "topo_node_bind() failed: %s",
2392 		    topo_mod_errmsg(mod));
2393 		goto error;
2394 	}
2395 
2396 	props = ses_node_props(scp->sec_enclosure);
2397 
2398 	/*
2399 	 * Look for the subchassis label in the following order:
2400 	 *	<ses-description>
2401 	 *	<ses-class-description> <instance>
2402 	 *	<default-type-label> <instance>
2403 	 *
2404 	 * For subchassis, the default label is "SUBCHASSIS"
2405 	 */
2406 	if (nvlist_lookup_string(props, SES_PROP_DESCRIPTION, &desc) != 0 ||
2407 	    desc[0] == '\0') {
2408 		if (nvlist_lookup_string(props, SES_PROP_CLASS_DESCRIPTION,
2409 		    &desc) == 0 && desc[0] != '\0')
2410 			(void) snprintf(label, sizeof (label), "%s %llu", desc,
2411 			    instance);
2412 		else
2413 			(void) snprintf(label, sizeof (label),
2414 			    "SUBCHASSIS %llu", instance);
2415 		desc = label;
2416 	}
2417 
2418 	if ((clean_label = topo_mod_clean_str(mod, desc)) == NULL)
2419 		goto error;
2420 
2421 	if (topo_prop_set_string(tn, TOPO_PGROUP_PROTOCOL, TOPO_PROP_LABEL,
2422 	    TOPO_PROP_MUTABLE, clean_label, &err) < 0)
2423 		goto error;
2424 
2425 	if (ses_set_standard_props(mod, NULL, tn, NULL,
2426 	    ses_node_id(scp->sec_enclosure), scp->sec_target->set_devpath) != 0)
2427 		goto error;
2428 
2429 	/*
2430 	 * Set the 'chassis-type' property for this subchassis.  This is either
2431 	 * 'ses-class-description' or 'subchassis'.
2432 	 */
2433 	if (nvlist_lookup_string(props, SES_PROP_CLASS_DESCRIPTION, &desc) != 0)
2434 		desc = "subchassis";
2435 
2436 	if (topo_prop_set_string(tn, TOPO_PGROUP_SES,
2437 	    TOPO_PROP_CHASSIS_TYPE, TOPO_PROP_IMMUTABLE, desc, &err) != 0) {
2438 		topo_mod_dprintf(mod, "failed to create property %s: %s\n",
2439 		    TOPO_PROP_CHASSIS_TYPE, topo_strerror(err));
2440 		goto error;
2441 	}
2442 
2443 	/*
2444 	 * For enclosures, we want to include all possible targets (for upgrade
2445 	 * purposes).
2446 	 */
2447 	for (i = 0, stp = topo_list_next(&scp->sec_targets); stp != NULL;
2448 	    stp = topo_list_next(stp), i++)
2449 		;
2450 
2451 	verify(i != 0);
2452 	paths = alloca(i * sizeof (char *));
2453 
2454 	for (i = 0, stp = topo_list_next(&scp->sec_targets); stp != NULL;
2455 	    stp = topo_list_next(stp), i++)
2456 		paths[i] = stp->set_devpath;
2457 
2458 	if (topo_prop_set_string_array(tn, TOPO_PGROUP_SES,
2459 	    TOPO_PROP_PATHS, TOPO_PROP_IMMUTABLE, (const char **)paths,
2460 	    i, &err) != 0) {
2461 		topo_mod_dprintf(mod, "failed to create property %s: %s\n",
2462 		    TOPO_PROP_PATHS, topo_strerror(err));
2463 		goto error;
2464 	}
2465 
2466 	if (topo_method_register(mod, tn, ses_enclosure_methods) != 0) {
2467 		topo_mod_dprintf(mod, "topo_method_register() failed: %s",
2468 		    topo_mod_errmsg(mod));
2469 		goto error;
2470 	}
2471 
2472 	/*
2473 	 * Create the nodes for controllers and bays.
2474 	 */
2475 	if (ses_create_children(sdp, tn, SES_ET_ESC_ELECTRONICS,
2476 	    CONTROLLER, "CONTROLLER", scp, B_TRUE) != 0 ||
2477 	    ses_create_children(sdp, tn, SES_ET_DEVICE,
2478 	    BAY, "BAY", scp, B_TRUE) != 0 ||
2479 	    ses_create_children(sdp, tn, SES_ET_ARRAY_DEVICE,
2480 	    BAY, "BAY", scp, B_TRUE) != 0)
2481 		goto error;
2482 
2483 	ret = 0;
2484 
2485 error:
2486 	nvlist_free(auth);
2487 	nvlist_free(fmri);
2488 	topo_mod_strfree(mod, clean_label);
2489 	return (ret);
2490 }
2491 
2492 /*
2493  * Function we use to insert a node.
2494  */
2495 static int
2496 ses_phys_tree_insert(topo_mod_t *mod, ses_phys_tree_t **sproot,
2497     ses_phys_tree_t *child)
2498 {
2499 	uint64_t ppindex, eindex, pindex;
2500 	ses_phys_tree_t *node_ptr;
2501 	int ret = 0;
2502 
2503 	assert(sproot != NULL);
2504 	assert(child != NULL);
2505 
2506 	if (*sproot == NULL) {
2507 		*sproot = child;
2508 		return (0);
2509 	}
2510 
2511 	pindex = child->spt_pindex;
2512 	ppindex = (*sproot)->spt_pindex;
2513 	eindex = (*sproot)->spt_eonlyindex;
2514 
2515 	/*
2516 	 * If the element only index of the root is same as the physical
2517 	 * parent index of a node to be added, add the node as a child of
2518 	 * the current root.
2519 	 */
2520 	if (eindex == pindex) {
2521 		(void) ses_phys_tree_insert(mod, &(*sproot)->spt_child, child);
2522 		child->spt_parent = *sproot;
2523 	} else if (ppindex == pindex) {
2524 		/*
2525 		 * if the physical parent of the current root and the child
2526 		 * is same, then this should be a sibling node.
2527 		 * Siblings can be different element types and arrange
2528 		 * them by group.
2529 		 */
2530 		if ((*sproot)->spt_senumnode->sen_type ==
2531 		    child->spt_senumnode->sen_type) {
2532 			child->spt_sibling = *sproot;
2533 			*sproot = child;
2534 		} else {
2535 			/* add a node in front of matching element type. */
2536 			node_ptr = *sproot;
2537 			while (node_ptr->spt_sibling != NULL) {
2538 				if (node_ptr->spt_sibling->
2539 				    spt_senumnode->sen_type ==
2540 				    child->spt_senumnode->sen_type) {
2541 					child->spt_sibling =
2542 					    node_ptr->spt_sibling;
2543 					node_ptr->spt_sibling = child;
2544 					break;
2545 				}
2546 				node_ptr = node_ptr->spt_sibling;
2547 			}
2548 			/* no matching.  Add the child at the end. */
2549 			if (node_ptr->spt_sibling == NULL) {
2550 				node_ptr->spt_sibling = child;
2551 			}
2552 		}
2553 		child->spt_parent = (*sproot)->spt_parent;
2554 	} else {
2555 		/*
2556 		 * The root and the node is not directly related.
2557 		 * Try to insert to the child sub-tree first and then try to
2558 		 * insert to the sibling sub-trees.  If fails for both
2559 		 * the caller will retry insertion later.
2560 		 */
2561 		if ((*sproot)->spt_child) {
2562 			ret = ses_phys_tree_insert(mod, &(*sproot)->spt_child,
2563 			    child);
2564 		}
2565 		if ((*sproot)->spt_child == NULL || ret != 0) {
2566 			if ((*sproot)->spt_sibling) {
2567 				ret = ses_phys_tree_insert(mod,
2568 				    &(*sproot)->spt_sibling, child);
2569 			} else {
2570 				ret = 1;
2571 			}
2572 		}
2573 		return (ret);
2574 	}
2575 	return (0);
2576 }
2577 
2578 /*
2579  * Construct tree view of ses elements through parent phyiscal element index.
2580  * The root of tree is already constructed using the enclosure element.
2581  */
2582 static int
2583 ses_construct_phys_tree(ses_enum_data_t *sdp, ses_enum_chassis_t *cp,
2584     ses_phys_tree_t *sproot)
2585 {
2586 	ses_enum_node_t *snp;
2587 	ses_phys_tree_t	*child;
2588 	ses_phys_tree_t	*u_watch = NULL;
2589 	ses_phys_tree_t	*u_head = NULL;
2590 	ses_phys_tree_t	*u_tail = NULL;
2591 	int u_inserted = 0, u_left = 0;
2592 	nvlist_t *props;
2593 	topo_mod_t *mod = sdp->sed_mod;
2594 
2595 	for (snp = topo_list_next(&cp->sec_nodes); snp != NULL;
2596 	    snp = topo_list_next(snp)) {
2597 		if ((child = topo_mod_zalloc(mod,
2598 		    sizeof (ses_phys_tree_t))) == NULL) {
2599 			topo_mod_dprintf(mod,
2600 			    "failed to allocate root.");
2601 			return (-1);
2602 		}
2603 		child->spt_snode = snp->sen_node;
2604 		props = ses_node_props(snp->sen_node);
2605 		if (nvlist_lookup_uint64(props,
2606 		    LIBSES_PROP_PHYS_PARENT, &child->spt_pindex) != 0) {
2607 			/*
2608 			 * the prop should exist. continue to see if
2609 			 * we can build a partial tree with other elements.
2610 			 */
2611 			topo_mod_dprintf(mod,
2612 			    "ses_construct_phys_tree(): Failed to find prop %s "
2613 			    "on ses element type %" PRIu64 " and instance %"
2614 			    PRIu64 " (CSN %s).", LIBSES_PROP_PHYS_PARENT,
2615 			    snp->sen_type, snp->sen_instance, cp->sec_csn);
2616 			topo_mod_free(mod, child, sizeof (ses_phys_tree_t));
2617 			continue;
2618 		} else {
2619 			if (nvlist_lookup_boolean_value(props,
2620 			    LIBSES_PROP_FRU, &child->spt_isfru) != 0) {
2621 				topo_mod_dprintf(mod,
2622 				    "ses_construct_phys_tree(): Failed to "
2623 				    "find prop %s on ses element type %" PRIu64
2624 				    " and instance %" PRIu64 " (CSN %s).",
2625 				    LIBSES_PROP_FRU,
2626 				    snp->sen_type, snp->sen_instance,
2627 				    cp->sec_csn);
2628 				/*
2629 				 * Ignore if the prop doesn't exist.
2630 				 * Note that the enclosure itself should be
2631 				 * a FRU so if no FRU found the enclosure FRU
2632 				 * can be a direct FRU.
2633 				 */
2634 			}
2635 			verify(nvlist_lookup_uint64(props,
2636 			    SES_PROP_ELEMENT_ONLY_INDEX,
2637 			    &child->spt_eonlyindex) == 0);
2638 			verify(nvlist_lookup_uint64(props,
2639 			    SES_PROP_ELEMENT_CLASS_INDEX,
2640 			    &child->spt_cindex) == 0);
2641 		}
2642 		child->spt_senumnode = snp;
2643 		if (ses_phys_tree_insert(mod, &sproot, child) != 0) {
2644 			/* collect unresolved element to process later. */
2645 			if (u_head == NULL) {
2646 				u_head = child;
2647 				u_tail = child;
2648 			} else {
2649 				child->spt_sibling = u_head;
2650 				u_head = child;
2651 			}
2652 		}
2653 	}
2654 
2655 	/*
2656 	 * The parent of a child node may not be inserted yet.
2657 	 * Trying to insert the child until no child is left or
2658 	 * no child is not added further.  For the latter
2659 	 * the hierarchical relationship between elements
2660 	 * should be checked through SUNW,FRUID page.
2661 	 * u_watch is a watch dog to check the prgress of unresolved
2662 	 * node.
2663 	 */
2664 	u_watch = u_tail;
2665 	while (u_head) {
2666 		child = u_head;
2667 		u_head = u_head->spt_sibling;
2668 		if (u_head == NULL)
2669 			u_tail = NULL;
2670 		child->spt_sibling = NULL;
2671 		if (ses_phys_tree_insert(mod, &sproot, child) != 0) {
2672 			u_tail->spt_sibling = child;
2673 			u_tail = child;
2674 			if (child == u_watch) {
2675 				/*
2676 				 * We just scanned one round for the
2677 				 * unresolved list. Check to see whether we
2678 				 * have nodes inserted, if none, we should
2679 				 * break in case of an indefinite loop.
2680 				 */
2681 				if (u_inserted == 0) {
2682 					/*
2683 					 * Indicate there is unhandled node.
2684 					 * Chain free the whole unsolved
2685 					 * list here.
2686 					 */
2687 					u_left++;
2688 					break;
2689 				} else {
2690 					u_inserted = 0;
2691 					u_watch = u_tail;
2692 				}
2693 			}
2694 		} else {
2695 			/*
2696 			 * We just inserted one rpnode, increment the
2697 			 * unsolved_inserted counter. We will utilize this
2698 			 * counter to detect an indefinite insertion loop.
2699 			 */
2700 			u_inserted++;
2701 			if (child == u_watch) {
2702 				/*
2703 				 * watch dog node itself is inserted.
2704 				 * Set it to the tail and refresh the watching.
2705 				 */
2706 				u_watch = u_tail;
2707 				u_inserted = 0;
2708 				u_left = 0;
2709 			}
2710 		}
2711 	}
2712 
2713 	/* check if there is left out unresolved nodes. */
2714 	if (u_left) {
2715 		topo_mod_dprintf(mod, "ses_construct_phys_tree(): "
2716 		    "Failed to construct physical view of the following "
2717 		    "ses elements of Chassis CSN %s.", cp->sec_csn);
2718 		while (u_head) {
2719 			u_tail = u_head->spt_sibling;
2720 			topo_mod_dprintf(mod,
2721 			    "\telement type (%" PRIu64 ") and instance (%"
2722 			    PRIu64 ")", u_head->spt_senumnode->sen_type,
2723 			    u_head->spt_senumnode->sen_instance);
2724 			topo_mod_free(mod, u_head, sizeof (ses_phys_tree_t));
2725 			u_head = u_tail;
2726 		}
2727 		return (-1);
2728 	}
2729 
2730 	return (0);
2731 }
2732 
2733 /*
2734  * Free the whole phys tree.
2735  */
2736 static void ses_phys_tree_free(topo_mod_t *mod, ses_phys_tree_t *sproot)
2737 {
2738 	if (sproot == NULL)
2739 		return;
2740 
2741 	/* Free child tree. */
2742 	if (sproot->spt_child) {
2743 		ses_phys_tree_free(mod, sproot->spt_child);
2744 	}
2745 
2746 	/* Free sibling trees. */
2747 	if (sproot->spt_sibling) {
2748 		ses_phys_tree_free(mod, sproot->spt_sibling);
2749 	}
2750 
2751 	/* Free root node itself. */
2752 	topo_mod_free(mod, sproot, sizeof (ses_phys_tree_t));
2753 }
2754 
2755 /*
2756  * Parses phys_enum_type table to get the index of the given type.
2757  */
2758 static boolean_t
2759 is_type_enumerated(ses_phys_tree_t *node, int *index)
2760 {
2761 	int i;
2762 
2763 	for (i = 0; i < N_PHYS_ENUM_TYPES; i++) {
2764 		if (node->spt_senumnode->sen_type ==
2765 		    phys_enum_type_list[i].pet_type) {
2766 			*index = i;
2767 			return (B_TRUE);
2768 		}
2769 	}
2770 	return (B_FALSE);
2771 }
2772 
2773 /*
2774  * Recusrive routine for top-down enumeration of the tree.
2775  */
2776 static int
2777 ses_enumerate_node(ses_enum_data_t *sdp, tnode_t *pnode, ses_enum_chassis_t *cp,
2778     ses_phys_tree_t *parent, int mrange[])
2779 {
2780 	topo_mod_t *mod = sdp->sed_mod;
2781 	ses_phys_tree_t *child = NULL;
2782 	int i, ret = 0, ret_ch;
2783 	uint64_t prevtype = SES_ET_UNSPECIFIED;
2784 	ses_phys_tree_t *dirfru = NULL;
2785 	tnode_t *tn = NULL, *frutn = NULL;
2786 
2787 	if (parent == NULL) {
2788 		return (0);
2789 	}
2790 
2791 	for (child = parent->spt_child; child != NULL;
2792 	    child = child->spt_sibling) {
2793 		if (is_type_enumerated(child, &i)) {
2794 			if (prevtype != phys_enum_type_list[i].pet_type) {
2795 				/* check if range needs to be created. */
2796 				if (phys_enum_type_list[i].pet_dorange &&
2797 				    topo_node_range_create(mod, pnode,
2798 				    phys_enum_type_list[i].pet_nodename, 0,
2799 				    mrange[i]) != 0) {
2800 					topo_mod_dprintf(mod,
2801 					    "topo_node_create_range() failed: "
2802 					    "%s", topo_mod_errmsg(mod));
2803 					return (-1);
2804 				}
2805 				prevtype = phys_enum_type_list[i].pet_type;
2806 			}
2807 
2808 			if (!(child->spt_isfru)) {
2809 				for (dirfru = parent; dirfru != NULL;
2810 				    dirfru = dirfru->spt_parent) {
2811 					if (dirfru->spt_isfru) {
2812 						break;
2813 					}
2814 				}
2815 				/* found direct FRU node. */
2816 				if (dirfru) {
2817 					frutn = dirfru->spt_tnode;
2818 				} else {
2819 					frutn = NULL;
2820 				}
2821 			} else {
2822 				frutn = NULL;
2823 			}
2824 
2825 			if (ses_create_generic(sdp, child->spt_senumnode,
2826 			    pnode, frutn, phys_enum_type_list[i].pet_nodename,
2827 			    phys_enum_type_list[i].pet_defaultlabel, &tn) != 0)
2828 				return (-1);
2829 
2830 			child->spt_tnode = tn;
2831 			/*
2832 			 * For some SES element there may be protocol specific
2833 			 * information to process.   Here we are processing
2834 			 * the association between enclosure controller and
2835 			 * SAS expanders.
2836 			 */
2837 			if (phys_enum_type_list[i].pet_type ==
2838 			    SES_ET_ESC_ELECTRONICS) {
2839 				/* create SAS expander node */
2840 				if (ses_create_protocol_specific(sdp,
2841 				    child->spt_senumnode, tn,
2842 				    phys_enum_type_list[i].pet_type,
2843 				    cp, phys_enum_type_list[i].pet_dorange) !=
2844 				    0) {
2845 					return (-1);
2846 				}
2847 			}
2848 		} else {
2849 			continue;
2850 		}
2851 		ret_ch = ses_enumerate_node(sdp, tn, cp, child, mrange);
2852 		if (ret_ch)
2853 			ret = ret_ch; /* there was an error and set the ret. */
2854 	}
2855 
2856 	return (ret);
2857 }
2858 
2859 /*
2860  * Instantiate types of nodes that are specified in the hierarchy
2861  * element type list.
2862  */
2863 static int
2864 ses_create_children_from_phys_tree(ses_enum_data_t *sdp, tnode_t *pnode,
2865     ses_enum_chassis_t *cp, ses_phys_tree_t *phys_tree)
2866 {
2867 	topo_mod_t *mod = sdp->sed_mod;
2868 	int mrange[N_PHYS_ENUM_TYPES] = { 0 };
2869 	ses_enum_node_t *snp;
2870 	int i, ret;
2871 
2872 	/*
2873 	 * First get max range for each type of element to be enumerated.
2874 	 */
2875 	for (i = 0; i < N_PHYS_ENUM_TYPES; i++) {
2876 		if (phys_enum_type_list[i].pet_dorange) {
2877 			for (snp = topo_list_next(&cp->sec_nodes); snp != NULL;
2878 			    snp = topo_list_next(snp)) {
2879 				if (snp->sen_type ==
2880 				    phys_enum_type_list[i].pet_type) {
2881 					if (snp->sen_instance > mrange[i])
2882 						mrange[i] =
2883 						    snp->sen_instance;
2884 				}
2885 			}
2886 		}
2887 	}
2888 
2889 	topo_mod_dprintf(mod, "%s: creating nodes from FRU hierarchy tree.",
2890 	    cp->sec_csn);
2891 
2892 	if ((ret = ses_enumerate_node(sdp, pnode, cp, phys_tree, mrange)) !=
2893 	    0) {
2894 		topo_mod_dprintf(mod,
2895 		    "ses_create_children_from_phys_tree() failed: ");
2896 		return (ret);
2897 	}
2898 
2899 	return (0);
2900 }
2901 
2902 /*
2903  * Instantiate a new chassis instance in the topology.
2904  */
2905 static int
2906 ses_create_chassis(ses_enum_data_t *sdp, tnode_t *pnode, ses_enum_chassis_t *cp)
2907 {
2908 	topo_mod_t *mod = sdp->sed_mod;
2909 	nvlist_t *props;
2910 	char *raw_manufacturer, *raw_model, *raw_revision;
2911 	char *manufacturer = NULL, *model = NULL, *product = NULL;
2912 	char *revision = NULL;
2913 	char *serial;
2914 	char **paths;
2915 	size_t prodlen;
2916 	tnode_t *tn;
2917 	nvlist_t *fmri = NULL, *auth = NULL;
2918 	int ret = -1;
2919 	ses_enum_node_t *snp;
2920 	ses_enum_target_t *stp;
2921 	ses_enum_chassis_t *scp;
2922 	int i, err;
2923 	uint64_t sc_count = 0, pindex;
2924 	ses_phys_tree_t	*sproot = NULL;
2925 	hrtime_t start;
2926 	hrtime_t end;
2927 	double duration;
2928 
2929 	/*
2930 	 * Ignore any internal enclosures.
2931 	 */
2932 	if (cp->sec_internal)
2933 		return (0);
2934 
2935 	/*
2936 	 * Check to see if there are any devices presennt in the chassis.  If
2937 	 * not, ignore the chassis alltogether.  This is most useful for
2938 	 * ignoring internal HBAs that present a SES target but don't actually
2939 	 * manage any of the devices.
2940 	 */
2941 	for (snp = topo_list_next(&cp->sec_nodes); snp != NULL;
2942 	    snp = topo_list_next(snp)) {
2943 		if (snp->sen_type == SES_ET_DEVICE ||
2944 		    snp->sen_type == SES_ET_ARRAY_DEVICE)
2945 			break;
2946 	}
2947 
2948 	if (snp == NULL)
2949 		return (0);
2950 
2951 	props = ses_node_props(cp->sec_enclosure);
2952 
2953 	/*
2954 	 * We use the following property mappings:
2955 	 *
2956 	 * manufacturer		vendor-id
2957 	 * model		product-id
2958 	 * serial-number	libses-chassis-serial
2959 	 */
2960 	verify(nvlist_lookup_string(props, SES_EN_PROP_VID,
2961 	    &raw_manufacturer) == 0);
2962 	verify(nvlist_lookup_string(props, SES_EN_PROP_PID, &raw_model) == 0);
2963 	verify(nvlist_lookup_string(props, SES_EN_PROP_REV,
2964 	    &raw_revision) == 0);
2965 	verify(nvlist_lookup_string(props, LIBSES_EN_PROP_CSN, &serial) == 0);
2966 
2967 	/*
2968 	 * To construct the authority information, we 'clean' each string by
2969 	 * removing any offensive characters and trimmming whitespace.  For the
2970 	 * 'product-id', we use a concatenation of 'manufacturer-model'.  We
2971 	 * also take the numerical serial number and convert it to a string.
2972 	 */
2973 	if ((manufacturer = topo_mod_clean_str(mod, raw_manufacturer)) ==
2974 	    NULL || (model = topo_mod_clean_str(mod, raw_model)) == NULL ||
2975 	    (revision = topo_mod_clean_str(mod, raw_revision)) == NULL) {
2976 		goto error;
2977 	}
2978 
2979 	prodlen = strlen(manufacturer) + strlen(model) + 2;
2980 	if ((product = topo_mod_alloc(mod, prodlen)) == NULL)
2981 		goto error;
2982 
2983 	(void) snprintf(product, prodlen, "%s-%s", manufacturer, model);
2984 
2985 	/*
2986 	 * Construct the topo node and bind it to our parent.
2987 	 */
2988 	if (topo_mod_nvalloc(mod, &auth, NV_UNIQUE_NAME) != 0)
2989 		goto error;
2990 
2991 	if (nvlist_add_string(auth, FM_FMRI_AUTH_PRODUCT, product) != 0 ||
2992 	    nvlist_add_string(auth, FM_FMRI_AUTH_CHASSIS, serial) != 0) {
2993 		(void) topo_mod_seterrno(mod, EMOD_NVL_INVAL);
2994 		goto error;
2995 	}
2996 
2997 	/*
2998 	 * We pass NULL for the parent FMRI because there is no resource
2999 	 * associated with it.  For the toplevel enclosure, we leave the
3000 	 * serial/part/revision portions empty, which are reserved for
3001 	 * individual components within the chassis.
3002 	 */
3003 	if ((fmri = topo_mod_hcfmri(mod, NULL, FM_HC_SCHEME_VERSION,
3004 	    SES_ENCLOSURE, cp->sec_instance, NULL, auth,
3005 	    model, revision, serial)) == NULL) {
3006 		topo_mod_dprintf(mod, "topo_mod_hcfmri() failed: %s",
3007 		    topo_mod_errmsg(mod));
3008 		goto error;
3009 	}
3010 
3011 	if ((tn = topo_node_bind(mod, pnode, SES_ENCLOSURE,
3012 	    cp->sec_instance, fmri)) == NULL) {
3013 		topo_mod_dprintf(mod, "topo_node_bind() failed: %s",
3014 		    topo_mod_errmsg(mod));
3015 		goto error;
3016 	}
3017 
3018 	if (topo_method_register(mod, tn, ses_enclosure_methods) != 0) {
3019 		topo_mod_dprintf(mod,
3020 		    "topo_method_register() failed: %s",
3021 		    topo_mod_errmsg(mod));
3022 		goto error;
3023 	}
3024 
3025 	if (ses_set_standard_props(mod, NULL, tn, auth,
3026 	    ses_node_id(cp->sec_enclosure), cp->sec_target->set_devpath) != 0)
3027 		goto error;
3028 
3029 	/*
3030 	 * For enclosures, we want to include all possible targets (for upgrade
3031 	 * purposes).
3032 	 */
3033 	for (i = 0, stp = topo_list_next(&cp->sec_targets); stp != NULL;
3034 	    stp = topo_list_next(stp), i++)
3035 		;
3036 
3037 	verify(i != 0);
3038 	paths = alloca(i * sizeof (char *));
3039 
3040 	for (i = 0, stp = topo_list_next(&cp->sec_targets); stp != NULL;
3041 	    stp = topo_list_next(stp), i++)
3042 		paths[i] = stp->set_devpath;
3043 
3044 
3045 	if (topo_prop_set_string_array(tn, TOPO_PGROUP_SES,
3046 	    TOPO_PROP_PATHS, TOPO_PROP_IMMUTABLE, (const char **)paths,
3047 	    i, &err) != 0) {
3048 		topo_mod_dprintf(mod,
3049 		    "failed to create property %s: %s\n",
3050 		    TOPO_PROP_PATHS, topo_strerror(err));
3051 		goto error;
3052 	}
3053 
3054 	if (nvlist_lookup_uint64(props,
3055 	    LIBSES_PROP_PHYS_PARENT, &pindex) == 0) {
3056 		start = gethrtime(); /* to mearusre performance */
3057 		/*
3058 		 * The enclosure is supported through SUNW,FRUID.
3059 		 * Need to enumerate the nodes through hierarchical order.
3060 		 */
3061 		if ((sproot = topo_mod_zalloc(mod,
3062 		    sizeof (ses_phys_tree_t))) == NULL) {
3063 			topo_mod_dprintf(mod,
3064 			    "failed to allocate root: %s\n",
3065 			    topo_strerror(err));
3066 			goto error;
3067 		}
3068 		sproot->spt_pindex = pindex;
3069 		if (nvlist_lookup_boolean_value(props,
3070 		    LIBSES_PROP_FRU, &sproot->spt_isfru) != 0) {
3071 			topo_mod_dprintf(mod,
3072 			    "ses_create_chassis(): Failed to find prop %s "
3073 			    "on enclosure element (CSN %s).",
3074 			    LIBSES_PROP_FRU, cp->sec_csn);
3075 			/* an enclosure should be a FRU. continue to process. */
3076 			sproot->spt_isfru = B_TRUE;
3077 		}
3078 		if (nvlist_lookup_uint64(props,
3079 		    SES_PROP_ELEMENT_ONLY_INDEX,
3080 		    &sproot->spt_eonlyindex) != 0) {
3081 			topo_mod_dprintf(mod,
3082 			    "ses_create_chassis(): Failed to find prop %s "
3083 			    "on enclosure element (CSN %s).",
3084 			    LIBSES_PROP_PHYS_PARENT, cp->sec_csn);
3085 			topo_mod_free(mod, sproot, sizeof (ses_phys_tree_t));
3086 			goto error;
3087 		}
3088 		if (sproot->spt_pindex != sproot->spt_eonlyindex) {
3089 			topo_mod_dprintf(mod, "ses_create_chassis(): "
3090 			    "Enclosure element(CSN %s) should have "
3091 			    "itself as the parent to be the root node "
3092 			    "of FRU hierarchical tree.)", cp->sec_csn);
3093 			topo_mod_free(mod, sproot, sizeof (ses_phys_tree_t));
3094 			goto error;
3095 		} else {
3096 			sproot->spt_snode = cp->sec_enclosure;
3097 			sproot->spt_tnode = tn;
3098 			/* construct a tree. */
3099 			if (ses_construct_phys_tree(sdp, cp, sproot) != 0) {
3100 				topo_mod_dprintf(mod, "ses_create_chassis(): "
3101 				    "Failed to construct FRU hierarchical "
3102 				    "tree on enclosure (CSN %s.)",
3103 				    cp->sec_csn);
3104 			}
3105 
3106 			/* enumerate elements from the tree. */
3107 			if (ses_create_children_from_phys_tree(sdp, tn, cp,
3108 			    sproot) != 0) {
3109 				topo_mod_dprintf(mod, "ses_create_chassis(): "
3110 				    "Failed to create children topo nodes out "
3111 				    "of FRU hierarchical tree on enclosure "
3112 				    "(CSN %s).", cp->sec_csn);
3113 			}
3114 			/* destroy the phys tree. */
3115 			ses_phys_tree_free(mod, sproot);
3116 		}
3117 
3118 		end = gethrtime();
3119 		duration = end - start;
3120 		duration /= HR_SECOND;
3121 		topo_mod_dprintf(mod,
3122 		    "FRU boundary tree based enumeration: %.6f seconds",
3123 		    duration);
3124 	} else {
3125 		/*
3126 		 * Create the nodes for power supplies, fans, controllers and
3127 		 * devices.  Note that SAS exopander nodes and connector nodes
3128 		 * are handled through protocol specific processing of
3129 		 * controllers.
3130 		 */
3131 		if (ses_create_children(sdp, tn, SES_ET_POWER_SUPPLY,
3132 		    PSU, "PSU", cp, B_TRUE) != 0 ||
3133 		    ses_create_children(sdp, tn, SES_ET_COOLING,
3134 		    FAN, "FAN", cp, B_TRUE) != 0 ||
3135 		    ses_create_children(sdp, tn, SES_ET_ESC_ELECTRONICS,
3136 		    CONTROLLER, "CONTROLLER", cp, B_TRUE) != 0 ||
3137 		    ses_create_children(sdp, tn, SES_ET_DEVICE,
3138 		    BAY, "BAY", cp, B_TRUE) != 0 ||
3139 		    ses_create_children(sdp, tn, SES_ET_ARRAY_DEVICE,
3140 		    BAY, "BAY", cp, B_TRUE) != 0)
3141 			goto error;
3142 	}
3143 
3144 	if (cp->sec_maxinstance != SES_INST_NOTSET &&
3145 	    (topo_node_range_create(mod, tn, SUBCHASSIS, 0,
3146 	    cp->sec_maxinstance) != 0)) {
3147 		topo_mod_dprintf(mod, "topo_node_create_range() failed: %s",
3148 		    topo_mod_errmsg(mod));
3149 		goto error;
3150 	}
3151 
3152 	for (scp = topo_list_next(&cp->sec_subchassis); scp != NULL;
3153 	    scp = topo_list_next(scp)) {
3154 
3155 		if (ses_create_subchassis(sdp, tn, scp) != 0)
3156 			goto error;
3157 
3158 		topo_mod_dprintf(mod, "created Subchassis node with "
3159 		    "instance %" PRIu64 "\nand target (%s) under Chassis "
3160 		    "with CSN %s", scp->sec_instance,
3161 		    scp->sec_target->set_devpath, cp->sec_csn);
3162 
3163 		sc_count++;
3164 	}
3165 
3166 	topo_mod_dprintf(mod, "%s: created %" PRIu64 " %s nodes",
3167 	    cp->sec_csn, sc_count, SUBCHASSIS);
3168 
3169 	cp->sec_target->set_refcount++;
3170 	topo_node_setspecific(tn, cp->sec_target);
3171 
3172 	ret = 0;
3173 error:
3174 	topo_mod_strfree(mod, manufacturer);
3175 	topo_mod_strfree(mod, model);
3176 	topo_mod_strfree(mod, revision);
3177 	topo_mod_strfree(mod, product);
3178 
3179 	nvlist_free(fmri);
3180 	nvlist_free(auth);
3181 	return (ret);
3182 }
3183 
3184 /*
3185  * Create a bay node explicitly enumerated via XML.
3186  */
3187 static int
3188 ses_create_bays(ses_enum_data_t *sdp, tnode_t *pnode)
3189 {
3190 	topo_mod_t *mod = sdp->sed_mod;
3191 	ses_enum_chassis_t *cp;
3192 
3193 	/*
3194 	 * Iterate over chassis looking for an internal enclosure.  This
3195 	 * property is set via a vendor-specific plugin, and there should only
3196 	 * ever be a single internal chassis in a system.
3197 	 */
3198 	for (cp = topo_list_next(&sdp->sed_chassis); cp != NULL;
3199 	    cp = topo_list_next(cp)) {
3200 		if (cp->sec_internal)
3201 			break;
3202 	}
3203 
3204 	if (cp == NULL) {
3205 		topo_mod_dprintf(mod, "failed to find internal chassis\n");
3206 		return (-1);
3207 	}
3208 
3209 	if (ses_create_children(sdp, pnode, SES_ET_DEVICE,
3210 	    BAY, "BAY", cp, B_FALSE) != 0 ||
3211 	    ses_create_children(sdp, pnode, SES_ET_ARRAY_DEVICE,
3212 	    BAY, "BAY", cp, B_FALSE) != 0)
3213 		return (-1);
3214 
3215 	return (0);
3216 }
3217 
3218 /*
3219  * Initialize chassis or subchassis.
3220  */
3221 static int
3222 ses_init_chassis(topo_mod_t *mod, ses_enum_data_t *sdp, ses_enum_chassis_t *pcp,
3223     ses_enum_chassis_t *cp, ses_node_t *np, nvlist_t *props,
3224     uint64_t subchassis, ses_chassis_type_e flags)
3225 {
3226 	boolean_t internal, ident;
3227 
3228 	assert((flags & (SES_NEW_CHASSIS | SES_NEW_SUBCHASSIS |
3229 	    SES_DUP_CHASSIS | SES_DUP_SUBCHASSIS)) != 0);
3230 
3231 	assert(cp != NULL);
3232 	assert(np != NULL);
3233 	assert(props != NULL);
3234 
3235 	if (flags & (SES_NEW_SUBCHASSIS | SES_DUP_SUBCHASSIS))
3236 		assert(pcp != NULL);
3237 
3238 	topo_mod_dprintf(mod, "ses_init_chassis: %s: index %" PRIu64
3239 	    ", flags (%d)", sdp->sed_name, subchassis, flags);
3240 
3241 	if (flags & (SES_NEW_CHASSIS | SES_NEW_SUBCHASSIS)) {
3242 
3243 		topo_mod_dprintf(mod, "new chassis/subchassis");
3244 		if (nvlist_lookup_boolean_value(props,
3245 		    LIBSES_EN_PROP_INTERNAL, &internal) == 0)
3246 			cp->sec_internal = internal;
3247 
3248 		cp->sec_enclosure = np;
3249 		cp->sec_target = sdp->sed_target;
3250 
3251 		if (flags & SES_NEW_CHASSIS) {
3252 			if (!cp->sec_internal)
3253 				cp->sec_instance = sdp->sed_instance++;
3254 			topo_list_append(&sdp->sed_chassis, cp);
3255 		} else {
3256 			if (subchassis != NO_SUBCHASSIS)
3257 				cp->sec_instance = subchassis;
3258 			else
3259 				cp->sec_instance = pcp->sec_scinstance++;
3260 
3261 			if (cp->sec_instance > pcp->sec_maxinstance)
3262 				pcp->sec_maxinstance = cp->sec_instance;
3263 
3264 			topo_list_append(&pcp->sec_subchassis, cp);
3265 		}
3266 
3267 	} else {
3268 		topo_mod_dprintf(mod, "dup chassis/subchassis");
3269 		if (nvlist_lookup_boolean_value(props,
3270 		    SES_PROP_IDENT, &ident) == 0) {
3271 			topo_mod_dprintf(mod,  "overriding enclosure node");
3272 
3273 			cp->sec_enclosure = np;
3274 			cp->sec_target = sdp->sed_target;
3275 		}
3276 	}
3277 
3278 	topo_list_append(&cp->sec_targets, sdp->sed_target);
3279 	sdp->sed_current = cp;
3280 
3281 	return (0);
3282 }
3283 
3284 /*
3285  * Gather nodes from the current SES target into our chassis list, merging the
3286  * results if necessary.
3287  */
3288 static ses_walk_action_t
3289 ses_enum_gather(ses_node_t *np, void *data)
3290 {
3291 	nvlist_t *props = ses_node_props(np);
3292 	ses_enum_data_t *sdp = data;
3293 	topo_mod_t *mod = sdp->sed_mod;
3294 	ses_enum_chassis_t *cp, *scp;
3295 	ses_enum_node_t *snp;
3296 	ses_alt_node_t *sap;
3297 	char *csn;
3298 	uint64_t instance, type;
3299 	uint64_t prevstatus, status;
3300 	boolean_t report;
3301 	uint64_t subchassis = NO_SUBCHASSIS;
3302 
3303 	if (ses_node_type(np) == SES_NODE_ENCLOSURE) {
3304 		/*
3305 		 * If we have already identified the chassis for this target,
3306 		 * then this is a secondary enclosure and we should ignore it,
3307 		 * along with the rest of the tree (since this is depth-first).
3308 		 */
3309 		if (sdp->sed_current != NULL)
3310 			return (SES_WALK_ACTION_TERMINATE);
3311 
3312 		/*
3313 		 * Go through the list of chassis we have seen so far and see
3314 		 * if this serial number matches one of the known values.
3315 		 * If so, check whether this enclosure is a subchassis.
3316 		 */
3317 		if (nvlist_lookup_string(props, LIBSES_EN_PROP_CSN,
3318 		    &csn) != 0)
3319 			return (SES_WALK_ACTION_TERMINATE);
3320 
3321 		(void) nvlist_lookup_uint64(props, LIBSES_EN_PROP_SUBCHASSIS_ID,
3322 		    &subchassis);
3323 
3324 		topo_mod_dprintf(mod, "ses_enum_gather: Enclosure Node (%s) "
3325 		    "CSN (%s), subchassis (%" PRIu64 ")", sdp->sed_name, csn,
3326 		    subchassis);
3327 
3328 		/*
3329 		 * We need to determine whether this enclosure node
3330 		 * represents a chassis or a subchassis. Since we may
3331 		 * receive the enclosure nodes in a non-deterministic
3332 		 * manner, we need to account for all possible combinations:
3333 		 *	1. Chassis for the current CSN has not yet been
3334 		 *	   allocated
3335 		 *		1.1 This is a new chassis:
3336 		 *			allocate and instantiate the chassis
3337 		 *		1.2 This is a new subchassis:
3338 		 *			allocate a placeholder chassis
3339 		 *			allocate and instantiate the subchassis
3340 		 *			link the subchassis to the chassis
3341 		 *	2. Chassis for the current CSN has been allocated
3342 		 *		2.1 This is a duplicate chassis enclosure
3343 		 *			check whether to override old chassis
3344 		 *			append to chassis' target list
3345 		 *		2.2 Only placeholder chassis exists
3346 		 *			fill in the chassis fields
3347 		 *		2.3 This is a new subchassis
3348 		 *			allocate and instantiate the subchassis
3349 		 *			link the subchassis to the chassis
3350 		 *		2.4 This is a duplicate subchassis enclosure
3351 		 *			 check whether to override old chassis
3352 		 *			 append to chassis' target list
3353 		 */
3354 
3355 		for (cp = topo_list_next(&sdp->sed_chassis); cp != NULL;
3356 		    cp = topo_list_next(cp))
3357 			if (strcmp(cp->sec_csn, csn) == 0)
3358 				break;
3359 
3360 		if (cp == NULL) {
3361 			/* 1. Haven't seen a chassis with this CSN before */
3362 
3363 			if ((cp = topo_mod_zalloc(mod,
3364 			    sizeof (ses_enum_chassis_t))) == NULL)
3365 				goto error;
3366 
3367 			cp->sec_scinstance = SES_STARTING_SUBCHASSIS;
3368 			cp->sec_maxinstance = SES_INST_NOTSET;
3369 			cp->sec_csn = csn;
3370 
3371 			if (subchassis == NO_SUBCHASSIS) {
3372 				/* 1.1 This is a new chassis */
3373 
3374 				topo_mod_dprintf(mod, "%s: Initialize new "
3375 				    "chassis with CSN %s", sdp->sed_name, csn);
3376 
3377 				if (ses_init_chassis(mod, sdp, NULL, cp,
3378 				    np, props, NO_SUBCHASSIS,
3379 				    SES_NEW_CHASSIS) < 0)
3380 					goto error;
3381 			} else {
3382 				/* 1.2 This is a new subchassis */
3383 
3384 				topo_mod_dprintf(mod, "%s: Initialize new "
3385 				    "subchassis with CSN %s and index %" PRIu64,
3386 				    sdp->sed_name, csn, subchassis);
3387 
3388 				if ((scp = topo_mod_zalloc(mod,
3389 				    sizeof (ses_enum_chassis_t))) == NULL)
3390 					goto error;
3391 
3392 				scp->sec_csn = csn;
3393 
3394 				if (ses_init_chassis(mod, sdp, cp, scp, np,
3395 				    props, subchassis, SES_NEW_SUBCHASSIS) < 0)
3396 					goto error;
3397 			}
3398 		} else {
3399 			/*
3400 			 * We have a chassis or subchassis with this CSN.  If
3401 			 * it's a chassis, we must check to see whether it is
3402 			 * a placeholder previously created because we found a
3403 			 * subchassis with this CSN.  We will know that because
3404 			 * the sec_target value will not be set; it is set only
3405 			 * in ses_init_chassis().  In that case, initialise it
3406 			 * as a new chassis; otherwise, it's a duplicate and we
3407 			 * need to append only.
3408 			 */
3409 			if (subchassis == NO_SUBCHASSIS) {
3410 				if (cp->sec_target != NULL) {
3411 					/* 2.1 This is a duplicate chassis */
3412 
3413 					topo_mod_dprintf(mod, "%s: Append "
3414 					    "duplicate chassis with CSN (%s)",
3415 					    sdp->sed_name, csn);
3416 
3417 					if (ses_init_chassis(mod, sdp, NULL, cp,
3418 					    np, props, NO_SUBCHASSIS,
3419 					    SES_DUP_CHASSIS) < 0)
3420 						goto error;
3421 				} else {
3422 					/* Placeholder chassis - init it up */
3423 					topo_mod_dprintf(mod, "%s: Initialize"
3424 					    "placeholder chassis with CSN %s",
3425 					    sdp->sed_name, csn);
3426 
3427 					if (ses_init_chassis(mod, sdp, NULL,
3428 					    cp, np, props, NO_SUBCHASSIS,
3429 					    SES_NEW_CHASSIS) < 0)
3430 						goto error;
3431 
3432 				}
3433 			} else {
3434 				/* This is a subchassis */
3435 
3436 				for (scp = topo_list_next(&cp->sec_subchassis);
3437 				    scp != NULL; scp = topo_list_next(scp))
3438 					if (scp->sec_instance == subchassis)
3439 						break;
3440 
3441 				if (scp == NULL) {
3442 					/* 2.3 This is a new subchassis */
3443 
3444 					topo_mod_dprintf(mod, "%s: Initialize "
3445 					    "new subchassis with CSN (%s)",
3446 					    sdp->sed_name, csn);
3447 
3448 					if ((scp = topo_mod_zalloc(mod,
3449 					    sizeof (ses_enum_chassis_t)))
3450 					    == NULL)
3451 						goto error;
3452 
3453 					scp->sec_csn = csn;
3454 
3455 					if (ses_init_chassis(mod, sdp, cp, scp,
3456 					    np, props, subchassis,
3457 					    SES_NEW_SUBCHASSIS) < 0)
3458 						goto error;
3459 				} else {
3460 					/* 2.4 This is a duplicate subchassis */
3461 
3462 					topo_mod_dprintf(mod, "%s: Append "
3463 					    "duplicate subchassis with "
3464 					    "CSN (%s)", sdp->sed_name, csn);
3465 
3466 					if (ses_init_chassis(mod, sdp, cp, scp,
3467 					    np, props, subchassis,
3468 					    SES_DUP_SUBCHASSIS) < 0)
3469 						goto error;
3470 				}
3471 			}
3472 		}
3473 	} else if (ses_node_type(np) == SES_NODE_ELEMENT) {
3474 		/*
3475 		 * If we haven't yet seen an enclosure node and identified the
3476 		 * current chassis, something is very wrong; bail out.
3477 		 */
3478 		if (sdp->sed_current == NULL)
3479 			return (SES_WALK_ACTION_TERMINATE);
3480 
3481 		/*
3482 		 * If this isn't one of the element types we care about, then
3483 		 * ignore it.
3484 		 */
3485 		verify(nvlist_lookup_uint64(props, SES_PROP_ELEMENT_TYPE,
3486 		    &type) == 0);
3487 		if (type != SES_ET_DEVICE &&
3488 		    type != SES_ET_ARRAY_DEVICE &&
3489 		    type != SES_ET_SUNW_FANBOARD &&
3490 		    type != SES_ET_SUNW_FANMODULE &&
3491 		    type != SES_ET_COOLING &&
3492 		    type != SES_ET_SUNW_POWERBOARD &&
3493 		    type != SES_ET_SUNW_POWERMODULE &&
3494 		    type != SES_ET_POWER_SUPPLY &&
3495 		    type != SES_ET_ESC_ELECTRONICS &&
3496 		    type != SES_ET_SAS_EXPANDER &&
3497 		    type != SES_ET_SAS_CONNECTOR)
3498 			return (SES_WALK_ACTION_CONTINUE);
3499 
3500 		/*
3501 		 * Get the current instance number and see if we already know
3502 		 * about this element.  If so, it means we have multiple paths
3503 		 * to the same elements, and we should ignore the current path.
3504 		 */
3505 		verify(nvlist_lookup_uint64(props, SES_PROP_ELEMENT_CLASS_INDEX,
3506 		    &instance) == 0);
3507 		if (type == SES_ET_DEVICE || type == SES_ET_ARRAY_DEVICE)
3508 			(void) nvlist_lookup_uint64(props, SES_PROP_BAY_NUMBER,
3509 			    &instance);
3510 
3511 		cp = sdp->sed_current;
3512 
3513 		for (snp = topo_list_next(&cp->sec_nodes); snp != NULL;
3514 		    snp = topo_list_next(snp)) {
3515 			if (snp->sen_type == type &&
3516 			    snp->sen_instance == instance)
3517 				break;
3518 		}
3519 
3520 		/*
3521 		 * We prefer the new element under the following circumstances:
3522 		 *
3523 		 * - The currently known element's status is unknown or not
3524 		 *   available, but the new element has a known status.  This
3525 		 *   occurs if a given element is only available through a
3526 		 *   particular target.
3527 		 *
3528 		 * - This is an ESC_ELECTRONICS element, and the 'reported-via'
3529 		 *   property is set.  This allows us to get reliable firmware
3530 		 *   revision information from the enclosure node.
3531 		 */
3532 		if (snp != NULL) {
3533 			if (nvlist_lookup_uint64(
3534 			    ses_node_props(snp->sen_node),
3535 			    SES_PROP_STATUS_CODE, &prevstatus) != 0)
3536 				prevstatus = SES_ESC_UNSUPPORTED;
3537 			if (nvlist_lookup_uint64(
3538 			    props, SES_PROP_STATUS_CODE, &status) != 0)
3539 				status = SES_ESC_UNSUPPORTED;
3540 			if (nvlist_lookup_boolean_value(
3541 			    props, SES_PROP_REPORT, &report) != 0)
3542 				report = B_FALSE;
3543 
3544 			if ((SES_STATUS_UNAVAIL(prevstatus) &&
3545 			    !SES_STATUS_UNAVAIL(status)) ||
3546 			    (type == SES_ET_ESC_ELECTRONICS &&
3547 			    report)) {
3548 				snp->sen_node = np;
3549 				snp->sen_target = sdp->sed_target;
3550 			}
3551 
3552 			if ((sap = topo_mod_zalloc(mod,
3553 			    sizeof (ses_alt_node_t))) == NULL)
3554 				goto error;
3555 
3556 			sap->san_node = np;
3557 			topo_list_append(&snp->sen_alt_nodes, sap);
3558 
3559 			return (SES_WALK_ACTION_CONTINUE);
3560 		}
3561 
3562 		if ((snp = topo_mod_zalloc(mod,
3563 		    sizeof (ses_enum_node_t))) == NULL)
3564 			goto error;
3565 
3566 		if ((sap = topo_mod_zalloc(mod,
3567 		    sizeof (ses_alt_node_t))) == NULL) {
3568 			topo_mod_free(mod, snp, sizeof (ses_enum_node_t));
3569 			goto error;
3570 		}
3571 
3572 		topo_mod_dprintf(mod, "%s: adding node (%" PRIu64
3573 		    ", %" PRIu64 ")", sdp->sed_name, type, instance);
3574 		snp->sen_node = np;
3575 		snp->sen_type = type;
3576 		snp->sen_instance = instance;
3577 		snp->sen_target = sdp->sed_target;
3578 		sap->san_node = np;
3579 		topo_list_append(&snp->sen_alt_nodes, sap);
3580 		topo_list_append(&cp->sec_nodes, snp);
3581 
3582 		if (type == SES_ET_DEVICE)
3583 			cp->sec_hasdev = B_TRUE;
3584 	}
3585 
3586 	return (SES_WALK_ACTION_CONTINUE);
3587 
3588 error:
3589 	sdp->sed_errno = -1;
3590 	return (SES_WALK_ACTION_TERMINATE);
3591 }
3592 
3593 static int
3594 ses_process_dir(const char *dirpath, ses_enum_data_t *sdp)
3595 {
3596 	topo_mod_t *mod = sdp->sed_mod;
3597 	DIR *dir;
3598 	struct dirent *dp;
3599 	char path[PATH_MAX];
3600 	ses_enum_target_t *stp;
3601 	int err = -1;
3602 
3603 	/*
3604 	 * Open the SES target directory and iterate over any available
3605 	 * targets.
3606 	 */
3607 	if ((dir = opendir(dirpath)) == NULL) {
3608 		/*
3609 		 * If the SES target directory does not exist, then return as if
3610 		 * there are no active targets.
3611 		 */
3612 		topo_mod_dprintf(mod, "failed to open ses "
3613 		    "directory '%s'", dirpath);
3614 		return (0);
3615 	}
3616 
3617 	while ((dp = readdir(dir)) != NULL) {
3618 		if (strcmp(dp->d_name, ".") == 0 ||
3619 		    strcmp(dp->d_name, "..") == 0)
3620 			continue;
3621 
3622 		/*
3623 		 * Create a new target instance and take a snapshot.
3624 		 */
3625 		if ((stp = topo_mod_zalloc(mod,
3626 		    sizeof (ses_enum_target_t))) == NULL)
3627 			goto error;
3628 
3629 		(void) pthread_mutex_init(&stp->set_lock, NULL);
3630 
3631 		(void) snprintf(path, sizeof (path), "%s/%s", dirpath,
3632 		    dp->d_name);
3633 
3634 		/*
3635 		 * We keep track of the SES device path and export it on a
3636 		 * per-node basis to allow higher level software to get to the
3637 		 * corresponding SES state.
3638 		 */
3639 		if ((stp->set_devpath = topo_mod_strdup(mod, path)) == NULL) {
3640 			topo_mod_free(mod, stp, sizeof (ses_enum_target_t));
3641 			goto error;
3642 		}
3643 
3644 		if ((stp->set_target =
3645 		    ses_open(LIBSES_VERSION, path)) == NULL) {
3646 			topo_mod_dprintf(mod, "failed to open ses target "
3647 			    "'%s': %s", dp->d_name, ses_errmsg());
3648 			ses_sof_alloc(mod, stp->set_devpath);
3649 			topo_mod_free(mod, stp, sizeof (ses_enum_target_t));
3650 			continue;
3651 		}
3652 		topo_mod_dprintf(mod, "open contract");
3653 		ses_ssl_alloc(mod, stp);
3654 		ses_create_contract(mod, stp);
3655 
3656 		stp->set_refcount = 1;
3657 		sdp->sed_target = stp;
3658 		stp->set_snap = ses_snap_hold(stp->set_target);
3659 		stp->set_snaptime = gethrtime();
3660 
3661 		/*
3662 		 * Enumerate over all SES elements and merge them into the
3663 		 * correct ses_enum_chassis_t.
3664 		 */
3665 		sdp->sed_current = NULL;
3666 		sdp->sed_errno = 0;
3667 		sdp->sed_name = dp->d_name;
3668 		(void) ses_walk(stp->set_snap, ses_enum_gather, sdp);
3669 
3670 		if (sdp->sed_errno != 0)
3671 			goto error;
3672 	}
3673 
3674 	err = 0;
3675 error:
3676 	(void) closedir(dir);
3677 	return (err);
3678 }
3679 
3680 /*
3681  * Different generations of SMCI's 4U36 storage servers used different models
3682  * of front and rear SAS expanders.
3683  */
3684 #define	SMCI4U36_FRONT_EXPANDER_PID1	"LSI-SAS2X36"
3685 #define	SMCI4U36_FRONT_EXPANDER_PID2	"LSI-SAS3x40"
3686 #define	SMCI4U36_FRONT_EXPANDER_PID3	"SMC-SC846P"
3687 
3688 #define	SMCI4U36_REAR_EXPANDER_PID1	"LSI-CORP-SAS2X28"
3689 #define	SMCI4U36_REAR_EXPANDER_PID2	"LSI-SAS3x28"
3690 
3691 static int
3692 smci_4u36_bay_label(topo_mod_t *mod, tnode_t *node, topo_version_t version,
3693     nvlist_t *in, nvlist_t **out)
3694 {
3695 	int err, ret = -1;
3696 	nvlist_t *pargs, *auth, *nvl = NULL, *fmri;
3697 	char *label = NULL, *product_id;
3698 
3699 	/*
3700 	 * Now look for a private argument list to determine if the invoker is
3701 	 * trying to do a set operation and if so, return an error as this
3702 	 * method only supports get operations.
3703 	 */
3704 	if ((nvlist_lookup_nvlist(in, TOPO_PROP_PARGS, &pargs) == 0) &&
3705 	    nvlist_exists(pargs, TOPO_PROP_VAL_VAL)) {
3706 		topo_mod_dprintf(mod, "%s: set operation not suppported",
3707 		    __func__);
3708 		return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
3709 	}
3710 
3711 	if (topo_node_resource(node, &fmri, &err) != 0) {
3712 		(void) topo_mod_seterrno(mod, err);
3713 		goto err;
3714 	}
3715 
3716 	if (nvlist_lookup_nvlist(fmri, FM_FMRI_AUTHORITY, &auth) != 0 ||
3717 	    nvlist_lookup_string(auth, FM_FMRI_AUTH_PRODUCT, &product_id) !=
3718 	    0) {
3719 		topo_mod_dprintf(mod, "%s: malformed FMRI", __func__);
3720 		(void) topo_mod_seterrno(mod, EMOD_UNKNOWN);
3721 		nvlist_free(fmri);
3722 		goto err;
3723 	}
3724 	nvlist_free(fmri);
3725 
3726 	if (strcmp(product_id, SMCI4U36_FRONT_EXPANDER_PID1) == 0 ||
3727 	    strcmp(product_id, SMCI4U36_FRONT_EXPANDER_PID2) == 0 ||
3728 	    strcmp(product_id, SMCI4U36_FRONT_EXPANDER_PID3) == 0) {
3729 		err = asprintf(&label, "Front Slot %" PRIu64,
3730 		    topo_node_instance(node));
3731 	} else if (strcmp(product_id, SMCI4U36_REAR_EXPANDER_PID1) == 0 ||
3732 	    strcmp(product_id, SMCI4U36_REAR_EXPANDER_PID2) == 0) {
3733 		err = asprintf(&label, "Rear Slot %" PRIu64,
3734 		    topo_node_instance(node));
3735 	} else {
3736 		topo_mod_dprintf(mod, "%s: unexpected expander product id: %s",
3737 		    __func__, product_id);
3738 		(void) topo_mod_seterrno(mod, EMOD_UNKNOWN);
3739 		goto err;
3740 	}
3741 
3742 	if (err < 0) {
3743 		(void) topo_mod_seterrno(mod, EMOD_NOMEM);
3744 		goto err;
3745 	}
3746 
3747 	if (topo_mod_nvalloc(mod, &nvl, NV_UNIQUE_NAME) != 0 ||
3748 	    nvlist_add_string(nvl, TOPO_PROP_VAL_NAME, TOPO_PROP_LABEL) != 0 ||
3749 	    nvlist_add_uint32(nvl, TOPO_PROP_VAL_TYPE, TOPO_TYPE_STRING)
3750 	    != 0 ||
3751 	    nvlist_add_string(nvl, TOPO_PROP_VAL_VAL, label)
3752 	    != 0) {
3753 		topo_mod_dprintf(mod, "Failed to allocate 'out' nvlist");
3754 		nvlist_free(nvl);
3755 		(void) topo_mod_seterrno(mod, EMOD_NOMEM);
3756 		goto err;
3757 	}
3758 	*out = nvl;
3759 	ret = 0;
3760 err:
3761 	free(label);
3762 	return (ret);
3763 
3764 }
3765 
3766 static void
3767 ses_release(topo_mod_t *mod, tnode_t *tn)
3768 {
3769 	ses_enum_target_t *stp;
3770 
3771 	if ((stp = topo_node_getspecific(tn)) != NULL) {
3772 		topo_node_setspecific(tn, NULL);
3773 		ses_target_free(mod, stp);
3774 	}
3775 }
3776 
3777 /*ARGSUSED*/
3778 static int
3779 ses_enum(topo_mod_t *mod, tnode_t *rnode, const char *name,
3780     topo_instance_t min, topo_instance_t max, void *arg, void *notused)
3781 {
3782 	ses_enum_chassis_t *cp;
3783 	ses_enum_data_t *data;
3784 
3785 	/*
3786 	 * Check to make sure we're being invoked sensibly, and that we're not
3787 	 * being invoked as part of a post-processing step.
3788 	 */
3789 	if (strcmp(name, SES_ENCLOSURE) != 0 && strcmp(name, BAY) != 0)
3790 		return (0);
3791 
3792 	/*
3793 	 * If this is the first time we've called our enumeration method, then
3794 	 * gather information about any available enclosures.
3795 	 */
3796 	if ((data = topo_mod_getspecific(mod)) == NULL) {
3797 		ses_sof_freeall(mod);
3798 		if ((data = topo_mod_zalloc(mod, sizeof (ses_enum_data_t))) ==
3799 		    NULL)
3800 			return (-1);
3801 
3802 		data->sed_mod = mod;
3803 		topo_mod_setspecific(mod, data);
3804 
3805 		if (dev_list_gather(mod, &data->sed_devs) != 0)
3806 			goto error;
3807 
3808 		/*
3809 		 * We search both the ses(4D) and sgen(4D) locations, so we are
3810 		 * independent of any particular driver class bindings.
3811 		 */
3812 		if (ses_process_dir("/dev/es", data) != 0 ||
3813 		    ses_process_dir("/dev/scsi/ses", data) != 0)
3814 			goto error;
3815 	}
3816 
3817 	if (strcmp(name, SES_ENCLOSURE) == 0) {
3818 		/*
3819 		 * This is a request to enumerate external enclosures.  Go
3820 		 * through all the targets and create chassis nodes where
3821 		 * necessary.
3822 		 */
3823 		for (cp = topo_list_next(&data->sed_chassis); cp != NULL;
3824 		    cp = topo_list_next(cp)) {
3825 			if (ses_create_chassis(data, rnode, cp) != 0)
3826 				goto error;
3827 		}
3828 	} else {
3829 		/*
3830 		 * This is a request to enumerate a specific bay underneath the
3831 		 * root chassis (for internal disks).
3832 		 */
3833 		if (ses_create_bays(data, rnode) != 0)
3834 			goto error;
3835 	}
3836 
3837 	/*
3838 	 * This is a bit of a kludge.  In order to allow internal disks to be
3839 	 * enumerated and share snapshot-specific information with the external
3840 	 * enclosure enumeration, we rely on the fact that we will be invoked
3841 	 * for the 'ses-enclosure' node last.
3842 	 */
3843 	if (strcmp(name, SES_ENCLOSURE) == 0) {
3844 		for (cp = topo_list_next(&data->sed_chassis); cp != NULL;
3845 		    cp = topo_list_next(cp))
3846 			ses_data_free(data, cp);
3847 		ses_data_free(data, NULL);
3848 		topo_mod_setspecific(mod, NULL);
3849 	}
3850 	return (0);
3851 
3852 error:
3853 	for (cp = topo_list_next(&data->sed_chassis); cp != NULL;
3854 	    cp = topo_list_next(cp))
3855 		ses_data_free(data, cp);
3856 	ses_data_free(data, NULL);
3857 	topo_mod_setspecific(mod, NULL);
3858 	return (-1);
3859 }
3860 
3861 static const topo_modops_t ses_ops =
3862 	{ ses_enum, ses_release };
3863 
3864 static topo_modinfo_t ses_info =
3865 	{ SES_ENCLOSURE, FM_FMRI_SCHEME_HC, SES_VERSION, &ses_ops };
3866 
3867 /*ARGSUSED*/
3868 int
3869 _topo_init(topo_mod_t *mod, topo_version_t version)
3870 {
3871 	int rval;
3872 
3873 	if (getenv("TOPOSESDEBUG") != NULL)
3874 		topo_mod_setdebug(mod);
3875 
3876 	topo_mod_dprintf(mod, "initializing %s enumerator\n",
3877 	    SES_ENCLOSURE);
3878 
3879 	if ((rval = topo_mod_register(mod, &ses_info, TOPO_VERSION)) == 0)
3880 		ses_thread_init(mod);
3881 
3882 	return (rval);
3883 }
3884 
3885 void
3886 _topo_fini(topo_mod_t *mod)
3887 {
3888 	ses_thread_fini(mod);
3889 	ses_sof_freeall(mod);
3890 	topo_mod_unregister(mod);
3891 }
3892