summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/io/hotplug/pcihp/pcihp.c
blob: 46dbcd10a2d4c44f925ee91ac3bd4cb4afd40cc2 (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
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (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 2005 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

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

/*
 * **********************************************************************
 * Extension module for PCI nexus drivers to support PCI Hot Plug feature.
 *
 * DESCRIPTION:
 *    This module basically implements "devctl" and Attachment Point device
 *    nodes for hot plug operations. The cb_ops functions needed for access
 *    to these device nodes are also implemented. For hotplug operations
 *    on Attachment Points it interacts with the hotplug services (HPS)
 *    framework. A pci nexus driver would simply call pcihp_init() in its
 *    attach() function and pcihp_uninit() call in its detach() function.
 * **********************************************************************
 */

#include <sys/conf.h>
#include <sys/kmem.h>
#include <sys/debug.h>
#include <sys/modctl.h>
#include <sys/autoconf.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/sunndi.h>
#include <sys/ddi_impldefs.h>
#include <sys/ndi_impldefs.h>
#include <sys/ddipropdefs.h>
#include <sys/open.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/pci.h>
#include <sys/pci_impl.h>
#include <sys/devctl.h>
#include <sys/hotplug/hpcsvc.h>
#include <sys/hotplug/pci/pcicfg.h>
#include <sys/hotplug/pci/pcihp.h>
#include <sys/sysevent.h>
#include <sys/sysevent/eventdefs.h>
#include <sys/sysevent/dr.h>
#include <sys/fs/dv_node.h>

/*
 * NOTE:
 * This module depends on PCI Configurator module (misc/pcicfg),
 * Hot Plug Services framework module (misc/hpcsvc) and Bus Resource
 * Allocator module (misc/busra).
 */

/*
 * ************************************************************************
 * *** Implementation specific data structures/definitions.		***
 * ************************************************************************
 */

/* soft state */
typedef enum { PCIHP_SOFT_STATE_CLOSED, PCIHP_SOFT_STATE_OPEN,
		PCIHP_SOFT_STATE_OPEN_EXCL } pcihp_soft_state_t;

#define	PCI_MAX_DEVS	32	/* max. number of devices on a pci bus */

/* the following correspond to sysevent defined subclasses */
#define	PCIHP_DR_AP_STATE_CHANGE	0
#define	PCIHP_DR_REQ			1

/*  pcihp_get_soft_state() command argument */
#define	PCIHP_DR_NOOP			0
#define	PCIHP_DR_BUS_CONFIGURE		1
#define	PCIHP_DR_BUS_UNCONFIGURE	2
#define	PCIHP_DR_SLOT_ENTER		4
#define	PCIHP_DR_SLOT_EXIT		8

/*  hot plug bus state */
enum { PCIHP_BUS_INITIALIZING, PCIHP_BUS_UNCONFIGURED,
		PCIHP_BUS_CONFIGURED };

/*
 * Soft state structure associated with each hot plug pci bus instance.
 */
typedef struct pcihp {
	struct pcihp		*nextp;

	/* devinfo pointer to the pci bus node */
	dev_info_t		*dip;

	/* soft state flags: PCIHP_SOFT_STATE_* */
	pcihp_soft_state_t	soft_state;

	/* global mutex to serialize exclusive access to the bus */
	kmutex_t		mutex;

	/* slot information structure */
	struct pcihp_slotinfo {
		hpc_slot_t	slot_hdl;	/* HPS slot handle */
		ap_rstate_t	rstate;		/* state of Receptacle */
		ap_ostate_t	ostate;		/* state of the Occupant */
		ap_condition_t	condition;	/* condition of the occupant */
		time32_t	last_change;	/* XXX needed? */
		uint32_t	event_mask;	/* last event mask registered */
		char		*name;		/* slot logical name */
		uint_t		slot_flags;
		uint16_t	slot_type;	/* slot type: pci or cpci */
		uint16_t	slot_capabilities; /* 64bit, etc. */
		int		hs_csr_location; /* Location of HS_CSR */
		kmutex_t	slot_mutex;	/* mutex to serialize hotplug */
						/* operations on the slot */
	} slotinfo[PCI_MAX_DEVS];

	/* misc. bus attributes */
	uint_t			bus_flags;
	uint_t			bus_state;
	uint_t			slots_active;
} pcihp_t;

/*
 * Bit definitions for slot_flags field:
 *
 *	PCIHP_SLOT_AUTO_CFG_EN	This flags is set if nexus can do auto
 *				configuration of hot plugged card on this slot
 *				if the hardware reports the hot plug events.
 *
 *	PCIHP_SLOT_DISABLED	Slot is disabled for hotplug operations.
 *
 *	PCIHP_SLOT_NOT_HEALTHY	HEALTHY# signal is not OK on this slot.
 */
#define	PCIHP_SLOT_AUTO_CFG_EN		0x1
#define	PCIHP_SLOT_DISABLED		0x2
#define	PCIHP_SLOT_NOT_HEALTHY		0x4
#define	PCIHP_SLOT_DEV_NON_HOTPLUG	0x8
#define	PCIHP_SLOT_ENUM_INS_PENDING	0x10
#define	PCIHP_SLOT_ENUM_EXT_PENDING	0x20

/*
 * Bit definitions for bus_flags field:
 *
 *	PCIHP_BUS_66MHZ	Bus is running at 66Mhz.
 */
#define	PCIHP_BUS_66MHZ		0x1
#define	PCIHP_BUS_ENUM_RADIAL	0x2

#define	PCIHP_DEVICES_STR		"/devices"

/*
 * control structure for tree walk during configure/unconfigure operation.
 */
struct pcihp_config_ctrl {
	int	pci_dev;	/* PCI device number for the slot */
	uint_t	flags;		/* control flags (see below) */
	int	op;		/* operation: PCIHP_ONLINE or PCIHP_OFFLINE */
	int	rv;		/* return error code */
	dev_info_t *dip;	/* dip at which the (first) error occurred */
	hpc_occupant_info_t *occupant;
};

/*
 * control flags for configure/unconfigure operations on the tree.
 *
 * PCIHP_CFG_CONTINUE	continue the operation ignoring errors
 */
#define	PCIHP_CFG_CONTINUE	0x1

#define	PCIHP_ONLINE	1
#define	PCIHP_OFFLINE	0


/* Leaf ops (hotplug controls for target devices) */
static int pcihp_open(dev_t *, int, int, cred_t *);
static int pcihp_close(dev_t, int, int, cred_t *);
static int pcihp_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);

#ifdef DEBUG
static int pcihp_debug = 0;
#define	PCIHP_DEBUG(args)	if (pcihp_debug >= 1) cmn_err args
#define	PCIHP_DEBUG2(args)	if (pcihp_debug >= 2) cmn_err args
#else
#define	PCIHP_DEBUG(args)
#define	PCIHP_DEBUG2(args)
#endif

/*
 * We process ENUM# event one device at a time ie. as soon as we detect
 * that a device has the right ENUM# conditions, we return. If the following
 * variable is set to non-zero, we scan all the devices on the bus
 * for ENUM# conditions.
 */
static int pcihp_enum_scan_all = 0;
/*
 * If HSC driver cannot determine the board type (for example: it may not
 * be possible to differentiate between a Basic Hotswap, Non Hotswap or
 * Non-friendly Full hotswap board), the default board type is assigned
 * to be as defined by the following variable.
 */
static int pcihp_cpci_board_type = HPC_BOARD_CPCI_NON_HS;
static int pcihp_cpci_led_blink = 30;
/*
 * It was noted that the blue LED when written on/off would cause INS/EXT
 * bit to be set causing an extra interrupt. Although the cPCI specifications
 * does not imply this, this behavior is seen with some FHS silicons.
 * Also, handling the INS/EXT bit would control the LED being On/Off.
 * Until the behavior is confirmed, this flag could be used to enable or
 * disable handling the LED.
 * 0 means the silicons handles the LED behavior via the INS/EXT bit.
 * 1 means the software must explicitly do the LED behavior.
 */
static int pcihp_cpci_blue_led = 1;

/* static functions */
static pcihp_t *pcihp_create_soft_state(dev_info_t *dip);
static void pcihp_destroy_soft_state(dev_info_t *dip);
static pcihp_t *pcihp_get_soft_state(dev_info_t *dip, int cmd, int *rv);
static int pcihp_configure_ap(pcihp_t *pcihp_p, int pci_dev);
static int pcihp_unconfigure_ap(pcihp_t *pcihp_p, int pci_dev);
static int pcihp_new_slot_state(dev_info_t *, hpc_slot_t,
	hpc_slot_info_t *, int);
static int pcihp_configure(dev_info_t *, void *);
static int pcihp_event_handler(caddr_t, uint_t);
static dev_info_t *pcihp_devi_find(dev_info_t *dip, uint_t dev, uint_t func);
static int pcihp_match_dev(dev_info_t *dip, void *hdl);
static int pcihp_get_hs_csr(struct pcihp_slotinfo *, ddi_acc_handle_t,
	uint8_t *);
static void pcihp_set_hs_csr(struct pcihp_slotinfo *, ddi_acc_handle_t,
	uint8_t *);
static int pcihp_get_hs_csr_location(ddi_acc_handle_t);
static int pcihp_handle_enum(pcihp_t *, int, int, int);
static void pcihp_hs_csr_op(pcihp_t *, int, int);
static int pcihp_enum_slot(pcihp_t *, struct pcihp_slotinfo *, int, int, int);
static int pcihp_handle_enum_extraction(pcihp_t *, int, int, int);
static int pcihp_handle_enum_insertion(pcihp_t *, int, int, int);
static int pcihp_add_dummy_reg_property(dev_info_t *, uint_t, uint_t, uint_t);
static int pcihp_config_setup(dev_info_t **, ddi_acc_handle_t *,
			dev_info_t **, int, pcihp_t *);
static void pcihp_config_teardown(ddi_acc_handle_t *,
			dev_info_t **, int, pcihp_t *);
static int pcihp_get_board_type(struct pcihp_slotinfo *);
/* sysevent function */
static void pcihp_gen_sysevent(char *, int, int, dev_info_t *, int);

extern int pcicfg_configure(dev_info_t *, uint_t);
extern int pcicfg_unconfigure(dev_info_t *, uint_t);

static int pcihp_list_occupants(dev_info_t *, void *);
static int pcihp_indirect_map(dev_info_t *dip);

#if 0
static void pcihp_probe_slot_state(dev_info_t *, int, hpc_slot_state_t *);
#endif

int pcihp_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
    int flags, char *name, caddr_t valuep, int *lengthp);

struct cb_ops pcihp_cb_ops = {
	pcihp_open,			/* open */
	pcihp_close,			/* close */
	nodev,				/* strategy */
	nodev,				/* print */
	nodev,				/* dump */
	nodev,				/* read */
	nodev,				/* write */
	pcihp_ioctl,			/* ioctl */
	nodev,				/* devmap */
	nodev,				/* mmap */
	nodev,				/* segmap */
	nochpoll,			/* poll */
	pcihp_prop_op,			/* cb_prop_op */
	NULL,				/* streamtab */
	D_NEW | D_MP | D_HOTPLUG,	/* Driver compatibility flag */
	CB_REV,				/* rev */
	nodev,				/* int (*cb_aread)() */
	nodev				/* int (*cb_awrite)() */
};

/*
 * local data
 */

int pcihp_autocfg_enabled = 1; /* auto config is enabled by default */

static kmutex_t pcihp_mutex; /* mutex to protect the following data */
static pcihp_t *pcihp_head = NULL;

static kmutex_t pcihp_open_mutex; /* mutex to protect open/close/uninit */
static int	pci_devlink_flags = 0;

/*
 * Module linkage information for the kernel.
 */
extern struct mod_ops mod_miscops;
static struct modlmisc modlmisc = {
	&mod_miscops,
	"PCI nexus hotplug support v%I%",
};

static struct modlinkage modlinkage = {
	MODREV_1,
	&modlmisc,
	NULL
};

int
_init(void)
{
	int error;

	mutex_init(&pcihp_mutex, NULL, MUTEX_DRIVER, NULL);
	mutex_init(&pcihp_open_mutex, NULL, MUTEX_DRIVER, NULL);
	if ((error = mod_install(&modlinkage)) != 0) {
		mutex_destroy(&pcihp_open_mutex);
		mutex_destroy(&pcihp_mutex);
	}

	return (error);
}

int
_fini(void)
{
	return (EBUSY);
}

int
_info(struct modinfo *modinfop)
{
	return (mod_info(&modlinkage, modinfop));
}

static	pcihp_t *
pcihp_create_soft_state(
	dev_info_t *dip)
{
	pcihp_t	*pcihp_p;

	pcihp_p = kmem_zalloc(sizeof (struct pcihp), KM_SLEEP);

	pcihp_p->dip = dip;
	mutex_init(&pcihp_p->mutex, NULL, MUTEX_DRIVER, NULL);

	mutex_enter(&pcihp_mutex);
	pcihp_p->nextp = pcihp_head;
	pcihp_head = pcihp_p;
	pcihp_p->bus_state = PCIHP_BUS_INITIALIZING;
	pcihp_p->slots_active = 0;
	mutex_exit(&pcihp_mutex);

	return (pcihp_p);
}

static	void
pcihp_destroy_soft_state(
	dev_info_t *dip)
{
	pcihp_t	*p;
	pcihp_t	**pp;

	mutex_enter(&pcihp_mutex);
	pp = &pcihp_head;
	while ((p = *pp) != NULL) {
		if (p->dip == dip) {
			*pp = p->nextp;
			kmem_free(p, sizeof (struct pcihp));
			break;
		}
		pp = &(p->nextp);
	}
	mutex_exit(&pcihp_mutex);
}

/*
 * This function should be imported by client nexus drivers as their
 * devo_getinfo() entry point.
 */

/* ARGSUSED */
int
pcihp_info(
	dev_info_t	*dip,
	ddi_info_cmd_t	cmd,
	void		*arg,
	void		**result)
{
	pcihp_t		*pcihp_p;
	major_t		major;
	minor_t		minor;
	int		instance;

	major = getmajor((dev_t)arg);
	minor = getminor((dev_t)arg);
	instance = PCIHP_AP_MINOR_NUM_TO_INSTANCE(minor);

	switch (cmd) {
	default:
		return (DDI_FAILURE);

	case DDI_INFO_DEVT2INSTANCE:
		*result = (void *)(intptr_t)instance;
		return (DDI_SUCCESS);

	case DDI_INFO_DEVT2DEVINFO:
		mutex_enter(&pcihp_mutex);
		pcihp_p = pcihp_head;
		while (pcihp_p != NULL) {
			if (ddi_name_to_major(ddi_get_name(pcihp_p->dip)) ==
			    major && ddi_get_instance(pcihp_p->dip) ==
			    instance) {
				*result = (void *)pcihp_p->dip;
				mutex_exit(&pcihp_mutex);
				return (DDI_SUCCESS);
			}
			pcihp_p = pcihp_p->nextp;
		}
		mutex_exit(&pcihp_mutex);
		return (DDI_FAILURE);
	}
}

/*
 * This function retrieves the hot plug soft state and performs the
 * following primitive commands while the soft state is locked:
 * mark the bus unconfigured, increment slot activity, decrement
 * slot activity and noop.
 */

/* ARGSUSED */
static	pcihp_t *
pcihp_get_soft_state(
	dev_info_t	*dip, int cmd, int *rv)
{
	pcihp_t		*pcihp_p;

	*rv = PCIHP_SUCCESS;
	mutex_enter(&pcihp_mutex);
	pcihp_p = pcihp_head;
	while (pcihp_p != NULL) {
		if (pcihp_p->dip == dip) {
			switch (cmd) {
			case PCIHP_DR_BUS_UNCONFIGURE:
				if (pcihp_p->slots_active == 0)
					pcihp_p->bus_state =
					    PCIHP_BUS_UNCONFIGURED;
				else
					*rv = PCIHP_FAILURE;
				break;
			case PCIHP_DR_SLOT_ENTER:
				if (pcihp_p->bus_state ==
				    PCIHP_BUS_UNCONFIGURED)
					*rv = PCIHP_FAILURE;
				else
					pcihp_p->slots_active++;
				break;
			case PCIHP_DR_SLOT_EXIT:
				ASSERT(pcihp_p->slots_active > 0);
				if (pcihp_p->slots_active == 0)
					cmn_err(CE_PANIC,
					    "pcihp (%s%d): mismatched slot"
					    " activity",
					    ddi_driver_name(dip),
					    ddi_get_instance(dip));
				else
					pcihp_p->slots_active--;
				break;
			case PCIHP_DR_NOOP:
				break;
			default:
				*rv = PCIHP_FAILURE;
				break;
			}
			mutex_exit(&pcihp_mutex);
			return (pcihp_p);
		}
		pcihp_p = pcihp_p->nextp;
	}
	mutex_exit(&pcihp_mutex);

	return (NULL);
}

