File: sim_init.c

package info (click to toggle)
openhpi 3.8.0-2.3
  • links: PTS
  • area: main
  • in suites: sid, trixie
  • size: 31,888 kB
  • sloc: ansic: 225,326; cpp: 63,687; java: 16,472; cs: 15,161; python: 11,884; sh: 11,508; makefile: 4,945; perl: 1,529; xml: 36; asm: 13
file content (354 lines) | stat: -rw-r--r-- 10,761 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*      -*- linux-c -*-
 *
 * (C) Copyright IBM Corp. 2005, 2006
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  This
 * file and program are licensed under a BSD style license.  See
 * the Copying file included with the OpenHPI distribution for
 * full licensing terms.
 *
 * Author(s):
 *        Christina Hernandez <hernanc@us.ibm.com>
 *        W. David Ashley <dashley@us.ibm.com>
 *	  Renier Morales <renier@openhpi.org>
 */

#include <sim_init.h>


/* This list maintains a list of all handler state structs. It is used by
   to determine the name of a handler so that the pointer to the handler state
   can be returned to an injector API.
 */
GSList *sim_handler_states = NULL;


void *sim_open(GHashTable *handler_config,
               unsigned int hid,
               oh_evt_queue *eventq)
{
        struct oh_handler_state *state = NULL;
        char *tok = NULL;

        if (!handler_config) {
                err("GHashTable *handler_config is NULL!");
                return NULL;
        } else if (!hid) {
                err("Bad handler id passed.");
                return NULL;
        } else if (!eventq) {
                err("No event queue was passed.");
                return NULL;
        }
        /* check for required hash table entries */
        tok = g_hash_table_lookup(handler_config, "entity_root");
        if (!tok) {
                err("entity_root is needed and not present in conf");
                return NULL;
        }

        state = g_malloc0(sizeof(struct oh_handler_state));
        if (!state) {
                err("out of memory");
                return NULL;
        }

        /* initialize rpt hashtable pointer */
        state->rptcache = (RPTable *)g_malloc0(sizeof(RPTable));
        oh_init_rpt(state->rptcache);

        /* initialize the event log */
        state->elcache = oh_el_create(256);
        if (!state->elcache) {
                err("Event log creation failed");
                g_free(state->rptcache);
                g_free(state);
                return NULL;
        }

        /* save the handler config hash table, it holds  */
        /* the openhpi.conf file config info             */
        state->config = handler_config;

        /* Store reference to event queue */
        state->eventq = eventq;

        /* Store id of this handler */
        state->hid = hid;

        /* save the handler state to our list */
        sim_handler_states = g_slist_append(sim_handler_states, state);

        return (void *)state;
}


