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