summaryrefslogtreecommitdiff
path: root/usr/src/cmd/dcs/sparc/sun4u/ri_init.c
blob: 164140fa7cca7d426aa7d141a021558bce9dda8c (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
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
/*
 * 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 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * Implementation of ri_init routine for obtaining mapping
 * of system board attachment points to physical devices and to
 * the Reconfiguration Coordination Manager (RCM) client usage
 * of these devices.
 */
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <kstat.h>
#include <sys/param.h>
#include <sys/sbd_ioctl.h>
#include "rsrc_info_impl.h"

/*
 * Occupant types exported by cfgadm sbd plugin via
 * config_admin(3CFGADM).
 */
#define	SBD_CM_CPU	"cpu"
#define	SBD_CM_MEM	"memory"
#define	SBD_CM_IO	"io"

/*
 * RCM abstract resource names.
 */
#define	RCM_MEM_ALL	"SUNW_memory"
#define	RCM_CPU_ALL	"SUNW_cpu"
#define	RCM_CPU		RCM_CPU_ALL"/cpu"

#define	KBYTE		1024
#define	MBYTE		1048576
#define	USAGE_ALLOC_SIZE	128

/*
 * define to allow io_cm_info to return NODE is NULL to ri_init,
 * in order to skip over nodes w/unattached drivers
 */
#define	RI_NODE_NIL	1

/*
 * This code is CMP aware as it parses the
 * cfgadm info field for individual cpuids.
 */
#define	CPUID_SEP	","
#define	CPU_INFO_FMT	"cpuid=%s speed=%d ecache=%d"

typedef struct {
	cfga_list_data_t *cfga_list_data;
	int		nlist;
} apd_t;

typedef struct {
	long		pagesize;
	long		syspages;
	long		sysmb;
} mem_stat_t;

#define	ms_syspages	m_stat.syspages
#define	ms_pagesize	m_stat.pagesize
#define	ms_sysmb	m_stat.sysmb

typedef int32_t		cpuid_t;

typedef struct {
	int	cpuid_max;	/* maximum cpuid value */
	int	ecache_curr;	/* cached during tree walk */
	int	*ecache_sizes;	/* indexed by cpuid */
} ecache_info_t;

typedef struct {
	rcm_handle_t	*hdl;
	rcm_info_t	*offline_query_info;
	char		**rlist;
	int		nrlist;
	cpuid_t		*cpus;
	int		ncpus;
	int		ndevs;
	uint_t		query_pages;
	mem_stat_t	m_stat;
	ecache_info_t	ecache_info;
} rcmd_t;

typedef struct {
	const char	*rsrc;
	const char	*info;
} usage_t;

/* Lookup table entry for matching IO devices to RCM resource usage */
typedef struct {
	int		index;		/* index into the table array */
	di_node_t	node;		/* associated devinfo node */
	char		*name;		/* device full path name */
	int		n_usage;
	usage_t		*usage;
} lookup_entry_t;

typedef struct {
	int		n_entries;
	int		n_slots;
	lookup_entry_t	*table;
} lookup_table_t;

typedef struct {
	int			err;
	di_node_t		node;
	char			*pathbuf;
	lookup_table_t		*table;
	di_devlink_handle_t	linkhd;
} devinfo_arg_t;

static int dyn_ap_ids(char *, cfga_list_data_t **, int *);
static int rcm_init(rcmd_t *, apd_t [], int, int);
static void rcm_fini(rcmd_t *);
static int rcm_query_init(rcmd_t *, apd_t [], int);
static int cap_request(ri_hdl_t *, rcmd_t *);
static int syscpus(cpuid_t **, int *);
static int cpu_cap_request(ri_hdl_t *, rcmd_t *);
static int mem_cap_request(ri_hdl_t *, rcmd_t *);
static int (*cm_rcm_qpass_func(cfga_type_t))(cfga_list_data_t *, rcmd_t *);
static int cpu_rcm_qpass(cfga_list_data_t *, rcmd_t *);
static int mem_rcm_qpass(cfga_list_data_t *, rcmd_t *);
static int io_rcm_qpass(cfga_list_data_t *, rcmd_t *);
static int (*cm_info_func(cfga_type_t))(ri_ap_t *, cfga_list_data_t *, int,
    rcmd_t *);
static int cpu_cm_info(ri_ap_t *, cfga_list_data_t *, int, rcmd_t *);
static int i_cpu_cm_info(processorid_t, int, int, ri_ap_t *, rcmd_t *);
static int mem_cm_info(ri_ap_t *, cfga_list_data_t *, int, rcmd_t *);
static int io_cm_info(ri_ap_t *, cfga_list_data_t *, int, rcmd_t *);
static int ident_leaf(di_node_t);
static int mk_drv_inst(di_node_t, char [], char *);
static int devinfo_node_walk(di_node_t, void *);
static int devinfo_minor_walk(di_node_t, di_minor_t, void *);
static int devinfo_devlink_walk(di_devlink_t, void *);
static int add_rcm_clients(ri_client_t **, rcmd_t *, rcm_info_t *, int, int *);
static int rcm_ignore(char *, char *);
static int add_query_state(rcmd_t *, ri_client_t *, const char *, const char *);
static int state2query(int);
static void dev_list_append(ri_dev_t **, ri_dev_t *);
static void dev_list_cpu_insert(ri_dev_t **, ri_dev_t *, processorid_t);
static rcm_info_tuple_t *tuple_lookup(rcmd_t *, const char *, const char *);
static ri_ap_t *ri_ap_alloc(char *, ri_hdl_t *);
static ri_dev_t *ri_dev_alloc(void);
static ri_dev_t *io_dev_alloc(char *);
static ri_client_t *ri_client_alloc(char *, char *);
static void apd_tbl_free(apd_t [], int);
static char *pstate2str(int);
static int ecache_info_init(ecache_info_t *);
static int find_cpu_nodes(di_node_t, void *);
static int prop_lookup_int(di_node_t, di_prom_handle_t, char *, int **);
static int add_lookup_entry(lookup_table_t *, const char *, di_node_t);
static int table_compare_names(const void *, const void *);
static int table_compare_indices(const void *, const void *);
static lookup_entry_t *lookup(lookup_table_t *table, const char *);
static int add_usage(lookup_entry_t *, const char *, rcm_info_tuple_t *);
static void empty_table(lookup_table_t *);

#ifdef DEBUG
static void		dump_apd_tbl(FILE *, apd_t *, int);
#endif /* DEBUG */

static struct {
	char	*type;
	int	(*cm_info)(ri_ap_t *, cfga_list_data_t *, int, rcmd_t *);
	int	(*cm_rcm_qpass)(cfga_list_data_t *, rcmd_t *);
} cm_ctl[] = {
	{SBD_CM_CPU,	cpu_cm_info,	cpu_rcm_qpass},
	{SBD_CM_MEM,	mem_cm_info,	mem_rcm_qpass},
	{SBD_CM_IO,	io_cm_info,	io_rcm_qpass}
};

/*
 * Table of known info string prefixes for RCM modules that do not
 * represent actual resource usage, but instead provide name translations
 * or sequencing within the RCM namespace. Since RCM provides no way to
 * filter these out, we must maintain this hack.
 */
static char *rcm_info_filter[] = {
	"Network interface",		/* Network naming module */
	NULL
};


/*
 * Allocate snapshot handle.
 */
int
ri_init(int n_apids, char **ap_ids, int flags, ri_hdl_t **hdlp)
{
	int			i, j;
	ri_hdl_t		*ri_hdl;
	ri_ap_t			*ap_hdl;
	rcmd_t			*rcm = NULL;
	cfga_list_data_t	*cfga_ldata;
	apd_t			*apd, *apd_tbl = NULL;
	int			(*cm_info)(ri_ap_t *, cfga_list_data_t *,
				    int, rcmd_t *);
	int			rv = RI_SUCCESS;
	int			cm_info_rv;

	if (n_apids <= 0 || ap_ids == NULL || hdlp == NULL)
		return (RI_INVAL);

	if (flags & ~RI_REQ_MASK)
		return (RI_NOTSUP);

	*hdlp = NULL;
	if ((ri_hdl = calloc(1, sizeof (*ri_hdl))) == NULL ||
	    (rcm = calloc(1, sizeof (*rcm))) == NULL ||
	    (apd_tbl = calloc(n_apids, sizeof (*apd_tbl))) == NULL) {
		dprintf((stderr, "calloc: %s\n", strerror(errno)));
		rv = RI_FAILURE;
		goto out;
	}

	/*
	 * Create mapping of boards to components.
	 */
	for (i = 0, apd = apd_tbl; i < n_apids; i++, apd++) {
		if (dyn_ap_ids(ap_ids[i], &apd->cfga_list_data,
		    &apd->nlist) == -1) {
			rv = RI_INVAL;
			goto out;
		}
	}
#ifdef DEBUG
	dump_apd_tbl(stderr, apd_tbl, n_apids);
#endif /* DEBUG */

	if (rcm_init(rcm, apd_tbl, n_apids, flags) != 0) {
		rv = RI_FAILURE;
		goto out;
	}

	/*
	 * Best effort attempt to read cpu ecache sizes from
	 * OBP/Solaris device trees. These are later looked up
	 * in i_cpu_cm_info().
	 */
	(void) ecache_info_init(&rcm->ecache_info);

	for (i = 0, apd = apd_tbl; i < n_apids; i++, apd++) {
		if ((ap_hdl = ri_ap_alloc(ap_ids[i], ri_hdl)) == NULL) {
			rv = RI_FAILURE;
			goto out;
		}

		/*
		 * Add component info based on occupant type. Note all
		 * passes through the apd table skip over the first
		 * cfgadm_list_data entry, which is the static system board
		 * attachment point.
		 */
		for (j = 1, cfga_ldata = &apd->cfga_list_data[1];
		    j < apd->nlist; j++, cfga_ldata++) {
			if (cfga_ldata->ap_o_state != CFGA_STAT_CONFIGURED) {
				continue;
			}

			if ((cm_info =
			    cm_info_func(cfga_ldata->ap_type)) != NULL) {
				cm_info_rv =
				    (*cm_info)(ap_hdl, cfga_ldata, flags, rcm);
				if (cm_info_rv != 0) {
					/*
					 * If we cannot obtain info for the ap,
					 * skip it and do not fail the entire
					 * operation.  This case occurs when the
					 * driver for a device is not attached:
					 * di_init() returns failed back to
					 * io_cm_info().
					 */
					if (cm_info_rv == RI_NODE_NIL)
						continue;
					else {
						rv = RI_FAILURE;
						goto out;
					}
				}
			}
		}
	}

	if ((flags & RI_INCLUDE_QUERY) && cap_request(ri_hdl, rcm) != 0)
		rv = RI_FAILURE;

out:
	if (apd_tbl != NULL)
		apd_tbl_free(apd_tbl, n_apids);
	if (rcm != NULL)
		rcm_fini(rcm);

	if (rv == RI_SUCCESS)
		*hdlp = ri_hdl;
	else
		ri_fini(ri_hdl);

	return (rv);
}

/*
 * Map static board attachment point to dynamic attachment points (components).
 */
static int
dyn_ap_ids(char *ap_id, cfga_list_data_t **ap_id_list, int *nlist)
{
	cfga_err_t	cfga_err;
	char		*errstr;
	char		*opts = "parsable";
	char		*listops = "class=sbd";

	cfga_err = config_list_ext(1, &ap_id, ap_id_list, nlist,
	    opts, listops, &errstr, CFGA_FLAG_LIST_ALL);
	if (cfga_err != CFGA_OK) {
		dprintf((stderr, "config_list_ext: %s\n",
		    config_strerror(cfga_err)));
		return (-1);
	}

	return (0);
}

/*
 * Initialize rcm handle, memory stats. Cache query result if necessary.
 */
static int
rcm_init(rcmd_t *rcm, apd_t apd_tbl[], int napds, int flags)
{
	longlong_t	ii;
	int		rv = 0;

	rcm->offline_query_info = NULL;
	rcm->rlist = NULL;
	rcm->cpus = NULL;

	if (rcm_alloc_handle(NULL, RCM_NOPID, NULL, &rcm->hdl) != RCM_SUCCESS) {
		dprintf((stderr, "rcm_alloc_handle (errno=%d)\n", errno));
		return (-1);
	}

	if ((rcm->ms_pagesize = sysconf(_SC_PAGE_SIZE)) == -1 ||
	    (rcm->ms_syspages = sysconf(_SC_PHYS_PAGES)) == -1) {
		dprintf((stderr, "sysconf: %s\n", strerror(errno)));
		return (-1);
	}
	ii = (longlong_t)rcm->ms_pagesize * rcm->ms_syspages;
	rcm->ms_sysmb = (int)((ii+MBYTE-1) / MBYTE);

	if (flags & RI_INCLUDE_QUERY)
		rv = rcm_query_init(rcm, apd_tbl, napds);

	return (rv);
}

static void
rcm_fini(rcmd_t *rcm)
{
	char	**cpp;

	assert(rcm != NULL);

	if (rcm->offline_query_info != NULL)
		rcm_free_info(rcm->offline_query_info);
	if (rcm->hdl != NULL)
		rcm_free_handle(rcm->hdl);

	if (rcm->rlist != NULL) {
		for (cpp = rcm->rlist; *cpp != NULL; cpp++)
			s_free(*cpp);
		free(rcm->rlist);
	}

	s_free(rcm->cpus);
	free(rcm);
}

#define	NODENAME_CMP		"cmp"
#define	NODENAME_SSM		"ssm"
#define	PROP_CPUID		"cpuid"
#define	PROP_DEVICE_TYPE	"device-type"
#define	PROP_ECACHE_SIZE	"ecache-size"
#define	PROP_L2_CACHE_SIZE	"l2-cache-size"
#define	PROP_L3_CACHE_SIZE	"l3-cache-size"

typedef struct {
	di_node_t		root;
	di_prom_handle_t	ph;
	ecache_info_t		*ecache_info;
} di_arg_t;

/*
 * The ecache sizes for individual cpus are read from the
 * OBP/Solaris device trees. This info cannot be derived
 * from the cfgadm_sbd cpu attachment point ecache info,
 * which may be a sum of multiple cores for CMP.
 */
static int
ecache_info_init(ecache_info_t *ec)
{
	di_arg_t	di_arg;
	di_prom_handle_t ph = DI_PROM_HANDLE_NIL;
	di_node_t	root = DI_NODE_NIL;
	int		cpuid_max, rv = 0;

	assert(ec != NULL && ec->cpuid_max == 0 && ec->ecache_sizes == NULL);

	if ((cpuid_max = sysconf(_SC_CPUID_MAX)) == -1) {
		dprintf((stderr, "sysconf fail: %s\n", strerror(errno)));
		rv = -1;
		goto done;
	}

	if ((root = di_init("/", DINFOCPYALL)) == DI_NODE_NIL) {
		dprintf((stderr, "di_init fail: %s\n", strerror(errno)));
		rv = -1;
		goto done;
	}

	if ((ph = di_prom_init()) == DI_PROM_HANDLE_NIL) {
		dprintf((stderr, "di_prom_init fail: %s\n", strerror(errno)));
		rv = -1;
		goto done;
	}

	if ((ec->ecache_sizes = calloc(cpuid_max + 1, sizeof (int))) == NULL) {
		dprintf((stderr, "calloc fail: %s\n", strerror(errno)));
		rv = -1;
		goto done;
	}
	ec->cpuid_max = cpuid_max;

	dprintf((stderr, "cpuid_max is set to %d\n", ec->cpuid_max));

	di_arg.ph = ph;
	di_arg.root = root;
	di_arg.ecache_info = ec;

	if (di_walk_node(root, DI_WALK_CLDFIRST, (void *)&di_arg,
	    find_cpu_nodes) != 0) {
		dprintf((stderr, "di_walk_node fail: %s\n", strerror(errno)));
		rv = -1;
	}

done:
	if (root != DI_NODE_NIL)
		di_fini(root);
	if (ph != DI_PROM_HANDLE_NIL)
		di_prom_fini(ph);

	return (rv);
}

/*
 * Libdevinfo node walk callback for reading ecache size
 * properties for cpu device nodes. Subtrees not containing
 * cpu nodes are filtered out.
 */
static int
find_cpu_nodes(di_node_t node, void *arg)
{
	char			*name;
	int			*cpuid, *ecache;
	di_arg_t		*di_arg = (di_arg_t *)arg;
	ecache_info_t		*ec = di_arg->ecache_info;
	di_prom_handle_t	ph = di_arg->ph;
	int			walk_child = 0;

	if (node == DI_NODE_NIL) {
		return (DI_WALK_TERMINATE);
	}

	if (node == di_arg->root) {
		return (DI_WALK_CONTINUE);
	}

	if (di_nodeid(node) == DI_PSEUDO_NODEID) {
		return (DI_WALK_PRUNECHILD);
	}

	name = di_node_name(node);
	if (name != NULL) {
		/*
		 * CMP nodes will be the parent of cpu nodes. On some platforms,
		 * cpu nodes will be under the ssm node. In either case,
		 * continue searching this subtree.
		 */
		if (strncmp(name, NODENAME_SSM, strlen(NODENAME_SSM)) == 0 ||
		    strncmp(name, NODENAME_CMP, strlen(NODENAME_CMP)) == 0) {
			return (DI_WALK_CONTINUE);
		}
	}

	dprintf((stderr, "find_cpu_nodes: node=%p, name=%s, binding_name=%s\n",
	    node, di_node_name(node), di_binding_name(node)));

	/*
	 * Ecache size property name differs with processor implementation.
	 * Panther has both L2 and L3, so check for L3 first to differentiate
	 * from Jaguar, which has only L2.
	 */
	if (prop_lookup_int(node, ph, PROP_ECACHE_SIZE, &ecache) == 0 ||
	    prop_lookup_int(node, ph, PROP_L3_CACHE_SIZE, &ecache) == 0 ||
	    prop_lookup_int(node, ph, PROP_L2_CACHE_SIZE, &ecache) == 0) {
		/*
		 * On some platforms the cache property is in the core
		 * node while the cpuid is in the child cpu node.  It may
		 * be needed while processing this node or a child node.
		 */
		ec->ecache_curr = *ecache;
		walk_child = 1;
	}

	if (prop_lookup_int(node, ph, PROP_CPUID, &cpuid) == 0) {

		assert(ec != NULL && ec->ecache_sizes != NULL &&
		    *cpuid <= ec->cpuid_max);

		if (ec->ecache_curr != 0) {
			ec->ecache_sizes[*cpuid] = ec->ecache_curr;

		}
	}

	return (walk_child ? DI_WALK_CONTINUE : DI_WALK_PRUNECHILD);
}

/*
 * Given a di_node_t, call the appropriate int property lookup routine.
 * Note: This lookup fails if the int property has multiple value entries.
 */
static int
prop_lookup_int(di_node_t node, di_prom_handle_t ph, char *propname, int **ival)
{
	int rv;

	rv = (di_nodeid(node) == DI_PROM_NODEID) ?
	    di_prom_prop_lookup_ints(ph, node, propname, ival) :
	    di_prop_lookup_ints(DDI_DEV_T_ANY, node, propname, ival);

	return (rv == 1 ? 0 : -1);
}

/*
 * For offline queries, RCM must be given a list of all resources
 * so modules can have access to the full scope of the operation.
 * The rcm_get_info calls are made individually in order to map the
 * returned rcm_info_t's to physical devices. The rcm_request_offline
 * result is cached so the query state can be looked up as we process
 * the rcm_get_info calls. This routine also tallies up the amount of
 * memory going away and creates a list of cpu ids to be used
 * later for rcm_request_capacity_change.
 */
static int
rcm_query_init(rcmd_t *rcm, apd_t apd_tbl[], int napds)
{
	apd_t			*apd;
	int 			i, j;
	cfga_list_data_t	*cfga_ldata;
	int			(*cm_rcm_qpass)(cfga_list_data_t *, rcmd_t *);
#ifdef DEBUG
	char			**cpp;
#endif /* DEBUG */

	/*
	 * Initial pass to size cpu and resource name arrays needed to
	 * interface with RCM. Attachment point ids for CMP can represent
	 * multiple cpus (and resource names). Instead of parsing the
	 * cfgadm info field here, use the worse case that all component
	 * attachment points are CMP.
	 */
	rcm->ndevs = 0;
	for (i = 0, apd = apd_tbl; i < napds; i++, apd++) {
		for (j = 1, cfga_ldata = &apd->cfga_list_data[1];
		    j < apd->nlist; j++, cfga_ldata++) {
			if (cfga_ldata->ap_o_state != CFGA_STAT_CONFIGURED) {
				continue;
			}
			rcm->ndevs += SBD_MAX_CORES_PER_CMP;
		}
	}

	/* account for trailing NULL in rlist */
	if (rcm->ndevs > 0 &&
	    ((rcm->cpus = calloc(rcm->ndevs, sizeof (cpuid_t))) == NULL ||
	    (rcm->rlist = calloc(rcm->ndevs + 1, sizeof (char *))) == NULL)) {
		dprintf((stderr, "calloc: %s\n", strerror(errno)));
		return (-1);
	}

	/*
	 * Second pass to fill in the RCM resource and cpu lists.
	 */
	for (i = 0, apd = apd_tbl; i < napds; i++, apd++) {
		for (j = 1, cfga_ldata = &apd->cfga_list_data[1];
		    j < apd->nlist; j++, cfga_ldata++) {
			if (cfga_ldata->ap_o_state != CFGA_STAT_CONFIGURED) {
				continue;
			}
			if ((cm_rcm_qpass =
			    cm_rcm_qpass_func(cfga_ldata->ap_type)) != NULL &&
			    (*cm_rcm_qpass)(cfga_ldata, rcm) != 0) {
				return (-1);
			}
		}
	}

	if (rcm->nrlist == 0)
		return (0);

	/*
	 * Cache query result. Since we are only interested in the
	 * set of RCM clients processed and not their request status,
	 * the return value is irrelevant.
	 */
	(void) rcm_request_offline_list(rcm->hdl, rcm->rlist,
	    RCM_QUERY|RCM_SCOPE, &rcm->offline_query_info);

#ifdef DEBUG
	dprintf((stderr, "RCM rlist: nrlist=%d\n", rcm->nrlist));
	for (cpp = rcm->rlist, i = 0; *cpp != NULL; cpp++, i++) {
		dprintf((stderr, "rlist[%d]=%s\n", i, *cpp));
	}
#endif /* DEBUG */

	return (0);
}

static int
cap_request(ri_hdl_t *ri_hdl, rcmd_t *rcm)
{
	return (((rcm->ncpus > 0 && cpu_cap_request(ri_hdl, rcm) != 0) ||
	    (rcm->query_pages > 0 && mem_cap_request(ri_hdl, rcm) != 0)) ?
	    -1 : 0);
}

/*
 * RCM capacity change request for cpus.
 */
static int
cpu_cap_request(ri_hdl_t *ri_hdl, rcmd_t *rcm)
{
	cpuid_t		*syscpuids, *newcpuids;
	int		sysncpus, newncpus;
	rcm_info_t	*rcm_info = NULL;
	int		i, j, k;
	nvlist_t	*nvl;
	int		rv = 0;

	/* get all cpus in the system */
	if (syscpus(&syscpuids, &sysncpus) == -1)
		return (-1);

	newncpus = sysncpus - rcm->ncpus;
	if ((newcpuids = calloc(newncpus, sizeof (cpuid_t))) == NULL) {
		dprintf((stderr, "calloc: %s", strerror(errno)));
		rv = -1;
		goto out;
	}

	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
		dprintf((stderr, "nvlist_alloc fail\n"));
		rv = -1;
		goto out;
	}

	/*
	 * Construct the new cpu list.
	 */
	for (i = 0, j = 0; i < sysncpus; i++) {
		for (k = 0; k < rcm->ncpus; k++) {
			if (rcm->cpus[k] == syscpuids[i]) {
				break;
			}
		}
		if (k == rcm->ncpus) {
			newcpuids[j++] = syscpuids[i];
		}
	}

	if (nvlist_add_int32(nvl, "old_total", sysncpus) != 0 ||
	    nvlist_add_int32(nvl, "new_total", newncpus) != 0 ||
	    nvlist_add_int32_array(nvl, "old_cpu_list", syscpuids,
	    sysncpus) != 0 ||
	    nvlist_add_int32_array(nvl, "new_cpu_list", newcpuids,
	    newncpus) != 0) {
		dprintf((stderr, "nvlist_add fail\n"));
		rv = -1;
		goto out;
	}

#ifdef DEBUG
	dprintf((stderr, "old_total=%d\n", sysncpus));
	for (i = 0; i < sysncpus; i++) {
		dprintf((stderr, "old_cpu_list[%d]=%d\n", i, syscpuids[i]));
	}
	dprintf((stderr, "new_total=%d\n", newncpus));
	for (i = 0; i < newncpus; i++) {
		dprintf((stderr, "new_cpu_list[%d]=%d\n", i, newcpuids[i]));
	}
#endif /* DEBUG */

	(void) rcm_request_capacity_change(rcm->hdl, RCM_CPU_ALL,
	    RCM_QUERY|RCM_SCOPE, nvl, &rcm_info);

	rv = add_rcm_clients(&ri_hdl->cpu_cap_clients, rcm, rcm_info, 0, NULL);

out:
	s_free(syscpuids);
	s_free(newcpuids);
	nvlist_free(nvl);
	if (rcm_info != NULL)
		rcm_free_info(rcm_info);

	return (rv);
}

