1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved. 24 */ 25 26 /* 27 * Functions in this file are shared between the disk and ses enumerators. 28 * 29 * A topo_list_t of all disks is returned by a successful disk_list_gather() 30 * call, and the list is freed by a disk_list_free(). To create a 'disk' topo 31 * node below a specific 'bay' parent node either disk_declare_path() or 32 * disk_declare_addr() are called. The caller determines which 'disk' is 33 * in which 'bay'. A disk's 'label' and 'authority' information come from 34 * its parent 'bay' node. 35 */ 36 37 #include <ctype.h> 38 #include <strings.h> 39 #include <libdevinfo.h> 40 #include <devid.h> 41 #include <sys/libdevid.h> 42 #include <pthread.h> 43 #include <inttypes.h> 44 #include <sys/dkio.h> 45 #include <sys/scsi/scsi_types.h> 46 #include <fm/topo_mod.h> 47 #include <fm/topo_list.h> 48 #include <fm/libdiskstatus.h> 49 #include <sys/fm/protocol.h> 50 #include <sys/scsi/generic/inquiry.h> 51 #include "disk.h" 52 53 /* common callback information for di_walk_node() and di_devlink_walk */ 54 typedef struct disk_cbdata { 55 topo_mod_t *dcb_mod; 56 topo_list_t *dcb_list; 57 58 di_devlink_handle_t dcb_devhdl; 59 dev_di_node_t *dcb_dnode; /* for di_devlink_walk only */ 60 } disk_cbdata_t; 61 62 /* 63 * Given a /devices path for a whole disk, appending this extension gives the 64 * path to a raw device that can be opened. 65 */ 66 #if defined(__i386) || defined(__amd64) 67 #define PHYS_EXTN ":q,raw" 68 #elif defined(__sparc) || defined(__sparcv9) 69 #define PHYS_EXTN ":c,raw" 70 #else 71 #error Unknown architecture 72 #endif 73 74 /* 75 * Methods for disks. This is used by the disk-transport module to 76 * generate ereports based off SCSI disk status. 77 */ 78 static int disk_status(topo_mod_t *, tnode_t *, topo_version_t, 79 nvlist_t *, nvlist_t **); 80 81 static const topo_method_t disk_methods[] = { 82 { TOPO_METH_DISK_STATUS, TOPO_METH_DISK_STATUS_DESC, 83 TOPO_METH_DISK_STATUS_VERSION, TOPO_STABILITY_INTERNAL, 84 disk_status }, 85 { NULL } 86 }; 87 88 static const topo_pgroup_info_t io_pgroup = { 89 TOPO_PGROUP_IO, 90 TOPO_STABILITY_PRIVATE, 91 TOPO_STABILITY_PRIVATE, 92 1 93 }; 94 95 static const topo_pgroup_info_t disk_auth_pgroup = { 96 FM_FMRI_AUTHORITY, 97 TOPO_STABILITY_PRIVATE, 98 TOPO_STABILITY_PRIVATE, 99 1 100 }; 101 102 static const topo_pgroup_info_t storage_pgroup = { 103 TOPO_PGROUP_STORAGE, 104 TOPO_STABILITY_PRIVATE, 105 TOPO_STABILITY_PRIVATE, 106 1 107 }; 108 109 /* 110 * Set the properties of the disk node, from dev_di_node_t data. 111 * Properties include: 112 * group: protocol properties: resource, asru, label, fru 113 * group: authority properties: product-id, chasis-id, server-id 114 * group: io properties: devfs-path, devid 115 * group: storage properties: 116 * - logical-disk, disk-model, disk-manufacturer, serial-number 117 * - firmware-revision, capacity-in-bytes 118 * 119 * NOTE: the io and storage groups won't be present if the dnode passed in is 120 * NULL. This happens when a disk is found through ses, but is not enumerated 121 * in the devinfo tree. 122 */ 123 static int 124 disk_set_props(topo_mod_t *mod, tnode_t *parent, 125 tnode_t *dtn, dev_di_node_t *dnode) 126 { 127 nvlist_t *asru = NULL; 128 char *label = NULL; 129 nvlist_t *fmri = NULL; 130 int err; 131 132 /* pull the label property down from our parent 'bay' node */ 133 if (topo_node_label(parent, &label, &err) != 0) { 134 topo_mod_dprintf(mod, "disk_set_props: " 135 "label error %s\n", topo_strerror(err)); 136 goto error; 137 } 138 if (topo_node_label_set(dtn, label, &err) != 0) { 139 topo_mod_dprintf(mod, "disk_set_props: " 140 "label_set error %s\n", topo_strerror(err)); 141 goto error; 142 } 143 144 /* get the resource fmri, and use it as the fru */ 145 if (topo_node_resource(dtn, &fmri, &err) != 0) { 146 topo_mod_dprintf(mod, "disk_set_props: " 147 "resource error: %s\n", topo_strerror(err)); 148 goto error; 149 } 150 if (topo_node_fru_set(dtn, fmri, 0, &err) != 0) { 151 topo_mod_dprintf(mod, "disk_set_props: " 152 "fru_set error: %s\n", topo_strerror(err)); 153 goto error; 154 } 155 156 /* create/set the authority group */ 157 if ((topo_pgroup_create(dtn, &disk_auth_pgroup, &err) != 0) && 158 (err != ETOPO_PROP_DEFD)) { 159 topo_mod_dprintf(mod, "disk_set_props: " 160 "create disk_auth error %s\n", topo_strerror(err)); 161 goto error; 162 } 163 164 /* create the storage group */ 165 if (topo_pgroup_create(dtn, &storage_pgroup, &err) != 0) { 166 topo_mod_dprintf(mod, "disk_set_props: " 167 "create storage error %s\n", topo_strerror(err)); 168 goto error; 169 } 170 171 /* no dnode was found for this disk - skip the io and storage groups */ 172 if (dnode == NULL) { 173 err = 0; 174 goto out; 175 } 176 177 /* form and set the asru */ 178 if ((asru = topo_mod_devfmri(mod, FM_DEV_SCHEME_VERSION, 179 dnode->ddn_dpath, dnode->ddn_devid)) == NULL) { 180 err = ETOPO_FMRI_UNKNOWN; 181 topo_mod_dprintf(mod, "disk_set_props: " 182 "asru error %s\n", topo_strerror(err)); 183 goto error; 184 } 185 if (topo_node_asru_set(dtn, asru, 0, &err) != 0) { 186 topo_mod_dprintf(mod, "disk_set_props: " 187 "asru_set error %s\n", topo_strerror(err)); 188 goto error; 189 } 190 191 /* create/set the devfs-path and devid in the io group */ 192 if (topo_pgroup_create(dtn, &io_pgroup, &err) != 0) { 193 topo_mod_dprintf(mod, "disk_set_props: " 194 "create io error %s\n", topo_strerror(err)); 195 goto error; 196 } 197 198 if (topo_prop_set_string(dtn, TOPO_PGROUP_IO, TOPO_IO_DEV_PATH, 199 TOPO_PROP_IMMUTABLE, dnode->ddn_dpath, &err) != 0) { 200 topo_mod_dprintf(mod, "disk_set_props: " 201 "set dev error %s\n", topo_strerror(err)); 202 goto error; 203 } 204 205 if (dnode->ddn_devid && topo_prop_set_string(dtn, TOPO_PGROUP_IO, 206 TOPO_IO_DEVID, TOPO_PROP_IMMUTABLE, dnode->ddn_devid, &err) != 0) { 207 topo_mod_dprintf(mod, "disk_set_props: " 208 "set devid error %s\n", topo_strerror(err)); 209 goto error; 210 } 211 212 if (dnode->ddn_ppath_count != 0 && 213 topo_prop_set_string_array(dtn, TOPO_PGROUP_IO, TOPO_IO_PHYS_PATH, 214 TOPO_PROP_IMMUTABLE, (const char **)dnode->ddn_ppath, 215 dnode->ddn_ppath_count, &err) != 0) { 216 topo_mod_dprintf(mod, "disk_set_props: " 217 "set phys-path error %s\n", topo_strerror(err)); 218 goto error; 219 } 220 221 /* set the storage group public /dev name */ 222 if (dnode->ddn_lpath != NULL && 223 topo_prop_set_string(dtn, TOPO_PGROUP_STORAGE, 224 TOPO_STORAGE_LOGICAL_DISK_NAME, TOPO_PROP_IMMUTABLE, 225 dnode->ddn_lpath, &err) != 0) { 226 topo_mod_dprintf(mod, "disk_set_props: " 227 "set disk_name error %s\n", topo_strerror(err)); 228 goto error; 229 } 230 231 /* populate other misc storage group properties */ 232 if (dnode->ddn_mfg && (topo_prop_set_string(dtn, TOPO_PGROUP_STORAGE, 233 TOPO_STORAGE_MANUFACTURER, TOPO_PROP_IMMUTABLE, 234 dnode->ddn_mfg, &err) != 0)) { 235 topo_mod_dprintf(mod, "disk_set_props: " 236 "set mfg error %s\n", topo_strerror(err)); 237 goto error; 238 } 239 if (dnode->ddn_model && (topo_prop_set_string(dtn, TOPO_PGROUP_STORAGE, 240 TOPO_STORAGE_MODEL, TOPO_PROP_IMMUTABLE, 241 dnode->ddn_model, &err) != 0)) { 242 topo_mod_dprintf(mod, "disk_set_props: " 243 "set model error %s\n", topo_strerror(err)); 244 goto error; 245 } 246 if (dnode->ddn_serial && (topo_prop_set_string(dtn, TOPO_PGROUP_STORAGE, 247 TOPO_STORAGE_SERIAL_NUM, TOPO_PROP_IMMUTABLE, 248 dnode->ddn_serial, &err) != 0)) { 249 topo_mod_dprintf(mod, "disk_set_props: " 250 "set serial error %s\n", topo_strerror(err)); 251 goto error; 252 } 253 if (dnode->ddn_firm && (topo_prop_set_string(dtn, TOPO_PGROUP_STORAGE, 254 TOPO_STORAGE_FIRMWARE_REV, TOPO_PROP_IMMUTABLE, 255 dnode->ddn_firm, &err) != 0)) { 256 topo_mod_dprintf(mod, "disk_set_props: " 257 "set firm error %s\n", topo_strerror(err)); 258 goto error; 259 } 260 if (dnode->ddn_cap && (topo_prop_set_string(dtn, TOPO_PGROUP_STORAGE, 261 TOPO_STORAGE_CAPACITY, TOPO_PROP_IMMUTABLE, 262 dnode->ddn_cap, &err) != 0)) { 263 topo_mod_dprintf(mod, "disk_set_props: " 264 "set cap error %s\n", topo_strerror(err)); 265 goto error; 266 } 267 err = 0; 268 269 out: if (fmri) 270 nvlist_free(fmri); 271 if (label) 272 topo_mod_strfree(mod, label); 273 if (asru) 274 nvlist_free(asru); 275 return (err); 276 277 error: err = topo_mod_seterrno(mod, err); 278 goto out; 279 } 280 281 /* 282 * Trim leading and trailing whitespace from the string. 283 */ 284 static char * 285 disk_trim_whitespace(topo_mod_t *mod, const char *begin) 286 { 287 const char *end; 288 char *buf; 289 size_t count; 290 291 if (begin == NULL) 292 return (NULL); 293 294 end = begin + strlen(begin); 295 296 while (begin < end && isspace(*begin)) 297 begin++; 298 while (begin < end && isspace(*(end - 1))) 299 end--; 300 301 count = end - begin; 302 if ((buf = topo_mod_alloc(mod, count + 1)) == NULL) 303 return (NULL); 304 305 (void) strlcpy(buf, begin, count + 1); 306 307 return (buf); 308 } 309 310 /* 311 * Manufacturing strings can contain characters that are invalid for use in hc 312 * authority names. This trims leading and trailing whitespace, and 313 * substitutes any characters known to be bad. 314 */ 315 char * 316 disk_auth_clean(topo_mod_t *mod, const char *str) 317 { 318 char *buf, *p; 319 320 if (str == NULL) 321 return (NULL); 322 323 if ((buf = topo_mod_strdup(mod, str)) == NULL) 324 return (NULL); 325 326 while ((p = strpbrk(buf, " :=")) != NULL) 327 *p = '-'; 328 329 return (buf); 330 } 331 332 /* create the disk topo node */ 333 static int 334 disk_tnode_create(topo_mod_t *mod, tnode_t *parent, 335 dev_di_node_t *dnode, const char *name, topo_instance_t i, tnode_t **rval) 336 { 337 int len; 338 nvlist_t *fmri; 339 tnode_t *dtn; 340 char *part = NULL; 341 nvlist_t *auth; 342 char *mfg, *model, *firm, *serial; 343 344 *rval = NULL; 345 if (dnode != NULL) { 346 mfg = disk_auth_clean(mod, dnode->ddn_mfg); 347 model = disk_auth_clean(mod, dnode->ddn_model); 348 firm = disk_auth_clean(mod, dnode->ddn_firm); 349 serial = disk_auth_clean(mod, dnode->ddn_serial); 350 } else { 351 mfg = model = firm = serial = NULL; 352 } 353 354 /* form 'part=' of fmri as "<mfg>-<model>" */ 355 if (mfg != NULL && model != NULL) { 356 len = strlen(mfg) + 1 + strlen(model) + 1; 357 if ((part = topo_mod_alloc(mod, len)) != NULL) 358 (void) snprintf(part, len, "%s-%s", 359 mfg, model); 360 } 361 362 auth = topo_mod_auth(mod, parent); 363 fmri = topo_mod_hcfmri(mod, parent, FM_HC_SCHEME_VERSION, name, i, NULL, 364 auth, part ? part : model, firm, serial); 365 nvlist_free(auth); 366 367 topo_mod_strfree(mod, part); 368 topo_mod_strfree(mod, mfg); 369 topo_mod_strfree(mod, model); 370 topo_mod_strfree(mod, firm); 371 topo_mod_strfree(mod, serial); 372 373 if (fmri == NULL) { 374 topo_mod_dprintf(mod, "disk_tnode_create: " 375 "hcfmri (%s%d/%s%d) error %s\n", 376 topo_node_name(parent), topo_node_instance(parent), 377 name, i, topo_strerror(topo_mod_errno(mod))); 378 return (-1); 379 } 380 381 if ((dtn = topo_node_bind(mod, parent, name, i, fmri)) == NULL) { 382 if (topo_mod_errno(mod) == EMOD_NODE_BOUND) { 383 /* 384 * if disk 0 is already there then we're done 385 */ 386 nvlist_free(fmri); 387 return (0); 388 } 389 topo_mod_dprintf(mod, "disk_tnode_create: " 390 "bind (%s%d/%s%d) error %s\n", 391 topo_node_name(parent), topo_node_instance(parent), 392 name, i, topo_strerror(topo_mod_errno(mod))); 393 nvlist_free(fmri); 394 return (-1); 395 } 396 nvlist_free(fmri); 397 398 /* add the properties of the disk */ 399 if (disk_set_props(mod, parent, dtn, dnode) != 0) { 400 topo_mod_dprintf(mod, "disk_tnode_create: " 401 "disk_set_props (%s%d/%s%d) error %s\n", 402 topo_node_name(parent), topo_node_instance(parent), 403 name, i, topo_strerror(topo_mod_errno(mod))); 404 topo_node_unbind(dtn); 405 return (-1); 406 } 407 *rval = dtn; 408 return (0); 409 } 410 411 static int 412 disk_declare(topo_mod_t *mod, tnode_t *parent, dev_di_node_t *dnode, 413 tnode_t **childp) 414 { 415 tnode_t *dtn = NULL; 416 int rval; 417 418 rval = disk_tnode_create(mod, parent, dnode, DISK, 0, &dtn); 419 if (dtn == NULL) { 420 if (rval == 0) 421 return (0); 422 topo_mod_dprintf(mod, "disk_declare: " 423 "disk_tnode_create error %s\n", 424 topo_strerror(topo_mod_errno(mod))); 425 return (-1); 426 } 427 428 /* register disk_methods against the disk topo node */ 429 if (topo_method_register(mod, dtn, disk_methods) != 0) { 430 topo_mod_dprintf(mod, "disk_declare: " 431 "topo_method_register error %s\n", 432 topo_strerror(topo_mod_errno(mod))); 433 topo_node_unbind(dtn); 434 return (-1); 435 } 436 if (childp != NULL) 437 *childp = dtn; 438 return (0); 439 } 440 441 int 442 disk_declare_path(topo_mod_t *mod, tnode_t *parent, topo_list_t *listp, 443 const char *path) 444 { 445 dev_di_node_t *dnode; 446 int i; 447 448 /* 449 * Check for match using physical phci (ddn_ppath). Use 450 * di_devfs_path_match so generic.vs.non-generic names match. 451 */ 452 for (dnode = topo_list_next(listp); dnode != NULL; 453 dnode = topo_list_next(dnode)) { 454 if (dnode->ddn_ppath == NULL) 455 continue; 456 457 for (i = 0; i < dnode->ddn_ppath_count; i++) { 458 if (di_devfs_path_match(dnode->ddn_ppath[0], path)) 459 return (disk_declare(mod, parent, dnode, NULL)); 460 } 461 } 462 463 topo_mod_dprintf(mod, "disk_declare_path: " 464 "failed to find disk matching path %s", path); 465 return (0); 466 } 467 468 int 469 disk_declare_addr(topo_mod_t *mod, tnode_t *parent, topo_list_t *listp, 470 const char *addr, tnode_t **childp) 471 { 472 dev_di_node_t *dnode; 473 int i; 474 475 /* Check for match using addr. */ 476 for (dnode = topo_list_next(listp); dnode != NULL; 477 dnode = topo_list_next(dnode)) { 478 if (dnode->ddn_target_port == NULL) 479 continue; 480 481 for (i = 0; i < dnode->ddn_target_port_count; i++) { 482 if (strncmp(dnode->ddn_target_port[i], addr, 483 strcspn(dnode->ddn_target_port[i], ":")) == 0) 484 return (disk_declare(mod, parent, dnode, 485 childp)); 486 } 487 } 488 489 topo_mod_dprintf(mod, "disk_declare_addr: " 490 "failed to find disk matching addr %s", addr); 491 492 return (1); 493 } 494 495 /* 496 * Used to declare a disk that has been discovered through other means (usually 497 * ses), that is not enumerated in the devinfo tree. 498 */ 499 int 500 disk_declare_non_enumerated(topo_mod_t *mod, tnode_t *parent, tnode_t **childp) 501 { 502 return (disk_declare(mod, parent, NULL, childp)); 503 } 504 505 /* di_devlink callback for dev_di_node_add */ 506 static int 507 disk_devlink_callback(di_devlink_t dl, void *arg) 508 { 509 disk_cbdata_t *cbp = (disk_cbdata_t *)arg; 510 topo_mod_t *mod = cbp->dcb_mod; 511 dev_di_node_t *dnode = cbp->dcb_dnode; 512 const char *devpath; 513 char *ctds, *slice; 514 515 devpath = di_devlink_path(dl); 516 if ((dnode == NULL) || (devpath == NULL)) 517 return (DI_WALK_TERMINATE); 518 519 /* trim the slice off the public name */ 520 if (((ctds = strrchr(devpath, '/')) != NULL) && 521 ((slice = strchr(ctds, 's')) != NULL)) 522 *slice = '\0'; 523 524 /* Establish the public /dev name (no slice) */ 525 dnode->ddn_lpath = topo_mod_strdup(mod, ctds ? ctds + 1 : devpath); 526 527 if (ctds && slice) 528 *slice = 's'; 529 return (DI_WALK_TERMINATE); 530 } 531 532 static void 533 dev_di_node_free(topo_mod_t *mod, dev_di_node_t *dnode) 534 { 535 int i; 536 537 /* free the stuff we point to */ 538 if (dnode->ddn_devid) 539 topo_mod_strfree(mod, dnode->ddn_devid); 540 for (i = 0; i < dnode->ddn_ppath_count; i++) 541 topo_mod_strfree(mod, dnode->ddn_ppath[i]); 542 topo_mod_free(mod, dnode->ddn_ppath, 543 dnode->ddn_ppath_count * sizeof (uintptr_t)); 544 topo_mod_strfree(mod, dnode->ddn_dpath); 545 topo_mod_strfree(mod, dnode->ddn_lpath); 546 547 topo_mod_strfree(mod, dnode->ddn_mfg); 548 topo_mod_strfree(mod, dnode->ddn_model); 549 topo_mod_strfree(mod, dnode->ddn_serial); 550 topo_mod_strfree(mod, dnode->ddn_firm); 551 topo_mod_strfree(mod, dnode->ddn_cap); 552 553 for (i = 0; i < dnode->ddn_target_port_count; i++) 554 topo_mod_strfree(mod, dnode->ddn_target_port[i]); 555 topo_mod_free(mod, dnode->ddn_target_port, 556 dnode->ddn_target_port_count * sizeof (uintptr_t)); 557 558 /* free self */ 559 topo_mod_free(mod, dnode, sizeof (dev_di_node_t)); 560 } 561 562 static int 563 dev_di_node_add(di_node_t node, char *devid, disk_cbdata_t *cbp) 564 { 565 topo_mod_t *mod = cbp->dcb_mod; 566 dev_di_node_t *dnode; 567 di_path_t pnode; 568 char *path; 569 int mlen; 570 char *minorpath; 571 char *extn = ":a"; 572 char *s; 573 int64_t *nblocksp; 574 uint64_t nblocks; 575 int *dblksizep; 576 uint_t dblksize; 577 char lentry[MAXPATHLEN]; 578 int pathcount, portcount; 579 int *inq_dtype, itype; 580 int ret, i; 581 582 if (devid) { 583 /* 584 * Check for list duplicate using devid search. 585 * Note if there is no devid, then we can end up with duplicates 586 * in the list, but this doesn't do any harm. 587 */ 588 for (dnode = topo_list_next(cbp->dcb_list); 589 dnode != NULL; dnode = topo_list_next(dnode)) { 590 if (dnode->ddn_devid && 591 devid_str_compare(dnode->ddn_devid, devid) == 0) { 592 topo_mod_dprintf(mod, "dev_di_node_add: " 593 "already there %s\n", devid); 594 return (0); 595 } 596 } 597 } 598 599 if ((dnode = topo_mod_zalloc(mod, sizeof (dev_di_node_t))) == NULL) 600 return (-1); 601 602 if (devid) { 603 /* Establish the devid. */ 604 dnode->ddn_devid = topo_mod_strdup(mod, devid); 605 if (dnode->ddn_devid == NULL) 606 goto error; 607 } 608 609 /* Establish the devinfo dpath */ 610 if ((path = di_devfs_path(node)) == NULL) { 611 (void) topo_mod_seterrno(mod, errno); 612 goto error; 613 } 614 615 dnode->ddn_dpath = topo_mod_strdup(mod, path); 616 di_devfs_path_free(path); 617 if (dnode->ddn_dpath == NULL) 618 goto error; 619 620 /* 621 * Establish the physical ppath and target ports. If the device is 622 * non-mpxio then dpath and ppath are the same, and the target port is a 623 * property of the device node. 624 * 625 * If dpath is a client node under scsi_vhci, then iterate over all 626 * paths and get their physical paths and target port properrties. 627 * di_path_client_next_path call below will 628 * return non-NULL, and ppath is set to the physical path to the first 629 * pathinfo node. 630 * 631 * NOTE: It is possible to get a generic.vs.non-generic path 632 * for di_devfs_path.vs.di_path_devfs_path like: 633 * xml: /pci@7b,0/pci1022,7458@11/pci1000,3060@2/sd@2,0 634 * pnode: /pci@7b,0/pci1022,7458@11/pci1000,3060@2/disk@2,0 635 * To resolve this issue disk_declare_path() needs to use the 636 * special di_devfs_path_match() interface. 637 */ 638 pathcount = portcount = 0; 639 pnode = NULL; 640 while ((pnode = di_path_client_next_path(node, pnode)) != NULL) { 641 if ((ret = di_path_prop_lookup_strings(pnode, 642 SCSI_ADDR_PROP_TARGET_PORT, &s)) > 0) 643 portcount += ret; 644 pathcount++; 645 } 646 647 if (pathcount == 0) { 648 if ((dnode->ddn_ppath = 649 topo_mod_zalloc(mod, sizeof (uintptr_t))) == NULL) 650 goto error; 651 652 dnode->ddn_ppath_count = 1; 653 if ((dnode->ddn_ppath[0] = topo_mod_strdup(mod, 654 dnode->ddn_dpath)) == NULL) 655 goto error; 656 657 if ((ret = di_prop_lookup_strings(DDI_DEV_T_ANY, node, 658 SCSI_ADDR_PROP_TARGET_PORT, &s)) > 0) { 659 if ((dnode->ddn_target_port = topo_mod_zalloc(mod, 660 ret * sizeof (uintptr_t))) == NULL) 661 goto error; 662 dnode->ddn_target_port_count = ret; 663 664 for (i = 0; i < ret; i++) { 665 if ((dnode->ddn_target_port[i] = 666 topo_mod_strdup(mod, 667 scsi_wwnstr_skip_ua_prefix(s))) == 668 NULL) 669 goto error; 670 671 s += strlen(s) + 1; 672 } 673 } 674 } else { 675 if ((dnode->ddn_ppath = topo_mod_zalloc(mod, 676 pathcount * sizeof (uintptr_t))) == NULL) 677 goto error; 678 679 dnode->ddn_ppath_count = pathcount; 680 681 if (portcount != 0 && 682 ((dnode->ddn_target_port = topo_mod_zalloc(mod, 683 portcount * sizeof (uintptr_t)))) == NULL) 684 goto error; 685 686 dnode->ddn_target_port_count = portcount; 687 688 pnode = NULL; 689 pathcount = portcount = 0; 690 while ((pnode = di_path_client_next_path(node, 691 pnode)) != NULL) { 692 if ((path = di_path_devfs_path(pnode)) == NULL) { 693 (void) topo_mod_seterrno(mod, errno); 694 goto error; 695 } 696 697 dnode->ddn_ppath[pathcount] = 698 topo_mod_strdup(mod, path); 699 di_devfs_path_free(path); 700 if (dnode->ddn_ppath[pathcount] == NULL) 701 goto error; 702 703 if ((ret = di_path_prop_lookup_strings(pnode, 704 SCSI_ADDR_PROP_TARGET_PORT, &s)) > 0) { 705 for (i = 0; i < ret; i++) { 706 if ((dnode->ddn_target_port[portcount] = 707 topo_mod_strdup(mod, 708 scsi_wwnstr_skip_ua_prefix(s))) == 709 NULL) 710 goto error; 711 712 portcount++; 713 s += strlen(s) + 1; 714 } 715 } 716 717 pathcount++; 718 } 719 } 720 721 /* 722 * Find the public /dev name for a disk by adding a minor name and using 723 * di_devlink interface for reverse translation (use devinfo path). 724 */ 725 if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, "inquiry-device-type", 726 &inq_dtype) > 0) { 727 itype = (*inq_dtype) & DTYPE_MASK; 728 if (itype == DTYPE_DIRECT) { 729 mlen = strlen(dnode->ddn_dpath) + strlen(extn) + 1; 730 if ((minorpath = topo_mod_alloc(mod, mlen)) == NULL) 731 goto error; 732 (void) snprintf(minorpath, mlen, "%s%s", 733 dnode->ddn_dpath, extn); 734 cbp->dcb_dnode = dnode; 735 (void) di_devlink_walk(cbp->dcb_devhdl, "^dsk/", 736 minorpath, DI_PRIMARY_LINK, cbp, 737 disk_devlink_callback); 738 topo_mod_free(mod, minorpath, mlen); 739 if (dnode->ddn_lpath == NULL) { 740 topo_mod_dprintf(mod, "dev_di_node_add: " 741 "failed to determine logical path"); 742 } 743 } 744 } 745 746 /* cache various bits of optional information about the device. */ 747 if (di_prop_lookup_strings(DDI_DEV_T_ANY, node, 748 INQUIRY_VENDOR_ID, &s) > 0) { 749 if ((dnode->ddn_mfg = disk_trim_whitespace(mod, s)) == NULL) 750 goto error; 751 } 752 if (di_prop_lookup_strings(DDI_DEV_T_ANY, node, 753 INQUIRY_PRODUCT_ID, &s) > 0) { 754 if ((dnode->ddn_model = disk_trim_whitespace(mod, s)) == NULL) 755 goto error; 756 } 757 if (di_prop_lookup_strings(DDI_DEV_T_ANY, node, 758 INQUIRY_REVISION_ID, &s) > 0) { 759 if ((dnode->ddn_firm = disk_trim_whitespace(mod, s)) == NULL) 760 goto error; 761 } 762 if (di_prop_lookup_strings(DDI_DEV_T_ANY, node, 763 INQUIRY_SERIAL_NO, &s) > 0) { 764 if ((dnode->ddn_serial = disk_trim_whitespace(mod, s)) == NULL) 765 goto error; 766 } 767 if (di_prop_lookup_int64(DDI_DEV_T_ANY, node, 768 "device-nblocks", &nblocksp) > 0) { 769 nblocks = (uint64_t)*nblocksp; 770 /* 771 * To save kernel memory, the driver may not define 772 * "device-dblksize" when its value is default DEV_BSIZE. 773 */ 774 if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, 775 "device-dblksize", &dblksizep) > 0) 776 dblksize = (uint_t)*dblksizep; 777 else 778 dblksize = DEV_BSIZE; /* default value */ 779 (void) snprintf(lentry, sizeof (lentry), 780 "%" PRIu64, nblocks * dblksize); 781 if ((dnode->ddn_cap = topo_mod_strdup(mod, lentry)) == NULL) 782 goto error; 783 } 784 785 topo_mod_dprintf(mod, "dev_di_node_add: " 786 "adding %s\n", devid ? dnode->ddn_devid : "NULL devid"); 787 topo_mod_dprintf(mod, " " 788 " %s\n", dnode->ddn_dpath); 789 for (i = 0; i < dnode->ddn_ppath_count; i++) { 790 topo_mod_dprintf(mod, " " 791 " %s\n", dnode->ddn_ppath[i]); 792 } 793 topo_list_append(cbp->dcb_list, dnode); 794 return (0); 795 796 error: 797 dev_di_node_free(mod, dnode); 798 return (-1); 799 } 800 801 802 /* di_walk_node callback for disk_list_gather */ 803 static int 804 dev_walk_di_nodes(di_node_t node, void *arg) 805 { 806 char *devidstr = NULL; 807 char *s; 808 809 /* 810 * if it's not a scsi_vhci client and doesn't have a target_port 811 * or target property then we're not interested 812 */ 813 if (di_path_client_next_path(node, NULL) == NULL && 814 (di_prop_lookup_strings(DDI_DEV_T_ANY, node, 815 SCSI_ADDR_PROP_TARGET_PORT, &s) <= 0 || 816 di_prop_lookup_strings(DDI_DEV_T_ANY, node, 817 SCSI_ADDR_PROP_TARGET, &s) <= 0)) { 818 return (DI_WALK_CONTINUE); 819 } 820 (void) di_prop_lookup_strings(DDI_DEV_T_ANY, node, 821 DEVID_PROP_NAME, &devidstr); 822 823 /* create/find the devid scsi topology node */ 824 (void) dev_di_node_add(node, devidstr, arg); 825 826 return (DI_WALK_CONTINUE); 827 } 828 829 int 830 dev_list_gather(topo_mod_t *mod, topo_list_t *listp) 831 { 832 di_node_t devtree; 833 di_devlink_handle_t devhdl; 834 disk_cbdata_t dcb; 835 836 if ((devtree = topo_mod_devinfo(mod)) == DI_NODE_NIL) { 837 topo_mod_dprintf(mod, "disk_list_gather: " 838 "topo_mod_devinfo() failed"); 839 return (-1); 840 } 841 842 if ((devhdl = di_devlink_init(NULL, 0)) == DI_NODE_NIL) { 843 topo_mod_dprintf(mod, "disk_list_gather: " 844 "di_devlink_init() failed"); 845 return (-1); 846 } 847 848 dcb.dcb_mod = mod; 849 dcb.dcb_list = listp; 850 dcb.dcb_devhdl = devhdl; 851 852 /* walk the devinfo snapshot looking for disk nodes */ 853 (void) di_walk_node(devtree, DI_WALK_CLDFIRST, &dcb, 854 dev_walk_di_nodes); 855 856 (void) di_devlink_fini(&devhdl); 857 858 return (0); 859 } 860 861 void 862 dev_list_free(topo_mod_t *mod, topo_list_t *listp) 863 { 864 dev_di_node_t *dnode; 865 866 while ((dnode = topo_list_next(listp)) != NULL) { 867 /* order of delete/free is important */ 868 topo_list_delete(listp, dnode); 869 dev_di_node_free(mod, dnode); 870 } 871 } 872 873 /* 874 * Query the current disk status. If successful, the disk status is returned 875 * as an nvlist consisting of at least the following members: 876 * 877 * protocol string Supported protocol (currently "scsi") 878 * 879 * status nvlist Arbitrary protocol-specific information 880 * about the current state of the disk. 881 * 882 * faults nvlist A list of supported faults. Each 883 * element of this list is a boolean value. 884 * An element's existence indicates that 885 * the drive supports detecting this fault, 886 * and the value indicates the current 887 * state of the fault. 888 * 889 * <fault-name> nvlist For each fault named in 'faults', a 890 * nvlist describing protocol-specific 891 * attributes of the fault. 892 * 893 * This method relies on the libdiskstatus library to query this information. 894 */ 895 static int 896 disk_status(topo_mod_t *mod, tnode_t *nodep, topo_version_t vers, 897 nvlist_t *in_nvl, nvlist_t **out_nvl) 898 { 899 disk_status_t *dsp; 900 char *devpath, *fullpath; 901 size_t pathlen; 902 nvlist_t *status; 903 int err; 904 905 *out_nvl = NULL; 906 907 if (vers != TOPO_METH_DISK_STATUS_VERSION) 908 return (topo_mod_seterrno(mod, EMOD_VER_NEW)); 909 910 /* 911 * If the caller specifies the "path" parameter, then this indicates 912 * that we should use this instead of deriving it from the topo node 913 * itself. 914 */ 915 if (nvlist_lookup_string(in_nvl, "path", &fullpath) == 0) { 916 devpath = NULL; 917 } else { 918 /* 919 * Get the /devices path and attempt to open the disk status 920 * handle. 921 */ 922 if (topo_prop_get_string(nodep, TOPO_PGROUP_IO, 923 TOPO_IO_DEV_PATH, &devpath, &err) != 0) 924 return (topo_mod_seterrno(mod, EMOD_METHOD_NOTSUP)); 925 926 /* 927 * Note that sizeof(string) includes the terminating NULL byte 928 */ 929 pathlen = strlen(devpath) + sizeof ("/devices") + 930 sizeof (PHYS_EXTN) - 1; 931 932 if ((fullpath = topo_mod_alloc(mod, pathlen)) == NULL) 933 return (topo_mod_seterrno(mod, EMOD_NOMEM)); 934 935 (void) snprintf(fullpath, pathlen, "/devices%s%s", devpath, 936 PHYS_EXTN); 937 938 topo_mod_strfree(mod, devpath); 939 } 940 941 if ((dsp = disk_status_open(fullpath, &err)) == NULL) { 942 if (devpath) 943 topo_mod_free(mod, fullpath, pathlen); 944 return (topo_mod_seterrno(mod, err == EDS_NOMEM ? 945 EMOD_NOMEM : EMOD_METHOD_NOTSUP)); 946 } 947 948 if (devpath) 949 topo_mod_free(mod, fullpath, pathlen); 950 951 if ((status = disk_status_get(dsp)) == NULL) { 952 err = (disk_status_errno(dsp) == EDS_NOMEM ? 953 EMOD_NOMEM : EMOD_METHOD_NOTSUP); 954 disk_status_close(dsp); 955 return (topo_mod_seterrno(mod, err)); 956 } 957 958 *out_nvl = status; 959 disk_status_close(dsp); 960 return (0); 961 } 962