Statistics
| Branch: | Revision:

root / hw / lsi53c895a.c @ fc4f0754

History | View | Annotate | Download (65.6 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 7d8406be pbrook
 * This code is licenced 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 7d8406be pbrook
    int mmio_io_addr;
189 7d8406be pbrook
    int ram_io_addr;
190 7d8406be pbrook
    uint32_t script_ram_base;
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 51336214 Jan Kiszka
        qemu_free(p);
357 51336214 Jan Kiszka
    }
358 51336214 Jan Kiszka
    if (s->current) {
359 51336214 Jan Kiszka
        qemu_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 7d8406be pbrook
    /* Optimize reading from SCRIPTS RAM.  */
395 7d8406be pbrook
    if ((addr & 0xffffe000) == s->script_ram_base) {
396 7d8406be pbrook
        return s->script_ram[(addr & 0x1fff) >> 2];
397 7d8406be pbrook
    }
398 7d8406be pbrook
    cpu_physical_memory_read(addr, (uint8_t *)&buf, 4);
399 7d8406be pbrook
    return cpu_to_le32(buf);
400 7d8406be pbrook
}
401 7d8406be pbrook
402 7d8406be pbrook
static void lsi_stop_script(LSIState *s)
403 7d8406be pbrook
{
404 7d8406be pbrook
    s->istat1 &= ~LSI_ISTAT1_SRUN;
405 7d8406be pbrook
}
406 7d8406be pbrook
407 7d8406be pbrook
static void lsi_update_irq(LSIState *s)
408 7d8406be pbrook
{
409 7d8406be pbrook
    int level;
410 7d8406be pbrook
    static int last_level;
411 042ec49d Gerd Hoffmann
    lsi_request *p;
412 7d8406be pbrook
413 7d8406be pbrook
    /* It's unclear whether the DIP/SIP bits should be cleared when the
414 7d8406be pbrook
       Interrupt Status Registers are cleared or when istat0 is read.
415 7d8406be pbrook
       We currently do the formwer, which seems to work.  */
416 7d8406be pbrook
    level = 0;
417 7d8406be pbrook
    if (s->dstat) {
418 7d8406be pbrook
        if (s->dstat & s->dien)
419 7d8406be pbrook
            level = 1;
420 7d8406be pbrook
        s->istat0 |= LSI_ISTAT0_DIP;
421 7d8406be pbrook
    } else {
422 7d8406be pbrook
        s->istat0 &= ~LSI_ISTAT0_DIP;
423 7d8406be pbrook
    }
424 7d8406be pbrook
425 7d8406be pbrook
    if (s->sist0 || s->sist1) {
426 7d8406be pbrook
        if ((s->sist0 & s->sien0) || (s->sist1 & s->sien1))
427 7d8406be pbrook
            level = 1;
428 7d8406be pbrook
        s->istat0 |= LSI_ISTAT0_SIP;
429 7d8406be pbrook
    } else {
430 7d8406be pbrook
        s->istat0 &= ~LSI_ISTAT0_SIP;
431 7d8406be pbrook
    }
432 7d8406be pbrook
    if (s->istat0 & LSI_ISTAT0_INTF)
433 7d8406be pbrook
        level = 1;
434 7d8406be pbrook
435 7d8406be pbrook
    if (level != last_level) {
436 7d8406be pbrook
        DPRINTF("Update IRQ level %d dstat %02x sist %02x%02x\n",
437 7d8406be pbrook
                level, s->dstat, s->sist1, s->sist0);
438 7d8406be pbrook
        last_level = level;
439 7d8406be pbrook
    }
440 f305261f Juan Quintela
    qemu_set_irq(s->dev.irq[0], level);
441 e560125e Laszlo Ast
442 e560125e Laszlo Ast
    if (!level && lsi_irq_on_rsl(s) && !(s->scntl1 & LSI_SCNTL1_CON)) {
443 e560125e Laszlo Ast
        DPRINTF("Handled IRQs & disconnected, looking for pending "
444 e560125e Laszlo Ast
                "processes\n");
445 042ec49d Gerd Hoffmann
        QTAILQ_FOREACH(p, &s->queue, next) {
446 042ec49d Gerd Hoffmann
            if (p->pending) {
447 aa4d32c4 Gerd Hoffmann
                lsi_reselect(s, p);
448 e560125e Laszlo Ast
                break;
449 e560125e Laszlo Ast
            }
450 e560125e Laszlo Ast
        }
451 e560125e Laszlo Ast
    }
452 7d8406be pbrook
}
453 7d8406be pbrook
454 7d8406be pbrook
/* Stop SCRIPTS execution and raise a SCSI interrupt.  */
455 7d8406be pbrook
static void lsi_script_scsi_interrupt(LSIState *s, int stat0, int stat1)
456 7d8406be pbrook
{
457 7d8406be pbrook
    uint32_t mask0;
458 7d8406be pbrook
    uint32_t mask1;
459 7d8406be pbrook
460 7d8406be pbrook
    DPRINTF("SCSI Interrupt 0x%02x%02x prev 0x%02x%02x\n",
461 7d8406be pbrook
            stat1, stat0, s->sist1, s->sist0);
462 7d8406be pbrook
    s->sist0 |= stat0;
463 7d8406be pbrook
    s->sist1 |= stat1;
464 7d8406be pbrook
    /* Stop processor on fatal or unmasked interrupt.  As a special hack
465 7d8406be pbrook
       we don't stop processing when raising STO.  Instead continue
466 7d8406be pbrook
       execution and stop at the next insn that accesses the SCSI bus.  */
467 7d8406be pbrook
    mask0 = s->sien0 | ~(LSI_SIST0_CMP | LSI_SIST0_SEL | LSI_SIST0_RSL);
468 7d8406be pbrook
    mask1 = s->sien1 | ~(LSI_SIST1_GEN | LSI_SIST1_HTH);
469 7d8406be pbrook
    mask1 &= ~LSI_SIST1_STO;
470 7d8406be pbrook
    if (s->sist0 & mask0 || s->sist1 & mask1) {
471 7d8406be pbrook
        lsi_stop_script(s);
472 7d8406be pbrook
    }
473 7d8406be pbrook
    lsi_update_irq(s);
474 7d8406be pbrook
}
475 7d8406be pbrook
476 7d8406be pbrook
/* Stop SCRIPTS execution and raise a DMA interrupt.  */
477 7d8406be pbrook
static void lsi_script_dma_interrupt(LSIState *s, int stat)
478 7d8406be pbrook
{
479 7d8406be pbrook
    DPRINTF("DMA Interrupt 0x%x prev 0x%x\n", stat, s->dstat);
480 7d8406be pbrook
    s->dstat |= stat;
481 7d8406be pbrook
    lsi_update_irq(s);
482 7d8406be pbrook
    lsi_stop_script(s);
483 7d8406be pbrook
}
484 7d8406be pbrook
485 7d8406be pbrook
static inline void lsi_set_phase(LSIState *s, int phase)
486 7d8406be pbrook
{
487 7d8406be pbrook
    s->sstat1 = (s->sstat1 & ~PHASE_MASK) | phase;
488 7d8406be pbrook
}
489 7d8406be pbrook
490 7d8406be pbrook
static void lsi_bad_phase(LSIState *s, int out, int new_phase)
491 7d8406be pbrook
{
492 7d8406be pbrook
    /* Trigger a phase mismatch.  */
493 7d8406be pbrook
    if (s->ccntl0 & LSI_CCNTL0_ENPMJ) {
494 d1d74664 Paolo Bonzini
        if ((s->ccntl0 & LSI_CCNTL0_PMJCTL)) {
495 d1d74664 Paolo Bonzini
            s->dsp = out ? s->pmjad1 : s->pmjad2;
496 7d8406be pbrook
        } else {
497 d1d74664 Paolo Bonzini
            s->dsp = (s->scntl2 & LSI_SCNTL2_WSR ? s->pmjad2 : s->pmjad1);
498 7d8406be pbrook
        }
499 7d8406be pbrook
        DPRINTF("Data phase mismatch jump to %08x\n", s->dsp);
500 7d8406be pbrook
    } else {
501 7d8406be pbrook
        DPRINTF("Phase mismatch interrupt\n");
502 7d8406be pbrook
        lsi_script_scsi_interrupt(s, LSI_SIST0_MA, 0);
503 7d8406be pbrook
        lsi_stop_script(s);
504 7d8406be pbrook
    }
505 7d8406be pbrook
    lsi_set_phase(s, new_phase);
506 7d8406be pbrook
}
507 7d8406be pbrook
508 a917d384 pbrook
509 a917d384 pbrook
/* Resume SCRIPTS execution after a DMA operation.  */
510 a917d384 pbrook
static void lsi_resume_script(LSIState *s)
511 a917d384 pbrook
{
512 a917d384 pbrook
    if (s->waiting != 2) {
513 a917d384 pbrook
        s->waiting = 0;
514 a917d384 pbrook
        lsi_execute_script(s);
515 a917d384 pbrook
    } else {
516 a917d384 pbrook
        s->waiting = 0;
517 a917d384 pbrook
    }
518 a917d384 pbrook
}
519 a917d384 pbrook
520 64d56409 Jan Kiszka
static void lsi_disconnect(LSIState *s)
521 64d56409 Jan Kiszka
{
522 64d56409 Jan Kiszka
    s->scntl1 &= ~LSI_SCNTL1_CON;
523 64d56409 Jan Kiszka
    s->sstat1 &= ~PHASE_MASK;
524 64d56409 Jan Kiszka
}
525 64d56409 Jan Kiszka
526 64d56409 Jan Kiszka
static void lsi_bad_selection(LSIState *s, uint32_t id)
527 64d56409 Jan Kiszka
{
528 64d56409 Jan Kiszka
    DPRINTF("Selected absent target %d\n", id);
529 64d56409 Jan Kiszka
    lsi_script_scsi_interrupt(s, 0, LSI_SIST1_STO);
530 64d56409 Jan Kiszka
    lsi_disconnect(s);
531 64d56409 Jan Kiszka
}
532 64d56409 Jan Kiszka
533 4d611c9a pbrook
/* Initiate a SCSI layer data transfer.  */
534 7d8406be pbrook
static void lsi_do_dma(LSIState *s, int out)
535 7d8406be pbrook
{
536 64d56409 Jan Kiszka
    uint32_t count, id;
537 c227f099 Anthony Liguori
    target_phys_addr_t addr;
538 64d56409 Jan Kiszka
    SCSIDevice *dev;
539 7d8406be pbrook
540 b96a0da0 Gerd Hoffmann
    assert(s->current);
541 b96a0da0 Gerd Hoffmann
    if (!s->current->dma_len) {
542 a917d384 pbrook
        /* Wait until data is available.  */
543 a917d384 pbrook
        DPRINTF("DMA no data available\n");
544 a917d384 pbrook
        return;
545 7d8406be pbrook
    }
546 7d8406be pbrook
547 259d5577 Jan Kiszka
    id = (s->current->tag >> 8) & 0xf;
548 64d56409 Jan Kiszka
    dev = s->bus.devs[id];
549 64d56409 Jan Kiszka
    if (!dev) {
550 64d56409 Jan Kiszka
        lsi_bad_selection(s, id);
551 64d56409 Jan Kiszka
        return;
552 64d56409 Jan Kiszka
    }
553 64d56409 Jan Kiszka
554 a917d384 pbrook
    count = s->dbc;
555 b96a0da0 Gerd Hoffmann
    if (count > s->current->dma_len)
556 b96a0da0 Gerd Hoffmann
        count = s->current->dma_len;
557 a917d384 pbrook
558 a917d384 pbrook
    addr = s->dnad;
559 dd8edf01 aliguori
    /* both 40 and Table Indirect 64-bit DMAs store upper bits in dnad64 */
560 dd8edf01 aliguori
    if (lsi_dma_40bit(s) || lsi_dma_ti64bit(s))
561 b25cf589 aliguori
        addr |= ((uint64_t)s->dnad64 << 32);
562 dd8edf01 aliguori
    else if (s->dbms)
563 dd8edf01 aliguori
        addr |= ((uint64_t)s->dbms << 32);
564 b25cf589 aliguori
    else if (s->sbms)
565 b25cf589 aliguori
        addr |= ((uint64_t)s->sbms << 32);
566 b25cf589 aliguori
567 3adae656 aliguori
    DPRINTF("DMA addr=0x" TARGET_FMT_plx " len=%d\n", addr, count);
568 7d8406be pbrook
    s->csbc += count;
569 a917d384 pbrook
    s->dnad += count;
570 a917d384 pbrook
    s->dbc -= count;
571 5c6c0e51 Hannes Reinecke
     if (s->current->dma_buf == NULL) {
572 5c6c0e51 Hannes Reinecke
        s->current->dma_buf = dev->info->get_buf(s->current->req);
573 a917d384 pbrook
    }
574 7d8406be pbrook
    /* ??? Set SFBR to first data byte.  */
575 a917d384 pbrook
    if (out) {
576 b96a0da0 Gerd Hoffmann
        cpu_physical_memory_read(addr, s->current->dma_buf, count);
577 a917d384 pbrook
    } else {
578 b96a0da0 Gerd Hoffmann
        cpu_physical_memory_write(addr, s->current->dma_buf, count);
579 a917d384 pbrook
    }
580 b96a0da0 Gerd Hoffmann
    s->current->dma_len -= count;
581 b96a0da0 Gerd Hoffmann
    if (s->current->dma_len == 0) {
582 b96a0da0 Gerd Hoffmann
        s->current->dma_buf = NULL;
583 a917d384 pbrook
        if (out) {
584 a917d384 pbrook
            /* Write the data.  */
585 5c6c0e51 Hannes Reinecke
            dev->info->write_data(s->current->req);
586 a917d384 pbrook
        } else {
587 a917d384 pbrook
            /* Request any remaining data.  */
588 5c6c0e51 Hannes Reinecke
            dev->info->read_data(s->current->req);
589 a917d384 pbrook
        }
590 a917d384 pbrook
    } else {
591 b96a0da0 Gerd Hoffmann
        s->current->dma_buf += count;
592 a917d384 pbrook
        lsi_resume_script(s);
593 a917d384 pbrook
    }
594 a917d384 pbrook
}
595 a917d384 pbrook
596 a917d384 pbrook
597 a917d384 pbrook
/* Add a command to the queue.  */
598 a917d384 pbrook
static void lsi_queue_command(LSIState *s)
599 a917d384 pbrook
{
600 af12ac98 Gerd Hoffmann
    lsi_request *p = s->current;
601 a917d384 pbrook
602 aa2b1e89 Bernhard Kohl
    DPRINTF("Queueing tag=0x%x\n", p->tag);
603 af12ac98 Gerd Hoffmann
    assert(s->current != NULL);
604 b96a0da0 Gerd Hoffmann
    assert(s->current->dma_len == 0);
605 af12ac98 Gerd Hoffmann
    QTAILQ_INSERT_TAIL(&s->queue, s->current, next);
606 af12ac98 Gerd Hoffmann
    s->current = NULL;
607 af12ac98 Gerd Hoffmann
608 a917d384 pbrook
    p->pending = 0;
609 a917d384 pbrook
    p->out = (s->sstat1 & PHASE_MASK) == PHASE_DO;
610 a917d384 pbrook
}
611 a917d384 pbrook
612 a917d384 pbrook
/* Queue a byte for a MSG IN phase.  */
613 a917d384 pbrook
static void lsi_add_msg_byte(LSIState *s, uint8_t data)
614 a917d384 pbrook
{
615 a917d384 pbrook
    if (s->msg_len >= LSI_MAX_MSGIN_LEN) {
616 a917d384 pbrook
        BADF("MSG IN data too long\n");
617 4d611c9a pbrook
    } else {
618 a917d384 pbrook
        DPRINTF("MSG IN 0x%02x\n", data);
619 a917d384 pbrook
        s->msg[s->msg_len++] = data;
620 7d8406be pbrook
    }
621 a917d384 pbrook
}
622 a917d384 pbrook
623 a917d384 pbrook
/* Perform reselection to continue a command.  */
624 aa4d32c4 Gerd Hoffmann
static void lsi_reselect(LSIState *s, lsi_request *p)
625 a917d384 pbrook
{
626 a917d384 pbrook
    int id;
627 a917d384 pbrook
628 af12ac98 Gerd Hoffmann
    assert(s->current == NULL);
629 af12ac98 Gerd Hoffmann
    QTAILQ_REMOVE(&s->queue, p, next);
630 af12ac98 Gerd Hoffmann
    s->current = p;
631 af12ac98 Gerd Hoffmann
632 aa4d32c4 Gerd Hoffmann
    id = (p->tag >> 8) & 0xf;
633 a917d384 pbrook
    s->ssid = id | 0x80;
634 cc9f28bc Laszlo Ast
    /* LSI53C700 Family Compatibility, see LSI53C895A 4-73 */
635 f6dc18df Blue Swirl
    if (!(s->dcntl & LSI_DCNTL_COM)) {
636 cc9f28bc Laszlo Ast
        s->sfbr = 1 << (id & 0x7);
637 cc9f28bc Laszlo Ast
    }
638 a917d384 pbrook
    DPRINTF("Reselected target %d\n", id);
639 a917d384 pbrook
    s->scntl1 |= LSI_SCNTL1_CON;
640 a917d384 pbrook
    lsi_set_phase(s, PHASE_MI);
641 a917d384 pbrook
    s->msg_action = p->out ? 2 : 3;
642 b96a0da0 Gerd Hoffmann
    s->current->dma_len = p->pending;
643 a917d384 pbrook
    lsi_add_msg_byte(s, 0x80);
644 af12ac98 Gerd Hoffmann
    if (s->current->tag & LSI_TAG_VALID) {
645 a917d384 pbrook
        lsi_add_msg_byte(s, 0x20);
646 aa4d32c4 Gerd Hoffmann
        lsi_add_msg_byte(s, p->tag & 0xff);
647 a917d384 pbrook
    }
648 a917d384 pbrook
649 e560125e Laszlo Ast
    if (lsi_irq_on_rsl(s)) {
650 e560125e Laszlo Ast
        lsi_script_scsi_interrupt(s, LSI_SIST0_RSL, 0);
651 e560125e Laszlo Ast
    }
652 a917d384 pbrook
}
653 a917d384 pbrook
654 11257187 Paolo Bonzini
static lsi_request *lsi_find_by_tag(LSIState *s, uint32_t tag)
655 a917d384 pbrook
{
656 042ec49d Gerd Hoffmann
    lsi_request *p;
657 042ec49d Gerd Hoffmann
658 042ec49d Gerd Hoffmann
    QTAILQ_FOREACH(p, &s->queue, next) {
659 a917d384 pbrook
        if (p->tag == tag) {
660 11257187 Paolo Bonzini
            return p;
661 a917d384 pbrook
        }
662 a917d384 pbrook
    }
663 11257187 Paolo Bonzini
664 11257187 Paolo Bonzini
    return NULL;
665 11257187 Paolo Bonzini
}
666 11257187 Paolo Bonzini
667 94d3f98a Paolo Bonzini
static void lsi_request_cancelled(SCSIRequest *req)
668 94d3f98a Paolo Bonzini
{
669 94d3f98a Paolo Bonzini
    LSIState *s = DO_UPCAST(LSIState, dev.qdev, req->bus->qbus.parent);
670 94d3f98a Paolo Bonzini
    lsi_request *p;
671 94d3f98a Paolo Bonzini
672 94d3f98a Paolo Bonzini
    if (s->current && req == s->current->req) {
673 94d3f98a Paolo Bonzini
        scsi_req_unref(req);
674 94d3f98a Paolo Bonzini
        qemu_free(s->current);
675 94d3f98a Paolo Bonzini
        s->current = NULL;
676 94d3f98a Paolo Bonzini
        return;
677 94d3f98a Paolo Bonzini
    }
678 94d3f98a Paolo Bonzini
679 94d3f98a Paolo Bonzini
    p = lsi_find_by_tag(s, req->tag);
680 94d3f98a Paolo Bonzini
    if (p) {
681 94d3f98a Paolo Bonzini
        QTAILQ_REMOVE(&s->queue, p, next);
682 94d3f98a Paolo Bonzini
        scsi_req_unref(req);
683 94d3f98a Paolo Bonzini
        qemu_free(p);
684 94d3f98a Paolo Bonzini
    }
685 94d3f98a Paolo Bonzini
}
686 94d3f98a Paolo Bonzini
687 11257187 Paolo Bonzini
/* Record that data is available for a queued command.  Returns zero if
688 11257187 Paolo Bonzini
   the device was reselected, nonzero if the IO is deferred.  */