static int
syscpus(cpuid_t **cpuids, int *ncpus)
{
	kstat_t		*ksp;
	kstat_ctl_t	*kc;
	cpuid_t		*cp;
	int		i;

	if ((*ncpus = sysconf(_SC_NPROCESSORS_CONF)) == -1) {
		dprintf((stderr, "sysconf: %s\n", errno));
		return (-1);
	}

	if ((kc = kstat_open()) == NULL) {
		dprintf((stderr, "kstat_open fail\n"));
		return (-1);
	}

	if ((cp = calloc(*ncpus, sizeof (cpuid_t))) == NULL) {
		dprintf((stderr, "calloc: %s\n", errno));
		(void) kstat_close(kc);
		return (-1);
	}

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

	(void) kstat_close(kc);
	*cpuids = cp;

	return (0);
}

/*
 * RCM capacity change request for memory.
 */
static int
mem_cap_request(ri_hdl_t *ri_hdl, rcmd_t *rcm)
{
	nvlist_t	*nvl;
	rcm_info_t	*rcm_info = NULL;
	long 		newpages;
	int		rv = 0;

	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
		dprintf((stderr, "nvlist_alloc fail\n"));
		return (-1);
	}

	newpages = rcm->ms_syspages - rcm->query_pages;
	if (nvlist_add_int32(nvl, "page_size", rcm->ms_pagesize) != 0 ||
	    nvlist_add_int32(nvl, "old_pages", rcm->ms_syspages) != 0 ||
	    nvlist_add_int32(nvl, "new_pages", newpages) != 0) {
		dprintf((stderr, "nvlist_add fail\n"));
		nvlist_free(nvl);
		return (-1);
	}

	dprintf((stderr, "memory capacity change req: "
	    "page_size=%d, old_pages=%d, new_pages=%d\n",
	    rcm->ms_pagesize, rcm->ms_syspages, newpages));

	(void) rcm_request_capacity_change(rcm->hdl, RCM_MEM_ALL,
	    RCM_QUERY|RCM_SCOPE, nvl, &rcm_info);

	rv = add_rcm_clients(&ri_hdl->mem_cap_clients, rcm, rcm_info, 0, NULL);

	nvlist_free(nvl);
	if (rcm_info != NULL)
		rcm_free_info(rcm_info);

	return (rv);
}

