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