summaryrefslogtreecommitdiff
path: root/usr/src/uts/sun4v/io/px/px_lib4v.c
blob: 710379950c7152cade0b0cfe33904931886fd56c (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
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
/*
 * 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 (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
 */

#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/ddi.h>
#include <sys/async.h>
#include <sys/sunddi.h>
#include <sys/ddifm.h>
#include <sys/fm/protocol.h>
#include <sys/vmem.h>
#include <sys/intr.h>
#include <sys/ivintr.h>
#include <sys/errno.h>
#include <sys/hypervisor_api.h>
#include <sys/hsvc.h>
#include <px_obj.h>
#include <sys/machsystm.h>
#include <sys/sunndi.h>
#include <sys/pcie_impl.h>
#include "px_lib4v.h"
#include "px_err.h"
#include <sys/pci_cfgacc.h>
#include <sys/pci_cfgacc_4v.h>


/* mask for the ranges property in calculating the real PFN range */
uint_t px_ranges_phi_mask = ((1 << 28) -1);

/*
 * Hypervisor VPCI services information for the px nexus driver.
 */
static	uint64_t	px_vpci_maj_ver; /* Negotiated VPCI API major version */
static	uint64_t	px_vpci_min_ver; /* Negotiated VPCI API minor version */
static	uint_t		px_vpci_users = 0; /* VPCI API users */
static	hsvc_info_t px_hsvc_vpci = {
	HSVC_REV_1, NULL, HSVC_GROUP_VPCI, PX_VPCI_MAJOR_VER,
	PX_VPCI_MINOR_VER, "PX"
};

/*
 * Hypervisor SDIO services information for the px nexus driver.
 */
static	uint64_t	px_sdio_min_ver; /* Negotiated SDIO API minor version */
static	uint_t		px_sdio_users = 0; /* SDIO API users */
static	hsvc_info_t px_hsvc_sdio = {
	HSVC_REV_1, NULL, HSVC_GROUP_SDIO, PX_SDIO_MAJOR_VER,
	PX_SDIO_MINOR_VER, "PX"
};

/*
 * Hypervisor SDIO ERR services information for the px nexus driver.
 */
static	uint64_t	px_sdio_err_min_ver; /* Negotiated SDIO ERR API */
						/* minor version */
static	uint_t		px_sdio_err_users = 0; /* SDIO ERR API users */
static	hsvc_info_t px_hsvc_sdio_err = {
	HSVC_REV_1, NULL, HSVC_GROUP_SDIO_ERR, PX_SDIO_ERR_MAJOR_VER,
	PX_SDIO_ERR_MINOR_VER, "PX"
};

#define	CHILD_LOANED	"child_loaned"
static int px_lib_count_waiting_dev(dev_info_t *);

int
px_lib_dev_init(dev_info_t *dip, devhandle_t *dev_hdl)
{
	px_nexus_regspec_t	*rp;
	uint_t			reglen;
	int			ret;
	px_t			*px_p = DIP_TO_STATE(dip);
	uint64_t mjrnum;
	uint64_t mnrnum;

	DBG(DBG_ATTACH, dip, "px_lib_dev_init: dip 0x%p\n", dip);

	/*
	 * Check HV intr group api versioning.
	 * This driver uses the old interrupt routines which are supported
	 * in old firmware in the CORE API group and in newer firmware in
	 * the INTR API group.  Support for these calls will be dropped
	 * once the INTR API group major goes to 2.
	 */
	if ((hsvc_version(HSVC_GROUP_INTR, &mjrnum, &mnrnum) == 0) &&
	    (mjrnum > 1)) {
		cmn_err(CE_WARN, "px: unsupported intr api group: "
		    "maj:0x%lx, min:0x%lx", mjrnum, mnrnum);
		return (ENOTSUP);
	}

	ret = ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
	    "reg", (uchar_t **)&rp, &reglen);
	if (ret != DDI_PROP_SUCCESS) {
		DBG(DBG_ATTACH, dip, "px_lib_dev_init failed ret=%d\n", ret);
		return (DDI_FAILURE);
	}

	/*
	 * Initilize device handle. The device handle uniquely identifies
	 * a SUN4V device. It consists of the lower 28-bits of the hi-cell
	 * of the first entry of the SUN4V device's "reg" property as
	 * defined by the SUN4V Bus Binding to Open Firmware.
	 */
	*dev_hdl = (devhandle_t)((rp->phys_addr >> 32) & DEVHDLE_MASK);
	ddi_prop_free(rp);

	/*
	 * hotplug implementation requires this property to be associated with
	 * any indirect PCI config access services
	 */
	(void) ddi_prop_update_int(makedevice(ddi_driver_major(dip),
	    PCI_MINOR_NUM(ddi_get_instance(dip), PCI_DEVCTL_MINOR)), dip,
	    PCI_BUS_CONF_MAP_PROP, 1);

	DBG(DBG_ATTACH, dip, "px_lib_dev_init: dev_hdl 0x%llx\n", *dev_hdl);

	/*
	 * If a /pci node has a pci-intx-not-supported property, this property
	 * represents that the fabric doesn't support fixed interrupt.
	 */
	if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
	    "pci-intx-not-supported")) {
		DBG(DBG_ATTACH, dip, "px_lib_dev_init: "
		    "pci-intx-not-supported is not found, dip=0x%p\n", dip);
		px_p->px_supp_intr_types |= DDI_INTR_TYPE_FIXED;
	}

	/*
	 * Negotiate the API version for VPCI hypervisor services.
	 */
	if (px_vpci_users == 0) {
		if ((ret = hsvc_register(&px_hsvc_vpci, &px_vpci_min_ver))
		    == 0) {
			px_vpci_maj_ver = px_hsvc_vpci.hsvc_major;
			goto hv_negotiation_complete;
		}
		/*
		 * Negotiation with the latest known VPCI hypervisor services
		 * failed.  Fallback to version 1.0.
		 */
		px_hsvc_vpci.hsvc_major = PX_HSVC_MAJOR_VER_1;
		px_hsvc_vpci.hsvc_minor = PX_HSVC_MINOR_VER_0;

		if ((ret = hsvc_register(&px_hsvc_vpci, &px_vpci_min_ver))
		    == 0) {
			px_vpci_maj_ver = px_hsvc_vpci.hsvc_major;
			goto hv_negotiation_complete;
		}

		cmn_err(CE_WARN, "%s: cannot negotiate hypervisor services "
		    "group: 0x%lx major: 0x%lx minor: 0x%lx errno: %d\n",
		    px_hsvc_vpci.hsvc_modname, px_hsvc_vpci.hsvc_group,
		    px_hsvc_vpci.hsvc_major, px_hsvc_vpci.hsvc_minor, ret);

		return (DDI_FAILURE);
	}
hv_negotiation_complete:

	px_vpci_users++;

	DBG(DBG_ATTACH, dip, "px_lib_dev_init: negotiated VPCI API version, "
	    "major 0x%lx minor 0x%lx\n", px_vpci_maj_ver,
	    px_vpci_min_ver);

	/*
	 * Negotiate the API version for SDIO hypervisor services.
	 */
	if ((px_sdio_users == 0) &&
	    ((ret = hsvc_register(&px_hsvc_sdio, &px_sdio_min_ver)) != 0)) {
		DBG(DBG_ATTACH, dip, "%s: cannot negotiate hypervisor "
		    "services group: 0x%lx major: 0x%lx minor: 0x%lx "
		    "errno: %d\n", px_hsvc_sdio.hsvc_modname,
		    px_hsvc_sdio.hsvc_group, px_hsvc_sdio.hsvc_major,
		    px_hsvc_sdio.hsvc_minor, ret);
	} else {
		px_sdio_users++;
		DBG(DBG_ATTACH, dip, "px_lib_dev_init: negotiated SDIO API"
		    "version, major 0x%lx minor 0x%lx\n",
		    px_hsvc_sdio.hsvc_major, px_sdio_min_ver);
	}

	/*
	 * Negotiate the API version for SDIO ERR hypervisor services.
	 */
	if ((px_sdio_err_users == 0) &&
	    ((ret = hsvc_register(&px_hsvc_sdio_err,
	    &px_sdio_err_min_ver)) != 0)) {
		DBG(DBG_ATTACH, dip, "%s: cannot negotiate SDIO ERR hypervisor "
		    "services group: 0x%lx major: 0x%lx minor: 0x%lx "
		    "errno: %d\n", px_hsvc_sdio_err.hsvc_modname,
		    px_hsvc_sdio_err.hsvc_group, px_hsvc_sdio_err.hsvc_major,
		    px_hsvc_sdio_err.hsvc_minor, ret);
	} else {
		px_sdio_err_users++;
		DBG(DBG_ATTACH, dip, "px_lib_dev_init: negotiated SDIO ERR API "
		    "version, major 0x%lx minor 0x%lx\n",
		    px_hsvc_sdio_err.hsvc_major, px_sdio_err_min_ver);
	}

	/*
	 * Find out the number of dev we need to wait under this RC
	 * before we issue fabric sync hypercall
	 */
	px_p->px_plat_p = (void *)(uintptr_t)px_lib_count_waiting_dev(dip);
	DBG(DBG_ATTACH, dip, "Found %d bridges need waiting under RC %p",
	    (int)(uintptr_t)px_p->px_plat_p, dip);
	return (DDI_SUCCESS);
}

/*ARGSUSED*/
int
px_lib_dev_fini(dev_info_t *dip)
{
	DBG(DBG_DETACH, dip, "px_lib_dev_fini: dip 0x%p\n", dip);

	(void) ddi_prop_remove(makedevice(ddi_driver_major(dip),
	    PCI_MINOR_NUM(ddi_get_instance(dip), PCI_DEVCTL_MINOR)), dip,
	    PCI_BUS_CONF_MAP_PROP);

	if (--px_vpci_users == 0)
		(void) hsvc_unregister(&px_hsvc_vpci);

	if (--px_sdio_users == 0)
		(void) hsvc_unregister(&px_hsvc_sdio);

	if (--px_sdio_err_users == 0)
		(void) hsvc_unregister(&px_hsvc_sdio_err);

	return (DDI_SUCCESS);
}

/*ARGSUSED*/
int
px_lib_intr_devino_to_sysino(dev_info_t *dip, devino_t devino,
    sysino_t *sysino)
{
	uint64_t	ret;

	DBG(DBG_LIB_INT, dip, "px_lib_intr_devino_to_sysino: dip 0x%p "
	    "devino 0x%x\n", dip, devino);

	if ((ret = hvio_intr_devino_to_sysino(DIP_TO_HANDLE(dip),
	    devino, sysino)) != H_EOK) {
		DBG(DBG_LIB_INT, dip,
		    "hvio_intr_devino_to_sysino failed, ret 0x%lx\n", ret);
		return (DDI_FAILURE);
	}

	DBG(DBG_LIB_INT, dip, "px_lib_intr_devino_to_sysino: sysino 0x%llx\n",
	    *sysino);

	return (DDI_SUCCESS);
}

