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