]>
git.proxmox.com Git - qemu.git/blob - hw/lsi53c895a.c
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. */
15 #include "scsi-disk.h"
18 //#define DEBUG_LSI_REG
21 #define DPRINTF(fmt, args...) \
22 do { printf("lsi_scsi: " fmt , ##args); } while (0)
23 #define BADF(fmt, args...) \
24 do { fprintf(stderr, "lsi_scsi: error: " fmt , ##args); exit(1);} while (0)
26 #define DPRINTF(fmt, args...) do {} while(0)
27 #define BADF(fmt, args...) \
28 do { fprintf(stderr, "lsi_scsi: error: " fmt , ##args);} while (0)
31 #define LSI_SCNTL0_TRG 0x01
32 #define LSI_SCNTL0_AAP 0x02
33 #define LSI_SCNTL0_EPC 0x08
34 #define LSI_SCNTL0_WATN 0x10
35 #define LSI_SCNTL0_START 0x20
37 #define LSI_SCNTL1_SST 0x01
38 #define LSI_SCNTL1_IARB 0x02
39 #define LSI_SCNTL1_AESP 0x04
40 #define LSI_SCNTL1_RST 0x08
41 #define LSI_SCNTL1_CON 0x10
42 #define LSI_SCNTL1_DHP 0x20
43 #define LSI_SCNTL1_ADB 0x40
44 #define LSI_SCNTL1_EXC 0x80
46 #define LSI_SCNTL2_WSR 0x01
47 #define LSI_SCNTL2_VUE0 0x02
48 #define LSI_SCNTL2_VUE1 0x04
49 #define LSI_SCNTL2_WSS 0x08
50 #define LSI_SCNTL2_SLPHBEN 0x10
51 #define LSI_SCNTL2_SLPMD 0x20
52 #define LSI_SCNTL2_CHM 0x40
53 #define LSI_SCNTL2_SDU 0x80
55 #define LSI_ISTAT0_DIP 0x01
56 #define LSI_ISTAT0_SIP 0x02
57 #define LSI_ISTAT0_INTF 0x04
58 #define LSI_ISTAT0_CON 0x08
59 #define LSI_ISTAT0_SEM 0x10
60 #define LSI_ISTAT0_SIGP 0x20
61 #define LSI_ISTAT0_SRST 0x40
62 #define LSI_ISTAT0_ABRT 0x80
64 #define LSI_ISTAT1_SI 0x01
65 #define LSI_ISTAT1_SRUN 0x02
66 #define LSI_ISTAT1_FLSH 0x04
68 #define LSI_SSTAT0_SDP0 0x01
69 #define LSI_SSTAT0_RST 0x02
70 #define LSI_SSTAT0_WOA 0x04
71 #define LSI_SSTAT0_LOA 0x08
72 #define LSI_SSTAT0_AIP 0x10
73 #define LSI_SSTAT0_OLF 0x20
74 #define LSI_SSTAT0_ORF 0x40
75 #define LSI_SSTAT0_ILF 0x80
77 #define LSI_SIST0_PAR 0x01
78 #define LSI_SIST0_RST 0x02
79 #define LSI_SIST0_UDC 0x04
80 #define LSI_SIST0_SGE 0x08
81 #define LSI_SIST0_RSL 0x10
82 #define LSI_SIST0_SEL 0x20
83 #define LSI_SIST0_CMP 0x40
84 #define LSI_SIST0_MA 0x80
86 #define LSI_SIST1_HTH 0x01
87 #define LSI_SIST1_GEN 0x02
88 #define LSI_SIST1_STO 0x04
89 #define LSI_SIST1_SBMC 0x10
91 #define LSI_SOCL_IO 0x01
92 #define LSI_SOCL_CD 0x02
93 #define LSI_SOCL_MSG 0x04
94 #define LSI_SOCL_ATN 0x08
95 #define LSI_SOCL_SEL 0x10
96 #define LSI_SOCL_BSY 0x20
97 #define LSI_SOCL_ACK 0x40
98 #define LSI_SOCL_REQ 0x80
100 #define LSI_DSTAT_IID 0x01
101 #define LSI_DSTAT_SIR 0x04
102 #define LSI_DSTAT_SSI 0x08
103 #define LSI_DSTAT_ABRT 0x10
104 #define LSI_DSTAT_BF 0x20
105 #define LSI_DSTAT_MDPE 0x40
106 #define LSI_DSTAT_DFE 0x80
108 #define LSI_DCNTL_COM 0x01
109 #define LSI_DCNTL_IRQD 0x02
110 #define LSI_DCNTL_STD 0x04
111 #define LSI_DCNTL_IRQM 0x08
112 #define LSI_DCNTL_SSM 0x10
113 #define LSI_DCNTL_PFEN 0x20
114 #define LSI_DCNTL_PFF 0x40
115 #define LSI_DCNTL_CLSE 0x80
117 #define LSI_DMODE_MAN 0x01
118 #define LSI_DMODE_BOF 0x02
119 #define LSI_DMODE_ERMP 0x04
120 #define LSI_DMODE_ERL 0x08
121 #define LSI_DMODE_DIOM 0x10
122 #define LSI_DMODE_SIOM 0x20
124 #define LSI_CTEST2_DACK 0x01
125 #define LSI_CTEST2_DREQ 0x02
126 #define LSI_CTEST2_TEOP 0x04
127 #define LSI_CTEST2_PCICIE 0x08
128 #define LSI_CTEST2_CM 0x10
129 #define LSI_CTEST2_CIO 0x20
130 #define LSI_CTEST2_SIGP 0x40
131 #define LSI_CTEST2_DDIR 0x80
133 #define LSI_CTEST5_BL2 0x04
134 #define LSI_CTEST5_DDIR 0x08
135 #define LSI_CTEST5_MASR 0x10
136 #define LSI_CTEST5_DFSN 0x20
137 #define LSI_CTEST5_BBCK 0x40
138 #define LSI_CTEST5_ADCK 0x80
140 #define LSI_CCNTL0_DILS 0x01
141 #define LSI_CCNTL0_DISFC 0x10
142 #define LSI_CCNTL0_ENNDJ 0x20
143 #define LSI_CCNTL0_PMJCTL 0x40
144 #define LSI_CCNTL0_ENPMJ 0x80
154 /* Maximum length of MSG IN data. */
155 #define LSI_MAX_MSGIN_LEN 8
157 /* Flag set if this is a tagged command. */
158 #define LSI_TAG_VALID (1 << 16)
170 uint32_t script_ram_base
;
172 int carry
; /* ??? Should this be an a visible register somewhere? */
174 /* Action to take at the end of a MSG IN phase.
175 0 = COMMAND, 1 = disconect, 2 = DATA OUT, 3 = DATA IN. */
178 uint8_t msg
[LSI_MAX_MSGIN_LEN
];
179 /* 0 if SCRIPTS are running or stopped.
180 * 1 if a Wait Reselect instruction has been issued.
181 * 2 if processing DMA from lsi_execute_script.
182 * 3 if a DMA operation is in progress. */
184 SCSIDevice
*scsi_dev
[LSI_MAX_DEVS
];
185 SCSIDevice
*current_dev
;
187 /* The tag is a combination of the device ID and the SCSI tag. */
188 uint32_t current_tag
;
189 uint32_t current_dma_len
;
190 int command_complete
;
254 uint32_t scratch
[18]; /* SCRATCHA-SCRATCHR */
256 /* Script ram is stored as 32-bit words in host byteorder. */
257 uint32_t script_ram
[2048];
260 static void lsi_soft_reset(LSIState
*s
)
270 memset(s
->scratch
, 0, sizeof(s
->scratch
));
324 static uint8_t lsi_reg_readb(LSIState
*s
, int offset
);
325 static void lsi_reg_writeb(LSIState
*s
, int offset
, uint8_t val
);
326 static void lsi_execute_script(LSIState
*s
);
328 static inline uint32_t read_dword(LSIState
*s
, uint32_t addr
)
332 /* Optimize reading from SCRIPTS RAM. */
333 if ((addr
& 0xffffe000) == s
->script_ram_base
) {
334 return s
->script_ram
[(addr
& 0x1fff) >> 2];
336 cpu_physical_memory_read(addr
, (uint8_t *)&buf
, 4);
337 return cpu_to_le32(buf
);
340 static void lsi_stop_script(LSIState
*s
)
342 s
->istat1
&= ~LSI_ISTAT1_SRUN
;
345 static void lsi_update_irq(LSIState
*s
)
348 static int last_level
;
350 /* It's unclear whether the DIP/SIP bits should be cleared when the
351 Interrupt Status Registers are cleared or when istat0 is read.
352 We currently do the formwer, which seems to work. */
355 if (s
->dstat
& s
->dien
)
357 s
->istat0
|= LSI_ISTAT0_DIP
;
359 s
->istat0
&= ~LSI_ISTAT0_DIP
;
362 if (s
->sist0
|| s
->sist1
) {
363 if ((s
->sist0
& s
->sien0
) || (s
->sist1
& s
->sien1
))
365 s
->istat0
|= LSI_ISTAT0_SIP
;
367 s
->istat0
&= ~LSI_ISTAT0_SIP
;
369 if (s
->istat0
& LSI_ISTAT0_INTF
)
372 if (level
!= last_level
) {
373 DPRINTF("Update IRQ level %d dstat %02x sist %02x%02x\n",
374 level
, s
->dstat
, s
->sist1
, s
->sist0
);
377 qemu_set_irq(s
->pci_dev
.irq
[0], level
);
380 /* Stop SCRIPTS execution and raise a SCSI interrupt. */
381 static void lsi_script_scsi_interrupt(LSIState
*s
, int stat0
, int stat1
)
386 DPRINTF("SCSI Interrupt 0x%02x%02x prev 0x%02x%02x\n",
387 stat1
, stat0
, s
->sist1
, s
->sist0
);
390 /* Stop processor on fatal or unmasked interrupt. As a special hack
391 we don't stop processing when raising STO. Instead continue
392 execution and stop at the next insn that accesses the SCSI bus. */
393 mask0
= s
->sien0
| ~(LSI_SIST0_CMP
| LSI_SIST0_SEL
| LSI_SIST0_RSL
);
394 mask1
= s
->sien1
| ~(LSI_SIST1_GEN
| LSI_SIST1_HTH
);
395 mask1
&= ~LSI_SIST1_STO
;
396 if (s
->sist0
& mask0
|| s
->sist1
& mask1
) {
402 /* Stop SCRIPTS execution and raise a DMA interrupt. */
403 static void lsi_script_dma_interrupt(LSIState
*s
, int stat
)
405 DPRINTF("DMA Interrupt 0x%x prev 0x%x\n", stat
, s
->dstat
);
411 static inline void lsi_set_phase(LSIState
*s
, int phase
)
413 s
->sstat1
= (s
->sstat1
& ~PHASE_MASK
) | phase
;
416 static void lsi_bad_phase(LSIState
*s
, int out
, int new_phase
)
418 /* Trigger a phase mismatch. */
419 if (s
->ccntl0
& LSI_CCNTL0_ENPMJ
) {
420 if ((s
->ccntl0
& LSI_CCNTL0_PMJCTL
) || out
) {
425 DPRINTF("Data phase mismatch jump to %08x\n", s
->dsp
);
427 DPRINTF("Phase mismatch interrupt\n");
428 lsi_script_scsi_interrupt(s
, LSI_SIST0_MA
, 0);
431 lsi_set_phase(s
, new_phase
);
435 /* Resume SCRIPTS execution after a DMA operation. */
436 static void lsi_resume_script(LSIState
*s
)
438 if (s
->waiting
!= 2) {
440 lsi_execute_script(s
);
446 /* Initiate a SCSI layer data transfer. */
447 static void lsi_do_dma(LSIState
*s
, int out
)
452 if (!s
->current_dma_len
) {
453 /* Wait until data is available. */
454 DPRINTF("DMA no data available\n");
459 if (count
> s
->current_dma_len
)
460 count
= s
->current_dma_len
;
461 DPRINTF("DMA addr=0x%08x len=%d\n", s
->dnad
, count
);
468 if (s
->dma_buf
== NULL
) {
469 s
->dma_buf
= s
->current_dev
->get_buf(s
->current_dev
,
473 /* ??? Set SFBR to first data byte. */
475 cpu_physical_memory_read(addr
, s
->dma_buf
, count
);
477 cpu_physical_memory_write(addr
, s
->dma_buf
, count
);
479 s
->current_dma_len
-= count
;
480 if (s
->current_dma_len
== 0) {
483 /* Write the data. */
484 s
->current_dev
->write_data(s
->current_dev
, s
->current_tag
);
486 /* Request any remaining data. */
487 s
->current_dev
->read_data(s
->current_dev
, s
->current_tag
);
491 lsi_resume_script(s
);
496 /* Add a command to the queue. */
497 static void lsi_queue_command(LSIState
*s
)
501 DPRINTF("Queueing tag=0x%x\n", s
->current_tag
);
502 if (s
->queue_len
== s
->active_commands
) {
504 s
->queue
= realloc(s
->queue
, s
->queue_len
* sizeof(lsi_queue
));
506 p
= &s
->queue
[s
->active_commands
++];
507 p
->tag
= s
->current_tag
;
509 p
->out
= (s
->sstat1
& PHASE_MASK
) == PHASE_DO
;
512 /* Queue a byte for a MSG IN phase. */
513 static void lsi_add_msg_byte(LSIState
*s
, uint8_t data
)
515 if (s
->msg_len
>= LSI_MAX_MSGIN_LEN
) {
516 BADF("MSG IN data too long\n");
518 DPRINTF("MSG IN 0x%02x\n", data
);
519 s
->msg
[s
->msg_len
++] = data
;
523 /* Perform reselection to continue a command. */
524 static void lsi_reselect(LSIState
*s
, uint32_t tag
)
531 for (n
= 0; n
< s
->active_commands
; n
++) {
536 if (n
== s
->active_commands
) {
537 BADF("Reselected non-existant command tag=0x%x\n", tag
);
540 id
= (tag
>> 8) & 0xf;
542 DPRINTF("Reselected target %d\n", id
);
543 s
->current_dev
= s
->scsi_dev
[id
];
544 s
->current_tag
= tag
;
545 s
->scntl1
|= LSI_SCNTL1_CON
;
546 lsi_set_phase(s
, PHASE_MI
);
547 s
->msg_action
= p
->out
? 2 : 3;
548 s
->current_dma_len
= p
->pending
;
550 lsi_add_msg_byte(s
, 0x80);
551 if (s
->current_tag
& LSI_TAG_VALID
) {
552 lsi_add_msg_byte(s
, 0x20);
553 lsi_add_msg_byte(s
, tag
& 0xff);
556 s
->active_commands
--;
557 if (n
!= s
->active_commands
) {
558 s
->queue
[n
] = s
->queue
[s
->active_commands
];
562 /* Record that data is available for a queued command. Returns zero if
563 the device was reselected, nonzero if the IO is deferred. */
564 static int lsi_queue_tag(LSIState
*s
, uint32_t tag
, uint32_t arg
)
568 for (i
= 0; i
< s
->active_commands
; i
++) {
572 BADF("Multiple IO pending for tag %d\n", tag
);
575 if (s
->waiting
== 1) {
576 /* Reselect device. */
577 lsi_reselect(s
, tag
);
580 DPRINTF("Queueing IO tag=0x%x\n", tag
);
586 BADF("IO with unknown tag %d\n", tag
);
590 /* Callback to indicate that the SCSI layer has completed a transfer. */
591 static void lsi_command_complete(void *opaque
, int reason
, uint32_t tag
,
594 LSIState
*s
= (LSIState
*)opaque
;
597 out
= (s
->sstat1
& PHASE_MASK
) == PHASE_DO
;
598 if (reason
== SCSI_REASON_DONE
) {
599 DPRINTF("Command complete sense=%d\n", (int)arg
);
601 s
->command_complete
= 2;
602 if (s
->waiting
&& s
->dbc
!= 0) {
603 /* Raise phase mismatch for short transfers. */
604 lsi_bad_phase(s
, out
, PHASE_ST
);
606 lsi_set_phase(s
, PHASE_ST
);
608 lsi_resume_script(s
);
612 if (s
->waiting
== 1 || tag
!= s
->current_tag
) {
613 if (lsi_queue_tag(s
, tag
, arg
))
616 DPRINTF("Data ready tag=0x%x len=%d\n", tag
, arg
);
617 s
->current_dma_len
= arg
;
618 s
->command_complete
= 1;
621 if (s
->waiting
== 1 || s
->dbc
== 0) {
622 lsi_resume_script(s
);
628 static void lsi_do_command(LSIState
*s
)
633 DPRINTF("Send command len=%d\n", s
->dbc
);
636 cpu_physical_memory_read(s
->dnad
, buf
, s
->dbc
);
638 s
->command_complete
= 0;
639 n
= s
->current_dev
->send_command(s
->current_dev
, s
->current_tag
, buf
,
642 lsi_set_phase(s
, PHASE_DI
);
643 s
->current_dev
->read_data(s
->current_dev
, s
->current_tag
);
645 lsi_set_phase(s
, PHASE_DO
);
646 s
->current_dev
->write_data(s
->current_dev
, s
->current_tag
);
649 if (!s
->command_complete
) {
651 /* Command did not complete immediately so disconnect. */
652 lsi_add_msg_byte(s
, 2); /* SAVE DATA POINTER */
653 lsi_add_msg_byte(s
, 4); /* DISCONNECT */
655 lsi_set_phase(s
, PHASE_MI
);
657 lsi_queue_command(s
);
659 /* wait command complete */
660 lsi_set_phase(s
, PHASE_DI
);
665 static void lsi_do_status(LSIState
*s
)
668 DPRINTF("Get status len=%d sense=%d\n", s
->dbc
, s
->sense
);
670 BADF("Bad Status move\n");
674 cpu_physical_memory_write(s
->dnad
, &sense
, 1);
675 lsi_set_phase(s
, PHASE_MI
);
677 lsi_add_msg_byte(s
, 0); /* COMMAND COMPLETE */
680 static void lsi_disconnect(LSIState
*s
)
682 s
->scntl1
&= ~LSI_SCNTL1_CON
;
683 s
->sstat1
&= ~PHASE_MASK
;
686 static void lsi_do_msgin(LSIState
*s
)
689 DPRINTF("Message in len=%d/%d\n", s
->dbc
, s
->msg_len
);
694 cpu_physical_memory_write(s
->dnad
, s
->msg
, len
);
695 /* Linux drivers rely on the last byte being in the SIDL. */
696 s
->sidl
= s
->msg
[len
- 1];
699 memmove(s
->msg
, s
->msg
+ len
, s
->msg_len
);
701 /* ??? Check if ATN (not yet implemented) is asserted and maybe
702 switch to PHASE_MO. */
703 switch (s
->msg_action
) {
705 lsi_set_phase(s
, PHASE_CMD
);
711 lsi_set_phase(s
, PHASE_DO
);
714 lsi_set_phase(s
, PHASE_DI
);
722 /* Read the next byte during a MSGOUT phase. */
723 static uint8_t lsi_get_msgbyte(LSIState
*s
)
726 cpu_physical_memory_read(s
->dnad
, &data
, 1);
732 static void lsi_do_msgout(LSIState
*s
)
737 DPRINTF("MSG out len=%d\n", s
->dbc
);
739 msg
= lsi_get_msgbyte(s
);
744 DPRINTF("MSG: Disconnect\n");
748 DPRINTF("MSG: No Operation\n");
749 lsi_set_phase(s
, PHASE_CMD
);
752 len
= lsi_get_msgbyte(s
);
753 msg
= lsi_get_msgbyte(s
);
754 DPRINTF("Extended message 0x%x (len %d)\n", msg
, len
);
757 DPRINTF("SDTR (ignored)\n");
761 DPRINTF("WDTR (ignored)\n");
768 case 0x20: /* SIMPLE queue */
769 s
->current_tag
|= lsi_get_msgbyte(s
) | LSI_TAG_VALID
;
770 DPRINTF("SIMPLE queue tag=0x%x\n", s
->current_tag
& 0xff);
772 case 0x21: /* HEAD of queue */
773 BADF("HEAD queue not implemented\n");
774 s
->current_tag
|= lsi_get_msgbyte(s
) | LSI_TAG_VALID
;
776 case 0x22: /* ORDERED queue */
777 BADF("ORDERED queue not implemented\n");
778 s
->current_tag
|= lsi_get_msgbyte(s
) | LSI_TAG_VALID
;
781 if ((msg
& 0x80) == 0) {
784 s
->current_lun
= msg
& 7;
785 DPRINTF("Select LUN %d\n", s
->current_lun
);
786 lsi_set_phase(s
, PHASE_CMD
);
792 BADF("Unimplemented message 0x%02x\n", msg
);
793 lsi_set_phase(s
, PHASE_MI
);
794 lsi_add_msg_byte(s
, 7); /* MESSAGE REJECT */
798 /* Sign extend a 24-bit value. */
799 static inline int32_t sxt24(int32_t n
)
801 return (n
<< 8) >> 8;
804 static void lsi_memcpy(LSIState
*s
, uint32_t dest
, uint32_t src
, int count
)
807 uint8_t buf
[TARGET_PAGE_SIZE
];
809 DPRINTF("memcpy dest 0x%08x src 0x%08x count %d\n", dest
, src
, count
);
811 n
= (count
> TARGET_PAGE_SIZE
) ? TARGET_PAGE_SIZE
: count
;
812 cpu_physical_memory_read(src
, buf
, n
);
813 cpu_physical_memory_write(dest
, buf
, n
);
820 static void lsi_wait_reselect(LSIState
*s
)
823 DPRINTF("Wait Reselect\n");
824 if (s
->current_dma_len
)
825 BADF("Reselect with pending DMA\n");
826 for (i
= 0; i
< s
->active_commands
; i
++) {
827 if (s
->queue
[i
].pending
) {
828 lsi_reselect(s
, s
->queue
[i
].tag
);
832 if (s
->current_dma_len
== 0) {
837 static void lsi_execute_script(LSIState
*s
)
843 s
->istat1
|= LSI_ISTAT1_SRUN
;
845 insn
= read_dword(s
, s
->dsp
);
846 addr
= read_dword(s
, s
->dsp
+ 4);
847 DPRINTF("SCRIPTS dsp=%08x opcode %08x arg %08x\n", s
->dsp
, insn
, addr
);
849 s
->dcmd
= insn
>> 24;
851 switch (insn
>> 30) {
852 case 0: /* Block move. */
853 if (s
->sist1
& LSI_SIST1_STO
) {
854 DPRINTF("Delayed select timeout\n");
858 s
->dbc
= insn
& 0xffffff;
860 if (insn
& (1 << 29)) {
861 /* Indirect addressing. */
862 addr
= read_dword(s
, addr
);
863 } else if (insn
& (1 << 28)) {
866 /* Table indirect addressing. */
867 offset
= sxt24(addr
);
868 cpu_physical_memory_read(s
->dsa
+ offset
, (uint8_t *)buf
, 8);
869 s
->dbc
= cpu_to_le32(buf
[0]);
871 addr
= cpu_to_le32(buf
[1]);
873 if ((s
->sstat1
& PHASE_MASK
) != ((insn
>> 24) & 7)) {
874 DPRINTF("Wrong phase got %d expected %d\n",
875 s
->sstat1
& PHASE_MASK
, (insn
>> 24) & 7);
876 lsi_script_scsi_interrupt(s
, LSI_SIST0_MA
, 0);
882 switch (s
->sstat1
& 0x7) {
908 BADF("Unimplemented phase %d\n", s
->sstat1
& PHASE_MASK
);
911 s
->dfifo
= s
->dbc
& 0xff;
912 s
->ctest5
= (s
->ctest5
& 0xfc) | ((s
->dbc
>> 8) & 3);
915 s
->ua
= addr
+ s
->dbc
;
918 case 1: /* IO or Read/Write instruction. */
919 opcode
= (insn
>> 27) & 7;
923 if (insn
& (1 << 25)) {
924 id
= read_dword(s
, s
->dsa
+ sxt24(insn
));
928 id
= (id
>> 16) & 0xf;
929 if (insn
& (1 << 26)) {
930 addr
= s
->dsp
+ sxt24(addr
);
936 if (s
->current_dma_len
&& (s
->ssid
& 0xf) == id
) {
937 DPRINTF("Already reselected by target %d\n", id
);
940 s
->sstat0
|= LSI_SSTAT0_WOA
;
941 s
->scntl1
&= ~LSI_SCNTL1_IARB
;
942 if (id
>= LSI_MAX_DEVS
|| !s
->scsi_dev
[id
]) {
943 DPRINTF("Selected absent target %d\n", id
);
944 lsi_script_scsi_interrupt(s
, 0, LSI_SIST1_STO
);
948 DPRINTF("Selected target %d%s\n",
949 id
, insn
& (1 << 3) ? " ATN" : "");
950 /* ??? Linux drivers compain when this is set. Maybe
951 it only applies in low-level mode (unimplemented).
952 lsi_script_scsi_interrupt(s, LSI_SIST0_CMP, 0); */
953 s
->current_dev
= s
->scsi_dev
[id
];
954 s
->current_tag
= id
<< 8;
955 s
->scntl1
|= LSI_SCNTL1_CON
;
956 if (insn
& (1 << 3)) {
957 s
->socl
|= LSI_SOCL_ATN
;
959 lsi_set_phase(s
, PHASE_MO
);
961 case 1: /* Disconnect */
962 DPRINTF("Wait Disconect\n");
963 s
->scntl1
&= ~LSI_SCNTL1_CON
;
965 case 2: /* Wait Reselect */
966 lsi_wait_reselect(s
);
969 DPRINTF("Set%s%s%s%s\n",
970 insn
& (1 << 3) ? " ATN" : "",
971 insn
& (1 << 6) ? " ACK" : "",
972 insn
& (1 << 9) ? " TM" : "",
973 insn
& (1 << 10) ? " CC" : "");
974 if (insn
& (1 << 3)) {
975 s
->socl
|= LSI_SOCL_ATN
;
976 lsi_set_phase(s
, PHASE_MO
);
978 if (insn
& (1 << 9)) {
979 BADF("Target mode not implemented\n");
982 if (insn
& (1 << 10))
986 DPRINTF("Clear%s%s%s%s\n",
987 insn
& (1 << 3) ? " ATN" : "",
988 insn
& (1 << 6) ? " ACK" : "",
989 insn
& (1 << 9) ? " TM" : "",
990 insn
& (1 << 10) ? " CC" : "");
991 if (insn
& (1 << 3)) {
992 s
->socl
&= ~LSI_SOCL_ATN
;
994 if (insn
& (1 << 10))
1005 static const char *opcode_names
[3] =
1006 {"Write", "Read", "Read-Modify-Write"};
1007 static const char *operator_names
[8] =
1008 {"MOV", "SHL", "OR", "XOR", "AND", "SHR", "ADD", "ADC"};
1011 reg
= ((insn
>> 16) & 0x7f) | (insn
& 0x80);
1012 data8
= (insn
>> 8) & 0xff;
1013 opcode
= (insn
>> 27) & 7;
1014 operator = (insn
>> 24) & 7;
1015 DPRINTF("%s reg 0x%x %s data8=0x%02x sfbr=0x%02x%s\n",
1016 opcode_names
[opcode
- 5], reg
,
1017 operator_names
[operator], data8
, s
->sfbr
,
1018 (insn
& (1 << 23)) ? " SFBR" : "");
1021 case 5: /* From SFBR */
1025 case 6: /* To SFBR */
1027 op0
= lsi_reg_readb(s
, reg
);
1030 case 7: /* Read-modify-write */
1032 op0
= lsi_reg_readb(s
, reg
);
1033 if (insn
& (1 << 23)) {
1045 case 1: /* Shift left */
1047 op0
= (op0
<< 1) | s
->carry
;
1061 op0
= (op0
>> 1) | (s
->carry
<< 7);
1066 s
->carry
= op0
< op1
;
1069 op0
+= op1
+ s
->carry
;
1071 s
->carry
= op0
<= op1
;
1073 s
->carry
= op0
< op1
;
1078 case 5: /* From SFBR */
1079 case 7: /* Read-modify-write */
1080 lsi_reg_writeb(s
, reg
, op0
);
1082 case 6: /* To SFBR */
1089 case 2: /* Transfer Control. */
1094 if ((insn
& 0x002e0000) == 0) {
1098 if (s
->sist1
& LSI_SIST1_STO
) {
1099 DPRINTF("Delayed select timeout\n");
1103 cond
= jmp
= (insn
& (1 << 19)) != 0;
1104 if (cond
== jmp
&& (insn
& (1 << 21))) {
1105 DPRINTF("Compare carry %d\n", s
->carry
== jmp
);
1106 cond
= s
->carry
!= 0;
1108 if (cond
== jmp
&& (insn
& (1 << 17))) {
1109 DPRINTF("Compare phase %d %c= %d\n",
1110 (s
->sstat1
& PHASE_MASK
),
1112 ((insn
>> 24) & 7));
1113 cond
= (s
->sstat1
& PHASE_MASK
) == ((insn
>> 24) & 7);
1115 if (cond
== jmp
&& (insn
& (1 << 18))) {
1118 mask
= (~insn
>> 8) & 0xff;
1119 DPRINTF("Compare data 0x%x & 0x%x %c= 0x%x\n",
1120 s
->sfbr
, mask
, jmp
? '=' : '!', insn
& mask
);
1121 cond
= (s
->sfbr
& mask
) == (insn
& mask
);
1124 if (insn
& (1 << 23)) {
1125 /* Relative address. */
1126 addr
= s
->dsp
+ sxt24(addr
);
1128 switch ((insn
>> 27) & 7) {
1130 DPRINTF("Jump to 0x%08x\n", addr
);
1134 DPRINTF("Call 0x%08x\n", addr
);
1138 case 2: /* Return */
1139 DPRINTF("Return to 0x%08x\n", s
->temp
);
1142 case 3: /* Interrupt */
1143 DPRINTF("Interrupt 0x%08x\n", s
->dsps
);
1144 if ((insn
& (1 << 20)) != 0) {
1145 s
->istat0
|= LSI_ISTAT0_INTF
;
1148 lsi_script_dma_interrupt(s
, LSI_DSTAT_SIR
);
1152 DPRINTF("Illegal transfer control\n");
1153 lsi_script_dma_interrupt(s
, LSI_DSTAT_IID
);
1157 DPRINTF("Control condition failed\n");
1163 if ((insn
& (1 << 29)) == 0) {
1166 /* ??? The docs imply the destination address is loaded into
1167 the TEMP register. However the Linux drivers rely on
1168 the value being presrved. */
1169 dest
= read_dword(s
, s
->dsp
);
1171 lsi_memcpy(s
, dest
, addr
, insn
& 0xffffff);
1178 if (insn
& (1 << 28)) {
1179 addr
= s
->dsa
+ sxt24(addr
);
1182 reg
= (insn
>> 16) & 0xff;
1183 if (insn
& (1 << 24)) {
1184 cpu_physical_memory_read(addr
, data
, n
);
1185 DPRINTF("Load reg 0x%x size %d addr 0x%08x = %08x\n", reg
, n
,
1186 addr
, *(int *)data
);
1187 for (i
= 0; i
< n
; i
++) {
1188 lsi_reg_writeb(s
, reg
+ i
, data
[i
]);
1191 DPRINTF("Store reg 0x%x size %d addr 0x%08x\n", reg
, n
, addr
);
1192 for (i
= 0; i
< n
; i
++) {
1193 data
[i
] = lsi_reg_readb(s
, reg
+ i
);
1195 cpu_physical_memory_write(addr
, data
, n
);
1199 /* ??? Need to avoid infinite loops. */
1200 if (s
->istat1
& LSI_ISTAT1_SRUN
&& !s
->waiting
) {
1201 if (s
->dcntl
& LSI_DCNTL_SSM
) {
1202 lsi_script_dma_interrupt(s
, LSI_DSTAT_SSI
);
1207 DPRINTF("SCRIPTS execution stopped\n");
1210 static uint8_t lsi_reg_readb(LSIState
*s
, int offset
)
1213 #define CASE_GET_REG32(name, addr) \
1214 case addr: return s->name & 0xff; \
1215 case addr + 1: return (s->name >> 8) & 0xff; \
1216 case addr + 2: return (s->name >> 16) & 0xff; \
1217 case addr + 3: return (s->name >> 24) & 0xff;
1219 #ifdef DEBUG_LSI_REG
1220 DPRINTF("Read reg %x\n", offset
);
1223 case 0x00: /* SCNTL0 */
1225 case 0x01: /* SCNTL1 */
1227 case 0x02: /* SCNTL2 */
1229 case 0x03: /* SCNTL3 */
1231 case 0x04: /* SCID */
1233 case 0x05: /* SXFER */
1235 case 0x06: /* SDID */
1237 case 0x07: /* GPREG0 */
1239 case 0x08: /* Revision ID */
1241 case 0xa: /* SSID */
1243 case 0xb: /* SBCL */
1244 /* ??? This is not correct. However it's (hopefully) only
1245 used for diagnostics, so should be ok. */
1247 case 0xc: /* DSTAT */
1248 tmp
= s
->dstat
| 0x80;
1249 if ((s
->istat0
& LSI_ISTAT0_INTF
) == 0)
1253 case 0x0d: /* SSTAT0 */
1255 case 0x0e: /* SSTAT1 */
1257 case 0x0f: /* SSTAT2 */
1258 return s
->scntl1
& LSI_SCNTL1_CON
? 0 : 2;
1259 CASE_GET_REG32(dsa
, 0x10)
1260 case 0x14: /* ISTAT0 */
1262 case 0x16: /* MBOX0 */
1264 case 0x17: /* MBOX1 */
1266 case 0x18: /* CTEST0 */
1268 case 0x19: /* CTEST1 */
1270 case 0x1a: /* CTEST2 */
1271 tmp
= LSI_CTEST2_DACK
| LSI_CTEST2_CM
;
1272 if (s
->istat0
& LSI_ISTAT0_SIGP
) {
1273 s
->istat0
&= ~LSI_ISTAT0_SIGP
;
1274 tmp
|= LSI_CTEST2_SIGP
;
1277 case 0x1b: /* CTEST3 */
1279 CASE_GET_REG32(temp
, 0x1c)
1280 case 0x20: /* DFIFO */
1282 case 0x21: /* CTEST4 */
1284 case 0x22: /* CTEST5 */
1286 case 0x23: /* CTEST6 */
1288 case 0x24: /* DBC[0:7] */
1289 return s
->dbc
& 0xff;
1290 case 0x25: /* DBC[8:15] */
1291 return (s
->dbc
>> 8) & 0xff;
1292 case 0x26: /* DBC[16->23] */
1293 return (s
->dbc
>> 16) & 0xff;
1294 case 0x27: /* DCMD */
1296 CASE_GET_REG32(dsp
, 0x2c)
1297 CASE_GET_REG32(dsps
, 0x30)
1298 CASE_GET_REG32(scratch
[0], 0x34)
1299 case 0x38: /* DMODE */
1301 case 0x39: /* DIEN */
1303 case 0x3b: /* DCNTL */
1305 case 0x40: /* SIEN0 */
1307 case 0x41: /* SIEN1 */
1309 case 0x42: /* SIST0 */
1314 case 0x43: /* SIST1 */
1319 case 0x47: /* GPCNTL0 */
1321 case 0x48: /* STIME0 */
1323 case 0x4a: /* RESPID0 */
1325 case 0x4b: /* RESPID1 */
1327 case 0x4d: /* STEST1 */
1329 case 0x4e: /* STEST2 */
1331 case 0x4f: /* STEST3 */
1333 case 0x50: /* SIDL */
1334 /* This is needed by the linux drivers. We currently only update it
1335 during the MSG IN phase. */
1337 case 0x52: /* STEST4 */
1339 case 0x56: /* CCNTL0 */
1341 case 0x57: /* CCNTL1 */
1343 case 0x58: /* SBDL */
1344 /* Some drivers peek at the data bus during the MSG IN phase. */
1345 if ((s
->sstat1
& PHASE_MASK
) == PHASE_MI
)
1348 case 0x59: /* SBDL high */
1350 CASE_GET_REG32(mmrs
, 0xa0)
1351 CASE_GET_REG32(mmws
, 0xa4)
1352 CASE_GET_REG32(sfs
, 0xa8)
1353 CASE_GET_REG32(drs
, 0xac)
1354 CASE_GET_REG32(sbms
, 0xb0)
1355 CASE_GET_REG32(dmbs
, 0xb4)
1356 CASE_GET_REG32(dnad64
, 0xb8)
1357 CASE_GET_REG32(pmjad1
, 0xc0)
1358 CASE_GET_REG32(pmjad2
, 0xc4)
1359 CASE_GET_REG32(rbc
, 0xc8)
1360 CASE_GET_REG32(ua
, 0xcc)
1361 CASE_GET_REG32(ia
, 0xd4)
1362 CASE_GET_REG32(sbc
, 0xd8)
1363 CASE_GET_REG32(csbc
, 0xdc)
1365 if (offset
>= 0x5c && offset
< 0xa0) {
1368 n
= (offset
- 0x58) >> 2;
1369 shift
= (offset
& 3) * 8;
1370 return (s
->scratch
[n
] >> shift
) & 0xff;
1372 BADF("readb 0x%x\n", offset
);
1374 #undef CASE_GET_REG32
1377 static void lsi_reg_writeb(LSIState
*s
, int offset
, uint8_t val
)
1379 #define CASE_SET_REG32(name, addr) \
1380 case addr : s->name &= 0xffffff00; s->name |= val; break; \
1381 case addr + 1: s->name &= 0xffff00ff; s->name |= val << 8; break; \
1382 case addr + 2: s->name &= 0xff00ffff; s->name |= val << 16; break; \
1383 case addr + 3: s->name &= 0x00ffffff; s->name |= val << 24; break;
1385 #ifdef DEBUG_LSI_REG
1386 DPRINTF("Write reg %x = %02x\n", offset
, val
);
1389 case 0x00: /* SCNTL0 */
1391 if (val
& LSI_SCNTL0_START
) {
1392 BADF("Start sequence not implemented\n");
1395 case 0x01: /* SCNTL1 */
1396 s
->scntl1
= val
& ~LSI_SCNTL1_SST
;
1397 if (val
& LSI_SCNTL1_IARB
) {
1398 BADF("Immediate Arbritration not implemented\n");
1400 if (val
& LSI_SCNTL1_RST
) {
1401 s
->sstat0
|= LSI_SSTAT0_RST
;
1402 lsi_script_scsi_interrupt(s
, LSI_SIST0_RST
, 0);
1404 s
->sstat0
&= ~LSI_SSTAT0_RST
;
1407 case 0x02: /* SCNTL2 */
1408 val
&= ~(LSI_SCNTL2_WSR
| LSI_SCNTL2_WSS
);
1411 case 0x03: /* SCNTL3 */
1414 case 0x04: /* SCID */
1417 case 0x05: /* SXFER */
1420 case 0x06: /* SDID */
1421 if ((val
& 0xf) != (s
->ssid
& 0xf))
1422 BADF("Destination ID does not match SSID\n");
1423 s
->sdid
= val
& 0xf;
1425 case 0x07: /* GPREG0 */
1427 case 0x08: /* SFBR */
1428 /* The CPU is not allowed to write to this register. However the
1429 SCRIPTS register move instructions are. */
1432 case 0x0c: case 0x0d: case 0x0e: case 0x0f:
1433 /* Linux writes to these readonly registers on startup. */
1435 CASE_SET_REG32(dsa
, 0x10)
1436 case 0x14: /* ISTAT0 */
1437 s
->istat0
= (s
->istat0
& 0x0f) | (val
& 0xf0);
1438 if (val
& LSI_ISTAT0_ABRT
) {
1439 lsi_script_dma_interrupt(s
, LSI_DSTAT_ABRT
);
1441 if (val
& LSI_ISTAT0_INTF
) {
1442 s
->istat0
&= ~LSI_ISTAT0_INTF
;
1445 if (s
->waiting
== 1 && val
& LSI_ISTAT0_SIGP
) {
1446 DPRINTF("Woken by SIGP\n");
1449 lsi_execute_script(s
);
1451 if (val
& LSI_ISTAT0_SRST
) {
1455 case 0x16: /* MBOX0 */
1458 case 0x17: /* MBOX1 */
1461 case 0x1b: /* CTEST3 */
1462 s
->ctest3
= val
& 0x0f;
1464 CASE_SET_REG32(temp
, 0x1c)
1465 case 0x21: /* CTEST4 */
1467 BADF("Unimplemented CTEST4-FBL 0x%x\n", val
);
1471 case 0x22: /* CTEST5 */
1472 if (val
& (LSI_CTEST5_ADCK
| LSI_CTEST5_BBCK
)) {
1473 BADF("CTEST5 DMA increment not implemented\n");
1477 case 0x2c: /* DSP[0:7] */
1478 s
->dsp
&= 0xffffff00;
1481 case 0x2d: /* DSP[8:15] */
1482 s
->dsp
&= 0xffff00ff;
1485 case 0x2e: /* DSP[16:23] */
1486 s
->dsp
&= 0xff00ffff;
1487 s
->dsp
|= val
<< 16;
1489 case 0x2f: /* DSP[24:31] */
1490 s
->dsp
&= 0x00ffffff;
1491 s
->dsp
|= val
<< 24;
1492 if ((s
->dmode
& LSI_DMODE_MAN
) == 0
1493 && (s
->istat1
& LSI_ISTAT1_SRUN
) == 0)
1494 lsi_execute_script(s
);
1496 CASE_SET_REG32(dsps
, 0x30)
1497 CASE_SET_REG32(scratch
[0], 0x34)
1498 case 0x38: /* DMODE */
1499 if (val
& (LSI_DMODE_SIOM
| LSI_DMODE_DIOM
)) {
1500 BADF("IO mappings not implemented\n");
1504 case 0x39: /* DIEN */
1508 case 0x3b: /* DCNTL */
1509 s
->dcntl
= val
& ~(LSI_DCNTL_PFF
| LSI_DCNTL_STD
);
1510 if ((val
& LSI_DCNTL_STD
) && (s
->istat1
& LSI_ISTAT1_SRUN
) == 0)
1511 lsi_execute_script(s
);
1513 case 0x40: /* SIEN0 */
1517 case 0x41: /* SIEN1 */
1521 case 0x47: /* GPCNTL0 */
1523 case 0x48: /* STIME0 */
1526 case 0x49: /* STIME1 */
1528 DPRINTF("General purpose timer not implemented\n");
1529 /* ??? Raising the interrupt immediately seems to be sufficient
1530 to keep the FreeBSD driver happy. */
1531 lsi_script_scsi_interrupt(s
, 0, LSI_SIST1_GEN
);
1534 case 0x4a: /* RESPID0 */
1537 case 0x4b: /* RESPID1 */
1540 case 0x4d: /* STEST1 */
1543 case 0x4e: /* STEST2 */
1545 BADF("Low level mode not implemented\n");
1549 case 0x4f: /* STEST3 */
1551 BADF("SCSI FIFO test mode not implemented\n");
1555 case 0x56: /* CCNTL0 */
1558 case 0x57: /* CCNTL1 */
1561 CASE_SET_REG32(mmrs
, 0xa0)
1562 CASE_SET_REG32(mmws
, 0xa4)
1563 CASE_SET_REG32(sfs
, 0xa8)
1564 CASE_SET_REG32(drs
, 0xac)
1565 CASE_SET_REG32(sbms
, 0xb0)
1566 CASE_SET_REG32(dmbs
, 0xb4)
1567 CASE_SET_REG32(dnad64
, 0xb8)
1568 CASE_SET_REG32(pmjad1
, 0xc0)
1569 CASE_SET_REG32(pmjad2
, 0xc4)
1570 CASE_SET_REG32(rbc
, 0xc8)
1571 CASE_SET_REG32(ua
, 0xcc)
1572 CASE_SET_REG32(ia
, 0xd4)
1573 CASE_SET_REG32(sbc
, 0xd8)
1574 CASE_SET_REG32(csbc
, 0xdc)
1576 if (offset
>= 0x5c && offset
< 0xa0) {
1579 n
= (offset
- 0x58) >> 2;
1580 shift
= (offset
& 3) * 8;
1581 s
->scratch
[n
] &= ~(0xff << shift
);
1582 s
->scratch
[n
] |= (val
& 0xff) << shift
;
1584 BADF("Unhandled writeb 0x%x = 0x%x\n", offset
, val
);
1587 #undef CASE_SET_REG32
1590 static void lsi_mmio_writeb(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1592 LSIState
*s
= (LSIState
*)opaque
;
1594 lsi_reg_writeb(s
, addr
& 0xff, val
);
1597 static void lsi_mmio_writew(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1599 LSIState
*s
= (LSIState
*)opaque
;
1602 lsi_reg_writeb(s
, addr
, val
& 0xff);
1603 lsi_reg_writeb(s
, addr
+ 1, (val
>> 8) & 0xff);
1606 static void lsi_mmio_writel(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1608 LSIState
*s
= (LSIState
*)opaque
;
1611 lsi_reg_writeb(s
, addr
, val
& 0xff);
1612 lsi_reg_writeb(s
, addr
+ 1, (val
>> 8) & 0xff);
1613 lsi_reg_writeb(s
, addr
+ 2, (val
>> 16) & 0xff);
1614 lsi_reg_writeb(s
, addr
+ 3, (val
>> 24) & 0xff);
1617 static uint32_t lsi_mmio_readb(void *opaque
, target_phys_addr_t addr
)
1619 LSIState
*s
= (LSIState
*)opaque
;
1621 return lsi_reg_readb(s
, addr
& 0xff);
1624 static uint32_t lsi_mmio_readw(void *opaque
, target_phys_addr_t addr
)
1626 LSIState
*s
= (LSIState
*)opaque
;
1630 val
= lsi_reg_readb(s
, addr
);
1631 val
|= lsi_reg_readb(s
, addr
+ 1) << 8;
1635 static uint32_t lsi_mmio_readl(void *opaque
, target_phys_addr_t addr
)
1637 LSIState
*s
= (LSIState
*)opaque
;
1640 val
= lsi_reg_readb(s
, addr
);
1641 val
|= lsi_reg_readb(s
, addr
+ 1) << 8;
1642 val
|= lsi_reg_readb(s
, addr
+ 2) << 16;
1643 val
|= lsi_reg_readb(s
, addr
+ 3) << 24;
1647 static CPUReadMemoryFunc
*lsi_mmio_readfn
[3] = {
1653 static CPUWriteMemoryFunc
*lsi_mmio_writefn
[3] = {
1659 static void lsi_ram_writeb(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1661 LSIState
*s
= (LSIState
*)opaque
;
1666 newval
= s
->script_ram
[addr
>> 2];
1667 shift
= (addr
& 3) * 8;
1668 newval
&= ~(0xff << shift
);
1669 newval
|= val
<< shift
;
1670 s
->script_ram
[addr
>> 2] = newval
;
1673 static void lsi_ram_writew(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1675 LSIState
*s
= (LSIState
*)opaque
;
1679 newval
= s
->script_ram
[addr
>> 2];
1681 newval
= (newval
& 0xffff) | (val
<< 16);
1683 newval
= (newval
& 0xffff0000) | val
;
1685 s
->script_ram
[addr
>> 2] = newval
;
1689 static void lsi_ram_writel(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1691 LSIState
*s
= (LSIState
*)opaque
;
1694 s
->script_ram
[addr
>> 2] = val
;
1697 static uint32_t lsi_ram_readb(void *opaque
, target_phys_addr_t addr
)
1699 LSIState
*s
= (LSIState
*)opaque
;
1703 val
= s
->script_ram
[addr
>> 2];
1704 val
>>= (addr
& 3) * 8;
1708 static uint32_t lsi_ram_readw(void *opaque
, target_phys_addr_t addr
)
1710 LSIState
*s
= (LSIState
*)opaque
;
1714 val
= s
->script_ram
[addr
>> 2];
1717 return le16_to_cpu(val
);
1720 static uint32_t lsi_ram_readl(void *opaque
, target_phys_addr_t addr
)
1722 LSIState
*s
= (LSIState
*)opaque
;
1725 return le32_to_cpu(s
->script_ram
[addr
>> 2]);
1728 static CPUReadMemoryFunc
*lsi_ram_readfn
[3] = {
1734 static CPUWriteMemoryFunc
*lsi_ram_writefn
[3] = {
1740 static uint32_t lsi_io_readb(void *opaque
, uint32_t addr
)
1742 LSIState
*s
= (LSIState
*)opaque
;
1743 return lsi_reg_readb(s
, addr
& 0xff);
1746 static uint32_t lsi_io_readw(void *opaque
, uint32_t addr
)
1748 LSIState
*s
= (LSIState
*)opaque
;
1751 val
= lsi_reg_readb(s
, addr
);
1752 val
|= lsi_reg_readb(s
, addr
+ 1) << 8;
1756 static uint32_t lsi_io_readl(void *opaque
, uint32_t addr
)
1758 LSIState
*s
= (LSIState
*)opaque
;
1761 val
= lsi_reg_readb(s
, addr
);
1762 val
|= lsi_reg_readb(s
, addr
+ 1) << 8;
1763 val
|= lsi_reg_readb(s
, addr
+ 2) << 16;
1764 val
|= lsi_reg_readb(s
, addr
+ 3) << 24;
1768 static void lsi_io_writeb(void *opaque
, uint32_t addr
, uint32_t val
)
1770 LSIState
*s
= (LSIState
*)opaque
;
1771 lsi_reg_writeb(s
, addr
& 0xff, val
);
1774 static void lsi_io_writew(void *opaque
, uint32_t addr
, uint32_t val
)
1776 LSIState
*s
= (LSIState
*)opaque
;
1778 lsi_reg_writeb(s
, addr
, val
& 0xff);
1779 lsi_reg_writeb(s
, addr
+ 1, (val
>> 8) & 0xff);
1782 static void lsi_io_writel(void *opaque
, uint32_t addr
, uint32_t val
)
1784 LSIState
*s
= (LSIState
*)opaque
;
1786 lsi_reg_writeb(s
, addr
, val
& 0xff);
1787 lsi_reg_writeb(s
, addr
+ 1, (val
>> 8) & 0xff);
1788 lsi_reg_writeb(s
, addr
+ 2, (val
>> 16) & 0xff);
1789 lsi_reg_writeb(s
, addr
+ 3, (val
>> 24) & 0xff);
1792 static void lsi_io_mapfunc(PCIDevice
*pci_dev
, int region_num
,
1793 uint32_t addr
, uint32_t size
, int type
)
1795 LSIState
*s
= (LSIState
*)pci_dev
;
1797 DPRINTF("Mapping IO at %08x\n", addr
);
1799 register_ioport_write(addr
, 256, 1, lsi_io_writeb
, s
);
1800 register_ioport_read(addr
, 256, 1, lsi_io_readb
, s
);
1801 register_ioport_write(addr
, 256, 2, lsi_io_writew
, s
);
1802 register_ioport_read(addr
, 256, 2, lsi_io_readw
, s
);
1803 register_ioport_write(addr
, 256, 4, lsi_io_writel
, s
);
1804 register_ioport_read(addr
, 256, 4, lsi_io_readl
, s
);
1807 static void lsi_ram_mapfunc(PCIDevice
*pci_dev
, int region_num
,
1808 uint32_t addr
, uint32_t size
, int type
)
1810 LSIState
*s
= (LSIState
*)pci_dev
;
1812 DPRINTF("Mapping ram at %08x\n", addr
);
1813 s
->script_ram_base
= addr
;
1814 cpu_register_physical_memory(addr
+ 0, 0x2000, s
->ram_io_addr
);
1817 static void lsi_mmio_mapfunc(PCIDevice
*pci_dev
, int region_num
,
1818 uint32_t addr
, uint32_t size
, int type
)
1820 LSIState
*s
= (LSIState
*)pci_dev
;
1822 DPRINTF("Mapping registers at %08x\n", addr
);
1823 cpu_register_physical_memory(addr
+ 0, 0x400, s
->mmio_io_addr
);
1826 void lsi_scsi_attach(void *opaque
, BlockDriverState
*bd
, int id
)
1828 LSIState
*s
= (LSIState
*)opaque
;
1831 for (id
= 0; id
< LSI_MAX_DEVS
; id
++) {
1832 if (s
->scsi_dev
[id
] == NULL
)
1836 if (id
>= LSI_MAX_DEVS
) {
1837 BADF("Bad Device ID %d\n", id
);
1840 if (s
->scsi_dev
[id
]) {
1841 DPRINTF("Destroying device %d\n", id
);
1842 s
->scsi_dev
[id
]->destroy(s
->scsi_dev
[id
]);
1844 DPRINTF("Attaching block device %d\n", id
);
1845 s
->scsi_dev
[id
] = scsi_generic_init(bd
, 1, lsi_command_complete
, s
);
1846 if (s
->scsi_dev
[id
] == NULL
)
1847 s
->scsi_dev
[id
] = scsi_disk_init(bd
, 1, lsi_command_complete
, s
);
1850 void *lsi_scsi_init(PCIBus
*bus
, int devfn
)
1854 s
= (LSIState
*)pci_register_device(bus
, "LSI53C895A SCSI HBA",
1855 sizeof(*s
), devfn
, NULL
, NULL
);
1857 fprintf(stderr
, "lsi-scsi: Failed to register PCI device\n");
1861 s
->pci_dev
.config
[0x00] = 0x00;
1862 s
->pci_dev
.config
[0x01] = 0x10;
1863 s
->pci_dev
.config
[0x02] = 0x12;
1864 s
->pci_dev
.config
[0x03] = 0x00;
1865 s
->pci_dev
.config
[0x0b] = 0x01;
1866 s
->pci_dev
.config
[0x3d] = 0x01; /* interrupt pin 1 */
1868 s
->mmio_io_addr
= cpu_register_io_memory(0, lsi_mmio_readfn
,
1869 lsi_mmio_writefn
, s
);
1870 s
->ram_io_addr
= cpu_register_io_memory(0, lsi_ram_readfn
,
1871 lsi_ram_writefn
, s
);
1873 pci_register_io_region((struct PCIDevice
*)s
, 0, 256,
1874 PCI_ADDRESS_SPACE_IO
, lsi_io_mapfunc
);
1875 pci_register_io_region((struct PCIDevice
*)s
, 1, 0x400,
1876 PCI_ADDRESS_SPACE_MEM
, lsi_mmio_mapfunc
);
1877 pci_register_io_region((struct PCIDevice
*)s
, 2, 0x2000,
1878 PCI_ADDRESS_SPACE_MEM
, lsi_ram_mapfunc
);
1879 s
->queue
= qemu_malloc(sizeof(lsi_queue
));
1881 s
->active_commands
= 0;