/*ARGSUSED*/
int
px_lib_intr_getvalid(dev_info_t *dip, sysino_t sysino,
    intr_valid_state_t *intr_valid_state)
{
	uint64_t	ret;

	DBG(DBG_LIB_INT, dip, "px_lib_intr_getvalid: dip 0x%p sysino 0x%llx\n",
	    dip, sysino);

	if ((ret = hvio_intr_getvalid(sysino,
	    (int *)intr_valid_state)) != H_EOK) {
		DBG(DBG_LIB_INT, dip, "hvio_intr_getvalid failed, ret 0x%lx\n",
		    ret);
		return (DDI_FAILURE);
	}

	DBG(DBG_LIB_INT, dip, "px_lib_intr_getvalid: intr_valid_state 0x%x\n",
	    *intr_valid_state);

	return (DDI_SUCCESS);
}

/*ARGSUSED*/
int
px_lib_intr_setvalid(dev_info_t *dip, sysino_t sysino,
    intr_valid_state_t intr_valid_state)
{
	uint64_t	ret;

	DBG(DBG_LIB_INT, dip, "px_lib_intr_setvalid: dip 0x%p sysino 0x%llx "
	    "intr_valid_state 0x%x\n", dip, sysino, intr_valid_state);

	if ((ret = hvio_intr_setvalid(sysino, intr_valid_state)) != H_EOK) {
		DBG(DBG_LIB_INT, dip, "hvio_intr_setvalid failed, ret 0x%lx\n",
		    ret);
		return (DDI_FAILURE);
	}

	return (DDI_SUCCESS);
}

/*ARGSUSED*/
int
px_lib_intr_getstate(dev_info_t *dip, sysino_t sysino,
    intr_state_t *intr_state)
{
	uint64_t	ret;

	DBG(DBG_LIB_INT, dip, "px_lib_intr_getstate: dip 0x%p sysino 0x%llx\n",
	    dip, sysino);

	if ((ret = hvio_intr_getstate(sysino, (int *)intr_state)) != H_EOK) {
		DBG(DBG_LIB_INT, dip, "hvio_intr_getstate failed, ret 0x%lx\n",
		    ret);
		return (DDI_FAILURE);
	}

	DBG(DBG_LIB_INT, dip, "px_lib_intr_getstate: intr_state 0x%x\n",
	    *intr_state);

	return (DDI_SUCCESS);
}

/*ARGSUSED*/
int
px_lib_intr_setstate(dev_info_t *dip, sysino_t sysino,
    intr_state_t intr_state)
{
	uint64_t	ret;

	DBG(DBG_LIB_INT, dip, "px_lib_intr_setstate: dip 0x%p sysino 0x%llx "
	    "intr_state 0x%x\n", dip, sysino, intr_state);

	if ((ret = hvio_intr_setstate(sysino, intr_state)) != H_EOK) {
		DBG(DBG_LIB_INT, dip, "hvio_intr_setstate failed, ret 0x%lx\n",
		    ret);
		return (DDI_FAILURE);
	}

	return (DDI_SUCCESS);
}

/*ARGSUSED*/
int
px_lib_intr_gettarget(dev_info_t *dip, sysino_t sysino, cpuid_t *cpuid)
{
	uint64_t	ret;

	DBG(DBG_LIB_INT, dip, "px_lib_intr_gettarget: dip 0x%p sysino 0x%llx\n",
	    dip, sysino);

	if ((ret = hvio_intr_gettarget(sysino, cpuid)) != H_EOK) {
		DBG(DBG_LIB_INT, dip,
		    "hvio_intr_gettarget failed, ret 0x%lx\n", ret);
		return (DDI_FAILURE);
	}

	DBG(DBG_LIB_INT, dip, "px_lib_intr_gettarget: cpuid 0x%x\n", *cpuid);

	return (DDI_SUCCESS);
}

/*ARGSUSED*/
int
px_lib_intr_settarget(dev_info_t *dip, sysino_t sysino, cpuid_t cpuid)
{
	uint64_t	ret;

	DBG(DBG_LIB_INT, dip, "px_lib_intr_settarget: dip 0x%p sysino 0x%llx "
	    "cpuid 0x%x\n", dip, sysino, cpuid);

	ret = hvio_intr_settarget(sysino, cpuid);
	if (ret == H_ECPUERROR) {
		cmn_err(CE_PANIC,
		    "px_lib_intr_settarget: hvio_intr_settarget failed, "
		    "ret = 0x%lx, cpuid = 0x%x, sysino = 0x%lx\n", ret,
		    cpuid, sysino);
	} else if (ret != H_EOK) {
		DBG(DBG_LIB_INT, dip,
		    "hvio_intr_settarget failed, ret 0x%lx\n", ret);
		return (DDI_FAILURE);
	}

	return (DDI_SUCCESS);
}

/*ARGSUSED*/
int
px_lib_intr_reset(dev_info_t *dip)
{
	px_t		*px_p = DIP_TO_STATE(dip);
	px_ib_t		*ib_p = px_p->px_ib_p;
	px_ino_t	*ino_p;

	DBG(DBG_LIB_INT, dip, "px_lib_intr_reset: dip 0x%p\n", dip);

	mutex_enter(&ib_p->ib_ino_lst_mutex);

	/* Reset all Interrupts */
	for (ino_p = ib_p->ib_ino_lst; ino_p; ino_p = ino_p->ino_next_p) {
		if (px_lib_intr_setstate(dip, ino_p->ino_sysino,
		    INTR_IDLE_STATE) != DDI_SUCCESS)
			return (BF_FATAL);
	}

	mutex_exit(&ib_p->ib_ino_lst_mutex);

	return (BF_NONE);
}

/*ARGSUSED*/
int
px_lib_iommu_map(dev_info_t *dip, tsbid_t tsbid, pages_t pages,
    io_attributes_t attr, void *addr, size_t pfn_index, int flags)
{
	tsbnum_t	tsb_num = PCI_TSBID_TO_TSBNUM(tsbid);
	tsbindex_t	tsb_index = PCI_TSBID_TO_TSBINDEX(tsbid);
	io_page_list_t	*pfns, *pfn_p;
	pages_t		ttes_mapped = 0;
	int		i, err = DDI_SUCCESS;

	DBG(DBG_LIB_DMA, dip, "px_lib_iommu_map: dip 0x%p tsbid 0x%llx "
	    "pages 0x%x attr 0x%llx addr 0x%p pfn_index 0x%llx flags 0x%x\n",
	    dip, tsbid, pages, attr, addr, pfn_index, flags);

	if ((pfns = pfn_p = kmem_zalloc((pages * sizeof (io_page_list_t)),
	    KM_NOSLEEP)) == NULL) {
		DBG(DBG_LIB_DMA, dip, "px_lib_iommu_map: kmem_zalloc failed\n");
		return (DDI_FAILURE);
	}

	for (i = 0; i < pages; i++)
		pfns[i] = MMU_PTOB(PX_ADDR2PFN(addr, pfn_index, flags, i));

	/*
	 * If HV VPCI version is 2.0 and higher, pass BDF, phantom function,
	 * and relaxed ordering attributes. Otherwise, pass only read or write
	 * attribute.
	 */
	if ((px_vpci_maj_ver == PX_HSVC_MAJOR_VER_1) &&
	    (px_vpci_min_ver == PX_HSVC_MINOR_VER_0))
		attr = attr & (PCI_MAP_ATTR_READ | PCI_MAP_ATTR_WRITE);

	while ((ttes_mapped = pfn_p - pfns) < pages) {
		uintptr_t	ra = va_to_pa(pfn_p);
		pages_t		ttes2map;
		uint64_t	ret;

		ttes2map = (MMU_PAGE_SIZE - P2PHASE(ra, MMU_PAGE_SIZE)) >> 3;
		ra = MMU_PTOB(MMU_BTOP(ra));

		for (ttes2map = MIN(ttes2map, pages - ttes_mapped); ttes2map;
		    ttes2map -= ttes_mapped, pfn_p += ttes_mapped) {

			ttes_mapped = 0;
			if ((ret = hvio_iommu_map(DIP_TO_HANDLE(dip),
			    PCI_TSBID(tsb_num, tsb_index + (pfn_p - pfns)),
			    ttes2map, attr, (io_page_list_t *)(ra |
			    ((uintptr_t)pfn_p & MMU_PAGE_OFFSET)),
			    &ttes_mapped)) != H_EOK) {
				DBG(DBG_LIB_DMA, dip, "hvio_iommu_map failed "
				    "ret 0x%lx\n", ret);

				ttes_mapped = pfn_p - pfns;
				err = DDI_FAILURE;
				goto cleanup;
			}

			DBG(DBG_LIB_DMA, dip, "px_lib_iommu_map: tsb_num 0x%x "
			    "tsb_index 0x%lx ttes_to_map 0x%lx attr 0x%llx "
			    "ra 0x%p ttes_mapped 0x%x\n", tsb_num,
			    tsb_index + (pfn_p - pfns), ttes2map, attr,
			    ra | ((uintptr_t)pfn_p & MMU_PAGE_OFFSET),
			    ttes_mapped);
		}
	}

cleanup:
	if ((err == DDI_FAILURE) && ttes_mapped)
		(void) px_lib_iommu_demap(dip, tsbid, ttes_mapped);

	kmem_free(pfns, pages * sizeof (io_page_list_t));
	return (err);
}

/*ARGSUSED*/
int
px_lib_iommu_demap(dev_info_t *dip, tsbid_t tsbid, pages_t pages)
{
	tsbnum_t	tsb_num = PCI_TSBID_TO_TSBNUM(tsbid);
	tsbindex_t	tsb_index = PCI_TSBID_TO_TSBINDEX(tsbid);
	pages_t		ttes2demap, ttes_demapped = 0;
	uint64_t	ret;

	DBG(DBG_LIB_DMA, dip, "px_lib_iommu_demap: dip 0x%p tsbid 0x%llx "
	    "pages 0x%x\n", dip, tsbid, pages);

	for (ttes2demap = pages; ttes2demap;
	    ttes2demap -= ttes_demapped, tsb_index += ttes_demapped) {
		if ((ret = hvio_iommu_demap(DIP_TO_HANDLE(dip),
		    PCI_TSBID(tsb_num, tsb_index), ttes2demap,
		    &ttes_demapped)) != H_EOK) {
			DBG(DBG_LIB_DMA, dip, "hvio_iommu_demap failed, "
			    "ret 0x%lx\n", ret);

			return (DDI_FAILURE);
		}

		DBG(DBG_LIB_DMA, dip, "px_lib_iommu_demap: tsb_num 0x%x "
		    "tsb_index 0x%lx ttes_to_demap 0x%lx ttes_demapped 0x%x\n",
		    tsb_num, tsb_index, ttes2demap, ttes_demapped);
	}

	return (DDI_SUCCESS);
}

