2017-08-22 21:28:22 +02:00
|
|
|
/*
|
|
|
|
|
* 86Box A hypervisor and IBM PC system emulator that specializes in
|
|
|
|
|
* running old operating systems and software designed for IBM
|
|
|
|
|
* PC systems and compatibles from 1981 through fairly recent
|
|
|
|
|
* system designs based on the PCI bus.
|
|
|
|
|
*
|
|
|
|
|
* This file is part of the 86Box distribution.
|
|
|
|
|
*
|
|
|
|
|
* The generic SCSI device command handler.
|
|
|
|
|
*
|
2018-10-10 22:33:24 +02:00
|
|
|
* Version: @(#)scsi_device.c 1.0.18 2018/10/10
|
2017-08-22 21:28:22 +02:00
|
|
|
*
|
|
|
|
|
* Authors: Miran Grca, <mgrca8@gmail.com>
|
|
|
|
|
* Fred N. van Kempen, <decwiz@yahoo.com>
|
2017-10-17 01:59:09 -04:00
|
|
|
*
|
2018-01-26 22:17:09 +01:00
|
|
|
* Copyright 2016-2018 Miran Grca.
|
|
|
|
|
* Copyright 2017,2018 Fred N. van Kempen.
|
2017-08-22 21:28:22 +02:00
|
|
|
*/
|
2017-09-25 04:31:20 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <wchar.h>
|
2017-10-17 01:59:09 -04:00
|
|
|
#include "../86box.h"
|
2017-10-07 00:46:54 -04:00
|
|
|
#include "../device.h"
|
2017-10-02 02:15:35 -04:00
|
|
|
#include "../disk/hdd.h"
|
2017-08-22 05:45:07 +02:00
|
|
|
#include "scsi.h"
|
2018-07-15 01:41:53 +02:00
|
|
|
#include "scsi_device.h"
|
2017-08-22 05:45:07 +02:00
|
|
|
|
2017-08-22 02:13:45 -04:00
|
|
|
|
2018-10-10 22:33:24 +02:00
|
|
|
scsi_device_t scsi_devices[SCSI_ID_MAX];
|
|
|
|
|
|
2018-07-15 01:41:53 +02:00
|
|
|
uint8_t scsi_null_device_sense[18] = { 0x70,0,SENSE_ILLEGAL_REQUEST,0,0,0,0,0,0,0,0,0,ASC_INV_LUN,0,0,0,0,0 };
|
2017-08-22 02:13:45 -04:00
|
|
|
|
2017-08-22 05:45:07 +02:00
|
|
|
|
2018-07-15 01:41:53 +02:00
|
|
|
static uint8_t
|
2018-10-10 22:33:24 +02:00
|
|
|
scsi_device_target_command(scsi_device_t *dev, uint8_t *cdb)
|
2017-08-22 05:45:07 +02:00
|
|
|
{
|
2018-10-10 22:33:24 +02:00
|
|
|
if (dev->command && dev->err_stat_to_scsi) {
|
|
|
|
|
dev->command(dev->p, cdb);
|
|
|
|
|
return dev->err_stat_to_scsi(dev->p);
|
|
|
|
|
} else
|
|
|
|
|
return SCSI_STATUS_CHECK_CONDITION;
|
2017-08-22 05:45:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-10-10 22:33:24 +02:00
|
|
|
static void scsi_device_target_callback(scsi_device_t *dev)
|
2017-08-22 05:45:07 +02:00
|
|
|
{
|
2018-10-10 22:33:24 +02:00
|
|
|
if (dev->callback)
|
|
|
|
|
dev->callback(dev->p);
|
|
|
|
|
|
2018-07-15 01:41:53 +02:00
|
|
|
return;
|
2017-08-22 05:45:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-10-10 22:33:24 +02:00
|
|
|
static int scsi_device_target_err_stat_to_scsi(scsi_device_t *dev)
|
2017-08-22 05:45:07 +02:00
|
|
|
{
|
2018-10-10 22:33:24 +02:00
|
|
|
if (dev->err_stat_to_scsi)
|
|
|
|
|
return dev->err_stat_to_scsi(dev->p);
|
|
|
|
|
else
|
|
|
|
|
return SCSI_STATUS_CHECK_CONDITION;
|
2017-08-22 05:45:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-10-10 22:33:24 +02:00
|
|
|
int64_t scsi_device_get_callback(scsi_device_t *dev)
|
2018-03-07 20:06:08 +01:00
|
|
|
{
|
2018-10-10 22:33:24 +02:00
|
|
|
scsi_device_data_t *sdd = (scsi_device_data_t *) dev->p;
|
|
|
|
|
|
|
|
|
|
if (sdd)
|
|
|
|
|
return sdd->callback;
|
|
|
|
|
else
|
|
|
|
|
return -1LL;
|
2018-03-07 20:06:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-10-10 22:33:24 +02:00
|
|
|
uint8_t *scsi_device_sense(scsi_device_t *dev)
|
2017-08-22 05:45:07 +02:00
|
|
|
{
|
2018-10-10 22:33:24 +02:00
|
|
|
scsi_device_data_t *sdd = (scsi_device_data_t *) dev->p;
|
|
|
|
|
|
|
|
|
|
if (sdd)
|
|
|
|
|
return sdd->sense;
|
|
|
|
|
else
|
|
|
|
|
return scsi_null_device_sense;
|
2017-08-22 05:45:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-10-10 22:33:24 +02:00
|
|
|
void scsi_device_request_sense(scsi_device_t *dev, uint8_t *buffer, uint8_t alloc_length)
|
2017-08-22 21:28:22 +02:00
|
|
|
{
|
2018-10-10 22:33:24 +02:00
|
|
|
if (dev->request_sense)
|
|
|
|
|
dev->request_sense(dev, buffer, alloc_length);
|
|
|
|
|
else
|
|
|
|
|
memcpy(buffer, scsi_null_device_sense, alloc_length);
|
2017-08-22 21:28:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-10-10 22:33:24 +02:00
|
|
|
void scsi_device_reset(scsi_device_t *dev)
|
2017-08-22 05:45:07 +02:00
|
|
|
{
|
2018-10-10 22:33:24 +02:00
|
|
|
if (dev->reset)
|
|
|
|
|
dev->reset(dev->p);
|
2018-04-25 23:51:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-10-10 22:33:24 +02:00
|
|
|
void scsi_device_type_data(scsi_device_t *dev, uint8_t *type, uint8_t *rmb)
|
2018-04-25 23:51:13 +02:00
|
|
|
{
|
2018-10-10 22:33:24 +02:00
|
|
|
*rmb = dev->type >> 8;
|
|
|
|
|
*type = dev->type & 0xff;
|
2017-08-22 05:45:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-10-10 22:33:24 +02:00
|
|
|
int scsi_device_read_capacity(scsi_device_t *dev, uint8_t *cdb, uint8_t *buffer, uint32_t *len)
|
2017-08-22 05:45:07 +02:00
|
|
|
{
|
2018-10-10 22:33:24 +02:00
|
|
|
if (dev->read_capacity)
|
|
|
|
|
return dev->read_capacity(dev->p, cdb, buffer, len);
|
|
|
|
|
else
|
|
|
|
|
return 0;
|
2017-08-22 05:45:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-10-10 22:33:24 +02:00
|
|
|
int scsi_device_present(scsi_device_t *dev)
|
2017-08-22 05:45:07 +02:00
|
|
|
{
|
2018-10-10 22:33:24 +02:00
|
|
|
if (dev->type == SCSI_NONE)
|
|
|
|
|
return 0;
|
|
|
|
|
else
|
|
|
|
|
return 1;
|
2017-08-22 05:45:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-10-10 22:33:24 +02:00
|
|
|
int scsi_device_valid(scsi_device_t *dev)
|
2017-08-22 05:45:07 +02:00
|
|
|
{
|
2018-10-10 22:33:24 +02:00
|
|
|
if (dev->p)
|
|
|
|
|
return 0;
|
|
|
|
|
else
|
|
|
|
|
return 1;
|
2017-08-22 05:45:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-10-10 22:33:24 +02:00
|
|
|
int scsi_device_cdb_length(scsi_device_t *dev)
|
2017-08-22 05:45:07 +02:00
|
|
|
{
|
2018-07-15 01:41:53 +02:00
|
|
|
/* Right now, it's 12 for all devices. */
|
|
|
|
|
return 12;
|
2017-08-22 05:45:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-10-10 22:33:24 +02:00
|
|
|
void scsi_device_command_phase0(scsi_device_t *dev, uint8_t *cdb)
|
2017-08-22 05:45:07 +02:00
|
|
|
{
|
2018-10-10 22:33:24 +02:00
|
|
|
if (!dev->p) {
|
|
|
|
|
dev->phase = SCSI_PHASE_STATUS;
|
|
|
|
|
dev->status = SCSI_STATUS_CHECK_CONDITION;
|
|
|
|
|
return;
|
2017-08-22 05:45:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Finally, execute the SCSI command immediately and get the transfer length. */
|
2018-10-10 22:33:24 +02:00
|
|
|
dev->phase = SCSI_PHASE_COMMAND;
|
|
|
|
|
dev->status = scsi_device_target_command(dev, cdb);
|
2018-01-06 22:47:41 +01:00
|
|
|
|
2018-10-10 22:33:24 +02:00
|
|
|
if (dev->phase == SCSI_PHASE_STATUS) {
|
2018-01-06 22:47:41 +01:00
|
|
|
/* Command completed (either OK or error) - call the phase callback to complete the command. */
|
2018-10-10 22:33:24 +02:00
|
|
|
scsi_device_target_callback(dev);
|
2017-08-22 05:45:07 +02:00
|
|
|
}
|
2018-01-06 22:47:41 +01:00
|
|
|
/* If the phase is DATA IN or DATA OUT, finish this here. */
|
2017-08-22 02:13:45 -04:00
|
|
|
}
|
2017-10-08 05:04:38 +02:00
|
|
|
|
2018-10-10 22:33:24 +02:00
|
|
|
void scsi_device_command_phase1(scsi_device_t *dev)
|
2017-10-08 05:04:38 +02:00
|
|
|
{
|
2018-10-10 22:33:24 +02:00
|
|
|
if (!dev->p)
|
|
|
|
|
return;
|
2018-07-15 01:41:53 +02:00
|
|
|
|
|
|
|
|
/* Call the second phase. */
|
2018-10-10 22:33:24 +02:00
|
|
|
scsi_device_target_callback(dev);
|
|
|
|
|
dev->status = scsi_device_target_err_stat_to_scsi(dev);
|
2018-07-15 01:41:53 +02:00
|
|
|
/* Command second phase complete - call the callback to complete the command. */
|
2018-10-10 22:33:24 +02:00
|
|
|
scsi_device_target_callback(dev);
|
2017-10-08 05:04:38 +02:00
|
|
|
}
|
2017-10-14 07:03:19 +02:00
|
|
|
|
2018-10-10 22:33:24 +02:00
|
|
|
int32_t *scsi_device_get_buf_len(scsi_device_t *dev)
|
2017-10-14 07:03:19 +02:00
|
|
|
{
|
2018-10-10 22:33:24 +02:00
|
|
|
return &dev->buffer_length;
|
2017-10-14 07:03:19 +02:00
|
|
|
}
|