File: ipmi_sensor_event.c

package info (click to toggle)
openhpi 3.8.0-2
  • links: PTS
  • area: main
  • in suites: buster
  • size: 31,792 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 (1208 lines) | stat: -rw-r--r-- 34,407 bytes parent folder | download | duplicates (8)
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
/*      -*- linux-c -*-
 *
 * Copyright (c) 2003 by Intel Corp.
 *
 * 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.
 *
 * Authors:
 *     Louis Zhuang <louis.zhuang@linux.intel.com>
 */

#include "ipmi.h"
#include <oh_utils.h>
#include <string.h>

enum ohoi_event_type {
	EVENT_DATA_0 = 0,
	EVENT_DATA_1,
	EVENT_DATA_2,
	EVENT_DATA_3
};

/* IPMI does not define the decrete sensor event state */
enum ohoi_discrete_e {
	IPMI_TRANS_IDLE = 0,
	IPMI_TRANS_ACTIVE,
	IPMI_TRANS_BUSY
};




static void set_discrete_sensor_misc_event(ipmi_event_t		*event,
					   SaHpiSensorEventT	*e)
{
	enum ohoi_event_type  type;
	char	data[IPMI_EVENT_DATA_MAX_LEN];
	int	dt_len;
	SaHpiSensorOptionalDataT od = 0;

	dt_len = ipmi_event_get_data(event, (unsigned char *)data, 0, IPMI_EVENT_DATA_MAX_LEN);
	if (dt_len != 13) {
		err("Wrong size of ipmi event data = %i", dt_len);
		return;
	}


	e->EventState = (1 << (data[10] & 0x0f));
	type = data[10] >> 6;
	if (type == EVENT_DATA_1) {
		if ((data[11] & 0x0f) != 0x0f) {
			od |= SAHPI_SOD_PREVIOUS_STATE;
			e->PreviousState = (1 << (data[11] & 0x0f));
		}
	} else if (type == EVENT_DATA_2) {
		od |= SAHPI_SOD_OEM;
	} else if (type == EVENT_DATA_3) {
		od |= SAHPI_SOD_SENSOR_SPECIFIC;
	}

	type = (data[10] & 0x30) >> 4;
	if (type == EVENT_DATA_2) {
		od |= SAHPI_SOD_OEM;
	} else if (type == EVENT_DATA_3) {
		od |= SAHPI_SOD_SENSOR_SPECIFIC;
	}
	if (e->SensorType == IPMI_SENSOR_TYPE_OS_CRITICAL_STOP) {
		// implementation feature
		od |= SAHPI_SOD_SENSOR_SPECIFIC;
		e->SensorSpecific = (data[12] << 16) |
			(data[11] << 8) | data[9];
	} else {
		e->SensorSpecific = (data[12] << 16) |
			(data[11] << 8) | data[10];
	}
	e->Oem = (data[12] << 16) | (data[11] << 8) | data[10];
	e->OptionalDataPresent = od;
}




static void set_event_sensor_num(ipmi_sensor_t    *sensor,
				 struct oh_event *e,
				 struct oh_handler_state *handler)
{
	ipmi_entity_id_t	entity_id;
        ipmi_sensor_id_t        sensor_id;
        SaHpiRptEntryT          *rpt_entry;
        SaHpiRdrT               *rdr;

	entity_id  = ipmi_entity_convert_to_id(ipmi_sensor_get_entity(sensor));
	sensor_id = ipmi_sensor_convert_to_id(sensor);
	rpt_entry  = ohoi_get_resource_by_entityid(handler->rptcache,
	                                 &entity_id);

	if (!rpt_entry) {
                dump_entity_id("Sensor without RPT Entry?!", entity_id);
		return;
	}
	e->event.Source = rpt_entry->ResourceId;
        rdr  = ohoi_get_rdr_by_data(handler->rptcache,
		                    rpt_entry->ResourceId,
	                            SAHPI_SENSOR_RDR, &sensor_id);
        if (!rdr) {
                err("No rdr for sensor %d in resource:%d\n",
			sensor_id.sensor_num, rpt_entry->ResourceId);
		return;
	}

	e->event.EventDataUnion.SensorEvent.SensorNum =
		rdr->RdrTypeUnion.SensorRec.Num;
}


/*
 * sensor_discrete_map_event
 * @dir: assertion or disassertion
 * @offset: not used now
 * @severity: severity of event
 * @prev_severity: previous state of sensor event
 * @event: ipmi event
 *
 * Helper function to map ipmi event from discrete sensor to oh_event.
 * It is used to implement saHpiEventLogEntryGet() and saHpiEventGet().
 *
 * Returns: oh_event to which ipmi event is mapped
 */
