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