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-30 13:32:25 +01:00
|
|
|
* Version: @(#)scsi_device.c 1.0.22 2018/10/28
|
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-30 13:32:25 +01:00
|
|
|
if (dev->command) {
|
|
|
|
|
dev->command(dev->sc, cdb);
|
|
|
|
|
|
|
|
|
|
if (dev->sc->status & ERR_STAT)
|
|
|
|
|
return SCSI_STATUS_CHECK_CONDITION;
|
|
|
|
|
else
|
|
|
|
|
return SCSI_STATUS_OK;
|
2018-10-10 22:33:24 +02:00
|
|
|
} else
|
|
|
|
|
return SCSI_STATUS_CHECK_CONDITION;
|
2017-08-22 05:45:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-10-30 13:32:25 +01: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)
|
2018-10-30 13:32:25 +01:00
|
|
|
dev->callback(dev->sc);
|
2018-10-10 22:33:24 +02:00
|
|
|
|
2018-07-15 01:41:53 +02:00
|
|
|
return;
|
2017-08-22 05:45:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-10-30 13:32:25 +01:00
|
|
|
static int
|
|
|
|
|
scsi_device_target_err_stat_to_scsi(scsi_device_t *dev)
|
2017-08-22 05:45:07 +02:00
|
|
|
{
|
2018-10-30 13:32:25 +01:00
|
|
|
if (dev->sc)
|
|
|
|
|
if (dev->sc->status & ERR_STAT)
|
|
|
|
|
return SCSI_STATUS_CHECK_CONDITION;
|
|
|
|
|
else
|
|
|
|
|
return SCSI_STATUS_OK;
|
2018-10-10 22:33:24 +02:00
|
|
|
else
|
|
|
|
|
return SCSI_STATUS_CHECK_CONDITION;
|
2017-08-22 05:45:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-10-30 13:32:25 +01:00
|
|
|
int64_t
|
|
|
|
|
scsi_device_get_callback(scsi_device_t *dev)
|
2018-03-07 20:06:08 +01:00
|
|
|
{
|
2018-10-30 13:32:25 +01:00
|
|
|
if (dev->sc)
|
|
|
|
|
return dev->sc->callback;
|
2018-10-10 22:33:24 +02:00
|
|
|
else
|
|
|
|
|
return -1LL;
|
2018-03-07 20:06:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-10-30 13:32:25 +01:00
|
|
|
uint8_t *
|
|
|
|
|
scsi_device_sense(scsi_device_t *dev)
|
2017-08-22 05:45:07 +02:00
|
|
|
{
|
2018-10-30 13:32:25 +01:00
|
|
|
if (dev->sc)
|
|
|
|
|
return dev->sc->sense;
|
2018-10-10 22:33:24 +02:00
|
|
|
else
|
|
|
|
|
return scsi_null_device_sense;
|
2017-08-22 05:45:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-10-30 13:32:25 +01: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)
|
2018-10-30 13:32:25 +01:00
|
|
|
dev->request_sense(dev->sc, buffer, alloc_length);
|
2018-10-10 22:33:24 +02:00
|
|
|
else
|
|
|
|
|
memcpy(buffer, scsi_null_device_sense, alloc_length);
|
2017-08-22 21:28:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-10-30 13:32:25 +01: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)
|
2018-10-30 13:32:25 +01:00
|
|
|
dev->reset(dev->sc);
|
2018-04-25 23:51:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-10-30 13:32:25 +01: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-30 13:32:25 +01:00
|
|
|
int
|
|
|
|
|
scsi_device_valid(scsi_device_t *dev)
|
2017-08-22 05:45:07 +02:00
|
|
|
{
|
2018-10-30 13:32:25 +01:00
|
|
|
if (dev->sc)
|
2018-10-10 22:33:24 +02:00
|
|
|
return 1;
|
2018-10-10 23:12:30 +02:00
|
|
|
else
|
|
|
|
|
return 0;
|
2017-08-22 05:45:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-10-30 13:32:25 +01: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-30 13:32:25 +01:00
|
|
|
void
|
|
|
|
|
scsi_device_command_phase0(scsi_device_t *dev, uint8_t *cdb)
|
2017-08-22 05:45:07 +02:00
|
|
|
{
|
2018-10-30 13:32:25 +01:00
|
|
|
if (!dev->sc) {
|
2018-10-10 22:33:24 +02:00
|
|
|
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-30 13:32:25 +01:00
|
|
|
|
|
|
|
|
void
|
|
|
|
|
scsi_device_command_phase1(scsi_device_t *dev)
|
2017-10-08 05:04:38 +02:00
|
|
|
{
|
2018-10-30 13:32:25 +01:00
|
|
|
if (!dev->sc)
|
2018-10-10 22:33:24 +02:00
|
|
|
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-30 13:32:25 +01:00
|
|
|
|
|
|
|
|
void
|
|
|
|
|
scsi_device_command_stop(scsi_device_t *dev)
|
|
|
|
|
{
|
|
|
|
|
if (!dev->command_stop)
|
|
|
|
|
dev->command_stop(dev->sc);
|
|
|
|
|
scsi_device_target_callback(dev);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
scsi_device_close_all(void)
|
2017-10-14 07:03:19 +02:00
|
|
|
{
|
2018-10-30 13:32:25 +01:00
|
|
|
int i;
|
|
|
|
|
scsi_device_t *dev;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < SCSI_ID_MAX; i++) {
|
|
|
|
|
dev = &(scsi_devices[i]);
|
|
|
|
|
if (dev->command_stop && dev->sc)
|
|
|
|
|
dev->command_stop(dev->sc);
|
|
|
|
|
}
|
2017-10-14 07:03:19 +02:00
|
|
|
}
|