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