/* ARGSUSED3 */
static int
pcihp_open(dev_t *devp, int flags, int otyp, cred_t *credp)
{
	dev_info_t *self;
	pcihp_t *pcihp_p;
	minor_t	minor;
	int pci_dev;
	int rv;

	/*
	 * Make sure the open is for the right file type.
	 */
	if (otyp != OTYP_CHR)
		return (EINVAL);

	mutex_enter(&pcihp_open_mutex);
	/*
	 * Get the soft state structure.
	 */
	if (pcihp_info(NULL, DDI_INFO_DEVT2DEVINFO, (void *)*devp,
	    (void **)&self) != DDI_SUCCESS) {
		mutex_exit(&pcihp_open_mutex);
		return (ENXIO);
	}

	pcihp_p = pcihp_get_soft_state(self, PCIHP_DR_NOOP, &rv);
	ASSERT(pcihp_p != NULL);

	mutex_enter(&pcihp_p->mutex);

	/*
	 * If the pci_dev is valid then the minor device is an
	 * AP. Otherwise it is ":devctl" minor device.
	 */
	minor = getminor(*devp);
	pci_dev = PCIHP_AP_MINOR_NUM_TO_PCI_DEVNUM(minor);
	if (pci_dev < PCI_MAX_DEVS) {
		struct pcihp_slotinfo *slotinfop;

		slotinfop = &pcihp_p->slotinfo[pci_dev];
		if (slotinfop->slot_hdl == NULL) {
			mutex_exit(&pcihp_p->mutex);
			mutex_exit(&pcihp_open_mutex);
			return (ENXIO);
		}
	}

	/*
	 * Handle the open by tracking the device state.
	 *
	 * Note: Needs review w.r.t exclusive access to AP or the bus.
	 * Currently in the pci plug-in we don't use EXCL open at all
	 * so the code below implements EXCL access on the bus.
	 */

	/* enforce exclusive access to the bus */
	if ((pcihp_p->soft_state == PCIHP_SOFT_STATE_OPEN_EXCL) ||
	    ((flags & FEXCL) &&
	    (pcihp_p->soft_state != PCIHP_SOFT_STATE_CLOSED))) {
		mutex_exit(&pcihp_p->mutex);
		mutex_exit(&pcihp_open_mutex);
		return (EBUSY);
	}

	if (flags & FEXCL)
		pcihp_p->soft_state = PCIHP_SOFT_STATE_OPEN_EXCL;
	else
		pcihp_p->soft_state = PCIHP_SOFT_STATE_OPEN;

	mutex_exit(&pcihp_p->mutex);
	mutex_exit(&pcihp_open_mutex);

	return (0);
}

/* ARGSUSED */
static int
pcihp_close(dev_t dev, int flags, int otyp, cred_t *credp)
{
	dev_info_t *self;
	pcihp_t *pcihp_p;
	int rv;

	if (otyp != OTYP_CHR)
		return (EINVAL);

	mutex_enter(&pcihp_open_mutex);

	if (pcihp_info(NULL, DDI_INFO_DEVT2DEVINFO, (void *)dev,
	    (void **)&self) != DDI_SUCCESS) {
		mutex_exit(&pcihp_open_mutex);
		return (ENXIO);
	}

	pcihp_p = pcihp_get_soft_state(self, PCIHP_DR_NOOP, &rv);
	ASSERT(pcihp_p != NULL);

	mutex_enter(&pcihp_p->mutex);
	pcihp_p->soft_state = PCIHP_SOFT_STATE_CLOSED;
	mutex_exit(&pcihp_p->mutex);

	mutex_exit(&pcihp_open_mutex);

	return (0);
}

static int
pcihp_list_occupants(dev_info_t *dip, void *hdl)
{
	int pci_dev;
	struct pcihp_config_ctrl *ctrl = (struct pcihp_config_ctrl *)hdl;
	pci_regspec_t *pci_rp;
	int length;
	major_t major;

	/*
	 * Get the PCI device number information from the devinfo
	 * node. Since the node may not have the address field
	 * setup (this is done in the DDI_INITCHILD of the parent)
	 * we look up the 'reg' property to decode that information.
	 */
	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip,
	    DDI_PROP_DONTPASS, "reg", (int **)&pci_rp,
	    (uint_t *)&length) != DDI_PROP_SUCCESS) {
		ctrl->rv = DDI_FAILURE;
		ctrl->dip = dip;
		return (DDI_WALK_TERMINATE);
	}

	/* get the pci device id information */
	pci_dev = PCI_REG_DEV_G(pci_rp->pci_phys_hi);

	/*
	 * free the memory allocated by ddi_prop_lookup_int_array
	 */
	ddi_prop_free(pci_rp);

	/*
	 * Match the node for the device number of the slot.
	 */
	if (pci_dev == ctrl->pci_dev) { /* node is a match */

		major = ddi_driver_major(dip);

		/*
		 * If the node is not yet attached, then don't list it
		 * as an occupant. This is valid, since nothing can be
		 * consuming it until it is attached, and cfgadm will
		 * ask for the property explicitly which will cause it
		 * to be re-freshed right before checking with rcm.
		 */
		if ((major == -1) || (i_ddi_node_state(dip) < DS_ATTACHED))
			return (DDI_WALK_PRUNECHILD);

		/*
		 * If we have used all our occupants then print mesage
		 * and terminate walk.
		 */
		if (ctrl->occupant->i >= HPC_MAX_OCCUPANTS) {
			cmn_err(CE_WARN,
			    "pcihp (%s%d): unable to list all occupants",
			    ddi_driver_name(ddi_get_parent(dip)),
			    ddi_get_instance(ddi_get_parent(dip)));
			return (DDI_WALK_TERMINATE);
		}

		/*
		 * No need to hold the dip as ddi_walk_devs
		 * has already arranged that for us.
		 */
		ctrl->occupant->id[ctrl->occupant->i] =
		    kmem_alloc(sizeof (char[MAXPATHLEN]), KM_SLEEP);
		(void) ddi_pathname(dip,
		    (char *)ctrl->occupant->id[ctrl->occupant->i]);
		ctrl->occupant->i++;
	}

	/*
	 * continue the walk to the next sibling to look for a match
	 * or to find other nodes if this card is a multi-function card.
	 */
	return (DDI_WALK_PRUNECHILD);
}

static void
pcihp_create_occupant_props_nolock(dev_info_t *self, dev_t dev, int pci_dev)
{
	struct pcihp_config_ctrl ctrl;
	hpc_occupant_info_t *occupant;
	int i;

	occupant = kmem_alloc(sizeof (hpc_occupant_info_t), KM_SLEEP);
	occupant->i = 0;

	ctrl.flags = 0;
	ctrl.dip = NULL;
	ctrl.rv = NDI_SUCCESS;
	ctrl.pci_dev = pci_dev;
	ctrl.op = 55; /* should define DRYRUN */
	ctrl.occupant = occupant;

	ddi_walk_devs(ddi_get_child(self), pcihp_list_occupants,
	    (void *)&ctrl);

	if (occupant->i == 0) {
		/* no occupants right now, need to create stub property */
		char *c[] = { "" };
		(void) ddi_prop_update_string_array(dev, self, "pci-occupant",
		    c, 1);
	} else {
		(void) ddi_prop_update_string_array(dev, self, "pci-occupant",
		    occupant->id, occupant->i);
	}
	for (i = 0; i < occupant->i; i++) {
		kmem_free(occupant->id[i], sizeof (char[MAXPATHLEN]));
	}

	kmem_free(occupant, sizeof (hpc_occupant_info_t));
}

static void
pcihp_create_occupant_props(dev_info_t *self, dev_t dev, int pci_dev)
{
	int circular;

	ndi_devi_enter(self, &circular);
	pcihp_create_occupant_props_nolock(self, dev, pci_dev);
	ndi_devi_exit(self, circular);
}

static void
pcihp_delete_occupant_props(dev_info_t *dip, dev_t dev)
{
	if (ddi_prop_remove(dev, dip, "pci-occupant")
	    != DDI_PROP_SUCCESS)
		return; /* add error handling */

}

/*
 * pcihp_ioctl: devctl hotplug controls
 */
