Files
86Box/src/scsi/scsi_device.c

195 lines
3.7 KiB
C
Raw Normal View History

/*
* 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.
*
* Version: @(#)scsi_device.c 1.0.22 2018/10/28
*
* 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.
*/
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <wchar.h>
2017-10-17 01:59:09 -04:00
#include "../86box.h"
#include "../device.h"
#include "../disk/hdd.h"
#include "scsi.h"
#include "scsi_device.h"
scsi_device_t scsi_devices[SCSI_ID_MAX];
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 };
static uint8_t
scsi_device_target_command(scsi_device_t *dev, uint8_t *cdb)
{
if (dev->command) {
dev->command(dev->sc, cdb);
if (dev->sc->status & ERR_STAT)
return SCSI_STATUS_CHECK_CONDITION;
else
return SCSI_STATUS_OK;
} else
return SCSI_STATUS_CHECK_CONDITION;
}
static void
scsi_device_target_callback(scsi_device_t *dev)
{
if (dev->callback)
dev->callback(dev->sc);
return;
}
static int
scsi_device_target_err_stat_to_scsi(scsi_device_t *dev)
{
if (dev->sc)
if (dev->sc->status & ERR_STAT)
return SCSI_STATUS_CHECK_CONDITION;
else
return SCSI_STATUS_OK;
else
return SCSI_STATUS_CHECK_CONDITION;
}
int64_t
scsi_device_get_callback(scsi_device_t *dev)
{
if (dev->sc)
return dev->sc->callback;
else
return -1LL;
}
uint8_t *
scsi_device_sense(scsi_device_t *dev)
{
if (dev->sc)
return dev->sc->sense;
else
return scsi_null_device_sense;
}
void
scsi_device_request_sense(scsi_device_t *dev, uint8_t *buffer, uint8_t alloc_length)
{
if (dev->request_sense)
dev->request_sense(dev->sc, buffer, alloc_length);
else
memcpy(buffer, scsi_null_device_sense, alloc_length);
}
void
scsi_device_reset(scsi_device_t *dev)
{
if (dev->reset)
dev->reset(dev->sc);
}
int
scsi_device_present(scsi_device_t *dev)
{
if (dev->type == SCSI_NONE)
return 0;
else
return 1;
}
int
scsi_device_valid(scsi_device_t *dev)
{
if (dev->sc)
return 1;
2018-10-10 23:12:30 +02:00
else
return 0;
}
int
scsi_device_cdb_length(scsi_device_t *dev)
{
/* Right now, it's 12 for all devices. */
return 12;
}
void
scsi_device_command_phase0(scsi_device_t *dev, uint8_t *cdb)
{
if (!dev->sc) {
dev->phase = SCSI_PHASE_STATUS;
dev->status = SCSI_STATUS_CHECK_CONDITION;
return;
}
/* Finally, execute the SCSI command immediately and get the transfer length. */
dev->phase = SCSI_PHASE_COMMAND;
dev->status = scsi_device_target_command(dev, cdb);
if (dev->phase == SCSI_PHASE_STATUS) {
/* Command completed (either OK or error) - call the phase callback to complete the command. */
scsi_device_target_callback(dev);
}
/* If the phase is DATA IN or DATA OUT, finish this here. */
}
void
scsi_device_command_phase1(scsi_device_t *dev)
{
if (!dev->sc)
return;
/* Call the second phase. */
scsi_device_target_callback(dev);
dev->status = scsi_device_target_err_stat_to_scsi(dev);
/* Command second phase complete - call the callback to complete the command. */
scsi_device_target_callback(dev);
}
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)
{
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);
}
}