Home | History | Annotate | Line # | Download | only in ic
esiop.c revision 1.46
      1  1.46   tsutsui /*	$NetBSD: esiop.c,v 1.46 2009/05/15 17:55:44 tsutsui Exp $	*/
      2   1.1    bouyer 
      3   1.1    bouyer /*
      4   1.1    bouyer  * Copyright (c) 2002 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.7    bouyer  *	This product includes software developed by Manuel Bouyer.
     17   1.1    bouyer  * 4. The name of the author may not be used to endorse or promote products
     18   1.1    bouyer  *    derived from this software without specific prior written permission.
     19   1.1    bouyer  *
     20   1.1    bouyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21   1.1    bouyer  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22   1.1    bouyer  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  1.11    bouyer  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24   1.1    bouyer  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25   1.1    bouyer  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26   1.1    bouyer  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27   1.1    bouyer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28   1.1    bouyer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29   1.1    bouyer  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30   1.1    bouyer  *
     31   1.1    bouyer  */
     32   1.1    bouyer 
     33   1.1    bouyer /* SYM53c7/8xx PCI-SCSI I/O Processors driver */
     34   1.1    bouyer 
     35   1.1    bouyer #include <sys/cdefs.h>
     36  1.46   tsutsui __KERNEL_RCSID(0, "$NetBSD: esiop.c,v 1.46 2009/05/15 17:55:44 tsutsui 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.1    bouyer #include <uvm/uvm_extern.h>
     46   1.1    bouyer 
     47   1.1    bouyer #include <machine/endian.h>
     48  1.41        ad #include <sys/bus.h>
     49   1.1    bouyer 
     50   1.1    bouyer #include <dev/microcode/siop/esiop.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.1    bouyer #include <dev/ic/siopvar_common.h>
     60   1.1    bouyer #include <dev/ic/esiopvar.h>
     61   1.1    bouyer 
     62   1.1    bouyer #include "opt_siop.h"
     63   1.1    bouyer 
     64   1.1    bouyer #ifndef DEBUG
     65   1.6    bouyer #undef DEBUG
     66   1.1    bouyer #endif
     67  1.38   garbled /*
     68  1.38   garbled #define SIOP_DEBUG
     69  1.38   garbled #define SIOP_DEBUG_DR
     70  1.38   garbled #define SIOP_DEBUG_INTR
     71  1.38   garbled #define SIOP_DEBUG_SCHED
     72  1.38   garbled #define DUMP_SCRIPT
     73  1.38   garbled */
     74   1.1    bouyer 
     75   1.1    bouyer #define SIOP_STATS
     76   1.1    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.1    bouyer /* number of cmd descriptors per block */
     82   1.1    bouyer #define SIOP_NCMDPB (PAGE_SIZE / sizeof(struct esiop_xfer))
     83   1.1    bouyer 
     84  1.32     perry void	esiop_reset(struct esiop_softc *);
     85  1.32     perry void	esiop_checkdone(struct esiop_softc *);
     86  1.32     perry void	esiop_handle_reset(struct esiop_softc *);
     87  1.32     perry void	esiop_scsicmd_end(struct esiop_cmd *, int);
     88  1.32     perry void	esiop_unqueue(struct esiop_softc *, int, int);
     89  1.32     perry int	esiop_handle_qtag_reject(struct esiop_cmd *);
     90  1.32     perry static void	esiop_start(struct esiop_softc *, struct esiop_cmd *);
     91  1.32     perry void 	esiop_timeout(void *);
     92  1.32     perry void	esiop_scsipi_request(struct scsipi_channel *,
     93  1.32     perry 			scsipi_adapter_req_t, void *);
     94  1.32     perry void	esiop_dump_script(struct esiop_softc *);
     95  1.32     perry void	esiop_morecbd(struct esiop_softc *);
     96  1.32     perry void	esiop_moretagtbl(struct esiop_softc *);
     97  1.32     perry void	siop_add_reselsw(struct esiop_softc *, int);
     98  1.32     perry void	esiop_target_register(struct esiop_softc *, u_int32_t);
     99   1.1    bouyer 
    100  1.32     perry void    esiop_update_scntl3(struct esiop_softc *, struct siop_common_target *);
    101   1.1    bouyer 
    102   1.1    bouyer #ifdef SIOP_STATS
    103   1.1    bouyer static int esiop_stat_intr = 0;
    104   1.1    bouyer static int esiop_stat_intr_shortxfer = 0;
    105   1.1    bouyer static int esiop_stat_intr_sdp = 0;
    106   1.1    bouyer static int esiop_stat_intr_done = 0;
    107   1.1    bouyer static int esiop_stat_intr_xferdisc = 0;
    108   1.1    bouyer static int esiop_stat_intr_lunresel = 0;
    109   1.1    bouyer static int esiop_stat_intr_qfull = 0;
    110  1.32     perry void esiop_printstats(void);
    111   1.1    bouyer #define INCSTAT(x) x++
    112   1.1    bouyer #else
    113  1.11    bouyer #define INCSTAT(x)
    114   1.1    bouyer #endif
    115   1.1    bouyer 
    116  1.35     perry static inline void esiop_script_sync(struct esiop_softc *, int);
    117  1.35     perry static inline void
    118  1.43       dsl esiop_script_sync(struct esiop_softc *sc, int ops)
    119   1.1    bouyer {
    120   1.1    bouyer 	if ((sc->sc_c.features & SF_CHIP_RAM) == 0)
    121   1.1    bouyer 		bus_dmamap_sync(sc->sc_c.sc_dmat, sc->sc_c.sc_scriptdma, 0,
    122   1.1    bouyer 		    PAGE_SIZE, ops);
    123   1.1    bouyer }
    124   1.1    bouyer 
    125  1.35     perry static inline u_int32_t esiop_script_read(struct esiop_softc *, u_int);
    126  1.35     perry static inline u_int32_t
    127  1.43       dsl esiop_script_read(struct esiop_softc *sc, u_int offset)
    128   1.1    bouyer {
    129   1.1    bouyer 	if (sc->sc_c.features & SF_CHIP_RAM) {
    130   1.1    bouyer 		return bus_space_read_4(sc->sc_c.sc_ramt, sc->sc_c.sc_ramh,
    131   1.1    bouyer 		    offset * 4);
    132   1.1    bouyer 	} else {
    133   1.1    bouyer 		return le32toh(sc->sc_c.sc_script[offset]);
    134   1.1    bouyer 	}
    135   1.1    bouyer }
    136   1.1    bouyer 
    137  1.35     perry static inline void esiop_script_write(struct esiop_softc *, u_int,
    138  1.32     perry 	u_int32_t);
    139  1.35     perry static inline void
    140  1.43       dsl esiop_script_write(struct esiop_softc *sc, u_int offset, u_int32_t val)
    141   1.1    bouyer {
    142   1.1    bouyer 	if (sc->sc_c.features & SF_CHIP_RAM) {
    143   1.1    bouyer 		bus_space_write_4(sc->sc_c.sc_ramt, sc->sc_c.sc_ramh,
    144   1.1    bouyer 		    offset * 4, val);
    145   1.1    bouyer 	} else {
    146   1.1    bouyer 		sc->sc_c.sc_script[offset] = htole32(val);
    147   1.1    bouyer 	}
    148   1.1    bouyer }
    149   1.1    bouyer 
    150   1.1    bouyer void
    151  1.43       dsl esiop_attach(struct esiop_softc *sc)
    152   1.1    bouyer {
    153  1.11    bouyer 	struct esiop_dsatbl *tagtbl_donering;
    154  1.11    bouyer 
    155   1.6    bouyer 	if (siop_common_attach(&sc->sc_c) != 0 )
    156   1.6    bouyer 		return;
    157   1.1    bouyer 
    158   1.1    bouyer 	TAILQ_INIT(&sc->free_list);
    159   1.1    bouyer 	TAILQ_INIT(&sc->cmds);
    160   1.2    bouyer 	TAILQ_INIT(&sc->free_tagtbl);
    161   1.2    bouyer 	TAILQ_INIT(&sc->tag_tblblk);
    162   1.1    bouyer 	sc->sc_currschedslot = 0;
    163   1.1    bouyer #ifdef SIOP_DEBUG
    164  1.46   tsutsui 	aprint_debug_dev(sc->sc_c.sc_dev, "script size = %d, PHY addr=0x%x, VIRT=%p\n",
    165  1.42    cegger 	    (int)sizeof(esiop_script),
    166   1.1    bouyer 	    (u_int32_t)sc->sc_c.sc_scriptaddr, sc->sc_c.sc_script);
    167   1.1    bouyer #endif
    168   1.1    bouyer 
    169   1.6    bouyer 	sc->sc_c.sc_adapt.adapt_max_periph = ESIOP_NTAG;
    170   1.1    bouyer 	sc->sc_c.sc_adapt.adapt_request = esiop_scsipi_request;
    171   1.1    bouyer 
    172  1.11    bouyer 	/*
    173  1.11    bouyer 	 * get space for the CMD done slot. For this we use a tag table entry.
    174  1.11    bouyer 	 * It's the same size and allows us to not waste 3/4 of a page
    175  1.11    bouyer 	 */
    176  1.11    bouyer #ifdef DIAGNOSTIC
    177  1.11    bouyer 	if (ESIOP_NTAG != A_ndone_slots) {
    178  1.46   tsutsui 		aprint_error_dev(sc->sc_c.sc_dev, "size of tag DSA table different from the done"
    179  1.42    cegger 		    " ring\n");
    180  1.11    bouyer 		return;
    181  1.11    bouyer 	}
    182  1.11    bouyer #endif
    183  1.11    bouyer 	esiop_moretagtbl(sc);
    184  1.11    bouyer 	tagtbl_donering = TAILQ_FIRST(&sc->free_tagtbl);
    185  1.11    bouyer 	if (tagtbl_donering == NULL) {
    186  1.46   tsutsui 		aprint_error_dev(sc->sc_c.sc_dev, "no memory for command done ring\n");
    187  1.11    bouyer 		return;
    188  1.11    bouyer 	}
    189  1.11    bouyer 	TAILQ_REMOVE(&sc->free_tagtbl, tagtbl_donering, next);
    190  1.11    bouyer 	sc->sc_done_map = tagtbl_donering->tblblk->blkmap;
    191  1.11    bouyer 	sc->sc_done_offset = tagtbl_donering->tbl_offset;
    192  1.11    bouyer 	sc->sc_done_slot = &tagtbl_donering->tbl[0];
    193  1.11    bouyer 
    194   1.1    bouyer 	/* Do a bus reset, so that devices fall back to narrow/async */
    195   1.1    bouyer 	siop_resetbus(&sc->sc_c);
    196   1.1    bouyer 	/*
    197   1.1    bouyer 	 * siop_reset() will reset the chip, thus clearing pending interrupts
    198   1.1    bouyer 	 */
    199   1.1    bouyer 	esiop_reset(sc);
    200   1.1    bouyer #ifdef DUMP_SCRIPT
    201   1.1    bouyer 	esiop_dump_script(sc);
    202   1.1    bouyer #endif
    203   1.1    bouyer 
    204  1.46   tsutsui 	config_found(sc->sc_c.sc_dev, &sc->sc_c.sc_chan, scsiprint);
    205   1.1    bouyer }
    206   1.1    bouyer 
    207   1.1    bouyer void
    208  1.43       dsl esiop_reset(struct esiop_softc *sc)
    209   1.1    bouyer {
    210   1.1    bouyer 	int i, j;
    211   1.1    bouyer 	u_int32_t addr;
    212  1.12    bouyer 	u_int32_t msgin_addr, sem_addr;
    213   1.1    bouyer 
    214   1.1    bouyer 	siop_common_reset(&sc->sc_c);
    215   1.1    bouyer 
    216   1.1    bouyer 	/*
    217  1.12    bouyer 	 * we copy the script at the beggining of RAM. Then there is 4 bytes
    218  1.12    bouyer 	 * for messages in, and 4 bytes for semaphore
    219   1.1    bouyer 	 */
    220   1.1    bouyer 	sc->sc_free_offset = sizeof(esiop_script) / sizeof(esiop_script[0]);
    221   1.1    bouyer 	msgin_addr =
    222   1.1    bouyer 	    sc->sc_free_offset * sizeof(u_int32_t) + sc->sc_c.sc_scriptaddr;
    223  1.12    bouyer 	sc->sc_free_offset += 1;
    224  1.12    bouyer 	sc->sc_semoffset = sc->sc_free_offset;
    225  1.12    bouyer 	sem_addr =
    226  1.12    bouyer 	    sc->sc_semoffset * sizeof(u_int32_t) + sc->sc_c.sc_scriptaddr;
    227  1.12    bouyer 	sc->sc_free_offset += 1;
    228   1.1    bouyer 	/* then we have the scheduler ring */
    229   1.1    bouyer 	sc->sc_shedoffset = sc->sc_free_offset;
    230   1.8    bouyer 	sc->sc_free_offset += A_ncmd_slots * CMD_SLOTSIZE;
    231   1.1    bouyer 	/* then the targets DSA table */
    232   1.1    bouyer 	sc->sc_target_table_offset = sc->sc_free_offset;
    233   1.1    bouyer 	sc->sc_free_offset += sc->sc_c.sc_chan.chan_ntargets;
    234   1.1    bouyer 	/* copy and patch the script */
    235   1.1    bouyer 	if (sc->sc_c.features & SF_CHIP_RAM) {
    236   1.1    bouyer 		bus_space_write_region_4(sc->sc_c.sc_ramt, sc->sc_c.sc_ramh, 0,
    237   1.1    bouyer 		    esiop_script,
    238   1.1    bouyer 		    sizeof(esiop_script) / sizeof(esiop_script[0]));
    239  1.11    bouyer 		for (j = 0; j <
    240   1.1    bouyer 		    (sizeof(E_tlq_offset_Used) / sizeof(E_tlq_offset_Used[0]));
    241   1.1    bouyer 		    j++) {
    242   1.1    bouyer 			bus_space_write_4(sc->sc_c.sc_ramt, sc->sc_c.sc_ramh,
    243   1.1    bouyer 			    E_tlq_offset_Used[j] * 4,
    244   1.1    bouyer 			    sizeof(struct siop_common_xfer));
    245   1.1    bouyer 		}
    246  1.11    bouyer 		for (j = 0; j <
    247  1.34    bouyer 		    (sizeof(E_saved_offset_offset_Used) /
    248  1.34    bouyer 		     sizeof(E_saved_offset_offset_Used[0]));
    249  1.34    bouyer 		    j++) {
    250  1.34    bouyer 			bus_space_write_4(sc->sc_c.sc_ramt, sc->sc_c.sc_ramh,
    251  1.34    bouyer 			    E_saved_offset_offset_Used[j] * 4,
    252  1.34    bouyer 			    sizeof(struct siop_common_xfer) + 4);
    253  1.34    bouyer 		}
    254  1.34    bouyer 		for (j = 0; j <
    255   1.1    bouyer 		    (sizeof(E_abs_msgin2_Used) / sizeof(E_abs_msgin2_Used[0]));
    256   1.1    bouyer 		    j++) {
    257   1.1    bouyer 			bus_space_write_4(sc->sc_c.sc_ramt, sc->sc_c.sc_ramh,
    258   1.1    bouyer 			    E_abs_msgin2_Used[j] * 4, msgin_addr);
    259   1.1    bouyer 		}
    260  1.12    bouyer 		for (j = 0; j <
    261  1.12    bouyer 		    (sizeof(E_abs_sem_Used) / sizeof(E_abs_sem_Used[0]));
    262  1.12    bouyer 		    j++) {
    263  1.12    bouyer 			bus_space_write_4(sc->sc_c.sc_ramt, sc->sc_c.sc_ramh,
    264  1.12    bouyer 			    E_abs_sem_Used[j] * 4, sem_addr);
    265  1.12    bouyer 		}
    266   1.1    bouyer 
    267   1.4    bouyer 		if (sc->sc_c.features & SF_CHIP_LED0) {
    268   1.4    bouyer 			bus_space_write_region_4(sc->sc_c.sc_ramt,
    269   1.4    bouyer 			    sc->sc_c.sc_ramh,
    270   1.4    bouyer 			    Ent_led_on1, esiop_led_on,
    271   1.4    bouyer 			    sizeof(esiop_led_on) / sizeof(esiop_led_on[0]));
    272   1.4    bouyer 			bus_space_write_region_4(sc->sc_c.sc_ramt,
    273   1.4    bouyer 			    sc->sc_c.sc_ramh,
    274   1.4    bouyer 			    Ent_led_on2, esiop_led_on,
    275   1.4    bouyer 			    sizeof(esiop_led_on) / sizeof(esiop_led_on[0]));
    276   1.4    bouyer 			bus_space_write_region_4(sc->sc_c.sc_ramt,
    277   1.4    bouyer 			    sc->sc_c.sc_ramh,
    278   1.4    bouyer 			    Ent_led_off, esiop_led_off,
    279   1.4    bouyer 			    sizeof(esiop_led_off) / sizeof(esiop_led_off[0]));
    280   1.4    bouyer 		}
    281   1.1    bouyer 	} else {
    282   1.1    bouyer 		for (j = 0;
    283   1.1    bouyer 		    j < (sizeof(esiop_script) / sizeof(esiop_script[0])); j++) {
    284   1.1    bouyer 			sc->sc_c.sc_script[j] = htole32(esiop_script[j]);
    285   1.1    bouyer 		}
    286   1.1    bouyer 		for (j = 0; j <
    287   1.1    bouyer 		    (sizeof(E_tlq_offset_Used) / sizeof(E_tlq_offset_Used[0]));
    288   1.1    bouyer 		    j++) {
    289   1.1    bouyer 			sc->sc_c.sc_script[E_tlq_offset_Used[j]] =
    290   1.1    bouyer 			    htole32(sizeof(struct siop_common_xfer));
    291   1.1    bouyer 		}
    292   1.1    bouyer 		for (j = 0; j <
    293  1.34    bouyer 		    (sizeof(E_saved_offset_offset_Used) /
    294  1.34    bouyer 		     sizeof(E_saved_offset_offset_Used[0]));
    295  1.34    bouyer 		    j++) {
    296  1.34    bouyer 			sc->sc_c.sc_script[E_saved_offset_offset_Used[j]] =
    297  1.34    bouyer 			    htole32(sizeof(struct siop_common_xfer) + 4);
    298  1.34    bouyer 		}
    299  1.34    bouyer 		for (j = 0; j <
    300   1.1    bouyer 		    (sizeof(E_abs_msgin2_Used) / sizeof(E_abs_msgin2_Used[0]));
    301   1.1    bouyer 		    j++) {
    302   1.1    bouyer 			sc->sc_c.sc_script[E_abs_msgin2_Used[j]] =
    303   1.1    bouyer 			    htole32(msgin_addr);
    304   1.1    bouyer 		}
    305  1.12    bouyer 		for (j = 0; j <
    306  1.12    bouyer 		    (sizeof(E_abs_sem_Used) / sizeof(E_abs_sem_Used[0]));
    307  1.12    bouyer 		    j++) {
    308  1.12    bouyer 			sc->sc_c.sc_script[E_abs_sem_Used[j]] =
    309  1.12    bouyer 			    htole32(sem_addr);
    310  1.12    bouyer 		}
    311   1.1    bouyer 
    312   1.4    bouyer 		if (sc->sc_c.features & SF_CHIP_LED0) {
    313   1.4    bouyer 			for (j = 0; j < (sizeof(esiop_led_on) /
    314   1.4    bouyer 			    sizeof(esiop_led_on[0])); j++)
    315   1.4    bouyer 				sc->sc_c.sc_script[
    316   1.4    bouyer 				    Ent_led_on1 / sizeof(esiop_led_on[0]) + j
    317   1.4    bouyer 				    ] = htole32(esiop_led_on[j]);
    318   1.4    bouyer 			for (j = 0; j < (sizeof(esiop_led_on) /
    319   1.4    bouyer 			    sizeof(esiop_led_on[0])); j++)
    320   1.4    bouyer 				sc->sc_c.sc_script[
    321   1.4    bouyer 				    Ent_led_on2 / sizeof(esiop_led_on[0]) + j
    322   1.4    bouyer 				    ] = htole32(esiop_led_on[j]);
    323   1.4    bouyer 			for (j = 0; j < (sizeof(esiop_led_off) /
    324   1.4    bouyer 			    sizeof(esiop_led_off[0])); j++)
    325   1.4    bouyer 				sc->sc_c.sc_script[
    326   1.4    bouyer 				   Ent_led_off / sizeof(esiop_led_off[0]) + j
    327   1.4    bouyer 				   ] = htole32(esiop_led_off[j]);
    328   1.4    bouyer 		}
    329   1.1    bouyer 	}
    330   1.1    bouyer 	/* get base of scheduler ring */
    331   1.1    bouyer 	addr = sc->sc_c.sc_scriptaddr + sc->sc_shedoffset * sizeof(u_int32_t);
    332   1.1    bouyer 	/* init scheduler */
    333   1.1    bouyer 	for (i = 0; i < A_ncmd_slots; i++) {
    334   1.8    bouyer 		esiop_script_write(sc,
    335   1.8    bouyer 		    sc->sc_shedoffset + i * CMD_SLOTSIZE, A_f_cmd_free);
    336   1.1    bouyer 	}
    337   1.1    bouyer 	sc->sc_currschedslot = 0;
    338   1.1    bouyer 	bus_space_write_1(sc->sc_c.sc_rt, sc->sc_c.sc_rh, SIOP_SCRATCHE, 0);
    339   1.1    bouyer 	bus_space_write_4(sc->sc_c.sc_rt, sc->sc_c.sc_rh, SIOP_SCRATCHD, addr);
    340   1.1    bouyer 	/*
    341   1.1    bouyer 	 * 0x78000000 is a 'move data8 to reg'. data8 is the second
    342   1.1    bouyer 	 * octet, reg offset is the third.
    343   1.1    bouyer 	 */
    344   1.1    bouyer 	esiop_script_write(sc, Ent_cmdr0 / 4,
    345   1.1    bouyer 	    0x78640000 | ((addr & 0x000000ff) <<  8));
    346   1.1    bouyer 	esiop_script_write(sc, Ent_cmdr1 / 4,
    347   1.1    bouyer 	    0x78650000 | ((addr & 0x0000ff00)      ));
    348   1.1    bouyer 	esiop_script_write(sc, Ent_cmdr2 / 4,
    349   1.1    bouyer 	    0x78660000 | ((addr & 0x00ff0000) >>  8));
    350   1.1    bouyer 	esiop_script_write(sc, Ent_cmdr3 / 4,
    351   1.1    bouyer 	    0x78670000 | ((addr & 0xff000000) >> 16));
    352  1.11    bouyer 	/* done ring */
    353  1.11    bouyer 	for (i = 0; i < A_ndone_slots; i++)
    354  1.11    bouyer 		sc->sc_done_slot[i] = 0;
    355  1.11    bouyer 	bus_dmamap_sync(sc->sc_c.sc_dmat, sc->sc_done_map,
    356  1.11    bouyer 	    sc->sc_done_offset, A_ndone_slots * sizeof(u_int32_t),
    357  1.11    bouyer 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
    358  1.11    bouyer 	addr = sc->sc_done_map->dm_segs[0].ds_addr + sc->sc_done_offset;
    359  1.11    bouyer 	sc->sc_currdoneslot = 0;
    360  1.11    bouyer 	bus_space_write_1(sc->sc_c.sc_rt, sc->sc_c.sc_rh, SIOP_SCRATCHE + 2, 0);
    361  1.11    bouyer 	bus_space_write_4(sc->sc_c.sc_rt, sc->sc_c.sc_rh, SIOP_SCRATCHF, addr);
    362  1.11    bouyer 	esiop_script_write(sc, Ent_doner0 / 4,
    363  1.11    bouyer 	    0x786c0000 | ((addr & 0x000000ff) <<  8));
    364  1.11    bouyer 	esiop_script_write(sc, Ent_doner1 / 4,
    365  1.11    bouyer 	    0x786d0000 | ((addr & 0x0000ff00)      ));
    366  1.11    bouyer 	esiop_script_write(sc, Ent_doner2 / 4,
    367  1.11    bouyer 	    0x786e0000 | ((addr & 0x00ff0000) >>  8));
    368  1.11    bouyer 	esiop_script_write(sc, Ent_doner3 / 4,
    369  1.11    bouyer 	    0x786f0000 | ((addr & 0xff000000) >> 16));
    370  1.11    bouyer 
    371   1.1    bouyer 	/* set flags */
    372   1.1    bouyer 	bus_space_write_4(sc->sc_c.sc_rt, sc->sc_c.sc_rh, SIOP_SCRATCHC, 0);
    373   1.1    bouyer 	/* write pointer of base of target DSA table */
    374   1.1    bouyer 	addr = (sc->sc_target_table_offset * sizeof(u_int32_t)) +
    375   1.1    bouyer 	    sc->sc_c.sc_scriptaddr;
    376   1.1    bouyer 	esiop_script_write(sc, (Ent_load_targtable / 4) + 0,
    377   1.1    bouyer 	    esiop_script_read(sc,(Ent_load_targtable / 4) + 0) |
    378   1.1    bouyer 	    ((addr & 0x000000ff) <<  8));
    379   1.1    bouyer 	esiop_script_write(sc, (Ent_load_targtable / 4) + 2,
    380   1.1    bouyer 	    esiop_script_read(sc,(Ent_load_targtable / 4) + 2) |
    381   1.1    bouyer 	    ((addr & 0x0000ff00)      ));
    382   1.1    bouyer 	esiop_script_write(sc, (Ent_load_targtable / 4) + 4,
    383   1.1    bouyer 	    esiop_script_read(sc,(Ent_load_targtable / 4) + 4) |
    384   1.1    bouyer 	    ((addr & 0x00ff0000) >>  8));
    385   1.1    bouyer 	esiop_script_write(sc, (Ent_load_targtable / 4) + 6,
    386   1.1    bouyer 	    esiop_script_read(sc,(Ent_load_targtable / 4) + 6) |
    387   1.1    bouyer 	    ((addr & 0xff000000) >> 16));
    388   1.1    bouyer #ifdef SIOP_DEBUG
    389   1.1    bouyer 	printf("%s: target table offset %d free offset %d\n",
    390  1.46   tsutsui 	    device_xname(sc->sc_c.sc_dev), sc->sc_target_table_offset,
    391   1.1    bouyer 	    sc->sc_free_offset);
    392   1.1    bouyer #endif
    393   1.1    bouyer 
    394   1.1    bouyer 	/* register existing targets */
    395   1.1    bouyer 	for (i = 0; i < sc->sc_c.sc_chan.chan_ntargets; i++) {
    396   1.1    bouyer 		if (sc->sc_c.targets[i])
    397   1.1    bouyer 			esiop_target_register(sc, i);
    398   1.1    bouyer 	}
    399   1.1    bouyer 	/* start script */
    400   1.1    bouyer 	if ((sc->sc_c.features & SF_CHIP_RAM) == 0) {
    401   1.1    bouyer 		bus_dmamap_sync(sc->sc_c.sc_dmat, sc->sc_c.sc_scriptdma, 0,
    402   1.1    bouyer 		    PAGE_SIZE, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
    403   1.1    bouyer 	}
    404   1.1    bouyer 	bus_space_write_4(sc->sc_c.sc_rt, sc->sc_c.sc_rh, SIOP_DSP,
    405   1.1    bouyer 	    sc->sc_c.sc_scriptaddr + Ent_reselect);
    406   1.1    bouyer }
    407   1.1    bouyer 
    408   1.1    bouyer #if 0
    409   1.1    bouyer #define CALL_SCRIPT(ent) do {\
    410   1.1    bouyer 	printf ("start script DSA 0x%lx DSP 0x%lx\n", \
    411   1.1    bouyer 	    esiop_cmd->cmd_c.dsa, \
    412   1.1    bouyer 	    sc->sc_c.sc_scriptaddr + ent); \
    413   1.1    bouyer bus_space_write_4(sc->sc_c.sc_rt, sc->sc_c.sc_rh, SIOP_DSP, sc->sc_c.sc_scriptaddr + ent); \
    414   1.1    bouyer } while (0)
    415   1.1    bouyer #else
    416   1.1    bouyer #define CALL_SCRIPT(ent) do {\
    417   1.1    bouyer bus_space_write_4(sc->sc_c.sc_rt, sc->sc_c.sc_rh, SIOP_DSP, sc->sc_c.sc_scriptaddr + ent); \
    418   1.1    bouyer } while (0)
    419   1.1    bouyer #endif
    420   1.1    bouyer 
    421   1.1    bouyer int
    422  1.43       dsl esiop_intr(void *v)
    423   1.1    bouyer {
    424   1.1    bouyer 	struct esiop_softc *sc = v;
    425   1.1    bouyer 	struct esiop_target *esiop_target;
    426   1.1    bouyer 	struct esiop_cmd *esiop_cmd;
    427   1.1    bouyer 	struct esiop_lun *esiop_lun;
    428   1.1    bouyer 	struct scsipi_xfer *xs;
    429  1.23   mycroft 	int istat, sist, sstat1, dstat = 0; /* XXX: gcc */
    430   1.1    bouyer 	u_int32_t irqcode;
    431   1.1    bouyer 	int need_reset = 0;
    432   1.1    bouyer 	int offset, target, lun, tag;
    433   1.1    bouyer 	u_int32_t tflags;
    434   1.3    bouyer 	u_int32_t addr;
    435   1.1    bouyer 	int freetarget = 0;
    436   1.1    bouyer 	int slot;
    437   1.1    bouyer 	int retval = 0;
    438   1.1    bouyer 
    439   1.1    bouyer again:
    440   1.1    bouyer 	istat = bus_space_read_1(sc->sc_c.sc_rt, sc->sc_c.sc_rh, SIOP_ISTAT);
    441   1.1    bouyer 	if ((istat & (ISTAT_INTF | ISTAT_DIP | ISTAT_SIP)) == 0) {
    442   1.1    bouyer 		return retval;
    443   1.1    bouyer 	}
    444   1.1    bouyer 	retval = 1;
    445   1.1    bouyer 	INCSTAT(esiop_stat_intr);
    446  1.12    bouyer 	esiop_checkdone(sc);
    447   1.1    bouyer 	if (istat & ISTAT_INTF) {
    448   1.1    bouyer 		bus_space_write_1(sc->sc_c.sc_rt, sc->sc_c.sc_rh,
    449   1.1    bouyer 		    SIOP_ISTAT, ISTAT_INTF);
    450   1.1    bouyer 		goto again;
    451   1.1    bouyer 	}
    452   1.9    bouyer 
    453  1.11    bouyer 	if ((istat &(ISTAT_DIP | ISTAT_SIP | ISTAT_ABRT)) ==
    454   1.9    bouyer 	    (ISTAT_DIP | ISTAT_ABRT)) {
    455  1.10    bouyer 		/* clear abort */
    456   1.9    bouyer 		bus_space_write_1(sc->sc_c.sc_rt, sc->sc_c.sc_rh,
    457   1.9    bouyer 		    SIOP_ISTAT, 0);
    458   1.9    bouyer 	}
    459  1.11    bouyer 
    460   1.1    bouyer 	/* get CMD from T/L/Q */
    461   1.1    bouyer 	tflags = bus_space_read_4(sc->sc_c.sc_rt, sc->sc_c.sc_rh,
    462   1.1    bouyer 	    SIOP_SCRATCHC);
    463   1.1    bouyer #ifdef SIOP_DEBUG_INTR
    464   1.1    bouyer 		printf("interrupt, istat=0x%x tflags=0x%x "
    465   1.1    bouyer 		    "DSA=0x%x DSP=0x%lx\n", istat, tflags,
    466   1.1    bouyer 		    bus_space_read_4(sc->sc_c.sc_rt, sc->sc_c.sc_rh, SIOP_DSA),
    467   1.1    bouyer 		    (u_long)(bus_space_read_4(sc->sc_c.sc_rt, sc->sc_c.sc_rh,
    468   1.1    bouyer 			SIOP_DSP) -
    469   1.1    bouyer 		    sc->sc_c.sc_scriptaddr));
    470   1.1    bouyer #endif
    471   1.1    bouyer 	target = (tflags & A_f_c_target) ? ((tflags >> 8) & 0xff) : -1;
    472   1.1    bouyer 	if (target > sc->sc_c.sc_chan.chan_ntargets) target = -1;
    473   1.1    bouyer 	lun = (tflags & A_f_c_lun) ? ((tflags >> 16) & 0xff) : -1;
    474   1.1    bouyer 	if (lun > sc->sc_c.sc_chan.chan_nluns) lun = -1;
    475   1.1    bouyer 	tag = (tflags & A_f_c_tag) ? ((tflags >> 24) & 0xff) : -1;
    476   1.1    bouyer 
    477   1.1    bouyer 	if (target >= 0 && lun >= 0) {
    478   1.1    bouyer 		esiop_target = (struct esiop_target *)sc->sc_c.targets[target];
    479   1.1    bouyer 		if (esiop_target == NULL) {
    480   1.1    bouyer 			printf("esiop_target (target %d) not valid\n", target);
    481   1.1    bouyer 			goto none;
    482   1.1    bouyer 		}
    483   1.1    bouyer 		esiop_lun = esiop_target->esiop_lun[lun];
    484   1.1    bouyer 		if (esiop_lun == NULL) {
    485   1.1    bouyer 			printf("esiop_lun (target %d lun %d) not valid\n",
    486   1.1    bouyer 			    target, lun);
    487   1.1    bouyer 			goto none;
    488   1.1    bouyer 		}
    489   1.2    bouyer 		esiop_cmd =
    490   1.2    bouyer 		    (tag >= 0) ? esiop_lun->tactive[tag] : esiop_lun->active;
    491   1.1    bouyer 		if (esiop_cmd == NULL) {
    492   1.2    bouyer 			printf("esiop_cmd (target %d lun %d tag %d) not valid\n",
    493   1.2    bouyer 			    target, lun, tag);
    494   1.1    bouyer 			goto none;
    495   1.1    bouyer 		}
    496   1.1    bouyer 		xs = esiop_cmd->cmd_c.xs;
    497   1.1    bouyer #ifdef DIAGNOSTIC
    498   1.1    bouyer 		if (esiop_cmd->cmd_c.status != CMDST_ACTIVE) {
    499   1.1    bouyer  			printf("esiop_cmd (target %d lun %d) "
    500   1.1    bouyer 			    "not active (%d)\n", target, lun,
    501   1.1    bouyer 			    esiop_cmd->cmd_c.status);
    502   1.1    bouyer 			goto none;
    503   1.1    bouyer 		}
    504   1.1    bouyer #endif
    505   1.3    bouyer 		esiop_table_sync(esiop_cmd,
    506   1.3    bouyer 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
    507   1.1    bouyer 	} else {
    508   1.1    bouyer none:
    509   1.1    bouyer 		xs = NULL;
    510   1.1    bouyer 		esiop_target = NULL;
    511   1.1    bouyer 		esiop_lun = NULL;
    512   1.1    bouyer 		esiop_cmd = NULL;
    513   1.1    bouyer 	}
    514   1.1    bouyer 	if (istat & ISTAT_DIP) {
    515   1.1    bouyer 		dstat = bus_space_read_1(sc->sc_c.sc_rt, sc->sc_c.sc_rh,
    516   1.1    bouyer 		    SIOP_DSTAT);
    517  1.10    bouyer 		if (dstat & DSTAT_ABRT) {
    518  1.10    bouyer 			/* was probably generated by a bus reset IOCTL */
    519  1.10    bouyer 			if ((dstat & DSTAT_DFE) == 0)
    520  1.10    bouyer 				siop_clearfifo(&sc->sc_c);
    521  1.10    bouyer 			goto reset;
    522  1.10    bouyer 		}
    523   1.1    bouyer 		if (dstat & DSTAT_SSI) {
    524   1.1    bouyer 			printf("single step dsp 0x%08x dsa 0x08%x\n",
    525   1.1    bouyer 			    (int)(bus_space_read_4(sc->sc_c.sc_rt,
    526   1.1    bouyer 			    sc->sc_c.sc_rh, SIOP_DSP) -
    527   1.1    bouyer 			    sc->sc_c.sc_scriptaddr),
    528   1.1    bouyer 			    bus_space_read_4(sc->sc_c.sc_rt, sc->sc_c.sc_rh,
    529   1.1    bouyer 				SIOP_DSA));
    530   1.1    bouyer 			if ((dstat & ~(DSTAT_DFE | DSTAT_SSI)) == 0 &&
    531   1.1    bouyer 			    (istat & ISTAT_SIP) == 0) {
    532   1.1    bouyer 				bus_space_write_1(sc->sc_c.sc_rt,
    533   1.1    bouyer 				    sc->sc_c.sc_rh, SIOP_DCNTL,
    534   1.1    bouyer 				    bus_space_read_1(sc->sc_c.sc_rt,
    535   1.1    bouyer 				    sc->sc_c.sc_rh, SIOP_DCNTL) | DCNTL_STD);
    536   1.1    bouyer 			}
    537   1.1    bouyer 			return 1;
    538   1.1    bouyer 		}
    539  1.10    bouyer 
    540   1.1    bouyer 		if (dstat & ~(DSTAT_SIR | DSTAT_DFE | DSTAT_SSI)) {
    541  1.46   tsutsui 		printf("%s: DMA IRQ:", device_xname(sc->sc_c.sc_dev));
    542   1.1    bouyer 		if (dstat & DSTAT_IID)
    543   1.1    bouyer 			printf(" Illegal instruction");
    544   1.1    bouyer 		if (dstat & DSTAT_BF)
    545   1.1    bouyer 			printf(" bus fault");
    546   1.1    bouyer 		if (dstat & DSTAT_MDPE)
    547   1.1    bouyer 			printf(" parity");
    548   1.1    bouyer 		if (dstat & DSTAT_DFE)
    549  1.19       wiz 			printf(" DMA fifo empty");
    550   1.9    bouyer 		else
    551   1.9    bouyer 			siop_clearfifo(&sc->sc_c);
    552   1.1    bouyer 		printf(", DSP=0x%x DSA=0x%x: ",
    553   1.1    bouyer 		    (int)(bus_space_read_4(sc->sc_c.sc_rt, sc->sc_c.sc_rh,
    554   1.1    bouyer 			SIOP_DSP) - sc->sc_c.sc_scriptaddr),
    555   1.1    bouyer 		    bus_space_read_4(sc->sc_c.sc_rt, sc->sc_c.sc_rh, SIOP_DSA));
    556   1.1    bouyer 		if (esiop_cmd)
    557   1.9    bouyer 			printf("T/L/Q=%d/%d/%d last msg_in=0x%x status=0x%x\n",
    558   1.9    bouyer 			    target, lun, tag, esiop_cmd->cmd_tables->msg_in[0],
    559   1.1    bouyer 			    le32toh(esiop_cmd->cmd_tables->status));
    560  1.11    bouyer 		else
    561   1.1    bouyer 			printf(" current T/L/Q invalid\n");
    562   1.1    bouyer 		need_reset = 1;
    563   1.1    bouyer 		}
    564   1.1    bouyer 	}
    565   1.1    bouyer 	if (istat & ISTAT_SIP) {
    566   1.1    bouyer 		if (istat & ISTAT_DIP)
    567   1.1    bouyer 			delay(10);
    568   1.1    bouyer 		/*
    569  1.24       wiz 		 * Can't read sist0 & sist1 independently, or we have to
    570   1.1    bouyer 		 * insert delay
    571   1.1    bouyer 		 */
    572   1.1    bouyer 		sist = bus_space_read_2(sc->sc_c.sc_rt, sc->sc_c.sc_rh,
    573   1.1    bouyer 		    SIOP_SIST0);
    574   1.1    bouyer 		sstat1 = bus_space_read_1(sc->sc_c.sc_rt, sc->sc_c.sc_rh,
    575   1.1    bouyer 		    SIOP_SSTAT1);
    576   1.1    bouyer #ifdef SIOP_DEBUG_INTR
    577   1.1    bouyer 		printf("scsi interrupt, sist=0x%x sstat1=0x%x "
    578   1.1    bouyer 		    "DSA=0x%x DSP=0x%lx\n", sist, sstat1,
    579   1.1    bouyer 		    bus_space_read_4(sc->sc_c.sc_rt, sc->sc_c.sc_rh, SIOP_DSA),
    580   1.1    bouyer 		    (u_long)(bus_space_read_4(sc->sc_c.sc_rt, sc->sc_c.sc_rh,
    581   1.1    bouyer 			SIOP_DSP) -
    582   1.1    bouyer 		    sc->sc_c.sc_scriptaddr));
    583   1.1    bouyer #endif
    584   1.1    bouyer 		if (sist & SIST0_RST) {
    585   1.1    bouyer 			esiop_handle_reset(sc);
    586   1.1    bouyer 			/* no table to flush here */
    587   1.1    bouyer 			return 1;
    588   1.1    bouyer 		}
    589   1.1    bouyer 		if (sist & SIST0_SGE) {
    590   1.1    bouyer 			if (esiop_cmd)
    591   1.1    bouyer 				scsipi_printaddr(xs->xs_periph);
    592   1.1    bouyer 			else
    593  1.46   tsutsui 				printf("%s:", device_xname(sc->sc_c.sc_dev));
    594   1.1    bouyer 			printf("scsi gross error\n");
    595   1.6    bouyer 			if (esiop_target)
    596   1.6    bouyer 				esiop_target->target_c.flags &= ~TARF_DT;
    597  1.13    bouyer #ifdef DEBUG
    598  1.13    bouyer 			printf("DSA=0x%x DSP=0x%lx\n",
    599  1.13    bouyer 			    bus_space_read_4(sc->sc_c.sc_rt, sc->sc_c.sc_rh, SIOP_DSA),
    600  1.13    bouyer 			    (u_long)(bus_space_read_4(sc->sc_c.sc_rt, sc->sc_c.sc_rh,
    601  1.13    bouyer 				SIOP_DSP) -
    602  1.13    bouyer 			    sc->sc_c.sc_scriptaddr));
    603  1.14    bouyer 			printf("SDID 0x%x SCNTL3 0x%x SXFER 0x%x SCNTL4 0x%x\n",
    604  1.14    bouyer 			    bus_space_read_1(sc->sc_c.sc_rt, sc->sc_c.sc_rh, SIOP_SDID),
    605  1.14    bouyer 			     bus_space_read_1(sc->sc_c.sc_rt, sc->sc_c.sc_rh, SIOP_SCNTL3),
    606  1.14    bouyer 			     bus_space_read_1(sc->sc_c.sc_rt, sc->sc_c.sc_rh, SIOP_SXFER),
    607  1.14    bouyer 			     bus_space_read_1(sc->sc_c.sc_rt, sc->sc_c.sc_rh, SIOP_SCNTL4));
    608  1.14    bouyer 
    609  1.13    bouyer #endif
    610   1.1    bouyer 			goto reset;
    611   1.1    bouyer 		}
    612   1.1    bouyer 		if ((sist & SIST0_MA) && need_reset == 0) {
    613  1.11    bouyer 			if (esiop_cmd) {
    614   1.1    bouyer 				int scratchc0;
    615   1.1    bouyer 				dstat = bus_space_read_1(sc->sc_c.sc_rt,
    616   1.1    bouyer 				    sc->sc_c.sc_rh, SIOP_DSTAT);
    617   1.1    bouyer 				/*
    618   1.1    bouyer 				 * first restore DSA, in case we were in a S/G
    619   1.1    bouyer 				 * operation.
    620   1.1    bouyer 				 */
    621   1.1    bouyer 				bus_space_write_4(sc->sc_c.sc_rt,
    622   1.1    bouyer 				    sc->sc_c.sc_rh,
    623   1.1    bouyer 				    SIOP_DSA, esiop_cmd->cmd_c.dsa);
    624   1.1    bouyer 				scratchc0 = bus_space_read_1(sc->sc_c.sc_rt,
    625   1.1    bouyer 				    sc->sc_c.sc_rh, SIOP_SCRATCHC);
    626   1.1    bouyer 				switch (sstat1 & SSTAT1_PHASE_MASK) {
    627   1.1    bouyer 				case SSTAT1_PHASE_STATUS:
    628   1.1    bouyer 				/*
    629   1.1    bouyer 				 * previous phase may be aborted for any reason
    630   1.1    bouyer 				 * ( for example, the target has less data to
    631  1.29    bouyer 				 * transfer than requested). Compute resid and
    632  1.29    bouyer 				 * just go to status, the command should
    633  1.29    bouyer 				 * terminate.
    634   1.1    bouyer 				 */
    635   1.1    bouyer 					INCSTAT(esiop_stat_intr_shortxfer);
    636  1.28    bouyer 					if (scratchc0 & A_f_c_data)
    637  1.29    bouyer 						siop_ma(&esiop_cmd->cmd_c);
    638  1.28    bouyer 					else if ((dstat & DSTAT_DFE) == 0)
    639   1.1    bouyer 						siop_clearfifo(&sc->sc_c);
    640   1.1    bouyer 					CALL_SCRIPT(Ent_status);
    641   1.1    bouyer 					return 1;
    642   1.1    bouyer 				case SSTAT1_PHASE_MSGIN:
    643  1.29    bouyer 				/*
    644  1.29    bouyer 				 * target may be ready to disconnect
    645  1.29    bouyer 				 * Compute resid which would be used later
    646  1.29    bouyer 				 * if a save data pointer is needed.
    647  1.29    bouyer 				 */
    648   1.1    bouyer 					INCSTAT(esiop_stat_intr_xferdisc);
    649   1.1    bouyer 					if (scratchc0 & A_f_c_data)
    650  1.29    bouyer 						siop_ma(&esiop_cmd->cmd_c);
    651   1.1    bouyer 					else if ((dstat & DSTAT_DFE) == 0)
    652   1.1    bouyer 						siop_clearfifo(&sc->sc_c);
    653   1.1    bouyer 					bus_space_write_1(sc->sc_c.sc_rt,
    654   1.1    bouyer 					    sc->sc_c.sc_rh, SIOP_SCRATCHC,
    655   1.1    bouyer 					    scratchc0 & ~A_f_c_data);
    656   1.1    bouyer 					CALL_SCRIPT(Ent_msgin);
    657   1.1    bouyer 					return 1;
    658   1.1    bouyer 				}
    659  1.46   tsutsui 				aprint_error_dev(sc->sc_c.sc_dev, "unexpected phase mismatch %d\n",
    660   1.1    bouyer 				    sstat1 & SSTAT1_PHASE_MASK);
    661   1.1    bouyer 			} else {
    662  1.46   tsutsui 				aprint_error_dev(sc->sc_c.sc_dev, "phase mismatch without command\n");
    663   1.1    bouyer 			}
    664   1.1    bouyer 			need_reset = 1;
    665   1.1    bouyer 		}
    666   1.1    bouyer 		if (sist & SIST0_PAR) {
    667   1.1    bouyer 			/* parity error, reset */
    668   1.1    bouyer 			if (esiop_cmd)
    669   1.1    bouyer 				scsipi_printaddr(xs->xs_periph);
    670   1.1    bouyer 			else
    671  1.46   tsutsui 				printf("%s:", device_xname(sc->sc_c.sc_dev));
    672   1.1    bouyer 			printf("parity error\n");
    673   1.6    bouyer 			if (esiop_target)
    674   1.6    bouyer 				esiop_target->target_c.flags &= ~TARF_DT;
    675   1.1    bouyer 			goto reset;
    676   1.1    bouyer 		}
    677   1.1    bouyer 		if ((sist & (SIST1_STO << 8)) && need_reset == 0) {
    678   1.1    bouyer 			/*
    679   1.9    bouyer 			 * selection time out, assume there's no device here
    680   1.9    bouyer 			 * We also have to update the ring pointer ourselve
    681   1.1    bouyer 			 */
    682   1.1    bouyer 			slot = bus_space_read_1(sc->sc_c.sc_rt,
    683   1.1    bouyer 			    sc->sc_c.sc_rh, SIOP_SCRATCHE);
    684   1.1    bouyer 			esiop_script_sync(sc,
    685   1.1    bouyer 			    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
    686   1.9    bouyer #ifdef SIOP_DEBUG_SCHED
    687   1.9    bouyer 			printf("sel timeout target %d, slot %d\n", target, slot);
    688   1.9    bouyer #endif
    689   1.3    bouyer 			/*
    690   1.3    bouyer 			 * mark this slot as free, and advance to next slot
    691   1.3    bouyer 			 */
    692   1.8    bouyer 			esiop_script_write(sc,
    693   1.8    bouyer 			    sc->sc_shedoffset + slot * CMD_SLOTSIZE,
    694   1.3    bouyer 			    A_f_cmd_free);
    695   1.3    bouyer 			addr = bus_space_read_4(sc->sc_c.sc_rt,
    696   1.3    bouyer 				    sc->sc_c.sc_rh, SIOP_SCRATCHD);
    697   1.3    bouyer 			if (slot < (A_ncmd_slots - 1)) {
    698   1.3    bouyer 				bus_space_write_1(sc->sc_c.sc_rt,
    699   1.3    bouyer 				    sc->sc_c.sc_rh, SIOP_SCRATCHE, slot + 1);
    700   1.8    bouyer 				addr = addr + sizeof(struct esiop_slot);
    701   1.3    bouyer 			} else {
    702   1.3    bouyer 				bus_space_write_1(sc->sc_c.sc_rt,
    703   1.3    bouyer 				    sc->sc_c.sc_rh, SIOP_SCRATCHE, 0);
    704   1.3    bouyer 				addr = sc->sc_c.sc_scriptaddr +
    705   1.3    bouyer 				    sc->sc_shedoffset * sizeof(u_int32_t);
    706   1.3    bouyer 			}
    707   1.3    bouyer 			bus_space_write_4(sc->sc_c.sc_rt, sc->sc_c.sc_rh,
    708   1.3    bouyer 			    SIOP_SCRATCHD, addr);
    709   1.3    bouyer 			esiop_script_sync(sc,
    710   1.3    bouyer 			    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
    711   1.1    bouyer 			if (esiop_cmd) {
    712   1.1    bouyer 				esiop_cmd->cmd_c.status = CMDST_DONE;
    713   1.1    bouyer 				xs->error = XS_SELTIMEOUT;
    714   1.1    bouyer 				freetarget = 1;
    715  1.29    bouyer 				goto end;
    716   1.1    bouyer 			} else {
    717   1.1    bouyer 				printf("%s: selection timeout without "
    718   1.3    bouyer 				    "command, target %d (sdid 0x%x), "
    719   1.3    bouyer 				    "slot %d\n",
    720  1.46   tsutsui 				    device_xname(sc->sc_c.sc_dev), target,
    721   1.3    bouyer 				    bus_space_read_1(sc->sc_c.sc_rt,
    722   1.3    bouyer 				    sc->sc_c.sc_rh, SIOP_SDID), slot);
    723   1.1    bouyer 				need_reset = 1;
    724   1.1    bouyer 			}
    725   1.1    bouyer 		}
    726   1.1    bouyer 		if (sist & SIST0_UDC) {
    727   1.1    bouyer 			/*
    728   1.1    bouyer 			 * unexpected disconnect. Usually the target signals
    729   1.1    bouyer 			 * a fatal condition this way. Attempt to get sense.
    730   1.1    bouyer 			 */
    731   1.1    bouyer 			 if (esiop_cmd) {
    732   1.1    bouyer 				esiop_cmd->cmd_tables->status =
    733   1.1    bouyer 				    htole32(SCSI_CHECK);
    734  1.29    bouyer 				goto end;
    735   1.1    bouyer 			}
    736  1.46   tsutsui 			aprint_error_dev(sc->sc_c.sc_dev, "unexpected disconnect without "
    737  1.42    cegger 			    "command\n");
    738   1.1    bouyer 			goto reset;
    739   1.1    bouyer 		}
    740   1.1    bouyer 		if (sist & (SIST1_SBMC << 8)) {
    741   1.1    bouyer 			/* SCSI bus mode change */
    742   1.1    bouyer 			if (siop_modechange(&sc->sc_c) == 0 || need_reset == 1)
    743   1.1    bouyer 				goto reset;
    744   1.1    bouyer 			if ((istat & ISTAT_DIP) && (dstat & DSTAT_SIR)) {
    745   1.1    bouyer 				/*
    746   1.1    bouyer 				 * we have a script interrupt, it will
    747   1.1    bouyer 				 * restart the script.
    748   1.1    bouyer 				 */
    749   1.1    bouyer 				goto scintr;
    750   1.1    bouyer 			}
    751   1.1    bouyer 			/*
    752   1.1    bouyer 			 * else we have to restart it ourselve, at the
    753   1.1    bouyer 			 * interrupted instruction.
    754   1.1    bouyer 			 */
    755   1.1    bouyer 			bus_space_write_4(sc->sc_c.sc_rt, sc->sc_c.sc_rh,
    756   1.1    bouyer 			    SIOP_DSP,
    757   1.1    bouyer 			    bus_space_read_4(sc->sc_c.sc_rt, sc->sc_c.sc_rh,
    758   1.1    bouyer 			    SIOP_DSP) - 8);
    759   1.1    bouyer 			return 1;
    760   1.1    bouyer 		}
    761  1.24       wiz 		/* Else it's an unhandled exception (for now). */
    762  1.46   tsutsui 		aprint_error_dev(sc->sc_c.sc_dev, "unhandled scsi interrupt, sist=0x%x sstat1=0x%x "
    763  1.42    cegger 		    "DSA=0x%x DSP=0x%x\n", sist,
    764   1.1    bouyer 		    bus_space_read_1(sc->sc_c.sc_rt, sc->sc_c.sc_rh,
    765   1.1    bouyer 			SIOP_SSTAT1),
    766   1.1    bouyer 		    bus_space_read_4(sc->sc_c.sc_rt, sc->sc_c.sc_rh, SIOP_DSA),
    767   1.1    bouyer 		    (int)(bus_space_read_4(sc->sc_c.sc_rt, sc->sc_c.sc_rh,
    768   1.1    bouyer 			SIOP_DSP) - sc->sc_c.sc_scriptaddr));
    769   1.1    bouyer 		if (esiop_cmd) {
    770   1.1    bouyer 			esiop_cmd->cmd_c.status = CMDST_DONE;
    771   1.1    bouyer 			xs->error = XS_SELTIMEOUT;
    772  1.29    bouyer 			goto end;
    773   1.1    bouyer 		}
    774   1.1    bouyer 		need_reset = 1;
    775   1.1    bouyer 	}
    776   1.1    bouyer 	if (need_reset) {
    777   1.1    bouyer reset:
    778   1.1    bouyer 		/* fatal error, reset the bus */
    779   1.1    bouyer 		siop_resetbus(&sc->sc_c);
    780   1.1    bouyer 		/* no table to flush here */
    781   1.1    bouyer 		return 1;
    782   1.1    bouyer 	}
    783   1.1    bouyer 
    784   1.1    bouyer scintr:
    785   1.1    bouyer 	if ((istat & ISTAT_DIP) && (dstat & DSTAT_SIR)) { /* script interrupt */
    786   1.1    bouyer 		irqcode = bus_space_read_4(sc->sc_c.sc_rt, sc->sc_c.sc_rh,
    787   1.1    bouyer 		    SIOP_DSPS);
    788   1.1    bouyer #ifdef SIOP_DEBUG_INTR
    789   1.1    bouyer 		printf("script interrupt 0x%x\n", irqcode);
    790   1.1    bouyer #endif
    791   1.1    bouyer 		/*
    792   1.1    bouyer 		 * no command, or an inactive command is only valid for a
    793   1.1    bouyer 		 * reselect interrupt
    794   1.1    bouyer 		 */
    795   1.1    bouyer 		if ((irqcode & 0x80) == 0) {
    796   1.1    bouyer 			if (esiop_cmd == NULL) {
    797  1.46   tsutsui 				aprint_error_dev(sc->sc_c.sc_dev,
    798  1.42    cegger 			"script interrupt (0x%x) with invalid DSA !!!\n",
    799  1.42    cegger 				    irqcode);
    800   1.1    bouyer 				goto reset;
    801   1.1    bouyer 			}
    802   1.1    bouyer 			if (esiop_cmd->cmd_c.status != CMDST_ACTIVE) {
    803  1.46   tsutsui 				aprint_error_dev(sc->sc_c.sc_dev, "command with invalid status "
    804   1.1    bouyer 				    "(IRQ code 0x%x current status %d) !\n",
    805   1.1    bouyer 				    irqcode, esiop_cmd->cmd_c.status);
    806   1.1    bouyer 				xs = NULL;
    807   1.1    bouyer 			}
    808   1.1    bouyer 		}
    809   1.1    bouyer 		switch(irqcode) {
    810   1.1    bouyer 		case A_int_err:
    811   1.1    bouyer 			printf("error, DSP=0x%x\n",
    812   1.1    bouyer 			    (int)(bus_space_read_4(sc->sc_c.sc_rt,
    813   1.1    bouyer 			    sc->sc_c.sc_rh, SIOP_DSP) - sc->sc_c.sc_scriptaddr));
    814   1.1    bouyer 			if (xs) {
    815   1.1    bouyer 				xs->error = XS_SELTIMEOUT;
    816  1.29    bouyer 				goto end;
    817   1.1    bouyer 			} else {
    818   1.1    bouyer 				goto reset;
    819   1.1    bouyer 			}
    820   1.1    bouyer 		case A_int_msgin:
    821   1.1    bouyer 		{
    822   1.1    bouyer 			int msgin = bus_space_read_1(sc->sc_c.sc_rt,
    823   1.1    bouyer 			    sc->sc_c.sc_rh, SIOP_SFBR);
    824   1.1    bouyer 			if (msgin == MSG_MESSAGE_REJECT) {
    825   1.1    bouyer 				int msg, extmsg;
    826   1.1    bouyer 				if (esiop_cmd->cmd_tables->msg_out[0] & 0x80) {
    827   1.1    bouyer 					/*
    828   1.1    bouyer 					 * message was part of a identify +
    829  1.17       wiz 					 * something else. Identify shouldn't
    830   1.1    bouyer 					 * have been rejected.
    831   1.1    bouyer 					 */
    832   1.1    bouyer 					msg =
    833   1.1    bouyer 					    esiop_cmd->cmd_tables->msg_out[1];
    834   1.1    bouyer 					extmsg =
    835   1.1    bouyer 					    esiop_cmd->cmd_tables->msg_out[3];
    836   1.1    bouyer 				} else {
    837   1.1    bouyer 					msg =
    838   1.1    bouyer 					    esiop_cmd->cmd_tables->msg_out[0];
    839   1.1    bouyer 					extmsg =
    840   1.1    bouyer 					    esiop_cmd->cmd_tables->msg_out[2];
    841   1.1    bouyer 				}
    842   1.1    bouyer 				if (msg == MSG_MESSAGE_REJECT) {
    843   1.1    bouyer 					/* MSG_REJECT  for a MSG_REJECT  !*/
    844   1.1    bouyer 					if (xs)
    845   1.1    bouyer 						scsipi_printaddr(xs->xs_periph);
    846   1.1    bouyer 					else
    847   1.1    bouyer 						printf("%s: ",
    848  1.46   tsutsui 						   device_xname(sc->sc_c.sc_dev));
    849   1.1    bouyer 					printf("our reject message was "
    850   1.1    bouyer 					    "rejected\n");
    851   1.1    bouyer 					goto reset;
    852   1.1    bouyer 				}
    853   1.1    bouyer 				if (msg == MSG_EXTENDED &&
    854   1.1    bouyer 				    extmsg == MSG_EXT_WDTR) {
    855   1.1    bouyer 					/* WDTR rejected, initiate sync */
    856   1.1    bouyer 					if ((esiop_target->target_c.flags &
    857   1.1    bouyer 					   TARF_SYNC) == 0) {
    858   1.1    bouyer 						esiop_target->target_c.status =
    859   1.1    bouyer 						    TARST_OK;
    860   1.1    bouyer 						siop_update_xfer_mode(&sc->sc_c,
    861   1.1    bouyer 						    target);
    862   1.1    bouyer 						/* no table to flush here */
    863   1.1    bouyer 						CALL_SCRIPT(Ent_msgin_ack);
    864   1.1    bouyer 						return 1;
    865   1.1    bouyer 					}
    866   1.1    bouyer 					esiop_target->target_c.status =
    867   1.1    bouyer 					    TARST_SYNC_NEG;
    868   1.1    bouyer 					siop_sdtr_msg(&esiop_cmd->cmd_c, 0,
    869   1.6    bouyer 					    sc->sc_c.st_minsync,
    870   1.6    bouyer 					    sc->sc_c.maxoff);
    871   1.1    bouyer 					esiop_table_sync(esiop_cmd,
    872   1.1    bouyer 					    BUS_DMASYNC_PREREAD |
    873   1.1    bouyer 					    BUS_DMASYNC_PREWRITE);
    874   1.1    bouyer 					CALL_SCRIPT(Ent_send_msgout);
    875   1.1    bouyer 					return 1;
    876   1.1    bouyer 				} else if (msg == MSG_EXTENDED &&
    877   1.1    bouyer 				    extmsg == MSG_EXT_SDTR) {
    878   1.1    bouyer 					/* sync rejected */
    879   1.1    bouyer 					esiop_target->target_c.offset = 0;
    880   1.1    bouyer 					esiop_target->target_c.period = 0;
    881   1.1    bouyer 					esiop_target->target_c.status =
    882   1.1    bouyer 					    TARST_OK;
    883   1.1    bouyer 					siop_update_xfer_mode(&sc->sc_c,
    884   1.1    bouyer 					    target);
    885   1.1    bouyer 					/* no table to flush here */
    886   1.1    bouyer 					CALL_SCRIPT(Ent_msgin_ack);
    887   1.1    bouyer 					return 1;
    888   1.6    bouyer 				} else if (msg == MSG_EXTENDED &&
    889   1.6    bouyer 				    extmsg == MSG_EXT_PPR) {
    890   1.6    bouyer 					/* PPR rejected */
    891   1.6    bouyer 					esiop_target->target_c.offset = 0;
    892   1.6    bouyer 					esiop_target->target_c.period = 0;
    893   1.6    bouyer 					esiop_target->target_c.status =
    894   1.6    bouyer 					    TARST_OK;
    895   1.6    bouyer 					siop_update_xfer_mode(&sc->sc_c,
    896   1.6    bouyer 					    target);
    897   1.6    bouyer 					/* no table to flush here */
    898   1.6    bouyer 					CALL_SCRIPT(Ent_msgin_ack);
    899   1.6    bouyer 					return 1;
    900  1.11    bouyer 				} else if (msg == MSG_SIMPLE_Q_TAG ||
    901   1.1    bouyer 				    msg == MSG_HEAD_OF_Q_TAG ||
    902   1.1    bouyer 				    msg == MSG_ORDERED_Q_TAG) {
    903   1.2    bouyer 					if (esiop_handle_qtag_reject(
    904   1.1    bouyer 					    esiop_cmd) == -1)
    905   1.1    bouyer 						goto reset;
    906   1.1    bouyer 					CALL_SCRIPT(Ent_msgin_ack);
    907   1.1    bouyer 					return 1;
    908   1.1    bouyer 				}
    909   1.1    bouyer 				if (xs)
    910   1.1    bouyer 					scsipi_printaddr(xs->xs_periph);
    911   1.1    bouyer 				else
    912   1.1    bouyer 					printf("%s: ",
    913  1.46   tsutsui 					    device_xname(sc->sc_c.sc_dev));
    914   1.1    bouyer 				if (msg == MSG_EXTENDED) {
    915   1.1    bouyer 					printf("scsi message reject, extended "
    916   1.1    bouyer 					    "message sent was 0x%x\n", extmsg);
    917   1.1    bouyer 				} else {
    918   1.1    bouyer 					printf("scsi message reject, message "
    919   1.1    bouyer 					    "sent was 0x%x\n", msg);
    920   1.1    bouyer 				}
    921   1.1    bouyer 				/* no table to flush here */
    922   1.1    bouyer 				CALL_SCRIPT(Ent_msgin_ack);
    923   1.1    bouyer 				return 1;
    924   1.1    bouyer 			}
    925  1.30    bouyer 			if (msgin == MSG_IGN_WIDE_RESIDUE) {
    926  1.30    bouyer 			/* use the extmsgdata table to get the second byte */
    927  1.30    bouyer 				esiop_cmd->cmd_tables->t_extmsgdata.count =
    928  1.30    bouyer 				    htole32(1);
    929  1.30    bouyer 				esiop_table_sync(esiop_cmd,
    930  1.30    bouyer 				    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
    931  1.30    bouyer 				CALL_SCRIPT(Ent_get_extmsgdata);
    932  1.30    bouyer 				return 1;
    933  1.30    bouyer 			}
    934   1.1    bouyer 			if (xs)
    935   1.1    bouyer 				scsipi_printaddr(xs->xs_periph);
    936   1.1    bouyer 			else
    937  1.46   tsutsui 				printf("%s: ", device_xname(sc->sc_c.sc_dev));
    938  1.25    bouyer 			printf("unhandled message 0x%x\n", msgin);
    939   1.1    bouyer 			esiop_cmd->cmd_tables->msg_out[0] = MSG_MESSAGE_REJECT;
    940   1.1    bouyer 			esiop_cmd->cmd_tables->t_msgout.count= htole32(1);
    941   1.1    bouyer 			esiop_table_sync(esiop_cmd,
    942   1.1    bouyer 			    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
    943   1.1    bouyer 			CALL_SCRIPT(Ent_send_msgout);
    944   1.1    bouyer 			return 1;
    945   1.1    bouyer 		}
    946   1.1    bouyer 		case A_int_extmsgin:
    947   1.1    bouyer #ifdef SIOP_DEBUG_INTR
    948   1.1    bouyer 			printf("extended message: msg 0x%x len %d\n",
    949  1.11    bouyer 			    esiop_cmd->cmd_tables->msg_in[2],
    950   1.1    bouyer 			    esiop_cmd->cmd_tables->msg_in[1]);
    951   1.1    bouyer #endif
    952   1.5    bouyer 			if (esiop_cmd->cmd_tables->msg_in[1] >
    953   1.5    bouyer 			    sizeof(esiop_cmd->cmd_tables->msg_in) - 2)
    954  1.46   tsutsui 				aprint_error_dev(sc->sc_c.sc_dev, "extended message too big (%d)\n",
    955   1.1    bouyer 				    esiop_cmd->cmd_tables->msg_in[1]);
    956   1.1    bouyer 			esiop_cmd->cmd_tables->t_extmsgdata.count =
    957   1.1    bouyer 			    htole32(esiop_cmd->cmd_tables->msg_in[1] - 1);
    958   1.1    bouyer 			esiop_table_sync(esiop_cmd,
    959   1.1    bouyer 			    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
    960   1.1    bouyer 			CALL_SCRIPT(Ent_get_extmsgdata);
    961   1.1    bouyer 			return 1;
    962   1.1    bouyer 		case A_int_extmsgdata:
    963   1.1    bouyer #ifdef SIOP_DEBUG_INTR
    964   1.1    bouyer 			{
    965   1.1    bouyer 			int i;
    966   1.1    bouyer 			printf("extended message: 0x%x, data:",
    967   1.1    bouyer 			    esiop_cmd->cmd_tables->msg_in[2]);
    968   1.1    bouyer 			for (i = 3; i < 2 + esiop_cmd->cmd_tables->msg_in[1];
    969   1.1    bouyer 			    i++)
    970   1.1    bouyer 				printf(" 0x%x",
    971   1.1    bouyer 				    esiop_cmd->cmd_tables->msg_in[i]);
    972   1.1    bouyer 			printf("\n");
    973   1.1    bouyer 			}
    974   1.1    bouyer #endif
    975  1.30    bouyer 			if (esiop_cmd->cmd_tables->msg_in[0] ==
    976  1.30    bouyer 			    MSG_IGN_WIDE_RESIDUE) {
    977  1.30    bouyer 			/* we got the second byte of MSG_IGN_WIDE_RESIDUE */
    978  1.30    bouyer 				if (esiop_cmd->cmd_tables->msg_in[3] != 1)
    979  1.30    bouyer 					printf("MSG_IGN_WIDE_RESIDUE: "
    980  1.30    bouyer 					     "bad len %d\n",
    981  1.30    bouyer 					     esiop_cmd->cmd_tables->msg_in[3]);
    982  1.30    bouyer 				switch (siop_iwr(&esiop_cmd->cmd_c)) {
    983  1.30    bouyer 				case SIOP_NEG_MSGOUT:
    984  1.30    bouyer 					esiop_table_sync(esiop_cmd,
    985  1.30    bouyer 					    BUS_DMASYNC_PREREAD |
    986  1.30    bouyer 					    BUS_DMASYNC_PREWRITE);
    987  1.30    bouyer 					CALL_SCRIPT(Ent_send_msgout);
    988  1.30    bouyer 					return 1;
    989  1.30    bouyer 				case SIOP_NEG_ACK:
    990  1.30    bouyer 					CALL_SCRIPT(Ent_msgin_ack);
    991  1.30    bouyer 					return 1;
    992  1.30    bouyer 				default:
    993  1.30    bouyer 					panic("invalid retval from "
    994  1.30    bouyer 					    "siop_iwr()");
    995  1.30    bouyer 				}
    996  1.30    bouyer 				return 1;
    997  1.30    bouyer 			}
    998   1.6    bouyer 			if (esiop_cmd->cmd_tables->msg_in[2] == MSG_EXT_PPR) {
    999   1.6    bouyer 				switch (siop_ppr_neg(&esiop_cmd->cmd_c)) {
   1000   1.6    bouyer 				case SIOP_NEG_MSGOUT:
   1001   1.9    bouyer 					esiop_update_scntl3(sc,
   1002   1.9    bouyer 					    esiop_cmd->cmd_c.siop_target);
   1003   1.6    bouyer 					esiop_table_sync(esiop_cmd,
   1004   1.6    bouyer 					    BUS_DMASYNC_PREREAD |
   1005   1.6    bouyer 					    BUS_DMASYNC_PREWRITE);
   1006   1.6    bouyer 					CALL_SCRIPT(Ent_send_msgout);
   1007   1.9    bouyer 					return 1;
   1008   1.6    bouyer 				case SIOP_NEG_ACK:
   1009   1.9    bouyer 					esiop_update_scntl3(sc,
   1010   1.9    bouyer 					    esiop_cmd->cmd_c.siop_target);
   1011   1.6    bouyer 					CALL_SCRIPT(Ent_msgin_ack);
   1012   1.9    bouyer 					return 1;
   1013   1.6    bouyer 				default:
   1014   1.6    bouyer 					panic("invalid retval from "
   1015   1.6    bouyer 					    "siop_wdtr_neg()");
   1016   1.6    bouyer 				}
   1017   1.9    bouyer 				return 1;
   1018   1.6    bouyer 			}
   1019   1.1    bouyer 			if (esiop_cmd->cmd_tables->msg_in[2] == MSG_EXT_WDTR) {
   1020   1.1    bouyer 				switch (siop_wdtr_neg(&esiop_cmd->cmd_c)) {
   1021   1.1    bouyer 				case SIOP_NEG_MSGOUT:
   1022   1.9    bouyer 					esiop_update_scntl3(sc,
   1023   1.9    bouyer 					    esiop_cmd->cmd_c.siop_target);
   1024   1.1    bouyer 					esiop_table_sync(esiop_cmd,
   1025   1.1    bouyer 					    BUS_DMASYNC_PREREAD |
   1026   1.1    bouyer 					    BUS_DMASYNC_PREWRITE);
   1027   1.1    bouyer 					CALL_SCRIPT(Ent_send_msgout);
   1028   1.9    bouyer 					return 1;
   1029   1.1    bouyer 				case SIOP_NEG_ACK:
   1030   1.9    bouyer 					esiop_update_scntl3(sc,
   1031   1.9    bouyer 					    esiop_cmd->cmd_c.siop_target);
   1032   1.1    bouyer 					CALL_SCRIPT(Ent_msgin_ack);
   1033   1.9    bouyer 					return 1;
   1034   1.1    bouyer 				default:
   1035   1.1    bouyer 					panic("invalid retval from "
   1036   1.1    bouyer 					    "siop_wdtr_neg()");
   1037   1.1    bouyer 				}
   1038   1.9    bouyer 				return 1;
   1039   1.1    bouyer 			}
   1040   1.1    bouyer 			if (esiop_cmd->cmd_tables->msg_in[2] == MSG_EXT_SDTR) {
   1041   1.1    bouyer 				switch (siop_sdtr_neg(&esiop_cmd->cmd_c)) {
   1042   1.1    bouyer 				case SIOP_NEG_MSGOUT:
   1043   1.9    bouyer 					esiop_update_scntl3(sc,
   1044   1.9    bouyer 					    esiop_cmd->cmd_c.siop_target);
   1045   1.1    bouyer 					esiop_table_sync(esiop_cmd,
   1046   1.1    bouyer 					    BUS_DMASYNC_PREREAD |
   1047   1.1    bouyer 					    BUS_DMASYNC_PREWRITE);
   1048   1.1    bouyer 					CALL_SCRIPT(Ent_send_msgout);
   1049   1.9    bouyer 					return 1;
   1050   1.1    bouyer 				case SIOP_NEG_ACK:
   1051   1.9    bouyer 					esiop_update_scntl3(sc,
   1052   1.9    bouyer 					    esiop_cmd->cmd_c.siop_target);
   1053   1.1    bouyer 					CALL_SCRIPT(Ent_msgin_ack);
   1054   1.9    bouyer 					return 1;
   1055   1.1    bouyer 				default:
   1056   1.1    bouyer 					panic("invalid retval from "
   1057   1.1    bouyer 					    "siop_wdtr_neg()");
   1058   1.1    bouyer 				}
   1059   1.9    bouyer 				return 1;
   1060   1.1    bouyer 			}
   1061   1.1    bouyer 			/* send a message reject */
   1062   1.1    bouyer 			esiop_cmd->cmd_tables->msg_out[0] = MSG_MESSAGE_REJECT;
   1063   1.1    bouyer 			esiop_cmd->cmd_tables->t_msgout.count = htole32(1);
   1064   1.1    bouyer 			esiop_table_sync(esiop_cmd,
   1065   1.1    bouyer 			    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1066   1.1    bouyer 			CALL_SCRIPT(Ent_send_msgout);
   1067   1.1    bouyer 			return 1;
   1068   1.1    bouyer 		case A_int_disc:
   1069   1.1    bouyer 			INCSTAT(esiop_stat_intr_sdp);
   1070   1.1    bouyer 			offset = bus_space_read_1(sc->sc_c.sc_rt,
   1071   1.1    bouyer 			    sc->sc_c.sc_rh, SIOP_SCRATCHA + 1);
   1072   1.1    bouyer #ifdef SIOP_DEBUG_DR
   1073   1.1    bouyer 			printf("disconnect offset %d\n", offset);
   1074   1.1    bouyer #endif
   1075  1.29    bouyer 			siop_sdp(&esiop_cmd->cmd_c, offset);
   1076  1.34    bouyer 			/* we start again with no offset */
   1077  1.34    bouyer 			ESIOP_XFER(esiop_cmd, saved_offset) =
   1078  1.34    bouyer 			    htole32(SIOP_NOOFFSET);
   1079  1.29    bouyer 			esiop_table_sync(esiop_cmd,
   1080  1.29    bouyer 			    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1081   1.1    bouyer 			CALL_SCRIPT(Ent_script_sched);
   1082   1.1    bouyer 			return 1;
   1083   1.1    bouyer 		case A_int_resfail:
   1084   1.1    bouyer 			printf("reselect failed\n");
   1085   1.1    bouyer 			CALL_SCRIPT(Ent_script_sched);
   1086   1.9    bouyer 			return 1;
   1087   1.1    bouyer 		case A_int_done:
   1088   1.1    bouyer 			if (xs == NULL) {
   1089   1.1    bouyer 				printf("%s: done without command\n",
   1090  1.46   tsutsui 				    device_xname(sc->sc_c.sc_dev));
   1091   1.1    bouyer 				CALL_SCRIPT(Ent_script_sched);
   1092   1.1    bouyer 				return 1;
   1093   1.1    bouyer 			}
   1094   1.1    bouyer #ifdef SIOP_DEBUG_INTR
   1095   1.1    bouyer 			printf("done, DSA=0x%lx target id 0x%x last msg "
   1096   1.1    bouyer 			    "in=0x%x status=0x%x\n", (u_long)esiop_cmd->cmd_c.dsa,
   1097   1.1    bouyer 			    le32toh(esiop_cmd->cmd_tables->id),
   1098   1.1    bouyer 			    esiop_cmd->cmd_tables->msg_in[0],
   1099   1.1    bouyer 			    le32toh(esiop_cmd->cmd_tables->status));
   1100   1.1    bouyer #endif
   1101   1.1    bouyer 			INCSTAT(esiop_stat_intr_done);
   1102   1.1    bouyer 			esiop_cmd->cmd_c.status = CMDST_DONE;
   1103   1.1    bouyer 			goto end;
   1104   1.1    bouyer 		default:
   1105   1.1    bouyer 			printf("unknown irqcode %x\n", irqcode);
   1106   1.1    bouyer 			if (xs) {
   1107   1.1    bouyer 				xs->error = XS_SELTIMEOUT;
   1108  1.29    bouyer 				goto end;
   1109   1.1    bouyer 			}
   1110   1.1    bouyer 			goto reset;
   1111   1.1    bouyer 		}
   1112   1.1    bouyer 		return 1;
   1113   1.1    bouyer 	}
   1114   1.1    bouyer 	/* We just should't get there */
   1115   1.1    bouyer 	panic("siop_intr: I shouldn't be there !");
   1116   1.1    bouyer 
   1117   1.1    bouyer end:
   1118   1.1    bouyer 	/*
   1119   1.1    bouyer 	 * restart the script now if command completed properly
   1120   1.1    bouyer 	 * Otherwise wait for siop_scsicmd_end(), we may need to cleanup the
   1121   1.1    bouyer 	 * queue
   1122   1.1    bouyer 	 */
   1123   1.1    bouyer 	xs->status = le32toh(esiop_cmd->cmd_tables->status);
   1124   1.1    bouyer #ifdef SIOP_DEBUG_INTR
   1125   1.1    bouyer 	printf("esiop_intr end: status %d\n", xs->status);
   1126   1.1    bouyer #endif
   1127   1.2    bouyer 	if (tag >= 0)
   1128   1.2    bouyer 		esiop_lun->tactive[tag] = NULL;
   1129   1.2    bouyer 	else
   1130   1.2    bouyer 		esiop_lun->active = NULL;
   1131  1.29    bouyer 	offset = bus_space_read_1(sc->sc_c.sc_rt, sc->sc_c.sc_rh,
   1132  1.29    bouyer 	    SIOP_SCRATCHA + 1);
   1133  1.34    bouyer 	/*
   1134  1.34    bouyer 	 * if we got a disconnect between the last data phase
   1135  1.34    bouyer 	 * and the status phase, offset will be 0. In this
   1136  1.34    bouyer 	 * case, cmd_tables->saved_offset will have the proper value
   1137  1.34    bouyer 	 * if it got updated by the controller
   1138  1.34    bouyer 	 */
   1139  1.34    bouyer 	if (offset == 0 &&
   1140  1.34    bouyer 	    ESIOP_XFER(esiop_cmd, saved_offset) != htole32(SIOP_NOOFFSET))
   1141  1.34    bouyer 		offset =
   1142  1.34    bouyer 		    (le32toh(ESIOP_XFER(esiop_cmd, saved_offset)) >> 8) & 0xff;
   1143  1.34    bouyer 
   1144  1.29    bouyer 	esiop_scsicmd_end(esiop_cmd, offset);
   1145   1.1    bouyer 	if (freetarget && esiop_target->target_c.status == TARST_PROBING)
   1146   1.1    bouyer 		esiop_del_dev(sc, target, lun);
   1147   1.9    bouyer 	CALL_SCRIPT(Ent_script_sched);
   1148   1.9    bouyer 	return 1;
   1149   1.1    bouyer }
   1150   1.1    bouyer 
   1151   1.1    bouyer void
   1152  1.43       dsl esiop_scsicmd_end(struct esiop_cmd *esiop_cmd, int offset)
   1153   1.1    bouyer {
   1154   1.1    bouyer 	struct scsipi_xfer *xs = esiop_cmd->cmd_c.xs;
   1155   1.1    bouyer 	struct esiop_softc *sc = (struct esiop_softc *)esiop_cmd->cmd_c.siop_sc;
   1156  1.28    bouyer 
   1157  1.29    bouyer 	siop_update_resid(&esiop_cmd->cmd_c, offset);
   1158   1.1    bouyer 
   1159   1.1    bouyer 	switch(xs->status) {
   1160   1.1    bouyer 	case SCSI_OK:
   1161   1.1    bouyer 		xs->error = XS_NOERROR;
   1162   1.1    bouyer 		break;
   1163   1.1    bouyer 	case SCSI_BUSY:
   1164   1.1    bouyer 		xs->error = XS_BUSY;
   1165   1.1    bouyer 		break;
   1166   1.1    bouyer 	case SCSI_CHECK:
   1167   1.1    bouyer 		xs->error = XS_BUSY;
   1168   1.1    bouyer 		/* remove commands in the queue and scheduler */
   1169   1.1    bouyer 		esiop_unqueue(sc, xs->xs_periph->periph_target,
   1170   1.1    bouyer 		    xs->xs_periph->periph_lun);
   1171   1.1    bouyer 		break;
   1172   1.1    bouyer 	case SCSI_QUEUE_FULL:
   1173   1.1    bouyer 		INCSTAT(esiop_stat_intr_qfull);
   1174   1.1    bouyer #ifdef SIOP_DEBUG
   1175   1.1    bouyer 		printf("%s:%d:%d: queue full (tag %d)\n",
   1176  1.46   tsutsui 		    device_xname(sc->sc_c.sc_dev),
   1177   1.1    bouyer 		    xs->xs_periph->periph_target,
   1178   1.1    bouyer 		    xs->xs_periph->periph_lun, esiop_cmd->cmd_c.tag);
   1179   1.1    bouyer #endif
   1180   1.1    bouyer 		xs->error = XS_BUSY;
   1181   1.1    bouyer 		break;
   1182   1.1    bouyer 	case SCSI_SIOP_NOCHECK:
   1183   1.1    bouyer 		/*
   1184   1.1    bouyer 		 * don't check status, xs->error is already valid
   1185   1.1    bouyer 		 */
   1186   1.1    bouyer 		break;
   1187   1.1    bouyer 	case SCSI_SIOP_NOSTATUS:
   1188   1.1    bouyer 		/*
   1189   1.1    bouyer 		 * the status byte was not updated, cmd was
   1190   1.1    bouyer 		 * aborted
   1191   1.1    bouyer 		 */
   1192   1.1    bouyer 		xs->error = XS_SELTIMEOUT;
   1193   1.1    bouyer 		break;
   1194   1.1    bouyer 	default:
   1195  1.16    bouyer 		scsipi_printaddr(xs->xs_periph);
   1196  1.16    bouyer 		printf("invalid status code %d\n", xs->status);
   1197   1.1    bouyer 		xs->error = XS_DRIVER_STUFFUP;
   1198   1.1    bouyer 	}
   1199   1.1    bouyer 	if (xs->xs_control & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) {
   1200   1.1    bouyer 		bus_dmamap_sync(sc->sc_c.sc_dmat,
   1201   1.1    bouyer 		    esiop_cmd->cmd_c.dmamap_data, 0,
   1202   1.1    bouyer 		    esiop_cmd->cmd_c.dmamap_data->dm_mapsize,
   1203   1.1    bouyer 		    (xs->xs_control & XS_CTL_DATA_IN) ?
   1204   1.1    bouyer 		    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
   1205   1.1    bouyer 		bus_dmamap_unload(sc->sc_c.sc_dmat,
   1206   1.1    bouyer 		    esiop_cmd->cmd_c.dmamap_data);
   1207   1.1    bouyer 	}
   1208   1.1    bouyer 	bus_dmamap_unload(sc->sc_c.sc_dmat, esiop_cmd->cmd_c.dmamap_cmd);
   1209  1.40    martin 	if ((xs->xs_control & XS_CTL_POLL) == 0)
   1210  1.40    martin 		callout_stop(&xs->xs_callout);
   1211   1.1    bouyer 	esiop_cmd->cmd_c.status = CMDST_FREE;
   1212   1.1    bouyer 	TAILQ_INSERT_TAIL(&sc->free_list, esiop_cmd, next);
   1213  1.29    bouyer #if 0
   1214  1.29    bouyer 	if (xs->resid != 0)
   1215  1.29    bouyer 		printf("resid %d datalen %d\n", xs->resid, xs->datalen);
   1216  1.29    bouyer #endif
   1217   1.1    bouyer 	scsipi_done (xs);
   1218   1.1    bouyer }
   1219   1.1    bouyer 
   1220   1.1    bouyer void
   1221  1.43       dsl esiop_checkdone(struct esiop_softc *sc)
   1222   1.1    bouyer {
   1223   1.2    bouyer 	int target, lun, tag;
   1224   1.1    bouyer 	struct esiop_target *esiop_target;
   1225   1.1    bouyer 	struct esiop_lun *esiop_lun;
   1226   1.1    bouyer 	struct esiop_cmd *esiop_cmd;
   1227  1.11    bouyer 	u_int32_t slot;
   1228  1.11    bouyer 	int needsync = 0;
   1229   1.1    bouyer 	int status;
   1230  1.34    bouyer 	u_int32_t sem, offset;
   1231  1.12    bouyer 
   1232  1.12    bouyer 	esiop_script_sync(sc, BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   1233  1.12    bouyer 	sem = esiop_script_read(sc, sc->sc_semoffset);
   1234  1.12    bouyer 	esiop_script_write(sc, sc->sc_semoffset, sem & ~A_sem_done);
   1235  1.12    bouyer 	if ((sc->sc_flags & SCF_CHAN_NOSLOT) && (sem & A_sem_start)) {
   1236  1.12    bouyer 		/*
   1237  1.12    bouyer 		 * at last one command have been started,
   1238  1.12    bouyer 		 * so we should have free slots now
   1239  1.12    bouyer 		 */
   1240  1.12    bouyer 		sc->sc_flags &= ~SCF_CHAN_NOSLOT;
   1241  1.12    bouyer 		scsipi_channel_thaw(&sc->sc_c.sc_chan, 1);
   1242  1.12    bouyer 	}
   1243  1.12    bouyer 	esiop_script_sync(sc, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1244  1.12    bouyer 
   1245  1.12    bouyer 	if ((sem & A_sem_done) == 0) {
   1246  1.12    bouyer 		/* no pending done command */
   1247  1.12    bouyer 		return;
   1248  1.12    bouyer 	}
   1249   1.1    bouyer 
   1250  1.11    bouyer 	bus_dmamap_sync(sc->sc_c.sc_dmat, sc->sc_done_map,
   1251  1.11    bouyer 	    sc->sc_done_offset, A_ndone_slots * sizeof(u_int32_t),
   1252  1.11    bouyer 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   1253  1.11    bouyer next:
   1254  1.11    bouyer 	if (sc->sc_done_slot[sc->sc_currdoneslot] == 0) {
   1255  1.11    bouyer 		if (needsync)
   1256  1.11    bouyer 			bus_dmamap_sync(sc->sc_c.sc_dmat, sc->sc_done_map,
   1257  1.11    bouyer 			    sc->sc_done_offset,
   1258  1.11    bouyer 			    A_ndone_slots * sizeof(u_int32_t),
   1259  1.11    bouyer 			    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1260  1.11    bouyer 		return;
   1261  1.11    bouyer 	}
   1262  1.11    bouyer 
   1263  1.11    bouyer 	needsync = 1;
   1264  1.11    bouyer 
   1265  1.11    bouyer 	slot = htole32(sc->sc_done_slot[sc->sc_currdoneslot]);
   1266  1.11    bouyer 	sc->sc_done_slot[sc->sc_currdoneslot] = 0;
   1267  1.11    bouyer 	sc->sc_currdoneslot += 1;
   1268  1.11    bouyer 	if (sc->sc_currdoneslot == A_ndone_slots)
   1269  1.11    bouyer 		sc->sc_currdoneslot = 0;
   1270  1.11    bouyer 
   1271  1.11    bouyer 	target =  (slot & A_f_c_target) ? (slot >> 8) & 0xff : -1;
   1272  1.11    bouyer 	lun =  (slot & A_f_c_lun) ? (slot >> 16) & 0xff : -1;
   1273  1.11    bouyer 	tag =  (slot & A_f_c_tag) ? (slot >> 24) & 0xff : -1;
   1274  1.11    bouyer 
   1275  1.11    bouyer 	esiop_target = (target >= 0) ?
   1276  1.11    bouyer 	    (struct esiop_target *)sc->sc_c.targets[target] : NULL;
   1277  1.11    bouyer 	if (esiop_target == NULL) {
   1278  1.11    bouyer 		printf("esiop_target (target %d) not valid\n", target);
   1279  1.11    bouyer 		goto next;
   1280  1.11    bouyer 	}
   1281  1.11    bouyer 	esiop_lun = (lun >= 0) ? esiop_target->esiop_lun[lun] : NULL;
   1282  1.11    bouyer 	if (esiop_lun == NULL) {
   1283  1.11    bouyer 		printf("esiop_lun (target %d lun %d) not valid\n",
   1284  1.11    bouyer 		    target, lun);
   1285  1.11    bouyer 		goto next;
   1286  1.11    bouyer 	}
   1287  1.11    bouyer 	esiop_cmd = (tag >= 0) ? esiop_lun->tactive[tag] : esiop_lun->active;
   1288  1.11    bouyer 	if (esiop_cmd == NULL) {
   1289  1.11    bouyer 		printf("esiop_cmd (target %d lun %d tag %d) not valid\n",
   1290  1.11    bouyer 		    target, lun, tag);
   1291  1.11    bouyer 			goto next;
   1292  1.11    bouyer 	}
   1293  1.11    bouyer 
   1294  1.11    bouyer 	esiop_table_sync(esiop_cmd,
   1295  1.11    bouyer 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   1296  1.11    bouyer 	status = le32toh(esiop_cmd->cmd_tables->status);
   1297  1.11    bouyer #ifdef DIAGNOSTIC
   1298  1.11    bouyer 	if (status != SCSI_OK) {
   1299  1.11    bouyer 		printf("command for T/L/Q %d/%d/%d status %d\n",
   1300  1.11    bouyer 		    target, lun, tag, status);
   1301  1.11    bouyer 		goto next;
   1302   1.1    bouyer 	}
   1303  1.11    bouyer 
   1304  1.11    bouyer #endif
   1305  1.11    bouyer 	/* Ok, this command has been handled */
   1306  1.11    bouyer 	esiop_cmd->cmd_c.xs->status = status;
   1307  1.11    bouyer 	if (tag >= 0)
   1308  1.11    bouyer 		esiop_lun->tactive[tag] = NULL;
   1309  1.11    bouyer 	else
   1310  1.11    bouyer 		esiop_lun->active = NULL;
   1311  1.34    bouyer 	/*
   1312  1.34    bouyer 	 * scratcha was eventually saved in saved_offset by script.
   1313  1.34    bouyer 	 * fetch offset from it
   1314  1.34    bouyer 	 */
   1315  1.34    bouyer 	offset = 0;
   1316  1.34    bouyer 	if (ESIOP_XFER(esiop_cmd, saved_offset) != htole32(SIOP_NOOFFSET))
   1317  1.34    bouyer 		offset =
   1318  1.34    bouyer 		    (le32toh(ESIOP_XFER(esiop_cmd, saved_offset)) >> 8) & 0xff;
   1319  1.34    bouyer 	esiop_scsicmd_end(esiop_cmd, offset);
   1320  1.11    bouyer 	goto next;
   1321   1.1    bouyer }
   1322   1.1    bouyer 
   1323   1.1    bouyer void
   1324  1.43       dsl esiop_unqueue(struct esiop_softc *sc, int target, int lun)
   1325   1.1    bouyer {
   1326   1.1    bouyer  	int slot, tag;
   1327   1.2    bouyer 	u_int32_t slotdsa;
   1328   1.1    bouyer 	struct esiop_cmd *esiop_cmd;
   1329   1.1    bouyer 	struct esiop_lun *esiop_lun =
   1330   1.1    bouyer 	    ((struct esiop_target *)sc->sc_c.targets[target])->esiop_lun[lun];
   1331   1.1    bouyer 
   1332   1.1    bouyer 	/* first make sure to read valid data */
   1333   1.1    bouyer 	esiop_script_sync(sc, BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   1334   1.1    bouyer 
   1335   1.2    bouyer 	for (tag = 0; tag < ESIOP_NTAG; tag++) {
   1336   1.1    bouyer 		/* look for commands in the scheduler, not yet started */
   1337  1.11    bouyer 		if (esiop_lun->tactive[tag] == NULL)
   1338   1.1    bouyer 			continue;
   1339   1.2    bouyer 		esiop_cmd = esiop_lun->tactive[tag];
   1340   1.3    bouyer 		for (slot = 0; slot < A_ncmd_slots; slot++) {
   1341   1.2    bouyer 			slotdsa = esiop_script_read(sc,
   1342   1.8    bouyer 			    sc->sc_shedoffset + slot * CMD_SLOTSIZE);
   1343   1.9    bouyer 			/* if the slot has any flag, it won't match the DSA */
   1344   1.9    bouyer 			if (slotdsa == esiop_cmd->cmd_c.dsa) { /* found it */
   1345   1.9    bouyer 				/* Mark this slot as ignore */
   1346   1.9    bouyer 				esiop_script_write(sc,
   1347   1.9    bouyer 				    sc->sc_shedoffset + slot * CMD_SLOTSIZE,
   1348   1.9    bouyer 				    esiop_cmd->cmd_c.dsa | A_f_cmd_ignore);
   1349   1.9    bouyer 				/* ask to requeue */
   1350   1.9    bouyer 				esiop_cmd->cmd_c.xs->error = XS_REQUEUE;
   1351   1.9    bouyer 				esiop_cmd->cmd_c.xs->status = SCSI_SIOP_NOCHECK;
   1352   1.9    bouyer 				esiop_lun->tactive[tag] = NULL;
   1353  1.29    bouyer 				esiop_scsicmd_end(esiop_cmd, 0);
   1354   1.1    bouyer 				break;
   1355   1.9    bouyer 			}
   1356   1.1    bouyer 		}
   1357   1.1    bouyer 	}
   1358   1.9    bouyer 	esiop_script_sync(sc, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1359   1.1    bouyer }
   1360   1.1    bouyer 
   1361   1.1    bouyer /*
   1362   1.1    bouyer  * handle a rejected queue tag message: the command will run untagged,
   1363   1.1    bouyer  * has to adjust the reselect script.
   1364   1.1    bouyer  */
   1365   1.1    bouyer 
   1366   1.2    bouyer 
   1367   1.1    bouyer int
   1368  1.43       dsl esiop_handle_qtag_reject(struct esiop_cmd *esiop_cmd)
   1369   1.1    bouyer {
   1370   1.2    bouyer 	struct esiop_softc *sc = (struct esiop_softc *)esiop_cmd->cmd_c.siop_sc;
   1371   1.1    bouyer 	int target = esiop_cmd->cmd_c.xs->xs_periph->periph_target;
   1372   1.1    bouyer 	int lun = esiop_cmd->cmd_c.xs->xs_periph->periph_lun;
   1373   1.1    bouyer 	int tag = esiop_cmd->cmd_tables->msg_out[2];
   1374   1.2    bouyer 	struct esiop_target *esiop_target =
   1375   1.2    bouyer 	    (struct esiop_target*)sc->sc_c.targets[target];
   1376   1.2    bouyer 	struct esiop_lun *esiop_lun = esiop_target->esiop_lun[lun];
   1377   1.1    bouyer 
   1378   1.1    bouyer #ifdef SIOP_DEBUG
   1379   1.1    bouyer 	printf("%s:%d:%d: tag message %d (%d) rejected (status %d)\n",
   1380  1.46   tsutsui 	    device_xname(sc->sc_c.sc_dev), target, lun, tag, esiop_cmd->cmd_c.tag,
   1381   1.1    bouyer 	    esiop_cmd->cmd_c.status);
   1382   1.1    bouyer #endif
   1383   1.1    bouyer 
   1384   1.2    bouyer 	if (esiop_lun->active != NULL) {
   1385  1.46   tsutsui 		aprint_error_dev(sc->sc_c.sc_dev, "untagged command already running for target %d "
   1386  1.42    cegger 		    "lun %d (status %d)\n",
   1387   1.2    bouyer 		    target, lun, esiop_lun->active->cmd_c.status);
   1388   1.1    bouyer 		return -1;
   1389   1.1    bouyer 	}
   1390   1.1    bouyer 	/* clear tag slot */
   1391   1.2    bouyer 	esiop_lun->tactive[tag] = NULL;
   1392   1.1    bouyer 	/* add command to non-tagged slot */
   1393   1.2    bouyer 	esiop_lun->active = esiop_cmd;
   1394   1.2    bouyer 	esiop_cmd->cmd_c.flags &= ~CMDFL_TAG;
   1395   1.2    bouyer 	esiop_cmd->cmd_c.tag = -1;
   1396   1.2    bouyer 	/* update DSA table */
   1397   1.9    bouyer 	esiop_script_write(sc, esiop_target->lun_table_offset +
   1398   1.9    bouyer 	    lun * 2 + A_target_luntbl / sizeof(u_int32_t),
   1399   1.2    bouyer 	    esiop_cmd->cmd_c.dsa);
   1400   1.2    bouyer 	esiop_script_sync(sc, BUS_DMASYNC_PREREAD |  BUS_DMASYNC_PREWRITE);
   1401   1.1    bouyer 	return 0;
   1402   1.1    bouyer }
   1403   1.1    bouyer 
   1404   1.1    bouyer /*
   1405   1.1    bouyer  * handle a bus reset: reset chip, unqueue all active commands, free all
   1406  1.24       wiz  * target struct and report lossage to upper layer.
   1407   1.1    bouyer  * As the upper layer may requeue immediatly we have to first store
   1408   1.1    bouyer  * all active commands in a temporary queue.
   1409   1.1    bouyer  */
   1410   1.1    bouyer void
   1411  1.43       dsl esiop_handle_reset(struct esiop_softc *sc)
   1412   1.1    bouyer {
   1413   1.1    bouyer 	struct esiop_cmd *esiop_cmd;
   1414   1.1    bouyer 	struct esiop_lun *esiop_lun;
   1415   1.1    bouyer 	int target, lun, tag;
   1416   1.1    bouyer 	/*
   1417   1.1    bouyer 	 * scsi bus reset. reset the chip and restart
   1418   1.1    bouyer 	 * the queue. Need to clean up all active commands
   1419   1.1    bouyer 	 */
   1420  1.46   tsutsui 	printf("%s: scsi bus reset\n", device_xname(sc->sc_c.sc_dev));
   1421   1.1    bouyer 	/* stop, reset and restart the chip */
   1422   1.1    bouyer 	esiop_reset(sc);
   1423   1.9    bouyer 
   1424   1.1    bouyer 	if (sc->sc_flags & SCF_CHAN_NOSLOT) {
   1425   1.1    bouyer 		/* chip has been reset, all slots are free now */
   1426   1.1    bouyer 		sc->sc_flags &= ~SCF_CHAN_NOSLOT;
   1427   1.1    bouyer 		scsipi_channel_thaw(&sc->sc_c.sc_chan, 1);
   1428   1.1    bouyer 	}
   1429   1.1    bouyer 	/*
   1430  1.24       wiz 	 * Process all commands: first commands completes, then commands
   1431   1.1    bouyer 	 * being executed
   1432   1.1    bouyer 	 */
   1433   1.1    bouyer 	esiop_checkdone(sc);
   1434   1.1    bouyer 	for (target = 0; target < sc->sc_c.sc_chan.chan_ntargets;
   1435   1.1    bouyer 	    target++) {
   1436  1.11    bouyer 		struct esiop_target *esiop_target =
   1437   1.1    bouyer 		    (struct esiop_target *)sc->sc_c.targets[target];
   1438   1.1    bouyer 		if (esiop_target == NULL)
   1439   1.1    bouyer 			continue;
   1440   1.1    bouyer 		for (lun = 0; lun < 8; lun++) {
   1441   1.1    bouyer 			esiop_lun = esiop_target->esiop_lun[lun];
   1442   1.1    bouyer 			if (esiop_lun == NULL)
   1443   1.1    bouyer 				continue;
   1444   1.2    bouyer 			for (tag = -1; tag <
   1445   1.1    bouyer 			    ((sc->sc_c.targets[target]->flags & TARF_TAG) ?
   1446   1.2    bouyer 			    ESIOP_NTAG : 0);
   1447   1.1    bouyer 			    tag++) {
   1448   1.2    bouyer 				if (tag >= 0)
   1449   1.2    bouyer 					esiop_cmd = esiop_lun->tactive[tag];
   1450   1.2    bouyer 				else
   1451   1.2    bouyer 					esiop_cmd = esiop_lun->active;
   1452   1.1    bouyer 				if (esiop_cmd == NULL)
   1453   1.1    bouyer 					continue;
   1454   1.1    bouyer 				scsipi_printaddr(esiop_cmd->cmd_c.xs->xs_periph);
   1455   1.1    bouyer 				printf("command with tag id %d reset\n", tag);
   1456   1.1    bouyer 				esiop_cmd->cmd_c.xs->error =
   1457   1.1    bouyer 				    (esiop_cmd->cmd_c.flags & CMDFL_TIMEOUT) ?
   1458   1.1    bouyer 		    		    XS_TIMEOUT : XS_RESET;
   1459   1.1    bouyer 				esiop_cmd->cmd_c.xs->status = SCSI_SIOP_NOCHECK;
   1460   1.2    bouyer 				if (tag >= 0)
   1461   1.2    bouyer 					esiop_lun->tactive[tag] = NULL;
   1462   1.2    bouyer 				else
   1463   1.2    bouyer 					esiop_lun->active = NULL;
   1464   1.1    bouyer 				esiop_cmd->cmd_c.status = CMDST_DONE;
   1465  1.29    bouyer 				esiop_scsicmd_end(esiop_cmd, 0);
   1466   1.1    bouyer 			}
   1467   1.1    bouyer 		}
   1468   1.1    bouyer 		sc->sc_c.targets[target]->status = TARST_ASYNC;
   1469   1.6    bouyer 		sc->sc_c.targets[target]->flags &= ~(TARF_ISWIDE | TARF_ISDT);
   1470   1.1    bouyer 		sc->sc_c.targets[target]->period =
   1471   1.1    bouyer 		    sc->sc_c.targets[target]->offset = 0;
   1472   1.1    bouyer 		siop_update_xfer_mode(&sc->sc_c, target);
   1473   1.1    bouyer 	}
   1474   1.1    bouyer 
   1475   1.1    bouyer 	scsipi_async_event(&sc->sc_c.sc_chan, ASYNC_EVENT_RESET, NULL);
   1476   1.1    bouyer }
   1477   1.1    bouyer 
   1478   1.1    bouyer void
   1479  1.43       dsl esiop_scsipi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req, void *arg)
   1480   1.1    bouyer {
   1481   1.1    bouyer 	struct scsipi_xfer *xs;
   1482   1.1    bouyer 	struct scsipi_periph *periph;
   1483  1.46   tsutsui 	struct esiop_softc *sc = device_private(chan->chan_adapter->adapt_dev);
   1484   1.1    bouyer 	struct esiop_cmd *esiop_cmd;
   1485   1.1    bouyer 	struct esiop_target *esiop_target;
   1486   1.1    bouyer 	int s, error, i;
   1487   1.1    bouyer 	int target;
   1488   1.1    bouyer 	int lun;
   1489   1.1    bouyer 
   1490   1.1    bouyer 	switch (req) {
   1491   1.1    bouyer 	case ADAPTER_REQ_RUN_XFER:
   1492   1.1    bouyer 		xs = arg;
   1493   1.1    bouyer 		periph = xs->xs_periph;
   1494   1.1    bouyer 		target = periph->periph_target;
   1495   1.1    bouyer 		lun = periph->periph_lun;
   1496   1.1    bouyer 
   1497   1.1    bouyer 		s = splbio();
   1498  1.12    bouyer 		/*
   1499  1.12    bouyer 		 * first check if there are pending complete commands.
   1500  1.12    bouyer 		 * this can free us some resources (in the rings for example).
   1501  1.12    bouyer 		 * we have to lock it to avoid recursion.
   1502  1.12    bouyer 		 */
   1503  1.12    bouyer 		if ((sc->sc_flags & SCF_CHAN_ADAPTREQ) == 0) {
   1504  1.12    bouyer 			sc->sc_flags |= SCF_CHAN_ADAPTREQ;
   1505  1.12    bouyer 			esiop_checkdone(sc);
   1506  1.12    bouyer 			sc->sc_flags &= ~SCF_CHAN_ADAPTREQ;
   1507  1.12    bouyer 		}
   1508   1.1    bouyer #ifdef SIOP_DEBUG_SCHED
   1509   1.9    bouyer 		printf("starting cmd for %d:%d tag %d(%d)\n", target, lun,
   1510   1.9    bouyer 		    xs->xs_tag_type, xs->xs_tag_id);
   1511   1.1    bouyer #endif
   1512   1.1    bouyer 		esiop_cmd = TAILQ_FIRST(&sc->free_list);
   1513   1.1    bouyer 		if (esiop_cmd == NULL) {
   1514   1.1    bouyer 			xs->error = XS_RESOURCE_SHORTAGE;
   1515   1.1    bouyer 			scsipi_done(xs);
   1516   1.1    bouyer 			splx(s);
   1517   1.1    bouyer 			return;
   1518   1.1    bouyer 		}
   1519   1.1    bouyer 		TAILQ_REMOVE(&sc->free_list, esiop_cmd, next);
   1520   1.1    bouyer #ifdef DIAGNOSTIC
   1521   1.1    bouyer 		if (esiop_cmd->cmd_c.status != CMDST_FREE)
   1522   1.1    bouyer 			panic("siop_scsicmd: new cmd not free");
   1523   1.1    bouyer #endif
   1524   1.1    bouyer 		esiop_target = (struct esiop_target*)sc->sc_c.targets[target];
   1525   1.1    bouyer 		if (esiop_target == NULL) {
   1526   1.1    bouyer #ifdef SIOP_DEBUG
   1527   1.1    bouyer 			printf("%s: alloc siop_target for target %d\n",
   1528  1.46   tsutsui 				device_xname(sc->sc_c.sc_dev), target);
   1529   1.1    bouyer #endif
   1530   1.1    bouyer 			sc->sc_c.targets[target] =
   1531   1.1    bouyer 			    malloc(sizeof(struct esiop_target),
   1532   1.1    bouyer 				M_DEVBUF, M_NOWAIT | M_ZERO);
   1533   1.1    bouyer 			if (sc->sc_c.targets[target] == NULL) {
   1534  1.46   tsutsui 				aprint_error_dev(sc->sc_c.sc_dev, "can't malloc memory for "
   1535  1.42    cegger 				    "target %d\n", target);
   1536   1.1    bouyer 				xs->error = XS_RESOURCE_SHORTAGE;
   1537   1.1    bouyer 				scsipi_done(xs);
   1538   1.1    bouyer 				splx(s);
   1539   1.1    bouyer 				return;
   1540   1.1    bouyer 			}
   1541   1.1    bouyer 			esiop_target =
   1542   1.1    bouyer 			    (struct esiop_target*)sc->sc_c.targets[target];
   1543   1.1    bouyer 			esiop_target->target_c.status = TARST_PROBING;
   1544   1.1    bouyer 			esiop_target->target_c.flags = 0;
   1545   1.1    bouyer 			esiop_target->target_c.id =
   1546   1.1    bouyer 			    sc->sc_c.clock_div << 24; /* scntl3 */
   1547   1.1    bouyer 			esiop_target->target_c.id |=  target << 16; /* id */
   1548   1.1    bouyer 			/* esiop_target->target_c.id |= 0x0 << 8; scxfer is 0 */
   1549   1.1    bouyer 
   1550   1.1    bouyer 			for (i=0; i < 8; i++)
   1551   1.1    bouyer 				esiop_target->esiop_lun[i] = NULL;
   1552   1.1    bouyer 			esiop_target_register(sc, target);
   1553   1.1    bouyer 		}
   1554   1.1    bouyer 		if (esiop_target->esiop_lun[lun] == NULL) {
   1555   1.1    bouyer 			esiop_target->esiop_lun[lun] =
   1556   1.1    bouyer 			    malloc(sizeof(struct esiop_lun), M_DEVBUF,
   1557   1.1    bouyer 			    M_NOWAIT|M_ZERO);
   1558   1.1    bouyer 			if (esiop_target->esiop_lun[lun] == NULL) {
   1559  1.46   tsutsui 				aprint_error_dev(sc->sc_c.sc_dev, "can't alloc esiop_lun for "
   1560   1.1    bouyer 				    "target %d lun %d\n",
   1561  1.42    cegger 				    target, lun);
   1562   1.1    bouyer 				xs->error = XS_RESOURCE_SHORTAGE;
   1563   1.1    bouyer 				scsipi_done(xs);
   1564   1.1    bouyer 				splx(s);
   1565   1.1    bouyer 				return;
   1566   1.1    bouyer 			}
   1567   1.1    bouyer 		}
   1568   1.1    bouyer 		esiop_cmd->cmd_c.siop_target = sc->sc_c.targets[target];
   1569   1.1    bouyer 		esiop_cmd->cmd_c.xs = xs;
   1570   1.1    bouyer 		esiop_cmd->cmd_c.flags = 0;
   1571   1.1    bouyer 		esiop_cmd->cmd_c.status = CMDST_READY;
   1572   1.1    bouyer 
   1573   1.1    bouyer 		/* load the DMA maps */
   1574   1.1    bouyer 		error = bus_dmamap_load(sc->sc_c.sc_dmat,
   1575   1.1    bouyer 		    esiop_cmd->cmd_c.dmamap_cmd,
   1576   1.1    bouyer 		    xs->cmd, xs->cmdlen, NULL, BUS_DMA_NOWAIT);
   1577   1.1    bouyer 		if (error) {
   1578  1.46   tsutsui 			aprint_error_dev(sc->sc_c.sc_dev, "unable to load cmd DMA map: %d\n",
   1579  1.42    cegger 			    error);
   1580   1.1    bouyer 			xs->error = XS_DRIVER_STUFFUP;
   1581   1.1    bouyer 			scsipi_done(xs);
   1582   1.1    bouyer 			splx(s);
   1583   1.1    bouyer 			return;
   1584   1.1    bouyer 		}
   1585   1.1    bouyer 		if (xs->xs_control & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) {
   1586   1.1    bouyer 			error = bus_dmamap_load(sc->sc_c.sc_dmat,
   1587   1.1    bouyer 			    esiop_cmd->cmd_c.dmamap_data, xs->data, xs->datalen,
   1588   1.1    bouyer 			    NULL, BUS_DMA_NOWAIT | BUS_DMA_STREAMING |
   1589   1.1    bouyer 			    ((xs->xs_control & XS_CTL_DATA_IN) ?
   1590   1.1    bouyer 			     BUS_DMA_READ : BUS_DMA_WRITE));
   1591   1.1    bouyer 			if (error) {
   1592  1.46   tsutsui 				aprint_error_dev(sc->sc_c.sc_dev, "unable to load cmd DMA map: %d",
   1593  1.42    cegger 				    error);
   1594   1.1    bouyer 				xs->error = XS_DRIVER_STUFFUP;
   1595   1.1    bouyer 				scsipi_done(xs);
   1596   1.1    bouyer 				bus_dmamap_unload(sc->sc_c.sc_dmat,
   1597   1.1    bouyer 				    esiop_cmd->cmd_c.dmamap_cmd);
   1598   1.1    bouyer 				splx(s);
   1599   1.1    bouyer 				return;
   1600   1.1    bouyer 			}
   1601   1.1    bouyer 			bus_dmamap_sync(sc->sc_c.sc_dmat,
   1602   1.1    bouyer 			    esiop_cmd->cmd_c.dmamap_data, 0,
   1603   1.1    bouyer 			    esiop_cmd->cmd_c.dmamap_data->dm_mapsize,
   1604   1.1    bouyer 			    (xs->xs_control & XS_CTL_DATA_IN) ?
   1605   1.1    bouyer 			    BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
   1606   1.1    bouyer 		}
   1607   1.1    bouyer 		bus_dmamap_sync(sc->sc_c.sc_dmat, esiop_cmd->cmd_c.dmamap_cmd,
   1608   1.1    bouyer 		    0, esiop_cmd->cmd_c.dmamap_cmd->dm_mapsize,
   1609   1.1    bouyer 		    BUS_DMASYNC_PREWRITE);
   1610   1.1    bouyer 
   1611   1.2    bouyer 		if (xs->xs_tag_type)
   1612   1.2    bouyer 			esiop_cmd->cmd_c.tag = xs->xs_tag_id;
   1613   1.2    bouyer 		else
   1614   1.2    bouyer 			esiop_cmd->cmd_c.tag = -1;
   1615   1.1    bouyer 		siop_setuptables(&esiop_cmd->cmd_c);
   1616  1.34    bouyer 		ESIOP_XFER(esiop_cmd, saved_offset) = htole32(SIOP_NOOFFSET);
   1617  1.34    bouyer 		ESIOP_XFER(esiop_cmd, tlq) = htole32(A_f_c_target | A_f_c_lun);
   1618  1.34    bouyer 		ESIOP_XFER(esiop_cmd, tlq) |=
   1619   1.2    bouyer 		    htole32((target << 8) | (lun << 16));
   1620   1.2    bouyer 		if (esiop_cmd->cmd_c.flags & CMDFL_TAG) {
   1621  1.34    bouyer 			ESIOP_XFER(esiop_cmd, tlq) |= htole32(A_f_c_tag);
   1622  1.34    bouyer 			ESIOP_XFER(esiop_cmd, tlq) |=
   1623   1.2    bouyer 			    htole32(esiop_cmd->cmd_c.tag << 24);
   1624   1.2    bouyer 		}
   1625   1.1    bouyer 
   1626   1.1    bouyer 		esiop_table_sync(esiop_cmd,
   1627   1.1    bouyer 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1628   1.1    bouyer 		esiop_start(sc, esiop_cmd);
   1629   1.1    bouyer 		if (xs->xs_control & XS_CTL_POLL) {
   1630   1.1    bouyer 			/* poll for command completion */
   1631   1.1    bouyer 			while ((xs->xs_status & XS_STS_DONE) == 0) {
   1632   1.1    bouyer 				delay(1000);
   1633   1.1    bouyer 				esiop_intr(sc);
   1634   1.1    bouyer 			}
   1635   1.1    bouyer 		}
   1636   1.1    bouyer 		splx(s);
   1637   1.1    bouyer 		return;
   1638   1.1    bouyer 
   1639   1.1    bouyer 	case ADAPTER_REQ_GROW_RESOURCES:
   1640   1.1    bouyer #ifdef SIOP_DEBUG
   1641  1.46   tsutsui 		printf("%s grow resources (%d)\n", device_xname(sc->sc_c.sc_dev),
   1642   1.1    bouyer 		    sc->sc_c.sc_adapt.adapt_openings);
   1643   1.1    bouyer #endif
   1644   1.1    bouyer 		esiop_morecbd(sc);
   1645   1.1    bouyer 		return;
   1646   1.1    bouyer 
   1647   1.1    bouyer 	case ADAPTER_REQ_SET_XFER_MODE:
   1648   1.1    bouyer 	{
   1649   1.1    bouyer 		struct scsipi_xfer_mode *xm = arg;
   1650   1.1    bouyer 		if (sc->sc_c.targets[xm->xm_target] == NULL)
   1651   1.1    bouyer 			return;
   1652   1.1    bouyer 		s = splbio();
   1653  1.33    bouyer 		if (xm->xm_mode & PERIPH_CAP_TQING) {
   1654   1.1    bouyer 			sc->sc_c.targets[xm->xm_target]->flags |= TARF_TAG;
   1655   1.9    bouyer 			/* allocate tag tables for this device */
   1656   1.9    bouyer 			for (lun = 0;
   1657   1.9    bouyer 			    lun < sc->sc_c.sc_chan.chan_nluns; lun++) {
   1658  1.15   thorpej 				if (scsipi_lookup_periph(chan,
   1659  1.15   thorpej 				    xm->xm_target, lun) != NULL)
   1660   1.9    bouyer 					esiop_add_dev(sc, xm->xm_target, lun);
   1661   1.9    bouyer 			}
   1662   1.9    bouyer 		}
   1663   1.1    bouyer 		if ((xm->xm_mode & PERIPH_CAP_WIDE16) &&
   1664   1.1    bouyer 		    (sc->sc_c.features & SF_BUS_WIDE))
   1665   1.1    bouyer 			sc->sc_c.targets[xm->xm_target]->flags |= TARF_WIDE;
   1666   1.1    bouyer 		if (xm->xm_mode & PERIPH_CAP_SYNC)
   1667   1.1    bouyer 			sc->sc_c.targets[xm->xm_target]->flags |= TARF_SYNC;
   1668   1.6    bouyer 		if ((xm->xm_mode & PERIPH_CAP_DT) &&
   1669   1.6    bouyer 		    (sc->sc_c.features & SF_CHIP_DT))
   1670   1.6    bouyer 			sc->sc_c.targets[xm->xm_target]->flags |= TARF_DT;
   1671   1.6    bouyer 		if ((xm->xm_mode &
   1672   1.6    bouyer 		    (PERIPH_CAP_SYNC | PERIPH_CAP_WIDE16 | PERIPH_CAP_DT)) ||
   1673   1.1    bouyer 		    sc->sc_c.targets[xm->xm_target]->status == TARST_PROBING)
   1674   1.6    bouyer 			sc->sc_c.targets[xm->xm_target]->status = TARST_ASYNC;
   1675   1.1    bouyer 
   1676   1.1    bouyer 		splx(s);
   1677   1.1    bouyer 	}
   1678   1.1    bouyer 	}
   1679   1.1    bouyer }
   1680   1.1    bouyer 
   1681   1.1    bouyer static void
   1682  1.43       dsl esiop_start(struct esiop_softc *sc, struct esiop_cmd *esiop_cmd)
   1683   1.1    bouyer {
   1684   1.1    bouyer 	struct esiop_lun *esiop_lun;
   1685   1.1    bouyer 	struct esiop_target *esiop_target;
   1686   1.1    bouyer 	int timeout;
   1687   1.1    bouyer 	int target, lun, slot;
   1688   1.1    bouyer 
   1689   1.1    bouyer 	/*
   1690   1.1    bouyer 	 * first make sure to read valid data
   1691   1.1    bouyer 	 */
   1692   1.1    bouyer 	esiop_script_sync(sc, BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   1693   1.1    bouyer 
   1694   1.1    bouyer 	/*
   1695   1.1    bouyer 	 * We use a circular queue here. sc->sc_currschedslot points to a
   1696   1.1    bouyer 	 * free slot, unless we have filled the queue. Check this.
   1697   1.1    bouyer 	 */
   1698   1.1    bouyer 	slot = sc->sc_currschedslot;
   1699   1.8    bouyer 	if ((esiop_script_read(sc, sc->sc_shedoffset + slot * CMD_SLOTSIZE) &
   1700   1.1    bouyer 	    A_f_cmd_free) == 0) {
   1701   1.1    bouyer 		/*
   1702   1.1    bouyer 		 * no more free slot, no need to continue. freeze the queue
   1703   1.1    bouyer 		 * and requeue this command.
   1704   1.1    bouyer 		 */
   1705   1.1    bouyer 		scsipi_channel_freeze(&sc->sc_c.sc_chan, 1);
   1706   1.1    bouyer 		sc->sc_flags |= SCF_CHAN_NOSLOT;
   1707  1.12    bouyer 		esiop_script_write(sc, sc->sc_semoffset,
   1708  1.12    bouyer 		    esiop_script_read(sc, sc->sc_semoffset) & ~A_sem_start);
   1709  1.12    bouyer 		esiop_script_sync(sc,
   1710  1.31    bouyer 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1711   1.1    bouyer 		esiop_cmd->cmd_c.xs->error = XS_REQUEUE;
   1712   1.1    bouyer 		esiop_cmd->cmd_c.xs->status = SCSI_SIOP_NOCHECK;
   1713  1.29    bouyer 		esiop_scsicmd_end(esiop_cmd, 0);
   1714   1.1    bouyer 		return;
   1715   1.1    bouyer 	}
   1716   1.1    bouyer 	/* OK, we can use this slot */
   1717   1.1    bouyer 
   1718   1.1    bouyer 	target = esiop_cmd->cmd_c.xs->xs_periph->periph_target;
   1719   1.1    bouyer 	lun = esiop_cmd->cmd_c.xs->xs_periph->periph_lun;
   1720   1.1    bouyer 	esiop_target = (struct esiop_target*)sc->sc_c.targets[target];
   1721   1.1    bouyer 	esiop_lun = esiop_target->esiop_lun[lun];
   1722   1.1    bouyer 	/* if non-tagged command active, panic: this shouldn't happen */
   1723   1.1    bouyer 	if (esiop_lun->active != NULL) {
   1724   1.1    bouyer 		panic("esiop_start: tagged cmd while untagged running");
   1725   1.1    bouyer 	}
   1726   1.1    bouyer #ifdef DIAGNOSTIC
   1727   1.1    bouyer 	/* sanity check the tag if needed */
   1728   1.1    bouyer 	if (esiop_cmd->cmd_c.flags & CMDFL_TAG) {
   1729   1.2    bouyer 		if (esiop_cmd->cmd_c.tag >= ESIOP_NTAG ||
   1730   1.2    bouyer 		    esiop_cmd->cmd_c.tag < 0) {
   1731   1.1    bouyer 			scsipi_printaddr(esiop_cmd->cmd_c.xs->xs_periph);
   1732   1.1    bouyer 			printf(": tag id %d\n", esiop_cmd->cmd_c.tag);
   1733   1.1    bouyer 			panic("esiop_start: invalid tag id");
   1734   1.1    bouyer 		}
   1735  1.36  christos 		if (esiop_lun->tactive[esiop_cmd->cmd_c.tag] != NULL)
   1736  1.36  christos 			panic("esiop_start: tag not free");
   1737   1.1    bouyer 	}
   1738   1.1    bouyer #endif
   1739   1.1    bouyer #ifdef SIOP_DEBUG_SCHED
   1740   1.1    bouyer 	printf("using slot %d for DSA 0x%lx\n", slot,
   1741   1.1    bouyer 	    (u_long)esiop_cmd->cmd_c.dsa);
   1742   1.1    bouyer #endif
   1743   1.1    bouyer 	/* mark command as active */
   1744   1.1    bouyer 	if (esiop_cmd->cmd_c.status == CMDST_READY)
   1745   1.1    bouyer 		esiop_cmd->cmd_c.status = CMDST_ACTIVE;
   1746   1.1    bouyer 	else
   1747   1.1    bouyer 		panic("esiop_start: bad status");
   1748   1.9    bouyer 	/* DSA table for reselect */
   1749   1.2    bouyer 	if (esiop_cmd->cmd_c.flags & CMDFL_TAG) {
   1750   1.2    bouyer 		esiop_lun->tactive[esiop_cmd->cmd_c.tag] = esiop_cmd;
   1751   1.2    bouyer 		/* DSA table for reselect */
   1752  1.11    bouyer 		esiop_lun->lun_tagtbl->tbl[esiop_cmd->cmd_c.tag] =
   1753   1.2    bouyer 		    htole32(esiop_cmd->cmd_c.dsa);
   1754   1.2    bouyer 		bus_dmamap_sync(sc->sc_c.sc_dmat,
   1755   1.2    bouyer 		    esiop_lun->lun_tagtbl->tblblk->blkmap,
   1756   1.2    bouyer 		    esiop_lun->lun_tagtbl->tbl_offset,
   1757   1.2    bouyer 		    sizeof(u_int32_t) * ESIOP_NTAG, BUS_DMASYNC_PREWRITE);
   1758   1.2    bouyer 	} else {
   1759   1.2    bouyer 		esiop_lun->active = esiop_cmd;
   1760   1.9    bouyer 		esiop_script_write(sc,
   1761   1.9    bouyer 		    esiop_target->lun_table_offset +
   1762   1.9    bouyer 		    lun * 2 + A_target_luntbl / sizeof(u_int32_t),
   1763   1.2    bouyer 		    esiop_cmd->cmd_c.dsa);
   1764   1.2    bouyer 	}
   1765   1.8    bouyer 	/* scheduler slot: DSA */
   1766   1.8    bouyer 	esiop_script_write(sc, sc->sc_shedoffset + slot * CMD_SLOTSIZE,
   1767   1.1    bouyer 	    esiop_cmd->cmd_c.dsa);
   1768   1.9    bouyer 	/* make sure SCRIPT processor will read valid data */
   1769   1.9    bouyer 	esiop_script_sync(sc, BUS_DMASYNC_PREREAD |  BUS_DMASYNC_PREWRITE);
   1770   1.1    bouyer 	/* handle timeout */
   1771   1.1    bouyer 	if ((esiop_cmd->cmd_c.xs->xs_control & XS_CTL_POLL) == 0) {
   1772   1.1    bouyer 		/* start exire timer */
   1773   1.1    bouyer 		timeout = mstohz(esiop_cmd->cmd_c.xs->timeout);
   1774   1.1    bouyer 		if (timeout == 0)
   1775   1.1    bouyer 			timeout = 1;
   1776   1.1    bouyer 		callout_reset( &esiop_cmd->cmd_c.xs->xs_callout,
   1777   1.1    bouyer 		    timeout, esiop_timeout, esiop_cmd);
   1778   1.1    bouyer 	}
   1779   1.1    bouyer 	/* Signal script it has some work to do */
   1780   1.1    bouyer 	bus_space_write_1(sc->sc_c.sc_rt, sc->sc_c.sc_rh,
   1781   1.1    bouyer 	    SIOP_ISTAT, ISTAT_SIGP);
   1782   1.1    bouyer 	/* update the current slot, and wait for IRQ */
   1783   1.1    bouyer 	sc->sc_currschedslot++;
   1784   1.1    bouyer 	if (sc->sc_currschedslot >= A_ncmd_slots)
   1785   1.1    bouyer 		sc->sc_currschedslot = 0;
   1786   1.1    bouyer 	return;
   1787   1.1    bouyer }
   1788   1.1    bouyer 
   1789   1.1    bouyer void
   1790  1.43       dsl esiop_timeout(void *v)
   1791   1.1    bouyer {
   1792   1.1    bouyer 	struct esiop_cmd *esiop_cmd = v;
   1793   1.1    bouyer 	struct esiop_softc *sc =
   1794   1.1    bouyer 	    (struct esiop_softc *)esiop_cmd->cmd_c.siop_sc;
   1795   1.1    bouyer 	int s;
   1796   1.9    bouyer #ifdef SIOP_DEBUG
   1797   1.9    bouyer 	int slot, slotdsa;
   1798   1.9    bouyer #endif
   1799   1.1    bouyer 
   1800   1.9    bouyer 	s = splbio();
   1801   1.9    bouyer 	esiop_table_sync(esiop_cmd,
   1802   1.9    bouyer 	    BUS_DMASYNC_POSTREAD |
   1803   1.9    bouyer 	    BUS_DMASYNC_POSTWRITE);
   1804   1.1    bouyer 	scsipi_printaddr(esiop_cmd->cmd_c.xs->xs_periph);
   1805   1.9    bouyer #ifdef SIOP_DEBUG
   1806   1.9    bouyer 	printf("command timeout (status %d)\n", le32toh(esiop_cmd->cmd_tables->status));
   1807   1.9    bouyer 
   1808   1.9    bouyer 	esiop_script_sync(sc, BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   1809   1.9    bouyer 	for (slot = 0; slot < A_ncmd_slots; slot++) {
   1810   1.9    bouyer 		slotdsa = esiop_script_read(sc,
   1811   1.9    bouyer 		    sc->sc_shedoffset + slot * CMD_SLOTSIZE);
   1812   1.9    bouyer 		if ((slotdsa & 0x01) == 0)
   1813   1.9    bouyer 			printf("slot %d not free (0x%x)\n", slot, slotdsa);
   1814   1.9    bouyer 	}
   1815   1.9    bouyer 	printf("istat 0x%x ", bus_space_read_1(sc->sc_c.sc_rt, sc->sc_c.sc_rh, SIOP_ISTAT));
   1816   1.9    bouyer 	printf("DSP 0x%lx DSA 0x%x\n",
   1817   1.9    bouyer 	    (u_long)(bus_space_read_4(sc->sc_c.sc_rt, sc->sc_c.sc_rh, SIOP_DSP) - sc->sc_c.sc_scriptaddr),
   1818   1.9    bouyer 	    bus_space_read_4(sc->sc_c.sc_rt, sc->sc_c.sc_rh, SIOP_DSA));
   1819   1.9    bouyer 	bus_space_read_1(sc->sc_c.sc_rt, sc->sc_c.sc_rh, SIOP_CTEST2);
   1820   1.9    bouyer 	printf("istat 0x%x\n", bus_space_read_1(sc->sc_c.sc_rt, sc->sc_c.sc_rh, SIOP_ISTAT));
   1821   1.9    bouyer #else
   1822  1.27    bouyer 	printf("command timeout, CDB: ");
   1823  1.27    bouyer 	scsipi_print_cdb(esiop_cmd->cmd_c.xs->cmd);
   1824  1.26    bouyer 	printf("\n");
   1825   1.9    bouyer #endif
   1826   1.1    bouyer 	/* reset the scsi bus */
   1827   1.1    bouyer 	siop_resetbus(&sc->sc_c);
   1828   1.1    bouyer 
   1829   1.1    bouyer 	/* deactivate callout */
   1830   1.1    bouyer 	callout_stop(&esiop_cmd->cmd_c.xs->xs_callout);
   1831   1.1    bouyer 	/*
   1832   1.1    bouyer 	 * mark command has being timed out and just return;
   1833   1.1    bouyer 	 * the bus reset will generate an interrupt,
   1834   1.1    bouyer 	 * it will be handled in siop_intr()
   1835   1.1    bouyer 	 */
   1836   1.1    bouyer 	esiop_cmd->cmd_c.flags |= CMDFL_TIMEOUT;
   1837   1.1    bouyer 	splx(s);
   1838   1.1    bouyer 	return;
   1839   1.1    bouyer 
   1840   1.1    bouyer }
   1841   1.1    bouyer 
   1842   1.1    bouyer void
   1843  1.43       dsl esiop_dump_script(struct esiop_softc *sc)
   1844   1.1    bouyer {
   1845   1.1    bouyer 	int i;
   1846   1.1    bouyer 	for (i = 0; i < PAGE_SIZE / 4; i += 2) {
   1847   1.1    bouyer 		printf("0x%04x: 0x%08x 0x%08x", i * 4,
   1848   1.1    bouyer 		    le32toh(sc->sc_c.sc_script[i]),
   1849   1.1    bouyer 		    le32toh(sc->sc_c.sc_script[i+1]));
   1850   1.1    bouyer 		if ((le32toh(sc->sc_c.sc_script[i]) & 0xe0000000) ==
   1851   1.1    bouyer 		    0xc0000000) {
   1852   1.1    bouyer 			i++;
   1853   1.1    bouyer 			printf(" 0x%08x", le32toh(sc->sc_c.sc_script[i+1]));
   1854   1.1    bouyer 		}
   1855   1.1    bouyer 		printf("\n");
   1856   1.1    bouyer 	}
   1857   1.1    bouyer }
   1858   1.1    bouyer 
   1859   1.1    bouyer void
   1860  1.43       dsl esiop_morecbd(struct esiop_softc *sc)
   1861   1.1    bouyer {
   1862   1.1    bouyer 	int error, i, s;
   1863   1.1    bouyer 	bus_dma_segment_t seg;
   1864   1.1    bouyer 	int rseg;
   1865   1.1    bouyer 	struct esiop_cbd *newcbd;
   1866   1.1    bouyer 	struct esiop_xfer *xfer;
   1867   1.1    bouyer 	bus_addr_t dsa;
   1868   1.1    bouyer 
   1869   1.1    bouyer 	/* allocate a new list head */
   1870   1.1    bouyer 	newcbd = malloc(sizeof(struct esiop_cbd), M_DEVBUF, M_NOWAIT|M_ZERO);
   1871   1.1    bouyer 	if (newcbd == NULL) {
   1872  1.46   tsutsui 		aprint_error_dev(sc->sc_c.sc_dev, "can't allocate memory for command descriptors "
   1873  1.42    cegger 		    "head\n");
   1874   1.1    bouyer 		return;
   1875   1.1    bouyer 	}
   1876   1.1    bouyer 
   1877   1.1    bouyer 	/* allocate cmd list */
   1878   1.1    bouyer 	newcbd->cmds = malloc(sizeof(struct esiop_cmd) * SIOP_NCMDPB,
   1879   1.1    bouyer 	    M_DEVBUF, M_NOWAIT|M_ZERO);
   1880   1.1    bouyer 	if (newcbd->cmds == NULL) {
   1881  1.46   tsutsui 		aprint_error_dev(sc->sc_c.sc_dev, "can't allocate memory for command descriptors\n");
   1882   1.1    bouyer 		goto bad3;
   1883   1.1    bouyer 	}
   1884   1.1    bouyer 	error = bus_dmamem_alloc(sc->sc_c.sc_dmat, PAGE_SIZE, PAGE_SIZE, 0,
   1885   1.1    bouyer 	    &seg, 1, &rseg, BUS_DMA_NOWAIT);
   1886   1.1    bouyer 	if (error) {
   1887  1.46   tsutsui 		aprint_error_dev(sc->sc_c.sc_dev, "unable to allocate cbd DMA memory, error = %d\n",
   1888  1.42    cegger 		    error);
   1889   1.1    bouyer 		goto bad2;
   1890   1.1    bouyer 	}
   1891   1.1    bouyer 	error = bus_dmamem_map(sc->sc_c.sc_dmat, &seg, rseg, PAGE_SIZE,
   1892  1.39  christos 	    (void **)&newcbd->xfers, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
   1893   1.1    bouyer 	if (error) {
   1894  1.46   tsutsui 		aprint_error_dev(sc->sc_c.sc_dev, "unable to map cbd DMA memory, error = %d\n",
   1895  1.42    cegger 		    error);
   1896   1.1    bouyer 		goto bad2;
   1897   1.1    bouyer 	}
   1898   1.1    bouyer 	error = bus_dmamap_create(sc->sc_c.sc_dmat, PAGE_SIZE, 1, PAGE_SIZE, 0,
   1899   1.1    bouyer 	    BUS_DMA_NOWAIT, &newcbd->xferdma);
   1900   1.1    bouyer 	if (error) {
   1901  1.46   tsutsui 		aprint_error_dev(sc->sc_c.sc_dev, "unable to create cbd DMA map, error = %d\n", error);
   1902   1.1    bouyer 		goto bad1;
   1903   1.1    bouyer 	}
   1904   1.1    bouyer 	error = bus_dmamap_load(sc->sc_c.sc_dmat, newcbd->xferdma,
   1905   1.1    bouyer 	    newcbd->xfers, PAGE_SIZE, NULL, BUS_DMA_NOWAIT);
   1906   1.1    bouyer 	if (error) {
   1907  1.46   tsutsui 		aprint_error_dev(sc->sc_c.sc_dev, "unable to load cbd DMA map, error = %d\n", error);
   1908   1.1    bouyer 		goto bad0;
   1909   1.1    bouyer 	}
   1910   1.1    bouyer #ifdef DEBUG
   1911  1.46   tsutsui 	printf("%s: alloc newcdb at PHY addr 0x%lx\n", device_xname(sc->sc_c.sc_dev),
   1912   1.1    bouyer 	    (unsigned long)newcbd->xferdma->dm_segs[0].ds_addr);
   1913   1.1    bouyer #endif
   1914   1.1    bouyer 	for (i = 0; i < SIOP_NCMDPB; i++) {
   1915   1.1    bouyer 		error = bus_dmamap_create(sc->sc_c.sc_dmat, MAXPHYS, SIOP_NSG,
   1916   1.1    bouyer 		    MAXPHYS, 0, BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
   1917   1.1    bouyer 		    &newcbd->cmds[i].cmd_c.dmamap_data);
   1918   1.1    bouyer 		if (error) {
   1919  1.46   tsutsui 			aprint_error_dev(sc->sc_c.sc_dev, "unable to create data DMA map for cbd: "
   1920  1.42    cegger 			    "error %d\n", error);
   1921   1.1    bouyer 			goto bad0;
   1922   1.1    bouyer 		}
   1923   1.1    bouyer 		error = bus_dmamap_create(sc->sc_c.sc_dmat,
   1924   1.1    bouyer 		    sizeof(struct scsipi_generic), 1,
   1925   1.1    bouyer 		    sizeof(struct scsipi_generic), 0,
   1926   1.1    bouyer 		    BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
   1927   1.1    bouyer 		    &newcbd->cmds[i].cmd_c.dmamap_cmd);
   1928   1.1    bouyer 		if (error) {
   1929  1.46   tsutsui 			aprint_error_dev(sc->sc_c.sc_dev, "unable to create cmd DMA map for cbd %d\n", error);
   1930   1.1    bouyer 			goto bad0;
   1931   1.1    bouyer 		}
   1932   1.1    bouyer 		newcbd->cmds[i].cmd_c.siop_sc = &sc->sc_c;
   1933   1.1    bouyer 		newcbd->cmds[i].esiop_cbdp = newcbd;
   1934   1.1    bouyer 		xfer = &newcbd->xfers[i];
   1935   1.1    bouyer 		newcbd->cmds[i].cmd_tables = (struct siop_common_xfer *)xfer;
   1936   1.1    bouyer 		memset(newcbd->cmds[i].cmd_tables, 0,
   1937   1.1    bouyer 		    sizeof(struct esiop_xfer));
   1938   1.1    bouyer 		dsa = newcbd->xferdma->dm_segs[0].ds_addr +
   1939   1.1    bouyer 		    i * sizeof(struct esiop_xfer);
   1940   1.1    bouyer 		newcbd->cmds[i].cmd_c.dsa = dsa;
   1941   1.1    bouyer 		newcbd->cmds[i].cmd_c.status = CMDST_FREE;
   1942   1.1    bouyer 		xfer->siop_tables.t_msgout.count= htole32(1);
   1943   1.1    bouyer 		xfer->siop_tables.t_msgout.addr = htole32(dsa);
   1944   1.1    bouyer 		xfer->siop_tables.t_msgin.count= htole32(1);
   1945  1.11    bouyer 		xfer->siop_tables.t_msgin.addr = htole32(dsa +
   1946   1.5    bouyer 			offsetof(struct siop_common_xfer, msg_in));
   1947   1.1    bouyer 		xfer->siop_tables.t_extmsgin.count= htole32(2);
   1948   1.5    bouyer 		xfer->siop_tables.t_extmsgin.addr = htole32(dsa +
   1949   1.5    bouyer 			offsetof(struct siop_common_xfer, msg_in) + 1);
   1950   1.5    bouyer 		xfer->siop_tables.t_extmsgdata.addr = htole32(dsa +
   1951   1.5    bouyer 			offsetof(struct siop_common_xfer, msg_in) + 3);
   1952   1.1    bouyer 		xfer->siop_tables.t_status.count= htole32(1);
   1953   1.5    bouyer 		xfer->siop_tables.t_status.addr = htole32(dsa +
   1954   1.5    bouyer 			offsetof(struct siop_common_xfer, status));
   1955   1.1    bouyer 
   1956   1.1    bouyer 		s = splbio();
   1957   1.1    bouyer 		TAILQ_INSERT_TAIL(&sc->free_list, &newcbd->cmds[i], next);
   1958   1.1    bouyer 		splx(s);
   1959   1.1    bouyer #ifdef SIOP_DEBUG
   1960  1.39  christos 		printf("tables[%d]: in=0x%x out=0x%x status=0x%x\n", i,
   1961   1.1    bouyer 		    le32toh(newcbd->cmds[i].cmd_tables->t_msgin.addr),
   1962   1.1    bouyer 		    le32toh(newcbd->cmds[i].cmd_tables->t_msgout.addr),
   1963  1.37   garbled 		    le32toh(newcbd->cmds[i].cmd_tables->t_status.addr));
   1964   1.1    bouyer #endif
   1965   1.1    bouyer 	}
   1966   1.1    bouyer 	s = splbio();
   1967   1.1    bouyer 	TAILQ_INSERT_TAIL(&sc->cmds, newcbd, next);
   1968   1.1    bouyer 	sc->sc_c.sc_adapt.adapt_openings += SIOP_NCMDPB;
   1969   1.1    bouyer 	splx(s);
   1970   1.1    bouyer 	return;
   1971   1.1    bouyer bad0:
   1972   1.1    bouyer 	bus_dmamap_unload(sc->sc_c.sc_dmat, newcbd->xferdma);
   1973   1.1    bouyer 	bus_dmamap_destroy(sc->sc_c.sc_dmat, newcbd->xferdma);
   1974   1.1    bouyer bad1:
   1975   1.1    bouyer 	bus_dmamem_free(sc->sc_c.sc_dmat, &seg, rseg);
   1976   1.1    bouyer bad2:
   1977   1.1    bouyer 	free(newcbd->cmds, M_DEVBUF);
   1978   1.1    bouyer bad3:
   1979   1.1    bouyer 	free(newcbd, M_DEVBUF);
   1980   1.1    bouyer 	return;
   1981   1.1    bouyer }
   1982   1.1    bouyer 
   1983   1.1    bouyer void
   1984  1.43       dsl esiop_moretagtbl(struct esiop_softc *sc)
   1985   1.2    bouyer {
   1986   1.2    bouyer 	int error, i, j, s;
   1987   1.2    bouyer 	bus_dma_segment_t seg;
   1988   1.2    bouyer 	int rseg;
   1989   1.2    bouyer 	struct esiop_dsatblblk *newtblblk;
   1990   1.2    bouyer 	struct esiop_dsatbl *newtbls;
   1991   1.2    bouyer 	u_int32_t *tbls;
   1992   1.2    bouyer 
   1993   1.2    bouyer 	/* allocate a new list head */
   1994   1.2    bouyer 	newtblblk = malloc(sizeof(struct esiop_dsatblblk),
   1995   1.2    bouyer 	    M_DEVBUF, M_NOWAIT|M_ZERO);
   1996   1.2    bouyer 	if (newtblblk == NULL) {
   1997  1.46   tsutsui 		aprint_error_dev(sc->sc_c.sc_dev, "can't allocate memory for tag DSA table block\n");
   1998   1.2    bouyer 		return;
   1999   1.2    bouyer 	}
   2000   1.2    bouyer 
   2001   1.2    bouyer 	/* allocate tbl list */
   2002   1.2    bouyer 	newtbls = malloc(sizeof(struct esiop_dsatbl) * ESIOP_NTPB,
   2003   1.2    bouyer 	    M_DEVBUF, M_NOWAIT|M_ZERO);
   2004   1.2    bouyer 	if (newtbls == NULL) {
   2005  1.46   tsutsui 		aprint_error_dev(sc->sc_c.sc_dev, "can't allocate memory for command descriptors\n");
   2006   1.2    bouyer 		goto bad3;
   2007   1.2    bouyer 	}
   2008   1.2    bouyer 	error = bus_dmamem_alloc(sc->sc_c.sc_dmat, PAGE_SIZE, PAGE_SIZE, 0,
   2009   1.2    bouyer 	    &seg, 1, &rseg, BUS_DMA_NOWAIT);
   2010   1.2    bouyer 	if (error) {
   2011  1.46   tsutsui 		aprint_error_dev(sc->sc_c.sc_dev, "unable to allocate tbl DMA memory, error = %d\n", error);
   2012   1.2    bouyer 		goto bad2;
   2013   1.2    bouyer 	}
   2014   1.2    bouyer 	error = bus_dmamem_map(sc->sc_c.sc_dmat, &seg, rseg, PAGE_SIZE,
   2015  1.20  drochner 	    (void *)&tbls, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
   2016   1.2    bouyer 	if (error) {
   2017  1.46   tsutsui 		aprint_error_dev(sc->sc_c.sc_dev, "unable to map tbls DMA memory, error = %d\n", error);
   2018   1.2    bouyer 		goto bad2;
   2019   1.2    bouyer 	}
   2020   1.2    bouyer 	error = bus_dmamap_create(sc->sc_c.sc_dmat, PAGE_SIZE, 1, PAGE_SIZE, 0,
   2021   1.2    bouyer 	    BUS_DMA_NOWAIT, &newtblblk->blkmap);
   2022   1.2    bouyer 	if (error) {
   2023  1.46   tsutsui 		aprint_error_dev(sc->sc_c.sc_dev, "unable to create tbl DMA map, error = %d\n", error);
   2024   1.2    bouyer 		goto bad1;
   2025   1.2    bouyer 	}
   2026   1.2    bouyer 	error = bus_dmamap_load(sc->sc_c.sc_dmat, newtblblk->blkmap,
   2027   1.2    bouyer 	    tbls, PAGE_SIZE, NULL, BUS_DMA_NOWAIT);
   2028   1.2    bouyer 	if (error) {
   2029  1.46   tsutsui 		aprint_error_dev(sc->sc_c.sc_dev, "unable to load tbl DMA map, error = %d\n", error);
   2030   1.2    bouyer 		goto bad0;
   2031   1.2    bouyer 	}
   2032   1.2    bouyer #ifdef DEBUG
   2033   1.2    bouyer 	printf("%s: alloc new tag DSA table at PHY addr 0x%lx\n",
   2034  1.46   tsutsui 	    device_xname(sc->sc_c.sc_dev),
   2035   1.2    bouyer 	    (unsigned long)newtblblk->blkmap->dm_segs[0].ds_addr);
   2036   1.2    bouyer #endif
   2037   1.2    bouyer 	for (i = 0; i < ESIOP_NTPB; i++) {
   2038   1.2    bouyer 		newtbls[i].tblblk = newtblblk;
   2039   1.2    bouyer 		newtbls[i].tbl = &tbls[i * ESIOP_NTAG];
   2040   1.2    bouyer 		newtbls[i].tbl_offset = i * ESIOP_NTAG * sizeof(u_int32_t);
   2041   1.2    bouyer 		newtbls[i].tbl_dsa = newtblblk->blkmap->dm_segs[0].ds_addr +
   2042   1.2    bouyer 		    newtbls[i].tbl_offset;
   2043   1.2    bouyer 		for (j = 0; j < ESIOP_NTAG; j++)
   2044   1.2    bouyer 			newtbls[i].tbl[j] = j;
   2045   1.2    bouyer 		s = splbio();
   2046   1.2    bouyer 		TAILQ_INSERT_TAIL(&sc->free_tagtbl, &newtbls[i], next);
   2047   1.2    bouyer 		splx(s);
   2048   1.2    bouyer 	}
   2049   1.2    bouyer 	s = splbio();
   2050   1.2    bouyer 	TAILQ_INSERT_TAIL(&sc->tag_tblblk, newtblblk, next);
   2051   1.2    bouyer 	splx(s);
   2052   1.2    bouyer 	return;
   2053   1.2    bouyer bad0:
   2054   1.2    bouyer 	bus_dmamap_unload(sc->sc_c.sc_dmat, newtblblk->blkmap);
   2055   1.2    bouyer 	bus_dmamap_destroy(sc->sc_c.sc_dmat, newtblblk->blkmap);
   2056   1.2    bouyer bad1:
   2057   1.2    bouyer 	bus_dmamem_free(sc->sc_c.sc_dmat, &seg, rseg);
   2058   1.2    bouyer bad2:
   2059   1.2    bouyer 	free(newtbls, M_DEVBUF);
   2060   1.2    bouyer bad3:
   2061   1.2    bouyer 	free(newtblblk, M_DEVBUF);
   2062   1.2    bouyer 	return;
   2063   1.1    bouyer }
   2064   1.1    bouyer 
   2065   1.1    bouyer void
   2066  1.44       dsl esiop_update_scntl3(struct esiop_softc *sc, struct siop_common_target *_siop_target)
   2067   1.9    bouyer {
   2068   1.9    bouyer 	struct esiop_target *esiop_target = (struct esiop_target *)_siop_target;
   2069   1.9    bouyer 	esiop_script_write(sc, esiop_target->lun_table_offset,
   2070   1.9    bouyer 	    esiop_target->target_c.id);
   2071  1.13    bouyer 	esiop_script_sync(sc, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   2072   1.9    bouyer }
   2073   1.9    bouyer 
   2074   1.9    bouyer void
   2075  1.43       dsl esiop_add_dev(struct esiop_softc *sc, int target, int lun)
   2076   1.1    bouyer {
   2077   1.1    bouyer 	struct esiop_target *esiop_target =
   2078   1.1    bouyer 	    (struct esiop_target *)sc->sc_c.targets[target];
   2079   1.1    bouyer 	struct esiop_lun *esiop_lun = esiop_target->esiop_lun[lun];
   2080   1.2    bouyer 
   2081  1.33    bouyer 	if (esiop_lun->lun_tagtbl != NULL)
   2082  1.33    bouyer 		return; /* already allocated */
   2083  1.33    bouyer 
   2084   1.9    bouyer 	/* we need a tag DSA table */
   2085   1.9    bouyer 	esiop_lun->lun_tagtbl= TAILQ_FIRST(&sc->free_tagtbl);
   2086   1.9    bouyer 	if (esiop_lun->lun_tagtbl == NULL) {
   2087   1.9    bouyer 		esiop_moretagtbl(sc);
   2088   1.2    bouyer 		esiop_lun->lun_tagtbl= TAILQ_FIRST(&sc->free_tagtbl);
   2089   1.2    bouyer 		if (esiop_lun->lun_tagtbl == NULL) {
   2090   1.9    bouyer 			/* no resources, run untagged */
   2091   1.9    bouyer 			esiop_target->target_c.flags &= ~TARF_TAG;
   2092   1.9    bouyer 			return;
   2093   1.1    bouyer 		}
   2094   1.1    bouyer 	}
   2095   1.9    bouyer 	TAILQ_REMOVE(&sc->free_tagtbl, esiop_lun->lun_tagtbl, next);
   2096   1.9    bouyer 	/* Update LUN DSA table */
   2097   1.9    bouyer 	esiop_script_write(sc, esiop_target->lun_table_offset +
   2098   1.9    bouyer 	   lun * 2 + A_target_luntbl_tag / sizeof(u_int32_t),
   2099   1.9    bouyer 	    esiop_lun->lun_tagtbl->tbl_dsa);
   2100   1.9    bouyer 	esiop_script_sync(sc, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   2101   1.1    bouyer }
   2102   1.1    bouyer 
   2103   1.1    bouyer void
   2104  1.43       dsl esiop_del_dev(struct esiop_softc *sc, int target, int lun)
   2105   1.1    bouyer {
   2106   1.1    bouyer 	struct esiop_target *esiop_target;
   2107   1.1    bouyer #ifdef SIOP_DEBUG
   2108   1.1    bouyer 		printf("%s:%d:%d: free lun sw entry\n",
   2109  1.46   tsutsui 		    device_xname(sc->sc_c.sc_dev), target, lun);
   2110   1.1    bouyer #endif
   2111   1.1    bouyer 	if (sc->sc_c.targets[target] == NULL)
   2112   1.1    bouyer 		return;
   2113   1.1    bouyer 	esiop_target = (struct esiop_target *)sc->sc_c.targets[target];
   2114   1.1    bouyer 	free(esiop_target->esiop_lun[lun], M_DEVBUF);
   2115   1.1    bouyer 	esiop_target->esiop_lun[lun] = NULL;
   2116   1.1    bouyer }
   2117   1.1    bouyer 
   2118   1.1    bouyer void
   2119  1.43       dsl esiop_target_register(struct esiop_softc *sc, u_int32_t target)
   2120   1.1    bouyer {
   2121   1.1    bouyer 	struct esiop_target *esiop_target =
   2122   1.1    bouyer 	    (struct esiop_target *)sc->sc_c.targets[target];
   2123   1.9    bouyer 	struct esiop_lun *esiop_lun;
   2124   1.9    bouyer 	int lun;
   2125   1.1    bouyer 
   2126   1.1    bouyer 	/* get a DSA table for this target */
   2127   1.1    bouyer 	esiop_target->lun_table_offset = sc->sc_free_offset;
   2128   1.9    bouyer 	sc->sc_free_offset += sc->sc_c.sc_chan.chan_nluns * 2 + 2;
   2129   1.1    bouyer #ifdef SIOP_DEBUG
   2130   1.1    bouyer 	printf("%s: lun table for target %d offset %d free offset %d\n",
   2131  1.46   tsutsui 	    device_xname(sc->sc_c.sc_dev), target, esiop_target->lun_table_offset,
   2132   1.1    bouyer 	    sc->sc_free_offset);
   2133   1.1    bouyer #endif
   2134   1.1    bouyer 	/* first 32 bytes are ID (for select) */
   2135   1.1    bouyer 	esiop_script_write(sc, esiop_target->lun_table_offset,
   2136   1.1    bouyer 	    esiop_target->target_c.id);
   2137   1.1    bouyer 	/* Record this table in the target DSA table */
   2138   1.1    bouyer 	esiop_script_write(sc,
   2139   1.1    bouyer 	    sc->sc_target_table_offset + target,
   2140   1.1    bouyer 	    (esiop_target->lun_table_offset * sizeof(u_int32_t)) +
   2141   1.1    bouyer 	    sc->sc_c.sc_scriptaddr);
   2142   1.9    bouyer 	/* if we have a tag table, register it */
   2143   1.9    bouyer 	for (lun = 0; lun < sc->sc_c.sc_chan.chan_nluns; lun++) {
   2144   1.9    bouyer 		esiop_lun = esiop_target->esiop_lun[lun];
   2145   1.9    bouyer 		if (esiop_lun == NULL)
   2146   1.9    bouyer 			continue;
   2147   1.9    bouyer 		if (esiop_lun->lun_tagtbl)
   2148   1.9    bouyer 			esiop_script_write(sc, esiop_target->lun_table_offset +
   2149   1.9    bouyer 			   lun * 2 + A_target_luntbl_tag / sizeof(u_int32_t),
   2150   1.9    bouyer 			    esiop_lun->lun_tagtbl->tbl_dsa);
   2151   1.9    bouyer 	}
   2152   1.1    bouyer 	esiop_script_sync(sc,
   2153   1.1    bouyer 	    BUS_DMASYNC_PREREAD |  BUS_DMASYNC_PREWRITE);
   2154   1.1    bouyer }
   2155   1.1    bouyer 
   2156   1.1    bouyer #ifdef SIOP_STATS
   2157   1.1    bouyer void
   2158  1.45    cegger esiop_printstats(void)
   2159   1.1    bouyer {
   2160   1.1    bouyer 	printf("esiop_stat_intr %d\n", esiop_stat_intr);
   2161   1.1    bouyer 	printf("esiop_stat_intr_shortxfer %d\n", esiop_stat_intr_shortxfer);
   2162   1.1    bouyer 	printf("esiop_stat_intr_xferdisc %d\n", esiop_stat_intr_xferdisc);
   2163   1.1    bouyer 	printf("esiop_stat_intr_sdp %d\n", esiop_stat_intr_sdp);
   2164   1.1    bouyer 	printf("esiop_stat_intr_done %d\n", esiop_stat_intr_done);
   2165   1.1    bouyer 	printf("esiop_stat_intr_lunresel %d\n", esiop_stat_intr_lunresel);
   2166   1.1    bouyer 	printf("esiop_stat_intr_qfull %d\n", esiop_stat_intr_qfull);
   2167   1.1    bouyer }
   2168   1.1    bouyer #endif
   2169