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
155 /* Maximum length of MSG IN data. */
156 #define LSI_MAX_MSGIN_LEN 8
158 /* Flag set if this is a tagged command. */
159 #define LSI_TAG_VALID (1 << 16)
171 uint32_t script_ram_base
;
173 int carry
; /* ??? Should this be an a visible register somewhere? */
175 /* Action to take at the end of a MSG IN phase.
176 0 = COMMAND, 1 = disconect, 2 = DATA OUT, 3 = DATA IN. */
179 uint8_t msg
[LSI_MAX_MSGIN_LEN
];
180 /* 0 if SCRIPTS are running or stopped.
181 * 1 if a Wait Reselect instruction has been issued.
182 * 2 if processing DMA from lsi_execute_script.
183 * 3 if a DMA operation is in progress. */
185 SCSIDevice
*scsi_dev
[LSI_MAX_DEVS
];
186 SCSIDevice
*current_dev
;
188 /* The tag is a combination of the device ID and the SCSI tag. */
189 uint32_t current_tag
;
190 uint32_t current_dma_len
;
191 int command_complete
;
255 uint32_t scratch
[18]; /* SCRATCHA-SCRATCHR */
257 /* Script ram is stored as 32-bit words in host byteorder. */
258 uint32_t script_ram
[2048];
261 static void lsi_soft_reset(LSIState
*s
)
271 memset(s
->scratch
, 0, sizeof(s
->scratch
));
325 static uint8_t lsi_reg_readb(LSIState
*s
, int offset
);
326 static void lsi_reg_writeb(LSIState
*s
, int offset
, uint8_t val
);
327 static void lsi_execute_script(LSIState
*s
);
329 static inline uint32_t read_dword(LSIState
*s
, uint32_t addr
)
333 /* Optimize reading from SCRIPTS RAM. */
334 if ((addr
& 0xffffe000) == s
->script_ram_base
) {
335 return s
->script_ram
[(addr
& 0x1fff) >> 2];
337 cpu_physical_memory_read(addr
, (uint8_t *)&buf
, 4);
338 return cpu_to_le32(buf
);
341 static void lsi_stop_script(LSIState
*s
)
343 s
->istat1
&= ~LSI_ISTAT1_SRUN
;
346 static void lsi_update_irq(LSIState
*s
)
349 static int last_level
;
351 /* It's unclear whether the DIP/SIP bits should be cleared when the
352 Interrupt Status Registers are cleared or when istat0 is read.
353 We currently do the formwer, which seems to work. */
356 if (s
->dstat
& s
->dien
)
358 s
->istat0
|= LSI_ISTAT0_DIP
;
360 s
->istat0
&= ~LSI_ISTAT0_DIP
;
363 if (s
->sist0
|| s
->sist1
) {
364 if ((s
->sist0
& s
->sien0
) || (s
->sist1
& s
->sien1
))
366 s
->istat0
|= LSI_ISTAT0_SIP
;
368 s
->istat0
&= ~LSI_ISTAT0_SIP
;
370 if (s
->istat0
& LSI_ISTAT0_INTF
)
373 if (level
!= last_level
) {
374 DPRINTF("Update IRQ level %d dstat %02x sist %02x%02x\n",
375 level
, s
->dstat
, s
->sist1
, s
->sist0
);
378 qemu_set_irq(s
->pci_dev
.irq
[0], level
);
381 /* Stop SCRIPTS execution and raise a SCSI interrupt. */
382 static void lsi_script_scsi_interrupt(LSIState
*s
, int stat0
, int stat1
)
387 DPRINTF("SCSI Interrupt 0x%02x%02x prev 0x%02x%02x\n",
388 stat1
, stat0
, s
->sist1
, s
->sist0
);
391 /* Stop processor on fatal or unmasked interrupt. As a special hack
392 we don't stop processing when raising STO. Instead continue
393 execution and stop at the next insn that accesses the SCSI bus. */
394 mask0
= s
->sien0
| ~(LSI_SIST0_CMP
| LSI_SIST0_SEL
| LSI_SIST0_RSL
);
395 mask1
= s
->sien1
| ~(LSI_SIST1_GEN
| LSI_SIST1_HTH
);
396 mask1
&= ~LSI_SIST1_STO
;
397 if (s
->sist0
& mask0
|| s
->sist1
& mask1
) {
403 /* Stop SCRIPTS execution and raise a DMA interrupt. */
404 static void lsi_script_dma_interrupt(LSIState
*s
, int stat
)
406 DPRINTF("DMA Interrupt 0x%x prev 0x%x\n", stat
, s
->dstat
);
412 static inline void lsi_set_phase(LSIState
*s
, int phase
)
414 s
->sstat1
= (s
->sstat1
& ~PHASE_MASK
) | phase
;
417 static void lsi_bad_phase(LSIState
*s
, int out
, int new_phase
)
419 /* Trigger a phase mismatch. */
420 if (s
->ccntl0
& LSI_CCNTL0_ENPMJ
) {
421 if ((s
->ccntl0
& LSI_CCNTL0_PMJCTL
) || out
) {
426 DPRINTF("Data phase mismatch jump to %08x\n", s
->dsp
);
428 DPRINTF("Phase mismatch interrupt\n");
429 lsi_script_scsi_interrupt(s
, LSI_SIST0_MA
, 0);
432 lsi_set_phase(s
, new_phase
);
436 /* Resume SCRIPTS execution after a DMA operation. */
437 static void lsi_resume_script(LSIState
*s
)
439 if (s
->waiting
!= 2) {
441 lsi_execute_script(s
);
447 /* Initiate a SCSI layer data transfer. */
448 static void lsi_do_dma(LSIState
*s
, int out
)
453 if (!s
->current_dma_len
) {
454 /* Wait until data is available. */
455 DPRINTF("DMA no data available\n");
460 if (count
> s
->current_dma_len
)
461 count
= s
->current_dma_len
;
462 DPRINTF("DMA addr=0x%08x len=%d\n", s
->dnad
, count
);
469 if (s
->dma_buf
== NULL
) {
470 s
->dma_buf
= s
->current_dev
->get_buf(s
->current_dev
,
474 /* ??? Set SFBR to first data byte. */
476 cpu_physical_memory_read(addr
, s
->dma_buf
, count
);
478 cpu_physical_memory_write(addr
, s
->dma_buf
, count
);
480 s
->current_dma_len
-= count
;
481 if (s
->current_dma_len
== 0) {
484 /* Write the data. */
485 s
->current_dev
->write_data(s
->current_dev
, s
->current_tag
);
487 /* Request any remaining data. */
488 s
->current_dev
->read_data(s
->current_dev
, s
->current_tag
);
492 lsi_resume_script(s
);
497 /* Add a command to the queue. */
498 static void lsi_queue_command(LSIState
*s
)
502 DPRINTF("Queueing tag=0x%x\n", s
->current_tag
);
503 if (s
->queue_len
== s
->active_commands
) {
505 s
->queue
= qemu_realloc(s
->queue
, s
->queue_len
* sizeof(lsi_queue
));
507 p
= &s
->queue
[s
->active_commands
++];
508 p
->tag
= s
->current_tag
;
510 p
->out
= (s
->sstat1
& PHASE_MASK
) == PHASE_DO
;
513 /* Queue a byte for a MSG IN phase. */
514 static void lsi_add_msg_byte(LSIState
*s
, uint8_t data
)
516 if (s
->msg_len
>= LSI_MAX_MSGIN_LEN
) {
517 BADF("MSG IN data too long\n");
519 DPRINTF("MSG IN 0x%02x\n", data
);
520 s
->msg
[s
->msg_len
++] = data
;
524 /* Perform reselection to continue a command. */
525 static void lsi_reselect(LSIState
*s
, uint32_t tag
)
532 for (n
= 0; n
< s
->active_commands
; n
++) {
537 if (n
== s
->active_commands
) {
538 BADF("Reselected non-existant command tag=0x%x\n", tag
);
541 id
= (tag
>> 8) & 0xf;
543 DPRINTF("Reselected target %d\n", id
);
544 s
->current_dev
= s
->scsi_dev
[id
];
545 s
->current_tag
= tag
;
546 s
->scntl1
|= LSI_SCNTL1_CON
;
547 lsi_set_phase(s
, PHASE_MI
);
548 s
->msg_action
= p
->out
? 2 : 3;
549 s
->current_dma_len
= p
->pending
;
551 lsi_add_msg_byte(s
, 0x80);
552 if (s
->current_tag
& LSI_TAG_VALID
) {
553 lsi_add_msg_byte(s
, 0x20);
554 lsi_add_msg_byte(s
, tag
& 0xff);
557 s
->active_commands
--;
558 if (n
!= s
->active_commands
) {
559 s
->queue
[n
] = s
->queue
[s
->active_commands
];
563 /* Record that data is available for a queued command. Returns zero if
564 the device was reselected, nonzero if the IO is deferred. */
565 static int lsi_queue_tag(LSIState
*s
, uint32_t tag
, uint32_t arg
)
569 for (i
= 0; i
< s
->active_commands
; i
++) {
573 BADF("Multiple IO pending for tag %d\n", tag
);
576 if (s
->waiting
== 1) {
577 /* Reselect device. */
578 lsi_reselect(s
, tag
);
581 DPRINTF("Queueing IO tag=0x%x\n", tag
);
587 BADF("IO with unknown tag %d\n", tag
);
591 /* Callback to indicate that the SCSI layer has completed a transfer. */
592 static void lsi_command_complete(void *opaque
, int reason
, uint32_t tag
,
595 LSIState
*s
= (LSIState
*)opaque
;
598 out
= (s
->sstat1
& PHASE_MASK
) == PHASE_DO
;
599 if (reason
== SCSI_REASON_DONE
) {
600 DPRINTF("Command complete sense=%d\n", (int)arg
);
602 s
->command_complete
= 2;
603 if (s
->waiting
&& s
->dbc
!= 0) {
604 /* Raise phase mismatch for short transfers. */
605 lsi_bad_phase(s
, out
, PHASE_ST
);
607 lsi_set_phase(s
, PHASE_ST
);
609 lsi_resume_script(s
);
613 if (s
->waiting
== 1 || tag
!= s
->current_tag
) {
614 if (lsi_queue_tag(s
, tag
, arg
))
617 DPRINTF("Data ready tag=0x%x len=%d\n", tag
, arg
);
618 s
->current_dma_len
= arg
;
619 s
->command_complete
= 1;
622 if (s
->waiting
== 1 || s
->dbc
== 0) {
623 lsi_resume_script(s
);
629 static void lsi_do_command(LSIState
*s
)
634 DPRINTF("Send command len=%d\n", s
->dbc
);
637 cpu_physical_memory_read(s
->dnad
, buf
, s
->dbc
);
639 s
->command_complete
= 0;
640 n
= s
->current_dev
->send_command(s
->current_dev
, s
->current_tag
, buf
,
643 lsi_set_phase(s
, PHASE_DI
);
644 s
->current_dev
->read_data(s
->current_dev
, s
->current_tag
);
646 lsi_set_phase(s
, PHASE_DO
);
647 s
->current_dev
->write_data(s
->current_dev
, s
->current_tag
);
650 if (!s
->command_complete
) {
652 /* Command did not complete immediately so disconnect. */
653 lsi_add_msg_byte(s
, 2); /* SAVE DATA POINTER */
654 lsi_add_msg_byte(s
, 4); /* DISCONNECT */
656 lsi_set_phase(s
, PHASE_MI
);
658 lsi_queue_command(s
);
660 /* wait command complete */
661 lsi_set_phase(s
, PHASE_DI
);
666 static void lsi_do_status(LSIState
*s
)
669 DPRINTF("Get status len=%d sense=%d\n", s
->dbc
, s
->sense
);
671 BADF("Bad Status move\n");
675 cpu_physical_memory_write(s
->dnad
, &sense
, 1);
676 lsi_set_phase(s
, PHASE_MI
);
678 lsi_add_msg_byte(s
, 0); /* COMMAND COMPLETE */
681 static void lsi_disconnect(LSIState
*s
)
683 s
->scntl1
&= ~LSI_SCNTL1_CON
;
684 s
->sstat1
&= ~PHASE_MASK
;
687 static void lsi_do_msgin(LSIState
*s
)
690 DPRINTF("Message in len=%d/%d\n", s
->dbc
, s
->msg_len
);
695 cpu_physical_memory_write(s
->dnad
, s
->msg
, len
);
696 /* Linux drivers rely on the last byte being in the SIDL. */
697 s
->sidl
= s
->msg
[len
- 1];
700 memmove(s
->msg
, s
->msg
+ len
, s
->msg_len
);
702 /* ??? Check if ATN (not yet implemented) is asserted and maybe
703 switch to PHASE_MO. */
704 switch (s
->msg_action
) {
706 lsi_set_phase(s
, PHASE_CMD
);
712 lsi_set_phase(s
, PHASE_DO
);
715 lsi_set_phase(s
, PHASE_DI
);
723 /* Read the next byte during a MSGOUT phase. */
724 static uint8_t lsi_get_msgbyte(LSIState
*s
)
727 cpu_physical_memory_read(s
->dnad
, &data
, 1);
733 static void lsi_do_msgout(LSIState
*s
)
738 DPRINTF("MSG out len=%d\n", s
->dbc
);
740 msg
= lsi_get_msgbyte(s
);
745 DPRINTF("MSG: Disconnect\n");
749 DPRINTF("MSG: No Operation\n");
750 lsi_set_phase(s
, PHASE_CMD
);
753 len
= lsi_get_msgbyte(s
);
754 msg
= lsi_get_msgbyte(s
);
755 DPRINTF("Extended message 0x%x (len %d)\n", msg
, len
);
758 DPRINTF("SDTR (ignored)\n");
762 DPRINTF("WDTR (ignored)\n");
769 case 0x20: /* SIMPLE queue */
770 s
->current_tag
|= lsi_get_msgbyte(s
) | LSI_TAG_VALID
;
771 DPRINTF("SIMPLE queue tag=0x%x\n", s
->current_tag
& 0xff);
773 case 0x21: /* HEAD of queue */
774 BADF("HEAD queue not implemented\n");
775 s
->current_tag
|= lsi_get_msgbyte(s
) | LSI_TAG_VALID
;
777 case 0x22: /* ORDERED queue */
778 BADF("ORDERED queue not implemented\n");
779 s
->current_tag
|= lsi_get_msgbyte(s
) | LSI_TAG_VALID
;
782 if ((msg
& 0x80) == 0) {
785 s
->current_lun
= msg
& 7;
786 DPRINTF("Select LUN %d\n", s
->current_lun
);
787 lsi_set_phase(s
, PHASE_CMD
);
793 BADF("Unimplemented message 0x%02x\n", msg
);
794 lsi_set_phase(s
, PHASE_MI
);
795 lsi_add_msg_byte(s
, 7); /* MESSAGE REJECT */
799 /* Sign extend a 24-bit value. */
800 static inline int32_t sxt24(int32_t n
)
802 return (n
<< 8) >> 8;
805 static void lsi_memcpy(LSIState
*s
, uint32_t dest
, uint32_t src
, int count
)
808 uint8_t buf
[TARGET_PAGE_SIZE
];
810 DPRINTF("memcpy dest 0x%08x src 0x%08x count %d\n", dest
, src
, count
);
812 n
= (count
> TARGET_PAGE_SIZE
) ? TARGET_PAGE_SIZE
: count
;
813 cpu_physical_memory_read(src
, buf
, n
);
814 cpu_physical_memory_write(dest
, buf
, n
);
821 static void lsi_wait_reselect(LSIState
*s
)
824 DPRINTF("Wait Reselect\n");
825 if (s
->current_dma_len
)
826 BADF("Reselect with pending DMA\n");
827 for (i
= 0; i
< s
->active_commands
; i
++) {
828 if (s
->queue
[i
].pending
) {
829 lsi_reselect(s
, s
->queue
[i
].tag
);
833 if (s
->current_dma_len
== 0) {
838 static void lsi_execute_script(LSIState
*s
)
843 int insn_processed
= 0;
845 s
->istat1
|= LSI_ISTAT1_SRUN
;
848 insn
= read_dword(s
, s
->dsp
);
849 addr
= read_dword(s
, s
->dsp
+ 4);
850 DPRINTF("SCRIPTS dsp=%08x opcode %08x arg %08x\n", s
->dsp
, insn
, addr
);
852 s
->dcmd
= insn
>> 24;
854 switch (insn
>> 30) {
855 case 0: /* Block move. */
856 if (s
->sist1
& LSI_SIST1_STO
) {
857 DPRINTF("Delayed select timeout\n");
861 s
->dbc
= insn
& 0xffffff;
863 if (insn
& (1 << 29)) {
864 /* Indirect addressing. */
865 addr
= read_dword(s
, addr
);
866 } else if (insn
& (1 << 28)) {
869 /* Table indirect addressing. */
870 offset
= sxt24(addr
);
871 cpu_physical_memory_read(s
->dsa
+ offset
, (uint8_t *)buf
, 8);
872 s
->dbc
= cpu_to_le32(buf
[0]);
874 addr
= cpu_to_le32(buf
[1]);
876 if ((s
->sstat1
& PHASE_MASK
) != ((insn
>> 24) & 7)) {
877 DPRINTF("Wrong phase got %d expected %d\n",
878 s
->sstat1
& PHASE_MASK
, (insn
>> 24) & 7);
879 lsi_script_scsi_interrupt(s
, LSI_SIST0_MA
, 0);
885 switch (s
->sstat1
& 0x7) {
911 BADF("Unimplemented phase %d\n", s
->sstat1
& PHASE_MASK
);
914 s
->dfifo
= s
->dbc
& 0xff;
915 s
->ctest5
= (s
->ctest5
& 0xfc) | ((s
->dbc
>> 8) & 3);
918 s
->ua
= addr
+ s
->dbc
;
921 case 1: /* IO or Read/Write instruction. */
922 opcode
= (insn
>> 27) & 7;
926 if (insn
& (1 << 25)) {
927 id
= read_dword(s
, s
->dsa
+ sxt24(insn
));
931 id
= (id
>> 16) & 0xf;
932 if (insn
& (1 << 26)) {
933 addr
= s
->dsp
+ sxt24(addr
);
939 if (s
->current_dma_len
&& (s
->ssid
& 0xf) == id
) {
940 DPRINTF("Already reselected by target %d\n", id
);
943 s
->sstat0
|= LSI_SSTAT0_WOA
;
944 s
->scntl1
&= ~LSI_SCNTL1_IARB
;
945 if (id
>= LSI_MAX_DEVS
|| !s
->scsi_dev
[id
]) {
946 DPRINTF("Selected absent target %d\n", id
);
947 lsi_script_scsi_interrupt(s
, 0, LSI_SIST1_STO
);
951 DPRINTF("Selected target %d%s\n",
952 id
, insn
& (1 << 3) ? " ATN" : "");
953 /* ??? Linux drivers compain when this is set. Maybe
954 it only applies in low-level mode (unimplemented).
955 lsi_script_scsi_interrupt(s, LSI_SIST0_CMP, 0); */
956 s
->current_dev
= s
->scsi_dev
[id
];
957 s
->current_tag
= id
<< 8;
958 s
->scntl1
|= LSI_SCNTL1_CON
;
959 if (insn
& (1 << 3)) {
960 s
->socl
|= LSI_SOCL_ATN
;
962 lsi_set_phase(s
, PHASE_MO
);
964 case 1: /* Disconnect */
965 DPRINTF("Wait Disconect\n");
966 s
->scntl1
&= ~LSI_SCNTL1_CON
;
968 case 2: /* Wait Reselect */
969 lsi_wait_reselect(s
);
972 DPRINTF("Set%s%s%s%s\n",
973 insn
& (1 << 3) ? " ATN" : "",
974 insn
& (1 << 6) ? " ACK" : "",
975 insn
& (1 << 9) ? " TM" : "",
976 insn
& (1 << 10) ? " CC" : "");
977 if (insn
& (1 << 3)) {
978 s
->socl
|= LSI_SOCL_ATN
;
979 lsi_set_phase(s
, PHASE_MO
);
981 if (insn
& (1 << 9)) {
982 BADF("Target mode not implemented\n");
985 if (insn
& (1 << 10))
989 DPRINTF("Clear%s%s%s%s\n",
990 insn
& (1 << 3) ? " ATN" : "",
991 insn
& (1 << 6) ? " ACK" : "",
992 insn
& (1 << 9) ? " TM" : "",
993 insn
& (1 << 10) ? " CC" : "");
994 if (insn
& (1 << 3)) {
995 s
->socl
&= ~LSI_SOCL_ATN
;
997 if (insn
& (1 << 10))
1008 static const char *opcode_names
[3] =
1009 {"Write", "Read", "Read-Modify-Write"};
1010 static const char *operator_names
[8] =
1011 {"MOV", "SHL", "OR", "XOR", "AND", "SHR", "ADD", "ADC"};
1014 reg
= ((insn
>> 16) & 0x7f) | (insn
& 0x80);
1015 data8
= (insn
>> 8) & 0xff;
1016 opcode
= (insn
>> 27) & 7;
1017 operator = (insn
>> 24) & 7;
1018 DPRINTF("%s reg 0x%x %s data8=0x%02x sfbr=0x%02x%s\n",
1019 opcode_names
[opcode
- 5], reg
,
1020 operator_names
[operator], data8
, s
->sfbr
,
1021 (insn
& (1 << 23)) ? " SFBR" : "");
1024 case 5: /* From SFBR */
1028 case 6: /* To SFBR */
1030 op0
= lsi_reg_readb(s
, reg
);
1033 case 7: /* Read-modify-write */
1035 op0
= lsi_reg_readb(s
, reg
);
1036 if (insn
& (1 << 23)) {
1048 case 1: /* Shift left */
1050 op0
= (op0
<< 1) | s
->carry
;
1064 op0
= (op0
>> 1) | (s
->carry
<< 7);
1069 s
->carry
= op0
< op1
;
1072 op0
+= op1
+ s
->carry
;
1074 s
->carry
= op0
<= op1
;
1076 s
->carry
= op0
< op1
;
1081 case 5: /* From SFBR */
1082 case 7: /* Read-modify-write */
1083 lsi_reg_writeb(s
, reg
, op0
);
1085 case 6: /* To SFBR */
1092 case 2: /* Transfer Control. */
1097 if ((insn
& 0x002e0000) == 0) {
1101 if (s
->sist1
& LSI_SIST1_STO
) {
1102 DPRINTF("Delayed select timeout\n");
1106 cond
= jmp
= (insn
& (1 << 19)) != 0;
1107 if (cond
== jmp
&& (insn
& (1 << 21))) {
1108 DPRINTF("Compare carry %d\n", s
->carry
== jmp
);
1109 cond
= s
->carry
!= 0;
1111 if (cond
== jmp
&& (insn
& (1 << 17))) {
1112 DPRINTF("Compare phase %d %c= %d\n",
1113 (s
->sstat1
& PHASE_MASK
),
1115 ((insn
>> 24) & 7));
1116 cond
= (s
->sstat1
& PHASE_MASK
) == ((insn
>> 24) & 7);
1118 if (cond
== jmp
&& (insn
& (1 << 18))) {
1121 mask
= (~insn
>> 8) & 0xff;
1122 DPRINTF("Compare data 0x%x & 0x%x %c= 0x%x\n",
1123 s
->sfbr
, mask
, jmp
? '=' : '!', insn
& mask
);
1124 cond
= (s
->sfbr
& mask
) == (insn
& mask
);
1127 if (insn
& (1 << 23)) {
1128 /* Relative address. */
1129 addr
= s
->dsp
+ sxt24(addr
);
1131 switch ((insn
>> 27) & 7) {
1133 DPRINTF("Jump to 0x%08x\n", addr
);
1137 DPRINTF("Call 0x%08x\n", addr
);
1141 case 2: /* Return */
1142 DPRINTF("Return to 0x%08x\n", s
->temp
);
1145 case 3: /* Interrupt */
1146 DPRINTF("Interrupt 0x%08x\n", s
->dsps
);
1147 if ((insn
& (1 << 20)) != 0) {
1148 s
->istat0
|= LSI_ISTAT0_INTF
;
1151 lsi_script_dma_interrupt(s
, LSI_DSTAT_SIR
);
1155 DPRINTF("Illegal transfer control\n");
1156 lsi_script_dma_interrupt(s
, LSI_DSTAT_IID
);
1160 DPRINTF("Control condition failed\n");
1166 if ((insn
& (1 << 29)) == 0) {
1169 /* ??? The docs imply the destination address is loaded into
1170 the TEMP register. However the Linux drivers rely on
1171 the value being presrved. */
1172 dest
= read_dword(s
, s
->dsp
);
1174 lsi_memcpy(s
, dest
, addr
, insn
& 0xffffff);
1181 if (insn
& (1 << 28)) {
1182 addr
= s
->dsa
+ sxt24(addr
);
1185 reg
= (insn
>> 16) & 0xff;
1186 if (insn
& (1 << 24)) {
1187 cpu_physical_memory_read(addr
, data
, n
);
1188 DPRINTF("Load reg 0x%x size %d addr 0x%08x = %08x\n", reg
, n
,
1189 addr
, *(int *)data
);
1190 for (i
= 0; i
< n
; i
++) {
1191 lsi_reg_writeb(s
, reg
+ i
, data
[i
]);
1194 DPRINTF("Store reg 0x%x size %d addr 0x%08x\n", reg
, n
, addr
);
1195 for (i
= 0; i
< n
; i
++) {
1196 data
[i
] = lsi_reg_readb(s
, reg
+ i
);
1198 cpu_physical_memory_write(addr
, data
, n
);
1202 if (insn_processed
> 10000 && !s
->waiting
) {
1203 /* Some windows drivers make the device spin waiting for a memory
1204 location to change. If we have been executed a lot of code then
1205 assume this is the case and force an unexpected device disconnect.
1206 This is apparently sufficient to beat the drivers into submission.
1208 if (!(s
->sien0
& LSI_SIST0_UDC
))
1209 fprintf(stderr
, "inf. loop with UDC masked\n");
1210 lsi_script_scsi_interrupt(s
, LSI_SIST0_UDC
, 0);
1212 } else if (s
->istat1
& LSI_ISTAT1_SRUN
&& !s
->waiting
) {
1213 if (s
->dcntl
& LSI_DCNTL_SSM
) {
1214 lsi_script_dma_interrupt(s
, LSI_DSTAT_SSI
);
1219 DPRINTF("SCRIPTS execution stopped\n");
1222 static uint8_t lsi_reg_readb(LSIState
*s
, int offset
)
1225 #define CASE_GET_REG32(name, addr) \
1226 case addr: return s->name & 0xff; \
1227 case addr + 1: return (s->name >> 8) & 0xff; \
1228 case addr + 2: return (s->name >> 16) & 0xff; \
1229 case addr + 3: return (s->name >> 24) & 0xff;
1231 #ifdef DEBUG_LSI_REG
1232 DPRINTF("Read reg %x\n", offset
);
1235 case 0x00: /* SCNTL0 */
1237 case 0x01: /* SCNTL1 */
1239 case 0x02: /* SCNTL2 */
1241 case 0x03: /* SCNTL3 */
1243 case 0x04: /* SCID */
1245 case 0x05: /* SXFER */
1247 case 0x06: /* SDID */
1249 case 0x07: /* GPREG0 */
1251 case 0x08: /* Revision ID */
1253 case 0xa: /* SSID */
1255 case 0xb: /* SBCL */
1256 /* ??? This is not correct. However it's (hopefully) only
1257 used for diagnostics, so should be ok. */
1259 case 0xc: /* DSTAT */
1260 tmp
= s
->dstat
| 0x80;
1261 if ((s
->istat0
& LSI_ISTAT0_INTF
) == 0)
1265 case 0x0d: /* SSTAT0 */
1267 case 0x0e: /* SSTAT1 */
1269 case 0x0f: /* SSTAT2 */
1270 return s
->scntl1
& LSI_SCNTL1_CON
? 0 : 2;
1271 CASE_GET_REG32(dsa
, 0x10)
1272 case 0x14: /* ISTAT0 */
1274 case 0x16: /* MBOX0 */
1276 case 0x17: /* MBOX1 */
1278 case 0x18: /* CTEST0 */
1280 case 0x19: /* CTEST1 */
1282 case 0x1a: /* CTEST2 */
1283 tmp
= LSI_CTEST2_DACK
| LSI_CTEST2_CM
;
1284 if (s
->istat0
& LSI_ISTAT0_SIGP
) {
1285 s
->istat0
&= ~LSI_ISTAT0_SIGP
;
1286 tmp
|= LSI_CTEST2_SIGP
;
1289 case 0x1b: /* CTEST3 */
1291 CASE_GET_REG32(temp
, 0x1c)
1292 case 0x20: /* DFIFO */
1294 case 0x21: /* CTEST4 */
1296 case 0x22: /* CTEST5 */
1298 case 0x23: /* CTEST6 */
1300 case 0x24: /* DBC[0:7] */
1301 return s
->dbc
& 0xff;
1302 case 0x25: /* DBC[8:15] */
1303 return (s
->dbc
>> 8) & 0xff;
1304 case 0x26: /* DBC[16->23] */
1305 return (s
->dbc
>> 16) & 0xff;
1306 case 0x27: /* DCMD */
1308 CASE_GET_REG32(dsp
, 0x2c)
1309 CASE_GET_REG32(dsps
, 0x30)
1310 CASE_GET_REG32(scratch
[0], 0x34)
1311 case 0x38: /* DMODE */
1313 case 0x39: /* DIEN */
1315 case 0x3b: /* DCNTL */
1317 case 0x40: /* SIEN0 */
1319 case 0x41: /* SIEN1 */
1321 case 0x42: /* SIST0 */
1326 case 0x43: /* SIST1 */
1331 case 0x47: /* GPCNTL0 */
1333 case 0x48: /* STIME0 */
1335 case 0x4a: /* RESPID0 */
1337 case 0x4b: /* RESPID1 */
1339 case 0x4d: /* STEST1 */
1341 case 0x4e: /* STEST2 */
1343 case 0x4f: /* STEST3 */
1345 case 0x50: /* SIDL */
1346 /* This is needed by the linux drivers. We currently only update it
1347 during the MSG IN phase. */
1349 case 0x52: /* STEST4 */
1351 case 0x56: /* CCNTL0 */
1353 case 0x57: /* CCNTL1 */
1355 case 0x58: /* SBDL */
1356 /* Some drivers peek at the data bus during the MSG IN phase. */
1357 if ((s
->sstat1
& PHASE_MASK
) == PHASE_MI
)
1360 case 0x59: /* SBDL high */
1362 CASE_GET_REG32(mmrs
, 0xa0)
1363 CASE_GET_REG32(mmws
, 0xa4)
1364 CASE_GET_REG32(sfs
, 0xa8)
1365 CASE_GET_REG32(drs
, 0xac)
1366 CASE_GET_REG32(sbms
, 0xb0)
1367 CASE_GET_REG32(dmbs
, 0xb4)
1368 CASE_GET_REG32(dnad64
, 0xb8)
1369 CASE_GET_REG32(pmjad1
, 0xc0)
1370 CASE_GET_REG32(pmjad2
, 0xc4)
1371 CASE_GET_REG32(rbc
, 0xc8)
1372 CASE_GET_REG32(ua
, 0xcc)
1373 CASE_GET_REG32(ia
, 0xd4)
1374 CASE_GET_REG32(sbc
, 0xd8)
1375 CASE_GET_REG32(csbc
, 0xdc)
1377 if (offset
>= 0x5c && offset
< 0xa0) {
1380 n
= (offset
- 0x58) >> 2;
1381 shift
= (offset
& 3) * 8;
1382 return (s
->scratch
[n
] >> shift
) & 0xff;
1384 BADF("readb 0x%x\n", offset
);
1386 #undef CASE_GET_REG32
1389 static void lsi_reg_writeb(LSIState
*s
, int offset
, uint8_t val
)
1391 #define CASE_SET_REG32(name, addr) \
1392 case addr : s->name &= 0xffffff00; s->name |= val; break; \
1393 case addr + 1: s->name &= 0xffff00ff; s->name |= val << 8; break; \
1394 case addr + 2: s->name &= 0xff00ffff; s->name |= val << 16; break; \
1395 case addr + 3: s->name &= 0x00ffffff; s->name |= val << 24; break;
1397 #ifdef DEBUG_LSI_REG
1398 DPRINTF("Write reg %x = %02x\n", offset
, val
);
1401 case 0x00: /* SCNTL0 */
1403 if (val
& LSI_SCNTL0_START
) {
1404 BADF("Start sequence not implemented\n");
1407 case 0x01: /* SCNTL1 */
1408 s
->scntl1
= val
& ~LSI_SCNTL1_SST
;
1409 if (val
& LSI_SCNTL1_IARB
) {
1410 BADF("Immediate Arbritration not implemented\n");
1412 if (val
& LSI_SCNTL1_RST
) {
1413 s
->sstat0
|= LSI_SSTAT0_RST
;
1414 lsi_script_scsi_interrupt(s
, LSI_SIST0_RST
, 0);
1416 s
->sstat0
&= ~LSI_SSTAT0_RST
;
1419 case 0x02: /* SCNTL2 */
1420 val
&= ~(LSI_SCNTL2_WSR
| LSI_SCNTL2_WSS
);
1423 case 0x03: /* SCNTL3 */
1426 case 0x04: /* SCID */
1429 case 0x05: /* SXFER */
1432 case 0x06: /* SDID */
1433 if ((val
& 0xf) != (s
->ssid
& 0xf))
1434 BADF("Destination ID does not match SSID\n");
1435 s
->sdid
= val
& 0xf;
1437 case 0x07: /* GPREG0 */
1439 case 0x08: /* SFBR */
1440 /* The CPU is not allowed to write to this register. However the
1441 SCRIPTS register move instructions are. */
1444 case 0x0c: case 0x0d: case 0x0e: case 0x0f:
1445 /* Linux writes to these readonly registers on startup. */
1447 CASE_SET_REG32(dsa
, 0x10)
1448 case 0x14: /* ISTAT0 */
1449 s
->istat0
= (s
->istat0
& 0x0f) | (val
& 0xf0);
1450 if (val
& LSI_ISTAT0_ABRT
) {
1451 lsi_script_dma_interrupt(s
, LSI_DSTAT_ABRT
);
1453 if (val
& LSI_ISTAT0_INTF
) {
1454 s
->istat0
&= ~LSI_ISTAT0_INTF
;
1457 if (s
->waiting
== 1 && val
& LSI_ISTAT0_SIGP
) {
1458 DPRINTF("Woken by SIGP\n");
1461 lsi_execute_script(s
);
1463 if (val
& LSI_ISTAT0_SRST
) {
1467 case 0x16: /* MBOX0 */
1470 case 0x17: /* MBOX1 */
1473 case 0x1b: /* CTEST3 */
1474 s
->ctest3
= val
& 0x0f;
1476 CASE_SET_REG32(temp
, 0x1c)
1477 case 0x21: /* CTEST4 */
1479 BADF("Unimplemented CTEST4-FBL 0x%x\n", val
);
1483 case 0x22: /* CTEST5 */
1484 if (val
& (LSI_CTEST5_ADCK
| LSI_CTEST5_BBCK
)) {
1485 BADF("CTEST5 DMA increment not implemented\n");
1489 case 0x2c: /* DSP[0:7] */
1490 s
->dsp
&= 0xffffff00;
1493 case 0x2d: /* DSP[8:15] */
1494 s
->dsp
&= 0xffff00ff;
1497 case 0x2e: /* DSP[16:23] */
1498 s
->dsp
&= 0xff00ffff;
1499 s
->dsp
|= val
<< 16;
1501 case 0x2f: /* DSP[24:31] */
1502 s
->dsp
&= 0x00ffffff;
1503 s
->dsp
|= val
<< 24;
1504 if ((s
->dmode
& LSI_DMODE_MAN
) == 0
1505 && (s
->istat1
& LSI_ISTAT1_SRUN
) == 0)
1506 lsi_execute_script(s
);
1508 CASE_SET_REG32(dsps
, 0x30)
1509 CASE_SET_REG32(scratch
[0], 0x34)
1510 case 0x38: /* DMODE */
1511 if (val
& (LSI_DMODE_SIOM
| LSI_DMODE_DIOM
)) {
1512 BADF("IO mappings not implemented\n");
1516 case 0x39: /* DIEN */
1520 case 0x3b: /* DCNTL */
1521 s
->dcntl
= val
& ~(LSI_DCNTL_PFF
| LSI_DCNTL_STD
);
1522 if ((val
& LSI_DCNTL_STD
) && (s
->istat1
& LSI_ISTAT1_SRUN
) == 0)
1523 lsi_execute_script(s
);
1525 case 0x40: /* SIEN0 */
1529 case 0x41: /* SIEN1 */
1533 case 0x47: /* GPCNTL0 */
1535 case 0x48: /* STIME0 */
1538 case 0x49: /* STIME1 */
1540 DPRINTF("General purpose timer not implemented\n");
1541 /* ??? Raising the interrupt immediately seems to be sufficient
1542 to keep the FreeBSD driver happy. */
1543 lsi_script_scsi_interrupt(s
, 0, LSI_SIST1_GEN
);
1546 case 0x4a: /* RESPID0 */
1549 case 0x4b: /* RESPID1 */
1552 case 0x4d: /* STEST1 */
1555 case 0x4e: /* STEST2 */
1557 BADF("Low level mode not implemented\n");
1561 case 0x4f: /* STEST3 */
1563 BADF("SCSI FIFO test mode not implemented\n");
1567 case 0x56: /* CCNTL0 */
1570 case 0x57: /* CCNTL1 */
1573 CASE_SET_REG32(mmrs
, 0xa0)
1574 CASE_SET_REG32(mmws
, 0xa4)
1575 CASE_SET_REG32(sfs
, 0xa8)
1576 CASE_SET_REG32(drs
, 0xac)
1577 CASE_SET_REG32(sbms
, 0xb0)
1578 CASE_SET_REG32(dmbs
, 0xb4)
1579 CASE_SET_REG32(dnad64
, 0xb8)
1580 CASE_SET_REG32(pmjad1
, 0xc0)
1581 CASE_SET_REG32(pmjad2
, 0xc4)
1582 CASE_SET_REG32(rbc
, 0xc8)
1583 CASE_SET_REG32(ua
, 0xcc)
1584 CASE_SET_REG32(ia
, 0xd4)
1585 CASE_SET_REG32(sbc
, 0xd8)
1586 CASE_SET_REG32(csbc
, 0xdc)
1588 if (offset
>= 0x5c && offset
< 0xa0) {
1591 n
= (offset
- 0x58) >> 2;
1592 shift
= (offset
& 3) * 8;
1593 s
->scratch
[n
] &= ~(0xff << shift
);
1594 s
->scratch
[n
] |= (val
& 0xff) << shift
;
1596 BADF("Unhandled writeb 0x%x = 0x%x\n", offset
, val
);
1599 #undef CASE_SET_REG32
1602 static void lsi_mmio_writeb(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1604 LSIState
*s
= (LSIState
*)opaque
;
1606 lsi_reg_writeb(s
, addr
& 0xff, val
);
1609 static void lsi_mmio_writew(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1611 LSIState
*s
= (LSIState
*)opaque
;
1614 lsi_reg_writeb(s
, addr
, val
& 0xff);
1615 lsi_reg_writeb(s
, addr
+ 1, (val
>> 8) & 0xff);
1618 static void lsi_mmio_writel(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1620 LSIState
*s
= (LSIState
*)opaque
;
1623 lsi_reg_writeb(s
, addr
, val
& 0xff);
1624 lsi_reg_writeb(s
, addr
+ 1, (val
>> 8) & 0xff);
1625 lsi_reg_writeb(s
, addr
+ 2, (val
>> 16) & 0xff);
1626 lsi_reg_writeb(s
, addr
+ 3, (val
>> 24) & 0xff);
1629 static uint32_t lsi_mmio_readb(void *opaque
, target_phys_addr_t addr
)
1631 LSIState
*s
= (LSIState
*)opaque
;
1633 return lsi_reg_readb(s
, addr
& 0xff);
1636 static uint32_t lsi_mmio_readw(void *opaque
, target_phys_addr_t addr
)
1638 LSIState
*s
= (LSIState
*)opaque
;
1642 val
= lsi_reg_readb(s
, addr
);
1643 val
|= lsi_reg_readb(s
, addr
+ 1) << 8;
1647 static uint32_t lsi_mmio_readl(void *opaque
, target_phys_addr_t addr
)
1649 LSIState
*s
= (LSIState
*)opaque
;
1652 val
= lsi_reg_readb(s
, addr
);
1653 val
|= lsi_reg_readb(s
, addr
+ 1) << 8;
1654 val
|= lsi_reg_readb(s
, addr
+ 2) << 16;
1655 val
|= lsi_reg_readb(s
, addr
+ 3) << 24;
1659 static CPUReadMemoryFunc
*lsi_mmio_readfn
[3] = {
1665 static CPUWriteMemoryFunc
*lsi_mmio_writefn
[3] = {
1671 static void lsi_ram_writeb(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1673 LSIState
*s
= (LSIState
*)opaque
;
1678 newval
= s
->script_ram
[addr
>> 2];
1679 shift
= (addr
& 3) * 8;
1680 newval
&= ~(0xff << shift
);
1681 newval
|= val
<< shift
;
1682 s
->script_ram
[addr
>> 2] = newval
;
1685 static void lsi_ram_writew(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1687 LSIState
*s
= (LSIState
*)opaque
;
1691 newval
= s
->script_ram
[addr
>> 2];
1693 newval
= (newval
& 0xffff) | (val
<< 16);
1695 newval
= (newval
& 0xffff0000) | val
;
1697 s
->script_ram
[addr
>> 2] = newval
;
1701 static void lsi_ram_writel(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1703 LSIState
*s
= (LSIState
*)opaque
;
1706 s
->script_ram
[addr
>> 2] = val
;
1709 static uint32_t lsi_ram_readb(void *opaque
, target_phys_addr_t addr
)
1711 LSIState
*s
= (LSIState
*)opaque
;
1715 val
= s
->script_ram
[addr
>> 2];
1716 val
>>= (addr
& 3) * 8;
1720 static uint32_t lsi_ram_readw(void *opaque
, target_phys_addr_t addr
)
1722 LSIState
*s
= (LSIState
*)opaque
;
1726 val
= s
->script_ram
[addr
>> 2];
1729 return le16_to_cpu(val
);
1732 static uint32_t lsi_ram_readl(void *opaque
, target_phys_addr_t addr
)
1734 LSIState
*s
= (LSIState
*)opaque
;
1737 return le32_to_cpu(s
->script_ram
[addr
>> 2]);
1740 static CPUReadMemoryFunc
*lsi_ram_readfn
[3] = {
1746 static CPUWriteMemoryFunc
*lsi_ram_writefn
[3] = {
1752 static uint32_t lsi_io_readb(void *opaque
, uint32_t addr
)
1754 LSIState
*s
= (LSIState
*)opaque
;
1755 return lsi_reg_readb(s
, addr
& 0xff);
1758 static uint32_t lsi_io_readw(void *opaque
, uint32_t addr
)
1760 LSIState
*s
= (LSIState
*)opaque
;
1763 val
= lsi_reg_readb(s
, addr
);
1764 val
|= lsi_reg_readb(s
, addr
+ 1) << 8;
1768 static uint32_t lsi_io_readl(void *opaque
, uint32_t addr
)
1770 LSIState
*s
= (LSIState
*)opaque
;
1773 val
= lsi_reg_readb(s
, addr
);
1774 val
|= lsi_reg_readb(s
, addr
+ 1) << 8;
1775 val
|= lsi_reg_readb(s
, addr
+ 2) << 16;
1776 val
|= lsi_reg_readb(s
, addr
+ 3) << 24;
1780 static void lsi_io_writeb(void *opaque
, uint32_t addr
, uint32_t val
)
1782 LSIState
*s
= (LSIState
*)opaque
;
1783 lsi_reg_writeb(s
, addr
& 0xff, val
);
1786 static void lsi_io_writew(void *opaque
, uint32_t addr
, uint32_t val
)
1788 LSIState
*s
= (LSIState
*)opaque
;
1790 lsi_reg_writeb(s
, addr
, val
& 0xff);
1791 lsi_reg_writeb(s
, addr
+ 1, (val
>> 8) & 0xff);
1794 static void lsi_io_writel(void *opaque
, uint32_t addr
, uint32_t val
)
1796 LSIState
*s
= (LSIState
*)opaque
;
1798 lsi_reg_writeb(s
, addr
, val
& 0xff);
1799 lsi_reg_writeb(s
, addr
+ 1, (val
>> 8) & 0xff);
1800 lsi_reg_writeb(s
, addr
+ 2, (val
>> 16) & 0xff);
1801 lsi_reg_writeb(s
, addr
+ 3, (val
>> 24) & 0xff);
1804 static void lsi_io_mapfunc(PCIDevice
*pci_dev
, int region_num
,
1805 uint32_t addr
, uint32_t size
, int type
)
1807 LSIState
*s
= (LSIState
*)pci_dev
;
1809 DPRINTF("Mapping IO at %08x\n", addr
);
1811 register_ioport_write(addr
, 256, 1, lsi_io_writeb
, s
);
1812 register_ioport_read(addr
, 256, 1, lsi_io_readb
, s
);
1813 register_ioport_write(addr
, 256, 2, lsi_io_writew
, s
);
1814 register_ioport_read(addr
, 256, 2, lsi_io_readw
, s
);
1815 register_ioport_write(addr
, 256, 4, lsi_io_writel
, s
);
1816 register_ioport_read(addr
, 256, 4, lsi_io_readl
, s
);
1819 static void lsi_ram_mapfunc(PCIDevice
*pci_dev
, int region_num
,
1820 uint32_t addr
, uint32_t size
, int type
)
1822 LSIState
*s
= (LSIState
*)pci_dev
;
1824 DPRINTF("Mapping ram at %08x\n", addr
);
1825 s
->script_ram_base
= addr
;
1826 cpu_register_physical_memory(addr
+ 0, 0x2000, s
->ram_io_addr
);
1829 static void lsi_mmio_mapfunc(PCIDevice
*pci_dev
, int region_num
,
1830 uint32_t addr
, uint32_t size
, int type
)
1832 LSIState
*s
= (LSIState
*)pci_dev
;
1834 DPRINTF("Mapping registers at %08x\n", addr
);
1835 cpu_register_physical_memory(addr
+ 0, 0x400, s
->mmio_io_addr
);
1838 void lsi_scsi_attach(void *opaque
, BlockDriverState
*bd
, int id
)
1840 LSIState
*s
= (LSIState
*)opaque
;
1843 for (id
= 0; id
< LSI_MAX_DEVS
; id
++) {
1844 if (s
->scsi_dev
[id
] == NULL
)
1848 if (id
>= LSI_MAX_DEVS
) {
1849 BADF("Bad Device ID %d\n", id
);
1852 if (s
->scsi_dev
[id
]) {
1853 DPRINTF("Destroying device %d\n", id
);
1854 s
->scsi_dev
[id
]->destroy(s
->scsi_dev
[id
]);
1856 DPRINTF("Attaching block device %d\n", id
);
1857 s
->scsi_dev
[id
] = scsi_generic_init(bd
, 1, lsi_command_complete
, s
);
1858 if (s
->scsi_dev
[id
] == NULL
)
1859 s
->scsi_dev
[id
] = scsi_disk_init(bd
, 1, lsi_command_complete
, s
);
1860 bd
->devfn
= s
->pci_dev
.devfn
;
1863 int lsi_scsi_uninit(PCIDevice
*d
)
1865 LSIState
*s
= (LSIState
*) d
;
1867 cpu_unregister_io_memory(s
->mmio_io_addr
);
1868 cpu_unregister_io_memory(s
->ram_io_addr
);
1870 qemu_free(s
->queue
);
1875 void *lsi_scsi_init(PCIBus
*bus
, int devfn
)
1879 s
= (LSIState
*)pci_register_device(bus
, "LSI53C895A SCSI HBA",
1880 sizeof(*s
), devfn
, NULL
, NULL
);
1882 fprintf(stderr
, "lsi-scsi: Failed to register PCI device\n");
1886 s
->pci_dev
.config
[0x00] = 0x00;
1887 s
->pci_dev
.config
[0x01] = 0x10;
1888 s
->pci_dev
.config
[0x02] = 0x12;
1889 s
->pci_dev
.config
[0x03] = 0x00;
1890 s
->pci_dev
.config
[0x0b] = 0x01;
1891 s
->pci_dev
.config
[0x3d] = 0x01; /* interrupt pin 1 */
1893 s
->mmio_io_addr
= cpu_register_io_memory(0, lsi_mmio_readfn
,
1894 lsi_mmio_writefn
, s
);
1895 s
->ram_io_addr
= cpu_register_io_memory(0, lsi_ram_readfn
,
1896 lsi_ram_writefn
, s
);
1898 pci_register_io_region((struct PCIDevice
*)s
, 0, 256,
1899 PCI_ADDRESS_SPACE_IO
, lsi_io_mapfunc
);
1900 pci_register_io_region((struct PCIDevice
*)s
, 1, 0x400,
1901 PCI_ADDRESS_SPACE_MEM
, lsi_mmio_mapfunc
);
1902 pci_register_io_region((struct PCIDevice
*)s
, 2, 0x2000,
1903 PCI_ADDRESS_SPACE_MEM
, lsi_ram_mapfunc
);
1904 s
->queue
= qemu_malloc(sizeof(lsi_queue
));
1906 s
->active_commands
= 0;
1907 s
->pci_dev
.unregister
= lsi_scsi_uninit
;