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