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 (topo_prop_set_string(dtn, TOPO_PGROUP_STORAGE, 223 TOPO_STORAGE_LOGICAL_DISK_NAME, TOPO_PROP_IMMUTABLE, 224 dnode->ddn_lpath, &err) != 0) { 225 topo_mod_dprintf(mod, "disk_set_props: " 226 "set disk_name error %s\n", topo_strerror(err)); 227 goto error; 228 } 229 230 /* populate other misc storage group properties */ 231 if (dnode->ddn_mfg && (topo_prop_set_string(dtn, TOPO_PGROUP_STORAGE, 232 TOPO_STORAGE_MANUFACTURER, TOPO_PROP_IMMUTABLE, 233 dnode->ddn_mfg, &err) != 0)) { 234 topo_mod_dprintf(mod, "disk_set_props: " 235 "set mfg error %s\n", topo_strerror(err)); 236 goto error; 237 } 238 if (dnode->ddn_model && (topo_prop_set_string(dtn, TOPO_PGROUP_STORAGE, 239 TOPO_STORAGE_MODEL, TOPO_PROP_IMMUTABLE, 240 dnode->ddn_model, &err) != 0)) { 241 topo_mod_dprintf(mod, "disk_set_props: " 242 "set model error %s\n", topo_strerror(err)); 243 goto error; 244 } 245 if (dnode->ddn_serial && (topo_prop_set_string(dtn, TOPO_PGROUP_STORAGE, 246 TOPO_STORAGE_SERIAL_NUM, TOPO_PROP_IMMUTABLE, 247 dnode->ddn_serial, &err) != 0)) { 248 topo_mod_dprintf(mod, "disk_set_props: " 249 "set serial error %s\n", topo_strerror(err)); 250 goto error; 251 } 252 if (dnode->ddn_firm && (topo_prop_set_string(dtn, TOPO_PGROUP_STORAGE, 253 TOPO_STORAGE_FIRMWARE_REV, TOPO_PROP_IMMUTABLE, 254 dnode->ddn_firm, &err) != 0)) { 255 topo_mod_dprintf(mod, "disk_set_props: " 256 "set firm error %s\n", topo_strerror(err)); 257 goto error; 258 } 259 if (dnode->ddn_cap && (topo_prop_set_string(dtn, TOPO_PGROUP_STORAGE, 260 TOPO_STORAGE_CAPACITY, TOPO_PROP_IMMUTABLE, 261 dnode->ddn_cap, &err) != 0)) { 262 topo_mod_dprintf(mod, "disk_set_props: " 263 "set cap error %s\n", topo_strerror(err)); 264 goto error; 265 } 266 err = 0; 267 268 out: if (fmri) 269 nvlist_free(fmri); 270 if (label) 271 topo_mod_strfree(mod, label); 272 if (asru) 273 nvlist_free(asru); 274 return (err); 275 276 error: err = topo_mod_seterrno(mod, err); 277 goto out; 278 } 279 280 /* 281 * Trim leading and trailing whitespace from the string. 282 */ 283 static char * 284 disk_trim_whitespace(topo_mod_t *mod, const char *begin) 285 { 286 const char *end; 287 char *buf; 288 size_t count; 289 290 if (begin == NULL) 291 return (NULL); 292 293 end = begin + strlen(begin); 294 295 while (begin < end && isspace(*begin)) 296 begin++; 297 while (begin < end && isspace(*(end - 1))) 298 end--; 299 300 count = end - begin; 301 if ((buf = topo_mod_alloc(mod, count + 1)) == NULL) 302 return (NULL); 303 304 (void) strlcpy(buf, begin, count + 1); 305 306 return (buf); 307 } 308 309 /* 310 * Manufacturing strings can contain characters that are invalid for use in hc 311 * authority names. This trims leading and trailing whitespace, and 312 * substitutes any characters known to be bad. 313 */ 314 char * 315 disk_auth_clean(topo_mod_t *mod, const char *str) 316 { 317 char *buf, *p; 318 319 if (str == NULL) 320 return (NULL); 321 322 if ((buf = topo_mod_strdup(mod, str)) == NULL) 323 return (NULL); 324 325 while ((p = strpbrk(buf, " :=")) != NULL) 326 *p = '-'; 327 328 return (buf); 329 } 330 331 /* create the disk topo node */ 332 static int 333 disk_tnode_create(topo_mod_t *mod, tnode_t *parent, 334 dev_di_node_t *dnode, const char *name, topo_instance_t i, tnode_t **rval) 335 { 336 int len; 337 nvlist_t *fmri; 338 tnode_t *dtn; 339 char *part = NULL; 340 nvlist_t *auth; 341 char *mfg, *model, *firm, *serial; 342 343 *rval = NULL; 344 if (dnode != NULL) { 345 mfg = disk_auth_clean(mod, dnode->ddn_mfg); 346 model = disk_auth_clean(mod, dnode->ddn_model); 347 firm = disk_auth_clean(mod, dnode->ddn_firm); 348 serial = disk_auth_clean(mod, dnode->ddn_serial); 349 } else { 350 mfg = model = firm = serial = NULL; 351 } 352 353 /* form 'part=' of fmri as "<mfg>-<model>" */ 354 if (mfg != NULL && model != NULL) { 355 len = strlen(mfg) + 1 + strlen(model) + 1; 356 if ((part = topo_mod_alloc(mod, len)) != NULL) 357 (void) snprintf(part, len, "%s-%s", 358 mfg, model); 359 } 360 361 auth = topo_mod_auth(mod, parent); 362 fmri = topo_mod_hcfmri(mod, parent, FM_HC_SCHEME_VERSION, name, i, NULL, 363 auth, part ? part : model, firm, serial); 364 nvlist_free(auth); 365 366 topo_mod_strfree(mod, part); 367 topo_mod_strfree(mod, mfg); 368 topo_mod_strfree(mod, model); 369 topo_mod_strfree(mod, firm); 370 topo_mod_strfree(mod, serial); 371 372 if (fmri == NULL) { 373 topo_mod_dprintf(mod, "disk_tnode_create: " 374 "hcfmri (%s%d/%s%d) error %s\n", 375 topo_node_name(parent), topo_node_instance(parent), 376 name, i, topo_strerror(topo_mod_errno(mod))); 377 return (-1); 378 } 379 380 if ((dtn = topo_node_bind(mod, parent, name, i, fmri)) == NULL) { 381 if (topo_mod_errno(mod) == EMOD_NODE_BOUND) { 382 /* 383 * if disk 0 is already there then we're done 384 */ 385 nvlist_free(fmri); 386 return (0); 387 } 388 topo_mod_dprintf(mod, "disk_tnode_create: " 389 "bind (%s%d/%s%d) error %s\n", 390 topo_node_name(parent), topo_node_instance(parent), 391 name, i, topo_strerror(topo_mod_errno(mod))); 392 nvlist_free(fmri); 393 return (-1); 394 } 395 nvlist_free(fmri); 396 397 /* add the properties of the disk */ 398 if (disk_set_props(mod, parent, dtn, dnode) != 0) { 399 topo_mod_dprintf(mod, "disk_tnode_create: " 400 "disk_set_props (%s%d/%s%d) error %s\n", 401 topo_node_name(parent), topo_node_instance(parent), 402 name, i, topo_strerror(topo_mod_errno(mod))); 403 topo_node_unbind(dtn); 404 return (-1); 405 } 406 *rval = dtn; 407 return (0); 408 } 409 410 static int 411 disk_declare(topo_mod_t *mod, tnode_t *parent, dev_di_node_t *dnode, 412 tnode_t **childp) 413 { 414 tnode_t *dtn = NULL; 415 int rval; 416 417 rval = disk_tnode_create(mod, parent, dnode, DISK, 0, &dtn); 418 if (dtn == NULL) { 419 if (rval == 0) 420 return (0); 421 topo_mod_dprintf(mod, "disk_declare: " 422 "disk_tnode_create error %s\n", 423 topo_strerror(topo_mod_errno(mod))); 424 return (-1); 425 } 426 427 /* register disk_methods against the disk topo node */ 428 if (topo_method_register(mod, dtn, disk_methods) != 0) { 429 topo_mod_dprintf(mod, "disk_declare: " 430 "topo_method_register error %s\n", 431 topo_strerror(topo_mod_errno(mod))); 432 topo_node_unbind(dtn); 433 return (-1); 434 } 435 if (childp != NULL) 436 *childp = dtn; 437 return (0); 438 } 439 440 int 441 disk_declare_path(topo_mod_t *mod, tnode_t *parent, topo_list_t *listp, 442 const char *path) 443 { 444 dev_di_node_t *dnode; 445 int i; 446 447 /* 448 * Check for match using physical phci (ddn_ppath). Use 449 * di_devfs_path_match so generic.vs.non-generic names match. 450 */ 451 for (dnode = topo_list_next(listp); dnode != NULL; 452 dnode = topo_list_next(dnode)) { 453 if (dnode->ddn_ppath == NULL) 454 continue; 455 456 for (i = 0; i < dnode->ddn_ppath_count; i++) { 457 if (di_devfs_path_match(dnode->ddn_ppath[0], path)) 458 return (disk_declare(mod, parent, dnode, NULL)); 459 } 460 } 461 462 topo_mod_dprintf(mod, "disk_declare_path: " 463 "failed to find disk matching path %s", path); 464 return (0); 465 } 466 467 int 468 disk_declare_addr(topo_mod_t *mod, tnode_t *parent, topo_list_t *listp, 469 const char *addr, tnode_t **childp) 470 { 471 dev_di_node_t *dnode; 472 int i; 473 474 /* Check for match using addr. */ 475 for (dnode = topo_list_next(listp); dnode != NULL; 476 dnode = topo_list_next(dnode)) { 477 if (dnode->ddn_target_port == NULL) 478 continue; 479 480 for (i = 0; i < dnode->ddn_target_port_count; i++) { 481 if (strncmp(dnode->ddn_target_port[i], addr, 482 strcspn(dnode->ddn_target_port[i], ":")) == 0) 483 return (disk_declare(mod, parent, dnode, 484 childp)); 485 } 486 } 487 488 topo_mod_dprintf(mod, "disk_declare_addr: " 489 "failed to find disk matching addr %s", addr); 490 491 return (1); 492 } 493 494 /* 495 * Used to declare a disk that has been discovered through other means (usually 496 * ses), that is not enumerated in the devinfo tree. 497 */ 498 int 499 disk_declare_non_enumerated(topo_mod_t *mod, tnode_t *parent, tnode_t **childp) 500 { 501 return (disk_declare(mod, parent, NULL, childp)); 502 } 503 504 /* di_devlink callback for dev_di_node_add */ 505 static int 506 disk_devlink_callback(di_devlink_t dl, void *arg) 507 { 508 disk_cbdata_t *cbp = (disk_cbdata_t *)arg; 509 topo_mod_t *mod = cbp->dcb_mod; 510 dev_di_node_t *dnode = cbp->dcb_dnode; 511 const char *devpath; 512 char *ctds, *slice; 513 514 devpath = di_devlink_path(dl); 515 if ((dnode == NULL) || (devpath == NULL)) 516 return (DI_WALK_TERMINATE); 517 518 /* trim the slice off the public name */ 519 if (((ctds = strrchr(devpath, '/')) != NULL) && 520 ((slice = strchr(ctds, 's')) != NULL)) 521 *slice = '\0'; 522 523 /* Establish the public /dev name (no slice) */ 524 dnode->ddn_lpath = topo_mod_strdup(mod, ctds ? ctds + 1 : devpath); 525 526 if (ctds && slice) 527 *slice = 's'; 528 return (DI_WALK_TERMINATE); 529 } 530 531 static void 532 dev_di_node_free(topo_mod_t *mod, dev_di_node_t *dnode) 533 { 534 int i; 535 536 /* free the stuff we point to */ 537 if (dnode->ddn_devid) 538 topo_mod_strfree(mod, dnode->ddn_devid); 539 for (i = 0; i < dnode->ddn_ppath_count; i++) 540 topo_mod_strfree(mod, dnode->ddn_ppath[i]); 541 topo_mod_free(mod, dnode->ddn_ppath, 542 dnode->ddn_ppath_count * sizeof (uintptr_t)); 543 topo_mod_strfree(mod, dnode->ddn_dpath); 544 topo_mod_strfree(mod, dnode->ddn_lpath); 545 546 topo_mod_strfree(mod, dnode->ddn_mfg); 547 topo_mod_strfree(mod, dnode->ddn_model); 548 topo_mod_strfree(mod, dnode->ddn_serial); 549 topo_mod_strfree(mod, dnode->ddn_firm); 550 topo_mod_strfree(mod, dnode->ddn_cap); 551 552 for (i = 0; i < dnode->ddn_target_port_count; i++) 553 topo_mod_strfree(mod, dnode->ddn_target_port[i]); 554 topo_mod_free(mod, dnode->ddn_target_port, 555 dnode->ddn_target_port_count * sizeof (uintptr_t)); 556 557 /* free self */ 558 topo_mod_free(mod, dnode, sizeof (dev_di_node_t)); 559 } 560 561 static int 562 dev_di_node_add(di_node_t node, char *devid, disk_cbdata_t *cbp) 563 { 564 topo_mod_t *mod = cbp->dcb_mod; 565 dev_di_node_t *dnode; 566 di_path_t pnode; 567 char *path; 568 int mlen; 569 char *minorpath; 570 char *extn = ":a"; 571 char *s; 572 int64_t *nblocksp; 573 uint64_t nblocks; 574 int *dblksizep; 575 uint_t dblksize; 576 char lentry[MAXPATHLEN]; 577 int pathcount, portcount; 578 int *inq_dtype, itype; 579 int ret, i; 580 581 if (devid) { 582 /* 583 * Check for list duplicate using devid search. 584 * Note if there is no devid, then we can end up with duplicates 585 * in the list, but this doesn't do any harm. 586 */ 587 for (dnode = topo_list_next(cbp->dcb_list); 588 dnode != NULL; dnode = topo_list_next(dnode)) { 589 if (dnode->ddn_devid && 590 devid_str_compare(dnode->ddn_devid, devid) == 0) { 591 topo_mod_dprintf(mod, "dev_di_node_add: " 592 "already there %s\n", devid); 593 return (0); 594 } 595 } 596 } 597 598 if ((dnode = topo_mod_zalloc(mod, sizeof (dev_di_node_t))) == NULL) 599 return (-1); 600 601 if (devid) { 602 /* Establish the devid. */ 603 dnode->ddn_devid = topo_mod_strdup(mod, devid); 604 if (dnode->ddn_devid == NULL) 605 goto error; 606 } 607 608 /* Establish the devinfo dpath */ 609 if ((path = di_devfs_path(node)) == NULL) { 610 (void) topo_mod_seterrno(mod, errno); 611 goto error; 612 } 613 614 dnode->ddn_dpath = topo_mod_strdup(mod, path); 615 di_devfs_path_free(path); 616 if (dnode->ddn_dpath == NULL) 617 goto error; 618 619 /* 620 * Establish the physical ppath and target ports. If the device is 621 * non-mpxio then dpath and ppath are the same, and the target port is a 622 * property of the device node. 623 * 624 * If dpath is a client node under scsi_vhci, then iterate over all 625 * paths and get their physical paths and target port properrties. 626 * di_path_client_next_path call below will 627 * return non-NULL, and ppath is set to the physical path to the first 628 * pathinfo node. 629 * 630 * NOTE: It is possible to get a generic.vs.non-generic path 631 * for di_devfs_path.vs.di_path_devfs_path like: 632 * xml: /pci@7b,0/pci1022,7458@11/pci1000,3060@2/sd@2,0 633 * pnode: /pci@7b,0/pci1022,7458@11/pci1000,3060@2/disk@2,0 634 * To resolve this issue disk_declare_path() needs to use the 635 * special di_devfs_path_match() interface. 636 */ 637 pathcount = portcount = 0; 638 pnode = NULL; 639 while ((pnode = di_path_client_next_path(node, pnode)) != NULL) { 640 if ((ret = di_path_prop_lookup_strings(pnode, 641 SCSI_ADDR_PROP_TARGET_PORT, &s)) > 0) 642 portcount += ret; 643 pathcount++; 644 } 645 646 if (pathcount == 0) { 647 if ((dnode->ddn_ppath = 648 topo_mod_zalloc(mod, sizeof (uintptr_t))) == NULL) 649 goto error; 650 651 dnode->ddn_ppath_count = 1; 652 if ((dnode->ddn_ppath[0] = topo_mod_strdup(mod, 653 dnode->ddn_dpath)) == NULL) 654 goto error; 655 656 if ((ret = di_prop_lookup_strings(DDI_DEV_T_ANY, node, 657 SCSI_ADDR_PROP_TARGET_PORT, &s)) > 0) { 658 if ((dnode->ddn_target_port = topo_mod_zalloc(mod, 659 ret * sizeof (uintptr_t))) == NULL) 660 goto error; 661 dnode->ddn_target_port_count = ret; 662 663 for (i = 0; i < ret; i++) { 664 if ((dnode->ddn_target_port[i] = 665 topo_mod_strdup(mod, 666 scsi_wwnstr_skip_ua_prefix(s))) == 667 NULL) 668 goto error; 669 670 s += strlen(s) + 1; 671 } 672 } 673 } else { 674 if ((dnode->ddn_ppath = topo_mod_zalloc(mod, 675 pathcount * sizeof (uintptr_t))) == NULL) 676 goto error; 677 678 dnode->ddn_ppath_count = pathcount; 679 680 if (portcount != 0 && 681 ((dnode->ddn_target_port = topo_mod_zalloc(mod, 682 portcount * sizeof (uintptr_t)))) == NULL) 683 goto error; 684 685 dnode->ddn_target_port_count = portcount; 686 687 pnode = NULL; 688 pathcount = portcount = 0; 689 while ((pnode = di_path_client_next_path(node, 690 pnode)) != NULL) { 691 if ((path = di_path_devfs_path(pnode)) == NULL) { 692 (void) topo_mod_seterrno(mod, errno); 693 goto error; 694 } 695 696 dnode->ddn_ppath[pathcount] = 697 topo_mod_strdup(mod, path); 698 di_devfs_path_free(path); 699 if (dnode->ddn_ppath[pathcount] == NULL) 700 goto error; 701 702 if ((ret = di_path_prop_lookup_strings(pnode, 703 SCSI_ADDR_PROP_TARGET_PORT, &s)) > 0) { 704 for (i = 0; i < ret; i++) { 705 if ((dnode->ddn_target_port[portcount] = 706 topo_mod_strdup(mod, 707 scsi_wwnstr_skip_ua_prefix(s))) == 708 NULL) 709 goto error; 710 711 portcount++; 712 s += strlen(s) + 1; 713 } 714 } 715 716 pathcount++; 717 } 718 } 719 720 /* 721 * Find the public /dev name for a disk by adding a minor name and using 722 * di_devlink interface for reverse translation (use devinfo path). 723 */ 724 if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, "inquiry-device-type", 725 &inq_dtype) > 0) { 726 itype = (*inq_dtype) & DTYPE_MASK; 727 if (itype == DTYPE_DIRECT) { 728 mlen = strlen(dnode->ddn_dpath) + strlen(extn) + 1; 729 if ((minorpath = topo_mod_alloc(mod, mlen)) == NULL) 730 goto error; 731 (void) snprintf(minorpath, mlen, "%s%s", 732 dnode->ddn_dpath, extn); 733 cbp->dcb_dnode = dnode; 734 (void) di_devlink_walk(cbp->dcb_devhdl, "^dsk/", 735 minorpath, DI_PRIMARY_LINK, cbp, 736 disk_devlink_callback); 737 topo_mod_free(mod, minorpath, mlen); 738 if (dnode->ddn_lpath == NULL) { 739 topo_mod_dprintf(mod, "dev_di_node_add: " 740 "failed to determine logical path"); 741 goto error; 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 * 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 return (DI_WALK_CONTINUE); 817 } 818 (void) di_prop_lookup_strings(DDI_DEV_T_ANY, node, 819 DEVID_PROP_NAME, &devidstr); 820 821 /* create/find the devid scsi topology node */ 822 (void) dev_di_node_add(node, devidstr, arg); 823 824 return (DI_WALK_CONTINUE); 825 } 826 827 int 828 dev_list_gather(topo_mod_t *mod, topo_list_t *listp) 829 { 830 di_node_t devtree; 831 di_devlink_handle_t devhdl; 832 disk_cbdata_t dcb; 833 834 if ((devtree = topo_mod_devinfo(mod)) == DI_NODE_NIL) { 835 topo_mod_dprintf(mod, "disk_list_gather: " 836 "topo_mod_devinfo() failed"); 837 return (-1); 838 } 839 840 if ((devhdl = di_devlink_init(NULL, 0)) == DI_NODE_NIL) { 841 topo_mod_dprintf(mod, "disk_list_gather: " 842 "di_devlink_init() failed"); 843 return (-1); 844 } 845 846 dcb.dcb_mod = mod; 847 dcb.dcb_list = listp; 848 dcb.dcb_devhdl = devhdl; 849 850 /* walk the devinfo snapshot looking for disk nodes */ 851 (void) di_walk_node(devtree, DI_WALK_CLDFIRST, &dcb, 852 dev_walk_di_nodes); 853 854 (void) di_devlink_fini(&devhdl); 855 856 return (0); 857 } 858 859 void 860 dev_list_free(topo_mod_t *mod, topo_list_t *listp) 861 { 862 dev_di_node_t *dnode; 863 864 while ((dnode = topo_list_next(listp)) != NULL) { 865 /* order of delete/free is important */ 866 topo_list_delete(listp, dnode); 867 dev_di_node_free(mod, dnode); 868 } 869 } 870 871 /* 872 * Query the current disk status. If successful, the disk status is returned 873 * as an nvlist consisting of at least the following members: 874 * 875 * protocol string Supported protocol (currently "scsi") 876 * 877 * status nvlist Arbitrary protocol-specific information 878 * about the current state of the disk. 879 * 880 * faults nvlist A list of supported faults. Each 881 * element of this list is a boolean value. 882 * An element's existence indicates that 883 * the drive supports detecting this fault, 884 * and the value indicates the current 885 * state of the fault. 886 * 887 * <fault-name> nvlist For each fault named in 'faults', a 888 * nvlist describing protocol-specific 889 * attributes of the fault. 890 * 891 * This method relies on the libdiskstatus library to query this information. 892 */ 893 static int 894 disk_status(topo_mod_t *mod, tnode_t *nodep, topo_version_t vers, 895 nvlist_t *in_nvl, nvlist_t **out_nvl) 896 { 897 disk_status_t *dsp; 898 char *devpath, *fullpath; 899 size_t pathlen; 900 nvlist_t *status; 901 int err; 902 903 *out_nvl = NULL; 904 905 if (vers != TOPO_METH_DISK_STATUS_VERSION) 906 return (topo_mod_seterrno(mod, EMOD_VER_NEW)); 907 908 /* 909 * If the caller specifies the "path" parameter, then this indicates 910 * that we should use this instead of deriving it from the topo node 911 * itself. 912 */ 913 if (nvlist_lookup_string(in_nvl, "path", &fullpath) == 0) { 914 devpath = NULL; 915 } else { 916 /* 917 * Get the /devices path and attempt to open the disk status 918 * handle. 919 */ 920 if (topo_prop_get_string(nodep, TOPO_PGROUP_IO, 921 TOPO_IO_DEV_PATH, &devpath, &err) != 0) 922 return (topo_mod_seterrno(mod, EMOD_METHOD_NOTSUP)); 923 924 /* 925 * Note that sizeof(string) includes the terminating NULL byte 926 */ 927 pathlen = strlen(devpath) + sizeof ("/devices") + 928 sizeof (PHYS_EXTN) - 1; 929 930 if ((fullpath = topo_mod_alloc(mod, pathlen)) == NULL) 931 return (topo_mod_seterrno(mod, EMOD_NOMEM)); 932 933 (void) snprintf(fullpath, pathlen, "/devices%s%s", devpath, 934 PHYS_EXTN); 935 936 topo_mod_strfree(mod, devpath); 937 } 938 939 if ((dsp = disk_status_open(fullpath, &err)) == NULL) { 940 if (devpath) 941 topo_mod_free(mod, fullpath, pathlen); 942 return (topo_mod_seterrno(mod, err == EDS_NOMEM ? 943 EMOD_NOMEM : EMOD_METHOD_NOTSUP)); 944 } 945 946 if (devpath) 947 topo_mod_free(mod, fullpath, pathlen); 948 949 if ((status = disk_status_get(dsp)) == NULL) { 950 err = (disk_status_errno(dsp) == EDS_NOMEM ? 951 EMOD_NOMEM : EMOD_METHOD_NOTSUP); 952 disk_status_close(dsp); 953 return (topo_mod_seterrno(mod, err)); 954 } 955 956 *out_nvl = status; 957 disk_status_close(dsp); 958 return (0); 959 } 960