689 11257187 Paolo Bonzini
static int lsi_queue_tag(LSIState *s, uint32_t tag, uint32_t arg)
690 11257187 Paolo Bonzini
{
691 11257187 Paolo Bonzini
    lsi_request *p;
692 11257187 Paolo Bonzini
693 11257187 Paolo Bonzini
    p = lsi_find_by_tag(s, tag);
694 11257187 Paolo Bonzini
    if (!p) {
695 11257187 Paolo Bonzini
        BADF("IO with unknown tag %d\n", tag);
696 11257187 Paolo Bonzini
        return 1;
697 11257187 Paolo Bonzini
    }
698 11257187 Paolo Bonzini
699 11257187 Paolo Bonzini
    if (p->pending) {
700 11257187 Paolo Bonzini
        BADF("Multiple IO pending for tag %d\n", tag);
701 11257187 Paolo Bonzini
    }
702 11257187 Paolo Bonzini
    p->pending = arg;
703 11257187 Paolo Bonzini
    /* Reselect if waiting for it, or if reselection triggers an IRQ
704 11257187 Paolo Bonzini
       and the bus is free.
705 11257187 Paolo Bonzini
       Since no interrupt stacking is implemented in the emulation, it
706 11257187 Paolo Bonzini
       is also required that there are no pending interrupts waiting
707 11257187 Paolo Bonzini
       for service from the device driver. */
708 11257187 Paolo Bonzini
    if (s->waiting == 1 ||
709 11257187 Paolo Bonzini
        (lsi_irq_on_rsl(s) && !(s->scntl1 & LSI_SCNTL1_CON) &&
710 11257187 Paolo Bonzini
         !(s->istat0 & (LSI_ISTAT0_SIP | LSI_ISTAT0_DIP)))) {
711 11257187 Paolo Bonzini
        /* Reselect device.  */
712 11257187 Paolo Bonzini
        lsi_reselect(s, p);
713 11257187 Paolo Bonzini
        return 0;
714 11257187 Paolo Bonzini
    } else {
715 11257187 Paolo Bonzini
        DPRINTF("Queueing IO tag=0x%x\n", tag);
716 11257187 Paolo Bonzini
        p->pending = arg;
717 11257187 Paolo Bonzini
        return 1;
718 11257187 Paolo Bonzini
    }
719 7d8406be pbrook
}
720 5c6c0e51 Hannes Reinecke
 /* Callback to indicate that the SCSI layer has completed a transfer.  */