/* ARGSUSED */
static int
pcihp_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
	int *rvalp)
{
	pcihp_t *pcihp_p;
	dev_info_t *self;
	struct devctl_iocdata *dcp;
	uint_t bus_state;
	int rv = 0;
	int pci_dev;
	struct pcihp_slotinfo *slotinfop;
	hpc_slot_state_t rstate;
	devctl_ap_state_t ap_state;
	struct hpc_control_data hpc_ctrldata;
	struct hpc_led_info led_info;
	time_t time;
	int state_locking;
	int state_unlocking;
	int rval;
	char *pathname = NULL;

	/*
	 * read devctl ioctl data before soft state retrieval
	 */
	if ((cmd != DEVCTL_AP_CONTROL) &&
	    ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS)
		return (EFAULT);

	if (pcihp_info(NULL, DDI_INFO_DEVT2DEVINFO, (void *)dev,
	    (void **)&self) != DDI_SUCCESS) {
		if (cmd != DEVCTL_AP_CONTROL)
			ndi_dc_freehdl(dcp);
		return (ENXIO);
	}

	switch (cmd) {
	case DEVCTL_AP_INSERT:
	case DEVCTL_AP_REMOVE:
	case DEVCTL_AP_CONNECT:
	case DEVCTL_AP_DISCONNECT:
	case DEVCTL_AP_CONFIGURE:
	case DEVCTL_AP_UNCONFIGURE:
	case DEVCTL_AP_GETSTATE:
	case DEVCTL_AP_CONTROL:
		state_locking = PCIHP_DR_SLOT_ENTER;
		state_unlocking = PCIHP_DR_SLOT_EXIT;
		break;
	default:
		state_locking = PCIHP_DR_NOOP;
		state_unlocking = PCIHP_DR_NOOP;
		break;
	}

	pcihp_p = pcihp_get_soft_state(self, state_locking, &rval);
	ASSERT(pcihp_p != NULL);

	if (rval == PCIHP_FAILURE) {
		(void) ddi_pathname(pcihp_p->dip, pathname);
		PCIHP_DEBUG((CE_WARN, "Hot Plug bus %s instance is unconfigured"
		    " while slot activity is requested\n", pathname));
		if (cmd != DEVCTL_AP_CONTROL)
			ndi_dc_freehdl(dcp);
		return (EBUSY);
	}

	/*
	 * For attachment points the lower 8 bits of the minor number is the
	 * PCI device number.
	 */
	pci_dev = PCIHP_AP_MINOR_NUM_TO_PCI_DEVNUM(getminor(dev));

	/*
	 * We can use the generic implementation for these ioctls
	 */
	switch (cmd) {
	case DEVCTL_DEVICE_GETSTATE:
	case DEVCTL_DEVICE_ONLINE:
	case DEVCTL_DEVICE_OFFLINE:
	case DEVCTL_BUS_GETSTATE:
		rv = ndi_devctl_ioctl(self, cmd, arg, mode, 0);
		ndi_dc_freehdl(dcp);
		return (rv);
	default:
		break;
	}

	switch (cmd) {

	case DEVCTL_DEVICE_RESET:
		rv = ENOTSUP;
		break;

	case DEVCTL_BUS_QUIESCE:
		if (ndi_get_bus_state(self, &bus_state) == NDI_SUCCESS)
			if (bus_state == BUS_QUIESCED)
				break;
		(void) ndi_set_bus_state(self, BUS_QUIESCED);
		break;

	case DEVCTL_BUS_UNQUIESCE:
		if (ndi_get_bus_state(self, &bus_state) == NDI_SUCCESS)
			if (bus_state == BUS_ACTIVE)
				break;
		(void) ndi_set_bus_state(self, BUS_ACTIVE);
		break;

	case DEVCTL_BUS_RESET:
		rv = ENOTSUP;
		break;

	case DEVCTL_BUS_RESETALL:
		rv = ENOTSUP;
		break;

	case DEVCTL_AP_CONNECT:
	case DEVCTL_AP_DISCONNECT:
		/*
		 * CONNECT(DISCONNECT) the hot plug slot to(from) the bus.
		 *
		 * For cPCI slots this operation is a nop so the HPC
		 * driver may return success if it is a valid operation.
		 */
	case DEVCTL_AP_INSERT:
	case DEVCTL_AP_REMOVE:
		/*
		 * Prepare the slot for INSERT/REMOVE operation.
		 */

		/*
		 * check for valid request:
		 *	1. It is a hotplug slot.
		 *	2. The slot has no occupant that is in
		 *	   the 'configured' state.
		 */
		if (pci_dev >= PCI_MAX_DEVS) {
			rv = ENXIO;
			break;
		}
		slotinfop = &pcihp_p->slotinfo[pci_dev];

		mutex_enter(&slotinfop->slot_mutex);

		if ((slotinfop->slot_hdl == NULL) ||
		    (slotinfop->slot_flags & PCIHP_SLOT_DISABLED)) {
			rv = ENXIO;
			mutex_exit(&slotinfop->slot_mutex);
			break;
		}

		/* the slot occupant must be in the UNCONFIGURED state */
		if (slotinfop->ostate != AP_OSTATE_UNCONFIGURED) {
			rv = EINVAL;
			mutex_exit(&slotinfop->slot_mutex);
			break;
		}
		/*
		 * Call the HPC driver to perform the operation on the slot.
		 */

		switch (cmd) {
		case DEVCTL_AP_INSERT:
			rv = hpc_nexus_insert(slotinfop->slot_hdl, NULL, 0);
			break;
		case DEVCTL_AP_REMOVE:
			rv = hpc_nexus_remove(slotinfop->slot_hdl, NULL, 0);
			break;
		case DEVCTL_AP_CONNECT:
			rv = hpc_nexus_connect(slotinfop->slot_hdl, NULL, 0);
			if (rv == HPC_SUCCESS) {
				slotinfop->rstate = AP_RSTATE_CONNECTED;

				if (drv_getparm(TIME, (void *)&time) !=
				    DDI_SUCCESS)
					slotinfop->last_change = (time_t)-1;
				else
					slotinfop->last_change = (time32_t)time;

				slotinfop = &pcihp_p->slotinfo[pci_dev];
				pcihp_gen_sysevent(slotinfop->name,
						PCIHP_DR_AP_STATE_CHANGE,
						SE_NO_HINT, pcihp_p->dip,
						KM_SLEEP);
			}
			break;
		case DEVCTL_AP_DISCONNECT:
			rv = hpc_nexus_disconnect(slotinfop->slot_hdl, NULL, 0);
			if (rv == HPC_SUCCESS) {
				slotinfop->rstate = AP_RSTATE_DISCONNECTED;

				if (drv_getparm(TIME, (void *)&time) !=
				    DDI_SUCCESS)
					slotinfop->last_change = (time_t)-1;
				else
					slotinfop->last_change = (time32_t)time;

				slotinfop = &pcihp_p->slotinfo[pci_dev];
				pcihp_gen_sysevent(slotinfop->name,
						PCIHP_DR_AP_STATE_CHANGE,
						SE_NO_HINT, pcihp_p->dip,
						KM_SLEEP);
			}
			break;
		}
		mutex_exit(&slotinfop->slot_mutex);

		switch (rv) {
		case HPC_ERR_INVALID:
			rv = ENXIO;
			break;
		case HPC_ERR_NOTSUPPORTED:
			rv = ENOTSUP;
			break;
		case HPC_ERR_FAILED:
			rv = EIO;
			break;
		}

		break;

	case DEVCTL_AP_CONFIGURE:
		/*
		 * **************************************
		 * CONFIGURE the occupant in the slot.
		 * **************************************
		 */

		rv = pcihp_configure_ap(pcihp_p, pci_dev);
		if (rv == HPC_SUCCESS) {
			slotinfop = &pcihp_p->slotinfo[pci_dev];
			pcihp_gen_sysevent(slotinfop->name,
			    PCIHP_DR_AP_STATE_CHANGE,
			    SE_NO_HINT, pcihp_p->dip, KM_SLEEP);
			pcihp_create_occupant_props(self, dev, pci_dev);
		}

		break;

	case DEVCTL_AP_UNCONFIGURE:
		/*
		 * **************************************
		 * UNCONFIGURE the occupant in the slot.
		 * **************************************
		 */

		rv = pcihp_unconfigure_ap(pcihp_p, pci_dev);

		if (rv == HPC_SUCCESS) {

			slotinfop = &pcihp_p->slotinfo[pci_dev];
			pcihp_gen_sysevent(slotinfop->name,
			    PCIHP_DR_AP_STATE_CHANGE,
			    SE_NO_HINT, pcihp_p->dip, KM_SLEEP);
			pcihp_delete_occupant_props(pcihp_p->dip, dev);
		}

		break;

	case DEVCTL_AP_GETSTATE:
	    {
		int mutex_held;

		/*
		 * return the state of Attachment Point.
		 *
		 * If the occupant is in UNCONFIGURED state then
		 * we should get the receptacle state from the
		 * HPC driver because the receptacle state
		 * maintained in the nexus may not be accurate.
		 */

		/*
		 * check for valid request:
		 *	1. It is a hotplug slot.
		 */
		slotinfop = &pcihp_p->slotinfo[pci_dev];

		/* try to acquire the slot mutex */
		mutex_held = mutex_tryenter(&slotinfop->slot_mutex);

		if (pci_dev >= PCI_MAX_DEVS || slotinfop->slot_hdl == NULL) {
			rv = ENXIO;
			if (mutex_held) {
				mutex_exit(&slotinfop->slot_mutex);
			}
			break;
		}

		if (slotinfop->ostate == AP_OSTATE_UNCONFIGURED) {
		    if (hpc_nexus_control(slotinfop->slot_hdl,
			HPC_CTRL_GET_SLOT_STATE, (caddr_t)&rstate) != 0) {
			rv = EIO;
			if (mutex_held)
			    mutex_exit(&slotinfop->slot_mutex);
			break;
		    }
		    slotinfop->rstate = (ap_rstate_t)rstate;
		}

		ap_state.ap_rstate = slotinfop->rstate;
		ap_state.ap_ostate = slotinfop->ostate;
		ap_state.ap_condition = slotinfop->condition;
		ap_state.ap_last_change = slotinfop->last_change;
		ap_state.ap_error_code = 0; /* XXX */
		if (mutex_held)
			ap_state.ap_in_transition = 0; /* AP is not busy */
		else
			ap_state.ap_in_transition = 1; /* AP is busy */

		if (mutex_held)
			mutex_exit(&slotinfop->slot_mutex);

		/* copy the return-AP-state information to the user space */
		if (ndi_dc_return_ap_state(&ap_state, dcp) != NDI_SUCCESS)
			rv = EFAULT;

		break;

	    }
	case DEVCTL_AP_CONTROL:
		/*
		 * HPC control functions:
		 *	HPC_CTRL_ENABLE_SLOT/HPC_CTRL_DISABLE_SLOT
		 *		Changes the state of the slot and preserves
		 *		the state across the reboot.
		 *	HPC_CTRL_ENABLE_AUTOCFG/HPC_CTRL_DISABLE_AUTOCFG
		 *		Enables or disables the auto configuration
		 *		of hot plugged occupant if the hardware
		 *		supports notification of the hot plug
		 *		events.
		 *	HPC_CTRL_GET_LED_STATE/HPC_CTRL_SET_LED_STATE
		 *		Controls the state of an LED.
		 *	HPC_CTRL_GET_SLOT_INFO
		 *		Get slot information data structure
		 *		(hpc_slot_info_t).
		 *	HPC_CTRL_GET_BOARD_TYPE
		 *		Get board type information (hpc_board_type_t).
		 *	HPC_CTRL_GET_CARD_INFO
		 *		Get card information (hpc_card_info_t).
		 *
		 * These control functions are used by the cfgadm plug-in
		 * to implement "-x" and "-v" options.
		 */

		/* copy user ioctl data first */
#ifdef _MULTI_DATAMODEL
		if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
			struct hpc_control32_data hpc_ctrldata32;

			if (copyin((void *)arg, (void *)&hpc_ctrldata32,
				sizeof (struct hpc_control32_data)) != 0) {
				rv = EFAULT;
				break;
			}
			hpc_ctrldata.cmd = hpc_ctrldata32.cmd;
			hpc_ctrldata.data =
				(void *)(intptr_t)hpc_ctrldata32.data;
		}
#else
		if (copyin((void *)arg, (void *)&hpc_ctrldata,
			sizeof (struct hpc_control_data)) != 0) {
			rv = EFAULT;
			break;
		}
#endif

		/*
		 * check for valid request:
		 *	1. It is a hotplug slot.
		 */
		slotinfop = &pcihp_p->slotinfo[pci_dev];

		mutex_enter(&slotinfop->slot_mutex);

		if (pci_dev >= PCI_MAX_DEVS || slotinfop->slot_hdl == NULL) {
			rv = ENXIO;
			mutex_exit(&slotinfop->slot_mutex);
			break;
		}

		switch (hpc_ctrldata.cmd) {

		case HPC_CTRL_GET_LED_STATE:
			/* copy the led info from the user space */
			if (copyin(hpc_ctrldata.data, (void *)&led_info,
					sizeof (hpc_led_info_t)) != 0) {
				rv = EFAULT;
				break;
			}

			/* get the state of LED information */
			if (hpc_nexus_control(slotinfop->slot_hdl,
				HPC_CTRL_GET_LED_STATE,
				(caddr_t)&led_info) != 0) {

				if (rv != ENOTSUP)
					rv = EIO;

				break;
			}

			/* copy the led info to the user space */
			if (copyout((void *)&led_info,
			    hpc_ctrldata.data,
			    sizeof (hpc_led_info_t)) != 0) {
			    rv = EFAULT;
			    break;
			}

			break;

		case HPC_CTRL_SET_LED_STATE:
			/* copy the led info from the user space */
			if (copyin(hpc_ctrldata.data, (void *)&led_info,
			    sizeof (hpc_led_info_t)) != 0) {
			    rv = EFAULT;
			    break;
			}

			/* set the state of an LED */
			rv = hpc_nexus_control(slotinfop->slot_hdl,
			    HPC_CTRL_SET_LED_STATE, (caddr_t)&led_info);

			/*
			 * If the Hotswap Controller does not support
			 * LED management (as you would find typically
			 * in the cPCI industry), then we handle the
			 * blue LED on/off/blink operations, just in
			 * case it helps slot identification.
			 */
			if ((rv == HPC_ERR_NOTSUPPORTED) &&
				(slotinfop->slot_type & HPC_SLOT_TYPE_CPCI)) {
				if (led_info.led != HPC_ATTN_LED)
					break;

				switch (led_info.state) {
					case HPC_LED_OFF:
						pcihp_hs_csr_op(pcihp_p,
						pci_dev,
						HPC_EVENT_SLOT_BLUE_LED_OFF);
						rv = 0;
						break;
					case HPC_LED_ON:
						/*
						 * Please note that leaving
						 * LED ON could be dangerous
						 * as it means it is Ok to
						 * remove the board, which
						 * is not what we want to
						 * convey. So it is upto the
						 * user to take care of this
						 * situation and usage.
						 *
						 * Normally, a Blink command
						 * is more appropriate for
						 * identifying a board.
						 */
						pcihp_hs_csr_op(pcihp_p,
						pci_dev,
						HPC_EVENT_SLOT_BLUE_LED_ON);
						rv = 0;
						break;
					case HPC_LED_BLINK:
					{
						int bl;

						for (bl = 0; bl < 2; bl++) {
						pcihp_hs_csr_op(pcihp_p,
						pci_dev,
						HPC_EVENT_SLOT_BLUE_LED_ON);
						delay(pcihp_cpci_led_blink);
						pcihp_hs_csr_op(pcihp_p,
						pci_dev,
						HPC_EVENT_SLOT_BLUE_LED_OFF);
						delay(pcihp_cpci_led_blink);
						}
						rv = 0;
						break;
					}
					default:
						break;
				}
			}

			if (rv == HPC_ERR_FAILED)
				rv = EIO;
			break;

		case HPC_CTRL_ENABLE_SLOT:

			/*
			 * If slot already enabled, do not send a duplicate
			 * control message to the HPC driver.
			 */
			if ((slotinfop->slot_flags & PCIHP_SLOT_DISABLED) == 0)
				break;

			/* tell the HPC driver also */
			if (hpc_nexus_control(slotinfop->slot_hdl,
					HPC_CTRL_ENABLE_SLOT, NULL)
							!= HPC_SUCCESS) {
				rv = EIO;
				break;
			}

			/*
			 * Enable the slot for hotplug operations.
			 */
			slotinfop->slot_flags &= ~PCIHP_SLOT_DISABLED;

			slotinfop->condition = AP_COND_UNKNOWN;

			/* XXX need to preserve this state across reboot? */

			break;

		case HPC_CTRL_DISABLE_SLOT:

			/* Do not disable if occupant configured */
			if (slotinfop->ostate == AP_OSTATE_CONFIGURED) {
				rv = EAGAIN;
				break;
			}

			/* tell the HPC driver also */
			if (hpc_nexus_control(slotinfop->slot_hdl,
					HPC_CTRL_DISABLE_SLOT, NULL)
							!= HPC_SUCCESS) {
				rv = EIO;
				break;
			}

			/*
			 * Disable the slot for hotplug operations.
			 */
			slotinfop->slot_flags |= PCIHP_SLOT_DISABLED;

			slotinfop->condition = AP_COND_UNUSABLE;

			/* XXX need to preserve this state across reboot? */

			break;

		case HPC_CTRL_ENABLE_AUTOCFG:
			/*
			 * Enable auto configuration on this slot.
			 */
			slotinfop->slot_flags |= PCIHP_SLOT_AUTO_CFG_EN;

			/* tell the HPC driver also */
			(void) hpc_nexus_control(slotinfop->slot_hdl,
				HPC_CTRL_ENABLE_AUTOCFG, NULL);

			if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI)
				pcihp_hs_csr_op(pcihp_p, pci_dev,
					HPC_EVENT_ENABLE_ENUM);
			break;

		case HPC_CTRL_DISABLE_AUTOCFG:
			/*
			 * Disable auto configuration on this slot.
			 */
			slotinfop->slot_flags &= ~PCIHP_SLOT_AUTO_CFG_EN;

			/* tell the HPC driver also */
			(void) hpc_nexus_control(slotinfop->slot_hdl,
			    HPC_CTRL_DISABLE_AUTOCFG, NULL);

			if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI)
				pcihp_hs_csr_op(pcihp_p, pci_dev,
					HPC_EVENT_DISABLE_ENUM);
			break;

		case HPC_CTRL_GET_BOARD_TYPE:
		    {
			hpc_board_type_t board_type;

			/*
			 * Get board type data structure, hpc_board_type_t.
			 */
			board_type = pcihp_get_board_type(slotinfop);
			if (board_type == -1) {
			    rv = ENXIO;
			    break;
			}

			/* copy the board type info to the user space */
			if (copyout((void *)&board_type, hpc_ctrldata.data,
			    sizeof (hpc_board_type_t)) != 0) {
			    rv = ENXIO;
			    break;
			}

			break;
		    }

		case HPC_CTRL_GET_SLOT_INFO:
		    {
			hpc_slot_info_t slot_info;

			/*
			 * Get slot information structure, hpc_slot_info_t.
			 */
			slot_info.version = HPC_SLOT_INFO_VERSION;
			slot_info.slot_type = slotinfop->slot_type;
			slot_info.pci_slot_capabilities =
					slotinfop->slot_capabilities;
			slot_info.pci_dev_num = (uint16_t)pci_dev;
			(void) strcpy(slot_info.pci_slot_name, slotinfop->name);

			/* copy the slot info structure to the user space */
			if (copyout((void *)&slot_info, hpc_ctrldata.data,
			    sizeof (hpc_slot_info_t)) != 0) {
			    rv = EFAULT;
			    break;
			}

			break;
		    }

		case HPC_CTRL_GET_CARD_INFO:
		    {
			hpc_card_info_t card_info;
			ddi_acc_handle_t handle;
			dev_info_t *cdip;

			/*
			 * Get card information structure, hpc_card_info_t.
			 */

			/* verify that the card is configured */
			if ((slotinfop->ostate != AP_OSTATE_CONFIGURED) ||
			    ((cdip = pcihp_devi_find(self, pci_dev,
							0)) == NULL)) {
			    /* either the card is not present or */
			    /* it is not configured.		 */
			    rv = ENXIO;
			    break;
			}

			/*
			 * If declared failed, don't allow Config operations.
			 * Otherwise, if good or failing, it is assumed Ok
			 * to get config data.
			 */
			if (slotinfop->condition == AP_COND_FAILED) {
				rv = EIO;
				break;
			}

			/* get the information from the PCI config header */
			/* for the function 0.				  */
			if (pci_config_setup(cdip, &handle) != DDI_SUCCESS) {
				rv = EIO;
				break;
			}
			card_info.prog_class = pci_config_get8(handle,
						PCI_CONF_PROGCLASS);
			card_info.base_class = pci_config_get8(handle,
						PCI_CONF_BASCLASS);
			card_info.sub_class = pci_config_get8(handle,
						PCI_CONF_SUBCLASS);
			card_info.header_type = pci_config_get8(handle,
						PCI_CONF_HEADER);
			pci_config_teardown(&handle);

			/* copy the card info structure to the user space */
			if (copyout((void *)&card_info, hpc_ctrldata.data,
			    sizeof (hpc_card_info_t)) != 0) {
			    rv = EFAULT;
			    break;
			}

			break;
		    }

		default:
			rv = EINVAL;
			break;
		}

		mutex_exit(&slotinfop->slot_mutex);

		break;

	default:
		rv = ENOTTY;
	}

	if (cmd != DEVCTL_AP_CONTROL)
		ndi_dc_freehdl(dcp);

	(void) pcihp_get_soft_state(self, state_unlocking, &rval);

	return (rv);
}

/*
 * **************************************
 * CONFIGURE the occupant in the slot.
 * **************************************
 */
static int
pcihp_configure_ap(pcihp_t *pcihp_p, int pci_dev)
{
	dev_info_t *self = pcihp_p->dip;
	int rv = HPC_SUCCESS;
	struct pcihp_slotinfo *slotinfop;
	hpc_slot_state_t rstate;
	struct pcihp_config_ctrl ctrl;
	int circular_count;
	time_t time;

	/*
	 * check for valid request:
	 *	1. It is a hotplug slot.
	 *	2. The receptacle is in the CONNECTED state.
	 */
	slotinfop = &pcihp_p->slotinfo[pci_dev];

	mutex_enter(&slotinfop->slot_mutex);

	if ((pci_dev >= PCI_MAX_DEVS) || (slotinfop->slot_hdl == NULL) ||
			(slotinfop->slot_flags & PCIHP_SLOT_DISABLED)) {
		mutex_exit(&slotinfop->slot_mutex);
		return (ENXIO);
	}

	/*
	 * If the occupant is already in (partially?) configured
	 * state then call the ndi_devi_online() on the device
	 * subtree(s) for this attachment point.
	 */

	if (slotinfop->ostate == AP_OSTATE_CONFIGURED) {
		ctrl.flags = PCIHP_CFG_CONTINUE;
		ctrl.rv = NDI_SUCCESS;
		ctrl.dip = NULL;
		ctrl.pci_dev = pci_dev;
		ctrl.op = PCIHP_ONLINE;

		ndi_devi_enter(self, &circular_count);
		ddi_walk_devs(ddi_get_child(self), pcihp_configure,
			(void *)&ctrl);
		ndi_devi_exit(self, circular_count);

		if (ctrl.rv != NDI_SUCCESS) {
			/*
			 * one or more of the devices are not
			 * onlined. How is this to be reported?
			 */
			cmn_err(CE_WARN,
				"pcihp (%s%d): failed to attach one or"
				" more drivers for the card in the slot %s",
				ddi_driver_name(self), ddi_get_instance(self),
				slotinfop->name);
			/* rv = EFAULT; */
		}
		/* tell HPC driver that the occupant is configured */
		(void) hpc_nexus_control(slotinfop->slot_hdl,
			HPC_CTRL_DEV_CONFIGURED, NULL);

		if (drv_getparm(TIME, (void *)&time) != DDI_SUCCESS)
			slotinfop->last_change = (time_t)-1;
		else
			slotinfop->last_change = (time32_t)time;

		mutex_exit(&slotinfop->slot_mutex);
		return (rv);
	}

	/*
	 * Occupant is in the UNCONFIGURED state.
	 */

	/* Check if the receptacle is in the CONNECTED state. */
	if (hpc_nexus_control(slotinfop->slot_hdl,
			HPC_CTRL_GET_SLOT_STATE, (caddr_t)&rstate) != 0) {
		mutex_exit(&slotinfop->slot_mutex);
		return (ENXIO);
	}

	if (rstate == HPC_SLOT_EMPTY) {
		/* error. slot is empty */
		mutex_exit(&slotinfop->slot_mutex);
		return (ENXIO);
	}

	if (rstate != HPC_SLOT_CONNECTED) {
		/* error. either the slot is empty or connect failed */
		mutex_exit(&slotinfop->slot_mutex);
		return (ENXIO);
	}

	slotinfop->rstate = AP_RSTATE_CONNECTED; /* record rstate */

	/* Turn INS and LED off, and start configuration. */
	if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI) {
		pcihp_hs_csr_op(pcihp_p, pci_dev,
				HPC_EVENT_SLOT_CONFIGURE);
		if (pcihp_cpci_blue_led)
			pcihp_hs_csr_op(pcihp_p, pci_dev,
				HPC_EVENT_SLOT_BLUE_LED_OFF);
		slotinfop->slot_flags &= ~PCIHP_SLOT_ENUM_INS_PENDING;
	}

	(void) hpc_nexus_control(slotinfop->slot_hdl,
		HPC_CTRL_DEV_CONFIG_START, NULL);

	/*
	 * Call the configurator to configure the card.
	 */
	if (pcicfg_configure(self, pci_dev) != PCICFG_SUCCESS) {
		if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI) {
			if (pcihp_cpci_blue_led)
				pcihp_hs_csr_op(pcihp_p, pci_dev,
					HPC_EVENT_SLOT_BLUE_LED_ON);
			pcihp_hs_csr_op(pcihp_p, pci_dev,
				HPC_EVENT_SLOT_UNCONFIGURE);
		}
		/* tell HPC driver occupant configure Error */
		(void) hpc_nexus_control(slotinfop->slot_hdl,
			HPC_CTRL_DEV_CONFIG_FAILURE, NULL);
		mutex_exit(&slotinfop->slot_mutex);
		return (EIO);
	}

	/* record the occupant state as CONFIGURED */
	slotinfop->ostate = AP_OSTATE_CONFIGURED;
	slotinfop->condition = AP_COND_OK;

	/* now, online all the devices in the AP */
	ctrl.flags = PCIHP_CFG_CONTINUE;
	ctrl.rv = NDI_SUCCESS;
	ctrl.dip = NULL;
	ctrl.pci_dev = pci_dev;
	ctrl.op = PCIHP_ONLINE;

	ndi_devi_enter(self, &circular_count);
	ddi_walk_devs(ddi_get_child(self), pcihp_configure,
		(void *)&ctrl);
	ndi_devi_exit(self, circular_count);

	if (ctrl.rv != NDI_SUCCESS) {
		/*
		 * one or more of the devices are not
		 * ONLINE'd. How is this to be
		 * reported?
		 */
		cmn_err(CE_WARN,
			"pcihp (%s%d): failed to attach one or"
			" more drivers for the card in"
			" the slot %s",
			ddi_driver_name(pcihp_p->dip),
			ddi_get_instance(pcihp_p->dip),
			slotinfop->name);
		/* rv = EFAULT; */
	}
	/* store HS_CSR location.  No events, jut a read operation. */
	if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI)
		pcihp_hs_csr_op(pcihp_p, pci_dev, -1);

	/* tell HPC driver that the occupant is configured */
	(void) hpc_nexus_control(slotinfop->slot_hdl,
		HPC_CTRL_DEV_CONFIGURED, NULL);

	mutex_exit(&slotinfop->slot_mutex);
	return (rv);
}

