2 * QEMU LSI53C895A SCSI Host Bus Adapter emulation
4 * Copyright (c) 2006 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licenced under the LGPL.
10 /* ??? Need to check if the {read,write}[wl] routines work properly on
11 big-endian targets. */
18 #include "block_int.h"
21 //#define DEBUG_LSI_REG
24 #define DPRINTF(fmt, ...) \
25 do { printf("lsi_scsi: " fmt , ## __VA_ARGS__); } while (0)
26 #define BADF(fmt, ...) \
27 do { fprintf(stderr, "lsi_scsi: error: " fmt , ## __VA_ARGS__); exit(1);} while (0)
29 #define DPRINTF(fmt, ...) do {} while(0)
30 #define BADF(fmt, ...) \
31 do { fprintf(stderr, "lsi_scsi: error: " fmt , ## __VA_ARGS__);} while (0)
34 #define LSI_MAX_DEVS 7
36 #define LSI_SCNTL0_TRG 0x01
37 #define LSI_SCNTL0_AAP 0x02
38 #define LSI_SCNTL0_EPC 0x08
39 #define LSI_SCNTL0_WATN 0x10
40 #define LSI_SCNTL0_START 0x20
42 #define LSI_SCNTL1_SST 0x01
43 #define LSI_SCNTL1_IARB 0x02
44 #define LSI_SCNTL1_AESP 0x04
45 #define LSI_SCNTL1_RST 0x08
46 #define LSI_SCNTL1_CON 0x10
47 #define LSI_SCNTL1_DHP 0x20
48 #define LSI_SCNTL1_ADB 0x40
49 #define LSI_SCNTL1_EXC 0x80
51 #define LSI_SCNTL2_WSR 0x01
52 #define LSI_SCNTL2_VUE0 0x02
53 #define LSI_SCNTL2_VUE1 0x04
54 #define LSI_SCNTL2_WSS 0x08
55 #define LSI_SCNTL2_SLPHBEN 0x10
56 #define LSI_SCNTL2_SLPMD 0x20
57 #define LSI_SCNTL2_CHM 0x40
58 #define LSI_SCNTL2_SDU 0x80
60 #define LSI_ISTAT0_DIP 0x01
61 #define LSI_ISTAT0_SIP 0x02
62 #define LSI_ISTAT0_INTF 0x04
63 #define LSI_ISTAT0_CON 0x08
64 #define LSI_ISTAT0_SEM 0x10
65 #define LSI_ISTAT0_SIGP 0x20
66 #define LSI_ISTAT0_SRST 0x40
67 #define LSI_ISTAT0_ABRT 0x80
69 #define LSI_ISTAT1_SI 0x01
70 #define LSI_ISTAT1_SRUN 0x02
71 #define LSI_ISTAT1_FLSH 0x04
73 #define LSI_SSTAT0_SDP0 0x01
74 #define LSI_SSTAT0_RST 0x02
75 #define LSI_SSTAT0_WOA 0x04
76 #define LSI_SSTAT0_LOA 0x08
77 #define LSI_SSTAT0_AIP 0x10
78 #define LSI_SSTAT0_OLF 0x20
79 #define LSI_SSTAT0_ORF 0x40
80 #define LSI_SSTAT0_ILF 0x80
82 #define LSI_SIST0_PAR 0x01
83 #define LSI_SIST0_RST 0x02
84 #define LSI_SIST0_UDC 0x04
85 #define LSI_SIST0_SGE 0x08
86 #define LSI_SIST0_RSL 0x10
87 #define LSI_SIST0_SEL 0x20
88 #define LSI_SIST0_CMP 0x40
89 #define LSI_SIST0_MA 0x80
91 #define LSI_SIST1_HTH 0x01
92 #define LSI_SIST1_GEN 0x02
93 #define LSI_SIST1_STO 0x04
94 #define LSI_SIST1_SBMC 0x10
96 #define LSI_SOCL_IO 0x01
97 #define LSI_SOCL_CD 0x02
98 #define LSI_SOCL_MSG 0x04
99 #define LSI_SOCL_ATN 0x08
100 #define LSI_SOCL_SEL 0x10
101 #define LSI_SOCL_BSY 0x20
102 #define LSI_SOCL_ACK 0x40
103 #define LSI_SOCL_REQ 0x80
105 #define LSI_DSTAT_IID 0x01
106 #define LSI_DSTAT_SIR 0x04
107 #define LSI_DSTAT_SSI 0x08
108 #define LSI_DSTAT_ABRT 0x10
109 #define LSI_DSTAT_BF 0x20
110 #define LSI_DSTAT_MDPE 0x40
111 #define LSI_DSTAT_DFE 0x80
113 #define LSI_DCNTL_COM 0x01
114 #define LSI_DCNTL_IRQD 0x02
115 #define LSI_DCNTL_STD 0x04
116 #define LSI_DCNTL_IRQM 0x08
117 #define LSI_DCNTL_SSM 0x10
118 #define LSI_DCNTL_PFEN 0x20
119 #define LSI_DCNTL_PFF 0x40
120 #define LSI_DCNTL_CLSE 0x80
122 #define LSI_DMODE_MAN 0x01
123 #define LSI_DMODE_BOF 0x02
124 #define LSI_DMODE_ERMP 0x04
125 #define LSI_DMODE_ERL 0x08
126 #define LSI_DMODE_DIOM 0x10
127 #define LSI_DMODE_SIOM 0x20
129 #define LSI_CTEST2_DACK 0x01
130 #define LSI_CTEST2_DREQ 0x02
131 #define LSI_CTEST2_TEOP 0x04
132 #define LSI_CTEST2_PCICIE 0x08
133 #define LSI_CTEST2_CM 0x10
134 #define LSI_CTEST2_CIO 0x20
135 #define LSI_CTEST2_SIGP 0x40
136 #define LSI_CTEST2_DDIR 0x80
138 #define LSI_CTEST5_BL2 0x04
139 #define LSI_CTEST5_DDIR 0x08
140 #define LSI_CTEST5_MASR 0x10
141 #define LSI_CTEST5_DFSN 0x20
142 #define LSI_CTEST5_BBCK 0x40
143 #define LSI_CTEST5_ADCK 0x80
145 #define LSI_CCNTL0_DILS 0x01
146 #define LSI_CCNTL0_DISFC 0x10
147 #define LSI_CCNTL0_ENNDJ 0x20
148 #define LSI_CCNTL0_PMJCTL 0x40
149 #define LSI_CCNTL0_ENPMJ 0x80
151 #define LSI_CCNTL1_EN64DBMV 0x01
152 #define LSI_CCNTL1_EN64TIBMV 0x02
153 #define LSI_CCNTL1_64TIMOD 0x04
154 #define LSI_CCNTL1_DDAC 0x08
155 #define LSI_CCNTL1_ZMOD 0x80
157 /* Enable Response to Reselection */
158 #define LSI_SCID_RRE 0x60
160 #define LSI_CCNTL1_40BIT (LSI_CCNTL1_EN64TIBMV|LSI_CCNTL1_64TIMOD)
170 /* Maximum length of MSG IN data. */
171 #define LSI_MAX_MSGIN_LEN 8
173 /* Flag set if this is a tagged command. */
174 #define LSI_TAG_VALID (1 << 16)
186 uint32_t script_ram_base
;
188 int carry
; /* ??? Should this be an a visible register somewhere? */
190 /* Action to take at the end of a MSG IN phase.
191 0 = COMMAND, 1 = disconnect, 2 = DATA OUT, 3 = DATA IN. */
194 uint8_t msg
[LSI_MAX_MSGIN_LEN
];
195 /* 0 if SCRIPTS are running or stopped.
196 * 1 if a Wait Reselect instruction has been issued.
197 * 2 if processing DMA from lsi_execute_script.
198 * 3 if a DMA operation is in progress. */
201 SCSIDevice
*current_dev
;
203 /* The tag is a combination of the device ID and the SCSI tag. */
204 uint32_t current_tag
;
205 uint32_t current_dma_len
;
206 int command_complete
;
271 uint32_t scratch
[18]; /* SCRATCHA-SCRATCHR */
274 /* Script ram is stored as 32-bit words in host byteorder. */
275 uint32_t script_ram
[2048];
278 static inline int lsi_irq_on_rsl(LSIState
*s
)
280 return (s
->sien0
& LSI_SIST0_RSL
) && (s
->scid
& LSI_SCID_RRE
);
283 static void lsi_soft_reset(LSIState
*s
)
293 memset(s
->scratch
, 0, sizeof(s
->scratch
));
349 static int lsi_dma_40bit(LSIState
*s
)
351 if ((s
->ccntl1
& LSI_CCNTL1_40BIT
) == LSI_CCNTL1_40BIT
)
356 static int lsi_dma_ti64bit(LSIState
*s
)
358 if ((s
->ccntl1
& LSI_CCNTL1_EN64TIBMV
) == LSI_CCNTL1_EN64TIBMV
)
363 static int lsi_dma_64bit(LSIState
*s
)
365 if ((s
->ccntl1
& LSI_CCNTL1_EN64DBMV
) == LSI_CCNTL1_EN64DBMV
)
370 static uint8_t lsi_reg_readb(LSIState
*s
, int offset
);
371 static void lsi_reg_writeb(LSIState
*s
, int offset
, uint8_t val
);
372 static void lsi_execute_script(LSIState
*s
);
373 static void lsi_reselect(LSIState
*s
, uint32_t tag
);
375 static inline uint32_t read_dword(LSIState
*s
, uint32_t addr
)
379 /* Optimize reading from SCRIPTS RAM. */
380 if ((addr
& 0xffffe000) == s
->script_ram_base
) {
381 return s
->script_ram
[(addr
& 0x1fff) >> 2];
383 cpu_physical_memory_read(addr
, (uint8_t *)&buf
, 4);
384 return cpu_to_le32(buf
);
387 static void lsi_stop_script(LSIState
*s
)
389 s
->istat1
&= ~LSI_ISTAT1_SRUN
;
392 static void lsi_update_irq(LSIState
*s
)
396 static int last_level
;
398 /* It's unclear whether the DIP/SIP bits should be cleared when the
399 Interrupt Status Registers are cleared or when istat0 is read.
400 We currently do the formwer, which seems to work. */
403 if (s
->dstat
& s
->dien
)
405 s
->istat0
|= LSI_ISTAT0_DIP
;
407 s
->istat0
&= ~LSI_ISTAT0_DIP
;
410 if (s
->sist0
|| s
->sist1
) {
411 if ((s
->sist0
& s
->sien0
) || (s
->sist1
& s
->sien1
))
413 s
->istat0
|= LSI_ISTAT0_SIP
;
415 s
->istat0
&= ~LSI_ISTAT0_SIP
;
417 if (s
->istat0
& LSI_ISTAT0_INTF
)
420 if (level
!= last_level
) {
421 DPRINTF("Update IRQ level %d dstat %02x sist %02x%02x\n",
422 level
, s
->dstat
, s
->sist1
, s
->sist0
);
425 qemu_set_irq(s
->dev
.irq
[0], level
);
427 if (!level
&& lsi_irq_on_rsl(s
) && !(s
->scntl1
& LSI_SCNTL1_CON
)) {
428 DPRINTF("Handled IRQs & disconnected, looking for pending "
430 for (i
= 0; i
< s
->active_commands
; i
++) {
431 if (s
->queue
[i
].pending
) {
432 lsi_reselect(s
, s
->queue
[i
].tag
);
439 /* Stop SCRIPTS execution and raise a SCSI interrupt. */
440 static void lsi_script_scsi_interrupt(LSIState
*s
, int stat0
, int stat1
)
445 DPRINTF("SCSI Interrupt 0x%02x%02x prev 0x%02x%02x\n",
446 stat1
, stat0
, s
->sist1
, s
->sist0
);
449 /* Stop processor on fatal or unmasked interrupt. As a special hack
450 we don't stop processing when raising STO. Instead continue
451 execution and stop at the next insn that accesses the SCSI bus. */
452 mask0
= s
->sien0
| ~(LSI_SIST0_CMP
| LSI_SIST0_SEL
| LSI_SIST0_RSL
);
453 mask1
= s
->sien1
| ~(LSI_SIST1_GEN
| LSI_SIST1_HTH
);
454 mask1
&= ~LSI_SIST1_STO
;
455 if (s
->sist0
& mask0
|| s
->sist1
& mask1
) {
461 /* Stop SCRIPTS execution and raise a DMA interrupt. */
462 static void lsi_script_dma_interrupt(LSIState
*s
, int stat
)
464 DPRINTF("DMA Interrupt 0x%x prev 0x%x\n", stat
, s
->dstat
);
470 static inline void lsi_set_phase(LSIState
*s
, int phase
)
472 s
->sstat1
= (s
->sstat1
& ~PHASE_MASK
) | phase
;
475 static void lsi_bad_phase(LSIState
*s
, int out
, int new_phase
)
477 /* Trigger a phase mismatch. */
478 if (s
->ccntl0
& LSI_CCNTL0_ENPMJ
) {
479 if ((s
->ccntl0
& LSI_CCNTL0_PMJCTL
) || out
) {
484 DPRINTF("Data phase mismatch jump to %08x\n", s
->dsp
);
486 DPRINTF("Phase mismatch interrupt\n");
487 lsi_script_scsi_interrupt(s
, LSI_SIST0_MA
, 0);
490 lsi_set_phase(s
, new_phase
);
494 /* Resume SCRIPTS execution after a DMA operation. */
495 static void lsi_resume_script(LSIState
*s
)
497 if (s
->waiting
!= 2) {
499 lsi_execute_script(s
);
505 /* Initiate a SCSI layer data transfer. */
506 static void lsi_do_dma(LSIState
*s
, int out
)
509 target_phys_addr_t addr
;
511 if (!s
->current_dma_len
) {
512 /* Wait until data is available. */
513 DPRINTF("DMA no data available\n");
518 if (count
> s
->current_dma_len
)
519 count
= s
->current_dma_len
;
522 /* both 40 and Table Indirect 64-bit DMAs store upper bits in dnad64 */
523 if (lsi_dma_40bit(s
) || lsi_dma_ti64bit(s
))
524 addr
|= ((uint64_t)s
->dnad64
<< 32);
526 addr
|= ((uint64_t)s
->dbms
<< 32);
528 addr
|= ((uint64_t)s
->sbms
<< 32);
530 DPRINTF("DMA addr=0x" TARGET_FMT_plx
" len=%d\n", addr
, count
);
535 if (s
->dma_buf
== NULL
) {
536 s
->dma_buf
= s
->current_dev
->info
->get_buf(s
->current_dev
,
540 /* ??? Set SFBR to first data byte. */
542 cpu_physical_memory_read(addr
, s
->dma_buf
, count
);
544 cpu_physical_memory_write(addr
, s
->dma_buf
, count
);
546 s
->current_dma_len
-= count
;
547 if (s
->current_dma_len
== 0) {
550 /* Write the data. */
551 s
->current_dev
->info
->write_data(s
->current_dev
, s
->current_tag
);
553 /* Request any remaining data. */
554 s
->current_dev
->info
->read_data(s
->current_dev
, s
->current_tag
);
558 lsi_resume_script(s
);
563 /* Add a command to the queue. */
564 static void lsi_queue_command(LSIState
*s
)
568 DPRINTF("Queueing tag=0x%x\n", s
->current_tag
);
569 if (s
->queue_len
== s
->active_commands
) {
571 s
->queue
= qemu_realloc(s
->queue
, s
->queue_len
* sizeof(lsi_queue
));
573 p
= &s
->queue
[s
->active_commands
++];
574 p
->tag
= s
->current_tag
;
576 p
->out
= (s
->sstat1
& PHASE_MASK
) == PHASE_DO
;
579 /* Queue a byte for a MSG IN phase. */
580 static void lsi_add_msg_byte(LSIState
*s
, uint8_t data
)
582 if (s
->msg_len
>= LSI_MAX_MSGIN_LEN
) {
583 BADF("MSG IN data too long\n");
585 DPRINTF("MSG IN 0x%02x\n", data
);
586 s
->msg
[s
->msg_len
++] = data
;
590 /* Perform reselection to continue a command. */
591 static void lsi_reselect(LSIState
*s
, uint32_t tag
)
598 for (n
= 0; n
< s
->active_commands
; n
++) {
603 if (n
== s
->active_commands
) {
604 BADF("Reselected non-existant command tag=0x%x\n", tag
);
607 id
= (tag
>> 8) & 0xf;
609 /* LSI53C700 Family Compatibility, see LSI53C895A 4-73 */
610 if (!s
->dcntl
& LSI_DCNTL_COM
) {
611 s
->sfbr
= 1 << (id
& 0x7);
613 DPRINTF("Reselected target %d\n", id
);
614 s
->current_dev
= s
->bus
.devs
[id
];
615 s
->current_tag
= tag
;
616 s
->scntl1
|= LSI_SCNTL1_CON
;
617 lsi_set_phase(s
, PHASE_MI
);
618 s
->msg_action
= p
->out
? 2 : 3;
619 s
->current_dma_len
= p
->pending
;
621 lsi_add_msg_byte(s
, 0x80);
622 if (s
->current_tag
& LSI_TAG_VALID
) {
623 lsi_add_msg_byte(s
, 0x20);
624 lsi_add_msg_byte(s
, tag
& 0xff);
627 s
->active_commands
--;
628 if (n
!= s
->active_commands
) {
629 s
->queue
[n
] = s
->queue
[s
->active_commands
];
632 if (lsi_irq_on_rsl(s
)) {
633 lsi_script_scsi_interrupt(s
, LSI_SIST0_RSL
, 0);
637 /* Record that data is available for a queued command. Returns zero if
638 the device was reselected, nonzero if the IO is deferred. */
639 static int lsi_queue_tag(LSIState
*s
, uint32_t tag
, uint32_t arg
)
643 for (i
= 0; i
< s
->active_commands
; i
++) {
647 BADF("Multiple IO pending for tag %d\n", tag
);
650 /* Reselect if waiting for it, or if reselection triggers an IRQ
652 Since no interrupt stacking is implemented in the emulation, it
653 is also required that there are no pending interrupts waiting
654 for service from the device driver. */
655 if (s
->waiting
== 1 ||
656 (lsi_irq_on_rsl(s
) && !(s
->scntl1
& LSI_SCNTL1_CON
) &&
657 !(s
->istat0
& (LSI_ISTAT0_SIP
| LSI_ISTAT0_DIP
)))) {
658 /* Reselect device. */
659 lsi_reselect(s
, tag
);
662 DPRINTF("Queueing IO tag=0x%x\n", tag
);
668 BADF("IO with unknown tag %d\n", tag
);
672 /* Callback to indicate that the SCSI layer has completed a transfer. */
673 static void lsi_command_complete(SCSIBus
*bus
, int reason
, uint32_t tag
,
676 LSIState
*s
= DO_UPCAST(LSIState
, dev
.qdev
, bus
->qbus
.parent
);
679 out
= (s
->sstat1
& PHASE_MASK
) == PHASE_DO
;
680 if (reason
== SCSI_REASON_DONE
) {
681 DPRINTF("Command complete sense=%d\n", (int)arg
);
683 s
->command_complete
= 2;
684 if (s
->waiting
&& s
->dbc
!= 0) {
685 /* Raise phase mismatch for short transfers. */
686 lsi_bad_phase(s
, out
, PHASE_ST
);
688 lsi_set_phase(s
, PHASE_ST
);
690 lsi_resume_script(s
);
694 if (s
->waiting
== 1 || tag
!= s
->current_tag
||
695 (lsi_irq_on_rsl(s
) && !(s
->scntl1
& LSI_SCNTL1_CON
))) {
696 if (lsi_queue_tag(s
, tag
, arg
))
700 /* host adapter (re)connected */
701 DPRINTF("Data ready tag=0x%x len=%d\n", tag
, arg
);
702 s
->current_dma_len
= arg
;
703 s
->command_complete
= 1;
706 if (s
->waiting
== 1 || s
->dbc
== 0) {
707 lsi_resume_script(s
);
713 static void lsi_do_command(LSIState
*s
)
718 DPRINTF("Send command len=%d\n", s
->dbc
);
721 cpu_physical_memory_read(s
->dnad
, buf
, s
->dbc
);
723 s
->command_complete
= 0;
724 n
= s
->current_dev
->info
->send_command(s
->current_dev
, s
->current_tag
, buf
,
727 lsi_set_phase(s
, PHASE_DI
);
728 s
->current_dev
->info
->read_data(s
->current_dev
, s
->current_tag
);
730 lsi_set_phase(s
, PHASE_DO
);
731 s
->current_dev
->info
->write_data(s
->current_dev
, s
->current_tag
);
734 if (!s
->command_complete
) {
736 /* Command did not complete immediately so disconnect. */
737 lsi_add_msg_byte(s
, 2); /* SAVE DATA POINTER */
738 lsi_add_msg_byte(s
, 4); /* DISCONNECT */
740 lsi_set_phase(s
, PHASE_MI
);
742 lsi_queue_command(s
);
744 /* wait command complete */
745 lsi_set_phase(s
, PHASE_DI
);
750 static void lsi_do_status(LSIState
*s
)
753 DPRINTF("Get status len=%d sense=%d\n", s
->dbc
, s
->sense
);
755 BADF("Bad Status move\n");
759 cpu_physical_memory_write(s
->dnad
, &sense
, 1);
760 lsi_set_phase(s
, PHASE_MI
);
762 lsi_add_msg_byte(s
, 0); /* COMMAND COMPLETE */
765 static void lsi_disconnect(LSIState
*s
)
767 s
->scntl1
&= ~LSI_SCNTL1_CON
;
768 s
->sstat1
&= ~PHASE_MASK
;
771 static void lsi_do_msgin(LSIState
*s
)
774 DPRINTF("Message in len=%d/%d\n", s
->dbc
, s
->msg_len
);
779 cpu_physical_memory_write(s
->dnad
, s
->msg
, len
);
780 /* Linux drivers rely on the last byte being in the SIDL. */
781 s
->sidl
= s
->msg
[len
- 1];
784 memmove(s
->msg
, s
->msg
+ len
, s
->msg_len
);
786 /* ??? Check if ATN (not yet implemented) is asserted and maybe
787 switch to PHASE_MO. */
788 switch (s
->msg_action
) {
790 lsi_set_phase(s
, PHASE_CMD
);
796 lsi_set_phase(s
, PHASE_DO
);
799 lsi_set_phase(s
, PHASE_DI
);
807 /* Read the next byte during a MSGOUT phase. */
808 static uint8_t lsi_get_msgbyte(LSIState
*s
)
811 cpu_physical_memory_read(s
->dnad
, &data
, 1);
817 static void lsi_do_msgout(LSIState
*s
)
822 DPRINTF("MSG out len=%d\n", s
->dbc
);
824 msg
= lsi_get_msgbyte(s
);
829 DPRINTF("MSG: Disconnect\n");
833 DPRINTF("MSG: No Operation\n");
834 lsi_set_phase(s
, PHASE_CMD
);
837 len
= lsi_get_msgbyte(s
);
838 msg
= lsi_get_msgbyte(s
);
839 DPRINTF("Extended message 0x%x (len %d)\n", msg
, len
);
842 DPRINTF("SDTR (ignored)\n");
846 DPRINTF("WDTR (ignored)\n");
853 case 0x20: /* SIMPLE queue */
854 s
->current_tag
|= lsi_get_msgbyte(s
) | LSI_TAG_VALID
;
855 DPRINTF("SIMPLE queue tag=0x%x\n", s
->current_tag
& 0xff);
857 case 0x21: /* HEAD of queue */
858 BADF("HEAD queue not implemented\n");
859 s
->current_tag
|= lsi_get_msgbyte(s
) | LSI_TAG_VALID
;
861 case 0x22: /* ORDERED queue */
862 BADF("ORDERED queue not implemented\n");
863 s
->current_tag
|= lsi_get_msgbyte(s
) | LSI_TAG_VALID
;
866 if ((msg
& 0x80) == 0) {
869 s
->current_lun
= msg
& 7;
870 DPRINTF("Select LUN %d\n", s
->current_lun
);
871 lsi_set_phase(s
, PHASE_CMD
);
877 BADF("Unimplemented message 0x%02x\n", msg
);
878 lsi_set_phase(s
, PHASE_MI
);
879 lsi_add_msg_byte(s
, 7); /* MESSAGE REJECT */
883 /* Sign extend a 24-bit value. */
884 static inline int32_t sxt24(int32_t n
)
886 return (n
<< 8) >> 8;
889 #define LSI_BUF_SIZE 4096
890 static void lsi_memcpy(LSIState
*s
, uint32_t dest
, uint32_t src
, int count
)
893 uint8_t buf
[LSI_BUF_SIZE
];
895 DPRINTF("memcpy dest 0x%08x src 0x%08x count %d\n", dest
, src
, count
);
897 n
= (count
> LSI_BUF_SIZE
) ? LSI_BUF_SIZE
: count
;
898 cpu_physical_memory_read(src
, buf
, n
);
899 cpu_physical_memory_write(dest
, buf
, n
);
906 static void lsi_wait_reselect(LSIState
*s
)
909 DPRINTF("Wait Reselect\n");
910 if (s
->current_dma_len
)
911 BADF("Reselect with pending DMA\n");
912 for (i
= 0; i
< s
->active_commands
; i
++) {
913 if (s
->queue
[i
].pending
) {
914 lsi_reselect(s
, s
->queue
[i
].tag
);
918 if (s
->current_dma_len
== 0) {
923 static void lsi_execute_script(LSIState
*s
)
926 uint32_t addr
, addr_high
;
928 int insn_processed
= 0;
930 s
->istat1
|= LSI_ISTAT1_SRUN
;
933 insn
= read_dword(s
, s
->dsp
);
935 /* If we receive an empty opcode increment the DSP by 4 bytes
936 instead of 8 and execute the next opcode at that location */
940 addr
= read_dword(s
, s
->dsp
+ 4);
942 DPRINTF("SCRIPTS dsp=%08x opcode %08x arg %08x\n", s
->dsp
, insn
, addr
);
944 s
->dcmd
= insn
>> 24;
946 switch (insn
>> 30) {
947 case 0: /* Block move. */
948 if (s
->sist1
& LSI_SIST1_STO
) {
949 DPRINTF("Delayed select timeout\n");
953 s
->dbc
= insn
& 0xffffff;
957 if (insn
& (1 << 29)) {
958 /* Indirect addressing. */
959 addr
= read_dword(s
, addr
);
960 } else if (insn
& (1 << 28)) {
963 /* Table indirect addressing. */
965 /* 32-bit Table indirect */
966 offset
= sxt24(addr
);
967 cpu_physical_memory_read(s
->dsa
+ offset
, (uint8_t *)buf
, 8);
968 /* byte count is stored in bits 0:23 only */
969 s
->dbc
= cpu_to_le32(buf
[0]) & 0xffffff;
971 addr
= cpu_to_le32(buf
[1]);
973 /* 40-bit DMA, upper addr bits [39:32] stored in first DWORD of
974 * table, bits [31:24] */
975 if (lsi_dma_40bit(s
))
976 addr_high
= cpu_to_le32(buf
[0]) >> 24;
977 else if (lsi_dma_ti64bit(s
)) {
978 int selector
= (cpu_to_le32(buf
[0]) >> 24) & 0x1f;
981 /* offset index into scratch registers since
982 * TI64 mode can use registers C to R */
983 addr_high
= s
->scratch
[2 + selector
];
1001 addr_high
= s
->dbms
;
1004 BADF("Illegal selector specified (0x%x > 0x15)"
1005 " for 64-bit DMA block move", selector
);
1009 } else if (lsi_dma_64bit(s
)) {
1010 /* fetch a 3rd dword if 64-bit direct move is enabled and
1011 only if we're not doing table indirect or indirect addressing */
1012 s
->dbms
= read_dword(s
, s
->dsp
);
1014 s
->ia
= s
->dsp
- 12;
1016 if ((s
->sstat1
& PHASE_MASK
) != ((insn
>> 24) & 7)) {
1017 DPRINTF("Wrong phase got %d expected %d\n",
1018 s
->sstat1
& PHASE_MASK
, (insn
>> 24) & 7);
1019 lsi_script_scsi_interrupt(s
, LSI_SIST0_MA
, 0);
1023 s
->dnad64
= addr_high
;
1024 switch (s
->sstat1
& 0x7) {
1050 BADF("Unimplemented phase %d\n", s
->sstat1
& PHASE_MASK
);
1053 s
->dfifo
= s
->dbc
& 0xff;
1054 s
->ctest5
= (s
->ctest5
& 0xfc) | ((s
->dbc
>> 8) & 3);
1057 s
->ua
= addr
+ s
->dbc
;
1060 case 1: /* IO or Read/Write instruction. */
1061 opcode
= (insn
>> 27) & 7;
1065 if (insn
& (1 << 25)) {
1066 id
= read_dword(s
, s
->dsa
+ sxt24(insn
));
1070 id
= (id
>> 16) & 0xf;
1071 if (insn
& (1 << 26)) {
1072 addr
= s
->dsp
+ sxt24(addr
);
1076 case 0: /* Select */
1078 if (s
->scntl1
& LSI_SCNTL1_CON
) {
1079 DPRINTF("Already reselected, jumping to alternative address\n");
1083 s
->sstat0
|= LSI_SSTAT0_WOA
;
1084 s
->scntl1
&= ~LSI_SCNTL1_IARB
;
1085 if (id
>= LSI_MAX_DEVS
|| !s
->bus
.devs
[id
]) {
1086 DPRINTF("Selected absent target %d\n", id
);
1087 lsi_script_scsi_interrupt(s
, 0, LSI_SIST1_STO
);
1091 DPRINTF("Selected target %d%s\n",
1092 id
, insn
& (1 << 3) ? " ATN" : "");
1093 /* ??? Linux drivers compain when this is set. Maybe
1094 it only applies in low-level mode (unimplemented).
1095 lsi_script_scsi_interrupt(s, LSI_SIST0_CMP, 0); */
1096 s
->current_dev
= s
->bus
.devs
[id
];
1097 s
->current_tag
= id
<< 8;
1098 s
->scntl1
|= LSI_SCNTL1_CON
;
1099 if (insn
& (1 << 3)) {
1100 s
->socl
|= LSI_SOCL_ATN
;
1102 lsi_set_phase(s
, PHASE_MO
);
1104 case 1: /* Disconnect */
1105 DPRINTF("Wait Disconnect\n");
1106 s
->scntl1
&= ~LSI_SCNTL1_CON
;
1108 case 2: /* Wait Reselect */
1109 if (!lsi_irq_on_rsl(s
)) {
1110 lsi_wait_reselect(s
);
1114 DPRINTF("Set%s%s%s%s\n",
1115 insn
& (1 << 3) ? " ATN" : "",
1116 insn
& (1 << 6) ? " ACK" : "",
1117 insn
& (1 << 9) ? " TM" : "",
1118 insn
& (1 << 10) ? " CC" : "");
1119 if (insn
& (1 << 3)) {
1120 s
->socl
|= LSI_SOCL_ATN
;
1121 lsi_set_phase(s
, PHASE_MO
);
1123 if (insn
& (1 << 9)) {
1124 BADF("Target mode not implemented\n");
1127 if (insn
& (1 << 10))
1131 DPRINTF("Clear%s%s%s%s\n",
1132 insn
& (1 << 3) ? " ATN" : "",
1133 insn
& (1 << 6) ? " ACK" : "",
1134 insn
& (1 << 9) ? " TM" : "",
1135 insn
& (1 << 10) ? " CC" : "");
1136 if (insn
& (1 << 3)) {
1137 s
->socl
&= ~LSI_SOCL_ATN
;
1139 if (insn
& (1 << 10))
1150 static const char *opcode_names
[3] =
1151 {"Write", "Read", "Read-Modify-Write"};
1152 static const char *operator_names
[8] =
1153 {"MOV", "SHL", "OR", "XOR", "AND", "SHR", "ADD", "ADC"};
1156 reg
= ((insn
>> 16) & 0x7f) | (insn
& 0x80);
1157 data8
= (insn
>> 8) & 0xff;
1158 opcode
= (insn
>> 27) & 7;
1159 operator = (insn
>> 24) & 7;
1160 DPRINTF("%s reg 0x%x %s data8=0x%02x sfbr=0x%02x%s\n",
1161 opcode_names
[opcode
- 5], reg
,
1162 operator_names
[operator], data8
, s
->sfbr
,
1163 (insn
& (1 << 23)) ? " SFBR" : "");
1166 case 5: /* From SFBR */
1170 case 6: /* To SFBR */
1172 op0
= lsi_reg_readb(s
, reg
);
1175 case 7: /* Read-modify-write */
1177 op0
= lsi_reg_readb(s
, reg
);
1178 if (insn
& (1 << 23)) {
1190 case 1: /* Shift left */
1192 op0
= (op0
<< 1) | s
->carry
;
1206 op0
= (op0
>> 1) | (s
->carry
<< 7);
1211 s
->carry
= op0
< op1
;
1214 op0
+= op1
+ s
->carry
;
1216 s
->carry
= op0
<= op1
;
1218 s
->carry
= op0
< op1
;
1223 case 5: /* From SFBR */
1224 case 7: /* Read-modify-write */
1225 lsi_reg_writeb(s
, reg
, op0
);
1227 case 6: /* To SFBR */
1234 case 2: /* Transfer Control. */
1239 if ((insn
& 0x002e0000) == 0) {
1243 if (s
->sist1
& LSI_SIST1_STO
) {
1244 DPRINTF("Delayed select timeout\n");
1248 cond
= jmp
= (insn
& (1 << 19)) != 0;
1249 if (cond
== jmp
&& (insn
& (1 << 21))) {
1250 DPRINTF("Compare carry %d\n", s
->carry
== jmp
);
1251 cond
= s
->carry
!= 0;
1253 if (cond
== jmp
&& (insn
& (1 << 17))) {
1254 DPRINTF("Compare phase %d %c= %d\n",
1255 (s
->sstat1
& PHASE_MASK
),
1257 ((insn
>> 24) & 7));
1258 cond
= (s
->sstat1
& PHASE_MASK
) == ((insn
>> 24) & 7);
1260 if (cond
== jmp
&& (insn
& (1 << 18))) {
1263 mask
= (~insn
>> 8) & 0xff;
1264 DPRINTF("Compare data 0x%x & 0x%x %c= 0x%x\n",
1265 s
->sfbr
, mask
, jmp
? '=' : '!', insn
& mask
);
1266 cond
= (s
->sfbr
& mask
) == (insn
& mask
);
1269 if (insn
& (1 << 23)) {
1270 /* Relative address. */
1271 addr
= s
->dsp
+ sxt24(addr
);
1273 switch ((insn
>> 27) & 7) {
1275 DPRINTF("Jump to 0x%08x\n", addr
);
1279 DPRINTF("Call 0x%08x\n", addr
);
1283 case 2: /* Return */
1284 DPRINTF("Return to 0x%08x\n", s
->temp
);
1287 case 3: /* Interrupt */
1288 DPRINTF("Interrupt 0x%08x\n", s
->dsps
);
1289 if ((insn
& (1 << 20)) != 0) {
1290 s
->istat0
|= LSI_ISTAT0_INTF
;
1293 lsi_script_dma_interrupt(s
, LSI_DSTAT_SIR
);
1297 DPRINTF("Illegal transfer control\n");
1298 lsi_script_dma_interrupt(s
, LSI_DSTAT_IID
);
1302 DPRINTF("Control condition failed\n");
1308 if ((insn
& (1 << 29)) == 0) {
1311 /* ??? The docs imply the destination address is loaded into
1312 the TEMP register. However the Linux drivers rely on
1313 the value being presrved. */
1314 dest
= read_dword(s
, s
->dsp
);
1316 lsi_memcpy(s
, dest
, addr
, insn
& 0xffffff);
1323 if (insn
& (1 << 28)) {
1324 addr
= s
->dsa
+ sxt24(addr
);
1327 reg
= (insn
>> 16) & 0xff;
1328 if (insn
& (1 << 24)) {
1329 cpu_physical_memory_read(addr
, data
, n
);
1330 DPRINTF("Load reg 0x%x size %d addr 0x%08x = %08x\n", reg
, n
,
1331 addr
, *(int *)data
);
1332 for (i
= 0; i
< n
; i
++) {
1333 lsi_reg_writeb(s
, reg
+ i
, data
[i
]);
1336 DPRINTF("Store reg 0x%x size %d addr 0x%08x\n", reg
, n
, addr
);
1337 for (i
= 0; i
< n
; i
++) {
1338 data
[i
] = lsi_reg_readb(s
, reg
+ i
);
1340 cpu_physical_memory_write(addr
, data
, n
);
1344 if (insn_processed
> 10000 && !s
->waiting
) {
1345 /* Some windows drivers make the device spin waiting for a memory
1346 location to change. If we have been executed a lot of code then
1347 assume this is the case and force an unexpected device disconnect.
1348 This is apparently sufficient to beat the drivers into submission.
1350 if (!(s
->sien0
& LSI_SIST0_UDC
))
1351 fprintf(stderr
, "inf. loop with UDC masked\n");
1352 lsi_script_scsi_interrupt(s
, LSI_SIST0_UDC
, 0);
1354 } else if (s
->istat1
& LSI_ISTAT1_SRUN
&& !s
->waiting
) {
1355 if (s
->dcntl
& LSI_DCNTL_SSM
) {
1356 lsi_script_dma_interrupt(s
, LSI_DSTAT_SSI
);
1361 DPRINTF("SCRIPTS execution stopped\n");
1364 static uint8_t lsi_reg_readb(LSIState
*s
, int offset
)
1367 #define CASE_GET_REG24(name, addr) \
1368 case addr: return s->name & 0xff; \
1369 case addr + 1: return (s->name >> 8) & 0xff; \
1370 case addr + 2: return (s->name >> 16) & 0xff;
1372 #define CASE_GET_REG32(name, addr) \
1373 case addr: return s->name & 0xff; \
1374 case addr + 1: return (s->name >> 8) & 0xff; \
1375 case addr + 2: return (s->name >> 16) & 0xff; \
1376 case addr + 3: return (s->name >> 24) & 0xff;
1378 #ifdef DEBUG_LSI_REG
1379 DPRINTF("Read reg %x\n", offset
);
1382 case 0x00: /* SCNTL0 */
1384 case 0x01: /* SCNTL1 */
1386 case 0x02: /* SCNTL2 */
1388 case 0x03: /* SCNTL3 */
1390 case 0x04: /* SCID */
1392 case 0x05: /* SXFER */
1394 case 0x06: /* SDID */
1396 case 0x07: /* GPREG0 */
1398 case 0x08: /* Revision ID */
1400 case 0xa: /* SSID */
1402 case 0xb: /* SBCL */
1403 /* ??? This is not correct. However it's (hopefully) only
1404 used for diagnostics, so should be ok. */
1406 case 0xc: /* DSTAT */
1407 tmp
= s
->dstat
| 0x80;
1408 if ((s
->istat0
& LSI_ISTAT0_INTF
) == 0)
1412 case 0x0d: /* SSTAT0 */
1414 case 0x0e: /* SSTAT1 */
1416 case 0x0f: /* SSTAT2 */
1417 return s
->scntl1
& LSI_SCNTL1_CON
? 0 : 2;
1418 CASE_GET_REG32(dsa
, 0x10)
1419 case 0x14: /* ISTAT0 */
1421 case 0x15: /* ISTAT1 */
1423 case 0x16: /* MBOX0 */
1425 case 0x17: /* MBOX1 */
1427 case 0x18: /* CTEST0 */
1429 case 0x19: /* CTEST1 */
1431 case 0x1a: /* CTEST2 */
1432 tmp
= s
->ctest2
| LSI_CTEST2_DACK
| LSI_CTEST2_CM
;
1433 if (s
->istat0
& LSI_ISTAT0_SIGP
) {
1434 s
->istat0
&= ~LSI_ISTAT0_SIGP
;
1435 tmp
|= LSI_CTEST2_SIGP
;
1438 case 0x1b: /* CTEST3 */
1440 CASE_GET_REG32(temp
, 0x1c)
1441 case 0x20: /* DFIFO */
1443 case 0x21: /* CTEST4 */
1445 case 0x22: /* CTEST5 */
1447 case 0x23: /* CTEST6 */
1449 CASE_GET_REG24(dbc
, 0x24)
1450 case 0x27: /* DCMD */
1452 CASE_GET_REG32(dnad
, 0x28)
1453 CASE_GET_REG32(dsp
, 0x2c)
1454 CASE_GET_REG32(dsps
, 0x30)
1455 CASE_GET_REG32(scratch
[0], 0x34)
1456 case 0x38: /* DMODE */
1458 case 0x39: /* DIEN */
1460 case 0x3a: /* SBR */
1462 case 0x3b: /* DCNTL */
1464 case 0x40: /* SIEN0 */
1466 case 0x41: /* SIEN1 */
1468 case 0x42: /* SIST0 */
1473 case 0x43: /* SIST1 */
1478 case 0x46: /* MACNTL */
1480 case 0x47: /* GPCNTL0 */
1482 case 0x48: /* STIME0 */
1484 case 0x4a: /* RESPID0 */
1486 case 0x4b: /* RESPID1 */
1488 case 0x4d: /* STEST1 */
1490 case 0x4e: /* STEST2 */
1492 case 0x4f: /* STEST3 */
1494 case 0x50: /* SIDL */
1495 /* This is needed by the linux drivers. We currently only update it
1496 during the MSG IN phase. */
1498 case 0x52: /* STEST4 */
1500 case 0x56: /* CCNTL0 */
1502 case 0x57: /* CCNTL1 */
1504 case 0x58: /* SBDL */
1505 /* Some drivers peek at the data bus during the MSG IN phase. */
1506 if ((s
->sstat1
& PHASE_MASK
) == PHASE_MI
)
1509 case 0x59: /* SBDL high */
1511 CASE_GET_REG32(mmrs
, 0xa0)
1512 CASE_GET_REG32(mmws
, 0xa4)
1513 CASE_GET_REG32(sfs
, 0xa8)
1514 CASE_GET_REG32(drs
, 0xac)
1515 CASE_GET_REG32(sbms
, 0xb0)
1516 CASE_GET_REG32(dbms
, 0xb4)
1517 CASE_GET_REG32(dnad64
, 0xb8)
1518 CASE_GET_REG32(pmjad1
, 0xc0)
1519 CASE_GET_REG32(pmjad2
, 0xc4)
1520 CASE_GET_REG32(rbc
, 0xc8)
1521 CASE_GET_REG32(ua
, 0xcc)
1522 CASE_GET_REG32(ia
, 0xd4)
1523 CASE_GET_REG32(sbc
, 0xd8)
1524 CASE_GET_REG32(csbc
, 0xdc)
1526 if (offset
>= 0x5c && offset
< 0xa0) {
1529 n
= (offset
- 0x58) >> 2;
1530 shift
= (offset
& 3) * 8;
1531 return (s
->scratch
[n
] >> shift
) & 0xff;
1533 BADF("readb 0x%x\n", offset
);
1535 #undef CASE_GET_REG24
1536 #undef CASE_GET_REG32
1539 static void lsi_reg_writeb(LSIState
*s
, int offset
, uint8_t val
)
1541 #define CASE_SET_REG24(name, addr) \
1542 case addr : s->name &= 0xffffff00; s->name |= val; break; \
1543 case addr + 1: s->name &= 0xffff00ff; s->name |= val << 8; break; \
1544 case addr + 2: s->name &= 0xff00ffff; s->name |= val << 16; break;
1546 #define CASE_SET_REG32(name, addr) \
1547 case addr : s->name &= 0xffffff00; s->name |= val; break; \
1548 case addr + 1: s->name &= 0xffff00ff; s->name |= val << 8; break; \
1549 case addr + 2: s->name &= 0xff00ffff; s->name |= val << 16; break; \
1550 case addr + 3: s->name &= 0x00ffffff; s->name |= val << 24; break;
1552 #ifdef DEBUG_LSI_REG
1553 DPRINTF("Write reg %x = %02x\n", offset
, val
);
1556 case 0x00: /* SCNTL0 */
1558 if (val
& LSI_SCNTL0_START
) {
1559 BADF("Start sequence not implemented\n");
1562 case 0x01: /* SCNTL1 */
1563 s
->scntl1
= val
& ~LSI_SCNTL1_SST
;
1564 if (val
& LSI_SCNTL1_IARB
) {
1565 BADF("Immediate Arbritration not implemented\n");
1567 if (val
& LSI_SCNTL1_RST
) {
1568 s
->sstat0
|= LSI_SSTAT0_RST
;
1569 lsi_script_scsi_interrupt(s
, LSI_SIST0_RST
, 0);
1571 s
->sstat0
&= ~LSI_SSTAT0_RST
;
1574 case 0x02: /* SCNTL2 */
1575 val
&= ~(LSI_SCNTL2_WSR
| LSI_SCNTL2_WSS
);
1578 case 0x03: /* SCNTL3 */
1581 case 0x04: /* SCID */
1584 case 0x05: /* SXFER */
1587 case 0x06: /* SDID */
1588 if ((val
& 0xf) != (s
->ssid
& 0xf))
1589 BADF("Destination ID does not match SSID\n");
1590 s
->sdid
= val
& 0xf;
1592 case 0x07: /* GPREG0 */
1594 case 0x08: /* SFBR */
1595 /* The CPU is not allowed to write to this register. However the
1596 SCRIPTS register move instructions are. */
1599 case 0x0a: case 0x0b:
1600 /* Openserver writes to these readonly registers on startup */
1602 case 0x0c: case 0x0d: case 0x0e: case 0x0f:
1603 /* Linux writes to these readonly registers on startup. */
1605 CASE_SET_REG32(dsa
, 0x10)
1606 case 0x14: /* ISTAT0 */
1607 s
->istat0
= (s
->istat0
& 0x0f) | (val
& 0xf0);
1608 if (val
& LSI_ISTAT0_ABRT
) {
1609 lsi_script_dma_interrupt(s
, LSI_DSTAT_ABRT
);
1611 if (val
& LSI_ISTAT0_INTF
) {
1612 s
->istat0
&= ~LSI_ISTAT0_INTF
;
1615 if (s
->waiting
== 1 && val
& LSI_ISTAT0_SIGP
) {
1616 DPRINTF("Woken by SIGP\n");
1619 lsi_execute_script(s
);
1621 if (val
& LSI_ISTAT0_SRST
) {
1625 case 0x16: /* MBOX0 */
1628 case 0x17: /* MBOX1 */
1631 case 0x1a: /* CTEST2 */
1632 s
->ctest2
= val
& LSI_CTEST2_PCICIE
;
1634 case 0x1b: /* CTEST3 */
1635 s
->ctest3
= val
& 0x0f;
1637 CASE_SET_REG32(temp
, 0x1c)
1638 case 0x21: /* CTEST4 */
1640 BADF("Unimplemented CTEST4-FBL 0x%x\n", val
);
1644 case 0x22: /* CTEST5 */
1645 if (val
& (LSI_CTEST5_ADCK
| LSI_CTEST5_BBCK
)) {
1646 BADF("CTEST5 DMA increment not implemented\n");
1650 CASE_SET_REG24(dbc
, 0x24)
1651 CASE_SET_REG32(dnad
, 0x28)
1652 case 0x2c: /* DSP[0:7] */
1653 s
->dsp
&= 0xffffff00;
1656 case 0x2d: /* DSP[8:15] */
1657 s
->dsp
&= 0xffff00ff;
1660 case 0x2e: /* DSP[16:23] */
1661 s
->dsp
&= 0xff00ffff;
1662 s
->dsp
|= val
<< 16;
1664 case 0x2f: /* DSP[24:31] */
1665 s
->dsp
&= 0x00ffffff;
1666 s
->dsp
|= val
<< 24;
1667 if ((s
->dmode
& LSI_DMODE_MAN
) == 0
1668 && (s
->istat1
& LSI_ISTAT1_SRUN
) == 0)
1669 lsi_execute_script(s
);
1671 CASE_SET_REG32(dsps
, 0x30)
1672 CASE_SET_REG32(scratch
[0], 0x34)
1673 case 0x38: /* DMODE */
1674 if (val
& (LSI_DMODE_SIOM
| LSI_DMODE_DIOM
)) {
1675 BADF("IO mappings not implemented\n");
1679 case 0x39: /* DIEN */
1683 case 0x3a: /* SBR */
1686 case 0x3b: /* DCNTL */
1687 s
->dcntl
= val
& ~(LSI_DCNTL_PFF
| LSI_DCNTL_STD
);
1688 if ((val
& LSI_DCNTL_STD
) && (s
->istat1
& LSI_ISTAT1_SRUN
) == 0)
1689 lsi_execute_script(s
);
1691 case 0x40: /* SIEN0 */
1695 case 0x41: /* SIEN1 */
1699 case 0x47: /* GPCNTL0 */
1701 case 0x48: /* STIME0 */
1704 case 0x49: /* STIME1 */
1706 DPRINTF("General purpose timer not implemented\n");
1707 /* ??? Raising the interrupt immediately seems to be sufficient
1708 to keep the FreeBSD driver happy. */
1709 lsi_script_scsi_interrupt(s
, 0, LSI_SIST1_GEN
);
1712 case 0x4a: /* RESPID0 */
1715 case 0x4b: /* RESPID1 */
1718 case 0x4d: /* STEST1 */
1721 case 0x4e: /* STEST2 */
1723 BADF("Low level mode not implemented\n");
1727 case 0x4f: /* STEST3 */
1729 BADF("SCSI FIFO test mode not implemented\n");
1733 case 0x56: /* CCNTL0 */
1736 case 0x57: /* CCNTL1 */
1739 CASE_SET_REG32(mmrs
, 0xa0)
1740 CASE_SET_REG32(mmws
, 0xa4)
1741 CASE_SET_REG32(sfs
, 0xa8)
1742 CASE_SET_REG32(drs
, 0xac)
1743 CASE_SET_REG32(sbms
, 0xb0)
1744 CASE_SET_REG32(dbms
, 0xb4)
1745 CASE_SET_REG32(dnad64
, 0xb8)
1746 CASE_SET_REG32(pmjad1
, 0xc0)
1747 CASE_SET_REG32(pmjad2
, 0xc4)
1748 CASE_SET_REG32(rbc
, 0xc8)
1749 CASE_SET_REG32(ua
, 0xcc)
1750 CASE_SET_REG32(ia
, 0xd4)
1751 CASE_SET_REG32(sbc
, 0xd8)
1752 CASE_SET_REG32(csbc
, 0xdc)
1754 if (offset
>= 0x5c && offset
< 0xa0) {
1757 n
= (offset
- 0x58) >> 2;
1758 shift
= (offset
& 3) * 8;
1759 s
->scratch
[n
] &= ~(0xff << shift
);
1760 s
->scratch
[n
] |= (val
& 0xff) << shift
;
1762 BADF("Unhandled writeb 0x%x = 0x%x\n", offset
, val
);
1765 #undef CASE_SET_REG24
1766 #undef CASE_SET_REG32
1769 static void lsi_mmio_writeb(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1771 LSIState
*s
= opaque
;
1773 lsi_reg_writeb(s
, addr
& 0xff, val
);
1776 static void lsi_mmio_writew(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1778 LSIState
*s
= opaque
;
1781 lsi_reg_writeb(s
, addr
, val
& 0xff);
1782 lsi_reg_writeb(s
, addr
+ 1, (val
>> 8) & 0xff);
1785 static void lsi_mmio_writel(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1787 LSIState
*s
= opaque
;
1790 lsi_reg_writeb(s
, addr
, val
& 0xff);
1791 lsi_reg_writeb(s
, addr
+ 1, (val
>> 8) & 0xff);
1792 lsi_reg_writeb(s
, addr
+ 2, (val
>> 16) & 0xff);
1793 lsi_reg_writeb(s
, addr
+ 3, (val
>> 24) & 0xff);
1796 static uint32_t lsi_mmio_readb(void *opaque
, target_phys_addr_t addr
)
1798 LSIState
*s
= opaque
;
1800 return lsi_reg_readb(s
, addr
& 0xff);
1803 static uint32_t lsi_mmio_readw(void *opaque
, target_phys_addr_t addr
)
1805 LSIState
*s
= opaque
;
1809 val
= lsi_reg_readb(s
, addr
);
1810 val
|= lsi_reg_readb(s
, addr
+ 1) << 8;
1814 static uint32_t lsi_mmio_readl(void *opaque
, target_phys_addr_t addr
)
1816 LSIState
*s
= opaque
;
1819 val
= lsi_reg_readb(s
, addr
);
1820 val
|= lsi_reg_readb(s
, addr
+ 1) << 8;
1821 val
|= lsi_reg_readb(s
, addr
+ 2) << 16;
1822 val
|= lsi_reg_readb(s
, addr
+ 3) << 24;
1826 static CPUReadMemoryFunc
* const lsi_mmio_readfn
[3] = {
1832 static CPUWriteMemoryFunc
* const lsi_mmio_writefn
[3] = {
1838 static void lsi_ram_writeb(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1840 LSIState
*s
= opaque
;
1845 newval
= s
->script_ram
[addr
>> 2];
1846 shift
= (addr
& 3) * 8;
1847 newval
&= ~(0xff << shift
);
1848 newval
|= val
<< shift
;
1849 s
->script_ram
[addr
>> 2] = newval
;
1852 static void lsi_ram_writew(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1854 LSIState
*s
= opaque
;
1858 newval
= s
->script_ram
[addr
>> 2];
1860 newval
= (newval
& 0xffff) | (val
<< 16);
1862 newval
= (newval
& 0xffff0000) | val
;
1864 s
->script_ram
[addr
>> 2] = newval
;
1868 static void lsi_ram_writel(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1870 LSIState
*s
= opaque
;
1873 s
->script_ram
[addr
>> 2] = val
;
1876 static uint32_t lsi_ram_readb(void *opaque
, target_phys_addr_t addr
)
1878 LSIState
*s
= opaque
;
1882 val
= s
->script_ram
[addr
>> 2];
1883 val
>>= (addr
& 3) * 8;
1887 static uint32_t lsi_ram_readw(void *opaque
, target_phys_addr_t addr
)
1889 LSIState
*s
= opaque
;
1893 val
= s
->script_ram
[addr
>> 2];
1896 return le16_to_cpu(val
);
1899 static uint32_t lsi_ram_readl(void *opaque
, target_phys_addr_t addr
)
1901 LSIState
*s
= opaque
;
1904 return le32_to_cpu(s
->script_ram
[addr
>> 2]);
1907 static CPUReadMemoryFunc
* const lsi_ram_readfn
[3] = {
1913 static CPUWriteMemoryFunc
* const lsi_ram_writefn
[3] = {
1919 static uint32_t lsi_io_readb(void *opaque
, uint32_t addr
)
1921 LSIState
*s
= opaque
;
1922 return lsi_reg_readb(s
, addr
& 0xff);
1925 static uint32_t lsi_io_readw(void *opaque
, uint32_t addr
)
1927 LSIState
*s
= opaque
;
1930 val
= lsi_reg_readb(s
, addr
);
1931 val
|= lsi_reg_readb(s
, addr
+ 1) << 8;
1935 static uint32_t lsi_io_readl(void *opaque
, uint32_t addr
)
1937 LSIState
*s
= opaque
;
1940 val
= lsi_reg_readb(s
, addr
);
1941 val
|= lsi_reg_readb(s
, addr
+ 1) << 8;
1942 val
|= lsi_reg_readb(s
, addr
+ 2) << 16;
1943 val
|= lsi_reg_readb(s
, addr
+ 3) << 24;
1947 static void lsi_io_writeb(void *opaque
, uint32_t addr
, uint32_t val
)
1949 LSIState
*s
= opaque
;
1950 lsi_reg_writeb(s
, addr
& 0xff, val
);
1953 static void lsi_io_writew(void *opaque
, uint32_t addr
, uint32_t val
)
1955 LSIState
*s
= opaque
;
1957 lsi_reg_writeb(s
, addr
, val
& 0xff);
1958 lsi_reg_writeb(s
, addr
+ 1, (val
>> 8) & 0xff);
1961 static void lsi_io_writel(void *opaque
, uint32_t addr
, uint32_t val
)
1963 LSIState
*s
= opaque
;
1965 lsi_reg_writeb(s
, addr
, val
& 0xff);
1966 lsi_reg_writeb(s
, addr
+ 1, (val
>> 8) & 0xff);
1967 lsi_reg_writeb(s
, addr
+ 2, (val
>> 16) & 0xff);
1968 lsi_reg_writeb(s
, addr
+ 3, (val
>> 24) & 0xff);
1971 static void lsi_io_mapfunc(PCIDevice
*pci_dev
, int region_num
,
1972 pcibus_t addr
, pcibus_t size
, int type
)
1974 LSIState
*s
= DO_UPCAST(LSIState
, dev
, pci_dev
);
1976 DPRINTF("Mapping IO at %08"FMT_PCIBUS
"\n", addr
);
1978 register_ioport_write(addr
, 256, 1, lsi_io_writeb
, s
);
1979 register_ioport_read(addr
, 256, 1, lsi_io_readb
, s
);
1980 register_ioport_write(addr
, 256, 2, lsi_io_writew
, s
);
1981 register_ioport_read(addr
, 256, 2, lsi_io_readw
, s
);
1982 register_ioport_write(addr
, 256, 4, lsi_io_writel
, s
);
1983 register_ioport_read(addr
, 256, 4, lsi_io_readl
, s
);
1986 static void lsi_ram_mapfunc(PCIDevice
*pci_dev
, int region_num
,
1987 pcibus_t addr
, pcibus_t size
, int type
)
1989 LSIState
*s
= DO_UPCAST(LSIState
, dev
, pci_dev
);
1991 DPRINTF("Mapping ram at %08"FMT_PCIBUS
"\n", addr
);
1992 s
->script_ram_base
= addr
;
1993 cpu_register_physical_memory(addr
+ 0, 0x2000, s
->ram_io_addr
);
1996 static void lsi_mmio_mapfunc(PCIDevice
*pci_dev
, int region_num
,
1997 pcibus_t addr
, pcibus_t size
, int type
)
1999 LSIState
*s
= DO_UPCAST(LSIState
, dev
, pci_dev
);
2001 DPRINTF("Mapping registers at %08"FMT_PCIBUS
"\n", addr
);
2002 cpu_register_physical_memory(addr
+ 0, 0x400, s
->mmio_io_addr
);
2005 static void lsi_pre_save(void *opaque
)
2007 LSIState
*s
= opaque
;
2009 assert(s
->dma_buf
== NULL
);
2010 assert(s
->current_dma_len
== 0);
2011 assert(s
->active_commands
== 0);
2014 static const VMStateDescription vmstate_lsi_scsi
= {
2017 .minimum_version_id
= 0,
2018 .minimum_version_id_old
= 0,
2019 .pre_save
= lsi_pre_save
,
2020 .fields
= (VMStateField
[]) {
2021 VMSTATE_PCI_DEVICE(dev
, LSIState
),
2023 VMSTATE_INT32(carry
, LSIState
),
2024 VMSTATE_INT32(sense
, LSIState
),
2025 VMSTATE_INT32(msg_action
, LSIState
),
2026 VMSTATE_INT32(msg_len
, LSIState
),
2027 VMSTATE_BUFFER(msg
, LSIState
),
2028 VMSTATE_INT32(waiting
, LSIState
),
2030 VMSTATE_UINT32(dsa
, LSIState
),
2031 VMSTATE_UINT32(temp
, LSIState
),
2032 VMSTATE_UINT32(dnad
, LSIState
),
2033 VMSTATE_UINT32(dbc
, LSIState
),
2034 VMSTATE_UINT8(istat0
, LSIState
),
2035 VMSTATE_UINT8(istat1
, LSIState
),
2036 VMSTATE_UINT8(dcmd
, LSIState
),
2037 VMSTATE_UINT8(dstat
, LSIState
),
2038 VMSTATE_UINT8(dien
, LSIState
),
2039 VMSTATE_UINT8(sist0
, LSIState
),
2040 VMSTATE_UINT8(sist1
, LSIState
),
2041 VMSTATE_UINT8(sien0
, LSIState
),
2042 VMSTATE_UINT8(sien1
, LSIState
),
2043 VMSTATE_UINT8(mbox0
, LSIState
),
2044 VMSTATE_UINT8(mbox1
, LSIState
),
2045 VMSTATE_UINT8(dfifo
, LSIState
),
2046 VMSTATE_UINT8(ctest2
, LSIState
),
2047 VMSTATE_UINT8(ctest3
, LSIState
),
2048 VMSTATE_UINT8(ctest4
, LSIState
),
2049 VMSTATE_UINT8(ctest5
, LSIState
),
2050 VMSTATE_UINT8(ccntl0
, LSIState
),
2051 VMSTATE_UINT8(ccntl1
, LSIState
),
2052 VMSTATE_UINT32(dsp
, LSIState
),
2053 VMSTATE_UINT32(dsps
, LSIState
),
2054 VMSTATE_UINT8(dmode
, LSIState
),
2055 VMSTATE_UINT8(dcntl
, LSIState
),
2056 VMSTATE_UINT8(scntl0
, LSIState
),
2057 VMSTATE_UINT8(scntl1
, LSIState
),
2058 VMSTATE_UINT8(scntl2
, LSIState
),
2059 VMSTATE_UINT8(scntl3
, LSIState
),
2060 VMSTATE_UINT8(sstat0
, LSIState
),
2061 VMSTATE_UINT8(sstat1
, LSIState
),
2062 VMSTATE_UINT8(scid
, LSIState
),
2063 VMSTATE_UINT8(sxfer
, LSIState
),
2064 VMSTATE_UINT8(socl
, LSIState
),
2065 VMSTATE_UINT8(sdid
, LSIState
),
2066 VMSTATE_UINT8(ssid
, LSIState
),
2067 VMSTATE_UINT8(sfbr
, LSIState
),
2068 VMSTATE_UINT8(stest1
, LSIState
),
2069 VMSTATE_UINT8(stest2
, LSIState
),
2070 VMSTATE_UINT8(stest3
, LSIState
),
2071 VMSTATE_UINT8(sidl
, LSIState
),
2072 VMSTATE_UINT8(stime0
, LSIState
),
2073 VMSTATE_UINT8(respid0
, LSIState
),
2074 VMSTATE_UINT8(respid1
, LSIState
),
2075 VMSTATE_UINT32(mmrs
, LSIState
),
2076 VMSTATE_UINT32(mmws
, LSIState
),
2077 VMSTATE_UINT32(sfs
, LSIState
),
2078 VMSTATE_UINT32(drs
, LSIState
),
2079 VMSTATE_UINT32(sbms
, LSIState
),
2080 VMSTATE_UINT32(dbms
, LSIState
),
2081 VMSTATE_UINT32(dnad64
, LSIState
),
2082 VMSTATE_UINT32(pmjad1
, LSIState
),
2083 VMSTATE_UINT32(pmjad2
, LSIState
),
2084 VMSTATE_UINT32(rbc
, LSIState
),
2085 VMSTATE_UINT32(ua
, LSIState
),
2086 VMSTATE_UINT32(ia
, LSIState
),
2087 VMSTATE_UINT32(sbc
, LSIState
),
2088 VMSTATE_UINT32(csbc
, LSIState
),
2089 VMSTATE_BUFFER_UNSAFE(scratch
, LSIState
, 0, 18 * sizeof(uint32_t)),
2090 VMSTATE_UINT8(sbr
, LSIState
),
2092 VMSTATE_BUFFER_UNSAFE(script_ram
, LSIState
, 0, 2048 * sizeof(uint32_t)),
2093 VMSTATE_END_OF_LIST()
2097 static int lsi_scsi_uninit(PCIDevice
*d
)
2099 LSIState
*s
= DO_UPCAST(LSIState
, dev
, d
);
2101 cpu_unregister_io_memory(s
->mmio_io_addr
);
2102 cpu_unregister_io_memory(s
->ram_io_addr
);
2104 qemu_free(s
->queue
);
2109 static int lsi_scsi_init(PCIDevice
*dev
)
2111 LSIState
*s
= DO_UPCAST(LSIState
, dev
, dev
);
2114 pci_conf
= s
->dev
.config
;
2116 /* PCI Vendor ID (word) */
2117 pci_config_set_vendor_id(pci_conf
, PCI_VENDOR_ID_LSI_LOGIC
);
2118 /* PCI device ID (word) */
2119 pci_config_set_device_id(pci_conf
, PCI_DEVICE_ID_LSI_53C895A
);
2120 /* PCI base class code */
2121 pci_config_set_class(pci_conf
, PCI_CLASS_STORAGE_SCSI
);
2122 /* PCI subsystem ID */
2123 pci_conf
[0x2e] = 0x00;
2124 pci_conf
[0x2f] = 0x10;
2125 /* PCI latency timer = 255 */
2126 pci_conf
[0x0d] = 0xff;
2127 /* Interrupt pin 1 */
2128 pci_conf
[0x3d] = 0x01;
2130 s
->mmio_io_addr
= cpu_register_io_memory(lsi_mmio_readfn
,
2131 lsi_mmio_writefn
, s
);
2132 s
->ram_io_addr
= cpu_register_io_memory(lsi_ram_readfn
,
2133 lsi_ram_writefn
, s
);
2135 pci_register_bar((struct PCIDevice
*)s
, 0, 256,
2136 PCI_BASE_ADDRESS_SPACE_IO
, lsi_io_mapfunc
);
2137 pci_register_bar((struct PCIDevice
*)s
, 1, 0x400,
2138 PCI_BASE_ADDRESS_SPACE_MEMORY
, lsi_mmio_mapfunc
);
2139 pci_register_bar((struct PCIDevice
*)s
, 2, 0x2000,
2140 PCI_BASE_ADDRESS_SPACE_MEMORY
, lsi_ram_mapfunc
);
2141 s
->queue
= qemu_malloc(sizeof(lsi_queue
));
2143 s
->active_commands
= 0;
2147 scsi_bus_new(&s
->bus
, &dev
->qdev
, 1, LSI_MAX_DEVS
, lsi_command_complete
);
2148 if (!dev
->qdev
.hotplugged
) {
2149 scsi_bus_legacy_handle_cmdline(&s
->bus
);
2154 static PCIDeviceInfo lsi_info
= {
2155 .qdev
.name
= "lsi53c895a",
2156 .qdev
.alias
= "lsi",
2157 .qdev
.size
= sizeof(LSIState
),
2158 .qdev
.vmsd
= &vmstate_lsi_scsi
,
2159 .init
= lsi_scsi_init
,
2160 .exit
= lsi_scsi_uninit
,
2163 static void lsi53c895a_register_devices(void)
2165 pci_qdev_register(&lsi_info
);
2168 device_init(lsi53c895a_register_devices
);