721 5c6c0e51 Hannes Reinecke
static void lsi_command_complete(SCSIRequest *req, int reason, uint32_t arg)
722 4d611c9a pbrook
{
723 5c6c0e51 Hannes Reinecke
    LSIState *s = DO_UPCAST(LSIState, dev.qdev, req->bus->qbus.parent);
724 4d611c9a pbrook
    int out;
725 4d611c9a pbrook
726 a917d384 pbrook
    out = (s->sstat1 & PHASE_MASK) == PHASE_DO;
727 4d611c9a pbrook
    if (reason == SCSI_REASON_DONE) {
728 2f172849 Hannes Reinecke
        DPRINTF("Command complete status=%d\n", (int)arg);
729 2f172849 Hannes Reinecke
        s->status = arg;
730 8ccc2ace ths
        s->command_complete = 2;
731 a917d384 pbrook
        if (s->waiting && s->dbc != 0) {
732 a917d384 pbrook
            /* Raise phase mismatch for short transfers.  */
733 a917d384 pbrook
            lsi_bad_phase(s, out, PHASE_ST);
734 a917d384 pbrook
        } else {
735 a917d384 pbrook
            lsi_set_phase(s, PHASE_ST);
736 a917d384 pbrook
        }
737 af12ac98 Gerd Hoffmann
738 5c6c0e51 Hannes Reinecke
        if (s->current && req == s->current->req) {
739 5c6c0e51 Hannes Reinecke
            scsi_req_unref(s->current->req);
740 5c6c0e51 Hannes Reinecke
            qemu_free(s->current);
741 5c6c0e51 Hannes Reinecke
            s->current = NULL;
742 5c6c0e51 Hannes Reinecke
        }
743 a917d384 pbrook
        lsi_resume_script(s);
744 a917d384 pbrook
        return;
745 4d611c9a pbrook
    }
746 4d611c9a pbrook
747 5c6c0e51 Hannes Reinecke
    if (s->waiting == 1 || !s->current || req->tag != s->current->tag ||
748 e560125e Laszlo Ast
        (lsi_irq_on_rsl(s) && !(s->scntl1 & LSI_SCNTL1_CON))) {
749 5c6c0e51 Hannes Reinecke
        if (lsi_queue_tag(s, req->tag, arg)) {
750 a917d384 pbrook
            return;
751 5c6c0e51 Hannes Reinecke
        }
752 a917d384 pbrook
    }
753 e560125e Laszlo Ast
754 e560125e Laszlo Ast
    /* host adapter (re)connected */
755 5c6c0e51 Hannes Reinecke
    DPRINTF("Data ready tag=0x%x len=%d\n", req->tag, arg);
756 b96a0da0 Gerd Hoffmann
    s->current->dma_len = arg;
757 8ccc2ace ths
    s->command_complete = 1;
758 a917d384 pbrook
    if (!s->waiting)
759 a917d384 pbrook
        return;
760 a917d384 pbrook
    if (s->waiting == 1 || s->dbc == 0) {
761 a917d384 pbrook
        lsi_resume_script(s);
762 a917d384 pbrook
    } else {
763 4d611c9a pbrook
        lsi_do_dma(s, out);
764 4d611c9a pbrook
    }
765 4d611c9a pbrook
}
766 7d8406be pbrook
767 7d8406be pbrook
static void lsi_do_command(LSIState *s)
768 7d8406be pbrook
{
769 64d56409 Jan Kiszka
    SCSIDevice *dev;
770 7d8406be pbrook
    uint8_t buf[16];
771 64d56409 Jan Kiszka
    uint32_t id;
772 7d8406be pbrook
    int n;
773 7d8406be pbrook
774 7d8406be pbrook
    DPRINTF("Send command len=%d\n", s->dbc);
775 7d8406be pbrook
    if (s->dbc > 16)
776 7d8406be pbrook
        s->dbc = 16;
777 7d8406be pbrook
    cpu_physical_memory_read(s->dnad, buf, s->dbc);
778 7d8406be pbrook
    s->sfbr = buf[0];
779 8ccc2ace ths
    s->command_complete = 0;
780 af12ac98 Gerd Hoffmann
781 259d5577 Jan Kiszka
    id = (s->select_tag >> 8) & 0xf;
782 64d56409 Jan Kiszka
    dev = s->bus.devs[id];
783 64d56409 Jan Kiszka
    if (!dev) {
784 64d56409 Jan Kiszka
        lsi_bad_selection(s, id);
785 64d56409 Jan Kiszka
        return;
786 64d56409 Jan Kiszka
    }
787 64d56409 Jan Kiszka
788 af12ac98 Gerd Hoffmann
    assert(s->current == NULL);
789 af12ac98 Gerd Hoffmann
    s->current = qemu_mallocz(sizeof(lsi_request));
790 af12ac98 Gerd Hoffmann
    s->current->tag = s->select_tag;
791 5c6c0e51 Hannes Reinecke
    s->current->req = dev->info->alloc_req(dev, s->current->tag,
792 5c6c0e51 Hannes Reinecke
                                           s->current_lun);
793 af12ac98 Gerd Hoffmann
794 fc4f0754 Paolo Bonzini
    n = scsi_req_enqueue(s->current->req, buf);
795 7d8406be pbrook
    if (n > 0) {
796 7d8406be pbrook
        lsi_set_phase(s, PHASE_DI);
797 5c6c0e51 Hannes Reinecke
        dev->info->read_data(s->current->req);
798 7d8406be pbrook
    } else if (n < 0) {
799 7d8406be pbrook
        lsi_set_phase(s, PHASE_DO);
800 5c6c0e51 Hannes Reinecke
        dev->info->write_data(s->current->req);
801 a917d384 pbrook
    }
802 8ccc2ace ths
803 8ccc2ace ths
    if (!s->command_complete) {
804 8ccc2ace ths
        if (n) {
805 8ccc2ace ths
            /* Command did not complete immediately so disconnect.  */
806 8ccc2ace ths
            lsi_add_msg_byte(s, 2); /* SAVE DATA POINTER */
807 8ccc2ace ths
            lsi_add_msg_byte(s, 4); /* DISCONNECT */
808 8ccc2ace ths
            /* wait data */
809 8ccc2ace ths
            lsi_set_phase(s, PHASE_MI);
810 8ccc2ace ths
            s->msg_action = 1;
811 8ccc2ace ths
            lsi_queue_command(s);
812 8ccc2ace ths
        } else {
813 8ccc2ace ths
            /* wait command complete */
814 8ccc2ace ths
            lsi_set_phase(s, PHASE_DI);
815 8ccc2ace ths
        }
816 7d8406be pbrook
    }
817 7d8406be pbrook
}
818 7d8406be pbrook
819 7d8406be pbrook
static void lsi_do_status(LSIState *s)
820 7d8406be pbrook
{
821 2f172849 Hannes Reinecke
    uint8_t status;
822 2f172849 Hannes Reinecke
    DPRINTF("Get status len=%d status=%d\n", s->dbc, s->status);
823 7d8406be pbrook
    if (s->dbc != 1)
824 7d8406be pbrook
        BADF("Bad Status move\n");
825 7d8406be pbrook
    s->dbc = 1;
826 2f172849 Hannes Reinecke
    status = s->status;
827 2f172849 Hannes Reinecke
    s->sfbr = status;
828 2f172849 Hannes Reinecke
    cpu_physical_memory_write(s->dnad, &status, 1);
829 7d8406be pbrook
    lsi_set_phase(s, PHASE_MI);
830 a917d384 pbrook
    s->msg_action = 1;
831 a917d384 pbrook
    lsi_add_msg_byte(s, 0); /* COMMAND COMPLETE */
832 7d8406be pbrook
}
833 7d8406be pbrook
834 7d8406be pbrook
static void lsi_do_msgin(LSIState *s)
835 7d8406be pbrook
{
836 a917d384 pbrook
    int len;
837 a917d384 pbrook
    DPRINTF("Message in len=%d/%d\n", s->dbc, s->msg_len);
838 a917d384 pbrook
    s->sfbr = s->msg[0];
839 a917d384 pbrook
    len = s->msg_len;
840 a917d384 pbrook
    if (len > s->dbc)
841 a917d384 pbrook
        len = s->dbc;
842 a917d384 pbrook
    cpu_physical_memory_write(s->dnad, s->msg, len);
843 a917d384 pbrook
    /* Linux drivers rely on the last byte being in the SIDL.  */
844 a917d384 pbrook
    s->sidl = s->msg[len - 1];
845 a917d384 pbrook
    s->msg_len -= len;
846 a917d384 pbrook
    if (s->msg_len) {
847 a917d384 pbrook
        memmove(s->msg, s->msg + len, s->msg_len);
848 7d8406be pbrook
    } else {
849 7d8406be pbrook
        /* ??? Check if ATN (not yet implemented) is asserted and maybe
850 7d8406be pbrook
           switch to PHASE_MO.  */
851 a917d384 pbrook
        switch (s->msg_action) {
852 a917d384 pbrook
        case 0:
853 a917d384 pbrook
            lsi_set_phase(s, PHASE_CMD);
854 a917d384 pbrook
            break;
855 a917d384 pbrook
        case 1:
856 a917d384 pbrook
            lsi_disconnect(s);
857 a917d384 pbrook
            break;
858 a917d384 pbrook
        case 2:
859 a917d384 pbrook
            lsi_set_phase(s, PHASE_DO);
860 a917d384 pbrook
            break;
861 a917d384 pbrook
        case 3:
862 a917d384 pbrook
            lsi_set_phase(s, PHASE_DI);
863 a917d384 pbrook
            break;
864 a917d384 pbrook
        default:
865 a917d384 pbrook
            abort();
866 a917d384 pbrook
        }
867 7d8406be pbrook
    }
868 7d8406be pbrook
}
869 7d8406be pbrook
870 a917d384 pbrook
/* Read the next byte during a MSGOUT phase.  */
871 a917d384 pbrook
static uint8_t lsi_get_msgbyte(LSIState *s)
872 a917d384 pbrook
{
873 a917d384 pbrook
    uint8_t data;
874 a917d384 pbrook
    cpu_physical_memory_read(s->dnad, &data, 1);
875 a917d384 pbrook
    s->dnad++;
876 a917d384 pbrook
    s->dbc--;
877 a917d384 pbrook
    return data;
878 a917d384 pbrook
}
879 a917d384 pbrook
880 444dd39b Stefan Hajnoczi
/* Skip the next n bytes during a MSGOUT phase. */
881 444dd39b Stefan Hajnoczi
static void lsi_skip_msgbytes(LSIState *s, unsigned int n)
882 444dd39b Stefan Hajnoczi
{
883 444dd39b Stefan Hajnoczi
    s->dnad += n;
884 444dd39b Stefan Hajnoczi
    s->dbc  -= n;
885 444dd39b Stefan Hajnoczi
}
886 444dd39b Stefan Hajnoczi
887 7d8406be pbrook
static void lsi_do_msgout(LSIState *s)
888 7d8406be pbrook
{
889 7d8406be pbrook
    uint8_t msg;
890 a917d384 pbrook
    int len;
891 508240c0 Bernhard Kohl
    uint32_t current_tag;
892 508240c0 Bernhard Kohl
    SCSIDevice *current_dev;
893 5c6c0e51 Hannes Reinecke
    lsi_request *current_req, *p, *p_next;
894 508240c0 Bernhard Kohl
    int id;
895 508240c0 Bernhard Kohl
896 508240c0 Bernhard Kohl
    if (s->current) {
897 508240c0 Bernhard Kohl
        current_tag = s->current->tag;
898 5c6c0e51 Hannes Reinecke
        current_req = s->current;
899 508240c0 Bernhard Kohl
    } else {
900 508240c0 Bernhard Kohl
        current_tag = s->select_tag;
901 5c6c0e51 Hannes Reinecke
        current_req = lsi_find_by_tag(s, current_tag);
902 508240c0 Bernhard Kohl
    }
903 508240c0 Bernhard Kohl
    id = (current_tag >> 8) & 0xf;
904 508240c0 Bernhard Kohl
    current_dev = s->bus.devs[id];
905 7d8406be pbrook
906 7d8406be pbrook
    DPRINTF("MSG out len=%d\n", s->dbc);
907 a917d384 pbrook
    while (s->dbc) {
908 a917d384 pbrook
        msg = lsi_get_msgbyte(s);
909 a917d384 pbrook
        s->sfbr = msg;
910 a917d384 pbrook
911 a917d384 pbrook
        switch (msg) {
912 77203ea0 Laszlo Ast
        case 0x04:
913 a917d384 pbrook
            DPRINTF("MSG: Disconnect\n");
914 a917d384 pbrook
            lsi_disconnect(s);
915 a917d384 pbrook
            break;
916 a917d384 pbrook
        case 0x08:
917 a917d384 pbrook
            DPRINTF("MSG: No Operation\n");
918 a917d384 pbrook
            lsi_set_phase(s, PHASE_CMD);
919 a917d384 pbrook
            break;
920 a917d384 pbrook
        case 0x01:
921 a917d384 pbrook
            len = lsi_get_msgbyte(s);
922 a917d384 pbrook
            msg = lsi_get_msgbyte(s);
923 f3f5b867 Blue Swirl
            (void)len; /* avoid a warning about unused variable*/
924 a917d384 pbrook
            DPRINTF("Extended message 0x%x (len %d)\n", msg, len);
925 a917d384 pbrook
            switch (msg) {
926 a917d384 pbrook
            case 1:
927 a917d384 pbrook
                DPRINTF("SDTR (ignored)\n");
928 444dd39b Stefan Hajnoczi
                lsi_skip_msgbytes(s, 2);
929 a917d384 pbrook
                break;
930 a917d384 pbrook
            case 3:
931 a917d384 pbrook
                DPRINTF("WDTR (ignored)\n");
932 444dd39b Stefan Hajnoczi
                lsi_skip_msgbytes(s, 1);
933 a917d384 pbrook
                break;
934 a917d384 pbrook
            default:
935 a917d384 pbrook
                goto bad;
936 a917d384 pbrook
            }
937 a917d384 pbrook
            break;
938 a917d384 pbrook
        case 0x20: /* SIMPLE queue */
939 af12ac98 Gerd Hoffmann
            s->select_tag |= lsi_get_msgbyte(s) | LSI_TAG_VALID;
940 aa2b1e89 Bernhard Kohl
            DPRINTF("SIMPLE queue tag=0x%x\n", s->select_tag & 0xff);
941 a917d384 pbrook
            break;
942 a917d384 pbrook
        case 0x21: /* HEAD of queue */
943 a917d384 pbrook
            BADF("HEAD queue not implemented\n");
944 af12ac98 Gerd Hoffmann
            s->select_tag |= lsi_get_msgbyte(s) | LSI_TAG_VALID;
945 a917d384 pbrook
            break;
946 a917d384 pbrook
        case 0x22: /* ORDERED queue */
947 a917d384 pbrook
            BADF("ORDERED queue not implemented\n");
948 af12ac98 Gerd Hoffmann
            s->select_tag |= lsi_get_msgbyte(s) | LSI_TAG_VALID;
949 a917d384 pbrook
            break;
950 508240c0 Bernhard Kohl
        case 0x0d:
951 508240c0 Bernhard Kohl
            /* The ABORT TAG message clears the current I/O process only. */
952 508240c0 Bernhard Kohl
            DPRINTF("MSG: ABORT TAG tag=0x%x\n", current_tag);
953 5c6c0e51 Hannes Reinecke
            if (current_req) {
954 94d3f98a Paolo Bonzini
                scsi_req_cancel(current_req->req);
955 5c6c0e51 Hannes Reinecke
            }
956 508240c0 Bernhard Kohl
            lsi_disconnect(s);
957 508240c0 Bernhard Kohl
            break;
958 508240c0 Bernhard Kohl
        case 0x06:
959 508240c0 Bernhard Kohl
        case 0x0e:
960 508240c0 Bernhard Kohl
        case 0x0c:
961 508240c0 Bernhard Kohl
            /* The ABORT message clears all I/O processes for the selecting
962 508240c0 Bernhard Kohl
               initiator on the specified logical unit of the target. */
963 508240c0 Bernhard Kohl
            if (msg == 0x06) {
964 508240c0 Bernhard Kohl
                DPRINTF("MSG: ABORT tag=0x%x\n", current_tag);
965 508240c0 Bernhard Kohl
            }
966 508240c0 Bernhard Kohl
            /* The CLEAR QUEUE message clears all I/O processes for all
967 508240c0 Bernhard Kohl
               initiators on the specified logical unit of the target. */
968 508240c0 Bernhard Kohl
            if (msg == 0x0e) {
969 508240c0 Bernhard Kohl
                DPRINTF("MSG: CLEAR QUEUE tag=0x%x\n", current_tag);
970 508240c0 Bernhard Kohl
            }
971 508240c0 Bernhard Kohl
            /* The BUS DEVICE RESET message clears all I/O processes for all
972 508240c0 Bernhard Kohl
               initiators on all logical units of the target. */
973 508240c0 Bernhard Kohl
            if (msg == 0x0c) {
974 508240c0 Bernhard Kohl
                DPRINTF("MSG: BUS DEVICE RESET tag=0x%x\n", current_tag);
975 508240c0 Bernhard Kohl
            }
976 508240c0 Bernhard Kohl
977 508240c0 Bernhard Kohl
            /* clear the current I/O process */
978 5c6c0e51 Hannes Reinecke
            if (s->current) {
979 94d3f98a Paolo Bonzini
                scsi_req_cancel(s->current->req);
980 5c6c0e51 Hannes Reinecke
            }
981 508240c0 Bernhard Kohl
982 508240c0 Bernhard Kohl
            /* As the current implemented devices scsi_disk and scsi_generic
983 508240c0 Bernhard Kohl
               only support one LUN, we don't need to keep track of LUNs.
984 508240c0 Bernhard Kohl
               Clearing I/O processes for other initiators could be possible
985 508240c0 Bernhard Kohl
               for scsi_generic by sending a SG_SCSI_RESET to the /dev/sgX
986 508240c0 Bernhard Kohl
               device, but this is currently not implemented (and seems not
987 508240c0 Bernhard Kohl
               to be really necessary). So let's simply clear all queued
988 508240c0 Bernhard Kohl
               commands for the current device: */
989 508240c0 Bernhard Kohl
            id = current_tag & 0x0000ff00;
990 508240c0 Bernhard Kohl
            QTAILQ_FOREACH_SAFE(p, &s->queue, next, p_next) {
991 508240c0 Bernhard Kohl
                if ((p->tag & 0x0000ff00) == id) {
992 94d3f98a Paolo Bonzini
                    scsi_req_cancel(p->req);
993 508240c0 Bernhard Kohl
                }
994 508240c0 Bernhard Kohl
            }
995 508240c0 Bernhard Kohl
996 508240c0 Bernhard Kohl
            lsi_disconnect(s);
997 508240c0 Bernhard Kohl
            break;
998 a917d384 pbrook
        default:
999 a917d384 pbrook
            if ((msg & 0x80) == 0) {
1000 a917d384 pbrook
                goto bad;
1001 a917d384 pbrook
            }
1002 a917d384 pbrook
            s->current_lun = msg & 7;
1003 a917d384 pbrook
            DPRINTF("Select LUN %d\n", s->current_lun);
1004 a917d384 pbrook
            lsi_set_phase(s, PHASE_CMD);
1005 a917d384 pbrook
            break;
1006 a917d384 pbrook
        }
1007 7d8406be pbrook
    }
1008 a917d384 pbrook
    return;
1009 a917d384 pbrook
bad:
1010 a917d384 pbrook
    BADF("Unimplemented message 0x%02x\n", msg);
1011 a917d384 pbrook
    lsi_set_phase(s, PHASE_MI);
1012 a917d384 pbrook
    lsi_add_msg_byte(s, 7); /* MESSAGE REJECT */
1013 a917d384 pbrook
    s->msg_action = 0;
1014 7d8406be pbrook
}
1015 7d8406be pbrook
1016 7d8406be pbrook
/* Sign extend a 24-bit value.  */
1017 7d8406be pbrook
static inline int32_t sxt24(int32_t n)
1018 7d8406be pbrook
{
1019 7d8406be pbrook
    return (n << 8) >> 8;
1020 7d8406be pbrook
}
1021 7d8406be pbrook
1022 e20a8dff Blue Swirl
#define LSI_BUF_SIZE 4096
1023 7d8406be pbrook
static void lsi_memcpy(LSIState *s, uint32_t dest, uint32_t src, int count)
1024 7d8406be pbrook
{
1025 7d8406be pbrook
    int n;
1026 e20a8dff Blue Swirl
    uint8_t buf[LSI_BUF_SIZE];
1027 7d8406be pbrook
1028 7d8406be pbrook
    DPRINTF("memcpy dest 0x%08x src 0x%08x count %d\n", dest, src, count);
1029 7d8406be pbrook
    while (count) {
1030 e20a8dff Blue Swirl
        n = (count > LSI_BUF_SIZE) ? LSI_BUF_SIZE : count;
1031 7d8406be pbrook
        cpu_physical_memory_read(src, buf, n);
1032 7d8406be pbrook
        cpu_physical_memory_write(dest, buf, n);
1033 7d8406be pbrook
        src += n;
1034 7d8406be pbrook
        dest += n;
1035 7d8406be pbrook
        count -= n;
1036 7d8406be pbrook
    }
1037 7d8406be pbrook
}
1038 7d8406be pbrook
1039 a917d384 pbrook
static void lsi_wait_reselect(LSIState *s)
1040 a917d384 pbrook
{
1041 042ec49d Gerd Hoffmann
    lsi_request *p;
1042 042ec49d Gerd Hoffmann
1043 a917d384 pbrook
    DPRINTF("Wait Reselect\n");
1044 042ec49d Gerd Hoffmann
1045 042ec49d Gerd Hoffmann
    QTAILQ_FOREACH(p, &s->queue, next) {
1046 042ec49d Gerd Hoffmann
        if (p->pending) {
1047 aa4d32c4 Gerd Hoffmann
            lsi_reselect(s, p);
1048 a917d384 pbrook
            break;
1049 a917d384 pbrook
        }
1050 a917d384 pbrook
    }
1051 b96a0da0 Gerd Hoffmann
    if (s->current == NULL) {
1052 a917d384 pbrook
        s->waiting = 1;
1053 a917d384 pbrook
    }
1054 a917d384 pbrook
}
1055 a917d384 pbrook
1056 7d8406be pbrook
static void lsi_execute_script(LSIState *s)
1057 7d8406be pbrook
{
1058 7d8406be pbrook
    uint32_t insn;
1059 b25cf589 aliguori
    uint32_t addr, addr_high;
1060 7d8406be pbrook
    int opcode;
1061 ee4d919f aliguori
    int insn_processed = 0;
1062 7d8406be pbrook
1063 7d8406be pbrook
    s->istat1 |= LSI_ISTAT1_SRUN;
1064 7d8406be pbrook
again:
1065 ee4d919f aliguori
    insn_processed++;
1066 7d8406be pbrook
    insn = read_dword(s, s->dsp);
1067 02b373ad balrog
    if (!insn) {
1068 02b373ad balrog
        /* If we receive an empty opcode increment the DSP by 4 bytes
1069 02b373ad balrog
           instead of 8 and execute the next opcode at that location */
1070 02b373ad balrog
        s->dsp += 4;
1071 02b373ad balrog
        goto again;
1072 02b373ad balrog
    }
1073 7d8406be pbrook
    addr = read_dword(s, s->dsp + 4);
1074 b25cf589 aliguori
    addr_high = 0;
1075 7d8406be pbrook
    DPRINTF("SCRIPTS dsp=%08x opcode %08x arg %08x\n", s->dsp, insn, addr);
1076 7d8406be pbrook
    s->dsps = addr;
1077 7d8406be pbrook
    s->dcmd = insn >> 24;
1078 7d8406be pbrook
    s->dsp += 8;
1079 7d8406be pbrook
    switch (insn >> 30) {
1080 7d8406be pbrook
    case 0: /* Block move.  */
1081 7d8406be pbrook
        if (s->sist1 & LSI_SIST1_STO) {
1082 7d8406be pbrook
            DPRINTF("Delayed select timeout\n");
1083 7d8406be pbrook
            lsi_stop_script(s);
1084 7d8406be pbrook
            break;
1085 7d8406be pbrook
        }
1086 7d8406be pbrook
        s->dbc = insn & 0xffffff;
1087 7d8406be pbrook
        s->rbc = s->dbc;
1088 dd8edf01 aliguori
        /* ??? Set ESA.  */
1089 dd8edf01 aliguori
        s->ia = s->dsp - 8;
1090 7d8406be pbrook
        if (insn & (1 << 29)) {
1091 7d8406be pbrook
            /* Indirect addressing.  */
1092 7d8406be pbrook
            addr = read_dword(s, addr);
1093 7d8406be pbrook
        } else if (insn & (1 << 28)) {
1094 7d8406be pbrook
            uint32_t buf[2];
1095 7d8406be pbrook
            int32_t offset;
1096 7d8406be pbrook
            /* Table indirect addressing.  */
1097 dd8edf01 aliguori
1098 dd8edf01 aliguori
            /* 32-bit Table indirect */
1099 7d8406be pbrook
            offset = sxt24(addr);
1100 7d8406be pbrook
            cpu_physical_memory_read(s->dsa + offset, (uint8_t *)buf, 8);
1101 b25cf589 aliguori
            /* byte count is stored in bits 0:23 only */
1102 b25cf589 aliguori
            s->dbc = cpu_to_le32(buf[0]) & 0xffffff;
1103 7faa239c ths
            s->rbc = s->dbc;
1104 7d8406be pbrook
            addr = cpu_to_le32(buf[1]);
1105 b25cf589 aliguori
1106 b25cf589 aliguori
            /* 40-bit DMA, upper addr bits [39:32] stored in first DWORD of
1107 b25cf589 aliguori
             * table, bits [31:24] */
1108 b25cf589 aliguori
            if (lsi_dma_40bit(s))
1109 b25cf589 aliguori
                addr_high = cpu_to_le32(buf[0]) >> 24;
1110 dd8edf01 aliguori
            else if (lsi_dma_ti64bit(s)) {
1111 dd8edf01 aliguori
                int selector = (cpu_to_le32(buf[0]) >> 24) & 0x1f;
1112 dd8edf01 aliguori
                switch (selector) {
1113 dd8edf01 aliguori
                case 0 ... 0x0f:
1114 dd8edf01 aliguori
                    /* offset index into scratch registers since
1115 dd8edf01 aliguori
                     * TI64 mode can use registers C to R */
1116 dd8edf01 aliguori
                    addr_high = s->scratch[2 + selector];
1117 dd8edf01 aliguori
                    break;
1118 dd8edf01 aliguori
                case 0x10:
1119 dd8edf01 aliguori
                    addr_high = s->mmrs;
1120 dd8edf01 aliguori
                    break;
1121 dd8edf01 aliguori
                case 0x11:
1122 dd8edf01 aliguori
                    addr_high = s->mmws;
1123 dd8edf01 aliguori
                    break;
1124 dd8edf01 aliguori
                case 0x12:
1125 dd8edf01 aliguori
                    addr_high = s->sfs;
1126 dd8edf01 aliguori
                    break;
1127 dd8edf01 aliguori
                case 0x13:
1128 dd8edf01 aliguori
                    addr_high = s->drs;
1129 dd8edf01 aliguori
                    break;
1130 dd8edf01 aliguori
                case 0x14:
1131 dd8edf01 aliguori
                    addr_high = s->sbms;
1132 dd8edf01 aliguori
                    break;
1133 dd8edf01 aliguori
                case 0x15:
1134 dd8edf01 aliguori
                    addr_high = s->dbms;
1135 dd8edf01 aliguori
                    break;
1136 dd8edf01 aliguori
                default:
1137 dd8edf01 aliguori
                    BADF("Illegal selector specified (0x%x > 0x15)"
1138 dd8edf01 aliguori
                         " for 64-bit DMA block move", selector);
1139 dd8edf01 aliguori
                    break;
1140 dd8edf01 aliguori
                }
1141 dd8edf01 aliguori
            }
1142 dd8edf01 aliguori
        } else if (lsi_dma_64bit(s)) {
1143 dd8edf01 aliguori
            /* fetch a 3rd dword if 64-bit direct move is enabled and
1144 dd8edf01 aliguori
               only if we're not doing table indirect or indirect addressing */
1145 dd8edf01 aliguori
            s->dbms = read_dword(s, s->dsp);
1146 dd8edf01 aliguori
            s->dsp += 4;
1147 dd8edf01 aliguori
            s->ia = s->dsp - 12;
1148 7d8406be pbrook
        }
1149 7d8406be pbrook
        if ((s->sstat1 & PHASE_MASK) != ((insn >> 24) & 7)) {
1150 7d8406be pbrook
            DPRINTF("Wrong phase got %d expected %d\n",
1151 7d8406be pbrook
                    s->sstat1 & PHASE_MASK, (insn >> 24) & 7);
1152 7d8406be pbrook
            lsi_script_scsi_interrupt(s, LSI_SIST0_MA, 0);
1153 7d8406be pbrook
            break;
1154 7d8406be pbrook
        }
1155 7d8406be pbrook
        s->dnad = addr;
1156 b25cf589 aliguori
        s->dnad64 = addr_high;
1157 7d8406be pbrook
        switch (s->sstat1 & 0x7) {
1158 7d8406be pbrook
        case PHASE_DO:
1159 a917d384 pbrook
            s->waiting = 2;
1160 7d8406be pbrook
            lsi_do_dma(s, 1);
1161 a917d384 pbrook
            if (s->waiting)
1162 a917d384 pbrook
                s->waiting = 3;
1163 7d8406be pbrook
            break;
1164 7d8406be pbrook
        case PHASE_DI:
1165 a917d384 pbrook
            s->waiting = 2;
1166 7d8406be pbrook
            lsi_do_dma(s, 0);
1167 a917d384 pbrook
            if (s->waiting)
1168 a917d384 pbrook
                s->waiting = 3;
1169 7d8406be pbrook
            break;
1170 7d8406be pbrook
        case PHASE_CMD:
1171 7d8406be pbrook
            lsi_do_command(s);
1172 7d8406be pbrook
            break;
1173 7d8406be pbrook
        case PHASE_ST:
1174 7d8406be pbrook
            lsi_do_status(s);
1175 7d8406be pbrook
            break;
1176 7d8406be pbrook
        case PHASE_MO:
1177 7d8406be pbrook
            lsi_do_msgout(s);
1178 7d8406be pbrook
            break;
1179 7d8406be pbrook
        case PHASE_MI:
1180 7d8406be pbrook
            lsi_do_msgin(s);
1181 7d8406be pbrook
            break;
1182 7d8406be pbrook
        default:
1183 7d8406be pbrook
            BADF("Unimplemented phase %d\n", s->sstat1 & PHASE_MASK);
1184 7d8406be pbrook
            exit(1);
1185 7d8406be pbrook
        }
1186 7d8406be pbrook
        s->dfifo = s->dbc & 0xff;
1187 7d8406be pbrook
        s->ctest5 = (s->ctest5 & 0xfc) | ((s->dbc >> 8) & 3);
1188 7d8406be pbrook
        s->sbc = s->dbc;
1189 7d8406be pbrook
        s->rbc -= s->dbc;
1190 7d8406be pbrook
        s->ua = addr + s->dbc;
1191 7d8406be pbrook
        break;
1192 7d8406be pbrook
1193 7d8406be pbrook
    case 1: /* IO or Read/Write instruction.  */
1194 7d8406be pbrook
        opcode = (insn >> 27) & 7;
1195 7d8406be pbrook
        if (opcode < 5) {
1196 7d8406be pbrook
            uint32_t id;
1197 7d8406be pbrook
1198 7d8406be pbrook
            if (insn & (1 << 25)) {
1199 7d8406be pbrook
                id = read_dword(s, s->dsa + sxt24(insn));
1200 7d8406be pbrook
            } else {
1201 07a1bea8 Laszlo Ast
                id = insn;
1202 7d8406be pbrook
            }
1203 7d8406be pbrook
            id = (id >> 16) & 0xf;
1204 7d8406be pbrook
            if (insn & (1 << 26)) {
1205 7d8406be pbrook
                addr = s->dsp + sxt24(addr);
1206 7d8406be pbrook
            }
1207 7d8406be pbrook
            s->dnad = addr;
1208 7d8406be pbrook
            switch (opcode) {
1209 7d8406be pbrook
            case 0: /* Select */
1210 a917d384 pbrook
                s->sdid = id;
1211 38f5b2b8 Laszlo Ast
                if (s->scntl1 & LSI_SCNTL1_CON) {
1212 38f5b2b8 Laszlo Ast
                    DPRINTF("Already reselected, jumping to alternative address\n");
1213 38f5b2b8 Laszlo Ast
                    s->dsp = s->dnad;
1214 a917d384 pbrook
                    break;
1215 a917d384 pbrook
                }
1216 7d8406be pbrook
                s->sstat0 |= LSI_SSTAT0_WOA;
1217 7d8406be pbrook
                s->scntl1 &= ~LSI_SCNTL1_IARB;
1218 ca9c39fa Gerd Hoffmann
                if (id >= LSI_MAX_DEVS || !s->bus.devs[id]) {
1219 64d56409 Jan Kiszka
                    lsi_bad_selection(s, id);
1220 7d8406be pbrook
                    break;
1221 7d8406be pbrook
                }
1222 7d8406be pbrook
                DPRINTF("Selected target %d%s\n",
1223 7d8406be pbrook
                        id, insn & (1 << 3) ? " ATN" : "");
1224 7d8406be pbrook
                /* ??? Linux drivers compain when this is set.  Maybe
1225 7d8406be pbrook
                   it only applies in low-level mode (unimplemented).
1226 7d8406be pbrook
                lsi_script_scsi_interrupt(s, LSI_SIST0_CMP, 0); */
1227 af12ac98 Gerd Hoffmann
                s->select_tag = id << 8;
1228 7d8406be pbrook
                s->scntl1 |= LSI_SCNTL1_CON;
1229 7d8406be pbrook
                if (insn & (1 << 3)) {
1230 7d8406be pbrook
                    s->socl |= LSI_SOCL_ATN;
1231 7d8406be pbrook
                }
1232 7d8406be pbrook
                lsi_set_phase(s, PHASE_MO);
1233 7d8406be pbrook
                break;
1234 7d8406be pbrook
            case 1: /* Disconnect */
1235 a15fdf86 Laszlo Ast
                DPRINTF("Wait Disconnect\n");
1236 7d8406be pbrook
                s->scntl1 &= ~LSI_SCNTL1_CON;
1237 7d8406be pbrook
                break;
1238 7d8406be pbrook
            case 2: /* Wait Reselect */
1239 e560125e Laszlo Ast
                if (!lsi_irq_on_rsl(s)) {
1240 e560125e Laszlo Ast
                    lsi_wait_reselect(s);
1241 e560125e Laszlo Ast
                }
1242 7d8406be pbrook
                break;
1243 7d8406be pbrook
            case 3: /* Set */
1244 7d8406be pbrook
                DPRINTF("Set%s%s%s%s\n",
1245 7d8406be pbrook
                        insn & (1 << 3) ? " ATN" : "",
1246 7d8406be pbrook
                        insn & (1 << 6) ? " ACK" : "",
1247 7d8406be pbrook
                        insn & (1 << 9) ? " TM" : "",
1248 7d8406be pbrook
                        insn & (1 << 10) ? " CC" : "");
1249 7d8406be pbrook
                if (insn & (1 << 3)) {
1250 7d8406be pbrook
                    s->socl |= LSI_SOCL_ATN;
1251 7d8406be pbrook
                    lsi_set_phase(s, PHASE_MO);
1252 7d8406be pbrook
                }
1253 7d8406be pbrook
                if (insn & (1 << 9)) {
1254 7d8406be pbrook
                    BADF("Target mode not implemented\n");
1255 7d8406be pbrook
                    exit(1);
1256 7d8406be pbrook
                }
1257 7d8406be pbrook
                if (insn & (1 << 10))
1258 7d8406be pbrook
                    s->carry = 1;
1259 7d8406be pbrook
                break;
1260 7d8406be pbrook
            case 4: /* Clear */
1261 7d8406be pbrook
                DPRINTF("Clear%s%s%s%s\n",
1262 7d8406be pbrook
                        insn & (1 << 3) ? " ATN" : "",
1263 7d8406be pbrook
                        insn & (1 << 6) ? " ACK" : "",
1264 7d8406be pbrook
                        insn & (1 << 9) ? " TM" : "",
1265 7d8406be pbrook
                        insn & (1 << 10) ? " CC" : "");
1266 7d8406be pbrook
                if (insn & (1 << 3)) {
1267 7d8406be pbrook
                    s->socl &= ~LSI_SOCL_ATN;
1268 7d8406be pbrook
                }
1269 7d8406be pbrook
                if (insn & (1 << 10))
1270 7d8406be pbrook
                    s->carry = 0;
1271 7d8406be pbrook
                break;
1272 7d8406be pbrook
            }
1273 7d8406be pbrook
        } else {
1274 7d8406be pbrook
            uint8_t op0;
1275 7d8406be pbrook
            uint8_t op1;
1276 7d8406be pbrook
            uint8_t data8;
1277 7d8406be pbrook
            int reg;
1278 7d8406be pbrook
            int operator;
1279 7d8406be pbrook
#ifdef DEBUG_LSI
1280 7d8406be pbrook
            static const char *opcode_names[3] =
1281 7d8406be pbrook
                {"Write", "Read", "Read-Modify-Write"};
1282 7d8406be pbrook
            static const char *operator_names[8] =
1283 7d8406be pbrook
                {"MOV", "SHL", "OR", "XOR", "AND", "SHR", "ADD", "ADC"};
1284 7d8406be pbrook
#endif
1285 7d8406be pbrook
1286 7d8406be pbrook
            reg = ((insn >> 16) & 0x7f) | (insn & 0x80);
1287 7d8406be pbrook
            data8 = (insn >> 8) & 0xff;
1288 7d8406be pbrook
            opcode = (insn >> 27) & 7;
1289 7d8406be pbrook
            operator = (insn >> 24) & 7;
1290 a917d384 pbrook
            DPRINTF("%s reg 0x%x %s data8=0x%02x sfbr=0x%02x%s\n",
1291 7d8406be pbrook
                    opcode_names[opcode - 5], reg,
1292 a917d384 pbrook
                    operator_names[operator], data8, s->sfbr,
1293 7d8406be pbrook
                    (insn & (1 << 23)) ? " SFBR" : "");
1294 7d8406be pbrook
            op0 = op1 = 0;
1295 7d8406be pbrook
            switch (opcode) {
1296 7d8406be pbrook
            case 5: /* From SFBR */
1297 7d8406be pbrook
                op0 = s->sfbr;
1298 7d8406be pbrook
                op1 = data8;
1299 7d8406be pbrook
                break;
1300 7d8406be pbrook
            case 6: /* To SFBR */
1301 7d8406be pbrook
                if (operator)
1302 7d8406be pbrook
                    op0 = lsi_reg_readb(s, reg);
1303 7d8406be pbrook
                op1 = data8;
1304 7d8406be pbrook
                break;
1305 7d8406be pbrook
            case 7: /* Read-modify-write */
1306 7d8406be pbrook
                if (operator)
1307 7d8406be pbrook
                    op0 = lsi_reg_readb(s, reg);
1308 7d8406be pbrook
                if (insn & (1 << 23)) {
1309 7d8406be pbrook
                    op1 = s->sfbr;
1310 7d8406be pbrook
                } else {
1311 7d8406be pbrook
                    op1 = data8;
1312 7d8406be pbrook
                }
1313 7d8406be pbrook
                break;
1314 7d8406be pbrook
            }
1315 7d8406be pbrook
1316 7d8406be pbrook
            switch (operator) {
1317 7d8406be pbrook
            case 0: /* move */
1318 7d8406be pbrook
                op0 = op1;
1319 7d8406be pbrook
                break;
1320 7d8406be pbrook
            case 1: /* Shift left */
1321 7d8406be pbrook
                op1 = op0 >> 7;
1322 7d8406be pbrook
                op0 = (op0 << 1) | s->carry;
1323 7d8406be pbrook
                s->carry = op1;
1324 7d8406be pbrook
                break;
1325 7d8406be pbrook
            case 2: /* OR */
1326 7d8406be pbrook
                op0 |= op1;
1327 7d8406be pbrook
                break;
1328 7d8406be pbrook
            case 3: /* XOR */
1329 dcfb9014 ths
                op0 ^= op1;
1330 7d8406be pbrook
                break;
1331 7d8406be pbrook
            case 4: /* AND */
1332 7d8406be pbrook
                op0 &= op1;
1333 7d8406be pbrook
                break;
1334 7d8406be pbrook
            case 5: /* SHR */
1335 7d8406be pbrook
                op1 = op0 & 1;
1336 7d8406be pbrook
                op0 = (op0 >> 1) | (s->carry << 7);
1337 687fa640 ths
                s->carry = op1;
1338 7d8406be pbrook
                break;
1339 7d8406be pbrook
            case 6: /* ADD */
1340 7d8406be pbrook
                op0 += op1;
1341 7d8406be pbrook
                s->carry = op0 < op1;
1342 7d8406be pbrook
                break;
1343 7d8406be pbrook
            case 7: /* ADC */
1344 7d8406be pbrook
                op0 += op1 + s->carry;
1345 7d8406be pbrook
                if (s->carry)
1346 7d8406be pbrook
                    s->carry = op0 <= op1;
1347 7d8406be pbrook
                else
1348 7d8406be pbrook
                    s->carry = op0 < op1;
1349 7d8406be pbrook
                break;
1350 7d8406be pbrook
            }
1351 7d8406be pbrook
1352 7d8406be pbrook
            switch (opcode) {
1353 7d8406be pbrook
            case 5: /* From SFBR */
1354 7d8406be pbrook
            case 7: /* Read-modify-write */
1355 7d8406be pbrook
                lsi_reg_writeb(s, reg, op0);
1356 7d8406be pbrook
                break;
1357 7d8406be pbrook
            case 6: /* To SFBR */
1358 7d8406be pbrook
                s->sfbr = op0;
1359 7d8406be pbrook
                break;
1360 7d8406be pbrook
            }
1361 7d8406be pbrook
        }
1362 7d8406be pbrook
        break;
1363 7d8406be pbrook
1364 7d8406be pbrook
    case 2: /* Transfer Control.  */
1365 7d8406be pbrook
        {
1366 7d8406be pbrook
            int cond;
1367 7d8406be pbrook
            int jmp;
1368 7d8406be pbrook
1369 7d8406be pbrook
            if ((insn & 0x002e0000) == 0) {
1370 7d8406be pbrook
                DPRINTF("NOP\n");
1371 7d8406be pbrook
                break;
1372 7d8406be pbrook
            }
1373 7d8406be pbrook
            if (s->sist1 & LSI_SIST1_STO) {
1374 7d8406be pbrook
                DPRINTF("Delayed select timeout\n");
1375 7d8406be pbrook
                lsi_stop_script(s);
1376 7d8406be pbrook
                break;
1377 7d8406be pbrook
            }
1378 7d8406be pbrook
            cond = jmp = (insn & (1 << 19)) != 0;
1379 7d8406be pbrook
            if (cond == jmp && (insn & (1 << 21))) {
1380 7d8406be pbrook
                DPRINTF("Compare carry %d\n", s->carry == jmp);
1381 7d8406be pbrook
                cond = s->carry != 0;
1382 7d8406be pbrook
            }
1383 7d8406be pbrook
            if (cond == jmp && (insn & (1 << 17))) {
1384 7d8406be pbrook
                DPRINTF("Compare phase %d %c= %d\n",
1385 7d8406be pbrook
                        (s->sstat1 & PHASE_MASK),
1386 7d8406be pbrook
                        jmp ? '=' : '!',
1387 7d8406be pbrook
                        ((insn >> 24) & 7));
1388 7d8406be pbrook
                cond = (s->sstat1 & PHASE_MASK) == ((insn >> 24) & 7);
1389 7d8406be pbrook
            }
1390 7d8406be pbrook
            if (cond == jmp && (insn & (1 << 18))) {
1391 7d8406be pbrook
                uint8_t mask;
1392 7d8406be pbrook
1393 7d8406be pbrook
                mask = (~insn >> 8) & 0xff;
1394 7d8406be pbrook
                DPRINTF("Compare data 0x%x & 0x%x %c= 0x%x\n",
1395 7d8406be pbrook
                        s->sfbr, mask, jmp ? '=' : '!', insn & mask);
1396 7d8406be pbrook
                cond = (s->sfbr & mask) == (insn & mask);
1397 7d8406be pbrook
            }
1398 7d8406be pbrook
            if (cond == jmp) {
1399 7d8406be pbrook
                if (insn & (1 << 23)) {
1400 7d8406be pbrook
                    /* Relative address.  */
1401 7d8406be pbrook
                    addr = s->dsp + sxt24(addr);
1402 7d8406be pbrook
                }
1403 7d8406be pbrook
                switch ((insn >> 27) & 7) {
1404 7d8406be pbrook
                case 0: /* Jump */
1405 7d8406be pbrook
                    DPRINTF("Jump to 0x%08x\n", addr);
1406 7d8406be pbrook
                    s->dsp = addr;
1407 7d8406be pbrook
                    break;
1408 7d8406be pbrook
                case 1: /* Call */
1409 7d8406be pbrook
                    DPRINTF("Call 0x%08x\n", addr);
1410 7d8406be pbrook
                    s->temp = s->dsp;
1411 7d8406be pbrook
                    s->dsp = addr;
1412 7d8406be pbrook
                    break;
1413 7d8406be pbrook
                case 2: /* Return */
1414 7d8406be pbrook
                    DPRINTF("Return to 0x%08x\n", s->temp);
1415 7d8406be pbrook
                    s->dsp = s->temp;
1416 7d8406be pbrook
                    break;
1417 7d8406be pbrook
                case 3: /* Interrupt */
1418 7d8406be pbrook
                    DPRINTF("Interrupt 0x%08x\n", s->dsps);
1419 7d8406be pbrook
                    if ((insn & (1 << 20)) != 0) {
1420 7d8406be pbrook
                        s->istat0 |= LSI_ISTAT0_INTF;
1421 7d8406be pbrook
                        lsi_update_irq(s);
1422 7d8406be pbrook
                    } else {
1423 7d8406be pbrook
                        lsi_script_dma_interrupt(s, LSI_DSTAT_SIR);
1424 7d8406be pbrook
                    }
1425 7d8406be pbrook
                    break;
1426 7d8406be pbrook
                default:
1427 7d8406be pbrook
                    DPRINTF("Illegal transfer control\n");
1428 7d8406be pbrook
                    lsi_script_dma_interrupt(s, LSI_DSTAT_IID);
1429 7d8406be pbrook
                    break;
1430 7d8406be pbrook
                }
1431 7d8406be pbrook
            } else {
1432 7d8406be pbrook
                DPRINTF("Control condition failed\n");
1433 7d8406be pbrook
            }
1434 7d8406be pbrook
        }
1435 7d8406be pbrook
        break;
1436 7d8406be pbrook
1437 7d8406be pbrook
    case 3:
1438 7d8406be pbrook
        if ((insn & (1 << 29)) == 0) {
1439 7d8406be pbrook
            /* Memory move.  */
1440 7d8406be pbrook
            uint32_t dest;
1441 7d8406be pbrook
            /* ??? The docs imply the destination address is loaded into
1442 7d8406be pbrook
               the TEMP register.  However the Linux drivers rely on
1443 7d8406be pbrook
               the value being presrved.  */
1444 7d8406be pbrook
            dest = read_dword(s, s->dsp);
1445 7d8406be pbrook
            s->dsp += 4;
1446 7d8406be pbrook
            lsi_memcpy(s, dest, addr, insn & 0xffffff);
1447 7d8406be pbrook
        } else {
1448 7d8406be pbrook
            uint8_t data[7];
1449 7d8406be pbrook
            int reg;
1450 7d8406be pbrook
            int n;
1451 7d8406be pbrook
            int i;
1452 7d8406be pbrook
1453 7d8406be pbrook
            if (insn & (1 << 28)) {
1454 7d8406be pbrook
                addr = s->dsa + sxt24(addr);
1455 7d8406be pbrook
            }
1456 7d8406be pbrook
            n = (insn & 7);
1457 7d8406be pbrook
            reg = (insn >> 16) & 0xff;
1458 7d8406be pbrook
            if (insn & (1 << 24)) {
1459 7d8406be pbrook
                cpu_physical_memory_read(addr, data, n);
1460 a917d384 pbrook
                DPRINTF("Load reg 0x%x size %d addr 0x%08x = %08x\n", reg, n,
1461 a917d384 pbrook
                        addr, *(int *)data);
1462 7d8406be pbrook
                for (i = 0; i < n; i++) {
1463 7d8406be pbrook
                    lsi_reg_writeb(s, reg + i, data[i]);
1464 7d8406be pbrook
                }
1465 7d8406be pbrook
            } else {
1466 7d8406be pbrook
                DPRINTF("Store reg 0x%x size %d addr 0x%08x\n", reg, n, addr);
1467 7d8406be pbrook
                for (i = 0; i < n; i++) {
1468 7d8406be pbrook
                    data[i] = lsi_reg_readb(s, reg + i);
1469 7d8406be pbrook
                }
1470 7d8406be pbrook
                cpu_physical_memory_write(addr, data, n);
1471 7d8406be pbrook
            }
1472 7d8406be pbrook
        }
1473 7d8406be pbrook
    }
1474 ee4d919f aliguori
    if (insn_processed > 10000 && !s->waiting) {
1475 64c68080 pbrook
        /* Some windows drivers make the device spin waiting for a memory
1476 64c68080 pbrook
           location to change.  If we have been executed a lot of code then
1477 64c68080 pbrook
           assume this is the case and force an unexpected device disconnect.
1478 64c68080 pbrook
           This is apparently sufficient to beat the drivers into submission.
1479 64c68080 pbrook
         */
1480 ee4d919f aliguori
        if (!(s->sien0 & LSI_SIST0_UDC))
1481 ee4d919f aliguori
            fprintf(stderr, "inf. loop with UDC masked\n");
1482 ee4d919f aliguori
        lsi_script_scsi_interrupt(s, LSI_SIST0_UDC, 0);
1483 ee4d919f aliguori
        lsi_disconnect(s);
1484 ee4d919f aliguori
    } else if (s->istat1 & LSI_ISTAT1_SRUN && !s->waiting) {
1485 7d8406be pbrook
        if (s->dcntl & LSI_DCNTL_SSM) {
1486 7d8406be pbrook
            lsi_script_dma_interrupt(s, LSI_DSTAT_SSI);
1487 7d8406be pbrook
        } else {
1488 7d8406be pbrook
            goto again;
1489 7d8406be pbrook
        }
1490 7d8406be pbrook
    }
1491 7d8406be pbrook
    DPRINTF("SCRIPTS execution stopped\n");
1492 7d8406be pbrook
}
1493 7d8406be pbrook
1494 7d8406be pbrook
static uint8_t lsi_reg_readb(LSIState *s, int offset)
1495 7d8406be pbrook
{
1496 7d8406be pbrook
    uint8_t tmp;
1497 75f76531 aurel32
#define CASE_GET_REG24(name, addr) \
1498 75f76531 aurel32
    case addr: return s->name & 0xff; \
1499 75f76531 aurel32
    case addr + 1: return (s->name >> 8) & 0xff; \
1500 75f76531 aurel32
    case addr + 2: return (s->name >> 16) & 0xff;
1501 75f76531 aurel32
1502 7d8406be pbrook
#define CASE_GET_REG32(name, addr) \
1503 7d8406be pbrook
    case addr: return s->name & 0xff; \
1504 7d8406be pbrook
    case addr + 1: return (s->name >> 8) & 0xff; \
1505 7d8406be pbrook
    case addr + 2: return (s->name >> 16) & 0xff; \
1506 7d8406be pbrook
    case addr + 3: return (s->name >> 24) & 0xff;
1507 7d8406be pbrook
1508 7d8406be pbrook
#ifdef DEBUG_LSI_REG
1509 7d8406be pbrook
    DPRINTF("Read reg %x\n", offset);
1510 7d8406be pbrook
#endif
1511 7d8406be pbrook
    switch (offset) {
1512 7d8406be pbrook
    case 0x00: /* SCNTL0 */
1513 7d8406be pbrook
        return s->scntl0;
1514 7d8406be pbrook
    case 0x01: /* SCNTL1 */
1515 7d8406be pbrook
        return s->scntl1;
1516 7d8406be pbrook
    case 0x02: /* SCNTL2 */
1517 7d8406be pbrook
        return s->scntl2;
1518 7d8406be pbrook
    case 0x03: /* SCNTL3 */
1519 7d8406be pbrook
        return s->scntl3;
1520 7d8406be pbrook
    case 0x04: /* SCID */
1521 7d8406be pbrook
        return s->scid;
1522 7d8406be pbrook
    case 0x05: /* SXFER */
1523 7d8406be pbrook
        return s->sxfer;
1524 7d8406be pbrook
    case 0x06: /* SDID */
1525 7d8406be pbrook
        return s->sdid;
1526 7d8406be pbrook
    case 0x07: /* GPREG0 */
1527 7d8406be pbrook
        return 0x7f;
1528 985a03b0 ths
    case 0x08: /* Revision ID */
1529 985a03b0 ths
        return 0x00;
1530 a917d384 pbrook
    case 0xa: /* SSID */
1531 a917d384 pbrook
        return s->ssid;
1532 7d8406be pbrook
    case 0xb: /* SBCL */
1533 7d8406be pbrook
        /* ??? This is not correct. However it's (hopefully) only
1534 7d8406be pbrook
           used for diagnostics, so should be ok.  */
1535 7d8406be pbrook
        return 0;
1536 7d8406be pbrook
    case 0xc: /* DSTAT */
1537 7d8406be pbrook
        tmp = s->dstat | 0x80;
1538 7d8406be pbrook
        if ((s->istat0 & LSI_ISTAT0_INTF) == 0)
1539 7d8406be pbrook
            s->dstat = 0;
1540 7d8406be pbrook
        lsi_update_irq(s);
1541 7d8406be pbrook
        return tmp;
1542 7d8406be pbrook
    case 0x0d: /* SSTAT0 */
1543 7d8406be pbrook
        return s->sstat0;
1544 7d8406be pbrook
    case 0x0e: /* SSTAT1 */
1545 7d8406be pbrook
        return s->sstat1;
1546 7d8406be pbrook
    case 0x0f: /* SSTAT2 */
1547 7d8406be pbrook
        return s->scntl1 & LSI_SCNTL1_CON ? 0 : 2;
1548 7d8406be pbrook
    CASE_GET_REG32(dsa, 0x10)
1549 7d8406be pbrook
    case 0x14: /* ISTAT0 */
1550 7d8406be pbrook
        return s->istat0;
1551 ecabe8cc aliguori
    case 0x15: /* ISTAT1 */
1552 ecabe8cc aliguori
        return s->istat1;
1553 7d8406be pbrook
    case 0x16: /* MBOX0 */
1554 7d8406be pbrook
        return s->mbox0;
1555 7d8406be pbrook
    case 0x17: /* MBOX1 */
1556 7d8406be pbrook
        return s->mbox1;
1557 7d8406be pbrook
    case 0x18: /* CTEST0 */
1558 7d8406be pbrook
        return 0xff;
1559 7d8406be pbrook
    case 0x19: /* CTEST1 */
1560 7d8406be pbrook
        return 0;
1561 7d8406be pbrook
    case 0x1a: /* CTEST2 */
1562 9167a69a balrog
        tmp = s->ctest2 | LSI_CTEST2_DACK | LSI_CTEST2_CM;
1563 7d8406be pbrook
        if (s->istat0 & LSI_ISTAT0_SIGP) {
1564 7d8406be pbrook
            s->istat0 &= ~LSI_ISTAT0_SIGP;
1565 7d8406be pbrook
            tmp |= LSI_CTEST2_SIGP;
1566 7d8406be pbrook
        }
1567 7d8406be pbrook
        return tmp;
1568 7d8406be pbrook
    case 0x1b: /* CTEST3 */
1569 7d8406be pbrook
        return s->ctest3;
1570 7d8406be pbrook
    CASE_GET_REG32(temp, 0x1c)
1571 7d8406be pbrook
    case 0x20: /* DFIFO */
1572 7d8406be pbrook
        return 0;
1573 7d8406be pbrook
    case 0x21: /* CTEST4 */
1574 7d8406be pbrook
        return s->ctest4;
1575 7d8406be pbrook
    case 0x22: /* CTEST5 */
1576 7d8406be pbrook
        return s->ctest5;
1577 985a03b0 ths
    case 0x23: /* CTEST6 */
1578 985a03b0 ths
         return 0;
1579 75f76531 aurel32
    CASE_GET_REG24(dbc, 0x24)
1580 7d8406be pbrook
    case 0x27: /* DCMD */
1581 7d8406be pbrook
        return s->dcmd;
1582 4b9a2d6d Sebastian Herbszt
    CASE_GET_REG32(dnad, 0x28)
1583 7d8406be pbrook
    CASE_GET_REG32(dsp, 0x2c)
1584 7d8406be pbrook
    CASE_GET_REG32(dsps, 0x30)
1585 7d8406be pbrook
    CASE_GET_REG32(scratch[0], 0x34)
1586 7d8406be pbrook
    case 0x38: /* DMODE */
1587 7d8406be pbrook
        return s->dmode;
1588 7d8406be pbrook
    case 0x39: /* DIEN */
1589 7d8406be pbrook
        return s->dien;
1590 bd8ee11a Sebastian Herbszt
    case 0x3a: /* SBR */
1591 bd8ee11a Sebastian Herbszt
        return s->sbr;
1592 7d8406be pbrook
    case 0x3b: /* DCNTL */
1593 7d8406be pbrook
        return s->dcntl;
1594 7d8406be pbrook
    case 0x40: /* SIEN0 */
1595 7d8406be pbrook
        return s->sien0;
1596 7d8406be pbrook
    case 0x41: /* SIEN1 */
1597 7d8406be pbrook
        return s->sien1;
1598 7d8406be pbrook
    case 0x42: /* SIST0 */
1599 7d8406be pbrook
        tmp = s->sist0;
1600 7d8406be pbrook
        s->sist0 = 0;
1601 7d8406be pbrook
        lsi_update_irq(s);
1602 7d8406be pbrook
        return tmp;
1603 7d8406be pbrook
    case 0x43: /* SIST1 */
1604 7d8406be pbrook
        tmp = s->sist1;
1605 7d8406be pbrook
        s->sist1 = 0;
1606 7d8406be pbrook
        lsi_update_irq(s);
1607 7d8406be pbrook
        return tmp;
1608 9167a69a balrog
    case 0x46: /* MACNTL */
1609 9167a69a balrog
        return 0x0f;
1610 7d8406be pbrook
    case 0x47: /* GPCNTL0 */
1611 7d8406be pbrook
        return 0x0f;
1612 7d8406be pbrook
    case 0x48: /* STIME0 */
1613 7d8406be pbrook
        return s->stime0;
1614 7d8406be pbrook
    case 0x4a: /* RESPID0 */
1615 7d8406be pbrook
        return s->respid0;
1616 7d8406be pbrook
    case 0x4b: /* RESPID1 */
1617 7d8406be pbrook
        return s->respid1;
1618 7d8406be pbrook
    case 0x4d: /* STEST1 */
1619 7d8406be pbrook
        return s->stest1;
1620 7d8406be pbrook
    case 0x4e: /* STEST2 */
1621 7d8406be pbrook
        return s->stest2;
1622 7d8406be pbrook
    case 0x4f: /* STEST3 */
1623 7d8406be pbrook
        return s->stest3;
1624 a917d384 pbrook
    case 0x50: /* SIDL */
1625 a917d384 pbrook
        /* This is needed by the linux drivers.  We currently only update it
1626 a917d384 pbrook
           during the MSG IN phase.  */
1627 a917d384 pbrook
        return s->sidl;
1628 7d8406be pbrook
    case 0x52: /* STEST4 */
1629 7d8406be pbrook
        return 0xe0;
1630 7d8406be pbrook
    case 0x56: /* CCNTL0 */
1631 7d8406be pbrook
        return s->ccntl0;
1632 7d8406be pbrook
    case 0x57: /* CCNTL1 */
1633 7d8406be pbrook
        return s->ccntl1;
1634 a917d384 pbrook
    case 0x58: /* SBDL */
1635 a917d384 pbrook
        /* Some drivers peek at the data bus during the MSG IN phase.  */
1636 a917d384 pbrook
        if ((s->sstat1 & PHASE_MASK) == PHASE_MI)
1637 a917d384 pbrook
            return s->msg[0];
1638 a917d384 pbrook
        return 0;
1639 a917d384 pbrook
    case 0x59: /* SBDL high */
1640 7d8406be pbrook
        return 0;
1641 7d8406be pbrook
    CASE_GET_REG32(mmrs, 0xa0)
1642 7d8406be pbrook
    CASE_GET_REG32(mmws, 0xa4)
1643 7d8406be pbrook
    CASE_GET_REG32(sfs, 0xa8)
1644 7d8406be pbrook
    CASE_GET_REG32(drs, 0xac)
1645 7d8406be pbrook
    CASE_GET_REG32(sbms, 0xb0)
1646 ab57d967 aliguori
    CASE_GET_REG32(dbms, 0xb4)
1647 7d8406be pbrook
    CASE_GET_REG32(dnad64, 0xb8)
1648 7d8406be pbrook
    CASE_GET_REG32(pmjad1, 0xc0)
1649 7d8406be pbrook
    CASE_GET_REG32(pmjad2, 0xc4)
1650 7d8406be pbrook
    CASE_GET_REG32(rbc, 0xc8)
1651 7d8406be pbrook
    CASE_GET_REG32(ua, 0xcc)
1652 7d8406be pbrook
    CASE_GET_REG32(ia, 0xd4)
1653 7d8406be pbrook
    CASE_GET_REG32(sbc, 0xd8)
1654 7d8406be pbrook
    CASE_GET_REG32(csbc, 0xdc)
1655 7d8406be pbrook
    }
1656 7d8406be pbrook
    if (offset >= 0x5c && offset < 0xa0) {
1657 7d8406be pbrook
        int n;
1658 7d8406be pbrook
        int shift;
1659 7d8406be pbrook
        n = (offset - 0x58) >> 2;
1660 7d8406be pbrook
        shift = (offset & 3) * 8;
1661 7d8406be pbrook
        return (s->scratch[n] >> shift) & 0xff;
1662 7d8406be pbrook
    }
1663 7d8406be pbrook
    BADF("readb 0x%x\n", offset);
1664 7d8406be pbrook
    exit(1);
1665 75f76531 aurel32
#undef CASE_GET_REG24
1666 7d8406be pbrook
#undef CASE_GET_REG32
1667 7d8406be pbrook
}
1668 7d8406be pbrook
1669 7d8406be pbrook
static void lsi_reg_writeb(LSIState *s, int offset, uint8_t val)
1670 7d8406be pbrook
{
1671 49c47daa Sebastian Herbszt
#define CASE_SET_REG24(name, addr) \
1672 49c47daa Sebastian Herbszt
    case addr    : s->name &= 0xffffff00; s->name |= val;       break; \
1673 49c47daa Sebastian Herbszt
    case addr + 1: s->name &= 0xffff00ff; s->name |= val << 8;  break; \
1674 49c47daa Sebastian Herbszt
    case addr + 2: s->name &= 0xff00ffff; s->name |= val << 16; break;
1675 49c47daa Sebastian Herbszt
1676 7d8406be pbrook
#define CASE_SET_REG32(name, addr) \
1677 7d8406be pbrook
    case addr    : s->name &= 0xffffff00; s->name |= val;       break; \
1678 7d8406be pbrook
    case addr + 1: s->name &= 0xffff00ff; s->name |= val << 8;  break; \
1679 7d8406be pbrook
    case addr + 2: s->name &= 0xff00ffff; s->name |= val << 16; break; \
1680 7d8406be pbrook
    case addr + 3: s->name &= 0x00ffffff; s->name |= val << 24; break;
1681 7d8406be pbrook
1682 7d8406be pbrook
#ifdef DEBUG_LSI_REG
1683 7d8406be pbrook
    DPRINTF("Write reg %x = %02x\n", offset, val);
1684 7d8406be pbrook
#endif
1685 7d8406be pbrook
    switch (offset) {
1686 7d8406be pbrook
    case 0x00: /* SCNTL0 */
1687 7d8406be pbrook
        s->scntl0 = val;
1688 7d8406be pbrook
        if (val & LSI_SCNTL0_START) {
1689 7d8406be pbrook
            BADF("Start sequence not implemented\n");
1690 7d8406be pbrook
        }
1691 7d8406be pbrook
        break;
1692 7d8406be pbrook
    case 0x01: /* SCNTL1 */
1693 7d8406be pbrook
        s->scntl1 = val & ~LSI_SCNTL1_SST;
1694 7d8406be pbrook
        if (val & LSI_SCNTL1_IARB) {
1695 7d8406be pbrook
            BADF("Immediate Arbritration not implemented\n");
1696 7d8406be pbrook
        }
1697 7d8406be pbrook
        if (val & LSI_SCNTL1_RST) {
1698 680a34ee Jan Kiszka
            if (!(s->sstat0 & LSI_SSTAT0_RST)) {
1699 680a34ee Jan Kiszka
                DeviceState *dev;
1700 680a34ee Jan Kiszka
                int id;
1701 680a34ee Jan Kiszka
1702 680a34ee Jan Kiszka
                for (id = 0; id < s->bus.ndev; id++) {
1703 680a34ee Jan Kiszka
                    if (s->bus.devs[id]) {
1704 680a34ee Jan Kiszka
                        dev = &s->bus.devs[id]->qdev;
1705 680a34ee Jan Kiszka
                        dev->info->reset(dev);
1706 680a34ee Jan Kiszka
                    }
1707 680a34ee Jan Kiszka
                }
1708 680a34ee Jan Kiszka
                s->sstat0 |= LSI_SSTAT0_RST;
1709 680a34ee Jan Kiszka
                lsi_script_scsi_interrupt(s, LSI_SIST0_RST, 0);
1710 680a34ee Jan Kiszka
            }
1711 7d8406be pbrook
        } else {
1712 7d8406be pbrook
            s->sstat0 &= ~LSI_SSTAT0_RST;
1713 7d8406be pbrook
        }
1714 7d8406be pbrook
        break;
1715 7d8406be pbrook
    case 0x02: /* SCNTL2 */
1716 7d8406be pbrook
        val &= ~(LSI_SCNTL2_WSR | LSI_SCNTL2_WSS);
1717 3d834c78 ths
        s->scntl2 = val;
1718 7d8406be pbrook
        break;
1719 7d8406be pbrook
    case 0x03: /* SCNTL3 */
1720 7d8406be pbrook
        s->scntl3 = val;
1721 7d8406be pbrook
        break;
1722 7d8406be pbrook
    case 0x04: /* SCID */
1723 7d8406be pbrook
        s->scid = val;
1724 7d8406be pbrook
        break;
1725 7d8406be pbrook
    case 0x05: /* SXFER */
1726 7d8406be pbrook
        s->sxfer = val;
1727 7d8406be pbrook
        break;
1728 a917d384 pbrook
    case 0x06: /* SDID */
1729 a917d384 pbrook
        if ((val & 0xf) != (s->ssid & 0xf))
1730 a917d384 pbrook
            BADF("Destination ID does not match SSID\n");
1731 a917d384 pbrook
        s->sdid = val & 0xf;
1732 a917d384 pbrook
        break;
1733 7d8406be pbrook
    case 0x07: /* GPREG0 */
1734 7d8406be pbrook
        break;
1735 a917d384 pbrook
    case 0x08: /* SFBR */
1736 a917d384 pbrook
        /* The CPU is not allowed to write to this register.  However the
1737 a917d384 pbrook
           SCRIPTS register move instructions are.  */
1738 a917d384 pbrook
        s->sfbr = val;
1739 a917d384 pbrook
        break;
1740 a15fdf86 Laszlo Ast
    case 0x0a: case 0x0b:
1741 9167a69a balrog
        /* Openserver writes to these readonly registers on startup */
1742 a15fdf86 Laszlo Ast
        return;
1743 7d8406be pbrook
    case 0x0c: case 0x0d: case 0x0e: case 0x0f:
1744 7d8406be pbrook
        /* Linux writes to these readonly registers on startup.  */
1745 7d8406be pbrook
        return;
1746 7d8406be pbrook
    CASE_SET_REG32(dsa, 0x10)
1747 7d8406be pbrook
    case 0x14: /* ISTAT0 */
1748 7d8406be pbrook
        s->istat0 = (s->istat0 & 0x0f) | (val & 0xf0);
1749 7d8406be pbrook
        if (val & LSI_ISTAT0_ABRT) {
1750 7d8406be pbrook
            lsi_script_dma_interrupt(s, LSI_DSTAT_ABRT);
1751 7d8406be pbrook
        }
1752 7d8406be pbrook
        if (val & LSI_ISTAT0_INTF) {
1753 7d8406be pbrook
            s->istat0 &= ~LSI_ISTAT0_INTF;
1754 7d8406be pbrook
            lsi_update_irq(s);
1755 7d8406be pbrook
        }
1756 4d611c9a pbrook
        if (s->waiting == 1 && val & LSI_ISTAT0_SIGP) {
1757 7d8406be pbrook
            DPRINTF("Woken by SIGP\n");
1758 7d8406be pbrook
            s->waiting = 0;
1759 7d8406be pbrook
            s->dsp = s->dnad;
1760 7d8406be pbrook
            lsi_execute_script(s);
1761 7d8406be pbrook
        }
1762 7d8406be pbrook
        if (val & LSI_ISTAT0_SRST) {
1763 7d8406be pbrook
            lsi_soft_reset(s);
1764 7d8406be pbrook
        }
1765 92d88ecb ths
        break;
1766 7d8406be pbrook
    case 0x16: /* MBOX0 */
1767 7d8406be pbrook
        s->mbox0 = val;
1768 92d88ecb ths
        break;
1769 7d8406be pbrook
    case 0x17: /* MBOX1 */
1770 7d8406be pbrook
        s->mbox1 = val;
1771 92d88ecb ths
        break;
1772 9167a69a balrog
    case 0x1a: /* CTEST2 */
1773 9167a69a balrog
        s->ctest2 = val & LSI_CTEST2_PCICIE;
1774 9167a69a balrog
        break;
1775 7d8406be pbrook
    case 0x1b: /* CTEST3 */
1776 7d8406be pbrook
        s->ctest3 = val & 0x0f;
1777 7d8406be pbrook
        break;
1778 7d8406be pbrook
    CASE_SET_REG32(temp, 0x1c)
1779 7d8406be pbrook
    case 0x21: /* CTEST4 */
1780 7d8406be pbrook
        if (val & 7) {
1781 7d8406be pbrook
           BADF("Unimplemented CTEST4-FBL 0x%x\n", val);
1782 7d8406be pbrook
        }
1783 7d8406be pbrook
        s->ctest4 = val;
1784 7d8406be pbrook
        break;
1785 7d8406be pbrook
    case 0x22: /* CTEST5 */
1786 7d8406be pbrook
        if (val & (LSI_CTEST5_ADCK | LSI_CTEST5_BBCK)) {
1787 7d8406be pbrook
            BADF("CTEST5 DMA increment not implemented\n");
1788 7d8406be pbrook
        }
1789 7d8406be pbrook
        s->ctest5 = val;
1790 7d8406be pbrook
        break;
1791 49c47daa Sebastian Herbszt
    CASE_SET_REG24(dbc, 0x24)
1792 4b9a2d6d Sebastian Herbszt
    CASE_SET_REG32(dnad, 0x28)
1793 3d834c78 ths
    case 0x2c: /* DSP[0:7] */
1794 7d8406be pbrook
        s->dsp &= 0xffffff00;
1795 7d8406be pbrook
        s->dsp |= val;
1796 7d8406be pbrook
        break;
1797 3d834c78 ths
    case 0x2d: /* DSP[8:15] */
1798 7d8406be pbrook
        s->dsp &= 0xffff00ff;
1799 7d8406be pbrook
        s->dsp |= val << 8;
1800 7d8406be pbrook
        break;
1801 3d834c78 ths
    case 0x2e: /* DSP[16:23] */
1802 7d8406be pbrook
        s->dsp &= 0xff00ffff;
1803 7d8406be pbrook
        s->dsp |= val << 16;
1804 7d8406be pbrook
        break;
1805 3d834c78 ths
    case 0x2f: /* DSP[24:31] */
1806 7d8406be pbrook
        s->dsp &= 0x00ffffff;
1807 7d8406be pbrook
        s->dsp |= val << 24;
1808 7d8406be pbrook
        if ((s->dmode & LSI_DMODE_MAN) == 0
1809 7d8406be pbrook
            && (s->istat1 & LSI_ISTAT1_SRUN) == 0)
1810 7d8406be pbrook
            lsi_execute_script(s);
1811 7d8406be pbrook
        break;
1812 7d8406be pbrook
    CASE_SET_REG32(dsps, 0x30)
1813 7d8406be pbrook
    CASE_SET_REG32(scratch[0], 0x34)
1814 7d8406be pbrook
    case 0x38: /* DMODE */
1815 7d8406be pbrook
        if (val & (LSI_DMODE_SIOM | LSI_DMODE_DIOM)) {
1816 7d8406be pbrook
            BADF("IO mappings not implemented\n");
1817 7d8406be pbrook
        }
1818 7d8406be pbrook
        s->dmode = val;
1819 7d8406be pbrook
        break;
1820 7d8406be pbrook
    case 0x39: /* DIEN */
1821 7d8406be pbrook
        s->dien = val;
1822 7d8406be pbrook
        lsi_update_irq(s);
1823 7d8406be pbrook
        break;
1824 bd8ee11a Sebastian Herbszt
    case 0x3a: /* SBR */
1825 bd8ee11a Sebastian Herbszt
        s->sbr = val;
1826 bd8ee11a Sebastian Herbszt
        break;
1827 7d8406be pbrook
    case 0x3b: /* DCNTL */
1828 7d8406be pbrook
        s->dcntl = val & ~(LSI_DCNTL_PFF | LSI_DCNTL_STD);
1829 7d8406be pbrook
        if ((val & LSI_DCNTL_STD) && (s->istat1 & LSI_ISTAT1_SRUN) == 0)
1830 7d8406be pbrook
            lsi_execute_script(s);
1831 7d8406be pbrook
        break;
1832 7d8406be pbrook
    case 0x40: /* SIEN0 */
1833 7d8406be pbrook
        s->sien0 = val;
1834 7d8406be pbrook
        lsi_update_irq(s);
1835 7d8406be pbrook
        break;
1836 7d8406be pbrook
    case 0x41: /* SIEN1 */
1837 7d8406be pbrook
        s->sien1 = val;
1838 7d8406be pbrook
        lsi_update_irq(s);
1839 7d8406be pbrook
        break;
1840 7d8406be pbrook
    case 0x47: /* GPCNTL0 */
1841 7d8406be pbrook
        break;
1842 7d8406be pbrook
    case 0x48: /* STIME0 */
1843 7d8406be pbrook
        s->stime0 = val;
1844 7d8406be pbrook
        break;
1845 7d8406be pbrook
    case 0x49: /* STIME1 */
1846 7d8406be pbrook
        if (val & 0xf) {
1847 7d8406be pbrook
            DPRINTF("General purpose timer not implemented\n");
1848 7d8406be pbrook
            /* ??? Raising the interrupt immediately seems to be sufficient
1849 7d8406be pbrook
               to keep the FreeBSD driver happy.  */
1850 7d8406be pbrook
            lsi_script_scsi_interrupt(s, 0, LSI_SIST1_GEN);
1851 7d8406be pbrook
        }
1852 7d8406be pbrook
        break;
1853 7d8406be pbrook
    case 0x4a: /* RESPID0 */
1854 7d8406be pbrook
        s->respid0 = val;
1855 7d8406be pbrook
        break;
1856 7d8406be pbrook
    case 0x4b: /* RESPID1 */
1857 7d8406be pbrook
        s->respid1 = val;
1858 7d8406be pbrook
        break;
1859 7d8406be pbrook
    case 0x4d: /* STEST1 */
1860 7d8406be pbrook
        s->stest1 = val;
1861 7d8406be pbrook
        break;
1862 7d8406be pbrook
    case 0x4e: /* STEST2 */
1863 7d8406be pbrook
        if (val & 1) {
1864 7d8406be pbrook
            BADF("Low level mode not implemented\n");
1865 7d8406be pbrook
        }
1866 7d8406be pbrook
        s->stest2 = val;
1867 7d8406be pbrook
        break;
1868 7d8406be pbrook
    case 0x4f: /* STEST3 */
1869 7d8406be pbrook
        if (val & 0x41) {
1870 7d8406be pbrook
            BADF("SCSI FIFO test mode not implemented\n");
1871 7d8406be pbrook
        }
1872 7d8406be pbrook
        s->stest3 = val;
1873 7d8406be pbrook
        break;
1874 7d8406be pbrook
    case 0x56: /* CCNTL0 */
1875 7d8406be pbrook
        s->ccntl0 = val;
1876 7d8406be pbrook
        break;
1877 7d8406be pbrook
    case 0x57: /* CCNTL1 */
1878 7d8406be pbrook
        s->ccntl1 = val;
1879 7d8406be pbrook
        break;
1880 7d8406be pbrook
    CASE_SET_REG32(mmrs, 0xa0)
1881 7d8406be pbrook
    CASE_SET_REG32(mmws, 0xa4)
1882 7d8406be pbrook
    CASE_SET_REG32(sfs, 0xa8)
1883 7d8406be pbrook
    CASE_SET_REG32(drs, 0xac)
1884 7d8406be pbrook
    CASE_SET_REG32(sbms, 0xb0)
1885 ab57d967 aliguori
    CASE_SET_REG32(dbms, 0xb4)
1886 7d8406be pbrook
    CASE_SET_REG32(dnad64, 0xb8)
1887 7d8406be pbrook
    CASE_SET_REG32(pmjad1, 0xc0)
1888 7d8406be pbrook
    CASE_SET_REG32(pmjad2, 0xc4)
1889 7d8406be pbrook
    CASE_SET_REG32(rbc, 0xc8)
1890 7d8406be pbrook
    CASE_SET_REG32(ua, 0xcc)
1891 7d8406be pbrook
    CASE_SET_REG32(ia, 0xd4)
1892 7d8406be pbrook
    CASE_SET_REG32(sbc, 0xd8)
1893 7d8406be pbrook
    CASE_SET_REG32(csbc, 0xdc)
1894 7d8406be pbrook
    default:
1895 7d8406be pbrook
        if (offset >= 0x5c && offset < 0xa0) {
1896 7d8406be pbrook
            int n;
1897 7d8406be pbrook
            int shift;
1898 7d8406be pbrook
            n = (offset - 0x58) >> 2;
1899 7d8406be pbrook
            shift = (offset & 3) * 8;
1900 7d8406be pbrook
            s->scratch[n] &= ~(0xff << shift);
1901 7d8406be pbrook
            s->scratch[n] |= (val & 0xff) << shift;
1902 7d8406be pbrook
        } else {
1903 7d8406be pbrook
            BADF("Unhandled writeb 0x%x = 0x%x\n", offset, val);
1904 7d8406be pbrook
        }
1905 7d8406be pbrook
    }
1906 49c47daa Sebastian Herbszt
#undef CASE_SET_REG24
1907 7d8406be pbrook
#undef CASE_SET_REG32
1908 7d8406be pbrook
}
1909 7d8406be pbrook
1910 c227f099 Anthony Liguori
static void lsi_mmio_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
1911 7d8406be pbrook
{
1912 eb40f984 Juan Quintela
    LSIState *s = opaque;
1913 7d8406be pbrook
1914 7d8406be pbrook
    lsi_reg_writeb(s, addr & 0xff, val);
1915 7d8406be pbrook
}
1916 7d8406be pbrook
1917 c227f099 Anthony Liguori
static void lsi_mmio_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
1918 7d8406be pbrook
{
1919 eb40f984 Juan Quintela
    LSIState *s = opaque;
1920 7d8406be pbrook
1921 7d8406be pbrook
    addr &= 0xff;
1922 7d8406be pbrook
    lsi_reg_writeb(s, addr, val & 0xff);
1923 7d8406be pbrook
    lsi_reg_writeb(s, addr + 1, (val >> 8) & 0xff);
1924 7d8406be pbrook
}
1925 7d8406be pbrook
1926 c227f099 Anthony Liguori
static void lsi_mmio_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
1927 7d8406be pbrook
{
1928 eb40f984 Juan Quintela
    LSIState *s = opaque;
1929 7d8406be pbrook
1930 7d8406be pbrook
    addr &= 0xff;
1931 7d8406be pbrook
    lsi_reg_writeb(s, addr, val & 0xff);
1932 7d8406be pbrook
    lsi_reg_writeb(s, addr + 1, (val >> 8) & 0xff);
1933 7d8406be pbrook
    lsi_reg_writeb(s, addr + 2, (val >> 16) & 0xff);
1934 7d8406be pbrook
    lsi_reg_writeb(s, addr + 3, (val >> 24) & 0xff);
1935 7d8406be pbrook
}
1936 7d8406be pbrook
1937 c227f099 Anthony Liguori
static uint32_t lsi_mmio_readb(void *opaque, target_phys_addr_t addr)
1938 7d8406be pbrook
{
1939 eb40f984 Juan Quintela
    LSIState *s = opaque;
1940 7d8406be pbrook
1941 7d8406be pbrook
    return lsi_reg_readb(s, addr & 0xff);
1942 7d8406be pbrook
}
1943 7d8406be pbrook
1944 c227f099 Anthony Liguori
static uint32_t lsi_mmio_readw(void *opaque, target_phys_addr_t addr)
1945 7d8406be pbrook
{
1946 eb40f984 Juan Quintela
    LSIState *s = opaque;
1947 7d8406be pbrook
    uint32_t val;
1948 7d8406be pbrook
1949 7d8406be pbrook
    addr &= 0xff;
1950 7d8406be pbrook
    val = lsi_reg_readb(s, addr);
1951 7d8406be pbrook
    val |= lsi_reg_readb(s, addr + 1) << 8;
1952 7d8406be pbrook
    return val;
1953 7d8406be pbrook
}
1954 7d8406be pbrook
1955 c227f099 Anthony Liguori
static uint32_t lsi_mmio_readl(void *opaque, target_phys_addr_t addr)
1956 7d8406be pbrook
{
1957 eb40f984 Juan Quintela
    LSIState *s = opaque;
1958 7d8406be pbrook
    uint32_t val;
1959 7d8406be pbrook
    addr &= 0xff;
1960 7d8406be pbrook
    val = lsi_reg_readb(s, addr);
1961 7d8406be pbrook
    val |= lsi_reg_readb(s, addr + 1) << 8;
1962 7d8406be pbrook
    val |= lsi_reg_readb(s, addr + 2) << 16;
1963 7d8406be pbrook
    val |= lsi_reg_readb(s, addr + 3) << 24;
1964 7d8406be pbrook
    return val;
1965 7d8406be pbrook
}
1966 7d8406be pbrook
1967 d60efc6b Blue Swirl
static CPUReadMemoryFunc * const lsi_mmio_readfn[3] = {
1968 7d8406be pbrook
    lsi_mmio_readb,
1969 7d8406be pbrook
    lsi_mmio_readw,
1970 7d8406be pbrook
    lsi_mmio_readl,
1971 7d8406be pbrook
};
1972 7d8406be pbrook
1973 d60efc6b Blue Swirl
static CPUWriteMemoryFunc * const lsi_mmio_writefn[3] = {
1974 7d8406be pbrook
    lsi_mmio_writeb,
1975 7d8406be pbrook
    lsi_mmio_writew,
1976 7d8406be pbrook
    lsi_mmio_writel,
1977 7d8406be pbrook
};
1978 7d8406be pbrook
1979 c227f099 Anthony Liguori
static void lsi_ram_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
1980 7d8406be pbrook
{
1981 eb40f984 Juan Quintela
    LSIState *s = opaque;
1982 7d8406be pbrook
    uint32_t newval;
1983 7d8406be pbrook
    int shift;
1984 7d8406be pbrook
1985 7d8406be pbrook
    addr &= 0x1fff;
1986 7d8406be pbrook
    newval = s->script_ram[addr >> 2];
1987 7d8406be pbrook
    shift = (addr & 3) * 8;
1988 7d8406be pbrook
    newval &= ~(0xff << shift);
1989 7d8406be pbrook
    newval |= val << shift;
1990 7d8406be pbrook
    s->script_ram[addr >> 2] = newval;
1991 7d8406be pbrook
}
1992 7d8406be pbrook
1993 c227f099 Anthony Liguori
static void lsi_ram_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
1994 7d8406be pbrook
{
1995 eb40f984 Juan Quintela
    LSIState *s = opaque;
1996 7d8406be pbrook
    uint32_t newval;
1997 7d8406be pbrook
1998 7d8406be pbrook
    addr &= 0x1fff;
1999 7d8406be pbrook
    newval = s->script_ram[addr >> 2];
2000 7d8406be pbrook
    if (addr & 2) {
2001 7d8406be pbrook
        newval = (newval & 0xffff) | (val << 16);
2002 7d8406be pbrook
    } else {
2003 7d8406be pbrook
        newval = (newval & 0xffff0000) | val;
2004 7d8406be pbrook
    }
2005 7d8406be pbrook
    s->script_ram[addr >> 2] = newval;
2006 7d8406be pbrook
}
2007 7d8406be pbrook
2008 7d8406be pbrook
2009 c227f099 Anthony Liguori
static void lsi_ram_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
2010 7d8406be pbrook
{
2011 eb40f984 Juan Quintela
    LSIState *s = opaque;
2012 7d8406be pbrook
2013 7d8406be pbrook
    addr &= 0x1fff;
2014 7d8406be pbrook
    s->script_ram[addr >> 2] = val;
2015 7d8406be pbrook
}
2016 7d8406be pbrook
2017 c227f099 Anthony Liguori
static uint32_t lsi_ram_readb(void *opaque, target_phys_addr_t addr)
2018 7d8406be pbrook
{
2019 eb40f984 Juan Quintela
    LSIState *s = opaque;
2020 7d8406be pbrook
    uint32_t val;
2021 7d8406be pbrook
2022 7d8406be pbrook
    addr &= 0x1fff;
2023 7d8406be pbrook
    val = s->script_ram[addr >> 2];
2024 7d8406be pbrook
    val >>= (addr & 3) * 8;
2025 7d8406be pbrook
    return val & 0xff;
2026 7d8406be pbrook
}
2027 7d8406be pbrook
2028 c227f099 Anthony Liguori
static uint32_t lsi_ram_readw(void *opaque, target_phys_addr_t addr)
2029 7d8406be pbrook
{
2030 eb40f984 Juan Quintela
    LSIState *s = opaque;
2031 7d8406be pbrook
    uint32_t val;
2032 7d8406be pbrook
2033 7d8406be pbrook
    addr &= 0x1fff;
2034 7d8406be pbrook
    val = s->script_ram[addr >> 2];
2035 7d8406be pbrook
    if (addr & 2)
2036 7d8406be pbrook
        val >>= 16;
2037 3bd4be3a Aurelien Jarno
    return val;
2038 7d8406be pbrook
}
2039 7d8406be pbrook
2040 c227f099 Anthony Liguori
static uint32_t lsi_ram_readl(void *opaque, target_phys_addr_t addr)
2041 7d8406be pbrook
{
2042 eb40f984 Juan Quintela
    LSIState *s = opaque;
2043 7d8406be pbrook
2044 7d8406be pbrook
    addr &= 0x1fff;
2045 3bd4be3a Aurelien Jarno
    return s->script_ram[addr >> 2];
2046 7d8406be pbrook
}
2047 7d8406be pbrook
2048 d60efc6b Blue Swirl
static CPUReadMemoryFunc * const lsi_ram_readfn[3] = {
2049 7d8406be pbrook
    lsi_ram_readb,
2050 7d8406be pbrook
    lsi_ram_readw,
2051 7d8406be pbrook
    lsi_ram_readl,
2052 7d8406be pbrook
};
2053 7d8406be pbrook
2054 d60efc6b Blue Swirl
static CPUWriteMemoryFunc * const lsi_ram_writefn[3] = {
2055 7d8406be pbrook
    lsi_ram_writeb,
2056 7d8406be pbrook
    lsi_ram_writew,
2057 7d8406be pbrook
    lsi_ram_writel,
2058 7d8406be pbrook
};
2059 7d8406be pbrook
2060 7d8406be pbrook
static uint32_t lsi_io_readb(void *opaque, uint32_t addr)
2061 7d8406be pbrook
{
2062 eb40f984 Juan Quintela
    LSIState *s = opaque;
2063 7d8406be pbrook
    return lsi_reg_readb(s, addr & 0xff);
2064 7d8406be pbrook
}
2065 7d8406be pbrook
2066 7d8406be pbrook
static uint32_t lsi_io_readw(void *opaque, uint32_t addr)
2067 7d8406be pbrook
{
2068 eb40f984 Juan Quintela
    LSIState *s = opaque;
2069 7d8406be pbrook
    uint32_t val;
2070 7d8406be pbrook
    addr &= 0xff;
2071 7d8406be pbrook
    val = lsi_reg_readb(s, addr);
2072 7d8406be pbrook
    val |= lsi_reg_readb(s, addr + 1) << 8;
2073 7d8406be pbrook
    return val;
2074 7d8406be pbrook
}
2075 7d8406be pbrook
2076 7d8406be pbrook
static uint32_t lsi_io_readl(void *opaque, uint32_t addr)
2077 7d8406be pbrook
{
2078 eb40f984 Juan Quintela
    LSIState *s = opaque;
2079 7d8406be pbrook
    uint32_t val;
2080 7d8406be pbrook
    addr &= 0xff;
2081 7d8406be pbrook
    val = lsi_reg_readb(s, addr);
2082 7d8406be pbrook
    val |= lsi_reg_readb(s, addr + 1) << 8;
2083 7d8406be pbrook
    val |= lsi_reg_readb(s, addr + 2) << 16;
2084 7d8406be pbrook
    val |= lsi_reg_readb(s, addr + 3) << 24;
2085 7d8406be pbrook
    return val;
2086 7d8406be pbrook
}
2087 7d8406be pbrook
2088 7d8406be pbrook
static void lsi_io_writeb(void *opaque, uint32_t addr, uint32_t val)
2089 7d8406be pbrook
{
2090 eb40f984 Juan Quintela
    LSIState *s = opaque;
2091 7d8406be pbrook
    lsi_reg_writeb(s, addr & 0xff, val);
2092 7d8406be pbrook
}
2093 7d8406be pbrook
2094 7d8406be pbrook
static void lsi_io_writew(void *opaque, uint32_t addr, uint32_t val)
2095 7d8406be pbrook
{
2096 eb40f984 Juan Quintela
    LSIState *s = opaque;
2097 7d8406be pbrook
    addr &= 0xff;
2098 7d8406be pbrook
    lsi_reg_writeb(s, addr, val & 0xff);
2099 7d8406be pbrook
    lsi_reg_writeb(s, addr + 1, (val >> 8) & 0xff);
2100 7d8406be pbrook
}
2101 7d8406be pbrook
2102 7d8406be pbrook
static void lsi_io_writel(void *opaque, uint32_t addr, uint32_t val)
2103 7d8406be pbrook
{
2104 eb40f984 Juan Quintela
    LSIState *s = opaque;
2105 7d8406be pbrook
    addr &= 0xff;
2106 7d8406be pbrook
    lsi_reg_writeb(s, addr, val & 0xff);
2107 7d8406be pbrook
    lsi_reg_writeb(s, addr + 1, (val >> 8) & 0xff);
2108 7d8406be pbrook
    lsi_reg_writeb(s, addr + 2, (val >> 16) & 0xff);
2109 dcfb9014 ths
    lsi_reg_writeb(s, addr + 3, (val >> 24) & 0xff);
2110 7d8406be pbrook
}
2111 7d8406be pbrook
2112 5fafdf24 ths
static void lsi_io_mapfunc(PCIDevice *pci_dev, int region_num,
2113 6e355d90 Isaku Yamahata
                           pcibus_t addr, pcibus_t size, int type)
