Home | History | Annotate | Line # | Download | only in dev
sbic.c revision 1.13
      1 /*	$NetBSD: sbic.c,v 1.13 1995/08/12 20:30:47 mycroft Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1994 Christian E. Hopps
      5  * Copyright (c) 1990 The Regents of the University of California.
      6  * All rights reserved.
      7  *
      8  * This code is derived from software contributed to Berkeley by
      9  * Van Jacobson of Lawrence Berkeley Laboratory.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the University of
     22  *	California, Berkeley and its contributors.
     23  * 4. Neither the name of the University nor the names of its contributors
     24  *    may be used to endorse or promote products derived from this software
     25  *    without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     37  * SUCH DAMAGE.
     38  *
     39  *	@(#)scsi.c	7.5 (Berkeley) 5/4/91
     40  */
     41 
     42 /*
     43  * AMIGA AMD 33C93 scsi adaptor driver
     44  */
     45 
     46 /* need to know if any tapes have been configured */
     47 #include "st.h"
     48 
     49 #include <sys/param.h>
     50 #include <sys/systm.h>
     51 #include <sys/device.h>
     52 #include <sys/disklabel.h>
     53 #include <sys/dkstat.h>
     54 #include <sys/buf.h>
     55 #include <scsi/scsi_all.h>
     56 #include <scsi/scsiconf.h>
     57 #include <vm/vm.h>
     58 #include <vm/vm_kern.h>
     59 #include <vm/vm_page.h>
     60 #include <machine/pmap.h>
     61 #include <machine/cpu.h>
     62 #include <amiga/amiga/device.h>
     63 #include <amiga/amiga/custom.h>
     64 #include <amiga/amiga/isr.h>
     65 #include <amiga/dev/dmavar.h>
     66 #include <amiga/dev/sbicreg.h>
     67 #include <amiga/dev/sbicvar.h>
     68 
     69 /*
     70  * SCSI delays
     71  * In u-seconds, primarily for state changes on the SPC.
     72  */
     73 #define	SBIC_CMD_WAIT	50000	/* wait per step of 'immediate' cmds */
     74 #define	SBIC_DATA_WAIT	50000	/* wait per data in/out step */
     75 #define	SBIC_INIT_WAIT	50000	/* wait per step (both) during init */
     76 
     77 #define	b_cylin		b_resid
     78 #define SBIC_WAIT(regs, until, timeo) sbicwait(regs, until, timeo, __LINE__)
     79 
     80 extern u_int kvtop();
     81 
     82 int  sbicicmd __P((struct sbic_softc *, int, int, void *, int, void *, int,u_char));
     83 int  sbicgo __P((struct sbic_softc *, struct scsi_xfer *));
     84 int  sbicdmaok __P((struct sbic_softc *, struct scsi_xfer *));
     85 int  sbicgetsense __P((struct sbic_softc *, struct scsi_xfer *));
     86 int  sbicwait __P((sbic_regmap_p, char, int , int));
     87 int  sbiccheckdmap __P((void *, u_long, u_long));
     88 int  sbicselectbus __P((struct sbic_softc *, sbic_regmap_p, u_char, u_char, u_char));
     89 int  sbicxfstart __P((sbic_regmap_p, int, u_char, int));
     90 int  sbicxfout __P((sbic_regmap_p regs, int, void *, int));
     91 int  sbicfromscsiperiod __P((struct sbic_softc *, sbic_regmap_p, int));
     92 int  sbictoscsiperiod __P((struct sbic_softc *, sbic_regmap_p, int));
     93 int  sbicintr __P((struct sbic_softc *));
     94 void sbicxfin __P((sbic_regmap_p regs, int, void *));
     95 void sbicxfdone __P((struct sbic_softc *, sbic_regmap_p, int));
     96 void sbicabort __P((struct sbic_softc *, sbic_regmap_p, char *));
     97 void sbicerror __P((struct sbic_softc *, sbic_regmap_p, u_char));
     98 void sbicstart __P((struct sbic_softc *));
     99 void sbicreset __P((struct sbic_softc *));
    100 void sbicsetdelay __P((int));
    101 void sbic_scsidone __P((struct sbic_softc *, int));
    102 void sbic_donextcmd __P((struct sbic_softc *));
    103 
    104 /*
    105  * Synch xfer parameters, and timing conversions
    106  */
    107 int sbic_min_period = SBIC_SYN_MIN_PERIOD;  /* in cycles = f(ICLK,FSn) */
    108 int sbic_max_offset = SBIC_SYN_MAX_OFFSET;  /* pure number */
    109 
    110 int sbic_cmd_wait = SBIC_CMD_WAIT;
    111 int sbic_data_wait = SBIC_DATA_WAIT;
    112 int sbic_init_wait = SBIC_INIT_WAIT;
    113 
    114 /*
    115  * was broken before.. now if you want this you get it for all drives
    116  * on sbic controllers.
    117  */
    118 int sbic_inhibit_sync = 0;
    119 int sbic_clock_override = 0;
    120 int sbic_no_dma = 0;
    121 
    122 #ifdef DEBUG
    123 int	sbicdma_ops = 0;	/* total DMA operations */
    124 int	sbicdma_bounces = 0;	/* number operations using bounce buffer */
    125 int	sbicdma_hits = 0;	/* number of DMA chains that were contiguous */
    126 int	sbicdma_misses = 0;	/* number of DMA chains that were not contiguous */
    127 #define QPRINTF(a) if (sbic_debug > 1) printf a
    128 int	sbic_debug = 0;
    129 int	sync_debug = 0;
    130 int	sbic_dma_debug = 0;
    131 #else
    132 #define QPRINTF
    133 #endif
    134 
    135 /*
    136  * default minphys routine for sbic based controllers
    137  */
    138 void
    139 sbic_minphys(bp)
    140 	struct buf *bp;
    141 {
    142 
    143 	/*
    144 	 * No max transfer at this level.
    145 	 */
    146 	minphys(bp);
    147 }
    148 
    149 /*
    150  * used by specific sbic controller
    151  *
    152  * it appears that the higher level code does nothing with LUN's
    153  * so I will too.  I could plug it in, however so could they
    154  * in scsi_scsi_cmd().
    155  */
    156 int
    157 sbic_scsicmd(xs)
    158 	struct scsi_xfer *xs;
    159 {
    160 	struct sbic_pending *pendp;
    161 	struct sbic_softc *dev;
    162 	struct scsi_link *slp;
    163 	int flags, s;
    164 
    165 	slp = xs->sc_link;
    166 	dev = slp->adapter_softc;
    167 	flags = xs->flags;
    168 
    169 	if (flags & SCSI_DATA_UIO)
    170 		panic("sbic: scsi data uio requested");
    171 
    172 	if (dev->sc_xs && flags & SCSI_POLL)
    173 		panic("sbic_scsicmd: busy");
    174 
    175 	s = splbio();
    176 	pendp = &dev->sc_xsstore[slp->target][slp->lun];
    177 	if (pendp->xs) {
    178 		splx(s);
    179 		return(TRY_AGAIN_LATER);
    180 	}
    181 
    182 	if (dev->sc_xs) {
    183 		pendp->xs = xs;
    184 		TAILQ_INSERT_TAIL(&dev->sc_xslist, pendp, link);
    185 		splx(s);
    186 		return(SUCCESSFULLY_QUEUED);
    187 	}
    188 	pendp->xs = NULL;
    189 	dev->sc_xs = xs;
    190 	splx(s);
    191 
    192 	/*
    193 	 * nothing is pending do it now.
    194 	 */
    195 	sbic_donextcmd(dev);
    196 
    197 	if (flags & SCSI_POLL)
    198 		return(COMPLETE);
    199 	return(SUCCESSFULLY_QUEUED);
    200 }
    201 
    202 /*
    203  * entered with dev->sc_xs pointing to the next xfer to perform
    204  */
    205 void
    206 sbic_donextcmd(dev)
    207 	struct sbic_softc *dev;
    208 {
    209 	struct scsi_xfer *xs;
    210 	struct scsi_link *slp;
    211 	int flags, phase, stat;
    212 
    213 	xs = dev->sc_xs;
    214 	slp = xs->sc_link;
    215 	flags = xs->flags;
    216 
    217 	if (flags & SCSI_DATA_IN)
    218 		phase = DATA_IN_PHASE;
    219 	else if (flags & SCSI_DATA_OUT)
    220 		phase = DATA_OUT_PHASE;
    221 	else
    222 		phase = STATUS_PHASE;
    223 
    224 	if (flags & SCSI_RESET)
    225 		sbicreset(dev);
    226 
    227 	dev->sc_stat[0] = -1;
    228 	xs->cmd->bytes[0] |= slp->lun << 5;
    229 	if (phase == STATUS_PHASE || flags & SCSI_POLL ||
    230 	    sbicdmaok(dev, xs) == 0)
    231 		stat = sbicicmd(dev, slp->target, slp->lun, xs->cmd,
    232 		    xs->cmdlen, xs->data, xs->datalen, phase);
    233 	else if (sbicgo(dev, xs) == 0)
    234 		return;
    235 	else
    236 		stat = dev->sc_stat[0];
    237 
    238 	sbic_scsidone(dev, stat);
    239 }
    240 
    241 void
    242 sbic_scsidone(dev, stat)
    243 	struct sbic_softc *dev;
    244 	int stat;
    245 {
    246 	struct sbic_pending *pendp;
    247 	struct scsi_xfer *xs;
    248 	int s, donext;
    249 
    250 	xs = dev->sc_xs;
    251 #ifdef DIAGNOSTIC
    252 	if (xs == NULL)
    253 		panic("sbic_scsidone");
    254 #endif
    255 #if 1
    256 	if (((struct device *)(xs->sc_link->device_softc))->dv_unit < dk_ndrive)
    257 		++dk_xfer[((struct device *)(xs->sc_link->device_softc))->dv_unit];
    258 #endif
    259 	/*
    260 	 * is this right?
    261 	 */
    262 	xs->status = stat;
    263 
    264 	if (stat == 0)
    265 		xs->resid = 0;
    266 	else {
    267 		switch(stat) {
    268 		case SCSI_CHECK:
    269 			if (stat = sbicgetsense(dev, xs))
    270 				goto bad_sense;
    271 			xs->error = XS_SENSE;
    272 			break;
    273 		case SCSI_BUSY:
    274 			xs->error = XS_BUSY;
    275 			break;
    276 		bad_sense:
    277 		default:
    278 			xs->error = XS_DRIVER_STUFFUP;
    279 			QPRINTF(("sbic_scsicmd() bad %x\n", stat));
    280 			break;
    281 		}
    282 	}
    283 	xs->flags |= ITSDONE;
    284 
    285 	/*
    286 	 * grab next command before scsi_done()
    287 	 * this way no single device can hog scsi resources.
    288 	 */
    289 	s = splbio();
    290 	pendp = dev->sc_xslist.tqh_first;
    291 	if (pendp == NULL) {
    292 		donext = 0;
    293 		dev->sc_xs = NULL;
    294 	} else {
    295 		donext = 1;
    296 		TAILQ_REMOVE(&dev->sc_xslist, pendp, link);
    297 		dev->sc_xs = pendp->xs;
    298 		pendp->xs = NULL;
    299 	}
    300 	splx(s);
    301 	scsi_done(xs);
    302 
    303 	if (donext)
    304 		sbic_donextcmd(dev);
    305 }
    306 
    307 int
    308 sbicgetsense(dev, xs)
    309 	struct sbic_softc *dev;
    310 	struct scsi_xfer *xs;
    311 {
    312 	struct scsi_sense rqs;
    313 	struct scsi_link *slp;
    314 	int stat;
    315 
    316 	slp = xs->sc_link;
    317 
    318 	rqs.opcode = REQUEST_SENSE;
    319 	rqs.byte2 = slp->lun << 5;
    320 #ifdef not_yet
    321 	rqs.length = xs->req_sense_length ? xs->req_sense_length :
    322 	    sizeof(xs->sense);
    323 #else
    324 	rqs.length = sizeof(xs->sense);
    325 #endif
    326 
    327 	rqs.unused[0] = rqs.unused[1] = rqs.control = 0;
    328 
    329 	return(sbicicmd(dev, slp->target, slp->lun, &rqs, sizeof(rqs),
    330 	    &xs->sense, rqs.length, DATA_IN_PHASE));
    331 }
    332 
    333 int
    334 sbicdmaok(dev, xs)
    335 	struct sbic_softc *dev;
    336 	struct scsi_xfer *xs;
    337 {
    338 	if (sbic_no_dma || xs->datalen & 0x1 || (u_int)xs->data & 0x3)
    339 		return(0);
    340 	/*
    341 	 * controller supports dma to any addresses?
    342 	 */
    343 	else if ((dev->sc_flags & SBICF_BADDMA) == 0)
    344 		return(1);
    345 	/*
    346 	 * this address is ok for dma?
    347 	 */
    348 	else if (sbiccheckdmap(xs->data, xs->datalen, dev->sc_dmamask) == 0)
    349 		return(1);
    350 	/*
    351 	 * we have a bounce buffer?
    352 	 */
    353 	else if (dev->sc_dmabuffer)
    354 		return(1);
    355 	return(0);
    356 }
    357 
    358 
    359 int
    360 sbicwait(regs, until, timeo, line)
    361 	sbic_regmap_p regs;
    362 	char until;
    363 	int timeo;
    364 	int line;
    365 {
    366 	u_char val;
    367 	int csr;
    368 
    369 	if (timeo == 0)
    370 		timeo = 1000000;	/* some large value.. */
    371 
    372 	GET_SBIC_asr(regs,val);
    373 	while ((val & until) == 0) {
    374 		if (timeo-- == 0) {
    375 			GET_SBIC_csr(regs, csr);
    376 			printf("sbicwait TIMEO @%d with asr=x%x csr=x%x\n",
    377 			    line, val, csr);
    378 			break;
    379 		}
    380 		DELAY(1);
    381 		GET_SBIC_asr(regs,val);
    382 	}
    383 	return(val);
    384 }
    385 
    386 void
    387 sbicabort(dev, regs, where)
    388 	struct sbic_softc *dev;
    389 	sbic_regmap_p regs;
    390 	char *where;
    391 {
    392 	u_char csr, asr;
    393 
    394 	GET_SBIC_csr(regs, csr);
    395 	GET_SBIC_asr(regs, asr);
    396 
    397 	printf ("%s: abort %s: csr = 0x%02x, asr = 0x%02x\n",
    398 	    dev->sc_dev.dv_xname, where, csr, asr);
    399 
    400 	if (dev->sc_flags & SBICF_SELECTED) {
    401 		SET_SBIC_cmd(regs, SBIC_CMD_ABORT);
    402 		WAIT_CIP(regs);
    403 
    404 		GET_SBIC_asr(regs, asr);
    405 		if (asr & (SBIC_ASR_BSY|SBIC_ASR_LCI)) {
    406 			/* ok, get more drastic.. */
    407 
    408 			SET_SBIC_cmd (regs, SBIC_CMD_RESET);
    409 			DELAY(25);
    410 			SBIC_WAIT(regs, SBIC_ASR_INT, 0);
    411 			/* clears interrupt also */
    412 			GET_SBIC_csr (regs, csr);
    413 
    414 			dev->sc_flags &= ~SBICF_SELECTED;
    415 			return;
    416 		}
    417 
    418 		do {
    419 			SBIC_WAIT (regs, SBIC_ASR_INT, 0);
    420 			GET_SBIC_csr (regs, csr);
    421 		} while ((csr != SBIC_CSR_DISC) && (csr != SBIC_CSR_DISC_1)
    422 		    && (csr != SBIC_CSR_CMD_INVALID));
    423 
    424 		/* lets just hope it worked.. */
    425 		dev->sc_flags &= ~SBICF_SELECTED;
    426 	}
    427 }
    428 
    429 /*
    430  * XXX Set/reset long delays.
    431  *
    432  * if delay == 0, reset default delays
    433  * if delay < 0,  set both delays to default long initialization values
    434  * if delay > 0,  set both delays to this value
    435  *
    436  * Used when a devices is expected to respond slowly (e.g. during
    437  * initialization).
    438  */
    439 void
    440 sbicsetdelay(del)
    441 	int del;
    442 {
    443 	static int saved_cmd_wait, saved_data_wait;
    444 
    445 	if (del) {
    446 		saved_cmd_wait = sbic_cmd_wait;
    447 		saved_data_wait = sbic_data_wait;
    448 		if (del > 0)
    449 			sbic_cmd_wait = sbic_data_wait = del;
    450 		else
    451 			sbic_cmd_wait = sbic_data_wait = sbic_init_wait;
    452 	} else {
    453 		sbic_cmd_wait = saved_cmd_wait;
    454 		sbic_data_wait = saved_data_wait;
    455 	}
    456 }
    457 
    458 void
    459 sbicreset(dev)
    460 	struct sbic_softc *dev;
    461 {
    462 	sbic_regmap_p regs;
    463 	u_int i, s;
    464 	u_char my_id, csr;
    465 
    466 	regs = dev->sc_sbicp;
    467 
    468 	if (dev->sc_flags & SBICF_ALIVE)
    469 		sbicabort(dev, regs, "reset");
    470 
    471 	s = splbio();
    472 	/* preserve our ID for now */
    473 	GET_SBIC_myid (regs, my_id);
    474 	my_id &= SBIC_ID_MASK;
    475 
    476 	if (dev->sc_clkfreq < 110)
    477 		my_id |= SBIC_ID_FS_8_10;
    478 	else if (dev->sc_clkfreq < 160)
    479 		my_id |= SBIC_ID_FS_12_15;
    480 	else if (dev->sc_clkfreq < 210)
    481 		my_id |= SBIC_ID_FS_16_20;
    482 
    483 	my_id |= SBIC_ID_EAF /*| SBIC_ID_EHP*/ ;
    484 
    485 	SET_SBIC_myid(regs, my_id);
    486 
    487 	/*
    488 	 * Disable interrupts (in dmainit) then reset the chip
    489 	 */
    490 	SET_SBIC_cmd(regs, SBIC_CMD_RESET);
    491 	DELAY(25);
    492 	SBIC_WAIT(regs, SBIC_ASR_INT, 0);
    493 	GET_SBIC_csr(regs, csr);       /* clears interrupt also */
    494 
    495 	/*
    496 	 * Set up various chip parameters
    497 	 */
    498 	SET_SBIC_control(regs, SBIC_CTL_EDI | SBIC_CTL_IDI
    499 	    | SBIC_MACHINE_DMA_MODE);
    500 	/*
    501 	 * don't allow (re)selection (SBIC_RID_ES)
    502 	 * until we can handle target mode!!
    503 	 */
    504 	SET_SBIC_rselid(regs, 0);
    505 	SET_SBIC_syn(regs, 0);     /* asynch for now */
    506 
    507 	/*
    508 	 * anything else was zeroed by reset
    509 	 */
    510 	splx(s);
    511 
    512 	dev->sc_flags |= SBICF_ALIVE;
    513 	dev->sc_flags &= ~SBICF_SELECTED;
    514 }
    515 
    516 void
    517 sbicerror(dev, regs, csr)
    518 	struct sbic_softc *dev;
    519 	sbic_regmap_p regs;
    520 	u_char csr;
    521 {
    522 	struct scsi_xfer *xs;
    523 
    524 	xs = dev->sc_xs;
    525 
    526 #ifdef DIAGNOSTIC
    527 	if (xs == NULL)
    528 		panic("sbicerror");
    529 #endif
    530 	if (xs->flags & SCSI_SILENT)
    531 		return;
    532 
    533 	printf("%s: ", dev->sc_dev.dv_xname);
    534 	printf("csr == 0x%02i\n", csr);	/* XXX */
    535 }
    536 
    537 /*
    538  * select the bus, return when selected or error.
    539  */
    540 int
    541 sbicselectbus(dev, regs, target, lun, our_addr)
    542         struct sbic_softc *dev;
    543 	sbic_regmap_p regs;
    544 	u_char target, lun, our_addr;
    545 {
    546 	u_char asr, csr, id;
    547 
    548 	QPRINTF(("sbicselectbus %d\n", target));
    549 
    550 	/*
    551 	 * if we're already selected, return (XXXX panic maybe?)
    552 	 */
    553 	if (dev->sc_flags & SBICF_SELECTED)
    554 		return(1);
    555 
    556 	/*
    557 	 * issue select
    558 	 */
    559 	SBIC_TC_PUT(regs, 0);
    560 	SET_SBIC_selid(regs, target);
    561 	SET_SBIC_timeo(regs, SBIC_TIMEOUT(250,dev->sc_clkfreq));
    562 
    563 	/*
    564 	 * set sync or async
    565 	 */
    566 	if (dev->sc_sync[target].state == SYNC_DONE)
    567 		SET_SBIC_syn(regs, SBIC_SYN (dev->sc_sync[target].offset,
    568 		    dev->sc_sync[target].period));
    569 	else
    570 		SET_SBIC_syn(regs, SBIC_SYN (0, sbic_min_period));
    571 
    572 	SET_SBIC_cmd(regs, SBIC_CMD_SEL_ATN);
    573 
    574 	/*
    575 	 * wait for select (merged from seperate function may need
    576 	 * cleanup)
    577 	 */
    578 	WAIT_CIP(regs);
    579 	do {
    580 		SBIC_WAIT(regs, SBIC_ASR_INT, 0);
    581 		GET_SBIC_csr (regs, csr);
    582 		QPRINTF(("%02x ", csr));
    583 	} while (csr != (SBIC_CSR_MIS_2|MESG_OUT_PHASE)
    584 	    && csr != (SBIC_CSR_MIS_2|CMD_PHASE) && csr != SBIC_CSR_SEL_TIMEO);
    585 
    586 	if (csr == (SBIC_CSR_MIS_2|CMD_PHASE))
    587 		dev->sc_flags |= SBICF_SELECTED;	/* device ignored ATN */
    588 	else if (csr == (SBIC_CSR_MIS_2|MESG_OUT_PHASE)) {
    589 		/*
    590 		 * Send identify message
    591 		 * (SCSI-2 requires an identify msg (?))
    592 		 */
    593 		GET_SBIC_selid(regs, id);
    594 
    595 		/*
    596 		 * handle drives that don't want to be asked
    597 		 * whether to go sync at all.
    598 		 */
    599 		if (sbic_inhibit_sync && dev->sc_sync[id].state == SYNC_START) {
    600 #ifdef DEBUG
    601 			if (sync_debug)
    602 				printf("Forcing target %d asynchronous.\n", id);
    603 #endif
    604 			dev->sc_sync[id].offset = 0;
    605 			dev->sc_sync[id].period = sbic_min_period;
    606 			dev->sc_sync[id].state = SYNC_DONE;
    607 		}
    608 
    609 
    610 		if (dev->sc_sync[id].state != SYNC_START)
    611 			SEND_BYTE (regs, MSG_IDENTIFY | lun);
    612 		else {
    613 			/*
    614 			 * try to initiate a sync transfer.
    615 			 * So compose the sync message we're going
    616 			 * to send to the target
    617 			 */
    618 
    619 #ifdef DEBUG
    620 			if (sync_debug)
    621 				printf("Sending sync request to target %d ... ",
    622 				    id);
    623 #endif
    624 			/*
    625 			 * setup scsi message sync message request
    626 			 */
    627 			dev->sc_msg[0] = MSG_IDENTIFY | lun;
    628 			dev->sc_msg[1] = MSG_EXT_MESSAGE;
    629 			dev->sc_msg[2] = 3;
    630 			dev->sc_msg[3] = MSG_SYNC_REQ;
    631 			dev->sc_msg[4] = sbictoscsiperiod(dev, regs,
    632 			    sbic_min_period);
    633 			dev->sc_msg[5] = sbic_max_offset;
    634 
    635 			if (sbicxfstart(regs, 6, MESG_OUT_PHASE, sbic_cmd_wait))
    636 				sbicxfout(regs, 6, dev->sc_msg, MESG_OUT_PHASE);
    637 
    638 			dev->sc_sync[id].state = SYNC_SENT;
    639 #ifdef DEBUG
    640 			if (sync_debug)
    641 				printf ("sent\n");
    642 #endif
    643 		}
    644 
    645 		SBIC_WAIT (regs, SBIC_ASR_INT, 0);
    646 		GET_SBIC_csr (regs, csr);
    647 		QPRINTF(("[%02x]", csr));
    648 #ifdef DEBUG
    649 		if (sync_debug && dev->sc_sync[id].state == SYNC_SENT)
    650 			printf("csr-result of last msgout: 0x%x\n", csr);
    651 #endif
    652 
    653 		if (csr != SBIC_CSR_SEL_TIMEO)
    654 			dev->sc_flags |= SBICF_SELECTED;
    655 	}
    656 
    657 	QPRINTF(("\n"));
    658 
    659 	return(csr == SBIC_CSR_SEL_TIMEO);
    660 }
    661 
    662 int
    663 sbicxfstart(regs, len, phase, wait)
    664 	sbic_regmap_p regs;
    665 	int len, wait;
    666 	u_char phase;
    667 {
    668 	u_char id;
    669 
    670 	if (phase == DATA_IN_PHASE || phase == MESG_IN_PHASE) {
    671 		GET_SBIC_selid (regs, id);
    672 		id |= SBIC_SID_FROM_SCSI;
    673 		SET_SBIC_selid (regs, id);
    674 		SBIC_TC_PUT (regs, (unsigned)len);
    675 	} else if (phase == DATA_OUT_PHASE || phase == MESG_OUT_PHASE
    676 	    || phase == CMD_PHASE)
    677 		SBIC_TC_PUT (regs, (unsigned)len);
    678 	else
    679 		SBIC_TC_PUT (regs, 0);
    680 	QPRINTF(("sbicxfstart %d, %d, %d\n", len, phase, wait));
    681 
    682 	return(1);
    683 }
    684 
    685 int
    686 sbicxfout(regs, len, bp, phase)
    687 	sbic_regmap_p regs;
    688 	int len;
    689 	void *bp;
    690 	int phase;
    691 {
    692 	u_char orig_csr, csr, asr, *buf;
    693 	int wait;
    694 
    695 	buf = bp;
    696 	wait = sbic_data_wait;
    697 
    698 	QPRINTF(("sbicxfout {%d} %02x %02x %02x %02x %02x "
    699 	    "%02x %02x %02x %02x %02x\n", len, buf[0], buf[1], buf[2],
    700 	    buf[3], buf[4], buf[5], buf[6], buf[7], buf[8], buf[9]));
    701 
    702 	GET_SBIC_csr (regs, orig_csr);
    703 
    704 	/*
    705 	 * sigh.. WD-PROTO strikes again.. sending the command in one go
    706 	 * causes the chip to lock up if talking to certain (misbehaving?)
    707 	 * targets. Anyway, this procedure should work for all targets, but
    708 	 * it's slightly slower due to the overhead
    709 	 */
    710 	WAIT_CIP (regs);
    711 	SET_SBIC_cmd (regs, SBIC_CMD_XFER_INFO);
    712 	for (;len > 0; len--) {
    713 		GET_SBIC_asr (regs, asr);
    714 		while ((asr & SBIC_ASR_DBR) == 0) {
    715 			if ((asr & SBIC_ASR_INT) || --wait < 0) {
    716 #ifdef DEBUG
    717 				if (sbic_debug)
    718 					printf("sbicxfout fail: l%d i%x w%d\n",
    719 					    len, asr, wait);
    720 #endif
    721 				return (len);
    722 			}
    723 			DELAY(1);
    724 			GET_SBIC_asr (regs, asr);
    725 		}
    726 
    727 		SET_SBIC_data (regs, *buf);
    728 		buf++;
    729 	}
    730 
    731 	QPRINTF(("sbicxfout done\n"));
    732 	/*
    733 	 * this leaves with one csr to be read
    734 	 */
    735 	return(0);
    736 }
    737 
    738 void
    739 sbicxfin(regs, len, bp)
    740 	sbic_regmap_p regs;
    741 	int len;
    742 	void *bp;
    743 {
    744 	int wait;
    745 	u_char *obp, *buf;
    746 	u_char orig_csr, csr, asr;
    747 
    748 	wait = sbic_data_wait;
    749 	obp = bp;
    750 	buf = bp;
    751 
    752 	GET_SBIC_csr (regs, orig_csr);
    753 
    754 	QPRINTF(("sbicxfin %d, csr=%02x\n", len, orig_csr));
    755 
    756 	WAIT_CIP (regs);
    757 	SET_SBIC_cmd (regs, SBIC_CMD_XFER_INFO);
    758 	for (;len > 0; len--) {
    759 		GET_SBIC_asr (regs, asr);
    760 		while ((asr & SBIC_ASR_DBR) == 0) {
    761 			if ((asr & SBIC_ASR_INT) || --wait < 0) {
    762 #ifdef DEBUG
    763 				if (sbic_debug)
    764 					printf("sbicxfin fail: l%d i%x w%d\n",
    765 					    len, asr, wait);
    766 #endif
    767 				return;
    768 			}
    769 
    770 			DELAY(1);
    771 			GET_SBIC_asr (regs, asr);
    772 		}
    773 
    774 		GET_SBIC_data (regs, *buf);
    775 		buf++;
    776 	}
    777 
    778 	QPRINTF(("sbicxfin {%d} %02x %02x %02x %02x %02x %02x "
    779 	    "%02x %02x %02x %02x\n", len, obp[0], obp[1], obp[2],
    780 	    obp[3], obp[4], obp[5], obp[6], obp[7], obp[8], obp[9]));
    781 
    782 	/* this leaves with one csr to be read */
    783 }
    784 
    785 
    786 /*
    787  * SCSI 'immediate' command:  issue a command to some SCSI device
    788  * and get back an 'immediate' response (i.e., do programmed xfer
    789  * to get the response data).  'cbuf' is a buffer containing a scsi
    790  * command of length clen bytes.  'buf' is a buffer of length 'len'
    791  * bytes for data.  The transfer direction is determined by the device
    792  * (i.e., by the scsi bus data xfer phase).  If 'len' is zero, the
    793  * command must supply no data.  'xferphase' is the bus phase the
    794  * caller expects to happen after the command is issued.  It should
    795  * be one of DATA_IN_PHASE, DATA_OUT_PHASE or STATUS_PHASE.
    796  */
    797 int
    798 sbicicmd(dev, target, lun, cbuf, clen, buf, len, xferphase)
    799 	struct sbic_softc *dev;
    800 	void *cbuf, *buf;
    801 	int clen, len;
    802 	u_char xferphase;
    803 {
    804 	sbic_regmap_p regs;
    805 	u_char phase, csr, asr;
    806 	int wait;
    807 
    808 	regs = dev->sc_sbicp;
    809 
    810 	/*
    811 	 * set the sbic into non-DMA mode
    812 	 */
    813 	SET_SBIC_control(regs, SBIC_CTL_EDI | SBIC_CTL_IDI);
    814 
    815 retry_selection:
    816 	/*
    817 	 * select the SCSI bus (it's an error if bus isn't free)
    818 	 */
    819 	if (sbicselectbus(dev, regs, target, lun, dev->sc_scsiaddr))
    820 		return(-1);
    821 	/*
    822 	 * Wait for a phase change (or error) then let the device sequence
    823 	 * us through the various SCSI phases.
    824 	 */
    825 	dev->sc_stat[0] = 0xff;
    826 	dev->sc_msg[0] = 0xff;
    827 	phase = CMD_PHASE;
    828 
    829 new_phase:
    830 	wait = sbic_cmd_wait;
    831 
    832 	GET_SBIC_csr (regs, csr);
    833 	QPRINTF((">CSR:%02x<", csr));
    834 
    835 	/*
    836 	 * requesting some new phase
    837 	 */
    838 	if ((csr != 0xff) && (csr & 0xf0) && (csr & 0x08))
    839 		phase = csr & PHASE;
    840 	else if ((csr == SBIC_CSR_DISC) || (csr == SBIC_CSR_DISC_1)
    841 	    || (csr == SBIC_CSR_S_XFERRED)) {
    842 		dev->sc_flags &= ~SBICF_SELECTED;
    843 		GET_SBIC_cmd_phase (regs, phase);
    844 		if (phase == 0x60)
    845 			GET_SBIC_tlun (regs, dev->sc_stat[0]);
    846 		else
    847 			return(-1);
    848 		goto out;
    849 	} else {
    850 		sbicerror(dev, regs, csr);
    851 		goto abort;
    852 	}
    853 
    854 	switch (phase) {
    855 	case CMD_PHASE:
    856 		if (sbicxfstart (regs, clen, phase, wait))
    857 			if (sbicxfout (regs, clen, cbuf, phase))
    858 				goto abort;
    859 		phase = xferphase;
    860 		break;
    861 	case DATA_IN_PHASE:
    862 		if (len <= 0)
    863 			goto abort;
    864 		wait = sbic_data_wait;
    865 		if (sbicxfstart(regs, len, phase, wait))
    866 			sbicxfin(regs, len, buf);
    867 		phase = STATUS_PHASE;
    868 		break;
    869 	case MESG_IN_PHASE:
    870 		if (sbicxfstart(regs, sizeof(dev->sc_msg), phase, wait) == 0)
    871 			break;
    872 		dev->sc_msg[0] = 0xff;
    873 		sbicxfin(regs, sizeof(dev->sc_msg), dev->sc_msg);
    874 		/*
    875 		 * get the command completion interrupt, or we
    876 		 * can't send a new command (LCI)
    877 		 */
    878 		SBIC_WAIT(regs, SBIC_ASR_INT, wait);
    879 		GET_SBIC_csr(regs, csr);
    880 #ifdef DEBUG
    881 		if (sync_debug)
    882 			printf("msgin done csr 0x%x\n", csr);
    883 #endif
    884 		/*
    885 		 * test whether this is a reply to our sync
    886 		 * request
    887 		 */
    888 		if (dev->sc_msg[0] == MSG_EXT_MESSAGE && dev->sc_msg[1] == 3
    889 		    && dev->sc_msg[2] == MSG_SYNC_REQ) {
    890 
    891 			dev->sc_sync[target].period = sbicfromscsiperiod(dev,
    892 			    regs, dev->sc_msg[3]);
    893 			dev->sc_sync[target].offset = dev->sc_msg[4];
    894 			dev->sc_sync[target].state = SYNC_DONE;
    895 			SET_SBIC_syn(regs, SBIC_SYN(dev->sc_sync[target].offset,
    896 			    dev->sc_sync[target].period));
    897 			/* ACK the message */
    898 			SET_SBIC_cmd(regs, SBIC_CMD_CLR_ACK);
    899 			WAIT_CIP(regs);
    900 			phase = CMD_PHASE;  /* or whatever */
    901 			printf("%s: target %d now synchronous,"
    902 			    " period=%dns, offset=%d.\n",
    903 			    dev->sc_dev.dv_xname, target, dev->sc_msg[3] * 4,
    904 			    dev->sc_msg[4]);
    905 		} else if (dev->sc_msg[0] == MSG_REJECT
    906 		    && dev->sc_sync[target].state == SYNC_SENT) {
    907 #ifdef DEBUG
    908 			if (sync_debug)
    909 				printf("target %d rejected sync, going async\n",
    910 				    target);
    911 #endif
    912 			dev->sc_sync[target].period = sbic_min_period;
    913 			dev->sc_sync[target].offset = 0;
    914 			dev->sc_sync[target].state = SYNC_DONE;
    915 			SET_SBIC_syn(regs, SBIC_SYN(dev->sc_sync[target].offset,
    916 			    dev->sc_sync[target].period));
    917 			/* ACK the message */
    918 			SET_SBIC_cmd(regs, SBIC_CMD_CLR_ACK);
    919 			WAIT_CIP(regs);
    920 			phase = CMD_PHASE;  /* or whatever */
    921 		} else if (dev->sc_msg[0] == MSG_REJECT) {
    922 			/*
    923 			 * we'll never REJECt a REJECT message..
    924 			 */
    925 			/* ACK the message */
    926 			SET_SBIC_cmd(regs, SBIC_CMD_CLR_ACK);
    927 			WAIT_CIP(regs);
    928 			phase = CMD_PHASE;  /* or whatever */
    929 		} else if (dev->sc_msg[0] == MSG_CMD_COMPLETE
    930 		    || dev->sc_msg[0] == 0xff) {
    931 			/* !! KLUDGE ALERT !! quite a few drives don't seem to
    932 			 * really like the current way of sending the
    933 			 * sync-handshake together with the ident-message, and
    934 			 * they react by sending command-complete and
    935 			 * disconnecting right after returning the valid sync
    936 			 * handshake. So, all I can do is reselect the drive,
    937 			 * and hope it won't disconnect again. I don't think
    938 			 * this is valid behavior, but I can't help fixing a
    939 			 * problem that apparently exists.
    940 			 *
    941 			 * Note: we should not get here on `normal' command
    942 			 * completion, as that condition is handled by the
    943 			 * high-level sel&xfer resume command used to walk
    944 			 * thru status/cc-phase.
    945 			 */
    946 
    947 #ifdef DEBUG
    948 			if (sync_debug)
    949 				printf ("GOT CMD-COMPLETE! %d acting weird.."
    950 				    " waiting for disconnect...\n", target);
    951 #endif
    952 			/* ACK the message */
    953 			SET_SBIC_cmd (regs, SBIC_CMD_CLR_ACK);
    954 			WAIT_CIP(regs);
    955 
    956 			/* wait for disconnect */
    957 			while (csr != SBIC_CSR_DISC &&
    958 			    csr != SBIC_CSR_DISC_1) {
    959 				DELAY(1);
    960 				GET_SBIC_csr(regs, csr);
    961 			}
    962 #ifdef DEBUG
    963 			if (sync_debug)
    964 				printf ("ok.\nRetrying selection.\n");
    965 #endif
    966 			dev->sc_flags &= ~SBICF_SELECTED;
    967 			goto retry_selection;
    968 		} else {
    969 #ifdef DEBUG
    970 			if (sbic_debug || sync_debug)
    971 				printf ("Rejecting message 0x%02x\n",
    972 				    dev->sc_msg[0]);
    973 #endif
    974 			/* prepare to reject the message, NACK */
    975 			SET_SBIC_cmd(regs, SBIC_CMD_SET_ATN);
    976 			WAIT_CIP(regs);
    977 			SET_SBIC_cmd(regs, SBIC_CMD_CLR_ACK);
    978 			WAIT_CIP(regs);
    979 			phase = MESG_OUT_PHASE;
    980 		}
    981 		break;
    982 
    983 	case MESG_OUT_PHASE:
    984 #ifdef DEBUG
    985 		if (sync_debug)
    986 			printf ("sending REJECT msg to last msg.\n");
    987 #endif
    988 		/*
    989 		 * should only get here on reject,
    990 		 * since it's always US that
    991 		 * initiate a sync transfer
    992 		 */
    993 		SEND_BYTE(regs, MSG_REJECT);
    994 		phase = STATUS_PHASE;
    995 		break;
    996 	case DATA_OUT_PHASE:
    997 		if (len <= 0)
    998 			goto abort;
    999 		wait = sbic_data_wait;
   1000 		if (sbicxfstart(regs, len, phase, wait))
   1001 			if (sbicxfout (regs, len, buf, phase))
   1002 				goto abort;
   1003 		phase = STATUS_PHASE;
   1004 		break;
   1005 	case STATUS_PHASE:
   1006 		/*
   1007 		 * the sbic does the status/cmd-complete reading ok,
   1008 		 * so do this with its hi-level commands.
   1009 		 */
   1010 		SBIC_TC_PUT(regs, 0);
   1011 		SET_SBIC_cmd_phase(regs, 0x46);
   1012 		SET_SBIC_cmd(regs, SBIC_CMD_SEL_ATN_XFER);
   1013 		phase = BUS_FREE_PHASE;
   1014 		break;
   1015 	case BUS_FREE_PHASE:
   1016 		goto out;
   1017 	default:
   1018 		printf("%s: unexpected phase %d in icmd from %d\n",
   1019 		    dev->sc_dev.dv_xname, phase, target);
   1020 		goto abort;
   1021 	}
   1022 
   1023 	/*
   1024 	 * make sure the last command was taken,
   1025 	 * ie. we're not hunting after an ignored command..
   1026 	 */
   1027 	GET_SBIC_asr(regs, asr);
   1028 	if (asr & SBIC_ASR_LCI)
   1029 		goto abort;
   1030 
   1031 	/* tapes may take a loooong time.. */
   1032 	while (asr & SBIC_ASR_BSY) {
   1033 		DELAY(1);
   1034 		GET_SBIC_asr(regs, asr);
   1035 	}
   1036 
   1037 	/*
   1038 	 * wait for last command to complete
   1039 	 */
   1040 	SBIC_WAIT (regs, SBIC_ASR_INT, wait);
   1041 
   1042 	/*
   1043 	 * do it again
   1044 	 */
   1045 	goto new_phase;
   1046 abort:
   1047 	sbicabort(dev, regs, "icmd");
   1048 out:
   1049 	QPRINTF(("=STS:%02x=", dev->sc_stat[0]));
   1050 	return(dev->sc_stat[0]);
   1051 }
   1052 
   1053 /*
   1054  * Finish SCSI xfer command:  After the completion interrupt from
   1055  * a read/write operation, sequence through the final phases in
   1056  * programmed i/o.  This routine is a lot like sbicicmd except we
   1057  * skip (and don't allow) the select, cmd out and data in/out phases.
   1058  */
   1059 void
   1060 sbicxfdone(dev, regs, target)
   1061 	struct sbic_softc *dev;
   1062 	sbic_regmap_p regs;
   1063 	int target;
   1064 {
   1065 	u_char phase, csr;
   1066 	int s;
   1067 
   1068 	QPRINTF(("{"));
   1069 	s = splbio();
   1070 
   1071 	/*
   1072 	 * have the sbic complete on its own
   1073 	 */
   1074 	SBIC_TC_PUT(regs, 0);
   1075 	SET_SBIC_cmd_phase(regs, 0x46);
   1076 	SET_SBIC_cmd(regs, SBIC_CMD_SEL_ATN_XFER);
   1077 
   1078 	do {
   1079 		SBIC_WAIT (regs, SBIC_ASR_INT, 0);
   1080 		GET_SBIC_csr (regs, csr);
   1081 		QPRINTF(("%02x:", csr));
   1082 	} while ((csr != SBIC_CSR_DISC) && (csr != SBIC_CSR_DISC_1)
   1083 	    && (csr != SBIC_CSR_S_XFERRED));
   1084 
   1085 	dev->sc_flags &= ~SBICF_SELECTED;
   1086 
   1087 	GET_SBIC_cmd_phase (regs, phase);
   1088 	QPRINTF(("}%02x", phase));
   1089 	if (phase == 0x60)
   1090 		GET_SBIC_tlun(regs, dev->sc_stat[0]);
   1091 	else
   1092 		sbicerror(dev, regs, csr);
   1093 
   1094 	QPRINTF(("=STS:%02x=\n", dev->sc_stat[0]));
   1095 	splx(s);
   1096 }
   1097 
   1098 int
   1099 sbicgo(dev, xs)
   1100 	struct sbic_softc *dev;
   1101 	struct scsi_xfer *xs;
   1102 {
   1103 	int i, dmaflags, count, tcount, target, len, wait;
   1104 	u_char phase, csr, asr, cmd, *addr, *tmpaddr;
   1105 	sbic_regmap_p regs;
   1106 	struct dma_chain *dcp;
   1107 	u_int deoff, dspa;
   1108 	char *dmaend;
   1109 
   1110 	target = xs->sc_link->target;
   1111 	count = xs->datalen;
   1112 	addr = xs->data;
   1113 
   1114 	regs = dev->sc_sbicp;
   1115 	dmaend = NULL;
   1116 
   1117 	/*
   1118 	 * set the sbic into DMA mode
   1119 	 */
   1120 	SET_SBIC_control(regs, SBIC_CTL_EDI | SBIC_CTL_IDI |
   1121 	    SBIC_MACHINE_DMA_MODE);
   1122 
   1123 	/*
   1124 	 * select the SCSI bus (it's an error if bus isn't free)
   1125 	 */
   1126 	if (sbicselectbus(dev, regs, target, xs->sc_link->lun,
   1127 	    dev->sc_scsiaddr)) {
   1128 		dev->sc_dmafree(dev);
   1129 		return(-1);
   1130 	}
   1131 
   1132 	/*
   1133 	 * Wait for a phase change (or error) then let the device
   1134 	 * sequence us through command phase (we may have to take
   1135 	 * a msg in/out before doing the command).  If the disk has
   1136 	 * to do a seek, it may be a long time until we get a change
   1137 	 * to data phase so, in the absense of an explicit phase
   1138 	 * change, we assume data phase will be coming up and tell
   1139 	 * the SPC to start a transfer whenever it does.  We'll get
   1140 	 * a service required interrupt later if this assumption is
   1141 	 * wrong.  Otherwise we'll get a service required int when
   1142 	 * the transfer changes to status phase.
   1143 	 */
   1144 	phase = CMD_PHASE;
   1145 
   1146 new_phase:
   1147 	wait = sbic_cmd_wait;
   1148 	switch (phase) {
   1149 	case CMD_PHASE:
   1150 		if (sbicxfstart(regs, xs->cmdlen, phase, wait))
   1151 			if (sbicxfout(regs, xs->cmdlen, xs->cmd, phase))
   1152 				goto abort;
   1153 		break;
   1154 	case MESG_IN_PHASE:
   1155 		if (sbicxfstart(regs, sizeof(dev->sc_msg), phase, wait) == 0)
   1156 			break;
   1157 
   1158 		sbicxfin(regs, sizeof(dev->sc_msg), dev->sc_msg);
   1159 		/*
   1160 		 * prepare to reject any mesgin,
   1161 		 * no matter what it might be..
   1162 		 */
   1163 		SET_SBIC_cmd(regs, SBIC_CMD_SET_ATN);
   1164 		WAIT_CIP(regs);
   1165 		SET_SBIC_cmd(regs, SBIC_CMD_CLR_ACK);
   1166 		phase = MESG_OUT_PHASE;
   1167 		break;
   1168 	case MESG_OUT_PHASE:
   1169 		SEND_BYTE(regs, MSG_REJECT);
   1170 		phase = STATUS_PHASE;
   1171 		break;
   1172 	case DATA_IN_PHASE:
   1173 	case DATA_OUT_PHASE:
   1174 		goto out;
   1175 	/*
   1176 	 * status phase can happen, if the issued read/write command
   1177 	 * is illegal (for example, reading after EOT on tape) and the
   1178 	 * device doesn't even go to data in/out phase. So handle this
   1179 	 * here normally, instead of going thru abort-handling.
   1180 	 */
   1181 	case STATUS_PHASE:
   1182 		dev->sc_dmafree(dev);
   1183 		sbicxfdone(dev, regs, target);
   1184 		dev->sc_flags &= ~(SBICF_INDMA | SBICF_BBUF);
   1185 		sbic_scsidone(dev, dev->sc_stat[0]);
   1186 		return(0);
   1187 	default:
   1188 		printf("%s: unexpected phase %d in go from %d\n", phase,
   1189 		    dev->sc_dev.dv_xname, target);
   1190 		goto abort;
   1191 	}
   1192 
   1193 	/*
   1194 	 * make sure the last command was taken,
   1195 	 * ie. we're not hunting after an ignored command..
   1196 	 */
   1197 	GET_SBIC_asr(regs, asr);
   1198 	if (asr & SBIC_ASR_LCI)
   1199 		goto abort;
   1200 
   1201 	/*
   1202 	 * tapes may take a loooong time..
   1203 	 */
   1204 	while (asr & SBIC_ASR_BSY) {
   1205 		DELAY(1);
   1206 		GET_SBIC_asr(regs, asr);
   1207 	}
   1208 
   1209 	if (wait <= 0)
   1210 		goto abort;
   1211 
   1212 	/*
   1213 	 * wait for last command to complete
   1214 	 */
   1215 	SBIC_WAIT(regs, SBIC_ASR_INT, wait);
   1216 
   1217 	GET_SBIC_csr(regs, csr);
   1218 	QPRINTF((">CSR:%02x<", csr));
   1219 
   1220 	/*
   1221 	 * requesting some new phase
   1222 	 */
   1223 	if ((csr != 0xff) && (csr & 0xf0) && (csr & 0x08))
   1224 		phase = csr & PHASE;
   1225 	else {
   1226 		sbicerror(dev, regs, csr);
   1227 		goto abort;
   1228 	}
   1229 	/*
   1230 	 * start again with for new phase
   1231 	 */
   1232 	goto new_phase;
   1233 out:
   1234 	dmaflags = 0;
   1235 	if (xs->flags & SCSI_DATA_IN)
   1236 		dmaflags |= DMAGO_READ;
   1237 
   1238 	if (count > MAXPHYS)
   1239 		printf("sbicgo: bp->b_bcount > MAXPHYS %08x\n", count);
   1240 
   1241 #ifdef DEBUG
   1242 	++sbicdma_ops;			/* count total DMA operations */
   1243 #endif
   1244 	if (dev->sc_flags & SBICF_BADDMA &&
   1245 	    sbiccheckdmap(addr, count, dev->sc_dmamask)) {
   1246 		/*
   1247 		 * need to bounce the dma.
   1248 		 */
   1249 		if (dmaflags & DMAGO_READ) {
   1250 			dev->sc_flags |= SBICF_BBUF;
   1251 			dev->sc_dmausrbuf = addr;
   1252 			dev->sc_dmausrlen = count;
   1253 		} else {	/* write: copy to dma buffer */
   1254 			bcopy (addr, dev->sc_dmabuffer, count);
   1255 		}
   1256 		addr = dev->sc_dmabuffer;	/* and use dma buffer */
   1257 #ifdef DEBUG
   1258 		++sbicdma_bounces;		/* count number of bounced */
   1259 #endif
   1260 	}
   1261 	tmpaddr = addr;
   1262 	len = count;
   1263 #ifdef DEBUG
   1264 	if (sbic_dma_debug & DDB_FOLLOW)
   1265 		printf("sbicgo(%d, %x, %x, %x)\n", dev->sc_dev.dv_unit,
   1266 		    addr, count, dmaflags);
   1267 #endif
   1268 	/*
   1269 	 * Build the DMA chain
   1270 	 */
   1271 	for (dcp = dev->sc_chain; count > 0; dcp++) {
   1272 		dcp->dc_addr = (char *) kvtop(addr);
   1273 		if (count < (tcount = NBPG - ((int)addr & PGOFSET)))
   1274 			tcount = count;
   1275 		addr += tcount;
   1276 		count -= tcount;
   1277 		dcp->dc_count = tcount >> 1;
   1278 
   1279 		/*
   1280 		 * check if contigous, if not mark new end
   1281 		 * else increment end and count on previous.
   1282 		 */
   1283 		if (dcp->dc_addr != dmaend) {
   1284 			dmaend = dcp->dc_addr + tcount;
   1285 #ifdef DEBUG
   1286 			if (dcp != dev->sc_chain)
   1287 				++sbicdma_misses;	/* count non-contiguous */
   1288 #endif
   1289 		} else {
   1290 			dcp--;
   1291 			dmaend += tcount;
   1292 			dcp->dc_count += tcount >> 1;
   1293 #ifdef DEBUG
   1294 			++sbicdma_hits;	/* count contiguous */
   1295 #endif
   1296 		}
   1297 	}
   1298 
   1299 	dev->sc_cur = dev->sc_chain;
   1300 	dev->sc_last = --dcp;
   1301 	dev->sc_tcnt = dev->sc_cur->dc_count << 1;
   1302 
   1303 #ifdef DEBUG
   1304 	if (sbic_dma_debug & DDB_IO) {
   1305 		for (dcp = dev->sc_chain; dcp <= dev->sc_last; dcp++)
   1306 			printf("  %d: %d@%x\n", dcp-dev->sc_chain,
   1307 			    dcp->dc_count, dcp->dc_addr);
   1308 	}
   1309 #endif
   1310 
   1311 	/*
   1312 	 * push the data cash
   1313 	 */
   1314 #if 0
   1315 	DCIS();
   1316 #elif defined(M68040)
   1317 	if (cpu040) {
   1318 		dma_cachectl(tmpaddr, len);
   1319 
   1320 		dspa = (u_int)dev->sc_chain[0].dc_addr;
   1321 		deoff = (u_int)dev->sc_last->dc_addr
   1322 		    + (dev->sc_last->dc_count >> 1);
   1323 		if ((dspa & 0xF) || (deoff & 0xF))
   1324 			dev->sc_flags |= SBICF_DCFLUSH;
   1325 	}
   1326 #endif
   1327 
   1328 	/*
   1329 	 * dmago() also enables interrupts for the sbic
   1330 	 */
   1331 	i = dev->sc_dmago(dev, addr, xs->datalen, dmaflags);
   1332 
   1333 	SBIC_TC_PUT(regs, (unsigned)i);
   1334 	SET_SBIC_cmd(regs, SBIC_CMD_XFER_INFO);
   1335 
   1336 	return(0);
   1337 
   1338 abort:
   1339 	sbicabort(dev, regs, "go");
   1340 	dev->sc_dmafree(dev);
   1341 	return(-1);
   1342 }
   1343 
   1344 
   1345 int
   1346 sbicintr(dev)
   1347 	struct sbic_softc *dev;
   1348 {
   1349 	sbic_regmap_p regs;
   1350 	struct dma_chain *df, *dl;
   1351 	u_char asr, csr;
   1352 	int i;
   1353 
   1354 	regs = dev->sc_sbicp;
   1355 
   1356 	/*
   1357 	 * pending interrupt?
   1358 	 */
   1359 	GET_SBIC_asr (regs, asr);
   1360 	if ((asr & SBIC_ASR_INT) == 0)
   1361 		return(0);
   1362 
   1363 	GET_SBIC_csr(regs, csr);
   1364 	QPRINTF(("[0x%x]", csr));
   1365 
   1366 	if (csr == (SBIC_CSR_XFERRED|STATUS_PHASE)
   1367 	    || csr == (SBIC_CSR_MIS|STATUS_PHASE)
   1368 	    || csr == (SBIC_CSR_MIS_1|STATUS_PHASE)
   1369 	    || csr == (SBIC_CSR_MIS_2|STATUS_PHASE)) {
   1370 		/*
   1371 		 * this should be the normal i/o completion case.
   1372 		 * get the status & cmd complete msg then let the
   1373 		 * device driver look at what happened.
   1374 		 */
   1375 		sbicxfdone(dev, regs, dev->sc_xs->sc_link->target);
   1376 		/*
   1377 		 * check for overlapping cache line, flush if so
   1378 		 */
   1379 #ifdef M68040
   1380 		if (dev->sc_flags & SBICF_DCFLUSH) {
   1381 			df = dev->sc_chain;
   1382 			dl = dev->sc_last;
   1383 			DCFL(df->dc_addr);
   1384 			DCFL(dl->dc_addr + (dl->dc_count >> 1));
   1385 		}
   1386 #endif
   1387 		dev->sc_dmafree(dev);
   1388 		if (dev->sc_flags & SBICF_BBUF) {
   1389 			bcopy(dev->sc_dmabuffer, dev->sc_dmausrbuf,
   1390 			    dev->sc_dmausrlen);
   1391 			if (dev->sc_dmausrbuf >= (u_char *)0xff000000)
   1392 				printf("%s: WARNING - dmausrbuf = %x\n",
   1393 				    dev->sc_dev.dv_xname, dev->sc_dmausrbuf);
   1394 			}
   1395 		dev->sc_flags &= ~(SBICF_INDMA | SBICF_BBUF | SBICF_DCFLUSH);
   1396 		sbic_scsidone(dev, dev->sc_stat[0]);
   1397 	} else if (csr == (SBIC_CSR_XFERRED|DATA_OUT_PHASE)
   1398 	    || csr == (SBIC_CSR_XFERRED|DATA_IN_PHASE)
   1399 	    || csr == (SBIC_CSR_MIS|DATA_OUT_PHASE)
   1400 	    || csr == (SBIC_CSR_MIS|DATA_IN_PHASE)
   1401 	    || csr == (SBIC_CSR_MIS_1|DATA_OUT_PHASE)
   1402 	    || csr == (SBIC_CSR_MIS_1|DATA_IN_PHASE)
   1403 	    || csr == (SBIC_CSR_MIS_2|DATA_OUT_PHASE)
   1404 	    || csr == (SBIC_CSR_MIS_2|DATA_IN_PHASE)) {
   1405 		/*
   1406 		 * do scatter-gather dma
   1407 		 * hacking the controller chip, ouch..
   1408 		 */
   1409 		/*
   1410 		 * set next dma addr and dec count
   1411 		 */
   1412 		dev->sc_cur->dc_addr += dev->sc_tcnt;
   1413 		dev->sc_cur->dc_count -= (dev->sc_tcnt >> 1);
   1414 
   1415 		if (dev->sc_cur->dc_count == 0)
   1416 			++dev->sc_cur;		/* advance to next segment */
   1417 
   1418 		i = dev->sc_dmanext(dev);
   1419 		SBIC_TC_PUT(regs, (unsigned)i);
   1420 		SET_SBIC_cmd(regs, SBIC_CMD_XFER_INFO);
   1421 	} else {
   1422 		/*
   1423 		 * Something unexpected happened -- deal with it.
   1424 		 */
   1425 		dev->sc_dmastop(dev);
   1426 		sbicerror(dev, regs, csr);
   1427 		sbicabort(dev, regs, "intr");
   1428 		if (dev->sc_flags & SBICF_INDMA) {
   1429 			/*
   1430 			 * check for overlapping cache line, flush if so
   1431 			 */
   1432 #ifdef M68040
   1433 			if (dev->sc_flags & SBICF_DCFLUSH) {
   1434 				df = dev->sc_chain;
   1435 				dl = dev->sc_last;
   1436 				DCFL(df->dc_addr);
   1437 				DCFL(dl->dc_addr + (dl->dc_count >> 1));
   1438 			}
   1439 #endif
   1440 			dev->sc_flags &=
   1441 			    ~(SBICF_INDMA | SBICF_BBUF | SBICF_DCFLUSH);
   1442 			dev->sc_dmafree(dev);
   1443 			sbic_scsidone(dev, -1);
   1444 		}
   1445 	}
   1446 	return(1);
   1447 }
   1448 
   1449 /*
   1450  * Check if DMA can not be used with specified buffer
   1451  */
   1452 
   1453 int
   1454 sbiccheckdmap(bp, len, mask)
   1455 	void *bp;
   1456 	u_long len, mask;
   1457 {
   1458 	u_char *buffer;
   1459 	u_long phy_buf;
   1460 	u_long phy_len;
   1461 
   1462 	buffer = bp;
   1463 
   1464 	if (len == 0)
   1465 		return(0);
   1466 
   1467 	while (len) {
   1468 		phy_buf = kvtop(buffer);
   1469 		if (len < (phy_len = NBPG - ((int) buffer & PGOFSET)))
   1470 			phy_len = len;
   1471 		if (phy_buf & mask)
   1472 			return(1);
   1473 		buffer += phy_len;
   1474 		len -= phy_len;
   1475 	}
   1476 	return(0);
   1477 }
   1478 
   1479 int
   1480 sbictoscsiperiod(dev, regs, a)
   1481 	struct sbic_softc *dev;
   1482 	sbic_regmap_p regs;
   1483 	int a;
   1484 {
   1485 	unsigned int fs;
   1486 
   1487 	/*
   1488 	 * cycle = DIV / (2*CLK)
   1489 	 * DIV = FS+2
   1490 	 * best we can do is 200ns at 20Mhz, 2 cycles
   1491 	 */
   1492 
   1493 	GET_SBIC_myid(regs,fs);
   1494 	fs = (fs >>6) + 2;		/* DIV */
   1495 	fs = (fs * 10000) / (dev->sc_clkfreq<<1);	/* Cycle, in ns */
   1496 	if (a < 2) a = 8;		/* map to Cycles */
   1497 	return ((fs*a)>>2);		/* in 4 ns units */
   1498 }
   1499 
   1500 int
   1501 sbicfromscsiperiod(dev, regs, p)
   1502 	struct sbic_softc *dev;
   1503 	sbic_regmap_p regs;
   1504 	int p;
   1505 {
   1506 	register unsigned int fs, ret;
   1507 
   1508 	/* Just the inverse of the above */
   1509 
   1510 	GET_SBIC_myid(regs,fs);
   1511 	fs = (fs >>6) + 2;		/* DIV */
   1512 	fs = (fs * 10000) / (dev->sc_clkfreq<<1);   /* Cycle, in ns */
   1513 
   1514 	ret = p << 2;			/* in ns units */
   1515 	ret = ret / fs;			/* in Cycles */
   1516 	if (ret < sbic_min_period)
   1517 		return(sbic_min_period);
   1518 
   1519 	/* verify rounding */
   1520 	if (sbictoscsiperiod(dev, regs, ret) < p)
   1521 		ret++;
   1522 	return (ret >= 8) ? 0 : ret;
   1523 }
   1524 
   1525