- SCSI emulation layer moved to separate file and C++ object

(TODO: cdrom support, update workspace files)
- USB mass storage device separation continued
- device qualifier descriptor implemented
This commit is contained in:
Volker Ruppert 2007-03-16 18:23:13 +00:00
parent b8787fd5a7
commit e39769a7e8
5 changed files with 647 additions and 490 deletions

View File

@ -100,6 +100,7 @@ OBJS_THAT_SUPPORT_OTHER_PLUGINS = \
hdimage.o \
vmware3.o \
vmware4.o \
scsi_device.o \
$(CDROM_OBJS) \
$(SOUNDLOW_OBJS) \
$(NETLOW_OBJS)
@ -158,6 +159,9 @@ libbx_serial.la: serial.lo serial_raw.lo
libbx_vga.la: vga.lo svga_cirrus.lo
$(LIBTOOL) --mode=link $(CXX) -module vga.lo svga_cirrus.lo -o libbx_vga.la -rpath $(PLUGIN_PATH)
libbx_pciusb.la: pciusb.lo scsi_device.lo hdimage.lo
$(LIBTOOL) --mode=link $(CXX) -module pciusb.lo scsi_device.lo hdimage.lo -o libbx_pciusb.la -rpath $(PLUGIN_PATH)
#### building DLLs for win32 (tested on cygwin only)
bx_%.dll: %.o
$(CXX) $(CXXFLAGS) -shared -o $@ $< $(WIN32_DLL_IMPORT_LIBRARY)
@ -190,6 +194,9 @@ bx_serial.dll: serial.o serial_raw.o
bx_vga.dll: vga.o svga_cirrus.o
$(CXX) $(CXXFLAGS) -shared -o bx_vga.dll vga.o svga_cirrus.o $(WIN32_DLL_IMPORT_LIBRARY)
bx_pciusb.dll: pciusb.o scsi_device.o hdimage.o
$(CXX) $(CXXFLAGS) -shared -o bx_pciusb.dll pciusb.o scsi_device.o hdimage.o $(WIN32_DLL_IMPORT_LIBRARY)
##### end DLL section
clean:

View File

