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