2114 7d8406be pbrook
{
2115 f305261f Juan Quintela
    LSIState *s = DO_UPCAST(LSIState, dev, pci_dev);
2116 7d8406be pbrook
2117 b4b2f054 Ryan Harper
    DPRINTF("Mapping IO at %08"FMT_PCIBUS"\n", addr);
2118 7d8406be pbrook
2119 7d8406be pbrook
    register_ioport_write(addr, 256, 1, lsi_io_writeb, s);
2120 7d8406be pbrook
    register_ioport_read(addr, 256, 1, lsi_io_readb, s);
2121 7d8406be pbrook
    register_ioport_write(addr, 256, 2, lsi_io_writew, s);
2122 7d8406be pbrook
    register_ioport_read(addr, 256, 2, lsi_io_readw, s);
2123 7d8406be pbrook
    register_ioport_write(addr, 256, 4, lsi_io_writel, s);
2124 7d8406be pbrook
    register_ioport_read(addr, 256, 4, lsi_io_readl, s);
2125 7d8406be pbrook
}
2126 7d8406be pbrook
2127 5fafdf24 ths
static void lsi_ram_mapfunc(PCIDevice *pci_dev, int region_num,
2128 6e355d90 Isaku Yamahata
                            pcibus_t addr, pcibus_t size, int type)
