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"
23 #include "qemu/module.h"
26 static const char *names
[] = {
27 "SCNTL0", "SCNTL1", "SCNTL2", "SCNTL3", "SCID", "SXFER", "SDID", "GPREG",
28 "SFBR", "SOCL", "SSID", "SBCL", "DSTAT", "SSTAT0", "SSTAT1", "SSTAT2",
29 "DSA0", "DSA1", "DSA2", "DSA3", "ISTAT", "0x15", "0x16", "0x17",
30 "CTEST0", "CTEST1", "CTEST2", "CTEST3", "TEMP0", "TEMP1", "TEMP2", "TEMP3",
31 "DFIFO", "CTEST4", "CTEST5", "CTEST6", "DBC0", "DBC1", "DBC2", "DCMD",
32 "DNAD0", "DNAD1", "DNAD2", "DNAD3", "DSP0", "DSP1", "DSP2", "DSP3",
33 "DSPS0", "DSPS1", "DSPS2", "DSPS3", "SCRATCHA0", "SCRATCHA1", "SCRATCHA2", "SCRATCHA3",
34 "DMODE", "DIEN", "SBR", "DCNTL", "ADDER0", "ADDER1", "ADDER2", "ADDER3",
35 "SIEN0", "SIEN1", "SIST0", "SIST1", "SLPAR", "0x45", "MACNTL", "GPCNTL",
36 "STIME0", "STIME1", "RESPID", "0x4b", "STEST0", "STEST1", "STEST2", "STEST3",
37 "SIDL", "0x51", "0x52", "0x53", "SODL", "0x55", "0x56", "0x57",
38 "SBDL", "0x59", "0x5a", "0x5b", "SCRATCHB0", "SCRATCHB1", "SCRATCHB2", "SCRATCHB3",
41 #define LSI_MAX_DEVS 7
43 #define LSI_SCNTL0_TRG 0x01
44 #define LSI_SCNTL0_AAP 0x02
45 #define LSI_SCNTL0_EPC 0x08
46 #define LSI_SCNTL0_WATN 0x10
47 #define LSI_SCNTL0_START 0x20
49 #define LSI_SCNTL1_SST 0x01
50 #define LSI_SCNTL1_IARB 0x02
51 #define LSI_SCNTL1_AESP 0x04
52 #define LSI_SCNTL1_RST 0x08
53 #define LSI_SCNTL1_CON 0x10
54 #define LSI_SCNTL1_DHP 0x20
55 #define LSI_SCNTL1_ADB 0x40
56 #define LSI_SCNTL1_EXC 0x80
58 #define LSI_SCNTL2_WSR 0x01
59 #define LSI_SCNTL2_VUE0 0x02
60 #define LSI_SCNTL2_VUE1 0x04
61 #define LSI_SCNTL2_WSS 0x08
62 #define LSI_SCNTL2_SLPHBEN 0x10
63 #define LSI_SCNTL2_SLPMD 0x20
64 #define LSI_SCNTL2_CHM 0x40
65 #define LSI_SCNTL2_SDU 0x80
67 #define LSI_ISTAT0_DIP 0x01
68 #define LSI_ISTAT0_SIP 0x02
69 #define LSI_ISTAT0_INTF 0x04
70 #define LSI_ISTAT0_CON 0x08
71 #define LSI_ISTAT0_SEM 0x10
72 #define LSI_ISTAT0_SIGP 0x20
73 #define LSI_ISTAT0_SRST 0x40
74 #define LSI_ISTAT0_ABRT 0x80
76 #define LSI_ISTAT1_SI 0x01
77 #define LSI_ISTAT1_SRUN 0x02
78 #define LSI_ISTAT1_FLSH 0x04
80 #define LSI_SSTAT0_SDP0 0x01
81 #define LSI_SSTAT0_RST 0x02
82 #define LSI_SSTAT0_WOA 0x04
83 #define LSI_SSTAT0_LOA 0x08
84 #define LSI_SSTAT0_AIP 0x10
85 #define LSI_SSTAT0_OLF 0x20
86 #define LSI_SSTAT0_ORF 0x40
87 #define LSI_SSTAT0_ILF 0x80
89 #define LSI_SIST0_PAR 0x01
90 #define LSI_SIST0_RST 0x02
91 #define LSI_SIST0_UDC 0x04
92 #define LSI_SIST0_SGE 0x08
93 #define LSI_SIST0_RSL 0x10
94 #define LSI_SIST0_SEL 0x20
95 #define LSI_SIST0_CMP 0x40
96 #define LSI_SIST0_MA 0x80
98 #define LSI_SIST1_HTH 0x01
99 #define LSI_SIST1_GEN 0x02
100 #define LSI_SIST1_STO 0x04
101 #define LSI_SIST1_SBMC 0x10
103 #define LSI_SOCL_IO 0x01
104 #define LSI_SOCL_CD 0x02
105 #define LSI_SOCL_MSG 0x04
106 #define LSI_SOCL_ATN 0x08
107 #define LSI_SOCL_SEL 0x10
108 #define LSI_SOCL_BSY 0x20
109 #define LSI_SOCL_ACK 0x40
110 #define LSI_SOCL_REQ 0x80
112 #define LSI_DSTAT_IID 0x01
113 #define LSI_DSTAT_SIR 0x04
114 #define LSI_DSTAT_SSI 0x08
115 #define LSI_DSTAT_ABRT 0x10
116 #define LSI_DSTAT_BF 0x20
117 #define LSI_DSTAT_MDPE 0x40
118 #define LSI_DSTAT_DFE 0x80
120 #define LSI_DCNTL_COM 0x01
121 #define LSI_DCNTL_IRQD 0x02
122 #define LSI_DCNTL_STD 0x04
123 #define LSI_DCNTL_IRQM 0x08
124 #define LSI_DCNTL_SSM 0x10
125 #define LSI_DCNTL_PFEN 0x20
126 #define LSI_DCNTL_PFF 0x40
127 #define LSI_DCNTL_CLSE 0x80
129 #define LSI_DMODE_MAN 0x01
130 #define LSI_DMODE_BOF 0x02
131 #define LSI_DMODE_ERMP 0x04
132 #define LSI_DMODE_ERL 0x08
133 #define LSI_DMODE_DIOM 0x10
134 #define LSI_DMODE_SIOM 0x20
136 #define LSI_CTEST2_DACK 0x01
137 #define LSI_CTEST2_DREQ 0x02
138 #define LSI_CTEST2_TEOP 0x04
139 #define LSI_CTEST2_PCICIE 0x08
140 #define LSI_CTEST2_CM 0x10
141 #define LSI_CTEST2_CIO 0x20
142 #define LSI_CTEST2_SIGP 0x40
143 #define LSI_CTEST2_DDIR 0x80
145 #define LSI_CTEST5_BL2 0x04
146 #define LSI_CTEST5_DDIR 0x08
147 #define LSI_CTEST5_MASR 0x10
148 #define LSI_CTEST5_DFSN 0x20
149 #define LSI_CTEST5_BBCK 0x40
150 #define LSI_CTEST5_ADCK 0x80
152 #define LSI_CCNTL0_DILS 0x01
153 #define LSI_CCNTL0_DISFC 0x10
154 #define LSI_CCNTL0_ENNDJ 0x20
155 #define LSI_CCNTL0_PMJCTL 0x40
156 #define LSI_CCNTL0_ENPMJ 0x80
158 #define LSI_CCNTL1_EN64DBMV 0x01
159 #define LSI_CCNTL1_EN64TIBMV 0x02
160 #define LSI_CCNTL1_64TIMOD 0x04
161 #define LSI_CCNTL1_DDAC 0x08
162 #define LSI_CCNTL1_ZMOD 0x80
164 #define LSI_SBCL_ATN 0x08
165 #define LSI_SBCL_BSY 0x20
166 #define LSI_SBCL_ACK 0x40
167 #define LSI_SBCL_REQ 0x80
169 /* Enable Response to Reselection */
170 #define LSI_SCID_RRE 0x60
172 #define LSI_CCNTL1_40BIT (LSI_CCNTL1_EN64TIBMV|LSI_CCNTL1_64TIMOD)
182 /* Maximum length of MSG IN data. */
183 #define LSI_MAX_MSGIN_LEN 8
185 /* Flag set if this is a tagged command. */
186 #define LSI_TAG_VALID (1 << 16)
188 typedef struct lsi_request
{
195 QTAILQ_ENTRY(lsi_request
) next
;
199 LSI_NOWAIT
, /* SCRIPTS are running or stopped */
200 LSI_WAIT_RESELECT
, /* Wait Reselect instruction has been issued */
201 LSI_DMA_SCRIPTS
, /* processing DMA from lsi_execute_script */
202 LSI_DMA_IN_PROGRESS
, /* DMA operation is in progress */
206 LSI_MSG_ACTION_COMMAND
= 0,
207 LSI_MSG_ACTION_DISCONNECT
= 1,
208 LSI_MSG_ACTION_DOUT
= 2,
209 LSI_MSG_ACTION_DIN
= 3,
214 PCIDevice parent_obj
;
218 MemoryRegion mmio_io
;
221 AddressSpace pci_io_as
;
223 int carry
; /* ??? Should this be an a visible register somewhere? */
227 uint8_t msg
[LSI_MAX_MSGIN_LEN
];
231 /* The tag is a combination of the device ID and the SCSI tag. */
233 int command_complete
;
234 QTAILQ_HEAD(, lsi_request
) queue
;
235 lsi_request
*current
;
297 uint32_t scratch
[18]; /* SCRATCHA-SCRATCHR */
301 uint8_t script_ram
[2048 * sizeof(uint32_t)];
304 #define TYPE_LSI53C810 "lsi53c810"
305 #define TYPE_LSI53C895A "lsi53c895a"
307 #define LSI53C895A(obj) \
308 OBJECT_CHECK(LSIState, (obj), TYPE_LSI53C895A)
310 static const char *scsi_phases
[] = {
321 static const char *scsi_phase_name(int phase
)
323 return scsi_phases
[phase
& PHASE_MASK
];
326 static inline int lsi_irq_on_rsl(LSIState
*s
)
328 return (s
->sien0
& LSI_SIST0_RSL
) && (s
->scid
& LSI_SCID_RRE
);
331 static lsi_request
*get_pending_req(LSIState
*s
)
335 QTAILQ_FOREACH(p
, &s
->queue
, next
) {
343 static void lsi_soft_reset(LSIState
*s
)
348 s
->msg_action
= LSI_MSG_ACTION_COMMAND
;
350 s
->waiting
= LSI_NOWAIT
;
355 memset(s
->scratch
, 0, sizeof(s
->scratch
));
368 s
->ctest2
= LSI_CTEST2_DACK
;
412 assert(QTAILQ_EMPTY(&s
->queue
));
416 static int lsi_dma_40bit(LSIState
*s
)
418 if ((s
->ccntl1
& LSI_CCNTL1_40BIT
) == LSI_CCNTL1_40BIT
)
423 static int lsi_dma_ti64bit(LSIState
*s
)
425 if ((s
->ccntl1
& LSI_CCNTL1_EN64TIBMV
) == LSI_CCNTL1_EN64TIBMV
)
430 static int lsi_dma_64bit(LSIState
*s
)
432 if ((s
->ccntl1
& LSI_CCNTL1_EN64DBMV
) == LSI_CCNTL1_EN64DBMV
)
437 static uint8_t lsi_reg_readb(LSIState
*s
, int offset
);
438 static void lsi_reg_writeb(LSIState
*s
, int offset
, uint8_t val
);
439 static void lsi_execute_script(LSIState
*s
);
440 static void lsi_reselect(LSIState
*s
, lsi_request
*p
);
442 static inline void lsi_mem_read(LSIState
*s
, dma_addr_t addr
,
443 void *buf
, dma_addr_t len
)
445 if (s
->dmode
& LSI_DMODE_SIOM
) {
446 address_space_read(&s
->pci_io_as
, addr
, MEMTXATTRS_UNSPECIFIED
,
449 pci_dma_read(PCI_DEVICE(s
), addr
, buf
, len
);
453 static inline void lsi_mem_write(LSIState
*s
, dma_addr_t addr
,
454 const void *buf
, dma_addr_t len
)
456 if (s
->dmode
& LSI_DMODE_DIOM
) {
457 address_space_write(&s
->pci_io_as
, addr
, MEMTXATTRS_UNSPECIFIED
,
460 pci_dma_write(PCI_DEVICE(s
), addr
, buf
, len
);
464 static inline uint32_t read_dword(LSIState
*s
, uint32_t addr
)
468 pci_dma_read(PCI_DEVICE(s
), addr
, &buf
, 4);
469 return cpu_to_le32(buf
);
472 static void lsi_stop_script(LSIState
*s
)
474 s
->istat1
&= ~LSI_ISTAT1_SRUN
;
477 static void lsi_set_irq(LSIState
*s
, int level
)
479 PCIDevice
*d
= PCI_DEVICE(s
);
482 qemu_set_irq(s
->ext_irq
, level
);
484 pci_set_irq(d
, level
);
488 static void lsi_update_irq(LSIState
*s
)
491 static int last_level
;
493 /* It's unclear whether the DIP/SIP bits should be cleared when the
494 Interrupt Status Registers are cleared or when istat0 is read.
495 We currently do the formwer, which seems to work. */
498 if (s
->dstat
& s
->dien
)
500 s
->istat0
|= LSI_ISTAT0_DIP
;
502 s
->istat0
&= ~LSI_ISTAT0_DIP
;
505 if (s
->sist0
|| s
->sist1
) {
506 if ((s
->sist0
& s
->sien0
) || (s
->sist1
& s
->sien1
))
508 s
->istat0
|= LSI_ISTAT0_SIP
;
510 s
->istat0
&= ~LSI_ISTAT0_SIP
;
512 if (s
->istat0
& LSI_ISTAT0_INTF
)
515 if (level
!= last_level
) {
516 trace_lsi_update_irq(level
, s
->dstat
, s
->sist1
, s
->sist0
);
519 lsi_set_irq(s
, level
);
521 if (!s
->current
&& !level
&& lsi_irq_on_rsl(s
) && !(s
->scntl1
& LSI_SCNTL1_CON
)) {
524 trace_lsi_update_irq_disconnected();
525 p
= get_pending_req(s
);
532 /* Stop SCRIPTS execution and raise a SCSI interrupt. */
533 static void lsi_script_scsi_interrupt(LSIState
*s
, int stat0
, int stat1
)
538 trace_lsi_script_scsi_interrupt(stat1
, stat0
, s
->sist1
, s
->sist0
);
541 /* Stop processor on fatal or unmasked interrupt. As a special hack
542 we don't stop processing when raising STO. Instead continue
543 execution and stop at the next insn that accesses the SCSI bus. */
544 mask0
= s
->sien0
| ~(LSI_SIST0_CMP
| LSI_SIST0_SEL
| LSI_SIST0_RSL
);
545 mask1
= s
->sien1
| ~(LSI_SIST1_GEN
| LSI_SIST1_HTH
);
546 mask1
&= ~LSI_SIST1_STO
;
547 if (s
->sist0
& mask0
|| s
->sist1
& mask1
) {
553 /* Stop SCRIPTS execution and raise a DMA interrupt. */
554 static void lsi_script_dma_interrupt(LSIState
*s
, int stat
)
556 trace_lsi_script_dma_interrupt(stat
, s
->dstat
);
562 static inline void lsi_set_phase(LSIState
*s
, int phase
)
564 s
->sbcl
&= ~PHASE_MASK
;
565 s
->sbcl
|= phase
| LSI_SBCL_REQ
;
566 s
->sstat1
= (s
->sstat1
& ~PHASE_MASK
) | phase
;
569 static void lsi_bad_phase(LSIState
*s
, int out
, int new_phase
)
571 /* Trigger a phase mismatch. */
572 if (s
->ccntl0
& LSI_CCNTL0_ENPMJ
) {
573 if ((s
->ccntl0
& LSI_CCNTL0_PMJCTL
)) {
574 s
->dsp
= out
? s
->pmjad1
: s
->pmjad2
;
576 s
->dsp
= (s
->scntl2
& LSI_SCNTL2_WSR
? s
->pmjad2
: s
->pmjad1
);
578 trace_lsi_bad_phase_jump(s
->dsp
);
580 trace_lsi_bad_phase_interrupt();
581 lsi_script_scsi_interrupt(s
, LSI_SIST0_MA
, 0);
584 lsi_set_phase(s
, new_phase
);
588 /* Resume SCRIPTS execution after a DMA operation. */
589 static void lsi_resume_script(LSIState
*s
)
591 if (s
->waiting
!= 2) {
592 s
->waiting
= LSI_NOWAIT
;
593 lsi_execute_script(s
);
595 s
->waiting
= LSI_NOWAIT
;
599 static void lsi_disconnect(LSIState
*s
)
601 s
->scntl1
&= ~LSI_SCNTL1_CON
;
602 s
->sstat1
&= ~PHASE_MASK
;
606 static void lsi_bad_selection(LSIState
*s
, uint32_t id
)
608 trace_lsi_bad_selection(id
);
609 lsi_script_scsi_interrupt(s
, 0, LSI_SIST1_STO
);
613 /* Initiate a SCSI layer data transfer. */
614 static void lsi_do_dma(LSIState
*s
, int out
)
621 if (!s
->current
->dma_len
) {
622 /* Wait until data is available. */
623 trace_lsi_do_dma_unavailable();
627 dev
= s
->current
->req
->dev
;
631 if (count
> s
->current
->dma_len
)
632 count
= s
->current
->dma_len
;
635 /* both 40 and Table Indirect 64-bit DMAs store upper bits in dnad64 */
636 if (lsi_dma_40bit(s
) || lsi_dma_ti64bit(s
))
637 addr
|= ((uint64_t)s
->dnad64
<< 32);
639 addr
|= ((uint64_t)s
->dbms
<< 32);
641 addr
|= ((uint64_t)s
->sbms
<< 32);
643 trace_lsi_do_dma(addr
, count
);
647 if (s
->current
->dma_buf
== NULL
) {
648 s
->current
->dma_buf
= scsi_req_get_buf(s
->current
->req
);
650 /* ??? Set SFBR to first data byte. */
652 lsi_mem_read(s
, addr
, s
->current
->dma_buf
, count
);
654 lsi_mem_write(s
, addr
, s
->current
->dma_buf
, count
);
656 s
->current
->dma_len
-= count
;
657 if (s
->current
->dma_len
== 0) {
658 s
->current
->dma_buf
= NULL
;
659 scsi_req_continue(s
->current
->req
);
661 s
->current
->dma_buf
+= count
;
662 lsi_resume_script(s
);
667 /* Add a command to the queue. */
668 static void lsi_queue_command(LSIState
*s
)
670 lsi_request
*p
= s
->current
;
672 trace_lsi_queue_command(p
->tag
);
673 assert(s
->current
!= NULL
);
674 assert(s
->current
->dma_len
== 0);
675 QTAILQ_INSERT_TAIL(&s
->queue
, s
->current
, next
);
679 p
->out
= (s
->sstat1
& PHASE_MASK
) == PHASE_DO
;
682 /* Queue a byte for a MSG IN phase. */
683 static void lsi_add_msg_byte(LSIState
*s
, uint8_t data
)
685 if (s
->msg_len
>= LSI_MAX_MSGIN_LEN
) {
686 trace_lsi_add_msg_byte_error();
688 trace_lsi_add_msg_byte(data
);
689 s
->msg
[s
->msg_len
++] = data
;
693 /* Perform reselection to continue a command. */
694 static void lsi_reselect(LSIState
*s
, lsi_request
*p
)
698 assert(s
->current
== NULL
);
699 QTAILQ_REMOVE(&s
->queue
, p
, next
);
702 id
= (p
->tag
>> 8) & 0xf;
704 /* LSI53C700 Family Compatibility, see LSI53C895A 4-73 */
705 if (!(s
->dcntl
& LSI_DCNTL_COM
)) {
706 s
->sfbr
= 1 << (id
& 0x7);
708 trace_lsi_reselect(id
);
709 s
->scntl1
|= LSI_SCNTL1_CON
;
710 lsi_set_phase(s
, PHASE_MI
);
711 s
->msg_action
= p
->out
? LSI_MSG_ACTION_DOUT
: LSI_MSG_ACTION_DIN
;
712 s
->current
->dma_len
= p
->pending
;
713 lsi_add_msg_byte(s
, 0x80);
714 if (s
->current
->tag
& LSI_TAG_VALID
) {
715 lsi_add_msg_byte(s
, 0x20);
716 lsi_add_msg_byte(s
, p
->tag
& 0xff);
719 if (lsi_irq_on_rsl(s
)) {
720 lsi_script_scsi_interrupt(s
, LSI_SIST0_RSL
, 0);
724 static lsi_request
*lsi_find_by_tag(LSIState
*s
, uint32_t tag
)
728 QTAILQ_FOREACH(p
, &s
->queue
, next
) {
737 static void lsi_request_free(LSIState
*s
, lsi_request
*p
)
739 if (p
== s
->current
) {
742 QTAILQ_REMOVE(&s
->queue
, p
, next
);
747 static void lsi_request_cancelled(SCSIRequest
*req
)
749 LSIState
*s
= LSI53C895A(req
->bus
->qbus
.parent
);
750 lsi_request
*p
= req
->hba_private
;
752 req
->hba_private
= NULL
;
753 lsi_request_free(s
, p
);
757 /* Record that data is available for a queued command. Returns zero if
758 the device was reselected, nonzero if the IO is deferred. */
759 static int lsi_queue_req(LSIState
*s
, SCSIRequest
*req
, uint32_t len
)
761 lsi_request
*p
= req
->hba_private
;
764 trace_lsi_queue_req_error(p
);
767 /* Reselect if waiting for it, or if reselection triggers an IRQ
769 Since no interrupt stacking is implemented in the emulation, it
770 is also required that there are no pending interrupts waiting
771 for service from the device driver. */
772 if (s
->waiting
== LSI_WAIT_RESELECT
||
773 (lsi_irq_on_rsl(s
) && !(s
->scntl1
& LSI_SCNTL1_CON
) &&
774 !(s
->istat0
& (LSI_ISTAT0_SIP
| LSI_ISTAT0_DIP
)))) {
775 /* Reselect device. */
779 trace_lsi_queue_req(p
->tag
);
785 /* Callback to indicate that the SCSI layer has completed a command. */
786 static void lsi_command_complete(SCSIRequest
*req
, uint32_t status
, size_t resid
)
788 LSIState
*s
= LSI53C895A(req
->bus
->qbus
.parent
);
791 out
= (s
->sstat1
& PHASE_MASK
) == PHASE_DO
;
792 trace_lsi_command_complete(status
);
794 s
->command_complete
= 2;
795 if (s
->waiting
&& s
->dbc
!= 0) {
796 /* Raise phase mismatch for short transfers. */
797 lsi_bad_phase(s
, out
, PHASE_ST
);
799 lsi_set_phase(s
, PHASE_ST
);
802 if (req
->hba_private
== s
->current
) {
803 req
->hba_private
= NULL
;
804 lsi_request_free(s
, s
->current
);
807 lsi_resume_script(s
);
810 /* Callback to indicate that the SCSI layer has completed a transfer. */
811 static void lsi_transfer_data(SCSIRequest
*req
, uint32_t len
)
813 LSIState
*s
= LSI53C895A(req
->bus
->qbus
.parent
);
816 assert(req
->hba_private
);
817 if (s
->waiting
== LSI_WAIT_RESELECT
|| req
->hba_private
!= s
->current
||
818 (lsi_irq_on_rsl(s
) && !(s
->scntl1
& LSI_SCNTL1_CON
))) {
819 if (lsi_queue_req(s
, req
, len
)) {
824 out
= (s
->sstat1
& PHASE_MASK
) == PHASE_DO
;
826 /* host adapter (re)connected */
827 trace_lsi_transfer_data(req
->tag
, len
);
828 s
->current
->dma_len
= len
;
829 s
->command_complete
= 1;
831 if (s
->waiting
== LSI_WAIT_RESELECT
|| s
->dbc
== 0) {
832 lsi_resume_script(s
);
839 static void lsi_do_command(LSIState
*s
)
846 trace_lsi_do_command(s
->dbc
);
849 pci_dma_read(PCI_DEVICE(s
), s
->dnad
, buf
, s
->dbc
);
851 s
->command_complete
= 0;
853 id
= (s
->select_tag
>> 8) & 0xf;
854 dev
= scsi_device_find(&s
->bus
, 0, id
, s
->current_lun
);
856 lsi_bad_selection(s
, id
);
860 assert(s
->current
== NULL
);
861 s
->current
= g_new0(lsi_request
, 1);
862 s
->current
->tag
= s
->select_tag
;
863 s
->current
->req
= scsi_req_new(dev
, s
->current
->tag
, s
->current_lun
, buf
,
866 n
= scsi_req_enqueue(s
->current
->req
);
869 lsi_set_phase(s
, PHASE_DI
);
871 lsi_set_phase(s
, PHASE_DO
);
873 scsi_req_continue(s
->current
->req
);
875 if (!s
->command_complete
) {
877 /* Command did not complete immediately so disconnect. */
878 lsi_add_msg_byte(s
, 2); /* SAVE DATA POINTER */
879 lsi_add_msg_byte(s
, 4); /* DISCONNECT */
881 lsi_set_phase(s
, PHASE_MI
);
882 s
->msg_action
= LSI_MSG_ACTION_DISCONNECT
;
883 lsi_queue_command(s
);
885 /* wait command complete */
886 lsi_set_phase(s
, PHASE_DI
);
891 static void lsi_do_status(LSIState
*s
)
894 trace_lsi_do_status(s
->dbc
, s
->status
);
896 trace_lsi_do_status_error();
901 pci_dma_write(PCI_DEVICE(s
), s
->dnad
, &status
, 1);
902 lsi_set_phase(s
, PHASE_MI
);
903 s
->msg_action
= LSI_MSG_ACTION_DISCONNECT
;
904 lsi_add_msg_byte(s
, 0); /* COMMAND COMPLETE */
907 static void lsi_do_msgin(LSIState
*s
)
910 trace_lsi_do_msgin(s
->dbc
, s
->msg_len
);
913 assert(len
> 0 && len
<= LSI_MAX_MSGIN_LEN
);
916 pci_dma_write(PCI_DEVICE(s
), s
->dnad
, s
->msg
, len
);
917 /* Linux drivers rely on the last byte being in the SIDL. */
918 s
->sidl
= s
->msg
[len
- 1];
921 memmove(s
->msg
, s
->msg
+ len
, s
->msg_len
);
923 /* ??? Check if ATN (not yet implemented) is asserted and maybe
924 switch to PHASE_MO. */
925 switch (s
->msg_action
) {
926 case LSI_MSG_ACTION_COMMAND
:
927 lsi_set_phase(s
, PHASE_CMD
);
929 case LSI_MSG_ACTION_DISCONNECT
:
932 case LSI_MSG_ACTION_DOUT
:
933 lsi_set_phase(s
, PHASE_DO
);
935 case LSI_MSG_ACTION_DIN
:
936 lsi_set_phase(s
, PHASE_DI
);
944 /* Read the next byte during a MSGOUT phase. */
945 static uint8_t lsi_get_msgbyte(LSIState
*s
)
948 pci_dma_read(PCI_DEVICE(s
), s
->dnad
, &data
, 1);
954 /* Skip the next n bytes during a MSGOUT phase. */
955 static void lsi_skip_msgbytes(LSIState
*s
, unsigned int n
)
961 static void lsi_do_msgout(LSIState
*s
)
965 uint32_t current_tag
;
966 lsi_request
*current_req
, *p
, *p_next
;
969 current_tag
= s
->current
->tag
;
970 current_req
= s
->current
;
972 current_tag
= s
->select_tag
;
973 current_req
= lsi_find_by_tag(s
, current_tag
);
976 trace_lsi_do_msgout(s
->dbc
);
978 msg
= lsi_get_msgbyte(s
);
983 trace_lsi_do_msgout_disconnect();
987 trace_lsi_do_msgout_noop();
988 lsi_set_phase(s
, PHASE_CMD
);
991 len
= lsi_get_msgbyte(s
);
992 msg
= lsi_get_msgbyte(s
);
993 (void)len
; /* avoid a warning about unused variable*/
994 trace_lsi_do_msgout_extended(msg
, len
);
997 trace_lsi_do_msgout_ignored("SDTR");
998 lsi_skip_msgbytes(s
, 2);
1001 trace_lsi_do_msgout_ignored("WDTR");
1002 lsi_skip_msgbytes(s
, 1);
1005 trace_lsi_do_msgout_ignored("PPR");
1006 lsi_skip_msgbytes(s
, 5);
1012 case 0x20: /* SIMPLE queue */
1013 s
->select_tag
|= lsi_get_msgbyte(s
) | LSI_TAG_VALID
;
1014 trace_lsi_do_msgout_simplequeue(s
->select_tag
& 0xff);
1016 case 0x21: /* HEAD of queue */
1017 qemu_log_mask(LOG_UNIMP
, "lsi_scsi: HEAD queue not implemented\n");
1018 s
->select_tag
|= lsi_get_msgbyte(s
) | LSI_TAG_VALID
;
1020 case 0x22: /* ORDERED queue */
1021 qemu_log_mask(LOG_UNIMP
,
1022 "lsi_scsi: ORDERED queue not implemented\n");
1023 s
->select_tag
|= lsi_get_msgbyte(s
) | LSI_TAG_VALID
;
1026 /* The ABORT TAG message clears the current I/O process only. */
1027 trace_lsi_do_msgout_abort(current_tag
);
1029 scsi_req_cancel(current_req
->req
);
1036 /* The ABORT message clears all I/O processes for the selecting
1037 initiator on the specified logical unit of the target. */
1039 trace_lsi_do_msgout_abort(current_tag
);
1041 /* The CLEAR QUEUE message clears all I/O processes for all
1042 initiators on the specified logical unit of the target. */
1044 trace_lsi_do_msgout_clearqueue(current_tag
);
1046 /* The BUS DEVICE RESET message clears all I/O processes for all
1047 initiators on all logical units of the target. */
1049 trace_lsi_do_msgout_busdevicereset(current_tag
);
1052 /* clear the current I/O process */
1054 scsi_req_cancel(s
->current
->req
);
1057 /* As the current implemented devices scsi_disk and scsi_generic
1058 only support one LUN, we don't need to keep track of LUNs.
1059 Clearing I/O processes for other initiators could be possible
1060 for scsi_generic by sending a SG_SCSI_RESET to the /dev/sgX
1061 device, but this is currently not implemented (and seems not
1062 to be really necessary). So let's simply clear all queued
1063 commands for the current device: */
1064 QTAILQ_FOREACH_SAFE(p
, &s
->queue
, next
, p_next
) {
1065 if ((p
->tag
& 0x0000ff00) == (current_tag
& 0x0000ff00)) {
1066 scsi_req_cancel(p
->req
);
1073 if ((msg
& 0x80) == 0) {
1076 s
->current_lun
= msg
& 7;
1077 trace_lsi_do_msgout_select(s
->current_lun
);
1078 lsi_set_phase(s
, PHASE_CMD
);
1084 qemu_log_mask(LOG_UNIMP
, "Unimplemented message 0x%02x\n", msg
);
1085 lsi_set_phase(s
, PHASE_MI
);
1086 lsi_add_msg_byte(s
, 7); /* MESSAGE REJECT */
1087 s
->msg_action
= LSI_MSG_ACTION_COMMAND
;
1090 #define LSI_BUF_SIZE 4096
1091 static void lsi_memcpy(LSIState
*s
, uint32_t dest
, uint32_t src
, int count
)
1094 uint8_t buf
[LSI_BUF_SIZE
];
1096 trace_lsi_memcpy(dest
, src
, count
);
1098 n
= (count
> LSI_BUF_SIZE
) ? LSI_BUF_SIZE
: count
;
1099 lsi_mem_read(s
, src
, buf
, n
);
1100 lsi_mem_write(s
, dest
, buf
, n
);
1107 static void lsi_wait_reselect(LSIState
*s
)
1111 trace_lsi_wait_reselect();
1116 p
= get_pending_req(s
);
1120 if (s
->current
== NULL
) {
1121 s
->waiting
= LSI_WAIT_RESELECT
;
1125 static void lsi_execute_script(LSIState
*s
)
1127 PCIDevice
*pci_dev
= PCI_DEVICE(s
);
1129 uint32_t addr
, addr_high
;
1131 int insn_processed
= 0;
1133 s
->istat1
|= LSI_ISTAT1_SRUN
;
1136 insn
= read_dword(s
, s
->dsp
);
1138 /* If we receive an empty opcode increment the DSP by 4 bytes
1139 instead of 8 and execute the next opcode at that location */
1143 addr
= read_dword(s
, s
->dsp
+ 4);
1145 trace_lsi_execute_script(s
->dsp
, insn
, addr
);
1147 s
->dcmd
= insn
>> 24;
1149 switch (insn
>> 30) {
1150 case 0: /* Block move. */
1151 if (s
->sist1
& LSI_SIST1_STO
) {
1152 trace_lsi_execute_script_blockmove_delayed();
1156 s
->dbc
= insn
& 0xffffff;
1160 if (insn
& (1 << 29)) {
1161 /* Indirect addressing. */
1162 addr
= read_dword(s
, addr
);
1163 } else if (insn
& (1 << 28)) {
1166 /* Table indirect addressing. */
1168 /* 32-bit Table indirect */
1169 offset
= sextract32(addr
, 0, 24);
1170 pci_dma_read(pci_dev
, s
->dsa
+ offset
, buf
, 8);
1171 /* byte count is stored in bits 0:23 only */
1172 s
->dbc
= cpu_to_le32(buf
[0]) & 0xffffff;
1174 addr
= cpu_to_le32(buf
[1]);
1176 /* 40-bit DMA, upper addr bits [39:32] stored in first DWORD of
1177 * table, bits [31:24] */
1178 if (lsi_dma_40bit(s
))
1179 addr_high
= cpu_to_le32(buf
[0]) >> 24;
1180 else if (lsi_dma_ti64bit(s
)) {
1181 int selector
= (cpu_to_le32(buf
[0]) >> 24) & 0x1f;
1184 /* offset index into scratch registers since
1185 * TI64 mode can use registers C to R */
1186 addr_high
= s
->scratch
[2 + selector
];
1189 addr_high
= s
->mmrs
;
1192 addr_high
= s
->mmws
;
1201 addr_high
= s
->sbms
;
1204 addr_high
= s
->dbms
;
1207 qemu_log_mask(LOG_GUEST_ERROR
,
1208 "lsi_scsi: Illegal selector specified (0x%x > 0x15) "
1209 "for 64-bit DMA block move", selector
);
1213 } else if (lsi_dma_64bit(s
)) {
1214 /* fetch a 3rd dword if 64-bit direct move is enabled and
1215 only if we're not doing table indirect or indirect addressing */
1216 s
->dbms
= read_dword(s
, s
->dsp
);
1218 s
->ia
= s
->dsp
- 12;
1220 if ((s
->sstat1
& PHASE_MASK
) != ((insn
>> 24) & 7)) {
1221 trace_lsi_execute_script_blockmove_badphase(
1222 scsi_phase_name(s
->sstat1
),
1223 scsi_phase_name(insn
>> 24));
1224 lsi_script_scsi_interrupt(s
, LSI_SIST0_MA
, 0);
1228 s
->dnad64
= addr_high
;
1229 switch (s
->sstat1
& 0x7) {
1231 s
->waiting
= LSI_DMA_SCRIPTS
;
1234 s
->waiting
= LSI_DMA_IN_PROGRESS
;
1237 s
->waiting
= LSI_DMA_SCRIPTS
;
1240 s
->waiting
= LSI_DMA_IN_PROGRESS
;
1255 qemu_log_mask(LOG_UNIMP
, "lsi_scsi: Unimplemented phase %s\n",
1256 scsi_phase_name(s
->sstat1
));
1258 s
->dfifo
= s
->dbc
& 0xff;
1259 s
->ctest5
= (s
->ctest5
& 0xfc) | ((s
->dbc
>> 8) & 3);
1262 s
->ua
= addr
+ s
->dbc
;
1265 case 1: /* IO or Read/Write instruction. */
1266 opcode
= (insn
>> 27) & 7;
1270 if (insn
& (1 << 25)) {
1271 id
= read_dword(s
, s
->dsa
+ sextract32(insn
, 0, 24));
1275 id
= (id
>> 16) & 0xf;
1276 if (insn
& (1 << 26)) {
1277 addr
= s
->dsp
+ sextract32(addr
, 0, 24);
1281 case 0: /* Select */
1283 if (s
->scntl1
& LSI_SCNTL1_CON
) {
1284 trace_lsi_execute_script_io_alreadyreselected();
1288 s
->sstat0
|= LSI_SSTAT0_WOA
;
1289 s
->scntl1
&= ~LSI_SCNTL1_IARB
;
1290 if (!scsi_device_find(&s
->bus
, 0, id
, 0)) {
1291 lsi_bad_selection(s
, id
);
1294 trace_lsi_execute_script_io_selected(id
,
1295 insn
& (1 << 3) ? " ATN" : "");
1296 /* ??? Linux drivers compain when this is set. Maybe
1297 it only applies in low-level mode (unimplemented).
1298 lsi_script_scsi_interrupt(s, LSI_SIST0_CMP, 0); */
1299 s
->select_tag
= id
<< 8;
1300 s
->scntl1
|= LSI_SCNTL1_CON
;
1301 if (insn
& (1 << 3)) {
1302 s
->socl
|= LSI_SOCL_ATN
;
1303 s
->sbcl
|= LSI_SBCL_ATN
;
1305 s
->sbcl
|= LSI_SBCL_BSY
;
1306 lsi_set_phase(s
, PHASE_MO
);
1307 s
->waiting
= LSI_NOWAIT
;
1309 case 1: /* Disconnect */
1310 trace_lsi_execute_script_io_disconnect();
1311 s
->scntl1
&= ~LSI_SCNTL1_CON
;
1312 /* FIXME: this is not entirely correct; the target need not ask
1313 * for reselection until it has to send data, while here we force a
1314 * reselection as soon as the bus is free. The correct flow would
1315 * reselect before lsi_transfer_data and disconnect as soon as
1319 lsi_request
*p
= get_pending_req(s
);
1325 case 2: /* Wait Reselect */
1326 if (s
->istat0
& LSI_ISTAT0_SIGP
) {
1328 } else if (!lsi_irq_on_rsl(s
)) {
1329 lsi_wait_reselect(s
);
1333 trace_lsi_execute_script_io_set(
1334 insn
& (1 << 3) ? " ATN" : "",
1335 insn
& (1 << 6) ? " ACK" : "",
1336 insn
& (1 << 9) ? " TM" : "",
1337 insn
& (1 << 10) ? " CC" : "");
1338 if (insn
& (1 << 3)) {
1339 s
->socl
|= LSI_SOCL_ATN
;
1340 s
->sbcl
|= LSI_SBCL_ATN
;
1341 lsi_set_phase(s
, PHASE_MO
);
1344 if (insn
& (1 << 6)) {
1345 s
->sbcl
|= LSI_SBCL_ACK
;
1348 if (insn
& (1 << 9)) {
1349 qemu_log_mask(LOG_UNIMP
,
1350 "lsi_scsi: Target mode not implemented\n");
1352 if (insn
& (1 << 10))
1356 trace_lsi_execute_script_io_clear(
1357 insn
& (1 << 3) ? " ATN" : "",
1358 insn
& (1 << 6) ? " ACK" : "",
1359 insn
& (1 << 9) ? " TM" : "",
1360 insn
& (1 << 10) ? " CC" : "");
1361 if (insn
& (1 << 3)) {
1362 s
->socl
&= ~LSI_SOCL_ATN
;
1363 s
->sbcl
&= ~LSI_SBCL_ATN
;
1366 if (insn
& (1 << 6)) {
1367 s
->sbcl
&= ~LSI_SBCL_ACK
;
1370 if (insn
& (1 << 10))
1381 static const char *opcode_names
[3] =
1382 {"Write", "Read", "Read-Modify-Write"};
1383 static const char *operator_names
[8] =
1384 {"MOV", "SHL", "OR", "XOR", "AND", "SHR", "ADD", "ADC"};
1386 reg
= ((insn
>> 16) & 0x7f) | (insn
& 0x80);
1387 data8
= (insn
>> 8) & 0xff;
1388 opcode
= (insn
>> 27) & 7;
1389 operator = (insn
>> 24) & 7;
1390 trace_lsi_execute_script_io_opcode(
1391 opcode_names
[opcode
- 5], reg
,
1392 operator_names
[operator], data8
, s
->sfbr
,
1393 (insn
& (1 << 23)) ? " SFBR" : "");
1396 case 5: /* From SFBR */
1400 case 6: /* To SFBR */
1402 op0
= lsi_reg_readb(s
, reg
);
1405 case 7: /* Read-modify-write */
1407 op0
= lsi_reg_readb(s
, reg
);
1408 if (insn
& (1 << 23)) {
1420 case 1: /* Shift left */
1422 op0
= (op0
<< 1) | s
->carry
;
1436 op0
= (op0
>> 1) | (s
->carry
<< 7);
1441 s
->carry
= op0
< op1
;
1444 op0
+= op1
+ s
->carry
;
1446 s
->carry
= op0
<= op1
;
1448 s
->carry
= op0
< op1
;
1453 case 5: /* From SFBR */
1454 case 7: /* Read-modify-write */
1455 lsi_reg_writeb(s
, reg
, op0
);
1457 case 6: /* To SFBR */
1464 case 2: /* Transfer Control. */
1469 if ((insn
& 0x002e0000) == 0) {
1470 trace_lsi_execute_script_tc_nop();
1473 if (s
->sist1
& LSI_SIST1_STO
) {
1474 trace_lsi_execute_script_tc_delayedselect_timeout();
1478 cond
= jmp
= (insn
& (1 << 19)) != 0;
1479 if (cond
== jmp
&& (insn
& (1 << 21))) {
1480 trace_lsi_execute_script_tc_compc(s
->carry
== jmp
);
1481 cond
= s
->carry
!= 0;
1483 if (cond
== jmp
&& (insn
& (1 << 17))) {
1484 trace_lsi_execute_script_tc_compp(scsi_phase_name(s
->sstat1
),
1485 jmp
? '=' : '!', scsi_phase_name(insn
>> 24));
1486 cond
= (s
->sstat1
& PHASE_MASK
) == ((insn
>> 24) & 7);
1488 if (cond
== jmp
&& (insn
& (1 << 18))) {
1491 mask
= (~insn
>> 8) & 0xff;
1492 trace_lsi_execute_script_tc_compd(
1493 s
->sfbr
, mask
, jmp
? '=' : '!', insn
& mask
);
1494 cond
= (s
->sfbr
& mask
) == (insn
& mask
);
1497 if (insn
& (1 << 23)) {
1498 /* Relative address. */
1499 addr
= s
->dsp
+ sextract32(addr
, 0, 24);
1501 switch ((insn
>> 27) & 7) {
1503 trace_lsi_execute_script_tc_jump(addr
);
1508 trace_lsi_execute_script_tc_call(addr
);
1512 case 2: /* Return */
1513 trace_lsi_execute_script_tc_return(s
->temp
);
1516 case 3: /* Interrupt */
1517 trace_lsi_execute_script_tc_interrupt(s
->dsps
);
1518 if ((insn
& (1 << 20)) != 0) {
1519 s
->istat0
|= LSI_ISTAT0_INTF
;
1522 lsi_script_dma_interrupt(s
, LSI_DSTAT_SIR
);
1526 trace_lsi_execute_script_tc_illegal();
1527 lsi_script_dma_interrupt(s
, LSI_DSTAT_IID
);
1531 trace_lsi_execute_script_tc_cc_failed();
1537 if ((insn
& (1 << 29)) == 0) {
1540 /* ??? The docs imply the destination address is loaded into
1541 the TEMP register. However the Linux drivers rely on
1542 the value being presrved. */
1543 dest
= read_dword(s
, s
->dsp
);
1545 lsi_memcpy(s
, dest
, addr
, insn
& 0xffffff);
1552 if (insn
& (1 << 28)) {
1553 addr
= s
->dsa
+ sextract32(addr
, 0, 24);
1556 reg
= (insn
>> 16) & 0xff;
1557 if (insn
& (1 << 24)) {
1558 pci_dma_read(pci_dev
, addr
, data
, n
);
1559 trace_lsi_execute_script_mm_load(reg
, n
, addr
, *(int *)data
);
1560 for (i
= 0; i
< n
; i
++) {
1561 lsi_reg_writeb(s
, reg
+ i
, data
[i
]);
1564 trace_lsi_execute_script_mm_store(reg
, n
, addr
);
1565 for (i
= 0; i
< n
; i
++) {
1566 data
[i
] = lsi_reg_readb(s
, reg
+ i
);
1568 pci_dma_write(pci_dev
, addr
, data
, n
);
1572 if (insn_processed
> 10000 && s
->waiting
== LSI_NOWAIT
) {
1573 /* Some windows drivers make the device spin waiting for a memory
1574 location to change. If we have been executed a lot of code then
1575 assume this is the case and force an unexpected device disconnect.
1576 This is apparently sufficient to beat the drivers into submission.
1578 if (!(s
->sien0
& LSI_SIST0_UDC
)) {
1579 qemu_log_mask(LOG_GUEST_ERROR
,
1580 "lsi_scsi: inf. loop with UDC masked");
1582 lsi_script_scsi_interrupt(s
, LSI_SIST0_UDC
, 0);
1584 } else if (s
->istat1
& LSI_ISTAT1_SRUN
&& s
->waiting
== LSI_NOWAIT
) {
1585 if (s
->dcntl
& LSI_DCNTL_SSM
) {
1586 lsi_script_dma_interrupt(s
, LSI_DSTAT_SSI
);
1591 trace_lsi_execute_script_stop();
1594 static uint8_t lsi_reg_readb(LSIState
*s
, int offset
)
1598 #define CASE_GET_REG24(name, addr) \
1599 case addr: ret = s->name & 0xff; break; \
1600 case addr + 1: ret = (s->name >> 8) & 0xff; break; \
1601 case addr + 2: ret = (s->name >> 16) & 0xff; break;
1603 #define CASE_GET_REG32(name, addr) \
1604 case addr: ret = s->name & 0xff; break; \
1605 case addr + 1: ret = (s->name >> 8) & 0xff; break; \
1606 case addr + 2: ret = (s->name >> 16) & 0xff; break; \
1607 case addr + 3: ret = (s->name >> 24) & 0xff; break;
1610 case 0x00: /* SCNTL0 */
1613 case 0x01: /* SCNTL1 */
1616 case 0x02: /* SCNTL2 */
1619 case 0x03: /* SCNTL3 */
1622 case 0x04: /* SCID */
1625 case 0x05: /* SXFER */
1628 case 0x06: /* SDID */
1631 case 0x07: /* GPREG0 */
1634 case 0x08: /* Revision ID */
1637 case 0x09: /* SOCL */
1640 case 0xa: /* SSID */
1643 case 0xb: /* SBCL */
1646 case 0xc: /* DSTAT */
1647 ret
= s
->dstat
| LSI_DSTAT_DFE
;
1648 if ((s
->istat0
& LSI_ISTAT0_INTF
) == 0)
1652 case 0x0d: /* SSTAT0 */
1655 case 0x0e: /* SSTAT1 */
1658 case 0x0f: /* SSTAT2 */
1659 ret
= s
->scntl1
& LSI_SCNTL1_CON
? 0 : 2;
1661 CASE_GET_REG32(dsa
, 0x10)
1662 case 0x14: /* ISTAT0 */
1665 case 0x15: /* ISTAT1 */
1668 case 0x16: /* MBOX0 */
1671 case 0x17: /* MBOX1 */
1674 case 0x18: /* CTEST0 */
1677 case 0x19: /* CTEST1 */
1680 case 0x1a: /* CTEST2 */
1681 ret
= s
->ctest2
| LSI_CTEST2_DACK
| LSI_CTEST2_CM
;
1682 if (s
->istat0
& LSI_ISTAT0_SIGP
) {
1683 s
->istat0
&= ~LSI_ISTAT0_SIGP
;
1684 ret
|= LSI_CTEST2_SIGP
;
1687 case 0x1b: /* CTEST3 */
1690 CASE_GET_REG32(temp
, 0x1c)
1691 case 0x20: /* DFIFO */
1694 case 0x21: /* CTEST4 */
1697 case 0x22: /* CTEST5 */
1700 case 0x23: /* CTEST6 */
1703 CASE_GET_REG24(dbc
, 0x24)
1704 case 0x27: /* DCMD */
1707 CASE_GET_REG32(dnad
, 0x28)
1708 CASE_GET_REG32(dsp
, 0x2c)
1709 CASE_GET_REG32(dsps
, 0x30)
1710 CASE_GET_REG32(scratch
[0], 0x34)
1711 case 0x38: /* DMODE */
1714 case 0x39: /* DIEN */
1717 case 0x3a: /* SBR */
1720 case 0x3b: /* DCNTL */
1723 /* ADDER Output (Debug of relative jump address) */
1724 CASE_GET_REG32(adder
, 0x3c)
1725 case 0x40: /* SIEN0 */
1728 case 0x41: /* SIEN1 */
1731 case 0x42: /* SIST0 */
1736 case 0x43: /* SIST1 */
1741 case 0x46: /* MACNTL */
1744 case 0x47: /* GPCNTL0 */
1747 case 0x48: /* STIME0 */
1750 case 0x4a: /* RESPID0 */
1753 case 0x4b: /* RESPID1 */
1756 case 0x4d: /* STEST1 */
1759 case 0x4e: /* STEST2 */
1762 case 0x4f: /* STEST3 */
1765 case 0x50: /* SIDL */
1766 /* This is needed by the linux drivers. We currently only update it
1767 during the MSG IN phase. */
1770 case 0x52: /* STEST4 */
1773 case 0x56: /* CCNTL0 */
1776 case 0x57: /* CCNTL1 */
1779 case 0x58: /* SBDL */
1780 /* Some drivers peek at the data bus during the MSG IN phase. */
1781 if ((s
->sstat1
& PHASE_MASK
) == PHASE_MI
) {
1782 assert(s
->msg_len
> 0);
1787 case 0x59: /* SBDL high */
1790 CASE_GET_REG32(mmrs
, 0xa0)
1791 CASE_GET_REG32(mmws
, 0xa4)
1792 CASE_GET_REG32(sfs
, 0xa8)
1793 CASE_GET_REG32(drs
, 0xac)
1794 CASE_GET_REG32(sbms
, 0xb0)
1795 CASE_GET_REG32(dbms
, 0xb4)
1796 CASE_GET_REG32(dnad64
, 0xb8)
1797 CASE_GET_REG32(pmjad1
, 0xc0)
1798 CASE_GET_REG32(pmjad2
, 0xc4)
1799 CASE_GET_REG32(rbc
, 0xc8)
1800 CASE_GET_REG32(ua
, 0xcc)
1801 CASE_GET_REG32(ia
, 0xd4)
1802 CASE_GET_REG32(sbc
, 0xd8)
1803 CASE_GET_REG32(csbc
, 0xdc)
1808 n
= (offset
- 0x58) >> 2;
1809 shift
= (offset
& 3) * 8;
1810 ret
= (s
->scratch
[n
] >> shift
) & 0xff;
1815 qemu_log_mask(LOG_GUEST_ERROR
,
1816 "lsi_scsi: invalid read from reg %s %x\n",
1817 offset
< ARRAY_SIZE(names
) ? names
[offset
] : "???",
1823 #undef CASE_GET_REG24
1824 #undef CASE_GET_REG32
1826 trace_lsi_reg_read(offset
< ARRAY_SIZE(names
) ? names
[offset
] : "???",
1832 static void lsi_reg_writeb(LSIState
*s
, int offset
, uint8_t val
)
1834 #define CASE_SET_REG24(name, addr) \
1835 case addr : s->name &= 0xffffff00; s->name |= val; break; \
1836 case addr + 1: s->name &= 0xffff00ff; s->name |= val << 8; break; \
1837 case addr + 2: s->name &= 0xff00ffff; s->name |= val << 16; break;
1839 #define CASE_SET_REG32(name, addr) \
1840 case addr : s->name &= 0xffffff00; s->name |= val; break; \
1841 case addr + 1: s->name &= 0xffff00ff; s->name |= val << 8; break; \
1842 case addr + 2: s->name &= 0xff00ffff; s->name |= val << 16; break; \
1843 case addr + 3: s->name &= 0x00ffffff; s->name |= val << 24; break;
1845 trace_lsi_reg_write(offset
< ARRAY_SIZE(names
) ? names
[offset
] : "???",
1849 case 0x00: /* SCNTL0 */
1851 if (val
& LSI_SCNTL0_START
) {
1852 qemu_log_mask(LOG_UNIMP
,
1853 "lsi_scsi: Start sequence not implemented\n");
1856 case 0x01: /* SCNTL1 */
1857 s
->scntl1
= val
& ~LSI_SCNTL1_SST
;
1858 if (val
& LSI_SCNTL1_IARB
) {
1859 qemu_log_mask(LOG_UNIMP
,
1860 "lsi_scsi: Immediate Arbritration not implemented\n");
1862 if (val
& LSI_SCNTL1_RST
) {
1863 if (!(s
->sstat0
& LSI_SSTAT0_RST
)) {
1864 qbus_reset_all(BUS(&s
->bus
));
1865 s
->sstat0
|= LSI_SSTAT0_RST
;
1866 lsi_script_scsi_interrupt(s
, LSI_SIST0_RST
, 0);
1869 s
->sstat0
&= ~LSI_SSTAT0_RST
;
1872 case 0x02: /* SCNTL2 */
1873 val
&= ~(LSI_SCNTL2_WSR
| LSI_SCNTL2_WSS
);
1876 case 0x03: /* SCNTL3 */
1879 case 0x04: /* SCID */
1882 case 0x05: /* SXFER */
1885 case 0x06: /* SDID */
1886 if ((s
->ssid
& 0x80) && (val
& 0xf) != (s
->ssid
& 0xf)) {
1887 qemu_log_mask(LOG_GUEST_ERROR
,
1888 "lsi_scsi: Destination ID does not match SSID\n");
1890 s
->sdid
= val
& 0xf;
1892 case 0x07: /* GPREG0 */
1894 case 0x08: /* SFBR */
1895 /* The CPU is not allowed to write to this register. However the
1896 SCRIPTS register move instructions are. */
1899 case 0x0a: case 0x0b:
1900 /* Openserver writes to these readonly registers on startup */
1902 case 0x0c: case 0x0d: case 0x0e: case 0x0f:
1903 /* Linux writes to these readonly registers on startup. */
1905 CASE_SET_REG32(dsa
, 0x10)
1906 case 0x14: /* ISTAT0 */
1907 s
->istat0
= (s
->istat0
& 0x0f) | (val
& 0xf0);
1908 if (val
& LSI_ISTAT0_ABRT
) {
1909 lsi_script_dma_interrupt(s
, LSI_DSTAT_ABRT
);
1911 if (val
& LSI_ISTAT0_INTF
) {
1912 s
->istat0
&= ~LSI_ISTAT0_INTF
;
1915 if (s
->waiting
== LSI_WAIT_RESELECT
&& val
& LSI_ISTAT0_SIGP
) {
1917 s
->waiting
= LSI_NOWAIT
;
1919 lsi_execute_script(s
);
1921 if (val
& LSI_ISTAT0_SRST
) {
1922 qdev_reset_all(DEVICE(s
));
1925 case 0x16: /* MBOX0 */
1928 case 0x17: /* MBOX1 */
1931 case 0x18: /* CTEST0 */
1934 case 0x1a: /* CTEST2 */
1935 s
->ctest2
= val
& LSI_CTEST2_PCICIE
;
1937 case 0x1b: /* CTEST3 */
1938 s
->ctest3
= val
& 0x0f;
1940 CASE_SET_REG32(temp
, 0x1c)
1941 case 0x21: /* CTEST4 */
1943 qemu_log_mask(LOG_UNIMP
,
1944 "lsi_scsi: Unimplemented CTEST4-FBL 0x%x\n", val
);
1948 case 0x22: /* CTEST5 */
1949 if (val
& (LSI_CTEST5_ADCK
| LSI_CTEST5_BBCK
)) {
1950 qemu_log_mask(LOG_UNIMP
,
1951 "lsi_scsi: CTEST5 DMA increment not implemented\n");
1955 CASE_SET_REG24(dbc
, 0x24)
1956 CASE_SET_REG32(dnad
, 0x28)
1957 case 0x2c: /* DSP[0:7] */
1958 s
->dsp
&= 0xffffff00;
1961 case 0x2d: /* DSP[8:15] */
1962 s
->dsp
&= 0xffff00ff;
1965 case 0x2e: /* DSP[16:23] */
1966 s
->dsp
&= 0xff00ffff;
1967 s
->dsp
|= val
<< 16;
1969 case 0x2f: /* DSP[24:31] */
1970 s
->dsp
&= 0x00ffffff;
1971 s
->dsp
|= val
<< 24;
1972 if ((s
->dmode
& LSI_DMODE_MAN
) == 0
1973 && (s
->istat1
& LSI_ISTAT1_SRUN
) == 0)
1974 lsi_execute_script(s
);
1976 CASE_SET_REG32(dsps
, 0x30)
1977 CASE_SET_REG32(scratch
[0], 0x34)
1978 case 0x38: /* DMODE */
1981 case 0x39: /* DIEN */
1985 case 0x3a: /* SBR */
1988 case 0x3b: /* DCNTL */
1989 s
->dcntl
= val
& ~(LSI_DCNTL_PFF
| LSI_DCNTL_STD
);
1990 if ((val
& LSI_DCNTL_STD
) && (s
->istat1
& LSI_ISTAT1_SRUN
) == 0)
1991 lsi_execute_script(s
);
1993 case 0x40: /* SIEN0 */
1997 case 0x41: /* SIEN1 */
2001 case 0x47: /* GPCNTL0 */
2003 case 0x48: /* STIME0 */
2006 case 0x49: /* STIME1 */
2008 qemu_log_mask(LOG_UNIMP
,
2009 "lsi_scsi: General purpose timer not implemented\n");
2010 /* ??? Raising the interrupt immediately seems to be sufficient
2011 to keep the FreeBSD driver happy. */
2012 lsi_script_scsi_interrupt(s
, 0, LSI_SIST1_GEN
);
2015 case 0x4a: /* RESPID0 */
2018 case 0x4b: /* RESPID1 */
2021 case 0x4d: /* STEST1 */
2024 case 0x4e: /* STEST2 */
2026 qemu_log_mask(LOG_UNIMP
,
2027 "lsi_scsi: Low level mode not implemented\n");
2031 case 0x4f: /* STEST3 */
2033 qemu_log_mask(LOG_UNIMP
,
2034 "lsi_scsi: SCSI FIFO test mode not implemented\n");
2038 case 0x56: /* CCNTL0 */
2041 case 0x57: /* CCNTL1 */
2044 CASE_SET_REG32(mmrs
, 0xa0)
2045 CASE_SET_REG32(mmws
, 0xa4)
2046 CASE_SET_REG32(sfs
, 0xa8)
2047 CASE_SET_REG32(drs
, 0xac)
2048 CASE_SET_REG32(sbms
, 0xb0)
2049 CASE_SET_REG32(dbms
, 0xb4)
2050 CASE_SET_REG32(dnad64
, 0xb8)
2051 CASE_SET_REG32(pmjad1
, 0xc0)
2052 CASE_SET_REG32(pmjad2
, 0xc4)
2053 CASE_SET_REG32(rbc
, 0xc8)
2054 CASE_SET_REG32(ua
, 0xcc)
2055 CASE_SET_REG32(ia
, 0xd4)
2056 CASE_SET_REG32(sbc
, 0xd8)
2057 CASE_SET_REG32(csbc
, 0xdc)
2059 if (offset
>= 0x5c && offset
< 0xa0) {
2062 n
= (offset
- 0x58) >> 2;
2063 shift
= (offset
& 3) * 8;
2064 s
->scratch
[n
] = deposit32(s
->scratch
[n
], shift
, 8, val
);
2066 qemu_log_mask(LOG_GUEST_ERROR
,
2067 "lsi_scsi: invalid write to reg %s %x (0x%02x)\n",
2068 offset
< ARRAY_SIZE(names
) ? names
[offset
] : "???",
2072 #undef CASE_SET_REG24
2073 #undef CASE_SET_REG32
2076 static void lsi_mmio_write(void *opaque
, hwaddr addr
,
2077 uint64_t val
, unsigned size
)
2079 LSIState
*s
= opaque
;
2081 lsi_reg_writeb(s
, addr
& 0xff, val
);
2084 static uint64_t lsi_mmio_read(void *opaque
, hwaddr addr
,
2087 LSIState
*s
= opaque
;
2088 return lsi_reg_readb(s
, addr
& 0xff);
2091 static const MemoryRegionOps lsi_mmio_ops
= {
2092 .read
= lsi_mmio_read
,
2093 .write
= lsi_mmio_write
,
2094 .endianness
= DEVICE_LITTLE_ENDIAN
,
2096 .min_access_size
= 1,
2097 .max_access_size
= 1,
2101 static void lsi_ram_write(void *opaque
, hwaddr addr
,
2102 uint64_t val
, unsigned size
)
2104 LSIState
*s
= opaque
;
2105 stn_le_p(s
->script_ram
+ addr
, size
, val
);
2108 static uint64_t lsi_ram_read(void *opaque
, hwaddr addr
,
2111 LSIState
*s
= opaque
;
2112 return ldn_le_p(s
->script_ram
+ addr
, size
);
2115 static const MemoryRegionOps lsi_ram_ops
= {
2116 .read
= lsi_ram_read
,
2117 .write
= lsi_ram_write
,
2118 .endianness
= DEVICE_LITTLE_ENDIAN
,
2121 static uint64_t lsi_io_read(void *opaque
, hwaddr addr
,
2124 LSIState
*s
= opaque
;
2125 return lsi_reg_readb(s
, addr
& 0xff);
2128 static void lsi_io_write(void *opaque
, hwaddr addr
,
2129 uint64_t val
, unsigned size
)
2131 LSIState
*s
= opaque
;
2132 lsi_reg_writeb(s
, addr
& 0xff, val
);
2135 static const MemoryRegionOps lsi_io_ops
= {
2136 .read
= lsi_io_read
,
2137 .write
= lsi_io_write
,
2138 .endianness
= DEVICE_LITTLE_ENDIAN
,
2140 .min_access_size
= 1,
2141 .max_access_size
= 1,
2145 static void lsi_scsi_reset(DeviceState
*dev
)
2147 LSIState
*s
= LSI53C895A(dev
);
2152 static int lsi_pre_save(void *opaque
)
2154 LSIState
*s
= opaque
;
2157 assert(s
->current
->dma_buf
== NULL
);
2158 assert(s
->current
->dma_len
== 0);
2160 assert(QTAILQ_EMPTY(&s
->queue
));
2165 static int lsi_post_load(void *opaque
, int version_id
)
2167 LSIState
*s
= opaque
;
2169 if (s
->msg_len
< 0 || s
->msg_len
> LSI_MAX_MSGIN_LEN
) {
2176 static const VMStateDescription vmstate_lsi_scsi
= {
2179 .minimum_version_id
= 0,
2180 .pre_save
= lsi_pre_save
,
2181 .post_load
= lsi_post_load
,
2182 .fields
= (VMStateField
[]) {
2183 VMSTATE_PCI_DEVICE(parent_obj
, LSIState
),
2185 VMSTATE_INT32(carry
, LSIState
),
2186 VMSTATE_INT32(status
, LSIState
),
2187 VMSTATE_INT32(msg_action
, LSIState
),
2188 VMSTATE_INT32(msg_len
, LSIState
),
2189 VMSTATE_BUFFER(msg
, LSIState
),
2190 VMSTATE_INT32(waiting
, LSIState
),
2192 VMSTATE_UINT32(dsa
, LSIState
),
2193 VMSTATE_UINT32(temp
, LSIState
),
2194 VMSTATE_UINT32(dnad
, LSIState
),
2195 VMSTATE_UINT32(dbc
, LSIState
),
2196 VMSTATE_UINT8(istat0
, LSIState
),
2197 VMSTATE_UINT8(istat1
, LSIState
),
2198 VMSTATE_UINT8(dcmd
, LSIState
),
2199 VMSTATE_UINT8(dstat
, LSIState
),
2200 VMSTATE_UINT8(dien
, LSIState
),
2201 VMSTATE_UINT8(sist0
, LSIState
),
2202 VMSTATE_UINT8(sist1
, LSIState
),
2203 VMSTATE_UINT8(sien0
, LSIState
),
2204 VMSTATE_UINT8(sien1
, LSIState
),
2205 VMSTATE_UINT8(mbox0
, LSIState
),
2206 VMSTATE_UINT8(mbox1
, LSIState
),
2207 VMSTATE_UINT8(dfifo
, LSIState
),
2208 VMSTATE_UINT8(ctest2
, LSIState
),
2209 VMSTATE_UINT8(ctest3
, LSIState
),
2210 VMSTATE_UINT8(ctest4
, LSIState
),
2211 VMSTATE_UINT8(ctest5
, LSIState
),
2212 VMSTATE_UINT8(ccntl0
, LSIState
),
2213 VMSTATE_UINT8(ccntl1
, LSIState
),
2214 VMSTATE_UINT32(dsp
, LSIState
),
2215 VMSTATE_UINT32(dsps
, LSIState
),
2216 VMSTATE_UINT8(dmode
, LSIState
),
2217 VMSTATE_UINT8(dcntl
, LSIState
),
2218 VMSTATE_UINT8(scntl0
, LSIState
),
2219 VMSTATE_UINT8(scntl1
, LSIState
),
2220 VMSTATE_UINT8(scntl2
, LSIState
),
2221 VMSTATE_UINT8(scntl3
, LSIState
),
2222 VMSTATE_UINT8(sstat0
, LSIState
),
2223 VMSTATE_UINT8(sstat1
, LSIState
),
2224 VMSTATE_UINT8(scid
, LSIState
),
2225 VMSTATE_UINT8(sxfer
, LSIState
),
2226 VMSTATE_UINT8(socl
, LSIState
),
2227 VMSTATE_UINT8(sdid
, LSIState
),
2228 VMSTATE_UINT8(ssid
, LSIState
),
2229 VMSTATE_UINT8(sfbr
, LSIState
),
2230 VMSTATE_UINT8(stest1
, LSIState
),
2231 VMSTATE_UINT8(stest2
, LSIState
),
2232 VMSTATE_UINT8(stest3
, LSIState
),
2233 VMSTATE_UINT8(sidl
, LSIState
),
2234 VMSTATE_UINT8(stime0
, LSIState
),
2235 VMSTATE_UINT8(respid0
, LSIState
),
2236 VMSTATE_UINT8(respid1
, LSIState
),
2237 VMSTATE_UINT8_V(sbcl
, LSIState
, 1),
2238 VMSTATE_UINT32(mmrs
, LSIState
),
2239 VMSTATE_UINT32(mmws
, LSIState
),
2240 VMSTATE_UINT32(sfs
, LSIState
),
2241 VMSTATE_UINT32(drs
, LSIState
),
2242 VMSTATE_UINT32(sbms
, LSIState
),
2243 VMSTATE_UINT32(dbms
, LSIState
),
2244 VMSTATE_UINT32(dnad64
, LSIState
),
2245 VMSTATE_UINT32(pmjad1
, LSIState
),
2246 VMSTATE_UINT32(pmjad2
, LSIState
),
2247 VMSTATE_UINT32(rbc
, LSIState
),
2248 VMSTATE_UINT32(ua
, LSIState
),
2249 VMSTATE_UINT32(ia
, LSIState
),
2250 VMSTATE_UINT32(sbc
, LSIState
),
2251 VMSTATE_UINT32(csbc
, LSIState
),
2252 VMSTATE_BUFFER_UNSAFE(scratch
, LSIState
, 0, 18 * sizeof(uint32_t)),
2253 VMSTATE_UINT8(sbr
, LSIState
),
2255 VMSTATE_BUFFER_UNSAFE(script_ram
, LSIState
, 0, 8192),
2256 VMSTATE_END_OF_LIST()
2260 static const struct SCSIBusInfo lsi_scsi_info
= {
2262 .max_target
= LSI_MAX_DEVS
,
2263 .max_lun
= 0, /* LUN support is buggy */
2265 .transfer_data
= lsi_transfer_data
,
2266 .complete
= lsi_command_complete
,
2267 .cancel
= lsi_request_cancelled
2270 static void lsi_scsi_realize(PCIDevice
*dev
, Error
**errp
)
2272 LSIState
*s
= LSI53C895A(dev
);
2273 DeviceState
*d
= DEVICE(dev
);
2276 pci_conf
= dev
->config
;
2278 /* PCI latency timer = 255 */
2279 pci_conf
[PCI_LATENCY_TIMER
] = 0xff;
2280 /* Interrupt pin A */
2281 pci_conf
[PCI_INTERRUPT_PIN
] = 0x01;
2283 memory_region_init_io(&s
->mmio_io
, OBJECT(s
), &lsi_mmio_ops
, s
,
2285 memory_region_init_io(&s
->ram_io
, OBJECT(s
), &lsi_ram_ops
, s
,
2287 memory_region_init_io(&s
->io_io
, OBJECT(s
), &lsi_io_ops
, s
,
2290 address_space_init(&s
->pci_io_as
, pci_address_space_io(dev
), "lsi-pci-io");
2291 qdev_init_gpio_out(d
, &s
->ext_irq
, 1);
2293 pci_register_bar(dev
, 0, PCI_BASE_ADDRESS_SPACE_IO
, &s
->io_io
);
2294 pci_register_bar(dev
, 1, PCI_BASE_ADDRESS_SPACE_MEMORY
, &s
->mmio_io
);
2295 pci_register_bar(dev
, 2, PCI_BASE_ADDRESS_SPACE_MEMORY
, &s
->ram_io
);
2296 QTAILQ_INIT(&s
->queue
);
2298 scsi_bus_new(&s
->bus
, sizeof(s
->bus
), d
, &lsi_scsi_info
, NULL
);
2301 static void lsi_scsi_unrealize(DeviceState
*dev
, Error
**errp
)
2303 LSIState
*s
= LSI53C895A(dev
);
2305 address_space_destroy(&s
->pci_io_as
);
2308 static void lsi_class_init(ObjectClass
*klass
, void *data
)
2310 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2311 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
2313 k
->realize
= lsi_scsi_realize
;
2314 k
->vendor_id
= PCI_VENDOR_ID_LSI_LOGIC
;
2315 k
->device_id
= PCI_DEVICE_ID_LSI_53C895A
;
2316 k
->class_id
= PCI_CLASS_STORAGE_SCSI
;
2317 k
->subsystem_id
= 0x1000;
2318 dc
->unrealize
= lsi_scsi_unrealize
;
2319 dc
->reset
= lsi_scsi_reset
;
2320 dc
->vmsd
= &vmstate_lsi_scsi
;
2321 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
2324 static const TypeInfo lsi_info
= {
2325 .name
= TYPE_LSI53C895A
,
2326 .parent
= TYPE_PCI_DEVICE
,
2327 .instance_size
= sizeof(LSIState
),
2328 .class_init
= lsi_class_init
,
2329 .interfaces
= (InterfaceInfo
[]) {
2330 { INTERFACE_CONVENTIONAL_PCI_DEVICE
},
2335 static void lsi53c810_class_init(ObjectClass
*klass
, void *data
)
2337 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
2339 k
->device_id
= PCI_DEVICE_ID_LSI_53C810
;
2342 static TypeInfo lsi53c810_info
= {
2343 .name
= TYPE_LSI53C810
,
2344 .parent
= TYPE_LSI53C895A
,
2345 .class_init
= lsi53c810_class_init
,
2348 static void lsi53c895a_register_types(void)
2350 type_register_static(&lsi_info
);
2351 type_register_static(&lsi53c810_info
);
2354 type_init(lsi53c895a_register_types
)
2356 void lsi53c8xx_handle_legacy_cmdline(DeviceState
*lsi_dev
)
2358 LSIState
*s
= LSI53C895A(lsi_dev
);
2360 scsi_bus_legacy_handle_cmdline(&s
->bus
);