static int
(*cm_rcm_qpass_func(cfga_type_t ap_type))(cfga_list_data_t *, rcmd_t *)
{
	int i;

	for (i = 0; i < sizeof (cm_ctl) / sizeof (cm_ctl[0]); i++) {
		if (strcmp(cm_ctl[i].type, ap_type) == 0) {
			return (cm_ctl[i].cm_rcm_qpass);
		}
	}
	return (NULL);
}

/*
 * Save cpu ids and RCM abstract resource names.
 * Cpu ids will be used for the capacity change request.
 * Resource names will be used for the offline query.
 */
static int
cpu_rcm_qpass(cfga_list_data_t *cfga_ldata, rcmd_t *rcm)
{
	processorid_t	cpuid;
	char		*cpustr, *lasts, *rsrcname, rbuf[32];
	char		cbuf[CFGA_INFO_LEN];
	int		speed, ecache;

	assert(sscanf(cfga_ldata->ap_info, CPU_INFO_FMT, &cbuf, &speed,
	    &ecache) == 3);

	for (cpustr = (char *)strtok_r(cbuf, CPUID_SEP, &lasts);
	    cpustr != NULL;
	    cpustr = (char *)strtok_r(NULL, CPUID_SEP, &lasts)) {
		cpuid = atoi(cpustr);

		(void) snprintf(rbuf, sizeof (rbuf), "%s%d", RCM_CPU, cpuid);
		if ((rsrcname = strdup(rbuf)) == NULL) {
			dprintf((stderr, "strdup fail\n"));
			return (-1);
		}
		assert(rcm->nrlist < rcm->ndevs && rcm->ncpus < rcm->ndevs);
		rcm->rlist[rcm->nrlist++] = rsrcname;
		rcm->cpus[rcm->ncpus++] = (cpuid_t)cpuid;

		dprintf((stderr, "cpu_cm_info: cpuid=%d, rsrcname=%s",
		    cpuid, rsrcname));
	}

	return (0);
}

