File: ipmi_sensor.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 (1417 lines) | stat: -rw-r--r-- 36,526 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
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
/*      -*- 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>
 *     Kevin Gao <kevin.gao@linux.intel.com>
 *     Rusty Lynch <rusty.lynch@linux.intel.com>
 *     Racing Guo <racing.guo@intel.com>
 */

#include "ipmi.h"

#define OHOI_TIMEOUT  10    /* 10 seconds, was 5 seconds */
struct ipmi_event_state_s
{
    unsigned int status;
    /* Pay no attention to the implementation. */
    unsigned int __assertion_events;
    unsigned int __deassertion_events;
};


/* 
 * Use for getting sensor reading
 */
struct ohoi_sensor_reading {
	SaHpiSensorReadingT	reading;
        SaHpiEventStateT        ev_state;
	int			done;
	int			rvalue;
};

/*
 * Use for getting/setting sensor threadholds
 */


struct ohoi_sensor_thresholds {
	SaHpiSensorThresholdsT	sensor_thres;
	ipmi_thresholds_t	*thrhlds;
	int			thres_done;
        int                     hyster_done;
	int			rvalue;
};


/*
 * Use for sensor event enable and sensor event masks
 */

struct ohoi_sensor_event_enable_masks {
	SaHpiBoolT   enable;
	SaHpiEventStateT  assert;
	SaHpiEventStateT  deassert;
	unsigned int	  a_support;
	unsigned int	  d_support;
	ipmi_event_state_t	*states;
	int done;
	int rvalue;
};

static int ignore_sensor(ipmi_sensor_t *sensor)
{
        ipmi_entity_t *ent;

        if (ipmi_sensor_get_ignore_if_no_entity(sensor)) {
		err("ignore if no entity");
                return 0;
	}
        ent = ipmi_sensor_get_entity(sensor);
	if (ent == NULL) {
		err("ipmi_sensor_get_entity = NULL");
		return 1;
	}
        if (!ipmi_entity_is_present(ent)) {
		err("!ipmi_entity_is_present. (%d,%d,%d,%d) %s",
		ipmi_entity_get_entity_id(ent), 
		ipmi_entity_get_entity_instance(ent),
		ipmi_entity_get_device_channel(ent),
		ipmi_entity_get_device_address(ent),
		ipmi_entity_get_entity_id_string(ent));
                return 0;
	}
        return 0;
}




		/*      GET SENSOR READING  */


static SaHpiEventStateT retrieve_states(ipmi_states_t *states)
{
	SaHpiEventStateT st = 0;
	int i;

	// fortunatly as discrete as threshold sensors IPMI states
	// is mapped 1:1 to HPI states
	for (i = 0; i < 15; i++) {
		if (ipmi_is_state_set(states, i)) {
			st |= (1 << i);
		}
	}
	return st;
}
	
static void sensor_read_states(ipmi_sensor_t *sensor,
			       int	   err,
			       ipmi_states_t *states,
			       void *cb_data)
{
	struct ohoi_sensor_reading *p = cb_data;

	p->done = 1;
	if (err) {
		err("sensor reading state error");
		p->rvalue = SA_ERR_HPI_INTERNAL_ERROR;
		return;
	}
	p->reading.IsSupported = SAHPI_FALSE;
	p->ev_state = retrieve_states(states);
}


static void sensor_reading(ipmi_sensor_t		*sensor,
		  	   int 				err,
			   enum ipmi_value_present_e	value_present,
			   unsigned int 		raw_val,
			   double 			val,
			   ipmi_states_t 		*states,
			   void 			*cb_data)
{
	struct ohoi_sensor_reading *p = cb_data;
 
	p->done = 1;

	if (err) {
		OHOI_MAP_ERROR(p->rvalue, err);
		err("sensor reading error");
		p->rvalue = SA_ERR_HPI_INTERNAL_ERROR;
		return;
	}

	p->reading.IsSupported = SAHPI_FALSE;

	if (value_present == IPMI_BOTH_VALUES_PRESENT) {
		p->reading.IsSupported = SAHPI_TRUE;
		p->reading.Type = SAHPI_SENSOR_READING_TYPE_FLOAT64;
		p->reading.Value.SensorFloat64 = val;
	} else if(value_present == IPMI_RAW_VALUE_PRESENT) {
		p->reading.IsSupported = SAHPI_TRUE;
		p->reading.Type = SAHPI_SENSOR_READING_TYPE_FLOAT64;
		p->reading.Value.SensorFloat64 = raw_val;
	} else {
		err("value present = 0x%x", value_present);
	}
	// always returns 1 in 7th bit. Ignore extra 1
	p->ev_state = retrieve_states(states) &
		(SAHPI_ES_LOWER_MINOR | SAHPI_ES_LOWER_MAJOR |
		 SAHPI_ES_LOWER_CRIT | SAHPI_ES_UPPER_MINOR |
		 SAHPI_ES_UPPER_MAJOR | SAHPI_ES_UPPER_CRIT);
}

static void get_sensor_reading(ipmi_sensor_t *sensor, void *cb_data)
{
	struct ohoi_sensor_reading *reading_data;	
	int rv;	

        reading_data = cb_data;
        
	if (ignore_sensor(sensor)) {
		reading_data->done = 1;
		reading_data->rvalue = SA_ERR_HPI_NOT_PRESENT;
		err("Sensor is not present, ignored");
		return;
	}

	if (ipmi_sensor_get_event_reading_type(sensor) ==
			 IPMI_EVENT_READING_TYPE_THRESHOLD) {
		rv = ipmi_sensor_get_reading(sensor, sensor_reading,
			reading_data);
		if (rv) {
			reading_data->done = 1;
			reading_data->rvalue = SA_ERR_HPI_INVALID_REQUEST;
			err("Unable to get sensor reading: 0x%x", rv);
				
		}
		return;
	}
	rv = ipmi_sensor_get_states(sensor, sensor_read_states, reading_data);
	if (rv) {
		reading_data->done = 1;
		reading_data->rvalue = SA_ERR_HPI_INVALID_REQUEST;
		err("Unable to get sensor reading states: 0x%x", rv);
	}

}