/*ARGSUSED*/
int
px_lib_iommu_getmap(dev_info_t *dip, tsbid_t tsbid, io_attributes_t *attr_p,
    r_addr_t *r_addr_p)
{
	uint64_t	ret;

	DBG(DBG_LIB_DMA, dip, "px_lib_iommu_getmap: dip 0x%p tsbid 0x%llx\n",
	    dip, tsbid);

	if ((ret = hvio_iommu_getmap(DIP_TO_HANDLE(dip), tsbid,
	    attr_p, r_addr_p)) != H_EOK) {
		DBG(DBG_LIB_DMA, dip,
		    "hvio_iommu_getmap failed, ret 0x%lx\n", ret);

		return ((ret == H_ENOMAP) ? DDI_DMA_NOMAPPING:DDI_FAILURE);
	}

	DBG(DBG_LIB_DMA, dip, "px_lib_iommu_getmap: attr 0x%llx "
	    "r_addr 0x%llx\n", *attr_p, *r_addr_p);

	return (DDI_SUCCESS);
}

/*ARGSUSED*/
int
px_lib_iommu_detach(px_t *px_p)
{
	return (DDI_SUCCESS);
}

/*ARGSUSED*/
uint64_t
px_get_rng_parent_hi_mask(px_t *px_p)
{
	return (PX_RANGE_PROP_MASK);
}

/*
 * Checks dma attributes against system bypass ranges
 * A sun4v device must be capable of generating the entire 64-bit
 * address in order to perform bypass DMA.
 */
/*ARGSUSED*/
int
px_lib_dma_bypass_rngchk(dev_info_t *dip, ddi_dma_attr_t *attr_p,
    uint64_t *lo_p, uint64_t *hi_p)
{
	if ((attr_p->dma_attr_addr_lo != 0ull) ||
	    (attr_p->dma_attr_addr_hi != UINT64_MAX)) {

		return (DDI_DMA_BADATTR);
	}

	*lo_p = 0ull;
	*hi_p = UINT64_MAX;

	return (DDI_SUCCESS);
}


/*ARGSUSED*/
int
px_lib_iommu_getbypass(dev_info_t *dip, r_addr_t ra, io_attributes_t attr,
    io_addr_t *io_addr_p)
{
	uint64_t	ret;

	DBG(DBG_LIB_DMA, dip, "px_lib_iommu_getbypass: dip 0x%p ra 0x%llx "
	    "attr 0x%llx\n", dip, ra, attr);
	/*
	 * If HV VPCI version is 2.0 and higher, pass BDF, phantom function,
	 * and relaxed ordering attributes. Otherwise, pass only read or write
	 * attribute.
	 */
	if ((px_vpci_maj_ver == PX_HSVC_MAJOR_VER_1) &&
	    (px_vpci_min_ver == PX_HSVC_MINOR_VER_0))
		attr &= PCI_MAP_ATTR_READ | PCI_MAP_ATTR_WRITE;

	if ((ret = hvio_iommu_getbypass(DIP_TO_HANDLE(dip), ra,
	    attr, io_addr_p)) != H_EOK) {
		DBG(DBG_LIB_DMA, dip,
		    "hvio_iommu_getbypass failed, ret 0x%lx\n", ret);
		return (ret == H_ENOTSUPPORTED ? DDI_ENOTSUP : DDI_FAILURE);
	}

	DBG(DBG_LIB_DMA, dip, "px_lib_iommu_getbypass: io_addr 0x%llx\n",
	    *io_addr_p);

	return (DDI_SUCCESS);
}

/*
 * Returns any needed IO address bit(s) for relaxed ordering in IOMMU
 * bypass mode.
 */
/* ARGSUSED */
uint64_t
px_lib_ro_bypass(dev_info_t *dip, io_attributes_t attr, uint64_t ioaddr)
{
	return (ioaddr);
}

/*ARGSUSED*/
int
px_lib_dma_sync(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handle,
	off_t off, size_t len, uint_t cache_flags)
{
	ddi_dma_impl_t *mp = (ddi_dma_impl_t *)handle;
	uint64_t sync_dir;
	size_t bytes_synced;
	int end, idx;
	off_t pg_off;
	devhandle_t hdl = DIP_TO_HANDLE(dip); /* need to cache hdl */

	DBG(DBG_LIB_DMA, dip, "px_lib_dma_sync: dip 0x%p rdip 0x%p "
	    "handle 0x%llx off 0x%x len 0x%x flags 0x%x\n",
	    dip, rdip, handle, off, len, cache_flags);

	if (!(mp->dmai_flags & PX_DMAI_FLAGS_INUSE)) {
		cmn_err(CE_WARN, "%s%d: Unbound dma handle %p.",
		    ddi_driver_name(rdip), ddi_get_instance(rdip), (void *)mp);
		return (DDI_FAILURE);
	}

	if (mp->dmai_flags & PX_DMAI_FLAGS_NOSYNC)
		return (DDI_SUCCESS);

	if (!len)
		len = mp->dmai_size;

	if (mp->dmai_rflags & DDI_DMA_READ)
		sync_dir = HVIO_DMA_SYNC_DIR_FROM_DEV;
	else
		sync_dir = HVIO_DMA_SYNC_DIR_TO_DEV;

	off += mp->dmai_offset;
	pg_off = off & MMU_PAGEOFFSET;

	DBG(DBG_LIB_DMA, dip, "px_lib_dma_sync: page offset %x size %x\n",
	    pg_off, len);

	/* sync on page basis */
	end = MMU_BTOPR(off + len - 1);
	for (idx = MMU_BTOP(off); idx < end; idx++,
	    len -= bytes_synced, pg_off = 0) {
		size_t bytes_to_sync = bytes_to_sync =
		    MIN(len, MMU_PAGESIZE - pg_off);

		if (hvio_dma_sync(hdl, MMU_PTOB(PX_GET_MP_PFN(mp, idx)) +
		    pg_off, bytes_to_sync, sync_dir, &bytes_synced) != H_EOK)
			break;

		DBG(DBG_LIB_DMA, dip, "px_lib_dma_sync: Called hvio_dma_sync "
		    "ra = %p bytes to sync = %x bytes synced %x\n",
		    MMU_PTOB(PX_GET_MP_PFN(mp, idx)) + pg_off, bytes_to_sync,
		    bytes_synced);

		if (bytes_to_sync != bytes_synced)
			break;
	}

	return (len ? DDI_FAILURE : DDI_SUCCESS);
}


/*
 * MSIQ Functions:
 */

/*ARGSUSED*/
int
px_lib_msiq_init(dev_info_t *dip)
{
	px_t		*px_p = DIP_TO_STATE(dip);
	px_msiq_state_t	*msiq_state_p = &px_p->px_ib_p->ib_msiq_state;
	r_addr_t	ra;
	size_t		msiq_size;
	uint_t		rec_cnt;
	int		i, err = DDI_SUCCESS;
	uint64_t	ret;

	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_init: dip 0x%p\n", dip);

	msiq_size = msiq_state_p->msiq_rec_cnt * sizeof (msiq_rec_t);

	/* sun4v requires all EQ allocation to be on q size boundary */
	if ((msiq_state_p->msiq_buf_p = contig_mem_alloc_align(
	    msiq_state_p->msiq_cnt * msiq_size, msiq_size)) == NULL) {
		DBG(DBG_LIB_MSIQ, dip,
		    "px_lib_msiq_init: Contig alloc failed\n");

		return (DDI_FAILURE);
	}

	for (i = 0; i < msiq_state_p->msiq_cnt; i++) {
		msiq_state_p->msiq_p[i].msiq_base_p = (msiqhead_t *)
		    ((caddr_t)msiq_state_p->msiq_buf_p + (i * msiq_size));

		ra = (r_addr_t)va_to_pa((caddr_t)msiq_state_p->msiq_buf_p +
		    (i * msiq_size));

		if ((ret = hvio_msiq_conf(DIP_TO_HANDLE(dip),
		    (i + msiq_state_p->msiq_1st_msiq_id),
		    ra, msiq_state_p->msiq_rec_cnt)) != H_EOK) {
			DBG(DBG_LIB_MSIQ, dip,
			    "hvio_msiq_conf failed, ret 0x%lx\n", ret);
			err = DDI_FAILURE;
			break;
		}

		if ((err = px_lib_msiq_info(dip,
		    (i + msiq_state_p->msiq_1st_msiq_id),
		    &ra, &rec_cnt)) != DDI_SUCCESS) {
			DBG(DBG_LIB_MSIQ, dip,
			    "px_lib_msiq_info failed, ret 0x%x\n", err);
			err = DDI_FAILURE;
			break;
		}

		DBG(DBG_LIB_MSIQ, dip,
		    "px_lib_msiq_init: ra 0x%p rec_cnt 0x%x\n", ra, rec_cnt);
	}

	return (err);
}

/*ARGSUSED*/
int
px_lib_msiq_fini(dev_info_t *dip)
{
	px_t		*px_p = DIP_TO_STATE(dip);
	px_msiq_state_t	*msiq_state_p = &px_p->px_ib_p->ib_msiq_state;
	size_t		msiq_size;

	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_fini: dip 0x%p\n", dip);
	msiq_size = msiq_state_p->msiq_rec_cnt * sizeof (msiq_rec_t);

	if (msiq_state_p->msiq_buf_p != NULL)
		contig_mem_free(msiq_state_p->msiq_buf_p,
		    msiq_state_p->msiq_cnt * msiq_size);

	return (DDI_SUCCESS);
}

/*ARGSUSED*/
int
px_lib_msiq_info(dev_info_t *dip, msiqid_t msiq_id, r_addr_t *ra_p,
    uint_t *msiq_rec_cnt_p)
{
	uint64_t	ret;

	DBG(DBG_LIB_MSIQ, dip, "px_msiq_info: dip 0x%p msiq_id 0x%x\n",
	    dip, msiq_id);

	if ((ret = hvio_msiq_info(DIP_TO_HANDLE(dip),
	    msiq_id, ra_p, msiq_rec_cnt_p)) != H_EOK) {
		DBG(DBG_LIB_MSIQ, dip,
		    "hvio_msiq_info failed, ret 0x%lx\n", ret);
		return (DDI_FAILURE);
	}

	DBG(DBG_LIB_MSIQ, dip, "px_msiq_info: ra_p 0x%p msiq_rec_cnt 0x%x\n",
	    ra_p, *msiq_rec_cnt_p);

	return (DDI_SUCCESS);
}