SaErrorT sim_discover(void *hnd)
{
        /* NOTE!!!!!!!!!!!!!!!!!!!!!!!!!!!!
           Since the simulator uses the full managed hot swap model and we
           do not have any latency issues, discovery only needs to be performed
           one time for each handler instance. Subsequent calls should just
           return SA_OK for that instance.
         */
        struct oh_handler_state *inst = (struct oh_handler_state *)hnd;
        int i;
        struct oh_event *e = NULL;
        SaErrorT error = SA_OK;

        /* We use the inst->data variable to store the initial discovery state
           for an instance of the handler.
         */
        if (inst->data) {
                return SA_OK;
        }

        /* ---------------------------------------------------------------
           The following assumes that the resource array is in a specific
           order. Changing this order means changing some of this code.
           ------------------------------------------------------------ */

        /* discover chassis resources and RDRs */
        i = SIM_RPT_ENTRY_CHASSIS - 1;
        error = sim_inject_resource(inst, &sim_rpt_array[i], NULL, &e);
        if (!error) {
		sim_discover_chassis_sensors(inst, e);
		sim_discover_chassis_controls(inst, e);
		sim_discover_chassis_annunciators(inst, e);
		sim_discover_chassis_watchdogs(inst, e);
		sim_discover_chassis_inventory(inst, e);
		sim_discover_chassis_dimis(inst,e); 
		sim_discover_chassis_fumis(inst,e);
		sim_inject_event(inst, e);
		e = NULL;
        } else err("Error discovering chassis");

        /* discover cpu resources and RDRs */
        i = SIM_RPT_ENTRY_CPU - 1;
        error = sim_inject_resource(inst, &sim_rpt_array[i], NULL, &e);
        if (!error) {
        	sim_discover_cpu_sensors(inst, e);
        	sim_discover_cpu_controls(inst, e);
        	sim_discover_cpu_annunciators(inst, e);
        	sim_discover_cpu_watchdogs(inst, e);
        	sim_discover_cpu_inventory(inst, e);
		sim_inject_event(inst, e);
        	e = NULL;
	} else err("Error discovering CPU");

        /* discover dasd resources and RDRs */
        i = SIM_RPT_ENTRY_DASD - 1;
	error = sim_inject_resource(inst, &sim_rpt_array[i], NULL, &e);
	if (!error) {
		sim_discover_dasd_sensors(inst, e);
		sim_discover_dasd_controls(inst, e);
		sim_discover_dasd_annunciators(inst, e);
		sim_discover_dasd_watchdogs(inst, e);
		sim_discover_dasd_inventory(inst, e);
		sim_inject_event(inst, e);
		e = NULL;
        } else err("Error discovering DASD");

        /* discover hot swap dasd resources and RDRs */
        i = SIM_RPT_ENTRY_HS_DASD - 1;
	error = sim_inject_resource(inst, &sim_rpt_array[i], NULL, &e);
	if (!error) {
		sim_discover_hs_dasd_sensors(inst, e);
		sim_discover_hs_dasd_controls(inst, e);
		sim_discover_hs_dasd_annunciators(inst, e);
		sim_discover_hs_dasd_watchdogs(inst, e);
		sim_discover_hs_dasd_inventory(inst, e);
		sim_inject_event(inst, e);
		e = NULL;
        } else err("Error discovering HS DASD");

        /* discover fan resources and RDRs */
        i = SIM_RPT_ENTRY_FAN - 1;
	error = sim_inject_resource(inst, &sim_rpt_array[i], NULL, &e);
	if (!error) {
		sim_discover_fan_sensors(inst, e);
		sim_discover_fan_controls(inst, e);
		sim_discover_fan_annunciators(inst, e);
		sim_discover_fan_watchdogs(inst, e);
		sim_discover_fan_inventory(inst, e);
		sim_inject_event(inst, e);
		e = NULL;
        } else err("Error discovering FAN");

        /* Let subsequent discovery invocations know that discovery has already
           been performed.
         */
        inst->data = (void *)1;
        return SA_OK;
}


/*
 * Return values:
 * 1 - events to be processed.
 * SA_OK - No events to be processed.
 * SA_ERR_HPI_INVALID_PARAMS - @hnd is NULL.
 */
SaErrorT sim_get_event(void *hnd)
{

        if (!hnd) return SA_ERR_HPI_INVALID_PARAMS;
        
	return SA_OK;
}


SaErrorT sim_close(void *hnd)
{
    struct oh_handler_state *state = hnd;

    SaHpiEntryIdT rid;
    SaHpiRptEntryT *rpte;
    GSList *events = 0;

    rid = SAHPI_FIRST_ENTRY;
    while ((rpte = oh_get_resource_next(state->rptcache, rid)) != 0) {
        struct oh_event *e = g_new0(struct oh_event, 1);
        e->hid = state->hid;
        e->resource = *rpte;
        e->rdrs = 0;
        e->rdrs_to_remove = 0;
        e->event.Source = rpte->ResourceId;
        e->event.EventType = SAHPI_ET_RESOURCE;
        oh_gettimeofday(&e->event.Timestamp);
        e->event.Severity = SAHPI_MAJOR;
        e->event.EventDataUnion.ResourceEvent.ResourceEventType = SAHPI_RESE_RESOURCE_REMOVED;
        events = g_slist_prepend(events, e);

        rid = rpte->ResourceId;
    }

    GSList *iter = events;
    while (iter) {
        oh_evt_queue_push(state->eventq, iter->data );
        iter = g_slist_next(iter);
    }
    g_slist_free(events);

    oh_el_close(state->elcache);
    oh_flush_rpt(state->rptcache);
    g_free(state->rptcache);
    g_free(state);

    return 0;
}