/*
 * No RCM resource names for individual memory units, so
 * just add to offline query page count.
 */
static int
mem_rcm_qpass(cfga_list_data_t *cfga, rcmd_t *rcm)
{
	char		*cp;
	uint_t		kbytes;
	longlong_t	ii;

	if ((cp = strstr(cfga->ap_info, "size")) == NULL ||
	    sscanf(cp, "size=%u", &kbytes) != 1) {
		dprintf((stderr, "unknown sbd info format: %s\n", cp));
		return (-1);
	}

	ii = (longlong_t)kbytes * KBYTE;
	rcm->query_pages += (uint_t)(ii / rcm->ms_pagesize);

	dprintf((stderr, "%s: npages=%u\n", cfga->ap_log_id,
	    (uint_t)(ii / rcm->ms_pagesize)));

	return (0);
}

/*
 * Add physical I/O bus name to RCM resource list.
 */
static int
io_rcm_qpass(cfga_list_data_t *cfga, rcmd_t *rcm)
{
	char		path[MAXPATHLEN];
	char		buf[MAXPATHLEN];
	char		*rsrcname;

	if (sscanf(cfga->ap_info, "device=%s", path) != 1) {
		dprintf((stderr, "unknown sbd info format: %s\n",
		    cfga->ap_info));
		return (-1);
	}

	(void) snprintf(buf, sizeof (buf), "/devices%s", path);
	if ((rsrcname = strdup(buf)) == NULL) {
		dprintf((stderr, "strdup fail\n"));
		return (-1);
	}

	assert(rcm->nrlist < rcm->ndevs);
	rcm->rlist[rcm->nrlist++] = rsrcname;

	return (0);
}

static int
(*cm_info_func(cfga_type_t ap_type))(ri_ap_t *, cfga_list_data_t *,
    int, rcmd_t *)
{
	int i;

	for (i = 0; i < sizeof (cm_ctl) / sizeof (cm_ctl[0]); i++) {
		if (strcmp(cm_ctl[i].type, ap_type) == 0) {
			return (cm_ctl[i].cm_info);
		}
	}
	return (NULL);
}

/*
 * Create cpu handle, adding properties exported by sbd plugin and
 * RCM client usage.
 */
/* ARGSUSED */
static int
cpu_cm_info(ri_ap_t *ap, cfga_list_data_t *cfga, int flags, rcmd_t *rcm)
{
	processorid_t	cpuid;
	int		speed, ecache, rv = 0;
	char		buf[CFGA_INFO_LEN], *cpustr, *lasts;

	if (sscanf(cfga->ap_info, CPU_INFO_FMT, &buf, &speed, &ecache) != 3) {
		dprintf((stderr, "unknown sbd info format: %s\n",
		    cfga->ap_info));
		return (-1);
	}

	/* parse cpuids */
	for (cpustr = (char *)strtok_r(buf, CPUID_SEP, &lasts);
	    cpustr != NULL;
	    cpustr = (char *)strtok_r(NULL, CPUID_SEP, &lasts)) {
		cpuid = atoi(cpustr);
		if ((rv = i_cpu_cm_info(cpuid, speed, ecache, ap, rcm)) != 0) {
			break;
		}
	}

	return (rv);
}

