feat: wait up to 10s for a late MONTAUKROOT, retry USB enumeration after a port reset, recover halted EP0/bulk endpoints, MSC card reader settle
This commit is contained in:
1 parent
ffee9002a6
commit
93d8becce3
14 files changed
+340
-57
No files matched your search
@@ -51,6 +51,16 @@ namespace Drivers::USB::MassStorage {
|
||||
|
||||
constexpr uint8_t SERVICE_READ_CAPACITY16 = 0x10;
|
||||
|
||||
constexpr uint8_t SENSE_NOT_READY = 0x02;
|
||||
constexpr uint8_t SENSE_UNIT_ATTENTION = 0x06;
|
||||
constexpr uint8_t ASC_MEDIUM_NOT_PRESENT = 0x3A;
|
||||
|
||||
// TEST UNIT READY attempts, 100 ms apart: any failure gets a few, a unit
|
||||
// that says it is coming up (see TestUnitReady) gets about five seconds.
|
||||
constexpr int TUR_TRIES = 5;
|
||||
constexpr int TUR_SETTLE_TRIES = 50;
|
||||
constexpr int TUR_NO_MEDIUM_TRIES = 3;
|
||||
|
||||
constexpr uint32_t MAX_BULK_TRANSFER_BYTES = 0x10000;
|
||||
constexpr int MAX_MSC_DEVICES = 16;
|
||||
|
||||
@@ -275,6 +285,15 @@ namespace Drivers::USB::MassStorage {
|
||||
return maxLun;
|
||||
}
|
||||
|
||||
// A failed stage leaves its endpoint halted, or after a timeout its TRB
|
||||
// still queued; either way the next transfer would fail too.
|
||||
static uint32_t BulkStage(Device& dev, bool dirIn, void* data, uint64_t dataPhys,
|
||||
uint32_t length, uint32_t* actual) {
|
||||
uint32_t cc = Xhci::BulkTransfer(dev.SlotId, dirIn, data, dataPhys, length, actual);
|
||||
if (!IsGoodCompletion(cc)) Xhci::RecoverBulkEndpoint(dev.SlotId, dirIn);
|
||||
return cc;
|
||||
}
|
||||
|
||||
static bool ScsiCommand(Device& dev, const uint8_t* cdb, uint8_t cdbLen,
|
||||
void* data, uint64_t dataPhys, uint32_t dataLen,
|
||||
bool dataIn) {
|
||||
@@ -295,24 +314,34 @@ namespace Drivers::USB::MassStorage {
|
||||
memcpy(cbw->CommandBlock, cdb, cdbLen);
|
||||
|
||||
uint32_t actual = 0;
|
||||
uint32_t cc = Xhci::BulkTransfer(dev.SlotId, false, dev.CbwBuf, dev.CbwPhys,
|
||||
sizeof(CommandBlockWrapper), &actual);
|
||||
uint32_t cc = BulkStage(dev, false, dev.CbwBuf, dev.CbwPhys,
|
||||
sizeof(CommandBlockWrapper), &actual);
|
||||
if (!IsGoodCompletion(cc) || actual != sizeof(CommandBlockWrapper)) {
|
||||
ReleaseTransport();
|
||||
return false;
|
||||
}
|
||||
|
||||
// A device that fails the command, or has less data than asked for,
|
||||
// may STALL the data stage; the status still follows in the CSW once
|
||||
// the halt is cleared (BOT 6.7.2, 6.7.3). Card readers do this for
|
||||
// commands on a slot that is not ready.
|
||||
if (dataLen > 0) {
|
||||
cc = Xhci::BulkTransfer(dev.SlotId, dataIn, data, dataPhys, dataLen, &actual);
|
||||
if (!IsGoodCompletion(cc)) {
|
||||
cc = BulkStage(dev, dataIn, data, dataPhys, dataLen, &actual);
|
||||
if (!IsGoodCompletion(cc) && cc != Xhci::CC_STALL_ERROR) {
|
||||
ReleaseTransport();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// A STALL on the CSW itself is cleared and the CSW read again once
|
||||
// (BOT 5.3.3, figure 2).
|
||||
memset(dev.CswBuf, 0, 0x1000);
|
||||
cc = Xhci::BulkTransfer(dev.SlotId, true, dev.CswBuf, dev.CswPhys,
|
||||
sizeof(CommandStatusWrapper), &actual);
|
||||
cc = BulkStage(dev, true, dev.CswBuf, dev.CswPhys,
|
||||
sizeof(CommandStatusWrapper), &actual);
|
||||
if (cc == Xhci::CC_STALL_ERROR) {
|
||||
cc = BulkStage(dev, true, dev.CswBuf, dev.CswPhys,
|
||||
sizeof(CommandStatusWrapper), &actual);
|
||||
}
|
||||
if (!IsGoodCompletion(cc) || actual < sizeof(CommandStatusWrapper)) {
|
||||
ReleaseTransport();
|
||||
return false;
|
||||
@@ -335,35 +364,66 @@ namespace Drivers::USB::MassStorage {
|
||||
// SCSI commands
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
static bool RequestSense(Device& dev) {
|
||||
struct Sense {
|
||||
uint8_t Key;
|
||||
uint8_t Asc;
|
||||
uint8_t Ascq;
|
||||
};
|
||||
|
||||
static bool RequestSense(Device& dev, Sense& out) {
|
||||
uint8_t cdb[6] = {};
|
||||
cdb[0] = SCSI_REQUEST_SENSE;
|
||||
cdb[4] = 18;
|
||||
|
||||
memset(dev.ScratchBuf, 0, 18);
|
||||
bool ok = ScsiCommand(dev, cdb, 6, dev.ScratchBuf, dev.ScratchPhys, 18, true);
|
||||
if (ok) {
|
||||
uint8_t senseKey = dev.ScratchBuf[2] & 0x0F;
|
||||
uint8_t asc = dev.ScratchBuf[12];
|
||||
uint8_t ascq = dev.ScratchBuf[13];
|
||||
KernelLogStream(INFO, "USB-MSC") << "REQUEST SENSE: key="
|
||||
<< (uint64_t)senseKey << " asc=" << base::hex << (uint64_t)asc
|
||||
<< " ascq=" << (uint64_t)ascq << base::dec;
|
||||
if (!ScsiCommand(dev, cdb, 6, dev.ScratchBuf, dev.ScratchPhys, 18, true)) {
|
||||
return false;
|
||||
}
|
||||
return ok;
|
||||
|
||||
out.Key = dev.ScratchBuf[2] & 0x0F;
|
||||
out.Asc = dev.ScratchBuf[12];
|
||||
out.Ascq = dev.ScratchBuf[13];
|
||||
return true;
|
||||
}
|
||||
|
||||
// Right after a reset a unit reports UNIT ATTENTION (power on, medium
|
||||
// changed), and a card reader then reports NOT READY while it brings the
|
||||
// card up, which can take well over a second. Keep asking while the sense
|
||||
// says the unit is on its way; an empty slot (MEDIUM NOT PRESENT) is final.
|
||||
static bool TestUnitReady(Device& dev) {
|
||||
uint8_t cdb[6] = {};
|
||||
cdb[0] = SCSI_TEST_UNIT_READY;
|
||||
|
||||
for (int i = 0; i < 5; i++) {
|
||||
if (ScsiCommand(dev, cdb, 6, nullptr, 0, 0, false)) return true;
|
||||
RequestSense(dev);
|
||||
PollWaitMs(50);
|
||||
}
|
||||
int limit = TUR_TRIES;
|
||||
int noMedium = 0;
|
||||
Sense last = {0xFF, 0xFF, 0xFF};
|
||||
|
||||
return false;
|
||||
for (int i = 0; ; i++) {
|
||||
if (ScsiCommand(dev, cdb, 6, nullptr, 0, 0, false)) return true;
|
||||
|
||||
Sense sense = {};
|
||||
if (RequestSense(dev, sense)) {
|
||||
if (sense.Key != last.Key || sense.Asc != last.Asc || sense.Ascq != last.Ascq) {
|
||||
KernelLogStream(INFO, "USB-MSC") << "LUN " << (uint64_t)dev.Lun
|
||||
<< " REQUEST SENSE: key=" << (uint64_t)sense.Key
|
||||
<< " asc=" << base::hex << (uint64_t)sense.Asc
|
||||
<< " ascq=" << (uint64_t)sense.Ascq << base::dec;
|
||||
last = sense;
|
||||
}
|
||||
|
||||
if (sense.Key == SENSE_NOT_READY && sense.Asc == ASC_MEDIUM_NOT_PRESENT) {
|
||||
if (++noMedium >= TUR_NO_MEDIUM_TRIES) return false;
|
||||
} else {
|
||||
noMedium = 0;
|
||||
if (sense.Key == SENSE_NOT_READY || sense.Key == SENSE_UNIT_ATTENTION) {
|
||||
limit = TUR_SETTLE_TRIES;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (i + 1 >= limit) return false;
|
||||
PollWaitMs(100);
|
||||
}
|
||||
}
|
||||
|
||||
static bool Inquiry(Device& dev) {
|
||||
@@ -621,7 +681,14 @@ namespace Drivers::USB::MassStorage {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!ReadCapacity(dev)) {
|
||||
// Some readers fail the first READ CAPACITY after the unit has
|
||||
// just become ready.
|
||||
bool haveCapacity = false;
|
||||
for (int attempt = 0; attempt < 3 && !haveCapacity; attempt++) {
|
||||
if (attempt > 0) PollWaitMs(100);
|
||||
haveCapacity = ReadCapacity(dev);
|
||||
}
|
||||
if (!haveCapacity) {
|
||||
KernelLogStream(WARNING, "USB-MSC") << "READ CAPACITY failed for LUN "
|
||||
<< (uint64_t)lun;
|
||||
dev.Active = false;
|
||||
|
||||
Reference in new issue
Block a user