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