SaErrorT orig_get_sensor_reading(struct oh_handler_state *handler,
				 struct ohoi_sensor_info *sensor_info,
				 SaHpiSensorReadingT *reading,
				 SaHpiEventStateT *ev_state)
{
	struct ohoi_handler	*ipmi_handler =
					(struct ohoi_handler *)(handler->data);
	struct ohoi_sensor_reading	reading_data;	
        int				rv;
	ipmi_sensor_id_t		sensor_id;

	sensor_id = sensor_info->info.orig_sensor_info.sensor_id;

	memset(&reading_data, 0, sizeof(reading_data));

        rv = ipmi_sensor_pointer_cb(sensor_id, 
				    get_sensor_reading,
				    &reading_data);
	if (rv) {
		err("Unable to convert sensor_id to pointer");
		return SA_ERR_HPI_INVALID_CMD;
	}

	rv = ohoi_loop(&reading_data.done, ipmi_handler);

	if (rv)
		return rv;
	if (reading_data.rvalue)
		return reading_data.rvalue;

	*reading = reading_data.reading;
	*ev_state = reading_data.ev_state & 0x7fff;

	return SA_OK;
}

SaErrorT ohoi_get_sensor_reading(void *hnd,
				 struct ohoi_sensor_info *sensor_info,
				 SaHpiSensorReadingT *reading,
				 SaHpiEventStateT *ev_state)
{
	struct oh_handler_state *handler = (struct oh_handler_state *)hnd;

	if (sensor_info->ohoii.get_sensor_reading == NULL) {
		return SA_ERR_HPI_INVALID_CMD;
	}

	return sensor_info->ohoii.get_sensor_reading(handler,
					sensor_info, reading, ev_state);
}



		/*     GET SENSOR THRESHOLDS     */


static void thres_get(ipmi_sensor_t		*sensor,
		      ipmi_thresholds_t		*th,
		      unsigned int		event,
		      SaHpiSensorReadingT	*thres)
{
	int val;
	
	ipmi_sensor_threshold_readable(sensor, event, &val);
	if (!val) {
		thres->IsSupported = SAHPI_FALSE;
		return;
	}	
	if (0 == ipmi_threshold_get(th, event,
				&thres->Value.SensorFloat64)) {
		thres->IsSupported = SAHPI_TRUE;
		thres->Type = SAHPI_SENSOR_READING_TYPE_FLOAT64;
	}else {
		thres->IsSupported = SAHPI_FALSE;
	}
}

static void thresholds_read(ipmi_sensor_t	*sensor,
			    int			err,
			    ipmi_thresholds_t	*th,
			    void		*cb_data)
{
	struct ohoi_sensor_thresholds *p = cb_data;


	if (err) {
		OHOI_MAP_ERROR(p->rvalue, err);
		p->thres_done = 1;
		err("sensor thresholds reading error");
		return;
	}
	
	thres_get(sensor, th, IPMI_LOWER_NON_CRITICAL,
		  &p->sensor_thres.LowMinor);
	thres_get(sensor, th, IPMI_LOWER_CRITICAL,
		  &p->sensor_thres.LowMajor);
	thres_get(sensor, th, IPMI_LOWER_NON_RECOVERABLE,
		  &p->sensor_thres.LowCritical);
	thres_get(sensor, th, IPMI_UPPER_NON_CRITICAL,
		  &p->sensor_thres.UpMinor);
	thres_get(sensor, th, IPMI_UPPER_CRITICAL,
		  &p->sensor_thres.UpMajor);
	thres_get(sensor, th, IPMI_UPPER_NON_RECOVERABLE,
		  &p->sensor_thres.UpCritical);
	p->thres_done = 1;
}

static SaErrorT get_thresholds(ipmi_sensor_t	*sensor,
			       struct ohoi_sensor_thresholds	*thres_data)
{
	int		rv;

	rv = ipmi_sensor_get_thresholds(sensor, thresholds_read, thres_data);
	if (rv) 
		err("Unable to get sensor thresholds: 0x%x\n", rv);
	return (rv? SA_ERR_HPI_INVALID_CMD : SA_OK);
}

static void hysteresis_read(ipmi_sensor_t	*sensor,
			    int			err,
			    unsigned int	positive_hysteresis,
			    unsigned int 	negative_hysteresis,
			    void 		*cb_data)
{
	struct ohoi_sensor_thresholds *p = cb_data;

	if (err) {
		p->rvalue = SA_ERR_HPI_INTERNAL_ERROR;
		p->hyster_done = 1;
		err("sensor hysteresis reading error");
		return;		
	}

	p->sensor_thres.PosThdHysteresis.IsSupported = SAHPI_TRUE;
	p->sensor_thres.PosThdHysteresis.Type =
		SAHPI_SENSOR_READING_TYPE_FLOAT64;
	p->sensor_thres.PosThdHysteresis.Value.SensorFloat64 =
		positive_hysteresis;

	p->sensor_thres.NegThdHysteresis.IsSupported = SAHPI_TRUE;
        p->sensor_thres.NegThdHysteresis.Type =
		SAHPI_SENSOR_READING_TYPE_FLOAT64;
        p->sensor_thres.NegThdHysteresis.Value.SensorFloat64 =
		negative_hysteresis;
	p->hyster_done = 1;
}

static SaErrorT get_hysteresis(ipmi_sensor_t			*sensor,
			  struct ohoi_sensor_thresholds	*thres_data)
{
	int		rv;
	
	rv = ipmi_sensor_get_hysteresis(sensor, hysteresis_read, thres_data);
        if (rv)
                err("Unable to get sensor hysteresis: 0x%x\n", rv);
        
	return (rv? SA_ERR_HPI_INVALID_CMD : SA_OK);

}

