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