Home | History | Annotate | Line # | Download | only in dev
sbic.c revision 1.73
      1 /*	$NetBSD: sbic.c,v 1.73 2014/08/07 08:59:42 joerg Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1990 The Regents of the University of California.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Van Jacobson of Lawrence Berkeley Laboratory.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  *
     34  *	@(#)scsi.c	7.5 (Berkeley) 5/4/91
     35  */
     36 
     37 /*
     38  * Copyright (c) 1994 Christian E. Hopps
     39  *
     40  * This code is derived from software contributed to Berkeley by
     41  * Van Jacobson of Lawrence Berkeley Laboratory.
     42  *
     43  * Redistribution and use in source and binary forms, with or without
     44  * modification, are permitted provided that the following conditions
     45  * are met:
     46  * 1. Redistributions of source code must retain the above copyright
     47  *    notice, this list of conditions and the following disclaimer.
     48  * 2. Redistributions in binary form must reproduce the above copyright
     49  *    notice, this list of conditions and the following disclaimer in the
     50  *    documentation and/or other materials provided with the distribution.
     51  * 3. All advertising materials mentioning features or use of this software
     52  *    must display the following acknowledgement:
     53  *	This product includes software developed by the University of
     54  *	California, Berkeley and its contributors.
     55  * 4. Neither the name of the University nor the names of its contributors
     56  *    may be used to endorse or promote products derived from this software
     57  *    without specific prior written permission.
     58  *
     59  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     61  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     62  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     63  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     64  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     65  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     69  * SUCH DAMAGE.
     70  *
     71  *	@(#)scsi.c	7.5 (Berkeley) 5/4/91
     72  */
     73 
     74 /*
     75  * AMIGA AMD 33C93 scsi adaptor driver
     76  */
     77 
     78 #include "opt_ddb.h"
     79 #ifdef __m68k__
     80 #include "opt_m68k_arch.h"
     81 #endif
     82 
     83 #include <sys/cdefs.h>
     84 __KERNEL_RCSID(0, "$NetBSD: sbic.c,v 1.73 2014/08/07 08:59:42 joerg Exp $");
     85 
     86 #include <sys/param.h>
     87 #include <sys/systm.h>
     88 #include <sys/device.h>
     89 #include <sys/kernel.h> /* For hz */
     90 #include <sys/disklabel.h>
     91 #include <sys/buf.h>
     92 #include <dev/scsipi/scsi_all.h>
     93 #include <dev/scsipi/scsipi_all.h>
     94 #include <dev/scsipi/scsiconf.h>
     95 #include <machine/cpu.h>
     96 #include <amiga/amiga/device.h>
     97 #include <amiga/amiga/custom.h>
     98 #include <amiga/amiga/isr.h>
     99 #include <amiga/dev/dmavar.h>
    100 #include <amiga/dev/sbicreg.h>
    101 #include <amiga/dev/sbicvar.h>
    102 
    103 /* These are for bounce buffers */
    104 #include <amiga/amiga/cc.h>
    105 #include <amiga/dev/zbusvar.h>
    106 
    107 /* Since I can't find this in any other header files */
    108 #define SCSI_PHASE(reg)	(reg&0x07)
    109 
    110 /*
    111  * SCSI delays
    112  * In u-seconds, primarily for state changes on the SPC.
    113  */
    114 #define	SBIC_CMD_WAIT	50000	/* wait per step of 'immediate' cmds */
    115 #define	SBIC_DATA_WAIT	50000	/* wait per data in/out step */
    116 #define	SBIC_INIT_WAIT	50000	/* wait per step (both) during init */
    117 
    118 #define SBIC_WAIT(regs, until, timeo) sbicwait(regs, until, timeo, __LINE__)
    119 
    120 int  sbicicmd(struct sbic_softc *, int, int, void *, int, void *, int);
    121 int  sbicgo(struct sbic_softc *, struct scsipi_xfer *);
    122 int  sbicdmaok(struct sbic_softc *, struct scsipi_xfer *);
    123 int  sbicwait(sbic_regmap_t, char, int , int);
    124 int  sbiccheckdmap(void *, u_long, u_long);
    125 int  sbicselectbus(struct sbic_softc *, sbic_regmap_t, u_char, u_char, u_char);
    126 int  sbicxfstart(sbic_regmap_t, int, u_char, int);
    127 int  sbicxfout(sbic_regmap_t regs, int, void *, int);
    128 int  sbicfromscsiperiod(struct sbic_softc *, sbic_regmap_t, int);
    129 int  sbictoscsiperiod(struct sbic_softc *, sbic_regmap_t, int);
    130 int  sbicpoll(struct sbic_softc *);
    131 int  sbicnextstate(struct sbic_softc *, u_char, u_char);
    132 int  sbicmsgin(struct sbic_softc *);
    133 int  sbicxfin(sbic_regmap_t regs, int, void *);
    134 int  sbicabort(struct sbic_softc *, sbic_regmap_t, const char *);
    135 void sbicxfdone(struct sbic_softc *, sbic_regmap_t, int);
    136 void sbicerror(struct sbic_softc *, sbic_regmap_t, u_char);
    137 void sbicstart(struct sbic_softc *);
    138 void sbicreset(struct sbic_softc *);
    139 void sbic_scsidone(struct sbic_acb *, int);
    140 void sbic_sched(struct sbic_softc *);
    141 void sbic_save_ptrs(struct sbic_softc *, sbic_regmap_t,int,int);
    142 void sbic_load_ptrs(struct sbic_softc *, sbic_regmap_t,int,int);
    143 #ifdef DEBUG
    144 void sbicdumpstate(void);
    145 void sbic_dump_acb(struct sbic_acb *);
    146 #endif
    147 
    148 /*
    149  * Synch xfer parameters, and timing conversions
    150  */
    151 int sbic_min_period = SBIC_SYN_MIN_PERIOD;  /* in cycles = f(ICLK,FSn) */
    152 int sbic_max_offset = SBIC_SYN_MAX_OFFSET;  /* pure number */
    153 
    154 int sbic_cmd_wait = SBIC_CMD_WAIT;
    155 int sbic_data_wait = SBIC_DATA_WAIT;
    156 int sbic_init_wait = SBIC_INIT_WAIT;
    157 
    158 /*
    159  * was broken before.. now if you want this you get it for all drives
    160  * on sbic controllers.
    161  */
    162 u_char sbic_inhibit_sync[8];
    163 int sbic_enable_reselect = 1;
    164 int sbic_clock_override = 0;
    165 int sbic_no_dma = 0;
    166 int sbic_parallel_operations = 1;
    167 
    168 #ifdef DEBUG
    169 sbic_regmap_t debug_sbic_regs;
    170 int	sbicdma_ops = 0;	/* total DMA operations */
    171 int	sbicdma_bounces = 0;	/* number operations using bounce buffer */
    172 int	sbicdma_hits = 0;	/* number of DMA chains that were contiguous */
    173 int	sbicdma_misses = 0;	/* number of DMA chains that were not contiguous */
    174 int     sbicdma_saves = 0;
    175 #define QPRINTF(a) if (sbic_debug > 1) printf a
    176 int	sbic_debug = 0;
    177 int	sync_debug = 0;
    178 int	sbic_dma_debug = 0;
    179 int	reselect_debug = 0;
    180 int	data_pointer_debug = 0;
    181 u_char	debug_asr, debug_csr, routine;
    182 void sbictimeout(struct sbic_softc *dev);
    183 
    184 #define CSR_TRACE_SIZE 32
    185 #if CSR_TRACE_SIZE
    186 #define CSR_TRACE(w,c,a,x) do { \
    187 	int s_csr_trace = splbio(); \
    188 	csr_trace[csr_traceptr].whr = (w); csr_trace[csr_traceptr].csr = (c); \
    189 	csr_trace[csr_traceptr].asr = (a); csr_trace[csr_traceptr].xtn = (x); \
    190 	dma_cachectl((void *)&csr_trace[csr_traceptr], sizeof(csr_trace[0])); \
    191 	csr_traceptr = (csr_traceptr + 1) & (CSR_TRACE_SIZE - 1); \
    192 /*	dma_cachectl((void *)&csr_traceptr, sizeof(csr_traceptr));*/ \
    193 	splx(s_csr_trace); \
    194 } while (0)
    195 int csr_traceptr;
    196 int csr_tracesize = CSR_TRACE_SIZE;
    197 struct {
    198 	u_char whr;
    199 	u_char csr;
    200 	u_char asr;
    201 	u_char xtn;
    202 } csr_trace[CSR_TRACE_SIZE];
    203 #else
    204 #define CSR_TRACE(w,c,a,x)
    205 #endif
    206 
    207 #define SBIC_TRACE_SIZE 0
    208 #if SBIC_TRACE_SIZE
    209 #define SBIC_TRACE(dev) do { \
    210 	int s = splbio(); \
    211 	sbic_trace[sbic_traceptr].sp = &s; \
    212 	sbic_trace[sbic_traceptr].line = __LINE__; \
    213 	sbic_trace[sbic_traceptr].sr = s; \
    214 	sbic_trace[sbic_traceptr].csr = csr_traceptr; \
    215 	dma_cachectl(&sbic_trace[sbic_traceptr], sizeof(sbic_trace[0])); \
    216 	sbic_traceptr = (sbic_traceptr + 1) & (SBIC_TRACE_SIZE - 1); \
    217 	dma_cachectl(&sbic_traceptr, sizeof(sbic_traceptr)); \
    218 	if (dev) dma_cachectl(dev, sizeof(*dev)); \
    219 	splx(s); \
    220 } while (0)
    221 int sbic_traceptr;
    222 int sbic_tracesize = SBIC_TRACE_SIZE;
    223 struct {
    224 	void *sp;
    225 	u_short line;
    226 	u_short sr;
    227 	int csr;
    228 } sbic_trace[SBIC_TRACE_SIZE];
    229 #else
    230 #define SBIC_TRACE(dev)
    231 #endif
    232 
    233 #else	/* DEBUG */
    234 #define QPRINTF(a)
    235 #define CSR_TRACE(w,c,a,x)
    236 #define SBIC_TRACE(dev)
    237 #endif	/* DEBUG */
    238 
    239 /*
    240  * default minphys routine for sbic based controllers
    241  */
    242 void
    243 sbic_minphys(struct buf *bp)
    244 {
    245 
    246 	/*
    247 	 * No max transfer at this level.
    248 	 */
    249 	minphys(bp);
    250 }
    251 
    252 /*
    253  * Save DMA pointers.  Take into account partial transfer. Shut down DMA.
    254  */
    255 void
    256 sbic_save_ptrs(struct sbic_softc *dev, sbic_regmap_t regs, int target, int lun)
    257 {
    258 	int count, asr, s;
    259 	struct sbic_acb* acb;
    260 
    261 	SBIC_TRACE(dev);
    262 	if( !dev->sc_cur ) return;
    263 	if( !(dev->sc_flags & SBICF_INDMA) ) return; /* DMA not active */
    264 
    265 	s = splbio();
    266 
    267 	acb = dev->sc_nexus;
    268 	count = -1;
    269 	do {
    270 		GET_SBIC_asr(regs, asr);
    271 		if( asr & SBIC_ASR_DBR ) {
    272 			printf("sbic_save_ptrs: asr %02x canceled!\n", asr);
    273 			splx(s);
    274 			SBIC_TRACE(dev);
    275 			return;
    276 		}
    277 	} while( asr & (SBIC_ASR_BSY|SBIC_ASR_CIP) );
    278 
    279 	/* Save important state */
    280 	/* must be done before dmastop */
    281 	acb->sc_dmacmd = dev->sc_dmacmd;
    282 	SBIC_TC_GET(regs, count);
    283 
    284 	/* Shut down DMA ====CAREFUL==== */
    285 	dev->sc_dmastop(dev);
    286 	dev->sc_flags &= ~SBICF_INDMA;
    287 	SBIC_TC_PUT(regs, 0);
    288 
    289 #ifdef DEBUG
    290 	if(!count && sbic_debug) printf("%dcount0",target);
    291 	if(data_pointer_debug == -1)
    292 		printf("SBIC saving target %d data pointers from (%p,%x)%xASR:%02x",
    293 		       target, dev->sc_cur->dc_addr, dev->sc_cur->dc_count,
    294 		       acb->sc_dmacmd, asr);
    295 #endif
    296 
    297 	/* Fixup partial xfers */
    298 	acb->sc_kv.dc_addr += (dev->sc_tcnt - count);
    299 	acb->sc_kv.dc_count -= (dev->sc_tcnt - count);
    300 	acb->sc_pa.dc_addr += (dev->sc_tcnt - count);
    301 	acb->sc_pa.dc_count -= ((dev->sc_tcnt - count)>>1);
    302 
    303 	acb->sc_tcnt = dev->sc_tcnt = count;
    304 #ifdef DEBUG
    305 	if(data_pointer_debug)
    306 		printf(" at (%p,%x):%x\n",
    307 		       dev->sc_cur->dc_addr, dev->sc_cur->dc_count,count);
    308 	sbicdma_saves++;
    309 #endif
    310 	splx(s);
    311 	SBIC_TRACE(dev);
    312 }
    313 
    314 
    315 /*
    316  * DOES NOT RESTART DMA!!!
    317  */
    318 void
    319 sbic_load_ptrs(struct sbic_softc *dev, sbic_regmap_t regs, int target, int lun)
    320 {
    321 	int s, count;
    322 	char* vaddr, * paddr;
    323 	struct sbic_acb *acb;
    324 
    325 	SBIC_TRACE(dev);
    326 	acb = dev->sc_nexus;
    327 	if( !acb->sc_kv.dc_count ) {
    328 		/* No data to xfer */
    329 		SBIC_TRACE(dev);
    330 		return;
    331 	}
    332 
    333 	s = splbio();
    334 
    335 	dev->sc_last = dev->sc_cur = &acb->sc_pa;
    336 	dev->sc_tcnt = acb->sc_tcnt;
    337 	dev->sc_dmacmd = acb->sc_dmacmd;
    338 
    339 #ifdef DEBUG
    340 	sbicdma_ops++;
    341 #endif
    342 	if( !dev->sc_tcnt ) {
    343 		/* sc_tcnt == 0 implies end of segment */
    344 
    345 		/* do kvm to pa mappings */
    346 		paddr = acb->sc_pa.dc_addr =
    347 			(char *) kvtop(acb->sc_kv.dc_addr);
    348 
    349 		vaddr = acb->sc_kv.dc_addr;
    350 		count = acb->sc_kv.dc_count;
    351 		for(count = (PAGE_SIZE - ((int)vaddr & PGOFSET));
    352 		    count < acb->sc_kv.dc_count
    353 		    && (char*)kvtop(vaddr + count + 4) == paddr + count + 4;
    354 		    count += PAGE_SIZE);
    355 		/* If it's all contiguous... */
    356 		if(count > acb->sc_kv.dc_count ) {
    357 			count = acb->sc_kv.dc_count;
    358 #ifdef DEBUG
    359 			sbicdma_hits++;
    360 #endif
    361 		} else {
    362 #ifdef DEBUG
    363 			sbicdma_misses++;
    364 #endif
    365 		}
    366 		acb->sc_tcnt = count;
    367 		acb->sc_pa.dc_count = count >> 1;
    368 
    369 #ifdef DEBUG
    370 		if(data_pointer_debug)
    371 			printf("DMA recalc:kv(%p,%x)pa(%p,%lx)\n",
    372 			       acb->sc_kv.dc_addr,
    373 			       acb->sc_kv.dc_count,
    374 			       acb->sc_pa.dc_addr,
    375 			       acb->sc_tcnt);
    376 #endif
    377 	}
    378 	splx(s);
    379 #ifdef DEBUG
    380 	if(data_pointer_debug)
    381 		printf("SBIC restoring target %d data pointers at (%p,%x)%x\n",
    382 		       target, dev->sc_cur->dc_addr, dev->sc_cur->dc_count,
    383 		       dev->sc_dmacmd);
    384 #endif
    385 	SBIC_TRACE(dev);
    386 }
    387 
    388 /*
    389  * used by specific sbic controller
    390  *
    391  * it appears that the higher level code does nothing with LUN's
    392  * so I will too.  I could plug it in, however so could they
    393  * in scsi_scsipi_cmd().
    394  */
    395 void
    396 sbic_scsipi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req,
    397                     void *arg)
    398 {
    399 	struct scsipi_xfer *xs;
    400 	struct scsipi_periph *periph;
    401 	struct sbic_acb *acb;
    402 	struct sbic_softc *dev = device_private(chan->chan_adapter->adapt_dev);
    403 	int flags, s, stat;
    404 
    405 	switch (req) {
    406 	case ADAPTER_REQ_RUN_XFER:
    407 		xs = arg;
    408 		periph = xs->xs_periph;
    409 
    410 		SBIC_TRACE(dev);
    411 		flags = xs->xs_control;
    412 
    413 		if (flags & XS_CTL_DATA_UIO)
    414 			panic("sbic: scsi data uio requested");
    415 
    416 		if (dev->sc_nexus && flags & XS_CTL_POLL)
    417 			panic("sbic_scsipi_request: busy");
    418 
    419 		s = splbio();
    420 		acb = dev->free_list.tqh_first;
    421 		if (acb)
    422 			TAILQ_REMOVE(&dev->free_list, acb, chain);
    423 		splx(s);
    424 
    425 #ifdef DIAGNOSTIC
    426 		if (acb == NULL) {
    427 			scsipi_printaddr(periph);
    428 			printf("unable to allocate acb\n");
    429 			panic("sbic_scsipi_request");
    430 		}
    431 #endif
    432 		acb->flags = ACB_ACTIVE;
    433 		if (flags & XS_CTL_DATA_IN)
    434 			acb->flags |= ACB_DATAIN;
    435 		acb->xs = xs;
    436 		memcpy(&acb->cmd, xs->cmd, xs->cmdlen);
    437 		acb->clen = xs->cmdlen;
    438 		acb->sc_kv.dc_addr = xs->data;
    439 		acb->sc_kv.dc_count = xs->datalen;
    440 		acb->pa_addr = xs->data ? (char *)kvtop(xs->data) : 0;	/* XXXX check */
    441 
    442 		if (flags & XS_CTL_POLL) {
    443 			s = splbio();
    444 			/*
    445 			 * This has major side effects - it locks up the machine
    446 			 */
    447 
    448 			dev->sc_flags |= SBICF_ICMD;
    449 			do {
    450 				while(dev->sc_nexus)
    451 					sbicpoll(dev);
    452 				dev->sc_nexus = acb;
    453 				dev->sc_stat[0] = -1;
    454 				dev->sc_xs = xs;
    455 				dev->target = periph->periph_target;
    456 				dev->lun = periph->periph_lun;
    457 				stat = sbicicmd(dev, dev->target, dev->lun,
    458 					&acb->cmd, acb->clen,
    459 					acb->sc_kv.dc_addr, acb->sc_kv.dc_count);
    460 			} while (dev->sc_nexus != acb);
    461 			sbic_scsidone(acb, stat);
    462 
    463 			splx(s);
    464 			SBIC_TRACE(dev);
    465 			return;
    466 		}
    467 
    468 		s = splbio();
    469 		TAILQ_INSERT_TAIL(&dev->ready_list, acb, chain);
    470 
    471 		if (dev->sc_nexus) {
    472 			splx(s);
    473 			SBIC_TRACE(dev);
    474 			return;
    475 		}
    476 
    477 		/*
    478 		 * nothing is active, try to start it now.
    479 		 */
    480 		sbic_sched(dev);
    481 		splx(s);
    482 
    483 		SBIC_TRACE(dev);
    484 /* TODO:  add sbic_poll to do XS_CTL_POLL operations */
    485 #if 0
    486 		if (flags & XS_CTL_POLL)
    487 			return(COMPLETE);
    488 #endif
    489 		return;
    490 
    491 	case ADAPTER_REQ_GROW_RESOURCES:
    492 		return;
    493 
    494 	case ADAPTER_REQ_SET_XFER_MODE:
    495 		return;
    496 	}
    497 }
    498 
    499 /*
    500  * attempt to start the next available command
    501  */
    502 void
    503 sbic_sched(struct sbic_softc *dev)
    504 {
    505 	struct scsipi_xfer *xs;
    506 	struct scsipi_periph *periph;
    507 	struct sbic_acb *acb;
    508 	int flags, /*phase,*/ stat, i;
    509 
    510 	SBIC_TRACE(dev);
    511 	if (dev->sc_nexus)
    512 		return;			/* a command is current active */
    513 
    514 	SBIC_TRACE(dev);
    515 	for (acb = dev->ready_list.tqh_first; acb; acb = acb->chain.tqe_next) {
    516 		periph = acb->xs->xs_periph;
    517 		i = periph->periph_target;
    518 		if (!(dev->sc_tinfo[i].lubusy & (1 << periph->periph_lun))) {
    519 			struct sbic_tinfo *ti = &dev->sc_tinfo[i];
    520 
    521 			TAILQ_REMOVE(&dev->ready_list, acb, chain);
    522 			dev->sc_nexus = acb;
    523 			ti = &dev->sc_tinfo[periph->periph_target];
    524 			ti->lubusy |= (1 << periph->periph_lun);
    525 			acb->sc_pa.dc_addr = acb->pa_addr;	/* XXXX check */
    526 			break;
    527 		}
    528 	}
    529 
    530 	SBIC_TRACE(dev);
    531 	if (acb == NULL)
    532 		return;			/* did not find an available command */
    533 
    534 	dev->sc_xs = xs = acb->xs;
    535 	periph = xs->xs_periph;
    536 	flags = xs->xs_control;
    537 
    538 	if (flags & XS_CTL_RESET)
    539 		sbicreset(dev);
    540 
    541 #ifdef DEBUG
    542 	if( data_pointer_debug > 1 )
    543 		printf("sbic_sched(%d,%d)\n", periph->periph_target,
    544 			periph->periph_lun);
    545 #endif
    546 	dev->sc_stat[0] = -1;
    547 	dev->target = periph->periph_target;
    548 	dev->lun = periph->periph_lun;
    549 	if ( flags & XS_CTL_POLL || ( !sbic_parallel_operations
    550 				   && (sbicdmaok(dev, xs) == 0)))
    551 		stat = sbicicmd(dev, periph->periph_target,
    552 			periph->periph_lun, &acb->cmd,
    553 		    acb->clen, acb->sc_kv.dc_addr, acb->sc_kv.dc_count);
    554 	else if (sbicgo(dev, xs) == 0 && xs->error != XS_SELTIMEOUT) {
    555 		SBIC_TRACE(dev);
    556 		return;
    557 	} else
    558 		stat = dev->sc_stat[0];
    559 
    560 	sbic_scsidone(acb, stat);
    561 	SBIC_TRACE(dev);
    562 }
    563 
    564 void
    565 sbic_scsidone(struct sbic_acb *acb, int stat)
    566 {
    567 	struct scsipi_xfer *xs;
    568 	struct scsipi_periph *periph;
    569 	struct sbic_softc *dev;
    570 	int dosched = 0;
    571 
    572 	xs = acb->xs;
    573 	periph = xs->xs_periph;
    574 	dev = device_private(periph->periph_channel->chan_adapter->adapt_dev);
    575 	SBIC_TRACE(dev);
    576 #ifdef DIAGNOSTIC
    577 	if (acb == NULL || xs == NULL) {
    578 		printf("sbic_scsidone -- (%d,%d) no scsi_xfer\n",
    579 		       dev->target, dev->lun);
    580 #ifdef DDB
    581 		Debugger();
    582 #endif
    583 		return;
    584 	}
    585 #endif
    586 
    587 	xs->status = stat;
    588 	xs->resid = 0;		/* XXXX */
    589 #ifdef DEBUG
    590 	if( data_pointer_debug > 1 )
    591 		printf("scsidone: (%d,%d)->(%d,%d)%02x\n",
    592 		       periph->periph_target, periph->periph_lun,
    593 		       dev->target,  dev->lun,  stat);
    594 	if( periph->periph_target ==
    595 		periph->periph_channel->chan_id)
    596 		panic("target == hostid");
    597 #endif
    598 
    599 	if (xs->error == XS_NOERROR) {
    600 		if (stat == SCSI_CHECK || stat == SCSI_BUSY)
    601 			xs->error = XS_BUSY;
    602 	}
    603 
    604 	/*
    605 	 * Remove the ACB from whatever queue it's on.  We have to do a bit of
    606 	 * a hack to figure out which queue it's on.  Note that it is *not*
    607 	 * necessary to cdr down the ready queue, but we must cdr down the
    608 	 * nexus queue and see if it's there, so we can mark the unit as no
    609 	 * longer busy.  This code is sickening, but it works.
    610 	 */
    611 	if (acb == dev->sc_nexus) {
    612 		dev->sc_nexus = NULL;
    613 		dev->sc_xs = NULL;
    614 		dev->sc_tinfo[periph->periph_target].lubusy &=
    615 			~(1<<periph->periph_lun);
    616 		if (dev->ready_list.tqh_first)
    617 			dosched = 1;	/* start next command */
    618 	} else if (dev->ready_list.tqh_last == &acb->chain.tqe_next) {
    619 		TAILQ_REMOVE(&dev->ready_list, acb, chain);
    620 	} else {
    621 		register struct sbic_acb *acb2;
    622 		for (acb2 = dev->nexus_list.tqh_first; acb2;
    623 		    acb2 = acb2->chain.tqe_next) {
    624 			if (acb2 == acb) {
    625 				TAILQ_REMOVE(&dev->nexus_list, acb, chain);
    626 				dev->sc_tinfo[periph->periph_target].lubusy
    627 					&= ~(1<<periph->periph_lun);
    628 				break;
    629 			}
    630 		}
    631 		if (acb2)
    632 			;
    633 		else if (acb->chain.tqe_next) {
    634 			TAILQ_REMOVE(&dev->ready_list, acb, chain);
    635 		} else {
    636 			printf("%s: can't find matching acb\n",
    637 			    device_xname(dev->sc_dev));
    638 #ifdef DDB
    639 			Debugger();
    640 #endif
    641 		}
    642 	}
    643 	/* Put it on the free list. */
    644 	acb->flags = ACB_FREE;
    645 	TAILQ_INSERT_HEAD(&dev->free_list, acb, chain);
    646 
    647 	dev->sc_tinfo[periph->periph_target].cmds++;
    648 
    649 	scsipi_done(xs);
    650 
    651 	if (dosched)
    652 		sbic_sched(dev);
    653 	SBIC_TRACE(dev);
    654 }
    655 
    656 int
    657 sbicdmaok(struct sbic_softc *dev, struct scsipi_xfer *xs)
    658 {
    659 	if (sbic_no_dma || !xs->datalen || xs->datalen & 0x1 ||
    660 	    (u_int)xs->data & 0x3)
    661 		return(0);
    662 	/*
    663 	 * controller supports dma to any addresses?
    664 	 */
    665 	else if ((dev->sc_flags & SBICF_BADDMA) == 0)
    666 		return(1);
    667 	/*
    668 	 * this address is ok for DMA?
    669 	 */
    670 	else if (sbiccheckdmap(xs->data, xs->datalen, dev->sc_dmamask) == 0)
    671 		return(1);
    672 	/*
    673 	 * we have a bounce buffer?
    674 	 */
    675 	else if (dev->sc_tinfo[xs->xs_periph->periph_target].bounce)
    676 		return(1);
    677 	/*
    678 	 * try to get one
    679 	 */
    680 	else if ((dev->sc_tinfo[xs->xs_periph->periph_target].bounce
    681 		 = (char *)alloc_z2mem(MAXPHYS))) {
    682 		if (isztwomem(dev->sc_tinfo[xs->xs_periph->periph_target].bounce))
    683 			printf("alloc ZII target %d bounce pa 0x%x\n",
    684 			       xs->xs_periph->periph_target,
    685 			       (unsigned)kvtop(dev->sc_tinfo[xs->xs_periph->periph_target].bounce));
    686 		else if (dev->sc_tinfo[xs->xs_periph->periph_target].bounce)
    687 			printf("alloc CHIP target %d bounce pa %p\n",
    688 			       xs->xs_periph->periph_target,
    689 			       PREP_DMA_MEM(dev->sc_tinfo[xs->xs_periph->periph_target].bounce));
    690 		return(1);
    691 	}
    692 
    693 	return(0);
    694 }
    695 
    696 
    697 int
    698 sbicwait(sbic_regmap_t regs, char until, int timeo, int line)
    699 {
    700 	u_char val;
    701 	int csr;
    702 
    703 	SBIC_TRACE((struct sbic_softc *)0);
    704 	if (timeo == 0)
    705 		timeo = 1000000;	/* some large value.. */
    706 
    707 	GET_SBIC_asr(regs,val);
    708 	while ((val & until) == 0) {
    709 		if (timeo-- == 0) {
    710 			GET_SBIC_csr(regs, csr);
    711 			printf("sbicwait TIMEO @%d with asr=x%x csr=x%x\n",
    712 			    line, val, csr);
    713 #if defined(DDB) && defined(DEBUG)
    714 			Debugger();
    715 #endif
    716 			return(val); /* Maybe I should abort */
    717 			break;
    718 		}
    719 		DELAY(1);
    720 		GET_SBIC_asr(regs,val);
    721 	}
    722 	SBIC_TRACE((struct sbic_softc *)0);
    723 	return(val);
    724 }
    725 
    726 int
    727 sbicabort(struct sbic_softc *dev, sbic_regmap_t regs, const char *where)
    728 {
    729 	u_char csr, asr;
    730 
    731 	GET_SBIC_asr(regs, asr);
    732 	GET_SBIC_csr(regs, csr);
    733 
    734 	printf ("%s: abort %s: csr = 0x%02x, asr = 0x%02x\n",
    735 	    device_xname(dev->sc_dev), where, csr, asr);
    736 
    737 
    738 #if 0
    739 	/* Clean up running command */
    740 	if (dev->sc_nexus != NULL) {
    741 		dev->sc_nexus->xs->error = XS_DRIVER_STUFFUP;
    742 		sbic_scsidone(dev->sc_nexus, dev->sc_stat[0]);
    743 	}
    744 	while (acb = dev->nexus_list.tqh_first) {
    745 		acb->xs->error = XS_DRIVER_STUFFUP;
    746 		sbic_scsidone(acb, -1 /*acb->stat[0]*/);
    747 	}
    748 #endif
    749 
    750 	/* Clean up chip itself */
    751 	if (dev->sc_flags & SBICF_SELECTED) {
    752 		while( asr & SBIC_ASR_DBR ) {
    753 			/* sbic is jammed w/data. need to clear it */
    754 			/* But we don't know what direction it needs to go */
    755 			GET_SBIC_data(regs, asr);
    756 			printf("%s: abort %s: clearing data buffer 0x%02x\n",
    757 			       device_xname(dev->sc_dev), where, asr);
    758 			GET_SBIC_asr(regs, asr);
    759 			if( asr & SBIC_ASR_DBR ) /* Not the read direction, then */
    760 				SET_SBIC_data(regs, asr);
    761 			GET_SBIC_asr(regs, asr);
    762 		}
    763 		WAIT_CIP(regs);
    764 printf("%s: sbicabort - sending ABORT command\n", device_xname(dev->sc_dev));
    765 		SET_SBIC_cmd(regs, SBIC_CMD_ABORT);
    766 		WAIT_CIP(regs);
    767 
    768 		GET_SBIC_asr(regs, asr);
    769 		if (asr & (SBIC_ASR_BSY|SBIC_ASR_LCI)) {
    770 			/* ok, get more drastic.. */
    771 
    772 printf("%s: sbicabort - asr %x, trying to reset\n", device_xname(dev->sc_dev), asr);
    773 			sbicreset(dev);
    774 			dev->sc_flags &= ~SBICF_SELECTED;
    775 			return -1;
    776 		}
    777 printf("%s: sbicabort - sending DISC command\n", device_xname(dev->sc_dev));
    778 		SET_SBIC_cmd(regs, SBIC_CMD_DISC);
    779 
    780 		do {
    781 			asr = SBIC_WAIT (regs, SBIC_ASR_INT, 0);
    782 			GET_SBIC_csr (regs, csr);
    783 			CSR_TRACE('a',csr,asr,0);
    784 		} while ((csr != SBIC_CSR_DISC) && (csr != SBIC_CSR_DISC_1)
    785 		    && (csr != SBIC_CSR_CMD_INVALID));
    786 
    787 		/* lets just hope it worked.. */
    788 		dev->sc_flags &= ~SBICF_SELECTED;
    789 	}
    790 	return -1;
    791 }
    792 
    793 
    794 /*
    795  * Initialize driver-private structures
    796  */
    797 
    798 void
    799 sbicinit(struct sbic_softc *dev)
    800 {
    801 	u_int i;
    802 	struct sbic_acb *acb;
    803 	u_int inhibit_sync;
    804 
    805 	extern u_long scsi_nosync;
    806 	extern int shift_nosync;
    807 
    808 	if ((dev->sc_flags & SBICF_ALIVE) == 0) {
    809 		TAILQ_INIT(&dev->ready_list);
    810 		TAILQ_INIT(&dev->nexus_list);
    811 		TAILQ_INIT(&dev->free_list);
    812 		callout_init(&dev->sc_timo_ch, 0);
    813 		dev->sc_nexus = NULL;
    814 		dev->sc_xs = NULL;
    815 		acb = dev->sc_acb;
    816 		memset(acb, 0, sizeof(dev->sc_acb));
    817 		for (i = 0; i < sizeof(dev->sc_acb) / sizeof(*acb); i++) {
    818 			TAILQ_INSERT_TAIL(&dev->free_list, acb, chain);
    819 			acb++;
    820 		}
    821 		memset(dev->sc_tinfo, 0, sizeof(dev->sc_tinfo));
    822 #ifdef DEBUG
    823 		/* make sure timeout is really not needed */
    824 		callout_reset(&dev->sc_timo_ch, 30 * hz,
    825 		    (void *)sbictimeout, dev);
    826 #endif
    827 
    828 	} else panic("sbic: reinitializing driver!");
    829 
    830 	dev->sc_flags |= SBICF_ALIVE;
    831 	dev->sc_flags &= ~SBICF_SELECTED;
    832 
    833 	/* initialize inhibit array */
    834 	if (scsi_nosync) {
    835 		inhibit_sync = (scsi_nosync >> shift_nosync) & 0xff;
    836 		shift_nosync += 8;
    837 #ifdef DEBUG
    838 		if (inhibit_sync)
    839 			printf("%s: Inhibiting synchronous transfer %02x\n",
    840 				device_xname(dev->sc_dev), inhibit_sync);
    841 #endif
    842 		for (i = 0; i < 8; ++i)
    843 			if (inhibit_sync & (1 << i))
    844 				sbic_inhibit_sync[i] = 1;
    845 	}
    846 
    847 	sbicreset(dev);
    848 }
    849 
    850 void
    851 sbicreset(struct sbic_softc *dev)
    852 {
    853 	sbic_regmap_t regs;
    854 	u_int my_id, s;
    855 	u_char csr;
    856 #if 0
    857 	u_int i;
    858 	struct sbic_acb *acb;
    859 #endif
    860 
    861 	regs = dev->sc_sbic;
    862 #if 0
    863 	if (dev->sc_flags & SBICF_ALIVE) {
    864 		SET_SBIC_cmd(regs, SBIC_CMD_ABORT);
    865 		WAIT_CIP(regs);
    866 	}
    867 #else
    868 		SET_SBIC_cmd(regs, SBIC_CMD_ABORT);
    869 		WAIT_CIP(regs);
    870 #endif
    871 	s = splbio();
    872 	my_id = dev->sc_channel.chan_id & SBIC_ID_MASK;
    873 
    874 	/* Enable advanced mode */
    875 	my_id |= SBIC_ID_EAF /*| SBIC_ID_EHP*/ ;
    876 	SET_SBIC_myid(regs, my_id);
    877 
    878 	/*
    879 	 * Disable interrupts (in dmainit) then reset the chip
    880 	 */
    881 	SET_SBIC_cmd(regs, SBIC_CMD_RESET);
    882 	DELAY(25);
    883 	SBIC_WAIT(regs, SBIC_ASR_INT, 0);
    884 	GET_SBIC_csr(regs, csr);       /* clears interrupt also */
    885 	__USE(csr);
    886 
    887 	if (dev->sc_clkfreq < 110)
    888 		my_id |= SBIC_ID_FS_8_10;
    889 	else if (dev->sc_clkfreq < 160)
    890 		my_id |= SBIC_ID_FS_12_15;
    891 	else if (dev->sc_clkfreq < 210)
    892 		my_id |= SBIC_ID_FS_16_20;
    893 
    894 	SET_SBIC_myid(regs, my_id);
    895 
    896 	/*
    897 	 * Set up various chip parameters
    898 	 */
    899 	SET_SBIC_control(regs, SBIC_CTL_EDI | SBIC_CTL_IDI /* | SBIC_CTL_HSP */
    900 	    | SBIC_MACHINE_DMA_MODE);
    901 	/*
    902 	 * don't allow (re)selection (SBIC_RID_ES)
    903 	 * until we can handle target mode!!
    904 	 */
    905 	SET_SBIC_rselid(regs, SBIC_RID_ER);
    906 	SET_SBIC_syn(regs, 0);     /* asynch for now */
    907 
    908 	/*
    909 	 * anything else was zeroed by reset
    910 	 */
    911 	splx(s);
    912 
    913 #if 0
    914 	if ((dev->sc_flags & SBICF_ALIVE) == 0) {
    915 		TAILQ_INIT(&dev->ready_list);
    916 		TAILQ_INIT(&dev->nexus_list);
    917 		TAILQ_INIT(&dev->free_list);
    918 		dev->sc_nexus = NULL;
    919 		dev->sc_xs = NULL;
    920 		acb = dev->sc_acb;
    921 		memset(acb, 0, sizeof(dev->sc_acb));
    922 		for (i = 0; i < sizeof(dev->sc_acb) / sizeof(*acb); i++) {
    923 			TAILQ_INSERT_TAIL(&dev->free_list, acb, chain);
    924 			acb++;
    925 		}
    926 		memset(dev->sc_tinfo, 0, sizeof(dev->sc_tinfo));
    927 	} else {
    928 		if (dev->sc_nexus != NULL) {
    929 			dev->sc_nexus->xs->error = XS_DRIVER_STUFFUP;
    930 			sbic_scsidone(dev->sc_nexus, dev->sc_stat[0]);
    931 		}
    932 		while (acb = dev->nexus_list.tqh_first) {
    933 			acb->xs->error = XS_DRIVER_STUFFUP;
    934 			sbic_scsidone(acb, -1 /*acb->stat[0]*/);
    935 		}
    936 	}
    937 
    938 	dev->sc_flags |= SBICF_ALIVE;
    939 #endif
    940 	dev->sc_flags &= ~SBICF_SELECTED;
    941 }
    942 
    943 void
    944 sbicerror(struct sbic_softc *dev, sbic_regmap_t regs, u_char csr)
    945 {
    946 	struct scsipi_xfer *xs;
    947 
    948 	xs = dev->sc_xs;
    949 
    950 #ifdef DIAGNOSTIC
    951 	if (xs == NULL)
    952 		panic("sbicerror");
    953 #endif
    954 	if (xs->xs_control & XS_CTL_SILENT)
    955 		return;
    956 
    957 	printf("%s: ", device_xname(dev->sc_dev));
    958 	printf("csr == 0x%02x\n", csr);	/* XXX */
    959 }
    960 
    961 /*
    962  * select the bus, return when selected or error.
    963  */
    964 int
    965 sbicselectbus(struct sbic_softc *dev, sbic_regmap_t regs, u_char target,
    966               u_char lun, u_char our_addr)
    967 {
    968 	u_char asr, csr, id;
    969 
    970 	SBIC_TRACE(dev);
    971 	QPRINTF(("sbicselectbus %d\n", target));
    972 
    973 	/*
    974 	 * if we're already selected, return (XXXX panic maybe?)
    975 	 */
    976 	if (dev->sc_flags & SBICF_SELECTED) {
    977 		SBIC_TRACE(dev);
    978 		return(1);
    979 	}
    980 
    981 	/*
    982 	 * issue select
    983 	 */
    984 	SBIC_TC_PUT(regs, 0);
    985 	SET_SBIC_selid(regs, target);
    986 	SET_SBIC_timeo(regs, SBIC_TIMEOUT(250,dev->sc_clkfreq));
    987 
    988 	/*
    989 	 * set sync or async
    990 	 */
    991 	if (dev->sc_sync[target].state == SYNC_DONE)
    992 		SET_SBIC_syn(regs, SBIC_SYN (dev->sc_sync[target].offset,
    993 		    dev->sc_sync[target].period));
    994 	else
    995 		SET_SBIC_syn(regs, SBIC_SYN (0, sbic_min_period));
    996 
    997 	GET_SBIC_asr(regs, asr);
    998 	if( asr & (SBIC_ASR_INT|SBIC_ASR_BSY) ) {
    999 		/* This means we got ourselves reselected upon */
   1000 /*		printf("sbicselectbus: INT/BSY asr %02x\n", asr);*/
   1001 #ifdef DDB
   1002 /*		Debugger();*/
   1003 #endif
   1004 		SBIC_TRACE(dev);
   1005 		return 1;
   1006 	}
   1007 
   1008 	SET_SBIC_cmd(regs, SBIC_CMD_SEL_ATN);
   1009 
   1010 	/*
   1011 	 * wait for select (merged from separate function may need
   1012 	 * cleanup)
   1013 	 */
   1014 	WAIT_CIP(regs);
   1015 	do {
   1016 		asr = SBIC_WAIT(regs, SBIC_ASR_INT | SBIC_ASR_LCI, 0);
   1017 		if (asr & SBIC_ASR_LCI) {
   1018 #ifdef DEBUG
   1019 			if (reselect_debug)
   1020 				printf("sbicselectbus: late LCI asr %02x\n", asr);
   1021 #endif
   1022 			SBIC_TRACE(dev);
   1023 			return 1;
   1024 		}
   1025 		GET_SBIC_csr (regs, csr);
   1026 		CSR_TRACE('s',csr,asr,target);
   1027 		QPRINTF(("%02x ", csr));
   1028 		if( csr == SBIC_CSR_RSLT_NI || csr == SBIC_CSR_RSLT_IFY) {
   1029 #ifdef DEBUG
   1030 			if(reselect_debug)
   1031 				printf("sbicselectbus: reselected asr %02x\n", asr);
   1032 #endif
   1033 			/* We need to handle this now so we don't lock up later */
   1034 			sbicnextstate(dev, csr, asr);
   1035 			SBIC_TRACE(dev);
   1036 			return 1;
   1037 		}
   1038 		if( csr == SBIC_CSR_SLT || csr == SBIC_CSR_SLT_ATN) {
   1039 			panic("sbicselectbus: target issued select!");
   1040 			return 1;
   1041 		}
   1042 	} while (csr != (SBIC_CSR_MIS_2|MESG_OUT_PHASE)
   1043 	    && csr != (SBIC_CSR_MIS_2|CMD_PHASE) && csr != SBIC_CSR_SEL_TIMEO);
   1044 
   1045 	/* Enable (or not) reselection */
   1046 	if(!sbic_enable_reselect && dev->nexus_list.tqh_first == NULL)
   1047 		SET_SBIC_rselid (regs, 0);
   1048 	else
   1049 		SET_SBIC_rselid (regs, SBIC_RID_ER);
   1050 
   1051 	if (csr == (SBIC_CSR_MIS_2|CMD_PHASE)) {
   1052 		dev->sc_flags |= SBICF_SELECTED;	/* device ignored ATN */
   1053 		GET_SBIC_selid(regs, id);
   1054 		dev->target = id;
   1055 		GET_SBIC_tlun(regs,dev->lun);
   1056 		if( dev->lun & SBIC_TLUN_VALID )
   1057 			dev->lun &= SBIC_TLUN_MASK;
   1058 		else
   1059 			dev->lun = lun;
   1060 	} else if (csr == (SBIC_CSR_MIS_2|MESG_OUT_PHASE)) {
   1061 		/*
   1062 		 * Send identify message
   1063 		 * (SCSI-2 requires an identify msg (?))
   1064 		 */
   1065 		GET_SBIC_selid(regs, id);
   1066 		dev->target = id;
   1067 		GET_SBIC_tlun(regs,dev->lun);
   1068 		if( dev->lun & SBIC_TLUN_VALID )
   1069 			dev->lun &= SBIC_TLUN_MASK;
   1070 		else
   1071 			dev->lun = lun;
   1072 		/*
   1073 		 * handle drives that don't want to be asked
   1074 		 * whether to go sync at all.
   1075 		 */
   1076 		if (sbic_inhibit_sync[id]
   1077 		    && dev->sc_sync[id].state == SYNC_START) {
   1078 #ifdef DEBUG
   1079 			if (sync_debug)
   1080 				printf("Forcing target %d asynchronous.\n", id);
   1081 #endif
   1082 			dev->sc_sync[id].offset = 0;
   1083 			dev->sc_sync[id].period = sbic_min_period;
   1084 			dev->sc_sync[id].state = SYNC_DONE;
   1085 		}
   1086 
   1087 
   1088 		if (dev->sc_sync[id].state != SYNC_START){
   1089 			if( dev->sc_xs->xs_control & XS_CTL_POLL
   1090 			   || (dev->sc_flags & SBICF_ICMD)
   1091 			   || !sbic_enable_reselect )
   1092 				SEND_BYTE (regs, MSG_IDENTIFY | lun);
   1093 			else
   1094 				SEND_BYTE (regs, MSG_IDENTIFY_DR | lun);
   1095 		} else {
   1096 			/*
   1097 			 * try to initiate a sync transfer.
   1098 			 * So compose the sync message we're going
   1099 			 * to send to the target
   1100 			 */
   1101 
   1102 #ifdef DEBUG
   1103 			if (sync_debug)
   1104 				printf("Sending sync request to target %d ... ",
   1105 				    id);
   1106 #endif
   1107 			/*
   1108 			 * setup scsi message sync message request
   1109 			 */
   1110 			dev->sc_msg[0] = MSG_IDENTIFY | lun;
   1111 			dev->sc_msg[1] = MSG_EXT_MESSAGE;
   1112 			dev->sc_msg[2] = 3;
   1113 			dev->sc_msg[3] = MSG_SYNC_REQ;
   1114 			dev->sc_msg[4] = sbictoscsiperiod(dev, regs,
   1115 			    sbic_min_period);
   1116 			dev->sc_msg[5] = sbic_max_offset;
   1117 
   1118 			if (sbicxfstart(regs, 6, MESG_OUT_PHASE, sbic_cmd_wait))
   1119 				sbicxfout(regs, 6, dev->sc_msg, MESG_OUT_PHASE);
   1120 
   1121 			dev->sc_sync[id].state = SYNC_SENT;
   1122 #ifdef DEBUG
   1123 			if (sync_debug)
   1124 				printf ("sent\n");
   1125 #endif
   1126 		}
   1127 
   1128 		asr = SBIC_WAIT (regs, SBIC_ASR_INT, 0);
   1129 		GET_SBIC_csr (regs, csr);
   1130 		CSR_TRACE('y',csr,asr,target);
   1131 		QPRINTF(("[%02x]", csr));
   1132 #ifdef DEBUG
   1133 		if (sync_debug && dev->sc_sync[id].state == SYNC_SENT)
   1134 			printf("csr-result of last msgout: 0x%x\n", csr);
   1135 #endif
   1136 
   1137 		if (csr != SBIC_CSR_SEL_TIMEO)
   1138 			dev->sc_flags |= SBICF_SELECTED;
   1139 	}
   1140 	if (csr == SBIC_CSR_SEL_TIMEO)
   1141 		dev->sc_xs->error = XS_SELTIMEOUT;
   1142 
   1143 	QPRINTF(("\n"));
   1144 
   1145 	SBIC_TRACE(dev);
   1146 	return(csr == SBIC_CSR_SEL_TIMEO);
   1147 }
   1148 
   1149 int
   1150 sbicxfstart(sbic_regmap_t regs, int len, u_char phase, int wait)
   1151 {
   1152 	u_char id;
   1153 
   1154 	switch (phase) {
   1155 	case DATA_IN_PHASE:
   1156 	case MESG_IN_PHASE:
   1157 		GET_SBIC_selid (regs, id);
   1158 		id |= SBIC_SID_FROM_SCSI;
   1159 		SET_SBIC_selid (regs, id);
   1160 		SBIC_TC_PUT (regs, (unsigned)len);
   1161 		break;
   1162 	case DATA_OUT_PHASE:
   1163 	case MESG_OUT_PHASE:
   1164 	case CMD_PHASE:
   1165 		GET_SBIC_selid (regs, id);
   1166 		id &= ~SBIC_SID_FROM_SCSI;
   1167 		SET_SBIC_selid (regs, id);
   1168 		SBIC_TC_PUT (regs, (unsigned)len);
   1169 		break;
   1170 	default:
   1171 		SBIC_TC_PUT (regs, 0);
   1172 	}
   1173 	QPRINTF(("sbicxfstart %d, %d, %d\n", len, phase, wait));
   1174 
   1175 	return(1);
   1176 }
   1177 
   1178 int
   1179 sbicxfout(sbic_regmap_t regs, int len, void *bp, int phase)
   1180 {
   1181 	u_char orig_csr, asr, *buf;
   1182 	int wait;
   1183 
   1184 	buf = bp;
   1185 	wait = sbic_data_wait;
   1186 
   1187 	QPRINTF(("sbicxfout {%d} %02x %02x %02x %02x %02x "
   1188 	    "%02x %02x %02x %02x %02x\n", len, buf[0], buf[1], buf[2],
   1189 	    buf[3], buf[4], buf[5], buf[6], buf[7], buf[8], buf[9]));
   1190 
   1191 	GET_SBIC_csr (regs, orig_csr);
   1192 	__USE(orig_csr);
   1193 	CSR_TRACE('>',orig_csr,0,0);
   1194 
   1195 	/*
   1196 	 * sigh.. WD-PROTO strikes again.. sending the command in one go
   1197 	 * causes the chip to lock up if talking to certain (misbehaving?)
   1198 	 * targets. Anyway, this procedure should work for all targets, but
   1199 	 * it's slightly slower due to the overhead
   1200 	 */
   1201 	WAIT_CIP (regs);
   1202 	SET_SBIC_cmd (regs, SBIC_CMD_XFER_INFO);
   1203 	for (;len > 0; len--) {
   1204 		GET_SBIC_asr (regs, asr);
   1205 		while ((asr & SBIC_ASR_DBR) == 0) {
   1206 			if ((asr & SBIC_ASR_INT) || --wait < 0) {
   1207 #ifdef DEBUG
   1208 				if (sbic_debug)
   1209 					printf("sbicxfout fail: l%d i%x w%d\n",
   1210 					    len, asr, wait);
   1211 #endif
   1212 				return (len);
   1213 			}
   1214 /*			DELAY(1);*/
   1215 			GET_SBIC_asr (regs, asr);
   1216 		}
   1217 
   1218 		SET_SBIC_data (regs, *buf);
   1219 		buf++;
   1220 	}
   1221 	SBIC_TC_GET(regs, len);
   1222 	QPRINTF(("sbicxfout done %d bytes\n", len));
   1223 	/*
   1224 	 * this leaves with one csr to be read
   1225 	 */
   1226 	return(0);
   1227 }
   1228 
   1229 /* returns # bytes left to read */
   1230 int
   1231 sbicxfin(sbic_regmap_t regs, int len, void *bp)
   1232 {
   1233 	int wait;
   1234 	u_char *buf;
   1235 	u_char orig_csr, csr, asr;
   1236 
   1237 	wait = sbic_data_wait;
   1238 	buf = bp;
   1239 
   1240 	GET_SBIC_csr (regs, orig_csr);
   1241 	__USE(orig_csr);
   1242 	CSR_TRACE('<',orig_csr,0,0);
   1243 
   1244 	QPRINTF(("sbicxfin %d, csr=%02x\n", len, orig_csr));
   1245 
   1246 	WAIT_CIP (regs);
   1247 	SET_SBIC_cmd (regs, SBIC_CMD_XFER_INFO);
   1248 	for (;len > 0; len--) {
   1249 		GET_SBIC_asr (regs, asr);
   1250 		if((asr & SBIC_ASR_PE)) {
   1251 #ifdef DEBUG
   1252 			printf("sbicxfin parity error: l%d i%x w%d\n",
   1253 			       len, asr, wait);
   1254 /*			return ((unsigned long)buf - (unsigned long)bp); */
   1255 #ifdef DDB
   1256 			Debugger();
   1257 #endif
   1258 #endif
   1259 		}
   1260 		while ((asr & SBIC_ASR_DBR) == 0) {
   1261 			if ((asr & SBIC_ASR_INT) || --wait < 0) {
   1262 #ifdef DEBUG
   1263 				if (sbic_debug) {
   1264 	QPRINTF(("sbicxfin fail:{%d} %02x %02x %02x %02x %02x %02x "
   1265 	    "%02x %02x %02x %02x\n", len, obp[0], obp[1], obp[2],
   1266 	    obp[3], obp[4], obp[5], obp[6], obp[7], obp[8], obp[9]));
   1267 					printf("sbicxfin fail: l%d i%x w%d\n",
   1268 					    len, asr, wait);
   1269 }
   1270 #endif
   1271 				return len;
   1272 			}
   1273 
   1274 			if (!(asr & SBIC_ASR_BSY)) {
   1275 				GET_SBIC_csr(regs, csr);
   1276 				__USE(csr);
   1277 				CSR_TRACE('<',csr,asr,len);
   1278 				QPRINTF(("[CSR%02xASR%02x]", csr, asr));
   1279 			}
   1280 
   1281 /*			DELAY(1);*/
   1282 			GET_SBIC_asr (regs, asr);
   1283 		}
   1284 
   1285 		GET_SBIC_data (regs, *buf);
   1286 /*		QPRINTF(("asr=%02x, csr=%02x, data=%02x\n", asr, csr, *buf));*/
   1287 		buf++;
   1288 	}
   1289 
   1290 	QPRINTF(("sbicxfin {%d} %02x %02x %02x %02x %02x %02x "
   1291 	    "%02x %02x %02x %02x\n", len, obp[0], obp[1], obp[2],
   1292 	    obp[3], obp[4], obp[5], obp[6], obp[7], obp[8], obp[9]));
   1293 
   1294 	/* this leaves with one csr to be read */
   1295 	return len;
   1296 }
   1297 
   1298 /*
   1299  * SCSI 'immediate' command:  issue a command to some SCSI device
   1300  * and get back an 'immediate' response (i.e., do programmed xfer
   1301  * to get the response data).  'cbuf' is a buffer containing a scsi
   1302  * command of length clen bytes.  'buf' is a buffer of length 'len'
   1303  * bytes for data.  The transfer direction is determined by the device
   1304  * (i.e., by the scsi bus data xfer phase).  If 'len' is zero, the
   1305  * command must supply no data.
   1306  */
   1307 int
   1308 sbicicmd(struct sbic_softc *dev, int target, int lun, void *cbuf, int clen,
   1309          void *buf, int len)
   1310 {
   1311 	sbic_regmap_t regs;
   1312 	u_char phase, csr, asr;
   1313 	int wait, i;
   1314 	struct sbic_acb *acb;
   1315 
   1316 #define CSR_LOG_BUF_SIZE 0
   1317 #if CSR_LOG_BUF_SIZE
   1318 	int bufptr;
   1319 	int csrbuf[CSR_LOG_BUF_SIZE];
   1320 	bufptr=0;
   1321 #endif
   1322 
   1323 	SBIC_TRACE(dev);
   1324 	regs = dev->sc_sbic;
   1325 	acb = dev->sc_nexus;
   1326 
   1327 	/* Make sure pointers are OK */
   1328 	dev->sc_last = dev->sc_cur = &acb->sc_pa;
   1329 	dev->sc_tcnt = acb->sc_tcnt = 0;
   1330 	acb->sc_pa.dc_count = 0; /* No DMA */
   1331 	acb->sc_kv.dc_addr = buf;
   1332 	acb->sc_kv.dc_count = len;
   1333 
   1334 #ifdef DEBUG
   1335 	routine = 3;
   1336 	debug_sbic_regs = regs; /* store this to allow debug calls */
   1337 	if( data_pointer_debug > 1 )
   1338 		printf("sbicicmd(%d,%d):%d\n", target, lun,
   1339 		       acb->sc_kv.dc_count);
   1340 #endif
   1341 
   1342 	/*
   1343 	 * set the sbic into non-DMA mode
   1344 	 */
   1345 	SET_SBIC_control(regs, SBIC_CTL_EDI | SBIC_CTL_IDI /*| SBIC_CTL_HSP*/);
   1346 
   1347 	dev->sc_stat[0] = 0xff;
   1348 	dev->sc_msg[0] = 0xff;
   1349 	i = 1; /* pre-load */
   1350 
   1351 	/* We're stealing the SCSI bus */
   1352 	dev->sc_flags |= SBICF_ICMD;
   1353 
   1354 	do {
   1355 		/*
   1356 		 * select the SCSI bus (it's an error if bus isn't free)
   1357 		 */
   1358 		if (!( dev->sc_flags & SBICF_SELECTED )
   1359 		    && sbicselectbus(dev, regs, target, lun, dev->sc_scsiaddr)) {
   1360 			/* printf("sbicicmd: trying to select busy bus!\n"); */
   1361 			dev->sc_flags &= ~SBICF_ICMD;
   1362 			return(-1);
   1363 		}
   1364 
   1365 		/*
   1366 		 * Wait for a phase change (or error) then let the device sequence
   1367 		 * us through the various SCSI phases.
   1368 		 */
   1369 
   1370 		wait = sbic_cmd_wait;
   1371 
   1372 		GET_SBIC_asr (regs, asr);
   1373 		GET_SBIC_csr (regs, csr);
   1374 		CSR_TRACE('I',csr,asr,target);
   1375 		QPRINTF((">ASR:%02xCSR:%02x<", asr, csr));
   1376 
   1377 #if CSR_LOG_BUF_SIZE
   1378 		csrbuf[bufptr++] = csr;
   1379 #endif
   1380 
   1381 
   1382 		switch (csr) {
   1383 		case SBIC_CSR_S_XFERRED:
   1384 		case SBIC_CSR_DISC:
   1385 		case SBIC_CSR_DISC_1:
   1386 			dev->sc_flags &= ~SBICF_SELECTED;
   1387 			GET_SBIC_cmd_phase (regs, phase);
   1388 			if (phase == 0x60) {
   1389 				GET_SBIC_tlun (regs, dev->sc_stat[0]);
   1390 				i = 0; /* done */
   1391 /*				break; */ /* Bypass all the state gobldygook */
   1392 			} else {
   1393 #ifdef DEBUG
   1394 				if(reselect_debug>1)
   1395 					printf("sbicicmd: handling disconnect\n");
   1396 #endif
   1397 				i = SBIC_STATE_DISCONNECT;
   1398 			}
   1399 			break;
   1400 
   1401 		case SBIC_CSR_XFERRED|CMD_PHASE:
   1402 		case SBIC_CSR_MIS|CMD_PHASE:
   1403 		case SBIC_CSR_MIS_1|CMD_PHASE:
   1404 		case SBIC_CSR_MIS_2|CMD_PHASE:
   1405 			if (sbicxfstart(regs, clen, CMD_PHASE, sbic_cmd_wait))
   1406 				if (sbicxfout(regs, clen,
   1407 					      cbuf, CMD_PHASE))
   1408 					i = sbicabort(dev, regs, "icmd sending cmd");
   1409 #if 0
   1410 			GET_SBIC_csr(regs, csr); /* Lets us reload tcount */
   1411 			WAIT_CIP(regs);
   1412 			GET_SBIC_asr(regs, asr);
   1413 			CSR_TRACE('I',csr,asr,target);
   1414 			if( asr & (SBIC_ASR_BSY|SBIC_ASR_LCI|SBIC_ASR_CIP) )
   1415 				printf("next: cmd sent asr %02x, csr %02x\n",
   1416 				       asr, csr);
   1417 #endif
   1418 			break;
   1419 
   1420 #if 0
   1421 		case SBIC_CSR_XFERRED|DATA_OUT_PHASE:
   1422 		case SBIC_CSR_XFERRED|DATA_IN_PHASE:
   1423 		case SBIC_CSR_MIS|DATA_OUT_PHASE:
   1424 		case SBIC_CSR_MIS|DATA_IN_PHASE:
   1425 		case SBIC_CSR_MIS_1|DATA_OUT_PHASE:
   1426 		case SBIC_CSR_MIS_1|DATA_IN_PHASE:
   1427 		case SBIC_CSR_MIS_2|DATA_OUT_PHASE:
   1428 		case SBIC_CSR_MIS_2|DATA_IN_PHASE:
   1429 			if (acb->sc_kv.dc_count <= 0)
   1430 				i = sbicabort(dev, regs, "icmd out of data");
   1431 			else {
   1432 			  wait = sbic_data_wait;
   1433 			  if (sbicxfstart(regs,
   1434 					  acb->sc_kv.dc_count,
   1435 					  SBIC_PHASE(csr), wait))
   1436 			    if (csr & 0x01)
   1437 			      /* data in? */
   1438 			      i=sbicxfin(regs,
   1439 					 acb->sc_kv.dc_count,
   1440 					 acb->sc_kv.dc_addr);
   1441 			    else
   1442 			      i=sbicxfout(regs,
   1443 					  acb->sc_kv.dc_count,
   1444 					  acb->sc_kv.dc_addr,
   1445 					     SBIC_PHASE(csr));
   1446 			  acb->sc_kv.dc_addr +=
   1447 				  (acb->sc_kv.dc_count - i);
   1448 			  acb->sc_kv.dc_count = i;
   1449 			  i = 1;
   1450 			}
   1451 			break;
   1452 
   1453 #endif
   1454 		case SBIC_CSR_XFERRED|STATUS_PHASE:
   1455 		case SBIC_CSR_MIS|STATUS_PHASE:
   1456 		case SBIC_CSR_MIS_1|STATUS_PHASE:
   1457 		case SBIC_CSR_MIS_2|STATUS_PHASE:
   1458 			/*
   1459 			 * the sbic does the status/cmd-complete reading ok,
   1460 			 * so do this with its hi-level commands.
   1461 			 */
   1462 #ifdef DEBUG
   1463 			if(sbic_debug)
   1464 				printf("SBICICMD status phase\n");
   1465 #endif
   1466 			SBIC_TC_PUT(regs, 0);
   1467 			SET_SBIC_cmd_phase(regs, 0x46);
   1468 			SET_SBIC_cmd(regs, SBIC_CMD_SEL_ATN_XFER);
   1469 			break;
   1470 
   1471 #if THIS_IS_A_RESERVED_STATE
   1472 		case BUS_FREE_PHASE:		/* This is not legal */
   1473 			if( dev->sc_stat[0] != 0xff )
   1474 				goto out;
   1475 			break;
   1476 #endif
   1477 
   1478 		default:
   1479 			i = sbicnextstate(dev, csr, asr);
   1480 		}
   1481 
   1482 		/*
   1483 		 * make sure the last command was taken,
   1484 		 * ie. we're not hunting after an ignored command..
   1485 		 */
   1486 		GET_SBIC_asr(regs, asr);
   1487 
   1488 		/* tapes may take a loooong time.. */
   1489 		while (asr & SBIC_ASR_BSY){
   1490 			if(asr & SBIC_ASR_DBR) {
   1491 				printf("sbicicmd: Waiting while sbic is jammed, CSR:%02x,ASR:%02x\n",
   1492 				       csr,asr);
   1493 #ifdef DDB
   1494 				Debugger();
   1495 #endif
   1496 				/* SBIC is jammed */
   1497 				/* DUNNO which direction */
   1498 				/* Try old direction */
   1499 				GET_SBIC_data(regs,i);
   1500 				GET_SBIC_asr(regs, asr);
   1501 				if( asr & SBIC_ASR_DBR) /* Wants us to write */
   1502 					SET_SBIC_data(regs,i);
   1503 			}
   1504 			GET_SBIC_asr(regs, asr);
   1505 		}
   1506 
   1507 		/*
   1508 		 * wait for last command to complete
   1509 		 */
   1510 		if (asr & SBIC_ASR_LCI) {
   1511 			printf("sbicicmd: last command ignored\n");
   1512 		}
   1513 		else if( i == 1 ) /* Bsy */
   1514 			SBIC_WAIT (regs, SBIC_ASR_INT, wait);
   1515 
   1516 		/*
   1517 		 * do it again
   1518 		 */
   1519 	} while ( i > 0 && dev->sc_stat[0] == 0xff);
   1520 
   1521 	/* Sometimes we need to do an extra read of the CSR */
   1522 	GET_SBIC_csr(regs, csr);
   1523 	CSR_TRACE('I',csr,asr,0xff);
   1524 
   1525 #if CSR_LOG_BUF_SIZE
   1526 	if(reselect_debug>1)
   1527 		for(i=0; i<bufptr; i++)
   1528 			printf("CSR:%02x", csrbuf[i]);
   1529 #endif
   1530 
   1531 #ifdef DEBUG
   1532 	if(data_pointer_debug > 1)
   1533 		printf("sbicicmd done(%d,%d):%d =%d=\n",
   1534 		       dev->target, lun,
   1535 		       acb->sc_kv.dc_count,
   1536 		       dev->sc_stat[0]);
   1537 #endif
   1538 
   1539 	QPRINTF(("=STS:%02x=", dev->sc_stat[0]));
   1540 	dev->sc_flags &= ~SBICF_ICMD;
   1541 
   1542 	SBIC_TRACE(dev);
   1543 	return(dev->sc_stat[0]);
   1544 }
   1545 
   1546 /*
   1547  * Finish SCSI xfer command:  After the completion interrupt from
   1548  * a read/write operation, sequence through the final phases in
   1549  * programmed i/o.  This routine is a lot like sbicicmd except we
   1550  * skip (and don't allow) the select, cmd out and data in/out phases.
   1551  */
   1552 void
   1553 sbicxfdone(struct sbic_softc *dev, sbic_regmap_t regs, int target)
   1554 {
   1555 	u_char phase, asr, csr;
   1556 	int s;
   1557 
   1558 	SBIC_TRACE(dev);
   1559 	QPRINTF(("{"));
   1560 	s = splbio();
   1561 
   1562 	/*
   1563 	 * have the sbic complete on its own
   1564 	 */
   1565 	SBIC_TC_PUT(regs, 0);
   1566 	SET_SBIC_cmd_phase(regs, 0x46);
   1567 	SET_SBIC_cmd(regs, SBIC_CMD_SEL_ATN_XFER);
   1568 
   1569 	do {
   1570 		asr = SBIC_WAIT (regs, SBIC_ASR_INT, 0);
   1571 		__USE(asr);
   1572 		GET_SBIC_csr (regs, csr);
   1573 		CSR_TRACE('f',csr,asr,target);
   1574 		QPRINTF(("%02x:", csr));
   1575 	} while ((csr != SBIC_CSR_DISC) && (csr != SBIC_CSR_DISC_1)
   1576 	    && (csr != SBIC_CSR_S_XFERRED));
   1577 
   1578 	dev->sc_flags &= ~SBICF_SELECTED;
   1579 
   1580 	GET_SBIC_cmd_phase (regs, phase);
   1581 	QPRINTF(("}%02x", phase));
   1582 	if (phase == 0x60)
   1583 		GET_SBIC_tlun(regs, dev->sc_stat[0]);
   1584 	else
   1585 		sbicerror(dev, regs, csr);
   1586 
   1587 	QPRINTF(("=STS:%02x=\n", dev->sc_stat[0]));
   1588 	splx(s);
   1589 	SBIC_TRACE(dev);
   1590 }
   1591 
   1592 	/*
   1593 	 * No DMA chains
   1594 	 */
   1595 
   1596 int
   1597 sbicgo(struct sbic_softc *dev, struct scsipi_xfer *xs)
   1598 {
   1599 	int i, dmaflags, count, usedma;
   1600 	u_char csr, asr, *addr;
   1601 	sbic_regmap_t regs;
   1602 	struct sbic_acb *acb;
   1603 
   1604 	SBIC_TRACE(dev);
   1605 	dev->target = xs->xs_periph->periph_target;
   1606 	dev->lun = xs->xs_periph->periph_lun;
   1607 	acb = dev->sc_nexus;
   1608 	regs = dev->sc_sbic;
   1609 
   1610 	usedma = sbicdmaok(dev, xs);
   1611 #ifdef DEBUG
   1612 	routine = 1;
   1613 	debug_sbic_regs = regs; /* store this to allow debug calls */
   1614 	if( data_pointer_debug > 1 )
   1615 		printf("sbicgo(%d,%d)\n", dev->target, dev->lun);
   1616 #endif
   1617 
   1618 	/*
   1619 	 * set the sbic into DMA mode
   1620 	 */
   1621 	if( usedma )
   1622 		SET_SBIC_control(regs, SBIC_CTL_EDI | SBIC_CTL_IDI |
   1623 				 SBIC_MACHINE_DMA_MODE);
   1624 	else
   1625 		SET_SBIC_control(regs, SBIC_CTL_EDI | SBIC_CTL_IDI);
   1626 
   1627 
   1628 	/*
   1629 	 * select the SCSI bus (it's an error if bus isn't free)
   1630 	 */
   1631 	if (sbicselectbus(dev, regs, dev->target, dev->lun,
   1632 	    dev->sc_scsiaddr)) {
   1633 		/* printf("sbicgo: Trying to select busy bus!\n"); */
   1634 		SBIC_TRACE(dev);
   1635 		return(0); /* Not done: needs to be rescheduled */
   1636 	}
   1637 	dev->sc_stat[0] = 0xff;
   1638 
   1639 	/*
   1640 	 * Calculate DMA chains now
   1641 	 */
   1642 
   1643 	dmaflags = 0;
   1644 	if (acb->flags & ACB_DATAIN)
   1645 		dmaflags |= DMAGO_READ;
   1646 
   1647 
   1648 	/*
   1649 	 * Deal w/bounce buffers.
   1650 	 */
   1651 
   1652 	addr = acb->sc_kv.dc_addr;
   1653 	count = acb->sc_kv.dc_count;
   1654 	if (count && (char *)kvtop(addr) != acb->sc_pa.dc_addr)	{ /* XXXX check */
   1655 		printf("sbic: DMA buffer mapping changed %p->%x\n",
   1656 		    acb->sc_pa.dc_addr, (unsigned)kvtop(addr));
   1657 #ifdef DDB
   1658 		Debugger();
   1659 #endif
   1660 	}
   1661 
   1662 #ifdef DEBUG
   1663 	++sbicdma_ops;			/* count total DMA operations */
   1664 #endif
   1665 	if (count && usedma && dev->sc_flags & SBICF_BADDMA &&
   1666 	    sbiccheckdmap(addr, count, dev->sc_dmamask)) {
   1667 		/*
   1668 		 * need to bounce the DMA.
   1669 		 */
   1670 		if (dmaflags & DMAGO_READ) {
   1671 			acb->flags |= ACB_BBUF;
   1672 			acb->sc_dmausrbuf = addr;
   1673 			acb->sc_dmausrlen = count;
   1674 			acb->sc_usrbufpa = (u_char *)kvtop(addr);
   1675 			if(!dev->sc_tinfo[dev->target].bounce) {
   1676 				printf("sbicgo: HELP! no bounce allocated for %d\n",
   1677 				       dev->target);
   1678 				printf("xfer: (%p->%p,%lx)\n", acb->sc_dmausrbuf,
   1679 				       acb->sc_usrbufpa, acb->sc_dmausrlen);
   1680 				dev->sc_tinfo[xs->xs_periph->periph_target].bounce
   1681 					= (char *)alloc_z2mem(MAXPHYS);
   1682 				if (isztwomem(dev->sc_tinfo[xs->xs_periph->periph_target].bounce))
   1683 					printf("alloc ZII target %d bounce pa 0x%x\n",
   1684 					       xs->xs_periph->periph_target,
   1685 					       (unsigned)kvtop(dev->sc_tinfo[xs->xs_periph->periph_target].bounce));
   1686 				else if (dev->sc_tinfo[xs->xs_periph->periph_target].bounce)
   1687 					printf("alloc CHIP target %d bounce pa %p\n",
   1688 					       xs->xs_periph->periph_target,
   1689 					       PREP_DMA_MEM(dev->sc_tinfo[xs->xs_periph->periph_target].bounce));
   1690 
   1691 				printf("Allocating %d bounce at %x\n",
   1692 				       dev->target,
   1693 				       (unsigned)kvtop(dev->sc_tinfo[dev->target].bounce));
   1694 			}
   1695 		} else {	/* write: copy to DMA buffer */
   1696 #ifdef DEBUG
   1697 			if(data_pointer_debug)
   1698 			printf("sbicgo: copying %x bytes to target %d bounce %x\n",
   1699 			       count, dev->target,
   1700 			       (unsigned)kvtop(dev->sc_tinfo[dev->target].bounce));
   1701 #endif
   1702 			bcopy (addr, dev->sc_tinfo[dev->target].bounce, count);
   1703 		}
   1704 		addr = dev->sc_tinfo[dev->target].bounce;/* and use DMA buffer */
   1705 		acb->sc_kv.dc_addr = addr;
   1706 #ifdef DEBUG
   1707 		++sbicdma_bounces;		/* count number of bounced */
   1708 #endif
   1709 	}
   1710 
   1711 	/*
   1712 	 * Allocate the DMA chain
   1713 	 */
   1714 
   1715 	/* Set start KVM addresses */
   1716 #if 0
   1717 	acb->sc_kv.dc_addr = addr;
   1718 	acb->sc_kv.dc_count = count;
   1719 #endif
   1720 
   1721 	/* Mark end of segment */
   1722 	acb->sc_tcnt = dev->sc_tcnt = 0;
   1723 	acb->sc_pa.dc_count = 0;
   1724 
   1725 	sbic_load_ptrs(dev, regs, dev->target, dev->lun);
   1726 	SBIC_TRACE(dev);
   1727 	/* Enable interrupts but don't do any DMA */
   1728 	dev->sc_enintr(dev);
   1729 	if (usedma) {
   1730 		dev->sc_tcnt = dev->sc_dmago(dev, acb->sc_pa.dc_addr,
   1731 		    acb->sc_pa.dc_count,
   1732 		    dmaflags);
   1733 #ifdef DEBUG
   1734 		dev->sc_dmatimo = dev->sc_tcnt ? 1 : 0;
   1735 #endif
   1736         } else
   1737 		dev->sc_dmacmd = 0; /* Don't use DMA */
   1738 	dev->sc_flags |= SBICF_INDMA;
   1739 /*	SBIC_TC_PUT(regs, dev->sc_tcnt); */ /* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
   1740 	SBIC_TRACE(dev);
   1741 	sbic_save_ptrs(dev, regs, dev->target, dev->lun);
   1742 
   1743 	/*
   1744 	 * push the data cache ( I think this won't work (EH))
   1745 	 */
   1746 #if defined(M68040) || defined(M68060)
   1747 	if (mmutype == MMU_68040 && usedma && count) {
   1748 		dma_cachectl(addr, count);
   1749 		if (((u_int)addr & 0xF) || (((u_int)addr + count) & 0xF))
   1750 			dev->sc_flags |= SBICF_DCFLUSH;
   1751 	}
   1752 #endif
   1753 #ifdef __powerpc__
   1754 	dma_cachectl(addr, count);
   1755 #endif
   1756 
   1757 	/*
   1758 	 * enintr() also enables interrupts for the sbic
   1759 	 */
   1760 #ifdef DEBUG
   1761 	if( data_pointer_debug > 1 )
   1762 		printf("sbicgo dmago:%d(%p:%lx)\n",
   1763 		       dev->target,dev->sc_cur->dc_addr,dev->sc_tcnt);
   1764 #if 0
   1765 	/*
   1766 	 * Hmm - this isn't right:  asr and csr haven't been set yet.
   1767 	 */
   1768 	debug_asr = asr;
   1769 	debug_csr = csr;
   1770 #endif
   1771 #endif
   1772 
   1773 	/*
   1774 	 * Lets cycle a while then let the interrupt handler take over
   1775 	 */
   1776 
   1777 	GET_SBIC_asr(regs, asr);
   1778 	do {
   1779 		GET_SBIC_csr(regs, csr);
   1780 		CSR_TRACE('g',csr,asr,dev->target);
   1781 #ifdef DEBUG
   1782 		debug_csr = csr;
   1783 		routine = 1;
   1784 #endif
   1785 		QPRINTF(("go[0x%x]", csr));
   1786 
   1787 		i = sbicnextstate(dev, csr, asr);
   1788 
   1789 		WAIT_CIP(regs);
   1790 		GET_SBIC_asr(regs, asr);
   1791 #ifdef DEBUG
   1792 		debug_asr = asr;
   1793 #endif
   1794 		if(asr & SBIC_ASR_LCI) printf("sbicgo: LCI asr:%02x csr:%02x\n",
   1795 					      asr,csr);
   1796 	} while( i == SBIC_STATE_RUNNING
   1797 		&& asr & (SBIC_ASR_INT|SBIC_ASR_LCI) );
   1798 
   1799 	CSR_TRACE('g',csr,asr,i<<4);
   1800 	SBIC_TRACE(dev);
   1801 if (i == SBIC_STATE_DONE && dev->sc_stat[0] == 0xff) printf("sbicgo: done & stat = 0xff\n");
   1802 	if (i == SBIC_STATE_DONE && dev->sc_stat[0] != 0xff) {
   1803 /*	if( i == SBIC_STATE_DONE && dev->sc_stat[0] ) { */
   1804 		/* Did we really finish that fast? */
   1805 		return 1;
   1806 	}
   1807 	return 0;
   1808 }
   1809 
   1810 
   1811 int
   1812 sbicintr(struct sbic_softc *dev)
   1813 {
   1814 	sbic_regmap_t regs;
   1815 	u_char asr, csr;
   1816 	int i;
   1817 
   1818 	regs = dev->sc_sbic;
   1819 
   1820 	/*
   1821 	 * pending interrupt?
   1822 	 */
   1823 	GET_SBIC_asr (regs, asr);
   1824 	if ((asr & SBIC_ASR_INT) == 0)
   1825 		return(0);
   1826 
   1827 	SBIC_TRACE(dev);
   1828 	do {
   1829 		GET_SBIC_csr(regs, csr);
   1830 		CSR_TRACE('i',csr,asr,dev->target);
   1831 #ifdef DEBUG
   1832 		debug_csr = csr;
   1833 		routine = 2;
   1834 #endif
   1835 		QPRINTF(("intr[0x%x]", csr));
   1836 
   1837 		i = sbicnextstate(dev, csr, asr);
   1838 
   1839 		WAIT_CIP(regs);
   1840 		GET_SBIC_asr(regs, asr);
   1841 #ifdef DEBUG
   1842 		debug_asr = asr;
   1843 #endif
   1844 #if 0
   1845 		if(asr & SBIC_ASR_LCI) printf("sbicintr: LCI asr:%02x csr:%02x\n",
   1846 					      asr,csr);
   1847 #endif
   1848 	} while(i == SBIC_STATE_RUNNING &&
   1849 		asr & (SBIC_ASR_INT|SBIC_ASR_LCI));
   1850 	CSR_TRACE('i',csr,asr,i<<4);
   1851 	SBIC_TRACE(dev);
   1852 	return(1);
   1853 }
   1854 
   1855 /*
   1856  * Run commands and wait for disconnect
   1857  */
   1858 int
   1859 sbicpoll(struct sbic_softc *dev)
   1860 {
   1861 	sbic_regmap_t regs;
   1862 	u_char asr, csr;
   1863 	int i;
   1864 
   1865 	SBIC_TRACE(dev);
   1866 	regs = dev->sc_sbic;
   1867 
   1868 	do {
   1869 		GET_SBIC_asr (regs, asr);
   1870 #ifdef DEBUG
   1871 		debug_asr = asr;
   1872 #endif
   1873 		GET_SBIC_csr(regs, csr);
   1874 		CSR_TRACE('p',csr,asr,dev->target);
   1875 #ifdef DEBUG
   1876 		debug_csr = csr;
   1877 		routine = 2;
   1878 #endif
   1879 		QPRINTF(("poll[0x%x]", csr));
   1880 
   1881 		i = sbicnextstate(dev, csr, asr);
   1882 
   1883 		WAIT_CIP(regs);
   1884 		GET_SBIC_asr(regs, asr);
   1885 		/* tapes may take a loooong time.. */
   1886 		while (asr & SBIC_ASR_BSY){
   1887 			if(asr & SBIC_ASR_DBR) {
   1888 				printf("sbipoll: Waiting while sbic is jammed, CSR:%02x,ASR:%02x\n",
   1889 				       csr,asr);
   1890 #ifdef DDB
   1891 				Debugger();
   1892 #endif
   1893 				/* SBIC is jammed */
   1894 				/* DUNNO which direction */
   1895 				/* Try old direction */
   1896 				GET_SBIC_data(regs,i);
   1897 				GET_SBIC_asr(regs, asr);
   1898 				if( asr & SBIC_ASR_DBR) /* Wants us to write */
   1899 					SET_SBIC_data(regs,i);
   1900 			}
   1901 			GET_SBIC_asr(regs, asr);
   1902 		}
   1903 
   1904 		if(asr & SBIC_ASR_LCI) printf("sbicpoll: LCI asr:%02x csr:%02x\n",
   1905 					      asr,csr);
   1906 		else if( i == 1 ) /* BSY */
   1907 			SBIC_WAIT(regs, SBIC_ASR_INT, sbic_cmd_wait);
   1908 	} while(i == SBIC_STATE_RUNNING);
   1909 	CSR_TRACE('p',csr,asr,i<<4);
   1910 	SBIC_TRACE(dev);
   1911 	return(1);
   1912 }
   1913 
   1914 /*
   1915  * Handle a single msgin
   1916  */
   1917 
   1918 int
   1919 sbicmsgin(struct sbic_softc *dev)
   1920 {
   1921 	sbic_regmap_t regs;
   1922 	int recvlen;
   1923 	u_char asr, csr, *tmpaddr;
   1924 
   1925 	regs = dev->sc_sbic;
   1926 
   1927 	dev->sc_msg[0] = 0xff;
   1928 	dev->sc_msg[1] = 0xff;
   1929 
   1930 	GET_SBIC_asr(regs, asr);
   1931 #ifdef DEBUG
   1932 	if(reselect_debug>1)
   1933 		printf("sbicmsgin asr=%02x\n", asr);
   1934 #endif
   1935 
   1936 	sbic_save_ptrs(dev, regs, dev->target, dev->lun);
   1937 
   1938 	GET_SBIC_selid (regs, csr);
   1939 	SET_SBIC_selid (regs, csr | SBIC_SID_FROM_SCSI);
   1940 
   1941 	SBIC_TC_PUT(regs, 0);
   1942 	tmpaddr = dev->sc_msg;
   1943 	recvlen = 1;
   1944 	do {
   1945 		while( recvlen-- ) {
   1946 			GET_SBIC_asr(regs, asr);
   1947 			GET_SBIC_csr(regs, csr);
   1948 			QPRINTF(("sbicmsgin ready to go (csr,asr)=(%02x,%02x)\n",
   1949 				 csr, asr));
   1950 
   1951 			RECV_BYTE(regs, *tmpaddr);
   1952 			CSR_TRACE('m',csr,asr,*tmpaddr);
   1953 #if 1
   1954 			/*
   1955 			 * get the command completion interrupt, or we
   1956 			 * can't send a new command (LCI)
   1957 			 */
   1958 			SBIC_WAIT(regs, SBIC_ASR_INT, 0);
   1959 			GET_SBIC_csr(regs, csr);
   1960 			CSR_TRACE('X',csr,asr,dev->target);
   1961 #else
   1962 			WAIT_CIP(regs);
   1963 			do {
   1964 				GET_SBIC_asr(regs, asr);
   1965 				csr = 0xff;
   1966 				GET_SBIC_csr(regs, csr);
   1967 				CSR_TRACE('X',csr,asr,dev->target);
   1968 				if( csr == 0xff )
   1969 					printf("sbicmsgin waiting: csr %02x asr %02x\n", csr, asr);
   1970 			} while( csr == 0xff );
   1971 #endif
   1972 #ifdef DEBUG
   1973 			if(reselect_debug>1)
   1974 				printf("sbicmsgin: got %02x csr %02x asr %02x\n",
   1975 				       *tmpaddr, csr, asr);
   1976 #endif
   1977 #if do_parity_check
   1978 			if( asr & SBIC_ASR_PE ) {
   1979 				printf ("Parity error");
   1980 				/* This code simply does not work. */
   1981 				WAIT_CIP(regs);
   1982 				SET_SBIC_cmd(regs, SBIC_CMD_SET_ATN);
   1983 				WAIT_CIP(regs);
   1984 				GET_SBIC_asr(regs, asr);
   1985 				WAIT_CIP(regs);
   1986 				SET_SBIC_cmd(regs, SBIC_CMD_CLR_ACK);
   1987 				WAIT_CIP(regs);
   1988 				if( !(asr & SBIC_ASR_LCI) )
   1989 					/* Target wants to send garbled msg*/
   1990 					continue;
   1991 				printf("--fixing\n");
   1992 				/* loop until a msgout phase occurs on target */
   1993 				while(csr & 0x07 != MESG_OUT_PHASE) {
   1994 					while( asr & SBIC_ASR_BSY &&
   1995 					      !(asr & SBIC_ASR_DBR|SBIC_ASR_INT) )
   1996 						GET_SBIC_asr(regs, asr);
   1997 					if( asr & SBIC_ASR_DBR )
   1998 						panic("msgin: jammed again!");
   1999 					GET_SBIC_csr(regs, csr);
   2000 					CSR_TRACE('e',csr,asr,dev->target);
   2001 					if( csr & 0x07 != MESG_OUT_PHASE ) {
   2002 						sbicnextstate(dev, csr, asr);
   2003 						sbic_save_ptrs(dev, regs,
   2004 							       dev->target,
   2005 							       dev->lun);
   2006 					}
   2007 				}
   2008 				/* Should be msg out by now */
   2009 				SEND_BYTE(regs, MSG_PARITY_ERROR);
   2010 			}
   2011 			else
   2012 #endif
   2013 				tmpaddr++;
   2014 
   2015 			if(recvlen) {
   2016 				/* Clear ACK */
   2017 				WAIT_CIP(regs);
   2018 				GET_SBIC_asr(regs, asr);
   2019 				GET_SBIC_csr(regs, csr);
   2020 				CSR_TRACE('X',csr,asr,dev->target);
   2021 				QPRINTF(("sbicmsgin pre byte CLR_ACK (csr,asr)=(%02x,%02x)\n",
   2022 					 csr, asr));
   2023 				SET_SBIC_cmd(regs, SBIC_CMD_CLR_ACK);
   2024 				SBIC_WAIT(regs, SBIC_ASR_INT, 0);
   2025 			}
   2026 
   2027 		};
   2028 
   2029 		if(dev->sc_msg[0] == 0xff) {
   2030 			printf("sbicmsgin: sbic swallowed our message\n");
   2031 			break;
   2032 		}
   2033 #ifdef DEBUG
   2034 		if (sync_debug)
   2035 			printf("msgin done csr 0x%x asr 0x%x msg 0x%x\n",
   2036 			       csr, asr, dev->sc_msg[0]);
   2037 #endif
   2038 		/*
   2039 		 * test whether this is a reply to our sync
   2040 		 * request
   2041 		 */
   2042 		if (MSG_ISIDENTIFY(dev->sc_msg[0])) {
   2043 			QPRINTF(("IFFY"));
   2044 #if 0
   2045 			/* There is an implied load-ptrs here */
   2046 			sbic_load_ptrs(dev, regs, dev->target, dev->lun);
   2047 #endif
   2048 			/* Got IFFY msg -- ack it */
   2049 		} else if (dev->sc_msg[0] == MSG_REJECT
   2050 			   && dev->sc_sync[dev->target].state == SYNC_SENT) {
   2051 			QPRINTF(("REJECT of SYN"));
   2052 #ifdef DEBUG
   2053 			if (sync_debug)
   2054 				printf("target %d rejected sync, going async\n",
   2055 				       dev->target);
   2056 #endif
   2057 			dev->sc_sync[dev->target].period = sbic_min_period;
   2058 			dev->sc_sync[dev->target].offset = 0;
   2059 			dev->sc_sync[dev->target].state = SYNC_DONE;
   2060 			SET_SBIC_syn(regs,
   2061 				     SBIC_SYN(dev->sc_sync[dev->target].offset,
   2062 					      dev->sc_sync[dev->target].period));
   2063 		} else if (dev->sc_msg[0] == MSG_REJECT) {
   2064 			QPRINTF(("REJECT"));
   2065 			/*
   2066 			 * we'll never REJECt a REJECT message..
   2067 			 */
   2068 		} else if (dev->sc_msg[0] == MSG_SAVE_DATA_PTR) {
   2069 			QPRINTF(("MSG_SAVE_DATA_PTR"));
   2070 			/*
   2071 			 * don't reject this either.
   2072 			 */
   2073 		} else if (dev->sc_msg[0] == MSG_DISCONNECT) {
   2074 			QPRINTF(("DISCONNECT"));
   2075 #ifdef DEBUG
   2076 			if( reselect_debug>1 && dev->sc_msg[0] == MSG_DISCONNECT )
   2077 				printf("sbicmsgin: got disconnect msg %s\n",
   2078 				       (dev->sc_flags & SBICF_ICMD)?"rejecting":"");
   2079 #endif
   2080 			if( dev->sc_flags & SBICF_ICMD ) {
   2081 				/* We're in immediate mode. Prevent disconnects. */
   2082 				/* prepare to reject the message, NACK */
   2083 				SET_SBIC_cmd(regs, SBIC_CMD_SET_ATN);
   2084 				WAIT_CIP(regs);
   2085 			}
   2086 		} else if (dev->sc_msg[0] == MSG_CMD_COMPLETE ) {
   2087 			QPRINTF(("CMD_COMPLETE"));
   2088 			/* !! KLUDGE ALERT !! quite a few drives don't seem to
   2089 			 * really like the current way of sending the
   2090 			 * sync-handshake together with the ident-message, and
   2091 			 * they react by sending command-complete and
   2092 			 * disconnecting right after returning the valid sync
   2093 			 * handshake. So, all I can do is reselect the drive,
   2094 			 * and hope it won't disconnect again. I don't think
   2095 			 * this is valid behavior, but I can't help fixing a
   2096 			 * problem that apparently exists.
   2097 			 *
   2098 			 * Note: we should not get here on `normal' command
   2099 			 * completion, as that condition is handled by the
   2100 			 * high-level sel&xfer resume command used to walk
   2101 			 * thru status/cc-phase.
   2102 			 */
   2103 
   2104 #ifdef DEBUG
   2105 			if (sync_debug)
   2106 				printf ("GOT MSG %d! target %d acting weird.."
   2107 					" waiting for disconnect...\n",
   2108 					dev->sc_msg[0], dev->target);
   2109 #endif
   2110 			/* Check to see if sbic is handling this */
   2111 			GET_SBIC_asr(regs, asr);
   2112 			if(asr & SBIC_ASR_BSY)
   2113 				return SBIC_STATE_RUNNING;
   2114 
   2115 			/* Let's try this: Assume it works and set status to 00 */
   2116 			dev->sc_stat[0] = 0;
   2117 		} else if (dev->sc_msg[0] == MSG_EXT_MESSAGE
   2118 			   && tmpaddr == &dev->sc_msg[1]) {
   2119 			QPRINTF(("ExtMSG\n"));
   2120 			/* Read in whole extended message */
   2121 			SET_SBIC_cmd(regs, SBIC_CMD_CLR_ACK);
   2122 			SBIC_WAIT(regs, SBIC_ASR_INT, 0);
   2123 			GET_SBIC_asr(regs, asr);
   2124 			GET_SBIC_csr(regs, csr);
   2125 			QPRINTF(("CLR ACK asr %02x, csr %02x\n", asr, csr));
   2126 			RECV_BYTE(regs, *tmpaddr);
   2127 			CSR_TRACE('x',csr,asr,*tmpaddr);
   2128 			/* Wait for command completion IRQ */
   2129 			SBIC_WAIT(regs, SBIC_ASR_INT, 0);
   2130 			recvlen = *tmpaddr++;
   2131 			QPRINTF(("Recving ext msg, asr %02x csr %02x len %02x\n",
   2132 			       asr, csr, recvlen));
   2133 		} else if (dev->sc_msg[0] == MSG_EXT_MESSAGE && dev->sc_msg[1] == 3
   2134 			   && dev->sc_msg[2] == MSG_SYNC_REQ) {
   2135 			QPRINTF(("SYN"));
   2136 			dev->sc_sync[dev->target].period =
   2137 				sbicfromscsiperiod(dev,
   2138 						   regs, dev->sc_msg[3]);
   2139 			dev->sc_sync[dev->target].offset = dev->sc_msg[4];
   2140 			dev->sc_sync[dev->target].state = SYNC_DONE;
   2141 			SET_SBIC_syn(regs,
   2142 				     SBIC_SYN(dev->sc_sync[dev->target].offset,
   2143 					      dev->sc_sync[dev->target].period));
   2144 			printf("%s: target %d now synchronous,"
   2145 			       " period=%dns, offset=%d.\n",
   2146 			       device_xname(dev->sc_dev), dev->target,
   2147 			       dev->sc_msg[3] * 4, dev->sc_msg[4]);
   2148 		} else {
   2149 #ifdef DEBUG
   2150 			if (sbic_debug || sync_debug)
   2151 				printf ("sbicmsgin: Rejecting message 0x%02x\n",
   2152 					dev->sc_msg[0]);
   2153 #endif
   2154 			/* prepare to reject the message, NACK */
   2155 			SET_SBIC_cmd(regs, SBIC_CMD_SET_ATN);
   2156 			WAIT_CIP(regs);
   2157 		}
   2158 		/* Clear ACK */
   2159 		WAIT_CIP(regs);
   2160 		GET_SBIC_asr(regs, asr);
   2161 		GET_SBIC_csr(regs, csr);
   2162 		CSR_TRACE('X',csr,asr,dev->target);
   2163 		QPRINTF(("sbicmsgin pre CLR_ACK (csr,asr)=(%02x,%02x)%d\n",
   2164 			 csr, asr, recvlen));
   2165 		SET_SBIC_cmd(regs, SBIC_CMD_CLR_ACK);
   2166 		SBIC_WAIT(regs, SBIC_ASR_INT, 0);
   2167 	}
   2168 #if 0
   2169 	while((csr == SBIC_CSR_MSGIN_W_ACK)
   2170 	      || (SBIC_PHASE(csr) == MESG_IN_PHASE));
   2171 #else
   2172 	while (recvlen>0);
   2173 #endif
   2174 
   2175 	QPRINTF(("sbicmsgin finished: csr %02x, asr %02x\n",csr, asr));
   2176 
   2177 	/* Should still have one CSR to read */
   2178 	return SBIC_STATE_RUNNING;
   2179 }
   2180 
   2181 
   2182 /*
   2183  * sbicnextstate()
   2184  * return:
   2185  *		0  == done
   2186  *		1  == working
   2187  *		2  == disconnected
   2188  *		-1 == error
   2189  */
   2190 int
   2191 sbicnextstate(struct sbic_softc *dev, u_char csr, u_char asr)
   2192 {
   2193 	sbic_regmap_t regs;
   2194 	struct sbic_acb *acb;
   2195 	int i, newtarget, newlun, wait;
   2196 #if 0
   2197 	unsigned tcnt;
   2198 #endif
   2199 
   2200 	i = 0;
   2201 	SBIC_TRACE(dev);
   2202 	regs = dev->sc_sbic;
   2203 	acb = dev->sc_nexus;
   2204 
   2205 	QPRINTF(("next[%02x,%02x]",asr,csr));
   2206 
   2207 	switch (csr) {
   2208 	case SBIC_CSR_XFERRED|CMD_PHASE:
   2209 	case SBIC_CSR_MIS|CMD_PHASE:
   2210 	case SBIC_CSR_MIS_1|CMD_PHASE:
   2211 	case SBIC_CSR_MIS_2|CMD_PHASE:
   2212 		sbic_save_ptrs(dev, regs, dev->target, dev->lun);
   2213 		if (sbicxfstart(regs, acb->clen, CMD_PHASE, sbic_cmd_wait))
   2214 			if (sbicxfout(regs, acb->clen,
   2215 				      &acb->cmd, CMD_PHASE))
   2216 				goto abort;
   2217 		break;
   2218 
   2219 	case SBIC_CSR_XFERRED|STATUS_PHASE:
   2220 	case SBIC_CSR_MIS|STATUS_PHASE:
   2221 	case SBIC_CSR_MIS_1|STATUS_PHASE:
   2222 	case SBIC_CSR_MIS_2|STATUS_PHASE:
   2223 		/*
   2224 		 * this should be the normal i/o completion case.
   2225 		 * get the status & cmd complete msg then let the
   2226 		 * device driver look at what happened.
   2227 		 */
   2228 		sbicxfdone(dev,regs,dev->target);
   2229 		/*
   2230 		 * check for overlapping cache line, flush if so
   2231 		 */
   2232 #if defined(M68040) || defined(M68060)
   2233 		if (dev->sc_flags & SBICF_DCFLUSH) {
   2234 #if 0
   2235 			printf("sbic: 68040/68060 DMA cache flush needs"
   2236 			    "fixing? %x:%x\n",
   2237 			    dev->sc_xs->data, dev->sc_xs->datalen);
   2238 #endif
   2239 		}
   2240 #endif
   2241 #ifdef DEBUG
   2242 		if( data_pointer_debug > 1 )
   2243 			printf("next dmastop: %d(%p:%lx)\n",
   2244 			       dev->target,dev->sc_cur->dc_addr,dev->sc_tcnt);
   2245 		dev->sc_dmatimo = 0;
   2246 #endif
   2247 		dev->sc_dmastop(dev); /* was dmafree */
   2248 		if (acb->flags & ACB_BBUF) {
   2249 			if ((u_char *)kvtop(acb->sc_dmausrbuf) != acb->sc_usrbufpa)
   2250 				printf("%s: WARNING - buffer mapping changed %p->%x\n",
   2251 				    device_xname(dev->sc_dev), acb->sc_usrbufpa,
   2252 				    (unsigned)kvtop(acb->sc_dmausrbuf));
   2253 #ifdef DEBUG
   2254 			if(data_pointer_debug)
   2255 			printf("sbicgo:copying %lx bytes from target %d bounce %x\n",
   2256 			       acb->sc_dmausrlen,
   2257 			       dev->target,
   2258 			       (unsigned)kvtop(dev->sc_tinfo[dev->target].bounce));
   2259 #endif
   2260 			bcopy(dev->sc_tinfo[dev->target].bounce,
   2261 			      acb->sc_dmausrbuf,
   2262 			      acb->sc_dmausrlen);
   2263 		}
   2264 		dev->sc_flags &= ~(SBICF_INDMA | SBICF_DCFLUSH);
   2265 		sbic_scsidone(acb, dev->sc_stat[0]);
   2266 		SBIC_TRACE(dev);
   2267 		return SBIC_STATE_DONE;
   2268 
   2269 	case SBIC_CSR_XFERRED|DATA_OUT_PHASE:
   2270 	case SBIC_CSR_XFERRED|DATA_IN_PHASE:
   2271 	case SBIC_CSR_MIS|DATA_OUT_PHASE:
   2272 	case SBIC_CSR_MIS|DATA_IN_PHASE:
   2273 	case SBIC_CSR_MIS_1|DATA_OUT_PHASE:
   2274 	case SBIC_CSR_MIS_1|DATA_IN_PHASE:
   2275 	case SBIC_CSR_MIS_2|DATA_OUT_PHASE:
   2276 	case SBIC_CSR_MIS_2|DATA_IN_PHASE:
   2277 		if( dev->sc_xs->xs_control & XS_CTL_POLL || dev->sc_flags & SBICF_ICMD
   2278 		   || acb->sc_dmacmd == 0 ) {
   2279 			/* Do PIO */
   2280 			SET_SBIC_control(regs, SBIC_CTL_EDI | SBIC_CTL_IDI);
   2281 			if (acb->sc_kv.dc_count <= 0) {
   2282 				printf("sbicnextstate:xfer count %d asr%x csr%x\n",
   2283 				       acb->sc_kv.dc_count, asr, csr);
   2284 				goto abort;
   2285 			}
   2286 			wait = sbic_data_wait;
   2287 			if( sbicxfstart(regs,
   2288 					acb->sc_kv.dc_count,
   2289 					SBIC_PHASE(csr), wait)) {
   2290 				if( SBIC_PHASE(csr) == DATA_IN_PHASE )
   2291 					/* data in? */
   2292 					i=sbicxfin(regs,
   2293 						   acb->sc_kv.dc_count,
   2294 						   acb->sc_kv.dc_addr);
   2295 				else
   2296 					i=sbicxfout(regs,
   2297 						    acb->sc_kv.dc_count,
   2298 						    acb->sc_kv.dc_addr,
   2299 						    SBIC_PHASE(csr));
   2300 			}
   2301 			acb->sc_kv.dc_addr +=
   2302 				(acb->sc_kv.dc_count - i);
   2303 			acb->sc_kv.dc_count = i;
   2304 		} else {
   2305 			if (acb->sc_kv.dc_count <= 0) {
   2306 				printf("sbicnextstate:xfer count %d asr%x csr%x\n",
   2307 				       acb->sc_kv.dc_count, asr, csr);
   2308 				goto abort;
   2309 			}
   2310 			/*
   2311 			 * do scatter-gather DMA
   2312 			 * hacking the controller chip, ouch..
   2313 			 */
   2314 			SET_SBIC_control(regs, SBIC_CTL_EDI | SBIC_CTL_IDI |
   2315 					 SBIC_MACHINE_DMA_MODE);
   2316 			/*
   2317 			 * set next DMA addr and dec count
   2318 			 */
   2319 #if 0
   2320 			SBIC_TC_GET(regs, tcnt);
   2321 			dev->sc_cur->dc_count -= ((dev->sc_tcnt - tcnt) >> 1);
   2322 			dev->sc_cur->dc_addr += (dev->sc_tcnt - tcnt);
   2323 			dev->sc_tcnt = acb->sc_tcnt = tcnt;
   2324 #else
   2325 			sbic_save_ptrs(dev, regs, dev->target, dev->lun);
   2326 			sbic_load_ptrs(dev, regs, dev->target, dev->lun);
   2327 #endif
   2328 #ifdef DEBUG
   2329 			if( data_pointer_debug > 1 )
   2330 				printf("next dmanext: %d(%p:%lx)\n",
   2331 				       dev->target,dev->sc_cur->dc_addr,
   2332 				       dev->sc_tcnt);
   2333 			dev->sc_dmatimo = 1;
   2334 #endif
   2335 			dev->sc_tcnt = dev->sc_dmanext(dev);
   2336 			SBIC_TC_PUT(regs, (unsigned)dev->sc_tcnt);
   2337 			SET_SBIC_cmd(regs, SBIC_CMD_XFER_INFO);
   2338 			dev->sc_flags |= SBICF_INDMA;
   2339 		}
   2340 		break;
   2341 
   2342 	case SBIC_CSR_XFERRED|MESG_IN_PHASE:
   2343 	case SBIC_CSR_MIS|MESG_IN_PHASE:
   2344 	case SBIC_CSR_MIS_1|MESG_IN_PHASE:
   2345 	case SBIC_CSR_MIS_2|MESG_IN_PHASE:
   2346 		SBIC_TRACE(dev);
   2347 		return sbicmsgin(dev);
   2348 
   2349 	case SBIC_CSR_MSGIN_W_ACK:
   2350 		SET_SBIC_cmd(regs, SBIC_CMD_CLR_ACK); /* Dunno what I'm ACKing */
   2351 		printf("Acking unknown msgin CSR:%02x",csr);
   2352 		break;
   2353 
   2354 	case SBIC_CSR_XFERRED|MESG_OUT_PHASE:
   2355 	case SBIC_CSR_MIS|MESG_OUT_PHASE:
   2356 	case SBIC_CSR_MIS_1|MESG_OUT_PHASE:
   2357 	case SBIC_CSR_MIS_2|MESG_OUT_PHASE:
   2358 #ifdef DEBUG
   2359 		if (sync_debug)
   2360 			printf ("sending REJECT msg to last msg.\n");
   2361 #endif
   2362 
   2363 		sbic_save_ptrs(dev, regs, dev->target, dev->lun);
   2364 		/*
   2365 		 * should only get here on reject,
   2366 		 * since it's always US that
   2367 		 * initiate a sync transfer
   2368 		 */
   2369 		SEND_BYTE(regs, MSG_REJECT);
   2370 		WAIT_CIP(regs);
   2371 		if( asr & (SBIC_ASR_BSY|SBIC_ASR_LCI|SBIC_ASR_CIP) )
   2372 			printf("next: REJECT sent asr %02x\n", asr);
   2373 		SBIC_TRACE(dev);
   2374 		return SBIC_STATE_RUNNING;
   2375 
   2376 	case SBIC_CSR_DISC:
   2377 	case SBIC_CSR_DISC_1:
   2378 		dev->sc_flags &= ~(SBICF_INDMA|SBICF_SELECTED);
   2379 
   2380 		/* Try to schedule another target */
   2381 #ifdef DEBUG
   2382 		if(reselect_debug>1)
   2383 			printf("sbicnext target %d disconnected\n", dev->target);
   2384 #endif
   2385 		TAILQ_INSERT_HEAD(&dev->nexus_list, acb, chain);
   2386 		++dev->sc_tinfo[dev->target].dconns;
   2387 		dev->sc_nexus = NULL;
   2388 		dev->sc_xs = NULL;
   2389 
   2390 		if( acb->xs->xs_control & XS_CTL_POLL
   2391 		   || (dev->sc_flags & SBICF_ICMD)
   2392 		   || !sbic_parallel_operations ) {
   2393 			SBIC_TRACE(dev);
   2394 			return SBIC_STATE_DISCONNECT;
   2395 		}
   2396 		sbic_sched(dev);
   2397 		SBIC_TRACE(dev);
   2398 		return SBIC_STATE_DISCONNECT;
   2399 
   2400 	case SBIC_CSR_RSLT_NI:
   2401 	case SBIC_CSR_RSLT_IFY:
   2402 		GET_SBIC_rselid(regs, newtarget);
   2403 		/* check SBIC_RID_SIV? */
   2404 		newtarget &= SBIC_RID_MASK;
   2405 		if (csr == SBIC_CSR_RSLT_IFY) {
   2406 			/* Read IFY msg to avoid lockup */
   2407 			GET_SBIC_data(regs, newlun);
   2408 			WAIT_CIP(regs);
   2409 			newlun &= SBIC_TLUN_MASK;
   2410 			CSR_TRACE('r',csr,asr,newtarget);
   2411 		} else {
   2412 			/* Need to get IFY message */
   2413 			for (newlun = 256; newlun; --newlun) {
   2414 				GET_SBIC_asr(regs, asr);
   2415 				if (asr & SBIC_ASR_INT)
   2416 					break;
   2417 				delay(1);
   2418 			}
   2419 			newlun = 0;	/* XXXX */
   2420 			if ((asr & SBIC_ASR_INT) == 0) {
   2421 #ifdef DEBUG
   2422 				if (reselect_debug)
   2423 					printf("RSLT_NI - no IFFY message? asr %x\n", asr);
   2424 #endif
   2425 			} else {
   2426 				GET_SBIC_csr(regs,csr);
   2427 				CSR_TRACE('n',csr,asr,newtarget);
   2428 				if (csr == (SBIC_CSR_MIS | MESG_IN_PHASE) ||
   2429 				    csr == (SBIC_CSR_MIS_1 | MESG_IN_PHASE) ||
   2430 				    csr == (SBIC_CSR_MIS_2 | MESG_IN_PHASE)) {
   2431 					sbicmsgin(dev);
   2432 					newlun = dev->sc_msg[0] & 7;
   2433 				} else {
   2434 					printf("RSLT_NI - not MESG_IN_PHASE %x\n",
   2435 					    csr);
   2436 				}
   2437 			}
   2438 		}
   2439 #ifdef DEBUG
   2440 		if(reselect_debug>1 || (reselect_debug && csr==SBIC_CSR_RSLT_NI))
   2441 			printf("sbicnext: reselect %s from targ %d lun %d\n",
   2442 			    csr == SBIC_CSR_RSLT_NI ? "NI" : "IFY",
   2443 			    newtarget, newlun);
   2444 #endif
   2445 		if (dev->sc_nexus) {
   2446 #ifdef DEBUG
   2447 			if (reselect_debug > 1)
   2448 				printf("%s: reselect %s with active command\n",
   2449 				    device_xname(dev->sc_dev),
   2450 				    csr == SBIC_CSR_RSLT_NI ? "NI" : "IFY");
   2451 #ifdef DDB
   2452 /*			Debugger();*/
   2453 #endif
   2454 #endif
   2455 			TAILQ_INSERT_HEAD(&dev->ready_list, dev->sc_nexus, chain);
   2456 			dev->sc_tinfo[dev->target].lubusy &= ~(1 << dev->lun);
   2457 			dev->sc_nexus = NULL;
   2458 			dev->sc_xs = NULL;
   2459 		}
   2460 		/* Reload sync values for this target */
   2461 		if (dev->sc_sync[newtarget].state == SYNC_DONE)
   2462 			SET_SBIC_syn(regs, SBIC_SYN (dev->sc_sync[newtarget].offset,
   2463 			    dev->sc_sync[newtarget].period));
   2464 		else
   2465 			SET_SBIC_syn(regs, SBIC_SYN (0, sbic_min_period));
   2466 		for (acb = dev->nexus_list.tqh_first; acb;
   2467 		    acb = acb->chain.tqe_next) {
   2468 			if (acb->xs->xs_periph->periph_target != newtarget ||
   2469 			    acb->xs->xs_periph->periph_lun != newlun)
   2470 				continue;
   2471 			TAILQ_REMOVE(&dev->nexus_list, acb, chain);
   2472 			dev->sc_nexus = acb;
   2473 			dev->sc_xs = acb->xs;
   2474 			dev->sc_flags |= SBICF_SELECTED;
   2475 			dev->target = newtarget;
   2476 			dev->lun = newlun;
   2477 			break;
   2478 		}
   2479 		if (acb == NULL) {
   2480 			printf("%s: reselect %s targ %d not in nexus_list %p\n",
   2481 			    device_xname(dev->sc_dev),
   2482 			    csr == SBIC_CSR_RSLT_NI ? "NI" : "IFY", newtarget,
   2483 			    &dev->nexus_list.tqh_first);
   2484 			panic("bad reselect in sbic");
   2485 		}
   2486 		if (csr == SBIC_CSR_RSLT_IFY)
   2487 			SET_SBIC_cmd(regs, SBIC_CMD_CLR_ACK);
   2488 		break;
   2489 
   2490 	default:
   2491         abort:
   2492 		/*
   2493 		 * Something unexpected happened -- deal with it.
   2494 		 */
   2495 		printf("sbicnextstate: aborting csr %02x asr %02x\n", csr, asr);
   2496 #ifdef DDB
   2497 		Debugger();
   2498 #endif
   2499 #ifdef DEBUG
   2500 		if( data_pointer_debug > 1 )
   2501 			printf("next dmastop: %d(%p:%lx)\n",
   2502 			       dev->target,dev->sc_cur->dc_addr,dev->sc_tcnt);
   2503 		dev->sc_dmatimo = 0;
   2504 #endif
   2505 		dev->sc_dmastop(dev);
   2506 		SET_SBIC_control(regs, SBIC_CTL_EDI | SBIC_CTL_IDI);
   2507 		sbicerror(dev, regs, csr);
   2508 		sbicabort(dev, regs, "next");
   2509 		if (dev->sc_flags & SBICF_INDMA) {
   2510 			/*
   2511 			 * check for overlapping cache line, flush if so
   2512 			 */
   2513 #if defined(M68040) || defined(M68060)
   2514 			if (dev->sc_flags & SBICF_DCFLUSH) {
   2515 #if 0
   2516 				printf("sbic: 68040/060 DMA cache flush needs"
   2517 				    "fixing? %x:%x\n",
   2518 				    dev->sc_xs->data, dev->sc_xs->datalen);
   2519 #endif
   2520 			}
   2521 #endif
   2522 			dev->sc_flags &=
   2523 				~(SBICF_INDMA | SBICF_DCFLUSH);
   2524 #ifdef DEBUG
   2525 			if( data_pointer_debug > 1 )
   2526 				printf("next dmastop: %d(%p:%lx)\n",
   2527 				    dev->target,dev->sc_cur->dc_addr,dev->sc_tcnt);
   2528 			dev->sc_dmatimo = 0;
   2529 #endif
   2530 			dev->sc_dmastop(dev);
   2531 			sbic_scsidone(acb, -1);
   2532 		}
   2533 		SBIC_TRACE(dev);
   2534                 return SBIC_STATE_ERROR;
   2535 	}
   2536 
   2537 	SBIC_TRACE(dev);
   2538 	return(SBIC_STATE_RUNNING);
   2539 }
   2540 
   2541 
   2542 /*
   2543  * Check if DMA can not be used with specified buffer
   2544  */
   2545 
   2546 int
   2547 sbiccheckdmap(void *bp, u_long len, u_long mask)
   2548 {
   2549 	u_char *buffer;
   2550 	u_long phy_buf;
   2551 	u_long phy_len;
   2552 
   2553 	buffer = bp;
   2554 
   2555 	if (len == 0)
   2556 		return(0);
   2557 
   2558 	while (len) {
   2559 		phy_buf = kvtop(buffer);
   2560 		if (len < (phy_len = PAGE_SIZE - ((int) buffer & PGOFSET)))
   2561 			phy_len = len;
   2562 		if (phy_buf & mask)
   2563 			return(1);
   2564 		buffer += phy_len;
   2565 		len -= phy_len;
   2566 	}
   2567 	return(0);
   2568 }
   2569 
   2570 int
   2571 sbictoscsiperiod(struct sbic_softc *dev, sbic_regmap_t regs, int a)
   2572 {
   2573 	unsigned int fs;
   2574 
   2575 	/*
   2576 	 * cycle = DIV / (2*CLK)
   2577 	 * DIV = FS+2
   2578 	 * best we can do is 200ns at 20 MHz, 2 cycles
   2579 	 */
   2580 
   2581 	GET_SBIC_myid(regs,fs);
   2582 	fs = (fs >>6) + 2;		/* DIV */
   2583 	fs = (fs * 10000) / (dev->sc_clkfreq<<1);	/* Cycle, in ns */
   2584 	if (a < 2) a = 8;		/* map to Cycles */
   2585 	return ((fs*a)>>2);		/* in 4 ns units */
   2586 }
   2587 
   2588 int
   2589 sbicfromscsiperiod(struct sbic_softc *dev, sbic_regmap_t regs, int p)
   2590 {
   2591 	register unsigned int fs, ret;
   2592 
   2593 	/* Just the inverse of the above */
   2594 
   2595 	GET_SBIC_myid(regs,fs);
   2596 	fs = (fs >>6) + 2;		/* DIV */
   2597 	fs = (fs * 10000) / (dev->sc_clkfreq<<1);   /* Cycle, in ns */
   2598 
   2599 	ret = p << 2;			/* in ns units */
   2600 	ret = ret / fs;			/* in Cycles */
   2601 	if (ret < sbic_min_period)
   2602 		return(sbic_min_period);
   2603 
   2604 	/* verify rounding */
   2605 	if (sbictoscsiperiod(dev, regs, ret) < p)
   2606 		ret++;
   2607 	return (ret >= 8) ? 0 : ret;
   2608 }
   2609 
   2610 #ifdef DEBUG
   2611 
   2612 void
   2613 sbicdumpstate(void)
   2614 {
   2615 	u_char csr, asr;
   2616 
   2617 	GET_SBIC_asr(debug_sbic_regs,asr);
   2618 	GET_SBIC_csr(debug_sbic_regs,csr);
   2619 	printf("%s: asr:csr(%02x:%02x)->(%02x:%02x)\n",
   2620 	       (routine==1)?"sbicgo":
   2621 	       (routine==2)?"sbicintr":
   2622 	       (routine==3)?"sbicicmd":
   2623 	       (routine==4)?"sbicnext":"unknown",
   2624 	       debug_asr, debug_csr, asr, csr);
   2625 
   2626 }
   2627 
   2628 void
   2629 sbictimeout(struct sbic_softc *dev)
   2630 {
   2631 	int s, asr;
   2632 
   2633 	s = splbio();
   2634 	if (dev->sc_dmatimo) {
   2635 		if (dev->sc_dmatimo > 1) {
   2636 			printf("%s: DMA timeout #%d\n",
   2637 			    device_xname(dev->sc_dev), dev->sc_dmatimo - 1);
   2638 			GET_SBIC_asr(dev->sc_sbic, asr);
   2639 			if( asr & SBIC_ASR_INT ) {
   2640 				/* We need to service a missed IRQ */
   2641 				printf("Servicing a missed int:(%02x,%02x)->(%02x,?)\n",
   2642 				    debug_asr, debug_csr, asr);
   2643 				sbicintr(dev);
   2644 			}
   2645 			sbicdumpstate();
   2646 		}
   2647 		dev->sc_dmatimo++;
   2648 	}
   2649 	splx(s);
   2650 	callout_reset(&dev->sc_timo_ch, 30 * hz,
   2651 	    (void *)sbictimeout, dev);
   2652 }
   2653 
   2654 void
   2655 sbic_dump_acb(struct sbic_acb *acb)
   2656 {
   2657 	u_char *b = (u_char *) &acb->cmd;
   2658 	int i;
   2659 
   2660 	printf("acb@%p ", acb);
   2661 	if (acb->xs == NULL) {
   2662 		printf("<unused>\n");
   2663 		return;
   2664 	}
   2665 	printf("(%d:%d) flags %2x clen %2d cmd ",
   2666 		acb->xs->xs_periph->periph_target,
   2667 	    acb->xs->xs_periph->periph_lun, acb->flags, acb->clen);
   2668 	for (i = acb->clen; i; --i)
   2669 		printf(" %02x", *b++);
   2670 	printf("\n");
   2671 	printf("  xs: %8p data %8p:%04x ", acb->xs, acb->xs->data,
   2672 	    acb->xs->datalen);
   2673 	printf("va %8p:%04x ", acb->sc_kv.dc_addr, acb->sc_kv.dc_count);
   2674 	printf("pa %8p:%04x tcnt %lx\n", acb->sc_pa.dc_addr, acb->sc_pa.dc_count,
   2675 	    acb->sc_tcnt);
   2676 }
   2677 
   2678 void
   2679 sbic_dump(struct sbic_softc *dev)
   2680 {
   2681 	sbic_regmap_t regs;
   2682 	u_char csr, asr;
   2683 	struct sbic_acb *acb;
   2684 	int s;
   2685 	int i;
   2686 
   2687 	s = splbio();
   2688 	regs = dev->sc_sbic;
   2689 #if CSR_TRACE_SIZE
   2690 	printf("csr trace: ");
   2691 	i = csr_traceptr;
   2692 	do {
   2693 		printf("%c%02x%02x%02x ", csr_trace[i].whr,
   2694 		    csr_trace[i].csr, csr_trace[i].asr, csr_trace[i].xtn);
   2695 		switch(csr_trace[i].whr) {
   2696 		case 'g':
   2697 			printf("go "); break;
   2698 		case 's':
   2699 			printf("select "); break;
   2700 		case 'y':
   2701 			printf("select+ "); break;
   2702 		case 'i':
   2703 			printf("intr "); break;
   2704 		case 'f':
   2705 			printf("finish "); break;
   2706 		case '>':
   2707 			printf("out "); break;
   2708 		case '<':
   2709 			printf("in "); break;
   2710 		case 'm':
   2711 			printf("msgin "); break;
   2712 		case 'x':
   2713 			printf("msginx "); break;
   2714 		case 'X':
   2715 			printf("msginX "); break;
   2716 		case 'r':
   2717 			printf("reselect "); break;
   2718 		case 'I':
   2719 			printf("icmd "); break;
   2720 		case 'a':
   2721 			printf("abort "); break;
   2722 		default:
   2723 			printf("? ");
   2724 		}
   2725 		switch(csr_trace[i].csr) {
   2726 		case 0x11:
   2727 			printf("INITIATOR"); break;
   2728 		case 0x16:
   2729 			printf("S_XFERRED"); break;
   2730 		case 0x20:
   2731 			printf("MSGIN_ACK"); break;
   2732 		case 0x41:
   2733 			printf("DISC"); break;
   2734 		case 0x42:
   2735 			printf("SEL_TIMEO"); break;
   2736 		case 0x80:
   2737 			printf("RSLT_NI"); break;
   2738 		case 0x81:
   2739 			printf("RSLT_IFY"); break;
   2740 		case 0x85:
   2741 			printf("DISC_1"); break;
   2742 		case 0x18: case 0x19: case 0x1a:
   2743 		case 0x1b: case 0x1e: case 0x1f:
   2744 		case 0x28: case 0x29: case 0x2a:
   2745 		case 0x2b: case 0x2e: case 0x2f:
   2746 		case 0x48: case 0x49: case 0x4a:
   2747 		case 0x4b: case 0x4e: case 0x4f:
   2748 		case 0x88: case 0x89: case 0x8a:
   2749 		case 0x8b: case 0x8e: case 0x8f:
   2750 			switch(csr_trace[i].csr & 0xf0) {
   2751 			case 0x10:
   2752 				printf("DONE_"); break;
   2753 			case 0x20:
   2754 				printf("STOP_"); break;
   2755 			case 0x40:
   2756 				printf("ERR_"); break;
   2757 			case 0x80:
   2758 				printf("REQ_"); break;
   2759 			}
   2760 			switch(csr_trace[i].csr & 7) {
   2761 			case 0:
   2762 				printf("DATA_OUT"); break;
   2763 			case 1:
   2764 				printf("DATA_IN"); break;
   2765 			case 2:
   2766 				printf("CMD"); break;
   2767 			case 3:
   2768 				printf("STATUS"); break;
   2769 			case 6:
   2770 				printf("MSG_OUT"); break;
   2771 			case 7:
   2772 				printf("MSG_IN"); break;
   2773 			default:
   2774 				printf("invld phs");
   2775 			}
   2776 			break;
   2777 		default:    printf("****"); break;
   2778 		}
   2779 		if (csr_trace[i].asr & SBIC_ASR_INT)
   2780 			printf(" ASR_INT");
   2781 		if (csr_trace[i].asr & SBIC_ASR_LCI)
   2782 			printf(" ASR_LCI");
   2783 		if (csr_trace[i].asr & SBIC_ASR_BSY)
   2784 			printf(" ASR_BSY");
   2785 		if (csr_trace[i].asr & SBIC_ASR_CIP)
   2786 			printf(" ASR_CIP");
   2787 		printf("\n");
   2788 		i = (i + 1) & (CSR_TRACE_SIZE - 1);
   2789 	} while (i != csr_traceptr);
   2790 #endif
   2791 	GET_SBIC_asr(regs, asr);
   2792 	if ((asr & SBIC_ASR_INT) == 0)
   2793 		GET_SBIC_csr(regs, csr);
   2794 	else
   2795 		csr = 0;
   2796 	printf("%s@%p regs %p/%p asr %x csr %x\n", device_xname(dev->sc_dev),
   2797 	    dev, regs.sbic_asr_p, regs.sbic_value_p, asr, csr);
   2798 	if ((acb = dev->free_list.tqh_first)) {
   2799 		printf("Free list:\n");
   2800 		while (acb) {
   2801 			sbic_dump_acb(acb);
   2802 			acb = acb->chain.tqe_next;
   2803 		}
   2804 	}
   2805 	if ((acb = dev->ready_list.tqh_first)) {
   2806 		printf("Ready list:\n");
   2807 		while (acb) {
   2808 			sbic_dump_acb(acb);
   2809 			acb = acb->chain.tqe_next;
   2810 		}
   2811 	}
   2812 	if ((acb = dev->nexus_list.tqh_first)) {
   2813 		printf("Nexus list:\n");
   2814 		while (acb) {
   2815 			sbic_dump_acb(acb);
   2816 			acb = acb->chain.tqe_next;
   2817 		}
   2818 	}
   2819 	if (dev->sc_nexus) {
   2820 		printf("nexus:\n");
   2821 		sbic_dump_acb(dev->sc_nexus);
   2822 	}
   2823 	printf("sc_xs %p targ %d lun %d flags %x tcnt %lx dmacmd %x mask %lx\n",
   2824 	    dev->sc_xs, dev->target, dev->lun, dev->sc_flags, dev->sc_tcnt,
   2825 	    dev->sc_dmacmd, dev->sc_dmamask);
   2826 	for (i = 0; i < 8; ++i) {
   2827 		if (dev->sc_tinfo[i].cmds > 2) {
   2828 			printf("tgt %d: cmds %d disc %d lubusy %x\n",
   2829 			    i, dev->sc_tinfo[i].cmds,
   2830 			    dev->sc_tinfo[i].dconns,
   2831 			    dev->sc_tinfo[i].lubusy);
   2832 		}
   2833 	}
   2834 	splx(s);
   2835 }
   2836 
   2837 #endif
   2838