summaryrefslogtreecommitdiff
path: root/usr/src/cmd/drd/drd_rcm.c
blob: 5f16ef1c7bcc69f762c3071e216f8f21293e498f (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
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*
 * RCM backend for the DR Daemon
 */

#include <unistd.h>
#include <strings.h>
#include <errno.h>
#include <kstat.h>
#include <libnvpair.h>
#include <librcm.h>

#include "drd.h"

/*
 * RCM Backend Support
 */
static int drd_rcm_init(void);
static int drd_rcm_fini(void);
static int drd_rcm_cpu_config_request(drctl_rsrc_t *rsrcs, int nrsrc);
static int drd_rcm_cpu_config_notify(drctl_rsrc_t *rsrcs, int nrsrc);
static int drd_rcm_cpu_unconfig_request(drctl_rsrc_t *rsrcs, int nrsrc);
static int drd_rcm_cpu_unconfig_notify(drctl_rsrc_t *rsrcs, int nrsrc);

drd_backend_t drd_rcm_backend = {
	drd_rcm_init,			/* init */
	drd_rcm_fini,			/* fini */
	drd_rcm_cpu_config_request,	/* cpu_config_request */
	drd_rcm_cpu_config_notify,	/* cpu_config_notify */
	drd_rcm_cpu_unconfig_request,	/* cpu_unconfig_request */
	drd_rcm_cpu_unconfig_notify	/* cpu_unconfig_notify */
};

#define	RCM_CPU_ALL		"SUNW_cpu"
#define	RCM_CPU			RCM_CPU_ALL"/cpu"
#define	RCM_CPU_MAX_LEN		(32)

/* global RCM handle used in all RCM operations */
static rcm_handle_t *rcm_hdl;

/* functions that call into RCM */
static int drd_rcm_online_cpu_notify(drctl_rsrc_t *rsrcs, int nrsrc);
static int drd_rcm_add_cpu_notify(drctl_rsrc_t *rsrcs, int nrsrc);
static int drd_rcm_del_cpu_request(drctl_rsrc_t *rsrcs, int nrsrc);
static int drd_rcm_offline_cpu_request(drctl_rsrc_t *rsrcs, int nrsrc);
static int drd_rcm_remove_cpu_notify(drctl_rsrc_t *rsrcs, int nrsrc);
static int drd_rcm_restore_cpu_notify(drctl_rsrc_t *rsrcs, int nrsrc);
static int drd_rcm_del_cpu_notify(drctl_rsrc_t *rsrcs, int nrsrc);

/* utility functions */
static char **drd_rcm_cpu_rlist_init(drctl_rsrc_t *, int nrsrc, int status);
static void drd_rcm_cpu_rlist_fini(char **rlist);
static drctl_rsrc_t *cpu_rsrcstr_to_rsrc(const char *, drctl_rsrc_t *, int);
static int get_sys_cpuids(cpuid_t **cpuids, int *ncpuids);
static boolean_t is_cpu_in_list(cpuid_t cpuid, cpuid_t *list, int len);

/* debugging utility functions */
static void dump_cpu_list(char *prefix, cpuid_t *cpuids, int ncpuids);
static void dump_cpu_rsrc_list(char *prefix, drctl_rsrc_t *, int nrsrc);
static void dump_cpu_rlist(char **rlist);

static int
drd_rcm_init(void)
{
	int	rv;

	drd_dbg("drd_rcm_init...");

	rv = rcm_alloc_handle(NULL, 0, NULL, &rcm_hdl);
	if (rv == RCM_FAILURE) {
		drd_err("unable to allocate RCM handle: %s", strerror(errno));
		return (-1);
	}

	return (0);
}

static int
drd_rcm_fini(void)
{
	drd_dbg("drd_rcm_fini...");

	if (rcm_hdl != NULL)
		rcm_free_handle(rcm_hdl);

	return (0);
}

static int
drd_rcm_cpu_config_request(drctl_rsrc_t *rsrcs, int nrsrc)
{
	int	idx;

	drd_dbg("drd_rcm_cpu_config_request...");
	dump_cpu_rsrc_list(NULL, rsrcs, nrsrc);

	/*
	 * There is no RCM operation to request the addition
	 * of resources. So, by definition, the operation for
	 * all the CPUs is allowed.
	 */
	for (idx = 0; idx < nrsrc; idx++)
		rsrcs[idx].status = DRCTL_STATUS_ALLOW;

	dump_cpu_rsrc_list("returning:", rsrcs, nrsrc);

	return (0);
}

static int
drd_rcm_cpu_config_notify(drctl_rsrc_t *rsrcs, int nrsrc)
{
	int	rv = 0;

	drd_dbg("drd_rcm_cpu_config_notify...");
	dump_cpu_rsrc_list(NULL, rsrcs, nrsrc);

	/* notify RCM about the newly added CPUs */
	if (drd_rcm_online_cpu_notify(rsrcs, nrsrc) != 0) {
		rv = -1;
		goto done;
	}

	/* notify RCM about the increased CPU capacity */
	if (drd_rcm_add_cpu_notify(rsrcs, nrsrc) != 0) {
		rv = -1;
	}

done:
	dump_cpu_rsrc_list("returning:", rsrcs, nrsrc);

	return (rv);
}

static int
drd_rcm_cpu_unconfig_request(drctl_rsrc_t *rsrcs, int nrsrc)
{
	int	rv = 0;
	int	idx;

	drd_dbg("drd_rcm_cpu_unconfig_request...");
	dump_cpu_rsrc_list(NULL, rsrcs, nrsrc);

	/* contact RCM to request a decrease in CPU capacity */
	if (drd_rcm_del_cpu_request(rsrcs, nrsrc) != 0) {
		rv = -1;
		goto done;
	}

	/* contact RCM to request the removal of CPUs */
	if (drd_rcm_offline_cpu_request(rsrcs, nrsrc) != 0) {
		rv = -1;
		goto done;
	}

done:
	/*
	 * If any errors occurred, the status field for
	 * a CPU may still be in the INIT state. Set the
	 * status for any such CPU to DENY to ensure it
	 * gets processed properly.
	 */
	for (idx = 0; idx < nrsrc; idx++) {
		if (rsrcs[idx].status == DRCTL_STATUS_INIT)
			rsrcs[idx].status = DRCTL_STATUS_DENY;
	}

	dump_cpu_rsrc_list("returning:", rsrcs, nrsrc);

	return (rv);
}

static int
drd_rcm_cpu_unconfig_notify(drctl_rsrc_t *rsrcs, int nrsrc)
{
	int	rv = 0;

	drd_dbg("drd_rcm_cpu_unconfig_notify...");
	dump_cpu_rsrc_list(NULL, rsrcs, nrsrc);

	/*
	 * Notify RCM about the CPUs that were removed.
	 * Failures are ignored so that CPUs that could
	 * not be unconfigured can be processed by RCM.
	 */
	(void) drd_rcm_remove_cpu_notify(rsrcs, nrsrc);

	/*
	 * Notify RCM about any CPUs that did not make it
	 * in to the unconfigured state.
	 */
	if (drd_rcm_restore_cpu_notify(rsrcs, nrsrc) != 0) {
		rv = -1;
		goto done;
	}

	/* notify RCM about the decreased CPU capacity */
	if (drd_rcm_del_cpu_notify(rsrcs, nrsrc) != 0) {
		rv = -1;
	}

done:
	dump_cpu_rsrc_list("returning:", rsrcs, nrsrc);

	return (rv);
}

static int
drd_rcm_online_cpu_notify(drctl_rsrc_t *rsrcs, int nrsrc)
{
	char		**rlist;
	int		rv = 0;
	rcm_info_t	*rinfo;

	drd_dbg("drd_rcm_online_cpu_notify...");

	if ((rlist = drd_rcm_cpu_rlist_init(rsrcs, nrsrc,
	    DRCTL_STATUS_CONFIG_SUCCESS)) == NULL) {
		drd_dbg("  no CPUs were successfully added, nothing to do");
		return (0);
	}

	rcm_notify_online_list(rcm_hdl, rlist, 0, &rinfo);
	if (rv != RCM_SUCCESS) {
		drd_info("rcm_notify_online_list failed: %d", rv);
		rcm_free_info(rinfo);
		rv = -1;
	}

	drd_rcm_cpu_rlist_fini(rlist);

	return (rv);
}

static int
drd_rcm_add_cpu_notify(drctl_rsrc_t *rsrcs, int nrsrc)
{
	cpuid_t		*cpus = NULL;
	int		ncpus;
	int		rv = -1;
	cpuid_t		*oldcpus = NULL;
	cpuid_t		*newcpus = NULL;
	int		oldncpus = 0;
	int		newncpus = 0;
	nvlist_t	*nvl = NULL;
	int		idx;
	rcm_info_t	*rinfo;

	drd_dbg("drd_rcm_add_cpu_notify...");

	if ((rsrcs == NULL) || (nrsrc == 0)) {
		drd_err("add_cpu_notify: cpu list empty");
		goto done;
	}

	ncpus = nrsrc;
	cpus = (cpuid_t *)malloc(nrsrc * sizeof (cpuid_t));

	for (idx = 0; idx < nrsrc; idx++) {
		drd_dbg("  cpu[%d] = %d", idx, rsrcs[idx].res_cpu_id);
		cpus[idx] = rsrcs[idx].res_cpu_id;
	}

	/* allocate an nvlist for the RCM call */
	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
		goto done;

	/*
	 * Added CPU capacity, so newcpus is the current list
	 * of CPUs in the system.
	 */
	if (get_sys_cpuids(&newcpus, &newncpus) == -1)
		goto done;

	/*
	 * Since the operation added CPU capacity, the old CPU
	 * list is the new CPU list with the CPUs involved in
	 * the operation removed.
	 */
	oldcpus = (cpuid_t *)calloc(newncpus, sizeof (cpuid_t));
	if (oldcpus == NULL)
		goto done;

	for (idx = 0; idx < newncpus; idx++) {
		if (!is_cpu_in_list(newcpus[idx], cpus, ncpus))
			oldcpus[oldncpus++] = newcpus[idx];
	}

	/* dump pre and post lists */
	dump_cpu_list("oldcpus: ", oldcpus, oldncpus);
	dump_cpu_list("newcpus: ", newcpus, newncpus);
	dump_cpu_list("delta:   ", cpus, ncpus);

	/* setup the nvlist for the RCM call */
	if (nvlist_add_string(nvl, "state", "capacity") ||
	    nvlist_add_int32(nvl, "old_total", oldncpus) ||
	    nvlist_add_int32(nvl, "new_total", newncpus) ||
	    nvlist_add_int32_array(nvl, "old_cpu_list", oldcpus, oldncpus) ||
	    nvlist_add_int32_array(nvl, "new_cpu_list", newcpus, newncpus)) {
		goto done;
	}

	rv = rcm_notify_capacity_change(rcm_hdl, RCM_CPU_ALL, 0, nvl, &rinfo);
	rv = (rv == RCM_SUCCESS) ? 0 : -1;

done:
	s_nvfree(nvl);
	s_free(cpus);
	s_free(oldcpus);
	s_free(newcpus);

	return (rv);
}

static int
drd_rcm_del_cpu_request(drctl_rsrc_t *rsrcs, int nrsrc)
{
	cpuid_t		*cpus = NULL;
	int		ncpus;
	int		rv = -1;
	cpuid_t		*oldcpus = NULL;
	cpuid_t		*newcpus = NULL;
	int		oldncpus = 0;
	int		newncpus = 0;
	nvlist_t	*nvl = NULL;
	int		idx;
	rcm_info_t	*rinfo;

	drd_dbg("drd_rcm_del_cpu_request...");

	if ((rsrcs == NULL) || (nrsrc == 0)) {
		drd_err("del_cpu_request: cpu list empty");
		goto done;
	}

	ncpus = nrsrc;
	cpus = (cpuid_t *)malloc(nrsrc * sizeof (cpuid_t));

	for (idx = 0; idx < nrsrc; idx++) {
		cpus[idx] = rsrcs[idx].res_cpu_id;
	}

	/* allocate an nvlist for the RCM call */
	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
		goto done;
	}

	/*
	 * Removing CPU capacity, so oldcpus is the current
	 * list of CPUs in the system.
	 */
	if (get_sys_cpuids(&oldcpus, &oldncpus) == -1) {
		goto done;
	}

	/*
	 * Since this is a request to remove CPU capacity,
	 * the new CPU list is the old CPU list with the CPUs
	 * involved in the operation removed.
	 */
	newcpus = (cpuid_t *)calloc(oldncpus, sizeof (cpuid_t));
	if (newcpus == NULL) {
		goto done;
	}

	for (idx = 0; idx < oldncpus; idx++) {
		if (!is_cpu_in_list(oldcpus[idx], cpus, ncpus))
			newcpus[newncpus++] = oldcpus[idx];
	}

	/* dump pre and post lists */
	dump_cpu_list("oldcpus: ", oldcpus, oldncpus);
	dump_cpu_list("newcpus: ", newcpus, newncpus);
	dump_cpu_list("delta:   ", cpus, ncpus);

	/* setup the nvlist for the RCM call */
	if (nvlist_add_string(nvl, "state", "capacity") ||
	    nvlist_add_int32(nvl, "old_total", oldncpus) ||
	    nvlist_add_int32(nvl, "new_total", newncpus) ||
	    nvlist_add_int32_array(nvl, "old_cpu_list", oldcpus, oldncpus) ||
	    nvlist_add_int32_array(nvl, "new_cpu_list", newcpus, newncpus)) {
		goto done;
	}

	rv = rcm_request_capacity_change(rcm_hdl, RCM_CPU_ALL, 0, nvl, &rinfo);
	if (rv != RCM_SUCCESS) {
		drd_dbg("RCM call failed: %d", rv);
		/*
		 * Since the capcity change was blocked, we
		 * mark all CPUs as blocked. It is up to the
		 * user to reframe the query so that it can
		 * succeed.
		 */
		for (idx = 0; idx < nrsrc; idx++) {
			rsrcs[idx].status = DRCTL_STATUS_DENY;
		}

		/* tack on message to first resource */
		rsrcs[0].offset = (uintptr_t)strdup("unable to remove "
		    "specified number of CPUs");
		drd_dbg("  unable to remove specified number of CPUs");
		goto done;
	}

	rv = 0;

done:
	s_nvfree(nvl);
	s_free(cpus);
	s_free(oldcpus);
	s_free(newcpus);

	return (rv);
}