@ -1,5 +1,5 @@
/////////////////////////////////////////////////////////////////////////
// $Id: pciusb.cc,v 1.48 2007-03-14 18:05:46 vruppert Exp $
// $Id: pciusb.cc,v 1.49 2007-03-16 18:23:13 vruppert Exp $
/////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2004 MandrakeSoft S.A.
@ -60,6 +60,7 @@
#include "iodev.h"
#if BX_SUPPORT_PCI && BX_SUPPORT_PCIUSB
#include "hdimage.h"
#include "scsi_device.h"
#define LOG_THIS theUSBDevice->
@ -99,7 +100,7 @@ bx_pciusb_c::~bx_pciusb_c()
if (BX_USB_THIS hub[0].device[i].dev_type == USB_DEV_TYPE_DISK) {
s = (MSDState*)BX_USB_THIS hub[0].device[i].devstate;
if (s != NULL) {
delete s;
usb_msd_handle_destroy(s);
}
}
}
@ -1456,8 +1457,14 @@ unsigned bx_pciusb_c::GetDescriptor(struct USB_DEVICE *dev, struct REQUEST_PACKE
ret = 1;
break;
case DEVICE_QUALIFIER:
BX_PANIC(("GET_DESCRIPTOR: DEVICE_QUALIFIER not implemented yet."));
ret = 1;
device_buffer[0] = 10;
device_buffer[1] = DEVICE_QUALIFIER;
memcpy(device_buffer+2, &dev->function.device_descr+2, 6);
device_buffer[8] = 1;
device_buffer[9] = 0;
dev->function.in = device_buffer;
dev->function.in_cnt = 10;
ret = 0;
break;
case OTHER_SPEED_CONFIG:
BX_PANIC(("GET_DESCRIPTOR: OTHER_SPEED_CONFIG not implemented yet."));
@ -1708,21 +1715,16 @@ void bx_pciusb_c::usb_set_connect_status(Bit8u port, int type, bx_bool connected
if ((type == USB_DEV_TYPE_DISK) &&
(BX_USB_THIS hub[0].device[BX_USB_THIS hub[0].usb_port[port].device_num].devstate == NULL)) {
s = new MSDState;
memset(s, 0, sizeof(MSDState));
s->hdimage = new default_image_t();
if (port == 0) {
strcpy(pname, BXPN_USB1_OPTION1);
} else {
strcpy(pname, BXPN_USB1_OPTION2);
}
if (s->hdimage->open(SIM->get_param_string(pname)->getptr()) < 0) {
BX_ERROR(("could not open hard drive image file '%s'", SIM->get_param_string(pname)->getptr()));
s = BX_USB_THIS usb_msd_init(SIM->get_param_string(pname)->getptr());
if (s == NULL) {
usb_set_connect_status(port, USB_DEV_TYPE_DISK, 0);
} else {
BX_INFO(("HD on USB port #%d: '%s'", port+1, SIM->get_param_string(pname)->getptr()));
s->scsi_dev = BX_USB_THIS scsi_disk_init(s->hdimage, 0, usb_msd_command_complete, (void*)s);
s->mode = USB_MSDM_CBW;
BX_USB_THIS hub[0].device[BX_USB_THIS hub[0].usb_port[port].device_num].devstate = (void*)s;
}
}
@ -1742,10 +1744,7 @@ void bx_pciusb_c::usb_set_connect_status(Bit8u port, int type, bx_bool connected
BX_USB_THIS disk_connected = 0;
s = (MSDState*)BX_USB_THIS hub[0].device[BX_USB_THIS hub[0].usb_port[port].device_num].devstate;
if (s != NULL) {
BX_USB_THIS scsi_disk_destroy(s->scsi_dev);
s->hdimage->close();
delete s->hdimage;
delete s;
BX_USB_THIS usb_msd_handle_destroy(s);
BX_USB_THIS hub[0].device[BX_USB_THIS hub[0].usb_port[port].device_num].devstate = NULL;
}
}
@ -1905,11 +1904,6 @@ void bx_pciusb_c::dump_packet(Bit8u *data, unsigned size)
// USB mass storage device support
enum scsi_reason {
SCSI_REASON_DONE,
SCSI_REASON_DATA
};
int bx_pciusb_c::usb_msd_handle_data(MSDState *s, USBPacket *p)
{
struct usb_msd_cbw cbw;
@ -1948,12 +1942,12 @@ int bx_pciusb_c::usb_msd_handle_data(MSDState *s, USBPacket *p)
BX_DEBUG(("command tag 0x%x flags %08x len %d data %d",
s->tag, cbw.flags, cbw.cmd_len, s->data_len));
s->residue = 0;
scsi_send_command(s->scsi_dev, s->tag, cbw.cmd, cbw.lun);
s->scsi_dev->scsi_send_command(s->tag, cbw.cmd, cbw.lun);
if (s->residue == 0) {
if (s->mode == USB_MSDM_DATAIN) {
scsi_read_data(s->scsi_dev, s->tag);
s->scsi_dev->scsi_read_data(s->tag);
} else if (s->mode == USB_MSDM_DATAOUT) {
scsi_write_data(s->scsi_dev, s->tag);
s->scsi_dev->scsi_write_data(s->tag);
}
}
ret = len;
@ -2077,9 +2071,9 @@ void bx_pciusb_c::usb_msd_copy_data(MSDState *s)
s->data_len -= len;
if (s->scsi_len == 0) {
if (s->mode == USB_MSDM_DATAIN) {
scsi_read_data(s->scsi_dev, s->tag);
s->scsi_dev->scsi_read_data(s->tag);
} else if (s->mode == USB_MSDM_DATAOUT) {
scsi_write_data(s->scsi_dev, s->tag);
s->scsi_dev->scsi_write_data(s->tag);
}
}
}
@ -2128,7 +2122,7 @@ void bx_pciusb_c::usb_msd_command_complete(void *opaque, int reason, Bit32u tag,
return;
}
s->scsi_len = arg;
s->scsi_buf = BX_USB_THIS scsi_get_buf(s->scsi_dev, tag);
s->scsi_buf = s->scsi_dev->scsi_get_buf(tag);
if (p) {
BX_USB_THIS usb_msd_copy_data(s);
if (s->usb_len == 0) {
@ -2138,428 +2132,27 @@ void bx_pciusb_c::usb_msd_command_complete(void *opaque, int reason, Bit32u tag,
}
}
// USB SCSI emulation layer
static SCSIRequest *free_requests = NULL;
SCSIRequest* bx_pciusb_c::scsi_new_request(SCSIDevice *s, Bit32u tag)
{
SCSIRequest *r;
if (free_requests) {
r = free_requests;
free_requests = r->next;
} else {
r = new SCSIRequest;
}
r->dev = s;
r->tag = tag;
r->sector_count = 0;
r->buf_len = 0;
r->next = s->requests;
s->requests = r;
return r;
}
void bx_pciusb_c::scsi_remove_request(SCSIRequest *r)
{
SCSIRequest *last;
SCSIDevice *s = r->dev;
if (s->requests == r) {
s->requests = r->next;
} else {
last = s->requests;
while (last != NULL) {
if (last->next != r)
last = last->next;
else
break;
}
if (last) {
last->next = r->next;
} else {
BX_ERROR(("orphaned request"));
}
}
r->next = free_requests;
free_requests = r;
}
SCSIRequest* bx_pciusb_c::scsi_find_request(SCSIDevice *s, Bit32u tag)
{
SCSIRequest *r = s->requests;
while (r != NULL) {
if (r->tag != tag)
r = r->next;
else
break;
}
return r;
}
void bx_pciusb_c::scsi_command_complete(SCSIRequest *r, int sense)
{
SCSIDevice *s = r->dev;
Bit32u tag;
BX_DEBUG(("command complete tag=0x%x sense=%d", r->tag, sense));
s->sense = sense;
tag = r->tag;
scsi_remove_request(r);
s->completion(s->opaque, SCSI_REASON_DONE, tag, sense);
}
void bx_pciusb_c::scsi_cancel_io(SCSIDevice *s, Bit32u tag)
{
BX_DEBUG(("cancel tag=0x%x", tag));
SCSIRequest *r = scsi_find_request(s, tag);
if (r) {
scsi_remove_request(r);
}
}
void bx_pciusb_c::scsi_read_complete(void *opaque, int ret)
{
SCSIRequest *r = (SCSIRequest *)opaque;
SCSIDevice *s = r->dev;
if (ret) {
BX_ERROR(("IO error"));
scsi_command_complete(r, SENSE_HARDWARE_ERROR);
return;
}
BX_DEBUG(("data ready tag=0x%x len=%d", r->tag, r->buf_len));
s->completion(s->opaque, SCSI_REASON_DATA, r->tag, r->buf_len);
}
void bx_pciusb_c::scsi_read_data(SCSIDevice *s, Bit32u tag)
{
Bit32u n;
int ret;
SCSIRequest *r = scsi_find_request(s, tag);
if (!r) {
BX_ERROR(("bad read tag 0x%x", tag));
scsi_command_complete(r, SENSE_HARDWARE_ERROR);
return;
}
if (r->sector_count == -1) {
BX_DEBUG(("read buf_len=%d", r->buf_len));
r->sector_count = 0;
s->completion(s->opaque, SCSI_REASON_DATA, r->tag, r->buf_len);
return;
}
BX_DEBUG(("read sector_count=%d", r->sector_count));
if (r->sector_count == 0) {
scsi_command_complete(r, SENSE_NO_SENSE);
return;
}
n = r->sector_count;
if (n > SCSI_DMA_BUF_SIZE / 512)
n = SCSI_DMA_BUF_SIZE / 512;
r->buf_len = n * 512;
ret = s->hdimage->lseek(r->sector * 512, SEEK_SET);
if (ret < 0) {
BX_ERROR(("could not lseek() hard drive image file"));
scsi_command_complete(r, SENSE_HARDWARE_ERROR);
}
ret = s->hdimage->read((bx_ptr_t)r->dma_buf, r->buf_len);
if (ret < r->buf_len) {
BX_ERROR(("could not read() hard drive image file"));
scsi_command_complete(r, SENSE_HARDWARE_ERROR);
} else {
scsi_read_complete((void*)r, 0);
}
r->sector += n;
r->sector_count -= n;
}
void bx_pciusb_c::scsi_write_complete(void *opaque, int ret)
{
SCSIRequest *r = (SCSIRequest *)opaque;
SCSIDevice *s = r->dev;
Bit32u len;
if (ret) {
BX_ERROR(("IO error"));
scsi_command_complete(r, SENSE_HARDWARE_ERROR);
return;
}
if (r->sector_count == 0) {
scsi_command_complete(r, SENSE_NO_SENSE);
} else {
len = r->sector_count * 512;
if (len > SCSI_DMA_BUF_SIZE) {
len = SCSI_DMA_BUF_SIZE;
}
r->buf_len = len;
BX_DEBUG(("write complete tag=0x%x more=%d", r->tag, len));
s->completion(s->opaque, SCSI_REASON_DATA, r->tag, len);
}
}
int bx_pciusb_c::scsi_write_data(SCSIDevice *s, Bit32u tag)
{
SCSIRequest *r;
Bit32u n;
int ret;
BX_DEBUG(("write data tag=0x%x", tag));
r = scsi_find_request(s, tag);
if (!r) {
BX_ERROR(("bad write tag 0x%x", tag));
scsi_command_complete(r, SENSE_HARDWARE_ERROR);
return 1;
}
n = r->buf_len / 512;
if (n) {
ret = s->hdimage->lseek(r->sector * 512, SEEK_SET);
if (ret < 0) {
BX_ERROR(("could not lseek() hard drive image file"));
scsi_command_complete(r, SENSE_HARDWARE_ERROR);
}
ret = s->hdimage->write((bx_ptr_t)r->dma_buf, r->buf_len);
if (ret < r->buf_len) {
BX_ERROR(("could not write() hard drive image file"));
scsi_command_complete(r, SENSE_HARDWARE_ERROR);
} else {
scsi_write_complete((void*)r, 0);
}
r->sector += n;
r->sector_count -= n;
} else {
scsi_write_complete(r, 0);
}
return 0;
}
Bit8u* bx_pciusb_c::scsi_get_buf(SCSIDevice *s, Bit32u tag)
{
SCSIRequest *r = scsi_find_request(s, tag);
if (!r) {
BX_ERROR(("bad buffer tag 0x%x", tag));
return NULL;
}
return r->dma_buf;
}
Bit32s bx_pciusb_c::scsi_send_command(SCSIDevice *s, Bit32u tag, Bit8u *buf, int lun)
{
Bit64u nb_sectors;
Bit32u lba;
Bit32u len;
int cmdlen;
int is_write;
Bit8u command;
Bit8u *outbuf;
SCSIRequest *r;
command = buf[0];
r = scsi_find_request(s, tag);
if (r) {
BX_ERROR(("tag 0x%x already in use", tag));
scsi_cancel_io(s, tag);
}
r = scsi_new_request(s, tag);
outbuf = r->dma_buf;
is_write = 0;
BX_DEBUG(("command: lun=%d tag=0x%x data=0x%02x", lun, tag, buf[0]));
switch (command >> 5) {
case 0:
lba = buf[3] | (buf[2] << 8) | ((buf[1] & 0x1f) << 16);
len = buf[4];
cmdlen = 6;
break;
case 1:
case 2:
lba = buf[5] | (buf[4] << 8) | (buf[3] << 16) | (buf[2] << 24);
len = buf[8] | (buf[7] << 8);
cmdlen = 10;
break;
case 4:
lba = buf[5] | (buf[4] << 8) | (buf[3] << 16) | (buf[2] << 24);
len = buf[13] | (buf[12] << 8) | (buf[11] << 16) | (buf[10] << 24);
cmdlen = 16;
break;
case 5:
lba = buf[5] | (buf[4] << 8) | (buf[3] << 16) | (buf[2] << 24);
len = buf[9] | (buf[8] << 8) | (buf[7] << 16) | (buf[6] << 24);
cmdlen = 12;
break;
default:
BX_ERROR(("Unsupported command length, command %x", command));
goto fail;
}
if (lun || buf[1] >> 5) {
BX_ERROR(("unimplemented LUN %d", lun ? lun : buf[1] >> 5));
goto fail;
}
switch (command) {
case 0x0:
BX_DEBUG(("Test Unit Ready"));
break;
case 0x03:
BX_DEBUG(("request Sense (len %d)", len));
if (len < 4)
goto fail;
memset(buf, 0, 4);
outbuf[0] = 0xf0;
outbuf[1] = 0;
outbuf[2] = s->sense;
r->buf_len = 4;
break;
case 0x12:
BX_DEBUG(("inquiry (len %d)", len));
if (len < 36) {
BX_ERROR(("inquiry buffer too small (%d)", len));
}
memset(outbuf, 0, 36);
outbuf[0] = 0;
memcpy(&outbuf[16], "BOCHS HARDDISK ", 16);
memcpy(&outbuf[8], "BOCHS ", 8);
memcpy(&outbuf[32], "1.0", 4);
outbuf[2] = 3;
outbuf[3] = 2;
outbuf[4] = 32;
outbuf[7] = 0x10 | (s->tcq ? 0x02 : 0);
r->buf_len = 36;
break;
case 0x16:
BX_INFO(("Reserve(6)"));
if (buf[1] & 1)
goto fail;
break;
case 0x17:
BX_INFO(("Release(6)"));
if (buf[1] & 1)
goto fail;
break;
case 0x1a:
case 0x5a:
{
Bit8u *p;
int page;
page = buf[2] & 0x3f;
BX_DEBUG(("mode sense (page %d, len %d)", page, len));
p = outbuf;
memset(p, 0, 4);
outbuf[1] = 0; /* Default media type. */
outbuf[3] = 0; /* Block descriptor length. */
p += 4;
if ((page == 8 || page == 0x3f)) {
/* Caching page. */
p[0] = 8;
p[1] = 0x12;
p[2] = 4; /* WCE */
p += 19;
}
r->buf_len = p - outbuf;
outbuf[0] = r->buf_len - 4;
if (r->buf_len > (int)len)
r->buf_len = len;
}
break;
case 0x1b:
BX_INFO(("Start Stop Unit"));
break;
case 0x1e:
BX_INFO(("Prevent Allow Medium Removal (prevent = %d)", buf[4] & 3));
break;
case 0x25:
BX_DEBUG(("Read Capacity"));
/* The normal LEN field for this command is zero. */
memset(outbuf, 0, 8);
nb_sectors = s->hdimage->hd_size / 512;
/* Returned value is the address of the last sector. */
if (nb_sectors) {
nb_sectors--;
outbuf[0] = (nb_sectors >> 24) & 0xff;
outbuf[1] = (nb_sectors >> 16) & 0xff;
outbuf[2] = (nb_sectors >> 8) & 0xff;
outbuf[3] = nb_sectors & 0xff;
outbuf[4] = 0;
outbuf[5] = 0;
outbuf[6] = s->cluster_size * 2;
outbuf[7] = 0;
r->buf_len = 8;
} else {
scsi_command_complete(r, SENSE_NOT_READY);
return 0;
}
break;
case 0x08:
case 0x28:
BX_DEBUG(("Read (sector %d, count %d)", lba, len));
r->sector = lba * s->cluster_size;
r->sector_count = len * s->cluster_size;
break;
case 0x0a:
case 0x2a:
BX_DEBUG(("Write (sector %d, count %d)", lba, len));
r->sector = lba * s->cluster_size;
r->sector_count = len * s->cluster_size;
is_write = 1;
break;
case 0x56:
BX_INFO(("Reserve(10)"));
if (buf[1] & 3)
goto fail;
break;
case 0x57:
BX_INFO(("Release(10)"));
if (buf[1] & 3)
goto fail;
break;
case 0xa0:
BX_INFO(("Report LUNs (len %d)", len));
if (len < 16)
goto fail;
memset(outbuf, 0, 16);
outbuf[3] = 8;
r->buf_len = 16;
break;
default:
BX_ERROR(("Unknown SCSI command (%2.2x)", buf[0]));
fail:
scsi_command_complete(r, SENSE_ILLEGAL_REQUEST);
return 0;
}
if (r->sector_count == 0 && r->buf_len == 0) {
scsi_command_complete(r, SENSE_NO_SENSE);
}
len = r->sector_count * 512 + r->buf_len;
if (is_write) {
return -len;
} else {
if (!r->sector_count)
r->sector_count = -1;
return len;
}
}
void bx_pciusb_c::scsi_disk_destroy(SCSIDevice *s)
void bx_pciusb_c::usb_msd_handle_destroy(MSDState *s)
{
delete s->scsi_dev;
s->hdimage->close();
delete s->hdimage;
delete s;
}
SCSIDevice* bx_pciusb_c::scsi_disk_init(device_image_t *_hdimage, int tcq,
scsi_completionfn completion, void *opaque)
MSDState* bx_pciusb_c::usb_msd_init(const char *filename)
{
SCSIDevice *s = new SCSIDevice;
memset(s, 0, sizeof(SCSIDevice));
s->hdimage = _hdimage;
s->tcq = tcq;
s->completion = completion;
s->opaque = opaque;
s->cluster_size = 1;
return s;
MSDState *s = new MSDState;
memset(s, 0, sizeof(MSDState));
s->hdimage = new default_image_t();
if (s->hdimage->open(filename) < 0) {
BX_ERROR(("could not open hard drive image file '%s'", filename));
return NULL;
} else {
s->scsi_dev = new scsi_device_t(s->hdimage, 0, usb_msd_command_complete, (void*)s);
s->mode = USB_MSDM_CBW;
return s;
}
}
// USB runtime parameter handler

View File

@ -1,5 +1,5 @@
/////////////////////////////////////////////////////////////////////////
// $Id: pciusb.h,v 1.21 2007-03-14 18:05:46 vruppert Exp $
// $Id: pciusb.h,v 1.22 2007-03-16 18:23:13 vruppert Exp $
/////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2004 MandrakeSoft S.A.
@ -53,9 +53,7 @@
#define USB_RET_BABBLE (-4)
#define USB_RET_ASYNC (-5)
typedef struct SCSIDevice SCSIDevice;
typedef void (*scsi_completionfn)(void *opaque, int reason, Bit32u tag,
Bit32u arg);
class scsi_device_t;
class device_image_t;
struct USBPacket {
@ -84,7 +82,7 @@ typedef struct {
Bit32u tag;
int result;
device_image_t *hdimage;
SCSIDevice *scsi_dev;
scsi_device_t *scsi_dev;
USBPacket *packet;
} MSDState;
@ -105,32 +103,6 @@ struct usb_msd_csw {
Bit8u status;
};
#define SENSE_NO_SENSE 0
#define SENSE_HARDWARE_ERROR 4
#define SCSI_DMA_BUF_SIZE 65536
typedef struct SCSIRequest {
SCSIDevice *dev;
Bit32u tag;
int sector;
int sector_count;
int buf_len;
Bit8u dma_buf[SCSI_DMA_BUF_SIZE];
struct SCSIRequest *next;
} SCSIRequest;
struct SCSIDevice
{
device_image_t *hdimage;
SCSIRequest *requests;
int cluster_size;
int sense;
int tcq;
scsi_completionfn completion;
void *opaque;
};
// device requests
enum { GET_STATUS=0, CLEAR_FEATURE, SET_FEATURE=3, SET_ADDRESS=5, GET_DESCRIPTOR=6, SET_DESCRIPTOR,
GET_CONFIGURATION, SET_CONFIGURATION,
@ -488,25 +460,12 @@ private:
void write(Bit32u address, Bit32u value, unsigned io_len);
#endif
// USB mass storage device support
int usb_msd_handle_data(MSDState *s, USBPacket *p);
void usb_msd_copy_data(MSDState *s);
void usb_msd_send_status(MSDState *s);
static void usb_msd_command_complete(void *opaque, int reason, Bit32u tag, Bit32u arg);
// USB SCSI emulation layer
SCSIRequest* scsi_new_request(SCSIDevice *s, Bit32u tag);
void scsi_remove_request(SCSIRequest *r);
SCSIRequest *scsi_find_request(SCSIDevice *s, Bit32u tag);
void scsi_command_complete(SCSIRequest *r, int sense);
void scsi_cancel_io(SCSIDevice *s, Bit32u tag);
void scsi_read_complete(void *opaque, int ret);
void scsi_read_data(SCSIDevice *s, Bit32u tag);
void scsi_write_complete(void *opaque, int ret);
int scsi_write_data(SCSIDevice *s, Bit32u tag);
Bit8u* scsi_get_buf(SCSIDevice *s, Bit32u tag);
Bit32s scsi_send_command(SCSIDevice *s, Bit32u tag, Bit8u *buf, int lun);
void scsi_disk_destroy(SCSIDevice *s);
SCSIDevice* scsi_disk_init(device_image_t *_hdimage, int tcq,
scsi_completionfn completion, void *opaque);
int usb_msd_handle_data(MSDState *s, USBPacket *p);
void usb_msd_handle_destroy(MSDState *s);
MSDState* usb_msd_init(const char *filename);
};
#endif

509
bochs/iodev/scsi_device.cc Normal file
View File

@ -0,0 +1,509 @@
/////////////////////////////////////////////////////////////////////////
// $Id: scsi_device.cc,v 1.1 2007-03-16 18:23:13 vruppert Exp $
/////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2007 Volker Ruppert
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
// SCSI emulation layer ported from the Qemu project
// Define BX_PLUGGABLE in files that can be compiled into plugins. For
// platforms that require a special tag on exported symbols, BX_PLUGGABLE
// is used to know when we are exporting symbols and when we are importing.
#define BX_PLUGGABLE
#define NO_DEVICE_INCLUDES
#include "iodev.h"
#include "hdimage.h"
#include "scsi_device.h"
#define LOG_THIS
static SCSIRequest *free_requests = NULL;
scsi_device_t::scsi_device_t(device_image_t *_hdimage, int _tcq,
scsi_completionfn _completion, void *_dev)
{
type = SCSIDEV_TYPE_DISK;
hdimage = _hdimage;
requests = NULL;
sense = 0;
tcq = _tcq;
completion = _completion;
dev = _dev;
cluster_size = 1;
}
scsi_device_t::~scsi_device_t(void)
{
}
SCSIRequest* scsi_device_t::scsi_new_request(Bit32u tag)
{
SCSIRequest *r;
if (free_requests) {
r = free_requests;
free_requests = r->next;
} else {
r = new SCSIRequest;
}
r->dev = this;
r->tag = tag;
r->sector_count = 0;
r->buf_len = 0;
r->next = requests;
requests = r;
return r;
}
void scsi_device_t::scsi_remove_request(SCSIRequest *r)
{
SCSIRequest *last;
if (requests == r) {
requests = r->next;
} else {
last = requests;
while (last != NULL) {
if (last->next != r)
last = last->next;
else
break;
}
if (last) {
last->next = r->next;
} else {
BX_ERROR(("orphaned request"));
}
}
r->next = free_requests;
free_requests = r;
}
SCSIRequest* scsi_device_t::scsi_find_request(Bit32u tag)
{
SCSIRequest *r = requests;
while (r != NULL) {
if (r->tag != tag)
r = r->next;
else
break;
}
return r;
}
void scsi_device_t::scsi_command_complete(SCSIRequest *r, int _sense)
{
Bit32u tag;
BX_DEBUG(("command complete tag=0x%x sense=%d", r->tag, sense));
sense = _sense;
tag = r->tag;
scsi_remove_request(r);
completion(dev, SCSI_REASON_DONE, tag, sense);
}
void scsi_device_t::scsi_cancel_io(Bit32u tag)
{
BX_DEBUG(("cancel tag=0x%x", tag));
SCSIRequest *r = scsi_find_request(tag);
if (r) {
scsi_remove_request(r);
}
}
void scsi_device_t::scsi_read_complete(void *req, int ret)
{
SCSIRequest *r = (SCSIRequest *)req;
if (ret) {
BX_ERROR(("IO error"));
scsi_command_complete(r, SENSE_HARDWARE_ERROR);
return;
}
BX_DEBUG(("data ready tag=0x%x len=%d", r->tag, r->buf_len));
completion(dev, SCSI_REASON_DATA, r->tag, r->buf_len);
}
void scsi_device_t::scsi_read_data(Bit32u tag)
{
Bit32u n;
int ret;
SCSIRequest *r = scsi_find_request(tag);
if (!r) {
BX_ERROR(("bad read tag 0x%x", tag));
scsi_command_complete(r, SENSE_HARDWARE_ERROR);
return;
}
if (r->sector_count == -1) {
BX_DEBUG(("read buf_len=%d", r->buf_len));
r->sector_count = 0;
completion(dev, SCSI_REASON_DATA, r->tag, r->buf_len);
return;
}
BX_DEBUG(("read sector_count=%d", r->sector_count));
if (r->sector_count == 0) {
scsi_command_complete(r, SENSE_NO_SENSE);
return;
}
n = r->sector_count;
if (n > SCSI_DMA_BUF_SIZE / 512)
n = SCSI_DMA_BUF_SIZE / 512;
r->buf_len = n * 512;
ret = hdimage->lseek(r->sector * 512, SEEK_SET);
if (ret < 0) {
BX_ERROR(("could not lseek() hard drive image file"));
scsi_command_complete(r, SENSE_HARDWARE_ERROR);
}
ret = hdimage->read((bx_ptr_t)r->dma_buf, r->buf_len);
if (ret < r->buf_len) {
BX_ERROR(("could not read() hard drive image file"));
scsi_command_complete(r, SENSE_HARDWARE_ERROR);
} else {
scsi_read_complete((void*)r, 0);
}
r->sector += n;
r->sector_count -= n;
}
void scsi_device_t::scsi_write_complete(void *req, int ret)
{
SCSIRequest *r = (SCSIRequest *)req;
Bit32u len;
if (ret) {
BX_ERROR(("IO error"));
scsi_command_complete(r, SENSE_HARDWARE_ERROR);
return;
}
if (r->sector_count == 0) {
scsi_command_complete(r, SENSE_NO_SENSE);
} else {
len = r->sector_count * 512;
if (len > SCSI_DMA_BUF_SIZE) {
len = SCSI_DMA_BUF_SIZE;
}
r->buf_len = len;
BX_DEBUG(("write complete tag=0x%x more=%d", r->tag, len));
completion(dev, SCSI_REASON_DATA, r->tag, len);
}
}
int scsi_device_t::scsi_write_data(Bit32u tag)
{
SCSIRequest *r;
Bit32u n;
int ret;
BX_DEBUG(("write data tag=0x%x", tag));
r = scsi_find_request(tag);
if (!r) {
BX_ERROR(("bad write tag 0x%x", tag));
scsi_command_complete(r, SENSE_HARDWARE_ERROR);
return 1;
}
n = r->buf_len / 512;
if (n) {
ret = hdimage->lseek(r->sector * 512, SEEK_SET);
if (ret < 0) {
BX_ERROR(("could not lseek() hard drive image file"));
scsi_command_complete(r, SENSE_HARDWARE_ERROR);
}
ret = hdimage->write((bx_ptr_t)r->dma_buf, r->buf_len);
if (ret < r->buf_len) {
BX_ERROR(("could not write() hard drive image file"));
scsi_command_complete(r, SENSE_HARDWARE_ERROR);
} else {
scsi_write_complete((void*)r, 0);
}
r->sector += n;
r->sector_count -= n;
} else {
scsi_write_complete(r, 0);
}
return 0;
}
Bit8u* scsi_device_t::scsi_get_buf(Bit32u tag)
{
SCSIRequest *r = scsi_find_request(tag);
if (!r) {
BX_ERROR(("bad buffer tag 0x%x", tag));
return NULL;
}
return r->dma_buf;
}
Bit32s scsi_device_t::scsi_send_command(Bit32u tag, Bit8u *buf, int lun)
{
Bit64u nb_sectors;
Bit32u lba;
Bit32u len;
int cmdlen;
int is_write;
Bit8u command;
Bit8u *outbuf;
SCSIRequest *r;
command = buf[0];
r = scsi_find_request(tag);
if (r) {
BX_ERROR(("tag 0x%x already in use", tag));
scsi_cancel_io(tag);
}
r = scsi_new_request(tag);
outbuf = r->dma_buf;
is_write = 0;
BX_DEBUG(("command: lun=%d tag=0x%x data=0x%02x", lun, tag, buf[0]));
switch (command >> 5) {
case 0:
lba = buf[3] | (buf[2] << 8) | ((buf[1] & 0x1f) << 16);
len = buf[4];
cmdlen = 6;
break;
case 1:
case 2:
lba = buf[5] | (buf[4] << 8) | (buf[3] << 16) | (buf[2] << 24);
len = buf[8] | (buf[7] << 8);
cmdlen = 10;
break;
case 4:
lba = buf[5] | (buf[4] << 8) | (buf[3] << 16) | (buf[2] << 24);
len = buf[13] | (buf[12] << 8) | (buf[11] << 16) | (buf[10] << 24);
cmdlen = 16;
break;
case 5:
lba = buf[5] | (buf[4] << 8) | (buf[3] << 16) | (buf[2] << 24);
len = buf[9] | (buf[8] << 8) | (buf[7] << 16) | (buf[6] << 24);
cmdlen = 12;
break;
default:
BX_ERROR(("Unsupported command length, command %x", command));
goto fail;
}
if (lun || buf[1] >> 5) {
BX_ERROR(("unimplemented LUN %d", lun ? lun : buf[1] >> 5));
goto fail;
}
switch (command) {
case 0x0:
BX_DEBUG(("Test Unit Ready"));
break;
case 0x03:
BX_DEBUG(("request Sense (len %d)", len));
if (len < 4)
goto fail;
memset(buf, 0, 4);
outbuf[0] = 0xf0;
outbuf[1] = 0;
outbuf[2] = sense;
r->buf_len = 4;
break;
case 0x12:
BX_DEBUG(("inquiry (len %d)", len));
if (len < 36) {
BX_ERROR(("inquiry buffer too small (%d)", len));
}
memset(outbuf, 0, 36);
if (type == SCSIDEV_TYPE_CDROM) {
outbuf[0] = 5;
outbuf[1] = 0x80;
memcpy(&outbuf[16], "QEMU CD-ROM ", 16);
} else {
outbuf[0] = 0;
memcpy(&outbuf[16], "BOCHS HARDDISK ", 16);
}
memcpy(&outbuf[8], "BOCHS ", 8);
memcpy(&outbuf[32], "1.0", 4);
outbuf[2] = 3;
outbuf[3] = 2;
outbuf[4] = 32;
outbuf[7] = 0x10 | (tcq ? 0x02 : 0);
r->buf_len = 36;
break;
case 0x16:
BX_INFO(("Reserve(6)"));
if (buf[1] & 1)
goto fail;
break;
case 0x17:
BX_INFO(("Release(6)"));
if (buf[1] & 1)
goto fail;
break;
case 0x1a:
case 0x5a:
{
Bit8u *p;
int page;
page = buf[2] & 0x3f;
BX_DEBUG(("mode sense (page %d, len %d)", page, len));
p = outbuf;
memset(p, 0, 4);
outbuf[1] = 0; /* Default media type. */
outbuf[3] = 0; /* Block descriptor length. */
if (type == SCSIDEV_TYPE_CDROM) {
outbuf[2] = 0x80; /* Readonly. */
}
p += 4;
if ((page == 8 || page == 0x3f)) {
/* Caching page. */
p[0] = 8;
p[1] = 0x12;
p[2] = 4; /* WCE */
p += 19;
}
if ((page == 0x3f || page == 0x2a)
&& (type == SCSIDEV_TYPE_CDROM)) {
/* CD Capabilities and Mechanical Status page. */
p[0] = 0x2a;
p[1] = 0x14;
p[2] = 3; // CD-R & CD-RW read
p[3] = 0; // Writing not supported
p[4] = 0x7f; /* Audio, composite, digital out,
mode 2 form 1&2, multi session */
p[5] = 0xff; /* CD DA, DA accurate, RW supported,
RW corrected, C2 errors, ISRC,
UPC, Bar code */
p[6] = 0x2d; // TODO: | (bdrv_is_locked(s->bdrv)? 2 : 0);
/* Locking supported, jumper present, eject, tray */
p[7] = 0; /* no volume & mute control, no changer */
p[8] = (50 * 176) >> 8; // 50x read speed
p[9] = (50 * 176) & 0xff;
p[10] = 0 >> 8; // No volume
p[11] = 0 & 0xff;
p[12] = 2048 >> 8; // 2M buffer
p[13] = 2048 & 0xff;
p[14] = (16 * 176) >> 8; // 16x read speed current
p[15] = (16 * 176) & 0xff;
p[18] = (16 * 176) >> 8; // 16x write speed
p[19] = (16 * 176) & 0xff;
p[20] = (16 * 176) >> 8; // 16x write speed current
p[21] = (16 * 176) & 0xff;
p += 21;
}
r->buf_len = p - outbuf;
outbuf[0] = r->buf_len - 4;
if (r->buf_len > (int)len)
r->buf_len = len;
}
break;
case 0x1b:
BX_INFO(("Start Stop Unit"));
break;
case 0x1e:
BX_INFO(("Prevent Allow Medium Removal (prevent = %d)", buf[4] & 3));
break;
case 0x25:
BX_DEBUG(("Read Capacity"));
/* The normal LEN field for this command is zero. */
memset(outbuf, 0, 8);
if (type == SCSIDEV_TYPE_CDROM) {
// TODO: get cdrom capacity
} else {
nb_sectors = hdimage->hd_size / 512;
}
/* Returned value is the address of the last sector. */
if (nb_sectors) {
nb_sectors--;
outbuf[0] = (nb_sectors >> 24) & 0xff;
outbuf[1] = (nb_sectors >> 16) & 0xff;
outbuf[2] = (nb_sectors >> 8) & 0xff;
outbuf[3] = nb_sectors & 0xff;
outbuf[4] = 0;
outbuf[5] = 0;
outbuf[6] = cluster_size * 2;
outbuf[7] = 0;
r->buf_len = 8;
} else {
scsi_command_complete(r, SENSE_NOT_READY);
return 0;
}
break;
case 0x08:
case 0x28:
BX_DEBUG(("Read (sector %d, count %d)", lba, len));
r->sector = lba * cluster_size;
r->sector_count = len * cluster_size;
break;
case 0x0a:
case 0x2a:
BX_DEBUG(("Write (sector %d, count %d)", lba, len));
r->sector = lba * cluster_size;
r->sector_count = len * cluster_size;
is_write = 1;
break;
case 0x35:
BX_DEBUG(("Syncronise cache (sector %d, count %d)", lba, len));
// TODO: flush cache
break;
case 0x43:
BX_ERROR(("Read TOC not implemented yet"));
goto fail;
break;
case 0x46:
BX_DEBUG(("Get Configuration (rt %d, maxlen %d)", buf[1] & 3, len));
memset(outbuf, 0, 8);
/* ??? This shoud probably return much more information. For now
just return the basic header indicating the CD-ROM profile. */
outbuf[7] = 8; // CD-ROM
r->buf_len = 8;
break;
case 0x56:
BX_INFO(("Reserve(10)"));
if (buf[1] & 3)
goto fail;
break;
case 0x57:
BX_INFO(("Release(10)"));
if (buf[1] & 3)
goto fail;
break;
case 0xa0:
BX_INFO(("Report LUNs (len %d)", len));
if (len < 16)
goto fail;
memset(outbuf, 0, 16);
outbuf[3] = 8;
r->buf_len = 16;
break;
default:
BX_ERROR(("Unknown SCSI command (%2.2x)", buf[0]));
fail:
scsi_command_complete(r, SENSE_ILLEGAL_REQUEST);
return 0;
}
if (r->sector_count == 0 && r->buf_len == 0) {
scsi_command_complete(r, SENSE_NO_SENSE);
}
len = r->sector_count * 512 + r->buf_len;
if (is_write) {
return -len;
} else {
if (!r->sector_count)
r->sector_count = -1;
return len;
}
}

89
bochs/iodev/scsi_device.h Normal file
View File

@ -0,0 +1,89 @@
/////////////////////////////////////////////////////////////////////////
// $Id: scsi_device.h,v 1.1 2007-03-16 18:23:13 vruppert Exp $
/////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2007 Volker Ruppert
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
// SCSI emulation layer ported from the Qemu project
#ifndef BX_IODEV_SCSI_DEVICE_H
#define BX_IODEV_SCSI_DEVICE_H
typedef void (*scsi_completionfn)(void *opaque, int reason, Bit32u tag,
Bit32u arg);
class scsi_device_t;
class device_image_t;
enum scsidev_type {
SCSIDEV_TYPE_DISK,
SCSIDEV_TYPE_CDROM
};
enum scsi_reason {
SCSI_REASON_DONE,
SCSI_REASON_DATA
};
#define SENSE_NO_SENSE 0
#define SENSE_NOT_READY 2
#define SENSE_HARDWARE_ERROR 4
#define SENSE_ILLEGAL_REQUEST 5
#define SCSI_DMA_BUF_SIZE 65536
typedef struct SCSIRequest {
scsi_device_t *dev;
Bit32u tag;
int sector;
int sector_count;
int buf_len;
Bit8u dma_buf[SCSI_DMA_BUF_SIZE];
struct SCSIRequest *next;
} SCSIRequest;
class scsi_device_t : public logfunctions {
public:
scsi_device_t(device_image_t *_hdimage, int _tcq,
scsi_completionfn _completion, void *_dev);
virtual ~scsi_device_t(void);
Bit32s scsi_send_command(Bit32u tag, Bit8u *buf, int lun);
void scsi_command_complete(SCSIRequest *r, int sense);
void scsi_cancel_io(Bit32u tag);
void scsi_read_complete(void *req, int ret);
void scsi_read_data(Bit32u tag);
void scsi_write_complete(void *req, int ret);
int scsi_write_data(Bit32u tag);
Bit8u* scsi_get_buf(Bit32u tag);
protected:
SCSIRequest* scsi_new_request(Bit32u tag);
void scsi_remove_request(SCSIRequest *r);
SCSIRequest *scsi_find_request(Bit32u tag);
private:
enum scsidev_type type;
device_image_t *hdimage;
SCSIRequest *requests;
int cluster_size;
int sense;
int tcq;
scsi_completionfn completion;
void *dev;
};
#endif