static int
i_cpu_cm_info(processorid_t cpuid, int speed, int ecache_cfga, ri_ap_t *ap,
    rcmd_t *rcm)
{
	int		ecache_mb = 0;
	int		ecache_kb = 0;
	char		*state, buf[32];
	processor_info_t cpu_info;
	ri_dev_t	*cpu = NULL;
	rcm_info_t	*rcm_info = NULL;

	/*
	 * Could have been unconfigured in the interim, so cannot
	 * count on processor_info recognizing it.
	 */
	state = (processor_info(cpuid, &cpu_info) == 0) ?
	    pstate2str(cpu_info.pi_state) : "unknown";

	if ((cpu = ri_dev_alloc()) == NULL) {
		dprintf((stderr, "ri_dev_alloc failed\n"));
		return (-1);
	}

	/*
	 * Assume the ecache_info table has the right e-cache size for
	 * this CPU.  Use the value found in cfgadm (ecache_cfga) if not.
	 */
	if (rcm->ecache_info.ecache_sizes != NULL) {
		assert(rcm->ecache_info.cpuid_max != 0 &&
		    cpuid <= rcm->ecache_info.cpuid_max);
		ecache_mb = rcm->ecache_info.ecache_sizes[cpuid] / MBYTE;
		ecache_kb = rcm->ecache_info.ecache_sizes[cpuid] / KBYTE;
	}

	if (ecache_mb == 0) {
		ecache_mb = ecache_cfga;
	}

	dprintf((stderr, "i_cpu_cm_info: cpu(%d) ecache=%d MB\n",
	    cpuid, ecache));

	if (nvlist_add_int32(cpu->conf_props, RI_CPU_ID, cpuid) != 0 ||
	    nvlist_add_int32(cpu->conf_props, RI_CPU_SPEED, speed) != 0 ||
	    nvlist_add_int32(cpu->conf_props, RI_CPU_ECACHE, ecache_mb) != 0 ||
	    nvlist_add_string(cpu->conf_props, RI_CPU_STATE, state) != 0) {
		dprintf((stderr, "nvlist_add fail\n"));
		ri_dev_free(cpu);
		return (-1);
	}

	/*
	 * Report cache size in kilobyte units if available.  This info is
	 * added to support processors with cache sizes that are non-integer
	 * megabyte multiples.
	 */
	if (ecache_kb != 0) {
		if (nvlist_add_int32(cpu->conf_props, RI_CPU_ECACHE_KBYTE,
		    ecache_kb) != 0)  {
			dprintf((stderr, "nvlist_add fail: %s\n",
			    RI_CPU_ECACHE_KBYTE));
			ri_dev_free(cpu);
			return (-1);
		}
	}

	(void) snprintf(buf, sizeof (buf), "%s%d", RCM_CPU, cpuid);
	dprintf((stderr, "rcm_get_info(%s)\n", buf));
	if (rcm_get_info(rcm->hdl, buf, RCM_INCLUDE_DEPENDENT,
	    &rcm_info) != RCM_SUCCESS) {
		dprintf((stderr, "rcm_get_info (errno=%d)\n", errno));
		ri_dev_free(cpu);
		if (rcm_info != NULL)
			rcm_free_info(rcm_info);
		return (-1);
	}

	dev_list_cpu_insert(&ap->cpus, cpu, cpuid);

	return (0);
}

/*
 * Create memory handle, adding properties exported by sbd plugin.
 * No RCM tuples to be saved unless RCM is modified to export names
 * for individual memory units.
 */
/* ARGSUSED */
static int
mem_cm_info(ri_ap_t *ap, cfga_list_data_t *cfga, int flags, rcmd_t *rcm)
{
	ri_dev_t	*mem;
	char		*cp;
	char		*cpval;
	int		len;
	uint64_t	base_addr;				/* required */
	int32_t		size_kb;				/* required */
	int32_t		perm_kb = 0;				/* optional */
	char		target[CFGA_AP_LOG_ID_LEN] = "";	/* optional */
	int32_t		del_kb = 0;				/* optional */
	int32_t		rem_kb = 0;				/* optional */
	char		source[CFGA_AP_LOG_ID_LEN] = "";	/* optional */

	if (sscanf(cfga->ap_info, "address=0x%llx size=%u", &base_addr,
	    &size_kb) != 2) {
		goto err_fmt;
	}

	if ((cp = strstr(cfga->ap_info, "permanent")) != NULL &&
	    sscanf(cp, "permanent=%u", &perm_kb) != 1) {
		goto err_fmt;
	}

	if ((cp = strstr(cfga->ap_info, "target")) != NULL) {
		if ((cpval = strstr(cp, "=")) == NULL) {
			goto err_fmt;
		}
		for (len = 0; cpval[len] != '\0' && cpval[len] != ' '; len++) {
			if (len >= CFGA_AP_LOG_ID_LEN) {
				goto err_fmt;
			}
		}
		if (sscanf(cp, "target=%s deleted=%u remaining=%u", &target,
		    &del_kb, &rem_kb) != 3) {
			goto err_fmt;
		}
	}

	if ((cp = strstr(cfga->ap_info, "source")) != NULL) {
		if ((cpval = strstr(cp, "=")) == NULL) {
			goto err_fmt;
		}
		for (len = 0; cpval[len] != '\0' && cpval[len] != ' '; len++) {
			if (len >= CFGA_AP_LOG_ID_LEN) {
				goto err_fmt;
			}
		}
		if (sscanf(cp, "source=%s", &source) != 1) {
			goto err_fmt;
		}
	}

	dprintf((stderr, "%s: base=0x%llx, size=%u, permanent=%u\n",
	    cfga->ap_log_id, base_addr, size_kb, perm_kb));

	if ((mem = ri_dev_alloc()) == NULL)
		return (-1);

	/*
	 * Convert memory sizes to MB (truncate).
	 */
	if (nvlist_add_uint64(mem->conf_props, RI_MEM_ADDR, base_addr) != 0 ||
	    nvlist_add_int32(mem->conf_props, RI_MEM_BRD, size_kb/KBYTE) != 0 ||
	    nvlist_add_int32(mem->conf_props, RI_MEM_PERM,
	    perm_kb/KBYTE) != 0) {
		dprintf((stderr, "nvlist_add failure\n"));
		ri_dev_free(mem);
		return (-1);
	}

	if (target[0] != '\0' &&
	    (nvlist_add_string(mem->conf_props, RI_MEM_TARG, target) != 0 ||
	    nvlist_add_int32(mem->conf_props, RI_MEM_DEL, del_kb/KBYTE) != 0 ||
	    nvlist_add_int32(mem->conf_props, RI_MEM_REMAIN,
	    rem_kb/KBYTE) != 0)) {
		dprintf((stderr, "nvlist_add failure\n"));
		ri_dev_free(mem);
		return (-1);
	}

	if (source[0] != '\0' &&
	    nvlist_add_string(mem->conf_props, RI_MEM_SRC, source) != 0) {
		dprintf((stderr, "nvlist_add failure\n"));
		ri_dev_free(mem);
		return (-1);
	}

	/*
	 * XXX - move this property to attachment point hdl?
	 */
	if (nvlist_add_int32(mem->conf_props, RI_MEM_DOMAIN,
	    rcm->ms_sysmb) != 0) {
		dprintf((stderr, "nvlist_add failure\n"));
		ri_dev_free(mem);
		return (-1);
	}

	dev_list_append(&ap->mems, mem);
	return (0);

err_fmt:
	dprintf((stderr, "unknown sbd info format: %s\n", cfga->ap_info));
	return (-1);
}

/*
 * Initiate a libdevinfo walk on the IO bus path.
 * XXX - investigate performance using two threads here: one thread to do the
 * libdevinfo snapshot and treewalk; and one thread to get RCM usage info
 */