static int
drd_rcm_offline_cpu_request(drctl_rsrc_t *rsrcs, int nrsrc)
{
	char		**rlist;
	drctl_rsrc_t	*rsrc;
	int		idx;
	int		state;
	int		rv = 0;
	rcm_info_t	*rinfo = NULL;
	rcm_info_tuple_t *tuple = NULL;
	const char	*rsrcstr;
	const char	*errstr;

	drd_dbg("drd_rcm_offline_cpu_request...");

	if ((rlist = drd_rcm_cpu_rlist_init(rsrcs, nrsrc,
	    DRCTL_STATUS_INIT)) == NULL) {
		drd_err("unable to generate resource list");
		return (-1);
	}

	rv = rcm_request_offline_list(rcm_hdl, rlist, 0, &rinfo);
	if (rv == RCM_SUCCESS) {
		drd_dbg("RCM success, rinfo=%p", rinfo);
		goto done;
	}

	drd_dbg("RCM call failed (%d):", rv);

	/*
	 * Loop through the result of the operation and add
	 * any error messages to the resource structure.
	 */
	while ((tuple = rcm_info_next(rinfo, tuple)) != NULL) {

		/* find the resource of interest */
		rsrcstr = rcm_info_rsrc(tuple);
		rsrc = cpu_rsrcstr_to_rsrc(rsrcstr, rsrcs, nrsrc);

		if (rsrc == NULL) {
			drd_dbg("unable to find resource for %s", rsrcstr);
			continue;
		}

		errstr = rcm_info_error(tuple);

		if (errstr) {
			drd_dbg("  %s: '%s'", rsrcstr, errstr);
			rsrc->offset = (uintptr_t)strdup(errstr);
		}
	}

	rcm_free_info(rinfo);
	rv = 0;

done:
	/*
	 * Set the state of the resource based on the RCM
	 * state. CPUs in the offline state have the ok to
	 * proceed. All others have been blocked.
	 */
	for (idx = 0; rlist[idx] != NULL; idx++) {

		state = 0;
		rcm_get_rsrcstate(rcm_hdl, rlist[idx], &state);

		/* find the resource of interest */
		rsrc = cpu_rsrcstr_to_rsrc(rlist[idx], rsrcs, nrsrc);

		if (rsrc == NULL) {
			drd_dbg("unable to find resource for %s", rlist[idx]);
			continue;
		}

		rsrc->status = ((state == RCM_STATE_OFFLINE) ?
		    DRCTL_STATUS_ALLOW : DRCTL_STATUS_DENY);
	}

	drd_rcm_cpu_rlist_fini(rlist);

	return (rv);
}