2129 7d8406be pbrook
{
2130 f305261f Juan Quintela
    LSIState *s = DO_UPCAST(LSIState, dev, pci_dev);
2131 7d8406be pbrook
2132 b4b2f054 Ryan Harper
    DPRINTF("Mapping ram at %08"FMT_PCIBUS"\n", addr);
2133 7d8406be pbrook
    s->script_ram_base = addr;
2134 7d8406be pbrook
    cpu_register_physical_memory(addr + 0, 0x2000, s->ram_io_addr);
2135 7d8406be pbrook
}
2136 7d8406be pbrook
2137 54eefd72 Jan Kiszka
static void lsi_scsi_reset(DeviceState *dev)
2138 54eefd72 Jan Kiszka
{
2139 54eefd72 Jan Kiszka
    LSIState *s = DO_UPCAST(LSIState, dev.qdev, dev);
2140 54eefd72 Jan Kiszka
2141 54eefd72 Jan Kiszka
    lsi_soft_reset(s);
2142 54eefd72 Jan Kiszka
}
2143 54eefd72 Jan Kiszka
2144 4a1b0f1c Juan Quintela
static void lsi_pre_save(void *opaque)
2145 777aec7a Nolan
{
2146 777aec7a Nolan
    LSIState *s = opaque;
2147 777aec7a Nolan
2148 b96a0da0 Gerd Hoffmann
    if (s->current) {
2149 b96a0da0 Gerd Hoffmann
        assert(s->current->dma_buf == NULL);
2150 b96a0da0 Gerd Hoffmann
        assert(s->current->dma_len == 0);
2151 b96a0da0 Gerd Hoffmann
    }
2152 042ec49d Gerd Hoffmann
    assert(QTAILQ_EMPTY(&s->queue));
2153 777aec7a Nolan
}
2154 777aec7a Nolan
2155 4a1b0f1c Juan Quintela
static const VMStateDescription vmstate_lsi_scsi = {
2156 4a1b0f1c Juan Quintela
    .name = "lsiscsi",
2157 4a1b0f1c Juan Quintela
    .version_id = 0,
2158 4a1b0f1c Juan Quintela
    .minimum_version_id = 0,
2159 4a1b0f1c Juan Quintela
    .minimum_version_id_old = 0,
2160 4a1b0f1c Juan Quintela
    .pre_save = lsi_pre_save,
2161 4a1b0f1c Juan Quintela
    .fields      = (VMStateField []) {
2162 4a1b0f1c Juan Quintela
        VMSTATE_PCI_DEVICE(dev, LSIState),
2163 4a1b0f1c Juan Quintela
2164 4a1b0f1c Juan Quintela
        VMSTATE_INT32(carry, LSIState),
2165 2f172849 Hannes Reinecke
        VMSTATE_INT32(status, LSIState),
2166 4a1b0f1c Juan Quintela
        VMSTATE_INT32(msg_action, LSIState),
2167 4a1b0f1c Juan Quintela
        VMSTATE_INT32(msg_len, LSIState),
2168 4a1b0f1c Juan Quintela
        VMSTATE_BUFFER(msg, LSIState),
2169 4a1b0f1c Juan Quintela
        VMSTATE_INT32(waiting, LSIState),
2170 4a1b0f1c Juan Quintela
2171 4a1b0f1c Juan Quintela
        VMSTATE_UINT32(dsa, LSIState),
2172 4a1b0f1c Juan Quintela
        VMSTATE_UINT32(temp, LSIState),
2173 4a1b0f1c Juan Quintela
        VMSTATE_UINT32(dnad, LSIState),
2174 4a1b0f1c Juan Quintela
        VMSTATE_UINT32(dbc, LSIState),
2175 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(istat0, LSIState),
2176 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(istat1, LSIState),
2177 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(dcmd, LSIState),
2178 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(dstat, LSIState),
2179 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(dien, LSIState),
2180 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(sist0, LSIState),
2181 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(sist1, LSIState),
2182 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(sien0, LSIState),
2183 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(sien1, LSIState),
2184 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(mbox0, LSIState),
2185 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(mbox1, LSIState),
2186 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(dfifo, LSIState),
2187 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(ctest2, LSIState),
2188 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(ctest3, LSIState),
2189 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(ctest4, LSIState),
2190 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(ctest5, LSIState),
2191 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(ccntl0, LSIState),
2192 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(ccntl1, LSIState),
2193 4a1b0f1c Juan Quintela
        VMSTATE_UINT32(dsp, LSIState),
2194 4a1b0f1c Juan Quintela
        VMSTATE_UINT32(dsps, LSIState),
2195 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(dmode, LSIState),
2196 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(dcntl, LSIState),
2197 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(scntl0, LSIState),
2198 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(scntl1, LSIState),
2199 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(scntl2, LSIState),
2200 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(scntl3, LSIState),
2201 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(sstat0, LSIState),
2202 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(sstat1, LSIState),
2203 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(scid, LSIState),
2204 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(sxfer, LSIState),
2205 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(socl, LSIState),
2206 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(sdid, LSIState),
2207 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(ssid, LSIState),
2208 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(sfbr, LSIState),
2209 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(stest1, LSIState),
2210 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(stest2, LSIState),
2211 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(stest3, LSIState),
2212 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(sidl, LSIState),
2213 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(stime0, LSIState),
2214 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(respid0, LSIState),
2215 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(respid1, LSIState),
2216 4a1b0f1c Juan Quintela
        VMSTATE_UINT32(mmrs, LSIState),
2217 4a1b0f1c Juan Quintela
        VMSTATE_UINT32(mmws, LSIState),
2218 4a1b0f1c Juan Quintela
        VMSTATE_UINT32(sfs, LSIState),
2219 4a1b0f1c Juan Quintela
        VMSTATE_UINT32(drs, LSIState),
2220 4a1b0f1c Juan Quintela
        VMSTATE_UINT32(sbms, LSIState),
2221 4a1b0f1c Juan Quintela
        VMSTATE_UINT32(dbms, LSIState),
2222 4a1b0f1c Juan Quintela
        VMSTATE_UINT32(dnad64, LSIState),
2223 4a1b0f1c Juan Quintela
        VMSTATE_UINT32(pmjad1, LSIState),
2224 4a1b0f1c Juan Quintela
        VMSTATE_UINT32(pmjad2, LSIState),
2225 4a1b0f1c Juan Quintela
        VMSTATE_UINT32(rbc, LSIState),
2226 4a1b0f1c Juan Quintela
        VMSTATE_UINT32(ua, LSIState),
2227 4a1b0f1c Juan Quintela
        VMSTATE_UINT32(ia, LSIState),
2228 4a1b0f1c Juan Quintela
        VMSTATE_UINT32(sbc, LSIState),
2229 4a1b0f1c Juan Quintela
        VMSTATE_UINT32(csbc, LSIState),
2230 4a1b0f1c Juan Quintela
        VMSTATE_BUFFER_UNSAFE(scratch, LSIState, 0, 18 * sizeof(uint32_t)),
2231 4a1b0f1c Juan Quintela
        VMSTATE_UINT8(sbr, LSIState),
2232 4a1b0f1c Juan Quintela
2233 4a1b0f1c Juan Quintela
        VMSTATE_BUFFER_UNSAFE(script_ram, LSIState, 0, 2048 * sizeof(uint32_t)),
2234 4a1b0f1c Juan Quintela
        VMSTATE_END_OF_LIST()
2235 777aec7a Nolan
    }
2236 4a1b0f1c Juan Quintela
};
2237 777aec7a Nolan
2238 4b09be85 aliguori
static int lsi_scsi_uninit(PCIDevice *d)
2239 4b09be85 aliguori
{
2240 f305261f Juan Quintela
    LSIState *s = DO_UPCAST(LSIState, dev, d);
2241 4b09be85 aliguori
2242 4b09be85 aliguori
    cpu_unregister_io_memory(s->mmio_io_addr);
2243 4b09be85 aliguori
    cpu_unregister_io_memory(s->ram_io_addr);
2244 4b09be85 aliguori
2245 4b09be85 aliguori
    return 0;
2246 4b09be85 aliguori
}
2247 4b09be85 aliguori
2248 cfdc1bb0 Paolo Bonzini
static const struct SCSIBusOps lsi_scsi_ops = {
2249 94d3f98a Paolo Bonzini
    .complete = lsi_command_complete,
2250 94d3f98a Paolo Bonzini
    .cancel = lsi_request_cancelled
2251 cfdc1bb0 Paolo Bonzini
};
2252 cfdc1bb0 Paolo Bonzini
2253 81a322d4 Gerd Hoffmann
static int lsi_scsi_init(PCIDevice *dev)
2254 7d8406be pbrook
{
2255 f305261f Juan Quintela
    LSIState *s = DO_UPCAST(LSIState, dev, dev);
2256 deb54399 aliguori
    uint8_t *pci_conf;
2257 7d8406be pbrook
2258 f305261f Juan Quintela
    pci_conf = s->dev.config;
2259 deb54399 aliguori
2260 9167a69a balrog
    /* PCI Vendor ID (word) */
2261 deb54399 aliguori
    pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_LSI_LOGIC);
