piclsbl.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* PICL Ontario platform plug-in to message the SC to light
* or extinguish the hdd 'OK2RM' ready-to-service light in
* the event of a soft unconfigure or configure, respectively.
*/
#include <picl.h>
#include <picltree.h>
#include <picldefs.h>
#include <stdio.h>
#include <umem.h>
#include <unistd.h>
#include <libnvpair.h>
#include <strings.h>
#include <syslog.h>
#include <dlfcn.h>
#include <link.h>
#include <signal.h>
#include <libpcp.h>
#include "piclsbl.h"
#include "errno.h"
#pragma init(piclsbl_register)
static void *pcp_handle;
static int (* pcp_init_ptr)();
static int (* pcp_send_recv_ptr)();
static int (* pcp_close_ptr)();
static int load_pcp_libs(void);
static void piclsbl_init(void);
static void piclsbl_fini(void);
static void piclsbl_register(void);
static void piclsbl_handler(const char *ename, const void *earg,
size_t size, void *cookie);
static picld_plugin_reg_t piclsbl_reg = {
PICLD_PLUGIN_VERSION_1,
PICLD_PLUGIN_CRITICAL,
"piclsbl",
piclsbl_init,
piclsbl_fini
};
/*
* called from init to load the pcp library
*/
static int
load_pcp_libs()
{
char pcp_dl_lib[80];
(void) snprintf(pcp_dl_lib, sizeof (pcp_dl_lib), "%s%s",
LIB_PCP_PATH, PCPLIB);
/* load the library and set up function pointers */
if ((pcp_handle = dlopen(pcp_dl_lib, RTLD_NOW)) == (void *) NULL)
return (1);
pcp_init_ptr = (int(*)())dlsym(pcp_handle, "pcp_init");
pcp_close_ptr = (int(*)())dlsym(pcp_handle, "pcp_close");
pcp_send_recv_ptr = (int(*)())dlsym(pcp_handle, "pcp_send_recv");
if (pcp_init_ptr == NULL || pcp_send_recv_ptr == NULL ||
pcp_close_ptr == NULL)
return (1);
return (0);
}
/*
* callback routine for ptree_walk_tree_by_class()
*/
static int
cb_find_disk(picl_nodehdl_t node, void *args)
{
disk_lookup_t *lookup = (disk_lookup_t *)args;
int status = -1;
char path[PICL_PROPNAMELEN_MAX];
status = ptree_get_propval_by_name(node, "Path", (void *)&path,
PICL_PROPNAMELEN_MAX);
if (status != PICL_SUCCESS) {
return (PICL_WALK_CONTINUE);
}
if (strcmp(path, lookup->path) == 0) {
lookup->disk = node;
lookup->result = DISK_FOUND;
return (PICL_WALK_TERMINATE);
}
return (PICL_WALK_CONTINUE);
}
/*
* Ontario SBL event handler, subscribed to:
* PICLEVENT_SYSEVENT_DEVICE_ADDED
* PICLEVENT_SYSEVENT_DEVICE_REMOVED
*/
static void
piclsbl_handler(const char *ename, const void *earg, size_t size,
void *cookie)
{
char *devfs_path;
char hdd_location[PICL_PROPNAMELEN_MAX];
nvlist_t *nvlp = NULL;
pcp_msg_t send_msg;
pcp_msg_t recv_msg;
pcp_sbl_req_t *req_ptr = NULL;
pcp_sbl_resp_t *resp_ptr = NULL;
int status = -1;
disk_lookup_t lookup;
/*
* setup the request data to attach to the libpcp msg
*/
if ((req_ptr = (pcp_sbl_req_t *)umem_zalloc(sizeof (pcp_sbl_req_t),
UMEM_DEFAULT)) == NULL)
goto sbl_return;
/*
* This plugin serves to enable or disable the blue RAS
* 'ok-to-remove' LED that is on each of the 4 disks on the
* Ontario. We catch the event via the picl handler, and
* if the event is DEVICE_ADDED for one of our onboard disks,
* then we'll be turning off the LED. Otherwise, if the event
* is DEVICE_REMOVED, then we turn it on.
*/
if (strcmp(ename, PICLEVENT_SYSEVENT_DEVICE_ADDED) == 0)
req_ptr->sbl_action = PCP_SBL_DISABLE;
else if (strcmp(ename, PICLEVENT_SYSEVENT_DEVICE_REMOVED) == 0)
req_ptr->sbl_action = PCP_SBL_ENABLE;
else
goto sbl_return;
/*
* retrieve the device's physical path from the event payload
*/
if (nvlist_unpack((char *)earg, size, &nvlp, NULL))
goto sbl_return;
if (nvlist_lookup_string(nvlp, "devfs-path", &devfs_path))
goto sbl_return;
/*
* look for this disk in the picl tree, and if it's
* location indicates that it's one of our internal
* disks, then set sbl_id to incdicate which one.
* otherwise, return as it is not one of our disks.
*/
lookup.path = strdup(devfs_path);
lookup.disk = NULL;
lookup.result = DISK_NOT_FOUND;
/* first, find the disk */
status = ptree_walk_tree_by_class(root_node, "disk", (void *)&lookup,
cb_find_disk);
if (status != PICL_SUCCESS)
goto sbl_return;
if (lookup.result == DISK_FOUND) {
/* now, lookup it's location in the node */
status = ptree_get_propval_by_name(lookup.disk, "Location",
(void *)&hdd_location, PICL_PROPNAMELEN_MAX);
if (status != PICL_SUCCESS) {
syslog(LOG_ERR, "piclsbl: failed hdd discovery");
goto sbl_return;
}
}
if (strcmp(hdd_location, HDD0) == 0)
req_ptr->sbl_id = PCP_SBL_HDD0;
else if (strcmp(hdd_location, HDD1) == 0)
req_ptr->sbl_id = PCP_SBL_HDD1;
else if (strcmp(hdd_location, HDD2) == 0)
req_ptr->sbl_id = PCP_SBL_HDD2;
else if (strcmp(hdd_location, HDD3) == 0)
req_ptr->sbl_id = PCP_SBL_HDD3;
else {
syslog(LOG_ERR, "piclsbl: hdd location does not match any hdd");
goto sbl_return;
}
/*
* we have the information we need, init the platform channel.
* the platform channel driver will only allow one connection
* at a time on this socket. on the offchance that more than
* one event comes in, we'll retry to initialize this connection
* up to 3 times
*/
if ((*pcp_init_ptr)(LED_CHANNEL) < 0) {
/* failed to init; wait and retry up to 3 times */
int s = PCPINIT_TIMEOUT;
int retries = 0;
while (retries++) {
(void) sleep(s);
if ((*pcp_init_ptr)(LED_CHANNEL) == 0)
break;
else if (retries == 3) {
syslog(LOG_ERR, "piclsbl: ",
"SC channel initialization failed");
goto sbl_return;
}
/* continue */
}
}
/*
* populate the message for libpcp
*/
send_msg.msg_type = PCP_SBL_CONTROL;
send_msg.sub_type = NULL;
send_msg.msg_len = sizeof (pcp_sbl_req_t);
send_msg.msg_data = (uint8_t *)req_ptr;
/*
* send the request, receive the response
*/
if ((*pcp_send_recv_ptr)(&send_msg, &recv_msg, PCPCOMM_TIMEOUT) < 0) {
/* we either timed out or erred; either way try again */
int s = PCPCOMM_TIMEOUT;
(void) sleep(s);
if ((*pcp_send_recv_ptr)(&send_msg, &recv_msg,
PCPCOMM_TIMEOUT) < 0) {
syslog(LOG_ERR, "piclsbl: communication failure");
goto sbl_return;
}
}
/*
* validate that this data was meant for us
*/
if (recv_msg.msg_type != PCP_SBL_CONTROL_R) {
syslog(LOG_ERR, "piclsbl: unbound packet received");
goto sbl_return;
}
/*
* verify that the LED action has taken place
*/
resp_ptr = (pcp_sbl_resp_t *)recv_msg.msg_data;
if (resp_ptr->status == PCP_SBL_ERROR) {
syslog(LOG_ERR, "piclsbl: OK2RM LED action error");
goto sbl_return;
}
/*
* ensure the LED action taken is the one requested
*/
if ((req_ptr->sbl_action == PCP_SBL_DISABLE) &&
(resp_ptr->sbl_state != SBL_STATE_OFF))
syslog(LOG_ERR, "piclsbl: OK2RM LED not OFF after disk "
"configuration");
else if ((req_ptr->sbl_action == PCP_SBL_ENABLE) &&
(resp_ptr->sbl_state != SBL_STATE_ON))
syslog(LOG_ERR, "piclsbl: OK2RM LED not ON after disk "
"unconfiguration");
else if (resp_ptr->sbl_state == SBL_STATE_UNKNOWN)
syslog(LOG_ERR, "piclsbl: OK2RM LED set to unknown state");
sbl_return:
(*pcp_close_ptr)();
if (req_ptr != NULL)
umem_free(req_ptr, sizeof (pcp_sbl_req_t));
if (resp_ptr != NULL)
free(resp_ptr);
if (nvlp != NULL)
nvlist_free(nvlp);
}
static void
piclsbl_init(void)
{
/* retrieve the root node for lookups in the event handler */
if ((ptree_get_root(&root_node)) != NULL)
return;
/* load libpcp */
if (load_pcp_libs()) {
syslog(LOG_ERR, "piclsbl: failed to load libpcp");
syslog(LOG_ERR, "piclsbl: aborting");
return;
}
/*
* register piclsbl_handler for both "sysevent-device-added" and
* and for "sysevent-device-removed" PICL events
*/
(void) ptree_register_handler(PICLEVENT_SYSEVENT_DEVICE_ADDED,
piclsbl_handler, NULL);
(void) ptree_register_handler(PICLEVENT_SYSEVENT_DEVICE_REMOVED,
piclsbl_handler, NULL);
}
static void
piclsbl_fini(void)
{
/* unregister the event handler */
(void) ptree_unregister_handler(PICLEVENT_SYSEVENT_DEVICE_ADDED,
piclsbl_handler, NULL);
(void) ptree_unregister_handler(PICLEVENT_SYSEVENT_DEVICE_REMOVED,
piclsbl_handler, NULL);
}
static void
piclsbl_register(void)
{
picld_plugin_register(&piclsbl_reg);
}