static struct oh_event *sensor_discrete_map_event(
				  struct ohoi_handler *ipmi_handler,
				  enum ipmi_event_dir_e	dir,
				  int	offset,
				  int	severity,
				  int	prev_severity,
				  ipmi_event_t	*event)
{
	struct oh_event         *e;
        unsigned char           data[IPMI_EVENT_DATA_MAX_LEN];
	unsigned int		dt_len;

        dt_len = ipmi_event_get_data(event, data, 0, IPMI_EVENT_DATA_MAX_LEN);
	if (dt_len != 13) {
		err("Wrong size of ipmi event data = %i", dt_len);
		return NULL;
	}
        e = malloc(sizeof(*e));
	if (!e) {
	err("Out of space");
		return NULL;
	}
	memset(e, 0, sizeof(*e));
	e->event.Source = 0;
	/* Do not find EventType in IPMI */
	e->event.EventType = SAHPI_ET_SENSOR;
	e->event.Timestamp = ipmi_event_get_timestamp(event);

	e->event.Severity = (SaHpiSeverityT)severity;

	e->event.EventDataUnion.SensorEvent.SensorNum = 0;
	e->event.EventDataUnion.SensorEvent.SensorType = data[7];
	if ((data[9] & 0x7f) == 0x6f) {
		/* sensor specific event */
		e->event.EventDataUnion.SensorEvent.EventCategory =
			SAHPI_EC_SENSOR_SPECIFIC;
	} else if ((data[9] & 0x7f) >= 0x70) {
		e->event.EventDataUnion.SensorEvent.EventCategory =
			SAHPI_EC_GENERIC;
	} else {
		e->event.EventDataUnion.SensorEvent.EventCategory
                	= data[9] & 0x7f;
	}
	if (data[7] >= 0xc0) {
		e->event.EventDataUnion.SensorEvent.SensorType =
				SAHPI_OEM_SENSOR;
	}


	e->event.EventDataUnion.SensorEvent.Assertion
                = !(dir);

	set_discrete_sensor_misc_event(event,
		&e->event.EventDataUnion.SensorEvent);


	if (!IS_ATCA(ipmi_handler->d_type) || ( data[7] != 0xf1)) {
		return e;
	}
	// ATCA IPMB-0 link sensor. Special mapping
	e->event.EventDataUnion.SensorEvent.EventCategory =
							SAHPI_EC_REDUNDANCY;
	switch (data[10] & 0x0f) { // IPMI event state
	case 0x00: // IPMB_A disable, IPMB-B disable
		e->event.EventDataUnion.SensorEvent.EventState =
				SAHPI_ES_NON_REDUNDANT_INSUFFICIENT_RESOURCES;
		e->event.Severity = SAHPI_CRITICAL;
		break;
	case 0x01: // IPMB_A enable, IPMB-B disable
	case 0x02: // IPMB_A disable, IPMB-B enable
		e->event.EventDataUnion.SensorEvent.EventState =
				SAHPI_ES_NON_REDUNDANT_SUFFICIENT_RESOURCES;
		e->event.Severity = SAHPI_MAJOR;
		break;
	case 0x03: // IPMB_A enable, IPMB-B enable
		e->event.EventDataUnion.SensorEvent.EventState =
					SAHPI_ES_FULLY_REDUNDANT;
		e->event.Severity = SAHPI_OK;
		break;
	default:
		err("wrong IPMIB-0 Status Change Event State = 0x%x",
				data[10] & 0x0f);
		break;
	}
	if (!(e->event.EventDataUnion.SensorEvent.OptionalDataPresent
			 & SAHPI_SOD_PREVIOUS_STATE)) {
		// no previous state
		return e;
	}
	switch (data[11] & 0x0f) { // previous IPMI event state
	case 0x00: // IPMB_A disable, IPMB-B disable
		e->event.EventDataUnion.SensorEvent.PreviousState =
				SAHPI_ES_NON_REDUNDANT_INSUFFICIENT_RESOURCES;
		break;
	case 0x01: // IPMB_A enable, IPMB-B disable
	case 0x02: // IPMB_A disable, IPMB-B enable
		e->event.EventDataUnion.SensorEvent.PreviousState =
				SAHPI_ES_NON_REDUNDANT_SUFFICIENT_RESOURCES;
		break;
	case 0x03: // IPMB_A enable, IPMB-B enable
		e->event.EventDataUnion.SensorEvent.PreviousState =
					SAHPI_ES_FULLY_REDUNDANT;
		break;
	default:
		err("wrong IPMIB-0 Status Change Previous Event State = 0x%x",
				data[11] & 0x0f);
		break;
	}

	return e;

}

static int sensor_discrete_event(ipmi_sensor_t	*sensor,
				  enum ipmi_event_dir_e		dir,
				  int				offset,
				  int				severity,
				  int				prev_severity,
				  void			*	cb_data,
				  ipmi_event_t			*event)
{
	struct oh_event		*e;
	struct oh_handler_state *handler = cb_data;
	struct ohoi_handler *ipmi_handler = handler->data;
	ipmi_sensor_id_t sid = ipmi_sensor_convert_to_id(sensor);

	trace_ipmi_sensors("EVENT", sid);
	if ((ipmi_handler->d_type == IPMI_DOMAIN_TYPE_ATCA) &&
			(ipmi_sensor_get_sensor_type(sensor) == 0xF0)) {
		// hot swap sensor. We don't report its event.
		// hotswap event will be reported via entity hotswap
		// handeler instead of.
		return IPMI_EVENT_NOT_HANDLED;
	}
	e = sensor_discrete_map_event(ipmi_handler, dir, offset, severity,
					prev_severity, event);
	if (e == NULL) {
		return IPMI_EVENT_HANDLED;
	}
	set_event_sensor_num(sensor, e, handler);
	e->hid = handler->hid;
        oh_evt_queue_push(handler->eventq, e);

	return IPMI_EVENT_HANDLED;
}


static void
set_thresholed_sensor_event_state(enum ipmi_thresh_e		threshold,
				  enum ipmi_event_dir_e		dir,
				  enum ipmi_event_value_dir_e	high_low,
				  SaHpiSensorEventT		*event,
				  SaHpiSeverityT		*severity)
{
	if ((dir == IPMI_ASSERTION && high_low == IPMI_GOING_HIGH) ||
	    (dir == IPMI_DEASSERTION && high_low == IPMI_GOING_LOW))
		event->Assertion = SAHPI_TRUE;
	else if ((dir == IPMI_ASSERTION &&  high_low == IPMI_GOING_LOW) ||
		 (dir == IPMI_DEASSERTION && high_low == IPMI_GOING_HIGH))
		event->Assertion = SAHPI_FALSE;

	switch (threshold) {
		case IPMI_LOWER_NON_CRITICAL:
			event->EventState = SAHPI_ES_LOWER_MINOR;
			*severity = SAHPI_MINOR;
			break;

		case IPMI_LOWER_CRITICAL:
			event->EventState = SAHPI_ES_LOWER_MAJOR;
			*severity = SAHPI_MAJOR;
			break;

		case IPMI_LOWER_NON_RECOVERABLE:
			event->EventState = SAHPI_ES_LOWER_CRIT;
			*severity = SAHPI_CRITICAL;
			break;

		case IPMI_UPPER_NON_CRITICAL:
			event->EventState = SAHPI_ES_UPPER_MINOR;
			*severity = SAHPI_MINOR;
			break;

		case IPMI_UPPER_CRITICAL:
			event->EventState = SAHPI_ES_UPPER_MAJOR;
			*severity = SAHPI_MAJOR;
			break;

		case IPMI_UPPER_NON_RECOVERABLE:
			event->EventState = SAHPI_ES_UPPER_CRIT;
			*severity = SAHPI_CRITICAL;
			break;

		default:
			err("Invalid threshold giving");
			event->EventState = SAHPI_ES_UNSPECIFIED;
	}
}

