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