/*ARGSUSED*/
int
px_lib_msiq_getvalid(dev_info_t *dip, msiqid_t msiq_id,
    pci_msiq_valid_state_t *msiq_valid_state)
{
	uint64_t	ret;

	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_getvalid: dip 0x%p msiq_id 0x%x\n",
	    dip, msiq_id);

	if ((ret = hvio_msiq_getvalid(DIP_TO_HANDLE(dip),
	    msiq_id, msiq_valid_state)) != H_EOK) {
		DBG(DBG_LIB_MSIQ, dip,
		    "hvio_msiq_getvalid failed, ret 0x%lx\n", ret);
		return (DDI_FAILURE);
	}

	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_getvalid: msiq_valid_state 0x%x\n",
	    *msiq_valid_state);

	return (DDI_SUCCESS);
}

/*ARGSUSED*/
int
px_lib_msiq_setvalid(dev_info_t *dip, msiqid_t msiq_id,
    pci_msiq_valid_state_t msiq_valid_state)
{
	uint64_t	ret;

	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_setvalid: dip 0x%p msiq_id 0x%x "
	    "msiq_valid_state 0x%x\n", dip, msiq_id, msiq_valid_state);

	if ((ret = hvio_msiq_setvalid(DIP_TO_HANDLE(dip),
	    msiq_id, msiq_valid_state)) != H_EOK) {
		DBG(DBG_LIB_MSIQ, dip,
		    "hvio_msiq_setvalid failed, ret 0x%lx\n", ret);
		return (DDI_FAILURE);
	}

	return (DDI_SUCCESS);
}

/*ARGSUSED*/
int
px_lib_msiq_getstate(dev_info_t *dip, msiqid_t msiq_id,
    pci_msiq_state_t *msiq_state)
{
	uint64_t	ret;

	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_getstate: dip 0x%p msiq_id 0x%x\n",
	    dip, msiq_id);

	if ((ret = hvio_msiq_getstate(DIP_TO_HANDLE(dip),
	    msiq_id, msiq_state)) != H_EOK) {
		DBG(DBG_LIB_MSIQ, dip,
		    "hvio_msiq_getstate failed, ret 0x%lx\n", ret);
		return (DDI_FAILURE);
	}

	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_getstate: msiq_state 0x%x\n",
	    *msiq_state);

	return (DDI_SUCCESS);
}

/*ARGSUSED*/
int
px_lib_msiq_setstate(dev_info_t *dip, msiqid_t msiq_id,
    pci_msiq_state_t msiq_state)
{
	uint64_t	ret;

	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_setstate: dip 0x%p msiq_id 0x%x "
	    "msiq_state 0x%x\n", dip, msiq_id, msiq_state);

	if ((ret = hvio_msiq_setstate(DIP_TO_HANDLE(dip),
	    msiq_id, msiq_state)) != H_EOK) {
		DBG(DBG_LIB_MSIQ, dip,
		    "hvio_msiq_setstate failed, ret 0x%lx\n", ret);
		return (DDI_FAILURE);
	}

	return (DDI_SUCCESS);
}

/*ARGSUSED*/
int
px_lib_msiq_gethead(dev_info_t *dip, msiqid_t msiq_id,
    msiqhead_t *msiq_head_p)
{
	uint64_t	ret;

	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_gethead: dip 0x%p msiq_id 0x%x\n",
	    dip, msiq_id);

	if ((ret = hvio_msiq_gethead(DIP_TO_HANDLE(dip),
	    msiq_id, msiq_head_p)) != H_EOK) {
		DBG(DBG_LIB_MSIQ, dip,
		    "hvio_msiq_gethead failed, ret 0x%lx\n", ret);
		return (DDI_FAILURE);
	}

	*msiq_head_p =  (*msiq_head_p / sizeof (msiq_rec_t));

	DBG(DBG_LIB_MSIQ, dip, "px_msiq_gethead: msiq_head 0x%x\n",
	    *msiq_head_p);

	return (DDI_SUCCESS);
}

/*ARGSUSED*/
int
px_lib_msiq_sethead(dev_info_t *dip, msiqid_t msiq_id,
    msiqhead_t msiq_head)
{
	uint64_t	ret;

	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_sethead: dip 0x%p msiq_id 0x%x "
	    "msiq_head 0x%x\n", dip, msiq_id, msiq_head);

	if ((ret = hvio_msiq_sethead(DIP_TO_HANDLE(dip),
	    msiq_id, msiq_head * sizeof (msiq_rec_t))) != H_EOK) {
		DBG(DBG_LIB_MSIQ, dip,
		    "hvio_msiq_sethead failed, ret 0x%lx\n", ret);
		return (DDI_FAILURE);
	}

	return (DDI_SUCCESS);
}

/*ARGSUSED*/
int
px_lib_msiq_gettail(dev_info_t *dip, msiqid_t msiq_id,
    msiqtail_t *msiq_tail_p)
{
	uint64_t	ret;

	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_gettail: dip 0x%p msiq_id 0x%x\n",
	    dip, msiq_id);

	if ((ret = hvio_msiq_gettail(DIP_TO_HANDLE(dip),
	    msiq_id, msiq_tail_p)) != H_EOK) {
		DBG(DBG_LIB_MSIQ, dip,
		    "hvio_msiq_gettail failed, ret 0x%lx\n", ret);
		return (DDI_FAILURE);
	}

	*msiq_tail_p =  (*msiq_tail_p / sizeof (msiq_rec_t));
	DBG(DBG_LIB_MSIQ, dip, "px_lib_msiq_gettail: msiq_tail 0x%x\n",
	    *msiq_tail_p);

	return (DDI_SUCCESS);
}

/*ARGSUSED*/
void
px_lib_get_msiq_rec(dev_info_t *dip, msiqhead_t *msiq_head_p,
    msiq_rec_t *msiq_rec_p)
{
	msiq_rec_t	*curr_msiq_rec_p = (msiq_rec_t *)msiq_head_p;

	DBG(DBG_LIB_MSIQ, dip, "px_lib_get_msiq_rec: dip 0x%p\n", dip);

	if (!curr_msiq_rec_p->msiq_rec_type) {
		/* Set msiq_rec_type to zero */
		msiq_rec_p->msiq_rec_type = 0;

		return;
	}

	*msiq_rec_p = *curr_msiq_rec_p;
}

/*ARGSUSED*/
void
px_lib_clr_msiq_rec(dev_info_t *dip, msiqhead_t *msiq_head_p)
{
	msiq_rec_t	*curr_msiq_rec_p = (msiq_rec_t *)msiq_head_p;

	DBG(DBG_LIB_MSIQ, dip, "px_lib_clr_msiq_rec: dip 0x%p\n", dip);

	/* Zero out msiq_rec_type field */
	curr_msiq_rec_p->msiq_rec_type  = 0;
}

/*
 * MSI Functions:
 */

/*ARGSUSED*/
int
px_lib_msi_init(dev_info_t *dip)
{
	DBG(DBG_LIB_MSI, dip, "px_lib_msi_init: dip 0x%p\n", dip);

	/* Noop */
	return (DDI_SUCCESS);
}

/*ARGSUSED*/
int
px_lib_msi_getmsiq(dev_info_t *dip, msinum_t msi_num,
    msiqid_t *msiq_id)
{
	uint64_t	ret;

	DBG(DBG_LIB_MSI, dip, "px_lib_msi_getmsiq: dip 0x%p msi_num 0x%x\n",
	    dip, msi_num);

	if ((ret = hvio_msi_getmsiq(DIP_TO_HANDLE(dip),
	    msi_num, msiq_id)) != H_EOK) {
		DBG(DBG_LIB_MSI, dip,
		    "hvio_msi_getmsiq failed, ret 0x%lx\n", ret);
		return (DDI_FAILURE);
	}

	DBG(DBG_LIB_MSI, dip, "px_lib_msi_getmsiq: msiq_id 0x%x\n",
	    *msiq_id);

	return (DDI_SUCCESS);
}

/*ARGSUSED*/
int
px_lib_msi_setmsiq(dev_info_t *dip, msinum_t msi_num,
    msiqid_t msiq_id, msi_type_t msitype)
{
	uint64_t	ret;

	DBG(DBG_LIB_MSI, dip, "px_lib_msi_setmsiq: dip 0x%p msi_num 0x%x "
	    "msq_id 0x%x\n", dip, msi_num, msiq_id);

	if ((ret = hvio_msi_setmsiq(DIP_TO_HANDLE(dip),
	    msi_num, msiq_id, msitype)) != H_EOK) {
		DBG(DBG_LIB_MSI, dip,
		    "hvio_msi_setmsiq failed, ret 0x%lx\n", ret);
		return (DDI_FAILURE);
	}

	return (DDI_SUCCESS);
}

/*ARGSUSED*/
int
px_lib_msi_getvalid(dev_info_t *dip, msinum_t msi_num,
    pci_msi_valid_state_t *msi_valid_state)
{
	uint64_t	ret;

	DBG(DBG_LIB_MSI, dip, "px_lib_msi_getvalid: dip 0x%p msi_num 0x%x\n",
	    dip, msi_num);

	if ((ret = hvio_msi_getvalid(DIP_TO_HANDLE(dip),
	    msi_num, msi_valid_state)) != H_EOK) {
		DBG(DBG_LIB_MSI, dip,
		    "hvio_msi_getvalid failed, ret 0x%lx\n", ret);
		return (DDI_FAILURE);
	}

	DBG(DBG_LIB_MSI, dip, "px_lib_msi_getvalid: msiq_id 0x%x\n",
	    *msi_valid_state);

	return (DDI_SUCCESS);
}

/*ARGSUSED*/
int
px_lib_msi_setvalid(dev_info_t *dip, msinum_t msi_num,
    pci_msi_valid_state_t msi_valid_state)
{
	uint64_t	ret;

	DBG(DBG_LIB_MSI, dip, "px_lib_msi_setvalid: dip 0x%p msi_num 0x%x "
	    "msi_valid_state 0x%x\n", dip, msi_num, msi_valid_state);

	if ((ret = hvio_msi_setvalid(DIP_TO_HANDLE(dip),
	    msi_num, msi_valid_state)) != H_EOK) {
		DBG(DBG_LIB_MSI, dip,
		    "hvio_msi_setvalid failed, ret 0x%lx\n", ret);
		return (DDI_FAILURE);
	}

	return (DDI_SUCCESS);
}

