Commit a917d384 authored by pbrook's avatar pbrook
Browse files

SCSI TCQ support.


git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@2139 c046a42c-6fe2-441c-8c8c-71466251a162
parent 508d92d0
......@@ -64,8 +64,7 @@ struct ESPState {
int do_cmd;
uint32_t dma_left;
uint8_t async_buf[TARGET_PAGE_SIZE];
uint32_t async_ptr;
uint8_t *async_buf;
uint32_t async_len;
};
......@@ -91,17 +90,16 @@ struct ESPState {
static int get_cmd(ESPState *s, uint8_t *buf)
{
uint32_t dmaptr, dmalen;
uint32_t dmalen;
int target;
dmalen = s->wregs[0] | (s->wregs[1] << 8);
target = s->wregs[4] & 7;
DPRINTF("get_cmd: len %d target %d\n", dmalen, target);
if (s->dma) {
dmaptr = iommu_translate(s->espdmaregs[1]);
DPRINTF("DMA Direction: %c, addr 0x%8.8x\n",
s->espdmaregs[0] & DMA_WRITE_MEM ? 'w': 'r', dmaptr);
cpu_physical_memory_read(dmaptr, buf, dmalen);
s->espdmaregs[0] & DMA_WRITE_MEM ? 'w': 'r', s->espdmaregs[1]);
sparc_iommu_memory_read(s->espdmaregs[1], buf, dmalen);
} else {
buf[0] = 0;
memcpy(&buf[1], s->ti_buf, dmalen);
......@@ -112,6 +110,12 @@ static int get_cmd(ESPState *s, uint8_t *buf)
s->ti_rptr = 0;
s->ti_wptr = 0;
if (s->current_dev) {
/* Started a new command before the old one finished. Cancel it. */
scsi_cancel_io(s->current_dev, 0);
s->async_len = 0;
}
if (target >= 4 || !s->scsi_dev[target]) {
// No such drive
s->rregs[4] = STAT_IN;
......@@ -137,12 +141,15 @@ static void do_cmd(ESPState *s, uint8_t *buf)
s->ti_size = 0;
} else {
s->rregs[4] = STAT_IN | STAT_TC;
s->dma_left = 0;
if (datalen > 0) {
s->rregs[4] |= STAT_DI;
s->ti_size = datalen;
scsi_read_data(s->current_dev, 0);
} else {
s->rregs[4] |= STAT_DO;
s->ti_size = -datalen;
scsi_write_data(s->current_dev, 0);
}
}
s->rregs[5] = INTR_BS | INTR_FC;
......@@ -178,16 +185,13 @@ static void handle_satn_stop(ESPState *s)
static void write_response(ESPState *s)
{
uint32_t dmaptr;
DPRINTF("Transfer status (sense=%d)\n", s->sense);
s->ti_buf[0] = s->sense;
s->ti_buf[1] = 0;
if (s->dma) {
dmaptr = iommu_translate(s->espdmaregs[1]);
DPRINTF("DMA Direction: %c\n",
s->espdmaregs[0] & DMA_WRITE_MEM ? 'w': 'r');
cpu_physical_memory_write(dmaptr, s->ti_buf, 2);
sparc_iommu_memory_write(s->espdmaregs[1], s->ti_buf, 2);
s->rregs[4] = STAT_IN | STAT_TC | STAT_ST;
s->rregs[5] = INTR_BS | INTR_FC;
s->rregs[6] = SEQ_CD;
......@@ -202,78 +206,89 @@ static void write_response(ESPState *s)
}
static void esp_dma_done(ESPState *s)
{
s->rregs[4] |= STAT_IN | STAT_TC;
s->rregs[5] = INTR_BS;
s->rregs[6] = 0;
s->rregs[7] = 0;
s->espdmaregs[0] |= DMA_INTR;
pic_set_irq(s->irq, 1);
}
static void esp_do_dma(ESPState *s)
{
uint32_t dmaptr, minlen, len, from, to;
uint32_t addr, len;
int to_device;
to_device = (s->espdmaregs[0] & DMA_WRITE_MEM) == 0;
from = s->espdmaregs[1];
minlen = s->dma_left;
to = from + minlen;
dmaptr = iommu_translate(s->espdmaregs[1]);
if ((from & TARGET_PAGE_MASK) != (to & TARGET_PAGE_MASK)) {
len = TARGET_PAGE_SIZE - (from & ~TARGET_PAGE_MASK);
} else {
len = to - from;
}
DPRINTF("DMA address p %08x v %08x len %08x, from %08x, to %08x\n", dmaptr, s->espdmaregs[1], len, from, to);
addr = s->espdmaregs[1];
len = s->dma_left;
DPRINTF("DMA address %08x len %08x\n", addr, len);
if (s->do_cmd) {
s->espdmaregs[1] += len;
s->ti_size -= len;
DPRINTF("command len %d + %d\n", s->cmdlen, len);
cpu_physical_memory_read(dmaptr, &s->cmdbuf[s->cmdlen], len);
sparc_iommu_memory_read(addr, &s->cmdbuf[s->cmdlen], len);
s->ti_size = 0;
s->cmdlen = 0;
s->do_cmd = 0;
do_cmd(s, s->cmdbuf);
return;
}
if (s->async_len == 0) {
/* Defer until data is available. */
return;
}
if (len > s->async_len) {
len = s->async_len;
}
if (to_device) {
sparc_iommu_memory_read(addr, s->async_buf, len);
} else {
s->async_len = len;
s->dma_left -= len;
sparc_iommu_memory_write(addr, s->async_buf, len);
}
s->ti_size -= len;
s->dma_left -= len;
s->async_buf += len;
s->async_len -= len;
s->espdmaregs[1] += len;
if (s->async_len == 0) {
if (to_device) {
s->async_ptr = -1;
cpu_physical_memory_read(dmaptr, s->async_buf, len);
scsi_write_data(s->current_dev, s->async_buf, len);
scsi_write_data(s->current_dev, 0);
} else {
s->async_ptr = dmaptr;
scsi_read_data(s->current_dev, s->async_buf, len);
scsi_read_data(s->current_dev, 0);
}
}
if (s->dma_left == 0) {
esp_dma_done(s);
}
}
static void esp_command_complete(void *opaque, uint32_t reason, int sense)
static void esp_command_complete(void *opaque, int reason, uint32_t tag,
uint32_t arg)
{
ESPState *s = (ESPState *)opaque;
s->ti_size -= s->async_len;
s->espdmaregs[1] += s->async_len;
if (s->async_ptr != (uint32_t)-1) {
cpu_physical_memory_write(s->async_ptr, s->async_buf, s->async_len);
}
if (reason == SCSI_REASON_DONE) {
DPRINTF("SCSI Command complete\n");
if (s->ti_size != 0)
DPRINTF("SCSI command completed unexpectedly\n");
s->ti_size = 0;
if (sense)
s->dma_left = 0;
s->async_len = 0;
if (arg)
DPRINTF("Command failed\n");
s->sense = sense;
s->sense = arg;
s->rregs[4] = STAT_ST;
esp_dma_done(s);
s->current_dev = NULL;
} else {
DPRINTF("transfer %d/%d\n", s->dma_left, s->ti_size);
}
if (s->dma_left) {
esp_do_dma(s);
} else {
if (s->ti_size) {
s->rregs[4] |= STAT_IN | STAT_TC;
} else {
s->rregs[4] = STAT_IN | STAT_TC | STAT_ST;
}
s->rregs[5] = INTR_BS;
s->rregs[6] = 0;
s->rregs[7] = 0;
s->espdmaregs[0] |= DMA_INTR;
pic_set_irq(s->irq, 1);
s->async_len = arg;
s->async_buf = scsi_get_buf(s->current_dev, 0);
if (s->dma_left)
esp_do_dma(s);
}
}
......@@ -333,7 +348,8 @@ static uint32_t esp_mem_readb(void *opaque, target_phys_addr_t addr)
s->ti_size--;
if ((s->rregs[4] & 6) == 0) {
/* Data in/out. */
scsi_read_data(s->current_dev, &s->rregs[2], 0);
fprintf(stderr, "esp: PIO data read not implemented\n");
s->rregs[2] = 0;
} else {
s->rregs[2] = s->ti_buf[s->ti_rptr++];
}
......@@ -378,7 +394,7 @@ static void esp_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
uint8_t buf;
buf = val & 0xff;
s->ti_size--;
scsi_write_data(s->current_dev, &buf, 0);
fprintf(stderr, "esp: PIO data write not implemented\n");
} else {
s->ti_size++;
s->ti_buf[s->ti_wptr++] = val & 0xff;
......@@ -590,8 +606,9 @@ void esp_init(BlockDriverState **bd, int irq, uint32_t espaddr, uint32_t espdadd
qemu_register_reset(esp_reset, s);
for (i = 0; i < MAX_DISKS; i++) {
if (bs_table[i]) {
/* Command queueing is not implemented. */
s->scsi_dev[i] =
scsi_disk_init(bs_table[i], esp_command_complete, s);
scsi_disk_init(bs_table[i], 0, esp_command_complete, s);
}
}
}
......
......@@ -186,21 +186,62 @@ static CPUWriteMemoryFunc *iommu_mem_write[3] = {
iommu_mem_writew,
};
uint32_t iommu_translate_local(void *opaque, uint32_t addr)
static uint32_t iommu_page_get_flags(IOMMUState *s, uint32_t addr)
{
IOMMUState *s = opaque;
uint32_t iopte, pa, tmppte;
uint32_t iopte;
iopte = s->regs[1] << 4;
addr &= ~s->iostart;
iopte += (addr >> (PAGE_SHIFT - 2)) & ~3;
pa = ldl_phys(iopte);
return ldl_phys(iopte);
}
static uint32_t iommu_translate_pa(IOMMUState *s, uint32_t addr, uint32_t pa)
{
uint32_t tmppte;
tmppte = pa;
pa = ((pa & IOPTE_PAGE) << 4) + (addr & PAGE_MASK);
DPRINTF("xlate dva %x => pa %x (iopte[%x] = %x)\n", addr, pa, iopte, tmppte);
DPRINTF("xlate dva %x => pa %x (iopte = %x)\n", addr, pa, tmppte);
return pa;
}
uint32_t iommu_translate_local(void *opaque, uint32_t addr)
{
uint32_t flags;
flags = iommu_page_get_flags(opaque, addr);
return iommu_translate_pa(opaque, addr, flags);
}
void sparc_iommu_memory_rw_local(void *opaque, target_phys_addr_t addr,
uint8_t *buf, int len, int is_write)
{
int l, flags;
target_ulong page, phys_addr;
void * p;
while (len > 0) {
page = addr & TARGET_PAGE_MASK;
l = (page + TARGET_PAGE_SIZE) - addr;
if (l > len)
l = len;
flags = iommu_page_get_flags(opaque, page);
if (!(flags & IOPTE_VALID))
return;
phys_addr = iommu_translate_pa(opaque, addr, flags);
if (is_write) {
if (!(flags & IOPTE_WRITE))
return;
cpu_physical_memory_write(phys_addr, buf, len);
} else {
cpu_physical_memory_read(phys_addr, buf, len);
}
len -= l;
buf += l;
addr += l;
}
}
static void iommu_save(QEMUFile *f, void *opaque)
{
IOMMUState *s = opaque;
......
This diff is collapsed.
This diff is collapsed.
......@@ -199,6 +199,18 @@ uint32_t iommu_translate(uint32_t addr)
return iommu_translate_local(iommu, addr);
}
void sparc_iommu_memory_read(target_phys_addr_t addr,
uint8_t *buf, int len)
{
return sparc_iommu_memory_rw_local(iommu, addr, buf, len, 0);
}
void sparc_iommu_memory_write(target_phys_addr_t addr,
uint8_t *buf, int len)
{
return sparc_iommu_memory_rw_local(iommu, addr, buf, len, 1);
}
static void *slavio_misc;
void qemu_system_powerdown(void)
......
......@@ -32,8 +32,12 @@ enum USBMSDMode {
typedef struct {
USBDevice dev;
enum USBMSDMode mode;
uint32_t scsi_len;
uint8_t *scsi_buf;
uint32_t usb_len;
uint8_t *usb_buf;
uint32_t data_len;
uint32_t transfer_len;
uint32_t residue;
uint32_t tag;
BlockDriverState *bs;
SCSIDevice *scsi_dev;
......@@ -42,6 +46,23 @@ typedef struct {
USBPacket *packet;
} MSDState;
struct usb_msd_cbw {
uint32_t sig;
uint32_t tag;
uint32_t data_len;
uint8_t flags;
uint8_t lun;
uint8_t cmd_len;
uint8_t cmd[16];
};
struct usb_msd_csw {
uint32_t sig;
uint32_t tag;
uint32_t residue;
uint8_t status;
};
static const uint8_t qemu_msd_dev_descriptor[] = {
0x12, /* u8 bLength; */
0x01, /* u8 bDescriptorType; Device */
......@@ -107,26 +128,90 @@ static const uint8_t qemu_msd_config_descriptor[] = {
0x00 /* u8 ep_bInterval; */
};
static void usb_msd_command_complete(void *opaque, uint32_t reason, int fail)
static void usb_msd_copy_data(MSDState *s)
{
uint32_t len;
len = s->usb_len;
if (len > s->scsi_len)
len = s->scsi_len;
if (s->mode == USB_MSDM_DATAIN) {
memcpy(s->usb_buf, s->scsi_buf, len);
} else {
memcpy(s->scsi_buf, s->usb_buf, len);
}
s->usb_len -= len;
s->scsi_len -= len;
s->usb_buf += len;
s->scsi_buf += len;
s->data_len -= len;
if (s->scsi_len == 0) {
if (s->mode == USB_MSDM_DATAIN) {
scsi_read_data(s->scsi_dev, s->tag);
} else if (s->mode == USB_MSDM_DATAOUT) {
scsi_write_data(s->scsi_dev, s->tag);
}
}
}
static void usb_msd_send_status(MSDState *s)
{
struct usb_msd_csw csw;
csw.sig = cpu_to_le32(0x53425355);
csw.tag = cpu_to_le32(s->tag);
csw.residue = s->residue;
csw.status = s->result;
memcpy(s->usb_buf, &csw, 13);
}
static void usb_msd_command_complete(void *opaque, int reason, uint32_t tag,
uint32_t arg)
{
MSDState *s = (MSDState *)opaque;
USBPacket *p;
USBPacket *p = s->packet;
s->data_len -= s->transfer_len;
s->transfer_len = 0;
if (tag != s->tag) {
fprintf(stderr, "usb-msd: Unexpected SCSI Tag 0x%x\n", tag);
}
if (reason == SCSI_REASON_DONE) {
DPRINTF("Command complete %d\n", fail);
s->result = fail;
s->mode = USB_MSDM_CSW;
DPRINTF("Command complete %d\n", arg);
s->residue = s->data_len;
s->result = arg != 0;
if (s->packet) {
if (s->data_len == 0 && s->mode == USB_MSDM_DATAOUT) {
/* A deferred packet with no write data remaining must be
the status read packet. */
usb_msd_send_status(s);
s->mode = USB_MSDM_CBW;
} else {
if (s->data_len) {
s->data_len -= s->usb_len;
if (s->mode == USB_MSDM_DATAIN)
memset(s->usb_buf, 0, s->usb_len);
s->usb_len = 0;
}
if (s->data_len == 0)
s->mode = USB_MSDM_CSW;
}
s->packet = NULL;
usb_packet_complete(p);
} else if (s->data_len == 0) {
s->mode = USB_MSDM_CSW;
}
return;
}
if (s->packet) {
/* Set s->packet to NULL before calling usb_packet_complete because
annother request may be issues before usb_packet_complete returns.
*/
DPRINTF("Packet complete %p\n", p);
p = s->packet;
s->packet = NULL;
usb_packet_complete(p);
s->scsi_len = arg;
s->scsi_buf = scsi_get_buf(s->scsi_dev, tag);
if (p) {
usb_msd_copy_data(s);
if (s->usb_len == 0) {
/* Set s->packet to NULL before calling usb_packet_complete
because annother request may be issued before
usb_packet_complete returns. */
DPRINTF("Packet complete %p\n", p);
s->packet = NULL;
usb_packet_complete(p);
}
}
}
......@@ -251,28 +336,12 @@ static int usb_msd_handle_control(USBDevice *dev, int request, int value,
return ret;
}
struct usb_msd_cbw {
uint32_t sig;
uint32_t tag;
uint32_t data_len;
uint8_t flags;
uint8_t lun;
uint8_t cmd_len;
uint8_t cmd[16];
};
struct usb_msd_csw {
uint32_t sig;
uint32_t tag;
uint32_t residue;
uint8_t status;
};
static void usb_msd_cancel_io(USBPacket *p, void *opaque)
{
MSDState *s = opaque;
scsi_cancel_io(s->scsi_dev);
scsi_cancel_io(s->scsi_dev, s->tag);
s->packet = NULL;
s->scsi_len = 0;
}
static int usb_msd_handle_data(USBDevice *dev, USBPacket *p)
......@@ -280,7 +349,6 @@ static int usb_msd_handle_data(USBDevice *dev, USBPacket *p)
MSDState *s = (MSDState *)dev;
int ret = 0;
struct usb_msd_cbw cbw;
struct usb_msd_csw csw;
uint8_t devep = p->devep;
uint8_t *data = p->data;
int len = p->len;
......@@ -318,7 +386,17 @@ static int usb_msd_handle_data(USBDevice *dev, USBPacket *p)
}
DPRINTF("Command tag 0x%x flags %08x len %d data %d\n",
s->tag, cbw.flags, cbw.cmd_len, s->data_len);
s->residue = 0;
scsi_send_command(s->scsi_dev, s->tag, cbw.cmd, 0);
/* ??? Should check that USB and SCSI data transfer
directions match. */
if (s->residue == 0) {
if (s->mode == USB_MSDM_DATAIN) {
scsi_read_data(s->scsi_dev, s->tag);
} else if (s->mode == USB_MSDM_DATAOUT) {
scsi_write_data(s->scsi_dev, s->tag);
}
}
ret = len;
break;
......@@ -327,17 +405,24 @@ static int usb_msd_handle_data(USBDevice *dev, USBPacket *p)
if (len > s->data_len)
goto fail;
s->transfer_len = len;
if (scsi_write_data(s->scsi_dev, data, len))
goto fail;
if (s->transfer_len == 0) {
ret = len;
} else {
s->usb_buf = data;
s->usb_len = len;
if (s->scsi_len) {
usb_msd_copy_data(s);
}
if (s->residue && s->usb_len) {
s->data_len -= s->usb_len;
if (s->data_len == 0)
s->mode = USB_MSDM_CSW;
s->usb_len = 0;
}
if (s->usb_len) {
DPRINTF("Deferring packet %p\n", p);
usb_defer_packet(p, usb_msd_cancel_io, s);
s->packet = p;
ret = USB_RET_ASYNC;
} else {
ret = len;
}
break;
......@@ -352,37 +437,51 @@ static int usb_msd_handle_data(USBDevice *dev, USBPacket *p)
goto fail;
switch (s->mode) {
case USB_MSDM_DATAOUT:
if (s->data_len != 0 || len < 13)
goto fail;
/* Waiting for SCSI write to complete. */
usb_defer_packet(p, usb_msd_cancel_io, s);
s->packet = p;
ret = USB_RET_ASYNC;
break;
case USB_MSDM_CSW:
DPRINTF("Command status %d tag 0x%x, len %d\n",
s->result, s->tag, len);
if (len < 13)
goto fail;
csw.sig = cpu_to_le32(0x53425355);
csw.tag = cpu_to_le32(s->tag);
csw.residue = 0;
csw.status = s->result;
memcpy(data, &csw, 13);
ret = 13;
s->usb_len = len;
s->usb_buf = data;
usb_msd_send_status(s);
s->mode = USB_MSDM_CBW;
ret = 13;
break;
case USB_MSDM_DATAIN:
DPRINTF("Data in %d/%d\n", len, s->data_len);
if (len > s->data_len)
len = s->data_len;
s->transfer_len = len;
if (scsi_read_data(s->scsi_dev, data, len))
goto fail;
if (s->transfer_len == 0) {
ret = len;
} else {
s->usb_buf = data;
s->usb_len = len;
if (s->scsi_len) {
usb_msd_copy_data(s);
}
if (s->residue && s->usb_len) {
s->data_len -= s->usb_len;
memset(s->usb_buf, 0, s->usb_len);
if (s->data_len == 0)
s->mode = USB_MSDM_CSW;
s->usb_len = 0;
}
if (s->usb_len) {
DPRINTF("Deferring packet %p\n", p);
usb_defer_packet(p, usb_msd_cancel_io, s);
s->packet = p;
ret = USB_RET_ASYNC;
} else {
ret = len;
}
break;
......@@ -436,7 +535,7 @@ USBDevice *usb_msd_init(const char *filename)
snprintf(s->dev.devname, sizeof(s->dev.devname), "QEMU USB MSD(%.16s)",