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