summaryrefslogtreecommitdiff
path: root/lib/ipmi_vita.c
blob: 39009743973e79f3f10ed15300faff1f7de1367c (plain)
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
/*
 * Copyright (c) 2014 Pigeon Point Systems. All right reserved
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * Redistribution of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *
 * Redistribution in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 *
 * Neither the name of Pigeon Point Systems, or the names of
 * contributors may be used to endorse or promote products derived
 * from this software without specific prior written permission.
 *
 * This software is provided "AS IS, " without a warranty of any kind.
 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED.
 * PIGEON POINT SYSTEMS ("PPS") AND ITS LICENSORS SHALL NOT BE LIABLE
 * FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
 * OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.  IN NO EVENT WILL
 * PPS OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA,
 * OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
 * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
 * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
 * EVEN IF PPS HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 */


#include <ipmitool/ipmi_intf.h>
#include <ipmitool/ipmi_picmg.h>
#include <ipmitool/ipmi_vita.h>
#include <ipmitool/ipmi_fru.h>
#include <ipmitool/ipmi_strings.h>
#include <ipmitool/log.h>

/* Handled VITA 46.11 commands */
#define VITA_CMD_HELP		0
#define VITA_CMD_PROPERTIES	1
#define VITA_CMD_FRUCONTROL	2
#define VITA_CMD_ADDRINFO	3
#define VITA_CMD_ACTIVATE	4
#define VITA_CMD_DEACTIVATE	5
#define VITA_CMD_POLICY_GET	6
#define VITA_CMD_POLICY_SET	7
#define VITA_CMD_LED_PROP	8
#define VITA_CMD_LED_CAP	9
#define VITA_CMD_LED_GET	10
#define VITA_CMD_LED_SET	11
#define VITA_CMD_UNKNOWN	255

/* VITA 46.11 Site Type strings */
static struct valstr vita_site_types[] = {
	{ VITA_FRONT_VPX_MODULE, "Front Loading VPX Plug-In Module" },
	{ VITA_POWER_ENTRY, "Power Entry Module" },
	{ VITA_CHASSIS_FRU, "Chassic FRU Information Module" },
	{ VITA_DEDICATED_CHMC, "Dedicated Chassis Manager" },
	{ VITA_FAN_TRAY, "Fan Tray" },
	{ VITA_FAN_TRAY_FILTER, "Fan Tray Filter" },
	{ VITA_ALARM_PANEL, "Alarm Panel" },
	{ VITA_XMC, "XMC" },
	{ VITA_VPX_RTM, "VPX Rear Transition Module" },
	{ VITA_FRONT_VME_MODULE, "Front Loading VME Plug-In Module" },
	{ VITA_FRONT_VXS_MODULE, "Front Loading VXS Plug-In Module" },
	{ VITA_POWER_SUPPLY, "Power Supply" },
	{ VITA_FRONT_VITA62_MODULE, "Front Loading VITA 62 Module\n" },
	{ VITA_71_MODULE, "VITA 71 Module\n" },
	{ VITA_FMC, "FMC\n" },
	{ 0, NULL }
};

