File: ipmi_controls.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 (1157 lines) | stat: -rw-r--r-- 31,969 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
/*      -*- 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:
 *     Tariq Shureih    <tariq.shureih@intel.com>
 *     Louis Zhuang     <louis.zhuang@intel.com>
 */

 
#include "ipmi.h"

static unsigned char oem_alarm_state = 0xff;

struct ohoi_ctrl_info {
        int done;
	SaErrorT err;
        SaHpiRdrT * rdr;
        struct oh_handler_state *handler;
        SaHpiCtrlModeT mode;
        SaHpiCtrlStateT *state;
};

/* struct for getting power state */
struct ohoi_power_info {
	int done;
	SaErrorT err;
	SaHpiPowerStateT *state;
};

/* struct for getting reset state */
struct ohoi_reset_info {
	int done;
	SaErrorT err;
	SaHpiResetActionT *state;
};

static void __get_control_state(ipmi_control_t *control,
                                int            err,
                                int            *val,
                                void           *cb_data)
{
        struct ohoi_ctrl_info *info = cb_data;

	if (err || !val) {
		err("__get_control_state: err = %d; val = %p", err, val);
		info->err = SA_ERR_HPI_INTERNAL_ERROR;
		info->done = 1;
		return;
	}
       if (info->state->Type != SAHPI_CTRL_TYPE_OEM) {
                err("IPMI plug-in only support OEM control now");
		info->err = SA_ERR_HPI_INTERNAL_ERROR;
		info->done = 1;
                return;
        }
        
	if (err || !val) {
		err("__get_control_state: err = %d; val = %p", err, val);
		info->err = SA_ERR_HPI_INTERNAL_ERROR;
		info->done = 1;
		return;
	}
        info->state->StateUnion.Oem.BodyLength 
                = ipmi_control_get_num_vals(control);
        memcpy(&info->state->StateUnion.Oem.Body[0],
               val,
               info->state->StateUnion.Oem.BodyLength);
        info->done = 1;
}

static void __get_control_leds_state(ipmi_control_t *control,
                int err, ipmi_light_setting_t *settings, void *cb_data)
{
	struct ohoi_ctrl_info	*info = cb_data;
	int			len, res ,val;

	if (err) {
		err("__get_control_leds_state: err = %d", err);
		info->err = SA_ERR_HPI_INTERNAL_ERROR;
		info->done = 1;
		return;
	}
	if (info->state->Type != SAHPI_CTRL_TYPE_OEM) {
		err("IPMI plug-in only support OEM control now");
		info->err = SA_ERR_HPI_INTERNAL_ERROR;
		info->done = 1;
		return;
	}
			               
	if (settings == (ipmi_light_setting_t *)NULL) {
		err("__get_control_leds_state: settings = NULL");
		info->err = SA_ERR_HPI_INTERNAL_ERROR;
		info->done = 1;
		return;
	}
	len = ipmi_control_get_num_vals(control);
        info->state->StateUnion.Oem.BodyLength = len;
	res = ipmi_light_setting_get_color(settings, 0, &val);
        info->state->StateUnion.Oem.Body[0] = val;
        info->done = 1;
}



static void _get_control_state(ipmi_control_t *control,
                                void           *cb_data)
{
	if (ipmi_control_light_set_with_setting(control))
		ipmi_control_get_light(control, __get_control_leds_state, cb_data);
	else
        	ipmi_control_get_val(control, __get_control_state, cb_data);
}




		/*
		 *     ATCA LEDs
		 */

static void __get_atca_led(ipmi_control_t *control,
                	   int err,
			   ipmi_light_setting_t *st,
			   void *cb_data)
{
	struct ohoi_ctrl_info	*info = cb_data;
	SaHpiCtrlStateT *state = info->state;
	int lc;
	int on_time, off_time;
	int color;
	
	ipmi_light_setting_in_local_control(st, 0, &lc);
	info->mode = lc ? SAHPI_CTRL_MODE_AUTO : SAHPI_CTRL_MODE_MANUAL;
	if (state == NULL) {
		info->done = 1;
		return;
	}
	if (!ipmi_light_setting_get_on_time(st, 0, &on_time) &&
		!ipmi_light_setting_get_off_time(st, 0, &off_time)) {
		if (off_time > 10) {
			state->StateUnion.Oem.Body[0] = off_time / 10;
		} else {
			state->StateUnion.Oem.Body[0] = off_time ? 1 : 0;
		}
		if (on_time > 10) {
			state->StateUnion.Oem.Body[1] = on_time / 10;
		} else {
			state->StateUnion.Oem.Body[1] = on_time ? 1 : 0;
		}
	} else {
		err("couldn't get on/off times");
		info->err = SA_ERR_HPI_INVALID_DATA;
		info->done = 1;
	}
	if (ipmi_light_setting_get_color(st, 0, &color) == 0) {
		state->StateUnion.Oem.Body[2] = ohoi_atca_led_to_hpi_color(color);
		state->StateUnion.Oem.Body[3] = state->StateUnion.Oem.Body[2];
	} else {
		err("couldn't get color");
		info->err = SA_ERR_HPI_INVALID_DATA;
		info->done = 1;
	}
	state->StateUnion.Oem.Body[4] = 0;
	state->StateUnion.Oem.Body[5] = 0;
	state->StateUnion.Oem.MId = ATCAHPI_PICMG_MID;
	state->StateUnion.Oem.BodyLength = 6;
	state->Type = SAHPI_CTRL_TYPE_OEM;
	info->done = 1;
}