static int
io_cm_info(ri_ap_t *ap, cfga_list_data_t *cfga, int flags, rcmd_t *rcm)
{
	int			i;
	int			j;
	int			k;
	int			set_size;
	int			retval = 0;
	int			n_usage;
	devinfo_arg_t		di_arg;
	lookup_table_t		devicetable;
	lookup_entry_t		*deventry;
	lookup_entry_t		*lastdeventry;
	ri_dev_t		*io = NULL;
	ri_client_t		*client;
	ri_client_t		*tmp;
	di_devlink_handle_t	linkhd = NULL;
	di_node_t		root = DI_NODE_NIL;
	di_node_t		node = DI_NODE_NIL;
	rcm_info_tuple_t	*rcm_tuple;
	rcm_info_t		*rcm_info = NULL;
	const char		*rcm_rsrc = NULL;
	char			drv_inst[MAXPATHLEN];
	char			path[MAXPATHLEN];
	char			pathbuf[MAXPATHLEN];

	dprintf((stderr, "io_cm_info(%s)\n", cfga->ap_log_id));

	/* Extract devfs path from cfgadm information */
	if (sscanf(cfga->ap_info, "device=%s\n", path) != 1) {
		dprintf((stderr, "unknown sbd info format: %s\n",
		    cfga->ap_info));
		return (-1);
	}

	/* Initialize empty device lookup table */
	devicetable.n_entries = 0;
	devicetable.n_slots = 0;
	devicetable.table = NULL;

	/* Get libdevinfo snapshot */
	dprintf((stderr, "di_init(%s)\n", path));
	if ((root = di_init(path, DINFOCPYALL)) == DI_NODE_NIL) {
		dprintf((stderr, "di_init: %s\n", strerror(errno)));
		retval = RI_NODE_NIL; /* tell ri_init to skip this node */
		goto end;
	}

	/*
	 * Map in devlinks database.
	 * XXX - This could be moved to ri_init() for better performance.
	 */
	dprintf((stderr, "di_devlink_init()\n"));
	if ((linkhd = di_devlink_init(NULL, 0)) == NULL) {
		dprintf((stderr, "di_devlink_init: %s\n", strerror(errno)));
		retval = -1;
		goto end;
	}

	/* Initialize argument for devinfo treewalk */
	di_arg.err = 0;
	di_arg.node = DI_NODE_NIL;
	di_arg.pathbuf = pathbuf;
	di_arg.table = &devicetable;
	di_arg.linkhd = linkhd;

	/* Use libdevinfo treewalk to build device lookup table */
	if (di_walk_node(root, DI_WALK_CLDFIRST, (void *)&di_arg,
	    devinfo_node_walk) != 0) {
		dprintf((stderr, "di_walk_node: %s\n", strerror(errno)));
		retval = -1;
		goto end;
	}
	if (di_arg.err != 0) {
		dprintf((stderr, "di_walk_node: device tree walk failed\n"));
		retval = -1;
		goto end;
	}

	/* Call RCM to gather usage information */
	(void) snprintf(pathbuf, MAXPATHLEN, "/devices%s", path);
	dprintf((stderr, "rcm_get_info(%s)\n", pathbuf));
	if (rcm_get_info(rcm->hdl, pathbuf,
	    RCM_INCLUDE_SUBTREE|RCM_INCLUDE_DEPENDENT, &rcm_info) !=
	    RCM_SUCCESS) {
		dprintf((stderr, "rcm_get_info (errno=%d)\n", errno));
		retval = -1;
		goto end;
	}

	/* Sort the device table by name (proper order for lookups) */
	qsort(devicetable.table, devicetable.n_entries, sizeof (lookup_entry_t),
	    table_compare_names);

	/* Perform mappings of RCM usage segments to device table entries */
	lastdeventry = NULL;
	rcm_tuple = NULL;
	while ((rcm_tuple = rcm_info_next(rcm_info, rcm_tuple)) != NULL) {
		if ((rcm_rsrc = rcm_info_rsrc(rcm_tuple)) == NULL)
			continue;
		if (deventry = lookup(&devicetable, rcm_rsrc)) {
			if (add_usage(deventry, rcm_rsrc, rcm_tuple)) {
				retval = -1;
				goto end;
			}
			lastdeventry = deventry;
		} else {
			if (add_usage(lastdeventry, rcm_rsrc, rcm_tuple)) {
				retval = -1;
				goto end;
			}
		}
	}

	/* Re-sort the device table by index number (original treewalk order) */
	qsort(devicetable.table, devicetable.n_entries, sizeof (lookup_entry_t),
	    table_compare_indices);

	/*
	 * Use the mapped usage and the device table to construct ri_dev_t's.
	 * Construct one for each set of entries in the device table with
	 * matching di_node_t's, if: 1) it has mapped RCM usage, or 2) it is
	 * a leaf node and the caller has requested that unmanaged nodes be
	 * included in the output.
	 */
	i = 0;
	while (i < devicetable.n_entries) {

		node = devicetable.table[i].node;

		/* Count how many usage records are mapped to this node's set */
		n_usage = 0;
		set_size = 0;
		while (((i + set_size) < devicetable.n_entries) &&
		    (devicetable.table[i + set_size].node == node)) {
			n_usage += devicetable.table[i + set_size].n_usage;
			set_size += 1;
		}

		/*
		 * If there's no usage, then the node is unmanaged.  Skip this
		 * set of devicetable entries unless the node is a leaf node
		 * and the caller has requested information on unmanaged leaves.
		 */
		if ((n_usage == 0) &&
		    !((flags & RI_INCLUDE_UNMANAGED) && (ident_leaf(node)))) {
			i += set_size;
			continue;
		}

		/*
		 * The checks above determined that this node is going in.
		 * So determine its driver/instance name and allocate an
		 * ri_dev_t for this node.
		 */
		if (mk_drv_inst(node, drv_inst, devicetable.table[i].name)) {
			dprintf((stderr, "mk_drv_inst failed\n"));
			retval = -1;
			break;
		}
		if ((io = io_dev_alloc(drv_inst)) == NULL) {
			dprintf((stderr, "io_dev_alloc failed\n"));
			retval = -1;
			break;
		}

		/* Now add all the RCM usage records (if any) to the ri_dev_t */
		for (j = i; j < (i + set_size); j++) {
			for (k = 0; k < devicetable.table[j].n_usage; k++) {
				/* Create new ri_client_t for basic usage */
				client = ri_client_alloc(
				    (char *)devicetable.table[j].usage[k].rsrc,
				    (char *)devicetable.table[j].usage[k].info);
				if (client == NULL) {
					dprintf((stderr,
					    "ri_client_alloc failed\n"));
					ri_dev_free(io);
					retval = -1;
					goto end;
				}

				/* Add extra query usage to the ri_client_t */
				if ((flags & RI_INCLUDE_QUERY) &&
				    (add_query_state(rcm, client,
				    devicetable.table[j].usage[k].rsrc,
				    devicetable.table[j].usage[k].info) != 0)) {
					dprintf((stderr,
					    "add_query_state failed\n"));
					ri_dev_free(io);
					ri_client_free(client);
					retval = -1;
					goto end;
				}

				/* Link new ri_client_t to ri_dev_t */
				if (io->rcm_clients) {
					tmp = io->rcm_clients;
					while (tmp->next)
						tmp = tmp->next;
					tmp->next = client;
				} else {
					io->rcm_clients = client;
				}
			}
		}

		/* Link the ri_dev_t into the return value */
		dev_list_append(&ap->ios, io);

		/* Advance to the next node set */
		i += set_size;
	}

end:
	if (rcm_info != NULL)
		rcm_free_info(rcm_info);
	if (linkhd != NULL)
		di_devlink_fini(&linkhd);
	if (root != DI_NODE_NIL)
		di_fini(root);
	empty_table(&devicetable);

	dprintf((stderr, "io_cm_info: returning %d\n", retval));
	return (retval);
}

static int
ident_leaf(di_node_t node)
{
	di_minor_t	minor = DI_MINOR_NIL;

	return ((minor = di_minor_next(node, minor)) != DI_MINOR_NIL &&
	    di_child_node(node) == DI_NODE_NIL);
}

/* ARGSUSED */
static int
mk_drv_inst(di_node_t node, char drv_inst[], char *devfs_path)
{
	char	*drv;
	int	inst;

	if ((drv = di_driver_name(node)) == NULL) {
		dprintf((stderr, "no driver bound to %s\n",
		    devfs_path));
		return (-1);
	}

	if ((inst = di_instance(node)) == -1) {
		dprintf((stderr, "no instance assigned to %s\n",
		    devfs_path));
		return (-1);
	}
	(void) snprintf(drv_inst, MAXPATHLEN, "%s%d", drv, inst);

	return (0);
}

/*
 * Libdevinfo walker.
 *
 * During the tree walk of the attached IO devices, for each node
 * and all of its associated minors, the following actions are performed:
 *  -  The /devices path of the physical device node or minor
 *     is stored in a lookup table along with a reference to the
 *     libdevinfo node it represents via add_lookup_entry().
 *  -  The device links associated with each device are also
 *     stored in the same lookup table along with a reference to
 *     the libdevinfo node it represents via the minor walk callback.
 *
 */
static int
devinfo_node_walk(di_node_t node, void *arg)
{
	char			*devfs_path;
#ifdef DEBUG
	char			*drv;
#endif /* DEBUG */
	devinfo_arg_t		*di_arg = (devinfo_arg_t *)arg;

	if (node == DI_NODE_NIL) {
		return (DI_WALK_TERMINATE);
	}

	if (((di_state(node) & DI_DRIVER_DETACHED) == 0) &&
	    ((devfs_path = di_devfs_path(node)) != NULL)) {

		/* Use the provided path buffer to create full /devices path */
		(void) snprintf(di_arg->pathbuf, MAXPATHLEN, "/devices%s",
		    devfs_path);

#ifdef DEBUG
		dprintf((stderr, "devinfo_node_walk(%s)\n", di_arg->pathbuf));
		if ((drv = di_driver_name(node)) != NULL)
			dprintf((stderr, " driver name %s instance %d\n", drv,
			    di_instance(node)));
#endif

		/* Free the devfs_path */
		di_devfs_path_free(devfs_path);

		/* Add an entry to the lookup table for this physical device */
		if (add_lookup_entry(di_arg->table, di_arg->pathbuf, node)) {
			dprintf((stderr, "add_lookup_entry: %s\n",
			    strerror(errno)));
			di_arg->err = 1;
			return (DI_WALK_TERMINATE);
		}

		/* Check if this node has minors */
		if ((di_minor_next(node, DI_MINOR_NIL)) != DI_MINOR_NIL) {
			/* Walk this node's minors */
			di_arg->node = node;
			if (di_walk_minor(node, NULL, DI_CHECK_ALIAS, arg,
			    devinfo_minor_walk) != 0) {
				dprintf((stderr, "di_walk_minor: %s\n",
				    strerror(errno)));
				di_arg->err = 1;
				return (DI_WALK_TERMINATE);
			}
		}
	}

	return (DI_WALK_CONTINUE);
}

/*
 * Use di_devlink_walk to find the /dev link from /devices path for this minor
 */
