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