2262 9167a69a balrog
    /* PCI device ID (word) */
2263 deb54399 aliguori
    pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_LSI_53C895A);
2264 9167a69a balrog
    /* PCI base class code */
2265 173a543b blueswir1
    pci_config_set_class(pci_conf, PCI_CLASS_STORAGE_SCSI);
2266 9167a69a balrog
    /* PCI subsystem ID */
2267 5845f0e5 Michael S. Tsirkin
    pci_conf[PCI_SUBSYSTEM_ID] = 0x00;
2268 5845f0e5 Michael S. Tsirkin
    pci_conf[PCI_SUBSYSTEM_ID + 1] = 0x10;
2269 9167a69a balrog
    /* PCI latency timer = 255 */
2270 5845f0e5 Michael S. Tsirkin
    pci_conf[PCI_LATENCY_TIMER] = 0xff;
2271 5845f0e5 Michael S. Tsirkin
    /* TODO: RST# value should be 0 */
2272 9167a69a balrog
    /* Interrupt pin 1 */
2273 5845f0e5 Michael S. Tsirkin
    pci_conf[PCI_INTERRUPT_PIN] = 0x01;
2274 7d8406be pbrook
2275 1eed09cb Avi Kivity
    s->mmio_io_addr = cpu_register_io_memory(lsi_mmio_readfn,
2276 2507c12a Alexander Graf
                                             lsi_mmio_writefn, s,
2277 2507c12a Alexander Graf
                                             DEVICE_NATIVE_ENDIAN);
