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 static const char *names
[] = {
26 "SCNTL0", "SCNTL1", "SCNTL2", "SCNTL3", "SCID", "SXFER", "SDID", "GPREG",
27 "SFBR", "SOCL", "SSID", "SBCL", "DSTAT", "SSTAT0", "SSTAT1", "SSTAT2",
28 "DSA0", "DSA1", "DSA2", "DSA3", "ISTAT", "0x15", "0x16", "0x17",
29 "CTEST0", "CTEST1", "CTEST2", "CTEST3", "TEMP0", "TEMP1", "TEMP2", "TEMP3",
30 "DFIFO", "CTEST4", "CTEST5", "CTEST6", "DBC0", "DBC1", "DBC2", "DCMD",
31 "DNAD0", "DNAD1", "DNAD2", "DNAD3", "DSP0", "DSP1", "DSP2", "DSP3",
32 "DSPS0", "DSPS1", "DSPS2", "DSPS3", "SCRATCHA0", "SCRATCHA1", "SCRATCHA2", "SCRATCHA3",
33 "DMODE", "DIEN", "SBR", "DCNTL", "ADDER0", "ADDER1", "ADDER2", "ADDER3",
34 "SIEN0", "SIEN1", "SIST0", "SIST1", "SLPAR", "0x45", "MACNTL", "GPCNTL",
35 "STIME0", "STIME1", "RESPID", "0x4b", "STEST0", "STEST1", "STEST2", "STEST3",
36 "SIDL", "0x51", "0x52", "0x53", "SODL", "0x55", "0x56", "0x57",
37 "SBDL", "0x59", "0x5a", "0x5b", "SCRATCHB0", "SCRATCHB1", "SCRATCHB2", "SCRATCHB3",
40 #define LSI_MAX_DEVS 7
42 #define LSI_SCNTL0_TRG 0x01
43 #define LSI_SCNTL0_AAP 0x02
44 #define LSI_SCNTL0_EPC 0x08
45 #define LSI_SCNTL0_WATN 0x10
46 #define LSI_SCNTL0_START 0x20
48 #define LSI_SCNTL1_SST 0x01
49 #define LSI_SCNTL1_IARB 0x02
50 #define LSI_SCNTL1_AESP 0x04
51 #define LSI_SCNTL1_RST 0x08
52 #define LSI_SCNTL1_CON 0x10
53 #define LSI_SCNTL1_DHP 0x20
54 #define LSI_SCNTL1_ADB 0x40
55 #define LSI_SCNTL1_EXC 0x80
57 #define LSI_SCNTL2_WSR 0x01
58 #define LSI_SCNTL2_VUE0 0x02
59 #define LSI_SCNTL2_VUE1 0x04
60 #define LSI_SCNTL2_WSS 0x08
61 #define LSI_SCNTL2_SLPHBEN 0x10
62 #define LSI_SCNTL2_SLPMD 0x20
63 #define LSI_SCNTL2_CHM 0x40
64 #define LSI_SCNTL2_SDU 0x80
66 #define LSI_ISTAT0_DIP 0x01
67 #define LSI_ISTAT0_SIP 0x02
68 #define LSI_ISTAT0_INTF 0x04
69 #define LSI_ISTAT0_CON 0x08
70 #define LSI_ISTAT0_SEM 0x10
71 #define LSI_ISTAT0_SIGP 0x20
72 #define LSI_ISTAT0_SRST 0x40
73 #define LSI_ISTAT0_ABRT 0x80
75 #define LSI_ISTAT1_SI 0x01
76 #define LSI_ISTAT1_SRUN 0x02
77 #define LSI_ISTAT1_FLSH 0x04
79 #define LSI_SSTAT0_SDP0 0x01
80 #define LSI_SSTAT0_RST 0x02
81 #define LSI_SSTAT0_WOA 0x04
82 #define LSI_SSTAT0_LOA 0x08
83 #define LSI_SSTAT0_AIP 0x10
84 #define LSI_SSTAT0_OLF 0x20
85 #define LSI_SSTAT0_ORF 0x40
86 #define LSI_SSTAT0_ILF 0x80
88 #define LSI_SIST0_PAR 0x01
89 #define LSI_SIST0_RST 0x02
90 #define LSI_SIST0_UDC 0x04
91 #define LSI_SIST0_SGE 0x08
92 #define LSI_SIST0_RSL 0x10
93 #define LSI_SIST0_SEL 0x20
94 #define LSI_SIST0_CMP 0x40
95 #define LSI_SIST0_MA 0x80
97 #define LSI_SIST1_HTH 0x01
98 #define LSI_SIST1_GEN 0x02
99 #define LSI_SIST1_STO 0x04
100 #define LSI_SIST1_SBMC 0x10
102 #define LSI_SOCL_IO 0x01
103 #define LSI_SOCL_CD 0x02
104 #define LSI_SOCL_MSG 0x04
105 #define LSI_SOCL_ATN 0x08
106 #define LSI_SOCL_SEL 0x10
107 #define LSI_SOCL_BSY 0x20
108 #define LSI_SOCL_ACK 0x40
109 #define LSI_SOCL_REQ 0x80
111 #define LSI_DSTAT_IID 0x01
112 #define LSI_DSTAT_SIR 0x04
113 #define LSI_DSTAT_SSI 0x08
114 #define LSI_DSTAT_ABRT 0x10
115 #define LSI_DSTAT_BF 0x20
116 #define LSI_DSTAT_MDPE 0x40
117 #define LSI_DSTAT_DFE 0x80
119 #define LSI_DCNTL_COM 0x01
120 #define LSI_DCNTL_IRQD 0x02
121 #define LSI_DCNTL_STD 0x04
122 #define LSI_DCNTL_IRQM 0x08
123 #define LSI_DCNTL_SSM 0x10
124 #define LSI_DCNTL_PFEN 0x20
125 #define LSI_DCNTL_PFF 0x40
126 #define LSI_DCNTL_CLSE 0x80
128 #define LSI_DMODE_MAN 0x01
129 #define LSI_DMODE_BOF 0x02
130 #define LSI_DMODE_ERMP 0x04
131 #define LSI_DMODE_ERL 0x08
132 #define LSI_DMODE_DIOM 0x10
133 #define LSI_DMODE_SIOM 0x20
135 #define LSI_CTEST2_DACK 0x01
136 #define LSI_CTEST2_DREQ 0x02
137 #define LSI_CTEST2_TEOP 0x04
138 #define LSI_CTEST2_PCICIE 0x08
139 #define LSI_CTEST2_CM 0x10
140 #define LSI_CTEST2_CIO 0x20
141 #define LSI_CTEST2_SIGP 0x40
142 #define LSI_CTEST2_DDIR 0x80
144 #define LSI_CTEST5_BL2 0x04
145 #define LSI_CTEST5_DDIR 0x08
146 #define LSI_CTEST5_MASR 0x10
147 #define LSI_CTEST5_DFSN 0x20
148 #define LSI_CTEST5_BBCK 0x40
149 #define LSI_CTEST5_ADCK 0x80
151 #define LSI_CCNTL0_DILS 0x01
152 #define LSI_CCNTL0_DISFC 0x10
153 #define LSI_CCNTL0_ENNDJ 0x20
154 #define LSI_CCNTL0_PMJCTL 0x40
155 #define LSI_CCNTL0_ENPMJ 0x80
157 #define LSI_CCNTL1_EN64DBMV 0x01
158 #define LSI_CCNTL1_EN64TIBMV 0x02
159 #define LSI_CCNTL1_64TIMOD 0x04
160 #define LSI_CCNTL1_DDAC 0x08
161 #define LSI_CCNTL1_ZMOD 0x80
163 /* Enable Response to Reselection */
164 #define LSI_SCID_RRE 0x60
166 #define LSI_CCNTL1_40BIT (LSI_CCNTL1_EN64TIBMV|LSI_CCNTL1_64TIMOD)
176 /* Maximum length of MSG IN data. */
177 #define LSI_MAX_MSGIN_LEN 8
179 /* Flag set if this is a tagged command. */
180 #define LSI_TAG_VALID (1 << 16)
182 typedef struct lsi_request
{
189 QTAILQ_ENTRY(lsi_request
) next
;
194 PCIDevice parent_obj
;
198 MemoryRegion mmio_io
;
201 AddressSpace pci_io_as
;
203 int carry
; /* ??? Should this be an a visible register somewhere? */
205 /* Action to take at the end of a MSG IN phase.
206 0 = COMMAND, 1 = disconnect, 2 = DATA OUT, 3 = DATA IN. */
209 uint8_t msg
[LSI_MAX_MSGIN_LEN
];
210 /* 0 if SCRIPTS are running or stopped.
211 * 1 if a Wait Reselect instruction has been issued.
212 * 2 if processing DMA from lsi_execute_script.
213 * 3 if a DMA operation is in progress. */
217 /* The tag is a combination of the device ID and the SCSI tag. */
219 int command_complete
;
220 QTAILQ_HEAD(, lsi_request
) queue
;
221 lsi_request
*current
;
282 uint32_t scratch
[18]; /* SCRATCHA-SCRATCHR */
286 /* Script ram is stored as 32-bit words in host byteorder. */
287 uint32_t script_ram
[2048];
290 #define TYPE_LSI53C810 "lsi53c810"
291 #define TYPE_LSI53C895A "lsi53c895a"
293 #define LSI53C895A(obj) \
294 OBJECT_CHECK(LSIState, (obj), TYPE_LSI53C895A)
296 static inline int lsi_irq_on_rsl(LSIState
*s
)
298 return (s
->sien0
& LSI_SIST0_RSL
) && (s
->scid
& LSI_SCID_RRE
);
301 static void lsi_soft_reset(LSIState
*s
)
313 memset(s
->scratch
, 0, sizeof(s
->scratch
));
326 s
->ctest2
= LSI_CTEST2_DACK
;
369 assert(QTAILQ_EMPTY(&s
->queue
));
373 static int lsi_dma_40bit(LSIState
*s
)
375 if ((s
->ccntl1
& LSI_CCNTL1_40BIT
) == LSI_CCNTL1_40BIT
)
380 static int lsi_dma_ti64bit(LSIState
*s
)
382 if ((s
->ccntl1
& LSI_CCNTL1_EN64TIBMV
) == LSI_CCNTL1_EN64TIBMV
)
387 static int lsi_dma_64bit(LSIState
*s
)
389 if ((s
->ccntl1
& LSI_CCNTL1_EN64DBMV
) == LSI_CCNTL1_EN64DBMV
)
394 static uint8_t lsi_reg_readb(LSIState
*s
, int offset
);
395 static void lsi_reg_writeb(LSIState
*s
, int offset
, uint8_t val
);
396 static void lsi_execute_script(LSIState
*s
);
397 static void lsi_reselect(LSIState
*s
, lsi_request
*p
);
399 static inline void lsi_mem_read(LSIState
*s
, dma_addr_t addr
,
400 void *buf
, dma_addr_t len
)
402 if (s
->dmode
& LSI_DMODE_SIOM
) {
403 address_space_read(&s
->pci_io_as
, addr
, MEMTXATTRS_UNSPECIFIED
,
406 pci_dma_read(PCI_DEVICE(s
), addr
, buf
, len
);
410 static inline void lsi_mem_write(LSIState
*s
, dma_addr_t addr
,
411 const void *buf
, dma_addr_t len
)
413 if (s
->dmode
& LSI_DMODE_DIOM
) {
414 address_space_write(&s
->pci_io_as
, addr
, MEMTXATTRS_UNSPECIFIED
,
417 pci_dma_write(PCI_DEVICE(s
), addr
, buf
, len
);
421 static inline uint32_t read_dword(LSIState
*s
, uint32_t addr
)
425 pci_dma_read(PCI_DEVICE(s
), addr
, &buf
, 4);
426 return cpu_to_le32(buf
);
429 static void lsi_stop_script(LSIState
*s
)
431 s
->istat1
&= ~LSI_ISTAT1_SRUN
;
434 static void lsi_set_irq(LSIState
*s
, int level
)
436 PCIDevice
*d
= PCI_DEVICE(s
);
439 qemu_set_irq(s
->ext_irq
, level
);
441 pci_set_irq(d
, level
);
445 static void lsi_update_irq(LSIState
*s
)
448 static int last_level
;
451 /* It's unclear whether the DIP/SIP bits should be cleared when the
452 Interrupt Status Registers are cleared or when istat0 is read.
453 We currently do the formwer, which seems to work. */
456 if (s
->dstat
& s
->dien
)
458 s
->istat0
|= LSI_ISTAT0_DIP
;
460 s
->istat0
&= ~LSI_ISTAT0_DIP
;
463 if (s
->sist0
|| s
->sist1
) {
464 if ((s
->sist0
& s
->sien0
) || (s
->sist1
& s
->sien1
))
466 s
->istat0
|= LSI_ISTAT0_SIP
;
468 s
->istat0
&= ~LSI_ISTAT0_SIP
;
470 if (s
->istat0
& LSI_ISTAT0_INTF
)
473 if (level
!= last_level
) {
474 trace_lsi_update_irq(level
, s
->dstat
, s
->sist1
, s
->sist0
);
477 lsi_set_irq(s
, level
);
479 if (!level
&& lsi_irq_on_rsl(s
) && !(s
->scntl1
& LSI_SCNTL1_CON
)) {
480 trace_lsi_update_irq_disconnected();
481 QTAILQ_FOREACH(p
, &s
->queue
, next
) {
490 /* Stop SCRIPTS execution and raise a SCSI interrupt. */
491 static void lsi_script_scsi_interrupt(LSIState
*s
, int stat0
, int stat1
)
496 trace_lsi_script_scsi_interrupt(stat1
, stat0
, s
->sist1
, s
->sist0
);
499 /* Stop processor on fatal or unmasked interrupt. As a special hack
500 we don't stop processing when raising STO. Instead continue
501 execution and stop at the next insn that accesses the SCSI bus. */
502 mask0
= s
->sien0
| ~(LSI_SIST0_CMP
| LSI_SIST0_SEL
| LSI_SIST0_RSL
);
503 mask1
= s
->sien1
| ~(LSI_SIST1_GEN
| LSI_SIST1_HTH
);
504 mask1
&= ~LSI_SIST1_STO
;
505 if (s
->sist0
& mask0
|| s
->sist1
& mask1
) {
511 /* Stop SCRIPTS execution and raise a DMA interrupt. */
512 static void lsi_script_dma_interrupt(LSIState
*s
, int stat
)
514 trace_lsi_script_dma_interrupt(stat
, s
->dstat
);
520 static inline void lsi_set_phase(LSIState
*s
, int phase
)
522 s
->sstat1
= (s
->sstat1
& ~PHASE_MASK
) | phase
;
525 static void lsi_bad_phase(LSIState
*s
, int out
, int new_phase
)
527 /* Trigger a phase mismatch. */
528 if (s
->ccntl0
& LSI_CCNTL0_ENPMJ
) {
529 if ((s
->ccntl0
& LSI_CCNTL0_PMJCTL
)) {
530 s
->dsp
= out
? s
->pmjad1
: s
->pmjad2
;
532 s
->dsp
= (s
->scntl2
& LSI_SCNTL2_WSR
? s
->pmjad2
: s
->pmjad1
);
534 trace_lsi_bad_phase_jump(s
->dsp
);
536 trace_lsi_bad_phase_interrupt();
537 lsi_script_scsi_interrupt(s
, LSI_SIST0_MA
, 0);
540 lsi_set_phase(s
, new_phase
);
544 /* Resume SCRIPTS execution after a DMA operation. */
545 static void lsi_resume_script(LSIState
*s
)
547 if (s
->waiting
!= 2) {
549 lsi_execute_script(s
);
555 static void lsi_disconnect(LSIState
*s
)
557 s
->scntl1
&= ~LSI_SCNTL1_CON
;
558 s
->sstat1
&= ~PHASE_MASK
;
561 static void lsi_bad_selection(LSIState
*s
, uint32_t id
)
563 trace_lsi_bad_selection(id
);
564 lsi_script_scsi_interrupt(s
, 0, LSI_SIST1_STO
);
568 /* Initiate a SCSI layer data transfer. */
569 static void lsi_do_dma(LSIState
*s
, int out
)
576 if (!s
->current
->dma_len
) {
577 /* Wait until data is available. */
578 trace_lsi_do_dma_unavailable();
582 dev
= s
->current
->req
->dev
;
586 if (count
> s
->current
->dma_len
)
587 count
= s
->current
->dma_len
;
590 /* both 40 and Table Indirect 64-bit DMAs store upper bits in dnad64 */
591 if (lsi_dma_40bit(s
) || lsi_dma_ti64bit(s
))
592 addr
|= ((uint64_t)s
->dnad64
<< 32);
594 addr
|= ((uint64_t)s
->dbms
<< 32);
596 addr
|= ((uint64_t)s
->sbms
<< 32);
598 trace_lsi_do_dma(addr
, count
);
602 if (s
->current
->dma_buf
== NULL
) {
603 s
->current
->dma_buf
= scsi_req_get_buf(s
->current
->req
);
605 /* ??? Set SFBR to first data byte. */
607 lsi_mem_read(s
, addr
, s
->current
->dma_buf
, count
);
609 lsi_mem_write(s
, addr
, s
->current
->dma_buf
, count
);
611 s
->current
->dma_len
-= count
;
612 if (s
->current
->dma_len
== 0) {
613 s
->current
->dma_buf
= NULL
;
614 scsi_req_continue(s
->current
->req
);
616 s
->current
->dma_buf
+= count
;
617 lsi_resume_script(s
);
622 /* Add a command to the queue. */
623 static void lsi_queue_command(LSIState
*s
)
625 lsi_request
*p
= s
->current
;
627 trace_lsi_queue_command(p
->tag
);
628 assert(s
->current
!= NULL
);
629 assert(s
->current
->dma_len
== 0);
630 QTAILQ_INSERT_TAIL(&s
->queue
, s
->current
, next
);
634 p
->out
= (s
->sstat1
& PHASE_MASK
) == PHASE_DO
;
637 /* Queue a byte for a MSG IN phase. */
638 static void lsi_add_msg_byte(LSIState
*s
, uint8_t data
)
640 if (s
->msg_len
>= LSI_MAX_MSGIN_LEN
) {
641 trace_lsi_add_msg_byte_error();
643 trace_lsi_add_msg_byte(data
);
644 s
->msg
[s
->msg_len
++] = data
;
648 /* Perform reselection to continue a command. */
649 static void lsi_reselect(LSIState
*s
, lsi_request
*p
)
653 assert(s
->current
== NULL
);
654 QTAILQ_REMOVE(&s
->queue
, p
, next
);
657 id
= (p
->tag
>> 8) & 0xf;
659 /* LSI53C700 Family Compatibility, see LSI53C895A 4-73 */
660 if (!(s
->dcntl
& LSI_DCNTL_COM
)) {
661 s
->sfbr
= 1 << (id
& 0x7);
663 trace_lsi_reselect(id
);
664 s
->scntl1
|= LSI_SCNTL1_CON
;
665 lsi_set_phase(s
, PHASE_MI
);
666 s
->msg_action
= p
->out
? 2 : 3;
667 s
->current
->dma_len
= p
->pending
;
668 lsi_add_msg_byte(s
, 0x80);
669 if (s
->current
->tag
& LSI_TAG_VALID
) {
670 lsi_add_msg_byte(s
, 0x20);
671 lsi_add_msg_byte(s
, p
->tag
& 0xff);
674 if (lsi_irq_on_rsl(s
)) {
675 lsi_script_scsi_interrupt(s
, LSI_SIST0_RSL
, 0);
679 static lsi_request
*lsi_find_by_tag(LSIState
*s
, uint32_t tag
)
683 QTAILQ_FOREACH(p
, &s
->queue
, next
) {
692 static void lsi_request_free(LSIState
*s
, lsi_request
*p
)
694 if (p
== s
->current
) {
697 QTAILQ_REMOVE(&s
->queue
, p
, next
);
702 static void lsi_request_cancelled(SCSIRequest
*req
)
704 LSIState
*s
= LSI53C895A(req
->bus
->qbus
.parent
);
705 lsi_request
*p
= req
->hba_private
;
707 req
->hba_private
= NULL
;
708 lsi_request_free(s
, p
);
712 /* Record that data is available for a queued command. Returns zero if
713 the device was reselected, nonzero if the IO is deferred. */
714 static int lsi_queue_req(LSIState
*s
, SCSIRequest
*req
, uint32_t len
)
716 lsi_request
*p
= req
->hba_private
;
719 trace_lsi_queue_req_error(p
);
722 /* Reselect if waiting for it, or if reselection triggers an IRQ
724 Since no interrupt stacking is implemented in the emulation, it
725 is also required that there are no pending interrupts waiting
726 for service from the device driver. */
727 if (s
->waiting
== 1 ||
728 (lsi_irq_on_rsl(s
) && !(s
->scntl1
& LSI_SCNTL1_CON
) &&
729 !(s
->istat0
& (LSI_ISTAT0_SIP
| LSI_ISTAT0_DIP
)))) {
730 /* Reselect device. */
734 trace_lsi_queue_req(p
->tag
);
740 /* Callback to indicate that the SCSI layer has completed a command. */
741 static void lsi_command_complete(SCSIRequest
*req
, uint32_t status
, size_t resid
)
743 LSIState
*s
= LSI53C895A(req
->bus
->qbus
.parent
);
746 out
= (s
->sstat1
& PHASE_MASK
) == PHASE_DO
;
747 trace_lsi_command_complete(status
);
749 s
->command_complete
= 2;
750 if (s
->waiting
&& s
->dbc
!= 0) {
751 /* Raise phase mismatch for short transfers. */
752 lsi_bad_phase(s
, out
, PHASE_ST
);
754 lsi_set_phase(s
, PHASE_ST
);
757 if (req
->hba_private
== s
->current
) {
758 req
->hba_private
= NULL
;
759 lsi_request_free(s
, s
->current
);
762 lsi_resume_script(s
);
765 /* Callback to indicate that the SCSI layer has completed a transfer. */
766 static void lsi_transfer_data(SCSIRequest
*req
, uint32_t len
)
768 LSIState
*s
= LSI53C895A(req
->bus
->qbus
.parent
);
771 assert(req
->hba_private
);
772 if (s
->waiting
== 1 || req
->hba_private
!= s
->current
||
773 (lsi_irq_on_rsl(s
) && !(s
->scntl1
& LSI_SCNTL1_CON
))) {
774 if (lsi_queue_req(s
, req
, len
)) {
779 out
= (s
->sstat1
& PHASE_MASK
) == PHASE_DO
;
781 /* host adapter (re)connected */
782 trace_lsi_transfer_data(req
->tag
, len
);
783 s
->current
->dma_len
= len
;
784 s
->command_complete
= 1;
786 if (s
->waiting
== 1 || s
->dbc
== 0) {
787 lsi_resume_script(s
);
794 static void lsi_do_command(LSIState
*s
)
801 trace_lsi_do_command(s
->dbc
);
804 pci_dma_read(PCI_DEVICE(s
), s
->dnad
, buf
, s
->dbc
);
806 s
->command_complete
= 0;
808 id
= (s
->select_tag
>> 8) & 0xf;
809 dev
= scsi_device_find(&s
->bus
, 0, id
, s
->current_lun
);
811 lsi_bad_selection(s
, id
);
815 assert(s
->current
== NULL
);
816 s
->current
= g_new0(lsi_request
, 1);
817 s
->current
->tag
= s
->select_tag
;
818 s
->current
->req
= scsi_req_new(dev
, s
->current
->tag
, s
->current_lun
, buf
,
821 n
= scsi_req_enqueue(s
->current
->req
);
824 lsi_set_phase(s
, PHASE_DI
);
826 lsi_set_phase(s
, PHASE_DO
);
828 scsi_req_continue(s
->current
->req
);
830 if (!s
->command_complete
) {
832 /* Command did not complete immediately so disconnect. */
833 lsi_add_msg_byte(s
, 2); /* SAVE DATA POINTER */
834 lsi_add_msg_byte(s
, 4); /* DISCONNECT */
836 lsi_set_phase(s
, PHASE_MI
);
838 lsi_queue_command(s
);
840 /* wait command complete */
841 lsi_set_phase(s
, PHASE_DI
);
846 static void lsi_do_status(LSIState
*s
)
849 trace_lsi_do_status(s
->dbc
, s
->status
);
851 trace_lsi_do_status_error();
856 pci_dma_write(PCI_DEVICE(s
), s
->dnad
, &status
, 1);
857 lsi_set_phase(s
, PHASE_MI
);
859 lsi_add_msg_byte(s
, 0); /* COMMAND COMPLETE */
862 static void lsi_do_msgin(LSIState
*s
)
865 trace_lsi_do_msgin(s
->dbc
, s
->msg_len
);
870 pci_dma_write(PCI_DEVICE(s
), s
->dnad
, s
->msg
, len
);
871 /* Linux drivers rely on the last byte being in the SIDL. */
872 s
->sidl
= s
->msg
[len
- 1];
875 memmove(s
->msg
, s
->msg
+ len
, s
->msg_len
);
877 /* ??? Check if ATN (not yet implemented) is asserted and maybe
878 switch to PHASE_MO. */
879 switch (s
->msg_action
) {
881 lsi_set_phase(s
, PHASE_CMD
);
887 lsi_set_phase(s
, PHASE_DO
);
890 lsi_set_phase(s
, PHASE_DI
);
898 /* Read the next byte during a MSGOUT phase. */
899 static uint8_t lsi_get_msgbyte(LSIState
*s
)
902 pci_dma_read(PCI_DEVICE(s
), s
->dnad
, &data
, 1);
908 /* Skip the next n bytes during a MSGOUT phase. */
909 static void lsi_skip_msgbytes(LSIState
*s
, unsigned int n
)
915 static void lsi_do_msgout(LSIState
*s
)
919 uint32_t current_tag
;
920 lsi_request
*current_req
, *p
, *p_next
;
923 current_tag
= s
->current
->tag
;
924 current_req
= s
->current
;
926 current_tag
= s
->select_tag
;
927 current_req
= lsi_find_by_tag(s
, current_tag
);
930 trace_lsi_do_msgout(s
->dbc
);
932 msg
= lsi_get_msgbyte(s
);
937 trace_lsi_do_msgout_disconnect();
941 trace_lsi_do_msgout_noop();
942 lsi_set_phase(s
, PHASE_CMD
);
945 len
= lsi_get_msgbyte(s
);
946 msg
= lsi_get_msgbyte(s
);
947 (void)len
; /* avoid a warning about unused variable*/
948 trace_lsi_do_msgout_extended(msg
, len
);
951 trace_lsi_do_msgout_ignored("SDTR");
952 lsi_skip_msgbytes(s
, 2);
955 trace_lsi_do_msgout_ignored("WDTR");
956 lsi_skip_msgbytes(s
, 1);
959 trace_lsi_do_msgout_ignored("PPR");
960 lsi_skip_msgbytes(s
, 5);
966 case 0x20: /* SIMPLE queue */
967 s
->select_tag
|= lsi_get_msgbyte(s
) | LSI_TAG_VALID
;
968 trace_lsi_do_msgout_simplequeue(s
->select_tag
& 0xff);
970 case 0x21: /* HEAD of queue */
971 qemu_log_mask(LOG_UNIMP
, "lsi_scsi: HEAD queue not implemented\n");
972 s
->select_tag
|= lsi_get_msgbyte(s
) | LSI_TAG_VALID
;
974 case 0x22: /* ORDERED queue */
975 qemu_log_mask(LOG_UNIMP
,
976 "lsi_scsi: ORDERED queue not implemented\n");
977 s
->select_tag
|= lsi_get_msgbyte(s
) | LSI_TAG_VALID
;
980 /* The ABORT TAG message clears the current I/O process only. */
981 trace_lsi_do_msgout_abort(current_tag
);
983 scsi_req_cancel(current_req
->req
);
990 /* The ABORT message clears all I/O processes for the selecting
991 initiator on the specified logical unit of the target. */
993 trace_lsi_do_msgout_abort(current_tag
);
995 /* The CLEAR QUEUE message clears all I/O processes for all
996 initiators on the specified logical unit of the target. */
998 trace_lsi_do_msgout_clearqueue(current_tag
);
1000 /* The BUS DEVICE RESET message clears all I/O processes for all
1001 initiators on all logical units of the target. */
1003 trace_lsi_do_msgout_busdevicereset(current_tag
);
1006 /* clear the current I/O process */
1008 scsi_req_cancel(s
->current
->req
);
1011 /* As the current implemented devices scsi_disk and scsi_generic
1012 only support one LUN, we don't need to keep track of LUNs.
1013 Clearing I/O processes for other initiators could be possible
1014 for scsi_generic by sending a SG_SCSI_RESET to the /dev/sgX
1015 device, but this is currently not implemented (and seems not
1016 to be really necessary). So let's simply clear all queued
1017 commands for the current device: */
1018 QTAILQ_FOREACH_SAFE(p
, &s
->queue
, next
, p_next
) {
1019 if ((p
->tag
& 0x0000ff00) == (current_tag
& 0x0000ff00)) {
1020 scsi_req_cancel(p
->req
);
1027 if ((msg
& 0x80) == 0) {
1030 s
->current_lun
= msg
& 7;
1031 trace_lsi_do_msgout_select(s
->current_lun
);
1032 lsi_set_phase(s
, PHASE_CMD
);
1038 qemu_log_mask(LOG_UNIMP
, "Unimplemented message 0x%02x\n", msg
);
1039 lsi_set_phase(s
, PHASE_MI
);
1040 lsi_add_msg_byte(s
, 7); /* MESSAGE REJECT */
1044 #define LSI_BUF_SIZE 4096
1045 static void lsi_memcpy(LSIState
*s
, uint32_t dest
, uint32_t src
, int count
)
1048 uint8_t buf
[LSI_BUF_SIZE
];
1050 trace_lsi_memcpy(dest
, src
, count
);
1052 n
= (count
> LSI_BUF_SIZE
) ? LSI_BUF_SIZE
: count
;
1053 lsi_mem_read(s
, src
, buf
, n
);
1054 lsi_mem_write(s
, dest
, buf
, n
);
1061 static void lsi_wait_reselect(LSIState
*s
)
1065 trace_lsi_wait_reselect();
1067 QTAILQ_FOREACH(p
, &s
->queue
, next
) {
1073 if (s
->current
== NULL
) {
1078 static void lsi_execute_script(LSIState
*s
)
1080 PCIDevice
*pci_dev
= PCI_DEVICE(s
);
1082 uint32_t addr
, addr_high
;
1084 int insn_processed
= 0;
1086 s
->istat1
|= LSI_ISTAT1_SRUN
;
1089 insn
= read_dword(s
, s
->dsp
);
1091 /* If we receive an empty opcode increment the DSP by 4 bytes
1092 instead of 8 and execute the next opcode at that location */
1096 addr
= read_dword(s
, s
->dsp
+ 4);
1098 trace_lsi_execute_script(s
->dsp
, insn
, addr
);
1100 s
->dcmd
= insn
>> 24;
1102 switch (insn
>> 30) {
1103 case 0: /* Block move. */
1104 if (s
->sist1
& LSI_SIST1_STO
) {
1105 trace_lsi_execute_script_blockmove_delayed();
1109 s
->dbc
= insn
& 0xffffff;
1113 if (insn
& (1 << 29)) {
1114 /* Indirect addressing. */
1115 addr
= read_dword(s
, addr
);
1116 } else if (insn
& (1 << 28)) {
1119 /* Table indirect addressing. */
1121 /* 32-bit Table indirect */
1122 offset
= sextract32(addr
, 0, 24);
1123 pci_dma_read(pci_dev
, s
->dsa
+ offset
, buf
, 8);
1124 /* byte count is stored in bits 0:23 only */
1125 s
->dbc
= cpu_to_le32(buf
[0]) & 0xffffff;
1127 addr
= cpu_to_le32(buf
[1]);
1129 /* 40-bit DMA, upper addr bits [39:32] stored in first DWORD of
1130 * table, bits [31:24] */
1131 if (lsi_dma_40bit(s
))
1132 addr_high
= cpu_to_le32(buf
[0]) >> 24;
1133 else if (lsi_dma_ti64bit(s
)) {
1134 int selector
= (cpu_to_le32(buf
[0]) >> 24) & 0x1f;
1137 /* offset index into scratch registers since
1138 * TI64 mode can use registers C to R */
1139 addr_high
= s
->scratch
[2 + selector
];
1142 addr_high
= s
->mmrs
;
1145 addr_high
= s
->mmws
;
1154 addr_high
= s
->sbms
;
1157 addr_high
= s
->dbms
;
1160 qemu_log_mask(LOG_GUEST_ERROR
,
1161 "lsi_scsi: Illegal selector specified (0x%x > 0x15) "
1162 "for 64-bit DMA block move", selector
);
1166 } else if (lsi_dma_64bit(s
)) {
1167 /* fetch a 3rd dword if 64-bit direct move is enabled and
1168 only if we're not doing table indirect or indirect addressing */
1169 s
->dbms
= read_dword(s
, s
->dsp
);
1171 s
->ia
= s
->dsp
- 12;
1173 if ((s
->sstat1
& PHASE_MASK
) != ((insn
>> 24) & 7)) {
1174 trace_lsi_execute_script_blockmove_badphase(s
->sstat1
& PHASE_MASK
,
1176 lsi_script_scsi_interrupt(s
, LSI_SIST0_MA
, 0);
1180 s
->dnad64
= addr_high
;
1181 switch (s
->sstat1
& 0x7) {
1207 qemu_log_mask(LOG_UNIMP
, "lsi_scsi: Unimplemented phase %d\n",
1208 s
->sstat1
& PHASE_MASK
);
1210 s
->dfifo
= s
->dbc
& 0xff;
1211 s
->ctest5
= (s
->ctest5
& 0xfc) | ((s
->dbc
>> 8) & 3);
1214 s
->ua
= addr
+ s
->dbc
;
1217 case 1: /* IO or Read/Write instruction. */
1218 opcode
= (insn
>> 27) & 7;
1222 if (insn
& (1 << 25)) {
1223 id
= read_dword(s
, s
->dsa
+ sextract32(insn
, 0, 24));
1227 id
= (id
>> 16) & 0xf;
1228 if (insn
& (1 << 26)) {
1229 addr
= s
->dsp
+ sextract32(addr
, 0, 24);
1233 case 0: /* Select */
1235 if (s
->scntl1
& LSI_SCNTL1_CON
) {
1236 trace_lsi_execute_script_io_alreadyreselected();
1240 s
->sstat0
|= LSI_SSTAT0_WOA
;
1241 s
->scntl1
&= ~LSI_SCNTL1_IARB
;
1242 if (!scsi_device_find(&s
->bus
, 0, id
, 0)) {
1243 lsi_bad_selection(s
, id
);
1246 trace_lsi_execute_script_io_selected(id
,
1247 insn
& (1 << 3) ? " ATN" : "");
1248 /* ??? Linux drivers compain when this is set. Maybe
1249 it only applies in low-level mode (unimplemented).
1250 lsi_script_scsi_interrupt(s, LSI_SIST0_CMP, 0); */
1251 s
->select_tag
= id
<< 8;
1252 s
->scntl1
|= LSI_SCNTL1_CON
;
1253 if (insn
& (1 << 3)) {
1254 s
->socl
|= LSI_SOCL_ATN
;
1256 lsi_set_phase(s
, PHASE_MO
);
1258 case 1: /* Disconnect */
1259 trace_lsi_execute_script_io_disconnect();
1260 s
->scntl1
&= ~LSI_SCNTL1_CON
;
1262 case 2: /* Wait Reselect */
1263 if (!lsi_irq_on_rsl(s
)) {
1264 lsi_wait_reselect(s
);
1268 trace_lsi_execute_script_io_set(
1269 insn
& (1 << 3) ? " ATN" : "",
1270 insn
& (1 << 6) ? " ACK" : "",
1271 insn
& (1 << 9) ? " TM" : "",
1272 insn
& (1 << 10) ? " CC" : "");
1273 if (insn
& (1 << 3)) {
1274 s
->socl
|= LSI_SOCL_ATN
;
1275 lsi_set_phase(s
, PHASE_MO
);
1277 if (insn
& (1 << 9)) {
1278 qemu_log_mask(LOG_UNIMP
,
1279 "lsi_scsi: Target mode not implemented\n");
1281 if (insn
& (1 << 10))
1285 trace_lsi_execute_script_io_clear(
1286 insn
& (1 << 3) ? " ATN" : "",
1287 insn
& (1 << 6) ? " ACK" : "",
1288 insn
& (1 << 9) ? " TM" : "",
1289 insn
& (1 << 10) ? " CC" : "");
1290 if (insn
& (1 << 3)) {
1291 s
->socl
&= ~LSI_SOCL_ATN
;
1293 if (insn
& (1 << 10))
1304 static const char *opcode_names
[3] =
1305 {"Write", "Read", "Read-Modify-Write"};
1306 static const char *operator_names
[8] =
1307 {"MOV", "SHL", "OR", "XOR", "AND", "SHR", "ADD", "ADC"};
1309 reg
= ((insn
>> 16) & 0x7f) | (insn
& 0x80);
1310 data8
= (insn
>> 8) & 0xff;
1311 opcode
= (insn
>> 27) & 7;
1312 operator = (insn
>> 24) & 7;
1313 trace_lsi_execute_script_io_opcode(
1314 opcode_names
[opcode
- 5], reg
,
1315 operator_names
[operator], data8
, s
->sfbr
,
1316 (insn
& (1 << 23)) ? " SFBR" : "");
1319 case 5: /* From SFBR */
1323 case 6: /* To SFBR */
1325 op0
= lsi_reg_readb(s
, reg
);
1328 case 7: /* Read-modify-write */
1330 op0
= lsi_reg_readb(s
, reg
);
1331 if (insn
& (1 << 23)) {
1343 case 1: /* Shift left */
1345 op0
= (op0
<< 1) | s
->carry
;
1359 op0
= (op0
>> 1) | (s
->carry
<< 7);
1364 s
->carry
= op0
< op1
;
1367 op0
+= op1
+ s
->carry
;
1369 s
->carry
= op0
<= op1
;
1371 s
->carry
= op0
< op1
;
1376 case 5: /* From SFBR */
1377 case 7: /* Read-modify-write */
1378 lsi_reg_writeb(s
, reg
, op0
);
1380 case 6: /* To SFBR */
1387 case 2: /* Transfer Control. */
1392 if ((insn
& 0x002e0000) == 0) {
1393 trace_lsi_execute_script_tc_nop();
1396 if (s
->sist1
& LSI_SIST1_STO
) {
1397 trace_lsi_execute_script_tc_delayedselect_timeout();
1401 cond
= jmp
= (insn
& (1 << 19)) != 0;
1402 if (cond
== jmp
&& (insn
& (1 << 21))) {
1403 trace_lsi_execute_script_tc_compc(s
->carry
== jmp
);
1404 cond
= s
->carry
!= 0;
1406 if (cond
== jmp
&& (insn
& (1 << 17))) {
1407 trace_lsi_execute_script_tc_compp(
1408 (s
->sstat1
& PHASE_MASK
),
1410 ((insn
>> 24) & 7));
1411 cond
= (s
->sstat1
& PHASE_MASK
) == ((insn
>> 24) & 7);
1413 if (cond
== jmp
&& (insn
& (1 << 18))) {
1416 mask
= (~insn
>> 8) & 0xff;
1417 trace_lsi_execute_script_tc_compd(
1418 s
->sfbr
, mask
, jmp
? '=' : '!', insn
& mask
);
1419 cond
= (s
->sfbr
& mask
) == (insn
& mask
);
1422 if (insn
& (1 << 23)) {
1423 /* Relative address. */
1424 addr
= s
->dsp
+ sextract32(addr
, 0, 24);
1426 switch ((insn
>> 27) & 7) {
1428 trace_lsi_execute_script_tc_jump(addr
);
1433 trace_lsi_execute_script_tc_call(addr
);
1437 case 2: /* Return */
1438 trace_lsi_execute_script_tc_return(s
->temp
);
1441 case 3: /* Interrupt */
1442 trace_lsi_execute_script_tc_interrupt(s
->dsps
);
1443 if ((insn
& (1 << 20)) != 0) {
1444 s
->istat0
|= LSI_ISTAT0_INTF
;
1447 lsi_script_dma_interrupt(s
, LSI_DSTAT_SIR
);
1451 trace_lsi_execute_script_tc_illegal();
1452 lsi_script_dma_interrupt(s
, LSI_DSTAT_IID
);
1456 trace_lsi_execute_script_tc_cc_failed();
1462 if ((insn
& (1 << 29)) == 0) {
1465 /* ??? The docs imply the destination address is loaded into
1466 the TEMP register. However the Linux drivers rely on
1467 the value being presrved. */
1468 dest
= read_dword(s
, s
->dsp
);
1470 lsi_memcpy(s
, dest
, addr
, insn
& 0xffffff);
1477 if (insn
& (1 << 28)) {
1478 addr
= s
->dsa
+ sextract32(addr
, 0, 24);
1481 reg
= (insn
>> 16) & 0xff;
1482 if (insn
& (1 << 24)) {
1483 pci_dma_read(pci_dev
, addr
, data
, n
);
1484 trace_lsi_execute_script_mm_load(reg
, n
, addr
, *(int *)data
);
1485 for (i
= 0; i
< n
; i
++) {
1486 lsi_reg_writeb(s
, reg
+ i
, data
[i
]);
1489 trace_lsi_execute_script_mm_store(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 qemu_log_mask(LOG_GUEST_ERROR
,
1505 "lsi_scsi: inf. loop with UDC masked");
1507 lsi_script_scsi_interrupt(s
, LSI_SIST0_UDC
, 0);
1509 } else if (s
->istat1
& LSI_ISTAT1_SRUN
&& !s
->waiting
) {
1510 if (s
->dcntl
& LSI_DCNTL_SSM
) {
1511 lsi_script_dma_interrupt(s
, LSI_DSTAT_SSI
);
1516 trace_lsi_execute_script_stop();
1519 static uint8_t lsi_reg_readb(LSIState
*s
, int offset
)
1523 #define CASE_GET_REG24(name, addr) \
1524 case addr: ret = s->name & 0xff; break; \
1525 case addr + 1: ret = (s->name >> 8) & 0xff; break; \
1526 case addr + 2: ret = (s->name >> 16) & 0xff; break;
1528 #define CASE_GET_REG32(name, addr) \
1529 case addr: ret = s->name & 0xff; break; \
1530 case addr + 1: ret = (s->name >> 8) & 0xff; break; \
1531 case addr + 2: ret = (s->name >> 16) & 0xff; break; \
1532 case addr + 3: ret = (s->name >> 24) & 0xff; break;
1535 case 0x00: /* SCNTL0 */
1538 case 0x01: /* SCNTL1 */
1541 case 0x02: /* SCNTL2 */
1544 case 0x03: /* SCNTL3 */
1547 case 0x04: /* SCID */
1550 case 0x05: /* SXFER */
1553 case 0x06: /* SDID */
1556 case 0x07: /* GPREG0 */
1559 case 0x08: /* Revision ID */
1562 case 0x09: /* SOCL */
1565 case 0xa: /* SSID */
1568 case 0xb: /* SBCL */
1569 /* ??? This is not correct. However it's (hopefully) only
1570 used for diagnostics, so should be ok. */
1573 case 0xc: /* DSTAT */
1574 ret
= s
->dstat
| LSI_DSTAT_DFE
;
1575 if ((s
->istat0
& LSI_ISTAT0_INTF
) == 0)
1579 case 0x0d: /* SSTAT0 */
1582 case 0x0e: /* SSTAT1 */
1585 case 0x0f: /* SSTAT2 */
1586 ret
= s
->scntl1
& LSI_SCNTL1_CON
? 0 : 2;
1588 CASE_GET_REG32(dsa
, 0x10)
1589 case 0x14: /* ISTAT0 */
1592 case 0x15: /* ISTAT1 */
1595 case 0x16: /* MBOX0 */
1598 case 0x17: /* MBOX1 */
1601 case 0x18: /* CTEST0 */
1604 case 0x19: /* CTEST1 */
1607 case 0x1a: /* CTEST2 */
1608 ret
= s
->ctest2
| LSI_CTEST2_DACK
| LSI_CTEST2_CM
;
1609 if (s
->istat0
& LSI_ISTAT0_SIGP
) {
1610 s
->istat0
&= ~LSI_ISTAT0_SIGP
;
1611 ret
|= LSI_CTEST2_SIGP
;
1614 case 0x1b: /* CTEST3 */
1617 CASE_GET_REG32(temp
, 0x1c)
1618 case 0x20: /* DFIFO */
1621 case 0x21: /* CTEST4 */
1624 case 0x22: /* CTEST5 */
1627 case 0x23: /* CTEST6 */
1630 CASE_GET_REG24(dbc
, 0x24)
1631 case 0x27: /* DCMD */
1634 CASE_GET_REG32(dnad
, 0x28)
1635 CASE_GET_REG32(dsp
, 0x2c)
1636 CASE_GET_REG32(dsps
, 0x30)
1637 CASE_GET_REG32(scratch
[0], 0x34)
1638 case 0x38: /* DMODE */
1641 case 0x39: /* DIEN */
1644 case 0x3a: /* SBR */
1647 case 0x3b: /* DCNTL */
1650 /* ADDER Output (Debug of relative jump address) */
1651 CASE_GET_REG32(adder
, 0x3c)
1652 case 0x40: /* SIEN0 */
1655 case 0x41: /* SIEN1 */
1658 case 0x42: /* SIST0 */
1663 case 0x43: /* SIST1 */
1668 case 0x46: /* MACNTL */
1671 case 0x47: /* GPCNTL0 */
1674 case 0x48: /* STIME0 */
1677 case 0x4a: /* RESPID0 */
1680 case 0x4b: /* RESPID1 */
1683 case 0x4d: /* STEST1 */
1686 case 0x4e: /* STEST2 */
1689 case 0x4f: /* STEST3 */
1692 case 0x50: /* SIDL */
1693 /* This is needed by the linux drivers. We currently only update it
1694 during the MSG IN phase. */
1697 case 0x52: /* STEST4 */
1700 case 0x56: /* CCNTL0 */
1703 case 0x57: /* CCNTL1 */
1706 case 0x58: /* SBDL */
1707 /* Some drivers peek at the data bus during the MSG IN phase. */
1708 if ((s
->sstat1
& PHASE_MASK
) == PHASE_MI
)
1712 case 0x59: /* SBDL high */
1715 CASE_GET_REG32(mmrs
, 0xa0)
1716 CASE_GET_REG32(mmws
, 0xa4)
1717 CASE_GET_REG32(sfs
, 0xa8)
1718 CASE_GET_REG32(drs
, 0xac)
1719 CASE_GET_REG32(sbms
, 0xb0)
1720 CASE_GET_REG32(dbms
, 0xb4)
1721 CASE_GET_REG32(dnad64
, 0xb8)
1722 CASE_GET_REG32(pmjad1
, 0xc0)
1723 CASE_GET_REG32(pmjad2
, 0xc4)
1724 CASE_GET_REG32(rbc
, 0xc8)
1725 CASE_GET_REG32(ua
, 0xcc)
1726 CASE_GET_REG32(ia
, 0xd4)
1727 CASE_GET_REG32(sbc
, 0xd8)
1728 CASE_GET_REG32(csbc
, 0xdc)
1733 n
= (offset
- 0x58) >> 2;
1734 shift
= (offset
& 3) * 8;
1735 ret
= (s
->scratch
[n
] >> shift
) & 0xff;
1740 qemu_log_mask(LOG_GUEST_ERROR
,
1741 "lsi_scsi: invalid read from reg %s %x\n",
1742 offset
< ARRAY_SIZE(names
) ? names
[offset
] : "???",
1748 #undef CASE_GET_REG24
1749 #undef CASE_GET_REG32
1751 trace_lsi_reg_read(offset
< ARRAY_SIZE(names
) ? names
[offset
] : "???",
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 trace_lsi_reg_write(offset
< ARRAY_SIZE(names
) ? names
[offset
] : "???",
1774 case 0x00: /* SCNTL0 */
1776 if (val
& LSI_SCNTL0_START
) {
1777 qemu_log_mask(LOG_UNIMP
,
1778 "lsi_scsi: Start sequence not implemented\n");
1781 case 0x01: /* SCNTL1 */
1782 s
->scntl1
= val
& ~LSI_SCNTL1_SST
;
1783 if (val
& LSI_SCNTL1_IARB
) {
1784 qemu_log_mask(LOG_UNIMP
,
1785 "lsi_scsi: Immediate Arbritration not implemented\n");
1787 if (val
& LSI_SCNTL1_RST
) {
1788 if (!(s
->sstat0
& LSI_SSTAT0_RST
)) {
1789 qbus_reset_all(&s
->bus
.qbus
);
1790 s
->sstat0
|= LSI_SSTAT0_RST
;
1791 lsi_script_scsi_interrupt(s
, LSI_SIST0_RST
, 0);
1794 s
->sstat0
&= ~LSI_SSTAT0_RST
;
1797 case 0x02: /* SCNTL2 */
1798 val
&= ~(LSI_SCNTL2_WSR
| LSI_SCNTL2_WSS
);
1801 case 0x03: /* SCNTL3 */
1804 case 0x04: /* SCID */
1807 case 0x05: /* SXFER */
1810 case 0x06: /* SDID */
1811 if ((s
->ssid
& 0x80) && (val
& 0xf) != (s
->ssid
& 0xf)) {
1812 qemu_log_mask(LOG_GUEST_ERROR
,
1813 "lsi_scsi: Destination ID does not match SSID\n");
1815 s
->sdid
= val
& 0xf;
1817 case 0x07: /* GPREG0 */
1819 case 0x08: /* SFBR */
1820 /* The CPU is not allowed to write to this register. However the
1821 SCRIPTS register move instructions are. */
1824 case 0x0a: case 0x0b:
1825 /* Openserver writes to these readonly registers on startup */
1827 case 0x0c: case 0x0d: case 0x0e: case 0x0f:
1828 /* Linux writes to these readonly registers on startup. */
1830 CASE_SET_REG32(dsa
, 0x10)
1831 case 0x14: /* ISTAT0 */
1832 s
->istat0
= (s
->istat0
& 0x0f) | (val
& 0xf0);
1833 if (val
& LSI_ISTAT0_ABRT
) {
1834 lsi_script_dma_interrupt(s
, LSI_DSTAT_ABRT
);
1836 if (val
& LSI_ISTAT0_INTF
) {
1837 s
->istat0
&= ~LSI_ISTAT0_INTF
;
1840 if (s
->waiting
== 1 && val
& LSI_ISTAT0_SIGP
) {
1844 lsi_execute_script(s
);
1846 if (val
& LSI_ISTAT0_SRST
) {
1847 qdev_reset_all(DEVICE(s
));
1850 case 0x16: /* MBOX0 */
1853 case 0x17: /* MBOX1 */
1856 case 0x18: /* CTEST0 */
1859 case 0x1a: /* CTEST2 */
1860 s
->ctest2
= val
& LSI_CTEST2_PCICIE
;
1862 case 0x1b: /* CTEST3 */
1863 s
->ctest3
= val
& 0x0f;
1865 CASE_SET_REG32(temp
, 0x1c)
1866 case 0x21: /* CTEST4 */
1868 qemu_log_mask(LOG_UNIMP
,
1869 "lsi_scsi: Unimplemented CTEST4-FBL 0x%x\n", val
);
1873 case 0x22: /* CTEST5 */
1874 if (val
& (LSI_CTEST5_ADCK
| LSI_CTEST5_BBCK
)) {
1875 qemu_log_mask(LOG_UNIMP
,
1876 "lsi_scsi: CTEST5 DMA increment not implemented\n");
1880 CASE_SET_REG24(dbc
, 0x24)
1881 CASE_SET_REG32(dnad
, 0x28)
1882 case 0x2c: /* DSP[0:7] */
1883 s
->dsp
&= 0xffffff00;
1886 case 0x2d: /* DSP[8:15] */
1887 s
->dsp
&= 0xffff00ff;
1890 case 0x2e: /* DSP[16:23] */
1891 s
->dsp
&= 0xff00ffff;
1892 s
->dsp
|= val
<< 16;
1894 case 0x2f: /* DSP[24:31] */
1895 s
->dsp
&= 0x00ffffff;
1896 s
->dsp
|= val
<< 24;
1897 if ((s
->dmode
& LSI_DMODE_MAN
) == 0
1898 && (s
->istat1
& LSI_ISTAT1_SRUN
) == 0)
1899 lsi_execute_script(s
);
1901 CASE_SET_REG32(dsps
, 0x30)
1902 CASE_SET_REG32(scratch
[0], 0x34)
1903 case 0x38: /* DMODE */
1906 case 0x39: /* DIEN */
1910 case 0x3a: /* SBR */
1913 case 0x3b: /* DCNTL */
1914 s
->dcntl
= val
& ~(LSI_DCNTL_PFF
| LSI_DCNTL_STD
);
1915 if ((val
& LSI_DCNTL_STD
) && (s
->istat1
& LSI_ISTAT1_SRUN
) == 0)
1916 lsi_execute_script(s
);
1918 case 0x40: /* SIEN0 */
1922 case 0x41: /* SIEN1 */
1926 case 0x47: /* GPCNTL0 */
1928 case 0x48: /* STIME0 */
1931 case 0x49: /* STIME1 */
1933 qemu_log_mask(LOG_UNIMP
,
1934 "lsi_scsi: General purpose timer not implemented\n");
1935 /* ??? Raising the interrupt immediately seems to be sufficient
1936 to keep the FreeBSD driver happy. */
1937 lsi_script_scsi_interrupt(s
, 0, LSI_SIST1_GEN
);
1940 case 0x4a: /* RESPID0 */
1943 case 0x4b: /* RESPID1 */
1946 case 0x4d: /* STEST1 */
1949 case 0x4e: /* STEST2 */
1951 qemu_log_mask(LOG_UNIMP
,
1952 "lsi_scsi: Low level mode not implemented\n");
1956 case 0x4f: /* STEST3 */
1958 qemu_log_mask(LOG_UNIMP
,
1959 "lsi_scsi: SCSI FIFO test mode not implemented\n");
1963 case 0x56: /* CCNTL0 */
1966 case 0x57: /* CCNTL1 */
1969 CASE_SET_REG32(mmrs
, 0xa0)
1970 CASE_SET_REG32(mmws
, 0xa4)
1971 CASE_SET_REG32(sfs
, 0xa8)
1972 CASE_SET_REG32(drs
, 0xac)
1973 CASE_SET_REG32(sbms
, 0xb0)
1974 CASE_SET_REG32(dbms
, 0xb4)
1975 CASE_SET_REG32(dnad64
, 0xb8)
1976 CASE_SET_REG32(pmjad1
, 0xc0)
1977 CASE_SET_REG32(pmjad2
, 0xc4)
1978 CASE_SET_REG32(rbc
, 0xc8)
1979 CASE_SET_REG32(ua
, 0xcc)
1980 CASE_SET_REG32(ia
, 0xd4)
1981 CASE_SET_REG32(sbc
, 0xd8)
1982 CASE_SET_REG32(csbc
, 0xdc)
1984 if (offset
>= 0x5c && offset
< 0xa0) {
1987 n
= (offset
- 0x58) >> 2;
1988 shift
= (offset
& 3) * 8;
1989 s
->scratch
[n
] = deposit32(s
->scratch
[n
], shift
, 8, val
);
1991 qemu_log_mask(LOG_GUEST_ERROR
,
1992 "lsi_scsi: invalid write to reg %s %x (0x%02x)\n",
1993 offset
< ARRAY_SIZE(names
) ? names
[offset
] : "???",
1997 #undef CASE_SET_REG24
1998 #undef CASE_SET_REG32
2001 static void lsi_mmio_write(void *opaque
, hwaddr addr
,
2002 uint64_t val
, unsigned size
)
2004 LSIState
*s
= opaque
;
2006 lsi_reg_writeb(s
, addr
& 0xff, val
);
2009 static uint64_t lsi_mmio_read(void *opaque
, hwaddr addr
,
2012 LSIState
*s
= opaque
;
2014 return lsi_reg_readb(s
, addr
& 0xff);
2017 static const MemoryRegionOps lsi_mmio_ops
= {
2018 .read
= lsi_mmio_read
,
2019 .write
= lsi_mmio_write
,
2020 .endianness
= DEVICE_NATIVE_ENDIAN
,
2022 .min_access_size
= 1,
2023 .max_access_size
= 1,
2027 static void lsi_ram_write(void *opaque
, hwaddr addr
,
2028 uint64_t val
, unsigned size
)
2030 LSIState
*s
= opaque
;
2035 newval
= s
->script_ram
[addr
>> 2];
2036 shift
= (addr
& 3) * 8;
2037 mask
= ((uint64_t)1 << (size
* 8)) - 1;
2038 newval
&= ~(mask
<< shift
);
2039 newval
|= val
<< shift
;
2040 s
->script_ram
[addr
>> 2] = newval
;
2043 static uint64_t lsi_ram_read(void *opaque
, hwaddr addr
,
2046 LSIState
*s
= opaque
;
2050 val
= s
->script_ram
[addr
>> 2];
2051 mask
= ((uint64_t)1 << (size
* 8)) - 1;
2052 val
>>= (addr
& 3) * 8;
2056 static const MemoryRegionOps lsi_ram_ops
= {
2057 .read
= lsi_ram_read
,
2058 .write
= lsi_ram_write
,
2059 .endianness
= DEVICE_NATIVE_ENDIAN
,
2062 static uint64_t lsi_io_read(void *opaque
, hwaddr addr
,
2065 LSIState
*s
= opaque
;
2066 return lsi_reg_readb(s
, addr
& 0xff);
2069 static void lsi_io_write(void *opaque
, hwaddr addr
,
2070 uint64_t val
, unsigned size
)
2072 LSIState
*s
= opaque
;
2073 lsi_reg_writeb(s
, addr
& 0xff, val
);
2076 static const MemoryRegionOps lsi_io_ops
= {
2077 .read
= lsi_io_read
,
2078 .write
= lsi_io_write
,
2079 .endianness
= DEVICE_NATIVE_ENDIAN
,
2081 .min_access_size
= 1,
2082 .max_access_size
= 1,
2086 static void lsi_scsi_reset(DeviceState
*dev
)
2088 LSIState
*s
= LSI53C895A(dev
);
2093 static int lsi_pre_save(void *opaque
)
2095 LSIState
*s
= opaque
;
2098 assert(s
->current
->dma_buf
== NULL
);
2099 assert(s
->current
->dma_len
== 0);
2101 assert(QTAILQ_EMPTY(&s
->queue
));
2106 static const VMStateDescription vmstate_lsi_scsi
= {
2109 .minimum_version_id
= 0,
2110 .pre_save
= lsi_pre_save
,
2111 .fields
= (VMStateField
[]) {
2112 VMSTATE_PCI_DEVICE(parent_obj
, LSIState
),
2114 VMSTATE_INT32(carry
, LSIState
),
2115 VMSTATE_INT32(status
, LSIState
),
2116 VMSTATE_INT32(msg_action
, LSIState
),
2117 VMSTATE_INT32(msg_len
, LSIState
),
2118 VMSTATE_BUFFER(msg
, LSIState
),
2119 VMSTATE_INT32(waiting
, LSIState
),
2121 VMSTATE_UINT32(dsa
, LSIState
),
2122 VMSTATE_UINT32(temp
, LSIState
),
2123 VMSTATE_UINT32(dnad
, LSIState
),
2124 VMSTATE_UINT32(dbc
, LSIState
),
2125 VMSTATE_UINT8(istat0
, LSIState
),
2126 VMSTATE_UINT8(istat1
, LSIState
),
2127 VMSTATE_UINT8(dcmd
, LSIState
),
2128 VMSTATE_UINT8(dstat
, LSIState
),
2129 VMSTATE_UINT8(dien
, LSIState
),
2130 VMSTATE_UINT8(sist0
, LSIState
),
2131 VMSTATE_UINT8(sist1
, LSIState
),
2132 VMSTATE_UINT8(sien0
, LSIState
),
2133 VMSTATE_UINT8(sien1
, LSIState
),
2134 VMSTATE_UINT8(mbox0
, LSIState
),
2135 VMSTATE_UINT8(mbox1
, LSIState
),
2136 VMSTATE_UINT8(dfifo
, LSIState
),
2137 VMSTATE_UINT8(ctest2
, LSIState
),
2138 VMSTATE_UINT8(ctest3
, LSIState
),
2139 VMSTATE_UINT8(ctest4
, LSIState
),
2140 VMSTATE_UINT8(ctest5
, LSIState
),
2141 VMSTATE_UINT8(ccntl0
, LSIState
),
2142 VMSTATE_UINT8(ccntl1
, LSIState
),
2143 VMSTATE_UINT32(dsp
, LSIState
),
2144 VMSTATE_UINT32(dsps
, LSIState
),
2145 VMSTATE_UINT8(dmode
, LSIState
),
2146 VMSTATE_UINT8(dcntl
, LSIState
),
2147 VMSTATE_UINT8(scntl0
, LSIState
),
2148 VMSTATE_UINT8(scntl1
, LSIState
),
2149 VMSTATE_UINT8(scntl2
, LSIState
),
2150 VMSTATE_UINT8(scntl3
, LSIState
),
2151 VMSTATE_UINT8(sstat0
, LSIState
),
2152 VMSTATE_UINT8(sstat1
, LSIState
),
2153 VMSTATE_UINT8(scid
, LSIState
),
2154 VMSTATE_UINT8(sxfer
, LSIState
),
2155 VMSTATE_UINT8(socl
, LSIState
),
2156 VMSTATE_UINT8(sdid
, LSIState
),
2157 VMSTATE_UINT8(ssid
, LSIState
),
2158 VMSTATE_UINT8(sfbr
, LSIState
),
2159 VMSTATE_UINT8(stest1
, LSIState
),
2160 VMSTATE_UINT8(stest2
, LSIState
),
2161 VMSTATE_UINT8(stest3
, LSIState
),
2162 VMSTATE_UINT8(sidl
, LSIState
),
2163 VMSTATE_UINT8(stime0
, LSIState
),
2164 VMSTATE_UINT8(respid0
, LSIState
),
2165 VMSTATE_UINT8(respid1
, LSIState
),
2166 VMSTATE_UINT32(mmrs
, LSIState
),
2167 VMSTATE_UINT32(mmws
, LSIState
),
2168 VMSTATE_UINT32(sfs
, LSIState
),
2169 VMSTATE_UINT32(drs
, LSIState
),
2170 VMSTATE_UINT32(sbms
, LSIState
),
2171 VMSTATE_UINT32(dbms
, LSIState
),
2172 VMSTATE_UINT32(dnad64
, LSIState
),
2173 VMSTATE_UINT32(pmjad1
, LSIState
),
2174 VMSTATE_UINT32(pmjad2
, LSIState
),
2175 VMSTATE_UINT32(rbc
, LSIState
),
2176 VMSTATE_UINT32(ua
, LSIState
),
2177 VMSTATE_UINT32(ia
, LSIState
),
2178 VMSTATE_UINT32(sbc
, LSIState
),
2179 VMSTATE_UINT32(csbc
, LSIState
),
2180 VMSTATE_BUFFER_UNSAFE(scratch
, LSIState
, 0, 18 * sizeof(uint32_t)),
2181 VMSTATE_UINT8(sbr
, LSIState
),
2183 VMSTATE_BUFFER_UNSAFE(script_ram
, LSIState
, 0, 2048 * sizeof(uint32_t)),
2184 VMSTATE_END_OF_LIST()
2188 static const struct SCSIBusInfo lsi_scsi_info
= {
2190 .max_target
= LSI_MAX_DEVS
,
2191 .max_lun
= 0, /* LUN support is buggy */
2193 .transfer_data
= lsi_transfer_data
,
2194 .complete
= lsi_command_complete
,
2195 .cancel
= lsi_request_cancelled
2198 static void lsi_scsi_realize(PCIDevice
*dev
, Error
**errp
)
2200 LSIState
*s
= LSI53C895A(dev
);
2201 DeviceState
*d
= DEVICE(dev
);
2204 pci_conf
= dev
->config
;
2206 /* PCI latency timer = 255 */
2207 pci_conf
[PCI_LATENCY_TIMER
] = 0xff;
2208 /* Interrupt pin A */
2209 pci_conf
[PCI_INTERRUPT_PIN
] = 0x01;
2211 memory_region_init_io(&s
->mmio_io
, OBJECT(s
), &lsi_mmio_ops
, s
,
2213 memory_region_init_io(&s
->ram_io
, OBJECT(s
), &lsi_ram_ops
, s
,
2215 memory_region_init_io(&s
->io_io
, OBJECT(s
), &lsi_io_ops
, s
,
2218 address_space_init(&s
->pci_io_as
, pci_address_space_io(dev
), "lsi-pci-io");
2219 qdev_init_gpio_out(d
, &s
->ext_irq
, 1);
2221 pci_register_bar(dev
, 0, PCI_BASE_ADDRESS_SPACE_IO
, &s
->io_io
);
2222 pci_register_bar(dev
, 1, PCI_BASE_ADDRESS_SPACE_MEMORY
, &s
->mmio_io
);
2223 pci_register_bar(dev
, 2, PCI_BASE_ADDRESS_SPACE_MEMORY
, &s
->ram_io
);
2224 QTAILQ_INIT(&s
->queue
);
2226 scsi_bus_new(&s
->bus
, sizeof(s
->bus
), d
, &lsi_scsi_info
, NULL
);
2229 static void lsi_scsi_unrealize(DeviceState
*dev
, Error
**errp
)
2231 LSIState
*s
= LSI53C895A(dev
);
2233 address_space_destroy(&s
->pci_io_as
);
2236 static void lsi_class_init(ObjectClass
*klass
, void *data
)
2238 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2239 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
2241 k
->realize
= lsi_scsi_realize
;
2242 k
->vendor_id
= PCI_VENDOR_ID_LSI_LOGIC
;
2243 k
->device_id
= PCI_DEVICE_ID_LSI_53C895A
;
2244 k
->class_id
= PCI_CLASS_STORAGE_SCSI
;
2245 k
->subsystem_id
= 0x1000;
2246 dc
->unrealize
= lsi_scsi_unrealize
;
2247 dc
->reset
= lsi_scsi_reset
;
2248 dc
->vmsd
= &vmstate_lsi_scsi
;
2249 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
2252 static const TypeInfo lsi_info
= {
2253 .name
= TYPE_LSI53C895A
,
2254 .parent
= TYPE_PCI_DEVICE
,
2255 .instance_size
= sizeof(LSIState
),
2256 .class_init
= lsi_class_init
,
2257 .interfaces
= (InterfaceInfo
[]) {
2258 { INTERFACE_CONVENTIONAL_PCI_DEVICE
},
2263 static void lsi53c810_class_init(ObjectClass
*klass
, void *data
)
2265 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
2267 k
->device_id
= PCI_DEVICE_ID_LSI_53C810
;
2270 static TypeInfo lsi53c810_info
= {
2271 .name
= TYPE_LSI53C810
,
2272 .parent
= TYPE_LSI53C895A
,
2273 .class_init
= lsi53c810_class_init
,
2276 static void lsi53c895a_register_types(void)
2278 type_register_static(&lsi_info
);
2279 type_register_static(&lsi53c810_info
);
2282 type_init(lsi53c895a_register_types
)
2284 void lsi53c8xx_handle_legacy_cmdline(DeviceState
*lsi_dev
)
2286 LSIState
*s
= LSI53C895A(lsi_dev
);
2288 scsi_bus_legacy_handle_cmdline(&s
->bus
);