/* VITA 46.11 command help strings */
static struct valstr vita_help_strings[] = {
	{
		VITA_CMD_HELP,
		"VITA commands:\n"
		"    properties        - get VSO properties\n"
		"    frucontrol        - FRU control\n"
		"    addrinfo          - get address information\n"
		"    activate          - activate a FRU\n"
		"    deactivate        - deactivate a FRU\n"
		"    policy get        - get the FRU activation policy\n"
		"    policy set        - set the FRU activation policy\n"
		"    led prop          - get led properties\n"
		"    led cap           - get led color capabilities\n"
		"    led get           - get led state\n"
		"    led set           - set led state"
	},
	{
		VITA_CMD_FRUCONTROL,
		"usage: frucontrol <FRU-ID> <OPTION>\n"
		"    OPTION: 0 - Cold Reset\n"
		"            1 - Warm Reset\n"
		"            2 - Graceful Reboot\n"
		"            3 - Issue Diagnostic Interrupt"
	},
	{
		VITA_CMD_ADDRINFO,
		"usage: addrinfo [<FRU-ID>]"
	},
	{
		VITA_CMD_ACTIVATE,
		"usage: activate <FRU-ID>"
	},
	{
		VITA_CMD_DEACTIVATE,
		"usage: deactivate <FRU-ID>"
	},
    	{
		VITA_CMD_POLICY_GET,
		"usage: policy get <FRU-ID>"
	},
	{
		VITA_CMD_POLICY_SET,
		"usage: policy set <FRU-ID> <MASK> <VALUE>\n"
		"    MASK:  [3] affect the Default-Activation-Locked Policy Bit\n"
		"           [2] affect the Commanded-Deactivation-Ignored Policy Bit\n"
		"           [1] affect the Deactivation-Locked Policy Bit\n"
		"           [0] affect the Activation-Locked Policy Bit\n"
		"    VALUE: [3] value for the Default-Activation-Locked Policy Bit\n"
		"           [2] value for the Commanded-Deactivation-Ignored Policy Bit\n"
		"           [1] value for the Deactivation-Locked Policy Bit\n"
		"           [0] value for the Activation-Locked Policy Bit"
	},
	{
		VITA_CMD_LED_PROP,
		"usage: led prop <FRU-ID>"
	},
	{
		VITA_CMD_LED_CAP,
		"usage: led cap <FRU-ID> <LED-ID"
	},
	{
		VITA_CMD_LED_GET,
		"usage: led get <FRU-ID> <LED-ID",
	},
	{
		VITA_CMD_LED_SET,
		"usage: led set <FRU-ID> <LED-ID> <FUNCTION> <DURATION> <COLOR>\n"
		"    <FRU-ID>\n"
		"    <LED-ID>   0-0xFE:    Specified LED\n"
		"               0xFF:      All LEDs under management control\n"
		"    <FUNCTION> 0:       LED OFF override\n"
		"               1 - 250: LED blinking override (off duration)\n"
		"               251:     LED Lamp Test\n"
		"               252:     LED restore to local control\n"
		"               255:     LED ON override\n"
		"    <DURATION> 1 - 127: LED Lamp Test / on duration\n"
		"    <COLOR>    1:   BLUE\n"
		"               2:   RED\n"
		"               3:   GREEN\n"
		"               4:   AMBER\n"
		"               5:   ORANGE\n"
		"               6:   WHITE\n"
		"               0xE: do not change\n"
		"               0xF: use default color"
	},
	{
		VITA_CMD_UNKNOWN,
		"Unknown command"
	},
	{ 0, NULL }
};

/* check if VITA 46.11 is supported */
uint8_t
vita_discover(struct ipmi_intf *intf)
{
	struct ipmi_rq req;
	struct ipmi_rs *rsp;
	unsigned char msg_data;
	int vita_avail = 0;

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

	req.msg.netfn = IPMI_NETFN_PICMG;
	req.msg.cmd = VITA_GET_VSO_CAPABILITIES_CMD;
	req.msg.data = &msg_data;
	req.msg.data_len = 1;

	msg_data = GROUP_EXT_VITA;

	lprintf(LOG_INFO, "Running Get VSO Capabilities my_addr %#x, "
		"transit %#x, target %#x",
		intf->my_addr, intf->transit_addr, intf->target_addr);

	rsp = intf->sendrecv(intf, &req);

	if (rsp == NULL) {
		lprintf(LOG_ERR, "No valid response received");
	} else if (rsp->ccode == 0xC1) {
		lprintf(LOG_INFO, "Invalid completion code received: %s",
			val2str(rsp->ccode, completion_code_vals));
	} else if (rsp->ccode == 0xCC) {
		lprintf(LOG_INFO, "Invalid data field received: %s",
			val2str(rsp->ccode, completion_code_vals));
	} else if (rsp->ccode != 0) {
		lprintf(LOG_INFO, "Invalid completion code received: %s",
			val2str(rsp->ccode, completion_code_vals));
	} else if (rsp->data_len < 5) {
		lprintf(LOG_INFO, "Invalid response length %d",
			rsp->data_len);
	} else if (rsp->data[0] != GROUP_EXT_VITA) {
		lprintf(LOG_INFO, "Invalid group extension %#x",
			rsp->data[0]);
	} else if ((rsp->data[3] & 0x03) != 0) {
		lprintf(LOG_INFO, "Unknown VSO Standard %d",
			(rsp->data[3] & 0x03));
	} else if ((rsp->data[4] & 0x0F) != 1) {
		lprintf(LOG_INFO, "Unknown VSO Specification Revision %d.%d",
			(rsp->data[4] & 0x0F), (rsp->data[4] >> 4));
	} else {
		vita_avail = 1;
		lprintf(LOG_INFO, "Discovered VITA 46.11 Revision %d.%d",
			(rsp->data[4] & 0x0F), (rsp->data[4] >> 4));
	}

	return vita_avail;
}