/*ARGSUSED*/
int
px_lib_msi_getstate(dev_info_t *dip, msinum_t msi_num,
    pci_msi_state_t *msi_state)
{
	uint64_t	ret;

	DBG(DBG_LIB_MSI, dip, "px_lib_msi_getstate: dip 0x%p msi_num 0x%x\n",
	    dip, msi_num);

	if ((ret = hvio_msi_getstate(DIP_TO_HANDLE(dip),
	    msi_num, msi_state)) != H_EOK) {
		DBG(DBG_LIB_MSI, dip,
		    "hvio_msi_getstate failed, ret 0x%lx\n", ret);
		return (DDI_FAILURE);
	}

	DBG(DBG_LIB_MSI, dip, "px_lib_msi_getstate: msi_state 0x%x\n",
	    *msi_state);

	return (DDI_SUCCESS);
}

/*ARGSUSED*/
int
px_lib_msi_setstate(dev_info_t *dip, msinum_t msi_num,
    pci_msi_state_t msi_state)
{
	uint64_t	ret;

	DBG(DBG_LIB_MSI, dip, "px_lib_msi_setstate: dip 0x%p msi_num 0x%x "
	    "msi_state 0x%x\n", dip, msi_num, msi_state);

	if ((ret = hvio_msi_setstate(DIP_TO_HANDLE(dip),
	    msi_num, msi_state)) != H_EOK) {
		DBG(DBG_LIB_MSI, dip,
		    "hvio_msi_setstate failed, ret 0x%lx\n", ret);
		return (DDI_FAILURE);
	}

	return (DDI_SUCCESS);
}

/*
 * MSG Functions:
 */

/*ARGSUSED*/
int
px_lib_msg_getmsiq(dev_info_t *dip, pcie_msg_type_t msg_type,
    msiqid_t *msiq_id)
{
	uint64_t	ret;

	DBG(DBG_LIB_MSG, dip, "px_lib_msg_getmsiq: dip 0x%p msg_type 0x%x\n",
	    dip, msg_type);

	if ((ret = hvio_msg_getmsiq(DIP_TO_HANDLE(dip),
	    msg_type, msiq_id)) != H_EOK) {
		DBG(DBG_LIB_MSG, dip,
		    "hvio_msg_getmsiq failed, ret 0x%lx\n", ret);
		return (DDI_FAILURE);
	}

	DBG(DBG_LIB_MSI, dip, "px_lib_msg_getmsiq: msiq_id 0x%x\n",
	    *msiq_id);

	return (DDI_SUCCESS);
}

/*ARGSUSED*/
int
px_lib_msg_setmsiq(dev_info_t *dip, pcie_msg_type_t msg_type,
    msiqid_t msiq_id)
{
	uint64_t	ret;

	DBG(DBG_LIB_MSG, dip, "px_lib_msg_setmsiq: dip 0x%p msg_type 0x%x "
	    "msq_id 0x%x\n", dip, msg_type, msiq_id);

	if ((ret = hvio_msg_setmsiq(DIP_TO_HANDLE(dip),
	    msg_type, msiq_id)) != H_EOK) {
		DBG(DBG_LIB_MSG, dip,
		    "hvio_msg_setmsiq failed, ret 0x%lx\n", ret);
		return (DDI_FAILURE);
	}

	return (DDI_SUCCESS);
}

/*ARGSUSED*/
int
px_lib_msg_getvalid(dev_info_t *dip, pcie_msg_type_t msg_type,
    pcie_msg_valid_state_t *msg_valid_state)
{
	uint64_t	ret;

	DBG(DBG_LIB_MSG, dip, "px_lib_msg_getvalid: dip 0x%p msg_type 0x%x\n",
	    dip, msg_type);

	if ((ret = hvio_msg_getvalid(DIP_TO_HANDLE(dip), msg_type,
	    msg_valid_state)) != H_EOK) {
		DBG(DBG_LIB_MSG, dip,
		    "hvio_msg_getvalid failed, ret 0x%lx\n", ret);
		return (DDI_FAILURE);
	}

	DBG(DBG_LIB_MSI, dip, "px_lib_msg_getvalid: msg_valid_state 0x%x\n",
	    *msg_valid_state);

	return (DDI_SUCCESS);
}

/*ARGSUSED*/
int
px_lib_msg_setvalid(dev_info_t *dip, pcie_msg_type_t msg_type,
    pcie_msg_valid_state_t msg_valid_state)
{
	uint64_t	ret;

	DBG(DBG_LIB_MSG, dip, "px_lib_msg_setvalid: dip 0x%p msg_type 0x%x "
	    "msg_valid_state 0x%x\n", dip, msg_type, msg_valid_state);

	if ((ret = hvio_msg_setvalid(DIP_TO_HANDLE(dip), msg_type,
	    msg_valid_state)) != H_EOK) {
		DBG(DBG_LIB_MSG, dip,
		    "hvio_msg_setvalid failed, ret 0x%lx\n", ret);
		return (DDI_FAILURE);
	}

	return (DDI_SUCCESS);
}

/*
 * Suspend/Resume Functions:
 * Currently unsupported by hypervisor and all functions are noops.
 */
/*ARGSUSED*/
int
px_lib_suspend(dev_info_t *dip)
{
	DBG(DBG_ATTACH, dip, "px_lib_suspend: Not supported\n");

	/* Not supported */
	return (DDI_FAILURE);
}

/*ARGSUSED*/
void
px_lib_resume(dev_info_t *dip)
{
	DBG(DBG_ATTACH, dip, "px_lib_resume: Not supported\n");

	/* Noop */
}

/*
 * Misc Functions:
 * Currently unsupported by hypervisor and all functions are noops.
 */
/*ARGSUSED*/
static int
px_lib_config_get(dev_info_t *dip, pci_device_t bdf, pci_config_offset_t off,
    uint8_t size, pci_cfg_data_t *data_p)
{
	uint64_t	ret;

	DBG(DBG_LIB_CFG, dip, "px_lib_config_get: dip 0x%p, bdf 0x%llx "
	    "off 0x%x size 0x%x\n", dip, bdf, off, size);

	if ((ret = hvio_config_get(DIP_TO_HANDLE(dip), bdf, off,
	    size, data_p)) != H_EOK) {
		DBG(DBG_LIB_CFG, dip,
		    "hvio_config_get failed, ret 0x%lx\n", ret);
		return (DDI_FAILURE);
	}
	DBG(DBG_LIB_CFG, dip, "px_config_get: data 0x%x\n", data_p->dw);

	return (DDI_SUCCESS);
}

/*ARGSUSED*/
static int
px_lib_config_put(dev_info_t *dip, pci_device_t bdf, pci_config_offset_t off,
    uint8_t size, pci_cfg_data_t data)
{
	uint64_t	ret;

	DBG(DBG_LIB_CFG, dip, "px_lib_config_put: dip 0x%p, bdf 0x%llx "
	    "off 0x%x size 0x%x data 0x%llx\n", dip, bdf, off, size, data.qw);

	if ((ret = hvio_config_put(DIP_TO_HANDLE(dip), bdf, off,
	    size, data)) != H_EOK) {
		DBG(DBG_LIB_CFG, dip,
		    "hvio_config_put failed, ret 0x%lx\n", ret);
		return (DDI_FAILURE);
	}

	return (DDI_SUCCESS);
}

static uint32_t
px_pci_config_get(ddi_acc_impl_t *handle, uint32_t *addr, int size)
{
	px_config_acc_pvt_t *px_pvt = (px_config_acc_pvt_t *)
	    handle->ahi_common.ah_bus_private;
	pcie_bus_t *busp = NULL;
	dev_info_t *cdip = NULL;
	uint32_t pci_dev_addr = px_pvt->raddr;
	uint32_t vaddr = px_pvt->vaddr;
	uint16_t off = (uint16_t)(uintptr_t)(addr - vaddr) & 0xfff;
	uint64_t rdata = 0;

	if (px_lib_config_get(px_pvt->dip, pci_dev_addr, off,
	    size, (pci_cfg_data_t *)&rdata) != DDI_SUCCESS)
		/* XXX update error kstats */
		return (0xffffffff);

	if (cdip = pcie_find_dip_by_bdf(px_pvt->dip, pci_dev_addr >> 8))
		busp = PCIE_DIP2BUS(cdip);
	/*
	 * This can be called early, before busp or busp->bus_dom has
	 * been initialized, so check both before invoking
	 * PCIE_IS_ASSIGNED.
	 */
	if (busp && PCIE_BUS2DOM(busp) && PCIE_IS_ASSIGNED(busp)) {
		if (off == PCI_CONF_VENID && size == 2)
			rdata = busp->bus_dev_ven_id & 0xffff;
		else if (off == PCI_CONF_DEVID && size == 2)
			rdata = busp->bus_dev_ven_id >> 16;
		else if (off == PCI_CONF_VENID && size == 4)
			rdata = busp->bus_dev_ven_id;
	}
	return ((uint32_t)rdata);
}

static void
px_pci_config_put(ddi_acc_impl_t *handle, uint32_t *addr,
		int size, pci_cfg_data_t wdata)
{
	px_config_acc_pvt_t *px_pvt = (px_config_acc_pvt_t *)
	    handle->ahi_common.ah_bus_private;
	uint32_t pci_dev_addr = px_pvt->raddr;
	uint32_t vaddr = px_pvt->vaddr;
	uint16_t off = (uint16_t)(uintptr_t)(addr - vaddr) & 0xfff;

	if (px_lib_config_put(px_pvt->dip, pci_dev_addr, off,
	    size, wdata) != DDI_SUCCESS) {
		/*EMPTY*/
		/* XXX update error kstats */
	}
}

static uint8_t
px_pci_config_get8(ddi_acc_impl_t *handle, uint8_t *addr)
{
	return ((uint8_t)px_pci_config_get(handle, (uint32_t *)addr, 1));
}

static uint16_t
px_pci_config_get16(ddi_acc_impl_t *handle, uint16_t *addr)
{
	return ((uint16_t)px_pci_config_get(handle, (uint32_t *)addr, 2));
}

static uint32_t
px_pci_config_get32(ddi_acc_impl_t *handle, uint32_t *addr)
{
	return ((uint32_t)px_pci_config_get(handle, (uint32_t *)addr, 4));
}

static uint64_t
px_pci_config_get64(ddi_acc_impl_t *handle, uint64_t *addr)
{
	uint32_t rdatah, rdatal;

	rdatal = (uint32_t)px_pci_config_get(handle, (uint32_t *)addr, 4);
	rdatah = (uint32_t)px_pci_config_get(handle,
	    (uint32_t *)((char *)addr+4), 4);
	return (((uint64_t)rdatah << 32) | rdatal);
}

