xref: /onnv-gate/usr/src/cmd/picl/plugins/sun4v/pri/priplugin.c (revision 6662:9bd68c85a3c0)
13941Svenki /*
23941Svenki  * CDDL HEADER START
33941Svenki  *
43941Svenki  * The contents of this file are subject to the terms of the
53941Svenki  * Common Development and Distribution License (the "License").
63941Svenki  * You may not use this file except in compliance with the License.
73941Svenki  *
83941Svenki  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
93941Svenki  * or http://www.opensolaris.org/os/licensing.
103941Svenki  * See the License for the specific language governing permissions
113941Svenki  * and limitations under the License.
123941Svenki  *
133941Svenki  * When distributing Covered Code, include this CDDL HEADER in each
143941Svenki  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
153941Svenki  * If applicable, add the following below this CDDL HEADER, with the
163941Svenki  * fields enclosed by brackets "[]" replaced with your own identifying
173941Svenki  * information: Portions Copyright [yyyy] [name of copyright owner]
183941Svenki  *
193941Svenki  * CDDL HEADER END
203941Svenki  */
213941Svenki 
223941Svenki /*
23*6662Sfw157321  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
243941Svenki  * Use is subject to license terms.
253941Svenki  */
263941Svenki 
273941Svenki #pragma ident	"%Z%%M%	%I%	%E% SMI"
283941Svenki 
294669Sfw157321 #include <pri.h>
303941Svenki #include "priplugin.h"
313941Svenki 
323941Svenki #pragma init(priplugin_register)	/* place in .init section */
333941Svenki 
345029Sfw157321 static md_t *mdp;
355029Sfw157321 
365029Sfw157321 static mutex_t	rebuild_lock;
375029Sfw157321 static cond_t	rebuild_cv;
383941Svenki 
395029Sfw157321 static thread_t pri_worker_thread_id, pri_reader_thread_id;
405029Sfw157321 static boolean_t all_thr_exit = B_FALSE;
41*6662Sfw157321 static boolean_t event_caught = B_FALSE;
425029Sfw157321 
435029Sfw157321 static void priplugin_init(void);
445029Sfw157321 static void priplugin_fini(void);
454669Sfw157321 static void
464669Sfw157321 event_handler(const char *ename, const void *earg, size_t size, void *cookie);
475029Sfw157321 static void *pri_worker_thread(void *arg);
485029Sfw157321 static void *pri_reader_thread(void *arg);
495029Sfw157321 static int remove_old_segments(picl_nodehdl_t node, void *args);
505029Sfw157321 
513941Svenki 
523941Svenki picld_plugin_reg_t priplugin_reg = {
533941Svenki 	PICLD_PLUGIN_VERSION_1,
543941Svenki 	PICLD_PLUGIN_CRITICAL,
553941Svenki 	"pri_plugin",
563941Svenki 	priplugin_init,
573941Svenki 	priplugin_fini
583941Svenki };
593941Svenki 
605029Sfw157321 static void
set_prop_info(ptree_propinfo_t * propinfo,int size,char * name,int type)613941Svenki set_prop_info(ptree_propinfo_t *propinfo, int size, char *name, int type)
623941Svenki {
633941Svenki 	propinfo->version = PICLD_PLUGIN_VERSION_1;
643941Svenki 	propinfo->read = NULL;
653941Svenki 	propinfo->write = NULL;
663941Svenki 	propinfo->piclinfo.type = type;
673941Svenki 	propinfo->piclinfo.accessmode = PICL_READ;
683941Svenki 	propinfo->piclinfo.size = size;
694003Svivek 	(void) strlcpy(propinfo->piclinfo.name, name,
703941Svenki 	    sizeof (propinfo->piclinfo.name));
713941Svenki }
723941Svenki 
733941Svenki boolean_t
prop_exists(picl_nodehdl_t node,char * name)743941Svenki prop_exists(picl_nodehdl_t node, char *name)
753941Svenki {
763941Svenki 	int status;
773941Svenki 	picl_prophdl_t proph;
783941Svenki 
793941Svenki 	status = ptree_get_prop_by_name(node, name, &proph);
803941Svenki 	if (status == PICL_SUCCESS)
813941Svenki 		return (B_TRUE);
823941Svenki 	else
833941Svenki 		return (B_FALSE);
843941Svenki }
853941Svenki 
863941Svenki void
add_md_prop(picl_nodehdl_t node,int size,char * name,void * value,int type)873941Svenki add_md_prop(picl_nodehdl_t node, int size, char *name, void* value, int type)
883941Svenki {
893941Svenki 	ptree_propinfo_t propinfo;
903941Svenki 	picl_prophdl_t proph;
913941Svenki 
923941Svenki 	if (!prop_exists(node, name)) {
933941Svenki 		set_prop_info(&propinfo, size, name, type);
943941Svenki 
953941Svenki 		(void) ptree_create_and_add_prop(node, &propinfo,
963941Svenki 		    value, &proph);
973941Svenki 	}
983941Svenki }
993941Svenki 
1005029Sfw157321 /*ARGSUSED*/
1015029Sfw157321 static int
remove_old_segments(picl_nodehdl_t node,void * args)1025029Sfw157321 remove_old_segments(picl_nodehdl_t node, void *args)
1035029Sfw157321 {
1045029Sfw157321 	int status;
1055029Sfw157321 
1065029Sfw157321 	if ((status = ptree_delete_node(node)) == PICL_SUCCESS)
1075029Sfw157321 		ptree_destroy_node(node);
1085029Sfw157321 	else
1095029Sfw157321 		pri_debug(LOG_NOTICE, "remove_old_segments: can't delete "
1105029Sfw157321 		    "segment node: %s\n", picl_strerror(status));
1115029Sfw157321 
1125029Sfw157321 	return (PICL_WALK_CONTINUE);
1135029Sfw157321 }
1145029Sfw157321 
1155029Sfw157321 static void
priplugin_init(void)1163941Svenki priplugin_init(void)
1173941Svenki {
1183941Svenki 	int status;
1193941Svenki 
1204669Sfw157321 	pri_debug(LOG_NOTICE, "priplugin: mem tree and io label thread "
1214669Sfw157321 	    "being created; callbacks being registered\n");
1224669Sfw157321 
1235029Sfw157321 	all_thr_exit = B_FALSE;
124*6662Sfw157321 	event_caught = B_FALSE;
1255029Sfw157321 
1265029Sfw157321 	(void) mutex_init(&rebuild_lock, USYNC_THREAD, NULL);
1275029Sfw157321 	(void) cond_init(&rebuild_cv, USYNC_THREAD, NULL);
1284669Sfw157321 
1295029Sfw157321 	if ((status = thr_create(NULL, NULL, pri_worker_thread, NULL, THR_BOUND,
1305029Sfw157321 	    &pri_worker_thread_id)) < 0) {
1315029Sfw157321 		pri_debug(LOG_NOTICE, "priplugin: can't create worker thread: "
1325029Sfw157321 		    "%d\n", status);
1335029Sfw157321 		all_thr_exit = B_TRUE;
1345029Sfw157321 		(void) mutex_destroy(&rebuild_lock);
1355029Sfw157321 		(void) cond_destroy(&rebuild_cv);
1365029Sfw157321 	} else if ((status = thr_create(NULL, NULL, pri_reader_thread, NULL,
1375029Sfw157321 	    THR_BOUND, &pri_reader_thread_id)) < 0) {
1385029Sfw157321 		pri_debug(LOG_NOTICE, "priplugin: can't create reader thread: "
1394669Sfw157321 		    "%d\n", status);
1405029Sfw157321 		(void) mutex_lock(&rebuild_lock);
1415029Sfw157321 		all_thr_exit = B_TRUE;
1425029Sfw157321 		(void) cond_signal(&rebuild_cv);
1435029Sfw157321 		(void) mutex_unlock(&rebuild_lock);
1445029Sfw157321 		(void) thr_join(pri_worker_thread_id, NULL, NULL);
1455029Sfw157321 		(void) mutex_destroy(&rebuild_lock);
1465029Sfw157321 		(void) cond_destroy(&rebuild_cv);
1475029Sfw157321 	} else {
1485029Sfw157321 		pri_debug(LOG_NOTICE, "priplugin_init: worker and reader "
1495029Sfw157321 		    "threads created - registering event handlers\n");
1505029Sfw157321 		/*
1515029Sfw157321 		 * register event_handler for both "sysevent-device-added",
1525029Sfw157321 		 * "sysevent_device_removed", and for
1535029Sfw157321 		 * "sysevent-dr-app-state-change" PICL events
1545029Sfw157321 		 */
1555029Sfw157321 		(void) ptree_register_handler(PICLEVENT_SYSEVENT_DEVICE_ADDED,
1565029Sfw157321 		    event_handler, NULL);
1575029Sfw157321 		(void) ptree_register_handler(PICLEVENT_SYSEVENT_DEVICE_REMOVED,
1585029Sfw157321 		    event_handler, NULL);
1595029Sfw157321 		(void) ptree_register_handler(PICLEVENT_DR_AP_STATE_CHANGE,
1605029Sfw157321 		    event_handler, NULL);
1613941Svenki 	}
1624669Sfw157321 }
1633941Svenki 
1645029Sfw157321 /*
1655029Sfw157321  * This thread handles the main processing of PRI data.  It is woken
1665029Sfw157321  * up by either the event handler, to process a PICL event, or it is
1675029Sfw157321  * woken up by the PRI reader thread which has just fetched a new
1685029Sfw157321  * copy of the PRI.
1695029Sfw157321  */
1704669Sfw157321 /*ARGSUSED*/
1714669Sfw157321 static void *
pri_worker_thread(void * arg)1725029Sfw157321 pri_worker_thread(void *arg)
1734669Sfw157321 {
1744669Sfw157321 	int status;
1755029Sfw157321 	picl_nodehdl_t picl_root_node;
1765029Sfw157321 
1775029Sfw157321 	pri_debug(LOG_NOTICE, "pri_worker_thread: start\n");
1785029Sfw157321 
1795029Sfw157321 	(void) mutex_lock(&rebuild_lock);
180*6662Sfw157321 	/*LINTED E_FUNC_RET_MAYBE_IGNORED2*/
1815029Sfw157321 	while (1) {
1825029Sfw157321 		(void) cond_wait(&rebuild_cv, &rebuild_lock);
1835029Sfw157321 
1845029Sfw157321 		if (all_thr_exit == B_TRUE) {
1855029Sfw157321 			(void) mutex_unlock(&rebuild_lock);
1865029Sfw157321 			pri_debug(LOG_NOTICE, "pri_worker_thread: time to "
1875029Sfw157321 			    "exit\n");
1885029Sfw157321 			break;
1895029Sfw157321 		}
1905029Sfw157321 
191*6662Sfw157321 		/*
192*6662Sfw157321 		 * We don't get events for changes to system memory,
193*6662Sfw157321 		 * and we do not want to interfere with other plug-ins
194*6662Sfw157321 		 * by making changes to the picl tree.  So if we were
195*6662Sfw157321 		 * woken up by a thread then do not destroy and rebuild
196*6662Sfw157321 		 * the memory info.  Just go fix the labels.
197*6662Sfw157321 		 */
198*6662Sfw157321 		if (event_caught == B_FALSE) {
199*6662Sfw157321 			status = ptree_get_root(&picl_root_node);
200*6662Sfw157321 			if (status != PICL_SUCCESS) {
201*6662Sfw157321 				pri_debug(LOG_NOTICE, "pri_worker_thread: "
202*6662Sfw157321 				    "can't get picl tree root node: %s\n",
203*6662Sfw157321 				    picl_strerror(status));
204*6662Sfw157321 				continue;
205*6662Sfw157321 			}
2065029Sfw157321 
207*6662Sfw157321 			pri_debug(LOG_NOTICE, "pri_worker_thread: have root "
208*6662Sfw157321 			    "picl and PRI nodes\n");
2095029Sfw157321 
210*6662Sfw157321 			status = ptree_walk_tree_by_class(picl_root_node,
211*6662Sfw157321 			    "memory-segment", NULL, remove_old_segments);
212*6662Sfw157321 			if (status != PICL_SUCCESS) {
213*6662Sfw157321 				pri_debug(LOG_NOTICE, "pri_worker_thread: "
214*6662Sfw157321 				    "can't remove old memory segments: \n",
215*6662Sfw157321 				    picl_strerror(status));
216*6662Sfw157321 			} else
217*6662Sfw157321 				pri_debug(LOG_NOTICE, "pri_worker_thread: "
218*6662Sfw157321 				    "old memory segments removed\n");
2195029Sfw157321 
220*6662Sfw157321 			status = ptree_walk_tree_by_class(picl_root_node,
221*6662Sfw157321 			    "memory", (void *) mdp, add_mem_prop);
222*6662Sfw157321 			if (status != PICL_SUCCESS) {
223*6662Sfw157321 				pri_debug(LOG_NOTICE, "pri_worker_thread: "
224*6662Sfw157321 				    "memory segments walk failed: \n",
225*6662Sfw157321 				    picl_strerror(status));
226*6662Sfw157321 			} else
227*6662Sfw157321 				pri_debug(LOG_NOTICE, "pri_worker_thread: "
228*6662Sfw157321 				    "success walking memory node\n");
2295029Sfw157321 		} else
230*6662Sfw157321 			event_caught = B_FALSE;
2315029Sfw157321 
2325029Sfw157321 		io_dev_addlabel(mdp);
2335029Sfw157321 	}
2345029Sfw157321 	pri_debug(LOG_NOTICE, "pri_worker_thread: exiting\n");
2355029Sfw157321 	return (NULL);
2365029Sfw157321 }
2375029Sfw157321 
2385029Sfw157321 /*
2395029Sfw157321  * This thread camps out in the PRI driver, waiting for it to return
2405029Sfw157321  * the contents of a new PRI.  When the PRI is changed this thread
2415029Sfw157321  * reads that data and prepares it for processing by the worker thread.
2425029Sfw157321  * It then signals the worker thread to process the new PRI data.
2435029Sfw157321  */
2445029Sfw157321 /*ARGSUSED*/
2455029Sfw157321 static void *
pri_reader_thread(void * arg)2465029Sfw157321 pri_reader_thread(void *arg)
2475029Sfw157321 {
2484669Sfw157321 	uint64_t tok;
2495029Sfw157321 	int status, count;
2505029Sfw157321 
2515029Sfw157321 	pri_debug(LOG_NOTICE, "pri_reader_thread: thread start\n");
2524669Sfw157321 
2534669Sfw157321 	if (pri_init() != 0) {
2545029Sfw157321 		pri_debug(LOG_NOTICE, "pri_reader_thread: pri_init failed\n");
2554669Sfw157321 		return (NULL);
2563941Svenki 	}
2573941Svenki 
2585029Sfw157321 	/*
2595029Sfw157321 	 * It's entirely possible that a new PRI may get pushed while
2605029Sfw157321 	 * the worker thread is processing the previous PRI.  We will
2615029Sfw157321 	 * wait until the worker is finished, then flush the old contents
2625029Sfw157321 	 * and wake up the worker again to process the new data.
2635029Sfw157321 	 */
2645029Sfw157321 	mdp = NULL;
2655029Sfw157321 	tok = 0;
2665029Sfw157321 	count = 0;
267*6662Sfw157321 	/*LINTED E_FUNC_RET_MAYBE_IGNORED2*/
2685029Sfw157321 	while (1) {
2695029Sfw157321 		/*
2705029Sfw157321 		 * The _fini() function will close the PRI's fd, which will
2715029Sfw157321 		 * cause this function to break out of waiting in the PRI
2725029Sfw157321 		 * driver and return an error.
2735029Sfw157321 		 */
2745029Sfw157321 		status = pri_devinit(&tok);
2755029Sfw157321 
2765029Sfw157321 		(void) mutex_lock(&rebuild_lock);
2775029Sfw157321 		if (all_thr_exit == B_TRUE) {
2785029Sfw157321 			(void) mutex_unlock(&rebuild_lock);
2795029Sfw157321 			pri_debug(LOG_NOTICE, "pri_reader_thread: time to "
2805029Sfw157321 			    "exit\n");
2815029Sfw157321 			break;
2825029Sfw157321 		}
2835029Sfw157321 
2845029Sfw157321 		/*
2855029Sfw157321 		 * Wait until the worker is idle before swapping in the
2865029Sfw157321 		 * new PRI contents, then signal the worker to process
2875029Sfw157321 		 * that new data.
2885029Sfw157321 		 */
2895029Sfw157321 		if (status == 0) {
2905029Sfw157321 			pri_debug(LOG_NOTICE, "pri_reader_thread: got PRI\n");
2915029Sfw157321 
2925029Sfw157321 			/* old buffer will be freed by pri_bufinit() */
2935029Sfw157321 			mdp = pri_bufinit(mdp);
2945029Sfw157321 			if (mdp != NULL) {
2955029Sfw157321 				(void) cond_signal(&rebuild_cv);
2965029Sfw157321 				count = 0;
2975029Sfw157321 			} else {
2985029Sfw157321 				pri_debug(LOG_NOTICE, "pri_reader_thread: "
2995029Sfw157321 				    "NULL mdp!\n");
3005029Sfw157321 				status = -1;
3015029Sfw157321 			}
3025029Sfw157321 		}
3035029Sfw157321 
3045029Sfw157321 		/*
3055029Sfw157321 		 * Try to handle SP resets or other unexplained errors
3065029Sfw157321 		 * from ds by closing down and re-opening the PRI driver.
3075029Sfw157321 		 */
3085029Sfw157321 		if (status == -1) {
3095029Sfw157321 			if (errno != 0) {
3105029Sfw157321 				pri_debug(LOG_NOTICE, "pri_reader_thread: "
3115029Sfw157321 				    "can't get PRI contents: %s\n",
3125029Sfw157321 				    strerror(errno));
3135029Sfw157321 			}
3145029Sfw157321 			if (++count > 6) {
3155029Sfw157321 				pri_debug(LOG_NOTICE, "pci_reader_thread: "
3165029Sfw157321 				    "can't process PRI data\n");
3175029Sfw157321 				(void) mutex_unlock(&rebuild_lock);
3185029Sfw157321 				break;
3195029Sfw157321 			}
3205029Sfw157321 			/* old buffer will be freed by pri_fini() */
3215029Sfw157321 			pri_fini();
3225029Sfw157321 			tok = 0;
3235029Sfw157321 			sleep(10);
3245029Sfw157321 			if (pri_init() != 0) {
3255029Sfw157321 				pri_debug(LOG_NOTICE, "pci_reader_thread: "
3265029Sfw157321 				    "can't reinitialize PRI driver\n");
3275029Sfw157321 				(void) mutex_unlock(&rebuild_lock);
3285029Sfw157321 				break;
3295029Sfw157321 			}
3305029Sfw157321 		}
3315029Sfw157321 		(void) mutex_unlock(&rebuild_lock);
3324669Sfw157321 	}
3334669Sfw157321 
3345029Sfw157321 	pri_debug(LOG_NOTICE, "pri_reader_thread: thread exiting\n");
3354669Sfw157321 	return (NULL);
3363941Svenki }
3373941Svenki 
3385029Sfw157321 static void
priplugin_fini(void)3393941Svenki priplugin_fini(void)
3403941Svenki {
3415029Sfw157321 	pri_debug(LOG_NOTICE, "priplugin_fini: called\n");
3425029Sfw157321 
3435029Sfw157321 	if (all_thr_exit == B_TRUE)
3445029Sfw157321 		return;
3455029Sfw157321 
3465029Sfw157321 	/* unregister the event handlers */
3474669Sfw157321 	(void) ptree_unregister_handler(PICLEVENT_SYSEVENT_DEVICE_ADDED,
3484669Sfw157321 	    event_handler, NULL);
3495029Sfw157321 	(void) ptree_unregister_handler(PICLEVENT_SYSEVENT_DEVICE_REMOVED,
3505029Sfw157321 	    event_handler, NULL);
3514669Sfw157321 	(void) ptree_unregister_handler(PICLEVENT_DR_AP_STATE_CHANGE,
3524669Sfw157321 	    event_handler, NULL);
3535029Sfw157321 
3545029Sfw157321 	/*
3555029Sfw157321 	 * Set the exit flag to tell the worker thread to quit and wake
3565029Sfw157321 	 * up that thread.  Once that thread is reaped then pull the rug
3575029Sfw157321 	 * out from the PRI reader thread by calling pri_fini(), which
3585029Sfw157321 	 * closes the PRI fd.  That wakes the PRI reader thread and it
3595029Sfw157321 	 * will then exit as well.
3605029Sfw157321 	 */
3615029Sfw157321 	(void) mutex_lock(&rebuild_lock);
3625029Sfw157321 	all_thr_exit = B_TRUE;
3635029Sfw157321 	(void) cond_signal(&rebuild_cv);
3645029Sfw157321 	(void) mutex_unlock(&rebuild_lock);
3655029Sfw157321 
3665029Sfw157321 	(void) thr_join(pri_worker_thread_id, NULL, NULL);
3675029Sfw157321 
3685029Sfw157321 	pri_devfini(mdp);
3695029Sfw157321 	mdp = NULL;
3705029Sfw157321 	pri_fini();
3715029Sfw157321 	(void) thr_join(pri_reader_thread_id, NULL, NULL);
3725029Sfw157321 
3735029Sfw157321 	(void) mutex_destroy(&rebuild_lock);
3745029Sfw157321 	(void) cond_destroy(&rebuild_cv);
3753941Svenki }
3763941Svenki 
3773941Svenki void
priplugin_register(void)3783941Svenki priplugin_register(void)
3793941Svenki {
3803941Svenki 	picld_plugin_register(&priplugin_reg);
3813941Svenki }
3823941Svenki 
3834669Sfw157321 /*
3844669Sfw157321  * Discovery event handler
3854669Sfw157321  * respond to the picl events:
3864669Sfw157321  *      PICLEVENT_SYSEVENT_DEVICE_ADDED
3875029Sfw157321  *      PICLEVENT_SYSEVENT_DEVICE_REMOVED
3884669Sfw157321  *      PICLEVENT_DR_AP_STATE_CHANGE
3894669Sfw157321  *
3904669Sfw157321  * We can't do much of anything fancy since the event data doesn't contain
3914669Sfw157321  * a nac for the device.  Nothing to do for remove - the devtree plug-in
3924669Sfw157321  * will have removed the node for us.  For add we have to go back and
3934669Sfw157321  * add labels again.
3944669Sfw157321  */
3954669Sfw157321 static void
event_handler(const char * ename,const void * earg,size_t size,void * cookie)3964669Sfw157321 event_handler(const char *ename, const void *earg, size_t size, void *cookie)
3974669Sfw157321 {
398*6662Sfw157321 
3995029Sfw157321 	pri_debug(LOG_NOTICE, "pri: event_handler: caught event "
4005029Sfw157321 	    "%s\n", ename);
4014669Sfw157321 	if ((strcmp(ename, PICLEVENT_SYSEVENT_DEVICE_ADDED) == 0) ||
4025029Sfw157321 	    (strcmp(ename, PICLEVENT_SYSEVENT_DEVICE_REMOVED) == 0) ||
4034669Sfw157321 	    (strcmp(ename, PICLEVENT_DR_AP_STATE_CHANGE) == 0)) {
4045029Sfw157321 		pri_debug(LOG_NOTICE, "pri: event_handler: handle event "
4055029Sfw157321 		    "%s; waking worker thread\n", ename);
406*6662Sfw157321 
4075029Sfw157321 		(void) mutex_lock(&rebuild_lock);
4085029Sfw157321 
409*6662Sfw157321 		if (all_thr_exit == B_FALSE) {
410*6662Sfw157321 			/*
411*6662Sfw157321 			 * Tell the worker thread to only re-examine the
412*6662Sfw157321 			 * IO device labels.
413*6662Sfw157321 			 */
414*6662Sfw157321 			event_caught = B_TRUE;
4155029Sfw157321 			(void) cond_signal(&rebuild_cv);
416*6662Sfw157321 		}
4175029Sfw157321 
4185029Sfw157321 		(void) mutex_unlock(&rebuild_lock);
4194669Sfw157321 	}
4204669Sfw157321 }
4214669Sfw157321 
4223941Svenki /*VARARGS2*/
4233941Svenki void
pri_debug(int level,char * fmt,...)4243941Svenki pri_debug(int level, char *fmt, ...)
4253941Svenki {
4263941Svenki #if (PRI_DEBUG != 0)
4273941Svenki 	va_list	ap;
4283941Svenki 
4293941Svenki 	va_start(ap, fmt);
4303941Svenki 	vsyslog(level, fmt, ap);
4313941Svenki 	va_end(ap);
4323941Svenki #endif
4333941Svenki }
434