2022-02-10 18:54:48 +00:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
2022-12-05 17:58:23 +00:00
|
|
|
// SCSI Target Emulator PiSCSI
|
2022-02-10 18:54:48 +00:00
|
|
|
// for Raspberry Pi
|
|
|
|
//
|
2023-10-15 06:38:15 +00:00
|
|
|
// Copyright (C) 2022-2023 Uwe Seimet
|
2022-02-10 18:54:48 +00:00
|
|
|
//
|
|
|
|
// A device implementing mandatory SCSI primary commands, to be used for subclassing
|
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2022-11-10 06:44:06 +00:00
|
|
|
#include "shared/scsi.h"
|
2022-02-10 18:54:48 +00:00
|
|
|
#include "interfaces/scsi_primary_commands.h"
|
2022-10-04 15:23:42 +00:00
|
|
|
#include "controllers/abstract_controller.h"
|
2022-02-10 18:54:48 +00:00
|
|
|
#include "device.h"
|
2022-11-11 20:08:48 +00:00
|
|
|
#include "device_logger.h"
|
2022-02-10 18:54:48 +00:00
|
|
|
#include <string>
|
2022-11-02 14:36:19 +00:00
|
|
|
#include <unordered_map>
|
2023-10-15 06:38:15 +00:00
|
|
|
#include <span>
|
2022-11-02 14:36:19 +00:00
|
|
|
#include <functional>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace scsi_defs;
|
2022-02-10 18:54:48 +00:00
|
|
|
|
2022-10-08 17:26:04 +00:00
|
|
|
class PrimaryDevice: private ScsiPrimaryCommands, public Device
|
2022-02-10 18:54:48 +00:00
|
|
|
{
|
2023-10-15 06:38:15 +00:00
|
|
|
friend class AbstractController;
|
|
|
|
|
2022-11-02 14:36:19 +00:00
|
|
|
using operation = function<void()>;
|
|
|
|
|
2022-02-10 18:54:48 +00:00
|
|
|
public:
|
|
|
|
|
2022-11-02 14:36:19 +00:00
|
|
|
PrimaryDevice(PbDeviceType type, int lun) : Device(type, lun) {}
|
2022-09-07 14:38:42 +00:00
|
|
|
~PrimaryDevice() override = default;
|
2022-02-10 18:54:48 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
virtual bool Init(const param_map&);
|
|
|
|
virtual void CleanUp() {
|
|
|
|
// Override if cleanup work is required for a derived device
|
|
|
|
};
|
2022-11-02 14:36:19 +00:00
|
|
|
|
|
|
|
virtual void Dispatch(scsi_command);
|
2022-02-10 18:54:48 +00:00
|
|
|
|
2022-10-04 15:23:42 +00:00
|
|
|
int GetId() const override;
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
virtual bool WriteByteSequence(span<const uint8_t>);
|
2022-02-10 18:54:48 +00:00
|
|
|
|
2022-10-23 19:51:39 +00:00
|
|
|
int GetSendDelay() const { return send_delay; }
|
|
|
|
|
|
|
|
bool CheckReservation(int, scsi_command, bool) const;
|
|
|
|
void DiscardReservation();
|
|
|
|
|
|
|
|
void Reset() override;
|
2022-10-04 15:23:42 +00:00
|
|
|
|
|
|
|
virtual void FlushCache() {
|
2023-10-30 12:32:45 +00:00
|
|
|
// Devices with a cache have to override this method
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual vector<PbStatistics> GetStatistics() const {
|
|
|
|
// Devices which provide statistics have to override this method
|
|
|
|
|
|
|
|
return vector<PbStatistics>();
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
|
2022-02-10 18:54:48 +00:00
|
|
|
protected:
|
|
|
|
|
2022-11-02 14:36:19 +00:00
|
|
|
void AddCommand(scsi_command, const operation&);
|
|
|
|
|
|
|
|
vector<uint8_t> HandleInquiry(scsi_defs::device_type, scsi_level, bool) const;
|
|
|
|
virtual vector<uint8_t> InquiryInternal() const = 0;
|
SASI code removal, error handling update, bug fixes, code cleanup (#806)
Summary ov most important changes triggered by the SASI code removal:
- Removed the SASI controller code
- New controller management. There is a new controller base class AbstractController and a class ControllerManager managing the controller lifecycle. The lifecycle management was removed from rasci.cpp and is covered by unit tests.
- New device management. The DeviceFactory manages the device lifecycle instead of rascsi.cpp. The new code is covered by unit tests.
- The lifecycle managment uses C++ collections with variable size instead of arrays with hard-coded sizes.
- The ScsiController method contains most of what was previously contained in scsidev_ctrl.cpp plus the code from sasidev_ctrl.cpp that was relevant for SCSI.
- scsi_command_util contains helper methods used for identical SCSI command implementations of more than one device
- Devices know their controllers, so that the controller instance does not need to be passed to each SCSI command. This change helps to decouple the devices from the controller. The phase_handler interface is also part of this decoupling.
- Use scsi_command_exception for propagating SCSI command execution errors, This resolves issues with the previous error handling, which was based on return values and often on magic numbers.
- Removed legacy SCSI error codes, all errors are now encoded by sense_key::, asc:: and status::.
- Fixed various warnings reported with -Wextra, -Weffc++ and -Wpedantic.
- Use constructor member initialization lists (recommended for ISO C++)
- Consistently use new/delete instead of malloc/free (recommended for ISO C++), resulting in better type safety and error handling
- Replaced variable sized arrays on the stack (violates ISO C++ and can cause a stack overflow)
- Replaced NULL by nullptr (recommended for C++), resulting in better type safety
- Use more const member functions in order to avoid side effects
- The format device page can now also be changed for hard disk drives (Fujitsu M2624S supports this, for instance), not just for MOs.
- Better encapsulation, updated access specifiers in many places
- Removed unused methods and method arguments
- Fixed a number of TODOs
- Added/updated unit tests for a lot of non-legacy classes
- Makefile support for creating HTML coverage reports with lcov/genhtml
2022-09-03 14:53:53 +00:00
|
|
|
void CheckReady();
|
2022-02-13 19:30:02 +00:00
|
|
|
|
2022-10-23 19:51:39 +00:00
|
|
|
void SetSendDelay(int s) { send_delay = s; }
|
|
|
|
|
2022-11-02 14:36:19 +00:00
|
|
|
void SendDiagnostic() override;
|
|
|
|
void ReserveUnit() override;
|
|
|
|
void ReleaseUnit() override;
|
2022-10-23 19:51:39 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
void EnterStatusPhase() const { controller->Status(); }
|
|
|
|
void EnterDataInPhase() const { controller->DataIn(); }
|
|
|
|
void EnterDataOutPhase() const { controller->DataOut(); }
|
SASI code removal, error handling update, bug fixes, code cleanup (#806)
Summary ov most important changes triggered by the SASI code removal:
- Removed the SASI controller code
- New controller management. There is a new controller base class AbstractController and a class ControllerManager managing the controller lifecycle. The lifecycle management was removed from rasci.cpp and is covered by unit tests.
- New device management. The DeviceFactory manages the device lifecycle instead of rascsi.cpp. The new code is covered by unit tests.
- The lifecycle managment uses C++ collections with variable size instead of arrays with hard-coded sizes.
- The ScsiController method contains most of what was previously contained in scsidev_ctrl.cpp plus the code from sasidev_ctrl.cpp that was relevant for SCSI.
- scsi_command_util contains helper methods used for identical SCSI command implementations of more than one device
- Devices know their controllers, so that the controller instance does not need to be passed to each SCSI command. This change helps to decouple the devices from the controller. The phase_handler interface is also part of this decoupling.
- Use scsi_command_exception for propagating SCSI command execution errors, This resolves issues with the previous error handling, which was based on return values and often on magic numbers.
- Removed legacy SCSI error codes, all errors are now encoded by sense_key::, asc:: and status::.
- Fixed various warnings reported with -Wextra, -Weffc++ and -Wpedantic.
- Use constructor member initialization lists (recommended for ISO C++)
- Consistently use new/delete instead of malloc/free (recommended for ISO C++), resulting in better type safety and error handling
- Replaced variable sized arrays on the stack (violates ISO C++ and can cause a stack overflow)
- Replaced NULL by nullptr (recommended for C++), resulting in better type safety
- Use more const member functions in order to avoid side effects
- The format device page can now also be changed for hard disk drives (Fujitsu M2624S supports this, for instance), not just for MOs.
- Better encapsulation, updated access specifiers in many places
- Removed unused methods and method arguments
- Fixed a number of TODOs
- Added/updated unit tests for a lot of non-legacy classes
- Makefile support for creating HTML coverage reports with lcov/genhtml
2022-09-03 14:53:53 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
auto GetController() const { return controller; }
|
|
|
|
|
|
|
|
void LogTrace(const string& s) const { device_logger.Trace(s); }
|
|
|
|
void LogDebug(const string& s) const { device_logger.Debug(s); }
|
|
|
|
void LogInfo(const string& s) const { device_logger.Info(s); }
|
|
|
|
void LogWarn(const string& s) const { device_logger.Warn(s); }
|
|
|
|
void LogError(const string& s) const { device_logger.Error(s); }
|
2022-02-10 18:54:48 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2022-10-23 19:51:39 +00:00
|
|
|
static const int NOT_RESERVED = -2;
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
void SetController(AbstractController *);
|
|
|
|
|
2022-09-07 14:38:42 +00:00
|
|
|
void TestUnitReady() override;
|
|
|
|
void RequestSense() override;
|
|
|
|
void ReportLuns() override;
|
|
|
|
void Inquiry() override;
|
SASI code removal, error handling update, bug fixes, code cleanup (#806)
Summary ov most important changes triggered by the SASI code removal:
- Removed the SASI controller code
- New controller management. There is a new controller base class AbstractController and a class ControllerManager managing the controller lifecycle. The lifecycle management was removed from rasci.cpp and is covered by unit tests.
- New device management. The DeviceFactory manages the device lifecycle instead of rascsi.cpp. The new code is covered by unit tests.
- The lifecycle managment uses C++ collections with variable size instead of arrays with hard-coded sizes.
- The ScsiController method contains most of what was previously contained in scsidev_ctrl.cpp plus the code from sasidev_ctrl.cpp that was relevant for SCSI.
- scsi_command_util contains helper methods used for identical SCSI command implementations of more than one device
- Devices know their controllers, so that the controller instance does not need to be passed to each SCSI command. This change helps to decouple the devices from the controller. The phase_handler interface is also part of this decoupling.
- Use scsi_command_exception for propagating SCSI command execution errors, This resolves issues with the previous error handling, which was based on return values and often on magic numbers.
- Removed legacy SCSI error codes, all errors are now encoded by sense_key::, asc:: and status::.
- Fixed various warnings reported with -Wextra, -Weffc++ and -Wpedantic.
- Use constructor member initialization lists (recommended for ISO C++)
- Consistently use new/delete instead of malloc/free (recommended for ISO C++), resulting in better type safety and error handling
- Replaced variable sized arrays on the stack (violates ISO C++ and can cause a stack overflow)
- Replaced NULL by nullptr (recommended for C++), resulting in better type safety
- Use more const member functions in order to avoid side effects
- The format device page can now also be changed for hard disk drives (Fujitsu M2624S supports this, for instance), not just for MOs.
- Better encapsulation, updated access specifiers in many places
- Removed unused methods and method arguments
- Fixed a number of TODOs
- Added/updated unit tests for a lot of non-legacy classes
- Makefile support for creating HTML coverage reports with lcov/genhtml
2022-09-03 14:53:53 +00:00
|
|
|
|
2022-09-21 06:27:51 +00:00
|
|
|
vector<byte> HandleRequestSense() const;
|
2022-02-10 18:54:48 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
// TODO Try to remove this field and use controller->Log*() methods instead
|
|
|
|
DeviceLogger device_logger;
|
2022-11-11 20:08:48 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
// Owned by the controller manager
|
|
|
|
AbstractController *controller = nullptr;
|
2022-11-04 07:22:32 +00:00
|
|
|
|
2022-11-02 14:36:19 +00:00
|
|
|
unordered_map<scsi_command, operation> commands;
|
2022-10-23 19:51:39 +00:00
|
|
|
|
|
|
|
int send_delay = BUS::SEND_NO_DELAY;
|
|
|
|
|
|
|
|
int reserving_initiator = NOT_RESERVED;
|
2022-02-10 18:54:48 +00:00
|
|
|
};
|