ncr53c9x.c revision 1.31 1 /* $NetBSD: ncr53c9x.c,v 1.31 1998/11/30 07:54:29 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 ncr53c9x reset function. It does not reset the SCSI bus,
245 * only this controller, 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 ncr53c9x 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 scsi_print_addr(ecb->xs->sc_link);
1050 printf("got %ld extra bytes\n",
1051 -(long)sc->sc_dleft);
1052 sc->sc_dleft = 0;
1053 }
1054 ecb->dleft = (ecb->flags & ECB_TENTATIVE_DONE)
1055 ? 0
1056 : sc->sc_dleft;
1057 if ((ecb->flags & ECB_SENSE) == 0)
1058 ecb->xs->resid = ecb->dleft;
1059 sc->sc_state = NCR_CMDCOMPLETE;
1060 break;
1061
1062 case MSG_MESSAGE_REJECT:
1063 NCR_MSGS(("msg reject (msgout=%x) ", sc->sc_msgout));
1064 switch (sc->sc_msgout) {
1065 case SEND_SDTR:
1066 sc->sc_flags &= ~NCR_SYNCHNEGO;
1067 ti->flags &= ~(T_NEGOTIATE | T_SYNCMODE);
1068 ncr53c9x_setsync(sc, ti);
1069 break;
1070 case SEND_INIT_DET_ERR:
1071 goto abort;
1072 }
1073 break;
1074
1075 case MSG_NOOP:
1076 NCR_MSGS(("noop "));
1077 break;
1078
1079 case MSG_DISCONNECT:
1080 NCR_MSGS(("disconnect "));
1081 ti->dconns++;
1082 sc->sc_state = NCR_DISCONNECT;
1083
1084 /*
1085 * Mark the fact that all bytes have moved. The
1086 * target may not bother to do a SAVE POINTERS
1087 * at this stage. This flag will set the residual
1088 * count to zero on MSG COMPLETE.
1089 */
1090 if (sc->sc_dleft == 0)
1091 ecb->flags |= ECB_TENTATIVE_DONE;
1092
1093 break;
1094
1095 case MSG_SAVEDATAPOINTER:
1096 NCR_MSGS(("save datapointer "));
1097 ecb->daddr = sc->sc_dp;
1098 ecb->dleft = sc->sc_dleft;
1099 break;
1100
1101 case MSG_RESTOREPOINTERS:
1102 NCR_MSGS(("restore datapointer "));
1103 sc->sc_dp = ecb->daddr;
1104 sc->sc_dleft = ecb->dleft;
1105 break;
1106
1107 case MSG_EXTENDED:
1108 NCR_MSGS(("extended(%x) ", sc->sc_imess[2]));
1109 switch (sc->sc_imess[2]) {
1110 case MSG_EXT_SDTR:
1111 NCR_MSGS(("SDTR period %d, offset %d ",
1112 sc->sc_imess[3], sc->sc_imess[4]));
1113 if (sc->sc_imess[1] != 3)
1114 goto reject;
1115 ti->period = sc->sc_imess[3];
1116 ti->offset = sc->sc_imess[4];
1117 ti->flags &= ~T_NEGOTIATE;
1118 if (sc->sc_minsync == 0 ||
1119 ti->offset == 0 ||
1120 ti->period > 124) {
1121 #ifdef NCR53C9X_DEBUG
1122 scsi_print_addr(ecb->xs->sc_link);
1123 printf("async mode\n");
1124 #endif
1125 if ((sc->sc_flags&NCR_SYNCHNEGO)
1126 == 0) {
1127 /*
1128 * target initiated negotiation
1129 */
1130 ti->offset = 0;
1131 ti->flags &= ~T_SYNCMODE;
1132 ncr53c9x_sched_msgout(
1133 SEND_SDTR);
1134 } else {
1135 /* we are async */
1136 ti->flags &= ~T_SYNCMODE;
1137 }
1138 } else {
1139 int r = 250/ti->period;
1140 int s = (100*250)/ti->period - 100*r;
1141 int p;
1142
1143 p = ncr53c9x_stp2cpb(sc, ti->period);
1144 ti->period = ncr53c9x_cpb2stp(sc, p);
1145 #ifdef NCR53C9X_DEBUG
1146 scsi_print_addr(ecb->xs->sc_link);
1147 printf("max sync rate %d.%02dMb/s\n",
1148 r, s);
1149 #endif
1150 if ((sc->sc_flags&NCR_SYNCHNEGO) == 0) {
1151 /*
1152 * target initiated negotiation
1153 */
1154 if (ti->period <
1155 sc->sc_minsync)
1156 ti->period =
1157 sc->sc_minsync;
1158 if (ti->offset > 15)
1159 ti->offset = 15;
1160 ti->flags &= ~T_SYNCMODE;
1161 ncr53c9x_sched_msgout(
1162 SEND_SDTR);
1163 } else {
1164 /* we are sync */
1165 ti->flags |= T_SYNCMODE;
1166 }
1167 }
1168 sc->sc_flags &= ~NCR_SYNCHNEGO;
1169 ncr53c9x_setsync(sc, ti);
1170 break;
1171
1172 default:
1173 scsi_print_addr(ecb->xs->sc_link);
1174 printf("unrecognized MESSAGE EXTENDED;"
1175 " sending REJECT\n");
1176 goto reject;
1177 }
1178 break;
1179
1180 default:
1181 NCR_MSGS(("ident "));
1182 scsi_print_addr(ecb->xs->sc_link);
1183 printf("unrecognized MESSAGE; sending REJECT\n");
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",
1194 sc->sc_dev.dv_xname);
1195 goto reset;
1196 }
1197
1198 (void) ncr53c9x_reselect(sc, sc->sc_imess[0]);
1199 break;
1200
1201 default:
1202 printf("%s: unexpected MESSAGE IN; sending DEVICE RESET\n",
1203 sc->sc_dev.dv_xname);
1204 reset:
1205 ncr53c9x_sched_msgout(SEND_DEV_RESET);
1206 break;
1207
1208 abort:
1209 ncr53c9x_sched_msgout(SEND_ABORT);
1210 break;
1211 }
1212
1213 /* Ack last message byte */
1214 NCRCMD(sc, NCRCMD_MSGOK);
1215
1216 /* Done, reset message pointer. */
1217 sc->sc_flags &= ~NCR_DROP_MSGI;
1218 sc->sc_imlen = 0;
1219 }
1220
1221
1222 /*
1223 * Send the highest priority, scheduled message
1224 */
1225 void
1226 ncr53c9x_msgout(sc)
1227 register struct ncr53c9x_softc *sc;
1228 {
1229 struct ncr53c9x_tinfo *ti;
1230 struct ncr53c9x_ecb *ecb;
1231 size_t size;
1232
1233 NCR_TRACE(("[ncr53c9x_msgout(priq:%x, prevphase:%x)]",
1234 sc->sc_msgpriq, sc->sc_prevphase));
1235
1236 /*
1237 * XXX - the NCR_ATN flag is not in sync with the actual ATN
1238 * condition on the SCSI bus. The 53c9x chip
1239 * automatically turns off ATN before sending the
1240 * message byte. (see also the comment below in the
1241 * default case when picking out a message to send)
1242 */
1243 if (sc->sc_flags & NCR_ATN) {
1244 if (sc->sc_prevphase != MESSAGE_OUT_PHASE) {
1245 new:
1246 NCRCMD(sc, NCRCMD_FLUSH);
1247 DELAY(1);
1248 sc->sc_msgoutq = 0;
1249 sc->sc_omlen = 0;
1250 }
1251 } else {
1252 if (sc->sc_prevphase == MESSAGE_OUT_PHASE) {
1253 ncr53c9x_sched_msgout(sc->sc_msgoutq);
1254 goto new;
1255 } else {
1256 printf("%s at line %d: unexpected MESSAGE OUT phase\n",
1257 sc->sc_dev.dv_xname, __LINE__);
1258 }
1259 }
1260
1261 if (sc->sc_omlen == 0) {
1262 /* Pick up highest priority message */
1263 sc->sc_msgout = sc->sc_msgpriq & -sc->sc_msgpriq;
1264 sc->sc_msgoutq |= sc->sc_msgout;
1265 sc->sc_msgpriq &= ~sc->sc_msgout;
1266 sc->sc_omlen = 1; /* "Default" message len */
1267 switch (sc->sc_msgout) {
1268 case SEND_SDTR:
1269 ecb = sc->sc_nexus;
1270 ti = &sc->sc_tinfo[ecb->xs->sc_link->scsipi_scsi.target];
1271 sc->sc_omess[0] = MSG_EXTENDED;
1272 sc->sc_omess[1] = 3;
1273 sc->sc_omess[2] = MSG_EXT_SDTR;
1274 sc->sc_omess[3] = ti->period;
1275 sc->sc_omess[4] = ti->offset;
1276 sc->sc_omlen = 5;
1277 if ((sc->sc_flags & NCR_SYNCHNEGO) == 0) {
1278 ti->flags |= T_SYNCMODE;
1279 ncr53c9x_setsync(sc, ti);
1280 }
1281 break;
1282 case SEND_IDENTIFY:
1283 if (sc->sc_state != NCR_CONNECTED) {
1284 printf("%s at line %d: no nexus\n",
1285 sc->sc_dev.dv_xname, __LINE__);
1286 }
1287 ecb = sc->sc_nexus;
1288 sc->sc_omess[0] =
1289 MSG_IDENTIFY(ecb->xs->sc_link->scsipi_scsi.lun, 0);
1290 break;
1291 case SEND_DEV_RESET:
1292 sc->sc_flags |= NCR_ABORTING;
1293 sc->sc_omess[0] = MSG_BUS_DEV_RESET;
1294 ecb = sc->sc_nexus;
1295 ti = &sc->sc_tinfo[ecb->xs->sc_link->scsipi_scsi.target];
1296 ti->flags &= ~T_SYNCMODE;
1297 ti->flags |= T_NEGOTIATE;
1298 break;
1299 case SEND_PARITY_ERROR:
1300 sc->sc_omess[0] = MSG_PARITY_ERROR;
1301 break;
1302 case SEND_ABORT:
1303 sc->sc_flags |= NCR_ABORTING;
1304 sc->sc_omess[0] = MSG_ABORT;
1305 break;
1306 case SEND_INIT_DET_ERR:
1307 sc->sc_omess[0] = MSG_INITIATOR_DET_ERR;
1308 break;
1309 case SEND_REJECT:
1310 sc->sc_omess[0] = MSG_MESSAGE_REJECT;
1311 break;
1312 default:
1313 /*
1314 * We normally do not get here, since the chip
1315 * automatically turns off ATN before the last
1316 * byte of a message is sent to the target.
1317 * However, if the target rejects our (multi-byte)
1318 * message early by switching to MSG IN phase
1319 * ATN remains on, so the target may return to
1320 * MSG OUT phase. If there are no scheduled messages
1321 * left we send a NO-OP.
1322 *
1323 * XXX - Note that this leaves no useful purpose for
1324 * the NCR_ATN flag.
1325 */
1326 sc->sc_flags &= ~NCR_ATN;
1327 sc->sc_omess[0] = MSG_NOOP;
1328 break;
1329 }
1330 sc->sc_omp = sc->sc_omess;
1331 }
1332
1333 /* (re)send the message */
1334 size = min(sc->sc_omlen, sc->sc_maxxfer);
1335 NCRDMA_SETUP(sc, &sc->sc_omp, &sc->sc_omlen, 0, &size);
1336 /* Program the SCSI counter */
1337 NCR_WRITE_REG(sc, NCR_TCL, size);
1338 NCR_WRITE_REG(sc, NCR_TCM, size >> 8);
1339 if (sc->sc_cfg2 & NCRCFG2_FE) {
1340 NCR_WRITE_REG(sc, NCR_TCH, size >> 16);
1341 }
1342 /* Load the count in and start the message-out transfer */
1343 NCRCMD(sc, NCRCMD_NOP|NCRCMD_DMA);
1344 NCRCMD(sc, NCRCMD_TRANS|NCRCMD_DMA);
1345 NCRDMA_GO(sc);
1346 }
1347
1348 /*
1349 * This is the most critical part of the driver, and has to know
1350 * how to deal with *all* error conditions and phases from the SCSI
1351 * bus. If there are no errors and the DMA was active, then call the
1352 * DMA pseudo-interrupt handler. If this returns 1, then that was it
1353 * and we can return from here without further processing.
1354 *
1355 * Most of this needs verifying.
1356 */
1357 int sdebug = 0;
1358 int
1359 ncr53c9x_intr(sc)
1360 register struct ncr53c9x_softc *sc;
1361 {
1362 register struct ncr53c9x_ecb *ecb;
1363 register struct scsipi_link *sc_link;
1364 struct ncr53c9x_tinfo *ti;
1365 size_t size;
1366 int nfifo;
1367
1368 NCR_TRACE(("[ncr53c9x_intr] "));
1369
1370 if (!NCRDMA_ISINTR(sc))
1371 return (0);
1372
1373 again:
1374 /* and what do the registers say... */
1375 ncr53c9x_readregs(sc);
1376
1377 sc->sc_intrcnt.ev_count++;
1378
1379 /*
1380 * At the moment, only a SCSI Bus Reset or Illegal
1381 * Command are classed as errors. A disconnect is a
1382 * valid condition, and we let the code check is the
1383 * "NCR_BUSFREE_OK" flag was set before declaring it
1384 * and error.
1385 *
1386 * Also, the status register tells us about "Gross
1387 * Errors" and "Parity errors". Only the Gross Error
1388 * is really bad, and the parity errors are dealt
1389 * with later
1390 *
1391 * TODO
1392 * If there are too many parity error, go to slow
1393 * cable mode ?
1394 */
1395
1396 /* SCSI Reset */
1397 if (sc->sc_espintr & NCRINTR_SBR) {
1398 if (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF) {
1399 NCRCMD(sc, NCRCMD_FLUSH);
1400 DELAY(1);
1401 }
1402 if (sc->sc_state != NCR_SBR) {
1403 printf("%s: SCSI bus reset\n",
1404 sc->sc_dev.dv_xname);
1405 ncr53c9x_init(sc, 0); /* Restart everything */
1406 return (1);
1407 }
1408 #if 0
1409 /*XXX*/ printf("<expected bus reset: "
1410 "[intr %x, stat %x, step %d]>\n",
1411 sc->sc_espintr, sc->sc_espstat,
1412 sc->sc_espstep);
1413 #endif
1414 if (sc->sc_nexus)
1415 panic("%s: nexus in reset state",
1416 sc->sc_dev.dv_xname);
1417 goto sched;
1418 }
1419
1420 ecb = sc->sc_nexus;
1421
1422 #define NCRINTR_ERR (NCRINTR_SBR|NCRINTR_ILL)
1423 if (sc->sc_espintr & NCRINTR_ERR ||
1424 sc->sc_espstat & NCRSTAT_GE) {
1425
1426 if (sc->sc_espstat & NCRSTAT_GE) {
1427 /* Gross Error; no target ? */
1428 if (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF) {
1429 NCRCMD(sc, NCRCMD_FLUSH);
1430 DELAY(1);
1431 }
1432 if (sc->sc_state == NCR_CONNECTED ||
1433 sc->sc_state == NCR_SELECTING) {
1434 ecb->xs->error = XS_TIMEOUT;
1435 ncr53c9x_done(sc, ecb);
1436 }
1437 return (1);
1438 }
1439
1440 if (sc->sc_espintr & NCRINTR_ILL) {
1441 if (sc->sc_flags & NCR_EXPECT_ILLCMD) {
1442 /*
1443 * Eat away "Illegal command" interrupt
1444 * on a ESP100 caused by a re-selection
1445 * while we were trying to select
1446 * another target.
1447 */
1448 #ifdef DEBUG
1449 printf("%s: ESP100 work-around activated\n",
1450 sc->sc_dev.dv_xname);
1451 #endif
1452 sc->sc_flags &= ~NCR_EXPECT_ILLCMD;
1453 return (1);
1454 }
1455 /* illegal command, out of sync ? */
1456 printf("%s: illegal command: 0x%x "
1457 "(state %d, phase %x, prevphase %x)\n",
1458 sc->sc_dev.dv_xname, sc->sc_lastcmd,
1459 sc->sc_state, sc->sc_phase,
1460 sc->sc_prevphase);
1461 if (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF) {
1462 NCRCMD(sc, NCRCMD_FLUSH);
1463 DELAY(1);
1464 }
1465 ncr53c9x_init(sc, 1); /* Restart everything */
1466 return (1);
1467 }
1468 }
1469 sc->sc_flags &= ~NCR_EXPECT_ILLCMD;
1470
1471 /*
1472 * Call if DMA is active.
1473 *
1474 * If DMA_INTR returns true, then maybe go 'round the loop
1475 * again in case there is no more DMA queued, but a phase
1476 * change is expected.
1477 */
1478 if (NCRDMA_ISACTIVE(sc)) {
1479 int r = NCRDMA_INTR(sc);
1480 if (r == -1) {
1481 printf("%s: DMA error; resetting\n",
1482 sc->sc_dev.dv_xname);
1483 ncr53c9x_init(sc, 1);
1484 }
1485 /* If DMA active here, then go back to work... */
1486 if (NCRDMA_ISACTIVE(sc))
1487 return (1);
1488
1489 if ((sc->sc_espstat & NCRSTAT_TC) == 0) {
1490 /*
1491 * DMA not completed. If we can not find a
1492 * acceptable explanation, print a diagnostic.
1493 */
1494 if (sc->sc_state == NCR_SELECTING)
1495 /*
1496 * This can happen if we are reselected
1497 * while using DMA to select a target.
1498 */
1499 /*void*/;
1500 else if (sc->sc_prevphase == MESSAGE_OUT_PHASE) {
1501 /*
1502 * Our (multi-byte) message (eg SDTR) was
1503 * interrupted by the target to send
1504 * a MSG REJECT.
1505 * Print diagnostic if current phase
1506 * is not MESSAGE IN.
1507 */
1508 if (sc->sc_phase != MESSAGE_IN_PHASE)
1509 printf("%s: !TC on MSG OUT"
1510 " [intr %x, stat %x, step %d]"
1511 " prevphase %x, resid %x\n",
1512 sc->sc_dev.dv_xname,
1513 sc->sc_espintr,
1514 sc->sc_espstat,
1515 sc->sc_espstep,
1516 sc->sc_prevphase,
1517 sc->sc_omlen);
1518 } else if (sc->sc_dleft == 0) {
1519 /*
1520 * The DMA operation was started for
1521 * a DATA transfer. Print a diagnostic
1522 * if the DMA counter and TC bit
1523 * appear to be out of sync.
1524 */
1525 printf("%s: !TC on DATA XFER"
1526 " [intr %x, stat %x, step %d]"
1527 " prevphase %x, resid %x\n",
1528 sc->sc_dev.dv_xname,
1529 sc->sc_espintr,
1530 sc->sc_espstat,
1531 sc->sc_espstep,
1532 sc->sc_prevphase,
1533 ecb?ecb->dleft:-1);
1534 }
1535 }
1536 }
1537
1538 /*
1539 * Check for less serious errors.
1540 */
1541 if (sc->sc_espstat & NCRSTAT_PE) {
1542 printf("%s: SCSI bus parity error\n", sc->sc_dev.dv_xname);
1543 if (sc->sc_prevphase == MESSAGE_IN_PHASE)
1544 ncr53c9x_sched_msgout(SEND_PARITY_ERROR);
1545 else
1546 ncr53c9x_sched_msgout(SEND_INIT_DET_ERR);
1547 }
1548
1549 if (sc->sc_espintr & NCRINTR_DIS) {
1550 NCR_MISC(("<DISC [intr %x, stat %x, step %d]>",
1551 sc->sc_espintr,sc->sc_espstat,sc->sc_espstep));
1552 if (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF) {
1553 NCRCMD(sc, NCRCMD_FLUSH);
1554 DELAY(1);
1555 }
1556 /*
1557 * This command must (apparently) be issued within
1558 * 250mS of a disconnect. So here you are...
1559 */
1560 NCRCMD(sc, NCRCMD_ENSEL);
1561
1562 switch (sc->sc_state) {
1563 case NCR_RESELECTED:
1564 goto sched;
1565
1566 case NCR_SELECTING:
1567 ecb->xs->error = XS_SELTIMEOUT;
1568 goto finish;
1569
1570 case NCR_CONNECTED:
1571 if ((sc->sc_flags & NCR_SYNCHNEGO)) {
1572 #ifdef NCR53C9X_DEBUG
1573 if (ecb)
1574 scsi_print_addr(ecb->xs->sc_link);
1575 printf("sync nego not completed!\n");
1576 #endif
1577 ti = &sc->sc_tinfo[ecb->xs->sc_link->scsipi_scsi.target];
1578 sc->sc_flags &= ~NCR_SYNCHNEGO;
1579 ti->flags &= ~(T_NEGOTIATE | T_SYNCMODE);
1580 }
1581
1582 /* it may be OK to disconnect */
1583 if ((sc->sc_flags & NCR_ABORTING) == 0) {
1584 /*
1585 * Section 5.1.1 of the SCSI 2 spec
1586 * suggests issuing a REQUEST SENSE
1587 * following an unexpected disconnect.
1588 * Some devices go into a contingent
1589 * allegiance condition when
1590 * disconnecting, and this is necessary
1591 * to clean up their state.
1592 */
1593 printf("%s: unexpected disconnect; ",
1594 sc->sc_dev.dv_xname);
1595 if (ecb->flags & ECB_SENSE) {
1596 printf("resetting\n");
1597 goto reset;
1598 }
1599 printf("sending REQUEST SENSE\n");
1600 untimeout(ncr53c9x_timeout, ecb);
1601 ncr53c9x_sense(sc, ecb);
1602 goto out;
1603 }
1604
1605 ecb->xs->error = XS_TIMEOUT;
1606 goto finish;
1607
1608 case NCR_DISCONNECT:
1609 TAILQ_INSERT_HEAD(&sc->nexus_list, ecb, chain);
1610 sc->sc_nexus = NULL;
1611 goto sched;
1612
1613 case NCR_CMDCOMPLETE:
1614 goto finish;
1615 }
1616 }
1617
1618 switch (sc->sc_state) {
1619
1620 case NCR_SBR:
1621 printf("%s: waiting for SCSI Bus Reset to happen\n",
1622 sc->sc_dev.dv_xname);
1623 return (1);
1624
1625 case NCR_RESELECTED:
1626 /*
1627 * we must be continuing a message ?
1628 */
1629 if (sc->sc_phase != MESSAGE_IN_PHASE) {
1630 printf("%s: target didn't identify\n",
1631 sc->sc_dev.dv_xname);
1632 ncr53c9x_init(sc, 1);
1633 return (1);
1634 }
1635 printf("<<RESELECT CONT'd>>");
1636 #if XXXX
1637 ncr53c9x_msgin(sc);
1638 if (sc->sc_state != NCR_CONNECTED) {
1639 /* IDENTIFY fail?! */
1640 printf("%s: identify failed\n",
1641 sc->sc_dev.dv_xname);
1642 ncr53c9x_init(sc, 1);
1643 return (1);
1644 }
1645 #endif
1646 break;
1647
1648 case NCR_IDLE:
1649 case NCR_SELECTING:
1650 sc->sc_msgpriq = sc->sc_msgout = sc->sc_msgoutq = 0;
1651 sc->sc_flags = 0;
1652 ecb = sc->sc_nexus;
1653 if (ecb != NULL && (ecb->flags & ECB_NEXUS)) {
1654 scsi_print_addr(ecb->xs->sc_link);
1655 printf("ECB_NEXUS while in state %x\n", sc->sc_state);
1656 }
1657
1658 if (sc->sc_espintr & NCRINTR_RESEL) {
1659 /*
1660 * If we're trying to select a
1661 * target ourselves, push our command
1662 * back into the ready list.
1663 */
1664 if (sc->sc_state == NCR_SELECTING) {
1665 NCR_MISC(("backoff selector "));
1666 untimeout(ncr53c9x_timeout, ecb);
1667 sc_link = ecb->xs->sc_link;
1668 ti = &sc->sc_tinfo[sc_link->scsipi_scsi.target];
1669 TAILQ_INSERT_HEAD(&sc->ready_list, ecb, chain);
1670 ecb = sc->sc_nexus = NULL;
1671 }
1672 sc->sc_state = NCR_RESELECTED;
1673 if (sc->sc_phase != MESSAGE_IN_PHASE) {
1674 /*
1675 * Things are seriously fucked up.
1676 * Pull the brakes, i.e. reset
1677 */
1678 printf("%s: target didn't identify\n",
1679 sc->sc_dev.dv_xname);
1680 ncr53c9x_init(sc, 1);
1681 return (1);
1682 }
1683 /*
1684 * The C90 only inhibits FIFO writes until
1685 * reselection is complete, instead of
1686 * waiting until the interrupt status register
1687 * has been read. So, if the reselect happens
1688 * while we were entering a command bytes (for
1689 * another target) some of those bytes can
1690 * appear in the FIFO here, after the
1691 * interrupt is taken.
1692 */
1693 nfifo = NCR_READ_REG(sc,NCR_FFLAG) & NCRFIFO_FF;
1694 if (nfifo < 2 ||
1695 (nfifo > 2 &&
1696 sc->sc_rev != NCR_VARIANT_ESP100)) {
1697 printf("%s: RESELECT: %d bytes in FIFO! "
1698 "[intr %x, stat %x, step %d, prevphase %x]\n",
1699 sc->sc_dev.dv_xname,
1700 nfifo,
1701 sc->sc_espintr,
1702 sc->sc_espstat,
1703 sc->sc_espstep,
1704 sc->sc_prevphase);
1705 ncr53c9x_init(sc, 1);
1706 return (1);
1707 }
1708 sc->sc_selid = NCR_READ_REG(sc, NCR_FIFO);
1709 NCR_MISC(("selid=0x%2x ", sc->sc_selid));
1710
1711 /* Handle identify message */
1712 ncr53c9x_msgin(sc);
1713 if (nfifo != 2) {
1714 /*
1715 * Note: this should not happen
1716 * with `dmaselect' on.
1717 */
1718 sc->sc_flags |= NCR_EXPECT_ILLCMD;
1719 NCRCMD(sc, NCRCMD_FLUSH);
1720 } else if (ncr53c9x_dmaselect &&
1721 sc->sc_rev == NCR_VARIANT_ESP100) {
1722 sc->sc_flags |= NCR_EXPECT_ILLCMD;
1723 }
1724
1725 if (sc->sc_state != NCR_CONNECTED) {
1726 /* IDENTIFY fail?! */
1727 printf("%s: identify failed\n",
1728 sc->sc_dev.dv_xname);
1729 ncr53c9x_init(sc, 1);
1730 return (1);
1731 }
1732 goto shortcut; /* ie. next phase expected soon */
1733 }
1734
1735 #define NCRINTR_DONE (NCRINTR_FC|NCRINTR_BS)
1736 if ((sc->sc_espintr & NCRINTR_DONE) == NCRINTR_DONE) {
1737 /*
1738 * Arbitration won; examine the `step' register
1739 * to determine how far the selection could progress.
1740 */
1741 ecb = sc->sc_nexus;
1742 if (!ecb)
1743 panic("ncr53c9x: no nexus");
1744
1745 sc_link = ecb->xs->sc_link;
1746 ti = &sc->sc_tinfo[sc_link->scsipi_scsi.target];
1747
1748 switch (sc->sc_espstep) {
1749 case 0:
1750 /*
1751 * The target did not respond with a
1752 * message out phase - probably an old
1753 * device that doesn't recognize ATN.
1754 * Clear ATN and just continue, the
1755 * target should be in the command
1756 * phase.
1757 * XXXX check for command phase?
1758 */
1759 NCRCMD(sc, NCRCMD_RSTATN);
1760 break;
1761 case 1:
1762 if ((ti->flags & T_NEGOTIATE) == 0) {
1763 printf("%s: step 1 & !NEG\n",
1764 sc->sc_dev.dv_xname);
1765 goto reset;
1766 }
1767 if (sc->sc_phase != MESSAGE_OUT_PHASE) {
1768 printf("%s: !MSGOUT\n",
1769 sc->sc_dev.dv_xname);
1770 goto reset;
1771 }
1772 /* Start negotiating */
1773 ti->period = sc->sc_minsync;
1774 ti->offset = 15;
1775 sc->sc_flags |= NCR_SYNCHNEGO;
1776 ncr53c9x_sched_msgout(SEND_SDTR);
1777 break;
1778 case 3:
1779 /*
1780 * Grr, this is supposed to mean
1781 * "target left command phase prematurely".
1782 * It seems to happen regularly when
1783 * sync mode is on.
1784 * Look at FIFO to see if command went out.
1785 * (Timing problems?)
1786 */
1787 if (ncr53c9x_dmaselect) {
1788 if (sc->sc_cmdlen == 0)
1789 /* Hope for the best.. */
1790 break;
1791 } else if ((NCR_READ_REG(sc, NCR_FFLAG)
1792 & NCRFIFO_FF) == 0) {
1793 /* Hope for the best.. */
1794 break;
1795 }
1796 printf("(%s:%d:%d): selection failed;"
1797 " %d left in FIFO "
1798 "[intr %x, stat %x, step %d]\n",
1799 sc->sc_dev.dv_xname,
1800 sc_link->scsipi_scsi.target,
1801 sc_link->scsipi_scsi.lun,
1802 NCR_READ_REG(sc, NCR_FFLAG)
1803 & NCRFIFO_FF,
1804 sc->sc_espintr, sc->sc_espstat,
1805 sc->sc_espstep);
1806 NCRCMD(sc, NCRCMD_FLUSH);
1807 ncr53c9x_sched_msgout(SEND_ABORT);
1808 return (1);
1809 case 2:
1810 /* Select stuck at Command Phase */
1811 NCRCMD(sc, NCRCMD_FLUSH);
1812 case 4:
1813 if (ncr53c9x_dmaselect &&
1814 sc->sc_cmdlen != 0)
1815 printf("(%s:%d:%d): select; "
1816 "%d left in DMA buffer "
1817 "[intr %x, stat %x, step %d]\n",
1818 sc->sc_dev.dv_xname,
1819 sc_link->scsipi_scsi.target,
1820 sc_link->scsipi_scsi.lun,
1821 sc->sc_cmdlen,
1822 sc->sc_espintr,
1823 sc->sc_espstat,
1824 sc->sc_espstep);
1825 /* So far, everything went fine */
1826 break;
1827 }
1828
1829 ecb->flags |= ECB_NEXUS;
1830 ti->lubusy |= (1 << sc_link->scsipi_scsi.lun);
1831
1832 sc->sc_prevphase = INVALID_PHASE; /* ?? */
1833 /* Do an implicit RESTORE POINTERS. */
1834 sc->sc_dp = ecb->daddr;
1835 sc->sc_dleft = ecb->dleft;
1836 sc->sc_state = NCR_CONNECTED;
1837 break;
1838
1839 } else {
1840
1841 printf("%s: unexpected status after select"
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 NCRCMD(sc, NCRCMD_FLUSH);
1847 DELAY(1);
1848 goto reset;
1849 }
1850 if (sc->sc_state == NCR_IDLE) {
1851 printf("%s: stray interrupt\n",
1852 sc->sc_dev.dv_xname);
1853 return (0);
1854 }
1855 break;
1856
1857 case NCR_CONNECTED:
1858 if (sc->sc_flags & NCR_ICCS) {
1859 /* "Initiate Command Complete Steps" in progress */
1860 u_char msg;
1861
1862 sc->sc_flags &= ~NCR_ICCS;
1863
1864 if (!(sc->sc_espintr & NCRINTR_DONE)) {
1865 printf("%s: ICCS: "
1866 ": [intr %x, stat %x, step %x]\n",
1867 sc->sc_dev.dv_xname,
1868 sc->sc_espintr, sc->sc_espstat,
1869 sc->sc_espstep);
1870 }
1871 if ((NCR_READ_REG(sc, NCR_FFLAG)
1872 & NCRFIFO_FF) != 2) {
1873 int i = (NCR_READ_REG(sc, NCR_FFLAG)
1874 & NCRFIFO_FF) - 2;
1875 while (i--)
1876 (void) NCR_READ_REG(sc, NCR_FIFO);
1877 }
1878 ecb->stat = NCR_READ_REG(sc, NCR_FIFO);
1879 msg = NCR_READ_REG(sc, NCR_FIFO);
1880 NCR_PHASE(("<stat:(%x,%x)>", ecb->stat, msg));
1881 if (msg == MSG_CMDCOMPLETE) {
1882 ecb->dleft = (ecb->flags & ECB_TENTATIVE_DONE)
1883 ? 0
1884 : sc->sc_dleft;
1885 if ((ecb->flags & ECB_SENSE) == 0)
1886 ecb->xs->resid = ecb->dleft;
1887 sc->sc_state = NCR_CMDCOMPLETE;
1888 } else
1889 printf("%s: STATUS_PHASE: msg %d\n",
1890 sc->sc_dev.dv_xname, msg);
1891 NCRCMD(sc, NCRCMD_MSGOK);
1892 goto shortcut; /* ie. wait for disconnect */
1893 }
1894 break;
1895 default:
1896 panic("%s: invalid state: %d",
1897 sc->sc_dev.dv_xname,
1898 sc->sc_state);
1899 }
1900
1901 /*
1902 * Driver is now in state NCR_CONNECTED, i.e. we
1903 * have a current command working the SCSI bus.
1904 */
1905 if (sc->sc_state != NCR_CONNECTED || ecb == NULL) {
1906 panic("ncr53c9x: no nexus");
1907 }
1908
1909 switch (sc->sc_phase) {
1910 case MESSAGE_OUT_PHASE:
1911 NCR_PHASE(("MESSAGE_OUT_PHASE "));
1912 ncr53c9x_msgout(sc);
1913 sc->sc_prevphase = MESSAGE_OUT_PHASE;
1914 break;
1915 case MESSAGE_IN_PHASE:
1916 NCR_PHASE(("MESSAGE_IN_PHASE "));
1917 sc->sc_prevphase = MESSAGE_IN_PHASE;
1918 if (sc->sc_espintr & NCRINTR_BS) {
1919 NCRCMD(sc, NCRCMD_FLUSH);
1920 sc->sc_flags |= NCR_WAITI;
1921 NCRCMD(sc, NCRCMD_TRANS);
1922 } else if (sc->sc_espintr & NCRINTR_FC) {
1923 if ((sc->sc_flags & NCR_WAITI) == 0) {
1924 printf("%s: MSGIN: unexpected FC bit: "
1925 "[intr %x, stat %x, step %x]\n",
1926 sc->sc_dev.dv_xname,
1927 sc->sc_espintr, sc->sc_espstat,
1928 sc->sc_espstep);
1929 }
1930 sc->sc_flags &= ~NCR_WAITI;
1931 ncr53c9x_msgin(sc);
1932 } else {
1933 printf("%s: MSGIN: weird bits: "
1934 "[intr %x, stat %x, step %x]\n",
1935 sc->sc_dev.dv_xname,
1936 sc->sc_espintr, sc->sc_espstat,
1937 sc->sc_espstep);
1938 }
1939 goto shortcut; /* i.e. expect data to be ready */
1940 break;
1941 case COMMAND_PHASE:
1942 /*
1943 * Send the command block. Normally we don't see this
1944 * phase because the SEL_ATN command takes care of
1945 * all this. However, we end up here if either the
1946 * target or we wanted to exchange some more messages
1947 * first (e.g. to start negotiations).
1948 */
1949
1950 NCR_PHASE(("COMMAND_PHASE 0x%02x (%d) ",
1951 ecb->cmd.cmd.opcode, ecb->clen));
1952 if (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF) {
1953 NCRCMD(sc, NCRCMD_FLUSH);
1954 DELAY(1);
1955 }
1956 if (ncr53c9x_dmaselect) {
1957 size_t size;
1958 /* setup DMA transfer for command */
1959 size = ecb->clen;
1960 sc->sc_cmdlen = size;
1961 sc->sc_cmdp = (caddr_t)&ecb->cmd.cmd;
1962 NCRDMA_SETUP(sc, &sc->sc_cmdp, &sc->sc_cmdlen,
1963 0, &size);
1964 /* Program the SCSI counter */
1965 NCR_WRITE_REG(sc, NCR_TCL, size);
1966 NCR_WRITE_REG(sc, NCR_TCM, size >> 8);
1967 if (sc->sc_cfg2 & NCRCFG2_FE) {
1968 NCR_WRITE_REG(sc, NCR_TCH, size >> 16);
1969 }
1970
1971 /* load the count in */
1972 NCRCMD(sc, NCRCMD_NOP|NCRCMD_DMA);
1973
1974 /* start the command transfer */
1975 NCRCMD(sc, NCRCMD_TRANS | NCRCMD_DMA);
1976 NCRDMA_GO(sc);
1977 } else {
1978 u_char *cmd = (u_char *)&ecb->cmd.cmd;
1979 int i;
1980 /* Now the command into the FIFO */
1981 for (i = 0; i < ecb->clen; i++)
1982 NCR_WRITE_REG(sc, NCR_FIFO, *cmd++);
1983 NCRCMD(sc, NCRCMD_TRANS);
1984 }
1985 sc->sc_prevphase = COMMAND_PHASE;
1986 break;
1987 case DATA_OUT_PHASE:
1988 NCR_PHASE(("DATA_OUT_PHASE [%ld] ",(long)sc->sc_dleft));
1989 NCRCMD(sc, NCRCMD_FLUSH);
1990 size = min(sc->sc_dleft, sc->sc_maxxfer);
1991 NCRDMA_SETUP(sc, &sc->sc_dp, &sc->sc_dleft,
1992 0, &size);
1993 sc->sc_prevphase = DATA_OUT_PHASE;
1994 goto setup_xfer;
1995 case DATA_IN_PHASE:
1996 NCR_PHASE(("DATA_IN_PHASE "));
1997 if (sc->sc_rev == NCR_VARIANT_ESP100)
1998 NCRCMD(sc, NCRCMD_FLUSH);
1999 size = min(sc->sc_dleft, sc->sc_maxxfer);
2000 NCRDMA_SETUP(sc, &sc->sc_dp, &sc->sc_dleft,
2001 1, &size);
2002 sc->sc_prevphase = DATA_IN_PHASE;
2003 setup_xfer:
2004 /* Target returned to data phase: wipe "done" memory */
2005 ecb->flags &= ~ECB_TENTATIVE_DONE;
2006
2007 /* Program the SCSI counter */
2008 NCR_WRITE_REG(sc, NCR_TCL, size);
2009 NCR_WRITE_REG(sc, NCR_TCM, size >> 8);
2010 if (sc->sc_cfg2 & NCRCFG2_FE) {
2011 NCR_WRITE_REG(sc, NCR_TCH, size >> 16);
2012 }
2013 /* load the count in */
2014 NCRCMD(sc, NCRCMD_NOP|NCRCMD_DMA);
2015
2016 /*
2017 * Note that if `size' is 0, we've already transceived
2018 * all the bytes we want but we're still in DATA PHASE.
2019 * Apparently, the device needs padding. Also, a
2020 * transfer size of 0 means "maximum" to the chip
2021 * DMA logic.
2022 */
2023 NCRCMD(sc,
2024 (size==0?NCRCMD_TRPAD:NCRCMD_TRANS)|NCRCMD_DMA);
2025 NCRDMA_GO(sc);
2026 return (1);
2027 case STATUS_PHASE:
2028 NCR_PHASE(("STATUS_PHASE "));
2029 sc->sc_flags |= NCR_ICCS;
2030 NCRCMD(sc, NCRCMD_ICCS);
2031 sc->sc_prevphase = STATUS_PHASE;
2032 goto shortcut; /* i.e. expect status results soon */
2033 break;
2034 case INVALID_PHASE:
2035 break;
2036 default:
2037 printf("%s: unexpected bus phase; resetting\n",
2038 sc->sc_dev.dv_xname);
2039 goto reset;
2040 }
2041
2042 out:
2043 return (1);
2044
2045 reset:
2046 ncr53c9x_init(sc, 1);
2047 goto out;
2048
2049 finish:
2050 ncr53c9x_done(sc, ecb);
2051 goto out;
2052
2053 sched:
2054 sc->sc_state = NCR_IDLE;
2055 ncr53c9x_sched(sc);
2056 goto out;
2057
2058 shortcut:
2059 /*
2060 * The idea is that many of the SCSI operations take very little
2061 * time, and going away and getting interrupted is too high an
2062 * overhead to pay. For example, selecting, sending a message
2063 * and command and then doing some work can be done in one "pass".
2064 *
2065 * The delay is a heuristic. It is 2 when at 20Mhz, 2 at 25Mhz and 1
2066 * at 40Mhz. This needs testing.
2067 */
2068 DELAY(50/sc->sc_freq);
2069 if (NCRDMA_ISINTR(sc))
2070 goto again;
2071 goto out;
2072 }
2073
2074 void
2075 ncr53c9x_abort(sc, ecb)
2076 struct ncr53c9x_softc *sc;
2077 struct ncr53c9x_ecb *ecb;
2078 {
2079
2080 /* 2 secs for the abort */
2081 ecb->timeout = NCR_ABORT_TIMEOUT;
2082 ecb->flags |= ECB_ABORT;
2083
2084 if (ecb == sc->sc_nexus) {
2085 /*
2086 * If we're still selecting, the message will be scheduled
2087 * after selection is complete.
2088 */
2089 if (sc->sc_state == NCR_CONNECTED)
2090 ncr53c9x_sched_msgout(SEND_ABORT);
2091
2092 /*
2093 * Reschedule timeout. First, cancel a queued timeout (if any)
2094 * in case someone decides to call ncr53c9x_abort() from
2095 * elsewhere.
2096 */
2097 untimeout(ncr53c9x_timeout, ecb);
2098 timeout(ncr53c9x_timeout, ecb, (ecb->timeout * hz) / 1000);
2099 } else {
2100 /* The command should be on the nexus list */
2101 if ((ecb->flags & ECB_NEXUS) == 0) {
2102 scsi_print_addr(ecb->xs->sc_link);
2103 printf("ncr53c9x_abort: not NEXUS\n");
2104 ncr53c9x_init(sc, 1);
2105 }
2106 /*
2107 * Just leave the command on the nexus list.
2108 * XXX - what choice do we have but to reset the SCSI
2109 * eventually?
2110 */
2111 if (sc->sc_state == NCR_IDLE)
2112 ncr53c9x_sched(sc);
2113 }
2114 }
2115
2116 void
2117 ncr53c9x_timeout(arg)
2118 void *arg;
2119 {
2120 struct ncr53c9x_ecb *ecb = arg;
2121 struct scsipi_xfer *xs = ecb->xs;
2122 struct scsipi_link *sc_link = xs->sc_link;
2123 struct ncr53c9x_softc *sc = sc_link->adapter_softc;
2124 struct ncr53c9x_tinfo *ti = &sc->sc_tinfo[sc_link->scsipi_scsi.target];
2125 int s;
2126
2127 scsi_print_addr(sc_link);
2128 printf("%s: timed out [ecb %p (flags 0x%x, dleft %x, stat %x)], "
2129 "<state %d, nexus %p, phase(l %x, c %x, p %x), resid %lx, "
2130 "msg(q %x,o %x) %s>",
2131 sc->sc_dev.dv_xname,
2132 ecb, ecb->flags, ecb->dleft, ecb->stat,
2133 sc->sc_state, sc->sc_nexus,
2134 NCR_READ_REG(sc, NCR_STAT),
2135 sc->sc_phase, sc->sc_prevphase,
2136 (long)sc->sc_dleft, sc->sc_msgpriq, sc->sc_msgout,
2137 NCRDMA_ISACTIVE(sc) ? "DMA active" : "");
2138 #if NCR53C9X_DEBUG > 1
2139 printf("TRACE: %s.", ecb->trace);
2140 #endif
2141
2142 s = splbio();
2143
2144 if (ecb->flags & ECB_ABORT) {
2145 /* abort timed out */
2146 printf(" AGAIN\n");
2147
2148 ncr53c9x_init(sc, 1);
2149 } else {
2150 /* abort the operation that has timed out */
2151 printf("\n");
2152 xs->error = XS_TIMEOUT;
2153 ncr53c9x_abort(sc, ecb);
2154
2155 /* Disable sync mode if stuck in a data phase */
2156 if (ecb == sc->sc_nexus &&
2157 (ti->flags & T_SYNCMODE) != 0 &&
2158 (sc->sc_phase & (MSGI|CDI)) == 0) {
2159 scsi_print_addr(sc_link);
2160 printf("sync negotiation disabled\n");
2161 sc->sc_cfflags |= (1<<(sc_link->scsipi_scsi.target+8));
2162 }
2163 }
2164
2165 splx(s);
2166 }
2167