static void set_thresholds_sensor_misc_event(ipmi_event_t	*event,
					      SaHpiSensorEventT	*e)
{
	unsigned int type;
	SaHpiSensorOptionalDataT od = 0;
	unsigned char	data[IPMI_EVENT_DATA_MAX_LEN];
	unsigned int	dt_len;

	dt_len = ipmi_event_get_data(event, data, 0, IPMI_EVENT_DATA_MAX_LEN);
	if (dt_len != 13) {
		err("Wrong size of ipmi event data = %i", dt_len);
		return;
	}
	type = data[10] >> 6;
	if (type == EVENT_DATA_1) {
		od |= SAHPI_SOD_TRIGGER_READING;
		e->TriggerReading.IsSupported = SAHPI_TRUE;
		e->TriggerReading.Type = SAHPI_SENSOR_READING_TYPE_UINT64;
		e->TriggerReading.Value.SensorUint64 = data[11];
	} else if (type == EVENT_DATA_2) {
		od |= SAHPI_SOD_OEM;
	} else if (type == EVENT_DATA_3) {
		od |= SAHPI_SOD_SENSOR_SPECIFIC;
	}

	type = (data[10] & 0x30) >> 4;
	if (type == EVENT_DATA_1) {
		od |= SAHPI_SOD_TRIGGER_THRESHOLD;
		e->TriggerThreshold.IsSupported = SAHPI_TRUE;
                e->TriggerThreshold.Type = SAHPI_SENSOR_READING_TYPE_UINT64;
                e->TriggerThreshold.Value.SensorUint64 = data[12];
	} else if (type == EVENT_DATA_2) {
		od |= SAHPI_SOD_OEM;
	} else if (type == EVENT_DATA_3) {
		od |= SAHPI_SOD_SENSOR_SPECIFIC;
	}
	if (e->SensorType == IPMI_SENSOR_TYPE_OS_CRITICAL_STOP) {
		od |= SAHPI_SOD_SENSOR_SPECIFIC;
		e->SensorSpecific = (data[12] << 16) |
			(data[11] << 8) | data[9];
	} else {
		e->SensorSpecific = (data[12] << 16) |
			(data[11] << 8) | data[10];
	}
	e->Oem = (data[12] << 16) | (data[11] << 8) | data[10];
	e->OptionalDataPresent = od;
}

/*
 * sensor_threshold_map_event
 * @dir: assertion or disassertion - pass to set_thresholed_sensor_event_state
 * @threshhold: to pass to set_thresholed_sensor_event_state
 * @high_low: to pass to set_thresholed_sensor_event_state
 * @value_present: not used now
 * @raw_value: not used now
 * value: not used now
 * @event: ipmi event
 *
 * Helper function to map ipmi event from threshold sensor to oh_event.
 * It is used to implement saHpiEventLogEntryGet() and saHpiEventGet().
 *
 * Returns: oh_event to which ipmi event is mapped
 */
static struct oh_event *sensor_threshold_map_event(
				   enum ipmi_event_dir_e	dir,
				   enum ipmi_thresh_e		threshold,
				   enum ipmi_event_value_dir_e	high_low,
				   enum ipmi_value_present_e	value_present,
				   unsigned int			raw_value,
				   double			value,
				   ipmi_event_t			*event)
{
	struct oh_event		*e;
	SaHpiSeverityT		severity = SAHPI_CRITICAL;
	unsigned char	data[IPMI_EVENT_DATA_MAX_LEN];
	unsigned int	dt_len;

	dt_len = ipmi_event_get_data(event, data, 0, IPMI_EVENT_DATA_MAX_LEN);
	if (dt_len != 13) {
		err("Wrong size of ipmi event data = %i", dt_len);
		return NULL;
	}

	e = malloc(sizeof(*e));
	if (!e) {
		err("Out of space");
		return NULL;
	}

	memset(e, 0, sizeof(*e));
	e->event.Source = 0;
	e->event.EventType = SAHPI_ET_SENSOR;
	e->event.Timestamp = ipmi_event_get_timestamp(event);

       // sensor num should be assigned later in calling functions
       e->event.EventDataUnion.SensorEvent.SensorNum = 0;

	if (data[7] >= 0xc0) {
		e->event.EventDataUnion.SensorEvent.SensorType =
			SAHPI_OEM_SENSOR;
	} else {
		e->event.EventDataUnion.SensorEvent.SensorType =
                	data[7];
	}
	e->event.EventDataUnion.SensorEvent.EventCategory
                = data[9] & 0x7f;

	set_thresholed_sensor_event_state(threshold, dir, high_low,
			&e->event.EventDataUnion.SensorEvent,
			&severity);
	e->event.Severity = severity;

	set_thresholds_sensor_misc_event
		(event, &e->event.EventDataUnion.SensorEvent);
        return e;

}


static int sensor_threshold_event(ipmi_sensor_t		*sensor,
				   enum ipmi_event_dir_e	dir,
				   enum ipmi_thresh_e		threshold,
				   enum ipmi_event_value_dir_e	high_low,
				   enum ipmi_value_present_e	value_present,
				   unsigned int			raw_value,
				   double			value,
				   void				*cb_data,
				   ipmi_event_t			*event)
{
	struct oh_event		*e = NULL;
	struct oh_handler_state *handler = cb_data;

	e = sensor_threshold_map_event(dir, threshold, high_low,
			value_present, raw_value, value, event);
	if (e == NULL) {
		return IPMI_EVENT_HANDLED;
	}
	set_event_sensor_num(sensor, e, handler);
	e->hid = handler->hid;
        oh_evt_queue_push(handler->eventq, e);

	return IPMI_EVENT_HANDLED;
}


