2 * QEMU LSI53C895A SCSI Host Bus Adapter emulation
4 * Copyright (c) 2006 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licensed under the LGPL.
11 * LSI53C810 emulation is incorrect, in the sense that it supports
12 * features added in later evolutions. This should not be a problem,
13 * as well-behaved operating systems will not try to use them.
16 #include "qemu/osdep.h"
19 #include "hw/pci/pci.h"
20 #include "hw/scsi/scsi.h"
21 #include "sysemu/dma.h"
25 //#define DEBUG_LSI_REG
28 #define DPRINTF(fmt, ...) \
29 do { printf("lsi_scsi: " fmt , ## __VA_ARGS__); } while (0)
30 #define BADF(fmt, ...) \
31 do { fprintf(stderr, "lsi_scsi: error: " fmt , ## __VA_ARGS__); exit(1);} while (0)
33 #define DPRINTF(fmt, ...) do {} while(0)
34 #define BADF(fmt, ...) \
35 do { fprintf(stderr, "lsi_scsi: error: " fmt , ## __VA_ARGS__);} while (0)
38 static const char *names
[] = {
39 "SCNTL0", "SCNTL1", "SCNTL2", "SCNTL3", "SCID", "SXFER", "SDID", "GPREG",
40 "SFBR", "SOCL", "SSID", "SBCL", "DSTAT", "SSTAT0", "SSTAT1", "SSTAT2",
41 "DSA0", "DSA1", "DSA2", "DSA3", "ISTAT", "0x15", "0x16", "0x17",
42 "CTEST0", "CTEST1", "CTEST2", "CTEST3", "TEMP0", "TEMP1", "TEMP2", "TEMP3",
43 "DFIFO", "CTEST4", "CTEST5", "CTEST6", "DBC0", "DBC1", "DBC2", "DCMD",
44 "DNAD0", "DNAD1", "DNAD2", "DNAD3", "DSP0", "DSP1", "DSP2", "DSP3",
45 "DSPS0", "DSPS1", "DSPS2", "DSPS3", "SCRATCHA0", "SCRATCHA1", "SCRATCHA2", "SCRATCHA3",
46 "DMODE", "DIEN", "SBR", "DCNTL", "ADDER0", "ADDER1", "ADDER2", "ADDER3",
47 "SIEN0", "SIEN1", "SIST0", "SIST1", "SLPAR", "0x45", "MACNTL", "GPCNTL",
48 "STIME0", "STIME1", "RESPID", "0x4b", "STEST0", "STEST1", "STEST2", "STEST3",
49 "SIDL", "0x51", "0x52", "0x53", "SODL", "0x55", "0x56", "0x57",
50 "SBDL", "0x59", "0x5a", "0x5b", "SCRATCHB0", "SCRATCHB1", "SCRATCHB2", "SCRATCHB3",
53 #define LSI_MAX_DEVS 7
55 #define LSI_SCNTL0_TRG 0x01
56 #define LSI_SCNTL0_AAP 0x02
57 #define LSI_SCNTL0_EPC 0x08
58 #define LSI_SCNTL0_WATN 0x10
59 #define LSI_SCNTL0_START 0x20
61 #define LSI_SCNTL1_SST 0x01
62 #define LSI_SCNTL1_IARB 0x02
63 #define LSI_SCNTL1_AESP 0x04
64 #define LSI_SCNTL1_RST 0x08
65 #define LSI_SCNTL1_CON 0x10
66 #define LSI_SCNTL1_DHP 0x20
67 #define LSI_SCNTL1_ADB 0x40
68 #define LSI_SCNTL1_EXC 0x80
70 #define LSI_SCNTL2_WSR 0x01
71 #define LSI_SCNTL2_VUE0 0x02
72 #define LSI_SCNTL2_VUE1 0x04
73 #define LSI_SCNTL2_WSS 0x08
74 #define LSI_SCNTL2_SLPHBEN 0x10
75 #define LSI_SCNTL2_SLPMD 0x20
76 #define LSI_SCNTL2_CHM 0x40
77 #define LSI_SCNTL2_SDU 0x80
79 #define LSI_ISTAT0_DIP 0x01
80 #define LSI_ISTAT0_SIP 0x02
81 #define LSI_ISTAT0_INTF 0x04
82 #define LSI_ISTAT0_CON 0x08
83 #define LSI_ISTAT0_SEM 0x10
84 #define LSI_ISTAT0_SIGP 0x20
85 #define LSI_ISTAT0_SRST 0x40
86 #define LSI_ISTAT0_ABRT 0x80
88 #define LSI_ISTAT1_SI 0x01
89 #define LSI_ISTAT1_SRUN 0x02
90 #define LSI_ISTAT1_FLSH 0x04
92 #define LSI_SSTAT0_SDP0 0x01
93 #define LSI_SSTAT0_RST 0x02
94 #define LSI_SSTAT0_WOA 0x04
95 #define LSI_SSTAT0_LOA 0x08
96 #define LSI_SSTAT0_AIP 0x10
97 #define LSI_SSTAT0_OLF 0x20
98 #define LSI_SSTAT0_ORF 0x40
99 #define LSI_SSTAT0_ILF 0x80
101 #define LSI_SIST0_PAR 0x01
102 #define LSI_SIST0_RST 0x02
103 #define LSI_SIST0_UDC 0x04
104 #define LSI_SIST0_SGE 0x08
105 #define LSI_SIST0_RSL 0x10
106 #define LSI_SIST0_SEL 0x20
107 #define LSI_SIST0_CMP 0x40
108 #define LSI_SIST0_MA 0x80
110 #define LSI_SIST1_HTH 0x01
111 #define LSI_SIST1_GEN 0x02
112 #define LSI_SIST1_STO 0x04
113 #define LSI_SIST1_SBMC 0x10
115 #define LSI_SOCL_IO 0x01
116 #define LSI_SOCL_CD 0x02
117 #define LSI_SOCL_MSG 0x04
118 #define LSI_SOCL_ATN 0x08
119 #define LSI_SOCL_SEL 0x10
120 #define LSI_SOCL_BSY 0x20
121 #define LSI_SOCL_ACK 0x40
122 #define LSI_SOCL_REQ 0x80
124 #define LSI_DSTAT_IID 0x01
125 #define LSI_DSTAT_SIR 0x04
126 #define LSI_DSTAT_SSI 0x08
127 #define LSI_DSTAT_ABRT 0x10
128 #define LSI_DSTAT_BF 0x20
129 #define LSI_DSTAT_MDPE 0x40
130 #define LSI_DSTAT_DFE 0x80
132 #define LSI_DCNTL_COM 0x01
133 #define LSI_DCNTL_IRQD 0x02
134 #define LSI_DCNTL_STD 0x04
135 #define LSI_DCNTL_IRQM 0x08
136 #define LSI_DCNTL_SSM 0x10
137 #define LSI_DCNTL_PFEN 0x20
138 #define LSI_DCNTL_PFF 0x40
139 #define LSI_DCNTL_CLSE 0x80
141 #define LSI_DMODE_MAN 0x01
142 #define LSI_DMODE_BOF 0x02
143 #define LSI_DMODE_ERMP 0x04
144 #define LSI_DMODE_ERL 0x08
145 #define LSI_DMODE_DIOM 0x10
146 #define LSI_DMODE_SIOM 0x20
148 #define LSI_CTEST2_DACK 0x01
149 #define LSI_CTEST2_DREQ 0x02
150 #define LSI_CTEST2_TEOP 0x04
151 #define LSI_CTEST2_PCICIE 0x08
152 #define LSI_CTEST2_CM 0x10
153 #define LSI_CTEST2_CIO 0x20
154 #define LSI_CTEST2_SIGP 0x40
155 #define LSI_CTEST2_DDIR 0x80
157 #define LSI_CTEST5_BL2 0x04
158 #define LSI_CTEST5_DDIR 0x08
159 #define LSI_CTEST5_MASR 0x10
160 #define LSI_CTEST5_DFSN 0x20
161 #define LSI_CTEST5_BBCK 0x40
162 #define LSI_CTEST5_ADCK 0x80
164 #define LSI_CCNTL0_DILS 0x01
165 #define LSI_CCNTL0_DISFC 0x10
166 #define LSI_CCNTL0_ENNDJ 0x20
167 #define LSI_CCNTL0_PMJCTL 0x40
168 #define LSI_CCNTL0_ENPMJ 0x80
170 #define LSI_CCNTL1_EN64DBMV 0x01
171 #define LSI_CCNTL1_EN64TIBMV 0x02
172 #define LSI_CCNTL1_64TIMOD 0x04
173 #define LSI_CCNTL1_DDAC 0x08
174 #define LSI_CCNTL1_ZMOD 0x80
176 /* Enable Response to Reselection */
177 #define LSI_SCID_RRE 0x60
179 #define LSI_CCNTL1_40BIT (LSI_CCNTL1_EN64TIBMV|LSI_CCNTL1_64TIMOD)
189 /* Maximum length of MSG IN data. */
190 #define LSI_MAX_MSGIN_LEN 8
192 /* Flag set if this is a tagged command. */
193 #define LSI_TAG_VALID (1 << 16)
195 typedef struct lsi_request
{
202 QTAILQ_ENTRY(lsi_request
) next
;
207 PCIDevice parent_obj
;
210 MemoryRegion mmio_io
;
213 AddressSpace pci_io_as
;
215 int carry
; /* ??? Should this be an a visible register somewhere? */
217 /* Action to take at the end of a MSG IN phase.
218 0 = COMMAND, 1 = disconnect, 2 = DATA OUT, 3 = DATA IN. */
221 uint8_t msg
[LSI_MAX_MSGIN_LEN
];
222 /* 0 if SCRIPTS are running or stopped.
223 * 1 if a Wait Reselect instruction has been issued.
224 * 2 if processing DMA from lsi_execute_script.
225 * 3 if a DMA operation is in progress. */
229 /* The tag is a combination of the device ID and the SCSI tag. */
231 int command_complete
;
232 QTAILQ_HEAD(, lsi_request
) queue
;
233 lsi_request
*current
;
294 uint32_t scratch
[18]; /* SCRATCHA-SCRATCHR */
298 /* Script ram is stored as 32-bit words in host byteorder. */
299 uint32_t script_ram
[2048];
302 #define TYPE_LSI53C810 "lsi53c810"
303 #define TYPE_LSI53C895A "lsi53c895a"
305 #define LSI53C895A(obj) \
306 OBJECT_CHECK(LSIState, (obj), TYPE_LSI53C895A)
308 static inline int lsi_irq_on_rsl(LSIState
*s
)
310 return (s
->sien0
& LSI_SIST0_RSL
) && (s
->scid
& LSI_SCID_RRE
);
313 static void lsi_soft_reset(LSIState
*s
)
325 memset(s
->scratch
, 0, sizeof(s
->scratch
));
338 s
->ctest2
= LSI_CTEST2_DACK
;
381 assert(QTAILQ_EMPTY(&s
->queue
));
385 static int lsi_dma_40bit(LSIState
*s
)
387 if ((s
->ccntl1
& LSI_CCNTL1_40BIT
) == LSI_CCNTL1_40BIT
)
392 static int lsi_dma_ti64bit(LSIState
*s
)
394 if ((s
->ccntl1
& LSI_CCNTL1_EN64TIBMV
) == LSI_CCNTL1_EN64TIBMV
)
399 static int lsi_dma_64bit(LSIState
*s
)
401 if ((s
->ccntl1
& LSI_CCNTL1_EN64DBMV
) == LSI_CCNTL1_EN64DBMV
)
406 static uint8_t lsi_reg_readb(LSIState
*s
, int offset
);
407 static void lsi_reg_writeb(LSIState
*s
, int offset
, uint8_t val
);
408 static void lsi_execute_script(LSIState
*s
);
409 static void lsi_reselect(LSIState
*s
, lsi_request
*p
);
411 static inline void lsi_mem_read(LSIState
*s
, dma_addr_t addr
,
412 void *buf
, dma_addr_t len
)
414 if (s
->dmode
& LSI_DMODE_SIOM
) {
415 address_space_read(&s
->pci_io_as
, addr
, MEMTXATTRS_UNSPECIFIED
,
418 pci_dma_read(PCI_DEVICE(s
), addr
, buf
, len
);
422 static inline void lsi_mem_write(LSIState
*s
, dma_addr_t addr
,
423 const void *buf
, dma_addr_t len
)
425 if (s
->dmode
& LSI_DMODE_DIOM
) {
426 address_space_write(&s
->pci_io_as
, addr
, MEMTXATTRS_UNSPECIFIED
,
429 pci_dma_write(PCI_DEVICE(s
), addr
, buf
, len
);
433 static inline uint32_t read_dword(LSIState
*s
, uint32_t addr
)
437 pci_dma_read(PCI_DEVICE(s
), addr
, &buf
, 4);
438 return cpu_to_le32(buf
);
441 static void lsi_stop_script(LSIState
*s
)
443 s
->istat1
&= ~LSI_ISTAT1_SRUN
;
446 static void lsi_update_irq(LSIState
*s
)
448 PCIDevice
*d
= PCI_DEVICE(s
);
450 static int last_level
;
453 /* It's unclear whether the DIP/SIP bits should be cleared when the
454 Interrupt Status Registers are cleared or when istat0 is read.
455 We currently do the formwer, which seems to work. */
458 if (s
->dstat
& s
->dien
)
460 s
->istat0
|= LSI_ISTAT0_DIP
;
462 s
->istat0
&= ~LSI_ISTAT0_DIP
;
465 if (s
->sist0
|| s
->sist1
) {
466 if ((s
->sist0
& s
->sien0
) || (s
->sist1
& s
->sien1
))
468 s
->istat0
|= LSI_ISTAT0_SIP
;
470 s
->istat0
&= ~LSI_ISTAT0_SIP
;
472 if (s
->istat0
& LSI_ISTAT0_INTF
)
475 if (level
!= last_level
) {
476 DPRINTF("Update IRQ level %d dstat %02x sist %02x%02x\n",
477 level
, s
->dstat
, s
->sist1
, s
->sist0
);
480 pci_set_irq(d
, level
);
482 if (!level
&& lsi_irq_on_rsl(s
) && !(s
->scntl1
& LSI_SCNTL1_CON
)) {
483 DPRINTF("Handled IRQs & disconnected, looking for pending "
485 QTAILQ_FOREACH(p
, &s
->queue
, next
) {
494 /* Stop SCRIPTS execution and raise a SCSI interrupt. */
495 static void lsi_script_scsi_interrupt(LSIState
*s
, int stat0
, int stat1
)
500 DPRINTF("SCSI Interrupt 0x%02x%02x prev 0x%02x%02x\n",
501 stat1
, stat0
, s
->sist1
, s
->sist0
);
504 /* Stop processor on fatal or unmasked interrupt. As a special hack
505 we don't stop processing when raising STO. Instead continue
506 execution and stop at the next insn that accesses the SCSI bus. */
507 mask0
= s
->sien0
| ~(LSI_SIST0_CMP
| LSI_SIST0_SEL
| LSI_SIST0_RSL
);
508 mask1
= s
->sien1
| ~(LSI_SIST1_GEN
| LSI_SIST1_HTH
);
509 mask1
&= ~LSI_SIST1_STO
;
510 if (s
->sist0
& mask0
|| s
->sist1
& mask1
) {
516 /* Stop SCRIPTS execution and raise a DMA interrupt. */
517 static void lsi_script_dma_interrupt(LSIState
*s
, int stat
)
519 DPRINTF("DMA Interrupt 0x%x prev 0x%x\n", stat
, s
->dstat
);
525 static inline void lsi_set_phase(LSIState
*s
, int phase
)
527 s
->sstat1
= (s
->sstat1
& ~PHASE_MASK
) | phase
;
530 static void lsi_bad_phase(LSIState
*s
, int out
, int new_phase
)
532 /* Trigger a phase mismatch. */
533 if (s
->ccntl0
& LSI_CCNTL0_ENPMJ
) {
534 if ((s
->ccntl0
& LSI_CCNTL0_PMJCTL
)) {
535 s
->dsp
= out
? s
->pmjad1
: s
->pmjad2
;
537 s
->dsp
= (s
->scntl2
& LSI_SCNTL2_WSR
? s
->pmjad2
: s
->pmjad1
);
539 DPRINTF("Data phase mismatch jump to %08x\n", s
->dsp
);
541 DPRINTF("Phase mismatch interrupt\n");
542 lsi_script_scsi_interrupt(s
, LSI_SIST0_MA
, 0);
545 lsi_set_phase(s
, new_phase
);
549 /* Resume SCRIPTS execution after a DMA operation. */
550 static void lsi_resume_script(LSIState
*s
)
552 if (s
->waiting
!= 2) {
554 lsi_execute_script(s
);
560 static void lsi_disconnect(LSIState
*s
)
562 s
->scntl1
&= ~LSI_SCNTL1_CON
;
563 s
->sstat1
&= ~PHASE_MASK
;
566 static void lsi_bad_selection(LSIState
*s
, uint32_t id
)
568 DPRINTF("Selected absent target %d\n", id
);
569 lsi_script_scsi_interrupt(s
, 0, LSI_SIST1_STO
);
573 /* Initiate a SCSI layer data transfer. */
574 static void lsi_do_dma(LSIState
*s
, int out
)
581 if (!s
->current
->dma_len
) {
582 /* Wait until data is available. */
583 DPRINTF("DMA no data available\n");
587 dev
= s
->current
->req
->dev
;
591 if (count
> s
->current
->dma_len
)
592 count
= s
->current
->dma_len
;
595 /* both 40 and Table Indirect 64-bit DMAs store upper bits in dnad64 */
596 if (lsi_dma_40bit(s
) || lsi_dma_ti64bit(s
))
597 addr
|= ((uint64_t)s
->dnad64
<< 32);
599 addr
|= ((uint64_t)s
->dbms
<< 32);
601 addr
|= ((uint64_t)s
->sbms
<< 32);
603 DPRINTF("DMA addr=0x" DMA_ADDR_FMT
" len=%d\n", addr
, count
);
607 if (s
->current
->dma_buf
== NULL
) {
608 s
->current
->dma_buf
= scsi_req_get_buf(s
->current
->req
);
610 /* ??? Set SFBR to first data byte. */
612 lsi_mem_read(s
, addr
, s
->current
->dma_buf
, count
);
614 lsi_mem_write(s
, addr
, s
->current
->dma_buf
, count
);
616 s
->current
->dma_len
-= count
;
617 if (s
->current
->dma_len
== 0) {
618 s
->current
->dma_buf
= NULL
;
619 scsi_req_continue(s
->current
->req
);
621 s
->current
->dma_buf
+= count
;
622 lsi_resume_script(s
);
627 /* Add a command to the queue. */
628 static void lsi_queue_command(LSIState
*s
)
630 lsi_request
*p
= s
->current
;
632 DPRINTF("Queueing tag=0x%x\n", p
->tag
);
633 assert(s
->current
!= NULL
);
634 assert(s
->current
->dma_len
== 0);
635 QTAILQ_INSERT_TAIL(&s
->queue
, s
->current
, next
);
639 p
->out
= (s
->sstat1
& PHASE_MASK
) == PHASE_DO
;
642 /* Queue a byte for a MSG IN phase. */
643 static void lsi_add_msg_byte(LSIState
*s
, uint8_t data
)
645 if (s
->msg_len
>= LSI_MAX_MSGIN_LEN
) {
646 BADF("MSG IN data too long\n");
648 DPRINTF("MSG IN 0x%02x\n", data
);
649 s
->msg
[s
->msg_len
++] = data
;
653 /* Perform reselection to continue a command. */
654 static void lsi_reselect(LSIState
*s
, lsi_request
*p
)
658 assert(s
->current
== NULL
);
659 QTAILQ_REMOVE(&s
->queue
, p
, next
);
662 id
= (p
->tag
>> 8) & 0xf;
664 /* LSI53C700 Family Compatibility, see LSI53C895A 4-73 */
665 if (!(s
->dcntl
& LSI_DCNTL_COM
)) {
666 s
->sfbr
= 1 << (id
& 0x7);
668 DPRINTF("Reselected target %d\n", id
);
669 s
->scntl1
|= LSI_SCNTL1_CON
;
670 lsi_set_phase(s
, PHASE_MI
);
671 s
->msg_action
= p
->out
? 2 : 3;
672 s
->current
->dma_len
= p
->pending
;
673 lsi_add_msg_byte(s
, 0x80);
674 if (s
->current
->tag
& LSI_TAG_VALID
) {
675 lsi_add_msg_byte(s
, 0x20);
676 lsi_add_msg_byte(s
, p
->tag
& 0xff);
679 if (lsi_irq_on_rsl(s
)) {
680 lsi_script_scsi_interrupt(s
, LSI_SIST0_RSL
, 0);
684 static lsi_request
*lsi_find_by_tag(LSIState
*s
, uint32_t tag
)
688 QTAILQ_FOREACH(p
, &s
->queue
, next
) {
697 static void lsi_request_free(LSIState
*s
, lsi_request
*p
)
699 if (p
== s
->current
) {
702 QTAILQ_REMOVE(&s
->queue
, p
, next
);
707 static void lsi_request_cancelled(SCSIRequest
*req
)
709 LSIState
*s
= LSI53C895A(req
->bus
->qbus
.parent
);
710 lsi_request
*p
= req
->hba_private
;
712 req
->hba_private
= NULL
;
713 lsi_request_free(s
, p
);
717 /* Record that data is available for a queued command. Returns zero if
718 the device was reselected, nonzero if the IO is deferred. */
719 static int lsi_queue_req(LSIState
*s
, SCSIRequest
*req
, uint32_t len
)
721 lsi_request
*p
= req
->hba_private
;
724 BADF("Multiple IO pending for request %p\n", p
);
727 /* Reselect if waiting for it, or if reselection triggers an IRQ
729 Since no interrupt stacking is implemented in the emulation, it
730 is also required that there are no pending interrupts waiting
731 for service from the device driver. */
732 if (s
->waiting
== 1 ||
733 (lsi_irq_on_rsl(s
) && !(s
->scntl1
& LSI_SCNTL1_CON
) &&
734 !(s
->istat0
& (LSI_ISTAT0_SIP
| LSI_ISTAT0_DIP
)))) {
735 /* Reselect device. */
739 DPRINTF("Queueing IO tag=0x%x\n", p
->tag
);
745 /* Callback to indicate that the SCSI layer has completed a command. */
746 static void lsi_command_complete(SCSIRequest
*req
, uint32_t status
, size_t resid
)
748 LSIState
*s
= LSI53C895A(req
->bus
->qbus
.parent
);
751 out
= (s
->sstat1
& PHASE_MASK
) == PHASE_DO
;
752 DPRINTF("Command complete status=%d\n", (int)status
);
754 s
->command_complete
= 2;
755 if (s
->waiting
&& s
->dbc
!= 0) {
756 /* Raise phase mismatch for short transfers. */
757 lsi_bad_phase(s
, out
, PHASE_ST
);
759 lsi_set_phase(s
, PHASE_ST
);
762 if (req
->hba_private
== s
->current
) {
763 req
->hba_private
= NULL
;
764 lsi_request_free(s
, s
->current
);
767 lsi_resume_script(s
);
770 /* Callback to indicate that the SCSI layer has completed a transfer. */
771 static void lsi_transfer_data(SCSIRequest
*req
, uint32_t len
)
773 LSIState
*s
= LSI53C895A(req
->bus
->qbus
.parent
);
776 assert(req
->hba_private
);
777 if (s
->waiting
== 1 || req
->hba_private
!= s
->current
||
778 (lsi_irq_on_rsl(s
) && !(s
->scntl1
& LSI_SCNTL1_CON
))) {
779 if (lsi_queue_req(s
, req
, len
)) {
784 out
= (s
->sstat1
& PHASE_MASK
) == PHASE_DO
;
786 /* host adapter (re)connected */
787 DPRINTF("Data ready tag=0x%x len=%d\n", req
->tag
, len
);
788 s
->current
->dma_len
= len
;
789 s
->command_complete
= 1;
791 if (s
->waiting
== 1 || s
->dbc
== 0) {
792 lsi_resume_script(s
);
799 static void lsi_do_command(LSIState
*s
)
806 DPRINTF("Send command len=%d\n", s
->dbc
);
809 pci_dma_read(PCI_DEVICE(s
), s
->dnad
, buf
, s
->dbc
);
811 s
->command_complete
= 0;
813 id
= (s
->select_tag
>> 8) & 0xf;
814 dev
= scsi_device_find(&s
->bus
, 0, id
, s
->current_lun
);
816 lsi_bad_selection(s
, id
);
820 assert(s
->current
== NULL
);
821 s
->current
= g_new0(lsi_request
, 1);
822 s
->current
->tag
= s
->select_tag
;
823 s
->current
->req
= scsi_req_new(dev
, s
->current
->tag
, s
->current_lun
, buf
,
826 n
= scsi_req_enqueue(s
->current
->req
);
829 lsi_set_phase(s
, PHASE_DI
);
831 lsi_set_phase(s
, PHASE_DO
);
833 scsi_req_continue(s
->current
->req
);
835 if (!s
->command_complete
) {
837 /* Command did not complete immediately so disconnect. */
838 lsi_add_msg_byte(s
, 2); /* SAVE DATA POINTER */
839 lsi_add_msg_byte(s
, 4); /* DISCONNECT */
841 lsi_set_phase(s
, PHASE_MI
);
843 lsi_queue_command(s
);
845 /* wait command complete */
846 lsi_set_phase(s
, PHASE_DI
);
851 static void lsi_do_status(LSIState
*s
)
854 DPRINTF("Get status len=%d status=%d\n", s
->dbc
, s
->status
);
856 BADF("Bad Status move\n");
860 pci_dma_write(PCI_DEVICE(s
), s
->dnad
, &status
, 1);
861 lsi_set_phase(s
, PHASE_MI
);
863 lsi_add_msg_byte(s
, 0); /* COMMAND COMPLETE */
866 static void lsi_do_msgin(LSIState
*s
)
869 DPRINTF("Message in len=%d/%d\n", s
->dbc
, s
->msg_len
);
874 pci_dma_write(PCI_DEVICE(s
), s
->dnad
, s
->msg
, len
);
875 /* Linux drivers rely on the last byte being in the SIDL. */
876 s
->sidl
= s
->msg
[len
- 1];
879 memmove(s
->msg
, s
->msg
+ len
, s
->msg_len
);
881 /* ??? Check if ATN (not yet implemented) is asserted and maybe
882 switch to PHASE_MO. */
883 switch (s
->msg_action
) {
885 lsi_set_phase(s
, PHASE_CMD
);
891 lsi_set_phase(s
, PHASE_DO
);
894 lsi_set_phase(s
, PHASE_DI
);
902 /* Read the next byte during a MSGOUT phase. */
903 static uint8_t lsi_get_msgbyte(LSIState
*s
)
906 pci_dma_read(PCI_DEVICE(s
), s
->dnad
, &data
, 1);
912 /* Skip the next n bytes during a MSGOUT phase. */
913 static void lsi_skip_msgbytes(LSIState
*s
, unsigned int n
)
919 static void lsi_do_msgout(LSIState
*s
)
923 uint32_t current_tag
;
924 lsi_request
*current_req
, *p
, *p_next
;
927 current_tag
= s
->current
->tag
;
928 current_req
= s
->current
;
930 current_tag
= s
->select_tag
;
931 current_req
= lsi_find_by_tag(s
, current_tag
);
934 DPRINTF("MSG out len=%d\n", s
->dbc
);
936 msg
= lsi_get_msgbyte(s
);
941 DPRINTF("MSG: Disconnect\n");
945 DPRINTF("MSG: No Operation\n");
946 lsi_set_phase(s
, PHASE_CMD
);
949 len
= lsi_get_msgbyte(s
);
950 msg
= lsi_get_msgbyte(s
);
951 (void)len
; /* avoid a warning about unused variable*/
952 DPRINTF("Extended message 0x%x (len %d)\n", msg
, len
);
955 DPRINTF("SDTR (ignored)\n");
956 lsi_skip_msgbytes(s
, 2);
959 DPRINTF("WDTR (ignored)\n");
960 lsi_skip_msgbytes(s
, 1);
966 case 0x20: /* SIMPLE queue */
967 s
->select_tag
|= lsi_get_msgbyte(s
) | LSI_TAG_VALID
;
968 DPRINTF("SIMPLE queue tag=0x%x\n", s
->select_tag
& 0xff);
970 case 0x21: /* HEAD of queue */
971 BADF("HEAD queue not implemented\n");
972 s
->select_tag
|= lsi_get_msgbyte(s
) | LSI_TAG_VALID
;
974 case 0x22: /* ORDERED queue */
975 BADF("ORDERED queue not implemented\n");
976 s
->select_tag
|= lsi_get_msgbyte(s
) | LSI_TAG_VALID
;
979 /* The ABORT TAG message clears the current I/O process only. */
980 DPRINTF("MSG: ABORT TAG tag=0x%x\n", current_tag
);
982 scsi_req_cancel(current_req
->req
);
989 /* The ABORT message clears all I/O processes for the selecting
990 initiator on the specified logical unit of the target. */
992 DPRINTF("MSG: ABORT tag=0x%x\n", current_tag
);
994 /* The CLEAR QUEUE message clears all I/O processes for all
995 initiators on the specified logical unit of the target. */
997 DPRINTF("MSG: CLEAR QUEUE tag=0x%x\n", current_tag
);
999 /* The BUS DEVICE RESET message clears all I/O processes for all
1000 initiators on all logical units of the target. */
1002 DPRINTF("MSG: BUS DEVICE RESET tag=0x%x\n", current_tag
);
1005 /* clear the current I/O process */
1007 scsi_req_cancel(s
->current
->req
);
1010 /* As the current implemented devices scsi_disk and scsi_generic
1011 only support one LUN, we don't need to keep track of LUNs.
1012 Clearing I/O processes for other initiators could be possible
1013 for scsi_generic by sending a SG_SCSI_RESET to the /dev/sgX
1014 device, but this is currently not implemented (and seems not
1015 to be really necessary). So let's simply clear all queued
1016 commands for the current device: */
1017 QTAILQ_FOREACH_SAFE(p
, &s
->queue
, next
, p_next
) {
1018 if ((p
->tag
& 0x0000ff00) == (current_tag
& 0x0000ff00)) {
1019 scsi_req_cancel(p
->req
);
1026 if ((msg
& 0x80) == 0) {
1029 s
->current_lun
= msg
& 7;
1030 DPRINTF("Select LUN %d\n", s
->current_lun
);
1031 lsi_set_phase(s
, PHASE_CMD
);
1037 BADF("Unimplemented message 0x%02x\n", msg
);
1038 lsi_set_phase(s
, PHASE_MI
);
1039 lsi_add_msg_byte(s
, 7); /* MESSAGE REJECT */
1043 #define LSI_BUF_SIZE 4096
1044 static void lsi_memcpy(LSIState
*s
, uint32_t dest
, uint32_t src
, int count
)
1047 uint8_t buf
[LSI_BUF_SIZE
];
1049 DPRINTF("memcpy dest 0x%08x src 0x%08x count %d\n", dest
, src
, count
);
1051 n
= (count
> LSI_BUF_SIZE
) ? LSI_BUF_SIZE
: count
;
1052 lsi_mem_read(s
, src
, buf
, n
);
1053 lsi_mem_write(s
, dest
, buf
, n
);
1060 static void lsi_wait_reselect(LSIState
*s
)
1064 DPRINTF("Wait Reselect\n");
1066 QTAILQ_FOREACH(p
, &s
->queue
, next
) {
1072 if (s
->current
== NULL
) {
1077 static void lsi_execute_script(LSIState
*s
)
1079 PCIDevice
*pci_dev
= PCI_DEVICE(s
);
1081 uint32_t addr
, addr_high
;
1083 int insn_processed
= 0;
1085 s
->istat1
|= LSI_ISTAT1_SRUN
;
1088 insn
= read_dword(s
, s
->dsp
);
1090 /* If we receive an empty opcode increment the DSP by 4 bytes
1091 instead of 8 and execute the next opcode at that location */
1095 addr
= read_dword(s
, s
->dsp
+ 4);
1097 DPRINTF("SCRIPTS dsp=%08x opcode %08x arg %08x\n", s
->dsp
, insn
, addr
);
1099 s
->dcmd
= insn
>> 24;
1101 switch (insn
>> 30) {
1102 case 0: /* Block move. */
1103 if (s
->sist1
& LSI_SIST1_STO
) {
1104 DPRINTF("Delayed select timeout\n");
1108 s
->dbc
= insn
& 0xffffff;
1112 if (insn
& (1 << 29)) {
1113 /* Indirect addressing. */
1114 addr
= read_dword(s
, addr
);
1115 } else if (insn
& (1 << 28)) {
1118 /* Table indirect addressing. */
1120 /* 32-bit Table indirect */
1121 offset
= sextract32(addr
, 0, 24);
1122 pci_dma_read(pci_dev
, s
->dsa
+ offset
, buf
, 8);
1123 /* byte count is stored in bits 0:23 only */
1124 s
->dbc
= cpu_to_le32(buf
[0]) & 0xffffff;
1126 addr
= cpu_to_le32(buf
[1]);
1128 /* 40-bit DMA, upper addr bits [39:32] stored in first DWORD of
1129 * table, bits [31:24] */
1130 if (lsi_dma_40bit(s
))
1131 addr_high
= cpu_to_le32(buf
[0]) >> 24;
1132 else if (lsi_dma_ti64bit(s
)) {
1133 int selector
= (cpu_to_le32(buf
[0]) >> 24) & 0x1f;
1136 /* offset index into scratch registers since
1137 * TI64 mode can use registers C to R */
1138 addr_high
= s
->scratch
[2 + selector
];
1141 addr_high
= s
->mmrs
;
1144 addr_high
= s
->mmws
;
1153 addr_high
= s
->sbms
;
1156 addr_high
= s
->dbms
;
1159 BADF("Illegal selector specified (0x%x > 0x15)"
1160 " for 64-bit DMA block move", selector
);
1164 } else if (lsi_dma_64bit(s
)) {
1165 /* fetch a 3rd dword if 64-bit direct move is enabled and
1166 only if we're not doing table indirect or indirect addressing */
1167 s
->dbms
= read_dword(s
, s
->dsp
);
1169 s
->ia
= s
->dsp
- 12;
1171 if ((s
->sstat1
& PHASE_MASK
) != ((insn
>> 24) & 7)) {
1172 DPRINTF("Wrong phase got %d expected %d\n",
1173 s
->sstat1
& PHASE_MASK
, (insn
>> 24) & 7);
1174 lsi_script_scsi_interrupt(s
, LSI_SIST0_MA
, 0);
1178 s
->dnad64
= addr_high
;
1179 switch (s
->sstat1
& 0x7) {
1205 BADF("Unimplemented phase %d\n", s
->sstat1
& PHASE_MASK
);
1208 s
->dfifo
= s
->dbc
& 0xff;
1209 s
->ctest5
= (s
->ctest5
& 0xfc) | ((s
->dbc
>> 8) & 3);
1212 s
->ua
= addr
+ s
->dbc
;
1215 case 1: /* IO or Read/Write instruction. */
1216 opcode
= (insn
>> 27) & 7;
1220 if (insn
& (1 << 25)) {
1221 id
= read_dword(s
, s
->dsa
+ sextract32(insn
, 0, 24));
1225 id
= (id
>> 16) & 0xf;
1226 if (insn
& (1 << 26)) {
1227 addr
= s
->dsp
+ sextract32(addr
, 0, 24);
1231 case 0: /* Select */
1233 if (s
->scntl1
& LSI_SCNTL1_CON
) {
1234 DPRINTF("Already reselected, jumping to alternative address\n");
1238 s
->sstat0
|= LSI_SSTAT0_WOA
;
1239 s
->scntl1
&= ~LSI_SCNTL1_IARB
;
1240 if (!scsi_device_find(&s
->bus
, 0, id
, 0)) {
1241 lsi_bad_selection(s
, id
);
1244 DPRINTF("Selected target %d%s\n",
1245 id
, insn
& (1 << 3) ? " ATN" : "");
1246 /* ??? Linux drivers compain when this is set. Maybe
1247 it only applies in low-level mode (unimplemented).
1248 lsi_script_scsi_interrupt(s, LSI_SIST0_CMP, 0); */
1249 s
->select_tag
= id
<< 8;
1250 s
->scntl1
|= LSI_SCNTL1_CON
;
1251 if (insn
& (1 << 3)) {
1252 s
->socl
|= LSI_SOCL_ATN
;
1254 lsi_set_phase(s
, PHASE_MO
);
1256 case 1: /* Disconnect */
1257 DPRINTF("Wait Disconnect\n");
1258 s
->scntl1
&= ~LSI_SCNTL1_CON
;
1260 case 2: /* Wait Reselect */
1261 if (!lsi_irq_on_rsl(s
)) {
1262 lsi_wait_reselect(s
);
1266 DPRINTF("Set%s%s%s%s\n",
1267 insn
& (1 << 3) ? " ATN" : "",
1268 insn
& (1 << 6) ? " ACK" : "",
1269 insn
& (1 << 9) ? " TM" : "",
1270 insn
& (1 << 10) ? " CC" : "");
1271 if (insn
& (1 << 3)) {
1272 s
->socl
|= LSI_SOCL_ATN
;
1273 lsi_set_phase(s
, PHASE_MO
);
1275 if (insn
& (1 << 9)) {
1276 BADF("Target mode not implemented\n");
1279 if (insn
& (1 << 10))
1283 DPRINTF("Clear%s%s%s%s\n",
1284 insn
& (1 << 3) ? " ATN" : "",
1285 insn
& (1 << 6) ? " ACK" : "",
1286 insn
& (1 << 9) ? " TM" : "",
1287 insn
& (1 << 10) ? " CC" : "");
1288 if (insn
& (1 << 3)) {
1289 s
->socl
&= ~LSI_SOCL_ATN
;
1291 if (insn
& (1 << 10))
1302 static const char *opcode_names
[3] =
1303 {"Write", "Read", "Read-Modify-Write"};
1304 static const char *operator_names
[8] =
1305 {"MOV", "SHL", "OR", "XOR", "AND", "SHR", "ADD", "ADC"};
1308 reg
= ((insn
>> 16) & 0x7f) | (insn
& 0x80);
1309 data8
= (insn
>> 8) & 0xff;
1310 opcode
= (insn
>> 27) & 7;
1311 operator = (insn
>> 24) & 7;
1312 DPRINTF("%s reg 0x%x %s data8=0x%02x sfbr=0x%02x%s\n",
1313 opcode_names
[opcode
- 5], reg
,
1314 operator_names
[operator], data8
, s
->sfbr
,
1315 (insn
& (1 << 23)) ? " SFBR" : "");
1318 case 5: /* From SFBR */
1322 case 6: /* To SFBR */
1324 op0
= lsi_reg_readb(s
, reg
);
1327 case 7: /* Read-modify-write */
1329 op0
= lsi_reg_readb(s
, reg
);
1330 if (insn
& (1 << 23)) {
1342 case 1: /* Shift left */
1344 op0
= (op0
<< 1) | s
->carry
;
1358 op0
= (op0
>> 1) | (s
->carry
<< 7);
1363 s
->carry
= op0
< op1
;
1366 op0
+= op1
+ s
->carry
;
1368 s
->carry
= op0
<= op1
;
1370 s
->carry
= op0
< op1
;
1375 case 5: /* From SFBR */
1376 case 7: /* Read-modify-write */
1377 lsi_reg_writeb(s
, reg
, op0
);
1379 case 6: /* To SFBR */
1386 case 2: /* Transfer Control. */
1391 if ((insn
& 0x002e0000) == 0) {
1395 if (s
->sist1
& LSI_SIST1_STO
) {
1396 DPRINTF("Delayed select timeout\n");
1400 cond
= jmp
= (insn
& (1 << 19)) != 0;
1401 if (cond
== jmp
&& (insn
& (1 << 21))) {
1402 DPRINTF("Compare carry %d\n", s
->carry
== jmp
);
1403 cond
= s
->carry
!= 0;
1405 if (cond
== jmp
&& (insn
& (1 << 17))) {
1406 DPRINTF("Compare phase %d %c= %d\n",
1407 (s
->sstat1
& PHASE_MASK
),
1409 ((insn
>> 24) & 7));
1410 cond
= (s
->sstat1
& PHASE_MASK
) == ((insn
>> 24) & 7);
1412 if (cond
== jmp
&& (insn
& (1 << 18))) {
1415 mask
= (~insn
>> 8) & 0xff;
1416 DPRINTF("Compare data 0x%x & 0x%x %c= 0x%x\n",
1417 s
->sfbr
, mask
, jmp
? '=' : '!', insn
& mask
);
1418 cond
= (s
->sfbr
& mask
) == (insn
& mask
);
1421 if (insn
& (1 << 23)) {
1422 /* Relative address. */
1423 addr
= s
->dsp
+ sextract32(addr
, 0, 24);
1425 switch ((insn
>> 27) & 7) {
1427 DPRINTF("Jump to 0x%08x\n", addr
);
1432 DPRINTF("Call 0x%08x\n", addr
);
1436 case 2: /* Return */
1437 DPRINTF("Return to 0x%08x\n", s
->temp
);
1440 case 3: /* Interrupt */
1441 DPRINTF("Interrupt 0x%08x\n", s
->dsps
);
1442 if ((insn
& (1 << 20)) != 0) {
1443 s
->istat0
|= LSI_ISTAT0_INTF
;
1446 lsi_script_dma_interrupt(s
, LSI_DSTAT_SIR
);
1450 DPRINTF("Illegal transfer control\n");
1451 lsi_script_dma_interrupt(s
, LSI_DSTAT_IID
);
1455 DPRINTF("Control condition failed\n");
1461 if ((insn
& (1 << 29)) == 0) {
1464 /* ??? The docs imply the destination address is loaded into
1465 the TEMP register. However the Linux drivers rely on
1466 the value being presrved. */
1467 dest
= read_dword(s
, s
->dsp
);
1469 lsi_memcpy(s
, dest
, addr
, insn
& 0xffffff);
1476 if (insn
& (1 << 28)) {
1477 addr
= s
->dsa
+ sextract32(addr
, 0, 24);
1480 reg
= (insn
>> 16) & 0xff;
1481 if (insn
& (1 << 24)) {
1482 pci_dma_read(pci_dev
, addr
, data
, n
);
1483 DPRINTF("Load reg 0x%x size %d addr 0x%08x = %08x\n", reg
, n
,
1484 addr
, *(int *)data
);
1485 for (i
= 0; i
< n
; i
++) {
1486 lsi_reg_writeb(s
, reg
+ i
, data
[i
]);
1489 DPRINTF("Store reg 0x%x size %d addr 0x%08x\n", reg
, n
, addr
);
1490 for (i
= 0; i
< n
; i
++) {
1491 data
[i
] = lsi_reg_readb(s
, reg
+ i
);
1493 pci_dma_write(pci_dev
, addr
, data
, n
);
1497 if (insn_processed
> 10000 && !s
->waiting
) {
1498 /* Some windows drivers make the device spin waiting for a memory
1499 location to change. If we have been executed a lot of code then
1500 assume this is the case and force an unexpected device disconnect.
1501 This is apparently sufficient to beat the drivers into submission.
1503 if (!(s
->sien0
& LSI_SIST0_UDC
))
1504 fprintf(stderr
, "inf. loop with UDC masked\n");
1505 lsi_script_scsi_interrupt(s
, LSI_SIST0_UDC
, 0);
1507 } else if (s
->istat1
& LSI_ISTAT1_SRUN
&& !s
->waiting
) {
1508 if (s
->dcntl
& LSI_DCNTL_SSM
) {
1509 lsi_script_dma_interrupt(s
, LSI_DSTAT_SSI
);
1514 DPRINTF("SCRIPTS execution stopped\n");
1517 static uint8_t lsi_reg_readb(LSIState
*s
, int offset
)
1521 #define CASE_GET_REG24(name, addr) \
1522 case addr: ret = s->name & 0xff; break; \
1523 case addr + 1: ret = (s->name >> 8) & 0xff; break; \
1524 case addr + 2: ret = (s->name >> 16) & 0xff; break;
1526 #define CASE_GET_REG32(name, addr) \
1527 case addr: ret = s->name & 0xff; break; \
1528 case addr + 1: ret = (s->name >> 8) & 0xff; break; \
1529 case addr + 2: ret = (s->name >> 16) & 0xff; break; \
1530 case addr + 3: ret = (s->name >> 24) & 0xff; break;
1533 case 0x00: /* SCNTL0 */
1536 case 0x01: /* SCNTL1 */
1539 case 0x02: /* SCNTL2 */
1542 case 0x03: /* SCNTL3 */
1545 case 0x04: /* SCID */
1548 case 0x05: /* SXFER */
1551 case 0x06: /* SDID */
1554 case 0x07: /* GPREG0 */
1557 case 0x08: /* Revision ID */
1560 case 0x09: /* SOCL */
1563 case 0xa: /* SSID */
1566 case 0xb: /* SBCL */
1567 /* ??? This is not correct. However it's (hopefully) only
1568 used for diagnostics, so should be ok. */
1571 case 0xc: /* DSTAT */
1572 ret
= s
->dstat
| LSI_DSTAT_DFE
;
1573 if ((s
->istat0
& LSI_ISTAT0_INTF
) == 0)
1577 case 0x0d: /* SSTAT0 */
1580 case 0x0e: /* SSTAT1 */
1583 case 0x0f: /* SSTAT2 */
1584 ret
= s
->scntl1
& LSI_SCNTL1_CON
? 0 : 2;
1586 CASE_GET_REG32(dsa
, 0x10)
1587 case 0x14: /* ISTAT0 */
1590 case 0x15: /* ISTAT1 */
1593 case 0x16: /* MBOX0 */
1596 case 0x17: /* MBOX1 */
1599 case 0x18: /* CTEST0 */
1602 case 0x19: /* CTEST1 */
1605 case 0x1a: /* CTEST2 */
1606 ret
= s
->ctest2
| LSI_CTEST2_DACK
| LSI_CTEST2_CM
;
1607 if (s
->istat0
& LSI_ISTAT0_SIGP
) {
1608 s
->istat0
&= ~LSI_ISTAT0_SIGP
;
1609 ret
|= LSI_CTEST2_SIGP
;
1612 case 0x1b: /* CTEST3 */
1615 CASE_GET_REG32(temp
, 0x1c)
1616 case 0x20: /* DFIFO */
1619 case 0x21: /* CTEST4 */
1622 case 0x22: /* CTEST5 */
1625 case 0x23: /* CTEST6 */
1628 CASE_GET_REG24(dbc
, 0x24)
1629 case 0x27: /* DCMD */
1632 CASE_GET_REG32(dnad
, 0x28)
1633 CASE_GET_REG32(dsp
, 0x2c)
1634 CASE_GET_REG32(dsps
, 0x30)
1635 CASE_GET_REG32(scratch
[0], 0x34)
1636 case 0x38: /* DMODE */
1639 case 0x39: /* DIEN */
1642 case 0x3a: /* SBR */
1645 case 0x3b: /* DCNTL */
1648 /* ADDER Output (Debug of relative jump address) */
1649 CASE_GET_REG32(adder
, 0x3c)
1650 case 0x40: /* SIEN0 */
1653 case 0x41: /* SIEN1 */
1656 case 0x42: /* SIST0 */
1661 case 0x43: /* SIST1 */
1666 case 0x46: /* MACNTL */
1669 case 0x47: /* GPCNTL0 */
1672 case 0x48: /* STIME0 */
1675 case 0x4a: /* RESPID0 */
1678 case 0x4b: /* RESPID1 */
1681 case 0x4d: /* STEST1 */
1684 case 0x4e: /* STEST2 */
1687 case 0x4f: /* STEST3 */
1690 case 0x50: /* SIDL */
1691 /* This is needed by the linux drivers. We currently only update it
1692 during the MSG IN phase. */
1695 case 0x52: /* STEST4 */
1698 case 0x56: /* CCNTL0 */
1701 case 0x57: /* CCNTL1 */
1704 case 0x58: /* SBDL */
1705 /* Some drivers peek at the data bus during the MSG IN phase. */
1706 if ((s
->sstat1
& PHASE_MASK
) == PHASE_MI
)
1710 case 0x59: /* SBDL high */
1713 CASE_GET_REG32(mmrs
, 0xa0)
1714 CASE_GET_REG32(mmws
, 0xa4)
1715 CASE_GET_REG32(sfs
, 0xa8)
1716 CASE_GET_REG32(drs
, 0xac)
1717 CASE_GET_REG32(sbms
, 0xb0)
1718 CASE_GET_REG32(dbms
, 0xb4)
1719 CASE_GET_REG32(dnad64
, 0xb8)
1720 CASE_GET_REG32(pmjad1
, 0xc0)
1721 CASE_GET_REG32(pmjad2
, 0xc4)
1722 CASE_GET_REG32(rbc
, 0xc8)
1723 CASE_GET_REG32(ua
, 0xcc)
1724 CASE_GET_REG32(ia
, 0xd4)
1725 CASE_GET_REG32(sbc
, 0xd8)
1726 CASE_GET_REG32(csbc
, 0xdc)
1731 n
= (offset
- 0x58) >> 2;
1732 shift
= (offset
& 3) * 8;
1733 ret
= (s
->scratch
[n
] >> shift
) & 0xff;
1738 qemu_log_mask(LOG_GUEST_ERROR
,
1739 "lsi_scsi: invalid read from reg %s %x\n",
1740 offset
< ARRAY_SIZE(names
) ? names
[offset
] : "???",
1746 #undef CASE_GET_REG24
1747 #undef CASE_GET_REG32
1749 #ifdef DEBUG_LSI_REG
1750 DPRINTF("Read reg %s %x = %02x\n",
1751 offset
< ARRAY_SIZE(names
) ? names
[offset
] : "???", offset
, ret
);
1757 static void lsi_reg_writeb(LSIState
*s
, int offset
, uint8_t val
)
1759 #define CASE_SET_REG24(name, addr) \
1760 case addr : s->name &= 0xffffff00; s->name |= val; break; \
1761 case addr + 1: s->name &= 0xffff00ff; s->name |= val << 8; break; \
1762 case addr + 2: s->name &= 0xff00ffff; s->name |= val << 16; break;
1764 #define CASE_SET_REG32(name, addr) \
1765 case addr : s->name &= 0xffffff00; s->name |= val; break; \
1766 case addr + 1: s->name &= 0xffff00ff; s->name |= val << 8; break; \
1767 case addr + 2: s->name &= 0xff00ffff; s->name |= val << 16; break; \
1768 case addr + 3: s->name &= 0x00ffffff; s->name |= val << 24; break;
1770 #ifdef DEBUG_LSI_REG
1771 DPRINTF("Write reg %s %x = %02x\n",
1772 offset
< ARRAY_SIZE(names
) ? names
[offset
] : "???", offset
, val
);
1775 case 0x00: /* SCNTL0 */
1777 if (val
& LSI_SCNTL0_START
) {
1778 BADF("Start sequence not implemented\n");
1781 case 0x01: /* SCNTL1 */
1782 s
->scntl1
= val
& ~LSI_SCNTL1_SST
;
1783 if (val
& LSI_SCNTL1_IARB
) {
1784 BADF("Immediate Arbritration not implemented\n");
1786 if (val
& LSI_SCNTL1_RST
) {
1787 if (!(s
->sstat0
& LSI_SSTAT0_RST
)) {
1788 qbus_reset_all(&s
->bus
.qbus
);
1789 s
->sstat0
|= LSI_SSTAT0_RST
;
1790 lsi_script_scsi_interrupt(s
, LSI_SIST0_RST
, 0);
1793 s
->sstat0
&= ~LSI_SSTAT0_RST
;
1796 case 0x02: /* SCNTL2 */
1797 val
&= ~(LSI_SCNTL2_WSR
| LSI_SCNTL2_WSS
);
1800 case 0x03: /* SCNTL3 */
1803 case 0x04: /* SCID */
1806 case 0x05: /* SXFER */
1809 case 0x06: /* SDID */
1810 if ((s
->ssid
& 0x80) && (val
& 0xf) != (s
->ssid
& 0xf)) {
1811 BADF("Destination ID does not match SSID\n");
1813 s
->sdid
= val
& 0xf;
1815 case 0x07: /* GPREG0 */
1817 case 0x08: /* SFBR */
1818 /* The CPU is not allowed to write to this register. However the
1819 SCRIPTS register move instructions are. */
1822 case 0x0a: case 0x0b:
1823 /* Openserver writes to these readonly registers on startup */
1825 case 0x0c: case 0x0d: case 0x0e: case 0x0f:
1826 /* Linux writes to these readonly registers on startup. */
1828 CASE_SET_REG32(dsa
, 0x10)
1829 case 0x14: /* ISTAT0 */
1830 s
->istat0
= (s
->istat0
& 0x0f) | (val
& 0xf0);
1831 if (val
& LSI_ISTAT0_ABRT
) {
1832 lsi_script_dma_interrupt(s
, LSI_DSTAT_ABRT
);
1834 if (val
& LSI_ISTAT0_INTF
) {
1835 s
->istat0
&= ~LSI_ISTAT0_INTF
;
1838 if (s
->waiting
== 1 && val
& LSI_ISTAT0_SIGP
) {
1839 DPRINTF("Woken by SIGP\n");
1842 lsi_execute_script(s
);
1844 if (val
& LSI_ISTAT0_SRST
) {
1845 qdev_reset_all(DEVICE(s
));
1848 case 0x16: /* MBOX0 */
1851 case 0x17: /* MBOX1 */
1854 case 0x18: /* CTEST0 */
1857 case 0x1a: /* CTEST2 */
1858 s
->ctest2
= val
& LSI_CTEST2_PCICIE
;
1860 case 0x1b: /* CTEST3 */
1861 s
->ctest3
= val
& 0x0f;
1863 CASE_SET_REG32(temp
, 0x1c)
1864 case 0x21: /* CTEST4 */
1866 BADF("Unimplemented CTEST4-FBL 0x%x\n", val
);
1870 case 0x22: /* CTEST5 */
1871 if (val
& (LSI_CTEST5_ADCK
| LSI_CTEST5_BBCK
)) {
1872 BADF("CTEST5 DMA increment not implemented\n");
1876 CASE_SET_REG24(dbc
, 0x24)
1877 CASE_SET_REG32(dnad
, 0x28)
1878 case 0x2c: /* DSP[0:7] */
1879 s
->dsp
&= 0xffffff00;
1882 case 0x2d: /* DSP[8:15] */
1883 s
->dsp
&= 0xffff00ff;
1886 case 0x2e: /* DSP[16:23] */
1887 s
->dsp
&= 0xff00ffff;
1888 s
->dsp
|= val
<< 16;
1890 case 0x2f: /* DSP[24:31] */
1891 s
->dsp
&= 0x00ffffff;
1892 s
->dsp
|= val
<< 24;
1893 if ((s
->dmode
& LSI_DMODE_MAN
) == 0
1894 && (s
->istat1
& LSI_ISTAT1_SRUN
) == 0)
1895 lsi_execute_script(s
);
1897 CASE_SET_REG32(dsps
, 0x30)
1898 CASE_SET_REG32(scratch
[0], 0x34)
1899 case 0x38: /* DMODE */
1902 case 0x39: /* DIEN */
1906 case 0x3a: /* SBR */
1909 case 0x3b: /* DCNTL */
1910 s
->dcntl
= val
& ~(LSI_DCNTL_PFF
| LSI_DCNTL_STD
);
1911 if ((val
& LSI_DCNTL_STD
) && (s
->istat1
& LSI_ISTAT1_SRUN
) == 0)
1912 lsi_execute_script(s
);
1914 case 0x40: /* SIEN0 */
1918 case 0x41: /* SIEN1 */
1922 case 0x47: /* GPCNTL0 */
1924 case 0x48: /* STIME0 */
1927 case 0x49: /* STIME1 */
1929 DPRINTF("General purpose timer not implemented\n");
1930 /* ??? Raising the interrupt immediately seems to be sufficient
1931 to keep the FreeBSD driver happy. */
1932 lsi_script_scsi_interrupt(s
, 0, LSI_SIST1_GEN
);
1935 case 0x4a: /* RESPID0 */
1938 case 0x4b: /* RESPID1 */
1941 case 0x4d: /* STEST1 */
1944 case 0x4e: /* STEST2 */
1946 BADF("Low level mode not implemented\n");
1950 case 0x4f: /* STEST3 */
1952 BADF("SCSI FIFO test mode not implemented\n");
1956 case 0x56: /* CCNTL0 */
1959 case 0x57: /* CCNTL1 */
1962 CASE_SET_REG32(mmrs
, 0xa0)
1963 CASE_SET_REG32(mmws
, 0xa4)
1964 CASE_SET_REG32(sfs
, 0xa8)
1965 CASE_SET_REG32(drs
, 0xac)
1966 CASE_SET_REG32(sbms
, 0xb0)
1967 CASE_SET_REG32(dbms
, 0xb4)
1968 CASE_SET_REG32(dnad64
, 0xb8)
1969 CASE_SET_REG32(pmjad1
, 0xc0)
1970 CASE_SET_REG32(pmjad2
, 0xc4)
1971 CASE_SET_REG32(rbc
, 0xc8)
1972 CASE_SET_REG32(ua
, 0xcc)
1973 CASE_SET_REG32(ia
, 0xd4)
1974 CASE_SET_REG32(sbc
, 0xd8)
1975 CASE_SET_REG32(csbc
, 0xdc)
1977 if (offset
>= 0x5c && offset
< 0xa0) {
1980 n
= (offset
- 0x58) >> 2;
1981 shift
= (offset
& 3) * 8;
1982 s
->scratch
[n
] = deposit32(s
->scratch
[n
], shift
, 8, val
);
1984 qemu_log_mask(LOG_GUEST_ERROR
,
1985 "lsi_scsi: invalid write to reg %s %x (0x%02x)\n",
1986 offset
< ARRAY_SIZE(names
) ? names
[offset
] : "???",
1990 #undef CASE_SET_REG24
1991 #undef CASE_SET_REG32
1994 static void lsi_mmio_write(void *opaque
, hwaddr addr
,
1995 uint64_t val
, unsigned size
)
1997 LSIState
*s
= opaque
;
1999 lsi_reg_writeb(s
, addr
& 0xff, val
);
2002 static uint64_t lsi_mmio_read(void *opaque
, hwaddr addr
,
2005 LSIState
*s
= opaque
;
2007 return lsi_reg_readb(s
, addr
& 0xff);
2010 static const MemoryRegionOps lsi_mmio_ops
= {
2011 .read
= lsi_mmio_read
,
2012 .write
= lsi_mmio_write
,
2013 .endianness
= DEVICE_NATIVE_ENDIAN
,
2015 .min_access_size
= 1,
2016 .max_access_size
= 1,
2020 static void lsi_ram_write(void *opaque
, hwaddr addr
,
2021 uint64_t val
, unsigned size
)
2023 LSIState
*s
= opaque
;
2028 newval
= s
->script_ram
[addr
>> 2];
2029 shift
= (addr
& 3) * 8;
2030 mask
= ((uint64_t)1 << (size
* 8)) - 1;
2031 newval
&= ~(mask
<< shift
);
2032 newval
|= val
<< shift
;
2033 s
->script_ram
[addr
>> 2] = newval
;
2036 static uint64_t lsi_ram_read(void *opaque
, hwaddr addr
,
2039 LSIState
*s
= opaque
;
2043 val
= s
->script_ram
[addr
>> 2];
2044 mask
= ((uint64_t)1 << (size
* 8)) - 1;
2045 val
>>= (addr
& 3) * 8;
2049 static const MemoryRegionOps lsi_ram_ops
= {
2050 .read
= lsi_ram_read
,
2051 .write
= lsi_ram_write
,
2052 .endianness
= DEVICE_NATIVE_ENDIAN
,
2055 static uint64_t lsi_io_read(void *opaque
, hwaddr addr
,
2058 LSIState
*s
= opaque
;
2059 return lsi_reg_readb(s
, addr
& 0xff);
2062 static void lsi_io_write(void *opaque
, hwaddr addr
,
2063 uint64_t val
, unsigned size
)
2065 LSIState
*s
= opaque
;
2066 lsi_reg_writeb(s
, addr
& 0xff, val
);
2069 static const MemoryRegionOps lsi_io_ops
= {
2070 .read
= lsi_io_read
,
2071 .write
= lsi_io_write
,
2072 .endianness
= DEVICE_NATIVE_ENDIAN
,
2074 .min_access_size
= 1,
2075 .max_access_size
= 1,
2079 static void lsi_scsi_reset(DeviceState
*dev
)
2081 LSIState
*s
= LSI53C895A(dev
);
2086 static int lsi_pre_save(void *opaque
)
2088 LSIState
*s
= opaque
;
2091 assert(s
->current
->dma_buf
== NULL
);
2092 assert(s
->current
->dma_len
== 0);
2094 assert(QTAILQ_EMPTY(&s
->queue
));
2099 static const VMStateDescription vmstate_lsi_scsi
= {
2102 .minimum_version_id
= 0,
2103 .pre_save
= lsi_pre_save
,
2104 .fields
= (VMStateField
[]) {
2105 VMSTATE_PCI_DEVICE(parent_obj
, LSIState
),
2107 VMSTATE_INT32(carry
, LSIState
),
2108 VMSTATE_INT32(status
, LSIState
),
2109 VMSTATE_INT32(msg_action
, LSIState
),
2110 VMSTATE_INT32(msg_len
, LSIState
),
2111 VMSTATE_BUFFER(msg
, LSIState
),
2112 VMSTATE_INT32(waiting
, LSIState
),
2114 VMSTATE_UINT32(dsa
, LSIState
),
2115 VMSTATE_UINT32(temp
, LSIState
),
2116 VMSTATE_UINT32(dnad
, LSIState
),
2117 VMSTATE_UINT32(dbc
, LSIState
),
2118 VMSTATE_UINT8(istat0
, LSIState
),
2119 VMSTATE_UINT8(istat1
, LSIState
),
2120 VMSTATE_UINT8(dcmd
, LSIState
),
2121 VMSTATE_UINT8(dstat
, LSIState
),
2122 VMSTATE_UINT8(dien
, LSIState
),
2123 VMSTATE_UINT8(sist0
, LSIState
),
2124 VMSTATE_UINT8(sist1
, LSIState
),
2125 VMSTATE_UINT8(sien0
, LSIState
),
2126 VMSTATE_UINT8(sien1
, LSIState
),
2127 VMSTATE_UINT8(mbox0
, LSIState
),
2128 VMSTATE_UINT8(mbox1
, LSIState
),
2129 VMSTATE_UINT8(dfifo
, LSIState
),
2130 VMSTATE_UINT8(ctest2
, LSIState
),
2131 VMSTATE_UINT8(ctest3
, LSIState
),
2132 VMSTATE_UINT8(ctest4
, LSIState
),
2133 VMSTATE_UINT8(ctest5
, LSIState
),
2134 VMSTATE_UINT8(ccntl0
, LSIState
),
2135 VMSTATE_UINT8(ccntl1
, LSIState
),
2136 VMSTATE_UINT32(dsp
, LSIState
),
2137 VMSTATE_UINT32(dsps
, LSIState
),
2138 VMSTATE_UINT8(dmode
, LSIState
),
2139 VMSTATE_UINT8(dcntl
, LSIState
),
2140 VMSTATE_UINT8(scntl0
, LSIState
),
2141 VMSTATE_UINT8(scntl1
, LSIState
),
2142 VMSTATE_UINT8(scntl2
, LSIState
),
2143 VMSTATE_UINT8(scntl3
, LSIState
),
2144 VMSTATE_UINT8(sstat0
, LSIState
),
2145 VMSTATE_UINT8(sstat1
, LSIState
),
2146 VMSTATE_UINT8(scid
, LSIState
),
2147 VMSTATE_UINT8(sxfer
, LSIState
),
2148 VMSTATE_UINT8(socl
, LSIState
),
2149 VMSTATE_UINT8(sdid
, LSIState
),
2150 VMSTATE_UINT8(ssid
, LSIState
),
2151 VMSTATE_UINT8(sfbr
, LSIState
),
2152 VMSTATE_UINT8(stest1
, LSIState
),
2153 VMSTATE_UINT8(stest2
, LSIState
),
2154 VMSTATE_UINT8(stest3
, LSIState
),
2155 VMSTATE_UINT8(sidl
, LSIState
),
2156 VMSTATE_UINT8(stime0
, LSIState
),
2157 VMSTATE_UINT8(respid0
, LSIState
),
2158 VMSTATE_UINT8(respid1
, LSIState
),
2159 VMSTATE_UINT32(mmrs
, LSIState
),
2160 VMSTATE_UINT32(mmws
, LSIState
),
2161 VMSTATE_UINT32(sfs
, LSIState
),
2162 VMSTATE_UINT32(drs
, LSIState
),
2163 VMSTATE_UINT32(sbms
, LSIState
),
2164 VMSTATE_UINT32(dbms
, LSIState
),
2165 VMSTATE_UINT32(dnad64
, LSIState
),
2166 VMSTATE_UINT32(pmjad1
, LSIState
),
2167 VMSTATE_UINT32(pmjad2
, LSIState
),
2168 VMSTATE_UINT32(rbc
, LSIState
),
2169 VMSTATE_UINT32(ua
, LSIState
),
2170 VMSTATE_UINT32(ia
, LSIState
),
2171 VMSTATE_UINT32(sbc
, LSIState
),
2172 VMSTATE_UINT32(csbc
, LSIState
),
2173 VMSTATE_BUFFER_UNSAFE(scratch
, LSIState
, 0, 18 * sizeof(uint32_t)),
2174 VMSTATE_UINT8(sbr
, LSIState
),
2176 VMSTATE_BUFFER_UNSAFE(script_ram
, LSIState
, 0, 2048 * sizeof(uint32_t)),
2177 VMSTATE_END_OF_LIST()
2181 static const struct SCSIBusInfo lsi_scsi_info
= {
2183 .max_target
= LSI_MAX_DEVS
,
2184 .max_lun
= 0, /* LUN support is buggy */
2186 .transfer_data
= lsi_transfer_data
,
2187 .complete
= lsi_command_complete
,
2188 .cancel
= lsi_request_cancelled
2191 static void lsi_scsi_realize(PCIDevice
*dev
, Error
**errp
)
2193 LSIState
*s
= LSI53C895A(dev
);
2194 DeviceState
*d
= DEVICE(dev
);
2197 pci_conf
= dev
->config
;
2199 /* PCI latency timer = 255 */
2200 pci_conf
[PCI_LATENCY_TIMER
] = 0xff;
2201 /* Interrupt pin A */
2202 pci_conf
[PCI_INTERRUPT_PIN
] = 0x01;
2204 memory_region_init_io(&s
->mmio_io
, OBJECT(s
), &lsi_mmio_ops
, s
,
2206 memory_region_init_io(&s
->ram_io
, OBJECT(s
), &lsi_ram_ops
, s
,
2208 memory_region_init_io(&s
->io_io
, OBJECT(s
), &lsi_io_ops
, s
,
2211 address_space_init(&s
->pci_io_as
, pci_address_space_io(dev
), "lsi-pci-io");
2213 pci_register_bar(dev
, 0, PCI_BASE_ADDRESS_SPACE_IO
, &s
->io_io
);
2214 pci_register_bar(dev
, 1, PCI_BASE_ADDRESS_SPACE_MEMORY
, &s
->mmio_io
);
2215 pci_register_bar(dev
, 2, PCI_BASE_ADDRESS_SPACE_MEMORY
, &s
->ram_io
);
2216 QTAILQ_INIT(&s
->queue
);
2218 scsi_bus_new(&s
->bus
, sizeof(s
->bus
), d
, &lsi_scsi_info
, NULL
);
2221 static void lsi_scsi_unrealize(DeviceState
*dev
, Error
**errp
)
2223 LSIState
*s
= LSI53C895A(dev
);
2225 address_space_destroy(&s
->pci_io_as
);
2228 static void lsi_class_init(ObjectClass
*klass
, void *data
)
2230 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2231 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
2233 k
->realize
= lsi_scsi_realize
;
2234 k
->vendor_id
= PCI_VENDOR_ID_LSI_LOGIC
;
2235 k
->device_id
= PCI_DEVICE_ID_LSI_53C895A
;
2236 k
->class_id
= PCI_CLASS_STORAGE_SCSI
;
2237 k
->subsystem_id
= 0x1000;
2238 dc
->unrealize
= lsi_scsi_unrealize
;
2239 dc
->reset
= lsi_scsi_reset
;
2240 dc
->vmsd
= &vmstate_lsi_scsi
;
2241 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
2244 static const TypeInfo lsi_info
= {
2245 .name
= TYPE_LSI53C895A
,
2246 .parent
= TYPE_PCI_DEVICE
,
2247 .instance_size
= sizeof(LSIState
),
2248 .class_init
= lsi_class_init
,
2249 .interfaces
= (InterfaceInfo
[]) {
2250 { INTERFACE_CONVENTIONAL_PCI_DEVICE
},
2255 static void lsi53c810_class_init(ObjectClass
*klass
, void *data
)
2257 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
2259 k
->device_id
= PCI_DEVICE_ID_LSI_53C810
;
2262 static TypeInfo lsi53c810_info
= {
2263 .name
= TYPE_LSI53C810
,
2264 .parent
= TYPE_LSI53C895A
,
2265 .class_init
= lsi53c810_class_init
,
2268 static void lsi53c895a_register_types(void)
2270 type_register_static(&lsi_info
);
2271 type_register_static(&lsi53c810_info
);
2274 type_init(lsi53c895a_register_types
)
2276 void lsi53c895a_create(PCIBus
*bus
)
2278 LSIState
*s
= LSI53C895A(pci_create_simple(bus
, -1, "lsi53c895a"));
2280 scsi_bus_legacy_handle_cmdline(&s
->bus
);
2283 void lsi53c810_create(PCIBus
*bus
, int devfn
)
2285 LSIState
*s
= LSI53C895A(pci_create_simple(bus
, devfn
, "lsi53c810"));
2287 scsi_bus_legacy_handle_cmdline(&s
->bus
);