/*
 * **************************************
 * UNCONFIGURE the occupant in the slot.
 * **************************************
 */
static int
pcihp_unconfigure_ap(pcihp_t *pcihp_p, int pci_dev)
{
	dev_info_t *self = pcihp_p->dip;
	int rv = HPC_SUCCESS;
	struct pcihp_slotinfo *slotinfop;
	struct pcihp_config_ctrl ctrl;
	int circular_count;
	time_t time;

	/*
	 * check for valid request:
	 *	1. It is a hotplug slot.
	 *	2. The occupant is in the CONFIGURED state.
	 */
	slotinfop = &pcihp_p->slotinfo[pci_dev];

	mutex_enter(&slotinfop->slot_mutex);

	if ((pci_dev >= PCI_MAX_DEVS) || (slotinfop->slot_hdl == NULL) ||
			(slotinfop->slot_flags & PCIHP_SLOT_DISABLED)) {
		mutex_exit(&slotinfop->slot_mutex);
		return (ENXIO);
	}
	/*
	 * The following may not need to be there, as we should
	 * support unconfiguring of boards and free resources
	 * even when the board is not hotswappable. But this is
	 * the only way, we may be able to tell the system
	 * administrator that it is not a hotswap board since
	 * disconnect operation is never called.
	 * This way we help the system administrator from not
	 * accidentally removing a non hotswap board and
	 * possibly destroying it. May be this behavior can
	 * be a default, and can be enabled or disabled via
	 * a global flag.
	 */
	if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI) {
		if (slotinfop->slot_flags &
				PCIHP_SLOT_DEV_NON_HOTPLUG) {
			/* Operation unsupported if no HS board/slot */
			mutex_exit(&slotinfop->slot_mutex);
			return (ENOTSUP);
		}
	}

	/*
	 * If the occupant is in the CONFIGURED state then
	 * call the configurator to unconfigure the slot.
	 */
	if (slotinfop->ostate == AP_OSTATE_CONFIGURED) {

		/*
		 * since potential state change is imminent mask
		 * enum events to prevent the slot from being re-configured
		 */
		pcihp_hs_csr_op(pcihp_p, pci_dev, HPC_EVENT_DISABLE_ENUM);

		/*
		 * Detach all the drivers for the devices in the
		 * slot. Call pcihp_configure() to do this.
		 */
		ctrl.flags = 0;
		ctrl.rv = NDI_SUCCESS;
		ctrl.dip = NULL;
		ctrl.pci_dev = pci_dev;
		ctrl.op = PCIHP_OFFLINE;

		(void) devfs_clean(self, NULL, DV_CLEAN_FORCE);
		ndi_devi_enter(self, &circular_count);
		ddi_walk_devs(ddi_get_child(self), pcihp_configure,
			(void *)&ctrl);
		ndi_devi_exit(self, circular_count);

		if (ctrl.rv != NDI_SUCCESS) {
			/*
			 * Failed to detach one or more drivers
			 * Restore the state of drivers which
			 * are offlined during this operation.
			 */
			ctrl.flags = 0;
			ctrl.rv = NDI_SUCCESS;
			ctrl.dip = NULL;
			ctrl.pci_dev = pci_dev;
			ctrl.op = PCIHP_ONLINE;

			ndi_devi_enter(self, &circular_count);
			ddi_walk_devs(ddi_get_child(self),
				pcihp_configure, (void *)&ctrl);
			ndi_devi_exit(self, circular_count);

			/* tell HPC driver that the occupant is Busy */
			(void) hpc_nexus_control(slotinfop->slot_hdl,
				HPC_CTRL_DEV_UNCONFIG_FAILURE, NULL);

			rv = EBUSY;
		} else {
			(void) hpc_nexus_control(slotinfop->slot_hdl,
				HPC_CTRL_DEV_UNCONFIG_START, NULL);

			if (pcicfg_unconfigure(self,
					pci_dev) == PCICFG_SUCCESS) {
			/*
			 * Now that resources are freed,
			 * clear EXT and Turn LED ON.
			 */
			if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI) {
				pcihp_hs_csr_op(pcihp_p, pci_dev,
					HPC_EVENT_SLOT_UNCONFIGURE);
				if (pcihp_cpci_blue_led)
					pcihp_hs_csr_op(pcihp_p, pci_dev,
						HPC_EVENT_SLOT_BLUE_LED_ON);
				slotinfop->hs_csr_location = 0;
				slotinfop->slot_flags &=
					~(PCIHP_SLOT_DEV_NON_HOTPLUG|
						PCIHP_SLOT_ENUM_EXT_PENDING);
			}
				slotinfop->ostate =
						AP_OSTATE_UNCONFIGURED;
				slotinfop->condition = AP_COND_UNKNOWN;
				/*
				 * send the notification of state change
				 * to the HPC driver.
				 */
				(void) hpc_nexus_control(
					slotinfop->slot_hdl,
					HPC_CTRL_DEV_UNCONFIGURED,
					NULL);
			} else {
				/* tell HPC driver occupant unconfigure Error */
				(void) hpc_nexus_control(slotinfop->slot_hdl,
					HPC_CTRL_DEV_UNCONFIG_FAILURE, NULL);

				rv = EIO;
			}
		}
	}

	if (drv_getparm(TIME, (void *)&time) != DDI_SUCCESS)
		slotinfop->last_change = (time_t)-1;
	else
		slotinfop->last_change = (time32_t)time;

	mutex_exit(&slotinfop->slot_mutex);

	/* unmask enum events again */
	if ((slotinfop->slot_flags & PCIHP_SLOT_AUTO_CFG_EN) == 0) {
		pcihp_hs_csr_op(pcihp_p, pci_dev, HPC_EVENT_ENABLE_ENUM);
	}

	return (rv);
}

/*
 * Accessor function to return pointer to the pci hotplug
 * cb_ops structure.
 */
struct cb_ops *
pcihp_get_cb_ops()
{
	return (&pcihp_cb_ops);
}

/*
 * Setup function to initialize hot plug feature. Returns DDI_SUCCESS
 * for successful initialization, otherwise it returns DDI_FAILURE.
 *
 * It is assumed that this this function is called from the attach()
 * entry point of the PCI nexus driver.
 */

int
pcihp_init(dev_info_t *dip)
{
	pcihp_t *pcihp_p;
	int i;
	caddr_t enum_data;
	int enum_size;
	int rv;

	mutex_enter(&pcihp_open_mutex);

	/*
	 * Make sure that it is not already initialized.
	 */
	if (pcihp_get_soft_state(dip, PCIHP_DR_NOOP, &rv) != NULL) {
		cmn_err(CE_WARN, "%s%d: pcihp instance already initialized!",
		    ddi_driver_name(dip), ddi_get_instance(dip));
		goto cleanup;
	}

	/*
	 * Initialize soft state structure for the bus instance.
	 */
	if ((pcihp_p = pcihp_create_soft_state(dip)) == NULL) {
		cmn_err(CE_WARN, "%s%d: can't allocate pcihp structure",
		    ddi_driver_name(dip), ddi_get_instance(dip));
		goto cleanup;
	}

	pcihp_p->soft_state = PCIHP_SOFT_STATE_CLOSED;
	/* XXX if bus is running at 66Mhz then set PCI_BUS_66MHZ bit */
	pcihp_p->bus_flags = 0;	/* XXX FIX IT */

	/*
	 * If a platform wishes to implement Radial ENUM# routing
	 * a property "enum-impl" must be presented to us with a
	 * string value "radial".
	 * This helps us not go for polling operation (default)
	 * during a ENUM# event.
	 */
	if (ddi_getlongprop(DDI_DEV_T_ANY, dip, 0, "enum-impl",
	    (caddr_t)&enum_data, &enum_size) == DDI_PROP_SUCCESS) {
		if (strcmp(enum_data, "radial") == 0) {
			pcihp_p->bus_flags |= PCIHP_BUS_ENUM_RADIAL;
		}
		kmem_free(enum_data, enum_size);
	}

	for (i = 0; i < PCI_MAX_DEVS; i++) {
		/* initialize slot mutex */
		mutex_init(&pcihp_p->slotinfo[i].slot_mutex, NULL,
						MUTEX_DRIVER, NULL);
	}

	/*
	 *  register the bus instance with the HPS framework.
	 */
	if (hpc_nexus_register_bus(dip, pcihp_new_slot_state, 0) != 0) {
		cmn_err(CE_WARN, "%s%d: failed to register the bus with HPS",
		    ddi_driver_name(dip), ddi_get_instance(dip));
		goto cleanup1;
	}

	/*
	 * Create the "devctl" minor for hot plug support. The minor
	 * number for "devctl" node is in the same format as the AP
	 * minor nodes.
	 */
	if (ddi_create_minor_node(dip, "devctl", S_IFCHR,
	    PCIHP_AP_MINOR_NUM(ddi_get_instance(dip), PCIHP_DEVCTL_MINOR),
	    DDI_NT_NEXUS, 0) != DDI_SUCCESS)
		goto cleanup2;

	/*
	 * Setup resource maps for this bus node. (Note: This can
	 * be done from the attach(9E) of the nexus itself.)
	 */
	(void) pci_resource_setup(dip);

	pcihp_p->bus_state = PCIHP_BUS_CONFIGURED;

	mutex_exit(&pcihp_open_mutex);

	return (DDI_SUCCESS);

cleanup2:
	(void) hpc_nexus_unregister_bus(dip);
cleanup1:
	for (i = 0; i < PCI_MAX_DEVS; i++)
		mutex_destroy(&pcihp_p->slotinfo[i].slot_mutex);
	pcihp_destroy_soft_state(dip);
cleanup:
	mutex_exit(&pcihp_open_mutex);
	return (DDI_FAILURE);
}

/*
 * pcihp_uninit()
 *
 * The bus instance is going away, cleanup any data associated with
 * the management of hot plug slots. It is assumed that this function
 * is called from detach() routine of the PCI nexus driver. Also,
 * it is assumed that no devices on the bus are in the configured state.
 */
int
pcihp_uninit(dev_info_t *dip)
{
	pcihp_t *pcihp_p;
	int i, j;
	int rv;

	mutex_enter(&pcihp_open_mutex);
	/* get a pointer to the soft state structure */
	pcihp_p = pcihp_get_soft_state(dip, PCIHP_DR_BUS_UNCONFIGURE, &rv);
	ASSERT(pcihp_p != NULL);

	/* slot mutexes should prevent any configure/unconfigure access */
	for (i = 0; i < PCI_MAX_DEVS; i++) {
		if (!mutex_tryenter(&pcihp_p->slotinfo[i].slot_mutex)) {
			for (j = 0; j < i; j++) {
				mutex_exit(&pcihp_p->slotinfo[j].slot_mutex);
			}
			mutex_exit(&pcihp_open_mutex);
			return (DDI_FAILURE);
		}
	}

	if ((pcihp_p->soft_state != PCIHP_SOFT_STATE_CLOSED) ||
	    (rv == PCIHP_FAILURE)) {
		cmn_err(CE_WARN, "%s%d: pcihp instance is busy",
		    ddi_driver_name(dip), ddi_get_instance(dip));
		for (i = 0; i < PCI_MAX_DEVS; i++) {
			mutex_exit(&pcihp_p->slotinfo[i].slot_mutex);
		}
		mutex_exit(&pcihp_open_mutex);
		return (DDI_FAILURE);
	}

	/*
	 * Unregister the bus with the HPS.
	 *
	 * (Note: It is assumed that the HPS framework uninstalls
	 *  event handlers for all the hot plug slots on this bus.)
	 */
	(void) hpc_nexus_unregister_bus(dip);

	/* Free up any kmem_alloc'd memory for slot info table. */
	for (i = 0; i < PCI_MAX_DEVS; i++) {
		/* free up slot name strings */
		if (pcihp_p->slotinfo[i].name != NULL)
			kmem_free(pcihp_p->slotinfo[i].name,
				strlen(pcihp_p->slotinfo[i].name) + 1);
	}

	/* destroy slot mutexes */
	for (i = 0; i < PCI_MAX_DEVS; i++)
		mutex_destroy(&pcihp_p->slotinfo[i].slot_mutex);

	ddi_remove_minor_node(dip, NULL);

	/* free up the soft state structure */
	pcihp_destroy_soft_state(dip);

	/*
	 * Destroy resource maps for this bus node. (Note: This can
	 * be done from the detach(9E) of the nexus itself.)
	 */
	(void) pci_resource_destroy(dip);

	mutex_exit(&pcihp_open_mutex);

	return (DDI_SUCCESS);
}

/*
 * pcihp_new_slot_state()
 *
 * This function is called by the HPS when it finds a hot plug
 * slot is added or being removed from the hot plug framework.
 * It returns 0 for success and HPC_ERR_FAILED for errors.
 */
