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