static void add_sensor_event_thresholds(ipmi_sensor_t	*sensor,
					SaHpiSensorRecT	*rec)
{
	int 			val;
	SaHpiSensorThdMaskT	temp;
	unsigned int		access;

	if (rec->Category != SAHPI_EC_THRESHOLD) {
		rec->ThresholdDefn.IsAccessible = SAHPI_FALSE;
		return;
	}

	access = ipmi_sensor_get_threshold_access(sensor);
	if (access == IPMI_THRESHOLD_ACCESS_SUPPORT_NONE) {
		rec->ThresholdDefn.IsAccessible = SAHPI_FALSE;
		return;
	}

	if (access >= IPMI_THRESHOLD_ACCESS_SUPPORT_READABLE) {
		rec->ThresholdDefn.IsAccessible = SAHPI_TRUE;

		temp = 0;
		ipmi_sensor_threshold_readable(sensor,
				IPMI_LOWER_NON_CRITICAL, &val);
		if (val)
			temp |= SAHPI_STM_LOW_MINOR;

		ipmi_sensor_threshold_readable(sensor, IPMI_LOWER_CRITICAL,
					       &val);
		if (val)
			temp |= SAHPI_STM_LOW_MAJOR;

		ipmi_sensor_threshold_readable(sensor,
				               IPMI_LOWER_NON_RECOVERABLE,
					       &val);
		if (val)
			temp |= SAHPI_STM_LOW_CRIT;

		ipmi_sensor_threshold_readable(sensor, IPMI_UPPER_NON_CRITICAL,
					       &val);
		if (val)
			temp |= SAHPI_STM_UP_MINOR;

		ipmi_sensor_threshold_readable(sensor, IPMI_UPPER_CRITICAL,
					       &val);
		if (val)
			temp |= SAHPI_STM_UP_MAJOR;

		ipmi_sensor_threshold_readable(sensor,
					       IPMI_UPPER_NON_RECOVERABLE,
					       &val);
		if (val)
			temp |= SAHPI_STM_UP_CRIT;

		val = ipmi_sensor_get_hysteresis_support(sensor);
		if (val == IPMI_HYSTERESIS_SUPPORT_READABLE ||
		    val == IPMI_HYSTERESIS_SUPPORT_SETTABLE)
			temp |= SAHPI_STM_UP_HYSTERESIS |
				SAHPI_STM_LOW_HYSTERESIS;

		rec->ThresholdDefn.ReadThold = temp;
	}

	if (access == IPMI_THRESHOLD_ACCESS_SUPPORT_SETTABLE) {
		temp = 0;
		ipmi_sensor_threshold_settable(sensor, IPMI_LOWER_NON_CRITICAL,
					       &val);
		if (val)
			temp |= SAHPI_STM_LOW_MINOR;

		ipmi_sensor_threshold_settable(sensor, IPMI_LOWER_CRITICAL,
					       &val);
		if (val)
			temp |= SAHPI_STM_LOW_MAJOR;

		ipmi_sensor_threshold_settable(sensor,
					       IPMI_LOWER_NON_RECOVERABLE,
					       &val);
		if (val)
			temp |= SAHPI_STM_LOW_CRIT;

		ipmi_sensor_threshold_settable(sensor, IPMI_UPPER_NON_CRITICAL,
					       &val);
		if (val)
			temp |= SAHPI_STM_UP_MINOR;

		ipmi_sensor_threshold_settable(sensor, IPMI_UPPER_CRITICAL,
					       &val);
		if (val)
			temp |= SAHPI_STM_UP_MAJOR;

		ipmi_sensor_threshold_settable(sensor,
					       IPMI_UPPER_NON_RECOVERABLE,
					       &val);
		if (val)
			temp |= SAHPI_STM_UP_CRIT;

		val = ipmi_sensor_get_hysteresis_support(sensor);
		if (val == IPMI_HYSTERESIS_SUPPORT_SETTABLE)
			temp |= SAHPI_STM_UP_HYSTERESIS |
				SAHPI_STM_LOW_HYSTERESIS;

		rec->ThresholdDefn.WriteThold = temp;
	}
}

static void add_sensor_event_data_format(ipmi_sensor_t		*sensor,
					 SaHpiSensorRecT	*rec)
{
#define FILL_READING(reading, val)                                         \
	do {                                                               \
		(reading).IsSupported = SAHPI_TRUE;                        \
		(reading).Type = SAHPI_SENSOR_READING_TYPE_FLOAT64;        \
		(reading).Value.SensorFloat64 = (SaHpiFloat64T)val;        \
	} while (0)
	int rv;
	double accur = 0;
	double fval = 0;


	if (ipmi_sensor_get_event_reading_type(sensor) !=
				IPMI_EVENT_READING_TYPE_THRESHOLD) {
		rec->DataFormat.IsSupported = SAHPI_FALSE;
		return;
	}
	rec->DataFormat.IsSupported = SAHPI_TRUE;

	rec->DataFormat.ReadingType = SAHPI_SENSOR_READING_TYPE_FLOAT64;

	rec->DataFormat.BaseUnits = (SaHpiSensorUnitsT)
		ipmi_sensor_get_base_unit(sensor);
	rec->DataFormat.ModifierUnits = (SaHpiSensorUnitsT)
		ipmi_sensor_get_modifier_unit(sensor);
	rec->DataFormat.ModifierUse = (SaHpiSensorModUnitUseT)
		ipmi_sensor_get_modifier_unit_use(sensor);

	ipmi_sensor_get_accuracy(sensor, 0, &accur);
	rec->DataFormat.AccuracyFactor = (SaHpiFloat64T)accur;

	rec->DataFormat.Percentage = (SaHpiBoolT)
		ipmi_sensor_get_percentage(sensor);

	rec->DataFormat.Range.Flags = 0;

	rv = ipmi_sensor_get_sensor_max(sensor, &fval);
	if (!rv) {
		FILL_READING(rec->DataFormat.Range.Max, fval);
		rec->DataFormat.Range.Flags |= SAHPI_SRF_MAX;
	}

	rv = ipmi_sensor_get_sensor_min(sensor, &fval);
	if (!rv) {
		FILL_READING(rec->DataFormat.Range.Min, fval);
		rec->DataFormat.Range.Flags |= SAHPI_SRF_MIN;
	}

	if (ipmi_sensor_get_nominal_reading_specified(sensor)) {
		rv = ipmi_sensor_get_nominal_reading(sensor, &fval);
		if (!rv) {
			FILL_READING(rec->DataFormat.Range.Nominal, fval);
			rec->DataFormat.Range.Flags |= SAHPI_SRF_NOMINAL;
		}
	}
	if (ipmi_sensor_get_normal_max_specified(sensor)) {
		rv = ipmi_sensor_get_normal_max(sensor, &fval);
		if (!rv) {
			FILL_READING(rec->DataFormat.Range.NormalMax, fval);
			rec->DataFormat.Range.Flags |= SAHPI_SRF_NORMAL_MAX;
		}
	}

	if (ipmi_sensor_get_normal_min_specified(sensor)) {
		rv = ipmi_sensor_get_normal_min(sensor, &fval);
		if (!rv) {
			FILL_READING(rec->DataFormat.Range.NormalMin, fval);
			rec->DataFormat.Range.Flags |= SAHPI_SRF_NORMAL_MIN;
		}
	}
}