static void _get_atca_led(ipmi_control_t *control,
			  void *cb_data)
{
	struct ohoi_ctrl_info *info = cb_data;

	
	int rv;
	rv = ipmi_control_get_light(control, __get_atca_led, info);
	if (rv) {
		err("ipmi_control_get_light. rv = %d", rv);
		info->err = SA_ERR_HPI_INVALID_DATA;
		info->done = 1;
	}
}


SaErrorT orig_get_control_state(struct oh_handler_state *handler,
                                struct ohoi_control_info *c,
				SaHpiRdrT * rdr,
                                SaHpiCtrlModeT *mode,
                                SaHpiCtrlStateT *state)
{
	struct ohoi_handler *ipmi_handler = (struct ohoi_handler *)handler->data;
        struct ohoi_ctrl_info info;
	SaErrorT         rv;
	ipmi_control_id_t ctrl;
	SaHpiUint8T val, mask, idx, i;
	SaHpiCtrlStateT localstate;
	SaHpiCtrlModeT  localmode;

	ctrl = c->info.orig_ctrl_info.ctrl_id;

	if (state == NULL) {
		state = &localstate;
	}
	if (mode == NULL) {
		mode = &localmode;
	}
	if ((rdr->RdrTypeUnion.CtrlRec.Type == SAHPI_CTRL_TYPE_OEM) &&
            (rdr->RdrTypeUnion.CtrlRec.OutputType == SAHPI_CTRL_LED) &&
            (rdr->RdrTypeUnion.CtrlRec.TypeUnion.Oem.MId == ATCAHPI_PICMG_MID)) {
	    	/* This is ATCA led */
		info.done = 0;
		info.err = SA_OK;
		info.rdr = rdr;
		info.handler = handler;
		info.mode = 0;
		info.state = state;
		rv = ipmi_control_pointer_cb(ctrl, _get_atca_led, &info);
		if (rv) {
			err("ipmi_control_pointer_cb. rv = %d", rv);
			return SA_ERR_HPI_INVALID_DATA;
		}
		rv = ohoi_loop(&info.done, handler->data);
		if (rv != SA_OK) {
			err("ohoi_loop. rv = %d", rv);
			return rv;
		}
		if (info.err != SA_OK) {
			err("info.err = %d", info.err);
			return info.err;
		}
		*mode = info.mode;
		c->mode = info.mode;
		return SA_OK;
        }

	*mode = c->mode;
	memset(state, 0, sizeof(*state));
	
        info.done  = 0;
        info.state = state;
	info.err = SA_OK;
        info.state->Type = SAHPI_CTRL_TYPE_OEM;
        
        rv = ipmi_control_pointer_cb(ctrl, _get_control_state, &info);
	if (rv) {
		err("Unable to retrieve control state");
		return SA_ERR_HPI_ERROR;
	}

	rv = ohoi_loop(&info.done, ipmi_handler);
	if (rv != SA_OK) {
		return rv;
	}
	if (info.err != SA_OK) {
		return info.err;
	}
	val = info.state->StateUnion.Oem.Body[0];

	if ((rdr->RdrTypeUnion.CtrlRec.Type == SAHPI_CTRL_TYPE_DIGITAL) &&
	    (rdr->RdrTypeUnion.CtrlRec.OutputType == SAHPI_CTRL_LED) &&
	    (rdr->RdrTypeUnion.CtrlRec.Oem >= OEM_ALARM_BASE)) {
		oem_alarm_state = val;
		/* This is a front panel alarm LED. */
		state->Type = SAHPI_CTRL_TYPE_DIGITAL;	
		mask = 0x01;
		idx = rdr->RdrTypeUnion.CtrlRec.Oem - OEM_ALARM_BASE;
		/* bits 0 - 3 = Pwr, Crit, Maj, Min */
		for (i = 0; i < idx; i++) mask = mask << 1;
		if ((val & mask) == 0) 
		  state->StateUnion.Digital = SAHPI_CTRL_STATE_ON;
		else 
		  state->StateUnion.Digital = SAHPI_CTRL_STATE_OFF;
	} 
	return SA_OK;
}