static int
drd_rcm_remove_cpu_notify(drctl_rsrc_t *rsrcs, int nrsrc)
{
	char		**rlist;
	int		rv = 0;
	rcm_info_t	*rinfo;

	drd_dbg("drd_rcm_remove_cpu_notify...");

	if ((rlist = drd_rcm_cpu_rlist_init(rsrcs, nrsrc,
	    DRCTL_STATUS_CONFIG_SUCCESS)) == NULL) {
		drd_dbg("  no CPUs in the success state, nothing to do");
		return (0);
	}

	rv = rcm_notify_remove_list(rcm_hdl, rlist, 0, &rinfo);
	if (rv != RCM_SUCCESS) {
		drd_info("rcm_notify_remove_list failed: %d", rv);
		rcm_free_info(rinfo);
		rv = -1;
	}

	drd_rcm_cpu_rlist_fini(rlist);

	return (rv);
}

static int
drd_rcm_restore_cpu_notify(drctl_rsrc_t *rsrcs, int nrsrc)
{
	char		**rlist;
	char		**full_rlist;
	int		idx;
	int		ridx;
	int		state;
	int		rv = 0;
	rcm_info_t	*rinfo;

	drd_dbg("drd_rcm_restore_cpu_notify...");

	if ((full_rlist = drd_rcm_cpu_rlist_init(rsrcs, nrsrc,
	    DRCTL_STATUS_CONFIG_FAILURE)) == NULL) {
		drd_dbg("  no CPUs in the failed state, nothing to do");
		return (0);
	}

	/*
	 * Since the desired result of this operation is to
	 * restore resources to the online state, filter out
	 * the resources already in the online state before
	 * passing the list to RCM.
	 */

	/* allocate a zero filled array to ensure NULL terminated list */
	rlist = (char **)calloc((nrsrc + 1), sizeof (char *));
	if (rlist == NULL) {
		drd_err("calloc failed: %s", strerror(errno));
		rv = -1;
		goto done;
	}

	for (idx = 0, ridx = 0; full_rlist[idx] != NULL; idx++) {
		state = 0;
		rcm_get_rsrcstate(rcm_hdl, full_rlist[idx], &state);
		if (state != RCM_STATE_ONLINE) {
			rlist[ridx] = full_rlist[idx];
			ridx++;
		}
	}

	/* check if everything got filtered out */
	if (ridx == 0) {
		drd_dbg("  all CPUs already online, nothing to do");
		goto done;
	}

	rv = rcm_notify_online_list(rcm_hdl, rlist, 0, &rinfo);
	if (rv != RCM_SUCCESS) {
		drd_info("rcm_notify_online_list failed: %d", rv);
		rcm_free_info(rinfo);
		rv = -1;
	}

done:
	drd_rcm_cpu_rlist_fini(full_rlist);
	s_free(rlist);

	return (rv);
}

