sii.c revision 1.1.18.2 1 1.1.18.2 yamt /* $NetBSD: sii.c,v 1.1.18.2 2007/09/03 14:28:51 yamt Exp $ */
2 1.1.18.1 yamt
3 1.1.18.1 yamt /*-
4 1.1.18.1 yamt * Copyright (c) 1992, 1993
5 1.1.18.1 yamt * The Regents of the University of California. All rights reserved.
6 1.1.18.1 yamt *
7 1.1.18.1 yamt * This code is derived from software contributed to Berkeley by
8 1.1.18.1 yamt * Ralph Campbell and Rick Macklem.
9 1.1.18.1 yamt *
10 1.1.18.1 yamt * Redistribution and use in source and binary forms, with or without
11 1.1.18.1 yamt * modification, are permitted provided that the following conditions
12 1.1.18.1 yamt * are met:
13 1.1.18.1 yamt * 1. Redistributions of source code must retain the above copyright
14 1.1.18.1 yamt * notice, this list of conditions and the following disclaimer.
15 1.1.18.1 yamt * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.18.1 yamt * notice, this list of conditions and the following disclaimer in the
17 1.1.18.1 yamt * documentation and/or other materials provided with the distribution.
18 1.1.18.1 yamt * 3. Neither the name of the University nor the names of its contributors
19 1.1.18.1 yamt * may be used to endorse or promote products derived from this software
20 1.1.18.1 yamt * without specific prior written permission.
21 1.1.18.1 yamt *
22 1.1.18.1 yamt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1.18.1 yamt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1.18.1 yamt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1.18.1 yamt * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1.18.1 yamt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1.18.1 yamt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1.18.1 yamt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1.18.1 yamt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1.18.1 yamt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1.18.1 yamt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1.18.1 yamt * SUCH DAMAGE.
33 1.1.18.1 yamt *
34 1.1.18.1 yamt * @(#)sii.c 8.2 (Berkeley) 11/30/93
35 1.1.18.1 yamt *
36 1.1.18.1 yamt * from: Header: /sprite/src/kernel/dev/ds3100.md/RCS/devSII.c,
37 1.1.18.1 yamt * v 9.2 89/09/14 13:37:41 jhh Exp $ SPRITE (DECWRL)";
38 1.1.18.1 yamt */
39 1.1.18.1 yamt
40 1.1.18.1 yamt #include <sys/cdefs.h>
41 1.1.18.2 yamt __KERNEL_RCSID(0, "$NetBSD: sii.c,v 1.1.18.2 2007/09/03 14:28:51 yamt Exp $");
42 1.1.18.1 yamt
43 1.1.18.1 yamt #include "sii.h"
44 1.1.18.1 yamt /*
45 1.1.18.1 yamt * SCSI interface driver
46 1.1.18.1 yamt */
47 1.1.18.1 yamt #include <sys/param.h>
48 1.1.18.1 yamt #include <sys/buf.h>
49 1.1.18.1 yamt #include <sys/conf.h>
50 1.1.18.1 yamt #include <sys/device.h>
51 1.1.18.1 yamt #include <sys/systm.h>
52 1.1.18.1 yamt
53 1.1.18.1 yamt #include <machine/locore.h>
54 1.1.18.1 yamt
55 1.1.18.1 yamt #include <dev/scsipi/scsi_all.h>
56 1.1.18.1 yamt #include <dev/scsipi/scsi_message.h>
57 1.1.18.1 yamt #include <dev/scsipi/scsipi_all.h>
58 1.1.18.1 yamt #include <dev/scsipi/scsipi_disk.h>
59 1.1.18.1 yamt #include <dev/scsipi/scsiconf.h>
60 1.1.18.1 yamt
61 1.1.18.1 yamt /* old 4.4BSD/pmax scsi drivers */
62 1.1.18.1 yamt #include <pmax/ibus/siireg.h> /* device registers */
63 1.1.18.1 yamt #include <pmax/ibus/siivar.h> /* softc and prototypes */
64 1.1.18.1 yamt
65 1.1.18.1 yamt #include <pmax/pmax/machdep.h> /* prom_scsiid prototype */
66 1.1.18.1 yamt
67 1.1.18.1 yamt /* XXX not in dev/scsipi/scsi_message.h */
68 1.1.18.1 yamt #define MSG_EXT_MODIFY_DATA_PTR 0x00
69 1.1.18.1 yamt
70 1.1.18.1 yamt extern struct cfdriver sii_cd;
71 1.1.18.1 yamt
72 1.1.18.1 yamt /*
73 1.1.18.1 yamt * MACROS for timing out spin loops.
74 1.1.18.1 yamt *
75 1.1.18.1 yamt * Wait until expression is true.
76 1.1.18.1 yamt *
77 1.1.18.1 yamt * Control register bits can change at any time so when the CPU
78 1.1.18.1 yamt * reads a register, the bits might change and
79 1.1.18.1 yamt * invalidate the setup and hold times for the CPU.
80 1.1.18.1 yamt * This macro reads the register twice to be sure the value is stable.
81 1.1.18.1 yamt *
82 1.1.18.1 yamt * args: var - variable to save control register contents
83 1.1.18.1 yamt * reg - control register to read
84 1.1.18.1 yamt * expr - expression to spin on
85 1.1.18.1 yamt * spincount - maximum number of times through the loop
86 1.1.18.1 yamt * cntr - variable for number of tries
87 1.1.18.1 yamt */
88 1.1.18.1 yamt #define SII_WAIT_UNTIL(var, reg, expr, spincount, cntr) { \
89 1.1.18.1 yamt u_int tmp = reg; \
90 1.1.18.1 yamt for (cntr = 0; cntr < spincount; cntr++) { \
91 1.1.18.1 yamt while (tmp != (var = reg)) \
92 1.1.18.1 yamt tmp = var; \
93 1.1.18.1 yamt if (expr) \
94 1.1.18.1 yamt break; \
95 1.1.18.1 yamt if (cntr >= 100) \
96 1.1.18.1 yamt DELAY(100); \
97 1.1.18.1 yamt } \
98 1.1.18.1 yamt }
99 1.1.18.1 yamt
100 1.1.18.1 yamt #ifdef DEBUG
101 1.1.18.1 yamt int sii_debug = 1;
102 1.1.18.1 yamt int sii_debug_cmd;
103 1.1.18.1 yamt int sii_debug_bn;
104 1.1.18.1 yamt int sii_debug_sz;
105 1.1.18.1 yamt #define NLOG 16
106 1.1.18.1 yamt struct sii_log {
107 1.1.18.1 yamt u_short cstat;
108 1.1.18.1 yamt u_short dstat;
109 1.1.18.1 yamt u_short comm;
110 1.1.18.1 yamt u_short msg;
111 1.1.18.1 yamt int rlen;
112 1.1.18.1 yamt int dlen;
113 1.1.18.1 yamt int target;
114 1.1.18.1 yamt } sii_log[NLOG], *sii_logp = sii_log;
115 1.1.18.1 yamt #endif
116 1.1.18.1 yamt
117 1.1.18.1 yamt static u_char sii_buf[256]; /* used for extended messages */
118 1.1.18.1 yamt
119 1.1.18.1 yamt #define NORESET 0
120 1.1.18.1 yamt #define RESET 1
121 1.1.18.1 yamt #define NOWAIT 0
122 1.1.18.1 yamt #define WAIT 1
123 1.1.18.1 yamt
124 1.1.18.1 yamt
125 1.1.18.1 yamt /*
126 1.1.18.1 yamt * Define a safe address in the SCSI buffer for doing status & message DMA
127 1.1.18.1 yamt * XXX why not add another field to softc?
128 1.1.18.1 yamt */
129 1.1.18.1 yamt #define SII_BUF_ADDR(sc) ((sc)->sc_buf + SII_MAX_DMA_XFER_LENGTH * 14)
130 1.1.18.1 yamt
131 1.1.18.1 yamt /*
132 1.1.18.1 yamt * Forward references
133 1.1.18.1 yamt */
134 1.1.18.1 yamt
135 1.1.18.1 yamt static void sii_Reset __P((struct siisoftc *sc, int resetbus));
136 1.1.18.1 yamt static void sii_StartCmd __P((struct siisoftc *sc, int target));
137 1.1.18.1 yamt static void sii_CmdDone __P((struct siisoftc *sc, int target, int error));
138 1.1.18.1 yamt static void sii_DoIntr __P((struct siisoftc *sc, u_int dstat));
139 1.1.18.1 yamt static void sii_StateChg __P((struct siisoftc *sc, u_int cstat));
140 1.1.18.1 yamt static int sii_GetByte __P((SIIRegs *regs, int phase, int ack));
141 1.1.18.1 yamt static void sii_DoSync __P((SIIRegs *regs, State *state));
142 1.1.18.1 yamt static void sii_StartDMA __P((SIIRegs *regs, int phase, u_short *dmaAddr,
143 1.1.18.1 yamt int size));
144 1.1.18.1 yamt
145 1.1.18.1 yamt #ifdef DEBUG
146 1.1.18.1 yamt static void sii_DumpLog __P((void));
147 1.1.18.1 yamt #endif
148 1.1.18.1 yamt
149 1.1.18.1 yamt
150 1.1.18.1 yamt /*
151 1.1.18.1 yamt * Match driver based on name
152 1.1.18.1 yamt */
153 1.1.18.1 yamt void
154 1.1.18.1 yamt siiattach(sc)
155 1.1.18.1 yamt struct siisoftc *sc;
156 1.1.18.1 yamt {
157 1.1.18.1 yamt int i;
158 1.1.18.1 yamt
159 1.1.18.1 yamt sc->sc_target = -1; /* no command active */
160 1.1.18.1 yamt
161 1.1.18.1 yamt /*
162 1.1.18.1 yamt * Give each target its own DMA buffer region.
163 1.1.18.1 yamt * Make it big enough for 2 max transfers so we can ping pong buffers
164 1.1.18.1 yamt * while we copy the data.
165 1.1.18.1 yamt */
166 1.1.18.1 yamt for (i = 0; i < SII_NCMD; i++) {
167 1.1.18.1 yamt sc->sc_st[i].dmaAddr[0] = (u_short *)
168 1.1.18.1 yamt sc->sc_buf + 2 * SII_MAX_DMA_XFER_LENGTH * i;
169 1.1.18.1 yamt sc->sc_st[i].dmaAddr[1] = sc->sc_st[i].dmaAddr[0] +
170 1.1.18.1 yamt SII_MAX_DMA_XFER_LENGTH;
171 1.1.18.1 yamt }
172 1.1.18.1 yamt
173 1.1.18.1 yamt sii_Reset(sc, RESET);
174 1.1.18.1 yamt printf(": target %d\n", sc->sc_regs->id & SII_IDMSK);
175 1.1.18.1 yamt
176 1.1.18.1 yamt sc->sc_adapter.adapt_dev = &sc->sc_dev;
177 1.1.18.1 yamt sc->sc_adapter.adapt_nchannels = 1;
178 1.1.18.1 yamt sc->sc_adapter.adapt_openings = 7;
179 1.1.18.1 yamt sc->sc_adapter.adapt_max_periph = 1;
180 1.1.18.1 yamt sc->sc_adapter.adapt_ioctl = NULL;
181 1.1.18.1 yamt sc->sc_adapter.adapt_minphys = minphys;
182 1.1.18.1 yamt sc->sc_adapter.adapt_request = sii_scsi_request;
183 1.1.18.1 yamt
184 1.1.18.1 yamt sc->sc_channel.chan_adapter = &sc->sc_adapter;
185 1.1.18.1 yamt sc->sc_channel.chan_bustype = &scsi_bustype;
186 1.1.18.1 yamt sc->sc_channel.chan_channel = 0;
187 1.1.18.1 yamt sc->sc_channel.chan_ntargets = 8;
188 1.1.18.1 yamt sc->sc_channel.chan_nluns = 8;
189 1.1.18.1 yamt sc->sc_channel.chan_id = sc->sc_regs->id & SII_IDMSK;
190 1.1.18.1 yamt
191 1.1.18.1 yamt
192 1.1.18.1 yamt /*
193 1.1.18.1 yamt * Now try to attach all the sub-devices
194 1.1.18.1 yamt */
195 1.1.18.1 yamt config_found(&sc->sc_dev, &sc->sc_channel, scsiprint);
196 1.1.18.1 yamt }
197 1.1.18.1 yamt
198 1.1.18.1 yamt /*
199 1.1.18.1 yamt * Start activity on a SCSI device.
200 1.1.18.1 yamt * We maintain information on each device separately since devices can
201 1.1.18.1 yamt * connect/disconnect during an operation.
202 1.1.18.1 yamt */
203 1.1.18.1 yamt
204 1.1.18.1 yamt void
205 1.1.18.1 yamt sii_scsi_request(chan, req, arg)
206 1.1.18.1 yamt struct scsipi_channel *chan;
207 1.1.18.1 yamt scsipi_adapter_req_t req;
208 1.1.18.1 yamt void *arg;
209 1.1.18.1 yamt {
210 1.1.18.1 yamt struct scsipi_xfer *xs;
211 1.1.18.1 yamt struct scsipi_periph *periph;
212 1.1.18.1 yamt struct siisoftc *sc = (void *)chan->chan_adapter->adapt_dev;
213 1.1.18.1 yamt int target;
214 1.1.18.1 yamt int s;
215 1.1.18.1 yamt int count;
216 1.1.18.1 yamt
217 1.1.18.1 yamt switch (req) {
218 1.1.18.1 yamt case ADAPTER_REQ_RUN_XFER:
219 1.1.18.1 yamt xs = arg;
220 1.1.18.1 yamt periph = xs->xs_periph;
221 1.1.18.1 yamt target = periph->periph_target;
222 1.1.18.1 yamt s = splbio();
223 1.1.18.1 yamt if (sc->sc_cmd[target]) {
224 1.1.18.1 yamt splx(s);
225 1.1.18.1 yamt xs->error = XS_RESOURCE_SHORTAGE;
226 1.1.18.1 yamt scsipi_done(xs);
227 1.1.18.1 yamt printf("[busy at start]\n");
228 1.1.18.1 yamt return;
229 1.1.18.1 yamt }
230 1.1.18.1 yamt /*
231 1.1.18.1 yamt * Build a ScsiCmd for this command and start it.
232 1.1.18.1 yamt */
233 1.1.18.1 yamt sc->sc_xs[target] = xs;
234 1.1.18.1 yamt sc->sc_cmd[target] = &sc->sc_cmd_fake[target]; /* XXX */
235 1.1.18.1 yamt sc->sc_cmd[target]->unit = 0;
236 1.1.18.1 yamt sc->sc_cmd[target]->flags = 0;
237 1.1.18.1 yamt sc->sc_cmd[target]->buflen = xs->datalen;
238 1.1.18.1 yamt sc->sc_cmd[target]->buf = xs->data;
239 1.1.18.1 yamt sc->sc_cmd[target]->cmdlen = xs->cmdlen;
240 1.1.18.1 yamt sc->sc_cmd[target]->cmd = (u_char *)xs->cmd;
241 1.1.18.1 yamt sc->sc_cmd[target]->lun = xs->xs_periph->periph_lun;
242 1.1.18.1 yamt sii_StartCmd(sc, target);
243 1.1.18.1 yamt splx(s);
244 1.1.18.1 yamt if ((xs->xs_control & XS_CTL_POLL) == 0)
245 1.1.18.1 yamt return;
246 1.1.18.1 yamt count = xs->timeout;
247 1.1.18.1 yamt while (count) {
248 1.1.18.1 yamt if ((xs->xs_status & XS_STS_DONE) != 0)
249 1.1.18.1 yamt return;
250 1.1.18.1 yamt siiintr(sc);
251 1.1.18.1 yamt /* XXX schedule another command? */
252 1.1.18.1 yamt DELAY(1000);
253 1.1.18.1 yamt --count;
254 1.1.18.1 yamt }
255 1.1.18.1 yamt xs->error = XS_TIMEOUT;
256 1.1.18.1 yamt scsipi_done(xs);
257 1.1.18.1 yamt return;
258 1.1.18.1 yamt case ADAPTER_REQ_GROW_RESOURCES:
259 1.1.18.1 yamt /* XXX Not supported. */
260 1.1.18.1 yamt return;
261 1.1.18.1 yamt
262 1.1.18.1 yamt case ADAPTER_REQ_SET_XFER_MODE:
263 1.1.18.1 yamt /* XXX Not supported. */
264 1.1.18.1 yamt return;
265 1.1.18.1 yamt }
266 1.1.18.1 yamt }
267 1.1.18.1 yamt
268 1.1.18.1 yamt /*
269 1.1.18.1 yamt * Check to see if any SII chips have pending interrupts
270 1.1.18.1 yamt * and process as appropriate.
271 1.1.18.1 yamt */
272 1.1.18.1 yamt int
273 1.1.18.1 yamt siiintr(xxxsc)
274 1.1.18.1 yamt void *xxxsc;
275 1.1.18.1 yamt {
276 1.1.18.1 yamt struct siisoftc *sc = xxxsc;
277 1.1.18.1 yamt u_int dstat;
278 1.1.18.1 yamt
279 1.1.18.1 yamt /*
280 1.1.18.1 yamt * Find which controller caused the interrupt.
281 1.1.18.1 yamt */
282 1.1.18.1 yamt dstat = sc->sc_regs->dstat;
283 1.1.18.1 yamt if (dstat & (SII_CI | SII_DI)) {
284 1.1.18.1 yamt sii_DoIntr(sc, dstat);
285 1.1.18.1 yamt return (0); /* XXX */
286 1.1.18.1 yamt }
287 1.1.18.1 yamt
288 1.1.18.1 yamt return (1); /* XXX spurious interrupt? */
289 1.1.18.1 yamt }
290 1.1.18.1 yamt
291 1.1.18.1 yamt /*
292 1.1.18.1 yamt * Reset the SII chip and do a SCSI reset if 'reset' is true.
293 1.1.18.1 yamt * NOTE: if !cold && reset, should probably probe for devices
294 1.1.18.1 yamt * since a SCSI bus reset will set UNIT_ATTENTION.
295 1.1.18.1 yamt */
296 1.1.18.1 yamt static void
297 1.1.18.1 yamt sii_Reset(sc, reset)
298 1.1.18.1 yamt struct siisoftc* sc;
299 1.1.18.1 yamt int reset; /* TRUE => reset SCSI bus */
300 1.1.18.1 yamt {
301 1.1.18.1 yamt SIIRegs *regs = sc->sc_regs;
302 1.1.18.1 yamt
303 1.1.18.1 yamt #ifdef DEBUG
304 1.1.18.1 yamt if (sii_debug > 1)
305 1.1.18.1 yamt printf("sii: RESET\n");
306 1.1.18.1 yamt #endif
307 1.1.18.1 yamt /*
308 1.1.18.1 yamt * Reset the SII chip.
309 1.1.18.1 yamt */
310 1.1.18.1 yamt regs->comm = SII_CHRESET;
311 1.1.18.1 yamt /*
312 1.1.18.1 yamt * Set arbitrated bus mode.
313 1.1.18.1 yamt */
314 1.1.18.1 yamt regs->csr = SII_HPM;
315 1.1.18.1 yamt /*
316 1.1.18.1 yamt * Set host adapter ID (from PROM sciiidN variable).
317 1.1.18.1 yamt */
318 1.1.18.1 yamt /* XXX device_unit() abuse */
319 1.1.18.1 yamt regs->id = SII_ID_IO | prom_scsiid(device_unit(&sc->sc_dev));
320 1.1.18.1 yamt /*
321 1.1.18.1 yamt * Enable SII to drive the SCSI bus.
322 1.1.18.1 yamt */
323 1.1.18.1 yamt regs->dictrl = SII_PRE;
324 1.1.18.1 yamt regs->dmctrl = 0;
325 1.1.18.1 yamt
326 1.1.18.1 yamt if (reset) {
327 1.1.18.1 yamt int i;
328 1.1.18.1 yamt
329 1.1.18.1 yamt /*
330 1.1.18.1 yamt * Assert SCSI bus reset for at least 25 Usec to clear the
331 1.1.18.1 yamt * world. SII_DO_RST is self clearing.
332 1.1.18.1 yamt * Delay 250 ms before doing any commands.
333 1.1.18.1 yamt */
334 1.1.18.1 yamt regs->comm = SII_DO_RST;
335 1.1.18.1 yamt wbflush();
336 1.1.18.1 yamt DELAY(250000);
337 1.1.18.1 yamt
338 1.1.18.1 yamt /* rearbitrate synchronous offset */
339 1.1.18.1 yamt for (i = 0; i < SII_NCMD; i++)
340 1.1.18.1 yamt sc->sc_st[i].dmaReqAck = 0;
341 1.1.18.1 yamt }
342 1.1.18.1 yamt
343 1.1.18.1 yamt /*
344 1.1.18.1 yamt * Clear any pending interrupts from the reset.
345 1.1.18.1 yamt */
346 1.1.18.1 yamt regs->cstat = regs->cstat;
347 1.1.18.1 yamt regs->dstat = regs->dstat;
348 1.1.18.1 yamt /*
349 1.1.18.1 yamt * Set up SII for arbitrated bus mode, SCSI parity checking,
350 1.1.18.1 yamt * Reselect Enable, and Interrupt Enable.
351 1.1.18.1 yamt */
352 1.1.18.1 yamt regs->csr = SII_HPM | SII_RSE | SII_PCE | SII_IE;
353 1.1.18.1 yamt wbflush();
354 1.1.18.1 yamt }
355 1.1.18.1 yamt
356 1.1.18.1 yamt /*
357 1.1.18.1 yamt * Start a SCSI command by sending the cmd data
358 1.1.18.1 yamt * to a SCSI controller via the SII.
359 1.1.18.1 yamt * Call the device done proceedure if it can't be started.
360 1.1.18.1 yamt * NOTE: we should be called with interrupts disabled.
361 1.1.18.1 yamt */
362 1.1.18.1 yamt static void
363 1.1.18.1 yamt sii_StartCmd(sc, target)
364 1.1.18.1 yamt struct siisoftc *sc; /* which SII to use */
365 1.1.18.1 yamt int target; /* which command to start */
366 1.1.18.1 yamt {
367 1.1.18.1 yamt SIIRegs *regs;
368 1.1.18.1 yamt ScsiCmd *scsicmd;
369 1.1.18.1 yamt State *state;
370 1.1.18.1 yamt u_int status;
371 1.1.18.1 yamt int error, retval;
372 1.1.18.1 yamt
373 1.1.18.1 yamt /* if another command is currently in progress, just wait */
374 1.1.18.1 yamt if (sc->sc_target >= 0)
375 1.1.18.1 yamt return;
376 1.1.18.1 yamt
377 1.1.18.1 yamt /* initialize state information for this command */
378 1.1.18.1 yamt scsicmd = sc->sc_cmd[target];
379 1.1.18.1 yamt state = &sc->sc_st[target];
380 1.1.18.1 yamt state->flags = FIRST_DMA;
381 1.1.18.1 yamt state->prevComm = 0;
382 1.1.18.1 yamt state->dmalen = 0;
383 1.1.18.1 yamt state->dmaCurPhase = -1;
384 1.1.18.1 yamt state->dmaPrevPhase = -1;
385 1.1.18.1 yamt state->dmaBufIndex = 0;
386 1.1.18.1 yamt state->cmd = scsicmd->cmd;
387 1.1.18.1 yamt state->cmdlen = scsicmd->cmdlen;
388 1.1.18.1 yamt if ((state->buflen = scsicmd->buflen) == 0) {
389 1.1.18.1 yamt state->dmaDataPhase = -1; /* illegal phase. shouldn't happen */
390 1.1.18.1 yamt state->buf = (char *)0;
391 1.1.18.1 yamt } else {
392 1.1.18.1 yamt state->buf = scsicmd->buf;
393 1.1.18.1 yamt }
394 1.1.18.1 yamt
395 1.1.18.1 yamt #ifdef DEBUG
396 1.1.18.1 yamt if (sii_debug > 1) {
397 1.1.18.1 yamt printf("sii_StartCmd: %s target %d cmd 0x%x addr %p size %d DMA %d\n",
398 1.1.18.1 yamt sc->sc_dev.dv_xname,
399 1.1.18.1 yamt target, scsicmd->cmd[0], scsicmd->buf, scsicmd->buflen,
400 1.1.18.1 yamt state->dmaDataPhase);
401 1.1.18.1 yamt }
402 1.1.18.1 yamt sii_debug_cmd = scsicmd->cmd[0];
403 1.1.18.1 yamt if (scsicmd->cmd[0] == READ_10 ||
404 1.1.18.1 yamt scsicmd->cmd[0] == WRITE_10) {
405 1.1.18.1 yamt sii_debug_bn = (scsicmd->cmd[2] << 24) |
406 1.1.18.1 yamt (scsicmd->cmd[3] << 16) |
407 1.1.18.1 yamt (scsicmd->cmd[4] << 8) |
408 1.1.18.1 yamt scsicmd->cmd[5];
409 1.1.18.1 yamt sii_debug_sz = (scsicmd->cmd[7] << 8) | scsicmd->cmd[8];
410 1.1.18.1 yamt } else {
411 1.1.18.1 yamt sii_debug_bn = 0;
412 1.1.18.1 yamt sii_debug_sz = 0;
413 1.1.18.1 yamt }
414 1.1.18.1 yamt #endif
415 1.1.18.1 yamt
416 1.1.18.1 yamt /* try to select the target */
417 1.1.18.1 yamt regs = sc->sc_regs;
418 1.1.18.1 yamt
419 1.1.18.1 yamt /*
420 1.1.18.1 yamt * Another device may have selected us; in which case,
421 1.1.18.1 yamt * this command will be restarted later.
422 1.1.18.1 yamt */
423 1.1.18.1 yamt if ((status = regs->dstat) & (SII_CI | SII_DI)) {
424 1.1.18.1 yamt sii_DoIntr(sc, status);
425 1.1.18.1 yamt return;
426 1.1.18.1 yamt }
427 1.1.18.1 yamt
428 1.1.18.1 yamt sc->sc_target = target;
429 1.1.18.1 yamt #if 0
430 1.1.18.1 yamt /* seem to have problems with synchronous transfers */
431 1.1.18.1 yamt if (scsicmd->flags & SCSICMD_USE_SYNC) {
432 1.1.18.1 yamt printf("sii_StartCmd: doing extended msg\n"); /* XXX */
433 1.1.18.1 yamt /*
434 1.1.18.1 yamt * Setup to send both the identify message and the synchronous
435 1.1.18.1 yamt * data transfer request.
436 1.1.18.1 yamt */
437 1.1.18.1 yamt sii_buf[0] = MSG_IDENTIFYFLAG | MSG_IDENTIFY_DISCFLAG;
438 1.1.18.1 yamt sii_buf[1] = MSG_EXTENDED;
439 1.1.18.1 yamt sii_buf[2] = MSG_EXT_SDTR_LEN;
440 1.1.18.1 yamt sii_buf[3] = MSG_EXT_SDTR;
441 1.1.18.1 yamt sii_buf[4] = 0;
442 1.1.18.1 yamt sii_buf[5] = 3; /* maximum SII chip supports */
443 1.1.18.1 yamt
444 1.1.18.1 yamt state->dmaCurPhase = SII_MSG_OUT_PHASE,
445 1.1.18.1 yamt state->dmalen = 6;
446 1.1.18.1 yamt sc->sii_copytobuf((u_short *)sii_buf,
447 1.1.18.1 yamt (volatile u_short *)SII_BUF_ADDR(sc), 6);
448 1.1.18.1 yamt regs->slcsr = target;
449 1.1.18.1 yamt regs->dmctrl = state->dmaReqAck;
450 1.1.18.1 yamt regs->dmaddrl = (u_short)(SII_BUF_ADDR(sc) >> 1);
451 1.1.18.1 yamt regs->dmaddrh = (u_short)(SII_BUF_ADDR(sc) >> 17) & 03;
452 1.1.18.1 yamt regs->dmlotc = 6;
453 1.1.18.1 yamt regs->comm = SII_DMA | SII_INXFER | SII_SELECT | SII_ATN |
454 1.1.18.1 yamt SII_CON | SII_MSG_OUT_PHASE;
455 1.1.18.1 yamt } else
456 1.1.18.1 yamt #endif
457 1.1.18.1 yamt {
458 1.1.18.1 yamt /* do a chained, select with ATN and programmed I/O command */
459 1.1.18.1 yamt regs->data = MSG_IDENTIFYFLAG | MSG_IDENTIFY_DISCFLAG |
460 1.1.18.1 yamt scsicmd->lun;
461 1.1.18.1 yamt regs->slcsr = target;
462 1.1.18.1 yamt regs->dmctrl = state->dmaReqAck;
463 1.1.18.1 yamt regs->comm = SII_INXFER | SII_SELECT | SII_ATN | SII_CON |
464 1.1.18.1 yamt SII_MSG_OUT_PHASE;
465 1.1.18.1 yamt }
466 1.1.18.1 yamt wbflush();
467 1.1.18.1 yamt
468 1.1.18.1 yamt /*
469 1.1.18.1 yamt * Wait for something to happen
470 1.1.18.1 yamt * (should happen soon or we would use interrupts).
471 1.1.18.1 yamt */
472 1.1.18.1 yamt SII_WAIT_UNTIL(status, regs->cstat, status & (SII_CI | SII_DI),
473 1.1.18.1 yamt SII_WAIT_COUNT/4, retval);
474 1.1.18.1 yamt
475 1.1.18.1 yamt /* check to see if we are connected OK */
476 1.1.18.1 yamt if ((status & (SII_RST | SII_SCH | SII_STATE_MSK)) ==
477 1.1.18.1 yamt (SII_SCH | SII_CON)) {
478 1.1.18.1 yamt regs->cstat = status;
479 1.1.18.1 yamt wbflush();
480 1.1.18.1 yamt
481 1.1.18.1 yamt #ifdef DEBUG
482 1.1.18.1 yamt sii_logp->target = target;
483 1.1.18.1 yamt sii_logp->cstat = status;
484 1.1.18.1 yamt sii_logp->dstat = 0;
485 1.1.18.1 yamt sii_logp->comm = regs->comm;
486 1.1.18.1 yamt sii_logp->msg = -1;
487 1.1.18.1 yamt sii_logp->rlen = state->buflen;
488 1.1.18.1 yamt sii_logp->dlen = state->dmalen;
489 1.1.18.1 yamt if (++sii_logp >= &sii_log[NLOG])
490 1.1.18.1 yamt sii_logp = sii_log;
491 1.1.18.1 yamt #endif
492 1.1.18.1 yamt
493 1.1.18.1 yamt /* wait a short time for command phase */
494 1.1.18.1 yamt SII_WAIT_UNTIL(status, regs->dstat, status & SII_MIS,
495 1.1.18.1 yamt SII_WAIT_COUNT, retval);
496 1.1.18.1 yamt #ifdef DEBUG
497 1.1.18.1 yamt if (sii_debug > 2)
498 1.1.18.1 yamt printf("sii_StartCmd: ds %x cnt %d\n", status, retval);
499 1.1.18.1 yamt #endif
500 1.1.18.1 yamt if ((status & (SII_CI | SII_MIS | SII_PHASE_MSK)) !=
501 1.1.18.1 yamt (SII_MIS | SII_CMD_PHASE)) {
502 1.1.18.1 yamt printf("sii_StartCmd: timeout cs %x ds %x cnt %d\n",
503 1.1.18.1 yamt regs->cstat, status, retval); /* XXX */
504 1.1.18.1 yamt /* process interrupt or continue until it happens */
505 1.1.18.1 yamt if (status & (SII_CI | SII_DI))
506 1.1.18.1 yamt sii_DoIntr(sc, status);
507 1.1.18.1 yamt return;
508 1.1.18.1 yamt }
509 1.1.18.1 yamt regs->dstat = SII_DNE; /* clear Msg Out DMA done */
510 1.1.18.1 yamt
511 1.1.18.1 yamt /* send command data */
512 1.1.18.1 yamt sc->sii_copytobuf((u_short *)state->cmd,
513 1.1.18.1 yamt (volatile u_short *)state->dmaAddr[0], state->cmdlen);
514 1.1.18.1 yamt sii_StartDMA(regs, state->dmaCurPhase = SII_CMD_PHASE,
515 1.1.18.1 yamt state->dmaAddr[0], state->dmalen = scsicmd->cmdlen);
516 1.1.18.1 yamt
517 1.1.18.1 yamt /* wait a little while for DMA to finish */
518 1.1.18.1 yamt SII_WAIT_UNTIL(status, regs->dstat, status & (SII_CI | SII_DI),
519 1.1.18.1 yamt SII_WAIT_COUNT, retval);
520 1.1.18.1 yamt #ifdef DEBUG
521 1.1.18.1 yamt if (sii_debug > 2)
522 1.1.18.1 yamt printf("sii_StartCmd: ds %x, cnt %d\n", status, retval);
523 1.1.18.1 yamt #endif
524 1.1.18.1 yamt if (status & (SII_CI | SII_DI))
525 1.1.18.1 yamt sii_DoIntr(sc, status);
526 1.1.18.1 yamt #ifdef DEBUG
527 1.1.18.1 yamt if (sii_debug > 2)
528 1.1.18.1 yamt printf("sii_StartCmd: DONE ds %x\n", regs->dstat);
529 1.1.18.1 yamt #endif
530 1.1.18.1 yamt return;
531 1.1.18.1 yamt }
532 1.1.18.1 yamt
533 1.1.18.1 yamt /*
534 1.1.18.1 yamt * Another device may have selected us; in which case,
535 1.1.18.1 yamt * this command will be restarted later.
536 1.1.18.1 yamt */
537 1.1.18.1 yamt if (status & (SII_CI | SII_DI)) {
538 1.1.18.1 yamt sii_DoIntr(sc, regs->dstat);
539 1.1.18.1 yamt return;
540 1.1.18.1 yamt }
541 1.1.18.1 yamt
542 1.1.18.1 yamt /*
543 1.1.18.1 yamt * Disconnect if selection command still in progress.
544 1.1.18.1 yamt */
545 1.1.18.1 yamt if (status & SII_SIP) {
546 1.1.18.1 yamt error = ENXIO; /* device didn't respond */
547 1.1.18.1 yamt regs->comm = SII_DISCON;
548 1.1.18.1 yamt wbflush();
549 1.1.18.1 yamt SII_WAIT_UNTIL(status, regs->cstat,
550 1.1.18.1 yamt !(status & (SII_CON | SII_SIP)),
551 1.1.18.1 yamt SII_WAIT_COUNT, retval);
552 1.1.18.1 yamt } else
553 1.1.18.1 yamt error = EBUSY; /* couldn't get the bus */
554 1.1.18.1 yamt #ifdef DEBUG
555 1.1.18.1 yamt if (sii_debug > 1)
556 1.1.18.1 yamt printf("sii_StartCmd: Couldn't select target %d error %d\n",
557 1.1.18.1 yamt target, error);
558 1.1.18.1 yamt #endif
559 1.1.18.1 yamt sc->sc_target = -1;
560 1.1.18.1 yamt regs->cstat = 0xffff;
561 1.1.18.1 yamt regs->dstat = 0xffff;
562 1.1.18.1 yamt regs->comm = 0;
563 1.1.18.1 yamt wbflush();
564 1.1.18.1 yamt sii_CmdDone(sc, target, error);
565 1.1.18.1 yamt }
566 1.1.18.1 yamt
567 1.1.18.1 yamt /*
568 1.1.18.1 yamt * Process interrupt conditions.
569 1.1.18.1 yamt */
570 1.1.18.1 yamt static void
571 1.1.18.1 yamt sii_DoIntr(sc, dstat)
572 1.1.18.1 yamt struct siisoftc *sc;
573 1.1.18.1 yamt u_int dstat;
574 1.1.18.1 yamt {
575 1.1.18.1 yamt SIIRegs *regs = sc->sc_regs;
576 1.1.18.1 yamt State *state;
577 1.1.18.1 yamt u_int cstat;
578 1.1.18.1 yamt int i, msg;
579 1.1.18.1 yamt u_int comm;
580 1.1.18.1 yamt
581 1.1.18.1 yamt again:
582 1.1.18.1 yamt comm = regs->comm;
583 1.1.18.1 yamt
584 1.1.18.1 yamt #ifdef DEBUG
585 1.1.18.1 yamt if (sii_debug > 3)
586 1.1.18.1 yamt printf("sii_DoIntr: cs %x, ds %x cm %x ",
587 1.1.18.1 yamt regs->cstat, dstat, comm);
588 1.1.18.1 yamt sii_logp->target = sc->sc_target;
589 1.1.18.1 yamt sii_logp->cstat = regs->cstat;
590 1.1.18.1 yamt sii_logp->dstat = dstat;
591 1.1.18.1 yamt sii_logp->comm = comm;
592 1.1.18.1 yamt sii_logp->msg = -1;
593 1.1.18.1 yamt if (sc->sc_target >= 0) {
594 1.1.18.1 yamt sii_logp->rlen = sc->sc_st[sc->sc_target].buflen;
595 1.1.18.1 yamt sii_logp->dlen = sc->sc_st[sc->sc_target].dmalen;
596 1.1.18.1 yamt } else {
597 1.1.18.1 yamt sii_logp->rlen = 0;
598 1.1.18.1 yamt sii_logp->dlen = 0;
599 1.1.18.1 yamt }
600 1.1.18.1 yamt if (++sii_logp >= &sii_log[NLOG])
601 1.1.18.1 yamt sii_logp = sii_log;
602 1.1.18.1 yamt #endif
603 1.1.18.1 yamt
604 1.1.18.1 yamt regs->dstat = dstat; /* acknowledge everything */
605 1.1.18.1 yamt wbflush();
606 1.1.18.1 yamt
607 1.1.18.1 yamt if (dstat & SII_CI) {
608 1.1.18.1 yamt /* deglitch cstat register */
609 1.1.18.1 yamt msg = regs->cstat;
610 1.1.18.1 yamt while (msg != (cstat = regs->cstat))
611 1.1.18.1 yamt msg = cstat;
612 1.1.18.1 yamt regs->cstat = cstat; /* acknowledge everything */
613 1.1.18.1 yamt wbflush();
614 1.1.18.1 yamt #ifdef DEBUG
615 1.1.18.1 yamt if (sii_logp > sii_log)
616 1.1.18.1 yamt sii_logp[-1].cstat = cstat;
617 1.1.18.1 yamt else
618 1.1.18.1 yamt sii_log[NLOG - 1].cstat = cstat;
619 1.1.18.1 yamt #endif
620 1.1.18.1 yamt
621 1.1.18.1 yamt /* check for a BUS RESET */
622 1.1.18.1 yamt if (cstat & SII_RST) {
623 1.1.18.1 yamt printf("%s: SCSI bus reset!!\n", sc->sc_dev.dv_xname);
624 1.1.18.1 yamt /* need to flush disconnected commands */
625 1.1.18.1 yamt for (i = 0; i < SII_NCMD; i++) {
626 1.1.18.1 yamt if (!sc->sc_cmd[i])
627 1.1.18.1 yamt continue;
628 1.1.18.1 yamt sii_CmdDone(sc, i, EIO);
629 1.1.18.1 yamt }
630 1.1.18.1 yamt /* rearbitrate synchronous offset */
631 1.1.18.1 yamt for (i = 0; i < SII_NCMD; i++)
632 1.1.18.1 yamt sc->sc_st[i].dmaReqAck = 0;
633 1.1.18.1 yamt sc->sc_target = -1;
634 1.1.18.1 yamt return;
635 1.1.18.1 yamt }
636 1.1.18.1 yamt
637 1.1.18.1 yamt #ifdef notdef
638 1.1.18.1 yamt /*
639 1.1.18.1 yamt * Check for a BUS ERROR.
640 1.1.18.1 yamt * According to DEC, this feature doesn't really work
641 1.1.18.1 yamt * and to just clear the bit if it's set.
642 1.1.18.1 yamt */
643 1.1.18.1 yamt if (cstat & SII_BER) {
644 1.1.18.1 yamt regs->cstat = SII_BER;
645 1.1.18.1 yamt wbflush();
646 1.1.18.1 yamt }
647 1.1.18.1 yamt #endif
648 1.1.18.1 yamt
649 1.1.18.1 yamt /* check for state change */
650 1.1.18.1 yamt if (cstat & SII_SCH) {
651 1.1.18.1 yamt sii_StateChg(sc, cstat);
652 1.1.18.1 yamt comm = regs->comm;
653 1.1.18.1 yamt }
654 1.1.18.1 yamt }
655 1.1.18.1 yamt
656 1.1.18.1 yamt /* check for DMA completion */
657 1.1.18.1 yamt if (dstat & SII_DNE) {
658 1.1.18.1 yamt u_short *dma;
659 1.1.18.1 yamt char *buf;
660 1.1.18.1 yamt
661 1.1.18.1 yamt /*
662 1.1.18.1 yamt * There is a race condition with SII_SCH. There is a short
663 1.1.18.1 yamt * window between the time a SII_SCH is seen after a disconnect
664 1.1.18.1 yamt * and when the SII_SCH is cleared. A reselect can happen
665 1.1.18.1 yamt * in this window and we will clear the SII_SCH without
666 1.1.18.1 yamt * processing the reconnect.
667 1.1.18.1 yamt */
668 1.1.18.1 yamt if (sc->sc_target < 0) {
669 1.1.18.1 yamt cstat = regs->cstat;
670 1.1.18.1 yamt printf("%s: target %d DNE?? dev %d,%d cs %x\n",
671 1.1.18.1 yamt sc->sc_dev.dv_xname, sc->sc_target,
672 1.1.18.1 yamt regs->slcsr, regs->destat,
673 1.1.18.1 yamt cstat); /* XXX */
674 1.1.18.1 yamt if (cstat & SII_DST) {
675 1.1.18.1 yamt sc->sc_target = regs->destat;
676 1.1.18.1 yamt state = &sc->sc_st[sc->sc_target];
677 1.1.18.1 yamt state->prevComm = 0;
678 1.1.18.1 yamt } else
679 1.1.18.1 yamt panic("sc_target 1");
680 1.1.18.1 yamt }
681 1.1.18.1 yamt state = &sc->sc_st[sc->sc_target];
682 1.1.18.1 yamt /* check for a PARITY ERROR */
683 1.1.18.1 yamt if (dstat & SII_IPE) {
684 1.1.18.1 yamt state->flags |= PARITY_ERR;
685 1.1.18.1 yamt printf("%s: Parity error!!\n", sc->sc_dev.dv_xname);
686 1.1.18.1 yamt goto abort;
687 1.1.18.1 yamt }
688 1.1.18.1 yamt /* dmalen = amount left to transfer, i = amount transfered */
689 1.1.18.1 yamt i = state->dmalen;
690 1.1.18.1 yamt state->dmalen = 0;
691 1.1.18.1 yamt state->dmaCurPhase = -1;
692 1.1.18.1 yamt #ifdef DEBUG
693 1.1.18.1 yamt if (sii_debug > 4) {
694 1.1.18.1 yamt printf("DNE: amt %d ", i);
695 1.1.18.1 yamt if (!(dstat & SII_TCZ))
696 1.1.18.1 yamt printf("no TCZ?? (%d) ", regs->dmlotc);
697 1.1.18.1 yamt } else if (!(dstat & SII_TCZ)) {
698 1.1.18.1 yamt printf("%s: device %d: no TCZ?? (%d)\n",
699 1.1.18.1 yamt sc->sc_dev.dv_xname, sc->sc_target, regs->dmlotc);
700 1.1.18.1 yamt sii_DumpLog(); /* XXX */
701 1.1.18.1 yamt }
702 1.1.18.1 yamt #endif
703 1.1.18.1 yamt switch (comm & SII_PHASE_MSK) {
704 1.1.18.1 yamt case SII_CMD_PHASE:
705 1.1.18.1 yamt state->cmdlen -= i;
706 1.1.18.1 yamt break;
707 1.1.18.1 yamt
708 1.1.18.1 yamt case SII_DATA_IN_PHASE:
709 1.1.18.1 yamt /* check for more data for the same phase */
710 1.1.18.1 yamt dma = state->dmaAddr[state->dmaBufIndex];
711 1.1.18.1 yamt buf = state->buf;
712 1.1.18.1 yamt state->buf += i;
713 1.1.18.1 yamt state->buflen -= i;
714 1.1.18.1 yamt if (state->buflen > 0 && !(dstat & SII_MIS)) {
715 1.1.18.1 yamt int len;
716 1.1.18.1 yamt
717 1.1.18.1 yamt /* start reading next chunk */
718 1.1.18.1 yamt len = state->buflen;
719 1.1.18.1 yamt if (len > SII_MAX_DMA_XFER_LENGTH)
720 1.1.18.1 yamt len = SII_MAX_DMA_XFER_LENGTH;
721 1.1.18.1 yamt state->dmaBufIndex = !state->dmaBufIndex;
722 1.1.18.1 yamt sii_StartDMA(regs,
723 1.1.18.1 yamt state->dmaCurPhase = SII_DATA_IN_PHASE,
724 1.1.18.1 yamt state->dmaAddr[state->dmaBufIndex],
725 1.1.18.1 yamt state->dmaCnt = state->dmalen = len);
726 1.1.18.1 yamt dstat &= ~(SII_IBF | SII_TBE);
727 1.1.18.1 yamt }
728 1.1.18.1 yamt /* copy in the data */
729 1.1.18.1 yamt sc->sii_copyfrombuf((volatile u_short *)dma, buf, i);
730 1.1.18.1 yamt break;
731 1.1.18.1 yamt
732 1.1.18.1 yamt case SII_DATA_OUT_PHASE:
733 1.1.18.1 yamt state->dmaBufIndex = !state->dmaBufIndex;
734 1.1.18.1 yamt state->buf += i;
735 1.1.18.1 yamt state->buflen -= i;
736 1.1.18.1 yamt
737 1.1.18.1 yamt /* check for more data for the same phase */
738 1.1.18.1 yamt if (state->buflen <= 0 || (dstat & SII_MIS))
739 1.1.18.1 yamt break;
740 1.1.18.1 yamt
741 1.1.18.1 yamt /* start next chunk */
742 1.1.18.1 yamt i = state->buflen;
743 1.1.18.1 yamt if (i > SII_MAX_DMA_XFER_LENGTH) {
744 1.1.18.1 yamt sii_StartDMA(regs, state->dmaCurPhase =
745 1.1.18.1 yamt SII_DATA_OUT_PHASE,
746 1.1.18.1 yamt state->dmaAddr[state->dmaBufIndex],
747 1.1.18.1 yamt state->dmaCnt = state->dmalen =
748 1.1.18.1 yamt SII_MAX_DMA_XFER_LENGTH);
749 1.1.18.1 yamt /* prepare for next chunk */
750 1.1.18.1 yamt i -= SII_MAX_DMA_XFER_LENGTH;
751 1.1.18.1 yamt if (i > SII_MAX_DMA_XFER_LENGTH)
752 1.1.18.1 yamt i = SII_MAX_DMA_XFER_LENGTH;
753 1.1.18.1 yamt sc->sii_copytobuf((u_short *)(state->buf +
754 1.1.18.1 yamt SII_MAX_DMA_XFER_LENGTH),
755 1.1.18.1 yamt (volatile u_short *)
756 1.1.18.1 yamt state->dmaAddr[!state->dmaBufIndex], i);
757 1.1.18.1 yamt } else {
758 1.1.18.1 yamt sii_StartDMA(regs, state->dmaCurPhase =
759 1.1.18.1 yamt SII_DATA_OUT_PHASE,
760 1.1.18.1 yamt state->dmaAddr[state->dmaBufIndex],
761 1.1.18.1 yamt state->dmaCnt = state->dmalen = i);
762 1.1.18.1 yamt }
763 1.1.18.1 yamt dstat &= ~(SII_IBF | SII_TBE);
764 1.1.18.1 yamt }
765 1.1.18.1 yamt }
766 1.1.18.1 yamt
767 1.1.18.1 yamt /* check for phase change or another MsgIn/Out */
768 1.1.18.1 yamt if (dstat & (SII_MIS | SII_IBF | SII_TBE)) {
769 1.1.18.1 yamt /*
770 1.1.18.1 yamt * There is a race condition with SII_SCH. There is a short
771 1.1.18.1 yamt * window between the time a SII_SCH is seen after a disconnect
772 1.1.18.1 yamt * and when the SII_SCH is cleared. A reselect can happen
773 1.1.18.1 yamt * in this window and we will clear the SII_SCH without
774 1.1.18.1 yamt * processing the reconnect.
775 1.1.18.1 yamt */
776 1.1.18.1 yamt if (sc->sc_target < 0) {
777 1.1.18.1 yamt cstat = regs->cstat;
778 1.1.18.1 yamt printf("%s: target %d MIS?? dev %d,%d cs %x ds %x\n",
779 1.1.18.1 yamt sc->sc_dev.dv_xname, sc->sc_target,
780 1.1.18.1 yamt regs->slcsr, regs->destat,
781 1.1.18.1 yamt cstat, dstat); /* XXX */
782 1.1.18.1 yamt if (cstat & SII_DST) {
783 1.1.18.1 yamt sc->sc_target = regs->destat;
784 1.1.18.1 yamt state = &sc->sc_st[sc->sc_target];
785 1.1.18.1 yamt state->prevComm = 0;
786 1.1.18.1 yamt } else {
787 1.1.18.1 yamt #ifdef DEBUG
788 1.1.18.1 yamt sii_DumpLog();
789 1.1.18.1 yamt #endif
790 1.1.18.1 yamt panic("sc_target 2");
791 1.1.18.1 yamt }
792 1.1.18.1 yamt }
793 1.1.18.1 yamt state = &sc->sc_st[sc->sc_target];
794 1.1.18.1 yamt switch (dstat & SII_PHASE_MSK) {
795 1.1.18.1 yamt case SII_CMD_PHASE:
796 1.1.18.1 yamt if (state->dmaPrevPhase >= 0) {
797 1.1.18.1 yamt /* restart DMA after disconnect/reconnect */
798 1.1.18.1 yamt if (state->dmaPrevPhase != SII_CMD_PHASE) {
799 1.1.18.1 yamt printf("%s: device %d: DMA reselect phase doesn't match\n",
800 1.1.18.1 yamt sc->sc_dev.dv_xname, sc->sc_target);
801 1.1.18.1 yamt goto abort;
802 1.1.18.1 yamt }
803 1.1.18.1 yamt state->dmaCurPhase = SII_CMD_PHASE;
804 1.1.18.1 yamt state->dmaPrevPhase = -1;
805 1.1.18.1 yamt regs->dmaddrl = state->dmaAddrL;
806 1.1.18.1 yamt regs->dmaddrh = state->dmaAddrH;
807 1.1.18.1 yamt regs->dmlotc = state->dmaCnt;
808 1.1.18.1 yamt if (state->dmaCnt & 1)
809 1.1.18.1 yamt regs->dmabyte = state->dmaByte;
810 1.1.18.1 yamt regs->comm = SII_DMA | SII_INXFER |
811 1.1.18.1 yamt (comm & SII_STATE_MSK) | SII_CMD_PHASE;
812 1.1.18.1 yamt wbflush();
813 1.1.18.1 yamt #ifdef DEBUG
814 1.1.18.1 yamt if (sii_debug > 4)
815 1.1.18.1 yamt printf("Cmd dcnt %d dadr %x ",
816 1.1.18.1 yamt state->dmaCnt,
817 1.1.18.1 yamt (state->dmaAddrH << 16) |
818 1.1.18.1 yamt state->dmaAddrL);
819 1.1.18.1 yamt #endif
820 1.1.18.1 yamt } else {
821 1.1.18.1 yamt /* send command data */
822 1.1.18.1 yamt i = state->cmdlen;
823 1.1.18.1 yamt if (i == 0) {
824 1.1.18.1 yamt printf("%s: device %d: cmd count exceeded\n",
825 1.1.18.1 yamt sc->sc_dev.dv_xname, sc->sc_target);
826 1.1.18.1 yamt goto abort;
827 1.1.18.1 yamt }
828 1.1.18.1 yamt sc->sii_copytobuf((u_short *)state->cmd,
829 1.1.18.1 yamt (volatile u_short *)state->dmaAddr[0],
830 1.1.18.1 yamt i);
831 1.1.18.1 yamt sii_StartDMA(regs, state->dmaCurPhase =
832 1.1.18.1 yamt SII_CMD_PHASE, state->dmaAddr[0],
833 1.1.18.1 yamt state->dmaCnt = state->dmalen = i);
834 1.1.18.1 yamt }
835 1.1.18.1 yamt /* wait a short time for XFER complete */
836 1.1.18.1 yamt SII_WAIT_UNTIL(dstat, regs->dstat,
837 1.1.18.1 yamt dstat & (SII_CI | SII_DI), SII_WAIT_COUNT, i);
838 1.1.18.1 yamt if (dstat & (SII_CI | SII_DI)) {
839 1.1.18.1 yamt #ifdef DEBUG
840 1.1.18.1 yamt if (sii_debug > 4)
841 1.1.18.1 yamt printf("cnt %d\n", i);
842 1.1.18.1 yamt else if (sii_debug > 0)
843 1.1.18.1 yamt printf("sii_DoIntr: cmd wait ds %x cnt %d\n",
844 1.1.18.1 yamt dstat, i);
845 1.1.18.1 yamt #endif
846 1.1.18.1 yamt goto again;
847 1.1.18.1 yamt }
848 1.1.18.1 yamt break;
849 1.1.18.1 yamt
850 1.1.18.1 yamt case SII_DATA_IN_PHASE:
851 1.1.18.1 yamt case SII_DATA_OUT_PHASE:
852 1.1.18.1 yamt if (state->cmdlen > 0) {
853 1.1.18.1 yamt printf("%s: device %d: cmd %x: command data not all sent (%d) 1\n",
854 1.1.18.1 yamt sc->sc_dev.dv_xname, sc->sc_target,
855 1.1.18.1 yamt sc->sc_cmd[sc->sc_target]->cmd[0],
856 1.1.18.1 yamt state->cmdlen);
857 1.1.18.1 yamt state->cmdlen = 0;
858 1.1.18.1 yamt #ifdef DEBUG
859 1.1.18.1 yamt sii_DumpLog();
860 1.1.18.1 yamt #endif
861 1.1.18.1 yamt }
862 1.1.18.1 yamt if (state->dmaPrevPhase >= 0) {
863 1.1.18.1 yamt /* restart DMA after disconnect/reconnect */
864 1.1.18.1 yamt if (state->dmaPrevPhase !=
865 1.1.18.1 yamt (dstat & SII_PHASE_MSK)) {
866 1.1.18.1 yamt printf("%s: device %d: DMA reselect phase doesn't match\n",
867 1.1.18.1 yamt sc->sc_dev.dv_xname, sc->sc_target);
868 1.1.18.1 yamt goto abort;
869 1.1.18.1 yamt }
870 1.1.18.1 yamt state->dmaCurPhase = state->dmaPrevPhase;
871 1.1.18.1 yamt state->dmaPrevPhase = -1;
872 1.1.18.1 yamt regs->dmaddrl = state->dmaAddrL;
873 1.1.18.1 yamt regs->dmaddrh = state->dmaAddrH;
874 1.1.18.1 yamt regs->dmlotc = state->dmaCnt;
875 1.1.18.1 yamt if (state->dmaCnt & 1)
876 1.1.18.1 yamt regs->dmabyte = state->dmaByte;
877 1.1.18.1 yamt regs->comm = SII_DMA | SII_INXFER |
878 1.1.18.1 yamt (comm & SII_STATE_MSK) |
879 1.1.18.1 yamt state->dmaCurPhase;
880 1.1.18.1 yamt wbflush();
881 1.1.18.1 yamt #ifdef DEBUG
882 1.1.18.1 yamt if (sii_debug > 4)
883 1.1.18.1 yamt printf("Data %d dcnt %d dadr %x ",
884 1.1.18.1 yamt state->dmaDataPhase,
885 1.1.18.1 yamt state->dmaCnt,
886 1.1.18.1 yamt (state->dmaAddrH << 16) |
887 1.1.18.1 yamt state->dmaAddrL);
888 1.1.18.1 yamt #endif
889 1.1.18.1 yamt break;
890 1.1.18.1 yamt }
891 1.1.18.1 yamt #ifdef DEBUG
892 1.1.18.1 yamt if (sii_debug > 4) {
893 1.1.18.1 yamt printf("Data %d ", state->dmaDataPhase);
894 1.1.18.1 yamt if (sii_debug > 5)
895 1.1.18.1 yamt printf("\n");
896 1.1.18.1 yamt }
897 1.1.18.1 yamt #endif
898 1.1.18.1 yamt i = state->buflen;
899 1.1.18.1 yamt if (i == 0) {
900 1.1.18.1 yamt printf("%s: device %d: data count exceeded\n",
901 1.1.18.1 yamt sc->sc_dev.dv_xname, sc->sc_target);
902 1.1.18.1 yamt goto abort;
903 1.1.18.1 yamt }
904 1.1.18.1 yamt if (i > SII_MAX_DMA_XFER_LENGTH)
905 1.1.18.1 yamt i = SII_MAX_DMA_XFER_LENGTH;
906 1.1.18.1 yamt if ((dstat & SII_PHASE_MSK) == SII_DATA_IN_PHASE) {
907 1.1.18.1 yamt sii_StartDMA(regs,
908 1.1.18.1 yamt state->dmaCurPhase = SII_DATA_IN_PHASE,
909 1.1.18.1 yamt state->dmaAddr[state->dmaBufIndex],
910 1.1.18.1 yamt state->dmaCnt = state->dmalen = i);
911 1.1.18.1 yamt break;
912 1.1.18.1 yamt }
913 1.1.18.1 yamt /* start first chunk */
914 1.1.18.1 yamt if (state->flags & FIRST_DMA) {
915 1.1.18.1 yamt state->flags &= ~FIRST_DMA;
916 1.1.18.1 yamt sc->sii_copytobuf((u_short *)state->buf,
917 1.1.18.1 yamt (volatile u_short *)
918 1.1.18.1 yamt state->dmaAddr[state->dmaBufIndex], i);
919 1.1.18.1 yamt }
920 1.1.18.1 yamt sii_StartDMA(regs,
921 1.1.18.1 yamt state->dmaCurPhase = SII_DATA_OUT_PHASE,
922 1.1.18.1 yamt state->dmaAddr[state->dmaBufIndex],
923 1.1.18.1 yamt state->dmaCnt = state->dmalen = i);
924 1.1.18.1 yamt i = state->buflen - SII_MAX_DMA_XFER_LENGTH;
925 1.1.18.1 yamt if (i > 0) {
926 1.1.18.1 yamt /* prepare for next chunk */
927 1.1.18.1 yamt if (i > SII_MAX_DMA_XFER_LENGTH)
928 1.1.18.1 yamt i = SII_MAX_DMA_XFER_LENGTH;
929 1.1.18.1 yamt sc->sii_copytobuf((u_short *)(state->buf +
930 1.1.18.1 yamt SII_MAX_DMA_XFER_LENGTH),
931 1.1.18.1 yamt (volatile u_short *)
932 1.1.18.1 yamt state->dmaAddr[!state->dmaBufIndex], i);
933 1.1.18.1 yamt }
934 1.1.18.1 yamt break;
935 1.1.18.1 yamt
936 1.1.18.1 yamt case SII_STATUS_PHASE:
937 1.1.18.1 yamt if (state->cmdlen > 0) {
938 1.1.18.1 yamt printf("%s: device %d: cmd %x: command data not all sent (%d) 2\n",
939 1.1.18.1 yamt sc->sc_dev.dv_xname, sc->sc_target,
940 1.1.18.1 yamt sc->sc_cmd[sc->sc_target]->cmd[0],
941 1.1.18.1 yamt state->cmdlen);
942 1.1.18.1 yamt state->cmdlen = 0;
943 1.1.18.1 yamt #ifdef DEBUG
944 1.1.18.1 yamt sii_DumpLog();
945 1.1.18.1 yamt #endif
946 1.1.18.1 yamt }
947 1.1.18.1 yamt
948 1.1.18.1 yamt /* read amount transfered if DMA didn't finish */
949 1.1.18.1 yamt if (state->dmalen > 0) {
950 1.1.18.1 yamt i = state->dmalen - regs->dmlotc;
951 1.1.18.1 yamt state->dmalen = 0;
952 1.1.18.1 yamt state->dmaCurPhase = -1;
953 1.1.18.1 yamt regs->dmlotc = 0;
954 1.1.18.1 yamt regs->comm = comm &
955 1.1.18.1 yamt (SII_STATE_MSK | SII_PHASE_MSK);
956 1.1.18.1 yamt wbflush();
957 1.1.18.1 yamt regs->dstat = SII_DNE;
958 1.1.18.1 yamt wbflush();
959 1.1.18.1 yamt #ifdef DEBUG
960 1.1.18.1 yamt if (sii_debug > 4)
961 1.1.18.1 yamt printf("DMA amt %d ", i);
962 1.1.18.1 yamt #endif
963 1.1.18.1 yamt switch (comm & SII_PHASE_MSK) {
964 1.1.18.1 yamt case SII_DATA_IN_PHASE:
965 1.1.18.1 yamt /* copy in the data */
966 1.1.18.1 yamt sc->sii_copyfrombuf((volatile u_short*)
967 1.1.18.1 yamt state->dmaAddr[state->dmaBufIndex],
968 1.1.18.1 yamt state->buf, i);
969 1.1.18.1 yamt
970 1.1.18.1 yamt case SII_CMD_PHASE:
971 1.1.18.1 yamt case SII_DATA_OUT_PHASE:
972 1.1.18.1 yamt state->buflen -= i;
973 1.1.18.1 yamt }
974 1.1.18.1 yamt }
975 1.1.18.1 yamt
976 1.1.18.1 yamt /* read a one byte status message */
977 1.1.18.1 yamt state->statusByte = msg =
978 1.1.18.1 yamt sii_GetByte(regs, SII_STATUS_PHASE, 1);
979 1.1.18.1 yamt if (msg < 0) {
980 1.1.18.1 yamt dstat = regs->dstat;
981 1.1.18.1 yamt goto again;
982 1.1.18.1 yamt }
983 1.1.18.1 yamt #ifdef DEBUG
984 1.1.18.1 yamt if (sii_debug > 4)
985 1.1.18.1 yamt printf("Status %x ", msg);
986 1.1.18.1 yamt if (sii_logp > sii_log)
987 1.1.18.1 yamt sii_logp[-1].msg = msg;
988 1.1.18.1 yamt else
989 1.1.18.1 yamt sii_log[NLOG - 1].msg = msg;
990 1.1.18.1 yamt #endif
991 1.1.18.1 yamt
992 1.1.18.1 yamt /* do a quick wait for COMMAND_COMPLETE */
993 1.1.18.1 yamt SII_WAIT_UNTIL(dstat, regs->dstat,
994 1.1.18.1 yamt dstat & (SII_CI | SII_DI), SII_WAIT_COUNT, i);
995 1.1.18.1 yamt if (dstat & (SII_CI | SII_DI)) {
996 1.1.18.1 yamt #ifdef DEBUG
997 1.1.18.1 yamt if (sii_debug > 4)
998 1.1.18.1 yamt printf("cnt2 %d\n", i);
999 1.1.18.1 yamt #endif
1000 1.1.18.1 yamt goto again;
1001 1.1.18.1 yamt }
1002 1.1.18.1 yamt break;
1003 1.1.18.1 yamt
1004 1.1.18.1 yamt case SII_MSG_IN_PHASE:
1005 1.1.18.1 yamt /*
1006 1.1.18.1 yamt * Save DMA state if DMA didn't finish.
1007 1.1.18.1 yamt * Be careful not to save state again after reconnect
1008 1.1.18.1 yamt * and see RESTORE_POINTER message.
1009 1.1.18.1 yamt * Note that the SII DMA address is not incremented
1010 1.1.18.1 yamt * as DMA proceeds.
1011 1.1.18.1 yamt */
1012 1.1.18.1 yamt if (state->dmaCurPhase >= 0) {
1013 1.1.18.1 yamt /* save DMA registers */
1014 1.1.18.1 yamt state->dmaPrevPhase = state->dmaCurPhase;
1015 1.1.18.1 yamt state->dmaCurPhase = -1;
1016 1.1.18.1 yamt if (dstat & SII_OBB)
1017 1.1.18.1 yamt state->dmaByte = regs->dmabyte;
1018 1.1.18.1 yamt i = regs->dmlotc;
1019 1.1.18.1 yamt if (i != 0)
1020 1.1.18.1 yamt i = state->dmaCnt - i;
1021 1.1.18.1 yamt /* note: no carry from dmaddrl to dmaddrh */
1022 1.1.18.1 yamt state->dmaAddrL = regs->dmaddrl + i;
1023 1.1.18.1 yamt state->dmaAddrH = regs->dmaddrh;
1024 1.1.18.1 yamt state->dmaCnt = regs->dmlotc;
1025 1.1.18.1 yamt if (state->dmaCnt == 0)
1026 1.1.18.1 yamt state->dmaCnt = SII_MAX_DMA_XFER_LENGTH;
1027 1.1.18.1 yamt regs->comm = comm &
1028 1.1.18.1 yamt (SII_STATE_MSK | SII_PHASE_MSK);
1029 1.1.18.1 yamt wbflush();
1030 1.1.18.1 yamt regs->dstat = SII_DNE;
1031 1.1.18.1 yamt wbflush();
1032 1.1.18.1 yamt #ifdef DEBUG
1033 1.1.18.1 yamt if (sii_debug > 4) {
1034 1.1.18.1 yamt printf("SavP dcnt %d dadr %x ",
1035 1.1.18.1 yamt state->dmaCnt,
1036 1.1.18.1 yamt (state->dmaAddrH << 16) |
1037 1.1.18.1 yamt state->dmaAddrL);
1038 1.1.18.1 yamt if (((dstat & SII_OBB) != 0) ^
1039 1.1.18.1 yamt (state->dmaCnt & 1))
1040 1.1.18.1 yamt printf("OBB??? ");
1041 1.1.18.1 yamt } else if (sii_debug > 0) {
1042 1.1.18.1 yamt if (((dstat & SII_OBB) != 0) ^
1043 1.1.18.1 yamt (state->dmaCnt & 1)) {
1044 1.1.18.1 yamt printf("sii_DoIntr: OBB??? ds %x cnt %d\n",
1045 1.1.18.1 yamt dstat, state->dmaCnt);
1046 1.1.18.1 yamt sii_DumpLog();
1047 1.1.18.1 yamt }
1048 1.1.18.1 yamt }
1049 1.1.18.1 yamt #endif
1050 1.1.18.1 yamt }
1051 1.1.18.1 yamt
1052 1.1.18.1 yamt /* read a one byte message */
1053 1.1.18.1 yamt msg = sii_GetByte(regs, SII_MSG_IN_PHASE, 0);
1054 1.1.18.1 yamt if (msg < 0) {
1055 1.1.18.1 yamt dstat = regs->dstat;
1056 1.1.18.1 yamt goto again;
1057 1.1.18.1 yamt }
1058 1.1.18.1 yamt #ifdef DEBUG
1059 1.1.18.1 yamt if (sii_debug > 4)
1060 1.1.18.1 yamt printf("MsgIn %x ", msg);
1061 1.1.18.1 yamt if (sii_logp > sii_log)
1062 1.1.18.1 yamt sii_logp[-1].msg = msg;
1063 1.1.18.1 yamt else
1064 1.1.18.1 yamt sii_log[NLOG - 1].msg = msg;
1065 1.1.18.1 yamt #endif
1066 1.1.18.1 yamt
1067 1.1.18.1 yamt /* process message */
1068 1.1.18.1 yamt switch (msg) {
1069 1.1.18.1 yamt case MSG_CMDCOMPLETE:
1070 1.1.18.1 yamt /* acknowledge last byte */
1071 1.1.18.1 yamt regs->comm = SII_INXFER | SII_MSG_IN_PHASE |
1072 1.1.18.1 yamt (comm & SII_STATE_MSK);
1073 1.1.18.1 yamt SII_WAIT_UNTIL(dstat, regs->dstat,
1074 1.1.18.1 yamt dstat & SII_DNE, SII_WAIT_COUNT, i);
1075 1.1.18.1 yamt regs->dstat = SII_DNE;
1076 1.1.18.1 yamt wbflush();
1077 1.1.18.1 yamt msg = sc->sc_target;
1078 1.1.18.1 yamt sc->sc_target = -1;
1079 1.1.18.1 yamt /*
1080 1.1.18.1 yamt * Wait a short time for disconnect.
1081 1.1.18.1 yamt * Don't be fooled if SII_BER happens first.
1082 1.1.18.1 yamt * Note: a reselect may happen here.
1083 1.1.18.1 yamt */
1084 1.1.18.1 yamt SII_WAIT_UNTIL(cstat, regs->cstat,
1085 1.1.18.1 yamt cstat & (SII_RST | SII_SCH),
1086 1.1.18.1 yamt SII_WAIT_COUNT, i);
1087 1.1.18.1 yamt if ((cstat & (SII_RST | SII_SCH |
1088 1.1.18.1 yamt SII_STATE_MSK)) == SII_SCH) {
1089 1.1.18.1 yamt regs->cstat = SII_SCH | SII_BER;
1090 1.1.18.1 yamt regs->comm = 0;
1091 1.1.18.1 yamt wbflush();
1092 1.1.18.1 yamt /*
1093 1.1.18.1 yamt * Double check that we didn't miss a
1094 1.1.18.1 yamt * state change between seeing it and
1095 1.1.18.1 yamt * clearing the SII_SCH bit.
1096 1.1.18.1 yamt */
1097 1.1.18.1 yamt i = regs->cstat;
1098 1.1.18.1 yamt if (!(i & SII_SCH) &&
1099 1.1.18.1 yamt (i & SII_STATE_MSK) !=
1100 1.1.18.1 yamt (cstat & SII_STATE_MSK))
1101 1.1.18.1 yamt sii_StateChg(sc, i);
1102 1.1.18.1 yamt }
1103 1.1.18.1 yamt #ifdef DEBUG
1104 1.1.18.1 yamt if (sii_debug > 4)
1105 1.1.18.1 yamt printf("cs %x\n", cstat);
1106 1.1.18.1 yamt #endif
1107 1.1.18.1 yamt sii_CmdDone(sc, msg, 0);
1108 1.1.18.1 yamt break;
1109 1.1.18.1 yamt
1110 1.1.18.1 yamt case MSG_EXTENDED:
1111 1.1.18.1 yamt /* acknowledge last byte */
1112 1.1.18.1 yamt regs->comm = SII_INXFER | SII_MSG_IN_PHASE |
1113 1.1.18.1 yamt (comm & SII_STATE_MSK);
1114 1.1.18.1 yamt SII_WAIT_UNTIL(dstat, regs->dstat,
1115 1.1.18.1 yamt dstat & SII_DNE, SII_WAIT_COUNT, i);
1116 1.1.18.1 yamt regs->dstat = SII_DNE;
1117 1.1.18.1 yamt wbflush();
1118 1.1.18.1 yamt /* read the message length */
1119 1.1.18.1 yamt msg = sii_GetByte(regs, SII_MSG_IN_PHASE, 1);
1120 1.1.18.1 yamt if (msg < 0) {
1121 1.1.18.1 yamt dstat = regs->dstat;
1122 1.1.18.1 yamt goto again;
1123 1.1.18.1 yamt }
1124 1.1.18.1 yamt sii_buf[1] = msg; /* message length */
1125 1.1.18.1 yamt if (msg == 0)
1126 1.1.18.1 yamt msg = 256;
1127 1.1.18.1 yamt /*
1128 1.1.18.1 yamt * We read and acknowlege all the bytes
1129 1.1.18.1 yamt * except the last so we can assert ATN
1130 1.1.18.1 yamt * if needed before acknowledging the last.
1131 1.1.18.1 yamt */
1132 1.1.18.1 yamt for (i = 0; i < msg; i++) {
1133 1.1.18.1 yamt dstat = sii_GetByte(regs,
1134 1.1.18.1 yamt SII_MSG_IN_PHASE, i < msg - 1);
1135 1.1.18.1 yamt if ((int)dstat < 0) {
1136 1.1.18.1 yamt dstat = regs->dstat;
1137 1.1.18.1 yamt goto again;
1138 1.1.18.1 yamt }
1139 1.1.18.1 yamt sii_buf[i + 2] = dstat;
1140 1.1.18.1 yamt }
1141 1.1.18.1 yamt
1142 1.1.18.1 yamt switch (sii_buf[2]) {
1143 1.1.18.1 yamt case MSG_EXT_MODIFY_DATA_PTR:
1144 1.1.18.1 yamt /* acknowledge last byte */
1145 1.1.18.1 yamt regs->comm = SII_INXFER |
1146 1.1.18.1 yamt SII_MSG_IN_PHASE |
1147 1.1.18.1 yamt (comm & SII_STATE_MSK);
1148 1.1.18.1 yamt SII_WAIT_UNTIL(dstat, regs->dstat,
1149 1.1.18.1 yamt dstat & SII_DNE,
1150 1.1.18.1 yamt SII_WAIT_COUNT, i);
1151 1.1.18.1 yamt regs->dstat = SII_DNE;
1152 1.1.18.1 yamt wbflush();
1153 1.1.18.1 yamt i = (sii_buf[3] << 24) |
1154 1.1.18.1 yamt (sii_buf[4] << 16) |
1155 1.1.18.1 yamt (sii_buf[5] << 8) |
1156 1.1.18.1 yamt sii_buf[6];
1157 1.1.18.1 yamt if (state->dmaPrevPhase >= 0) {
1158 1.1.18.1 yamt state->dmaAddrL += i;
1159 1.1.18.1 yamt state->dmaCnt -= i;
1160 1.1.18.1 yamt }
1161 1.1.18.1 yamt break;
1162 1.1.18.1 yamt
1163 1.1.18.1 yamt case MSG_EXT_SDTR_LEN:
1164 1.1.18.1 yamt /*
1165 1.1.18.1 yamt * Acknowledge last byte and
1166 1.1.18.1 yamt * signal a request for MSG_OUT.
1167 1.1.18.1 yamt */
1168 1.1.18.1 yamt regs->comm = SII_INXFER | SII_ATN |
1169 1.1.18.1 yamt SII_MSG_IN_PHASE |
1170 1.1.18.1 yamt (comm & SII_STATE_MSK);
1171 1.1.18.1 yamt SII_WAIT_UNTIL(dstat, regs->dstat,
1172 1.1.18.1 yamt dstat & SII_DNE,
1173 1.1.18.1 yamt SII_WAIT_COUNT, i);
1174 1.1.18.1 yamt regs->dstat = SII_DNE;
1175 1.1.18.1 yamt wbflush();
1176 1.1.18.1 yamt sii_DoSync(regs, state);
1177 1.1.18.1 yamt break;
1178 1.1.18.1 yamt
1179 1.1.18.1 yamt default:
1180 1.1.18.1 yamt reject:
1181 1.1.18.1 yamt /*
1182 1.1.18.1 yamt * Acknowledge last byte and
1183 1.1.18.1 yamt * signal a request for MSG_OUT.
1184 1.1.18.1 yamt */
1185 1.1.18.1 yamt regs->comm = SII_INXFER | SII_ATN |
1186 1.1.18.1 yamt SII_MSG_IN_PHASE |
1187 1.1.18.1 yamt (comm & SII_STATE_MSK);
1188 1.1.18.1 yamt SII_WAIT_UNTIL(dstat, regs->dstat,
1189 1.1.18.1 yamt dstat & SII_DNE,
1190 1.1.18.1 yamt SII_WAIT_COUNT, i);
1191 1.1.18.1 yamt regs->dstat = SII_DNE;
1192 1.1.18.1 yamt wbflush();
1193 1.1.18.1 yamt
1194 1.1.18.1 yamt /* wait for MSG_OUT phase */
1195 1.1.18.1 yamt SII_WAIT_UNTIL(dstat, regs->dstat,
1196 1.1.18.1 yamt dstat & SII_TBE,
1197 1.1.18.1 yamt SII_WAIT_COUNT, i);
1198 1.1.18.1 yamt
1199 1.1.18.1 yamt /* send a reject message */
1200 1.1.18.1 yamt regs->data = MSG_MESSAGE_REJECT;
1201 1.1.18.1 yamt regs->comm = SII_INXFER |
1202 1.1.18.1 yamt (regs->cstat & SII_STATE_MSK) |
1203 1.1.18.1 yamt SII_MSG_OUT_PHASE;
1204 1.1.18.1 yamt SII_WAIT_UNTIL(dstat, regs->dstat,
1205 1.1.18.1 yamt dstat & SII_DNE,
1206 1.1.18.1 yamt SII_WAIT_COUNT, i);
1207 1.1.18.1 yamt regs->dstat = SII_DNE;
1208 1.1.18.1 yamt wbflush();
1209 1.1.18.1 yamt }
1210 1.1.18.1 yamt break;
1211 1.1.18.1 yamt
1212 1.1.18.1 yamt case MSG_SAVEDATAPOINTER:
1213 1.1.18.1 yamt case MSG_RESTOREPOINTERS:
1214 1.1.18.1 yamt /* acknowledge last byte */
1215 1.1.18.1 yamt regs->comm = SII_INXFER | SII_MSG_IN_PHASE |
1216 1.1.18.1 yamt (comm & SII_STATE_MSK);
1217 1.1.18.1 yamt SII_WAIT_UNTIL(dstat, regs->dstat,
1218 1.1.18.1 yamt dstat & SII_DNE, SII_WAIT_COUNT, i);
1219 1.1.18.1 yamt regs->dstat = SII_DNE;
1220 1.1.18.1 yamt wbflush();
1221 1.1.18.1 yamt /* wait a short time for another msg */
1222 1.1.18.1 yamt SII_WAIT_UNTIL(dstat, regs->dstat,
1223 1.1.18.1 yamt dstat & (SII_CI | SII_DI),
1224 1.1.18.1 yamt SII_WAIT_COUNT, i);
1225 1.1.18.1 yamt if (dstat & (SII_CI | SII_DI)) {
1226 1.1.18.1 yamt #ifdef DEBUG
1227 1.1.18.1 yamt if (sii_debug > 4)
1228 1.1.18.1 yamt printf("cnt %d\n", i);
1229 1.1.18.1 yamt #endif
1230 1.1.18.1 yamt goto again;
1231 1.1.18.1 yamt }
1232 1.1.18.1 yamt break;
1233 1.1.18.1 yamt
1234 1.1.18.1 yamt case MSG_DISCONNECT:
1235 1.1.18.1 yamt /* acknowledge last byte */
1236 1.1.18.1 yamt regs->comm = SII_INXFER | SII_MSG_IN_PHASE |
1237 1.1.18.1 yamt (comm & SII_STATE_MSK);
1238 1.1.18.1 yamt SII_WAIT_UNTIL(dstat, regs->dstat,
1239 1.1.18.1 yamt dstat & SII_DNE, SII_WAIT_COUNT, i);
1240 1.1.18.1 yamt regs->dstat = SII_DNE;
1241 1.1.18.1 yamt wbflush();
1242 1.1.18.1 yamt state->prevComm = comm;
1243 1.1.18.1 yamt #ifdef DEBUG
1244 1.1.18.1 yamt if (sii_debug > 4)
1245 1.1.18.1 yamt printf("disconn %d ", sc->sc_target);
1246 1.1.18.1 yamt #endif
1247 1.1.18.1 yamt /*
1248 1.1.18.1 yamt * Wait a short time for disconnect.
1249 1.1.18.1 yamt * Don't be fooled if SII_BER happens first.
1250 1.1.18.1 yamt * Note: a reselect may happen here.
1251 1.1.18.1 yamt */
1252 1.1.18.1 yamt SII_WAIT_UNTIL(cstat, regs->cstat,
1253 1.1.18.1 yamt cstat & (SII_RST | SII_SCH),
1254 1.1.18.1 yamt SII_WAIT_COUNT, i);
1255 1.1.18.1 yamt if ((cstat & (SII_RST | SII_SCH |
1256 1.1.18.1 yamt SII_STATE_MSK)) != SII_SCH) {
1257 1.1.18.1 yamt #ifdef DEBUG
1258 1.1.18.1 yamt if (sii_debug > 4)
1259 1.1.18.1 yamt printf("cnt %d\n", i);
1260 1.1.18.1 yamt #endif
1261 1.1.18.1 yamt dstat = regs->dstat;
1262 1.1.18.1 yamt goto again;
1263 1.1.18.1 yamt }
1264 1.1.18.1 yamt regs->cstat = SII_SCH | SII_BER;
1265 1.1.18.1 yamt regs->comm = 0;
1266 1.1.18.1 yamt wbflush();
1267 1.1.18.1 yamt sc->sc_target = -1;
1268 1.1.18.1 yamt /*
1269 1.1.18.1 yamt * Double check that we didn't miss a state
1270 1.1.18.1 yamt * change between seeing it and clearing
1271 1.1.18.1 yamt * the SII_SCH bit.
1272 1.1.18.1 yamt */
1273 1.1.18.1 yamt i = regs->cstat;
1274 1.1.18.1 yamt if (!(i & SII_SCH) && (i & SII_STATE_MSK) !=
1275 1.1.18.1 yamt (cstat & SII_STATE_MSK))
1276 1.1.18.1 yamt sii_StateChg(sc, i);
1277 1.1.18.1 yamt break;
1278 1.1.18.1 yamt
1279 1.1.18.1 yamt case MSG_MESSAGE_REJECT:
1280 1.1.18.1 yamt /* acknowledge last byte */
1281 1.1.18.1 yamt regs->comm = SII_INXFER | SII_MSG_IN_PHASE |
1282 1.1.18.1 yamt (comm & SII_STATE_MSK);
1283 1.1.18.1 yamt SII_WAIT_UNTIL(dstat, regs->dstat,
1284 1.1.18.1 yamt dstat & SII_DNE, SII_WAIT_COUNT, i);
1285 1.1.18.1 yamt regs->dstat = SII_DNE;
1286 1.1.18.1 yamt wbflush();
1287 1.1.18.1 yamt printf("%s: device %d: message reject.\n",
1288 1.1.18.1 yamt sc->sc_dev.dv_xname, sc->sc_target);
1289 1.1.18.1 yamt break;
1290 1.1.18.1 yamt
1291 1.1.18.1 yamt default:
1292 1.1.18.1 yamt if (!(msg & MSG_IDENTIFYFLAG)) {
1293 1.1.18.1 yamt printf("%s: device %d: couldn't handle "
1294 1.1.18.1 yamt "message 0x%x... rejecting.\n",
1295 1.1.18.1 yamt sc->sc_dev.dv_xname, sc->sc_target,
1296 1.1.18.1 yamt msg);
1297 1.1.18.1 yamt #ifdef DEBUG
1298 1.1.18.1 yamt sii_DumpLog();
1299 1.1.18.1 yamt #endif
1300 1.1.18.1 yamt goto reject;
1301 1.1.18.1 yamt }
1302 1.1.18.1 yamt /* acknowledge last byte */
1303 1.1.18.1 yamt regs->comm = SII_INXFER | SII_MSG_IN_PHASE |
1304 1.1.18.1 yamt (comm & SII_STATE_MSK);
1305 1.1.18.1 yamt SII_WAIT_UNTIL(dstat, regs->dstat,
1306 1.1.18.1 yamt dstat & SII_DNE, SII_WAIT_COUNT, i);
1307 1.1.18.1 yamt regs->dstat = SII_DNE;
1308 1.1.18.1 yamt wbflush();
1309 1.1.18.1 yamt /* may want to check LUN some day */
1310 1.1.18.1 yamt /* wait a short time for another msg */
1311 1.1.18.1 yamt SII_WAIT_UNTIL(dstat, regs->dstat,
1312 1.1.18.1 yamt dstat & (SII_CI | SII_DI),
1313 1.1.18.1 yamt SII_WAIT_COUNT, i);
1314 1.1.18.1 yamt if (dstat & (SII_CI | SII_DI)) {
1315 1.1.18.1 yamt #ifdef DEBUG
1316 1.1.18.1 yamt if (sii_debug > 4)
1317 1.1.18.1 yamt printf("cnt %d\n", i);
1318 1.1.18.1 yamt #endif
1319 1.1.18.1 yamt goto again;
1320 1.1.18.1 yamt }
1321 1.1.18.1 yamt }
1322 1.1.18.1 yamt break;
1323 1.1.18.1 yamt
1324 1.1.18.1 yamt case SII_MSG_OUT_PHASE:
1325 1.1.18.1 yamt #ifdef DEBUG
1326 1.1.18.1 yamt if (sii_debug > 4)
1327 1.1.18.1 yamt printf("MsgOut\n");
1328 1.1.18.1 yamt #endif
1329 1.1.18.1 yamt printf("MsgOut %x\n", state->flags); /* XXX */
1330 1.1.18.1 yamt
1331 1.1.18.1 yamt /*
1332 1.1.18.1 yamt * Check for parity error.
1333 1.1.18.1 yamt * Hardware will automatically set ATN
1334 1.1.18.1 yamt * to request the device for a MSG_OUT phase.
1335 1.1.18.1 yamt */
1336 1.1.18.1 yamt if (state->flags & PARITY_ERR) {
1337 1.1.18.1 yamt state->flags &= ~PARITY_ERR;
1338 1.1.18.1 yamt regs->data = MSG_PARITY_ERROR;
1339 1.1.18.1 yamt } else
1340 1.1.18.1 yamt regs->data = MSG_NOOP;
1341 1.1.18.1 yamt regs->comm = SII_INXFER | (comm & SII_STATE_MSK) |
1342 1.1.18.1 yamt SII_MSG_OUT_PHASE;
1343 1.1.18.1 yamt wbflush();
1344 1.1.18.1 yamt
1345 1.1.18.1 yamt /* wait a short time for XFER complete */
1346 1.1.18.1 yamt SII_WAIT_UNTIL(dstat, regs->dstat, dstat & SII_DNE,
1347 1.1.18.1 yamt SII_WAIT_COUNT, i);
1348 1.1.18.1 yamt #ifdef DEBUG
1349 1.1.18.1 yamt if (sii_debug > 4)
1350 1.1.18.1 yamt printf("ds %x i %d\n", dstat, i);
1351 1.1.18.1 yamt #endif
1352 1.1.18.1 yamt /* just clear the DNE bit and check errors later */
1353 1.1.18.1 yamt if (dstat & SII_DNE) {
1354 1.1.18.1 yamt regs->dstat = SII_DNE;
1355 1.1.18.1 yamt wbflush();
1356 1.1.18.1 yamt }
1357 1.1.18.1 yamt break;
1358 1.1.18.1 yamt
1359 1.1.18.1 yamt default:
1360 1.1.18.1 yamt printf("%s: Couldn't handle phase %d... ignoring.\n",
1361 1.1.18.1 yamt sc->sc_dev.dv_xname, dstat & SII_PHASE_MSK);
1362 1.1.18.1 yamt }
1363 1.1.18.1 yamt }
1364 1.1.18.1 yamt
1365 1.1.18.1 yamt #ifdef DEBUG
1366 1.1.18.1 yamt if (sii_debug > 3)
1367 1.1.18.1 yamt printf("\n");
1368 1.1.18.1 yamt #endif
1369 1.1.18.1 yamt /*
1370 1.1.18.1 yamt * Check to make sure we won't be interrupted again.
1371 1.1.18.1 yamt * Deglitch dstat register.
1372 1.1.18.1 yamt */
1373 1.1.18.1 yamt msg = regs->dstat;
1374 1.1.18.1 yamt while (msg != (dstat = regs->dstat))
1375 1.1.18.1 yamt msg = dstat;
1376 1.1.18.1 yamt if (dstat & (SII_CI | SII_DI))
1377 1.1.18.1 yamt goto again;
1378 1.1.18.1 yamt
1379 1.1.18.1 yamt if (sc->sc_target < 0) {
1380 1.1.18.1 yamt /* look for another device that is ready */
1381 1.1.18.1 yamt for (i = 0; i < SII_NCMD; i++) {
1382 1.1.18.1 yamt /* don't restart a disconnected command */
1383 1.1.18.1 yamt if (!sc->sc_cmd[i] || sc->sc_st[i].prevComm)
1384 1.1.18.1 yamt continue;
1385 1.1.18.1 yamt sii_StartCmd(sc, i);
1386 1.1.18.1 yamt break;
1387 1.1.18.1 yamt }
1388 1.1.18.1 yamt }
1389 1.1.18.1 yamt return;
1390 1.1.18.1 yamt
1391 1.1.18.1 yamt abort:
1392 1.1.18.1 yamt /* jump here to abort the current command */
1393 1.1.18.1 yamt printf("%s: device %d: current command terminated\n",
1394 1.1.18.1 yamt sc->sc_dev.dv_xname, sc->sc_target);
1395 1.1.18.1 yamt #ifdef DEBUG
1396 1.1.18.1 yamt sii_DumpLog();
1397 1.1.18.1 yamt #endif
1398 1.1.18.1 yamt
1399 1.1.18.1 yamt if ((cstat = regs->cstat) & SII_CON) {
1400 1.1.18.1 yamt /* try to send an abort msg for awhile */
1401 1.1.18.1 yamt regs->dstat = SII_DNE;
1402 1.1.18.1 yamt regs->data = MSG_ABORT;
1403 1.1.18.1 yamt regs->comm = SII_INXFER | SII_ATN | (cstat & SII_STATE_MSK) |
1404 1.1.18.1 yamt SII_MSG_OUT_PHASE;
1405 1.1.18.1 yamt wbflush();
1406 1.1.18.1 yamt SII_WAIT_UNTIL(dstat, regs->dstat,
1407 1.1.18.1 yamt (dstat & (SII_DNE | SII_PHASE_MSK)) ==
1408 1.1.18.1 yamt (SII_DNE | SII_MSG_OUT_PHASE),
1409 1.1.18.1 yamt 2 * SII_WAIT_COUNT, i);
1410 1.1.18.1 yamt #ifdef DEBUG
1411 1.1.18.1 yamt if (sii_debug > 0)
1412 1.1.18.1 yamt printf("Abort: cs %x ds %x i %d\n", cstat, dstat, i);
1413 1.1.18.1 yamt #endif
1414 1.1.18.1 yamt if ((dstat & (SII_DNE | SII_PHASE_MSK)) ==
1415 1.1.18.1 yamt (SII_DNE | SII_MSG_OUT_PHASE)) {
1416 1.1.18.1 yamt /* disconnect if command in progress */
1417 1.1.18.1 yamt regs->comm = SII_DISCON;
1418 1.1.18.1 yamt wbflush();
1419 1.1.18.1 yamt SII_WAIT_UNTIL(cstat, regs->cstat,
1420 1.1.18.1 yamt !(cstat & SII_CON), SII_WAIT_COUNT, i);
1421 1.1.18.1 yamt }
1422 1.1.18.1 yamt } else {
1423 1.1.18.1 yamt #ifdef DEBUG
1424 1.1.18.1 yamt if (sii_debug > 0)
1425 1.1.18.1 yamt printf("Abort: cs %x\n", cstat);
1426 1.1.18.1 yamt #endif
1427 1.1.18.1 yamt }
1428 1.1.18.1 yamt regs->cstat = 0xffff;
1429 1.1.18.1 yamt regs->dstat = 0xffff;
1430 1.1.18.1 yamt regs->comm = 0;
1431 1.1.18.1 yamt wbflush();
1432 1.1.18.1 yamt
1433 1.1.18.1 yamt i = sc->sc_target;
1434 1.1.18.1 yamt sc->sc_target = -1;
1435 1.1.18.1 yamt sii_CmdDone(sc, i, EIO);
1436 1.1.18.1 yamt #ifdef DEBUG
1437 1.1.18.1 yamt if (sii_debug > 4)
1438 1.1.18.1 yamt printf("sii_DoIntr: after CmdDone target %d\n", sc->sc_target);
1439 1.1.18.1 yamt #endif
1440 1.1.18.1 yamt }
1441 1.1.18.1 yamt
1442 1.1.18.1 yamt static void
1443 1.1.18.1 yamt sii_StateChg(sc, cstat)
1444 1.1.18.1 yamt struct siisoftc *sc;
1445 1.1.18.1 yamt u_int cstat;
1446 1.1.18.1 yamt {
1447 1.1.18.1 yamt SIIRegs *regs = sc->sc_regs;
1448 1.1.18.1 yamt State *state;
1449 1.1.18.1 yamt int i;
1450 1.1.18.1 yamt
1451 1.1.18.1 yamt #ifdef DEBUG
1452 1.1.18.1 yamt if (sii_debug > 4)
1453 1.1.18.1 yamt printf("SCH: ");
1454 1.1.18.1 yamt #endif
1455 1.1.18.1 yamt
1456 1.1.18.1 yamt switch (cstat & SII_STATE_MSK) {
1457 1.1.18.1 yamt case 0:
1458 1.1.18.1 yamt /* disconnect */
1459 1.1.18.1 yamt i = sc->sc_target;
1460 1.1.18.1 yamt sc->sc_target = -1;
1461 1.1.18.1 yamt #ifdef DEBUG
1462 1.1.18.1 yamt if (sii_debug > 4)
1463 1.1.18.1 yamt printf("disconn %d ", i);
1464 1.1.18.1 yamt #endif
1465 1.1.18.1 yamt if (i >= 0 && !sc->sc_st[i].prevComm) {
1466 1.1.18.1 yamt printf("%s: device %d: spurrious disconnect (%d)\n",
1467 1.1.18.1 yamt sc->sc_dev.dv_xname, i, regs->slcsr);
1468 1.1.18.1 yamt sc->sc_st[i].prevComm = 0;
1469 1.1.18.1 yamt }
1470 1.1.18.1 yamt break;
1471 1.1.18.1 yamt
1472 1.1.18.1 yamt case SII_CON:
1473 1.1.18.1 yamt /* connected as initiator */
1474 1.1.18.1 yamt i = regs->slcsr;
1475 1.1.18.1 yamt if (sc->sc_target == i)
1476 1.1.18.1 yamt break;
1477 1.1.18.1 yamt printf("%s: device %d: connect to device %d??\n",
1478 1.1.18.1 yamt sc->sc_dev.dv_xname, sc->sc_target, i);
1479 1.1.18.1 yamt sc->sc_target = i;
1480 1.1.18.1 yamt break;
1481 1.1.18.1 yamt
1482 1.1.18.1 yamt case SII_DST:
1483 1.1.18.1 yamt /*
1484 1.1.18.1 yamt * Wait for CON to become valid,
1485 1.1.18.1 yamt * chip is slow sometimes.
1486 1.1.18.1 yamt */
1487 1.1.18.1 yamt SII_WAIT_UNTIL(cstat, regs->cstat,
1488 1.1.18.1 yamt cstat & SII_CON, SII_WAIT_COUNT, i);
1489 1.1.18.1 yamt if (!(cstat & SII_CON))
1490 1.1.18.1 yamt panic("sii resel");
1491 1.1.18.1 yamt /* FALLTHROUGH */
1492 1.1.18.1 yamt
1493 1.1.18.1 yamt case SII_CON | SII_DST:
1494 1.1.18.1 yamt /*
1495 1.1.18.1 yamt * Its a reselection. Save the ID and wait for
1496 1.1.18.1 yamt * interrupts to tell us what to do next
1497 1.1.18.1 yamt * (should be MSG_IN of IDENTIFY).
1498 1.1.18.1 yamt * NOTE: sc_target may be >= 0 if we were in
1499 1.1.18.1 yamt * the process of trying to start a command
1500 1.1.18.1 yamt * and were reselected before the select
1501 1.1.18.1 yamt * command finished.
1502 1.1.18.1 yamt */
1503 1.1.18.1 yamt sc->sc_target = i = regs->destat;
1504 1.1.18.1 yamt state = &sc->sc_st[i];
1505 1.1.18.1 yamt regs->comm = SII_CON | SII_DST | SII_MSG_IN_PHASE;
1506 1.1.18.1 yamt regs->dmctrl = state->dmaReqAck;
1507 1.1.18.1 yamt wbflush();
1508 1.1.18.1 yamt if (!state->prevComm) {
1509 1.1.18.1 yamt printf("%s: device %d: spurious reselection\n",
1510 1.1.18.1 yamt sc->sc_dev.dv_xname, i);
1511 1.1.18.1 yamt break;
1512 1.1.18.1 yamt }
1513 1.1.18.1 yamt state->prevComm = 0;
1514 1.1.18.1 yamt #ifdef DEBUG
1515 1.1.18.1 yamt if (sii_debug > 4)
1516 1.1.18.1 yamt printf("resel %d ", sc->sc_target);
1517 1.1.18.1 yamt #endif
1518 1.1.18.1 yamt break;
1519 1.1.18.1 yamt
1520 1.1.18.1 yamt #ifdef notyet
1521 1.1.18.1 yamt case SII_DST | SII_TGT:
1522 1.1.18.1 yamt case SII_CON | SII_DST | SII_TGT:
1523 1.1.18.1 yamt /* connected as target */
1524 1.1.18.1 yamt printf("%s: Selected by device %d as target!!\n",
1525 1.1.18.1 yamt sc->sc_dev.dv_xname, regs->destat);
1526 1.1.18.1 yamt regs->comm = SII_DISCON;
1527 1.1.18.1 yamt wbflush();
1528 1.1.18.1 yamt SII_WAIT_UNTIL(!(regs->cstat & SII_CON),
1529 1.1.18.1 yamt SII_WAIT_COUNT, i);
1530 1.1.18.1 yamt regs->cstat = 0xffff;
1531 1.1.18.1 yamt regs->dstat = 0xffff;
1532 1.1.18.1 yamt regs->comm = 0;
1533 1.1.18.1 yamt break;
1534 1.1.18.1 yamt #endif
1535 1.1.18.1 yamt
1536 1.1.18.1 yamt default:
1537 1.1.18.1 yamt printf("%s: Unknown state change (cs %x)!!\n",
1538 1.1.18.1 yamt sc->sc_dev.dv_xname, cstat);
1539 1.1.18.1 yamt #ifdef DEBUG
1540 1.1.18.1 yamt sii_DumpLog();
1541 1.1.18.1 yamt #endif
1542 1.1.18.1 yamt }
1543 1.1.18.1 yamt }
1544 1.1.18.1 yamt
1545 1.1.18.1 yamt /*
1546 1.1.18.1 yamt * Read one byte of data.
1547 1.1.18.1 yamt * If 'ack' is true, acknowledge the byte.
1548 1.1.18.1 yamt */
1549 1.1.18.1 yamt static int
1550 1.1.18.1 yamt sii_GetByte(regs, phase, ack)
1551 1.1.18.1 yamt SIIRegs *regs;
1552 1.1.18.1 yamt int phase, ack;
1553 1.1.18.1 yamt {
1554 1.1.18.1 yamt u_int dstat;
1555 1.1.18.1 yamt u_int state;
1556 1.1.18.1 yamt int i;
1557 1.1.18.1 yamt int data;
1558 1.1.18.1 yamt
1559 1.1.18.1 yamt dstat = regs->dstat;
1560 1.1.18.1 yamt state = regs->cstat & SII_STATE_MSK;
1561 1.1.18.1 yamt i = -1;
1562 1.1.18.1 yamt if (!(dstat & SII_IBF) || (dstat & SII_MIS)) {
1563 1.1.18.1 yamt regs->comm = state | phase;
1564 1.1.18.1 yamt wbflush();
1565 1.1.18.1 yamt /* wait a short time for IBF */
1566 1.1.18.1 yamt SII_WAIT_UNTIL(dstat, regs->dstat, dstat & SII_IBF,
1567 1.1.18.1 yamt SII_WAIT_COUNT, i);
1568 1.1.18.1 yamt #ifdef DEBUG
1569 1.1.18.1 yamt if (!(dstat & SII_IBF))
1570 1.1.18.1 yamt printf("status no IBF\n");
1571 1.1.18.1 yamt #endif
1572 1.1.18.1 yamt }
1573 1.1.18.1 yamt if (dstat & SII_DNE) { /* XXX */
1574 1.1.18.1 yamt printf("sii_GetByte: DNE set 5\n");
1575 1.1.18.1 yamt #ifdef DEBUG
1576 1.1.18.1 yamt sii_DumpLog();
1577 1.1.18.1 yamt #endif
1578 1.1.18.1 yamt regs->dstat = SII_DNE;
1579 1.1.18.1 yamt }
1580 1.1.18.1 yamt data = regs->data;
1581 1.1.18.1 yamt /* check for parity error */
1582 1.1.18.1 yamt if (dstat & SII_IPE) {
1583 1.1.18.1 yamt #ifdef DEBUG
1584 1.1.18.1 yamt if (sii_debug > 4)
1585 1.1.18.1 yamt printf("cnt0 %d\n", i);
1586 1.1.18.1 yamt #endif
1587 1.1.18.1 yamt printf("sii_GetByte: data %x ?? ds %x cm %x i %d\n",
1588 1.1.18.1 yamt data, dstat, regs->comm, i); /* XXX */
1589 1.1.18.1 yamt data = -1;
1590 1.1.18.1 yamt ack = 1;
1591 1.1.18.1 yamt }
1592 1.1.18.1 yamt
1593 1.1.18.1 yamt if (ack) {
1594 1.1.18.1 yamt regs->comm = SII_INXFER | state | phase;
1595 1.1.18.1 yamt wbflush();
1596 1.1.18.1 yamt
1597 1.1.18.1 yamt /* wait a short time for XFER complete */
1598 1.1.18.1 yamt SII_WAIT_UNTIL(dstat, regs->dstat, dstat & SII_DNE,
1599 1.1.18.1 yamt SII_WAIT_COUNT, i);
1600 1.1.18.1 yamt
1601 1.1.18.1 yamt /* clear the DNE */
1602 1.1.18.1 yamt if (dstat & SII_DNE) {
1603 1.1.18.1 yamt regs->dstat = SII_DNE;
1604 1.1.18.1 yamt wbflush();
1605 1.1.18.1 yamt }
1606 1.1.18.1 yamt }
1607 1.1.18.1 yamt
1608 1.1.18.1 yamt return (data);
1609 1.1.18.1 yamt }
1610 1.1.18.1 yamt
1611 1.1.18.1 yamt /*
1612 1.1.18.1 yamt * Exchange messages to initiate synchronous data transfers.
1613 1.1.18.1 yamt */
1614 1.1.18.1 yamt static void
1615 1.1.18.1 yamt sii_DoSync(regs, state)
1616 1.1.18.1 yamt SIIRegs *regs;
1617 1.1.18.1 yamt State *state;
1618 1.1.18.1 yamt {
1619 1.1.18.1 yamt u_int dstat, comm;
1620 1.1.18.1 yamt int i, j;
1621 1.1.18.1 yamt u_int len;
1622 1.1.18.1 yamt
1623 1.1.18.1 yamt #ifdef DEBUG
1624 1.1.18.1 yamt if (sii_debug)
1625 1.1.18.1 yamt printf("sii_DoSync: len %d per %d req/ack %d\n",
1626 1.1.18.1 yamt sii_buf[1], sii_buf[3], sii_buf[4]);
1627 1.1.18.1 yamt #endif
1628 1.1.18.1 yamt
1629 1.1.18.1 yamt /* SII chip can only handle a minimum transfer period of ??? */
1630 1.1.18.1 yamt if (sii_buf[3] < 64)
1631 1.1.18.1 yamt sii_buf[3] = 64;
1632 1.1.18.1 yamt /* SII chip can only handle a maximum REQ/ACK offset of 3 */
1633 1.1.18.1 yamt len = sii_buf[4];
1634 1.1.18.1 yamt if (len > 3)
1635 1.1.18.1 yamt len = 3;
1636 1.1.18.1 yamt
1637 1.1.18.1 yamt sii_buf[0] = MSG_EXTENDED;
1638 1.1.18.1 yamt sii_buf[1] = MSG_EXT_SDTR_LEN;
1639 1.1.18.1 yamt sii_buf[2] = MSG_EXT_SDTR;
1640 1.1.18.1 yamt sii_buf[4] = len;
1641 1.1.18.1 yamt #if 1
1642 1.1.18.1 yamt comm = SII_INXFER | SII_ATN | SII_MSG_OUT_PHASE |
1643 1.1.18.1 yamt (regs->cstat & SII_STATE_MSK);
1644 1.1.18.1 yamt regs->comm = comm & ~SII_INXFER;
1645 1.1.18.1 yamt for (j = 0; j < 5; j++) {
1646 1.1.18.1 yamt /* wait for target to request the next byte */
1647 1.1.18.1 yamt SII_WAIT_UNTIL(dstat, regs->dstat, dstat & SII_TBE,
1648 1.1.18.1 yamt SII_WAIT_COUNT, i);
1649 1.1.18.1 yamt if (!(dstat & SII_TBE) ||
1650 1.1.18.1 yamt (dstat & SII_PHASE_MSK) != SII_MSG_OUT_PHASE) {
1651 1.1.18.1 yamt printf("sii_DoSync: TBE? ds %x cm %x i %d\n",
1652 1.1.18.1 yamt dstat, comm, i); /* XXX */
1653 1.1.18.1 yamt return;
1654 1.1.18.1 yamt }
1655 1.1.18.1 yamt
1656 1.1.18.1 yamt /* the last message byte should have ATN off */
1657 1.1.18.1 yamt if (j == 4)
1658 1.1.18.1 yamt comm &= ~SII_ATN;
1659 1.1.18.1 yamt
1660 1.1.18.1 yamt regs->data = sii_buf[j];
1661 1.1.18.1 yamt regs->comm = comm;
1662 1.1.18.1 yamt wbflush();
1663 1.1.18.1 yamt
1664 1.1.18.1 yamt /* wait a short time for XFER complete */
1665 1.1.18.1 yamt SII_WAIT_UNTIL(dstat, regs->dstat, dstat & SII_DNE,
1666 1.1.18.1 yamt SII_WAIT_COUNT, i);
1667 1.1.18.1 yamt
1668 1.1.18.1 yamt if (!(dstat & SII_DNE)) {
1669 1.1.18.1 yamt printf("sii_DoSync: DNE? ds %x cm %x i %d\n",
1670 1.1.18.1 yamt dstat, comm, i); /* XXX */
1671 1.1.18.1 yamt return;
1672 1.1.18.1 yamt }
1673 1.1.18.1 yamt
1674 1.1.18.1 yamt /* clear the DNE, other errors handled later */
1675 1.1.18.1 yamt regs->dstat = SII_DNE;
1676 1.1.18.1 yamt wbflush();
1677 1.1.18.1 yamt }
1678 1.1.18.1 yamt #else /* 0 */
1679 1.1.18.1 yamt sc->sii_copytobuf((u_short *)sii_buf,
1680 1.1.18.1 yamt (volatile u_short *)SII_BUF_ADDR(sc), 5);
1681 1.1.18.1 yamt printf("sii_DoSync: %x %x %x ds %x\n",
1682 1.1.18.1 yamt ((volatile u_short *)SII_BUF_ADDR(sc))[0],
1683 1.1.18.1 yamt ((volatile u_short *)SII_BUF_ADDR(sc))[2],
1684 1.1.18.1 yamt ((volatile u_short *)SII_BUF_ADDR(sc))[4],
1685 1.1.18.1 yamt regs->dstat); /* XXX */
1686 1.1.18.1 yamt regs->dmaddrl = (u_short)(SII_BUF_ADDR(sc) >> 1);
1687 1.1.18.1 yamt regs->dmaddrh = (u_short)(SII_BUF_ADDR(sc) >> 17) & 03;
1688 1.1.18.1 yamt regs->dmlotc = 5;
1689 1.1.18.1 yamt regs->comm = SII_DMA | SII_INXFER | SII_ATN |
1690 1.1.18.1 yamt (regs->cstat & SII_STATE_MSK) | SII_MSG_OUT_PHASE;
1691 1.1.18.1 yamt wbflush();
1692 1.1.18.1 yamt
1693 1.1.18.1 yamt /* wait a short time for XFER complete */
1694 1.1.18.1 yamt SII_WAIT_UNTIL(dstat, regs->dstat,
1695 1.1.18.1 yamt (dstat & (SII_DNE | SII_TCZ)) == (SII_DNE | SII_TCZ),
1696 1.1.18.1 yamt SII_WAIT_COUNT, i);
1697 1.1.18.1 yamt
1698 1.1.18.1 yamt if ((dstat & (SII_DNE | SII_TCZ)) != (SII_DNE | SII_TCZ)) {
1699 1.1.18.1 yamt printf("sii_DoSync: ds %x cm %x i %d lotc %d\n",
1700 1.1.18.1 yamt dstat, regs->comm, i, regs->dmlotc); /* XXX */
1701 1.1.18.1 yamt sii_DumpLog(); /* XXX */
1702 1.1.18.1 yamt return;
1703 1.1.18.1 yamt }
1704 1.1.18.1 yamt /* clear the DNE, other errors handled later */
1705 1.1.18.1 yamt regs->dstat = SII_DNE;
1706 1.1.18.1 yamt wbflush();
1707 1.1.18.1 yamt #endif /* 0 */
1708 1.1.18.1 yamt
1709 1.1.18.1 yamt #if 0
1710 1.1.18.1 yamt SII_WAIT_UNTIL(dstat, regs->dstat, dstat & (SII_CI | SII_DI),
1711 1.1.18.1 yamt SII_WAIT_COUNT, i);
1712 1.1.18.1 yamt printf("sii_DoSync: ds %x cm %x i %d lotc %d\n",
1713 1.1.18.1 yamt dstat, regs->comm, i, regs->dmlotc); /* XXX */
1714 1.1.18.1 yamt #endif
1715 1.1.18.1 yamt
1716 1.1.18.1 yamt state->dmaReqAck = len;
1717 1.1.18.1 yamt }
1718 1.1.18.1 yamt
1719 1.1.18.1 yamt /*
1720 1.1.18.1 yamt * Issue the sequence of commands to the controller to start DMA.
1721 1.1.18.1 yamt * NOTE: the data buffer should be word-aligned for DMA out.
1722 1.1.18.1 yamt */
1723 1.1.18.1 yamt static void
1724 1.1.18.1 yamt sii_StartDMA(regs, phase, dmaAddr, size)
1725 1.1.18.1 yamt SIIRegs *regs; /* which SII to use */
1726 1.1.18.1 yamt int phase; /* phase to send/receive data */
1727 1.1.18.1 yamt u_short *dmaAddr; /* DMA buffer address */
1728 1.1.18.1 yamt int size; /* # of bytes to transfer */
1729 1.1.18.1 yamt {
1730 1.1.18.1 yamt
1731 1.1.18.1 yamt if (regs->dstat & SII_DNE) { /* XXX */
1732 1.1.18.1 yamt regs->dstat = SII_DNE;
1733 1.1.18.1 yamt printf("sii_StartDMA: DNE set\n");
1734 1.1.18.1 yamt #ifdef DEBUG
1735 1.1.18.1 yamt sii_DumpLog();
1736 1.1.18.1 yamt #endif
1737 1.1.18.1 yamt }
1738 1.1.18.1 yamt regs->dmaddrl = ((u_long)dmaAddr >> 1);
1739 1.1.18.1 yamt regs->dmaddrh = ((u_long)dmaAddr >> 17) & 03;
1740 1.1.18.1 yamt regs->dmlotc = size;
1741 1.1.18.1 yamt regs->comm = SII_DMA | SII_INXFER | (regs->cstat & SII_STATE_MSK) |
1742 1.1.18.1 yamt phase;
1743 1.1.18.1 yamt wbflush();
1744 1.1.18.1 yamt
1745 1.1.18.1 yamt #ifdef DEBUG
1746 1.1.18.1 yamt if (sii_debug > 5) {
1747 1.1.18.1 yamt printf("sii_StartDMA: cs 0x%x, ds 0x%x, cm 0x%x, size %d\n",
1748 1.1.18.1 yamt regs->cstat, regs->dstat, regs->comm, size);
1749 1.1.18.1 yamt }
1750 1.1.18.1 yamt #endif
1751 1.1.18.1 yamt }
1752 1.1.18.1 yamt
1753 1.1.18.1 yamt /*
1754 1.1.18.1 yamt * Call the device driver's 'done' routine to let it know the command is done.
1755 1.1.18.1 yamt * The 'done' routine may try to start another command.
1756 1.1.18.1 yamt * To be fair, we should start pending commands for other devices
1757 1.1.18.1 yamt * before allowing the same device to start another command.
1758 1.1.18.1 yamt */
1759 1.1.18.1 yamt static void
1760 1.1.18.1 yamt sii_CmdDone(sc, target, error)
1761 1.1.18.1 yamt struct siisoftc *sc; /* which SII to use */
1762 1.1.18.1 yamt int target; /* which device is done */
1763 1.1.18.1 yamt int error; /* error code if any errors */
1764 1.1.18.1 yamt {
1765 1.1.18.1 yamt ScsiCmd *scsicmd;
1766 1.1.18.1 yamt int i;
1767 1.1.18.1 yamt
1768 1.1.18.1 yamt scsicmd = sc->sc_cmd[target];
1769 1.1.18.1 yamt #ifdef DIAGNOSTIC
1770 1.1.18.1 yamt if (target < 0 || !scsicmd)
1771 1.1.18.1 yamt panic("sii_CmdDone");
1772 1.1.18.1 yamt #endif
1773 1.1.18.1 yamt sc->sc_cmd[target] = (ScsiCmd *)0;
1774 1.1.18.1 yamt #ifdef DEBUG
1775 1.1.18.1 yamt if (sii_debug > 1) {
1776 1.1.18.1 yamt printf("sii_CmdDone: %s target %d cmd %x err %d resid %d\n",
1777 1.1.18.1 yamt sc->sc_dev.dv_xname,
1778 1.1.18.1 yamt target, scsicmd->cmd[0], error, sc->sc_st[target].buflen);
1779 1.1.18.1 yamt }
1780 1.1.18.1 yamt #endif
1781 1.1.18.1 yamt
1782 1.1.18.1 yamt /* look for another device that is ready */
1783 1.1.18.1 yamt for (i = 0; i < SII_NCMD; i++) {
1784 1.1.18.1 yamt /* don't restart a disconnected command */
1785 1.1.18.1 yamt if (!sc->sc_cmd[i] || sc->sc_st[i].prevComm)
1786 1.1.18.1 yamt continue;
1787 1.1.18.1 yamt sii_StartCmd(sc, i);
1788 1.1.18.1 yamt break;
1789 1.1.18.1 yamt }
1790 1.1.18.1 yamt
1791 1.1.18.1 yamt sc->sc_xs[target]->status = sc->sc_st[target].statusByte;
1792 1.1.18.1 yamt /*
1793 1.1.18.1 yamt * Convert SII driver error code to MI SCSI XS_*.
1794 1.1.18.1 yamt */
1795 1.1.18.1 yamt switch (error) {
1796 1.1.18.1 yamt case 0:
1797 1.1.18.1 yamt sc->sc_xs[target]->error = XS_NOERROR;
1798 1.1.18.1 yamt break;
1799 1.1.18.1 yamt case ENXIO:
1800 1.1.18.1 yamt sc->sc_xs[target]->error = XS_SELTIMEOUT;
1801 1.1.18.1 yamt break;
1802 1.1.18.1 yamt case EBUSY:
1803 1.1.18.1 yamt sc->sc_xs[target]->error = XS_BUSY;
1804 1.1.18.1 yamt break;
1805 1.1.18.1 yamt case EIO:
1806 1.1.18.1 yamt sc->sc_xs[target]->error = XS_DRIVER_STUFFUP;
1807 1.1.18.1 yamt break;
1808 1.1.18.1 yamt default:
1809 1.1.18.1 yamt sc->sc_xs[target]->error = XS_DRIVER_STUFFUP;
1810 1.1.18.1 yamt }
1811 1.1.18.1 yamt sc->sc_xs[target]->resid = sc->sc_st[target].buflen;
1812 1.1.18.1 yamt scsipi_done(sc->sc_xs[target]);
1813 1.1.18.1 yamt }
1814 1.1.18.1 yamt
1815 1.1.18.1 yamt #ifdef DEBUG
1816 1.1.18.1 yamt static void
1817 1.1.18.1 yamt sii_DumpLog()
1818 1.1.18.1 yamt {
1819 1.1.18.1 yamt struct sii_log *lp;
1820 1.1.18.1 yamt
1821 1.1.18.1 yamt printf("sii: cmd %x bn %d cnt %d\n", sii_debug_cmd, sii_debug_bn,
1822 1.1.18.1 yamt sii_debug_sz);
1823 1.1.18.1 yamt lp = sii_logp;
1824 1.1.18.1 yamt do {
1825 1.1.18.1 yamt printf("target %d cs %x ds %x cm %x msg %x rlen %x dlen %x\n",
1826 1.1.18.1 yamt lp->target, lp->cstat, lp->dstat, lp->comm, lp->msg,
1827 1.1.18.1 yamt lp->rlen, lp->dlen);
1828 1.1.18.1 yamt if (++lp >= &sii_log[NLOG])
1829 1.1.18.1 yamt lp = sii_log;
1830 1.1.18.1 yamt } while (lp != sii_logp);
1831 1.1.18.1 yamt }
1832 1.1.18.1 yamt #endif
1833