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