SaErrorT sim_set_resource_tag(void *hnd, SaHpiResourceIdT id, SaHpiTextBufferT *tag)
{
        struct oh_handler_state *inst = hnd;
        SaHpiRptEntryT *resource = NULL;

        if (!tag)
                return SA_ERR_HPI_INVALID_PARAMS;

        resource = oh_get_resource_by_id(inst->rptcache, id);
        if (!resource) {
                return SA_ERR_HPI_NOT_PRESENT;
        }

        memcpy(&resource->ResourceTag, tag, sizeof(SaHpiTextBufferT));

        return SA_OK;
}

SaErrorT sim_set_resource_severity(void *hnd, SaHpiResourceIdT rid, SaHpiSeverityT sev)
{
	struct oh_handler_state *h = hnd;
	SaHpiRptEntryT *resource = NULL;
	
	resource = oh_get_resource_by_id(h->rptcache, rid);
	if (!resource) {
		return SA_ERR_HPI_NOT_PRESENT;
	}
	
	resource->ResourceSeverity = sev;
	
	return SA_OK;
}

SaErrorT sim_resource_failed_remove(void *hnd, SaHpiResourceIdT rid)
{
	struct oh_handler_state *h;
	SaHpiRptEntryT *resource = NULL;
	struct oh_event e;
	SaHpiHsStateT hsstate = SAHPI_HS_STATE_ACTIVE;
	SaErrorT rv;

	if (hnd == NULL) {
		err("Invalid parameter");
		return SA_ERR_HPI_INVALID_PARAMS;
	}

	h = (struct oh_handler_state *) hnd;
	resource = oh_get_resource_by_id(h->rptcache, rid);
	if (resource == NULL) {
		err("Failed to get the RPT entry");
		return SA_ERR_HPI_NOT_PRESENT;
	}

	if (resource->ResourceCapabilities & SAHPI_CAPABILITY_MANAGED_HOTSWAP) {
		rv = sim_get_hotswap_state(hnd, rid, &hsstate);
		if (rv != SA_OK) {
			err("Failed to get the hotswap state");
			return rv;
		}
	}

	/* Raise the resource removal hotswap event */
	memset(&e, 0, sizeof(struct oh_event));
	e.hid = h->hid;
	e.resource = *resource;
	e.rdrs = NULL;
	e.event.Source = rid;
	e.event.Severity = resource->ResourceSeverity;
	oh_gettimeofday(&e.event.Timestamp);
	e.event.EventType = SAHPI_ET_HOTSWAP;
	e.event.EventDataUnion.HotSwapEvent.PreviousHotSwapState = hsstate;
	e.event.EventDataUnion.HotSwapEvent.HotSwapState =
		SAHPI_HS_STATE_NOT_PRESENT;
	e.event.EventDataUnion.HotSwapEvent.CauseOfStateChange =
		SAHPI_HS_CAUSE_USER_UPDATE;

	oh_evt_queue_push(h->eventq, oh_dup_event(&e));

	/* Remove the failed resource from plugin rptcache */
	rv = oh_remove_resource(h->rptcache, rid);
	if (rv != SA_OK) {
		err("Resource removal from RPTable failed");
		return rv;
	}

	return SA_OK;
}

/*
 * Simulator plugin interface
 *
 */

void * oh_open (GHashTable *, unsigned int, oh_evt_queue *) __attribute__ ((weak, alias("sim_open")));

void * oh_close (void *) __attribute__ ((weak, alias("sim_close")));

void * oh_get_event (void *)
                __attribute__ ((weak, alias("sim_get_event")));

void * oh_discover_resources (void *)
                __attribute__ ((weak, alias("sim_discover")));

void * oh_set_resource_tag (void *, SaHpiResourceIdT, SaHpiTextBufferT *)
                __attribute__ ((weak, alias("sim_set_resource_tag")));
                
void * oh_set_resource_severity (void *, SaHpiResourceIdT, SaHpiSeverityT)
		__attribute__ ((weak, alias("sim_set_resource_severity")));

void * oh_resource_failed_remove (void *, SaHpiResourceIdT)
		__attribute__ ((weak, alias("sim_resource_failed_remove")));