static SaHpiEventCategoryT ohoi_sensor_get_event_reading_type(ipmi_sensor_t   *sensor)
{
	SaHpiEventCategoryT 	hpi_category;
	unsigned int 		ipmi_category;

	ipmi_category = ipmi_sensor_get_event_reading_type(sensor);
	switch (ipmi_category) {
		case IPMI_EVENT_READING_TYPE_DISCRETE_ACPI_POWER:
		case IPMI_EVENT_READING_TYPE_SENSOR_SPECIFIC:
			hpi_category = SAHPI_EC_GENERIC;
			break;
		default:
			hpi_category = ipmi_category;
			break;
	}
	return hpi_category;
}

static void add_sensor_states(ipmi_sensor_t	*sensor,
			struct ohoi_sensor_info *sensor_info)
{
	int i;
	int val;
	int rv;

	sensor_info->support_assert = 0;
	sensor_info->support_deassert = 0;
	if(ipmi_sensor_get_event_reading_type(sensor) !=
			IPMI_EVENT_READING_TYPE_THRESHOLD) {
		for (i = 0; i < 15; i++) {
			rv = ipmi_sensor_discrete_event_supported(sensor, i,
					IPMI_ASSERTION, &val);
			if ((rv == 0) && val) {
				sensor_info->support_assert |= (1 << i);
			}
			rv = ipmi_sensor_discrete_event_supported(sensor, i,
					IPMI_DEASSERTION, &val);
			if ((rv == 0) && val) {
				sensor_info->support_deassert |= (1 << i);
			}

		}

		return;
	}

	// threshold sensor
	rv = ipmi_sensor_threshold_event_supported(sensor,
		IPMI_LOWER_NON_CRITICAL, IPMI_GOING_LOW,
		IPMI_ASSERTION, &val);
	if ((rv == 0) && (val != 0)) {
		sensor_info->support_assert |= OHOI_THS_LMINL;
	}
	rv = ipmi_sensor_threshold_event_supported(sensor,
		IPMI_LOWER_NON_CRITICAL, IPMI_GOING_HIGH,
		IPMI_ASSERTION, &val);
	if ((rv == 0) && (val != 0)) {
		sensor_info->support_assert |= OHOI_THS_LMINH;
	}
	rv = ipmi_sensor_threshold_event_supported(sensor,
		IPMI_LOWER_NON_CRITICAL, IPMI_GOING_LOW,
		IPMI_DEASSERTION, &val);
	if ((rv == 0) && (val != 0)) {
		sensor_info->support_deassert |= OHOI_THS_LMINL;
	}
	rv = ipmi_sensor_threshold_event_supported(sensor,
		IPMI_LOWER_NON_CRITICAL, IPMI_GOING_HIGH,
		IPMI_DEASSERTION, &val);
	if ((rv == 0) && (val != 0)) {
		sensor_info->support_deassert |= OHOI_THS_LMINH;
	}

	rv = ipmi_sensor_threshold_event_supported(sensor,
		IPMI_LOWER_CRITICAL, IPMI_GOING_LOW,
		IPMI_ASSERTION, &val);
	if ((rv == 0) && (val != 0)) {
		sensor_info->support_assert |= OHOI_THS_LMAJL;
	}
	rv = ipmi_sensor_threshold_event_supported(sensor,
		IPMI_LOWER_CRITICAL, IPMI_GOING_HIGH,
		IPMI_ASSERTION, &val);
	if ((rv == 0) && (val != 0)) {
		sensor_info->support_assert |= OHOI_THS_LMAJH;
	}
	rv = ipmi_sensor_threshold_event_supported(sensor,
		IPMI_LOWER_CRITICAL, IPMI_GOING_LOW,
		IPMI_DEASSERTION, &val);
	if ((rv == 0) && (val != 0)) {
		sensor_info->support_deassert |= OHOI_THS_LMAJL;
	}
	rv = ipmi_sensor_threshold_event_supported(sensor,
		IPMI_LOWER_CRITICAL, IPMI_GOING_HIGH,
		IPMI_DEASSERTION, &val);
	if ((rv == 0) && (val != 0)) {
		sensor_info->support_deassert |= OHOI_THS_LMAJH;
	}

	rv = ipmi_sensor_threshold_event_supported(sensor,
		IPMI_LOWER_NON_RECOVERABLE, IPMI_GOING_LOW,
		IPMI_ASSERTION, &val);
	if ((rv == 0) && (val != 0)) {
		sensor_info->support_assert |= OHOI_THS_LCRTL;
	}
	rv = ipmi_sensor_threshold_event_supported(sensor,
		IPMI_LOWER_NON_RECOVERABLE, IPMI_GOING_HIGH,
		IPMI_ASSERTION, &val);
	if ((rv == 0) && (val != 0)) {
		sensor_info->support_assert |= OHOI_THS_LCRTH;
	}
	rv = ipmi_sensor_threshold_event_supported(sensor,
		IPMI_LOWER_NON_RECOVERABLE, IPMI_GOING_LOW,
		IPMI_DEASSERTION, &val);
	if ((rv == 0) && (val != 0)) {
		sensor_info->support_deassert |= OHOI_THS_LCRTL;
	}
	rv = ipmi_sensor_threshold_event_supported(sensor,
		IPMI_LOWER_NON_RECOVERABLE, IPMI_GOING_HIGH,
		IPMI_DEASSERTION, &val);
	if ((rv == 0) && (val != 0)) {
		sensor_info->support_deassert |= OHOI_THS_LCRTH;
	}