static void get_sensor_thresholds(ipmi_sensor_t *sensor, 
                                  void          *cb_data)
{
	struct ohoi_sensor_thresholds *thres_data;
	int rv;
	
        thres_data = cb_data;

	if (ignore_sensor(sensor)) {
		thres_data->hyster_done = 1;
		thres_data->thres_done = 1;
		thres_data->rvalue = SA_ERR_HPI_NOT_PRESENT;
                err("ENTITY_NOT_PRESENT");
		return;
	}	

	if (ipmi_sensor_get_event_reading_type(sensor) !=
			IPMI_EVENT_READING_TYPE_THRESHOLD) {
		err("Not threshold sensor!");
		thres_data->hyster_done = 1;
		thres_data->thres_done = 1;
		thres_data->rvalue = SA_ERR_HPI_INVALID_CMD;
		return;
	}
	if (ipmi_sensor_get_threshold_access(sensor) ==
				IPMI_THRESHOLD_ACCESS_SUPPORT_NONE) {
		err("sensor doesn't support threshold read");
			err("Unable to get sensor thresholds");
			thres_data->hyster_done = 1;
			thres_data->thres_done = 1;
			thres_data->rvalue = SA_ERR_HPI_INVALID_CMD;
			return;
	}
	rv = get_thresholds(sensor, thres_data);
	if (rv != SA_OK) {
		err("Unable to get sensor thresholds");
		thres_data->hyster_done = 1;
		thres_data->thres_done = 1;
		thres_data->rvalue = rv;
		return;
	}

	rv = ipmi_sensor_get_hysteresis_support(sensor);

	if (rv != IPMI_HYSTERESIS_SUPPORT_READABLE &&
			rv != IPMI_HYSTERESIS_SUPPORT_SETTABLE) {
//		thres_data->thres_done = 1;
		thres_data->hyster_done = 1;
		thres_data->sensor_thres.PosThdHysteresis.IsSupported =
				SAHPI_FALSE;
		thres_data->sensor_thres.NegThdHysteresis.IsSupported =
				SAHPI_FALSE;
		return;
	} 
	rv = get_hysteresis(sensor, thres_data);
	if (rv != SA_OK) {
		err("failed to get hysteresis");
		thres_data->hyster_done = 1;
//		thres_data->thres_done = 1;
		thres_data->rvalue = SA_ERR_HPI_INTERNAL_ERROR;
		return;
	}

	return;
}

static int is_get_sensor_thresholds_done(const void *cb_data)
{
        const struct ohoi_sensor_thresholds *thres_data;
        
        thres_data = cb_data;
	/* Can we check the validity of this pointer here to avoid SegFault? */
        return (thres_data->thres_done && thres_data->hyster_done);
}

SaErrorT orig_get_sensor_thresholds(struct oh_handler_state *handler,
				    struct ohoi_sensor_info *sensor_info,
				    SaHpiSensorThresholdsT *thres)
{
	struct ohoi_handler	*ipmi_handler =
					(struct ohoi_handler *)(handler->data);
	struct ohoi_sensor_thresholds	thres_data;
        int				rv;
		ipmi_sensor_id_t		sensor_id;

	sensor_id = sensor_info->info.orig_sensor_info.sensor_id;

        memset(&thres_data, 0, sizeof(thres_data));
        
        rv = ipmi_sensor_pointer_cb(sensor_id,
                                    get_sensor_thresholds,
                                    &thres_data);
        if (rv) {
                err("Unable to convert sensor id into pointer");
		return SA_ERR_HPI_INVALID_CMD;
        }

        rv = ohoi_loop_until(is_get_sensor_thresholds_done, 
                               &thres_data, OHOI_TIMEOUT, ipmi_handler);

	if (rv) 
		return rv;
	
	if (thres_data.rvalue)
		return thres_data.rvalue;

	if (thres)
		*thres = thres_data.sensor_thres;
	return SA_OK;
}

SaErrorT ohoi_get_sensor_thresholds(void *hnd,
				    struct ohoi_sensor_info *sensor_info,
				    SaHpiSensorThresholdsT *thres)
{
	struct oh_handler_state *handler = (struct oh_handler_state *)hnd;

	if (sensor_info->ohoii.get_sensor_thresholds == NULL) {
		return SA_ERR_HPI_INVALID_CMD;
	}

	return sensor_info->ohoii.get_sensor_thresholds(handler,
							sensor_info, thres);
}




		/*      SET SENSOR THRESHOLDS  */


static void thres_set_data(ipmi_sensor_t *sensor, int err, void *cb_data)
{
	struct ohoi_sensor_thresholds *info = cb_data;
	if (err) {
		err("err = 0x%x", err);
		if (info->rvalue == SA_OK) {
			OHOI_MAP_ERROR(info->rvalue, err);
		}
	}
	info->thres_done = 1;
}


static void hys_set_data(ipmi_sensor_t *sensor, int err, void *cb_data)
{
	struct ohoi_sensor_thresholds *info = cb_data;
	if (err) {
		err("err = 0x%x", err);
		if (info->rvalue == SA_OK) {
			OHOI_MAP_ERROR(info->rvalue, err);
		}
	}
	info->hyster_done = 1;
}



static SaErrorT thres_cpy(ipmi_sensor_t			*sensor, 
		      const SaHpiSensorReadingT		reading,
		      unsigned int			event,
		      ipmi_thresholds_t			*info) 
{
	int	val;
	int	rv;

	if (!reading.IsSupported) {
		return SA_OK;
	}
	if ((rv = ipmi_sensor_threshold_settable(sensor, event, &val))) {
		err("ipmi_sensor_threshold_settable error = %d", rv);
		return SA_ERR_HPI_INVALID_CMD;
	}
	if (!val) {
		err("ipmi threshold 0x%x isn't settable", event);
		return SA_ERR_HPI_INVALID_DATA;
	}
	
	switch (reading.Type) {
		/*Fix Me* case...*/
		case  SAHPI_SENSOR_READING_TYPE_INT64:
		case  SAHPI_SENSOR_READING_TYPE_UINT64:
		case  SAHPI_SENSOR_READING_TYPE_BUFFER:
			break;
		case SAHPI_SENSOR_READING_TYPE_FLOAT64:
			if(ipmi_threshold_set(info, sensor, event, 
				reading.Value.SensorFloat64)) {
				return SA_OK;
			}
			break;
	}
	return SA_OK;
}

