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