static int
pcihp_new_slot_state(dev_info_t *dip, hpc_slot_t hdl,
	hpc_slot_info_t *slot_info, int slot_state)
{
	pcihp_t *pcihp_p;
	struct pcihp_slotinfo *slotinfop;
	int pci_dev;
	minor_t ap_minor;
	major_t ap_major;
	int rv = 0;
	time_t time;
	int auto_enable = 1;
	int rval;

	/* get a pointer to the soft state structure */
	pcihp_p = pcihp_get_soft_state(dip, PCIHP_DR_SLOT_ENTER, &rval);
	ASSERT(pcihp_p != NULL);

	if (rval == PCIHP_FAILURE) {
		PCIHP_DEBUG((CE_WARN, "pcihp instance is unconfigured"
		    " while slot activity is requested\n"));
		return (HPC_ERR_FAILED);
	}

	pci_dev = slot_info->pci_dev_num;
	slotinfop = &pcihp_p->slotinfo[pci_dev];

	mutex_enter(&slotinfop->slot_mutex);

	switch (slot_state) {

	case HPC_SLOT_ONLINE:

		/*
		 * Make sure the slot is not already ONLINE (paranoia?).
		 * (Note: Should this be simply an ASSERTION?)
		 */
		if (slotinfop->slot_hdl != NULL) {
		    PCIHP_DEBUG((CE_WARN,
			"pcihp (%s%d): pci slot (dev %x) already ONLINE!!",
			ddi_driver_name(dip), ddi_get_instance(dip), pci_dev));
			rv = HPC_ERR_FAILED;
			break;
		}

		/*
		 * Add the hot plug slot to the bus.
		 */

		/* create the AP minor node */
		ap_minor = PCIHP_AP_MINOR_NUM(ddi_get_instance(dip), pci_dev);
		if (ddi_create_minor_node(dip, slot_info->pci_slot_name,
			S_IFCHR, ap_minor,
			DDI_NT_PCI_ATTACHMENT_POINT, 0) == DDI_FAILURE) {
		    cmn_err(CE_WARN,
			"pcihp (%s%d): ddi_create_minor_node failed"
			" for pci dev %x", ddi_driver_name(dip),
			ddi_get_instance(dip), pci_dev);
		    rv = HPC_ERR_FAILED;
		    break;
		}

		/* save the slot handle */
		slotinfop->slot_hdl = hdl;

		/* setup event handler for all hardware events on the slot */
		ap_major = ddi_name_to_major(ddi_get_name(dip));
		if (hpc_install_event_handler(hdl, -1, pcihp_event_handler,
			(caddr_t)makedevice(ap_major, ap_minor)) != 0) {
		    cmn_err(CE_WARN,
			"pcihp (%s%d): install event handler failed"
			" for pci dev %x", ddi_driver_name(dip),
			ddi_get_instance(dip), pci_dev);
		    rv = HPC_ERR_FAILED;
		    break;
		}
		slotinfop->event_mask = (uint32_t)0xFFFFFFFF;

		pcihp_create_occupant_props(dip, makedevice(ap_major,
		    ap_minor), pci_dev);

		/* set default auto configuration enabled flag for this slot */
		slotinfop->slot_flags = pcihp_autocfg_enabled;

		/* copy the slot information */
		slotinfop->name =
		    kmem_alloc(strlen(slot_info->pci_slot_name) + 1, KM_SLEEP);
		(void) strcpy(slotinfop->name, slot_info->pci_slot_name);
		slotinfop->slot_type = slot_info->slot_type;
		slotinfop->hs_csr_location = 0;
		slotinfop->slot_capabilities = slot_info->pci_slot_capabilities;
		if (slot_info->slot_flags & HPC_SLOT_NO_AUTO_ENABLE)
			auto_enable = 0;

		if (slot_info->slot_flags & HPC_SLOT_CREATE_DEVLINK) {
			pci_devlink_flags |= (1 << pci_dev);
			(void) ddi_prop_update_int(DDI_DEV_T_NONE,
						dip,
						"ap-names",
						pci_devlink_flags);
		}

		PCIHP_DEBUG((CE_NOTE,
		    "pcihp (%s%d): pci slot (dev %x) ONLINE\n",
		    ddi_driver_name(dip), ddi_get_instance(dip), pci_dev));

		/*
		 * The slot may have an occupant that was configured
		 * at boot time. If we find a devinfo node in the tree
		 * for this slot (i.e pci device number) then we
		 * record the occupant state as CONFIGURED.
		 */
		if (pcihp_devi_find(dip, pci_dev, 0) != NULL) {
			/* we have a configured occupant */
			slotinfop->ostate = AP_OSTATE_CONFIGURED;
			slotinfop->rstate = AP_RSTATE_CONNECTED;
			slotinfop->condition = AP_COND_OK;

			if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI) {
				/* this will set slot flags too. */
				(void) pcihp_get_board_type(slotinfop);
				pcihp_hs_csr_op(pcihp_p, pci_dev,
					HPC_EVENT_SLOT_CONFIGURE);
				if (pcihp_cpci_blue_led)
					pcihp_hs_csr_op(pcihp_p, pci_dev,
						HPC_EVENT_SLOT_BLUE_LED_OFF);
				/* ENUM# enabled by default for cPCI devices */
				slotinfop->slot_flags |= PCIHP_SLOT_AUTO_CFG_EN;
				slotinfop->slot_flags &=
					~PCIHP_SLOT_ENUM_INS_PENDING;
			}

			/* tell HPC driver that the occupant is configured */
			(void) hpc_nexus_control(slotinfop->slot_hdl,
				HPC_CTRL_DEV_CONFIGURED, NULL);
		} else {
			struct pcihp_config_ctrl ctrl;
			int circular_count;

			slotinfop->ostate = AP_OSTATE_UNCONFIGURED;
			slotinfop->rstate = AP_RSTATE_EMPTY;
			slotinfop->condition = AP_COND_UNKNOWN;

			if (!auto_enable) {	/* no further action */
				break;
			}

			/*
			 * We enable power to the slot and try to
			 * configure if there is any card present.
			 *
			 * Note: This case is possible if the BIOS or
			 * firmware doesn't enable the slots during
			 * soft reboot.
			 */
			if (hpc_nexus_connect(slotinfop->slot_hdl,
				NULL, 0) != HPC_SUCCESS)
				break;

			if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI) {
				pcihp_hs_csr_op(pcihp_p, pci_dev,
					HPC_EVENT_SLOT_CONFIGURE);
				if (pcihp_cpci_blue_led)
					pcihp_hs_csr_op(pcihp_p, pci_dev,
						HPC_EVENT_SLOT_BLUE_LED_OFF);
				slotinfop->slot_flags |= PCIHP_SLOT_AUTO_CFG_EN;
				slotinfop->slot_flags &=
					~PCIHP_SLOT_ENUM_INS_PENDING;
			}

			(void) hpc_nexus_control(slotinfop->slot_hdl,
				HPC_CTRL_DEV_CONFIG_START, NULL);

			/*
			 * Call the configurator to configure the card.
			 */
			if (pcicfg_configure(dip, pci_dev) != PCICFG_SUCCESS) {
				if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI) {
					if (pcihp_cpci_blue_led)
						pcihp_hs_csr_op(pcihp_p,
						pci_dev,
						HPC_EVENT_SLOT_BLUE_LED_ON);
					pcihp_hs_csr_op(pcihp_p, pci_dev,
						HPC_EVENT_SLOT_UNCONFIGURE);
				}

				/* tell HPC driver occupant configure Error */
				(void) hpc_nexus_control(slotinfop->slot_hdl,
					HPC_CTRL_DEV_CONFIG_FAILURE, NULL);

				/*
				 * call HPC driver to turn off the power for
				 * the slot.
				 */
				(void) hpc_nexus_disconnect(slotinfop->slot_hdl,
							NULL, 0);
			} else {
			    /* record the occupant state as CONFIGURED */
			    slotinfop->ostate = AP_OSTATE_CONFIGURED;
			    slotinfop->rstate = AP_RSTATE_CONNECTED;
			    slotinfop->condition = AP_COND_OK;

			    /* now, online all the devices in the AP */
			    ctrl.flags = PCIHP_CFG_CONTINUE;
			    ctrl.rv = NDI_SUCCESS;
			    ctrl.dip = NULL;
			    ctrl.pci_dev = pci_dev;
			    ctrl.op = PCIHP_ONLINE;
				/*
				 * the following sets slot_flags and
				 * hs_csr_location too.
				 */
				(void) pcihp_get_board_type(slotinfop);

			    ndi_devi_enter(dip, &circular_count);
			    ddi_walk_devs(ddi_get_child(dip), pcihp_configure,
				(void *)&ctrl);
			    ndi_devi_exit(dip, circular_count);

			    if (ctrl.rv != NDI_SUCCESS) {
				/*
				 * one or more of the devices are not
				 * ONLINE'd. How is this to be
				 * reported?
				 */
				cmn_err(CE_WARN,
					"pcihp (%s%d): failed to attach one or"
					" more drivers for the card in"
					" the slot %s",
					ddi_driver_name(dip),
					ddi_get_instance(dip),
					slotinfop->name);
			    }

			    /* tell HPC driver about the configured occupant */
			    (void) hpc_nexus_control(slotinfop->slot_hdl,
				HPC_CTRL_DEV_CONFIGURED, NULL);
			}
		}

		break;

	case HPC_SLOT_OFFLINE:
		/*
		 * A hot plug slot is being removed from the bus.
		 * Make sure there is no occupant configured on the
		 * slot before removing the AP minor node.
		 */
		if (slotinfop->ostate != AP_OSTATE_UNCONFIGURED) {
		    cmn_err(CE_WARN, "pcihp (%s%d): Card is still in configured"
			" state for pci dev %x",
			ddi_driver_name(dip), ddi_get_instance(dip), pci_dev);
		    rv = HPC_ERR_FAILED;
		    break;
		}

		/*
		 * If the AP device is in open state then return
		 * error.
		 */
		if (pcihp_p->soft_state != PCIHP_SOFT_STATE_CLOSED) {
		    rv = HPC_ERR_FAILED;
		    break;
		}
		if (slot_info->slot_flags & HPC_SLOT_CREATE_DEVLINK) {
			pci_devlink_flags &= ~(1 << pci_dev);
			(void) ddi_prop_update_int(DDI_DEV_T_NONE,
					dip,
					"ap-names",
					pci_devlink_flags);
		}

		/* remove the minor node */
		ddi_remove_minor_node(dip, slotinfop->name);

		/* free up the memory for the name string */
		kmem_free(slotinfop->name, strlen(slotinfop->name) + 1);

		/* update the slot info data */
		slotinfop->name = NULL;
		slotinfop->slot_hdl = NULL;

		PCIHP_DEBUG((CE_NOTE,
		    "pcihp (%s%d): pci slot (dev %x) OFFLINE\n",
		    ddi_driver_name(dip), ddi_get_instance(dip),
		    slot_info->pci_dev_num));

		break;
	default:
		cmn_err(CE_WARN,
		    "pcihp_new_slot_state: unknown slot_state %d", slot_state);
		rv = HPC_ERR_FAILED;
	}

	if (rv == 0) {
		if (drv_getparm(TIME, (void *)&time) != DDI_SUCCESS)
			slotinfop->last_change = (time_t)-1;
		else
			slotinfop->last_change = (time32_t)time;
	}

	mutex_exit(&slotinfop->slot_mutex);

	(void) pcihp_get_soft_state(dip, PCIHP_DR_SLOT_EXIT, &rval);

	return (rv);
}

/*
 * Event handler. It is assumed that this function is called from
 * a kernel context only.
 *
 * Parameters:
 *	slot_arg	AP minor number.
 *	event_mask	Event that occurred.
 */

