2019-08-18 03:43:42 +00:00
|
|
|
//
|
2019-08-23 03:16:58 +00:00
|
|
|
// Target.hpp
|
2019-08-18 03:43:42 +00:00
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 17/08/2019.
|
|
|
|
// Copyright © 2019 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2019-08-23 03:16:58 +00:00
|
|
|
#ifndef SCSI_Target_hpp
|
|
|
|
#define SCSI_Target_hpp
|
2019-08-18 03:43:42 +00:00
|
|
|
|
|
|
|
#include "SCSI.hpp"
|
2019-09-19 01:41:29 +00:00
|
|
|
#include "../../../Outputs/Log.hpp"
|
2019-08-18 03:43:42 +00:00
|
|
|
|
2019-09-19 02:06:13 +00:00
|
|
|
#include <cassert>
|
2019-09-09 01:59:56 +00:00
|
|
|
#include <cstring>
|
2019-08-22 02:37:39 +00:00
|
|
|
#include <functional>
|
|
|
|
|
2019-08-18 03:43:42 +00:00
|
|
|
namespace SCSI {
|
2019-08-20 02:47:01 +00:00
|
|
|
namespace Target {
|
2019-08-18 03:43:42 +00:00
|
|
|
|
|
|
|
/*!
|
2019-08-20 02:47:01 +00:00
|
|
|
Encapsulates the arguments supplied for a target SCSI command during
|
2019-08-22 02:37:39 +00:00
|
|
|
the command phase plus any other data read since then.
|
2019-08-18 03:43:42 +00:00
|
|
|
*/
|
2019-08-22 02:37:39 +00:00
|
|
|
class CommandState {
|
2019-08-20 02:47:01 +00:00
|
|
|
public:
|
2019-09-13 01:58:09 +00:00
|
|
|
CommandState(const std::vector<uint8_t> &command, const std::vector<uint8_t> &received);
|
2019-08-20 02:47:01 +00:00
|
|
|
|
2019-09-08 02:04:44 +00:00
|
|
|
// For read and write commands.
|
2019-09-17 02:20:42 +00:00
|
|
|
struct ReadWrite {
|
2019-09-18 01:59:32 +00:00
|
|
|
uint32_t address, number_of_blocks;
|
2019-09-17 02:20:42 +00:00
|
|
|
};
|
|
|
|
ReadWrite read_write_specs() const;
|
2019-08-20 02:47:01 +00:00
|
|
|
|
2019-09-08 02:04:44 +00:00
|
|
|
// For inquiry commands.
|
|
|
|
size_t allocated_inquiry_bytes() const;
|
|
|
|
|
2019-09-09 01:59:56 +00:00
|
|
|
// For mode sense commands.
|
|
|
|
struct ModeSense {
|
|
|
|
bool exclude_block_descriptors = false;
|
|
|
|
enum class PageControlValues {
|
|
|
|
Current = 0,
|
|
|
|
Changeable = 1,
|
|
|
|
Default = 2,
|
|
|
|
Saved = 3
|
|
|
|
} page_control_values = PageControlValues::Current;
|
|
|
|
uint8_t page_code;
|
|
|
|
uint8_t subpage_code;
|
|
|
|
uint16_t allocated_bytes;
|
|
|
|
};
|
|
|
|
ModeSense mode_sense_specs() const;
|
|
|
|
|
2019-09-13 01:58:09 +00:00
|
|
|
struct ModeSelect {
|
|
|
|
bool content_is_vendor_specific = true;
|
|
|
|
bool revert_to_default = false;
|
|
|
|
bool save_pages = false;
|
|
|
|
uint16_t parameter_list_length = 0;
|
|
|
|
};
|
|
|
|
ModeSelect mode_select_specs() const;
|
|
|
|
|
2019-09-12 01:52:02 +00:00
|
|
|
struct ReadBuffer {
|
|
|
|
enum class Mode {
|
|
|
|
CombinedHeaderAndData = 0,
|
|
|
|
VendorSpecific = 1,
|
|
|
|
Data = 2,
|
|
|
|
Descriptor = 3,
|
|
|
|
Reserved = 4
|
|
|
|
} mode = Mode::CombinedHeaderAndData;
|
|
|
|
uint8_t buffer_id = 0;
|
|
|
|
uint32_t buffer_offset = 0, buffer_length = 0;
|
|
|
|
};
|
|
|
|
ReadBuffer read_buffer_specs() const;
|
|
|
|
|
2019-09-16 02:06:45 +00:00
|
|
|
const std::vector<uint8_t> &received_data() const {
|
2019-09-13 01:58:09 +00:00
|
|
|
return received_;
|
|
|
|
}
|
|
|
|
|
2019-08-20 02:47:01 +00:00
|
|
|
private:
|
2019-09-17 02:20:42 +00:00
|
|
|
uint32_t address() const;
|
|
|
|
uint16_t number_of_blocks() const;
|
2019-08-20 02:47:01 +00:00
|
|
|
const std::vector<uint8_t> &data_;
|
2019-09-13 01:58:09 +00:00
|
|
|
const std::vector<uint8_t> &received_;
|
2019-08-20 02:47:01 +00:00
|
|
|
};
|
|
|
|
|
2019-08-22 02:37:39 +00:00
|
|
|
/*!
|
|
|
|
A Responder is supplied both (i) to the initial call-in to an Executor; and
|
|
|
|
(ii) to all continuations provided by that Executor. It allows the next
|
|
|
|
set of bus interactions to be dictated.
|
|
|
|
*/
|
|
|
|
struct Responder {
|
|
|
|
using continuation = std::function<void(const CommandState &, Responder &)>;
|
|
|
|
|
|
|
|
enum class Status {
|
|
|
|
Good = 0x00,
|
|
|
|
CheckCondition = 0x02,
|
|
|
|
ConditionMet = 0x04,
|
|
|
|
Busy = 0x08,
|
|
|
|
Intermediate = 0x10,
|
|
|
|
IntermediateConditionMet = 0x14,
|
|
|
|
ReservationConflict = 0x18,
|
|
|
|
CommandTerminated = 0x22,
|
|
|
|
TaskSetFull = 0x28,
|
|
|
|
ACAActive = 0x30,
|
|
|
|
TaskAborted = 0x40
|
|
|
|
};
|
|
|
|
|
|
|
|
enum class Message {
|
|
|
|
CommandComplete = 0x00
|
|
|
|
};
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Causes the SCSI device to send @c data to the initiator and
|
|
|
|
call @c next when done.
|
|
|
|
*/
|
|
|
|
virtual void send_data(std::vector<uint8_t> &&data, continuation next) = 0;
|
|
|
|
/*!
|
|
|
|
Causes the SCSI device to receive @c length bytes from the initiator and
|
|
|
|
call @c next when done. The bytes will be accessible via the CommandInput object.
|
|
|
|
*/
|
|
|
|
virtual void receive_data(size_t length, continuation next) = 0;
|
|
|
|
/*!
|
|
|
|
Communicates the supplied status to the initiator.
|
|
|
|
*/
|
|
|
|
virtual void send_status(Status, continuation next) = 0;
|
|
|
|
/*!
|
|
|
|
Communicates the supplied message to the initiator.
|
|
|
|
*/
|
|
|
|
virtual void send_message(Message, continuation next) = 0;
|
|
|
|
/*!
|
|
|
|
Ends the SCSI command.
|
|
|
|
*/
|
|
|
|
virtual void end_command() = 0;
|
2019-09-04 02:40:18 +00:00
|
|
|
/*!
|
|
|
|
Terminates a SCSI command, sending the proper sequence of status and message phases.
|
|
|
|
*/
|
|
|
|
void terminate_command(Status status) {
|
2020-05-30 04:37:06 +00:00
|
|
|
send_status(status, [] (const Target::CommandState &, Target::Responder &responder) {
|
|
|
|
responder.send_message(Target::Responder::Message::CommandComplete, [] (const Target::CommandState &, Target::Responder &responder) {
|
2019-09-04 02:40:18 +00:00
|
|
|
responder.end_command();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2019-08-22 02:37:39 +00:00
|
|
|
};
|
|
|
|
|
2019-08-20 02:47:01 +00:00
|
|
|
/*!
|
|
|
|
Executors contain device-specific logic; when the target has completed
|
|
|
|
the command phase it will call the appropriate method on its executor,
|
|
|
|
supplying it with the command's arguments.
|
|
|
|
|
|
|
|
If you implement a method, you should push a result and return @c true.
|
|
|
|
Return @c false if you do not implement a method (or, just inherit from
|
|
|
|
the basic executor below, and don't implement anything you don't support).
|
|
|
|
*/
|
|
|
|
struct Executor {
|
|
|
|
/* Group 0 commands. */
|
2019-09-04 02:40:18 +00:00
|
|
|
bool test_unit_ready(const CommandState &, Responder &responder) {
|
|
|
|
/* "Returns zero status if addressed unit is powered on and ready. */
|
|
|
|
responder.terminate_command(Target::Responder::Status::Good);
|
|
|
|
return true;
|
|
|
|
}
|
2019-08-22 02:37:39 +00:00
|
|
|
bool rezero_unit(const CommandState &, Responder &) { return false; }
|
|
|
|
bool request_sense(const CommandState &, Responder &) { return false; }
|
|
|
|
bool format_unit(const CommandState &, Responder &) { return false; }
|
|
|
|
bool seek(const CommandState &, Responder &) { return false; }
|
|
|
|
bool reserve_unit(const CommandState &, Responder &) { return false; }
|
|
|
|
bool release_unit(const CommandState &, Responder &) { return false; }
|
|
|
|
bool read_diagnostic(const CommandState &, Responder &) { return false; }
|
|
|
|
bool write_diagnostic(const CommandState &, Responder &) { return false; }
|
2019-09-09 01:59:56 +00:00
|
|
|
|
|
|
|
/// Mode sense: the default implementation will call into the appropriate
|
2019-09-12 01:52:02 +00:00
|
|
|
/// structured getter.
|
2019-09-09 01:59:56 +00:00
|
|
|
bool mode_sense(const CommandState &state, Responder &responder) {
|
|
|
|
const auto specs = state.mode_sense_specs();
|
|
|
|
std::vector<uint8_t> response = {
|
|
|
|
specs.page_code,
|
|
|
|
uint8_t(specs.allocated_bytes)
|
|
|
|
};
|
|
|
|
switch(specs.page_code) {
|
|
|
|
default:
|
|
|
|
printf("Unknown mode sense page code %02x\n", specs.page_code);
|
|
|
|
response.resize(specs.allocated_bytes);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x30:
|
|
|
|
response.resize(34);
|
|
|
|
strcpy(reinterpret_cast<char *>(&response[14]), "APPLE COMPUTER, INC"); // This seems to be required to satisfy the Apple HD SC Utility.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(specs.allocated_bytes < response.size()) {
|
|
|
|
response.resize(specs.allocated_bytes);
|
|
|
|
}
|
2020-05-30 04:37:06 +00:00
|
|
|
responder.send_data(std::move(response), [] (const Target::CommandState &, Target::Responder &responder) {
|
2019-09-09 01:59:56 +00:00
|
|
|
responder.terminate_command(Target::Responder::Status::Good);
|
|
|
|
});
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-09-13 01:58:09 +00:00
|
|
|
bool mode_select(const CommandState &state, Responder &responder) {
|
|
|
|
const auto specs = state.mode_select_specs();
|
|
|
|
|
2020-05-30 04:37:06 +00:00
|
|
|
responder.receive_data(specs.parameter_list_length, [] (const Target::CommandState &, Target::Responder &responder) {
|
2019-09-13 01:58:09 +00:00
|
|
|
// TODO: parse data according to current sense mode.
|
|
|
|
responder.terminate_command(Target::Responder::Status::Good);
|
|
|
|
});
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-09-09 01:59:56 +00:00
|
|
|
/// Inquiry: the default implementation will call the structured version and
|
|
|
|
/// package appropriately.
|
|
|
|
struct Inquiry {
|
|
|
|
enum class DeviceType {
|
|
|
|
DirectAccess = 0,
|
|
|
|
SequentialAccess = 1,
|
|
|
|
Printer = 2,
|
|
|
|
Processor = 3,
|
|
|
|
WriteOnceMultipleRead = 4,
|
|
|
|
ReadOnlyDirectAccess = 5,
|
|
|
|
Scanner = 6,
|
|
|
|
OpticalMemory = 7,
|
|
|
|
MediumChanger = 8,
|
|
|
|
Communications = 9,
|
|
|
|
} device_type = DeviceType::DirectAccess;
|
|
|
|
bool is_removeable = false;
|
|
|
|
uint8_t iso_standard = 0, ecma_standard = 0, ansi_standard = 0;
|
|
|
|
bool supports_asynchronous_events = false;
|
|
|
|
bool supports_terminate_io_process = false;
|
|
|
|
bool supports_relative_addressing = false;
|
|
|
|
bool supports_synchronous_transfer = true;
|
|
|
|
bool supports_linked_commands = false;
|
|
|
|
bool supports_command_queing = false;
|
|
|
|
bool supports_soft_reset = false;
|
|
|
|
char vendor_identifier[9] = "";
|
|
|
|
char product_identifier[17] = "";
|
|
|
|
char product_revision_level[5] = "";
|
|
|
|
|
|
|
|
Inquiry(const char *vendor, const char *product, const char *revision) {
|
|
|
|
assert(strlen(vendor) <= 8);
|
|
|
|
assert(strlen(product) <= 16);
|
|
|
|
assert(strlen(revision) <= 4);
|
|
|
|
strcpy(vendor_identifier, vendor);
|
|
|
|
strcpy(product_identifier, product);
|
|
|
|
strcpy(product_revision_level, revision);
|
|
|
|
}
|
|
|
|
Inquiry() = default;
|
|
|
|
};
|
|
|
|
Inquiry inquiry_values() {
|
|
|
|
return Inquiry();
|
|
|
|
}
|
|
|
|
bool inquiry(const CommandState &state, Responder &responder) {
|
|
|
|
const Inquiry inq = inquiry_values();
|
|
|
|
|
|
|
|
// Set up the easy fields.
|
|
|
|
std::vector<uint8_t> response = {
|
|
|
|
uint8_t(inq.device_type),
|
|
|
|
uint8_t(inq.is_removeable ? 0x80 : 0x00),
|
|
|
|
uint8_t((inq.iso_standard << 5) | (inq.ecma_standard << 3) | (inq.ansi_standard)),
|
|
|
|
uint8_t((inq.supports_asynchronous_events ? 0x80 : 0x00) | (inq.supports_terminate_io_process ? 0x40 : 0x00) | 0x02),
|
|
|
|
32, /* Additional length: 36 - 4. */
|
|
|
|
0x00, /* Reserved. */
|
|
|
|
0x00, /* Reserved. */
|
|
|
|
uint8_t(
|
|
|
|
(inq.supports_relative_addressing ? 0x80 : 0x00) |
|
|
|
|
/* b6: supports 32-bit data; b5: supports 16-bit data. */
|
|
|
|
(inq.supports_synchronous_transfer ? 0x10 : 0x00) |
|
|
|
|
(inq.supports_linked_commands ? 0x08 : 0x00) |
|
|
|
|
/* b3: reserved. */
|
|
|
|
(inq.supports_command_queing ? 0x02 : 0x00) |
|
|
|
|
(inq.supports_soft_reset ? 0x01 : 0x00)
|
|
|
|
),
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Space for the vendor ID. */
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Space for the product ID. */
|
|
|
|
0x00, 0x00, 0x00, 0x00 /* Space for the revision level. */
|
|
|
|
};
|
|
|
|
|
|
|
|
auto copy_string = [] (uint8_t *destination, const char *source, size_t length) -> void {
|
2020-05-28 01:31:46 +00:00
|
|
|
// Determine length of source and copy in as much as possible.
|
|
|
|
const auto source_length = std::min(strlen(source), length);
|
|
|
|
memcpy(destination, source, source_length);
|
|
|
|
|
|
|
|
// Fill the rest with spaces.
|
|
|
|
memset(&destination[source_length], ' ', length - source_length);
|
2019-09-09 01:59:56 +00:00
|
|
|
};
|
|
|
|
copy_string(&response[8], inq.vendor_identifier, 8);
|
|
|
|
copy_string(&response[16], inq.product_identifier, 16);
|
|
|
|
copy_string(&response[32], inq.product_revision_level, 4);
|
|
|
|
|
|
|
|
// Truncate if requested.
|
|
|
|
const auto allocated_bytes = state.allocated_inquiry_bytes();
|
|
|
|
if(allocated_bytes < response.size()) {
|
|
|
|
response.resize(allocated_bytes);
|
|
|
|
}
|
|
|
|
|
2020-05-30 04:37:06 +00:00
|
|
|
responder.send_data(std::move(response), [] (const Target::CommandState &, Target::Responder &responder) {
|
2019-09-09 01:59:56 +00:00
|
|
|
responder.terminate_command(Target::Responder::Status::Good);
|
|
|
|
});
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2019-08-20 02:47:01 +00:00
|
|
|
|
|
|
|
/* Group 0/1 commands. */
|
2019-08-22 02:37:39 +00:00
|
|
|
bool read(const CommandState &, Responder &) { return false; }
|
|
|
|
bool write(const CommandState &, Responder &) { return false; }
|
2019-08-20 02:47:01 +00:00
|
|
|
|
|
|
|
/* Group 1 commands. */
|
2019-08-22 02:37:39 +00:00
|
|
|
bool read_capacity(const CommandState &, Responder &) { return false; }
|
|
|
|
bool write_and_verify(const CommandState &, Responder &) { return false; }
|
|
|
|
bool verify(const CommandState &, Responder &) { return false; }
|
|
|
|
bool search_data_equal(const CommandState &, Responder &) { return false; }
|
|
|
|
bool search_data_high(const CommandState &, Responder &) { return false; }
|
|
|
|
bool search_data_low(const CommandState &, Responder &) { return false; }
|
2019-09-12 01:52:02 +00:00
|
|
|
bool read_buffer(const CommandState &state, Responder &responder) {
|
|
|
|
// Since I have no idea what earthly function READ BUFFER is meant to allow,
|
|
|
|
// the default implementation just returns an empty buffer of the requested size.
|
|
|
|
const auto specs = state.read_buffer_specs();
|
|
|
|
responder.send_data(std::vector<uint8_t>(specs.buffer_length), [] (const Target::CommandState &, Target::Responder &responder) {
|
|
|
|
responder.terminate_command(Target::Responder::Status::Good);
|
|
|
|
});
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2019-08-20 02:47:01 +00:00
|
|
|
|
|
|
|
/* Group 5 commands. */
|
2019-08-22 02:37:39 +00:00
|
|
|
bool set_block_limits(const CommandState &, Responder &) { return false; }
|
2019-08-20 02:47:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*!
|
|
|
|
A template for any SCSI target; provides the necessary bus glue to
|
|
|
|
receive and respond to commands. Specific targets should be implemented
|
|
|
|
as Executors.
|
|
|
|
*/
|
2019-08-22 02:37:39 +00:00
|
|
|
template <typename Executor> class Target: public Bus::Observer, public Responder {
|
2019-08-18 03:43:42 +00:00
|
|
|
public:
|
|
|
|
/*!
|
2019-08-20 02:47:01 +00:00
|
|
|
Instantiates a target attached to @c bus,
|
2019-08-18 03:43:42 +00:00
|
|
|
with SCSI ID @c scsi_id — a number in the range 0 to 7.
|
2019-08-20 02:47:01 +00:00
|
|
|
|
|
|
|
Received commands will be handed to the Executor to perform.
|
2019-08-18 03:43:42 +00:00
|
|
|
*/
|
2019-08-20 02:47:01 +00:00
|
|
|
Target(Bus &bus, int scsi_id);
|
|
|
|
|
2019-08-25 21:03:41 +00:00
|
|
|
inline Executor *operator->() {
|
|
|
|
return &executor_;
|
|
|
|
}
|
2019-08-18 03:43:42 +00:00
|
|
|
|
|
|
|
private:
|
2019-08-25 21:03:41 +00:00
|
|
|
Executor executor_;
|
|
|
|
|
2019-08-22 02:37:39 +00:00
|
|
|
// Bus::Observer.
|
2019-09-01 01:44:22 +00:00
|
|
|
void scsi_bus_did_change(Bus *, BusState new_state, double time_since_change) final;
|
2019-08-18 03:43:42 +00:00
|
|
|
|
2019-08-22 02:37:39 +00:00
|
|
|
// Responder
|
|
|
|
void send_data(std::vector<uint8_t> &&data, continuation next) final;
|
|
|
|
void receive_data(size_t length, continuation next) final;
|
|
|
|
void send_status(Status, continuation next) final;
|
|
|
|
void send_message(Message, continuation next) final;
|
|
|
|
void end_command() final;
|
|
|
|
|
|
|
|
// Instance storage.
|
2019-08-18 03:43:42 +00:00
|
|
|
Bus &bus_;
|
|
|
|
const BusState scsi_id_mask_;
|
|
|
|
const size_t scsi_bus_device_id_;
|
|
|
|
|
2019-08-18 19:10:07 +00:00
|
|
|
enum class Phase {
|
|
|
|
AwaitingSelection,
|
2019-08-23 03:16:58 +00:00
|
|
|
Command,
|
|
|
|
ReceivingData,
|
|
|
|
SendingData,
|
|
|
|
SendingStatus,
|
|
|
|
SendingMessage
|
2019-08-18 19:10:07 +00:00
|
|
|
} phase_ = Phase::AwaitingSelection;
|
2019-08-18 03:43:42 +00:00
|
|
|
BusState bus_state_ = DefaultBusState;
|
2019-08-20 02:47:01 +00:00
|
|
|
|
2019-09-01 01:44:22 +00:00
|
|
|
void set_device_output(BusState state) {
|
|
|
|
expected_control_state_ = state & (Line::Control | Line::Input | Line::Message);
|
|
|
|
bus_.set_device_output(scsi_bus_device_id_, state);
|
|
|
|
}
|
|
|
|
BusState expected_control_state_ = DefaultBusState;
|
|
|
|
|
2019-08-20 02:47:01 +00:00
|
|
|
void begin_command(uint8_t first_byte);
|
|
|
|
std::vector<uint8_t> command_;
|
2019-09-04 01:15:30 +00:00
|
|
|
Status status_;
|
|
|
|
Message message_;
|
2019-08-20 02:47:01 +00:00
|
|
|
size_t command_pointer_ = 0;
|
|
|
|
bool dispatch_command();
|
2019-08-23 03:16:58 +00:00
|
|
|
|
|
|
|
std::vector<uint8_t> data_;
|
|
|
|
size_t data_pointer_ = 0;
|
|
|
|
|
|
|
|
continuation next_function_;
|
2019-08-18 03:43:42 +00:00
|
|
|
};
|
|
|
|
|
2019-09-19 01:53:22 +00:00
|
|
|
#include "TargetImplementation.hpp"
|
2019-08-20 02:47:01 +00:00
|
|
|
|
|
|
|
}
|
2019-08-18 03:43:42 +00:00
|
|
|
}
|
|
|
|
|
2019-08-23 03:16:58 +00:00
|
|
|
#endif /* SCSI_Target_hpp */
|