static SaErrorT init_thresholeds_info(
				ipmi_sensor_t			*sensor, 
				const SaHpiSensorThresholdsT	*thres,
				ipmi_thresholds_t		*info)
{
	SaErrorT rv;
	if (ipmi_thresholds_init(info)) {
		return SA_ERR_HPI_INTERNAL_ERROR;
	}
	rv = thres_cpy(sensor, thres->LowMinor, IPMI_LOWER_NON_CRITICAL, info);
	if (rv != SA_OK)
		return rv;
	
	rv = thres_cpy(sensor, thres->LowMajor, IPMI_LOWER_CRITICAL, info);
	if (rv != SA_OK)
		return rv;
	
	rv = thres_cpy(sensor, thres->LowCritical, IPMI_LOWER_NON_RECOVERABLE,
		       info);
	if (rv != SA_OK)
		return rv;
	
	rv = thres_cpy(sensor, thres->UpMinor, IPMI_UPPER_NON_CRITICAL, info);
	if (rv != SA_OK)
		return rv;
	
	rv = thres_cpy(sensor, thres->UpMajor, IPMI_UPPER_CRITICAL, info);
	if (rv != SA_OK)
		return rv;
	
	rv = thres_cpy(sensor, thres->UpCritical, IPMI_UPPER_NON_RECOVERABLE,
		       info);
	if (rv != SA_OK)
		return rv;

	return SA_OK;
}

static SaErrorT set_thresholds(ipmi_sensor_t                 *sensor, 
			  struct ohoi_sensor_thresholds *thres_data)
{
	ipmi_thresholds_t	*info = thres_data->thrhlds;
	int			rv;	
	

	rv = init_thresholeds_info(sensor, &thres_data->sensor_thres, info);
	if (rv != SA_OK) {
		err("Unable to init sensor thresholds: 0x%x\n", rv);
		return rv;
	}

	rv = ipmi_sensor_set_thresholds(sensor, info, thres_set_data, 
                                 thres_data);
	if (rv) {
		err("Unable to set sensor thresholds: 0x%x\n", rv);
		return SA_ERR_HPI_INTERNAL_ERROR;
	}

	return SA_OK;
}

static SaErrorT set_hysteresis(ipmi_sensor_t	                *sensor,
			  struct ohoi_sensor_thresholds	*thres_data)
{
	int			rv;	
	unsigned int		pos = 0, neg = 0;
	SaHpiSensorReadingT	pos_reading 
                = thres_data->sensor_thres.PosThdHysteresis;
	SaHpiSensorReadingT	neg_reading 
                = thres_data->sensor_thres.NegThdHysteresis;
	
        switch (pos_reading.Type) {
                case SAHPI_SENSOR_READING_TYPE_INT64:
                        pos = pos_reading.Value.SensorInt64;
                        break;
                case SAHPI_SENSOR_READING_TYPE_UINT64:
                        pos = pos_reading.Value.SensorUint64;
                        break;
                case SAHPI_SENSOR_READING_TYPE_FLOAT64:
                        pos = pos_reading.Value.SensorFloat64;
                        break;
                case SAHPI_SENSOR_READING_TYPE_BUFFER:
                        err("ipmi sensor doesn't support this type of reading");
                        return SA_ERR_HPI_INVALID_DATA;   
        }
	
        switch (neg_reading.Type) {
                case SAHPI_SENSOR_READING_TYPE_INT64:
                        neg = neg_reading.Value.SensorInt64;
                        break;
                case SAHPI_SENSOR_READING_TYPE_UINT64:
                        neg = neg_reading.Value.SensorUint64;
                        break;
                case SAHPI_SENSOR_READING_TYPE_FLOAT64:
                        neg = neg_reading.Value.SensorFloat64;
                        break;
                case SAHPI_SENSOR_READING_TYPE_BUFFER:
                        err("ipmi sensor doesn't support this type of reading");
                        return SA_ERR_HPI_INVALID_DATA;
        }


	rv = ipmi_sensor_set_hysteresis(sensor, pos, neg, hys_set_data, 
                                        thres_data);
	if (rv) {
		err("Unable to set sensor hysteresis: 0x%x\n", rv);
		return SA_ERR_HPI_INTERNAL_ERROR;
	}

        return SA_OK;
}

static void set_sensor_thresholds(ipmi_sensor_t *sensor, 
                                  void          *cb_data)
{
	struct ohoi_sensor_thresholds *thres_data;
	SaErrorT rv;	

	thres_data = cb_data;
	if (ignore_sensor(sensor)) {
		err("sensor is ignored");
		thres_data->hyster_done = 1;
		thres_data->thres_done = 1;
		thres_data->rvalue = SA_ERR_HPI_NOT_PRESENT;
		return;
	}

	if (ipmi_sensor_get_event_reading_type(sensor) !=
			IPMI_EVENT_READING_TYPE_THRESHOLD) {
		err("Not threshold sensor!");
		thres_data->hyster_done = 1;
		thres_data->thres_done = 1;
		thres_data->rvalue = SA_ERR_HPI_INVALID_CMD;
		return;
	}
				
	if ((ipmi_sensor_get_threshold_access(sensor) !=
				IPMI_THRESHOLD_ACCESS_SUPPORT_SETTABLE) ||
			(ipmi_sensor_get_hysteresis_support(sensor) !=
					IPMI_HYSTERESIS_SUPPORT_SETTABLE)) {
		err("sensor doesn't support threshold or histeresis set");
		thres_data->hyster_done = 1;
		thres_data->thres_done = 1;
		thres_data->rvalue = SA_ERR_HPI_INVALID_CMD;
		return;
	}
	rv = set_thresholds(sensor, thres_data);
	if (rv != SA_OK) {
		err("Unable to set thresholds");
		thres_data->hyster_done = 1;
		thres_data->thres_done = 1;
		thres_data->rvalue = rv;
		return;
	}	
 
	rv = set_hysteresis(sensor, thres_data);
	if (rv != SA_OK) {
		thres_data->hyster_done = 1;
		thres_data->thres_done = 1;
		thres_data->rvalue = rv;
		err("Unable to set hysteresis");
		return;
	}
	return;
}

SaErrorT orig_set_sensor_thresholds(struct oh_handler_state *handler,
				    struct ohoi_sensor_info *sensor_info,
				    const SaHpiSensorThresholdsT *thres)
{
	struct ohoi_handler	*ipmi_handler =
					(struct ohoi_handler *)(handler->data);
        struct ohoi_sensor_thresholds	thres_data;
        int				rv;
		ipmi_sensor_id_t		sensor_id;

	sensor_id = sensor_info->info.orig_sensor_info.sensor_id;

	memset(&thres_data, 0, sizeof(thres_data));
	thres_data.thrhlds = malloc(ipmi_thresholds_size()); 
	if (thres_data.thrhlds == NULL) {
		err("could not alloc memory");
		return SA_ERR_HPI_OUT_OF_MEMORY;
	}
       
	thres_data.sensor_thres = *thres;
        
        rv = ipmi_sensor_pointer_cb(sensor_id,
				    set_sensor_thresholds,
				    &thres_data);
		
        if (rv) {
		err("Unable to convert sensor_id to pointer");
		free(thres_data.thrhlds);
                return SA_ERR_HPI_INVALID_CMD;
	}

	rv = ohoi_loop_until(is_get_sensor_thresholds_done, 
                               &thres_data, OHOI_TIMEOUT, ipmi_handler);
	free(thres_data.thrhlds);
	if (rv != SA_OK) {
		return rv;
	}
	if (thres_data.rvalue) {
		return thres_data.rvalue;
	}
	return SA_OK;
}