uint8_t
ipmi_vita_ipmb_address(struct ipmi_intf *intf)
{
	struct ipmi_rq req;
	struct ipmi_rs *rsp;
	unsigned char msg_data;

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

	req.msg.netfn = IPMI_NETFN_PICMG;
	req.msg.cmd = VITA_GET_FRU_ADDRESS_INFO_CMD;
	req.msg.data = &msg_data;
	req.msg.data_len = 1;

	msg_data = GROUP_EXT_VITA;

	rsp = intf->sendrecv(intf, &req);

	if (rsp == NULL) {
		lprintf(LOG_ERR, "No valid response received");
	} else if (rsp->ccode != 0) {
		lprintf(LOG_ERR, "Invalid completion code received: %s",
			val2str(rsp->ccode, completion_code_vals));
	} else if (rsp->data_len < 7) {
		lprintf(LOG_ERR, "Invalid response length %d",
			rsp->data_len);
	} else if (rsp->data[0] != GROUP_EXT_VITA) {
		lprintf(LOG_ERR, "Invalid group extension %#x",
			rsp->data[0]);
	} else {
		return rsp->data[2];
	}

	return 0;
}

static int
ipmi_vita_getaddr(struct ipmi_intf *intf, int argc, char **argv)
{
	struct ipmi_rs *rsp;
	struct ipmi_rq req;
	unsigned char msg_data[2];

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

	req.msg.netfn = IPMI_NETFN_PICMG;
	req.msg.cmd = VITA_GET_FRU_ADDRESS_INFO_CMD;
	req.msg.data = msg_data;
	req.msg.data_len = 2;

	msg_data[0] = GROUP_EXT_VITA;		/* VITA identifier */
	msg_data[1] = 0;			/* default FRU ID */

	if (argc > 0) {
		/* validate and get FRU Device ID */
		if (is_fru_id(argv[0], &msg_data[1]) != 0) {
			return -1;
		}
	}

	rsp = intf->sendrecv(intf, &req);

	if (rsp == NULL) {
		lprintf(LOG_ERR, "No valid response received");
		return -1;
	} else if (rsp->ccode != 0) {
		lprintf(LOG_ERR, "Invalid completion code received: %s",
			val2str(rsp->ccode, completion_code_vals));
		return -1;
	} else if (rsp->data_len < 7) {
		lprintf(LOG_ERR, "Invalid response length %d",
			rsp->data_len);
		return -1;
	} else if (rsp->data[0] != GROUP_EXT_VITA) {
		lprintf(LOG_ERR, "Invalid group extension %#x",
			rsp->data[0]);
		return -1;
	}

	printf("Hardware Address : 0x%02x\n", rsp->data[1]);
	printf("IPMB-0 Address   : 0x%02x\n", rsp->data[2]);
	printf("FRU ID           : 0x%02x\n", rsp->data[4]);
	printf("Site ID          : 0x%02x\n", rsp->data[5]);
	printf("Site Type        : %s\n", val2str(rsp->data[6],
		vita_site_types));
	if (rsp->data_len > 8) {
		printf("Channel 7 Address: 0x%02x\n", rsp->data[8]);
	}

	return 0;
}