static void
px_pci_config_put8(ddi_acc_impl_t *handle, uint8_t *addr, uint8_t data)
{
	pci_cfg_data_t wdata = { 0 };

	wdata.qw = (uint8_t)data;
	px_pci_config_put(handle, (uint32_t *)addr, 1, wdata);
}

static void
px_pci_config_put16(ddi_acc_impl_t *handle, uint16_t *addr, uint16_t data)
{
	pci_cfg_data_t wdata = { 0 };

	wdata.qw = (uint16_t)data;
	px_pci_config_put(handle, (uint32_t *)addr, 2, wdata);
}

static void
px_pci_config_put32(ddi_acc_impl_t *handle, uint32_t *addr, uint32_t data)
{
	pci_cfg_data_t wdata = { 0 };

	wdata.qw = (uint32_t)data;
	px_pci_config_put(handle, (uint32_t *)addr, 4, wdata);
}

static void
px_pci_config_put64(ddi_acc_impl_t *handle, uint64_t *addr, uint64_t data)
{
	pci_cfg_data_t wdata = { 0 };

	wdata.qw = (uint32_t)(data & 0xffffffff);
	px_pci_config_put(handle, (uint32_t *)addr, 4, wdata);
	wdata.qw = (uint32_t)((data >> 32) & 0xffffffff);
	px_pci_config_put(handle, (uint32_t *)((char *)addr+4), 4, wdata);
}

static void
px_pci_config_rep_get8(ddi_acc_impl_t *handle, uint8_t *host_addr,
			uint8_t *dev_addr, size_t repcount, uint_t flags)
{
	if (flags == DDI_DEV_AUTOINCR)
		for (; repcount; repcount--)
			*host_addr++ = px_pci_config_get8(handle, dev_addr++);
	else
		for (; repcount; repcount--)
			*host_addr++ = px_pci_config_get8(handle, dev_addr);
}

/*
 * Function to rep read 16 bit data off the PCI configuration space behind
 * the 21554's host interface.
 */
static void
px_pci_config_rep_get16(ddi_acc_impl_t *handle, uint16_t *host_addr,
			uint16_t *dev_addr, size_t repcount, uint_t flags)
{
	if (flags == DDI_DEV_AUTOINCR)
		for (; repcount; repcount--)
			*host_addr++ = px_pci_config_get16(handle, dev_addr++);
	else
		for (; repcount; repcount--)
			*host_addr++ = px_pci_config_get16(handle, dev_addr);
}

/*
 * Function to rep read 32 bit data off the PCI configuration space behind
 * the 21554's host interface.
 */
static void
px_pci_config_rep_get32(ddi_acc_impl_t *handle, uint32_t *host_addr,
			uint32_t *dev_addr, size_t repcount, uint_t flags)
{
	if (flags == DDI_DEV_AUTOINCR)
		for (; repcount; repcount--)
			*host_addr++ = px_pci_config_get32(handle, dev_addr++);
	else
		for (; repcount; repcount--)
			*host_addr++ = px_pci_config_get32(handle, dev_addr);
}

/*
 * Function to rep read 64 bit data off the PCI configuration space behind
 * the 21554's host interface.
 */
static void
px_pci_config_rep_get64(ddi_acc_impl_t *handle, uint64_t *host_addr,
			uint64_t *dev_addr, size_t repcount, uint_t flags)
{
	if (flags == DDI_DEV_AUTOINCR)
		for (; repcount; repcount--)
			*host_addr++ = px_pci_config_get64(handle, dev_addr++);
	else
		for (; repcount; repcount--)
			*host_addr++ = px_pci_config_get64(handle, dev_addr);
}

/*
 * Function to rep write 8 bit data into the PCI configuration space behind
 * the 21554's host interface.
 */
static void
px_pci_config_rep_put8(ddi_acc_impl_t *handle, uint8_t *host_addr,
			uint8_t *dev_addr, size_t repcount, uint_t flags)
{
	if (flags == DDI_DEV_AUTOINCR)
		for (; repcount; repcount--)
			px_pci_config_put8(handle, dev_addr++, *host_addr++);
	else
		for (; repcount; repcount--)
			px_pci_config_put8(handle, dev_addr, *host_addr++);
}

/*
 * Function to rep write 16 bit data into the PCI configuration space behind
 * the 21554's host interface.
 */
static void
px_pci_config_rep_put16(ddi_acc_impl_t *handle, uint16_t *host_addr,
			uint16_t *dev_addr, size_t repcount, uint_t flags)
{
	if (flags == DDI_DEV_AUTOINCR)
		for (; repcount; repcount--)
			px_pci_config_put16(handle, dev_addr++, *host_addr++);
	else
		for (; repcount; repcount--)
			px_pci_config_put16(handle, dev_addr, *host_addr++);
}

/*
 * Function to rep write 32 bit data into the PCI configuration space behind
 * the 21554's host interface.
 */
static void
px_pci_config_rep_put32(ddi_acc_impl_t *handle, uint32_t *host_addr,
			uint32_t *dev_addr, size_t repcount, uint_t flags)
{
	if (flags == DDI_DEV_AUTOINCR)
		for (; repcount; repcount--)
			px_pci_config_put32(handle, dev_addr++, *host_addr++);
	else
		for (; repcount; repcount--)
			px_pci_config_put32(handle, dev_addr, *host_addr++);
}

/*
 * Function to rep write 64 bit data into the PCI configuration space behind
 * the 21554's host interface.
 */
static void
px_pci_config_rep_put64(ddi_acc_impl_t *handle, uint64_t *host_addr,
			uint64_t *dev_addr, size_t repcount, uint_t flags)
{
	if (flags == DDI_DEV_AUTOINCR)
		for (; repcount; repcount--)
			px_pci_config_put64(handle, dev_addr++, *host_addr++);
	else
		for (; repcount; repcount--)
			px_pci_config_put64(handle, dev_addr, *host_addr++);
}

/*
 * Provide a private access handle to route config access calls to Hypervisor.
 * Beware: Do all error checking for config space accesses before calling
 * this function. ie. do error checking from the calling function.
 * Due to a lack of meaningful error code in DDI, the gauranteed return of
 * DDI_SUCCESS from here makes the code organization readable/easier from
 * the generic code.
 */
/*ARGSUSED*/
int
px_lib_map_vconfig(dev_info_t *dip,
	ddi_map_req_t *mp, pci_config_offset_t off,
	pci_regspec_t *rp, caddr_t *addrp)
{
	int fmcap;
	ndi_err_t *errp;
	on_trap_data_t *otp;
	ddi_acc_hdl_t *hp;
	ddi_acc_impl_t *ap;
	uchar_t busnum;	/* bus number */
	uchar_t devnum;	/* device number */
	uchar_t funcnum; /* function number */
	px_config_acc_pvt_t *px_pvt;

	hp = (ddi_acc_hdl_t *)mp->map_handlep;
	ap = (ddi_acc_impl_t *)hp->ah_platform_private;

	/* Check for mapping teardown operation */
	if ((mp->map_op == DDI_MO_UNMAP) ||
	    (mp->map_op == DDI_MO_UNLOCK)) {
		/* free up memory allocated for the private access handle. */
		px_pvt = (px_config_acc_pvt_t *)hp->ah_bus_private;
		kmem_free((void *)px_pvt, sizeof (px_config_acc_pvt_t));

		/* unmap operation of PCI IO/config space. */
		return (DDI_SUCCESS);
	}

	fmcap = ddi_fm_capable(dip);
	if (DDI_FM_ACC_ERR_CAP(fmcap)) {
		errp = ((ddi_acc_impl_t *)hp)->ahi_err;
		otp = (on_trap_data_t *)errp->err_ontrap;
		otp->ot_handle = (void *)(hp);
		otp->ot_prot = OT_DATA_ACCESS;
		errp->err_status = DDI_FM_OK;
		errp->err_expected = DDI_FM_ERR_UNEXPECTED;
		errp->err_cf = px_err_cfg_hdl_check;
	}

	ap->ahi_get8 = px_pci_config_get8;
	ap->ahi_get16 = px_pci_config_get16;
	ap->ahi_get32 = px_pci_config_get32;
	ap->ahi_get64 = px_pci_config_get64;
	ap->ahi_put8 = px_pci_config_put8;
	ap->ahi_put16 = px_pci_config_put16;
	ap->ahi_put32 = px_pci_config_put32;
	ap->ahi_put64 = px_pci_config_put64;
	ap->ahi_rep_get8 = px_pci_config_rep_get8;
	ap->ahi_rep_get16 = px_pci_config_rep_get16;
	ap->ahi_rep_get32 = px_pci_config_rep_get32;
	ap->ahi_rep_get64 = px_pci_config_rep_get64;
	ap->ahi_rep_put8 = px_pci_config_rep_put8;
	ap->ahi_rep_put16 = px_pci_config_rep_put16;
	ap->ahi_rep_put32 = px_pci_config_rep_put32;
	ap->ahi_rep_put64 = px_pci_config_rep_put64;

	/* Initialize to default check/notify functions */
	ap->ahi_fault = 0;
	ap->ahi_fault_check = i_ddi_acc_fault_check;
	ap->ahi_fault_notify = i_ddi_acc_fault_notify;

	/* allocate memory for our private handle */
	px_pvt = (px_config_acc_pvt_t *)
	    kmem_zalloc(sizeof (px_config_acc_pvt_t), KM_SLEEP);
	hp->ah_bus_private = (void *)px_pvt;

	busnum = PCI_REG_BUS_G(rp->pci_phys_hi);
	devnum = PCI_REG_DEV_G(rp->pci_phys_hi);
	funcnum = PCI_REG_FUNC_G(rp->pci_phys_hi);

	/* set up private data for use during IO routines */

	/* addr needed by the HV APIs */
	px_pvt->raddr = busnum << 16 | devnum << 11 | funcnum << 8;
	/*
	 * Address that specifies the actual offset into the 256MB
	 * memory mapped configuration space, 4K per device.
	 * First 12bits form the offset into 4K config space.
	 * This address is only used during the IO routines to calculate
	 * the offset at which the transaction must be performed.
	 * Drivers bypassing DDI functions to access PCI config space will
	 * panic the system since the following is a bogus virtual address.
	 */
	px_pvt->vaddr = busnum << 20 | devnum << 15 | funcnum << 12 | off;
	px_pvt->dip = dip;

	DBG(DBG_LIB_CFG, dip, "px_config_setup: raddr 0x%x, vaddr 0x%x\n",
	    px_pvt->raddr, px_pvt->vaddr);
	*addrp = (caddr_t)(uintptr_t)px_pvt->vaddr;
	return (DDI_SUCCESS);
}

/*ARGSUSED*/
void
px_lib_map_attr_check(ddi_map_req_t *mp)
{
}

/*
 * px_lib_log_safeacc_err:
 * Imitate a cpu/mem trap call when a peek/poke fails.
 * This will initiate something similar to px_fm_callback.
 */
