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