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