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]
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #include <sys/param.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
;
57 /* SATA specific help messages */
58 static char *sata_help
[] = {
60 "SATA specific commands:\n",
61 " cfgadm -c [configure|unconfigure|disconnect|connect] ap_id "
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",
70 "\tunknown command or option:\n",
72 }; /* End help messages */
78 static msgcvt_t sata_msgs
[] = {
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 " },
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
);
240 cleanup_after_devctl_cmd(devctl_hdl_t devctl_hdl
, nvlist_t
*user_nvlist
);
243 sata_get_devicepath(const char *ap_id
);
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
);
253 static cfga_sata_ret_t
254 physpath_to_devlink(const char *basedir
, const char *node_path
,
255 char **logpp
, int *l_errnop
)
261 struct dirent
*dep
, *newdep
;
263 boolean_t found
= B_FALSE
;
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
274 if ((dp
= opendir(basedir
)) == NULL
) {
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
||
291 rv
= CFGA_SATA_ALLOC_FAIL
;
297 while (!found
&& (err
= readdir_r(dp
, dep
, &newdep
)) == 0 &&
300 assert(newdep
== dep
);
302 if (strcmp(dep
->d_name
, ".") == 0 ||
303 strcmp(dep
->d_name
, "..") == 0)
306 (void) snprintf(linkpath
, MAXPATHLEN
,
307 "%s/%s", basedir
, dep
->d_name
);
309 if (lstat(linkpath
, &sb
) < 0)
312 if (S_ISDIR(sb
.st_mode
)) {
314 if ((rv
= physpath_to_devlink(linkpath
, node_path
,
315 logpp
, l_errnop
)) != CFGA_SATA_OK
) {
323 } else if (S_ISLNK(sb
.st_mode
)) {
325 bzero(buf
, PATH_MAX
);
326 if (readlink(linkpath
, buf
, PATH_MAX
) < 0)
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>
337 while (strncmp(p
, "../", 3) == 0)
341 p
--; /* back up to get a slash */
345 if (strcmp(p
, node_path
) == 0) {
346 *logpp
= strdup(linkpath
);
347 if (*logpp
== NULL
) {
349 rv
= CFGA_SATA_ALLOC_FAIL
;
366 return (CFGA_SATA_INTERNAL_ERROR
);
369 return (CFGA_SATA_OK
);
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
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.
410 set_msg(char **ret_str
, ...)
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
) {
432 (void) strcpy(*ret_str
+ total_len
, str
);
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
459 if (errstring
== NULL
) {
460 return (sata_msgs
[rv
].cfga_err
);
464 * Generate the appropriate SATA-specific error message(s) (if any).
469 /* Special case - do nothing. */
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
:
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
);
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
);
501 case CFGA_SATA_NOT_CONNECTED
:
502 case CFGA_SATA_NOT_CONFIGURED
:
503 case CFGA_SATA_ALREADY_CONNECTED
:
504 case CFGA_SATA_ALREADY_CONFIGURED
:
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
);
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
);
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
);
541 set_msg(errstring
, ERR_STR(CFGA_SATA_INTERNAL_ERROR
), NULL
);
547 * Determine the proper error code to send back to the cfgadm library.
549 return (sata_msgs
[rv
].cfga_err
);
556 /* cfgadm entry point */
560 cfga_cmd_t state_change_cmd
,
563 struct cfga_confirm
*confp
,
564 struct cfga_msg
*msgp
,
572 nvlist_t
*nvl
= NULL
;
575 devctl_hdl_t hdl
= NULL
;
576 cfga_sata_ret_t rv
= CFGA_SATA_OK
;
580 boolean_t pmult
= B_FALSE
;
583 * All sub-commands which can change state of device require
586 if (geteuid() != 0) {
591 if ((rv
= verify_params(ap_id
, options
, errstring
)) != CFGA_SATA_OK
) {
592 (void) cfga_help(msgp
, options
, flags
);
596 if ((rv
= setup_for_devctl_cmd(ap_id
, &hdl
, &nvl
,
597 DC_RDONLY
)) != CFGA_SATA_OK
) {
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
) {
610 if (strncmp(str_type
, "sata-pmult", sizeof ("sata-pmult")) == 0) {
614 switch (state_change_cmd
) {
615 case CFGA_CMD_CONFIGURE
:
616 if (pmult
== B_TRUE
) {
617 rv
= CFGA_SATA_HWOPNOTSUPP
;
621 if ((rv
= port_state(hdl
, nvl
, &rstate
, &ostate
)) !=
625 if (ostate
== AP_OSTATE_CONFIGURED
) {
626 rv
= CFGA_SATA_ALREADY_CONFIGURED
;
629 /* Disallow dynamic AP name component */
630 if (GET_DYN(ap_id
) != NULL
) {
631 rv
= CFGA_SATA_INVALID_DEVNAME
;
635 if (rstate
== AP_RSTATE_EMPTY
) {
636 rv
= CFGA_SATA_NOT_CONNECTED
;
641 if (devctl_ap_configure(hdl
, nvl
) != 0) {
642 rv
= CFGA_SATA_DEV_CONFIGURE
;
646 devpath
= sata_get_devicepath(ap_id
);
647 if (devpath
== NULL
) {
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
++) {
656 devpath
= sata_get_devicepath(ap_id
);
659 if (devpath
== NULL
) {
660 rv
= CFGA_SATA_DEV_CONFIGURE
;
668 case CFGA_CMD_UNCONFIGURE
:
669 if (pmult
== B_TRUE
) {
670 rv
= CFGA_SATA_HWOPNOTSUPP
;
674 if ((rv
= port_state(hdl
, nvl
, &rstate
, &ostate
)) !=
678 if (rstate
!= AP_RSTATE_CONNECTED
) {
679 rv
= CFGA_SATA_NOT_CONNECTED
;
683 if (ostate
!= AP_OSTATE_CONFIGURED
) {
684 rv
= CFGA_SATA_NOT_CONFIGURED
;
687 /* Strip off AP name dynamic component, if present */
688 if ((pdyn
= GET_DYN(ap_id
)) != NULL
) {
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"
700 SATA_CONFIRM_DEVICE
, ap_id
,
701 SATA_CONFIRM_DEVICE_SUSPEND
);
704 if (!sata_confirm(confp
, msg
)) {
711 devpath
= sata_get_devicepath(ap_id
);
712 if (devpath
== NULL
) {
714 "cfga_change_state: get device path failed\n");
715 rv
= CFGA_SATA_DEV_UNCONFIGURE
;
719 if ((rv
= sata_rcm_offline(ap_id
, errstring
, devpath
, flags
))
724 ret
= devctl_ap_unconfigure(hdl
, nvl
);
727 rv
= CFGA_SATA_DEV_UNCONFIGURE
;
728 if (errno
== EBUSY
) {
731 (void) sata_rcm_online(ap_id
, errstring
, devpath
,
734 (void) sata_rcm_remove(ap_id
, errstring
, devpath
,
742 case CFGA_CMD_DISCONNECT
:
743 if ((rv
= port_state(hdl
, nvl
, &rstate
, &ostate
)) !=
747 if (rstate
== AP_RSTATE_DISCONNECTED
) {
748 rv
= CFGA_SATA_DISCONNECTED
;
752 /* Strip off AP name dynamic component, if present */
753 if ((pdyn
= GET_DYN(ap_id
)) != NULL
) {
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
&&
768 devpath
= sata_get_devicepath(ap_id
);
769 if (devpath
== NULL
) {
771 "cfga_change_state: get path failed\n");
772 rv
= CFGA_SATA_DEV_UNCONFIGURE
;
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,
783 SATA_CONFIRM_DEVICE
, ap_id
,
784 SATA_CONFIRM_DEVICE_SUSPEND
);
786 if (!sata_confirm(confp
, msg
)) {
793 if ((rv
= sata_rcm_offline(ap_id
, errstring
,
794 devpath
, flags
)) != CFGA_SATA_OK
) {
798 ret
= devctl_ap_unconfigure(hdl
, nvl
);
801 "devctl_ap_unconfigure failed\n");
802 rv
= CFGA_SATA_DEV_UNCONFIGURE
;
805 (void) sata_rcm_online(ap_id
, errstring
,
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
819 (void) printf("%s\n",
820 ERR_STR(CFGA_SATA_DEVICE_UNCONFIGURED
));
821 (void) sata_rcm_remove(ap_id
, errstring
,
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,
834 SATA_CONFIRM_PORT
, ap_id
,
835 SATA_CONFIRM_PORT_DISABLE
);
837 if (!sata_confirm(confp
, msg
)) {
843 ret
= devctl_ap_disconnect(hdl
, nvl
);
845 rv
= CFGA_SATA_IOCTL
;
846 if (errno
== EBUSY
) {
852 case CFGA_CMD_CONNECT
:
853 if ((rv
= port_state(hdl
, nvl
, &rstate
, &ostate
)) !=
857 if (rstate
== AP_RSTATE_CONNECTED
) {
858 rv
= CFGA_SATA_ALREADY_CONNECTED
;
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"
868 SATA_CONFIRM_PORT
, ap_id
,
869 SATA_CONFIRM_PORT_ENABLE
);
871 if (!sata_confirm(confp
, msg
)) {
876 /* Disallow dynamic AP name component */
877 if (GET_DYN(ap_id
) != NULL
) {
878 rv
= CFGA_SATA_INVALID_DEVNAME
;
882 ret
= devctl_ap_connect(hdl
, nvl
);
884 rv
= CFGA_SATA_IOCTL
;
892 case CFGA_CMD_UNLOAD
:
893 (void) cfga_help(msgp
, options
, flags
);
894 rv
= CFGA_SATA_OPNOTSUPP
;
899 (void) cfga_help(msgp
, options
, flags
);
900 rv
= CFGA_SATA_INTERNAL_ERROR
;
904 cleanup_after_devctl_cmd(hdl
, nvl
);
906 return (sata_err_msg(errstring
, rv
, ap_id
, errno
));
909 /* cfgadm entry point */
915 struct cfga_confirm
*confp
,
916 struct cfga_msg
*msgp
,
922 nvlist_t
*list
= NULL
;
925 devctl_hdl_t hdl
= NULL
;
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
939 if (geteuid() != 0) {
945 (void) printf("No valid option specified\n");
946 rv
= CFGA_SATA_OPTIONS
;
950 if ((rv
= setup_for_devctl_cmd(ap_id
, &hdl
, &list
, 0)) !=
955 /* We do not care here about dynamic AP name component */
956 if ((str_p
= GET_DYN(ap_id
)) != NULL
) {
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"
970 SATA_CONFIRM_PORT
, ap_id
,
971 SATA_CONFIRM_DEVICE_ABORT
);
977 if (!sata_confirm(confp
, msg
)) {
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
)) !=
990 * Reset device function requires device to be connected
992 if (rstate
!= AP_RSTATE_CONNECTED
) {
993 rv
= CFGA_SATA_NOT_CONNECTED
;
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"
1004 SATA_CONFIRM_DEVICE
, ap_id
,
1005 SATA_CONFIRM_DEVICE_ABORT
);
1007 rv
= CFGA_SATA_NACK
;
1011 if (!sata_confirm(confp
, msg
)) {
1012 rv
= CFGA_SATA_NACK
;
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"
1027 SATA_CONFIRM_CONTROLLER
, ap_id
,
1028 SATA_CONFIRM_CONTROLLER_ABORT
);
1030 rv
= CFGA_SATA_NACK
;
1034 if (!sata_confirm(confp
, msg
)) {
1035 rv
= CFGA_SATA_NACK
;
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"
1049 SATA_CONFIRM_PORT
, ap_id
,
1050 SATA_CONFIRM_PORT_DISABLE
);
1052 rv
= CFGA_SATA_NACK
;
1055 if (!sata_confirm(confp
, msg
)) {
1056 rv
= CFGA_SATA_NACK
;
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"
1071 SATA_CONFIRM_PORT
, ap_id
,
1072 SATA_CONFIRM_PORT_ENABLE
);
1074 rv
= CFGA_SATA_NACK
;
1077 if (!sata_confirm(confp
, msg
)) {
1078 rv
= CFGA_SATA_NACK
;
1082 rv
= do_control_ioctl(ap_id
, SATA_CFGA_PORT_ACTIVATE
,
1083 0, (void **)&str_p
, &size
);
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"
1094 SATA_CONFIRM_PORT
, ap_id
,
1095 SATA_CONFIRM_DEVICE_SUSPEND
);
1097 rv
= CFGA_SATA_NACK
;
1100 if (!sata_confirm(confp
, msg
)) {
1101 rv
= CFGA_SATA_NACK
;
1105 rv
= do_control_ioctl(ap_id
, SATA_CFGA_PORT_SELF_TEST
,
1106 0, (void **)&str_p
, &size
);
1108 /* Unrecognized operation request */
1109 rv
= CFGA_SATA_HWOPNOTSUPP
;
1113 cleanup_after_devctl_cmd(hdl
, list
);
1115 return (sata_err_msg(errstring
, rv
, ap_id
, errno
));
1119 /* cfgadm entry point */
1124 const char *options
,
1125 struct cfga_msg
*msgp
,
1129 /* Should call ioctl for self test - phase 2 */
1130 return (CFGA_OPNOTSUPP
);
1135 sata_check_target_node(di_node_t node
, void *arg
)
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
);
1145 (void) strcpy(arg
, cp
);
1148 return (DI_WALK_TERMINATE
);
1152 return (DI_WALK_CONTINUE
);
1162 chk_dev_fcn(di_node_t node
, di_minor_t minor
, void *arg
)
1165 struct chk_dev
*chkp
= (struct chk_dev
*)arg
;
1167 mn
= di_minor_name(minor
);
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.
1184 * 1 - minor exists and is of type BLK
1185 * 0 - minor does not exist or is not of type BLK.
1188 is_devinfo_blk(char *minor_path
)
1190 char *minor_portion
;
1191 struct chk_dev chk_dev
;
1196 * prune minor path for di_init() - no /devices prefix and no minor name
1198 if (strncmp(minor_path
, "/devices/", strlen("/devices/")) != 0)
1201 minor_portion
= strrchr(minor_path
, *MINOR_SEP
);
1202 if (minor_portion
== NULL
)
1207 node
= di_init(minor_path
+ strlen("/devices"), DINFOMINOR
);
1209 *minor_portion
= *MINOR_SEP
;
1211 if (node
== DI_NODE_NIL
)
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
);
1221 if (rv
== 0 && chk_dev
.c_isblk
)
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
;
1236 char minor_path
[MAXPATHLEN
];
1237 char name_part
[MAXNAMELEN
];
1238 char *devlink
= NULL
;
1239 char *minor_portion
= NULL
;
1244 struct dirent
*dep
= NULL
;
1245 struct dirent
*newdep
= NULL
;
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
);
1261 cp
= strrchr(devpath
, *PATH_SEP
);
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
1272 if ((dp
= opendir(devpath
)) == NULL
) {
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
);
1288 while ((err
= readdir_r(dp
, dep
, &newdep
)) == 0 &&
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
)
1300 if (strcmp(dep
->d_name
, name_part
) != 0)
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)
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
)) {
1327 if (S_ISBLK(sb
.st_mode
))
1332 (void) closedir(dp
);
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
)
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
))
1369 *dyncomp
= strdup(cp
);
1373 } else if (strcmp(type
, "tape") == 0) {
1376 * For tape device, logical name looks like
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
);
1391 return (SATA_CFGA_OK
);
1396 (void) closedir(dp
);
1399 return (CFGA_SATA_DYNAMIC_AP
);
1402 /* cfgadm entry point */
1407 cfga_list_data_t
**ap_id_list
,
1409 const char *options
,
1410 const char *listopts
,
1415 char *ap_id_log
= NULL
;
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
;
1422 boolean_t pmult
= B_FALSE
;
1426 if ((rv
= verify_params(ap_id
, options
, errstring
)) != CFGA_SATA_OK
) {
1427 (void) cfga_help(NULL
, options
, flags
);
1430 /* We do not care here about dynamic AP name component */
1431 if ((pdyn
= GET_DYN(ap_id
)) != NULL
) {
1435 if (ap_id_list
== NULL
|| nlistp
== NULL
) {
1436 rv
= CFGA_SATA_DATA_ERROR
;
1437 (void) cfga_help(NULL
, options
, flags
);
1442 if ((rv
= setup_for_devctl_cmd(ap_id
, &devctl_hdl
, &user_nvlist
,
1443 DC_RDONLY
)) != CFGA_SATA_OK
) {
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
;
1455 cleanup_after_devctl_cmd(devctl_hdl
, user_nvlist
);
1458 * Create cfga_list_data_t struct.
1461 (cfga_list_data_t
*)malloc(sizeof (**ap_id_list
))) == NULL
) {
1462 rv
= CFGA_SATA_ALLOC_FAIL
;
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
);
1477 rv
= CFGA_SATA_DEVLINK
;
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
;
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
));
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
;
1502 case AP_RSTATE_DISCONNECTED
:
1503 (*ap_id_list
)->ap_r_state
= CFGA_STAT_DISCONNECTED
;
1506 case AP_RSTATE_CONNECTED
:
1507 (*ap_id_list
)->ap_r_state
= CFGA_STAT_CONNECTED
;
1511 rv
= CFGA_SATA_STATE
;
1515 switch (devctl_ap_state
.ap_ostate
) {
1516 case AP_OSTATE_CONFIGURED
:
1517 (*ap_id_list
)->ap_o_state
= CFGA_STAT_CONFIGURED
;
1520 case AP_OSTATE_UNCONFIGURED
:
1521 (*ap_id_list
)->ap_o_state
= CFGA_STAT_UNCONFIGURED
;
1525 rv
= CFGA_SATA_STATE
;
1529 switch (devctl_ap_state
.ap_condition
) {
1531 (*ap_id_list
)->ap_cond
= CFGA_COND_OK
;
1534 case AP_COND_FAILING
:
1535 (*ap_id_list
)->ap_cond
= CFGA_COND_FAILING
;
1538 case AP_COND_FAILED
:
1539 (*ap_id_list
)->ap_cond
= CFGA_COND_FAILED
;
1542 case AP_COND_UNUSABLE
:
1543 (*ap_id_list
)->ap_cond
= CFGA_COND_UNUSABLE
;
1546 case AP_COND_UNKNOWN
:
1547 (*ap_id_list
)->ap_cond
= CFGA_COND_UNKNOWN
;
1551 rv
= CFGA_SATA_STATE
;
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
) {
1565 * Fill in the 'Information' field for the -v option
1568 if ((rv
= do_control_ioctl(ap_id
, SATA_CFGA_GET_MODEL_INFO
,
1569 0, (void **)&str_p
, &size
)) != CFGA_SATA_OK
) {
1571 "SATA_CFGA_GET_MODULE_INFO ioctl failed\n");
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')
1579 else if (str_p
[i
] != '\0')
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
));
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
) {
1598 "SATA_CFGA_GET_REVFIRMWARE_INFO ioctl failed\n");
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')
1606 else if (str_p
[i
] != '\0')
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
));
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
) {
1625 "SATA_CFGA_GET_SERIALNUMBER_INFO ioctl failed\n");
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')
1633 else if (str_p
[i
] != '\0')
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
));
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
) {
1650 "SATA_CFGA_GET_AP_TYPE ioctl failed\n");
1654 (void) strlcpy((*ap_id_list
)->ap_type
, str_p
,
1655 sizeof ((*ap_id_list
)->ap_type
));
1660 * Checking device type. Port multiplier has no dynamic
1663 if (strncmp((*ap_id_list
)->ap_type
, "sata-pmult",
1664 sizeof ("sata-pmult")) == 0)
1667 if ((*ap_id_list
)->ap_o_state
== CFGA_STAT_CONFIGURED
&&
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
)
1680 if (dyncomp
!= NULL
) {
1681 (void) strcat((*ap_id_list
)->ap_log_id
,
1683 (void) strlcat((*ap_id_list
)->ap_log_id
,
1685 sizeof ((*ap_id_list
)->ap_log_id
));
1691 /* This is an empty port */
1692 if (get_port_num(ap_id
, &port
) != CFGA_SATA_OK
) {
1696 if (port
& SATA_CFGA_PMPORT_QUAL
) {
1697 (void) strlcpy((*ap_id_list
)->ap_type
, "pmult-port",
1698 sizeof ((*ap_id_list
)->ap_type
));
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
));
1708 if (*ap_id_list
!= NULL
) {
1711 if (ap_id_log
!= NULL
) {
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.
1722 cfga_msg(struct cfga_msg
*msgp
, const char *str
)
1727 if (msgp
== NULL
|| msgp
->message_routine
== NULL
) {
1728 (void) printf("cfga_msg: NULL msgp\n");
1732 if ((len
= strlen(str
)) == 0) {
1733 (void) printf("cfga_msg: null str\n");
1737 if ((q
= (char *)calloc(len
+ 1, 1)) == NULL
) {
1742 (void) strcpy(q
, str
);
1743 (*msgp
->message_routine
)(msgp
->appdata_ptr
, q
);
1748 /* cfgadm entry point */
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
]);
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.
1779 verify_valid_apid(const char *ap_id
)
1786 l_ap_id
= strrchr(ap_id
, (int)*MINOR_SEP
);
1789 if (strspn(l_ap_id
, "0123456789.") != strlen(l_ap_id
)) {
1790 /* Bad characters in the ap_id */
1794 if (strstr(l_ap_id
, "..") != NULL
) {
1795 /* ap_id has 1..2 or more than 2 dots */
1805 * Verify the params passed in are valid.
1807 static cfga_sata_ret_t
1810 const char *options
,
1813 char *pdyn
, *lap_id
;
1816 if (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
) {
1833 if (verify_valid_apid(lap_id
) != 0) {
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
);
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 */
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.
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(
1890 devctl_hdl_t
*devctl_hdl
,
1891 nvlist_t
**user_nvlistp
,
1896 cfga_sata_ret_t rv
= CFGA_SATA_OK
;
1897 char *lap_id
, *pdyn
;
1899 lap_id
= strdup(ap_id
);
1901 return (CFGA_SATA_ALLOC_FAIL
);
1902 if ((pdyn
= GET_DYN(lap_id
)) != NULL
) {
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",
1911 rv
= CFGA_SATA_DEVCTL
;
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");
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
) {
1932 "setup_for_devctl_cmd: get_port_num, errno: %d\n",
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
;
1949 (void) cleanup_after_devctl_cmd(*devctl_hdl
, *user_nvlistp
);
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.
1980 do_control_ioctl(const char *ap_id
, sata_cfga_apctl_t subcommand
, uint_t arg
,
1981 void **descrp
, size_t *sizep
)
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
);
1991 assert(sizep
!= NULL
);
1993 if ((rv
= get_port_num(ap_id
, &port
)) != CFGA_SATA_OK
) {
1997 if ((fd
= open(ap_id
, O_RDONLY
)) == -1) {
1998 (void) printf("do_control_ioctl: open failed: errno:%d\n",
2000 rv
= CFGA_SATA_OPEN
;
2001 if (errno
== EBUSY
) {
2002 rv
= CFGA_SATA_BUSY
;
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
;
2030 *sizep
= local_size
;
2032 if (local_size
== 0) {
2033 (void) printf("zero length data\n");
2034 rv
= CFGA_SATA_ZEROLEN
;
2037 if ((*descrp
= malloc(*sizep
)) == NULL
) {
2038 (void) printf("do_control_ioctl: malloc failed\n");
2039 rv
= CFGA_SATA_ALLOC_FAIL
;
2045 ioctl_data
.get_size
= B_FALSE
;
2046 ioctl_data
.buf
= *descrp
;
2047 ioctl_data
.bufsiz
= *sizep
;
2051 if (ioctl(fd
, DEVCTL_AP_CONTROL
, &ioctl_data
) != 0) {
2052 rv
= CFGA_SATA_IOCTL
;
2064 if (*descrp
!= NULL
) {
2069 if (rv
== CFGA_SATA_IOCTL
&& errno
== EBUSY
) {
2070 rv
= CFGA_SATA_BUSY
;
2078 sata_confirm(struct cfga_confirm
*confp
, char *msg
)
2082 if (confp
== NULL
|| confp
->confirm
== NULL
) {
2085 rval
= (*confp
->confirm
)(confp
->appdata_ptr
, msg
);
2092 sata_get_devicepath(const char *ap_id
)
2094 char *devpath
= NULL
;
2098 rv
= do_control_ioctl(ap_id
, SATA_CFGA_GET_DEVICE_PATH
, 0,
2099 (void **)&devpath
, &size
);
2101 if (rv
== CFGA_SATA_OK
) {