From edc3bab07a66eb603ed8a730b0fc2a62f56f1054 Mon Sep 17 00:00:00 2001 From: Uwe Seimet Date: Mon, 23 Aug 2021 09:30:09 +0200 Subject: [PATCH] Updated LUN handling --- src/raspberrypi/controllers/scsidev_ctrl.cpp | 24 ++++++++++++-------- src/raspberrypi/devices/device.cpp | 4 +++- src/raspberrypi/devices/scsi_daynaport.cpp | 5 ---- src/raspberrypi/devices/scsi_host_bridge.cpp | 6 ----- src/raspberrypi/devices/scsicd.cpp | 6 ----- src/raspberrypi/devices/scsihd.cpp | 6 ----- src/raspberrypi/devices/scsimo.cpp | 6 ----- 7 files changed, 18 insertions(+), 39 deletions(-) diff --git a/src/raspberrypi/controllers/scsidev_ctrl.cpp b/src/raspberrypi/controllers/scsidev_ctrl.cpp index 6245511b..8c976a10 100644 --- a/src/raspberrypi/controllers/scsidev_ctrl.cpp +++ b/src/raspberrypi/controllers/scsidev_ctrl.cpp @@ -245,30 +245,39 @@ void SCSIDEV::Execute() ctrl.status = 0; } + LOGDEBUG("++++ CMD ++++ %s Executing command $%02X", __PRETTY_FUNCTION__, (unsigned int)ctrl.cmd[0]); + + int lun; try { - // TODO Verify LUN handling if ((SCSIDEV::scsi_command)ctrl.cmd[0] == eCmdInquiry) { // Use LUN0 for INQUIRY because LUN0 is assumed to be always available - ctrl.device = ctrl.unit[0]; + lun = 0; } else { - ctrl.device = ctrl.unit[GetLun()]; + lun = GetLun(); } } catch (const lun_exception& e) { - LOGINFO("%s Invalid LUN %d for ID %d", __PRETTY_FUNCTION__, e.getlun(), GetSCSIID()); + LOGINFO("Invalid LUN %d for ID %d", e.getlun(), GetSCSIID()); Error(ERROR_CODES::sense_key::ILLEGAL_REQUEST, ERROR_CODES::asc::INVALID_LUN); return; } - LOGDEBUG("++++ CMD ++++ %s Dispatching command $%02X", __PRETTY_FUNCTION__, (unsigned int)ctrl.cmd[0]); + ctrl.device = ctrl.unit[lun]; if (!ctrl.device->Dispatch(this)) { - LOGWARN("%s ID %d received unsupported command: $%02X", __PRETTY_FUNCTION__, GetSCSIID(), (BYTE)ctrl.cmd[0]); + LOGWARN("ID %d LUN %d received unsupported command: $%02X", GetSCSIID(), lun, (BYTE)ctrl.cmd[0]); Error(ERROR_CODES::sense_key::ILLEGAL_REQUEST, ERROR_CODES::asc::INVALID_COMMAND_OPERATION_CODE); } + + if ((SCSIDEV::scsi_command)ctrl.cmd[0] == eCmdInquiry) { + // SCSI-2 p.104 4.4.3 Incorrect logical unit handling + if (((ctrl.cmd[1] >> 5) & 0x07) != ctrl.device->GetLun()) { + ctrl.buffer[0] = 0x7f; + } + } } //--------------------------------------------------------------------------- @@ -337,14 +346,11 @@ void SCSIDEV::Error(ERROR_CODES::sense_key sense_key, ERROR_CODES::asc asc) return; } - // Logical Unit DWORD lun = (ctrl.cmd[1] >> 5) & 0x07; if (!ctrl.unit[lun] || asc == ERROR_CODES::INVALID_LUN) { lun = 0; } - LOGDEBUG("%s Sense Key and ASC: $%02X, $%02X", __PRETTY_FUNCTION__, sense_key, asc); - if (sense_key || asc) { // Set Sense Key and ASC for a subsequent REQUEST SENSE ctrl.unit[lun]->SetStatusCode((sense_key << 16) | (asc << 8)); diff --git a/src/raspberrypi/devices/device.cpp b/src/raspberrypi/devices/device.cpp index e2a57656..1e7f83cd 100644 --- a/src/raspberrypi/devices/device.cpp +++ b/src/raspberrypi/devices/device.cpp @@ -111,7 +111,9 @@ const string Device::GetPaddedName() const void Device::SetStatusCode(int status_code) { - LOGDEBUG("Setting status: Sense Key: $%02X, ASC: $%02X, ASCQ: $%02X", status_code >> 16, (status_code >> 8 &0xff), status_code & 0xff); + if (status_code) { + LOGDEBUG("Error status: Sense Key $%02X, ASC $%02X, ASCQ $%02X", status_code >> 16, (status_code >> 8 &0xff), status_code & 0xff); + } this->status_code = status_code; } diff --git a/src/raspberrypi/devices/scsi_daynaport.cpp b/src/raspberrypi/devices/scsi_daynaport.cpp index e36324f2..34915eb6 100644 --- a/src/raspberrypi/devices/scsi_daynaport.cpp +++ b/src/raspberrypi/devices/scsi_daynaport.cpp @@ -179,11 +179,6 @@ int SCSIDaynaPort::Inquiry(const DWORD *cdb, BYTE *buffer) memcpy(&buffer[8], GetPaddedName().c_str(), 28); } - // SCSI-2 p.104 4.4.3 Incorrect logical unit handling - if ((cdb[1] >> 5) & 0x07) { - buffer[0] |= 0x7f; - } - LOGTRACE("response size is %d", (int)allocation_length); return allocation_length; diff --git a/src/raspberrypi/devices/scsi_host_bridge.cpp b/src/raspberrypi/devices/scsi_host_bridge.cpp index 35fd2a5d..0ddeb9ed 100644 --- a/src/raspberrypi/devices/scsi_host_bridge.cpp +++ b/src/raspberrypi/devices/scsi_host_bridge.cpp @@ -140,12 +140,6 @@ int SCSIBR::Inquiry(const DWORD *cdb, BYTE *buf) // buf[4] ... Inquiry additional data memset(buf, 0, 8); buf[0] = 0x09; - - // SCSI-2 p.104 4.4.3 Incorrect logical unit handling - if (((cdb[1] >> 5) & 0x07) != GetLun()) { - buf[0] = 0x7f; - } - buf[2] = 0x02; buf[3] = 0x02; buf[4] = 36 - 5 + 8; // required + 8 byte extension diff --git a/src/raspberrypi/devices/scsicd.cpp b/src/raspberrypi/devices/scsicd.cpp index 75b362cb..bf829f06 100644 --- a/src/raspberrypi/devices/scsicd.cpp +++ b/src/raspberrypi/devices/scsicd.cpp @@ -583,12 +583,6 @@ int SCSICD::Inquiry(const DWORD *cdb, BYTE *buf) // buf[4] ... Inquiry additional data memset(buf, 0, 8); buf[0] = 0x05; - - // SCSI-2 p.104 4.4.3 Incorrect logical unit handling - if (((cdb[1] >> 5) & 0x07) != GetLun()) { - buf[0] = 0x7f; - } - buf[1] = 0x80; buf[2] = 0x02; buf[3] = 0x02; diff --git a/src/raspberrypi/devices/scsihd.cpp b/src/raspberrypi/devices/scsihd.cpp index f3092707..d80ab6de 100644 --- a/src/raspberrypi/devices/scsihd.cpp +++ b/src/raspberrypi/devices/scsihd.cpp @@ -126,12 +126,6 @@ int SCSIHD::Inquiry(const DWORD *cdb, BYTE *buf) // buf[3] ... SCSI-2 compliant Inquiry response // buf[4] ... Inquiry additional data memset(buf, 0, 8); - - // SCSI-2 p.104 4.4.3 Incorrect logical unit handling - if (((cdb[1] >> 5) & 0x07) != GetLun()) { - buf[0] = 0x7f; - } - buf[1] = IsRemovable() ? 0x80 : 0x00; buf[2] = 0x02; buf[3] = 0x02; diff --git a/src/raspberrypi/devices/scsimo.cpp b/src/raspberrypi/devices/scsimo.cpp index a38e7828..24764058 100644 --- a/src/raspberrypi/devices/scsimo.cpp +++ b/src/raspberrypi/devices/scsimo.cpp @@ -125,12 +125,6 @@ int SCSIMO::Inquiry(const DWORD *cdb, BYTE *buf) // buf[4] ... Inquiry additional data memset(buf, 0, 8); buf[0] = 0x07; - - // SCSI-2 p.104 4.4.3 Incorrect logical unit handling - if (((cdb[1] >> 5) & 0x07) != GetLun()) { - buf[0] = 0x7f; - } - buf[1] = 0x80; buf[2] = 0x02; buf[3] = 0x02;