static int
pcihp_event_handler(caddr_t slot_arg, uint_t event_mask)
{
	dev_t ap_dev = (dev_t)slot_arg;
	dev_info_t *self;
	pcihp_t *pcihp_p;
	int pci_dev;
	int rv = HPC_EVENT_CLAIMED;
	struct pcihp_slotinfo *slotinfop;
	struct pcihp_config_ctrl ctrl;
	int circular_count;
	int rval;

	/*
	 * Get the soft state structure.
	 */
	if (pcihp_info(NULL, DDI_INFO_DEVT2DEVINFO, (void *)ap_dev,
	    (void **)&self) != DDI_SUCCESS)
		return (ENXIO);

	pcihp_p = pcihp_get_soft_state(self, PCIHP_DR_SLOT_ENTER, &rval);
	ASSERT(pcihp_p != NULL);

	if (rval == PCIHP_FAILURE) {
		PCIHP_DEBUG((CE_WARN, "pcihp instance is unconfigured"
		    " while slot activity is requested\n"));
		return (-1);
	}

	/* get the PCI device number for the slot */
	pci_dev = PCIHP_AP_MINOR_NUM_TO_PCI_DEVNUM(getminor(ap_dev));

	slotinfop = &pcihp_p->slotinfo[pci_dev];

	/*
	 * All the events that may be handled in interrupt context should be
	 * free of any mutex usage.
	 */
	switch (event_mask) {

	case HPC_EVENT_CLEAR_ENUM:
		/*
		 * Check and clear ENUM# interrupt status. This may be
		 * called by the Hotswap controller driver when it is
		 * operating in a full hotswap system where the
		 * platform may not have control on globally disabling ENUM#.
		 * In such cases, the intent is to clear interrupt and
		 * process the interrupt in non-interrupt context.
		 * This is the first part of the ENUM# event processing.
		 */
		PCIHP_DEBUG((CE_NOTE, "pcihp (%s%d): ENUM# is generated"
		    " on the bus (for slot %s ?)",
		    ddi_driver_name(pcihp_p->dip),
		    ddi_get_instance(pcihp_p->dip), slotinfop->name));

		/* this is the only event coming through in interrupt context */
		rv = pcihp_handle_enum(pcihp_p, pci_dev, PCIHP_CLEAR_ENUM,
		    KM_NOSLEEP);

		(void) pcihp_get_soft_state(self, PCIHP_DR_SLOT_EXIT, &rval);

		return (rv);
	default:
		break;
	}

	mutex_enter(&slotinfop->slot_mutex);

	switch (event_mask) {

	case HPC_EVENT_SLOT_INSERTION:
		/*
		 * A card is inserted in the slot. Just report this
		 * event and return.
		 */
		cmn_err(CE_NOTE, "pcihp (%s%d): card is inserted"
			" in the slot %s (pci dev %x)",
			ddi_driver_name(pcihp_p->dip),
			ddi_get_instance(pcihp_p->dip),
			slotinfop->name, pci_dev);

		/* +++ HOOK for RCM to report this hotplug event? +++ */

		break;

	case HPC_EVENT_SLOT_CONFIGURE:
		/*
		 * Configure the occupant that is just inserted in the slot.
		 * The receptacle may or may not be in the connected state. If
		 * the receptacle is not connected and the auto configuration
		 * is enabled on this slot then connect the slot. If auto
		 * configuration is enabled then configure the card.
		 */
		if ((slotinfop->slot_flags & PCIHP_SLOT_AUTO_CFG_EN) == 0) {
			/*
			 * auto configuration is disabled. Tell someone
			 * like RCM about this hotplug event?
			 */
			cmn_err(CE_NOTE, "pcihp (%s%d): SLOT_CONFIGURE event"
				" occurred for pci dev %x (slot %s),"
				" Slot disabled for auto-configuration.",
				ddi_driver_name(pcihp_p->dip),
				ddi_get_instance(pcihp_p->dip), pci_dev,
				slotinfop->name);

			/* +++ HOOK for RCM to report this hotplug event? +++ */

			break;
		}

		if (slotinfop->ostate == AP_OSTATE_CONFIGURED) {
			cmn_err(CE_WARN, "pcihp (%s%d): SLOT_CONFIGURE event"
				" re-occurred for pci dev %x (slot %s),",
				ddi_driver_name(pcihp_p->dip),
				ddi_get_instance(pcihp_p->dip), pci_dev,
				slotinfop->name);
			mutex_exit(&slotinfop->slot_mutex);

			(void) pcihp_get_soft_state(self, PCIHP_DR_SLOT_EXIT,
			    &rval);

			return (EAGAIN);
		}

		/*
		 * Auto configuration is enabled. First, make sure the
		 * receptacle is in the CONNECTED state.
		 */
		if ((rv = hpc_nexus_connect(slotinfop->slot_hdl,
		    NULL, 0)) == HPC_SUCCESS) {
		    slotinfop->rstate = AP_RSTATE_CONNECTED; /* record rstate */
		}

		/* Clear INS and Turn LED Off and start configuring. */
		if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI) {
			pcihp_hs_csr_op(pcihp_p, pci_dev,
				HPC_EVENT_SLOT_CONFIGURE);
			if (pcihp_cpci_blue_led)
				pcihp_hs_csr_op(pcihp_p, pci_dev,
					HPC_EVENT_SLOT_BLUE_LED_OFF);
		}

		(void) hpc_nexus_control(slotinfop->slot_hdl,
			HPC_CTRL_DEV_CONFIG_START, NULL);

		/*
		 * Call the configurator to configure the card.
		 */
		if (pcicfg_configure(pcihp_p->dip, pci_dev) != PCICFG_SUCCESS) {
			if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI) {
				if (pcihp_cpci_blue_led)
					pcihp_hs_csr_op(pcihp_p, pci_dev,
						HPC_EVENT_SLOT_BLUE_LED_ON);
				pcihp_hs_csr_op(pcihp_p, pci_dev,
					HPC_EVENT_SLOT_UNCONFIGURE);
			}
			/* failed to configure the card */
			cmn_err(CE_WARN, "pcihp (%s%d): failed to configure"
				" the card in the slot %s",
				ddi_driver_name(pcihp_p->dip),
				ddi_get_instance(pcihp_p->dip),
				slotinfop->name);
			/* failed to configure; disconnect the slot */
			if (hpc_nexus_disconnect(slotinfop->slot_hdl,
			    NULL, 0) == HPC_SUCCESS) {
			    slotinfop->rstate = AP_RSTATE_DISCONNECTED;
			}

			/* tell HPC driver occupant configure Error */
			(void) hpc_nexus_control(slotinfop->slot_hdl,
				HPC_CTRL_DEV_CONFIG_FAILURE, NULL);
		} else {
			/* record the occupant state as CONFIGURED */
			slotinfop->ostate = AP_OSTATE_CONFIGURED;
			slotinfop->condition = AP_COND_OK;

			/* now, online all the devices in the AP */
			ctrl.flags = PCIHP_CFG_CONTINUE;
			ctrl.rv = NDI_SUCCESS;
			ctrl.dip = NULL;
			ctrl.pci_dev = pci_dev;
			ctrl.op = PCIHP_ONLINE;
				(void) pcihp_get_board_type(slotinfop);

			ndi_devi_enter(pcihp_p->dip, &circular_count);
			ddi_walk_devs(ddi_get_child(pcihp_p->dip),
				pcihp_configure, (void *)&ctrl);
			ndi_devi_exit(pcihp_p->dip, circular_count);

			if (ctrl.rv != NDI_SUCCESS) {
				/*
				 * one or more of the devices are not
				 * ONLINE'd. How is this to be
				 * reported?
				 */
				cmn_err(CE_WARN,
					"pcihp (%s%d): failed to attach one or"
					" more drivers for the card in"
					" the slot %s",
					ddi_driver_name(pcihp_p->dip),
					ddi_get_instance(pcihp_p->dip),
					slotinfop->name);
			}

			/* tell HPC driver that the occupant is configured */
			(void) hpc_nexus_control(slotinfop->slot_hdl,
				HPC_CTRL_DEV_CONFIGURED, NULL);

			cmn_err(CE_NOTE, "pcihp (%s%d): card is CONFIGURED"
				" in the slot %s (pci dev %x)",
				ddi_driver_name(pcihp_p->dip),
				ddi_get_instance(pcihp_p->dip),
				slotinfop->name, pci_dev);
		}

		break;

	case HPC_EVENT_SLOT_UNCONFIGURE:
		/*
		 * Unconfigure the occupant in this slot.
		 */
		if ((slotinfop->slot_flags & PCIHP_SLOT_AUTO_CFG_EN) == 0) {
			/*
			 * auto configuration is disabled. Tell someone
			 * like RCM about this hotplug event?
			 */
			cmn_err(CE_NOTE, "pcihp (%s%d): SLOT_UNCONFIGURE event"
				" for pci dev %x (slot %s) ignored,"
				" Slot disabled for auto-configuration.",
				ddi_driver_name(pcihp_p->dip),
				ddi_get_instance(pcihp_p->dip), pci_dev,
				slotinfop->name);

			/* +++ HOOK for RCM to report this hotplug event? +++ */

			break;
		}

		if (slotinfop->ostate == AP_OSTATE_UNCONFIGURED) {
			cmn_err(CE_WARN, "pcihp (%s%d): SLOT_UNCONFIGURE "
				"event re-occurred for pci dev %x (slot %s),",
				ddi_driver_name(pcihp_p->dip),
				ddi_get_instance(pcihp_p->dip), pci_dev,
				slotinfop->name);
			mutex_exit(&slotinfop->slot_mutex);

			(void) pcihp_get_soft_state(self, PCIHP_DR_SLOT_EXIT,
			    &rval);

			return (EAGAIN);
		}
		/*
		 * If the occupant is in the CONFIGURED state then
		 * call the configurator to unconfigure the slot.
		 */
		if (slotinfop->ostate == AP_OSTATE_CONFIGURED) {
			/*
			 * Detach all the drivers for the devices in the
			 * slot. Call pcihp_configure() to offline the
			 * devices.
			 */
			ctrl.flags = 0;
			ctrl.rv = NDI_SUCCESS;
			ctrl.dip = NULL;
			ctrl.pci_dev = pci_dev;
			ctrl.op = PCIHP_OFFLINE;

			(void) devfs_clean(pcihp_p->dip, NULL, DV_CLEAN_FORCE);
			ndi_devi_enter(pcihp_p->dip, &circular_count);
			ddi_walk_devs(ddi_get_child(pcihp_p->dip),
				pcihp_configure, (void *)&ctrl);
			ndi_devi_exit(pcihp_p->dip, circular_count);

			if (ctrl.rv != NDI_SUCCESS) {
				/*
				 * Failed to detach one or more drivers.
				 * Restore the status for the drivers
				 * which are offlined during this step.
				 */
				ctrl.flags = PCIHP_CFG_CONTINUE;
				ctrl.rv = NDI_SUCCESS;
				ctrl.dip = NULL;
				ctrl.pci_dev = pci_dev;
				ctrl.op = PCIHP_ONLINE;

				ndi_devi_enter(pcihp_p->dip, &circular_count);
				ddi_walk_devs(ddi_get_child(pcihp_p->dip),
					pcihp_configure, (void *)&ctrl);
				ndi_devi_exit(pcihp_p->dip, circular_count);
				rv = HPC_ERR_FAILED;
			} else {
				(void) hpc_nexus_control(slotinfop->slot_hdl,
					HPC_CTRL_DEV_UNCONFIG_START, NULL);

				if (pcicfg_unconfigure(pcihp_p->dip,
						pci_dev) == PCICFG_SUCCESS) {

				/* Resources freed. Turn LED on. Clear EXT. */
				if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI) {
					if (pcihp_cpci_blue_led)
						pcihp_hs_csr_op(pcihp_p,
						pci_dev,
						HPC_EVENT_SLOT_BLUE_LED_ON);
					pcihp_hs_csr_op(pcihp_p, pci_dev,
						HPC_EVENT_SLOT_UNCONFIGURE);
					slotinfop->hs_csr_location = 0;
					slotinfop->slot_flags &=
						~PCIHP_SLOT_DEV_NON_HOTPLUG;
				}
					slotinfop->ostate =
						AP_OSTATE_UNCONFIGURED;
					slotinfop->condition = AP_COND_UNKNOWN;
					/*
					 * send the notification of state change
					 * to the HPC driver.
					 */
					(void) hpc_nexus_control(
						slotinfop->slot_hdl,
						HPC_CTRL_DEV_UNCONFIGURED,
						NULL);
					/* disconnect the slot */
					if (hpc_nexus_disconnect(
						slotinfop->slot_hdl,
						NULL, 0) == HPC_SUCCESS) {
							slotinfop->rstate =
							AP_RSTATE_DISCONNECTED;
					}

					cmn_err(CE_NOTE,
					"pcihp (%s%d): card is UNCONFIGURED"
						" in the slot %s (pci dev %x)",
						ddi_driver_name(pcihp_p->dip),
						ddi_get_instance(pcihp_p->dip),
						slotinfop->name, pci_dev);
				} else {
					/* tell HPC driver occupant is Busy */
					(void) hpc_nexus_control(
						slotinfop->slot_hdl,
						HPC_CTRL_DEV_UNCONFIG_FAILURE,
						NULL);

					rv = HPC_ERR_FAILED;
				}
			}
		}

		/* +++ HOOK for RCM to report this hotplug event? +++ */

		break;

	case HPC_EVENT_SLOT_REMOVAL:
		/*
		 * Card is removed from the slot. The card must have been
		 * unconfigured before this event.
		 */
		if (slotinfop->ostate != AP_OSTATE_UNCONFIGURED) {
			panic("pcihp (%s%d): card is removed from"
			    " the slot %s before doing unconfigure!!",
			    ddi_driver_name(pcihp_p->dip),
			    ddi_get_instance(pcihp_p->dip),
			    slotinfop->name);
			/*NOTREACHED*/
		}

		cmn_err(CE_NOTE, "pcihp (%s%d): card is removed"
		    " from the slot %s",
		    ddi_driver_name(pcihp_p->dip),
		    ddi_get_instance(pcihp_p->dip),
		    slotinfop->name);

		/* +++ HOOK for RCM to report this hotplug event? +++ */

		break;

	case HPC_EVENT_SLOT_POWER_ON:
		/*
		 * Slot is connected to the bus. i.e the card is powered
		 * on. Are there any error conditions to be checked?
		 */
		PCIHP_DEBUG((CE_NOTE, "pcihp (%s%d): card is powered"
			" on in the slot %s",
			ddi_driver_name(pcihp_p->dip),
			ddi_get_instance(pcihp_p->dip),
			slotinfop->name));

		slotinfop->rstate = AP_RSTATE_CONNECTED; /* record rstate */

		/* +++ HOOK for RCM to report this hotplug event? +++ */

		break;

	case HPC_EVENT_SLOT_POWER_OFF:
		/*
		 * Slot is disconnected from the bus. i.e the card is powered
		 * off. Are there any error conditions to be checked?
		 */
		PCIHP_DEBUG((CE_NOTE, "pcihp (%s%d): card is powered"
			" off in the slot %s",
			ddi_driver_name(pcihp_p->dip),
			ddi_get_instance(pcihp_p->dip),
			slotinfop->name));

		slotinfop->rstate = AP_RSTATE_DISCONNECTED; /* record rstate */

		/* +++ HOOK for RCM to report this hotplug event? +++ */

		break;

	case HPC_EVENT_SLOT_LATCH_SHUT:
		/*
		 * Latch on the slot is closed.
		 */
		cmn_err(CE_NOTE, "pcihp (%s%d): latch is shut"
			" for the slot %s",
			ddi_driver_name(pcihp_p->dip),
			ddi_get_instance(pcihp_p->dip),
			slotinfop->name);

		/* +++ HOOK for RCM to report this hotplug event? +++ */

		break;

	case HPC_EVENT_SLOT_LATCH_OPEN:
		/*
		 * Latch on the slot is open.
		 */
		cmn_err(CE_NOTE, "pcihp (%s%d): latch is open"
			" for the slot %s",
			ddi_driver_name(pcihp_p->dip),
			ddi_get_instance(pcihp_p->dip),
			slotinfop->name);

		/* +++ HOOK for RCM to report this hotplug event? +++ */

		break;

	case HPC_EVENT_PROCESS_ENUM:
		/*
		 * HSC knows the device number of the slot where the
		 * ENUM# was triggered.
		 * Now finish the necessary actions to be taken on that
		 * slot. Please note that the interrupt is already cleared.
		 * This is the second(last) part of the ENUM# event processing.
		 */
		PCIHP_DEBUG((CE_NOTE, "pcihp (%s%d): processing ENUM#"
			" for slot %s",
			ddi_driver_name(pcihp_p->dip),
			ddi_get_instance(pcihp_p->dip),
			slotinfop->name));

		mutex_exit(&slotinfop->slot_mutex);
		rv = pcihp_enum_slot(pcihp_p, slotinfop, pci_dev,
			PCIHP_HANDLE_ENUM, KM_SLEEP);
		mutex_enter(&slotinfop->slot_mutex);

		/* +++ HOOK for RCM to report this hotplug event? +++ */

		break;

	case HPC_EVENT_BUS_ENUM:
		/*
		 * Same as HPC_EVENT_SLOT_ENUM as defined the PSARC doc.
		 * This term is used for better clarity of its usage.
		 *
		 * ENUM signal occurred on the bus. It may be from this
		 * slot or any other hotplug slot on the bus.
		 *
		 * It is NOT recommended that the hotswap controller uses
		 * event without queuing as NDI and other DDI calls may not
		 * necessarily be invokable in interrupt context.
		 * Hence the hotswap controller driver should use the
		 * CLEAR_ENUM event which returns the slot device number
		 * and then call HPC_EVENT_PROCESS_ENUM event with queuing.
		 *
		 * This can be used when the hotswap controller is
		 * implementing a polled event mechanism to do the
		 * necessary actions in a single call.
		 */
		PCIHP_DEBUG((CE_NOTE, "pcihp (%s%d): ENUM# is generated"
		    " on the bus (for slot %s ?)",
		    ddi_driver_name(pcihp_p->dip),
		    ddi_get_instance(pcihp_p->dip),
		    slotinfop->name));

		mutex_exit(&slotinfop->slot_mutex);
		rv = pcihp_handle_enum(pcihp_p, pci_dev, PCIHP_HANDLE_ENUM,
			KM_SLEEP);
		mutex_enter(&slotinfop->slot_mutex);

		/* +++ HOOK for RCM to report this hotplug event? +++ */

		break;

	case HPC_EVENT_SLOT_BLUE_LED_ON:

		/*
		 * Request to turn Hot Swap Blue LED on.
		 */
		PCIHP_DEBUG((CE_NOTE, "pcihp (%s%d): Request To Turn On Blue "
		    "LED on the bus (for slot %s ?)",
		    ddi_driver_name(pcihp_p->dip),
		    ddi_get_instance(pcihp_p->dip),
		    slotinfop->name));

		pcihp_hs_csr_op(pcihp_p, pci_dev, HPC_EVENT_SLOT_BLUE_LED_ON);
		break;

	case HPC_EVENT_DISABLE_ENUM:
		/*
		 * Disable ENUM# which disables auto configuration on this slot
		 */
		if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI) {
			pcihp_hs_csr_op(pcihp_p, pci_dev,
				HPC_EVENT_DISABLE_ENUM);
			slotinfop->slot_flags &= ~PCIHP_SLOT_AUTO_CFG_EN;
		}
		break;

	case HPC_EVENT_ENABLE_ENUM:
		/*
		 * Enable ENUM# which enables auto configuration on this slot.
		 */
		if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI) {
			pcihp_hs_csr_op(pcihp_p, pci_dev,
				HPC_EVENT_ENABLE_ENUM);
			slotinfop->slot_flags |= PCIHP_SLOT_AUTO_CFG_EN;
		}
		break;

	case HPC_EVENT_SLOT_BLUE_LED_OFF:

		/*
		 * Request to turn Hot Swap Blue LED off.
		 */
		PCIHP_DEBUG((CE_NOTE, "pcihp (%s%d): Request To Turn Off Blue "
		    "LED on the bus (for slot %s ?)",
		    ddi_driver_name(pcihp_p->dip),
		    ddi_get_instance(pcihp_p->dip),
		    slotinfop->name));

		pcihp_hs_csr_op(pcihp_p, pci_dev, HPC_EVENT_SLOT_BLUE_LED_OFF);

		break;

	case HPC_EVENT_SLOT_NOT_HEALTHY:
		/*
		 * HEALTHY# signal on this slot is not OK.
		 */
		PCIHP_DEBUG((CE_NOTE, "pcihp (%s%d): HEALTHY# signal is not OK"
			" for this slot %s",
			ddi_driver_name(pcihp_p->dip),
			ddi_get_instance(pcihp_p->dip),
			slotinfop->name));

		/* record the state in slot_flags field */
		slotinfop->slot_flags |= PCIHP_SLOT_NOT_HEALTHY;
		slotinfop->condition = AP_COND_FAILED;

		/* +++ HOOK for RCM to report this hotplug event? +++ */

		break;

	case HPC_EVENT_SLOT_HEALTHY_OK:
		/*
		 * HEALTHY# signal on this slot is OK now.
		 */
		PCIHP_DEBUG((CE_NOTE, "pcihp (%s%d): HEALTHY# signal is OK now"
			" for this slot %s",
			ddi_driver_name(pcihp_p->dip),
			ddi_get_instance(pcihp_p->dip),
			slotinfop->name));

		/* update the state in slot_flags field */
		slotinfop->slot_flags &= ~PCIHP_SLOT_NOT_HEALTHY;
		slotinfop->condition = AP_COND_OK;

		/* +++ HOOK for RCM to report this hotplug event? +++ */

		break;

	default:
		cmn_err(CE_NOTE, "pcihp (%s%d): unknown event %x"
			" for this slot %s",
			ddi_driver_name(pcihp_p->dip),
			ddi_get_instance(pcihp_p->dip), event_mask,
			slotinfop->name);

		/* +++ HOOK for RCM to report this hotplug event? +++ */

		break;
	}

	mutex_exit(&slotinfop->slot_mutex);

	(void) pcihp_get_soft_state(self, PCIHP_DR_SLOT_EXIT, &rval);

	return (rv);
}

/*
 * This function is called to online or offline the devices for an
 * attachment point. If the PCI device number of the node matches
 * with the device number of the specified hot plug slot then
 * the operation is performed.
 */
static int
pcihp_configure(dev_info_t *dip, void *hdl)
{
	int pci_dev;
	struct pcihp_config_ctrl *ctrl = (struct pcihp_config_ctrl *)hdl;
	int rv;
	pci_regspec_t *pci_rp;
	int length;

	/*
	 * Get the PCI device number information from the devinfo
	 * node. Since the node may not have the address field
	 * setup (this is done in the DDI_INITCHILD of the parent)
	 * we look up the 'reg' property to decode that information.
	 */
	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip,
		DDI_PROP_DONTPASS, "reg", (int **)&pci_rp,
		(uint_t *)&length) != DDI_PROP_SUCCESS) {
		ctrl->rv = DDI_FAILURE;
		ctrl->dip = dip;
		return (DDI_WALK_TERMINATE);
	}

	/* get the pci device id information */
	pci_dev = PCI_REG_DEV_G(pci_rp->pci_phys_hi);

	/*
	 * free the memory allocated by ddi_prop_lookup_int_array
	 */
	ddi_prop_free(pci_rp);

	/*
	 * Match the node for the device number of the slot.
	 */
	if (pci_dev == ctrl->pci_dev) {	/* node is a match */
		if (ctrl->op == PCIHP_ONLINE) {
			/* it is CONFIGURE operation */
			rv = ndi_devi_online(dip, NDI_ONLINE_ATTACH|NDI_CONFIG);
		} else {
			/*
			 * it is UNCONFIGURE operation.
			 */
			rv = ndi_devi_offline(dip, NDI_UNCONFIG);
		}
		if (rv != NDI_SUCCESS) {
			/* failed to attach/detach the driver(s) */
			ctrl->rv = rv;
			ctrl->dip = dip;
			/* terminate the search if specified */
			if (!(ctrl->flags & PCIHP_CFG_CONTINUE))
				return (DDI_WALK_TERMINATE);
		}
	}

	/*
	 * continue the walk to the next sibling to look for a match
	 * or to find other nodes if this card is a multi-function card.
	 */
	return (DDI_WALK_PRUNECHILD);
}