2278 1eed09cb Avi Kivity
    s->ram_io_addr = cpu_register_io_memory(lsi_ram_readfn,
2279 2507c12a Alexander Graf
                                            lsi_ram_writefn, s,
2280 2507c12a Alexander Graf
                                            DEVICE_NATIVE_ENDIAN);
2281 7d8406be pbrook
2282 b90c73cf Stefan Weil
    pci_register_bar(&s->dev, 0, 256,
2283 0392a017 Isaku Yamahata
                           PCI_BASE_ADDRESS_SPACE_IO, lsi_io_mapfunc);
2284 f32dd06b Avi Kivity
    pci_register_bar_simple(&s->dev, 1, 0x400, 0, s->mmio_io_addr);
2285 b90c73cf Stefan Weil
    pci_register_bar(&s->dev, 2, 0x2000,
2286 0392a017 Isaku Yamahata
                           PCI_BASE_ADDRESS_SPACE_MEMORY, lsi_ram_mapfunc);
2287 042ec49d Gerd Hoffmann
    QTAILQ_INIT(&s->queue);
2288 7d8406be pbrook
2289 cfdc1bb0 Paolo Bonzini
    scsi_bus_new(&s->bus, &dev->qdev, 1, LSI_MAX_DEVS, &lsi_scsi_ops);
