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