ncr53c9x.c revision 1.14 1 /* $NetBSD: ncr53c9x.c,v 1.14 1997/07/22 18:55:20 pk Exp $ */
2
3 /*
4 * Copyright (c) 1996 Charles M. Hannum. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Charles M. Hannum.
17 * 4. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 1994 Peter Galbavy
34 * Copyright (c) 1995 Paul Kranenburg
35 * All rights reserved.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by Peter Galbavy
48 * 4. The name of the author may not be used to endorse or promote products
49 * derived from this software without specific prior written permission.
50 *
51 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
52 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
53 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
54 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
55 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
56 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
57 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
59 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
60 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
61 * POSSIBILITY OF SUCH DAMAGE.
62 */
63
64 /*
65 * Based on aic6360 by Jarle Greipsland
66 *
67 * Acknowledgements: Many of the algorithms used in this driver are
68 * inspired by the work of Julian Elischer (julian (at) tfs.com) and
69 * Charles Hannum (mycroft (at) duality.gnu.ai.mit.edu). Thanks a million!
70 */
71
72 #include <sys/types.h>
73 #include <sys/param.h>
74 #include <sys/systm.h>
75 #include <sys/kernel.h>
76 #include <sys/errno.h>
77 #include <sys/ioctl.h>
78 #include <sys/device.h>
79 #include <sys/buf.h>
80 #include <sys/proc.h>
81 #include <sys/user.h>
82 #include <sys/queue.h>
83
84 #include <scsi/scsi_all.h>
85 #include <scsi/scsiconf.h>
86 #include <scsi/scsi_message.h>
87
88 #include <machine/cpu.h>
89
90 #include <dev/ic/ncr53c9xreg.h>
91 #include <dev/ic/ncr53c9xvar.h>
92
93 int ncr53c9x_debug = 0; /*NCR_SHOWPHASE|NCR_SHOWMISC|NCR_SHOWTRAC|NCR_SHOWCMDS;*/
94
95 /*static*/ void ncr53c9x_readregs __P((struct ncr53c9x_softc *));
96 /*static*/ void ncr53c9x_select __P((struct ncr53c9x_softc *,
97 struct ncr53c9x_ecb *));
98 /*static*/ int ncr53c9x_reselect __P((struct ncr53c9x_softc *, int));
99 /*static*/ void ncr53c9x_scsi_reset __P((struct ncr53c9x_softc *));
100 /*static*/ void ncr53c9x_init __P((struct ncr53c9x_softc *, int));
101 /*static*/ int ncr53c9x_poll __P((struct ncr53c9x_softc *,
102 struct scsi_xfer *, int));
103 /*static*/ void ncr53c9x_sched __P((struct ncr53c9x_softc *));
104 /*static*/ void ncr53c9x_done __P((struct ncr53c9x_softc *,
105 struct ncr53c9x_ecb *));
106 /*static*/ void ncr53c9x_msgin __P((struct ncr53c9x_softc *));
107 /*static*/ void ncr53c9x_msgout __P((struct ncr53c9x_softc *));
108 /*static*/ void ncr53c9x_timeout __P((void *arg));
109 /*static*/ void ncr53c9x_abort __P((struct ncr53c9x_softc *,
110 struct ncr53c9x_ecb *));
111 /*static*/ void ncr53c9x_dequeue __P((struct ncr53c9x_softc *,
112 struct ncr53c9x_ecb *));
113
114 void ncr53c9x_sense __P((struct ncr53c9x_softc *,
115 struct ncr53c9x_ecb *));
116 void ncr53c9x_free_ecb __P((struct ncr53c9x_softc *,
117 struct ncr53c9x_ecb *, int));
118 struct ncr53c9x_ecb *ncr53c9x_get_ecb __P((struct ncr53c9x_softc *, int));
119
120 static inline int ncr53c9x_stp2cpb __P((struct ncr53c9x_softc *, int));
121 static inline void ncr53c9x_setsync __P((struct ncr53c9x_softc *,
122 struct ncr53c9x_tinfo *));
123
124 /*
125 * Names for the NCR53c9x variants, correspnding to the variant tags
126 * in ncr53c9xvar.h.
127 */
128 const char *ncr53c9x_variant_names[] = {
129 "ESP100",
130 "ESP100A",
131 "ESP200",
132 "NCR53C94",
133 "NCR53C96",
134 "ESP406",
135 "FAS408",
136 };
137
138 /*
139 * Attach this instance, and then all the sub-devices
140 */
141 void
142 ncr53c9x_attach(sc, adapter, dev)
143 struct ncr53c9x_softc *sc;
144 struct scsi_adapter *adapter;
145 struct scsi_device *dev;
146 {
147
148 /*
149 * Note, the front-end has set us up to print the chip variation.
150 */
151
152 if (sc->sc_rev >= NCR_VARIANT_MAX) {
153 printf("\n%s: unknown variant %d, devices not attached\n",
154 sc->sc_dev.dv_xname, sc->sc_rev);
155 return;
156 }
157
158 printf(": %s, %dMHz, SCSI ID %d\n",
159 ncr53c9x_variant_names[sc->sc_rev], sc->sc_freq, sc->sc_id);
160
161 sc->sc_ccf = FREQTOCCF(sc->sc_freq);
162
163 /* The value *must not* be == 1. Make it 2 */
164 if (sc->sc_ccf == 1)
165 sc->sc_ccf = 2;
166
167 /*
168 * The recommended timeout is 250ms. This register is loaded
169 * with a value calculated as follows, from the docs:
170 *
171 * (timout period) x (CLK frequency)
172 * reg = -------------------------------------
173 * 8192 x (Clock Conversion Factor)
174 *
175 * Since CCF has a linear relation to CLK, this generally computes
176 * to the constant of 153.
177 */
178 sc->sc_timeout = ((250 * 1000) * sc->sc_freq) / (8192 * sc->sc_ccf);
179
180 /* CCF register only has 3 bits; 0 is actually 8 */
181 sc->sc_ccf &= 7;
182
183 /* Reset state & bus */
184 sc->sc_state = 0;
185 ncr53c9x_init(sc, 1);
186
187 /*
188 * fill in the prototype scsi_link.
189 */
190 sc->sc_link.channel = SCSI_CHANNEL_ONLY_ONE;
191 sc->sc_link.adapter_softc = sc;
192 sc->sc_link.adapter_target = sc->sc_id;
193 sc->sc_link.adapter = adapter;
194 sc->sc_link.device = dev;
195 sc->sc_link.openings = 2;
196 sc->sc_link.max_target = 7;
197
198 /*
199 * Now try to attach all the sub-devices
200 */
201 config_found(&sc->sc_dev, &sc->sc_link, scsiprint);
202
203 /*
204 * Enable interupts from the SCSI core
205 */
206 if ((sc->sc_rev == NCR_VARIANT_ESP406) ||
207 (sc->sc_rev == NCR_VARIANT_FAS408)) {
208 NCR_PIOREGS(sc);
209 NCR_WRITE_REG(sc, NCR_CFG5, NCRCFG5_SINT |
210 NCR_READ_REG(sc, NCR_CFG5));
211 NCR_SCSIREGS(sc);
212 }
213 }
214
215 /*
216 * This is the generic esp reset function. It does not reset the SCSI bus,
217 * only this controllers, but kills any on-going commands, and also stops
218 * and resets the DMA.
219 *
220 * After reset, registers are loaded with the defaults from the attach
221 * routine above.
222 */
223 void
224 ncr53c9x_reset(sc)
225 struct ncr53c9x_softc *sc;
226 {
227
228 /* reset DMA first */
229 NCRDMA_RESET(sc);
230
231 /* reset SCSI chip */
232 NCRCMD(sc, NCRCMD_RSTCHIP);
233 NCRCMD(sc, NCRCMD_NOP);
234 DELAY(500);
235
236 /* do these backwards, and fall through */
237 switch (sc->sc_rev) {
238 case NCR_VARIANT_ESP406:
239 case NCR_VARIANT_FAS408:
240 NCR_SCSIREGS(sc);
241 case NCR_VARIANT_NCR53C94:
242 case NCR_VARIANT_NCR53C96:
243 case NCR_VARIANT_ESP200:
244 NCR_WRITE_REG(sc, NCR_CFG3, sc->sc_cfg3);
245 case NCR_VARIANT_ESP100A:
246 NCR_WRITE_REG(sc, NCR_CFG2, sc->sc_cfg2);
247 case NCR_VARIANT_ESP100:
248 NCR_WRITE_REG(sc, NCR_CFG1, sc->sc_cfg1);
249 NCR_WRITE_REG(sc, NCR_CCF, sc->sc_ccf);
250 NCR_WRITE_REG(sc, NCR_SYNCOFF, 0);
251 NCR_WRITE_REG(sc, NCR_TIMEOUT, sc->sc_timeout);
252 break;
253 default:
254 printf("%s: unknown revision code, assuming ESP100\n",
255 sc->sc_dev.dv_xname);
256 NCR_WRITE_REG(sc, NCR_CFG1, sc->sc_cfg1);
257 NCR_WRITE_REG(sc, NCR_CCF, sc->sc_ccf);
258 NCR_WRITE_REG(sc, NCR_SYNCOFF, 0);
259 NCR_WRITE_REG(sc, NCR_TIMEOUT, sc->sc_timeout);
260 }
261 }
262
263 /*
264 * Reset the SCSI bus, but not the chip
265 */
266 void
267 ncr53c9x_scsi_reset(sc)
268 struct ncr53c9x_softc *sc;
269 {
270
271 (*sc->sc_glue->gl_dma_stop)(sc);
272
273 printf("%s: resetting SCSI bus\n", sc->sc_dev.dv_xname);
274 NCRCMD(sc, NCRCMD_RSTSCSI);
275 }
276
277 /*
278 * Initialize esp state machine
279 */
280 void
281 ncr53c9x_init(sc, doreset)
282 struct ncr53c9x_softc *sc;
283 int doreset;
284 {
285 struct ncr53c9x_ecb *ecb;
286 int r;
287
288 NCR_TRACE(("[NCR_INIT(%d)] ", doreset));
289
290 if (sc->sc_state == 0) {
291 /* First time through; initialize. */
292 TAILQ_INIT(&sc->ready_list);
293 TAILQ_INIT(&sc->nexus_list);
294 TAILQ_INIT(&sc->free_list);
295 sc->sc_nexus = NULL;
296 ecb = sc->sc_ecb;
297 bzero(ecb, sizeof(sc->sc_ecb));
298 for (r = 0; r < sizeof(sc->sc_ecb) / sizeof(*ecb); r++) {
299 TAILQ_INSERT_TAIL(&sc->free_list, ecb, chain);
300 ecb++;
301 }
302 bzero(sc->sc_tinfo, sizeof(sc->sc_tinfo));
303 } else {
304 /* Cancel any active commands. */
305 sc->sc_state = NCR_CLEANING;
306 if ((ecb = sc->sc_nexus) != NULL) {
307 ecb->xs->error = XS_DRIVER_STUFFUP;
308 ncr53c9x_done(sc, ecb);
309 }
310 while ((ecb = sc->nexus_list.tqh_first) != NULL) {
311 ecb->xs->error = XS_DRIVER_STUFFUP;
312 ncr53c9x_done(sc, ecb);
313 }
314 }
315
316 /*
317 * reset the chip to a known state
318 */
319 ncr53c9x_reset(sc);
320
321 sc->sc_phase = sc->sc_prevphase = INVALID_PHASE;
322 for (r = 0; r < 8; r++) {
323 struct ncr53c9x_tinfo *ti = &sc->sc_tinfo[r];
324 /* XXX - config flags per target: low bits: no reselect; high bits: no synch */
325 int fl = sc->sc_dev.dv_cfdata->cf_flags;
326
327 ti->flags = ((sc->sc_minsync && !(fl & (1<<(r+8))))
328 ? T_NEGOTIATE : 0) |
329 ((fl & (1<<r)) ? T_RSELECTOFF : 0) |
330 T_NEED_TO_RESET;
331 ti->period = sc->sc_minsync;
332 ti->offset = 0;
333 }
334
335 if (doreset) {
336 sc->sc_state = NCR_SBR;
337 NCRCMD(sc, NCRCMD_RSTSCSI);
338 } else {
339 sc->sc_state = NCR_IDLE;
340 }
341 }
342
343 /*
344 * Read the NCR registers, and save their contents for later use.
345 * NCR_STAT, NCR_STEP & NCR_INTR are mostly zeroed out when reading
346 * NCR_INTR - so make sure it is the last read.
347 *
348 * I think that (from reading the docs) most bits in these registers
349 * only make sense when he DMA CSR has an interrupt showing. Call only
350 * if an interrupt is pending.
351 */
352 void
353 ncr53c9x_readregs(sc)
354 struct ncr53c9x_softc *sc;
355 {
356
357 sc->sc_espstat = NCR_READ_REG(sc, NCR_STAT);
358 /* Only the stepo bits are of interest */
359 sc->sc_espstep = NCR_READ_REG(sc, NCR_STEP) & NCRSTEP_MASK;
360 sc->sc_espintr = NCR_READ_REG(sc, NCR_INTR);
361
362 if (sc->sc_glue->gl_clear_latched_intr != NULL)
363 (*sc->sc_glue->gl_clear_latched_intr)(sc);
364
365 /*
366 * Determine the SCSI bus phase, return either a real SCSI bus phase
367 * or some pseudo phase we use to detect certain exceptions.
368 */
369
370 sc->sc_phase = (sc->sc_espintr & NCRINTR_DIS)
371 ? /* Disconnected */ BUSFREE_PHASE
372 : sc->sc_espstat & NCRSTAT_PHASE;
373
374 NCR_MISC(("regs[intr=%02x,stat=%02x,step=%02x] ",
375 sc->sc_espintr, sc->sc_espstat, sc->sc_espstep));
376 }
377
378 /*
379 * Convert Synchronous Transfer Period to chip register Clock Per Byte value.
380 */
381 static inline int
382 ncr53c9x_stp2cpb(sc, period)
383 struct ncr53c9x_softc *sc;
384 int period;
385 {
386 int v;
387 v = (sc->sc_freq * period) / 250;
388 if (ncr53c9x_cpb2stp(sc, v) < period)
389 /* Correct round-down error */
390 v++;
391 return v;
392 }
393
394 static inline void
395 ncr53c9x_setsync(sc, ti)
396 struct ncr53c9x_softc *sc;
397 struct ncr53c9x_tinfo *ti;
398 {
399
400 if (ti->flags & T_SYNCMODE) {
401 NCR_WRITE_REG(sc, NCR_SYNCOFF, ti->offset);
402 NCR_WRITE_REG(sc, NCR_SYNCTP,
403 ncr53c9x_stp2cpb(sc, ti->period));
404 } else {
405 NCR_WRITE_REG(sc, NCR_SYNCOFF, 0);
406 NCR_WRITE_REG(sc, NCR_SYNCTP, 0);
407 }
408 }
409
410 int ncr53c9x_dmaselect = 0;
411 /*
412 * Send a command to a target, set the driver state to NCR_SELECTING
413 * and let the caller take care of the rest.
414 *
415 * Keeping this as a function allows me to say that this may be done
416 * by DMA instead of programmed I/O soon.
417 */
418 void
419 ncr53c9x_select(sc, ecb)
420 struct ncr53c9x_softc *sc;
421 struct ncr53c9x_ecb *ecb;
422 {
423 struct scsi_link *sc_link = ecb->xs->sc_link;
424 int target = sc_link->target;
425 struct ncr53c9x_tinfo *ti = &sc->sc_tinfo[target];
426 u_char *cmd;
427 int clen;
428
429 NCR_TRACE(("[ncr53c9x_select(t%d,l%d,cmd:%x)] ",
430 sc_link->target, sc_link->lun, ecb->cmd.cmd.opcode));
431
432 /* new state NCR_SELECTING */
433 sc->sc_state = NCR_SELECTING;
434
435 /*
436 * Schedule the timeout now, the first time we will go away
437 * expecting to come back due to an interrupt, because it is
438 * always possible that the interrupt may never happen.
439 */
440 if ((ecb->xs->flags & SCSI_POLL) == 0)
441 timeout(ncr53c9x_timeout, ecb,
442 (ecb->timeout * hz) / 1000);
443
444 NCRCMD(sc, NCRCMD_FLUSH);
445
446 /*
447 * The docs say the target register is never reset, and I
448 * can't think of a better place to set it
449 */
450 NCR_WRITE_REG(sc, NCR_SELID, target);
451 ncr53c9x_setsync(sc, ti);
452
453 if (ncr53c9x_dmaselect && (ti->flags & T_NEGOTIATE) == 0) {
454 size_t dmacl;
455 ecb->cmd.id =
456 MSG_IDENTIFY(sc_link->lun, (ti->flags & T_RSELECTOFF)?0:1);
457
458 /* setup DMA transfer for command */
459 clen = ecb->clen + 1;
460 sc->sc_cmdlen = clen;
461 sc->sc_cmdp = (caddr_t)&ecb->cmd;
462 dmacl = clen;
463 NCRDMA_SETUP(sc, &sc->sc_cmdp, &sc->sc_cmdlen, 0, &dmacl);
464 /* Program the SCSI counter */
465 NCR_WRITE_REG(sc, NCR_TCL, clen);
466 NCR_WRITE_REG(sc, NCR_TCM, clen >> 8);
467 if (sc->sc_cfg2 & NCRCFG2_FE) {
468 NCR_WRITE_REG(sc, NCR_TCH, clen >> 16);
469 }
470
471 /* And get the targets attention */
472 NCRCMD(sc, NCRCMD_SELATN | NCRCMD_DMA);
473 NCRDMA_GO(sc);
474 return;
475 }
476 /*
477 * Who am I. This is where we tell the target that we are
478 * happy for it to disconnect etc.
479 */
480 NCR_WRITE_REG(sc, NCR_FIFO,
481 MSG_IDENTIFY(sc_link->lun, (ti->flags & T_RSELECTOFF)?0:1));
482
483 if (ti->flags & T_NEGOTIATE) {
484 /* Arbitrate, select and stop after IDENTIFY message */
485 NCRCMD(sc, NCRCMD_SELATNS);
486 return;
487 }
488
489 /* Now the command into the FIFO */
490 cmd = (u_char *)&ecb->cmd.cmd;
491 clen = ecb->clen;
492 while (clen--)
493 NCR_WRITE_REG(sc, NCR_FIFO, *cmd++);
494
495 /* And get the targets attention */
496 NCRCMD(sc, NCRCMD_SELATN);
497 }
498
499 void
500 ncr53c9x_free_ecb(sc, ecb, flags)
501 struct ncr53c9x_softc *sc;
502 struct ncr53c9x_ecb *ecb;
503 int flags;
504 {
505 int s;
506
507 s = splbio();
508
509 ecb->flags = 0;
510 TAILQ_INSERT_HEAD(&sc->free_list, ecb, chain);
511
512 /*
513 * If there were none, wake anybody waiting for one to come free,
514 * starting with queued entries.
515 */
516 if (ecb->chain.tqe_next == 0)
517 wakeup(&sc->free_list);
518
519 splx(s);
520 }
521
522 struct ncr53c9x_ecb *
523 ncr53c9x_get_ecb(sc, flags)
524 struct ncr53c9x_softc *sc;
525 int flags;
526 {
527 struct ncr53c9x_ecb *ecb;
528 int s;
529
530 s = splbio();
531
532 while ((ecb = sc->free_list.tqh_first) == NULL &&
533 (flags & SCSI_NOSLEEP) == 0)
534 tsleep(&sc->free_list, PRIBIO, "especb", 0);
535 if (ecb) {
536 TAILQ_REMOVE(&sc->free_list, ecb, chain);
537 ecb->flags |= ECB_ALLOC;
538 }
539
540 splx(s);
541 return ecb;
542 }
543
544 /*
545 * DRIVER FUNCTIONS CALLABLE FROM HIGHER LEVEL DRIVERS
546 */
547
548 /*
549 * Start a SCSI-command
550 * This function is called by the higher level SCSI-driver to queue/run
551 * SCSI-commands.
552 */
553 int
554 ncr53c9x_scsi_cmd(xs)
555 struct scsi_xfer *xs;
556 {
557 struct scsi_link *sc_link = xs->sc_link;
558 struct ncr53c9x_softc *sc = sc_link->adapter_softc;
559 struct ncr53c9x_ecb *ecb;
560 int s, flags;
561
562 NCR_TRACE(("[ncr53c9x_scsi_cmd] "));
563 NCR_CMDS(("[0x%x, %d]->%d ", (int)xs->cmd->opcode, xs->cmdlen,
564 sc_link->target));
565
566 flags = xs->flags;
567 if ((ecb = ncr53c9x_get_ecb(sc, flags)) == NULL) {
568 xs->error = XS_DRIVER_STUFFUP;
569 return TRY_AGAIN_LATER;
570 }
571
572 /* Initialize ecb */
573 ecb->xs = xs;
574 ecb->timeout = xs->timeout;
575
576 if (flags & SCSI_RESET) {
577 ecb->flags |= ECB_RESET;
578 ecb->clen = 0;
579 ecb->dleft = 0;
580 } else {
581 bcopy(xs->cmd, &ecb->cmd.cmd, xs->cmdlen);
582 ecb->clen = xs->cmdlen;
583 ecb->daddr = xs->data;
584 ecb->dleft = xs->datalen;
585 }
586 ecb->stat = 0;
587
588 s = splbio();
589
590 TAILQ_INSERT_TAIL(&sc->ready_list, ecb, chain);
591 if (sc->sc_state == NCR_IDLE)
592 ncr53c9x_sched(sc);
593
594 splx(s);
595
596 if ((flags & SCSI_POLL) == 0)
597 return SUCCESSFULLY_QUEUED;
598
599 /* Not allowed to use interrupts, use polling instead */
600 if (ncr53c9x_poll(sc, xs, ecb->timeout)) {
601 ncr53c9x_timeout(ecb);
602 if (ncr53c9x_poll(sc, xs, ecb->timeout))
603 ncr53c9x_timeout(ecb);
604 }
605 return COMPLETE;
606 }
607
608 /*
609 * Used when interrupt driven I/O isn't allowed, e.g. during boot.
610 */
611 int
612 ncr53c9x_poll(sc, xs, count)
613 struct ncr53c9x_softc *sc;
614 struct scsi_xfer *xs;
615 int count;
616 {
617
618 NCR_TRACE(("[ncr53c9x_poll] "));
619 while (count) {
620 if (NCRDMA_ISINTR(sc)) {
621 ncr53c9x_intr(sc);
622 }
623 #if alternatively
624 if (NCR_READ_REG(sc, NCR_STAT) & NCRSTAT_INT)
625 ncr53c9x_intr(sc);
626 #endif
627 if ((xs->flags & ITSDONE) != 0)
628 return 0;
629 if (sc->sc_state == NCR_IDLE) {
630 NCR_TRACE(("[ncr53c9x_poll: rescheduling] "));
631 ncr53c9x_sched(sc);
632 }
633 DELAY(1000);
634 count--;
635 }
636 return 1;
637 }
638
639
640 /*
641 * LOW LEVEL SCSI UTILITIES
642 */
643
644 /*
645 * Schedule a scsi operation. This has now been pulled out of the interrupt
646 * handler so that we may call it from ncr53c9x_scsi_cmd and ncr53c9x_done.
647 * This may save us an unecessary interrupt just to get things going.
648 * Should only be called when state == NCR_IDLE and at bio pl.
649 */
650 void
651 ncr53c9x_sched(sc)
652 struct ncr53c9x_softc *sc;
653 {
654 struct ncr53c9x_ecb *ecb;
655 struct scsi_link *sc_link;
656 struct ncr53c9x_tinfo *ti;
657
658 NCR_TRACE(("[ncr53c9x_sched] "));
659 if (sc->sc_state != NCR_IDLE)
660 panic("ncr53c9x_sched: not IDLE (state=%d)", sc->sc_state);
661
662 /*
663 * Find first ecb in ready queue that is for a target/lunit
664 * combinations that is not busy.
665 */
666 for (ecb = sc->ready_list.tqh_first; ecb; ecb = ecb->chain.tqe_next) {
667 sc_link = ecb->xs->sc_link;
668 ti = &sc->sc_tinfo[sc_link->target];
669 if ((ti->lubusy & (1 << sc_link->lun)) == 0) {
670 TAILQ_REMOVE(&sc->ready_list, ecb, chain);
671 sc->sc_nexus = ecb;
672 ncr53c9x_select(sc, ecb);
673 break;
674 } else
675 NCR_MISC(("%d:%d busy\n",
676 sc_link->target, sc_link->lun));
677 }
678 }
679
680 void
681 ncr53c9x_sense(sc, ecb)
682 struct ncr53c9x_softc *sc;
683 struct ncr53c9x_ecb *ecb;
684 {
685 struct scsi_xfer *xs = ecb->xs;
686 struct scsi_link *sc_link = xs->sc_link;
687 struct ncr53c9x_tinfo *ti = &sc->sc_tinfo[sc_link->target];
688 struct scsi_sense *ss = (void *)&ecb->cmd.cmd;
689
690 NCR_MISC(("requesting sense "));
691 /* Next, setup a request sense command block */
692 bzero(ss, sizeof(*ss));
693 ss->opcode = REQUEST_SENSE;
694 ss->byte2 = sc_link->lun << 5;
695 ss->length = sizeof(struct scsi_sense_data);
696 ecb->clen = sizeof(*ss);
697 ecb->daddr = (char *)&xs->sense;
698 ecb->dleft = sizeof(struct scsi_sense_data);
699 ecb->flags |= ECB_SENSE;
700 ecb->timeout = NCR_SENSE_TIMEOUT;
701 ti->senses++;
702 if (ecb->flags & ECB_NEXUS)
703 ti->lubusy &= ~(1 << sc_link->lun);
704 if (ecb == sc->sc_nexus) {
705 ncr53c9x_select(sc, ecb);
706 } else {
707 ncr53c9x_dequeue(sc, ecb);
708 TAILQ_INSERT_HEAD(&sc->ready_list, ecb, chain);
709 if (sc->sc_state == NCR_IDLE)
710 ncr53c9x_sched(sc);
711 }
712 }
713
714 /*
715 * POST PROCESSING OF SCSI_CMD (usually current)
716 */
717 void
718 ncr53c9x_done(sc, ecb)
719 struct ncr53c9x_softc *sc;
720 struct ncr53c9x_ecb *ecb;
721 {
722 struct scsi_xfer *xs = ecb->xs;
723 struct scsi_link *sc_link = xs->sc_link;
724 struct ncr53c9x_tinfo *ti = &sc->sc_tinfo[sc_link->target];
725
726 NCR_TRACE(("[ncr53c9x_done(error:%x)] ", xs->error));
727
728 untimeout(ncr53c9x_timeout, ecb);
729
730 /*
731 * Now, if we've come here with no error code, i.e. we've kept the
732 * initial XS_NOERROR, and the status code signals that we should
733 * check sense, we'll need to set up a request sense cmd block and
734 * push the command back into the ready queue *before* any other
735 * commands for this target/lunit, else we lose the sense info.
736 * We don't support chk sense conditions for the request sense cmd.
737 */
738 if (xs->error == XS_NOERROR) {
739 xs->status = ecb->stat;
740 if ((ecb->flags & ECB_ABORT) != 0) {
741 xs->error = XS_DRIVER_STUFFUP;
742 } else if ((ecb->flags & ECB_SENSE) != 0) {
743 xs->error = XS_SENSE;
744 } else if ((ecb->stat & ST_MASK) == SCSI_CHECK) {
745 /* First, save the return values */
746 xs->resid = ecb->dleft;
747 ncr53c9x_sense(sc, ecb);
748 return;
749 } else {
750 xs->resid = ecb->dleft;
751 }
752 }
753
754 xs->flags |= ITSDONE;
755
756 #ifdef NCR53C9X_DEBUG
757 if (ncr53c9x_debug & NCR_SHOWMISC) {
758 if (xs->resid != 0)
759 printf("resid=%d ", xs->resid);
760 if (xs->error == XS_SENSE)
761 printf("sense=0x%02x\n", xs->sense.error_code);
762 else
763 printf("error=%d\n", xs->error);
764 }
765 #endif
766
767 /*
768 * Remove the ECB from whatever queue it's on.
769 */
770 if (ecb->flags & ECB_NEXUS)
771 ti->lubusy &= ~(1 << sc_link->lun);
772 if (ecb == sc->sc_nexus) {
773 sc->sc_nexus = NULL;
774 sc->sc_state = NCR_IDLE;
775 ncr53c9x_sched(sc);
776 } else
777 ncr53c9x_dequeue(sc, ecb);
778
779 ncr53c9x_free_ecb(sc, ecb, xs->flags);
780 ti->cmds++;
781 scsi_done(xs);
782 }
783
784 void
785 ncr53c9x_dequeue(sc, ecb)
786 struct ncr53c9x_softc *sc;
787 struct ncr53c9x_ecb *ecb;
788 {
789
790 if (ecb->flags & ECB_NEXUS) {
791 TAILQ_REMOVE(&sc->nexus_list, ecb, chain);
792 } else {
793 TAILQ_REMOVE(&sc->ready_list, ecb, chain);
794 }
795 }
796
797 /*
798 * INTERRUPT/PROTOCOL ENGINE
799 */
800
801 /*
802 * Schedule an outgoing message by prioritizing it, and asserting
803 * attention on the bus. We can only do this when we are the initiator
804 * else there will be an illegal command interrupt.
805 */
806 #define ncr53c9x_sched_msgout(m) \
807 do { \
808 NCR_MISC(("ncr53c9x_sched_msgout %d ", m)); \
809 NCRCMD(sc, NCRCMD_SETATN); \
810 sc->sc_flags |= NCR_ATN; \
811 sc->sc_msgpriq |= (m); \
812 } while (0)
813
814 int
815 ncr53c9x_reselect(sc, message)
816 struct ncr53c9x_softc *sc;
817 int message;
818 {
819 u_char selid, target, lun;
820 struct ncr53c9x_ecb *ecb;
821 struct scsi_link *sc_link;
822 struct ncr53c9x_tinfo *ti;
823
824 /*
825 * The SCSI chip made a snapshot of the data bus while the reselection
826 * was being negotiated. This enables us to determine which target did
827 * the reselect.
828 */
829 selid = sc->sc_selid & ~(1 << sc->sc_id);
830 if (selid & (selid - 1)) {
831 printf("%s: reselect with invalid selid %02x;"
832 " sending DEVICE RESET\n", sc->sc_dev.dv_xname, selid);
833 goto reset;
834 }
835
836 /*
837 * Search wait queue for disconnected cmd
838 * The list should be short, so I haven't bothered with
839 * any more sophisticated structures than a simple
840 * singly linked list.
841 */
842 target = ffs(selid) - 1;
843 lun = message & 0x07;
844 for (ecb = sc->nexus_list.tqh_first; ecb != NULL;
845 ecb = ecb->chain.tqe_next) {
846 sc_link = ecb->xs->sc_link;
847 if (sc_link->target == target && sc_link->lun == lun)
848 break;
849 }
850 if (ecb == NULL) {
851 printf("%s: reselect from target %d lun %d with no nexus;"
852 " sending ABORT\n", sc->sc_dev.dv_xname, target, lun);
853 goto abort;
854 }
855
856 /* Make this nexus active again. */
857 TAILQ_REMOVE(&sc->nexus_list, ecb, chain);
858 sc->sc_state = NCR_CONNECTED;
859 sc->sc_nexus = ecb;
860 ti = &sc->sc_tinfo[target];
861 ti->lubusy |= (1 << lun);
862 ncr53c9x_setsync(sc, ti);
863
864 if (ecb->flags & ECB_RESET)
865 ncr53c9x_sched_msgout(SEND_DEV_RESET);
866 else if (ecb->flags & ECB_ABORT)
867 ncr53c9x_sched_msgout(SEND_ABORT);
868
869 /* Do an implicit RESTORE POINTERS. */
870 sc->sc_dp = ecb->daddr;
871 sc->sc_dleft = ecb->dleft;
872
873 return (0);
874
875 reset:
876 ncr53c9x_sched_msgout(SEND_DEV_RESET);
877 return (1);
878
879 abort:
880 ncr53c9x_sched_msgout(SEND_ABORT);
881 return (1);
882 }
883
884 #define IS1BYTEMSG(m) (((m) != 1 && (m) < 0x20) || (m) & 0x80)
885 #define IS2BYTEMSG(m) (((m) & 0xf0) == 0x20)
886 #define ISEXTMSG(m) ((m) == 1)
887
888 /*
889 * Get an incoming message as initiator.
890 *
891 * The SCSI bus must already be in MESSAGE_IN_PHASE and there is a
892 * byte in the FIFO
893 */
894 void
895 ncr53c9x_msgin(sc)
896 register struct ncr53c9x_softc *sc;
897 {
898 register int v;
899
900 NCR_TRACE(("[ncr53c9x_msgin(curmsglen:%ld)] ", (long)sc->sc_imlen));
901
902 if ((NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF) == 0) {
903 printf("%s: msgin: no msg byte available\n",
904 sc->sc_dev.dv_xname);
905 return;
906 }
907
908 /*
909 * Prepare for a new message. A message should (according
910 * to the SCSI standard) be transmitted in one single
911 * MESSAGE_IN_PHASE. If we have been in some other phase,
912 * then this is a new message.
913 */
914 if (sc->sc_prevphase != MESSAGE_IN_PHASE) {
915 sc->sc_flags &= ~NCR_DROP_MSGI;
916 sc->sc_imlen = 0;
917 }
918
919 v = NCR_READ_REG(sc, NCR_FIFO);
920 NCR_MISC(("<msgbyte:0x%02x>", v));
921
922 #if 0
923 if (sc->sc_state == NCR_RESELECTED && sc->sc_imlen == 0) {
924 /*
925 * Which target is reselecting us? (The ID bit really)
926 */
927 sc->sc_selid = v;
928 NCR_MISC(("selid=0x%2x ", sc->sc_selid));
929 return;
930 }
931 #endif
932
933 sc->sc_imess[sc->sc_imlen] = v;
934
935 /*
936 * If we're going to reject the message, don't bother storing
937 * the incoming bytes. But still, we need to ACK them.
938 */
939
940 if ((sc->sc_flags & NCR_DROP_MSGI)) {
941 NCRCMD(sc, NCRCMD_MSGOK);
942 printf("<dropping msg byte %x>",
943 sc->sc_imess[sc->sc_imlen]);
944 return;
945 }
946
947 if (sc->sc_imlen >= NCR_MAX_MSG_LEN) {
948 ncr53c9x_sched_msgout(SEND_REJECT);
949 sc->sc_flags |= NCR_DROP_MSGI;
950 } else {
951 sc->sc_imlen++;
952 /*
953 * This testing is suboptimal, but most
954 * messages will be of the one byte variety, so
955 * it should not effect performance
956 * significantly.
957 */
958 if (sc->sc_imlen == 1 && IS1BYTEMSG(sc->sc_imess[0]))
959 goto gotit;
960 if (sc->sc_imlen == 2 && IS2BYTEMSG(sc->sc_imess[0]))
961 goto gotit;
962 if (sc->sc_imlen >= 3 && ISEXTMSG(sc->sc_imess[0]) &&
963 sc->sc_imlen == sc->sc_imess[1] + 2)
964 goto gotit;
965 }
966 /* Ack what we have so far */
967 NCRCMD(sc, NCRCMD_MSGOK);
968 return;
969
970 gotit:
971 NCR_MSGS(("gotmsg(%x)", sc->sc_imess[0]));
972 /*
973 * Now we should have a complete message (1 byte, 2 byte
974 * and moderately long extended messages). We only handle
975 * extended messages which total length is shorter than
976 * NCR_MAX_MSG_LEN. Longer messages will be amputated.
977 */
978 switch (sc->sc_state) {
979 struct ncr53c9x_ecb *ecb;
980 struct ncr53c9x_tinfo *ti;
981
982 case NCR_CONNECTED:
983 ecb = sc->sc_nexus;
984 ti = &sc->sc_tinfo[ecb->xs->sc_link->target];
985
986 switch (sc->sc_imess[0]) {
987 case MSG_CMDCOMPLETE:
988 NCR_MSGS(("cmdcomplete "));
989 if (sc->sc_dleft < 0) {
990 struct scsi_link *sc_link = ecb->xs->sc_link;
991 printf("%s: %ld extra bytes from %d:%d\n",
992 sc->sc_dev.dv_xname, -(long)sc->sc_dleft,
993 sc_link->target, sc_link->lun);
994 sc->sc_dleft = 0;
995 }
996 ecb->dleft = (ecb->flags & ECB_TENTATIVE_DONE)
997 ? 0
998 : sc->sc_dleft;
999 if ((ecb->flags & ECB_SENSE) == 0)
1000 ecb->xs->resid = ecb->dleft;
1001 sc->sc_state = NCR_CMDCOMPLETE;
1002 break;
1003
1004 case MSG_MESSAGE_REJECT:
1005 if (ncr53c9x_debug & NCR_SHOWMSGS)
1006 printf("%s: our msg rejected by target\n",
1007 sc->sc_dev.dv_xname);
1008 switch (sc->sc_msgout) {
1009 case SEND_SDTR:
1010 sc->sc_flags &= ~NCR_SYNCHNEGO;
1011 ti->flags &= ~(T_NEGOTIATE | T_SYNCMODE);
1012 ncr53c9x_setsync(sc, ti);
1013 break;
1014 case SEND_INIT_DET_ERR:
1015 goto abort;
1016 }
1017 break;
1018
1019 case MSG_NOOP:
1020 NCR_MSGS(("noop "));
1021 break;
1022
1023 case MSG_DISCONNECT:
1024 NCR_MSGS(("disconnect "));
1025 ti->dconns++;
1026 sc->sc_state = NCR_DISCONNECT;
1027
1028 /*
1029 * Mark the fact that all bytes have moved. The
1030 * target may not bother to do a SAVE POINTERS
1031 * at this stage. This flag will set the residual
1032 * count to zero on MSG COMPLETE.
1033 */
1034 if (sc->sc_dleft == 0)
1035 ecb->flags |= ECB_TENTATIVE_DONE;
1036
1037 break;
1038
1039 case MSG_SAVEDATAPOINTER:
1040 NCR_MSGS(("save datapointer "));
1041 ecb->daddr = sc->sc_dp;
1042 ecb->dleft = sc->sc_dleft;
1043 break;
1044
1045 case MSG_RESTOREPOINTERS:
1046 NCR_MSGS(("restore datapointer "));
1047 sc->sc_dp = ecb->daddr;
1048 sc->sc_dleft = ecb->dleft;
1049 break;
1050
1051 case MSG_EXTENDED:
1052 NCR_MSGS(("extended(%x) ", sc->sc_imess[2]));
1053 switch (sc->sc_imess[2]) {
1054 case MSG_EXT_SDTR:
1055 NCR_MSGS(("SDTR period %d, offset %d ",
1056 sc->sc_imess[3], sc->sc_imess[4]));
1057 if (sc->sc_imess[1] != 3)
1058 goto reject;
1059 ti->period = sc->sc_imess[3];
1060 ti->offset = sc->sc_imess[4];
1061 ti->flags &= ~T_NEGOTIATE;
1062 if (sc->sc_minsync == 0 ||
1063 ti->offset == 0 ||
1064 ti->period > 124) {
1065 printf("%s:%d: async\n", "esp",
1066 ecb->xs->sc_link->target);
1067 if ((sc->sc_flags&NCR_SYNCHNEGO)
1068 == 0) {
1069 /*
1070 * target initiated negotiation
1071 */
1072 ti->offset = 0;
1073 ti->flags &= ~T_SYNCMODE;
1074 ncr53c9x_sched_msgout(
1075 SEND_SDTR);
1076 } else {
1077 /* we are async */
1078 ti->flags &= ~T_SYNCMODE;
1079 }
1080 } else {
1081 int r = 250/ti->period;
1082 int s = (100*250)/ti->period - 100*r;
1083 int p;
1084
1085 p = ncr53c9x_stp2cpb(sc, ti->period);
1086 ti->period = ncr53c9x_cpb2stp(sc, p);
1087 #ifdef NCR53C9X_DEBUG
1088 sc_print_addr(ecb->xs->sc_link);
1089 printf("max sync rate %d.%02dMb/s\n",
1090 r, s);
1091 #endif
1092 if ((sc->sc_flags&NCR_SYNCHNEGO)
1093 == 0) {
1094 /*
1095 * target initiated negotiation
1096 */
1097 if (ti->period <
1098 sc->sc_minsync)
1099 ti->period =
1100 sc->sc_minsync;
1101 if (ti->offset > 15)
1102 ti->offset = 15;
1103 ti->flags &= ~T_SYNCMODE;
1104 ncr53c9x_sched_msgout(
1105 SEND_SDTR);
1106 } else {
1107 /* we are sync */
1108 ti->flags |= T_SYNCMODE;
1109 }
1110 }
1111 sc->sc_flags &= ~NCR_SYNCHNEGO;
1112 ncr53c9x_setsync(sc, ti);
1113 break;
1114
1115 default:
1116 printf("%s: unrecognized MESSAGE EXTENDED;"
1117 " sending REJECT\n", sc->sc_dev.dv_xname);
1118 goto reject;
1119 }
1120 break;
1121
1122 default:
1123 NCR_MSGS(("ident "));
1124 printf("%s: unrecognized MESSAGE; sending REJECT\n",
1125 sc->sc_dev.dv_xname);
1126 reject:
1127 ncr53c9x_sched_msgout(SEND_REJECT);
1128 break;
1129 }
1130 break;
1131
1132 case NCR_RESELECTED:
1133 if (!MSG_ISIDENTIFY(sc->sc_imess[0])) {
1134 printf("%s: reselect without IDENTIFY;"
1135 " sending DEVICE RESET\n", sc->sc_dev.dv_xname);
1136 goto reset;
1137 }
1138
1139 (void) ncr53c9x_reselect(sc, sc->sc_imess[0]);
1140 break;
1141
1142 default:
1143 printf("%s: unexpected MESSAGE IN; sending DEVICE RESET\n",
1144 sc->sc_dev.dv_xname);
1145 reset:
1146 ncr53c9x_sched_msgout(SEND_DEV_RESET);
1147 break;
1148
1149 abort:
1150 ncr53c9x_sched_msgout(SEND_ABORT);
1151 break;
1152 }
1153
1154 /* Ack last message byte */
1155 NCRCMD(sc, NCRCMD_MSGOK);
1156
1157 /* Done, reset message pointer. */
1158 sc->sc_flags &= ~NCR_DROP_MSGI;
1159 sc->sc_imlen = 0;
1160 }
1161
1162
1163 /*
1164 * Send the highest priority, scheduled message
1165 */
1166 void
1167 ncr53c9x_msgout(sc)
1168 register struct ncr53c9x_softc *sc;
1169 {
1170 struct ncr53c9x_tinfo *ti;
1171 struct ncr53c9x_ecb *ecb;
1172 size_t size;
1173
1174 NCR_TRACE(("[ncr53c9x_msgout(priq:%x, prevphase:%x)]",
1175 sc->sc_msgpriq, sc->sc_prevphase));
1176
1177 if (sc->sc_flags & NCR_ATN) {
1178 if (sc->sc_prevphase != MESSAGE_OUT_PHASE) {
1179 new:
1180 NCRCMD(sc, NCRCMD_FLUSH);
1181 DELAY(1);
1182 sc->sc_msgoutq = 0;
1183 sc->sc_omlen = 0;
1184 }
1185 } else {
1186 if (sc->sc_prevphase == MESSAGE_OUT_PHASE) {
1187 ncr53c9x_sched_msgout(sc->sc_msgoutq);
1188 goto new;
1189 } else {
1190 printf("%s at line %d: unexpected MESSAGE OUT phase\n",
1191 sc->sc_dev.dv_xname, __LINE__);
1192 }
1193 }
1194
1195 if (sc->sc_omlen == 0) {
1196 /* Pick up highest priority message */
1197 sc->sc_msgout = sc->sc_msgpriq & -sc->sc_msgpriq;
1198 sc->sc_msgoutq |= sc->sc_msgout;
1199 sc->sc_msgpriq &= ~sc->sc_msgout;
1200 sc->sc_omlen = 1; /* "Default" message len */
1201 switch (sc->sc_msgout) {
1202 case SEND_SDTR:
1203 ecb = sc->sc_nexus;
1204 ti = &sc->sc_tinfo[ecb->xs->sc_link->target];
1205 sc->sc_omess[0] = MSG_EXTENDED;
1206 sc->sc_omess[1] = 3;
1207 sc->sc_omess[2] = MSG_EXT_SDTR;
1208 sc->sc_omess[3] = ti->period;
1209 sc->sc_omess[4] = ti->offset;
1210 sc->sc_omlen = 5;
1211 if ((sc->sc_flags & NCR_SYNCHNEGO) == 0) {
1212 ti->flags |= T_SYNCMODE;
1213 ncr53c9x_setsync(sc, ti);
1214 }
1215 break;
1216 case SEND_IDENTIFY:
1217 if (sc->sc_state != NCR_CONNECTED) {
1218 printf("%s at line %d: no nexus\n",
1219 sc->sc_dev.dv_xname, __LINE__);
1220 }
1221 ecb = sc->sc_nexus;
1222 sc->sc_omess[0] =
1223 MSG_IDENTIFY(ecb->xs->sc_link->lun, 0);
1224 break;
1225 case SEND_DEV_RESET:
1226 sc->sc_flags |= NCR_ABORTING;
1227 sc->sc_omess[0] = MSG_BUS_DEV_RESET;
1228 ecb = sc->sc_nexus;
1229 ti = &sc->sc_tinfo[ecb->xs->sc_link->target];
1230 ti->flags &= ~T_SYNCMODE;
1231 ti->flags |= T_NEGOTIATE;
1232 break;
1233 case SEND_PARITY_ERROR:
1234 sc->sc_omess[0] = MSG_PARITY_ERROR;
1235 break;
1236 case SEND_ABORT:
1237 sc->sc_flags |= NCR_ABORTING;
1238 sc->sc_omess[0] = MSG_ABORT;
1239 break;
1240 case SEND_INIT_DET_ERR:
1241 sc->sc_omess[0] = MSG_INITIATOR_DET_ERR;
1242 break;
1243 case SEND_REJECT:
1244 sc->sc_omess[0] = MSG_MESSAGE_REJECT;
1245 break;
1246 default:
1247 NCRCMD(sc, NCRCMD_RSTATN);
1248 sc->sc_flags &= ~NCR_ATN;
1249 sc->sc_omess[0] = MSG_NOOP;
1250 break;
1251 }
1252 sc->sc_omp = sc->sc_omess;
1253 }
1254
1255 #if 1
1256 /* (re)send the message */
1257 size = min(sc->sc_omlen, sc->sc_maxxfer);
1258 NCRDMA_SETUP(sc, &sc->sc_omp, &sc->sc_omlen, 0, &size);
1259 /* Program the SCSI counter */
1260 NCR_WRITE_REG(sc, NCR_TCL, size);
1261 NCR_WRITE_REG(sc, NCR_TCM, size >> 8);
1262 if (sc->sc_cfg2 & NCRCFG2_FE) {
1263 NCR_WRITE_REG(sc, NCR_TCH, size >> 16);
1264 }
1265 /* load the count in */
1266 NCRCMD(sc, NCRCMD_NOP|NCRCMD_DMA);
1267 NCRCMD(sc, NCRCMD_TRANS|NCRCMD_DMA);
1268 NCRDMA_GO(sc);
1269 #else
1270 { int i;
1271 for (i = 0; i < sc->sc_omlen; i++)
1272 NCR_WRITE_REG(sc, FIFO, sc->sc_omess[i]);
1273 NCRCMD(sc, NCRCMD_TRANS);
1274 sc->sc_omlen = 0;
1275 }
1276 #endif
1277 }
1278
1279 /*
1280 * This is the most critical part of the driver, and has to know
1281 * how to deal with *all* error conditions and phases from the SCSI
1282 * bus. If there are no errors and the DMA was active, then call the
1283 * DMA pseudo-interrupt handler. If this returns 1, then that was it
1284 * and we can return from here without further processing.
1285 *
1286 * Most of this needs verifying.
1287 */
1288 int
1289 ncr53c9x_intr(sc)
1290 register struct ncr53c9x_softc *sc;
1291 {
1292 register struct ncr53c9x_ecb *ecb;
1293 register struct scsi_link *sc_link;
1294 struct ncr53c9x_tinfo *ti;
1295 int loop;
1296 size_t size;
1297 int nfifo;
1298
1299 NCR_TRACE(("[ncr53c9x_intr] "));
1300
1301 /*
1302 * I have made some (maybe seriously flawed) assumptions here,
1303 * but basic testing (uncomment the printf() below), show that
1304 * certainly something happens when this loop is here.
1305 *
1306 * The idea is that many of the SCSI operations take very little
1307 * time, and going away and getting interrupted is too high an
1308 * overhead to pay. For example, selecting, sending a message
1309 * and command and then doing some work can be done in one "pass".
1310 *
1311 * The DELAY is not variable because I do not understand that the
1312 * DELAY loop should be fixed-time regardless of CPU speed, but
1313 * I am *assuming* that the faster SCSI processors get things done
1314 * quicker (sending a command byte etc), and so there is no
1315 * need to be too slow.
1316 *
1317 * This is a heuristic. It is 2 when at 20Mhz, 2 at 25Mhz and 1
1318 * at 40Mhz. This needs testing.
1319 */
1320 for (loop = 0; 1;loop++, DELAY(50/sc->sc_freq)) {
1321 /* a feeling of deja-vu */
1322 if (!NCRDMA_ISINTR(sc))
1323 return (loop != 0);
1324 #if 0
1325 if (loop)
1326 printf("*");
1327 #endif
1328
1329 /* and what do the registers say... */
1330 ncr53c9x_readregs(sc);
1331
1332 sc->sc_intrcnt.ev_count++;
1333
1334 /*
1335 * At the moment, only a SCSI Bus Reset or Illegal
1336 * Command are classed as errors. A disconnect is a
1337 * valid condition, and we let the code check is the
1338 * "NCR_BUSFREE_OK" flag was set before declaring it
1339 * and error.
1340 *
1341 * Also, the status register tells us about "Gross
1342 * Errors" and "Parity errors". Only the Gross Error
1343 * is really bad, and the parity errors are dealt
1344 * with later
1345 *
1346 * TODO
1347 * If there are too many parity error, go to slow
1348 * cable mode ?
1349 */
1350
1351 /* SCSI Reset */
1352 if (sc->sc_espintr & NCRINTR_SBR) {
1353 if (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF) {
1354 NCRCMD(sc, NCRCMD_FLUSH);
1355 DELAY(1);
1356 }
1357 if (sc->sc_state != NCR_SBR) {
1358 printf("%s: SCSI bus reset\n",
1359 sc->sc_dev.dv_xname);
1360 ncr53c9x_init(sc, 0); /* Restart everything */
1361 return 1;
1362 }
1363 #if 0
1364 /*XXX*/ printf("<expected bus reset: "
1365 "[intr %x, stat %x, step %d]>\n",
1366 sc->sc_espintr, sc->sc_espstat,
1367 sc->sc_espstep);
1368 #endif
1369 if (sc->sc_nexus)
1370 panic("%s: nexus in reset state",
1371 sc->sc_dev.dv_xname);
1372 goto sched;
1373 }
1374
1375 ecb = sc->sc_nexus;
1376
1377 #define NCRINTR_ERR (NCRINTR_SBR|NCRINTR_ILL)
1378 if (sc->sc_espintr & NCRINTR_ERR ||
1379 sc->sc_espstat & NCRSTAT_GE) {
1380
1381 if (sc->sc_espstat & NCRSTAT_GE) {
1382 /* no target ? */
1383 if (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF) {
1384 NCRCMD(sc, NCRCMD_FLUSH);
1385 DELAY(1);
1386 }
1387 if (sc->sc_state == NCR_CONNECTED ||
1388 sc->sc_state == NCR_SELECTING) {
1389 ecb->xs->error = XS_DRIVER_STUFFUP;
1390 ncr53c9x_done(sc, ecb);
1391 }
1392 return 1;
1393 }
1394
1395 if (sc->sc_espintr & NCRINTR_ILL) {
1396 if (sc->sc_flags & NCR_EXPECT_ILLCMD) {
1397 printf("%s: ILL: ESP100 work-around activated\n", sc->sc_dev.dv_xname);
1398 sc->sc_flags &= ~NCR_EXPECT_ILLCMD;
1399 continue;
1400 }
1401 /* illegal command, out of sync ? */
1402 printf("%s: illegal command: 0x%x "
1403 "(state %d, phase %x, prevphase %x)\n",
1404 sc->sc_dev.dv_xname, sc->sc_lastcmd,
1405 sc->sc_state, sc->sc_phase,
1406 sc->sc_prevphase);
1407 if (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF) {
1408 NCRCMD(sc, NCRCMD_FLUSH);
1409 DELAY(1);
1410 }
1411 ncr53c9x_init(sc, 1); /* Restart everything */
1412 return 1;
1413 }
1414 }
1415 sc->sc_flags &= ~NCR_EXPECT_ILLCMD;
1416
1417 /*
1418 * Call if DMA is active.
1419 *
1420 * If DMA_INTR returns true, then maybe go 'round the loop
1421 * again in case there is no more DMA queued, but a phase
1422 * change is expected.
1423 */
1424 if (NCRDMA_ISACTIVE(sc)) {
1425 int r = NCRDMA_INTR(sc);
1426 if (r == -1) {
1427 printf("%s: DMA error; resetting\n",
1428 sc->sc_dev.dv_xname);
1429 ncr53c9x_init(sc, 1);
1430 }
1431 /* If DMA active here, then go back to work... */
1432 if (NCRDMA_ISACTIVE(sc))
1433 return 1;
1434
1435 /*
1436 * Note that this can happen during normal operation
1437 * if we are reselected while using DMA to select
1438 * a target. If this is the case, don't issue the
1439 * warning.
1440 */
1441 if (sc->sc_dleft == 0 &&
1442 (sc->sc_espstat & NCRSTAT_TC) == 0 &&
1443 sc->sc_state != NCR_SELECTING)
1444 printf("%s: !TC [intr %x, stat %x, step %d]"
1445 " prevphase %x, resid %x\n",
1446 sc->sc_dev.dv_xname,
1447 sc->sc_espintr,
1448 sc->sc_espstat,
1449 sc->sc_espstep,
1450 sc->sc_prevphase,
1451 ecb?ecb->dleft:-1);
1452 }
1453
1454 #if 0 /* Unreliable on some NCR revisions? */
1455 if ((sc->sc_espstat & NCRSTAT_INT) == 0) {
1456 printf("%s: spurious interrupt\n",
1457 sc->sc_dev.dv_xname);
1458 return 1;
1459 }
1460 #endif
1461
1462 /*
1463 * check for less serious errors
1464 */
1465 if (sc->sc_espstat & NCRSTAT_PE) {
1466 printf("%s: SCSI bus parity error\n",
1467 sc->sc_dev.dv_xname);
1468 if (sc->sc_prevphase == MESSAGE_IN_PHASE)
1469 ncr53c9x_sched_msgout(SEND_PARITY_ERROR);
1470 else
1471 ncr53c9x_sched_msgout(SEND_INIT_DET_ERR);
1472 }
1473
1474 if (sc->sc_espintr & NCRINTR_DIS) {
1475 NCR_MISC(("<DISC [intr %x, stat %x, step %d]>",
1476 sc->sc_espintr,sc->sc_espstat,sc->sc_espstep));
1477 if (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF) {
1478 NCRCMD(sc, NCRCMD_FLUSH);
1479 DELAY(1);
1480 }
1481 /*
1482 * This command must (apparently) be issued within
1483 * 250mS of a disconnect. So here you are...
1484 */
1485 NCRCMD(sc, NCRCMD_ENSEL);
1486 switch (sc->sc_state) {
1487 case NCR_RESELECTED:
1488 goto sched;
1489
1490 case NCR_SELECTING:
1491 ecb->xs->error = XS_SELTIMEOUT;
1492 goto finish;
1493
1494 case NCR_CONNECTED:
1495 if ((sc->sc_flags & NCR_SYNCHNEGO)) {
1496 #ifdef NCR53C9X_DEBUG
1497 if (ecb)
1498 sc_print_addr(ecb->xs->sc_link);
1499 printf("sync nego not completed!\n");
1500 #endif
1501 ti = &sc->sc_tinfo[ecb->xs->sc_link->target];
1502 sc->sc_flags &= ~NCR_SYNCHNEGO;
1503 ti->flags &=
1504 ~(T_NEGOTIATE | T_SYNCMODE);
1505 }
1506
1507 /* it may be OK to disconnect */
1508 if ((sc->sc_flags & NCR_ABORTING) == 0) {
1509 /*
1510 * Section 5.1.1 of the SCSI 2 spec
1511 * suggests issuing a REQUEST SENSE
1512 * following an unexpected disconnect.
1513 * Some devices go into a contingent
1514 * allegiance condition when
1515 * disconnecting, and this is necessary
1516 * to clean up their state.
1517 */
1518 printf("%s: unexpected disconnect; ",
1519 sc->sc_dev.dv_xname);
1520 if (ecb->flags & ECB_SENSE) {
1521 printf("resetting\n");
1522 goto reset;
1523 }
1524 printf("sending REQUEST SENSE\n");
1525 untimeout(ncr53c9x_timeout, ecb);
1526 ncr53c9x_sense(sc, ecb);
1527 goto out;
1528 }
1529
1530 ecb->xs->error = XS_DRIVER_STUFFUP;
1531 goto finish;
1532
1533 case NCR_DISCONNECT:
1534 TAILQ_INSERT_HEAD(&sc->nexus_list, ecb, chain);
1535 sc->sc_nexus = NULL;
1536 goto sched;
1537
1538 case NCR_CMDCOMPLETE:
1539 goto finish;
1540 }
1541 }
1542
1543 switch (sc->sc_state) {
1544
1545 case NCR_SBR:
1546 printf("%s: waiting for SCSI Bus Reset to happen\n",
1547 sc->sc_dev.dv_xname);
1548 return 1;
1549
1550 case NCR_RESELECTED:
1551 /*
1552 * we must be continuing a message ?
1553 */
1554 if (sc->sc_phase != MESSAGE_IN_PHASE) {
1555 printf("%s: target didn't identify\n",
1556 sc->sc_dev.dv_xname);
1557 ncr53c9x_init(sc, 1);
1558 return 1;
1559 }
1560 printf("<<RESELECT CONT'd>>");
1561 #if XXXX
1562 ncr53c9x_msgin(sc);
1563 if (sc->sc_state != NCR_CONNECTED) {
1564 /* IDENTIFY fail?! */
1565 printf("%s: identify failed\n",
1566 sc->sc_dev.dv_xname);
1567 ncr53c9x_init(sc, 1);
1568 return 1;
1569 }
1570 #endif
1571 break;
1572
1573 case NCR_IDLE:
1574 if (sc->sc_flags & NCR_ICCS) printf("[[esp: BUMMER]]");
1575 case NCR_SELECTING:
1576 sc->sc_msgpriq = sc->sc_msgout = sc->sc_msgoutq = 0;
1577 sc->sc_flags = 0;
1578 ecb = sc->sc_nexus;
1579
1580 if (sc->sc_espintr & NCRINTR_RESEL) {
1581 /*
1582 * If we're trying to select a
1583 * target ourselves, push our command
1584 * back into the ready list.
1585 */
1586 if (sc->sc_state == NCR_SELECTING) {
1587 NCR_MISC(("backoff selector "));
1588 untimeout(ncr53c9x_timeout, ecb);
1589 sc_link = ecb->xs->sc_link;
1590 ti = &sc->sc_tinfo[sc_link->target];
1591 TAILQ_INSERT_HEAD(&sc->ready_list,
1592 ecb, chain);
1593 ecb = sc->sc_nexus = NULL;
1594 }
1595 sc->sc_state = NCR_RESELECTED;
1596 if (sc->sc_phase != MESSAGE_IN_PHASE) {
1597 /*
1598 * Things are seriously fucked up.
1599 * Pull the brakes, i.e. reset
1600 */
1601 printf("%s: target didn't identify\n",
1602 sc->sc_dev.dv_xname);
1603 ncr53c9x_init(sc, 1);
1604 return 1;
1605 }
1606 /*
1607 * The C90 only inhibits FIFO writes until
1608 * reselection is complete, instead of
1609 * waiting until the interrupt status register
1610 * has been read. So, if the reselect happens
1611 * while we were entering a command bytes (for
1612 * another target) some of those bytes can
1613 * appear in the FIFO here, after the
1614 * interrupt is taken.
1615 */
1616 nfifo = NCR_READ_REG(sc,NCR_FFLAG) & NCRFIFO_FF;
1617 if (nfifo < 2 ||
1618 (nfifo > 2 &&
1619 sc->sc_rev != NCR_VARIANT_ESP100)) {
1620 printf("%s: RESELECT: "
1621 "%d bytes in FIFO! "
1622 "[intr %x, stat %x, step %d, prevphase %x]\n",
1623 sc->sc_dev.dv_xname,
1624 nfifo,
1625 sc->sc_espintr,
1626 sc->sc_espstat,
1627 sc->sc_espstep,
1628 sc->sc_prevphase);
1629 ncr53c9x_init(sc, 1);
1630 return 1;
1631 }
1632 sc->sc_selid = NCR_READ_REG(sc, NCR_FIFO);
1633 NCR_MISC(("selid=0x%2x ", sc->sc_selid));
1634
1635 /* Handle identify message */
1636 ncr53c9x_msgin(sc);
1637 if (nfifo != 2) {
1638 sc->sc_flags |= NCR_EXPECT_ILLCMD;
1639 NCRCMD(sc, NCRCMD_FLUSH);
1640 }
1641
1642 if (sc->sc_state != NCR_CONNECTED) {
1643 /* IDENTIFY fail?! */
1644 printf("%s: identify failed\n",
1645 sc->sc_dev.dv_xname);
1646 ncr53c9x_init(sc, 1);
1647 return 1;
1648 }
1649 continue; /* ie. next phase expected soon */
1650 }
1651
1652 #define NCRINTR_DONE (NCRINTR_FC|NCRINTR_BS)
1653 if ((sc->sc_espintr & NCRINTR_DONE) == NCRINTR_DONE) {
1654 ecb = sc->sc_nexus;
1655 if (!ecb)
1656 panic("esp: not nexus at sc->sc_nexus");
1657
1658 sc_link = ecb->xs->sc_link;
1659 ti = &sc->sc_tinfo[sc_link->target];
1660
1661 switch (sc->sc_espstep) {
1662 case 0:
1663 printf("%s: select timeout/no "
1664 "disconnect\n",
1665 sc->sc_dev.dv_xname);
1666 ecb->xs->error = XS_SELTIMEOUT;
1667 goto finish;
1668 case 1:
1669 if ((ti->flags & T_NEGOTIATE) == 0) {
1670 printf("%s: step 1 & !NEG\n",
1671 sc->sc_dev.dv_xname);
1672 goto reset;
1673 }
1674 if (sc->sc_phase != MESSAGE_OUT_PHASE) {
1675 printf("%s: !MSGOUT\n",
1676 sc->sc_dev.dv_xname);
1677 goto reset;
1678 }
1679 /* Start negotiating */
1680 ti->period = sc->sc_minsync;
1681 ti->offset = 15;
1682 sc->sc_flags |= NCR_SYNCHNEGO;
1683 ncr53c9x_sched_msgout(SEND_SDTR);
1684 break;
1685 case 3:
1686 /*
1687 * Grr, this is supposed to mean
1688 * "target left command phase
1689 * prematurely". It seems to happen
1690 * regularly when sync mode is on.
1691 * Look at FIFO to see if command
1692 * went out.
1693 * (Timing problems?)
1694 */
1695 if (ncr53c9x_dmaselect) {
1696 if (sc->sc_cmdlen == 0)
1697 /* Hope for the best.. */
1698 break;
1699 } else if ((NCR_READ_REG(sc, NCR_FFLAG)
1700 & NCRFIFO_FF) == 0) {
1701 /* Hope for the best.. */
1702 break;
1703 }
1704 printf("(%s:%d:%d): selection failed;"
1705 " %d left in FIFO "
1706 "[intr %x, stat %x, step %d]\n",
1707 sc->sc_dev.dv_xname,
1708 sc_link->target,
1709 sc_link->lun,
1710 NCR_READ_REG(sc, NCR_FFLAG)
1711 & NCRFIFO_FF,
1712 sc->sc_espintr, sc->sc_espstat,
1713 sc->sc_espstep);
1714 NCRCMD(sc, NCRCMD_FLUSH);
1715 ncr53c9x_sched_msgout(SEND_ABORT);
1716 return 1;
1717 case 2:
1718 /* Select stuck at Command Phase */
1719 NCRCMD(sc, NCRCMD_FLUSH);
1720 case 4:
1721 if (ncr53c9x_dmaselect &&
1722 sc->sc_cmdlen != 0)
1723 printf("(%s:%d:%d): select; "
1724 "%d left in DMA buffer\n",
1725 sc->sc_dev.dv_xname,
1726 sc_link->target,
1727 sc_link->lun,
1728 sc->sc_cmdlen);
1729 /* So far, everything went fine */
1730 break;
1731 }
1732 #if 0
1733 if (ecb->xs->flags & SCSI_RESET)
1734 ncr53c9x_sched_msgout(SEND_DEV_RESET);
1735 else if (ti->flags & T_NEGOTIATE)
1736 ncr53c9x_sched_msgout(
1737 SEND_IDENTIFY | SEND_SDTR);
1738 else
1739 ncr53c9x_sched_msgout(SEND_IDENTIFY);
1740 #endif
1741
1742 ecb->flags |= ECB_NEXUS;
1743 ti->lubusy |= (1 << sc_link->lun);
1744
1745 sc->sc_prevphase = INVALID_PHASE; /* ?? */
1746 /* Do an implicit RESTORE POINTERS. */
1747 sc->sc_dp = ecb->daddr;
1748 sc->sc_dleft = ecb->dleft;
1749 sc->sc_state = NCR_CONNECTED;
1750 break;
1751 } else {
1752 printf("%s: unexpected status after select"
1753 ": [intr %x, stat %x, step %x]\n",
1754 sc->sc_dev.dv_xname,
1755 sc->sc_espintr, sc->sc_espstat,
1756 sc->sc_espstep);
1757 NCRCMD(sc, NCRCMD_FLUSH);
1758 DELAY(1);
1759 goto reset;
1760 }
1761 if (sc->sc_state == NCR_IDLE) {
1762 printf("%s: stray interrupt\n",
1763 sc->sc_dev.dv_xname);
1764 return 0;
1765 }
1766 break;
1767
1768 case NCR_CONNECTED:
1769 if (sc->sc_flags & NCR_ICCS) {
1770 u_char msg;
1771
1772 sc->sc_flags &= ~NCR_ICCS;
1773
1774 if (!(sc->sc_espintr & NCRINTR_DONE)) {
1775 printf("%s: ICCS: "
1776 ": [intr %x, stat %x, step %x]\n",
1777 sc->sc_dev.dv_xname,
1778 sc->sc_espintr, sc->sc_espstat,
1779 sc->sc_espstep);
1780 }
1781 if ((NCR_READ_REG(sc, NCR_FFLAG)
1782 & NCRFIFO_FF) != 2) {
1783 int i = (NCR_READ_REG(sc, NCR_FFLAG)
1784 & NCRFIFO_FF) - 2;
1785 while (i--)
1786 (void) NCR_READ_REG(sc,
1787 NCR_FIFO);
1788 }
1789 ecb->stat = NCR_READ_REG(sc, NCR_FIFO);
1790 msg = NCR_READ_REG(sc, NCR_FIFO);
1791 NCR_PHASE(("<stat:(%x,%x)>", ecb->stat, msg));
1792 if (msg == MSG_CMDCOMPLETE) {
1793 ecb->dleft =
1794 (ecb->flags & ECB_TENTATIVE_DONE)
1795 ? 0
1796 : sc->sc_dleft;
1797 if ((ecb->flags & ECB_SENSE) == 0)
1798 ecb->xs->resid = ecb->dleft;
1799 sc->sc_state = NCR_CMDCOMPLETE;
1800 } else
1801 printf("%s: STATUS_PHASE: msg %d\n",
1802 sc->sc_dev.dv_xname, msg);
1803 NCRCMD(sc, NCRCMD_MSGOK);
1804 continue; /* ie. wait for disconnect */
1805 }
1806 break;
1807 default:
1808 panic("%s: invalid state: %d",
1809 sc->sc_dev.dv_xname,
1810 sc->sc_state);
1811 }
1812
1813 /*
1814 * Driver is now in state NCR_CONNECTED, i.e. we
1815 * have a current command working the SCSI bus.
1816 */
1817 if (sc->sc_state != NCR_CONNECTED || ecb == NULL) {
1818 panic("esp no nexus");
1819 }
1820
1821 switch (sc->sc_phase) {
1822 case MESSAGE_OUT_PHASE:
1823 NCR_PHASE(("MESSAGE_OUT_PHASE "));
1824 ncr53c9x_msgout(sc);
1825 sc->sc_prevphase = MESSAGE_OUT_PHASE;
1826 break;
1827 case MESSAGE_IN_PHASE:
1828 NCR_PHASE(("MESSAGE_IN_PHASE "));
1829 if (sc->sc_espintr & NCRINTR_BS) {
1830 NCRCMD(sc, NCRCMD_FLUSH);
1831 sc->sc_flags |= NCR_WAITI;
1832 NCRCMD(sc, NCRCMD_TRANS);
1833 } else if (sc->sc_espintr & NCRINTR_FC) {
1834 if ((sc->sc_flags & NCR_WAITI) == 0) {
1835 printf("%s: MSGIN: unexpected FC bit: "
1836 "[intr %x, stat %x, step %x]\n",
1837 sc->sc_dev.dv_xname,
1838 sc->sc_espintr, sc->sc_espstat,
1839 sc->sc_espstep);
1840 }
1841 sc->sc_flags &= ~NCR_WAITI;
1842 ncr53c9x_msgin(sc);
1843 } else {
1844 printf("%s: MSGIN: weird bits: "
1845 "[intr %x, stat %x, step %x]\n",
1846 sc->sc_dev.dv_xname,
1847 sc->sc_espintr, sc->sc_espstat,
1848 sc->sc_espstep);
1849 }
1850 sc->sc_prevphase = MESSAGE_IN_PHASE;
1851 break;
1852 case COMMAND_PHASE:
1853 /*
1854 * Send the command block. Normally we don't see this
1855 * phase because the SEL_ATN command takes care of
1856 * all this. However, we end up here if either the
1857 * target or we wanted exchange some more messages
1858 * first (e.g. to start negotiations).
1859 */
1860
1861 NCR_PHASE(("COMMAND_PHASE 0x%02x (%d) ",
1862 ecb->cmd.cmd.opcode, ecb->clen));
1863 if (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF) {
1864 NCRCMD(sc, NCRCMD_FLUSH);
1865 DELAY(1);
1866 }
1867 if (ncr53c9x_dmaselect) {
1868 size_t size;
1869 /* setup DMA transfer for command */
1870 size = ecb->clen;
1871 sc->sc_cmdlen = size;
1872 sc->sc_cmdp = (caddr_t)&ecb->cmd.cmd;
1873 NCRDMA_SETUP(sc, &sc->sc_cmdp, &sc->sc_cmdlen,
1874 0, &size);
1875 /* Program the SCSI counter */
1876 NCR_WRITE_REG(sc, NCR_TCL, size);
1877 NCR_WRITE_REG(sc, NCR_TCM, size >> 8);
1878 if (sc->sc_cfg2 & NCRCFG2_FE) {
1879 NCR_WRITE_REG(sc, NCR_TCH, size >> 16);
1880 }
1881
1882 NCRCMD(sc, NCRCMD_TRANS | NCRCMD_DMA);
1883 NCRDMA_GO(sc);
1884 } else {
1885 u_char *cmd = (u_char *)&ecb->cmd.cmd;
1886 int i;
1887 /* Now the command into the FIFO */
1888 for (i = 0; i < ecb->clen; i++)
1889 NCR_WRITE_REG(sc, NCR_FIFO, *cmd++);
1890 NCRCMD(sc, NCRCMD_TRANS);
1891 }
1892 sc->sc_prevphase = COMMAND_PHASE;
1893 break;
1894 case DATA_OUT_PHASE:
1895 NCR_PHASE(("DATA_OUT_PHASE [%ld] ",(long)sc->sc_dleft));
1896 NCRCMD(sc, NCRCMD_FLUSH);
1897 size = min(sc->sc_dleft, sc->sc_maxxfer);
1898 NCRDMA_SETUP(sc, &sc->sc_dp, &sc->sc_dleft,
1899 0, &size);
1900 sc->sc_prevphase = DATA_OUT_PHASE;
1901 goto setup_xfer;
1902 case DATA_IN_PHASE:
1903 NCR_PHASE(("DATA_IN_PHASE "));
1904 if (sc->sc_rev == NCR_VARIANT_ESP100)
1905 NCRCMD(sc, NCRCMD_FLUSH);
1906 size = min(sc->sc_dleft, sc->sc_maxxfer);
1907 NCRDMA_SETUP(sc, &sc->sc_dp, &sc->sc_dleft,
1908 1, &size);
1909 sc->sc_prevphase = DATA_IN_PHASE;
1910 setup_xfer:
1911 /* Target returned to data phase: wipe "done" memory */
1912 ecb->flags &= ~ECB_TENTATIVE_DONE;
1913
1914 /* Program the SCSI counter */
1915 NCR_WRITE_REG(sc, NCR_TCL, size);
1916 NCR_WRITE_REG(sc, NCR_TCM, size >> 8);
1917 if (sc->sc_cfg2 & NCRCFG2_FE) {
1918 NCR_WRITE_REG(sc, NCR_TCH, size >> 16);
1919 }
1920 /* load the count in */
1921 NCRCMD(sc, NCRCMD_NOP|NCRCMD_DMA);
1922
1923 /*
1924 * Note that if `size' is 0, we've already transceived
1925 * all the bytes we want but we're still in DATA PHASE.
1926 * Apparently, the device needs padding. Also, a
1927 * transfer size of 0 means "maximum" to the chip
1928 * DMA logic.
1929 */
1930 NCRCMD(sc,
1931 (size==0?NCRCMD_TRPAD:NCRCMD_TRANS)|NCRCMD_DMA);
1932 NCRDMA_GO(sc);
1933 return 1;
1934 case STATUS_PHASE:
1935 NCR_PHASE(("STATUS_PHASE "));
1936 sc->sc_flags |= NCR_ICCS;
1937 NCRCMD(sc, NCRCMD_ICCS);
1938 sc->sc_prevphase = STATUS_PHASE;
1939 break;
1940 case INVALID_PHASE:
1941 break;
1942 default:
1943 printf("%s: unexpected bus phase; resetting\n",
1944 sc->sc_dev.dv_xname);
1945 goto reset;
1946 }
1947 }
1948 panic("esp: should not get here..");
1949
1950 reset:
1951 ncr53c9x_init(sc, 1);
1952 return 1;
1953
1954 finish:
1955 ncr53c9x_done(sc, ecb);
1956 goto out;
1957
1958 sched:
1959 sc->sc_state = NCR_IDLE;
1960 ncr53c9x_sched(sc);
1961 goto out;
1962
1963 out:
1964 return 1;
1965 }
1966
1967 void
1968 ncr53c9x_abort(sc, ecb)
1969 struct ncr53c9x_softc *sc;
1970 struct ncr53c9x_ecb *ecb;
1971 {
1972
1973 /* 2 secs for the abort */
1974 ecb->timeout = NCR_ABORT_TIMEOUT;
1975 ecb->flags |= ECB_ABORT;
1976
1977 if (ecb == sc->sc_nexus) {
1978 /*
1979 * If we're still selecting, the message will be scheduled
1980 * after selection is complete.
1981 */
1982 if (sc->sc_state == NCR_CONNECTED)
1983 ncr53c9x_sched_msgout(SEND_ABORT);
1984
1985 /*
1986 * Reschedule timeout. First, cancel a queued timeout (if any)
1987 * in case someone decides to call ncr53c9x_abort() from
1988 * elsewhere.
1989 */
1990 untimeout(ncr53c9x_timeout, ecb);
1991 timeout(ncr53c9x_timeout, ecb, (ecb->timeout * hz) / 1000);
1992 } else {
1993 ncr53c9x_dequeue(sc, ecb);
1994 TAILQ_INSERT_HEAD(&sc->ready_list, ecb, chain);
1995 if (sc->sc_state == NCR_IDLE)
1996 ncr53c9x_sched(sc);
1997 }
1998 }
1999
2000 void
2001 ncr53c9x_timeout(arg)
2002 void *arg;
2003 {
2004 struct ncr53c9x_ecb *ecb = arg;
2005 struct scsi_xfer *xs = ecb->xs;
2006 struct scsi_link *sc_link = xs->sc_link;
2007 struct ncr53c9x_softc *sc = sc_link->adapter_softc;
2008 int s;
2009
2010 sc_print_addr(sc_link);
2011 printf("%s: timed out [ecb %p (flags 0x%x, dleft %x, stat %x)], "
2012 "<state %d, nexus %p, phase(c %x, p %x), resid %lx, "
2013 "msg(q %x,o %x) %s>",
2014 sc->sc_dev.dv_xname,
2015 ecb, ecb->flags, ecb->dleft, ecb->stat,
2016 sc->sc_state, sc->sc_nexus, sc->sc_phase, sc->sc_prevphase,
2017 (long)sc->sc_dleft, sc->sc_msgpriq, sc->sc_msgout,
2018 NCRDMA_ISACTIVE(sc) ? "DMA active" : "");
2019 #if NCR53C9X_DEBUG > 1
2020 printf("TRACE: %s.", ecb->trace);
2021 #endif
2022
2023 s = splbio();
2024
2025 if (ecb->flags & ECB_ABORT) {
2026 /* abort timed out */
2027 printf(" AGAIN\n");
2028 ncr53c9x_init(sc, 1);
2029 } else {
2030 /* abort the operation that has timed out */
2031 printf("\n");
2032 xs->error = XS_TIMEOUT;
2033 ncr53c9x_abort(sc, ecb);
2034 }
2035
2036 splx(s);
2037 }
2038