static int
ipmi_vita_get_vso_capabilities(struct ipmi_intf *intf)
{
	struct ipmi_rs *rsp;
	struct ipmi_rq req;
	unsigned char msg_data, tmp;

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

	req.msg.netfn = IPMI_NETFN_PICMG;
	req.msg.cmd = VITA_GET_VSO_CAPABILITIES_CMD;
	req.msg.data = &msg_data;
	req.msg.data_len = 1;

	msg_data = GROUP_EXT_VITA;		/* VITA identifier */

	rsp = intf->sendrecv(intf, &req);

	if (rsp == NULL) {
		lprintf(LOG_ERR, "No valid response received.");
		return -1;
	} else if (rsp->ccode != 0) {
		lprintf(LOG_ERR, "Invalid completion code received: %s",
			val2str(rsp->ccode, completion_code_vals));
		return -1;
	} else if (rsp->data_len < 5) {
		lprintf(LOG_ERR, "Invalid response length %d", rsp->data_len);
		return -1;
	} else if (rsp->data[0] != GROUP_EXT_VITA) {
		lprintf(LOG_ERR, "Invalid group extension %#x", rsp->data[0]);
		return -1;
	}

	printf("VSO Identifier    : 0x%02x\n", rsp->data[0]);
	printf("IPMC Identifier   : 0x%02x\n", rsp->data[1]);
	printf("    Tier  %d\n", (rsp->data[1] & 0x03) + 1);
	printf("    Layer %d\n", ((rsp->data[1] & 0x30) >> 4) + 1);

	printf("IPMB Capabilities : 0x%02x\n", rsp->data[2]);

	tmp = (rsp->data[2] & 0x30) >> 4;

	printf("    Frequency  %skHz\n",
		tmp == 0 ? "100" : tmp == 1 ? "400" : "RESERVED");

	tmp = rsp->data[2] & 3;

	if (tmp == 1) {
		printf("    2 IPMB interfaces supported\n");
	} else if (tmp == 0) {
		printf("    1 IPMB interface supported\n");
	}

	printf("VSO Standard      : %s\n",
		(rsp->data[3] & 0x3) == 0 ? "VITA 46.11" : "RESERVED");

	printf("VSO Spec Revision : %d.%d\n", rsp->data[4] & 0xf,
		rsp->data[4] >> 4);

	printf("Max FRU Device ID : 0x%02x\n", rsp->data[5]);
	printf("FRU Device ID     : 0x%02x\n", rsp->data[6]);

	return 0;
}

static int
ipmi_vita_set_fru_activation(struct ipmi_intf *intf,
	char **argv, unsigned char command)
{
	struct ipmi_rs *rsp;
	struct ipmi_rq req;
	unsigned char msg_data[3];

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

	req.msg.netfn = IPMI_NETFN_PICMG;
	req.msg.cmd = VITA_SET_FRU_ACTIVATION_CMD;
	req.msg.data = msg_data;
	req.msg.data_len = 3;

	msg_data[0]	= GROUP_EXT_VITA;		/* VITA identifier */
	if (is_fru_id(argv[0], &msg_data[1]) != 0) {	/* FRU ID */
		return -1;
	}
	msg_data[2]	= command;			/* command */

	rsp = intf->sendrecv(intf, &req);

	if (rsp == NULL) {
		lprintf(LOG_ERR, "No valid response received.");
		return -1;
	} else if (rsp->ccode != 0) {
		lprintf(LOG_ERR, "Invalid completion code received: %s",
			val2str(rsp->ccode, completion_code_vals));
		return -1;
	} else if (rsp->data_len < 1) {
		lprintf(LOG_ERR, "Invalid response length %d", rsp->data_len);
		return -1;
	} else if (rsp->data[0] != GROUP_EXT_VITA) {
		lprintf(LOG_ERR, "Invalid group extension %#x", rsp->data[0]);
		return -1;
	}

	printf("FRU has been successfully %s\n",
		command ? "activated" : "deactivated");

	return 0;
}

static int
ipmi_vita_get_fru_state_policy_bits(struct ipmi_intf *intf, char **argv)
{
	struct ipmi_rs *rsp;
	struct ipmi_rq req;
	unsigned char msg_data[2];

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

	req.msg.netfn = IPMI_NETFN_PICMG;
	req.msg.cmd = VITA_GET_FRU_STATE_POLICY_BITS_CMD;
	req.msg.data = msg_data;
	req.msg.data_len = 2;

	msg_data[0] = GROUP_EXT_VITA;			/* VITA identifier */
	if (is_fru_id(argv[0], &msg_data[1]) != 0) {	/* FRU ID */
		return -1;
	}

	rsp = intf->sendrecv(intf, &req);

	if (rsp == NULL) {
		lprintf(LOG_ERR, "No valid response received.");
		return -1;
	} else if (rsp->ccode != 0) {
		lprintf(LOG_ERR, "Invalid completion code received: %s",
			val2str(rsp->ccode, completion_code_vals));
		return -1;
	} else if (rsp->data_len < 2) {
		lprintf(LOG_ERR, "Invalid response length %d", rsp->data_len);
		return -1;
	} else if (rsp->data[0] != GROUP_EXT_VITA) {
		lprintf(LOG_ERR, "Invalid group extension %#x", rsp->data[0]);
		return -1;
	}

	printf("FRU State Policy Bits:	%xh\n", rsp->data[1]);
	printf("    Default-Activation-Locked Policy Bit is %d\n",
		rsp->data[1] & 0x08 ? 1 : 0);
	printf("    Commanded-Deactivation-Ignored Policy Bit is %d\n",
		rsp->data[1] & 0x04 ? 1 : 0);
	printf("    Deactivation-Locked Policy Bit is %d\n",
		rsp->data[1] & 0x02 ? 1 : 0);
	printf("    Activation-Locked Policy Bit is %d\n",
		rsp->data[1] & 0x01);

	return 0;
}

