[BACK]Return to ncr53c9x.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sys / dev / ic

Annotation of src/sys/dev/ic/ncr53c9x.c, Revision 1.52

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

CVSweb <webmaster@jp.NetBSD.org>