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

Annotation of src/sys/dev/ic/siop.c, Revision 1.40.2.6

1.40.2.6! nathanw     1: /*     $NetBSD: siop.c,v 1.40.2.5 2001/11/14 19:14:36 nathanw Exp $    */
1.1       bouyer      2:
                      3: /*
                      4:  * Copyright (c) 2000 Manuel Bouyer.
                      5:  *
                      6:  * Redistribution and use in source and binary forms, with or without
                      7:  * modification, are permitted provided that the following conditions
                      8:  * are met:
                      9:  * 1. Redistributions of source code must retain the above copyright
                     10:  *    notice, this list of conditions and the following disclaimer.
                     11:  * 2. Redistributions in binary form must reproduce the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer in the
                     13:  *    documentation and/or other materials provided with the distribution.
                     14:  * 3. All advertising materials mentioning features or use of this software
                     15:  *    must display the following acknowledgement:
1.5       bouyer     16:  *     This product includes software developed by Manuel Bouyer
                     17:  * 4. The name of the author may not be used to endorse or promote products
                     18:  *    derived from this software without specific prior written permission.
1.1       bouyer     19:  *
1.14      bouyer     20:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     21:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     22:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     23:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     24:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     25:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     26:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     27:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     28:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     29:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.1       bouyer     30:  *
                     31:  */
                     32:
                     33: /* SYM53c7/8xx PCI-SCSI I/O Processors driver */
1.40.2.5  nathanw    34:
                     35: #include <sys/cdefs.h>
1.40.2.6! nathanw    36: __KERNEL_RCSID(0, "$NetBSD: siop.c,v 1.40.2.5 2001/11/14 19:14:36 nathanw Exp $");
1.1       bouyer     37:
                     38: #include <sys/param.h>
                     39: #include <sys/systm.h>
                     40: #include <sys/device.h>
                     41: #include <sys/malloc.h>
                     42: #include <sys/buf.h>
                     43: #include <sys/kernel.h>
                     44:
1.37      thorpej    45: #include <uvm/uvm_extern.h>
                     46:
1.1       bouyer     47: #include <machine/endian.h>
                     48: #include <machine/bus.h>
                     49:
                     50: #include <dev/microcode/siop/siop.out>
                     51:
                     52: #include <dev/scsipi/scsi_all.h>
                     53: #include <dev/scsipi/scsi_message.h>
                     54: #include <dev/scsipi/scsipi_all.h>
                     55:
                     56: #include <dev/scsipi/scsiconf.h>
                     57:
                     58: #include <dev/ic/siopreg.h>
                     59: #include <dev/ic/siopvar.h>
1.14      bouyer     60: #include <dev/ic/siopvar_common.h>
1.1       bouyer     61:
1.35      bouyer     62: #ifndef DEBUG
1.30      bouyer     63: #undef DEBUG
1.35      bouyer     64: #endif
                     65: #undef SIOP_DEBUG
                     66: #undef SIOP_DEBUG_DR
                     67: #undef SIOP_DEBUG_INTR
                     68: #undef SIOP_DEBUG_SCHED
1.2       bouyer     69: #undef DUMP_SCRIPT
                     70:
                     71: #define SIOP_STATS
                     72:
1.1       bouyer     73: #ifndef SIOP_DEFAULT_TARGET
                     74: #define SIOP_DEFAULT_TARGET 7
                     75: #endif
                     76:
1.16      bouyer     77: /* number of cmd descriptors per block */
1.37      thorpej    78: #define SIOP_NCMDPB (PAGE_SIZE / sizeof(struct siop_xfer))
1.1       bouyer     79:
1.35      bouyer     80: /* Number of scheduler slot (needs to match script) */
                     81: #define SIOP_NSLOTS 40
                     82:
1.1       bouyer     83: void   siop_reset __P((struct siop_softc *));
1.2       bouyer     84: void   siop_handle_reset __P((struct siop_softc *));
1.31      bouyer     85: int    siop_handle_qtag_reject __P((struct siop_cmd *));
1.2       bouyer     86: void   siop_scsicmd_end __P((struct siop_cmd *));
1.40.2.2  nathanw    87: void   siop_unqueue __P((struct siop_softc *, int, int));
                     88: static void    siop_start __P((struct siop_softc *, struct siop_cmd *));
1.1       bouyer     89: void   siop_timeout __P((void *));
                     90: int    siop_scsicmd __P((struct scsipi_xfer *));
1.40.2.2  nathanw    91: void   siop_scsipi_request __P((struct scsipi_channel *,
                     92:                        scsipi_adapter_req_t, void *));
1.2       bouyer     93: void   siop_dump_script __P((struct siop_softc *));
1.40.2.4  nathanw    94: void   siop_morecbd __P((struct siop_softc *));
1.31      bouyer     95: struct siop_lunsw *siop_get_lunsw __P((struct siop_softc *));
                     96: void   siop_add_reselsw __P((struct siop_softc *, int));
                     97: void   siop_update_scntl3 __P((struct siop_softc *, struct siop_target *));
1.1       bouyer     98:
1.2       bouyer     99: #ifdef SIOP_STATS
                    100: static int siop_stat_intr = 0;
                    101: static int siop_stat_intr_shortxfer = 0;
                    102: static int siop_stat_intr_sdp = 0;
                    103: static int siop_stat_intr_done = 0;
                    104: static int siop_stat_intr_xferdisc = 0;
1.35      bouyer    105: static int siop_stat_intr_lunresel = 0;
1.36      bouyer    106: static int siop_stat_intr_qfull = 0;
1.2       bouyer    107: void siop_printstats __P((void));
                    108: #define INCSTAT(x) x++
                    109: #else
                    110: #define INCSTAT(x)
                    111: #endif
                    112:
1.32      bouyer    113: static __inline__ void siop_script_sync __P((struct siop_softc *, int));
                    114: static __inline__ void
                    115: siop_script_sync(sc, ops)
                    116:        struct siop_softc *sc;
                    117:        int ops;
                    118: {
1.34      bouyer    119:        if ((sc->features & SF_CHIP_RAM) == 0)
1.37      thorpej   120:                bus_dmamap_sync(sc->sc_dmat, sc->sc_scriptdma, 0,
                    121:                    PAGE_SIZE, ops);
1.32      bouyer    122: }
                    123:
1.35      bouyer    124: static __inline__ u_int32_t siop_script_read __P((struct siop_softc *, u_int));
1.31      bouyer    125: static __inline__ u_int32_t
                    126: siop_script_read(sc, offset)
                    127:        struct siop_softc *sc;
1.35      bouyer    128:        u_int offset;
1.31      bouyer    129: {
                    130:        if (sc->features & SF_CHIP_RAM) {
                    131:                return bus_space_read_4(sc->sc_ramt, sc->sc_ramh, offset * 4);
                    132:        } else {
                    133:                return le32toh(sc->sc_script[offset]);
                    134:        }
                    135: }
                    136:
1.35      bouyer    137: static __inline__ void siop_script_write __P((struct siop_softc *, u_int,
1.31      bouyer    138:        u_int32_t));
1.28      bouyer    139: static __inline__ void
1.31      bouyer    140: siop_script_write(sc, offset, val)
1.28      bouyer    141:        struct siop_softc *sc;
1.35      bouyer    142:        u_int offset;
1.31      bouyer    143:        u_int32_t val;
1.28      bouyer    144: {
1.31      bouyer    145:        if (sc->features & SF_CHIP_RAM) {
                    146:                bus_space_write_4(sc->sc_ramt, sc->sc_ramh, offset * 4, val);
                    147:        } else {
                    148:                sc->sc_script[offset] = htole32(val);
                    149:        }
1.2       bouyer    150: }
                    151:
1.1       bouyer    152: void
                    153: siop_attach(sc)
                    154:        struct siop_softc *sc;
                    155: {
                    156:        int error, i;
                    157:        bus_dma_segment_t seg;
                    158:        int rseg;
                    159:
                    160:        /*
1.35      bouyer    161:         * Allocate DMA-safe memory for the script and map it.
1.1       bouyer    162:         */
1.17      bouyer    163:        if ((sc->features & SF_CHIP_RAM) == 0) {
1.37      thorpej   164:                error = bus_dmamem_alloc(sc->sc_dmat, PAGE_SIZE,
                    165:                    PAGE_SIZE, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT);
1.17      bouyer    166:                if (error) {
                    167:                        printf("%s: unable to allocate script DMA memory, "
                    168:                            "error = %d\n", sc->sc_dev.dv_xname, error);
                    169:                        return;
                    170:                }
1.37      thorpej   171:                error = bus_dmamem_map(sc->sc_dmat, &seg, rseg, PAGE_SIZE,
1.17      bouyer    172:                    (caddr_t *)&sc->sc_script, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
                    173:                if (error) {
                    174:                        printf("%s: unable to map script DMA memory, "
                    175:                            "error = %d\n", sc->sc_dev.dv_xname, error);
                    176:                        return;
                    177:                }
1.37      thorpej   178:                error = bus_dmamap_create(sc->sc_dmat, PAGE_SIZE, 1,
                    179:                    PAGE_SIZE, 0, BUS_DMA_NOWAIT, &sc->sc_scriptdma);
1.17      bouyer    180:                if (error) {
                    181:                        printf("%s: unable to create script DMA map, "
                    182:                            "error = %d\n", sc->sc_dev.dv_xname, error);
                    183:                        return;
                    184:                }
                    185:                error = bus_dmamap_load(sc->sc_dmat, sc->sc_scriptdma,
1.37      thorpej   186:                    sc->sc_script, PAGE_SIZE, NULL, BUS_DMA_NOWAIT);
1.17      bouyer    187:                if (error) {
                    188:                        printf("%s: unable to load script DMA map, "
                    189:                            "error = %d\n", sc->sc_dev.dv_xname, error);
                    190:                        return;
                    191:                }
                    192:                sc->sc_scriptaddr = sc->sc_scriptdma->dm_segs[0].ds_addr;
1.37      thorpej   193:                sc->ram_size = PAGE_SIZE;
1.1       bouyer    194:        }
                    195:        TAILQ_INIT(&sc->free_list);
1.16      bouyer    196:        TAILQ_INIT(&sc->cmds);
1.31      bouyer    197:        TAILQ_INIT(&sc->lunsw_list);
1.29      bouyer    198:        sc->sc_currschedslot = 0;
1.35      bouyer    199: #ifdef SIOP_DEBUG
                    200:        printf("%s: script size = %d, PHY addr=0x%x, VIRT=%p\n",
1.2       bouyer    201:            sc->sc_dev.dv_xname, (int)sizeof(siop_script),
1.35      bouyer    202:            (u_int32_t)sc->sc_scriptaddr, sc->sc_script);
1.1       bouyer    203: #endif
                    204:
1.40.2.2  nathanw   205:        sc->sc_adapt.adapt_dev = &sc->sc_dev;
                    206:        sc->sc_adapt.adapt_nchannels = 1;
1.40.2.4  nathanw   207:        sc->sc_adapt.adapt_openings = 0;
1.40.2.2  nathanw   208:        sc->sc_adapt.adapt_max_periph = SIOP_NTAG - 1;
                    209:        sc->sc_adapt.adapt_ioctl = siop_ioctl;
                    210:        sc->sc_adapt.adapt_minphys = minphys;
                    211:        sc->sc_adapt.adapt_request = siop_scsipi_request;
                    212:
                    213:        memset(&sc->sc_chan, 0, sizeof(sc->sc_chan));
                    214:        sc->sc_chan.chan_adapter = &sc->sc_adapt;
                    215:        sc->sc_chan.chan_bustype = &scsi_bustype;
                    216:        sc->sc_chan.chan_channel = 0;
1.40.2.4  nathanw   217:        sc->sc_chan.chan_flags = SCSIPI_CHAN_CANGROW;
1.40.2.2  nathanw   218:        sc->sc_chan.chan_ntargets = (sc->features & SF_BUS_WIDE) ? 16 : 8;
                    219:        sc->sc_chan.chan_nluns = 8;
                    220:        sc->sc_chan.chan_id = bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_SCID);
                    221:        if (sc->sc_chan.chan_id == 0 ||
                    222:            sc->sc_chan.chan_id >= sc->sc_chan.chan_ntargets)
                    223:                sc->sc_chan.chan_id = SIOP_DEFAULT_TARGET;
1.1       bouyer    224:
1.7       bouyer    225:        for (i = 0; i < 16; i++)
                    226:                sc->targets[i] = NULL;
                    227:
                    228:        /* find min/max sync period for this chip */
                    229:        sc->maxsync = 0;
                    230:        sc->minsync = 255;
                    231:        for (i = 0; i < sizeof(scf_period) / sizeof(scf_period[0]); i++) {
                    232:                if (sc->clock_period != scf_period[i].clock)
                    233:                        continue;
                    234:                if (sc->maxsync < scf_period[i].period)
                    235:                        sc->maxsync = scf_period[i].period;
                    236:                if (sc->minsync > scf_period[i].period)
                    237:                        sc->minsync = scf_period[i].period;
                    238:        }
                    239:        if (sc->maxsync == 255 || sc->minsync == 0)
                    240:                panic("siop: can't find my sync parameters\n");
1.26      bouyer    241:        /* Do a bus reset, so that devices fall back to narrow/async */
                    242:        siop_resetbus(sc);
                    243:        /*
                    244:         * siop_reset() will reset the chip, thus clearing pending interrupts
                    245:         */
1.1       bouyer    246:        siop_reset(sc);
1.2       bouyer    247: #ifdef DUMP_SCRIPT
                    248:        siop_dump_script(sc);
                    249: #endif
1.1       bouyer    250:
1.40.2.2  nathanw   251:        config_found((struct device*)sc, &sc->sc_chan, scsiprint);
1.1       bouyer    252: }
                    253:
                    254: void
                    255: siop_reset(sc)
                    256:        struct siop_softc *sc;
                    257: {
1.4       bouyer    258:        int i, j;
1.31      bouyer    259:        struct siop_lunsw *lunsw;
1.4       bouyer    260:
1.14      bouyer    261:        siop_common_reset(sc);
1.1       bouyer    262:
                    263:        /* copy and patch the script */
1.17      bouyer    264:        if (sc->features & SF_CHIP_RAM) {
                    265:                bus_space_write_region_4(sc->sc_ramt, sc->sc_ramh, 0,
                    266:                    siop_script, sizeof(siop_script) / sizeof(siop_script[0]));
                    267:                for (j = 0; j <
1.31      bouyer    268:                    (sizeof(E_abs_msgin_Used) / sizeof(E_abs_msgin_Used[0]));
1.28      bouyer    269:                    j++) {
                    270:                        bus_space_write_4(sc->sc_ramt, sc->sc_ramh,
1.31      bouyer    271:                            E_abs_msgin_Used[j] * 4,
                    272:                            sc->sc_scriptaddr + Ent_msgin_space);
1.28      bouyer    273:                }
1.17      bouyer    274:        } else {
                    275:                for (j = 0;
                    276:                    j < (sizeof(siop_script) / sizeof(siop_script[0])); j++) {
                    277:                        sc->sc_script[j] = htole32(siop_script[j]);
                    278:                }
                    279:                for (j = 0; j <
1.31      bouyer    280:                    (sizeof(E_abs_msgin_Used) / sizeof(E_abs_msgin_Used[0]));
1.28      bouyer    281:                    j++) {
1.31      bouyer    282:                        sc->sc_script[E_abs_msgin_Used[j]] =
                    283:                            htole32(sc->sc_scriptaddr + Ent_msgin_space);
1.17      bouyer    284:                }
1.4       bouyer    285:        }
1.35      bouyer    286:        sc->script_free_lo = sizeof(siop_script) / sizeof(siop_script[0]);
                    287:        sc->script_free_hi = sc->ram_size / 4;
1.1       bouyer    288:
1.31      bouyer    289:        /* free used and unused lun switches */
                    290:        while((lunsw = TAILQ_FIRST(&sc->lunsw_list)) != NULL) {
1.35      bouyer    291: #ifdef SIOP_DEBUG
1.31      bouyer    292:                printf("%s: free lunsw at offset %d\n",
                    293:                                sc->sc_dev.dv_xname, lunsw->lunsw_off);
                    294: #endif
                    295:                TAILQ_REMOVE(&sc->lunsw_list, lunsw, next);
                    296:                free(lunsw, M_DEVBUF);
                    297:        }
                    298:        TAILQ_INIT(&sc->lunsw_list);
                    299:        /* restore reselect switch */
1.40.2.2  nathanw   300:        for (i = 0; i < sc->sc_chan.chan_ntargets; i++) {
1.31      bouyer    301:                if (sc->targets[i] == NULL)
                    302:                        continue;
1.35      bouyer    303: #ifdef SIOP_DEBUG
1.31      bouyer    304:                printf("%s: restore sw for target %d\n",
                    305:                                sc->sc_dev.dv_xname, i);
                    306: #endif
                    307:                free(sc->targets[i]->lunsw, M_DEVBUF);
                    308:                sc->targets[i]->lunsw = siop_get_lunsw(sc);
                    309:                if (sc->targets[i]->lunsw == NULL) {
                    310:                        printf("%s: can't alloc lunsw for target %d\n",
                    311:                            sc->sc_dev.dv_xname, i);
                    312:                        break;
1.28      bouyer    313:                }
1.31      bouyer    314:                siop_add_reselsw(sc, i);
1.28      bouyer    315:        }
                    316:
1.2       bouyer    317:        /* start script */
1.22      bouyer    318:        if ((sc->features & SF_CHIP_RAM) == 0) {
1.37      thorpej   319:                bus_dmamap_sync(sc->sc_dmat, sc->sc_scriptdma, 0, PAGE_SIZE,
1.22      bouyer    320:                    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
                    321:        }
1.2       bouyer    322:        bus_space_write_4(sc->sc_rt, sc->sc_rh, SIOP_DSP,
1.17      bouyer    323:            sc->sc_scriptaddr + Ent_reselect);
1.1       bouyer    324: }
                    325:
                    326: #if 0
                    327: #define CALL_SCRIPT(ent) do {\
                    328:        printf ("start script DSA 0x%lx DSP 0x%lx\n", \
1.4       bouyer    329:            siop_cmd->dsa, \
1.17      bouyer    330:            sc->sc_scriptaddr + ent); \
                    331: bus_space_write_4(sc->sc_rt, sc->sc_rh, SIOP_DSP, sc->sc_scriptaddr + ent); \
1.1       bouyer    332: } while (0)
                    333: #else
                    334: #define CALL_SCRIPT(ent) do {\