static int
ipmi_vita_set_fru_state_policy_bits(struct ipmi_intf *intf, char **argv)
{
	struct ipmi_rs *rsp;
	struct ipmi_rq req;
	unsigned char msg_data[4];

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

	req.msg.netfn = IPMI_NETFN_PICMG;
	req.msg.cmd = VITA_SET_FRU_STATE_POLICY_BITS_CMD;
	req.msg.data = msg_data;
	req.msg.data_len = 4;

	msg_data[0] = GROUP_EXT_VITA;			/* VITA identifier */
	if (is_fru_id(argv[0], &msg_data[1]) != 0) {	/* FRU ID */
		return -1;
	}
	if (str2uchar(argv[1], &msg_data[2]) != 0) {	/* bits mask */
		return -1;
	}
	if (str2uchar(argv[2], &msg_data[3]) != 0) {	/* bits */
		return -1;
	}

	rsp = intf->sendrecv(intf, &req);

	if (rsp == NULL) {
		lprintf(LOG_ERR, "No valid response received.");
		return -1;
	} else if (rsp->ccode != 0) {
		lprintf(LOG_ERR, "Invalid completion code received: %s",
			val2str(rsp->ccode, completion_code_vals));
		return -1;
	} else if (rsp->data_len < 1) {
		lprintf(LOG_ERR, "Invalid response length %d", rsp->data_len);
		return -1;
	} else if (rsp->data[0] != GROUP_EXT_VITA) {
		lprintf(LOG_ERR, "Invalid group extension %#x", rsp->data[0]);
		return -1;
	}

	printf("FRU state policy bits have been updated\n");

	return 0;
}

static int
ipmi_vita_get_led_properties(struct ipmi_intf *intf, char **argv)
{
	struct ipmi_rs *rsp;
	struct ipmi_rq req;
	unsigned char msg_data[2];

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

	req.msg.netfn = IPMI_NETFN_PICMG;
	req.msg.cmd = VITA_GET_FRU_LED_PROPERTIES_CMD;
	req.msg.data = msg_data;
	req.msg.data_len = 2;

	msg_data[0] = GROUP_EXT_VITA;			/* VITA identifier */
	if (is_fru_id(argv[0], &msg_data[1]) != 0) {	/* FRU ID */
		return -1;
	}

	rsp = intf->sendrecv(intf, &req);

	if (rsp == NULL) {
		lprintf(LOG_ERR, "No valid response received.");
		return -1;
	} else if (rsp->ccode != 0) {
		lprintf(LOG_ERR, "Invalid completion code received: %s",
			val2str(rsp->ccode, completion_code_vals));
		return -1;
	} else if (rsp->data_len < 3) {
		lprintf(LOG_ERR, "Invalid response length %d", rsp->data_len);
		return -1;
	} else if (rsp->data[0] != GROUP_EXT_VITA) {
		lprintf(LOG_ERR, "Invalid group extension %#x", rsp->data[0]);
		return -1;
	}

	printf("LED Count:	   %#x\n", rsp->data[2]);

	return 0;
}