	rv = ipmi_sensor_threshold_event_supported(sensor,
		IPMI_UPPER_NON_CRITICAL, IPMI_GOING_LOW,
		IPMI_ASSERTION, &val);
	if ((rv == 0) && (val != 0)) {
		sensor_info->support_assert |= OHOI_THS_UMINL;
	}
	rv = ipmi_sensor_threshold_event_supported(sensor,
		IPMI_UPPER_NON_CRITICAL, IPMI_GOING_HIGH,
		IPMI_ASSERTION, &val);
	if ((rv == 0) && (val != 0)) {
		sensor_info->support_assert |= OHOI_THS_UMINH;
	}
	rv = ipmi_sensor_threshold_event_supported(sensor,
		IPMI_UPPER_NON_CRITICAL, IPMI_GOING_LOW,
		IPMI_DEASSERTION, &val);
	if ((rv == 0) && (val != 0)) {
		sensor_info->support_deassert |= OHOI_THS_UMINL;
	}
	rv = ipmi_sensor_threshold_event_supported(sensor,
		IPMI_UPPER_NON_CRITICAL, IPMI_GOING_HIGH,
		IPMI_DEASSERTION, &val);
	if ((rv == 0) && (val != 0)) {
		sensor_info->support_deassert |= OHOI_THS_UMINH;
	}

	rv = ipmi_sensor_threshold_event_supported(sensor,
		IPMI_UPPER_CRITICAL, IPMI_GOING_LOW,
		IPMI_ASSERTION, &val);
	if ((rv == 0) && (val != 0)) {
		sensor_info->support_assert |= OHOI_THS_UMAJL;
	}
	rv = ipmi_sensor_threshold_event_supported(sensor,
		IPMI_UPPER_CRITICAL, IPMI_GOING_HIGH,
		IPMI_ASSERTION, &val);
	if ((rv == 0) && (val != 0)) {
		sensor_info->support_assert |= OHOI_THS_UMAJH;
	}
	rv = ipmi_sensor_threshold_event_supported(sensor,
		IPMI_UPPER_CRITICAL, IPMI_GOING_LOW,
		IPMI_DEASSERTION, &val);
	if ((rv == 0) && (val != 0)) {
		sensor_info->support_deassert |= OHOI_THS_UMAJL;
	}
	rv = ipmi_sensor_threshold_event_supported(sensor,
		IPMI_UPPER_CRITICAL, IPMI_GOING_HIGH,
		IPMI_DEASSERTION, &val);
	if ((rv == 0) && (val != 0)) {
		sensor_info->support_deassert |= OHOI_THS_UMAJH;
	}

	rv = ipmi_sensor_threshold_event_supported(sensor,
		IPMI_UPPER_NON_RECOVERABLE, IPMI_GOING_LOW,
		IPMI_ASSERTION, &val);
	if ((rv == 0) && (val != 0)) {
		sensor_info->support_assert |= OHOI_THS_UCRTL;
	}
	rv = ipmi_sensor_threshold_event_supported(sensor,
		IPMI_UPPER_NON_RECOVERABLE, IPMI_GOING_HIGH,
		IPMI_ASSERTION, &val);
	if ((rv == 0) && (val != 0)) {
		sensor_info->support_assert |= OHOI_THS_UCRTH;
	}
	rv = ipmi_sensor_threshold_event_supported(sensor,
		IPMI_UPPER_NON_RECOVERABLE, IPMI_GOING_LOW,
		IPMI_DEASSERTION, &val);
	if ((rv == 0) && (val != 0)) {
		sensor_info->support_deassert |= OHOI_THS_UCRTL;
	}
	rv = ipmi_sensor_threshold_event_supported(sensor,
		IPMI_UPPER_NON_RECOVERABLE, IPMI_GOING_HIGH,
		IPMI_DEASSERTION, &val);
	if ((rv == 0) && (val != 0)) {
		sensor_info->support_deassert |= OHOI_THS_UCRTH;
	}
}

static SaHpiEventStateT convert_to_hpi_event_state(
					ipmi_sensor_t	*sensor,
					struct ohoi_sensor_info *sensor_info)
{
	unsigned int ast = sensor_info->support_assert;
	unsigned int dst = sensor_info->support_deassert;
	SaHpiEventStateT hst = 0;

	if(ipmi_sensor_get_event_reading_type(sensor) !=
			IPMI_EVENT_READING_TYPE_THRESHOLD) {
		return (SaHpiEventStateT)((ast | dst) & 0x7fff);
	}
	if ((ast | dst) & (OHOI_THS_LMINL | OHOI_THS_LMINH)) {
		hst |= SAHPI_ES_LOWER_MINOR;
	}
	if ((ast | dst) & (OHOI_THS_LMAJL | OHOI_THS_LMAJH)) {
		hst |= SAHPI_ES_LOWER_MAJOR;
	}
	if ((ast | dst) & (OHOI_THS_LCRTL | OHOI_THS_LCRTH)) {
		hst |= SAHPI_ES_LOWER_CRIT;
	}
	if ((ast | dst) & (OHOI_THS_UMINL | OHOI_THS_UMINH)) {
		hst |= SAHPI_ES_UPPER_MINOR;
	}
	if ((ast | dst) & (OHOI_THS_UMAJL | OHOI_THS_UMAJH)) {
		hst |= SAHPI_ES_UPPER_MAJOR;
	}
	if ((ast | dst) & (OHOI_THS_UCRTL | OHOI_THS_UCRTH)) {
		hst |= SAHPI_ES_UPPER_CRIT;
	}
	return hst;
}


