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