Home | History | Annotate | Line # | Download | only in dev
      1  1.71    andvar /*	$NetBSD: siop.c,v 1.71 2022/04/07 19:33:37 andvar Exp $ */
      2  1.17       cgd 
      3   1.1        mw /*
      4   1.1        mw  * Copyright (c) 1990 The Regents of the University of California.
      5   1.1        mw  * All rights reserved.
      6   1.1        mw  *
      7   1.1        mw  * This code is derived from software contributed to Berkeley by
      8   1.1        mw  * Van Jacobson of Lawrence Berkeley Laboratory.
      9   1.1        mw  *
     10   1.1        mw  * Redistribution and use in source and binary forms, with or without
     11   1.1        mw  * modification, are permitted provided that the following conditions
     12   1.1        mw  * are met:
     13   1.1        mw  * 1. Redistributions of source code must retain the above copyright
     14   1.1        mw  *    notice, this list of conditions and the following disclaimer.
     15   1.1        mw  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1        mw  *    notice, this list of conditions and the following disclaimer in the
     17   1.1        mw  *    documentation and/or other materials provided with the distribution.
     18  1.50       agc  * 3. Neither the name of the University nor the names of its contributors
     19  1.50       agc  *    may be used to endorse or promote products derived from this software
     20  1.50       agc  *    without specific prior written permission.
     21  1.50       agc  *
     22  1.50       agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  1.50       agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  1.50       agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  1.50       agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  1.50       agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  1.50       agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  1.50       agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  1.50       agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  1.50       agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  1.50       agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  1.50       agc  * SUCH DAMAGE.
     33  1.50       agc  *
     34  1.50       agc  *	@(#)siop.c	7.5 (Berkeley) 5/4/91
     35  1.50       agc  */
     36  1.50       agc 
     37  1.50       agc /*
     38  1.50       agc  * Copyright (c) 1994 Michael L. Hitch
     39  1.50       agc  *
     40  1.50       agc  * This code is derived from software contributed to Berkeley by
     41  1.50       agc  * Van Jacobson of Lawrence Berkeley Laboratory.
     42  1.50       agc  *
     43  1.50       agc  * Redistribution and use in source and binary forms, with or without
     44  1.50       agc  * modification, are permitted provided that the following conditions
     45  1.50       agc  * are met:
     46  1.50       agc  * 1. Redistributions of source code must retain the above copyright
     47  1.50       agc  *    notice, this list of conditions and the following disclaimer.
     48  1.50       agc  * 2. Redistributions in binary form must reproduce the above copyright
     49  1.50       agc  *    notice, this list of conditions and the following disclaimer in the
     50  1.50       agc  *    documentation and/or other materials provided with the distribution.
     51   1.1        mw  *
     52  1.51    mhitch  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     53  1.51    mhitch  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     54  1.51    mhitch  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     55  1.51    mhitch  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     56  1.51    mhitch  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     57  1.51    mhitch  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     58  1.51    mhitch  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     59  1.51    mhitch  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     60  1.51    mhitch  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     61  1.51    mhitch  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     62   1.1        mw  *
     63   1.1        mw  *	@(#)siop.c	7.5 (Berkeley) 5/4/91
     64   1.1        mw  */
     65   1.1        mw 
     66   1.1        mw /*
     67   1.1        mw  * AMIGA 53C710 scsi adaptor driver
     68   1.1        mw  */
     69  1.39  jonathan 
     70  1.39  jonathan #include "opt_ddb.h"
     71  1.46   aymeric 
     72  1.46   aymeric #include <sys/cdefs.h>
     73  1.71    andvar __KERNEL_RCSID(0, "$NetBSD: siop.c,v 1.71 2022/04/07 19:33:37 andvar Exp $");
     74   1.2    chopps 
     75   1.4    chopps #include <sys/param.h>
     76   1.4    chopps #include <sys/systm.h>
     77  1.61    mhitch #include <sys/callout.h>
     78  1.61    mhitch #include <sys/kernel.h>
     79   1.8    chopps #include <sys/device.h>
     80  1.20    chopps #include <sys/disklabel.h>
     81   1.4    chopps #include <sys/buf.h>
     82  1.28     chuck #include <sys/malloc.h>
     83  1.48   thorpej 
     84  1.37    bouyer #include <dev/scsipi/scsi_all.h>
     85  1.37    bouyer #include <dev/scsipi/scsipi_all.h>
     86  1.37    bouyer #include <dev/scsipi/scsiconf.h>
     87   1.8    chopps #include <machine/cpu.h>
     88  1.42        is #ifdef __m68k__
     89  1.42        is #include <m68k/cacheops.h>
     90  1.65       phx #else
     91  1.65       phx #define DCIAS(pa)
     92  1.42        is #endif
     93   1.8    chopps #include <amiga/amiga/custom.h>
     94  1.20    chopps #include <amiga/amiga/isr.h>
     95   1.8    chopps #include <amiga/dev/siopreg.h>
     96   1.4    chopps #include <amiga/dev/siopvar.h>
     97   1.1        mw 
     98   1.1        mw /*
     99   1.1        mw  * SCSI delays
    100   1.1        mw  * In u-seconds, primarily for state changes on the SPC.
    101   1.1        mw  */
    102  1.11    chopps #define	SCSI_CMD_WAIT	500000	/* wait per step of 'immediate' cmds */
    103  1.11    chopps #define	SCSI_DATA_WAIT	500000	/* wait per data in/out step */
    104  1.11    chopps #define	SCSI_INIT_WAIT	500000	/* wait per step (both) during init */
    105   1.1        mw 
    106  1.45   aymeric void siop_select(struct siop_softc *);
    107  1.52       jmc void siopabort(struct siop_softc *, siop_regmap_p, const char *);
    108  1.45   aymeric void sioperror(struct siop_softc *, siop_regmap_p, u_char);
    109  1.45   aymeric void siopstart(struct siop_softc *);
    110  1.45   aymeric int  siop_checkintr(struct siop_softc *, u_char, u_char, u_char, int *);
    111  1.45   aymeric void siopreset(struct siop_softc *);
    112  1.45   aymeric void siopsetdelay(int);
    113  1.45   aymeric void siop_scsidone(struct siop_acb *, int);
    114  1.61    mhitch void siop_timeout(void *);
    115  1.45   aymeric void siop_sched(struct siop_softc *);
    116  1.45   aymeric void siop_poll(struct siop_softc *, struct siop_acb *);
    117  1.45   aymeric void siopintr(struct siop_softc *);
    118  1.45   aymeric void scsi_period_to_siop(struct siop_softc *, int);
    119  1.45   aymeric void siop_start(struct siop_softc *, int, int, u_char *, int, u_char *, int);
    120  1.45   aymeric void siop_dump_acb(struct siop_acb *);
    121   1.1        mw 
    122   1.1        mw /* 53C710 script */
    123  1.23    chopps const
    124  1.18    chopps #include <amiga/dev/siop_script.out>
    125   1.1        mw 
    126   1.8    chopps /* default to not inhibit sync negotiation on any drive */
    127  1.14    chopps u_char siop_inhibit_sync[8] = { 0, 0, 0, 0, 0, 0, 0 }; /* initialize, so patchable */
    128  1.23    chopps u_char siop_allow_disc[8] = {3, 3, 3, 3, 3, 3, 3, 3};
    129   1.8    chopps int siop_no_dma = 0;
    130   1.8    chopps 
    131  1.18    chopps int siop_reset_delay = 250;	/* delay after reset, in milleseconds */
    132   1.1        mw 
    133   1.1        mw int siop_cmd_wait = SCSI_CMD_WAIT;
    134   1.1        mw int siop_data_wait = SCSI_DATA_WAIT;
    135   1.1        mw int siop_init_wait = SCSI_INIT_WAIT;
    136   1.1        mw 
    137  1.29    mhitch #ifdef DEBUG_SYNC
    138  1.18    chopps /*
    139  1.54     lukem  * sync period transfer lookup - only valid for 66 MHz clock
    140  1.18    chopps  */
    141   1.1        mw static struct {
    142  1.24    chopps 	unsigned char p;	/* period from sync request message */
    143  1.24    chopps 	unsigned char r;	/* siop_period << 4 | sbcl */
    144  1.24    chopps } sync_tab[] = {
    145  1.18    chopps 	{ 60/4, 0<<4 | 1},
    146  1.18    chopps 	{ 76/4, 1<<4 | 1},
    147  1.18    chopps 	{ 92/4, 2<<4 | 1},
    148  1.18    chopps 	{ 92/4, 0<<4 | 2},
    149  1.18    chopps 	{108/4, 3<<4 | 1},
    150  1.18    chopps 	{116/4, 1<<4 | 2},
    151  1.18    chopps 	{120/4, 4<<4 | 1},
    152  1.18    chopps 	{120/4, 0<<4 | 3},
    153  1.18    chopps 	{136/4, 5<<4 | 1},
    154  1.18    chopps 	{140/4, 2<<4 | 2},
    155  1.18    chopps 	{152/4, 6<<4 | 1},
    156  1.18    chopps 	{152/4, 1<<4 | 3},
    157  1.18    chopps 	{164/4, 3<<4 | 2},
    158  1.18    chopps 	{168/4, 7<<4 | 1},
    159  1.18    chopps 	{180/4, 2<<4 | 3},
    160  1.18    chopps 	{184/4, 4<<4 | 2},
    161  1.18    chopps 	{208/4, 5<<4 | 2},
    162  1.18    chopps 	{212/4, 3<<4 | 3},
    163  1.18    chopps 	{232/4, 6<<4 | 2},
    164  1.18    chopps 	{240/4, 4<<4 | 3},
    165  1.18    chopps 	{256/4, 7<<4 | 2},
    166  1.18    chopps 	{272/4, 5<<4 | 3},
    167  1.18    chopps 	{300/4, 6<<4 | 3},
    168  1.18    chopps 	{332/4, 7<<4 | 3}
    169   1.1        mw };
    170  1.24    chopps #endif
    171   1.1        mw 
    172   1.1        mw #ifdef DEBUG
    173   1.8    chopps /*
    174   1.8    chopps  *	0x01 - full debug
    175   1.8    chopps  *	0x02 - DMA chaining
    176   1.8    chopps  *	0x04 - siopintr
    177   1.8    chopps  *	0x08 - phase mismatch
    178  1.23    chopps  *	0x10 - <not used>
    179  1.15    chopps  *	0x20 - panic on unhandled exceptions
    180  1.23    chopps  *	0x100 - disconnect/reselect
    181   1.8    chopps  */
    182   1.8    chopps int	siop_debug = 0;
    183   1.8    chopps int	siopsync_debug = 0;
    184   1.8    chopps int	siopdma_hits = 0;
    185   1.8    chopps int	siopdma_misses = 0;
    186  1.14    chopps int	siopchain_ints = 0;
    187  1.18    chopps int	siopstarts = 0;
    188  1.18    chopps int	siopints = 0;
    189  1.18    chopps int	siopphmm = 0;
    190  1.23    chopps #define SIOP_TRACE_SIZE	128
    191  1.23    chopps #define SIOP_TRACE(a,b,c,d) \
    192  1.23    chopps 	siop_trbuf[siop_trix] = (a); \
    193  1.23    chopps 	siop_trbuf[siop_trix+1] = (b); \
    194  1.23    chopps 	siop_trbuf[siop_trix+2] = (c); \
    195  1.23    chopps 	siop_trbuf[siop_trix+3] = (d); \
    196  1.23    chopps 	siop_trix = (siop_trix + 4) & (SIOP_TRACE_SIZE - 1);
    197  1.23    chopps u_char	siop_trbuf[SIOP_TRACE_SIZE];
    198  1.23    chopps int	siop_trix;
    199  1.45   aymeric void siop_dump(struct siop_softc *);
    200  1.45   aymeric void siop_dump_trace(void);
    201  1.16    chopps #else
    202  1.23    chopps #define SIOP_TRACE(a,b,c,d)
    203   1.1        mw #endif
    204   1.8    chopps 
    205   1.8    chopps 
    206   1.8    chopps /*
    207   1.8    chopps  * default minphys routine for siop based controllers
    208   1.8    chopps  */
    209  1.22   mycroft void
    210  1.45   aymeric siop_minphys(struct buf *bp)
    211   1.8    chopps {
    212  1.22   mycroft 
    213   1.8    chopps 	/*
    214  1.22   mycroft 	 * No max transfer at this level.
    215   1.8    chopps 	 */
    216  1.22   mycroft 	minphys(bp);
    217   1.1        mw }
    218   1.1        mw 
    219   1.8    chopps /*
    220   1.8    chopps  * used by specific siop controller
    221   1.1        mw  *
    222   1.8    chopps  */
    223  1.44    bouyer void
    224  1.45   aymeric siop_scsipi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req,
    225  1.45   aymeric                     void *arg)
    226  1.44    bouyer {
    227  1.37    bouyer 	struct scsipi_xfer *xs;
    228  1.68  christos #ifdef DIAGNOSTIC
    229  1.44    bouyer 	struct scsipi_periph *periph;
    230  1.68  christos #endif
    231  1.23    chopps 	struct siop_acb *acb;
    232  1.67       chs 	struct siop_softc *sc = device_private(chan->chan_adapter->adapt_dev);
    233  1.30     veego 	int flags, s;
    234   1.8    chopps 
    235  1.44    bouyer 	switch (req) {
    236  1.44    bouyer 	case ADAPTER_REQ_RUN_XFER:
    237  1.44    bouyer 		xs = arg;
    238  1.68  christos #ifdef DIAGNOSTIC
    239  1.44    bouyer 		periph = xs->xs_periph;
    240  1.68  christos #endif
    241  1.44    bouyer 		flags = xs->xs_control;
    242  1.44    bouyer 
    243  1.44    bouyer 		/* XXXX ?? */
    244  1.44    bouyer 		if (flags & XS_CTL_DATA_UIO)
    245  1.44    bouyer 			panic("siop: scsi data uio requested");
    246  1.44    bouyer 
    247  1.44    bouyer 		/* XXXX ?? */
    248  1.44    bouyer 		if (sc->sc_nexus && flags & XS_CTL_POLL)
    249  1.44    bouyer /*			panic("siop_scsicmd: busy");*/
    250  1.44    bouyer 			printf("siop_scsicmd: busy\n");
    251  1.44    bouyer 
    252  1.44    bouyer 		s = splbio();
    253  1.44    bouyer 		acb = sc->free_list.tqh_first;
    254  1.44    bouyer 		if (acb) {
    255  1.44    bouyer 			TAILQ_REMOVE(&sc->free_list, acb, chain);
    256  1.44    bouyer 		}
    257  1.44    bouyer 		splx(s);
    258   1.8    chopps 
    259  1.44    bouyer #ifdef DIAGNOSTIC
    260  1.44    bouyer 		/*
    261  1.44    bouyer 		 * This should never happen as we track the resources
    262  1.44    bouyer 		 * in the mid-layer.
    263  1.44    bouyer 		 */
    264  1.44    bouyer 		if (acb == NULL) {
    265  1.44    bouyer 			scsipi_printaddr(periph);
    266  1.44    bouyer 			printf("unable to allocate acb\n");
    267  1.44    bouyer 			panic("siop_scsipi_request");
    268  1.44    bouyer 		}
    269  1.44    bouyer #endif
    270  1.44    bouyer 
    271  1.44    bouyer 		acb->flags = ACB_ACTIVE;
    272  1.44    bouyer 		acb->xs = xs;
    273  1.64    cegger 		memcpy(&acb->cmd, xs->cmd, xs->cmdlen);
    274  1.44    bouyer 		acb->clen = xs->cmdlen;
    275  1.44    bouyer 		acb->daddr = xs->data;
    276  1.44    bouyer 		acb->dleft = xs->datalen;
    277  1.23    chopps 
    278  1.44    bouyer 		s = splbio();
    279  1.44    bouyer 		TAILQ_INSERT_TAIL(&sc->ready_list, acb, chain);
    280   1.8    chopps 
    281  1.44    bouyer 		if (sc->sc_nexus == NULL)
    282  1.44    bouyer 			siop_sched(sc);
    283  1.23    chopps 
    284  1.44    bouyer 		splx(s);
    285  1.23    chopps 
    286  1.44    bouyer 		if (flags & XS_CTL_POLL || siop_no_dma)
    287  1.44    bouyer 			siop_poll(sc, acb);
    288  1.44    bouyer 		return;
    289  1.23    chopps 
    290  1.44    bouyer 	case ADAPTER_REQ_GROW_RESOURCES:
    291  1.44    bouyer 		return;
    292   1.8    chopps 
    293  1.44    bouyer 	case ADAPTER_REQ_SET_XFER_MODE:
    294  1.44    bouyer 		return;
    295  1.44    bouyer 	}
    296  1.23    chopps }
    297  1.23    chopps 
    298  1.44    bouyer void
    299  1.45   aymeric siop_poll(struct siop_softc *sc, struct siop_acb *acb)
    300  1.23    chopps {
    301  1.23    chopps 	siop_regmap_p rp = sc->sc_siopp;
    302  1.37    bouyer 	struct scsipi_xfer *xs = acb->xs;
    303  1.23    chopps 	int i;
    304  1.23    chopps 	int status;
    305  1.23    chopps 	u_char istat;
    306  1.23    chopps 	u_char dstat;
    307  1.23    chopps 	u_char sstat0;
    308  1.23    chopps 	int s;
    309  1.23    chopps 	int to;
    310   1.8    chopps 
    311  1.23    chopps 	s = splbio();
    312  1.23    chopps 	to = xs->timeout / 1000;
    313  1.23    chopps 	if (sc->nexus_list.tqh_first)
    314  1.35  christos 		printf("%s: siop_poll called with disconnected device\n",
    315  1.67       chs 		    device_xname(sc->sc_dev));
    316  1.23    chopps 	for (;;) {
    317  1.23    chopps 		/* use cmd_wait values? */
    318  1.23    chopps 		i = 50000;
    319  1.36        is 		/* XXX spl0(); */
    320  1.23    chopps 		while (((istat = rp->siop_istat) &
    321  1.23    chopps 		    (SIOP_ISTAT_SIP | SIOP_ISTAT_DIP)) == 0) {
    322  1.23    chopps 			if (--i <= 0) {
    323  1.23    chopps #ifdef DEBUG
    324  1.35  christos 				printf ("waiting: tgt %d cmd %02x sbcl %02x dsp %lx (+%lx) dcmd %lx ds %p timeout %d\n",
    325  1.44    bouyer 				    xs->xs_periph->periph_target, acb->cmd.opcode,
    326  1.23    chopps 				    rp->siop_sbcl, rp->siop_dsp,
    327  1.23    chopps 				    rp->siop_dsp - sc->sc_scriptspa,
    328  1.56    mhitch 				    *((volatile long *)&rp->siop_dcmd), &acb->ds, acb->xs->timeout);
    329  1.23    chopps #endif
    330  1.23    chopps 				i = 50000;
    331  1.23    chopps 				--to;
    332  1.23    chopps 				if (to <= 0) {
    333  1.23    chopps 					siopreset(sc);
    334  1.44    bouyer 					return;
    335  1.23    chopps 				}
    336  1.23    chopps 			}
    337  1.38    mhitch 			delay(20);
    338  1.23    chopps 		}
    339  1.23    chopps 		sstat0 = rp->siop_sstat0;
    340  1.23    chopps 		dstat = rp->siop_dstat;
    341  1.23    chopps 		if (siop_checkintr(sc, istat, dstat, sstat0, &status)) {
    342  1.23    chopps 			if (acb != sc->sc_nexus)
    343  1.35  christos 				printf("%s: siop_poll disconnected device completed\n",
    344  1.67       chs 				    device_xname(sc->sc_dev));
    345  1.23    chopps 			else if ((sc->sc_flags & SIOP_INTDEFER) == 0) {
    346  1.23    chopps 				sc->sc_flags &= ~SIOP_INTSOFF;
    347  1.23    chopps 				rp->siop_sien = sc->sc_sien;
    348  1.23    chopps 				rp->siop_dien = sc->sc_dien;
    349  1.23    chopps 			}
    350  1.23    chopps 			siop_scsidone(sc->sc_nexus, status);
    351  1.23    chopps 		}
    352  1.43   thorpej 
    353  1.43   thorpej 		if (xs->xs_status & XS_STS_DONE)
    354  1.23    chopps 			break;
    355  1.23    chopps 	}
    356  1.23    chopps 	splx(s);
    357   1.8    chopps }
    358   1.8    chopps 
    359   1.8    chopps /*
    360  1.23    chopps  * start next command that's ready
    361   1.1        mw  */
    362   1.1        mw void
    363  1.45   aymeric siop_sched(struct siop_softc *sc)
    364   1.8    chopps {
    365  1.44    bouyer 	struct scsipi_periph *periph;
    366  1.23    chopps 	struct siop_acb *acb;
    367  1.30     veego 	int i;
    368   1.8    chopps 
    369  1.23    chopps #ifdef DEBUG
    370  1.23    chopps 	if (sc->sc_nexus) {
    371  1.35  christos 		printf("%s: siop_sched- nexus %p/%d ready %p/%d\n",
    372  1.67       chs 		    device_xname(sc->sc_dev), sc->sc_nexus,
    373  1.44    bouyer 		    sc->sc_nexus->xs->xs_periph->periph_target,
    374  1.23    chopps 		    sc->ready_list.tqh_first,
    375  1.44    bouyer 		    sc->ready_list.tqh_first->xs->xs_periph->periph_target);
    376  1.23    chopps 		return;
    377  1.23    chopps 	}
    378  1.23    chopps #endif
    379  1.23    chopps 	for (acb = sc->ready_list.tqh_first; acb; acb = acb->chain.tqe_next) {
    380  1.44    bouyer 		periph = acb->xs->xs_periph;
    381  1.44    bouyer 		i = periph->periph_target;
    382  1.44    bouyer 		if(!(sc->sc_tinfo[i].lubusy & (1 << periph->periph_lun))) {
    383  1.23    chopps 			struct siop_tinfo *ti = &sc->sc_tinfo[i];
    384  1.23    chopps 
    385  1.23    chopps 			TAILQ_REMOVE(&sc->ready_list, acb, chain);
    386  1.23    chopps 			sc->sc_nexus = acb;
    387  1.44    bouyer 			periph = acb->xs->xs_periph;
    388  1.44    bouyer 			ti = &sc->sc_tinfo[periph->periph_target];
    389  1.44    bouyer 			ti->lubusy |= (1 << periph->periph_lun);
    390  1.18    chopps 			break;
    391  1.18    chopps 		}
    392  1.18    chopps 	}
    393  1.23    chopps 
    394  1.23    chopps 	if (acb == NULL) {
    395  1.23    chopps #ifdef DEBUGXXX
    396  1.35  christos 		printf("%s: siop_sched didn't find ready command\n",
    397  1.67       chs 		    device_xname(sc->sc_dev));
    398  1.18    chopps #endif
    399   1.8    chopps 		return;
    400  1.23    chopps 	}
    401  1.23    chopps 
    402  1.43   thorpej 	if (acb->xs->xs_control & XS_CTL_RESET)
    403  1.23    chopps 		siopreset(sc);
    404  1.22   mycroft 
    405  1.23    chopps #if 0
    406  1.37    bouyer 	acb->cmd.bytes[0] |= slp->scsipi_scsi.lun << 5;	/* XXXX */
    407  1.23    chopps #endif
    408  1.23    chopps 	++sc->sc_active;
    409  1.23    chopps 	siop_select(sc);
    410   1.8    chopps }
    411   1.8    chopps 
    412   1.8    chopps void
    413  1.45   aymeric siop_scsidone(struct siop_acb *acb, int stat)
    414   1.1        mw {
    415  1.37    bouyer 	struct scsipi_xfer *xs;
    416  1.44    bouyer 	struct scsipi_periph *periph;
    417  1.29    mhitch 	struct siop_softc *sc;
    418  1.30     veego 	int dosched = 0;
    419   1.8    chopps 
    420  1.32    mhitch 	if (acb == NULL || (xs = acb->xs) == NULL) {
    421   1.8    chopps #ifdef DIAGNOSTIC
    422  1.37    bouyer 		printf("siop_scsidone: NULL acb or scsipi_xfer\n");
    423  1.32    mhitch #if defined(DEBUG) && defined(DDB)
    424  1.29    mhitch 		Debugger();
    425  1.32    mhitch #endif
    426  1.32    mhitch #endif
    427  1.29    mhitch 		return;
    428  1.29    mhitch 	}
    429  1.61    mhitch 
    430  1.61    mhitch 	callout_stop(&xs->xs_callout);
    431  1.61    mhitch 
    432  1.44    bouyer 	periph = xs->xs_periph;
    433  1.67       chs 	sc = device_private(periph->periph_channel->chan_adapter->adapt_dev);
    434  1.45   aymeric 
    435   1.8    chopps 	xs->status = stat;
    436  1.44    bouyer 	xs->resid = 0;		/* XXXX */
    437   1.8    chopps 
    438  1.44    bouyer 	if (xs->error == XS_NOERROR) {
    439  1.44    bouyer 		if (stat == SCSI_CHECK || stat == SCSI_BUSY)
    440  1.44    bouyer 			xs->error = XS_BUSY;
    441  1.23    chopps 	}
    442   1.1        mw 
    443   1.8    chopps 	/*
    444  1.23    chopps 	 * Remove the ACB from whatever queue it's on.  We have to do a bit of
    445  1.23    chopps 	 * a hack to figure out which queue it's on.  Note that it is *not*
    446  1.23    chopps 	 * necessary to cdr down the ready queue, but we must cdr down the
    447  1.23    chopps 	 * nexus queue and see if it's there, so we can mark the unit as no
    448  1.23    chopps 	 * longer busy.  This code is sickening, but it works.
    449   1.8    chopps 	 */
    450  1.23    chopps 	if (acb == sc->sc_nexus) {
    451  1.23    chopps 		sc->sc_nexus = NULL;
    452  1.44    bouyer 		sc->sc_tinfo[periph->periph_target].lubusy &=
    453  1.44    bouyer 			~(1<<periph->periph_lun);
    454  1.23    chopps 		if (sc->ready_list.tqh_first)
    455  1.23    chopps 			dosched = 1;	/* start next command */
    456  1.23    chopps 		--sc->sc_active;
    457  1.23    chopps 		SIOP_TRACE('d','a',stat,0)
    458  1.23    chopps 	} else if (sc->ready_list.tqh_last == &acb->chain.tqe_next) {
    459  1.23    chopps 		TAILQ_REMOVE(&sc->ready_list, acb, chain);
    460  1.23    chopps 		SIOP_TRACE('d','r',stat,0)
    461   1.1        mw 	} else {
    462  1.23    chopps 		register struct siop_acb *acb2;
    463  1.23    chopps 		for (acb2 = sc->nexus_list.tqh_first; acb2;
    464  1.23    chopps 		    acb2 = acb2->chain.tqe_next)
    465  1.23    chopps 			if (acb2 == acb) {
    466  1.23    chopps 				TAILQ_REMOVE(&sc->nexus_list, acb, chain);
    467  1.44    bouyer 				sc->sc_tinfo[periph->periph_target].lubusy
    468  1.44    bouyer 					&= ~(1<<periph->periph_lun);
    469  1.23    chopps 				--sc->sc_active;
    470  1.23    chopps 				break;
    471  1.23    chopps 			}
    472  1.23    chopps 		if (acb2)
    473  1.23    chopps 			;
    474  1.23    chopps 		else if (acb->chain.tqe_next) {
    475  1.23    chopps 			TAILQ_REMOVE(&sc->ready_list, acb, chain);
    476  1.23    chopps 			--sc->sc_active;
    477  1.23    chopps 		} else {
    478  1.35  christos 			printf("%s: can't find matching acb\n",
    479  1.67       chs 			    device_xname(sc->sc_dev));
    480  1.23    chopps #ifdef DDB
    481  1.23    chopps /*			Debugger(); */
    482  1.23    chopps #endif
    483  1.23    chopps 		}
    484  1.23    chopps 		SIOP_TRACE('d','n',stat,0);
    485  1.23    chopps 	}
    486  1.23    chopps 	/* Put it on the free list. */
    487  1.23    chopps 	acb->flags = ACB_FREE;
    488  1.23    chopps 	TAILQ_INSERT_HEAD(&sc->free_list, acb, chain);
    489   1.8    chopps 
    490  1.44    bouyer 	sc->sc_tinfo[periph->periph_target].cmds++;
    491   1.1        mw 
    492  1.37    bouyer 	scsipi_done(xs);
    493   1.1        mw 
    494  1.23    chopps 	if (dosched && sc->sc_nexus == NULL)
    495  1.23    chopps 		siop_sched(sc);
    496   1.8    chopps }
    497   1.1        mw 
    498   1.8    chopps void
    499  1.52       jmc siopabort(register struct siop_softc *sc, siop_regmap_p rp, const char *where)
    500   1.1        mw {
    501  1.30     veego #ifdef fix_this
    502  1.18    chopps 	int i;
    503  1.30     veego #endif
    504   1.1        mw 
    505  1.35  christos 	printf ("%s: abort %s: dstat %02x, sstat0 %02x sbcl %02x\n",
    506  1.67       chs 	    device_xname(sc->sc_dev),
    507  1.20    chopps 	    where, rp->siop_dstat, rp->siop_sstat0, rp->siop_sbcl);
    508   1.1        mw 
    509  1.20    chopps 	if (sc->sc_active > 0) {
    510   1.1        mw #ifdef TODO
    511  1.20    chopps       SET_SBIC_cmd (rp, SBIC_CMD_ABORT);
    512  1.20    chopps       WAIT_CIP (rp);
    513   1.1        mw 
    514  1.20    chopps       GET_SBIC_asr (rp, asr);
    515   1.1        mw       if (asr & (SBIC_ASR_BSY|SBIC_ASR_LCI))
    516   1.1        mw         {
    517   1.1        mw           /* ok, get more drastic.. */
    518  1.22   mycroft 
    519  1.20    chopps 	  SET_SBIC_cmd (rp, SBIC_CMD_RESET);
    520  1.11    chopps 	  delay(25);
    521  1.20    chopps 	  SBIC_WAIT(rp, SBIC_ASR_INT, 0);
    522  1.20    chopps 	  GET_SBIC_csr (rp, csr);       /* clears interrupt also */
    523   1.1        mw 
    524   1.1        mw           return;
    525   1.1        mw         }
    526   1.1        mw 
    527   1.1        mw       do
    528   1.1        mw         {
    529  1.20    chopps           SBIC_WAIT (rp, SBIC_ASR_INT, 0);
    530  1.20    chopps           GET_SBIC_csr (rp, csr);
    531   1.1        mw         }
    532   1.1        mw       while ((csr != SBIC_CSR_DISC) && (csr != SBIC_CSR_DISC_1)
    533   1.1        mw 	      && (csr != SBIC_CSR_CMD_INVALID));
    534   1.1        mw #endif
    535   1.1        mw 
    536   1.1        mw 		/* lets just hope it worked.. */
    537  1.23    chopps #ifdef fix_this
    538  1.18    chopps 		for (i = 0; i < 2; ++i) {
    539  1.20    chopps 			if (sc->sc_iob[i].sc_xs && &sc->sc_iob[i] !=
    540  1.20    chopps 			    sc->sc_cur) {
    541  1.35  christos 				printf ("siopabort: cleanup!\n");
    542  1.20    chopps 				sc->sc_iob[i].sc_xs = NULL;
    543  1.18    chopps 			}
    544  1.18    chopps 		}
    545  1.23    chopps #endif	/* fix_this */
    546  1.23    chopps /*		sc->sc_active = 0; */
    547   1.8    chopps 	}
    548   1.1        mw }
    549   1.1        mw 
    550   1.1        mw void
    551  1.45   aymeric siopinitialize(struct siop_softc *sc)
    552  1.12    chopps {
    553  1.26       jtc 	int i;
    554  1.26       jtc 	u_int inhibit_sync;
    555  1.26       jtc 	extern u_long scsi_nosync;
    556  1.26       jtc 	extern int shift_nosync;
    557  1.26       jtc 
    558  1.12    chopps 	/*
    559  1.14    chopps 	 * Need to check that scripts is on a long word boundary
    560  1.23    chopps 	 * Also should verify that dev doesn't span non-contiguous
    561  1.14    chopps 	 * physical pages.
    562  1.12    chopps 	 */
    563  1.55  christos 	sc->sc_scriptspa = kvtop((void *)__UNCONST(scripts));
    564  1.28     chuck 
    565  1.28     chuck 	/*
    566  1.28     chuck 	 * malloc sc_acb to ensure that DS is on a long word boundary.
    567  1.28     chuck 	 */
    568  1.28     chuck 
    569  1.60    cegger 	sc->sc_acb = malloc(sizeof(struct siop_acb) * SIOP_NACB,
    570  1.70       chs 		M_DEVBUF, M_WAITOK);
    571  1.28     chuck 
    572  1.24    chopps 	sc->sc_tcp[1] = 1000 / sc->sc_clock_freq;
    573  1.24    chopps 	sc->sc_tcp[2] = 1500 / sc->sc_clock_freq;
    574  1.24    chopps 	sc->sc_tcp[3] = 2000 / sc->sc_clock_freq;
    575  1.24    chopps 	sc->sc_minsync = sc->sc_tcp[1];		/* in 4ns units */
    576  1.24    chopps 	if (sc->sc_minsync < 25)
    577  1.24    chopps 		sc->sc_minsync = 25;
    578  1.24    chopps 	if (sc->sc_clock_freq <= 25) {
    579  1.29    mhitch 		sc->sc_dcntl |= 0x80;		/* SCLK/1 */
    580  1.24    chopps 		sc->sc_tcp[0] = sc->sc_tcp[1];
    581  1.24    chopps 	} else if (sc->sc_clock_freq <= 37) {
    582  1.29    mhitch 		sc->sc_dcntl |= 0x40;		/* SCLK/1.5 */
    583  1.24    chopps 		sc->sc_tcp[0] = sc->sc_tcp[2];
    584  1.24    chopps 	} else if (sc->sc_clock_freq <= 50) {
    585  1.29    mhitch 		sc->sc_dcntl |= 0x00;		/* SCLK/2 */
    586  1.24    chopps 		sc->sc_tcp[0] = sc->sc_tcp[3];
    587  1.24    chopps 	} else {
    588  1.29    mhitch 		sc->sc_dcntl |= 0xc0;		/* SCLK/3 */
    589  1.24    chopps 		sc->sc_tcp[0] = 3000 / sc->sc_clock_freq;
    590  1.26       jtc 	}
    591  1.26       jtc 
    592  1.26       jtc 	if (scsi_nosync) {
    593  1.26       jtc 		inhibit_sync = (scsi_nosync >> shift_nosync) & 0xff;
    594  1.26       jtc 		shift_nosync += 8;
    595  1.26       jtc #ifdef DEBUG
    596  1.26       jtc 		if (inhibit_sync)
    597  1.35  christos 			printf("%s: Inhibiting synchronous transfer %02x\n",
    598  1.67       chs 				device_xname(sc->sc_dev), inhibit_sync);
    599  1.26       jtc #endif
    600  1.26       jtc 		for (i = 0; i < 8; ++i)
    601  1.26       jtc 			if (inhibit_sync & (1 << i))
    602  1.26       jtc 				siop_inhibit_sync[i] = 1;
    603  1.24    chopps 	}
    604  1.23    chopps 
    605  1.20    chopps 	siopreset (sc);
    606  1.12    chopps }
    607  1.12    chopps 
    608  1.12    chopps void
    609  1.61    mhitch siop_timeout(void *arg)
    610  1.61    mhitch {
    611  1.61    mhitch 	struct siop_acb *acb;
    612  1.61    mhitch 	struct scsipi_periph *periph;
    613  1.61    mhitch 	struct siop_softc *sc;
    614  1.61    mhitch 	int s;
    615  1.61    mhitch 
    616  1.61    mhitch 	acb = arg;
    617  1.61    mhitch 	periph = acb->xs->xs_periph;
    618  1.61    mhitch 	sc = device_private(periph->periph_channel->chan_adapter->adapt_dev);
    619  1.61    mhitch 	scsipi_printaddr(periph);
    620  1.61    mhitch 	printf("timed out\n");
    621  1.61    mhitch 
    622  1.61    mhitch 	s = splbio();
    623  1.61    mhitch 
    624  1.61    mhitch 	acb->xs->error = XS_TIMEOUT;
    625  1.61    mhitch 	siopreset(sc);
    626  1.61    mhitch 
    627  1.61    mhitch 	splx(s);
    628  1.61    mhitch }
    629  1.61    mhitch 
    630  1.61    mhitch void
    631  1.45   aymeric siopreset(struct siop_softc *sc)
    632   1.1        mw {
    633  1.20    chopps 	siop_regmap_p rp;
    634   1.1        mw 	u_int i, s;
    635  1.23    chopps 	u_char  dummy;
    636  1.23    chopps 	struct siop_acb *acb;
    637   1.1        mw 
    638  1.20    chopps 	rp = sc->sc_siopp;
    639   1.8    chopps 
    640  1.20    chopps 	if (sc->sc_flags & SIOP_ALIVE)
    641  1.20    chopps 		siopabort(sc, rp, "reset");
    642  1.22   mycroft 
    643  1.67       chs 	printf("%s: ", device_xname(sc->sc_dev));		/* XXXX */
    644   1.1        mw 
    645   1.1        mw 	s = splbio();
    646   1.1        mw 
    647   1.1        mw 	/*
    648   1.1        mw 	 * Reset the chip
    649   1.1        mw 	 * XXX - is this really needed?
    650   1.1        mw 	 */
    651  1.20    chopps 	rp->siop_istat |= SIOP_ISTAT_ABRT;	/* abort current script */
    652  1.20    chopps 	rp->siop_istat |= SIOP_ISTAT_RST;		/* reset chip */
    653  1.20    chopps 	rp->siop_istat &= ~SIOP_ISTAT_RST;
    654  1.18    chopps 	/*
    655  1.18    chopps 	 * Reset SCSI bus (do we really want this?)
    656  1.18    chopps 	 */
    657  1.23    chopps 	rp->siop_sien = 0;
    658  1.20    chopps 	rp->siop_scntl1 |= SIOP_SCNTL1_RST;
    659  1.18    chopps 	delay(1);
    660  1.20    chopps 	rp->siop_scntl1 &= ~SIOP_SCNTL1_RST;
    661   1.1        mw 
    662   1.1        mw 	/*
    663   1.1        mw 	 * Set up various chip parameters
    664   1.1        mw 	 */
    665  1.20    chopps 	rp->siop_scntl0 = SIOP_ARB_FULL | SIOP_SCNTL0_EPC | SIOP_SCNTL0_EPG;
    666  1.23    chopps 	rp->siop_scntl1 = SIOP_SCNTL1_ESR;
    667  1.24    chopps 	rp->siop_dcntl = sc->sc_dcntl;
    668  1.20    chopps 	rp->siop_dmode = 0x80;	/* burst length = 4 */
    669  1.20    chopps 	rp->siop_sien = 0x00;	/* don't enable interrupts yet */
    670  1.20    chopps 	rp->siop_dien = 0x00;	/* don't enable interrupts yet */
    671  1.44    bouyer 	rp->siop_scid = 1 << sc->sc_channel.chan_id;
    672  1.20    chopps 	rp->siop_dwt = 0x00;
    673  1.20    chopps 	rp->siop_ctest0 |= SIOP_CTEST0_BTD | SIOP_CTEST0_EAN;
    674  1.24    chopps 	rp->siop_ctest7 |= sc->sc_ctest7;
    675   1.1        mw 
    676   1.1        mw 	/* will need to re-negotiate sync xfers */
    677  1.62    cegger 	memset(&sc->sc_sync, 0, sizeof (sc->sc_sync));
    678   1.1        mw 
    679  1.20    chopps 	i = rp->siop_istat;
    680  1.18    chopps 	if (i & SIOP_ISTAT_SIP)
    681  1.20    chopps 		dummy = rp->siop_sstat0;
    682  1.18    chopps 	if (i & SIOP_ISTAT_DIP)
    683  1.20    chopps 		dummy = rp->siop_dstat;
    684  1.18    chopps 
    685  1.68  christos 	__USE(dummy);
    686   1.1        mw 	splx (s);
    687   1.1        mw 
    688  1.23    chopps 	delay (siop_reset_delay * 1000);
    689  1.44    bouyer 	printf("siop id %d reset V%d\n", sc->sc_channel.chan_id,
    690  1.23    chopps 	    rp->siop_ctest8 >> 4);
    691  1.23    chopps 
    692  1.23    chopps 	if ((sc->sc_flags & SIOP_ALIVE) == 0) {
    693  1.23    chopps 		TAILQ_INIT(&sc->ready_list);
    694  1.23    chopps 		TAILQ_INIT(&sc->nexus_list);
    695  1.23    chopps 		TAILQ_INIT(&sc->free_list);
    696  1.23    chopps 		sc->sc_nexus = NULL;
    697  1.23    chopps 		acb = sc->sc_acb;
    698  1.62    cegger 		memset(acb, 0, sizeof(struct siop_acb) * SIOP_NACB);
    699  1.28     chuck 		for (i = 0; i < SIOP_NACB; i++) {
    700  1.23    chopps 			TAILQ_INSERT_TAIL(&sc->free_list, acb, chain);
    701  1.23    chopps 			acb++;
    702  1.23    chopps 		}
    703  1.62    cegger 		memset(sc->sc_tinfo, 0, sizeof(sc->sc_tinfo));
    704  1.23    chopps 	} else {
    705  1.23    chopps 		if (sc->sc_nexus != NULL) {
    706  1.44    bouyer 			sc->sc_nexus->xs->error = XS_RESET;
    707  1.23    chopps 			siop_scsidone(sc->sc_nexus, sc->sc_nexus->stat[0]);
    708  1.23    chopps 		}
    709  1.30     veego 		while ((acb = sc->nexus_list.tqh_first) > 0) {
    710  1.44    bouyer 			acb->xs->error = XS_RESET;
    711  1.23    chopps 			siop_scsidone(acb, acb->stat[0]);
    712  1.23    chopps 		}
    713  1.23    chopps 	}
    714  1.23    chopps 
    715  1.20    chopps 	sc->sc_flags |= SIOP_ALIVE;
    716  1.23    chopps 	sc->sc_flags &= ~(SIOP_INTDEFER|SIOP_INTSOFF);
    717  1.23    chopps 	/* enable SCSI and DMA interrupts */
    718  1.23    chopps 	sc->sc_sien = SIOP_SIEN_M_A | SIOP_SIEN_STO | /*SIOP_SIEN_SEL |*/ SIOP_SIEN_SGE |
    719  1.23    chopps 	    SIOP_SIEN_UDC | SIOP_SIEN_RST | SIOP_SIEN_PAR;
    720  1.23    chopps 	sc->sc_dien = SIOP_DIEN_BF | SIOP_DIEN_ABRT | SIOP_DIEN_SIR |
    721  1.23    chopps 	    /*SIOP_DIEN_WTD |*/ SIOP_DIEN_IID;
    722  1.23    chopps 	rp->siop_sien = sc->sc_sien;
    723  1.23    chopps 	rp->siop_dien = sc->sc_dien;
    724   1.1        mw }
    725   1.1        mw 
    726   1.1        mw /*
    727   1.1        mw  * Setup Data Storage for 53C710 and start SCRIPTS processing
    728   1.1        mw  */
    729   1.1        mw 
    730   1.8    chopps void
    731  1.45   aymeric siop_start(struct siop_softc *sc, int target, int lun, u_char *cbuf, int clen,
    732  1.45   aymeric            u_char *buf, int len)
    733   1.1        mw {
    734  1.20    chopps 	siop_regmap_p rp = sc->sc_siopp;
    735   1.1        mw 	int nchain;
    736   1.1        mw 	int count, tcount;
    737   1.1        mw 	char *addr, *dmaend;
    738  1.23    chopps 	struct siop_acb *acb = sc->sc_nexus;
    739  1.31     veego #ifdef DEBUG
    740  1.31     veego 	int i;
    741  1.31     veego #endif
    742   1.1        mw 
    743  1.18    chopps #ifdef DEBUG
    744  1.23    chopps 	if (siop_debug & 0x100 && rp->siop_sbcl & SIOP_BSY) {
    745  1.35  christos 		printf ("ACK! siop was busy: rp %p script %p dsa %p active %ld\n",
    746  1.23    chopps 		    rp, &scripts, &acb->ds, sc->sc_active);
    747  1.35  christos 		printf ("istat %02x sfbr %02x lcrc %02x sien %02x dien %02x\n",
    748  1.23    chopps 		    rp->siop_istat, rp->siop_sfbr, rp->siop_lcrc,
    749  1.23    chopps 		    rp->siop_sien, rp->siop_dien);
    750  1.23    chopps #ifdef DDB
    751  1.18    chopps 		/*Debugger();*/
    752  1.23    chopps #endif
    753  1.18    chopps 	}
    754  1.18    chopps #endif
    755  1.23    chopps 	acb->msgout[0] = MSG_IDENTIFY | lun;
    756  1.23    chopps 	if (siop_allow_disc[target] & 2 ||
    757  1.23    chopps 	    (siop_allow_disc[target] && len == 0))
    758  1.23    chopps 		acb->msgout[0] = MSG_IDENTIFY_DR | lun;
    759  1.23    chopps 	acb->status = 0;
    760  1.23    chopps 	acb->stat[0] = -1;
    761  1.23    chopps 	acb->msg[0] = -1;
    762  1.24    chopps 	acb->ds.scsi_addr = (0x10000 << target) | (sc->sc_sync[target].sxfer << 8);
    763  1.23    chopps 	acb->ds.idlen = 1;
    764  1.23    chopps 	acb->ds.idbuf = (char *) kvtop(&acb->msgout[0]);
    765  1.23    chopps 	acb->ds.cmdlen = clen;
    766  1.23    chopps 	acb->ds.cmdbuf = (char *) kvtop(cbuf);
    767  1.23    chopps 	acb->ds.stslen = 1;
    768  1.23    chopps 	acb->ds.stsbuf = (char *) kvtop(&acb->stat[0]);
    769  1.23    chopps 	acb->ds.msglen = 1;
    770  1.23    chopps 	acb->ds.msgbuf = (char *) kvtop(&acb->msg[0]);
    771  1.23    chopps 	acb->msg[1] = -1;
    772  1.23    chopps 	acb->ds.msginlen = 1;
    773  1.23    chopps 	acb->ds.extmsglen = 1;
    774  1.23    chopps 	acb->ds.synmsglen = 3;
    775  1.23    chopps 	acb->ds.msginbuf = (char *) kvtop(&acb->msg[1]);
    776  1.23    chopps 	acb->ds.extmsgbuf = (char *) kvtop(&acb->msg[2]);
    777  1.23    chopps 	acb->ds.synmsgbuf = (char *) kvtop(&acb->msg[3]);
    778  1.62    cegger 	memset(&acb->ds.chain, 0, sizeof (acb->ds.chain));
    779   1.1        mw 
    780  1.40    mhitch 	/*
    781  1.40    mhitch 	 * Negotiate wide is the initial negotiation state;  since the 53c710
    782  1.40    mhitch 	 * doesn't do wide transfers, just begin the synchronous transfer
    783  1.71    andvar 	 * negotiation here.
    784  1.40    mhitch 	 */
    785  1.40    mhitch 	if (sc->sc_sync[target].state == NEG_WIDE) {
    786   1.8    chopps 		if (siop_inhibit_sync[target]) {
    787  1.40    mhitch 			sc->sc_sync[target].state = NEG_DONE;
    788  1.24    chopps 			sc->sc_sync[target].sbcl = 0;
    789  1.24    chopps 			sc->sc_sync[target].sxfer = 0;
    790   1.1        mw #ifdef DEBUG
    791   1.1        mw 			if (siopsync_debug)
    792  1.35  christos 				printf ("Forcing target %d asynchronous\n", target);
    793   1.1        mw #endif
    794   1.1        mw 		}
    795   1.1        mw 		else {
    796  1.23    chopps 			acb->msg[2] = -1;
    797  1.23    chopps 			acb->msgout[1] = MSG_EXT_MESSAGE;
    798  1.23    chopps 			acb->msgout[2] = 3;
    799  1.23    chopps 			acb->msgout[3] = MSG_SYNC_REQ;
    800  1.23    chopps #ifdef MAXTOR_SYNC_KLUDGE
    801  1.23    chopps 			acb->msgout[4] = 50 / 4;	/* ask for ridiculous period */
    802  1.23    chopps #else
    803  1.24    chopps 			acb->msgout[4] = sc->sc_minsync;
    804  1.23    chopps #endif
    805  1.23    chopps 			acb->msgout[5] = SIOP_MAX_OFFSET;
    806  1.23    chopps 			acb->ds.idlen = 6;
    807  1.40    mhitch 			sc->sc_sync[target].state = NEG_WAITS;
    808   1.1        mw #ifdef DEBUG
    809   1.1        mw 			if (siopsync_debug)
    810  1.35  christos 				printf ("Sending sync request to target %d\n", target);
    811   1.1        mw #endif
    812   1.1        mw 		}
    813   1.1        mw 	}
    814   1.1        mw 
    815   1.1        mw /*
    816  1.23    chopps  * Build physical DMA addresses for scatter/gather I/O
    817   1.1        mw  */
    818  1.23    chopps 	acb->iob_buf = buf;
    819  1.23    chopps 	acb->iob_len = len;
    820  1.23    chopps 	acb->iob_curbuf = acb->iob_curlen = 0;
    821   1.1        mw 	nchain = 0;
    822   1.1        mw 	count = len;
    823   1.1        mw 	addr = buf;
    824   1.1        mw 	dmaend = NULL;
    825   1.1        mw 	while (count > 0) {
    826  1.23    chopps 		acb->ds.chain[nchain].databuf = (char *) kvtop (addr);
    827  1.48   thorpej 		if (count < (tcount = PAGE_SIZE - ((int) addr & PGOFSET)))
    828   1.1        mw 			tcount = count;
    829  1.23    chopps 		acb->ds.chain[nchain].datalen = tcount;
    830   1.1        mw 		addr += tcount;
    831   1.1        mw 		count -= tcount;
    832  1.23    chopps 		if (acb->ds.chain[nchain].databuf == dmaend) {
    833  1.23    chopps 			dmaend += acb->ds.chain[nchain].datalen;
    834  1.23    chopps 			acb->ds.chain[nchain].datalen = 0;
    835  1.23    chopps 			acb->ds.chain[--nchain].datalen += tcount;
    836   1.1        mw #ifdef DEBUG
    837   1.1        mw 			++siopdma_hits;
    838   1.1        mw #endif
    839   1.1        mw 		}
    840   1.1        mw 		else {
    841  1.23    chopps 			dmaend = acb->ds.chain[nchain].databuf +
    842  1.23    chopps 			    acb->ds.chain[nchain].datalen;
    843  1.23    chopps 			acb->ds.chain[nchain].datalen = tcount;
    844   1.1        mw #ifdef DEBUG
    845  1.14    chopps 			if (nchain)	/* Don't count miss on first one */
    846  1.14    chopps 				++siopdma_misses;
    847   1.1        mw #endif
    848   1.1        mw 		}
    849   1.1        mw 		++nchain;
    850   1.1        mw 	}
    851   1.1        mw #ifdef DEBUG
    852   1.1        mw 	if (nchain != 1 && len != 0 && siop_debug & 3) {
    853  1.35  christos 		printf ("DMA chaining set: %d\n", nchain);
    854   1.1        mw 		for (i = 0; i < nchain; ++i) {
    855  1.35  christos 			printf ("  [%d] %8p %lx\n", i, acb->ds.chain[i].databuf,
    856  1.23    chopps 			    acb->ds.chain[i].datalen);
    857   1.1        mw 		}
    858   1.1        mw 	}
    859   1.1        mw #endif
    860   1.1        mw 
    861  1.23    chopps 	/* push data cache for all data the 53c710 needs to access */
    862  1.55  christos 	dma_cachectl ((void *)acb, sizeof (struct siop_acb));
    863  1.12    chopps 	dma_cachectl (cbuf, clen);
    864  1.12    chopps 	if (buf != NULL && len != 0)
    865  1.12    chopps 		dma_cachectl (buf, len);
    866  1.23    chopps #ifdef DEBUG
    867  1.23    chopps 	if (siop_debug & 0x100 && rp->siop_sbcl & SIOP_BSY) {
    868  1.35  christos 		printf ("ACK! siop was busy at start: rp %p script %p dsa %p active %ld\n",
    869  1.23    chopps 		    rp, &scripts, &acb->ds, sc->sc_active);
    870  1.23    chopps #ifdef DDB
    871  1.23    chopps 		/*Debugger();*/
    872  1.23    chopps #endif
    873  1.23    chopps 	}
    874  1.23    chopps #endif
    875  1.23    chopps 	if (sc->nexus_list.tqh_first == NULL) {
    876  1.61    mhitch 		callout_reset(&acb->xs->xs_callout,
    877  1.61    mhitch 		    mstohz(acb->xs->timeout) + 1, siop_timeout, acb);
    878  1.23    chopps 		if (rp->siop_istat & SIOP_ISTAT_CON)
    879  1.35  christos 			printf("%s: siop_select while connected?\n",
    880  1.67       chs 			    device_xname(sc->sc_dev));
    881  1.23    chopps 		rp->siop_temp = 0;
    882  1.24    chopps 		rp->siop_sbcl = sc->sc_sync[target].sbcl;
    883  1.55  christos 		rp->siop_dsa = kvtop((void *)&acb->ds);
    884  1.20    chopps 		rp->siop_dsp = sc->sc_scriptspa;
    885  1.23    chopps 		SIOP_TRACE('s',1,0,0)
    886  1.18    chopps 	} else {
    887  1.23    chopps 		if ((rp->siop_istat & SIOP_ISTAT_CON) == 0) {
    888  1.23    chopps 			rp->siop_istat = SIOP_ISTAT_SIGP;
    889  1.23    chopps 			SIOP_TRACE('s',2,0,0);
    890  1.23    chopps 		}
    891  1.23    chopps 		else {
    892  1.23    chopps 			SIOP_TRACE('s',3,rp->siop_istat,0);
    893  1.23    chopps 		}
    894  1.18    chopps 	}
    895  1.18    chopps #ifdef DEBUG
    896  1.18    chopps 	++siopstarts;
    897  1.18    chopps #endif
    898   1.1        mw }
    899   1.1        mw 
    900   1.1        mw /*
    901   1.1        mw  * Process a DMA or SCSI interrupt from the 53C710 SIOP
    902   1.1        mw  */
    903   1.1        mw 
    904   1.8    chopps int
    905  1.45   aymeric siop_checkintr(struct siop_softc *sc, u_char istat, u_char dstat,
    906  1.45   aymeric                u_char sstat0, int *status)
    907   1.1        mw {
    908  1.20    chopps 	siop_regmap_p rp = sc->sc_siopp;
    909  1.23    chopps 	struct siop_acb *acb = sc->sc_nexus;
    910  1.30     veego 	int	target = 0;
    911  1.18    chopps 	int	dfifo, dbc, sstat1;
    912   1.1        mw 
    913  1.20    chopps 	dfifo = rp->siop_dfifo;
    914  1.20    chopps 	dbc = rp->siop_dbc0;
    915  1.20    chopps 	sstat1 = rp->siop_sstat1;
    916  1.20    chopps 	rp->siop_ctest8 |= SIOP_CTEST8_CLF;
    917  1.23    chopps 	while ((rp->siop_ctest1 & SIOP_CTEST1_FMT) != SIOP_CTEST1_FMT)
    918   1.1        mw 		;
    919  1.20    chopps 	rp->siop_ctest8 &= ~SIOP_CTEST8_CLF;
    920   1.1        mw #ifdef DEBUG
    921  1.18    chopps 	++siopints;
    922  1.23    chopps #if 0
    923  1.18    chopps 	if (siop_debug & 0x100) {
    924  1.23    chopps 		DCIAS(&acb->stat[0]);	/* XXX */
    925  1.35  christos 		printf ("siopchkintr: istat %x dstat %x sstat0 %x dsps %x sbcl %x sts %x msg %x\n",
    926  1.23    chopps 		    istat, dstat, sstat0, rp->siop_dsps, rp->siop_sbcl, acb->stat[0], acb->msg[0]);
    927  1.35  christos 		printf ("sync msg in: %02x %02x %02x %02x %02x %02x\n",
    928  1.23    chopps 		    acb->msg[0], acb->msg[1], acb->msg[2],
    929  1.23    chopps 		    acb->msg[3], acb->msg[4], acb->msg[5]);
    930  1.23    chopps 	}
    931  1.23    chopps #endif
    932  1.23    chopps 	if (rp->siop_dsp && (rp->siop_dsp < sc->sc_scriptspa ||
    933  1.23    chopps 	    rp->siop_dsp >= sc->sc_scriptspa + sizeof(scripts))) {
    934  1.35  christos 		printf ("%s: dsp not within script dsp %lx scripts %lx:%lx",
    935  1.67       chs 		    device_xname(sc->sc_dev), rp->siop_dsp, sc->sc_scriptspa,
    936  1.23    chopps 		    sc->sc_scriptspa + sizeof(scripts));
    937  1.35  christos 		printf(" istat %x dstat %x sstat0 %x\n",
    938  1.23    chopps 		    istat, dstat, sstat0);
    939  1.32    mhitch #ifdef DDB
    940  1.23    chopps 		Debugger();
    941  1.32    mhitch #endif
    942   1.1        mw 	}
    943   1.1        mw #endif
    944  1.23    chopps 	SIOP_TRACE('i',dstat,istat,(istat&SIOP_ISTAT_DIP)?rp->siop_dsps&0xff:sstat0);
    945  1.20    chopps 	if (dstat & SIOP_DSTAT_SIR && rp->siop_dsps == 0xff00) {
    946   1.1        mw 		/* Normal completion status, or check condition */
    947  1.23    chopps #ifdef DEBUG
    948  1.55  christos 		if (rp->siop_dsa != kvtop((void *)&acb->ds)) {
    949  1.35  christos 			printf ("siop: invalid dsa: %lx %x\n", rp->siop_dsa,
    950  1.57        is 			    (unsigned)kvtop((void *)&acb->ds));
    951   1.1        mw 			panic("*** siop DSA invalid ***");
    952   1.1        mw 		}
    953  1.23    chopps #endif
    954  1.44    bouyer 		target = acb->xs->xs_periph->periph_target;
    955  1.40    mhitch 		if (sc->sc_sync[target].state == NEG_WAITS) {
    956  1.40    mhitch 			if (acb->msg[1] == 0xff)
    957  1.40    mhitch 				printf ("%s: target %d ignored sync request\n",
    958  1.67       chs 				    device_xname(sc->sc_dev), target);
    959  1.40    mhitch 			else if (acb->msg[1] == MSG_REJECT)
    960  1.40    mhitch 				printf ("%s: target %d rejected sync request\n",
    961  1.67       chs 				    device_xname(sc->sc_dev), target);
    962  1.40    mhitch 			else
    963  1.40    mhitch /* XXX - need to set sync transfer parameters */
    964  1.40    mhitch 				printf("%s: target %d (sync) %02x %02x %02x\n",
    965  1.67       chs 				    device_xname(sc->sc_dev), target, acb->msg[1],
    966  1.40    mhitch 				    acb->msg[2], acb->msg[3]);
    967  1.40    mhitch 			sc->sc_sync[target].state = NEG_DONE;
    968  1.40    mhitch 		}
    969  1.40    mhitch 		dma_cachectl(&acb->stat[0], 1);
    970  1.40    mhitch 		*status = acb->stat[0];
    971  1.40    mhitch #ifdef DEBUG
    972  1.40    mhitch 		if (rp->siop_sbcl & SIOP_BSY) {
    973  1.40    mhitch 			/*printf ("ACK! siop was busy at end: rp %x script %x dsa %x\n",
    974  1.40    mhitch 			    rp, &scripts, &acb->ds);*/
    975  1.40    mhitch #ifdef DDB
    976  1.40    mhitch 			/*Debugger();*/
    977  1.40    mhitch #endif
    978  1.40    mhitch 		}
    979  1.40    mhitch 		if (acb->msg[0] != 0x00)
    980  1.40    mhitch 			printf("%s: message was not COMMAND COMPLETE: %x\n",
    981  1.67       chs 			    device_xname(sc->sc_dev), acb->msg[0]);
    982  1.40    mhitch #endif
    983  1.40    mhitch 		if (sc->nexus_list.tqh_first)
    984  1.40    mhitch 			rp->siop_dcntl |= SIOP_DCNTL_STD;
    985  1.40    mhitch 		return 1;
    986  1.40    mhitch 	}
    987  1.40    mhitch 	if (dstat & SIOP_DSTAT_SIR && rp->siop_dsps == 0xff0b) {
    988  1.44    bouyer 		target = acb->xs->xs_periph->periph_target;
    989  1.40    mhitch 		if (acb->msg[1] == MSG_EXT_MESSAGE && acb->msg[2] == 3 &&
    990  1.40    mhitch 		    acb->msg[3] == MSG_SYNC_REQ) {
    991   1.1        mw #ifdef DEBUG
    992   1.1        mw 			if (siopsync_debug)
    993  1.35  christos 				printf ("sync msg in: %02x %02x %02x %02x %02x %02x\n",
    994  1.23    chopps 				    acb->msg[0], acb->msg[1], acb->msg[2],
    995  1.23    chopps 				    acb->msg[3], acb->msg[4], acb->msg[5]);
    996   1.1        mw #endif
    997  1.24    chopps 			sc->sc_sync[target].sxfer = 0;
    998  1.24    chopps 			sc->sc_sync[target].sbcl = 0;
    999  1.23    chopps 			if (acb->msg[2] == 3 &&
   1000  1.23    chopps 			    acb->msg[3] == MSG_SYNC_REQ &&
   1001  1.23    chopps 			    acb->msg[5] != 0) {
   1002  1.23    chopps #ifdef MAXTOR_KLUDGE
   1003  1.23    chopps 				/*
   1004  1.23    chopps 				 * Kludge for my Maxtor XT8580S
   1005  1.23    chopps 				 * It accepts whatever we request, even
   1006  1.23    chopps 				 * though it won't work.  So we ask for
   1007  1.23    chopps 				 * a short period than we can handle.  If
   1008  1.23    chopps 				 * the device says it can do it, use 208ns.
   1009  1.23    chopps 				 * If the device says it can do less than
   1010  1.23    chopps 				 * 100ns, then we limit it to 100ns.
   1011  1.23    chopps 				 */
   1012  1.23    chopps 				if (acb->msg[4] && acb->msg[4] < 100 / 4) {
   1013   1.8    chopps #ifdef DEBUG
   1014  1.35  christos 					printf ("%d: target %d wanted %dns period\n",
   1015  1.67       chs 					    device_xname(sc->sc_dev), target,
   1016  1.23    chopps 					    acb->msg[4] * 4);
   1017   1.8    chopps #endif
   1018  1.23    chopps 					if (acb->msg[4] == 50 / 4)
   1019  1.23    chopps 						acb->msg[4] = 208 / 4;
   1020   1.8    chopps 					else
   1021  1.23    chopps 						acb->msg[4] = 100 / 4;
   1022   1.8    chopps 				}
   1023  1.23    chopps #endif /* MAXTOR_KLUDGE */
   1024  1.35  christos 				printf ("%s: target %d now synchronous, period=%dns, offset=%d\n",
   1025  1.67       chs 				    device_xname(sc->sc_dev), target,
   1026  1.23    chopps 				    acb->msg[4] * 4, acb->msg[5]);
   1027  1.20    chopps 				scsi_period_to_siop (sc, target);
   1028   1.1        mw 			}
   1029  1.40    mhitch 			rp->siop_sxfer = sc->sc_sync[target].sxfer;
   1030  1.40    mhitch 			rp->siop_sbcl = sc->sc_sync[target].sbcl;
   1031  1.40    mhitch 			if (sc->sc_sync[target].state == NEG_WAITS) {
   1032  1.40    mhitch 				sc->sc_sync[target].state = NEG_DONE;
   1033  1.40    mhitch 				rp->siop_dsp = sc->sc_scriptspa + Ent_clear_ack;
   1034  1.40    mhitch 				return(0);
   1035  1.40    mhitch 			}
   1036  1.40    mhitch 			rp->siop_dcntl |= SIOP_DCNTL_STD;
   1037  1.40    mhitch 			sc->sc_sync[target].state = NEG_DONE;
   1038  1.40    mhitch 			return (0);
   1039   1.1        mw 		}
   1040  1.40    mhitch 		/* XXX - not SDTR message */
   1041   1.1        mw 	}
   1042   1.1        mw 	if (sstat0 & SIOP_SSTAT0_M_A) {		/* Phase mismatch */
   1043   1.1        mw #ifdef DEBUG
   1044  1.18    chopps 		++siopphmm;
   1045  1.23    chopps 		if (acb == NULL)
   1046  1.35  christos 			printf("%s: Phase mismatch with no active command?\n",
   1047  1.67       chs 			    device_xname(sc->sc_dev));
   1048  1.23    chopps #endif
   1049  1.23    chopps 		if (acb->iob_len) {
   1050  1.18    chopps 			int adjust;
   1051  1.18    chopps 			adjust = ((dfifo - (dbc & 0x7f)) & 0x7f);
   1052  1.18    chopps 			if (sstat1 & SIOP_SSTAT1_ORF)
   1053  1.18    chopps 				++adjust;
   1054  1.18    chopps 			if (sstat1 & SIOP_SSTAT1_OLF)
   1055  1.18    chopps 				++adjust;
   1056  1.52       jmc 			acb->iob_curlen =
   1057  1.52       jmc 			    *((long *)__UNVOLATILE(&rp->siop_dcmd)) & 0xffffff;
   1058  1.23    chopps 			acb->iob_curlen += adjust;
   1059  1.52       jmc 			acb->iob_curbuf =
   1060  1.52       jmc 			    *((long *)__UNVOLATILE(&rp->siop_dnad)) - adjust;
   1061  1.23    chopps #ifdef DEBUG
   1062  1.23    chopps 			if (siop_debug & 0x100) {
   1063  1.23    chopps 				int i;
   1064  1.35  christos 				printf ("Phase mismatch: curbuf %lx curlen %lx dfifo %x dbc %x sstat1 %x adjust %x sbcl %x starts %d acb %p\n",
   1065  1.23    chopps 				    acb->iob_curbuf, acb->iob_curlen, dfifo,
   1066  1.52       jmc 				    dbc, sstat1, adjust, rp->siop_sbcl,
   1067  1.52       jmc 				    siopstarts, acb);
   1068  1.23    chopps 				if (acb->ds.chain[1].datalen) {
   1069  1.23    chopps 					for (i = 0; acb->ds.chain[i].datalen; ++i)
   1070  1.35  christos 						printf("chain[%d] addr %p len %lx\n",
   1071  1.23    chopps 						    i, acb->ds.chain[i].databuf,
   1072  1.23    chopps 						    acb->ds.chain[i].datalen);
   1073  1.23    chopps 				}
   1074  1.23    chopps 			}
   1075  1.18    chopps #endif
   1076  1.55  christos 			dma_cachectl ((void *)acb, sizeof(*acb));
   1077  1.18    chopps 		}
   1078  1.23    chopps #ifdef DEBUG
   1079  1.23    chopps 		SIOP_TRACE('m',rp->siop_sbcl,(rp->siop_dsp>>8),rp->siop_dsp);
   1080   1.1        mw 		if (siop_debug & 9)
   1081  1.35  christos 			printf ("Phase mismatch: %x dsp +%lx dcmd %lx\n",
   1082  1.20    chopps 			    rp->siop_sbcl,
   1083  1.20    chopps 			    rp->siop_dsp - sc->sc_scriptspa,
   1084  1.56    mhitch 			    *((volatile long *)&rp->siop_dcmd));
   1085   1.1        mw #endif
   1086  1.23    chopps 		if ((rp->siop_sbcl & SIOP_REQ) == 0) {
   1087  1.35  christos 			printf ("Phase mismatch: REQ not asserted! %02x dsp %lx\n",
   1088  1.23    chopps 			    rp->siop_sbcl, rp->siop_dsp);
   1089  1.32    mhitch #if defined(DEBUG) && defined(DDB)
   1090  1.41        is 			/*Debugger(); XXX is*/
   1091  1.23    chopps #endif
   1092  1.23    chopps 		}
   1093  1.20    chopps 		switch (rp->siop_sbcl & 7) {
   1094  1.18    chopps 		case 0:		/* data out */
   1095  1.18    chopps 		case 1:		/* data in */
   1096  1.18    chopps 		case 2:		/* status */
   1097  1.18    chopps 		case 3:		/* command */
   1098  1.18    chopps 		case 6:		/* message in */
   1099  1.18    chopps 		case 7:		/* message out */
   1100  1.20    chopps 			rp->siop_dsp = sc->sc_scriptspa + Ent_switch;
   1101   1.1        mw 			break;
   1102   1.1        mw 		default:
   1103   1.1        mw 			goto bad_phase;
   1104   1.1        mw 		}
   1105   1.1        mw 		return 0;
   1106   1.1        mw 	}
   1107   1.1        mw 	if (sstat0 & SIOP_SSTAT0_STO) {		/* Select timed out */
   1108   1.1        mw #ifdef DEBUG
   1109  1.23    chopps 		if (acb == NULL)
   1110  1.35  christos 			printf("%s: Select timeout with no active command?\n",
   1111  1.67       chs 			    device_xname(sc->sc_dev));
   1112  1.20    chopps 		if (rp->siop_sbcl & SIOP_BSY) {
   1113  1.35  christos 			printf ("ACK! siop was busy at timeout: rp %p script %p dsa %p\n",
   1114  1.23    chopps 			    rp, &scripts, &acb->ds);
   1115  1.35  christos 			printf(" sbcl %x sdid %x istat %x dstat %x sstat0 %x\n",
   1116  1.20    chopps 			    rp->siop_sbcl, rp->siop_sdid, istat, dstat, sstat0);
   1117  1.23    chopps 			if (!(rp->siop_sbcl & SIOP_BSY)) {
   1118  1.35  christos 				printf ("Yikes, it's not busy now!\n");
   1119  1.18    chopps #if 0
   1120  1.23    chopps 				*status = -1;
   1121  1.23    chopps 				if (sc->nexus_list.tqh_first)
   1122  1.23    chopps 					rp->siop_dsp = sc->sc_scriptspa + Ent_wait_reselect;
   1123  1.23    chopps 				return 1;
   1124   1.1        mw #endif
   1125  1.23    chopps 			}
   1126  1.20    chopps /*			rp->siop_dcntl |= SIOP_DCNTL_STD;*/
   1127   1.1        mw 			return (0);
   1128   1.1        mw 		}
   1129  1.18    chopps #endif
   1130  1.18    chopps 		*status = -1;
   1131  1.23    chopps 		acb->xs->error = XS_SELTIMEOUT;
   1132  1.23    chopps 		if (sc->nexus_list.tqh_first)
   1133  1.20    chopps 			rp->siop_dsp = sc->sc_scriptspa + Ent_wait_reselect;
   1134  1.18    chopps 		return 1;
   1135   1.1        mw 	}
   1136  1.23    chopps 	if (acb)
   1137  1.44    bouyer 		target = acb->xs->xs_periph->periph_target;
   1138  1.23    chopps 	else
   1139  1.23    chopps 		target = 7;
   1140  1.18    chopps 	if (sstat0 & SIOP_SSTAT0_UDC) {
   1141   1.8    chopps #ifdef DEBUG
   1142  1.23    chopps 		if (acb == NULL)
   1143  1.35  christos 			printf("%s: Unexpected disconnect with no active command?\n",
   1144  1.67       chs 			    device_xname(sc->sc_dev));
   1145  1.35  christos 		printf ("%s: target %d disconnected unexpectedly\n",
   1146  1.67       chs 		   device_xname(sc->sc_dev), target);
   1147   1.8    chopps #endif
   1148   1.8    chopps #if 0
   1149  1.20    chopps 		siopabort (sc, rp, "siopchkintr");
   1150   1.8    chopps #endif
   1151   1.8    chopps 		*status = STS_BUSY;
   1152  1.23    chopps 		if (sc->nexus_list.tqh_first)
   1153  1.20    chopps 			rp->siop_dsp = sc->sc_scriptspa + Ent_wait_reselect;
   1154  1.29    mhitch 		return (acb != NULL);
   1155   1.8    chopps 	}
   1156  1.23    chopps 	if (dstat & SIOP_DSTAT_SIR && (rp->siop_dsps == 0xff01 ||
   1157  1.23    chopps 	    rp->siop_dsps == 0xff02)) {
   1158  1.18    chopps #ifdef DEBUG
   1159  1.18    chopps 		if (siop_debug & 0x100)
   1160  1.35  christos 			printf ("%s: ID %02x disconnected TEMP %lx (+%lx) curbuf %lx curlen %lx buf %p len %lx dfifo %x dbc %x sstat1 %x starts %d acb %p\n",
   1161  1.67       chs 			    device_xname(sc->sc_dev), 1 << target, rp->siop_temp,
   1162  1.23    chopps 			    rp->siop_temp ? rp->siop_temp - sc->sc_scriptspa : 0,
   1163  1.23    chopps 			    acb->iob_curbuf, acb->iob_curlen,
   1164  1.23    chopps 			    acb->ds.chain[0].databuf, acb->ds.chain[0].datalen, dfifo, dbc, sstat1, siopstarts, acb);
   1165  1.23    chopps #endif
   1166  1.23    chopps 		if (acb == NULL) {
   1167  1.35  christos 			printf("%s: Disconnect with no active command?\n",
   1168  1.67       chs 			    device_xname(sc->sc_dev));
   1169  1.23    chopps 			return (0);
   1170  1.23    chopps 		}
   1171  1.23    chopps 		/*
   1172  1.23    chopps 		 * XXXX need to update iob_curbuf/iob_curlen to reflect
   1173  1.23    chopps 		 * current data transferred.  If device disconnected in
   1174  1.23    chopps 		 * the middle of a DMA block, they should already be set
   1175  1.23    chopps 		 * by the phase change interrupt.  If the disconnect
   1176  1.23    chopps 		 * occurs on a DMA block boundary, we have to figure out
   1177  1.23    chopps 		 * which DMA block it was.
   1178  1.23    chopps 		 */
   1179  1.23    chopps 		if (acb->iob_len && rp->siop_temp) {
   1180  1.23    chopps 			int n = rp->siop_temp - sc->sc_scriptspa;
   1181  1.23    chopps 
   1182  1.23    chopps 			if (acb->iob_curlen && acb->iob_curlen != acb->ds.chain[0].datalen)
   1183  1.35  christos 				printf("%s: iob_curbuf/len already set? n %x iob %lx/%lx chain[0] %p/%lx\n",
   1184  1.67       chs 				    device_xname(sc->sc_dev), n, acb->iob_curbuf, acb->iob_curlen,
   1185  1.23    chopps 				    acb->ds.chain[0].databuf, acb->ds.chain[0].datalen);
   1186  1.23    chopps 			if (n < Ent_datain)
   1187  1.23    chopps 				n = (n - Ent_dataout) / 16;
   1188  1.23    chopps 			else
   1189  1.23    chopps 				n = (n - Ent_datain) / 16;
   1190  1.23    chopps 			if (n <= 0 && n > DMAMAXIO)
   1191  1.35  christos 				printf("TEMP invalid %d\n", n);
   1192  1.23    chopps 			else {
   1193  1.23    chopps 				acb->iob_curbuf = (u_long)acb->ds.chain[n].databuf;
   1194  1.23    chopps 				acb->iob_curlen = acb->ds.chain[n].datalen;
   1195  1.23    chopps 			}
   1196  1.23    chopps #ifdef DEBUG
   1197  1.23    chopps 			if (siop_debug & 0x100) {
   1198  1.67       chs 				printf("%s: TEMP offset %d", device_xname(sc->sc_dev), n);
   1199  1.35  christos 				printf(" curbuf %lx curlen %lx\n", acb->iob_curbuf,
   1200  1.23    chopps 				    acb->iob_curlen);
   1201  1.23    chopps 			}
   1202  1.18    chopps #endif
   1203  1.23    chopps 		}
   1204  1.18    chopps 		/*
   1205  1.23    chopps 		 * If data transfer was interrupted by disconnect, iob_curbuf
   1206  1.23    chopps 		 * and iob_curlen should reflect the point of interruption.
   1207  1.23    chopps 		 * Adjust the DMA chain so that the data transfer begins
   1208  1.23    chopps 		 * at the appropriate place upon reselection.
   1209  1.23    chopps 		 * XXX This should only be done on save data pointer message?
   1210  1.18    chopps 		 */
   1211  1.23    chopps 		if (acb->iob_curlen) {
   1212  1.23    chopps 			int i, j;
   1213  1.23    chopps 
   1214  1.23    chopps #ifdef DEBUG
   1215  1.23    chopps 			if (siop_debug & 0x100)
   1216  1.35  christos 				printf ("%s: adjusting DMA chain\n",
   1217  1.67       chs 				    device_xname(sc->sc_dev));
   1218  1.23    chopps 			if (rp->siop_dsps == 0xff02)
   1219  1.35  christos 				printf ("%s: ID %02x disconnected without Save Data Pointers\n",
   1220  1.67       chs 				    device_xname(sc->sc_dev), 1 << target);
   1221  1.23    chopps #endif
   1222  1.47        is /* XXX is: 		if (rp->siop_dsps != 0xff02) { */
   1223  1.47        is 				/* not disconnected without save data ptr */
   1224  1.23    chopps 			for (i = 0; i < DMAMAXIO; ++i) {
   1225  1.23    chopps 				if (acb->ds.chain[i].datalen == 0)
   1226  1.23    chopps 					break;
   1227  1.23    chopps 				if (acb->iob_curbuf >= (long)acb->ds.chain[i].databuf &&
   1228  1.23    chopps 				    acb->iob_curbuf < (long)(acb->ds.chain[i].databuf +
   1229  1.23    chopps 				    acb->ds.chain[i].datalen))
   1230  1.23    chopps 					break;
   1231  1.23    chopps 			}
   1232  1.29    mhitch 			if (i >= DMAMAXIO || acb->ds.chain[i].datalen == 0) {
   1233  1.35  christos 				printf("couldn't find saved data pointer: ");
   1234  1.35  christos 				printf("curbuf %lx curlen %lx i %d\n",
   1235  1.29    mhitch 				    acb->iob_curbuf, acb->iob_curlen, i);
   1236  1.29    mhitch #ifdef DDB
   1237  1.29    mhitch 				Debugger();
   1238  1.29    mhitch #endif
   1239  1.29    mhitch 			}
   1240  1.47        is /* XXX is: 		}			*/
   1241  1.23    chopps #ifdef DEBUG
   1242  1.23    chopps 			if (siop_debug & 0x100)
   1243  1.35  christos 				printf("  chain[0]: %p/%lx -> %lx/%lx\n",
   1244  1.23    chopps 				    acb->ds.chain[0].databuf,
   1245  1.23    chopps 				    acb->ds.chain[0].datalen,
   1246  1.23    chopps 				    acb->iob_curbuf,
   1247  1.23    chopps 				    acb->iob_curlen);
   1248  1.23    chopps #endif
   1249  1.23    chopps 			acb->ds.chain[0].databuf = (char *)acb->iob_curbuf;
   1250  1.23    chopps 			acb->ds.chain[0].datalen = acb->iob_curlen;
   1251  1.23    chopps 			for (j = 1, ++i; i < DMAMAXIO && acb->ds.chain[i].datalen; ++i, ++j) {
   1252  1.18    chopps #ifdef DEBUG
   1253  1.23    chopps 			if (siop_debug & 0x100)
   1254  1.35  christos 				printf("  chain[%d]: %p/%lx -> %p/%lx\n", j,
   1255  1.23    chopps 				    acb->ds.chain[j].databuf,
   1256  1.23    chopps 				    acb->ds.chain[j].datalen,
   1257  1.23    chopps 				    acb->ds.chain[i].databuf,
   1258  1.23    chopps 				    acb->ds.chain[i].datalen);
   1259   1.8    chopps #endif
   1260  1.23    chopps 				acb->ds.chain[j].databuf = acb->ds.chain[i].databuf;
   1261  1.23    chopps 				acb->ds.chain[j].datalen = acb->ds.chain[i].datalen;
   1262  1.18    chopps 			}
   1263  1.23    chopps 			if (j < DMAMAXIO)
   1264  1.23    chopps 				acb->ds.chain[j].datalen = 0;
   1265  1.55  christos 			DCIAS(kvtop((void *)&acb->ds.chain));
   1266  1.18    chopps 		}
   1267  1.23    chopps 		++sc->sc_tinfo[target].dconns;
   1268  1.23    chopps 		/*
   1269  1.23    chopps 		 * add nexus to waiting list
   1270  1.23    chopps 		 * clear nexus
   1271  1.23    chopps 		 * try to start another command for another target/lun
   1272  1.23    chopps 		 */
   1273  1.23    chopps 		acb->status = sc->sc_flags & SIOP_INTSOFF;
   1274  1.23    chopps 		TAILQ_INSERT_HEAD(&sc->nexus_list, acb, chain);
   1275  1.23    chopps 		sc->sc_nexus = NULL;		/* no current device */
   1276  1.23    chopps 		/* start script to wait for reselect */
   1277  1.23    chopps 		if (sc->sc_nexus == NULL)
   1278  1.23    chopps 			rp->siop_dsp = sc->sc_scriptspa + Ent_wait_reselect;
   1279  1.23    chopps /* XXXX start another command ? */
   1280  1.23    chopps 		if (sc->ready_list.tqh_first)
   1281  1.23    chopps 			siop_sched(sc);
   1282  1.18    chopps 		return (0);
   1283  1.18    chopps 	}
   1284  1.23    chopps 	if (dstat & SIOP_DSTAT_SIR && rp->siop_dsps == 0xff03) {
   1285  1.23    chopps 		int reselid = rp->siop_scratch & 0x7f;
   1286  1.23    chopps 		int reselun = rp->siop_sfbr & 0x07;
   1287  1.23    chopps 
   1288  1.23    chopps 		sc->sc_sstat1 = rp->siop_sbcl;	/* XXXX save current SBCL */
   1289  1.18    chopps #ifdef DEBUG
   1290  1.18    chopps 		if (siop_debug & 0x100)
   1291  1.35  christos 			printf ("%s: target ID %02x reselected dsps %lx\n",
   1292  1.67       chs 			     device_xname(sc->sc_dev), reselid,
   1293  1.20    chopps 			     rp->siop_dsps);
   1294  1.23    chopps 		if ((rp->siop_sfbr & 0x80) == 0)
   1295  1.35  christos 			printf("%s: Reselect message in was not identify: %x\n",
   1296  1.67       chs 			    device_xname(sc->sc_dev), rp->siop_sfbr);
   1297  1.18    chopps #endif
   1298  1.23    chopps 		if (sc->sc_nexus) {
   1299  1.23    chopps #ifdef DEBUG
   1300  1.23    chopps 			if (siop_debug & 0x100)
   1301  1.35  christos 				printf ("%s: reselect ID %02x w/active\n",
   1302  1.67       chs 				    device_xname(sc->sc_dev), reselid);
   1303  1.23    chopps #endif
   1304  1.23    chopps 			TAILQ_INSERT_HEAD(&sc->ready_list, sc->sc_nexus, chain);
   1305  1.44    bouyer 			sc->sc_tinfo[sc->sc_nexus->xs->xs_periph->periph_target].lubusy
   1306  1.44    bouyer 			    &= ~(1 << sc->sc_nexus->xs->xs_periph->periph_lun);
   1307  1.23    chopps 			--sc->sc_active;
   1308  1.18    chopps 		}
   1309  1.18    chopps 		/*
   1310  1.23    chopps 		 * locate acb of reselecting device
   1311  1.23    chopps 		 * set sc->sc_nexus to acb
   1312  1.18    chopps 		 */
   1313  1.23    chopps 		for (acb = sc->nexus_list.tqh_first; acb;
   1314  1.23    chopps 		    acb = acb->chain.tqe_next) {
   1315  1.23    chopps 			if (reselid != (acb->ds.scsi_addr >> 16) ||
   1316  1.23    chopps 			    reselun != (acb->msgout[0] & 0x07))
   1317  1.18    chopps 				continue;
   1318  1.23    chopps 			TAILQ_REMOVE(&sc->nexus_list, acb, chain);
   1319  1.23    chopps 			sc->sc_nexus = acb;
   1320  1.23    chopps 			sc->sc_flags |= acb->status;
   1321  1.23    chopps 			acb->status = 0;
   1322  1.23    chopps 			DCIAS(kvtop(&acb->stat[0]));
   1323  1.55  christos 			rp->siop_dsa = kvtop((void *)&acb->ds);
   1324  1.37    bouyer 			rp->siop_sxfer =
   1325  1.44    bouyer 				sc->sc_sync[acb->xs->xs_periph->periph_target].sxfer;
   1326  1.37    bouyer 			rp->siop_sbcl =
   1327  1.44    bouyer 				sc->sc_sync[acb->xs->xs_periph->periph_target].sbcl;
   1328  1.23    chopps 			break;
   1329  1.23    chopps 		}
   1330  1.23    chopps 		if (acb == NULL) {
   1331  1.35  christos 			printf("%s: target ID %02x reselect nexus_list %p\n",
   1332  1.67       chs 			    device_xname(sc->sc_dev), reselid,
   1333  1.23    chopps 			    sc->nexus_list.tqh_first);
   1334  1.23    chopps 			panic("unable to find reselecting device");
   1335  1.18    chopps 		}
   1336  1.55  christos 		dma_cachectl ((void *)acb, sizeof(*acb));
   1337  1.23    chopps 		rp->siop_temp = 0;
   1338  1.20    chopps 		rp->siop_dcntl |= SIOP_DCNTL_STD;
   1339  1.18    chopps 		return (0);
   1340  1.18    chopps 	}
   1341  1.23    chopps 	if (dstat & SIOP_DSTAT_SIR && rp->siop_dsps == 0xff04) {
   1342  1.31     veego #ifdef DEBUG
   1343  1.24    chopps 		u_short ctest2 = rp->siop_ctest2;
   1344  1.24    chopps 
   1345  1.23    chopps 		/* reselect was interrupted (by Sig_P or select) */
   1346  1.23    chopps 		if (siop_debug & 0x100 ||
   1347  1.24    chopps 		    (ctest2 & SIOP_CTEST2_SIGP) == 0)
   1348  1.35  christos 			printf ("%s: reselect interrupted (Sig_P?) scntl1 %x ctest2 %x sfbr %x istat %x/%x\n",
   1349  1.67       chs 			    device_xname(sc->sc_dev), rp->siop_scntl1,
   1350  1.24    chopps 			    ctest2, rp->siop_sfbr, istat, rp->siop_istat);
   1351  1.24    chopps #endif
   1352  1.24    chopps 		/* XXX assumes it was not select */
   1353  1.24    chopps 		if (sc->sc_nexus == NULL) {
   1354  1.32    mhitch #ifdef DEBUG
   1355  1.35  christos 			printf("%s: reselect interrupted, sc_nexus == NULL\n",
   1356  1.67       chs 			    device_xname(sc->sc_dev));
   1357  1.24    chopps #if 0
   1358  1.24    chopps 			siop_dump(sc);
   1359  1.24    chopps #ifdef DDB
   1360  1.24    chopps 			Debugger();
   1361  1.32    mhitch #endif
   1362  1.24    chopps #endif
   1363  1.18    chopps #endif
   1364  1.24    chopps 			rp->siop_dcntl |= SIOP_DCNTL_STD;
   1365  1.24    chopps 			return(0);
   1366  1.24    chopps 		}
   1367  1.44    bouyer 		target = sc->sc_nexus->xs->xs_periph->periph_target;
   1368  1.23    chopps 		rp->siop_temp = 0;
   1369  1.55  christos 		rp->siop_dsa = kvtop((void *)&sc->sc_nexus->ds);
   1370  1.24    chopps 		rp->siop_sxfer = sc->sc_sync[target].sxfer;
   1371  1.24    chopps 		rp->siop_sbcl = sc->sc_sync[target].sbcl;
   1372  1.23    chopps 		rp->siop_dsp = sc->sc_scriptspa;
   1373  1.23    chopps 		return (0);
   1374  1.23    chopps 	}
   1375  1.23    chopps 	if (dstat & SIOP_DSTAT_SIR && rp->siop_dsps == 0xff06) {
   1376  1.23    chopps 		if (acb == NULL)
   1377  1.35  christos 			printf("%s: Bad message-in with no active command?\n",
   1378  1.67       chs 			    device_xname(sc->sc_dev));
   1379  1.23    chopps 		/* Unrecognized message in byte */
   1380  1.23    chopps 		dma_cachectl (&acb->msg[1],1);
   1381  1.35  christos 		printf ("%s: Unrecognized message in data sfbr %x msg %x sbcl %x\n",
   1382  1.67       chs 			device_xname(sc->sc_dev), rp->siop_sfbr, acb->msg[1], rp->siop_sbcl);
   1383  1.23    chopps 		/* what should be done here? */
   1384  1.23    chopps 		DCIAS(kvtop(&acb->msg[1]));
   1385  1.59    mhitch 		rp->siop_dsp = sc->sc_scriptspa + Ent_clear_ack;
   1386  1.18    chopps 		return (0);
   1387   1.8    chopps 	}
   1388  1.23    chopps 	if (dstat & SIOP_DSTAT_SIR && rp->siop_dsps == 0xff0a) {
   1389  1.23    chopps 		/* Status phase wasn't followed by message in phase? */
   1390  1.35  christos 		printf ("%s: Status phase not followed by message in phase? sbcl %x sbdl %x\n",
   1391  1.67       chs 			device_xname(sc->sc_dev), rp->siop_sbcl, rp->siop_sbdl);
   1392  1.23    chopps 		if (rp->siop_sbcl == 0xa7) {
   1393  1.23    chopps 			/* It is now, just continue the script? */
   1394  1.23    chopps 			rp->siop_dcntl |= SIOP_DCNTL_STD;
   1395  1.23    chopps 			return (0);
   1396  1.23    chopps 		}
   1397  1.23    chopps 	}
   1398   1.1        mw 	if (sstat0 == 0 && dstat & SIOP_DSTAT_SIR) {
   1399  1.23    chopps 		dma_cachectl (&acb->stat[0], 1);
   1400  1.23    chopps 		dma_cachectl (&acb->msg[0], 1);
   1401  1.35  christos 		printf ("SIOP interrupt: %lx sts %x msg %x %x sbcl %x\n",
   1402  1.23    chopps 		    rp->siop_dsps, acb->stat[0], acb->msg[0], acb->msg[1],
   1403  1.20    chopps 		    rp->siop_sbcl);
   1404  1.20    chopps 		siopreset (sc);
   1405   1.1        mw 		*status = -1;
   1406  1.23    chopps 		return 0;	/* siopreset has cleaned up */
   1407   1.1        mw 	}
   1408  1.15    chopps 	if (sstat0 & SIOP_SSTAT0_SGE)
   1409  1.35  christos 		printf ("SIOP: SCSI Gross Error\n");
   1410  1.15    chopps 	if (sstat0 & SIOP_SSTAT0_PAR)
   1411  1.35  christos 		printf ("SIOP: Parity Error\n");
   1412  1.18    chopps 	if (dstat & SIOP_DSTAT_IID)
   1413  1.35  christos 		printf ("SIOP: Invalid instruction detected\n");
   1414   1.1        mw bad_phase:
   1415  1.15    chopps 	/*
   1416  1.15    chopps 	 * temporary panic for unhandled conditions
   1417  1.15    chopps 	 * displays various things about the 53C710 status and registers
   1418  1.15    chopps 	 * then panics.
   1419  1.15    chopps 	 * XXXX need to clean this up to print out the info, reset, and continue
   1420  1.15    chopps 	 */
   1421  1.35  christos 	printf ("siopchkintr: target %x ds %p\n", target, &acb->ds);
   1422  1.57        is 	printf ("scripts %lx ds %x rp %x dsp %lx dcmd %lx\n",
   1423  1.57        is 	    sc->sc_scriptspa, (unsigned)kvtop((void *)&acb->ds),
   1424  1.57        is 	    (unsigned)kvtop((void *)__UNVOLATILE(rp)), rp->siop_dsp,
   1425  1.57        is 	    *((volatile long *)&rp->siop_dcmd));
   1426  1.35  christos 	printf ("siopchkintr: istat %x dstat %x sstat0 %x dsps %lx dsa %lx sbcl %x sts %x msg %x %x sfbr %x\n",
   1427  1.20    chopps 	    istat, dstat, sstat0, rp->siop_dsps, rp->siop_dsa,
   1428  1.23    chopps 	     rp->siop_sbcl, acb->stat[0], acb->msg[0], acb->msg[1], rp->siop_sfbr);
   1429  1.15    chopps #ifdef DEBUG
   1430  1.15    chopps 	if (siop_debug & 0x20)
   1431  1.15    chopps 		panic("siopchkintr: **** temp ****");
   1432  1.15    chopps #endif
   1433  1.23    chopps #ifdef DDB
   1434  1.23    chopps 	Debugger ();
   1435  1.23    chopps #endif
   1436  1.20    chopps 	siopreset (sc);		/* hard reset */
   1437  1.15    chopps 	*status = -1;
   1438  1.23    chopps 	return 0;		/* siopreset cleaned up */
   1439   1.1        mw }
   1440   1.1        mw 
   1441  1.23    chopps void
   1442  1.45   aymeric siop_select(struct siop_softc *sc)
   1443   1.1        mw {
   1444  1.23    chopps 	siop_regmap_p rp;
   1445  1.23    chopps 	struct siop_acb *acb = sc->sc_nexus;
   1446   1.1        mw 
   1447   1.1        mw #ifdef DEBUG
   1448   1.1        mw 	if (siop_debug & 1)
   1449  1.67       chs 		printf ("%s: select ", device_xname(sc->sc_dev));
   1450   1.1        mw #endif
   1451   1.1        mw 
   1452  1.23    chopps 	rp = sc->sc_siopp;
   1453  1.43   thorpej 	if (acb->xs->xs_control & XS_CTL_POLL || siop_no_dma) {
   1454  1.23    chopps 		sc->sc_flags |= SIOP_INTSOFF;
   1455  1.23    chopps 		sc->sc_flags &= ~SIOP_INTDEFER;
   1456  1.23    chopps 		if ((rp->siop_istat & 0x08) == 0) {
   1457  1.23    chopps 			rp->siop_sien = 0;
   1458  1.23    chopps 			rp->siop_dien = 0;
   1459   1.1        mw 		}
   1460  1.23    chopps #if 0
   1461  1.23    chopps 	} else if ((sc->sc_flags & SIOP_INTDEFER) == 0) {
   1462  1.23    chopps 		sc->sc_flags &= ~SIOP_INTSOFF;
   1463  1.23    chopps 		if ((rp->siop_istat & 0x08) == 0) {
   1464  1.23    chopps 			rp->siop_sien = sc->sc_sien;
   1465  1.23    chopps 			rp->siop_dien = sc->sc_dien;
   1466   1.1        mw 		}
   1467   1.1        mw #endif
   1468   1.1        mw 	}
   1469   1.1        mw #ifdef DEBUG
   1470   1.1        mw 	if (siop_debug & 1)
   1471  1.35  christos 		printf ("siop_select: target %x cmd %02x ds %p\n",
   1472  1.44    bouyer 		    acb->xs->xs_periph->periph_target, acb->cmd.opcode,
   1473  1.23    chopps 		    &sc->sc_nexus->ds);
   1474   1.8    chopps #endif
   1475   1.1        mw 
   1476  1.44    bouyer 	siop_start(sc, acb->xs->xs_periph->periph_target,
   1477  1.44    bouyer 		acb->xs->xs_periph->periph_lun,
   1478  1.30     veego 	    (u_char *)&acb->cmd, acb->clen, acb->daddr, acb->dleft);
   1479   1.1        mw 
   1480  1.23    chopps 	return;
   1481   1.1        mw }
   1482   1.1        mw 
   1483   1.1        mw /*
   1484  1.23    chopps  * 53C710 interrupt handler
   1485   1.1        mw  */
   1486   1.1        mw 
   1487  1.30     veego void
   1488  1.45   aymeric siopintr(register struct siop_softc *sc)
   1489   1.1        mw {
   1490  1.20    chopps 	siop_regmap_p rp;
   1491   1.1        mw 	register u_char istat, dstat, sstat0;
   1492   1.1        mw 	int status;
   1493  1.23    chopps 	int s = splbio();
   1494   1.1        mw 
   1495  1.20    chopps 	istat = sc->sc_istat;
   1496  1.23    chopps 	if ((istat & (SIOP_ISTAT_SIP | SIOP_ISTAT_DIP)) == 0) {
   1497  1.23    chopps 		splx(s);
   1498   1.8    chopps 		return;
   1499  1.23    chopps 	}
   1500  1.23    chopps 
   1501  1.18    chopps 	/* Got a valid interrupt on this device */
   1502  1.23    chopps 	rp = sc->sc_siopp;
   1503  1.20    chopps 	dstat = sc->sc_dstat;
   1504  1.20    chopps 	sstat0 = sc->sc_sstat0;
   1505  1.23    chopps 	if (dstat & SIOP_DSTAT_SIR)
   1506  1.23    chopps 		sc->sc_intcode = rp->siop_dsps;
   1507  1.20    chopps 	sc->sc_istat = 0;
   1508   1.1        mw #ifdef DEBUG
   1509   1.8    chopps 	if (siop_debug & 1)
   1510  1.35  christos 		printf ("%s: intr istat %x dstat %x sstat0 %x\n",
   1511  1.67       chs 		    device_xname(sc->sc_dev), istat, dstat, sstat0);
   1512  1.20    chopps 	if (!sc->sc_active) {
   1513  1.35  christos 		printf ("%s: spurious interrupt? istat %x dstat %x sstat0 %x nexus %p status %x\n",
   1514  1.67       chs 		    device_xname(sc->sc_dev), istat, dstat, sstat0,
   1515  1.29    mhitch 		    sc->sc_nexus, sc->sc_nexus ? sc->sc_nexus->stat[0] : 0);
   1516   1.8    chopps 	}
   1517   1.1        mw #endif
   1518   1.1        mw 
   1519   1.1        mw #ifdef DEBUG
   1520   1.8    chopps 	if (siop_debug & 5) {
   1521  1.23    chopps 		DCIAS(kvtop(&sc->sc_nexus->stat[0]));
   1522  1.35  christos 		printf ("%s: intr istat %x dstat %x sstat0 %x dsps %lx sbcl %x sts %x msg %x\n",
   1523  1.67       chs 		    device_xname(sc->sc_dev), istat, dstat, sstat0,
   1524  1.20    chopps 		    rp->siop_dsps,  rp->siop_sbcl,
   1525  1.23    chopps 		    sc->sc_nexus->stat[0], sc->sc_nexus->msg[0]);
   1526   1.8    chopps 	}
   1527   1.1        mw #endif
   1528  1.23    chopps 	if (sc->sc_flags & SIOP_INTDEFER) {
   1529  1.23    chopps 		sc->sc_flags &= ~(SIOP_INTDEFER | SIOP_INTSOFF);
   1530  1.23    chopps 		rp->siop_sien = sc->sc_sien;
   1531  1.23    chopps 		rp->siop_dien = sc->sc_dien;
   1532  1.23    chopps 	}
   1533  1.20    chopps 	if (siop_checkintr (sc, istat, dstat, sstat0, &status)) {
   1534   1.1        mw #if 1
   1535   1.8    chopps 		if (status == 0xff)
   1536  1.35  christos 			printf ("siopintr: status == 0xff\n");
   1537   1.8    chopps #endif
   1538  1.23    chopps 		if ((sc->sc_flags & (SIOP_INTSOFF | SIOP_INTDEFER)) != SIOP_INTSOFF) {
   1539  1.23    chopps #if 0
   1540  1.23    chopps 			if (rp->siop_sbcl & SIOP_BSY) {
   1541  1.35  christos 				printf ("%s: SCSI bus busy at completion",
   1542  1.67       chs 					device_xname(sc->sc_dev));
   1543  1.35  christos 				printf(" targ %d sbcl %02x sfbr %x lcrc %02x dsp +%x\n",
   1544  1.44    bouyer 				    sc->sc_nexus->xs->xs_periph->periph_target,
   1545  1.23    chopps 				    rp->siop_sbcl, rp->siop_sfbr, rp->siop_lcrc,
   1546  1.23    chopps 				    rp->siop_dsp - sc->sc_scriptspa);
   1547  1.23    chopps 			}
   1548  1.23    chopps #endif
   1549  1.29    mhitch 			siop_scsidone(sc->sc_nexus, sc->sc_nexus ?
   1550  1.29    mhitch 			    sc->sc_nexus->stat[0] : -1);
   1551  1.18    chopps 		}
   1552   1.1        mw 	}
   1553  1.23    chopps 	splx(s);
   1554   1.1        mw }
   1555   1.1        mw 
   1556  1.18    chopps /*
   1557  1.23    chopps  * This is based on the Progressive Peripherals 33Mhz Zeus driver and will
   1558  1.18    chopps  * not be correct for other 53c710 boards.
   1559  1.18    chopps  *
   1560  1.18    chopps  */
   1561  1.30     veego void
   1562  1.45   aymeric scsi_period_to_siop(struct siop_softc *sc, int target)
   1563   1.1        mw {
   1564  1.30     veego 	int period, offset, sxfer, sbcl = 0;
   1565  1.30     veego #ifdef DEBUG_SYNC
   1566  1.30     veego 	int i;
   1567  1.30     veego #endif
   1568   1.1        mw 
   1569  1.23    chopps 	period = sc->sc_nexus->msg[4];
   1570  1.23    chopps 	offset = sc->sc_nexus->msg[5];
   1571  1.29    mhitch #ifdef DEBUG_SYNC
   1572   1.8    chopps 	sxfer = 0;
   1573   1.8    chopps 	if (offset <= SIOP_MAX_OFFSET)
   1574   1.8    chopps 		sxfer = offset;
   1575  1.24    chopps 	for (i = 0; i < sizeof (sync_tab) / 2; ++i) {
   1576  1.24    chopps 		if (period <= sync_tab[i].p) {
   1577  1.24    chopps 			sxfer |= sync_tab[i].r & 0x70;
   1578  1.24    chopps 			sbcl = sync_tab[i].r & 0x03;
   1579   1.8    chopps 			break;
   1580   1.8    chopps 		}
   1581   1.8    chopps 	}
   1582  1.35  christos 	printf ("siop sync old: siop_sxfr %02x, siop_sbcl %02x\n", sxfer, sbcl);
   1583  1.24    chopps #endif
   1584  1.24    chopps 	for (sbcl = 1; sbcl < 4; ++sbcl) {
   1585  1.24    chopps 		sxfer = (period * 4 - 1) / sc->sc_tcp[sbcl] - 3;
   1586  1.24    chopps 		if (sxfer >= 0 && sxfer <= 7)
   1587  1.24    chopps 			break;
   1588  1.24    chopps 	}
   1589  1.24    chopps 	if (sbcl > 3) {
   1590  1.35  christos 		printf("siop sync: unable to compute sync params for period %dns\n",
   1591  1.24    chopps 		    period * 4);
   1592  1.24    chopps 		/*
   1593  1.24    chopps 		 * XXX need to pick a value we can do and renegotiate
   1594  1.24    chopps 		 */
   1595  1.24    chopps 		sxfer = sbcl = 0;
   1596  1.24    chopps 	} else {
   1597  1.24    chopps 		sxfer = (sxfer << 4) | ((offset <= SIOP_MAX_OFFSET) ?
   1598  1.24    chopps 		    offset : SIOP_MAX_OFFSET);
   1599  1.29    mhitch #ifdef DEBUG_SYNC
   1600  1.35  christos 		printf("siop sync: params for period %dns: sxfer %x sbcl %x",
   1601  1.24    chopps 		    period * 4, sxfer, sbcl);
   1602  1.35  christos 		printf(" actual period %dns\n",
   1603  1.24    chopps 		    sc->sc_tcp[sbcl] * ((sxfer >> 4) + 4));
   1604  1.29    mhitch #endif
   1605  1.24    chopps 	}
   1606  1.24    chopps 	sc->sc_sync[target].sxfer = sxfer;
   1607  1.24    chopps 	sc->sc_sync[target].sbcl = sbcl;
   1608  1.29    mhitch #ifdef DEBUG_SYNC
   1609  1.35  christos 	printf ("siop sync: siop_sxfr %02x, siop_sbcl %02x\n", sxfer, sbcl);
   1610  1.24    chopps #endif
   1611  1.24    chopps }
   1612  1.24    chopps 
   1613   1.1        mw #ifdef DEBUG
   1614  1.24    chopps 
   1615  1.24    chopps #if SIOP_TRACE_SIZE
   1616  1.24    chopps void
   1617  1.45   aymeric siop_dump_trace(void)
   1618  1.24    chopps {
   1619  1.24    chopps 	int i;
   1620  1.24    chopps 
   1621  1.35  christos 	printf("siop trace: next index %d\n", siop_trix);
   1622  1.24    chopps 	i = siop_trix;
   1623  1.24    chopps 	do {
   1624  1.35  christos 		printf("%3d: '%c' %02x %02x %02x\n", i, siop_trbuf[i],
   1625  1.24    chopps 		    siop_trbuf[i + 1], siop_trbuf[i + 2], siop_trbuf[i + 3]);
   1626  1.24    chopps 		i = (i + 4) & (SIOP_TRACE_SIZE - 1);
   1627  1.24    chopps 	} while (i != siop_trix);
   1628  1.24    chopps }
   1629   1.1        mw #endif
   1630  1.24    chopps 
   1631  1.24    chopps void
   1632  1.45   aymeric siop_dump_acb(struct siop_acb *acb)
   1633  1.24    chopps {
   1634  1.24    chopps 	u_char *b = (u_char *) &acb->cmd;
   1635  1.24    chopps 	int i;
   1636  1.24    chopps 
   1637  1.35  christos 	printf("acb@%p ", acb);
   1638  1.24    chopps 	if (acb->xs == NULL) {
   1639  1.35  christos 		printf("<unused>\n");
   1640  1.24    chopps 		return;
   1641  1.24    chopps 	}
   1642  1.37    bouyer 	printf("(%d:%d) flags %2x clen %2d cmd ",
   1643  1.44    bouyer 		acb->xs->xs_periph->periph_target,
   1644  1.44    bouyer 	    acb->xs->xs_periph->periph_lun, acb->flags, acb->clen);
   1645  1.24    chopps 	for (i = acb->clen; i; --i)
   1646  1.35  christos 		printf(" %02x", *b++);
   1647  1.35  christos 	printf("\n");
   1648  1.35  christos 	printf("  xs: %p data %p:%04x ", acb->xs, acb->xs->data,
   1649  1.24    chopps 	    acb->xs->datalen);
   1650  1.35  christos 	printf("va %p:%lx ", acb->iob_buf, acb->iob_len);
   1651  1.35  christos 	printf("cur %lx:%lx\n", acb->iob_curbuf, acb->iob_curlen);
   1652   1.1        mw }
   1653  1.24    chopps 
   1654  1.24    chopps void
   1655  1.45   aymeric siop_dump(struct siop_softc *sc)
   1656  1.24    chopps {
   1657  1.24    chopps 	struct siop_acb *acb;
   1658  1.24    chopps 	siop_regmap_p rp = sc->sc_siopp;
   1659  1.24    chopps 	int s;
   1660  1.24    chopps 	int i;
   1661  1.24    chopps 
   1662  1.24    chopps 	s = splbio();
   1663  1.24    chopps #if SIOP_TRACE_SIZE
   1664  1.24    chopps 	siop_dump_trace();
   1665  1.24    chopps #endif
   1666  1.35  christos 	printf("%s@%p regs %p istat %x\n",
   1667  1.67       chs 	    device_xname(sc->sc_dev), sc, rp, rp->siop_istat);
   1668  1.30     veego 	if ((acb = sc->free_list.tqh_first) > 0) {
   1669  1.35  christos 		printf("Free list:\n");
   1670  1.24    chopps 		while (acb) {
   1671  1.24    chopps 			siop_dump_acb(acb);
   1672  1.24    chopps 			acb = acb->chain.tqe_next;
   1673  1.24    chopps 		}
   1674  1.24    chopps 	}
   1675  1.30     veego 	if ((acb = sc->ready_list.tqh_first) > 0) {
   1676  1.35  christos 		printf("Ready list:\n");
   1677  1.24    chopps 		while (acb) {
   1678  1.24    chopps 			siop_dump_acb(acb);
   1679  1.24    chopps 			acb = acb->chain.tqe_next;
   1680  1.24    chopps 		}
   1681  1.24    chopps 	}
   1682  1.30     veego 	if ((acb = sc->nexus_list.tqh_first) > 0) {
   1683  1.35  christos 		printf("Nexus list:\n");
   1684  1.24    chopps 		while (acb) {
   1685  1.24    chopps 			siop_dump_acb(acb);
   1686  1.24    chopps 			acb = acb->chain.tqe_next;
   1687  1.24    chopps 		}
   1688  1.24    chopps 	}
   1689  1.24    chopps 	if (sc->sc_nexus) {
   1690  1.35  christos 		printf("Nexus:\n");
   1691  1.24    chopps 		siop_dump_acb(sc->sc_nexus);
   1692  1.24    chopps 	}
   1693  1.24    chopps 	for (i = 0; i < 8; ++i) {
   1694  1.24    chopps 		if (sc->sc_tinfo[i].cmds > 2) {
   1695  1.44    bouyer 			printf("tgt %d: cmds %d disc %d lubusy %x\n",
   1696  1.24    chopps 			    i, sc->sc_tinfo[i].cmds,
   1697  1.24    chopps 			    sc->sc_tinfo[i].dconns,
   1698  1.24    chopps 			    sc->sc_tinfo[i].lubusy);
   1699  1.24    chopps 		}
   1700  1.24    chopps 	}
   1701  1.24    chopps 	splx(s);
   1702  1.24    chopps }
   1703  1.24    chopps #endif
   1704