static int
ipmi_vita_get_led_color_capabilities(struct ipmi_intf *intf, char **argv)
{
	struct ipmi_rs *rsp;
	struct ipmi_rq req;
	unsigned char msg_data[3];
	int i;

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

	req.msg.netfn = IPMI_NETFN_PICMG;
	req.msg.cmd = VITA_GET_LED_COLOR_CAPABILITIES_CMD;
	req.msg.data = msg_data;
	req.msg.data_len = 3;

	msg_data[0] = GROUP_EXT_VITA;			/* VITA identifier */
	if (is_fru_id(argv[0], &msg_data[1]) != 0) {	/* FRU ID */
		return -1;
	}
	if (str2uchar(argv[1], &msg_data[2]) != 0) {	/* LED-ID */
		return -1;
	}

	rsp = intf->sendrecv(intf, &req);

	if (rsp == NULL) {
		lprintf(LOG_ERR, "No valid response received.");
		return -1;
	} else if (rsp->ccode != 0) {
		lprintf(LOG_ERR, "Invalid completion code received: %s",
			val2str(rsp->ccode, completion_code_vals));
		return -1;
	} else if (rsp->data_len < 4) {
		lprintf(LOG_ERR, "Invalid response length %d", rsp->data_len);
		return -1;
	} else if (rsp->data[0] != GROUP_EXT_VITA) {
		lprintf(LOG_ERR, "Invalid group extension %#x", rsp->data[0]);
		return -1;
	}

	printf("LED Color Capabilities: ");
	for (i = 0; i < 8; i++) {
		if (rsp->data[1] & (0x01 << i)) {
			printf("%s, ", led_color_str[i]);
		}
	}
	putchar('\n');

	printf("Default LED Color in\n");
	printf("      LOCAL control:  %s\n", led_color_str[rsp->data[2]]);
	printf("      OVERRIDE state: %s\n", led_color_str[rsp->data[3]]);

	if (rsp->data_len == 5) {
		printf("LED flags:\n");
		if (rsp->data[4] & 2) {
			printf("      [HW RESTRICT]\n");
		}
		if (rsp->data[4] & 1) {
			printf("      [PAYLOAD PWR]\n");
		}
	}

	return 0;
}

static int
ipmi_vita_get_led_state(struct ipmi_intf *intf, char **argv)
{
	struct ipmi_rs *rsp;
	struct ipmi_rq req;
	unsigned char msg_data[3];

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

	req.msg.netfn = IPMI_NETFN_PICMG;
	req.msg.cmd = VITA_GET_FRU_LED_STATE_CMD;
	req.msg.data = msg_data;
	req.msg.data_len = 3;

	msg_data[0] = GROUP_EXT_VITA;			/* VITA identifier */
	if (is_fru_id(argv[0], &msg_data[1]) != 0) {	/* FRU ID */
		return -1;
	}
	if (str2uchar(argv[1], &msg_data[2]) != 0) {	/* LED-ID */
		return -1;
	}

	rsp = intf->sendrecv(intf, &req);

	if (rsp == NULL) {
		lprintf(LOG_ERR, "No valid response received.");
		return -1;
	} else if (rsp->ccode != 0) {
		lprintf(LOG_ERR, "Invalid completion code received: %s",
			val2str(rsp->ccode, completion_code_vals));
		return -1;
	} else if (rsp->data_len < 5
		|| ((rsp->data[1] & 0x2) && rsp->data_len < 8)
		|| ((rsp->data[1] & 0x4) && rsp->data_len < 9)) {
		lprintf(LOG_ERR, "Invalid response length %d", rsp->data_len);
		return -1;
	} else if (rsp->data[0] != GROUP_EXT_VITA) {
		lprintf(LOG_ERR, "Invalid group extension %#x", rsp->data[0]);
		return -1;
	}

	printf("LED states:                   %x\t", rsp->data[1]);
	if (rsp->data[1] & 0x1) {
		printf("[LOCAL CONTROL] ");
	}
	if (rsp->data[1] & 0x2) {
		printf("[OVERRIDE] ");
	}
	if (rsp->data[1] & 0x4) {
		printf("[LAMPTEST] ");
	}
	if (rsp->data[1] & 0x8) {
		printf("[HW RESTRICT] ");
	}
	putchar('\n');

	if (rsp->data[1] & 1) {
		printf("  Local Control function:     %x\t", rsp->data[2]);
		if (rsp->data[2] == 0x0) {
			printf("[OFF]\n");
		} else if (rsp->data[2] == 0xff) {
			printf("[ON]\n");
		} else {
			printf("[BLINKING]\n");
		}
		printf("  Local Control On-Duration:  %x\n", rsp->data[3]);
		printf("  Local Control Color:        %x\t[%s]\n",
			rsp->data[4], led_color_str[rsp->data[4] & 7]);
	}

	/* override state or lamp test */
	if (rsp->data[1] & 0x06) {
		printf("  Override function:     %x\t", rsp->data[5]);
		if (rsp->data[5] == 0x0) {
			printf("[OFF]\n");
		} else if (rsp->data[5] == 0xff) {
			printf("[ON]\n");
		} else {
			printf("[BLINKING]\n");
		}
		printf("  Override On-Duration:  %x\n", rsp->data[6]);
		printf("  Override Color:        %x\t[%s]\n",
			rsp->data[7], led_color_str[rsp->data[7] & 7]);
		if (rsp->data[1] == 0x04) {
			printf("  Lamp test duration:    %x\n", rsp->data[8]);
		}
	}

	return 0;
}