SaErrorT ohoi_get_control_state(void *hnd, SaHpiResourceIdT id,
                                SaHpiCtrlNumT num,
                                SaHpiCtrlModeT *mode,
                                SaHpiCtrlStateT *state)
{
	struct oh_handler_state	*handler = (struct oh_handler_state *)hnd;
	SaErrorT		rv;
	struct ohoi_control_info *ctrl_info;
	SaHpiRdrT		*rdr;

	rdr = oh_get_rdr_by_type(handler->rptcache, id, SAHPI_CTRL_RDR, num);
	if (!rdr) return SA_ERR_HPI_INVALID_RESOURCE;
        rv = ohoi_get_rdr_data(hnd, id, SAHPI_CTRL_RDR, num, (void *)&ctrl_info);
        if (rv!=SA_OK) return rv;
	
	if (ctrl_info->ohoii.get_control_state == NULL) {
		return SA_ERR_HPI_INVALID_CMD;
	}
	
	return ctrl_info->ohoii.get_control_state(handler, ctrl_info, rdr, mode, state);	
}

static void __set_control_state(ipmi_control_t *control,
                                int            err,
                                void           *cb_data)
{
	struct ohoi_ctrl_info *info = cb_data;

	if (err) {
		err("__set_control_state: err = %d", err);
		info->err = SA_ERR_HPI_INTERNAL_ERROR;
	}
	info->done = 1;
}

static void _set_control_state(ipmi_control_t *control,
                                void           *cb_data)
{
        struct ohoi_ctrl_info *info = cb_data;
        
        if (info->state->StateUnion.Oem.BodyLength 
                        != ipmi_control_get_num_vals(control)) {
                err("control number is not equal to supplied data");
                info->done = -1;
		info->err = SA_ERR_HPI_INVALID_PARAMS;
                return;
        }
                        
	if (ipmi_control_light_set_with_setting(control)) {
		ipmi_light_setting_t *setting;

		setting = ipmi_alloc_light_settings(1);
		ipmi_light_setting_set_local_control(setting, 0, 1);
		ipmi_light_setting_set_color(setting, 0,
			info->state->StateUnion.Oem.Body[0]);
		ipmi_control_set_light(control, setting,
			__set_control_state, cb_data);
		ipmi_free_light_settings(setting);
	} else {
        	ipmi_control_set_val(control, 
	/* compile error */
//                             (int *)&info->state->StateUnion.Oem.Body[0],
                             (int *)(void *)&info->state->StateUnion.Oem.Body[0],
                             __set_control_state, info);
	}
}

static SaErrorT set_front_panrl_alarm_led(void *hnd,
                                	  struct ohoi_control_info *c,
					  SaHpiRdrT * rdr,
					  ipmi_control_id_t ctrl,
					  SaHpiUint8T idx,
					  SaHpiCtrlModeT mode,
					  SaHpiCtrlStateT *state)
{
	struct oh_handler_state *handler = (struct oh_handler_state *)hnd;
	struct ohoi_handler *ipmi_handler = (struct ohoi_handler *)handler->data;
        struct ohoi_ctrl_info info;
	SaErrorT         rv;
	SaHpiUint8T val, mask;
	SaHpiCtrlStateT my_state;


	
	/* just get the current control states */
	rv = orig_get_control_state(hnd, c, rdr, NULL, &my_state);
	if (rv != SA_OK) {
		return SA_ERR_HPI_NOT_PRESENT;
	}
	if (my_state.Type != state->Type) {
		return SA_ERR_HPI_INVALID_DATA;
	}
	val = oem_alarm_state;
	val |= 0xf0;  /* h.o. nibble always write 1 */
	state->Type = SAHPI_CTRL_TYPE_OEM;
	state->StateUnion.Oem.BodyLength = 1;
	state->StateUnion.Oem.Body[0] = val;
	mask = 0x01;
	/* bits 0 - 3 = Pwr, Crit, Maj, Min */
	mask = 1 << idx;
//	for (i = 0; i < idx; i++) mask = mask << 1;
        info.done  = 0;
        info.state = state;
	info.err = SA_OK;
	switch (state->StateUnion.Digital) {
	case SAHPI_CTRL_STATE_ON :
		if (!(val & mask)) { /* already turned on */
			return SA_OK;
		} 
		state->StateUnion.Oem.Body[0] = val & ~mask;
		break;
	case SAHPI_CTRL_STATE_OFF :
		if (val & mask) { /* already turned off */
			return SA_OK;
		} 
		state->StateUnion.Oem.Body[0] = val |mask;
		break;

	case SAHPI_CTRL_STATE_PULSE_ON :
		if (!(val & mask)) { /* already turned on */
			return SA_ERR_HPI_INVALID_REQUEST;
		}
		/* turn it on */
		state->StateUnion.Oem.Body[0] = val & ~mask;
        	rv = ipmi_control_pointer_cb(ctrl, _set_control_state, &info);
		if (rv) {
			err("Unable to set control state");
			return SA_ERR_HPI_ERROR;
		}
        	rv = ohoi_loop(&info.done, ipmi_handler);
		if (info.err != SA_OK) {
			err("Unable to set control state");
			return info.err;
		}
		if (rv != SA_OK) {
			err("Unable to set control state");
			return rv;
		}
		/* then turn it off. IPMI is slow, it provides us delay */
		state->StateUnion.Oem.Body[0] = val | mask;
		break;
	case SAHPI_CTRL_STATE_PULSE_OFF :
		if (val & mask) { /* already turned off */
			return SA_ERR_HPI_INVALID_REQUEST;
		}
		/* turn it off */
		state->StateUnion.Oem.Body[0] = val | mask;
        	rv = ipmi_control_pointer_cb(ctrl, _set_control_state, &info);
		if (rv) {
			err("Unable to set control state");
			return SA_ERR_HPI_ERROR;
		}
        	rv = ohoi_loop(&info.done, ipmi_handler);
		if (info.err != SA_OK) {
			err("Unable to set control state");
			return info.err;
		}
		if (rv != SA_OK) {
			err("Unable to set control state");
			return rv;
		}
		/* then turn it on. IPMI is slow, it provides us delay */
		state->StateUnion.Oem.Body[0] = val | mask;
		break;
	default :
		return SA_ERR_HPI_INVALID_PARAMS;
	}
	
        rv = ipmi_control_pointer_cb(ctrl, _set_control_state, &info);
	if (rv) {
		err("Unable to set control state");
		return SA_ERR_HPI_ERROR;
	}
        rv = ohoi_loop(&info.done, ipmi_handler);
	if (info.err != SA_OK) {
		err("Unable to set control state");
		return info.err;
	}
	if (rv != SA_OK) {
		err("Unable to set control state");
		return rv;
	}
	
	return SA_OK;
}





	/*
	 *      ATCA LED controls
	 */