SaErrorT ohoi_set_sensor_thresholds(void *hnd,
				    struct ohoi_sensor_info *sensor_info,
				    const SaHpiSensorThresholdsT *thres)
{
	struct oh_handler_state *handler = (struct oh_handler_state *)hnd;

	if (sensor_info->ohoii.set_sensor_thresholds == NULL) {
		return SA_ERR_HPI_INVALID_CMD;
	}

	return sensor_info->ohoii.set_sensor_thresholds(handler,
						sensor_info, thres);
}




		/*  SENSOR ENABLE  */


static void set_sensor_enable(ipmi_sensor_t *sensor,
			      void          *cb_data)
{
//	SaHpiBoolT *enable = cb_data;
//	ipmi_sensor_set_ignore_if_no_entity(sensor, *enable);
	return;

}

int ohoi_set_sensor_enable(ipmi_sensor_id_t sensor_id,
			   SaHpiBoolT   enable,
			   void *cb_data)
{
	SaErrorT rv;
        rv = ipmi_sensor_pointer_cb(sensor_id,
				    set_sensor_enable,
		  		    &enable);
	if (rv) {
		err("Unable to convert sensor_id to pointer");
                return SA_ERR_HPI_INVALID_CMD;
        }
	return SA_OK;
}



			/*    SENSOR EVENT ENABLE MASK    */
			

static void convert_to_ohoi_event_states(ipmi_sensor_t	*sensor,
			ipmi_event_state_t	*state,
			SaHpiEventStateT *assert,
			SaHpiEventStateT *deassert)
{
	int i;

	*assert = 0;
	*deassert = 0;
	if(ipmi_sensor_get_event_reading_type(sensor) !=
			IPMI_EVENT_READING_TYPE_THRESHOLD) {
		for (i = 0; i < 15; i++) {
			if (ipmi_is_discrete_event_set(state, i,
					IPMI_ASSERTION)) {
				*assert |= (1 << i);
			}
			if (ipmi_is_discrete_event_set(state, i,
					IPMI_DEASSERTION)) {
				*deassert |= (1 << i);
			}

		}
		return;
	}
	
	// threshold sensor
	if (ipmi_is_threshold_event_set(state,
			IPMI_LOWER_NON_CRITICAL, IPMI_GOING_LOW, 
			IPMI_ASSERTION)) {
		*assert |= SAHPI_ES_LOWER_MINOR;
	}
	if (ipmi_is_threshold_event_set(state,
			IPMI_LOWER_NON_CRITICAL, IPMI_GOING_HIGH, 
			IPMI_ASSERTION)) {
		*deassert |= SAHPI_ES_LOWER_MINOR;
	}
	if (ipmi_is_threshold_event_set(state,
			IPMI_LOWER_NON_CRITICAL, IPMI_GOING_LOW, 
			IPMI_DEASSERTION)) {
		*deassert |= SAHPI_ES_LOWER_MINOR;
	}	
	if (ipmi_is_threshold_event_set(state,
			IPMI_LOWER_NON_CRITICAL, IPMI_GOING_HIGH, 
			IPMI_DEASSERTION)) {
		*assert |= SAHPI_ES_LOWER_MINOR;
	}

	if (ipmi_is_threshold_event_set(state,
			IPMI_LOWER_CRITICAL, IPMI_GOING_LOW, 
			IPMI_ASSERTION)) {
		*assert |= SAHPI_ES_LOWER_MAJOR;
	}
	if (ipmi_is_threshold_event_set(state,
			IPMI_LOWER_CRITICAL, IPMI_GOING_HIGH, 
			IPMI_ASSERTION)) {
		*deassert |= SAHPI_ES_LOWER_MAJOR;
	}
	if (ipmi_is_threshold_event_set(state,
			IPMI_LOWER_CRITICAL, IPMI_GOING_LOW, 
			IPMI_DEASSERTION)) {
		*deassert |= SAHPI_ES_LOWER_MAJOR;
	}	
	if (ipmi_is_threshold_event_set(state,
			IPMI_LOWER_CRITICAL, IPMI_GOING_HIGH, 
			IPMI_DEASSERTION)) {
		*assert |= SAHPI_ES_LOWER_MAJOR;
	}

	if (ipmi_is_threshold_event_set(state,
			IPMI_LOWER_NON_RECOVERABLE, IPMI_GOING_LOW, 
			IPMI_ASSERTION)) {
		*assert |= SAHPI_ES_LOWER_CRIT;
	}
	if (ipmi_is_threshold_event_set(state,
			IPMI_LOWER_NON_RECOVERABLE, IPMI_GOING_HIGH, 
			IPMI_ASSERTION)) {
		*deassert |= SAHPI_ES_LOWER_CRIT;
	}
	if (ipmi_is_threshold_event_set(state,
			IPMI_LOWER_NON_RECOVERABLE, IPMI_GOING_LOW, 
			IPMI_DEASSERTION)) {
		*deassert |= SAHPI_ES_LOWER_CRIT;
	}	
	if (ipmi_is_threshold_event_set(state,
			IPMI_LOWER_NON_RECOVERABLE, IPMI_GOING_HIGH, 
			IPMI_DEASSERTION)) {
		*assert |= SAHPI_ES_LOWER_CRIT;
	}	


	if (ipmi_is_threshold_event_set(state,
			IPMI_UPPER_NON_CRITICAL, IPMI_GOING_LOW, 
			IPMI_ASSERTION)) {
		*deassert |= SAHPI_ES_UPPER_MINOR;
	}
	if (ipmi_is_threshold_event_set(state,
			IPMI_UPPER_NON_CRITICAL, IPMI_GOING_HIGH, 
			IPMI_ASSERTION)) {
		*assert |= SAHPI_ES_UPPER_MINOR;
	}
	if (ipmi_is_threshold_event_set(state,
			IPMI_UPPER_NON_CRITICAL, IPMI_GOING_LOW, 
			IPMI_DEASSERTION)) {
		*assert |= SAHPI_ES_UPPER_MINOR;
	}	
	if (ipmi_is_threshold_event_set(state,
			IPMI_UPPER_NON_CRITICAL, IPMI_GOING_HIGH, 
			IPMI_DEASSERTION)) {
		*deassert |= SAHPI_ES_UPPER_MINOR;
	}

	if (ipmi_is_threshold_event_set(state,
			IPMI_UPPER_CRITICAL, IPMI_GOING_LOW, 
			IPMI_ASSERTION)) {
		*deassert |= SAHPI_ES_UPPER_MAJOR;
	}
	if (ipmi_is_threshold_event_set(state,
			IPMI_UPPER_CRITICAL, IPMI_GOING_HIGH, 
			IPMI_ASSERTION)) {
		*assert |= SAHPI_ES_UPPER_MAJOR;
	}
	if (ipmi_is_threshold_event_set(state,
			IPMI_UPPER_CRITICAL, IPMI_GOING_LOW, 
			IPMI_DEASSERTION)) {
		*assert |= SAHPI_ES_UPPER_MAJOR;
	}	
	if (ipmi_is_threshold_event_set(state,
			IPMI_UPPER_CRITICAL, IPMI_GOING_HIGH, 
			IPMI_DEASSERTION)) {
		*deassert |= SAHPI_ES_UPPER_MAJOR;
	}

	if (ipmi_is_threshold_event_set(state,
			IPMI_UPPER_NON_RECOVERABLE, IPMI_GOING_LOW, 
			IPMI_ASSERTION)) {
		*deassert |= SAHPI_ES_UPPER_CRIT;
	}
	if (ipmi_is_threshold_event_set(state,
			IPMI_UPPER_NON_RECOVERABLE, IPMI_GOING_HIGH, 
			IPMI_ASSERTION)) {
		*assert |= SAHPI_ES_UPPER_CRIT;
	}
	if (ipmi_is_threshold_event_set(state,
			IPMI_UPPER_NON_RECOVERABLE, IPMI_GOING_LOW, 
			IPMI_DEASSERTION)) {
		*assert |= SAHPI_ES_UPPER_CRIT;
	}	
	if (ipmi_is_threshold_event_set(state,
			IPMI_UPPER_NON_RECOVERABLE, IPMI_GOING_HIGH, 
			IPMI_DEASSERTION)) {
		*deassert |= SAHPI_ES_UPPER_CRIT;
	}	

}

