ncr53c9x.c revision 1.73 1 /* $NetBSD: ncr53c9x.c,v 1.73 2001/04/21 05:35:20 tsutsui 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/callout.h>
83 #include <sys/kernel.h>
84 #include <sys/errno.h>
85 #include <sys/ioctl.h>
86 #include <sys/device.h>
87 #include <sys/buf.h>
88 #include <sys/malloc.h>
89 #include <sys/proc.h>
90 #include <sys/queue.h>
91 #include <sys/pool.h>
92 #include <sys/scsiio.h>
93
94 #include <dev/scsipi/scsi_all.h>
95 #include <dev/scsipi/scsipi_all.h>
96 #include <dev/scsipi/scsiconf.h>
97 #include <dev/scsipi/scsi_message.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 #ifdef DEBUG
104 int ncr53c9x_notag = 0;
105 #endif
106
107 /*static*/ void ncr53c9x_readregs(struct ncr53c9x_softc *);
108 /*static*/ void ncr53c9x_select(struct ncr53c9x_softc *, struct ncr53c9x_ecb *);
109 /*static*/ int ncr53c9x_reselect(struct ncr53c9x_softc *, int, int, int);
110 /*static*/ void ncr53c9x_scsi_reset(struct ncr53c9x_softc *);
111 /*static*/ int ncr53c9x_poll(struct ncr53c9x_softc *,
112 struct scsipi_xfer *, int);
113 /*static*/ void ncr53c9x_sched(struct ncr53c9x_softc *);
114 /*static*/ void ncr53c9x_done(struct ncr53c9x_softc *, struct ncr53c9x_ecb *);
115 /*static*/ void ncr53c9x_msgin(struct ncr53c9x_softc *);
116 /*static*/ void ncr53c9x_msgout(struct ncr53c9x_softc *);
117 /*static*/ void ncr53c9x_timeout(void *arg);
118 /*static*/ void ncr53c9x_watch(void *arg);
119 /*static*/ void ncr53c9x_abort(struct ncr53c9x_softc *, struct ncr53c9x_ecb *);
120 /*static*/ void ncr53c9x_dequeue(struct ncr53c9x_softc *,
121 struct ncr53c9x_ecb *);
122 /*static*/ int ncr53c9x_ioctl(struct scsipi_link *, u_long,
123 caddr_t, int, struct proc *);
124
125 void ncr53c9x_sense(struct ncr53c9x_softc *, struct ncr53c9x_ecb *);
126 void ncr53c9x_free_ecb(struct ncr53c9x_softc *, struct ncr53c9x_ecb *, int);
127 struct ncr53c9x_ecb *ncr53c9x_get_ecb(struct ncr53c9x_softc *, int);
128
129 static inline int ncr53c9x_stp2cpb(struct ncr53c9x_softc *, int);
130 static inline void ncr53c9x_setsync(struct ncr53c9x_softc *,
131 struct ncr53c9x_tinfo *);
132 static struct ncr53c9x_linfo *ncr53c9x_lunsearch(struct ncr53c9x_tinfo *,
133 int64_t lun);
134
135 static void ncr53c9x_wrfifo(struct ncr53c9x_softc *, u_char *, int);
136
137 static int ncr53c9x_rdfifo(struct ncr53c9x_softc *, int);
138 #define NCR_RDFIFO_START 0
139 #define NCR_RDFIFO_CONTINUE 1
140
141
142 #define NCR_SET_COUNT(sc, size) do { \
143 NCR_WRITE_REG((sc), NCR_TCL, (size)); \
144 NCR_WRITE_REG((sc), NCR_TCM, (size) >> 8); \
145 if ((sc->sc_cfg2 & NCRCFG2_FE) || \
146 (sc->sc_rev == NCR_VARIANT_FAS366)) { \
147 NCR_WRITE_REG((sc), NCR_TCH, (size) >> 16); \
148 } \
149 if (sc->sc_rev == NCR_VARIANT_FAS366) { \
150 NCR_WRITE_REG(sc, NCR_RCH, 0); \
151 } \
152 } while (0)
153
154 static int ecb_pool_initialized = 0;
155 static struct pool ecb_pool;
156
157 /*
158 * Names for the NCR53c9x variants, correspnding to the variant tags
159 * in ncr53c9xvar.h.
160 */
161 static const char *ncr53c9x_variant_names[] = {
162 "ESP100",
163 "ESP100A",
164 "ESP200",
165 "NCR53C94",
166 "NCR53C96",
167 "ESP406",
168 "FAS408",
169 "FAS216",
170 "AM53C974",
171 "FAS366/HME",
172 };
173
174 static struct scsipi_adapter ncr53c9x_adapter = {
175 0, /* adapter refcnt */
176 ncr53c9x_scsi_cmd, /* cmd */
177 minphys, /* minphys */
178 ncr53c9x_ioctl, /* ioctl */
179 NULL, /* enable */
180 NULL, /* getgeom */
181 };
182
183 static struct scsipi_device ncr53c9x_device = {
184 NULL, /* use default error handler */
185 NULL, /* have a queue, served by this */
186 NULL, /* have no async handler */
187 NULL, /* use default 'done' routine */
188 };
189
190 /*
191 * Search linked list for LUN info by LUN id.
192 */
193 static struct ncr53c9x_linfo *
194 ncr53c9x_lunsearch(ti, lun)
195 struct ncr53c9x_tinfo *ti;
196 int64_t lun;
197 {
198 struct ncr53c9x_linfo *li;
199 LIST_FOREACH(li, &ti->luns, link)
200 if (li->lun == lun)
201 return (li);
202 return (NULL);
203 }
204
205 /*
206 * Attach this instance, and then all the sub-devices
207 */
208 void
209 ncr53c9x_attach(sc, adapter, device)
210 struct ncr53c9x_softc *sc;
211 struct scsipi_adapter *adapter;
212 struct scsipi_device *device;
213 {
214
215 callout_init(&sc->sc_watchdog);
216 /*
217 * Allocate SCSI message buffers.
218 * Front-ends can override allocation to avoid alignment
219 * handling in the DMA engines. Note that that ncr53c9x_msgout()
220 * can request a 1 byte DMA transfer.
221 */
222 if (sc->sc_omess == NULL)
223 sc->sc_omess = malloc(NCR_MAX_MSG_LEN, M_DEVBUF, M_NOWAIT);
224
225 if (sc->sc_imess == NULL)
226 sc->sc_imess = malloc(NCR_MAX_MSG_LEN + 1, M_DEVBUF, M_NOWAIT);
227
228 if (sc->sc_omess == NULL || sc->sc_imess == NULL) {
229 printf("out of memory\n");
230 return;
231 }
232
233 /*
234 * Note, the front-end has set us up to print the chip variation.
235 */
236 if (sc->sc_rev >= NCR_VARIANT_MAX) {
237 printf("\n%s: unknown variant %d, devices not attached\n",
238 sc->sc_dev.dv_xname, sc->sc_rev);
239 return;
240 }
241
242 printf(": %s, %dMHz, SCSI ID %d\n",
243 ncr53c9x_variant_names[sc->sc_rev], sc->sc_freq, sc->sc_id);
244
245 sc->sc_ccf = FREQTOCCF(sc->sc_freq);
246
247 /* The value *must not* be == 1. Make it 2 */
248 if (sc->sc_ccf == 1)
249 sc->sc_ccf = 2;
250
251 /*
252 * The recommended timeout is 250ms. This register is loaded
253 * with a value calculated as follows, from the docs:
254 *
255 * (timout period) x (CLK frequency)
256 * reg = -------------------------------------
257 * 8192 x (Clock Conversion Factor)
258 *
259 * Since CCF has a linear relation to CLK, this generally computes
260 * to the constant of 153.
261 */
262 sc->sc_timeout = ((250 * 1000) * sc->sc_freq) / (8192 * sc->sc_ccf);
263
264 /* CCF register only has 3 bits; 0 is actually 8 */
265 sc->sc_ccf &= 7;
266
267 /*
268 * fill in the prototype scsipi_link.
269 */
270 sc->sc_link.scsipi_scsi.channel = SCSI_CHANNEL_ONLY_ONE;
271 sc->sc_link.adapter_softc = sc;
272 sc->sc_link.scsipi_scsi.adapter_target = sc->sc_id;
273 sc->sc_link.adapter = (adapter) ? adapter : &ncr53c9x_adapter;
274 sc->sc_link.device = (device) ? device : &ncr53c9x_device;
275 sc->sc_link.openings = 2;
276 sc->sc_link.scsipi_scsi.max_target = 7; /* XXX fas has 16(not supported) */
277 sc->sc_link.scsipi_scsi.max_lun = 7;
278 sc->sc_link.type = BUS_SCSI;
279
280 /*
281 * Add reference to adapter so that we drop the reference after
282 * config_found() to make sure the adatper is disabled.
283 */
284 if (scsipi_adapter_addref(&sc->sc_link) != 0) {
285 printf("%s: unable to enable controller\n",
286 sc->sc_dev.dv_xname);
287 return;
288 }
289
290 /* Reset state & bus */
291 sc->sc_cfflags = sc->sc_dev.dv_cfdata->cf_flags;
292 sc->sc_state = 0;
293 ncr53c9x_init(sc, 1);
294
295 /*
296 * Now try to attach all the sub-devices
297 */
298 sc->sc_child = config_found(&sc->sc_dev, &sc->sc_link, scsiprint);
299
300 scsipi_adapter_delref(&sc->sc_link);
301 callout_reset(&sc->sc_watchdog, 60*hz, ncr53c9x_watch, sc);
302 }
303
304 int
305 ncr53c9x_detach(sc, flags)
306 struct ncr53c9x_softc *sc;
307 int flags;
308 {
309 int error;
310
311 if (sc->sc_child) {
312 error = config_detach(sc->sc_child, flags);
313 if (error)
314 return (error);
315 }
316
317 free(sc->sc_imess, M_DEVBUF);
318 free(sc->sc_omess, M_DEVBUF);
319
320 return (0);
321 }
322
323 /*
324 * This is the generic ncr53c9x reset function. It does not reset the SCSI bus,
325 * only this controller, but kills any on-going commands, and also stops
326 * and resets the DMA.
327 *
328 * After reset, registers are loaded with the defaults from the attach
329 * routine above.
330 */
331 void
332 ncr53c9x_reset(sc)
333 struct ncr53c9x_softc *sc;
334 {
335
336 /* reset DMA first */
337 NCRDMA_RESET(sc);
338
339 /* reset SCSI chip */
340 NCRCMD(sc, NCRCMD_RSTCHIP);
341 NCRCMD(sc, NCRCMD_NOP);
342 DELAY(500);
343
344 /* do these backwards, and fall through */
345 switch (sc->sc_rev) {
346 case NCR_VARIANT_ESP406:
347 case NCR_VARIANT_FAS408:
348 NCR_WRITE_REG(sc, NCR_CFG5, sc->sc_cfg5 | NCRCFG5_SINT);
349 NCR_WRITE_REG(sc, NCR_CFG4, sc->sc_cfg4);
350 case NCR_VARIANT_AM53C974:
351 case NCR_VARIANT_FAS216:
352 case NCR_VARIANT_NCR53C94:
353 case NCR_VARIANT_NCR53C96:
354 case NCR_VARIANT_ESP200:
355 sc->sc_features |= NCR_F_HASCFG3;
356 NCR_WRITE_REG(sc, NCR_CFG3, sc->sc_cfg3);
357 case NCR_VARIANT_ESP100A:
358 sc->sc_features |= NCR_F_SELATN3;
359 NCR_WRITE_REG(sc, NCR_CFG2, sc->sc_cfg2);
360 case NCR_VARIANT_ESP100:
361 NCR_WRITE_REG(sc, NCR_CFG1, sc->sc_cfg1);
362 NCR_WRITE_REG(sc, NCR_CCF, sc->sc_ccf);
363 NCR_WRITE_REG(sc, NCR_SYNCOFF, 0);
364 NCR_WRITE_REG(sc, NCR_TIMEOUT, sc->sc_timeout);
365 break;
366
367 case NCR_VARIANT_FAS366:
368 sc->sc_features |=
369 NCR_F_HASCFG3 | NCR_F_FASTSCSI | NCR_F_SELATN3;
370 sc->sc_cfg3 = NCRFASCFG3_FASTCLK | NCRFASCFG3_OBAUTO;
371 sc->sc_cfg3_fscsi = NCRFASCFG3_FASTSCSI;
372 NCR_WRITE_REG(sc, NCR_CFG3, sc->sc_cfg3);
373 sc->sc_cfg2 = 0; /* NCRCFG2_HMEFE| NCRCFG2_HME32 */
374 NCR_WRITE_REG(sc, NCR_CFG2, sc->sc_cfg2);
375 NCR_WRITE_REG(sc, NCR_CFG1, sc->sc_cfg1);
376 NCR_WRITE_REG(sc, NCR_CCF, sc->sc_ccf);
377 NCR_WRITE_REG(sc, NCR_SYNCOFF, 0);
378 NCR_WRITE_REG(sc, NCR_TIMEOUT, sc->sc_timeout);
379 break;
380
381 default:
382 printf("%s: unknown revision code, assuming ESP100\n",
383 sc->sc_dev.dv_xname);
384 NCR_WRITE_REG(sc, NCR_CFG1, sc->sc_cfg1);
385 NCR_WRITE_REG(sc, NCR_CCF, sc->sc_ccf);
386 NCR_WRITE_REG(sc, NCR_SYNCOFF, 0);
387 NCR_WRITE_REG(sc, NCR_TIMEOUT, sc->sc_timeout);
388 }
389
390 if (sc->sc_rev == NCR_VARIANT_AM53C974)
391 NCR_WRITE_REG(sc, NCR_AMDCFG4, sc->sc_cfg4);
392
393 #if 0
394 printf("%s: ncr53c9x_reset: revision %d\n",
395 sc->sc_dev.dv_xname, sc->sc_rev);
396 printf("%s: ncr53c9x_reset: cfg1 0x%x, cfg2 0x%x, cfg3 0x%x, "
397 "ccf 0x%x, timeout 0x%x\n",
398 sc->sc_dev.dv_xname, sc->sc_cfg1, sc->sc_cfg2, sc->sc_cfg3,
399 sc->sc_ccf, sc->sc_timeout);
400 #endif
401 }
402
403 /*
404 * Reset the SCSI bus, but not the chip
405 */
406 void
407 ncr53c9x_scsi_reset(sc)
408 struct ncr53c9x_softc *sc;
409 {
410
411 (*sc->sc_glue->gl_dma_stop)(sc);
412
413 printf("%s: resetting SCSI bus\n", sc->sc_dev.dv_xname);
414 NCRCMD(sc, NCRCMD_RSTSCSI);
415 }
416
417 /*
418 * Initialize ncr53c9x state machine
419 */
420 void
421 ncr53c9x_init(sc, doreset)
422 struct ncr53c9x_softc *sc;
423 int doreset;
424 {
425 struct ncr53c9x_ecb *ecb;
426 struct ncr53c9x_linfo *li;
427 int i, r;
428
429 NCR_TRACE(("[NCR_INIT(%d) %d] ", doreset, sc->sc_state));
430
431 if (!ecb_pool_initialized) {
432 /* All instances share this pool */
433 pool_init(&ecb_pool, sizeof(struct ncr53c9x_ecb), 0, 0, 0,
434 "ncr53c9x_ecb", 0, NULL, NULL, 0);
435 ecb_pool_initialized = 1;
436 }
437
438 if (sc->sc_state == 0) {
439 /* First time through; initialize. */
440
441 TAILQ_INIT(&sc->ready_list);
442 sc->sc_nexus = NULL;
443 bzero(sc->sc_tinfo, sizeof(sc->sc_tinfo));
444 for (r = 0; r < NCR_NTARG; r++) {
445 LIST_INIT(&sc->sc_tinfo[r].luns);
446 }
447 } else {
448 /* Cancel any active commands. */
449 sc->sc_state = NCR_CLEANING;
450 sc->sc_msgify = 0;
451 if ((ecb = sc->sc_nexus) != NULL) {
452 ecb->xs->error = XS_TIMEOUT;
453 ncr53c9x_done(sc, ecb);
454 }
455 /* Cancel outstanding disconnected commands on each LUN */
456 for (r = 0; r < 8; r++) {
457 LIST_FOREACH(li, &sc->sc_tinfo[r].luns, link) {
458 if ((ecb = li->untagged) != NULL) {
459 li->untagged = NULL;
460 /*
461 * XXXXXXX
462 *
463 * Should we terminate a command
464 * that never reached the disk?
465 */
466 li->busy = 0;
467 ecb->xs->error = XS_TIMEOUT;
468 ncr53c9x_done(sc, ecb);
469 }
470 for (i = 0; i < 256; i++)
471 if ((ecb = li->queued[i])) {
472 li->queued[i] = NULL;
473 ecb->xs->error = XS_TIMEOUT;
474 ncr53c9x_done(sc, ecb);
475 }
476 li->used = 0;
477 }
478 }
479 }
480
481 /*
482 * reset the chip to a known state
483 */
484 ncr53c9x_reset(sc);
485
486 sc->sc_phase = sc->sc_prevphase = INVALID_PHASE;
487 for (r = 0; r < 8; r++) {
488 struct ncr53c9x_tinfo *ti = &sc->sc_tinfo[r];
489 /* XXX - config flags per target: low bits: no reselect; high bits: no synch */
490
491 ti->flags = ((sc->sc_minsync && !(sc->sc_cfflags & (1<<(r+8))))
492 ? 0 : T_SYNCHOFF) |
493 ((sc->sc_cfflags & (1<<r)) ? T_RSELECTOFF : 0) |
494 T_NEED_TO_RESET;
495 #ifdef DEBUG
496 if (ncr53c9x_notag)
497 ti->flags &= ~T_TAG;
498 #endif
499 ti->period = sc->sc_minsync;
500 ti->offset = 0;
501 ti->cfg3 = 0;
502 }
503
504 if (doreset) {
505 sc->sc_state = NCR_SBR;
506 NCRCMD(sc, NCRCMD_RSTSCSI);
507 } else {
508 sc->sc_state = NCR_IDLE;
509 ncr53c9x_sched(sc);
510 }
511 }
512
513 /*
514 * Read the NCR registers, and save their contents for later use.
515 * NCR_STAT, NCR_STEP & NCR_INTR are mostly zeroed out when reading
516 * NCR_INTR - so make sure it is the last read.
517 *
518 * I think that (from reading the docs) most bits in these registers
519 * only make sense when he DMA CSR has an interrupt showing. Call only
520 * if an interrupt is pending.
521 */
522 __inline__ void
523 ncr53c9x_readregs(sc)
524 struct ncr53c9x_softc *sc;
525 {
526
527 sc->sc_espstat = NCR_READ_REG(sc, NCR_STAT);
528 /* Only the stepo bits are of interest */
529 sc->sc_espstep = NCR_READ_REG(sc, NCR_STEP) & NCRSTEP_MASK;
530
531 if (sc->sc_rev == NCR_VARIANT_FAS366)
532 sc->sc_espstat2 = NCR_READ_REG(sc, NCR_STAT2);
533
534 sc->sc_espintr = NCR_READ_REG(sc, NCR_INTR);
535
536 if (sc->sc_glue->gl_clear_latched_intr != NULL)
537 (*sc->sc_glue->gl_clear_latched_intr)(sc);
538
539 /*
540 * Determine the SCSI bus phase, return either a real SCSI bus phase
541 * or some pseudo phase we use to detect certain exceptions.
542 */
543
544 sc->sc_phase = (sc->sc_espintr & NCRINTR_DIS) ?
545 /* Disconnected */ BUSFREE_PHASE : sc->sc_espstat & NCRSTAT_PHASE;
546
547 NCR_MISC(("regs[intr=%02x,stat=%02x,step=%02x,stat2=%02x] ",
548 sc->sc_espintr, sc->sc_espstat, sc->sc_espstep, sc->sc_espstat2));
549 }
550
551 /*
552 * Convert Synchronous Transfer Period to chip register Clock Per Byte value.
553 */
554 static inline int
555 ncr53c9x_stp2cpb(sc, period)
556 struct ncr53c9x_softc *sc;
557 int period;
558 {
559 int v;
560 v = (sc->sc_freq * period) / 250;
561 if (ncr53c9x_cpb2stp(sc, v) < period)
562 /* Correct round-down error */
563 v++;
564 return (v);
565 }
566
567 static inline void
568 ncr53c9x_setsync(sc, ti)
569 struct ncr53c9x_softc *sc;
570 struct ncr53c9x_tinfo *ti;
571 {
572 u_char syncoff, synctp;
573 u_char cfg3 = sc->sc_cfg3 | ti->cfg3;
574
575 if (ti->flags & T_SYNCMODE) {
576 syncoff = ti->offset;
577 synctp = ncr53c9x_stp2cpb(sc, ti->period);
578 if (sc->sc_features & NCR_F_FASTSCSI) {
579 /*
580 * If the period is 200ns or less (ti->period <= 50),
581 * put the chip in Fast SCSI mode.
582 */
583 if (ti->period <= 50)
584 /*
585 * There are (at least) 4 variations of the
586 * configuration 3 register. The drive attach
587 * routine sets the appropriate bit to put the
588 * chip into Fast SCSI mode so that it doesn't
589 * have to be figured out here each time.
590 */
591 cfg3 |= sc->sc_cfg3_fscsi;
592 }
593
594 /*
595 * Am53c974 requires different SYNCTP values when the
596 * FSCSI bit is off.
597 */
598 if (sc->sc_rev == NCR_VARIANT_AM53C974 &&
599 (cfg3 & NCRAMDCFG3_FSCSI) == 0)
600 synctp--;
601 } else {
602 syncoff = 0;
603 synctp = 0;
604 }
605
606 if (sc->sc_features & NCR_F_HASCFG3)
607 NCR_WRITE_REG(sc, NCR_CFG3, cfg3);
608
609 NCR_WRITE_REG(sc, NCR_SYNCOFF, syncoff);
610 NCR_WRITE_REG(sc, NCR_SYNCTP, synctp);
611 }
612
613 /*
614 * Send a command to a target, set the driver state to NCR_SELECTING
615 * and let the caller take care of the rest.
616 *
617 * Keeping this as a function allows me to say that this may be done
618 * by DMA instead of programmed I/O soon.
619 */
620 void
621 ncr53c9x_select(sc, ecb)
622 struct ncr53c9x_softc *sc;
623 struct ncr53c9x_ecb *ecb;
624 {
625 struct scsipi_link *sc_link = ecb->xs->sc_link;
626 int target = sc_link->scsipi_scsi.target;
627 int lun = sc_link->scsipi_scsi.lun;
628 struct ncr53c9x_tinfo *ti = &sc->sc_tinfo[target];
629 int tiflags = ti->flags;
630 u_char *cmd;
631 int clen;
632 int selatn3, selatns;
633 size_t dmasize;
634
635 NCR_TRACE(("[ncr53c9x_select(t%d,l%d,cmd:%x,tag:%x,%x)] ",
636 target, lun, ecb->cmd.cmd.opcode, ecb->tag[0], ecb->tag[1]));
637
638 sc->sc_state = NCR_SELECTING;
639 /*
640 * Schedule the timeout now, the first time we will go away
641 * expecting to come back due to an interrupt, because it is
642 * always possible that the interrupt may never happen.
643 */
644 if ((ecb->xs->xs_control & XS_CTL_POLL) == 0) {
645 int timeout = ecb->timeout;
646
647 if (hz > 100 && timeout > 1000)
648 timeout = (timeout / 1000) * hz;
649 else
650 timeout = (timeout * hz) / 1000;
651
652 callout_reset(&ecb->xs->xs_callout, timeout,
653 ncr53c9x_timeout, ecb);
654 }
655
656 /*
657 * The docs say the target register is never reset, and I
658 * can't think of a better place to set it
659 */
660 if (sc->sc_rev == NCR_VARIANT_FAS366) {
661 NCRCMD(sc, NCRCMD_FLUSH);
662 NCR_WRITE_REG(sc, NCR_SELID, target | NCR_BUSID_HME);
663 } else {
664 NCR_WRITE_REG(sc, NCR_SELID, target);
665 }
666 ncr53c9x_setsync(sc, ti);
667
668 if ((ecb->flags & ECB_SENSE) != 0) {
669 /*
670 * For REQUEST SENSE, we should not send an IDENTIFY or
671 * otherwise mangle the target. There should be no MESSAGE IN
672 * phase.
673 */
674 if (sc->sc_features & NCR_F_DMASELECT) {
675 /* setup DMA transfer for command */
676 dmasize = clen = ecb->clen;
677 sc->sc_cmdlen = clen;
678 sc->sc_cmdp = (caddr_t)&ecb->cmd.cmd;
679
680 /* Program the SCSI counter */
681 NCR_SET_COUNT(sc, dmasize);
682
683 if (sc->sc_rev != NCR_VARIANT_FAS366)
684 NCRCMD(sc, NCRCMD_NOP|NCRCMD_DMA);
685
686 /* And get the targets attention */
687 NCRCMD(sc, NCRCMD_SELNATN | NCRCMD_DMA);
688 NCRDMA_SETUP(sc, &sc->sc_cmdp, &sc->sc_cmdlen, 0,
689 &dmasize);
690 NCRDMA_GO(sc);
691 } else {
692 ncr53c9x_wrfifo(sc, (u_char *)&ecb->cmd.cmd, ecb->clen);
693 NCRCMD(sc, NCRCMD_SELNATN);
694 }
695 return;
696 }
697
698 selatn3 = selatns = 0;
699 if (ecb->tag[0] != 0) {
700 if (sc->sc_features & NCR_F_SELATN3)
701 /* use SELATN3 to send tag messages */
702 selatn3 = 1;
703 else
704 /* We don't have SELATN3; use SELATNS to send tags */
705 selatns = 1;
706 }
707
708 if (ti->flags & T_NEGOTIATE) {
709 /* We have to use SELATNS to send sync/wide messages */
710 selatn3 = 0;
711 selatns = 1;
712 }
713
714 cmd = (u_char *)&ecb->cmd.cmd;
715
716 if (selatn3) {
717 /* We'll use tags with SELATN3 */
718 clen = ecb->clen + 3;
719 cmd -= 3;
720 cmd[0] = MSG_IDENTIFY(lun, 1); /* msg[0] */
721 cmd[1] = ecb->tag[0]; /* msg[1] */
722 cmd[2] = ecb->tag[1]; /* msg[2] */
723 } else {
724 /* We don't have tags, or will send messages with SELATNS */
725 clen = ecb->clen + 1;
726 cmd -= 1;
727 cmd[0] = MSG_IDENTIFY(lun, (tiflags & T_RSELECTOFF) == 0);
728 }
729
730 if ((sc->sc_features & NCR_F_DMASELECT) && !selatns) {
731
732 /* setup DMA transfer for command */
733 dmasize = clen;
734 sc->sc_cmdlen = clen;
735 sc->sc_cmdp = cmd;
736
737 /* Program the SCSI counter */
738 NCR_SET_COUNT(sc, dmasize);
739
740 /* load the count in */
741 /* if (sc->sc_rev != NCR_VARIANT_FAS366) */
742 NCRCMD(sc, NCRCMD_NOP|NCRCMD_DMA);
743
744 /* And get the targets attention */
745 if (selatn3) {
746 sc->sc_msgout = SEND_TAG;
747 sc->sc_flags |= NCR_ATN;
748 NCRCMD(sc, NCRCMD_SELATN3 | NCRCMD_DMA);
749 } else
750 NCRCMD(sc, NCRCMD_SELATN | NCRCMD_DMA);
751 NCRDMA_SETUP(sc, &sc->sc_cmdp, &sc->sc_cmdlen, 0, &dmasize);
752 NCRDMA_GO(sc);
753 return;
754 }
755
756 /*
757 * Who am I. This is where we tell the target that we are
758 * happy for it to disconnect etc.
759 */
760
761 /* Now get the command into the FIFO */
762 ncr53c9x_wrfifo(sc, cmd, clen);
763
764 /* And get the targets attention */
765 if (selatns) {
766 NCR_MISC(("SELATNS \n"));
767 /* Arbitrate, select and stop after IDENTIFY message */
768 NCRCMD(sc, NCRCMD_SELATNS);
769 } else if (selatn3) {
770 sc->sc_msgout = SEND_TAG;
771 sc->sc_flags |= NCR_ATN;
772 NCRCMD(sc, NCRCMD_SELATN3);
773 } else
774 NCRCMD(sc, NCRCMD_SELATN);
775 }
776
777 void
778 ncr53c9x_free_ecb(sc, ecb, flags)
779 struct ncr53c9x_softc *sc;
780 struct ncr53c9x_ecb *ecb;
781 int flags;
782 {
783 int s;
784
785 s = splbio();
786 ecb->flags = 0;
787 pool_put(&ecb_pool, (void *)ecb);
788 splx(s);
789 return;
790 }
791
792 struct ncr53c9x_ecb *
793 ncr53c9x_get_ecb(sc, flags)
794 struct ncr53c9x_softc *sc;
795 int flags;
796 {
797 struct ncr53c9x_ecb *ecb;
798 int s, wait = 0;
799
800 if ((curproc != NULL) && ((flags & XS_CTL_NOSLEEP) == 0))
801 wait = PR_WAITOK;
802
803 s = splbio();
804 ecb = (struct ncr53c9x_ecb *)pool_get(&ecb_pool, wait);
805 splx(s);
806 bzero(ecb, sizeof(*ecb));
807 if (ecb)
808 ecb->flags |= ECB_ALLOC;
809 return (ecb);
810 }
811
812 /*
813 * DRIVER FUNCTIONS CALLABLE FROM HIGHER LEVEL DRIVERS
814 */
815
816 /*
817 * Start a SCSI-command
818 * This function is called by the higher level SCSI-driver to queue/run
819 * SCSI-commands.
820 */
821 int
822 ncr53c9x_scsi_cmd(xs)
823 struct scsipi_xfer *xs;
824 {
825 struct scsipi_link *sc_link = xs->sc_link;
826 struct ncr53c9x_softc *sc = sc_link->adapter_softc;
827 struct ncr53c9x_ecb *ecb;
828 struct ncr53c9x_tinfo *ti;
829 struct ncr53c9x_linfo *li;
830 int64_t lun = sc_link->scsipi_scsi.lun;
831 int s, flags;
832
833 NCR_TRACE(("[ncr53c9x_scsi_cmd] "));
834 NCR_CMDS(("[0x%x, %d]->%d ", (int)xs->cmd->opcode, xs->cmdlen,
835 sc_link->scsipi_scsi.target));
836
837 /*
838 * Find the LUN info structure and allocate one if it does
839 * not exist.
840 */
841 flags = xs->xs_control;
842 ti = &sc->sc_tinfo[sc_link->scsipi_scsi.target];
843 li = TINFO_LUN(ti, lun);
844 if (li == NULL) {
845 int wait = M_NOWAIT;
846
847 /* Initialize LUN info and add to list. */
848 if ((curproc != NULL) && ((flags & XS_CTL_NOSLEEP) == 0))
849 wait = M_WAITOK;
850 if ((li = malloc(sizeof(*li), M_DEVBUF, M_NOWAIT)) == NULL)
851 return (TRY_AGAIN_LATER);
852
853 bzero(li, sizeof(*li));
854 li->last_used = time.tv_sec;
855 li->lun = lun;
856 s = splbio();
857 LIST_INSERT_HEAD(&ti->luns, li, link);
858 if (lun < NCR_NLUN)
859 ti->lun[lun] = li;
860 splx(s);
861 }
862
863 if ((ecb = ncr53c9x_get_ecb(sc, flags)) == NULL)
864 return (TRY_AGAIN_LATER);
865
866 /* Initialize ecb */
867 ecb->xs = xs;
868 ecb->timeout = xs->timeout;
869
870 if (flags & XS_CTL_RESET) {
871 ecb->flags |= ECB_RESET;
872 ecb->clen = 0;
873 ecb->dleft = 0;
874 } else {
875 bcopy(xs->cmd, &ecb->cmd.cmd, xs->cmdlen);
876 ecb->clen = xs->cmdlen;
877 ecb->daddr = xs->data;
878 ecb->dleft = xs->datalen;
879 }
880 ecb->stat = 0;
881
882 s = splbio();
883
884 TAILQ_INSERT_TAIL(&sc->ready_list, ecb, chain);
885 ecb->flags |= ECB_READY;
886 if (sc->sc_state == NCR_IDLE)
887 ncr53c9x_sched(sc);
888
889 splx(s);
890
891 if ((flags & XS_CTL_POLL) == 0)
892 return (SUCCESSFULLY_QUEUED);
893
894 /* Not allowed to use interrupts, use polling instead */
895 if (ncr53c9x_poll(sc, xs, ecb->timeout)) {
896 ncr53c9x_timeout(ecb);
897 if (ncr53c9x_poll(sc, xs, ecb->timeout))
898 ncr53c9x_timeout(ecb);
899 }
900 return (COMPLETE);
901 }
902
903 /*
904 * Used when interrupt driven I/O isn't allowed, e.g. during boot.
905 */
906 int
907 ncr53c9x_poll(sc, xs, count)
908 struct ncr53c9x_softc *sc;
909 struct scsipi_xfer *xs;
910 int count;
911 {
912
913 NCR_TRACE(("[ncr53c9x_poll] "));
914 while (count) {
915 if (NCRDMA_ISINTR(sc)) {
916 ncr53c9x_intr(sc);
917 }
918 #if alternatively
919 if (NCR_READ_REG(sc, NCR_STAT) & NCRSTAT_INT)
920 ncr53c9x_intr(sc);
921 #endif
922 if ((xs->xs_status & XS_STS_DONE) != 0)
923 return (0);
924 if (sc->sc_state == NCR_IDLE) {
925 NCR_TRACE(("[ncr53c9x_poll: rescheduling] "));
926 ncr53c9x_sched(sc);
927 }
928 DELAY(1000);
929 count--;
930 }
931 return (1);
932 }
933
934 int
935 ncr53c9x_ioctl(link, cmd, arg, flag, p)
936 struct scsipi_link *link;
937 u_long cmd;
938 caddr_t arg;
939 int flag;
940 struct proc *p;
941 {
942 struct ncr53c9x_softc *sc = link->adapter_softc;
943 int s, error = 0;
944
945 s = splbio();
946
947 switch (cmd) {
948 case SCBUSACCEL: {
949 struct scbusaccel_args *sp = (struct scbusaccel_args *)arg;
950 struct ncr53c9x_tinfo *ti = &sc->sc_tinfo[sp->sa_target];
951
952 if (sp->sa_lun != 0)
953 break;
954
955 NCR_MISC(("%s: SCBUSACCEL flag 0x%x\n", sc->sc_dev.dv_xname,
956 sp->sa_flags));
957
958 if ((sp->sa_flags & SC_ACCEL_WIDE) != 0) {
959 NCR_MISC(("%s: target %d: wide scsi negotiation\n",
960 sc->sc_dev.dv_xname, sp->sa_target));
961
962 if (sc->sc_rev == NCR_VARIANT_FAS366) {
963 ti->flags |= T_WIDE;
964 ti->width = 1;
965 }
966 }
967
968 if ((sp->sa_flags & SC_ACCEL_SYNC) != 0) {
969 /* If this adapter can't do sync; drop it */
970 if (sc->sc_minsync == 0)
971 break;
972
973 /*
974 * Check whether target is already clamped at
975 * non-sync operation on user request.
976 */
977 if ((ti->flags & T_SYNCHOFF) != 0)
978 break;
979
980 NCR_MISC(("%s: target %d: sync negotiation\n",
981 sc->sc_dev.dv_xname, sp->sa_target));
982 ti->flags |= T_NEGOTIATE;
983 }
984
985 if ((sp->sa_flags & SC_ACCEL_TAGS) != 0) {
986 if ((sc->sc_cfflags & (1<<((sp->sa_target)+16))) == 0) {
987 ti->flags |= T_TAG;
988 link->openings = 255;
989
990 printf("%s: target %d: tags\n",
991 sc->sc_dev.dv_xname, sp->sa_target);
992 }
993 }
994 break;
995 }
996 default:
997 error = ENOTTY;
998 break;
999 }
1000 splx(s);
1001 return (error);
1002 }
1003
1004
1005 /*
1006 * LOW LEVEL SCSI UTILITIES
1007 */
1008
1009 /*
1010 * Schedule a scsi operation. This has now been pulled out of the interrupt
1011 * handler so that we may call it from ncr53c9x_scsi_cmd and ncr53c9x_done.
1012 * This may save us an unecessary interrupt just to get things going.
1013 * Should only be called when state == NCR_IDLE and at bio pl.
1014 */
1015 void
1016 ncr53c9x_sched(sc)
1017 struct ncr53c9x_softc *sc;
1018 {
1019 struct ncr53c9x_ecb *ecb;
1020 struct scsipi_link *sc_link;
1021 struct ncr53c9x_tinfo *ti;
1022 int lun;
1023 struct ncr53c9x_linfo *li;
1024 int s, tag;
1025
1026 NCR_TRACE(("[ncr53c9x_sched] "));
1027 if (sc->sc_state != NCR_IDLE)
1028 panic("ncr53c9x_sched: not IDLE (state=%d)", sc->sc_state);
1029
1030 /*
1031 * Find first ecb in ready queue that is for a target/lunit
1032 * combinations that is not busy.
1033 */
1034 for (ecb = TAILQ_FIRST(&sc->ready_list); ecb != NULL;
1035 ecb = TAILQ_NEXT(ecb, chain)) {
1036 sc_link = ecb->xs->sc_link;
1037 ti = &sc->sc_tinfo[sc_link->scsipi_scsi.target];
1038 lun = sc_link->scsipi_scsi.lun;
1039
1040 /* Select type of tag for this command */
1041 if ((ti->flags & (T_RSELECTOFF)) != 0)
1042 tag = 0;
1043 else if ((ti->flags & (T_TAG)) == 0)
1044 tag = 0;
1045 else if ((ecb->flags & ECB_SENSE) != 0)
1046 tag = 0;
1047 else if (ecb->xs->xs_control & XS_CTL_URGENT)
1048 tag = MSG_HEAD_OF_Q_TAG;
1049 else
1050 tag = MSG_SIMPLE_Q_TAG;
1051 #if 0
1052 /* XXXX Use tags for polled commands? */
1053 if (ecb->xs->xs_control & XS_CTL_POLL)
1054 tag = 0;
1055 #endif
1056
1057 s = splbio();
1058 li = TINFO_LUN(ti, lun);
1059 if (li == NULL) {
1060 int wait = M_NOWAIT;
1061 int flags = ecb->xs->xs_control;
1062
1063 /* Initialize LUN info and add to list. */
1064 if ((curproc != NULL) &&
1065 ((flags & XS_CTL_NOSLEEP) == 0))
1066 wait = M_WAITOK;
1067 if ((li = malloc(sizeof(*li), M_DEVBUF, M_NOWAIT))
1068 == NULL) {
1069 splx(s);
1070 continue;
1071 }
1072 bzero(li, sizeof(*li));
1073 li->lun = lun;
1074
1075 LIST_INSERT_HEAD(&ti->luns, li, link);
1076 if (lun < NCR_NLUN)
1077 ti->lun[lun] = li;
1078 }
1079 li->last_used = time.tv_sec;
1080 if (tag == 0) {
1081 /* Try to issue this as an un-tagged command */
1082 if (li->untagged == NULL)
1083 li->untagged = ecb;
1084 }
1085 if (li->untagged != NULL) {
1086 tag = 0;
1087 if ((li->busy != 1) && li->used == 0) {
1088 /* We need to issue this untagged command now */
1089 ecb = li->untagged;
1090 sc_link = ecb->xs->sc_link;
1091 } else {
1092 /* Not ready yet */
1093 splx(s);
1094 continue;
1095 }
1096 }
1097 ecb->tag[0] = tag;
1098 if (tag != 0) {
1099 int i;
1100
1101 /* Allocate a tag */
1102 if (li->used == 255) {
1103 /* no free tags */
1104 splx(s);
1105 continue;
1106 }
1107 /* Start from the last used location */
1108 for (i = li->avail; i < 256; i++) {
1109 if (li->queued[i] == NULL)
1110 break;
1111 }
1112 /* Couldn't find one, start again from the beginning */
1113 if (i == 256) {
1114 for (i = 0; i < 256; i++) {
1115 if (li->queued[i] == NULL)
1116 break;
1117 }
1118 }
1119 #ifdef DIAGNOSTIC
1120 /* There's supposed to be at least 1 tag avail */
1121 if (i == 256)
1122 panic("ncr53c9x_sched: tag alloc failure\n");
1123 #endif
1124
1125 /* Save where to start next time. */
1126 li->avail = i + 1;
1127 li->used++;
1128
1129 li->queued[i] = ecb;
1130 ecb->tag[1] = i;
1131 }
1132 splx(s);
1133 if (li->untagged != NULL && (li->busy != 1)) {
1134 li->busy = 1;
1135 TAILQ_REMOVE(&sc->ready_list, ecb, chain);
1136 ecb->flags &= ~ECB_READY;
1137 sc->sc_nexus = ecb;
1138 ncr53c9x_select(sc, ecb);
1139 break;
1140 }
1141 if (li->untagged == NULL && tag != 0) {
1142 TAILQ_REMOVE(&sc->ready_list, ecb, chain);
1143 ecb->flags &= ~ECB_READY;
1144 sc->sc_nexus = ecb;
1145 ncr53c9x_select(sc, ecb);
1146 break;
1147 } else
1148 NCR_MISC(("%d:%d busy\n",
1149 sc_link->scsipi_scsi.target,
1150 sc_link->scsipi_scsi.lun));
1151 }
1152 }
1153
1154 void
1155 ncr53c9x_sense(sc, ecb)
1156 struct ncr53c9x_softc *sc;
1157 struct ncr53c9x_ecb *ecb;
1158 {
1159 struct scsipi_xfer *xs = ecb->xs;
1160 struct scsipi_link *sc_link = xs->sc_link;
1161 struct ncr53c9x_tinfo *ti = &sc->sc_tinfo[sc_link->scsipi_scsi.target];
1162 struct scsipi_sense *ss = (void *)&ecb->cmd.cmd;
1163 struct ncr53c9x_linfo *li;
1164 int lun = sc_link->scsipi_scsi.lun;
1165
1166 NCR_MISC(("requesting sense "));
1167 /* Next, setup a request sense command block */
1168 bzero(ss, sizeof(*ss));
1169 ss->opcode = REQUEST_SENSE;
1170 ss->byte2 = sc_link->scsipi_scsi.lun << SCSI_CMD_LUN_SHIFT;
1171 ss->length = sizeof(struct scsipi_sense_data);
1172 ecb->clen = sizeof(*ss);
1173 ecb->daddr = (char *)&xs->sense.scsi_sense;
1174 ecb->dleft = sizeof(struct scsipi_sense_data);
1175 ecb->flags |= ECB_SENSE;
1176 ecb->timeout = NCR_SENSE_TIMEOUT;
1177 ti->senses++;
1178 li = TINFO_LUN(ti, lun);
1179 if (li->busy)
1180 li->busy = 0;
1181 ncr53c9x_dequeue(sc, ecb);
1182 li->untagged = ecb; /* must be executed first to fix C/A */
1183 li->busy = 2;
1184 if (ecb == sc->sc_nexus) {
1185 ncr53c9x_select(sc, ecb);
1186 } else {
1187 TAILQ_INSERT_HEAD(&sc->ready_list, ecb, chain);
1188 ecb->flags |= ECB_READY;
1189 if (sc->sc_state == NCR_IDLE)
1190 ncr53c9x_sched(sc);
1191 }
1192 }
1193
1194 /*
1195 * POST PROCESSING OF SCSI_CMD (usually current)
1196 */
1197 void
1198 ncr53c9x_done(sc, ecb)
1199 struct ncr53c9x_softc *sc;
1200 struct ncr53c9x_ecb *ecb;
1201 {
1202 struct scsipi_xfer *xs = ecb->xs;
1203 struct scsipi_link *sc_link = xs->sc_link;
1204 struct ncr53c9x_tinfo *ti = &sc->sc_tinfo[sc_link->scsipi_scsi.target];
1205 int lun = sc_link->scsipi_scsi.lun;
1206 struct ncr53c9x_linfo *li = TINFO_LUN(ti, lun);
1207
1208 NCR_TRACE(("[ncr53c9x_done(error:%x)] ", xs->error));
1209
1210 callout_stop(&ecb->xs->xs_callout);
1211
1212 if (ecb->stat == SCSI_QUEUE_FULL) {
1213 /*
1214 * Set current throttle -- we should reset
1215 * this periodically
1216 */
1217 sc_link->openings = li->used - 1;
1218 printf("\n%s: QFULL -- throttling to %d commands\n",
1219 sc->sc_dev.dv_xname, sc_link->openings);
1220
1221 }
1222
1223 /*
1224 * Now, if we've come here with no error code, i.e. we've kept the
1225 * initial XS_NOERROR, and the status code signals that we should
1226 * check sense, we'll need to set up a request sense cmd block and
1227 * push the command back into the ready queue *before* any other
1228 * commands for this target/lunit, else we lose the sense info.
1229 * We don't support chk sense conditions for the request sense cmd.
1230 */
1231 if (xs->error == XS_NOERROR) {
1232 xs->status = ecb->stat;
1233 if ((ecb->flags & ECB_ABORT) != 0) {
1234 xs->error = XS_TIMEOUT;
1235 } else if ((ecb->flags & ECB_SENSE) != 0) {
1236 xs->error = XS_SENSE;
1237 } else if ((ecb->stat & ST_MASK) == SCSI_CHECK) {
1238 /* First, save the return values */
1239 xs->resid = ecb->dleft;
1240 ncr53c9x_sense(sc, ecb);
1241 return;
1242 } else {
1243 xs->resid = ecb->dleft;
1244 }
1245 }
1246
1247 xs->xs_status |= XS_STS_DONE;
1248
1249 #ifdef NCR53C9X_DEBUG
1250 if (ncr53c9x_debug & NCR_SHOWMISC) {
1251 if (xs->resid != 0)
1252 printf("resid=%d ", xs->resid);
1253 if (xs->error == XS_SENSE)
1254 printf("sense=0x%02x\n",
1255 xs->sense.scsi_sense.error_code);
1256 else
1257 printf("error=%d\n", xs->error);
1258 }
1259 #endif
1260
1261 /*
1262 * Remove the ECB from whatever queue it's on.
1263 */
1264 ncr53c9x_dequeue(sc, ecb);
1265 if (ecb == sc->sc_nexus) {
1266 sc->sc_nexus = NULL;
1267 if (sc->sc_state != NCR_CLEANING) {
1268 sc->sc_state = NCR_IDLE;
1269 ncr53c9x_sched(sc);
1270 }
1271 }
1272
1273 if (xs->error == XS_SELTIMEOUT) {
1274 /* Selection timeout -- discard this LUN if empty */
1275 if (li->untagged == NULL && li->used == 0) {
1276 if (lun < NCR_NLUN)
1277 ti->lun[lun] = NULL;
1278 LIST_REMOVE(li, link);
1279 free(li, M_DEVBUF);
1280 }
1281 }
1282
1283 ncr53c9x_free_ecb(sc, ecb, xs->xs_control);
1284 ti->cmds++;
1285 scsipi_done(xs);
1286 }
1287
1288 void
1289 ncr53c9x_dequeue(sc, ecb)
1290 struct ncr53c9x_softc *sc;
1291 struct ncr53c9x_ecb *ecb;
1292 {
1293 struct ncr53c9x_tinfo *ti =
1294 &sc->sc_tinfo[ecb->xs->sc_link->scsipi_scsi.target];
1295 struct ncr53c9x_linfo *li;
1296 int64_t lun = ecb->xs->sc_link->scsipi_scsi.lun;
1297
1298 li = TINFO_LUN(ti, lun);
1299 #ifdef DIAGNOSTIC
1300 if (li == NULL || li->lun != lun)
1301 panic("ncr53c9x_dequeue: lun %qx for ecb %p does not exist\n",
1302 (long long) lun, ecb);
1303 #endif
1304 if (li->untagged == ecb) {
1305 li->busy = 0;
1306 li->untagged = NULL;
1307 }
1308 if (ecb->tag[0] && li->queued[ecb->tag[1]] != NULL) {
1309 #ifdef DIAGNOSTIC
1310 if (li->queued[ecb->tag[1]] != NULL &&
1311 (li->queued[ecb->tag[1]] != ecb))
1312 panic("ncr53c9x_dequeue: slot %d for lun %qx has %p "
1313 "instead of ecb %p\n", ecb->tag[1],
1314 (long long) lun,
1315 li->queued[ecb->tag[1]], ecb);
1316 #endif
1317 li->queued[ecb->tag[1]] = NULL;
1318 li->used--;
1319 }
1320
1321 if ((ecb->flags & ECB_READY) != 0) {
1322 ecb->flags &= ~ECB_READY;
1323 TAILQ_REMOVE(&sc->ready_list, ecb, chain);
1324 }
1325 }
1326
1327 /*
1328 * INTERRUPT/PROTOCOL ENGINE
1329 */
1330
1331 /*
1332 * Schedule an outgoing message by prioritizing it, and asserting
1333 * attention on the bus. We can only do this when we are the initiator
1334 * else there will be an illegal command interrupt.
1335 */
1336 #define ncr53c9x_sched_msgout(m) \
1337 do { \
1338 NCR_MISC(("ncr53c9x_sched_msgout %x %d", m, __LINE__)); \
1339 NCRCMD(sc, NCRCMD_SETATN); \
1340 sc->sc_flags |= NCR_ATN; \
1341 sc->sc_msgpriq |= (m); \
1342 } while (0)
1343
1344 static void
1345 ncr53c9x_flushfifo(struct ncr53c9x_softc *sc)
1346 {
1347 NCR_MISC(("[flushfifo] "));
1348
1349 NCRCMD(sc, NCRCMD_FLUSH);
1350
1351 if (sc->sc_phase == COMMAND_PHASE ||
1352 sc->sc_phase == MESSAGE_OUT_PHASE)
1353 DELAY(2);
1354 }
1355
1356 static int
1357 ncr53c9x_rdfifo(struct ncr53c9x_softc *sc, int how)
1358 {
1359 int i, n;
1360 u_char *buf;
1361
1362 switch(how) {
1363 case NCR_RDFIFO_START:
1364 buf = sc->sc_imess;
1365 sc->sc_imlen = 0;
1366 break;
1367 case NCR_RDFIFO_CONTINUE:
1368 buf = sc->sc_imess + sc->sc_imlen;
1369 break;
1370 default:
1371 panic("ncr53c9x_rdfifo: bad flag\n");
1372 break;
1373 }
1374
1375 /*
1376 * XXX buffer (sc_imess) size for message
1377 */
1378
1379 n = NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF;
1380
1381 if (sc->sc_rev == NCR_VARIANT_FAS366) {
1382 n *= 2;
1383
1384 for (i = 0; i < n; i++)
1385 buf[i] = NCR_READ_REG(sc, NCR_FIFO);
1386
1387 if (sc->sc_espstat2 & FAS_STAT2_ISHUTTLE) {
1388
1389 NCR_WRITE_REG(sc, NCR_FIFO, 0);
1390 buf[i++] = NCR_READ_REG(sc, NCR_FIFO);
1391
1392 NCR_READ_REG(sc, NCR_FIFO);
1393
1394 ncr53c9x_flushfifo(sc);
1395 }
1396 } else {
1397 for (i = 0; i < n; i++)
1398 buf[i] = NCR_READ_REG(sc, NCR_FIFO);
1399 }
1400
1401 sc->sc_imlen += i;
1402
1403 #ifdef NCR53C9X_DEBUG
1404 {
1405 int j;
1406
1407 NCR_TRACE(("\n[rdfifo %s (%d):",
1408 (how == NCR_RDFIFO_START) ? "start" : "cont",
1409 (int)sc->sc_imlen));
1410 if (ncr53c9x_debug & NCR_SHOWTRAC) {
1411 for (j = 0; j < sc->sc_imlen; j++)
1412 printf(" %02x", sc->sc_imess[j]);
1413 printf("]\n");
1414 }
1415 }
1416 #endif
1417 return sc->sc_imlen;
1418 }
1419
1420 static void
1421 ncr53c9x_wrfifo(struct ncr53c9x_softc *sc, u_char *p, int len)
1422 {
1423 int i;
1424
1425 #ifdef NCR53C9X_DEBUG
1426 NCR_MISC(("[wrfifo(%d):", len));
1427 if (ncr53c9x_debug & NCR_SHOWTRAC) {
1428 for (i = 0; i < len; i++)
1429 printf(" %02x", p[i]);
1430 printf("]\n");
1431 }
1432 #endif
1433
1434 for (i = 0; i < len; i++) {
1435 NCR_WRITE_REG(sc, NCR_FIFO, p[i]);
1436
1437 if (sc->sc_rev == NCR_VARIANT_FAS366)
1438 NCR_WRITE_REG(sc, NCR_FIFO, 0);
1439 }
1440 }
1441
1442 int
1443 ncr53c9x_reselect(sc, message, tagtype, tagid)
1444 struct ncr53c9x_softc *sc;
1445 int message;
1446 int tagtype, tagid;
1447 {
1448 u_char selid, target, lun;
1449 struct ncr53c9x_ecb *ecb = NULL;
1450 struct ncr53c9x_tinfo *ti;
1451 struct ncr53c9x_linfo *li;
1452
1453
1454 if (sc->sc_rev == NCR_VARIANT_FAS366) {
1455 target = sc->sc_selid;
1456 } else {
1457 /*
1458 * The SCSI chip made a snapshot of the data bus
1459 * while the reselection was being negotiated.
1460 * This enables us to determine which target did
1461 * the reselect.
1462 */
1463 selid = sc->sc_selid & ~(1 << sc->sc_id);
1464 if (selid & (selid - 1)) {
1465 printf("%s: reselect with invalid selid %02x;"
1466 " sending DEVICE RESET\n",
1467 sc->sc_dev.dv_xname, selid);
1468 goto reset;
1469 }
1470
1471 target = ffs(selid) - 1;
1472 }
1473 lun = message & 0x07;
1474
1475 /*
1476 * Search wait queue for disconnected cmd
1477 * The list should be short, so I haven't bothered with
1478 * any more sophisticated structures than a simple
1479 * singly linked list.
1480 */
1481 ti = &sc->sc_tinfo[target];
1482 li = TINFO_LUN(ti, lun);
1483
1484 /*
1485 * We can get as far as the LUN with the IDENTIFY
1486 * message. Check to see if we're running an
1487 * un-tagged command. Otherwise ack the IDENTIFY
1488 * and wait for a tag message.
1489 */
1490 if (li != NULL) {
1491 if (li->untagged != NULL && li->busy)
1492 ecb = li->untagged;
1493 else if (tagtype != MSG_SIMPLE_Q_TAG) {
1494 /* Wait for tag to come by */
1495 sc->sc_state = NCR_IDENTIFIED;
1496 return (0);
1497 } else if (tagtype)
1498 ecb = li->queued[tagid];
1499 }
1500 if (ecb == NULL) {
1501 printf("%s: reselect from target %d lun %d tag %x:%x "
1502 "with no nexus; sending ABORT\n",
1503 sc->sc_dev.dv_xname, target, lun, tagtype, tagid);
1504 goto abort;
1505 }
1506
1507 /* Make this nexus active again. */
1508 sc->sc_state = NCR_CONNECTED;
1509 sc->sc_nexus = ecb;
1510 ncr53c9x_setsync(sc, ti);
1511
1512 if (ecb->flags & ECB_RESET)
1513 ncr53c9x_sched_msgout(SEND_DEV_RESET);
1514 else if (ecb->flags & ECB_ABORT)
1515 ncr53c9x_sched_msgout(SEND_ABORT);
1516
1517 /* Do an implicit RESTORE POINTERS. */
1518 sc->sc_dp = ecb->daddr;
1519 sc->sc_dleft = ecb->dleft;
1520
1521 return (0);
1522
1523 reset:
1524 ncr53c9x_sched_msgout(SEND_DEV_RESET);
1525 return (1);
1526
1527 abort:
1528 ncr53c9x_sched_msgout(SEND_ABORT);
1529 return (1);
1530 }
1531
1532
1533 /*
1534 * XXX this might be common thing(check with scsipi)
1535 */
1536 #define IS1BYTEMSG(m) (((m) != 1 && (m) < 0x20) || (m) & 0x80)
1537 #define IS2BYTEMSG(m) (((m) & 0xf0) == 0x20)
1538 #define ISEXTMSG(m) ((m) == 1)
1539
1540 static inline int
1541 __verify_msg_format(u_char *p, int len)
1542 {
1543
1544 if (len == 1 && IS1BYTEMSG(p[0]))
1545 return 1;
1546 if (len == 2 && IS2BYTEMSG(p[0]))
1547 return 1;
1548 if (len >= 3 && ISEXTMSG(p[0]) &&
1549 len == p[1] + 2)
1550 return 1;
1551
1552 return 0;
1553 }
1554
1555 /*
1556 * Get an incoming message as initiator.
1557 *
1558 * The SCSI bus must already be in MESSAGE_IN_PHASE and there is a
1559 * byte in the FIFO
1560 */
1561 void
1562 ncr53c9x_msgin(sc)
1563 struct ncr53c9x_softc *sc;
1564 {
1565
1566 NCR_TRACE(("[ncr53c9x_msgin(curmsglen:%ld)] ", (long)sc->sc_imlen));
1567
1568 if (sc->sc_imlen == 0) {
1569 printf("%s: msgin: no msg byte available\n",
1570 sc->sc_dev.dv_xname);
1571 return;
1572 }
1573
1574 /*
1575 * Prepare for a new message. A message should (according
1576 * to the SCSI standard) be transmitted in one single
1577 * MESSAGE_IN_PHASE. If we have been in some other phase,
1578 * then this is a new message.
1579 */
1580 if (sc->sc_prevphase != MESSAGE_IN_PHASE &&
1581 sc->sc_state != NCR_RESELECTED) {
1582 printf("%s: phase change, dropping message, "
1583 "prev %d, state %d\n",
1584 sc->sc_dev.dv_xname, sc->sc_prevphase, sc->sc_state);
1585 sc->sc_flags &= ~NCR_DROP_MSGI;
1586 sc->sc_imlen = 0;
1587 }
1588
1589 NCR_TRACE(("<msgbyte:0x%02x>", sc->sc_imess[0]));
1590
1591 /*
1592 * If we're going to reject the message, don't bother storing
1593 * the incoming bytes. But still, we need to ACK them.
1594 */
1595 if ((sc->sc_flags & NCR_DROP_MSGI) != 0) {
1596 NCRCMD(sc, NCRCMD_MSGOK);
1597 printf("<dropping msg byte %x>", sc->sc_imess[sc->sc_imlen]);
1598 return;
1599 }
1600
1601 if (sc->sc_imlen >= NCR_MAX_MSG_LEN) {
1602 ncr53c9x_sched_msgout(SEND_REJECT);
1603 sc->sc_flags |= NCR_DROP_MSGI;
1604 } else {
1605 u_char *pb;
1606 int plen;
1607
1608 switch (sc->sc_state) {
1609 /*
1610 * if received message is the first of reselection
1611 * then first byte is selid, and then message
1612 */
1613 case NCR_RESELECTED:
1614 pb = sc->sc_imess + 1;
1615 plen = sc->sc_imlen - 1;
1616 break;
1617 default:
1618 pb = sc->sc_imess;
1619 plen = sc->sc_imlen;
1620 break;
1621 }
1622
1623 if (__verify_msg_format(pb, plen))
1624 goto gotit;
1625 }
1626
1627 /* Ack what we have so far */
1628 NCRCMD(sc, NCRCMD_MSGOK);
1629 return;
1630
1631 gotit:
1632 NCR_MSGS(("gotmsg(%x) state %d", sc->sc_imess[0], sc->sc_state));
1633 /* we got complete message, flush the imess, */
1634 /* XXX nobody uses imlen below */
1635 sc->sc_imlen = 0;
1636 /*
1637 * Now we should have a complete message (1 byte, 2 byte
1638 * and moderately long extended messages). We only handle
1639 * extended messages which total length is shorter than
1640 * NCR_MAX_MSG_LEN. Longer messages will be amputated.
1641 */
1642 switch (sc->sc_state) {
1643 struct ncr53c9x_ecb *ecb;
1644 struct ncr53c9x_tinfo *ti;
1645 struct ncr53c9x_linfo *li;
1646 int lun;
1647
1648 case NCR_CONNECTED:
1649 ecb = sc->sc_nexus;
1650 ti = &sc->sc_tinfo[ecb->xs->sc_link->scsipi_scsi.target];
1651
1652 switch (sc->sc_imess[0]) {
1653 case MSG_CMDCOMPLETE:
1654 NCR_MSGS(("cmdcomplete "));
1655 if (sc->sc_dleft < 0) {
1656 scsi_print_addr(ecb->xs->sc_link);
1657 printf("got %ld extra bytes\n",
1658 -(long)sc->sc_dleft);
1659 sc->sc_dleft = 0;
1660 }
1661 ecb->dleft = (ecb->flags & ECB_TENTATIVE_DONE) ?
1662 0 : sc->sc_dleft;
1663 if ((ecb->flags & ECB_SENSE) == 0)
1664 ecb->xs->resid = ecb->dleft;
1665 sc->sc_state = NCR_CMDCOMPLETE;
1666 break;
1667
1668 case MSG_MESSAGE_REJECT:
1669 NCR_MSGS(("msg reject (msgout=%x) ", sc->sc_msgout));
1670 switch (sc->sc_msgout) {
1671 case SEND_TAG:
1672 /*
1673 * Target does not like tagged queuing.
1674 * - Flush the command queue
1675 * - Disable tagged queuing for the target
1676 * - Dequeue ecb from the queued array.
1677 */
1678 printf("%s: tagged queuing rejected: "
1679 "target %d\n",
1680 sc->sc_dev.dv_xname,
1681 ecb->xs->sc_link->scsipi_scsi.target);
1682
1683 NCR_MSGS(("(rejected sent tag)"));
1684 NCRCMD(sc, NCRCMD_FLUSH);
1685 DELAY(1);
1686 ti->flags &= ~T_TAG;
1687 lun = ecb->xs->sc_link->scsipi_scsi.lun;
1688 li = TINFO_LUN(ti, lun);
1689 if (ecb->tag[0] &&
1690 li->queued[ecb->tag[1]] != NULL) {
1691 li->queued[ecb->tag[1]] = NULL;
1692 li->used--;
1693 }
1694 ecb->tag[0] = ecb->tag[1] = 0;
1695 li->untagged = ecb;
1696 li->busy = 1;
1697 break;
1698
1699 case SEND_SDTR:
1700 printf("%s: sync transfer rejected: "
1701 "target %d\n",
1702 sc->sc_dev.dv_xname,
1703 ecb->xs->sc_link->scsipi_scsi.target);
1704
1705 sc->sc_flags &= ~NCR_SYNCHNEGO;
1706 ti->flags &= ~(T_NEGOTIATE | T_SYNCMODE);
1707 ncr53c9x_setsync(sc, ti);
1708 break;
1709
1710 case SEND_WDTR:
1711 printf("%s: wide transfer rejected: "
1712 "target %d\n",
1713 sc->sc_dev.dv_xname,
1714 ecb->xs->sc_link->scsipi_scsi.target);
1715 ti->flags &= ~T_WIDE;
1716 break;
1717
1718 case SEND_INIT_DET_ERR:
1719 goto abort;
1720 }
1721 break;
1722
1723 case MSG_NOOP:
1724 NCR_MSGS(("noop "));
1725 break;
1726
1727 case MSG_HEAD_OF_Q_TAG:
1728 case MSG_SIMPLE_Q_TAG:
1729 case MSG_ORDERED_Q_TAG:
1730 NCR_MSGS(("TAG %x:%x",
1731 sc->sc_imess[0], sc->sc_imess[1]));
1732 break;
1733
1734 case MSG_DISCONNECT:
1735 NCR_MSGS(("disconnect "));
1736 ti->dconns++;
1737 sc->sc_state = NCR_DISCONNECT;
1738
1739 /*
1740 * Mark the fact that all bytes have moved. The
1741 * target may not bother to do a SAVE POINTERS
1742 * at this stage. This flag will set the residual
1743 * count to zero on MSG COMPLETE.
1744 */
1745 if (sc->sc_dleft == 0)
1746 ecb->flags |= ECB_TENTATIVE_DONE;
1747
1748 break;
1749
1750 case MSG_SAVEDATAPOINTER:
1751 NCR_MSGS(("save datapointer "));
1752 ecb->daddr = sc->sc_dp;
1753 ecb->dleft = sc->sc_dleft;
1754 break;
1755
1756 case MSG_RESTOREPOINTERS:
1757 NCR_MSGS(("restore datapointer "));
1758 sc->sc_dp = ecb->daddr;
1759 sc->sc_dleft = ecb->dleft;
1760 break;
1761
1762 case MSG_EXTENDED:
1763 NCR_MSGS(("extended(%x) ", sc->sc_imess[2]));
1764 switch (sc->sc_imess[2]) {
1765 case MSG_EXT_SDTR:
1766 NCR_MSGS(("SDTR period %d, offset %d ",
1767 sc->sc_imess[3], sc->sc_imess[4]));
1768 if (sc->sc_imess[1] != 3)
1769 goto reject;
1770 ti->period = sc->sc_imess[3];
1771 ti->offset = sc->sc_imess[4];
1772 ti->flags &= ~T_NEGOTIATE;
1773 if (sc->sc_minsync == 0 ||
1774 ti->offset == 0 ||
1775 ti->period > 124) {
1776 #ifdef NCR53C9X_DEBUG
1777 scsi_print_addr(ecb->xs->sc_link);
1778 printf("async mode\n");
1779 #endif
1780 if ((sc->sc_flags&NCR_SYNCHNEGO) == 0) {
1781 /*
1782 * target initiated negotiation
1783 */
1784 ti->offset = 0;
1785 ti->flags &= ~T_SYNCMODE;
1786 ncr53c9x_sched_msgout(
1787 SEND_SDTR);
1788 } else {
1789 /* we are async */
1790 ti->flags &= ~T_SYNCMODE;
1791 }
1792 } else {
1793 int r = 250/ti->period;
1794 int s = (100*250)/ti->period - 100*r;
1795 int p;
1796
1797 p = ncr53c9x_stp2cpb(sc, ti->period);
1798 ti->period = ncr53c9x_cpb2stp(sc, p);
1799 #ifdef NCR53C9X_DEBUG
1800 scsi_print_addr(ecb->xs->sc_link);
1801 printf("max sync rate %d.%02dMB/s\n",
1802 r, s);
1803 #endif
1804 if ((sc->sc_flags&NCR_SYNCHNEGO) == 0) {
1805 /*
1806 * target initiated negotiation
1807 */
1808 if (ti->period <
1809 sc->sc_minsync)
1810 ti->period =
1811 sc->sc_minsync;
1812 if (ti->offset > 15)
1813 ti->offset = 15;
1814 ti->flags &= ~T_SYNCMODE;
1815 ncr53c9x_sched_msgout(
1816 SEND_SDTR);
1817 } else {
1818 /* we are sync */
1819 ti->flags |= T_SYNCMODE;
1820 }
1821 }
1822 sc->sc_flags &= ~NCR_SYNCHNEGO;
1823 ncr53c9x_setsync(sc, ti);
1824 break;
1825
1826 case MSG_EXT_WDTR:
1827 printf("%s: wide mode %d\n",
1828 sc->sc_dev.dv_xname, sc->sc_imess[3]);
1829 if (sc->sc_imess[3] == 1) {
1830 ti->cfg3 |= NCRFASCFG3_EWIDE;
1831 ncr53c9x_setsync(sc, ti);
1832 }
1833 ti->flags &= ~T_WIDE;
1834 break;
1835 default:
1836 scsi_print_addr(ecb->xs->sc_link);
1837 printf("unrecognized MESSAGE EXTENDED;"
1838 " sending REJECT\n");
1839 goto reject;
1840 }
1841 break;
1842
1843 default:
1844 NCR_MSGS(("ident "));
1845 scsi_print_addr(ecb->xs->sc_link);
1846 printf("unrecognized MESSAGE; sending REJECT\n");
1847 reject:
1848 ncr53c9x_sched_msgout(SEND_REJECT);
1849 break;
1850 }
1851 break;
1852
1853 case NCR_IDENTIFIED:
1854 /*
1855 * IDENTIFY message was received and queue tag is expected now
1856 */
1857 if ((sc->sc_imess[0] != MSG_SIMPLE_Q_TAG) ||
1858 (sc->sc_msgify == 0)) {
1859 printf("%s: TAG reselect without IDENTIFY;"
1860 " MSG %x;"
1861 " sending DEVICE RESET\n",
1862 sc->sc_dev.dv_xname,
1863 sc->sc_imess[0]);
1864 goto reset;
1865 }
1866 (void) ncr53c9x_reselect(sc, sc->sc_msgify,
1867 sc->sc_imess[0], sc->sc_imess[1]);
1868 break;
1869
1870 case NCR_RESELECTED:
1871 if (MSG_ISIDENTIFY(sc->sc_imess[1])) {
1872 sc->sc_msgify = sc->sc_imess[1];
1873 } else {
1874 printf("%s: reselect without IDENTIFY;"
1875 " MSG %x;"
1876 " sending DEVICE RESET\n",
1877 sc->sc_dev.dv_xname,
1878 sc->sc_imess[1]);
1879 goto reset;
1880 }
1881 (void) ncr53c9x_reselect(sc, sc->sc_msgify, 0, 0);
1882 break;
1883
1884 default:
1885 printf("%s: unexpected MESSAGE IN; sending DEVICE RESET\n",
1886 sc->sc_dev.dv_xname);
1887 reset:
1888 ncr53c9x_sched_msgout(SEND_DEV_RESET);
1889 break;
1890
1891 abort:
1892 ncr53c9x_sched_msgout(SEND_ABORT);
1893 break;
1894 }
1895
1896 /* if we have more messages to send set ATN */
1897 if (sc->sc_msgpriq)
1898 NCRCMD(sc, NCRCMD_SETATN);
1899
1900 /* Ack last message byte */
1901 NCRCMD(sc, NCRCMD_MSGOK);
1902
1903 /* Done, reset message pointer. */
1904 sc->sc_flags &= ~NCR_DROP_MSGI;
1905 sc->sc_imlen = 0;
1906 }
1907
1908
1909 /*
1910 * Send the highest priority, scheduled message
1911 */
1912 void
1913 ncr53c9x_msgout(sc)
1914 struct ncr53c9x_softc *sc;
1915 {
1916 struct ncr53c9x_tinfo *ti;
1917 struct ncr53c9x_ecb *ecb;
1918 size_t size;
1919
1920 NCR_TRACE(("[ncr53c9x_msgout(priq:%x, prevphase:%x)]",
1921 sc->sc_msgpriq, sc->sc_prevphase));
1922
1923 /*
1924 * XXX - the NCR_ATN flag is not in sync with the actual ATN
1925 * condition on the SCSI bus. The 53c9x chip
1926 * automatically turns off ATN before sending the
1927 * message byte. (see also the comment below in the
1928 * default case when picking out a message to send)
1929 */
1930 if (sc->sc_flags & NCR_ATN) {
1931 if (sc->sc_prevphase != MESSAGE_OUT_PHASE) {
1932 new:
1933 NCRCMD(sc, NCRCMD_FLUSH);
1934 /* DELAY(1); */
1935 sc->sc_msgoutq = 0;
1936 sc->sc_omlen = 0;
1937 }
1938 } else {
1939 if (sc->sc_prevphase == MESSAGE_OUT_PHASE) {
1940 ncr53c9x_sched_msgout(sc->sc_msgoutq);
1941 goto new;
1942 } else {
1943 printf("%s at line %d: unexpected MESSAGE OUT phase\n",
1944 sc->sc_dev.dv_xname, __LINE__);
1945 }
1946 }
1947
1948 if (sc->sc_omlen == 0) {
1949 /* Pick up highest priority message */
1950 sc->sc_msgout = sc->sc_msgpriq & -sc->sc_msgpriq;
1951 sc->sc_msgoutq |= sc->sc_msgout;
1952 sc->sc_msgpriq &= ~sc->sc_msgout;
1953 sc->sc_omlen = 1; /* "Default" message len */
1954 switch (sc->sc_msgout) {
1955 case SEND_SDTR:
1956 ecb = sc->sc_nexus;
1957 ti =
1958 &sc->sc_tinfo[ecb->xs->sc_link->scsipi_scsi.target];
1959 sc->sc_omess[0] = MSG_EXTENDED;
1960 sc->sc_omess[1] = MSG_EXT_SDTR_LEN;
1961 sc->sc_omess[2] = MSG_EXT_SDTR;
1962 sc->sc_omess[3] = ti->period;
1963 sc->sc_omess[4] = ti->offset;
1964 sc->sc_omlen = 5;
1965 if ((sc->sc_flags & NCR_SYNCHNEGO) == 0) {
1966 ti->flags |= T_SYNCMODE;
1967 ncr53c9x_setsync(sc, ti);
1968 }
1969 break;
1970 case SEND_WDTR:
1971 ecb = sc->sc_nexus;
1972 ti =
1973 &sc->sc_tinfo[ecb->xs->sc_link->scsipi_scsi.target];
1974 sc->sc_omess[0] = MSG_EXTENDED;
1975 sc->sc_omess[1] = MSG_EXT_WDTR_LEN;
1976 sc->sc_omess[2] = MSG_EXT_WDTR;
1977 sc->sc_omess[3] = ti->width;
1978 sc->sc_omlen = 4;
1979 break;
1980 case SEND_IDENTIFY:
1981 if (sc->sc_state != NCR_CONNECTED) {
1982 printf("%s at line %d: no nexus\n",
1983 sc->sc_dev.dv_xname, __LINE__);
1984 }
1985 ecb = sc->sc_nexus;
1986 sc->sc_omess[0] =
1987 MSG_IDENTIFY(ecb->xs->sc_link->scsipi_scsi.lun, 0);
1988 break;
1989 case SEND_TAG:
1990 if (sc->sc_state != NCR_CONNECTED) {
1991 printf("%s at line %d: no nexus\n",
1992 sc->sc_dev.dv_xname, __LINE__);
1993 }
1994 ecb = sc->sc_nexus;
1995 sc->sc_omess[0] = ecb->tag[0];
1996 sc->sc_omess[1] = ecb->tag[1];
1997 sc->sc_omlen = 2;
1998 break;
1999 case SEND_DEV_RESET:
2000 sc->sc_flags |= NCR_ABORTING;
2001 sc->sc_omess[0] = MSG_BUS_DEV_RESET;
2002 ecb = sc->sc_nexus;
2003 ti =
2004 &sc->sc_tinfo[ecb->xs->sc_link->scsipi_scsi.target];
2005 ti->flags &= ~T_SYNCMODE;
2006 if ((ti->flags & T_SYNCHOFF) == 0)
2007 /* We can re-start sync negotiation */
2008 ti->flags |= T_NEGOTIATE;
2009 break;
2010 case SEND_PARITY_ERROR:
2011 sc->sc_omess[0] = MSG_PARITY_ERROR;
2012 break;
2013 case SEND_ABORT:
2014 sc->sc_flags |= NCR_ABORTING;
2015 sc->sc_omess[0] = MSG_ABORT;
2016 break;
2017 case SEND_INIT_DET_ERR:
2018 sc->sc_omess[0] = MSG_INITIATOR_DET_ERR;
2019 break;
2020 case SEND_REJECT:
2021 sc->sc_omess[0] = MSG_MESSAGE_REJECT;
2022 break;
2023 default:
2024 /*
2025 * We normally do not get here, since the chip
2026 * automatically turns off ATN before the last
2027 * byte of a message is sent to the target.
2028 * However, if the target rejects our (multi-byte)
2029 * message early by switching to MSG IN phase
2030 * ATN remains on, so the target may return to
2031 * MSG OUT phase. If there are no scheduled messages
2032 * left we send a NO-OP.
2033 *
2034 * XXX - Note that this leaves no useful purpose for
2035 * the NCR_ATN flag.
2036 */
2037 sc->sc_flags &= ~NCR_ATN;
2038 sc->sc_omess[0] = MSG_NOOP;
2039 break;
2040 }
2041 sc->sc_omp = sc->sc_omess;
2042 }
2043
2044 #ifdef DEBUG
2045 {
2046 int i;
2047
2048 NCR_MISC(("<msgout:"));
2049 for (i = 0; i < sc->sc_omlen; i++)
2050 NCR_MISC((" %02x", sc->sc_omess[i]));
2051 NCR_MISC(("> "));
2052 }
2053 #endif
2054 if (sc->sc_rev == NCR_VARIANT_FAS366) {
2055 /*
2056 * XXX fifo size
2057 */
2058 ncr53c9x_flushfifo(sc);
2059 ncr53c9x_wrfifo(sc, sc->sc_omp, sc->sc_omlen);
2060 NCRCMD(sc, NCRCMD_TRANS);
2061 } else {
2062 /* (re)send the message */
2063 size = min(sc->sc_omlen, sc->sc_maxxfer);
2064 NCRDMA_SETUP(sc, &sc->sc_omp, &sc->sc_omlen, 0, &size);
2065 /* Program the SCSI counter */
2066 NCR_SET_COUNT(sc, size);
2067
2068 /* Load the count in and start the message-out transfer */
2069 NCRCMD(sc, NCRCMD_NOP|NCRCMD_DMA);
2070 NCRCMD(sc, NCRCMD_TRANS|NCRCMD_DMA);
2071 NCRDMA_GO(sc);
2072 }
2073 }
2074
2075 /*
2076 * This is the most critical part of the driver, and has to know
2077 * how to deal with *all* error conditions and phases from the SCSI
2078 * bus. If there are no errors and the DMA was active, then call the
2079 * DMA pseudo-interrupt handler. If this returns 1, then that was it
2080 * and we can return from here without further processing.
2081 *
2082 * Most of this needs verifying.
2083 */
2084 int
2085 ncr53c9x_intr(arg)
2086 void *arg;
2087 {
2088 struct ncr53c9x_softc *sc = arg;
2089 struct ncr53c9x_ecb *ecb;
2090 struct scsipi_link *sc_link;
2091 struct ncr53c9x_tinfo *ti;
2092 size_t size;
2093 int nfifo;
2094
2095 NCR_MISC(("[ncr53c9x_intr: state %d]", sc->sc_state));
2096
2097 if (!NCRDMA_ISINTR(sc))
2098 return (0);
2099
2100 again:
2101 /* and what do the registers say... */
2102 ncr53c9x_readregs(sc);
2103
2104 sc->sc_intrcnt.ev_count++;
2105
2106 /*
2107 * At the moment, only a SCSI Bus Reset or Illegal
2108 * Command are classed as errors. A disconnect is a
2109 * valid condition, and we let the code check is the
2110 * "NCR_BUSFREE_OK" flag was set before declaring it
2111 * and error.
2112 *
2113 * Also, the status register tells us about "Gross
2114 * Errors" and "Parity errors". Only the Gross Error
2115 * is really bad, and the parity errors are dealt
2116 * with later
2117 *
2118 * TODO
2119 * If there are too many parity error, go to slow
2120 * cable mode ?
2121 */
2122
2123 /* SCSI Reset */
2124 if ((sc->sc_espintr & NCRINTR_SBR) != 0) {
2125 if ((NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF) != 0) {
2126 NCRCMD(sc, NCRCMD_FLUSH);
2127 DELAY(1);
2128 }
2129 if (sc->sc_state != NCR_SBR) {
2130 printf("%s: SCSI bus reset\n", sc->sc_dev.dv_xname);
2131 ncr53c9x_init(sc, 0); /* Restart everything */
2132 return (1);
2133 }
2134 #if 0
2135 /*XXX*/ printf("<expected bus reset: "
2136 "[intr %x, stat %x, step %d]>\n",
2137 sc->sc_espintr, sc->sc_espstat, sc->sc_espstep);
2138 #endif
2139 if (sc->sc_nexus != NULL)
2140 panic("%s: nexus in reset state",
2141 sc->sc_dev.dv_xname);
2142 goto sched;
2143 }
2144
2145 ecb = sc->sc_nexus;
2146
2147 #define NCRINTR_ERR (NCRINTR_SBR|NCRINTR_ILL)
2148 if (sc->sc_espintr & NCRINTR_ERR ||
2149 sc->sc_espstat & NCRSTAT_GE) {
2150
2151 if ((sc->sc_espstat & NCRSTAT_GE) != 0) {
2152 /* Gross Error; no target ? */
2153 if (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF) {
2154 NCRCMD(sc, NCRCMD_FLUSH);
2155 DELAY(1);
2156 }
2157 if (sc->sc_state == NCR_CONNECTED ||
2158 sc->sc_state == NCR_SELECTING) {
2159 ecb->xs->error = XS_TIMEOUT;
2160 ncr53c9x_done(sc, ecb);
2161 }
2162 return (1);
2163 }
2164
2165 if ((sc->sc_espintr & NCRINTR_ILL) != 0) {
2166 if ((sc->sc_flags & NCR_EXPECT_ILLCMD) != 0) {
2167 /*
2168 * Eat away "Illegal command" interrupt
2169 * on a ESP100 caused by a re-selection
2170 * while we were trying to select
2171 * another target.
2172 */
2173 #ifdef DEBUG
2174 printf("%s: ESP100 work-around activated\n",
2175 sc->sc_dev.dv_xname);
2176 #endif
2177 sc->sc_flags &= ~NCR_EXPECT_ILLCMD;
2178 return (1);
2179 }
2180 /* illegal command, out of sync ? */
2181 printf("%s: illegal command: 0x%x "
2182 "(state %d, phase %x, prevphase %x)\n",
2183 sc->sc_dev.dv_xname, sc->sc_lastcmd,
2184 sc->sc_state, sc->sc_phase, sc->sc_prevphase);
2185 if (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF) {
2186 NCRCMD(sc, NCRCMD_FLUSH);
2187 DELAY(1);
2188 }
2189 ncr53c9x_init(sc, 1); /* Restart everything */
2190 return (1);
2191 }
2192 }
2193 sc->sc_flags &= ~NCR_EXPECT_ILLCMD;
2194
2195 /*
2196 * Call if DMA is active.
2197 *
2198 * If DMA_INTR returns true, then maybe go 'round the loop
2199 * again in case there is no more DMA queued, but a phase
2200 * change is expected.
2201 */
2202 if (NCRDMA_ISACTIVE(sc)) {
2203 int r = NCRDMA_INTR(sc);
2204 if (r == -1) {
2205 printf("%s: DMA error; resetting\n",
2206 sc->sc_dev.dv_xname);
2207 ncr53c9x_init(sc, 1);
2208 }
2209 /* If DMA active here, then go back to work... */
2210 if (NCRDMA_ISACTIVE(sc))
2211 return (1);
2212
2213 if ((sc->sc_espstat & NCRSTAT_TC) == 0) {
2214 /*
2215 * DMA not completed. If we can not find a
2216 * acceptable explanation, print a diagnostic.
2217 */
2218 if (sc->sc_state == NCR_SELECTING)
2219 /*
2220 * This can happen if we are reselected
2221 * while using DMA to select a target.
2222 */
2223 /*void*/;
2224 else if (sc->sc_prevphase == MESSAGE_OUT_PHASE) {
2225 /*
2226 * Our (multi-byte) message (eg SDTR) was
2227 * interrupted by the target to send
2228 * a MSG REJECT.
2229 * Print diagnostic if current phase
2230 * is not MESSAGE IN.
2231 */
2232 if (sc->sc_phase != MESSAGE_IN_PHASE)
2233 printf("%s: !TC on MSG OUT"
2234 " [intr %x, stat %x, step %d]"
2235 " prevphase %x, resid %lx\n",
2236 sc->sc_dev.dv_xname,
2237 sc->sc_espintr,
2238 sc->sc_espstat,
2239 sc->sc_espstep,
2240 sc->sc_prevphase,
2241 (u_long)sc->sc_omlen);
2242 } else if (sc->sc_dleft == 0) {
2243 /*
2244 * The DMA operation was started for
2245 * a DATA transfer. Print a diagnostic
2246 * if the DMA counter and TC bit
2247 * appear to be out of sync.
2248 */
2249 printf("%s: !TC on DATA XFER"
2250 " [intr %x, stat %x, step %d]"
2251 " prevphase %x, resid %x\n",
2252 sc->sc_dev.dv_xname,
2253 sc->sc_espintr,
2254 sc->sc_espstat,
2255 sc->sc_espstep,
2256 sc->sc_prevphase,
2257 ecb ? ecb->dleft : -1);
2258 }
2259 }
2260 }
2261
2262 /*
2263 * Check for less serious errors.
2264 */
2265 if ((sc->sc_espstat & NCRSTAT_PE) != 0) {
2266 printf("%s: SCSI bus parity error\n", sc->sc_dev.dv_xname);
2267 if (sc->sc_prevphase == MESSAGE_IN_PHASE)
2268 ncr53c9x_sched_msgout(SEND_PARITY_ERROR);
2269 else
2270 ncr53c9x_sched_msgout(SEND_INIT_DET_ERR);
2271 }
2272
2273 if ((sc->sc_espintr & NCRINTR_DIS) != 0) {
2274 sc->sc_msgify = 0;
2275 NCR_MISC(("<DISC [intr %x, stat %x, step %d]>",
2276 sc->sc_espintr,sc->sc_espstat,sc->sc_espstep));
2277 if (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF) {
2278 NCRCMD(sc, NCRCMD_FLUSH);
2279 /* DELAY(1); */
2280 }
2281 /*
2282 * This command must (apparently) be issued within
2283 * 250mS of a disconnect. So here you are...
2284 */
2285 NCRCMD(sc, NCRCMD_ENSEL);
2286
2287 switch (sc->sc_state) {
2288 case NCR_RESELECTED:
2289 goto sched;
2290
2291 case NCR_SELECTING:
2292 {
2293 struct ncr53c9x_linfo *li;
2294
2295 ecb->xs->error = XS_SELTIMEOUT;
2296
2297 /* Selection timeout -- discard all LUNs if empty */
2298 sc_link = ecb->xs->sc_link;
2299 ti = &sc->sc_tinfo[sc_link->scsipi_scsi.target];
2300 li = LIST_FIRST(&ti->luns);
2301 while (li != NULL) {
2302 if (li->untagged == NULL && li->used == 0) {
2303 if (li->lun < NCR_NLUN)
2304 ti->lun[li->lun] = NULL;
2305 LIST_REMOVE(li, link);
2306 free(li, M_DEVBUF);
2307 /*
2308 * Restart the search at the beginning
2309 */
2310 li = LIST_FIRST(&ti->luns);
2311 continue;
2312 }
2313 li = LIST_NEXT(li, link);
2314 }
2315 goto finish;
2316 }
2317 case NCR_CONNECTED:
2318 if ((sc->sc_flags & NCR_SYNCHNEGO) != 0) {
2319 #ifdef NCR53C9X_DEBUG
2320 if (ecb != NULL)
2321 scsi_print_addr(ecb->xs->sc_link);
2322 printf("sync nego not completed!\n");
2323 #endif
2324 ti = &sc->sc_tinfo[ecb->xs->sc_link->scsipi_scsi.target];
2325 sc->sc_flags &= ~NCR_SYNCHNEGO;
2326 ti->flags &= ~(T_NEGOTIATE | T_SYNCMODE);
2327 }
2328
2329 /* it may be OK to disconnect */
2330 if ((sc->sc_flags & NCR_ABORTING) == 0) {
2331 /*
2332 * Section 5.1.1 of the SCSI 2 spec
2333 * suggests issuing a REQUEST SENSE
2334 * following an unexpected disconnect.
2335 * Some devices go into a contingent
2336 * allegiance condition when
2337 * disconnecting, and this is necessary
2338 * to clean up their state.
2339 */
2340 printf("%s: unexpected disconnect; ",
2341 sc->sc_dev.dv_xname);
2342 if ((ecb->flags & ECB_SENSE) != 0) {
2343 printf("resetting\n");
2344 goto reset;
2345 }
2346 printf("sending REQUEST SENSE\n");
2347 callout_stop(&ecb->xs->xs_callout);
2348 ncr53c9x_sense(sc, ecb);
2349 goto out;
2350 }
2351
2352 ecb->xs->error = XS_TIMEOUT;
2353 goto finish;
2354
2355 case NCR_DISCONNECT:
2356 sc->sc_nexus = NULL;
2357 goto sched;
2358
2359 case NCR_CMDCOMPLETE:
2360 goto finish;
2361 }
2362 }
2363
2364 switch (sc->sc_state) {
2365
2366 case NCR_SBR:
2367 printf("%s: waiting for SCSI Bus Reset to happen\n",
2368 sc->sc_dev.dv_xname);
2369 return (1);
2370
2371 case NCR_RESELECTED:
2372 /*
2373 * we must be continuing a message ?
2374 */
2375 if (sc->sc_phase != MESSAGE_IN_PHASE) {
2376 printf("%s: target didn't identify\n",
2377 sc->sc_dev.dv_xname);
2378 ncr53c9x_init(sc, 1);
2379 return (1);
2380 }
2381 printf("<<RESELECT CONT'd>>");
2382 #if XXXX
2383 ncr53c9x_msgin(sc);
2384 if (sc->sc_state != NCR_CONNECTED) {
2385 /* IDENTIFY fail?! */
2386 printf("%s: identify failed\n",
2387 sc->sc_dev.dv_xname, sc->sc_state);
2388 ncr53c9x_init(sc, 1);
2389 return (1);
2390 }
2391 #endif
2392 break;
2393
2394 case NCR_IDENTIFIED:
2395 ecb = sc->sc_nexus;
2396 if (sc->sc_phase != MESSAGE_IN_PHASE) {
2397 int i = (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF);
2398 /*
2399 * Things are seriously fucked up.
2400 * Pull the brakes, i.e. reset
2401 */
2402 printf("%s: target didn't send tag: %d bytes in fifo\n",
2403 sc->sc_dev.dv_xname, i);
2404 /* Drain and display fifo */
2405 while (i-- > 0)
2406 printf("[%d] ", NCR_READ_REG(sc, NCR_FIFO));
2407
2408 ncr53c9x_init(sc, 1);
2409 return (1);
2410 } else
2411 goto msgin;
2412
2413 break;
2414
2415 case NCR_IDLE:
2416 case NCR_SELECTING:
2417 ecb = sc->sc_nexus;
2418 if (sc->sc_espintr & NCRINTR_RESEL) {
2419 sc->sc_msgpriq = sc->sc_msgout = sc->sc_msgoutq = 0;
2420 sc->sc_flags = 0;
2421 /*
2422 * If we're trying to select a
2423 * target ourselves, push our command
2424 * back into the ready list.
2425 */
2426 if (sc->sc_state == NCR_SELECTING) {
2427 NCR_MISC(("backoff selector "));
2428 callout_stop(&ecb->xs->xs_callout);
2429 ncr53c9x_dequeue(sc, ecb);
2430 TAILQ_INSERT_HEAD(&sc->ready_list, ecb, chain);
2431 ecb->flags |= ECB_READY;
2432 ecb = sc->sc_nexus = NULL;
2433 }
2434 sc->sc_state = NCR_RESELECTED;
2435 if (sc->sc_phase != MESSAGE_IN_PHASE) {
2436 /*
2437 * Things are seriously fucked up.
2438 * Pull the brakes, i.e. reset
2439 */
2440 printf("%s: target didn't identify\n",
2441 sc->sc_dev.dv_xname);
2442 ncr53c9x_init(sc, 1);
2443 return (1);
2444 }
2445 /*
2446 * The C90 only inhibits FIFO writes until
2447 * reselection is complete, instead of
2448 * waiting until the interrupt status register
2449 * has been read. So, if the reselect happens
2450 * while we were entering a command bytes (for
2451 * another target) some of those bytes can
2452 * appear in the FIFO here, after the
2453 * interrupt is taken.
2454 */
2455 nfifo = ncr53c9x_rdfifo(sc, NCR_RDFIFO_START);
2456
2457 if (nfifo < 2 ||
2458 (nfifo > 2 && sc->sc_rev != NCR_VARIANT_ESP100)) {
2459 printf("%s: RESELECT: %d bytes in FIFO! "
2460 "[intr %x, stat %x, step %d, "
2461 "prevphase %x]\n",
2462 sc->sc_dev.dv_xname,
2463 nfifo,
2464 sc->sc_espintr,
2465 sc->sc_espstat,
2466 sc->sc_espstep,
2467 sc->sc_prevphase);
2468 ncr53c9x_init(sc, 1);
2469 return (1);
2470 }
2471 sc->sc_selid = sc->sc_imess[0];
2472 NCR_MISC(("selid=%02x ", sc->sc_selid));
2473
2474 /* Handle identify message */
2475 ncr53c9x_msgin(sc);
2476 if (nfifo != 2) {
2477 /*
2478 * Note: this should not happen
2479 * with `dmaselect' on.
2480 */
2481 sc->sc_flags |= NCR_EXPECT_ILLCMD;
2482 NCRCMD(sc, NCRCMD_FLUSH);
2483 } else if (sc->sc_features & NCR_F_DMASELECT &&
2484 sc->sc_rev == NCR_VARIANT_ESP100) {
2485 sc->sc_flags |= NCR_EXPECT_ILLCMD;
2486 }
2487
2488 if (sc->sc_state != NCR_CONNECTED &&
2489 sc->sc_state != NCR_IDENTIFIED) {
2490 /* IDENTIFY fail?! */
2491 printf("%s: identify failed, "
2492 "state %d, intr %02x\n",
2493 sc->sc_dev.dv_xname, sc->sc_state,
2494 sc->sc_espintr);
2495 ncr53c9x_init(sc, 1);
2496 return (1);
2497 }
2498 goto shortcut; /* ie. next phase expected soon */
2499 }
2500
2501 #define NCRINTR_DONE (NCRINTR_FC|NCRINTR_BS)
2502 if ((sc->sc_espintr & NCRINTR_DONE) == NCRINTR_DONE) {
2503 /*
2504 * Arbitration won; examine the `step' register
2505 * to determine how far the selection could progress.
2506 */
2507 ecb = sc->sc_nexus;
2508 if (ecb == NULL)
2509 panic("ncr53c9x: no nexus");
2510
2511 sc_link = ecb->xs->sc_link;
2512 ti = &sc->sc_tinfo[sc_link->scsipi_scsi.target];
2513
2514 switch (sc->sc_espstep) {
2515 case 0:
2516 /*
2517 * The target did not respond with a
2518 * message out phase - probably an old
2519 * device that doesn't recognize ATN.
2520 * Clear ATN and just continue, the
2521 * target should be in the command
2522 * phase.
2523 * XXXX check for command phase?
2524 */
2525 NCRCMD(sc, NCRCMD_RSTATN);
2526 break;
2527 case 1:
2528 if ((ti->flags & T_NEGOTIATE) == 0 &&
2529 ecb->tag[0] == 0) {
2530 printf("%s: step 1 & !NEG\n",
2531 sc->sc_dev.dv_xname);
2532 goto reset;
2533 }
2534 if (sc->sc_phase != MESSAGE_OUT_PHASE) {
2535 printf("%s: !MSGOUT\n",
2536 sc->sc_dev.dv_xname);
2537 goto reset;
2538 }
2539 if (ti->flags & T_WIDE) {
2540 ncr53c9x_sched_msgout(SEND_WDTR);
2541 }
2542 if (ti->flags & T_NEGOTIATE) {
2543 /* Start negotiating */
2544 ti->period = sc->sc_minsync;
2545 ti->offset = 15;
2546 sc->sc_flags |= NCR_SYNCHNEGO;
2547 if (ecb->tag[0])
2548 ncr53c9x_sched_msgout(
2549 SEND_TAG|SEND_SDTR);
2550 else
2551 ncr53c9x_sched_msgout(
2552 SEND_SDTR);
2553 } else {
2554 /* Could not do ATN3 so send TAG */
2555 ncr53c9x_sched_msgout(SEND_TAG);
2556 }
2557 sc->sc_prevphase = MESSAGE_OUT_PHASE; /* XXXX */
2558 break;
2559 case 3:
2560 /*
2561 * Grr, this is supposed to mean
2562 * "target left command phase prematurely".
2563 * It seems to happen regularly when
2564 * sync mode is on.
2565 * Look at FIFO to see if command went out.
2566 * (Timing problems?)
2567 */
2568 if (sc->sc_features & NCR_F_DMASELECT) {
2569 if (sc->sc_cmdlen == 0)
2570 /* Hope for the best.. */
2571 break;
2572 } else if ((NCR_READ_REG(sc, NCR_FFLAG)
2573 & NCRFIFO_FF) == 0) {
2574 /* Hope for the best.. */
2575 break;
2576 }
2577 printf("(%s:%d:%d): selection failed;"
2578 " %d left in FIFO "
2579 "[intr %x, stat %x, step %d]\n",
2580 sc->sc_dev.dv_xname,
2581 sc_link->scsipi_scsi.target,
2582 sc_link->scsipi_scsi.lun,
2583 NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF,
2584 sc->sc_espintr, sc->sc_espstat,
2585 sc->sc_espstep);
2586 NCRCMD(sc, NCRCMD_FLUSH);
2587 ncr53c9x_sched_msgout(SEND_ABORT);
2588 return (1);
2589 case 2:
2590 /* Select stuck at Command Phase */
2591 NCRCMD(sc, NCRCMD_FLUSH);
2592 break;
2593 case 4:
2594 if (sc->sc_features & NCR_F_DMASELECT &&
2595 sc->sc_cmdlen != 0)
2596 printf("(%s:%d:%d): select; "
2597 "%lu left in DMA buffer "
2598 "[intr %x, stat %x, step %d]\n",
2599 sc->sc_dev.dv_xname,
2600 sc_link->scsipi_scsi.target,
2601 sc_link->scsipi_scsi.lun,
2602 (u_long)sc->sc_cmdlen,
2603 sc->sc_espintr,
2604 sc->sc_espstat,
2605 sc->sc_espstep);
2606 /* So far, everything went fine */
2607 break;
2608 }
2609
2610 sc->sc_prevphase = INVALID_PHASE; /* ?? */
2611 /* Do an implicit RESTORE POINTERS. */
2612 sc->sc_dp = ecb->daddr;
2613 sc->sc_dleft = ecb->dleft;
2614 sc->sc_state = NCR_CONNECTED;
2615 break;
2616
2617 } else {
2618
2619 printf("%s: unexpected status after select"
2620 ": [intr %x, stat %x, step %x]\n",
2621 sc->sc_dev.dv_xname,
2622 sc->sc_espintr, sc->sc_espstat, sc->sc_espstep);
2623 NCRCMD(sc, NCRCMD_FLUSH);
2624 DELAY(1);
2625 goto reset;
2626 }
2627 if (sc->sc_state == NCR_IDLE) {
2628 printf("%s: stray interrupt\n", sc->sc_dev.dv_xname);
2629 return (0);
2630 }
2631 break;
2632
2633 case NCR_CONNECTED:
2634 if ((sc->sc_flags & NCR_ICCS) != 0) {
2635 /* "Initiate Command Complete Steps" in progress */
2636 u_char msg;
2637
2638 sc->sc_flags &= ~NCR_ICCS;
2639
2640 if (!(sc->sc_espintr & NCRINTR_DONE)) {
2641 printf("%s: ICCS: "
2642 ": [intr %x, stat %x, step %x]\n",
2643 sc->sc_dev.dv_xname,
2644 sc->sc_espintr, sc->sc_espstat,
2645 sc->sc_espstep);
2646 }
2647 ncr53c9x_rdfifo(sc, NCR_RDFIFO_START);
2648 if (sc->sc_imlen < 2)
2649 printf("%s: can't get status, only %d bytes\n",
2650 sc->sc_dev.dv_xname, (int)sc->sc_imlen);
2651 ecb->stat = sc->sc_imess[sc->sc_imlen - 2];
2652 msg = sc->sc_imess[sc->sc_imlen - 1];
2653 NCR_PHASE(("<stat:(%x,%x)>", ecb->stat, msg));
2654 if (msg == MSG_CMDCOMPLETE) {
2655 ecb->dleft = (ecb->flags & ECB_TENTATIVE_DONE)
2656 ? 0 : sc->sc_dleft;
2657 if ((ecb->flags & ECB_SENSE) == 0)
2658 ecb->xs->resid = ecb->dleft;
2659 sc->sc_state = NCR_CMDCOMPLETE;
2660 } else
2661 printf("%s: STATUS_PHASE: msg %d\n",
2662 sc->sc_dev.dv_xname, msg);
2663 sc->sc_imlen = 0;
2664 NCRCMD(sc, NCRCMD_MSGOK);
2665 goto shortcut; /* ie. wait for disconnect */
2666 }
2667 break;
2668
2669 default:
2670 /* Don't panic: reset. */
2671 printf("%s: invalid state: %d",
2672 sc->sc_dev.dv_xname, sc->sc_state);
2673 ncr53c9x_scsi_reset(sc);
2674 goto out;
2675 #if 0
2676 panic("%s: invalid state: %d",
2677 sc->sc_dev.dv_xname, sc->sc_state);
2678 #endif
2679 break;
2680 }
2681
2682 /*
2683 * Driver is now in state NCR_CONNECTED, i.e. we
2684 * have a current command working the SCSI bus.
2685 */
2686 if (sc->sc_state != NCR_CONNECTED || ecb == NULL) {
2687 panic("ncr53c9x: no nexus");
2688 }
2689
2690 switch (sc->sc_phase) {
2691 case MESSAGE_OUT_PHASE:
2692 NCR_PHASE(("MESSAGE_OUT_PHASE "));
2693 ncr53c9x_msgout(sc);
2694 sc->sc_prevphase = MESSAGE_OUT_PHASE;
2695 break;
2696
2697 case MESSAGE_IN_PHASE:
2698 msgin:
2699 NCR_PHASE(("MESSAGE_IN_PHASE "));
2700 if ((sc->sc_espintr & NCRINTR_BS) != 0) {
2701 if ((sc->sc_rev != NCR_VARIANT_FAS366) ||
2702 !(sc->sc_espstat2 & FAS_STAT2_EMPTY)) {
2703 NCRCMD(sc, NCRCMD_FLUSH);
2704 }
2705 sc->sc_flags |= NCR_WAITI;
2706 NCRCMD(sc, NCRCMD_TRANS);
2707 } else if ((sc->sc_espintr & NCRINTR_FC) != 0) {
2708 if ((sc->sc_flags & NCR_WAITI) == 0) {
2709 printf("%s: MSGIN: unexpected FC bit: "
2710 "[intr %x, stat %x, step %x]\n",
2711 sc->sc_dev.dv_xname,
2712 sc->sc_espintr, sc->sc_espstat,
2713 sc->sc_espstep);
2714 }
2715 sc->sc_flags &= ~NCR_WAITI;
2716 ncr53c9x_rdfifo(sc,
2717 (sc->sc_prevphase == sc->sc_phase) ?
2718 NCR_RDFIFO_CONTINUE : NCR_RDFIFO_START);
2719 ncr53c9x_msgin(sc);
2720 } else {
2721 printf("%s: MSGIN: weird bits: "
2722 "[intr %x, stat %x, step %x]\n",
2723 sc->sc_dev.dv_xname,
2724 sc->sc_espintr, sc->sc_espstat, sc->sc_espstep);
2725 }
2726 sc->sc_prevphase = MESSAGE_IN_PHASE;
2727 goto shortcut; /* i.e. expect data to be ready */
2728 break;
2729
2730 case COMMAND_PHASE:
2731 /*
2732 * Send the command block. Normally we don't see this
2733 * phase because the SEL_ATN command takes care of
2734 * all this. However, we end up here if either the
2735 * target or we wanted to exchange some more messages
2736 * first (e.g. to start negotiations).
2737 */
2738
2739 NCR_PHASE(("COMMAND_PHASE 0x%02x (%d) ",
2740 ecb->cmd.cmd.opcode, ecb->clen));
2741 if (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF) {
2742 NCRCMD(sc, NCRCMD_FLUSH);
2743 /* DELAY(1);*/
2744 }
2745 if (sc->sc_features & NCR_F_DMASELECT) {
2746 size_t size;
2747 /* setup DMA transfer for command */
2748 size = ecb->clen;
2749 sc->sc_cmdlen = size;
2750 sc->sc_cmdp = (caddr_t)&ecb->cmd.cmd;
2751 NCRDMA_SETUP(sc, &sc->sc_cmdp, &sc->sc_cmdlen,
2752 0, &size);
2753 /* Program the SCSI counter */
2754 NCR_SET_COUNT(sc, size);
2755
2756 /* load the count in */
2757 NCRCMD(sc, NCRCMD_NOP|NCRCMD_DMA);
2758
2759 /* start the command transfer */
2760 NCRCMD(sc, NCRCMD_TRANS | NCRCMD_DMA);
2761 NCRDMA_GO(sc);
2762 } else {
2763 ncr53c9x_wrfifo(sc, (u_char *)&ecb->cmd.cmd, ecb->clen);
2764 NCRCMD(sc, NCRCMD_TRANS);
2765 }
2766 sc->sc_prevphase = COMMAND_PHASE;
2767 break;
2768
2769 case DATA_OUT_PHASE:
2770 NCR_PHASE(("DATA_OUT_PHASE [%ld] ",(long)sc->sc_dleft));
2771 NCRCMD(sc, NCRCMD_FLUSH);
2772 size = min(sc->sc_dleft, sc->sc_maxxfer);
2773 NCRDMA_SETUP(sc, &sc->sc_dp, &sc->sc_dleft, 0, &size);
2774 sc->sc_prevphase = DATA_OUT_PHASE;
2775 goto setup_xfer;
2776
2777 case DATA_IN_PHASE:
2778 NCR_PHASE(("DATA_IN_PHASE "));
2779 if (sc->sc_rev == NCR_VARIANT_ESP100)
2780 NCRCMD(sc, NCRCMD_FLUSH);
2781 size = min(sc->sc_dleft, sc->sc_maxxfer);
2782 NCRDMA_SETUP(sc, &sc->sc_dp, &sc->sc_dleft, 1, &size);
2783 sc->sc_prevphase = DATA_IN_PHASE;
2784 setup_xfer:
2785 /* Target returned to data phase: wipe "done" memory */
2786 ecb->flags &= ~ECB_TENTATIVE_DONE;
2787
2788 /* Program the SCSI counter */
2789 NCR_SET_COUNT(sc, size);
2790
2791 /* load the count in */
2792 NCRCMD(sc, NCRCMD_NOP|NCRCMD_DMA);
2793
2794 /*
2795 * Note that if `size' is 0, we've already transceived
2796 * all the bytes we want but we're still in DATA PHASE.
2797 * Apparently, the device needs padding. Also, a
2798 * transfer size of 0 means "maximum" to the chip
2799 * DMA logic.
2800 */
2801 NCRCMD(sc,
2802 (size == 0 ? NCRCMD_TRPAD : NCRCMD_TRANS) | NCRCMD_DMA);
2803 NCRDMA_GO(sc);
2804 return (1);
2805
2806 case STATUS_PHASE:
2807 NCR_PHASE(("STATUS_PHASE "));
2808 sc->sc_flags |= NCR_ICCS;
2809 NCRCMD(sc, NCRCMD_ICCS);
2810 sc->sc_prevphase = STATUS_PHASE;
2811 goto shortcut; /* i.e. expect status results soon */
2812 break;
2813
2814 case INVALID_PHASE:
2815 break;
2816
2817 default:
2818 printf("%s: unexpected bus phase; resetting\n",
2819 sc->sc_dev.dv_xname);
2820 goto reset;
2821 }
2822
2823 out:
2824 return (1);
2825
2826 reset:
2827 ncr53c9x_init(sc, 1);
2828 goto out;
2829
2830 finish:
2831 ncr53c9x_done(sc, ecb);
2832 goto out;
2833
2834 sched:
2835 sc->sc_state = NCR_IDLE;
2836 ncr53c9x_sched(sc);
2837 goto out;
2838
2839 shortcut:
2840 /*
2841 * The idea is that many of the SCSI operations take very little
2842 * time, and going away and getting interrupted is too high an
2843 * overhead to pay. For example, selecting, sending a message
2844 * and command and then doing some work can be done in one "pass".
2845 *
2846 * The delay is a heuristic. It is 2 when at 20MHz, 2 at 25MHz and 1
2847 * at 40MHz. This needs testing.
2848 */
2849 {
2850 struct timeval wait, cur;
2851
2852 microtime(&wait);
2853 wait.tv_usec += 50 / sc->sc_freq;
2854 if (wait.tv_usec > 1000000) {
2855 wait.tv_sec++;
2856 wait.tv_usec -= 1000000;
2857 }
2858 do {
2859 if (NCRDMA_ISINTR(sc))
2860 goto again;
2861 microtime(&cur);
2862 } while (cur.tv_sec <= wait.tv_sec &&
2863 cur.tv_usec <= wait.tv_usec);
2864 }
2865 goto out;
2866 }
2867
2868 void
2869 ncr53c9x_abort(sc, ecb)
2870 struct ncr53c9x_softc *sc;
2871 struct ncr53c9x_ecb *ecb;
2872 {
2873
2874 /* 2 secs for the abort */
2875 ecb->timeout = NCR_ABORT_TIMEOUT;
2876 ecb->flags |= ECB_ABORT;
2877
2878 if (ecb == sc->sc_nexus) {
2879 int timeout;
2880
2881 /*
2882 * If we're still selecting, the message will be scheduled
2883 * after selection is complete.
2884 */
2885 if (sc->sc_state == NCR_CONNECTED)
2886 ncr53c9x_sched_msgout(SEND_ABORT);
2887
2888 /*
2889 * Reschedule timeout.
2890 */
2891 timeout = ecb->timeout;
2892 if (hz > 100 && timeout > 1000)
2893 timeout = (timeout / 1000) * hz;
2894 else
2895 timeout = (timeout * hz) / 1000;
2896 callout_reset(&ecb->xs->xs_callout, timeout,
2897 ncr53c9x_timeout, ecb);
2898 } else {
2899 /*
2900 * Just leave the command where it is.
2901 * XXX - what choice do we have but to reset the SCSI
2902 * eventually?
2903 */
2904 if (sc->sc_state == NCR_IDLE)
2905 ncr53c9x_sched(sc);
2906 }
2907 }
2908
2909 void
2910 ncr53c9x_timeout(arg)
2911 void *arg;
2912 {
2913 struct ncr53c9x_ecb *ecb = arg;
2914 struct scsipi_xfer *xs = ecb->xs;
2915 struct scsipi_link *sc_link = xs->sc_link;
2916 struct ncr53c9x_softc *sc = sc_link->adapter_softc;
2917 struct ncr53c9x_tinfo *ti = &sc->sc_tinfo[sc_link->scsipi_scsi.target];
2918 int s;
2919
2920 scsi_print_addr(sc_link);
2921 printf("%s: timed out [ecb %p (flags 0x%x, dleft %x, stat %x)], "
2922 "<state %d, nexus %p, phase(l %x, c %x, p %x), resid %lx, "
2923 "msg(q %x,o %x) %s>",
2924 sc->sc_dev.dv_xname,
2925 ecb, ecb->flags, ecb->dleft, ecb->stat,
2926 sc->sc_state, sc->sc_nexus,
2927 NCR_READ_REG(sc, NCR_STAT),
2928 sc->sc_phase, sc->sc_prevphase,
2929 (long)sc->sc_dleft, sc->sc_msgpriq, sc->sc_msgout,
2930 NCRDMA_ISACTIVE(sc) ? "DMA active" : "");
2931 #if NCR53C9X_DEBUG > 1
2932 printf("TRACE: %s.", ecb->trace);
2933 #endif
2934
2935 s = splbio();
2936
2937 if (ecb->flags & ECB_ABORT) {
2938 /* abort timed out */
2939 printf(" AGAIN\n");
2940
2941 ncr53c9x_init(sc, 1);
2942 } else {
2943 /* abort the operation that has timed out */
2944 printf("\n");
2945 xs->error = XS_TIMEOUT;
2946 ncr53c9x_abort(sc, ecb);
2947
2948 /* Disable sync mode if stuck in a data phase */
2949 if (ecb == sc->sc_nexus &&
2950 (ti->flags & T_SYNCMODE) != 0 &&
2951 (sc->sc_phase & (MSGI|CDI)) == 0) {
2952 scsi_print_addr(sc_link);
2953 printf("sync negotiation disabled\n");
2954 sc->sc_cfflags |=
2955 (1 << (sc_link->scsipi_scsi.target + 8));
2956 }
2957 }
2958
2959 splx(s);
2960 }
2961
2962 void
2963 ncr53c9x_watch(arg)
2964 void *arg;
2965 {
2966 struct ncr53c9x_softc *sc = (struct ncr53c9x_softc *)arg;
2967 struct ncr53c9x_tinfo *ti;
2968 struct ncr53c9x_linfo *li;
2969 int t, s;
2970 /* Delete any structures that have not been used in 10min. */
2971 time_t old = time.tv_sec - (10 * 60);
2972
2973 s = splbio();
2974 for (t = 0; t < NCR_NTARG; t++) {
2975 ti = &sc->sc_tinfo[t];
2976 li = LIST_FIRST(&ti->luns);
2977 while (li) {
2978 if (li->last_used < old &&
2979 li->untagged == NULL &&
2980 li->used == 0) {
2981 if (li->lun < NCR_NLUN)
2982 ti->lun[li->lun] = NULL;
2983 LIST_REMOVE(li, link);
2984 free(li, M_DEVBUF);
2985 /* Restart the search at the beginning */
2986 li = LIST_FIRST(&ti->luns);
2987 continue;
2988 }
2989 li = LIST_NEXT(li, link);
2990 }
2991 }
2992 splx(s);
2993 callout_reset(&sc->sc_watchdog, 60 * hz, ncr53c9x_watch, sc);
2994 }
2995
2996