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. */
17 #include "scsi-disk.h"
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_SCNTL0_TRG 0x01
35 #define LSI_SCNTL0_AAP 0x02
36 #define LSI_SCNTL0_EPC 0x08
37 #define LSI_SCNTL0_WATN 0x10
38 #define LSI_SCNTL0_START 0x20
40 #define LSI_SCNTL1_SST 0x01
41 #define LSI_SCNTL1_IARB 0x02
42 #define LSI_SCNTL1_AESP 0x04
43 #define LSI_SCNTL1_RST 0x08
44 #define LSI_SCNTL1_CON 0x10
45 #define LSI_SCNTL1_DHP 0x20
46 #define LSI_SCNTL1_ADB 0x40
47 #define LSI_SCNTL1_EXC 0x80
49 #define LSI_SCNTL2_WSR 0x01
50 #define LSI_SCNTL2_VUE0 0x02
51 #define LSI_SCNTL2_VUE1 0x04
52 #define LSI_SCNTL2_WSS 0x08
53 #define LSI_SCNTL2_SLPHBEN 0x10
54 #define LSI_SCNTL2_SLPMD 0x20
55 #define LSI_SCNTL2_CHM 0x40
56 #define LSI_SCNTL2_SDU 0x80
58 #define LSI_ISTAT0_DIP 0x01
59 #define LSI_ISTAT0_SIP 0x02
60 #define LSI_ISTAT0_INTF 0x04
61 #define LSI_ISTAT0_CON 0x08
62 #define LSI_ISTAT0_SEM 0x10
63 #define LSI_ISTAT0_SIGP 0x20
64 #define LSI_ISTAT0_SRST 0x40
65 #define LSI_ISTAT0_ABRT 0x80
67 #define LSI_ISTAT1_SI 0x01
68 #define LSI_ISTAT1_SRUN 0x02
69 #define LSI_ISTAT1_FLSH 0x04
71 #define LSI_SSTAT0_SDP0 0x01
72 #define LSI_SSTAT0_RST 0x02
73 #define LSI_SSTAT0_WOA 0x04
74 #define LSI_SSTAT0_LOA 0x08
75 #define LSI_SSTAT0_AIP 0x10
76 #define LSI_SSTAT0_OLF 0x20
77 #define LSI_SSTAT0_ORF 0x40
78 #define LSI_SSTAT0_ILF 0x80
80 #define LSI_SIST0_PAR 0x01
81 #define LSI_SIST0_RST 0x02
82 #define LSI_SIST0_UDC 0x04
83 #define LSI_SIST0_SGE 0x08
84 #define LSI_SIST0_RSL 0x10
85 #define LSI_SIST0_SEL 0x20
86 #define LSI_SIST0_CMP 0x40
87 #define LSI_SIST0_MA 0x80
89 #define LSI_SIST1_HTH 0x01
90 #define LSI_SIST1_GEN 0x02
91 #define LSI_SIST1_STO 0x04
92 #define LSI_SIST1_SBMC 0x10
94 #define LSI_SOCL_IO 0x01
95 #define LSI_SOCL_CD 0x02
96 #define LSI_SOCL_MSG 0x04
97 #define LSI_SOCL_ATN 0x08
98 #define LSI_SOCL_SEL 0x10
99 #define LSI_SOCL_BSY 0x20
100 #define LSI_SOCL_ACK 0x40
101 #define LSI_SOCL_REQ 0x80
103 #define LSI_DSTAT_IID 0x01
104 #define LSI_DSTAT_SIR 0x04
105 #define LSI_DSTAT_SSI 0x08
106 #define LSI_DSTAT_ABRT 0x10
107 #define LSI_DSTAT_BF 0x20
108 #define LSI_DSTAT_MDPE 0x40
109 #define LSI_DSTAT_DFE 0x80
111 #define LSI_DCNTL_COM 0x01
112 #define LSI_DCNTL_IRQD 0x02
113 #define LSI_DCNTL_STD 0x04
114 #define LSI_DCNTL_IRQM 0x08
115 #define LSI_DCNTL_SSM 0x10
116 #define LSI_DCNTL_PFEN 0x20
117 #define LSI_DCNTL_PFF 0x40
118 #define LSI_DCNTL_CLSE 0x80
120 #define LSI_DMODE_MAN 0x01
121 #define LSI_DMODE_BOF 0x02
122 #define LSI_DMODE_ERMP 0x04
123 #define LSI_DMODE_ERL 0x08
124 #define LSI_DMODE_DIOM 0x10
125 #define LSI_DMODE_SIOM 0x20
127 #define LSI_CTEST2_DACK 0x01
128 #define LSI_CTEST2_DREQ 0x02
129 #define LSI_CTEST2_TEOP 0x04
130 #define LSI_CTEST2_PCICIE 0x08
131 #define LSI_CTEST2_CM 0x10
132 #define LSI_CTEST2_CIO 0x20
133 #define LSI_CTEST2_SIGP 0x40
134 #define LSI_CTEST2_DDIR 0x80
136 #define LSI_CTEST5_BL2 0x04
137 #define LSI_CTEST5_DDIR 0x08
138 #define LSI_CTEST5_MASR 0x10
139 #define LSI_CTEST5_DFSN 0x20
140 #define LSI_CTEST5_BBCK 0x40
141 #define LSI_CTEST5_ADCK 0x80
143 #define LSI_CCNTL0_DILS 0x01
144 #define LSI_CCNTL0_DISFC 0x10
145 #define LSI_CCNTL0_ENNDJ 0x20
146 #define LSI_CCNTL0_PMJCTL 0x40
147 #define LSI_CCNTL0_ENPMJ 0x80
149 #define LSI_CCNTL1_EN64DBMV 0x01
150 #define LSI_CCNTL1_EN64TIBMV 0x02
151 #define LSI_CCNTL1_64TIMOD 0x04
152 #define LSI_CCNTL1_DDAC 0x08
153 #define LSI_CCNTL1_ZMOD 0x80
155 #define LSI_CCNTL1_40BIT (LSI_CCNTL1_EN64TIBMV|LSI_CCNTL1_64TIMOD)
165 /* Maximum length of MSG IN data. */
166 #define LSI_MAX_MSGIN_LEN 8
168 /* Flag set if this is a tagged command. */
169 #define LSI_TAG_VALID (1 << 16)
181 uint32_t script_ram_base
;
183 int carry
; /* ??? Should this be an a visible register somewhere? */
185 /* Action to take at the end of a MSG IN phase.
186 0 = COMMAND, 1 = disconect, 2 = DATA OUT, 3 = DATA IN. */
189 uint8_t msg
[LSI_MAX_MSGIN_LEN
];
190 /* 0 if SCRIPTS are running or stopped.
191 * 1 if a Wait Reselect instruction has been issued.
192 * 2 if processing DMA from lsi_execute_script.
193 * 3 if a DMA operation is in progress. */
195 SCSIDevice
*scsi_dev
[LSI_MAX_DEVS
];
196 SCSIDevice
*current_dev
;
198 /* The tag is a combination of the device ID and the SCSI tag. */
199 uint32_t current_tag
;
200 uint32_t current_dma_len
;
201 int command_complete
;
266 uint32_t scratch
[18]; /* SCRATCHA-SCRATCHR */
269 /* Script ram is stored as 32-bit words in host byteorder. */
270 uint32_t script_ram
[2048];
273 static void lsi_soft_reset(LSIState
*s
)
283 memset(s
->scratch
, 0, sizeof(s
->scratch
));
339 static int lsi_dma_40bit(LSIState
*s
)
341 if ((s
->ccntl1
& LSI_CCNTL1_40BIT
) == LSI_CCNTL1_40BIT
)
346 static int lsi_dma_ti64bit(LSIState
*s
)
348 if ((s
->ccntl1
& LSI_CCNTL1_EN64TIBMV
) == LSI_CCNTL1_EN64TIBMV
)
353 static int lsi_dma_64bit(LSIState
*s
)
355 if ((s
->ccntl1
& LSI_CCNTL1_EN64DBMV
) == LSI_CCNTL1_EN64DBMV
)
360 static uint8_t lsi_reg_readb(LSIState
*s
, int offset
);
361 static void lsi_reg_writeb(LSIState
*s
, int offset
, uint8_t val
);
362 static void lsi_execute_script(LSIState
*s
);
364 static inline uint32_t read_dword(LSIState
*s
, uint32_t addr
)
368 /* Optimize reading from SCRIPTS RAM. */
369 if ((addr
& 0xffffe000) == s
->script_ram_base
) {
370 return s
->script_ram
[(addr
& 0x1fff) >> 2];
372 cpu_physical_memory_read(addr
, (uint8_t *)&buf
, 4);
373 return cpu_to_le32(buf
);
376 static void lsi_stop_script(LSIState
*s
)
378 s
->istat1
&= ~LSI_ISTAT1_SRUN
;
381 static void lsi_update_irq(LSIState
*s
)
384 static int last_level
;
386 /* It's unclear whether the DIP/SIP bits should be cleared when the
387 Interrupt Status Registers are cleared or when istat0 is read.
388 We currently do the formwer, which seems to work. */
391 if (s
->dstat
& s
->dien
)
393 s
->istat0
|= LSI_ISTAT0_DIP
;
395 s
->istat0
&= ~LSI_ISTAT0_DIP
;
398 if (s
->sist0
|| s
->sist1
) {
399 if ((s
->sist0
& s
->sien0
) || (s
->sist1
& s
->sien1
))
401 s
->istat0
|= LSI_ISTAT0_SIP
;
403 s
->istat0
&= ~LSI_ISTAT0_SIP
;
405 if (s
->istat0
& LSI_ISTAT0_INTF
)
408 if (level
!= last_level
) {
409 DPRINTF("Update IRQ level %d dstat %02x sist %02x%02x\n",
410 level
, s
->dstat
, s
->sist1
, s
->sist0
);
413 qemu_set_irq(s
->pci_dev
.irq
[0], level
);
416 /* Stop SCRIPTS execution and raise a SCSI interrupt. */
417 static void lsi_script_scsi_interrupt(LSIState
*s
, int stat0
, int stat1
)
422 DPRINTF("SCSI Interrupt 0x%02x%02x prev 0x%02x%02x\n",
423 stat1
, stat0
, s
->sist1
, s
->sist0
);
426 /* Stop processor on fatal or unmasked interrupt. As a special hack
427 we don't stop processing when raising STO. Instead continue
428 execution and stop at the next insn that accesses the SCSI bus. */
429 mask0
= s
->sien0
| ~(LSI_SIST0_CMP
| LSI_SIST0_SEL
| LSI_SIST0_RSL
);
430 mask1
= s
->sien1
| ~(LSI_SIST1_GEN
| LSI_SIST1_HTH
);
431 mask1
&= ~LSI_SIST1_STO
;
432 if (s
->sist0
& mask0
|| s
->sist1
& mask1
) {
438 /* Stop SCRIPTS execution and raise a DMA interrupt. */
439 static void lsi_script_dma_interrupt(LSIState
*s
, int stat
)
441 DPRINTF("DMA Interrupt 0x%x prev 0x%x\n", stat
, s
->dstat
);
447 static inline void lsi_set_phase(LSIState
*s
, int phase
)
449 s
->sstat1
= (s
->sstat1
& ~PHASE_MASK
) | phase
;
452 static void lsi_bad_phase(LSIState
*s
, int out
, int new_phase
)
454 /* Trigger a phase mismatch. */
455 if (s
->ccntl0
& LSI_CCNTL0_ENPMJ
) {
456 if ((s
->ccntl0
& LSI_CCNTL0_PMJCTL
) || out
) {
461 DPRINTF("Data phase mismatch jump to %08x\n", s
->dsp
);
463 DPRINTF("Phase mismatch interrupt\n");
464 lsi_script_scsi_interrupt(s
, LSI_SIST0_MA
, 0);
467 lsi_set_phase(s
, new_phase
);
471 /* Resume SCRIPTS execution after a DMA operation. */
472 static void lsi_resume_script(LSIState
*s
)
474 if (s
->waiting
!= 2) {
476 lsi_execute_script(s
);
482 /* Initiate a SCSI layer data transfer. */
483 static void lsi_do_dma(LSIState
*s
, int out
)
486 target_phys_addr_t addr
;
488 if (!s
->current_dma_len
) {
489 /* Wait until data is available. */
490 DPRINTF("DMA no data available\n");
495 if (count
> s
->current_dma_len
)
496 count
= s
->current_dma_len
;
499 /* both 40 and Table Indirect 64-bit DMAs store upper bits in dnad64 */
500 if (lsi_dma_40bit(s
) || lsi_dma_ti64bit(s
))
501 addr
|= ((uint64_t)s
->dnad64
<< 32);
503 addr
|= ((uint64_t)s
->dbms
<< 32);
505 addr
|= ((uint64_t)s
->sbms
<< 32);
507 DPRINTF("DMA addr=0x" TARGET_FMT_plx
" len=%d\n", addr
, count
);
512 if (s
->dma_buf
== NULL
) {
513 s
->dma_buf
= s
->current_dev
->get_buf(s
->current_dev
,
517 /* ??? Set SFBR to first data byte. */
519 cpu_physical_memory_read(addr
, s
->dma_buf
, count
);
521 cpu_physical_memory_write(addr
, s
->dma_buf
, count
);
523 s
->current_dma_len
-= count
;
524 if (s
->current_dma_len
== 0) {
527 /* Write the data. */
528 s
->current_dev
->write_data(s
->current_dev
, s
->current_tag
);
530 /* Request any remaining data. */
531 s
->current_dev
->read_data(s
->current_dev
, s
->current_tag
);
535 lsi_resume_script(s
);
540 /* Add a command to the queue. */
541 static void lsi_queue_command(LSIState
*s
)
545 DPRINTF("Queueing tag=0x%x\n", s
->current_tag
);
546 if (s
->queue_len
== s
->active_commands
) {
548 s
->queue
= qemu_realloc(s
->queue
, s
->queue_len
* sizeof(lsi_queue
));
550 p
= &s
->queue
[s
->active_commands
++];
551 p
->tag
= s
->current_tag
;
553 p
->out
= (s
->sstat1
& PHASE_MASK
) == PHASE_DO
;
556 /* Queue a byte for a MSG IN phase. */
557 static void lsi_add_msg_byte(LSIState
*s
, uint8_t data
)
559 if (s
->msg_len
>= LSI_MAX_MSGIN_LEN
) {
560 BADF("MSG IN data too long\n");
562 DPRINTF("MSG IN 0x%02x\n", data
);
563 s
->msg
[s
->msg_len
++] = data
;
567 /* Perform reselection to continue a command. */
568 static void lsi_reselect(LSIState
*s
, uint32_t tag
)
575 for (n
= 0; n
< s
->active_commands
; n
++) {
580 if (n
== s
->active_commands
) {
581 BADF("Reselected non-existant command tag=0x%x\n", tag
);
584 id
= (tag
>> 8) & 0xf;
586 DPRINTF("Reselected target %d\n", id
);
587 s
->current_dev
= s
->scsi_dev
[id
];
588 s
->current_tag
= tag
;
589 s
->scntl1
|= LSI_SCNTL1_CON
;
590 lsi_set_phase(s
, PHASE_MI
);
591 s
->msg_action
= p
->out
? 2 : 3;
592 s
->current_dma_len
= p
->pending
;
594 lsi_add_msg_byte(s
, 0x80);
595 if (s
->current_tag
& LSI_TAG_VALID
) {
596 lsi_add_msg_byte(s
, 0x20);
597 lsi_add_msg_byte(s
, tag
& 0xff);
600 s
->active_commands
--;
601 if (n
!= s
->active_commands
) {
602 s
->queue
[n
] = s
->queue
[s
->active_commands
];
606 /* Record that data is available for a queued command. Returns zero if
607 the device was reselected, nonzero if the IO is deferred. */
608 static int lsi_queue_tag(LSIState
*s
, uint32_t tag
, uint32_t arg
)
612 for (i
= 0; i
< s
->active_commands
; i
++) {
616 BADF("Multiple IO pending for tag %d\n", tag
);
619 if (s
->waiting
== 1) {
620 /* Reselect device. */
621 lsi_reselect(s
, tag
);
624 DPRINTF("Queueing IO tag=0x%x\n", tag
);
630 BADF("IO with unknown tag %d\n", tag
);
634 /* Callback to indicate that the SCSI layer has completed a transfer. */
635 static void lsi_command_complete(void *opaque
, int reason
, uint32_t tag
,
638 LSIState
*s
= (LSIState
*)opaque
;
641 out
= (s
->sstat1
& PHASE_MASK
) == PHASE_DO
;
642 if (reason
== SCSI_REASON_DONE
) {
643 DPRINTF("Command complete sense=%d\n", (int)arg
);
645 s
->command_complete
= 2;
646 if (s
->waiting
&& s
->dbc
!= 0) {
647 /* Raise phase mismatch for short transfers. */
648 lsi_bad_phase(s
, out
, PHASE_ST
);
650 lsi_set_phase(s
, PHASE_ST
);
652 lsi_resume_script(s
);
656 if (s
->waiting
== 1 || tag
!= s
->current_tag
) {
657 if (lsi_queue_tag(s
, tag
, arg
))
660 DPRINTF("Data ready tag=0x%x len=%d\n", tag
, arg
);
661 s
->current_dma_len
= arg
;
662 s
->command_complete
= 1;
665 if (s
->waiting
== 1 || s
->dbc
== 0) {
666 lsi_resume_script(s
);
672 static void lsi_do_command(LSIState
*s
)
677 DPRINTF("Send command len=%d\n", s
->dbc
);
680 cpu_physical_memory_read(s
->dnad
, buf
, s
->dbc
);
682 s
->command_complete
= 0;
683 n
= s
->current_dev
->send_command(s
->current_dev
, s
->current_tag
, buf
,
686 lsi_set_phase(s
, PHASE_DI
);
687 s
->current_dev
->read_data(s
->current_dev
, s
->current_tag
);
689 lsi_set_phase(s
, PHASE_DO
);
690 s
->current_dev
->write_data(s
->current_dev
, s
->current_tag
);
693 if (!s
->command_complete
) {
695 /* Command did not complete immediately so disconnect. */
696 lsi_add_msg_byte(s
, 2); /* SAVE DATA POINTER */
697 lsi_add_msg_byte(s
, 4); /* DISCONNECT */
699 lsi_set_phase(s
, PHASE_MI
);
701 lsi_queue_command(s
);
703 /* wait command complete */
704 lsi_set_phase(s
, PHASE_DI
);
709 static void lsi_do_status(LSIState
*s
)
712 DPRINTF("Get status len=%d sense=%d\n", s
->dbc
, s
->sense
);
714 BADF("Bad Status move\n");
718 cpu_physical_memory_write(s
->dnad
, &sense
, 1);
719 lsi_set_phase(s
, PHASE_MI
);
721 lsi_add_msg_byte(s
, 0); /* COMMAND COMPLETE */
724 static void lsi_disconnect(LSIState
*s
)
726 s
->scntl1
&= ~LSI_SCNTL1_CON
;
727 s
->sstat1
&= ~PHASE_MASK
;
730 static void lsi_do_msgin(LSIState
*s
)
733 DPRINTF("Message in len=%d/%d\n", s
->dbc
, s
->msg_len
);
738 cpu_physical_memory_write(s
->dnad
, s
->msg
, len
);
739 /* Linux drivers rely on the last byte being in the SIDL. */
740 s
->sidl
= s
->msg
[len
- 1];
743 memmove(s
->msg
, s
->msg
+ len
, s
->msg_len
);
745 /* ??? Check if ATN (not yet implemented) is asserted and maybe
746 switch to PHASE_MO. */
747 switch (s
->msg_action
) {
749 lsi_set_phase(s
, PHASE_CMD
);
755 lsi_set_phase(s
, PHASE_DO
);
758 lsi_set_phase(s
, PHASE_DI
);
766 /* Read the next byte during a MSGOUT phase. */
767 static uint8_t lsi_get_msgbyte(LSIState
*s
)
770 cpu_physical_memory_read(s
->dnad
, &data
, 1);
776 static void lsi_do_msgout(LSIState
*s
)
781 DPRINTF("MSG out len=%d\n", s
->dbc
);
783 msg
= lsi_get_msgbyte(s
);
788 DPRINTF("MSG: Disconnect\n");
792 DPRINTF("MSG: No Operation\n");
793 lsi_set_phase(s
, PHASE_CMD
);
796 len
= lsi_get_msgbyte(s
);
797 msg
= lsi_get_msgbyte(s
);
798 DPRINTF("Extended message 0x%x (len %d)\n", msg
, len
);
801 DPRINTF("SDTR (ignored)\n");
805 DPRINTF("WDTR (ignored)\n");
812 case 0x20: /* SIMPLE queue */
813 s
->current_tag
|= lsi_get_msgbyte(s
) | LSI_TAG_VALID
;
814 DPRINTF("SIMPLE queue tag=0x%x\n", s
->current_tag
& 0xff);
816 case 0x21: /* HEAD of queue */
817 BADF("HEAD queue not implemented\n");
818 s
->current_tag
|= lsi_get_msgbyte(s
) | LSI_TAG_VALID
;
820 case 0x22: /* ORDERED queue */
821 BADF("ORDERED queue not implemented\n");
822 s
->current_tag
|= lsi_get_msgbyte(s
) | LSI_TAG_VALID
;
825 if ((msg
& 0x80) == 0) {
828 s
->current_lun
= msg
& 7;
829 DPRINTF("Select LUN %d\n", s
->current_lun
);
830 lsi_set_phase(s
, PHASE_CMD
);
836 BADF("Unimplemented message 0x%02x\n", msg
);
837 lsi_set_phase(s
, PHASE_MI
);
838 lsi_add_msg_byte(s
, 7); /* MESSAGE REJECT */
842 /* Sign extend a 24-bit value. */
843 static inline int32_t sxt24(int32_t n
)
845 return (n
<< 8) >> 8;
848 #define LSI_BUF_SIZE 4096
849 static void lsi_memcpy(LSIState
*s
, uint32_t dest
, uint32_t src
, int count
)
852 uint8_t buf
[LSI_BUF_SIZE
];
854 DPRINTF("memcpy dest 0x%08x src 0x%08x count %d\n", dest
, src
, count
);
856 n
= (count
> LSI_BUF_SIZE
) ? LSI_BUF_SIZE
: count
;
857 cpu_physical_memory_read(src
, buf
, n
);
858 cpu_physical_memory_write(dest
, buf
, n
);
865 static void lsi_wait_reselect(LSIState
*s
)
868 DPRINTF("Wait Reselect\n");
869 if (s
->current_dma_len
)
870 BADF("Reselect with pending DMA\n");
871 for (i
= 0; i
< s
->active_commands
; i
++) {
872 if (s
->queue
[i
].pending
) {
873 lsi_reselect(s
, s
->queue
[i
].tag
);
877 if (s
->current_dma_len
== 0) {
882 static void lsi_execute_script(LSIState
*s
)
885 uint32_t addr
, addr_high
;
887 int insn_processed
= 0;
889 s
->istat1
|= LSI_ISTAT1_SRUN
;
892 insn
= read_dword(s
, s
->dsp
);
894 /* If we receive an empty opcode increment the DSP by 4 bytes
895 instead of 8 and execute the next opcode at that location */
899 addr
= read_dword(s
, s
->dsp
+ 4);
901 DPRINTF("SCRIPTS dsp=%08x opcode %08x arg %08x\n", s
->dsp
, insn
, addr
);
903 s
->dcmd
= insn
>> 24;
905 switch (insn
>> 30) {
906 case 0: /* Block move. */
907 if (s
->sist1
& LSI_SIST1_STO
) {
908 DPRINTF("Delayed select timeout\n");
912 s
->dbc
= insn
& 0xffffff;
916 if (insn
& (1 << 29)) {
917 /* Indirect addressing. */
918 addr
= read_dword(s
, addr
);
919 } else if (insn
& (1 << 28)) {
922 /* Table indirect addressing. */
924 /* 32-bit Table indirect */
925 offset
= sxt24(addr
);
926 cpu_physical_memory_read(s
->dsa
+ offset
, (uint8_t *)buf
, 8);
927 /* byte count is stored in bits 0:23 only */
928 s
->dbc
= cpu_to_le32(buf
[0]) & 0xffffff;
930 addr
= cpu_to_le32(buf
[1]);
932 /* 40-bit DMA, upper addr bits [39:32] stored in first DWORD of
933 * table, bits [31:24] */
934 if (lsi_dma_40bit(s
))
935 addr_high
= cpu_to_le32(buf
[0]) >> 24;
936 else if (lsi_dma_ti64bit(s
)) {
937 int selector
= (cpu_to_le32(buf
[0]) >> 24) & 0x1f;
940 /* offset index into scratch registers since
941 * TI64 mode can use registers C to R */
942 addr_high
= s
->scratch
[2 + selector
];
963 BADF("Illegal selector specified (0x%x > 0x15)"
964 " for 64-bit DMA block move", selector
);
968 } else if (lsi_dma_64bit(s
)) {
969 /* fetch a 3rd dword if 64-bit direct move is enabled and
970 only if we're not doing table indirect or indirect addressing */
971 s
->dbms
= read_dword(s
, s
->dsp
);
975 if ((s
->sstat1
& PHASE_MASK
) != ((insn
>> 24) & 7)) {
976 DPRINTF("Wrong phase got %d expected %d\n",
977 s
->sstat1
& PHASE_MASK
, (insn
>> 24) & 7);
978 lsi_script_scsi_interrupt(s
, LSI_SIST0_MA
, 0);
982 s
->dnad64
= addr_high
;
983 switch (s
->sstat1
& 0x7) {
1009 BADF("Unimplemented phase %d\n", s
->sstat1
& PHASE_MASK
);
1012 s
->dfifo
= s
->dbc
& 0xff;
1013 s
->ctest5
= (s
->ctest5
& 0xfc) | ((s
->dbc
>> 8) & 3);
1016 s
->ua
= addr
+ s
->dbc
;
1019 case 1: /* IO or Read/Write instruction. */
1020 opcode
= (insn
>> 27) & 7;
1024 if (insn
& (1 << 25)) {
1025 id
= read_dword(s
, s
->dsa
+ sxt24(insn
));
1029 id
= (id
>> 16) & 0xf;
1030 if (insn
& (1 << 26)) {
1031 addr
= s
->dsp
+ sxt24(addr
);
1035 case 0: /* Select */
1037 if (s
->current_dma_len
&& (s
->ssid
& 0xf) == id
) {
1038 DPRINTF("Already reselected by target %d\n", id
);
1041 s
->sstat0
|= LSI_SSTAT0_WOA
;
1042 s
->scntl1
&= ~LSI_SCNTL1_IARB
;
1043 if (id
>= LSI_MAX_DEVS
|| !s
->scsi_dev
[id
]) {
1044 DPRINTF("Selected absent target %d\n", id
);
1045 lsi_script_scsi_interrupt(s
, 0, LSI_SIST1_STO
);
1049 DPRINTF("Selected target %d%s\n",
1050 id
, insn
& (1 << 3) ? " ATN" : "");
1051 /* ??? Linux drivers compain when this is set. Maybe
1052 it only applies in low-level mode (unimplemented).
1053 lsi_script_scsi_interrupt(s, LSI_SIST0_CMP, 0); */
1054 s
->current_dev
= s
->scsi_dev
[id
];
1055 s
->current_tag
= id
<< 8;
1056 s
->scntl1
|= LSI_SCNTL1_CON
;
1057 if (insn
& (1 << 3)) {
1058 s
->socl
|= LSI_SOCL_ATN
;
1060 lsi_set_phase(s
, PHASE_MO
);
1062 case 1: /* Disconnect */
1063 DPRINTF("Wait Disconect\n");
1064 s
->scntl1
&= ~LSI_SCNTL1_CON
;
1066 case 2: /* Wait Reselect */
1067 lsi_wait_reselect(s
);
1070 DPRINTF("Set%s%s%s%s\n",
1071 insn
& (1 << 3) ? " ATN" : "",
1072 insn
& (1 << 6) ? " ACK" : "",
1073 insn
& (1 << 9) ? " TM" : "",
1074 insn
& (1 << 10) ? " CC" : "");
1075 if (insn
& (1 << 3)) {
1076 s
->socl
|= LSI_SOCL_ATN
;
1077 lsi_set_phase(s
, PHASE_MO
);
1079 if (insn
& (1 << 9)) {
1080 BADF("Target mode not implemented\n");
1083 if (insn
& (1 << 10))
1087 DPRINTF("Clear%s%s%s%s\n",
1088 insn
& (1 << 3) ? " ATN" : "",
1089 insn
& (1 << 6) ? " ACK" : "",
1090 insn
& (1 << 9) ? " TM" : "",
1091 insn
& (1 << 10) ? " CC" : "");
1092 if (insn
& (1 << 3)) {
1093 s
->socl
&= ~LSI_SOCL_ATN
;
1095 if (insn
& (1 << 10))
1106 static const char *opcode_names
[3] =
1107 {"Write", "Read", "Read-Modify-Write"};
1108 static const char *operator_names
[8] =
1109 {"MOV", "SHL", "OR", "XOR", "AND", "SHR", "ADD", "ADC"};
1112 reg
= ((insn
>> 16) & 0x7f) | (insn
& 0x80);
1113 data8
= (insn
>> 8) & 0xff;
1114 opcode
= (insn
>> 27) & 7;
1115 operator = (insn
>> 24) & 7;
1116 DPRINTF("%s reg 0x%x %s data8=0x%02x sfbr=0x%02x%s\n",
1117 opcode_names
[opcode
- 5], reg
,
1118 operator_names
[operator], data8
, s
->sfbr
,
1119 (insn
& (1 << 23)) ? " SFBR" : "");
1122 case 5: /* From SFBR */
1126 case 6: /* To SFBR */
1128 op0
= lsi_reg_readb(s
, reg
);
1131 case 7: /* Read-modify-write */
1133 op0
= lsi_reg_readb(s
, reg
);
1134 if (insn
& (1 << 23)) {
1146 case 1: /* Shift left */
1148 op0
= (op0
<< 1) | s
->carry
;
1162 op0
= (op0
>> 1) | (s
->carry
<< 7);
1167 s
->carry
= op0
< op1
;
1170 op0
+= op1
+ s
->carry
;
1172 s
->carry
= op0
<= op1
;
1174 s
->carry
= op0
< op1
;
1179 case 5: /* From SFBR */
1180 case 7: /* Read-modify-write */
1181 lsi_reg_writeb(s
, reg
, op0
);
1183 case 6: /* To SFBR */
1190 case 2: /* Transfer Control. */
1195 if ((insn
& 0x002e0000) == 0) {
1199 if (s
->sist1
& LSI_SIST1_STO
) {
1200 DPRINTF("Delayed select timeout\n");
1204 cond
= jmp
= (insn
& (1 << 19)) != 0;
1205 if (cond
== jmp
&& (insn
& (1 << 21))) {
1206 DPRINTF("Compare carry %d\n", s
->carry
== jmp
);
1207 cond
= s
->carry
!= 0;
1209 if (cond
== jmp
&& (insn
& (1 << 17))) {
1210 DPRINTF("Compare phase %d %c= %d\n",
1211 (s
->sstat1
& PHASE_MASK
),
1213 ((insn
>> 24) & 7));
1214 cond
= (s
->sstat1
& PHASE_MASK
) == ((insn
>> 24) & 7);
1216 if (cond
== jmp
&& (insn
& (1 << 18))) {
1219 mask
= (~insn
>> 8) & 0xff;
1220 DPRINTF("Compare data 0x%x & 0x%x %c= 0x%x\n",
1221 s
->sfbr
, mask
, jmp
? '=' : '!', insn
& mask
);
1222 cond
= (s
->sfbr
& mask
) == (insn
& mask
);
1225 if (insn
& (1 << 23)) {
1226 /* Relative address. */
1227 addr
= s
->dsp
+ sxt24(addr
);
1229 switch ((insn
>> 27) & 7) {
1231 DPRINTF("Jump to 0x%08x\n", addr
);
1235 DPRINTF("Call 0x%08x\n", addr
);
1239 case 2: /* Return */
1240 DPRINTF("Return to 0x%08x\n", s
->temp
);
1243 case 3: /* Interrupt */
1244 DPRINTF("Interrupt 0x%08x\n", s
->dsps
);
1245 if ((insn
& (1 << 20)) != 0) {
1246 s
->istat0
|= LSI_ISTAT0_INTF
;
1249 lsi_script_dma_interrupt(s
, LSI_DSTAT_SIR
);
1253 DPRINTF("Illegal transfer control\n");
1254 lsi_script_dma_interrupt(s
, LSI_DSTAT_IID
);
1258 DPRINTF("Control condition failed\n");
1264 if ((insn
& (1 << 29)) == 0) {
1267 /* ??? The docs imply the destination address is loaded into
1268 the TEMP register. However the Linux drivers rely on
1269 the value being presrved. */
1270 dest
= read_dword(s
, s
->dsp
);
1272 lsi_memcpy(s
, dest
, addr
, insn
& 0xffffff);
1279 if (insn
& (1 << 28)) {
1280 addr
= s
->dsa
+ sxt24(addr
);
1283 reg
= (insn
>> 16) & 0xff;
1284 if (insn
& (1 << 24)) {
1285 cpu_physical_memory_read(addr
, data
, n
);
1286 DPRINTF("Load reg 0x%x size %d addr 0x%08x = %08x\n", reg
, n
,
1287 addr
, *(int *)data
);
1288 for (i
= 0; i
< n
; i
++) {
1289 lsi_reg_writeb(s
, reg
+ i
, data
[i
]);
1292 DPRINTF("Store reg 0x%x size %d addr 0x%08x\n", reg
, n
, addr
);
1293 for (i
= 0; i
< n
; i
++) {
1294 data
[i
] = lsi_reg_readb(s
, reg
+ i
);
1296 cpu_physical_memory_write(addr
, data
, n
);
1300 if (insn_processed
> 10000 && !s
->waiting
) {
1301 /* Some windows drivers make the device spin waiting for a memory
1302 location to change. If we have been executed a lot of code then
1303 assume this is the case and force an unexpected device disconnect.
1304 This is apparently sufficient to beat the drivers into submission.
1306 if (!(s
->sien0
& LSI_SIST0_UDC
))
1307 fprintf(stderr
, "inf. loop with UDC masked\n");
1308 lsi_script_scsi_interrupt(s
, LSI_SIST0_UDC
, 0);
1310 } else if (s
->istat1
& LSI_ISTAT1_SRUN
&& !s
->waiting
) {
1311 if (s
->dcntl
& LSI_DCNTL_SSM
) {
1312 lsi_script_dma_interrupt(s
, LSI_DSTAT_SSI
);
1317 DPRINTF("SCRIPTS execution stopped\n");
1320 static uint8_t lsi_reg_readb(LSIState
*s
, int offset
)
1323 #define CASE_GET_REG24(name, addr) \
1324 case addr: return s->name & 0xff; \
1325 case addr + 1: return (s->name >> 8) & 0xff; \
1326 case addr + 2: return (s->name >> 16) & 0xff;
1328 #define CASE_GET_REG32(name, addr) \
1329 case addr: return s->name & 0xff; \
1330 case addr + 1: return (s->name >> 8) & 0xff; \
1331 case addr + 2: return (s->name >> 16) & 0xff; \
1332 case addr + 3: return (s->name >> 24) & 0xff;
1334 #ifdef DEBUG_LSI_REG
1335 DPRINTF("Read reg %x\n", offset
);
1338 case 0x00: /* SCNTL0 */
1340 case 0x01: /* SCNTL1 */
1342 case 0x02: /* SCNTL2 */
1344 case 0x03: /* SCNTL3 */
1346 case 0x04: /* SCID */
1348 case 0x05: /* SXFER */
1350 case 0x06: /* SDID */
1352 case 0x07: /* GPREG0 */
1354 case 0x08: /* Revision ID */
1356 case 0xa: /* SSID */
1358 case 0xb: /* SBCL */
1359 /* ??? This is not correct. However it's (hopefully) only
1360 used for diagnostics, so should be ok. */
1362 case 0xc: /* DSTAT */
1363 tmp
= s
->dstat
| 0x80;
1364 if ((s
->istat0
& LSI_ISTAT0_INTF
) == 0)
1368 case 0x0d: /* SSTAT0 */
1370 case 0x0e: /* SSTAT1 */
1372 case 0x0f: /* SSTAT2 */
1373 return s
->scntl1
& LSI_SCNTL1_CON
? 0 : 2;
1374 CASE_GET_REG32(dsa
, 0x10)
1375 case 0x14: /* ISTAT0 */
1377 case 0x15: /* ISTAT1 */
1379 case 0x16: /* MBOX0 */
1381 case 0x17: /* MBOX1 */
1383 case 0x18: /* CTEST0 */
1385 case 0x19: /* CTEST1 */
1387 case 0x1a: /* CTEST2 */
1388 tmp
= s
->ctest2
| LSI_CTEST2_DACK
| LSI_CTEST2_CM
;
1389 if (s
->istat0
& LSI_ISTAT0_SIGP
) {
1390 s
->istat0
&= ~LSI_ISTAT0_SIGP
;
1391 tmp
|= LSI_CTEST2_SIGP
;
1394 case 0x1b: /* CTEST3 */
1396 CASE_GET_REG32(temp
, 0x1c)
1397 case 0x20: /* DFIFO */
1399 case 0x21: /* CTEST4 */
1401 case 0x22: /* CTEST5 */
1403 case 0x23: /* CTEST6 */
1405 CASE_GET_REG24(dbc
, 0x24)
1406 case 0x27: /* DCMD */
1408 CASE_GET_REG32(dnad
, 0x28)
1409 CASE_GET_REG32(dsp
, 0x2c)
1410 CASE_GET_REG32(dsps
, 0x30)
1411 CASE_GET_REG32(scratch
[0], 0x34)
1412 case 0x38: /* DMODE */
1414 case 0x39: /* DIEN */
1416 case 0x3a: /* SBR */
1418 case 0x3b: /* DCNTL */
1420 case 0x40: /* SIEN0 */
1422 case 0x41: /* SIEN1 */
1424 case 0x42: /* SIST0 */
1429 case 0x43: /* SIST1 */
1434 case 0x46: /* MACNTL */
1436 case 0x47: /* GPCNTL0 */
1438 case 0x48: /* STIME0 */
1440 case 0x4a: /* RESPID0 */
1442 case 0x4b: /* RESPID1 */
1444 case 0x4d: /* STEST1 */
1446 case 0x4e: /* STEST2 */
1448 case 0x4f: /* STEST3 */
1450 case 0x50: /* SIDL */
1451 /* This is needed by the linux drivers. We currently only update it
1452 during the MSG IN phase. */
1454 case 0x52: /* STEST4 */
1456 case 0x56: /* CCNTL0 */
1458 case 0x57: /* CCNTL1 */
1460 case 0x58: /* SBDL */
1461 /* Some drivers peek at the data bus during the MSG IN phase. */
1462 if ((s
->sstat1
& PHASE_MASK
) == PHASE_MI
)
1465 case 0x59: /* SBDL high */
1467 CASE_GET_REG32(mmrs
, 0xa0)
1468 CASE_GET_REG32(mmws
, 0xa4)
1469 CASE_GET_REG32(sfs
, 0xa8)
1470 CASE_GET_REG32(drs
, 0xac)
1471 CASE_GET_REG32(sbms
, 0xb0)
1472 CASE_GET_REG32(dbms
, 0xb4)
1473 CASE_GET_REG32(dnad64
, 0xb8)
1474 CASE_GET_REG32(pmjad1
, 0xc0)
1475 CASE_GET_REG32(pmjad2
, 0xc4)
1476 CASE_GET_REG32(rbc
, 0xc8)
1477 CASE_GET_REG32(ua
, 0xcc)
1478 CASE_GET_REG32(ia
, 0xd4)
1479 CASE_GET_REG32(sbc
, 0xd8)
1480 CASE_GET_REG32(csbc
, 0xdc)
1482 if (offset
>= 0x5c && offset
< 0xa0) {
1485 n
= (offset
- 0x58) >> 2;
1486 shift
= (offset
& 3) * 8;
1487 return (s
->scratch
[n
] >> shift
) & 0xff;
1489 BADF("readb 0x%x\n", offset
);
1491 #undef CASE_GET_REG24
1492 #undef CASE_GET_REG32
1495 static void lsi_reg_writeb(LSIState
*s
, int offset
, uint8_t val
)
1497 #define CASE_SET_REG24(name, addr) \
1498 case addr : s->name &= 0xffffff00; s->name |= val; break; \
1499 case addr + 1: s->name &= 0xffff00ff; s->name |= val << 8; break; \
1500 case addr + 2: s->name &= 0xff00ffff; s->name |= val << 16; break;
1502 #define CASE_SET_REG32(name, addr) \
1503 case addr : s->name &= 0xffffff00; s->name |= val; break; \
1504 case addr + 1: s->name &= 0xffff00ff; s->name |= val << 8; break; \
1505 case addr + 2: s->name &= 0xff00ffff; s->name |= val << 16; break; \
1506 case addr + 3: s->name &= 0x00ffffff; s->name |= val << 24; break;
1508 #ifdef DEBUG_LSI_REG
1509 DPRINTF("Write reg %x = %02x\n", offset
, val
);
1512 case 0x00: /* SCNTL0 */
1514 if (val
& LSI_SCNTL0_START
) {
1515 BADF("Start sequence not implemented\n");
1518 case 0x01: /* SCNTL1 */
1519 s
->scntl1
= val
& ~LSI_SCNTL1_SST
;
1520 if (val
& LSI_SCNTL1_IARB
) {
1521 BADF("Immediate Arbritration not implemented\n");
1523 if (val
& LSI_SCNTL1_RST
) {
1524 s
->sstat0
|= LSI_SSTAT0_RST
;
1525 lsi_script_scsi_interrupt(s
, LSI_SIST0_RST
, 0);
1527 s
->sstat0
&= ~LSI_SSTAT0_RST
;
1530 case 0x02: /* SCNTL2 */
1531 val
&= ~(LSI_SCNTL2_WSR
| LSI_SCNTL2_WSS
);
1534 case 0x03: /* SCNTL3 */
1537 case 0x04: /* SCID */
1540 case 0x05: /* SXFER */
1543 case 0x06: /* SDID */
1544 if ((val
& 0xf) != (s
->ssid
& 0xf))
1545 BADF("Destination ID does not match SSID\n");
1546 s
->sdid
= val
& 0xf;
1548 case 0x07: /* GPREG0 */
1550 case 0x08: /* SFBR */
1551 /* The CPU is not allowed to write to this register. However the
1552 SCRIPTS register move instructions are. */
1555 case 0x0a: case 0x0b:
1556 /* Openserver writes to these readonly registers on startup */
1558 case 0x0c: case 0x0d: case 0x0e: case 0x0f:
1559 /* Linux writes to these readonly registers on startup. */
1561 CASE_SET_REG32(dsa
, 0x10)
1562 case 0x14: /* ISTAT0 */
1563 s
->istat0
= (s
->istat0
& 0x0f) | (val
& 0xf0);
1564 if (val
& LSI_ISTAT0_ABRT
) {
1565 lsi_script_dma_interrupt(s
, LSI_DSTAT_ABRT
);
1567 if (val
& LSI_ISTAT0_INTF
) {
1568 s
->istat0
&= ~LSI_ISTAT0_INTF
;
1571 if (s
->waiting
== 1 && val
& LSI_ISTAT0_SIGP
) {
1572 DPRINTF("Woken by SIGP\n");
1575 lsi_execute_script(s
);
1577 if (val
& LSI_ISTAT0_SRST
) {
1581 case 0x16: /* MBOX0 */
1584 case 0x17: /* MBOX1 */
1587 case 0x1a: /* CTEST2 */
1588 s
->ctest2
= val
& LSI_CTEST2_PCICIE
;
1590 case 0x1b: /* CTEST3 */
1591 s
->ctest3
= val
& 0x0f;
1593 CASE_SET_REG32(temp
, 0x1c)
1594 case 0x21: /* CTEST4 */
1596 BADF("Unimplemented CTEST4-FBL 0x%x\n", val
);
1600 case 0x22: /* CTEST5 */
1601 if (val
& (LSI_CTEST5_ADCK
| LSI_CTEST5_BBCK
)) {
1602 BADF("CTEST5 DMA increment not implemented\n");
1606 CASE_SET_REG24(dbc
, 0x24)
1607 CASE_SET_REG32(dnad
, 0x28)
1608 case 0x2c: /* DSP[0:7] */
1609 s
->dsp
&= 0xffffff00;
1612 case 0x2d: /* DSP[8:15] */
1613 s
->dsp
&= 0xffff00ff;
1616 case 0x2e: /* DSP[16:23] */
1617 s
->dsp
&= 0xff00ffff;
1618 s
->dsp
|= val
<< 16;
1620 case 0x2f: /* DSP[24:31] */
1621 s
->dsp
&= 0x00ffffff;
1622 s
->dsp
|= val
<< 24;
1623 if ((s
->dmode
& LSI_DMODE_MAN
) == 0
1624 && (s
->istat1
& LSI_ISTAT1_SRUN
) == 0)
1625 lsi_execute_script(s
);
1627 CASE_SET_REG32(dsps
, 0x30)
1628 CASE_SET_REG32(scratch
[0], 0x34)
1629 case 0x38: /* DMODE */
1630 if (val
& (LSI_DMODE_SIOM
| LSI_DMODE_DIOM
)) {
1631 BADF("IO mappings not implemented\n");
1635 case 0x39: /* DIEN */
1639 case 0x3a: /* SBR */
1642 case 0x3b: /* DCNTL */
1643 s
->dcntl
= val
& ~(LSI_DCNTL_PFF
| LSI_DCNTL_STD
);
1644 if ((val
& LSI_DCNTL_STD
) && (s
->istat1
& LSI_ISTAT1_SRUN
) == 0)
1645 lsi_execute_script(s
);
1647 case 0x40: /* SIEN0 */
1651 case 0x41: /* SIEN1 */
1655 case 0x47: /* GPCNTL0 */
1657 case 0x48: /* STIME0 */
1660 case 0x49: /* STIME1 */
1662 DPRINTF("General purpose timer not implemented\n");
1663 /* ??? Raising the interrupt immediately seems to be sufficient
1664 to keep the FreeBSD driver happy. */
1665 lsi_script_scsi_interrupt(s
, 0, LSI_SIST1_GEN
);
1668 case 0x4a: /* RESPID0 */
1671 case 0x4b: /* RESPID1 */
1674 case 0x4d: /* STEST1 */
1677 case 0x4e: /* STEST2 */
1679 BADF("Low level mode not implemented\n");
1683 case 0x4f: /* STEST3 */
1685 BADF("SCSI FIFO test mode not implemented\n");
1689 case 0x56: /* CCNTL0 */
1692 case 0x57: /* CCNTL1 */
1695 CASE_SET_REG32(mmrs
, 0xa0)
1696 CASE_SET_REG32(mmws
, 0xa4)
1697 CASE_SET_REG32(sfs
, 0xa8)
1698 CASE_SET_REG32(drs
, 0xac)
1699 CASE_SET_REG32(sbms
, 0xb0)
1700 CASE_SET_REG32(dbms
, 0xb4)
1701 CASE_SET_REG32(dnad64
, 0xb8)
1702 CASE_SET_REG32(pmjad1
, 0xc0)
1703 CASE_SET_REG32(pmjad2
, 0xc4)
1704 CASE_SET_REG32(rbc
, 0xc8)
1705 CASE_SET_REG32(ua
, 0xcc)
1706 CASE_SET_REG32(ia
, 0xd4)
1707 CASE_SET_REG32(sbc
, 0xd8)
1708 CASE_SET_REG32(csbc
, 0xdc)
1710 if (offset
>= 0x5c && offset
< 0xa0) {
1713 n
= (offset
- 0x58) >> 2;
1714 shift
= (offset
& 3) * 8;
1715 s
->scratch
[n
] &= ~(0xff << shift
);
1716 s
->scratch
[n
] |= (val
& 0xff) << shift
;
1718 BADF("Unhandled writeb 0x%x = 0x%x\n", offset
, val
);
1721 #undef CASE_SET_REG24
1722 #undef CASE_SET_REG32
1725 static void lsi_mmio_writeb(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1727 LSIState
*s
= (LSIState
*)opaque
;
1729 lsi_reg_writeb(s
, addr
& 0xff, val
);
1732 static void lsi_mmio_writew(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1734 LSIState
*s
= (LSIState
*)opaque
;
1737 lsi_reg_writeb(s
, addr
, val
& 0xff);
1738 lsi_reg_writeb(s
, addr
+ 1, (val
>> 8) & 0xff);
1741 static void lsi_mmio_writel(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1743 LSIState
*s
= (LSIState
*)opaque
;
1746 lsi_reg_writeb(s
, addr
, val
& 0xff);
1747 lsi_reg_writeb(s
, addr
+ 1, (val
>> 8) & 0xff);
1748 lsi_reg_writeb(s
, addr
+ 2, (val
>> 16) & 0xff);
1749 lsi_reg_writeb(s
, addr
+ 3, (val
>> 24) & 0xff);
1752 static uint32_t lsi_mmio_readb(void *opaque
, target_phys_addr_t addr
)
1754 LSIState
*s
= (LSIState
*)opaque
;
1756 return lsi_reg_readb(s
, addr
& 0xff);
1759 static uint32_t lsi_mmio_readw(void *opaque
, target_phys_addr_t addr
)
1761 LSIState
*s
= (LSIState
*)opaque
;
1765 val
= lsi_reg_readb(s
, addr
);
1766 val
|= lsi_reg_readb(s
, addr
+ 1) << 8;
1770 static uint32_t lsi_mmio_readl(void *opaque
, target_phys_addr_t addr
)
1772 LSIState
*s
= (LSIState
*)opaque
;
1775 val
= lsi_reg_readb(s
, addr
);
1776 val
|= lsi_reg_readb(s
, addr
+ 1) << 8;
1777 val
|= lsi_reg_readb(s
, addr
+ 2) << 16;
1778 val
|= lsi_reg_readb(s
, addr
+ 3) << 24;
1782 static CPUReadMemoryFunc
* const lsi_mmio_readfn
[3] = {
1788 static CPUWriteMemoryFunc
* const lsi_mmio_writefn
[3] = {
1794 static void lsi_ram_writeb(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1796 LSIState
*s
= (LSIState
*)opaque
;
1801 newval
= s
->script_ram
[addr
>> 2];
1802 shift
= (addr
& 3) * 8;
1803 newval
&= ~(0xff << shift
);
1804 newval
|= val
<< shift
;
1805 s
->script_ram
[addr
>> 2] = newval
;
1808 static void lsi_ram_writew(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1810 LSIState
*s
= (LSIState
*)opaque
;
1814 newval
= s
->script_ram
[addr
>> 2];
1816 newval
= (newval
& 0xffff) | (val
<< 16);
1818 newval
= (newval
& 0xffff0000) | val
;
1820 s
->script_ram
[addr
>> 2] = newval
;
1824 static void lsi_ram_writel(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1826 LSIState
*s
= (LSIState
*)opaque
;
1829 s
->script_ram
[addr
>> 2] = val
;
1832 static uint32_t lsi_ram_readb(void *opaque
, target_phys_addr_t addr
)
1834 LSIState
*s
= (LSIState
*)opaque
;
1838 val
= s
->script_ram
[addr
>> 2];
1839 val
>>= (addr
& 3) * 8;
1843 static uint32_t lsi_ram_readw(void *opaque
, target_phys_addr_t addr
)
1845 LSIState
*s
= (LSIState
*)opaque
;
1849 val
= s
->script_ram
[addr
>> 2];
1852 return le16_to_cpu(val
);
1855 static uint32_t lsi_ram_readl(void *opaque
, target_phys_addr_t addr
)
1857 LSIState
*s
= (LSIState
*)opaque
;
1860 return le32_to_cpu(s
->script_ram
[addr
>> 2]);
1863 static CPUReadMemoryFunc
* const lsi_ram_readfn
[3] = {
1869 static CPUWriteMemoryFunc
* const lsi_ram_writefn
[3] = {
1875 static uint32_t lsi_io_readb(void *opaque
, uint32_t addr
)
1877 LSIState
*s
= (LSIState
*)opaque
;
1878 return lsi_reg_readb(s
, addr
& 0xff);
1881 static uint32_t lsi_io_readw(void *opaque
, uint32_t addr
)
1883 LSIState
*s
= (LSIState
*)opaque
;
1886 val
= lsi_reg_readb(s
, addr
);
1887 val
|= lsi_reg_readb(s
, addr
+ 1) << 8;
1891 static uint32_t lsi_io_readl(void *opaque
, uint32_t addr
)
1893 LSIState
*s
= (LSIState
*)opaque
;
1896 val
= lsi_reg_readb(s
, addr
);
1897 val
|= lsi_reg_readb(s
, addr
+ 1) << 8;
1898 val
|= lsi_reg_readb(s
, addr
+ 2) << 16;
1899 val
|= lsi_reg_readb(s
, addr
+ 3) << 24;
1903 static void lsi_io_writeb(void *opaque
, uint32_t addr
, uint32_t val
)
1905 LSIState
*s
= (LSIState
*)opaque
;
1906 lsi_reg_writeb(s
, addr
& 0xff, val
);
1909 static void lsi_io_writew(void *opaque
, uint32_t addr
, uint32_t val
)
1911 LSIState
*s
= (LSIState
*)opaque
;
1913 lsi_reg_writeb(s
, addr
, val
& 0xff);
1914 lsi_reg_writeb(s
, addr
+ 1, (val
>> 8) & 0xff);
1917 static void lsi_io_writel(void *opaque
, uint32_t addr
, uint32_t val
)
1919 LSIState
*s
= (LSIState
*)opaque
;
1921 lsi_reg_writeb(s
, addr
, val
& 0xff);
1922 lsi_reg_writeb(s
, addr
+ 1, (val
>> 8) & 0xff);
1923 lsi_reg_writeb(s
, addr
+ 2, (val
>> 16) & 0xff);
1924 lsi_reg_writeb(s
, addr
+ 3, (val
>> 24) & 0xff);
1927 static void lsi_io_mapfunc(PCIDevice
*pci_dev
, int region_num
,
1928 uint32_t addr
, uint32_t size
, int type
)
1930 LSIState
*s
= (LSIState
*)pci_dev
;
1932 DPRINTF("Mapping IO at %08x\n", addr
);
1934 register_ioport_write(addr
, 256, 1, lsi_io_writeb
, s
);
1935 register_ioport_read(addr
, 256, 1, lsi_io_readb
, s
);
1936 register_ioport_write(addr
, 256, 2, lsi_io_writew
, s
);
1937 register_ioport_read(addr
, 256, 2, lsi_io_readw
, s
);
1938 register_ioport_write(addr
, 256, 4, lsi_io_writel
, s
);
1939 register_ioport_read(addr
, 256, 4, lsi_io_readl
, s
);
1942 static void lsi_ram_mapfunc(PCIDevice
*pci_dev
, int region_num
,
1943 uint32_t addr
, uint32_t size
, int type
)
1945 LSIState
*s
= (LSIState
*)pci_dev
;
1947 DPRINTF("Mapping ram at %08x\n", addr
);
1948 s
->script_ram_base
= addr
;
1949 cpu_register_physical_memory(addr
+ 0, 0x2000, s
->ram_io_addr
);
1952 static void lsi_mmio_mapfunc(PCIDevice
*pci_dev
, int region_num
,
1953 uint32_t addr
, uint32_t size
, int type
)
1955 LSIState
*s
= (LSIState
*)pci_dev
;
1957 DPRINTF("Mapping registers at %08x\n", addr
);
1958 cpu_register_physical_memory(addr
+ 0, 0x400, s
->mmio_io_addr
);
1961 void lsi_scsi_attach(DeviceState
*host
, BlockDriverState
*bd
, int id
)
1963 LSIState
*s
= (LSIState
*)host
;
1966 for (id
= 0; id
< LSI_MAX_DEVS
; id
++) {
1967 if (s
->scsi_dev
[id
] == NULL
)
1971 if (id
>= LSI_MAX_DEVS
) {
1972 BADF("Bad Device ID %d\n", id
);
1975 if (s
->scsi_dev
[id
]) {
1976 DPRINTF("Destroying device %d\n", id
);
1977 s
->scsi_dev
[id
]->destroy(s
->scsi_dev
[id
]);
1979 DPRINTF("Attaching block device %d\n", id
);
1980 s
->scsi_dev
[id
] = scsi_generic_init(bd
, 1, lsi_command_complete
, s
);
1981 if (s
->scsi_dev
[id
] == NULL
)
1982 s
->scsi_dev
[id
] = scsi_disk_init(bd
, 1, lsi_command_complete
, s
);
1983 bd
->private = &s
->pci_dev
;
1986 static void lsi_scsi_save(QEMUFile
*f
, void *opaque
)
1988 LSIState
*s
= opaque
;
1990 assert(s
->dma_buf
== NULL
);
1991 assert(s
->current_dma_len
== 0);
1992 assert(s
->active_commands
== 0);
1994 pci_device_save(&s
->pci_dev
, f
);
1996 qemu_put_sbe32s(f
, &s
->carry
);
1997 qemu_put_sbe32s(f
, &s
->sense
);
1998 qemu_put_sbe32s(f
, &s
->msg_action
);
1999 qemu_put_sbe32s(f
, &s
->msg_len
);
2000 qemu_put_buffer(f
, s
->msg
, sizeof (s
->msg
));
2001 qemu_put_sbe32s(f
, &s
->waiting
);
2003 qemu_put_be32s(f
, &s
->dsa
);
2004 qemu_put_be32s(f
, &s
->temp
);
2005 qemu_put_be32s(f
, &s
->dnad
);
2006 qemu_put_be32s(f
, &s
->dbc
);
2007 qemu_put_8s(f
, &s
->istat0
);
2008 qemu_put_8s(f
, &s
->istat1
);
2009 qemu_put_8s(f
, &s
->dcmd
);
2010 qemu_put_8s(f
, &s
->dstat
);
2011 qemu_put_8s(f
, &s
->dien
);
2012 qemu_put_8s(f
, &s
->sist0
);
2013 qemu_put_8s(f
, &s
->sist1
);
2014 qemu_put_8s(f
, &s
->sien0
);
2015 qemu_put_8s(f
, &s
->sien1
);
2016 qemu_put_8s(f
, &s
->mbox0
);
2017 qemu_put_8s(f
, &s
->mbox1
);
2018 qemu_put_8s(f
, &s
->dfifo
);
2019 qemu_put_8s(f
, &s
->ctest2
);
2020 qemu_put_8s(f
, &s
->ctest3
);
2021 qemu_put_8s(f
, &s
->ctest4
);
2022 qemu_put_8s(f
, &s
->ctest5
);
2023 qemu_put_8s(f
, &s
->ccntl0
);
2024 qemu_put_8s(f
, &s
->ccntl1
);
2025 qemu_put_be32s(f
, &s
->dsp
);
2026 qemu_put_be32s(f
, &s
->dsps
);
2027 qemu_put_8s(f
, &s
->dmode
);
2028 qemu_put_8s(f
, &s
->dcntl
);
2029 qemu_put_8s(f
, &s
->scntl0
);
2030 qemu_put_8s(f
, &s
->scntl1
);
2031 qemu_put_8s(f
, &s
->scntl2
);
2032 qemu_put_8s(f
, &s
->scntl3
);
2033 qemu_put_8s(f
, &s
->sstat0
);
2034 qemu_put_8s(f
, &s
->sstat1
);
2035 qemu_put_8s(f
, &s
->scid
);
2036 qemu_put_8s(f
, &s
->sxfer
);
2037 qemu_put_8s(f
, &s
->socl
);
2038 qemu_put_8s(f
, &s
->sdid
);
2039 qemu_put_8s(f
, &s
->ssid
);
2040 qemu_put_8s(f
, &s
->sfbr
);
2041 qemu_put_8s(f
, &s
->stest1
);
2042 qemu_put_8s(f
, &s
->stest2
);
2043 qemu_put_8s(f
, &s
->stest3
);
2044 qemu_put_8s(f
, &s
->sidl
);
2045 qemu_put_8s(f
, &s
->stime0
);
2046 qemu_put_8s(f
, &s
->respid0
);
2047 qemu_put_8s(f
, &s
->respid1
);
2048 qemu_put_be32s(f
, &s
->mmrs
);
2049 qemu_put_be32s(f
, &s
->mmws
);
2050 qemu_put_be32s(f
, &s
->sfs
);
2051 qemu_put_be32s(f
, &s
->drs
);
2052 qemu_put_be32s(f
, &s
->sbms
);
2053 qemu_put_be32s(f
, &s
->dbms
);
2054 qemu_put_be32s(f
, &s
->dnad64
);
2055 qemu_put_be32s(f
, &s
->pmjad1
);
2056 qemu_put_be32s(f
, &s
->pmjad2
);
2057 qemu_put_be32s(f
, &s
->rbc
);
2058 qemu_put_be32s(f
, &s
->ua
);
2059 qemu_put_be32s(f
, &s
->ia
);
2060 qemu_put_be32s(f
, &s
->sbc
);
2061 qemu_put_be32s(f
, &s
->csbc
);
2062 qemu_put_buffer(f
, (uint8_t *)s
->scratch
, sizeof (s
->scratch
));
2063 qemu_put_8s(f
, &s
->sbr
);
2065 qemu_put_buffer(f
, (uint8_t *)s
->script_ram
, sizeof (s
->script_ram
));
2068 static int lsi_scsi_load(QEMUFile
*f
, void *opaque
, int version_id
)
2070 LSIState
*s
= opaque
;
2073 if (version_id
> 0) {
2077 if ((ret
= pci_device_load(&s
->pci_dev
, f
)) < 0)
2080 qemu_get_sbe32s(f
, &s
->carry
);
2081 qemu_get_sbe32s(f
, &s
->sense
);
2082 qemu_get_sbe32s(f
, &s
->msg_action
);
2083 qemu_get_sbe32s(f
, &s
->msg_len
);
2084 qemu_get_buffer(f
, s
->msg
, sizeof (s
->msg
));
2085 qemu_get_sbe32s(f
, &s
->waiting
);
2087 qemu_get_be32s(f
, &s
->dsa
);
2088 qemu_get_be32s(f
, &s
->temp
);
2089 qemu_get_be32s(f
, &s
->dnad
);
2090 qemu_get_be32s(f
, &s
->dbc
);
2091 qemu_get_8s(f
, &s
->istat0
);
2092 qemu_get_8s(f
, &s
->istat1
);
2093 qemu_get_8s(f
, &s
->dcmd
);
2094 qemu_get_8s(f
, &s
->dstat
);
2095 qemu_get_8s(f
, &s
->dien
);
2096 qemu_get_8s(f
, &s
->sist0
);
2097 qemu_get_8s(f
, &s
->sist1
);
2098 qemu_get_8s(f
, &s
->sien0
);
2099 qemu_get_8s(f
, &s
->sien1
);
2100 qemu_get_8s(f
, &s
->mbox0
);
2101 qemu_get_8s(f
, &s
->mbox1
);
2102 qemu_get_8s(f
, &s
->dfifo
);
2103 qemu_get_8s(f
, &s
->ctest2
);
2104 qemu_get_8s(f
, &s
->ctest3
);
2105 qemu_get_8s(f
, &s
->ctest4
);
2106 qemu_get_8s(f
, &s
->ctest5
);
2107 qemu_get_8s(f
, &s
->ccntl0
);
2108 qemu_get_8s(f
, &s
->ccntl1
);
2109 qemu_get_be32s(f
, &s
->dsp
);
2110 qemu_get_be32s(f
, &s
->dsps
);
2111 qemu_get_8s(f
, &s
->dmode
);
2112 qemu_get_8s(f
, &s
->dcntl
);
2113 qemu_get_8s(f
, &s
->scntl0
);
2114 qemu_get_8s(f
, &s
->scntl1
);
2115 qemu_get_8s(f
, &s
->scntl2
);
2116 qemu_get_8s(f
, &s
->scntl3
);
2117 qemu_get_8s(f
, &s
->sstat0
);
2118 qemu_get_8s(f
, &s
->sstat1
);
2119 qemu_get_8s(f
, &s
->scid
);
2120 qemu_get_8s(f
, &s
->sxfer
);
2121 qemu_get_8s(f
, &s
->socl
);
2122 qemu_get_8s(f
, &s
->sdid
);
2123 qemu_get_8s(f
, &s
->ssid
);
2124 qemu_get_8s(f
, &s
->sfbr
);
2125 qemu_get_8s(f
, &s
->stest1
);
2126 qemu_get_8s(f
, &s
->stest2
);
2127 qemu_get_8s(f
, &s
->stest3
);
2128 qemu_get_8s(f
, &s
->sidl
);
2129 qemu_get_8s(f
, &s
->stime0
);
2130 qemu_get_8s(f
, &s
->respid0
);
2131 qemu_get_8s(f
, &s
->respid1
);
2132 qemu_get_be32s(f
, &s
->mmrs
);
2133 qemu_get_be32s(f
, &s
->mmws
);
2134 qemu_get_be32s(f
, &s
->sfs
);
2135 qemu_get_be32s(f
, &s
->drs
);
2136 qemu_get_be32s(f
, &s
->sbms
);
2137 qemu_get_be32s(f
, &s
->dbms
);
2138 qemu_get_be32s(f
, &s
->dnad64
);
2139 qemu_get_be32s(f
, &s
->pmjad1
);
2140 qemu_get_be32s(f
, &s
->pmjad2
);
2141 qemu_get_be32s(f
, &s
->rbc
);
2142 qemu_get_be32s(f
, &s
->ua
);
2143 qemu_get_be32s(f
, &s
->ia
);
2144 qemu_get_be32s(f
, &s
->sbc
);
2145 qemu_get_be32s(f
, &s
->csbc
);
2146 qemu_get_buffer(f
, (uint8_t *)s
->scratch
, sizeof (s
->scratch
));
2147 qemu_get_8s(f
, &s
->sbr
);
2149 qemu_get_buffer(f
, (uint8_t *)s
->script_ram
, sizeof (s
->script_ram
));
2154 static int lsi_scsi_uninit(PCIDevice
*d
)
2156 LSIState
*s
= (LSIState
*) d
;
2158 cpu_unregister_io_memory(s
->mmio_io_addr
);
2159 cpu_unregister_io_memory(s
->ram_io_addr
);
2161 qemu_free(s
->queue
);
2166 static void lsi_scsi_init(PCIDevice
*dev
)
2168 LSIState
*s
= (LSIState
*)dev
;
2171 pci_conf
= s
->pci_dev
.config
;
2173 /* PCI Vendor ID (word) */
2174 pci_config_set_vendor_id(pci_conf
, PCI_VENDOR_ID_LSI_LOGIC
);
2175 /* PCI device ID (word) */
2176 pci_config_set_device_id(pci_conf
, PCI_DEVICE_ID_LSI_53C895A
);
2177 /* PCI base class code */
2178 pci_config_set_class(pci_conf
, PCI_CLASS_STORAGE_SCSI
);
2179 /* PCI subsystem ID */
2180 pci_conf
[0x2e] = 0x00;
2181 pci_conf
[0x2f] = 0x10;
2182 /* PCI latency timer = 255 */
2183 pci_conf
[0x0d] = 0xff;
2184 /* Interrupt pin 1 */
2185 pci_conf
[0x3d] = 0x01;
2187 s
->mmio_io_addr
= cpu_register_io_memory(lsi_mmio_readfn
,
2188 lsi_mmio_writefn
, s
);
2189 s
->ram_io_addr
= cpu_register_io_memory(lsi_ram_readfn
,
2190 lsi_ram_writefn
, s
);
2192 pci_register_bar((struct PCIDevice
*)s
, 0, 256,
2193 PCI_ADDRESS_SPACE_IO
, lsi_io_mapfunc
);
2194 pci_register_bar((struct PCIDevice
*)s
, 1, 0x400,
2195 PCI_ADDRESS_SPACE_MEM
, lsi_mmio_mapfunc
);
2196 pci_register_bar((struct PCIDevice
*)s
, 2, 0x2000,
2197 PCI_ADDRESS_SPACE_MEM
, lsi_ram_mapfunc
);
2198 s
->queue
= qemu_malloc(sizeof(lsi_queue
));
2200 s
->active_commands
= 0;
2201 s
->pci_dev
.unregister
= lsi_scsi_uninit
;
2205 scsi_bus_new(&dev
->qdev
, lsi_scsi_attach
);
2207 register_savevm("lsiscsi", -1, 0, lsi_scsi_save
, lsi_scsi_load
, s
);
2210 static PCIDeviceInfo lsi_info
= {
2211 .qdev
.name
= "lsi53c895a",
2212 .qdev
.size
= sizeof(LSIState
),
2213 .init
= lsi_scsi_init
,
2216 static void lsi53c895a_register_devices(void)
2218 pci_qdev_register(&lsi_info
);
2221 device_init(lsi53c895a_register_devices
);