static void __set_atca_led(ipmi_control_t *control,
                          int err,
			  ipmi_light_setting_t *st,
			  void *cb_data)
{
	struct ohoi_ctrl_info *info = cb_data;
	SaHpiUint8T *body;
	int lc = 0;
	int color = 0;
	int rv;
	
	ipmi_light_setting_in_local_control(st, 0, &lc);
	if (info->mode == SAHPI_CTRL_MODE_AUTO) {
		/* set MODE only */
		ipmi_light_setting_set_local_control(st, 0, 1);
	} else {
		body = &info->state->StateUnion.Oem.Body[0];
		color = ohoi_atca_led_to_ipmi_color(body[2]);
	
		ipmi_light_setting_set_local_control(st, 0, 0);
	
		rv = ipmi_light_setting_set_color(st, 0, color);
		if (rv) {
			err("ipmi_light_setting_set_color. rv = %d", rv);
		}
		rv = ipmi_light_setting_set_off_time(st, 0, body[0] * 10);
		if (rv) {
			err("ipmi_light_setting_set_off_time. rv = %d", rv);
		}
		rv = ipmi_light_setting_set_on_time(st, 0, body[1] * 10);
		if (rv) {
			err("ipmi_light_setting_set_on_time. rv = %d", rv);
		}
	}
	
	rv = ipmi_control_set_light(control, st,
			__set_control_state, info);
	if (rv) {
		err("ipmi_control_set_light = %d", rv);
		info->err = SA_ERR_HPI_INVALID_DATA;
		info->done = 1;
		return;
	}
}





static void _set_atca_led(ipmi_control_t *control,
			  void *cb_data)
{
	struct ohoi_ctrl_info *info = cb_data;

	
	int rv;
	rv = ipmi_control_get_light(control, __set_atca_led, info);
	if (rv) {
		err("ipmi_control_get_light. rv = %d", rv);
		info->err = SA_ERR_HPI_INVALID_DATA;
		info->done = 1;
	}
}

/**
 * is_supported_color
 * @hpi_color: HPI Color to test if supported
 * @rdr: RDR object containing control record for LED.
 *
 * Check if hpi_color is supported by the LED associated with rdr.
 *
 * Return value: 0 if color is supported, 1 otherwise.
 **/
static int is_supported_color(AtcaHpiLedColorT hpi_color, 
			      SaHpiRdrT *rdr)
{
	AtcaHpiColorCapabilitiesT supported_colors = rdr->RdrTypeUnion.CtrlRec.TypeUnion.Oem.ConfigData[0];

	switch(hpi_color)
	{
		case ATCAHPI_LED_COLOR_BLUE:
			return ((supported_colors & ATCAHPI_LED_BLUE) != 0);
		case ATCAHPI_LED_COLOR_RED:
			return ((supported_colors & ATCAHPI_LED_RED) != 0);
		case ATCAHPI_LED_COLOR_GREEN:
			return ((supported_colors & ATCAHPI_LED_GREEN) != 0);
		case ATCAHPI_LED_COLOR_AMBER:
			return ((supported_colors & ATCAHPI_LED_AMBER) != 0);
		case ATCAHPI_LED_COLOR_ORANGE:
			return ((supported_colors & ATCAHPI_LED_ORANGE) != 0);
		case ATCAHPI_LED_COLOR_WHITE:
			return ((supported_colors & ATCAHPI_LED_WHITE) != 0);
		case ATCAHPI_LED_COLOR_NO_CHANGE:
			return 1;
		case ATCAHPI_LED_COLOR_USE_DEFAULT:
			return 1;
		case ATCAHPI_LED_COLOR_RESERVED:
			return 0;
	}

	return 0;
}