static int
ipmi_vita_set_led_state(struct ipmi_intf *intf, char **argv)
{
	struct ipmi_rs *rsp;
	struct ipmi_rq req;
	unsigned char msg_data[6];

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

	req.msg.netfn = IPMI_NETFN_PICMG;
	req.msg.cmd = VITA_SET_FRU_LED_STATE_CMD;
	req.msg.data = msg_data;
	req.msg.data_len = 6;

	msg_data[0] = GROUP_EXT_VITA;			/* VITA identifier */
	if (is_fru_id(argv[0], &msg_data[1]) != 0) {	/* FRU ID */
		return -1;
	}
	if (str2uchar(argv[1], &msg_data[2]) != 0) {	/* LED-ID */
		return -1;
	}
	if (str2uchar(argv[2], &msg_data[3]) != 0) {	/* LED function */
		return -1;
	}
	if (str2uchar(argv[3], &msg_data[4]) != 0) {	/* LED on duration */
		return -1;
	}
	if (str2uchar(argv[4], &msg_data[5]) != 0) {	/* LED color */
		return -1;
	}

	rsp = intf->sendrecv(intf, &req);

	if (rsp == NULL) {
		lprintf(LOG_ERR, "No valid response received.");
		return -1;
	} else if (rsp->ccode != 0) {
		lprintf(LOG_ERR, "Invalid completion code received: %s",
			val2str(rsp->ccode, completion_code_vals));
		return -1;
	} else if (rsp->data_len < 1) {
		lprintf(LOG_ERR, "Invalid response length %d", rsp->data_len);
		return -1;
	} else if (rsp->data[0] != GROUP_EXT_VITA) {
		lprintf(LOG_ERR, "Invalid group extension %#x", rsp->data[0]);
		return -1;
	}

	printf("LED state has been updated\n");

	return 0;
}

static int
ipmi_vita_fru_control(struct ipmi_intf *intf, char **argv)
{
	struct ipmi_rs *rsp;
	struct ipmi_rq req;
	unsigned char msg_data[3];

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

	req.msg.netfn = IPMI_NETFN_PICMG;
	req.msg.cmd = VITA_FRU_CONTROL_CMD;
	req.msg.data = msg_data;
	req.msg.data_len = 3;

	msg_data[0] = GROUP_EXT_VITA;			/* VITA identifier */
	if (is_fru_id(argv[0], &msg_data[1]) != 0) {	/* FRU ID */
		return -1;
	}
	if (str2uchar(argv[1], &msg_data[2]) != 0) {	/* control option */
		return -1;
	}

	printf("FRU Device Id: %d FRU Control Option: %s\n", msg_data[1],
		val2str(msg_data[2], picmg_frucontrol_vals));

	rsp = intf->sendrecv(intf, &req);

	if (rsp == NULL) {
		lprintf(LOG_ERR, "No valid response received.");
		return -1;
	} else if (rsp->ccode != 0) {
		lprintf(LOG_ERR, "Invalid completion code received: %s",
			val2str(rsp->ccode, completion_code_vals));
		return -1;
	} else if (rsp->data_len < 1) {
		lprintf(LOG_ERR, "Invalid response length %d", rsp->data_len);
		return -1;
	} else if (rsp->data[0] != GROUP_EXT_VITA) {
		lprintf(LOG_ERR, "Invalid group extension %#x", rsp->data[0]);
		return -1;
	}

	printf("FRU Control: ok\n");

	return 0;
}