2290 5b684b5a Gerd Hoffmann
    if (!dev->qdev.hotplugged) {
2291 fa66b909 Markus Armbruster
        return scsi_bus_legacy_handle_cmdline(&s->bus);
2292 5b684b5a Gerd Hoffmann
    }
2293 81a322d4 Gerd Hoffmann
    return 0;
2294 7d8406be pbrook
}
2295 9be5dafe Paul Brook
2296 0aab0d3a Gerd Hoffmann
static PCIDeviceInfo lsi_info = {
2297 d52affa7 Gerd Hoffmann
    .qdev.name  = "lsi53c895a",
2298 d52affa7 Gerd Hoffmann
    .qdev.alias = "lsi",
2299 d52affa7 Gerd Hoffmann
    .qdev.size  = sizeof(LSIState),
2300 54eefd72 Jan Kiszka
    .qdev.reset = lsi_scsi_reset,
2301 be73cfe2 Juan Quintela
    .qdev.vmsd  = &vmstate_lsi_scsi,
2302 d52affa7 Gerd Hoffmann
    .init       = lsi_scsi_init,
2303 e3936fa5 Gerd Hoffmann
    .exit       = lsi_scsi_uninit,
2304 0aab0d3a Gerd Hoffmann
};
2305 0aab0d3a Gerd Hoffmann
2306 9be5dafe Paul Brook
static void lsi53c895a_register_devices(void)
2307 9be5dafe Paul Brook
{
2308 0aab0d3a Gerd Hoffmann
    pci_qdev_register(&lsi_info);
2309 9be5dafe Paul Brook
}
2310 9be5dafe Paul Brook
2311 9be5dafe Paul Brook
device_init(lsi53c895a_register_devices);