static int
drd_rcm_del_cpu_notify(drctl_rsrc_t *rsrcs, int nrsrc)
{
	cpuid_t		*cpus = NULL;
	int		rv = -1;
	cpuid_t		*oldcpus = NULL;
	cpuid_t		*newcpus = NULL;
	int		oldncpus = 0;
	int		newncpus = 0;
	nvlist_t	*nvl = NULL;
	int		idx;
	int		cidx;
	rcm_info_t	*rinfo;

	drd_dbg("drd_rcm_del_cpu_notify...");

	if ((rsrcs == NULL) || (nrsrc == 0)) {
		drd_err("del_cpu_notify: cpu list empty");
		goto done;
	}

	cpus = (cpuid_t *)malloc(nrsrc * sizeof (cpuid_t));

	/*
	 * Filter out the CPUs that could not be unconfigured.
	 */
	for (idx = 0, cidx = 0; idx < nrsrc; idx++) {
		if (rsrcs[idx].status != DRCTL_STATUS_CONFIG_SUCCESS)
			continue;
		drd_dbg("  cpu[%d] = %d", idx, rsrcs[idx].res_cpu_id);
		cpus[cidx] = rsrcs[idx].res_cpu_id;
		cidx++;
	}

	drd_dbg("  ncpus = %d", cidx);

	/* nothing to do */
	if (cidx == 0) {
		rv = 0;
		goto done;
	}

	/* allocate an nvlist for the RCM call */
	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
		goto done;
	}

	/*
	 * Removed CPU capacity, so newcpus is the current list
	 * of CPUs in the system.
	 */
	if (get_sys_cpuids(&newcpus, &newncpus) == -1) {
		goto done;
	}

	/*
	 * Since the operation removed CPU capacity, the old CPU
	 * list is the new CPU list with the CPUs involved in
	 * the operation added.
	 */
	oldcpus = (cpuid_t *)calloc(newncpus + cidx, sizeof (cpuid_t));
	if (oldcpus == NULL) {
		goto done;
	}

	for (idx = 0; idx < newncpus; idx++) {
		if (!is_cpu_in_list(newcpus[idx], cpus, cidx))
			oldcpus[oldncpus++] = newcpus[idx];
	}

	for (idx = 0; idx < cidx; idx++) {
		oldcpus[oldncpus++] = cpus[idx];
	}

	/* dump pre and post lists */
	dump_cpu_list("oldcpus: ", oldcpus, oldncpus);
	dump_cpu_list("newcpus: ", newcpus, newncpus);
	dump_cpu_list("delta:   ", cpus, cidx);

	/* setup the nvlist for the RCM call */
	if (nvlist_add_string(nvl, "state", "capacity") ||
	    nvlist_add_int32(nvl, "old_total", oldncpus) ||
	    nvlist_add_int32(nvl, "new_total", newncpus) ||
	    nvlist_add_int32_array(nvl, "old_cpu_list", oldcpus, oldncpus) ||
	    nvlist_add_int32_array(nvl, "new_cpu_list", newcpus, newncpus)) {
		goto done;
	}

	rv = rcm_notify_capacity_change(rcm_hdl, RCM_CPU_ALL, 0, nvl, &rinfo);
	rv = (rv == RCM_SUCCESS) ? 0 : -1;