1.17      bouyer    335: bus_space_write_4(sc->sc_rt, sc->sc_rh, SIOP_DSP, sc->sc_scriptaddr + ent); \
1.1       bouyer    336: } while (0)
                    337: #endif
                    338:
                    339: int
                    340: siop_intr(v)
                    341:        void *v;
                    342: {
                    343:        struct siop_softc *sc = v;
1.7       bouyer    344:        struct siop_target *siop_target;
1.1       bouyer    345:        struct siop_cmd *siop_cmd;
1.31      bouyer    346:        struct siop_lun *siop_lun;
1.1       bouyer    347:        struct scsipi_xfer *xs;
1.35      bouyer    348:        int istat, sist, sstat1, dstat;
1.1       bouyer    349:        u_int32_t irqcode;
                    350:        int need_reset = 0;
1.35      bouyer    351:        int offset, target, lun, tag;
1.2       bouyer    352:        bus_addr_t dsa;
1.16      bouyer    353:        struct siop_cbd *cbdp;
1.35      bouyer    354:        int freetarget = 0;
1.39      bouyer    355:        int restart = 0;
1.1       bouyer    356:
                    357:        istat = bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_ISTAT);
1.2       bouyer    358:        if ((istat & (ISTAT_INTF | ISTAT_DIP | ISTAT_SIP)) == 0)
                    359:                return 0;
                    360:        INCSTAT(siop_stat_intr);
1.1       bouyer    361:        if (istat & ISTAT_INTF) {
                    362:                printf("INTRF\n");
                    363:                bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_ISTAT, ISTAT_INTF);
                    364:        }
1.2       bouyer    365:        /* use DSA to find the current siop_cmd */
                    366:        dsa = bus_space_read_4(sc->sc_rt, sc->sc_rh, SIOP_DSA);
1.16      bouyer    367:        for (cbdp = TAILQ_FIRST(&sc->cmds); cbdp != NULL;
                    368:            cbdp = TAILQ_NEXT(cbdp, next)) {
                    369:                if (dsa >= cbdp->xferdma->dm_segs[0].ds_addr &&
1.37      thorpej   370:                    dsa < cbdp->xferdma->dm_segs[0].ds_addr + PAGE_SIZE) {
1.16      bouyer    371:                        dsa -= cbdp->xferdma->dm_segs[0].ds_addr;
                    372:                        siop_cmd = &cbdp->cmds[dsa / sizeof(struct siop_xfer)];
                    373:                        siop_table_sync(siop_cmd,
                    374:                            BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
                    375:                        break;
                    376:                }
                    377:        }
                    378:        if (cbdp == NULL) {
1.2       bouyer    379:                siop_cmd = NULL;
                    380:        }
1.31      bouyer    381:        if (siop_cmd) {
                    382:                xs = siop_cmd->xs;
                    383:                siop_target = siop_cmd->siop_target;
1.40.2.2  nathanw   384:                target = siop_cmd->xs->xs_periph->periph_target;
                    385:                lun = siop_cmd->xs->xs_periph->periph_lun;
1.35      bouyer    386:                tag = siop_cmd->tag;
                    387:                siop_lun = siop_target->siop_lun[lun];
1.31      bouyer    388: #ifdef DIAGNOSTIC
1.40.2.2  nathanw   389:                if (siop_cmd->status != CMDST_ACTIVE) {
1.40.2.3  nathanw   390:                        printf("siop_cmd (lun %d) for DSA 0x%x "
                    391:                            "not active (%d)\n", lun, (u_int)dsa,
                    392:                            siop_cmd->status);
1.31      bouyer    393:                        xs = NULL;
                    394:                        siop_target = NULL;
1.35      bouyer    395:                        target = -1;
1.31      bouyer    396:                        lun = -1;
1.35      bouyer    397:                        tag = -1;
1.31      bouyer    398:                        siop_lun = NULL;
                    399:                        siop_cmd = NULL;
1.35      bouyer    400:                } else if (siop_lun->siop_tag[tag].active != siop_cmd) {
                    401:                        printf("siop_cmd (lun %d tag %d) not in siop_lun "
                    402:                            "active (%p != %p)\n", lun, tag, siop_cmd,
                    403:                            siop_lun->siop_tag[tag].active);
1.31      bouyer    404:                }
                    405: #endif
                    406:        } else {
                    407:                xs = NULL;
                    408:                siop_target = NULL;
1.35      bouyer    409:                target = -1;
1.31      bouyer    410:                lun = -1;
1.35      bouyer    411:                tag = -1;
1.31      bouyer    412:                siop_lun = NULL;
                    413:        }
1.1       bouyer    414:        if (istat & ISTAT_DIP) {
                    415:                dstat = bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_DSTAT);
1.2       bouyer    416:                if (dstat & DSTAT_SSI) {
                    417:                        printf("single step dsp 0x%08x dsa 0x08%x\n",
1.7       bouyer    418:                            (int)(bus_space_read_4(sc->sc_rt, sc->sc_rh, SIOP_DSP) -
1.17      bouyer    419:                            sc->sc_scriptaddr),
1.2       bouyer    420:                            bus_space_read_4(sc->sc_rt, sc->sc_rh, SIOP_DSA));
                    421:                        if ((dstat & ~(DSTAT_DFE | DSTAT_SSI)) == 0 &&
                    422:                            (istat & ISTAT_SIP) == 0) {
                    423:                                bus_space_write_1(sc->sc_rt, sc->sc_rh,
                    424:                                    SIOP_DCNTL, bus_space_read_1(sc->sc_rt,
                    425:                                    sc->sc_rh, SIOP_DCNTL) | DCNTL_STD);
                    426:                        }
                    427:                        return 1;
                    428:                }
                    429:                if (dstat & ~(DSTAT_SIR | DSTAT_DFE | DSTAT_SSI)) {
1.1       bouyer    430:                printf("DMA IRQ:");
                    431:                if (dstat & DSTAT_IID)
                    432:                        printf(" Illegal instruction");
                    433:                if (dstat & DSTAT_ABRT)
                    434:                        printf(" abort");
                    435:                if (dstat & DSTAT_BF)
                    436:                        printf(" bus fault");
                    437:                if (dstat & DSTAT_MDPE)
                    438:                        printf(" parity");
                    439:                if (dstat & DSTAT_DFE)
                    440:                        printf(" dma fifo empty");
                    441:                printf(", DSP=0x%x DSA=0x%x: ",
1.7       bouyer    442:                    (int)(bus_space_read_4(sc->sc_rt, sc->sc_rh, SIOP_DSP) -
1.17      bouyer    443:                    sc->sc_scriptaddr),
1.1       bouyer    444:                    bus_space_read_4(sc->sc_rt, sc->sc_rh, SIOP_DSA));
1.2       bouyer    445:                if (siop_cmd)
1.1       bouyer    446:                        printf("last msg_in=0x%x status=0x%x\n",
1.31      bouyer    447:                            siop_cmd->siop_tables.msg_in[0],
                    448:                            le32toh(siop_cmd->siop_tables.status));
1.20      bouyer    449:                else
                    450:                        printf("%s: current DSA invalid\n",
                    451:                            sc->sc_dev.dv_xname);
1.1       bouyer    452:                need_reset = 1;
                    453:                }
                    454:        }
                    455:        if (istat & ISTAT_SIP) {
                    456:                if (istat & ISTAT_DIP)
1.8       bouyer    457:                        delay(10);
1.35      bouyer    458:                /*
                    459:                 * Can't read sist0 & sist1 independantly, or we have to
                    460:                 * insert delay
                    461:                 */
                    462:                sist = bus_space_read_2(sc->sc_rt, sc->sc_rh, SIOP_SIST0);
1.1       bouyer    463:                sstat1 = bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_SSTAT1);
1.35      bouyer    464: #ifdef SIOP_DEBUG_INTR
                    465:                printf("scsi interrupt, sist=0x%x sstat1=0x%x "
                    466:                    "DSA=0x%x DSP=0x%lx\n", sist,
1.1       bouyer    467:                    bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_SSTAT1),
                    468:                    bus_space_read_4(sc->sc_rt, sc->sc_rh, SIOP_DSA),
1.8       bouyer    469:                    (u_long)(bus_space_read_4(sc->sc_rt, sc->sc_rh, SIOP_DSP) -
1.17      bouyer    470:                    sc->sc_scriptaddr));
1.1       bouyer    471: #endif
1.35      bouyer    472:                if (sist & SIST0_RST) {
1.2       bouyer    473:                        siop_handle_reset(sc);
                    474:                        /* no table to flush here */
1.1       bouyer    475:                        return 1;
                    476:                }
1.35      bouyer    477:                if (sist & SIST0_SGE) {
1.1       bouyer    478:                        if (siop_cmd)
1.40.2.2  nathanw   479:                                scsipi_printaddr(xs->xs_periph);
1.1       bouyer    480:                        else
                    481:                                printf("%s:", sc->sc_dev.dv_xname);
                    482:                        printf("scsi gross error\n");
                    483:                        goto reset;
                    484:                }
