1*f6e214c7SGavin Maltby /*
2*f6e214c7SGavin Maltby  * CDDL HEADER START
3*f6e214c7SGavin Maltby  *
4*f6e214c7SGavin Maltby  * The contents of this file are subject to the terms of the
5*f6e214c7SGavin Maltby  * Common Development and Distribution License (the "License").
6*f6e214c7SGavin Maltby  * You may not use this file except in compliance with the License.
7*f6e214c7SGavin Maltby  *
8*f6e214c7SGavin Maltby  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*f6e214c7SGavin Maltby  * or http://www.opensolaris.org/os/licensing.
10*f6e214c7SGavin Maltby  * See the License for the specific language governing permissions
11*f6e214c7SGavin Maltby  * and limitations under the License.
12*f6e214c7SGavin Maltby  *
13*f6e214c7SGavin Maltby  * When distributing Covered Code, include this CDDL HEADER in each
14*f6e214c7SGavin Maltby  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*f6e214c7SGavin Maltby  * If applicable, add the following below this CDDL HEADER, with the
16*f6e214c7SGavin Maltby  * fields enclosed by brackets "[]" replaced with your own identifying
17*f6e214c7SGavin Maltby  * information: Portions Copyright [yyyy] [name of copyright owner]
18*f6e214c7SGavin Maltby  *
19*f6e214c7SGavin Maltby  * CDDL HEADER END
20*f6e214c7SGavin Maltby  */
21*f6e214c7SGavin Maltby 
22*f6e214c7SGavin Maltby /*
23*f6e214c7SGavin Maltby  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
24*f6e214c7SGavin Maltby  */
25*f6e214c7SGavin Maltby 
26*f6e214c7SGavin Maltby /*
27*f6e214c7SGavin Maltby  * Code shared by software-diagnosis and software-response modules.
28*f6e214c7SGavin Maltby  * The fmd module linkage info for the two modules lives in swde_main.c
29*f6e214c7SGavin Maltby  * (for software-diagnosis) and swrp_main.c (for software-response).
30*f6e214c7SGavin Maltby  */
31*f6e214c7SGavin Maltby 
32*f6e214c7SGavin Maltby #include "../common/sw_impl.h"
33*f6e214c7SGavin Maltby 
34*f6e214c7SGavin Maltby /*
35*f6e214c7SGavin Maltby  * Each subsidiary that is hosted is assigned a unique subsidiary id.  These
36*f6e214c7SGavin Maltby  * macros convert between the id of a subsidiary and the index used in keeping
37*f6e214c7SGavin Maltby  * track of subsidiaries.  Outside of this file these ids should remain
38*f6e214c7SGavin Maltby  * opaque.
39*f6e214c7SGavin Maltby  */
40*f6e214c7SGavin Maltby #define	ID2IDX(id)	((int)((id) & 0xff0000) >> 16)
41*f6e214c7SGavin Maltby #define	IDX2ID(i)	((id_t)((i) << 16) | 0x1d000000)
42*f6e214c7SGavin Maltby 
43*f6e214c7SGavin Maltby #define	SUBIDVALID(msinfo, id)  (((int)(id) & 0xff00ffff) == 0x1d000000 && \
44*f6e214c7SGavin Maltby     ID2IDX(id) < (msinfo)->swms_dispcnt)
45*f6e214c7SGavin Maltby 
46*f6e214c7SGavin Maltby static struct {
47*f6e214c7SGavin Maltby 	fmd_stat_t sw_recv_total;
48*f6e214c7SGavin Maltby 	fmd_stat_t sw_recv_match;
49*f6e214c7SGavin Maltby 	fmd_stat_t sw_recv_callback;
50*f6e214c7SGavin Maltby } sw_stats = {
51*f6e214c7SGavin Maltby 	{ "sw_recv_total", FMD_TYPE_UINT64,
52*f6e214c7SGavin Maltby 	    "total events received" },
53*f6e214c7SGavin Maltby 	{ "sw_recv_match", FMD_TYPE_UINT64,
54*f6e214c7SGavin Maltby 	    "events matching some subsidiary" },
55*f6e214c7SGavin Maltby 	{ "sw_recv_callback", FMD_TYPE_UINT64,
56*f6e214c7SGavin Maltby 	    "callbacks to all subsidiaries" },
57*f6e214c7SGavin Maltby };
58*f6e214c7SGavin Maltby 
59*f6e214c7SGavin Maltby #define	BUMPSTAT(stat)		sw_stats.stat.fmds_value.ui64++
60*f6e214c7SGavin Maltby #define	BUMPSTATN(stat, n)	sw_stats.stat.fmds_value.ui64 += (n)
61*f6e214c7SGavin Maltby 
62*f6e214c7SGavin Maltby /*
63*f6e214c7SGavin Maltby  * ========================== Event Receipt =================================
64*f6e214c7SGavin Maltby  *
65*f6e214c7SGavin Maltby  * The fmdo_recv entry point.  See which sub de/response agents have a
66*f6e214c7SGavin Maltby  * matching subscription and callback for the first match from each.
67*f6e214c7SGavin Maltby  * The sub de/response agents should dispatch *all* their subscriptions
68*f6e214c7SGavin Maltby  * via their registered dispatch table, including things like list.repaired.
69*f6e214c7SGavin Maltby  */
70*f6e214c7SGavin Maltby void
sw_recv(fmd_hdl_t * hdl,fmd_event_t * ep,nvlist_t * nvl,const char * class)71*f6e214c7SGavin Maltby sw_recv(fmd_hdl_t *hdl, fmd_event_t *ep, nvlist_t *nvl, const char *class)
72*f6e214c7SGavin Maltby {
73*f6e214c7SGavin Maltby 	struct sw_modspecific *msinfo;
74*f6e214c7SGavin Maltby 	int calls = 0;
75*f6e214c7SGavin Maltby 	int mod;
76*f6e214c7SGavin Maltby 
77*f6e214c7SGavin Maltby 	BUMPSTAT(sw_recv_total);
78*f6e214c7SGavin Maltby 
79*f6e214c7SGavin Maltby 	msinfo = (struct sw_modspecific *)fmd_hdl_getspecific(hdl);
80*f6e214c7SGavin Maltby 
81*f6e214c7SGavin Maltby 	/*
82*f6e214c7SGavin Maltby 	 * For each sub module that has a matching class pattern call the
83*f6e214c7SGavin Maltby 	 * registered callback for that sub DE.  Only one match per sub module
84*f6e214c7SGavin Maltby 	 * is allowed (the first match in its table, others are not checked).
85*f6e214c7SGavin Maltby 	 */
86*f6e214c7SGavin Maltby 	for (mod = 0; mod < msinfo->swms_dispcnt; mod++) {
87*f6e214c7SGavin Maltby 		const struct sw_disp *dp;
88*f6e214c7SGavin Maltby 		sw_dispfunc_t *dispf = NULL;
89*f6e214c7SGavin Maltby 
90*f6e214c7SGavin Maltby 		for (dp = (*msinfo->swms_disptbl)[mod];
91*f6e214c7SGavin Maltby 		    dp != NULL && dp->swd_classpat != NULL; dp++) {
92*f6e214c7SGavin Maltby 			if (fmd_nvl_class_match(hdl, nvl, dp->swd_classpat)) {
93*f6e214c7SGavin Maltby 				dispf = dp->swd_func;
94*f6e214c7SGavin Maltby 				break;
95*f6e214c7SGavin Maltby 			}
96*f6e214c7SGavin Maltby 		}
97*f6e214c7SGavin Maltby 		if (dispf != NULL) {
98*f6e214c7SGavin Maltby 			calls++;
99*f6e214c7SGavin Maltby 			(*dispf)(hdl, ep, nvl, class, dp->swd_arg);
100*f6e214c7SGavin Maltby 		}
101*f6e214c7SGavin Maltby 	}
102*f6e214c7SGavin Maltby 
103*f6e214c7SGavin Maltby 	BUMPSTAT(sw_recv_match);
104*f6e214c7SGavin Maltby 	if (calls)
105*f6e214c7SGavin Maltby 		BUMPSTATN(sw_recv_callback, calls);
106*f6e214c7SGavin Maltby }
107*f6e214c7SGavin Maltby 
108*f6e214c7SGavin Maltby /*
109*f6e214c7SGavin Maltby  * ========================== Timers ========================================
110*f6e214c7SGavin Maltby  *
111*f6e214c7SGavin Maltby  * A subsidiary can install a timer; it must pass an additional argument
112*f6e214c7SGavin Maltby  * identifying itself so that we can hand off to the appropriate
113*f6e214c7SGavin Maltby  * swsub_timeout function in the fmdo_timeout entry point when the timer fires.
114*f6e214c7SGavin Maltby  */
115*f6e214c7SGavin Maltby id_t
sw_timer_install(fmd_hdl_t * hdl,id_t who,void * arg,fmd_event_t * ep,hrtime_t hrt)116*f6e214c7SGavin Maltby sw_timer_install(fmd_hdl_t *hdl, id_t who, void *arg, fmd_event_t *ep,
117*f6e214c7SGavin Maltby     hrtime_t hrt)
118*f6e214c7SGavin Maltby {
119*f6e214c7SGavin Maltby 	struct sw_modspecific *msinfo;
120*f6e214c7SGavin Maltby 	const struct sw_subinfo **subinfo;
121*f6e214c7SGavin Maltby 	const struct sw_subinfo *sip;
122*f6e214c7SGavin Maltby 	int slot, chosen = -1;
123*f6e214c7SGavin Maltby 	id_t timerid;
124*f6e214c7SGavin Maltby 
125*f6e214c7SGavin Maltby 	msinfo = (struct sw_modspecific *)fmd_hdl_getspecific(hdl);
126*f6e214c7SGavin Maltby 	if (!SUBIDVALID(msinfo, who))
127*f6e214c7SGavin Maltby 		fmd_hdl_abort(hdl, "sw_timer_install: invalid subid %d\n", who);
128*f6e214c7SGavin Maltby 
129*f6e214c7SGavin Maltby 	subinfo = *msinfo->swms_subinfo;
130*f6e214c7SGavin Maltby 	sip = subinfo[ID2IDX(who)];
131*f6e214c7SGavin Maltby 
132*f6e214c7SGavin Maltby 	if (sip-> swsub_timeout == NULL)
133*f6e214c7SGavin Maltby 		fmd_hdl_abort(hdl, "sw_timer_install: no swsub_timeout\n");
134*f6e214c7SGavin Maltby 
135*f6e214c7SGavin Maltby 	/*
136*f6e214c7SGavin Maltby 	 * Look for a slot.  Module entry points are single-threaded
137*f6e214c7SGavin Maltby 	 * in nature, but if someone installs a timer from a door
138*f6e214c7SGavin Maltby 	 * service function we're contended.
139*f6e214c7SGavin Maltby 	 */
140*f6e214c7SGavin Maltby 	(void) pthread_mutex_lock(&msinfo->swms_timerlock);
141*f6e214c7SGavin Maltby 	for (slot = 0; slot < SW_TIMER_MAX; slot++) {
142*f6e214c7SGavin Maltby 		if (msinfo->swms_timers[slot].swt_state != SW_TMR_INUSE) {
143*f6e214c7SGavin Maltby 			chosen = slot;
144*f6e214c7SGavin Maltby 			break;
145*f6e214c7SGavin Maltby 		}
146*f6e214c7SGavin Maltby 	}
147*f6e214c7SGavin Maltby 
148*f6e214c7SGavin Maltby 	if (chosen == -1)
149*f6e214c7SGavin Maltby 		fmd_hdl_abort(hdl, "timer slots exhausted\n");
150*f6e214c7SGavin Maltby 
151*f6e214c7SGavin Maltby 	msinfo->swms_timers[chosen].swt_state = SW_TMR_INUSE;
152*f6e214c7SGavin Maltby 	msinfo->swms_timers[chosen].swt_ownerid = who;
153*f6e214c7SGavin Maltby 	msinfo->swms_timers[chosen].swt_timerid = timerid =
154*f6e214c7SGavin Maltby 	    fmd_timer_install(hdl, arg, ep, hrt);
155*f6e214c7SGavin Maltby 
156*f6e214c7SGavin Maltby 	(void) pthread_mutex_unlock(&msinfo->swms_timerlock);
157*f6e214c7SGavin Maltby 
158*f6e214c7SGavin Maltby 	return (timerid);
159*f6e214c7SGavin Maltby }
160*f6e214c7SGavin Maltby 
161*f6e214c7SGavin Maltby /*
162*f6e214c7SGavin Maltby  * Look for a timer installed by a given subsidiary matching timerid.
163*f6e214c7SGavin Maltby  */
164*f6e214c7SGavin Maltby static int
subtimer_find(struct sw_modspecific * msinfo,id_t who,id_t timerid)165*f6e214c7SGavin Maltby subtimer_find(struct sw_modspecific *msinfo, id_t who, id_t timerid)
166*f6e214c7SGavin Maltby {
167*f6e214c7SGavin Maltby 	int slot;
168*f6e214c7SGavin Maltby 
169*f6e214c7SGavin Maltby 	for (slot = 0; slot < SW_TIMER_MAX; slot++) {
170*f6e214c7SGavin Maltby 		if (msinfo->swms_timers[slot].swt_state == SW_TMR_INUSE &&
171*f6e214c7SGavin Maltby 		    (who == -1 ||
172*f6e214c7SGavin Maltby 		    msinfo->swms_timers[slot].swt_ownerid == who) &&
173*f6e214c7SGavin Maltby 		    msinfo->swms_timers[slot].swt_timerid == timerid)
174*f6e214c7SGavin Maltby 			return (slot);
175*f6e214c7SGavin Maltby 	}
176*f6e214c7SGavin Maltby 
177*f6e214c7SGavin Maltby 	return (-1);
178*f6e214c7SGavin Maltby }
179*f6e214c7SGavin Maltby 
180*f6e214c7SGavin Maltby void
sw_timer_remove(fmd_hdl_t * hdl,id_t who,id_t timerid)181*f6e214c7SGavin Maltby sw_timer_remove(fmd_hdl_t *hdl, id_t who, id_t timerid)
182*f6e214c7SGavin Maltby {
183*f6e214c7SGavin Maltby 	struct sw_modspecific *msinfo;
184*f6e214c7SGavin Maltby 	const struct sw_subinfo **subinfo;
185*f6e214c7SGavin Maltby 	const struct sw_subinfo *sip;
186*f6e214c7SGavin Maltby 	int slot;
187*f6e214c7SGavin Maltby 
188*f6e214c7SGavin Maltby 	msinfo = (struct sw_modspecific *)fmd_hdl_getspecific(hdl);
189*f6e214c7SGavin Maltby 	if (!SUBIDVALID(msinfo, who))
190*f6e214c7SGavin Maltby 		fmd_hdl_abort(hdl, "sw_timer_remove: invalid subid\n");
191*f6e214c7SGavin Maltby 
192*f6e214c7SGavin Maltby 	subinfo = *msinfo->swms_subinfo;
193*f6e214c7SGavin Maltby 	sip = subinfo[ID2IDX(who)];
194*f6e214c7SGavin Maltby 
195*f6e214c7SGavin Maltby 	(void) pthread_mutex_lock(&msinfo->swms_timerlock);
196*f6e214c7SGavin Maltby 	if ((slot = subtimer_find(msinfo, who, timerid)) == -1)
197*f6e214c7SGavin Maltby 		fmd_hdl_abort(hdl, "sw_timer_remove: timerid %d not found "
198*f6e214c7SGavin Maltby 		    "for %s\n", timerid, sip->swsub_name);
199*f6e214c7SGavin Maltby 	fmd_timer_remove(hdl, timerid);
200*f6e214c7SGavin Maltby 	msinfo->swms_timers[slot].swt_state = SW_TMR_RMVD;
201*f6e214c7SGavin Maltby 	(void) pthread_mutex_unlock(&msinfo->swms_timerlock);
202*f6e214c7SGavin Maltby }
203*f6e214c7SGavin Maltby 
204*f6e214c7SGavin Maltby /*
205*f6e214c7SGavin Maltby  * The fmdo_timeout entry point.
206*f6e214c7SGavin Maltby  */
207*f6e214c7SGavin Maltby void
sw_timeout(fmd_hdl_t * hdl,id_t timerid,void * arg)208*f6e214c7SGavin Maltby sw_timeout(fmd_hdl_t *hdl, id_t timerid, void *arg)
209*f6e214c7SGavin Maltby {
210*f6e214c7SGavin Maltby 	struct sw_modspecific *msinfo;
211*f6e214c7SGavin Maltby 	const struct sw_subinfo **subinfo;
212*f6e214c7SGavin Maltby 	const struct sw_subinfo *sip;
213*f6e214c7SGavin Maltby 	id_t owner;
214*f6e214c7SGavin Maltby 	int slot;
215*f6e214c7SGavin Maltby 
216*f6e214c7SGavin Maltby 	msinfo = (struct sw_modspecific *)fmd_hdl_getspecific(hdl);
217*f6e214c7SGavin Maltby 
218*f6e214c7SGavin Maltby 	(void) pthread_mutex_lock(&msinfo->swms_timerlock);
219*f6e214c7SGavin Maltby 	if ((slot = subtimer_find(msinfo, -1, timerid)) == -1)
220*f6e214c7SGavin Maltby 		fmd_hdl_abort(hdl, "sw_timeout: timerid %d not found\n");
221*f6e214c7SGavin Maltby 	(void) pthread_mutex_unlock(&msinfo->swms_timerlock);
222*f6e214c7SGavin Maltby 
223*f6e214c7SGavin Maltby 	owner = msinfo->swms_timers[slot].swt_ownerid;
224*f6e214c7SGavin Maltby 	if (!SUBIDVALID(msinfo, owner))
225*f6e214c7SGavin Maltby 		fmd_hdl_abort(hdl, "sw_timeout: invalid subid\n");
226*f6e214c7SGavin Maltby 
227*f6e214c7SGavin Maltby 	subinfo = *msinfo->swms_subinfo;
228*f6e214c7SGavin Maltby 	sip = subinfo[ID2IDX(owner)];
229*f6e214c7SGavin Maltby 
230*f6e214c7SGavin Maltby 	sip->swsub_timeout(hdl, timerid, arg);
231*f6e214c7SGavin Maltby }
232*f6e214c7SGavin Maltby 
233*f6e214c7SGavin Maltby /*
234*f6e214c7SGavin Maltby  * ========================== sw_subinfo access =============================
235*f6e214c7SGavin Maltby  */
236*f6e214c7SGavin Maltby 
237*f6e214c7SGavin Maltby enum sw_casetype
sw_id_to_casetype(fmd_hdl_t * hdl,id_t who)238*f6e214c7SGavin Maltby sw_id_to_casetype(fmd_hdl_t *hdl, id_t who)
239*f6e214c7SGavin Maltby {
240*f6e214c7SGavin Maltby 	struct sw_modspecific *msinfo;
241*f6e214c7SGavin Maltby 	const struct sw_subinfo **subinfo;
242*f6e214c7SGavin Maltby 	const struct sw_subinfo *sip;
243*f6e214c7SGavin Maltby 
244*f6e214c7SGavin Maltby 	msinfo = (struct sw_modspecific *)fmd_hdl_getspecific(hdl);
245*f6e214c7SGavin Maltby 	if (!SUBIDVALID(msinfo, who))
246*f6e214c7SGavin Maltby 		fmd_hdl_abort(hdl, "sw_id_to_casetype: invalid subid %d\n",
247*f6e214c7SGavin Maltby 		    who);
248*f6e214c7SGavin Maltby 
249*f6e214c7SGavin Maltby 	subinfo = *msinfo->swms_subinfo;
250*f6e214c7SGavin Maltby 	sip = subinfo[ID2IDX(who)];
251*f6e214c7SGavin Maltby 
252*f6e214c7SGavin Maltby 	if ((sip->swsub_casetype & SW_CASE_NONE) != SW_CASE_NONE)
253*f6e214c7SGavin Maltby 		fmd_hdl_abort(hdl, "sw_id_to_casetype: bad case type %d "
254*f6e214c7SGavin Maltby 		    "for %s\n", sip->swsub_casetype, sip->swsub_name);
255*f6e214c7SGavin Maltby 
256*f6e214c7SGavin Maltby 	return (sip->swsub_casetype);
257*f6e214c7SGavin Maltby }
258*f6e214c7SGavin Maltby 
259*f6e214c7SGavin Maltby /*
260*f6e214c7SGavin Maltby  * Given a case type lookup the struct sw_subinfo for the subsidiary
261*f6e214c7SGavin Maltby  * that opens cases of that type.
262*f6e214c7SGavin Maltby  */
263*f6e214c7SGavin Maltby static const struct sw_subinfo *
sw_subinfo_bycase(fmd_hdl_t * hdl,enum sw_casetype type)264*f6e214c7SGavin Maltby sw_subinfo_bycase(fmd_hdl_t *hdl, enum sw_casetype type)
265*f6e214c7SGavin Maltby {
266*f6e214c7SGavin Maltby 	struct sw_modspecific *msinfo;
267*f6e214c7SGavin Maltby 	const struct sw_subinfo **subinfo;
268*f6e214c7SGavin Maltby 	const struct sw_subinfo *sip;
269*f6e214c7SGavin Maltby 	int i;
270*f6e214c7SGavin Maltby 
271*f6e214c7SGavin Maltby 	msinfo = (struct sw_modspecific *)fmd_hdl_getspecific(hdl);
272*f6e214c7SGavin Maltby 
273*f6e214c7SGavin Maltby 	subinfo = *msinfo->swms_subinfo;
274*f6e214c7SGavin Maltby 	for (i = 0; i < SW_SUB_MAX; i++) {
275*f6e214c7SGavin Maltby 		sip = subinfo[i];
276*f6e214c7SGavin Maltby 		if (sip->swsub_casetype == type)
277*f6e214c7SGavin Maltby 			return (sip);
278*f6e214c7SGavin Maltby 	}
279*f6e214c7SGavin Maltby 
280*f6e214c7SGavin Maltby 	return (NULL);
281*f6e214c7SGavin Maltby }
282*f6e214c7SGavin Maltby 
283*f6e214c7SGavin Maltby /*
284*f6e214c7SGavin Maltby  * Find the case close function for the given case type; can be NULL.
285*f6e214c7SGavin Maltby  */
286*f6e214c7SGavin Maltby swsub_case_close_func_t *
sw_sub_case_close_func(fmd_hdl_t * hdl,enum sw_casetype type)287*f6e214c7SGavin Maltby sw_sub_case_close_func(fmd_hdl_t *hdl, enum sw_casetype type)
288*f6e214c7SGavin Maltby {
289*f6e214c7SGavin Maltby 	const struct sw_subinfo *sip;
290*f6e214c7SGavin Maltby 
291*f6e214c7SGavin Maltby 	if ((sip = sw_subinfo_bycase(hdl, type)) == NULL)
292*f6e214c7SGavin Maltby 		fmd_hdl_abort(hdl, "sw_sub_case_close_func: case type "
293*f6e214c7SGavin Maltby 		    "%d not found\n", type);
294*f6e214c7SGavin Maltby 
295*f6e214c7SGavin Maltby 	return (sip->swsub_case_close);
296*f6e214c7SGavin Maltby }
297*f6e214c7SGavin Maltby 
298*f6e214c7SGavin Maltby /*
299*f6e214c7SGavin Maltby  * Find the case verify function for the given case type; can be NULL.
300*f6e214c7SGavin Maltby  */
301*f6e214c7SGavin Maltby sw_case_vrfy_func_t *
sw_sub_case_vrfy_func(fmd_hdl_t * hdl,enum sw_casetype type)302*f6e214c7SGavin Maltby sw_sub_case_vrfy_func(fmd_hdl_t *hdl, enum sw_casetype type)
303*f6e214c7SGavin Maltby {
304*f6e214c7SGavin Maltby 	const struct sw_subinfo *sip;
305*f6e214c7SGavin Maltby 
306*f6e214c7SGavin Maltby 	if ((sip = sw_subinfo_bycase(hdl, type)) == NULL)
307*f6e214c7SGavin Maltby 		fmd_hdl_abort(hdl, "sw_sub_case_vrfy_func: case type "
308*f6e214c7SGavin Maltby 		    "%d not found\n", type);
309*f6e214c7SGavin Maltby 
310*f6e214c7SGavin Maltby 	return (sip->swsub_case_verify);
311*f6e214c7SGavin Maltby }
312*f6e214c7SGavin Maltby 
313*f6e214c7SGavin Maltby /*
314*f6e214c7SGavin Maltby  * ========================== Initialization ================================
315*f6e214c7SGavin Maltby  *
316*f6e214c7SGavin Maltby  * The two modules - software-diagnosis and software-response - call
317*f6e214c7SGavin Maltby  * sw_fmd_init from their _fmd_init entry points.
318*f6e214c7SGavin Maltby  */
319*f6e214c7SGavin Maltby 
320*f6e214c7SGavin Maltby static void
sw_add_callbacks(fmd_hdl_t * hdl,const char * who,const struct sw_disp * dp,int nelem,struct sw_modspecific * msinfo)321*f6e214c7SGavin Maltby sw_add_callbacks(fmd_hdl_t *hdl, const char *who,
322*f6e214c7SGavin Maltby     const struct sw_disp *dp, int nelem, struct sw_modspecific *msinfo)
323*f6e214c7SGavin Maltby {
324*f6e214c7SGavin Maltby 	int i;
325*f6e214c7SGavin Maltby 
326*f6e214c7SGavin Maltby 	(*msinfo->swms_disptbl)[msinfo->swms_dispcnt++] = dp;
327*f6e214c7SGavin Maltby 
328*f6e214c7SGavin Maltby 	if (dp == NULL)
329*f6e214c7SGavin Maltby 		return;		/* subsidiary failed init */
330*f6e214c7SGavin Maltby 
331*f6e214c7SGavin Maltby 	/* check that the nelem'th entry is the NULL termination */
332*f6e214c7SGavin Maltby 	if (dp[nelem - 1].swd_classpat != NULL ||
333*f6e214c7SGavin Maltby 	    dp[nelem - 1].swd_func != NULL || dp[nelem - 1].swd_arg != NULL)
334*f6e214c7SGavin Maltby 		fmd_hdl_abort(hdl, "subsidiary %s dispatch table not NULL-"
335*f6e214c7SGavin Maltby 		    "terminated\n", who);
336*f6e214c7SGavin Maltby 
337*f6e214c7SGavin Maltby 	/* now validate the entries; we allow NULL handlers */
338*f6e214c7SGavin Maltby 	for (i = 0; i < nelem - 1; i++) {
339*f6e214c7SGavin Maltby 		if (dp[i].swd_classpat == NULL)
340*f6e214c7SGavin Maltby 			fmd_hdl_abort(hdl, "subsidiary %s dispatch table entry "
341*f6e214c7SGavin Maltby 			    "%d has a NULL pattern or function\n", who, i);
342*f6e214c7SGavin Maltby 	}
343*f6e214c7SGavin Maltby 
344*f6e214c7SGavin Maltby }
345*f6e214c7SGavin Maltby 
346*f6e214c7SGavin Maltby int
sw_fmd_init(fmd_hdl_t * hdl,const fmd_hdl_info_t * hdlinfo,const struct sw_subinfo * (* subsid)[SW_SUB_MAX])347*f6e214c7SGavin Maltby sw_fmd_init(fmd_hdl_t *hdl, const fmd_hdl_info_t *hdlinfo,
348*f6e214c7SGavin Maltby     const struct sw_subinfo *(*subsid)[SW_SUB_MAX])
349*f6e214c7SGavin Maltby {
350*f6e214c7SGavin Maltby 	struct sw_modspecific *msinfo;
351*f6e214c7SGavin Maltby 	int i;
352*f6e214c7SGavin Maltby 
353*f6e214c7SGavin Maltby 	if (fmd_hdl_register(hdl, FMD_API_VERSION, hdlinfo) != 0)
354*f6e214c7SGavin Maltby 		return (0);
355*f6e214c7SGavin Maltby 
356*f6e214c7SGavin Maltby 	if (fmd_prop_get_int32(hdl, "enable") != B_TRUE) {
357*f6e214c7SGavin Maltby 		fmd_hdl_debug(hdl, "%s disabled though .conf file setting\n",
358*f6e214c7SGavin Maltby 		    hdlinfo->fmdi_desc);
359*f6e214c7SGavin Maltby 		fmd_hdl_unregister(hdl);
360*f6e214c7SGavin Maltby 		return (0);
361*f6e214c7SGavin Maltby 	}
362*f6e214c7SGavin Maltby 
363*f6e214c7SGavin Maltby 	msinfo = fmd_hdl_zalloc(hdl, sizeof (*msinfo), FMD_SLEEP);
364*f6e214c7SGavin Maltby 
365*f6e214c7SGavin Maltby 	msinfo->swms_subinfo = subsid;
366*f6e214c7SGavin Maltby 	msinfo->swms_disptbl = fmd_hdl_zalloc(hdl,
367*f6e214c7SGavin Maltby 	    SW_SUB_MAX * sizeof (struct sw_disp *), FMD_SLEEP);
368*f6e214c7SGavin Maltby 
369*f6e214c7SGavin Maltby 	(void) pthread_mutex_init(&msinfo->swms_timerlock, NULL);
370*f6e214c7SGavin Maltby 
371*f6e214c7SGavin Maltby 	for (i = 0; i < SW_TIMER_MAX; i++)
372*f6e214c7SGavin Maltby 		msinfo->swms_timers[i].swt_state = SW_TMR_UNTOUCHED;
373*f6e214c7SGavin Maltby 
374*f6e214c7SGavin Maltby 	fmd_hdl_setspecific(hdl, (void *)msinfo);
375*f6e214c7SGavin Maltby 
376*f6e214c7SGavin Maltby 	(void) fmd_stat_create(hdl, FMD_STAT_NOALLOC, sizeof (sw_stats) /
377*f6e214c7SGavin Maltby 	    sizeof (fmd_stat_t), (fmd_stat_t *)&sw_stats);
378*f6e214c7SGavin Maltby 
379*f6e214c7SGavin Maltby 	/*
380*f6e214c7SGavin Maltby 	 * Initialize subsidiaries.  Each must make any subscription
381*f6e214c7SGavin Maltby 	 * requests it needs and return a pointer to a NULL-terminated
382*f6e214c7SGavin Maltby 	 * callback dispatch table and an indication of the number of
383*f6e214c7SGavin Maltby 	 * entries in that table including the NULL termination entry.
384*f6e214c7SGavin Maltby 	 */
385*f6e214c7SGavin Maltby 	for (i = 0; i < SW_SUB_MAX; i++) {
386*f6e214c7SGavin Maltby 		const struct sw_subinfo *sip = (*subsid)[i];
387*f6e214c7SGavin Maltby 		const struct sw_disp *dp;
388*f6e214c7SGavin Maltby 		char dbgbuf[80];
389*f6e214c7SGavin Maltby 		int nelem = -1;
390*f6e214c7SGavin Maltby 		int initrslt;
391*f6e214c7SGavin Maltby 
392*f6e214c7SGavin Maltby 		if (!sip || sip->swsub_name == NULL)
393*f6e214c7SGavin Maltby 			break;
394*f6e214c7SGavin Maltby 
395*f6e214c7SGavin Maltby 		initrslt = (*sip->swsub_init)(hdl, IDX2ID(i), &dp, &nelem);
396*f6e214c7SGavin Maltby 
397*f6e214c7SGavin Maltby 		(void) snprintf(dbgbuf, sizeof (dbgbuf),
398*f6e214c7SGavin Maltby 		    "subsidiary %d (id 0x%lx) '%s'",
399*f6e214c7SGavin Maltby 		    i, IDX2ID(i), sip->swsub_name);
400*f6e214c7SGavin Maltby 
401*f6e214c7SGavin Maltby 		switch (initrslt) {
402*f6e214c7SGavin Maltby 		case SW_SUB_INIT_SUCCESS:
403*f6e214c7SGavin Maltby 			if (dp == NULL || nelem < 1)
404*f6e214c7SGavin Maltby 				fmd_hdl_abort(hdl, "%s returned dispatch "
405*f6e214c7SGavin Maltby 				    "table 0x%p and nelem %d\n",
406*f6e214c7SGavin Maltby 				    dbgbuf, dp, nelem);
407*f6e214c7SGavin Maltby 
408*f6e214c7SGavin Maltby 			fmd_hdl_debug(hdl, "%s initialized\n", dbgbuf);
409*f6e214c7SGavin Maltby 			sw_add_callbacks(hdl, sip->swsub_name, dp, nelem,
410*f6e214c7SGavin Maltby 			    msinfo);
411*f6e214c7SGavin Maltby 			break;
412*f6e214c7SGavin Maltby 
413*f6e214c7SGavin Maltby 		case SW_SUB_INIT_FAIL_VOLUNTARY:
414*f6e214c7SGavin Maltby 			fmd_hdl_debug(hdl, "%s chose not to initialize\n",
415*f6e214c7SGavin Maltby 			    dbgbuf);
416*f6e214c7SGavin Maltby 			sw_add_callbacks(hdl, sip->swsub_name, NULL, -1,
417*f6e214c7SGavin Maltby 			    msinfo);
418*f6e214c7SGavin Maltby 			break;
419*f6e214c7SGavin Maltby 
420*f6e214c7SGavin Maltby 		case SW_SUB_INIT_FAIL_ERROR:
421*f6e214c7SGavin Maltby 			fmd_hdl_debug(hdl, "%s failed to initialize "
422*f6e214c7SGavin Maltby 			    "because of an error\n", dbgbuf);
423*f6e214c7SGavin Maltby 			sw_add_callbacks(hdl, sip->swsub_name, NULL, -1,
424*f6e214c7SGavin Maltby 			    msinfo);
425*f6e214c7SGavin Maltby 			break;
426*f6e214c7SGavin Maltby 
427*f6e214c7SGavin Maltby 		default:
428*f6e214c7SGavin Maltby 			fmd_hdl_abort(hdl, "%s returned out-of-range result "
429*f6e214c7SGavin Maltby 			    "%d\n", dbgbuf, initrslt);
430*f6e214c7SGavin Maltby 			break;
431*f6e214c7SGavin Maltby 		}
432*f6e214c7SGavin Maltby 	}
433*f6e214c7SGavin Maltby 
434*f6e214c7SGavin Maltby 	return (1);
435*f6e214c7SGavin Maltby }
436*f6e214c7SGavin Maltby 
437*f6e214c7SGavin Maltby void
sw_fmd_fini(fmd_hdl_t * hdl)438*f6e214c7SGavin Maltby sw_fmd_fini(fmd_hdl_t *hdl)
439*f6e214c7SGavin Maltby {
440*f6e214c7SGavin Maltby 	const struct sw_subinfo **subinfo;
441*f6e214c7SGavin Maltby 	struct sw_modspecific *msinfo;
442*f6e214c7SGavin Maltby 	int i;
443*f6e214c7SGavin Maltby 
444*f6e214c7SGavin Maltby 	msinfo = (struct sw_modspecific *)fmd_hdl_getspecific(hdl);
445*f6e214c7SGavin Maltby 	subinfo = *msinfo->swms_subinfo;
446*f6e214c7SGavin Maltby 
447*f6e214c7SGavin Maltby 	(void) pthread_mutex_lock(&msinfo->swms_timerlock);
448*f6e214c7SGavin Maltby 	for (i = 0; i < SW_TIMER_MAX; i++) {
449*f6e214c7SGavin Maltby 		if (msinfo->swms_timers[i].swt_state != SW_TMR_INUSE)
450*f6e214c7SGavin Maltby 			continue;
451*f6e214c7SGavin Maltby 
452*f6e214c7SGavin Maltby 		fmd_timer_remove(hdl, msinfo->swms_timers[i].swt_timerid);
453*f6e214c7SGavin Maltby 		msinfo->swms_timers[i].swt_state = SW_TMR_RMVD;
454*f6e214c7SGavin Maltby 	}
455*f6e214c7SGavin Maltby 	(void) pthread_mutex_unlock(&msinfo->swms_timerlock);
456*f6e214c7SGavin Maltby 
457*f6e214c7SGavin Maltby 	(void) pthread_mutex_destroy(&msinfo->swms_timerlock);
458*f6e214c7SGavin Maltby 
459*f6e214c7SGavin Maltby 	for (i = 0; i < msinfo->swms_dispcnt; i++) {
460*f6e214c7SGavin Maltby 		const struct sw_subinfo *sip = subinfo[i];
461*f6e214c7SGavin Maltby 
462*f6e214c7SGavin Maltby 		if ((*msinfo->swms_disptbl)[i] == NULL)
463*f6e214c7SGavin Maltby 			continue;	/* swsub_init did not succeed */
464*f6e214c7SGavin Maltby 
465*f6e214c7SGavin Maltby 		if (sip->swsub_fini != NULL)
466*f6e214c7SGavin Maltby 			(*sip->swsub_fini)(hdl);
467*f6e214c7SGavin Maltby 	}
468*f6e214c7SGavin Maltby 
469*f6e214c7SGavin Maltby 	fmd_hdl_free(hdl, msinfo->swms_disptbl,
470*f6e214c7SGavin Maltby 	    SW_SUB_MAX * sizeof (struct sw_disp *));
471*f6e214c7SGavin Maltby 
472*f6e214c7SGavin Maltby 	fmd_hdl_setspecific(hdl, NULL);
473*f6e214c7SGavin Maltby 	fmd_hdl_free(hdl, msinfo, sizeof (*msinfo));
474*f6e214c7SGavin Maltby }
475