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