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