/* control structure used to find a device in the devinfo tree */
struct pcihp_find_ctrl {
	uint_t		device;
	uint_t		function;
	dev_info_t	*dip;
};

static dev_info_t *
pcihp_devi_find(dev_info_t *dip, uint_t device, uint_t function)
{
	struct pcihp_find_ctrl ctrl;
	int circular_count;

	ctrl.device = device;
	ctrl.function = function;
	ctrl.dip = NULL;

	ndi_devi_enter(dip, &circular_count);
	ddi_walk_devs(ddi_get_child(dip), pcihp_match_dev, (void *)&ctrl);
	ndi_devi_exit(dip, circular_count);

	return (ctrl.dip);
}

static int
pcihp_match_dev(dev_info_t *dip, void *hdl)
{
	struct pcihp_find_ctrl *ctrl = (struct pcihp_find_ctrl *)hdl;
	pci_regspec_t *pci_rp;
	int length;
	int pci_dev;
	int pci_func;

	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip,
		DDI_PROP_DONTPASS, "reg", (int **)&pci_rp,
		(uint_t *)&length) != DDI_PROP_SUCCESS) {
		ctrl->dip = NULL;
		return (DDI_WALK_TERMINATE);
	}

	/* get the PCI device address info */
	pci_dev = PCI_REG_DEV_G(pci_rp->pci_phys_hi);
	pci_func = PCI_REG_FUNC_G(pci_rp->pci_phys_hi);

	/*
	 * free the memory allocated by ddi_prop_lookup_int_array
	 */
	ddi_prop_free(pci_rp);


	if ((pci_dev == ctrl->device) && (pci_func == ctrl->function)) {
		/* found the match for the specified device address */
		ctrl->dip = dip;
		return (DDI_WALK_TERMINATE);
	}

	/*
	 * continue the walk to the next sibling to look for a match.
	 */
	return (DDI_WALK_PRUNECHILD);
}

#if 0
/*
 * Probe the configuration space of the slot to determine the receptacle
 * state. There may not be any devinfo tree created for this slot.
 */
static void
pcihp_probe_slot_state(dev_info_t *dip, int dev, hpc_slot_state_t *rstatep)
{
	/* XXX FIX IT */
}
#endif

/*
 * This routine is called when a ENUM# assertion is detected for a bus.
 * Since ENUM# may be bussed, the slot that asserted ENUM# may not be known.
 * The HPC Driver passes the handle of a slot that is its best guess.
 * If the best guess slot is the one that asserted ENUM#, the proper handling
 * will be done.  If its not, all possible slots will be locked at until
 * one that is asserting ENUM is found.
 * Also, indicate to the HSC to turn on ENUM# after it is serviced,
 * incase if it was disabled by the HSC due to the nature of asynchronous
 * delivery of interrupt by the framework.
 *
 * opcode has the following meanings.
 * PCIHP_CLEAR_ENUM = just clear interrupt and return the PCI device no. if
 *			success, else return -1.
 * PCIHP_HANDLE_ENUM = clear interrupt and handle interrupt also.
 *
 */
static int
pcihp_handle_enum(pcihp_t *pcihp_p, int favorite_pci_dev, int opcode,
	int kmflag)
{
	struct pcihp_slotinfo *slotinfop;
	int pci_dev, rc, event_serviced = 0;

	/*
	 * Handle ENUM# condition for the "favorite" slot first.
	 */
	slotinfop = &pcihp_p->slotinfo[favorite_pci_dev];
	if (slotinfop) {
		/*
		 * First try the "favorite" pci device.  This is the device
		 * associated with the handle passed by the HPC Driver.
		 */
		rc = pcihp_enum_slot(pcihp_p, slotinfop, favorite_pci_dev,
			opcode, kmflag);
		if (rc != HPC_EVENT_UNCLAIMED) {	/* indicates success */
			event_serviced = 1;
			/* This MUST be a non-DEBUG feature. */
			if (! pcihp_enum_scan_all) {
				return (rc);
			}
		}
	}

	/*
	 * If ENUM# is implemented as a radial signal, then there is no
	 * need to further poll the slots.
	 */
	if (pcihp_p->bus_flags & PCIHP_BUS_ENUM_RADIAL)
		goto enum_service_check;

	/*
	 * If the "favorite" pci device didn't assert ENUM#, then
	 * try the rest.  Once we find and handle a device that asserted
	 * ENUM#, then we will terminate the walk by returning unless
	 * scan-all flag is set.
	 */
	for (pci_dev = 0; pci_dev < PCI_MAX_DEVS; pci_dev++) {
		if (pci_dev != favorite_pci_dev) {
			slotinfop = &pcihp_p->slotinfo[pci_dev];
			if (slotinfop == NULL) {
				continue;
			}
			/* Only CPCI devices support ENUM# generation. */
			if (!(slotinfop->slot_type & HPC_SLOT_TYPE_CPCI))
				continue;
			rc = pcihp_enum_slot(pcihp_p, slotinfop, pci_dev,
				opcode, kmflag);
			if (rc != HPC_EVENT_UNCLAIMED) {
				event_serviced = 1;
				/* This MUST be a non-DEBUG feature. */
				if (! pcihp_enum_scan_all)
					break;
			}
		}
	}

enum_service_check:
	if (event_serviced) {
		return (rc);
	}

	/* No ENUM# event found, Return */
	return (HPC_EVENT_UNCLAIMED);
}

/*
 * This routine attempts to handle a possible ENUM# assertion case for a
 * specified slot.  This only works for adapters that implement Hot Swap
 * Friendly Silicon.  If the slot's HS_CSR is read and it specifies ENUM#
 * has been asserted, either the insertion or removal handlers will be
 * called.
 */
static int
pcihp_enum_slot(pcihp_t *pcihp_p, struct pcihp_slotinfo *slotinfop, int pci_dev,
		int opcode, int kmflag)
{
	ddi_acc_handle_t handle;
	dev_info_t *dip, *new_child = NULL;
	int result, rv = -1;
	uint8_t hs_csr;

	if (pcihp_config_setup(&dip, &handle, &new_child, pci_dev,
				pcihp_p) != DDI_SUCCESS) {
		return (HPC_EVENT_UNCLAIMED);
	}

	/*
	 * Read the device's HS_CSR.
	 */
	result = pcihp_get_hs_csr(slotinfop, handle, (uint8_t *)&hs_csr);
	PCIHP_DEBUG((CE_NOTE, "pcihp (%s%d): hs_csr = %x, flags = %x",
		ddi_driver_name(pcihp_p->dip), ddi_get_instance(pcihp_p->dip),
		hs_csr, slotinfop->slot_flags));
	/*
	 * we teardown our device map here, because in case of an
	 * extraction event, our nodes would be freed and a teardown
	 * will cause problems.
	 */
	pcihp_config_teardown(&handle, &new_child, pci_dev, pcihp_p);

	if (result == PCIHP_SUCCESS) {

		/* If ENUM# is masked, then it is not us. Some other device */
		if ((hs_csr & HS_CSR_EIM) && (opcode == PCIHP_CLEAR_ENUM))
			return (HPC_EVENT_UNCLAIMED);
		/*
		 * This device supports Full Hot Swap and implements
		 * the Hot Swap Control and Status Register.
		 */
		if ((hs_csr & HS_CSR_INS) ||
			(slotinfop->slot_flags & PCIHP_SLOT_ENUM_INS_PENDING)) {
			/* handle insertion ENUM */
			PCIHP_DEBUG((CE_NOTE, "pcihp (%s%d): "
			    "Handle Insertion ENUM (INS) "
			    "on the bus (for slot %s ?)",
			    ddi_driver_name(pcihp_p->dip),
			    ddi_get_instance(pcihp_p->dip),
			    slotinfop->name));

			/*
			 * generate sysevent
			 */

			if (opcode == PCIHP_CLEAR_ENUM)
				pcihp_gen_sysevent(slotinfop->name,
					PCIHP_DR_REQ,
					SE_INCOMING_RES, pcihp_p->dip,
					kmflag);

			rv = pcihp_handle_enum_insertion(pcihp_p, pci_dev,
				opcode, kmflag);

		} else if ((hs_csr & HS_CSR_EXT) || (slotinfop->slot_flags &
					PCIHP_SLOT_ENUM_EXT_PENDING)) {
			/* handle extraction ENUM */
			PCIHP_DEBUG((CE_NOTE, "pcihp (%s%d): "
			    "Handle Extraction ENUM (EXT) "
			    "on the bus (for slot %s ?)",
			    ddi_driver_name(pcihp_p->dip),
			    ddi_get_instance(pcihp_p->dip),
			    slotinfop->name));

			/*
			 * generate sysevent
			 */

			if (opcode == PCIHP_CLEAR_ENUM)
				pcihp_gen_sysevent(slotinfop->name,
					PCIHP_DR_REQ,
					SE_OUTGOING_RES,
					pcihp_p->dip,
					kmflag);

			rv = pcihp_handle_enum_extraction(pcihp_p, pci_dev,
				opcode, kmflag);
		}
		if (opcode == PCIHP_CLEAR_ENUM) {
			if (rv == PCIHP_SUCCESS)
				rv = pci_dev;
			else
				rv = HPC_EVENT_UNCLAIMED;
		}
	}

	return (rv);
}

/*
 * This routine is called when a ENUM# caused by lifting the lever
 * is detected.  If the occupant is configured, it will be unconfigured.
 * If the occupant is already unconfigured or is successfully unconfigured,
 * the blue LED on the adapter is illuminated which means its OK to remove.
 * Please note that the lock must be released before invoking the
 * generic AP unconfigure function.
 */
static int
pcihp_handle_enum_extraction(pcihp_t *pcihp_p, int pci_dev, int opcode,
	int kmflag)
{
	struct pcihp_slotinfo *slotinfop;
	int rv = PCIHP_FAILURE;

	slotinfop = &pcihp_p->slotinfo[pci_dev];

	/*
	 * It was observed that, clearing the EXT bit turned the LED ON.
	 * This is a BIG problem in case if the unconfigure operation
	 * failed because the board was busy.
	 * In order to avoid this confusing situation (LED ON but the board
	 * is not unconfigured), we instead decided not to clear EXT but
	 * disable further ENUM# from this slot. Disabling ENUM# clears
	 * the interrupt.
	 * Finally before returning we clear the interrupt and enable
	 * ENUM# back again from this slot.
	 */
	pcihp_hs_csr_op(pcihp_p, pci_dev, HPC_EVENT_DISABLE_ENUM);
	if (opcode == PCIHP_CLEAR_ENUM) {
		slotinfop->slot_flags |= PCIHP_SLOT_ENUM_EXT_PENDING;
		return (PCIHP_SUCCESS);
	}

	rv = pcihp_unconfigure_ap(pcihp_p, pci_dev);
	if (rv != HPC_SUCCESS && rv != EBUSY) {
		cmn_err(CE_NOTE, "%s%d: PCI device %x Failed on Unconfigure",
		    ddi_driver_name(pcihp_p->dip),
		    ddi_get_instance(pcihp_p->dip), pci_dev);
	}
	if (rv == EBUSY)
		cmn_err(CE_NOTE, "%s%d: PCI device %x Busy",
		    ddi_driver_name(pcihp_p->dip),
		    ddi_get_instance(pcihp_p->dip), pci_dev);
	if (rv) {
		if (pcihp_cpci_blue_led)
			pcihp_hs_csr_op(pcihp_p, pci_dev,
					HPC_EVENT_SLOT_BLUE_LED_OFF);
	}
	/*
	 * we must clear interrupt in case the unconfigure didn't do it
	 * due to a duplicate interrupt. Extraction is success.
	 */
	pcihp_hs_csr_op(pcihp_p, pci_dev, HPC_EVENT_SLOT_UNCONFIGURE);

	if (!rv) {
		/*
		 * Sys Event Notification.
		 */
		pcihp_gen_sysevent(slotinfop->name, PCIHP_DR_AP_STATE_CHANGE,
			SE_HINT_REMOVE, pcihp_p->dip, kmflag);
	}

	/*
	 * Enable interrupts back from this board.
	 * This could potentially be problematic in case if the user is
	 * quick enough to extract the board.
	 * But we must do it just in case if the switch is closed again.
	 */
	pcihp_hs_csr_op(pcihp_p, pci_dev, HPC_EVENT_ENABLE_ENUM);
	slotinfop->slot_flags &= ~PCIHP_SLOT_ENUM_EXT_PENDING;
	return (rv);
}

/*
 * This routine is called when a ENUM# caused by when an adapter insertion
 * is detected.  If the occupant is successfully configured (i.e. PCI resources
 * successfully assigned, the blue LED is left off, otherwise if configuration
 * is not successful, the blue LED is illuminated.
 * Please note that the lock must be released before invoking the
 * generic AP configure function.
 */
static int
pcihp_handle_enum_insertion(pcihp_t *pcihp_p, int pci_dev, int opcode,
	int kmflag)
{
	struct pcihp_slotinfo *slotinfop;
	int rv = PCIHP_FAILURE;
	minor_t ap_minor;
	major_t ap_major;

	slotinfop = &pcihp_p->slotinfo[pci_dev];
	slotinfop->hs_csr_location = 0;
	/* we clear the interrupt here. This is a must here. */
	pcihp_hs_csr_op(pcihp_p, pci_dev, HPC_EVENT_SLOT_CONFIGURE);
	/*
	 * disable further interrupt from this board till it is
	 * configured.
	 */
	pcihp_hs_csr_op(pcihp_p, pci_dev, HPC_EVENT_DISABLE_ENUM);
	if (opcode == PCIHP_CLEAR_ENUM) {
		slotinfop->slot_flags |= PCIHP_SLOT_ENUM_INS_PENDING;
		return (PCIHP_SUCCESS);
	}

	if ((slotinfop->slot_flags & PCIHP_SLOT_AUTO_CFG_EN) ==
					PCIHP_SLOT_AUTO_CFG_EN) {
		rv = pcihp_configure_ap(pcihp_p, pci_dev);
		if (rv != HPC_SUCCESS) {	/* configure failed */
			cmn_err(CE_NOTE, "%s%d: PCI device %x Failed on"
				" Configure", ddi_driver_name(pcihp_p->dip),
				ddi_get_instance(pcihp_p->dip), pci_dev);
			if (pcihp_cpci_blue_led)
				pcihp_hs_csr_op(pcihp_p, pci_dev,
						HPC_EVENT_SLOT_BLUE_LED_ON);
		}

		/* pcihp_hs_csr_op(pcihp_p, pci_dev, HPC_EVENT_CLEAR_ENUM); */
		pcihp_hs_csr_op(pcihp_p, pci_dev, HPC_EVENT_ENABLE_ENUM);

		if (!rv) {
			ap_major = ddi_driver_major(pcihp_p->dip);
			ap_minor = PCIHP_AP_MINOR_NUM(
			    ddi_get_instance(pcihp_p->dip), pci_dev);
			pcihp_create_occupant_props(pcihp_p->dip,
			    makedevice(ap_major, ap_minor), pci_dev);

			/*
			 * Sys Event Notification.
			 */
			pcihp_gen_sysevent(slotinfop->name,
				PCIHP_DR_AP_STATE_CHANGE,
				SE_HINT_INSERT, pcihp_p->dip, kmflag);
		}

	} else
		rv = PCIHP_SUCCESS;
	slotinfop->slot_flags &= ~PCIHP_SLOT_ENUM_INS_PENDING;
	return (rv);
}

/*
 * Read the Hot Swap Control and Status Register (HS_CSR) and
 * place the result in the location pointed to be hs_csr.
 */
static int
pcihp_get_hs_csr(struct pcihp_slotinfo *slotinfop,
    ddi_acc_handle_t config_handle, uint8_t *hs_csr)
{
	if (slotinfop->hs_csr_location == -1)
		return (PCIHP_FAILURE);

	if (slotinfop->hs_csr_location == 0) {
		slotinfop->hs_csr_location =
			pcihp_get_hs_csr_location(config_handle);

		if (slotinfop->hs_csr_location == -1)
			return (PCIHP_FAILURE);
	}
	*hs_csr = pci_config_get8(config_handle, slotinfop->hs_csr_location);
	return (PCIHP_SUCCESS);
}

/*
 * Write the Hot Swap Control and Status Register (HS_CSR) with
 * the value being pointed at by hs_csr.
 */
static void
pcihp_set_hs_csr(struct pcihp_slotinfo *slotinfop,
    ddi_acc_handle_t config_handle, uint8_t *hs_csr)
{
	if (slotinfop->hs_csr_location == -1)
		return;
	if (slotinfop->hs_csr_location == 0) {
		slotinfop->hs_csr_location =
			pcihp_get_hs_csr_location(config_handle);
		if (slotinfop->hs_csr_location == -1)
			return;
	}
	pci_config_put8(config_handle, slotinfop->hs_csr_location, *hs_csr);
	PCIHP_DEBUG((CE_NOTE, "hs_csr wrote %x, read %x", *hs_csr,
		pci_config_get8(config_handle, slotinfop->hs_csr_location)));
}

