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