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