SaErrorT orig_set_control_state(struct oh_handler_state *handler,
                                struct ohoi_control_info *c,
				SaHpiRdrT * rdr,
                                SaHpiCtrlModeT mode,
                                SaHpiCtrlStateT *state)
{
	struct ohoi_handler *ipmi_handler = (struct ohoi_handler *)handler->data;
        struct ohoi_ctrl_info info;
	SaErrorT         rv;
	ipmi_control_id_t ctrl;

	ctrl = c->info.orig_ctrl_info.ctrl_id;


	if ((rdr->RdrTypeUnion.CtrlRec.Type == SAHPI_CTRL_TYPE_OEM) &&
            (rdr->RdrTypeUnion.CtrlRec.OutputType == SAHPI_CTRL_LED) &&
            (rdr->RdrTypeUnion.CtrlRec.TypeUnion.Oem.MId == ATCAHPI_PICMG_MID)) {
	    	/* This is ATCA led */
		if (state != NULL) {
			if (state->StateUnion.Oem.MId != ATCAHPI_PICMG_MID) {
				err("state->StateUnion.Mid isn't ATCAHPI_PICMG_MID");
				return SA_ERR_HPI_INVALID_DATA;
			}
			if (state->StateUnion.Oem.BodyLength != 6) {
				err("state->StateUnion.Oem.BodyLength(%d) != 6",
					state->StateUnion.Oem.BodyLength);
				return SA_ERR_HPI_INVALID_DATA;
			}
			SaHpiUint8T *body = &state->StateUnion.Oem.Body[0];
			if ((body[2] == 0) || ((body[2] & (body[2] - 1)) != 0)) {
				/* exactly one color must be set */
				return SA_ERR_HPI_INVALID_DATA;
			}
			if (!is_supported_color(body[2], rdr)) {
				/* LED doesn't support this color */
				return SA_ERR_HPI_INVALID_DATA;
			}
		}
		info.done = 0;
		info.err = 0;
		info.rdr = rdr;
		info.handler = handler;
		info.mode = mode;
		info.state = state;
		rv = ipmi_control_pointer_cb(ctrl, _set_atca_led, &info);
		if (rv) {
			err("ipmi_control_pointer_cb. rv = %d", rv);
			return SA_ERR_HPI_INVALID_DATA;
		}
		rv = ohoi_loop(&info.done, handler->data);
		if (rv != SA_OK) {
			err("ohoi_loop. rv = %d", rv);
			return rv;
		}
		if (info.err != SA_OK) {
			err("info.err = %d", info.err);
			return info.err;
		}
		c->mode = mode;
		return SA_OK;
        }

	if (mode == SAHPI_CTRL_MODE_AUTO) {
		c->mode = mode;
		return SA_OK;
	}

	if ((rdr->RdrTypeUnion.CtrlRec.Type == SAHPI_CTRL_TYPE_DIGITAL) &&
            (rdr->RdrTypeUnion.CtrlRec.OutputType == SAHPI_CTRL_LED) &&
            (rdr->RdrTypeUnion.CtrlRec.Oem >= OEM_ALARM_BASE)) {
                /* This is a front panel alarm LED. */
		rv = set_front_panrl_alarm_led(handler, c, rdr, ctrl,
			rdr->RdrTypeUnion.CtrlRec.Oem - OEM_ALARM_BASE,
			mode, state);
		if (rv == SA_OK) {
			c->mode = mode;
		}
		return rv;
        }

	    
        info.done  = 0;
        info.state = state;
	info.err = SA_OK;
        if (info.state->Type != SAHPI_CTRL_TYPE_OEM) {
                err("IPMI only support OEM control");
                return SA_ERR_HPI_INVALID_CMD;
        }
       
        rv = ipmi_control_pointer_cb(ctrl, _set_control_state, &info);
	if (rv) {
		err("Unable to set control state");
		return SA_ERR_HPI_ERROR;
	}

        rv = ohoi_loop(&info.done, ipmi_handler);
	if (rv != SA_OK) {
		return rv;
	}
	if (info.err != SA_OK) {
		return info.err;
	}
	c->mode = mode;
	return SA_OK;
}	 


	/* interface function */

SaErrorT ohoi_set_control_state(void *hnd,
                                SaHpiResourceIdT id,
                                SaHpiCtrlNumT num,
                                SaHpiCtrlModeT mode,
                                SaHpiCtrlStateT *state)
{
	struct oh_handler_state		*handler =
						(struct oh_handler_state *)hnd;
	struct ohoi_control_info	*ctrl_info;
	SaErrorT			rv;
	SaHpiRdrT			*rdr;

	rdr = oh_get_rdr_by_type(handler->rptcache, id, SAHPI_CTRL_RDR, num);
	if (!rdr) return SA_ERR_HPI_INVALID_RESOURCE;
        rv = ohoi_get_rdr_data(hnd, id, SAHPI_CTRL_RDR, num, (void *)&ctrl_info);
        if (rv != SA_OK) return rv;
		
	if (rdr->RdrTypeUnion.CtrlRec.DefaultMode.ReadOnly &&
		rdr->RdrTypeUnion.CtrlRec.DefaultMode.Mode != mode) {
		err("Attempt to change mode of RO sensor mode");
		return SA_ERR_HPI_READ_ONLY;
	}

	if (ctrl_info->ohoii.set_control_state == NULL) {
		return SA_ERR_HPI_UNSUPPORTED_API;
	}
	
	rv = ctrl_info->ohoii.set_control_state(handler, ctrl_info, rdr, mode, state);
	
	return rv;

}