1.35      bouyer    485:                if ((sist & SIST0_MA) && need_reset == 0) {
1.1       bouyer    486:                        if (siop_cmd) {
1.8       bouyer    487:                                int scratcha0;
                    488:                                dstat = bus_space_read_1(sc->sc_rt, sc->sc_rh,
                    489:                                    SIOP_DSTAT);
1.3       bouyer    490:                                /*
                    491:                                 * first restore DSA, in case we were in a S/G
                    492:                                 * operation.
                    493:                                 */
                    494:                                bus_space_write_4(sc->sc_rt, sc->sc_rh,
1.4       bouyer    495:                                    SIOP_DSA, siop_cmd->dsa);
1.8       bouyer    496:                                scratcha0 = bus_space_read_1(sc->sc_rt,
                    497:                                    sc->sc_rh, SIOP_SCRATCHA);
1.1       bouyer    498:                                switch (sstat1 & SSTAT1_PHASE_MASK) {
                    499:                                case SSTAT1_PHASE_STATUS:
                    500:                                /*
                    501:                                 * previous phase may be aborted for any reason
                    502:                                 * ( for example, the target has less data to
                    503:                                 * transfer than requested). Just go to status
                    504:                                 * and the command should terminate.
                    505:                                 */
1.2       bouyer    506:                                        INCSTAT(siop_stat_intr_shortxfer);
1.8       bouyer    507:                                        if ((dstat & DSTAT_DFE) == 0)
                    508:                                                siop_clearfifo(sc);
1.2       bouyer    509:                                        /* no table to flush here */
1.31      bouyer    510:                                        CALL_SCRIPT(Ent_status);
1.1       bouyer    511:                                        return 1;
                    512:                                case SSTAT1_PHASE_MSGIN:
                    513:                                        /*
                    514:                                         * target may be ready to disconnect
                    515:                                         * Save data pointers just in case.
                    516:                                         */
1.2       bouyer    517:                                        INCSTAT(siop_stat_intr_xferdisc);
1.8       bouyer    518:                                        if (scratcha0 & A_flag_data)
                    519:                                                siop_sdp(siop_cmd);
                    520:                                        else if ((dstat & DSTAT_DFE) == 0)
                    521:                                                siop_clearfifo(sc);
                    522:                                        bus_space_write_1(sc->sc_rt, sc->sc_rh,
                    523:                                            SIOP_SCRATCHA,
                    524:                                            scratcha0 & ~A_flag_data);
1.2       bouyer    525:                                        siop_table_sync(siop_cmd,
                    526:                                            BUS_DMASYNC_PREREAD |
                    527:                                            BUS_DMASYNC_PREWRITE);
1.1       bouyer    528:                                        CALL_SCRIPT(Ent_msgin);
                    529:                                        return 1;
                    530:                                }
                    531:                                printf("%s: unexpected phase mismatch %d\n",
                    532:                                    sc->sc_dev.dv_xname,
                    533:                                    sstat1 & SSTAT1_PHASE_MASK);
                    534:                        } else {
                    535:                                printf("%s: phase mismatch without command\n",
                    536:                                    sc->sc_dev.dv_xname);
                    537:                        }
                    538:                        need_reset = 1;
                    539:                }
1.35      bouyer    540:                if (sist & SIST0_PAR) {
1.1       bouyer    541:                        /* parity error, reset */
                    542:                        if (siop_cmd)
1.40.2.2  nathanw   543:                                scsipi_printaddr(xs->xs_periph);
1.1       bouyer    544:                        else
                    545:                                printf("%s:", sc->sc_dev.dv_xname);
                    546:                        printf("parity error\n");
1.11      bouyer    547:                        goto reset;
1.1       bouyer    548:                }
1.35      bouyer    549:                if ((sist & (SIST1_STO << 8)) && need_reset == 0) {
1.1       bouyer    550:                        /* selection time out, assume there's no device here */
                    551:                        if (siop_cmd) {
                    552:                                siop_cmd->status = CMDST_DONE;
                    553:                                xs->error = XS_SELTIMEOUT;
1.31      bouyer    554:                                freetarget = 1;
1.1       bouyer    555:                                goto end;
                    556:                        } else {
                    557:                                printf("%s: selection timeout without "
                    558:                                    "command\n", sc->sc_dev.dv_xname);
                    559:                                need_reset = 1;
                    560:                        }
                    561:                }
1.35      bouyer    562:                if (sist & SIST0_UDC) {
1.1       bouyer    563:                        /*
                    564:                         * unexpected disconnect. Usually the target signals
                    565:                         * a fatal condition this way. Attempt to get sense.
                    566:                         */
1.36      bouyer    567:                         if (siop_cmd) {
                    568:                                siop_cmd->siop_tables.status =
                    569:                                    htole32(SCSI_CHECK);
                    570:                                goto end;
                    571:                        }
1.1       bouyer    572:                        printf("%s: unexpected disconnect without "
                    573:                            "command\n", sc->sc_dev.dv_xname);
1.2       bouyer    574:                        goto reset;
1.1       bouyer    575:                }
1.35      bouyer    576:                if (sist & (SIST1_SBMC << 8)) {
1.20      bouyer    577:                        /* SCSI bus mode change */
                    578:                        if (siop_modechange(sc) == 0 || need_reset == 1)
                    579:                                goto reset;
                    580:                        if ((istat & ISTAT_DIP) && (dstat & DSTAT_SIR)) {
                    581:                                /*
                    582:                                 * we have a script interrupt, it will
                    583:                                 * restart the script.
                    584:                                 */
                    585:                                goto scintr;
                    586:                        }
                    587:                        /*
                    588:                         * else we have to restart it ourselve, at the
                    589:                         * interrupted instruction.
                    590:                         */
                    591:                        bus_space_write_4(sc->sc_rt, sc->sc_rh, SIOP_DSP,
                    592:                            bus_space_read_4(sc->sc_rt, sc->sc_rh,
                    593:                            SIOP_DSP) - 8);
                    594:                        return 1;
                    595:                }
1.1       bouyer    596:                /* Else it's an unhandled exeption (for now). */
1.35      bouyer    597:                printf("%s: unhandled scsi interrupt, sist=0x%x sstat1=0x%x "
                    598:                    "DSA=0x%x DSP=0x%x\n", sc->sc_dev.dv_xname, sist,
1.1       bouyer    599:                    bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_SSTAT1),
                    600:                    bus_space_read_4(sc->sc_rt, sc->sc_rh, SIOP_DSA),
1.7       bouyer    601:                    (int)(bus_space_read_4(sc->sc_rt, sc->sc_rh, SIOP_DSP) -
1.17      bouyer    602:                    sc->sc_scriptaddr));
1.1       bouyer    603:                if (siop_cmd) {
                    604:                        siop_cmd->status = CMDST_DONE;
                    605:                        xs->error = XS_SELTIMEOUT;
                    606:                        goto end;
                    607:                }
                    608:                need_reset = 1;
                    609:        }
                    610:        if (need_reset) {
                    611: reset:
                    612:                /* fatal error, reset the bus */
1.26      bouyer    613:                siop_resetbus(sc);
1.2       bouyer    614:                /* no table to flush here */
1.1       bouyer    615:                return 1;
                    616:        }
                    617:
