summaryrefslogtreecommitdiffstats
path: root/lib/plugins/stonith/bladehpi.c
blob: ae9a4cfc307f22d9aaaa8b597ce04538a5c6512b (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
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
/*
 * Stonith module for BladeCenter via OpenHPI, an implementation of Service 
 *   Availability Forum's Hardware Platfrom Interface
 *
 * Author: Dave Blaschke <debltc@us.ibm.com>
 *
 * Copyright (c) 2005 International Business Machines
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library 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.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include <lha_internal.h>

#define DEVICE		"IBM BladeCenter (OpenHPI)"

#include "stonith_plugin_common.h"

#define PIL_PLUGIN		bladehpi
#define PIL_PLUGIN_S            "bladehpi"
#define PIL_PLUGINLICENSE 	LICENSE_LGPL
#define PIL_PLUGINLICENSEURL 	URL_LGPL
#include <pils/plugin.h>

#include <openhpi/SaHpi.h>

/* Maximum number of seconds to wait for host to power off */
#define MAX_POWEROFF_WAIT	60

/* entity_root, the one required plugin parameter */
#define ST_ENTITYROOT		"entity_root"

/* String format of entity_root */
#define SYSTEM_CHASSIS_FMT	"{SYSTEM_CHASSIS,%d}"

/* soft_reset, the one optional plugin parameter */
#define ST_SOFTRESET		"soft_reset"

#define OPENHPIURL		"http://www.openhpi.org/"

/* OpenHPI resource types of interest to this plugin */
#define OHRES_NONE		0
#define OHRES_BLADECENT		1
#define OHRES_MGMTMOD		2
#define OHRES_BLADE		3

/* IBMBC_WAIT_FOR_OFF - This constant has to do with the problem that
   saHpiResourcePowerStateSet can return before the desired state has been
   achieved by the blade.  In the SAHPI_POWER_OFF case this is not good,
   as whoever calls this plugin assumes that the power is actually off
   when the plugin returns with a successful return code.  Define this
   constant to build code that loops in one second intervals after calling
   saHpiResourcePowerStateSet(SAHPI_POWER_OFF) to make sure the power is
   really off.
#define IBMBC_WAIT_FOR_OFF */

static StonithPlugin *	bladehpi_new(const char *);
static void		bladehpi_destroy(StonithPlugin *);
static const char *	bladehpi_getinfo(StonithPlugin *, int);
static const char * const *	bladehpi_get_confignames(StonithPlugin *);
static int		bladehpi_status(StonithPlugin *);
static int		bladehpi_reset_req(StonithPlugin *, int, const char *);
static char **		bladehpi_hostlist(StonithPlugin *);
static int		bladehpi_set_config(StonithPlugin *, StonithNVpair *);

static struct stonith_ops bladehpiOps = {
	bladehpi_new,			/* Create new STONITH object	*/
	bladehpi_destroy,		/* Destroy STONITH object	*/
	bladehpi_getinfo,		/* Return STONITH info string	*/
	bladehpi_get_confignames,	/* Return configuration parameters */
	bladehpi_set_config,		/* Set configuration            */
	bladehpi_status,		/* Return STONITH device status	*/
	bladehpi_reset_req,		/* Request a reset */
	bladehpi_hostlist,		/* Return list of supported hosts */
};

PIL_PLUGIN_BOILERPLATE2("1.0", Debug)

static const PILPluginImports *	PluginImports;
static PILPlugin *		OurPlugin;
static PILInterface *		OurInterface;
static StonithImports *		OurImports;
static void *			interfprivate;


PIL_rc
PIL_PLUGIN_INIT(PILPlugin *us, const PILPluginImports *imports);

PIL_rc
PIL_PLUGIN_INIT(PILPlugin *us, const PILPluginImports *imports)
{
	/* Force the compiler to do a little type checking */
	(void)(PILPluginInitFun)PIL_PLUGIN_INIT;

	PluginImports = imports;
	OurPlugin = us;

	/* Register ourself as a plugin */
	imports->register_plugin(us, &OurPIExports);  

	/*  Register our interface implementation */
 	return imports->register_interface(us
	,	PIL_PLUGINTYPE_S
	,	PIL_PLUGIN_S
	,	&bladehpiOps
	,	NULL		/* close */
	,	&OurInterface
	,	(void *)&OurImports
	,	&interfprivate); 
}

struct pluginDevice {	
	StonithPlugin		sp;
	const char *		pluginid;
	char *			idinfo;
	char *			device;
	int			softreset;
	GList *		 	hostlist;
	SaHpiVersionT		ohver;		/* OpenHPI interface version */
	SaHpiSessionIdT		ohsession;	/* session ID */
	SaHpiUint32T		ohrptcnt;	/* RPT count for hostlist */
	SaHpiResourceIdT	ohdevid;	/* device resource ID */
	SaHpiResourceIdT	ohsensid;	/* sensor resource ID */
	SaHpiSensorNumT		ohsensnum;	/* sensor number */
};

static int open_hpi_session(struct pluginDevice *dev);
static void close_hpi_session(struct pluginDevice *dev);

static const char *pluginid = "BladeCenterDevice-Stonith";
static const char *NOTpluginID = "IBM BladeCenter device has been destroyed";

#include "stonith_config_xml.h"

#define XML_ENTITYROOT_SHORTDESC \
	XML_PARM_SHORTDESC_BEGIN("en") \
	ST_ENTITYROOT \
	XML_PARM_SHORTDESC_END

#define XML_ENTITYROOT_LONGDESC \
	XML_PARM_LONGDESC_BEGIN("en") \
	"The entity_root of the STONITH device from the OpenHPI config file" \
	XML_PARM_LONGDESC_END

#define XML_ENTITYROOT_PARM \
	XML_PARAMETER_BEGIN(ST_ENTITYROOT, "string", "1", "0") \
	  XML_ENTITYROOT_SHORTDESC \
	  XML_ENTITYROOT_LONGDESC \
	XML_PARAMETER_END

#define XML_SOFTRESET_SHORTDESC \
	XML_PARM_SHORTDESC_BEGIN("en") \
	ST_SOFTRESET \
	XML_PARM_SHORTDESC_END

#define XML_SOFTRESET_LONGDESC \
	XML_PARM_LONGDESC_BEGIN("en") \
	"Soft reset indicator, true|1 if STONITH device should use soft reset (power cycle) to reset nodes, false|0 if device should use hard reset (power off, wait, power on); default is false" \
	XML_PARM_LONGDESC_END

#define XML_SOFTRESET_PARM \
	XML_PARAMETER_BEGIN(ST_SOFTRESET, "string", "0", "0") \
	  XML_SOFTRESET_SHORTDESC \
	  XML_SOFTRESET_LONGDESC \
	XML_PARAMETER_END

static const char *bladehpiXML = 
  XML_PARAMETERS_BEGIN
    XML_ENTITYROOT_PARM
    XML_SOFTRESET_PARM
  XML_PARAMETERS_END;

static int get_resource_type(char *, SaHpiRptEntryT *);
static int get_sensor_num(SaHpiSessionIdT, SaHpiResourceIdT);
static int get_bladehpi_hostlist(struct pluginDevice *);
static void free_bladehpi_hostlist(struct pluginDevice *);
static int get_num_tokens(char *str);

struct blade_info {
	char *			name;		/* blade name */
	SaHpiResourceIdT	resourceId;	/* blade resource ID */
	SaHpiCapabilitiesT	resourceCaps;	/* blade capabilities */
};


static int
bladehpi_status(StonithPlugin *s)
{
	struct pluginDevice *	dev;
	SaErrorT		ohrc;
	SaHpiDomainInfoT 	ohdi;
	int rc = S_OK;
	
	if (Debug) {
		LOG(PIL_DEBUG, "%s: called", __FUNCTION__);
	}

	ERRIFWRONGDEV(s, S_OOPS);

	dev = (struct pluginDevice *)s;
	rc = open_hpi_session(dev);
	if( rc != S_OK )
		return rc;

	/* Refresh the hostlist only if RPTs updated */
	ohrc = saHpiDomainInfoGet(dev->ohsession, &ohdi);
	if (ohrc != SA_OK) {
		LOG(PIL_CRIT, "Unable to get domain info in %s (%d)"
		,	__FUNCTION__, ohrc);
		rc = S_BADCONFIG;
		goto done;
	}
	if (dev->ohrptcnt != ohdi.RptUpdateCount) {
		free_bladehpi_hostlist(dev);
		if (get_bladehpi_hostlist(dev) != S_OK) {
			LOG(PIL_CRIT, "Unable to obtain list of hosts in %s"
			,	__FUNCTION__);
			rc = S_BADCONFIG;
			goto done;
		}
	}

	/* At this point, hostlist is up to date */
	if (dev->ohsensid && dev->ohsensnum) {
		/*
		 * For accurate status, need to make a call that goes out to
		 * BladeCenter MM because the calls made so far by this
		 * function (and perhaps get_bladehpi_hostlist) only retrieve
		 * information from memory cached by OpenHPI
		 */
		ohrc = saHpiSensorReadingGet(dev->ohsession
			, dev->ohsensid, dev->ohsensnum, NULL, NULL);
		if (ohrc == SA_ERR_HPI_BUSY || ohrc == SA_ERR_HPI_NO_RESPONSE) {
			LOG(PIL_CRIT, "Unable to connect to BladeCenter in %s"
			,	__FUNCTION__);
			rc = S_OOPS;
			goto done;
		}
	}

done:
	close_hpi_session(dev);
	return (rc == S_OK) ? (dev->ohdevid ? S_OK : S_OOPS) : rc;
}


/*
 *	Return the list of hosts configured for this HMC device
 */

static char **
bladehpi_hostlist(StonithPlugin *s)
{
	struct pluginDevice *	dev;
	int			numnames = 0, j;
	char **			ret = NULL;
	GList *			node = NULL;
	SaErrorT		ohrc;
	SaHpiDomainInfoT 	ohdi;
	int rc = S_OK;

	if (Debug) {
		LOG(PIL_DEBUG, "%s: called", __FUNCTION__);
	}

	ERRIFWRONGDEV(s, NULL);

	dev = (struct pluginDevice *)s;
	rc = open_hpi_session(dev);
	if( rc != S_OK )
		return NULL;

	/* Refresh the hostlist only if RPTs updated */
	ohrc = saHpiDomainInfoGet(dev->ohsession, &ohdi);
	if (ohrc != SA_OK) {
		LOG(PIL_CRIT, "Unable to get domain info in %s (%d)"
		,	__FUNCTION__, ohrc);
		goto done;
	}
	if (dev->ohrptcnt != ohdi.RptUpdateCount) {
		free_bladehpi_hostlist(dev);
		if (get_bladehpi_hostlist(dev) != S_OK) {
			LOG(PIL_CRIT, "Unable to obtain list of hosts in %s"
			,	__FUNCTION__);
			goto done;
		}
	}

	/* At this point, hostlist is up to date */
	numnames = g_list_length(dev->hostlist);
	if (numnames < 0) {
		LOG(PIL_CRIT, "Unconfigured stonith object in %s"
		,	__FUNCTION__);
		goto done;
	}

	ret = (char **)MALLOC((numnames+1) * sizeof(char *));
	if (ret == NULL) {
		LOG(PIL_CRIT, "Out of memory for malloc in %s", __FUNCTION__);
		goto done;
	}

	memset(ret, 0, (numnames+1) * sizeof(char *));
	for (node = g_list_first(dev->hostlist), j = 0
	;	NULL != node
	;	j++, node = g_list_next(node))	{
		ret[j] = STRDUP(((struct blade_info *)node->data)->name);
		if (ret[j] == NULL) {
			LOG(PIL_CRIT, "Out of memory for strdup in %s"
			,	__FUNCTION__);
			stonith_free_hostlist(ret);
			ret = NULL;
			goto done;
		}
		strdown(ret[j]);
	}

done:
	close_hpi_session(dev);
	return ret;
}


static const char * const *
bladehpi_get_confignames(StonithPlugin *s)
{
	static const char *	names[] = {ST_ENTITYROOT, NULL};

	if (Debug) {
		LOG(PIL_DEBUG, "%s: called", __FUNCTION__);
	}

	return names;
}


/*
 *	Reset the given host, and obey the request type.
 */

static int
bladehpi_reset_req(StonithPlugin *s, int request, const char *host)
{
	GList *			node = NULL;
	struct pluginDevice *	dev = NULL;
	struct blade_info *	bi = NULL;
	SaHpiPowerStateT	ohcurstate, ohnewstate;
	SaHpiDomainInfoT 	ohdi;
	SaErrorT		ohrc;
	int rc = S_OK;
	
	if (Debug) {
		LOG(PIL_DEBUG, "%s: called, request=%d, host=%s"
		,	__FUNCTION__, request, host);
	}
	
	ERRIFWRONGDEV(s, S_OOPS);
	
	if (host == NULL) {
		LOG(PIL_CRIT, "Invalid host argument to %s", __FUNCTION__);
		rc = S_OOPS;
		goto done;
	}

	dev = (struct pluginDevice *)s;
	rc = open_hpi_session(dev);
	if( rc != S_OK )
		return rc;

	ohrc = saHpiDomainInfoGet(dev->ohsession, &ohdi);
	if (ohrc != SA_OK) {
		LOG(PIL_CRIT, "Unable to get domain info in %s (%d)"
		,	__FUNCTION__, ohrc);
		rc = S_BADCONFIG;
		goto done;
	}
	if (dev->ohrptcnt != ohdi.RptUpdateCount) {
		free_bladehpi_hostlist(dev);
		if (get_bladehpi_hostlist(dev) != S_OK) {
			LOG(PIL_CRIT, "Unable to obtain list of hosts in %s"
			,	__FUNCTION__);
			rc = S_OOPS;
			goto done;
		}
	}

	for (node = g_list_first(dev->hostlist)
	;	node != NULL
	;	node = g_list_next(node)) {
		bi = ((struct blade_info *)node->data);
		if (Debug) {
			LOG(PIL_DEBUG, "Found host %s in hostlist", bi->name);
		}
		
		if (!strcasecmp(bi->name, host)) {
			break;
		}
	}

	if (!node || !bi) {
		LOG(PIL_CRIT
		,	"Host %s is not configured in this STONITH module, "
			"please check your configuration information", host);
		rc = S_OOPS;
		goto done;
	}

	/* Make sure host has proper capabilities for get */
	if (!(bi->resourceCaps & SAHPI_CAPABILITY_POWER)) {
		LOG(PIL_CRIT
		,	"Host %s does not have power capability", host);
		rc = S_OOPS;
		goto done;
	}

	ohrc = saHpiResourcePowerStateGet(dev->ohsession, bi->resourceId
			, &ohcurstate);
	if (ohrc != SA_OK) {
		LOG(PIL_CRIT, "Unable to get host %s power state (%d)"
		,	host, ohrc);
		rc = S_OOPS;
		goto done;
	}

	switch (request) {
		case ST_POWERON:
			if (ohcurstate == SAHPI_POWER_ON) {
				LOG(PIL_INFO, "Host %s already on", host);
				goto done;
			}
			ohnewstate = SAHPI_POWER_ON;

			break;

		case ST_POWEROFF:
			if (ohcurstate == SAHPI_POWER_OFF) {
				LOG(PIL_INFO, "Host %s already off", host);
				goto done;
			}
			ohnewstate = SAHPI_POWER_OFF;
	
			break;

		case ST_GENERIC_RESET:
			if (ohcurstate == SAHPI_POWER_OFF) {
				ohnewstate = SAHPI_POWER_ON;
			} else {
				ohnewstate = SAHPI_POWER_CYCLE;
			}

			break;

		default:
			LOG(PIL_CRIT, "Invalid request argument to %s"
			,	__FUNCTION__);
			rc = S_INVAL;
			goto done;
	}

	if (!dev->softreset && (ohnewstate == SAHPI_POWER_CYCLE)) {
		int maxwait;

		ohrc = saHpiResourcePowerStateSet(dev->ohsession
				, bi->resourceId, SAHPI_POWER_OFF);
		if (ohrc != SA_OK) {
			LOG(PIL_CRIT, "Unable to set host %s power state to"
				" OFF (%d)", host, ohrc);
			rc = S_OOPS;
			goto done;
		}

		/* 
		 * Must wait for power off here or subsequent power on request
		 * may take place while power is still on and thus ignored
		 */
		maxwait = MAX_POWEROFF_WAIT;
		do {
			maxwait--;
			sleep(1);
			ohrc = saHpiResourcePowerStateGet(dev->ohsession
					, bi->resourceId, &ohcurstate);
		} while ((ohrc == SA_OK)
			&& (ohcurstate != SAHPI_POWER_OFF)
			&& (maxwait > 0));

		if (Debug) {
			LOG(PIL_DEBUG, "Waited %d seconds for power off"
			,	MAX_POWEROFF_WAIT - maxwait);
		}

		ohrc = saHpiResourcePowerStateSet(dev->ohsession
				, bi->resourceId, SAHPI_POWER_ON);
		if (ohrc != SA_OK) {
			LOG(PIL_CRIT, "Unable to set host %s power state to"
			" ON (%d)", host, ohrc);
			rc = S_OOPS;
			goto done;
		}
	} else {
		/* Make sure host has proper capabilities to reset */
		if ((ohnewstate == SAHPI_POWER_CYCLE) &&
		    (!(bi->resourceCaps & SAHPI_CAPABILITY_RESET))) {
			LOG(PIL_CRIT
			,	"Host %s does not have reset capability"
			,	host);
			rc = S_OOPS;
			goto done;
		}

		if ((ohrc = saHpiResourcePowerStateSet(dev->ohsession
				, bi->resourceId, ohnewstate)) != SA_OK) {
			LOG(PIL_CRIT, "Unable to set host %s power state (%d)"
			,	host, ohrc);
			rc = S_OOPS;
			goto done;
		}
	}

#ifdef IBMBC_WAIT_FOR_OFF
	if (ohnewstate == SAHPI_POWER_OFF) {
		int maxwait = MAX_POWEROFF_WAIT;

		do {
			maxwait--;
			sleep(1);
			ohrc = saHpiResourcePowerStateGet(dev->ohsession
					, bi->resourceId, &ohcurstate);
		} while ((ohrc == SA_OK)
			&& (ohcurstate != SAHPI_POWER_OFF)
			&& (maxwait > 0));

		if (Debug) {
			LOG(PIL_DEBUG, "Waited %d seconds for power off"
			,	MAX_POWEROFF_WAIT - maxwait);
		}
	}
#endif

	LOG(PIL_INFO, "Host %s %s %d.", host, __FUNCTION__, request);

done:
	close_hpi_session(dev);
	return rc;
}


/*
 *	Parse the information in the given configuration file,
 *	and stash it away...
 */

static int
bladehpi_set_config(StonithPlugin *s, StonithNVpair *list)
{
	struct pluginDevice *	dev = NULL;
	StonithNamesToGet	namestocopy [] =
	{	{ST_ENTITYROOT,	NULL}
	,	{NULL,		NULL}
	};
	int			rc, i;
	
	if (Debug) {
		LOG(PIL_DEBUG, "%s: called", __FUNCTION__);
	}
	
	ERRIFWRONGDEV(s, S_OOPS);

	dev = (struct pluginDevice *)s;

	if (Debug) {
		LOG(PIL_DEBUG, "%s conditionally compiled with:"
#ifdef IBMBC_WAIT_FOR_OFF
		" IBMBC_WAIT_FOR_OFF"
#endif
		, dev->pluginid);
	}
	
	if ((rc = OurImports->CopyAllValues(namestocopy, list)) != S_OK) {
		return rc;
	}

	if (Debug) {
		LOG(PIL_DEBUG, "%s = %s", ST_ENTITYROOT
		,	namestocopy[0].s_value);	
	}

	if (get_num_tokens(namestocopy[0].s_value) == 1) {
		/* name=value pairs on command line, look for soft_reset */
		const char *softreset = 
			OurImports->GetValue(list, ST_SOFTRESET);
		if (softreset != NULL) {
			if (!strcasecmp(softreset, "true") ||
			    !strcmp(softreset, "1")) {
				dev->softreset = 1;
			} else if (!strcasecmp(softreset, "false") ||
				   !strcmp(softreset, "0")) {
				dev->softreset = 0;
			} else {
				LOG(PIL_CRIT, "Invalid %s %s, must be "
					"true, 1, false or 0"
				,	ST_SOFTRESET, softreset);
				FREE(namestocopy[0].s_value);
				return S_OOPS;
			}
		}
	} else {
		/* -p or -F option with args "entity_root [soft_reset]..." */
		char *pch = namestocopy[0].s_value;

		/* skip over entity_root and null-terminate */
		pch += strcspn(pch, WHITESPACE);
		*pch = EOS;

		/* skip over white-space up to next token */
		pch++;
		pch += strspn(pch, WHITESPACE);
		if (!strcasecmp(pch, "true") || !strcmp(pch, "1")) {
			dev->softreset = 1;
		} else if (!strcasecmp(pch, "false") || !strcmp(pch, "0")) {
			dev->softreset = 0;
		} else {
			LOG(PIL_CRIT, "Invalid %s %s, must be "
				"true, 1, false or 0"
			,	ST_SOFTRESET, pch);
			FREE(namestocopy[0].s_value);
			return S_OOPS;
		}
	}

	dev->device = STRDUP(namestocopy[0].s_value);
	FREE(namestocopy[0].s_value);
	if (dev->device == NULL) {
		LOG(PIL_CRIT, "Out of memory for strdup in %s", __FUNCTION__);
		return S_OOPS;
	}

	if (strcspn(dev->device, WHITESPACE) != strlen(dev->device) ||
	    sscanf(dev->device, SYSTEM_CHASSIS_FMT, &i) != 1 || i < 0) {
		LOG(PIL_CRIT, "Invalid %s %s, must be of format %s"
		,	ST_ENTITYROOT, dev->device, SYSTEM_CHASSIS_FMT);
		return S_BADCONFIG;
	}
	
	dev->ohver = saHpiVersionGet();
	if (dev->ohver > SAHPI_INTERFACE_VERSION) {
		LOG(PIL_CRIT, "Installed OpenHPI interface (%x) greater than "
			"one used by plugin (%x), incompatibilites may exist"
		,	dev->ohver, SAHPI_INTERFACE_VERSION);
		return S_BADCONFIG;
	}
	return S_OK;
}

static int
open_hpi_session(struct pluginDevice *dev)
{
	SaErrorT		ohrc;

	ohrc = saHpiSessionOpen(SAHPI_UNSPECIFIED_DOMAIN_ID
				    , &dev->ohsession, NULL);
	if (ohrc != SA_OK) {
		LOG(PIL_CRIT, "Unable to open HPI session (%d)", ohrc);
		return S_BADCONFIG;
	}

	ohrc = saHpiDiscover(dev->ohsession);
	if (ohrc != SA_OK) {
		LOG(PIL_CRIT, "Unable to discover resources (%d)", ohrc);
		return S_BADCONFIG;
	}

	return S_OK;
}
static void
close_hpi_session(struct pluginDevice *dev)
{
	if (dev && dev->ohsession) {
		saHpiSessionClose(dev->ohsession);
		dev->ohsession = 0;
	}
}

static const char *
bladehpi_getinfo(StonithPlugin *s, int reqtype)
{
	struct pluginDevice *	dev;
	const char *		ret;

	if (Debug) {
		LOG(PIL_DEBUG, "%s: called, reqtype=%d"
		,	__FUNCTION__, reqtype);
	}
	
	ERRIFWRONGDEV(s, NULL);

	dev = (struct pluginDevice *)s;

	switch (reqtype) {
		case ST_DEVICEID:
			ret = dev->idinfo;
			break;

		case ST_DEVICENAME:
			ret = dev->device;
			break;

		case ST_DEVICEDESCR:
			ret = "IBM BladeCenter via OpenHPI\n"
			"Use for IBM xSeries systems managed by BladeCenter\n"
			"  Required parameter name " ST_ENTITYROOT " is "
			"a string (no white-space) of\n"
			"the format \""SYSTEM_CHASSIS_FMT"\" "
			"which is entity_root of BladeCenter\n"
			"from OpenHPI config file, where %d is a positive "
			"integer\n"
			"  Optional parameter name " ST_SOFTRESET " is "
			"true|1 if STONITH device should\n"
			"use soft reset (power cycle) to reset nodes or "
			"false|0 if device should\n"
			"use hard reset (power off, wait, power on); "
			"default is false";
			break;

		case ST_DEVICEURL:
			ret = OPENHPIURL;
			break;

		case ST_CONF_XML:		/* XML metadata */
			ret = bladehpiXML;
			break;

		default:
			ret = NULL;
			break;
	}

	return ret;
}


/*
 *	HMC Stonith destructor...
 */

static void
bladehpi_destroy(StonithPlugin *s)
{
	struct pluginDevice *	dev;

	if (Debug) {
		LOG(PIL_DEBUG, "%s: called", __FUNCTION__);
	}

	VOIDERRIFWRONGDEV(s);

	dev = (struct pluginDevice *)s;

	dev->pluginid = NOTpluginID;
	if (dev->device) {
		FREE(dev->device);
		dev->device = NULL;
	}
	if (dev->idinfo) {
		FREE(dev->idinfo);
		dev->idinfo = NULL;
	}
	free_bladehpi_hostlist(dev);

	if (dev->ohsession) {
		saHpiSessionClose(dev->ohsession);
		dev->ohsession = 0;
	}
	
	FREE(dev);
}


static StonithPlugin *
bladehpi_new(const char *subplugin)
{
	struct pluginDevice *	dev = ST_MALLOCT(struct pluginDevice);
	
	if (Debug) {
		LOG(PIL_DEBUG, "%s: called", __FUNCTION__);
	}
	
	if (dev == NULL) {
		LOG(PIL_CRIT, "Out of memory in %s", __FUNCTION__);
		return NULL;
	}

	memset(dev, 0, sizeof(*dev));

	dev->pluginid = pluginid;
	dev->device = NULL;
	dev->hostlist = NULL;
	REPLSTR(dev->idinfo, DEVICE);
	if (dev->idinfo == NULL) {
		FREE(dev);
		return NULL;
	}
	dev->sp.s_ops = &bladehpiOps;

	if (Debug) {
		LOG(PIL_DEBUG, "%s: returning successfully", __FUNCTION__);
	}

	return ((void *)dev);
}


static int
get_resource_type(char *entityRoot, SaHpiRptEntryT *ohRPT)
{
	int			i, rc = OHRES_NONE;
	int			foundBlade = 0, foundExp = 0, foundMgmt = 0;
	int			foundRoot = 0, foundOther = 0;
	char			rootName[64];
	SaHpiEntityPathT *	ohep = &ohRPT->ResourceEntity;

	if (ohep == NULL || entityRoot == NULL) {
		return 0;
	}

	/* First find root of entity path, which is last entity in entry */
        for (i = 0; i < SAHPI_MAX_ENTITY_PATH; i++) {
                if (ohep->Entry[i].EntityType == SAHPI_ENT_ROOT) {
                            break;
                }
        }

	/* Then back up through entries looking for specific entity */
        for (i--; i >= 0; i--) {
		switch (ohep->Entry[i].EntityType) {
			case SAHPI_ENT_SBC_BLADE:
				foundBlade = 1;
				break;

			case SAHPI_ENT_SYS_EXPANSION_BOARD:
				foundExp = 1;
				break;

			case SAHPI_ENT_SYS_MGMNT_MODULE:
				if (ohep->Entry[i].EntityLocation == 0) {
					foundMgmt = 1;
				}
				break;

			case SAHPI_ENT_SYSTEM_CHASSIS:
				snprintf(rootName, sizeof(rootName)
				,	SYSTEM_CHASSIS_FMT
				,	ohep->Entry[i].EntityLocation);
				if (!strcmp(entityRoot, rootName)) {
					foundRoot = 1;
				}
				break;

			default:
				foundOther = 1;
				break;
		}
	}

	/* We are only interested in specific entities on specific device */
	if (foundRoot) {
		if (foundMgmt && !(foundBlade||foundExp||foundOther)) {
			rc = OHRES_MGMTMOD;
		} else if (!(foundMgmt||foundBlade||foundExp||foundOther)) {
			rc = OHRES_BLADECENT;
		} else if (foundBlade && !foundExp) {
			rc = OHRES_BLADE;
		}
	}

	return rc;
}


static int
get_sensor_num(SaHpiSessionIdT ohsession, SaHpiResourceIdT ohresid)
{
	SaErrorT	ohrc = SA_OK;
	SaHpiEntryIdT	ohnextid;
	SaHpiRdrT	ohRDR;

	ohnextid = SAHPI_FIRST_ENTRY;
	do {
		ohrc = saHpiRdrGet(ohsession, ohresid, ohnextid
				, &ohnextid, &ohRDR);
		if (ohrc != SA_OK) {
			LOG(PIL_CRIT, "Unable to get RDR entry in %s (%d)"
			,	__FUNCTION__, ohrc);
		} else if (ohRDR.RdrType == SAHPI_SENSOR_RDR) {
			return ohRDR.RdrTypeUnion.SensorRec.Num;
		}
	} while (ohrc == SA_OK && ohnextid != SAHPI_LAST_ENTRY);

	return 0;
}


/*
 *	Get RPT update count
 *	Loop through all RPT entries
 *	  If entry is BladeCenter, save resource ID in dev->ohdevid
 *	  If entry is MgmtMod and has sensor, save resource ID in dev->ohsensid
 *	    and sensor number in dev->ohsensnum
 *	  If entry is blade, save blade_info and add to dev->hostlist
 *	Get RPT update count
 *	If RPT update count changed since start of loop, repeat loop
 *	Save RPT update count in dev->ohrptcnt
 *
 *	Note that not only does this function update hostlist, it also
 *	updates ohrptcnt, ohdevid, ohsensid and ohsensnum.  However, with
 *	this logic it does not need to be called again until the RPT update
 *	count changes.
 */

static int
get_bladehpi_hostlist(struct pluginDevice *dev)
{
	struct blade_info *	bi;
	SaErrorT		ohrc;
	SaHpiEntryIdT		ohnextid;
	SaHpiRptEntryT		ohRPT;
	SaHpiDomainInfoT 	ohdi;
	SaHpiUint32T		ohupdate;

	if (Debug) {
		LOG(PIL_DEBUG, "%s: called, dev->device=%s"
		,	__FUNCTION__,	dev->device);
	}

	if (dev->device == NULL || *dev->device == 0) {
		LOG(PIL_CRIT, "Unconfigured stonith object in %s"
		,	__FUNCTION__);
		return S_BADCONFIG;
	}

	ohrc = saHpiDomainInfoGet(dev->ohsession, &ohdi);
	if (ohrc != SA_OK) {
		LOG(PIL_CRIT, "Unable to get domain info in %s (%d)"
		,	__FUNCTION__, ohrc);
		return S_BADCONFIG;
	}
	
try_again:
	ohupdate = ohdi.RptUpdateCount;
	dev->ohdevid = dev->ohsensid = dev->ohsensnum = 0;
	ohnextid = SAHPI_FIRST_ENTRY;
	do {
		char blname[SAHPI_MAX_TEXT_BUFFER_LENGTH];
		int  blnum;

		ohrc = saHpiRptEntryGet(dev->ohsession, ohnextid
				       , &ohnextid, &ohRPT);
		if (ohrc != SA_OK) {
			LOG(PIL_CRIT, "Unable to get RPT entry in %s (%d)"
			,	__FUNCTION__, ohrc);
			free_bladehpi_hostlist(dev);
			return S_BADCONFIG;
		}

		switch (get_resource_type(dev->device, &ohRPT)) {
		case OHRES_BLADECENT:
			dev->ohdevid = ohRPT.ResourceId;

			if (Debug) {
				LOG(PIL_DEBUG, "BladeCenter '%s' has id %d"
				,	(char*)ohRPT.ResourceTag.Data
				,	dev->ohdevid);
			}
			break;

		case OHRES_MGMTMOD:
			if (ohRPT.ResourceCapabilities&SAHPI_CAPABILITY_SENSOR){
 				dev->ohsensnum = get_sensor_num(dev->ohsession
							, ohRPT.ResourceId);

				if (dev->ohsensnum) {
					dev->ohsensid = ohRPT.ResourceId;

					if (Debug) {
						LOG(PIL_DEBUG
						, "MgmtModule '%s' has id %d "
						"with sensor #%d"
						, (char*)ohRPT.ResourceTag.Data
						, dev->ohsensid
						, dev->ohsensnum);
					}
				}
			} 
			break;

		case OHRES_BLADE:
			if ((bi = (struct blade_info *)
				MALLOC(sizeof(struct blade_info))) == NULL) {
			        LOG(PIL_CRIT, "Out of memory in %s"
				,	__FUNCTION__);
				free_bladehpi_hostlist(dev);
			        return S_OOPS;
			}

			/*
			 * New format consists of "Blade N - name" while older
			 * format consists only of "name"; we only need to
			 * stash name because ResourceID is the important info
			 */
			if (sscanf((char*)ohRPT.ResourceTag.Data, "Blade %d - %s"
					, &blnum, blname) == 2) {
				bi->name = STRDUP(blname);
			} else {
				bi->name = STRDUP((char*)ohRPT.ResourceTag.Data);
			}
			if (bi->name == NULL) {
				LOG(PIL_CRIT, "Out of memory for strdup in %s"
				,	__FUNCTION__);
				free_bladehpi_hostlist(dev);
			        return S_OOPS;
			}

			bi->resourceId = ohRPT.ResourceId;
			bi->resourceCaps = ohRPT.ResourceCapabilities;
			dev->hostlist = g_list_append(dev->hostlist, bi);

			if (Debug) {
				LOG(PIL_DEBUG, "Blade '%s' has id %d, caps %x"
				, bi->name, bi->resourceId, bi->resourceCaps);
			}
			break;
		}
	} while (ohrc == SA_OK && ohnextid != SAHPI_LAST_ENTRY);

	ohrc = saHpiDomainInfoGet(dev->ohsession, &ohdi);
	if (ohrc != SA_OK) {
		LOG(PIL_CRIT, "Unable to get domain info in %s (%d)"
		,	__FUNCTION__, ohrc);
		free_bladehpi_hostlist(dev);
		return S_BADCONFIG;
	}

	if (ohupdate != ohdi.RptUpdateCount) {
		free_bladehpi_hostlist(dev);
		if(Debug){
			LOG(PIL_DEBUG, "Looping through entries again,"
				" count changed from %d to %d"
			,	ohupdate, ohdi.RptUpdateCount);
		}
		goto try_again;
	}

	dev->ohrptcnt = ohupdate;

	return S_OK;
}


static void
free_bladehpi_hostlist(struct pluginDevice *dev)
{
	if (dev->hostlist) {
		GList *node;
		while (NULL != (node = g_list_first(dev->hostlist))) {
			dev->hostlist = 
				g_list_remove_link(dev->hostlist, node);
			FREE(((struct blade_info *)node->data)->name);
			FREE(node->data);
			g_list_free(node);
		}
		dev->hostlist = NULL;
	}
	dev->ohdevid = dev->ohsensid = dev->ohsensnum = 0;
}


static int
get_num_tokens(char *str)
{
	int 	namecount = 0;

	while (*str != EOS) {
		str += strspn(str, WHITESPACE);
		if (*str == EOS)
			break;
		str += strcspn(str, WHITESPACE);
		namecount++;
	}
	return namecount;
}