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