dingusppc/devices/common/scsi/scsidevice.cpp

251 lines
8.3 KiB
C++
Raw Normal View History

2022-10-25 00:10:54 +00:00
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-23 divingkatae and maximum
2022-10-25 00:10:54 +00:00
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <core/timermanager.h>
#include <devices/common/scsi/scsi.h>
#include <loguru.hpp>
#include <cinttypes>
#include <cstring>
2022-10-25 00:10:54 +00:00
void ScsiDevice::notify(ScsiMsg msg_type, int param)
2022-10-25 00:10:54 +00:00
{
if (msg_type == ScsiMsg::BUS_PHASE_CHANGE) {
switch (param) {
case ScsiPhase::RESET:
2022-11-07 23:33:38 +00:00
LOG_F(9, "ScsiDevice %d: bus reset aknowledged", this->scsi_id);
2022-10-25 00:10:54 +00:00
break;
case ScsiPhase::SELECTION:
// check if something tries to select us
if (this->bus_obj->get_data_lines() & (1 << scsi_id)) {
2022-11-07 23:33:38 +00:00
LOG_F(9, "ScsiDevice %d selected", this->scsi_id);
2022-10-25 00:10:54 +00:00
TimerManager::get_instance()->add_oneshot_timer(
BUS_SETTLE_DELAY,
[this]() {
2022-10-25 00:10:54 +00:00
// don't confirm selection if BSY or I/O are asserted
if (this->bus_obj->test_ctrl_lines(SCSI_CTRL_BSY | SCSI_CTRL_IO))
2022-10-25 00:10:54 +00:00
return;
this->bus_obj->assert_ctrl_line(this->scsi_id, SCSI_CTRL_BSY);
this->bus_obj->confirm_selection(this->scsi_id);
this->initiator_id = this->bus_obj->get_initiator_id();
if (this->bus_obj->test_ctrl_lines(SCSI_CTRL_ATN)) {
2022-11-07 20:56:01 +00:00
this->switch_phase(ScsiPhase::MESSAGE_OUT);
this->last_selection_has_atention = true;
this->last_selection_message = this->msg_buf[0];
2024-03-13 04:30:38 +00:00
//LOG_F(SCSIDEVICE, "%s: received message:0x%02x", this->get_name().c_str(), this->msg_buf[0]);
2022-11-07 11:24:02 +00:00
} else {
this->last_selection_has_atention = false;
2023-11-03 09:50:50 +00:00
this->switch_phase(ScsiPhase::COMMAND);
2022-11-07 11:24:02 +00:00
}
2022-10-25 00:10:54 +00:00
});
}
break;
}
}
}
2022-11-02 20:28:30 +00:00
2022-11-07 20:56:01 +00:00
void ScsiDevice::switch_phase(const int new_phase)
{
this->cur_phase = new_phase;
this->bus_obj->switch_phase(this->scsi_id, this->cur_phase);
}
void ScsiDevice::next_step()
2022-11-02 20:28:30 +00:00
{
switch (this->cur_phase) {
2022-11-07 20:56:01 +00:00
case ScsiPhase::DATA_OUT:
if (this->data_size >= this->incoming_size) {
if (this->post_xfer_action != nullptr) {
this->post_xfer_action();
}
this->switch_phase(ScsiPhase::STATUS);
}
2022-11-07 11:24:02 +00:00
break;
2022-11-02 20:28:30 +00:00
case ScsiPhase::DATA_IN:
2022-11-07 11:24:02 +00:00
if (!this->has_data()) {
2022-11-07 20:56:01 +00:00
this->switch_phase(ScsiPhase::STATUS);
2022-11-02 20:28:30 +00:00
}
break;
2023-11-03 09:50:50 +00:00
case ScsiPhase::COMMAND:
this->process_command();
if (this->cur_phase != ScsiPhase::COMMAND) {
if (this->prepare_data()) {
this->bus_obj->assert_ctrl_line(this->scsi_id, SCSI_CTRL_REQ);
} else {
ABORT_F("ScsiDevice: prepare_data() failed");
}
}
break;
2022-11-02 20:28:30 +00:00
case ScsiPhase::STATUS:
2022-11-07 20:56:01 +00:00
this->switch_phase(ScsiPhase::MESSAGE_IN);
2022-11-02 20:28:30 +00:00
break;
2023-11-03 09:50:50 +00:00
case ScsiPhase::MESSAGE_OUT:
this->switch_phase(ScsiPhase::COMMAND);
break;
2022-11-02 20:28:30 +00:00
case ScsiPhase::MESSAGE_IN:
case ScsiPhase::BUS_FREE:
this->bus_obj->release_ctrl_lines(this->scsi_id);
2022-11-07 20:56:01 +00:00
this->switch_phase(ScsiPhase::BUS_FREE);
2022-11-02 20:28:30 +00:00
break;
default:
2022-11-07 11:24:02 +00:00
LOG_F(WARNING, "ScsiDevice: nothing to do for phase %d", this->cur_phase);
}
}
void ScsiDevice::prepare_xfer(ScsiBus* bus_obj, int& bytes_in, int& bytes_out)
{
this->cur_phase = bus_obj->current_phase();
switch (this->cur_phase) {
case ScsiPhase::COMMAND:
this->data_ptr = this->cmd_buf;
this->data_size = 0;
2023-11-03 09:50:50 +00:00
bytes_out = 0;
2022-11-07 11:24:02 +00:00
break;
case ScsiPhase::STATUS:
this->data_ptr = &this->status;
this->data_size = 1;
bytes_out = 1;
break;
case ScsiPhase::DATA_IN:
bytes_out = this->data_size;
break;
2022-11-07 20:56:01 +00:00
case ScsiPhase::DATA_OUT:
break;
2022-11-07 11:24:02 +00:00
case ScsiPhase::MESSAGE_OUT:
this->data_ptr = this->msg_buf;
this->data_size = bytes_in;
2023-11-03 09:50:50 +00:00
bytes_out = 0;
2022-11-07 11:24:02 +00:00
break;
case ScsiPhase::MESSAGE_IN:
this->data_ptr = this->msg_buf;
this->data_size = 1;
bytes_out = 1;
break;
default:
ABORT_F("ScsiDevice: unhandled phase %d in prepare_xfer()", this->cur_phase);
2022-11-02 20:28:30 +00:00
}
}
2022-11-07 11:24:02 +00:00
2023-11-03 09:50:50 +00:00
static const int CmdGroupLen[8] = {6, 10, 10, -1, -1, 12, -1, -1};
int ScsiDevice::xfer_data() {
this->cur_phase = bus_obj->current_phase();
switch (this->cur_phase) {
case ScsiPhase::MESSAGE_OUT:
if (this->bus_obj->pull_data(this->initiator_id, this->msg_buf, 1)) {
if (this->msg_buf[0] & 0x80) {
LOG_F(9, "%s: IDENTIFY MESSAGE received, code = 0x%X",
this->name.c_str(), this->msg_buf[0]);
this->next_step();
} else {
ABORT_F("%s: unsupported message received, code = 0x%X",
this->name.c_str(), this->msg_buf[0]);
}
}
break;
case ScsiPhase::COMMAND:
if (this->bus_obj->pull_data(this->initiator_id, this->cmd_buf, 1)) {
int cmd_len = CmdGroupLen[this->cmd_buf[0] >> 5];
if (cmd_len < 0) {
ABORT_F("%s: unsupported command received, code = 0x%X",
this->name.c_str(), this->msg_buf[0]);
}
if (this->bus_obj->pull_data(this->initiator_id, &this->cmd_buf[1], cmd_len - 1))
this->next_step();
}
break;
default:
ABORT_F("ScsiDevice: unhandled phase %d in xfer_data()", this->cur_phase);
}
return 0;
}
2022-11-07 11:24:02 +00:00
int ScsiDevice::send_data(uint8_t* dst_ptr, const int count)
{
if (dst_ptr == nullptr || !count) {
return 0;
}
int actual_count = std::min(this->data_size, count);
std::memcpy(dst_ptr, this->data_ptr, actual_count);
this->data_ptr += actual_count;
this->data_size -= actual_count;
// attempt to return the requested amount of data
// when data_size drops down to zero
if (!this->data_size) {
if (this->get_more_data() && count > actual_count) {
dst_ptr += actual_count;
actual_count = std::min(this->data_size, count - actual_count);
std::memcpy(dst_ptr, this->data_ptr, actual_count);
this->data_ptr += actual_count;
this->data_size -= actual_count;
}
}
2022-11-07 11:24:02 +00:00
return actual_count;
}
int ScsiDevice::rcv_data(const uint8_t* src_ptr, const int count)
{
2022-11-07 20:56:01 +00:00
// accumulating incoming data in the pre-configured buffer
std::memcpy(this->data_ptr, src_ptr, count);
this->data_ptr += count;
this->data_size += count;
2022-11-07 11:24:02 +00:00
2023-11-03 09:50:50 +00:00
if (this->cur_phase == ScsiPhase::COMMAND)
this->next_step();
2022-11-07 11:24:02 +00:00
return count;
}
bool ScsiDevice::check_lun()
{
if (this->cmd_buf[1] >> 5 != this->lun) {
LOG_F(ERROR, "%s: non-matching LUN", this->name.c_str());
this->status = ScsiStatus::CHECK_CONDITION;
this->sense = ScsiSense::ILLEGAL_REQ;
this->asc = 0x25; // Logical Unit Not Supported
this->ascq = 0;
this->sksv = 0;
this->field = 0;
this->switch_phase(ScsiPhase::STATUS);
return false;
}
return true;
}
void ScsiDevice::illegal_command(const uint8_t *cmd)
{
LOG_F(ERROR, "%s: unsupported command: 0x%02x", this->name.c_str(), cmd[0]);
this->status = ScsiStatus::CHECK_CONDITION;
this->sense = ScsiSense::ILLEGAL_REQ;
this->asc = 0x20; // Invalid command operation code
this->ascq = 0;
this->sksv = 0xc0; // sksv=1, C/D=Command, BPV=0, BP=0
this->field = 0;
this->switch_phase(ScsiPhase::STATUS);
}