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