Home | History | Annotate | Line # | Download | only in dev
si.c revision 1.8
      1 /*	$NetBSD: si.c,v 1.8 1994/10/26 09:09:27 cgd Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1994 Adam Glass, Gordon W. Ross
      5  * Copyright (C) 1993	Allen K. Briggs, Chris P. Caputo,
      6  *			Michael L. Finch, Bradley A. Grantham, and
      7  *			Lawrence A. Kesteloot
      8  * All rights reserved.
      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. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the Alice Group.
     21  * 4. The names of the Alice Group or any of its members may not be used
     22  *    to endorse or promote products derived from this software without
     23  *    specific prior written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY THE ALICE GROUP ``AS IS'' AND ANY EXPRESS OR
     26  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     27  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     28  * IN NO EVENT SHALL THE ALICE GROUP BE LIABLE FOR ANY DIRECT, INDIRECT,
     29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     30  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     31  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     32  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     33  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     34  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     35  */
     36 
     37 /* #define DEBUG 1 */
     38 
     39 /* XXX - Need to add support for real DMA. -gwr */
     40 /* #define PSEUDO_DMA 1 (broken) */
     41 
     42 static int si_debug=0;
     43 
     44 #include <sys/types.h>
     45 #include <sys/malloc.h>
     46 #include <sys/param.h>
     47 #include <sys/systm.h>
     48 #include <sys/errno.h>
     49 #include <sys/buf.h>
     50 #include <sys/proc.h>
     51 #include <sys/user.h>
     52 #include <sys/device.h>
     53 
     54 #include <machine/autoconf.h>
     55 #include <machine/isr.h>
     56 #include <machine/obio.h>
     57 
     58 #include <scsi/scsi_all.h>
     59 #include <scsi/scsi_debug.h>
     60 #include <scsi/scsiconf.h>
     61 
     62 #include "scsi_defs.h"
     63 #include "scsi_5380.h"
     64 
     65 #define SCI_PHASE_DISC		0	/* sort of ... */
     66 #define SCI_CLR_INTR(regs)	{register int temp = regs->sci_iack;}
     67 #define SCI_ACK(ptr,phase)	(ptr)->sci_tcmd = (phase)
     68 #define SCSI_TIMEOUT_VAL	10000000
     69 #define WAIT_FOR_NOT_REQ(ptr) {	\
     70 	int scsi_timeout = SCSI_TIMEOUT_VAL; \
     71 	while ( ((ptr)->sci_bus_csr & SCI_BUS_REQ) && \
     72 		 ((ptr)->sci_bus_csr & SCI_BUS_REQ) && \
     73 		 ((ptr)->sci_bus_csr & SCI_BUS_REQ) && \
     74 		 (--scsi_timeout) ); \
     75 	if (!scsi_timeout) { \
     76 		printf("scsi timeout--WAIT_FOR_NOT_REQ---%s, line %d.\n", \
     77 			__FILE__, __LINE__); \
     78 		goto scsi_timeout_error; \
     79 	} \
     80 	}
     81 #define WAIT_FOR_REQ(ptr) {	\
     82 	int scsi_timeout = SCSI_TIMEOUT_VAL; \
     83 	while ( (((ptr)->sci_bus_csr & SCI_BUS_REQ) == 0) && \
     84 		(((ptr)->sci_bus_csr & SCI_BUS_REQ) == 0) && \
     85 		(((ptr)->sci_bus_csr & SCI_BUS_REQ) == 0) && \
     86 		 (--scsi_timeout) ); \
     87 	if (!scsi_timeout) { \
     88 		printf("scsi timeout--WAIT_FOR_REQ---%s, line %d.\n", \
     89 			__FILE__, __LINE__); \
     90 		goto scsi_timeout_error; \
     91 	} \
     92 	}
     93 #define WAIT_FOR_BSY(ptr) {	\
     94 	int scsi_timeout = SCSI_TIMEOUT_VAL; \
     95 	while ( (((ptr)->sci_bus_csr & SCI_BUS_BSY) == 0) && \
     96 		(((ptr)->sci_bus_csr & SCI_BUS_BSY) == 0) && \
     97 		(((ptr)->sci_bus_csr & SCI_BUS_BSY) == 0) && \
     98 		 (--scsi_timeout) ); \
     99 	if (!scsi_timeout) { \
    100 		printf("scsi timeout--WAIT_FOR_BSY---%s, line %d.\n", \
    101 			__FILE__, __LINE__); \
    102 		goto scsi_timeout_error; \
    103 	} \
    104 	}
    105 
    106 #ifdef DDB
    107 int Debugger();
    108 #else
    109 #define Debugger() panic("Should call Debugger here %s:%d", \
    110 			 __FILE__, __LINE__)
    111 #endif
    112 
    113 struct ncr5380_softc {
    114     struct device sc_dev;
    115     void *sc_regs;
    116     int sc_adapter_target;
    117     struct scsi_link sc_link;
    118 };
    119 
    120 static u_int	ncr5380_adapter_info(struct ncr5380_softc *ncr5380);
    121 static void		ncr5380_minphys(struct buf *bp);
    122 static int		ncr5380_scsi_cmd(struct scsi_xfer *xs);
    123 
    124 static int		ncr5380_show_scsi_cmd(struct scsi_xfer *xs);
    125 static int		ncr5380_reset_target(int adapter, int target);
    126 static int		ncr5380_poll(int adapter, int timeout);
    127 static int		ncr5380_send_cmd(struct scsi_xfer *xs);
    128 
    129 void		ncr5380_intr(int adapter);
    130 
    131 static int	si_generic(int adapter, int id, int lun,
    132 			 struct scsi_generic *cmd, int cmdlen,
    133 			 void *databuf, int datalen);
    134 static int	si_group0(int adapter, int id, int lun,
    135 			    int opcode, int addr, int len,
    136 			    int flags, caddr_t databuf, int datalen);
    137 
    138 static char scsi_name[] = "si";
    139 
    140 struct scsi_adapter	ncr5380_switch = {
    141 	ncr5380_scsi_cmd,		/* scsi_cmd()		*/
    142 	ncr5380_minphys,		/* scsi_minphys()	*/
    143 	0,				/* open_target_lu()	*/
    144 	0,				/* close_target_lu()	*/
    145 	ncr5380_adapter_info,		/* adapter_info()	*/
    146 	scsi_name,			/* name			*/
    147 	0, 0				/* spare[2]		*/
    148 };
    149 
    150 /* This is copied from julian's bt driver */
    151 /* "so we have a default dev struct for our link struct." */
    152 struct scsi_device ncr_dev = {
    153 	NULL,		/* Use default error handler.	    */
    154 	NULL,		/* Use default start handler.		*/
    155 	NULL,		/* Use default async handler.	    */
    156 	NULL,		/* Use default "done" routine.	    */
    157 	"si",		/* name of device type */
    158 	0,			/* device type dependent flags */
    159 	0, 0		/* spare[2] */
    160 };
    161 
    162 extern int	matchbyname();
    163 static int	si_match();
    164 static void	si_attach();
    165 
    166 struct cfdriver sicd = {
    167 	NULL, "si", si_match, si_attach, DV_DULL,
    168 	sizeof(struct ncr5380_softc), NULL, 0,
    169 };
    170 
    171 static int
    172 si_print(aux, name)
    173 	void *aux;
    174 	char *name;
    175 {
    176 	if (name)
    177 		printf("%s: (sc_link = 0x%x)", name, (int) aux);
    178 	return UNCONF;
    179 }
    180 
    181 static int
    182 si_match(parent, cf, aux)
    183 	struct device	*parent;
    184 	struct cfdata	*cf;
    185 	void		*aux;
    186 {
    187     caddr_t si_addr;
    188     struct obio_cf_loc *obio_loc = (struct obio_cf_loc *) CFDATA_LOC(cf);
    189 
    190     si_addr = OBIO_DEFAULT_PARAM(caddr_t, obio_loc->obio_addr, OBIO_NCR_SCSI);
    191     return !obio_probe_byte(si_addr);
    192 }
    193 
    194 static void
    195 si_attach(parent, self, aux)
    196 	struct device	*parent, *self;
    197 	void		*aux;
    198 {
    199     caddr_t dvma_malloc();
    200 	int si_addr, level;
    201 	int unit = DEVICE_UNIT(self);
    202     struct ncr5380_softc *ncr5380 = (struct ncr5380_softc *) self;
    203     struct obio_cf_loc *obio_loc = OBIO_LOC(self);
    204 	struct cfdata *new_match;
    205 
    206     si_addr = OBIO_DEFAULT_PARAM(int, obio_loc->obio_addr, OBIO_NCR_SCSI);
    207     ncr5380->sc_regs = (sci_regmap_t *)
    208         obio_alloc(si_addr, OBIO_NCR_SCSI_SIZE);
    209 
    210     level = OBIO_DEFAULT_PARAM(int, obio_loc->obio_level, 2);
    211 
    212     ncr5380->sc_link.scsibus = unit;	/* needed? */
    213     ncr5380->sc_link.adapter_softc = ncr5380;
    214     ncr5380->sc_link.adapter_targ = 7;
    215     ncr5380->sc_link.adapter = &ncr5380_switch;
    216     ncr5380->sc_link.device = &ncr_dev;
    217 
    218     obio_print(si_addr, level);
    219     printf("\n");
    220 
    221     config_found(self, &(ncr5380->sc_link), si_print);
    222 }
    223 
    224 static u_int
    225 ncr5380_adapter_info(struct ncr5380_softc *ncr5380)
    226 {
    227 	return 1;
    228 }
    229 
    230 #define MIN_PHYS	65536	/*BARF!!!!*/
    231 static void
    232 ncr5380_minphys(struct buf *bp)
    233 {
    234 	if (bp->b_bcount > MIN_PHYS) {
    235 		printf("Uh-oh...  ncr5380_minphys setting bp->b_bcount = %x.\n", MIN_PHYS);
    236 		bp->b_bcount = MIN_PHYS;
    237 	}
    238 }
    239 #undef MIN_PHYS
    240 
    241 static int
    242 ncr5380_scsi_cmd(struct scsi_xfer *xs)
    243 {
    244 	int flags, s, r;
    245 
    246 	flags = xs->flags;
    247 	if (xs->bp) flags |= (SCSI_NOSLEEP);
    248 	if ( flags & ITSDONE ) {
    249 		printf("Already done?");
    250 		xs->flags &= ~ITSDONE;
    251 	}
    252 	if ( ! ( flags & INUSE ) ) {
    253 		printf("Not in use?");
    254 		xs->flags |= INUSE;
    255 	}
    256 
    257 	if ( flags & SCSI_RESET ) {
    258 		printf("flags & SCSIRESET.\n");
    259 		if ( ! ( flags & SCSI_NOSLEEP ) ) {
    260 			s = splbio();
    261 			ncr5380_reset_target(xs->sc_link->scsibus, xs->sc_link->target);
    262 			splx(s);
    263 			return(SUCCESSFULLY_QUEUED);
    264 		} else {
    265 			ncr5380_reset_target(xs->sc_link->scsibus, xs->sc_link->target);
    266 			if (ncr5380_poll(xs->sc_link->scsibus, xs->timeout)) {
    267 				return (HAD_ERROR);
    268 			}
    269 			return (COMPLETE);
    270 		}
    271 	}
    272 #if 0
    273 	/*
    274 	 * OK.  Now that that's over with, let's pack up that
    275 	 * SCSI puppy and send it off.  If we can, we'll just
    276 	 * queue and go; otherwise, we'll wait for the command
    277 	 * to finish.
    278 	 */
    279 	if ( ! ( flags & SCSI_NOSLEEP ) ) {
    280 		s = splbio();
    281 		ncr5380_send_cmd(xs);
    282 		splx(s);
    283 		return(SUCCESSFULLY_QUEUED);
    284 	}
    285 #endif
    286 
    287 	r = ncr5380_send_cmd(xs);
    288 	xs->flags |= ITSDONE;
    289 	scsi_done(xs);
    290 	switch(r) {
    291 		case COMPLETE:
    292 		case SUCCESSFULLY_QUEUED:
    293 			r = SUCCESSFULLY_QUEUED;
    294 			if (xs->flags&SCSI_NOMASK)
    295 				r = COMPLETE;
    296 			break;
    297 		default:
    298 			break;
    299 	}
    300 	return r;
    301 #if 0
    302 	do {
    303 		if (ncr5380_poll(xs->sc_link->scsibus, xs->timeout)) {
    304 			if ( ! ( xs->flags & SCSI_SILENT ) )
    305 				printf("cmd fail.\n");
    306 			cmd_cleanup
    307 			xs->error = XS_DRIVER_STUFFUP;
    308 			splx(s);
    309 		}
    310 	} while ( ! ( xs->flags & ITSDONE ) );
    311 #endif
    312 }
    313 
    314 static int
    315 ncr5380_show_scsi_cmd(struct scsi_xfer *xs)
    316 {
    317 	u_char	*b = (u_char *) xs->cmd;
    318 	int	i  = 0;
    319 
    320 	if ( ! ( xs->flags & SCSI_RESET ) ) {
    321 		printf("si(%d:%d:%d)-",
    322 			   xs->sc_link->scsibus,
    323 			   xs->sc_link->target,
    324 			   xs->sc_link->lun);
    325 		while (i < xs->cmdlen) {
    326 			if (i) printf(",");
    327 			printf("%x",b[i++]);
    328 		}
    329 		printf("-\n");
    330 	} else {
    331 		printf("si(%d:%d:%d)-RESET-\n",
    332 			   xs->sc_link->scsibus,
    333 			   xs->sc_link->target,
    334 			   xs->sc_link->lun);
    335 	}
    336 }
    337 
    338 /*
    339  * Actual chip control.
    340  */
    341 
    342 void
    343 ncr5380_intr(int adapter)
    344 {
    345     register struct ncr5380_softc *ncr5380 = sicd.cd_devs[adapter];
    346     register volatile sci_regmap_t *regs = ncr5380->sc_regs;
    347 
    348     SCI_CLR_INTR(regs);
    349     regs->sci_mode    = 0x00;
    350 #ifdef	DEBUG
    351 	printf ("ncr_intr\n");
    352 #endif
    353 }
    354 
    355 int
    356 scsi_irq_intr(void)
    357 {
    358 #if 0	/* XXX - no way to get regs */
    359     register volatile sci_regmap_t *regs = ncr;
    360 	if (regs->sci_csr != SCI_CSR_PHASE_MATCH)
    361 		printf("scsi_irq_intr called (not just phase match -- "
    362 			"csr = 0x%x, bus_csr = 0x%x).\n",
    363 			regs->sci_csr, regs->sci_bus_csr);
    364 	ncr5380_intr(0);
    365 #endif
    366 	return 1;
    367 }
    368 
    369 int
    370 scsi_drq_intr(void)
    371 {
    372 #if 0
    373 	printf("scsi_drq_intr called.\n");
    374 	ncr5380_intr(0);
    375 #endif
    376 	return 1;
    377 }
    378 
    379 static int
    380 ncr5380_reset_target(int adapter, int target)
    381 {
    382 	register struct ncr5380_softc *ncr5380 = sicd.cd_devs[adapter];
    383 	register volatile sci_regmap_t *regs = ncr5380->sc_regs;
    384 	int dummy;
    385 
    386 	regs->sci_icmd = SCI_ICMD_TEST;
    387 	regs->sci_icmd = SCI_ICMD_TEST | SCI_ICMD_RST;
    388 	delay(2500);
    389 	regs->sci_icmd = 0;
    390 
    391 	regs->sci_mode = 0;
    392 	regs->sci_tcmd = SCI_PHASE_DISC;
    393 	regs->sci_sel_enb = 0;
    394 
    395 	SCI_CLR_INTR(regs);
    396 	SCI_CLR_INTR(regs);
    397 }
    398 
    399 static int
    400 ncr5380_poll(int adapter, int timeout)
    401 {
    402 }
    403 
    404 static int
    405 ncr5380_send_cmd(struct scsi_xfer *xs)
    406 {
    407 	int	s;
    408 	int	sense;
    409 
    410 /*	ncr5380_show_scsi_cmd(xs); */
    411 	s = splbio();
    412 	sense = si_generic( xs->sc_link->scsibus, xs->sc_link->target,
    413 			  xs->sc_link->lun, xs->cmd, xs->cmdlen,
    414 			  xs->data, xs->datalen );
    415 	splx(s);
    416 	if (sense) {
    417 		switch (sense) {
    418 			case 0x02:	/* Check condition */
    419 #ifdef	DEBUG
    420 				printf("check cond. target %d.\n",
    421 					   xs->sc_link->target);
    422 #endif
    423 				delay(10);	/* Phil's fix for slow devices. */
    424 				s = splbio();
    425 				si_group0(xs->sc_link->scsibus,
    426 					    xs->sc_link->target,
    427 					    xs->sc_link->lun,
    428 					    0x3, 0x0,
    429 					    sizeof(struct scsi_sense_data),
    430 					    0, (caddr_t) &(xs->sense),
    431 					    sizeof(struct scsi_sense_data));
    432 				splx(s);
    433 				xs->error = XS_SENSE;
    434 				return HAD_ERROR;
    435 			case 0x08:	/* Busy */
    436 				xs->error = XS_BUSY;
    437 				return HAD_ERROR;
    438 			default:
    439 				xs->error = XS_DRIVER_STUFFUP;
    440 				return HAD_ERROR;
    441 		}
    442 	}
    443 	xs->error = XS_NOERROR;
    444 	return (COMPLETE);
    445 }
    446 
    447 static int
    448 si_select_target(register volatile sci_regmap_t *regs,
    449 	      u_char myid, u_char tid, int with_atn)
    450 {
    451 	register u_char	bid, icmd;
    452 	int		ret = SCSI_RET_RETRY;
    453 
    454 	if ((regs->sci_bus_csr & (SCI_BUS_BSY|SCI_BUS_SEL)) &&
    455 	    (regs->sci_bus_csr & (SCI_BUS_BSY|SCI_BUS_SEL)) &&
    456 	    (regs->sci_bus_csr & (SCI_BUS_BSY|SCI_BUS_SEL)))
    457 		return ret;
    458 
    459 	/* for our purposes.. */
    460 	myid = 1 << myid;
    461 	tid = 1 << tid;
    462 
    463 	regs->sci_sel_enb = 0; /* we don't want any interrupts. */
    464 	regs->sci_tcmd = 0;	/* get into a harmless state */
    465 	regs->sci_mode = 0;	/* get into a harmless state */
    466 
    467 	regs->sci_odata = myid;
    468 	regs->sci_mode = SCI_MODE_ARB;
    469 /*	regs->sci_mode |= SCI_MODE_ARB;	XXX? */
    470 	/* AIP might not set if BSY went true after we checked */
    471 	for (bid = 0; bid < 20; bid++)	/* 20usec circa */
    472 		if (regs->sci_icmd & SCI_ICMD_AIP)
    473 			break;
    474 	if ((regs->sci_icmd & SCI_ICMD_AIP) == 0) {
    475 		goto lost;
    476 	}
    477 
    478 	delay(2200);	/* 2.2 millisecond arbitration delay */
    479 
    480 	if (regs->sci_icmd & SCI_ICMD_LST) {
    481 #ifdef	DEBUG
    482 		printf ("lost 1\n");
    483 #endif
    484 		goto lost;
    485 	}
    486 
    487 	regs->sci_mode &= ~SCI_MODE_PAR_CHK;
    488 	bid = regs->sci_data;
    489 
    490 	if ((bid & ~myid) > myid) {
    491 #ifdef	DEBUG
    492 		printf ("lost 2\n");
    493 #endif
    494 		goto lost;
    495 	}
    496 	if (regs->sci_icmd & SCI_ICMD_LST) {
    497 #ifdef	DEBUG
    498 		printf ("lost 3\n");
    499 #endif
    500 		goto lost;
    501 	}
    502 
    503 	/* Won arbitration, enter selection phase now */
    504 	icmd = regs->sci_icmd & ~(SCI_ICMD_DIFF|SCI_ICMD_TEST);
    505 	icmd |= (with_atn ? (SCI_ICMD_SEL|SCI_ICMD_ATN) : SCI_ICMD_SEL);
    506 	icmd |= SCI_ICMD_BSY;
    507 	regs->sci_icmd = icmd;
    508 
    509 	if (regs->sci_icmd & SCI_ICMD_LST) {
    510 #ifdef	DEBUG
    511 		printf ("nosel\n");
    512 #endif
    513 		goto nosel;
    514 	}
    515 
    516 	/* XXX a target that violates specs might still drive the bus XXX */
    517 	/* XXX should put our id out, and after the delay check nothi XXX */
    518 	/* XXX ng else is out there.				      XXX */
    519 
    520 	delay2us();
    521 
    522 	regs->sci_tcmd = 0;
    523 	regs->sci_odata = myid | tid;
    524 	regs->sci_sel_enb = 0;
    525 
    526 /*	regs->sci_mode &= ~SCI_MODE_ARB;	 2 deskew delays, too */
    527 	regs->sci_mode = 0;			/* 2 deskew delays, too */
    528 
    529 	icmd |= SCI_ICMD_DATA;
    530 	icmd &= ~(SCI_ICMD_BSY);
    531 
    532 	regs->sci_icmd = icmd;
    533 
    534 	/* bus settle delay, 400ns */
    535 	delay2us(); /* too much (was 2) ? */
    536 
    537 /*	regs->sci_mode |= SCI_MODE_PAR_CHK; */
    538 
    539 	{
    540 		register int timeo  = 2500;/* 250 msecs in 100 usecs chunks */
    541 		while ((regs->sci_bus_csr & SCI_BUS_BSY) == 0) {
    542 			if (--timeo > 0) {
    543 				delay(100);
    544 			} else {
    545 				goto nodev;
    546 			}
    547 		}
    548 	}
    549 
    550 	icmd &= ~(SCI_ICMD_DATA|SCI_ICMD_SEL);
    551 	regs->sci_icmd = icmd;
    552 /*	regs->sci_sel_enb = myid;*/	/* looks like we should NOT have it */
    553 	return SCSI_RET_SUCCESS;
    554 nodev:
    555 	ret = SCSI_RET_DEVICE_DOWN;
    556 	regs->sci_sel_enb = myid;
    557 nosel:
    558 	icmd &= ~(SCI_ICMD_DATA|SCI_ICMD_SEL|SCI_ICMD_ATN);
    559 	regs->sci_icmd = icmd;
    560 lost:
    561 	regs->sci_mode = 0;
    562 
    563 	return ret;
    564 }
    565 
    566 sci_data_out(regs, phase, count, data)
    567 	register volatile sci_regmap_t	*regs;
    568 	unsigned char		*data;
    569 {
    570 	register unsigned char	icmd;
    571 	register int		cnt=0;
    572 
    573 	/* ..checks.. */
    574 
    575 	icmd = regs->sci_icmd & ~(SCI_ICMD_DIFF|SCI_ICMD_TEST);
    576 loop:
    577 	if (SCI_CUR_PHASE(regs->sci_bus_csr) != phase)
    578 		return cnt;
    579 
    580 	WAIT_FOR_REQ(regs);
    581 	icmd |= SCI_ICMD_DATA;
    582 	regs->sci_icmd = icmd;
    583 	regs->sci_odata = *data++;
    584 	icmd |= SCI_ICMD_ACK;
    585 	regs->sci_icmd = icmd;
    586 
    587 	icmd &= ~(SCI_ICMD_DATA|SCI_ICMD_ACK);
    588 	WAIT_FOR_NOT_REQ(regs);
    589 	regs->sci_icmd = icmd;
    590 	++cnt;
    591 	if (--count > 0)
    592 		goto loop;
    593 scsi_timeout_error:
    594 	return cnt;
    595 }
    596 
    597 sci_data_in(regs, phase, count, data)
    598 	register volatile sci_regmap_t	*regs;
    599 	unsigned char		*data;
    600 {
    601 	register unsigned char	icmd;
    602 	register int		cnt=0;
    603 
    604 	/* ..checks.. */
    605 
    606 	icmd = regs->sci_icmd & ~(SCI_ICMD_DIFF|SCI_ICMD_TEST);
    607 
    608 loop:
    609 	if (SCI_CUR_PHASE(regs->sci_bus_csr) != phase)
    610 		return cnt;
    611 
    612 	WAIT_FOR_REQ(regs);
    613 	*data++ = regs->sci_data;
    614 	icmd |= SCI_ICMD_ACK;
    615 	regs->sci_icmd = icmd;
    616 
    617 	icmd &= ~SCI_ICMD_ACK;
    618 	WAIT_FOR_NOT_REQ(regs);
    619 	regs->sci_icmd = icmd;
    620 	++cnt;
    621 	if (--count > 0)
    622 		goto loop;
    623 
    624 scsi_timeout_error:
    625 	return cnt;
    626 }
    627 
    628 static int
    629 si_command_transfer(register volatile sci_regmap_t *regs,
    630 		 int maxlen, u_char *data, u_char *status, u_char *msg)
    631 {
    632 	int	xfer=0, phase;
    633 
    634 /*	printf("command_transfer called for 0x%x.\n", *data); */
    635 
    636 	regs->sci_icmd = 0;
    637 
    638 	while (1) {
    639 
    640 		WAIT_FOR_REQ(regs);
    641 
    642 		phase = SCI_CUR_PHASE(regs->sci_bus_csr);
    643 
    644 		switch (phase) {
    645 			case SCSI_PHASE_CMD:
    646 				SCI_ACK(regs,SCSI_PHASE_CMD);
    647 				xfer += sci_data_out(regs, SCSI_PHASE_CMD,
    648 						   	maxlen, data);
    649 				return xfer;
    650 			case SCSI_PHASE_DATA_IN:
    651 				printf("Data in phase in command_transfer?\n");
    652 				return 0;
    653 			case SCSI_PHASE_DATA_OUT:
    654 				printf("Data out phase in command_transfer?\n");
    655 				return 0;
    656 			case SCSI_PHASE_STATUS:
    657 				SCI_ACK(regs,SCSI_PHASE_STATUS);
    658 				printf("status in command_transfer.\n");
    659 				sci_data_in(regs, SCSI_PHASE_STATUS,
    660 					  	1, status);
    661 				break;
    662 			case SCSI_PHASE_MESSAGE_IN:
    663 				SCI_ACK(regs,SCSI_PHASE_MESSAGE_IN);
    664 				printf("msgin in command_transfer.\n");
    665 				sci_data_in(regs, SCSI_PHASE_MESSAGE_IN,
    666 					  	1, msg);
    667 				break;
    668 			case SCSI_PHASE_MESSAGE_OUT:
    669 				SCI_ACK(regs,SCSI_PHASE_MESSAGE_OUT);
    670 				sci_data_out(regs, SCSI_PHASE_MESSAGE_OUT,
    671 					  	1, msg);
    672 				break;
    673 			default:
    674 				printf("Unexpected phase 0x%x in "
    675 					"command_transfer().\n", phase);
    676 scsi_timeout_error:
    677 				return xfer;
    678 				break;
    679 		}
    680 	}
    681 }
    682 
    683 static int
    684 si_data_transfer(register volatile sci_regmap_t *regs,
    685 	      int maxlen, u_char *data, u_char *status, u_char *msg)
    686 {
    687 	int	retlen = 0, xfer, phase;
    688 
    689 	regs->sci_icmd = 0;
    690 
    691 	*status = 0;
    692 
    693 	while (1) {
    694 
    695 		WAIT_FOR_REQ(regs);
    696 
    697 		phase = SCI_CUR_PHASE(regs->sci_bus_csr);
    698 
    699 		switch (phase) {
    700 			case SCSI_PHASE_CMD:
    701 				printf("Command phase in data_transfer().\n");
    702 				return retlen;
    703 			case SCSI_PHASE_DATA_IN:
    704 				SCI_ACK(regs,SCSI_PHASE_DATA_IN);
    705 #if PSEUDO_DMA
    706 				xfer = sci_pdma_in(regs, SCSI_PHASE_DATA_IN,
    707 						  	maxlen, data);
    708 #else
    709 				xfer = sci_data_in(regs, SCSI_PHASE_DATA_IN,
    710 						  	maxlen, data);
    711 #endif
    712 				retlen += xfer;
    713 				maxlen -= xfer;
    714 				break;
    715 			case SCSI_PHASE_DATA_OUT:
    716 				SCI_ACK(regs,SCSI_PHASE_DATA_OUT);
    717 #if PSEUDO_DMA
    718 				xfer = sci_pdma_out(regs, SCSI_PHASE_DATA_OUT,
    719 						   	maxlen, data);
    720 #else
    721 				xfer = sci_data_out(regs, SCSI_PHASE_DATA_OUT,
    722 						   	maxlen, data);
    723 #endif
    724 				retlen += xfer;
    725 				maxlen -= xfer;
    726 				break;
    727 			case SCSI_PHASE_STATUS:
    728 				SCI_ACK(regs,SCSI_PHASE_STATUS);
    729 				sci_data_in(regs, SCSI_PHASE_STATUS,
    730 					  	1, status);
    731 				break;
    732 			case SCSI_PHASE_MESSAGE_IN:
    733 				SCI_ACK(regs,SCSI_PHASE_MESSAGE_IN);
    734 				sci_data_in(regs, SCSI_PHASE_MESSAGE_IN,
    735 					  	1, msg);
    736 				if (*msg == 0) {
    737 					return retlen;
    738 				} else {
    739 					printf( "message 0x%x in "
    740 						"data_transfer.\n", *msg);
    741 				}
    742 				break;
    743 			case SCSI_PHASE_MESSAGE_OUT:
    744 				SCI_ACK(regs,SCSI_PHASE_MESSAGE_OUT);
    745 				sci_data_out(regs, SCSI_PHASE_MESSAGE_OUT,
    746 					  	1, msg);
    747 				break;
    748 			default:
    749 				printf( "Unexpected phase 0x%x in "
    750 					"data_transfer().\n", phase);
    751 scsi_timeout_error:
    752 				return retlen;
    753 				break;
    754 		}
    755 	}
    756 }
    757 
    758 static int
    759 si_dorequest(register volatile sci_regmap_t *regs,
    760 		int target, int lun, u_char *cmd, int cmdlen,
    761 		char *databuf, int datalen, int *sent, int *ret)
    762 {
    763 /* Returns 0 on success, -1 on internal error, or the status byte */
    764 	int	cmd_bytes_sent, r;
    765 	u_char	stat, msg, c;
    766 
    767 	*sent = 0;
    768 
    769 	if ( ( r = si_select_target(regs, 7, target, 1) ) != SCSI_RET_SUCCESS) {
    770 		*ret = r;
    771 		SCI_CLR_INTR(regs);
    772 		switch (r) {
    773 		case SCSI_RET_RETRY:
    774 			return 0x08;
    775 		default:
    776 			printf("si_select_target(target %d, lun %d) failed(%d).\n",
    777 				target, lun, r);
    778 		case SCSI_RET_DEVICE_DOWN:
    779 			return -1;
    780 		}
    781 	}
    782 
    783 	c = 0x80 | lun;
    784 
    785 	if ((cmd_bytes_sent = si_command_transfer(regs, cmdlen,
    786 				(u_char *) cmd, &stat, &c))
    787 	     != cmdlen) {
    788 		SCI_CLR_INTR(regs);
    789 		*ret = SCSI_RET_COMMAND_FAIL;
    790 		printf("Data underrun sending CCB (%d bytes of %d, sent).\n",
    791 			cmd_bytes_sent, cmdlen);
    792 		return -1;
    793 	}
    794 
    795 	*sent=si_data_transfer(regs, datalen, (u_char *)databuf,
    796 				  &stat, &msg);
    797 
    798 	*ret = 0;
    799 	return stat;
    800 }
    801 
    802 static int
    803 si_generic(int adapter, int id, int lun, struct scsi_generic *cmd,
    804   	 int cmdlen, void *databuf, int datalen)
    805 {
    806 	register struct ncr5380_softc *ncr5380 = sicd.cd_devs[adapter];
    807 	register volatile sci_regmap_t *regs = ncr5380->sc_regs;
    808 	int i,j,sent,ret;
    809 
    810 	if (cmd->opcode == TEST_UNIT_READY)	/* XXX */
    811 		cmd->bytes[0] = ((u_char) lun << 5);
    812 
    813 	i = si_dorequest(regs, id, lun, (u_char *) cmd, cmdlen,
    814 					 databuf, datalen, &sent, &ret);
    815 
    816 	return i;
    817 }
    818 
    819 static int
    820 si_group0(int adapter, int id, int lun, int opcode, int addr, int len,
    821 		int flags, caddr_t databuf, int datalen)
    822 {
    823 	register struct ncr5380_softc *ncr5380 = sicd.cd_devs[adapter];
    824 	register volatile sci_regmap_t *regs = ncr5380->sc_regs;
    825 	unsigned char cmd[6];
    826 	int i,j,sent,ret;
    827 
    828 	cmd[0] = opcode;		/* Operation code           		*/
    829 	cmd[1] = (lun << 5) | ((addr >> 16) & 0x1F);	/* Lun & MSB of addr	*/
    830 	cmd[2] = (addr >> 8) & 0xFF;	/* addr					*/
    831 	cmd[3] = addr & 0xFF;		/* LSB of addr				*/
    832 	cmd[4] = len;			/* Allocation length			*/
    833 	cmd[5] = flags;		/* Link/Flag				*/
    834 
    835 	i = si_dorequest(regs, id, lun, cmd, 6, databuf, datalen, &sent, &ret);
    836 
    837 	return i;
    838 }
    839