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