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