[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.135.2.1

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

CVSweb <webmaster@jp.NetBSD.org>