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 topo_mod_dprintf(mod, "disk_declare_addr: " 485 "found disk matching addr %s", addr); 486 return (disk_declare(mod, parent, dnode, 487 childp)); 488 } 489 } 490 } 491 492 topo_mod_dprintf(mod, "disk_declare_addr: " 493 "failed to find disk matching addr %s", addr); 494 495 return (1); 496 } 497 498 /* 499 * Used to declare a disk that has been discovered through other means (usually 500 * ses), that is not enumerated in the devinfo tree. 501 */ 502 int 503 disk_declare_non_enumerated(topo_mod_t *mod, tnode_t *parent, tnode_t **childp) 504 { 505 return (disk_declare(mod, parent, NULL, childp)); 506 } 507 508 /* di_devlink callback for dev_di_node_add */ 509 static int 510 disk_devlink_callback(di_devlink_t dl, void *arg) 511 { 512 disk_cbdata_t *cbp = (disk_cbdata_t *)arg; 513 topo_mod_t *mod = cbp->dcb_mod; 514 dev_di_node_t *dnode = cbp->dcb_dnode; 515 const char *devpath; 516 char *ctds, *slice; 517 518 devpath = di_devlink_path(dl); 519 if ((dnode == NULL) || (devpath == NULL)) 520 return (DI_WALK_TERMINATE); 521 522 /* trim the slice off the public name */ 523 if (((ctds = strrchr(devpath, '/')) != NULL) && 524 ((slice = strchr(ctds, 's')) != NULL)) 525 *slice = '\0'; 526 527 /* Establish the public /dev name (no slice) */ 528 dnode->ddn_lpath = topo_mod_strdup(mod, ctds ? ctds + 1 : devpath); 529 530 if (ctds && slice) 531 *slice = 's'; 532 return (DI_WALK_TERMINATE); 533 } 534 535 static void 536 dev_di_node_free(topo_mod_t *mod, dev_di_node_t *dnode) 537 { 538 int i; 539 540 /* free the stuff we point to */ 541 if (dnode->ddn_devid) 542 topo_mod_strfree(mod, dnode->ddn_devid); 543 for (i = 0; i < dnode->ddn_ppath_count; i++) 544 topo_mod_strfree(mod, dnode->ddn_ppath[i]); 545 topo_mod_free(mod, dnode->ddn_ppath, 546 dnode->ddn_ppath_count * sizeof (uintptr_t)); 547 topo_mod_strfree(mod, dnode->ddn_dpath); 548 topo_mod_strfree(mod, dnode->ddn_lpath); 549 550 topo_mod_strfree(mod, dnode->ddn_mfg); 551 topo_mod_strfree(mod, dnode->ddn_model); 552 topo_mod_strfree(mod, dnode->ddn_serial); 553 topo_mod_strfree(mod, dnode->ddn_firm); 554 topo_mod_strfree(mod, dnode->ddn_cap); 555 556 for (i = 0; i < dnode->ddn_target_port_count; i++) 557 topo_mod_strfree(mod, dnode->ddn_target_port[i]); 558 topo_mod_free(mod, dnode->ddn_target_port, 559 dnode->ddn_target_port_count * sizeof (uintptr_t)); 560 561 /* free self */ 562 topo_mod_free(mod, dnode, sizeof (dev_di_node_t)); 563 } 564 565 static int 566 dev_di_node_add(di_node_t node, char *devid, disk_cbdata_t *cbp) 567 { 568 topo_mod_t *mod = cbp->dcb_mod; 569 dev_di_node_t *dnode; 570 di_path_t pnode; 571 char *path; 572 int mlen; 573 char *minorpath; 574 char *extn = ":a"; 575 char *s; 576 int64_t *nblocksp; 577 uint64_t nblocks; 578 int *dblksizep; 579 uint_t dblksize; 580 char lentry[MAXPATHLEN]; 581 int pathcount, portcount; 582 int *inq_dtype, itype; 583 int ret, i; 584 585 if (devid) { 586 /* 587 * Check for list duplicate using devid search. 588 * Note if there is no devid, then we can end up with duplicates 589 * in the list, but this doesn't do any harm. 590 */ 591 for (dnode = topo_list_next(cbp->dcb_list); 592 dnode != NULL; dnode = topo_list_next(dnode)) { 593 if (dnode->ddn_devid && 594 devid_str_compare(dnode->ddn_devid, devid) == 0) { 595 topo_mod_dprintf(mod, "dev_di_node_add: " 596 "already there %s\n", devid); 597 return (0); 598 } 599 } 600 } 601 602 if ((dnode = topo_mod_zalloc(mod, sizeof (dev_di_node_t))) == NULL) 603 return (-1); 604 605 if (devid) { 606 /* Establish the devid. */ 607 dnode->ddn_devid = topo_mod_strdup(mod, devid); 608 if (dnode->ddn_devid == NULL) 609 goto error; 610 } 611 612 /* Establish the devinfo dpath */ 613 if ((path = di_devfs_path(node)) == NULL) { 614 (void) topo_mod_seterrno(mod, errno); 615 goto error; 616 } 617 618 dnode->ddn_dpath = topo_mod_strdup(mod, path); 619 di_devfs_path_free(path); 620 if (dnode->ddn_dpath == NULL) 621 goto error; 622 623 /* 624 * Establish the physical ppath and target ports. If the device is 625 * non-mpxio then dpath and ppath are the same, and the target port is a 626 * property of the device node. 627 * 628 * If dpath is a client node under scsi_vhci, then iterate over all 629 * paths and get their physical paths and target port properrties. 630 * di_path_client_next_path call below will 631 * return non-NULL, and ppath is set to the physical path to the first 632 * pathinfo node. 633 * 634 * NOTE: It is possible to get a generic.vs.non-generic path 635 * for di_devfs_path.vs.di_path_devfs_path like: 636 * xml: /pci@7b,0/pci1022,7458@11/pci1000,3060@2/sd@2,0 637 * pnode: /pci@7b,0/pci1022,7458@11/pci1000,3060@2/disk@2,0 638 * To resolve this issue disk_declare_path() needs to use the 639 * special di_devfs_path_match() interface. 640 */ 641 pathcount = portcount = 0; 642 pnode = NULL; 643 while ((pnode = di_path_client_next_path(node, pnode)) != NULL) { 644 if ((ret = di_path_prop_lookup_strings(pnode, 645 SCSI_ADDR_PROP_TARGET_PORT, &s)) > 0) 646 portcount += ret; 647 pathcount++; 648 } 649 650 if (pathcount == 0) { 651 if ((dnode->ddn_ppath = 652 topo_mod_zalloc(mod, sizeof (uintptr_t))) == NULL) 653 goto error; 654 655 dnode->ddn_ppath_count = 1; 656 if ((dnode->ddn_ppath[0] = topo_mod_strdup(mod, 657 dnode->ddn_dpath)) == NULL) 658 goto error; 659 660 if ((ret = di_prop_lookup_strings(DDI_DEV_T_ANY, node, 661 SCSI_ADDR_PROP_TARGET_PORT, &s)) > 0) { 662 if ((dnode->ddn_target_port = topo_mod_zalloc(mod, 663 ret * sizeof (uintptr_t))) == NULL) 664 goto error; 665 dnode->ddn_target_port_count = ret; 666 667 for (i = 0; i < ret; i++) { 668 if ((dnode->ddn_target_port[i] = 669 topo_mod_strdup(mod, 670 scsi_wwnstr_skip_ua_prefix(s))) == 671 NULL) 672 goto error; 673 674 s += strlen(s) + 1; 675 } 676 } 677 } else { 678 if ((dnode->ddn_ppath = topo_mod_zalloc(mod, 679 pathcount * sizeof (uintptr_t))) == NULL) 680 goto error; 681 682 dnode->ddn_ppath_count = pathcount; 683 684 if (portcount != 0 && 685 ((dnode->ddn_target_port = topo_mod_zalloc(mod, 686 portcount * sizeof (uintptr_t)))) == NULL) 687 goto error; 688 689 dnode->ddn_target_port_count = portcount; 690 691 pnode = NULL; 692 pathcount = portcount = 0; 693 while ((pnode = di_path_client_next_path(node, 694 pnode)) != NULL) { 695 if ((path = di_path_devfs_path(pnode)) == NULL) { 696 (void) topo_mod_seterrno(mod, errno); 697 goto error; 698 } 699 700 dnode->ddn_ppath[pathcount] = 701 topo_mod_strdup(mod, path); 702 di_devfs_path_free(path); 703 if (dnode->ddn_ppath[pathcount] == NULL) 704 goto error; 705 706 if ((ret = di_path_prop_lookup_strings(pnode, 707 SCSI_ADDR_PROP_TARGET_PORT, &s)) > 0) { 708 for (i = 0; i < ret; i++) { 709 if ((dnode->ddn_target_port[portcount] = 710 topo_mod_strdup(mod, 711 scsi_wwnstr_skip_ua_prefix(s))) == 712 NULL) 713 goto error; 714 715 portcount++; 716 s += strlen(s) + 1; 717 } 718 } 719 720 pathcount++; 721 } 722 } 723 724 /* 725 * Find the public /dev name for a disk by adding a minor name and using 726 * di_devlink interface for reverse translation (use devinfo path). 727 */ 728 if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, "inquiry-device-type", 729 &inq_dtype) > 0) { 730 itype = (*inq_dtype) & DTYPE_MASK; 731 if (itype == DTYPE_DIRECT) { 732 mlen = strlen(dnode->ddn_dpath) + strlen(extn) + 1; 733 if ((minorpath = topo_mod_alloc(mod, mlen)) == NULL) 734 goto error; 735 (void) snprintf(minorpath, mlen, "%s%s", 736 dnode->ddn_dpath, extn); 737 cbp->dcb_dnode = dnode; 738 (void) di_devlink_walk(cbp->dcb_devhdl, "^dsk/", 739 minorpath, DI_PRIMARY_LINK, cbp, 740 disk_devlink_callback); 741 topo_mod_free(mod, minorpath, mlen); 742 if (dnode->ddn_lpath == NULL) { 743 topo_mod_dprintf(mod, "dev_di_node_add: " 744 "failed to determine logical path"); 745 } 746 } 747 } 748 749 /* cache various bits of optional information about the device. */ 750 if (di_prop_lookup_strings(DDI_DEV_T_ANY, node, 751 INQUIRY_VENDOR_ID, &s) > 0) { 752 if ((dnode->ddn_mfg = disk_trim_whitespace(mod, s)) == NULL) 753 goto error; 754 } 755 if (di_prop_lookup_strings(DDI_DEV_T_ANY, node, 756 INQUIRY_PRODUCT_ID, &s) > 0) { 757 if ((dnode->ddn_model = disk_trim_whitespace(mod, s)) == NULL) 758 goto error; 759 } 760 if (di_prop_lookup_strings(DDI_DEV_T_ANY, node, 761 INQUIRY_REVISION_ID, &s) > 0) { 762 if ((dnode->ddn_firm = disk_trim_whitespace(mod, s)) == NULL) 763 goto error; 764 } 765 if (di_prop_lookup_strings(DDI_DEV_T_ANY, node, 766 INQUIRY_SERIAL_NO, &s) > 0) { 767 if ((dnode->ddn_serial = disk_trim_whitespace(mod, s)) == NULL) 768 goto error; 769 } 770 if (di_prop_lookup_int64(DDI_DEV_T_ANY, node, 771 "device-nblocks", &nblocksp) > 0) { 772 nblocks = (uint64_t)*nblocksp; 773 /* 774 * To save kernel memory, the driver may not define 775 * "device-dblksize" when its value is default DEV_BSIZE. 776 */ 777 if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, 778 "device-dblksize", &dblksizep) > 0) 779 dblksize = (uint_t)*dblksizep; 780 else 781 dblksize = DEV_BSIZE; /* default value */ 782 (void) snprintf(lentry, sizeof (lentry), 783 "%" PRIu64, nblocks * dblksize); 784 if ((dnode->ddn_cap = topo_mod_strdup(mod, lentry)) == NULL) 785 goto error; 786 } 787 788 topo_mod_dprintf(mod, "dev_di_node_add: " 789 "adding %s\n", devid ? dnode->ddn_devid : "NULL devid"); 790 topo_mod_dprintf(mod, " " 791 " %s\n", dnode->ddn_dpath); 792 for (i = 0; i < dnode->ddn_ppath_count; i++) { 793 topo_mod_dprintf(mod, " " 794 " %s\n", dnode->ddn_ppath[i]); 795 } 796 topo_list_append(cbp->dcb_list, dnode); 797 return (0); 798 799 error: 800 dev_di_node_free(mod, dnode); 801 return (-1); 802 } 803 804 /* di_walk_node callback for disk_list_gather */ 805 static int 806 dev_walk_di_nodes(di_node_t node, void *arg) 807 { 808 char *devidstr = NULL; 809 char *s; 810 int *val; 811 812 /* 813 * If it's not a scsi_vhci client and doesn't have a target_port 814 * property and doesn't have a target property then it's not a storage 815 * device and we're not interested. 816 */ 817 if (di_path_client_next_path(node, NULL) == NULL && 818 di_prop_lookup_strings(DDI_DEV_T_ANY, node, 819 SCSI_ADDR_PROP_TARGET_PORT, &s) <= 0 && 820 di_prop_lookup_ints(DDI_DEV_T_ANY, node, 821 SCSI_ADDR_PROP_TARGET, &val) <= 0) { 822 return (DI_WALK_CONTINUE); 823 } 824 (void) di_prop_lookup_strings(DDI_DEV_T_ANY, node, 825 DEVID_PROP_NAME, &devidstr); 826 827 /* create/find the devid scsi topology node */ 828 (void) dev_di_node_add(node, devidstr, arg); 829 830 return (DI_WALK_CONTINUE); 831 } 832 833 int 834 dev_list_gather(topo_mod_t *mod, topo_list_t *listp) 835 { 836 di_node_t devtree; 837 di_devlink_handle_t devhdl; 838 disk_cbdata_t dcb; 839 840 if ((devtree = topo_mod_devinfo(mod)) == DI_NODE_NIL) { 841 topo_mod_dprintf(mod, "disk_list_gather: " 842 "topo_mod_devinfo() failed"); 843 return (-1); 844 } 845 846 if ((devhdl = di_devlink_init(NULL, 0)) == DI_NODE_NIL) { 847 topo_mod_dprintf(mod, "disk_list_gather: " 848 "di_devlink_init() failed"); 849 return (-1); 850 } 851 852 dcb.dcb_mod = mod; 853 dcb.dcb_list = listp; 854 dcb.dcb_devhdl = devhdl; 855 856 /* walk the devinfo snapshot looking for disk nodes */ 857 (void) di_walk_node(devtree, DI_WALK_CLDFIRST, &dcb, 858 dev_walk_di_nodes); 859 860 (void) di_devlink_fini(&devhdl); 861 862 return (0); 863 } 864 865 void 866 dev_list_free(topo_mod_t *mod, topo_list_t *listp) 867 { 868 dev_di_node_t *dnode; 869 870 while ((dnode = topo_list_next(listp)) != NULL) { 871 /* order of delete/free is important */ 872 topo_list_delete(listp, dnode); 873 dev_di_node_free(mod, dnode); 874 } 875 } 876 877 /* 878 * Query the current disk status. If successful, the disk status is returned 879 * as an nvlist consisting of at least the following members: 880 * 881 * protocol string Supported protocol (currently "scsi") 882 * 883 * status nvlist Arbitrary protocol-specific information 884 * about the current state of the disk. 885 * 886 * faults nvlist A list of supported faults. Each 887 * element of this list is a boolean value. 888 * An element's existence indicates that 889 * the drive supports detecting this fault, 890 * and the value indicates the current 891 * state of the fault. 892 * 893 * <fault-name> nvlist For each fault named in 'faults', a 894 * nvlist describing protocol-specific 895 * attributes of the fault. 896 * 897 * This method relies on the libdiskstatus library to query this information. 898 */ 899 static int 900 disk_status(topo_mod_t *mod, tnode_t *nodep, topo_version_t vers, 901 nvlist_t *in_nvl, nvlist_t **out_nvl) 902 { 903 disk_status_t *dsp; 904 char *devpath, *fullpath; 905 size_t pathlen; 906 nvlist_t *status; 907 int err; 908 909 *out_nvl = NULL; 910 911 if (vers != TOPO_METH_DISK_STATUS_VERSION) 912 return (topo_mod_seterrno(mod, EMOD_VER_NEW)); 913 914 /* 915 * If the caller specifies the "path" parameter, then this indicates 916 * that we should use this instead of deriving it from the topo node 917 * itself. 918 */ 919 if (nvlist_lookup_string(in_nvl, "path", &fullpath) == 0) { 920 devpath = NULL; 921 } else { 922 /* 923 * Get the /devices path and attempt to open the disk status 924 * handle. 925 */ 926 if (topo_prop_get_string(nodep, TOPO_PGROUP_IO, 927 TOPO_IO_DEV_PATH, &devpath, &err) != 0) 928 return (topo_mod_seterrno(mod, EMOD_METHOD_NOTSUP)); 929 930 /* 931 * Note that sizeof(string) includes the terminating NULL byte 932 */ 933 pathlen = strlen(devpath) + sizeof ("/devices") + 934 sizeof (PHYS_EXTN) - 1; 935 936 if ((fullpath = topo_mod_alloc(mod, pathlen)) == NULL) 937 return (topo_mod_seterrno(mod, EMOD_NOMEM)); 938 939 (void) snprintf(fullpath, pathlen, "/devices%s%s", devpath, 940 PHYS_EXTN); 941 942 topo_mod_strfree(mod, devpath); 943 } 944 945 if ((dsp = disk_status_open(fullpath, &err)) == NULL) { 946 if (devpath) 947 topo_mod_free(mod, fullpath, pathlen); 948 return (topo_mod_seterrno(mod, err == EDS_NOMEM ? 949 EMOD_NOMEM : EMOD_METHOD_NOTSUP)); 950 } 951 952 if (devpath) 953 topo_mod_free(mod, fullpath, pathlen); 954 955 if ((status = disk_status_get(dsp)) == NULL) { 956 err = (disk_status_errno(dsp) == EDS_NOMEM ? 957 EMOD_NOMEM : EMOD_METHOD_NOTSUP); 958 disk_status_close(dsp); 959 return (topo_mod_seterrno(mod, err)); 960 } 961 962 *out_nvl = status; 963 disk_status_close(dsp); 964 return (0); 965 } 966