#include <pri.h>
#include "priplugin.h"
#pragma init(priplugin_register)
static md_t *mdp;
static mutex_t rebuild_lock;
static cond_t rebuild_cv;
static thread_t pri_worker_thread_id, pri_reader_thread_id;
static boolean_t all_thr_exit = B_FALSE;
static boolean_t event_caught = B_FALSE;
static void priplugin_init(void);
static void priplugin_fini(void);
static void
event_handler(const char *ename, const void *earg, size_t size, void *cookie);
static void *pri_worker_thread(void *arg);
static void *pri_reader_thread(void *arg);
static int remove_old_segments(picl_nodehdl_t node, void *args);
picld_plugin_reg_t priplugin_reg = {
PICLD_PLUGIN_VERSION_1,
PICLD_PLUGIN_CRITICAL,
"pri_plugin",
priplugin_init,
priplugin_fini
};
static void
set_prop_info(ptree_propinfo_t *propinfo, int size, char *name, int type)
{
propinfo->version = PICLD_PLUGIN_VERSION_1;
propinfo->read = NULL;
propinfo->write = NULL;
propinfo->piclinfo.type = type;
propinfo->piclinfo.accessmode = PICL_READ;
propinfo->piclinfo.size = size;
(void) strlcpy(propinfo->piclinfo.name, name,
sizeof (propinfo->piclinfo.name));
}
boolean_t
prop_exists(picl_nodehdl_t node, char *name)
{
int status;
picl_prophdl_t proph;
status = ptree_get_prop_by_name(node, name, &proph);
if (status == PICL_SUCCESS)
return (B_TRUE);
else
return (B_FALSE);
}
void
add_md_prop(picl_nodehdl_t node, int size, char *name, void* value, int type)
{
ptree_propinfo_t propinfo;
picl_prophdl_t proph;
if (!prop_exists(node, name)) {
set_prop_info(&propinfo, size, name, type);
(void) ptree_create_and_add_prop(node, &propinfo,
value, &proph);
}
}
static int
remove_old_segments(picl_nodehdl_t node, void *args)
{
int status;
if ((status = ptree_delete_node(node)) == PICL_SUCCESS)
ptree_destroy_node(node);
else
pri_debug(LOG_NOTICE, "remove_old_segments: can't delete "
"segment node: %s\n", picl_strerror(status));
return (PICL_WALK_CONTINUE);
}
static void
priplugin_init(void)
{
int status;
pri_debug(LOG_NOTICE, "priplugin: mem tree and io label thread "
"being created; callbacks being registered\n");
all_thr_exit = B_FALSE;
event_caught = B_FALSE;
(void) mutex_init(&rebuild_lock, USYNC_THREAD, NULL);
(void) cond_init(&rebuild_cv, USYNC_THREAD, NULL);
if ((status = thr_create(NULL, 0, pri_worker_thread, NULL, THR_BOUND,
&pri_worker_thread_id)) < 0) {
pri_debug(LOG_NOTICE, "priplugin: can't create worker thread: "
"%d\n", status);
all_thr_exit = B_TRUE;
(void) mutex_destroy(&rebuild_lock);
(void) cond_destroy(&rebuild_cv);
} else if ((status = thr_create(NULL, 0, pri_reader_thread, NULL,
THR_BOUND, &pri_reader_thread_id)) < 0) {
pri_debug(LOG_NOTICE, "priplugin: can't create reader thread: "
"%d\n", status);
(void) mutex_lock(&rebuild_lock);
all_thr_exit = B_TRUE;
(void) cond_signal(&rebuild_cv);
(void) mutex_unlock(&rebuild_lock);
(void) thr_join(pri_worker_thread_id, NULL, NULL);
(void) mutex_destroy(&rebuild_lock);
(void) cond_destroy(&rebuild_cv);
} else {
pri_debug(LOG_NOTICE, "priplugin_init: worker and reader "
"threads created - registering event handlers\n");
(void) ptree_register_handler(PICLEVENT_SYSEVENT_DEVICE_ADDED,
event_handler, NULL);
(void) ptree_register_handler(PICLEVENT_SYSEVENT_DEVICE_REMOVED,
event_handler, NULL);
(void) ptree_register_handler(PICLEVENT_DR_AP_STATE_CHANGE,
event_handler, NULL);
}
}
static void *
pri_worker_thread(void *arg)
{
int status;
picl_nodehdl_t picl_root_node;
pri_debug(LOG_NOTICE, "pri_worker_thread: start\n");
(void) mutex_lock(&rebuild_lock);
while (1) {
(void) cond_wait(&rebuild_cv, &rebuild_lock);
if (all_thr_exit == B_TRUE) {
(void) mutex_unlock(&rebuild_lock);
pri_debug(LOG_NOTICE, "pri_worker_thread: time to "
"exit\n");
break;
}
if (event_caught == B_FALSE) {
status = ptree_get_root(&picl_root_node);
if (status != PICL_SUCCESS) {
pri_debug(LOG_NOTICE, "pri_worker_thread: "
"can't get picl tree root node: %s\n",
picl_strerror(status));
continue;
}
pri_debug(LOG_NOTICE, "pri_worker_thread: have root "
"picl and PRI nodes\n");
status = ptree_walk_tree_by_class(picl_root_node,
"memory-segment", NULL, remove_old_segments);
if (status != PICL_SUCCESS) {
pri_debug(LOG_NOTICE, "pri_worker_thread: "
"can't remove old memory segments: \n",
picl_strerror(status));
} else
pri_debug(LOG_NOTICE, "pri_worker_thread: "
"old memory segments removed\n");
status = ptree_walk_tree_by_class(picl_root_node,
"memory", (void *) mdp, add_mem_prop);
if (status != PICL_SUCCESS) {
pri_debug(LOG_NOTICE, "pri_worker_thread: "
"memory segments walk failed: \n",
picl_strerror(status));
} else
pri_debug(LOG_NOTICE, "pri_worker_thread: "
"success walking memory node\n");
} else
event_caught = B_FALSE;
io_dev_addlabel(mdp);
}
pri_debug(LOG_NOTICE, "pri_worker_thread: exiting\n");
return (NULL);
}
static void *
pri_reader_thread(void *arg)
{
uint64_t tok;
int status, count;
pri_debug(LOG_NOTICE, "pri_reader_thread: thread start\n");
if (pri_init() != 0) {
pri_debug(LOG_NOTICE, "pri_reader_thread: pri_init failed\n");
return (NULL);
}
mdp = NULL;
tok = 0;
count = 0;
while (1) {
status = pri_devinit(&tok);
(void) mutex_lock(&rebuild_lock);
if (all_thr_exit == B_TRUE) {
(void) mutex_unlock(&rebuild_lock);
pri_debug(LOG_NOTICE, "pri_reader_thread: time to "
"exit\n");
break;
}
if (status == 0) {
pri_debug(LOG_NOTICE, "pri_reader_thread: got PRI\n");
mdp = pri_bufinit(mdp);
if (mdp != NULL) {
(void) cond_signal(&rebuild_cv);
count = 0;
} else {
pri_debug(LOG_NOTICE, "pri_reader_thread: "
"NULL mdp!\n");
status = -1;
}
}
if (status == -1) {
if (errno != 0) {
pri_debug(LOG_NOTICE, "pri_reader_thread: "
"can't get PRI contents: %s\n",
strerror(errno));
}
if (++count > 6) {
pri_debug(LOG_NOTICE, "pci_reader_thread: "
"can't process PRI data\n");
(void) mutex_unlock(&rebuild_lock);
break;
}
pri_fini();
tok = 0;
sleep(10);
if (pri_init() != 0) {
pri_debug(LOG_NOTICE, "pci_reader_thread: "
"can't reinitialize PRI driver\n");
(void) mutex_unlock(&rebuild_lock);
break;
}
}
(void) mutex_unlock(&rebuild_lock);
}
pri_debug(LOG_NOTICE, "pri_reader_thread: thread exiting\n");
return (NULL);
}
static void
priplugin_fini(void)
{
pri_debug(LOG_NOTICE, "priplugin_fini: called\n");
if (all_thr_exit == B_TRUE)
return;
(void) ptree_unregister_handler(PICLEVENT_SYSEVENT_DEVICE_ADDED,
event_handler, NULL);
(void) ptree_unregister_handler(PICLEVENT_SYSEVENT_DEVICE_REMOVED,
event_handler, NULL);
(void) ptree_unregister_handler(PICLEVENT_DR_AP_STATE_CHANGE,
event_handler, NULL);
(void) mutex_lock(&rebuild_lock);
all_thr_exit = B_TRUE;
(void) cond_signal(&rebuild_cv);
(void) mutex_unlock(&rebuild_lock);
(void) thr_join(pri_worker_thread_id, NULL, NULL);
pri_devfini(mdp);
mdp = NULL;
pri_fini();
(void) thr_join(pri_reader_thread_id, NULL, NULL);
(void) mutex_destroy(&rebuild_lock);
(void) cond_destroy(&rebuild_cv);
}
void
priplugin_register(void)
{
picld_plugin_register(&priplugin_reg);
}
static void
event_handler(const char *ename, const void *earg, size_t size, void *cookie)
{
pri_debug(LOG_NOTICE, "pri: event_handler: caught event "
"%s\n", ename);
if ((strcmp(ename, PICLEVENT_SYSEVENT_DEVICE_ADDED) == 0) ||
(strcmp(ename, PICLEVENT_SYSEVENT_DEVICE_REMOVED) == 0) ||
(strcmp(ename, PICLEVENT_DR_AP_STATE_CHANGE) == 0)) {
pri_debug(LOG_NOTICE, "pri: event_handler: handle event "
"%s; waking worker thread\n", ename);
(void) mutex_lock(&rebuild_lock);
if (all_thr_exit == B_FALSE) {
event_caught = B_TRUE;
(void) cond_signal(&rebuild_cv);
}
(void) mutex_unlock(&rebuild_lock);
}
}
void
pri_debug(int level, char *fmt, ...)
{
#if (PRI_DEBUG != 0)
va_list ap;
va_start(ap, fmt);
vsyslog(level, fmt, ap);
va_end(ap);
#endif
}