2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A
2N/A#include <stdio.h>
2N/A#include <strings.h>
2N/A#include <libgen.h>
2N/A#include <cfga_scsi.h>
2N/A#include <sys/scfd/opcioif.h>
2N/A
2N/A
2N/A#define SCF_DRV "/devices/pseudo/scfd@200:rasctl"
2N/A#define SCFRETRY 3
2N/A#define SCFIOCWAIT 3
2N/A
2N/A
2N/A#define OPL_LOCATOR_OPT 0
2N/A#define OPL_LED_OPT 1
2N/A#define OPL_MODE_OPT 2
2N/Achar *opl_opts[] = {
2N/A "locator",
2N/A "led",
2N/A "mode",
2N/A NULL
2N/A};
2N/A
2N/A
2N/Astatic scfga_ret_t
2N/Aopl_get_scf_logical_disk(const apid_t *apidp, char **errstring,
2N/A scfiocgetdiskled_t *scf_disk)
2N/A{
2N/A int len;
2N/A char *phys_path;
2N/A char *strptr;
2N/A
2N/A phys_path = strdup(apidp->path);
2N/A if (phys_path == NULL) {
2N/A cfga_err(errstring, ENOMEM, ERR_OP_FAILED, 0);
2N/A return (SCFGA_ERR);
2N/A }
2N/A scf_disk->path[0] = '\0';
2N/A if ((strptr = strstr(phys_path, ":")) != NULL) {
2N/A strptr[0] = '\0';
2N/A len = snprintf((char *)scf_disk->path, sizeof (scf_disk->path),
2N/A "%s", (char *)(phys_path));
2N/A if (len >= sizeof (scf_disk->path)) {
2N/A free(phys_path);
2N/A cfga_err(errstring, 0, ERR_OP_FAILED, 0);
2N/A return (SCFGA_ERR);
2N/A }
2N/A } else {
2N/A free(phys_path);
2N/A cfga_err(errstring, 0, ERR_UNKNOWN, 0);
2N/A return (SCFGA_ERR);
2N/A }
2N/A free(phys_path);
2N/A
2N/A return (SCFGA_OK);
2N/A}
2N/A
2N/A
2N/A/*
2N/A * Open the SCF driver and use the ioctl interface to set or get the status.
2N/A *
2N/A * Returns 0 on success. Returns OP_FAILED on error.
2N/A */
2N/Astatic scfga_ret_t
2N/Aopl_disk_led_control(apid_t *apidp, char **errstring, struct cfga_msg *msgp,
2N/A int request, scfiocgetdiskled_t *scf_disk)
2N/A{
2N/A scfga_ret_t retval;
2N/A int scf_fd = -1;
2N/A int retry = 0;
2N/A
2N/A /* paranoid check */
2N/A if ((apidp == NULL) || (msgp == NULL) || (scf_disk == NULL)) {
2N/A cfga_err(errstring, 0, ERR_UNKNOWN, 0, 0);
2N/A return (SCFGA_ERR);
2N/A }
2N/A
2N/A retval = opl_get_scf_logical_disk((const apid_t *)apidp, errstring,
2N/A scf_disk);
2N/A if (retval != SCFGA_OK) {
2N/A /* errstring is set in opl_get_scf_logical_disk */
2N/A return (retval);
2N/A }
2N/A
2N/A /* Open a file descriptor for the scf driver. */
2N/A scf_fd = open(SCF_DRV, O_RDWR);
2N/A if (scf_fd < 0) {
2N/A cfga_err(errstring, errno, ERRARG_OPEN, SCF_DRV, 0);
2N/A return (SCFGA_LIB_ERR);
2N/A }
2N/A
2N/A /*
2N/A * Use the ioctl interface with the SCF driver to get/set the
2N/A * hdd locator indicator.
2N/A */
2N/A errno = 0;
2N/A while (ioctl(scf_fd, request, scf_disk) < 0) {
2N/A /* Check Retry Error Number */
2N/A if (errno != EBUSY && errno != EIO) {
2N/A break;
2N/A }
2N/A
2N/A /* Check Retry Times */
2N/A if (++retry > SCFRETRY) {
2N/A break;
2N/A }
2N/A errno = 0;
2N/A
2N/A (void) sleep(SCFIOCWAIT);
2N/A }
2N/A (void) close(scf_fd);
2N/A
2N/A if ((errno != 0) || (retry > SCFRETRY)) {
2N/A cfga_err(errstring, errno, ERR_OP_FAILED, 0, 0);
2N/A return (SCFGA_LIB_ERR);
2N/A }
2N/A return (SCFGA_OK);
2N/A}
2N/A
2N/A/*
2N/A * Print the value of the hard disk locator in a human friendly form.
2N/A */
2N/Astatic void
2N/Aopl_print_locator(apid_t *apidp, struct cfga_msg *msgp, unsigned char led)
2N/A{
2N/A led_modeid_t mode = LED_MODE_UNK;
2N/A
2N/A if ((msgp == NULL) || (msgp->message_routine == NULL)) {
2N/A return;
2N/A }
2N/A
2N/A cfga_msg(msgp, MSG_LED_HDR, 0);
2N/A switch ((int)led) {
2N/A case SCF_DISK_LED_ON:
2N/A mode = LED_MODE_FAULTED;
2N/A break;
2N/A
2N/A case SCF_DISK_LED_OFF:
2N/A mode = LED_MODE_OFF;
2N/A break;
2N/A
2N/A case SCF_DISK_LED_BLINK:
2N/A mode = LED_MODE_ON;
2N/A break;
2N/A
2N/A default:
2N/A mode = LED_MODE_UNK;
2N/A }
2N/A cfga_led_msg(msgp, apidp, LED_STR_LOCATOR, mode);
2N/A}
2N/A
2N/A/*
2N/A * Print the value of the hard disk fault LED in a human friendly form.
2N/A */
2N/Astatic void
2N/Aopl_print_led(apid_t *apidp, struct cfga_msg *msgp, unsigned char led)
2N/A{
2N/A led_modeid_t mode = LED_MODE_UNK;
2N/A
2N/A if ((msgp == NULL) || (msgp->message_routine == NULL)) {
2N/A return;
2N/A }
2N/A
2N/A cfga_msg(msgp, MSG_LED_HDR, 0);
2N/A switch ((int)led) {
2N/A case SCF_DISK_LED_ON:
2N/A mode = LED_MODE_ON;
2N/A break;
2N/A
2N/A case SCF_DISK_LED_OFF:
2N/A mode = LED_MODE_OFF;
2N/A break;
2N/A
2N/A case SCF_DISK_LED_BLINK:
2N/A mode = LED_MODE_BLINK;
2N/A break;
2N/A
2N/A default:
2N/A mode = LED_MODE_UNK;
2N/A }
2N/A cfga_led_msg(msgp, apidp, LED_STR_FAULT, mode);
2N/A}
2N/A
2N/Astatic scfga_ret_t
2N/Aopl_setlocator(
2N/A const char *mode,
2N/A apid_t *apidp,
2N/A char **errstring,
2N/A struct cfga_msg *msgp)
2N/A{
2N/A scfga_ret_t retval;
2N/A scfiocgetdiskled_t scf_disk;
2N/A
2N/A if (strcmp(mode, "on") == 0) {
2N/A scf_disk.led = SCF_DISK_LED_BLINK;
2N/A } else if (strcmp(mode, "off") == 0) {
2N/A scf_disk.led = SCF_DISK_LED_OFF;
2N/A } else {
2N/A cfga_err(errstring, 0, ERRARG_OPT_INVAL, mode, 0);
2N/A return (SCFGA_ERR);
2N/A }
2N/A
2N/A retval = opl_disk_led_control(apidp, errstring, msgp,
2N/A SCFIOCSETDISKLED, &scf_disk);
2N/A
2N/A return (retval);
2N/A}
2N/A
2N/A
2N/Astatic scfga_ret_t
2N/Aopl_getled(
2N/A int print_switch,
2N/A apid_t *apidp,
2N/A char **errstring,
2N/A struct cfga_msg *msgp)
2N/A{
2N/A scfga_ret_t retval;
2N/A scfiocgetdiskled_t scf_disk;
2N/A
2N/A (void) memset((void *)&scf_disk, 0, sizeof (scf_disk));
2N/A
2N/A retval = opl_disk_led_control(apidp, errstring, msgp,
2N/A SCFIOCGETDISKLED, &scf_disk);
2N/A if (retval != SCFGA_OK) {
2N/A return (retval);
2N/A }
2N/A if (print_switch == OPL_LED_OPT) {
2N/A opl_print_led(apidp, msgp, scf_disk.led);
2N/A } else {
2N/A opl_print_locator(apidp, msgp, scf_disk.led);
2N/A }
2N/A
2N/A return (SCFGA_OK);
2N/A}
2N/A
2N/A
2N/Astatic scfga_ret_t
2N/Aopl_setled(
2N/A const char *mode,
2N/A apid_t *apidp,
2N/A char **errstring,
2N/A struct cfga_msg *msgp)
2N/A{
2N/A scfga_ret_t retval;
2N/A scfiocgetdiskled_t scf_disk;
2N/A
2N/A (void) memset((void *)&scf_disk, 0, sizeof (scf_disk));
2N/A
2N/A if (strcmp(mode, "on") == 0) {
2N/A scf_disk.led = SCF_DISK_LED_ON;
2N/A } else if (strcmp(mode, "off") == 0) {
2N/A scf_disk.led = SCF_DISK_LED_OFF;
2N/A } else if (strcmp(mode, "blink") == 0) {
2N/A scf_disk.led = SCF_DISK_LED_BLINK;
2N/A } else {
2N/A cfga_err(errstring, 0, ERRARG_OPT_INVAL, mode, 0);
2N/A return (SCFGA_ERR);
2N/A }
2N/A
2N/A retval = opl_disk_led_control(apidp, errstring, msgp,
2N/A SCFIOCSETDISKLED, &scf_disk);
2N/A return (retval);
2N/A}
2N/A
2N/A/*
2N/A * The func argument is a string in one of the two following forms:
2N/A * led=LED[,mode=MODE]
2N/A * locator[=on|off]
2N/A * which can generically be thought of as:
2N/A * name=value[,name=value]
2N/A * So first, split the function based on the comma into two name-value
2N/A * pairs.
2N/A */
2N/A/*ARGSUSED*/
2N/Ascfga_ret_t
2N/Aplat_dev_led(
2N/A const char *func,
2N/A scfga_cmd_t cmd,
2N/A apid_t *apidp,
2N/A prompt_t *argsp,
2N/A cfga_flags_t flags,
2N/A char **errstring)
2N/A{
2N/A scfga_ret_t retval = SCFGA_ERR;
2N/A char *optptr = (char *)func;
2N/A char *value = NULL;
2N/A
2N/A int opt_locator = 0;
2N/A int opt_led = 0;
2N/A int opt_mode = 0;
2N/A char *locator_value = NULL;
2N/A char *led_value = NULL;
2N/A char *mode_value = NULL;
2N/A
2N/A if (func == NULL) {
2N/A return (SCFGA_ERR);
2N/A }
2N/A
2N/A while (*optptr != '\0') {
2N/A switch (getsubopt(&optptr, opl_opts, &value)) {
2N/A case OPL_LOCATOR_OPT:
2N/A opt_locator++;
2N/A locator_value = value;
2N/A break;
2N/A case OPL_LED_OPT:
2N/A opt_led++;
2N/A led_value = value;
2N/A break;
2N/A case OPL_MODE_OPT:
2N/A opt_mode++;
2N/A mode_value = value;
2N/A break;
2N/A default:
2N/A cfga_err(errstring, 0, ERR_CMD_INVAL, 0);
2N/A return (SCFGA_OPNOTSUPP);
2N/A break;
2N/A }
2N/A }
2N/A
2N/A if (!opt_locator && !opt_led) {
2N/A cfga_err(errstring, 0, ERR_CMD_INVAL, 0);
2N/A return (SCFGA_ERR);
2N/A }
2N/A
2N/A if (opt_locator) {
2N/A if ((opt_locator > 1) || opt_led || opt_mode ||
2N/A (strncmp(func, "locator", strlen("locator")) != 0) ||
2N/A (locator_value &&
2N/A (strcmp(locator_value, "blink") == 0))) {
2N/A cfga_err(errstring, 0, ERR_CMD_INVAL, 0);
2N/A return (SCFGA_ERR);
2N/A }
2N/A
2N/A /* Options are sane so set or get the locator. */
2N/A if (locator_value) {
2N/A retval = opl_setlocator(locator_value, apidp,
2N/A errstring, argsp->msgp);
2N/A } else {
2N/A retval = opl_getled(OPL_LOCATOR_OPT, apidp, errstring,
2N/A argsp->msgp);
2N/A }
2N/A }
2N/A if (opt_led) {
2N/A if ((opt_led > 1) || (opt_mode > 1) || (opt_locator) ||
2N/A (strncmp(func, "led", strlen("led")) != 0) ||
2N/A (!led_value || strcmp(led_value, "fault")) ||
2N/A (opt_mode && !mode_value)) {
2N/A
2N/A cfga_err(errstring, 0, ERR_CMD_INVAL, 0);
2N/A return (SCFGA_ERR);
2N/A }
2N/A
2N/A /* options are sane so go ahead and set or get the led */
2N/A if (mode_value != NULL) {
2N/A retval = opl_setled(mode_value, apidp, errstring,
2N/A argsp->msgp);
2N/A } else {
2N/A retval = opl_getled(OPL_LED_OPT, apidp, errstring,
2N/A argsp->msgp);
2N/A }
2N/A }
2N/A return (retval);
2N/A
2N/A}