static void add_sensor_event_sensor_rec(struct oh_handler_state *handler,
					ipmi_sensor_t	*sensor,
					SaHpiSensorRecT	*rec)
{

	rec->Type = (SaHpiSensorTypeT)ipmi_sensor_get_sensor_type(sensor);
	if (rec->Type >= 0xc0) {
		rec->Type = SAHPI_OEM_SENSOR;
	}
	rec->Category = ohoi_sensor_get_event_reading_type(sensor);
	rec->EventCtrl =
		(SaHpiSensorEventCtrlT)ipmi_sensor_get_event_support(sensor);
	add_sensor_event_data_format(sensor, rec);
	add_sensor_event_thresholds(sensor, rec);
	rec->Oem = ipmi_sensor_get_oem1(sensor);
}

static void add_sensor_event_rdr(struct oh_handler_state *handler,
				 ipmi_sensor_t		*sensor,
				 SaHpiRdrT		*rdr,
				 SaHpiEntityPathT	parent_ep,
				 SaHpiResourceIdT	res_id)
{
	char		name[SAHPI_MAX_TEXT_BUFFER_LENGTH];
	SaHpiTextTypeT	data_type;
	int		name_len;

	memset(name, '\0', SAHPI_MAX_TEXT_BUFFER_LENGTH);
	rdr->RecordId = 0;
	rdr->RdrType = SAHPI_SENSOR_RDR;
	rdr->Entity = parent_ep;

	add_sensor_event_sensor_rec(handler, sensor,
				&rdr->RdrTypeUnion.SensorRec);

	ipmi_sensor_get_id(sensor, name, SAHPI_MAX_TEXT_BUFFER_LENGTH);
	data_type = convert_to_hpi_data_type(ipmi_sensor_get_id_type(sensor));
	name_len = ipmi_sensor_get_id_length(sensor);
	if (name_len >= SAHPI_MAX_TEXT_BUFFER_LENGTH)
		name_len = SAHPI_MAX_TEXT_BUFFER_LENGTH - 1;
	rdr->IdString.DataType = data_type;
	rdr->IdString.Language = SAHPI_LANG_ENGLISH;
	rdr->IdString.DataLength = name_len;

	switch ( ipmi_sensor_get_event_support(sensor) ) {
		case IPMI_EVENT_SUPPORT_PER_STATE:
			rdr->RdrTypeUnion.SensorRec.EventCtrl =
							SAHPI_SEC_PER_EVENT;
			rdr->RdrTypeUnion.SensorRec.EnableCtrl = SAHPI_TRUE;
			break;
		case IPMI_EVENT_SUPPORT_ENTIRE_SENSOR:
			rdr->RdrTypeUnion.SensorRec.EventCtrl =
						SAHPI_SEC_READ_ONLY_MASKS;
			rdr->RdrTypeUnion.SensorRec.EnableCtrl = SAHPI_TRUE;
			break;

                case IPMI_EVENT_SUPPORT_GLOBAL_ENABLE:
		case IPMI_EVENT_SUPPORT_NONE:
                        rdr->RdrTypeUnion.SensorRec.EventCtrl =
						SAHPI_SEC_READ_ONLY;
			break;
	}

	memset(rdr->IdString.Data, 0, SAHPI_MAX_TEXT_BUFFER_LENGTH);
	memcpy(rdr->IdString.Data, name, name_len);
}



static void add_sensor_event(ipmi_entity_t	*ent,
			     ipmi_sensor_t	*sensor,
			     struct oh_handler_state *handler,
			     SaHpiRptEntryT	*rpt)
{
	struct ohoi_sensor_info *sensor_info;
	SaHpiRdrT		rdr;
	int lun, num;
	int rv;

	sensor_info = malloc(sizeof(*sensor_info));

	if (!sensor_info) {
	      	err("Out of memory for sensor info");
		return;
	}

	sensor_info->type = OHOI_SENSOR_ORIGINAL;
	sensor_info->ohoii.get_sensor_event_enable =
					orig_get_sensor_event_enable;
	sensor_info->ohoii.set_sensor_event_enable =
					orig_set_sensor_event_enable;
	sensor_info->ohoii.get_sensor_reading =
					orig_get_sensor_reading;
	sensor_info->ohoii.get_sensor_thresholds =
					orig_get_sensor_thresholds;
	sensor_info->ohoii.set_sensor_thresholds =
					orig_set_sensor_thresholds;

	sensor_info->info.orig_sensor_info.sensor_id  =
				ipmi_sensor_convert_to_id(sensor);
	sensor_info->sen_enabled = SAHPI_TRUE;
        sensor_info->enable = SAHPI_TRUE;
	add_sensor_states(sensor, sensor_info);

	memset(&rdr, 0, sizeof(rdr));
	rdr.RdrTypeUnion.SensorRec.Events =
		convert_to_hpi_event_state(sensor, sensor_info);

	rv = ipmi_sensor_get_num(sensor, &lun, &num);
	if(rv) {
		err("Error getting sensor number");
        	rdr.RdrTypeUnion.SensorRec.Num =
					SA_ERR_HPI_INVALID_DATA;
	} else {
	      rdr.RdrTypeUnion.SensorRec.Num = num;
	}

	add_sensor_event_rdr(handler, sensor, &rdr,
		rpt->ResourceEntity, rpt->ResourceId);

	adjust_sensor_to_atcahpi_spec(handler, rpt, &rdr,
				sensor_info, sensor);

	rv = oh_add_rdr(handler->rptcache, rpt->ResourceId,
				&rdr, sensor_info, 1);
	if (rv != SA_OK) {
		free(sensor_info);
		err("Failed to add sensor rdr");
	}
}

