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
|
/*++
Copyright (C) Microsoft Corporation, 2009
Module Name:
pnppower.c
Abstract:
This file contains function of pnp and power process of the AHCI miniport.
Notes:
Revision History:
--*/
#pragma warning(push)
#pragma warning(disable:26015) //26015: "Potential overflow using expression 'outParams->DriverStatus.bDriverError'. Buffer access is apparently unbounded by the buffer size.
//Output buffer cannot be checked for size. ATAport provides this validation check as the input buffer size and output buffer size are 2 of the 4 parameters passed in on the SMART IRP. Storport doesn’t do this and the miniport doesn’t get the IRP so it cannot do this for itself. This is just the condition of a legacy IOCTL.
//26015: "Potential overflow using expression 'nRB->NRBStatus' Buffer access is apparently unbounded by the buffer size.
//The same is true for the NVCache IOCTL. Instead of the output buffer, this time it is the NVCache_Request_Block.
#pragma warning(disable:4214) // bit field types other than int
#pragma warning(disable:4201) // nameless struct/union
#include "generic.h"
#include "acpiioct.h"
//_DSM for Link Power is uniquely identified by the following UUID:
// E4DB149B-FCFE-425B-A6D8-92357D78FC7F
static const GUID LINK_POWER_ACPI_DSM_GUID = {
0xE4DB149B,
0xFCFE,
0x425B,
{ 0xA6,0xD8,0x92,0x35,0x7D,0x78,0xFC,0x7F }
};
VOID
LogPageDiscoveryCompletion (
_In_ PAHCI_CHANNEL_EXTENSION ChannelExtension,
_In_ PSTORAGE_REQUEST_BLOCK Srb
);
BOOLEAN
AhciPortInitialize(
_In_ PAHCI_CHANNEL_EXTENSION ChannelExtension
)
{
/*++
This function is used to start an AHCI port
It assumes:
Called By:
HwFindAdapter
Affected Variables/Registers:
ChannelExtension->StateFlags
ChannelExtension->CommandList
ChannelExtension->ReceivedFIS
PX.CLB,PX.CLBU
PX.FB,PX.FBU
CMD.ST
It performs:
(overview)
1. Start with some defensive structure checking and variable initialization
2. Initialize the ChannelConfiguration structure (final steps)
3. Enable the AHCI interface as per AHCI 1.1 section 10.1.2 (final steps)
4. Allocate memory for the CommandList, the Receive FIS buffer, and SRB Extension.
5. Start the channel processing commands.
(details)
1.1 Initialize variables
1.2 Verify the Channel Configuration
2.1 Initialize the ChannelConfiguration structure
2.2 Initialize the Channel's base address and the controller's Interrupt Status register address
3.1 Enable the AHCI interface
AHCI 1.1 Section 10.1.2 - 5.
"For each implemented port, system software shall allocate memory for and program:
• PxCLB and PxCLBU (if CAP.S64A is set to ‘1’)
• PxFB and PxFBU (if CAP.S64A is set to ‘1’)
It is good practice for system software to ‘zero-out’ the memory allocated and referenced by PxCLB and PxFB. After setting PxFB and PxFBU to the physical address of the FIS receive area, system software shall set PxCMD.FRE to ‘1’."
3.2 Enable Interrupts on the Channel
AHCI 1.1 Section 10.1.2 - 7.
"Determine which events should cause an interrupt, and set each implemented port’s PxIE register with the appropriate enables."
Note: Due to the multi-tiered nature of the AHCI HBA’s interrupt architecture, system software must always ensure that the PxIS (clear this first) and IS.IPS (clear this second) registers are cleared to ‘0’ before programming the PxIE and GHC.IE registers. This will prevent any residual bits set in these registers from causing an interrupt to be asserted.
4.1 Allocate memory for the CommandList, the Receive FIS buffer and SRB Extension
Now is the time to allocate memory that will be used for controller and per request structures.
In AHCI, the controller structures are both the command header list and the received FIS buffer.
The per request structure will be received through the SRB and will be used to make a Command Table
The mechanism for requesting all of this memory is AtaPortGetUnCachedExtension.
NOTE! AtaPortGetUnCachedExtension can only be called while processing a HwControl IdeStart.
Also NOTE! In order to perform crashdump/hibernate the UncachedExtensionSize cannot be larger than 30K.
The call to AtaPortGetUnCachedExtension is complicated by alignment restrictions that an AHCI controller has so here are the rules:
- Command List Base Addresses must be 1K aligned, and the Command list is (sizeof (AHCI_COMMAND_HEADER) * cap.NCS), which is some multiple of 32 bytes in length.
- The FIS Base Address must be 256 aligned, and the FIS Receive buffer is sizeof (AHCI_RECEIVED_FIS), 256 bytes in length.
- The Command Table must be 128 aligned, and is sizeof(AHCI_COMMAND_TABLE), 1280 bytes in length thanks to some padding in the AHCI_COMMAND_TABLE structure.
The alignment of the addresses (virtual and physical) returned by the function follow these rules
- the address returned by AtaPortGetUnCachedExtension will have both its virtual and physical addresses page aligned
- the memory received through the SRB will either be physically and virtually 4K aligned or SRBExtensionSize aligned. The first allocation will be on a 4K boundary the address of the second will be SRBExtensionSize larger than the first, the third will be SRBExtensionSize larger than the second, etc.
Since the Command Header must be 1K aligned and the uncached extension starts 4K aligned, this works.
However, the Command Header is variable and must be padded so the Received FIS is on a 256 boundary.
Therefore the number of Command Headers must be 256/32 = 8. Round cap.NCS to the next multiple of 8
4.2 Setup the CommandList
Although the pointer returned from AtaPortGetUnCachedExtension is useful to this driver, it does the controller no good and can't be used in CLB. The VIRTUAL address must be translated into the PHYSICAL address before being written to the CLB register as the controller doesn't have the CPU's virtual address translation tables. AtaPortGetPhysicalAddress Returns the physical address for the given Va. The va has to be an offset into any one of the following buffers.
- SRB's data buffer
- SRB's SrbExtension
- Miniport's uncached extension
4.3 Setup the Receive FIS buffer
Handle the Receive FIS buffer the same as 4.2 Command List
4.4 Setup the Local SRB Extension
5.1 Enable Command Processing
5.2 Initialize the ChannelConfiguration structure
ChannelConfiguration->ChannelNumber and ChannelConfiguration->ChannelResources are kept default values.
If it is found that CI and/or SACT can be changed from a 1 to 0, Number of overlapped requests becomes 1.
Number of overlapped requests is a 1 based number (1=1, 2=2, etc.), CAP.NCS is a 0 based number.
5.3 START COMMAND PROCESSING
Return Values:
The miniport driver returns TRUE if it successfully execute the whole function.
Any errors causes the function to return FALSE and prevents the channel from loading. This ultimately causes a yellow '!' to show up on the channel in device manager.
NOTE: as this routine is invoked from FindAdapter where the adapter might not be fully initialized, do not retrieve registry information.
--*/
PAHCI_ADAPTER_EXTENSION adapterExtension = NULL;
PAHCI_MEMORY_REGISTERS abar = NULL;
//these are throw away variables
ULONG mappedLength = 0;
//1.1 Initialize variables
adapterExtension = ChannelExtension->AdapterExtension;
if (LogExecuteFullDetail(adapterExtension->LogFlags)) {
RecordExecutionHistory(ChannelExtension, 0x00000024);//AhciPortInitialize
}
ChannelExtension->CurrentCommandSlot = 1; //slot 0 is reserved for internal commands,
ChannelExtension->StateFlags.IgnoreHotplugInterrupt = TRUE;
abar = (PAHCI_MEMORY_REGISTERS)adapterExtension->ABAR_Address;
ChannelExtension->Px = &abar->PortList[ChannelExtension->PortNumber];
// NonCachedExtension is for CommandList, Receive FIS, SRB Extension for Local SRB and Sense SRB., READ_LOG/IDENTIFY buffer
// (sizeof(AHCI_COMMAND_HEADER) * paddedNCS) + sizeof(AHCI_RECEIVED_FIS) + 2*sizeof(AHCI_SRB_EXTENSION) + sizeof(AHCI_READ_LOG_EXT_DATA);
//4.2 Setup the CommandList
ChannelExtension->CommandListPhysicalAddress = StorPortGetPhysicalAddress(adapterExtension, NULL, (PVOID)ChannelExtension->CommandList, &mappedLength);
if (ChannelExtension->CommandListPhysicalAddress.QuadPart == 0) {
RecordExecutionHistory(ChannelExtension, 0xff02);//Command List Failed
return FALSE;
}
//3.1.1 PxCLB and PxCLBU (AHCI 1.1 Section 10.1.2 - 5)
if ( (ChannelExtension->CommandListPhysicalAddress.LowPart % 1024) == 0 ) {
// validate the alignment is fine
StorPortWriteRegisterUlong(adapterExtension, &ChannelExtension->Px->CLB.AsUlong, ChannelExtension->CommandListPhysicalAddress.LowPart);
}else{
RecordExecutionHistory(ChannelExtension, 0xff03);//Command List alignment failed
return FALSE;
}
if (adapterExtension->CAP.S64A) { //If the controller supports 64 bits, write the high part too
StorPortWriteRegisterUlong(adapterExtension, &ChannelExtension->Px->CLBU, ChannelExtension->CommandListPhysicalAddress.HighPart);
}
//4.3 Setup the Receive FIS buffer
ChannelExtension->ReceivedFisPhysicalAddress = StorPortGetPhysicalAddress(adapterExtension, NULL, (PVOID)ChannelExtension->ReceivedFIS, &mappedLength);
if (ChannelExtension->ReceivedFisPhysicalAddress.QuadPart == 0) {
RecordExecutionHistory(ChannelExtension, 0xff04);//Receive FIS failed
return FALSE;
}
//3.1.2 PxFB and PxFBU (AHCI 1.1 Section 10.1.2 - 5)
if ((ChannelExtension->ReceivedFisPhysicalAddress.LowPart % 256) == 0) {
// validate the alignment is fine
StorPortWriteRegisterUlong(adapterExtension, &ChannelExtension->Px->FB.AsUlong, ChannelExtension->ReceivedFisPhysicalAddress.LowPart);
} else {
RecordExecutionHistory(ChannelExtension, 0xff05);//Receive FIS alignment failed
return FALSE;
}
if (adapterExtension->CAP.S64A) { //If the controller supports 64 bits, write the high part too
StorPortWriteRegisterUlong(adapterExtension, &ChannelExtension->Px->FBU, ChannelExtension->ReceivedFisPhysicalAddress.HighPart);
}
//4.4 Setup the Local SRB Extension
ChannelExtension->Local.SrbExtensionPhysicalAddress = StorPortGetPhysicalAddress(adapterExtension, NULL, (PVOID)ChannelExtension->Local.SrbExtension, &mappedLength);
ChannelExtension->Sense.SrbExtensionPhysicalAddress = StorPortGetPhysicalAddress(adapterExtension, NULL, (PVOID)ChannelExtension->Sense.SrbExtension, &mappedLength);
//4.6 Setup Device Identify Data and Inquiry Data buffers
ChannelExtension->DeviceExtension[0].IdentifyDataPhysicalAddress = StorPortGetPhysicalAddress(adapterExtension, NULL, (PVOID)ChannelExtension->DeviceExtension[0].IdentifyDeviceData, &mappedLength);
ChannelExtension->DeviceExtension[0].InquiryDataPhysicalAddress = StorPortGetPhysicalAddress(adapterExtension, NULL, (PVOID)ChannelExtension->DeviceExtension[0].InquiryData, &mappedLength);
//4.8 Setup STOR_ADDRESS for the device. StorAHCI uses Bus/Target/Lun addressing model, thus uses STOR_ADDRESS_TYPE_BTL8.
// Port - not need to be set by miniport, Storport has this knowledge. miniport can get the value by calling StorPortGetSystemPortNumber().
// Path - StorAHCI reports (highest implemented port number + 1) as bus number, thus "port number" will be "Path" value.
// Target - StorAHCI only supports single device on each port, the "Target" value will be 0.
// Lun - StorAHCI only supports single device on each port, the "Lun" value will be 0.
ChannelExtension->DeviceExtension[0].DeviceAddress.Type = STOR_ADDRESS_TYPE_BTL8;
ChannelExtension->DeviceExtension[0].DeviceAddress.Port = 0;
ChannelExtension->DeviceExtension[0].DeviceAddress.AddressLength = STOR_ADDR_BTL8_ADDRESS_LENGTH;
ChannelExtension->DeviceExtension[0].DeviceAddress.Path = (UCHAR)ChannelExtension->PortNumber;
ChannelExtension->DeviceExtension[0].DeviceAddress.Target = 0;
ChannelExtension->DeviceExtension[0].DeviceAddress.Lun = 0;
//
// Initialize device power state to D0.
//
ChannelExtension->DevicePowerState = StorPowerDeviceD0;
//3.2 Clear Enable Interrupts on the Channel (AHCI 1.1 Section 10.1.2 - 7)
//We will enable interrupt after channel started
PortClearPendingInterrupt(ChannelExtension);
//5.1 Enable Command Processing
ChannelExtension->StateFlags.Initialized = TRUE;
if (adapterExtension->CAP.NCS > 0) { //this leaves one emergency slot free if possible, as CAP.NCS is 0-based.
ChannelExtension->MaxPortQueueDepth = (UCHAR)adapterExtension->CAP.NCS;
} else {
ChannelExtension->MaxPortQueueDepth = 1;
}
if ( IsSingleIoDevice(adapterExtension) || IsDumpMode(adapterExtension) ) {
ChannelExtension->MaxPortQueueDepth = 1;
}
ChannelExtension->LastActiveSlot = 0;
ChannelExtension->DeviceExtension[0].DeviceParameters.MaxDeviceQueueDepth = ChannelExtension->MaxPortQueueDepth;
if (!IsDumpMode(adapterExtension)) {
if (AdapterResetInInit(adapterExtension)) {
P_NotRunning(ChannelExtension, ChannelExtension->Px);
AhciCOMRESET(ChannelExtension, ChannelExtension->Px);
}
}
RecordExecutionHistory(ChannelExtension, 0x10000024);//Exit AhciPortInitialize
return TRUE;
}
BOOLEAN
AhciAdapterPowerUp(
_In_ PAHCI_ADAPTER_EXTENSION AdapterExtension
)
/*++
Indicates that the adapter is being powered up.
Anything that doesn't persist across a power cycle needs to be done here.
It assumes:
PCI Ensures the HBA is in D0 (Offset PMCAP + 4h: PMCS[0,1])
Called by:
AhciAdapterControl
It performs:
Enables the AHCI Interface and global Interrupts
Affected Variables/Registers:
GHC.AE, GHC.IE
Return Values:
TRUE always.
--*/
{
ULONG i;
AHCI_Global_HBA_CONTROL ghc;
PAHCI_MEMORY_REGISTERS abar = (PAHCI_MEMORY_REGISTERS)AdapterExtension->ABAR_Address;
AdapterExtension->StateFlags.PowerDown = 0;
// adapter is on its way power up. there will be no power down request coming in before this function finishes.
// thus there is no need to call AdapterAcquireActiveReference;
ghc.AsUlong = StorPortReadRegisterUlong(AdapterExtension, &abar->GHC.AsUlong);
if (ghc.AE == 0) {
ghc.AsUlong = 0;
ghc.AE = 1;
StorPortWriteRegisterUlong(AdapterExtension, &abar->GHC.AsUlong, ghc.AsUlong);
}
if (ghc.IE == 0) {
ghc.IE = 1;
StorPortWriteRegisterUlong(AdapterExtension, &abar->GHC.AsUlong, ghc.AsUlong);
}
// Power up all ports that don't have a device present.
// There is protection method in AhciPortPowerUp() to only allow it run once.
for (i = 0; i <= AdapterExtension->HighestPort; i++) {
if ((AdapterExtension->PortExtension[i] != NULL) &&
(AdapterExtension->PortExtension[i]->StateFlags.PowerDown == TRUE) &&
(AdapterExtension->PortExtension[i]->DeviceExtension[0].DeviceParameters.AtaDeviceType == DeviceNotExist)) {
AhciPortPowerUp(AdapterExtension->PortExtension[i]);
}
}
return TRUE;
}
BOOLEAN
AhciAdapterPowerDown(
_In_ PAHCI_ADAPTER_EXTENSION AdapterExtension
)
/*++
Indicates that the adapter is being powered down.
It assumes:
PCI powers down the HBA after this function returns: D3 Offset PMCAP + 4h: PMCS[0,1]
Called by:
AhciAdapterControl
It performs:
1. Clear GHC.IE
AHCI 1.1 Section 8.3.3
"Software must disable interrupts (GHC.IE must be cleared to ‘0’) prior to requesting a transition of the HBA to the D3 state. This precaution by software avoids an interrupt storm if an interrupt occurs during the transition to the D3 state."
Affected Variables/Registers:
GHC.IE
Return Values:
TRUE always.
--*/
{
ULONG i;
AHCI_Global_HBA_CONTROL ghc;
PAHCI_MEMORY_REGISTERS abar = (PAHCI_MEMORY_REGISTERS)AdapterExtension->ABAR_Address;
// Power down all ports that don't have a device present.
for (i = 0; i <= AdapterExtension->HighestPort; i++) {
if ((AdapterExtension->PortExtension[i] != NULL) &&
(AdapterExtension->PortExtension[i]->StateFlags.PowerDown == FALSE) &&
(AdapterExtension->PortExtension[i]->DeviceExtension[0].DeviceParameters.AtaDeviceType == DeviceNotExist)) {
AhciPortPowerDown(AdapterExtension->PortExtension[i]);
}
}
ghc.AsUlong = StorPortReadRegisterUlong(AdapterExtension, &abar->GHC.AsUlong);
ghc.IE = 0;
StorPortWriteRegisterUlong(AdapterExtension, &abar->GHC.AsUlong, ghc.AsUlong);
AdapterExtension->StateFlags.PowerDown = 1;
return TRUE;
}
VOID
AhciPortStop(
_In_ PAHCI_CHANNEL_EXTENSION ChannelExtension
)
{
/*++
The miniport driver should stop using the resources allocated for this port.
It assumes:
AhciAdapterStop is called after all the ports are stopped.
StartIo spin lock must be held before this function is invoked.
Note:
Currently this function has only one caller - AdapterStop, which has the
following callers. Every respective code path has StartIo spin lock acquired.
1. AhciAdapterStop.
It has one caller - AhciHwStartIo. Storport has already acquired StartIo spin lock
before calling AhciHwStartIo.
2. AhciHwAdapterControl.
AhciHwAdapterControl has already acquired StartIo spin lock before calling AdapterStop.
Called by:
It performs:
(overview)
1. Stop the channel
2. Undefine all references to anything within the Uncached Extension
Affected Variables/Registers:
CMD.ST, CMD.CR, CMD.FRE, CMD.FR
Return Values:
TRUE if the function executed completely.
FALSE if the channel could not be stopped.
--*/
if (LogExecuteFullDetail(ChannelExtension->AdapterExtension->LogFlags)) {
RecordExecutionHistory(ChannelExtension, 0x00000025);//AhciPortStop
}
//1. Stop the channel
if ( !P_NotRunning(ChannelExtension, ChannelExtension->Px) ) {
// don't need RESET, the port will be tried to start when processing start device request
RecordExecutionHistory(ChannelExtension, 0xff08); //AhciPortStop Failed
}
//2. Disable Interrupt and disconnect with Port resources
StorPortWriteRegisterUlong(ChannelExtension->AdapterExtension, &ChannelExtension->Px->IE.AsUlong, 0); //disabling interrupts
PortClearPendingInterrupt(ChannelExtension);
ChannelExtension->Px = 0;
ChannelExtension->StateFlags.Initialized = FALSE;
ChannelExtension->StateFlags.NoMoreIO = FALSE;
RecordExecutionHistory(ChannelExtension, 0x10000025);//Exit AhciPortStop
return;
}
VOID
AhciPortPowerUp(
_In_ PAHCI_CHANNEL_EXTENSION ChannelExtension
)
{
/*++
Indicates that the channel is being powered up.
Called by:
AhciHwStartIo
It performs:
(overview)
1. Start the Port.
2. If APM is supported, make sure the Link is Active as defined in AHCI1.0 8.3.1.2.
3. If Port Multiplier is supported, powered it up.
(details)
1.1 Reload the CLB,CBU,FLB,FBU stored in the channel extension
1.3 Reinitialize the StateFlags
1.4 Start the channel
Affected Variables/Registers:
PxCMD.ST, PxCMD.ICC
PxCLB,PxCLBU,PxFB,PxFBU
PxIE
Return Values:
none
--*/
AHCI_LPM_POWER_SETTINGS userLpmSettings;
BOOLEAN portPowerDown;
AHCI_INTERRUPT_STATUS pxis = { 0 };
RecordExecutionHistory(ChannelExtension, 0x00000026);//Enter AhciPortPowerUp
++(ChannelExtension->TotalCountPowerUp);
pxis.AsUlong = StorPortReadRegisterUlong(ChannelExtension->AdapterExtension, &ChannelExtension->Px->IS.AsUlong);
// 1.0 Reinitialize the StateFlags. e.g. ChannelExtension->StateFlags.PowerDown = FALSE;
portPowerDown = InterlockedBitTestAndReset((LONG*)&ChannelExtension->StateFlags, 12); //StateFlags.PownDown field is at bit 12
if (portPowerDown == FALSE) {
RecordExecutionHistory(ChannelExtension, 0x00010026);//AhciPortPowerUp: port already powered up.
return;
}
// 1.1 Reload the CLB,CBU,FLB,FBU
StorPortWriteRegisterUlong(ChannelExtension->AdapterExtension, &ChannelExtension->Px->CLB.AsUlong, ChannelExtension->CommandListPhysicalAddress.LowPart);
if (ChannelExtension->AdapterExtension->CAP.S64A) { //If the controller supports 64 bits, write high part
StorPortWriteRegisterUlong(ChannelExtension->AdapterExtension, &ChannelExtension->Px->CLBU, ChannelExtension->CommandListPhysicalAddress.HighPart);
}
StorPortWriteRegisterUlong(ChannelExtension->AdapterExtension, &ChannelExtension->Px->FB.AsUlong, ChannelExtension->ReceivedFisPhysicalAddress.LowPart);
if (ChannelExtension->AdapterExtension->CAP.S64A) { //If the controller supports 64 bits, write high part
StorPortWriteRegisterUlong(ChannelExtension->AdapterExtension, &ChannelExtension->Px->FBU, ChannelExtension->ReceivedFisPhysicalAddress.HighPart);
}
//
// If D3 Cold is enabled and we are being powered up from D3, we need to be
// a bit heavy-handed with powering up the port due to loss of context.
//
if (ChannelExtension->DevicePowerState == StorPowerDeviceD3 && IsPortD3ColdEnabled(ChannelExtension)) {
STOR_LOCK_HANDLE lockhandle = { InterruptLock, 0 };
BOOLEAN powerUpInitializationInProgress = 0;
// 1.4.1 Re-issue init commands (this will also restore preserved settings).
AhciPortIssueInitCommands(ChannelExtension);
// Set PowerUpInitializationInProgress flag, which will be cleared when preserved settings command done.
powerUpInitializationInProgress = InterlockedBitTestAndSet((LONG*)&ChannelExtension->StateFlags, 22); //PowerUpInitializationInProgress field is at bit 22
if (powerUpInitializationInProgress == 1) {
// PowerUpInitializationInProgress flag was not cleared properly, which is not expected to happen, but not a fatal issue.
RecordExecutionHistory(ChannelExtension, 0x10020026); // AhciPortPowerUp: PowerUpInitializationInProgress flag was not cleared properly.
}
// 1.4.2 Restore LPM settings
userLpmSettings.AsUlong = ChannelExtension->LastUserLpmPowerSetting;
AhciLpmSettingsModes(ChannelExtension, userLpmSettings); //ignore the returned value, IO will be restarted anyway.
// 1.5 Start the channel by issuing a reset to restore PHY communication.
AhciInterruptSpinlockAcquire(ChannelExtension->AdapterExtension, ChannelExtension->PortNumber, &lockhandle);
AhciPortReset(ChannelExtension, FALSE);
AhciInterruptSpinlockRelease(ChannelExtension->AdapterExtension, ChannelExtension->PortNumber, &lockhandle);
} else {
//
// If there is change in Current Connect Status(PCS:1), then QDR is needed.
//
if ((pxis.PCS == 1) &&
(ChannelExtension->StartState.ChannelNextStartState == StartFailed)) {
ChannelExtension->StateFlags.NeedQDR = TRUE;
}
// 1.4.1 Restore Preserved Settings
if (NeedToSetTransferMode(ChannelExtension)) {
RestorePreservedSettings(ChannelExtension, FALSE);
}
// 1.5 Start the channel
P_Running_StartAttempt(ChannelExtension, FALSE);
}
RecordExecutionHistory(ChannelExtension, 0x10000026);//Exit AhciPortPowerUp
return;
}
VOID
AhciPortPowerDown(
_In_ PAHCI_CHANNEL_EXTENSION ChannelExtension
)
{
/*++
Indicates that the channel is being powered down.
It assumes:
the device has been powered down through ATA commands
All outstanding IO will be complete before the first power request is sent to the miniport
Called by:
AhciHwStartIo
It performs:
Then each port must be stopped. PxCMD.ST
If APM is supported, the Link need to be put into Slumber as defined in AHCI 1.1 Section 8.3.1.2
If Port Multiplier is support, it would need to be powered down next.
Affected Variables/Registers:
none
Return Values:
TRUE if the function executed completely.
FALSE if the channel could not be stopped for Power Down.
Neither return value is used by ATAport.
--*/
ChannelExtension->StateFlags.PowerDown = TRUE;
++(ChannelExtension->TotalCountPowerDown);
//
// Cancel the StartPortTimer since we're going into a lower power state.
//
StorPortRequestTimer(ChannelExtension->AdapterExtension,
ChannelExtension->StartPortTimer,
P_Running_Callback,
ChannelExtension,
0, 0);
if (ChannelExtension->StateFlags.PoFxEnabled == 1) {
if (IsPortD3ColdEnabled(ChannelExtension)) {
// the link will be inactive, ignore the hot plug noise.
ChannelExtension->StateFlags.IgnoreHotplugInterrupt = TRUE;
}
}
RecordExecutionHistory(ChannelExtension, 0x10000027);//Exit AhciPortPowerDown
}
VOID
ReportLunsComplete(
_In_ PAHCI_CHANNEL_EXTENSION ChannelExtension,
_In_ PSTORAGE_REQUEST_BLOCK Srb
)
{
//Port start completed. Prepare device list.
ULONG lunCount;
ULONG lunLength;
PLUN_LIST lunList;
ULONG i;
PAHCI_SRB_EXTENSION srbExtension;
ULONG srbDataBufferLength = SrbGetDataTransferLength(Srb);
srbExtension = GetSrbExtension(Srb);
// clean up callback fields so that the SRB can be completed.
srbExtension->AtaFunction = 0;
srbExtension->CompletionRoutine = NULL;
// report error back so that Storport may retry the command.
// tolerate failure from IDE_COMMAND_READ_LOG_EXT during device enumeration as it's not part of device enumeration commands.
if ( (srbExtension->TaskFile.Current.bCommandReg != IDE_COMMAND_READ_LOG_EXT) &&
(Srb->SrbStatus != SRB_STATUS_PENDING) &&
(Srb->SrbStatus != SRB_STATUS_SUCCESS) &&
(Srb->SrbStatus != SRB_STATUS_NO_DEVICE) ) {
return;
}
Srb->SrbStatus = SRB_STATUS_SUCCESS;
SrbSetScsiStatus(Srb, SCSISTAT_GOOD);
lunList = (PLUN_LIST)SrbGetDataBuffer(Srb);
if ( ChannelExtension->DeviceExtension->DeviceParameters.AtaDeviceType == DeviceNotExist ) {
lunCount = 0;
} else {
//lunCount = ChannelExtension->DeviceExtension->DeviceParameters.MaximumLun + 1;
lunCount = 1;
}
lunLength = lunCount * 8;
if ( srbDataBufferLength < (sizeof(LUN_LIST) + lunLength) ) {
Srb->SrbStatus = SRB_STATUS_DATA_OVERRUN;
if (srbDataBufferLength >= sizeof(ULONG)) {
//fill in required buffer size
lunList->LunListLength[0] = (UCHAR)(lunLength >> (8*3));
lunList->LunListLength[1] = (UCHAR)(lunLength >> (8*2));
lunList->LunListLength[2] = (UCHAR)(lunLength >> (8*1));
lunList->LunListLength[3] = (UCHAR)(lunLength >> (8*0));
}
} else {
lunList->LunListLength[0] = (UCHAR)(lunLength >> (8*3));
lunList->LunListLength[1] = (UCHAR)(lunLength >> (8*2));
lunList->LunListLength[2] = (UCHAR)(lunLength >> (8*1));
lunList->LunListLength[3] = (UCHAR)(lunLength >> (8*0));
for (i = 0; i < lunCount; i++) {
lunList->Lun[i][0] = 0;
lunList->Lun[i][1] = (UCHAR)i;
lunList->Lun[i][2] = 0;
lunList->Lun[i][3] = 0;
lunList->Lun[i][4] = 0;
lunList->Lun[i][5] = 0;
lunList->Lun[i][6] = 0;
lunList->Lun[i][7] = 0;
}
}
return;
}
__inline
VOID
GetLogInfoRegValueName(
_In_ PAHCI_CHANNEL_EXTENSION ChannelExtension,
_Out_writes_(ValueNameLength) PCHAR ValueName,
_In_ ULONG ValueNameLength
)
/*++
Routine Description:
This function append Port Number to "LogPageInfo" as name of registry value.
This is a work around as StorAHCI doesn't have access to RtlStringCbPrintfA().
Arguments:
ChannelExtension - Pointer to the device extension for channel.
ValueName - Registry name to be written.
ValueNameLength - Registry name length.
Return Value:
None.
--*/
{
NT_ASSERT(ChannelExtension->PortNumber <= 255);
if (ValueNameLength >= 14) {
ULONG portNumber = ChannelExtension->PortNumber;
ULONG remainder = 0;
StorPortCopyMemory(ValueName, "LogPageInfo", 12);
ValueName[13] = '\0';
remainder = portNumber % 16; // use HEX value, base is 16.
ValueName[12] = (CHAR)((remainder < 10) ? (remainder + '0') : (remainder - 10 + 'A'));
portNumber /= 16;
remainder = portNumber % 16;
ValueName[11] = (CHAR)((remainder < 10) ? (remainder + '0') : (remainder - 10 + 'A'));
}
return;
}
_Function_class_(HW_WORKITEM)
VOID
AhciRegistryWriteWorker(
_In_ PAHCI_ADAPTER_EXTENSION AdapterExtension,
_In_ PVOID Context,
_In_ PVOID WorkItem
)
/*++
Routine Description:
This function runs in the context of a work item. It performs writting registry with data
from Log Pages that needs to be cached.
Arguments:
AdapterExtension - Pointer to the device extension for adapter.
Context - The AHCI_CHANNEL_EXTENSION we specified when we queued this work item.
WorkItem - The work item object.
Return Value:
None.
--*/
{
PAHCI_CHANNEL_EXTENSION channelExtension = (PAHCI_CHANNEL_EXTENSION)Context;
ULONG storStatus;
CHAR valueName[16] = { 0 };
AHCI_DEVICE_LOG_PAGE_INFO logPageInfo = { 0 };
NT_ASSERT(Context != NULL);
NT_ASSERT(WorkItem != NULL);
GetLogInfoRegValueName(channelExtension, valueName, sizeof(valueName));
StorPortCopyMemory((PVOID)&logPageInfo.QueryLogPages, &channelExtension->DeviceExtension[0].QueryLogPages, sizeof(ATA_GPL_PAGES_TO_QUERY));
StorPortCopyMemory((PVOID)&logPageInfo.SupportedGPLPages, &channelExtension->DeviceExtension[0].SupportedGPLPages, sizeof(ATA_SUPPORTED_GPL_PAGES));
StorPortCopyMemory((PVOID)&logPageInfo.SupportedCommands, &channelExtension->DeviceExtension[0].SupportedCommands, sizeof(ATA_COMMAND_SUPPORTED));
StorPortCopyMemory((PVOID)&logPageInfo.FirmwareUpdate, &channelExtension->DeviceExtension[0].FirmwareUpdate, sizeof(DOWNLOAD_MICROCODE_CAPABILITIES));
storStatus = StorPortRegistryWriteAdapterKey(AdapterExtension,
(PUCHAR)"StorAHCI",
(PUCHAR)valueName,
MINIPORT_REG_BINARY,
&logPageInfo,
sizeof(AHCI_DEVICE_LOG_PAGE_INFO));
if (storStatus == STOR_STATUS_SUCCESS) {
channelExtension->DeviceExtension[0].UpdateCachedLogPageInfo = FALSE;
} else {
NT_ASSERT(FALSE);
}
//
// Call the callback routine if there is one.
//
if (WorkItem != NULL) {
StorPortFreeWorker(AdapterExtension, WorkItem);
}
return;
}
VOID
PreserveLogPageInformation(
_In_ PAHCI_CHANNEL_EXTENSION ChannelExtension
)
/*++
This routine allocates a work item and schedule it for saving information retrieved from Log Pages.
Parameters:
ChannelExtension - port and device that log pages are retrieved from.
Return Values:
None
--*/
{
ULONG storStatus;
PVOID workItem = NULL;
storStatus = StorPortInitializeWorker(ChannelExtension->AdapterExtension, &workItem);
if (storStatus == STOR_STATUS_SUCCESS) {
storStatus = StorPortQueueWorkItem(ChannelExtension->AdapterExtension, AhciRegistryWriteWorker, workItem, ChannelExtension);
}
NT_ASSERT(storStatus == STOR_STATUS_SUCCESS);
if ((storStatus != STOR_STATUS_SUCCESS) && (workItem != NULL)) {
// Free the work item as it cannot be scheduled to run.
StorPortFreeWorker(ChannelExtension->AdapterExtension, workItem);
}
return;
}
VOID
InitQueryLogPages(
_In_ PAHCI_CHANNEL_EXTENSION ChannelExtension
)
/*++
Initialize Log Pages to Read.
Note that this functions should only be called after Identify Device Data is retrieved.
Parameters:
ChannelExtension - port that log-pages-to-query should be inited.
Return Values:
None
--*/
{
PUSHORT index = &ChannelExtension->DeviceExtension->QueryLogPages.TotalPageCount;
//
// Log Page only applies to ATA device; General Purpose Logging feature should be supported;
// 48bit command should be supported as READ LOG EXT is a 48bit command.
//
if (!IsDeviceGeneralPurposeLoggingSupported(ChannelExtension)) {
return;
}
AhciZeroMemory((PCHAR)&ChannelExtension->DeviceExtension->QueryLogPages, sizeof(ATA_GPL_PAGES_TO_QUERY));
// Read Log Directory
ChannelExtension->DeviceExtension->QueryLogPages.LogPage[*index].Query = TRUE;
ChannelExtension->DeviceExtension->QueryLogPages.LogPage[*index].LogAddress = IDE_GP_LOG_DIRECTORY_ADDRESS;
ChannelExtension->DeviceExtension->QueryLogPages.LogPage[*index].PageNumber = 0;
ChannelExtension->DeviceExtension->QueryLogPages.LogPage[*index].BlockCount = 1;
ChannelExtension->DeviceExtension->QueryLogPages.LogPage[*index].FeatureField = 0;
*index = *index + 1;
// Read Device Statistics log - supported page
ChannelExtension->DeviceExtension->QueryLogPages.LogPage[*index].Query = TRUE;
ChannelExtension->DeviceExtension->QueryLogPages.LogPage[*index].LogAddress = IDE_GP_LOG_DEVICE_STATISTICS_ADDRESS;
ChannelExtension->DeviceExtension->QueryLogPages.LogPage[*index].PageNumber = IDE_GP_LOG_SUPPORTED_PAGES;
ChannelExtension->DeviceExtension->QueryLogPages.LogPage[*index].BlockCount = 1;
ChannelExtension->DeviceExtension->QueryLogPages.LogPage[*index].FeatureField = 0;
*index = *index + 1;
// Read Device Statistics log - general page
ChannelExtension->DeviceExtension->QueryLogPages.LogPage[*index].Query = TRUE;
ChannelExtension->DeviceExtension->QueryLogPages.LogPage[*index].LogAddress = IDE_GP_LOG_DEVICE_STATISTICS_ADDRESS;
ChannelExtension->DeviceExtension->QueryLogPages.LogPage[*index].PageNumber = IDE_GP_LOG_DEVICE_STATISTICS_GENERAL_PAGE;
ChannelExtension->DeviceExtension->QueryLogPages.LogPage[*index].BlockCount = 1;
ChannelExtension->DeviceExtension->QueryLogPages.LogPage[*index].FeatureField = 0;
*index = *index + 1;
// Read Identify Device Data log - supported page
ChannelExtension->DeviceExtension->QueryLogPages.LogPage[*index].Query = TRUE;
ChannelExtension->DeviceExtension->QueryLogPages.LogPage[*index].LogAddress = IDE_GP_LOG_IDENTIFY_DEVICE_DATA_ADDRESS;
ChannelExtension->DeviceExtension->QueryLogPages.LogPage[*index].PageNumber = IDE_GP_LOG_SUPPORTED_PAGES;
ChannelExtension->DeviceExtension->QueryLogPages.LogPage[*index].BlockCount = 1;
ChannelExtension->DeviceExtension->QueryLogPages.LogPage[*index].FeatureField = 0;
*index = *index + 1;
// Read Identify Device Data log - Supported Capabilities page
ChannelExtension->DeviceExtension->QueryLogPages.LogPage[*index].Query = TRUE;
ChannelExtension->DeviceExtension->QueryLogPages.LogPage[*index].LogAddress = IDE_GP_LOG_IDENTIFY_DEVICE_DATA_ADDRESS;
ChannelExtension->DeviceExtension->QueryLogPages.LogPage[*index].PageNumber = IDE_GP_LOG_IDENTIFY_DEVICE_DATA_SUPPORTED_CAPABILITIES_PAGE;
ChannelExtension->DeviceExtension->QueryLogPages.LogPage[*index].BlockCount = 1;
ChannelExtension->DeviceExtension->QueryLogPages.LogPage[*index].FeatureField = 0;
*index = *index + 1;
// Read Identify Device Data log - SATA page
ChannelExtension->DeviceExtension->QueryLogPages.LogPage[*index].Query = TRUE;
ChannelExtension->DeviceExtension->QueryLogPages.LogPage[*index].LogAddress = IDE_GP_LOG_IDENTIFY_DEVICE_DATA_ADDRESS;
ChannelExtension->DeviceExtension->QueryLogPages.LogPage[*index].PageNumber = IDE_GP_LOG_IDENTIFY_DEVICE_DATA_SATA_PAGE;
ChannelExtension->DeviceExtension->QueryLogPages.LogPage[*index].BlockCount = 1;
ChannelExtension->DeviceExtension->QueryLogPages.LogPage[*index].FeatureField = 0;
*index = *index + 1;
// Read Saved Device Internal log
ChannelExtension->DeviceExtension->QueryLogPages.LogPage[*index].Query = TRUE;
ChannelExtension->DeviceExtension->QueryLogPages.LogPage[*index].LogAddress = IDE_GP_LOG_SAVED_DEVICE_INTERNAL_STATUS;
ChannelExtension->DeviceExtension->QueryLogPages.LogPage[*index].PageNumber = 0;
ChannelExtension->DeviceExtension->QueryLogPages.LogPage[*index].BlockCount = 1;
ChannelExtension->DeviceExtension->QueryLogPages.LogPage[*index].FeatureField = 0;
*index = *index + 1;
// Read NCQ non-Data log
if ((ChannelExtension->DeviceExtension->IdentifyDeviceData->SerialAtaCapabilities.NCQ == 1) &&
(ChannelExtension->DeviceExtension->IdentifyDeviceData->SerialAtaCapabilities.NcqQueueMgmt == 1)) {
ChannelExtension->DeviceExtension->QueryLogPages.LogPage[*index].Query = TRUE;
} else {
ChannelExtension->DeviceExtension->QueryLogPages.LogPage[*index].Query = FALSE;
}
ChannelExtension->DeviceExtension->QueryLogPages.LogPage[*index].LogAddress = IDE_GP_LOG_NCQ_NON_DATA_ADDRESS;
ChannelExtension->DeviceExtension->QueryLogPages.LogPage[*index].PageNumber = 0;
ChannelExtension->DeviceExtension->QueryLogPages.LogPage[*index].BlockCount = 1;
ChannelExtension->DeviceExtension->QueryLogPages.LogPage[*index].FeatureField = 0;
*index = *index + 1;
// Read NCQ Send Receive log
if ((ChannelExtension->DeviceExtension->IdentifyDeviceData->SerialAtaCapabilities.NCQ == 1) &&
(ChannelExtension->DeviceExtension->IdentifyDeviceData->SerialAtaCapabilities.NcqReceiveSend == 1)) {
ChannelExtension->DeviceExtension->QueryLogPages.LogPage[*index].Query = TRUE;
} else {
ChannelExtension->DeviceExtension->QueryLogPages.LogPage[*index].Query = FALSE;
}
ChannelExtension->DeviceExtension->QueryLogPages.LogPage[*index].LogAddress = IDE_GP_LOG_NCQ_SEND_RECEIVE_ADDRESS;
ChannelExtension->DeviceExtension->QueryLogPages.LogPage[*index].PageNumber = 0;
ChannelExtension->DeviceExtension->QueryLogPages.LogPage[*index].BlockCount = 1;
ChannelExtension->DeviceExtension->QueryLogPages.LogPage[*index].FeatureField = 0;
*index = *index + 1;
NT_ASSERT(*index <= ATA_GPL_PAGES_QUERY_COUNT);
return;
}
VOID
IssueReadLogExtCommand(
_In_ PAHCI_CHANNEL_EXTENSION ChannelExtension,
_In_ PSTORAGE_REQUEST_BLOCK Srb,
_In_ UCHAR LogAddress,
_In_ USHORT PageNumber,
_In_ USHORT BlockCount,
_In_ USHORT FeatureField,
_In_opt_ PSTOR_PHYSICAL_ADDRESS PhysicalAddress,
_In_ PVOID DataBuffer,
_In_opt_ PSRB_COMPLETION_ROUTINE CompletionRoutine
)
/*++
Issue READ LOG EXT command to device
Parameters:
ChannelExtension - port that the command should be sent to
Srb - Srb that carries READ LOG EXT command in SrbExtension
LogAddress - Log address
PageNumber - Page# of the log
BlockCount - How many blocks (in 512 bytes)
FeatureField - log specific value
PhysicalAddress - Buffer physical address
DataBuffer - Buffer
CompletionRoutine - Routine that needs to be executed after READ LOG EXT command completed
Return Values:
None
--*/
{
PAHCI_SRB_EXTENSION srbExtension = GetSrbExtension(Srb);
UNREFERENCED_PARAMETER(ChannelExtension);
//1 Fills in the local SRB
srbExtension->AtaFunction = ATA_FUNCTION_ATA_COMMAND;
srbExtension->Flags |= ATA_FLAGS_DATA_IN ;
srbExtension->Flags |= ATA_FLAGS_48BIT_COMMAND ;
srbExtension->CompletionRoutine = CompletionRoutine;
//setup TaskFile
srbExtension->TaskFile.Current.bFeaturesReg = (UCHAR)(FeatureField & 0xFF); //FeatureField, low part
srbExtension->TaskFile.Current.bSectorCountReg = (UCHAR)(BlockCount & 0xFF); //Number of blocks to read, low part
srbExtension->TaskFile.Current.bSectorNumberReg = LogAddress; //Log address
srbExtension->TaskFile.Current.bCylLowReg = (UCHAR)(PageNumber & 0xFF); //Page#, low part
srbExtension->TaskFile.Current.bCylHighReg = 0;
srbExtension->TaskFile.Current.bDriveHeadReg = 0xA0 | IDE_LBA_MODE;
srbExtension->TaskFile.Current.bCommandReg = IDE_COMMAND_READ_LOG_EXT;
srbExtension->TaskFile.Current.bReserved = 0;
srbExtension->TaskFile.Previous.bFeaturesReg = (UCHAR)((FeatureField >> 8) & 0xFF); //FeatureField, high part
srbExtension->TaskFile.Previous.bSectorCountReg = (UCHAR)((BlockCount >> 8) & 0xFF); //Number of blocks to read, high part
srbExtension->TaskFile.Previous.bSectorNumberReg = 0;
srbExtension->TaskFile.Previous.bCylLowReg = (UCHAR)((PageNumber >> 8) & 0xFF); //Page#, high part
srbExtension->TaskFile.Previous.bCylHighReg = 0;
srbExtension->TaskFile.Previous.bDriveHeadReg = 0;
srbExtension->TaskFile.Previous.bCommandReg = 0;
srbExtension->TaskFile.Previous.bReserved = 0;
Srb->SrbStatus = SRB_STATUS_PENDING;
srbExtension->DataBuffer = DataBuffer;
if ( PhysicalAddress ) {
srbExtension->DataBufferPhysicalAddress.QuadPart = PhysicalAddress->QuadPart;
}
//setup SGL
if ( PhysicalAddress ) {
srbExtension->LocalSgl.NumberOfElements = 1;
srbExtension->LocalSgl.List[0].PhysicalAddress.LowPart = PhysicalAddress->LowPart;
srbExtension->LocalSgl.List[0].PhysicalAddress.HighPart = PhysicalAddress->HighPart;
srbExtension->LocalSgl.List[0].Length = ATA_BLOCK_SIZE * (ULONG)BlockCount;
srbExtension->Sgl = &srbExtension->LocalSgl;
srbExtension->DataTransferLength = ATA_BLOCK_SIZE * (ULONG)BlockCount;
}
return;
}
__inline
USHORT
GetNextQueryLogPageIndex (
_In_ PAHCI_CHANNEL_EXTENSION ChannelExtension
)
/*++
Get an Index value that log page should be queried.
Parameters:
ChannelExtension
Return Values:
USHORT
--*/
{
USHORT i;
USHORT index = ChannelExtension->DeviceExtension->QueryLogPages.CurrentPageIndex;
if (index >= ChannelExtension->DeviceExtension->QueryLogPages.TotalPageCount) {
return ATA_GPL_PAGES_INVALID_INDEX;
}
for (i = index; i < ChannelExtension->DeviceExtension->QueryLogPages.TotalPageCount; i++) {
if (ChannelExtension->DeviceExtension->QueryLogPages.LogPage[i].Query) {
ChannelExtension->DeviceExtension->QueryLogPages.CurrentPageIndex = i;
return i;
}
}
return ATA_GPL_PAGES_INVALID_INDEX;
}
__inline
VOID
ReadQueryLogPage (
_In_ PAHCI_CHANNEL_EXTENSION ChannelExtension,
_In_ PSTORAGE_REQUEST_BLOCK Srb,
_In_ USHORT Index
)
{
IssueReadLogExtCommand( ChannelExtension,
Srb,
ChannelExtension->DeviceExtension->QueryLogPages.LogPage[Index].LogAddress,
ChannelExtension->DeviceExtension->QueryLogPages.LogPage[Index].PageNumber,
ChannelExtension->DeviceExtension->QueryLogPages.LogPage[Index].BlockCount,
ChannelExtension->DeviceExtension->QueryLogPages.LogPage[Index].FeatureField,
&ChannelExtension->DeviceExtension->ReadLogExtPageDataPhysicalAddress,
(PVOID)ChannelExtension->DeviceExtension->ReadLogExtPageData,
LogPageDiscoveryCompletion
);
}
__inline
VOID
UpdateQueryLogPageSupportive (
_In_ PAHCI_CHANNEL_EXTENSION ChannelExtension,
_In_ UCHAR LogAddress,
_In_ USHORT PageNumber,
_In_ BOOLEAN Supported
)
/*++
Mark log page supportive information.
Parameters:
ChannelExtension - port that log-page-to-query should be updated.
LogAddress - Log address
PageNumber - Page# of the log
Supported - log page is supported or not by device
Return Values:
None
--*/
{
USHORT i;
for (i = 0; i < ChannelExtension->DeviceExtension->QueryLogPages.TotalPageCount; i++) {
if ((ChannelExtension->DeviceExtension->QueryLogPages.LogPage[i].LogAddress == LogAddress) &&
(ChannelExtension->DeviceExtension->QueryLogPages.LogPage[i].PageNumber == PageNumber)) {
ChannelExtension->DeviceExtension->QueryLogPages.LogPage[i].Query = Supported;
return;
}
}
return;
}
VOID
UpdateDownloadMicrocodeSupport(
_Inout_ PAHCI_CHANNEL_EXTENSION ChannelExtension,
_In_ PIDENTIFY_DEVICE_DATA_LOG_PAGE_SUPPORTED_CAPABILITIES Capabilities
)
/*++
Routine Description:
Takes the given Supported Capabilities log page and updates the firmware
update (download microcode) support information that is cached in the given
Channel Extension.
Arguments:
Channel Extension
Supported Capabilities
--*/
{
NT_ASSERT(Capabilities->Header.RevisionNumber == IDE_GP_LOG_VERSION);
NT_ASSERT(Capabilities->Header.PageNumber == IDE_GP_LOG_IDENTIFY_DEVICE_DATA_SUPPORTED_CAPABILITIES_PAGE);
//
// Set it to unsupported by default.
//
ChannelExtension->DeviceExtension->FirmwareUpdate.DmOffsetsDeferredSupported = 0;
//
// If the Download Microcode capabilites are valid then fill in our cached copy.
//
if (Capabilities->DownloadMicrocodeCapabilities.Valid == 1) {
if ((ChannelExtension->DeviceExtension->IdentifyDeviceData->AdditionalSupported.DownloadMicrocodeDmaSupported == 1) ||
(ChannelExtension->DeviceExtension->IdentifyDeviceData->CommandSetSupport.DownloadMicrocode == 1)) {
ChannelExtension->DeviceExtension->FirmwareUpdate.DmOffsetsDeferredSupported = (Capabilities->DownloadMicrocodeCapabilities.DmOffsetsDeferredSupported == 1);
if (ChannelExtension->DeviceExtension->FirmwareUpdate.DmOffsetsDeferredSupported) {
if ((Capabilities->DownloadMicrocodeCapabilities.DmMinTransferSize > 0) &&
(Capabilities->DownloadMicrocodeCapabilities.DmMinTransferSize < 0xFFFF)) {
ChannelExtension->DeviceExtension->FirmwareUpdate.DmMinTransferBlocks = (USHORT)min(Capabilities->DownloadMicrocodeCapabilities.DmMinTransferSize, AHCI_MAX_TRANSFER_LENGTH_DEFAULT / ATA_BLOCK_SIZE);
} else {
ChannelExtension->DeviceExtension->FirmwareUpdate.DmMinTransferBlocks = 1;
}
if ((Capabilities->DownloadMicrocodeCapabilities.DmMaxTransferSize > 0) &&
(Capabilities->DownloadMicrocodeCapabilities.DmMaxTransferSize < 0xFFFF)) {
ChannelExtension->DeviceExtension->FirmwareUpdate.DmMaxTransferBlocks = (USHORT)min(Capabilities->DownloadMicrocodeCapabilities.DmMaxTransferSize, AHCI_MAX_TRANSFER_LENGTH_DEFAULT / ATA_BLOCK_SIZE);
} else {
ChannelExtension->DeviceExtension->FirmwareUpdate.DmMaxTransferBlocks = AHCI_MAX_TRANSFER_LENGTH_DEFAULT / ATA_BLOCK_SIZE;
}
}
}
}
}
VOID
GetDownloadMicrocodeSupportCompletion(
_Inout_ PAHCI_CHANNEL_EXTENSION ChannelExtension,
_In_ PSTORAGE_REQUEST_BLOCK Srb
)
/*++
Routine Description:
Called when the Supported Capabilities log page has been obtained from the
device. The log page is parsed for the Download Microcode support and the
cached copy of this data is updated in the ChannelExtension.
Arguments:
Channel Extension
SRB
--*/
{
PAHCI_SRB_EXTENSION srbExtension;
srbExtension = GetSrbExtension(Srb);
//
// If the request succeeded then update the cached copy of the Download
// Microcode support information.
//
if (Srb->SrbStatus == SRB_STATUS_SUCCESS) {
UpdateDownloadMicrocodeSupport(ChannelExtension, srbExtension->DataBuffer);
}
AhciFreeDmaBuffer(ChannelExtension->AdapterExtension, IDE_GP_LOG_SECTOR_SIZE, srbExtension->DataBuffer, srbExtension->DataBufferPhysicalAddress);
//
// The SRB will be completed after this completion routine returns so
// there's no need to do it here.
//
}
ULONG
GetDownloadMicrocodeSupport(
_In_ PAHCI_CHANNEL_EXTENSION ChannelExtension,
_In_ PSTORAGE_REQUEST_BLOCK Srb
)
/*++
Routine Description:
Queries the Download Microcode support from the Identify Log Page and
updates our cached copy of it.
Arguments:
ChannelExtension
SRB
Return Value:
Status code.
--*/
{
ULONG status = STOR_STATUS_SUCCESS;
PVOID buffer = NULL;
STOR_PHYSICAL_ADDRESS bufferPhysicalAddress = { 0 };
//
// Fail the request if it's not supported by device.
//
if (IsDeviceGeneralPurposeLoggingSupported(ChannelExtension) == FALSE) {
Srb->SrbStatus = SRB_STATUS_INVALID_REQUEST;
return STOR_STATUS_INVALID_PARAMETER;
}
//
// Allocate DMA buffer to the log page.
//
status = AhciAllocateDmaBuffer((PVOID)ChannelExtension->AdapterExtension, IDE_GP_LOG_SECTOR_SIZE, (PVOID*)&buffer, &bufferPhysicalAddress);
if ((status != STOR_STATUS_SUCCESS) || (buffer == NULL)) {
if (buffer != NULL) {
AhciFreeDmaBuffer((PVOID)ChannelExtension->AdapterExtension, IDE_GP_LOG_SECTOR_SIZE, buffer, bufferPhysicalAddress);
}
Srb->SrbStatus = SRB_STATUS_ERROR;
return STOR_STATUS_INSUFFICIENT_RESOURCES;
}
AhciZeroMemory((PCHAR)buffer, IDE_GP_LOG_SECTOR_SIZE);
//
// Issue the command. The completion routine will update the cached
// firmware update information with the info from the log page.
// The completion routine will also free the DMA buffer.
//
IssueReadLogExtCommand(ChannelExtension,
Srb,
IDE_GP_LOG_IDENTIFY_DEVICE_DATA_ADDRESS,
IDE_GP_LOG_IDENTIFY_DEVICE_DATA_SUPPORTED_CAPABILITIES_PAGE,
1,
0, // feature field
&bufferPhysicalAddress,
buffer,
GetDownloadMicrocodeSupportCompletion);
return status;
}
VOID
LogPageDiscoveryCompletion (
_In_ PAHCI_CHANNEL_EXTENSION ChannelExtension,
_In_ PSTORAGE_REQUEST_BLOCK Srb
)
/*
This process is to discover all needed information from general log pages.
The process is initiated by reading log directory when Identify Device command completes.
*/
{
USHORT nextPageIndex = ATA_GPL_PAGES_INVALID_INDEX;
UCHAR completedLogAddress = 0;
USHORT completedPageNumber = 0;
PAHCI_SRB_EXTENSION srbExtension = GetSrbExtension(Srb);
// get the Log Address and Log Page just received
completedLogAddress = srbExtension->TaskFile.Current.bSectorNumberReg;
completedPageNumber = ((USHORT)srbExtension->TaskFile.Previous.bCylLowReg << 8) | srbExtension->TaskFile.Current.bCylLowReg;
if ( (completedLogAddress == IDE_GP_LOG_DIRECTORY_ADDRESS) && (completedPageNumber == 0) ) {
// the issued command was for getting Log Directory
// ACS8-3, 4.12.1 Devices that report support for the NCQ feature set shall also report support for the GPL feature set (see 4.9),
// the General Purpose Log Directory log and the NCQ Command Error log.
NT_ASSERT( (Srb->SrbStatus == SRB_STATUS_SUCCESS) ||
(ChannelExtension->DeviceExtension->IdentifyDeviceData->SerialAtaCapabilities.NCQ == 0) );
if ( (Srb->SrbStatus == SRB_STATUS_SUCCESS) &&
(ChannelExtension->DeviceExtension->ReadLogExtPageData[0] == IDE_GP_LOG_VERSION) ) {
// per ACS spec: The value of the General Purpose Logging Version word shall be 0001h
// preserve the log address supportive information
ChannelExtension->DeviceExtension->SupportedGPLPages.DeviceStatistics.LogAddressSupported = (ChannelExtension->DeviceExtension->ReadLogExtPageData[IDE_GP_LOG_DEVICE_STATISTICS_ADDRESS] > 0) ? 1 : 0;
if (ChannelExtension->DeviceExtension->SupportedGPLPages.DeviceStatistics.LogAddressSupported == 0) {
UpdateQueryLogPageSupportive(ChannelExtension, IDE_GP_LOG_DEVICE_STATISTICS_ADDRESS, IDE_GP_LOG_SUPPORTED_PAGES, FALSE);
UpdateQueryLogPageSupportive(ChannelExtension, IDE_GP_LOG_DEVICE_STATISTICS_ADDRESS, IDE_GP_LOG_DEVICE_STATISTICS_GENERAL_PAGE, FALSE);
}
ChannelExtension->DeviceExtension->SupportedGPLPages.IdentifyDeviceData.LogAddressSupported = (ChannelExtension->DeviceExtension->ReadLogExtPageData[IDE_GP_LOG_IDENTIFY_DEVICE_DATA_ADDRESS] > 0) ? 1 : 0;
if (ChannelExtension->DeviceExtension->SupportedGPLPages.IdentifyDeviceData.LogAddressSupported == 0) {
UpdateQueryLogPageSupportive(ChannelExtension, IDE_GP_LOG_IDENTIFY_DEVICE_DATA_ADDRESS, IDE_GP_LOG_SUPPORTED_PAGES, FALSE);
UpdateQueryLogPageSupportive(ChannelExtension, IDE_GP_LOG_IDENTIFY_DEVICE_DATA_ADDRESS, IDE_GP_LOG_IDENTIFY_DEVICE_DATA_SUPPORTED_CAPABILITIES_PAGE, FALSE);
UpdateQueryLogPageSupportive(ChannelExtension, IDE_GP_LOG_IDENTIFY_DEVICE_DATA_ADDRESS, IDE_GP_LOG_IDENTIFY_DEVICE_DATA_SATA_PAGE, FALSE);
}
ChannelExtension->DeviceExtension->SupportedGPLPages.SinglePage.NcqCommandError = (ChannelExtension->DeviceExtension->ReadLogExtPageData[IDE_GP_LOG_NCQ_COMMAND_ERROR_ADDRESS] > 0) ? 1 : 0;
ChannelExtension->DeviceExtension->SupportedGPLPages.SinglePage.NcqNonData = (ChannelExtension->DeviceExtension->ReadLogExtPageData[IDE_GP_LOG_NCQ_NON_DATA_ADDRESS] > 0) ? 1 : 0;
if (ChannelExtension->DeviceExtension->SupportedGPLPages.SinglePage.NcqNonData == 0) {
UpdateQueryLogPageSupportive(ChannelExtension, IDE_GP_LOG_NCQ_NON_DATA_ADDRESS, 0, FALSE);
}
ChannelExtension->DeviceExtension->SupportedGPLPages.SinglePage.NcqSendReceive = (ChannelExtension->DeviceExtension->ReadLogExtPageData[IDE_GP_LOG_NCQ_SEND_RECEIVE_ADDRESS] > 0) ? 1 : 0;
if (ChannelExtension->DeviceExtension->SupportedGPLPages.SinglePage.NcqSendReceive == 0) {
UpdateQueryLogPageSupportive(ChannelExtension, IDE_GP_LOG_NCQ_SEND_RECEIVE_ADDRESS, 0, FALSE);
}
ChannelExtension->DeviceExtension->SupportedGPLPages.SinglePage.HybridInfo = (ChannelExtension->DeviceExtension->ReadLogExtPageData[IDE_GP_LOG_HYBRID_INFO_ADDRESS] > 0) ? 1 : 0;
} else {
// Log Directory can be optional. Preset supportive info, they will be updated if the actual command fails later.
// In case of the disk doesn't support NCQ and doesn't support Log Directory, still try to discover some log pages.
// don't query all other log pages.
UpdateQueryLogPageSupportive(ChannelExtension, IDE_GP_LOG_DEVICE_STATISTICS_ADDRESS, IDE_GP_LOG_SUPPORTED_PAGES, FALSE);
UpdateQueryLogPageSupportive(ChannelExtension, IDE_GP_LOG_DEVICE_STATISTICS_ADDRESS, IDE_GP_LOG_DEVICE_STATISTICS_GENERAL_PAGE, FALSE);
UpdateQueryLogPageSupportive(ChannelExtension, IDE_GP_LOG_IDENTIFY_DEVICE_DATA_ADDRESS, IDE_GP_LOG_SUPPORTED_PAGES, FALSE);
UpdateQueryLogPageSupportive(ChannelExtension, IDE_GP_LOG_IDENTIFY_DEVICE_DATA_ADDRESS, IDE_GP_LOG_IDENTIFY_DEVICE_DATA_SUPPORTED_CAPABILITIES_PAGE, FALSE);
UpdateQueryLogPageSupportive(ChannelExtension, IDE_GP_LOG_IDENTIFY_DEVICE_DATA_ADDRESS, IDE_GP_LOG_IDENTIFY_DEVICE_DATA_SATA_PAGE, FALSE);
UpdateQueryLogPageSupportive(ChannelExtension, IDE_GP_LOG_NCQ_NON_DATA_ADDRESS, 0, FALSE);
UpdateQueryLogPageSupportive(ChannelExtension, IDE_GP_LOG_NCQ_SEND_RECEIVE_ADDRESS, 0, FALSE);
}
} else if ( (completedLogAddress == IDE_GP_LOG_DEVICE_STATISTICS_ADDRESS) && (completedPageNumber == IDE_GP_LOG_SUPPORTED_PAGES) ) {
// the issued command was for getting supported log pages of device statistics log
if (Srb->SrbStatus == SRB_STATUS_SUCCESS) {
PDEVICE_STATISTICS_LOG_PAGE_HEADER pageHeader = (PDEVICE_STATISTICS_LOG_PAGE_HEADER)ChannelExtension->DeviceExtension->ReadLogExtPageData;
PUCHAR pageSupported = (PUCHAR)ChannelExtension->DeviceExtension->ReadLogExtPageData;
// first byte after header is how many entries in following list.
UCHAR pageCount = *(pageSupported + sizeof(DEVICE_STATISTICS_LOG_PAGE_HEADER));
// The value of revision number word shall be 0001h. The first supported page shall be 00h.
if ( (pageHeader->RevisionNumber == IDE_GP_LOG_VERSION) &&
(pageHeader->PageNumber == IDE_GP_LOG_SUPPORTED_PAGES) &&
(pageCount > 1) ) {
int i;
for (i = 1; i <= pageCount; i++) {
// if the page number is shown in supported list, mark it's supported.
if (*(pageSupported + sizeof(DEVICE_STATISTICS_LOG_PAGE_HEADER) + i) == IDE_GP_LOG_DEVICE_STATISTICS_GENERAL_PAGE) {
ChannelExtension->DeviceExtension->SupportedGPLPages.DeviceStatistics.GeneralStatistics = 1;
}
if (*(pageSupported + sizeof(DEVICE_STATISTICS_LOG_PAGE_HEADER) + i) == IDE_GP_LOG_DEVICE_STATISTICS_TEMPERATURE_PAGE) {
ChannelExtension->DeviceExtension->SupportedGPLPages.DeviceStatistics.TemperatureStatistics = 1;
break;
}
}
}
if (ChannelExtension->DeviceExtension->SupportedGPLPages.DeviceStatistics.GeneralStatistics == 0) {
UpdateQueryLogPageSupportive(ChannelExtension, IDE_GP_LOG_DEVICE_STATISTICS_ADDRESS, IDE_GP_LOG_DEVICE_STATISTICS_GENERAL_PAGE, FALSE);
}
} else {
ChannelExtension->DeviceExtension->SupportedGPLPages.DeviceStatistics.LogAddressSupported = 0;
UpdateQueryLogPageSupportive(ChannelExtension, IDE_GP_LOG_DEVICE_STATISTICS_ADDRESS, IDE_GP_LOG_DEVICE_STATISTICS_GENERAL_PAGE, FALSE);
}
} else if ( (completedLogAddress == IDE_GP_LOG_DEVICE_STATISTICS_ADDRESS) && (completedPageNumber == IDE_GP_LOG_DEVICE_STATISTICS_GENERAL_PAGE) ) {
// the issued command was for getting General Statistics page of Device Statistics Log
if (Srb->SrbStatus == SRB_STATUS_SUCCESS) {
PGP_LOG_GENERAL_STATISTICS generalStatistics = (PGP_LOG_GENERAL_STATISTICS)ChannelExtension->DeviceExtension->ReadLogExtPageData;
// The value of revision number word shall be 0002h. (It's changed to 0001h in ACS4)
if ( ((generalStatistics->Header.RevisionNumber == 0x0002) || (generalStatistics->Header.RevisionNumber == IDE_GP_LOG_VERSION)) &&
(generalStatistics->Header.PageNumber == IDE_GP_LOG_DEVICE_STATISTICS_GENERAL_PAGE) ) {
if (generalStatistics->DateAndTime.Supported == 1) {
ChannelExtension->DeviceExtension->SupportedCommands.SetDateAndTime = 1;
}
}
} else {
ChannelExtension->DeviceExtension->SupportedGPLPages.DeviceStatistics.GeneralStatistics = 0;
}
} else if ( (completedLogAddress == IDE_GP_LOG_IDENTIFY_DEVICE_DATA_ADDRESS) && (completedPageNumber == IDE_GP_LOG_SUPPORTED_PAGES) ) {
// the issued command was for getting supported log pages of identify device data log
if (Srb->SrbStatus == SRB_STATUS_SUCCESS) {
PIDENTIFY_DEVICE_DATA_LOG_PAGE_HEADER pageHeader = (PIDENTIFY_DEVICE_DATA_LOG_PAGE_HEADER)ChannelExtension->DeviceExtension->ReadLogExtPageData;
PUCHAR pageSupported = (PUCHAR)ChannelExtension->DeviceExtension->ReadLogExtPageData;
// first byte after header is how many entries in following list.
UCHAR pageCount = *(pageSupported + sizeof(IDENTIFY_DEVICE_DATA_LOG_PAGE_HEADER));
// The value of revision number word shall be 0001h. The first supported page shall be 00h.
if ( (pageHeader->RevisionNumber == IDE_GP_LOG_VERSION) &&
(pageHeader->PageNumber == IDE_GP_LOG_SUPPORTED_PAGES) &&
(pageCount > 1) ) {
int i;
for (i = 1; i <= pageCount; i++) {
// if the page number is shown in supported list, mark it's supported.
if (*(pageSupported + sizeof(IDENTIFY_DEVICE_DATA_LOG_PAGE_HEADER) + i) == IDE_GP_LOG_IDENTIFY_DEVICE_DATA_SUPPORTED_CAPABILITIES_PAGE) {
ChannelExtension->DeviceExtension->SupportedGPLPages.IdentifyDeviceData.SupportedCapabilities = 1;
} else if (*(pageSupported + sizeof(IDENTIFY_DEVICE_DATA_LOG_PAGE_HEADER) + i) == IDE_GP_LOG_IDENTIFY_DEVICE_DATA_SATA_PAGE) {
ChannelExtension->DeviceExtension->SupportedGPLPages.IdentifyDeviceData.SATA = 1;
}
}
}
if (ChannelExtension->DeviceExtension->SupportedGPLPages.IdentifyDeviceData.SATA == 0) {
UpdateQueryLogPageSupportive(ChannelExtension, IDE_GP_LOG_IDENTIFY_DEVICE_DATA_ADDRESS, IDE_GP_LOG_IDENTIFY_DEVICE_DATA_SUPPORTED_CAPABILITIES_PAGE, FALSE);
UpdateQueryLogPageSupportive(ChannelExtension, IDE_GP_LOG_IDENTIFY_DEVICE_DATA_ADDRESS, IDE_GP_LOG_IDENTIFY_DEVICE_DATA_SATA_PAGE, FALSE);
}
} else {
ChannelExtension->DeviceExtension->SupportedGPLPages.IdentifyDeviceData.LogAddressSupported = 0;
UpdateQueryLogPageSupportive(ChannelExtension, IDE_GP_LOG_IDENTIFY_DEVICE_DATA_ADDRESS, IDE_GP_LOG_IDENTIFY_DEVICE_DATA_SUPPORTED_CAPABILITIES_PAGE, FALSE);
UpdateQueryLogPageSupportive(ChannelExtension, IDE_GP_LOG_IDENTIFY_DEVICE_DATA_ADDRESS, IDE_GP_LOG_IDENTIFY_DEVICE_DATA_SATA_PAGE, FALSE);
}
} else if ( (completedLogAddress == IDE_GP_LOG_IDENTIFY_DEVICE_DATA_ADDRESS) && (completedPageNumber == IDE_GP_LOG_IDENTIFY_DEVICE_DATA_SUPPORTED_CAPABILITIES_PAGE) ) {
// the issued command was for getting supported capabilities log page of identify device data log
if (Srb->SrbStatus == SRB_STATUS_SUCCESS) {
PIDENTIFY_DEVICE_DATA_LOG_PAGE_SUPPORTED_CAPABILITIES supportedCapabilities = (PIDENTIFY_DEVICE_DATA_LOG_PAGE_SUPPORTED_CAPABILITIES)ChannelExtension->DeviceExtension->ReadLogExtPageData;
// The value of revision number word shall be 0001h.
if ((supportedCapabilities->Header.RevisionNumber == IDE_GP_LOG_VERSION) &&
(supportedCapabilities->Header.PageNumber == IDE_GP_LOG_IDENTIFY_DEVICE_DATA_SUPPORTED_CAPABILITIES_PAGE)) {
UpdateDownloadMicrocodeSupport(ChannelExtension, supportedCapabilities);
}
} else {
ChannelExtension->DeviceExtension->SupportedGPLPages.IdentifyDeviceData.SupportedCapabilities = 0;
}
} else if ( (completedLogAddress == IDE_GP_LOG_IDENTIFY_DEVICE_DATA_ADDRESS) && (completedPageNumber == IDE_GP_LOG_IDENTIFY_DEVICE_DATA_SATA_PAGE) ) {
// the issued command was for getting SATA log page of identify device data log
if (Srb->SrbStatus == SRB_STATUS_SUCCESS) {
} else {
ChannelExtension->DeviceExtension->SupportedGPLPages.IdentifyDeviceData.SATA = 0;
}
} else if ( (completedLogAddress == IDE_GP_LOG_SAVED_DEVICE_INTERNAL_STATUS) && (completedPageNumber == 0) ) {
// the issued command was for getting saved device internal data log
if (Srb->SrbStatus != SRB_STATUS_SUCCESS) {
ChannelExtension->DeviceExtension->SupportedGPLPages.SinglePage.SavedDeviceInternalStatusData = 0;
}
} else if ( (completedLogAddress == IDE_GP_LOG_NCQ_NON_DATA_ADDRESS) && (completedPageNumber == 0) ) {
// the issued command was for getting ncq non-data log
if (Srb->SrbStatus == SRB_STATUS_SUCCESS) {
PGP_LOG_NCQ_NON_DATA ncqNonData = (PGP_LOG_NCQ_NON_DATA)ChannelExtension->DeviceExtension->ReadLogExtPageData;
ChannelExtension->DeviceExtension->SupportedCommands.HybridDemoteBySize = ncqNonData->SubCmd2.HybridDemoteBySize;
ChannelExtension->DeviceExtension->SupportedCommands.HybridChangeByLbaRange = ncqNonData->SubCmd3.HybridChangeByLbaRange;
ChannelExtension->DeviceExtension->SupportedCommands.HybridControl = ncqNonData->SubCmd4.HybridControl;
} else {
NT_ASSERT(FALSE);
}
} else if ( (completedLogAddress == IDE_GP_LOG_NCQ_SEND_RECEIVE_ADDRESS) && (completedPageNumber == 0) ) {
// the issued command was for getting ncq send receive log
if (Srb->SrbStatus == SRB_STATUS_SUCCESS) {
PGP_LOG_NCQ_SEND_RECEIVE ncqSendReceive = (PGP_LOG_NCQ_SEND_RECEIVE)ChannelExtension->DeviceExtension->ReadLogExtPageData;
ChannelExtension->DeviceExtension->SupportedCommands.HybridEvict = ncqSendReceive->SubCmd.HybridEvict;
} else {
NT_ASSERT(FALSE);
}
} else {
// all log addresses and log pages in log page discovery process should be covered in above conditions.
NT_ASSERT(FALSE);
}
//
// Move index to the next one and check if there is any log pages pending to read.
//
ChannelExtension->DeviceExtension->QueryLogPages.CurrentPageIndex++;
nextPageIndex = GetNextQueryLogPageIndex(ChannelExtension);
if (nextPageIndex != ATA_GPL_PAGES_INVALID_INDEX) {
ReadQueryLogPage(ChannelExtension, Srb, nextPageIndex);
} else {
ReportLunsComplete(ChannelExtension, Srb);
// Use a work itme to preserve information from Log Pages into registry.
PreserveLogPageInformation(ChannelExtension);
}
return;
}
VOID
UpdateDeviceType(
_In_ PAHCI_CHANNEL_EXTENSION ChannelExtension
)
/*++
Routine Description:
Update device type information after device enumeration.
Assumption - PxSIG is ready to be accessed.
Arguments:
ChannelExtension
Return Value:
None.
--*/
{
PATA_DEVICE_PARAMETERS deviceParameters = &ChannelExtension->DeviceExtension->DeviceParameters;
// There is chance that during device enumeration, port is not started yet and signature register is 0xffffffff,
// which will result in the AtaDeviceType is set to DeviceUnknown.
// Update it here if above situation is true.
if (IsUnknownDevice(deviceParameters)) {
ULONG sig = 0;
sig = StorPortReadRegisterUlong(ChannelExtension->AdapterExtension, &ChannelExtension->Px->SIG.AsUlong);
if (sig == ATA_DEVICE_SIGNATURE_ATA) {
ChannelExtension->DeviceExtension[0].DeviceParameters.AtaDeviceType = DeviceIsAta;
} else if (sig == ATA_DEVICE_SIGNATURE_ATAPI) {
ChannelExtension->DeviceExtension[0].DeviceParameters.AtaDeviceType = DeviceIsAtapi;
} else {
NT_ASSERT(FALSE);
}
}
}
VOID
AhciPortIdentifyDevice(
_In_ PAHCI_CHANNEL_EXTENSION ChannelExtension,
_In_ PSTORAGE_REQUEST_BLOCK Srb
)
{
PAHCI_SRB_EXTENSION srbExtension;
PCDB cdb = SrbGetCdb(Srb);
srbExtension = GetSrbExtension(Srb);
if (Srb->SrbStatus == SRB_STATUS_BUS_RESET) {
return;
}
if (Srb->SrbStatus == SRB_STATUS_NO_DEVICE) {
// command failed consider as no device
ChannelExtension->DeviceExtension->DeviceParameters.AtaDeviceType = DeviceNotExist;
}
if (Srb->SrbStatus == SRB_STATUS_SUCCESS) {
// Update device ata type if it isn't initialized correctly.
UpdateDeviceType(ChannelExtension);
//Re-initialize device specific information to avoid the values being reused after device switched.
ChannelExtension->StateFlags.NCQ_Activated = 0;
ChannelExtension->StateFlags.NCQ_Succeeded = 0;
ChannelExtension->StateFlags.HybridInfoEnabledOnHiberFile = 0;
ChannelExtension->DeviceExtension->HybridCachingMediumEnableRefs = 0;
AhciZeroMemory((PCHAR)&ChannelExtension->DeviceExtension->SupportedGPLPages, sizeof(ATA_SUPPORTED_GPL_PAGES));
AhciZeroMemory((PCHAR)&ChannelExtension->DeviceExtension->SupportedCommands, sizeof(ATA_COMMAND_SUPPORTED));
AhciZeroMemory((PCHAR)&ChannelExtension->DeviceExtension->FirmwareUpdate, sizeof(DOWNLOAD_MICROCODE_CAPABILITIES));
// identify completes, digest identify data / inquiry data
UpdateDeviceParameters(ChannelExtension);
//
// Cache if PUIS is enabled or not. We'll look at this later when
// powering up the port to determine if we need to send the spin up
// command first.
//
ChannelExtension->StateFlags.PuisEnabled = ChannelExtension->DeviceExtension->IdentifyDeviceData->CommandSetActive.PowerUpInStandby;
// Initialize port properties
ChannelExtension->PortProperties = 0;
if (ChannelExtension->StateFlags.IdentifyDeviceSuccess == 0) {
ChannelExtension->StateFlags.IdentifyDeviceSuccess = 1;
}
if (IsExternalPort(ChannelExtension)) {
SETMASK(ChannelExtension->PortProperties, PORT_PROPERTIES_EXTERNAL_PORT);
}
}
// Identify Device can only be triggered from REPORT LUNS command or
// INQUIRY command (for disk in dump environment)
if ((cdb != NULL) && (cdb->CDB10.OperationCode == SCSIOP_REPORT_LUNS)) {
BOOLEAN cachedLogPageInfoUsable = FALSE;
if ((Srb->SrbStatus == SRB_STATUS_SUCCESS) &&
IsDeviceGeneralPurposeLoggingSupported(ChannelExtension)) {
ULONG storStatus;
//
// Check to determine whether use cached device settings.
//
storStatus = StorPortIsDeviceOperationAllowed(ChannelExtension->AdapterExtension,
NULL,
&STORPORT_DEVICEOPERATION_CACHED_SETTINGS_INIT_GUID,
(PULONG)(&cachedLogPageInfoUsable));
cachedLogPageInfoUsable = (cachedLogPageInfoUsable && (ChannelExtension->DeviceExtension[0].UpdateCachedLogPageInfo == FALSE));
if (cachedLogPageInfoUsable) {
// In case this is not a newly plugged in device and it's allowed to use cached Log Page Information.
CHAR valueName[16] = { 0 };
AHCI_DEVICE_LOG_PAGE_INFO logPageInfo = { 0 };
PVOID dataBuffer = &logPageInfo;
ULONG dataLength = sizeof(AHCI_DEVICE_LOG_PAGE_INFO);
GetLogInfoRegValueName(ChannelExtension, valueName, sizeof(valueName));
storStatus = StorPortRegistryReadAdapterKey(ChannelExtension->AdapterExtension,
(PUCHAR)"StorAHCI",
(PUCHAR)valueName,
MINIPORT_REG_BINARY,
&dataBuffer,
&dataLength);
if ((storStatus == STOR_STATUS_SUCCESS) && (dataLength == sizeof(AHCI_DEVICE_LOG_PAGE_INFO))) {
StorPortCopyMemory((PVOID)&ChannelExtension->DeviceExtension[0].QueryLogPages, &logPageInfo.QueryLogPages, sizeof(ATA_GPL_PAGES_TO_QUERY));
StorPortCopyMemory((PVOID)&ChannelExtension->DeviceExtension[0].SupportedGPLPages, &logPageInfo.SupportedGPLPages, sizeof(ATA_SUPPORTED_GPL_PAGES));
StorPortCopyMemory((PVOID)&ChannelExtension->DeviceExtension[0].SupportedCommands, &logPageInfo.SupportedCommands, sizeof(ATA_COMMAND_SUPPORTED));
StorPortCopyMemory((PVOID)&ChannelExtension->DeviceExtension[0].FirmwareUpdate, &logPageInfo.FirmwareUpdate, sizeof(DOWNLOAD_MICROCODE_CAPABILITIES));
ReportLunsComplete(ChannelExtension, Srb);
} else {
cachedLogPageInfoUsable = FALSE;
}
}
//
// Start log page discovery process if decide not use cached log page data.
//
if (!cachedLogPageInfoUsable) {
USHORT index;
InitQueryLogPages(ChannelExtension);
index = GetNextQueryLogPageIndex(ChannelExtension);
NT_ASSERT(index == 0);
if (index != ATA_GPL_PAGES_INVALID_INDEX) {
// First page should be log directory. Read it to get pages supported by device.
ReadQueryLogPage(ChannelExtension, Srb, index);
} else {
ReportLunsComplete(ChannelExtension, Srb);
}
}
} else {
ReportLunsComplete(ChannelExtension, Srb);
}
} else if (IsDumpMode(ChannelExtension->AdapterExtension) &&
(cdb != NULL) && (cdb->CDB10.OperationCode == SCSIOP_INQUIRY)) {
if (IsDumpResumeMode(ChannelExtension->AdapterExtension) &&
(Srb->SrbStatus == SRB_STATUS_SUCCESS) &&
IsDeviceGeneralPurposeLoggingSupported(ChannelExtension) &&
IsDeviceHybridInfoSupported(ChannelExtension)) {
//
// Read Hybrid Information log during resume, so that disk can stop self-pinning.
// In normal stack, suerfetch sends down HYBRID_FUNCTION_GET_INFO triggers the log to be read.
//
IssueReadLogExtCommand( ChannelExtension,
Srb,
IDE_GP_LOG_HYBRID_INFO_ADDRESS,
0,
1,
0, // feature field
&ChannelExtension->DeviceExtension->ReadLogExtPageDataPhysicalAddress,
(PVOID)ChannelExtension->DeviceExtension->ReadLogExtPageData,
(PSRB_COMPLETION_ROUTINE)InquiryComplete
);
} else {
InquiryComplete(ChannelExtension, Srb);
}
} else if ((ChannelExtension->DeviceExtension->DeviceParameters.StateFlags.NeedUpdateIdentifyDeviceData == 1) &&
(cdb != NULL) && (cdb->CDB10.OperationCode == SCSIOP_INQUIRY)) {
//
// We are refreshing Identify information because of a firmware update.
// The firmware update support information is contained in the Supported
// Capabilities log page so we need to make sure we query that page as well.
//
ChannelExtension->DeviceExtension->DeviceParameters.StateFlags.NeedUpdateIdentifyDeviceData = 0;
//
// Finish processing the Inquiry command before re-using the SRB to get
// the Supported Capabilities log page.
//
InquiryComplete(ChannelExtension, Srb);
if (IsDeviceGeneralPurposeLoggingSupported(ChannelExtension)) {
GetDownloadMicrocodeSupport(ChannelExtension, Srb);
}
}
return;
}
VOID
AhciPortNVCacheCompletion(
_In_ PAHCI_CHANNEL_EXTENSION ChannelExtension,
_In_ PSTORAGE_REQUEST_BLOCK Srb
)
{
PSRB_IO_CONTROL srbControl;
PNVCACHE_REQUEST_BLOCK nRB;
PATA_TASK_FILE TaskFile;
PAHCI_SRB_EXTENSION srbExtension = GetSrbExtension(Srb);
UNREFERENCED_PARAMETER(ChannelExtension);
srbControl = (PSRB_IO_CONTROL)SrbGetDataBuffer(Srb);
nRB = ((PNVCACHE_REQUEST_BLOCK) ( (PSRB_IO_CONTROL) srbControl + 1) ); //26015: "Potential overflow using expression 'nRB->NRBStatus' Buffer access is apparently unbounded by the buffer size.
// Return status success indicating that the request was handled by the device.
nRB->NRBStatus = NRB_SUCCESS;
TaskFile = (PATA_TASK_FILE)srbExtension->ResultBuffer;
if ( TaskFile != NULL ) {
nRB->NVCacheStatus = TaskFile->Current.bCommandReg;
if (TaskFile->Current.bCommandReg & 1) { // command failed
nRB->NVCacheSubStatus = TaskFile->Current.bFeaturesReg;
}
nRB->Count = (TaskFile->Current.bSectorCountReg << 8) +
(TaskFile->Previous.bSectorCountReg);
nRB->LBA = (ULONGLONG) TaskFile->Previous.bCylHighReg;
nRB->LBA <<= 8;
nRB->LBA += (ULONGLONG) TaskFile->Previous.bCylLowReg;
nRB->LBA <<= 8;
nRB->LBA += (ULONGLONG) TaskFile->Previous.bSectorNumberReg;
nRB->LBA <<= 8;
nRB->LBA += (ULONGLONG) TaskFile->Current.bCylHighReg;
nRB->LBA <<= 8;
nRB->LBA += (ULONGLONG) TaskFile->Current.bCylLowReg;
nRB->LBA <<= 8;
nRB->LBA += (ULONGLONG) TaskFile->Current.bSectorNumberReg;
//
// Free the buffer allocated as mode sense info buffer , holding task file
//
AhciFreeDmaBuffer(ChannelExtension->AdapterExtension, srbExtension->ResultBufferLength, TaskFile, srbExtension->ResultBufferPhysicalAddress);
} else {
// in case TaskFile is not returned in SenseInfoBuffer, use cached ATA Status and Error register values.
if (Srb->SrbStatus == SRB_STATUS_SUCCESS) {
// command succeeded
nRB->NVCacheStatus = 0;
nRB->NVCacheSubStatus = 0;
} else {
// command failed
nRB->NVCacheStatus = srbExtension->AtaStatus;
nRB->NVCacheSubStatus = srbExtension->AtaError;
}
}
return;
}
VOID
AhciPortSmartCompletion(
_In_ PAHCI_CHANNEL_EXTENSION ChannelExtension,
_In_ PSTORAGE_REQUEST_BLOCK Srb
)
{
PSENDCMDOUTPARAMS outParams;
PAHCI_SRB_EXTENSION srbExtension;
PUCHAR buffer;//to make the pointer arithmatic easier
UNREFERENCED_PARAMETER(ChannelExtension);
buffer = (PUCHAR)SrbGetDataBuffer(Srb) + sizeof(SRB_IO_CONTROL);
outParams = (PSENDCMDOUTPARAMS) buffer; //26015: "Potential overflow using expression 'outParams->DriverStatus.bDriverError' Buffer access is apparently unbounded by the buffer size.
srbExtension = GetSrbExtension(Srb);
Srb->SrbStatus &= ~SRB_STATUS_AUTOSENSE_VALID; // remove this flag as there is no data copy back to original Sense Buffer
if (Srb->SrbStatus == SRB_STATUS_SUCCESS) {
outParams->DriverStatus.bDriverError = 0;
outParams->DriverStatus.bIDEError = 0;
// RETURN_SMART_STATUS does not perform data transfer but copies the registers.
if (srbExtension->TaskFile.Current.bFeaturesReg == RETURN_SMART_STATUS) {
outParams->cBufferSize = sizeof(ATAREGISTERS);
} else {
outParams->cBufferSize = srbExtension->DataTransferLength;
}
} else {
// command failed
outParams->DriverStatus.bDriverError = SMART_IDE_ERROR;
outParams->DriverStatus.bIDEError = srbExtension->AtaStatus;
outParams->cBufferSize = 0;
}
return;
}
__inline
VOID
BuildLocalCommand(
_In_ PAHCI_CHANNEL_EXTENSION ChannelExtension,
_In_ PATA_TASK_FILE TaskFile,
_In_opt_ PSRB_COMPLETION_ROUTINE CompletionRountine
)
/*++
It assumes:
nothing
Called by:
IssuePreservedSettingCommands
It performs:
1 Fills in the local SRB with the ATA command
Affected Variables/Registers:
none
--*/
{
PSCSI_REQUEST_BLOCK srb;
PAHCI_SRB_EXTENSION srbExtension;
//
// Local Srb still uses SCSI_REQUEST_BLOCK type.
// do not touch field "srb->NextSrb". It should be only touched in queue related operations.
//
srb = &ChannelExtension->Local.Srb;
srb->SrbStatus = SRB_STATUS_PENDING;
srb->SrbExtension = (PVOID)ChannelExtension->Local.SrbExtension;
srb->TimeOutValue = 1; //as it's sent by miniport, no one monitors the timeout value.
// Fills in the local SRB with the SetFeatures command
srbExtension = ChannelExtension->Local.SrbExtension;
AhciZeroMemory((PCHAR)srbExtension, sizeof(AHCI_SRB_EXTENSION));
srbExtension->AtaFunction = ATA_FUNCTION_ATA_COMMAND;
srbExtension->CompletionRoutine = CompletionRountine;
//setup TaskFile
StorPortCopyMemory(&srbExtension->TaskFile, TaskFile, sizeof(ATA_TASK_FILE));
if (LogExecuteFullDetail(ChannelExtension->AdapterExtension->LogFlags)) {
RecordExecutionHistory(ChannelExtension, 0x1000001d);//Exit BuildLocalCommand
}
return;
}
VOID
IssuePreservedSettingCommands(
_In_ PAHCI_CHANNEL_EXTENSION ChannelExtension,
_In_opt_ PSTORAGE_REQUEST_BLOCK Srb
)
/*++
Uses the local SRB to send down the next Preserved Setting
It assumes:
Local SRB is only used for restoring preserved settings
Called by:
RestorePreservedSettings,
IssueInitCommands,
Itself indirectly through local SRB callback
It performs:
1 Verify local SRB is not in use
2 Find the next Preserved Setting
3 Send it
Affected Variables/Registers:
none
--*/
{
UCHAR i;
ULONG allocated;
ATA_TASK_FILE taskFile = {0};
UNREFERENCED_PARAMETER(Srb);
RecordExecutionHistory(ChannelExtension, 0x00000043); // IssuePreservedSettingCommands
//1 Verify local SRB is not in use
allocated = GetOccupiedSlots(ChannelExtension);
if ((allocated & (1 << 0)) > 0) {
// Already restoring preserved Settings
RecordExecutionHistory(ChannelExtension, 0x10010043); // IssuePreservedSettingCommands Slot 0 in use
return;
}
//2 find the next command to send
for (i = 0; i < MAX_SETTINGS_PRESERVED; i++) {
if ( (ChannelExtension->PersistentSettings.SlotsToSend & (1 << i)) > 0 ) {
ChannelExtension->PersistentSettings.SlotsToSend &= ~(1 << i);
break;
}
}
// Perhaps there is none. Done.
if ( i >= MAX_SETTINGS_PRESERVED) {
// Release active reference for process of restore preserved settings
if (ChannelExtension->StateFlags.RestorePreservedSettingsActiveReferenced == 1) {
PortReleaseActiveReference(ChannelExtension, NULL);
ChannelExtension->StateFlags.RestorePreservedSettingsActiveReferenced = 0;
}
RecordExecutionHistory(ChannelExtension, 0x10020043); // IssuePreservedSettingCommands done and clear flag
InterlockedBitTestAndReset((LONG*)&ChannelExtension->StateFlags, 3); //ReservedSlotInUse field is at bit 3
InterlockedBitTestAndReset((LONG*)&ChannelExtension->StateFlags, 22); //PowerUpInitializationInProgress field is at bit 22
return;
}
//3 Otherwise use the LocalSRB to send the command. When it is done, call this routine again
taskFile.Current.bFeaturesReg = ChannelExtension->PersistentSettings.CommandParams[i].Features;
taskFile.Current.bSectorCountReg = ChannelExtension->PersistentSettings.CommandParams[i].SectorCount;
taskFile.Current.bDriveHeadReg = 0xA0;
taskFile.Current.bCommandReg = IDE_COMMAND_SET_FEATURE;
BuildLocalCommand(ChannelExtension, &taskFile, IssuePreservedSettingCommands);
return;
}
VOID
IssueInitCommands(
_In_ PAHCI_CHANNEL_EXTENSION ChannelExtension,
_In_opt_ PSTORAGE_REQUEST_BLOCK Srb
)
/*++
Uses the local SRB to send down the next Init Command or Preserved Setting Command
It assumes:
Local SRB is only used for restoring preserved settings
Called by:
AhciIssueInitCommands,
Itself indirectly through local SRB callback
It performs:
1 Verify local SRB is not in use
2 Find the next Init Command or Preserved Setting Command
3 Send it
Affected Variables/Registers:
none
--*/
{
ULONG allocated;
PATA_TASK_FILE taskFile;
UNREFERENCED_PARAMETER(Srb);
RecordExecutionHistory(ChannelExtension, 0x00000044); // IssueInitCommands
// Verify local SRB is not in use
allocated = GetOccupiedSlots(ChannelExtension);
if ((allocated & (1 << 0)) > 0) {
// Already restoring preserved Settings
RecordExecutionHistory(ChannelExtension, 0x10010044); // IssueInitCommands slot 0 in use
return;
}
// if all Init commands have been sent, send Preserved Setting Commands
if (ChannelExtension->DeviceInitCommands.CommandToSend >= ChannelExtension->DeviceInitCommands.ValidCommandCount) {
RecordExecutionHistory(ChannelExtension, 0x10020044); // IssueInitCommands init commands done, send Preserved Setting Commands
ChannelExtension->PersistentSettings.SlotsToSend = ChannelExtension->PersistentSettings.Slots;
IssuePreservedSettingCommands(ChannelExtension, NULL);
return;
}
// find the next command to send
taskFile = ChannelExtension->DeviceInitCommands.CommandTaskFile + ChannelExtension->DeviceInitCommands.CommandToSend;
taskFile->Current.bDriveHeadReg = 0xA0;
//
// If this is a PUIS spin-up command and a spin-up is *not* needed then
// skip to the next command. This command is not needed if:
// * PUIS is not enabled; or
// * The device doesn't support PUIS, is a hybrid, or is an SSD; or
// * The device is currently powered up (not in D3)
//
if (taskFile->Current.bFeaturesReg == IDE_FEATURE_PUIS_SPIN_UP &&
(ChannelExtension->StateFlags.PuisEnabled == FALSE ||
NeedsPuisSpinUpOnPowerUp(ChannelExtension) == FALSE ||
ChannelExtension->DevicePowerState != StorPowerDeviceD3)) {
ChannelExtension->DeviceInitCommands.CommandToSend++;
taskFile = ChannelExtension->DeviceInitCommands.CommandTaskFile + ChannelExtension->DeviceInitCommands.CommandToSend;
taskFile->Current.bDriveHeadReg = 0xA0;
}
BuildLocalCommand(ChannelExtension, taskFile, IssueInitCommands);
ChannelExtension->DeviceInitCommands.CommandToSend++;
return;
}
VOID
SetDateAndTimeCompletion(
_In_ PAHCI_CHANNEL_EXTENSION ChannelExtension,
_In_ PSTORAGE_REQUEST_BLOCK Srb
)
{
PAHCI_SRB_EXTENSION srbExtension = GetSrbExtension(Srb);
UNREFERENCED_PARAMETER(ChannelExtension);
AhciZeroMemory((PCHAR)srbExtension, sizeof(AHCI_SRB_EXTENSION));
srbExtension->AtaFunction = ATA_FUNCTION_ATA_COMMAND;
srbExtension->CompletionRoutine = NULL;
SetCommandReg((&srbExtension->TaskFile.Current), IDE_COMMAND_STANDBY_IMMEDIATE);
return;
}
VOID
BuildSetDateAndTimeTaskFile(
_In_ PATA_TASK_FILE TaskFile
)
{
LARGE_INTEGER temp;
ULONGLONG now;
AhciZeroMemory((PCHAR)TaskFile, sizeof(ATA_TASK_FILE));
//setup TaskFile
StorPortQuerySystemTime(&temp);
now = (ULONGLONG) temp.QuadPart;
now /= 10000; //4 orders of magnitude
// 2) subtract 369 years in seconds.
// Number of milliseconds in a Julian year = 31,557,600,000 (1millisecond * 1000second * 60minute * 60hour * 24day * 365.25year)
// 369 * 31,557,600,000 = 11,644,754,400,000 (0xA97 4173 1300)
now -= 0xA9741731300;
// Example 2010-09-29 10 am = 0x1cb5ffd`22c1bf5e
// 0x1cb5ffd`22c1bf5e/10000 = 0xbc2`8f496393 (12930255512467 or 12930255512467.8494) milliseconds
// 0xbc2`8f496393 - 0xa97'41731300 = 0x012b`4dd65093
// NOTE: this number won't roll over for another ~8700 years.
TaskFile->Current.bSectorNumberReg = (UCHAR) (0xFF & now);
now >>= 8;
TaskFile->Current.bCylLowReg = (UCHAR) (0xFF & now);
now >>= 8;
TaskFile->Current.bCylHighReg = (UCHAR) (0xFF & now);
now >>= 8;
TaskFile->Previous.bSectorNumberReg = (UCHAR) (0xFF & now);
now >>= 8;
TaskFile->Previous.bCylLowReg = (UCHAR) (0xFF & now);
now >>= 8;
TaskFile->Previous.bCylHighReg = (UCHAR) (0xFF & now);
TaskFile->Current.bDriveHeadReg = 0xA0;
TaskFile->Current.bCommandReg = IDE_COMMAND_SET_DATE_AND_TIME;
return;
}
VOID
IssueSetDateAndTimeCommand(
_In_ PAHCI_CHANNEL_EXTENSION ChannelExtension,
_Inout_ PSCSI_REQUEST_BLOCK Srb,
_In_ BOOLEAN SendStandBy
)
/*++
It assumes:
Srb is not the local SRB.
Called by:
AhciHwStartIo with SRB_FUNCTION_SHUTDOWN
It performs:
1 Builds a Set Date & Time taskfile and associates it with the provided Srb.
Affected Variables/Registers:
none
--*/
{
PAHCI_SRB_EXTENSION srbExtension = GetSrbExtension((PSTORAGE_REQUEST_BLOCK)Srb);
NT_ASSERT(Srb != &ChannelExtension->Local.Srb);
UNREFERENCED_PARAMETER(ChannelExtension);
//setup TaskFile
BuildSetDateAndTimeTaskFile(&srbExtension->TaskFile);
srbExtension->AtaFunction = ATA_FUNCTION_ATA_COMMAND;
srbExtension->CompletionRoutine = (SendStandBy ? SetDateAndTimeCompletion : NULL);
}
BOOLEAN
AhciDeviceInitialize (
_In_ PAHCI_CHANNEL_EXTENSION ChannelExtension
)
{
RecordExecutionHistory(ChannelExtension, 0x00000007); //AhciDeviceInitialize
//1 update preserved commands per device needs.
if (IsAtaDevice(&ChannelExtension->DeviceExtension->DeviceParameters)) {
if (NeedToSetTransferMode(ChannelExtension)) {
//1.1 Set DMA mode to this device
UpdateSetFeatureCommands(ChannelExtension, IDE_FEATURE_INVALID, IDE_FEATURE_SET_TRANSFER_MODE, 0, 0x44);
}
//1.2 Persist Write Cache
UpdateSetFeatureCommands(ChannelExtension, IDE_FEATURE_INVALID, IDE_FEATURE_ENABLE_WRITE_CACHE, 0, 0);
} else if (IsAtapiDevice(&ChannelExtension->DeviceExtension->DeviceParameters)) {
//2.1 Persist SATA transfer mode for some SATAI/PATAPI bridge chips
UpdateSetFeatureCommands(ChannelExtension, IDE_FEATURE_INVALID, IDE_FEATURE_SET_TRANSFER_MODE, 0, 0x42);
if ( IsDeviceSupportsAN(ChannelExtension->DeviceExtension->IdentifyPacketData) &&
!IsDeviceEnabledAN(ChannelExtension->DeviceExtension->IdentifyPacketData) ) {
//2.2 Enable Asynchronous Notification if supported
UpdateSetFeatureCommands(ChannelExtension, IDE_FEATURE_INVALID, IDE_FEATURE_ENABLE_SATA_FEATURE, 0, IDE_SATA_FEATURE_ASYNCHRONOUS_NOTIFICATION);
}
}
//
// Enable Power Up in Standby (PUIS) on hybrids if it's supported and not
// already enabled.
//
if (IsDeviceHybridInfoSupported(ChannelExtension) &&
ChannelExtension->DeviceExtension[0].IdentifyDeviceData->CommandSetSupport.PowerUpInStandby &&
ChannelExtension->DeviceExtension[0].IdentifyDeviceData->CommandSetActive.PowerUpInStandby == FALSE) {
UpdateSetFeatureCommands(ChannelExtension, IDE_FEATURE_DISABLE_PUIS, IDE_FEATURE_ENABLE_PUIS, 0, 0);
ChannelExtension->StateFlags.PuisEnabled = TRUE;
}
//3.1 evaluate ACPI _SDD method informing information about device connected.
AhciPortEvaluateSDDMethod(ChannelExtension);
//3.2 retrieve _GTF commands and add needed commands in list.
AhciPortGetInitCommands(ChannelExtension);
//5.1 Configure device with init commands and persistent configuration commands
AhciPortIssueInitCommands(ChannelExtension);
ActivateQueue(ChannelExtension, FALSE);
RecordExecutionHistory(ChannelExtension, 0x10000007);//Exit AhciDeviceInitialize
return TRUE;
}
VOID
AhciDeviceStart (
_In_ PAHCI_CHANNEL_EXTENSION ChannelExtension
)
/*
Running at PASSIVE_LEVEL
This function is called when IRP_MN_START_DEVICE is being processed.
device registry access, ACPI calls can be processed in this function.
*/
{
if (ChannelExtension == NULL) {
return;
}
AhciDeviceInitialize(ChannelExtension);
return;
}
__inline
BOOLEAN
IsLpmModeSetting(
_In_ PSTOR_POWER_SETTING_INFO PowerInfo
)
{
if (PowerInfo->PowerSettingGuid.Data1 == 0x0b2d69d7) {
if (PowerInfo->PowerSettingGuid.Data2 == 0xa2a1){
if (PowerInfo->PowerSettingGuid.Data3 == 0x449c){
if (PowerInfo->PowerSettingGuid.Data4[0] == 0x96){
if (PowerInfo->PowerSettingGuid.Data4[1] == 0x80){
if (PowerInfo->PowerSettingGuid.Data4[2] == 0xf9){
if (PowerInfo->PowerSettingGuid.Data4[3] == 0x1c){
if (PowerInfo->PowerSettingGuid.Data4[4] == 0x70){
if (PowerInfo->PowerSettingGuid.Data4[5] == 0x52){
if (PowerInfo->PowerSettingGuid.Data4[6] == 0x1c){
if (PowerInfo->PowerSettingGuid.Data4[7] == 0x60){
return TRUE;
} } } } } } } } } } }
return FALSE;
}
__inline
BOOLEAN
IsLpmAdaptiveSetting(
_In_ PSTOR_POWER_SETTING_INFO PowerInfo
)
{
if (PowerInfo->PowerSettingGuid.Data1 == 0xDAB60367) {
if (PowerInfo->PowerSettingGuid.Data2 == 0x53FE){
if (PowerInfo->PowerSettingGuid.Data3 == 0x4fbc){
if (PowerInfo->PowerSettingGuid.Data4[0] == 0x82){
if (PowerInfo->PowerSettingGuid.Data4[1] == 0x5E){
if (PowerInfo->PowerSettingGuid.Data4[2] == 0x52){
if (PowerInfo->PowerSettingGuid.Data4[3] == 0x1D){
if (PowerInfo->PowerSettingGuid.Data4[4] == 0x06){
if (PowerInfo->PowerSettingGuid.Data4[5] == 0x9D){
if (PowerInfo->PowerSettingGuid.Data4[6] == 0x24){
if (PowerInfo->PowerSettingGuid.Data4[7] == 0x56){
return TRUE;
} } } } } } } } } } }
return FALSE;
}
UCHAR
SetAllowedLpmStates(
_In_ PAHCI_CHANNEL_EXTENSION ChannelExtension
)
// Return Value: disabled modes;
{
UCHAR lpm;
AHCI_SERIAL_ATA_CONTROL sctl;
// 0h No interface restrictions
// 1h Transitions to the Partial state disabled
// 2h Transitions to the Slumber state disabled
// 3h Transitions to both Partial and Slumber states disabled
// disable LPM for eSATA port as hot-plug cannot be detected in partial or slumber state.
if ((ChannelExtension->LastUserLpmPowerSetting == 0) ||
!IsLPMCapablePort(ChannelExtension)) {
lpm = 0x03; // slumber and partial disallowed
} else {
AHCI_COMMAND cmd;
cmd.AsUlong = StorPortReadRegisterUlong(ChannelExtension->AdapterExtension, &ChannelExtension->Px->CMD.AsUlong);
if ((ChannelExtension->AutoPartialToSlumberInterval == 0) && // No software auto partial to slumber
( (ChannelExtension->AdapterExtension->CAP2.APST == 0) || // Host auto Partial to Slumber is not supported.
(cmd.APSTE == 0) ) ) { // Host auto Partial to Slumber is not enabled.
lpm = 0x02; // partial allowed; slumber disallowed
} else {
lpm = 0x00; // partial allowed; slumber allowed
}
}
if (ChannelExtension->AdapterExtension->CAP.SSC == 0) {
// disable Slumber if controller does not support it.
lpm |= 0x02;
}
if (ChannelExtension->AdapterExtension->CAP.PSC == 0) {
// storahci LPM is to put device into partial, then transit into slumber according to defined interval value.
// do not enable LPM if partial is not supported.
// the case of device supporting slumber but not partial is very rare.
lpm = 0x03;
}
//Set PxSCTL.IPM to 3h to restrict slumber and partial interface power management state transitions.
sctl.AsUlong = StorPortReadRegisterUlong(ChannelExtension->AdapterExtension, &ChannelExtension->Px->SCTL.AsUlong);
sctl.IPM = lpm;
StorPortWriteRegisterUlong(ChannelExtension->AdapterExtension, &ChannelExtension->Px->SCTL.AsUlong, sctl.AsUlong);
return lpm;
}
BOOLEAN
AhciLpmSettingsModes(
_In_ PAHCI_CHANNEL_EXTENSION ChannelExtension,
_In_ AHCI_LPM_POWER_SETTINGS LpmMode
)
/*
NOTE: this routine may prepared command in Local.Srb. Caller of this routine should try to start IO process.
Return Value:
TRUE: the caller should start IO process
*/
{
AHCI_COMMAND cmd;
BOOLEAN needStartIo = FALSE;
UCHAR sctlIpm = 0;
RecordExecutionHistory(ChannelExtension, 0x00000047); // AhciLpmSettingsModes
//Make sure the configuration supports LPM, otherwise, don't touch anything.
if (NoLpmSupport(ChannelExtension) || !IsLPMCapablePort(ChannelExtension)) {
return needStartIo;
}
ChannelExtension->LastUserLpmPowerSetting = (UCHAR)LpmMode.AsUlong;
if (LpmMode.AsUlong == 0) {
// Active Mode.
//Turn LPM off as Active is chosen
if (ChannelExtension->AdapterExtension->CAP.SALP == 1) {
cmd.AsUlong = StorPortReadRegisterUlong(ChannelExtension->AdapterExtension, &ChannelExtension->Px->CMD.AsUlong);
if (cmd.ALPE != 0) {
cmd.ALPE = 0;
cmd.ASP = 0;
StorPortWriteRegisterUlong(ChannelExtension->AdapterExtension, &ChannelExtension->Px->CMD.AsUlong, cmd.AsUlong);
}
}
//Set PxSCTL.IPM to 3h to restrict slumber and partial interface power management state transitions.
sctlIpm = SetAllowedLpmStates(ChannelExtension);
// Disable DIPM
if (IsDeviceSupportsDIPM(ChannelExtension->DeviceExtension[0].IdentifyDeviceData)) {
//The enable/disable state for device initiated power management shall persist across software reset.
//The enable/disable state shall be reset to its default disabled state upon COMRESET.
UpdateSetFeatureCommands(ChannelExtension,
IDE_FEATURE_ENABLE_SATA_FEATURE,
IDE_FEATURE_DISABLE_SATA_FEATURE,
IDE_SATA_FEATURE_DEVICE_INITIATED_POWER_MANAGEMENT,
IDE_SATA_FEATURE_DEVICE_INITIATED_POWER_MANAGEMENT);
//Configure device with persistent configuration commands
RestorePreservedSettings(ChannelExtension, FALSE);
needStartIo = TRUE;
}
} else {
// link power management is allowed.
//Set PxSCTL.IPM for LPM allowed states.
sctlIpm = SetAllowedLpmStates(ChannelExtension);
// Setting HIPM if it's enabled.
if ( (LpmMode.HipmEnabled > 0) &&
(sctlIpm != 0x03) &&
(ChannelExtension->AdapterExtension->CAP.SALP == 1) &&
IsDeviceSupportsHIPM(ChannelExtension->DeviceExtension[0].IdentifyDeviceData) ) {
// If Partial is capable and device supports HIPM.
// Turn on LPM and set it for Partial
cmd.AsUlong = StorPortReadRegisterUlong(ChannelExtension->AdapterExtension, &ChannelExtension->Px->CMD.AsUlong);
cmd.ALPE = 1;
cmd.ASP = 0; //0 = partial, 1 = slumber
StorPortWriteRegisterUlong(ChannelExtension->AdapterExtension, &ChannelExtension->Px->CMD.AsUlong, cmd.AsUlong);
} else if (ChannelExtension->AdapterExtension->CAP.SALP == 1) {
//Turn off HIPM if it's supported
cmd.AsUlong = StorPortReadRegisterUlong(ChannelExtension->AdapterExtension, &ChannelExtension->Px->CMD.AsUlong);
if (cmd.ALPE != 0) {
cmd.ALPE = 0;
cmd.ASP = 0;
StorPortWriteRegisterUlong(ChannelExtension->AdapterExtension, &ChannelExtension->Px->CMD.AsUlong, cmd.AsUlong);
}
}
// Setting DIPM if it's enabled.
if ((LpmMode.DipmEnabled > 0) &&
(sctlIpm != 0x03)) {
// Enable DIPM feature.
if (IsDeviceSupportsDIPM(ChannelExtension->DeviceExtension[0].IdentifyDeviceData)) {
//The enable/disable state for device initiated power management shall persist across software reset.
//The enable/disable state shall be reset to its default disabled state upon COMRESET.
UpdateSetFeatureCommands(ChannelExtension,
IDE_FEATURE_DISABLE_SATA_FEATURE,
IDE_FEATURE_ENABLE_SATA_FEATURE,
IDE_SATA_FEATURE_DEVICE_INITIATED_POWER_MANAGEMENT,
IDE_SATA_FEATURE_DEVICE_INITIATED_POWER_MANAGEMENT);
//Configure device with persistent configuration commands
RestorePreservedSettings(ChannelExtension, FALSE);
needStartIo = TRUE;
}
} else {
// Disable DIPM feature.
if (IsDeviceSupportsDIPM(ChannelExtension->DeviceExtension[0].IdentifyDeviceData)) {
//The enable/disable state for device initiated power management shall persist across software reset.
//The enable/disable state shall be reset to its default disabled state upon COMRESET.
UpdateSetFeatureCommands(ChannelExtension,
IDE_FEATURE_ENABLE_SATA_FEATURE,
IDE_FEATURE_DISABLE_SATA_FEATURE,
IDE_SATA_FEATURE_DEVICE_INITIATED_POWER_MANAGEMENT,
IDE_SATA_FEATURE_DEVICE_INITIATED_POWER_MANAGEMENT);
//Configure device with persistent configuration commands
RestorePreservedSettings(ChannelExtension, FALSE);
needStartIo = TRUE;
}
}
}
++(ChannelExtension->TotalCountPowerSettingNotification);
AhciTelemetryLogPowerSettingChange(ChannelExtension,
(PSTOR_ADDRESS)&(ChannelExtension->DeviceExtension->DeviceAddress),
AhciTelemetryEventIdLpmSettingsModes,
"AhciLpmSettingsModes",
0,
sctlIpm,
LpmMode.AsUlong
);
RecordExecutionHistory(ChannelExtension, 0x10000047); // AhciLpmSettingsModes Exit
return needStartIo;
}
BOOLEAN
AhciPortPowerSettingNotification(
IN PAHCI_CHANNEL_EXTENSION ChannelExtension,
IN PSTOR_POWER_SETTING_INFO PowerInfo
)
{
// do nothing if there is no device connected
if ( (ChannelExtension->StartState.ChannelNextStartState == StartFailed) ||
(ChannelExtension->DeviceExtension->DeviceParameters.AtaDeviceType == DeviceNotExist) ) {
return FALSE;
}
//Make sure the configuration supports LPM, otherwise, don't touch anything.
if (NoLpmSupport(ChannelExtension) || !IsLPMCapablePort(ChannelExtension)) {
return FALSE;
}
// Validate input LPM data from the Power Manager
if (PowerInfo->ValueLength != sizeof(ULONG)) {
return FALSE;
}
if (!IsLpmModeSetting(PowerInfo) &&
!IsLpmAdaptiveSetting(PowerInfo)) {
// invalid power policy.
return FALSE;
}
if (IsLpmAdaptiveSetting(PowerInfo)) {
// max allowed value: 5 minutes (in ms)
ULONG interval = (ULONG)*((PULONG)PowerInfo->Value);
if (interval <= 300000) {
UCHAR sctlIpm = 0;
ChannelExtension->AutoPartialToSlumberInterval = interval;
//Set PxSCTL.IPM register for LPM allowed states.
sctlIpm = SetAllowedLpmStates(ChannelExtension);
++(ChannelExtension->TotalCountPowerSettingNotification);
AhciTelemetryLogPowerSettingChange(ChannelExtension,
(PSTOR_ADDRESS)&(ChannelExtension->DeviceExtension->DeviceAddress),
AhciTelemetryEventIdLpmAdaptiveSetting,
"LpmAdaptiveSetting",
0,
sctlIpm,
ChannelExtension->AutoPartialToSlumberInterval
);
}
} else if (IsLpmModeSetting(PowerInfo)) {
BOOLEAN needRestartIo;
AHCI_LPM_POWER_SETTINGS userLpmPowerSettings;
userLpmPowerSettings.AsUlong = (ULONG)*((PULONG)PowerInfo->Value);
needRestartIo = AhciLpmSettingsModes(ChannelExtension, userLpmPowerSettings);
if (needRestartIo) {
ActivateQueue(ChannelExtension, FALSE);
}
}
return TRUE;
}
VOID
AhciAutoPartialToSlumber(
_In_ PVOID AdapterExtension,
_In_opt_ PVOID ChannelExtension
)
/*
NOTE: input parameter - Context is required as this is a callback function. But it's not used by this function.
*/
{
PAHCI_CHANNEL_EXTENSION channelExtension = (PAHCI_CHANNEL_EXTENSION)ChannelExtension;
AHCI_SERIAL_ATA_STATUS ssts;
AHCI_COMMAND cmd;
ULONG ci;
ULONG sact;
if (channelExtension == NULL) {
NT_ASSERT(FALSE);
return;
}
if (channelExtension->Px == NULL) {
// The port has been stopped. Do not touch its registers.
// There is no need to transit the link power state to Slumber state.
//
// Note:
// Px is set to NULL in AhciPortStop function. StartIo spin lock is utilized to
// prevent race condition with AhciPortStop function. StartIo spin lock is acquired
// before AhciPortStop is called. When we are here in AhciAutoPartialToSlumber, because
// it is a timer callback function, StartIo spin lock is already held - Storport holds
// StartIo spin lock before invoking miniport timer callback function.
//
return;
}
NT_ASSERT(AdapterExtension == (PVOID)(channelExtension->AdapterExtension));
UNREFERENCED_PARAMETER(AdapterExtension);
// 1.1 check the Link Power State should be enabled.
cmd.AsUlong = StorPortReadRegisterUlong(channelExtension->AdapterExtension, &channelExtension->Px->CMD.AsUlong);
ci = StorPortReadRegisterUlong(AdapterExtension, &channelExtension->Px->CI);
sact = StorPortReadRegisterUlong(AdapterExtension, &channelExtension->Px->SACT);
if (((ci | sact) != 0) ||
!PartialToSlumberTransitionIsAllowed(channelExtension, &cmd)) {
// validate again in case any condition changed that not allowing StorAHCI to perform Partial to Slumber transition.
StorPortDebugPrint(3, "StorAHCI - LPM: Port %02d - Transit into Slumber from Partial - bailed out, request outstanding: CI: 0x%08X, SACT: 0x%08X \n", channelExtension->PortNumber, ci, sact);
return;
}
ssts.AsUlong = StorPortReadRegisterUlong(channelExtension->AdapterExtension, &channelExtension->Px->SSTS.AsUlong);
// 1.3 check the Link Power State, should be Partial (value 2).
if (ssts.IPM != 2) {
StorPortDebugPrint(3, "StorAHCI - LPM: Port %02d - Transit into Slumber from Partial - bailed out, current link state is not Partial: %1x \n", channelExtension->PortNumber, ssts.IPM);
return;
}
// 2. Change LPM State.
// Link should be in idle state (able to accept new interface commands).
if (cmd.ICC == 0) {
ULONG waitTime;
ULONG waitTimeLimit = AHCI_LINK_POWER_STATE_CHANGE_TIMEOUT_US;
UCHAR iccAttempts = 0;
AhciUlongIncrement(&(channelExtension->AutoPartialToSlumberDbgStats.InterfaceReady));
//
// Attempt to transition the link to Active.
// By spec, Partial to Active transition should be completed in 10us. Reading register already takes sometime.
// Poll for a little bit to give the link some time to go Active.
//
cmd.ICC = 1;
StorPortWriteRegisterUlong(channelExtension->AdapterExtension, &channelExtension->Px->CMD.AsUlong, cmd.AsUlong);
ssts.AsUlong = StorPortReadRegisterUlong(channelExtension->AdapterExtension, &channelExtension->Px->SSTS.AsUlong);
for (waitTime = 0; (waitTime < waitTimeLimit) && (ssts.IPM != 1); waitTime += 10) {
//
// Make a few attempts to program ICC if we haven't transitioned yet.
//
if (iccAttempts++ < 3) {
cmd.ICC = 1;
StorPortWriteRegisterUlong(channelExtension->AdapterExtension, &channelExtension->Px->CMD.AsUlong, cmd.AsUlong);
}
StorPortStallExecution(10); //10 microseconds
ssts.AsUlong = StorPortReadRegisterUlong(channelExtension->AdapterExtension, &channelExtension->Px->SSTS.AsUlong);
}
if (ssts.IPM != 1) {
AhciUlongIncrement(&(channelExtension->AutoPartialToSlumberDbgStats.ActiveFailCount));
StorPortDebugPrint(3, "StorAHCI - LPM: Port %02d - Transit into Slumber from Partial - Failed to go to Active, SSTS.IPM = %u \n", channelExtension->PortNumber, ssts.IPM);
return;
}
AhciUlongIncrement(&(channelExtension->AutoPartialToSlumberDbgStats.ActiveSuccessCount));
//
// Attempt to transition the link to Slumber.
// Poll for a little bit to give the link some time to go Slumber.
//
iccAttempts = 0;
cmd.ICC = 6;
StorPortWriteRegisterUlong(channelExtension->AdapterExtension, &channelExtension->Px->CMD.AsUlong, cmd.AsUlong);
ssts.AsUlong = StorPortReadRegisterUlong(channelExtension->AdapterExtension, &channelExtension->Px->SSTS.AsUlong);
for (waitTime = 0; (waitTime < waitTimeLimit) && (ssts.IPM != 6); waitTime += 10) {
//
// Make a few attempts to program ICC if we haven't transitioned yet.
//
if (iccAttempts++ < 3) {
cmd.ICC = 6;
StorPortWriteRegisterUlong(channelExtension->AdapterExtension, &channelExtension->Px->CMD.AsUlong, cmd.AsUlong);
}
StorPortStallExecution(10); //10 microseconds
ssts.AsUlong = StorPortReadRegisterUlong(channelExtension->AdapterExtension, &channelExtension->Px->SSTS.AsUlong);
}
if (ssts.IPM == 6) {
AhciUlongIncrement(&(channelExtension->AutoPartialToSlumberDbgStats.SlumberSuccessCount));
StorPortDebugPrint(3, "StorAHCI - LPM: Port %02d - Transit into Slumber from Partial - Succeeded \n", channelExtension->PortNumber);
} else {
AhciUlongIncrement(&(channelExtension->AutoPartialToSlumberDbgStats.SlumberFailCount));
StorPortDebugPrint(3, "StorAHCI - LPM: Port %02d - Transit into Slumber from Partial - Failed, SSTS.IPM = %u \n", channelExtension->PortNumber, ssts.IPM);
}
} else {
AhciUlongIncrement(&(channelExtension->AutoPartialToSlumberDbgStats.InterfaceNotReady));
}
return;
}
BOOLEAN
AhciAdapterPowerSettingNotification(
_In_ PAHCI_ADAPTER_EXTENSION AdapterExtension,
_In_ PSTOR_POWER_SETTING_INFO PowerSettingInfo
)
{
ULONG i;
for (i = 0; i <= AdapterExtension->HighestPort; i++) {
if (AdapterExtension->PortExtension[i] != NULL) {
AhciPortPowerSettingNotification(AdapterExtension->PortExtension[i], PowerSettingInfo);
}
}
return TRUE;
}
VOID
AhciPortGetInitCommands(
_In_ PAHCI_CHANNEL_EXTENSION ChannelExtension
)
{
// Read _GTF from ACPI
ULONG status = STOR_STATUS_SUCCESS;
ACPI_EVAL_INPUT_BUFFER inputData = {0};
PACPI_EVAL_OUTPUT_BUFFER acpiData = NULL;
PACPI_METHOD_ARGUMENT argument = NULL;
ULONG acpiDataSize = 256; // initial size, should be good enough for most cases
ULONG returnedLength = 0;
UCHAR gtfCommandCount = 0;
// send SECURE_FREEZE_LOCK by default
BOOLEAN sendSecureFreezeLock = TRUE;
// clear Init Commands area. need to do this for device removed previously (StorAHCI only knows about adapter removal, not device removal)
ChannelExtension->DeviceInitCommands.CommandCount = 0;
ChannelExtension->DeviceInitCommands.ValidCommandCount = 0;
ChannelExtension->DeviceInitCommands.CommandToSend = 0;
if (ChannelExtension->DeviceInitCommands.CommandTaskFile != NULL) {
StorPortFreePool(ChannelExtension->AdapterExtension, (PVOID)ChannelExtension->DeviceInitCommands.CommandTaskFile);
ChannelExtension->DeviceInitCommands.CommandTaskFile = NULL;
}
inputData.Signature = ACPI_EVAL_INPUT_BUFFER_SIGNATURE;
inputData.MethodNameAsUlong = ACPI_METHOD_GTF;
status = StorPortAllocatePool(ChannelExtension->AdapterExtension,
acpiDataSize,
AHCI_POOL_TAG,
(PVOID*)&acpiData);
if (acpiData != NULL) {
// call API to get required buffer size
status = StorPortInvokeAcpiMethod(ChannelExtension->AdapterExtension,
(PSTOR_ADDRESS)&ChannelExtension->DeviceExtension[0].DeviceAddress,
ACPI_METHOD_GTF,
&inputData,
sizeof(ACPI_EVAL_INPUT_BUFFER),
(PVOID)acpiData,
acpiDataSize,
&returnedLength
);
// in case of the allocate buffer is too small, re-allocate buffer and retry the call
if ( (status == STOR_STATUS_BUFFER_TOO_SMALL) && (acpiData->Length > acpiDataSize) ) {
acpiDataSize = acpiData->Length;
StorPortFreePool(ChannelExtension->AdapterExtension, (PVOID)acpiData);
acpiData = NULL;
// re-allocate a bigger buffer
status = StorPortAllocatePool(ChannelExtension->AdapterExtension,
acpiDataSize,
AHCI_POOL_TAG,
(PVOID*)&acpiData);
if (acpiData != NULL) {
status = StorPortInvokeAcpiMethod(ChannelExtension->AdapterExtension,
(PSTOR_ADDRESS)&ChannelExtension->DeviceExtension[0].DeviceAddress,
ACPI_METHOD_GTF,
&inputData,
sizeof(ACPI_EVAL_INPUT_BUFFER),
(PVOID)acpiData,
acpiDataSize,
&returnedLength
);
}
}
}
// get _GTF commands count
if ( (status == STOR_STATUS_SUCCESS) &&
(acpiData != NULL) &&
(acpiData->Signature == ACPI_EVAL_OUTPUT_BUFFER_SIGNATURE) &&
(acpiData->Count == 1) ) {
argument = acpiData->Argument;
if (argument->Type == ACPI_METHOD_ARGUMENT_BUFFER) {
NT_ASSERT ((argument->DataLength % sizeof(ACPI_GTF_IDE_REGISTERS)) == 0);
gtfCommandCount = (UCHAR)(argument->DataLength / sizeof(ACPI_GTF_IDE_REGISTERS));
} else {
NT_ASSERT(argument->Type == ACPI_METHOD_ARGUMENT_BUFFER);
}
}
// Get Init Command count for devices
//
// calculate possible command count for memory allocation
// an IDE_FEATURE_DISABLE_REVERT_TO_POWER_ON command will be sent to device anyway.
//
ChannelExtension->DeviceInitCommands.CommandCount = gtfCommandCount + 1;
if (IsAtaDevice(&ChannelExtension->DeviceExtension[0].DeviceParameters)) {
if (ChannelExtension->DeviceExtension->SupportedCommands.SetDateAndTime == 0x1) {
ChannelExtension->DeviceInitCommands.CommandCount++;
}
if (sendSecureFreezeLock) {
ChannelExtension->DeviceInitCommands.CommandCount++;
}
if (NeedsPuisSpinUpOnPowerUp(ChannelExtension)) {
ChannelExtension->DeviceInitCommands.CommandCount++;
}
}
// copy _GTF commands into buffer
if (ChannelExtension->DeviceInitCommands.CommandCount > 0) {
PATA_TASK_FILE taskFile;
ULONG i = 0, gtfIndex = 0;
status = StorPortAllocatePool(ChannelExtension->AdapterExtension,
ChannelExtension->DeviceInitCommands.CommandCount * sizeof(ATA_TASK_FILE),
AHCI_POOL_TAG,
(PVOID*)&ChannelExtension->DeviceInitCommands.CommandTaskFile);
if ( (status != STOR_STATUS_SUCCESS) || (ChannelExtension->DeviceInitCommands.CommandTaskFile == NULL) ) {
goto exit;
}
AhciZeroMemory((PCHAR)ChannelExtension->DeviceInitCommands.CommandTaskFile, ChannelExtension->DeviceInitCommands.CommandCount * sizeof(ATA_TASK_FILE));
//
// If we may need to send the PUIS spin-up command to this device later
// then insert it here. It needs to be first so that the drive is spun
// up and subsequent commands can succeed.
//
if (NeedsPuisSpinUpOnPowerUp(ChannelExtension) &&
(ChannelExtension->DeviceInitCommands.CommandCount - i) >= 1) {
taskFile = ChannelExtension->DeviceInitCommands.CommandTaskFile + i;
taskFile->Current.bCommandReg = IDE_COMMAND_SET_FEATURE;
taskFile->Current.bFeaturesReg = IDE_FEATURE_PUIS_SPIN_UP;
i++;
}
//
// Now copy over the _GTF commands.
//
for (gtfIndex = 0; gtfIndex < gtfCommandCount; gtfIndex++, i++) {
StorPortCopyMemory(ChannelExtension->DeviceInitCommands.CommandTaskFile + i,
argument->Data + (gtfIndex * sizeof(ACPI_GTF_IDE_REGISTERS)),
sizeof(ACPI_GTF_IDE_REGISTERS)
);
}
// add IDE_FEATURE_DISABLE_REVERT_TO_POWER_ON for all devices
if ((ChannelExtension->DeviceInitCommands.CommandCount - i) >= 1) {
taskFile = ChannelExtension->DeviceInitCommands.CommandTaskFile + i;
taskFile->Current.bFeaturesReg = IDE_FEATURE_DISABLE_REVERT_TO_POWER_ON;
taskFile->Current.bCommandReg = IDE_COMMAND_SET_FEATURE;
i++;
}
// add more commands for ATA devices
if (IsAtaDevice(&ChannelExtension->DeviceExtension[0].DeviceParameters)) {
if (sendSecureFreezeLock) {
if ((ChannelExtension->DeviceInitCommands.CommandCount - i) >= 1) {
taskFile = ChannelExtension->DeviceInitCommands.CommandTaskFile + i;
taskFile->Current.bCommandReg = IDE_COMMAND_SECURITY_FREEZE_LOCK;
i++;
}
}
if ((ChannelExtension->DeviceInitCommands.CommandCount - i) >= 1) {
if (ChannelExtension->DeviceExtension->SupportedCommands.SetDateAndTime == 0x1) {
taskFile = ChannelExtension->DeviceInitCommands.CommandTaskFile + i;
//setup TaskFile
BuildSetDateAndTimeTaskFile(taskFile);
i++;
}
}
}
ChannelExtension->DeviceInitCommands.ValidCommandCount = (UCHAR)i;
}
exit:
if (acpiData != NULL) {
StorPortFreePool(ChannelExtension->AdapterExtension, (PVOID)acpiData);
acpiData = NULL;
}
return;
}
VOID
AhciPortEvaluateSDDMethod(
_In_ PAHCI_CHANNEL_EXTENSION ChannelExtension
)
{
ULONG status = STOR_STATUS_SUCCESS;
ULONG returnedLength = 0;
PACPI_EVAL_INPUT_BUFFER_COMPLEX inputData;
PACPI_METHOD_ARGUMENT argument;
ULONG inputDataSize;
// get the memory we need
inputDataSize = sizeof(ACPI_EVAL_INPUT_BUFFER_COMPLEX) + sizeof(IDENTIFY_DEVICE_DATA);
status = StorPortAllocatePool(ChannelExtension->AdapterExtension,
inputDataSize,
AHCI_POOL_TAG,
(PVOID*)&inputData);
if ( (status != STOR_STATUS_SUCCESS) || (inputData == NULL) ) {
goto Exit;
}
AhciZeroMemory((PCHAR)inputData, inputDataSize);
inputData->Signature = ACPI_EVAL_INPUT_BUFFER_COMPLEX_SIGNATURE;
inputData->MethodNameAsUlong = ACPI_METHOD_SDD;
inputData->Size = inputDataSize;
inputData->ArgumentCount = 1;
argument = inputData->Argument;
argument->Type = ACPI_METHOD_ARGUMENT_BUFFER;
argument->DataLength = sizeof(IDENTIFY_DEVICE_DATA);
StorPortCopyMemory(argument->Data, ChannelExtension->DeviceExtension[0].IdentifyDeviceData, sizeof(IDENTIFY_DEVICE_DATA));
status = StorPortInvokeAcpiMethod(ChannelExtension->AdapterExtension,
(PSTOR_ADDRESS)&ChannelExtension->DeviceExtension[0].DeviceAddress,
ACPI_METHOD_SDD,
(PVOID)inputData,
inputDataSize,
NULL,
0,
&returnedLength
);
Exit:
// we don't care about the return status
UNREFERENCED_PARAMETER(status);
if (inputData != NULL) {
StorPortFreePool(ChannelExtension->AdapterExtension, inputData);
}
return;
}
VOID
AhciAdapterEvaluateDSMMethod(
_In_ PAHCI_ADAPTER_EXTENSION AdapterExtension
)
{
ULONG status = STOR_STATUS_SUCCESS;
ULONG returnedLength = 0;
PACPI_METHOD_ARGUMENT argument;
PACPI_EVAL_INPUT_BUFFER_COMPLEX inputData = NULL;
ULONG inputDataSize;
PACPI_EVAL_OUTPUT_BUFFER outputData = NULL;
ULONG outputDataSize;
// 0. get output buffer ready, make sure the buffer is big enough.
outputDataSize = FIELD_OFFSET(ACPI_EVAL_OUTPUT_BUFFER, Argument) +
AHCI_MAX_PORT_COUNT * ACPI_METHOD_ARGUMENT_LENGTH(sizeof(ULONG));
status = StorPortAllocatePool(AdapterExtension,
outputDataSize,
AHCI_POOL_TAG,
(PVOID*)&outputData);
if ( (status != STOR_STATUS_SUCCESS) || (outputData == NULL) ) {
goto Exit;
}
AhciZeroMemory((PCHAR)outputData, outputDataSize);
// 1. check if Link Power Management is supported in ACPI
// get the memory we need
// N.B. 4 arguments are stored in the ACPI_EVAL_INPUT_BUFFER_COMPLEX
// and passed to acpi.sys to eval _DSM.
//
// ACPI_EVAL_INPUT_BUFFER_COMPLEX with 4 arguments.
// 0 - GUID
// 1 - ULONG (revision id)
// 2 - ULONG (function index)
// 3 - unused (package)
//
inputDataSize = FIELD_OFFSET(ACPI_EVAL_INPUT_BUFFER_COMPLEX, Argument) +
ACPI_METHOD_ARGUMENT_LENGTH(sizeof(GUID)) +
ACPI_METHOD_ARGUMENT_LENGTH(sizeof(ULONG)) +
ACPI_METHOD_ARGUMENT_LENGTH(sizeof(ULONG)) +
ACPI_METHOD_ARGUMENT_LENGTH(sizeof(ULONG));
status = StorPortAllocatePool(AdapterExtension,
inputDataSize,
AHCI_POOL_TAG,
(PVOID*)&inputData);
if ( (status != STOR_STATUS_SUCCESS) || (inputData == NULL) ) {
goto Exit;
}
AhciZeroMemory((PCHAR)inputData, inputDataSize);
inputData->Signature = ACPI_EVAL_INPUT_BUFFER_COMPLEX_SIGNATURE;
inputData->MethodNameAsUlong = ACPI_METHOD_DSM;
inputData->Size = inputDataSize;
inputData->ArgumentCount = 4;
// argument 0 - Interface GUID
argument = &inputData->Argument[0];
argument->Type = ACPI_METHOD_ARGUMENT_BUFFER;
argument->DataLength = sizeof(GUID);
StorPortCopyMemory(&argument->Data[0], &LINK_POWER_ACPI_DSM_GUID, sizeof(GUID));
// argument 1 - Revision number
argument = ACPI_METHOD_NEXT_ARGUMENT(argument);
ACPI_METHOD_SET_ARGUMENT_INTEGER(argument,
ACPI_METHOD_DSM_LINKPOWER_REVISION);
// argument 2 - Function Index
argument = ACPI_METHOD_NEXT_ARGUMENT(argument);
ACPI_METHOD_SET_ARGUMENT_INTEGER(argument,
ACPI_METHOD_DSM_LINKPOWER_FUNCTION_SUPPORT);
// argument 3 - Function-dependent package. not used for ACPI_METHOD_DSM_LINKPOWER_FUNCTION_SUPPORT
argument = ACPI_METHOD_NEXT_ARGUMENT(argument);
ACPI_METHOD_SET_ARGUMENT_INTEGER(argument, 0);
argument->Type = ACPI_METHOD_ARGUMENT_PACKAGE;
status = StorPortInvokeAcpiMethod(AdapterExtension,
NULL, // NULL for Address field means the request is for Adapter
ACPI_METHOD_DSM,
(PVOID)inputData,
inputDataSize,
outputData,
outputDataSize,
&returnedLength
);
if ( (status != STOR_STATUS_SUCCESS) ||
(returnedLength < (FIELD_OFFSET(ACPI_EVAL_OUTPUT_BUFFER, Argument) + ACPI_METHOD_ARGUMENT_LENGTH(sizeof(ULONG)))) ) {
goto Exit;
}
argument = outputData->Argument;
if ( ((argument->Argument & ACPI_METHOD_DSM_LINKPOWER_FUNCTION_QUERY) != 0) &&
((argument->Argument & ACPI_METHOD_DSM_LINKPOWER_FUNCTION_CONTROL) != 0) ) {
// If ACPI_METHOD_DSM_LINKPOWER_FUNCTION_CONTROL is supported, it supports value (-1) that apply action to all ports.
// Mark adapter supports _DSM. This indicates that it has capability to power on all ports and connected devices.
if (AdapterExtension->StateFlags.SupportsAcpiDSM != TRUE) {
AdapterExtension->StateFlags.SupportsAcpiDSM = TRUE;
}
}
Exit:
if (inputData != NULL) {
StorPortFreePool(AdapterExtension, inputData);
}
if (outputData != NULL) {
StorPortFreePool(AdapterExtension, outputData);
}
return;
}
VOID
AhciPortAcpiDSMControl(
_In_ PAHCI_ADAPTER_EXTENSION AdapterExtension,
_In_ ULONG PortNumber,
_In_ BOOLEAN Sleep
)
{
ULONG status = STOR_STATUS_SUCCESS;
ULONG returnedLength = 0;
PACPI_EVAL_INPUT_BUFFER_COMPLEX inputData;
PACPI_METHOD_ARGUMENT argument;
ULONG inputDataSize;
// get the memory we need
inputDataSize = FIELD_OFFSET(ACPI_EVAL_INPUT_BUFFER_COMPLEX, Argument) +
ACPI_METHOD_ARGUMENT_LENGTH(sizeof(GUID)) +
ACPI_METHOD_ARGUMENT_LENGTH(sizeof(ULONG)) +
ACPI_METHOD_ARGUMENT_LENGTH(sizeof(ULONG)) +
FIELD_OFFSET(ACPI_METHOD_ARGUMENT, Argument) +
2* ACPI_METHOD_ARGUMENT_LENGTH(sizeof(ULONG));
status = StorPortAllocatePool(AdapterExtension,
inputDataSize,
AHCI_POOL_TAG,
(PVOID*)&inputData);
if ( (status != STOR_STATUS_SUCCESS) || (inputData == NULL) ) {
goto Exit;
}
AhciZeroMemory((PCHAR)inputData, inputDataSize);
inputData->Signature = ACPI_EVAL_INPUT_BUFFER_COMPLEX_SIGNATURE;
inputData->MethodNameAsUlong = ACPI_METHOD_DSM;
inputData->Size = inputDataSize;
inputData->ArgumentCount = 4;
// argument 0 - Interface GUID
argument = &inputData->Argument[0];
argument->Type = ACPI_METHOD_ARGUMENT_BUFFER;
argument->DataLength = sizeof(GUID);
StorPortCopyMemory(&argument->Data[0], &LINK_POWER_ACPI_DSM_GUID, sizeof(GUID));
// argument 1 - Revision number
argument = ACPI_METHOD_NEXT_ARGUMENT(argument);
ACPI_METHOD_SET_ARGUMENT_INTEGER(argument,
ACPI_METHOD_DSM_LINKPOWER_REVISION);
// argument 2 - Function Index
argument = ACPI_METHOD_NEXT_ARGUMENT(argument);
ACPI_METHOD_SET_ARGUMENT_INTEGER(argument,
ACPI_METHOD_DSM_LINKPOWER_FUNCTION_CONTROL);
// argument 3 - Function-dependent package.
argument = ACPI_METHOD_NEXT_ARGUMENT(argument);
argument->Type = ACPI_METHOD_ARGUMENT_PACKAGE_EX;
argument->DataLength = 2 * ACPI_METHOD_ARGUMENT_LENGTH(sizeof(ULONG));
// argument 3 - Package entry 0
argument = (PACPI_METHOD_ARGUMENT)argument->Data;
if (PortNumber == (ULONG)-1) {
// all 1s indicates power on operation is for all ports/devices
NT_ASSERT(Sleep == FALSE);
ACPI_METHOD_SET_ARGUMENT_INTEGER(argument, PortNumber);
} else {
// convert PortNumber to be ACPI format of Address
ACPI_METHOD_SET_ARGUMENT_INTEGER(argument, (PortNumber << 16) | 0xFFFF);
}
// argument 3 - Package entry 1
argument = ACPI_METHOD_NEXT_ARGUMENT(argument);
ACPI_METHOD_SET_ARGUMENT_INTEGER(argument, Sleep ? 0 : 1);
status = StorPortInvokeAcpiMethod(AdapterExtension,
NULL, // NULL for Address field means the request is for Adapter
ACPI_METHOD_DSM,
(PVOID)inputData,
inputDataSize,
NULL,
0,
&returnedLength
);
Exit:
UNREFERENCED_PARAMETER(status);
if (inputData != NULL) {
StorPortFreePool(AdapterExtension, inputData);
}
return;
}
#pragma warning(pop) // un-sets any local warning changes
|