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 2006 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 #include <sys/param.h> 30 #include <sys/stat.h> 31 #include <errno.h> 32 #include <string.h> 33 #include <dirent.h> 34 #include "cfga_sata.h" 35 36 /* 37 * This file contains the entry points to the plug-in as defined in the 38 * config_admin(3X) man page. 39 */ 40 41 /* 42 * Set the version number for the cfgadm library's use. 43 */ 44 int cfga_version = CFGA_HSL_V2; 45 46 enum { 47 HELP_HEADER = 1, 48 HELP_CONFIG, 49 HELP_RESET_PORT, 50 HELP_RESET_DEVICE, 51 HELP_RESET_ALL, 52 HELP_PORT_DEACTIVATE, 53 HELP_PORT_ACTIVATE, 54 HELP_PORT_SELF_TEST, 55 HELP_CNTRL_SELF_TEST, 56 HELP_UNKNOWN 57 }; 58 59 /* SATA specific help messages */ 60 static char *sata_help[] = { 61 NULL, 62 "SATA specific commands:\n", 63 " cfgadm -c [configure|unconfigure|disconnect|connect] ap_id " 64 "[ap_id...]\n", 65 " cfgadm -x sata_reset_port ap_id [ap_id...]\n", 66 " cfgadm -x sata_reset_device ap_id [ap_id...]\n", 67 " cfgadm -x sata_reset_all ap_id\n", 68 " cfgadm -x sata_port_deactivate ap_id [ap_id...]\n", 69 " cfgadm -x sata_port_activate ap_id [ap_id...]\n", 70 " cfgadm -x sata_port_self_test ap_id [ap_id...]\n", 71 " cfgadm -t ap_id\n", 72 "\tunknown command or option:\n", 73 NULL 74 }; /* End help messages */ 75 76 77 /* 78 * Messages. 79 */ 80 static msgcvt_t sata_msgs[] = { 81 /* CFGA_SATA_OK */ 82 { CVT, CFGA_OK, "" }, 83 84 /* CFGA_SATA_NACK */ 85 { CVT, CFGA_NACK, "" }, 86 87 /* CFGA_SATA_DEVICE_UNCONFIGURED */ 88 { CVT, CFGA_OK, "Device unconfigured prior to disconnect" }, 89 90 /* CFGA_SATA_UNKNOWN / CFGA_LIB_ERROR -> "Library error" */ 91 { CVT, CFGA_LIB_ERROR, "Unknown message; internal error" }, 92 93 /* CFGA_SATA_INTERNAL_ERROR / CFGA_LIB_ERROR -> "Library error" */ 94 { CVT, CFGA_LIB_ERROR, "Internal error" }, 95 96 /* CFGA_SATA_DATA_ERROR / CFGA_DATA_ERROR -> "Data error" */ 97 { CVT, CFGA_DATA_ERROR, "cfgadm data error" }, 98 99 /* CFGA_SATA_OPTIONS / CFGA_ERROR -> "Hardware specific failure" */ 100 { CVT, CFGA_ERROR, "Hardware specific option not supported" }, 101 102 /* CFGA_SATA_HWOPNOTSUPP / CFGA_ERROR -> "Hardware specific failure" */ 103 { CVT, CFGA_ERROR, "Hardware specific operation not supported" }, 104 105 /* 106 * CFGA_SATA_DYNAMIC_AP / 107 * CFGA_LIB_ERROR -> "Configuration operation invalid" 108 */ 109 { CVT, CFGA_INVAL, "Cannot identify attached device" }, 110 111 /* CFGA_SATA_AP / CFGA_APID_NOEXIST -> "Attachment point not found" */ 112 { CVT, CFGA_APID_NOEXIST, "" }, 113 114 /* CFGA_SATA_PORT / CFGA_LIB_ERROR -> "Library error" */ 115 { CVT, CFGA_LIB_ERROR, "Cannot determine sata port number for " }, 116 117 /* CFGA_SATA_DEVCTL / CFGA_LIB_ERROR -> "Library error" */ 118 { CVT, CFGA_LIB_ERROR, "Internal error: " 119 "Cannot allocate devctl handle " }, 120 121 /* 122 * CFGA_SATA_DEV_CONFIGURE / 123 * CFGA_ERROR -> "Hardware specific failure" 124 */ 125 { CVT, CFGA_ERROR, "Failed to config device at " }, 126 127 /* 128 * CFGA_SATA_DEV_UNCONFIGURE / 129 * CFGA_ERROR -> "Hardware specific failure" 130 */ 131 { CVT, CFGA_ERROR, "Failed to unconfig device at " }, 132 133 /* 134 * CFGA_SATA_DISCONNECTED 135 * CFGA_INVAL -> "Configuration operation invalid" 136 */ 137 { CVT, CFGA_INVAL, "Port already disconnected " }, 138 139 /* 140 * CFGA_SATA_NOT_CONNECTED 141 * CFGA_INVAL -> "Configuration operation invalid" 142 */ 143 { CVT, CFGA_INVAL, "No device connected to " }, 144 145 /* 146 * CFGA_SATA_NOT_CONFIGURED / 147 * CFGA_INVAL -> "Configuration operation invalid" 148 */ 149 { CVT, CFGA_INVAL, "No device configured at " }, 150 151 /* 152 * CFGA_SATA_ALREADY_CONNECTED / 153 * CFGA_INVAL -> "Configuration operation invalid" 154 */ 155 { CVT, CFGA_INVAL, "Device already connected to " }, 156 157 /* 158 * CFGA_SATA_ALREADY_CONFIGURED / 159 * CFGA_INVAL -> "Configuration operation invalid" 160 */ 161 { CVT, CFGA_INVAL, "Device already configured at " }, 162 163 /* 164 * CFGA_SATA_INVALID_DEVNAME / 165 * CFGA_INVAL -> "Configuration operation invalid" 166 */ 167 { CVT, CFGA_INVAL, "Cannot specify device name" }, 168 169 /* CFGA_SATA_OPEN / CFGA_LIB_ERROR -> "Library error" */ 170 { CVT, CFGA_LIB_ERROR, "Cannot open " }, 171 172 /* CFGA_SATA_IOCTL / CFGA_ERROR -> "Hardware specific failure" */ 173 { CVT, CFGA_ERROR, "Driver ioctl failed " }, 174 175 /* 176 * CFGA_SATA_BUSY / 177 * CFGA_SYSTEM_BUSY -> "System is busy, try again" 178 */ 179 { CVT, CFGA_SYSTEM_BUSY, "" }, 180 181 /* CFGA_SATA_ALLOC_FAIL / CFGA_LIB_ERROR -> "Library error" */ 182 { CVT, CFGA_LIB_ERROR, "Memory allocation failure" }, 183 184 /* 185 * CFGA_SATA_OPNOTSUPP / 186 * CFGA_OPNOTSUPP -> "Configuration operation not supported" 187 */ 188 { CVT, CFGA_OPNOTSUPP, "Operation not supported" }, 189 190 /* CFGA_SATA_DEVLINK / CFGA_LIB_ERROR -> "Library error" */ 191 { CVT, CFGA_LIB_ERROR, "Could not find /dev/cfg link for " }, 192 193 /* CFGA_SATA_STATE / CFGA_LIB_ERROR -> "Library error" */ 194 { CVT, CFGA_LIB_ERROR, "Internal error: Unrecognized ap state" }, 195 196 /* CFGA_SATA_PRIV / CFGA_PRIV -> "Insufficient privileges" */ 197 { CVT, CFGA_PRIV, "" }, 198 199 /* CFGA_SATA_NVLIST / CFGA_ERROR -> "Hardware specific failure" */ 200 { CVT, CFGA_ERROR, "Internal error (nvlist)" }, 201 202 /* CFGA_SATA_ZEROLEN / CFGA_ERROR -> "Hardware specific failure" */ 203 { CVT, CFGA_ERROR, "Internal error (zerolength string)" }, 204 205 /* CFGA_SATA_RCM_HANDLE / CFGA_ERROR -> "Hardware specific failure" */ 206 { CVT, CFGA_ERROR, "cannot get RCM handle"}, 207 208 /* 209 * CFGA_SATA_RCM_ONLINE / 210 * CFGA_SYSTEM_BUSY -> "System is busy, try again" 211 */ 212 { CVT, CFGA_SYSTEM_BUSY, "failed to online: "}, 213 214 /* 215 * CFGA_SATA_RCM_OFFLINE / 216 * CFGA_SYSTEM_BUSY -> "System is busy, try again" 217 */ 218 { CVT, CFGA_SYSTEM_BUSY, "failed to offline: "}, 219 220 /* CFGA_SATA_RCM_INFO / CFGA_ERROR -> "Hardware specific failure" */ 221 { CVT, CFGA_ERROR, "failed to query: "} 222 223 }; /* End error messages */ 224 225 static cfga_sata_ret_t 226 verify_params(const char *ap_id, const char *options, char **errstring); 227 228 229 static cfga_sata_ret_t 230 setup_for_devctl_cmd(const char *ap_id, devctl_hdl_t *devctl_hdl, 231 nvlist_t **user_nvlistp, uint_t oflag); 232 233 static cfga_sata_ret_t 234 port_state(devctl_hdl_t hdl, nvlist_t *list, 235 ap_rstate_t *rstate, ap_ostate_t *ostate); 236 237 static cfga_sata_ret_t 238 do_control_ioctl(const char *ap_id, sata_cfga_apctl_t subcommand, uint_t arg, 239 void **descrp, size_t *sizep); 240 241 static void 242 cleanup_after_devctl_cmd(devctl_hdl_t devctl_hdl, nvlist_t *user_nvlist); 243 244 static char * 245 sata_get_devicepath(const char *ap_id); 246 247 static int 248 sata_confirm(struct cfga_confirm *confp, char *msg); 249 250 251 /* Utilities */ 252 253 static cfga_sata_ret_t 254 physpath_to_devlink(const char *basedir, const char *node_path, 255 char **logpp, int *l_errnop) 256 { 257 char *linkpath; 258 char *buf; 259 char *real_path; 260 DIR *dp; 261 struct dirent *dep, *newdep; 262 int deplen; 263 boolean_t found = B_FALSE; 264 int err = 0; 265 struct stat sb; 266 char *p; 267 cfga_sata_ret_t rv = CFGA_SATA_INTERNAL_ERROR; 268 269 /* 270 * Using libdevinfo for this is overkill and kills performance 271 * when multiple consumers of libcfgadm are executing 272 * concurrently. 273 */ 274 if ((dp = opendir(basedir)) == NULL) { 275 *l_errnop = errno; 276 return (CFGA_SATA_INTERNAL_ERROR); 277 } 278 279 linkpath = malloc(PATH_MAX); 280 buf = malloc(PATH_MAX); 281 real_path = malloc(PATH_MAX); 282 283 deplen = pathconf(basedir, _PC_NAME_MAX); 284 deplen = (deplen <= 0 ? MAXNAMELEN : deplen) + 285 sizeof (struct dirent); 286 dep = (struct dirent *)malloc(deplen); 287 288 if (dep == NULL || linkpath == NULL || buf == NULL || 289 real_path == NULL) { 290 *l_errnop = ENOMEM; 291 rv = CFGA_SATA_ALLOC_FAIL; 292 goto pp_cleanup; 293 } 294 295 *logpp = NULL; 296 297 while (!found && (err = readdir_r(dp, dep, &newdep)) == 0 && 298 newdep != NULL) { 299 300 assert(newdep == dep); 301 302 if (strcmp(dep->d_name, ".") == 0 || 303 strcmp(dep->d_name, "..") == 0) 304 continue; 305 306 (void) snprintf(linkpath, MAXPATHLEN, 307 "%s/%s", basedir, dep->d_name); 308 309 if (lstat(linkpath, &sb) < 0) 310 continue; 311 312 if (S_ISDIR(sb.st_mode)) { 313 314 if ((rv = physpath_to_devlink(linkpath, node_path, 315 logpp, l_errnop)) != CFGA_SATA_OK) { 316 317 goto pp_cleanup; 318 } 319 320 if (*logpp != NULL) 321 found = B_TRUE; 322 323 } else if (S_ISLNK(sb.st_mode)) { 324 325 bzero(buf, PATH_MAX); 326 if (readlink(linkpath, buf, PATH_MAX) < 0) 327 continue; 328 329 330 /* 331 * realpath() is too darn slow, so fake 332 * it, by using what we know about /dev 333 * links: they are always of the form: 334 * <"../">+/devices/<path> 335 */ 336 p = buf; 337 while (strncmp(p, "../", 3) == 0) 338 p += 3; 339 340 if (p != buf) 341 p--; /* back up to get a slash */ 342 343 assert (*p == '/'); 344 345 if (strcmp(p, node_path) == 0) { 346 *logpp = strdup(linkpath); 347 if (*logpp == NULL) { 348 349 rv = CFGA_SATA_ALLOC_FAIL; 350 goto pp_cleanup; 351 } 352 353 found = B_TRUE; 354 } 355 } 356 } 357 358 free(linkpath); 359 free(buf); 360 free(real_path); 361 free(dep); 362 (void) closedir(dp); 363 364 if (err != 0) { 365 *l_errnop = err; 366 return (CFGA_SATA_INTERNAL_ERROR); 367 } 368 369 return (CFGA_SATA_OK); 370 371 pp_cleanup: 372 373 if (dp) 374 (void) closedir(dp); 375 if (dep) 376 free(dep); 377 if (linkpath) 378 free(linkpath); 379 if (buf) 380 free(buf); 381 if (real_path) 382 free(real_path); 383 if (*logpp) { 384 free(*logpp); 385 *logpp = NULL; 386 } 387 return (rv); 388 } 389 390 391 /* 392 * Given the index into a table (msgcvt_t) of messages, get the message 393 * string, converting it to the proper locale if necessary. 394 * NOTE: Indexes are defined in cfga_sata.h 395 */ 396 static const char * 397 get_msg(uint_t msg_index, msgcvt_t *msg_tbl, uint_t tbl_size) 398 { 399 if (msg_index >= tbl_size) { 400 msg_index = CFGA_SATA_UNKNOWN; 401 } 402 403 return ((msg_tbl[msg_index].intl) ? 404 dgettext(TEXT_DOMAIN, msg_tbl[msg_index].msgstr) : 405 msg_tbl[msg_index].msgstr); 406 } 407 408 /* 409 * Allocates and creates a message string (in *ret_str), 410 * by concatenating all the (char *) args together, in order. 411 * Last arg MUST be NULL. 412 */ 413 static void 414 set_msg(char **ret_str, ...) 415 { 416 char *str; 417 size_t total_len; 418 va_list valist; 419 420 va_start(valist, ret_str); 421 422 total_len = (*ret_str == NULL) ? 0 : strlen(*ret_str); 423 424 while ((str = va_arg(valist, char *)) != NULL) { 425 size_t len = strlen(str); 426 char *old_str = *ret_str; 427 428 *ret_str = (char *)realloc(*ret_str, total_len + len + 1); 429 if (*ret_str == NULL) { 430 /* We're screwed */ 431 free(old_str); 432 va_end(valist); 433 return; 434 } 435 436 (void) strcpy(*ret_str + total_len, str); 437 total_len += len; 438 } 439 440 va_end(valist); 441 } 442 443 /* 444 * Error message handling. 445 * For the rv passed in, looks up the corresponding error message string(s), 446 * internationalized if necessary, and concatenates it into a new 447 * memory buffer, and points *errstring to it. 448 * Note not all rvs will result in an error message return, as not all 449 * error conditions warrant a SATA-specific error message - for those 450 * conditions the cfgadm generic messages are sufficient. 451 * 452 * Some messages may display ap_id or errno, which is why they are passed 453 * in. 454 */ 455 456 cfga_err_t 457 sata_err_msg( 458 char **errstring, 459 cfga_sata_ret_t rv, 460 const char *ap_id, 461 int l_errno) 462 { 463 if (errstring == NULL) { 464 return (sata_msgs[rv].cfga_err); 465 } 466 467 /* 468 * Generate the appropriate SATA-specific error message(s) (if any). 469 */ 470 switch (rv) { 471 case CFGA_SATA_OK: 472 case CFGA_NACK: 473 /* Special case - do nothing. */ 474 break; 475 476 case CFGA_SATA_UNKNOWN: 477 case CFGA_SATA_DYNAMIC_AP: 478 case CFGA_SATA_INTERNAL_ERROR: 479 case CFGA_SATA_OPTIONS: 480 case CFGA_SATA_ALLOC_FAIL: 481 case CFGA_SATA_STATE: 482 case CFGA_SATA_PRIV: 483 case CFGA_SATA_OPNOTSUPP: 484 case CFGA_SATA_DATA_ERROR: 485 /* These messages require no additional strings passed. */ 486 set_msg(errstring, ERR_STR(rv), NULL); 487 break; 488 489 case CFGA_SATA_HWOPNOTSUPP: 490 /* hardware-specific help needed */ 491 set_msg(errstring, ERR_STR(rv), NULL); 492 set_msg(errstring, "\n", 493 dgettext(TEXT_DOMAIN, sata_help[HELP_HEADER]), NULL); 494 set_msg(errstring, sata_help[HELP_RESET_PORT], NULL); 495 set_msg(errstring, sata_help[HELP_RESET_DEVICE], NULL); 496 set_msg(errstring, sata_help[HELP_RESET_ALL], NULL); 497 set_msg(errstring, sata_help[HELP_PORT_ACTIVATE], NULL); 498 set_msg(errstring, sata_help[HELP_PORT_DEACTIVATE], NULL); 499 set_msg(errstring, sata_help[HELP_PORT_SELF_TEST], NULL); 500 set_msg(errstring, sata_help[HELP_CNTRL_SELF_TEST], NULL); 501 break; 502 503 case CFGA_SATA_AP: 504 case CFGA_SATA_PORT: 505 case CFGA_SATA_NOT_CONNECTED: 506 case CFGA_SATA_NOT_CONFIGURED: 507 case CFGA_SATA_ALREADY_CONNECTED: 508 case CFGA_SATA_ALREADY_CONFIGURED: 509 case CFGA_SATA_BUSY: 510 case CFGA_SATA_DEVLINK: 511 case CFGA_SATA_RCM_HANDLE: 512 case CFGA_SATA_RCM_ONLINE: 513 case CFGA_SATA_RCM_OFFLINE: 514 case CFGA_SATA_RCM_INFO: 515 case CFGA_SATA_DEV_CONFIGURE: 516 case CFGA_SATA_DEV_UNCONFIGURE: 517 case CFGA_SATA_DISCONNECTED: 518 /* These messages also print ap_id. */ 519 set_msg(errstring, ERR_STR(rv), "ap_id: ", ap_id, "", NULL); 520 break; 521 522 523 case CFGA_SATA_IOCTL: 524 case CFGA_SATA_NVLIST: 525 /* These messages also print errno. */ 526 { 527 char *errno_str = l_errno ? strerror(l_errno) : ""; 528 529 set_msg(errstring, ERR_STR(rv), errno_str, 530 l_errno ? "\n" : "", NULL); 531 break; 532 } 533 534 case CFGA_SATA_OPEN: 535 /* These messages also apid and errno. */ 536 { 537 char *errno_str = l_errno ? strerror(l_errno) : ""; 538 539 set_msg(errstring, ERR_STR(rv), "ap_id: ", ap_id, "\n", 540 errno_str, l_errno ? "\n" : "", NULL); 541 break; 542 } 543 544 default: 545 set_msg(errstring, ERR_STR(CFGA_SATA_INTERNAL_ERROR), NULL); 546 547 } /* end switch */ 548 549 550 /* 551 * Determine the proper error code to send back to the cfgadm library. 552 */ 553 return (sata_msgs[rv].cfga_err); 554 } 555 556 557 /* 558 * Entry points 559 */ 560 /* cfgadm entry point */ 561 /*ARGSUSED*/ 562 cfga_err_t 563 cfga_change_state( 564 cfga_cmd_t state_change_cmd, 565 const char *ap_id, 566 const char *options, 567 struct cfga_confirm *confp, 568 struct cfga_msg *msgp, 569 char **errstring, 570 cfga_flags_t flags) 571 { 572 int ret; 573 int len; 574 char *msg; 575 char *devpath; 576 nvlist_t *nvl = NULL; 577 ap_rstate_t rstate; 578 ap_ostate_t ostate; 579 devctl_hdl_t hdl = NULL; 580 cfga_sata_ret_t rv = CFGA_SATA_OK; 581 char *pdyn; 582 583 /* 584 * All sub-commands which can change state of device require 585 * root privileges. 586 */ 587 if (geteuid() != 0) { 588 rv = CFGA_SATA_PRIV; 589 goto bailout; 590 } 591 592 if ((rv = verify_params(ap_id, options, errstring)) != CFGA_SATA_OK) { 593 (void) cfga_help(msgp, options, flags); 594 goto bailout; 595 } 596 597 if ((rv = setup_for_devctl_cmd(ap_id, &hdl, &nvl, 598 DC_RDONLY)) != CFGA_SATA_OK) { 599 goto bailout; 600 } 601 602 switch (state_change_cmd) { 603 case CFGA_CMD_CONFIGURE: 604 if ((rv = port_state(hdl, nvl, &rstate, &ostate)) != 605 CFGA_SATA_OK) 606 goto bailout; 607 608 if (ostate == AP_OSTATE_CONFIGURED) { 609 rv = CFGA_SATA_ALREADY_CONFIGURED; 610 goto bailout; 611 } 612 /* Disallow dynamic AP name component */ 613 if (GET_DYN(ap_id) != NULL) { 614 rv = CFGA_SATA_INVALID_DEVNAME; 615 goto bailout; 616 } 617 618 if (rstate == AP_RSTATE_EMPTY) { 619 rv = CFGA_SATA_NOT_CONNECTED; 620 goto bailout; 621 } 622 rv = CFGA_SATA_OK; 623 624 if (devctl_ap_configure(hdl, nvl) != 0) { 625 rv = CFGA_SATA_DEV_CONFIGURE; 626 goto bailout; 627 } 628 629 devpath = sata_get_devicepath(ap_id); 630 if (devpath == NULL) { 631 int i; 632 /* 633 * Try for some time as SATA hotplug thread 634 * takes a while to create the path then 635 * eventually give up. 636 */ 637 for (i = 0; i < 12 && (devpath == NULL); i++) { 638 (void) sleep(6); 639 devpath = sata_get_devicepath(ap_id); 640 } 641 642 if (devpath == NULL) { 643 rv = CFGA_SATA_DEV_CONFIGURE; 644 break; 645 } 646 } 647 648 S_FREE(devpath); 649 break; 650 651 case CFGA_CMD_UNCONFIGURE: 652 if ((rv = port_state(hdl, nvl, &rstate, &ostate)) != 653 CFGA_SATA_OK) 654 goto bailout; 655 656 if (rstate != AP_RSTATE_CONNECTED) { 657 rv = CFGA_SATA_NOT_CONNECTED; 658 goto bailout; 659 } 660 661 if (ostate != AP_OSTATE_CONFIGURED) { 662 rv = CFGA_SATA_NOT_CONFIGURED; 663 goto bailout; 664 } 665 /* Strip off AP name dynamic component, if present */ 666 if ((pdyn = GET_DYN(ap_id)) != NULL) { 667 *pdyn = '\0'; 668 } 669 670 rv = CFGA_SATA_OK; 671 672 len = strlen(SATA_CONFIRM_DEVICE) + 673 strlen(SATA_CONFIRM_DEVICE_SUSPEND) + 674 strlen("Unconfigure") + strlen(ap_id); 675 if ((msg = (char *)calloc(len +3, 1)) != NULL) { 676 (void) snprintf(msg, len + 3, "Unconfigure" 677 " %s%s\n%s", 678 SATA_CONFIRM_DEVICE, ap_id, 679 SATA_CONFIRM_DEVICE_SUSPEND); 680 } 681 682 if (!sata_confirm(confp, msg)) { 683 free(msg); 684 rv = CFGA_SATA_NACK; 685 break; 686 } 687 free(msg); 688 689 devpath = sata_get_devicepath(ap_id); 690 if (devpath == NULL) { 691 (void) printf( 692 "cfga_change_state: get device path failed\n"); 693 rv = CFGA_SATA_DEV_UNCONFIGURE; 694 break; 695 } 696 697 if ((rv = sata_rcm_offline(ap_id, errstring, devpath, flags)) 698 != CFGA_SATA_OK) { 699 break; 700 } 701 702 ret = devctl_ap_unconfigure(hdl, nvl); 703 704 if (ret != 0) { 705 rv = CFGA_SATA_DEV_UNCONFIGURE; 706 if (errno == EBUSY) { 707 rv = CFGA_SATA_BUSY; 708 } 709 (void) sata_rcm_online(ap_id, errstring, devpath, 710 flags); 711 } else { 712 (void) sata_rcm_remove(ap_id, errstring, devpath, 713 flags); 714 715 } 716 S_FREE(devpath); 717 718 break; 719 720 case CFGA_CMD_DISCONNECT: 721 if ((rv = port_state(hdl, nvl, &rstate, &ostate)) != 722 CFGA_SATA_OK) 723 goto bailout; 724 725 if (rstate == AP_RSTATE_DISCONNECTED) { 726 rv = CFGA_SATA_DISCONNECTED; 727 goto bailout; 728 } 729 730 /* Strip off AP name dynamic component, if present */ 731 if ((pdyn = GET_DYN(ap_id)) != NULL) { 732 *pdyn = '\0'; 733 } 734 735 736 rv = CFGA_SATA_OK; /* other statuses don't matter */ 737 738 739 /* 740 * If the port originally with device attached and was 741 * unconfigured already, the devicepath for the sd will be 742 * removed. sata_get_devicepath in this case is not necessary. 743 */ 744 745 /* only call rcm_offline if the state was CONFIGURED */ 746 if (ostate == AP_OSTATE_CONFIGURED) { 747 devpath = sata_get_devicepath(ap_id); 748 if (devpath == NULL) { 749 (void) printf( 750 "cfga_change_state: get path failed\n"); 751 rv = CFGA_SATA_DEV_UNCONFIGURE; 752 break; 753 } 754 755 len = strlen(SATA_CONFIRM_DEVICE) + 756 strlen(SATA_CONFIRM_DEVICE_SUSPEND) + 757 strlen("Disconnect") + strlen(ap_id); 758 if ((msg = (char *)calloc(len +3, 1)) != NULL) { 759 (void) snprintf(msg, len + 3, 760 "Disconnect" 761 " %s%s\n%s", 762 SATA_CONFIRM_DEVICE, ap_id, 763 SATA_CONFIRM_DEVICE_SUSPEND); 764 } 765 if (!sata_confirm(confp, msg)) { 766 free(msg); 767 rv = CFGA_SATA_NACK; 768 break; 769 } 770 free(msg); 771 772 if ((rv = sata_rcm_offline(ap_id, errstring, 773 devpath, flags)) != CFGA_SATA_OK) { 774 break; 775 } 776 777 ret = devctl_ap_unconfigure(hdl, nvl); 778 if (ret != 0) { 779 (void) printf( 780 "devctl_ap_unconfigure failed\n"); 781 rv = CFGA_SATA_DEV_UNCONFIGURE; 782 if (errno == EBUSY) 783 rv = CFGA_SATA_BUSY; 784 (void) sata_rcm_online(ap_id, errstring, 785 devpath, flags); 786 S_FREE(devpath); 787 788 /* 789 * The current policy is that if unconfigure 790 * failed, do not continue with disconnect. 791 * If the port needs to be forced into the 792 * disconnect (shutdown) state, 793 * the -x sata_port_poweroff command should be 794 * used instead of -c disconnect 795 */ 796 break; 797 } else { 798 (void) printf("%s\n", 799 ERR_STR(CFGA_SATA_DEVICE_UNCONFIGURED)); 800 (void) sata_rcm_remove(ap_id, errstring, 801 devpath, flags); 802 } 803 S_FREE(devpath); 804 } else if (rstate == AP_RSTATE_CONNECTED || 805 rstate == AP_RSTATE_EMPTY) { 806 len = strlen(SATA_CONFIRM_PORT) + 807 strlen(SATA_CONFIRM_PORT_DISABLE) + 808 strlen("Deactivate Port") + strlen(ap_id); 809 if ((msg = (char *)calloc(len +3, 1)) != NULL) { 810 (void) snprintf(msg, len +3, 811 "Disconnect" 812 " %s%s\n%s", 813 SATA_CONFIRM_PORT, ap_id, 814 SATA_CONFIRM_PORT_DISABLE); 815 } 816 if (!sata_confirm(confp, msg)) { 817 free(msg); 818 rv = CFGA_SATA_NACK; 819 break; 820 } 821 } 822 ret = devctl_ap_disconnect(hdl, nvl); 823 if (ret != 0) { 824 rv = CFGA_SATA_IOCTL; 825 if (errno == EBUSY) { 826 rv = CFGA_SATA_BUSY; 827 } 828 } 829 break; 830 831 case CFGA_CMD_CONNECT: 832 if ((rv = port_state(hdl, nvl, &rstate, &ostate)) != 833 CFGA_SATA_OK) 834 goto bailout; 835 836 if (rstate == AP_RSTATE_CONNECTED) { 837 rv = CFGA_SATA_ALREADY_CONNECTED; 838 goto bailout; 839 } 840 841 len = strlen(SATA_CONFIRM_PORT) + 842 strlen(SATA_CONFIRM_PORT_ENABLE) + 843 strlen("Activate Port") + strlen(ap_id); 844 if ((msg = (char *)calloc(len +3, 1)) != NULL) { 845 (void) snprintf(msg, len +3, "Activate" 846 " %s%s\n%s", 847 SATA_CONFIRM_PORT, ap_id, 848 SATA_CONFIRM_PORT_ENABLE); 849 } 850 if (!sata_confirm(confp, msg)) { 851 rv = CFGA_SATA_NACK; 852 break; 853 } 854 855 /* Disallow dynamic AP name component */ 856 if (GET_DYN(ap_id) != NULL) { 857 rv = CFGA_SATA_INVALID_DEVNAME; 858 goto bailout; 859 } 860 861 ret = devctl_ap_connect(hdl, nvl); 862 if (ret != 0) { 863 rv = CFGA_SATA_IOCTL; 864 } else { 865 rv = CFGA_SATA_OK; 866 } 867 868 break; 869 870 case CFGA_CMD_LOAD: 871 case CFGA_CMD_UNLOAD: 872 (void) cfga_help(msgp, options, flags); 873 rv = CFGA_SATA_OPNOTSUPP; 874 break; 875 876 case CFGA_CMD_NONE: 877 default: 878 (void) cfga_help(msgp, options, flags); 879 rv = CFGA_SATA_INTERNAL_ERROR; 880 } 881 882 bailout: 883 cleanup_after_devctl_cmd(hdl, nvl); 884 885 return (sata_err_msg(errstring, rv, ap_id, errno)); 886 } 887 888 /* cfgadm entry point */ 889 cfga_err_t 890 cfga_private_func( 891 const char *func, 892 const char *ap_id, 893 const char *options, 894 struct cfga_confirm *confp, 895 struct cfga_msg *msgp, 896 char **errstring, 897 cfga_flags_t flags) 898 { 899 int len; 900 char *msg; 901 nvlist_t *list = NULL; 902 ap_ostate_t ostate; 903 ap_rstate_t rstate; 904 devctl_hdl_t hdl = NULL; 905 cfga_sata_ret_t rv; 906 char *str_p; 907 size_t size; 908 909 if ((rv = verify_params(ap_id, NULL, errstring)) != CFGA_SATA_OK) { 910 (void) cfga_help(msgp, options, flags); 911 return (sata_err_msg(errstring, rv, ap_id, errno)); 912 } 913 914 /* 915 * All subcommands which can change state of device require 916 * root privileges. 917 */ 918 if (geteuid() != 0) { 919 rv = CFGA_SATA_PRIV; 920 goto bailout; 921 } 922 923 if (func == NULL) { 924 (void) printf("No valid option specified\n"); 925 rv = CFGA_SATA_OPTIONS; 926 goto bailout; 927 } 928 929 if ((rv = setup_for_devctl_cmd(ap_id, &hdl, &list, 0)) != 930 CFGA_SATA_OK) { 931 goto bailout; 932 } 933 934 /* We do not care here about dynamic AP name component */ 935 if ((str_p = GET_DYN(ap_id)) != NULL) { 936 *str_p = '\0'; 937 } 938 939 rv = CFGA_SATA_OK; 940 941 if (strcmp(func, SATA_RESET_PORT) == 0) { 942 len = strlen(SATA_CONFIRM_PORT) + 943 strlen(SATA_CONFIRM_DEVICE_ABORT) + 944 strlen("Reset Port") + strlen(ap_id); 945 946 if ((msg = (char *)calloc(len +3, 1)) != NULL) { 947 (void) snprintf(msg, len +3, "Reset" 948 " %s%s\n%s", 949 SATA_CONFIRM_PORT, ap_id, 950 SATA_CONFIRM_DEVICE_ABORT); 951 } else { 952 rv = CFGA_SATA_NACK; 953 goto bailout; 954 } 955 956 if (!sata_confirm(confp, msg)) { 957 rv = CFGA_SATA_NACK; 958 goto bailout; 959 } 960 961 rv = do_control_ioctl(ap_id, SATA_CFGA_RESET_PORT, NULL, 962 (void **)&str_p, &size); 963 964 } else if (strcmp(func, SATA_RESET_DEVICE) == 0) { 965 if ((rv = port_state(hdl, list, &rstate, &ostate)) != 966 CFGA_SATA_OK) 967 goto bailout; 968 /* 969 * Reset device function requires device to be connected 970 */ 971 if (rstate != AP_RSTATE_CONNECTED) { 972 rv = CFGA_SATA_NOT_CONNECTED; 973 goto bailout; 974 } 975 976 len = strlen(SATA_CONFIRM_DEVICE) + 977 strlen(SATA_CONFIRM_DEVICE_ABORT) + 978 strlen("Reset Device") + strlen(ap_id); 979 980 if ((msg = (char *)calloc(len +3, 1)) != NULL) { 981 (void) snprintf(msg, len +3, "Reset" 982 " %s%s\n%s", 983 SATA_CONFIRM_DEVICE, ap_id, 984 SATA_CONFIRM_DEVICE_ABORT); 985 } else { 986 rv = CFGA_SATA_NACK; 987 goto bailout; 988 } 989 990 if (!sata_confirm(confp, msg)) { 991 rv = CFGA_SATA_NACK; 992 goto bailout; 993 } 994 995 rv = do_control_ioctl(ap_id, SATA_CFGA_RESET_DEVICE, NULL, 996 (void **)&str_p, &size); 997 998 } else if (strcmp(func, SATA_RESET_ALL) == 0) { 999 len = strlen(SATA_CONFIRM_CONTROLLER) + 1000 strlen(SATA_CONFIRM_CONTROLLER_ABORT) + 1001 strlen("Reset All") + strlen(ap_id); 1002 1003 if ((msg = (char *)calloc(len +3, 1)) != NULL) { 1004 (void) snprintf(msg, len +3, "Reset" 1005 " %s%s\n%s", 1006 SATA_CONFIRM_CONTROLLER, ap_id, 1007 SATA_CONFIRM_CONTROLLER_ABORT); 1008 } else { 1009 rv = CFGA_SATA_NACK; 1010 goto bailout; 1011 } 1012 1013 if (!sata_confirm(confp, msg)) { 1014 rv = CFGA_SATA_NACK; 1015 goto bailout; 1016 } 1017 rv = do_control_ioctl(ap_id, SATA_CFGA_RESET_ALL, NULL, 1018 (void **)&str_p, &size); 1019 1020 } else if (strcmp(func, SATA_PORT_DEACTIVATE) == 0) { 1021 len = strlen(SATA_CONFIRM_PORT) + 1022 strlen(SATA_CONFIRM_PORT_DISABLE) + 1023 strlen("Deactivate Port") + strlen(ap_id); 1024 1025 if ((msg = (char *)calloc(len +3, 1)) != NULL) { 1026 (void) snprintf(msg, len +3, "Deactivate" 1027 " %s%s\n%s", 1028 SATA_CONFIRM_PORT, ap_id, 1029 SATA_CONFIRM_PORT_DISABLE); 1030 } else { 1031 rv = CFGA_SATA_NACK; 1032 goto bailout; 1033 } 1034 if (!sata_confirm(confp, msg)) { 1035 rv = CFGA_SATA_NACK; 1036 goto bailout; 1037 } 1038 1039 rv = do_control_ioctl(ap_id, SATA_CFGA_PORT_DEACTIVATE, NULL, 1040 (void **)&str_p, &size); 1041 1042 } else if (strcmp(func, SATA_PORT_ACTIVATE) == 0) { 1043 len = strlen(SATA_CONFIRM_PORT) + 1044 strlen(SATA_CONFIRM_PORT_ENABLE) + 1045 strlen("Activate Port") + strlen(ap_id); 1046 1047 if ((msg = (char *)calloc(len +3, 1)) != NULL) { 1048 (void) snprintf(msg, len +3, "Activate" 1049 " %s%s\n%s", 1050 SATA_CONFIRM_PORT, ap_id, 1051 SATA_CONFIRM_PORT_ENABLE); 1052 } else { 1053 rv = CFGA_SATA_NACK; 1054 goto bailout; 1055 } 1056 if (!sata_confirm(confp, msg)) { 1057 rv = CFGA_SATA_NACK; 1058 goto bailout; 1059 } 1060 1061 rv = do_control_ioctl(ap_id, SATA_CFGA_PORT_ACTIVATE, 1062 NULL, (void **)&str_p, &size); 1063 goto bailout; 1064 1065 } else if (strcmp(func, SATA_PORT_SELF_TEST) == 0) { 1066 len = strlen(SATA_CONFIRM_PORT) + 1067 strlen(SATA_CONFIRM_DEVICE_SUSPEND) + 1068 strlen("Self Test Port") + strlen(ap_id); 1069 1070 if ((msg = (char *)calloc(len +3, 1)) != NULL) { 1071 (void) snprintf(msg, len +3, "Self Test" 1072 " %s%s\n%s", 1073 SATA_CONFIRM_PORT, ap_id, 1074 SATA_CONFIRM_DEVICE_SUSPEND); 1075 } else { 1076 rv = CFGA_SATA_NACK; 1077 goto bailout; 1078 } 1079 if (!sata_confirm(confp, msg)) { 1080 rv = CFGA_SATA_NACK; 1081 goto bailout; 1082 } 1083 1084 rv = do_control_ioctl(ap_id, SATA_CFGA_PORT_SELF_TEST, 1085 NULL, (void **)&str_p, &size); 1086 } else { 1087 /* Unrecognized operation request */ 1088 rv = CFGA_SATA_HWOPNOTSUPP; 1089 } 1090 1091 bailout: 1092 cleanup_after_devctl_cmd(hdl, list); 1093 1094 return (sata_err_msg(errstring, rv, ap_id, errno)); 1095 1096 } 1097 1098 /* cfgadm entry point */ 1099 /*ARGSUSED*/ 1100 cfga_err_t 1101 cfga_test( 1102 const char *ap_id, 1103 const char *options, 1104 struct cfga_msg *msgp, 1105 char **errstring, 1106 cfga_flags_t flags) 1107 { 1108 /* Should call ioctl for self test - phase 2 */ 1109 return (CFGA_OPNOTSUPP); 1110 } 1111 1112 1113 int 1114 sata_check_target_node(di_node_t node, void *arg) 1115 { 1116 char *minorpath; 1117 char *cp; 1118 1119 minorpath = di_devfs_minor_path(di_minor_next(node, DI_MINOR_NIL)); 1120 if (minorpath != NULL) { 1121 if (strstr(minorpath, arg) != NULL) { 1122 cp = strrchr(minorpath, (int)*MINOR_SEP); 1123 if (cp != NULL) { 1124 (void) strcpy(arg, cp); 1125 } 1126 free(minorpath); 1127 return (DI_WALK_TERMINATE); 1128 } 1129 free(minorpath); 1130 } 1131 return (DI_WALK_CONTINUE); 1132 } 1133 1134 1135 /* 1136 * The dynamic component buffer returned by this function has to be freed! 1137 */ 1138 int 1139 sata_make_dyncomp(const char *ap_id, char **dyncomp) 1140 { 1141 char *devpath = NULL; 1142 char *cp = NULL; 1143 int l_errno; 1144 char minor_path[MAXPATHLEN]; 1145 char name_part[MAXNAMELEN]; 1146 char *devlink = NULL; 1147 char *minor_portion = NULL; 1148 int deplen; 1149 int err; 1150 DIR *dp = NULL; 1151 struct stat sb; 1152 struct dirent *dep = NULL; 1153 struct dirent *newdep = NULL; 1154 char *p; 1155 1156 assert(dyncomp != NULL); 1157 1158 /* 1159 * Get target node path 1160 */ 1161 devpath = sata_get_devicepath(ap_id); 1162 if (devpath == NULL) { 1163 1164 (void) printf("cfga_list_ext: cannot locate target device\n"); 1165 return (CFGA_SATA_DYNAMIC_AP); 1166 1167 } else { 1168 1169 cp = strrchr(devpath, *PATH_SEP); 1170 assert(cp != NULL); 1171 *cp = 0; /* terminate path for opendir() */ 1172 1173 (void) strncpy(name_part, cp + 1, MAXNAMELEN); 1174 1175 /* 1176 * Using libdevinfo for this is overkill and kills 1177 * performance when many consumers are using libcfgadm 1178 * concurrently. 1179 */ 1180 if ((dp = opendir(devpath)) == NULL) { 1181 goto bailout; 1182 } 1183 1184 /* 1185 * deplen is large enough to fit the largest path- 1186 * struct dirent includes one byte (the terminator) 1187 * so we don't add 1 to the calculation here. 1188 */ 1189 deplen = pathconf(devpath, _PC_NAME_MAX); 1190 deplen = ((deplen <= 0) ? MAXNAMELEN : deplen) + 1191 sizeof (struct dirent); 1192 dep = (struct dirent *)malloc(deplen); 1193 if (dep == NULL) 1194 goto bailout; 1195 1196 while ((err = readdir_r(dp, dep, &newdep)) == 0 && 1197 newdep != NULL) { 1198 1199 assert(newdep == dep); 1200 1201 if (strcmp(dep->d_name, ".") == 0 || 1202 strcmp(dep->d_name, "..") == 0 || 1203 (minor_portion = strchr(dep->d_name, 1204 *MINOR_SEP)) == NULL) 1205 continue; 1206 1207 *minor_portion = 0; 1208 if (strcmp(dep->d_name, name_part) != 0) 1209 continue; 1210 *minor_portion = *MINOR_SEP; 1211 1212 (void) snprintf(minor_path, MAXPATHLEN, 1213 "%s/%s", devpath, dep->d_name); 1214 1215 if (stat(minor_path, &sb) < 0) 1216 continue; 1217 1218 if (S_ISBLK(sb.st_mode)) 1219 break; 1220 } 1221 1222 (void) closedir(dp); 1223 free(dep); 1224 free(devpath); 1225 1226 dp = NULL; 1227 dep = NULL; 1228 devpath = NULL; 1229 1230 /* 1231 * If there was an error, or we didn't exit the loop 1232 * by finding a block or character device, bail out. 1233 */ 1234 if (err != 0 || newdep == NULL) 1235 goto bailout; 1236 1237 /* 1238 * Look for links to the physical path in /dev/dsk, 1239 * since we ONLY looked for BLOCK devices above. 1240 */ 1241 (void) physpath_to_devlink("/dev/dsk", 1242 minor_path, &devlink, &l_errno); 1243 1244 /* postprocess and copy logical name here */ 1245 if (devlink != NULL) { 1246 /* 1247 * For disks, remove partition/slice info 1248 */ 1249 if ((cp = strstr(devlink, "dsk/")) != NULL) { 1250 /* cXtYdZ[(s[0..15])|(p[0..X])] */ 1251 if ((p = strchr(cp + 4, 'd')) != NULL) { 1252 p++; /* Skip the 'd' */ 1253 while (*p != 0 && isdigit(*p)) 1254 p++; 1255 *p = 0; 1256 } 1257 *dyncomp = strdup(cp); 1258 } 1259 1260 free(devlink); 1261 } 1262 1263 return (SATA_CFGA_OK); 1264 } 1265 1266 bailout: 1267 if (dp) 1268 (void) closedir(dp); 1269 if (devpath) 1270 free(devpath); 1271 if (dep) 1272 free(dep); 1273 return (CFGA_SATA_DYNAMIC_AP); 1274 } 1275 1276 /* cfgadm entry point */ 1277 /*ARGSUSED*/ 1278 cfga_err_t 1279 cfga_list_ext( 1280 const char *ap_id, 1281 cfga_list_data_t **ap_id_list, 1282 int *nlistp, 1283 const char *options, 1284 const char *listopts, 1285 char **errstring, 1286 cfga_flags_t flags) 1287 { 1288 int l_errno; 1289 char *ap_id_log = NULL; 1290 size_t size; 1291 nvlist_t *user_nvlist = NULL; 1292 devctl_hdl_t devctl_hdl = NULL; 1293 cfga_sata_ret_t rv = CFGA_SATA_OK; 1294 devctl_ap_state_t devctl_ap_state; 1295 char *pdyn; 1296 1297 1298 if ((rv = verify_params(ap_id, options, errstring)) != CFGA_SATA_OK) { 1299 (void) cfga_help(NULL, options, flags); 1300 goto bailout; 1301 } 1302 /* We do not care here about dynamic AP name component */ 1303 if ((pdyn = GET_DYN(ap_id)) != NULL) { 1304 *pdyn = '\0'; 1305 } 1306 1307 if (ap_id_list == NULL || nlistp == NULL) { 1308 rv = CFGA_SATA_DATA_ERROR; 1309 (void) cfga_help(NULL, options, flags); 1310 goto bailout; 1311 } 1312 1313 /* Get ap status */ 1314 if ((rv = setup_for_devctl_cmd(ap_id, &devctl_hdl, &user_nvlist, 1315 DC_RDONLY)) != CFGA_SATA_OK) { 1316 goto bailout; 1317 } 1318 1319 /* will call dc_cmd to send IOCTL to kernel */ 1320 if (devctl_ap_getstate(devctl_hdl, user_nvlist, 1321 &devctl_ap_state) == -1) { 1322 cleanup_after_devctl_cmd(devctl_hdl, user_nvlist); 1323 rv = CFGA_SATA_IOCTL; 1324 goto bailout; 1325 } 1326 1327 cleanup_after_devctl_cmd(devctl_hdl, user_nvlist); 1328 1329 /* 1330 * Create cfga_list_data_t struct. 1331 */ 1332 if ((*ap_id_list = 1333 (cfga_list_data_t *)malloc(sizeof (**ap_id_list))) == NULL) { 1334 rv = CFGA_SATA_ALLOC_FAIL; 1335 goto bailout; 1336 } 1337 *nlistp = 1; 1338 1339 /* 1340 * Rest of the code fills in the cfga_list_data_t struct. 1341 */ 1342 1343 /* Get /dev/cfg path to corresponding to the physical ap_id */ 1344 /* Remember ap_id_log must be freed */ 1345 rv = physpath_to_devlink(CFGA_DEV_DIR, (char *)ap_id, 1346 &ap_id_log, &l_errno); 1347 1348 if (rv != 0) { 1349 rv = CFGA_SATA_DEVLINK; 1350 goto bailout; 1351 } 1352 1353 /* Get logical ap_id corresponding to the physical */ 1354 if (ap_id_log == NULL || strstr(ap_id_log, CFGA_DEV_DIR) == NULL) { 1355 rv = CFGA_SATA_DEVLINK; 1356 goto bailout; 1357 } 1358 1359 (void) strlcpy((*ap_id_list)->ap_log_id, 1360 /* Strip off /dev/cfg/ */ ap_id_log + strlen(CFGA_DEV_DIR)+ 1, 1361 sizeof ((*ap_id_list)->ap_log_id)); 1362 1363 free(ap_id_log); 1364 ap_id_log = NULL; 1365 1366 (void) strlcpy((*ap_id_list)->ap_phys_id, ap_id, 1367 sizeof ((*ap_id_list)->ap_phys_id)); 1368 1369 switch (devctl_ap_state.ap_rstate) { 1370 case AP_RSTATE_EMPTY: 1371 (*ap_id_list)->ap_r_state = CFGA_STAT_EMPTY; 1372 break; 1373 1374 case AP_RSTATE_DISCONNECTED: 1375 (*ap_id_list)->ap_r_state = CFGA_STAT_DISCONNECTED; 1376 break; 1377 1378 case AP_RSTATE_CONNECTED: 1379 (*ap_id_list)->ap_r_state = CFGA_STAT_CONNECTED; 1380 break; 1381 1382 default: 1383 rv = CFGA_SATA_STATE; 1384 goto bailout; 1385 } 1386 1387 switch (devctl_ap_state.ap_ostate) { 1388 case AP_OSTATE_CONFIGURED: 1389 (*ap_id_list)->ap_o_state = CFGA_STAT_CONFIGURED; 1390 break; 1391 1392 case AP_OSTATE_UNCONFIGURED: 1393 (*ap_id_list)->ap_o_state = CFGA_STAT_UNCONFIGURED; 1394 break; 1395 1396 default: 1397 rv = CFGA_SATA_STATE; 1398 goto bailout; 1399 } 1400 1401 switch (devctl_ap_state.ap_condition) { 1402 case AP_COND_OK: 1403 (*ap_id_list)->ap_cond = CFGA_COND_OK; 1404 break; 1405 1406 case AP_COND_FAILING: 1407 (*ap_id_list)->ap_cond = CFGA_COND_FAILING; 1408 break; 1409 1410 case AP_COND_FAILED: 1411 (*ap_id_list)->ap_cond = CFGA_COND_FAILED; 1412 break; 1413 1414 case AP_COND_UNUSABLE: 1415 (*ap_id_list)->ap_cond = CFGA_COND_UNUSABLE; 1416 break; 1417 1418 case AP_COND_UNKNOWN: 1419 (*ap_id_list)->ap_cond = CFGA_COND_UNKNOWN; 1420 break; 1421 1422 default: 1423 rv = CFGA_SATA_STATE; 1424 goto bailout; 1425 } 1426 1427 (*ap_id_list)->ap_class[0] = '\0'; /* Filled by libcfgadm */ 1428 (*ap_id_list)->ap_busy = devctl_ap_state.ap_in_transition; 1429 (*ap_id_list)->ap_status_time = devctl_ap_state.ap_last_change; 1430 (*ap_id_list)->ap_info[0] = NULL; 1431 1432 if ((*ap_id_list)->ap_r_state == CFGA_STAT_CONNECTED) { 1433 char *str_p; 1434 int skip, i; 1435 1436 /* 1437 * Fill in the 'Information' field for the -v option 1438 * Model (MOD:) 1439 */ 1440 if ((rv = do_control_ioctl(ap_id, SATA_CFGA_GET_MODEL_INFO, 1441 NULL, (void **)&str_p, &size)) != CFGA_SATA_OK) { 1442 (void) printf( 1443 "SATA_CFGA_GET_MODULE_INFO ioctl failed\n"); 1444 goto bailout; 1445 } 1446 /* drop leading and trailing spaces */ 1447 skip = strspn(str_p, " "); 1448 for (i = size - 1; i >= 0; i--) { 1449 if (str_p[i] == '\040') 1450 str_p[i] = '\0'; 1451 else if (str_p[i] != '\0') 1452 break; 1453 } 1454 1455 (void) strlcpy((*ap_id_list)->ap_info, "Mod: ", 1456 sizeof ((*ap_id_list)->ap_info)); 1457 (void) strlcat((*ap_id_list)->ap_info, str_p + skip, 1458 sizeof ((*ap_id_list)->ap_info)); 1459 1460 free(str_p); 1461 1462 /* 1463 * Fill in the 'Information' field for the -v option 1464 * Firmware revision (FREV:) 1465 */ 1466 if ((rv = do_control_ioctl(ap_id, 1467 SATA_CFGA_GET_REVFIRMWARE_INFO, 1468 NULL, (void **)&str_p, &size)) != CFGA_SATA_OK) { 1469 (void) printf( 1470 "SATA_CFGA_GET_REVFIRMWARE_INFO ioctl failed\n"); 1471 goto bailout; 1472 } 1473 /* drop leading and trailing spaces */ 1474 skip = strspn(str_p, " "); 1475 for (i = size - 1; i >= 0; i--) { 1476 if (str_p[i] == '\040') 1477 str_p[i] = '\0'; 1478 else if (str_p[i] != '\0') 1479 break; 1480 } 1481 (void) strlcat((*ap_id_list)->ap_info, " FRev: ", 1482 sizeof ((*ap_id_list)->ap_info)); 1483 (void) strlcat((*ap_id_list)->ap_info, str_p + skip, 1484 sizeof ((*ap_id_list)->ap_info)); 1485 1486 free(str_p); 1487 1488 1489 /* 1490 * Fill in the 'Information' field for the -v option 1491 * Serial Number (SN:) 1492 */ 1493 if ((rv = do_control_ioctl(ap_id, 1494 SATA_CFGA_GET_SERIALNUMBER_INFO, 1495 NULL, (void **)&str_p, &size)) != CFGA_SATA_OK) { 1496 (void) printf( 1497 "SATA_CFGA_GET_SERIALNUMBER_INFO ioctl failed\n"); 1498 goto bailout; 1499 } 1500 /* drop leading and trailing spaces */ 1501 skip = strspn(str_p, " "); 1502 for (i = size - 1; i >= 0; i--) { 1503 if (str_p[i] == '\040') 1504 str_p[i] = '\0'; 1505 else if (str_p[i] != '\0') 1506 break; 1507 } 1508 (void) strlcat((*ap_id_list)->ap_info, " SN: ", 1509 sizeof ((*ap_id_list)->ap_info)); 1510 (void) strlcat((*ap_id_list)->ap_info, str_p + skip, 1511 sizeof ((*ap_id_list)->ap_info)); 1512 1513 free(str_p); 1514 1515 1516 1517 /* Fill in ap_type which is collected from HBA driver */ 1518 /* call do_control_ioctl TBD */ 1519 if ((rv = do_control_ioctl(ap_id, SATA_CFGA_GET_AP_TYPE, NULL, 1520 (void **)&str_p, &size)) != CFGA_SATA_OK) { 1521 (void) printf( 1522 "SATA_CFGA_GET_AP_TYPE ioctl failed\n"); 1523 goto bailout; 1524 } 1525 1526 (void) strlcpy((*ap_id_list)->ap_type, str_p, 1527 sizeof ((*ap_id_list)->ap_type)); 1528 1529 free(str_p); 1530 1531 if ((*ap_id_list)->ap_o_state == CFGA_STAT_CONFIGURED) { 1532 1533 char *dyncomp = NULL; 1534 1535 /* 1536 * This is the case where we need to generate 1537 * a dynamic component of the ap_id, i.e. device. 1538 */ 1539 rv = sata_make_dyncomp(ap_id, &dyncomp); 1540 if (rv != CFGA_SATA_OK) 1541 goto bailout; 1542 if (dyncomp != NULL) { 1543 (void) strcat((*ap_id_list)->ap_log_id, 1544 DYN_SEP); 1545 (void) strlcat((*ap_id_list)->ap_log_id, 1546 dyncomp, 1547 sizeof ((*ap_id_list)->ap_log_id)); 1548 free(dyncomp); 1549 } 1550 } 1551 1552 } else { 1553 /* Change it when port multiplier is supported */ 1554 (void) strlcpy((*ap_id_list)->ap_type, "sata-port", 1555 sizeof ((*ap_id_list)->ap_type)); 1556 } 1557 1558 return (sata_err_msg(errstring, rv, ap_id, errno)); 1559 1560 bailout: 1561 if (*ap_id_list != NULL) { 1562 free(*ap_id_list); 1563 } 1564 if (ap_id_log != NULL) { 1565 free(ap_id_log); 1566 } 1567 1568 return (sata_err_msg(errstring, rv, ap_id, errno)); 1569 } 1570 /* 1571 * This routine accepts a string adn prints it using 1572 * the message print routine argument. 1573 */ 1574 static void 1575 cfga_msg(struct cfga_msg *msgp, const char *str) 1576 { 1577 int len; 1578 char *q; 1579 1580 if (msgp == NULL || msgp->message_routine == NULL) { 1581 (void) printf("cfga_msg: NULL msgp\n"); 1582 return; 1583 } 1584 1585 if ((len = strlen(str)) == 0) { 1586 (void) printf("cfga_msg: null str\n"); 1587 return; 1588 } 1589 1590 if ((q = (char *)calloc(len + 1, 1)) == NULL) { 1591 perror("cfga_msg" 1592 ); 1593 return; 1594 } 1595 1596 (void) strcpy(q, str); 1597 (*msgp->message_routine)(msgp->appdata_ptr, q); 1598 1599 free(q); 1600 } 1601 1602 /* cfgadm entry point */ 1603 /* ARGSUSED */ 1604 cfga_err_t 1605 cfga_help(struct cfga_msg *msgp, const char *options, cfga_flags_t flags) 1606 { 1607 if (options != NULL) { 1608 cfga_msg(msgp, dgettext(TEXT_DOMAIN, sata_help[HELP_UNKNOWN])); 1609 cfga_msg(msgp, options); 1610 } 1611 cfga_msg(msgp, dgettext(TEXT_DOMAIN, sata_help[HELP_HEADER])); 1612 cfga_msg(msgp, sata_help[HELP_CONFIG]); 1613 cfga_msg(msgp, sata_help[HELP_RESET_PORT]); 1614 cfga_msg(msgp, sata_help[HELP_RESET_DEVICE]); 1615 cfga_msg(msgp, sata_help[HELP_RESET_ALL]); 1616 cfga_msg(msgp, sata_help[HELP_PORT_ACTIVATE]); 1617 cfga_msg(msgp, sata_help[HELP_PORT_DEACTIVATE]); 1618 cfga_msg(msgp, sata_help[HELP_PORT_SELF_TEST]); 1619 cfga_msg(msgp, sata_help[HELP_CNTRL_SELF_TEST]); 1620 1621 return (CFGA_OK); 1622 } 1623 1624 1625 /* 1626 * Ensure the ap_id passed is in the correct (physical ap_id) form: 1627 * path/device:xx[.xx] 1628 * where xx is a one or two-digit number. 1629 * 1630 * Note the library always calls the plugin with a physical ap_id. 1631 */ 1632 static int 1633 verify_valid_apid(const char *ap_id) 1634 { 1635 char *l_ap_id; 1636 1637 if (ap_id == NULL) 1638 return (-1); 1639 1640 l_ap_id = strrchr(ap_id, (int)*MINOR_SEP); 1641 l_ap_id++; 1642 1643 if (strspn(l_ap_id, "0123456789.") != strlen(l_ap_id)) { 1644 /* Bad characters in the ap_id */ 1645 return (-1); 1646 } 1647 1648 if (strstr(l_ap_id, "..") != NULL) { 1649 /* ap_id has 1..2 or more than 2 dots */ 1650 return (-1); 1651 } 1652 1653 return (0); 1654 } 1655 1656 1657 1658 /* 1659 * Verify the params passed in are valid. 1660 */ 1661 static cfga_sata_ret_t 1662 verify_params( 1663 const char *ap_id, 1664 const char *options, 1665 char **errstring) 1666 { 1667 char *pdyn, *lap_id; 1668 int rv; 1669 1670 if (errstring != NULL) { 1671 *errstring = NULL; 1672 } 1673 1674 if (options != NULL) { 1675 return (CFGA_SATA_OPTIONS); 1676 } 1677 1678 /* Strip dynamic AP name component if it is present. */ 1679 lap_id = strdup(ap_id); 1680 if (lap_id == NULL) { 1681 return (CFGA_SATA_ALLOC_FAIL); 1682 } 1683 if ((pdyn = GET_DYN(lap_id)) != NULL) { 1684 *pdyn = '\0'; 1685 } 1686 1687 if (verify_valid_apid(lap_id) != 0) { 1688 rv = CFGA_SATA_AP; 1689 } else { 1690 rv = CFGA_SATA_OK; 1691 } 1692 free(lap_id); 1693 1694 return (rv); 1695 } 1696 1697 /* 1698 * Takes a validated ap_id and extracts the port number. 1699 * For now, we do not support port multiplier port . 1700 */ 1701 static cfga_sata_ret_t 1702 get_port_num(const char *ap_id, uint32_t *port) 1703 { 1704 char *port_nbr_str; 1705 char *temp; 1706 uint32_t cport; 1707 uint32_t pmport = 0; /* port multiplier not supported yet */ 1708 int pmport_qual = 0; /* port multiplier not supported yet */ 1709 1710 port_nbr_str = strrchr(ap_id, (int)*MINOR_SEP) + strlen(MINOR_SEP); 1711 if ((temp = strrchr(ap_id, (int)*PORT_SEPARATOR)) != 0) { 1712 port_nbr_str = temp + strlen(PORT_SEPARATOR); 1713 } 1714 1715 errno = 0; 1716 cport = strtol(port_nbr_str, NULL, 10); 1717 if ((cport & ~SATA_CFGA_CPORT_MASK) != 0 || errno != 0) 1718 return (CFGA_SATA_PORT); 1719 1720 *port = cport + (pmport << SATA_CFGA_PMPORT_SHIFT) + pmport_qual; 1721 1722 return (CFGA_SATA_OK); 1723 } 1724 1725 /* 1726 * Pair of routines to set up for/clean up after a devctl_ap_* lib call. 1727 */ 1728 static void 1729 cleanup_after_devctl_cmd(devctl_hdl_t devctl_hdl, nvlist_t *user_nvlist) 1730 { 1731 if (user_nvlist != NULL) { 1732 nvlist_free(user_nvlist); 1733 } 1734 if (devctl_hdl != NULL) { 1735 devctl_release(devctl_hdl); 1736 } 1737 } 1738 1739 static cfga_sata_ret_t 1740 setup_for_devctl_cmd( 1741 const char *ap_id, 1742 devctl_hdl_t *devctl_hdl, 1743 nvlist_t **user_nvlistp, 1744 uint_t oflag) 1745 { 1746 1747 uint_t port; 1748 cfga_sata_ret_t rv = CFGA_SATA_OK; 1749 char *lap_id, *pdyn; 1750 1751 lap_id = strdup(ap_id); 1752 if (lap_id == NULL) 1753 return (CFGA_SATA_ALLOC_FAIL); 1754 if ((pdyn = GET_DYN(lap_id)) != NULL) { 1755 *pdyn = '\0'; 1756 } 1757 1758 /* Get a devctl handle to pass to the devctl_ap_XXX functions */ 1759 if ((*devctl_hdl = devctl_ap_acquire((char *)lap_id, oflag)) == NULL) { 1760 (void) fprintf(stderr, "[libcfgadm:sata] " 1761 "setup_for_devctl_cmd: devctl_ap_acquire failed: %s\n", 1762 strerror(errno)); 1763 rv = CFGA_SATA_DEVCTL; 1764 goto bailout; 1765 } 1766 1767 /* Set up nvlist to pass the port number down to the driver */ 1768 if (nvlist_alloc(user_nvlistp, NV_UNIQUE_NAME_TYPE, NULL) != 0) { 1769 *user_nvlistp = NULL; 1770 rv = CFGA_SATA_NVLIST; 1771 (void) printf("nvlist_alloc failed\n"); 1772 goto bailout; 1773 } 1774 1775 /* 1776 * Get port id, for Port Multiplier port, things could be a little bit 1777 * complicated because of "port.port" format in ap_id, thus for 1778 * port multiplier port, port number should be coded as 32bit int 1779 * with the sig 16 bit as sata channel number, least 16 bit as 1780 * the port number of sata port multiplier port. 1781 */ 1782 if ((rv = get_port_num(lap_id, &port)) != CFGA_SATA_OK) { 1783 (void) printf( 1784 "setup_for_devctl_cmd: get_port_num, errno: %d\n", 1785 errno); 1786 goto bailout; 1787 } 1788 1789 /* Creates an int32_t entry */ 1790 if (nvlist_add_int32(*user_nvlistp, PORT, port) == -1) { 1791 (void) printf("nvlist_add_int32 failed\n"); 1792 rv = CFGA_SATA_NVLIST; 1793 goto bailout; 1794 } 1795 1796 free(lap_id); 1797 return (rv); 1798 1799 bailout: 1800 free(lap_id); 1801 (void) cleanup_after_devctl_cmd(*devctl_hdl, *user_nvlistp); 1802 1803 return (rv); 1804 } 1805 1806 1807 static cfga_sata_ret_t 1808 port_state(devctl_hdl_t hdl, nvlist_t *list, 1809 ap_rstate_t *rstate, ap_ostate_t *ostate) 1810 { 1811 devctl_ap_state_t devctl_ap_state; 1812 1813 if (devctl_ap_getstate(hdl, list, &devctl_ap_state) == -1) { 1814 (void) printf("devctl_ap_getstate failed, errno: %d\n", errno); 1815 return (CFGA_SATA_IOCTL); 1816 } 1817 *rstate = devctl_ap_state.ap_rstate; 1818 *ostate = devctl_ap_state.ap_ostate; 1819 return (CFGA_SATA_OK); 1820 } 1821 1822 1823 /* 1824 * Given a subcommand to the DEVCTL_AP_CONTROL ioctl, rquest the size of 1825 * the data to be returned, allocate a buffer, then get the data. 1826 * Returns *descrp (which must be freed) and size. 1827 * 1828 * Note SATA_DESCR_TYPE_STRING returns an ASCII NULL-terminated string, 1829 * not a string descr. 1830 */ 1831 cfga_sata_ret_t 1832 do_control_ioctl(const char *ap_id, sata_cfga_apctl_t subcommand, uint_t arg, 1833 void **descrp, size_t *sizep) 1834 { 1835 int fd = -1; 1836 uint_t port; 1837 uint32_t local_size; 1838 cfga_sata_ret_t rv = CFGA_SATA_OK; 1839 struct sata_ioctl_data ioctl_data; 1840 1841 assert(descrp != NULL); 1842 *descrp = NULL; 1843 assert(sizep != NULL); 1844 1845 if ((rv = get_port_num(ap_id, &port)) != CFGA_SATA_OK) { 1846 goto bailout; 1847 } 1848 1849 if ((fd = open(ap_id, O_RDONLY)) == -1) { 1850 (void) printf("do_control_ioctl: open failed: errno:%d\n", 1851 errno); 1852 rv = CFGA_SATA_OPEN; 1853 if (errno == EBUSY) { 1854 rv = CFGA_SATA_BUSY; 1855 } 1856 goto bailout; 1857 } 1858 1859 ioctl_data.cmd = subcommand; 1860 ioctl_data.port = port; 1861 ioctl_data.misc_arg = (uint_t)arg; 1862 1863 /* 1864 * Find out how large a buf we need to get the data. 1865 * Note the ioctls only accept/return a 32-bit int for a get_size 1866 * to avoid 32/64 and BE/LE issues. 1867 */ 1868 if ((subcommand == SATA_CFGA_GET_AP_TYPE) || 1869 (subcommand == SATA_CFGA_GET_DEVICE_PATH) || 1870 (subcommand == SATA_CFGA_GET_MODEL_INFO) || 1871 (subcommand == SATA_CFGA_GET_REVFIRMWARE_INFO) || 1872 (subcommand == SATA_CFGA_GET_SERIALNUMBER_INFO)) { 1873 ioctl_data.get_size = B_TRUE; 1874 ioctl_data.buf = (caddr_t)&local_size; 1875 ioctl_data.bufsiz = sizeof (local_size); 1876 1877 if (ioctl(fd, DEVCTL_AP_CONTROL, &ioctl_data) != 0) { 1878 perror("ioctl failed (size)"); 1879 rv = CFGA_SATA_IOCTL; 1880 goto bailout; 1881 } 1882 *sizep = local_size; 1883 1884 if (local_size == 0) { 1885 (void) printf("zero length data\n"); 1886 rv = CFGA_SATA_ZEROLEN; 1887 goto bailout; 1888 } 1889 if ((*descrp = malloc(*sizep)) == NULL) { 1890 (void) printf("do_control_ioctl: malloc failed\n"); 1891 rv = CFGA_SATA_ALLOC_FAIL; 1892 goto bailout; 1893 } 1894 } else { 1895 *sizep = 0; 1896 } 1897 ioctl_data.get_size = B_FALSE; 1898 ioctl_data.buf = *descrp; 1899 ioctl_data.bufsiz = *sizep; 1900 1901 /* Execute IOCTL */ 1902 1903 if (ioctl(fd, DEVCTL_AP_CONTROL, &ioctl_data) != 0) { 1904 rv = CFGA_SATA_IOCTL; 1905 goto bailout; 1906 } 1907 1908 (void) close(fd); 1909 1910 return (rv); 1911 1912 bailout: 1913 if (fd != -1) { 1914 (void) close(fd); 1915 } 1916 if (*descrp != NULL) { 1917 free(*descrp); 1918 *descrp = NULL; 1919 } 1920 1921 if (rv == CFGA_SATA_IOCTL && errno == EBUSY) { 1922 rv = CFGA_SATA_BUSY; 1923 } 1924 1925 return (rv); 1926 } 1927 1928 1929 static int 1930 sata_confirm(struct cfga_confirm *confp, char *msg) 1931 { 1932 int rval; 1933 1934 if (confp == NULL || confp->confirm == NULL) { 1935 return (0); 1936 } 1937 rval = (*confp->confirm)(confp->appdata_ptr, msg); 1938 1939 return (rval); 1940 } 1941 1942 1943 static char * 1944 sata_get_devicepath(const char *ap_id) 1945 { 1946 char *devpath = NULL; 1947 size_t size; 1948 cfga_sata_ret_t rv; 1949 1950 rv = do_control_ioctl(ap_id, SATA_CFGA_GET_DEVICE_PATH, NULL, 1951 (void **)&devpath, &size); 1952 1953 if (rv == CFGA_SATA_OK) { 1954 return (devpath); 1955 } else { 1956 return ((char *)NULL); 1957 } 1958 1959 } 1960