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 "scsi-disk.h"
19 #include "block_int.h"
22 //#define DEBUG_LSI_REG
25 #define DPRINTF(fmt, ...) \
26 do { printf("lsi_scsi: " fmt , ## __VA_ARGS__); } while (0)
27 #define BADF(fmt, ...) \
28 do { fprintf(stderr, "lsi_scsi: error: " fmt , ## __VA_ARGS__); exit(1);} while (0)
30 #define DPRINTF(fmt, ...) do {} while(0)
31 #define BADF(fmt, ...) \
32 do { fprintf(stderr, "lsi_scsi: error: " fmt , ## __VA_ARGS__);} while (0)
35 #define LSI_SCNTL0_TRG 0x01
36 #define LSI_SCNTL0_AAP 0x02
37 #define LSI_SCNTL0_EPC 0x08
38 #define LSI_SCNTL0_WATN 0x10
39 #define LSI_SCNTL0_START 0x20
41 #define LSI_SCNTL1_SST 0x01
42 #define LSI_SCNTL1_IARB 0x02
43 #define LSI_SCNTL1_AESP 0x04
44 #define LSI_SCNTL1_RST 0x08
45 #define LSI_SCNTL1_CON 0x10
46 #define LSI_SCNTL1_DHP 0x20
47 #define LSI_SCNTL1_ADB 0x40
48 #define LSI_SCNTL1_EXC 0x80
50 #define LSI_SCNTL2_WSR 0x01
51 #define LSI_SCNTL2_VUE0 0x02
52 #define LSI_SCNTL2_VUE1 0x04
53 #define LSI_SCNTL2_WSS 0x08
54 #define LSI_SCNTL2_SLPHBEN 0x10
55 #define LSI_SCNTL2_SLPMD 0x20
56 #define LSI_SCNTL2_CHM 0x40
57 #define LSI_SCNTL2_SDU 0x80
59 #define LSI_ISTAT0_DIP 0x01
60 #define LSI_ISTAT0_SIP 0x02
61 #define LSI_ISTAT0_INTF 0x04
62 #define LSI_ISTAT0_CON 0x08
63 #define LSI_ISTAT0_SEM 0x10
64 #define LSI_ISTAT0_SIGP 0x20
65 #define LSI_ISTAT0_SRST 0x40
66 #define LSI_ISTAT0_ABRT 0x80
68 #define LSI_ISTAT1_SI 0x01
69 #define LSI_ISTAT1_SRUN 0x02
70 #define LSI_ISTAT1_FLSH 0x04
72 #define LSI_SSTAT0_SDP0 0x01
73 #define LSI_SSTAT0_RST 0x02
74 #define LSI_SSTAT0_WOA 0x04
75 #define LSI_SSTAT0_LOA 0x08
76 #define LSI_SSTAT0_AIP 0x10
77 #define LSI_SSTAT0_OLF 0x20
78 #define LSI_SSTAT0_ORF 0x40
79 #define LSI_SSTAT0_ILF 0x80
81 #define LSI_SIST0_PAR 0x01
82 #define LSI_SIST0_RST 0x02
83 #define LSI_SIST0_UDC 0x04
84 #define LSI_SIST0_SGE 0x08
85 #define LSI_SIST0_RSL 0x10
86 #define LSI_SIST0_SEL 0x20
87 #define LSI_SIST0_CMP 0x40
88 #define LSI_SIST0_MA 0x80
90 #define LSI_SIST1_HTH 0x01
91 #define LSI_SIST1_GEN 0x02
92 #define LSI_SIST1_STO 0x04
93 #define LSI_SIST1_SBMC 0x10
95 #define LSI_SOCL_IO 0x01
96 #define LSI_SOCL_CD 0x02
97 #define LSI_SOCL_MSG 0x04
98 #define LSI_SOCL_ATN 0x08
99 #define LSI_SOCL_SEL 0x10
100 #define LSI_SOCL_BSY 0x20
101 #define LSI_SOCL_ACK 0x40
102 #define LSI_SOCL_REQ 0x80
104 #define LSI_DSTAT_IID 0x01
105 #define LSI_DSTAT_SIR 0x04
106 #define LSI_DSTAT_SSI 0x08
107 #define LSI_DSTAT_ABRT 0x10
108 #define LSI_DSTAT_BF 0x20
109 #define LSI_DSTAT_MDPE 0x40
110 #define LSI_DSTAT_DFE 0x80
112 #define LSI_DCNTL_COM 0x01
113 #define LSI_DCNTL_IRQD 0x02
114 #define LSI_DCNTL_STD 0x04
115 #define LSI_DCNTL_IRQM 0x08
116 #define LSI_DCNTL_SSM 0x10
117 #define LSI_DCNTL_PFEN 0x20
118 #define LSI_DCNTL_PFF 0x40
119 #define LSI_DCNTL_CLSE 0x80
121 #define LSI_DMODE_MAN 0x01
122 #define LSI_DMODE_BOF 0x02
123 #define LSI_DMODE_ERMP 0x04
124 #define LSI_DMODE_ERL 0x08
125 #define LSI_DMODE_DIOM 0x10
126 #define LSI_DMODE_SIOM 0x20
128 #define LSI_CTEST2_DACK 0x01
129 #define LSI_CTEST2_DREQ 0x02
130 #define LSI_CTEST2_TEOP 0x04
131 #define LSI_CTEST2_PCICIE 0x08
132 #define LSI_CTEST2_CM 0x10
133 #define LSI_CTEST2_CIO 0x20
134 #define LSI_CTEST2_SIGP 0x40
135 #define LSI_CTEST2_DDIR 0x80
137 #define LSI_CTEST5_BL2 0x04
138 #define LSI_CTEST5_DDIR 0x08
139 #define LSI_CTEST5_MASR 0x10
140 #define LSI_CTEST5_DFSN 0x20
141 #define LSI_CTEST5_BBCK 0x40
142 #define LSI_CTEST5_ADCK 0x80
144 #define LSI_CCNTL0_DILS 0x01
145 #define LSI_CCNTL0_DISFC 0x10
146 #define LSI_CCNTL0_ENNDJ 0x20
147 #define LSI_CCNTL0_PMJCTL 0x40
148 #define LSI_CCNTL0_ENPMJ 0x80
150 #define LSI_CCNTL1_EN64DBMV 0x01
151 #define LSI_CCNTL1_EN64TIBMV 0x02
152 #define LSI_CCNTL1_64TIMOD 0x04
153 #define LSI_CCNTL1_DDAC 0x08
154 #define LSI_CCNTL1_ZMOD 0x80
156 #define LSI_CCNTL1_40BIT (LSI_CCNTL1_EN64TIBMV|LSI_CCNTL1_64TIMOD)
166 /* Maximum length of MSG IN data. */
167 #define LSI_MAX_MSGIN_LEN 8
169 /* Flag set if this is a tagged command. */
170 #define LSI_TAG_VALID (1 << 16)
182 uint32_t script_ram_base
;
184 int carry
; /* ??? Should this be an a visible register somewhere? */
186 /* Action to take at the end of a MSG IN phase.
187 0 = COMMAND, 1 = disconect, 2 = DATA OUT, 3 = DATA IN. */
190 uint8_t msg
[LSI_MAX_MSGIN_LEN
];
191 /* 0 if SCRIPTS are running or stopped.
192 * 1 if a Wait Reselect instruction has been issued.
193 * 2 if processing DMA from lsi_execute_script.
194 * 3 if a DMA operation is in progress. */
197 SCSIDevice
*current_dev
;
199 /* The tag is a combination of the device ID and the SCSI tag. */
200 uint32_t current_tag
;
201 uint32_t current_dma_len
;
202 int command_complete
;
267 uint32_t scratch
[18]; /* SCRATCHA-SCRATCHR */
270 /* Script ram is stored as 32-bit words in host byteorder. */
271 uint32_t script_ram
[2048];
274 static void lsi_soft_reset(LSIState
*s
)
284 memset(s
->scratch
, 0, sizeof(s
->scratch
));
340 static int lsi_dma_40bit(LSIState
*s
)
342 if ((s
->ccntl1
& LSI_CCNTL1_40BIT
) == LSI_CCNTL1_40BIT
)
347 static int lsi_dma_ti64bit(LSIState
*s
)
349 if ((s
->ccntl1
& LSI_CCNTL1_EN64TIBMV
) == LSI_CCNTL1_EN64TIBMV
)
354 static int lsi_dma_64bit(LSIState
*s
)
356 if ((s
->ccntl1
& LSI_CCNTL1_EN64DBMV
) == LSI_CCNTL1_EN64DBMV
)
361 static uint8_t lsi_reg_readb(LSIState
*s
, int offset
);
362 static void lsi_reg_writeb(LSIState
*s
, int offset
, uint8_t val
);
363 static void lsi_execute_script(LSIState
*s
);
365 static inline uint32_t read_dword(LSIState
*s
, uint32_t addr
)
369 /* Optimize reading from SCRIPTS RAM. */
370 if ((addr
& 0xffffe000) == s
->script_ram_base
) {
371 return s
->script_ram
[(addr
& 0x1fff) >> 2];
373 cpu_physical_memory_read(addr
, (uint8_t *)&buf
, 4);
374 return cpu_to_le32(buf
);
377 static void lsi_stop_script(LSIState
*s
)
379 s
->istat1
&= ~LSI_ISTAT1_SRUN
;
382 static void lsi_update_irq(LSIState
*s
)
385 static int last_level
;
387 /* It's unclear whether the DIP/SIP bits should be cleared when the
388 Interrupt Status Registers are cleared or when istat0 is read.
389 We currently do the formwer, which seems to work. */
392 if (s
->dstat
& s
->dien
)
394 s
->istat0
|= LSI_ISTAT0_DIP
;
396 s
->istat0
&= ~LSI_ISTAT0_DIP
;
399 if (s
->sist0
|| s
->sist1
) {
400 if ((s
->sist0
& s
->sien0
) || (s
->sist1
& s
->sien1
))
402 s
->istat0
|= LSI_ISTAT0_SIP
;
404 s
->istat0
&= ~LSI_ISTAT0_SIP
;
406 if (s
->istat0
& LSI_ISTAT0_INTF
)
409 if (level
!= last_level
) {
410 DPRINTF("Update IRQ level %d dstat %02x sist %02x%02x\n",
411 level
, s
->dstat
, s
->sist1
, s
->sist0
);
414 qemu_set_irq(s
->dev
.irq
[0], level
);
417 /* Stop SCRIPTS execution and raise a SCSI interrupt. */
418 static void lsi_script_scsi_interrupt(LSIState
*s
, int stat0
, int stat1
)
423 DPRINTF("SCSI Interrupt 0x%02x%02x prev 0x%02x%02x\n",
424 stat1
, stat0
, s
->sist1
, s
->sist0
);
427 /* Stop processor on fatal or unmasked interrupt. As a special hack
428 we don't stop processing when raising STO. Instead continue
429 execution and stop at the next insn that accesses the SCSI bus. */
430 mask0
= s
->sien0
| ~(LSI_SIST0_CMP
| LSI_SIST0_SEL
| LSI_SIST0_RSL
);
431 mask1
= s
->sien1
| ~(LSI_SIST1_GEN
| LSI_SIST1_HTH
);
432 mask1
&= ~LSI_SIST1_STO
;
433 if (s
->sist0
& mask0
|| s
->sist1
& mask1
) {
439 /* Stop SCRIPTS execution and raise a DMA interrupt. */
440 static void lsi_script_dma_interrupt(LSIState
*s
, int stat
)
442 DPRINTF("DMA Interrupt 0x%x prev 0x%x\n", stat
, s
->dstat
);
448 static inline void lsi_set_phase(LSIState
*s
, int phase
)
450 s
->sstat1
= (s
->sstat1
& ~PHASE_MASK
) | phase
;
453 static void lsi_bad_phase(LSIState
*s
, int out
, int new_phase
)
455 /* Trigger a phase mismatch. */
456 if (s
->ccntl0
& LSI_CCNTL0_ENPMJ
) {
457 if ((s
->ccntl0
& LSI_CCNTL0_PMJCTL
) || out
) {
462 DPRINTF("Data phase mismatch jump to %08x\n", s
->dsp
);
464 DPRINTF("Phase mismatch interrupt\n");
465 lsi_script_scsi_interrupt(s
, LSI_SIST0_MA
, 0);
468 lsi_set_phase(s
, new_phase
);
472 /* Resume SCRIPTS execution after a DMA operation. */
473 static void lsi_resume_script(LSIState
*s
)
475 if (s
->waiting
!= 2) {
477 lsi_execute_script(s
);
483 /* Initiate a SCSI layer data transfer. */
484 static void lsi_do_dma(LSIState
*s
, int out
)
487 target_phys_addr_t addr
;
489 if (!s
->current_dma_len
) {
490 /* Wait until data is available. */
491 DPRINTF("DMA no data available\n");
496 if (count
> s
->current_dma_len
)
497 count
= s
->current_dma_len
;
500 /* both 40 and Table Indirect 64-bit DMAs store upper bits in dnad64 */
501 if (lsi_dma_40bit(s
) || lsi_dma_ti64bit(s
))
502 addr
|= ((uint64_t)s
->dnad64
<< 32);
504 addr
|= ((uint64_t)s
->dbms
<< 32);
506 addr
|= ((uint64_t)s
->sbms
<< 32);
508 DPRINTF("DMA addr=0x" TARGET_FMT_plx
" len=%d\n", addr
, count
);
513 if (s
->dma_buf
== NULL
) {
514 s
->dma_buf
= s
->current_dev
->info
->get_buf(s
->current_dev
,
518 /* ??? Set SFBR to first data byte. */
520 cpu_physical_memory_read(addr
, s
->dma_buf
, count
);
522 cpu_physical_memory_write(addr
, s
->dma_buf
, count
);
524 s
->current_dma_len
-= count
;
525 if (s
->current_dma_len
== 0) {
528 /* Write the data. */
529 s
->current_dev
->info
->write_data(s
->current_dev
, s
->current_tag
);
531 /* Request any remaining data. */
532 s
->current_dev
->info
->read_data(s
->current_dev
, s
->current_tag
);
536 lsi_resume_script(s
);
541 /* Add a command to the queue. */
542 static void lsi_queue_command(LSIState
*s
)
546 DPRINTF("Queueing tag=0x%x\n", s
->current_tag
);
547 if (s
->queue_len
== s
->active_commands
) {
549 s
->queue
= qemu_realloc(s
->queue
, s
->queue_len
* sizeof(lsi_queue
));
551 p
= &s
->queue
[s
->active_commands
++];
552 p
->tag
= s
->current_tag
;
554 p
->out
= (s
->sstat1
& PHASE_MASK
) == PHASE_DO
;
557 /* Queue a byte for a MSG IN phase. */
558 static void lsi_add_msg_byte(LSIState
*s
, uint8_t data
)
560 if (s
->msg_len
>= LSI_MAX_MSGIN_LEN
) {
561 BADF("MSG IN data too long\n");
563 DPRINTF("MSG IN 0x%02x\n", data
);
564 s
->msg
[s
->msg_len
++] = data
;
568 /* Perform reselection to continue a command. */
569 static void lsi_reselect(LSIState
*s
, uint32_t tag
)
576 for (n
= 0; n
< s
->active_commands
; n
++) {
581 if (n
== s
->active_commands
) {
582 BADF("Reselected non-existant command tag=0x%x\n", tag
);
585 id
= (tag
>> 8) & 0xf;
587 DPRINTF("Reselected target %d\n", id
);
588 s
->current_dev
= s
->bus
->devs
[id
];
589 s
->current_tag
= tag
;
590 s
->scntl1
|= LSI_SCNTL1_CON
;
591 lsi_set_phase(s
, PHASE_MI
);
592 s
->msg_action
= p
->out
? 2 : 3;
593 s
->current_dma_len
= p
->pending
;
595 lsi_add_msg_byte(s
, 0x80);
596 if (s
->current_tag
& LSI_TAG_VALID
) {
597 lsi_add_msg_byte(s
, 0x20);
598 lsi_add_msg_byte(s
, tag
& 0xff);
601 s
->active_commands
--;
602 if (n
!= s
->active_commands
) {
603 s
->queue
[n
] = s
->queue
[s
->active_commands
];
607 /* Record that data is available for a queued command. Returns zero if
608 the device was reselected, nonzero if the IO is deferred. */
609 static int lsi_queue_tag(LSIState
*s
, uint32_t tag
, uint32_t arg
)
613 for (i
= 0; i
< s
->active_commands
; i
++) {
617 BADF("Multiple IO pending for tag %d\n", tag
);
620 if (s
->waiting
== 1) {
621 /* Reselect device. */
622 lsi_reselect(s
, tag
);
625 DPRINTF("Queueing IO tag=0x%x\n", tag
);
631 BADF("IO with unknown tag %d\n", tag
);
635 /* Callback to indicate that the SCSI layer has completed a transfer. */
636 static void lsi_command_complete(SCSIBus
*bus
, int reason
, uint32_t tag
,
639 LSIState
*s
= DO_UPCAST(LSIState
, dev
.qdev
, bus
->qbus
.parent
);
642 out
= (s
->sstat1
& PHASE_MASK
) == PHASE_DO
;
643 if (reason
== SCSI_REASON_DONE
) {
644 DPRINTF("Command complete sense=%d\n", (int)arg
);
646 s
->command_complete
= 2;
647 if (s
->waiting
&& s
->dbc
!= 0) {
648 /* Raise phase mismatch for short transfers. */
649 lsi_bad_phase(s
, out
, PHASE_ST
);
651 lsi_set_phase(s
, PHASE_ST
);
653 lsi_resume_script(s
);
657 if (s
->waiting
== 1 || tag
!= s
->current_tag
) {
658 if (lsi_queue_tag(s
, tag
, arg
))
661 DPRINTF("Data ready tag=0x%x len=%d\n", tag
, arg
);
662 s
->current_dma_len
= arg
;
663 s
->command_complete
= 1;
666 if (s
->waiting
== 1 || s
->dbc
== 0) {
667 lsi_resume_script(s
);
673 static void lsi_do_command(LSIState
*s
)
678 DPRINTF("Send command len=%d\n", s
->dbc
);
681 cpu_physical_memory_read(s
->dnad
, buf
, s
->dbc
);
683 s
->command_complete
= 0;
684 n
= s
->current_dev
->info
->send_command(s
->current_dev
, s
->current_tag
, buf
,
687 lsi_set_phase(s
, PHASE_DI
);
688 s
->current_dev
->info
->read_data(s
->current_dev
, s
->current_tag
);
690 lsi_set_phase(s
, PHASE_DO
);
691 s
->current_dev
->info
->write_data(s
->current_dev
, s
->current_tag
);
694 if (!s
->command_complete
) {
696 /* Command did not complete immediately so disconnect. */
697 lsi_add_msg_byte(s
, 2); /* SAVE DATA POINTER */
698 lsi_add_msg_byte(s
, 4); /* DISCONNECT */
700 lsi_set_phase(s
, PHASE_MI
);
702 lsi_queue_command(s
);
704 /* wait command complete */
705 lsi_set_phase(s
, PHASE_DI
);
710 static void lsi_do_status(LSIState
*s
)
713 DPRINTF("Get status len=%d sense=%d\n", s
->dbc
, s
->sense
);
715 BADF("Bad Status move\n");
719 cpu_physical_memory_write(s
->dnad
, &sense
, 1);
720 lsi_set_phase(s
, PHASE_MI
);
722 lsi_add_msg_byte(s
, 0); /* COMMAND COMPLETE */
725 static void lsi_disconnect(LSIState
*s
)
727 s
->scntl1
&= ~LSI_SCNTL1_CON
;
728 s
->sstat1
&= ~PHASE_MASK
;
731 static void lsi_do_msgin(LSIState
*s
)
734 DPRINTF("Message in len=%d/%d\n", s
->dbc
, s
->msg_len
);
739 cpu_physical_memory_write(s
->dnad
, s
->msg
, len
);
740 /* Linux drivers rely on the last byte being in the SIDL. */
741 s
->sidl
= s
->msg
[len
- 1];
744 memmove(s
->msg
, s
->msg
+ len
, s
->msg_len
);
746 /* ??? Check if ATN (not yet implemented) is asserted and maybe
747 switch to PHASE_MO. */
748 switch (s
->msg_action
) {
750 lsi_set_phase(s
, PHASE_CMD
);
756 lsi_set_phase(s
, PHASE_DO
);
759 lsi_set_phase(s
, PHASE_DI
);
767 /* Read the next byte during a MSGOUT phase. */
768 static uint8_t lsi_get_msgbyte(LSIState
*s
)
771 cpu_physical_memory_read(s
->dnad
, &data
, 1);
777 static void lsi_do_msgout(LSIState
*s
)
782 DPRINTF("MSG out len=%d\n", s
->dbc
);
784 msg
= lsi_get_msgbyte(s
);
789 DPRINTF("MSG: Disconnect\n");
793 DPRINTF("MSG: No Operation\n");
794 lsi_set_phase(s
, PHASE_CMD
);
797 len
= lsi_get_msgbyte(s
);
798 msg
= lsi_get_msgbyte(s
);
799 DPRINTF("Extended message 0x%x (len %d)\n", msg
, len
);
802 DPRINTF("SDTR (ignored)\n");
806 DPRINTF("WDTR (ignored)\n");
813 case 0x20: /* SIMPLE queue */
814 s
->current_tag
|= lsi_get_msgbyte(s
) | LSI_TAG_VALID
;
815 DPRINTF("SIMPLE queue tag=0x%x\n", s
->current_tag
& 0xff);
817 case 0x21: /* HEAD of queue */
818 BADF("HEAD queue not implemented\n");
819 s
->current_tag
|= lsi_get_msgbyte(s
) | LSI_TAG_VALID
;
821 case 0x22: /* ORDERED queue */
822 BADF("ORDERED queue not implemented\n");
823 s
->current_tag
|= lsi_get_msgbyte(s
) | LSI_TAG_VALID
;
826 if ((msg
& 0x80) == 0) {
829 s
->current_lun
= msg
& 7;
830 DPRINTF("Select LUN %d\n", s
->current_lun
);
831 lsi_set_phase(s
, PHASE_CMD
);
837 BADF("Unimplemented message 0x%02x\n", msg
);
838 lsi_set_phase(s
, PHASE_MI
);
839 lsi_add_msg_byte(s
, 7); /* MESSAGE REJECT */
843 /* Sign extend a 24-bit value. */
844 static inline int32_t sxt24(int32_t n
)
846 return (n
<< 8) >> 8;
849 #define LSI_BUF_SIZE 4096
850 static void lsi_memcpy(LSIState
*s
, uint32_t dest
, uint32_t src
, int count
)
853 uint8_t buf
[LSI_BUF_SIZE
];
855 DPRINTF("memcpy dest 0x%08x src 0x%08x count %d\n", dest
, src
, count
);
857 n
= (count
> LSI_BUF_SIZE
) ? LSI_BUF_SIZE
: count
;
858 cpu_physical_memory_read(src
, buf
, n
);
859 cpu_physical_memory_write(dest
, buf
, n
);
866 static void lsi_wait_reselect(LSIState
*s
)
869 DPRINTF("Wait Reselect\n");
870 if (s
->current_dma_len
)
871 BADF("Reselect with pending DMA\n");
872 for (i
= 0; i
< s
->active_commands
; i
++) {
873 if (s
->queue
[i
].pending
) {
874 lsi_reselect(s
, s
->queue
[i
].tag
);
878 if (s
->current_dma_len
== 0) {
883 static void lsi_execute_script(LSIState
*s
)
886 uint32_t addr
, addr_high
;
888 int insn_processed
= 0;
890 s
->istat1
|= LSI_ISTAT1_SRUN
;
893 insn
= read_dword(s
, s
->dsp
);
895 /* If we receive an empty opcode increment the DSP by 4 bytes
896 instead of 8 and execute the next opcode at that location */
900 addr
= read_dword(s
, s
->dsp
+ 4);
902 DPRINTF("SCRIPTS dsp=%08x opcode %08x arg %08x\n", s
->dsp
, insn
, addr
);
904 s
->dcmd
= insn
>> 24;
906 switch (insn
>> 30) {
907 case 0: /* Block move. */
908 if (s
->sist1
& LSI_SIST1_STO
) {
909 DPRINTF("Delayed select timeout\n");
913 s
->dbc
= insn
& 0xffffff;
917 if (insn
& (1 << 29)) {
918 /* Indirect addressing. */
919 addr
= read_dword(s
, addr
);
920 } else if (insn
& (1 << 28)) {
923 /* Table indirect addressing. */
925 /* 32-bit Table indirect */
926 offset
= sxt24(addr
);
927 cpu_physical_memory_read(s
->dsa
+ offset
, (uint8_t *)buf
, 8);
928 /* byte count is stored in bits 0:23 only */
929 s
->dbc
= cpu_to_le32(buf
[0]) & 0xffffff;
931 addr
= cpu_to_le32(buf
[1]);
933 /* 40-bit DMA, upper addr bits [39:32] stored in first DWORD of
934 * table, bits [31:24] */
935 if (lsi_dma_40bit(s
))
936 addr_high
= cpu_to_le32(buf
[0]) >> 24;
937 else if (lsi_dma_ti64bit(s
)) {
938 int selector
= (cpu_to_le32(buf
[0]) >> 24) & 0x1f;
941 /* offset index into scratch registers since
942 * TI64 mode can use registers C to R */
943 addr_high
= s
->scratch
[2 + selector
];
964 BADF("Illegal selector specified (0x%x > 0x15)"
965 " for 64-bit DMA block move", selector
);
969 } else if (lsi_dma_64bit(s
)) {
970 /* fetch a 3rd dword if 64-bit direct move is enabled and
971 only if we're not doing table indirect or indirect addressing */
972 s
->dbms
= read_dword(s
, s
->dsp
);
976 if ((s
->sstat1
& PHASE_MASK
) != ((insn
>> 24) & 7)) {
977 DPRINTF("Wrong phase got %d expected %d\n",
978 s
->sstat1
& PHASE_MASK
, (insn
>> 24) & 7);
979 lsi_script_scsi_interrupt(s
, LSI_SIST0_MA
, 0);
983 s
->dnad64
= addr_high
;
984 switch (s
->sstat1
& 0x7) {
1010 BADF("Unimplemented phase %d\n", s
->sstat1
& PHASE_MASK
);
1013 s
->dfifo
= s
->dbc
& 0xff;
1014 s
->ctest5
= (s
->ctest5
& 0xfc) | ((s
->dbc
>> 8) & 3);
1017 s
->ua
= addr
+ s
->dbc
;
1020 case 1: /* IO or Read/Write instruction. */
1021 opcode
= (insn
>> 27) & 7;
1025 if (insn
& (1 << 25)) {
1026 id
= read_dword(s
, s
->dsa
+ sxt24(insn
));
1030 id
= (id
>> 16) & 0xf;
1031 if (insn
& (1 << 26)) {
1032 addr
= s
->dsp
+ sxt24(addr
);
1036 case 0: /* Select */
1038 if (s
->current_dma_len
&& (s
->ssid
& 0xf) == id
) {
1039 DPRINTF("Already reselected by target %d\n", id
);
1042 s
->sstat0
|= LSI_SSTAT0_WOA
;
1043 s
->scntl1
&= ~LSI_SCNTL1_IARB
;
1044 if (id
>= LSI_MAX_DEVS
|| !s
->bus
->devs
[id
]) {
1045 DPRINTF("Selected absent target %d\n", id
);
1046 lsi_script_scsi_interrupt(s
, 0, LSI_SIST1_STO
);
1050 DPRINTF("Selected target %d%s\n",
1051 id
, insn
& (1 << 3) ? " ATN" : "");
1052 /* ??? Linux drivers compain when this is set. Maybe
1053 it only applies in low-level mode (unimplemented).
1054 lsi_script_scsi_interrupt(s, LSI_SIST0_CMP, 0); */
1055 s
->current_dev
= s
->bus
->devs
[id
];
1056 s
->current_tag
= id
<< 8;
1057 s
->scntl1
|= LSI_SCNTL1_CON
;
1058 if (insn
& (1 << 3)) {
1059 s
->socl
|= LSI_SOCL_ATN
;
1061 lsi_set_phase(s
, PHASE_MO
);
1063 case 1: /* Disconnect */
1064 DPRINTF("Wait Disconect\n");
1065 s
->scntl1
&= ~LSI_SCNTL1_CON
;
1067 case 2: /* Wait Reselect */
1068 lsi_wait_reselect(s
);
1071 DPRINTF("Set%s%s%s%s\n",
1072 insn
& (1 << 3) ? " ATN" : "",
1073 insn
& (1 << 6) ? " ACK" : "",
1074 insn
& (1 << 9) ? " TM" : "",
1075 insn
& (1 << 10) ? " CC" : "");
1076 if (insn
& (1 << 3)) {
1077 s
->socl
|= LSI_SOCL_ATN
;
1078 lsi_set_phase(s
, PHASE_MO
);
1080 if (insn
& (1 << 9)) {
1081 BADF("Target mode not implemented\n");
1084 if (insn
& (1 << 10))
1088 DPRINTF("Clear%s%s%s%s\n",
1089 insn
& (1 << 3) ? " ATN" : "",
1090 insn
& (1 << 6) ? " ACK" : "",
1091 insn
& (1 << 9) ? " TM" : "",
1092 insn
& (1 << 10) ? " CC" : "");
1093 if (insn
& (1 << 3)) {
1094 s
->socl
&= ~LSI_SOCL_ATN
;
1096 if (insn
& (1 << 10))
1107 static const char *opcode_names
[3] =
1108 {"Write", "Read", "Read-Modify-Write"};
1109 static const char *operator_names
[8] =
1110 {"MOV", "SHL", "OR", "XOR", "AND", "SHR", "ADD", "ADC"};
1113 reg
= ((insn
>> 16) & 0x7f) | (insn
& 0x80);
1114 data8
= (insn
>> 8) & 0xff;
1115 opcode
= (insn
>> 27) & 7;
1116 operator = (insn
>> 24) & 7;
1117 DPRINTF("%s reg 0x%x %s data8=0x%02x sfbr=0x%02x%s\n",
1118 opcode_names
[opcode
- 5], reg
,
1119 operator_names
[operator], data8
, s
->sfbr
,
1120 (insn
& (1 << 23)) ? " SFBR" : "");
1123 case 5: /* From SFBR */
1127 case 6: /* To SFBR */
1129 op0
= lsi_reg_readb(s
, reg
);
1132 case 7: /* Read-modify-write */
1134 op0
= lsi_reg_readb(s
, reg
);
1135 if (insn
& (1 << 23)) {
1147 case 1: /* Shift left */
1149 op0
= (op0
<< 1) | s
->carry
;
1163 op0
= (op0
>> 1) | (s
->carry
<< 7);
1168 s
->carry
= op0
< op1
;
1171 op0
+= op1
+ s
->carry
;
1173 s
->carry
= op0
<= op1
;
1175 s
->carry
= op0
< op1
;
1180 case 5: /* From SFBR */
1181 case 7: /* Read-modify-write */
1182 lsi_reg_writeb(s
, reg
, op0
);
1184 case 6: /* To SFBR */
1191 case 2: /* Transfer Control. */
1196 if ((insn
& 0x002e0000) == 0) {
1200 if (s
->sist1
& LSI_SIST1_STO
) {
1201 DPRINTF("Delayed select timeout\n");
1205 cond
= jmp
= (insn
& (1 << 19)) != 0;
1206 if (cond
== jmp
&& (insn
& (1 << 21))) {
1207 DPRINTF("Compare carry %d\n", s
->carry
== jmp
);
1208 cond
= s
->carry
!= 0;
1210 if (cond
== jmp
&& (insn
& (1 << 17))) {
1211 DPRINTF("Compare phase %d %c= %d\n",
1212 (s
->sstat1
& PHASE_MASK
),
1214 ((insn
>> 24) & 7));
1215 cond
= (s
->sstat1
& PHASE_MASK
) == ((insn
>> 24) & 7);
1217 if (cond
== jmp
&& (insn
& (1 << 18))) {
1220 mask
= (~insn
>> 8) & 0xff;
1221 DPRINTF("Compare data 0x%x & 0x%x %c= 0x%x\n",
1222 s
->sfbr
, mask
, jmp
? '=' : '!', insn
& mask
);
1223 cond
= (s
->sfbr
& mask
) == (insn
& mask
);
1226 if (insn
& (1 << 23)) {
1227 /* Relative address. */
1228 addr
= s
->dsp
+ sxt24(addr
);
1230 switch ((insn
>> 27) & 7) {
1232 DPRINTF("Jump to 0x%08x\n", addr
);
1236 DPRINTF("Call 0x%08x\n", addr
);
1240 case 2: /* Return */
1241 DPRINTF("Return to 0x%08x\n", s
->temp
);
1244 case 3: /* Interrupt */
1245 DPRINTF("Interrupt 0x%08x\n", s
->dsps
);
1246 if ((insn
& (1 << 20)) != 0) {
1247 s
->istat0
|= LSI_ISTAT0_INTF
;
1250 lsi_script_dma_interrupt(s
, LSI_DSTAT_SIR
);
1254 DPRINTF("Illegal transfer control\n");
1255 lsi_script_dma_interrupt(s
, LSI_DSTAT_IID
);
1259 DPRINTF("Control condition failed\n");
1265 if ((insn
& (1 << 29)) == 0) {
1268 /* ??? The docs imply the destination address is loaded into
1269 the TEMP register. However the Linux drivers rely on
1270 the value being presrved. */
1271 dest
= read_dword(s
, s
->dsp
);
1273 lsi_memcpy(s
, dest
, addr
, insn
& 0xffffff);
1280 if (insn
& (1 << 28)) {
1281 addr
= s
->dsa
+ sxt24(addr
);
1284 reg
= (insn
>> 16) & 0xff;
1285 if (insn
& (1 << 24)) {
1286 cpu_physical_memory_read(addr
, data
, n
);
1287 DPRINTF("Load reg 0x%x size %d addr 0x%08x = %08x\n", reg
, n
,
1288 addr
, *(int *)data
);
1289 for (i
= 0; i
< n
; i
++) {
1290 lsi_reg_writeb(s
, reg
+ i
, data
[i
]);
1293 DPRINTF("Store reg 0x%x size %d addr 0x%08x\n", reg
, n
, addr
);
1294 for (i
= 0; i
< n
; i
++) {
1295 data
[i
] = lsi_reg_readb(s
, reg
+ i
);
1297 cpu_physical_memory_write(addr
, data
, n
);
1301 if (insn_processed
> 10000 && !s
->waiting
) {
1302 /* Some windows drivers make the device spin waiting for a memory
1303 location to change. If we have been executed a lot of code then
1304 assume this is the case and force an unexpected device disconnect.
1305 This is apparently sufficient to beat the drivers into submission.
1307 if (!(s
->sien0
& LSI_SIST0_UDC
))
1308 fprintf(stderr
, "inf. loop with UDC masked\n");
1309 lsi_script_scsi_interrupt(s
, LSI_SIST0_UDC
, 0);
1311 } else if (s
->istat1
& LSI_ISTAT1_SRUN
&& !s
->waiting
) {
1312 if (s
->dcntl
& LSI_DCNTL_SSM
) {
1313 lsi_script_dma_interrupt(s
, LSI_DSTAT_SSI
);
1318 DPRINTF("SCRIPTS execution stopped\n");
1321 static uint8_t lsi_reg_readb(LSIState
*s
, int offset
)
1324 #define CASE_GET_REG24(name, addr) \
1325 case addr: return s->name & 0xff; \
1326 case addr + 1: return (s->name >> 8) & 0xff; \
1327 case addr + 2: return (s->name >> 16) & 0xff;
1329 #define CASE_GET_REG32(name, addr) \
1330 case addr: return s->name & 0xff; \
1331 case addr + 1: return (s->name >> 8) & 0xff; \
1332 case addr + 2: return (s->name >> 16) & 0xff; \
1333 case addr + 3: return (s->name >> 24) & 0xff;
1335 #ifdef DEBUG_LSI_REG
1336 DPRINTF("Read reg %x\n", offset
);
1339 case 0x00: /* SCNTL0 */
1341 case 0x01: /* SCNTL1 */
1343 case 0x02: /* SCNTL2 */
1345 case 0x03: /* SCNTL3 */
1347 case 0x04: /* SCID */
1349 case 0x05: /* SXFER */
1351 case 0x06: /* SDID */
1353 case 0x07: /* GPREG0 */
1355 case 0x08: /* Revision ID */
1357 case 0xa: /* SSID */
1359 case 0xb: /* SBCL */
1360 /* ??? This is not correct. However it's (hopefully) only
1361 used for diagnostics, so should be ok. */
1363 case 0xc: /* DSTAT */
1364 tmp
= s
->dstat
| 0x80;
1365 if ((s
->istat0
& LSI_ISTAT0_INTF
) == 0)
1369 case 0x0d: /* SSTAT0 */
1371 case 0x0e: /* SSTAT1 */
1373 case 0x0f: /* SSTAT2 */
1374 return s
->scntl1
& LSI_SCNTL1_CON
? 0 : 2;
1375 CASE_GET_REG32(dsa
, 0x10)
1376 case 0x14: /* ISTAT0 */
1378 case 0x15: /* ISTAT1 */
1380 case 0x16: /* MBOX0 */
1382 case 0x17: /* MBOX1 */
1384 case 0x18: /* CTEST0 */
1386 case 0x19: /* CTEST1 */
1388 case 0x1a: /* CTEST2 */
1389 tmp
= s
->ctest2
| LSI_CTEST2_DACK
| LSI_CTEST2_CM
;
1390 if (s
->istat0
& LSI_ISTAT0_SIGP
) {
1391 s
->istat0
&= ~LSI_ISTAT0_SIGP
;
1392 tmp
|= LSI_CTEST2_SIGP
;
1395 case 0x1b: /* CTEST3 */
1397 CASE_GET_REG32(temp
, 0x1c)
1398 case 0x20: /* DFIFO */
1400 case 0x21: /* CTEST4 */
1402 case 0x22: /* CTEST5 */
1404 case 0x23: /* CTEST6 */
1406 CASE_GET_REG24(dbc
, 0x24)
1407 case 0x27: /* DCMD */
1409 CASE_GET_REG32(dnad
, 0x28)
1410 CASE_GET_REG32(dsp
, 0x2c)
1411 CASE_GET_REG32(dsps
, 0x30)
1412 CASE_GET_REG32(scratch
[0], 0x34)
1413 case 0x38: /* DMODE */
1415 case 0x39: /* DIEN */
1417 case 0x3a: /* SBR */
1419 case 0x3b: /* DCNTL */
1421 case 0x40: /* SIEN0 */
1423 case 0x41: /* SIEN1 */
1425 case 0x42: /* SIST0 */
1430 case 0x43: /* SIST1 */
1435 case 0x46: /* MACNTL */
1437 case 0x47: /* GPCNTL0 */
1439 case 0x48: /* STIME0 */
1441 case 0x4a: /* RESPID0 */
1443 case 0x4b: /* RESPID1 */
1445 case 0x4d: /* STEST1 */
1447 case 0x4e: /* STEST2 */
1449 case 0x4f: /* STEST3 */
1451 case 0x50: /* SIDL */
1452 /* This is needed by the linux drivers. We currently only update it
1453 during the MSG IN phase. */
1455 case 0x52: /* STEST4 */
1457 case 0x56: /* CCNTL0 */
1459 case 0x57: /* CCNTL1 */
1461 case 0x58: /* SBDL */
1462 /* Some drivers peek at the data bus during the MSG IN phase. */
1463 if ((s
->sstat1
& PHASE_MASK
) == PHASE_MI
)
1466 case 0x59: /* SBDL high */
1468 CASE_GET_REG32(mmrs
, 0xa0)
1469 CASE_GET_REG32(mmws
, 0xa4)
1470 CASE_GET_REG32(sfs
, 0xa8)
1471 CASE_GET_REG32(drs
, 0xac)
1472 CASE_GET_REG32(sbms
, 0xb0)
1473 CASE_GET_REG32(dbms
, 0xb4)
1474 CASE_GET_REG32(dnad64
, 0xb8)
1475 CASE_GET_REG32(pmjad1
, 0xc0)
1476 CASE_GET_REG32(pmjad2
, 0xc4)
1477 CASE_GET_REG32(rbc
, 0xc8)
1478 CASE_GET_REG32(ua
, 0xcc)
1479 CASE_GET_REG32(ia
, 0xd4)
1480 CASE_GET_REG32(sbc
, 0xd8)
1481 CASE_GET_REG32(csbc
, 0xdc)
1483 if (offset
>= 0x5c && offset
< 0xa0) {
1486 n
= (offset
- 0x58) >> 2;
1487 shift
= (offset
& 3) * 8;
1488 return (s
->scratch
[n
] >> shift
) & 0xff;
1490 BADF("readb 0x%x\n", offset
);
1492 #undef CASE_GET_REG24
1493 #undef CASE_GET_REG32
1496 static void lsi_reg_writeb(LSIState
*s
, int offset
, uint8_t val
)
1498 #define CASE_SET_REG24(name, addr) \
1499 case addr : s->name &= 0xffffff00; s->name |= val; break; \
1500 case addr + 1: s->name &= 0xffff00ff; s->name |= val << 8; break; \
1501 case addr + 2: s->name &= 0xff00ffff; s->name |= val << 16; break;
1503 #define CASE_SET_REG32(name, addr) \
1504 case addr : s->name &= 0xffffff00; s->name |= val; break; \
1505 case addr + 1: s->name &= 0xffff00ff; s->name |= val << 8; break; \
1506 case addr + 2: s->name &= 0xff00ffff; s->name |= val << 16; break; \
1507 case addr + 3: s->name &= 0x00ffffff; s->name |= val << 24; break;
1509 #ifdef DEBUG_LSI_REG
1510 DPRINTF("Write reg %x = %02x\n", offset
, val
);
1513 case 0x00: /* SCNTL0 */
1515 if (val
& LSI_SCNTL0_START
) {
1516 BADF("Start sequence not implemented\n");
1519 case 0x01: /* SCNTL1 */
1520 s
->scntl1
= val
& ~LSI_SCNTL1_SST
;
1521 if (val
& LSI_SCNTL1_IARB
) {
1522 BADF("Immediate Arbritration not implemented\n");
1524 if (val
& LSI_SCNTL1_RST
) {
1525 s
->sstat0
|= LSI_SSTAT0_RST
;
1526 lsi_script_scsi_interrupt(s
, LSI_SIST0_RST
, 0);
1528 s
->sstat0
&= ~LSI_SSTAT0_RST
;
1531 case 0x02: /* SCNTL2 */
1532 val
&= ~(LSI_SCNTL2_WSR
| LSI_SCNTL2_WSS
);
1535 case 0x03: /* SCNTL3 */
1538 case 0x04: /* SCID */
1541 case 0x05: /* SXFER */
1544 case 0x06: /* SDID */
1545 if ((val
& 0xf) != (s
->ssid
& 0xf))
1546 BADF("Destination ID does not match SSID\n");
1547 s
->sdid
= val
& 0xf;
1549 case 0x07: /* GPREG0 */
1551 case 0x08: /* SFBR */
1552 /* The CPU is not allowed to write to this register. However the
1553 SCRIPTS register move instructions are. */
1556 case 0x0a: case 0x0b:
1557 /* Openserver writes to these readonly registers on startup */
1559 case 0x0c: case 0x0d: case 0x0e: case 0x0f:
1560 /* Linux writes to these readonly registers on startup. */
1562 CASE_SET_REG32(dsa
, 0x10)
1563 case 0x14: /* ISTAT0 */
1564 s
->istat0
= (s
->istat0
& 0x0f) | (val
& 0xf0);
1565 if (val
& LSI_ISTAT0_ABRT
) {
1566 lsi_script_dma_interrupt(s
, LSI_DSTAT_ABRT
);
1568 if (val
& LSI_ISTAT0_INTF
) {
1569 s
->istat0
&= ~LSI_ISTAT0_INTF
;
1572 if (s
->waiting
== 1 && val
& LSI_ISTAT0_SIGP
) {
1573 DPRINTF("Woken by SIGP\n");
1576 lsi_execute_script(s
);
1578 if (val
& LSI_ISTAT0_SRST
) {
1582 case 0x16: /* MBOX0 */
1585 case 0x17: /* MBOX1 */
1588 case 0x1a: /* CTEST2 */
1589 s
->ctest2
= val
& LSI_CTEST2_PCICIE
;
1591 case 0x1b: /* CTEST3 */
1592 s
->ctest3
= val
& 0x0f;
1594 CASE_SET_REG32(temp
, 0x1c)
1595 case 0x21: /* CTEST4 */
1597 BADF("Unimplemented CTEST4-FBL 0x%x\n", val
);
1601 case 0x22: /* CTEST5 */
1602 if (val
& (LSI_CTEST5_ADCK
| LSI_CTEST5_BBCK
)) {
1603 BADF("CTEST5 DMA increment not implemented\n");
1607 CASE_SET_REG24(dbc
, 0x24)
1608 CASE_SET_REG32(dnad
, 0x28)
1609 case 0x2c: /* DSP[0:7] */
1610 s
->dsp
&= 0xffffff00;
1613 case 0x2d: /* DSP[8:15] */
1614 s
->dsp
&= 0xffff00ff;
1617 case 0x2e: /* DSP[16:23] */
1618 s
->dsp
&= 0xff00ffff;
1619 s
->dsp
|= val
<< 16;
1621 case 0x2f: /* DSP[24:31] */
1622 s
->dsp
&= 0x00ffffff;
1623 s
->dsp
|= val
<< 24;
1624 if ((s
->dmode
& LSI_DMODE_MAN
) == 0
1625 && (s
->istat1
& LSI_ISTAT1_SRUN
) == 0)
1626 lsi_execute_script(s
);
1628 CASE_SET_REG32(dsps
, 0x30)
1629 CASE_SET_REG32(scratch
[0], 0x34)
1630 case 0x38: /* DMODE */
1631 if (val
& (LSI_DMODE_SIOM
| LSI_DMODE_DIOM
)) {
1632 BADF("IO mappings not implemented\n");
1636 case 0x39: /* DIEN */
1640 case 0x3a: /* SBR */
1643 case 0x3b: /* DCNTL */
1644 s
->dcntl
= val
& ~(LSI_DCNTL_PFF
| LSI_DCNTL_STD
);
1645 if ((val
& LSI_DCNTL_STD
) && (s
->istat1
& LSI_ISTAT1_SRUN
) == 0)
1646 lsi_execute_script(s
);
1648 case 0x40: /* SIEN0 */
1652 case 0x41: /* SIEN1 */
1656 case 0x47: /* GPCNTL0 */
1658 case 0x48: /* STIME0 */
1661 case 0x49: /* STIME1 */
1663 DPRINTF("General purpose timer not implemented\n");
1664 /* ??? Raising the interrupt immediately seems to be sufficient
1665 to keep the FreeBSD driver happy. */
1666 lsi_script_scsi_interrupt(s
, 0, LSI_SIST1_GEN
);
1669 case 0x4a: /* RESPID0 */
1672 case 0x4b: /* RESPID1 */
1675 case 0x4d: /* STEST1 */
1678 case 0x4e: /* STEST2 */
1680 BADF("Low level mode not implemented\n");
1684 case 0x4f: /* STEST3 */
1686 BADF("SCSI FIFO test mode not implemented\n");
1690 case 0x56: /* CCNTL0 */
1693 case 0x57: /* CCNTL1 */
1696 CASE_SET_REG32(mmrs
, 0xa0)
1697 CASE_SET_REG32(mmws
, 0xa4)
1698 CASE_SET_REG32(sfs
, 0xa8)
1699 CASE_SET_REG32(drs
, 0xac)
1700 CASE_SET_REG32(sbms
, 0xb0)
1701 CASE_SET_REG32(dbms
, 0xb4)
1702 CASE_SET_REG32(dnad64
, 0xb8)
1703 CASE_SET_REG32(pmjad1
, 0xc0)
1704 CASE_SET_REG32(pmjad2
, 0xc4)
1705 CASE_SET_REG32(rbc
, 0xc8)
1706 CASE_SET_REG32(ua
, 0xcc)
1707 CASE_SET_REG32(ia
, 0xd4)
1708 CASE_SET_REG32(sbc
, 0xd8)
1709 CASE_SET_REG32(csbc
, 0xdc)
1711 if (offset
>= 0x5c && offset
< 0xa0) {
1714 n
= (offset
- 0x58) >> 2;
1715 shift
= (offset
& 3) * 8;
1716 s
->scratch
[n
] &= ~(0xff << shift
);
1717 s
->scratch
[n
] |= (val
& 0xff) << shift
;
1719 BADF("Unhandled writeb 0x%x = 0x%x\n", offset
, val
);
1722 #undef CASE_SET_REG24
1723 #undef CASE_SET_REG32
1726 static void lsi_mmio_writeb(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1728 LSIState
*s
= opaque
;
1730 lsi_reg_writeb(s
, addr
& 0xff, val
);
1733 static void lsi_mmio_writew(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1735 LSIState
*s
= opaque
;
1738 lsi_reg_writeb(s
, addr
, val
& 0xff);
1739 lsi_reg_writeb(s
, addr
+ 1, (val
>> 8) & 0xff);
1742 static void lsi_mmio_writel(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1744 LSIState
*s
= opaque
;
1747 lsi_reg_writeb(s
, addr
, val
& 0xff);
1748 lsi_reg_writeb(s
, addr
+ 1, (val
>> 8) & 0xff);
1749 lsi_reg_writeb(s
, addr
+ 2, (val
>> 16) & 0xff);
1750 lsi_reg_writeb(s
, addr
+ 3, (val
>> 24) & 0xff);
1753 static uint32_t lsi_mmio_readb(void *opaque
, target_phys_addr_t addr
)
1755 LSIState
*s
= opaque
;
1757 return lsi_reg_readb(s
, addr
& 0xff);
1760 static uint32_t lsi_mmio_readw(void *opaque
, target_phys_addr_t addr
)
1762 LSIState
*s
= opaque
;
1766 val
= lsi_reg_readb(s
, addr
);
1767 val
|= lsi_reg_readb(s
, addr
+ 1) << 8;
1771 static uint32_t lsi_mmio_readl(void *opaque
, target_phys_addr_t addr
)
1773 LSIState
*s
= opaque
;
1776 val
= lsi_reg_readb(s
, addr
);
1777 val
|= lsi_reg_readb(s
, addr
+ 1) << 8;
1778 val
|= lsi_reg_readb(s
, addr
+ 2) << 16;
1779 val
|= lsi_reg_readb(s
, addr
+ 3) << 24;
1783 static CPUReadMemoryFunc
* const lsi_mmio_readfn
[3] = {
1789 static CPUWriteMemoryFunc
* const lsi_mmio_writefn
[3] = {
1795 static void lsi_ram_writeb(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1797 LSIState
*s
= opaque
;
1802 newval
= s
->script_ram
[addr
>> 2];
1803 shift
= (addr
& 3) * 8;
1804 newval
&= ~(0xff << shift
);
1805 newval
|= val
<< shift
;
1806 s
->script_ram
[addr
>> 2] = newval
;
1809 static void lsi_ram_writew(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1811 LSIState
*s
= opaque
;
1815 newval
= s
->script_ram
[addr
>> 2];
1817 newval
= (newval
& 0xffff) | (val
<< 16);
1819 newval
= (newval
& 0xffff0000) | val
;
1821 s
->script_ram
[addr
>> 2] = newval
;
1825 static void lsi_ram_writel(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1827 LSIState
*s
= opaque
;
1830 s
->script_ram
[addr
>> 2] = val
;
1833 static uint32_t lsi_ram_readb(void *opaque
, target_phys_addr_t addr
)
1835 LSIState
*s
= opaque
;
1839 val
= s
->script_ram
[addr
>> 2];
1840 val
>>= (addr
& 3) * 8;
1844 static uint32_t lsi_ram_readw(void *opaque
, target_phys_addr_t addr
)
1846 LSIState
*s
= opaque
;
1850 val
= s
->script_ram
[addr
>> 2];
1853 return le16_to_cpu(val
);
1856 static uint32_t lsi_ram_readl(void *opaque
, target_phys_addr_t addr
)
1858 LSIState
*s
= opaque
;
1861 return le32_to_cpu(s
->script_ram
[addr
>> 2]);
1864 static CPUReadMemoryFunc
* const lsi_ram_readfn
[3] = {
1870 static CPUWriteMemoryFunc
* const lsi_ram_writefn
[3] = {
1876 static uint32_t lsi_io_readb(void *opaque
, uint32_t addr
)
1878 LSIState
*s
= opaque
;
1879 return lsi_reg_readb(s
, addr
& 0xff);
1882 static uint32_t lsi_io_readw(void *opaque
, uint32_t addr
)
1884 LSIState
*s
= opaque
;
1887 val
= lsi_reg_readb(s
, addr
);
1888 val
|= lsi_reg_readb(s
, addr
+ 1) << 8;
1892 static uint32_t lsi_io_readl(void *opaque
, uint32_t addr
)
1894 LSIState
*s
= opaque
;
1897 val
= lsi_reg_readb(s
, addr
);
1898 val
|= lsi_reg_readb(s
, addr
+ 1) << 8;
1899 val
|= lsi_reg_readb(s
, addr
+ 2) << 16;
1900 val
|= lsi_reg_readb(s
, addr
+ 3) << 24;
1904 static void lsi_io_writeb(void *opaque
, uint32_t addr
, uint32_t val
)
1906 LSIState
*s
= opaque
;
1907 lsi_reg_writeb(s
, addr
& 0xff, val
);
1910 static void lsi_io_writew(void *opaque
, uint32_t addr
, uint32_t val
)
1912 LSIState
*s
= opaque
;
1914 lsi_reg_writeb(s
, addr
, val
& 0xff);
1915 lsi_reg_writeb(s
, addr
+ 1, (val
>> 8) & 0xff);
1918 static void lsi_io_writel(void *opaque
, uint32_t addr
, uint32_t val
)
1920 LSIState
*s
= opaque
;
1922 lsi_reg_writeb(s
, addr
, val
& 0xff);
1923 lsi_reg_writeb(s
, addr
+ 1, (val
>> 8) & 0xff);
1924 lsi_reg_writeb(s
, addr
+ 2, (val
>> 16) & 0xff);
1925 lsi_reg_writeb(s
, addr
+ 3, (val
>> 24) & 0xff);
1928 static void lsi_io_mapfunc(PCIDevice
*pci_dev
, int region_num
,
1929 uint32_t addr
, uint32_t size
, int type
)
1931 LSIState
*s
= DO_UPCAST(LSIState
, dev
, pci_dev
);
1933 DPRINTF("Mapping IO at %08x\n", addr
);
1935 register_ioport_write(addr
, 256, 1, lsi_io_writeb
, s
);
1936 register_ioport_read(addr
, 256, 1, lsi_io_readb
, s
);
1937 register_ioport_write(addr
, 256, 2, lsi_io_writew
, s
);
1938 register_ioport_read(addr
, 256, 2, lsi_io_readw
, s
);
1939 register_ioport_write(addr
, 256, 4, lsi_io_writel
, s
);
1940 register_ioport_read(addr
, 256, 4, lsi_io_readl
, s
);
1943 static void lsi_ram_mapfunc(PCIDevice
*pci_dev
, int region_num
,
1944 uint32_t addr
, uint32_t size
, int type
)
1946 LSIState
*s
= DO_UPCAST(LSIState
, dev
, pci_dev
);
1948 DPRINTF("Mapping ram at %08x\n", addr
);
1949 s
->script_ram_base
= addr
;
1950 cpu_register_physical_memory(addr
+ 0, 0x2000, s
->ram_io_addr
);
1953 static void lsi_mmio_mapfunc(PCIDevice
*pci_dev
, int region_num
,
1954 uint32_t addr
, uint32_t size
, int type
)
1956 LSIState
*s
= DO_UPCAST(LSIState
, dev
, pci_dev
);
1958 DPRINTF("Mapping registers at %08x\n", addr
);
1959 cpu_register_physical_memory(addr
+ 0, 0x400, s
->mmio_io_addr
);
1962 static void lsi_scsi_save(QEMUFile
*f
, void *opaque
)
1964 LSIState
*s
= opaque
;
1966 assert(s
->dma_buf
== NULL
);
1967 assert(s
->current_dma_len
== 0);
1968 assert(s
->active_commands
== 0);
1970 pci_device_save(&s
->dev
, f
);
1972 qemu_put_sbe32s(f
, &s
->carry
);
1973 qemu_put_sbe32s(f
, &s
->sense
);
1974 qemu_put_sbe32s(f
, &s
->msg_action
);
1975 qemu_put_sbe32s(f
, &s
->msg_len
);
1976 qemu_put_buffer(f
, s
->msg
, sizeof (s
->msg
));
1977 qemu_put_sbe32s(f
, &s
->waiting
);
1979 qemu_put_be32s(f
, &s
->dsa
);
1980 qemu_put_be32s(f
, &s
->temp
);
1981 qemu_put_be32s(f
, &s
->dnad
);
1982 qemu_put_be32s(f
, &s
->dbc
);
1983 qemu_put_8s(f
, &s
->istat0
);
1984 qemu_put_8s(f
, &s
->istat1
);
1985 qemu_put_8s(f
, &s
->dcmd
);
1986 qemu_put_8s(f
, &s
->dstat
);
1987 qemu_put_8s(f
, &s
->dien
);
1988 qemu_put_8s(f
, &s
->sist0
);
1989 qemu_put_8s(f
, &s
->sist1
);
1990 qemu_put_8s(f
, &s
->sien0
);
1991 qemu_put_8s(f
, &s
->sien1
);
1992 qemu_put_8s(f
, &s
->mbox0
);
1993 qemu_put_8s(f
, &s
->mbox1
);
1994 qemu_put_8s(f
, &s
->dfifo
);
1995 qemu_put_8s(f
, &s
->ctest2
);
1996 qemu_put_8s(f
, &s
->ctest3
);
1997 qemu_put_8s(f
, &s
->ctest4
);
1998 qemu_put_8s(f
, &s
->ctest5
);
1999 qemu_put_8s(f
, &s
->ccntl0
);
2000 qemu_put_8s(f
, &s
->ccntl1
);
2001 qemu_put_be32s(f
, &s
->dsp
);
2002 qemu_put_be32s(f
, &s
->dsps
);
2003 qemu_put_8s(f
, &s
->dmode
);
2004 qemu_put_8s(f
, &s
->dcntl
);
2005 qemu_put_8s(f
, &s
->scntl0
);
2006 qemu_put_8s(f
, &s
->scntl1
);
2007 qemu_put_8s(f
, &s
->scntl2
);
2008 qemu_put_8s(f
, &s
->scntl3
);
2009 qemu_put_8s(f
, &s
->sstat0
);
2010 qemu_put_8s(f
, &s
->sstat1
);
2011 qemu_put_8s(f
, &s
->scid
);
2012 qemu_put_8s(f
, &s
->sxfer
);
2013 qemu_put_8s(f
, &s
->socl
);
2014 qemu_put_8s(f
, &s
->sdid
);
2015 qemu_put_8s(f
, &s
->ssid
);
2016 qemu_put_8s(f
, &s
->sfbr
);
2017 qemu_put_8s(f
, &s
->stest1
);
2018 qemu_put_8s(f
, &s
->stest2
);
2019 qemu_put_8s(f
, &s
->stest3
);
2020 qemu_put_8s(f
, &s
->sidl
);
2021 qemu_put_8s(f
, &s
->stime0
);
2022 qemu_put_8s(f
, &s
->respid0
);
2023 qemu_put_8s(f
, &s
->respid1
);
2024 qemu_put_be32s(f
, &s
->mmrs
);
2025 qemu_put_be32s(f
, &s
->mmws
);
2026 qemu_put_be32s(f
, &s
->sfs
);
2027 qemu_put_be32s(f
, &s
->drs
);
2028 qemu_put_be32s(f
, &s
->sbms
);
2029 qemu_put_be32s(f
, &s
->dbms
);
2030 qemu_put_be32s(f
, &s
->dnad64
);
2031 qemu_put_be32s(f
, &s
->pmjad1
);
2032 qemu_put_be32s(f
, &s
->pmjad2
);
2033 qemu_put_be32s(f
, &s
->rbc
);
2034 qemu_put_be32s(f
, &s
->ua
);
2035 qemu_put_be32s(f
, &s
->ia
);
2036 qemu_put_be32s(f
, &s
->sbc
);
2037 qemu_put_be32s(f
, &s
->csbc
);
2038 qemu_put_buffer(f
, (uint8_t *)s
->scratch
, sizeof (s
->scratch
));
2039 qemu_put_8s(f
, &s
->sbr
);
2041 qemu_put_buffer(f
, (uint8_t *)s
->script_ram
, sizeof (s
->script_ram
));
2044 static int lsi_scsi_load(QEMUFile
*f
, void *opaque
, int version_id
)
2046 LSIState
*s
= opaque
;
2049 if (version_id
> 0) {
2053 if ((ret
= pci_device_load(&s
->dev
, f
)) < 0)
2056 qemu_get_sbe32s(f
, &s
->carry
);
2057 qemu_get_sbe32s(f
, &s
->sense
);
2058 qemu_get_sbe32s(f
, &s
->msg_action
);
2059 qemu_get_sbe32s(f
, &s
->msg_len
);
2060 qemu_get_buffer(f
, s
->msg
, sizeof (s
->msg
));
2061 qemu_get_sbe32s(f
, &s
->waiting
);
2063 qemu_get_be32s(f
, &s
->dsa
);
2064 qemu_get_be32s(f
, &s
->temp
);
2065 qemu_get_be32s(f
, &s
->dnad
);
2066 qemu_get_be32s(f
, &s
->dbc
);
2067 qemu_get_8s(f
, &s
->istat0
);
2068 qemu_get_8s(f
, &s
->istat1
);
2069 qemu_get_8s(f
, &s
->dcmd
);
2070 qemu_get_8s(f
, &s
->dstat
);
2071 qemu_get_8s(f
, &s
->dien
);
2072 qemu_get_8s(f
, &s
->sist0
);
2073 qemu_get_8s(f
, &s
->sist1
);
2074 qemu_get_8s(f
, &s
->sien0
);
2075 qemu_get_8s(f
, &s
->sien1
);
2076 qemu_get_8s(f
, &s
->mbox0
);
2077 qemu_get_8s(f
, &s
->mbox1
);
2078 qemu_get_8s(f
, &s
->dfifo
);
2079 qemu_get_8s(f
, &s
->ctest2
);
2080 qemu_get_8s(f
, &s
->ctest3
);
2081 qemu_get_8s(f
, &s
->ctest4
);
2082 qemu_get_8s(f
, &s
->ctest5
);
2083 qemu_get_8s(f
, &s
->ccntl0
);
2084 qemu_get_8s(f
, &s
->ccntl1
);
2085 qemu_get_be32s(f
, &s
->dsp
);
2086 qemu_get_be32s(f
, &s
->dsps
);
2087 qemu_get_8s(f
, &s
->dmode
);
2088 qemu_get_8s(f
, &s
->dcntl
);
2089 qemu_get_8s(f
, &s
->scntl0
);
2090 qemu_get_8s(f
, &s
->scntl1
);
2091 qemu_get_8s(f
, &s
->scntl2
);
2092 qemu_get_8s(f
, &s
->scntl3
);
2093 qemu_get_8s(f
, &s
->sstat0
);
2094 qemu_get_8s(f
, &s
->sstat1
);
2095 qemu_get_8s(f
, &s
->scid
);
2096 qemu_get_8s(f
, &s
->sxfer
);
2097 qemu_get_8s(f
, &s
->socl
);
2098 qemu_get_8s(f
, &s
->sdid
);
2099 qemu_get_8s(f
, &s
->ssid
);
2100 qemu_get_8s(f
, &s
->sfbr
);
2101 qemu_get_8s(f
, &s
->stest1
);
2102 qemu_get_8s(f
, &s
->stest2
);
2103 qemu_get_8s(f
, &s
->stest3
);
2104 qemu_get_8s(f
, &s
->sidl
);
2105 qemu_get_8s(f
, &s
->stime0
);
2106 qemu_get_8s(f
, &s
->respid0
);
2107 qemu_get_8s(f
, &s
->respid1
);
2108 qemu_get_be32s(f
, &s
->mmrs
);
2109 qemu_get_be32s(f
, &s
->mmws
);
2110 qemu_get_be32s(f
, &s
->sfs
);
2111 qemu_get_be32s(f
, &s
->drs
);
2112 qemu_get_be32s(f
, &s
->sbms
);
2113 qemu_get_be32s(f
, &s
->dbms
);
2114 qemu_get_be32s(f
, &s
->dnad64
);
2115 qemu_get_be32s(f
, &s
->pmjad1
);
2116 qemu_get_be32s(f
, &s
->pmjad2
);
2117 qemu_get_be32s(f
, &s
->rbc
);
2118 qemu_get_be32s(f
, &s
->ua
);
2119 qemu_get_be32s(f
, &s
->ia
);
2120 qemu_get_be32s(f
, &s
->sbc
);
2121 qemu_get_be32s(f
, &s
->csbc
);
2122 qemu_get_buffer(f
, (uint8_t *)s
->scratch
, sizeof (s
->scratch
));
2123 qemu_get_8s(f
, &s
->sbr
);
2125 qemu_get_buffer(f
, (uint8_t *)s
->script_ram
, sizeof (s
->script_ram
));
2130 static int lsi_scsi_uninit(PCIDevice
*d
)
2132 LSIState
*s
= DO_UPCAST(LSIState
, dev
, d
);
2134 cpu_unregister_io_memory(s
->mmio_io_addr
);
2135 cpu_unregister_io_memory(s
->ram_io_addr
);
2137 qemu_free(s
->queue
);
2142 static int lsi_scsi_init(PCIDevice
*dev
)
2144 LSIState
*s
= DO_UPCAST(LSIState
, dev
, dev
);
2147 pci_conf
= s
->dev
.config
;
2149 /* PCI Vendor ID (word) */
2150 pci_config_set_vendor_id(pci_conf
, PCI_VENDOR_ID_LSI_LOGIC
);
2151 /* PCI device ID (word) */
2152 pci_config_set_device_id(pci_conf
, PCI_DEVICE_ID_LSI_53C895A
);
2153 /* PCI base class code */
2154 pci_config_set_class(pci_conf
, PCI_CLASS_STORAGE_SCSI
);
2155 /* PCI subsystem ID */
2156 pci_conf
[0x2e] = 0x00;
2157 pci_conf
[0x2f] = 0x10;
2158 /* PCI latency timer = 255 */
2159 pci_conf
[0x0d] = 0xff;
2160 /* Interrupt pin 1 */
2161 pci_conf
[0x3d] = 0x01;
2163 s
->mmio_io_addr
= cpu_register_io_memory(lsi_mmio_readfn
,
2164 lsi_mmio_writefn
, s
);
2165 s
->ram_io_addr
= cpu_register_io_memory(lsi_ram_readfn
,
2166 lsi_ram_writefn
, s
);
2168 pci_register_bar((struct PCIDevice
*)s
, 0, 256,
2169 PCI_ADDRESS_SPACE_IO
, lsi_io_mapfunc
);
2170 pci_register_bar((struct PCIDevice
*)s
, 1, 0x400,
2171 PCI_ADDRESS_SPACE_MEM
, lsi_mmio_mapfunc
);
2172 pci_register_bar((struct PCIDevice
*)s
, 2, 0x2000,
2173 PCI_ADDRESS_SPACE_MEM
, lsi_ram_mapfunc
);
2174 s
->queue
= qemu_malloc(sizeof(lsi_queue
));
2176 s
->active_commands
= 0;
2177 s
->dev
.unregister
= lsi_scsi_uninit
;
2181 s
->bus
= scsi_bus_new(&dev
->qdev
, 1, LSI_MAX_DEVS
, lsi_command_complete
);
2182 scsi_bus_legacy_handle_cmdline(s
->bus
);
2183 register_savevm("lsiscsi", -1, 0, lsi_scsi_save
, lsi_scsi_load
, s
);
2187 static PCIDeviceInfo lsi_info
= {
2188 .qdev
.name
= "lsi53c895a",
2189 .qdev
.alias
= "lsi",
2190 .qdev
.size
= sizeof(LSIState
),
2191 .init
= lsi_scsi_init
,
2194 static void lsi53c895a_register_devices(void)
2196 pci_qdev_register(&lsi_info
);
2199 device_init(lsi53c895a_register_devices
);