static int
devinfo_minor_walk(di_node_t node, di_minor_t minor, void *arg)
{
	char		*name;
	char		*devfs_path;
	devinfo_arg_t	*di_arg = (devinfo_arg_t *)arg;
	char		pathbuf[MAXPATHLEN];

#ifdef DEBUG
	dprintf((stderr, "devinfo_minor_walk(%d) %s\n", minor,
	    di_arg->pathbuf));

	if ((name = di_minor_name(minor)) != NULL) {
		dprintf((stderr, "  minor name %s\n", name));
	}
#endif /* DEBUG */

	/* Terminate the walk when the device node changes */
	if (node != di_arg->node) {
		return (DI_WALK_TERMINATE);
	}

	/* Construct full /devices path for this minor */
	if ((name = di_minor_name(minor)) == NULL) {
		return (DI_WALK_CONTINUE);
	}
	(void) snprintf(pathbuf, MAXPATHLEN, "%s:%s", di_arg->pathbuf, name);

	/* Add lookup entry for this minor node */
	if (add_lookup_entry(di_arg->table, pathbuf, node)) {
		dprintf((stderr, "add_lookup_entry: %s\n", strerror(errno)));
		di_arg->err = 1;
		return (DI_WALK_TERMINATE);
	}

	/*
	 * Walk the associated device links.
	 * Note that di_devlink_walk() doesn't want "/devices" in its paths.
	 * Also note that di_devlink_walk() will fail if there are no device
	 * links, which is fine; so ignore if it fails.  Only check for
	 * internal failures during such a walk.
	 */
	devfs_path = &pathbuf[strlen("/devices")];
	(void) di_devlink_walk(di_arg->linkhd, NULL, devfs_path, 0, arg,
	    devinfo_devlink_walk);
	if (di_arg->err != 0) {
		return (DI_WALK_TERMINATE);
	}

	return (DI_WALK_CONTINUE);
}

static int
devinfo_devlink_walk(di_devlink_t devlink, void *arg)
{
	const char	*linkpath;
	devinfo_arg_t	*di_arg = (devinfo_arg_t *)arg;

	/* Get the devlink's path */
	if ((linkpath = di_devlink_path(devlink)) == NULL) {
		dprintf((stderr, "di_devlink_path: %s\n", strerror(errno)));
		di_arg->err = 1;
		return (DI_WALK_TERMINATE);
	}
	dprintf((stderr, "devinfo_devlink_walk: %s\n", linkpath));

	/* Add lookup entry for this devlink */
	if (add_lookup_entry(di_arg->table, linkpath, di_arg->node)) {
		dprintf((stderr, "add_lookup_entry: %s\n", strerror(errno)));
		di_arg->err = 1;
		return (DI_WALK_TERMINATE);
	}

	return (DI_WALK_CONTINUE);
}

/*
 * Map rcm_info_t's to ri_client_t's, filtering out "uninteresting" (hack)
 * RCM clients. The number of "interesting" ri_client_t's is returned
 * in cnt if passed non-NULL.
 */
static int
add_rcm_clients(ri_client_t **client_list, rcmd_t *rcm, rcm_info_t *info,
    int flags, int *cnt)
{
	rcm_info_tuple_t	*tuple;
	char			*rsrc, *usage;
	ri_client_t		*client, *tmp;

	assert(client_list != NULL && rcm != NULL);

	if (info == NULL)
		return (0);

	if (cnt != NULL)
		*cnt = 0;

	tuple = NULL;
	while ((tuple = rcm_info_next(info, tuple)) != NULL) {
		if ((rsrc = (char *)rcm_info_rsrc(tuple)) == NULL ||
		    (usage = (char *)rcm_info_info(tuple)) == NULL) {
			continue;
		}

		if (rcm_ignore(rsrc, usage) == 0)
			continue;

		if ((client = ri_client_alloc(rsrc, usage)) == NULL)
			return (-1);

		if ((flags & RI_INCLUDE_QUERY) && add_query_state(rcm, client,
		    rsrc, usage) != 0) {
			ri_client_free(client);
			return (-1);
		}

		if (cnt != NULL)
			++*cnt;

		/*
		 * Link in
		 */
		if ((tmp = *client_list) == NULL) {
			*client_list = client;
			continue;
		}
		while (tmp->next != NULL) {
			tmp = tmp->next;
		}
		tmp->next = client;
	}

	return (0);
}

/*
 * Currently only filtering out based on known info string prefixes.
 */
/* ARGSUSED */
static int
rcm_ignore(char *rsrc, char *infostr)
{
	char	**cpp;

	for (cpp = rcm_info_filter; *cpp != NULL; cpp++) {
		if (strncmp(infostr, *cpp, strlen(*cpp)) == 0) {
			return (0);
		}
	}
	return (-1);
}

/*
 * If this tuple was cached in the offline query pass, add the
 * query state and error string to the ri_client_t.
 */
static int
add_query_state(rcmd_t *rcm, ri_client_t *client, const char *rsrc,
    const char *info)
{
	int			qstate = RI_QUERY_UNKNOWN;
	char			*errstr = NULL;
	rcm_info_tuple_t	*cached_tuple;

	if ((cached_tuple = tuple_lookup(rcm, rsrc, info)) != NULL) {
		qstate = state2query(rcm_info_state(cached_tuple));
		errstr = (char *)rcm_info_error(cached_tuple);
	}

	if (nvlist_add_int32(client->usg_props, RI_QUERY_STATE, qstate) != 0 ||
	    (errstr != NULL && nvlist_add_string(client->usg_props,
	    RI_QUERY_ERR, errstr) != 0)) {
		dprintf((stderr, "nvlist_add fail\n"));
		return (-1);
	}

	return (0);
}

static int
state2query(int rcm_state)
{
	int	query;

	switch (rcm_state) {
	case RCM_STATE_OFFLINE_QUERY:
	case RCM_STATE_SUSPEND_QUERY:
		query = RI_QUERY_OK;
		break;
	case RCM_STATE_OFFLINE_QUERY_FAIL:
	case RCM_STATE_SUSPEND_QUERY_FAIL:
		query = RI_QUERY_FAIL;
		break;
	default:
		query = RI_QUERY_UNKNOWN;
		break;
	}

	return (query);
}

static void
dev_list_append(ri_dev_t **head, ri_dev_t *dev)
{
	ri_dev_t	*tmp;

	if ((tmp = *head) == NULL) {
		*head = dev;
		return;
	}
	while (tmp->next != NULL) {
		tmp = tmp->next;
	}
	tmp->next = dev;
}

/*
 * The cpu list is ordered on cpuid since CMP cpuids will not necessarily
 * be discovered in sequence.
 */
static void
dev_list_cpu_insert(ri_dev_t **listp, ri_dev_t *dev, processorid_t newid)
{
	ri_dev_t	*tmp;
	int32_t		cpuid;

	while ((tmp = *listp) != NULL &&
	    nvlist_lookup_int32(tmp->conf_props, RI_CPU_ID, &cpuid) == 0 &&
	    cpuid < newid) {
		listp = &tmp->next;
	}

	dev->next = tmp;
	*listp = dev;
}

/*
 * Linear lookup. Should convert to hash tab.
 */
static rcm_info_tuple_t *
tuple_lookup(rcmd_t *rcm, const char *krsrc, const char *kinfo)
{
	rcm_info_tuple_t	*tuple = NULL;
	const char		*rsrc, *info;

	if ((rcm == NULL) || (krsrc == NULL) || (kinfo == NULL)) {
		return (NULL);
	}

	while ((tuple = rcm_info_next(rcm->offline_query_info,
	    tuple)) != NULL) {
		if ((rsrc = rcm_info_rsrc(tuple)) == NULL ||
		    (info = rcm_info_info(tuple)) == NULL) {
			continue;
		}

		if (strcmp(rsrc, krsrc) == 0 && strcmp(info, kinfo) == 0) {
			return (tuple);
		}
	}
	return (NULL);
}

/*
 * Create and link attachment point handle.
 */
static ri_ap_t *
ri_ap_alloc(char *ap_id, ri_hdl_t *hdl)
{
	ri_ap_t		*ap, *tmp;

	if ((ap = calloc(1, sizeof (*ap))) == NULL) {
		dprintf((stderr, "calloc: %s\n", strerror(errno)));
		return (NULL);
	}

	if (nvlist_alloc(&ap->conf_props, NV_UNIQUE_NAME, 0) != 0 ||
	    nvlist_add_string(ap->conf_props, RI_AP_REQ_ID, ap_id) != 0) {
		nvlist_free(ap->conf_props);
		free(ap);
		return (NULL);
	}

	if ((tmp = hdl->aps) == NULL) {
		hdl->aps = ap;
	} else {
		while (tmp->next != NULL) {
			tmp = tmp->next;
		}
		tmp->next = ap;
	}

	return (ap);
}