static void event_enable_masks_read(ipmi_sensor_t	*sensor,
			           int 			err,
			           ipmi_event_state_t	*state,
			           void			*cb_data)
{
	struct ohoi_sensor_event_enable_masks *p = cb_data;
	int 				rv;

	p->done = 1;

	if (err) {
		err("Sensor event enable reading error 0x%x", err);
			OHOI_MAP_ERROR(p->rvalue, err);
		return;
	}

	p->enable = SAHPI_FALSE;

	rv = ipmi_event_state_get_events_enabled(state);
        if (rv)
		p->enable = SAHPI_TRUE;

	convert_to_ohoi_event_states(sensor, state,
		&p->assert, &p->deassert);
}

static void get_sensor_event_enable_masks(ipmi_sensor_t *sensor, 
                                          void          *cb_data)
{
	struct ohoi_sensor_event_enable_masks *enable_data;
	int rv;
	
        enable_data = cb_data;

	if (ignore_sensor(sensor)) {
		err("sensor is ignored");
		enable_data->done = 1;
		enable_data->rvalue = SA_ERR_HPI_NOT_PRESENT;
		return;
	}	

	if ((ipmi_sensor_get_event_support(sensor) ==
			IPMI_EVENT_SUPPORT_PER_STATE)||
	   (ipmi_sensor_get_event_support(sensor) ==
				 IPMI_EVENT_SUPPORT_ENTIRE_SENSOR)){
		rv = ipmi_sensor_get_event_enables(sensor,
				event_enable_masks_read, enable_data);
		if (rv) {
			err("Unable to sensor event enable: 0x%x\n", rv);
			enable_data->rvalue = SA_ERR_HPI_INTERNAL_ERROR;
			return;
		}
	} else {
                err("Sensor do not support event");
		enable_data->assert = 0;
		enable_data->deassert = 0;
		enable_data->enable = SAHPI_FALSE;
		enable_data->rvalue = SA_OK;
		enable_data->done = 1;       
        }
}

