Skip to content

Commit

Permalink
Update LUN handling
Browse files Browse the repository at this point in the history
  • Loading branch information
uweseimet committed Jan 2, 2025
1 parent a2485e1 commit 0f53a17
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 25 deletions.
33 changes: 9 additions & 24 deletions cpp/devices/scsi_generic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(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<uint8_t>(SenseKey::ILLEGAL_REQUEST);
buf[7] = 10;
buf[12] = static_cast<uint8_t>(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;
Expand Down Expand Up @@ -232,7 +217,7 @@ int ScsiGeneric::ReadWriteData(span<uint8_t> 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;

Expand Down Expand Up @@ -260,10 +245,10 @@ int ScsiGeneric::ReadWriteData(span<uint8_t> buf, int chunk_size, bool enable_lo
return transferred_length;
}

void ScsiGeneric::EvaluateStatus(int status, span<uint8_t> buf, span<uint8_t> sense_data, bool write, bool enable_log)
void ScsiGeneric::EvaluateStatus(int status, span<uint8_t> buf, span<uint8_t> 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)));
}

Expand All @@ -277,8 +262,8 @@ void ScsiGeneric::EvaluateStatus(int status, span<uint8_t> buf, span<uint8_t> se
if (!status) {
status = static_cast<int>(sense_data[2]) & 0x0f;

if (static_cast<ScsiCommand>(local_cdb[0]) == ScsiCommand::INQUIRY
&& (static_cast<int>(local_cdb[1]) & 0b11100000)) {
if (static_cast<ScsiCommand>(local_cdb[0]) == ScsiCommand::INQUIRY && GetController()
&& GetController()->GetEffectiveLun()) {
// SCSI-2 section 8.2.5.1: Incorrect logical unit handling
buf[0] = 0x7f;
}
Expand Down
2 changes: 1 addition & 1 deletion cpp/devices/scsi_generic.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class ScsiGeneric : public PrimaryDevice

int ReadWriteData(span<uint8_t>, int, bool);

void EvaluateStatus(int, span<uint8_t>, span<uint8_t>, bool, bool);
void EvaluateStatus(int, span<uint8_t>, span<uint8_t>, bool);

void UpdateInternalBlockSize(span<uint8_t> buf, int);

Expand Down

0 comments on commit 0f53a17

Please sign in to comment.