static ri_dev_t *
ri_dev_alloc(void)
{
	ri_dev_t	*dev;

	if ((dev = calloc(1, sizeof (*dev))) == NULL ||
	    nvlist_alloc(&dev->conf_props, NV_UNIQUE_NAME, 0) != 0) {
		s_free(dev);
	}
	return (dev);
}

static ri_dev_t *
io_dev_alloc(char *drv_inst)
{
	ri_dev_t	*io;

	assert(drv_inst != NULL);

	if ((io = ri_dev_alloc()) == NULL)
		return (NULL);

	if (nvlist_add_string(io->conf_props, RI_IO_DRV_INST,
	    drv_inst) != 0) {
		dprintf((stderr, "nvlist_add_string fail\n"));
		ri_dev_free(io);
		return (NULL);
	}

	return (io);
}

static ri_client_t *
ri_client_alloc(char *rsrc, char *usage)
{
	ri_client_t	*client;

	assert(rsrc != NULL && usage != NULL);

	if ((client = calloc(1, sizeof (*client))) == NULL) {
		dprintf((stderr, "calloc: %s\n", strerror(errno)));
		return (NULL);
	}

	if (nvlist_alloc(&client->usg_props, NV_UNIQUE_NAME, 0) != 0) {
		dprintf((stderr, "nvlist_alloc fail\n"));
		free(client);
		return (NULL);
	}

	if (nvlist_add_string(client->usg_props, RI_CLIENT_RSRC, rsrc) != 0 ||
	    nvlist_add_string(client->usg_props, RI_CLIENT_USAGE, usage) != 0) {
		dprintf((stderr, "nvlist_add_string fail\n"));
		ri_client_free(client);
		return (NULL);
	}

	return (client);
}

static void
apd_tbl_free(apd_t apd_tbl[], int napds)
{
	int	i;
	apd_t	*apd;

	for (i = 0, apd = apd_tbl; i < napds; i++, apd++)
		s_free(apd->cfga_list_data);

	free(apd_tbl);
}

static char *
pstate2str(int pi_state)
{
	char	*state;

	switch (pi_state) {
	case P_OFFLINE:
		state = PS_OFFLINE;
		break;
	case P_ONLINE:
		state = PS_ONLINE;
		break;
	case P_FAULTED:
		state = PS_FAULTED;
		break;
	case P_POWEROFF:
		state = PS_POWEROFF;
		break;
	case P_NOINTR:
		state = PS_NOINTR;
		break;
	case P_SPARE:
		state = PS_SPARE;
		break;
	default:
		state = "unknown";
		break;
	}

	return (state);
}

#ifdef DEBUG
static void
dump_apd_tbl(FILE *fp, apd_t *apds, int n_apds)
{
	int			i, j;
	cfga_list_data_t	*cfga_ldata;

	for (i = 0; i < n_apds; i++, apds++) {
		dprintf((stderr, "apd_tbl[%d].nlist=%d\n", i, apds->nlist));
		for (j = 0, cfga_ldata = apds->cfga_list_data; j < apds->nlist;
		    j++, cfga_ldata++) {
			dprintf((fp,
			    "apd_tbl[%d].cfga_list_data[%d].ap_log_id=%s\n",
			    i, j, cfga_ldata->ap_log_id));
		}
	}
}
#endif /* DEBUG */

/*
 * The lookup table is a simple array that is grown in chunks
 * to optimize memory allocation.
 * Indices are assigned to each array entry in-order so that
 * the original device tree ordering can be discerned at a later time.
 *
 * add_lookup_entry is called from the libdevinfo tree traversal callbacks:
 * 1) devinfo_node_walk - physical device path for each node in
 *    the devinfo tree via di_walk_node(), lookup entry name is
 *    /devices/[di_devfs_path]
 * 2) devinfo_minor_walk - physical device path plus minor name for
 *    each minor associated with a node via di_walk_minor(), lookup entry
 *    name is /devices/[di_devfs_path:di_minor_name]
 * 3) devinfo_devlink_walk - for each minor's /dev link from its /devices
 *    path via di_devlink_walk(), lookup entry name is di_devlink_path()
 */
static int
add_lookup_entry(lookup_table_t *table, const char *name, di_node_t node)
{
	size_t		size;
	lookup_entry_t	*new_table;


	/* Grow the lookup table by USAGE_ALLOC_SIZE slots if necessary */
	if (table->n_entries == table->n_slots) {
		size = (table->n_slots + USAGE_ALLOC_SIZE) *
		    sizeof (lookup_entry_t);
		new_table = (lookup_entry_t *)realloc(table->table, size);
		if (new_table == NULL) {
			dprintf((stderr, "add_lookup_entry: alloc failed: %s\n",
			    strerror(errno)));
			errno = ENOMEM;
			return (-1);
		}
		table->table = new_table;
		table->n_slots += USAGE_ALLOC_SIZE;
	}

	dprintf((stderr, "add_lookup_entry[%d]:%s\n", table->n_entries, name));

	/* Add this name to the next slot */
	if ((table->table[table->n_entries].name = strdup(name)) == NULL) {
		dprintf((stderr, "add_lookup_entry: strdup failed: %s\n",
		    strerror(errno)));
		errno = ENOMEM;
		return (-1);
	}
	table->table[table->n_entries].index = table->n_entries;
	table->table[table->n_entries].node = node;
	table->table[table->n_entries].n_usage = 0;
	table->table[table->n_entries].usage = NULL;
	table->n_entries += 1;

	return (0);
}

/*
 * lookup table entry names are full pathname strings, all start with /
 */
static int
table_compare_names(const void *a, const void *b)
{
	lookup_entry_t *entry1 = (lookup_entry_t *)a;
	lookup_entry_t *entry2 = (lookup_entry_t *)b;

	return (strcmp(entry1->name, entry2->name));
}


/*
 * Compare two indices and return -1 for less, 1 for greater, 0 for equal
 */
static int
table_compare_indices(const void *a, const void *b)
{
	lookup_entry_t *entry1 = (lookup_entry_t *)a;
	lookup_entry_t *entry2 = (lookup_entry_t *)b;

	if (entry1->index < entry2->index)
		return (-1);
	if (entry1->index > entry2->index)
		return (1);
	return (0);
}

/*
 * Given a RCM resource name, find the matching entry in the IO device table
 */
static lookup_entry_t *
lookup(lookup_table_t *table, const char *rcm_rsrc)
{
	lookup_entry_t	*entry;
	lookup_entry_t	lookup_arg;

	dprintf((stderr, "lookup:%s\n", rcm_rsrc));
	lookup_arg.name = (char *)rcm_rsrc;
	entry = bsearch(&lookup_arg, table->table, table->n_entries,
	    sizeof (lookup_entry_t), table_compare_names);

#ifdef DEBUG
	if (entry != NULL) {
		dprintf((stderr, " found entry:%d\n", entry->index));
	}
#endif /* DEBUG */
	return (entry);
}

/*
 * Add RCM usage to the given device table entry.
 * Returns -1 on realloc failure.
 */
static int
add_usage(lookup_entry_t *entry, const char *rcm_rsrc, rcm_info_tuple_t *tuple)
{
	size_t		size;
	const char	*info;
	usage_t		*new_usage;

	if ((entry == NULL) ||
	    ((info = rcm_info_info(tuple)) == NULL))
		return (0);

	if (rcm_ignore((char *)rcm_rsrc, (char *)info) == 0)
		return (0);

	size = (entry->n_usage + 1) * sizeof (usage_t);
	new_usage = (usage_t *)realloc(entry->usage, size);
	if (new_usage == NULL) {
		dprintf((stderr, "add_usage: alloc failed: %s\n",
		    strerror(errno)));
		return (-1);
	}
	dprintf((stderr, "add_usage: entry %d rsrc: %s info: %s\n",
	    entry->index, rcm_rsrc, info));

	entry->usage = new_usage;
	entry->usage[entry->n_usage].rsrc = rcm_rsrc;
	entry->usage[entry->n_usage].info = info;
	entry->n_usage += 1;
	return (0);
}

static void
empty_table(lookup_table_t *table)
{
	int i;

	if (table) {
		for (i = 0; i < table->n_entries; i++) {
			if (table->table[i].name)
				free(table->table[i].name);
			/*
			 * Note: the strings pointed to from within
			 * usage were freed already by rcm_free_info
			 */
			if (table->table[i].usage)
				free(table->table[i].usage);
		}
		if (table->table)
			free(table->table);
		table->table = NULL;
		table->n_entries = 0;
		table->n_slots = 0;
	}
}