Home | History | Annotate | Line # | Download | only in dev
sci.c revision 1.10
      1 /*	$NetBSD: sci.c,v 1.10 1995/01/05 07:22:46 chopps Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1994 Michael L. Hitch
      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 NCR 5380 scsi adaptor driver
     44  */
     45 
     46 #include <sys/param.h>
     47 #include <sys/systm.h>
     48 #include <sys/device.h>
     49 #include <sys/buf.h>
     50 #include <scsi/scsi_all.h>
     51 #include <scsi/scsiconf.h>
     52 #include <vm/vm.h>
     53 #include <vm/vm_kern.h>
     54 #include <vm/vm_page.h>
     55 #include <machine/pmap.h>
     56 #include <machine/cpu.h>
     57 #include <amiga/amiga/device.h>
     58 #include <amiga/amiga/custom.h>
     59 #include <amiga/dev/scireg.h>
     60 #include <amiga/dev/scivar.h>
     61 
     62 /*
     63  * SCSI delays
     64  * In u-seconds, primarily for state changes on the SPC.
     65  */
     66 #define	SCI_CMD_WAIT	50000	/* wait per step of 'immediate' cmds */
     67 #define	SCI_DATA_WAIT	50000	/* wait per data in/out step */
     68 #define	SCI_INIT_WAIT	50000	/* wait per step (both) during init */
     69 
     70 #define	b_cylin		b_resid
     71 
     72 int  sciicmd __P((struct sci_softc *, int, void *, int, void *, int,u_char));
     73 int  scigo __P((struct sci_softc *, struct scsi_xfer *));
     74 int  scigetsense __P((struct sci_softc *, struct scsi_xfer *));
     75 int  sciselectbus __P((struct sci_softc *, u_char, u_char));
     76 void sciabort __P((struct sci_softc *, char *));
     77 void scierror __P((struct sci_softc *, u_char));
     78 void scireset __P((struct sci_softc *));
     79 void scisetdelay __P((int));
     80 void sci_scsidone __P((struct sci_softc *, int));
     81 void sci_donextcmd __P((struct sci_softc *));
     82 
     83 int sci_cmd_wait = SCI_CMD_WAIT;
     84 int sci_data_wait = SCI_DATA_WAIT;
     85 int sci_init_wait = SCI_INIT_WAIT;
     86 
     87 int sci_no_dma = 0;
     88 
     89 #ifdef DEBUG
     90 #define QPRINTF(a) if (sci_debug > 1) printf a
     91 int	sci_debug = 0;
     92 #else
     93 #define QPRINTF
     94 #endif
     95 
     96 /*
     97  * default minphys routine for sci based controllers
     98  */
     99 void
    100 sci_minphys(bp)
    101 	struct buf *bp;
    102 {
    103 	/*
    104 	 * no max transfer at this level
    105 	 */
    106 }
    107 
    108 /*
    109  * used by specific sci controller
    110  *
    111  * it appears that the higher level code does nothing with LUN's
    112  * so I will too.  I could plug it in, however so could they
    113  * in scsi_scsi_cmd().
    114  */
    115 int
    116 sci_scsicmd(xs)
    117 	struct scsi_xfer *xs;
    118 {
    119 	struct sci_pending *pendp;
    120 	struct sci_softc *dev;
    121 	struct scsi_link *slp;
    122 	int flags, s;
    123 
    124 	slp = xs->sc_link;
    125 	dev = slp->adapter_softc;
    126 	flags = xs->flags;
    127 
    128 	if (flags & SCSI_DATA_UIO)
    129 		panic("sci: scsi data uio requested");
    130 
    131 	if (dev->sc_xs && flags & SCSI_POLL)
    132 		panic("sci_scsicmd: busy");
    133 
    134 	s = splbio();
    135 	pendp = &dev->sc_xsstore[slp->target][slp->lun];
    136 	if (pendp->xs) {
    137 		splx(s);
    138 		return(TRY_AGAIN_LATER);
    139 	}
    140 
    141 	if (dev->sc_xs) {
    142 		pendp->xs = xs;
    143 		TAILQ_INSERT_TAIL(&dev->sc_xslist, pendp, link);
    144 		splx(s);
    145 		return(SUCCESSFULLY_QUEUED);
    146 	}
    147 	pendp->xs = NULL;
    148 	dev->sc_xs = xs;
    149 	splx(s);
    150 
    151 	/*
    152 	 * nothing is pending do it now.
    153 	 */
    154 	sci_donextcmd(dev);
    155 
    156 	if (flags & SCSI_POLL)
    157 		return(COMPLETE);
    158 	return(SUCCESSFULLY_QUEUED);
    159 }
    160 
    161 /*
    162  * entered with dev->sc_xs pointing to the next xfer to perform
    163  */
    164 void
    165 sci_donextcmd(dev)
    166 	struct sci_softc *dev;
    167 {
    168 	struct scsi_xfer *xs;
    169 	struct scsi_link *slp;
    170 	int flags, phase, stat;
    171 
    172 	xs = dev->sc_xs;
    173 	slp = xs->sc_link;
    174 	flags = xs->flags;
    175 
    176 	if (flags & SCSI_DATA_IN)
    177 		phase = DATA_IN_PHASE;
    178 	else if (flags & SCSI_DATA_OUT)
    179 		phase = DATA_OUT_PHASE;
    180 	else
    181 		phase = STATUS_PHASE;
    182 
    183 	if (flags & SCSI_RESET)
    184 		scireset(dev);
    185 
    186 	dev->sc_stat[0] = -1;
    187 	xs->cmd->bytes[0] |= slp->lun << 5;
    188 	if (phase == STATUS_PHASE || flags & SCSI_POLL)
    189 		stat = sciicmd(dev, slp->target, xs->cmd, xs->cmdlen,
    190 		    xs->data, xs->datalen, phase);
    191 	else if (scigo(dev, xs) == 0)
    192 		return;
    193 	else
    194 		stat = dev->sc_stat[0];
    195 
    196 	sci_scsidone(dev, stat);
    197 }
    198 
    199 void
    200 sci_scsidone(dev, stat)
    201 	struct sci_softc *dev;
    202 	int stat;
    203 {
    204 	struct sci_pending *pendp;
    205 	struct scsi_xfer *xs;
    206 	int s, donext;
    207 
    208 	xs = dev->sc_xs;
    209 #ifdef DIAGNOSTIC
    210 	if (xs == NULL)
    211 		panic("sci_scsidone");
    212 #endif
    213 	/*
    214 	 * is this right?
    215 	 */
    216 	xs->status = stat;
    217 
    218 	if (stat == 0)
    219 		xs->resid = 0;
    220 	else {
    221 		switch(stat) {
    222 		case SCSI_CHECK:
    223 			if (stat = scigetsense(dev, xs))
    224 				goto bad_sense;
    225 			xs->error = XS_SENSE;
    226 			break;
    227 		case SCSI_BUSY:
    228 			xs->error = XS_BUSY;
    229 			break;
    230 		bad_sense:
    231 		default:
    232 			xs->error = XS_DRIVER_STUFFUP;
    233 			QPRINTF(("sci_scsicmd() bad %x\n", stat));
    234 			break;
    235 		}
    236 	}
    237 	xs->flags |= ITSDONE;
    238 
    239 	/*
    240 	 * grab next command before scsi_done()
    241 	 * this way no single device can hog scsi resources.
    242 	 */
    243 	s = splbio();
    244 	pendp = dev->sc_xslist.tqh_first;
    245 	if (pendp == NULL) {
    246 		donext = 0;
    247 		dev->sc_xs = NULL;
    248 	} else {
    249 		donext = 1;
    250 		TAILQ_REMOVE(&dev->sc_xslist, pendp, link);
    251 		dev->sc_xs = pendp->xs;
    252 		pendp->xs = NULL;
    253 	}
    254 	splx(s);
    255 	scsi_done(xs);
    256 
    257 	if (donext)
    258 		sci_donextcmd(dev);
    259 }
    260 
    261 int
    262 scigetsense(dev, xs)
    263 	struct sci_softc *dev;
    264 	struct scsi_xfer *xs;
    265 {
    266 	struct scsi_sense rqs;
    267 	struct scsi_link *slp;
    268 	int stat;
    269 
    270 	slp = xs->sc_link;
    271 
    272 	rqs.opcode = REQUEST_SENSE;
    273 	rqs.byte2 = slp->lun << 5;
    274 #ifdef not_yet
    275 	rqs.length = xs->req_sense_length ? xs->req_sense_length :
    276 	    sizeof(xs->sense);
    277 #else
    278 	rqs.length = sizeof(xs->sense);
    279 #endif
    280 
    281 	rqs.unused[0] = rqs.unused[1] = rqs.control = 0;
    282 
    283 	return(sciicmd(dev, slp->target, &rqs, sizeof(rqs), &xs->sense,
    284 	    rqs.length, DATA_IN_PHASE));
    285 }
    286 
    287 void
    288 sciabort(dev, where)
    289 	struct sci_softc *dev;
    290 	char *where;
    291 {
    292 	printf ("%s: abort %s: csr = 0x%02x, bus = 0x%02x\n",
    293 	  dev->sc_dev.dv_xname, where, *dev->sci_csr, *dev->sci_bus_csr);
    294 
    295 	if (dev->sc_flags & SCI_SELECTED) {
    296 
    297 		/* lets just hope it worked.. */
    298 		dev->sc_flags &= ~SCI_SELECTED;
    299 		/* XXX */
    300 		scireset (dev);
    301 	}
    302 }
    303 
    304 /*
    305  * XXX Set/reset long delays.
    306  *
    307  * if delay == 0, reset default delays
    308  * if delay < 0,  set both delays to default long initialization values
    309  * if delay > 0,  set both delays to this value
    310  *
    311  * Used when a devices is expected to respond slowly (e.g. during
    312  * initialization).
    313  */
    314 void
    315 scisetdelay(del)
    316 	int del;
    317 {
    318 	static int saved_cmd_wait, saved_data_wait;
    319 
    320 	if (del) {
    321 		saved_cmd_wait = sci_cmd_wait;
    322 		saved_data_wait = sci_data_wait;
    323 		if (del > 0)
    324 			sci_cmd_wait = sci_data_wait = del;
    325 		else
    326 			sci_cmd_wait = sci_data_wait = sci_init_wait;
    327 	} else {
    328 		sci_cmd_wait = saved_cmd_wait;
    329 		sci_data_wait = saved_data_wait;
    330 	}
    331 }
    332 
    333 void
    334 scireset(dev)
    335 	struct sci_softc *dev;
    336 {
    337 	u_int i, s;
    338 	u_char my_id, csr;
    339 
    340 	dev->sc_flags &= ~SCI_SELECTED;
    341 	if (dev->sc_flags & SCI_ALIVE)
    342 		sciabort(dev, "reset");
    343 
    344 	printf("%s: ", dev->sc_dev.dv_xname);
    345 
    346 	s = splbio();
    347 	/* preserve our ID for now */
    348 	my_id = 7;
    349 
    350 	/*
    351 	 * Reset the chip
    352 	 */
    353 	*dev->sci_icmd = SCI_ICMD_TEST;
    354 	*dev->sci_icmd = SCI_ICMD_TEST | SCI_ICMD_RST;
    355 	delay (25);
    356 	*dev->sci_icmd = 0;
    357 
    358 	/*
    359 	 * Set up various chip parameters
    360 	 */
    361 	*dev->sci_icmd = 0;
    362 	*dev->sci_tcmd = 0;
    363 	*dev->sci_sel_enb = 0;
    364 
    365 	/* anything else was zeroed by reset */
    366 
    367 	splx (s);
    368 
    369 	printf("sci id %d\n", my_id);
    370 	dev->sc_flags |= SCI_ALIVE;
    371 }
    372 
    373 void
    374 scierror(dev, csr)
    375 	struct sci_softc *dev;
    376 	u_char csr;
    377 {
    378 	struct scsi_xfer *xs;
    379 
    380 	xs = dev->sc_xs;
    381 
    382 #ifdef DIAGNOSTIC
    383 	if (xs == NULL)
    384 		panic("scierror");
    385 #endif
    386 	if (xs->flags & SCSI_SILENT)
    387 		return;
    388 
    389 	printf("%s: ", dev->sc_dev.dv_xname);
    390 	printf("csr == 0x%02i\n", csr);	/* XXX */
    391 }
    392 
    393 /*
    394  * select the bus, return when selected or error.
    395  */
    396 int
    397 sciselectbus(dev, target, our_addr)
    398         struct sci_softc *dev;
    399 	u_char target, our_addr;
    400 {
    401 	register int timeo = 2500;
    402 
    403 	QPRINTF (("sciselectbus %d\n", target));
    404 
    405 	/* if we're already selected, return */
    406 	if (dev->sc_flags & SCI_SELECTED)	/* XXXX */
    407 		return 1;
    408 
    409 	if ((*dev->sci_bus_csr & (SCI_BUS_BSY|SCI_BUS_SEL)) &&
    410 	    (*dev->sci_bus_csr & (SCI_BUS_BSY|SCI_BUS_SEL)) &&
    411 	    (*dev->sci_bus_csr & (SCI_BUS_BSY|SCI_BUS_SEL)))
    412 		return 1;
    413 
    414 	*dev->sci_tcmd = 0;
    415 	*dev->sci_odata = 0x80 + (1 << target);
    416 	*dev->sci_icmd = SCI_ICMD_DATA|SCI_ICMD_SEL;
    417 	while ((*dev->sci_bus_csr & SCI_BUS_BSY) == 0) {
    418 		if (--timeo > 0) {
    419 			delay(100);
    420 		} else {
    421 			break;
    422 		}
    423 	}
    424 	if (timeo) {
    425 		*dev->sci_icmd = 0;
    426 		dev->sc_flags |= SCI_SELECTED;
    427 		return (0);
    428 	}
    429 	*dev->sci_icmd = 0;
    430 	return (1);
    431 }
    432 
    433 int
    434 sci_ixfer_out(dev, len, buf, phase)
    435 	register struct sci_softc *dev;
    436 	int len;
    437 	register u_char *buf;
    438 	int phase;
    439 {
    440 	register int wait = sci_data_wait;
    441 	u_char csr;
    442 
    443 	QPRINTF(("sci_ixfer_out {%d} %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
    444 	  len, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5],
    445 	  buf[6], buf[7], buf[8], buf[9]));
    446 
    447 	*dev->sci_tcmd = phase;
    448 	*dev->sci_icmd = SCI_ICMD_DATA;
    449 	for (;len > 0; len--) {
    450 		csr = *dev->sci_bus_csr;
    451 		while (!(csr & SCI_BUS_REQ)) {
    452 			if ((csr & SCI_BUS_BSY) == 0 || --wait < 0) {
    453 #ifdef DEBUG
    454 				if (sci_debug)
    455 					printf("sci_ixfer_out fail: l%d i%x w%d\n",
    456 					  len, csr, wait);
    457 #endif
    458 				return (len);
    459 			}
    460 			delay(1);
    461 			csr = *dev->sci_bus_csr;
    462 		}
    463 
    464 		if (!(*dev->sci_csr & SCI_CSR_PHASE_MATCH))
    465 			break;
    466 		*dev->sci_odata = *buf;
    467 		*dev->sci_icmd = SCI_ICMD_DATA|SCI_ICMD_ACK;
    468 		buf++;
    469 		while (*dev->sci_bus_csr & SCI_BUS_REQ);
    470 		*dev->sci_icmd = SCI_ICMD_DATA;
    471 	}
    472 
    473 	QPRINTF(("sci_ixfer_out done\n"));
    474 	return (0);
    475 }
    476 
    477 void
    478 sci_ixfer_in(dev, len, buf, phase)
    479 	struct sci_softc *dev;
    480 	int len;
    481 	register u_char *buf;
    482 	int phase;
    483 {
    484 	int wait = sci_data_wait;
    485 	u_char *obp = buf;
    486 	u_char csr;
    487 	volatile register u_char *sci_bus_csr = dev->sci_bus_csr;
    488 	volatile register u_char *sci_data = dev->sci_data;
    489 	volatile register u_char *sci_icmd = dev->sci_icmd;
    490 
    491 	csr = *sci_bus_csr;
    492 
    493 	QPRINTF(("sci_ixfer_in %d, csr=%02x\n", len, csr));
    494 
    495 	*dev->sci_tcmd = phase;
    496 	*sci_icmd = 0;
    497 	for (;len > 0; len--) {
    498 		csr = *sci_bus_csr;
    499 		while (!(csr & SCI_BUS_REQ)) {
    500 			if (!(csr & SCI_BUS_BSY) || --wait < 0) {
    501 #ifdef DEBUG
    502 				if (sci_debug)
    503 					printf("sci_ixfer_in fail: l%d i%x w%d\n",
    504 					len, csr, wait);
    505 #endif
    506 				return;
    507 			}
    508 
    509 			delay(1);
    510 			csr = *sci_bus_csr;
    511 		}
    512 
    513 		if (!(*dev->sci_csr & SCI_CSR_PHASE_MATCH))
    514 			break;
    515 		*buf = *sci_data;
    516 		*sci_icmd = SCI_ICMD_ACK;
    517 		buf++;
    518 		while (*sci_bus_csr & SCI_BUS_REQ);
    519 		*sci_icmd = 0;
    520 	}
    521 
    522 	QPRINTF(("sci_ixfer_in {%d} %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
    523 	  len, obp[0], obp[1], obp[2], obp[3], obp[4], obp[5],
    524 	  obp[6], obp[7], obp[8], obp[9]));
    525 }
    526 
    527 /*
    528  * SCSI 'immediate' command:  issue a command to some SCSI device
    529  * and get back an 'immediate' response (i.e., do programmed xfer
    530  * to get the response data).  'cbuf' is a buffer containing a scsi
    531  * command of length clen bytes.  'buf' is a buffer of length 'len'
    532  * bytes for data.  The transfer direction is determined by the device
    533  * (i.e., by the scsi bus data xfer phase).  If 'len' is zero, the
    534  * command must supply no data.  'xferphase' is the bus phase the
    535  * caller expects to happen after the command is issued.  It should
    536  * be one of DATA_IN_PHASE, DATA_OUT_PHASE or STATUS_PHASE.
    537  */
    538 int
    539 sciicmd(dev, target, cbuf, clen, buf, len, xferphase)
    540 	struct sci_softc *dev;
    541 	void *cbuf, *buf;
    542 	int clen, len;
    543 	u_char xferphase;
    544 {
    545 	u_char phase, csr, asr;
    546 	register int wait;
    547 
    548 	/* select the SCSI bus (it's an error if bus isn't free) */
    549 	if (sciselectbus (dev, target, dev->sc_scsi_addr))
    550 		return -1;
    551 	/*
    552 	 * Wait for a phase change (or error) then let the device
    553 	 * sequence us through the various SCSI phases.
    554 	 */
    555 	dev->sc_stat[0] = 0xff;
    556 	dev->sc_msg[0] = 0xff;
    557 	phase = CMD_PHASE;
    558 	while (1) {
    559 		wait = sci_cmd_wait;
    560 
    561 		while ((*dev->sci_bus_csr & (SCI_BUS_REQ|SCI_BUS_BSY)) == SCI_BUS_BSY);
    562 
    563 		QPRINTF((">CSR:%02x<", *dev->sci_bus_csr));
    564 		if ((*dev->sci_bus_csr & SCI_BUS_REQ) == 0) {
    565 			return -1;
    566 		}
    567 		phase = SCI_PHASE(*dev->sci_bus_csr);
    568 
    569 		switch (phase) {
    570 		case CMD_PHASE:
    571 			if (sci_ixfer_out (dev, clen, cbuf, phase))
    572 				goto abort;
    573 			phase = xferphase;
    574 			break;
    575 
    576 		case DATA_IN_PHASE:
    577 			if (len <= 0)
    578 				goto abort;
    579 			wait = sci_data_wait;
    580 			sci_ixfer_in (dev, len, buf, phase);
    581 			phase = STATUS_PHASE;
    582 			break;
    583 
    584 		case DATA_OUT_PHASE:
    585 			if (len <= 0)
    586 				goto abort;
    587 			wait = sci_data_wait;
    588 			if (sci_ixfer_out (dev, len, buf, phase))
    589 				goto abort;
    590 			phase = STATUS_PHASE;
    591 			break;
    592 
    593 		case MESG_IN_PHASE:
    594 			dev->sc_msg[0] = 0xff;
    595 			sci_ixfer_in (dev, 1, dev->sc_msg,phase);
    596 			dev->sc_flags &= ~SCI_SELECTED;
    597 			while (*dev->sci_bus_csr & SCI_BUS_BSY);
    598 			goto out;
    599 			break;
    600 
    601 		case MESG_OUT_PHASE:
    602 			phase = STATUS_PHASE;
    603 			break;
    604 
    605 		case STATUS_PHASE:
    606 			sci_ixfer_in (dev, 1, dev->sc_stat, phase);
    607 			phase = MESG_IN_PHASE;
    608 			break;
    609 
    610 		case BUS_FREE_PHASE:
    611 			goto out;
    612 
    613 		default:
    614 		printf("sci: unexpected phase %d in icmd from %d\n",
    615 		  phase, target);
    616 		goto abort;
    617 		}
    618 #if 0
    619 		if (wait <= 0)
    620 			goto abort;
    621 #endif
    622 	}
    623 
    624 abort:
    625 	sciabort(dev, "icmd");
    626 out:
    627 	QPRINTF(("=STS:%02x=", dev->sc_stat[0]));
    628 	return (dev->sc_stat[0]);
    629 }
    630 
    631 int
    632 scigo(dev, xs)
    633 	struct sci_softc *dev;
    634 	struct scsi_xfer *xs;
    635 {
    636 	int i, count, target;
    637 	u_char phase, csr, asr, cmd, *addr;
    638 	int wait;
    639 
    640 	target = xs->sc_link->target;
    641 	count = xs->datalen;
    642 	addr = xs->data;
    643 
    644 	if (sci_no_dma)	{
    645 		sciicmd (dev, target, (u_char *) xs->cmd, xs->cmdlen,
    646 		  addr, count,
    647 		  xs->flags & SCSI_DATA_IN ? DATA_IN_PHASE : DATA_OUT_PHASE);
    648 
    649 		return (1);
    650 	}
    651 
    652 	/* select the SCSI bus (it's an error if bus isn't free) */
    653 	if (sciselectbus (dev, target, dev->sc_scsi_addr))
    654 		return -1;
    655 	/*
    656 	 * Wait for a phase change (or error) then let the device
    657 	 * sequence us through the various SCSI phases.
    658 	 */
    659 	dev->sc_stat[0] = 0xff;
    660 	dev->sc_msg[0] = 0xff;
    661 	phase = CMD_PHASE;
    662 	while (1) {
    663 		while ((*dev->sci_bus_csr & (SCI_BUS_REQ|SCI_BUS_BSY)) ==
    664 		  SCI_BUS_BSY);
    665 
    666 		QPRINTF((">CSR:%02x<", *dev->sci_bus_csr));
    667 		if ((*dev->sci_bus_csr & SCI_BUS_REQ) == 0) {
    668 			goto abort;
    669 		}
    670 		phase = SCI_PHASE(*dev->sci_bus_csr);
    671 
    672 		switch (phase) {
    673 		case CMD_PHASE:
    674 			if (sci_ixfer_out (dev, xs->cmdlen, xs->cmd, phase))
    675 				goto abort;
    676 			phase = xs->flags & SCSI_DATA_IN ? DATA_IN_PHASE : DATA_OUT_PHASE;
    677 			break;
    678 
    679 		case DATA_IN_PHASE:
    680 			if (count <= 0)
    681 				goto abort;
    682 			/* XXX use psuedo DMA if available */
    683 			if (count >= 128 && dev->dma_xfer_in)
    684 				(*dev->dma_xfer_in)(dev, count, addr, phase);
    685 			else
    686 				sci_ixfer_in (dev, count, addr, phase);
    687 			phase = STATUS_PHASE;
    688 			break;
    689 
    690 		case DATA_OUT_PHASE:
    691 			if (count <= 0)
    692 				goto abort;
    693 			/* XXX use psuedo DMA if available */
    694 			if (count >= 128 && dev->dma_xfer_out)
    695 				(*dev->dma_xfer_out)(dev, count, addr, phase);
    696 			else
    697 				if (sci_ixfer_out (dev, count, addr, phase))
    698 					goto abort;
    699 			phase = STATUS_PHASE;
    700 			break;
    701 
    702 		case MESG_IN_PHASE:
    703 			dev->sc_msg[0] = 0xff;
    704 			sci_ixfer_in (dev, 1, dev->sc_msg,phase);
    705 			dev->sc_flags &= ~SCI_SELECTED;
    706 			while (*dev->sci_bus_csr & SCI_BUS_BSY);
    707 			goto out;
    708 			break;
    709 
    710 		case MESG_OUT_PHASE:
    711 			phase = STATUS_PHASE;
    712 			break;
    713 
    714 		case STATUS_PHASE:
    715 			sci_ixfer_in (dev, 1, dev->sc_stat, phase);
    716 			phase = MESG_IN_PHASE;
    717 			break;
    718 
    719 		case BUS_FREE_PHASE:
    720 			goto out;
    721 
    722 		default:
    723 		printf("sci: unexpected phase %d in icmd from %d\n",
    724 		  phase, target);
    725 		goto abort;
    726 		}
    727 	}
    728 
    729 abort:
    730 	sciabort(dev, "go");
    731 out:
    732 	QPRINTF(("=STS:%02x=", dev->sc_stat[0]));
    733 	return (1);
    734 }
    735