Statistics
| Branch: | Revision:

root / hw / lsi53c895a.c @ 46c305ef

History | View | Annotate | Download (61.4 kB)

1 5fafdf24 ths
/*
2 7d8406be pbrook
 * QEMU LSI53C895A SCSI Host Bus Adapter emulation
3 7d8406be pbrook
 *
4 7d8406be pbrook
 * Copyright (c) 2006 CodeSourcery.
5 7d8406be pbrook
 * Written by Paul Brook
6 7d8406be pbrook
 *
7 8e31bf38 Matthew Fernandez
 * This code is licensed under the LGPL.
8 7d8406be pbrook
 */
9 7d8406be pbrook
10 7d8406be pbrook
/* ??? Need to check if the {read,write}[wl] routines work properly on
11 7d8406be pbrook
   big-endian targets.  */
12 7d8406be pbrook
13 a15fdf86 Laszlo Ast
#include <assert.h>
14 777aec7a Nolan
15 87ecb68b pbrook
#include "hw.h"
16 87ecb68b pbrook
#include "pci.h"
17 43b443b6 Gerd Hoffmann
#include "scsi.h"
18 b0a7b120 aliguori
#include "block_int.h"
19 7d8406be pbrook
20 7d8406be pbrook
//#define DEBUG_LSI
21 7d8406be pbrook
//#define DEBUG_LSI_REG
22 7d8406be pbrook
23 7d8406be pbrook
#ifdef DEBUG_LSI
24 001faf32 Blue Swirl
#define DPRINTF(fmt, ...) \
25 001faf32 Blue Swirl
do { printf("lsi_scsi: " fmt , ## __VA_ARGS__); } while (0)
26 001faf32 Blue Swirl
#define BADF(fmt, ...) \
27 001faf32 Blue Swirl
do { fprintf(stderr, "lsi_scsi: error: " fmt , ## __VA_ARGS__); exit(1);} while (0)
28 7d8406be pbrook
#else
29 001faf32 Blue Swirl
#define DPRINTF(fmt, ...) do {} while(0)
30 001faf32 Blue Swirl
#define BADF(fmt, ...) \
31 001faf32 Blue Swirl
do { fprintf(stderr, "lsi_scsi: error: " fmt , ## __VA_ARGS__);} while (0)
32 7d8406be pbrook
#endif
33 7d8406be pbrook
34 18e08a55 Michael S. Tsirkin
#define LSI_MAX_DEVS 7
35 18e08a55 Michael S. Tsirkin
36 7d8406be pbrook
#define LSI_SCNTL0_TRG    0x01
37 7d8406be pbrook
#define LSI_SCNTL0_AAP    0x02
38 7d8406be pbrook
#define LSI_SCNTL0_EPC    0x08
39 7d8406be pbrook
#define LSI_SCNTL0_WATN   0x10
40 7d8406be pbrook
#define LSI_SCNTL0_START  0x20
41 7d8406be pbrook
42 7d8406be pbrook
#define LSI_SCNTL1_SST    0x01
43 7d8406be pbrook
#define LSI_SCNTL1_IARB   0x02
44 7d8406be pbrook
#define LSI_SCNTL1_AESP   0x04
45 7d8406be pbrook
#define LSI_SCNTL1_RST    0x08
46 7d8406be pbrook
#define LSI_SCNTL1_CON    0x10
47 7d8406be pbrook
#define LSI_SCNTL1_DHP    0x20
48 7d8406be pbrook
#define LSI_SCNTL1_ADB    0x40
49 7d8406be pbrook
#define LSI_SCNTL1_EXC    0x80
50 7d8406be pbrook
51 7d8406be pbrook
#define LSI_SCNTL2_WSR    0x01
52 7d8406be pbrook
#define LSI_SCNTL2_VUE0   0x02
53 7d8406be pbrook
#define LSI_SCNTL2_VUE1   0x04
54 7d8406be pbrook
#define LSI_SCNTL2_WSS    0x08
55 7d8406be pbrook
#define LSI_SCNTL2_SLPHBEN 0x10
56 7d8406be pbrook
#define LSI_SCNTL2_SLPMD  0x20
57 7d8406be pbrook
#define LSI_SCNTL2_CHM    0x40
58 7d8406be pbrook
#define LSI_SCNTL2_SDU    0x80
59 7d8406be pbrook
60 7d8406be pbrook
#define LSI_ISTAT0_DIP    0x01
61 7d8406be pbrook
#define LSI_ISTAT0_SIP    0x02
62 7d8406be pbrook
#define LSI_ISTAT0_INTF   0x04
63 7d8406be pbrook
#define LSI_ISTAT0_CON    0x08
64 7d8406be pbrook
#define LSI_ISTAT0_SEM    0x10
65 7d8406be pbrook
#define LSI_ISTAT0_SIGP   0x20
66 7d8406be pbrook
#define LSI_ISTAT0_SRST   0x40
67 7d8406be pbrook
#define LSI_ISTAT0_ABRT   0x80
68 7d8406be pbrook
69 7d8406be pbrook
#define LSI_ISTAT1_SI     0x01
70 7d8406be pbrook
#define LSI_ISTAT1_SRUN   0x02
71 7d8406be pbrook
#define LSI_ISTAT1_FLSH   0x04
72 7d8406be pbrook
73 7d8406be pbrook
#define LSI_SSTAT0_SDP0   0x01
74 7d8406be pbrook
#define LSI_SSTAT0_RST    0x02
75 7d8406be pbrook
#define LSI_SSTAT0_WOA    0x04
76 7d8406be pbrook
#define LSI_SSTAT0_LOA    0x08
77 7d8406be pbrook
#define LSI_SSTAT0_AIP    0x10
78 7d8406be pbrook
#define LSI_SSTAT0_OLF    0x20
79 7d8406be pbrook
#define LSI_SSTAT0_ORF    0x40
80 7d8406be pbrook
#define LSI_SSTAT0_ILF    0x80
81 7d8406be pbrook
82 7d8406be pbrook
#define LSI_SIST0_PAR     0x01
83 7d8406be pbrook
#define LSI_SIST0_RST     0x02
84 7d8406be pbrook
#define LSI_SIST0_UDC     0x04
85 7d8406be pbrook
#define LSI_SIST0_SGE     0x08
86 7d8406be pbrook
#define LSI_SIST0_RSL     0x10
87 7d8406be pbrook
#define LSI_SIST0_SEL     0x20
88 7d8406be pbrook
#define LSI_SIST0_CMP     0x40
89 7d8406be pbrook
#define LSI_SIST0_MA      0x80
90 7d8406be pbrook
91 7d8406be pbrook
#define LSI_SIST1_HTH     0x01
92 7d8406be pbrook
#define LSI_SIST1_GEN     0x02
93 7d8406be pbrook
#define LSI_SIST1_STO     0x04
94 7d8406be pbrook
#define LSI_SIST1_SBMC    0x10
95 7d8406be pbrook
96 7d8406be pbrook
#define LSI_SOCL_IO       0x01
97 7d8406be pbrook
#define LSI_SOCL_CD       0x02
98 7d8406be pbrook
#define LSI_SOCL_MSG      0x04
99 7d8406be pbrook
#define LSI_SOCL_ATN      0x08
100 7d8406be pbrook
#define LSI_SOCL_SEL      0x10
101 7d8406be pbrook
#define LSI_SOCL_BSY      0x20
102 7d8406be pbrook
#define LSI_SOCL_ACK      0x40
103 7d8406be pbrook
#define LSI_SOCL_REQ      0x80
104 7d8406be pbrook
105 7d8406be pbrook
#define LSI_DSTAT_IID     0x01
106 7d8406be pbrook
#define LSI_DSTAT_SIR     0x04
107 7d8406be pbrook
#define LSI_DSTAT_SSI     0x08
108 7d8406be pbrook
#define LSI_DSTAT_ABRT    0x10
109 7d8406be pbrook
#define LSI_DSTAT_BF      0x20
110 7d8406be pbrook
#define LSI_DSTAT_MDPE    0x40
111 7d8406be pbrook
#define LSI_DSTAT_DFE     0x80
112 7d8406be pbrook
113 7d8406be pbrook
#define LSI_DCNTL_COM     0x01
114 7d8406be pbrook
#define LSI_DCNTL_IRQD    0x02
115 7d8406be pbrook
#define LSI_DCNTL_STD     0x04
116 7d8406be pbrook
#define LSI_DCNTL_IRQM    0x08
117 7d8406be pbrook
#define LSI_DCNTL_SSM     0x10
118 7d8406be pbrook
#define LSI_DCNTL_PFEN    0x20
119 7d8406be pbrook
#define LSI_DCNTL_PFF     0x40
120 7d8406be pbrook
#define LSI_DCNTL_CLSE    0x80
121 7d8406be pbrook
122 7d8406be pbrook
#define LSI_DMODE_MAN     0x01
123 7d8406be pbrook
#define LSI_DMODE_BOF     0x02
124 7d8406be pbrook
#define LSI_DMODE_ERMP    0x04
125 7d8406be pbrook
#define LSI_DMODE_ERL     0x08
126 7d8406be pbrook
#define LSI_DMODE_DIOM    0x10
127 7d8406be pbrook
#define LSI_DMODE_SIOM    0x20
128 7d8406be pbrook
129 7d8406be pbrook
#define LSI_CTEST2_DACK   0x01
130 7d8406be pbrook
#define LSI_CTEST2_DREQ   0x02
131 7d8406be pbrook
#define LSI_CTEST2_TEOP   0x04
132 7d8406be pbrook
#define LSI_CTEST2_PCICIE 0x08
133 7d8406be pbrook
#define LSI_CTEST2_CM     0x10
134 7d8406be pbrook
#define LSI_CTEST2_CIO    0x20
135 7d8406be pbrook
#define LSI_CTEST2_SIGP   0x40
136 7d8406be pbrook
#define LSI_CTEST2_DDIR   0x80
137 7d8406be pbrook
138 7d8406be pbrook
#define LSI_CTEST5_BL2    0x04
139 7d8406be pbrook
#define LSI_CTEST5_DDIR   0x08
140 7d8406be pbrook
#define LSI_CTEST5_MASR   0x10
141 7d8406be pbrook
#define LSI_CTEST5_DFSN   0x20
142 7d8406be pbrook
#define LSI_CTEST5_BBCK   0x40
143 7d8406be pbrook
#define LSI_CTEST5_ADCK   0x80
144 7d8406be pbrook
145 7d8406be pbrook
#define LSI_CCNTL0_DILS   0x01
146 7d8406be pbrook
#define LSI_CCNTL0_DISFC  0x10
147 7d8406be pbrook
#define LSI_CCNTL0_ENNDJ  0x20
148 7d8406be pbrook
#define LSI_CCNTL0_PMJCTL 0x40
149 7d8406be pbrook
#define LSI_CCNTL0_ENPMJ  0x80
150 7d8406be pbrook
151 b25cf589 aliguori
#define LSI_CCNTL1_EN64DBMV  0x01
152 b25cf589 aliguori
#define LSI_CCNTL1_EN64TIBMV 0x02
153 b25cf589 aliguori
#define LSI_CCNTL1_64TIMOD   0x04
154 b25cf589 aliguori
#define LSI_CCNTL1_DDAC      0x08
155 b25cf589 aliguori
#define LSI_CCNTL1_ZMOD      0x80
156 b25cf589 aliguori
157 e560125e Laszlo Ast
/* Enable Response to Reselection */
158 e560125e Laszlo Ast
#define LSI_SCID_RRE      0x60
159 e560125e Laszlo Ast
160 b25cf589 aliguori
#define LSI_CCNTL1_40BIT (LSI_CCNTL1_EN64TIBMV|LSI_CCNTL1_64TIMOD)
161 b25cf589 aliguori
162 7d8406be pbrook
#define PHASE_DO          0
163 7d8406be pbrook
#define PHASE_DI          1
164 7d8406be pbrook
#define PHASE_CMD         2
165 7d8406be pbrook
#define PHASE_ST          3
166 7d8406be pbrook
#define PHASE_MO          6
167 7d8406be pbrook
#define PHASE_MI          7
168 7d8406be pbrook
#define PHASE_MASK        7
169 7d8406be pbrook
170 a917d384 pbrook
/* Maximum length of MSG IN data.  */
171 a917d384 pbrook
#define LSI_MAX_MSGIN_LEN 8
172 a917d384 pbrook
173 a917d384 pbrook
/* Flag set if this is a tagged command.  */
174 a917d384 pbrook
#define LSI_TAG_VALID     (1 << 16)
175 a917d384 pbrook
176 042ec49d Gerd Hoffmann
typedef struct lsi_request {
177 5c6c0e51 Hannes Reinecke
    SCSIRequest *req;
178 a917d384 pbrook
    uint32_t tag;
179 b96a0da0 Gerd Hoffmann
    uint32_t dma_len;
180 b96a0da0 Gerd Hoffmann
    uint8_t *dma_buf;
181 a917d384 pbrook
    uint32_t pending;
182 a917d384 pbrook
    int out;
183 042ec49d Gerd Hoffmann
    QTAILQ_ENTRY(lsi_request) next;
184 042ec49d Gerd Hoffmann
} lsi_request;
185 4d611c9a pbrook
186 7d8406be pbrook
typedef struct {
187 f305261f Juan Quintela
    PCIDevice dev;
188 b0ce84e5 Avi Kivity
    MemoryRegion mmio_io;
189 b0ce84e5 Avi Kivity
    MemoryRegion ram_io;
190 b0ce84e5 Avi Kivity
    MemoryRegion io_io;
191 7d8406be pbrook
192 7d8406be pbrook
    int carry; /* ??? Should this be an a visible register somewhere?  */
193 2f172849 Hannes Reinecke
    int status;
194 a917d384 pbrook
    /* Action to take at the end of a MSG IN phase.
195 a15fdf86 Laszlo Ast
       0 = COMMAND, 1 = disconnect, 2 = DATA OUT, 3 = DATA IN.  */
196 a917d384 pbrook
    int msg_action;
197 a917d384 pbrook
    int msg_len;
198 a917d384 pbrook
    uint8_t msg[LSI_MAX_MSGIN_LEN];
199 4d611c9a pbrook
    /* 0 if SCRIPTS are running or stopped.
200 4d611c9a pbrook
     * 1 if a Wait Reselect instruction has been issued.
201 a917d384 pbrook
     * 2 if processing DMA from lsi_execute_script.
202 a917d384 pbrook
     * 3 if a DMA operation is in progress.  */
203 7d8406be pbrook
    int waiting;
204 ca9c39fa Gerd Hoffmann
    SCSIBus bus;
205 7d8406be pbrook
    int current_lun;
206 a917d384 pbrook
    /* The tag is a combination of the device ID and the SCSI tag.  */
207 af12ac98 Gerd Hoffmann
    uint32_t select_tag;
208 8ccc2ace ths
    int command_complete;
209 042ec49d Gerd Hoffmann
    QTAILQ_HEAD(, lsi_request) queue;
210 af12ac98 Gerd Hoffmann
    lsi_request *current;
211 7d8406be pbrook
212 7d8406be pbrook
    uint32_t dsa;
213 7d8406be pbrook
    uint32_t temp;
214 7d8406be pbrook
    uint32_t dnad;
215 7d8406be pbrook
    uint32_t dbc;
216 7d8406be pbrook
    uint8_t istat0;
217 7d8406be pbrook
    uint8_t istat1;
218 7d8406be pbrook
    uint8_t dcmd;
219 7d8406be pbrook
    uint8_t dstat;
220 7d8406be pbrook
    uint8_t dien;
221 7d8406be pbrook
    uint8_t sist0;
222 7d8406be pbrook
    uint8_t sist1;
223 7d8406be pbrook
    uint8_t sien0;
224 7d8406be pbrook
    uint8_t sien1;
225 7d8406be pbrook
    uint8_t mbox0;
226 7d8406be pbrook
    uint8_t mbox1;
227 7d8406be pbrook
    uint8_t dfifo;
228 9167a69a balrog
    uint8_t ctest2;
229 7d8406be pbrook
    uint8_t ctest3;
230 7d8406be pbrook
    uint8_t ctest4;
231 7d8406be pbrook
    uint8_t ctest5;
232 7d8406be pbrook
    uint8_t ccntl0;
233 7d8406be pbrook
    uint8_t ccntl1;
234 7d8406be pbrook
    uint32_t dsp;
235 7d8406be pbrook
    uint32_t dsps;
236 7d8406be pbrook
    uint8_t dmode;
237 7d8406be pbrook
    uint8_t dcntl;
238 7d8406be pbrook
    uint8_t scntl0;
239 7d8406be pbrook
    uint8_t scntl1;
240 7d8406be pbrook
    uint8_t scntl2;
241 7d8406be pbrook
    uint8_t scntl3;
242 7d8406be pbrook
    uint8_t sstat0;
243 7d8406be pbrook
    uint8_t sstat1;
244 7d8406be pbrook
    uint8_t scid;
245 7d8406be pbrook
    uint8_t sxfer;
246 7d8406be pbrook
    uint8_t socl;
247 7d8406be pbrook
    uint8_t sdid;
248 a917d384 pbrook
    uint8_t ssid;
249 7d8406be pbrook
    uint8_t sfbr;
250 7d8406be pbrook
    uint8_t stest1;
251 7d8406be pbrook
    uint8_t stest2;
252 7d8406be pbrook
    uint8_t stest3;
253 a917d384 pbrook
    uint8_t sidl;
254 7d8406be pbrook
    uint8_t stime0;
255 7d8406be pbrook
    uint8_t respid0;
256 7d8406be pbrook
    uint8_t respid1;
257 7d8406be pbrook
    uint32_t mmrs;
258 7d8406be pbrook
    uint32_t mmws;
259 7d8406be pbrook
    uint32_t sfs;
260 7d8406be pbrook
    uint32_t drs;
261 7d8406be pbrook
    uint32_t sbms;
262 ab57d967 aliguori
    uint32_t dbms;
263 7d8406be pbrook
    uint32_t dnad64;
264 7d8406be pbrook
    uint32_t pmjad1;
265 7d8406be pbrook
    uint32_t pmjad2;
266 7d8406be pbrook
    uint32_t rbc;
267 7d8406be pbrook
    uint32_t ua;
268 7d8406be pbrook
    uint32_t ia;
269 7d8406be pbrook
    uint32_t sbc;
270 7d8406be pbrook
    uint32_t csbc;
271 dcfb9014 ths
    uint32_t scratch[18]; /* SCRATCHA-SCRATCHR */
272 bd8ee11a Sebastian Herbszt
    uint8_t sbr;
273 7d8406be pbrook
274 7d8406be pbrook
    /* Script ram is stored as 32-bit words in host byteorder.  */
275 7d8406be pbrook
    uint32_t script_ram[2048];
276 7d8406be pbrook
} LSIState;
277 7d8406be pbrook
278 e560125e Laszlo Ast
static inline int lsi_irq_on_rsl(LSIState *s)
279 e560125e Laszlo Ast
{
280 e560125e Laszlo Ast
    return (s->sien0 & LSI_SIST0_RSL) && (s->scid & LSI_SCID_RRE);
281 e560125e Laszlo Ast
}
282 e560125e Laszlo Ast
283 7d8406be pbrook
static void lsi_soft_reset(LSIState *s)
284 7d8406be pbrook
{
285 51336214 Jan Kiszka
    lsi_request *p;
286 51336214 Jan Kiszka
287 7d8406be pbrook
    DPRINTF("Reset\n");
288 7d8406be pbrook
    s->carry = 0;
289 7d8406be pbrook
290 d43ba0af Jan Kiszka
    s->msg_action = 0;
291 d43ba0af Jan Kiszka
    s->msg_len = 0;
292 7d8406be pbrook
    s->waiting = 0;
293 7d8406be pbrook
    s->dsa = 0;
294 7d8406be pbrook
    s->dnad = 0;
295 7d8406be pbrook
    s->dbc = 0;
296 7d8406be pbrook
    s->temp = 0;
297 7d8406be pbrook
    memset(s->scratch, 0, sizeof(s->scratch));
298 7d8406be pbrook
    s->istat0 = 0;
299 7d8406be pbrook
    s->istat1 = 0;
300 12aa6dd6 Jan Kiszka
    s->dcmd = 0x40;
301 12aa6dd6 Jan Kiszka
    s->dstat = LSI_DSTAT_DFE;
302 7d8406be pbrook
    s->dien = 0;
303 7d8406be pbrook
    s->sist0 = 0;
304 7d8406be pbrook
    s->sist1 = 0;
305 7d8406be pbrook
    s->sien0 = 0;
306 7d8406be pbrook
    s->sien1 = 0;
307 7d8406be pbrook
    s->mbox0 = 0;
308 7d8406be pbrook
    s->mbox1 = 0;
309 7d8406be pbrook
    s->dfifo = 0;
310 12aa6dd6 Jan Kiszka
    s->ctest2 = LSI_CTEST2_DACK;
311 7d8406be pbrook
    s->ctest3 = 0;
312 7d8406be pbrook
    s->ctest4 = 0;
313 7d8406be pbrook
    s->ctest5 = 0;
314 7d8406be pbrook
    s->ccntl0 = 0;
315 7d8406be pbrook
    s->ccntl1 = 0;
316 7d8406be pbrook
    s->dsp = 0;
317 7d8406be pbrook
    s->dsps = 0;
318 7d8406be pbrook
    s->dmode = 0;
319 7d8406be pbrook
    s->dcntl = 0;
320 7d8406be pbrook
    s->scntl0 = 0xc0;
321 7d8406be pbrook
    s->scntl1 = 0;
322 7d8406be pbrook
    s->scntl2 = 0;
323 7d8406be pbrook
    s->scntl3 = 0;
324 7d8406be pbrook
    s->sstat0 = 0;
325 7d8406be pbrook
    s->sstat1 = 0;
326 7d8406be pbrook
    s->scid = 7;
327 7d8406be pbrook
    s->sxfer = 0;
328 7d8406be pbrook
    s->socl = 0;
329 12aa6dd6 Jan Kiszka
    s->sdid = 0;
330 12aa6dd6 Jan Kiszka
    s->ssid = 0;
331 7d8406be pbrook
    s->stest1 = 0;
332 7d8406be pbrook
    s->stest2 = 0;
333 7d8406be pbrook
    s->stest3 = 0;
334 a917d384 pbrook
    s->sidl = 0;
335 7d8406be pbrook
    s->stime0 = 0;
336 7d8406be pbrook
    s->respid0 = 0x80;
337 7d8406be pbrook
    s->respid1 = 0;
338 7d8406be pbrook
    s->mmrs = 0;
339 7d8406be pbrook
    s->mmws = 0;
340 7d8406be pbrook
    s->sfs = 0;
341 7d8406be pbrook
    s->drs = 0;
342 7d8406be pbrook
    s->sbms = 0;
343 ab57d967 aliguori
    s->dbms = 0;
344 7d8406be pbrook
    s->dnad64 = 0;
345 7d8406be pbrook
    s->pmjad1 = 0;
346 7d8406be pbrook
    s->pmjad2 = 0;
347 7d8406be pbrook
    s->rbc = 0;
348 7d8406be pbrook
    s->ua = 0;
349 7d8406be pbrook
    s->ia = 0;
350 7d8406be pbrook
    s->sbc = 0;
351 7d8406be pbrook
    s->csbc = 0;
352 bd8ee11a Sebastian Herbszt
    s->sbr = 0;
353 51336214 Jan Kiszka
    while (!QTAILQ_EMPTY(&s->queue)) {
354 51336214 Jan Kiszka
        p = QTAILQ_FIRST(&s->queue);
355 51336214 Jan Kiszka
        QTAILQ_REMOVE(&s->queue, p, next);
356 7267c094 Anthony Liguori
        g_free(p);
357 51336214 Jan Kiszka
    }
358 51336214 Jan Kiszka
    if (s->current) {
359 7267c094 Anthony Liguori
        g_free(s->current);
360 51336214 Jan Kiszka
        s->current = NULL;
361 51336214 Jan Kiszka
    }
362 7d8406be pbrook
}
363 7d8406be pbrook
364 b25cf589 aliguori
static int lsi_dma_40bit(LSIState *s)
365 b25cf589 aliguori
{
366 b25cf589 aliguori
    if ((s->ccntl1 & LSI_CCNTL1_40BIT) == LSI_CCNTL1_40BIT)
367 b25cf589 aliguori
        return 1;
368 b25cf589 aliguori
    return 0;
369 b25cf589 aliguori
}
370 b25cf589 aliguori
371 dd8edf01 aliguori
static int lsi_dma_ti64bit(LSIState *s)
372 dd8edf01 aliguori
{
373 dd8edf01 aliguori
    if ((s->ccntl1 & LSI_CCNTL1_EN64TIBMV) == LSI_CCNTL1_EN64TIBMV)
374 dd8edf01 aliguori
        return 1;
375 dd8edf01 aliguori
    return 0;
376 dd8edf01 aliguori
}
377 dd8edf01 aliguori
378 dd8edf01 aliguori
static int lsi_dma_64bit(LSIState *s)
379 dd8edf01 aliguori
{
380 dd8edf01 aliguori
    if ((s->ccntl1 & LSI_CCNTL1_EN64DBMV) == LSI_CCNTL1_EN64DBMV)
381 dd8edf01 aliguori
        return 1;
382 dd8edf01 aliguori
    return 0;
383 dd8edf01 aliguori
}
384 dd8edf01 aliguori
385 7d8406be pbrook
static uint8_t lsi_reg_readb(LSIState *s, int offset);
386 7d8406be pbrook
static void lsi_reg_writeb(LSIState *s, int offset, uint8_t val);
387 4d611c9a pbrook
static void lsi_execute_script(LSIState *s);
388 aa4d32c4 Gerd Hoffmann
static void lsi_reselect(LSIState *s, lsi_request *p);
389 7d8406be pbrook
390 7d8406be pbrook
static inline uint32_t read_dword(LSIState *s, uint32_t addr)
391 7d8406be pbrook
{
392 7d8406be pbrook
    uint32_t buf;
393 7d8406be pbrook
394 b0ce84e5 Avi Kivity
    /* XXX: an optimization here used to fast-path the read from scripts
395 b0ce84e5 Avi Kivity
     * memory.  But that bypasses any iommu.
396 b0ce84e5 Avi Kivity
     */
397 7d8406be pbrook
    cpu_physical_memory_read(addr, (uint8_t *)&buf, 4);
398 7d8406be pbrook
    return cpu_to_le32(buf);
399 7d8406be pbrook
}
400 7d8406be pbrook
401 7d8406be pbrook
static void lsi_stop_script(LSIState *s)
402 7d8406be pbrook
{
403 7d8406be pbrook
    s->istat1 &= ~LSI_ISTAT1_SRUN;
404 7d8406be pbrook
}
405 7d8406be pbrook
406 7d8406be pbrook
static void lsi_update_irq(LSIState *s)
407 7d8406be pbrook
{
408 7d8406be pbrook
    int level;
409 7d8406be pbrook
    static int last_level;
410 042ec49d Gerd Hoffmann
    lsi_request *p;
411 7d8406be pbrook
412 7d8406be pbrook
    /* It's unclear whether the DIP/SIP bits should be cleared when the
413 7d8406be pbrook
       Interrupt Status Registers are cleared or when istat0 is read.
414 7d8406be pbrook
       We currently do the formwer, which seems to work.  */
415 7d8406be pbrook
    level = 0;
416 7d8406be pbrook
    if (s->dstat) {
417 7d8406be pbrook
        if (s->dstat & s->dien)
418 7d8406be pbrook
            level = 1;
419 7d8406be pbrook
        s->istat0 |= LSI_ISTAT0_DIP;
420 7d8406be pbrook
    } else {
421 7d8406be pbrook
        s->istat0 &= ~LSI_ISTAT0_DIP;
422 7d8406be pbrook
    }
423 7d8406be pbrook
424 7d8406be pbrook
    if (s->sist0 || s->sist1) {
425 7d8406be pbrook
        if ((s->sist0 & s->sien0) || (s->sist1 & s->sien1))
426 7d8406be pbrook
            level = 1;
427 7d8406be pbrook
        s->istat0 |= LSI_ISTAT0_SIP;
428 7d8406be pbrook
    } else {
429 7d8406be pbrook
        s->istat0 &= ~LSI_ISTAT0_SIP;
430 7d8406be pbrook
    }
431 7d8406be pbrook
    if (s->istat0 & LSI_ISTAT0_INTF)
432 7d8406be pbrook
        level = 1;
433 7d8406be pbrook
434 7d8406be pbrook
    if (level != last_level) {
435 7d8406be pbrook
        DPRINTF("Update IRQ level %d dstat %02x sist %02x%02x\n",
436 7d8406be pbrook
                level, s->dstat, s->sist1, s->sist0);
437 7d8406be pbrook
        last_level = level;
438 7d8406be pbrook
    }
439 f305261f Juan Quintela
    qemu_set_irq(s->dev.irq[0], level);
440 e560125e Laszlo Ast
441 e560125e Laszlo Ast
    if (!level && lsi_irq_on_rsl(s) && !(s->scntl1 & LSI_SCNTL1_CON)) {
442 e560125e Laszlo Ast
        DPRINTF("Handled IRQs & disconnected, looking for pending "
443 e560125e Laszlo Ast
                "processes\n");
444 042ec49d Gerd Hoffmann
        QTAILQ_FOREACH(p, &s->queue, next) {
445 042ec49d Gerd Hoffmann
            if (p->pending) {
446 aa4d32c4 Gerd Hoffmann
                lsi_reselect(s, p);
447 e560125e Laszlo Ast
                break;
448 e560125e Laszlo Ast
            }
449 e560125e Laszlo Ast
        }
450 e560125e Laszlo Ast
    }
451 7d8406be pbrook
}
452 7d8406be pbrook
453 7d8406be pbrook
/* Stop SCRIPTS execution and raise a SCSI interrupt.  */
454 7d8406be pbrook
static void lsi_script_scsi_interrupt(LSIState *s, int stat0, int stat1)
455 7d8406be pbrook
{
456 7d8406be pbrook
    uint32_t mask0;
457 7d8406be pbrook
    uint32_t mask1;
458 7d8406be pbrook
459 7d8406be pbrook
    DPRINTF("SCSI Interrupt 0x%02x%02x prev 0x%02x%02x\n",
460 7d8406be pbrook
            stat1, stat0, s->sist1, s->sist0);
461 7d8406be pbrook
    s->sist0 |= stat0;
462 7d8406be pbrook
    s->sist1 |= stat1;
463 7d8406be pbrook
    /* Stop processor on fatal or unmasked interrupt.  As a special hack
464 7d8406be pbrook
       we don't stop processing when raising STO.  Instead continue
465 7d8406be pbrook
       execution and stop at the next insn that accesses the SCSI bus.  */
466 7d8406be pbrook
    mask0 = s->sien0 | ~(LSI_SIST0_CMP | LSI_SIST0_SEL | LSI_SIST0_RSL);
467 7d8406be pbrook
    mask1 = s->sien1 | ~(LSI_SIST1_GEN | LSI_SIST1_HTH);
468 7d8406be pbrook
    mask1 &= ~LSI_SIST1_STO;
469 7d8406be pbrook
    if (s->sist0 & mask0 || s->sist1 & mask1) {
470 7d8406be pbrook
        lsi_stop_script(s);
471 7d8406be pbrook
    }
472 7d8406be pbrook
    lsi_update_irq(s);
473 7d8406be pbrook
}
474 7d8406be pbrook
475 7d8406be pbrook
/* Stop SCRIPTS execution and raise a DMA interrupt.  */
476 7d8406be pbrook
static void lsi_script_dma_interrupt(LSIState *s, int stat)
477 7d8406be pbrook
{
478 7d8406be pbrook
    DPRINTF("DMA Interrupt 0x%x prev 0x%x\n", stat, s->dstat);
479 7d8406be pbrook
    s->dstat |= stat;
480 7d8406be pbrook
    lsi_update_irq(s);
481 7d8406be pbrook
    lsi_stop_script(s);
482 7d8406be pbrook
}
483 7d8406be pbrook
484 7d8406be pbrook
static inline void lsi_set_phase(LSIState *s, int phase)
485 7d8406be pbrook
{
486 7d8406be pbrook
    s->sstat1 = (s->sstat1 & ~PHASE_MASK) | phase;
487 7d8406be pbrook
}
488 7d8406be pbrook
489 7d8406be pbrook
static void lsi_bad_phase(LSIState *s, int out, int new_phase)
490 7d8406be pbrook
{
491 7d8406be pbrook
    /* Trigger a phase mismatch.  */
492 7d8406be pbrook
    if (s->ccntl0 & LSI_CCNTL0_ENPMJ) {
493 d1d74664 Paolo Bonzini
        if ((s->ccntl0 & LSI_CCNTL0_PMJCTL)) {
494 d1d74664 Paolo Bonzini
            s->dsp = out ? s->pmjad1 : s->pmjad2;
495 7d8406be pbrook
        } else {
496 d1d74664 Paolo Bonzini
            s->dsp = (s->scntl2 & LSI_SCNTL2_WSR ? s->pmjad2 : s->pmjad1);
497 7d8406be pbrook
        }
498 7d8406be pbrook
        DPRINTF("Data phase mismatch jump to %08x\n", s->dsp);
499 7d8406be pbrook
    } else {
500 7d8406be pbrook
        DPRINTF("Phase mismatch interrupt\n");
501 7d8406be pbrook
        lsi_script_scsi_interrupt(s, LSI_SIST0_MA, 0);
502 7d8406be pbrook
        lsi_stop_script(s);
503 7d8406be pbrook
    }
504 7d8406be pbrook
    lsi_set_phase(s, new_phase);
505 7d8406be pbrook
}
506 7d8406be pbrook
507 a917d384 pbrook
508 a917d384 pbrook
/* Resume SCRIPTS execution after a DMA operation.  */
509 a917d384 pbrook
static void lsi_resume_script(LSIState *s)
510 a917d384 pbrook
{
511 a917d384 pbrook
    if (s->waiting != 2) {
512 a917d384 pbrook
        s->waiting = 0;
513 a917d384 pbrook
        lsi_execute_script(s);
514 a917d384 pbrook
    } else {
515 a917d384 pbrook
        s->waiting = 0;
516 a917d384 pbrook
    }
517 a917d384 pbrook
}
518 a917d384 pbrook
519 64d56409 Jan Kiszka
static void lsi_disconnect(LSIState *s)
520 64d56409 Jan Kiszka
{
521 64d56409 Jan Kiszka
    s->scntl1 &= ~LSI_SCNTL1_CON;
522 64d56409 Jan Kiszka
    s->sstat1 &= ~PHASE_MASK;
523 64d56409 Jan Kiszka
}
524 64d56409 Jan Kiszka
525 64d56409 Jan Kiszka
static void lsi_bad_selection(LSIState *s, uint32_t id)
526 64d56409 Jan Kiszka
{
527 64d56409 Jan Kiszka
    DPRINTF("Selected absent target %d\n", id);
528 64d56409 Jan Kiszka
    lsi_script_scsi_interrupt(s, 0, LSI_SIST1_STO);
529 64d56409 Jan Kiszka
    lsi_disconnect(s);
530 64d56409 Jan Kiszka
}
531 64d56409 Jan Kiszka
532 4d611c9a pbrook
/* Initiate a SCSI layer data transfer.  */
533 7d8406be pbrook
static void lsi_do_dma(LSIState *s, int out)
534 7d8406be pbrook
{
535 64d56409 Jan Kiszka
    uint32_t count, id;
536 c227f099 Anthony Liguori
    target_phys_addr_t addr;
537 64d56409 Jan Kiszka
    SCSIDevice *dev;
538 7d8406be pbrook
539 b96a0da0 Gerd Hoffmann
    assert(s->current);
540 b96a0da0 Gerd Hoffmann
    if (!s->current->dma_len) {
541 a917d384 pbrook
        /* Wait until data is available.  */
542 a917d384 pbrook
        DPRINTF("DMA no data available\n");
543 a917d384 pbrook
        return;
544 7d8406be pbrook
    }
545 7d8406be pbrook
546 259d5577 Jan Kiszka
    id = (s->current->tag >> 8) & 0xf;
547 64d56409 Jan Kiszka
    dev = s->bus.devs[id];
548 64d56409 Jan Kiszka
    if (!dev) {
549 64d56409 Jan Kiszka
        lsi_bad_selection(s, id);
550 64d56409 Jan Kiszka
        return;
551 64d56409 Jan Kiszka
    }
552 64d56409 Jan Kiszka
553 a917d384 pbrook
    count = s->dbc;
554 b96a0da0 Gerd Hoffmann
    if (count > s->current->dma_len)
555 b96a0da0 Gerd Hoffmann
        count = s->current->dma_len;
556 a917d384 pbrook
557 a917d384 pbrook
    addr = s->dnad;
558 dd8edf01 aliguori
    /* both 40 and Table Indirect 64-bit DMAs store upper bits in dnad64 */
559 dd8edf01 aliguori
    if (lsi_dma_40bit(s) || lsi_dma_ti64bit(s))
560 b25cf589 aliguori
        addr |= ((uint64_t)s->dnad64 << 32);
561 dd8edf01 aliguori
    else if (s->dbms)
562 dd8edf01 aliguori
        addr |= ((uint64_t)s->dbms << 32);
563 b25cf589 aliguori
    else if (s->sbms)
564 b25cf589 aliguori
        addr |= ((uint64_t)s->sbms << 32);
565 b25cf589 aliguori
566 3adae656 aliguori
    DPRINTF("DMA addr=0x" TARGET_FMT_plx " len=%d\n", addr, count);
567 7d8406be pbrook
    s->csbc += count;
568 a917d384 pbrook
    s->dnad += count;
569 a917d384 pbrook
    s->dbc -= count;
570 5c6c0e51 Hannes Reinecke
     if (s->current->dma_buf == NULL) {
571 0c34459b Paolo Bonzini
        s->current->dma_buf = scsi_req_get_buf(s->current->req);
572 a917d384 pbrook
    }
573 7d8406be pbrook
    /* ??? Set SFBR to first data byte.  */
574 a917d384 pbrook
    if (out) {
575 b96a0da0 Gerd Hoffmann
        cpu_physical_memory_read(addr, s->current->dma_buf, count);
576 a917d384 pbrook
    } else {
577 b96a0da0 Gerd Hoffmann
        cpu_physical_memory_write(addr, s->current->dma_buf, count);
578 a917d384 pbrook
    }
579 b96a0da0 Gerd Hoffmann
    s->current->dma_len -= count;
580 b96a0da0 Gerd Hoffmann
    if (s->current->dma_len == 0) {
581 b96a0da0 Gerd Hoffmann
        s->current->dma_buf = NULL;
582 ad3376cc Paolo Bonzini
        scsi_req_continue(s->current->req);
583 a917d384 pbrook
    } else {
584 b96a0da0 Gerd Hoffmann
        s->current->dma_buf += count;
585 a917d384 pbrook
        lsi_resume_script(s);
586 a917d384 pbrook
    }
587 a917d384 pbrook
}
588 a917d384 pbrook
589 a917d384 pbrook
590 a917d384 pbrook
/* Add a command to the queue.  */
591 a917d384 pbrook
static void lsi_queue_command(LSIState *s)
592 a917d384 pbrook
{
593 af12ac98 Gerd Hoffmann
    lsi_request *p = s->current;
594 a917d384 pbrook
595 aa2b1e89 Bernhard Kohl
    DPRINTF("Queueing tag=0x%x\n", p->tag);
596 af12ac98 Gerd Hoffmann
    assert(s->current != NULL);
597 b96a0da0 Gerd Hoffmann
    assert(s->current->dma_len == 0);
598 af12ac98 Gerd Hoffmann
    QTAILQ_INSERT_TAIL(&s->queue, s->current, next);
599 af12ac98 Gerd Hoffmann
    s->current = NULL;
600 af12ac98 Gerd Hoffmann
601 a917d384 pbrook
    p->pending = 0;
602 a917d384 pbrook
    p->out = (s->sstat1 & PHASE_MASK) == PHASE_DO;
603 a917d384 pbrook
}
604 a917d384 pbrook
605 a917d384 pbrook
/* Queue a byte for a MSG IN phase.  */
606 a917d384 pbrook
static void lsi_add_msg_byte(LSIState *s, uint8_t data)
607 a917d384 pbrook
{
608 a917d384 pbrook
    if (s->msg_len >= LSI_MAX_MSGIN_LEN) {
609 a917d384 pbrook
        BADF("MSG IN data too long\n");
610 4d611c9a pbrook
    } else {
611 a917d384 pbrook
        DPRINTF("MSG IN 0x%02x\n", data);
612 a917d384 pbrook
        s->msg[s->msg_len++] = data;
613 7d8406be pbrook
    }
614 a917d384 pbrook
}
615 a917d384 pbrook
616 a917d384 pbrook
/* Perform reselection to continue a command.  */
617 aa4d32c4 Gerd Hoffmann
static void lsi_reselect(LSIState *s, lsi_request *p)
618 a917d384 pbrook
{
619 a917d384 pbrook
    int id;
620 a917d384 pbrook
621 af12ac98 Gerd Hoffmann
    assert(s->current == NULL);
622 af12ac98 Gerd Hoffmann
    QTAILQ_REMOVE(&s->queue, p, next);
623 af12ac98 Gerd Hoffmann
    s->current = p;
624 af12ac98 Gerd Hoffmann
625 aa4d32c4 Gerd Hoffmann
    id = (p->tag >> 8) & 0xf;
626 a917d384 pbrook
    s->ssid = id | 0x80;
627 cc9f28bc Laszlo Ast
    /* LSI53C700 Family Compatibility, see LSI53C895A 4-73 */
628 f6dc18df Blue Swirl
    if (!(s->dcntl & LSI_DCNTL_COM)) {
629 cc9f28bc Laszlo Ast
        s->sfbr = 1 << (id & 0x7);
630 cc9f28bc Laszlo Ast
    }
631 a917d384 pbrook
    DPRINTF("Reselected target %d\n", id);
632 a917d384 pbrook
    s->scntl1 |= LSI_SCNTL1_CON;
633 a917d384 pbrook
    lsi_set_phase(s, PHASE_MI);
634 a917d384 pbrook
    s->msg_action = p->out ? 2 : 3;
635 b96a0da0 Gerd Hoffmann
    s->current->dma_len = p->pending;
636 a917d384 pbrook
    lsi_add_msg_byte(s, 0x80);
637 af12ac98 Gerd Hoffmann
    if (s->current->tag & LSI_TAG_VALID) {
638 a917d384 pbrook
        lsi_add_msg_byte(s, 0x20);
639 aa4d32c4 Gerd Hoffmann
        lsi_add_msg_byte(s, p->tag & 0xff);
640 a917d384 pbrook
    }
641 a917d384 pbrook
642 e560125e Laszlo Ast
    if (lsi_irq_on_rsl(s)) {
643 e560125e Laszlo Ast
        lsi_script_scsi_interrupt(s, LSI_SIST0_RSL, 0);
644 e560125e Laszlo Ast
    }
645 a917d384 pbrook
}
646 a917d384 pbrook
647 11257187 Paolo Bonzini
static lsi_request *lsi_find_by_tag(LSIState *s, uint32_t tag)
648 a917d384 pbrook
{
649 042ec49d Gerd Hoffmann
    lsi_request *p;
650 042ec49d Gerd Hoffmann
651 042ec49d Gerd Hoffmann
    QTAILQ_FOREACH(p, &s->queue, next) {
652 a917d384 pbrook
        if (p->tag == tag) {
653 11257187 Paolo Bonzini
            return p;
654 a917d384 pbrook
        }
655 a917d384 pbrook
    }
656 11257187 Paolo Bonzini
657 11257187 Paolo Bonzini
    return NULL;
658 11257187 Paolo Bonzini
}
659 11257187 Paolo Bonzini
660 94d3f98a Paolo Bonzini
static void lsi_request_cancelled(SCSIRequest *req)
661 94d3f98a Paolo Bonzini
{
662 94d3f98a Paolo Bonzini
    LSIState *s = DO_UPCAST(LSIState, dev.qdev, req->bus->qbus.parent);
663 c5bf71a9 Hannes Reinecke
    lsi_request *p = req->hba_private;
664 94d3f98a Paolo Bonzini
665 94d3f98a Paolo Bonzini
    if (s->current && req == s->current->req) {
666 94d3f98a Paolo Bonzini
        scsi_req_unref(req);
667 7267c094 Anthony Liguori
        g_free(s->current);
668 94d3f98a Paolo Bonzini
        s->current = NULL;
669 94d3f98a Paolo Bonzini
        return;
670 94d3f98a Paolo Bonzini
    }
671 94d3f98a Paolo Bonzini
672 94d3f98a Paolo Bonzini
    if (p) {
673 94d3f98a Paolo Bonzini
        QTAILQ_REMOVE(&s->queue, p, next);
674 94d3f98a Paolo Bonzini
        scsi_req_unref(req);
675 7267c094 Anthony Liguori
        g_free(p);
676 94d3f98a Paolo Bonzini
    }
677 94d3f98a Paolo Bonzini
}
678 94d3f98a Paolo Bonzini
679 11257187 Paolo Bonzini
/* Record that data is available for a queued command.  Returns zero if
680 11257187 Paolo Bonzini
   the device was reselected, nonzero if the IO is deferred.  */
681 c5bf71a9 Hannes Reinecke
static int lsi_queue_req(LSIState *s, SCSIRequest *req, uint32_t len)
682 11257187 Paolo Bonzini
{
683 c5bf71a9 Hannes Reinecke
    lsi_request *p = req->hba_private;
684 11257187 Paolo Bonzini
685 11257187 Paolo Bonzini
    if (p->pending) {
686 c5bf71a9 Hannes Reinecke
        BADF("Multiple IO pending for request %p\n", p);
687 11257187 Paolo Bonzini
    }
688 aba1f023 Paolo Bonzini
    p->pending = len;
689 11257187 Paolo Bonzini
    /* Reselect if waiting for it, or if reselection triggers an IRQ
690 11257187 Paolo Bonzini
       and the bus is free.
691 11257187 Paolo Bonzini
       Since no interrupt stacking is implemented in the emulation, it
692 11257187 Paolo Bonzini
       is also required that there are no pending interrupts waiting
693 11257187 Paolo Bonzini
       for service from the device driver. */
694 11257187 Paolo Bonzini
    if (s->waiting == 1 ||
695 11257187 Paolo Bonzini
        (lsi_irq_on_rsl(s) && !(s->scntl1 & LSI_SCNTL1_CON) &&
696 11257187 Paolo Bonzini
         !(s->istat0 & (LSI_ISTAT0_SIP | LSI_ISTAT0_DIP)))) {
697 11257187 Paolo Bonzini
        /* Reselect device.  */
698 11257187 Paolo Bonzini
        lsi_reselect(s, p);
699 11257187 Paolo Bonzini
        return 0;
700 11257187 Paolo Bonzini
    } else {
701 11257187 Paolo Bonzini
        DPRINTF("Queueing IO tag=0x%x\n", tag);
702 aba1f023 Paolo Bonzini
        p->pending = len;
703 11257187 Paolo Bonzini
        return 1;
704 11257187 Paolo Bonzini
    }
705 7d8406be pbrook
}
706 c6df7102 Paolo Bonzini
707 c6df7102 Paolo Bonzini
 /* Callback to indicate that the SCSI layer has completed a command.  */
708 aba1f023 Paolo Bonzini
static void lsi_command_complete(SCSIRequest *req, uint32_t status)
709 4d611c9a pbrook
{
710 5c6c0e51 Hannes Reinecke
    LSIState *s = DO_UPCAST(LSIState, dev.qdev, req->bus->qbus.parent);
711 4d611c9a pbrook
    int out;
712 4d611c9a pbrook
713 a917d384 pbrook
    out = (s->sstat1 & PHASE_MASK) == PHASE_DO;
714 aba1f023 Paolo Bonzini
    DPRINTF("Command complete status=%d\n", (int)status);
715 aba1f023 Paolo Bonzini
    s->status = status;
716 c6df7102 Paolo Bonzini
    s->command_complete = 2;
717 c6df7102 Paolo Bonzini
    if (s->waiting && s->dbc != 0) {
718 c6df7102 Paolo Bonzini
        /* Raise phase mismatch for short transfers.  */
719 c6df7102 Paolo Bonzini
        lsi_bad_phase(s, out, PHASE_ST);
720 c6df7102 Paolo Bonzini
    } else {
721 c6df7102 Paolo Bonzini
        lsi_set_phase(s, PHASE_ST);
722 c6df7102 Paolo Bonzini
    }
723 af12ac98 Gerd Hoffmann
724 c6df7102 Paolo Bonzini
    if (s->current && req == s->current->req) {
725 c6df7102 Paolo Bonzini
        scsi_req_unref(s->current->req);
726 7267c094 Anthony Liguori
        g_free(s->current);
727 c6df7102 Paolo Bonzini
        s->current = NULL;
728 4d611c9a pbrook
    }
729 c6df7102 Paolo Bonzini
    lsi_resume_script(s);
730 c6df7102 Paolo Bonzini
}
731 c6df7102 Paolo Bonzini
732 c6df7102 Paolo Bonzini
 /* Callback to indicate that the SCSI layer has completed a transfer.  */
733 aba1f023 Paolo Bonzini
static void lsi_transfer_data(SCSIRequest *req, uint32_t len)
734 c6df7102 Paolo Bonzini
{
735 c6df7102 Paolo Bonzini
    LSIState *s = DO_UPCAST(LSIState, dev.qdev, req->bus->qbus.parent);
736 c6df7102 Paolo Bonzini
    int out;
737 4d611c9a pbrook
738 c5bf71a9 Hannes Reinecke
    if (s->waiting == 1 || !s->current || req->hba_private != s->current ||
739 e560125e Laszlo Ast
        (lsi_irq_on_rsl(s) && !(s->scntl1 & LSI_SCNTL1_CON))) {
740 c5bf71a9 Hannes Reinecke
        if (lsi_queue_req(s, req, len)) {
741 a917d384 pbrook
            return;
742 5c6c0e51 Hannes Reinecke
        }
743 a917d384 pbrook
    }
744 e560125e Laszlo Ast
745 c6df7102 Paolo Bonzini
    out = (s->sstat1 & PHASE_MASK) == PHASE_DO;
746 c6df7102 Paolo Bonzini
747 e560125e Laszlo Ast
    /* host adapter (re)connected */
748 aba1f023 Paolo Bonzini
    DPRINTF("Data ready tag=0x%x len=%d\n", req->tag, len);
749 aba1f023 Paolo Bonzini
    s->current->dma_len = len;
750 8ccc2ace ths
    s->command_complete = 1;
751 c6df7102 Paolo Bonzini
    if (s->waiting) {
752 c6df7102 Paolo Bonzini
        if (s->waiting == 1 || s->dbc == 0) {
753 c6df7102 Paolo Bonzini
            lsi_resume_script(s);
754 c6df7102 Paolo Bonzini
        } else {
755 c6df7102 Paolo Bonzini
            lsi_do_dma(s, out);
756 c6df7102 Paolo Bonzini
        }
757 4d611c9a pbrook
    }
758 4d611c9a pbrook
}
759 7d8406be pbrook
760 7d8406be pbrook
static void lsi_do_command(LSIState *s)
761 7d8406be pbrook
{
762 64d56409 Jan Kiszka
    SCSIDevice *dev;
763 7d8406be pbrook
    uint8_t buf[16];
764 64d56409 Jan Kiszka
    uint32_t id;
765 7d8406be pbrook
    int n;
766 7d8406be pbrook
767 7d8406be pbrook
    DPRINTF("Send command len=%d\n", s->dbc);
768 7d8406be pbrook
    if (s->dbc > 16)
769 7d8406be pbrook
        s->dbc = 16;
770 7d8406be pbrook
    cpu_physical_memory_read(s->dnad, buf, s->dbc);
771 7d8406be pbrook
    s->sfbr = buf[0];
772 8ccc2ace ths
    s->command_complete = 0;
773 af12ac98 Gerd Hoffmann
774 259d5577 Jan Kiszka
    id = (s->select_tag >> 8) & 0xf;
775 64d56409 Jan Kiszka
    dev = s->bus.devs[id];
776 64d56409 Jan Kiszka
    if (!dev) {
777 64d56409 Jan Kiszka
        lsi_bad_selection(s, id);
778 64d56409 Jan Kiszka
        return;
779 64d56409 Jan Kiszka
    }
780 64d56409 Jan Kiszka
781 af12ac98 Gerd Hoffmann
    assert(s->current == NULL);
782 7267c094 Anthony Liguori
    s->current = g_malloc0(sizeof(lsi_request));
783 af12ac98 Gerd Hoffmann
    s->current->tag = s->select_tag;
784 c39ce112 Paolo Bonzini
    s->current->req = scsi_req_new(dev, s->current->tag, s->current_lun, buf,
785 c5bf71a9 Hannes Reinecke
                                   s->current);
786 af12ac98 Gerd Hoffmann
787 c39ce112 Paolo Bonzini
    n = scsi_req_enqueue(s->current->req);
788 ad3376cc Paolo Bonzini
    if (n) {
789 ad3376cc Paolo Bonzini
        if (n > 0) {
790 ad3376cc Paolo Bonzini
            lsi_set_phase(s, PHASE_DI);
791 ad3376cc Paolo Bonzini
        } else if (n < 0) {
792 ad3376cc Paolo Bonzini
            lsi_set_phase(s, PHASE_DO);
793 ad3376cc Paolo Bonzini
        }
794 ad3376cc Paolo Bonzini
        scsi_req_continue(s->current->req);
795 a917d384 pbrook
    }
796 8ccc2ace ths
    if (!s->command_complete) {
797 8ccc2ace ths
        if (n) {
798 8ccc2ace ths
            /* Command did not complete immediately so disconnect.  */
799 8ccc2ace ths
            lsi_add_msg_byte(s, 2); /* SAVE DATA POINTER */
800 8ccc2ace ths
            lsi_add_msg_byte(s, 4); /* DISCONNECT */
801 8ccc2ace ths
            /* wait data */
802 8ccc2ace ths
            lsi_set_phase(s, PHASE_MI);
803 8ccc2ace ths
            s->msg_action = 1;
804 8ccc2ace ths
            lsi_queue_command(s);
805 8ccc2ace ths
        } else {
806 8ccc2ace ths
            /* wait command complete */
807 8ccc2ace ths
            lsi_set_phase(s, PHASE_DI);
808 8ccc2ace ths
        }
809 7d8406be pbrook
    }
810 7d8406be pbrook
}
811 7d8406be pbrook
812 7d8406be pbrook
static void lsi_do_status(LSIState *s)
813 7d8406be pbrook
{
814 2f172849 Hannes Reinecke
    uint8_t status;
815 2f172849 Hannes Reinecke
    DPRINTF("Get status len=%d status=%d\n", s->dbc, s->status);
816 7d8406be pbrook
    if (s->dbc != 1)
817 7d8406be pbrook
        BADF("Bad Status move\n");
818 7d8406be pbrook
    s->dbc = 1;
819 2f172849 Hannes Reinecke
    status = s->status;
820 2f172849 Hannes Reinecke
    s->sfbr = status;
821 2f172849 Hannes Reinecke
    cpu_physical_memory_write(s->dnad, &status, 1);
822 7d8406be pbrook
    lsi_set_phase(s, PHASE_MI);
823 a917d384 pbrook
    s->msg_action = 1;
824 a917d384 pbrook
    lsi_add_msg_byte(s, 0); /* COMMAND COMPLETE */
825 7d8406be pbrook
}
826 7d8406be pbrook
827 7d8406be pbrook
static void lsi_do_msgin(LSIState *s)
828 7d8406be pbrook
{
829 a917d384 pbrook
    int len;
830 a917d384 pbrook
    DPRINTF("Message in len=%d/%d\n", s->dbc, s->msg_len);
831 a917d384 pbrook
    s->sfbr = s->msg[0];
832 a917d384 pbrook
    len = s->msg_len;
833 a917d384 pbrook
    if (len > s->dbc)
834 a917d384 pbrook
        len = s->dbc;
835 a917d384 pbrook
    cpu_physical_memory_write(s->dnad, s->msg, len);
836 a917d384 pbrook
    /* Linux drivers rely on the last byte being in the SIDL.  */
837 a917d384 pbrook
    s->sidl = s->msg[len - 1];
838 a917d384 pbrook
    s->msg_len -= len;
839 a917d384 pbrook
    if (s->msg_len) {
840 a917d384 pbrook
        memmove(s->msg, s->msg + len, s->msg_len);
841 7d8406be pbrook
    } else {
842 7d8406be pbrook
        /* ??? Check if ATN (not yet implemented) is asserted and maybe
843 7d8406be pbrook
           switch to PHASE_MO.  */
844 a917d384 pbrook
        switch (s->msg_action) {
845 a917d384 pbrook
        case 0:
846 a917d384 pbrook
            lsi_set_phase(s, PHASE_CMD);
847 a917d384 pbrook
            break;
848 a917d384 pbrook
        case 1:
849 a917d384 pbrook
            lsi_disconnect(s);
850 a917d384 pbrook
            break;
851 a917d384 pbrook
        case 2:
852 a917d384 pbrook
            lsi_set_phase(s, PHASE_DO);
853 a917d384 pbrook
            break;
854 a917d384 pbrook
        case 3:
855 a917d384 pbrook
            lsi_set_phase(s, PHASE_DI);
856 a917d384 pbrook
            break;
857 a917d384 pbrook
        default:
858 a917d384 pbrook
            abort();
859 a917d384 pbrook
        }
860 7d8406be pbrook
    }
861 7d8406be pbrook
}
862 7d8406be pbrook
863 a917d384 pbrook
/* Read the next byte during a MSGOUT phase.  */
864 a917d384 pbrook
static uint8_t lsi_get_msgbyte(LSIState *s)
865 a917d384 pbrook
{
866 a917d384 pbrook
    uint8_t data;
867 a917d384 pbrook
    cpu_physical_memory_read(s->dnad, &data, 1);
868 a917d384 pbrook
    s->dnad++;
869 a917d384 pbrook
    s->dbc--;
870 a917d384 pbrook
    return data;
871 a917d384 pbrook
}
872 a917d384 pbrook
873 444dd39b Stefan Hajnoczi
/* Skip the next n bytes during a MSGOUT phase. */
874 444dd39b Stefan Hajnoczi
static void lsi_skip_msgbytes(LSIState *s, unsigned int n)
875 444dd39b Stefan Hajnoczi
{
876 444dd39b Stefan Hajnoczi
    s->dnad += n;
877 444dd39b Stefan Hajnoczi
    s->dbc  -= n;
878 444dd39b Stefan Hajnoczi
}
879 444dd39b Stefan Hajnoczi
880 7d8406be pbrook
static void lsi_do_msgout(LSIState *s)
881 7d8406be pbrook
{
882 7d8406be pbrook
    uint8_t msg;
883 a917d384 pbrook
    int len;
884 508240c0 Bernhard Kohl
    uint32_t current_tag;
885 5c6c0e51 Hannes Reinecke
    lsi_request *current_req, *p, *p_next;
886 508240c0 Bernhard Kohl
    int id;
887 508240c0 Bernhard Kohl
888 508240c0 Bernhard Kohl
    if (s->current) {
889 508240c0 Bernhard Kohl
        current_tag = s->current->tag;
890 5c6c0e51 Hannes Reinecke
        current_req = s->current;
891 508240c0 Bernhard Kohl
    } else {
892 508240c0 Bernhard Kohl
        current_tag = s->select_tag;
893 5c6c0e51 Hannes Reinecke
        current_req = lsi_find_by_tag(s, current_tag);
894 508240c0 Bernhard Kohl
    }
895 508240c0 Bernhard Kohl
    id = (current_tag >> 8) & 0xf;
896 7d8406be pbrook
897 7d8406be pbrook
    DPRINTF("MSG out len=%d\n", s->dbc);
898 a917d384 pbrook
    while (s->dbc) {
899 a917d384 pbrook
        msg = lsi_get_msgbyte(s);
900 a917d384 pbrook
        s->sfbr = msg;
901 a917d384 pbrook
902 a917d384 pbrook
        switch (msg) {
903 77203ea0 Laszlo Ast
        case 0x04:
904 a917d384 pbrook
            DPRINTF("MSG: Disconnect\n");
905 a917d384 pbrook
            lsi_disconnect(s);
906 a917d384 pbrook
            break;
907 a917d384 pbrook
        case 0x08:
908 a917d384 pbrook
            DPRINTF("MSG: No Operation\n");
909 a917d384 pbrook
            lsi_set_phase(s, PHASE_CMD);
910 a917d384 pbrook
            break;
911 a917d384 pbrook
        case 0x01:
912 a917d384 pbrook
            len = lsi_get_msgbyte(s);
913 a917d384 pbrook
            msg = lsi_get_msgbyte(s);
914 f3f5b867 Blue Swirl
            (void)len; /* avoid a warning about unused variable*/
915 a917d384 pbrook
            DPRINTF("Extended message 0x%x (len %d)\n", msg, len);
916 a917d384 pbrook
            switch (msg) {
917 a917d384 pbrook
            case 1:
918 a917d384 pbrook
                DPRINTF("SDTR (ignored)\n");
919 444dd39b Stefan Hajnoczi
                lsi_skip_msgbytes(s, 2);
920 a917d384 pbrook
                break;
921 a917d384 pbrook
            case 3:
922 a917d384 pbrook
                DPRINTF("WDTR (ignored)\n");
923 444dd39b Stefan Hajnoczi
                lsi_skip_msgbytes(s, 1);
924 a917d384 pbrook
                break;
925 a917d384 pbrook
            default:
926 a917d384 pbrook
                goto bad;
927 a917d384 pbrook
            }
928 a917d384 pbrook
            break;
929 a917d384 pbrook
        case 0x20: /* SIMPLE queue */
930 af12ac98 Gerd Hoffmann
            s->select_tag |= lsi_get_msgbyte(s) | LSI_TAG_VALID;
931 aa2b1e89 Bernhard Kohl
            DPRINTF("SIMPLE queue tag=0x%x\n", s->select_tag & 0xff);
932 a917d384 pbrook
            break;
933 a917d384 pbrook
        case 0x21: /* HEAD of queue */
934 a917d384 pbrook
            BADF("HEAD queue not implemented\n");
935 af12ac98 Gerd Hoffmann
            s->select_tag |= lsi_get_msgbyte(s) | LSI_TAG_VALID;
936 a917d384 pbrook
            break;
937 a917d384 pbrook
        case 0x22: /* ORDERED queue */
938 a917d384 pbrook
            BADF("ORDERED queue not implemented\n");
939 af12ac98 Gerd Hoffmann
            s->select_tag |= lsi_get_msgbyte(s) | LSI_TAG_VALID;
940 a917d384 pbrook
            break;
941 508240c0 Bernhard Kohl
        case 0x0d:
942 508240c0 Bernhard Kohl
            /* The ABORT TAG message clears the current I/O process only. */
943 508240c0 Bernhard Kohl
            DPRINTF("MSG: ABORT TAG tag=0x%x\n", current_tag);
944 5c6c0e51 Hannes Reinecke
            if (current_req) {
945 94d3f98a Paolo Bonzini
                scsi_req_cancel(current_req->req);
946 5c6c0e51 Hannes Reinecke
            }
947 508240c0 Bernhard Kohl
            lsi_disconnect(s);
948 508240c0 Bernhard Kohl
            break;
949 508240c0 Bernhard Kohl
        case 0x06:
950 508240c0 Bernhard Kohl
        case 0x0e:
951 508240c0 Bernhard Kohl
        case 0x0c:
952 508240c0 Bernhard Kohl
            /* The ABORT message clears all I/O processes for the selecting
953 508240c0 Bernhard Kohl
               initiator on the specified logical unit of the target. */
954 508240c0 Bernhard Kohl
            if (msg == 0x06) {
955 508240c0 Bernhard Kohl
                DPRINTF("MSG: ABORT tag=0x%x\n", current_tag);
956 508240c0 Bernhard Kohl
            }
957 508240c0 Bernhard Kohl
            /* The CLEAR QUEUE message clears all I/O processes for all
958 508240c0 Bernhard Kohl
               initiators on the specified logical unit of the target. */
959 508240c0 Bernhard Kohl
            if (msg == 0x0e) {
960 508240c0 Bernhard Kohl
                DPRINTF("MSG: CLEAR QUEUE tag=0x%x\n", current_tag);
961 508240c0 Bernhard Kohl
            }
962 508240c0 Bernhard Kohl
            /* The BUS DEVICE RESET message clears all I/O processes for all
963 508240c0 Bernhard Kohl
               initiators on all logical units of the target. */
964 508240c0 Bernhard Kohl
            if (msg == 0x0c) {
965 508240c0 Bernhard Kohl
                DPRINTF("MSG: BUS DEVICE RESET tag=0x%x\n", current_tag);
966 508240c0 Bernhard Kohl
            }
967 508240c0 Bernhard Kohl
968 508240c0 Bernhard Kohl
            /* clear the current I/O process */
969 5c6c0e51 Hannes Reinecke
            if (s->current) {
970 94d3f98a Paolo Bonzini
                scsi_req_cancel(s->current->req);
971 5c6c0e51 Hannes Reinecke
            }
972 508240c0 Bernhard Kohl
973 508240c0 Bernhard Kohl
            /* As the current implemented devices scsi_disk and scsi_generic
974 508240c0 Bernhard Kohl
               only support one LUN, we don't need to keep track of LUNs.
975 508240c0 Bernhard Kohl
               Clearing I/O processes for other initiators could be possible
976 508240c0 Bernhard Kohl
               for scsi_generic by sending a SG_SCSI_RESET to the /dev/sgX
977 508240c0 Bernhard Kohl
               device, but this is currently not implemented (and seems not
978 508240c0 Bernhard Kohl
               to be really necessary). So let's simply clear all queued
979 508240c0 Bernhard Kohl
               commands for the current device: */
980 508240c0 Bernhard Kohl
            id = current_tag & 0x0000ff00;
981 508240c0 Bernhard Kohl
            QTAILQ_FOREACH_SAFE(p, &s->queue, next, p_next) {
982 508240c0 Bernhard Kohl
                if ((p->tag & 0x0000ff00) == id) {
983 94d3f98a Paolo Bonzini
                    scsi_req_cancel(p->req);
984 508240c0 Bernhard Kohl
                }
985 508240c0 Bernhard Kohl
            }
986 508240c0 Bernhard Kohl
987 508240c0 Bernhard Kohl
            lsi_disconnect(s);
988 508240c0 Bernhard Kohl
            break;
989 a917d384 pbrook
        default:
990 a917d384 pbrook
            if ((msg & 0x80) == 0) {
991 a917d384 pbrook
                goto bad;
992 a917d384 pbrook
            }
993 a917d384 pbrook
            s->current_lun = msg & 7;
994 a917d384 pbrook
            DPRINTF("Select LUN %d\n", s->current_lun);
995 a917d384 pbrook
            lsi_set_phase(s, PHASE_CMD);
996 a917d384 pbrook
            break;
997 a917d384 pbrook
        }
998 7d8406be pbrook
    }
999 a917d384 pbrook
    return;
1000 a917d384 pbrook
bad:
1001 a917d384 pbrook
    BADF("Unimplemented message 0x%02x\n", msg);
1002 a917d384 pbrook
    lsi_set_phase(s, PHASE_MI);
1003 a917d384 pbrook
    lsi_add_msg_byte(s, 7); /* MESSAGE REJECT */
1004 a917d384 pbrook
    s->msg_action = 0;
1005 7d8406be pbrook
}
1006 7d8406be pbrook
1007 7d8406be pbrook
/* Sign extend a 24-bit value.  */
1008 7d8406be pbrook
static inline int32_t sxt24(int32_t n)
1009 7d8406be pbrook
{
1010 7d8406be pbrook
    return (n << 8) >> 8;
1011 7d8406be pbrook
}
1012 7d8406be pbrook
1013 e20a8dff Blue Swirl
#define LSI_BUF_SIZE 4096
1014 7d8406be pbrook
static void lsi_memcpy(LSIState *s, uint32_t dest, uint32_t src, int count)
1015 7d8406be pbrook
{
1016 7d8406be pbrook
    int n;
1017 e20a8dff Blue Swirl
    uint8_t buf[LSI_BUF_SIZE];
1018 7d8406be pbrook
1019 7d8406be pbrook
    DPRINTF("memcpy dest 0x%08x src 0x%08x count %d\n", dest, src, count);
1020 7d8406be pbrook
    while (count) {
1021 e20a8dff Blue Swirl
        n = (count > LSI_BUF_SIZE) ? LSI_BUF_SIZE : count;
1022 7d8406be pbrook
        cpu_physical_memory_read(src, buf, n);
1023 7d8406be pbrook
        cpu_physical_memory_write(dest, buf, n);
1024 7d8406be pbrook
        src += n;
1025 7d8406be pbrook
        dest += n;
1026 7d8406be pbrook
        count -= n;
1027 7d8406be pbrook
    }
1028 7d8406be pbrook
}
1029 7d8406be pbrook
1030 a917d384 pbrook
static void lsi_wait_reselect(LSIState *s)
1031 a917d384 pbrook
{
1032 042ec49d Gerd Hoffmann
    lsi_request *p;
1033 042ec49d Gerd Hoffmann
1034 a917d384 pbrook
    DPRINTF("Wait Reselect\n");
1035 042ec49d Gerd Hoffmann
1036 042ec49d Gerd Hoffmann
    QTAILQ_FOREACH(p, &s->queue, next) {
1037 042ec49d Gerd Hoffmann
        if (p->pending) {
1038 aa4d32c4 Gerd Hoffmann
            lsi_reselect(s, p);
1039 a917d384 pbrook
            break;
1040 a917d384 pbrook
        }
1041 a917d384 pbrook
    }
1042 b96a0da0 Gerd Hoffmann
    if (s->current == NULL) {
1043 a917d384 pbrook
        s->waiting = 1;
1044 a917d384 pbrook
    }
1045 a917d384 pbrook
}
1046 a917d384 pbrook
1047 7d8406be pbrook
static void lsi_execute_script(LSIState *s)
1048 7d8406be pbrook
{
1049 7d8406be pbrook
    uint32_t insn;
1050 b25cf589 aliguori
    uint32_t addr, addr_high;
1051 7d8406be pbrook
    int opcode;
1052 ee4d919f aliguori
    int insn_processed = 0;
1053 7d8406be pbrook
1054 7d8406be pbrook
    s->istat1 |= LSI_ISTAT1_SRUN;
1055 7d8406be pbrook
again:
1056 ee4d919f aliguori
    insn_processed++;
1057 7d8406be pbrook
    insn = read_dword(s, s->dsp);
1058 02b373ad balrog
    if (!insn) {
1059 02b373ad balrog
        /* If we receive an empty opcode increment the DSP by 4 bytes
1060 02b373ad balrog
           instead of 8 and execute the next opcode at that location */
1061 02b373ad balrog
        s->dsp += 4;
1062 02b373ad balrog
        goto again;
1063 02b373ad balrog
    }
1064 7d8406be pbrook
    addr = read_dword(s, s->dsp + 4);
1065 b25cf589 aliguori
    addr_high = 0;
1066 7d8406be pbrook
    DPRINTF("SCRIPTS dsp=%08x opcode %08x arg %08x\n", s->dsp, insn, addr);
1067 7d8406be pbrook
    s->dsps = addr;
1068 7d8406be pbrook
    s->dcmd = insn >> 24;
1069 7d8406be pbrook
    s->dsp += 8;
1070 7d8406be pbrook
    switch (insn >> 30) {
1071 7d8406be pbrook
    case 0: /* Block move.  */
1072 7d8406be pbrook
        if (s->sist1 & LSI_SIST1_STO) {
1073 7d8406be pbrook
            DPRINTF("Delayed select timeout\n");
1074 7d8406be pbrook
            lsi_stop_script(s);
1075 7d8406be pbrook
            break;
1076 7d8406be pbrook
        }
1077 7d8406be pbrook
        s->dbc = insn & 0xffffff;
1078 7d8406be pbrook
        s->rbc = s->dbc;
1079 dd8edf01 aliguori
        /* ??? Set ESA.  */
1080 dd8edf01 aliguori
        s->ia = s->dsp - 8;
1081 7d8406be pbrook
        if (insn & (1 << 29)) {
1082 7d8406be pbrook
            /* Indirect addressing.  */
1083 7d8406be pbrook
            addr = read_dword(s, addr);
1084 7d8406be pbrook
        } else if (insn & (1 << 28)) {
1085 7d8406be pbrook
            uint32_t buf[2];
1086 7d8406be pbrook
            int32_t offset;
1087 7d8406be pbrook
            /* Table indirect addressing.  */
1088 dd8edf01 aliguori
1089 dd8edf01 aliguori
            /* 32-bit Table indirect */
1090 7d8406be pbrook
            offset = sxt24(addr);
1091 7d8406be pbrook
            cpu_physical_memory_read(s->dsa + offset, (uint8_t *)buf, 8);
1092 b25cf589 aliguori
            /* byte count is stored in bits 0:23 only */
1093 b25cf589 aliguori
            s->dbc = cpu_to_le32(buf[0]) & 0xffffff;
1094 7faa239c ths
            s->rbc = s->dbc;
1095 7d8406be pbrook
            addr = cpu_to_le32(buf[1]);
1096 b25cf589 aliguori
1097 b25cf589 aliguori
            /* 40-bit DMA, upper addr bits [39:32] stored in first DWORD of
1098 b25cf589 aliguori
             * table, bits [31:24] */
1099 b25cf589 aliguori
            if (lsi_dma_40bit(s))
1100 b25cf589 aliguori
                addr_high = cpu_to_le32(buf[0]) >> 24;
1101 dd8edf01 aliguori
            else if (lsi_dma_ti64bit(s)) {
1102 dd8edf01 aliguori
                int selector = (cpu_to_le32(buf[0]) >> 24) & 0x1f;
1103 dd8edf01 aliguori
                switch (selector) {
1104 dd8edf01 aliguori
                case 0 ... 0x0f:
1105 dd8edf01 aliguori
                    /* offset index into scratch registers since
1106 dd8edf01 aliguori
                     * TI64 mode can use registers C to R */
1107 dd8edf01 aliguori
                    addr_high = s->scratch[2 + selector];
1108 dd8edf01 aliguori
                    break;
1109 dd8edf01 aliguori
                case 0x10:
1110 dd8edf01 aliguori
                    addr_high = s->mmrs;
1111 dd8edf01 aliguori
                    break;
1112 dd8edf01 aliguori
                case 0x11:
1113 dd8edf01 aliguori
                    addr_high = s->mmws;
1114 dd8edf01 aliguori
                    break;
1115 dd8edf01 aliguori
                case 0x12:
1116 dd8edf01 aliguori
                    addr_high = s->sfs;
1117 dd8edf01 aliguori
                    break;
1118 dd8edf01 aliguori
                case 0x13:
1119 dd8edf01 aliguori
                    addr_high = s->drs;
1120 dd8edf01 aliguori
                    break;
1121 dd8edf01 aliguori
                case 0x14:
1122 dd8edf01 aliguori
                    addr_high = s->sbms;
1123 dd8edf01 aliguori
                    break;
1124 dd8edf01 aliguori
                case 0x15:
1125 dd8edf01 aliguori
                    addr_high = s->dbms;
1126 dd8edf01 aliguori
                    break;
1127 dd8edf01 aliguori
                default:
1128 dd8edf01 aliguori
                    BADF("Illegal selector specified (0x%x > 0x15)"
1129 dd8edf01 aliguori
                         " for 64-bit DMA block move", selector);
1130 dd8edf01 aliguori
                    break;
1131 dd8edf01 aliguori
                }
1132 dd8edf01 aliguori
            }
1133 dd8edf01 aliguori
        } else if (lsi_dma_64bit(s)) {
1134 dd8edf01 aliguori
            /* fetch a 3rd dword if 64-bit direct move is enabled and
1135 dd8edf01 aliguori
               only if we're not doing table indirect or indirect addressing */
1136 dd8edf01 aliguori
            s->dbms = read_dword(s, s->dsp);
1137 dd8edf01 aliguori
            s->dsp += 4;
1138 dd8edf01 aliguori
            s->ia = s->dsp - 12;
1139 7d8406be pbrook
        }
1140 7d8406be pbrook
        if ((s->sstat1 & PHASE_MASK) != ((insn >> 24) & 7)) {
1141 7d8406be pbrook
            DPRINTF("Wrong phase got %d expected %d\n",
1142 7d8406be pbrook
                    s->sstat1 & PHASE_MASK, (insn >> 24) & 7);
1143 7d8406be pbrook
            lsi_script_scsi_interrupt(s, LSI_SIST0_MA, 0);
1144 7d8406be pbrook
            break;
1145 7d8406be pbrook
        }
1146 7d8406be pbrook
        s->dnad = addr;
1147 b25cf589 aliguori
        s->dnad64 = addr_high;
1148 7d8406be pbrook
        switch (s->sstat1 & 0x7) {
1149 7d8406be pbrook
        case PHASE_DO:
1150 a917d384 pbrook
            s->waiting = 2;
1151 7d8406be pbrook
            lsi_do_dma(s, 1);
1152 a917d384 pbrook
            if (s->waiting)
1153 a917d384 pbrook
                s->waiting = 3;
1154 7d8406be pbrook
            break;
1155 7d8406be pbrook
        case PHASE_DI:
1156 a917d384 pbrook
            s->waiting = 2;
1157 7d8406be pbrook
            lsi_do_dma(s, 0);
1158 a917d384 pbrook
            if (s->waiting)
1159 a917d384 pbrook
                s->waiting = 3;
1160 7d8406be pbrook
            break;
1161 7d8406be pbrook
        case PHASE_CMD:
1162 7d8406be pbrook
            lsi_do_command(s);
1163 7d8406be pbrook
            break;
1164 7d8406be pbrook
        case PHASE_ST:
1165 7d8406be pbrook
            lsi_do_status(s);
1166 7d8406be pbrook
            break;
1167 7d8406be pbrook
        case PHASE_MO:
1168 7d8406be pbrook
            lsi_do_msgout(s);
1169 7d8406be pbrook
            break;
1170 7d8406be pbrook
        case PHASE_MI:
1171 7d8406be pbrook
            lsi_do_msgin(s);
1172 7d8406be pbrook
            break;
1173 7d8406be pbrook
        default:
1174 7d8406be pbrook
            BADF("Unimplemented phase %d\n", s->sstat1 & PHASE_MASK);
1175 7d8406be pbrook
            exit(1);
1176 7d8406be pbrook
        }
1177 7d8406be pbrook
        s->dfifo = s->dbc & 0xff;
1178 7d8406be pbrook
        s->ctest5 = (s->ctest5 & 0xfc) | ((s->dbc >> 8) & 3);
1179 7d8406be pbrook
        s->sbc = s->dbc;
1180 7d8406be pbrook
        s->rbc -= s->dbc;
1181 7d8406be pbrook
        s->ua = addr + s->dbc;
1182 7d8406be pbrook
        break;
1183 7d8406be pbrook
1184 7d8406be pbrook
    case 1: /* IO or Read/Write instruction.  */
1185 7d8406be pbrook
        opcode = (insn >> 27) & 7;
1186 7d8406be pbrook
        if (opcode < 5) {
1187 7d8406be pbrook
            uint32_t id;
1188 7d8406be pbrook
1189 7d8406be pbrook
            if (insn & (1 << 25)) {
1190 7d8406be pbrook
                id = read_dword(s, s->dsa + sxt24(insn));
1191 7d8406be pbrook
            } else {
1192 07a1bea8 Laszlo Ast
                id = insn;
1193 7d8406be pbrook
            }
1194 7d8406be pbrook
            id = (id >> 16) & 0xf;
1195 7d8406be pbrook
            if (insn & (1 << 26)) {
1196 7d8406be pbrook
                addr = s->dsp + sxt24(addr);
1197 7d8406be pbrook
            }
1198 7d8406be pbrook
            s->dnad = addr;
1199 7d8406be pbrook
            switch (opcode) {
1200 7d8406be pbrook
            case 0: /* Select */
1201 a917d384 pbrook
                s->sdid = id;
1202 38f5b2b8 Laszlo Ast
                if (s->scntl1 & LSI_SCNTL1_CON) {
1203 38f5b2b8 Laszlo Ast
                    DPRINTF("Already reselected, jumping to alternative address\n");
1204 38f5b2b8 Laszlo Ast
                    s->dsp = s->dnad;
1205 a917d384 pbrook
                    break;
1206 a917d384 pbrook
                }
1207 7d8406be pbrook
                s->sstat0 |= LSI_SSTAT0_WOA;
1208 7d8406be pbrook
                s->scntl1 &= ~LSI_SCNTL1_IARB;
1209 ca9c39fa Gerd Hoffmann
                if (id >= LSI_MAX_DEVS || !s->bus.devs[id]) {
1210 64d56409 Jan Kiszka
                    lsi_bad_selection(s, id);
1211 7d8406be pbrook
                    break;
1212 7d8406be pbrook
                }
1213 7d8406be pbrook
                DPRINTF("Selected target %d%s\n",
1214 7d8406be pbrook
                        id, insn & (1 << 3) ? " ATN" : "");
1215 7d8406be pbrook
                /* ??? Linux drivers compain when this is set.  Maybe
1216 7d8406be pbrook
                   it only applies in low-level mode (unimplemented).
1217 7d8406be pbrook
                lsi_script_scsi_interrupt(s, LSI_SIST0_CMP, 0); */
1218 af12ac98 Gerd Hoffmann
                s->select_tag = id << 8;
1219 7d8406be pbrook
                s->scntl1 |= LSI_SCNTL1_CON;
1220 7d8406be pbrook
                if (insn & (1 << 3)) {
1221 7d8406be pbrook
                    s->socl |= LSI_SOCL_ATN;
1222 7d8406be pbrook
                }
1223 7d8406be pbrook
                lsi_set_phase(s, PHASE_MO);
1224 7d8406be pbrook
                break;
1225 7d8406be pbrook
            case 1: /* Disconnect */
1226 a15fdf86 Laszlo Ast
                DPRINTF("Wait Disconnect\n");
1227 7d8406be pbrook
                s->scntl1 &= ~LSI_SCNTL1_CON;
1228 7d8406be pbrook
                break;
1229 7d8406be pbrook
            case 2: /* Wait Reselect */
1230 e560125e Laszlo Ast
                if (!lsi_irq_on_rsl(s)) {
1231 e560125e Laszlo Ast
                    lsi_wait_reselect(s);
1232 e560125e Laszlo Ast
                }
1233 7d8406be pbrook
                break;
1234 7d8406be pbrook
            case 3: /* Set */
1235 7d8406be pbrook
                DPRINTF("Set%s%s%s%s\n",
1236 7d8406be pbrook
                        insn & (1 << 3) ? " ATN" : "",
1237 7d8406be pbrook
                        insn & (1 << 6) ? " ACK" : "",
1238 7d8406be pbrook
                        insn & (1 << 9) ? " TM" : "",
1239 7d8406be pbrook
                        insn & (1 << 10) ? " CC" : "");
1240 7d8406be pbrook
                if (insn & (1 << 3)) {
1241 7d8406be pbrook
                    s->socl |= LSI_SOCL_ATN;
1242 7d8406be pbrook
                    lsi_set_phase(s, PHASE_MO);
1243 7d8406be pbrook
                }
1244 7d8406be pbrook
                if (insn & (1 << 9)) {
1245 7d8406be pbrook
                    BADF("Target mode not implemented\n");
1246 7d8406be pbrook
                    exit(1);
1247 7d8406be pbrook
                }
1248 7d8406be pbrook
                if (insn & (1 << 10))
1249 7d8406be pbrook
                    s->carry = 1;
1250 7d8406be pbrook
                break;
1251 7d8406be pbrook
            case 4: /* Clear */
1252 7d8406be pbrook
                DPRINTF("Clear%s%s%s%s\n",
1253 7d8406be pbrook
                        insn & (1 << 3) ? " ATN" : "",
1254 7d8406be pbrook
                        insn & (1 << 6) ? " ACK" : "",
1255 7d8406be pbrook
                        insn & (1 << 9) ? " TM" : "",
1256 7d8406be pbrook
                        insn & (1 << 10) ? " CC" : "");
1257 7d8406be pbrook
                if (insn & (1 << 3)) {
1258 7d8406be pbrook
                    s->socl &= ~LSI_SOCL_ATN;
1259 7d8406be pbrook
                }
1260 7d8406be pbrook
                if (insn & (1 << 10))
1261 7d8406be pbrook
                    s->carry = 0;
1262 7d8406be pbrook
                break;
1263 7d8406be pbrook
            }
1264 7d8406be pbrook
        } else {
1265 7d8406be pbrook
            uint8_t op0;
1266 7d8406be pbrook
            uint8_t op1;
1267 7d8406be pbrook
            uint8_t data8;
1268 7d8406be pbrook
            int reg;
1269 7d8406be pbrook
            int operator;
1270 7d8406be pbrook
#ifdef DEBUG_LSI
1271 7d8406be pbrook
            static const char *opcode_names[3] =
1272 7d8406be pbrook
                {"Write", "Read", "Read-Modify-Write"};
1273 7d8406be pbrook
            static const char *operator_names[8] =
1274 7d8406be pbrook
                {"MOV", "SHL", "OR", "XOR", "AND", "SHR", "ADD", "ADC"};
1275 7d8406be pbrook
#endif
1276 7d8406be pbrook
1277 7d8406be pbrook
            reg = ((insn >> 16) & 0x7f) | (insn & 0x80);
1278 7d8406be pbrook
            data8 = (insn >> 8) & 0xff;
1279 7d8406be pbrook
            opcode = (insn >> 27) & 7;
1280 7d8406be pbrook
            operator = (insn >> 24) & 7;
1281 a917d384 pbrook
            DPRINTF("%s reg 0x%x %s data8=0x%02x sfbr=0x%02x%s\n",
1282 7d8406be pbrook
                    opcode_names[opcode - 5], reg,
1283 a917d384 pbrook
                    operator_names[operator], data8, s->sfbr,
1284 7d8406be pbrook
                    (insn & (1 << 23)) ? " SFBR" : "");
1285 7d8406be pbrook
            op0 = op1 = 0;
1286 7d8406be pbrook
            switch (opcode) {
1287 7d8406be pbrook
            case 5: /* From SFBR */
1288 7d8406be pbrook
                op0 = s->sfbr;
1289 7d8406be pbrook
                op1 = data8;
1290 7d8406be pbrook
                break;
1291 7d8406be pbrook
            case 6: /* To SFBR */
1292 7d8406be pbrook
                if (operator)
1293 7d8406be pbrook
                    op0 = lsi_reg_readb(s, reg);
1294 7d8406be pbrook
                op1 = data8;
1295 7d8406be pbrook
                break;
1296 7d8406be pbrook
            case 7: /* Read-modify-write */
1297 7d8406be pbrook
                if (operator)
1298 7d8406be pbrook
                    op0 = lsi_reg_readb(s, reg);
1299 7d8406be pbrook
                if (insn & (1 << 23)) {
1300 7d8406be pbrook
                    op1 = s->sfbr;
1301 7d8406be pbrook
                } else {
1302 7d8406be pbrook
                    op1 = data8;
1303 7d8406be pbrook
                }
1304 7d8406be pbrook
                break;
1305 7d8406be pbrook
            }
1306 7d8406be pbrook
1307 7d8406be pbrook
            switch (operator) {
1308 7d8406be pbrook
            case 0: /* move */
1309 7d8406be pbrook
                op0 = op1;
1310 7d8406be pbrook
                break;
1311 7d8406be pbrook
            case 1: /* Shift left */
1312 7d8406be pbrook
                op1 = op0 >> 7;
1313 7d8406be pbrook
                op0 = (op0 << 1) | s->carry;
1314 7d8406be pbrook
                s->carry = op1;
1315 7d8406be pbrook
                break;
1316 7d8406be pbrook
            case 2: /* OR */
1317 7d8406be pbrook
                op0 |= op1;
1318 7d8406be pbrook
                break;
1319 7d8406be pbrook
            case 3: /* XOR */
1320 dcfb9014 ths
                op0 ^= op1;
1321 7d8406be pbrook
                break;
1322 7d8406be pbrook
            case 4: /* AND */
1323 7d8406be pbrook
                op0 &= op1;
1324 7d8406be pbrook
                break;
1325 7d8406be pbrook
            case 5: /* SHR */
1326 7d8406be pbrook
                op1 = op0 & 1;
1327 7d8406be pbrook
                op0 = (op0 >> 1) | (s->carry << 7);
1328 687fa640 ths
                s->carry = op1;
1329 7d8406be pbrook
                break;
1330 7d8406be pbrook
            case 6: /* ADD */
1331 7d8406be pbrook
                op0 += op1;
1332 7d8406be pbrook
                s->carry = op0 < op1;
1333 7d8406be pbrook
                break;
1334 7d8406be pbrook
            case 7: /* ADC */
1335 7d8406be pbrook
                op0 += op1 + s->carry;
1336 7d8406be pbrook
                if (s->carry)
1337 7d8406be pbrook
                    s->carry = op0 <= op1;
1338 7d8406be pbrook
                else
1339 7d8406be pbrook
                    s->carry = op0 < op1;
1340 7d8406be pbrook
                break;
1341 7d8406be pbrook
            }
1342 7d8406be pbrook
1343 7d8406be pbrook
            switch (opcode) {
1344 7d8406be pbrook
            case 5: /* From SFBR */
1345 7d8406be pbrook
            case 7: /* Read-modify-write */
1346 7d8406be pbrook
                lsi_reg_writeb(s, reg, op0);
1347 7d8406be pbrook
                break;
1348 7d8406be pbrook
            case 6: /* To SFBR */
1349 7d8406be pbrook
                s->sfbr = op0;
1350 7d8406be pbrook
                break;
1351 7d8406be pbrook
            }
1352 7d8406be pbrook
        }
1353 7d8406be pbrook
        break;
1354 7d8406be pbrook
1355 7d8406be pbrook
    case 2: /* Transfer Control.  */
1356 7d8406be pbrook
        {
1357 7d8406be pbrook
            int cond;
1358 7d8406be pbrook
            int jmp;
1359 7d8406be pbrook
1360 7d8406be pbrook
            if ((insn & 0x002e0000) == 0) {
1361 7d8406be pbrook
                DPRINTF("NOP\n");
1362 7d8406be pbrook
                break;
1363 7d8406be pbrook
            }
1364 7d8406be pbrook
            if (s->sist1 & LSI_SIST1_STO) {
1365 7d8406be pbrook
                DPRINTF("Delayed select timeout\n");
1366 7d8406be pbrook
                lsi_stop_script(s);
1367 7d8406be pbrook
                break;
1368 7d8406be pbrook
            }
1369 7d8406be pbrook
            cond = jmp = (insn & (1 << 19)) != 0;
1370 7d8406be pbrook
            if (cond == jmp && (insn & (1 << 21))) {
1371 7d8406be pbrook
                DPRINTF("Compare carry %d\n", s->carry == jmp);
1372 7d8406be pbrook
                cond = s->carry != 0;
1373 7d8406be pbrook
            }
1374 7d8406be pbrook
            if (cond == jmp && (insn & (1 << 17))) {
1375 7d8406be pbrook
                DPRINTF("Compare phase %d %c= %d\n",
1376 7d8406be pbrook
                        (s->sstat1 & PHASE_MASK),
1377 7d8406be pbrook
                        jmp ? '=' : '!',
1378 7d8406be pbrook
                        ((insn >> 24) & 7));
1379 7d8406be pbrook
                cond = (s->sstat1 & PHASE_MASK) == ((insn >> 24) & 7);
1380 7d8406be pbrook
            }
1381 7d8406be pbrook
            if (cond == jmp && (insn & (1 << 18))) {
1382 7d8406be pbrook
                uint8_t mask;
1383 7d8406be pbrook
1384 7d8406be pbrook
                mask = (~insn >> 8) & 0xff;
1385 7d8406be pbrook
                DPRINTF("Compare data 0x%x & 0x%x %c= 0x%x\n",
1386 7d8406be pbrook
                        s->sfbr, mask, jmp ? '=' : '!', insn & mask);
1387 7d8406be pbrook
                cond = (s->sfbr & mask) == (insn & mask);
1388 7d8406be pbrook
            }
1389 7d8406be pbrook
            if (cond == jmp) {
1390 7d8406be pbrook
                if (insn & (1 << 23)) {
1391 7d8406be pbrook
                    /* Relative address.  */
1392 7d8406be pbrook
                    addr = s->dsp + sxt24(addr);
1393 7d8406be pbrook
                }
1394 7d8406be pbrook
                switch ((insn >> 27) & 7) {
1395 7d8406be pbrook
                case 0: /* Jump */
1396 7d8406be pbrook
                    DPRINTF("Jump to 0x%08x\n", addr);
1397 7d8406be pbrook
                    s->dsp = addr;
1398 7d8406be pbrook
                    break;
1399 7d8406be pbrook
                case 1: /* Call */
1400 7d8406be pbrook
                    DPRINTF("Call 0x%08x\n", addr);
1401 7d8406be pbrook
                    s->temp = s->dsp;
1402 7d8406be pbrook
                    s->dsp = addr;
1403 7d8406be pbrook
                    break;
1404 7d8406be pbrook
                case 2: /* Return */
1405 7d8406be pbrook
                    DPRINTF("Return to 0x%08x\n", s->temp);
1406 7d8406be pbrook
                    s->dsp = s->temp;
1407 7d8406be pbrook
                    break;
1408 7d8406be pbrook
                case 3: /* Interrupt */
1409 7d8406be pbrook
                    DPRINTF("Interrupt 0x%08x\n", s->dsps);
1410 7d8406be pbrook
                    if ((insn & (1 << 20)) != 0) {
1411 7d8406be pbrook
                        s->istat0 |= LSI_ISTAT0_INTF;
1412 7d8406be pbrook
                        lsi_update_irq(s);
1413 7d8406be pbrook
                    } else {
1414 7d8406be pbrook
                        lsi_script_dma_interrupt(s, LSI_DSTAT_SIR);
1415 7d8406be pbrook
                    }
1416 7d8406be pbrook
                    break;
1417 7d8406be pbrook
                default:
1418 7d8406be pbrook
                    DPRINTF("Illegal transfer control\n");
1419 7d8406be pbrook
                    lsi_script_dma_interrupt(s, LSI_DSTAT_IID);
1420 7d8406be pbrook
                    break;
1421 7d8406be pbrook
                }
1422 7d8406be pbrook
            } else {
1423 7d8406be pbrook
                DPRINTF("Control condition failed\n");
1424 7d8406be pbrook
            }
1425 7d8406be pbrook
        }
1426 7d8406be pbrook
        break;
1427 7d8406be pbrook
1428 7d8406be pbrook
    case 3:
1429 7d8406be pbrook
        if ((insn & (1 << 29)) == 0) {
1430 7d8406be pbrook
            /* Memory move.  */
1431 7d8406be pbrook
            uint32_t dest;
1432 7d8406be pbrook
            /* ??? The docs imply the destination address is loaded into
1433 7d8406be pbrook
               the TEMP register.  However the Linux drivers rely on
1434 7d8406be pbrook
               the value being presrved.  */
1435 7d8406be pbrook
            dest = read_dword(s, s->dsp);
1436 7d8406be pbrook
            s->dsp += 4;
1437 7d8406be pbrook
            lsi_memcpy(s, dest, addr, insn & 0xffffff);
1438 7d8406be pbrook
        } else {
1439 7d8406be pbrook
            uint8_t data[7];
1440 7d8406be pbrook
            int reg;
1441 7d8406be pbrook
            int n;
1442 7d8406be pbrook
            int i;
1443 7d8406be pbrook
1444 7d8406be pbrook
            if (insn & (1 << 28)) {
1445 7d8406be pbrook
                addr = s->dsa + sxt24(addr);
1446 7d8406be pbrook
            }
1447 7d8406be pbrook
            n = (insn & 7);
1448 7d8406be pbrook
            reg = (insn >> 16) & 0xff;
1449 7d8406be pbrook
            if (insn & (1 << 24)) {
1450 7d8406be pbrook
                cpu_physical_memory_read(addr, data, n);
1451 a917d384 pbrook
                DPRINTF("Load reg 0x%x size %d addr 0x%08x = %08x\n", reg, n,
1452 a917d384 pbrook
                        addr, *(int *)data);
1453 7d8406be pbrook
                for (i = 0; i < n; i++) {
1454 7d8406be pbrook
                    lsi_reg_writeb(s, reg + i, data[i]);
1455 7d8406be pbrook
                }
1456 7d8406be pbrook
            } else {
1457 7d8406be pbrook
                DPRINTF("Store reg 0x%x size %d addr 0x%08x\n", reg, n, addr);
1458 7d8406be pbrook
                for (i = 0; i < n; i++) {
1459 7d8406be pbrook
                    data[i] = lsi_reg_readb(s, reg + i);
1460 7d8406be pbrook
                }
1461 7d8406be pbrook
                cpu_physical_memory_write(addr, data, n);
1462 7d8406be pbrook
            }
1463 7d8406be pbrook
        }
1464 7d8406be pbrook
    }
1465 ee4d919f aliguori
    if (insn_processed > 10000 && !s->waiting) {
1466 64c68080 pbrook
        /* Some windows drivers make the device spin waiting for a memory
1467 64c68080 pbrook
           location to change.  If we have been executed a lot of code then
1468 64c68080 pbrook
           assume this is the case and force an unexpected device disconnect.
1469 64c68080 pbrook
           This is apparently sufficient to beat the drivers into submission.
1470 64c68080 pbrook
         */
1471 ee4d919f aliguori
        if (!(s->sien0 & LSI_SIST0_UDC))
1472 ee4d919f aliguori
            fprintf(stderr, "inf. loop with UDC masked\n");
1473 ee4d919f aliguori
        lsi_script_scsi_interrupt(s, LSI_SIST0_UDC, 0);
1474 ee4d919f aliguori
        lsi_disconnect(s);
1475 ee4d919f aliguori
    } else if (s->istat1 & LSI_ISTAT1_SRUN && !s->waiting) {
1476 7d8406be pbrook
        if (s->dcntl & LSI_DCNTL_SSM) {
1477 7d8406be pbrook
            lsi_script_dma_interrupt(s, LSI_DSTAT_SSI);
1478 7d8406be pbrook
        } else {
1479 7d8406be pbrook
            goto again;
1480 7d8406be pbrook
        }
1481 7d8406be pbrook
    }
1482 7d8406be pbrook
    DPRINTF("SCRIPTS execution stopped\n");
1483 7d8406be pbrook
}
1484 7d8406be pbrook
1485 7d8406be pbrook
static uint8_t lsi_reg_readb(LSIState *s, int offset)
1486 7d8406be pbrook
{
1487 7d8406be pbrook
    uint8_t tmp;
1488 75f76531 aurel32
#define CASE_GET_REG24(name, addr) \
1489 75f76531 aurel32
    case addr: return s->name & 0xff; \
1490 75f76531 aurel32
    case addr + 1: return (s->name >> 8) & 0xff; \
1491 75f76531 aurel32
    case addr + 2: return (s->name >> 16) & 0xff;
1492 75f76531 aurel32
1493 7d8406be pbrook
#define CASE_GET_REG32(name, addr) \
1494 7d8406be pbrook
    case addr: return s->name & 0xff; \
1495 7d8406be pbrook
    case addr + 1: return (s->name >> 8) & 0xff; \
1496 7d8406be pbrook
    case addr + 2: return (s->name >> 16) & 0xff; \
1497 7d8406be pbrook
    case addr + 3: return (s->name >> 24) & 0xff;
1498 7d8406be pbrook
1499 7d8406be pbrook
#ifdef DEBUG_LSI_REG
1500 7d8406be pbrook
    DPRINTF("Read reg %x\n", offset);
1501 7d8406be pbrook
#endif
1502 7d8406be pbrook
    switch (offset) {
1503 7d8406be pbrook
    case 0x00: /* SCNTL0 */
1504 7d8406be pbrook
        return s->scntl0;
1505 7d8406be pbrook
    case 0x01: /* SCNTL1 */
1506 7d8406be pbrook
        return s->scntl1;
1507 7d8406be pbrook
    case 0x02: /* SCNTL2 */
1508 7d8406be pbrook
        return s->scntl2;
1509 7d8406be pbrook
    case 0x03: /* SCNTL3 */
1510 7d8406be pbrook
        return s->scntl3;
1511 7d8406be pbrook
    case 0x04: /* SCID */
1512 7d8406be pbrook
        return s->scid;
1513 7d8406be pbrook
    case 0x05: /* SXFER */
1514 7d8406be pbrook
        return s->sxfer;
1515 7d8406be pbrook
    case 0x06: /* SDID */
1516 7d8406be pbrook
        return s->sdid;
1517 7d8406be pbrook
    case 0x07: /* GPREG0 */
1518 7d8406be pbrook
        return 0x7f;
1519 985a03b0 ths
    case 0x08: /* Revision ID */
1520 985a03b0 ths
        return 0x00;
1521 a917d384 pbrook
    case 0xa: /* SSID */
1522 a917d384 pbrook
        return s->ssid;
1523 7d8406be pbrook
    case 0xb: /* SBCL */
1524 7d8406be pbrook
        /* ??? This is not correct. However it's (hopefully) only
1525 7d8406be pbrook
           used for diagnostics, so should be ok.  */
1526 7d8406be pbrook
        return 0;
1527 7d8406be pbrook
    case 0xc: /* DSTAT */
1528 7d8406be pbrook
        tmp = s->dstat | 0x80;
1529 7d8406be pbrook
        if ((s->istat0 & LSI_ISTAT0_INTF) == 0)
1530 7d8406be pbrook
            s->dstat = 0;
1531 7d8406be pbrook
        lsi_update_irq(s);
1532 7d8406be pbrook
        return tmp;
1533 7d8406be pbrook
    case 0x0d: /* SSTAT0 */
1534 7d8406be pbrook
        return s->sstat0;
1535 7d8406be pbrook
    case 0x0e: /* SSTAT1 */
1536 7d8406be pbrook
        return s->sstat1;
1537 7d8406be pbrook
    case 0x0f: /* SSTAT2 */
1538 7d8406be pbrook
        return s->scntl1 & LSI_SCNTL1_CON ? 0 : 2;
1539 7d8406be pbrook
    CASE_GET_REG32(dsa, 0x10)
1540 7d8406be pbrook
    case 0x14: /* ISTAT0 */
1541 7d8406be pbrook
        return s->istat0;
1542 ecabe8cc aliguori
    case 0x15: /* ISTAT1 */
1543 ecabe8cc aliguori
        return s->istat1;
1544 7d8406be pbrook
    case 0x16: /* MBOX0 */
1545 7d8406be pbrook
        return s->mbox0;
1546 7d8406be pbrook
    case 0x17: /* MBOX1 */
1547 7d8406be pbrook
        return s->mbox1;
1548 7d8406be pbrook
    case 0x18: /* CTEST0 */
1549 7d8406be pbrook
        return 0xff;
1550 7d8406be pbrook
    case 0x19: /* CTEST1 */
1551 7d8406be pbrook
        return 0;
1552 7d8406be pbrook
    case 0x1a: /* CTEST2 */
1553 9167a69a balrog
        tmp = s->ctest2 | LSI_CTEST2_DACK | LSI_CTEST2_CM;
1554 7d8406be pbrook
        if (s->istat0 & LSI_ISTAT0_SIGP) {
1555 7d8406be pbrook
            s->istat0 &= ~LSI_ISTAT0_SIGP;
1556 7d8406be pbrook
            tmp |= LSI_CTEST2_SIGP;
1557 7d8406be pbrook
        }
1558 7d8406be pbrook
        return tmp;
1559 7d8406be pbrook
    case 0x1b: /* CTEST3 */
1560 7d8406be pbrook
        return s->ctest3;
1561 7d8406be pbrook
    CASE_GET_REG32(temp, 0x1c)
1562 7d8406be pbrook
    case 0x20: /* DFIFO */
1563 7d8406be pbrook
        return 0;
1564 7d8406be pbrook
    case 0x21: /* CTEST4 */
1565 7d8406be pbrook
        return s->ctest4;
1566 7d8406be pbrook
    case 0x22: /* CTEST5 */
1567 7d8406be pbrook
        return s->ctest5;
1568 985a03b0 ths
    case 0x23: /* CTEST6 */
1569 985a03b0 ths
         return 0;
1570 75f76531 aurel32
    CASE_GET_REG24(dbc, 0x24)
1571 7d8406be pbrook
    case 0x27: /* DCMD */
1572 7d8406be pbrook
        return s->dcmd;
1573 4b9a2d6d Sebastian Herbszt
    CASE_GET_REG32(dnad, 0x28)
1574 7d8406be pbrook
    CASE_GET_REG32(dsp, 0x2c)
1575 7d8406be pbrook
    CASE_GET_REG32(dsps, 0x30)
1576 7d8406be pbrook
    CASE_GET_REG32(scratch[0], 0x34)
1577 7d8406be pbrook
    case 0x38: /* DMODE */
1578 7d8406be pbrook
        return s->dmode;
1579 7d8406be pbrook
    case 0x39: /* DIEN */
1580 7d8406be pbrook
        return s->dien;
1581 bd8ee11a Sebastian Herbszt
    case 0x3a: /* SBR */
1582 bd8ee11a Sebastian Herbszt
        return s->sbr;
1583 7d8406be pbrook
    case 0x3b: /* DCNTL */
1584 7d8406be pbrook
        return s->dcntl;
1585 7d8406be pbrook
    case 0x40: /* SIEN0 */
1586 7d8406be pbrook
        return s->sien0;
1587 7d8406be pbrook
    case 0x41: /* SIEN1 */
1588 7d8406be pbrook
        return s->sien1;
1589 7d8406be pbrook
    case 0x42: /* SIST0 */
1590 7d8406be pbrook
        tmp = s->sist0;
1591 7d8406be pbrook
        s->sist0 = 0;
1592 7d8406be pbrook
        lsi_update_irq(s);
1593 7d8406be pbrook
        return tmp;
1594 7d8406be pbrook
    case 0x43: /* SIST1 */
1595 7d8406be pbrook
        tmp = s->sist1;
1596 7d8406be pbrook
        s->sist1 = 0;
1597 7d8406be pbrook
        lsi_update_irq(s);
1598 7d8406be pbrook
        return tmp;
1599 9167a69a balrog
    case 0x46: /* MACNTL */
1600 9167a69a balrog
        return 0x0f;
1601 7d8406be pbrook
    case 0x47: /* GPCNTL0 */
1602 7d8406be pbrook
        return 0x0f;
1603 7d8406be pbrook
    case 0x48: /* STIME0 */
1604 7d8406be pbrook
        return s->stime0;
1605 7d8406be pbrook
    case 0x4a: /* RESPID0 */
1606 7d8406be pbrook
        return s->respid0;
1607 7d8406be pbrook
    case 0x4b: /* RESPID1 */
1608 7d8406be pbrook
        return s->respid1;
1609 7d8406be pbrook
    case 0x4d: /* STEST1 */
1610 7d8406be pbrook
        return s->stest1;
1611 7d8406be pbrook
    case 0x4e: /* STEST2 */
1612 7d8406be pbrook
        return s->stest2;
1613 7d8406be pbrook
    case 0x4f: /* STEST3 */
1614 7d8406be pbrook
        return s->stest3;
1615 a917d384 pbrook
    case 0x50: /* SIDL */
1616 a917d384 pbrook
        /* This is needed by the linux drivers.  We currently only update it
1617 a917d384 pbrook
           during the MSG IN phase.  */
1618 a917d384 pbrook
        return s->sidl;
1619 7d8406be pbrook
    case 0x52: /* STEST4 */
1620 7d8406be pbrook
        return 0xe0;
1621 7d8406be pbrook
    case 0x56: /* CCNTL0 */
1622 7d8406be pbrook
        return s->ccntl0;
1623 7d8406be pbrook
    case 0x57: /* CCNTL1 */
1624 7d8406be pbrook
        return s->ccntl1;
1625 a917d384 pbrook
    case 0x58: /* SBDL */
1626 a917d384 pbrook
        /* Some drivers peek at the data bus during the MSG IN phase.  */
1627 a917d384 pbrook
        if ((s->sstat1 & PHASE_MASK) == PHASE_MI)
1628 a917d384 pbrook
            return s->msg[0];
1629 a917d384 pbrook
        return 0;
1630 a917d384 pbrook
    case 0x59: /* SBDL high */
1631 7d8406be pbrook
        return 0;
1632 7d8406be pbrook
    CASE_GET_REG32(mmrs, 0xa0)
1633 7d8406be pbrook
    CASE_GET_REG32(mmws, 0xa4)
1634 7d8406be pbrook
    CASE_GET_REG32(sfs, 0xa8)
1635 7d8406be pbrook
    CASE_GET_REG32(drs, 0xac)
1636 7d8406be pbrook
    CASE_GET_REG32(sbms, 0xb0)
1637 ab57d967 aliguori
    CASE_GET_REG32(dbms, 0xb4)
1638 7d8406be pbrook
    CASE_GET_REG32(dnad64, 0xb8)
1639 7d8406be pbrook
    CASE_GET_REG32(pmjad1, 0xc0)
1640 7d8406be pbrook
    CASE_GET_REG32(pmjad2, 0xc4)
1641 7d8406be pbrook
    CASE_GET_REG32(rbc, 0xc8)
1642 7d8406be pbrook
    CASE_GET_REG32(ua, 0xcc)
1643 7d8406be pbrook
    CASE_GET_REG32(ia, 0xd4)
1644 7d8406be pbrook
    CASE_GET_REG32(sbc, 0xd8)
1645 7d8406be pbrook
    CASE_GET_REG32(csbc, 0xdc)
1646 7d8406be pbrook
    }
1647 7d8406be pbrook
    if (offset >= 0x5c && offset < 0xa0) {
1648 7d8406be pbrook
        int n;
1649 7d8406be pbrook
        int shift;
1650 7d8406be pbrook
        n = (offset - 0x58) >> 2;
1651 7d8406be pbrook
        shift = (offset & 3) * 8;
1652 7d8406be pbrook
        return (s->scratch[n] >> shift) & 0xff;
1653 7d8406be pbrook
    }
1654 7d8406be pbrook
    BADF("readb 0x%x\n", offset);
1655 7d8406be pbrook
    exit(1);
1656 75f76531 aurel32
#undef CASE_GET_REG24
1657 7d8406be pbrook
#undef CASE_GET_REG32
1658 7d8406be pbrook
}
1659 7d8406be pbrook
1660 7d8406be pbrook
static void lsi_reg_writeb(LSIState *s, int offset, uint8_t val)
1661 7d8406be pbrook
{
1662 49c47daa Sebastian Herbszt
#define CASE_SET_REG24(name, addr) \
1663 49c47daa Sebastian Herbszt
    case addr    : s->name &= 0xffffff00; s->name |= val;       break; \
1664 49c47daa Sebastian Herbszt
    case addr + 1: s->name &= 0xffff00ff; s->name |= val << 8;  break; \
1665 49c47daa Sebastian Herbszt
    case addr + 2: s->name &= 0xff00ffff; s->name |= val << 16; break;
1666 49c47daa Sebastian Herbszt
1667 7d8406be pbrook
#define CASE_SET_REG32(name, addr) \
1668 7d8406be pbrook
    case addr    : s->name &= 0xffffff00; s->name |= val;       break; \
1669 7d8406be pbrook
    case addr + 1: s->name &= 0xffff00ff; s->name |= val << 8;  break; \
1670 7d8406be pbrook
    case addr + 2: s->name &= 0xff00ffff; s->name |= val << 16; break; \
1671 7d8406be pbrook
    case addr + 3: s->name &= 0x00ffffff; s->name |= val << 24; break;
1672 7d8406be pbrook
1673 7d8406be pbrook
#ifdef DEBUG_LSI_REG
1674 7d8406be pbrook
    DPRINTF("Write reg %x = %02x\n", offset, val);
1675 7d8406be pbrook
#endif
1676 7d8406be pbrook
    switch (offset) {
1677 7d8406be pbrook
    case 0x00: /* SCNTL0 */
1678 7d8406be pbrook
        s->scntl0 = val;
1679 7d8406be pbrook
        if (val & LSI_SCNTL0_START) {
1680 7d8406be pbrook
            BADF("Start sequence not implemented\n");
1681 7d8406be pbrook
        }
1682 7d8406be pbrook
        break;
1683 7d8406be pbrook
    case 0x01: /* SCNTL1 */
1684 7d8406be pbrook
        s->scntl1 = val & ~LSI_SCNTL1_SST;
1685 7d8406be pbrook
        if (val & LSI_SCNTL1_IARB) {
1686 7d8406be pbrook
            BADF("Immediate Arbritration not implemented\n");
1687 7d8406be pbrook
        }
1688 7d8406be pbrook
        if (val & LSI_SCNTL1_RST) {
1689 680a34ee Jan Kiszka
            if (!(s->sstat0 & LSI_SSTAT0_RST)) {
1690 680a34ee Jan Kiszka
                DeviceState *dev;
1691 680a34ee Jan Kiszka
                int id;
1692 680a34ee Jan Kiszka
1693 680a34ee Jan Kiszka
                for (id = 0; id < s->bus.ndev; id++) {
1694 680a34ee Jan Kiszka
                    if (s->bus.devs[id]) {
1695 680a34ee Jan Kiszka
                        dev = &s->bus.devs[id]->qdev;
1696 680a34ee Jan Kiszka
                        dev->info->reset(dev);
1697 680a34ee Jan Kiszka
                    }
1698 680a34ee Jan Kiszka
                }
1699 680a34ee Jan Kiszka
                s->sstat0 |= LSI_SSTAT0_RST;
1700 680a34ee Jan Kiszka
                lsi_script_scsi_interrupt(s, LSI_SIST0_RST, 0);
1701 680a34ee Jan Kiszka
            }
1702 7d8406be pbrook
        } else {
1703 7d8406be pbrook
            s->sstat0 &= ~LSI_SSTAT0_RST;
1704 7d8406be pbrook
        }
1705 7d8406be pbrook
        break;
1706 7d8406be pbrook
    case 0x02: /* SCNTL2 */
1707 7d8406be pbrook
        val &= ~(LSI_SCNTL2_WSR | LSI_SCNTL2_WSS);
1708 3d834c78 ths
        s->scntl2 = val;
1709 7d8406be pbrook
        break;
1710 7d8406be pbrook
    case 0x03: /* SCNTL3 */
1711 7d8406be pbrook
        s->scntl3 = val;
1712 7d8406be pbrook
        break;
1713 7d8406be pbrook
    case 0x04: /* SCID */
1714 7d8406be pbrook
        s->scid = val;
1715 7d8406be pbrook
        break;
1716 7d8406be pbrook
    case 0x05: /* SXFER */
1717 7d8406be pbrook
        s->sxfer = val;
1718 7d8406be pbrook
        break;
1719 a917d384 pbrook
    case 0x06: /* SDID */
1720 a917d384 pbrook
        if ((val & 0xf) != (s->ssid & 0xf))
1721 a917d384 pbrook
            BADF("Destination ID does not match SSID\n");
1722 a917d384 pbrook
        s->sdid = val & 0xf;
1723 a917d384 pbrook
        break;
1724 7d8406be pbrook
    case 0x07: /* GPREG0 */
1725 7d8406be pbrook
        break;
1726 a917d384 pbrook
    case 0x08: /* SFBR */
1727 a917d384 pbrook
        /* The CPU is not allowed to write to this register.  However the
1728 a917d384 pbrook
           SCRIPTS register move instructions are.  */
1729 a917d384 pbrook
        s->sfbr = val;
1730 a917d384 pbrook
        break;
1731 a15fdf86 Laszlo Ast
    case 0x0a: case 0x0b:
1732 9167a69a balrog
        /* Openserver writes to these readonly registers on startup */
1733 a15fdf86 Laszlo Ast
        return;
1734 7d8406be pbrook
    case 0x0c: case 0x0d: case 0x0e: case 0x0f:
1735 7d8406be pbrook
        /* Linux writes to these readonly registers on startup.  */
1736 7d8406be pbrook
        return;
1737 7d8406be pbrook
    CASE_SET_REG32(dsa, 0x10)
1738 7d8406be pbrook
    case 0x14: /* ISTAT0 */
1739 7d8406be pbrook
        s->istat0 = (s->istat0 & 0x0f) | (val & 0xf0);
1740 7d8406be pbrook
        if (val & LSI_ISTAT0_ABRT) {
1741 7d8406be pbrook
            lsi_script_dma_interrupt(s, LSI_DSTAT_ABRT);
1742 7d8406be pbrook
        }
1743 7d8406be pbrook
        if (val & LSI_ISTAT0_INTF) {
1744 7d8406be pbrook
            s->istat0 &= ~LSI_ISTAT0_INTF;
1745 7d8406be pbrook
            lsi_update_irq(s);
1746 7d8406be pbrook
        }
1747 4d611c9a pbrook
        if (s->waiting == 1 && val & LSI_ISTAT0_SIGP) {
1748 7d8406be pbrook
            DPRINTF("Woken by SIGP\n");
1749 7d8406be pbrook
            s->waiting = 0;
1750 7d8406be pbrook
            s->dsp = s->dnad;
1751 7d8406be pbrook
            lsi_execute_script(s);
1752 7d8406be pbrook
        }
1753 7d8406be pbrook
        if (val & LSI_ISTAT0_SRST) {
1754 7d8406be pbrook
            lsi_soft_reset(s);
1755 7d8406be pbrook
        }
1756 92d88ecb ths
        break;
1757 7d8406be pbrook
    case 0x16: /* MBOX0 */
1758 7d8406be pbrook
        s->mbox0 = val;
1759 92d88ecb ths
        break;
1760 7d8406be pbrook
    case 0x17: /* MBOX1 */
1761 7d8406be pbrook
        s->mbox1 = val;
1762 92d88ecb ths
        break;
1763 9167a69a balrog
    case 0x1a: /* CTEST2 */
1764 9167a69a balrog
        s->ctest2 = val & LSI_CTEST2_PCICIE;
1765 9167a69a balrog
        break;
1766 7d8406be pbrook
    case 0x1b: /* CTEST3 */
1767 7d8406be pbrook
        s->ctest3 = val & 0x0f;
1768 7d8406be pbrook
        break;
1769 7d8406be pbrook
    CASE_SET_REG32(temp, 0x1c)
1770 7d8406be pbrook
    case 0x21: /* CTEST4 */
1771 7d8406be pbrook
        if (val & 7) {
1772 7d8406be pbrook
           BADF("Unimplemented CTEST4-FBL 0x%x\n", val);
1773 7d8406be pbrook
        }
1774 7d8406be pbrook
        s->ctest4 = val;
1775 7d8406be pbrook
        break;
1776 7d8406be pbrook
    case 0x22: /* CTEST5 */
1777 7d8406be pbrook
        if (val & (LSI_CTEST5_ADCK | LSI_CTEST5_BBCK)) {
1778 7d8406be pbrook
            BADF("CTEST5 DMA increment not implemented\n");
1779 7d8406be pbrook
        }
1780 7d8406be pbrook
        s->ctest5 = val;
1781 7d8406be pbrook
        break;
1782 49c47daa Sebastian Herbszt
    CASE_SET_REG24(dbc, 0x24)
1783 4b9a2d6d Sebastian Herbszt
    CASE_SET_REG32(dnad, 0x28)
1784 3d834c78 ths
    case 0x2c: /* DSP[0:7] */
1785 7d8406be pbrook
        s->dsp &= 0xffffff00;
1786 7d8406be pbrook
        s->dsp |= val;
1787 7d8406be pbrook
        break;
1788 3d834c78 ths
    case 0x2d: /* DSP[8:15] */
1789 7d8406be pbrook
        s->dsp &= 0xffff00ff;
1790 7d8406be pbrook
        s->dsp |= val << 8;
1791 7d8406be pbrook
        break;
1792 3d834c78 ths
    case 0x2e: /* DSP[16:23] */
1793 7d8406be pbrook
        s->dsp &= 0xff00ffff;
1794 7d8406be pbrook
        s->dsp |= val << 16;
1795 7d8406be pbrook
        break;
1796 3d834c78 ths
    case 0x2f: /* DSP[24:31] */
1797 7d8406be pbrook
        s->dsp &= 0x00ffffff;
1798 7d8406be pbrook
        s->dsp |= val << 24;
1799 7d8406be pbrook
        if ((s->dmode & LSI_DMODE_MAN) == 0
1800 7d8406be pbrook
            && (s->istat1 & LSI_ISTAT1_SRUN) == 0)
1801 7d8406be pbrook
            lsi_execute_script(s);
1802 7d8406be pbrook
        break;
1803 7d8406be pbrook
    CASE_SET_REG32(dsps, 0x30)
1804 7d8406be pbrook
    CASE_SET_REG32(scratch[0], 0x34)
1805 7d8406be pbrook
    case 0x38: /* DMODE */
1806 7d8406be pbrook
        if (val & (LSI_DMODE_SIOM | LSI_DMODE_DIOM)) {
1807 7d8406be pbrook
            BADF("IO mappings not implemented\n");
1808 7d8406be pbrook
        }
1809 7d8406be pbrook
        s->dmode = val;
1810 7d8406be pbrook
        break;
1811 7d8406be pbrook
    case 0x39: /* DIEN */
1812 7d8406be pbrook
        s->dien = val;
1813 7d8406be pbrook
        lsi_update_irq(s);
1814 7d8406be pbrook
        break;
1815 bd8ee11a Sebastian Herbszt
    case 0x3a: /* SBR */
1816 bd8ee11a Sebastian Herbszt
        s->sbr = val;
1817 bd8ee11a Sebastian Herbszt
        break;
1818 7d8406be pbrook
    case 0x3b: /* DCNTL */
1819 7d8406be pbrook
        s->dcntl = val & ~(LSI_DCNTL_PFF | LSI_DCNTL_STD);
1820 7d8406be pbrook
        if ((val & LSI_DCNTL_STD) && (s->istat1 & LSI_ISTAT1_SRUN) == 0)
1821 7d8406be pbrook
            lsi_execute_script(s);
1822 7d8406be pbrook
        break;
1823 7d8406be pbrook
    case 0x40: /* SIEN0 */
1824 7d8406be pbrook
        s->sien0 = val;
1825 7d8406be pbrook
        lsi_update_irq(s);
1826 7d8406be pbrook
        break;
1827 7d8406be pbrook
    case 0x41: /* SIEN1 */
1828 7d8406be pbrook
        s->sien1 = val;
1829 7d8406be pbrook
        lsi_update_irq(s);
1830 7d8406be pbrook
        break;
1831 7d8406be pbrook
    case 0x47: /* GPCNTL0 */
1832 7d8406be pbrook
        break;
1833 7d8406be pbrook
    case 0x48: /* STIME0 */
1834 7d8406be pbrook
        s->stime0 = val;
1835 7d8406be pbrook
        break;
1836 7d8406be pbrook
    case 0x49: /* STIME1 */
1837 7d8406be pbrook
        if (val & 0xf) {
1838 7d8406be pbrook
            DPRINTF("General purpose timer not implemented\n");
1839 7d8406be pbrook
            /* ??? Raising the interrupt immediately seems to be sufficient
1840 7d8406be pbrook
               to keep the FreeBSD driver happy.  */
1841 7d8406be pbrook
            lsi_script_scsi_interrupt(s, 0, LSI_SIST1_GEN);
1842 7d8406be pbrook
        }
1843 7d8406be pbrook
        break;
1844 7d8406be pbrook
    case 0x4a: /* RESPID0 */
1845 7d8406be pbrook
        s->respid0 = val;
1846 7d8406be pbrook
        break;
1847 7d8406be pbrook
    case 0x4b: /* RESPID1 */
1848 7d8406be pbrook
        s->respid1 = val;
1849 7d8406be pbrook
        break;
1850 7d8406be pbrook
    case 0x4d: /* STEST1 */
1851 7d8406be pbrook
        s->stest1 = val;
1852 7d8406be pbrook
        break;
1853 7d8406be pbrook
    case 0x4e: /* STEST2 */
1854 7d8406be pbrook
        if (val & 1) {
1855 7d8406be pbrook
            BADF("Low level mode not implemented\n");
1856 7d8406be pbrook
        }
1857 7d8406be pbrook
        s->stest2 = val;
1858 7d8406be pbrook
        break;
1859 7d8406be pbrook
    case 0x4f: /* STEST3 */
1860 7d8406be pbrook
        if (val & 0x41) {
1861 7d8406be pbrook
            BADF("SCSI FIFO test mode not implemented\n");
1862 7d8406be pbrook
        }
1863 7d8406be pbrook
        s->stest3 = val;
1864 7d8406be pbrook
        break;
1865 7d8406be pbrook
    case 0x56: /* CCNTL0 */
1866 7d8406be pbrook
        s->ccntl0 = val;
1867 7d8406be pbrook
        break;
1868 7d8406be pbrook
    case 0x57: /* CCNTL1 */
1869 7d8406be pbrook
        s->ccntl1 = val;
1870 7d8406be pbrook
        break;
1871 7d8406be pbrook
    CASE_SET_REG32(mmrs, 0xa0)
1872 7d8406be pbrook
    CASE_SET_REG32(mmws, 0xa4)
1873 7d8406be pbrook
    CASE_SET_REG32(sfs, 0xa8)
1874 7d8406be pbrook
    CASE_SET_REG32(drs, 0xac)
1875 7d8406be pbrook
    CASE_SET_REG32(sbms, 0xb0)
1876 ab57d967 aliguori
    CASE_SET_REG32(dbms, 0xb4)
1877 7d8406be pbrook
    CASE_SET_REG32(dnad64, 0xb8)
1878 7d8406be pbrook
    CASE_SET_REG32(pmjad1, 0xc0)
1879 7d8406be pbrook
    CASE_SET_REG32(pmjad2, 0xc4)
1880 7d8406be pbrook
    CASE_SET_REG32(rbc, 0xc8)
1881 7d8406be pbrook
    CASE_SET_REG32(ua, 0xcc)
1882 7d8406be pbrook
    CASE_SET_REG32(ia, 0xd4)
1883 7d8406be pbrook
    CASE_SET_REG32(sbc, 0xd8)
1884 7d8406be pbrook
    CASE_SET_REG32(csbc, 0xdc)
1885 7d8406be pbrook
    default:
1886 7d8406be pbrook
        if (offset >= 0x5c && offset < 0xa0) {
1887 7d8406be pbrook
            int n;
1888 7d8406be pbrook
            int shift;
1889 7d8406be pbrook
            n = (offset - 0x58) >> 2;
1890 7d8406be pbrook
            shift = (offset & 3) * 8;
1891 7d8406be pbrook
            s->scratch[n] &= ~(0xff << shift);
1892 7d8406be pbrook
            s->scratch[n] |= (val & 0xff) << shift;
1893 7d8406be pbrook
        } else {
1894 7d8406be pbrook
            BADF("Unhandled writeb 0x%x = 0x%x\n", offset, val);
1895 7d8406be pbrook
        }
1896 7d8406be pbrook
    }
1897 49c47daa Sebastian Herbszt
#undef CASE_SET_REG24
1898 7d8406be pbrook
#undef CASE_SET_REG32
1899 7d8406be pbrook
}
1900 7d8406be pbrook
1901 b0ce84e5 Avi Kivity
static void lsi_mmio_write(void *opaque, target_phys_addr_t addr,
1902 b0ce84e5 Avi Kivity
                           uint64_t val, unsigned size)
1903 7d8406be pbrook
{
1904 eb40f984 Juan Quintela
    LSIState *s = opaque;
1905 7d8406be pbrook
1906 7d8406be pbrook
    lsi_reg_writeb(s, addr & 0xff, val);
1907 7d8406be pbrook
}
1908 7d8406be pbrook
1909 b0ce84e5 Avi Kivity
static uint64_t lsi_mmio_read(void *opaque, target_phys_addr_t addr,
1910 b0ce84e5 Avi Kivity
                              unsigned size)
1911 7d8406be pbrook
{
1912 eb40f984 Juan Quintela
    LSIState *s = opaque;
1913 7d8406be pbrook
1914 7d8406be pbrook
    return lsi_reg_readb(s, addr & 0xff);
1915 7d8406be pbrook
}
1916 7d8406be pbrook
1917 b0ce84e5 Avi Kivity
static const MemoryRegionOps lsi_mmio_ops = {
1918 b0ce84e5 Avi Kivity
    .read = lsi_mmio_read,
1919 b0ce84e5 Avi Kivity
    .write = lsi_mmio_write,
1920 b0ce84e5 Avi Kivity
    .endianness = DEVICE_NATIVE_ENDIAN,
1921 b0ce84e5 Avi Kivity
    .impl = {
1922 b0ce84e5 Avi Kivity
        .min_access_size = 1,
1923 b0ce84e5 Avi Kivity
        .max_access_size = 1,
1924 b0ce84e5 Avi Kivity
    },
1925 7d8406be pbrook
};
1926 7d8406be pbrook
1927 b0ce84e5 Avi Kivity
static void lsi_ram_write(void *opaque, target_phys_addr_t addr,
1928 b0ce84e5 Avi Kivity
                          uint64_t val, unsigned size)
1929 7d8406be pbrook
{
1930 eb40f984 Juan Quintela
    LSIState *s = opaque;
1931 7d8406be pbrook
    uint32_t newval;
1932 b0ce84e5 Avi Kivity
    uint32_t mask;
1933 7d8406be pbrook
    int shift;
1934 7d8406be pbrook
1935 7d8406be pbrook
    newval = s->script_ram[addr >> 2];
1936 7d8406be pbrook
    shift = (addr & 3) * 8;
1937 b0ce84e5 Avi Kivity
    mask = ((uint64_t)1 << (size * 8)) - 1;
1938 b0ce84e5 Avi Kivity
    newval &= ~(mask << shift);
1939 7d8406be pbrook
    newval |= val << shift;
1940 7d8406be pbrook
    s->script_ram[addr >> 2] = newval;
1941 7d8406be pbrook
}
1942 7d8406be pbrook
1943 b0ce84e5 Avi Kivity
static uint64_t lsi_ram_read(void *opaque, target_phys_addr_t addr,
1944 b0ce84e5 Avi Kivity
                             unsigned size)
1945 7d8406be pbrook
{
1946 eb40f984 Juan Quintela
    LSIState *s = opaque;
1947 7d8406be pbrook
    uint32_t val;
1948 b0ce84e5 Avi Kivity
    uint32_t mask;
1949 7d8406be pbrook
1950 7d8406be pbrook
    val = s->script_ram[addr >> 2];
1951 b0ce84e5 Avi Kivity
    mask = ((uint64_t)1 << (size * 8)) - 1;
1952 7d8406be pbrook
    val >>= (addr & 3) * 8;
1953 b0ce84e5 Avi Kivity
    return val & mask;
1954 7d8406be pbrook
}
1955 7d8406be pbrook
1956 b0ce84e5 Avi Kivity
static const MemoryRegionOps lsi_ram_ops = {
1957 b0ce84e5 Avi Kivity
    .read = lsi_ram_read,
1958 b0ce84e5 Avi Kivity
    .write = lsi_ram_write,
1959 b0ce84e5 Avi Kivity
    .endianness = DEVICE_NATIVE_ENDIAN,
1960 7d8406be pbrook
};
1961 7d8406be pbrook
1962 b0ce84e5 Avi Kivity
static uint64_t lsi_io_read(void *opaque, target_phys_addr_t addr,
1963 b0ce84e5 Avi Kivity
                            unsigned size)
1964 7d8406be pbrook
{
1965 eb40f984 Juan Quintela
    LSIState *s = opaque;
1966 7d8406be pbrook
    return lsi_reg_readb(s, addr & 0xff);
1967 7d8406be pbrook
}
1968 7d8406be pbrook
1969 b0ce84e5 Avi Kivity
static void lsi_io_write(void *opaque, target_phys_addr_t addr,
1970 b0ce84e5 Avi Kivity
                         uint64_t val, unsigned size)
1971 7d8406be pbrook
{
1972 eb40f984 Juan Quintela
    LSIState *s = opaque;
1973 7d8406be pbrook
    lsi_reg_writeb(s, addr & 0xff, val);
1974 7d8406be pbrook
}
1975 7d8406be pbrook
1976 b0ce84e5 Avi Kivity
static const MemoryRegionOps lsi_io_ops = {
1977 b0ce84e5 Avi Kivity
    .read = lsi_io_read,
1978 b0ce84e5 Avi Kivity
    .write = lsi_io_write,
1979 b0ce84e5 Avi Kivity
    .endianness = DEVICE_NATIVE_ENDIAN,
1980 b0ce84e5 Avi Kivity
    .impl = {
1981 b0ce84e5 Avi Kivity
        .min_access_size = 1,
1982 b0ce84e5 Avi Kivity
        .max_access_size = 1,
1983 b0ce84e5 Avi Kivity
    },
1984 b0ce84e5 Avi Kivity
};
1985 7d8406be pbrook
1986 54eefd72 Jan Kiszka
static void lsi_scsi_reset(DeviceState *dev)
1987 54eefd72 Jan Kiszka
{
1988 54eefd72 Jan Kiszka
    LSIState *s = DO_UPCAST(LSIState, dev.qdev, dev);
1989 54eefd72 Jan Kiszka
1990 54eefd72 Jan Kiszka
    lsi_soft_reset(s);
1991 54eefd72 Jan Kiszka
}
1992 54eefd72 Jan Kiszka
1993 4a1b0f1c Juan Quintela
static void lsi_pre_save(void *opaque)
1994 777aec7a Nolan
{
1995 777aec7a Nolan
    LSIState *s = opaque;
1996 777aec7a Nolan
1997 b96a0da0 Gerd Hoffmann
    if (s->current) {
1998 b96a0da0 Gerd Hoffmann
        assert(s->current->dma_buf == NULL);
1999 b96a0da0 Gerd Hoffmann
        assert(s->current->dma_len == 0);
2000 b96a0da0 Gerd Hoffmann
    }
2001 042ec49d Gerd Hoffmann
    assert(QTAILQ_EMPTY(&s->queue));
2002 777aec7a Nolan
}
2003 777aec7a Nolan
2004 4a1b0f1c Juan Quintela
static const VMStateDescription vmstate_lsi_scsi = {
2005 4a1b0f1c Juan Quintela
    .name = "lsiscsi",
2006 4a1b0f1c Juan Quintela
    .version_id = 0,
2007 4a1b0f1c Juan Quintela
    .minimum_version_id = 0,
2008 4a1b0f1c Juan Quintela
    .minimum_version_id_old = 0,
2009 4a1b0f1c Juan Quintela
    .pre_save = lsi_pre_save,
2010 4a1b0f1c Juan Quintela
    .fields      = (VMStateField []) {
2011 4a1b0f1c Juan Quintela
        VMSTATE_PCI_DEVICE(dev, LSIState),
2012 4a1b0f1c Juan Quintela
2013 4a1b0f1c Juan Quintela
        VMSTATE_INT32(carry, LSIState),
2014 2f172849 Hannes Reinecke
        VMSTATE_INT32(status, LSIState),
2015 4a1b0f1c Juan Quintela
        VMSTATE_INT32(msg_action, LSIState),
2016 4a1b0f1c Juan Quintela
        VMSTATE_INT32(msg_len, LSIState),
2017 4a1b0f1c Juan Quintela
        VMSTATE_BUFFER(msg, LSIState),
2018 4a1b0f1c Juan Quintela
        VMSTATE_INT32(waiting, LSIState),
2019 4a1b0f1c Juan Quintela
2020 4a1b0f1c Juan Quintela
        VMSTATE_UINT32(dsa, LSIState),
2021 4a1b0f1c Juan Quintela
        VMSTATE_UINT32(temp, LSIState),
2022 4a1b0f1c Juan Quintela
        VMSTATE_UINT32(dnad, LSIState),
2023 4a1b0f1c Juan Quintela
        VMSTATE_UINT32(dbc, LSIState),
2024 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(istat0, LSIState),
2025 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(istat1, LSIState),
2026 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(dcmd, LSIState),
2027 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(dstat, LSIState),
2028 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(dien, LSIState),
2029 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(sist0, LSIState),
2030 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(sist1, LSIState),
2031 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(sien0, LSIState),
2032 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(sien1, LSIState),
2033 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(mbox0, LSIState),
2034 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(mbox1, LSIState),
2035 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(dfifo, LSIState),
2036 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(ctest2, LSIState),
2037 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(ctest3, LSIState),
2038 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(ctest4, LSIState),
2039 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(ctest5, LSIState),
2040 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(ccntl0, LSIState),
2041 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(ccntl1, LSIState),
2042 4a1b0f1c Juan Quintela
        VMSTATE_UINT32(dsp, LSIState),
2043 4a1b0f1c Juan Quintela
        VMSTATE_UINT32(dsps, LSIState),
2044 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(dmode, LSIState),
2045 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(dcntl, LSIState),
2046 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(scntl0, LSIState),
2047 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(scntl1, LSIState),
2048 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(scntl2, LSIState),
2049 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(scntl3, LSIState),
2050 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(sstat0, LSIState),
2051 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(sstat1, LSIState),
2052 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(scid, LSIState),
2053 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(sxfer, LSIState),
2054 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(socl, LSIState),
2055 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(sdid, LSIState),
2056 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(ssid, LSIState),
2057 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(sfbr, LSIState),
2058 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(stest1, LSIState),
2059 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(stest2, LSIState),
2060 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(stest3, LSIState),
2061 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(sidl, LSIState),
2062 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(stime0, LSIState),
2063 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(respid0, LSIState),
2064 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(respid1, LSIState),
2065 4a1b0f1c Juan Quintela
        VMSTATE_UINT32(mmrs, LSIState),
2066 4a1b0f1c Juan Quintela
        VMSTATE_UINT32(mmws, LSIState),
2067 4a1b0f1c Juan Quintela
        VMSTATE_UINT32(sfs, LSIState),
2068 4a1b0f1c Juan Quintela
        VMSTATE_UINT32(drs, LSIState),
2069 4a1b0f1c Juan Quintela
        VMSTATE_UINT32(sbms, LSIState),
2070 4a1b0f1c Juan Quintela
        VMSTATE_UINT32(dbms, LSIState),
2071 4a1b0f1c Juan Quintela
        VMSTATE_UINT32(dnad64, LSIState),
2072 4a1b0f1c Juan Quintela
        VMSTATE_UINT32(pmjad1, LSIState),
2073 4a1b0f1c Juan Quintela
        VMSTATE_UINT32(pmjad2, LSIState),
2074 4a1b0f1c Juan Quintela
        VMSTATE_UINT32(rbc, LSIState),
2075 4a1b0f1c Juan Quintela
        VMSTATE_UINT32(ua, LSIState),
2076 4a1b0f1c Juan Quintela
        VMSTATE_UINT32(ia, LSIState),
2077 4a1b0f1c Juan Quintela
        VMSTATE_UINT32(sbc, LSIState),
2078 4a1b0f1c Juan Quintela
        VMSTATE_UINT32(csbc, LSIState),
2079 4a1b0f1c Juan Quintela
        VMSTATE_BUFFER_UNSAFE(scratch, LSIState, 0, 18 * sizeof(uint32_t)),
2080 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(sbr, LSIState),
2081 4a1b0f1c Juan Quintela
2082 4a1b0f1c Juan Quintela
        VMSTATE_BUFFER_UNSAFE(script_ram, LSIState, 0, 2048 * sizeof(uint32_t)),
2083 4a1b0f1c Juan Quintela
        VMSTATE_END_OF_LIST()
2084 777aec7a Nolan
    }
2085 4a1b0f1c Juan Quintela
};
2086 777aec7a Nolan
2087 4b09be85 aliguori
static int lsi_scsi_uninit(PCIDevice *d)
2088 4b09be85 aliguori
{
2089 f305261f Juan Quintela
    LSIState *s = DO_UPCAST(LSIState, dev, d);
2090 4b09be85 aliguori
2091 b0ce84e5 Avi Kivity
    memory_region_destroy(&s->mmio_io);
2092 b0ce84e5 Avi Kivity
    memory_region_destroy(&s->ram_io);
2093 b0ce84e5 Avi Kivity
    memory_region_destroy(&s->io_io);
2094 4b09be85 aliguori
2095 4b09be85 aliguori
    return 0;
2096 4b09be85 aliguori
}
2097 4b09be85 aliguori
2098 cfdc1bb0 Paolo Bonzini
static const struct SCSIBusOps lsi_scsi_ops = {
2099 c6df7102 Paolo Bonzini
    .transfer_data = lsi_transfer_data,
2100 94d3f98a Paolo Bonzini
    .complete = lsi_command_complete,
2101 94d3f98a Paolo Bonzini
    .cancel = lsi_request_cancelled
2102 cfdc1bb0 Paolo Bonzini
};
2103 cfdc1bb0 Paolo Bonzini
2104 81a322d4 Gerd Hoffmann
static int lsi_scsi_init(PCIDevice *dev)
2105 7d8406be pbrook
{
2106 f305261f Juan Quintela
    LSIState *s = DO_UPCAST(LSIState, dev, dev);
2107 deb54399 aliguori
    uint8_t *pci_conf;
2108 7d8406be pbrook
2109 f305261f Juan Quintela
    pci_conf = s->dev.config;
2110 deb54399 aliguori
2111 9167a69a balrog
    /* PCI latency timer = 255 */
2112 5845f0e5 Michael S. Tsirkin
    pci_conf[PCI_LATENCY_TIMER] = 0xff;
2113 5845f0e5 Michael S. Tsirkin
    /* TODO: RST# value should be 0 */
2114 9167a69a balrog
    /* Interrupt pin 1 */
2115 5845f0e5 Michael S. Tsirkin
    pci_conf[PCI_INTERRUPT_PIN] = 0x01;
2116 7d8406be pbrook
2117 b0ce84e5 Avi Kivity
    memory_region_init_io(&s->mmio_io, &lsi_mmio_ops, s, "lsi-mmio", 0x400);
2118 b0ce84e5 Avi Kivity
    memory_region_init_io(&s->ram_io, &lsi_ram_ops, s, "lsi-ram", 0x2000);
2119 b0ce84e5 Avi Kivity
    memory_region_init_io(&s->io_io, &lsi_io_ops, s, "lsi-io", 256);
2120 b0ce84e5 Avi Kivity
2121 e824b2cc Avi Kivity
    pci_register_bar(&s->dev, 0, PCI_BASE_ADDRESS_SPACE_IO, &s->io_io);
2122 e824b2cc Avi Kivity
    pci_register_bar(&s->dev, 1, 0, &s->mmio_io);
2123 e824b2cc Avi Kivity
    pci_register_bar(&s->dev, 2, PCI_BASE_ADDRESS_SPACE_MEMORY, &s->ram_io);
2124 042ec49d Gerd Hoffmann
    QTAILQ_INIT(&s->queue);
2125 7d8406be pbrook
2126 cfdc1bb0 Paolo Bonzini
    scsi_bus_new(&s->bus, &dev->qdev, 1, LSI_MAX_DEVS, &lsi_scsi_ops);
2127 5b684b5a Gerd Hoffmann
    if (!dev->qdev.hotplugged) {
2128 fa66b909 Markus Armbruster
        return scsi_bus_legacy_handle_cmdline(&s->bus);
2129 5b684b5a Gerd Hoffmann
    }
2130 81a322d4 Gerd Hoffmann
    return 0;
2131 7d8406be pbrook
}
2132 9be5dafe Paul Brook
2133 0aab0d3a Gerd Hoffmann
static PCIDeviceInfo lsi_info = {
2134 d52affa7 Gerd Hoffmann
    .qdev.name  = "lsi53c895a",
2135 d52affa7 Gerd Hoffmann
    .qdev.alias = "lsi",
2136 d52affa7 Gerd Hoffmann
    .qdev.size  = sizeof(LSIState),
2137 54eefd72 Jan Kiszka
    .qdev.reset = lsi_scsi_reset,
2138 be73cfe2 Juan Quintela
    .qdev.vmsd  = &vmstate_lsi_scsi,
2139 d52affa7 Gerd Hoffmann
    .init       = lsi_scsi_init,
2140 e3936fa5 Gerd Hoffmann
    .exit       = lsi_scsi_uninit,
2141 af5374aa Isaku Yamahata
    .vendor_id  = PCI_VENDOR_ID_LSI_LOGIC,
2142 af5374aa Isaku Yamahata
    .device_id  = PCI_DEVICE_ID_LSI_53C895A,
2143 af5374aa Isaku Yamahata
    .class_id   = PCI_CLASS_STORAGE_SCSI,
2144 af5374aa Isaku Yamahata
    .subsystem_id = 0x1000,
2145 0aab0d3a Gerd Hoffmann
};
2146 0aab0d3a Gerd Hoffmann
2147 9be5dafe Paul Brook
static void lsi53c895a_register_devices(void)
2148 9be5dafe Paul Brook
{
2149 0aab0d3a Gerd Hoffmann
    pci_qdev_register(&lsi_info);
2150 9be5dafe Paul Brook
}
2151 9be5dafe Paul Brook
2152 9be5dafe Paul Brook
device_init(lsi53c895a_register_devices);