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