2 * Copyright (c) 2004 Scott Long
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 /* $NetBSD: ncr53c9x.c,v 1.114 2005/02/27 00:27:02 perry Exp $ */
31 * Copyright (c) 1998, 2002 The NetBSD Foundation, Inc.
32 * All rights reserved.
34 * This code is derived from software contributed to The NetBSD Foundation
35 * by Charles M. Hannum.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the NetBSD
48 * Foundation, Inc. and its contributors.
49 * 4. Neither the name of The NetBSD Foundation nor the names of its
50 * contributors may be used to endorse or promote products derived
51 * from this software without specific prior written permission.
53 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
54 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
55 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
56 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
57 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
58 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
59 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
60 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
61 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
62 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
63 * POSSIBILITY OF SUCH DAMAGE.
67 * Copyright (c) 1994 Peter Galbavy
68 * Copyright (c) 1995 Paul Kranenburg
69 * All rights reserved.
71 * Redistribution and use in source and binary forms, with or without
72 * modification, are permitted provided that the following conditions
74 * 1. Redistributions of source code must retain the above copyright
75 * notice, this list of conditions and the following disclaimer.
76 * 2. Redistributions in binary form must reproduce the above copyright
77 * notice, this list of conditions and the following disclaimer in the
78 * documentation and/or other materials provided with the distribution.
79 * 3. All advertising materials mentioning features or use of this software
80 * must display the following acknowledgement:
81 * This product includes software developed by Peter Galbavy
82 * 4. The name of the author may not be used to endorse or promote products
83 * derived from this software without specific prior written permission.
85 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
86 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
87 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
88 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
89 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
90 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
91 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
92 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
93 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
94 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
95 * POSSIBILITY OF SUCH DAMAGE.
99 * Based on aic6360 by Jarle Greipsland
101 * Acknowledgements: Many of the algorithms used in this driver are
102 * inspired by the work of Julian Elischer (julian@FreeBSD.org) and
103 * Charles Hannum (mycroft@duality.gnu.ai.mit.edu). Thanks a million!
106 #include <sys/cdefs.h>
107 __FBSDID("$FreeBSD$");
109 #include <sys/param.h>
110 #include <sys/systm.h>
112 #include <sys/kernel.h>
113 #include <sys/malloc.h>
114 #include <sys/resource.h>
115 #include <sys/lock.h>
116 #include <sys/mutex.h>
117 #include <sys/queue.h>
118 #include <sys/time.h>
119 #include <sys/callout.h>
122 #include <cam/cam_ccb.h>
123 #include <cam/cam_debug.h>
124 #include <cam/cam_sim.h>
125 #include <cam/cam_xpt_sim.h>
126 #include <cam/scsi/scsi_all.h>
127 #include <cam/scsi/scsi_message.h>
129 #include <dev/esp/ncr53c9xreg.h>
130 #include <dev/esp/ncr53c9xvar.h>
132 int ncr53c9x_debug = NCR_SHOWMISC /*|NCR_SHOWPHASE|NCR_SHOWTRAC|NCR_SHOWCMDS*/;
134 int ncr53c9x_notag = 0;
137 static void ncr53c9x_select(struct ncr53c9x_softc *, struct ncr53c9x_ecb *);
138 static int ncr53c9x_reselect(struct ncr53c9x_softc *, int, int, int);
139 static void ncr53c9x_scsi_reset(struct ncr53c9x_softc *);
140 static void ncr53c9x_poll(struct cam_sim *);
141 static void ncr53c9x_sched(struct ncr53c9x_softc *);
142 static void ncr53c9x_done(struct ncr53c9x_softc *, struct ncr53c9x_ecb *);
143 static void ncr53c9x_msgin(struct ncr53c9x_softc *);
144 static void ncr53c9x_msgout(struct ncr53c9x_softc *);
145 static void ncr53c9x_timeout(void *arg);
146 static void ncr53c9x_watch(void *arg);
147 static void ncr53c9x_abort(struct ncr53c9x_softc *, struct ncr53c9x_ecb *);
148 static void ncr53c9x_dequeue(struct ncr53c9x_softc *,
149 struct ncr53c9x_ecb *);
150 static void ncr53c9x_sense(struct ncr53c9x_softc *, struct ncr53c9x_ecb *);
151 static void ncr53c9x_free_ecb(struct ncr53c9x_softc *,
152 struct ncr53c9x_ecb *);
153 static void ncr53c9x_wrfifo(struct ncr53c9x_softc *, u_char *, int);
154 static int ncr53c9x_rdfifo(struct ncr53c9x_softc *, int);
156 static struct ncr53c9x_ecb *ncr53c9x_get_ecb(struct ncr53c9x_softc *);
157 static struct ncr53c9x_linfo *ncr53c9x_lunsearch(struct ncr53c9x_tinfo *,
160 static __inline void ncr53c9x_readregs(struct ncr53c9x_softc *);
161 static __inline int ncr53c9x_stp2cpb(struct ncr53c9x_softc *, int);
162 static __inline void ncr53c9x_setsync(struct ncr53c9x_softc *,
163 struct ncr53c9x_tinfo *);
165 #define NCR_RDFIFO_START 0
166 #define NCR_RDFIFO_CONTINUE 1
168 #define NCR_SET_COUNT(sc, size) do { \
169 NCR_WRITE_REG((sc), NCR_TCL, (size)); \
170 NCR_WRITE_REG((sc), NCR_TCM, (size) >> 8); \
171 if ((sc->sc_cfg2 & NCRCFG2_FE) || \
172 (sc->sc_rev == NCR_VARIANT_FAS366)) { \
173 NCR_WRITE_REG((sc), NCR_TCH, (size) >> 16); \
175 if (sc->sc_rev == NCR_VARIANT_FAS366) { \
176 NCR_WRITE_REG(sc, NCR_RCH, 0); \
182 (((ms) < 0x20000) ? \
183 ((ms +0u) / 1000u) * hz : \
184 ((ms +0u) * hz) /1000u)
188 * Names for the NCR53c9x variants, corresponding to the variant tags
191 static const char *ncr53c9x_variant_names[] = {
208 * Search linked list for LUN info by LUN id.
210 static struct ncr53c9x_linfo *
211 ncr53c9x_lunsearch(struct ncr53c9x_tinfo *ti, int64_t lun)
213 struct ncr53c9x_linfo *li;
214 LIST_FOREACH(li, &ti->luns, link)
221 * Attach this instance, and then all the sub-devices.
224 ncr53c9x_attach(struct ncr53c9x_softc *sc)
226 struct cam_devq *devq;
228 struct cam_path *path;
229 struct ncr53c9x_ecb *ecb;
232 mtx_init(&sc->sc_lock, "ncr", "ncr53c9x lock", MTX_DEF);
235 * Note, the front-end has set us up to print the chip variation.
237 if (sc->sc_rev >= NCR_VARIANT_MAX) {
238 device_printf(sc->sc_dev, "unknown variant %d, devices not "
239 "attached\n", sc->sc_rev);
243 device_printf(sc->sc_dev, "%s, %dMHz, SCSI ID %d\n",
244 ncr53c9x_variant_names[sc->sc_rev], sc->sc_freq, sc->sc_id);
246 sc->sc_ntarg = (sc->sc_rev == NCR_VARIANT_FAS366) ? 16 : 8;
249 * Allocate SCSI message buffers.
250 * Front-ends can override allocation to avoid alignment
251 * handling in the DMA engines. Note that that ncr53c9x_msgout()
252 * can request a 1 byte DMA transfer.
254 if (sc->sc_omess == NULL) {
255 sc->sc_omess_self = 1;
256 sc->sc_omess = malloc(NCR_MAX_MSG_LEN, M_DEVBUF, M_NOWAIT);
257 if (sc->sc_omess == NULL) {
258 device_printf(sc->sc_dev,
259 "cannot allocate MSGOUT buffer\n");
263 sc->sc_omess_self = 0;
265 if (sc->sc_imess == NULL) {
266 sc->sc_imess_self = 1;
267 sc->sc_imess = malloc(NCR_MAX_MSG_LEN + 1, M_DEVBUF, M_NOWAIT);
268 if (sc->sc_imess == NULL) {
269 device_printf(sc->sc_dev,
270 "cannot allocate MSGIN buffer\n");
275 sc->sc_imess_self = 0;
277 sc->sc_tinfo = malloc(sc->sc_ntarg * sizeof(sc->sc_tinfo[0]),
278 M_DEVBUF, M_NOWAIT | M_ZERO);
279 if (sc->sc_tinfo == NULL) {
280 device_printf(sc->sc_dev,
281 "cannot allocate target info buffer\n");
286 callout_init(&sc->sc_watchdog, 0);
289 * Treat NCR53C90 with the 86C01 DMA chip exactly as ESP100
292 if (sc->sc_rev == NCR_VARIANT_NCR53C90_86C01)
293 sc->sc_rev = NCR_VARIANT_ESP100;
295 sc->sc_ccf = FREQTOCCF(sc->sc_freq);
297 /* The value *must not* be == 1. Make it 2 */
302 * The recommended timeout is 250ms. This register is loaded
303 * with a value calculated as follows, from the docs:
305 * (timout period) x (CLK frequency)
306 * reg = -------------------------------------
307 * 8192 x (Clock Conversion Factor)
309 * Since CCF has a linear relation to CLK, this generally computes
310 * to the constant of 153.
312 sc->sc_timeout = ((250 * 1000) * sc->sc_freq) / (8192 * sc->sc_ccf);
314 /* CCF register only has 3 bits; 0 is actually 8 */
320 devq = cam_simq_alloc(sc->sc_ntarg);
322 device_printf(sc->sc_dev, "cannot allocate device queue\n");
327 sim = cam_sim_alloc(ncr53c9x_action, ncr53c9x_poll, "esp", sc,
328 device_get_unit(sc->sc_dev), &Giant, 1,
329 NCR_TAG_DEPTH, devq);
331 device_printf(sc->sc_dev, "cannot allocate SIM entry\n");
335 if (xpt_bus_register(sim, sc->sc_dev, 0) != CAM_SUCCESS) {
336 device_printf(sc->sc_dev, "cannot register bus\n");
341 if (xpt_create_path(&path, NULL, cam_sim_path(sim),
342 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD)
344 device_printf(sc->sc_dev, "cannot create path\n");
352 /* Reset state & bus */
354 sc->sc_cfflags = sc->sc_dev.dv_cfdata->cf_flags;
357 ncr53c9x_init(sc, 1);
359 TAILQ_INIT(&sc->free_list);
360 if ((sc->ecb_array = malloc(sizeof(struct ncr53c9x_ecb) * NCR_TAG_DEPTH,
361 M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL) {
362 device_printf(sc->sc_dev, "cannot allocate ECB array\n");
366 for (i = 0; i < NCR_TAG_DEPTH; i++) {
367 ecb = &sc->ecb_array[i];
370 TAILQ_INSERT_HEAD(&sc->free_list, ecb, free_links);
373 callout_reset(&sc->sc_watchdog, 60*hz, ncr53c9x_watch, sc);
380 xpt_bus_deregister(cam_sim_path(sim));
382 cam_sim_free(sim, TRUE);
386 free(sc->sc_tinfo, M_DEVBUF);
388 if (sc->sc_imess_self)
389 free(sc->sc_imess, M_DEVBUF);
391 if (sc->sc_omess_self)
392 free(sc->sc_omess, M_DEVBUF);
397 ncr53c9x_detach(struct ncr53c9x_softc *sc)
400 callout_drain(&sc->sc_watchdog);
401 mtx_lock(&sc->sc_lock);
402 ncr53c9x_init(sc, 1);
403 mtx_unlock(&sc->sc_lock);
404 xpt_free_path(sc->sc_path);
405 xpt_bus_deregister(cam_sim_path(sc->sc_sim));
406 cam_sim_free(sc->sc_sim, TRUE);
407 free(sc->ecb_array, M_DEVBUF);
408 free(sc->sc_tinfo, M_DEVBUF);
409 if (sc->sc_imess_self)
410 free(sc->sc_imess, M_DEVBUF);
411 if (sc->sc_omess_self)
412 free(sc->sc_omess, M_DEVBUF);
413 mtx_destroy(&sc->sc_lock);
419 * This is the generic ncr53c9x reset function. It does not reset the SCSI bus,
420 * only this controller, but kills any on-going commands, and also stops
421 * and resets the DMA.
423 * After reset, registers are loaded with the defaults from the attach
427 ncr53c9x_reset(struct ncr53c9x_softc *sc)
430 /* reset DMA first */
433 /* reset SCSI chip */
434 NCRCMD(sc, NCRCMD_RSTCHIP);
435 NCRCMD(sc, NCRCMD_NOP);
438 /* do these backwards, and fall through */
439 switch (sc->sc_rev) {
440 case NCR_VARIANT_ESP406:
441 case NCR_VARIANT_FAS408:
442 NCR_WRITE_REG(sc, NCR_CFG5, sc->sc_cfg5 | NCRCFG5_SINT);
443 NCR_WRITE_REG(sc, NCR_CFG4, sc->sc_cfg4);
444 case NCR_VARIANT_AM53C974:
445 case NCR_VARIANT_FAS100A:
446 case NCR_VARIANT_FAS216:
447 case NCR_VARIANT_FAS236:
448 case NCR_VARIANT_NCR53C94:
449 case NCR_VARIANT_NCR53C96:
450 case NCR_VARIANT_ESP200:
451 sc->sc_features |= NCR_F_HASCFG3;
452 NCR_WRITE_REG(sc, NCR_CFG3, sc->sc_cfg3);
453 case NCR_VARIANT_ESP100A:
454 sc->sc_features |= NCR_F_SELATN3;
455 NCR_WRITE_REG(sc, NCR_CFG2, sc->sc_cfg2);
456 case NCR_VARIANT_ESP100:
457 NCR_WRITE_REG(sc, NCR_CFG1, sc->sc_cfg1);
458 NCR_WRITE_REG(sc, NCR_CCF, sc->sc_ccf);
459 NCR_WRITE_REG(sc, NCR_SYNCOFF, 0);
460 NCR_WRITE_REG(sc, NCR_TIMEOUT, sc->sc_timeout);
463 case NCR_VARIANT_FAS366:
465 NCR_F_HASCFG3 | NCR_F_FASTSCSI | NCR_F_SELATN3;
466 sc->sc_cfg3 = NCRFASCFG3_FASTCLK | NCRFASCFG3_OBAUTO;
467 sc->sc_cfg3_fscsi = NCRFASCFG3_FASTSCSI;
468 NCR_WRITE_REG(sc, NCR_CFG3, sc->sc_cfg3);
469 sc->sc_cfg2 = 0; /* NCRCFG2_HMEFE| NCRCFG2_HME32 */
470 NCR_WRITE_REG(sc, NCR_CFG2, sc->sc_cfg2);
471 NCR_WRITE_REG(sc, NCR_CFG1, sc->sc_cfg1);
472 NCR_WRITE_REG(sc, NCR_CCF, sc->sc_ccf);
473 NCR_WRITE_REG(sc, NCR_SYNCOFF, 0);
474 NCR_WRITE_REG(sc, NCR_TIMEOUT, sc->sc_timeout);
478 device_printf(sc->sc_dev, "unknown revision code, "
479 "assuming ESP100\n");
480 NCR_WRITE_REG(sc, NCR_CFG1, sc->sc_cfg1);
481 NCR_WRITE_REG(sc, NCR_CCF, sc->sc_ccf);
482 NCR_WRITE_REG(sc, NCR_SYNCOFF, 0);
483 NCR_WRITE_REG(sc, NCR_TIMEOUT, sc->sc_timeout);
486 if (sc->sc_rev == NCR_VARIANT_AM53C974)
487 NCR_WRITE_REG(sc, NCR_AMDCFG4, sc->sc_cfg4);
490 device_printf(sc->sc_dev, "ncr53c9x_reset: revision %d\n",
492 device_printf(sc->sc_dev, "ncr53c9x_reset: cfg1 0x%x, cfg2 0x%x, "
493 "cfg3 0x%x, ccf 0x%x, timeout 0x%x\n",
494 sc->sc_cfg1, sc->sc_cfg2, sc->sc_cfg3, sc->sc_ccf, sc->sc_timeout);
499 * Reset the SCSI bus, but not the chip
502 ncr53c9x_scsi_reset(struct ncr53c9x_softc *sc)
505 (*sc->sc_glue->gl_dma_stop)(sc);
507 NCR_MISC(("%s: resetting SCSI bus\n", device_get_nameunit(sc->sc_dev)));
508 NCRCMD(sc, NCRCMD_RSTSCSI);
509 DELAY(250000); /* Give the bus a fighting chance to settle */
513 * Initialize ncr53c9x state machine
516 ncr53c9x_init(struct ncr53c9x_softc *sc, int doreset)
518 struct ncr53c9x_ecb *ecb;
519 struct ncr53c9x_linfo *li;
522 NCR_MISC(("[NCR_INIT(%d) %d] ", doreset, sc->sc_state));
524 if (sc->sc_state == 0) {
525 /* First time through; initialize. */
527 TAILQ_INIT(&sc->ready_list);
529 memset(sc->sc_tinfo, 0, sizeof(sc->sc_tinfo));
530 for (r = 0; r < sc->sc_ntarg; r++) {
531 LIST_INIT(&sc->sc_tinfo[r].luns);
534 /* Cancel any active commands. */
535 sc->sc_state = NCR_CLEANING;
537 if ((ecb = sc->sc_nexus) != NULL) {
538 ecb->ccb->ccb_h.status = CAM_CMD_TIMEOUT;
539 ncr53c9x_done(sc, ecb);
541 /* Cancel outstanding disconnected commands on each LUN */
542 for (r = 0; r < sc->sc_ntarg; r++) {
543 LIST_FOREACH(li, &sc->sc_tinfo[r].luns, link) {
544 if ((ecb = li->untagged) != NULL) {
549 * Should we terminate a command
550 * that never reached the disk?
553 ecb->ccb->ccb_h.status =
555 ncr53c9x_done(sc, ecb);
557 for (i = 0; i < 256; i++)
558 if ((ecb = li->queued[i])) {
559 li->queued[i] = NULL;
560 ecb->ccb->ccb_h.status =
562 ncr53c9x_done(sc, ecb);
570 * reset the chip to a known state
575 sc->sc_msgpriq = sc->sc_msgout = sc->sc_msgoutq = 0;
576 sc->sc_phase = sc->sc_prevphase = INVALID_PHASE;
578 for (r = 0; r < sc->sc_ntarg; r++) {
579 struct ncr53c9x_tinfo *ti = &sc->sc_tinfo[r];
580 /* XXX - config flags per target: low bits: no reselect; high bits: no synch */
582 ti->flags = ((sc->sc_minsync && !(sc->sc_cfflags & (1<<((r&7)+8))))
584 ((sc->sc_cfflags & (1<<(r&7))) ? T_RSELECTOFF : 0);
589 ti->period = sc->sc_minsync;
595 sc->sc_state = NCR_SBR;
596 NCRCMD(sc, NCRCMD_RSTSCSI);
598 sc->sc_state = NCR_IDLE;
604 * Read the NCR registers, and save their contents for later use.
605 * NCR_STAT, NCR_STEP & NCR_INTR are mostly zeroed out when reading
606 * NCR_INTR - so make sure it is the last read.
608 * I think that (from reading the docs) most bits in these registers
609 * only make sense when he DMA CSR has an interrupt showing. Call only
610 * if an interrupt is pending.
613 ncr53c9x_readregs(struct ncr53c9x_softc *sc)
616 sc->sc_espstat = NCR_READ_REG(sc, NCR_STAT);
617 /* Only the stepo bits are of interest */
618 sc->sc_espstep = NCR_READ_REG(sc, NCR_STEP) & NCRSTEP_MASK;
620 if (sc->sc_rev == NCR_VARIANT_FAS366)
621 sc->sc_espstat2 = NCR_READ_REG(sc, NCR_STAT2);
623 sc->sc_espintr = NCR_READ_REG(sc, NCR_INTR);
625 if (sc->sc_glue->gl_clear_latched_intr != NULL)
626 (*sc->sc_glue->gl_clear_latched_intr)(sc);
629 * Determine the SCSI bus phase, return either a real SCSI bus phase
630 * or some pseudo phase we use to detect certain exceptions.
633 sc->sc_phase = (sc->sc_espintr & NCRINTR_DIS) ?
634 /* Disconnected */ BUSFREE_PHASE : sc->sc_espstat & NCRSTAT_PHASE;
636 NCR_INTS(("regs[intr=%02x,stat=%02x,step=%02x,stat2=%02x] ",
637 sc->sc_espintr, sc->sc_espstat, sc->sc_espstep, sc->sc_espstat2));
641 * Convert Synchronous Transfer Period to chip register Clock Per Byte value.
644 ncr53c9x_stp2cpb(struct ncr53c9x_softc *sc, int period)
647 v = (sc->sc_freq * period) / 250;
648 if (ncr53c9x_cpb2stp(sc, v) < period)
649 /* Correct round-down error */
655 ncr53c9x_setsync(struct ncr53c9x_softc *sc, struct ncr53c9x_tinfo *ti)
657 u_char syncoff, synctp;
658 u_char cfg3 = sc->sc_cfg3 | ti->cfg3;
660 if (ti->flags & T_SYNCMODE) {
661 syncoff = ti->offset;
662 synctp = ncr53c9x_stp2cpb(sc, ti->period);
663 if (sc->sc_features & NCR_F_FASTSCSI) {
665 * If the period is 200ns or less (ti->period <= 50),
666 * put the chip in Fast SCSI mode.
668 if (ti->period <= 50)
670 * There are (at least) 4 variations of the
671 * configuration 3 register. The drive attach
672 * routine sets the appropriate bit to put the
673 * chip into Fast SCSI mode so that it doesn't
674 * have to be figured out here each time.
676 cfg3 |= sc->sc_cfg3_fscsi;
680 * Am53c974 requires different SYNCTP values when the
683 if (sc->sc_rev == NCR_VARIANT_AM53C974 &&
684 (cfg3 & NCRAMDCFG3_FSCSI) == 0)
691 if (sc->sc_features & NCR_F_HASCFG3)
692 NCR_WRITE_REG(sc, NCR_CFG3, cfg3);
694 NCR_WRITE_REG(sc, NCR_SYNCOFF, syncoff);
695 NCR_WRITE_REG(sc, NCR_SYNCTP, synctp);
699 * Send a command to a target, set the driver state to NCR_SELECTING
700 * and let the caller take care of the rest.
702 * Keeping this as a function allows me to say that this may be done
703 * by DMA instead of programmed I/O soon.
706 ncr53c9x_select(struct ncr53c9x_softc *sc, struct ncr53c9x_ecb *ecb)
708 int target = ecb->ccb->ccb_h.target_id;
709 int lun = ecb->ccb->ccb_h.target_lun;
710 struct ncr53c9x_tinfo *ti;
714 int selatn3, selatns;
717 NCR_TRACE(("[ncr53c9x_select(t%d,l%d,cmd:%x,tag:%x,%x)] ",
718 target, lun, ecb->cmd.cmd.opcode, ecb->tag[0], ecb->tag[1]));
720 ti = &sc->sc_tinfo[target];
722 sc->sc_state = NCR_SELECTING;
724 * Schedule the timeout now, the first time we will go away
725 * expecting to come back due to an interrupt, because it is
726 * always possible that the interrupt may never happen.
728 ecb->ccb->ccb_h.timeout_ch =
729 timeout(ncr53c9x_timeout, ecb, mstohz(ecb->timeout));
732 * The docs say the target register is never reset, and I
733 * can't think of a better place to set it
735 if (sc->sc_rev == NCR_VARIANT_FAS366) {
736 NCRCMD(sc, NCRCMD_FLUSH);
737 NCR_WRITE_REG(sc, NCR_SELID, target | NCR_BUSID_HME);
739 NCR_WRITE_REG(sc, NCR_SELID, target);
741 ncr53c9x_setsync(sc, ti);
743 if ((ecb->flags & ECB_SENSE) != 0) {
745 * For REQUEST SENSE, we should not send an IDENTIFY or
746 * otherwise mangle the target. There should be no MESSAGE IN
749 if (sc->sc_features & NCR_F_DMASELECT) {
750 /* setup DMA transfer for command */
751 dmasize = clen = ecb->clen;
752 sc->sc_cmdlen = clen;
753 sc->sc_cmdp = (caddr_t)&ecb->cmd.cmd;
755 /* Program the SCSI counter */
756 NCR_SET_COUNT(sc, dmasize);
758 if (sc->sc_rev != NCR_VARIANT_FAS366)
759 NCRCMD(sc, NCRCMD_NOP|NCRCMD_DMA);
761 /* And get the targets attention */
762 NCRCMD(sc, NCRCMD_SELNATN | NCRCMD_DMA);
763 NCRDMA_SETUP(sc, &sc->sc_cmdp, &sc->sc_cmdlen, 0,
767 ncr53c9x_wrfifo(sc, (u_char *)&ecb->cmd.cmd, ecb->clen);
768 NCRCMD(sc, NCRCMD_SELNATN);
773 selatn3 = selatns = 0;
774 if (ecb->tag[0] != 0) {
775 if (sc->sc_features & NCR_F_SELATN3)
776 /* use SELATN3 to send tag messages */
779 /* We don't have SELATN3; use SELATNS to send tags */
783 if (ti->flags & T_NEGOTIATE) {
784 /* We have to use SELATNS to send sync/wide messages */
789 cmd = (u_char *)&ecb->cmd.cmd;
792 /* We'll use tags with SELATN3 */
793 clen = ecb->clen + 3;
795 cmd[0] = MSG_IDENTIFY(lun, 1); /* msg[0] */
796 cmd[1] = ecb->tag[0]; /* msg[1] */
797 cmd[2] = ecb->tag[1]; /* msg[2] */
799 /* We don't have tags, or will send messages with SELATNS */
800 clen = ecb->clen + 1;
802 cmd[0] = MSG_IDENTIFY(lun, (tiflags & T_RSELECTOFF) == 0);
805 if ((sc->sc_features & NCR_F_DMASELECT) && !selatns) {
807 /* setup DMA transfer for command */
809 sc->sc_cmdlen = clen;
812 /* Program the SCSI counter */
813 NCR_SET_COUNT(sc, dmasize);
815 /* load the count in */
816 /* if (sc->sc_rev != NCR_VARIANT_FAS366) */
817 NCRCMD(sc, NCRCMD_NOP|NCRCMD_DMA);
819 /* And get the targets attention */
821 sc->sc_msgout = SEND_TAG;
822 sc->sc_flags |= NCR_ATN;
823 NCRCMD(sc, NCRCMD_SELATN3 | NCRCMD_DMA);
825 NCRCMD(sc, NCRCMD_SELATN | NCRCMD_DMA);
826 NCRDMA_SETUP(sc, &sc->sc_cmdp, &sc->sc_cmdlen, 0, &dmasize);
832 * Who am I? This is where we tell the target that we are
833 * happy for it to disconnect etc.
836 /* Now get the command into the FIFO */
837 ncr53c9x_wrfifo(sc, cmd, clen);
839 /* And get the targets attention */
841 NCR_MSGS(("SELATNS \n"));
842 /* Arbitrate, select and stop after IDENTIFY message */
843 NCRCMD(sc, NCRCMD_SELATNS);
844 } else if (selatn3) {
845 sc->sc_msgout = SEND_TAG;
846 sc->sc_flags |= NCR_ATN;
847 NCRCMD(sc, NCRCMD_SELATN3);
849 NCRCMD(sc, NCRCMD_SELATN);
853 ncr53c9x_free_ecb(struct ncr53c9x_softc *sc, struct ncr53c9x_ecb *ecb)
857 TAILQ_INSERT_TAIL(&sc->free_list, ecb, free_links);
861 static struct ncr53c9x_ecb *
862 ncr53c9x_get_ecb(struct ncr53c9x_softc *sc)
864 struct ncr53c9x_ecb *ecb;
866 ecb = TAILQ_FIRST(&sc->free_list);
869 panic("ecb flags not cleared\n");
870 TAILQ_REMOVE(&sc->free_list, ecb, free_links);
871 ecb->flags = ECB_ALLOC;
872 bzero(&ecb->ccb, sizeof(struct ncr53c9x_ecb) -
873 offsetof(struct ncr53c9x_ecb, ccb));
879 * DRIVER FUNCTIONS CALLABLE FROM HIGHER LEVEL DRIVERS:
883 * Start a SCSI-command
884 * This function is called by the higher level SCSI-driver to queue/run
889 ncr53c9x_action(struct cam_sim *sim, union ccb *ccb)
891 struct ncr53c9x_softc *sc;
892 struct ncr53c9x_ecb *ecb;
894 NCR_TRACE(("[ncr53c9x_action %d]", ccb->ccb_h.func_code));
896 sc = cam_sim_softc(sim);
897 mtx_lock(&sc->sc_lock);
899 switch (ccb->ccb_h.func_code) {
901 ncr53c9x_scsi_reset(sc);
902 ccb->ccb_h.status = CAM_REQ_CMP;
903 mtx_unlock(&sc->sc_lock);
906 case XPT_CALC_GEOMETRY:
907 mtx_unlock(&sc->sc_lock);
908 cam_calc_geometry(&ccb->ccg, sc->sc_extended_geom);
913 struct ccb_pathinq *cpi = &ccb->cpi;
915 cpi->version_num = 1;
916 cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE;
918 (sc->sc_rev == NCR_VARIANT_FAS366) ? PI_WIDE_16 : 0;
919 cpi->target_sprt = 0;
921 cpi->hba_eng_cnt = 0;
922 cpi->max_target = sc->sc_ntarg - 1;
924 cpi->initiator_id = sc->sc_id;
926 cpi->base_transfer_speed = 3300;
927 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
928 strncpy(cpi->hba_vid, "Sun", HBA_IDLEN);
929 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
930 cpi->unit_number = cam_sim_unit(sim);
931 cpi->transport = XPORT_SPI;
932 cpi->transport_version = 2;
933 cpi->protocol = PROTO_SCSI;
934 cpi->protocol_version = SCSI_REV_2;
935 ccb->ccb_h.status = CAM_REQ_CMP;
936 mtx_unlock(&sc->sc_lock);
940 case XPT_GET_TRAN_SETTINGS:
942 struct ccb_trans_settings *cts = &ccb->cts;
943 struct ncr53c9x_tinfo *ti = &sc->sc_tinfo[ccb->ccb_h.target_id];
944 struct ccb_trans_settings_scsi *scsi =
945 &cts->proto_specific.scsi;
946 struct ccb_trans_settings_spi *spi =
947 &cts->xport_specific.spi;
949 cts->protocol = PROTO_SCSI;
950 cts->protocol_version = SCSI_REV_2;
951 cts->transport = XPORT_SPI;
952 cts->transport_version = 2;
954 if (cts->type == CTS_TYPE_CURRENT_SETTINGS) {
955 spi->sync_period = ti->period;
956 spi->sync_offset = ti->offset;
957 spi->bus_width = ti->width;
958 if ((ti->flags & T_TAG) != 0) {
959 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
960 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
962 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
963 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
966 spi->sync_period = sc->sc_maxsync;
967 spi->sync_offset = sc->sc_maxoffset;
968 spi->bus_width = sc->sc_maxwidth;
969 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
970 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
973 CTS_SPI_VALID_BUS_WIDTH |
974 CTS_SPI_VALID_SYNC_RATE |
975 CTS_SPI_VALID_SYNC_OFFSET |
977 scsi->valid = CTS_SCSI_VALID_TQ;
978 ccb->ccb_h.status = CAM_REQ_CMP;
979 mtx_unlock(&sc->sc_lock);
984 printf("XPT_ABORT called\n");
985 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
986 mtx_unlock(&sc->sc_lock);
990 printf("XPT_TERM_IO called\n");
991 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
992 mtx_unlock(&sc->sc_lock);
996 printf("XPT_RESET_DEV called\n");
999 struct ccb_scsiio *csio;
1001 if (ccb->ccb_h.target_id < 0 ||
1002 ccb->ccb_h.target_id >= sc->sc_ntarg) {
1003 ccb->ccb_h.status = CAM_PATH_INVALID;
1004 mtx_unlock(&sc->sc_lock);
1008 /* Get an ECB to use. */
1009 ecb = ncr53c9x_get_ecb(sc);
1011 * This should never happen as we track resources
1015 xpt_freeze_simq(sim, 1);
1016 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1017 printf("unable to allocate ecb\n");
1018 mtx_unlock(&sc->sc_lock);
1023 /* Initialize ecb */
1025 ecb->timeout = ccb->ccb_h.timeout;
1027 if (ccb->ccb_h.func_code == XPT_RESET_DEV) {
1028 ecb->flags |= ECB_RESET;
1033 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0)
1034 bcopy(csio->cdb_io.cdb_ptr, &ecb->cmd.cmd,
1037 bcopy(csio->cdb_io.cdb_bytes, &ecb->cmd.cmd,
1039 ecb->clen = csio->cdb_len;
1040 ecb->daddr = csio->data_ptr;
1041 ecb->dleft = csio->dxfer_len;
1045 TAILQ_INSERT_TAIL(&sc->ready_list, ecb, chain);
1046 ecb->flags |= ECB_READY;
1047 if (sc->sc_state == NCR_IDLE)
1053 case XPT_SET_TRAN_SETTINGS:
1055 struct ccb_trans_settings *cts = &ccb->cts;
1056 int target = ccb->ccb_h.target_id;
1057 struct ncr53c9x_tinfo *ti = &sc->sc_tinfo[target];
1058 struct ccb_trans_settings_scsi *scsi =
1059 &cts->proto_specific.scsi;
1060 struct ccb_trans_settings_spi *spi =
1061 &cts->xport_specific.spi;
1063 if ((scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
1064 if ((sc->sc_cfflags & (1<<((target & 7) + 16))) == 0 &&
1065 (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB)) {
1066 NCR_MISC(("%s: target %d: tagged queuing\n",
1067 device_get_nameunit(sc->sc_dev), target));
1070 ti->flags &= ~T_TAG;
1073 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) {
1074 if (spi->bus_width != 0) {
1075 NCR_MISC(("%s: target %d: wide negotiation\n",
1076 device_get_nameunit(sc->sc_dev), target));
1077 if (sc->sc_rev == NCR_VARIANT_FAS366) {
1078 ti->flags |= T_WIDE;
1082 ti->flags &= ~T_WIDE;
1085 ti->flags |= T_NEGOTIATE;
1088 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) != 0) {
1089 NCR_MISC(("%s: target %d: sync period negotiation\n",
1090 device_get_nameunit(sc->sc_dev), target));
1091 ti->flags |= T_NEGOTIATE;
1092 ti->period = spi->sync_period;
1095 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0) {
1096 NCR_MISC(("%s: target %d: sync offset negotiation\n",
1097 device_get_nameunit(sc->sc_dev), target));
1098 ti->flags |= T_NEGOTIATE;
1099 ti->offset = spi->sync_offset;
1102 mtx_unlock(&sc->sc_lock);
1103 ccb->ccb_h.status = CAM_REQ_CMP;
1109 device_printf(sc->sc_dev, "Unhandled function code %d\n",
1110 ccb->ccb_h.func_code);
1111 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
1112 mtx_unlock(&sc->sc_lock);
1117 mtx_unlock(&sc->sc_lock);
1121 * Used when interrupt driven I/O is not allowed, e.g. during boot.
1124 ncr53c9x_poll(struct cam_sim *sim)
1126 struct ncr53c9x_softc *sc;
1128 NCR_TRACE(("[ncr53c9x_poll] "));
1129 sc = cam_sim_softc(sim);
1130 if (NCRDMA_ISINTR(sc)) {
1136 * LOW LEVEL SCSI UTILITIES
1140 * Schedule a scsi operation. This has now been pulled out of the interrupt
1141 * handler so that we may call it from ncr53c9x_scsipi_request and
1142 * ncr53c9x_done. This may save us an unnecessary interrupt just to get
1143 * things going. Should only be called when state == NCR_IDLE and at bio pl.
1146 ncr53c9x_sched(struct ncr53c9x_softc *sc)
1148 struct ncr53c9x_ecb *ecb;
1149 struct ncr53c9x_tinfo *ti;
1150 struct ncr53c9x_linfo *li;
1154 NCR_TRACE(("[ncr53c9x_sched] "));
1155 if (sc->sc_state != NCR_IDLE)
1156 panic("ncr53c9x_sched: not IDLE (state=%d)", sc->sc_state);
1159 * Find first ecb in ready queue that is for a target/lunit
1160 * combinations that is not busy.
1162 for (ecb = TAILQ_FIRST(&sc->ready_list); ecb != NULL;
1163 ecb = TAILQ_NEXT(ecb, chain)) {
1164 ti = &sc->sc_tinfo[ecb->ccb->ccb_h.target_id];
1165 lun = ecb->ccb->ccb_h.target_lun;
1167 /* Select type of tag for this command */
1168 if ((ti->flags & (T_RSELECTOFF)) != 0)
1170 else if ((ti->flags & (T_TAG)) == 0)
1172 else if ((ecb->flags & ECB_SENSE) != 0)
1174 else if ((ecb->ccb->ccb_h.flags & CAM_TAG_ACTION_VALID) == 0)
1176 else if (ecb->ccb->csio.tag_action == CAM_TAG_ACTION_NONE)
1179 tag = ecb->ccb->csio.tag_action;
1181 li = TINFO_LUN(ti, lun);
1183 /* Initialize LUN info and add to list. */
1184 if ((li = malloc(sizeof(*li),
1185 M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL) {
1190 LIST_INSERT_HEAD(&ti->luns, li, link);
1194 li->last_used = time_second;
1196 /* Try to issue this as an un-tagged command */
1197 if (li->untagged == NULL)
1200 if (li->untagged != NULL) {
1202 if ((li->busy != 1) && li->used == 0) {
1203 /* We need to issue this untagged command now */
1212 li->queued[ecb->tag_id] = ecb;
1213 ecb->tag[1] = ecb->tag_id;
1216 if (li->untagged != NULL && (li->busy != 1)) {
1218 TAILQ_REMOVE(&sc->ready_list, ecb, chain);
1219 ecb->flags &= ~ECB_READY;
1221 ncr53c9x_select(sc, ecb);
1224 if (li->untagged == NULL && tag != 0) {
1225 TAILQ_REMOVE(&sc->ready_list, ecb, chain);
1226 ecb->flags &= ~ECB_READY;
1228 ncr53c9x_select(sc, ecb);
1231 NCR_TRACE(("%d:%d busy\n",
1232 ecb->ccb->ccb_h.target_id,
1233 ecb->ccb->ccb_h.target_lun));
1238 ncr53c9x_sense(struct ncr53c9x_softc *sc, struct ncr53c9x_ecb *ecb)
1240 union ccb *ccb = ecb->ccb;
1241 struct ncr53c9x_tinfo *ti;
1242 struct scsi_request_sense *ss = (void *)&ecb->cmd.cmd;
1243 struct ncr53c9x_linfo *li;
1246 NCR_TRACE(("requesting sense "));
1248 lun = ccb->ccb_h.target_lun;
1249 ti = &sc->sc_tinfo[ccb->ccb_h.target_id];
1251 /* Next, setup a request sense command block */
1252 memset(ss, 0, sizeof(*ss));
1253 ss->opcode = REQUEST_SENSE;
1254 ss->byte2 = ccb->ccb_h.target_lun << SCSI_CMD_LUN_SHIFT;
1255 ss->length = sizeof(struct scsi_sense_data);
1256 ecb->clen = sizeof(*ss);
1257 ecb->daddr = (char *)&ecb->ccb->csio.sense_data;
1258 ecb->dleft = sizeof(struct scsi_sense_data);
1259 ecb->flags |= ECB_SENSE;
1260 ecb->timeout = NCR_SENSE_TIMEOUT;
1262 li = TINFO_LUN(ti, lun);
1265 ncr53c9x_dequeue(sc, ecb);
1266 li->untagged = ecb; /* must be executed first to fix C/A */
1268 if (ecb == sc->sc_nexus) {
1269 ncr53c9x_select(sc, ecb);
1271 TAILQ_INSERT_HEAD(&sc->ready_list, ecb, chain);
1272 ecb->flags |= ECB_READY;
1273 if (sc->sc_state == NCR_IDLE)
1279 * POST PROCESSING OF SCSI_CMD (usually current)
1282 ncr53c9x_done(struct ncr53c9x_softc *sc, struct ncr53c9x_ecb *ecb)
1284 union ccb *ccb = ecb->ccb;
1285 struct ncr53c9x_tinfo *ti;
1286 struct ncr53c9x_linfo *li;
1289 NCR_TRACE(("[ncr53c9x_done(status:%x)] ", ccb->ccb_h.status));
1291 ti = &sc->sc_tinfo[ccb->ccb_h.target_id];
1292 lun = ccb->ccb_h.target_lun;
1293 li = TINFO_LUN(ti, lun);
1295 untimeout(ncr53c9x_timeout, ecb, ccb->ccb_h.timeout_ch);
1298 * Now, if we've come here with no error code, i.e. we've kept the
1299 * initial XS_NOERROR, and the status code signals that we should
1300 * check sense, we'll need to set up a request sense cmd block and
1301 * push the command back into the ready queue *before* any other
1302 * commands for this target/lunit, else we lose the sense info.
1303 * We don't support chk sense conditions for the request sense cmd.
1305 if (ccb->ccb_h.status == CAM_REQ_CMP) {
1306 if ((ecb->flags & ECB_ABORT) != 0) {
1307 ccb->ccb_h.status = CAM_CMD_TIMEOUT;
1308 } else if ((ecb->flags & ECB_SENSE) != 0 &&
1309 (ecb->stat != SCSI_STATUS_CHECK_COND)) {
1310 ccb->ccb_h.status = CAM_AUTOSNS_VALID;
1311 } else if (ecb->stat == SCSI_STATUS_CHECK_COND) {
1312 if ((ecb->flags & ECB_SENSE) != 0)
1313 ccb->ccb_h.status = CAM_AUTOSENSE_FAIL;
1315 /* First, save the return values */
1316 ccb->csio.resid = ecb->dleft;
1317 ncr53c9x_sense(sc, ecb);
1321 ccb->csio.resid = ecb->dleft;
1324 if (xs->status == SCSI_QUEUE_FULL || xs->status == XS_BUSY)
1325 xs->error = XS_BUSY;
1329 #ifdef NCR53C9X_DEBUG
1330 if (ncr53c9x_debug & NCR_SHOWTRAC) {
1331 if (ccb->csio.resid != 0)
1332 printf("resid=%d ", ccb->csio.resid);
1334 if (xs->error == XS_SENSE)
1335 printf("sense=0x%02x\n",
1336 xs->sense.scsi_sense.error_code);
1338 printf("error=%d\n", xs->error);
1344 * Remove the ECB from whatever queue it's on.
1346 ncr53c9x_dequeue(sc, ecb);
1347 if (ecb == sc->sc_nexus) {
1348 sc->sc_nexus = NULL;
1349 if (sc->sc_state != NCR_CLEANING) {
1350 sc->sc_state = NCR_IDLE;
1355 if (ccb->ccb_h.status == CAM_SEL_TIMEOUT) {
1356 /* Selection timeout -- discard this LUN if empty */
1357 if (li->untagged == NULL && li->used == 0) {
1359 ti->lun[lun] = NULL;
1360 LIST_REMOVE(li, link);
1365 ncr53c9x_free_ecb(sc, ecb);
1371 ncr53c9x_dequeue(struct ncr53c9x_softc *sc, struct ncr53c9x_ecb *ecb)
1373 struct ncr53c9x_tinfo *ti;
1374 struct ncr53c9x_linfo *li;
1377 ti = &sc->sc_tinfo[ecb->ccb->ccb_h.target_id];
1378 lun = ecb->ccb->ccb_h.target_lun;
1379 li = TINFO_LUN(ti, lun);
1381 if (li == NULL || li->lun != lun)
1382 panic("ncr53c9x_dequeue: lun %qx for ecb %p does not exist",
1383 (long long) lun, ecb);
1385 if (li->untagged == ecb) {
1387 li->untagged = NULL;
1389 if (ecb->tag[0] && li->queued[ecb->tag[1]] != NULL) {
1391 if (li->queued[ecb->tag[1]] != NULL &&
1392 (li->queued[ecb->tag[1]] != ecb))
1393 panic("ncr53c9x_dequeue: slot %d for lun %qx has %p "
1394 "instead of ecb %p\n", ecb->tag[1],
1396 li->queued[ecb->tag[1]], ecb);
1398 li->queued[ecb->tag[1]] = NULL;
1402 if ((ecb->flags & ECB_READY) != 0) {
1403 ecb->flags &= ~ECB_READY;
1404 TAILQ_REMOVE(&sc->ready_list, ecb, chain);
1409 * INTERRUPT/PROTOCOL ENGINE
1413 * Schedule an outgoing message by prioritizing it, and asserting
1414 * attention on the bus. We can only do this when we are the initiator
1415 * else there will be an illegal command interrupt.
1417 #define ncr53c9x_sched_msgout(m) \
1419 NCR_MSGS(("ncr53c9x_sched_msgout %x %d", m, __LINE__)); \
1420 NCRCMD(sc, NCRCMD_SETATN); \
1421 sc->sc_flags |= NCR_ATN; \
1422 sc->sc_msgpriq |= (m); \
1426 ncr53c9x_flushfifo(struct ncr53c9x_softc *sc)
1428 NCR_TRACE(("[flushfifo] "));
1430 NCRCMD(sc, NCRCMD_FLUSH);
1432 if (sc->sc_phase == COMMAND_PHASE ||
1433 sc->sc_phase == MESSAGE_OUT_PHASE)
1438 ncr53c9x_rdfifo(struct ncr53c9x_softc *sc, int how)
1444 case NCR_RDFIFO_START:
1448 case NCR_RDFIFO_CONTINUE:
1449 buf = sc->sc_imess + sc->sc_imlen;
1452 panic("ncr53c9x_rdfifo: bad flag");
1457 * XXX buffer (sc_imess) size for message
1460 n = NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF;
1462 if (sc->sc_rev == NCR_VARIANT_FAS366) {
1465 for (i = 0; i < n; i++)
1466 buf[i] = NCR_READ_REG(sc, NCR_FIFO);
1468 if (sc->sc_espstat2 & NCRFAS_STAT2_ISHUTTLE) {
1470 NCR_WRITE_REG(sc, NCR_FIFO, 0);
1471 buf[i++] = NCR_READ_REG(sc, NCR_FIFO);
1473 NCR_READ_REG(sc, NCR_FIFO);
1475 ncr53c9x_flushfifo(sc);
1478 for (i = 0; i < n; i++)
1479 buf[i] = NCR_READ_REG(sc, NCR_FIFO);
1485 #ifdef NCR53C9X_DEBUG
1489 NCR_TRACE(("\n[rdfifo %s (%d):",
1490 (how == NCR_RDFIFO_START) ? "start" : "cont",
1491 (int)sc->sc_imlen));
1492 if (ncr53c9x_debug & NCR_SHOWTRAC) {
1493 for (j = 0; j < sc->sc_imlen; j++)
1494 printf(" %02x", sc->sc_imess[j]);
1500 return sc->sc_imlen;
1504 ncr53c9x_wrfifo(struct ncr53c9x_softc *sc, u_char *p, int len)
1508 #ifdef NCR53C9X_DEBUG
1509 NCR_MSGS(("[wrfifo(%d):", len));
1510 if (ncr53c9x_debug & NCR_SHOWMSGS) {
1511 for (i = 0; i < len; i++)
1512 printf(" %02x", p[i]);
1517 for (i = 0; i < len; i++) {
1518 NCR_WRITE_REG(sc, NCR_FIFO, p[i]);
1520 if (sc->sc_rev == NCR_VARIANT_FAS366)
1521 NCR_WRITE_REG(sc, NCR_FIFO, 0);
1526 ncr53c9x_reselect(struct ncr53c9x_softc *sc, int message, int tagtype,
1529 u_char selid, target, lun;
1530 struct ncr53c9x_ecb *ecb = NULL;
1531 struct ncr53c9x_tinfo *ti;
1532 struct ncr53c9x_linfo *li;
1535 if (sc->sc_rev == NCR_VARIANT_FAS366) {
1536 target = sc->sc_selid;
1539 * The SCSI chip made a snapshot of the data bus
1540 * while the reselection was being negotiated.
1541 * This enables us to determine which target did
1544 selid = sc->sc_selid & ~(1 << sc->sc_id);
1545 if (selid & (selid - 1)) {
1546 device_printf(sc->sc_dev, "reselect with invalid "
1547 "selid %02x; sending DEVICE RESET\n", selid);
1551 target = ffs(selid) - 1;
1553 lun = message & 0x07;
1556 * Search wait queue for disconnected cmd
1557 * The list should be short, so I haven't bothered with
1558 * any more sophisticated structures than a simple
1559 * singly linked list.
1561 ti = &sc->sc_tinfo[target];
1562 li = TINFO_LUN(ti, lun);
1565 * We can get as far as the LUN with the IDENTIFY
1566 * message. Check to see if we're running an
1567 * un-tagged command. Otherwise ack the IDENTIFY
1568 * and wait for a tag message.
1571 if (li->untagged != NULL && li->busy)
1573 else if (tagtype != MSG_SIMPLE_Q_TAG) {
1574 /* Wait for tag to come by */
1575 sc->sc_state = NCR_IDENTIFIED;
1578 ecb = li->queued[tagid];
1581 device_printf(sc->sc_dev, "reselect from target %d lun %d "
1582 "tag %x:%x with no nexus; sending ABORT\n",
1583 target, lun, tagtype, tagid);
1587 /* Make this nexus active again. */
1588 sc->sc_state = NCR_CONNECTED;
1590 ncr53c9x_setsync(sc, ti);
1592 if (ecb->flags & ECB_RESET)
1593 ncr53c9x_sched_msgout(SEND_DEV_RESET);
1594 else if (ecb->flags & ECB_ABORT)
1595 ncr53c9x_sched_msgout(SEND_ABORT);
1597 /* Do an implicit RESTORE POINTERS. */
1598 sc->sc_dp = ecb->daddr;
1599 sc->sc_dleft = ecb->dleft;
1604 ncr53c9x_sched_msgout(SEND_DEV_RESET);
1608 ncr53c9x_sched_msgout(SEND_ABORT);
1612 /* From NetBSD. These should go into CAM at some point */
1613 #define MSG_ISEXTENDED(m) ((m) == MSG_EXTENDED)
1614 #define MSG_IS1BYTE(m) \
1615 ((!MSG_ISEXTENDED(m) && (m) < 0x20) || MSG_ISIDENTIFY(m))
1616 #define MSG_IS2BYTE(m) (((m) & 0xf0) == 0x20)
1619 __verify_msg_format(u_char *p, int len)
1622 if (len == 1 && MSG_IS1BYTE(p[0]))
1624 if (len == 2 && MSG_IS2BYTE(p[0]))
1626 if (len >= 3 && MSG_ISEXTENDED(p[0]) &&
1634 * Get an incoming message as initiator.
1636 * The SCSI bus must already be in MESSAGE_IN_PHASE and there is a
1640 ncr53c9x_msgin(struct ncr53c9x_softc *sc)
1643 NCR_TRACE(("[ncr53c9x_msgin(curmsglen:%ld)] ", (long)sc->sc_imlen));
1645 if (sc->sc_imlen == 0) {
1646 device_printf(sc->sc_dev, "msgin: no msg byte available\n");
1651 * Prepare for a new message. A message should (according
1652 * to the SCSI standard) be transmitted in one single
1653 * MESSAGE_IN_PHASE. If we have been in some other phase,
1654 * then this is a new message.
1656 if (sc->sc_prevphase != MESSAGE_IN_PHASE &&
1657 sc->sc_state != NCR_RESELECTED) {
1658 device_printf(sc->sc_dev, "phase change, dropping message, "
1659 "prev %d, state %d\n", sc->sc_prevphase, sc->sc_state);
1660 sc->sc_flags &= ~NCR_DROP_MSGI;
1665 * If we're going to reject the message, don't bother storing
1666 * the incoming bytes. But still, we need to ACK them.
1668 if ((sc->sc_flags & NCR_DROP_MSGI) != 0) {
1669 NCRCMD(sc, NCRCMD_MSGOK);
1670 printf("<dropping msg byte %x>", sc->sc_imess[sc->sc_imlen]);
1674 if (sc->sc_imlen >= NCR_MAX_MSG_LEN) {
1675 ncr53c9x_sched_msgout(SEND_REJECT);
1676 sc->sc_flags |= NCR_DROP_MSGI;
1681 switch (sc->sc_state) {
1683 * if received message is the first of reselection
1684 * then first byte is selid, and then message
1686 case NCR_RESELECTED:
1687 pb = sc->sc_imess + 1;
1688 plen = sc->sc_imlen - 1;
1692 plen = sc->sc_imlen;
1696 if (__verify_msg_format(pb, plen))
1700 /* Ack what we have so far */
1701 NCRCMD(sc, NCRCMD_MSGOK);
1705 NCR_MSGS(("gotmsg(%x) state %d", sc->sc_imess[0], sc->sc_state));
1706 /* We got a complete message, flush the imess, */
1707 /* XXX nobody uses imlen below */
1710 * Now we should have a complete message (1 byte, 2 byte
1711 * and moderately long extended messages). We only handle
1712 * extended messages which total length is shorter than
1713 * NCR_MAX_MSG_LEN. Longer messages will be amputated.
1715 switch (sc->sc_state) {
1716 struct ncr53c9x_ecb *ecb;
1717 struct ncr53c9x_tinfo *ti;
1718 struct ncr53c9x_linfo *li;
1723 ti = &sc->sc_tinfo[ecb->ccb->ccb_h.target_id];
1725 switch (sc->sc_imess[0]) {
1726 case MSG_CMDCOMPLETE:
1727 NCR_MSGS(("cmdcomplete "));
1728 if (sc->sc_dleft < 0) {
1729 xpt_print_path(ecb->ccb->ccb_h.path);
1730 printf("got %ld extra bytes\n",
1731 -(long)sc->sc_dleft);
1734 ecb->dleft = (ecb->flags & ECB_TENTATIVE_DONE) ?
1736 if ((ecb->flags & ECB_SENSE) == 0)
1737 ecb->ccb->csio.resid = ecb->dleft;
1738 sc->sc_state = NCR_CMDCOMPLETE;
1741 case MSG_MESSAGE_REJECT:
1742 NCR_MSGS(("msg reject (msgout=%x) ", sc->sc_msgout));
1743 switch (sc->sc_msgout) {
1746 * Target does not like tagged queuing.
1747 * - Flush the command queue
1748 * - Disable tagged queuing for the target
1749 * - Dequeue ecb from the queued array.
1751 device_printf(sc->sc_dev, "tagged queuing "
1752 "rejected: target %d\n",
1753 ecb->ccb->ccb_h.target_id);
1755 NCR_MSGS(("(rejected sent tag)"));
1756 NCRCMD(sc, NCRCMD_FLUSH);
1758 ti->flags &= ~T_TAG;
1759 lun = ecb->ccb->ccb_h.target_lun;
1760 li = TINFO_LUN(ti, lun);
1762 li->queued[ecb->tag[1]] != NULL) {
1763 li->queued[ecb->tag[1]] = NULL;
1766 ecb->tag[0] = ecb->tag[1] = 0;
1772 device_printf(sc->sc_dev, "sync transfer "
1773 "rejected: target %d\n",
1774 ecb->ccb->ccb_h.target_id);
1776 sc->sc_flags &= ~NCR_SYNCHNEGO;
1777 ti->flags &= ~(T_NEGOTIATE | T_SYNCMODE);
1778 ncr53c9x_setsync(sc, ti);
1782 device_printf(sc->sc_dev, "wide transfer "
1783 "rejected: target %d\n",
1784 ecb->ccb->ccb_h.target_id);
1785 ti->flags &= ~(T_WIDE | T_WDTRSENT);
1789 case SEND_INIT_DET_ERR:
1795 NCR_MSGS(("noop "));
1798 case MSG_HEAD_OF_Q_TAG:
1799 case MSG_SIMPLE_Q_TAG:
1800 case MSG_ORDERED_Q_TAG:
1801 NCR_MSGS(("TAG %x:%x",
1802 sc->sc_imess[0], sc->sc_imess[1]));
1805 case MSG_DISCONNECT:
1806 NCR_MSGS(("disconnect "));
1808 sc->sc_state = NCR_DISCONNECT;
1811 * Mark the fact that all bytes have moved. The
1812 * target may not bother to do a SAVE POINTERS
1813 * at this stage. This flag will set the residual
1814 * count to zero on MSG COMPLETE.
1816 if (sc->sc_dleft == 0)
1817 ecb->flags |= ECB_TENTATIVE_DONE;
1821 case MSG_SAVEDATAPOINTER:
1822 NCR_MSGS(("save datapointer "));
1823 ecb->daddr = sc->sc_dp;
1824 ecb->dleft = sc->sc_dleft;
1827 case MSG_RESTOREPOINTERS:
1828 NCR_MSGS(("restore datapointer "));
1829 sc->sc_dp = ecb->daddr;
1830 sc->sc_dleft = ecb->dleft;
1834 NCR_MSGS(("extended(%x) ", sc->sc_imess[2]));
1835 switch (sc->sc_imess[2]) {
1837 NCR_MSGS(("SDTR period %d, offset %d ",
1838 sc->sc_imess[3], sc->sc_imess[4]));
1839 if (sc->sc_imess[1] != 3)
1841 ti->period = sc->sc_imess[3];
1842 ti->offset = sc->sc_imess[4];
1843 ti->flags &= ~T_NEGOTIATE;
1844 if (sc->sc_minsync == 0 ||
1848 #ifdef NCR53C9X_DEBUG
1849 xpt_print_path(ecb->ccb->ccb_h.path);
1850 printf("async mode\n");
1853 ti->flags &= ~T_SYNCMODE;
1854 if ((sc->sc_flags&NCR_SYNCHNEGO) == 0) {
1856 * target initiated negotiation
1859 ncr53c9x_sched_msgout(
1865 p = ncr53c9x_stp2cpb(sc, ti->period);
1866 ti->period = ncr53c9x_cpb2stp(sc, p);
1867 if ((sc->sc_flags&NCR_SYNCHNEGO) == 0) {
1869 * target initiated negotiation
1871 if (ti->period < sc->sc_minsync)
1874 if (ti->offset > 15)
1876 ti->flags &= ~T_SYNCMODE;
1877 ncr53c9x_sched_msgout(
1881 ti->flags |= T_SYNCMODE;
1884 sc->sc_flags &= ~NCR_SYNCHNEGO;
1885 ncr53c9x_setsync(sc, ti);
1889 #ifdef NCR53C9X_DEBUG
1890 device_printf(sc->sc_dev, "wide mode %d\n",
1893 if (sc->sc_imess[3] == 1) {
1894 ti->cfg3 |= NCRFASCFG3_EWIDE;
1895 ncr53c9x_setsync(sc, ti);
1899 * Device started width negotiation.
1901 if (!(ti->flags & T_WDTRSENT))
1902 ncr53c9x_sched_msgout(SEND_WDTR);
1903 ti->flags &= ~(T_WIDE | T_WDTRSENT);
1906 xpt_print_path(ecb->ccb->ccb_h.path);
1907 printf("unrecognized MESSAGE EXTENDED;"
1908 " sending REJECT\n");
1914 NCR_MSGS(("ident "));
1915 xpt_print_path(ecb->ccb->ccb_h.path);
1916 printf("unrecognized MESSAGE; sending REJECT\n");
1918 ncr53c9x_sched_msgout(SEND_REJECT);
1923 case NCR_IDENTIFIED:
1925 * IDENTIFY message was received and queue tag is expected now
1927 if ((sc->sc_imess[0] != MSG_SIMPLE_Q_TAG) ||
1928 (sc->sc_msgify == 0)) {
1929 device_printf(sc->sc_dev, "TAG reselect without "
1930 "IDENTIFY; MSG %x; sending DEVICE RESET\n",
1934 (void) ncr53c9x_reselect(sc, sc->sc_msgify,
1935 sc->sc_imess[0], sc->sc_imess[1]);
1938 case NCR_RESELECTED:
1939 if (MSG_ISIDENTIFY(sc->sc_imess[1])) {
1940 sc->sc_msgify = sc->sc_imess[1];
1942 device_printf(sc->sc_dev, "reselect without IDENTIFY;"
1943 " MSG %x; sending DEVICE RESET\n", sc->sc_imess[1]);
1946 (void) ncr53c9x_reselect(sc, sc->sc_msgify, 0, 0);
1950 device_printf(sc->sc_dev, "unexpected MESSAGE IN; "
1951 "sending DEVICE RESET\n");
1953 ncr53c9x_sched_msgout(SEND_DEV_RESET);
1957 ncr53c9x_sched_msgout(SEND_ABORT);
1961 /* if we have more messages to send set ATN */
1963 NCRCMD(sc, NCRCMD_SETATN);
1965 /* Ack last message byte */
1966 NCRCMD(sc, NCRCMD_MSGOK);
1968 /* Done, reset message pointer. */
1969 sc->sc_flags &= ~NCR_DROP_MSGI;
1975 * Send the highest priority, scheduled message
1978 ncr53c9x_msgout(struct ncr53c9x_softc *sc)
1980 struct ncr53c9x_tinfo *ti;
1981 struct ncr53c9x_ecb *ecb;
1984 NCR_TRACE(("[ncr53c9x_msgout(priq:%x, prevphase:%x)]",
1985 sc->sc_msgpriq, sc->sc_prevphase));
1988 * XXX - the NCR_ATN flag is not in sync with the actual ATN
1989 * condition on the SCSI bus. The 53c9x chip
1990 * automatically turns off ATN before sending the
1991 * message byte. (See also the comment below in the
1992 * default case when picking out a message to send.)
1994 if (sc->sc_flags & NCR_ATN) {
1995 if (sc->sc_prevphase != MESSAGE_OUT_PHASE) {
1997 NCRCMD(sc, NCRCMD_FLUSH);
2003 if (sc->sc_prevphase == MESSAGE_OUT_PHASE) {
2004 ncr53c9x_sched_msgout(sc->sc_msgoutq);
2007 device_printf(sc->sc_dev, "at line %d: unexpected "
2008 "MESSAGE OUT phase\n", __LINE__);
2012 if (sc->sc_omlen == 0) {
2013 /* Pick up highest priority message */
2014 sc->sc_msgout = sc->sc_msgpriq & -sc->sc_msgpriq;
2015 sc->sc_msgoutq |= sc->sc_msgout;
2016 sc->sc_msgpriq &= ~sc->sc_msgout;
2017 sc->sc_omlen = 1; /* "Default" message len */
2018 switch (sc->sc_msgout) {
2021 ti = &sc->sc_tinfo[ecb->ccb->ccb_h.target_id];
2022 sc->sc_omess[0] = MSG_EXTENDED;
2023 sc->sc_omess[1] = MSG_EXT_SDTR_LEN;
2024 sc->sc_omess[2] = MSG_EXT_SDTR;
2025 sc->sc_omess[3] = ti->period;
2026 sc->sc_omess[4] = ti->offset;
2028 if ((sc->sc_flags & NCR_SYNCHNEGO) == 0) {
2029 ti->flags |= T_SYNCMODE;
2030 ncr53c9x_setsync(sc, ti);
2035 ti = &sc->sc_tinfo[ecb->ccb->ccb_h.target_id];
2036 sc->sc_omess[0] = MSG_EXTENDED;
2037 sc->sc_omess[1] = MSG_EXT_WDTR_LEN;
2038 sc->sc_omess[2] = MSG_EXT_WDTR;
2039 sc->sc_omess[3] = ti->width;
2043 if (sc->sc_state != NCR_CONNECTED) {
2044 device_printf(sc->sc_dev, "at line %d: no "
2045 "nexus\n", __LINE__);
2049 MSG_IDENTIFY(ecb->ccb->ccb_h.target_lun, 0);
2052 if (sc->sc_state != NCR_CONNECTED) {
2053 device_printf(sc->sc_dev, "at line %d: no "
2054 "nexus\n", __LINE__);
2057 sc->sc_omess[0] = ecb->tag[0];
2058 sc->sc_omess[1] = ecb->tag[1];
2061 case SEND_DEV_RESET:
2062 sc->sc_flags |= NCR_ABORTING;
2063 sc->sc_omess[0] = MSG_BUS_DEV_RESET;
2065 ti = &sc->sc_tinfo[ecb->ccb->ccb_h.target_id];
2066 ti->flags &= ~T_SYNCMODE;
2067 if ((ti->flags & T_SYNCHOFF) == 0)
2068 /* We can re-start sync negotiation */
2069 ti->flags |= T_NEGOTIATE;
2071 case SEND_PARITY_ERROR:
2072 sc->sc_omess[0] = MSG_PARITY_ERROR;
2075 sc->sc_flags |= NCR_ABORTING;
2076 sc->sc_omess[0] = MSG_ABORT;
2078 case SEND_INIT_DET_ERR:
2079 sc->sc_omess[0] = MSG_INITIATOR_DET_ERR;
2082 sc->sc_omess[0] = MSG_MESSAGE_REJECT;
2086 * We normally do not get here, since the chip
2087 * automatically turns off ATN before the last
2088 * byte of a message is sent to the target.
2089 * However, if the target rejects our (multi-byte)
2090 * message early by switching to MSG IN phase
2091 * ATN remains on, so the target may return to
2092 * MSG OUT phase. If there are no scheduled messages
2093 * left we send a NO-OP.
2095 * XXX - Note that this leaves no useful purpose for
2098 sc->sc_flags &= ~NCR_ATN;
2099 sc->sc_omess[0] = MSG_NOOP;
2102 sc->sc_omp = sc->sc_omess;
2106 if (ncr53c9x_debug & NCR_SHOWMSGS) {
2109 NCR_MSGS(("<msgout:"));
2110 for (i = 0; i < sc->sc_omlen; i++)
2111 NCR_MSGS((" %02x", sc->sc_omess[i]));
2115 if (sc->sc_rev == NCR_VARIANT_FAS366) {
2119 ncr53c9x_flushfifo(sc);
2120 ncr53c9x_wrfifo(sc, sc->sc_omp, sc->sc_omlen);
2121 NCRCMD(sc, NCRCMD_TRANS);
2123 /* (re)send the message */
2124 size = min(sc->sc_omlen, sc->sc_maxxfer);
2125 NCRDMA_SETUP(sc, &sc->sc_omp, &sc->sc_omlen, 0, &size);
2126 /* Program the SCSI counter */
2127 NCR_SET_COUNT(sc, size);
2129 /* Load the count in and start the message-out transfer */
2130 NCRCMD(sc, NCRCMD_NOP|NCRCMD_DMA);
2131 NCRCMD(sc, NCRCMD_TRANS|NCRCMD_DMA);
2137 * This is the most critical part of the driver, and has to know
2138 * how to deal with *all* error conditions and phases from the SCSI
2139 * bus. If there are no errors and the DMA was active, then call the
2140 * DMA pseudo-interrupt handler. If this returns 1, then that was it
2141 * and we can return from here without further processing.
2143 * Most of this needs verifying.
2146 ncr53c9x_intr(void *arg)
2148 struct ncr53c9x_softc *sc = arg;
2149 struct ncr53c9x_ecb *ecb;
2150 struct ncr53c9x_tinfo *ti;
2154 NCR_INTS(("[ncr53c9x_intr: state %d]", sc->sc_state));
2156 if (!NCRDMA_ISINTR(sc))
2159 mtx_lock(&sc->sc_lock);
2161 /* and what do the registers say... */
2162 ncr53c9x_readregs(sc);
2165 * At the moment, only a SCSI Bus Reset or Illegal
2166 * Command are classed as errors. A disconnect is a
2167 * valid condition, and we let the code check is the
2168 * "NCR_BUSFREE_OK" flag was set before declaring it
2171 * Also, the status register tells us about "Gross
2172 * Errors" and "Parity errors". Only the Gross Error
2173 * is really bad, and the parity errors are dealt
2177 * If there are too many parity error, go to slow
2182 if ((sc->sc_espintr & NCRINTR_SBR) != 0) {
2183 if ((NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF) != 0) {
2184 NCRCMD(sc, NCRCMD_FLUSH);
2187 if (sc->sc_state != NCR_SBR) {
2188 device_printf(sc->sc_dev, "SCSI bus reset\n");
2189 ncr53c9x_init(sc, 0); /* Restart everything */
2193 /*XXX*/ printf("<expected bus reset: "
2194 "[intr %x, stat %x, step %d]>\n",
2195 sc->sc_espintr, sc->sc_espstat, sc->sc_espstep);
2197 if (sc->sc_nexus != NULL)
2198 panic("%s: nexus in reset state",
2199 device_get_nameunit(sc->sc_dev));
2205 #define NCRINTR_ERR (NCRINTR_SBR|NCRINTR_ILL)
2206 if (sc->sc_espintr & NCRINTR_ERR ||
2207 sc->sc_espstat & NCRSTAT_GE) {
2209 if ((sc->sc_espstat & NCRSTAT_GE) != 0) {
2210 /* Gross Error; no target ? */
2211 if (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF) {
2212 NCRCMD(sc, NCRCMD_FLUSH);
2215 if (sc->sc_state == NCR_CONNECTED ||
2216 sc->sc_state == NCR_SELECTING) {
2217 ecb->ccb->ccb_h.status = CAM_SEL_TIMEOUT;
2218 ncr53c9x_done(sc, ecb);
2223 if ((sc->sc_espintr & NCRINTR_ILL) != 0) {
2224 if ((sc->sc_flags & NCR_EXPECT_ILLCMD) != 0) {
2226 * Eat away "Illegal command" interrupt
2227 * on a ESP100 caused by a re-selection
2228 * while we were trying to select
2232 device_printf(sc->sc_dev, "ESP100 work-around "
2235 sc->sc_flags &= ~NCR_EXPECT_ILLCMD;
2238 /* illegal command, out of sync ? */
2239 device_printf(sc->sc_dev, "illegal command: 0x%x "
2240 "(state %d, phase %x, prevphase %x)\n",
2242 sc->sc_state, sc->sc_phase, sc->sc_prevphase);
2243 if (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF) {
2244 NCRCMD(sc, NCRCMD_FLUSH);
2247 ncr53c9x_init(sc, 1); /* Restart everything */
2251 sc->sc_flags &= ~NCR_EXPECT_ILLCMD;
2254 * Call if DMA is active.
2256 * If DMA_INTR returns true, then maybe go 'round the loop
2257 * again in case there is no more DMA queued, but a phase
2258 * change is expected.
2260 if (NCRDMA_ISACTIVE(sc)) {
2261 int r = NCRDMA_INTR(sc);
2263 device_printf(sc->sc_dev, "DMA error; resetting\n");
2264 ncr53c9x_init(sc, 1);
2267 /* If DMA active here, then go back to work... */
2268 if (NCRDMA_ISACTIVE(sc))
2271 if ((sc->sc_espstat & NCRSTAT_TC) == 0) {
2273 * DMA not completed. If we can not find a
2274 * acceptable explanation, print a diagnostic.
2276 if (sc->sc_state == NCR_SELECTING)
2278 * This can happen if we are reselected
2279 * while using DMA to select a target.
2282 else if (sc->sc_prevphase == MESSAGE_OUT_PHASE) {
2284 * Our (multi-byte) message (eg SDTR) was
2285 * interrupted by the target to send
2287 * Print diagnostic if current phase
2288 * is not MESSAGE IN.
2290 if (sc->sc_phase != MESSAGE_IN_PHASE)
2291 device_printf(sc->sc_dev,"!TC on MSGOUT"
2292 " [intr %x, stat %x, step %d]"
2293 " prevphase %x, resid %lx\n",
2298 (u_long)sc->sc_omlen);
2299 } else if (sc->sc_dleft == 0) {
2301 * The DMA operation was started for
2302 * a DATA transfer. Print a diagnostic
2303 * if the DMA counter and TC bit
2304 * appear to be out of sync.
2306 * XXX This is fatal and usually means that
2307 * the DMA engine is hopelessly out of
2308 * sync with reality. A disk is likely
2309 * getting spammed at this point.
2311 device_printf(sc->sc_dev, "!TC on DATA XFER"
2312 " [intr %x, stat %x, step %d]"
2313 " prevphase %x, resid %x\n",
2318 ecb ? ecb->dleft : -1);
2319 panic("esp: unrecoverable DMA error");
2325 * Check for less serious errors.
2327 if ((sc->sc_espstat & NCRSTAT_PE) != 0) {
2328 device_printf(sc->sc_dev, "SCSI bus parity error\n");
2329 if (sc->sc_prevphase == MESSAGE_IN_PHASE)
2330 ncr53c9x_sched_msgout(SEND_PARITY_ERROR);
2332 ncr53c9x_sched_msgout(SEND_INIT_DET_ERR);
2335 if ((sc->sc_espintr & NCRINTR_DIS) != 0) {
2337 NCR_INTS(("<DISC [intr %x, stat %x, step %d]>",
2338 sc->sc_espintr,sc->sc_espstat,sc->sc_espstep));
2339 if (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF) {
2340 NCRCMD(sc, NCRCMD_FLUSH);
2344 * This command must (apparently) be issued within
2345 * 250mS of a disconnect. So here you are...
2347 NCRCMD(sc, NCRCMD_ENSEL);
2349 switch (sc->sc_state) {
2350 case NCR_RESELECTED:
2355 struct ncr53c9x_linfo *li;
2357 ecb->ccb->ccb_h.status = CAM_SEL_TIMEOUT;
2359 /* Selection timeout -- discard all LUNs if empty */
2360 ti = &sc->sc_tinfo[ecb->ccb->ccb_h.target_id];
2361 li = LIST_FIRST(&ti->luns);
2362 while (li != NULL) {
2363 if (li->untagged == NULL && li->used == 0) {
2364 if (li->lun < NCR_NLUN)
2365 ti->lun[li->lun] = NULL;
2366 LIST_REMOVE(li, link);
2369 * Restart the search at the beginning
2371 li = LIST_FIRST(&ti->luns);
2374 li = LIST_NEXT(li, link);
2379 if ((sc->sc_flags & NCR_SYNCHNEGO) != 0) {
2380 #ifdef NCR53C9X_DEBUG
2382 xpt_print_path(ecb->ccb->ccb_h.path);
2383 printf("sync nego not completed!\n");
2385 ti = &sc->sc_tinfo[ecb->ccb->ccb_h.target_id];
2386 sc->sc_flags &= ~NCR_SYNCHNEGO;
2387 ti->flags &= ~(T_NEGOTIATE | T_SYNCMODE);
2390 /* it may be OK to disconnect */
2391 if ((sc->sc_flags & NCR_ABORTING) == 0) {
2393 * Section 5.1.1 of the SCSI 2 spec
2394 * suggests issuing a REQUEST SENSE
2395 * following an unexpected disconnect.
2396 * Some devices go into a contingent
2397 * allegiance condition when
2398 * disconnecting, and this is necessary
2399 * to clean up their state.
2401 device_printf(sc->sc_dev, "unexpected "
2402 "disconnect [state %d, intr %x, stat %x, "
2403 "phase(c %x, p %x)]; ", sc->sc_state,
2404 sc->sc_espintr, sc->sc_espstat,
2405 sc->sc_phase, sc->sc_prevphase);
2408 * XXX This will cause a chip reset and will
2409 * prevent us from finding out the real
2410 * problem with the device. However, it's
2411 * neccessary until a way can be found to
2412 * safely cancel the DMA that is in
2415 if (1 || (ecb->flags & ECB_SENSE) != 0) {
2416 printf("resetting\n");
2419 printf("sending REQUEST SENSE\n");
2420 untimeout(ncr53c9x_timeout, ecb,
2421 ecb->ccb->ccb_h.timeout_ch);
2422 ncr53c9x_sense(sc, ecb);
2426 ecb->ccb->ccb_h.status = CAM_CMD_TIMEOUT;
2429 case NCR_DISCONNECT:
2430 sc->sc_nexus = NULL;
2433 case NCR_CMDCOMPLETE:
2434 ecb->ccb->ccb_h.status = CAM_REQ_CMP;
2439 switch (sc->sc_state) {
2442 device_printf(sc->sc_dev, "waiting for Bus Reset to happen\n");
2445 case NCR_RESELECTED:
2447 * we must be continuing a message ?
2449 device_printf(sc->sc_dev, "unhandled reselect continuation, "
2450 "state %d, intr %02x\n", sc->sc_state, sc->sc_espintr);
2451 ncr53c9x_init(sc, 1);
2455 case NCR_IDENTIFIED:
2457 if (sc->sc_phase != MESSAGE_IN_PHASE) {
2458 int i = (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF);
2460 * Things are seriously screwed up.
2461 * Pull the brakes, i.e. reset
2463 device_printf(sc->sc_dev, "target didn't send tag: %d "
2464 "bytes in fifo\n", i);
2465 /* Drain and display fifo */
2467 printf("[%d] ", NCR_READ_REG(sc, NCR_FIFO));
2469 ncr53c9x_init(sc, 1);
2477 if (sc->sc_espintr & NCRINTR_RESEL) {
2478 sc->sc_msgpriq = sc->sc_msgout = sc->sc_msgoutq = 0;
2481 * If we're trying to select a
2482 * target ourselves, push our command
2483 * back into the ready list.
2485 if (sc->sc_state == NCR_SELECTING) {
2486 NCR_INTS(("backoff selector "));
2487 untimeout(ncr53c9x_timeout, ecb,
2488 ecb->ccb->ccb_h.timeout_ch);
2489 ncr53c9x_dequeue(sc, ecb);
2490 TAILQ_INSERT_HEAD(&sc->ready_list, ecb, chain);
2491 ecb->flags |= ECB_READY;
2492 ecb = sc->sc_nexus = NULL;
2494 sc->sc_state = NCR_RESELECTED;
2495 if (sc->sc_phase != MESSAGE_IN_PHASE) {
2497 * Things are seriously screwed up.
2498 * Pull the brakes, i.e. reset
2500 device_printf(sc->sc_dev, "target didn't "
2502 ncr53c9x_init(sc, 1);
2506 * The C90 only inhibits FIFO writes until reselection
2507 * is complete instead of waiting until the interrupt
2508 * status register has been read. So, if the reselect
2509 * happens while we were entering command bytes (for
2510 * another target) some of those bytes can appear in
2511 * the FIFO here, after the interrupt is taken.
2513 * To remedy this situation, pull the Selection ID
2514 * and Identify message from the FIFO directly, and
2515 * ignore any extraneous fifo contents. Also, set
2516 * a flag that allows one Illegal Command Interrupt
2517 * to occur which the chip also generates as a result
2518 * of writing to the FIFO during a reselect.
2520 if (sc->sc_rev == NCR_VARIANT_ESP100) {
2521 nfifo = NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF;
2522 sc->sc_imess[0] = NCR_READ_REG(sc, NCR_FIFO);
2523 sc->sc_imess[1] = NCR_READ_REG(sc, NCR_FIFO);
2526 /* Flush the rest */
2527 NCRCMD(sc, NCRCMD_FLUSH);
2529 sc->sc_flags |= NCR_EXPECT_ILLCMD;
2531 nfifo = 2; /* We fixed it.. */
2533 nfifo = ncr53c9x_rdfifo(sc, NCR_RDFIFO_START);
2536 device_printf(sc->sc_dev, "RESELECT: %d bytes "
2537 "in FIFO! [intr %x, stat %x, step %d, "
2544 ncr53c9x_init(sc, 1);
2547 sc->sc_selid = sc->sc_imess[0];
2548 NCR_INTS(("selid=%02x ", sc->sc_selid));
2550 /* Handle identify message */
2553 if (sc->sc_state != NCR_CONNECTED &&
2554 sc->sc_state != NCR_IDENTIFIED) {
2555 /* IDENTIFY fail?! */
2556 device_printf(sc->sc_dev, "identify failed, "
2557 "state %d, intr %02x\n", sc->sc_state,
2559 ncr53c9x_init(sc, 1);
2562 goto shortcut; /* ie. next phase expected soon */
2565 #define NCRINTR_DONE (NCRINTR_FC|NCRINTR_BS)
2566 if ((sc->sc_espintr & NCRINTR_DONE) == NCRINTR_DONE) {
2568 * Arbitration won; examine the `step' register
2569 * to determine how far the selection could progress.
2573 * When doing path inquiry during boot
2574 * FAS100A trigger a stray interrupt which
2575 * we just ignore instead of panicing.
2577 if (sc->sc_state == NCR_IDLE &&
2578 sc->sc_espstep == 0)
2580 panic("ncr53c9x: no nexus");
2583 ti = &sc->sc_tinfo[ecb->ccb->ccb_h.target_id];
2585 switch (sc->sc_espstep) {
2588 * The target did not respond with a
2589 * message out phase - probably an old
2590 * device that doesn't recognize ATN.
2591 * Clear ATN and just continue, the
2592 * target should be in the command
2594 * XXXX check for command phase?
2596 NCRCMD(sc, NCRCMD_RSTATN);
2599 if ((ti->flags & T_NEGOTIATE) == 0 &&
2601 device_printf(sc->sc_dev, "step 1 & "
2605 if (sc->sc_phase != MESSAGE_OUT_PHASE) {
2606 device_printf(sc->sc_dev, "!MSGOUT\n");
2609 if (ti->flags & T_WIDE) {
2610 ti->flags |= T_WDTRSENT;
2611 ncr53c9x_sched_msgout(SEND_WDTR);
2613 if (ti->flags & T_NEGOTIATE) {
2614 /* Start negotiating */
2615 sc->sc_flags |= NCR_SYNCHNEGO;
2617 ncr53c9x_sched_msgout(
2618 SEND_TAG|SEND_SDTR);
2620 ncr53c9x_sched_msgout(
2623 /* Could not do ATN3 so send TAG */
2624 ncr53c9x_sched_msgout(SEND_TAG);
2626 sc->sc_prevphase = MESSAGE_OUT_PHASE; /* XXXX */
2630 * Grr, this is supposed to mean
2631 * "target left command phase prematurely".
2632 * It seems to happen regularly when
2634 * Look at FIFO to see if command went out.
2635 * (Timing problems?)
2637 if (sc->sc_features & NCR_F_DMASELECT) {
2638 if (sc->sc_cmdlen == 0)
2639 /* Hope for the best.. */
2641 } else if ((NCR_READ_REG(sc, NCR_FFLAG)
2642 & NCRFIFO_FF) == 0) {
2643 /* Hope for the best.. */
2646 printf("(%s:%d:%d): selection failed;"
2648 "[intr %x, stat %x, step %d]\n",
2649 device_get_nameunit(sc->sc_dev),
2650 ecb->ccb->ccb_h.target_id,
2651 ecb->ccb->ccb_h.target_lun,
2652 NCR_READ_REG(sc, NCR_FFLAG)
2654 sc->sc_espintr, sc->sc_espstat,
2656 NCRCMD(sc, NCRCMD_FLUSH);
2657 ncr53c9x_sched_msgout(SEND_ABORT);
2660 /* Select stuck at Command Phase */
2661 NCRCMD(sc, NCRCMD_FLUSH);
2664 if (sc->sc_features & NCR_F_DMASELECT &&
2666 printf("(%s:%d:%d): select; "
2667 "%lu left in DMA buffer "
2668 "[intr %x, stat %x, step %d]\n",
2669 device_get_nameunit(sc->sc_dev),
2670 ecb->ccb->ccb_h.target_id,
2671 ecb->ccb->ccb_h.target_lun,
2672 (u_long)sc->sc_cmdlen,
2676 /* So far, everything went fine */
2680 sc->sc_prevphase = INVALID_PHASE; /* ?? */
2681 /* Do an implicit RESTORE POINTERS. */
2682 sc->sc_dp = ecb->daddr;
2683 sc->sc_dleft = ecb->dleft;
2684 sc->sc_state = NCR_CONNECTED;
2689 device_printf(sc->sc_dev, "unexpected status after "
2690 "select: [intr %x, stat %x, step %x]\n",
2691 sc->sc_espintr, sc->sc_espstat, sc->sc_espstep);
2692 NCRCMD(sc, NCRCMD_FLUSH);
2696 if (sc->sc_state == NCR_IDLE) {
2697 device_printf(sc->sc_dev, "stray interrupt\n");
2703 if ((sc->sc_flags & NCR_ICCS) != 0) {
2704 /* "Initiate Command Complete Steps" in progress */
2707 sc->sc_flags &= ~NCR_ICCS;
2709 if (!(sc->sc_espintr & NCRINTR_DONE)) {
2710 device_printf(sc->sc_dev, "ICCS: "
2711 ": [intr %x, stat %x, step %x]\n",
2712 sc->sc_espintr, sc->sc_espstat,
2715 ncr53c9x_rdfifo(sc, NCR_RDFIFO_START);
2716 if (sc->sc_imlen < 2)
2717 device_printf(sc->sc_dev, "can't get status, "
2718 "only %d bytes\n", (int)sc->sc_imlen);
2719 ecb->stat = sc->sc_imess[sc->sc_imlen - 2];
2720 msg = sc->sc_imess[sc->sc_imlen - 1];
2721 NCR_PHASE(("<stat:(%x,%x)>", ecb->stat, msg));
2722 if (msg == MSG_CMDCOMPLETE) {
2723 ecb->dleft = (ecb->flags & ECB_TENTATIVE_DONE)
2725 if ((ecb->flags & ECB_SENSE) == 0)
2726 ecb->ccb->csio.resid = ecb->dleft;
2727 sc->sc_state = NCR_CMDCOMPLETE;
2729 device_printf(sc->sc_dev, "STATUS_PHASE: "
2732 NCRCMD(sc, NCRCMD_MSGOK);
2733 goto shortcut; /* ie. wait for disconnect */
2738 device_printf(sc->sc_dev, "invalid state: %d [intr %x, "
2739 "phase(c %x, p %x)]\n", sc->sc_state,
2740 sc->sc_espintr, sc->sc_phase, sc->sc_prevphase);
2745 * Driver is now in state NCR_CONNECTED, i.e. we
2746 * have a current command working the SCSI bus.
2748 if (sc->sc_state != NCR_CONNECTED || ecb == NULL) {
2749 panic("ncr53c9x: no nexus");
2752 switch (sc->sc_phase) {
2753 case MESSAGE_OUT_PHASE:
2754 NCR_PHASE(("MESSAGE_OUT_PHASE "));
2755 ncr53c9x_msgout(sc);
2756 sc->sc_prevphase = MESSAGE_OUT_PHASE;
2759 case MESSAGE_IN_PHASE:
2761 NCR_PHASE(("MESSAGE_IN_PHASE "));
2762 if ((sc->sc_espintr & NCRINTR_BS) != 0) {
2763 if ((sc->sc_rev != NCR_VARIANT_FAS366) ||
2764 !(sc->sc_espstat2 & NCRFAS_STAT2_EMPTY)) {
2765 NCRCMD(sc, NCRCMD_FLUSH);
2767 sc->sc_flags |= NCR_WAITI;
2768 NCRCMD(sc, NCRCMD_TRANS);
2769 } else if ((sc->sc_espintr & NCRINTR_FC) != 0) {
2770 if ((sc->sc_flags & NCR_WAITI) == 0) {
2771 device_printf(sc->sc_dev, "MSGIN: unexpected "
2772 "FC bit: [intr %x, stat %x, step %x]\n",
2773 sc->sc_espintr, sc->sc_espstat,
2776 sc->sc_flags &= ~NCR_WAITI;
2778 (sc->sc_prevphase == sc->sc_phase) ?
2779 NCR_RDFIFO_CONTINUE : NCR_RDFIFO_START);
2782 device_printf(sc->sc_dev, "MSGIN: weird bits: "
2783 "[intr %x, stat %x, step %x]\n",
2784 sc->sc_espintr, sc->sc_espstat, sc->sc_espstep);
2786 sc->sc_prevphase = MESSAGE_IN_PHASE;
2787 goto shortcut; /* i.e. expect data to be ready */
2791 * Send the command block. Normally we don't see this
2792 * phase because the SEL_ATN command takes care of
2793 * all this. However, we end up here if either the
2794 * target or we wanted to exchange some more messages
2795 * first (e.g. to start negotiations).
2798 NCR_PHASE(("COMMAND_PHASE 0x%02x (%d) ",
2799 ecb->cmd.cmd.opcode, ecb->clen));
2800 if (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF) {
2801 NCRCMD(sc, NCRCMD_FLUSH);
2804 if (sc->sc_features & NCR_F_DMASELECT) {
2805 /* setup DMA transfer for command */
2807 sc->sc_cmdlen = size;
2808 sc->sc_cmdp = (caddr_t)&ecb->cmd.cmd;
2809 NCRDMA_SETUP(sc, &sc->sc_cmdp, &sc->sc_cmdlen,
2811 /* Program the SCSI counter */
2812 NCR_SET_COUNT(sc, size);
2814 /* load the count in */
2815 NCRCMD(sc, NCRCMD_NOP|NCRCMD_DMA);
2817 /* start the command transfer */
2818 NCRCMD(sc, NCRCMD_TRANS | NCRCMD_DMA);
2821 ncr53c9x_wrfifo(sc, (u_char *)&ecb->cmd.cmd, ecb->clen);
2822 NCRCMD(sc, NCRCMD_TRANS);
2824 sc->sc_prevphase = COMMAND_PHASE;
2827 case DATA_OUT_PHASE:
2828 NCR_PHASE(("DATA_OUT_PHASE [%ld] ",(long)sc->sc_dleft));
2829 NCRCMD(sc, NCRCMD_FLUSH);
2830 size = min(sc->sc_dleft, sc->sc_maxxfer);
2831 NCRDMA_SETUP(sc, &sc->sc_dp, &sc->sc_dleft, 0, &size);
2832 sc->sc_prevphase = DATA_OUT_PHASE;
2836 NCR_PHASE(("DATA_IN_PHASE "));
2837 if (sc->sc_rev == NCR_VARIANT_ESP100)
2838 NCRCMD(sc, NCRCMD_FLUSH);
2839 size = min(sc->sc_dleft, sc->sc_maxxfer);
2840 NCRDMA_SETUP(sc, &sc->sc_dp, &sc->sc_dleft, 1, &size);
2841 sc->sc_prevphase = DATA_IN_PHASE;
2843 /* Target returned to data phase: wipe "done" memory */
2844 ecb->flags &= ~ECB_TENTATIVE_DONE;
2846 /* Program the SCSI counter */
2847 NCR_SET_COUNT(sc, size);
2849 /* load the count in */
2850 NCRCMD(sc, NCRCMD_NOP|NCRCMD_DMA);
2853 * Note that if `size' is 0, we've already transceived
2854 * all the bytes we want but we're still in DATA PHASE.
2855 * Apparently, the device needs padding. Also, a
2856 * transfer size of 0 means "maximum" to the chip
2860 (size == 0 ? NCRCMD_TRPAD : NCRCMD_TRANS) | NCRCMD_DMA);
2865 NCR_PHASE(("STATUS_PHASE "));
2866 sc->sc_flags |= NCR_ICCS;
2867 NCRCMD(sc, NCRCMD_ICCS);
2868 sc->sc_prevphase = STATUS_PHASE;
2869 goto shortcut; /* i.e. expect status results soon */
2875 device_printf(sc->sc_dev, "unexpected bus phase; resetting\n");
2880 mtx_unlock(&sc->sc_lock);
2884 ncr53c9x_init(sc, 1);
2888 ncr53c9x_done(sc, ecb);
2892 sc->sc_state = NCR_IDLE;
2898 * The idea is that many of the SCSI operations take very little
2899 * time, and going away and getting interrupted is too high an
2900 * overhead to pay. For example, selecting, sending a message
2901 * and command and then doing some work can be done in one "pass".
2903 * The delay is a heuristic. It is 2 when at 20MHz, 2 at 25MHz and 1
2904 * at 40MHz. This needs testing.
2907 struct timeval wait, cur;
2910 wait.tv_usec += 50 / sc->sc_freq;
2911 if (wait.tv_usec > 1000000) {
2913 wait.tv_usec -= 1000000;
2916 if (NCRDMA_ISINTR(sc))
2919 } while (cur.tv_sec <= wait.tv_sec &&
2920 cur.tv_usec <= wait.tv_usec);
2926 ncr53c9x_abort(sc, ecb)
2927 struct ncr53c9x_softc *sc;
2928 struct ncr53c9x_ecb *ecb;
2931 /* 2 secs for the abort */
2932 ecb->timeout = NCR_ABORT_TIMEOUT;
2933 ecb->flags |= ECB_ABORT;
2935 if (ecb == sc->sc_nexus) {
2937 * If we're still selecting, the message will be scheduled
2938 * after selection is complete.
2940 if (sc->sc_state == NCR_CONNECTED)
2941 ncr53c9x_sched_msgout(SEND_ABORT);
2944 * Reschedule timeout.
2946 ecb->ccb->ccb_h.timeout_ch =
2947 timeout(ncr53c9x_timeout, ecb, mstohz(ecb->timeout));
2950 * Just leave the command where it is.
2951 * XXX - what choice do we have but to reset the SCSI
2954 if (sc->sc_state == NCR_IDLE)
2960 ncr53c9x_timeout(void *arg)
2962 struct ncr53c9x_ecb *ecb = arg;
2963 union ccb *ccb = ecb->ccb;
2964 struct ncr53c9x_softc *sc = ecb->sc;
2965 struct ncr53c9x_tinfo *ti = &sc->sc_tinfo[ccb->ccb_h.target_id];
2967 xpt_print_path(ccb->ccb_h.path);
2968 device_printf(sc->sc_dev, "timed out [ecb %p (flags 0x%x, dleft %x, "
2969 "stat %x)], <state %d, nexus %p, phase(l %x, c %x, p %x), "
2970 "resid %lx, msg(q %x,o %x) %s>",
2971 ecb, ecb->flags, ecb->dleft, ecb->stat,
2972 sc->sc_state, sc->sc_nexus,
2973 NCR_READ_REG(sc, NCR_STAT),
2974 sc->sc_phase, sc->sc_prevphase,
2975 (long)sc->sc_dleft, sc->sc_msgpriq, sc->sc_msgout,
2976 NCRDMA_ISACTIVE(sc) ? "DMA active" : "");
2977 #if defined(NCR53C9X_DEBUG) && NCR53C9X_DEBUG > 1
2978 printf("TRACE: %s.", ecb->trace);
2981 mtx_lock(&sc->sc_lock);
2983 if (ecb->flags & ECB_ABORT) {
2984 /* abort timed out */
2987 ncr53c9x_init(sc, 1);
2989 /* abort the operation that has timed out */
2991 ccb->ccb_h.status = CAM_CMD_TIMEOUT;
2992 ncr53c9x_abort(sc, ecb);
2994 /* Disable sync mode if stuck in a data phase */
2995 if (ecb == sc->sc_nexus &&
2996 (ti->flags & T_SYNCMODE) != 0 &&
2997 (sc->sc_phase & (MSGI|CDI)) == 0) {
2998 /* XXX ASYNC CALLBACK! */
2999 xpt_print_path(ccb->ccb_h.path);
3000 printf("sync negotiation disabled\n");
3002 (1 << ((ccb->ccb_h.target_id & 7) + 8));
3006 mtx_unlock(&sc->sc_lock);
3010 ncr53c9x_watch(void *arg)
3012 struct ncr53c9x_softc *sc = (struct ncr53c9x_softc *)arg;
3013 struct ncr53c9x_tinfo *ti;
3014 struct ncr53c9x_linfo *li;
3016 /* Delete any structures that have not been used in 10min. */
3017 time_t old = time_second - (10 * 60);
3019 mtx_lock(&sc->sc_lock);
3020 for (t = 0; t < sc->sc_ntarg; t++) {
3021 ti = &sc->sc_tinfo[t];
3022 li = LIST_FIRST(&ti->luns);
3024 if (li->last_used < old &&
3025 li->untagged == NULL &&
3027 if (li->lun < NCR_NLUN)
3028 ti->lun[li->lun] = NULL;
3029 LIST_REMOVE(li, link);
3031 /* Restart the search at the beginning */
3032 li = LIST_FIRST(&ti->luns);
3035 li = LIST_NEXT(li, link);
3038 mtx_unlock(&sc->sc_lock);
3039 callout_reset(&sc->sc_watchdog, 60 * hz, ncr53c9x_watch, sc);