done:
	s_nvfree(nvl);
	s_free(cpus);
	s_free(oldcpus);
	s_free(newcpus);

	return (rv);
}

/*
 * Given a list of resource structures, create a list of CPU
 * resource strings formatted as expected by RCM. Only resources
 * that are in the state specified by the status argument are
 * included in the resulting list.
 */
static char **
drd_rcm_cpu_rlist_init(drctl_rsrc_t *rsrcs, int nrsrc, int status)
{
	char	rbuf[RCM_CPU_MAX_LEN];
	char	**rlist;
	int	idx;
	int	ridx;

	drd_dbg("drd_rcm_cpu_rlist_init...");

	if ((rsrcs == NULL) || (nrsrc == 0)) {
		drd_dbg("cpu list is empty");
		return (NULL);
	}

	/* allocate a zero filled array to ensure NULL terminated list */
	rlist = (char **)calloc((nrsrc + 1), sizeof (char *));
	if (rlist == NULL) {
		drd_err("calloc failed: %s", strerror(errno));
		return (NULL);
	}

	for (idx = 0, ridx = 0; idx < nrsrc; idx++) {

		drd_dbg("  checking cpu %d, status=%d, expected status=%d",
		    rsrcs[idx].res_cpu_id, rsrcs[idx].status, status);

		/*
		 * Filter out the CPUs that are not in
		 * the requested state.
		 */
		if (rsrcs[idx].status != status)
			continue;

		/* generate the resource string */
		(void) sprintf(rbuf, "%s%d", RCM_CPU, rsrcs[idx].res_cpu_id);

		rlist[ridx] = strdup(rbuf);
		if (rlist[ridx] == NULL) {
			drd_err("strdup failed: %s", strerror(errno));
			drd_rcm_cpu_rlist_fini(rlist);
			return (NULL);
		}

		ridx++;
	}

	/* cleanup if the list is empty */
	if (ridx == 0) {
		s_free(rlist);
	}

	drd_dbg("final rlist:");
	dump_cpu_rlist(rlist);

	return (rlist);
}

