diff --git a/cpp/devices/scsi_generic.cpp b/cpp/devices/scsi_generic.cpp index d017f569..cf7d4ac3 100644 --- a/cpp/devices/scsi_generic.cpp +++ b/cpp/devices/scsi_generic.cpp @@ -96,28 +96,13 @@ void ScsiGeneric::Dispatch(ScsiCommand cmd) remaining_count = byte_count; - auto &buf = GetController()->GetBuffer(); - // There is no explicit LUN support, the SG driver maps each LUN to a device file - if ((static_cast(local_cdb[1]) & 0b11100000) && cmd != ScsiCommand::INQUIRY) { - if (cmd != ScsiCommand::REQUEST_SENSE) { - throw ScsiException(SenseKey::ILLEGAL_REQUEST, Asc::LOGICAL_UNIT_NOT_SUPPORTED); - } - - fill_n(buf.begin(), 18, 0); - buf[0] = 0x70; - buf[2] = static_cast(SenseKey::ILLEGAL_REQUEST); - buf[7] = 10; - buf[12] = static_cast(Asc::LOGICAL_UNIT_NOT_SUPPORTED); - - const int length = min(18, byte_count); - GetController()->SetTransferSize(length, length); - DataInPhase(length); - - // When signalling an invalid LUN, for REQUEST SENSE the status must be GOOD - return; + if (GetController()->GetEffectiveLun() && cmd != ScsiCommand::INQUIRY) { + throw ScsiException(SenseKey::ILLEGAL_REQUEST, Asc::LOGICAL_UNIT_NOT_SUPPORTED); } + auto &buf = GetController()->GetBuffer(); + if (cmd == ScsiCommand::REQUEST_SENSE && deferred_sense_data_valid) { memcpy(buf.data(), deferred_sense_data.data(), deferred_sense_data.size()); deferred_sense_data_valid = false; @@ -232,7 +217,7 @@ int ScsiGeneric::ReadWriteData(span buf, int chunk_size, bool enable_lo format_header.clear(); - EvaluateStatus(status, span(buf.data(), length), sense_data, write, enable_log); + EvaluateStatus(status, span(buf.data(), length), sense_data, write); const int transferred_length = length - io_hdr.resid; @@ -260,10 +245,10 @@ int ScsiGeneric::ReadWriteData(span buf, int chunk_size, bool enable_lo return transferred_length; } -void ScsiGeneric::EvaluateStatus(int status, span buf, span sense_data, bool write, bool enable_log) +void ScsiGeneric::EvaluateStatus(int status, span buf, span sense_data, bool write) { if (status == -1) { - if (enable_log) { + if (GetController()) { LogError(fmt::format("Transfer of {0} byte(s) failed: {1}", buf.size(), strerror(errno))); } @@ -277,8 +262,8 @@ void ScsiGeneric::EvaluateStatus(int status, span buf, span se if (!status) { status = static_cast(sense_data[2]) & 0x0f; - if (static_cast(local_cdb[0]) == ScsiCommand::INQUIRY - && (static_cast(local_cdb[1]) & 0b11100000)) { + if (static_cast(local_cdb[0]) == ScsiCommand::INQUIRY && GetController() + && GetController()->GetEffectiveLun()) { // SCSI-2 section 8.2.5.1: Incorrect logical unit handling buf[0] = 0x7f; } diff --git a/cpp/devices/scsi_generic.h b/cpp/devices/scsi_generic.h index 5f956afd..7c35cffd 100644 --- a/cpp/devices/scsi_generic.h +++ b/cpp/devices/scsi_generic.h @@ -48,7 +48,7 @@ class ScsiGeneric : public PrimaryDevice int ReadWriteData(span, int, bool); - void EvaluateStatus(int, span, span, bool, bool); + void EvaluateStatus(int, span, span, bool); void UpdateInternalBlockSize(span buf, int);