static void reset_resource_done (ipmi_control_t *ipmi_control,
				 int err,
				 void *cb_data)
{
	struct ohoi_reset_info *info = cb_data;
	info->done = 1;
	if (err) {
		err("reset_resource_done: err = %d", err);
		info->err = SA_ERR_HPI_INTERNAL_ERROR;
	}
}

static void set_resource_reset_state(ipmi_control_t *control,
                            void           *cb_data)
{
        struct ohoi_reset_info *info = cb_data;
	int val = 1;
	int rv;

        /* Just cold reset the entity*/
        rv = ipmi_control_set_val(control, &val, reset_resource_done, cb_data);
	if (rv) {
		err("ipmi_control_set_val returned err = %d", rv);
		info->err = SA_ERR_HPI_INTERNAL_ERROR;
		info->done = 1;
	}
}


static void reset_mc_done (ipmi_mc_t *mc,
			int err,
			void *cb_data)
{
	struct ohoi_reset_info *info = cb_data;
	info->done = 1;
	if (err) {
		err("reset_mc_done err = %d", err);
		info->err = SA_ERR_HPI_INTERNAL_ERROR;
	}
}

static void set_mc_reset_state(ipmi_mc_t *mc,
                            void           *cb_data)
{
        struct ohoi_reset_info *info = cb_data;
	int rv;
	int act;

	if (*info->state == SAHPI_COLD_RESET) {
		act = IPMI_MC_RESET_COLD;
	} else if (*info->state == SAHPI_WARM_RESET) {
		act = IPMI_MC_RESET_WARM;
	} else {
		info->err = SA_ERR_HPI_INVALID_CMD;
		info->done = 1;
		return;
	}		
        rv = ipmi_mc_reset(mc, act, reset_mc_done, cb_data);
	if (rv) {
		err("ipmi_mc_reset returned err = %d", rv);
		info->err = SA_ERR_HPI_INTERNAL_ERROR;
		info->done = 1;
	}
}



SaErrorT ohoi_set_reset_state(void *hnd, SaHpiResourceIdT id, 
		              SaHpiResetActionT act)
{
	struct oh_handler_state *handler = (struct oh_handler_state *)hnd;
	struct ohoi_handler *ipmi_handler = (struct ohoi_handler *)handler->data;
	struct ohoi_reset_info info;	
        struct ohoi_resource_info *ohoi_res_info;
        
	int rv;
        
	info.done = 0;
	info.err = 0;
	if ((act == SAHPI_COLD_RESET) || (act == SAHPI_WARM_RESET)) {
	      	dbg("ResetAction requested: %d", act);
	      	info.state = &act;
	} else {
	      	err("Currently we only support cold and warm reset");
		return SA_ERR_HPI_INVALID_CMD;
	}

        ohoi_res_info = oh_get_resource_data(handler->rptcache, id);
        if (!(ohoi_res_info->type & OHOI_RESOURCE_ENTITY)) {
                rv = ipmi_control_pointer_cb(ohoi_res_info->reset_ctrl, 
                                     set_resource_reset_state, &info);
	} else {
		rv = ipmi_mc_pointer_cb(ohoi_res_info->u.entity.mc_id, 
			set_mc_reset_state, &info);
	}

        if (rv) {
                err("Not support reset in the entity or mc");
                return SA_ERR_HPI_CAPABILITY;
        }

	/* wait until reset_done is called to exit this function */
	rv = ohoi_loop(&info.done, ipmi_handler);
	if ((rv == SA_OK) && (info.err == 0)) {
		return SA_OK;
	} else if (info.err) {
		return info.err;
	} else {
		return rv;
	}
}

static void power_done (ipmi_control_t *ipmi_control,
                        int err,
                        void *cb_data)
{
	
	struct ohoi_power_info *power_info = cb_data;

	power_info->done = 1;
	if (err) {
		err("power_done: err = %d", err);
		power_info->err = SA_ERR_HPI_INTERNAL_ERROR;
	}
}

static void set_power_state_on(ipmi_control_t *control,
                            void           *cb_data)
{
	struct ohoi_power_info *power_info = cb_data;
	int rv;

        rv = ipmi_control_set_val(control, (int *)power_info->state, power_done, cb_data);

	if (rv) {
		err("Failed to set control val (power on). rv = %d", rv);
		OHOI_MAP_ERROR(power_info->err, rv);
		power_info->done = 1;
	} else
	  	power_info->err = SA_OK;
}