static int
pcihp_get_hs_csr_location(ddi_acc_handle_t config_handle)
{
	uint8_t	cap_id;
	uint_t	cap_id_loc;
	uint16_t	status;
	int location = -1;
#define	PCI_STAT_ECP_SUPP	0x10

	/*
	 * Need to check the Status register for ECP support first.
	 * Also please note that for type 1 devices, the
	 * offset could change. Should support type 1 next.
	 */
	status = pci_config_get16(config_handle, PCI_CONF_STAT);
	if (!(status & PCI_STAT_ECP_SUPP)) {
		PCIHP_DEBUG((CE_NOTE, "No Ext Capabilities for device\n"));
		return (-1);
	}
	cap_id_loc = pci_config_get8(config_handle, PCI_CONF_EXTCAP);

	/*
	 * Walk the list of capabilities, but don't walk past the end
	 * of the Configuration Space Header.
	 */
	while ((cap_id_loc) && (cap_id_loc < PCI_CONF_HDR_SIZE)) {

		cap_id = pci_config_get8(config_handle, cap_id_loc);

		if (cap_id == CPCI_HOTSWAP_CAPID) {
			location = cap_id_loc + PCI_ECP_HS_CSR;
			break;
		}
		cap_id_loc = pci_config_get8(config_handle,
		    cap_id_loc + 1);
	}
	return (location);
}

static int
pcihp_add_dummy_reg_property(dev_info_t *dip,
    uint_t bus, uint_t device, uint_t func)
{
	pci_regspec_t dummy_reg;

	bzero(&dummy_reg, sizeof (dummy_reg));

	dummy_reg.pci_phys_hi = PCIHP_MAKE_REG_HIGH(bus, device, func, 0);

	return (ndi_prop_update_int_array(DDI_DEV_T_NONE, dip,
	    "reg", (int *)&dummy_reg, sizeof (pci_regspec_t)/sizeof (int)));
}

static void
pcihp_hs_csr_op(pcihp_t *pcihp_p, int pci_dev, int event)
{
	struct pcihp_slotinfo *slotinfop;
	ddi_acc_handle_t config_handle;
	dev_info_t *dip, *new_child = NULL;
	uint8_t hs_csr;
	int result;

	slotinfop = &pcihp_p->slotinfo[pci_dev];

	if (pcihp_config_setup(&dip, &config_handle, &new_child, pci_dev,
				pcihp_p) != DDI_SUCCESS) {
		return;
	}

	result = pcihp_get_hs_csr(slotinfop, config_handle, (uint8_t *)&hs_csr);
	if ((result != PCIHP_SUCCESS) || (event == -1)) {
		pcihp_config_teardown(&config_handle, &new_child, pci_dev,
							pcihp_p);
		return;
	}

	hs_csr &= 0xf;
	switch (event) {
		case HPC_EVENT_SLOT_BLUE_LED_ON:
			hs_csr |= HS_CSR_LOO;
			break;
		case HPC_EVENT_SLOT_BLUE_LED_OFF:
			hs_csr &= ~HS_CSR_LOO;
			break;
		case HPC_EVENT_SLOT_CONFIGURE:
			hs_csr |= HS_CSR_INS;	/* clear INS */
			break;
		case HPC_EVENT_CLEAR_ENUM:
			hs_csr |= (HS_CSR_INS | HS_CSR_EXT);
			break;
		case HPC_EVENT_SLOT_UNCONFIGURE:
			hs_csr |= HS_CSR_EXT;	/* clear EXT */
			break;
		case HPC_EVENT_ENABLE_ENUM:
			hs_csr &= ~HS_CSR_EIM;
			break;
		case HPC_EVENT_DISABLE_ENUM:
			hs_csr |= HS_CSR_EIM;
			break;
		case HPC_EVENT_SLOT_NOT_HEALTHY:
		case HPC_EVENT_SLOT_HEALTHY_OK:
		default:
			break;
	}
	pcihp_set_hs_csr(slotinfop, config_handle, (uint8_t *)&hs_csr);
	pcihp_config_teardown(&config_handle, &new_child, pci_dev, pcihp_p);
}

static int
pcihp_config_setup(dev_info_t **dip, ddi_acc_handle_t *handle,
			dev_info_t **new_child, int pci_dev, pcihp_t *pcihp_p)
{
	dev_info_t *pdip = pcihp_p->dip;
	int bus, len, rc = DDI_SUCCESS;
	struct pcihp_slotinfo *slotinfop;
	hpc_slot_state_t rstate;
	ddi_acc_hdl_t *hp;
	struct bus_range {
		uint32_t lo;
		uint32_t hi;
	} pci_bus_range;
	int flags = 0;

	slotinfop = &pcihp_p->slotinfo[pci_dev];

	/*
	 * If declared failed, don't allow Config operations.
	 * Otherwise, if good or failing, it is assumed Ok
	 * to get config data.
	 */
	if (slotinfop->condition == AP_COND_FAILED) {
		return (PCIHP_FAILURE);
	}
	/*
	 * check to see if there is a hardware present first.
	 * If no hardware present, no need to probe this slot.
	 * We can do this first probably as a first step towards
	 * safeguarding from accidental removal (we don't support it!).
	 */
	if (hpc_nexus_control(slotinfop->slot_hdl,
			HPC_CTRL_GET_SLOT_STATE, (caddr_t)&rstate) != 0) {
		return (DDI_FAILURE);
	}

	if (rstate != HPC_SLOT_CONNECTED) {
		/* error. slot must be connected */
		return (DDI_FAILURE);
	}
	*new_child = NULL;

	/*
	 * If there is no dip then we need to see if an
	 * adapter has just been hot plugged.
	 */
	len = sizeof (struct bus_range);
	if (ddi_getlongprop_buf(DDI_DEV_T_ANY, pdip,
	    0, "bus-range",
	    (caddr_t)&pci_bus_range, &len) != DDI_SUCCESS) {

		return (PCIHP_FAILURE);
	}

	/* primary bus number of this bus node */
	bus = pci_bus_range.lo;

	if (ndi_devi_alloc(pdip, DEVI_PSEUDO_NEXNAME,
	    (pnode_t)DEVI_SID_NODEID, dip) != NDI_SUCCESS) {

		PCIHP_DEBUG((CE_NOTE, "Failed to alloc probe node\n"));
		return (PCIHP_FAILURE);
	}

	if (pcihp_add_dummy_reg_property(*dip, bus,
	    pci_dev, 0) != DDI_SUCCESS) {

		(void) ndi_devi_free(*dip);
		return (PCIHP_FAILURE);
	}

	/*
	 * Probe for a device. Possibly a non (c)PCI board could be sitting
	 * here which would never respond to PCI config cycles - in which
	 * case we return. Eventually a configure operation would fail.
	 */
	if (pci_config_setup(*dip, handle) != DDI_SUCCESS) {
		cmn_err(CE_WARN, "Cannot set config space map for"
		    " pci device number %d", pci_dev);
		(void) ndi_devi_free(*dip);
		return (PCIHP_FAILURE);
	}

	if (pcihp_indirect_map(*dip) == DDI_SUCCESS)
		flags |= PCICFG_CONF_INDIRECT_MAP;

	/*
	 * See if there is any PCI HW at this location
	 * by reading the Vendor ID.  If it returns with 0xffff
	 * then there is no hardware at this location.
	 */
	if (flags & PCICFG_CONF_INDIRECT_MAP) {

		if (pci_config_get16(*handle, 0) == 0xffff) {
			pci_config_teardown(handle);
			(void) ndi_devi_free(*dip);
			return (PCIHP_FAILURE);
		}
	} else {
		/* Check if mapping is OK */
		hp = impl_acc_hdl_get(*handle);

		if (ddi_peek16(*dip, (int16_t *)(hp->ah_addr),
			(int16_t *)0) != DDI_SUCCESS) {
#ifdef DEBUG
			cmn_err(CE_WARN, "Cannot Map PCI config space for "
			    "device number %d", pci_dev);
#endif
			pci_config_teardown(handle);
			(void) ndi_devi_free(*dip);
			return (PCIHP_FAILURE);
		}
	}

	*new_child = *dip;
	return (rc);

}

static void
pcihp_config_teardown(ddi_acc_handle_t *handle,
			dev_info_t **new_child, int pci_dev, pcihp_t *pcihp_p)
{
	struct pcihp_slotinfo *slotinfop = &pcihp_p->slotinfo[pci_dev];

	pci_config_teardown(handle);
	if (*new_child) {
		(void) ndi_devi_free(*new_child);
		/*
		 * If occupant not configured, reset HS_CSR location
		 * so that we reprobe. This covers cases where
		 * the receptacle had a status change without a
		 * notification to the framework.
		 */
		if (slotinfop->ostate != AP_OSTATE_CONFIGURED)
			slotinfop->hs_csr_location = 0;
	}
}

static int
pcihp_get_board_type(struct pcihp_slotinfo *slotinfop)
{
	hpc_board_type_t board_type;

	/*
	 * Get board type data structure, hpc_board_type_t.
	 */
	if (hpc_nexus_control(slotinfop->slot_hdl, HPC_CTRL_GET_BOARD_TYPE,
				(caddr_t)&board_type) != 0) {

		cmn_err(CE_WARN, "Cannot Get Board Type..");
		return (-1);
	}

	/*
	 * We expect the Hotswap Controller to tell us if the board is
	 * a hotswap board or not, as it probably cannot differentiate
	 * between a basic hotswap board, a non hotswap board and a
	 * hotswap nonfriendly board.
	 * So here is the logic to differentiate between the various
	 * types of cPCI boards.
	 * In case if the HSC returns board type as unknown, we assign
	 * the default board type as defined by a configurable variable
	 * for a BHS, nonfriendly FHS and non HS board.
	 */
	if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI) {
		if (slotinfop->hs_csr_location > 0)
			board_type = HPC_BOARD_CPCI_FULL_HS;
		else {
			if (board_type == HPC_BOARD_CPCI_HS) {
				if (slotinfop->hs_csr_location
					== -1)
					board_type =
					HPC_BOARD_CPCI_BASIC_HS;
			}
			if (board_type == HPC_BOARD_UNKNOWN) {
				if (slotinfop->hs_csr_location
					== -1) {
					board_type =
					pcihp_cpci_board_type;
				} else if
				(slotinfop->hs_csr_location
					!= 0) {
					board_type =
					HPC_BOARD_CPCI_FULL_HS;
				}
			}
		}
		/*
		 * If board type is a non hotswap board, then we must
		 * deny a unconfigure operation. So set this flag.
		 * Strictly speaking, there is no reason not to disallow
		 * a unconfigure operation on nonhotswap boards. But this
		 * is the only way we can prevent a user from accidentally
		 * removing the board and damaging it.
		 */
		if (board_type == HPC_BOARD_CPCI_NON_HS)
			slotinfop->slot_flags |=
				PCIHP_SLOT_DEV_NON_HOTPLUG;
		else
			slotinfop->slot_flags &=
				~PCIHP_SLOT_DEV_NON_HOTPLUG;
	}
	return (board_type);
}


/*
 * Generate the System Event with a possible hint.
 */
static void
pcihp_gen_sysevent(char *slot_name, int event_sub_class, int hint,
				dev_info_t *self, int kmflag)
{

	int err;
	char *ev_subclass = NULL;
	sysevent_id_t eid;
	nvlist_t *ev_attr_list = NULL;
	char attach_pnt[MAXPATHLEN];

	/*
	 * Minor device name (AP) will be bus path
	 * concatenated with slot name
	 */

	(void) strcpy(attach_pnt, PCIHP_DEVICES_STR);
	(void) ddi_pathname(self, attach_pnt + strlen(PCIHP_DEVICES_STR));
	(void) strcat(attach_pnt, ":");
	(void) strcat(attach_pnt, slot_name);
	err = nvlist_alloc(&ev_attr_list, NV_UNIQUE_NAME_TYPE, kmflag);
	if (err != 0) {
		cmn_err(CE_WARN,
		    "%s%d: Failed to allocate memory "
		    "for event attributes%s", ddi_driver_name(self),
		    ddi_get_instance(self), ESC_DR_AP_STATE_CHANGE);
		return;
	}

	switch (event_sub_class) {

	/* event sub class: ESC_DR_AP_STATE_CHANGE */
	case PCIHP_DR_AP_STATE_CHANGE:

		ev_subclass = ESC_DR_AP_STATE_CHANGE;

		switch (hint) {

		case SE_NO_HINT:	/* fall through */
		case SE_HINT_INSERT:	/* fall through */
		case SE_HINT_REMOVE:


			err = nvlist_add_string(ev_attr_list, DR_HINT,
			    SE_HINT2STR(hint));

			if (err != 0) {
				cmn_err(CE_WARN, "%s%d: Failed to add attr [%s]"
					" for %s event", ddi_driver_name(self),
					ddi_get_instance(self),
					DR_HINT, ESC_DR_AP_STATE_CHANGE);
				nvlist_free(ev_attr_list);
				return;
			}
			break;

		default:
			cmn_err(CE_WARN, "%s%d: Unknown hint on sysevent",
				ddi_driver_name(self), ddi_get_instance(self));
			nvlist_free(ev_attr_list);
			return;
		}

		break;

	/* event sub class: ESC_DR_REQ */
	case PCIHP_DR_REQ:

		ev_subclass = ESC_DR_REQ;

		switch (hint) {

		case SE_INVESTIGATE_RES:	/* fall through */
		case SE_INCOMING_RES:	/* fall through */
		case SE_OUTGOING_RES:	/* fall through */

			err = nvlist_add_string(ev_attr_list, DR_REQ_TYPE,
				SE_REQ2STR(hint));

			if (err != 0) {
				cmn_err(CE_WARN,
					"%s%d: Failed to add attr [%s] "
					"for %s event", ddi_driver_name(self),
					ddi_get_instance(self),
					DR_REQ_TYPE, ESC_DR_REQ);
				nvlist_free(ev_attr_list);
				return;
			}
			break;

		default:
			cmn_err(CE_WARN,
				"%s%d:  Unknown hint on sysevent",
				ddi_driver_name(self), ddi_get_instance(self));
			nvlist_free(ev_attr_list);
			return;
		}

		break;

	default:
		cmn_err(CE_WARN,
			"%s%d:  Unknown Event subclass", ddi_driver_name(self),
			ddi_get_instance(self));
		nvlist_free(ev_attr_list);
		return;
	}

	/*
	 * Add attachment point as attribute (common attribute)
	 */

	err = nvlist_add_string(ev_attr_list, DR_AP_ID, attach_pnt);

	if (err != 0) {
		cmn_err(CE_WARN,
			"%s%d: Failed to add attr [%s] for %s event",
			ddi_driver_name(self), ddi_get_instance(self),
			DR_AP_ID, EC_DR);
		nvlist_free(ev_attr_list);
		return;
	}


	/*
	 * Log this event with sysevent framework.
	 */

	err = ddi_log_sysevent(self, DDI_VENDOR_SUNW, EC_DR,
	    ev_subclass, ev_attr_list, &eid,
	    ((kmflag == KM_SLEEP) ? DDI_SLEEP : DDI_NOSLEEP));
	if (err != 0) {
		cmn_err(CE_WARN, "%s%d: Failed to log %s event",
			ddi_driver_name(self), ddi_get_instance(self), EC_DR);
	}

	nvlist_free(ev_attr_list);
}

int
pcihp_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
    int flags, char *name, caddr_t valuep, int *lengthp)
{
	int pci_dev;

	if (dev == DDI_DEV_T_ANY)
		goto skip;

	if (strcmp(name, "pci-occupant") == 0) {
		pci_dev = PCIHP_AP_MINOR_NUM_TO_PCI_DEVNUM(getminor(dev));
		pcihp_create_occupant_props_nolock(dip, dev, pci_dev);
	}
	/* other cases... */
skip:
	return (ddi_prop_op(dev, dip, prop_op, flags, name, valuep, lengthp));
}

/*
 * this function is called only for SPARC platforms, where we may have
 * a mix n' match of direct vs indirectly mapped configuration space.
 * On x86, this function should always return success since the configuration
 * space is always indirect mapped.
 */
/*ARGSUSED*/
static int
pcihp_indirect_map(dev_info_t *dip)
{
#if defined(__sparc)
	int rc = DDI_FAILURE;

	if (ddi_prop_get_int(DDI_DEV_T_ANY, ddi_get_parent(dip), 0,
			PCICFG_DEV_CONF_MAP_PROP, DDI_FAILURE) != DDI_FAILURE)
		rc = DDI_SUCCESS;
	else
		if (ddi_prop_get_int(DDI_DEV_T_ANY, ddi_get_parent(dip),
				0, PCICFG_BUS_CONF_MAP_PROP,
				DDI_FAILURE) != DDI_FAILURE)
			rc = DDI_SUCCESS;
	return (rc);
#else
	return (DDI_SUCCESS);
#endif
}