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