1.20      bouyer    618: scintr:
1.1       bouyer    619:        if ((istat & ISTAT_DIP) && (dstat & DSTAT_SIR)) { /* script interrupt */
                    620:                irqcode = bus_space_read_4(sc->sc_rt, sc->sc_rh,
                    621:                    SIOP_DSPS);
1.35      bouyer    622: #ifdef SIOP_DEBUG_INTR
1.2       bouyer    623:                printf("script interrupt 0x%x\n", irqcode);
                    624: #endif
                    625:                /*
1.31      bouyer    626:                 * no command, or an inactive command is only valid for a
                    627:                 * reselect interrupt
1.2       bouyer    628:                 */
1.31      bouyer    629:                if ((irqcode & 0x80) == 0) {
                    630:                        if (siop_cmd == NULL) {
1.40.2.2  nathanw   631:                                printf(
                    632:                        "%s: script interrupt (0x%x) with invalid DSA !!!\n",
                    633:                                    sc->sc_dev.dv_xname, irqcode);
1.31      bouyer    634:                                goto reset;
                    635:                        }
1.40.2.2  nathanw   636:                        if (siop_cmd->status != CMDST_ACTIVE) {
1.31      bouyer    637:                                printf("%s: command with invalid status "
                    638:                                    "(IRQ code 0x%x current status %d) !\n",
                    639:                                    sc->sc_dev.dv_xname,
                    640:                                    irqcode, siop_cmd->status);
                    641:                                xs = NULL;
                    642:                        }
1.7       bouyer    643:                }
1.1       bouyer    644:                switch(irqcode) {
                    645:                case A_int_err:
1.2       bouyer    646:                        printf("error, DSP=0x%x\n",
1.17      bouyer    647:                            (int)(bus_space_read_4(sc->sc_rt, sc->sc_rh,
                    648:                            SIOP_DSP) - sc->sc_scriptaddr));
1.2       bouyer    649:                        if (xs) {
                    650:                                xs->error = XS_SELTIMEOUT;
                    651:                                goto end;
                    652:                        } else {
                    653:                                goto reset;
                    654:                        }
1.31      bouyer    655:                case A_int_reseltarg:
                    656:                        printf("%s: reselect with invalid target\n",
                    657:                                    sc->sc_dev.dv_xname);
                    658:                        goto reset;
1.35      bouyer    659:                case A_int_resellun:
                    660:                        INCSTAT(siop_stat_intr_lunresel);
                    661:                        target = bus_space_read_1(sc->sc_rt, sc->sc_rh,
                    662:                            SIOP_SCRATCHA) & 0xf;
                    663:                        lun = bus_space_read_1(sc->sc_rt, sc->sc_rh,
                    664:                            SIOP_SCRATCHA + 1);
                    665:                        tag = bus_space_read_1(sc->sc_rt, sc->sc_rh,
                    666:                            SIOP_SCRATCHA + 2);
                    667:                        siop_target = sc->targets[target];
                    668:                        if (siop_target == NULL) {
                    669:                                printf("%s: reselect with invalid "
                    670:                                    "target %d\n", sc->sc_dev.dv_xname, target);
                    671:                                goto reset;
                    672:                        }
                    673:                        siop_lun = siop_target->siop_lun[lun];
                    674:                        if (siop_lun == NULL) {
                    675:                                printf("%s: target %d reselect with invalid "
                    676:                                    "lun %d\n", sc->sc_dev.dv_xname,
                    677:                                    target, lun);
                    678:                                goto reset;
                    679:                        }
                    680:                        if (siop_lun->siop_tag[tag].active == NULL) {
                    681:                                printf("%s: target %d lun %d tag %d reselect "
                    682:                                    "without command\n", sc->sc_dev.dv_xname,
                    683:                                    target, lun, tag);
                    684:                                goto reset;
                    685:                        }
                    686:                        siop_cmd = siop_lun->siop_tag[tag].active;
                    687:                        bus_space_write_4(sc->sc_rt, sc->sc_rh, SIOP_DSP,
                    688:                            siop_cmd->dsa + sizeof(struct siop_xfer_common) +
                    689:                            Ent_ldsa_reload_dsa);
1.38      bouyer    690:                        siop_table_sync(siop_cmd, BUS_DMASYNC_PREWRITE);
1.35      bouyer    691:                        return 1;
1.31      bouyer    692:                case A_int_reseltag:
                    693:                        printf("%s: reselect with invalid tag\n",
                    694:                                    sc->sc_dev.dv_xname);
                    695:                        goto reset;
1.1       bouyer    696:                case A_int_msgin:
1.31      bouyer    697:                {
                    698:                        int msgin = bus_space_read_1(sc->sc_rt, sc->sc_rh,
                    699:                            SIOP_SFBR);
                    700:                        if (msgin == MSG_MESSAGE_REJECT) {
1.8       bouyer    701:                                int msg, extmsg;
1.31      bouyer    702:                                if (siop_cmd->siop_tables.msg_out[0] & 0x80) {
1.8       bouyer    703:                                        /*
                    704:                                         * message was part of a identify +
                    705:                                         * something else. Identify shoudl't
                    706:                                         * have been rejected.
                    707:                                         */
1.31      bouyer    708:                                        msg = siop_cmd->siop_tables.msg_out[1];
1.8       bouyer    709:                                        extmsg =
1.31      bouyer    710:                                            siop_cmd->siop_tables.msg_out[3];
1.8       bouyer    711:                                } else {
1.31      bouyer    712:                                        msg = siop_cmd->siop_tables.msg_out[0];
1.8       bouyer    713:                                        extmsg =
1.31      bouyer    714:                                            siop_cmd->siop_tables.msg_out[2];
1.8       bouyer    715:                                }
                    716:                                if (msg == MSG_MESSAGE_REJECT) {
1.2       bouyer    717:                                        /* MSG_REJECT  for a MSG_REJECT  !*/
1.9       bouyer    718:                                        if (xs)
1.40.2.2  nathanw   719:                                                scsipi_printaddr(xs->xs_periph);
1.9       bouyer    720:                                        else
                    721:                                                printf("%s: ",
                    722:                                                   sc->sc_dev.dv_xname);
                    723:                                        printf("our reject message was "
                    724:                                            "rejected\n");
1.2       bouyer    725:                                        goto reset;
                    726:                                }
1.8       bouyer    727:                                if (msg == MSG_EXTENDED &&
                    728:                                    extmsg == MSG_EXT_WDTR) {
1.9       bouyer    729:                                        /* WDTR rejected, initiate sync */
1.31      bouyer    730:                                        if ((siop_target->flags & TARF_SYNC)
                    731:                                            == 0) {
                    732:                                                siop_target->status = TARST_OK;
1.40.2.2  nathanw   733:                                                siop_update_xfer_mode(sc,
                    734:                                                    target);
1.26      bouyer    735:                                                /* no table to flush here */
                    736:                                                CALL_SCRIPT(Ent_msgin_ack);
                    737:                                                return 1;
                    738:                                        }
1.9       bouyer    739:                                        siop_target->status = TARST_SYNC_NEG;
1.35      bouyer    740:                                        siop_sdtr_msg(siop_cmd, 0,
                    741:                                            sc->minsync, sc->maxoff);
1.9       bouyer    742:                                        siop_table_sync(siop_cmd,
                    743:                                            BUS_DMASYNC_PREREAD |
                    744:                                            BUS_DMASYNC_PREWRITE);
                    745:                                        CALL_SCRIPT(Ent_send_msgout);
                    746:                                        return 1;
1.8       bouyer    747:                                } else if (msg == MSG_EXTENDED &&
                    748:                                    extmsg == MSG_EXT_SDTR) {
1.7       bouyer    749:                                        /* sync rejected */
1.40.2.2  nathanw   750:                                        siop_target->offset = 0;
                    751:                                        siop_target->period = 0;
1.31      bouyer    752:                                        siop_target->status = TARST_OK;
1.40.2.2  nathanw   753:                                        siop_update_xfer_mode(sc, target);
1.9       bouyer    754:                                        /* no table to flush here */
                    755:                                        CALL_SCRIPT(Ent_msgin_ack);
                    756:                                        return 1;
1.35      bouyer    757:                                } else if (msg == MSG_SIMPLE_Q_TAG ||
                    758:                                    msg == MSG_HEAD_OF_Q_TAG ||
                    759:                                    msg == MSG_ORDERED_Q_TAG) {
                    760:                                        if (siop_handle_qtag_reject(
                    761:                                            siop_cmd) == -1)
                    762:                                                goto reset;
                    763:                                        CALL_SCRIPT(Ent_msgin_ack);
                    764:                                        return 1;
1.9       bouyer    765:                                }
                    766:                                if (xs)
1.40.2.2  nathanw   767:                                        scsipi_printaddr(xs->xs_periph);
1.9       bouyer    768:                                else
                    769:                                        printf("%s: ", sc->sc_dev.dv_xname);
                    770:                                if (msg == MSG_EXTENDED) {
                    771:                                        printf("scsi message reject, extended "
                    772:                                            "message sent was 0x%x\n", extmsg);
                    773:                                } else {
                    774:                                        printf("scsi message reject, message "
                    775:                                            "sent was 0x%x\n", msg);
1.7       bouyer    776:                                }
1.2       bouyer    777:                                /* no table to flush here */
                    778:                                CALL_SCRIPT(Ent_msgin_ack);
                    779:                                return 1;
                    780:                        }
1.9       bouyer    781:                        if (xs)
1.40.2.2  nathanw   782:                                scsipi_printaddr(xs->xs_periph);
1.9       bouyer    783:                        else
                    784:                                printf("%s: ", sc->sc_dev.dv_xname);
1.8       bouyer    785:                        printf("unhandled message 0x%x\n",
1.31      bouyer    786:                            siop_cmd->siop_tables.msg_in[0]);
1.35      bouyer    787:                        siop_cmd->siop_tables.msg_out[0] = MSG_MESSAGE_REJECT;
1.31      bouyer    788:                        siop_cmd->siop_tables.t_msgout.count= htole32(1);
1.2       bouyer    789:                        siop_table_sync(siop_cmd,
                    790:                            BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
                    791:                        CALL_SCRIPT(Ent_send_msgout);
                    792:                        return 1;
1.31      bouyer    793:                }
1.2       bouyer    794:                case A_int_extmsgin:
1.35      bouyer    795: #ifdef SIOP_DEBUG_INTR
1.2       bouyer    796:                        printf("extended message: msg 0x%x len %d\n",
1.31      bouyer    797:                            siop_cmd->siop_tables.msg_in[2],
                    798:                            siop_cmd->siop_tables.msg_in[1]);
1.2       bouyer    799: #endif
1.31      bouyer    800:                        if (siop_cmd->siop_tables.msg_in[1] > 6)
1.8       bouyer    801:                                printf("%s: extended message too big (%d)\n",
                    802:                                    sc->sc_dev.dv_xname,
1.31      bouyer    803:                                    siop_cmd->siop_tables.msg_in[1]);
                    804:                        siop_cmd->siop_tables.t_extmsgdata.count =
                    805:                            htole32(siop_cmd->siop_tables.msg_in[1] - 1);
1.2       bouyer    806:                        siop_table_sync(siop_cmd,
                    807:                            BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
                    808:                        CALL_SCRIPT(Ent_get_extmsgdata);
                    809:                        return 1;
                    810:                case A_int_extmsgdata:
1.35      bouyer    811: #ifdef SIOP_DEBUG_INTR
1.2       bouyer    812:                        {
                    813:                        int i;
                    814:                        printf("extended message: 0x%x, data:",
1.31      bouyer    815:                            siop_cmd->siop_tables.msg_in[2]);
                    816:                        for (i = 3; i < 2 + siop_cmd->siop_tables.msg_in[1];
1.2       bouyer    817:                            i++)
                    818:                                printf(" 0x%x",
1.31      bouyer    819:                                    siop_cmd->siop_tables.msg_in[i]);
1.2       bouyer    820:                        printf("\n");
                    821:                        }
                    822: #endif
1.31      bouyer    823:                        if (siop_cmd->siop_tables.msg_in[2] == MSG_EXT_WDTR) {
1.14      bouyer    824:                                switch (siop_wdtr_neg(siop_cmd)) {
                    825:                                case SIOP_NEG_MSGOUT:
1.31      bouyer    826:                                        siop_update_scntl3(sc,
                    827:                                            siop_cmd->siop_target);
1.14      bouyer    828:                                        siop_table_sync(siop_cmd,
                    829:                                            BUS_DMASYNC_PREREAD |
                    830:                                            BUS_DMASYNC_PREWRITE);
                    831:                                        CALL_SCRIPT(Ent_send_msgout);
1.31      bouyer    832:                                        return(1);
1.26      bouyer    833:                                case SIOP_NEG_ACK:
1.31      bouyer    834:                                        siop_update_scntl3(sc,
                    835:                                            siop_cmd->siop_target);
1.26      bouyer    836:                                        CALL_SCRIPT(Ent_msgin_ack);
1.31      bouyer    837:                                        return(1);
1.14      bouyer    838:                                default:
                    839:                                        panic("invalid retval from "
                    840:                                            "siop_wdtr_neg()");
                    841:                                }
1.7       bouyer    842:                                return(1);
                    843:                        }
1.31      bouyer    844:                        if (siop_cmd->siop_tables.msg_in[2] == MSG_EXT_SDTR) {
1.14      bouyer    845:                                switch (siop_sdtr_neg(siop_cmd)) {
                    846:                                case SIOP_NEG_MSGOUT:
1.31      bouyer    847:                                        siop_update_scntl3(sc,
                    848:                                            siop_cmd->siop_target);
1.14      bouyer    849:                                        siop_table_sync(siop_cmd,
                    850:                                            BUS_DMASYNC_PREREAD |
                    851:                                            BUS_DMASYNC_PREWRITE);
                    852:                                        CALL_SCRIPT(Ent_send_msgout);
1.31      bouyer    853:                                        return(1);
1.14      bouyer    854:                                case SIOP_NEG_ACK:
1.31      bouyer    855:                                        siop_update_scntl3(sc,
                    856:                                            siop_cmd->siop_target);
1.14      bouyer    857:                                        CALL_SCRIPT(Ent_msgin_ack);
1.31      bouyer    858:                                        return(1);
1.14      bouyer    859:                                default:
                    860:                                        panic("invalid retval from "
                    861:                                            "siop_wdtr_neg()");
                    862:                                }
1.7       bouyer    863:                                return(1);
1.2       bouyer    864:                        }
1.7       bouyer    865:                        /* send a message reject */
1.35      bouyer    866:                        siop_cmd->siop_tables.msg_out[0] = MSG_MESSAGE_REJECT;
1.31      bouyer    867:                        siop_cmd->siop_tables.t_msgout.count = htole32(1);
1.2       bouyer    868:                        siop_table_sync(siop_cmd,
                    869:                            BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
                    870:                        CALL_SCRIPT(Ent_send_msgout);
                    871:                        return 1;
1.1       bouyer    872:                case A_int_disc:
1.2       bouyer    873:                        INCSTAT(siop_stat_intr_sdp);
1.1       bouyer    874:                        offset = bus_space_read_1(sc->sc_rt, sc->sc_rh,
                    875:                            SIOP_SCRATCHA + 1);
1.35      bouyer    876: #ifdef SIOP_DEBUG_DR
1.1       bouyer    877:                        printf("disconnect offset %d\n", offset);
                    878: #endif
                    879:                        if (offset > SIOP_NSG) {
                    880:                                printf("%s: bad offset for disconnect (%d)\n",
                    881:                                    sc->sc_dev.dv_xname, offset);
                    882:                                goto reset;
                    883:                        }
                    884:                        /*
                    885:                         * offset == SIOP_NSG may be a valid condition if
                    886:                         * we get a sdp when the xfer is done.
                    887:                         * Don't call memmove in this case.
                    888:                         */
                    889:                        if (offset < SIOP_NSG) {
1.31      bouyer    890:                                memmove(&siop_cmd->siop_tables.data[0],
                    891:                                    &siop_cmd->siop_tables.data[offset],
1.1       bouyer    892:                                    (SIOP_NSG - offset) * sizeof(scr_table_t));
1.2       bouyer    893:                                siop_table_sync(siop_cmd,
                    894:                                    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1.1       bouyer    895:                        }
1.31      bouyer    896:                        CALL_SCRIPT(Ent_script_sched);
1.1       bouyer    897:                        return 1;
                    898:                case A_int_resfail:
                    899:                        printf("reselect failed\n");
1.31      bouyer    900:                        CALL_SCRIPT(Ent_script_sched);
1.1       bouyer    901:                        return  1;
                    902:                case A_int_done:
1.2       bouyer    903:                        if (xs == NULL) {
1.8       bouyer    904:                                printf("%s: done without command, DSA=0x%lx\n",
                    905:                                    sc->sc_dev.dv_xname, (u_long)siop_cmd->dsa);
                    906:                                siop_cmd->status = CMDST_FREE;
1.31      bouyer    907:                                CALL_SCRIPT(Ent_script_sched);
1.2       bouyer    908:                                return 1;
                    909:                        }
1.35      bouyer    910: #ifdef SIOP_DEBUG_INTR
1.8       bouyer    911:                        printf("done, DSA=0x%lx target id 0x%x last msg "
                    912:                            "in=0x%x status=0x%x\n", (u_long)siop_cmd->dsa,
1.31      bouyer    913:                            le32toh(siop_cmd->siop_tables.id),
                    914:                            siop_cmd->siop_tables.msg_in[0],
                    915:                            le32toh(siop_cmd->siop_tables.status));
1.1       bouyer    916: #endif
1.2       bouyer    917:                        INCSTAT(siop_stat_intr_done);
1.40.2.2  nathanw   918:                        siop_cmd->status = CMDST_DONE;
1.1       bouyer    919:                        goto end;
                    920:                default:
                    921:                        printf("unknown irqcode %x\n", irqcode);
1.35      bouyer    922:                        if (xs) {
                    923:                                xs->error = XS_SELTIMEOUT;
                    924:                                goto end;
                    925:                        }
                    926:                        goto reset;
1.1       bouyer    927:                }
                    928:                return 1;
                    929:        }
1.2       bouyer    930:        /* We just should't get there */
                    931:        panic("siop_intr: I shouldn't be there !");
                    932:        return 1;
1.1       bouyer    933: end:
1.39      bouyer    934:        /*
                    935:         * restart the script now if command completed properly
1.40.2.2  nathanw   936:         * Otherwise wait for siop_scsicmd_end(), we may need to cleanup the
                    937:         * queue
1.39      bouyer    938:         */
1.40.2.2  nathanw   939:        xs->status = le32toh(siop_cmd->siop_tables.status);
                    940:        if (xs->status == SCSI_OK)
1.39      bouyer    941:                CALL_SCRIPT(Ent_script_sched);
                    942:        else
                    943:                restart = 1;
1.35      bouyer    944:        siop_lun->siop_tag[tag].active = NULL;
1.40.2.2  nathanw   945:        siop_scsicmd_end(siop_cmd);
                    946:        if (freetarget && siop_target->status == TARST_PROBING)
                    947:                siop_del_dev(sc, target, lun);
1.39      bouyer    948:        if (restart)
                    949:                CALL_SCRIPT(Ent_script_sched);
1.40.2.2  nathanw   950:        if (sc->sc_flags & SCF_CHAN_NOSLOT) {
                    951:                /* a command terminated, so we have free slots now */
                    952:                sc->sc_flags &= ~SCF_CHAN_NOSLOT;
                    953:                scsipi_channel_thaw(&sc->sc_chan, 1);
                    954:        }
                    955:
1.2       bouyer    956:        return 1;
                    957: }
                    958:
                    959: void
                    960: siop_scsicmd_end(siop_cmd)
                    961:        struct siop_cmd *siop_cmd;
                    962: {
                    963:        struct scsipi_xfer *xs = siop_cmd->xs;
1.31      bouyer    964:        struct siop_softc *sc = siop_cmd->siop_sc;
1.2       bouyer    965:
1.40.2.2  nathanw   966:        switch(xs->status) {
1.36      bouyer    967:        case SCSI_OK:
1.40.2.2  nathanw   968:                xs->error = XS_NOERROR;
1.36      bouyer    969:                break;
                    970:        case SCSI_BUSY:
                    971:                xs->error = XS_BUSY;
                    972:                break;
                    973:        case SCSI_CHECK:
1.40.2.2  nathanw   974:                xs->error = XS_BUSY;
                    975:                /* remove commands in the queue and scheduler */
                    976:                siop_unqueue(sc, xs->xs_periph->periph_target,
                    977:                    xs->xs_periph->periph_lun);
1.36      bouyer    978:                break;
                    979:        case SCSI_QUEUE_FULL:
                    980:                INCSTAT(siop_stat_intr_qfull);
                    981: #ifdef SIOP_DEBUG
                    982:                printf("%s:%d:%d: queue full (tag %d)\n", sc->sc_dev.dv_xname,
1.40.2.2  nathanw   983:                    xs->xs_periph->periph_target,
                    984:                    xs->xs_periph->periph_lun, siop_cmd->tag);
1.36      bouyer    985: #endif
1.40.2.2  nathanw   986:                xs->error = XS_BUSY;
                    987:                break;
1.36      bouyer    988:        case SCSI_SIOP_NOCHECK:
                    989:                /*
                    990:                 * don't check status, xs->error is already valid
                    991:                 */
                    992:                break;
                    993:        case SCSI_SIOP_NOSTATUS:
                    994:                /*
                    995:                 * the status byte was not updated, cmd was
                    996:                 * aborted
                    997:                 */
                    998:                xs->error = XS_SELTIMEOUT;
                    999:                break;
                   1000:        default:
                   1001:                xs->error = XS_DRIVER_STUFFUP;
                   1002:        }
1.40.2.2  nathanw  1003:        if (xs->xs_control & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) {
1.1       bouyer   1004:                bus_dmamap_sync(sc->sc_dmat, siop_cmd->dmamap_data, 0,
                   1005:                    siop_cmd->dmamap_data->dm_mapsize,
                   1006:                    (xs->xs_control & XS_CTL_DATA_IN) ?
                   1007:                    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
                   1008:                bus_dmamap_unload(sc->sc_dmat, siop_cmd->dmamap_data);
                   1009:        }
                   1010:        bus_dmamap_unload(sc->sc_dmat, siop_cmd->dmamap_cmd);
                   1011:        callout_stop(&siop_cmd->xs->xs_callout);
                   1012:        siop_cmd->status = CMDST_FREE;
1.40.2.2  nathanw  1013:        TAILQ_INSERT_TAIL(&sc->free_list, siop_cmd, next);
1.1       bouyer   1014:        xs->resid = 0;
1.40.2.2  nathanw  1015:        scsipi_done (xs);
                   1016: }
                   1017:
                   1018: void
                   1019: siop_unqueue(sc, target, lun)
                   1020:        struct siop_softc *sc;
                   1021:        int target;
                   1022:        int lun;
                   1023: {
                   1024:        int slot, tag;
                   1025:        struct siop_cmd *siop_cmd;
                   1026:        struct siop_lun *siop_lun = sc->targets[target]->siop_lun[lun];
                   1027:
                   1028:        /* first make sure to read valid data */
                   1029:        siop_script_sync(sc, BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
                   1030:
                   1031:        for (tag = 1; tag < SIOP_NTAG; tag++) {
                   1032:                /* look for commands in the scheduler, not yet started */
                   1033:                if (siop_lun->siop_tag[tag].active == NULL)
                   1034:                        continue;
                   1035:                siop_cmd = siop_lun->siop_tag[tag].active;
                   1036:                for (slot = 0; slot <= sc->sc_currschedslot; slot++) {
                   1037:                        if (siop_script_read(sc,
                   1038:                            (Ent_script_sched_slot0 / 4) + slot * 2 + 1) ==
                   1039:                            siop_cmd->dsa + sizeof(struct siop_xfer_common) +
                   1040:                            Ent_ldsa_select)
                   1041:                                break;
                   1042:                }
                   1043:                if (slot >  sc->sc_currschedslot)
                   1044:                        continue; /* didn't find it */
                   1045:                if (siop_script_read(sc,
                   1046:                    (Ent_script_sched_slot0 / 4) + slot * 2) == 0x80000000)
                   1047:                        continue; /* already started */
                   1048:                /* clear the slot */
                   1049:                siop_script_write(sc, (Ent_script_sched_slot0 / 4) + slot * 2,
                   1050:                    0x80000000);
                   1051:                /* ask to requeue */
                   1052:                siop_cmd->xs->error = XS_REQUEUE;
                   1053:                siop_cmd->xs->status = SCSI_SIOP_NOCHECK;
                   1054:                siop_lun->siop_tag[tag].active = NULL;
                   1055:                siop_scsicmd_end(siop_cmd);
                   1056:        }
                   1057:        /* update sc_currschedslot */
                   1058:        sc->sc_currschedslot = 0;
                   1059:        for (slot = SIOP_NSLOTS - 1; slot >= 0; slot--) {
                   1060:                if (siop_script_read(sc,
                   1061:                    (Ent_script_sched_slot0 / 4) + slot * 2) != 0x80000000)
                   1062:                        sc->sc_currschedslot = slot;
                   1063:        }
1.7       bouyer   1064: }
                   1065:
1.2       bouyer   1066: /*
1.31      bouyer   1067:  * handle a rejected queue tag message: the command will run untagged,
                   1068:  * has to adjust the reselect script.
                   1069:  */
                   1070: int
                   1071: siop_handle_qtag_reject(siop_cmd)
                   1072:        struct siop_cmd *siop_cmd;
                   1073: {
                   1074:        struct siop_softc *sc = siop_cmd->siop_sc;
1.40.2.2  nathanw  1075:        int target = siop_cmd->xs->xs_periph->periph_target;
                   1076:        int lun = siop_cmd->xs->xs_periph->periph_lun;
1.31      bouyer   1077:        int tag = siop_cmd->siop_tables.msg_out[2];
1.35      bouyer   1078:        struct siop_lun *siop_lun = sc->targets[target]->siop_lun[lun];
1.31      bouyer   1079:
1.36      bouyer   1080: #ifdef SIOP_DEBUG
                   1081:        printf("%s:%d:%d: tag message %d (%d) rejected (status %d)\n",
                   1082:            sc->sc_dev.dv_xname, target, lun, tag, siop_cmd->tag,
                   1083:            siop_cmd->status);
                   1084: #endif
                   1085:
1.35      bouyer   1086:        if (siop_lun->siop_tag[0].active != NULL) {
1.36      bouyer   1087:                printf("%s: untagged command already running for target %d "
                   1088:                    "lun %d (status %d)\n", sc->sc_dev.dv_xname, target, lun,
                   1089:                    siop_lun->siop_tag[0].active->status);
1.35      bouyer   1090:                return -1;
                   1091:        }
                   1092:        /* clear tag slot */
                   1093:        siop_lun->siop_tag[tag].active = NULL;
                   1094:        /* add command to non-tagged slot */
                   1095:        siop_lun->siop_tag[0].active = siop_cmd;
                   1096:        siop_cmd->tag = 0;
                   1097:        /* adjust reselect script if there is one */
                   1098:        if (siop_lun->siop_tag[0].reseloff > 0) {
                   1099:                siop_script_write(sc,
                   1100:                    siop_lun->siop_tag[0].reseloff + 1,
                   1101:                    siop_cmd->dsa + sizeof(struct siop_xfer_common) +
                   1102:                    Ent_ldsa_reload_dsa);
1.38      bouyer   1103:                siop_table_sync(siop_cmd, BUS_DMASYNC_PREWRITE);
1.35      bouyer   1104:        }
1.31      bouyer   1105:        return 0;
                   1106: }
                   1107:
                   1108: /*
                   1109:  * handle a bus reset: reset chip, unqueue all active commands, free all
                   1110:  * target struct and report loosage to upper layer.
1.2       bouyer   1111:  * As the upper layer may requeue immediatly we have to first store
                   1112:  * all active commands in a temporary queue.
                   1113:  */
                   1114: void
                   1115: siop_handle_reset(sc)
                   1116:        struct siop_softc *sc;
                   1117: {
1.40.2.2  nathanw  1118:        struct siop_cmd *siop_cmd;
1.31      bouyer   1119:        struct siop_lun *siop_lun;
1.35      bouyer   1120:        int target, lun, tag;
1.2       bouyer   1121:        /*
                   1122:         * scsi bus reset. reset the chip and restart
                   1123:         * the queue. Need to clean up all active commands
                   1124:         */
                   1125:        printf("%s: scsi bus reset\n", sc->sc_dev.dv_xname);
                   1126:        /* stop, reset and restart the chip */
                   1127:        siop_reset(sc);
1.40.2.2  nathanw  1128:        if (sc->sc_flags & SCF_CHAN_NOSLOT) {
                   1129:                /* chip has been reset, all slots are free now */
                   1130:                sc->sc_flags &= ~SCF_CHAN_NOSLOT;
                   1131:                scsipi_channel_thaw(&sc->sc_chan, 1);
                   1132:        }
1.36      bouyer   1133:        /*
                   1134:         * Process all commands: first commmands being executed
                   1135:         */
1.40.2.2  nathanw  1136:        for (target = 0; target < sc->sc_chan.chan_ntargets;
1.7       bouyer   1137:            target++) {
                   1138:                if (sc->targets[target] == NULL)
                   1139:                        continue;
                   1140:                for (lun = 0; lun < 8; lun++) {
1.35      bouyer   1141:                        siop_lun = sc->targets[target]->siop_lun[lun];
1.31      bouyer   1142:                        if (siop_lun == NULL)
                   1143:                                continue;
1.35      bouyer   1144:                        for (tag = 0; tag <
                   1145:                            ((sc->targets[target]->flags & TARF_TAG) ?
1.36      bouyer   1146:                            SIOP_NTAG : 1);
1.35      bouyer   1147:                            tag++) {
                   1148:                                siop_cmd = siop_lun->siop_tag[tag].active;
                   1149:                                if (siop_cmd == NULL)
                   1150:                                        continue;
1.40.2.2  nathanw  1151:                                scsipi_printaddr(siop_cmd->xs->xs_periph);
                   1152:                                printf("command with tag id %d reset\n", tag);
                   1153:                                siop_cmd->xs->error =
                   1154:                                    (siop_cmd->flags & CMDFL_TIMEOUT) ?
                   1155:                                    XS_TIMEOUT : XS_RESET;
                   1156:                                siop_cmd->xs->status = SCSI_SIOP_NOCHECK;
1.35      bouyer   1157:                                siop_lun->siop_tag[tag].active = NULL;
1.40.2.2  nathanw  1158:                                siop_cmd->status = CMDST_DONE;
                   1159:                                siop_scsicmd_end(siop_cmd);
1.35      bouyer   1160:                        }
1.2       bouyer   1161:                }
1.7       bouyer   1162:                sc->targets[target]->status = TARST_ASYNC;
1.31      bouyer   1163:                sc->targets[target]->flags &= ~TARF_ISWIDE;
1.40.2.2  nathanw  1164:                sc->targets[target]->period = sc->targets[target]->offset = 0;
                   1165:                siop_update_xfer_mode(sc, target);
1.31      bouyer   1166:        }
1.40.2.2  nathanw  1167:
                   1168:        scsipi_async_event(&sc->sc_chan, ASYNC_EVENT_RESET, NULL);
1.1       bouyer   1169: }
                   1170:
1.40.2.2  nathanw  1171: void
                   1172: siop_scsipi_request(chan, req, arg)
                   1173:        struct scsipi_channel *chan;
                   1174:        scsipi_adapter_req_t req;
                   1175:        void *arg;
1.1       bouyer   1176: {
1.40.2.2  nathanw  1177:        struct scsipi_xfer *xs;
                   1178:        struct scsipi_periph *periph;
                   1179:        struct siop_softc *sc = (void *)chan->chan_adapter->adapt_dev;
1.1       bouyer   1180:        struct siop_cmd *siop_cmd;
                   1181:        int s, error, i;
1.40.2.2  nathanw  1182:        int target;
                   1183:        int lun;
1.1       bouyer   1184:
1.40.2.2  nathanw  1185:        switch (req) {
                   1186:        case ADAPTER_REQ_RUN_XFER:
                   1187:                xs = arg;
                   1188:                periph = xs->xs_periph;
                   1189:                target = periph->periph_target;
                   1190:                lun = periph->periph_lun;
                   1191:
                   1192:                s = splbio();
1.35      bouyer   1193: #ifdef SIOP_DEBUG_SCHED
1.40.2.2  nathanw  1194:                printf("starting cmd for %d:%d\n", target, lun);
1.1       bouyer   1195: #endif
1.40.2.2  nathanw  1196:                siop_cmd = TAILQ_FIRST(&sc->free_list);
                   1197:                if (siop_cmd == NULL) {
                   1198:                        xs->error = XS_RESOURCE_SHORTAGE;
                   1199:                        scsipi_done(xs);
                   1200:                        splx(s);
                   1201:                        return;
1.16      bouyer   1202:                }
1.40.2.4  nathanw  1203:                TAILQ_REMOVE(&sc->free_list, siop_cmd, next);
1.1       bouyer   1204: #ifdef DIAGNOSTIC
1.40.2.2  nathanw  1205:                if (siop_cmd->status != CMDST_FREE)
                   1206:                        panic("siop_scsicmd: new cmd not free");
1.1       bouyer   1207: #endif
1.40.2.2  nathanw  1208:                if (sc->targets[target] == NULL) {
1.35      bouyer   1209: #ifdef SIOP_DEBUG
1.40.2.2  nathanw  1210:                        printf("%s: alloc siop_target for target %d\n",
                   1211:                                sc->sc_dev.dv_xname, target);
1.31      bouyer   1212: #endif
1.40.2.2  nathanw  1213:                        sc->targets[target] =
                   1214:                            malloc(sizeof(struct siop_target),
                   1215:                                M_DEVBUF, M_NOWAIT);
                   1216:                        if (sc->targets[target] == NULL) {
                   1217:                                printf("%s: can't malloc memory for "
                   1218:                                    "target %d\n", sc->sc_dev.dv_xname, target);
                   1219:                                xs->error = XS_RESOURCE_SHORTAGE;
                   1220:                                scsipi_done(xs);
                   1221:                                splx(s);
                   1222:                                return;
                   1223:                        }
                   1224:                        sc->targets[target]->status = TARST_PROBING;
                   1225:                        sc->targets[target]->flags = 0;
                   1226:                        sc->targets[target]->id =
                   1227:                            sc->clock_div << 24; /* scntl3 */
                   1228:                        sc->targets[target]->id |=  target << 16; /* id */
                   1229:                        /* sc->targets[target]->id |= 0x0 << 8; scxfer is 0 */
                   1230:
                   1231:                        /* get a lun switch script */
                   1232:                        sc->targets[target]->lunsw = siop_get_lunsw(sc);
                   1233:                        if (sc->targets[target]->lunsw == NULL) {
                   1234:                                printf("%s: can't alloc lunsw for target %d\n",
                   1235:                                    sc->sc_dev.dv_xname, target);
                   1236:                                xs->error = XS_RESOURCE_SHORTAGE;
                   1237:                                scsipi_done(xs);
                   1238:                                splx(s);
                   1239:                                return;
                   1240:                        }
                   1241:                        for (i=0; i < 8; i++)
                   1242:                                sc->targets[target]->siop_lun[i] = NULL;
                   1243:                        siop_add_reselsw(sc, target);
1.31      bouyer   1244:                }
1.35      bouyer   1245:                if (sc->targets[target]->siop_lun[lun] == NULL) {
1.40.2.2  nathanw  1246:                        sc->targets[target]->siop_lun[lun] =
1.40.2.6! nathanw  1247:                            malloc(sizeof(struct siop_lun), M_DEVBUF,
        !          1248:                            M_NOWAIT|M_ZERO);
1.40.2.2  nathanw  1249:                        if (sc->targets[target]->siop_lun[lun] == NULL) {
                   1250:                                printf("%s: can't alloc siop_lun for "
                   1251:                                    "target %d lun %d\n",
                   1252:                                    sc->sc_dev.dv_xname, target, lun);
                   1253:                                xs->error = XS_RESOURCE_SHORTAGE;
                   1254:                                scsipi_done(xs);
                   1255:                                splx(s);
                   1256:                                return;
                   1257:                        }
                   1258:                }
                   1259:                siop_cmd->siop_target = sc->targets[target];
                   1260:                siop_cmd->xs = xs;
                   1261:                siop_cmd->flags = 0;
                   1262:                siop_cmd->status = CMDST_READY;
                   1263:
                   1264:                /* load the DMA maps */
                   1265:                error = bus_dmamap_load(sc->sc_dmat, siop_cmd->dmamap_cmd,
                   1266:                    xs->cmd, xs->cmdlen, NULL, BUS_DMA_NOWAIT);
1.1       bouyer   1267:                if (error) {
1.40.2.2  nathanw  1268:                        printf("%s: unable to load cmd DMA map: %d\n",
1.1       bouyer   1269:                            sc->sc_dev.dv_xname, error);
                   1270:                        xs->error = XS_DRIVER_STUFFUP;
1.40.2.2  nathanw  1271:                        scsipi_done(xs);
1.31      bouyer   1272:                        splx(s);
1.40.2.2  nathanw  1273:                        return;
1.1       bouyer   1274:                }
1.40.2.2  nathanw  1275:                if (xs->xs_control & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) {
                   1276:                        error = bus_dmamap_load(sc->sc_dmat,
                   1277:                            siop_cmd->dmamap_data, xs->data, xs->datalen,
1.40.2.3  nathanw  1278:                            NULL, BUS_DMA_NOWAIT | BUS_DMA_STREAMING |
                   1279:                            ((xs->xs_control & XS_CTL_DATA_IN) ?
                   1280:                             BUS_DMA_READ : BUS_DMA_WRITE));
1.40.2.2  nathanw  1281:                        if (error) {
                   1282:                                printf("%s: unable to load cmd DMA map: %d",
                   1283:                                    sc->sc_dev.dv_xname, error);
                   1284:                                xs->error = XS_DRIVER_STUFFUP;
                   1285:                                scsipi_done(xs);
                   1286:                                bus_dmamap_unload(sc->sc_dmat, siop_cmd->dmamap_cmd);
                   1287:                                splx(s);
                   1288:                                return;
                   1289:                        }
                   1290:                        bus_dmamap_sync(sc->sc_dmat, siop_cmd->dmamap_data, 0,
                   1291:                            siop_cmd->dmamap_data->dm_mapsize,
                   1292:                            (xs->xs_control & XS_CTL_DATA_IN) ?
                   1293:                            BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
                   1294:                }
                   1295:                bus_dmamap_sync(sc->sc_dmat, siop_cmd->dmamap_cmd, 0,
                   1296:                    siop_cmd->dmamap_cmd->dm_mapsize, BUS_DMASYNC_PREWRITE);
                   1297:
                   1298:                siop_setuptables(siop_cmd);
                   1299:                siop_start(sc, siop_cmd);
                   1300:                if (xs->xs_control & XS_CTL_POLL) {
                   1301:                        /* poll for command completion */
                   1302:                        while ((xs->xs_status & XS_STS_DONE) == 0) {
                   1303:                                delay(1000);
                   1304:                                siop_intr(sc);
                   1305:                        }
                   1306:                }
                   1307:                splx(s);
                   1308:                return;
1.1       bouyer   1309:
1.40.2.2  nathanw  1310:        case ADAPTER_REQ_GROW_RESOURCES:
1.40.2.4  nathanw  1311: #ifdef SIOP_DEBUG
                   1312:                printf("%s grow resources (%d)\n", sc->sc_dev.dv_xname,
                   1313:                    sc->sc_adapt.adapt_openings);
                   1314: #endif
                   1315:                siop_morecbd(sc);
1.40.2.2  nathanw  1316:                return;
1.35      bouyer   1317:
1.40.2.2  nathanw  1318:        case ADAPTER_REQ_SET_XFER_MODE:
                   1319:        {
                   1320:                struct scsipi_xfer_mode *xm = arg;
                   1321:                if (sc->targets[xm->xm_target] == NULL)
                   1322:                        return;
                   1323:                s = splbio();
                   1324:                if (xm->xm_mode & PERIPH_CAP_TQING)
                   1325:                        sc->targets[xm->xm_target]->flags |= TARF_TAG;
                   1326:                if ((xm->xm_mode & PERIPH_CAP_WIDE16) &&
                   1327:                    (sc->features & SF_BUS_WIDE))
                   1328:                        sc->targets[xm->xm_target]->flags |= TARF_WIDE;
                   1329:                if (xm->xm_mode & PERIPH_CAP_SYNC)
                   1330:                        sc->targets[xm->xm_target]->flags |= TARF_SYNC;
                   1331:                if ((xm->xm_mode & (PERIPH_CAP_SYNC | PERIPH_CAP_WIDE16)) ||
                   1332:                    sc->targets[xm->xm_target]->status == TARST_PROBING)
                   1333:                        sc->targets[xm->xm_target]->status =
                   1334:                            TARST_ASYNC;
                   1335:
                   1336:                for (lun = 0; lun < sc->sc_chan.chan_nluns; lun++) {
                   1337:                        if (sc->sc_chan.chan_periphs[xm->xm_target][lun])
                   1338:                                /* allocate a lun sw entry for this device */
                   1339:                                siop_add_dev(sc, xm->xm_target, lun);
1.31      bouyer   1340:                }
1.40.2.2  nathanw  1341:
1.14      bouyer   1342:                splx(s);
                   1343:        }
1.40.2.2  nathanw  1344:        }
1.1       bouyer   1345: }
                   1346:
1.40.2.2  nathanw  1347: static void
                   1348: siop_start(sc, siop_cmd)
1.1       bouyer   1349:        struct siop_softc *sc;
1.40.2.2  nathanw  1350:        struct siop_cmd *siop_cmd;
1.1       bouyer   1351: {
1.31      bouyer   1352:        struct siop_lun *siop_lun;
1.2       bouyer   1353:        u_int32_t dsa;
1.1       bouyer   1354:        int timeout;
1.40.2.2  nathanw  1355:        int target, lun, slot;
1.2       bouyer   1356:
                   1357:        /*
                   1358:         * first make sure to read valid data
                   1359:         */
1.35      bouyer   1360:        siop_script_sync(sc, BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1.1       bouyer   1361:
1.2       bouyer   1362:        /*
1.10      bouyer   1363:         * The queue management here is a bit tricky: the script always looks
                   1364:         * at the slot from first to last, so if we always use the first
                   1365:         * free slot commands can stay at the tail of the queue ~forever.
                   1366:         * The algorithm used here is to restart from the head when we know
                   1367:         * that the queue is empty, and only add commands after the last one.
                   1368:         * When we're at the end of the queue wait for the script to clear it.
                   1369:         * The best thing to do here would be to implement a circular queue,
                   1370:         * but using only 53c720 features this can be "interesting".
                   1371:         * A mid-way solution could be to implement 2 queues and swap orders.
1.2       bouyer   1372:         */
1.29      bouyer   1373:        slot = sc->sc_currschedslot;
1.10      bouyer   1374:        /*
1.35      bouyer   1375:         * If the instruction is 0x80000000 (JUMP foo, IF FALSE) the slot is
                   1376:         * free. As this is the last used slot, all previous slots are free,
1.40.2.2  nathanw  1377:         * we can restart from 0.
1.10      bouyer   1378:         */
1.35      bouyer   1379:        if (siop_script_read(sc, (Ent_script_sched_slot0 / 4) + slot * 2) ==
                   1380:            0x80000000) {
1.40.2.2  nathanw  1381:                slot = sc->sc_currschedslot = 0;
1.10      bouyer   1382:        } else {
                   1383:                slot++;
                   1384:        }
1.40.2.2  nathanw  1385:        target = siop_cmd->xs->xs_periph->periph_target;
                   1386:        lun = siop_cmd->xs->xs_periph->periph_lun;
                   1387:        siop_lun = sc->targets[target]->siop_lun[lun];
                   1388:        /* if non-tagged command active, panic: this shouldn't happen */
                   1389:        if (siop_lun->siop_tag[0].active != NULL) {
                   1390:                panic("siop_start: tagged cmd while untagged running");
                   1391:        }
1.31      bouyer   1392: #ifdef DIAGNOSTIC
1.40.2.2  nathanw  1393:        /* sanity check the tag if needed */
                   1394:        if (siop_cmd->flags & CMDFL_TAG) {
                   1395:                if (siop_lun->siop_tag[siop_cmd->tag].active != NULL)
                   1396:                        panic("siop_start: tag not free");
                   1397:                if (siop_cmd->tag >= SIOP_NTAG) {
                   1398:                        scsipi_printaddr(siop_cmd->xs->xs_periph);
                   1399:                        printf(": tag id %d\n", siop_cmd->tag);
                   1400:                        panic("siop_start: invalid tag id");
                   1401:                }
                   1402:        }
                   1403: #endif
                   1404:        /*
                   1405:         * find a free scheduler slot and load it.
                   1406:         */
                   1407:        for (; slot < SIOP_NSLOTS; slot++) {
1.35      bouyer   1408:                /*
1.40.2.2  nathanw  1409:                 * If cmd if 0x80000000 the slot is free
1.35      bouyer   1410:                 */
1.40.2.2  nathanw  1411:                if (siop_script_read(sc,
                   1412:                    (Ent_script_sched_slot0 / 4) + slot * 2) ==
                   1413:                    0x80000000)
                   1414:                        break;
                   1415:        }
                   1416:        if (slot == SIOP_NSLOTS) {
1.39      bouyer   1417:                /*
1.40.2.2  nathanw  1418:                 * no more free slot, no need to continue. freeze the queue
                   1419:                 * and requeue this command.
1.39      bouyer   1420:                 */
1.40.2.2  nathanw  1421:                scsipi_channel_freeze(&sc->sc_chan, 1);
                   1422:                sc->sc_flags |= SCF_CHAN_NOSLOT;
                   1423:                siop_cmd->xs->error = XS_REQUEUE;
                   1424:                siop_cmd->xs->status = SCSI_SIOP_NOCHECK;
                   1425:                siop_scsicmd_end(siop_cmd);
                   1426:                return;
                   1427:        }
1.35      bouyer   1428: #ifdef SIOP_DEBUG_SCHED
1.40.2.2  nathanw  1429:        printf("using slot %d for DSA 0x%lx\n", slot,
                   1430:            (u_long)siop_cmd->dsa);
1.31      bouyer   1431: #endif
1.40.2.2  nathanw  1432:        /* mark command as active */
                   1433:        if (siop_cmd->status == CMDST_READY)
                   1434:                siop_cmd->status = CMDST_ACTIVE;
                   1435:        else
                   1436:                panic("siop_start: bad status");
                   1437:        siop_lun->siop_tag[siop_cmd->tag].active = siop_cmd;
                   1438:        /* patch scripts with DSA addr */
                   1439:        dsa = siop_cmd->dsa;
                   1440:        /* first reselect switch, if we have an entry */
                   1441:        if (siop_lun->siop_tag[siop_cmd->tag].reseloff > 0)
1.35      bouyer   1442:                siop_script_write(sc,
1.40.2.2  nathanw  1443:                    siop_lun->siop_tag[siop_cmd->tag].reseloff + 1,
                   1444:                    dsa + sizeof(struct siop_xfer_common) +
                   1445:                    Ent_ldsa_reload_dsa);
                   1446:        /* CMD script: MOVE MEMORY addr */
                   1447:        siop_cmd->siop_xfer->resel[E_ldsa_abs_slot_Used[0]] =
                   1448:           htole32(sc->sc_scriptaddr + Ent_script_sched_slot0 + slot * 8);
                   1449:                siop_table_sync(siop_cmd, BUS_DMASYNC_PREWRITE);
                   1450:        /* scheduler slot: JUMP ldsa_select */
                   1451:        siop_script_write(sc,
                   1452:            (Ent_script_sched_slot0 / 4) + slot * 2 + 1,
                   1453:            dsa + sizeof(struct siop_xfer_common) + Ent_ldsa_select);
                   1454:        /* handle timeout */
                   1455:        if ((siop_cmd->xs->xs_control & XS_CTL_POLL) == 0) {
                   1456:                /* start exire timer */
                   1457:                timeout =
                   1458:                    (u_int64_t)siop_cmd->xs->timeout * (u_int64_t)hz / 1000;
                   1459:                if (timeout == 0)
                   1460:                        timeout = 1;
                   1461:                callout_reset( &siop_cmd->xs->xs_callout,
                   1462:                    timeout, siop_timeout, siop_cmd);
1.36      bouyer   1463:        }
1.40.2.2  nathanw  1464:        /*
                   1465:         * Change JUMP cmd so that this slot will be handled
                   1466:         */
                   1467:        siop_script_write(sc, (Ent_script_sched_slot0 / 4) + slot * 2,
                   1468:            0x80080000);
                   1469:        sc->sc_currschedslot = slot;
1.36      bouyer   1470:
1.2       bouyer   1471:        /* make sure SCRIPT processor will read valid data */
1.35      bouyer   1472:        siop_script_sync(sc,BUS_DMASYNC_PREREAD |  BUS_DMASYNC_PREWRITE);
1.2       bouyer   1473:        /* Signal script it has some work to do */
                   1474:        bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_ISTAT, ISTAT_SIGP);
                   1475:        /* and wait for IRQ */
                   1476:        return;
1.1       bouyer   1477: }
                   1478:
                   1479: void
                   1480: siop_timeout(v)
                   1481:        void *v;
                   1482: {
                   1483:        struct siop_cmd *siop_cmd = v;
1.31      bouyer   1484:        struct siop_softc *sc = siop_cmd->siop_sc;
1.1       bouyer   1485:        int s;
                   1486:
1.40.2.2  nathanw  1487:        scsipi_printaddr(siop_cmd->xs->xs_periph);
1.1       bouyer   1488:        printf("command timeout\n");
                   1489:
                   1490:        s = splbio();
                   1491:        /* reset the scsi bus */
1.26      bouyer   1492:        siop_resetbus(sc);
1.1       bouyer   1493:
1.12      soren    1494:        /* deactivate callout */
1.1       bouyer   1495:        callout_stop(&siop_cmd->xs->xs_callout);
1.31      bouyer   1496:        /* mark command as being timed out; siop_intr will handle it */
1.1       bouyer   1497:        /*
                   1498:         * mark command has being timed out and just return;
                   1499:         * the bus reset will generate an interrupt,
                   1500:         * it will be handled in siop_intr()
                   1501:         */
                   1502:        siop_cmd->flags |= CMDFL_TIMEOUT;
                   1503:        splx(s);
                   1504:        return;
                   1505:
                   1506: }
1.2       bouyer   1507:
                   1508: void
                   1509: siop_dump_script(sc)
                   1510:        struct siop_softc *sc;
                   1511: {
                   1512:        int i;
1.37      thorpej  1513:        for (i = 0; i < PAGE_SIZE / 4; i += 2) {
1.4       bouyer   1514:                printf("0x%04x: 0x%08x 0x%08x", i * 4,
                   1515:                    le32toh(sc->sc_script[i]), le32toh(sc->sc_script[i+1]));
                   1516:                if ((le32toh(sc->sc_script[i]) & 0xe0000000) == 0xc0000000) {
1.2       bouyer   1517:                        i++;
1.4       bouyer   1518:                        printf(" 0x%08x", le32toh(sc->sc_script[i+1]));
1.2       bouyer   1519:                }
                   1520:                printf("\n");
                   1521:        }
1.16      bouyer   1522: }
                   1523:
1.40.2.4  nathanw  1524: void
1.16      bouyer   1525: siop_morecbd(sc)
                   1526:        struct siop_softc *sc;
                   1527: {
1.31      bouyer   1528:        int error, i, j;
1.16      bouyer   1529:        bus_dma_segment_t seg;
                   1530:        int rseg;
                   1531:        struct siop_cbd *newcbd;
1.31      bouyer   1532:        bus_addr_t dsa;
                   1533:        u_int32_t *scr;
1.16      bouyer   1534:
                   1535:        /* allocate a new list head */
1.40.2.6! nathanw  1536:        newcbd = malloc(sizeof(struct siop_cbd), M_DEVBUF, M_NOWAIT|M_ZERO);
1.16      bouyer   1537:        if (newcbd == NULL) {
                   1538:                printf("%s: can't allocate memory for command descriptors "
                   1539:                    "head\n", sc->sc_dev.dv_xname);
1.40.2.4  nathanw  1540:                return;
1.16      bouyer   1541:        }
                   1542:
                   1543:        /* allocate cmd list */
1.40.2.6! nathanw  1544:        newcbd->cmds = malloc(sizeof(struct siop_cmd) * SIOP_NCMDPB,
        !          1545:            M_DEVBUF, M_NOWAIT|M_ZERO);
1.16      bouyer   1546:        if (newcbd->cmds == NULL) {
                   1547:                printf("%s: can't allocate memory for command descriptors\n",
                   1548:                    sc->sc_dev.dv_xname);
                   1549:                goto bad3;
                   1550:        }
1.37      thorpej  1551:        error = bus_dmamem_alloc(sc->sc_dmat, PAGE_SIZE, PAGE_SIZE, 0, &seg,
                   1552:            1, &rseg, BUS_DMA_NOWAIT);
1.16      bouyer   1553:        if (error) {
                   1554:                printf("%s: unable to allocate cbd DMA memory, error = %d\n",
                   1555:                    sc->sc_dev.dv_xname, error);
                   1556:                goto bad2;
                   1557:        }
1.37      thorpej  1558:        error = bus_dmamem_map(sc->sc_dmat, &seg, rseg, PAGE_SIZE,
1.16      bouyer   1559:            (caddr_t *)&newcbd->xfers, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
                   1560:        if (error) {
                   1561:                printf("%s: unable to map cbd DMA memory, error = %d\n",
                   1562:                    sc->sc_dev.dv_xname, error);
                   1563:                goto bad2;
                   1564:        }
1.37      thorpej  1565:        error = bus_dmamap_create(sc->sc_dmat, PAGE_SIZE, 1, PAGE_SIZE, 0,
1.16      bouyer   1566:            BUS_DMA_NOWAIT, &newcbd->xferdma);
                   1567:        if (error) {
                   1568:                printf("%s: unable to create cbd DMA map, error = %d\n",
                   1569:                    sc->sc_dev.dv_xname, error);
                   1570:                goto bad1;
                   1571:        }
                   1572:        error = bus_dmamap_load(sc->sc_dmat, newcbd->xferdma, newcbd->xfers,
1.37      thorpej  1573:            PAGE_SIZE, NULL, BUS_DMA_NOWAIT);
1.16      bouyer   1574:        if (error) {
1.17      bouyer   1575:                printf("%s: unable to load cbd DMA map, error = %d\n",
1.16      bouyer   1576:                    sc->sc_dev.dv_xname, error);
                   1577:                goto bad0;
                   1578:        }
1.31      bouyer   1579: #ifdef DEBUG
1.35      bouyer   1580:        printf("%s: alloc newcdb at PHY addr 0x%lx\n", sc->sc_dev.dv_xname,
1.31      bouyer   1581:            (unsigned long)newcbd->xferdma->dm_segs[0].ds_addr);
                   1582: #endif
1.16      bouyer   1583:        for (i = 0; i < SIOP_NCMDPB; i++) {
                   1584:                error = bus_dmamap_create(sc->sc_dmat, MAXPHYS, SIOP_NSG,
                   1585:                    MAXPHYS, 0, BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
                   1586:                    &newcbd->cmds[i].dmamap_data);
                   1587:                if (error) {
                   1588:                        printf("%s: unable to create data DMA map for cbd: "
                   1589:                            "error %d\n",
                   1590:                            sc->sc_dev.dv_xname, error);
                   1591:                        goto bad0;
                   1592:                }
                   1593:                error = bus_dmamap_create(sc->sc_dmat,
                   1594:                    sizeof(struct scsipi_generic), 1,
                   1595:                    sizeof(struct scsipi_generic), 0,
                   1596:                    BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
                   1597:                    &newcbd->cmds[i].dmamap_cmd);
                   1598:                if (error) {
                   1599:                        printf("%s: unable to create cmd DMA map for cbd %d\n",
                   1600:                            sc->sc_dev.dv_xname, error);
                   1601:                        goto bad0;
                   1602:                }
1.31      bouyer   1603:                newcbd->cmds[i].siop_sc = sc;
1.16      bouyer   1604:                newcbd->cmds[i].siop_cbdp = newcbd;
1.31      bouyer   1605:                newcbd->cmds[i].siop_xfer = &newcbd->xfers[i];
                   1606:                memset(newcbd->cmds[i].siop_xfer, 0,
                   1607:                    sizeof(struct siop_xfer));
1.16      bouyer   1608:                newcbd->cmds[i].dsa = newcbd->xferdma->dm_segs[0].ds_addr +
                   1609:                    i * sizeof(struct siop_xfer);
1.31      bouyer   1610:                dsa = newcbd->cmds[i].dsa;
1.16      bouyer   1611:                newcbd->cmds[i].status = CMDST_FREE;
1.31      bouyer   1612:                newcbd->cmds[i].siop_tables.t_msgout.count= htole32(1);
                   1613:                newcbd->cmds[i].siop_tables.t_msgout.addr = htole32(dsa);
                   1614:                newcbd->cmds[i].siop_tables.t_msgin.count= htole32(1);
                   1615:                newcbd->cmds[i].siop_tables.t_msgin.addr = htole32(dsa + 8);
                   1616:                newcbd->cmds[i].siop_tables.t_extmsgin.count= htole32(2);
1.35      bouyer   1617:                newcbd->cmds[i].siop_tables.t_extmsgin.addr = htole32(dsa + 9);
                   1618:                newcbd->cmds[i].siop_tables.t_extmsgdata.addr =
                   1619:                    htole32(dsa + 11);
1.31      bouyer   1620:                newcbd->cmds[i].siop_tables.t_status.count= htole32(1);
1.35      bouyer   1621:                newcbd->cmds[i].siop_tables.t_status.addr = htole32(dsa + 16);
1.31      bouyer   1622:
1.35      bouyer   1623:                /* The select/reselect script */
1.31      bouyer   1624:                scr = &newcbd->cmds[i].siop_xfer->resel[0];
                   1625:                for (j = 0; j < sizeof(load_dsa) / sizeof(load_dsa[0]); j++)
                   1626:                        scr[j] = htole32(load_dsa[j]);
                   1627:                /*
                   1628:                 * 0x78000000 is a 'move data8 to reg'. data8 is the second
                   1629:                 * octet, reg offset is the third.
                   1630:                 */
                   1631:                scr[Ent_rdsa0 / 4] =
                   1632:                    htole32(0x78100000 | ((dsa & 0x000000ff) <<  8));
                   1633:                scr[Ent_rdsa1 / 4] =
                   1634:                    htole32(0x78110000 | ( dsa & 0x0000ff00       ));
                   1635:                scr[Ent_rdsa2 / 4] =
                   1636:                    htole32(0x78120000 | ((dsa & 0x00ff0000) >>  8));
                   1637:                scr[Ent_rdsa3 / 4] =
                   1638:                    htole32(0x78130000 | ((dsa & 0xff000000) >> 16));
1.35      bouyer   1639:                scr[E_ldsa_abs_reselected_Used[0]] =
                   1640:                    htole32(sc->sc_scriptaddr + Ent_reselected);
                   1641:                scr[E_ldsa_abs_reselect_Used[0]] =
                   1642:                    htole32(sc->sc_scriptaddr + Ent_reselect);
                   1643:                scr[E_ldsa_abs_selected_Used[0]] =
                   1644:                    htole32(sc->sc_scriptaddr + Ent_selected);
                   1645:                scr[E_ldsa_abs_data_Used[0]] =
                   1646:                    htole32(dsa + sizeof(struct siop_xfer_common) +
                   1647:                    Ent_ldsa_data);
                   1648:                /* JUMP foo, IF FALSE - used by MOVE MEMORY to clear the slot */
                   1649:                scr[Ent_ldsa_data / 4] = htole32(0x80000000);
1.16      bouyer   1650:                TAILQ_INSERT_TAIL(&sc->free_list, &newcbd->cmds[i], next);
1.35      bouyer   1651: #ifdef SIOP_DEBUG
1.31      bouyer   1652:                printf("tables[%d]: in=0x%x out=0x%x status=0x%x\n", i,
                   1653:                    le32toh(newcbd->cmds[i].siop_tables.t_msgin.addr),
                   1654:                    le32toh(newcbd->cmds[i].siop_tables.t_msgout.addr),
                   1655:                    le32toh(newcbd->cmds[i].siop_tables.t_status.addr));
1.16      bouyer   1656: #endif
                   1657:        }
                   1658:        TAILQ_INSERT_TAIL(&sc->cmds, newcbd, next);
1.40.2.4  nathanw  1659:        sc->sc_adapt.adapt_openings += SIOP_NCMDPB;
                   1660:        return;
1.16      bouyer   1661: bad0:
1.40.2.4  nathanw  1662:        bus_dmamap_unload(sc->sc_dmat, newcbd->xferdma);
1.16      bouyer   1663:        bus_dmamap_destroy(sc->sc_dmat, newcbd->xferdma);
                   1664: bad1:
                   1665:        bus_dmamem_free(sc->sc_dmat, &seg, rseg);
                   1666: bad2:
                   1667:        free(newcbd->cmds, M_DEVBUF);
                   1668: bad3:
                   1669:        free(newcbd, M_DEVBUF);
1.40.2.4  nathanw  1670:        return;
1.2       bouyer   1671: }
                   1672:
1.31      bouyer   1673: struct siop_lunsw *
                   1674: siop_get_lunsw(sc)
                   1675:        struct siop_softc *sc;
                   1676: {
                   1677:        struct siop_lunsw *lunsw;
                   1678:        int i;
                   1679:
1.35      bouyer   1680:        if (sc->script_free_lo + (sizeof(lun_switch) / sizeof(lun_switch[0])) >=
                   1681:            sc->script_free_hi)
                   1682:                return NULL;
1.31      bouyer   1683:        lunsw = TAILQ_FIRST(&sc->lunsw_list);
                   1684:        if (lunsw != NULL) {
1.35      bouyer   1685: #ifdef SIOP_DEBUG
1.31      bouyer   1686:                printf("siop_get_lunsw got lunsw at offset %d\n",
                   1687:                    lunsw->lunsw_off);
                   1688: #endif
                   1689:                TAILQ_REMOVE(&sc->lunsw_list, lunsw, next);
                   1690:                return lunsw;
                   1691:        }
1.40.2.6! nathanw  1692:        lunsw = malloc(sizeof(struct siop_lunsw), M_DEVBUF, M_NOWAIT|M_ZERO);
1.31      bouyer   1693:        if (lunsw == NULL)
                   1694:                return NULL;
1.35      bouyer   1695: #ifdef SIOP_DEBUG
                   1696:        printf("allocating lunsw at offset %d\n", sc->script_free_lo);
1.31      bouyer   1697: #endif
                   1698:        if (sc->features & SF_CHIP_RAM) {
                   1699:                bus_space_write_region_4(sc->sc_ramt, sc->sc_ramh,
1.35      bouyer   1700:                    sc->script_free_lo * 4, lun_switch,
1.31      bouyer   1701:                    sizeof(lun_switch) / sizeof(lun_switch[0]));
                   1702:                bus_space_write_4(sc->sc_ramt, sc->sc_ramh,
1.35      bouyer   1703:                    (sc->script_free_lo + E_abs_lunsw_return_Used[0]) * 4,
1.31      bouyer   1704:                    sc->sc_scriptaddr + Ent_lunsw_return);
                   1705:        } else {
                   1706:                for (i = 0; i < sizeof(lun_switch) / sizeof(lun_switch[0]);
                   1707:                    i++)
1.35      bouyer   1708:                        sc->sc_script[sc->script_free_lo + i] =
1.31      bouyer   1709:                            htole32(lun_switch[i]);
1.35      bouyer   1710:                sc->sc_script[sc->script_free_lo + E_abs_lunsw_return_Used[0]] =
1.31      bouyer   1711:                    htole32(sc->sc_scriptaddr + Ent_lunsw_return);
                   1712:        }
1.35      bouyer   1713:        lunsw->lunsw_off = sc->script_free_lo;
                   1714:        lunsw->lunsw_size = sizeof(lun_switch) / sizeof(lun_switch[0]);
                   1715:        sc->script_free_lo += lunsw->lunsw_size;
                   1716:        siop_script_sync(sc, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1.31      bouyer   1717:        return lunsw;
                   1718: }
                   1719:
                   1720: void
                   1721: siop_add_reselsw(sc, target)
                   1722:        struct siop_softc *sc;
                   1723:        int target;
                   1724: {
                   1725:        int i;
                   1726:        struct siop_lun *siop_lun;
1.32      bouyer   1727:        /*
                   1728:         * add an entry to resel switch
                   1729:         */
                   1730:        siop_script_sync(sc, BUS_DMASYNC_POSTWRITE);
1.31      bouyer   1731:        for (i = 0; i < 15; i++) {
                   1732:                sc->targets[target]->reseloff = Ent_resel_targ0 / 4 + i * 2;
                   1733:                if ((siop_script_read(sc, sc->targets[target]->reseloff) & 0xff)
                   1734:                    == 0xff) { /* it's free */
1.35      bouyer   1735: #ifdef SIOP_DEBUG
1.31      bouyer   1736:                        printf("siop: target %d slot %d offset %d\n",
                   1737:                            target, i, sc->targets[target]->reseloff);
                   1738: #endif
                   1739:                        /* JUMP abs_foo, IF target | 0x80; */
                   1740:                        siop_script_write(sc, sc->targets[target]->reseloff,
                   1741:                            0x800c0080 | target);
                   1742:                        siop_script_write(sc, sc->targets[target]->reseloff + 1,
                   1743:                            sc->sc_scriptaddr +
1.33      bouyer   1744:                            sc->targets[target]->lunsw->lunsw_off * 4 +
                   1745:                            Ent_lun_switch_entry);
1.31      bouyer   1746:                        break;
                   1747:                }
                   1748:        }
                   1749:        if (i == 15) /* no free slot, shouldn't happen */
                   1750:                panic("siop: resel switch full");
                   1751:
1.35      bouyer   1752:        sc->sc_ntargets++;
1.31      bouyer   1753:        for (i = 0; i < 8; i++) {
1.35      bouyer   1754:                siop_lun = sc->targets[target]->siop_lun[i];
                   1755:                if (siop_lun == NULL)
                   1756:                        continue;
                   1757:                if (siop_lun->reseloff > 0) {
                   1758:                        siop_lun->reseloff = 0;
                   1759:                        siop_add_dev(sc, target, i);
                   1760:                }
1.31      bouyer   1761:        }
                   1762:        siop_update_scntl3(sc, sc->targets[target]);
1.32      bouyer   1763:        siop_script_sync(sc, BUS_DMASYNC_PREWRITE);
1.31      bouyer   1764: }
                   1765:
                   1766: void
                   1767: siop_update_scntl3(sc, siop_target)
                   1768:        struct siop_softc *sc;
                   1769:        struct siop_target *siop_target;
                   1770: {
                   1771:        /* MOVE target->id >> 24 TO SCNTL3 */
                   1772:        siop_script_write(sc,
                   1773:            siop_target->lunsw->lunsw_off + (Ent_restore_scntl3 / 4),
                   1774:            0x78030000 | ((siop_target->id >> 16) & 0x0000ff00));
                   1775:        /* MOVE target->id >> 8 TO SXFER */
                   1776:        siop_script_write(sc,
                   1777:            siop_target->lunsw->lunsw_off + (Ent_restore_scntl3 / 4) + 2,
                   1778:            0x78050000 | (siop_target->id & 0x0000ff00));
1.32      bouyer   1779:        siop_script_sync(sc, BUS_DMASYNC_PREWRITE);
1.31      bouyer   1780: }
                   1781:
1.35      bouyer   1782: void
                   1783: siop_add_dev(sc, target, lun)
                   1784:        struct siop_softc *sc;
                   1785:        int target;
                   1786:        int lun;
                   1787: {
                   1788:        struct siop_lunsw *lunsw;
                   1789:        struct siop_lun *siop_lun = sc->targets[target]->siop_lun[lun];
                   1790:        int i, ntargets;
                   1791:
                   1792:        if (siop_lun->reseloff > 0)
                   1793:                return;
                   1794:        lunsw = sc->targets[target]->lunsw;
                   1795:        if ((lunsw->lunsw_off + lunsw->lunsw_size) < sc->script_free_lo) {
                   1796:                /*
                   1797:                 * can't extend this slot. Probably not worth trying to deal
                   1798:                 * with this case
                   1799:                 */
                   1800: #ifdef DEBUG
                   1801:                printf("%s:%d:%d: can't allocate a lun sw slot\n",
                   1802:                    sc->sc_dev.dv_xname, target, lun);
                   1803: #endif
                   1804:                return;
                   1805:        }
                   1806:        /* count how many free targets we still have to probe */
1.40.2.2  nathanw  1807:        ntargets =  sc->sc_chan.chan_ntargets - 1 - sc->sc_ntargets;
1.35      bouyer   1808:
                   1809:        /*
                   1810:         * we need 8 bytes for the lun sw additionnal entry, and
                   1811:         * eventually sizeof(tag_switch) for the tag switch entry.
                   1812:         * Keep enouth free space for the free targets that could be
                   1813:         * probed later.
                   1814:         */
                   1815:        if (sc->script_free_lo + 2 +
                   1816:            (ntargets * sizeof(lun_switch) / sizeof(lun_switch[0])) >=
                   1817:            ((sc->targets[target]->flags & TARF_TAG) ?
                   1818:            sc->script_free_hi - (sizeof(tag_switch) / sizeof(tag_switch[0])) :
                   1819:            sc->script_free_hi)) {
                   1820:                /*
                   1821:                 * not enouth space, probably not worth dealing with it.
                   1822:                 * We can hold 13 tagged-queuing capable devices in the 4k RAM.
                   1823:                 */
                   1824: #ifdef DEBUG
                   1825:                printf("%s:%d:%d: not enouth memory for a lun sw slot\n",
                   1826:                    sc->sc_dev.dv_xname, target, lun);
                   1827: #endif
                   1828:                return;
                   1829:        }
                   1830: #ifdef SIOP_DEBUG
                   1831:        printf("%s:%d:%d: allocate lun sw entry\n",
                   1832:            sc->sc_dev.dv_xname, target, lun);
                   1833: #endif
                   1834:        /* INT int_resellun */
                   1835:        siop_script_write(sc, sc->script_free_lo, 0x98080000);
                   1836:        siop_script_write(sc, sc->script_free_lo + 1, A_int_resellun);
                   1837:        /* Now the slot entry: JUMP abs_foo, IF lun */
                   1838:        siop_script_write(sc, sc->script_free_lo - 2,
                   1839:            0x800c0000 | lun);
                   1840:        siop_script_write(sc, sc->script_free_lo - 1, 0);
                   1841:        siop_lun->reseloff = sc->script_free_lo - 2;
                   1842:        lunsw->lunsw_size += 2;
                   1843:        sc->script_free_lo += 2;
                   1844:        if (sc->targets[target]->flags & TARF_TAG) {
                   1845:                /* we need a tag switch */
                   1846:                sc->script_free_hi -=
                   1847:                    sizeof(tag_switch) / sizeof(tag_switch[0]);
                   1848:                if (sc->features & SF_CHIP_RAM) {
                   1849:                        bus_space_write_region_4(sc->sc_ramt, sc->sc_ramh,
                   1850:                            sc->script_free_hi * 4, tag_switch,
                   1851:                            sizeof(tag_switch) / sizeof(tag_switch[0]));
                   1852:                } else {
                   1853:                        for(i = 0;
                   1854:                            i < sizeof(tag_switch) / sizeof(tag_switch[0]);
                   1855:                            i++) {
                   1856:                                sc->sc_script[sc->script_free_hi + i] =
                   1857:                                    htole32(tag_switch[i]);
                   1858:                        }
                   1859:                }
                   1860:                siop_script_write(sc,
                   1861:                    siop_lun->reseloff + 1,
                   1862:                    sc->sc_scriptaddr + sc->script_free_hi * 4 +
                   1863:                    Ent_tag_switch_entry);
                   1864:
                   1865:                for (i = 0; i < SIOP_NTAG; i++) {
                   1866:                        siop_lun->siop_tag[i].reseloff =
                   1867:                            sc->script_free_hi + (Ent_resel_tag0 / 4) + i * 2;
                   1868:                }
                   1869:        } else {
                   1870:                /* non-tag case; just work with the lun switch */
                   1871:                siop_lun->siop_tag[0].reseloff =
                   1872:                    sc->targets[target]->siop_lun[lun]->reseloff;
                   1873:        }
                   1874:        siop_script_sync(sc, BUS_DMASYNC_PREWRITE);
                   1875: }
                   1876:
                   1877: void
                   1878: siop_del_dev(sc, target, lun)
                   1879:        struct siop_softc *sc;
                   1880:        int target;
                   1881:        int lun;
                   1882: {
                   1883:        int i;
                   1884: #ifdef SIOP_DEBUG
                   1885:                printf("%s:%d:%d: free lun sw entry\n",
                   1886:                    sc->sc_dev.dv_xname, target, lun);
                   1887: #endif
                   1888:        if (sc->targets[target] == NULL)
                   1889:                return;
                   1890:        free(sc->targets[target]->siop_lun[lun], M_DEVBUF);
                   1891:        sc->targets[target]->siop_lun[lun] = NULL;
                   1892:        /* XXX compact sw entry too ? */
                   1893:        /* check if we can free the whole target */
                   1894:        for (i = 0; i < 8; i++) {
                   1895:                if (sc->targets[target]->siop_lun[i] != NULL)
                   1896:                        return;
                   1897:        }
                   1898: #ifdef SIOP_DEBUG
                   1899:        printf("%s: free siop_target for target %d lun %d lunsw offset %d\n",
                   1900:            sc->sc_dev.dv_xname, target, lun,
                   1901:            sc->targets[target]->lunsw->lunsw_off);
                   1902: #endif
                   1903:        /*
                   1904:         * nothing here, free the target struct and resel
                   1905:         * switch entry
                   1906:         */
                   1907:        siop_script_write(sc, sc->targets[target]->reseloff, 0x800c00ff);
                   1908:        siop_script_sync(sc, BUS_DMASYNC_PREWRITE);
                   1909:        TAILQ_INSERT_TAIL(&sc->lunsw_list, sc->targets[target]->lunsw, next);
                   1910:        free(sc->targets[target], M_DEVBUF);
                   1911:        sc->targets[target] = NULL;
                   1912:        sc->sc_ntargets--;
1.40.2.2  nathanw  1913: }
                   1914:
                   1915: void
                   1916: siop_update_xfer_mode(sc, target)
                   1917:        struct siop_softc *sc;
                   1918:        int target;
                   1919: {
                   1920:        struct siop_target *siop_target = sc->targets[target];
                   1921:        struct scsipi_xfer_mode xm;
                   1922:
                   1923:        xm.xm_target = target;
                   1924:        xm.xm_mode = 0;
                   1925:        xm.xm_period = 0;
                   1926:        xm.xm_offset = 0;
                   1927:
                   1928:        if (siop_target->flags & TARF_ISWIDE)
                   1929:                xm.xm_mode |= PERIPH_CAP_WIDE16;
                   1930:        if (siop_target->period) {
                   1931:                xm.xm_period = siop_target->period;
                   1932:                xm.xm_offset = siop_target->offset;
                   1933:                xm.xm_mode |= PERIPH_CAP_SYNC;
                   1934:        }
                   1935:        if (siop_target->flags & TARF_TAG)
                   1936:                xm.xm_mode |= PERIPH_CAP_TQING;
                   1937:        scsipi_async_event(&sc->sc_chan, ASYNC_EVENT_XFER_MODE, &xm);
1.35      bouyer   1938: }
                   1939:
1.2       bouyer   1940: #ifdef SIOP_STATS
                   1941: void
                   1942: siop_printstats()
                   1943: {
                   1944:        printf("siop_stat_intr %d\n", siop_stat_intr);
                   1945:        printf("siop_stat_intr_shortxfer %d\n", siop_stat_intr_shortxfer);
                   1946:        printf("siop_stat_intr_xferdisc %d\n", siop_stat_intr_xferdisc);
                   1947:        printf("siop_stat_intr_sdp %d\n", siop_stat_intr_sdp);
                   1948:        printf("siop_stat_intr_done %d\n", siop_stat_intr_done);
1.35      bouyer   1949:        printf("siop_stat_intr_lunresel %d\n", siop_stat_intr_lunresel);
1.36      bouyer   1950:        printf("siop_stat_intr_qfull %d\n", siop_stat_intr_qfull);
1.2       bouyer   1951: }
                   1952: #endif

CVSweb <webmaster@jp.NetBSD.org>