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"
16 #include "block_int.h"
19 //#define DEBUG_LSI_REG
22 #define DPRINTF(fmt, args...) \
23 do { printf("lsi_scsi: " fmt , ##args); } while (0)
24 #define BADF(fmt, args...) \
25 do { fprintf(stderr, "lsi_scsi: error: " fmt , ##args); exit(1);} while (0)
27 #define DPRINTF(fmt, args...) do {} while(0)
28 #define BADF(fmt, args...) \
29 do { fprintf(stderr, "lsi_scsi: error: " fmt , ##args);} while (0)
32 #define LSI_SCNTL0_TRG 0x01
33 #define LSI_SCNTL0_AAP 0x02
34 #define LSI_SCNTL0_EPC 0x08
35 #define LSI_SCNTL0_WATN 0x10
36 #define LSI_SCNTL0_START 0x20
38 #define LSI_SCNTL1_SST 0x01
39 #define LSI_SCNTL1_IARB 0x02
40 #define LSI_SCNTL1_AESP 0x04
41 #define LSI_SCNTL1_RST 0x08
42 #define LSI_SCNTL1_CON 0x10
43 #define LSI_SCNTL1_DHP 0x20
44 #define LSI_SCNTL1_ADB 0x40
45 #define LSI_SCNTL1_EXC 0x80
47 #define LSI_SCNTL2_WSR 0x01
48 #define LSI_SCNTL2_VUE0 0x02
49 #define LSI_SCNTL2_VUE1 0x04
50 #define LSI_SCNTL2_WSS 0x08
51 #define LSI_SCNTL2_SLPHBEN 0x10
52 #define LSI_SCNTL2_SLPMD 0x20
53 #define LSI_SCNTL2_CHM 0x40
54 #define LSI_SCNTL2_SDU 0x80
56 #define LSI_ISTAT0_DIP 0x01
57 #define LSI_ISTAT0_SIP 0x02
58 #define LSI_ISTAT0_INTF 0x04
59 #define LSI_ISTAT0_CON 0x08
60 #define LSI_ISTAT0_SEM 0x10
61 #define LSI_ISTAT0_SIGP 0x20
62 #define LSI_ISTAT0_SRST 0x40
63 #define LSI_ISTAT0_ABRT 0x80
65 #define LSI_ISTAT1_SI 0x01
66 #define LSI_ISTAT1_SRUN 0x02
67 #define LSI_ISTAT1_FLSH 0x04
69 #define LSI_SSTAT0_SDP0 0x01
70 #define LSI_SSTAT0_RST 0x02
71 #define LSI_SSTAT0_WOA 0x04
72 #define LSI_SSTAT0_LOA 0x08
73 #define LSI_SSTAT0_AIP 0x10
74 #define LSI_SSTAT0_OLF 0x20
75 #define LSI_SSTAT0_ORF 0x40
76 #define LSI_SSTAT0_ILF 0x80
78 #define LSI_SIST0_PAR 0x01
79 #define LSI_SIST0_RST 0x02
80 #define LSI_SIST0_UDC 0x04
81 #define LSI_SIST0_SGE 0x08
82 #define LSI_SIST0_RSL 0x10
83 #define LSI_SIST0_SEL 0x20
84 #define LSI_SIST0_CMP 0x40
85 #define LSI_SIST0_MA 0x80
87 #define LSI_SIST1_HTH 0x01
88 #define LSI_SIST1_GEN 0x02
89 #define LSI_SIST1_STO 0x04
90 #define LSI_SIST1_SBMC 0x10
92 #define LSI_SOCL_IO 0x01
93 #define LSI_SOCL_CD 0x02
94 #define LSI_SOCL_MSG 0x04
95 #define LSI_SOCL_ATN 0x08
96 #define LSI_SOCL_SEL 0x10
97 #define LSI_SOCL_BSY 0x20
98 #define LSI_SOCL_ACK 0x40
99 #define LSI_SOCL_REQ 0x80
101 #define LSI_DSTAT_IID 0x01
102 #define LSI_DSTAT_SIR 0x04
103 #define LSI_DSTAT_SSI 0x08
104 #define LSI_DSTAT_ABRT 0x10
105 #define LSI_DSTAT_BF 0x20
106 #define LSI_DSTAT_MDPE 0x40
107 #define LSI_DSTAT_DFE 0x80
109 #define LSI_DCNTL_COM 0x01
110 #define LSI_DCNTL_IRQD 0x02
111 #define LSI_DCNTL_STD 0x04
112 #define LSI_DCNTL_IRQM 0x08
113 #define LSI_DCNTL_SSM 0x10
114 #define LSI_DCNTL_PFEN 0x20
115 #define LSI_DCNTL_PFF 0x40
116 #define LSI_DCNTL_CLSE 0x80
118 #define LSI_DMODE_MAN 0x01
119 #define LSI_DMODE_BOF 0x02
120 #define LSI_DMODE_ERMP 0x04
121 #define LSI_DMODE_ERL 0x08
122 #define LSI_DMODE_DIOM 0x10
123 #define LSI_DMODE_SIOM 0x20
125 #define LSI_CTEST2_DACK 0x01
126 #define LSI_CTEST2_DREQ 0x02
127 #define LSI_CTEST2_TEOP 0x04
128 #define LSI_CTEST2_PCICIE 0x08
129 #define LSI_CTEST2_CM 0x10
130 #define LSI_CTEST2_CIO 0x20
131 #define LSI_CTEST2_SIGP 0x40
132 #define LSI_CTEST2_DDIR 0x80
134 #define LSI_CTEST5_BL2 0x04
135 #define LSI_CTEST5_DDIR 0x08
136 #define LSI_CTEST5_MASR 0x10
137 #define LSI_CTEST5_DFSN 0x20
138 #define LSI_CTEST5_BBCK 0x40
139 #define LSI_CTEST5_ADCK 0x80
141 #define LSI_CCNTL0_DILS 0x01
142 #define LSI_CCNTL0_DISFC 0x10
143 #define LSI_CCNTL0_ENNDJ 0x20
144 #define LSI_CCNTL0_PMJCTL 0x40
145 #define LSI_CCNTL0_ENPMJ 0x80
147 #define LSI_CCNTL1_EN64DBMV 0x01
148 #define LSI_CCNTL1_EN64TIBMV 0x02
149 #define LSI_CCNTL1_64TIMOD 0x04
150 #define LSI_CCNTL1_DDAC 0x08
151 #define LSI_CCNTL1_ZMOD 0x80
153 #define LSI_CCNTL1_40BIT (LSI_CCNTL1_EN64TIBMV|LSI_CCNTL1_64TIMOD)
163 /* Maximum length of MSG IN data. */
164 #define LSI_MAX_MSGIN_LEN 8
166 /* Flag set if this is a tagged command. */
167 #define LSI_TAG_VALID (1 << 16)
179 uint32_t script_ram_base
;
181 int carry
; /* ??? Should this be an a visible register somewhere? */
183 /* Action to take at the end of a MSG IN phase.
184 0 = COMMAND, 1 = disconect, 2 = DATA OUT, 3 = DATA IN. */
187 uint8_t msg
[LSI_MAX_MSGIN_LEN
];
188 /* 0 if SCRIPTS are running or stopped.
189 * 1 if a Wait Reselect instruction has been issued.
190 * 2 if processing DMA from lsi_execute_script.
191 * 3 if a DMA operation is in progress. */
193 SCSIDevice
*scsi_dev
[LSI_MAX_DEVS
];
194 SCSIDevice
*current_dev
;
196 /* The tag is a combination of the device ID and the SCSI tag. */
197 uint32_t current_tag
;
198 uint32_t current_dma_len
;
199 int command_complete
;
264 uint32_t scratch
[18]; /* SCRATCHA-SCRATCHR */
267 /* Script ram is stored as 32-bit words in host byteorder. */
268 uint32_t script_ram
[2048];
271 static void lsi_soft_reset(LSIState
*s
)
281 memset(s
->scratch
, 0, sizeof(s
->scratch
));
337 static int lsi_dma_40bit(LSIState
*s
)
339 if ((s
->ccntl1
& LSI_CCNTL1_40BIT
) == LSI_CCNTL1_40BIT
)
344 static int lsi_dma_ti64bit(LSIState
*s
)
346 if ((s
->ccntl1
& LSI_CCNTL1_EN64TIBMV
) == LSI_CCNTL1_EN64TIBMV
)
351 static int lsi_dma_64bit(LSIState
*s
)
353 if ((s
->ccntl1
& LSI_CCNTL1_EN64DBMV
) == LSI_CCNTL1_EN64DBMV
)
358 static uint8_t lsi_reg_readb(LSIState
*s
, int offset
);
359 static void lsi_reg_writeb(LSIState
*s
, int offset
, uint8_t val
);
360 static void lsi_execute_script(LSIState
*s
);
362 static inline uint32_t read_dword(LSIState
*s
, uint32_t addr
)
366 /* Optimize reading from SCRIPTS RAM. */
367 if ((addr
& 0xffffe000) == s
->script_ram_base
) {
368 return s
->script_ram
[(addr
& 0x1fff) >> 2];
370 cpu_physical_memory_read(addr
, (uint8_t *)&buf
, 4);
371 return cpu_to_le32(buf
);
374 static void lsi_stop_script(LSIState
*s
)
376 s
->istat1
&= ~LSI_ISTAT1_SRUN
;
379 static void lsi_update_irq(LSIState
*s
)
382 static int last_level
;
384 /* It's unclear whether the DIP/SIP bits should be cleared when the
385 Interrupt Status Registers are cleared or when istat0 is read.
386 We currently do the formwer, which seems to work. */
389 if (s
->dstat
& s
->dien
)
391 s
->istat0
|= LSI_ISTAT0_DIP
;
393 s
->istat0
&= ~LSI_ISTAT0_DIP
;
396 if (s
->sist0
|| s
->sist1
) {
397 if ((s
->sist0
& s
->sien0
) || (s
->sist1
& s
->sien1
))
399 s
->istat0
|= LSI_ISTAT0_SIP
;
401 s
->istat0
&= ~LSI_ISTAT0_SIP
;
403 if (s
->istat0
& LSI_ISTAT0_INTF
)
406 if (level
!= last_level
) {
407 DPRINTF("Update IRQ level %d dstat %02x sist %02x%02x\n",
408 level
, s
->dstat
, s
->sist1
, s
->sist0
);
411 qemu_set_irq(s
->pci_dev
.irq
[0], level
);
414 /* Stop SCRIPTS execution and raise a SCSI interrupt. */
415 static void lsi_script_scsi_interrupt(LSIState
*s
, int stat0
, int stat1
)
420 DPRINTF("SCSI Interrupt 0x%02x%02x prev 0x%02x%02x\n",
421 stat1
, stat0
, s
->sist1
, s
->sist0
);
424 /* Stop processor on fatal or unmasked interrupt. As a special hack
425 we don't stop processing when raising STO. Instead continue
426 execution and stop at the next insn that accesses the SCSI bus. */
427 mask0
= s
->sien0
| ~(LSI_SIST0_CMP
| LSI_SIST0_SEL
| LSI_SIST0_RSL
);
428 mask1
= s
->sien1
| ~(LSI_SIST1_GEN
| LSI_SIST1_HTH
);
429 mask1
&= ~LSI_SIST1_STO
;
430 if (s
->sist0
& mask0
|| s
->sist1
& mask1
) {
436 /* Stop SCRIPTS execution and raise a DMA interrupt. */
437 static void lsi_script_dma_interrupt(LSIState
*s
, int stat
)
439 DPRINTF("DMA Interrupt 0x%x prev 0x%x\n", stat
, s
->dstat
);
445 static inline void lsi_set_phase(LSIState
*s
, int phase
)
447 s
->sstat1
= (s
->sstat1
& ~PHASE_MASK
) | phase
;
450 static void lsi_bad_phase(LSIState
*s
, int out
, int new_phase
)
452 /* Trigger a phase mismatch. */
453 if (s
->ccntl0
& LSI_CCNTL0_ENPMJ
) {
454 if ((s
->ccntl0
& LSI_CCNTL0_PMJCTL
) || out
) {
459 DPRINTF("Data phase mismatch jump to %08x\n", s
->dsp
);
461 DPRINTF("Phase mismatch interrupt\n");
462 lsi_script_scsi_interrupt(s
, LSI_SIST0_MA
, 0);
465 lsi_set_phase(s
, new_phase
);
469 /* Resume SCRIPTS execution after a DMA operation. */
470 static void lsi_resume_script(LSIState
*s
)
472 if (s
->waiting
!= 2) {
474 lsi_execute_script(s
);
480 /* Initiate a SCSI layer data transfer. */
481 static void lsi_do_dma(LSIState
*s
, int out
)
484 target_phys_addr_t addr
;
486 if (!s
->current_dma_len
) {
487 /* Wait until data is available. */
488 DPRINTF("DMA no data available\n");
493 if (count
> s
->current_dma_len
)
494 count
= s
->current_dma_len
;
497 /* both 40 and Table Indirect 64-bit DMAs store upper bits in dnad64 */
498 if (lsi_dma_40bit(s
) || lsi_dma_ti64bit(s
))
499 addr
|= ((uint64_t)s
->dnad64
<< 32);
501 addr
|= ((uint64_t)s
->dbms
<< 32);
503 addr
|= ((uint64_t)s
->sbms
<< 32);
505 DPRINTF("DMA addr=0x" TARGET_FMT_plx
" len=%d\n", addr
, count
);
510 if (s
->dma_buf
== NULL
) {
511 s
->dma_buf
= s
->current_dev
->get_buf(s
->current_dev
,
515 /* ??? Set SFBR to first data byte. */
517 cpu_physical_memory_read(addr
, s
->dma_buf
, count
);
519 cpu_physical_memory_write(addr
, s
->dma_buf
, count
);
521 s
->current_dma_len
-= count
;
522 if (s
->current_dma_len
== 0) {
525 /* Write the data. */
526 s
->current_dev
->write_data(s
->current_dev
, s
->current_tag
);
528 /* Request any remaining data. */
529 s
->current_dev
->read_data(s
->current_dev
, s
->current_tag
);
533 lsi_resume_script(s
);
538 /* Add a command to the queue. */
539 static void lsi_queue_command(LSIState
*s
)
543 DPRINTF("Queueing tag=0x%x\n", s
->current_tag
);
544 if (s
->queue_len
== s
->active_commands
) {
546 s
->queue
= qemu_realloc(s
->queue
, s
->queue_len
* sizeof(lsi_queue
));
548 p
= &s
->queue
[s
->active_commands
++];
549 p
->tag
= s
->current_tag
;
551 p
->out
= (s
->sstat1
& PHASE_MASK
) == PHASE_DO
;
554 /* Queue a byte for a MSG IN phase. */
555 static void lsi_add_msg_byte(LSIState
*s
, uint8_t data
)
557 if (s
->msg_len
>= LSI_MAX_MSGIN_LEN
) {
558 BADF("MSG IN data too long\n");
560 DPRINTF("MSG IN 0x%02x\n", data
);
561 s
->msg
[s
->msg_len
++] = data
;
565 /* Perform reselection to continue a command. */
566 static void lsi_reselect(LSIState
*s
, uint32_t tag
)
573 for (n
= 0; n
< s
->active_commands
; n
++) {
578 if (n
== s
->active_commands
) {
579 BADF("Reselected non-existant command tag=0x%x\n", tag
);
582 id
= (tag
>> 8) & 0xf;
584 DPRINTF("Reselected target %d\n", id
);
585 s
->current_dev
= s
->scsi_dev
[id
];
586 s
->current_tag
= tag
;
587 s
->scntl1
|= LSI_SCNTL1_CON
;
588 lsi_set_phase(s
, PHASE_MI
);
589 s
->msg_action
= p
->out
? 2 : 3;
590 s
->current_dma_len
= p
->pending
;
592 lsi_add_msg_byte(s
, 0x80);
593 if (s
->current_tag
& LSI_TAG_VALID
) {
594 lsi_add_msg_byte(s
, 0x20);
595 lsi_add_msg_byte(s
, tag
& 0xff);
598 s
->active_commands
--;
599 if (n
!= s
->active_commands
) {
600 s
->queue
[n
] = s
->queue
[s
->active_commands
];
604 /* Record that data is available for a queued command. Returns zero if
605 the device was reselected, nonzero if the IO is deferred. */
606 static int lsi_queue_tag(LSIState
*s
, uint32_t tag
, uint32_t arg
)
610 for (i
= 0; i
< s
->active_commands
; i
++) {
614 BADF("Multiple IO pending for tag %d\n", tag
);
617 if (s
->waiting
== 1) {
618 /* Reselect device. */
619 lsi_reselect(s
, tag
);
622 DPRINTF("Queueing IO tag=0x%x\n", tag
);
628 BADF("IO with unknown tag %d\n", tag
);
632 /* Callback to indicate that the SCSI layer has completed a transfer. */
633 static void lsi_command_complete(void *opaque
, int reason
, uint32_t tag
,
636 LSIState
*s
= (LSIState
*)opaque
;
639 out
= (s
->sstat1
& PHASE_MASK
) == PHASE_DO
;
640 if (reason
== SCSI_REASON_DONE
) {
641 DPRINTF("Command complete sense=%d\n", (int)arg
);
643 s
->command_complete
= 2;
644 if (s
->waiting
&& s
->dbc
!= 0) {
645 /* Raise phase mismatch for short transfers. */
646 lsi_bad_phase(s
, out
, PHASE_ST
);
648 lsi_set_phase(s
, PHASE_ST
);
650 lsi_resume_script(s
);
654 if (s
->waiting
== 1 || tag
!= s
->current_tag
) {
655 if (lsi_queue_tag(s
, tag
, arg
))
658 DPRINTF("Data ready tag=0x%x len=%d\n", tag
, arg
);
659 s
->current_dma_len
= arg
;
660 s
->command_complete
= 1;
663 if (s
->waiting
== 1 || s
->dbc
== 0) {
664 lsi_resume_script(s
);
670 static void lsi_do_command(LSIState
*s
)
675 DPRINTF("Send command len=%d\n", s
->dbc
);
678 cpu_physical_memory_read(s
->dnad
, buf
, s
->dbc
);
680 s
->command_complete
= 0;
681 n
= s
->current_dev
->send_command(s
->current_dev
, s
->current_tag
, buf
,
684 lsi_set_phase(s
, PHASE_DI
);
685 s
->current_dev
->read_data(s
->current_dev
, s
->current_tag
);
687 lsi_set_phase(s
, PHASE_DO
);
688 s
->current_dev
->write_data(s
->current_dev
, s
->current_tag
);
691 if (!s
->command_complete
) {
693 /* Command did not complete immediately so disconnect. */
694 lsi_add_msg_byte(s
, 2); /* SAVE DATA POINTER */
695 lsi_add_msg_byte(s
, 4); /* DISCONNECT */
697 lsi_set_phase(s
, PHASE_MI
);
699 lsi_queue_command(s
);
701 /* wait command complete */
702 lsi_set_phase(s
, PHASE_DI
);
707 static void lsi_do_status(LSIState
*s
)
710 DPRINTF("Get status len=%d sense=%d\n", s
->dbc
, s
->sense
);
712 BADF("Bad Status move\n");
716 cpu_physical_memory_write(s
->dnad
, &sense
, 1);
717 lsi_set_phase(s
, PHASE_MI
);
719 lsi_add_msg_byte(s
, 0); /* COMMAND COMPLETE */
722 static void lsi_disconnect(LSIState
*s
)
724 s
->scntl1
&= ~LSI_SCNTL1_CON
;
725 s
->sstat1
&= ~PHASE_MASK
;
728 static void lsi_do_msgin(LSIState
*s
)
731 DPRINTF("Message in len=%d/%d\n", s
->dbc
, s
->msg_len
);
736 cpu_physical_memory_write(s
->dnad
, s
->msg
, len
);
737 /* Linux drivers rely on the last byte being in the SIDL. */
738 s
->sidl
= s
->msg
[len
- 1];
741 memmove(s
->msg
, s
->msg
+ len
, s
->msg_len
);
743 /* ??? Check if ATN (not yet implemented) is asserted and maybe
744 switch to PHASE_MO. */
745 switch (s
->msg_action
) {
747 lsi_set_phase(s
, PHASE_CMD
);
753 lsi_set_phase(s
, PHASE_DO
);
756 lsi_set_phase(s
, PHASE_DI
);
764 /* Read the next byte during a MSGOUT phase. */
765 static uint8_t lsi_get_msgbyte(LSIState
*s
)
768 cpu_physical_memory_read(s
->dnad
, &data
, 1);
774 static void lsi_do_msgout(LSIState
*s
)
779 DPRINTF("MSG out len=%d\n", s
->dbc
);
781 msg
= lsi_get_msgbyte(s
);
786 DPRINTF("MSG: Disconnect\n");
790 DPRINTF("MSG: No Operation\n");
791 lsi_set_phase(s
, PHASE_CMD
);
794 len
= lsi_get_msgbyte(s
);
795 msg
= lsi_get_msgbyte(s
);
796 DPRINTF("Extended message 0x%x (len %d)\n", msg
, len
);
799 DPRINTF("SDTR (ignored)\n");
803 DPRINTF("WDTR (ignored)\n");
810 case 0x20: /* SIMPLE queue */
811 s
->current_tag
|= lsi_get_msgbyte(s
) | LSI_TAG_VALID
;
812 DPRINTF("SIMPLE queue tag=0x%x\n", s
->current_tag
& 0xff);
814 case 0x21: /* HEAD of queue */
815 BADF("HEAD queue not implemented\n");
816 s
->current_tag
|= lsi_get_msgbyte(s
) | LSI_TAG_VALID
;
818 case 0x22: /* ORDERED queue */
819 BADF("ORDERED queue not implemented\n");
820 s
->current_tag
|= lsi_get_msgbyte(s
) | LSI_TAG_VALID
;
823 if ((msg
& 0x80) == 0) {
826 s
->current_lun
= msg
& 7;
827 DPRINTF("Select LUN %d\n", s
->current_lun
);
828 lsi_set_phase(s
, PHASE_CMD
);
834 BADF("Unimplemented message 0x%02x\n", msg
);
835 lsi_set_phase(s
, PHASE_MI
);
836 lsi_add_msg_byte(s
, 7); /* MESSAGE REJECT */
840 /* Sign extend a 24-bit value. */
841 static inline int32_t sxt24(int32_t n
)
843 return (n
<< 8) >> 8;
846 static void lsi_memcpy(LSIState
*s
, uint32_t dest
, uint32_t src
, int count
)
849 uint8_t buf
[TARGET_PAGE_SIZE
];
851 DPRINTF("memcpy dest 0x%08x src 0x%08x count %d\n", dest
, src
, count
);
853 n
= (count
> TARGET_PAGE_SIZE
) ? TARGET_PAGE_SIZE
: count
;
854 cpu_physical_memory_read(src
, buf
, n
);
855 cpu_physical_memory_write(dest
, buf
, n
);
862 static void lsi_wait_reselect(LSIState
*s
)
865 DPRINTF("Wait Reselect\n");
866 if (s
->current_dma_len
)
867 BADF("Reselect with pending DMA\n");
868 for (i
= 0; i
< s
->active_commands
; i
++) {
869 if (s
->queue
[i
].pending
) {
870 lsi_reselect(s
, s
->queue
[i
].tag
);
874 if (s
->current_dma_len
== 0) {
879 static void lsi_execute_script(LSIState
*s
)
882 uint32_t addr
, addr_high
;
884 int insn_processed
= 0;
886 s
->istat1
|= LSI_ISTAT1_SRUN
;
889 insn
= read_dword(s
, s
->dsp
);
891 /* If we receive an empty opcode increment the DSP by 4 bytes
892 instead of 8 and execute the next opcode at that location */
896 addr
= read_dword(s
, s
->dsp
+ 4);
898 DPRINTF("SCRIPTS dsp=%08x opcode %08x arg %08x\n", s
->dsp
, insn
, addr
);
900 s
->dcmd
= insn
>> 24;
902 switch (insn
>> 30) {
903 case 0: /* Block move. */
904 if (s
->sist1
& LSI_SIST1_STO
) {
905 DPRINTF("Delayed select timeout\n");
909 s
->dbc
= insn
& 0xffffff;
913 if (insn
& (1 << 29)) {
914 /* Indirect addressing. */
915 addr
= read_dword(s
, addr
);
916 } else if (insn
& (1 << 28)) {
919 /* Table indirect addressing. */
921 /* 32-bit Table indirect */
922 offset
= sxt24(addr
);
923 cpu_physical_memory_read(s
->dsa
+ offset
, (uint8_t *)buf
, 8);
924 /* byte count is stored in bits 0:23 only */
925 s
->dbc
= cpu_to_le32(buf
[0]) & 0xffffff;
927 addr
= cpu_to_le32(buf
[1]);
929 /* 40-bit DMA, upper addr bits [39:32] stored in first DWORD of
930 * table, bits [31:24] */
931 if (lsi_dma_40bit(s
))
932 addr_high
= cpu_to_le32(buf
[0]) >> 24;
933 else if (lsi_dma_ti64bit(s
)) {
934 int selector
= (cpu_to_le32(buf
[0]) >> 24) & 0x1f;
937 /* offset index into scratch registers since
938 * TI64 mode can use registers C to R */
939 addr_high
= s
->scratch
[2 + selector
];
960 BADF("Illegal selector specified (0x%x > 0x15)"
961 " for 64-bit DMA block move", selector
);
965 } else if (lsi_dma_64bit(s
)) {
966 /* fetch a 3rd dword if 64-bit direct move is enabled and
967 only if we're not doing table indirect or indirect addressing */
968 s
->dbms
= read_dword(s
, s
->dsp
);
972 if ((s
->sstat1
& PHASE_MASK
) != ((insn
>> 24) & 7)) {
973 DPRINTF("Wrong phase got %d expected %d\n",
974 s
->sstat1
& PHASE_MASK
, (insn
>> 24) & 7);
975 lsi_script_scsi_interrupt(s
, LSI_SIST0_MA
, 0);
979 s
->dnad64
= addr_high
;
980 switch (s
->sstat1
& 0x7) {
1006 BADF("Unimplemented phase %d\n", s
->sstat1
& PHASE_MASK
);
1009 s
->dfifo
= s
->dbc
& 0xff;
1010 s
->ctest5
= (s
->ctest5
& 0xfc) | ((s
->dbc
>> 8) & 3);
1013 s
->ua
= addr
+ s
->dbc
;
1016 case 1: /* IO or Read/Write instruction. */
1017 opcode
= (insn
>> 27) & 7;
1021 if (insn
& (1 << 25)) {
1022 id
= read_dword(s
, s
->dsa
+ sxt24(insn
));
1026 id
= (id
>> 16) & 0xf;
1027 if (insn
& (1 << 26)) {
1028 addr
= s
->dsp
+ sxt24(addr
);
1032 case 0: /* Select */
1034 if (s
->current_dma_len
&& (s
->ssid
& 0xf) == id
) {
1035 DPRINTF("Already reselected by target %d\n", id
);
1038 s
->sstat0
|= LSI_SSTAT0_WOA
;
1039 s
->scntl1
&= ~LSI_SCNTL1_IARB
;
1040 if (id
>= LSI_MAX_DEVS
|| !s
->scsi_dev
[id
]) {
1041 DPRINTF("Selected absent target %d\n", id
);
1042 lsi_script_scsi_interrupt(s
, 0, LSI_SIST1_STO
);
1046 DPRINTF("Selected target %d%s\n",
1047 id
, insn
& (1 << 3) ? " ATN" : "");
1048 /* ??? Linux drivers compain when this is set. Maybe
1049 it only applies in low-level mode (unimplemented).
1050 lsi_script_scsi_interrupt(s, LSI_SIST0_CMP, 0); */
1051 s
->current_dev
= s
->scsi_dev
[id
];
1052 s
->current_tag
= id
<< 8;
1053 s
->scntl1
|= LSI_SCNTL1_CON
;
1054 if (insn
& (1 << 3)) {
1055 s
->socl
|= LSI_SOCL_ATN
;
1057 lsi_set_phase(s
, PHASE_MO
);
1059 case 1: /* Disconnect */
1060 DPRINTF("Wait Disconect\n");
1061 s
->scntl1
&= ~LSI_SCNTL1_CON
;
1063 case 2: /* Wait Reselect */
1064 lsi_wait_reselect(s
);
1067 DPRINTF("Set%s%s%s%s\n",
1068 insn
& (1 << 3) ? " ATN" : "",
1069 insn
& (1 << 6) ? " ACK" : "",
1070 insn
& (1 << 9) ? " TM" : "",
1071 insn
& (1 << 10) ? " CC" : "");
1072 if (insn
& (1 << 3)) {
1073 s
->socl
|= LSI_SOCL_ATN
;
1074 lsi_set_phase(s
, PHASE_MO
);
1076 if (insn
& (1 << 9)) {
1077 BADF("Target mode not implemented\n");
1080 if (insn
& (1 << 10))
1084 DPRINTF("Clear%s%s%s%s\n",
1085 insn
& (1 << 3) ? " ATN" : "",
1086 insn
& (1 << 6) ? " ACK" : "",
1087 insn
& (1 << 9) ? " TM" : "",
1088 insn
& (1 << 10) ? " CC" : "");
1089 if (insn
& (1 << 3)) {
1090 s
->socl
&= ~LSI_SOCL_ATN
;
1092 if (insn
& (1 << 10))
1103 static const char *opcode_names
[3] =
1104 {"Write", "Read", "Read-Modify-Write"};
1105 static const char *operator_names
[8] =
1106 {"MOV", "SHL", "OR", "XOR", "AND", "SHR", "ADD", "ADC"};
1109 reg
= ((insn
>> 16) & 0x7f) | (insn
& 0x80);
1110 data8
= (insn
>> 8) & 0xff;
1111 opcode
= (insn
>> 27) & 7;
1112 operator = (insn
>> 24) & 7;
1113 DPRINTF("%s reg 0x%x %s data8=0x%02x sfbr=0x%02x%s\n",
1114 opcode_names
[opcode
- 5], reg
,
1115 operator_names
[operator], data8
, s
->sfbr
,
1116 (insn
& (1 << 23)) ? " SFBR" : "");
1119 case 5: /* From SFBR */
1123 case 6: /* To SFBR */
1125 op0
= lsi_reg_readb(s
, reg
);
1128 case 7: /* Read-modify-write */
1130 op0
= lsi_reg_readb(s
, reg
);
1131 if (insn
& (1 << 23)) {
1143 case 1: /* Shift left */
1145 op0
= (op0
<< 1) | s
->carry
;
1159 op0
= (op0
>> 1) | (s
->carry
<< 7);
1164 s
->carry
= op0
< op1
;
1167 op0
+= op1
+ s
->carry
;
1169 s
->carry
= op0
<= op1
;
1171 s
->carry
= op0
< op1
;
1176 case 5: /* From SFBR */
1177 case 7: /* Read-modify-write */
1178 lsi_reg_writeb(s
, reg
, op0
);
1180 case 6: /* To SFBR */
1187 case 2: /* Transfer Control. */
1192 if ((insn
& 0x002e0000) == 0) {
1196 if (s
->sist1
& LSI_SIST1_STO
) {
1197 DPRINTF("Delayed select timeout\n");
1201 cond
= jmp
= (insn
& (1 << 19)) != 0;
1202 if (cond
== jmp
&& (insn
& (1 << 21))) {
1203 DPRINTF("Compare carry %d\n", s
->carry
== jmp
);
1204 cond
= s
->carry
!= 0;
1206 if (cond
== jmp
&& (insn
& (1 << 17))) {
1207 DPRINTF("Compare phase %d %c= %d\n",
1208 (s
->sstat1
& PHASE_MASK
),
1210 ((insn
>> 24) & 7));
1211 cond
= (s
->sstat1
& PHASE_MASK
) == ((insn
>> 24) & 7);
1213 if (cond
== jmp
&& (insn
& (1 << 18))) {
1216 mask
= (~insn
>> 8) & 0xff;
1217 DPRINTF("Compare data 0x%x & 0x%x %c= 0x%x\n",
1218 s
->sfbr
, mask
, jmp
? '=' : '!', insn
& mask
);
1219 cond
= (s
->sfbr
& mask
) == (insn
& mask
);
1222 if (insn
& (1 << 23)) {
1223 /* Relative address. */
1224 addr
= s
->dsp
+ sxt24(addr
);
1226 switch ((insn
>> 27) & 7) {
1228 DPRINTF("Jump to 0x%08x\n", addr
);
1232 DPRINTF("Call 0x%08x\n", addr
);
1236 case 2: /* Return */
1237 DPRINTF("Return to 0x%08x\n", s
->temp
);
1240 case 3: /* Interrupt */
1241 DPRINTF("Interrupt 0x%08x\n", s
->dsps
);
1242 if ((insn
& (1 << 20)) != 0) {
1243 s
->istat0
|= LSI_ISTAT0_INTF
;
1246 lsi_script_dma_interrupt(s
, LSI_DSTAT_SIR
);
1250 DPRINTF("Illegal transfer control\n");
1251 lsi_script_dma_interrupt(s
, LSI_DSTAT_IID
);
1255 DPRINTF("Control condition failed\n");
1261 if ((insn
& (1 << 29)) == 0) {
1264 /* ??? The docs imply the destination address is loaded into
1265 the TEMP register. However the Linux drivers rely on
1266 the value being presrved. */
1267 dest
= read_dword(s
, s
->dsp
);
1269 lsi_memcpy(s
, dest
, addr
, insn
& 0xffffff);
1276 if (insn
& (1 << 28)) {
1277 addr
= s
->dsa
+ sxt24(addr
);
1280 reg
= (insn
>> 16) & 0xff;
1281 if (insn
& (1 << 24)) {
1282 cpu_physical_memory_read(addr
, data
, n
);
1283 DPRINTF("Load reg 0x%x size %d addr 0x%08x = %08x\n", reg
, n
,
1284 addr
, *(int *)data
);
1285 for (i
= 0; i
< n
; i
++) {
1286 lsi_reg_writeb(s
, reg
+ i
, data
[i
]);
1289 DPRINTF("Store reg 0x%x size %d addr 0x%08x\n", reg
, n
, addr
);
1290 for (i
= 0; i
< n
; i
++) {
1291 data
[i
] = lsi_reg_readb(s
, reg
+ i
);
1293 cpu_physical_memory_write(addr
, data
, n
);
1297 if (insn_processed
> 10000 && !s
->waiting
) {
1298 /* Some windows drivers make the device spin waiting for a memory
1299 location to change. If we have been executed a lot of code then
1300 assume this is the case and force an unexpected device disconnect.
1301 This is apparently sufficient to beat the drivers into submission.
1303 if (!(s
->sien0
& LSI_SIST0_UDC
))
1304 fprintf(stderr
, "inf. loop with UDC masked\n");
1305 lsi_script_scsi_interrupt(s
, LSI_SIST0_UDC
, 0);
1307 } else if (s
->istat1
& LSI_ISTAT1_SRUN
&& !s
->waiting
) {
1308 if (s
->dcntl
& LSI_DCNTL_SSM
) {
1309 lsi_script_dma_interrupt(s
, LSI_DSTAT_SSI
);
1314 DPRINTF("SCRIPTS execution stopped\n");
1317 static uint8_t lsi_reg_readb(LSIState
*s
, int offset
)
1320 #define CASE_GET_REG24(name, addr) \
1321 case addr: return s->name & 0xff; \
1322 case addr + 1: return (s->name >> 8) & 0xff; \
1323 case addr + 2: return (s->name >> 16) & 0xff;
1325 #define CASE_GET_REG32(name, addr) \
1326 case addr: return s->name & 0xff; \
1327 case addr + 1: return (s->name >> 8) & 0xff; \
1328 case addr + 2: return (s->name >> 16) & 0xff; \
1329 case addr + 3: return (s->name >> 24) & 0xff;
1331 #ifdef DEBUG_LSI_REG
1332 DPRINTF("Read reg %x\n", offset
);
1335 case 0x00: /* SCNTL0 */
1337 case 0x01: /* SCNTL1 */
1339 case 0x02: /* SCNTL2 */
1341 case 0x03: /* SCNTL3 */
1343 case 0x04: /* SCID */
1345 case 0x05: /* SXFER */
1347 case 0x06: /* SDID */
1349 case 0x07: /* GPREG0 */
1351 case 0x08: /* Revision ID */
1353 case 0xa: /* SSID */
1355 case 0xb: /* SBCL */
1356 /* ??? This is not correct. However it's (hopefully) only
1357 used for diagnostics, so should be ok. */
1359 case 0xc: /* DSTAT */
1360 tmp
= s
->dstat
| 0x80;
1361 if ((s
->istat0
& LSI_ISTAT0_INTF
) == 0)
1365 case 0x0d: /* SSTAT0 */
1367 case 0x0e: /* SSTAT1 */
1369 case 0x0f: /* SSTAT2 */
1370 return s
->scntl1
& LSI_SCNTL1_CON
? 0 : 2;
1371 CASE_GET_REG32(dsa
, 0x10)
1372 case 0x14: /* ISTAT0 */
1374 case 0x15: /* ISTAT1 */
1376 case 0x16: /* MBOX0 */
1378 case 0x17: /* MBOX1 */
1380 case 0x18: /* CTEST0 */
1382 case 0x19: /* CTEST1 */
1384 case 0x1a: /* CTEST2 */
1385 tmp
= s
->ctest2
| LSI_CTEST2_DACK
| LSI_CTEST2_CM
;
1386 if (s
->istat0
& LSI_ISTAT0_SIGP
) {
1387 s
->istat0
&= ~LSI_ISTAT0_SIGP
;
1388 tmp
|= LSI_CTEST2_SIGP
;
1391 case 0x1b: /* CTEST3 */
1393 CASE_GET_REG32(temp
, 0x1c)
1394 case 0x20: /* DFIFO */
1396 case 0x21: /* CTEST4 */
1398 case 0x22: /* CTEST5 */
1400 case 0x23: /* CTEST6 */
1402 CASE_GET_REG24(dbc
, 0x24)
1403 case 0x27: /* DCMD */
1405 CASE_GET_REG32(dnad
, 0x28)
1406 CASE_GET_REG32(dsp
, 0x2c)
1407 CASE_GET_REG32(dsps
, 0x30)
1408 CASE_GET_REG32(scratch
[0], 0x34)
1409 case 0x38: /* DMODE */
1411 case 0x39: /* DIEN */
1413 case 0x3a: /* SBR */
1415 case 0x3b: /* DCNTL */
1417 case 0x40: /* SIEN0 */
1419 case 0x41: /* SIEN1 */
1421 case 0x42: /* SIST0 */
1426 case 0x43: /* SIST1 */
1431 case 0x46: /* MACNTL */
1433 case 0x47: /* GPCNTL0 */
1435 case 0x48: /* STIME0 */
1437 case 0x4a: /* RESPID0 */
1439 case 0x4b: /* RESPID1 */
1441 case 0x4d: /* STEST1 */
1443 case 0x4e: /* STEST2 */
1445 case 0x4f: /* STEST3 */
1447 case 0x50: /* SIDL */
1448 /* This is needed by the linux drivers. We currently only update it
1449 during the MSG IN phase. */
1451 case 0x52: /* STEST4 */
1453 case 0x56: /* CCNTL0 */
1455 case 0x57: /* CCNTL1 */
1457 case 0x58: /* SBDL */
1458 /* Some drivers peek at the data bus during the MSG IN phase. */
1459 if ((s
->sstat1
& PHASE_MASK
) == PHASE_MI
)
1462 case 0x59: /* SBDL high */
1464 CASE_GET_REG32(mmrs
, 0xa0)
1465 CASE_GET_REG32(mmws
, 0xa4)
1466 CASE_GET_REG32(sfs
, 0xa8)
1467 CASE_GET_REG32(drs
, 0xac)
1468 CASE_GET_REG32(sbms
, 0xb0)
1469 CASE_GET_REG32(dbms
, 0xb4)
1470 CASE_GET_REG32(dnad64
, 0xb8)
1471 CASE_GET_REG32(pmjad1
, 0xc0)
1472 CASE_GET_REG32(pmjad2
, 0xc4)
1473 CASE_GET_REG32(rbc
, 0xc8)
1474 CASE_GET_REG32(ua
, 0xcc)
1475 CASE_GET_REG32(ia
, 0xd4)
1476 CASE_GET_REG32(sbc
, 0xd8)
1477 CASE_GET_REG32(csbc
, 0xdc)
1479 if (offset
>= 0x5c && offset
< 0xa0) {
1482 n
= (offset
- 0x58) >> 2;
1483 shift
= (offset
& 3) * 8;
1484 return (s
->scratch
[n
] >> shift
) & 0xff;
1486 BADF("readb 0x%x\n", offset
);
1488 #undef CASE_GET_REG24
1489 #undef CASE_GET_REG32
1492 static void lsi_reg_writeb(LSIState
*s
, int offset
, uint8_t val
)
1494 #define CASE_SET_REG24(name, addr) \
1495 case addr : s->name &= 0xffffff00; s->name |= val; break; \
1496 case addr + 1: s->name &= 0xffff00ff; s->name |= val << 8; break; \
1497 case addr + 2: s->name &= 0xff00ffff; s->name |= val << 16; break;
1499 #define CASE_SET_REG32(name, addr) \
1500 case addr : s->name &= 0xffffff00; s->name |= val; break; \
1501 case addr + 1: s->name &= 0xffff00ff; s->name |= val << 8; break; \
1502 case addr + 2: s->name &= 0xff00ffff; s->name |= val << 16; break; \
1503 case addr + 3: s->name &= 0x00ffffff; s->name |= val << 24; break;
1505 #ifdef DEBUG_LSI_REG
1506 DPRINTF("Write reg %x = %02x\n", offset
, val
);
1509 case 0x00: /* SCNTL0 */
1511 if (val
& LSI_SCNTL0_START
) {
1512 BADF("Start sequence not implemented\n");
1515 case 0x01: /* SCNTL1 */
1516 s
->scntl1
= val
& ~LSI_SCNTL1_SST
;
1517 if (val
& LSI_SCNTL1_IARB
) {
1518 BADF("Immediate Arbritration not implemented\n");
1520 if (val
& LSI_SCNTL1_RST
) {
1521 s
->sstat0
|= LSI_SSTAT0_RST
;
1522 lsi_script_scsi_interrupt(s
, LSI_SIST0_RST
, 0);
1524 s
->sstat0
&= ~LSI_SSTAT0_RST
;
1527 case 0x02: /* SCNTL2 */
1528 val
&= ~(LSI_SCNTL2_WSR
| LSI_SCNTL2_WSS
);
1531 case 0x03: /* SCNTL3 */
1534 case 0x04: /* SCID */
1537 case 0x05: /* SXFER */
1540 case 0x06: /* SDID */
1541 if ((val
& 0xf) != (s
->ssid
& 0xf))
1542 BADF("Destination ID does not match SSID\n");
1543 s
->sdid
= val
& 0xf;
1545 case 0x07: /* GPREG0 */
1547 case 0x08: /* SFBR */
1548 /* The CPU is not allowed to write to this register. However the
1549 SCRIPTS register move instructions are. */
1552 case 0x0a: case 0x0b:
1553 /* Openserver writes to these readonly registers on startup */
1555 case 0x0c: case 0x0d: case 0x0e: case 0x0f:
1556 /* Linux writes to these readonly registers on startup. */
1558 CASE_SET_REG32(dsa
, 0x10)
1559 case 0x14: /* ISTAT0 */
1560 s
->istat0
= (s
->istat0
& 0x0f) | (val
& 0xf0);
1561 if (val
& LSI_ISTAT0_ABRT
) {
1562 lsi_script_dma_interrupt(s
, LSI_DSTAT_ABRT
);
1564 if (val
& LSI_ISTAT0_INTF
) {
1565 s
->istat0
&= ~LSI_ISTAT0_INTF
;
1568 if (s
->waiting
== 1 && val
& LSI_ISTAT0_SIGP
) {
1569 DPRINTF("Woken by SIGP\n");
1572 lsi_execute_script(s
);
1574 if (val
& LSI_ISTAT0_SRST
) {
1578 case 0x16: /* MBOX0 */
1581 case 0x17: /* MBOX1 */
1584 case 0x1a: /* CTEST2 */
1585 s
->ctest2
= val
& LSI_CTEST2_PCICIE
;
1587 case 0x1b: /* CTEST3 */
1588 s
->ctest3
= val
& 0x0f;
1590 CASE_SET_REG32(temp
, 0x1c)
1591 case 0x21: /* CTEST4 */
1593 BADF("Unimplemented CTEST4-FBL 0x%x\n", val
);
1597 case 0x22: /* CTEST5 */
1598 if (val
& (LSI_CTEST5_ADCK
| LSI_CTEST5_BBCK
)) {
1599 BADF("CTEST5 DMA increment not implemented\n");
1603 CASE_SET_REG24(dbc
, 0x24)
1604 CASE_SET_REG32(dnad
, 0x28)
1605 case 0x2c: /* DSP[0:7] */
1606 s
->dsp
&= 0xffffff00;
1609 case 0x2d: /* DSP[8:15] */
1610 s
->dsp
&= 0xffff00ff;
1613 case 0x2e: /* DSP[16:23] */
1614 s
->dsp
&= 0xff00ffff;
1615 s
->dsp
|= val
<< 16;
1617 case 0x2f: /* DSP[24:31] */
1618 s
->dsp
&= 0x00ffffff;
1619 s
->dsp
|= val
<< 24;
1620 if ((s
->dmode
& LSI_DMODE_MAN
) == 0
1621 && (s
->istat1
& LSI_ISTAT1_SRUN
) == 0)
1622 lsi_execute_script(s
);
1624 CASE_SET_REG32(dsps
, 0x30)
1625 CASE_SET_REG32(scratch
[0], 0x34)
1626 case 0x38: /* DMODE */
1627 if (val
& (LSI_DMODE_SIOM
| LSI_DMODE_DIOM
)) {
1628 BADF("IO mappings not implemented\n");
1632 case 0x39: /* DIEN */
1636 case 0x3a: /* SBR */
1639 case 0x3b: /* DCNTL */
1640 s
->dcntl
= val
& ~(LSI_DCNTL_PFF
| LSI_DCNTL_STD
);
1641 if ((val
& LSI_DCNTL_STD
) && (s
->istat1
& LSI_ISTAT1_SRUN
) == 0)
1642 lsi_execute_script(s
);
1644 case 0x40: /* SIEN0 */
1648 case 0x41: /* SIEN1 */
1652 case 0x47: /* GPCNTL0 */
1654 case 0x48: /* STIME0 */
1657 case 0x49: /* STIME1 */
1659 DPRINTF("General purpose timer not implemented\n");
1660 /* ??? Raising the interrupt immediately seems to be sufficient
1661 to keep the FreeBSD driver happy. */
1662 lsi_script_scsi_interrupt(s
, 0, LSI_SIST1_GEN
);
1665 case 0x4a: /* RESPID0 */
1668 case 0x4b: /* RESPID1 */
1671 case 0x4d: /* STEST1 */
1674 case 0x4e: /* STEST2 */
1676 BADF("Low level mode not implemented\n");
1680 case 0x4f: /* STEST3 */
1682 BADF("SCSI FIFO test mode not implemented\n");
1686 case 0x56: /* CCNTL0 */
1689 case 0x57: /* CCNTL1 */
1692 CASE_SET_REG32(mmrs
, 0xa0)
1693 CASE_SET_REG32(mmws
, 0xa4)
1694 CASE_SET_REG32(sfs
, 0xa8)
1695 CASE_SET_REG32(drs
, 0xac)
1696 CASE_SET_REG32(sbms
, 0xb0)
1697 CASE_SET_REG32(dbms
, 0xb4)
1698 CASE_SET_REG32(dnad64
, 0xb8)
1699 CASE_SET_REG32(pmjad1
, 0xc0)
1700 CASE_SET_REG32(pmjad2
, 0xc4)
1701 CASE_SET_REG32(rbc
, 0xc8)
1702 CASE_SET_REG32(ua
, 0xcc)
1703 CASE_SET_REG32(ia
, 0xd4)
1704 CASE_SET_REG32(sbc
, 0xd8)
1705 CASE_SET_REG32(csbc
, 0xdc)
1707 if (offset
>= 0x5c && offset
< 0xa0) {
1710 n
= (offset
- 0x58) >> 2;
1711 shift
= (offset
& 3) * 8;
1712 s
->scratch
[n
] &= ~(0xff << shift
);
1713 s
->scratch
[n
] |= (val
& 0xff) << shift
;
1715 BADF("Unhandled writeb 0x%x = 0x%x\n", offset
, val
);
1718 #undef CASE_SET_REG24
1719 #undef CASE_SET_REG32
1722 static void lsi_mmio_writeb(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1724 LSIState
*s
= (LSIState
*)opaque
;
1726 lsi_reg_writeb(s
, addr
& 0xff, val
);
1729 static void lsi_mmio_writew(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1731 LSIState
*s
= (LSIState
*)opaque
;
1734 lsi_reg_writeb(s
, addr
, val
& 0xff);
1735 lsi_reg_writeb(s
, addr
+ 1, (val
>> 8) & 0xff);
1738 static void lsi_mmio_writel(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1740 LSIState
*s
= (LSIState
*)opaque
;
1743 lsi_reg_writeb(s
, addr
, val
& 0xff);
1744 lsi_reg_writeb(s
, addr
+ 1, (val
>> 8) & 0xff);
1745 lsi_reg_writeb(s
, addr
+ 2, (val
>> 16) & 0xff);
1746 lsi_reg_writeb(s
, addr
+ 3, (val
>> 24) & 0xff);
1749 static uint32_t lsi_mmio_readb(void *opaque
, target_phys_addr_t addr
)
1751 LSIState
*s
= (LSIState
*)opaque
;
1753 return lsi_reg_readb(s
, addr
& 0xff);
1756 static uint32_t lsi_mmio_readw(void *opaque
, target_phys_addr_t addr
)
1758 LSIState
*s
= (LSIState
*)opaque
;
1762 val
= lsi_reg_readb(s
, addr
);
1763 val
|= lsi_reg_readb(s
, addr
+ 1) << 8;
1767 static uint32_t lsi_mmio_readl(void *opaque
, target_phys_addr_t addr
)
1769 LSIState
*s
= (LSIState
*)opaque
;
1772 val
= lsi_reg_readb(s
, addr
);
1773 val
|= lsi_reg_readb(s
, addr
+ 1) << 8;
1774 val
|= lsi_reg_readb(s
, addr
+ 2) << 16;
1775 val
|= lsi_reg_readb(s
, addr
+ 3) << 24;
1779 static CPUReadMemoryFunc
*lsi_mmio_readfn
[3] = {
1785 static CPUWriteMemoryFunc
*lsi_mmio_writefn
[3] = {
1791 static void lsi_ram_writeb(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1793 LSIState
*s
= (LSIState
*)opaque
;
1798 newval
= s
->script_ram
[addr
>> 2];
1799 shift
= (addr
& 3) * 8;
1800 newval
&= ~(0xff << shift
);
1801 newval
|= val
<< shift
;
1802 s
->script_ram
[addr
>> 2] = newval
;
1805 static void lsi_ram_writew(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1807 LSIState
*s
= (LSIState
*)opaque
;
1811 newval
= s
->script_ram
[addr
>> 2];
1813 newval
= (newval
& 0xffff) | (val
<< 16);
1815 newval
= (newval
& 0xffff0000) | val
;
1817 s
->script_ram
[addr
>> 2] = newval
;
1821 static void lsi_ram_writel(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1823 LSIState
*s
= (LSIState
*)opaque
;
1826 s
->script_ram
[addr
>> 2] = val
;
1829 static uint32_t lsi_ram_readb(void *opaque
, target_phys_addr_t addr
)
1831 LSIState
*s
= (LSIState
*)opaque
;
1835 val
= s
->script_ram
[addr
>> 2];
1836 val
>>= (addr
& 3) * 8;
1840 static uint32_t lsi_ram_readw(void *opaque
, target_phys_addr_t addr
)
1842 LSIState
*s
= (LSIState
*)opaque
;
1846 val
= s
->script_ram
[addr
>> 2];
1849 return le16_to_cpu(val
);
1852 static uint32_t lsi_ram_readl(void *opaque
, target_phys_addr_t addr
)
1854 LSIState
*s
= (LSIState
*)opaque
;
1857 return le32_to_cpu(s
->script_ram
[addr
>> 2]);
1860 static CPUReadMemoryFunc
*lsi_ram_readfn
[3] = {
1866 static CPUWriteMemoryFunc
*lsi_ram_writefn
[3] = {
1872 static uint32_t lsi_io_readb(void *opaque
, uint32_t addr
)
1874 LSIState
*s
= (LSIState
*)opaque
;
1875 return lsi_reg_readb(s
, addr
& 0xff);
1878 static uint32_t lsi_io_readw(void *opaque
, uint32_t addr
)
1880 LSIState
*s
= (LSIState
*)opaque
;
1883 val
= lsi_reg_readb(s
, addr
);
1884 val
|= lsi_reg_readb(s
, addr
+ 1) << 8;
1888 static uint32_t lsi_io_readl(void *opaque
, uint32_t addr
)
1890 LSIState
*s
= (LSIState
*)opaque
;
1893 val
= lsi_reg_readb(s
, addr
);
1894 val
|= lsi_reg_readb(s
, addr
+ 1) << 8;
1895 val
|= lsi_reg_readb(s
, addr
+ 2) << 16;
1896 val
|= lsi_reg_readb(s
, addr
+ 3) << 24;
1900 static void lsi_io_writeb(void *opaque
, uint32_t addr
, uint32_t val
)
1902 LSIState
*s
= (LSIState
*)opaque
;
1903 lsi_reg_writeb(s
, addr
& 0xff, val
);
1906 static void lsi_io_writew(void *opaque
, uint32_t addr
, uint32_t val
)
1908 LSIState
*s
= (LSIState
*)opaque
;
1910 lsi_reg_writeb(s
, addr
, val
& 0xff);
1911 lsi_reg_writeb(s
, addr
+ 1, (val
>> 8) & 0xff);
1914 static void lsi_io_writel(void *opaque
, uint32_t addr
, uint32_t val
)
1916 LSIState
*s
= (LSIState
*)opaque
;
1918 lsi_reg_writeb(s
, addr
, val
& 0xff);
1919 lsi_reg_writeb(s
, addr
+ 1, (val
>> 8) & 0xff);
1920 lsi_reg_writeb(s
, addr
+ 2, (val
>> 16) & 0xff);
1921 lsi_reg_writeb(s
, addr
+ 3, (val
>> 24) & 0xff);
1924 static void lsi_io_mapfunc(PCIDevice
*pci_dev
, int region_num
,
1925 uint32_t addr
, uint32_t size
, int type
)
1927 LSIState
*s
= (LSIState
*)pci_dev
;
1929 DPRINTF("Mapping IO at %08x\n", addr
);
1931 register_ioport_write(addr
, 256, 1, lsi_io_writeb
, s
);
1932 register_ioport_read(addr
, 256, 1, lsi_io_readb
, s
);
1933 register_ioport_write(addr
, 256, 2, lsi_io_writew
, s
);
1934 register_ioport_read(addr
, 256, 2, lsi_io_readw
, s
);
1935 register_ioport_write(addr
, 256, 4, lsi_io_writel
, s
);
1936 register_ioport_read(addr
, 256, 4, lsi_io_readl
, s
);
1939 static void lsi_ram_mapfunc(PCIDevice
*pci_dev
, int region_num
,
1940 uint32_t addr
, uint32_t size
, int type
)
1942 LSIState
*s
= (LSIState
*)pci_dev
;
1944 DPRINTF("Mapping ram at %08x\n", addr
);
1945 s
->script_ram_base
= addr
;
1946 cpu_register_physical_memory(addr
+ 0, 0x2000, s
->ram_io_addr
);
1949 static void lsi_mmio_mapfunc(PCIDevice
*pci_dev
, int region_num
,
1950 uint32_t addr
, uint32_t size
, int type
)
1952 LSIState
*s
= (LSIState
*)pci_dev
;
1954 DPRINTF("Mapping registers at %08x\n", addr
);
1955 cpu_register_physical_memory(addr
+ 0, 0x400, s
->mmio_io_addr
);
1958 void lsi_scsi_attach(void *opaque
, BlockDriverState
*bd
, int id
)
1960 LSIState
*s
= (LSIState
*)opaque
;
1963 for (id
= 0; id
< LSI_MAX_DEVS
; id
++) {
1964 if (s
->scsi_dev
[id
] == NULL
)
1968 if (id
>= LSI_MAX_DEVS
) {
1969 BADF("Bad Device ID %d\n", id
);
1972 if (s
->scsi_dev
[id
]) {
1973 DPRINTF("Destroying device %d\n", id
);
1974 s
->scsi_dev
[id
]->destroy(s
->scsi_dev
[id
]);
1976 DPRINTF("Attaching block device %d\n", id
);
1977 s
->scsi_dev
[id
] = scsi_generic_init(bd
, 1, lsi_command_complete
, s
);
1978 if (s
->scsi_dev
[id
] == NULL
)
1979 s
->scsi_dev
[id
] = scsi_disk_init(bd
, 1, lsi_command_complete
, s
);
1980 bd
->private = &s
->pci_dev
;
1983 static int lsi_scsi_uninit(PCIDevice
*d
)
1985 LSIState
*s
= (LSIState
*) d
;
1987 cpu_unregister_io_memory(s
->mmio_io_addr
);
1988 cpu_unregister_io_memory(s
->ram_io_addr
);
1990 qemu_free(s
->queue
);
1995 void *lsi_scsi_init(PCIBus
*bus
, int devfn
)
2000 s
= (LSIState
*)pci_register_device(bus
, "LSI53C895A SCSI HBA",
2001 sizeof(*s
), devfn
, NULL
, NULL
);
2003 fprintf(stderr
, "lsi-scsi: Failed to register PCI device\n");
2007 pci_conf
= s
->pci_dev
.config
;
2009 /* PCI Vendor ID (word) */
2010 pci_config_set_vendor_id(pci_conf
, PCI_VENDOR_ID_LSI_LOGIC
);
2011 /* PCI device ID (word) */
2012 pci_config_set_device_id(pci_conf
, PCI_DEVICE_ID_LSI_53C895A
);
2013 /* PCI base class code */
2014 pci_config_set_class(pci_conf
, PCI_CLASS_STORAGE_SCSI
);
2015 /* PCI subsystem ID */
2016 pci_conf
[0x2e] = 0x00;
2017 pci_conf
[0x2f] = 0x10;
2018 /* PCI latency timer = 255 */
2019 pci_conf
[0x0d] = 0xff;
2020 /* Interrupt pin 1 */
2021 pci_conf
[0x3d] = 0x01;
2023 s
->mmio_io_addr
= cpu_register_io_memory(0, lsi_mmio_readfn
,
2024 lsi_mmio_writefn
, s
);
2025 s
->ram_io_addr
= cpu_register_io_memory(0, lsi_ram_readfn
,
2026 lsi_ram_writefn
, s
);
2028 pci_register_io_region((struct PCIDevice
*)s
, 0, 256,
2029 PCI_ADDRESS_SPACE_IO
, lsi_io_mapfunc
);
2030 pci_register_io_region((struct PCIDevice
*)s
, 1, 0x400,
2031 PCI_ADDRESS_SPACE_MEM
, lsi_mmio_mapfunc
);
2032 pci_register_io_region((struct PCIDevice
*)s
, 2, 0x2000,
2033 PCI_ADDRESS_SPACE_MEM
, lsi_ram_mapfunc
);
2034 s
->queue
= qemu_malloc(sizeof(lsi_queue
));
2036 s
->active_commands
= 0;
2037 s
->pci_dev
.unregister
= lsi_scsi_uninit
;