static void set_power_state_off(ipmi_control_t *control,
                            void           *cb_data)
{
	struct ohoi_power_info *power_info = cb_data;
	int rv;

        rv = ipmi_control_set_val(control, (int *)power_info->state,
				  power_done, cb_data);
	if (rv) {
	  	err("Failed to set control val (power off). rv = %d", rv);
		OHOI_MAP_ERROR(power_info->err, rv);
		power_info->done = 1;
	} else
	  	power_info->err = SA_OK;
}

SaErrorT ohoi_set_power_state(void *hnd, SaHpiResourceIdT id, 
		              SaHpiPowerStateT state)
{
        struct ohoi_resource_info *ohoi_res_info;

	struct oh_handler_state *handler = (struct oh_handler_state *)hnd;
	struct ohoi_handler *ipmi_handler = (struct ohoi_handler *)handler->data;
	struct ohoi_power_info power_info;

        int rv;
	power_info.done = 0;
	power_info.state = &state;

        ohoi_res_info = oh_get_resource_data(handler->rptcache, id);
        if (!(ohoi_res_info->type & OHOI_RESOURCE_ENTITY)) {
                err("Not support power in MC");
                return SA_ERR_HPI_INVALID_CMD;
        }
	
	switch (state) {
		case SAHPI_POWER_ON:
			rv = ipmi_control_pointer_cb(ohoi_res_info->power_ctrl, 
						    set_power_state_on, &power_info);
			if (rv) {
				err("set_power_state_on failed");
				return SA_ERR_HPI_INTERNAL_ERROR;
			}
			break;
		case SAHPI_POWER_OFF:
			rv = ipmi_control_pointer_cb(ohoi_res_info->power_ctrl, 
                                                    set_power_state_off, &power_info);
	                if (rv) {
        	            	err("set_power_state_off failed");
                	    	return SA_ERR_HPI_INTERNAL_ERROR;
                	}
			break;
		case SAHPI_POWER_CYCLE:
			dbg("CYCLE power");
			SaHpiPowerStateT	cy_state = 0;
			cy_state = SAHPI_POWER_OFF;
			power_info.state = &cy_state;
			rv = ipmi_control_pointer_cb(ohoi_res_info->power_ctrl, 
						    set_power_state_off, &power_info);
			if (rv) {
				err("set_power_state_off failed");
				return SA_ERR_HPI_INTERNAL_ERROR;
			}
			rv = ohoi_loop(&power_info.done, ipmi_handler);
			if (rv != SA_OK) {
				err("ohopi_loop = 0x%x", rv);
				return rv;
			}
			dbg("CYCLE Stage 1: OK");

			if ((power_info.done) && (power_info.err == SA_OK)) {
			      dbg("CYCLE: done = %d , err = %d",
				  				power_info.done,
								power_info.err);
			      	cy_state = SAHPI_POWER_ON;
				power_info.state = &cy_state;
			      	power_info.done = 0;
			      	rv = ipmi_control_pointer_cb(ohoi_res_info->power_ctrl, 
							    set_power_state_on, &power_info);
				if (rv) {
				      	err("set_power_state_on failed");
					return SA_ERR_HPI_INTERNAL_ERROR;
				}
			}
			break;
		default:
			err("Invalid power state requested");
			return SA_ERR_HPI_INVALID_PARAMS;
	}

        rv = ohoi_loop(&power_info.done, ipmi_handler);
 	if (rv != SA_OK) {
		return rv;
	}       
        return power_info.err;
}

static void get_power_control_val (ipmi_control_t *control,
			     int err,
			     int *val,
			     void *cb_data)
{
	struct ohoi_power_info *info = cb_data;

	if (err || !val) {
		err("get_power_control_val: err = %d; val = %p", err, val);
		info->err = SA_ERR_HPI_INTERNAL_ERROR;
		info->done = 1;
		return;
	}
	if (*val == 0) {
		err("Power Control Value: %d", *val);
		*info->state = SAHPI_POWER_OFF;
		info->err = SA_OK;
	} else if (*val == 1) {
		err("Power Control Value: %d", *val);
		*info->state = SAHPI_POWER_ON;
		info->err = SA_OK;
	} else {
		err("invalid power state");
		info->err = SA_ERR_HPI_INTERNAL_ERROR;
	}
	info->done = 1;

}

static void get_power_state (ipmi_control_t *ipmi_control,
			     void *cb_data)
{
	struct ohoi_power_info *power_state = cb_data;
	int rv;

	rv = ipmi_control_get_val(ipmi_control, get_power_control_val, cb_data);

	if(rv) {
		err("[power]control_get_val failed. rv = %d", rv);
		power_state->err = SA_ERR_HPI_INTERNAL_ERROR;
		power_state->done = 1;
	}
}

