dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / lib / cfgadm_plugins / sata / common / cfga_sata.c
blob41453cdd6e91d2ed4000fe86b8d94a5242cbd150
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #include <sys/param.h>
28 #include <sys/stat.h>
29 #include <errno.h>
30 #include <string.h>
31 #include <dirent.h>
32 #include "cfga_sata.h"
35 * This file contains the entry points to the plug-in as defined in the
36 * config_admin(3X) man page.
40 * Set the version number for the cfgadm library's use.
42 int cfga_version = CFGA_HSL_V2;
44 enum {
45 HELP_HEADER = 1,
46 HELP_CONFIG,
47 HELP_RESET_PORT,
48 HELP_RESET_DEVICE,
49 HELP_RESET_ALL,
50 HELP_PORT_DEACTIVATE,
51 HELP_PORT_ACTIVATE,
52 HELP_PORT_SELF_TEST,
53 HELP_CNTRL_SELF_TEST,
54 HELP_UNKNOWN
57 /* SATA specific help messages */
58 static char *sata_help[] = {
59 NULL,
60 "SATA specific commands:\n",
61 " cfgadm -c [configure|unconfigure|disconnect|connect] ap_id "
62 "[ap_id...]\n",
63 " cfgadm -x sata_reset_port ap_id [ap_id...]\n",
64 " cfgadm -x sata_reset_device ap_id [ap_id...]\n",
65 " cfgadm -x sata_reset_all ap_id\n",
66 " cfgadm -x sata_port_deactivate ap_id [ap_id...]\n",
67 " cfgadm -x sata_port_activate ap_id [ap_id...]\n",
68 " cfgadm -x sata_port_self_test ap_id [ap_id...]\n",
69 " cfgadm -t ap_id\n",
70 "\tunknown command or option:\n",
71 NULL
72 }; /* End help messages */
76 * Messages.
78 static msgcvt_t sata_msgs[] = {
79 /* CFGA_SATA_OK */
80 { CVT, CFGA_OK, "" },
82 /* CFGA_SATA_NACK */
83 { CVT, CFGA_NACK, "" },
85 /* CFGA_SATA_DEVICE_UNCONFIGURED */
86 { CVT, CFGA_OK, "Device unconfigured prior to disconnect" },
88 /* CFGA_SATA_UNKNOWN / CFGA_LIB_ERROR -> "Library error" */
89 { CVT, CFGA_LIB_ERROR, "Unknown message; internal error" },
91 /* CFGA_SATA_INTERNAL_ERROR / CFGA_LIB_ERROR -> "Library error" */
92 { CVT, CFGA_LIB_ERROR, "Internal error" },
94 /* CFGA_SATA_DATA_ERROR / CFGA_DATA_ERROR -> "Data error" */
95 { CVT, CFGA_DATA_ERROR, "cfgadm data error" },
97 /* CFGA_SATA_OPTIONS / CFGA_ERROR -> "Hardware specific failure" */
98 { CVT, CFGA_ERROR, "Hardware specific option not supported" },
100 /* CFGA_SATA_HWOPNOTSUPP / CFGA_ERROR -> "Hardware specific failure" */
101 { CVT, CFGA_ERROR, "Hardware specific operation not supported" },
104 * CFGA_SATA_DYNAMIC_AP /
105 * CFGA_LIB_ERROR -> "Configuration operation invalid"
107 { CVT, CFGA_INVAL, "Cannot identify attached device" },
109 /* CFGA_SATA_AP / CFGA_APID_NOEXIST -> "Attachment point not found" */
110 { CVT, CFGA_APID_NOEXIST, "" },
112 /* CFGA_SATA_PORT / CFGA_LIB_ERROR -> "Library error" */
113 { CVT, CFGA_LIB_ERROR, "Cannot determine sata port number for " },
115 /* CFGA_SATA_DEVCTL / CFGA_LIB_ERROR -> "Library error" */
116 { CVT, CFGA_LIB_ERROR, "Internal error: "
117 "Cannot allocate devctl handle " },
120 * CFGA_SATA_DEV_CONFIGURE /
121 * CFGA_ERROR -> "Hardware specific failure"
123 { CVT, CFGA_ERROR, "Failed to config device at " },
126 * CFGA_SATA_DEV_UNCONFIGURE /
127 * CFGA_ERROR -> "Hardware specific failure"
129 { CVT, CFGA_ERROR, "Failed to unconfig device at " },
132 * CFGA_SATA_DISCONNECTED
133 * CFGA_INVAL -> "Configuration operation invalid"
135 { CVT, CFGA_INVAL, "Port already disconnected " },
138 * CFGA_SATA_NOT_CONNECTED
139 * CFGA_INVAL -> "Configuration operation invalid"
141 { CVT, CFGA_INVAL, "No device connected to " },
144 * CFGA_SATA_NOT_CONFIGURED /
145 * CFGA_INVAL -> "Configuration operation invalid"
147 { CVT, CFGA_INVAL, "No device configured at " },
150 * CFGA_SATA_ALREADY_CONNECTED /
151 * CFGA_INVAL -> "Configuration operation invalid"
153 { CVT, CFGA_INVAL, "Device already connected to " },
156 * CFGA_SATA_ALREADY_CONFIGURED /
157 * CFGA_INVAL -> "Configuration operation invalid"
159 { CVT, CFGA_INVAL, "Device already configured at " },
162 * CFGA_SATA_INVALID_DEVNAME /
163 * CFGA_INVAL -> "Configuration operation invalid"
165 { CVT, CFGA_INVAL, "Cannot specify device name" },
167 /* CFGA_SATA_OPEN / CFGA_LIB_ERROR -> "Library error" */
168 { CVT, CFGA_LIB_ERROR, "Cannot open " },
170 /* CFGA_SATA_IOCTL / CFGA_ERROR -> "Hardware specific failure" */
171 { CVT, CFGA_ERROR, "Driver ioctl failed " },
174 * CFGA_SATA_BUSY /
175 * CFGA_SYSTEM_BUSY -> "System is busy, try again"
177 { CVT, CFGA_SYSTEM_BUSY, "" },
179 /* CFGA_SATA_ALLOC_FAIL / CFGA_LIB_ERROR -> "Library error" */
180 { CVT, CFGA_LIB_ERROR, "Memory allocation failure" },
183 * CFGA_SATA_OPNOTSUPP /
184 * CFGA_OPNOTSUPP -> "Configuration operation not supported"
186 { CVT, CFGA_OPNOTSUPP, "Operation not supported" },
188 /* CFGA_SATA_DEVLINK / CFGA_LIB_ERROR -> "Library error" */
189 { CVT, CFGA_LIB_ERROR, "Could not find /dev/cfg link for " },
191 /* CFGA_SATA_STATE / CFGA_LIB_ERROR -> "Library error" */
192 { CVT, CFGA_LIB_ERROR, "Internal error: Unrecognized ap state" },
194 /* CFGA_SATA_PRIV / CFGA_PRIV -> "Insufficient privileges" */
195 { CVT, CFGA_PRIV, "" },
197 /* CFGA_SATA_NVLIST / CFGA_ERROR -> "Hardware specific failure" */
198 { CVT, CFGA_ERROR, "Internal error (nvlist)" },
200 /* CFGA_SATA_ZEROLEN / CFGA_ERROR -> "Hardware specific failure" */
201 { CVT, CFGA_ERROR, "Internal error (zerolength string)" },
203 /* CFGA_SATA_RCM_HANDLE / CFGA_ERROR -> "Hardware specific failure" */
204 { CVT, CFGA_ERROR, "cannot get RCM handle"},
207 * CFGA_SATA_RCM_ONLINE /
208 * CFGA_SYSTEM_BUSY -> "System is busy, try again"
210 { CVT, CFGA_SYSTEM_BUSY, "failed to online: "},
213 * CFGA_SATA_RCM_OFFLINE /
214 * CFGA_SYSTEM_BUSY -> "System is busy, try again"
216 { CVT, CFGA_SYSTEM_BUSY, "failed to offline: "},
218 /* CFGA_SATA_RCM_INFO / CFGA_ERROR -> "Hardware specific failure" */
219 { CVT, CFGA_ERROR, "failed to query: "}
221 }; /* End error messages */
223 static cfga_sata_ret_t
224 verify_params(const char *ap_id, const char *options, char **errstring);
227 static cfga_sata_ret_t
228 setup_for_devctl_cmd(const char *ap_id, devctl_hdl_t *devctl_hdl,
229 nvlist_t **user_nvlistp, uint_t oflag);
231 static cfga_sata_ret_t
232 port_state(devctl_hdl_t hdl, nvlist_t *list,
233 ap_rstate_t *rstate, ap_ostate_t *ostate);
235 static cfga_sata_ret_t
236 do_control_ioctl(const char *ap_id, sata_cfga_apctl_t subcommand, uint_t arg,
237 void **descrp, size_t *sizep);
239 static void
240 cleanup_after_devctl_cmd(devctl_hdl_t devctl_hdl, nvlist_t *user_nvlist);
242 static char *
243 sata_get_devicepath(const char *ap_id);
245 static int
246 sata_confirm(struct cfga_confirm *confp, char *msg);
248 static cfga_sata_ret_t
249 get_port_num(const char *ap_id, uint32_t *port);
251 /* Utilities */
253 static cfga_sata_ret_t
254 physpath_to_devlink(const char *basedir, const char *node_path,
255 char **logpp, int *l_errnop)
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;
270 * Using libdevinfo for this is overkill and kills performance
271 * when multiple consumers of libcfgadm are executing
272 * concurrently.
274 if ((dp = opendir(basedir)) == NULL) {
275 *l_errnop = errno;
276 return (CFGA_SATA_INTERNAL_ERROR);
279 linkpath = malloc(PATH_MAX);
280 buf = malloc(PATH_MAX);
281 real_path = malloc(PATH_MAX);
283 deplen = pathconf(basedir, _PC_NAME_MAX);
284 deplen = (deplen <= 0 ? MAXNAMELEN : deplen) +
285 sizeof (struct dirent);
286 dep = (struct dirent *)malloc(deplen);
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;
295 *logpp = NULL;
297 while (!found && (err = readdir_r(dp, dep, &newdep)) == 0 &&
298 newdep != NULL) {
300 assert(newdep == dep);
302 if (strcmp(dep->d_name, ".") == 0 ||
303 strcmp(dep->d_name, "..") == 0)
304 continue;
306 (void) snprintf(linkpath, MAXPATHLEN,
307 "%s/%s", basedir, dep->d_name);
309 if (lstat(linkpath, &sb) < 0)
310 continue;
312 if (S_ISDIR(sb.st_mode)) {
314 if ((rv = physpath_to_devlink(linkpath, node_path,
315 logpp, l_errnop)) != CFGA_SATA_OK) {
317 goto pp_cleanup;
320 if (*logpp != NULL)
321 found = B_TRUE;
323 } else if (S_ISLNK(sb.st_mode)) {
325 bzero(buf, PATH_MAX);
326 if (readlink(linkpath, buf, PATH_MAX) < 0)
327 continue;
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>
336 p = buf;
337 while (strncmp(p, "../", 3) == 0)
338 p += 3;
340 if (p != buf)
341 p--; /* back up to get a slash */
343 assert (*p == '/');
345 if (strcmp(p, node_path) == 0) {
346 *logpp = strdup(linkpath);
347 if (*logpp == NULL) {
349 rv = CFGA_SATA_ALLOC_FAIL;
350 goto pp_cleanup;
353 found = B_TRUE;
358 free(linkpath);
359 free(buf);
360 free(real_path);
361 free(dep);
362 (void) closedir(dp);
364 if (err != 0) {
365 *l_errnop = err;
366 return (CFGA_SATA_INTERNAL_ERROR);
369 return (CFGA_SATA_OK);
371 pp_cleanup:
373 if (dp)
374 (void) closedir(dp);
375 free(dep);
376 free(linkpath);
377 free(buf);
378 free(real_path);
379 if (*logpp) {
380 free(*logpp);
381 *logpp = NULL;
383 return (rv);
388 * Given the index into a table (msgcvt_t) of messages, get the message
389 * string, converting it to the proper locale if necessary.
390 * NOTE: Indexes are defined in cfga_sata.h
392 static const char *
393 get_msg(uint_t msg_index, msgcvt_t *msg_tbl, uint_t tbl_size)
395 if (msg_index >= tbl_size) {
396 msg_index = CFGA_SATA_UNKNOWN;
399 return ((msg_tbl[msg_index].intl) ?
400 dgettext(TEXT_DOMAIN, msg_tbl[msg_index].msgstr) :
401 msg_tbl[msg_index].msgstr);
405 * Allocates and creates a message string (in *ret_str),
406 * by concatenating all the (char *) args together, in order.
407 * Last arg MUST be NULL.
409 static void
410 set_msg(char **ret_str, ...)
412 char *str;
413 size_t total_len;
414 va_list valist;
416 va_start(valist, ret_str);
418 total_len = (*ret_str == NULL) ? 0 : strlen(*ret_str);
420 while ((str = va_arg(valist, char *)) != NULL) {
421 size_t len = strlen(str);
422 char *old_str = *ret_str;
424 *ret_str = (char *)realloc(*ret_str, total_len + len + 1);
425 if (*ret_str == NULL) {
426 /* We're screwed */
427 free(old_str);
428 va_end(valist);
429 return;
432 (void) strcpy(*ret_str + total_len, str);
433 total_len += len;
436 va_end(valist);
440 * Error message handling.
441 * For the rv passed in, looks up the corresponding error message string(s),
442 * internationalized if necessary, and concatenates it into a new
443 * memory buffer, and points *errstring to it.
444 * Note not all rvs will result in an error message return, as not all
445 * error conditions warrant a SATA-specific error message - for those
446 * conditions the cfgadm generic messages are sufficient.
448 * Some messages may display ap_id or errno, which is why they are passed
449 * in.
452 cfga_err_t
453 sata_err_msg(
454 char **errstring,
455 cfga_sata_ret_t rv,
456 const char *ap_id,
457 int l_errno)
459 if (errstring == NULL) {
460 return (sata_msgs[rv].cfga_err);
464 * Generate the appropriate SATA-specific error message(s) (if any).
466 switch (rv) {
467 case CFGA_SATA_OK:
468 case CFGA_NACK:
469 /* Special case - do nothing. */
470 break;
472 case CFGA_SATA_UNKNOWN:
473 case CFGA_SATA_DYNAMIC_AP:
474 case CFGA_SATA_INTERNAL_ERROR:
475 case CFGA_SATA_OPTIONS:
476 case CFGA_SATA_ALLOC_FAIL:
477 case CFGA_SATA_STATE:
478 case CFGA_SATA_PRIV:
479 case CFGA_SATA_OPNOTSUPP:
480 case CFGA_SATA_DATA_ERROR:
481 /* These messages require no additional strings passed. */
482 set_msg(errstring, ERR_STR(rv), NULL);
483 break;
485 case CFGA_SATA_HWOPNOTSUPP:
486 /* hardware-specific help needed */
487 set_msg(errstring, ERR_STR(rv), NULL);
488 set_msg(errstring, "\n",
489 dgettext(TEXT_DOMAIN, sata_help[HELP_HEADER]), NULL);
490 set_msg(errstring, sata_help[HELP_RESET_PORT], NULL);
491 set_msg(errstring, sata_help[HELP_RESET_DEVICE], NULL);
492 set_msg(errstring, sata_help[HELP_RESET_ALL], NULL);
493 set_msg(errstring, sata_help[HELP_PORT_ACTIVATE], NULL);
494 set_msg(errstring, sata_help[HELP_PORT_DEACTIVATE], NULL);
495 set_msg(errstring, sata_help[HELP_PORT_SELF_TEST], NULL);
496 set_msg(errstring, sata_help[HELP_CNTRL_SELF_TEST], NULL);
497 break;
499 case CFGA_SATA_AP:
500 case CFGA_SATA_PORT:
501 case CFGA_SATA_NOT_CONNECTED:
502 case CFGA_SATA_NOT_CONFIGURED:
503 case CFGA_SATA_ALREADY_CONNECTED:
504 case CFGA_SATA_ALREADY_CONFIGURED:
505 case CFGA_SATA_BUSY:
506 case CFGA_SATA_DEVLINK:
507 case CFGA_SATA_RCM_HANDLE:
508 case CFGA_SATA_RCM_ONLINE:
509 case CFGA_SATA_RCM_OFFLINE:
510 case CFGA_SATA_RCM_INFO:
511 case CFGA_SATA_DEV_CONFIGURE:
512 case CFGA_SATA_DEV_UNCONFIGURE:
513 case CFGA_SATA_DISCONNECTED:
514 /* These messages also print ap_id. */
515 set_msg(errstring, ERR_STR(rv), "ap_id: ", ap_id, "", NULL);
516 break;
519 case CFGA_SATA_IOCTL:
520 case CFGA_SATA_NVLIST:
521 /* These messages also print errno. */
523 char *errno_str = l_errno ? strerror(l_errno) : "";
525 set_msg(errstring, ERR_STR(rv), errno_str,
526 l_errno ? "\n" : "", NULL);
527 break;
530 case CFGA_SATA_OPEN:
531 /* These messages also apid and errno. */
533 char *errno_str = l_errno ? strerror(l_errno) : "";
535 set_msg(errstring, ERR_STR(rv), "ap_id: ", ap_id, "\n",
536 errno_str, l_errno ? "\n" : "", NULL);
537 break;
540 default:
541 set_msg(errstring, ERR_STR(CFGA_SATA_INTERNAL_ERROR), NULL);
543 } /* end switch */
547 * Determine the proper error code to send back to the cfgadm library.
549 return (sata_msgs[rv].cfga_err);
554 * Entry points
556 /* cfgadm entry point */
557 /*ARGSUSED*/
558 cfga_err_t
559 cfga_change_state(
560 cfga_cmd_t state_change_cmd,
561 const char *ap_id,
562 const char *options,
563 struct cfga_confirm *confp,
564 struct cfga_msg *msgp,
565 char **errstring,
566 cfga_flags_t flags)
568 int ret;
569 int len;
570 char *msg;
571 char *devpath;
572 nvlist_t *nvl = NULL;
573 ap_rstate_t rstate;
574 ap_ostate_t ostate;
575 devctl_hdl_t hdl = NULL;
576 cfga_sata_ret_t rv = CFGA_SATA_OK;
577 char *pdyn;
578 char *str_type;
579 size_t size;
580 boolean_t pmult = B_FALSE;
583 * All sub-commands which can change state of device require
584 * root privileges.
586 if (geteuid() != 0) {
587 rv = CFGA_SATA_PRIV;
588 goto bailout;
591 if ((rv = verify_params(ap_id, options, errstring)) != CFGA_SATA_OK) {
592 (void) cfga_help(msgp, options, flags);
593 goto bailout;
596 if ((rv = setup_for_devctl_cmd(ap_id, &hdl, &nvl,
597 DC_RDONLY)) != CFGA_SATA_OK) {
598 goto bailout;
602 * Checking device type. A port multiplier is not configurable - it is
603 * already configured as soon as it is connected.
605 if ((rv = do_control_ioctl(ap_id, SATA_CFGA_GET_AP_TYPE, 0,
606 (void **)&str_type, &size)) != CFGA_SATA_OK) {
607 /* no such deivce */
608 goto bailout;
610 if (strncmp(str_type, "sata-pmult", sizeof ("sata-pmult")) == 0) {
611 pmult = B_TRUE;
614 switch (state_change_cmd) {
615 case CFGA_CMD_CONFIGURE:
616 if (pmult == B_TRUE) {
617 rv = CFGA_SATA_HWOPNOTSUPP;
618 goto bailout;
621 if ((rv = port_state(hdl, nvl, &rstate, &ostate)) !=
622 CFGA_SATA_OK)
623 goto bailout;
625 if (ostate == AP_OSTATE_CONFIGURED) {
626 rv = CFGA_SATA_ALREADY_CONFIGURED;
627 goto bailout;
629 /* Disallow dynamic AP name component */
630 if (GET_DYN(ap_id) != NULL) {
631 rv = CFGA_SATA_INVALID_DEVNAME;
632 goto bailout;
635 if (rstate == AP_RSTATE_EMPTY) {
636 rv = CFGA_SATA_NOT_CONNECTED;
637 goto bailout;
639 rv = CFGA_SATA_OK;
641 if (devctl_ap_configure(hdl, nvl) != 0) {
642 rv = CFGA_SATA_DEV_CONFIGURE;
643 goto bailout;
646 devpath = sata_get_devicepath(ap_id);
647 if (devpath == NULL) {
648 int i;
650 * Try for some time as SATA hotplug thread
651 * takes a while to create the path then
652 * eventually give up.
654 for (i = 0; i < 12 && (devpath == NULL); i++) {
655 (void) sleep(6);
656 devpath = sata_get_devicepath(ap_id);
659 if (devpath == NULL) {
660 rv = CFGA_SATA_DEV_CONFIGURE;
661 break;
665 S_FREE(devpath);
666 break;
668 case CFGA_CMD_UNCONFIGURE:
669 if (pmult == B_TRUE) {
670 rv = CFGA_SATA_HWOPNOTSUPP;
671 goto bailout;
674 if ((rv = port_state(hdl, nvl, &rstate, &ostate)) !=
675 CFGA_SATA_OK)
676 goto bailout;
678 if (rstate != AP_RSTATE_CONNECTED) {
679 rv = CFGA_SATA_NOT_CONNECTED;
680 goto bailout;
683 if (ostate != AP_OSTATE_CONFIGURED) {
684 rv = CFGA_SATA_NOT_CONFIGURED;
685 goto bailout;
687 /* Strip off AP name dynamic component, if present */
688 if ((pdyn = GET_DYN(ap_id)) != NULL) {
689 *pdyn = '\0';
692 rv = CFGA_SATA_OK;
694 len = strlen(SATA_CONFIRM_DEVICE) +
695 strlen(SATA_CONFIRM_DEVICE_SUSPEND) +
696 strlen("Unconfigure") + strlen(ap_id);
697 if ((msg = (char *)calloc(len +3, 1)) != NULL) {
698 (void) snprintf(msg, len + 3, "Unconfigure"
699 " %s%s\n%s",
700 SATA_CONFIRM_DEVICE, ap_id,
701 SATA_CONFIRM_DEVICE_SUSPEND);
704 if (!sata_confirm(confp, msg)) {
705 free(msg);
706 rv = CFGA_SATA_NACK;
707 break;
709 free(msg);
711 devpath = sata_get_devicepath(ap_id);
712 if (devpath == NULL) {
713 (void) printf(
714 "cfga_change_state: get device path failed\n");
715 rv = CFGA_SATA_DEV_UNCONFIGURE;
716 break;
719 if ((rv = sata_rcm_offline(ap_id, errstring, devpath, flags))
720 != CFGA_SATA_OK) {
721 break;
724 ret = devctl_ap_unconfigure(hdl, nvl);
726 if (ret != 0) {
727 rv = CFGA_SATA_DEV_UNCONFIGURE;
728 if (errno == EBUSY) {
729 rv = CFGA_SATA_BUSY;
731 (void) sata_rcm_online(ap_id, errstring, devpath,
732 flags);
733 } else {
734 (void) sata_rcm_remove(ap_id, errstring, devpath,
735 flags);
738 S_FREE(devpath);
740 break;
742 case CFGA_CMD_DISCONNECT:
743 if ((rv = port_state(hdl, nvl, &rstate, &ostate)) !=
744 CFGA_SATA_OK)
745 goto bailout;
747 if (rstate == AP_RSTATE_DISCONNECTED) {
748 rv = CFGA_SATA_DISCONNECTED;
749 goto bailout;
752 /* Strip off AP name dynamic component, if present */
753 if ((pdyn = GET_DYN(ap_id)) != NULL) {
754 *pdyn = '\0';
758 rv = CFGA_SATA_OK; /* other statuses don't matter */
761 * If the port originally with device attached and was
762 * unconfigured already, the devicepath for the sd will be
763 * removed. sata_get_devicepath in this case is not necessary.
765 /* only call rcm_offline if the state was CONFIGURED */
766 if (ostate == AP_OSTATE_CONFIGURED &&
767 pmult == B_FALSE) {
768 devpath = sata_get_devicepath(ap_id);
769 if (devpath == NULL) {
770 (void) printf(
771 "cfga_change_state: get path failed\n");
772 rv = CFGA_SATA_DEV_UNCONFIGURE;
773 break;
776 len = strlen(SATA_CONFIRM_DEVICE) +
777 strlen(SATA_CONFIRM_DEVICE_SUSPEND) +
778 strlen("Disconnect") + strlen(ap_id);
779 if ((msg = (char *)calloc(len +3, 1)) != NULL) {
780 (void) snprintf(msg, len + 3,
781 "Disconnect"
782 " %s%s\n%s",
783 SATA_CONFIRM_DEVICE, ap_id,
784 SATA_CONFIRM_DEVICE_SUSPEND);
786 if (!sata_confirm(confp, msg)) {
787 free(msg);
788 rv = CFGA_SATA_NACK;
789 break;
791 free(msg);
793 if ((rv = sata_rcm_offline(ap_id, errstring,
794 devpath, flags)) != CFGA_SATA_OK) {
795 break;
798 ret = devctl_ap_unconfigure(hdl, nvl);
799 if (ret != 0) {
800 (void) printf(
801 "devctl_ap_unconfigure failed\n");
802 rv = CFGA_SATA_DEV_UNCONFIGURE;
803 if (errno == EBUSY)
804 rv = CFGA_SATA_BUSY;
805 (void) sata_rcm_online(ap_id, errstring,
806 devpath, flags);
807 S_FREE(devpath);
810 * The current policy is that if unconfigure
811 * failed, do not continue with disconnect.
812 * If the port needs to be forced into the
813 * disconnect (shutdown) state,
814 * the -x sata_port_poweroff command should be
815 * used instead of -c disconnect
817 break;
818 } else {
819 (void) printf("%s\n",
820 ERR_STR(CFGA_SATA_DEVICE_UNCONFIGURED));
821 (void) sata_rcm_remove(ap_id, errstring,
822 devpath, flags);
824 S_FREE(devpath);
825 } else if (rstate == AP_RSTATE_CONNECTED ||
826 rstate == AP_RSTATE_EMPTY) {
827 len = strlen(SATA_CONFIRM_PORT) +
828 strlen(SATA_CONFIRM_PORT_DISABLE) +
829 strlen("Deactivate Port") + strlen(ap_id);
830 if ((msg = (char *)calloc(len +3, 1)) != NULL) {
831 (void) snprintf(msg, len +3,
832 "Disconnect"
833 " %s%s\n%s",
834 SATA_CONFIRM_PORT, ap_id,
835 SATA_CONFIRM_PORT_DISABLE);
837 if (!sata_confirm(confp, msg)) {
838 free(msg);
839 rv = CFGA_SATA_NACK;
840 break;
843 ret = devctl_ap_disconnect(hdl, nvl);
844 if (ret != 0) {
845 rv = CFGA_SATA_IOCTL;
846 if (errno == EBUSY) {
847 rv = CFGA_SATA_BUSY;
850 break;
852 case CFGA_CMD_CONNECT:
853 if ((rv = port_state(hdl, nvl, &rstate, &ostate)) !=
854 CFGA_SATA_OK)
855 goto bailout;
857 if (rstate == AP_RSTATE_CONNECTED) {
858 rv = CFGA_SATA_ALREADY_CONNECTED;
859 goto bailout;
862 len = strlen(SATA_CONFIRM_PORT) +
863 strlen(SATA_CONFIRM_PORT_ENABLE) +
864 strlen("Activate Port") + strlen(ap_id);
865 if ((msg = (char *)calloc(len +3, 1)) != NULL) {
866 (void) snprintf(msg, len +3, "Activate"
867 " %s%s\n%s",
868 SATA_CONFIRM_PORT, ap_id,
869 SATA_CONFIRM_PORT_ENABLE);
871 if (!sata_confirm(confp, msg)) {
872 rv = CFGA_SATA_NACK;
873 break;
876 /* Disallow dynamic AP name component */
877 if (GET_DYN(ap_id) != NULL) {
878 rv = CFGA_SATA_INVALID_DEVNAME;
879 goto bailout;
882 ret = devctl_ap_connect(hdl, nvl);
883 if (ret != 0) {
884 rv = CFGA_SATA_IOCTL;
885 } else {
886 rv = CFGA_SATA_OK;
889 break;
891 case CFGA_CMD_LOAD:
892 case CFGA_CMD_UNLOAD:
893 (void) cfga_help(msgp, options, flags);
894 rv = CFGA_SATA_OPNOTSUPP;
895 break;
897 case CFGA_CMD_NONE:
898 default:
899 (void) cfga_help(msgp, options, flags);
900 rv = CFGA_SATA_INTERNAL_ERROR;
903 bailout:
904 cleanup_after_devctl_cmd(hdl, nvl);
906 return (sata_err_msg(errstring, rv, ap_id, errno));
909 /* cfgadm entry point */
910 cfga_err_t
911 cfga_private_func(
912 const char *func,
913 const char *ap_id,
914 const char *options,
915 struct cfga_confirm *confp,
916 struct cfga_msg *msgp,
917 char **errstring,
918 cfga_flags_t flags)
920 int len;
921 char *msg;
922 nvlist_t *list = NULL;
923 ap_ostate_t ostate;
924 ap_rstate_t rstate;
925 devctl_hdl_t hdl = NULL;
926 cfga_sata_ret_t rv;
927 char *str_p;
928 size_t size;
930 if ((rv = verify_params(ap_id, NULL, errstring)) != CFGA_SATA_OK) {
931 (void) cfga_help(msgp, options, flags);
932 return (sata_err_msg(errstring, rv, ap_id, errno));
936 * All subcommands which can change state of device require
937 * root privileges.
939 if (geteuid() != 0) {
940 rv = CFGA_SATA_PRIV;
941 goto bailout;
944 if (func == NULL) {
945 (void) printf("No valid option specified\n");
946 rv = CFGA_SATA_OPTIONS;
947 goto bailout;
950 if ((rv = setup_for_devctl_cmd(ap_id, &hdl, &list, 0)) !=
951 CFGA_SATA_OK) {
952 goto bailout;
955 /* We do not care here about dynamic AP name component */
956 if ((str_p = GET_DYN(ap_id)) != NULL) {
957 *str_p = '\0';
960 rv = CFGA_SATA_OK;
962 if (strcmp(func, SATA_RESET_PORT) == 0) {
963 len = strlen(SATA_CONFIRM_PORT) +
964 strlen(SATA_CONFIRM_DEVICE_ABORT) +
965 strlen("Reset Port") + strlen(ap_id);
967 if ((msg = (char *)calloc(len +3, 1)) != NULL) {
968 (void) snprintf(msg, len +3, "Reset"
969 " %s%s\n%s",
970 SATA_CONFIRM_PORT, ap_id,
971 SATA_CONFIRM_DEVICE_ABORT);
972 } else {
973 rv = CFGA_SATA_NACK;
974 goto bailout;
977 if (!sata_confirm(confp, msg)) {
978 rv = CFGA_SATA_NACK;
979 goto bailout;
982 rv = do_control_ioctl(ap_id, SATA_CFGA_RESET_PORT, 0,
983 (void **)&str_p, &size);
985 } else if (strcmp(func, SATA_RESET_DEVICE) == 0) {
986 if ((rv = port_state(hdl, list, &rstate, &ostate)) !=
987 CFGA_SATA_OK)
988 goto bailout;
990 * Reset device function requires device to be connected
992 if (rstate != AP_RSTATE_CONNECTED) {
993 rv = CFGA_SATA_NOT_CONNECTED;
994 goto bailout;
997 len = strlen(SATA_CONFIRM_DEVICE) +
998 strlen(SATA_CONFIRM_DEVICE_ABORT) +
999 strlen("Reset Device") + strlen(ap_id);
1001 if ((msg = (char *)calloc(len +3, 1)) != NULL) {
1002 (void) snprintf(msg, len +3, "Reset"
1003 " %s%s\n%s",
1004 SATA_CONFIRM_DEVICE, ap_id,
1005 SATA_CONFIRM_DEVICE_ABORT);
1006 } else {
1007 rv = CFGA_SATA_NACK;
1008 goto bailout;
1011 if (!sata_confirm(confp, msg)) {
1012 rv = CFGA_SATA_NACK;
1013 goto bailout;
1016 rv = do_control_ioctl(ap_id, SATA_CFGA_RESET_DEVICE, 0,
1017 (void **)&str_p, &size);
1019 } else if (strcmp(func, SATA_RESET_ALL) == 0) {
1020 len = strlen(SATA_CONFIRM_CONTROLLER) +
1021 strlen(SATA_CONFIRM_CONTROLLER_ABORT) +
1022 strlen("Reset All") + strlen(ap_id);
1024 if ((msg = (char *)calloc(len +3, 1)) != NULL) {
1025 (void) snprintf(msg, len +3, "Reset"
1026 " %s%s\n%s",
1027 SATA_CONFIRM_CONTROLLER, ap_id,
1028 SATA_CONFIRM_CONTROLLER_ABORT);
1029 } else {
1030 rv = CFGA_SATA_NACK;
1031 goto bailout;
1034 if (!sata_confirm(confp, msg)) {
1035 rv = CFGA_SATA_NACK;
1036 goto bailout;
1038 rv = do_control_ioctl(ap_id, SATA_CFGA_RESET_ALL, 0,
1039 (void **)&str_p, &size);
1041 } else if (strcmp(func, SATA_PORT_DEACTIVATE) == 0) {
1042 len = strlen(SATA_CONFIRM_PORT) +
1043 strlen(SATA_CONFIRM_PORT_DISABLE) +
1044 strlen("Deactivate Port") + strlen(ap_id);
1046 if ((msg = (char *)calloc(len +3, 1)) != NULL) {
1047 (void) snprintf(msg, len +3, "Deactivate"
1048 " %s%s\n%s",
1049 SATA_CONFIRM_PORT, ap_id,
1050 SATA_CONFIRM_PORT_DISABLE);
1051 } else {
1052 rv = CFGA_SATA_NACK;
1053 goto bailout;
1055 if (!sata_confirm(confp, msg)) {
1056 rv = CFGA_SATA_NACK;
1057 goto bailout;
1060 rv = do_control_ioctl(ap_id, SATA_CFGA_PORT_DEACTIVATE, 0,
1061 (void **)&str_p, &size);
1063 } else if (strcmp(func, SATA_PORT_ACTIVATE) == 0) {
1064 len = strlen(SATA_CONFIRM_PORT) +
1065 strlen(SATA_CONFIRM_PORT_ENABLE) +
1066 strlen("Activate Port") + strlen(ap_id);
1068 if ((msg = (char *)calloc(len +3, 1)) != NULL) {
1069 (void) snprintf(msg, len +3, "Activate"
1070 " %s%s\n%s",
1071 SATA_CONFIRM_PORT, ap_id,
1072 SATA_CONFIRM_PORT_ENABLE);
1073 } else {
1074 rv = CFGA_SATA_NACK;
1075 goto bailout;
1077 if (!sata_confirm(confp, msg)) {
1078 rv = CFGA_SATA_NACK;
1079 goto bailout;
1082 rv = do_control_ioctl(ap_id, SATA_CFGA_PORT_ACTIVATE,
1083 0, (void **)&str_p, &size);
1084 goto bailout;
1086 } else if (strcmp(func, SATA_PORT_SELF_TEST) == 0) {
1087 len = strlen(SATA_CONFIRM_PORT) +
1088 strlen(SATA_CONFIRM_DEVICE_SUSPEND) +
1089 strlen("Self Test Port") + strlen(ap_id);
1091 if ((msg = (char *)calloc(len +3, 1)) != NULL) {
1092 (void) snprintf(msg, len +3, "Self Test"
1093 " %s%s\n%s",
1094 SATA_CONFIRM_PORT, ap_id,
1095 SATA_CONFIRM_DEVICE_SUSPEND);
1096 } else {
1097 rv = CFGA_SATA_NACK;
1098 goto bailout;
1100 if (!sata_confirm(confp, msg)) {
1101 rv = CFGA_SATA_NACK;
1102 goto bailout;
1105 rv = do_control_ioctl(ap_id, SATA_CFGA_PORT_SELF_TEST,
1106 0, (void **)&str_p, &size);
1107 } else {
1108 /* Unrecognized operation request */
1109 rv = CFGA_SATA_HWOPNOTSUPP;
1112 bailout:
1113 cleanup_after_devctl_cmd(hdl, list);
1115 return (sata_err_msg(errstring, rv, ap_id, errno));
1119 /* cfgadm entry point */
1120 /*ARGSUSED*/
1121 cfga_err_t
1122 cfga_test(
1123 const char *ap_id,
1124 const char *options,
1125 struct cfga_msg *msgp,
1126 char **errstring,
1127 cfga_flags_t flags)
1129 /* Should call ioctl for self test - phase 2 */
1130 return (CFGA_OPNOTSUPP);
1135 sata_check_target_node(di_node_t node, void *arg)
1137 char *minorpath;
1138 char *cp;
1140 minorpath = di_devfs_minor_path(di_minor_next(node, DI_MINOR_NIL));
1141 if (minorpath != NULL) {
1142 if (strstr(minorpath, arg) != NULL) {
1143 cp = strrchr(minorpath, (int)*MINOR_SEP);
1144 if (cp != NULL) {
1145 (void) strcpy(arg, cp);
1147 free(minorpath);
1148 return (DI_WALK_TERMINATE);
1150 free(minorpath);
1152 return (DI_WALK_CONTINUE);
1155 struct chk_dev {
1156 int c_isblk;
1157 char *c_minor;
1160 /*ARGSUSED*/
1161 static int
1162 chk_dev_fcn(di_node_t node, di_minor_t minor, void *arg)
1164 char *mn;
1165 struct chk_dev *chkp = (struct chk_dev *)arg;
1167 mn = di_minor_name(minor);
1168 if (mn == NULL)
1169 return (DI_WALK_CONTINUE);
1171 if (strcmp(mn, chkp->c_minor) != 0)
1172 return (DI_WALK_CONTINUE);
1174 chkp->c_isblk = di_minor_spectype(minor) == S_IFBLK ? 1 : 0;
1176 return (DI_WALK_TERMINATE);
1180 * Don't use devfs if stat() in /devices fails. Use libdevinfo instead.
1181 * Retired devices don't show up in devfs.
1183 * Returns:
1184 * 1 - minor exists and is of type BLK
1185 * 0 - minor does not exist or is not of type BLK.
1187 static int
1188 is_devinfo_blk(char *minor_path)
1190 char *minor_portion;
1191 struct chk_dev chk_dev;
1192 di_node_t node;
1193 int rv;
1196 * prune minor path for di_init() - no /devices prefix and no minor name
1198 if (strncmp(minor_path, "/devices/", strlen("/devices/")) != 0)
1199 return (0);
1201 minor_portion = strrchr(minor_path, *MINOR_SEP);
1202 if (minor_portion == NULL)
1203 return (0);
1205 *minor_portion = 0;
1207 node = di_init(minor_path + strlen("/devices"), DINFOMINOR);
1209 *minor_portion = *MINOR_SEP;
1211 if (node == DI_NODE_NIL)
1212 return (0);
1214 chk_dev.c_isblk = 0;
1215 chk_dev.c_minor = minor_portion + 1;
1217 rv = di_walk_minor(node, NULL, 0, &chk_dev, chk_dev_fcn);
1219 di_fini(node);
1221 if (rv == 0 && chk_dev.c_isblk)
1222 return (1);
1223 else
1224 return (0);
1228 * The dynamic component buffer returned by this function has to be freed!
1231 sata_make_dyncomp(const char *ap_id, char **dyncomp, const char *type)
1233 char *devpath = NULL;
1234 char *cp = NULL;
1235 int l_errno;
1236 char minor_path[MAXPATHLEN];
1237 char name_part[MAXNAMELEN];
1238 char *devlink = NULL;
1239 char *minor_portion = NULL;
1240 int deplen;
1241 int err;
1242 DIR *dp = NULL;
1243 struct stat sb;
1244 struct dirent *dep = NULL;
1245 struct dirent *newdep = NULL;
1246 char *p;
1248 assert(dyncomp != NULL);
1251 * Get target node path
1253 devpath = sata_get_devicepath(ap_id);
1254 if (devpath == NULL) {
1256 (void) printf("cfga_list_ext: cannot locate target device\n");
1257 return (CFGA_SATA_DYNAMIC_AP);
1259 } else {
1261 cp = strrchr(devpath, *PATH_SEP);
1262 assert(cp != NULL);
1263 *cp = 0; /* terminate path for opendir() */
1265 (void) strncpy(name_part, cp + 1, MAXNAMELEN);
1268 * Using libdevinfo for this is overkill and kills
1269 * performance when many consumers are using libcfgadm
1270 * concurrently.
1272 if ((dp = opendir(devpath)) == NULL) {
1273 goto bailout;
1277 * deplen is large enough to fit the largest path-
1278 * struct dirent includes one byte (the terminator)
1279 * so we don't add 1 to the calculation here.
1281 deplen = pathconf(devpath, _PC_NAME_MAX);
1282 deplen = ((deplen <= 0) ? MAXNAMELEN : deplen) +
1283 sizeof (struct dirent);
1284 dep = (struct dirent *)malloc(deplen);
1285 if (dep == NULL)
1286 goto bailout;
1288 while ((err = readdir_r(dp, dep, &newdep)) == 0 &&
1289 newdep != NULL) {
1291 assert(newdep == dep);
1293 if (strcmp(dep->d_name, ".") == 0 ||
1294 strcmp(dep->d_name, "..") == 0 ||
1295 (minor_portion = strchr(dep->d_name,
1296 *MINOR_SEP)) == NULL)
1297 continue;
1299 *minor_portion = 0;
1300 if (strcmp(dep->d_name, name_part) != 0)
1301 continue;
1302 *minor_portion = *MINOR_SEP;
1304 (void) snprintf(minor_path, MAXPATHLEN,
1305 "%s/%s", devpath, dep->d_name);
1308 * Break directly for tape device
1310 if (strcmp(type, "tape") == 0)
1311 break;
1314 * If stat() fails, the device *may* be retired.
1315 * Check via libdevinfo if the device has a BLK minor.
1316 * We don't use libdevinfo all the time, since taking
1317 * a snapshot is slower than a stat().
1319 if (stat(minor_path, &sb) < 0) {
1320 if (is_devinfo_blk(minor_path)) {
1321 break;
1322 } else {
1323 continue;
1327 if (S_ISBLK(sb.st_mode))
1328 break;
1332 (void) closedir(dp);
1333 free(dep);
1334 free(devpath);
1336 dp = NULL;
1337 dep = NULL;
1338 devpath = NULL;
1341 * If there was an error, or we didn't exit the loop
1342 * by finding a block or character device, bail out.
1344 if (err != 0 || newdep == NULL)
1345 goto bailout;
1348 * Look for links to the physical path in /dev/dsk
1349 * and /dev/rmt. So far, sata modue supports disk,
1350 * dvd and tape devices, so we will first look for
1351 * BLOCK devices, and then look for tape devices.
1353 (void) physpath_to_devlink("/dev/dsk",
1354 minor_path, &devlink, &l_errno);
1356 /* postprocess and copy logical name here */
1357 if (devlink != NULL) {
1359 * For disks, remove partition/slice info
1361 if ((cp = strstr(devlink, "dsk/")) != NULL) {
1362 /* cXtYdZ[(s[0..15])|(p[0..X])] */
1363 if ((p = strchr(cp + 4, 'd')) != NULL) {
1364 p++; /* Skip the 'd' */
1365 while (*p != 0 && isdigit(*p))
1366 p++;
1367 *p = 0;
1369 *dyncomp = strdup(cp);
1372 free(devlink);
1373 } else if (strcmp(type, "tape") == 0) {
1376 * For tape device, logical name looks like
1377 * rmt/X
1379 (void) physpath_to_devlink("/dev/rmt",
1380 minor_path, &devlink, &l_errno);
1382 if (devlink != NULL) {
1383 if ((cp = strstr(devlink, "rmt/")) != NULL) {
1384 *dyncomp = strdup(cp);
1387 free(devlink);
1391 return (SATA_CFGA_OK);
1394 bailout:
1395 if (dp)
1396 (void) closedir(dp);
1397 free(devpath);
1398 free(dep);
1399 return (CFGA_SATA_DYNAMIC_AP);
1402 /* cfgadm entry point */
1403 /*ARGSUSED*/
1404 cfga_err_t
1405 cfga_list_ext(
1406 const char *ap_id,
1407 cfga_list_data_t **ap_id_list,
1408 int *nlistp,
1409 const char *options,
1410 const char *listopts,
1411 char **errstring,
1412 cfga_flags_t flags)
1414 int l_errno;
1415 char *ap_id_log = NULL;
1416 size_t size;
1417 nvlist_t *user_nvlist = NULL;
1418 devctl_hdl_t devctl_hdl = NULL;
1419 cfga_sata_ret_t rv = CFGA_SATA_OK;
1420 devctl_ap_state_t devctl_ap_state;
1421 char *pdyn;
1422 boolean_t pmult = B_FALSE;
1423 uint32_t port;
1426 if ((rv = verify_params(ap_id, options, errstring)) != CFGA_SATA_OK) {
1427 (void) cfga_help(NULL, options, flags);
1428 goto bailout;
1430 /* We do not care here about dynamic AP name component */
1431 if ((pdyn = GET_DYN(ap_id)) != NULL) {
1432 *pdyn = '\0';
1435 if (ap_id_list == NULL || nlistp == NULL) {
1436 rv = CFGA_SATA_DATA_ERROR;
1437 (void) cfga_help(NULL, options, flags);
1438 goto bailout;
1441 /* Get ap status */
1442 if ((rv = setup_for_devctl_cmd(ap_id, &devctl_hdl, &user_nvlist,
1443 DC_RDONLY)) != CFGA_SATA_OK) {
1444 goto bailout;
1447 /* will call dc_cmd to send IOCTL to kernel */
1448 if (devctl_ap_getstate(devctl_hdl, user_nvlist,
1449 &devctl_ap_state) == -1) {
1450 cleanup_after_devctl_cmd(devctl_hdl, user_nvlist);
1451 rv = CFGA_SATA_IOCTL;
1452 goto bailout;
1455 cleanup_after_devctl_cmd(devctl_hdl, user_nvlist);
1458 * Create cfga_list_data_t struct.
1460 if ((*ap_id_list =
1461 (cfga_list_data_t *)malloc(sizeof (**ap_id_list))) == NULL) {
1462 rv = CFGA_SATA_ALLOC_FAIL;
1463 goto bailout;
1465 *nlistp = 1;
1468 * Rest of the code fills in the cfga_list_data_t struct.
1471 /* Get /dev/cfg path to corresponding to the physical ap_id */
1472 /* Remember ap_id_log must be freed */
1473 rv = physpath_to_devlink(CFGA_DEV_DIR, (char *)ap_id,
1474 &ap_id_log, &l_errno);
1476 if (rv != 0) {
1477 rv = CFGA_SATA_DEVLINK;
1478 goto bailout;
1481 /* Get logical ap_id corresponding to the physical */
1482 if (ap_id_log == NULL || strstr(ap_id_log, CFGA_DEV_DIR) == NULL) {
1483 rv = CFGA_SATA_DEVLINK;
1484 goto bailout;
1487 (void) strlcpy((*ap_id_list)->ap_log_id,
1488 /* Strip off /dev/cfg/ */ ap_id_log + strlen(CFGA_DEV_DIR)+ 1,
1489 sizeof ((*ap_id_list)->ap_log_id));
1491 free(ap_id_log);
1492 ap_id_log = NULL;
1494 (void) strlcpy((*ap_id_list)->ap_phys_id, ap_id,
1495 sizeof ((*ap_id_list)->ap_phys_id));
1497 switch (devctl_ap_state.ap_rstate) {
1498 case AP_RSTATE_EMPTY:
1499 (*ap_id_list)->ap_r_state = CFGA_STAT_EMPTY;
1500 break;
1502 case AP_RSTATE_DISCONNECTED:
1503 (*ap_id_list)->ap_r_state = CFGA_STAT_DISCONNECTED;
1504 break;
1506 case AP_RSTATE_CONNECTED:
1507 (*ap_id_list)->ap_r_state = CFGA_STAT_CONNECTED;
1508 break;
1510 default:
1511 rv = CFGA_SATA_STATE;
1512 goto bailout;
1515 switch (devctl_ap_state.ap_ostate) {
1516 case AP_OSTATE_CONFIGURED:
1517 (*ap_id_list)->ap_o_state = CFGA_STAT_CONFIGURED;
1518 break;
1520 case AP_OSTATE_UNCONFIGURED:
1521 (*ap_id_list)->ap_o_state = CFGA_STAT_UNCONFIGURED;
1522 break;
1524 default:
1525 rv = CFGA_SATA_STATE;
1526 goto bailout;
1529 switch (devctl_ap_state.ap_condition) {
1530 case AP_COND_OK:
1531 (*ap_id_list)->ap_cond = CFGA_COND_OK;
1532 break;
1534 case AP_COND_FAILING:
1535 (*ap_id_list)->ap_cond = CFGA_COND_FAILING;
1536 break;
1538 case AP_COND_FAILED:
1539 (*ap_id_list)->ap_cond = CFGA_COND_FAILED;
1540 break;
1542 case AP_COND_UNUSABLE:
1543 (*ap_id_list)->ap_cond = CFGA_COND_UNUSABLE;
1544 break;
1546 case AP_COND_UNKNOWN:
1547 (*ap_id_list)->ap_cond = CFGA_COND_UNKNOWN;
1548 break;
1550 default:
1551 rv = CFGA_SATA_STATE;
1552 goto bailout;
1555 (*ap_id_list)->ap_class[0] = '\0'; /* Filled by libcfgadm */
1556 (*ap_id_list)->ap_busy = devctl_ap_state.ap_in_transition;
1557 (*ap_id_list)->ap_status_time = devctl_ap_state.ap_last_change;
1558 (*ap_id_list)->ap_info[0] = '\0';
1560 if ((*ap_id_list)->ap_r_state == CFGA_STAT_CONNECTED) {
1561 char *str_p;
1562 int skip, i;
1565 * Fill in the 'Information' field for the -v option
1566 * Model (MOD:)
1568 if ((rv = do_control_ioctl(ap_id, SATA_CFGA_GET_MODEL_INFO,
1569 0, (void **)&str_p, &size)) != CFGA_SATA_OK) {
1570 (void) printf(
1571 "SATA_CFGA_GET_MODULE_INFO ioctl failed\n");
1572 goto bailout;
1574 /* drop leading and trailing spaces */
1575 skip = strspn(str_p, " ");
1576 for (i = size - 1; i >= 0; i--) {
1577 if (str_p[i] == '\040')
1578 str_p[i] = '\0';
1579 else if (str_p[i] != '\0')
1580 break;
1583 (void) strlcpy((*ap_id_list)->ap_info, "Mod: ",
1584 sizeof ((*ap_id_list)->ap_info));
1585 (void) strlcat((*ap_id_list)->ap_info, str_p + skip,
1586 sizeof ((*ap_id_list)->ap_info));
1588 free(str_p);
1591 * Fill in the 'Information' field for the -v option
1592 * Firmware revision (FREV:)
1594 if ((rv = do_control_ioctl(ap_id,
1595 SATA_CFGA_GET_REVFIRMWARE_INFO,
1596 0, (void **)&str_p, &size)) != CFGA_SATA_OK) {
1597 (void) printf(
1598 "SATA_CFGA_GET_REVFIRMWARE_INFO ioctl failed\n");
1599 goto bailout;
1601 /* drop leading and trailing spaces */
1602 skip = strspn(str_p, " ");
1603 for (i = size - 1; i >= 0; i--) {
1604 if (str_p[i] == '\040')
1605 str_p[i] = '\0';
1606 else if (str_p[i] != '\0')
1607 break;
1609 (void) strlcat((*ap_id_list)->ap_info, " FRev: ",
1610 sizeof ((*ap_id_list)->ap_info));
1611 (void) strlcat((*ap_id_list)->ap_info, str_p + skip,
1612 sizeof ((*ap_id_list)->ap_info));
1614 free(str_p);
1618 * Fill in the 'Information' field for the -v option
1619 * Serial Number (SN:)
1621 if ((rv = do_control_ioctl(ap_id,
1622 SATA_CFGA_GET_SERIALNUMBER_INFO,
1623 0, (void **)&str_p, &size)) != CFGA_SATA_OK) {
1624 (void) printf(
1625 "SATA_CFGA_GET_SERIALNUMBER_INFO ioctl failed\n");
1626 goto bailout;
1628 /* drop leading and trailing spaces */
1629 skip = strspn(str_p, " ");
1630 for (i = size - 1; i >= 0; i--) {
1631 if (str_p[i] == '\040')
1632 str_p[i] = '\0';
1633 else if (str_p[i] != '\0')
1634 break;
1636 (void) strlcat((*ap_id_list)->ap_info, " SN: ",
1637 sizeof ((*ap_id_list)->ap_info));
1638 (void) strlcat((*ap_id_list)->ap_info, str_p + skip,
1639 sizeof ((*ap_id_list)->ap_info));
1641 free(str_p);
1645 /* Fill in ap_type which is collected from HBA driver */
1646 /* call do_control_ioctl TBD */
1647 if ((rv = do_control_ioctl(ap_id, SATA_CFGA_GET_AP_TYPE, 0,
1648 (void **)&str_p, &size)) != CFGA_SATA_OK) {
1649 (void) printf(
1650 "SATA_CFGA_GET_AP_TYPE ioctl failed\n");
1651 goto bailout;
1654 (void) strlcpy((*ap_id_list)->ap_type, str_p,
1655 sizeof ((*ap_id_list)->ap_type));
1657 free(str_p);
1660 * Checking device type. Port multiplier has no dynamic
1661 * suffix.
1663 if (strncmp((*ap_id_list)->ap_type, "sata-pmult",
1664 sizeof ("sata-pmult")) == 0)
1665 pmult = B_TRUE;
1667 if ((*ap_id_list)->ap_o_state == CFGA_STAT_CONFIGURED &&
1668 pmult == B_FALSE) {
1670 char *dyncomp = NULL;
1673 * This is the case where we need to generate
1674 * a dynamic component of the ap_id, i.e. device.
1676 rv = sata_make_dyncomp(ap_id, &dyncomp,
1677 (*ap_id_list)->ap_type);
1678 if (rv != CFGA_SATA_OK)
1679 goto bailout;
1680 if (dyncomp != NULL) {
1681 (void) strcat((*ap_id_list)->ap_log_id,
1682 DYN_SEP);
1683 (void) strlcat((*ap_id_list)->ap_log_id,
1684 dyncomp,
1685 sizeof ((*ap_id_list)->ap_log_id));
1686 free(dyncomp);
1690 } else {
1691 /* This is an empty port */
1692 if (get_port_num(ap_id, &port) != CFGA_SATA_OK) {
1693 goto bailout;
1696 if (port & SATA_CFGA_PMPORT_QUAL) {
1697 (void) strlcpy((*ap_id_list)->ap_type, "pmult-port",
1698 sizeof ((*ap_id_list)->ap_type));
1699 } else {
1700 (void) strlcpy((*ap_id_list)->ap_type, "sata-port",
1701 sizeof ((*ap_id_list)->ap_type));
1705 return (sata_err_msg(errstring, rv, ap_id, errno));
1707 bailout:
1708 if (*ap_id_list != NULL) {
1709 free(*ap_id_list);
1711 if (ap_id_log != NULL) {
1712 free(ap_id_log);
1715 return (sata_err_msg(errstring, rv, ap_id, errno));
1718 * This routine accepts a string adn prints it using
1719 * the message print routine argument.
1721 static void
1722 cfga_msg(struct cfga_msg *msgp, const char *str)
1724 int len;
1725 char *q;
1727 if (msgp == NULL || msgp->message_routine == NULL) {
1728 (void) printf("cfga_msg: NULL msgp\n");
1729 return;
1732 if ((len = strlen(str)) == 0) {
1733 (void) printf("cfga_msg: null str\n");
1734 return;
1737 if ((q = (char *)calloc(len + 1, 1)) == NULL) {
1738 perror("cfga_msg");
1739 return;
1742 (void) strcpy(q, str);
1743 (*msgp->message_routine)(msgp->appdata_ptr, q);
1745 free(q);
1748 /* cfgadm entry point */
1749 /* ARGSUSED */
1750 cfga_err_t
1751 cfga_help(struct cfga_msg *msgp, const char *options, cfga_flags_t flags)
1753 if (options != NULL) {
1754 cfga_msg(msgp, dgettext(TEXT_DOMAIN, sata_help[HELP_UNKNOWN]));
1755 cfga_msg(msgp, options);
1757 cfga_msg(msgp, dgettext(TEXT_DOMAIN, sata_help[HELP_HEADER]));
1758 cfga_msg(msgp, sata_help[HELP_CONFIG]);
1759 cfga_msg(msgp, sata_help[HELP_RESET_PORT]);
1760 cfga_msg(msgp, sata_help[HELP_RESET_DEVICE]);
1761 cfga_msg(msgp, sata_help[HELP_RESET_ALL]);
1762 cfga_msg(msgp, sata_help[HELP_PORT_ACTIVATE]);
1763 cfga_msg(msgp, sata_help[HELP_PORT_DEACTIVATE]);
1764 cfga_msg(msgp, sata_help[HELP_PORT_SELF_TEST]);
1765 cfga_msg(msgp, sata_help[HELP_CNTRL_SELF_TEST]);
1767 return (CFGA_OK);
1772 * Ensure the ap_id passed is in the correct (physical ap_id) form:
1773 * path/device:xx[.xx]
1774 * where xx is a one or two-digit number.
1776 * Note the library always calls the plugin with a physical ap_id.
1778 static int
1779 verify_valid_apid(const char *ap_id)
1781 char *l_ap_id;
1783 if (ap_id == NULL)
1784 return (-1);
1786 l_ap_id = strrchr(ap_id, (int)*MINOR_SEP);
1787 l_ap_id++;
1789 if (strspn(l_ap_id, "0123456789.") != strlen(l_ap_id)) {
1790 /* Bad characters in the ap_id */
1791 return (-1);
1794 if (strstr(l_ap_id, "..") != NULL) {
1795 /* ap_id has 1..2 or more than 2 dots */
1796 return (-1);
1799 return (0);
1805 * Verify the params passed in are valid.
1807 static cfga_sata_ret_t
1808 verify_params(
1809 const char *ap_id,
1810 const char *options,
1811 char **errstring)
1813 char *pdyn, *lap_id;
1814 int rv;
1816 if (errstring != NULL) {
1817 *errstring = NULL;
1820 if (options != NULL) {
1821 return (CFGA_SATA_OPTIONS);
1824 /* Strip dynamic AP name component if it is present. */
1825 lap_id = strdup(ap_id);
1826 if (lap_id == NULL) {
1827 return (CFGA_SATA_ALLOC_FAIL);
1829 if ((pdyn = GET_DYN(lap_id)) != NULL) {
1830 *pdyn = '\0';
1833 if (verify_valid_apid(lap_id) != 0) {
1834 rv = CFGA_SATA_AP;
1835 } else {
1836 rv = CFGA_SATA_OK;
1838 free(lap_id);
1840 return (rv);
1844 * Takes a validated ap_id and extracts the port number.
1845 * Port multiplier is supported now.
1847 static cfga_sata_ret_t
1848 get_port_num(const char *ap_id, uint32_t *port)
1850 uint32_t cport, pmport = 0, qual = 0;
1851 char *cport_str, *pmport_str;
1853 /* Get the cport number */
1854 cport_str = strrchr(ap_id, (int)*MINOR_SEP) + strlen(MINOR_SEP);
1856 errno = 0;
1857 cport = strtol(cport_str, NULL, 10);
1858 if ((cport & ~SATA_CFGA_CPORT_MASK) != 0 || errno != 0) {
1859 return (CFGA_SATA_PORT);
1862 /* Get pmport number if there is a PORT_SEPARATOR */
1863 errno = 0;
1864 if ((pmport_str = strrchr(ap_id, (int)*PORT_SEPARATOR)) != 0) {
1865 pmport_str += strlen(PORT_SEPARATOR);
1866 pmport = strtol(pmport_str, NULL, 10);
1867 qual = SATA_CFGA_PMPORT_QUAL;
1868 if ((pmport & ~SATA_CFGA_PMPORT_MASK) != 0 || errno != 0) {
1869 return (CFGA_SATA_PORT);
1873 *port = cport | (pmport << SATA_CFGA_PMPORT_SHIFT) | qual;
1874 return (CFGA_SATA_OK);
1878 * Pair of routines to set up for/clean up after a devctl_ap_* lib call.
1880 static void
1881 cleanup_after_devctl_cmd(devctl_hdl_t devctl_hdl, nvlist_t *user_nvlist)
1883 nvlist_free(user_nvlist);
1884 devctl_release(devctl_hdl);
1887 static cfga_sata_ret_t
1888 setup_for_devctl_cmd(
1889 const char *ap_id,
1890 devctl_hdl_t *devctl_hdl,
1891 nvlist_t **user_nvlistp,
1892 uint_t oflag)
1895 uint_t port;
1896 cfga_sata_ret_t rv = CFGA_SATA_OK;
1897 char *lap_id, *pdyn;
1899 lap_id = strdup(ap_id);
1900 if (lap_id == NULL)
1901 return (CFGA_SATA_ALLOC_FAIL);
1902 if ((pdyn = GET_DYN(lap_id)) != NULL) {
1903 *pdyn = '\0';
1906 /* Get a devctl handle to pass to the devctl_ap_XXX functions */
1907 if ((*devctl_hdl = devctl_ap_acquire((char *)lap_id, oflag)) == NULL) {
1908 (void) fprintf(stderr, "[libcfgadm:sata] "
1909 "setup_for_devctl_cmd: devctl_ap_acquire failed: %s\n",
1910 strerror(errno));
1911 rv = CFGA_SATA_DEVCTL;
1912 goto bailout;
1915 /* Set up nvlist to pass the port number down to the driver */
1916 if (nvlist_alloc(user_nvlistp, NV_UNIQUE_NAME_TYPE, 0) != 0) {
1917 *user_nvlistp = NULL;
1918 rv = CFGA_SATA_NVLIST;
1919 (void) printf("nvlist_alloc failed\n");
1920 goto bailout;
1924 * Get port id, for Port Multiplier port, things could be a little bit
1925 * complicated because of "port.port" format in ap_id, thus for
1926 * port multiplier port, port number should be coded as 32bit int
1927 * with the sig 16 bit as sata channel number, least 16 bit as
1928 * the port number of sata port multiplier port.
1930 if ((rv = get_port_num(lap_id, &port)) != CFGA_SATA_OK) {
1931 (void) printf(
1932 "setup_for_devctl_cmd: get_port_num, errno: %d\n",
1933 errno);
1934 goto bailout;
1937 /* Creates an int32_t entry */
1938 if (nvlist_add_int32(*user_nvlistp, PORT, port) == -1) {
1939 (void) printf("nvlist_add_int32 failed\n");
1940 rv = CFGA_SATA_NVLIST;
1941 goto bailout;
1944 free(lap_id);
1945 return (rv);
1947 bailout:
1948 free(lap_id);
1949 (void) cleanup_after_devctl_cmd(*devctl_hdl, *user_nvlistp);
1951 return (rv);
1955 static cfga_sata_ret_t
1956 port_state(devctl_hdl_t hdl, nvlist_t *list,
1957 ap_rstate_t *rstate, ap_ostate_t *ostate)
1959 devctl_ap_state_t devctl_ap_state;
1961 if (devctl_ap_getstate(hdl, list, &devctl_ap_state) == -1) {
1962 (void) printf("devctl_ap_getstate failed, errno: %d\n", errno);
1963 return (CFGA_SATA_IOCTL);
1965 *rstate = devctl_ap_state.ap_rstate;
1966 *ostate = devctl_ap_state.ap_ostate;
1967 return (CFGA_SATA_OK);
1972 * Given a subcommand to the DEVCTL_AP_CONTROL ioctl, rquest the size of
1973 * the data to be returned, allocate a buffer, then get the data.
1974 * Returns *descrp (which must be freed) and size.
1976 * Note SATA_DESCR_TYPE_STRING returns an ASCII NULL-terminated string,
1977 * not a string descr.
1979 cfga_sata_ret_t
1980 do_control_ioctl(const char *ap_id, sata_cfga_apctl_t subcommand, uint_t arg,
1981 void **descrp, size_t *sizep)
1983 int fd = -1;
1984 uint_t port;
1985 uint32_t local_size;
1986 cfga_sata_ret_t rv = CFGA_SATA_OK;
1987 struct sata_ioctl_data ioctl_data;
1989 assert(descrp != NULL);
1990 *descrp = NULL;
1991 assert(sizep != NULL);
1993 if ((rv = get_port_num(ap_id, &port)) != CFGA_SATA_OK) {
1994 goto bailout;
1997 if ((fd = open(ap_id, O_RDONLY)) == -1) {
1998 (void) printf("do_control_ioctl: open failed: errno:%d\n",
1999 errno);
2000 rv = CFGA_SATA_OPEN;
2001 if (errno == EBUSY) {
2002 rv = CFGA_SATA_BUSY;
2004 goto bailout;
2007 ioctl_data.cmd = subcommand;
2008 ioctl_data.port = port;
2009 ioctl_data.misc_arg = (uint_t)arg;
2012 * Find out how large a buf we need to get the data.
2013 * Note the ioctls only accept/return a 32-bit int for a get_size
2014 * to avoid 32/64 and BE/LE issues.
2016 if ((subcommand == SATA_CFGA_GET_AP_TYPE) ||
2017 (subcommand == SATA_CFGA_GET_DEVICE_PATH) ||
2018 (subcommand == SATA_CFGA_GET_MODEL_INFO) ||
2019 (subcommand == SATA_CFGA_GET_REVFIRMWARE_INFO) ||
2020 (subcommand == SATA_CFGA_GET_SERIALNUMBER_INFO)) {
2021 ioctl_data.get_size = B_TRUE;
2022 ioctl_data.buf = (caddr_t)&local_size;
2023 ioctl_data.bufsiz = sizeof (local_size);
2025 if (ioctl(fd, DEVCTL_AP_CONTROL, &ioctl_data) != 0) {
2026 perror("ioctl failed (size)");
2027 rv = CFGA_SATA_IOCTL;
2028 goto bailout;
2030 *sizep = local_size;
2032 if (local_size == 0) {
2033 (void) printf("zero length data\n");
2034 rv = CFGA_SATA_ZEROLEN;
2035 goto bailout;
2037 if ((*descrp = malloc(*sizep)) == NULL) {
2038 (void) printf("do_control_ioctl: malloc failed\n");
2039 rv = CFGA_SATA_ALLOC_FAIL;
2040 goto bailout;
2042 } else {
2043 *sizep = 0;
2045 ioctl_data.get_size = B_FALSE;
2046 ioctl_data.buf = *descrp;
2047 ioctl_data.bufsiz = *sizep;
2049 /* Execute IOCTL */
2051 if (ioctl(fd, DEVCTL_AP_CONTROL, &ioctl_data) != 0) {
2052 rv = CFGA_SATA_IOCTL;
2053 goto bailout;
2056 (void) close(fd);
2058 return (rv);
2060 bailout:
2061 if (fd != -1) {
2062 (void) close(fd);
2064 if (*descrp != NULL) {
2065 free(*descrp);
2066 *descrp = NULL;
2069 if (rv == CFGA_SATA_IOCTL && errno == EBUSY) {
2070 rv = CFGA_SATA_BUSY;
2073 return (rv);
2077 static int
2078 sata_confirm(struct cfga_confirm *confp, char *msg)
2080 int rval;
2082 if (confp == NULL || confp->confirm == NULL) {
2083 return (0);
2085 rval = (*confp->confirm)(confp->appdata_ptr, msg);
2087 return (rval);
2091 static char *
2092 sata_get_devicepath(const char *ap_id)
2094 char *devpath = NULL;
2095 size_t size;
2096 cfga_sata_ret_t rv;
2098 rv = do_control_ioctl(ap_id, SATA_CFGA_GET_DEVICE_PATH, 0,
2099 (void **)&devpath, &size);
2101 if (rv == CFGA_SATA_OK) {
2102 return (devpath);
2103 } else {
2104 return (NULL);