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