SaErrorT ohoi_get_power_state (void *hnd,
                               SaHpiResourceIdT	id,
                               SaHpiPowerStateT *state)
{
        struct oh_handler_state *handler = (struct oh_handler_state *)hnd;
	struct ohoi_handler *ipmi_handler = (struct ohoi_handler *)handler->data;
        struct ohoi_resource_info *ohoi_res_info;
	struct ohoi_power_info power_state;

	int rv;

	power_state.done = 0;
	power_state.err = SA_OK;
	power_state.state = state;
	
        ohoi_res_info = oh_get_resource_data(handler->rptcache, id);
        if (!(ohoi_res_info->type & OHOI_RESOURCE_ENTITY)) {
                err("MC does not support power!");
                return SA_ERR_HPI_CAPABILITY;
        }

	rv = ipmi_control_pointer_cb(ohoi_res_info->power_ctrl,
					get_power_state, &power_state);
	if (rv) {
		err("get_power_state failed");
		return SA_ERR_HPI_INTERNAL_ERROR;
	}

	dbg("waiting for OIPMI to return");
	rv = ohoi_loop(&power_state.done, ipmi_handler);
	if (rv != SA_OK) {
		return rv;
	}	     
        return power_state.err;
}

static void get_reset_control_val (ipmi_control_t *control,
			     int err,
			     int *val,
			     void *cb_data)
{
	struct ohoi_reset_info *info = cb_data;

	if (err || !val) {
		err("get_reset_control_val: err = %d; val = %p", err, val);
		info->err = SA_ERR_HPI_INTERNAL_ERROR;
		info->done = 1;
		return;
	}
	if (*val == 0) {
		err("Reset Control Value: %d", *val);
		*info->state = SAHPI_RESET_DEASSERT;
		info->err = SA_OK;
	} else if (*val == 1) {
		err("Power Control Value: %d", *val);
		*info->state = SAHPI_RESET_ASSERT;
		info->err = SA_OK;
	} else {
	  	/* Note: IPMI platfroms don't hold reset state
		 * so we'll always return SAHPI_RESET_DEASSER
		 * this code is here just in case ;-)
		 */
		err("System does not support holding ResetState");
		*info->state = SAHPI_RESET_DEASSERT;
		info->err = SA_OK;
	}
	info->done = 1;
}

static
void get_reset_state(ipmi_control_t *control,
		     void *cb_data)
{
	struct ohoi_reset_info *reset_info = cb_data;

	int rv;

	rv = ipmi_control_get_val(control, get_reset_control_val, cb_data);
	if (rv) {
		//err("[reset] control_get_val failed. IPMI error = %i", rv);
		err("This IPMI system has a pulse reset, state is always DEASSERT");
		/* OpenIPMI will return an error for this call
		   since pulse resets do not support get_state
		   we will return UNSUPPORTED_API
		 */
		*reset_info->state = SAHPI_RESET_DEASSERT;
		reset_info->err = SA_OK;
		reset_info->done = 1;
	}
}
		    
SaErrorT ohoi_get_reset_state(void *hnd,
			      SaHpiResourceIdT id,
			      SaHpiResetActionT *act)
{
	struct oh_handler_state *handler = (struct oh_handler_state *)hnd;
	struct ohoi_handler *ipmi_handler = (struct ohoi_handler *)handler->data;
        struct ohoi_resource_info *ohoi_res_info;
        
	struct ohoi_reset_info reset_state;

	int rv;

	reset_state.done = 0;
	reset_state.err = 0;
	reset_state.state = act;

        ohoi_res_info = oh_get_resource_data(handler->rptcache, id);
        if (!(ohoi_res_info->type & OHOI_RESOURCE_ENTITY)) {
                err("Not support power in MC");
                return SA_ERR_HPI_CAPABILITY;
        }

	rv = ipmi_control_pointer_cb(ohoi_res_info->reset_ctrl,
				     get_reset_state, &reset_state);
	if(rv) {
		err("[reset_state] control pointer callback failed. rv = %d", rv);
		return SA_ERR_HPI_INVALID_CMD;
	}

	rv = ohoi_loop(&reset_state.done, ipmi_handler);
	if (rv != SA_OK) {
		return rv;
	}
	return reset_state.err;
}


void * oh_get_control_state (void *, SaHpiResourceIdT, SaHpiCtrlNumT,
                             SaHpiCtrlModeT *, SaHpiCtrlStateT *)
                __attribute__ ((weak, alias("ohoi_get_control_state")));

void * oh_set_control_state (void *, SaHpiResourceIdT,SaHpiCtrlNumT,
                             SaHpiCtrlModeT, SaHpiCtrlStateT *)
                __attribute__ ((weak, alias("ohoi_set_control_state")));

void * oh_get_power_state (void *, SaHpiResourceIdT, SaHpiPowerStateT *)
                __attribute__ ((weak, alias("ohoi_get_power_state")));

void * oh_set_power_state (void *, SaHpiResourceIdT, SaHpiPowerStateT)
                __attribute__ ((weak, alias("ohoi_set_power_state")));

void * oh_get_reset_state (void *, SaHpiResourceIdT, SaHpiResetActionT *)
                __attribute__ ((weak, alias("ohoi_get_reset_state")));

void * oh_set_reset_state (void *, SaHpiResourceIdT, SaHpiResetActionT)
                __attribute__ ((weak, alias("ohoi_set_reset_state")));