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