static void
drd_rcm_cpu_rlist_fini(char **rlist)
{
	int idx;

	drd_dbg("drd_rcm_cpu_rlist_fini...");

	dump_cpu_rlist(rlist);

	for (idx = 0; rlist[idx] != NULL; idx++) {
		s_free(rlist[idx]);
	}

	s_free(rlist);
}

/*
 * Convert an RCM CPU resource string into a numerical cpuid.
 * Assumes the resource string has the form: "SUNW_cpu/cpu<C>"
 * where "<C>" is the numerical cpuid of interest.
 */
static cpuid_t
cpu_rsrcstr_to_cpuid(const char *rsrc)
{
	char	*cpuid_off;
	cpuid_t	cpuid;

	/*
	 * Search for the last occurrance of 'u' in the
	 * expected RCM resource string "SUNW_cpu/cpu<C>".
	 * This will give a pointer to the cpuid portion.
	 */
	cpuid_off = strrchr(rsrc, 'u');
	cpuid_off++;

	cpuid = atoi(cpuid_off);

	return (cpuid);
}

/*
 * Given an RCM CPU resource string, return a pointer to the
 * corresponding resource structure from the given resource list.
 * NULL is returned if no matching resource structure can be
 * found.
 */
static drctl_rsrc_t *
cpu_rsrcstr_to_rsrc(const char *rsrcstr, drctl_rsrc_t *rsrcs, int nrsrc)
{
	cpuid_t	cpuid;
	int	idx;

	cpuid = cpu_rsrcstr_to_cpuid(rsrcstr);

	for (idx = 0; idx < nrsrc; idx++) {
		if (rsrcs[idx].res_cpu_id == cpuid)
			return (&rsrcs[idx]);
	}

	return (NULL);
}

