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
|
/*++
Copyright (C) Microsoft Corporation, 2009
Module Name:
io.c
Abstract:
This file contains the core logic for programming and completing IO commands into an AHCI controller
Notes:
Revision History:
--*/
#if _MSC_VER >= 1200
#pragma warning(push)
#endif
#pragma warning(disable:4214) // bit field types other than int
#pragma warning(disable:4201) // nameless struct/union
#include "generic.h"
ULONG
GetSlotToActivate(
_In_ PAHCI_CHANNEL_EXTENSION ChannelExtension,
_In_ ULONG TargetSlots
)
/*++
Select Slot to activate based on maximum slot number allowed
Called by:
Activate Queue
It performs:
Affected Variables/Registers:
none
Return Value:
--*/
{
UCHAR activeCount = 0;
UCHAR emptyCount;
UCHAR requestCount;
UCHAR lastActiveSlot;
ULONG slotToActivate = 0;
UCHAR i;
// 1. Device's queue depth is smaller than Controller's
NT_ASSERT(ChannelExtension->DeviceExtension[0].DeviceParameters.MaxDeviceQueueDepth <= ChannelExtension->AdapterExtension->CAP.NCS);
// Count the number of slots already in use
if (ChannelExtension->SlotManager.CommandsIssued > 0) {
activeCount = NumberOfSetBits(ChannelExtension->SlotManager.CommandsIssued);
}
// 1.1 Check if all slots are active.
if (activeCount >= ChannelExtension->DeviceExtension[0].DeviceParameters.MaxDeviceQueueDepth) {
// If all possible slots are full, no matter what, return no work (0)
return 0;
}
// 2. Look for any entry from last active slot
requestCount = NumberOfSetBits(TargetSlots);
lastActiveSlot = ChannelExtension->LastActiveSlot;
emptyCount = ChannelExtension->DeviceExtension[0].DeviceParameters.MaxDeviceQueueDepth - activeCount;
// 3.1 Look for any entry from last active slot
for (i = lastActiveSlot; i <= ChannelExtension->AdapterExtension->CAP.NCS; i++) {
if ((TargetSlots & (1 << i)) > 0) {
slotToActivate |= (1 << i);
emptyCount--;
requestCount--;
if (emptyCount == 0 || requestCount == 0) {
ChannelExtension->LastActiveSlot = i;
return slotToActivate;
}
}
}
// 3.2 Look for any entry from beginning to last active slot
// Slot 0 is reserved for internal command
for (i = 1 ; i <= lastActiveSlot; i++) {
if ((TargetSlots & (1 << i)) > 0) {
slotToActivate |= (1 << i);
emptyCount--;
requestCount--;
if (emptyCount == 0 || requestCount == 0) {
ChannelExtension->LastActiveSlot = i;
return slotToActivate;
}
}
}
NT_ASSERT(FALSE);
return slotToActivate;
}
UCHAR
GetSingleIo(
PAHCI_CHANNEL_EXTENSION ChannelExtension
)
/*++
Selects an IO circularly from the programmed Single IO slice starting with the slot after the most recently programmed slot
This ensures the issuing of the SingleIO is FIFO
It assumes:
Single IO are never high priority
Called by:
Activate Queue
It performs:
(overview)
1 Initialize
2 Chose the slot
(details)
1.1 Initialize variables
2.1 Chose the slot circularly starting with CCS
Affected Variables/Registers:
none
Return Value:
Slot number of the oldest programmed Single IO Slice slot
If no slots are programmed the tag returned is 0xFF
--*/
{
UCHAR limit;
UCHAR i;
PAHCI_ADAPTER_EXTENSION adapterExtension = ChannelExtension->AdapterExtension;
// 1.1 Initialize variables
limit = ChannelExtension->CurrentCommandSlot;
// if there is internal request pending, always get it first.
if ((ChannelExtension->SlotManager.SingleIoSlice & 1) > 0) {
return 0;
}
// 2.1 Chose the slot circularly starting with CCS
for (i = limit; i <= adapterExtension->CAP.NCS; i++) {
if ((ChannelExtension->SlotManager.SingleIoSlice & (1 << i)) > 0) {
return i;
}
}
for (i = 0; i < limit; i++) {
if ((ChannelExtension->SlotManager.SingleIoSlice & (1 << i)) > 0) {
return i;
}
}
return 0xff;
}
VOID
AddQueue(
_In_ PAHCI_CHANNEL_EXTENSION ChannelExtension,
_Inout_ PSTORAHCI_QUEUE Queue,
_In_ PSTORAGE_REQUEST_BLOCK Srb,
_In_ ULONG Signature,
_In_ UCHAR Tag
)
/*
Input Parameters:
ChannelExtension
Queue
Srb
Signature: used to mark the location of last Srb in history log
Tag: bit 31 ~ 24, Queue->CurrentDepth: bit 23 ~ 0
*/
{
PVOID tempTail, foundSrb;
ULONG srbsFound;
UNREFERENCED_PARAMETER(ChannelExtension);
StorPortQuerySystemTime(&(Queue->LastTimeStampAddQueue));
if (Queue->Tail == NULL) {
Queue->Tail = (PVOID) Srb;
Queue->Head = (PVOID) Srb;
} else {
tempTail = Queue->Tail;
if (SrbGetNextSrb(tempTail) == NULL) { //Verify SRBs are not about to be severed from the Queue
SrbSetNextSrb(tempTail, (PVOID)Srb);
}
Queue->Tail = (PVOID)Srb;
}
Queue->CurrentDepth++;
if (Queue->Head == NULL) { //It is impossible for Head to be NULL here
srbsFound = 0;
} else {
foundSrb = Queue->Head;
srbsFound = 1;
foundSrb = SrbGetNextSrb(foundSrb);
while (foundSrb) {
srbsFound++;
foundSrb = SrbGetNextSrb(foundSrb);
}
}
Queue->DepthHistory[Queue->DepthHistoryIndex] = ( (Tag << 24) | Queue->CurrentDepth );
Queue->DepthHistoryIndex++;
Queue->DepthHistoryIndex %= 100;
Queue->DepthHistory[Queue->DepthHistoryIndex] = Signature;
if (Queue->CurrentDepth > Queue->DeepestDepth) {
Queue->DeepestDepth = Queue->CurrentDepth;
}
return;
}
PSTORAGE_REQUEST_BLOCK
RemoveQueue(
_In_ PAHCI_CHANNEL_EXTENSION ChannelExtension,
_Inout_ PSTORAHCI_QUEUE Queue,
_In_ ULONG Signature,
_In_ UCHAR Tag
)
{
PVOID nextSrb, foundSrb;
ULONG srbsFound;
UNREFERENCED_PARAMETER(ChannelExtension);
// Check to see if the queue is empty
if (Queue->Head == NULL) {
return NULL;
}
StorPortQuerySystemTime(&(Queue->LastTimeStampRemoveQueue));
//if it is not empty, pop
nextSrb = Queue->Head;
Queue->Head = SrbGetNextSrb(nextSrb);
SrbSetNextSrb(nextSrb, NULL); //This should have been NULL to start with, return it to it's NULL state
if (Queue->Head == NULL) {
Queue->Tail = NULL;
}
Queue->CurrentDepth--;
if (Queue->Head == NULL) {
srbsFound = 0;
} else {
foundSrb = Queue->Head;
srbsFound = 1;
foundSrb = SrbGetNextSrb(foundSrb);
while (foundSrb) {
srbsFound++;
foundSrb = SrbGetNextSrb(foundSrb);
}
}
Queue->DepthHistory[Queue->DepthHistoryIndex] = ( (Tag << 24) | Queue->CurrentDepth );
Queue->DepthHistoryIndex++;
Queue->DepthHistoryIndex %= 100;
Queue->DepthHistory[Queue->DepthHistoryIndex] = Signature;
if (Queue->CurrentDepth > Queue->DeepestDepth) {
Queue->DeepestDepth = Queue->CurrentDepth;
}
return (PSTORAGE_REQUEST_BLOCK)nextSrb;
}
BOOLEAN
ActivateQueue(
_In_ PAHCI_CHANNEL_EXTENSION ChannelExtension,
_In_ BOOLEAN AtDIRQL
)
/*++
It assumes:
Called by:
It performs:
(overview)
1. Ensures the channel is ready for programming
2. Program the next IO according to the priority scheme
(details)
1.1 Initialize variables
1.2 If the programming should not happen now, leave, ActivateQueue will be called again when these conditions are changed
2.1 Choose the Queue with which to program the controller
Algorithm:
2.1.1 Single IO SRBs (including Request Sense and non data control commands) have highest priority.
2.1.2 When there are no Single IO commands, Normal IO get the next highest priority
2.1.3 When there are no Single or Normal commands, NCQ commands get the next highest priority
2.1.4 In the case that no IO is present in any Slices, program nothing
2.2 Program all the IO from the chosen queue into the controller
Affected Variables/Registers:
channelExtension
CI
SACT
Return Values:
--*/
{
BOOLEAN status = TRUE;
ULONG slotsToActivate = 0;
BOOLEAN activateNcq = FALSE;
int i;
PAHCI_ADAPTER_EXTENSION adapterExtension = ChannelExtension->AdapterExtension;
STOR_LOCK_HANDLE lockhandle = { InterruptLock, 0 };
// 1.1 Initialize variables
if (LogExecuteFullDetail(adapterExtension->LogFlags)) {
RecordExecutionHistory(ChannelExtension, 0x00000022);//ActivateQueue
}
// If the programming should not happen now, leave
if (ChannelExtension->StartState.ChannelNextStartState != StartComplete) {
RecordExecutionHistory(ChannelExtension, 0x10010022);//ActivateQueue, Channel Not Yet Started
status = FALSE;
return status;
}
if (ChannelExtension->StateFlags.QueuePaused == TRUE) {
RecordExecutionHistory(ChannelExtension, 0x10020022);//ActivateQueue, Channel Queue Paused
status = FALSE;
return status;
}
if (!IsPortStartCapable(ChannelExtension)) {
RecordExecutionHistory(ChannelExtension, 0x10030022);//ActivateQueue, Channel Not Start Capable
status = FALSE;
return status;
}
if (ErrorRecoveryIsPending(ChannelExtension)) {
RecordExecutionHistory(ChannelExtension, 0x10070022);//ActivateQueue, Error Recovery is pending.
status = FALSE;
return status;
}
if (!AtDIRQL) {
AhciInterruptSpinlockAcquire(ChannelExtension->AdapterExtension, ChannelExtension->PortNumber, &lockhandle);
}
// 1.1.1 If there is no command to program, leave
if ((ChannelExtension->SlotManager.SingleIoSlice == 0) &&
(ChannelExtension->SlotManager.NormalQueueSlice == 0) &&
(ChannelExtension->SlotManager.NCQueueSlice == 0)) {
if (LogExecuteFullDetail(adapterExtension->LogFlags)) {
RecordExecutionHistory(ChannelExtension, 0x10040022);//ActivateQueue, No Commands to program
}
status = FALSE;
goto Exit;
}
if ((ChannelExtension->StateFlags.ReservedSlotInUse == 1) &&
((ChannelExtension->SlotManager.SingleIoSlice & 1) == 0)) {
//In case of device initialization is in process but the local command is not ready yet, do not program other commands.
if (LogExecuteFullDetail(ChannelExtension->AdapterExtension->LogFlags)) {
RecordExecutionHistory(ChannelExtension, 0x10080022);//ActivateQueue, an init command is in process.
}
status = FALSE;
goto Exit;
}
// 2.1 Choose the Queue with which to program the controller
// 2.1.1 Single IO SRBs have highest priority.
if (ChannelExtension->SlotManager.SingleIoSlice != 0) {
if ((ChannelExtension->SlotManager.NCQueueSliceIssued | ChannelExtension->SlotManager.NormalQueueSliceIssued | ChannelExtension->SlotManager.SingleIoSliceIssued) == 0) {
// Safely get Single IO in round robin fashion
i = GetSingleIo(ChannelExtension);
if (i != 0xff) {
PSLOT_CONTENT slotContent = &ChannelExtension->Slot[i];
slotsToActivate = (1 << i);
ChannelExtension->SlotManager.SingleIoSlice &= ~slotsToActivate;
ChannelExtension->SlotManager.SingleIoSliceIssued |= slotsToActivate;
ChannelExtension->StateFlags.QueuePaused = TRUE; //and pause the queue so no other IO get programmed
if (LogExecuteFullDetail(adapterExtension->LogFlags)) {
RecordExecutionHistory(ChannelExtension, 0x10090022); //ActivateQueue, Single IO pause queue
}
// 2.1.1.1 Update the Identify command for enumeration. This needs to be here for PxSIG to be accessible.
// Accessing PxSIG register here to avoid invalid values from time when the port is being reset or not started, or other scenarios.
if (slotContent->Srb != NULL) {
PAHCI_SRB_EXTENSION srbExtension = GetSrbExtension(slotContent->Srb);
PAHCI_COMMAND_TABLE cmdTable = (PAHCI_COMMAND_TABLE)srbExtension;
if ((srbExtension->AtaFunction == ATA_FUNCTION_ATA_IDENTIFY) &&
(srbExtension->CompletionRoutine == AhciPortIdentifyDevice) &&
(srbExtension->TaskFile.Current.bCommandReg == IDE_COMMAND_NOT_VALID) &&
(cmdTable->CFIS.Command == IDE_COMMAND_NOT_VALID) ) {
// For enumeration command, it's safe now to check the PxSIG register as starting port has been executed.
ULONG sig = 0;
sig = StorPortReadRegisterUlong(ChannelExtension->AdapterExtension, &ChannelExtension->Px->SIG.AsUlong);
if (sig == ATA_DEVICE_SIGNATURE_ATA) { //ATA
srbExtension->TaskFile.Current.bCommandReg = IDE_COMMAND_IDENTIFY;
cmdTable->CFIS.Command = IDE_COMMAND_IDENTIFY;
} else { //ATAPI
srbExtension->TaskFile.Current.bCommandReg = IDE_COMMAND_ATAPI_IDENTIFY;
cmdTable->CFIS.Command = IDE_COMMAND_ATAPI_IDENTIFY;
}
}
} else {
NT_ASSERT(FALSE);
}
}
}
// 2.1.2 When there are no Single IO commands, Normal IO get the next highest priority
} else if (ChannelExtension->SlotManager.NormalQueueSlice != 0) {
// Normal commands can not be sent when NCQ commands are outstanding. When the NCQ commands complete ActivateQueue will get called again.
if (ChannelExtension->SlotManager.NCQueueSliceIssued == 0) {
// Grab the High Priority Normal IO before the Low Priority Normal IO
slotsToActivate = ChannelExtension->SlotManager.HighPriorityAttribute & ChannelExtension->SlotManager.NormalQueueSlice;
// If there aren't any High Priority, grab everything else
if (slotsToActivate > 0) {
ChannelExtension->SlotManager.NormalQueueSlice &= ~slotsToActivate;
} else {
slotsToActivate = ChannelExtension->SlotManager.NormalQueueSlice;
ChannelExtension->SlotManager.NormalQueueSlice = 0;
}
ChannelExtension->SlotManager.NormalQueueSliceIssued |= slotsToActivate;
}
// 2.1.3 When there are no Single or Normal commands, NCQ commands get the next highest priority
} else if ((ChannelExtension->SlotManager.NCQueueSlice != 0) &&
(ChannelExtension->StateFlags.NcqErrorRecoveryInProcess == 0)) {
// NCQ commands can not be sent when Normal commands are outstanding. When the Normal commands complete, Activate Queue will get called again.
if ((ChannelExtension->SlotManager.SingleIoSliceIssued | ChannelExtension->SlotManager.NormalQueueSliceIssued ) != 0) {
slotsToActivate = 0;
} else {
// Grab the High Priority NCQ IO before the Low Priority NCQ IO
slotsToActivate = ChannelExtension->SlotManager.HighPriorityAttribute & ChannelExtension->SlotManager.NCQueueSlice;
// If there aren't any High Priority, grab everything else
if (slotsToActivate == 0) {
slotsToActivate = ChannelExtension->SlotManager.NCQueueSlice;
}
// And apply any device outstanding IO limits to filter down which IO to activate
if (slotsToActivate > 0) {
if (ChannelExtension->DeviceExtension[0].DeviceParameters.MaxDeviceQueueDepth < ChannelExtension->MaxPortQueueDepth) {
// Get allowed slots if the device queue depth is less than the port can support.
slotsToActivate = GetSlotToActivate(ChannelExtension, slotsToActivate);
}
if (slotsToActivate > 0) {
// And if there are any IO still selected, clear them from the NCQueue
activateNcq = TRUE; //Remember to program SACT for these commands
ChannelExtension->SlotManager.NCQueueSlice &= ~slotsToActivate;
ChannelExtension->SlotManager.NCQueueSliceIssued |= slotsToActivate;
// The selected IO will be activated at the end of this function
}
}
}
// 2.1.4 In the case that no IO is present in any Slices, program nothing
}
// 2.1 Program all the IO from the chosen queue into the controller
if (slotsToActivate != 0) {
// 2.2 Get command start time
if (adapterExtension->TracingEnabled) {
LARGE_INTEGER perfCounter = {0};
ULONG pendingProgrammingCommands = slotsToActivate;
i = 0;
StorPortQueryPerformanceCounter((PVOID)adapterExtension, NULL, &perfCounter);
while (pendingProgrammingCommands) {
// TODO: there is potential race condition that causes slot flags mismatch and Srb is NULL here.
if ((pendingProgrammingCommands & 1) && (ChannelExtension->Slot[i].Srb != NULL)) {
PAHCI_SRB_EXTENSION srbExtension = GetSrbExtension(ChannelExtension->Slot[i].Srb);
srbExtension->StartTime = perfCounter.QuadPart;
}
i++;
pendingProgrammingCommands >>= 1;
}
}
ChannelExtension->SlotManager.CommandsIssued |= slotsToActivate;
// Program registers
if (activateNcq) {
StorPortWriteRegisterUlong(adapterExtension, &ChannelExtension->Px->SACT, slotsToActivate);
}
StorPortWriteRegisterUlong(adapterExtension, &ChannelExtension->Px->CI, slotsToActivate);
}
if (LogExecuteFullDetail(adapterExtension->LogFlags)) {
RecordInterruptHistory(ChannelExtension,
slotsToActivate,
0,
0,
(ChannelExtension->SlotManager.SingleIoSliceIssued | ChannelExtension->SlotManager.NormalQueueSliceIssued),
ChannelExtension->SlotManager.NCQueueSliceIssued,
0x10060022); //ActivateQueue, after getting IO slices,
RecordExecutionHistory(ChannelExtension, 0x10050022);//Exit ActivateQueue
}
status = TRUE;
Exit:
if (!AtDIRQL) {
AhciInterruptSpinlockRelease(ChannelExtension->AdapterExtension, ChannelExtension->PortNumber, &lockhandle);
}
return status;
}
__inline
ULONGLONG
CalculateTimeDurationIn100ns (
_In_ ULONGLONG TimeDuration,
_In_ ULONGLONG CounterFrequency
)
{
ULONGLONG timeIn100ns = 0;
// As the counter can hold more than 20,000 years for the same power cycle,
// the situation about the counter start over again is considered an error case of inputs.
if (CounterFrequency > 0) {
// Difference between performance counters, needs to convert to 100ns.
ULONGLONG countersDiff = TimeDuration;
// Get seconds
timeIn100ns = countersDiff / CounterFrequency;
// Get milliseconds
countersDiff = (countersDiff % CounterFrequency) * 1000;
timeIn100ns *= 1000;
timeIn100ns += countersDiff / CounterFrequency;
// Get 100 nanoseconds
countersDiff = (countersDiff % CounterFrequency) * 10000;
timeIn100ns *= 10000;
timeIn100ns += countersDiff / CounterFrequency;
}
return timeIn100ns;
}
VOID
AhciCompleteIssuedSRBs(
_In_ PAHCI_CHANNEL_EXTENSION ChannelExtension,
_In_ UCHAR SrbStatus,
_In_ BOOLEAN AtDIRQL
)
/*++
This function completes a command that has been programmed into a Slot
It assumes:
The command in not running on the AHCI controller
ATAPI commands will be sent 1 at a time
Called by:
AhciHwInterrupt
AhciPortReset
AhciNonQueuedErrorRecovery
It performs:
(overview)
1 Initialize
2 Process all commands in CommandsToComplete
3 Start the next batch of commands
(details)
1.1 Initialize variables
2.1 For every command marked as completed
2.2 Set the status
2.3 Monitor to see that any NCQ commands are completing
2.4 Give the slot back
3.1 Start the next IO(s) if any
Affected Variables/Registers:
ChannelExtension->Slot[srbExtension->QueueTag].Srb
ChannelExtension->SlotManager.CommandsToComplete
--*/
{
PSLOT_CONTENT slotContent;
UCHAR i;
PAHCI_ADAPTER_EXTENSION adapterExtension;
PAHCI_SRB_EXTENSION srbExtension;
LARGE_INTEGER perfCounter = { 0 };
LARGE_INTEGER perfFrequency = { 0 };
// 1.1 Initialize variables
adapterExtension = ChannelExtension->AdapterExtension;
if (LogExecuteFullDetail(adapterExtension->LogFlags)) {
RecordExecutionHistory(ChannelExtension, 0x00000046);//AhciCompleteIssuedSRBs
}
if (adapterExtension->TracingEnabled && (ChannelExtension->SlotManager.CommandsToComplete)) {
StorPortQueryPerformanceCounter((PVOID)adapterExtension, &perfFrequency, &perfCounter);
}
// 2.1 For every command marked as completed
for (i = 0; i <= (adapterExtension->CAP.NCS); i++) {
if ((ChannelExtension->SlotManager.CommandsToComplete & (1 << i)) > 0) {
slotContent = &ChannelExtension->Slot[i];
if (slotContent->Srb == NULL) {
// This shall never happen.
// The completed slot has no SRB so it can not be completed back to Storport.
// Give back the empty slot
NT_ASSERT(FALSE);
ChannelExtension->SlotManager.CommandsToComplete &= ~(1 << i);
ChannelExtension->SlotManager.HighPriorityAttribute &= ~(1 << i);
continue;
}
if (slotContent->CmdHeader == NULL) {
// This shall never happen.
// Give back the empty slot
NT_ASSERT(FALSE);
ChannelExtension->SlotManager.CommandsToComplete &= ~(1 << i);
ChannelExtension->SlotManager.HighPriorityAttribute &= ~(1 << i);
// It is now impossible to determine if a data transfer completed correctly
slotContent->Srb->SrbStatus = SRB_STATUS_ABORTED;
AhciCompleteRequest(ChannelExtension, slotContent->Srb, AtDIRQL);
continue;
}
srbExtension = GetSrbExtension(slotContent->Srb);
// 2.1.2 Log command execution time, if it's allowed
if (adapterExtension->TracingEnabled &&
(srbExtension->StartTime != 0) &&
(perfCounter.QuadPart != 0) &&
!IsMiniportInternalSrb(ChannelExtension, slotContent->Srb)) {
ULONGLONG durationTime = CalculateTimeDurationIn100ns((perfCounter.QuadPart - srbExtension->StartTime), perfFrequency.QuadPart);
StorPortNotification(IoTargetRequestServiceTime, (PVOID)adapterExtension, durationTime, slotContent->Srb);
}
// 2.2 Set the status
if ((SrbStatus == SRB_STATUS_SUCCESS) &&
(!IsRequestSenseSrb(srbExtension->AtaFunction)) &&
(srbExtension->AtaFunction != ATA_FUNCTION_ATA_SMART) &&
(!IsNCQCommand(srbExtension)) &&
(slotContent->CmdHeader->PRDBC != RequestGetDataTransferLength(slotContent->Srb))) {
if (slotContent->CmdHeader->PRDBC < RequestGetDataTransferLength(slotContent->Srb)) {
// Buffer underrun,
RequestSetDataTransferLength(slotContent->Srb, slotContent->CmdHeader->PRDBC);
slotContent->Srb->SrbStatus = SrbStatus;
} else {
// Buffer overrun, return error
NT_ASSERT(FALSE);
slotContent->Srb->SrbStatus = SRB_STATUS_DATA_OVERRUN;
}
} else {
// If anything has set a STATUS on this SRB, honor that over the one passed in
if (slotContent->Srb->SrbStatus == SRB_STATUS_PENDING) {
slotContent->Srb->SrbStatus = SrbStatus;
}
}
// 2.3 Monitor to see that any NCQ commands are completing
if ((slotContent->Srb->SrbStatus == SRB_STATUS_SUCCESS) &&
(IsNCQCommand(srbExtension))) {
ChannelExtension->StateFlags.NCQ_Succeeded = TRUE;
}
// 2.4 Give the slot back
ReleaseSlottedCommand(ChannelExtension, i, AtDIRQL); // Request sense is handled here.
if (LogExecuteFullDetail(adapterExtension->LogFlags)) {
RecordExecutionHistory(ChannelExtension, 0x10000046);//Completed one SRB
}
}
}
// 3.1 Start the next IO(s) if any
ActivateQueue(ChannelExtension, AtDIRQL);
return;
}
VOID
SRBtoATA_CFIS(
PAHCI_CHANNEL_EXTENSION ChannelExtension,
PSLOT_CONTENT SlotContent
)
/*++
Populates CFIS structure with an ATA command in the given slot
It assumes:
The slot is empty and not being used
Called by:
AhciHwStartIo
It performs:
(overview)
1 Fills in the CFIS structure
(details)
1.1 Map SRB fields to CFIS fields
1.2 Special case mapping of NCQ
Affected Variables/Registers:
Command Table
--*/
{
PAHCI_SRB_EXTENSION srbExtension = GetSrbExtension(SlotContent->Srb);
PAHCI_COMMAND_TABLE cmdTable = (PAHCI_COMMAND_TABLE)srbExtension;
UNREFERENCED_PARAMETER(ChannelExtension);
// 1.1 Map SRB fields to CFIS fields
cmdTable->CFIS.FisType = 0x27;
cmdTable->CFIS.PMPort = 0; // StorAHCI doesn't support Port Multiplier
cmdTable->CFIS.Reserved1 = 0;
cmdTable->CFIS.C = 1;
cmdTable->CFIS.Command = srbExtension->TaskFile.Current.bCommandReg;
// 1.2 Special case mapping of NCQ
if (IsNCQCommand(srbExtension)) {
cmdTable->CFIS.Features = srbExtension->TaskFile.Current.bSectorCountReg;
cmdTable->CFIS.Features_Exp = srbExtension->TaskFile.Previous.bSectorCountReg;
cmdTable->CFIS.SectorCount = (srbExtension->QueueTag << 3);
cmdTable->CFIS.Dev_Head = 0xF & srbExtension->TaskFile.Current.bDriveHeadReg;
cmdTable->CFIS.Dev_Head |= (1 << 6);
if (SlotContent->StateFlags.FUA) {
cmdTable->CFIS.Dev_Head |= ATA_NCQ_FUA_BIT;
} else {
cmdTable->CFIS.Dev_Head &= ~ATA_NCQ_FUA_BIT;
}
} else {
cmdTable->CFIS.Features = srbExtension->TaskFile.Current.bFeaturesReg;
cmdTable->CFIS.Features_Exp = srbExtension->TaskFile.Previous.bFeaturesReg;
cmdTable->CFIS.SectorCount = srbExtension->TaskFile.Current.bSectorCountReg;
cmdTable->CFIS.SectorCount_Exp = srbExtension->TaskFile.Previous.bSectorCountReg;
cmdTable->CFIS.Dev_Head = srbExtension->TaskFile.Current.bDriveHeadReg;
}
// 1.1 Map SRB fields to CFIS fields
cmdTable->CFIS.SectorNumber = srbExtension->TaskFile.Current.bSectorNumberReg;
cmdTable->CFIS.SecNum_Exp = srbExtension->TaskFile.Previous.bSectorNumberReg;
cmdTable->CFIS.CylLow = srbExtension->TaskFile.Current.bCylLowReg;
cmdTable->CFIS.CylLow_Exp = srbExtension->TaskFile.Previous.bCylLowReg;
cmdTable->CFIS.CylHigh = srbExtension->TaskFile.Current.bCylHighReg;
cmdTable->CFIS.CylHigh_Exp = srbExtension->TaskFile.Previous.bCylHighReg;
cmdTable->CFIS.ICC = 0;
cmdTable->CFIS.Control = 0; // Device control consists of the 48bit HighOrderByte, SRST and nIEN. None apply here.
cmdTable->CFIS.Auxiliary7_0 = 0;
cmdTable->CFIS.Auxiliary15_8 = 0;
cmdTable->CFIS.Auxiliary23_16 = 0;
cmdTable->CFIS.Auxiliary31_24 = 0;
}
VOID
SRBtoATAPI_CFIS(
PAHCI_CHANNEL_EXTENSION ChannelExtension,
PSLOT_CONTENT SlotContent
)
/*++
Populates CFIS structure with an ATAPI command in the given slot
It assumes:
The slot is empty and not being used
Called by:
AhciHwStartIo
It performs:
(overview)
1 Populate the ACMD
2 Populate the PACKET command
(details)
1.1 Memcopy CDB into ACMD
2.1 Put the PACKET Command in the CFIS
2.2 Populate DMA bit properly
Affected Variables/Registers:
Command Table
--*/
{
// These are used for copying a CDB to ACMD on ATAPI commands.
ULONG dataLength;
PVOID cdb;
// 1.1 Memcopy CDB into ACMD
PAHCI_SRB_EXTENSION srbExtension = GetSrbExtension(SlotContent->Srb);
PAHCI_COMMAND_TABLE cmdTable = (PAHCI_COMMAND_TABLE)srbExtension;
UNREFERENCED_PARAMETER(ChannelExtension);
cdb = SrbGetCdb(SlotContent->Srb);
dataLength = RequestGetDataTransferLength(SlotContent->Srb);
if (cdb != NULL) {
StorPortCopyMemory((PVOID)cmdTable->ACMD, cdb, 16);
} else {
NT_ASSERT(cdb != NULL);
}
// 2.1 Put the PACKET Command in the CFIS
cmdTable->CFIS.FisType = 0x27;
cmdTable->CFIS.PMPort = 0; // StorAHCI doesn't support Port Multiplier
cmdTable->CFIS.Reserved1 = 0;
cmdTable->CFIS.C = 1;
cmdTable->CFIS.Command = IDE_COMMAND_ATAPI_PACKET; //A0 is the PACKET command
// 2.2 Populate DMA bit properly, only use it for data transfer
if (IsDmaCommand(srbExtension->Flags)) {
cmdTable->CFIS.Features = 0x1;
// If word 62 bit 15 is set to one, then the DMADIR bit in the PACKET command is required by the device for
// PACKET commands using the DMA data transfer protocol and:
// a) word 63 bits (2:0); -- MultiWordDMASupport & 0x7 == 0
// b) word 49 bit 15; -- Capabilities.InterleavedDmaSupported
// c) word 49 bit 8; and -- Capabilities.DmaSupported
// d) word 88 bits (6:0), -- UltraDMASupport & 0x7F == 0
//shall be cleared to zero.
if ((ChannelExtension->DeviceExtension[0].IdentifyPacketData->DMADIR.DMADIRBitRequired != 0) &&
(ChannelExtension->DeviceExtension[0].IdentifyPacketData->Capabilities.DmaSupported != 0) &&
(ChannelExtension->DeviceExtension[0].IdentifyPacketData->Capabilities.InterleavedDmaSupported == 0) &&
((ChannelExtension->DeviceExtension[0].IdentifyPacketData->MultiWordDMASupport & 0x7) == 0) &&
((ChannelExtension->DeviceExtension[0].IdentifyPacketData->UltraDMASupport & 0x7F) == 0)) {
// Bit 0 is DMA, bit 2 is DMADIR. (0 = transfer to the device; 4 = transfer to the host).
cmdTable->CFIS.Features |= (srbExtension->Flags & ATA_FLAGS_DATA_IN) ? 4 : 0;
}
} else {
cmdTable->CFIS.Features = 0x0;
}
// 2.1 Put the PACKET Command in the CFIS
cmdTable->CFIS.SectorNumber = 0;
cmdTable->CFIS.CylLow = (UCHAR)dataLength; //put the low byte of the in CylLow
dataLength >>= 8;
cmdTable->CFIS.CylHigh = (UCHAR)dataLength; //put the high byte in CylHigh
cmdTable->CFIS.Dev_Head = 0;
cmdTable->CFIS.SecNum_Exp = 0;
cmdTable->CFIS.CylLow_Exp = 0;
cmdTable->CFIS.CylHigh_Exp = 0;
cmdTable->CFIS.Features_Exp = 0;
cmdTable->CFIS.SectorCount = 0;
cmdTable->CFIS.SectorCount_Exp = 0;
cmdTable->CFIS.ICC = 0;
cmdTable->CFIS.Control = 0; // Device control consists of the 48bit HighOrderByte, SRST and nIEN. None apply here.
cmdTable->CFIS.Auxiliary7_0 = 0;
cmdTable->CFIS.Auxiliary15_8 = 0;
cmdTable->CFIS.Auxiliary23_16 = 0;
cmdTable->CFIS.Auxiliary31_24 = 0;
}
VOID
CfistoATA_CFIS(
_In_ PAHCI_CHANNEL_EXTENSION ChannelExtension,
_In_ PSLOT_CONTENT SlotContent
)
/*++
Populates CFIS structure with an ATA command in the given slot from a CFIS data structure.
It assumes:
The slot is empty and not being used
Called by:
AhciHwStartIo
It performs:
1 Copy CFIS structure from SrbExtension
2 Make sure common data fields value are appropriate
Affected Variables/Registers:
Command Table
--*/
{
PAHCI_SRB_EXTENSION srbExtension = GetSrbExtension(SlotContent->Srb);
PAHCI_COMMAND_TABLE cmdTable = (PAHCI_COMMAND_TABLE)srbExtension;
UNREFERENCED_PARAMETER(ChannelExtension);
//
// Copy CFIS data structure.
//
StorPortCopyMemory(&cmdTable->CFIS, &srbExtension->Cfis, sizeof(AHCI_H2D_REGISTER_FIS));
//
// When write a hiber file, the request can be retried.
// Retry the command without Hybrid Hinting info.
//
if (IsDumpHiberMode(ChannelExtension->AdapterExtension) &&
IsNCQWriteCommand(srbExtension) &&
(srbExtension->RetryCount > 0) &&
(ChannelExtension->StateFlags.HybridInfoEnabledOnHiberFile == 0)) {
cmdTable->CFIS.Auxiliary23_16 = 0;
}
//
// Set common data fields.
//
if (IsNCQCommand(srbExtension)) {
cmdTable->CFIS.Count7_0 = (srbExtension->QueueTag << 3);
}
cmdTable->CFIS.FisType = 0x27;
cmdTable->CFIS.PMPort = 0; // StorAHCI doesn't support Port Multiplier
cmdTable->CFIS.Reserved1 = 0;
cmdTable->CFIS.C = 1;
cmdTable->CFIS.Auxiliary15_8 = 0;
cmdTable->CFIS.Auxiliary31_24 = 0;
cmdTable->CFIS.ICC = 0;
cmdTable->CFIS.Control = 0; // Device control consists of the 48bit HighOrderByte, SRST and nIEN. None apply here.
}
ULONG
SRBtoPRDT(
_In_ PAHCI_CHANNEL_EXTENSION ChannelExtension,
_In_ PSLOT_CONTENT SlotContent
)
/*++
It assumes:
MDLs and ScatterGatherList entries will not violate PRDT rules
Called by:
AhciHwStartIo
It performs:
(overview)
1 Get the DataBuffer's address
2 Map SGL entries into PRDT entries
(details)
1.1 Get the ScatterGatherList
1.2 Verify that the DataBuffer is properly aligned
2.1 Map SGL entries into PRDT entries
2.2 Break up all 128K single entry IO into 2 64K IO entries
2.3 Verify that the DataLength is even
Affected Variables/Registers:
Return Values:
The number of entries generated to fill the PRDT
If the value returned is -1 the PRDT could not be built.
--*/
{
ULONG i;
PAHCI_SRB_EXTENSION srbExtension = GetSrbExtension(SlotContent->Srb);
PAHCI_COMMAND_TABLE cmdTable = (PAHCI_COMMAND_TABLE)srbExtension;
PLOCAL_SCATTER_GATHER_LIST sgl = srbExtension->Sgl;
if (sgl == NULL) {
// Return as invalid request in case of cannot get scatter gather list.
NT_ASSERT(FALSE);
return (ULONG)-1;
}
for (i = 0; i < sgl->NumberOfElements; i++) {
// 1.2 Verify that the DataBuffer is properly aligned
if ((sgl->List[i].PhysicalAddress.LowPart & 0x1) == 0) {
// 2.1 Map SGL entries into PRDT entries
if (sgl->List[i].Length != 0x20000) {
cmdTable->PRDT[i].DBA.AsUlong = sgl->List[i].PhysicalAddress.LowPart;
if (ChannelExtension->AdapterExtension->CAP.S64A) {//If the controller supports 64 bits, write the high part too
cmdTable->PRDT[i].DBAU = sgl->List[i].PhysicalAddress.HighPart;
}
// 2.2 Break up a 128K single entry IO into 2 64K IO entries (128K is max transfer so there can be only 1 in any SGL)
// although one entry can represent at max 4M length IO, some adapters cannot handle a DBC >= 128K.
} else {
// Entry 0
cmdTable->PRDT[0].DBA.AsUlong = sgl->List[0].PhysicalAddress.LowPart;
if (ChannelExtension->AdapterExtension->CAP.S64A) {//If the controller supports 64 bits, write the high part too
cmdTable->PRDT[0].DBAU = sgl->List[0].PhysicalAddress.HighPart;
}
cmdTable->PRDT[0].DI.DBC = (0x10000 - 1);
// Entry 1
cmdTable->PRDT[1].DBA.AsUlong = (sgl->List[0].PhysicalAddress.LowPart + 0x10000);
if (ChannelExtension->AdapterExtension->CAP.S64A) {//If the controller supports 64 bits, write the high part too
if ((sgl->List[0].PhysicalAddress.LowPart + 0x10000) < sgl->List[0].PhysicalAddress.LowPart) {
cmdTable->PRDT[1].DBAU = (sgl->List[0].PhysicalAddress.HighPart + 1); //add 1 to the highpart if adding 0x10000 caused a rollover
} else {
cmdTable->PRDT[1].DBAU = sgl->List[0].PhysicalAddress.HighPart;
}
}
cmdTable->PRDT[1].DI.DBC = (0x10000 - 1);
return 2;
}
} else {
NT_ASSERT(FALSE); //Shall Not Pass
return (ULONG)-1;
}
// 1.3 Verify that the DataLength is even
// all SATA transfers must be even
// DBC is a 0 based number (i.e. 0 is 1, 1 is 2, etc.
// sgl->Elements.Length is not (i.e. 0 is 0, 1 is 1, etc.
if ((sgl->List[i].Length & 1) == 0) { //therefore length must be even here
// 2.3 Set Datalength in the PRDT entries
cmdTable->PRDT[i].DI.DBC = sgl->List[i].Length - 1; //but it must be odd here
} else if (sgl->List[i].Length <= RequestGetDataTransferLength(SlotContent->Srb)) {
// Storport may send down SCSI commands with odd number of data transfer length, and it builds SGL using that transfer length value.
// we use the length -1 to get as much data as we can. If the data length is over (length - 1), buffer overrun will be reported when the command is completed.
RequestSetDataTransferLength(SlotContent->Srb, RequestGetDataTransferLength(SlotContent->Srb) - 1);
cmdTable->PRDT[i].DI.DBC = sgl->List[i].Length - 2;
} else {
NT_ASSERT(FALSE); //Shall Not Pass
return (ULONG)-1;
}
}
return sgl->NumberOfElements;
}
VOID
SRBtoCmdHeader(
_In_ PAHCI_CHANNEL_EXTENSION ChannelExtension,
_In_ PSLOT_CONTENT SlotContent,
_In_ ULONG Length,
_In_ BOOLEAN Reset
)
/*++
It assumes:
The chosen slot is available and not in use
Called by:
AhciHwStartIo
It performs:
Steps defined in AHCI 1.2 section 5.5.1 step #3
Affected Variables/Registers:
Command Header
--*/
{
PAHCI_COMMAND_HEADER cmdHeader = SlotContent->CmdHeader;
PSTORAGE_REQUEST_BLOCK srb = SlotContent->Srb;
PAHCI_SRB_EXTENSION srbExtension = GetSrbExtension(srb);
UNREFERENCED_PARAMETER(ChannelExtension);
// a. PRDTL containing the number of entries in the PRD table
cmdHeader->DI.PRDTL = Length;
// b. CFL set to the length of the command in the CFIS area
cmdHeader->DI.CFL = 5;
// c. A bit set if it is an ATAPI command
cmdHeader->DI.A = (srbExtension->AtaFunction & ATA_FUNCTION_ATAPI_COMMAND) ? 1 : 0;
// d. W (Write) bit set if data is going to the device
cmdHeader->DI.W = (srbExtension->Flags & ATA_FLAGS_DATA_OUT) ? 1 : 0;
// e. P (Prefetch) bit optionally set (see rules in section 5.5.2)
//Some controllers have problems if P is set.
cmdHeader->DI.P = 0;
// f. If a Port Multiplier is attached, the PMP field set to the correct Port Multiplier port.
cmdHeader->DI.PMP = 0;
// Reset
cmdHeader->DI.R = Reset;
cmdHeader->DI.B = 0;
cmdHeader->DI.C = Reset;
// Initialize the PRD byte count
cmdHeader->PRDBC = 0;
cmdHeader->Reserved[0] = 0;
cmdHeader->Reserved[1] = 0;
cmdHeader->Reserved[2] = 0;
cmdHeader->Reserved[3] = 0;
}
BOOLEAN
AhciProcessIo(
_In_ PAHCI_CHANNEL_EXTENSION ChannelExtension,
_In_ PSTORAGE_REQUEST_BLOCK Srb,
_In_ BOOLEAN AtDIRQL
)
/*
This routine does following:
1. Check if no device command associated with Srb, or the port failed to start. Bail out in these cases.
2. Central place for special command handling.
3. Get available slot
4. Fill the slot; Program command Table & Header; Put IO in Slice
Note: This routine can be called even the Port is stopped.
This routine does NOT program IO to adapter.
*/
{
PAHCI_SRB_EXTENSION srbExtension = GetSrbExtension(Srb);
UCHAR pathId = 0;
UCHAR targetId = 0;
UCHAR lun = 0;
STOR_LOCK_HANDLE lockHandle = { InterruptLock, 0 };
SrbGetPathTargetLun(Srb, &pathId, &targetId, &lun);
// 1.0 complete Srb if no command should be sent to device.
if (srbExtension->AtaFunction == 0) {
NT_ASSERT(FALSE); // should investigate if this ASSERT fires
Srb->SrbStatus = SRB_STATUS_ERROR;
MarkSrbToBeCompleted(Srb);
AhciCompleteRequest(ChannelExtension, Srb, AtDIRQL);
return TRUE;
}
// 1.1 Check if command processing should happen
if (ChannelExtension->StartState.ChannelNextStartState == StartFailed) {
Srb->SrbStatus = SRB_STATUS_NO_DEVICE;
MarkSrbToBeCompleted(Srb);
AhciCompleteRequest(ChannelExtension, Srb, AtDIRQL);
return TRUE;
}
// 2. central place for command special handling. this part cannot be put in command translation layer, as there might be internal command also needs special handling.
// 2.1 central place for special handling of Enable/Disable WRITE CACHE, update persistent settings command.
if ((srbExtension->AtaFunction == ATA_FUNCTION_ATA_COMMAND) &&
(srbExtension->TaskFile.Current.bCommandReg == IDE_COMMAND_SET_FEATURE)) {
if (srbExtension->TaskFile.Current.bFeaturesReg == IDE_FEATURE_DISABLE_WRITE_CACHE) {
// IDE_FEATURE_DISABLE_WRITE_CACHE => Modify Persistent Settings
UpdateSetFeatureCommands(ChannelExtension, IDE_FEATURE_ENABLE_WRITE_CACHE, IDE_FEATURE_DISABLE_WRITE_CACHE, 0, 0);
}
if (srbExtension->TaskFile.Current.bFeaturesReg == IDE_FEATURE_ENABLE_WRITE_CACHE) {
// IDE_FEATURE_ENABLE_WRITE_CACHE => Modify Persistent Settings
UpdateSetFeatureCommands(ChannelExtension, IDE_FEATURE_DISABLE_WRITE_CACHE, IDE_FEATURE_ENABLE_WRITE_CACHE, 0, 0);
}
}
// 2.2 update the Identify command for enumeration. This needs to be here waiting for PxSIG to be accessible.
if ((srbExtension->AtaFunction == ATA_FUNCTION_ATA_IDENTIFY) &&
(srbExtension->CompletionRoutine == AhciPortIdentifyDevice) &&
(srbExtension->TaskFile.Current.bCommandReg == IDE_COMMAND_NOT_VALID)) {
// For enumeration command, it's safe now to check the PxSIG register as starting port has been executed.
// Note: there is chance that the signature register is FFFFFFFFh when accessing it here because reset is in progress.
// Handle this special case in the follow checking device type code.
ULONG sig = 0;
sig = StorPortReadRegisterUlong(ChannelExtension->AdapterExtension, &ChannelExtension->Px->SIG.AsUlong);
if (sig == ATA_DEVICE_SIGNATURE_ATA) {
srbExtension->TaskFile.Current.bCommandReg = IDE_COMMAND_IDENTIFY;
ChannelExtension->DeviceExtension[0].DeviceParameters.AtaDeviceType = DeviceIsAta;
} else if (sig == ATA_DEVICE_SIGNATURE_ATAPI) {
srbExtension->TaskFile.Current.bCommandReg = IDE_COMMAND_ATAPI_IDENTIFY;
ChannelExtension->DeviceExtension[0].DeviceParameters.AtaDeviceType = DeviceIsAtapi;
} else {
ULONG ssts = StorPortReadRegisterUlong(ChannelExtension->AdapterExtension, &ChannelExtension->Px->SSTS.AsUlong);
RecordExecutionHistory(ChannelExtension, 0x10090020); // Unexpected signature register value
if ((sig == 0) || ((sig == (ULONG)~0) && (ssts != (ULONG)~0))) {
// Case 1: A value of 0 is not a valid signature. Some devices
// initially report a valid signature but soon after the PxSIG
// register becomes 0.
// Case 2: A value of FFFFFFFFh is set to signature register during reset according to AHCI 1.3.1 spec,
// while the value maybe also FFFFFFFFh when device is removed, because PCI configuration space that mapped with AHCI registers
// are FFFFFFFFh. To differentiate whether it is reset or device removed, check Px.SSTS register value(it is 0 during reset).
//
// If above situations happen, just set the identify command according to the currently known device type.
switch (ChannelExtension->DeviceExtension[0].DeviceParameters.AtaDeviceType) {
case DeviceIsAta:
srbExtension->TaskFile.Current.bCommandReg = IDE_COMMAND_IDENTIFY;
break;
case DeviceIsAtapi:
default:
srbExtension->TaskFile.Current.bCommandReg = IDE_COMMAND_ATAPI_IDENTIFY;
break;
}
} else {
// Other devices types, such as with signature ATA_DEVICE_SIGNATURE_ENCLOSURE, or
// ATA_DEVICE_SIGNATURE_PORT_MULTIPLIER, are not supported.
// IDENTIFY PACKET DEVICE command will fail on these devices.
srbExtension->TaskFile.Current.bCommandReg = IDE_COMMAND_ATAPI_IDENTIFY;
ChannelExtension->DeviceExtension[0].DeviceParameters.AtaDeviceType = DeviceUnknown;
}
}
}
// 2.3 this is central place to update this register value in case it's wrongly set.
if (IsAtaCommand(srbExtension->AtaFunction)) {
SetDeviceReg((&srbExtension->TaskFile.Current), 0); //make sure set to device0
}
// 3 Find an available slot/tag (AHCI 1.1 Section 5.5.1)
if (!AtDIRQL) {
AhciInterruptSpinlockAcquire(ChannelExtension->AdapterExtension, ChannelExtension->PortNumber, &lockHandle);
}
GetAvailableSlot(ChannelExtension, Srb); //srbExtension->QueueTag will be set
// 3.1 If no tag is available, reject the command to be retried later
if (srbExtension->QueueTag > ChannelExtension->AdapterExtension->CAP.NCS) {
// Wait for 8 IO (random picked number) being completed before re-starting sending IO to miniport for this device.
// if filled up,
if (!IsMiniportInternalSrb(ChannelExtension, Srb)) {
// Report busy only for IOs from Storport
StorPortDeviceBusy(ChannelExtension->AdapterExtension, pathId, targetId, lun, min(8, ChannelExtension->AdapterExtension->CAP.NCS));
}
Srb->SrbStatus = SRB_STATUS_BUSY;
MarkSrbToBeCompleted(Srb);
AhciCompleteRequest(ChannelExtension, Srb, TRUE);
RecordExecutionHistory(ChannelExtension, 0x10030020); //AllocateQueueTagFailed
goto exit;
}
#ifdef DBG
{
ULONG ci;
ULONG sact;
ci = StorPortReadRegisterUlong(ChannelExtension->AdapterExtension, &ChannelExtension->Px->CI);
sact = StorPortReadRegisterUlong(ChannelExtension->AdapterExtension, &ChannelExtension->Px->SACT);
// 3.2 if the tag returned can't be used, something is seriously wrong. Reject the command to be retried later
if ((( (1 << srbExtension->QueueTag) & ci) > 0) || (((1 << srbExtension->QueueTag) & sact) > 0)) {
if (!IsMiniportInternalSrb(ChannelExtension, Srb)) {
// pause device only for IOs from Storport
StorPortPauseDevice(ChannelExtension->AdapterExtension, pathId, targetId, lun, 1); //pause for 1 seconds.
}
// One reason why this can happen is if the adapter was removed.
if (IsAdapterRemoved(ChannelExtension->AdapterExtension)) {
Srb->SrbStatus = SRB_STATUS_NO_DEVICE;
} else {
NT_ASSERT(FALSE); //can this happen? catch it.
Srb->SrbStatus = SRB_STATUS_BUSY;
}
MarkSrbToBeCompleted(Srb);
AhciCompleteRequest(ChannelExtension, Srb, TRUE);
RecordExecutionHistory(ChannelExtension, 0x10040020);//Tag given for slot in use
goto exit;
}
}
#endif
//srbExtension->QueueTag and Slot[srbExtension->QueueTag] are now guaranteed ready.
AhciFormIo (ChannelExtension, Srb, TRUE);
exit:
if (!AtDIRQL) {
AhciInterruptSpinlockRelease(ChannelExtension->AdapterExtension, ChannelExtension->PortNumber, &lockHandle);
}
return TRUE;
}
BOOLEAN
AhciFormIo(
_In_ PAHCI_CHANNEL_EXTENSION ChannelExtension,
_In_ PSTORAGE_REQUEST_BLOCK Srb,
_In_ BOOLEAN AtDIRQL
)
/*
Caller: only AhciProcessIo()
*/
{
PAHCI_SRB_EXTENSION srbExtension;
PSLOT_CONTENT slotContent;
PAHCI_COMMAND_TABLE cmdTable;
PAHCI_COMMAND_HEADER cmdHeader;
STOR_PHYSICAL_ADDRESS cmdTablePhysicalAddress;
ULONG prdtLength = 0;
PCDB cdb = SrbGetCdb(Srb);
srbExtension = GetSrbExtension(Srb);
// Command header is allocated in ChannelExtension for all command slots, get the correct one.
cmdHeader = ChannelExtension->CommandList;
cmdHeader += srbExtension->QueueTag;
slotContent = &ChannelExtension->Slot[srbExtension->QueueTag];
// 1. setup slot content and log history
slotContent->Srb = Srb;
slotContent->CmdHeader = cmdHeader;
// 1.1 Update FUA tracking flag if needed
if (!IsReturnResults(srbExtension->Flags) &&
(cdb != NULL) &&
((cdb->CDB10.OperationCode == SCSIOP_WRITE) || (cdb->CDB10.OperationCode == SCSIOP_WRITE16)) &&
(cdb->CDB10.ForceUnitAccess == 1) &&
IsFuaSupported(ChannelExtension)) {
// Keep track of FUA to add it back in when the command is put in the FIS
slotContent->StateFlags.FUA = TRUE;
}
if (LogCommand(ChannelExtension->AdapterExtension->LogFlags)) {
// 1/2 Log command starting part for our debugging records
PCOMMAND_HISTORY cmdHistory;
ChannelExtension->CommandHistoryNextAvailableIndex++;
ChannelExtension->CommandHistoryNextAvailableIndex %= 64;
slotContent->CommandHistoryIndex = ChannelExtension->CommandHistoryNextAvailableIndex;
cmdHistory = &ChannelExtension->CommandHistory[slotContent->CommandHistoryIndex];
cmdHistory->Tag = srbExtension->QueueTag;
StorPortCopyMemory(&cmdHistory->InitialTaskFile, &srbExtension->TaskFile, 0x10);
cmdHistory->InitialPx[0] = StorPortReadRegisterUlong(ChannelExtension->AdapterExtension, &ChannelExtension->Px->CLB.AsUlong);
cmdHistory->InitialPx[1] = StorPortReadRegisterUlong(ChannelExtension->AdapterExtension, &ChannelExtension->Px->CLBU);
cmdHistory->InitialPx[2] = StorPortReadRegisterUlong(ChannelExtension->AdapterExtension, &ChannelExtension->Px->FB.AsUlong);
cmdHistory->InitialPx[3] = StorPortReadRegisterUlong(ChannelExtension->AdapterExtension, &ChannelExtension->Px->FBU);
cmdHistory->InitialPx[4] = StorPortReadRegisterUlong(ChannelExtension->AdapterExtension, &ChannelExtension->Px->IS.AsUlong);
cmdHistory->InitialPx[5] = StorPortReadRegisterUlong(ChannelExtension->AdapterExtension, &ChannelExtension->Px->IE.AsUlong);
cmdHistory->InitialPx[6] = StorPortReadRegisterUlong(ChannelExtension->AdapterExtension, &ChannelExtension->Px->CMD.AsUlong);
cmdHistory->InitialPx[7] = StorPortReadRegisterUlong(ChannelExtension->AdapterExtension, &ChannelExtension->Px->DW7_Reserved);
cmdHistory->InitialPx[8] = StorPortReadRegisterUlong(ChannelExtension->AdapterExtension, &ChannelExtension->Px->TFD.AsUlong);
cmdHistory->InitialPx[9] = StorPortReadRegisterUlong(ChannelExtension->AdapterExtension, &ChannelExtension->Px->SIG.AsUlong);
cmdHistory->InitialPx[10] = StorPortReadRegisterUlong(ChannelExtension->AdapterExtension, &ChannelExtension->Px->SSTS.AsUlong);
cmdHistory->InitialPx[11] = StorPortReadRegisterUlong(ChannelExtension->AdapterExtension, &ChannelExtension->Px->SCTL.AsUlong);
cmdHistory->InitialPx[12] = StorPortReadRegisterUlong(ChannelExtension->AdapterExtension, &ChannelExtension->Px->SERR.AsUlong);
cmdHistory->InitialPx[13] = StorPortReadRegisterUlong(ChannelExtension->AdapterExtension, &ChannelExtension->Px->SACT);
cmdHistory->InitialPx[14] = StorPortReadRegisterUlong(ChannelExtension->AdapterExtension, &ChannelExtension->Px->CI);
cmdHistory->InitialPx[15] = StorPortReadRegisterUlong(ChannelExtension->AdapterExtension, &ChannelExtension->Px->SNTF.AsUlong);
cmdHistory->Function = SrbGetSrbFunction(slotContent->Srb);
cmdHistory->SrbStatus = 0;
}
// Already get a slot, after this point, any request completion effort needs to release the slot.
// just like what's done in: ReleaseSlottedCommand()
// 2. Program the CFIS in the CommandTable (allocated in srbExtension)
cmdTable = (PAHCI_COMMAND_TABLE)srbExtension;
if (IsAtapiCommand(srbExtension->AtaFunction)) {
SRBtoATAPI_CFIS(ChannelExtension, slotContent);
} else if (IsAtaCfisPayload(srbExtension->AtaFunction)) {
CfistoATA_CFIS(ChannelExtension, slotContent);
} else if (IsAtaCommand(srbExtension->AtaFunction)) {
SRBtoATA_CFIS(ChannelExtension, slotContent);
} else {
NT_ASSERT(FALSE);
slotContent->Srb->SrbStatus = SRB_STATUS_INVALID_REQUEST;
AhciCompleteJustSlottedRequest(ChannelExtension, Srb, AtDIRQL);
RecordExecutionHistory(ChannelExtension, 0x10070020); //Invalid ATA Function
return TRUE;
}
// 3. Build the PRD Table in CommandTable.
if (IsDataTransferNeeded(slotContent->Srb)) {
prdtLength = SRBtoPRDT(ChannelExtension, slotContent);
if (prdtLength == -1) {
NT_ASSERT(FALSE);
slotContent->Srb->SrbStatus = SRB_STATUS_INVALID_REQUEST;
AhciCompleteJustSlottedRequest(ChannelExtension, Srb, AtDIRQL);
RecordExecutionHistory(ChannelExtension, 0x10060020); //Invalid SGL
return TRUE;
}
}
// 4. Program the Command Header (allocated in ChannelExtension for all command slots)
SRBtoCmdHeader(ChannelExtension, slotContent, prdtLength, FALSE);
// 4.1. Get the Command Table's physical address to verify the alignment and program cmdHeader->CTBA
if ((PSTORAGE_REQUEST_BLOCK)&ChannelExtension->Local.Srb == Srb) {
cmdTablePhysicalAddress = ChannelExtension->Local.SrbExtensionPhysicalAddress;
} else if ((PSTORAGE_REQUEST_BLOCK)&ChannelExtension->Sense.Srb == Srb) {
cmdTablePhysicalAddress = ChannelExtension->Sense.SrbExtensionPhysicalAddress;
} else {
ULONG length;
// SRB is needed for StorPortGetPhysicalAddress to calculate logical address when DMAR is enabled.
cmdTablePhysicalAddress = StorPortGetPhysicalAddress(ChannelExtension->AdapterExtension, (PSCSI_REQUEST_BLOCK)Srb, srbExtension, &length);
}
if ((cmdTablePhysicalAddress.LowPart % 128) == 0) {
cmdHeader->CTBA.AsUlong = cmdTablePhysicalAddress.LowPart;
} else {
slotContent->Srb->SrbStatus = SRB_STATUS_INVALID_REQUEST;
AhciCompleteJustSlottedRequest(ChannelExtension, Srb, AtDIRQL);
RecordExecutionHistory(ChannelExtension, 0x10080020);//Invalid PhyscialAddress alignment
return TRUE;
}
// If the controller supports 64 bits, write the high part too
if (ChannelExtension->AdapterExtension->CAP.S64A) {
cmdHeader->CTBAU = cmdTablePhysicalAddress.HighPart;
}
// 5. Sort it, put request in one of IO Slices.
// 5.1. Keep track of high priority outside of the Slices
if (IsHighPriorityCommand(srbExtension->Flags)) {
ChannelExtension->SlotManager.HighPriorityAttribute |= ( 1 << srbExtension->QueueTag);
}
// 5.2. Put the slot content in the correct Slice
if (IsNCQCommand(srbExtension)) {
ChannelExtension->SlotManager.NCQueueSlice |= ( 1 << srbExtension->QueueTag);
} else if (IsReturnResults(srbExtension->Flags)) {
ChannelExtension->SlotManager.SingleIoSlice |= ( 1 << srbExtension->QueueTag);
} else if (IsNormalCommand(slotContent->Srb)) {
ChannelExtension->SlotManager.NormalQueueSlice |= ( 1 << srbExtension->QueueTag);
} else {
ChannelExtension->SlotManager.SingleIoSlice |= ( 1 << srbExtension->QueueTag);
}
if (LogExecuteFullDetail(ChannelExtension->AdapterExtension->LogFlags)) {
RecordExecutionHistory(ChannelExtension, 0x10000020);//Exit AhciHwStartIo
}
return TRUE;
}
PSCSI_REQUEST_BLOCK
BuildRequestSenseSrb(
_In_ PAHCI_CHANNEL_EXTENSION ChannelExtension,
_In_ PSTORAGE_REQUEST_BLOCK FailingSrb
)
{
STOR_PHYSICAL_ADDRESS dataBufferPhysicalAddress;
ULONG length;
PCDB cdb;
PSCSI_REQUEST_BLOCK senseSrb = &ChannelExtension->Sense.Srb;
PAHCI_SRB_EXTENSION srbExtension = ChannelExtension->Sense.SrbExtension;
PVOID srbSenseBuffer = NULL;
UCHAR srbSenseBufferLength = 0;
RequestGetSrbScsiData(FailingSrb, NULL, NULL, &srbSenseBuffer, &srbSenseBufferLength);
// 1. Validate if senseSrb should be used.
if ((srbExtension == NULL) || (srbExtension->AtaFunction != 0) ||
(srbSenseBuffer == NULL) || (srbSenseBufferLength == 0)) {
// Request Sense is for ATAPI commands which are single IOs. At the same time there should be only one command failed and needs this Srb.
NT_ASSERT(srbExtension && srbExtension->AtaFunction == 0);
return NULL;
}
if ((srbSenseBuffer != NULL) && (srbSenseBufferLength > 0)) {
AhciZeroMemory((PCHAR)srbSenseBuffer, srbSenseBufferLength);
}
// 2. Initialize Srb and SrbExtension structures.
AhciZeroMemory((PCHAR)senseSrb, sizeof(SCSI_REQUEST_BLOCK));
AhciZeroMemory((PCHAR)srbExtension, sizeof(AHCI_SRB_EXTENSION));
// 3. Setup Srb and CDB. Note that Sense Srb uses SCSI_REQUEST_BLOCK type.
senseSrb->Length = sizeof(SCSI_REQUEST_BLOCK);
senseSrb->Function = SRB_FUNCTION_EXECUTE_SCSI;
senseSrb->PathId = (UCHAR)ChannelExtension->PortNumber;
senseSrb->SrbFlags = SRB_FLAGS_DATA_IN;
senseSrb->DataBuffer = srbSenseBuffer;
senseSrb->DataTransferLength = srbSenseBufferLength;
senseSrb->SrbExtension = (PVOID)srbExtension;
senseSrb->OriginalRequest = (PVOID)FailingSrb;
senseSrb->TimeOutValue = 1;
senseSrb->CdbLength = 6;
cdb = (PCDB)&senseSrb->Cdb;
cdb->CDB6INQUIRY.OperationCode = SCSIOP_REQUEST_SENSE;
cdb->CDB6INQUIRY.AllocationLength = (UCHAR)senseSrb->DataTransferLength;
// 4. Fill in the srbExtension fields
srbExtension->AtaFunction = ATA_FUNCTION_REQUEST_SENSE;
srbExtension->Flags = ATA_FLAGS_DATA_IN;
// SRB is needed for StorPortGetPhysicalAddress to calculate logical address when DMAR is enabled.
dataBufferPhysicalAddress = StorPortGetPhysicalAddress(ChannelExtension->AdapterExtension, (PSCSI_REQUEST_BLOCK)FailingSrb, srbSenseBuffer, &length);
if (dataBufferPhysicalAddress.QuadPart == 0) {
NT_ASSERT(FALSE); //Shall Not Pass
return NULL;
}
srbExtension->LocalSgl.NumberOfElements = 1;
srbExtension->LocalSgl.List[0].PhysicalAddress.LowPart = dataBufferPhysicalAddress.LowPart;
srbExtension->LocalSgl.List[0].PhysicalAddress.HighPart = dataBufferPhysicalAddress.HighPart;
srbExtension->LocalSgl.List[0].Length = senseSrb->DataTransferLength;
srbExtension->Sgl = &srbExtension->LocalSgl;
return senseSrb;
}
VOID
AhciPortFailAllIos(
_In_ PAHCI_CHANNEL_EXTENSION ChannelExtension,
_In_ UCHAR SrbStatus,
_In_ BOOLEAN AtDIRQL
)
{
UCHAR i;
// Complete all requests in slots
for (i = 0; i <= ChannelExtension->AdapterExtension->CAP.NCS; i++) {
if (ChannelExtension->Slot[i].Srb != NULL) {
ChannelExtension->Slot[i].Srb->SrbStatus = SrbStatus;
AhciCompleteJustSlottedRequest(ChannelExtension, ChannelExtension->Slot[i].Srb, AtDIRQL);
}
}
// Requests in completion queue will be completed by AhciPortSrbCompletionDpcRoutine.
return;
}
VOID
AhciPortSrbCompletionDpcRoutine(
_In_ PSTOR_DPC Dpc,
_In_ PVOID AdapterExtension,
_In_opt_ PVOID SystemArgument1,
_In_opt_ PVOID SystemArgument2
)
{
PAHCI_CHANNEL_EXTENSION channelExtension = (PAHCI_CHANNEL_EXTENSION)SystemArgument1;
STOR_LOCK_HANDLE lockhandle = { InterruptLock, 0 };
PSTORAGE_REQUEST_BLOCK srb = NULL;
PSRB_COMPLETION_ROUTINE completionRoutine = NULL;
BOOLEAN reservedSlotInUse = FALSE;
BOOLEAN sendCommand = FALSE;
BOOLEAN sendCommandForLocalSrb = FALSE;
ULONGLONG count = 0; // record the number of Srb being completed in this routine. It's for diagnostic purpose.
UNREFERENCED_PARAMETER(Dpc);
UNREFERENCED_PARAMETER(SystemArgument2);
if (channelExtension == NULL) {
NT_ASSERT(channelExtension != NULL);
return;
}
if (LogExecuteFullDetail(channelExtension->AdapterExtension->LogFlags)) {
RecordExecutionHistory(channelExtension, 0x0000001e);//Enter AhciPortSrbCompletionDpcRoutine
}
StorPortQuerySystemTime(&(channelExtension->LastTimeStampDpcStart));
//
// Preserve the use status about Reserved Slot.
//
reservedSlotInUse = (channelExtension->StateFlags.ReservedSlotInUse == 1);
do {
AhciInterruptSpinlockAcquire(channelExtension->AdapterExtension, channelExtension->PortNumber, &lockhandle);
srb = RemoveQueue(channelExtension, &channelExtension->CompletionQueue, 0xDEADC0DE, 0x9F);
AhciInterruptSpinlockRelease(channelExtension->AdapterExtension, channelExtension->PortNumber, &lockhandle);
if (srb != NULL) {
PAHCI_SRB_EXTENSION srbExtension = GetSrbExtension(srb);
BOOLEAN completeSrb = TRUE;
completionRoutine = srbExtension->CompletionRoutine;
srbExtension->AtaFunction = 0; // clear this field.
srbExtension->CompletionRoutine = NULL;
if (completionRoutine != NULL) {
completionRoutine(channelExtension, srb);
// A request with STATUS_BUS_RESET should be completed after running completion routine.
if ((srbExtension->AtaFunction != 0) &&
(!SrbShouldBeCompleted(srbExtension->Flags)) &&
(srb->SrbStatus != SRB_STATUS_BUS_RESET)) {
// new command associated needs to be processed, do not complete the request.
AhciProcessIo(channelExtension, srb, FALSE);
// This Srb should not be completed yet
completeSrb = FALSE;
sendCommand = TRUE;
if (srb == (PSTORAGE_REQUEST_BLOCK)&channelExtension->Local.Srb) {
sendCommandForLocalSrb = TRUE;
}
} else if (SrbShouldBeCompleted(srbExtension->Flags)) {
// Clear the flag
CLRMASK(srbExtension->Flags, ATA_FLAGS_COMPLETE_SRB);
}
} else {
// An Srb without completion routine should be completed.
}
if ((srb == (PSTORAGE_REQUEST_BLOCK)&channelExtension->Local.Srb) &&
reservedSlotInUse &&
channelExtension->StateFlags.ReservedSlotInUse &&
(!sendCommandForLocalSrb)) {
//
// This should never happen. ReservedSlotInUse flag will stuck.
//
NT_ASSERT(FALSE);
RecordExecutionHistory(channelExtension, 0x1001001e); //AhciPortSrbCompletionDpcRoutine, ReservedSlotInUse starts to stuck
AhciTelemetryLog(channelExtension->AdapterExtension,
(PSTOR_ADDRESS)&(channelExtension->DeviceExtension->DeviceAddress),
AHCI_TELEMETRY_DRIVER_VERSION,
AhciTelemetryEventIdReservedSlotStuck,
"Reserved Slot Stuck",
AHCI_TELEMETRY_EVENT_VERSION,
0,
0,
NULL,
"CommandsIssued",
channelExtension->SlotManager.CommandsIssued,
"NCQueueSlice",
channelExtension->SlotManager.NCQueueSlice,
"NormalQueueSlice",
channelExtension->SlotManager.NormalQueueSlice,
"SingleIoSlice",
channelExtension->SlotManager.SingleIoSlice,
"CommandsToComplete",
channelExtension->SlotManager.CommandsToComplete,
"StateFlags",
*(ULONGLONG *)&(channelExtension->StateFlags),
"StartState",
*(ULONGLONG *)&(channelExtension->StartState),
"Flags|Function|Status",
(ULONGLONG)((((ULONGLONG)srbExtension->Flags) << 32) |
(((ULONGLONG)srbExtension->AtaFunction) << 16) |
(ULONGLONG)(srb->SrbStatus))
);
}
if (completeSrb) {
// Release active reference for port/device and adapter.
if ((srbExtension->Flags & ATA_FLAGS_ACTIVE_REFERENCE) != 0) {
PortReleaseActiveReference(channelExtension, srb);
}
if (!IsMiniportInternalSrb(channelExtension, srb)) {
if (SRB_STATUS(srb->SrbStatus) != SRB_STATUS_SUCCESS) {
}
NT_ASSERT(srb->SrbStatus != SRB_STATUS_PENDING);
StorPortNotification(RequestComplete, AdapterExtension, srb);
}
}
}
count++;
} while (srb != NULL);
//
// If Reserved Slot is freed by one of completion routine, send pending commands to device.
//
if (reservedSlotInUse && (channelExtension->StateFlags.ReservedSlotInUse == 0)) {
sendCommand = TRUE;
}
if (sendCommand) {
ActivateQueue(channelExtension, (IsDumpMode(channelExtension->AdapterExtension) ? (TRUE) : (FALSE)));
}
StorPortQuerySystemTime(&(channelExtension->LastTimeStampDpcCompletion));
if (LogExecuteFullDetail(channelExtension->AdapterExtension->LogFlags)) {
RecordExecutionHistory(channelExtension, 0x1000001e);//Exit AhciPortSrbCompletionDpcRoutine
}
return;
}
#if _MSC_VER >= 1200
#pragma warning(pop)
#else
#pragma warning(default:4214)
#pragma warning(default:4201)
#endif
|