static int insert_events_to_ipmi_event_state(
		ipmi_sensor_t		*sensor,
		ipmi_event_state_t	*state,
		SaHpiEventStateT	a_mask,
		SaHpiEventStateT	d_mask,
		unsigned int		a_sup,
		unsigned int		d_sup)
{
	int i;  

	if (ipmi_sensor_get_event_support(sensor) !=
			IPMI_EVENT_SUPPORT_PER_STATE) {
		return 0;
	}

	if (ipmi_sensor_get_event_reading_type(sensor) !=
			IPMI_EVENT_READING_TYPE_THRESHOLD) {
		// discrete sensor. map states 1:1
		if ((a_mask &~a_sup) || (d_mask & ~d_sup)) {
			return 1;
		}
		for (i = 0; i < 15; i++) {
			if (a_mask & (1 << i)) {
				ipmi_discrete_event_set(state, i,
					IPMI_ASSERTION);
			}
			if (d_mask & (1 << i)) {
				ipmi_discrete_event_set(state, i,
					IPMI_DEASSERTION);
			}
		}
		return 0;
	}

	// threhold sensor;
		// set assertion mask
	if (a_mask & SAHPI_ES_LOWER_MINOR) {
		if (a_sup & OHOI_THS_LMINL) {
			ipmi_threshold_event_set(state, IPMI_LOWER_NON_CRITICAL,
				IPMI_GOING_LOW, IPMI_ASSERTION);
		} else if (d_sup & OHOI_THS_LMINH) {
			ipmi_threshold_event_set(state, IPMI_LOWER_NON_CRITICAL,
				IPMI_GOING_HIGH, IPMI_DEASSERTION);
		} else {
			return 1;
		}
	}

	if (a_mask & SAHPI_ES_LOWER_MAJOR) {
		if (a_sup & OHOI_THS_LMAJL) {
			ipmi_threshold_event_set(state, IPMI_LOWER_CRITICAL,
				IPMI_GOING_LOW, IPMI_ASSERTION);
		} else if (d_sup & OHOI_THS_LMAJH) {
			ipmi_threshold_event_set(state, IPMI_LOWER_CRITICAL,
				IPMI_GOING_HIGH, IPMI_DEASSERTION);
		} else {
			return 1;
		}
	}

	if (a_mask & SAHPI_ES_LOWER_CRIT) {
		if (a_sup & OHOI_THS_LCRTL) {
			ipmi_threshold_event_set(state,
				IPMI_LOWER_NON_RECOVERABLE, IPMI_GOING_LOW,
				IPMI_ASSERTION);
		} else if (d_sup & OHOI_THS_LCRTH) {
			ipmi_threshold_event_set(state,
				IPMI_LOWER_NON_RECOVERABLE, IPMI_GOING_HIGH,
				IPMI_DEASSERTION);
		} else {
			return 1;
		}
	}


	if (a_mask & SAHPI_ES_UPPER_MINOR) {
		if (a_sup & OHOI_THS_UMINH) {
			ipmi_threshold_event_set(state,
				IPMI_UPPER_NON_CRITICAL, IPMI_GOING_HIGH,
				IPMI_ASSERTION);
		} else if (d_sup & OHOI_THS_UMINL) {
			ipmi_threshold_event_set(state,
				IPMI_UPPER_NON_CRITICAL, IPMI_GOING_LOW,
				IPMI_DEASSERTION);
		} else {
			return 1;
		}
	}

	if (a_mask & SAHPI_ES_UPPER_MAJOR) {
		if (a_sup & OHOI_THS_UMAJH) {
			ipmi_threshold_event_set(state, IPMI_UPPER_CRITICAL,
				IPMI_GOING_HIGH, IPMI_ASSERTION);
		} else if (d_sup & OHOI_THS_UMAJL) {
			ipmi_threshold_event_set(state, IPMI_UPPER_CRITICAL,
				IPMI_GOING_LOW, IPMI_DEASSERTION);
		} else {
			return 1;
		}
	}

	if (a_mask & SAHPI_ES_UPPER_CRIT) {
		if (a_sup & OHOI_THS_UCRTH) {
			ipmi_threshold_event_set(state,
				IPMI_UPPER_NON_RECOVERABLE, IPMI_GOING_HIGH,
				IPMI_ASSERTION);
		} else if (d_sup & OHOI_THS_UCRTL) {
			ipmi_threshold_event_set(state, IPMI_GOING_LOW,
				IPMI_UPPER_NON_RECOVERABLE, IPMI_DEASSERTION);
		} else {
			return 1;
		}
	}

		// set deassertion mask
	if (d_mask & SAHPI_ES_LOWER_MINOR) {
		if (d_sup & OHOI_THS_LMINL) {
			ipmi_threshold_event_set(state,
				IPMI_LOWER_NON_CRITICAL, IPMI_GOING_LOW,
				IPMI_DEASSERTION);
		} else if (a_sup & OHOI_THS_LMINH) {
			ipmi_threshold_event_set(state,
				IPMI_LOWER_NON_CRITICAL, IPMI_GOING_HIGH,
				IPMI_ASSERTION);
		} else {
			return 1;
		}
	}

	if (d_mask & SAHPI_ES_LOWER_MAJOR) {
		if (d_sup & OHOI_THS_LMAJL) {
			ipmi_threshold_event_set(state,
				IPMI_LOWER_CRITICAL, IPMI_GOING_LOW,
				IPMI_DEASSERTION);
		} else if (a_sup & OHOI_THS_LMAJH) {
			ipmi_threshold_event_set(state, IPMI_GOING_HIGH,
				IPMI_LOWER_CRITICAL, IPMI_ASSERTION);
		} else {
			return 1;
		}
	}

	if (d_mask & SAHPI_ES_LOWER_CRIT) {
		if (d_sup & OHOI_THS_LCRTL) {
			ipmi_threshold_event_set(state,
				IPMI_LOWER_NON_RECOVERABLE, IPMI_GOING_LOW,
				IPMI_DEASSERTION);
		} else if (a_sup & OHOI_THS_LCRTH) {
			ipmi_threshold_event_set(state,
				IPMI_LOWER_NON_RECOVERABLE, IPMI_GOING_HIGH,
				IPMI_ASSERTION);
		} else {
			return 1;
		}
	}


	if (d_mask & SAHPI_ES_UPPER_MINOR) {
		if (d_sup & OHOI_THS_UMINH) {
			ipmi_threshold_event_set(state,
				IPMI_UPPER_NON_CRITICAL, IPMI_GOING_HIGH,
				IPMI_DEASSERTION);
		} else if (a_sup & OHOI_THS_UMINL) {
			ipmi_threshold_event_set(state,
				IPMI_UPPER_NON_CRITICAL, IPMI_GOING_LOW,
				IPMI_ASSERTION);
		} else {
			return 1;
		}
	}

	if (d_mask & SAHPI_ES_UPPER_MAJOR) {
		if (d_sup & OHOI_THS_UMAJH) {
			ipmi_threshold_event_set(state,
				IPMI_UPPER_CRITICAL, IPMI_GOING_HIGH,
				IPMI_DEASSERTION);
		} else if (a_sup & OHOI_THS_UMAJL) {
			ipmi_threshold_event_set(state, IPMI_GOING_LOW,
				IPMI_UPPER_CRITICAL, IPMI_ASSERTION);
		} else {
			return 1;
		}
	}

	if (d_mask & SAHPI_ES_UPPER_CRIT) {
		if (d_sup & OHOI_THS_UCRTH) {
			ipmi_threshold_event_set(state,
				IPMI_UPPER_NON_RECOVERABLE, IPMI_GOING_HIGH,
				IPMI_DEASSERTION);
		} else if (a_sup & OHOI_THS_UCRTL) {
			ipmi_threshold_event_set(state,
				IPMI_UPPER_NON_RECOVERABLE, IPMI_GOING_LOW,
				IPMI_ASSERTION);
		} else {
			return 1;
		}
	}

	return 0;
}