static int
get_sys_cpuids(cpuid_t **cpuids, int *ncpuids)
{
	int		ncpu = 0;
	int		maxncpu;
	kstat_t		*ksp;
	kstat_ctl_t	*kc = NULL;
	cpuid_t		*cp;

	drd_dbg("get_sys_cpuids...");

	if ((maxncpu = sysconf(_SC_NPROCESSORS_MAX)) == -1)
		return (-1);

	if ((kc = kstat_open()) == NULL)
		return (-1);

	if ((cp = (cpuid_t *)calloc(maxncpu, sizeof (cpuid_t))) == NULL) {
		(void) kstat_close(kc);
		return (-1);
	}

	for (ksp = kc->kc_chain; ksp != NULL; ksp = ksp->ks_next) {
		if (strcmp(ksp->ks_module, "cpu_info") == 0)
			cp[ncpu++] = ksp->ks_instance;
	}

	dump_cpu_list("syscpus: ", cp, ncpu);

	(void) kstat_close(kc);

	*cpuids = cp;
	*ncpuids = ncpu;

	return (0);
}

static boolean_t
is_cpu_in_list(cpuid_t cpuid, cpuid_t *list, int len)
{
	int idx;

	if (list == NULL)
		return (B_FALSE);

	for (idx = 0; idx < len; idx++) {
		if (list[idx] == cpuid)
			return (B_TRUE);
	}

	return (B_FALSE);
}