static void
px_lib_log_safeacc_err(px_t *px_p, ddi_acc_handle_t handle, int fme_flag,
    r_addr_t addr)
{
	uint32_t	addr_high, addr_low;
	pcie_req_id_t	bdf = PCIE_INVALID_BDF;
	pci_ranges_t	*ranges_p;
	int		range_len, i;
	ddi_acc_impl_t *hp = (ddi_acc_impl_t *)handle;
	ddi_fm_error_t derr;

	if (px_fm_enter(px_p) != DDI_SUCCESS)
		return;

	derr.fme_status = DDI_FM_NONFATAL;
	derr.fme_version = DDI_FME_VERSION;
	derr.fme_flag = fme_flag;
	derr.fme_ena = fm_ena_generate(0, FM_ENA_FMT1);
	derr.fme_acc_handle = handle;
	if (hp)
		hp->ahi_err->err_expected = DDI_FM_ERR_EXPECTED;

	addr_high = (uint32_t)(addr >> 32);
	addr_low = (uint32_t)addr;

	/*
	 * Make sure this failed load came from this PCIe port.  Check by
	 * matching the upper 32 bits of the address with the ranges property.
	 */
	range_len = px_p->px_ranges_length / sizeof (pci_ranges_t);
	i = 0;
	for (ranges_p = px_p->px_ranges_p; i < range_len; i++, ranges_p++) {
		if (ranges_p->parent_high == addr_high) {
			switch (ranges_p->child_high & PCI_ADDR_MASK) {
			case PCI_ADDR_CONFIG:
				bdf = (pcie_req_id_t)(addr_low >> 12);
				break;
			default:
				bdf = PCIE_INVALID_BDF;
				break;
			}
			break;
		}
	}

	(void) px_rp_en_q(px_p, bdf, addr, 0);
	(void) px_scan_fabric(px_p, px_p->px_dip, &derr);
	px_fm_exit(px_p);
}


#ifdef  DEBUG
int	px_peekfault_cnt = 0;
int	px_pokefault_cnt = 0;
#endif  /* DEBUG */

/*
 * Do a safe write to a device.
 *
 * When this function is given a handle (cautious access), all errors are
 * suppressed.
 *
 * When this function is not given a handle (poke), only Unsupported Request
 * and Completer Abort errors are suppressed.
 *
 * In all cases, all errors are returned in the function return status.
 */

int
px_lib_ctlops_poke(dev_info_t *dip, dev_info_t *rdip,
    peekpoke_ctlops_t *in_args)
{
	px_t *px_p = DIP_TO_STATE(dip);
	px_pec_t *pec_p = px_p->px_pec_p;
	ddi_acc_impl_t *hp = (ddi_acc_impl_t *)in_args->handle;

	size_t repcount = in_args->repcount;
	size_t size = in_args->size;
	uintptr_t dev_addr = in_args->dev_addr;
	uintptr_t host_addr = in_args->host_addr;

	int err	= DDI_SUCCESS;
	uint64_t hvio_poke_status;
	uint32_t wrt_stat;

	r_addr_t ra;
	uint64_t pokeval;
	pcie_req_id_t bdf;

	ra = (r_addr_t)va_to_pa((void *)dev_addr);
	for (; repcount; repcount--) {

		switch (size) {
		case sizeof (uint8_t):
			pokeval = *(uint8_t *)host_addr;
			break;
		case sizeof (uint16_t):
			pokeval = *(uint16_t *)host_addr;
			break;
		case sizeof (uint32_t):
			pokeval = *(uint32_t *)host_addr;
			break;
		case sizeof (uint64_t):
			pokeval = *(uint64_t *)host_addr;
			break;
		default:
			DBG(DBG_MAP, px_p->px_dip,
			    "poke: invalid size %d passed\n", size);
			err = DDI_FAILURE;
			goto done;
		}

		/*
		 * Grab pokefault mutex since hypervisor does not guarantee
		 * poke serialization.
		 */
		if (hp) {
			i_ndi_busop_access_enter(hp->ahi_common.ah_dip,
			    (ddi_acc_handle_t)hp);
			pec_p->pec_safeacc_type = DDI_FM_ERR_EXPECTED;
		} else {
			mutex_enter(&pec_p->pec_pokefault_mutex);
			pec_p->pec_safeacc_type = DDI_FM_ERR_POKE;
		}

		if (pcie_get_bdf_from_dip(rdip, &bdf) != DDI_SUCCESS) {
			err = DDI_FAILURE;
			goto done;
		}

		hvio_poke_status = hvio_poke(px_p->px_dev_hdl, ra, size,
		    pokeval, bdf << 8, &wrt_stat);

		if ((hvio_poke_status != H_EOK) || (wrt_stat != H_EOK)) {
			err = DDI_FAILURE;
#ifdef  DEBUG
			px_pokefault_cnt++;
#endif
			/*
			 * For CAUTIOUS and POKE access, notify FMA to
			 * cleanup.  Imitate a cpu/mem trap call like in sun4u.
			 */
			px_lib_log_safeacc_err(px_p, (ddi_acc_handle_t)hp,
			    (hp ? DDI_FM_ERR_EXPECTED :
			    DDI_FM_ERR_POKE), ra);

			pec_p->pec_ontrap_data = NULL;
			pec_p->pec_safeacc_type = DDI_FM_ERR_UNEXPECTED;
			if (hp) {
				i_ndi_busop_access_exit(hp->ahi_common.ah_dip,
				    (ddi_acc_handle_t)hp);
			} else {
				mutex_exit(&pec_p->pec_pokefault_mutex);
			}
			goto done;
		}

		pec_p->pec_ontrap_data = NULL;
		pec_p->pec_safeacc_type = DDI_FM_ERR_UNEXPECTED;
		if (hp) {
			i_ndi_busop_access_exit(hp->ahi_common.ah_dip,
			    (ddi_acc_handle_t)hp);
		} else {
			mutex_exit(&pec_p->pec_pokefault_mutex);
		}

		host_addr += size;

		if (in_args->flags == DDI_DEV_AUTOINCR) {
			dev_addr += size;
			ra = (r_addr_t)va_to_pa((void *)dev_addr);
		}
	}

done:
	return (err);
}


/*ARGSUSED*/
int
px_lib_ctlops_peek(dev_info_t *dip, dev_info_t *rdip,
    peekpoke_ctlops_t *in_args, void *result)
{
	px_t *px_p = DIP_TO_STATE(dip);
	px_pec_t *pec_p = px_p->px_pec_p;
	ddi_acc_impl_t *hp = (ddi_acc_impl_t *)in_args->handle;

	size_t repcount = in_args->repcount;
	uintptr_t dev_addr = in_args->dev_addr;
	uintptr_t host_addr = in_args->host_addr;

	r_addr_t ra;
	uint32_t read_status;
	uint64_t hvio_peek_status;
	uint64_t peekval;
	int err = DDI_SUCCESS;

	result = (void *)in_args->host_addr;

	ra = (r_addr_t)va_to_pa((void *)dev_addr);
	for (; repcount; repcount--) {

		/* Lock pokefault mutex so read doesn't mask a poke fault. */
		if (hp) {
			i_ndi_busop_access_enter(hp->ahi_common.ah_dip,
			    (ddi_acc_handle_t)hp);
			pec_p->pec_safeacc_type = DDI_FM_ERR_EXPECTED;
		} else {
			mutex_enter(&pec_p->pec_pokefault_mutex);
			pec_p->pec_safeacc_type = DDI_FM_ERR_PEEK;
		}

		hvio_peek_status = hvio_peek(px_p->px_dev_hdl, ra,
		    in_args->size, &read_status, &peekval);

		if ((hvio_peek_status != H_EOK) || (read_status != H_EOK)) {
			err = DDI_FAILURE;

			/*
			 * For CAUTIOUS and PEEK access, notify FMA to
			 * cleanup.  Imitate a cpu/mem trap call like in sun4u.
			 */
			px_lib_log_safeacc_err(px_p, (ddi_acc_handle_t)hp,
			    (hp ? DDI_FM_ERR_EXPECTED :
			    DDI_FM_ERR_PEEK), ra);

			/* Stuff FFs in host addr if peek. */
			if (hp == NULL) {
				int i;
				uint8_t *ff_addr = (uint8_t *)host_addr;
				for (i = 0; i < in_args->size; i++)
					*ff_addr++ = 0xff;
			}
#ifdef  DEBUG
			px_peekfault_cnt++;
#endif
			pec_p->pec_ontrap_data = NULL;
			pec_p->pec_safeacc_type = DDI_FM_ERR_UNEXPECTED;
			if (hp) {
				i_ndi_busop_access_exit(hp->ahi_common.ah_dip,
				    (ddi_acc_handle_t)hp);
			} else {
				mutex_exit(&pec_p->pec_pokefault_mutex);
			}
			goto done;

		}
		pec_p->pec_ontrap_data = NULL;
		pec_p->pec_safeacc_type = DDI_FM_ERR_UNEXPECTED;
		if (hp) {
			i_ndi_busop_access_exit(hp->ahi_common.ah_dip,
			    (ddi_acc_handle_t)hp);
		} else {
			mutex_exit(&pec_p->pec_pokefault_mutex);
		}

		switch (in_args->size) {
		case sizeof (uint8_t):
			*(uint8_t *)host_addr = (uint8_t)peekval;
			break;
		case sizeof (uint16_t):
			*(uint16_t *)host_addr = (uint16_t)peekval;
			break;
		case sizeof (uint32_t):
			*(uint32_t *)host_addr = (uint32_t)peekval;
			break;
		case sizeof (uint64_t):
			*(uint64_t *)host_addr = (uint64_t)peekval;
			break;
		default:
			DBG(DBG_MAP, px_p->px_dip,
			    "peek: invalid size %d passed\n",
			    in_args->size);
			err = DDI_FAILURE;
			goto done;
		}

		host_addr += in_args->size;

		if (in_args->flags == DDI_DEV_AUTOINCR) {
			dev_addr += in_args->size;
			ra = (r_addr_t)va_to_pa((void *)dev_addr);
		}
	}
done:
	return (err);
}


/* add interrupt vector */
int
px_err_add_intr(px_fault_t *px_fault_p)
{
	px_t	*px_p = DIP_TO_STATE(px_fault_p->px_fh_dip);

	DBG(DBG_LIB_INT, px_p->px_dip,
	    "px_err_add_intr: calling add_ivintr");

	VERIFY(add_ivintr(px_fault_p->px_fh_sysino, PX_ERR_PIL,
	    (intrfunc)px_fault_p->px_err_func, (caddr_t)px_fault_p, NULL,
	    (caddr_t)&px_fault_p->px_intr_payload[0]) == 0);

	DBG(DBG_LIB_INT, px_p->px_dip,
	    "px_err_add_intr: ib_intr_enable ");

	px_ib_intr_enable(px_p, intr_dist_cpuid(), px_fault_p->px_intr_ino);

	return (DDI_SUCCESS);
}