static void mask_set_data(ipmi_sensor_t *sensor, int err, void *cb_data)
{
	struct ohoi_sensor_event_enable_masks *info = cb_data;
	if (err) {
		err("err = 0x%x", err);
		info->rvalue = SA_ERR_HPI_INVALID_CMD;
	}
	info->done = 1;
}
				
static void set_sensor_event_enable_masks(ipmi_sensor_t      *sensor,
                                    void               *cb_data)
{
	struct ohoi_sensor_event_enable_masks *enable_data = cb_data;
	int			rv;
	ipmi_event_state_t	*info = enable_data->states;

	if (ignore_sensor(sensor)) {
		err("sensor is ignored");
		enable_data->done = 1;
		enable_data->rvalue = SA_ERR_HPI_NOT_PRESENT;
		return;
	}

	ipmi_event_state_init(info);
	ipmi_event_state_set_events_enabled(info,
		(enable_data->enable == SAHPI_TRUE) ? 1 : 0);
	if (insert_events_to_ipmi_event_state(sensor, info,
			enable_data->assert, enable_data->deassert,
			enable_data->a_support, enable_data->d_support)) {
		err("Attempt to set not supported event 0x%x/0x%x",
			enable_data->assert, enable_data->deassert);
		enable_data->done = 1;
		enable_data->rvalue = SA_ERR_HPI_INVALID_DATA;
		return;
	}


	rv = ipmi_sensor_set_event_enables(sensor, info, mask_set_data,
					   enable_data);
	if (rv) {
		err("Unable to sensor event enable = %d", rv);
		enable_data->done = 1;
		if (rv == EINVAL) {
			// invalid event in mask for this sensor */
			enable_data->rvalue = SA_ERR_HPI_INVALID_DATA;
		} else {	
			enable_data->rvalue = SA_ERR_HPI_INTERNAL_ERROR;
		}
	}
}

SaErrorT orig_get_sensor_event_enable(struct oh_handler_state *handler,
				      struct ohoi_sensor_info *sensor_info,
				      SaHpiBoolT   *enable,
				      SaHpiEventStateT  *assert,
				      SaHpiEventStateT  *deassert)
{
        SaErrorT		rv;
	struct ohoi_handler	*ipmi_handler =
					(struct ohoi_handler *)(handler->data);
	struct ohoi_sensor_event_enable_masks	enable_data;
		ipmi_sensor_id_t		sensor_id;

	sensor_id = sensor_info->info.orig_sensor_info.sensor_id;
        
	memset(&enable_data, 0, sizeof(enable_data));
        
        rv = ipmi_sensor_pointer_cb(sensor_id,
				    get_sensor_event_enable_masks,
				    &enable_data);
        if (rv) {
		err("Unable to convert sensor_id to pointer");
                return SA_ERR_HPI_INVALID_CMD;
        }
        
        rv = ohoi_loop(&enable_data.done, ipmi_handler);

	if (rv)
		return rv;
	if (enable_data.rvalue)
		return enable_data.rvalue;

	*enable = enable_data.enable;
	*assert = enable_data.assert & 0x7fff;
	*deassert = enable_data.deassert & 0x7fff;

	return SA_OK;

}


SaErrorT ohoi_get_sensor_event_enable(void *hnd,
				      struct ohoi_sensor_info *sensor_info,
				      SaHpiBoolT   *enable,
				      SaHpiEventStateT  *assert,
				      SaHpiEventStateT  *deassert)
{
	struct oh_handler_state *handler = (struct oh_handler_state *)hnd;

	if (sensor_info->ohoii.get_sensor_event_enable == NULL) {
		return SA_ERR_HPI_INVALID_CMD;
	}

	return sensor_info->ohoii.get_sensor_event_enable(handler,
					sensor_info, enable,
					assert, deassert);
}

SaErrorT orig_set_sensor_event_enable(struct oh_handler_state *handler,
				      struct ohoi_sensor_info *sensor_info,
				      SaHpiBoolT enable,
				      SaHpiEventStateT assert,
				      SaHpiEventStateT deassert,
				      unsigned int a_supported,
				      unsigned int d_supported)
{
        int			rv;
	struct ohoi_handler	*ipmi_handler =
					(struct ohoi_handler *)(handler->data);
	struct ohoi_sensor_event_enable_masks	enable_data;
		ipmi_sensor_id_t		sensor_id;

	sensor_id = sensor_info->info.orig_sensor_info.sensor_id;

	memset(&enable_data, 0, sizeof(enable_data));
	enable_data.states = malloc(ipmi_event_state_size());
	if (enable_data.states == NULL) {
		err("out of memory");
		return SA_ERR_HPI_OUT_OF_MEMORY;
	}
	enable_data.enable = enable;
	enable_data.assert = assert;
	enable_data.deassert = deassert;
	enable_data.a_support = a_supported;
	enable_data.d_support = d_supported;
	rv = ipmi_sensor_pointer_cb(sensor_id,
				    set_sensor_event_enable_masks,
		  		    &enable_data);
	if (rv) {
		err("Unable to convert sensor_id to pointer");
		free(enable_data.states);
		return SA_ERR_HPI_INVALID_CMD;
	}       
	rv = ohoi_loop(&enable_data.done, ipmi_handler);
	free(enable_data.states);      
	if (rv) {
		return rv;
	}
	if (enable_data.rvalue)
		return enable_data.rvalue;

	return SA_OK;
}


SaErrorT ohoi_set_sensor_event_enable(void *hnd,
				      struct ohoi_sensor_info *sensor_info,
				      SaHpiBoolT enable,
				      SaHpiEventStateT assert,
				      SaHpiEventStateT deassert,
				      unsigned int a_supported,
				      unsigned int d_supported)
{
	struct oh_handler_state *handler = (struct oh_handler_state *)hnd;

	if (sensor_info->ohoii.set_sensor_event_enable == NULL) {
		return SA_ERR_HPI_INVALID_CMD;
	}

	return sensor_info->ohoii.set_sensor_event_enable(handler,
					sensor_info, enable,
					assert, deassert,
					a_supported, d_supported);
}