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