/* remove interrupt vector */
void
px_err_rem_intr(px_fault_t *px_fault_p)
{
	px_t	*px_p = DIP_TO_STATE(px_fault_p->px_fh_dip);

	px_ib_intr_disable(px_p->px_ib_p, px_fault_p->px_intr_ino,
	    IB_INTR_WAIT);

	VERIFY(rem_ivintr(px_fault_p->px_fh_sysino, PX_ERR_PIL) == 0);
}

void
px_cb_intr_redist(void *arg)
{
	px_t	*px_p = (px_t *)arg;
	px_ib_intr_dist_en(px_p->px_dip, intr_dist_cpuid(),
	    px_p->px_inos[PX_INTR_XBC], B_FALSE);
}

int
px_cb_add_intr(px_fault_t *f_p)
{
	px_t	*px_p = DIP_TO_STATE(f_p->px_fh_dip);

	DBG(DBG_LIB_INT, px_p->px_dip,
	    "px_err_add_intr: calling add_ivintr");

	VERIFY(add_ivintr(f_p->px_fh_sysino, PX_ERR_PIL,
	    (intrfunc)f_p->px_err_func, (caddr_t)f_p, NULL,
	    (caddr_t)&f_p->px_intr_payload[0]) == 0);

	intr_dist_add(px_cb_intr_redist, px_p);

	DBG(DBG_LIB_INT, px_p->px_dip,
	    "px_err_add_intr: ib_intr_enable ");

	px_ib_intr_enable(px_p, intr_dist_cpuid(), f_p->px_intr_ino);

	return (DDI_SUCCESS);
}

void
px_cb_rem_intr(px_fault_t *f_p)
{
	intr_dist_rem(px_cb_intr_redist, DIP_TO_STATE(f_p->px_fh_dip));
	px_err_rem_intr(f_p);
}

#ifdef FMA
void
px_fill_rc_status(px_fault_t *px_fault_p, pciex_rc_error_regs_t *rc_status)
{
	px_pec_err_t	*err_pkt;

	err_pkt = (px_pec_err_t *)px_fault_p->px_intr_payload;

	/* initialise all the structure members */
	rc_status->status_valid = 0;

	if (err_pkt->pec_descr.P) {
		/* PCI Status Register */
		rc_status->pci_err_status = err_pkt->pci_err_status;
		rc_status->status_valid |= PCI_ERR_STATUS_VALID;
	}

	if (err_pkt->pec_descr.E) {
		/* PCIe Status Register */
		rc_status->pcie_err_status = err_pkt->pcie_err_status;
		rc_status->status_valid |= PCIE_ERR_STATUS_VALID;
	}

	if (err_pkt->pec_descr.U) {
		rc_status->ue_status = err_pkt->ue_reg_status;
		rc_status->status_valid |= UE_STATUS_VALID;
	}

	if (err_pkt->pec_descr.H) {
		rc_status->ue_hdr1 = err_pkt->hdr[0];
		rc_status->status_valid |= UE_HDR1_VALID;
	}

	if (err_pkt->pec_descr.I) {
		rc_status->ue_hdr2 = err_pkt->hdr[1];
		rc_status->status_valid |= UE_HDR2_VALID;
	}

	/* ue_fst_err_ptr - not available for sun4v?? */


	if (err_pkt->pec_descr.S) {
		rc_status->source_id = err_pkt->err_src_reg;
		rc_status->status_valid |= SOURCE_ID_VALID;
	}

	if (err_pkt->pec_descr.R) {
		rc_status->root_err_status = err_pkt->root_err_status;
		rc_status->status_valid |= CE_STATUS_VALID;
	}
}
#endif

/*ARGSUSED*/
int
px_lib_pmctl(int cmd, px_t *px_p)
{
	return (DDI_FAILURE);
}

/*ARGSUSED*/
uint_t
px_pmeq_intr(caddr_t arg)
{
	return (DDI_INTR_CLAIMED);
}

/*
 * fetch the config space base addr of the root complex
 * note this depends on px structure being initialized
 */
uint64_t
px_lib_get_cfgacc_base(dev_info_t *dip)
{
	int		instance = DIP_TO_INST(dip);
	px_t		*px_p = INST_TO_STATE(instance);

	return (px_p->px_dev_hdl);
}

void
px_panic_domain(px_t *px_p, pcie_req_id_t bdf)
{
	uint64_t	ret;
	dev_info_t	*dip = px_p->px_dip;

	DBG(DBG_ERR_INTR, dip, "px_panic_domain: handle 0x%lx, ino %d, "
	    "bdf<<8 0x%lx\n",
	    (uint64_t)DIP_TO_HANDLE(dip), px_p->px_cb_fault.px_intr_ino,
	    (pci_device_t)bdf << 8);
	if ((ret = pci_error_send(DIP_TO_HANDLE(dip),
	    px_p->px_cb_fault.px_intr_ino, (pci_device_t)bdf << 8)) != H_EOK) {
		DBG(DBG_ERR_INTR, dip, "pci_error_send failed, ret 0x%lx\n",
		    ret);
	} else
		DBG(DBG_ERR_INTR, dip, "pci_error_send worked\n");
}

/*ARGSUSED*/
int
px_lib_hotplug_init(dev_info_t *dip, void *arg)
{
	return (DDI_ENOTSUP);
}

/*ARGSUSED*/
void
px_lib_hotplug_uninit(dev_info_t *dip)
{
}

/*ARGSUSED*/
void
px_hp_intr_redist(px_t *px_p)
{
}

/* Dummy cpr add callback */
/*ARGSUSED*/
void
px_cpr_add_callb(px_t *px_p)
{
}

/* Dummy cpr rem callback */
/*ARGSUSED*/
void
px_cpr_rem_callb(px_t *px_p)
{
}

/*ARGSUSED*/
boolean_t
px_lib_is_in_drain_state(px_t *px_p)
{
	return (B_FALSE);
}

/*
 * There is no IOAPI to get the BDF of the pcie root port nexus at this moment.
 * Assume it is 0x0000, until otherwise noted.  For now, all sun4v platforms
 * have programmed the BDF to be 0x0000.
 */
/*ARGSUSED*/
pcie_req_id_t
px_lib_get_bdf(px_t *px_p)
{
	return (0x0000);
}

int
px_lib_get_root_complex_mps(px_t *px_p, dev_info_t *dip, int *mps)
{
	pci_device_t	bdf = px_lib_get_bdf(px_p);

	if (hvio_get_rp_mps_cap(DIP_TO_HANDLE(dip), bdf, mps) == H_EOK)
		return (DDI_SUCCESS);
	else
		return (DDI_FAILURE);
}

int
px_lib_set_root_complex_mps(px_t *px_p,  dev_info_t *dip, int mps)
{
	pci_device_t	bdf = px_lib_get_bdf(px_p);

	if (hvio_set_rp_mps(DIP_TO_HANDLE(dip), bdf, mps) == H_EOK)
		return (DDI_SUCCESS);
	else
		return (DDI_FAILURE);
}

static int
px_lib_do_count_waiting_dev(dev_info_t *dip, void *arg)
{
	int *count = (int *)arg;
	dev_info_t *cdip = ddi_get_child(dip);

	while (cdip != NULL) {
		/* check if this is an assigned device */
		if (ddi_prop_exists(DDI_DEV_T_NONE, cdip, DDI_PROP_DONTPASS,
		    "ddi-assigned")) {
			DBG(DBG_ATTACH, dip, "px_lib_do_count_waiting_dev: "
			    "Found an assigned dev %p, under bridge %p",
			    cdip, dip);

			/*
			 * Mark this bridge as needing waiting for
			 * CHILD_LOANED will be removed after bridge reports
			 * its readyness back to px driver
			 */
			if (ddi_prop_update_int(DDI_DEV_T_NONE, dip,
			    CHILD_LOANED, 1) == DDI_PROP_SUCCESS)
				(*count)++;
			break;
		}
		cdip = ddi_get_next_sibling(cdip);
	}

	return (DDI_WALK_CONTINUE);
}

static int
px_lib_count_waiting_dev(dev_info_t *dip)
{
	int circular_count;
	int count = 0;

	/* No need to continue if this system is not SDIO capable */
	if (px_sdio_users == 0)
		return (0);

	/* see if px iteslf has assigned children */
	(void) px_lib_do_count_waiting_dev(dip, &count);

	/* scan dev under this px */
	ndi_devi_enter(dip, &circular_count);
	ddi_walk_devs(ddi_get_child(dip), px_lib_do_count_waiting_dev, &count);
	ndi_devi_exit(dip, circular_count);
	return (count);
}

/* Called from px/bridge driver directly to report its readyness */
int
px_lib_fabric_sync(dev_info_t *dip)
{
	px_t *px;
	dev_info_t *rcdip;
	int waitdev;

	/* No need to continue if this system is not SDIO capable */
	if (px_sdio_users == 0)
		return (DDI_SUCCESS);

	/* a valid bridge w/ assigned dev under it? */
	if (ddi_prop_remove(DDI_DEV_T_NONE, dip, CHILD_LOANED) !=
	    DDI_PROP_SUCCESS)
		return (DDI_FAILURE);

	/* find out RC dip */
	for (rcdip = dip; rcdip != NULL; rcdip = ddi_get_parent(rcdip)) {
		if (PCIE_DIP2BUS(rcdip) && PCIE_IS_RC(PCIE_DIP2BUS(rcdip)))
			break;
	}
	if ((rcdip == NULL) || ((px = (px_t *)DIP_TO_STATE(rcdip)) == NULL))
		return (DDI_FAILURE);

	/* are we ready? */
	waitdev = (int)(uintptr_t)px->px_plat_p;
	ASSERT(waitdev);
	DBG(DBG_CTLOPS, rcdip, "px_lib_fabric_sync: "
	    "Px/bridge %p is ready, %d left", rcdip, waitdev - 1);
	--waitdev;
	px->px_plat_p = (void *)(uintptr_t)waitdev;
	if (waitdev != 0)
		return (DDI_SUCCESS);

	/* notify hpyervisor */
	DBG(DBG_CTLOPS, rcdip, "px_lib_fabric_sync: "
	    "Notifying HV that RC %p is ready users=%d", rcdip, px_sdio_users);

	if (pci_iov_root_configured(px->px_dev_hdl) != H_EOK)
		return (DDI_FAILURE);

	return (DDI_SUCCESS);
}