#define	CPUIDS_PER_LINE		16
#define	LINEWIDTH		(2 * (CPUIDS_PER_LINE * 4))

static void
dump_cpu_list(char *prefix, cpuid_t *cpuids, int ncpuids)
{
	char	line[LINEWIDTH];
	char	*curr;
	int	i, j;

	/* return if not debugging */
	if (drd_debug == 0)
		return;

	/* print just the prefix if CPU list is empty */
	if (ncpuids == 0) {
		if (prefix)
			drd_dbg("%s", prefix);
		return;
	}

	for (i = 0; i < ncpuids; i += CPUIDS_PER_LINE) {

		bzero(line, LINEWIDTH);
		curr = line;

		/* start with the prefix */
		(void) sprintf(curr, "%s", (prefix) ? prefix : "");
		curr = line + strlen(line);

		/* format the CPUs for this line */
		for (j = 0; (j < CPUIDS_PER_LINE) && ((i + j) < ncpuids); j++) {
			(void) sprintf(curr, "%3d ", cpuids[i + j]);
			curr = line + strlen(line);
		}

		drd_dbg("%s", line);
	}
}

static void
dump_cpu_rsrc_list(char *prefix, drctl_rsrc_t *rsrcs, int nrsrc)
{
	int	idx;
	char	*errstr;

	/* just return if not debugging */
	if (drd_debug == 0)
		return;

	if (prefix)
		drd_dbg("%s", prefix);

	for (idx = 0; idx < nrsrc; idx++) {

		/* get a pointer to the error string */
		errstr = (char *)(uintptr_t)rsrcs[idx].offset;

		drd_dbg("  cpu[%d]: cpuid=%d, status=%d, errstr='%s'", idx,
		    rsrcs[idx].res_cpu_id, rsrcs[idx].status,
		    (errstr != NULL) ? errstr : "");
	}
}

static void
dump_cpu_rlist(char **rlist)
{
	int	idx;
	int	state;

	static char *rcm_state_str[] = {
		"UNKNOWN",		"ONLINE",		"ONLINING",
		"OFFLINE_FAIL",		"OFFLINING",		"OFFLINE",
		"REMOVING",		"INVALID_7",		"INVALID_8",
		"INVALID_9",		"RESUMING",		"SUSPEND_FAIL",
		"SUSPENDING",		"SUSPEND",		"REMOVE",
		"OFFLINE_QUERYING",	"OFFLINE_QUERY_FAIL",	"OFFLINE_QUERY",
		"SUSPEND_QUERYING",	"SUSPEND_QUERY_FAIL",	"SUSPEND_QUERY"
	};

	/* just return if not debugging */
	if (drd_debug == 0)
		return;

	if (rlist == NULL) {
		drd_dbg("  empty rlist");
		return;
	}

	for (idx = 0; rlist[idx] != NULL; idx++) {
		state = 0;
		rcm_get_rsrcstate(rcm_hdl, rlist[idx], &state);
		drd_dbg("  rlist[%d]: rsrc=%s, state=%-2d (%s)", idx,
		    rlist[idx], state, rcm_state_str[state]);
	}
}