static int
ipmi_vita_get_cmd(int argc, char **argv)
{
	if (argc < 1 || !strncmp(argv[0], "help", 4)) {
		return VITA_CMD_HELP;
	}

	/* Get VSO Properties */
	if (!strncmp(argv[0], "properties", 10)) {
		return VITA_CMD_PROPERTIES;
	}

	/* FRU Control command */
	if (!strncmp(argv[0], "frucontrol", 10)) {
		return VITA_CMD_FRUCONTROL;
	}

	/* Get FRU Address Info command */
	if (!strncmp(argv[0], "addrinfo", 8)) {
		return VITA_CMD_ADDRINFO;
	}

	/* Set FRU Activation (activate) command */
	if (!strncmp(argv[0], "activate", 8)) {
		return VITA_CMD_ACTIVATE;
	}

	/* Set FRU Activation (deactivate) command */
	if (!strncmp(argv[0], "deactivate", 10)) {
		return VITA_CMD_DEACTIVATE;
	}

	/* FRU State Policy Bits commands */
	if (!strncmp(argv[0], "policy", 6)) {
		if (argc < 2) {
			return VITA_CMD_UNKNOWN;
		}

		/* Get FRU State Policy Bits command */
		if (!strncmp(argv[1], "get", 3)) {
			return VITA_CMD_POLICY_GET;
		}

		/* Set FRU State Policy Bits command */
		if (!strncmp(argv[1], "set", 3)) {
			return VITA_CMD_POLICY_SET;
		}

		/* unknown command */
		return VITA_CMD_UNKNOWN;
	}

	/* FRU LED commands */
	if (!strncmp(argv[0], "led", 3)) {
		if (argc < 2) {
			return VITA_CMD_UNKNOWN;
		}

		/* FRU LED Get Properties */
		if (!strncmp(argv[1], "prop", 4)) {
			return VITA_CMD_LED_PROP;
		}

		/* FRU LED Get Capabilities */
		if (!strncmp(argv[1], "cap", 3)) {
			return VITA_CMD_LED_CAP;
		}

		/* FRU LED Get State */
		if (!strncmp(argv[1], "get", 3)) {
			return VITA_CMD_LED_GET;
		}

		/* FRU LED Set State */
		if (!strncmp(argv[1], "set", 3)) {
			return VITA_CMD_LED_SET;
		}

		/* unknown command */
		return VITA_CMD_UNKNOWN;
	}

	/* unknown command */
	return VITA_CMD_UNKNOWN;
}

int
ipmi_vita_main (struct ipmi_intf *intf, int argc, char **argv)
{
	int rc = -1, show_help = 0;
	int cmd = ipmi_vita_get_cmd(argc, argv);

	switch (cmd) {
	case VITA_CMD_HELP:
		cmd = ipmi_vita_get_cmd(argc - 1, &argv[1]);
		show_help = 1;
		rc = 0;
		break;

	case VITA_CMD_PROPERTIES:
		rc = ipmi_vita_get_vso_capabilities(intf);
		break;

	case VITA_CMD_FRUCONTROL:
		if (argc > 2) {
			rc = ipmi_vita_fru_control(intf, &argv[1]);
		} else {
			show_help = 1;
		}
		break;

	case VITA_CMD_ADDRINFO:
		rc = ipmi_vita_getaddr(intf, argc - 1, &argv[1]);
		break;

	case VITA_CMD_ACTIVATE:
		if (argc > 1) {
			rc = ipmi_vita_set_fru_activation(intf, &argv[1], 1);
		} else {
			show_help = 1;
		}
		break;

	case VITA_CMD_DEACTIVATE:
		if (argc > 1) {
			rc = ipmi_vita_set_fru_activation(intf, &argv[1], 0);
		} else {
			show_help = 1;
		}
		break;

	case VITA_CMD_POLICY_GET:
		if (argc > 2) {
			rc = ipmi_vita_get_fru_state_policy_bits(intf,
				&argv[2]);
		} else {
			show_help = 1;
		}
		break;

	case VITA_CMD_POLICY_SET:
		if (argc > 4) {
			rc = ipmi_vita_set_fru_state_policy_bits(intf,
				&argv[2]);
		} else {
			show_help = 1;
		}
		break;

	case VITA_CMD_LED_PROP:
		if (argc > 2) {
			rc = ipmi_vita_get_led_properties(intf, &argv[2]);
		} else {
			show_help = 1;
		}
		break;

	case VITA_CMD_LED_CAP:
		if (argc > 3) {
			rc = ipmi_vita_get_led_color_capabilities(intf,
				&argv[2]);
		} else {
			show_help = 1;
		}
		break;

	case VITA_CMD_LED_GET:
		if (argc > 3) {
			rc = ipmi_vita_get_led_state(intf, &argv[2]);
		} else {
			show_help = 1;
		}
		break;

	case VITA_CMD_LED_SET:
		if (argc > 6) {
			rc = ipmi_vita_set_led_state(intf, &argv[2]);
		} else {
			show_help = 1;
		}
		break;
	default:
		lprintf(LOG_NOTICE, "Unknown command");
		cmd = VITA_CMD_HELP;
		show_help = 1;
		break;
	}

	if (show_help) {
		lprintf(LOG_NOTICE, "%s", val2str(cmd, vita_help_strings));
	}

	return rc;
}