void ohoi_sensor_event(enum ipmi_update_e op,
                       ipmi_entity_t      *ent,
                       ipmi_sensor_t      *sensor,
                       void               *cb_data)
{
      	char			name[33];
	int			rv;


	ipmi_sensor_id_t sid = ipmi_sensor_convert_to_id(sensor);
	struct oh_handler_state *handler = cb_data;
	struct ohoi_handler *ipmi_handler = handler->data;
	struct ohoi_resource_info *res_info;
	ipmi_entity_id_t entity_id;
	SaHpiRptEntryT *rpt_entry;

	g_static_rec_mutex_lock(&ipmi_handler->ohoih_lock);
	ipmi_sensor_get_id(sensor, name, 32);

        entity_id = ipmi_entity_convert_to_id(ent);

        rpt_entry = ohoi_get_resource_by_entityid(handler->rptcache,
						  &entity_id);
        if (!rpt_entry) {
                dump_entity_id("Sensor without RPT Entry?!", entity_id);
		g_static_rec_mutex_unlock(&ipmi_handler->ohoih_lock);
                return;
        }

	res_info =  oh_get_resource_data(handler->rptcache,
						rpt_entry->ResourceId);

	switch (op) {
		case IPMI_ADDED:
			trace_ipmi_sensors("ADD", sid);
			rpt_entry->ResourceCapabilities |=
				 SAHPI_CAPABILITY_RDR | SAHPI_CAPABILITY_SENSOR;

	               	/* fill in the sensor data, add it to ipmi_event_list
			 * and finally to the rpt-cache
			 */
			add_sensor_event(ent, sensor, handler, rpt_entry);

			trace_ipmi("Sensor Added");

			if (ipmi_sensor_get_event_reading_type(sensor) ==
					IPMI_EVENT_READING_TYPE_THRESHOLD) {
			      	rv = ipmi_sensor_add_threshold_event_handler(
					sensor, sensor_threshold_event,
					handler);
			} else {
				if (IS_ATCA(ipmi_handler->d_type) &&
					(ipmi_sensor_get_sensor_type(sensor)
								== 0xF0)) {
					break;
				}
			      	rv = ipmi_sensor_add_discrete_event_handler(
					sensor, sensor_discrete_event, handler);
			}
			if (rv)
			      	err("Unable to reg sensor event handler: %#x\n",
					rv);
			break;
		case IPMI_CHANGED:
			trace_ipmi_sensors("CHANGED", sid);
			add_sensor_event(ent, sensor, handler, rpt_entry);
			dbg("Sensor Changed");
			break;
		case IPMI_DELETED:
			trace_ipmi_sensors("DELELE", sid);
			if (ohoi_delete_orig_sensor_rdr(handler,
							rpt_entry, &sid)) {
				// no more sensors for rpt
				rpt_entry->ResourceCapabilities &=
				 ~SAHPI_CAPABILITY_SENSOR;
			}
			if ((oh_get_rdr_next(handler->rptcache,
					 rpt_entry->ResourceId,
					 SAHPI_FIRST_ENTRY) == NULL) &&
					 (res_info->fru == NULL)) {
				// no more rdrs for rpt
				rpt_entry->ResourceCapabilities &=
				 ~SAHPI_CAPABILITY_RDR;
			}
			/* We don't do anything, if the entity is gone
			   infrastructre deletes the RDRs automatically
			*/
			break;
	}
	trace_ipmi("Set updated for resource %d . Sensor", rpt_entry->ResourceId);
	entity_rpt_set_updated(res_info, ipmi_handler);
	g_static_rec_mutex_unlock(&ipmi_handler->ohoih_lock);
}

/*
 * get_sensor_by_sensor_id_handler
 *
 * This is just callback to get ipmi_sensor_t from ipmi_sensor_id_t
 * and auxiliary structure to do it
 */



static void get_sensor_by_sensor_id_handler(ipmi_sensor_t *sensor,
					    void *cb_data)
{
	ipmi_entity_id_t    *entity_id = cb_data;
	if (sensor == NULL) {
		ipmi_entity_id_set_invalid(entity_id);
	}
	*entity_id = ipmi_entity_convert_to_id(
			ipmi_sensor_get_entity(sensor));
}


int ohoi_sensor_ipmi_event_to_hpi_event(
				struct ohoi_handler *ipmi_handler,
				ipmi_sensor_id_t sid,
				ipmi_event_t     *event,
				struct oh_event **e,
				ipmi_entity_id_t    *entity_id
				)
{
//	sens_info_t		info;
	enum ipmi_event_dir_e       dir;
	struct oh_event * ev = NULL;
	unsigned char	data[IPMI_EVENT_DATA_MAX_LEN];
	unsigned int	dt_len;
	int rv;

	dt_len = ipmi_event_get_data(event, data, 0, IPMI_EVENT_DATA_MAX_LEN);
	if (dt_len != 13) {
		err("Wrong size of ipmi event data = %i", dt_len);
		return 0;
	}

	rv = ipmi_sensor_pointer_cb(sid, get_sensor_by_sensor_id_handler,
					entity_id);
	if (rv) {
		err("no sensor for sensor_id rv = 0x%x", rv);
	}

	dir = data[9] >> 7;

	if ((data[9] & 0x7f) == IPMI_EVENT_READING_TYPE_THRESHOLD) {
		enum ipmi_thresh_e          threshold;
		enum ipmi_event_value_dir_e high_low;
		enum ipmi_value_present_e   value_present;
		unsigned int                raw_value;
		double                      value;

		threshold = (data[10] >> 1) & 0x07;
		high_low = data[10] & 1;
		raw_value = data[11];
		value = 0.0;
		value_present = IPMI_NO_VALUES_PRESENT;
		ev = sensor_threshold_map_event(dir, threshold, high_low,
		                   value_present, raw_value,  value, event);
	} else {
		int                   offset;
		int                   severity = 0;
		int                   prev_severity = 0;

		offset = data[10] & 0x0f;
		if ((data[10] >> 6) == 2) {
			severity = data[11] >> 4;
			prev_severity = data[11] & 0xf;
			 if (severity == 0xf) {
				severity = -1;
			}
			if (prev_severity == 0xf) {
				prev_severity = -1;
			}
		}
		ev = sensor_discrete_map_event(ipmi_handler,dir, offset,
					severity, prev_severity, event);
	}
	if (ev == NULL) {
		return 1;
	}

	if (ev->event.EventDataUnion.SensorEvent.SensorNum == 0) {
		ev->event.EventDataUnion.SensorEvent.SensorNum =
				data[8];
	}
	*e = ev;
	return 0;
}