Home | History | Annotate | Line # | Download | only in dev
ncr5380.c revision 1.19
      1 /*	$NetBSD: ncr5380.c,v 1.19 1996/02/03 23:17:56 briggs Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995 Leo Weppelman.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *      This product includes software developed by Leo Weppelman.
     18  * 4. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 
     34 /*
     35  * Bit mask of targets you want debugging to be shown
     36  */
     37 u_char	dbg_target_mask = 0x7f;
     38 
     39 /*
     40  * Set bit for target when parity checking must be disabled.
     41  * My (LWP) Maxtor 7245S  seems to generate parity errors on about 50%
     42  * of all transfers while the data is correct!?
     43  */
     44 u_char	ncr5380_no_parchk = 0xff;
     45 
     46 #ifdef	AUTO_SENSE
     47 
     48 /*
     49  * Bit masks of targets that accept linked commands, and those
     50  * that we've already checked out
     51  */
     52 u_char	ncr_will_link = 0x00;
     53 u_char	ncr_test_link = 0x00;
     54 
     55 #endif	/* AUTO_SENSE */
     56 
     57 /*
     58  * This is the default sense-command we send.
     59  */
     60 static	u_char	sense_cmd[] = {
     61 		REQUEST_SENSE, 0, 0, 0, sizeof(struct scsi_sense_data), 0
     62 };
     63 
     64 /*
     65  * True if the main co-routine is running
     66  */
     67 static volatile int	main_running = 0;
     68 
     69 /*
     70  * Mask of targets selected
     71  */
     72 static u_char	busy;
     73 
     74 static void	ncr5380_minphys(struct buf *bp);
     75 static int	ncr5380_scsi_cmd(struct scsi_xfer *xs);
     76 static int	ncr5380_show_scsi_cmd(struct scsi_xfer *xs);
     77 
     78 struct scsi_adapter ncr5380_switch = {
     79 	ncr5380_scsi_cmd,		/* scsi_cmd()			*/
     80 	ncr5380_minphys,		/* scsi_minphys()		*/
     81 	0,				/* open_target_lu()		*/
     82 	0				/* close_target_lu()		*/
     83 };
     84 
     85 struct scsi_device ncr5380_dev = {
     86 	NULL,		/* use default error handler		*/
     87 	NULL,		/* do not have a start functio		*/
     88 	NULL,		/* have no async handler		*/
     89 	NULL		/* Use default done routine		*/
     90 };
     91 
     92 
     93 static SC_REQ	req_queue[NREQ];
     94 static SC_REQ	*free_head = NULL;	/* Free request structures	*/
     95 
     96 
     97 /*
     98  * Inline functions:
     99  */
    100 
    101 /*
    102  * Determine the size of a SCSI command.
    103  */
    104 extern __inline__ int command_size(opcode)
    105 u_char	opcode;
    106 {
    107 	switch ((opcode >> 4) & 0xf) {
    108 		case 0:
    109 		case 1:
    110 			return (6);
    111 		case 2:
    112 		case 3:
    113 			return (10);
    114 	}
    115 	return (12);
    116 }
    117 
    118 
    119 /*
    120  * Wait for request-line to become active. When it doesn't return 0.
    121  * Otherwise return != 0.
    122  * The timeouts in the 'wait_req_*' functions are arbitrary and rather
    123  * large. In 99% of the invocations nearly no timeout is needed but in
    124  * some cases (especially when using my tapedrive, a Tandberg 3600) the
    125  * device is busy internally and the first SCSI-phase will be delayed.
    126  */
    127 extern __inline__ int wait_req_true(void)
    128 {
    129 	int	timeout = 25000;
    130 
    131 	while (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_REQ) && --timeout)
    132 		delay(1);
    133 	return (GET_5380_REG(NCR5380_IDSTAT) & SC_S_REQ);
    134 }
    135 
    136 /*
    137  * Wait for request-line to become inactive. When it doesn't return 0.
    138  * Otherwise return != 0.
    139  */
    140 extern __inline__ int wait_req_false(void)
    141 {
    142 	int	timeout = 25000;
    143 
    144 	while ((GET_5380_REG(NCR5380_IDSTAT) & SC_S_REQ) && --timeout)
    145 		delay(1);
    146 	return (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_REQ));
    147 }
    148 
    149 extern __inline__ void ack_message()
    150 {
    151 	SET_5380_REG(NCR5380_ICOM, 0);
    152 }
    153 
    154 extern __inline__ void nack_message(SC_REQ *reqp, u_char msg)
    155 {
    156 	SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
    157 	reqp->msgout = msg;
    158 }
    159 
    160 extern __inline__ void finish_req(SC_REQ *reqp)
    161 {
    162 	int			sps;
    163 	struct scsi_xfer	*xs = reqp->xs;
    164 
    165 #ifdef REAL_DMA
    166 	/*
    167 	 * If we bounced, free the bounce buffer
    168 	 */
    169 	if (reqp->dr_flag & DRIVER_BOUNCING)
    170 		free_bounceb(reqp->bounceb);
    171 #endif /* REAL_DMA */
    172 #ifdef DBG_REQ
    173 	if (dbg_target_mask & (1 << reqp->targ_id))
    174 		show_request(reqp, "DONE");
    175 #endif
    176 #ifdef DBG_ERR_RET
    177 	if (reqp->xs->error != 0)
    178 		show_request(reqp, "ERR_RET");
    179 #endif
    180 	/*
    181 	 * Return request to free-q
    182 	 */
    183 	sps = splbio();
    184 	reqp->next = free_head;
    185 	free_head  = reqp;
    186 	splx(sps);
    187 
    188 	xs->flags |= ITSDONE;
    189 	if (!(reqp->dr_flag & DRIVER_LINKCHK))
    190 		scsi_done(xs);
    191 }
    192 
    193 /*
    194  * Auto config stuff....
    195  */
    196 int	ncr_cprint __P((void *auxp, char *));
    197 void	ncr_attach __P((struct device *, struct device *, void *));
    198 int	ncr_match __P((struct device *, struct cfdata *, void *));
    199 
    200 /*
    201  * Tricks to make driver-name configurable
    202  */
    203 #define CFNAME(n)	__CONCAT(n,cd)
    204 #define CFSTRING(n)	__STRING(n)
    205 
    206 struct cfdriver CFNAME(DRNAME) = {
    207 	NULL, CFSTRING(DRNAME), (cfmatch_t)ncr_match, ncr_attach,
    208 	DV_DULL, sizeof(struct ncr_softc), NULL, 0 };
    209 
    210 int
    211 ncr_match(pdp, cdp, auxp)
    212 struct device	*pdp;
    213 struct cfdata	*cdp;
    214 void		*auxp;
    215 {
    216 	return (machine_match(pdp, cdp, auxp, &CFNAME(DRNAME)));
    217 }
    218 
    219 void
    220 ncr_attach(pdp, dp, auxp)
    221 struct device	*pdp, *dp;
    222 void		*auxp;
    223 {
    224 	struct ncr_softc	*sc;
    225 	int			i;
    226 
    227 	sc = (struct ncr_softc *)dp;
    228 
    229 	sc->sc_link.adapter_softc   = sc;
    230 	sc->sc_link.adapter_target  = 7;
    231 	sc->sc_link.adapter         = &ncr5380_switch;
    232 	sc->sc_link.device          = &ncr5380_dev;
    233 	sc->sc_link.openings        = NREQ - 1;
    234 
    235 	/*
    236 	 * bitmasks
    237 	 */
    238 	sc->sc_noselatn = 0;
    239 	sc->sc_selected = 0;
    240 
    241 	/*
    242 	 * Initialize machine-type specific things...
    243 	 */
    244 	scsi_mach_init(sc);
    245 	printf("\n");
    246 
    247 	/*
    248 	 * Initialize request queue freelist.
    249 	 */
    250 	for (i = 0; i < NREQ; i++) {
    251 		req_queue[i].next = free_head;
    252 		free_head = &req_queue[i];
    253 	}
    254 
    255 	/*
    256 	 * Initialize the host adapter
    257 	 */
    258 	scsi_idisable();
    259 	ENABLE_NCR5380(sc);
    260 	SET_5380_REG(NCR5380_ICOM, 0);
    261 	SET_5380_REG(NCR5380_MODE, IMODE_BASE);
    262 	SET_5380_REG(NCR5380_TCOM, 0);
    263 	SET_5380_REG(NCR5380_IDSTAT, 0);
    264 	scsi_ienable();
    265 
    266 	/*
    267 	 * attach all scsi units on us
    268 	 */
    269 	config_found(dp, &sc->sc_link, ncr_cprint);
    270 }
    271 
    272 /*
    273  * print diag if name is NULL else just extra
    274  */
    275 int
    276 ncr_cprint(auxp, name)
    277 void	*auxp;
    278 char	*name;
    279 {
    280 	if (name == NULL)
    281 		return (UNCONF);
    282 	return (QUIET);
    283 }
    284 /*
    285  * End of auto config stuff....
    286  */
    287 
    288 /*
    289  * Carry out a request from the high level driver.
    290  */
    291 static int
    292 ncr5380_scsi_cmd(struct scsi_xfer *xs)
    293 {
    294 	int	sps;
    295 	SC_REQ	*reqp, *link, *tmp;
    296 	int	flags = xs->flags;
    297 
    298 	/*
    299 	 * We do not queue RESET commands
    300 	 */
    301 	if (flags & SCSI_RESET) {
    302 		scsi_reset(xs->sc_link->adapter_softc);
    303 		return (COMPLETE);
    304 	}
    305 
    306 	/*
    307 	 * Get a request block
    308 	 */
    309 	sps = splbio();
    310 	if ((reqp = free_head) == 0) {
    311 		splx(sps);
    312 		return (TRY_AGAIN_LATER);
    313 	}
    314 	free_head  = reqp->next;
    315 	reqp->next = NULL;
    316 	splx(sps);
    317 
    318 	/*
    319 	 * Initialize our private fields
    320 	 */
    321 	reqp->dr_flag   = (flags & SCSI_POLL) ? DRIVER_NOINT : 0;
    322 	reqp->phase     = NR_PHASE;
    323 	reqp->msgout    = MSG_NOOP;
    324 	reqp->status    = SCSGOOD;
    325 	reqp->message   = 0xff;
    326 	reqp->link      = NULL;
    327 	reqp->xs        = xs;
    328 	reqp->targ_id   = xs->sc_link->target;
    329 	reqp->targ_lun  = xs->sc_link->lun;
    330 	reqp->xdata_ptr = (u_char*)xs->data;
    331 	reqp->xdata_len = xs->datalen;
    332 	memcpy(&reqp->xcmd, xs->cmd, sizeof(struct scsi_generic));
    333 	reqp->xcmd.bytes[0] |= reqp->targ_lun << 5;
    334 
    335 	/*
    336 	 * Sanity check on flags...
    337 	 */
    338 	if (flags & ITSDONE) {
    339 		ncr_tprint(reqp, "scsi_cmd: command already done.....\n");
    340 		xs->flags &= ~ITSDONE;
    341 	}
    342 	if (!(flags & INUSE)) {
    343 		ncr_tprint(reqp, "scsi_cmd: command not in use.....\n");
    344 		xs->flags |= ~INUSE;
    345 	}
    346 
    347 #ifdef REAL_DMA
    348 	/*
    349 	 * Check if DMA can be used on this request
    350 	 */
    351 	if (scsi_dmaok(reqp))
    352 		reqp->dr_flag |= DRIVER_DMAOK;
    353 #endif /* REAL_DMA */
    354 
    355 	/*
    356 	 * Insert the command into the issue queue. Note that 'REQUEST SENSE'
    357 	 * commands are inserted at the head of the queue since any command
    358 	 * will clear the existing contingent allegience condition and the sense
    359 	 * data is only valid while the condition exists.
    360 	 * When possible, link the command to a previous command to the same
    361 	 * target. This is not very sensible when AUTO_SENSE is not defined!
    362 	 * Interrupts are disabled while we are fiddling with the issue-queue.
    363 	 */
    364 	sps = splbio();
    365 	link = NULL;
    366 	if ((issue_q == NULL) || (reqp->xcmd.opcode == REQUEST_SENSE)) {
    367 		reqp->next = issue_q;
    368 		issue_q    = reqp;
    369 	}
    370 	else {
    371 		tmp  = issue_q;
    372 		do {
    373 		    if (!link && (tmp->targ_id == reqp->targ_id) && !tmp->link)
    374 				link = tmp;
    375 		} while (tmp->next && (tmp = tmp->next));
    376 		tmp->next = reqp;
    377 #ifdef AUTO_SENSE
    378 		if (link && (ncr_will_link & (1<<reqp->targ_id))) {
    379 			link->link = reqp;
    380 			link->xcmd.bytes[link->xs->cmdlen-2] |= 1;
    381 		}
    382 #endif
    383 	}
    384 #ifdef AUTO_SENSE
    385 	/*
    386 	 * If we haven't already, check the target for link support.
    387 	 * Do this by prefixing the current command with a dummy
    388 	 * Request_Sense command, link the dummy to the current
    389 	 * command, and insert the dummy command at the head of the
    390 	 * issue queue.  Set the DRIVER_LINKCHK flag so that we'll
    391 	 * ignore the results of the dummy command, since we only
    392 	 * care about whether it was accepted or not.
    393 	 */
    394 	if (!link && !(ncr_test_link & (1<<reqp->targ_id)) &&
    395 	    (tmp = free_head) && !(reqp->dr_flag & DRIVER_NOINT)) {
    396 		free_head = tmp->next;
    397 		tmp->dr_flag = (reqp->dr_flag & ~DRIVER_DMAOK) | DRIVER_LINKCHK;
    398 		tmp->phase = NR_PHASE;
    399 		tmp->msgout = MSG_NOOP;
    400 		tmp->status = SCSGOOD;
    401 		tmp->xs = reqp->xs;
    402 		tmp->targ_id = reqp->targ_id;
    403 		tmp->targ_lun = reqp->targ_lun;
    404 		bcopy(sense_cmd, &tmp->xcmd, sizeof(sense_cmd));
    405 		tmp->xdata_ptr = (u_char *)&tmp->xs->sense;
    406 		tmp->xdata_len = sizeof(tmp->xs->sense);
    407 		ncr_test_link |= 1<<tmp->targ_id;
    408 		tmp->link = reqp;
    409 		tmp->xcmd.bytes[sizeof(sense_cmd)-2] |= 1;
    410 		tmp->next = issue_q;
    411 		issue_q = tmp;
    412 #ifdef DBG_REQ
    413 		if (dbg_target_mask & (1 << tmp->targ_id))
    414 			show_request(tmp, "LINKCHK");
    415 #endif
    416 	}
    417 #endif
    418 	splx(sps);
    419 
    420 #ifdef DBG_REQ
    421 	if (dbg_target_mask & (1 << reqp->targ_id))
    422 		show_request(reqp, (reqp->xcmd.opcode == REQUEST_SENSE) ?
    423 								"HEAD":"TAIL");
    424 #endif
    425 
    426 	run_main(xs->sc_link->adapter_softc);
    427 
    428 	if (xs->flags & (SCSI_POLL|ITSDONE))
    429 		return (COMPLETE); /* We're booting or run_main has completed */
    430 	return (SUCCESSFULLY_QUEUED);
    431 }
    432 
    433 static void
    434 ncr5380_minphys(struct buf *bp)
    435 {
    436     if (bp->b_bcount > MIN_PHYS)
    437 	bp->b_bcount = MIN_PHYS;
    438     minphys(bp);
    439 }
    440 #undef MIN_PHYS
    441 
    442 static int
    443 ncr5380_show_scsi_cmd(struct scsi_xfer *xs)
    444 {
    445 	u_char	*b = (u_char *) xs->cmd;
    446 	int	i  = 0;
    447 
    448 	if (!(xs->flags & SCSI_RESET)) {
    449 		printf("(%d:%d:%d,0x%x)-", xs->sc_link->scsibus,
    450 		    xs->sc_link->target, xs->sc_link->lun, xs->sc_link->flags);
    451 		while (i < xs->cmdlen) {
    452 			if (i)
    453 				printf(",");
    454 			printf("%x",b[i++]);
    455 		}
    456 		printf("-\n");
    457 	}
    458 	else {
    459 		printf("(%d:%d:%d)-RESET-\n",
    460 		    xs->sc_link->scsibus,xs->sc_link->target, xs->sc_link->lun);
    461 	}
    462 }
    463 
    464 /*
    465  * The body of the driver.
    466  */
    467 static void
    468 scsi_main(sc)
    469 struct ncr_softc *sc;
    470 {
    471 	SC_REQ	*req, *prev;
    472 	int	itype;
    473 	int	sps;
    474 
    475 	/*
    476 	 * While running in the driver SCSI-interrupts are disabled.
    477 	 */
    478 	scsi_idisable();
    479 	ENABLE_NCR5380(sc);
    480 
    481 	PID("scsi_main1");
    482 	for (;;) {
    483 	    sps = splbio();
    484 	    if (!connected) {
    485 		/*
    486 		 * Check if it is fair keep any exclusive access to DMA
    487 		 * claimed. If not, stop queueing new jobs so the discon_q
    488 		 * will be eventually drained and DMA can be given up.
    489 		 */
    490 		if (!fair_to_keep_dma())
    491 			goto main_exit;
    492 
    493 		/*
    494 		 * Search through the issue-queue for a command
    495 		 * destined for a target that isn't busy.
    496 		 */
    497 		prev = NULL;
    498 		for (req=issue_q; req != NULL; prev = req, req = req->next) {
    499 			if (!(busy & (1 << req->targ_id))) {
    500 				/*
    501 				 * Found one, remove it from the issue queue
    502 				 */
    503 				if (prev == NULL)
    504 					issue_q = req->next;
    505 				else prev->next = req->next;
    506 				req->next = NULL;
    507 				break;
    508 			}
    509 		}
    510 
    511 		/*
    512 		 * When a request has just ended, we get here before an other
    513 		 * device detects that the bus is free and that it can
    514 		 * reconnect. The problem is that when this happens, we always
    515 		 * baffle the device because our (initiator) id is higher. This
    516 		 * can cause a sort of starvation on slow devices. So we check
    517 		 * for a pending reselection here.
    518 		 * Note that 'connected' will be non-null if the reselection
    519 		 * succeeds.
    520 		 */
    521 		if ((GET_5380_REG(NCR5380_IDSTAT) & (SC_S_SEL|SC_S_IO))
    522 						== (SC_S_SEL|SC_S_IO)){
    523 			if (req != NULL) {
    524 				req->next = issue_q;
    525 				issue_q = req;
    526 			}
    527 			splx(sps);
    528 
    529 			reselect(sc);
    530 			scsi_clr_ipend();
    531 			goto connected;
    532 		}
    533 
    534 		/*
    535 		 * The host is not connected and there is no request
    536 		 * pending, exit.
    537 		 */
    538 		if (req == NULL) {
    539 			PID("scsi_main2");
    540 			goto main_exit;
    541 		}
    542 
    543 		/*
    544 		 * Re-enable interrupts before handling the request.
    545 		 */
    546 		splx(sps);
    547 
    548 #ifdef DBG_REQ
    549 		if (dbg_target_mask & (1 << req->targ_id))
    550 			show_request(req, "TARGET");
    551 #endif
    552 		/*
    553 		 * We found a request. Try to connect to the target. If the
    554 		 * initiator fails arbitration, the command is put back in the
    555 		 * issue queue.
    556 		 */
    557 		if (scsi_select(req, 0)) {
    558 			sps = splbio();
    559 			req->next = issue_q;
    560 			issue_q = req;
    561 			splx(sps);
    562 #ifdef DBG_REQ
    563 			if (dbg_target_mask & (1 << req->targ_id))
    564 				ncr_tprint(req, "Select failed\n");
    565 #endif
    566 		}
    567 	    }
    568 	    else splx(sps);
    569 connected:
    570 	    if (connected) {
    571 		/*
    572 		 * If the host is currently connected but a 'real-dma' transfer
    573 		 * is in progress, the 'end-of-dma' interrupt restarts main.
    574 		 * So quit.
    575 		 */
    576 		sps = splbio();
    577 		if (connected && (connected->dr_flag & DRIVER_IN_DMA)) {
    578 			PID("scsi_main3");
    579 			goto main_exit;
    580 		}
    581 		splx(sps);
    582 
    583 		/*
    584 		 * Let the target guide us through the bus-phases
    585 		 */
    586 		while (information_transfer() == -1)
    587 			;
    588 	    }
    589 	}
    590 	/* NEVER TO REACH HERE */
    591 	panic("ncr5380-SCSI: not designed to come here");
    592 
    593 main_exit:
    594 	/*
    595 	 * We enter here with interrupts disabled. We are about to exit main
    596 	 * so interrupts should be re-enabled. Because interrupts are edge
    597 	 * triggered, we could already have missed the interrupt. Therefore
    598 	 * we check the IRQ-line here and re-enter when we really missed a
    599 	 * valid interrupt.
    600 	 */
    601 	PID("scsi_main4");
    602 	scsi_ienable();
    603 	SET_5380_REG(NCR5380_IDSTAT, SC_HOST_ID);
    604 	if (GET_5380_REG(NCR5380_DMSTAT) & SC_IRQ_SET) {
    605 		if ((itype = check_intr(sc)) != INTR_SPURIOUS) {
    606 			scsi_idisable();
    607 			splx(sps);
    608 
    609 			if (itype == INTR_RESEL)
    610 				reselect(sc);
    611 #ifdef REAL_DMA
    612 			else dma_ready();
    613 #else
    614 			else {
    615 				if (!pdma_ready())
    616 					panic("Got DMA interrupt without DMA");
    617 			}
    618 #endif
    619 			scsi_clr_ipend();
    620 			goto connected;
    621 		}
    622 	}
    623 	reconsider_dma();
    624 
    625 	main_running = 0;
    626 	splx(sps);
    627 	PID("scsi_main5");
    628 }
    629 
    630 #ifdef REAL_DMA
    631 /*
    632  * The SCSI-DMA interrupt.
    633  * This interrupt can only be triggered when running in non-polled DMA
    634  * mode. When DMA is not active, it will be silently ignored, it is usually
    635  * to late because the EOP interrupt of the controller happens just a tiny
    636  * bit earlier. It might become usefull when scatter/gather is implemented,
    637  * because in that case only part of the DATAIN/DATAOUT transfer is taken
    638  * out of a single buffer.
    639  */
    640 static void
    641 ncr_dma_intr(sc)
    642 struct ncr_softc *sc;
    643 {
    644 	SC_REQ	*reqp;
    645 	int	dma_done;
    646 
    647 	PID("ncr_dma_intr");
    648 	if ((reqp = connected) && (reqp->dr_flag & DRIVER_IN_DMA)) {
    649 		scsi_idisable();
    650 		if (!(dma_done = dma_ready())) {
    651 			transfer_dma(reqp, reqp->phase, 0);
    652 			return;
    653 		}
    654 		run_main(sc);
    655 	}
    656 }
    657 #endif /* REAL_DMA */
    658 
    659 /*
    660  * The SCSI-controller interrupt. This interrupt occurs on reselections and
    661  * at the end of non-polled DMA-interrupts. It is assumed to be called from
    662  * the machine-dependent hardware interrupt.
    663  */
    664 static void
    665 ncr_ctrl_intr(sc)
    666 struct ncr_softc *sc;
    667 {
    668 	int	itype;
    669 	int	dma_done;
    670 
    671 	while (GET_5380_REG(NCR5380_DMSTAT) & SC_IRQ_SET) {
    672 		scsi_idisable();
    673 		if ((itype = check_intr(sc)) != INTR_SPURIOUS) {
    674 			if (itype == INTR_RESEL)
    675 				reselect(sc);
    676 			else {
    677 #ifdef REAL_DMA
    678 			    if (!(dma_done = dma_ready())) {
    679 				transfer_dma(connected, connected->phase, 0);
    680 				return;
    681 			    }
    682 #else
    683 			    if (!pdma_ready())
    684 				panic("Got DMA interrupt without DMA\n");
    685 			    return;
    686 #endif
    687 			}
    688 			scsi_clr_ipend();
    689 		}
    690 		run_main(sc);
    691 		return;
    692 	}
    693 	PID("ncr_ctrl_intr1");
    694 }
    695 
    696 /*
    697  * Initiate a connection path between the host and the target. The function
    698  * first goes into arbitration for the SCSI-bus. When this succeeds, the target
    699  * is selected and an 'IDENTIFY' message is send.
    700  * Returns -1 when the arbitration failed. Otherwise 0 is returned. When
    701  * the target does not respond (to either selection or 'MESSAGE OUT') the
    702  * 'done' function is executed.
    703  * The result code given by the driver can be influenced by setting 'code'
    704  * to a non-zero value. This is the case when 'select' is called by abort.
    705  */
    706 static int
    707 scsi_select(reqp, code)
    708 SC_REQ	*reqp;
    709 {
    710 	u_long			timeout;
    711 	u_char			tmp[1];
    712 	u_char			phase;
    713 	u_long			cnt;
    714 	int			sps;
    715 	u_int8_t		atn_flag;
    716 	u_int8_t		targ_bit;
    717 	struct ncr_softc	*sc;
    718 
    719 	sc = reqp->xs->sc_link->adapter_softc;
    720 	DBG_SELPRINT ("Starting arbitration\n", 0);
    721 	PID("scsi_select1");
    722 
    723 	sps = splbio();
    724 
    725 	/*
    726 	 * Prevent a race condition here. If a reslection interrupt occurred
    727 	 * between the decision to pick a new request and the call to select,
    728 	 * we abort the selection.
    729 	 * Interrupts are lowered when the 5380 is setup to arbitrate for the
    730 	 * bus.
    731 	 */
    732 	if (connected) {
    733 		splx(sps);
    734 		PID("scsi_select2");
    735 		return (-1);
    736 	}
    737 
    738 	/*
    739 	 * Set phase bits to 0, otherwise the 5380 won't drive the bus during
    740 	 * selection.
    741 	 */
    742 	SET_5380_REG(NCR5380_TCOM, 0);
    743 	SET_5380_REG(NCR5380_ICOM, 0);
    744 
    745 	/*
    746 	 * Arbitrate for the bus.
    747 	 */
    748 	SET_5380_REG(NCR5380_DATA, SC_HOST_ID);
    749 	SET_5380_REG(NCR5380_MODE, SC_ARBIT);
    750 
    751 	splx(sps);
    752 
    753 	cnt = 10;
    754 	while (!(GET_5380_REG(NCR5380_ICOM) & SC_AIP) && --cnt)
    755 		delay(1);
    756 
    757 	if (!(GET_5380_REG(NCR5380_ICOM) & SC_AIP)) {
    758 		SET_5380_REG(NCR5380_MODE, IMODE_BASE);
    759 		SET_5380_REG(NCR5380_ICOM, 0);
    760 		DBG_SELPRINT ("Arbitration lost, bus not free\n",0);
    761 		PID("scsi_select3");
    762 		return (-1);
    763 	}
    764 
    765 	/* The arbitration delay is 2.2 usecs */
    766 	delay(3);
    767 
    768 	/*
    769 	 * Check the result of the arbitration. If we failed, return -1.
    770 	 */
    771 	if (GET_5380_REG(NCR5380_ICOM) & SC_LA) {
    772 		SET_5380_REG(NCR5380_MODE, IMODE_BASE);
    773 		SET_5380_REG(NCR5380_ICOM, 0);
    774 		PID("scsi_select4");
    775 		return (-1);
    776 	}
    777 
    778 	/*
    779 	 * The spec requires that we should read the data register to
    780 	 * check for higher id's and check the SC_LA again.
    781 	 */
    782 	tmp[0] = GET_5380_REG(NCR5380_DATA);
    783 	if (tmp[0] & ~((SC_HOST_ID << 1) - 1)) {
    784 		SET_5380_REG(NCR5380_MODE, IMODE_BASE);
    785 		SET_5380_REG(NCR5380_ICOM, 0);
    786 		DBG_SELPRINT ("Arbitration lost, higher id present\n",0);
    787 		PID("scsi_select5");
    788 		return (-1);
    789 	}
    790 	if (GET_5380_REG(NCR5380_ICOM) & SC_LA) {
    791 		SET_5380_REG(NCR5380_MODE, IMODE_BASE);
    792 		SET_5380_REG(NCR5380_ICOM, 0);
    793 		DBG_SELPRINT ("Arbitration lost,deassert SC_ARBIT\n",0);
    794 		PID("scsi_select6");
    795 		return (-1);
    796 	}
    797 	SET_5380_REG(NCR5380_ICOM, SC_A_SEL | SC_A_BSY);
    798 	if (GET_5380_REG(NCR5380_ICOM) & SC_LA) {
    799 		SET_5380_REG(NCR5380_MODE, IMODE_BASE);
    800 		SET_5380_REG(NCR5380_ICOM, 0);
    801 		DBG_SELPRINT ("Arbitration lost, deassert SC_A_SEL\n", 0);
    802 		PID("scsi_select7");
    803 		return (-1);
    804 	}
    805 	/* Bus settle delay + Bus clear delay = 1.2 usecs */
    806 	delay(2);
    807 	DBG_SELPRINT ("Arbitration complete\n", 0);
    808 
    809 	/*
    810 	 * Now that we won the arbitration, start the selection.
    811 	 */
    812 	targ_bit = 1 << reqp->targ_id;
    813 	SET_5380_REG(NCR5380_DATA, SC_HOST_ID | targ_bit);
    814 
    815 	if (sc->sc_noselatn & targ_bit)
    816 		atn_flag = 0;
    817 	else
    818 		atn_flag = SC_A_ATN;
    819 
    820 	/*
    821 	 * Raise ATN while SEL is true before BSY goes false from arbitration,
    822 	 * since this is the only way to guarantee that we'll get a MESSAGE OUT
    823 	 * phase immediately after the selection.
    824 	 */
    825 	SET_5380_REG(NCR5380_ICOM, SC_A_BSY | SC_A_SEL | atn_flag | SC_ADTB);
    826 	SET_5380_REG(NCR5380_MODE, IMODE_BASE);
    827 
    828 	/*
    829 	 * Turn off reselection interrupts
    830 	 */
    831 	SET_5380_REG(NCR5380_IDSTAT, 0);
    832 
    833 	/*
    834 	 * Reset BSY. The delay following it, surpresses a glitch in the
    835 	 * 5380 which causes us to see our own BSY signal instead of that of
    836 	 * the target.
    837 	 */
    838 	SET_5380_REG(NCR5380_ICOM, SC_A_SEL | atn_flag | SC_ADTB);
    839 	delay(1);
    840 
    841 	/*
    842 	 * Wait for the target to react, the specs call for a timeout of
    843 	 * 250 ms.
    844 	 */
    845 	cnt = 25000;
    846 	while (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY) && --cnt)
    847 		delay(10);
    848 
    849 	if (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY)) {
    850 		/*
    851 		 * There is no reaction from the target, start the selection
    852 		 * timeout procedure. We release the databus but keep SEL
    853 		 * asserted. After that we wait a 'selection abort time' (200
    854 		 * usecs) and 2 deskew delays (90 ns) and check BSY again.
    855 		 * When BSY is asserted, we assume the selection succeeded,
    856 		 * otherwise we release the bus.
    857 		 */
    858 		SET_5380_REG(NCR5380_ICOM, SC_A_SEL | atn_flag);
    859 		delay(201);
    860 		if (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY)) {
    861 			SET_5380_REG(NCR5380_ICOM, 0);
    862 			reqp->xs->error      = code ? code : XS_SELTIMEOUT;
    863 			DBG_SELPRINT ("Target %d not responding to sel\n",
    864 								reqp->targ_id);
    865 			if (reqp->dr_flag & DRIVER_LINKCHK)
    866 				ncr_test_link &= ~(1<<reqp->targ_id);
    867 			finish_req(reqp);
    868 			PID("scsi_select8");
    869 			return (0);
    870 		}
    871 	}
    872 	SET_5380_REG(NCR5380_ICOM, atn_flag);
    873 
    874 	DBG_SELPRINT ("Target %d responding to select.\n", reqp->targ_id);
    875 
    876 	/*
    877 	 * The SCSI-interrupts are disabled while a request is being handled.
    878 	 */
    879 	scsi_idisable();
    880 
    881 	/*
    882 	 * If we did not request ATN, then don't try to send IDENTIFY.
    883 	 */
    884 	if (atn_flag == 0) {
    885 		reqp->phase = PH_CMD;
    886 		goto identify_failed;
    887 	}
    888 
    889 	/*
    890 	 * Here we prepare to send an 'IDENTIFY' message.
    891 	 * Allow disconnect only when interrups are allowed.
    892 	 */
    893 	tmp[0] = MSG_IDENTIFY(reqp->targ_lun,
    894 			(reqp->dr_flag & DRIVER_NOINT) ? 0 : 1);
    895 	cnt    = 1;
    896 	phase  = PH_MSGOUT;
    897 
    898 	/*
    899 	 * Since we followed the SCSI-spec and raised ATN while SEL was true
    900 	 * but before BSY was false during the selection, a 'MESSAGE OUT'
    901 	 * phase should follow.  Unfortunately, this does not happen on
    902 	 * all targets (Asante ethernet devices, for example), so we must
    903 	 * check the actual mode if the message transfer fails--if the
    904 	 * new phase is PH_CMD and has never been successfully selected
    905 	 * w/ATN in the past, then we assume that it is an old device
    906 	 * that doesn't support select w/ATN.
    907 	 */
    908 	if (transfer_pio(&phase, tmp, &cnt, 0) || cnt) {
    909 
    910 		if ((phase == PH_CMD) && !(sc->sc_selected & targ_bit)) {
    911 			DBG_SELPRINT ("Target %d: not responding to ATN.\n",
    912 							reqp->targ_id);
    913 			sc->sc_noselatn |= targ_bit;
    914 			reqp->phase = PH_CMD;
    915 			goto identify_failed;
    916 		}
    917 
    918 		DBG_SELPRINT ("Target %d: failed to send identify\n",
    919 							reqp->targ_id);
    920 		/*
    921 		 * Try to disconnect from the target.  We cannot leave
    922 		 * it just hanging here.
    923 		 */
    924 		if (!reach_msg_out(sc, sizeof(struct scsi_generic))) {
    925 			u_long	len   = 1;
    926 			u_char	phase = PH_MSGOUT;
    927 			u_char	msg   = MSG_ABORT;
    928 
    929 			transfer_pio(&phase, &msg, &len, 0);
    930 		}
    931 		else scsi_reset(sc);
    932 
    933 		SET_5380_REG(NCR5380_ICOM, 0);
    934 		reqp->xs->error = code ? code : XS_DRIVER_STUFFUP;
    935 		finish_req(reqp);
    936 		PID("scsi_select9");
    937 		return (0);
    938 	}
    939 	reqp->phase = PH_MSGOUT;
    940 
    941 identify_failed:
    942 	sc->sc_selected |= targ_bit;
    943 
    944 #ifdef notyet /* LWP: Do we need timeouts in the driver? */
    945 	/*
    946 	 * Command is connected, start timer ticking.
    947 	 */
    948 	ccb_p->xtimeout = ccb_p->timeout + Lbolt;
    949 #endif
    950 
    951 	connected  = reqp;
    952 	busy      |= targ_bit;
    953 	PID("scsi_select10");
    954 	return (0);
    955 }
    956 
    957 /*
    958  * Return codes:
    959  *	-1: quit main, trigger on interrupt
    960  *	 0: keep on running main.
    961  */
    962 static int
    963 information_transfer()
    964 {
    965 	SC_REQ	*reqp = connected;
    966 	u_char	tmp, phase;
    967 	u_long	len;
    968 
    969 	PID("info_transf1");
    970 	/*
    971 	 * Clear pending interrupts from 5380-chip.
    972 	 */
    973 	scsi_clr_ipend();
    974 
    975 	/*
    976 	 * We only have a valid SCSI-phase when REQ is asserted. Something
    977 	 * is deadly wrong when BSY has dropped.
    978 	 */
    979 	tmp = GET_5380_REG(NCR5380_IDSTAT);
    980 
    981 	if (!(tmp & SC_S_BSY)) {
    982 		busy           &= ~(1 << reqp->targ_id);
    983 		connected       = NULL;
    984 		reqp->xs->error = XS_BUSY;
    985 		finish_req(reqp);
    986 		PID("info_transf2");
    987 		return (0);
    988 	}
    989 
    990 	if (tmp & SC_S_REQ) {
    991 		phase = (tmp >> 2) & 7;
    992 		if (phase != reqp->phase) {
    993 			reqp->phase = phase;
    994 			DBG_INFPRINT(show_phase, reqp, phase);
    995 		}
    996 	}
    997 	else return (-1);
    998 
    999 	switch (phase) {
   1000 	    case PH_DATAOUT:
   1001 #ifdef DBG_NOWRITE
   1002 		ncr_tprint(reqp, "NOWRITE set -- write attempt aborted.");
   1003 		reqp->msgout = MSG_ABORT;
   1004 		SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
   1005 		return (-1);
   1006 #endif /* DBG_NOWRITE */
   1007 		/*
   1008 		 * If this is the first write using DMA, fill
   1009 		 * the bounce buffer.
   1010 		 */
   1011 		if (reqp->xdata_ptr == reqp->xs->data) { /* XXX */
   1012 		    if (reqp->dr_flag & DRIVER_BOUNCING)
   1013 			bcopy(reqp->xdata_ptr, reqp->bounceb, reqp->xdata_len);
   1014 		}
   1015 
   1016 	   case PH_DATAIN:
   1017 #ifdef REAL_DMA
   1018 		if (reqp->dr_flag & DRIVER_DMAOK) {
   1019 			int poll = REAL_DMA_POLL|(reqp->dr_flag & DRIVER_NOINT);
   1020 			transfer_dma(reqp, phase, poll);
   1021 			if (!poll)
   1022 				return (0);
   1023 		}
   1024 		else
   1025 #endif
   1026 		{
   1027 			PID("info_transf3");
   1028 			len = reqp->xdata_len;
   1029 #ifdef USE_PDMA
   1030 			if (transfer_pdma(&phase, reqp->xdata_ptr, &len) == 0)
   1031 				return (0);
   1032 #else
   1033 			transfer_pio(&phase, reqp->xdata_ptr, &len, 0);
   1034 #endif
   1035 			reqp->xdata_ptr += reqp->xdata_len - len;
   1036 			reqp->xdata_len  = len;
   1037 		}
   1038 		return (-1);
   1039 	   case PH_MSGIN:
   1040 		/*
   1041 		 * We only expect single byte messages here.
   1042 		 */
   1043 		len = 1;
   1044 		transfer_pio(&phase, &tmp, &len, 1);
   1045 		reqp->message = tmp;
   1046 		return (handle_message(reqp, tmp));
   1047 	   case PH_MSGOUT:
   1048 		len = 1;
   1049 		transfer_pio(&phase, &reqp->msgout, &len, 0);
   1050 		if (reqp->msgout == MSG_ABORT) {
   1051 			busy     &= ~(1 << reqp->targ_id);
   1052 			connected = NULL;
   1053 			reqp->xs->error = XS_DRIVER_STUFFUP;
   1054 			finish_req(reqp);
   1055 			PID("info_transf4");
   1056 			return (0);
   1057 		}
   1058 		reqp->msgout = MSG_NOOP;
   1059 		return (-1);
   1060 	   case PH_CMD :
   1061 		len = command_size(reqp->xcmd.opcode);
   1062 		transfer_pio(&phase, (u_char *)&reqp->xcmd, &len, 0);
   1063 		PID("info_transf5");
   1064 		return (-1);
   1065 	   case PH_STATUS:
   1066 		len = 1;
   1067 		transfer_pio(&phase, &tmp, &len, 0);
   1068 		reqp->status = tmp;
   1069 		PID("info_transf6");
   1070 		return (-1);
   1071 	   default :
   1072 		ncr_tprint(reqp, "Unknown phase\n");
   1073 	}
   1074 	PID("info_transf7");
   1075 	return (-1);
   1076 }
   1077 
   1078 /*
   1079  * Handle the message 'msg' send to us by the target.
   1080  * Return values:
   1081  *	 0 : The current command has completed.
   1082  *	-1 : Get on to the next phase.
   1083  */
   1084 static int
   1085 handle_message(reqp, msg)
   1086 SC_REQ	*reqp;
   1087 u_int	msg;
   1088 {
   1089 	int	sps;
   1090 	SC_REQ	*prev, *req;
   1091 
   1092 	PID("hmessage1");
   1093 	switch (msg) {
   1094 		/*
   1095 		 * Linking lets us reduce the time required to get
   1096 		 * the next command to the device, skipping the arbitration
   1097 		 * and selection time. In the current implementation,
   1098 		 * we merely have to start the next command pointed
   1099 		 * to by 'next_link'.
   1100 		 */
   1101 		case MSG_LINK_CMD_COMPLETE:
   1102 		case MSG_LINK_CMD_COMPLETEF:
   1103 			if (reqp->link == NULL) {
   1104 				ncr_tprint(reqp, "No link for linked command");
   1105 				nack_message(reqp, MSG_ABORT);
   1106 				PID("hmessage2");
   1107 				return (-1);
   1108 			}
   1109 			ack_message();
   1110 			if (!(reqp->dr_flag & DRIVER_AUTOSEN)) {
   1111 				reqp->xs->resid = reqp->xdata_len;
   1112 				reqp->xs->error = 0;
   1113 			}
   1114 
   1115 #ifdef AUTO_SENSE
   1116 			if (check_autosense(reqp, 1) == -1)
   1117 				return (-1);
   1118 #endif /* AUTO_SENSE */
   1119 
   1120 #ifdef DBG_REQ
   1121 			if (dbg_target_mask & (1 << reqp->targ_id))
   1122 				show_request(reqp->link, "LINK");
   1123 #endif
   1124 			connected = reqp->link;
   1125 
   1126 			/*
   1127 			 * Unlink the 'linked' request from the issue_q
   1128 			 */
   1129 			sps  = splbio();
   1130 			prev = NULL;
   1131 			req  = issue_q;
   1132 			for (; req != NULL; prev = req, req = req->next) {
   1133 				if (req == connected)
   1134 					break;
   1135 			}
   1136 			if (req == NULL)
   1137 				panic("Inconsistent issue_q");
   1138 			if (prev == NULL)
   1139 				issue_q = req->next;
   1140 			else prev->next = req->next;
   1141 			req->next = NULL;
   1142 			splx(sps);
   1143 
   1144 			finish_req(reqp);
   1145 			PID("hmessage3");
   1146 			return (-1);
   1147 		case MSG_ABORT:
   1148 		case MSG_CMDCOMPLETE:
   1149 			ack_message();
   1150 			connected = NULL;
   1151 			busy     &= ~(1 << reqp->targ_id);
   1152 			if (!(reqp->dr_flag & DRIVER_AUTOSEN)) {
   1153 				reqp->xs->resid = reqp->xdata_len;
   1154 				reqp->xs->error = 0;
   1155 			}
   1156 
   1157 #ifdef AUTO_SENSE
   1158 			if (check_autosense(reqp, 0) == -1) {
   1159 				PID("hmessage4");
   1160 				return (0);
   1161 			}
   1162 #endif /* AUTO_SENSE */
   1163 
   1164 			finish_req(reqp);
   1165 			PID("hmessage5");
   1166 			return (0);
   1167 		case MSG_MESSAGE_REJECT:
   1168 			ack_message();
   1169 			PID("hmessage6");
   1170 			return (-1);
   1171 		case MSG_DISCONNECT:
   1172 			ack_message();
   1173 #ifdef DBG_REQ
   1174 			if (dbg_target_mask & (1 << reqp->targ_id))
   1175 				show_request(reqp, "DISCON");
   1176 #endif
   1177 			sps = splbio();
   1178 			connected  = NULL;
   1179 			reqp->next = discon_q;
   1180 			discon_q   = reqp;
   1181 			splx(sps);
   1182 			PID("hmessage7");
   1183 			return (0);
   1184 		case MSG_SAVEDATAPOINTER:
   1185 		case MSG_RESTOREPOINTERS:
   1186 			/*
   1187 			 * We save pointers implicitely at disconnect.
   1188 			 * So we can ignore these messages.
   1189 			 */
   1190 			ack_message();
   1191 			PID("hmessage8");
   1192 			return (-1);
   1193 		case MSG_EXTENDED:
   1194 			nack_message(reqp, MSG_MESSAGE_REJECT);
   1195 			PID("hmessage9");
   1196 			return (-1);
   1197 		default:
   1198 			ncr_tprint(reqp, "Unknown message %x\n", msg);
   1199 			return (-1);
   1200 	}
   1201 	PID("hmessage10");
   1202 	return (-1);
   1203 }
   1204 
   1205 /*
   1206  * Handle reselection. If a valid reconnection occurs, connected
   1207  * points at the reconnected command. The command is removed from the
   1208  * disconnected queue.
   1209  */
   1210 static void
   1211 reselect(sc)
   1212 struct ncr_softc *sc;
   1213 {
   1214 	u_char	phase;
   1215 	u_long	len;
   1216 	u_char	msg;
   1217 	u_char	target_mask;
   1218 	int	abort = 0;
   1219 	SC_REQ	*tmp, *prev;
   1220 
   1221 	PID("reselect1");
   1222 	target_mask = GET_5380_REG(NCR5380_DATA) & ~SC_HOST_ID;
   1223 
   1224 	/*
   1225 	 * At this point, we have detected that our SCSI-id is on the bus,
   1226 	 * SEL is true and BSY was false for at least one bus settle
   1227 	 * delay (400 ns.).
   1228 	 * We must assert BSY ourselves, until the target drops the SEL signal.
   1229 	 * The SCSI-spec specifies no maximum time for this, so we have to
   1230 	 * choose something long enough to suit all targets.
   1231 	 */
   1232 	SET_5380_REG(NCR5380_ICOM, SC_A_BSY);
   1233 	len = 250000;
   1234 	while ((GET_5380_REG(NCR5380_IDSTAT) & SC_S_SEL) && (len > 0)) {
   1235 		delay(1);
   1236 		len--;
   1237 	}
   1238 	if (GET_5380_REG(NCR5380_IDSTAT) & SC_S_SEL) {
   1239 		/* Damn SEL isn't dropping */
   1240 		scsi_reset(sc);
   1241 		return;
   1242 	}
   1243 
   1244 	SET_5380_REG(NCR5380_ICOM, 0);
   1245 
   1246 	/*
   1247 	 * Check if the reselection is still valid. Check twice because
   1248 	 * of possible line glitches - cheaper than delay(1) and we need
   1249 	 * only a few nanoseconds.
   1250 	 */
   1251 	if (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY)) {
   1252 	    if (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY)) {
   1253 		ncr_aprint(sc, "Stepped into the reselection timeout\n");
   1254 		return;
   1255 	    }
   1256 	}
   1257 
   1258 	/*
   1259 	 * Get the expected identify message.
   1260 	 */
   1261 	phase = PH_MSGIN;
   1262 	len   = 1;
   1263 	transfer_pio(&phase, &msg, &len, 0);
   1264 	if (len || !MSG_ISIDENTIFY(msg)) {
   1265 		ncr_aprint(sc, "Expecting IDENTIFY, got 0x%x\n", msg);
   1266 		abort = 1;
   1267 	}
   1268 	else {
   1269 	    /*
   1270 	     * Find the command reconnecting
   1271 	     */
   1272 	    for (tmp = discon_q, prev = NULL; tmp; prev = tmp, tmp = tmp->next){
   1273 		if (target_mask == (1 << tmp->targ_id)) {
   1274 			if (prev)
   1275 				prev->next = tmp->next;
   1276 			else discon_q = tmp->next;
   1277 			tmp->next = NULL;
   1278 			break;
   1279 		}
   1280 	    }
   1281 	    if (tmp == NULL) {
   1282 		ncr_aprint(sc, "No disconnected job for targetmask %x\n",
   1283 								target_mask);
   1284 		abort = 1;
   1285 	    }
   1286 	}
   1287 	if (abort) {
   1288 		msg   = MSG_ABORT;
   1289 		len   = 1;
   1290 		phase = PH_MSGOUT;
   1291 
   1292 		SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
   1293 		if (transfer_pio(&phase, &msg, &len, 0) || len)
   1294 			scsi_reset(sc);
   1295 	}
   1296 	else {
   1297 		connected = tmp;
   1298 #ifdef DBG_REQ
   1299 		if (dbg_target_mask & (1 << tmp->targ_id))
   1300 			show_request(tmp, "RECON");
   1301 #endif
   1302 	}
   1303 	PID("reselect2");
   1304 }
   1305 
   1306 /*
   1307  * Transfer data in a given phase using programmed I/O.
   1308  * Returns -1 when a different phase is entered without transferring the
   1309  * maximum number of bytes, 0 if all bytes transferred or exit is in the same
   1310  * phase.
   1311  */
   1312 static int
   1313 transfer_pio(phase, data, len, dont_drop_ack)
   1314 u_char	*phase;
   1315 u_char	*data;
   1316 u_long	*len;
   1317 int	dont_drop_ack;
   1318 {
   1319 	u_int	cnt = *len;
   1320 	u_char	ph  = *phase;
   1321 	u_char	tmp, new_icom;
   1322 
   1323 	DBG_PIOPRINT ("SCSI: transfer_pio start: phase: %d, len: %d\n", ph,cnt);
   1324 	PID("tpio1");
   1325 	SET_5380_REG(NCR5380_TCOM, ph);
   1326 	do {
   1327 	    if (!wait_req_true()) {
   1328 		DBG_PIOPRINT ("SCSI: transfer_pio: missing REQ\n", 0, 0);
   1329 		break;
   1330 	    }
   1331 	    if (((GET_5380_REG(NCR5380_IDSTAT) >> 2) & 7) != ph) {
   1332 		DBG_PIOPRINT ("SCSI: transfer_pio: phase mismatch\n", 0, 0);
   1333 		break;
   1334 	    }
   1335 	    if (PH_IN(ph)) {
   1336 		*data++ = GET_5380_REG(NCR5380_DATA);
   1337 		SET_5380_REG(NCR5380_ICOM, SC_A_ACK);
   1338 		if ((cnt == 1) && dont_drop_ack)
   1339 			new_icom = SC_A_ACK;
   1340 		else new_icom = 0;
   1341 	    }
   1342 	    else {
   1343 		SET_5380_REG(NCR5380_DATA, *data++);
   1344 
   1345 		/*
   1346 		 * The SCSI-standard suggests that in the 'MESSAGE OUT' phase,
   1347 		 * the initiator should drop ATN on the last byte of the
   1348 		 * message phase after REQ has been asserted for the handshake
   1349 		 * but before the initiator raises ACK.
   1350 		 */
   1351 		if (!( (ph == PH_MSGOUT) && (cnt > 1) )) {
   1352 			SET_5380_REG(NCR5380_ICOM, SC_ADTB);
   1353 			SET_5380_REG(NCR5380_ICOM, SC_ADTB | SC_A_ACK);
   1354 			new_icom = 0;
   1355 		}
   1356 		else {
   1357 			SET_5380_REG(NCR5380_ICOM, SC_ADTB | SC_A_ATN);
   1358 			SET_5380_REG(NCR5380_ICOM, SC_ADTB|SC_A_ATN|SC_A_ACK);
   1359 			new_icom = SC_A_ATN;
   1360 		}
   1361 	    }
   1362 	    if (!wait_req_false()) {
   1363 		DBG_PIOPRINT ("SCSI: transfer_pio - REQ not dropping\n", 0, 0);
   1364 		break;
   1365 	    }
   1366 	    SET_5380_REG(NCR5380_ICOM, new_icom);
   1367 
   1368 	} while (--cnt);
   1369 
   1370 	if ((tmp = GET_5380_REG(NCR5380_IDSTAT)) & SC_S_REQ)
   1371 		*phase = (tmp >> 2) & 7;
   1372 	else *phase = NR_PHASE;
   1373 	*len = cnt;
   1374 	DBG_PIOPRINT ("SCSI: transfer_pio done: phase: %d, len: %d\n",
   1375 								*phase, cnt);
   1376 	PID("tpio2");
   1377 	if (!cnt || (*phase == ph))
   1378 		return (0);
   1379 	return (-1);
   1380 }
   1381 
   1382 #ifdef REAL_DMA
   1383 /*
   1384  * Start a DMA-transfer on the device using the current pointers.
   1385  * If 'poll' is true, the function busy-waits until DMA has completed.
   1386  */
   1387 static void
   1388 transfer_dma(reqp, phase, poll)
   1389 SC_REQ	*reqp;
   1390 u_int	phase;
   1391 int	poll;
   1392 {
   1393 	int	dma_done;
   1394 	u_char	mbase = 0;
   1395 	int	sps;
   1396 
   1397 again:
   1398 	PID("tdma1");
   1399 
   1400 	/*
   1401 	 * We should be in phase, otherwise we are not allowed to
   1402 	 * drive the bus.
   1403 	 */
   1404 	SET_5380_REG(NCR5380_TCOM, phase);
   1405 
   1406 	/*
   1407 	 * Defer interrupts until DMA is fully running.
   1408 	 */
   1409 	sps = splbio();
   1410 
   1411 	/*
   1412 	 * Clear pending interrupts and parity errors.
   1413 	 */
   1414 	scsi_clr_ipend();
   1415 
   1416 	if (!poll) {
   1417 		/*
   1418 		 * Enable SCSI interrupts and set IN_DMA flag, set 'mbase'
   1419 		 * to the interrupts we want enabled.
   1420 		 */
   1421 		scsi_ienable();
   1422 		reqp->dr_flag |= DRIVER_IN_DMA;
   1423 		mbase = SC_E_EOPI | SC_MON_BSY;
   1424 	}
   1425 	else scsi_idisable();
   1426 	mbase |=  IMODE_BASE | SC_M_DMA;
   1427 	scsi_dma_setup(reqp, phase, mbase);
   1428 
   1429 	splx(sps);
   1430 
   1431 	if (poll) {
   1432 		/*
   1433 		 * On polled-dma transfers, we wait here until the
   1434 		 * 'end-of-dma' condition occurs.
   1435 		 */
   1436 		poll_edma(reqp);
   1437 		if (!(dma_done = dma_ready()))
   1438 			goto again;
   1439 	}
   1440 	PID("tdma2");
   1441 }
   1442 
   1443 /*
   1444  * Check results of a DMA data-transfer.
   1445  */
   1446 static int
   1447 dma_ready()
   1448 {
   1449 	SC_REQ	*reqp = connected;
   1450 	int	dmstat, is_edma;
   1451 	long	bytes_left, bytes_done;
   1452 
   1453 	is_edma = get_dma_result(reqp, &bytes_left);
   1454 	dmstat  = GET_5380_REG(NCR5380_DMSTAT);
   1455 
   1456 	/*
   1457 	 * Check if the call is sensible and not caused by any spurious
   1458 	 * interrupt.
   1459 	 */
   1460 	if (!is_edma && !(dmstat & (SC_END_DMA|SC_BSY_ERR))
   1461 		     && (dmstat & SC_PHS_MTCH) ) {
   1462 		ncr_tprint(reqp, "dma_ready: spurious call "
   1463 				 "(dm:%x,last_hit: %s)\n",
   1464 #ifdef DBG_PID
   1465 					dmstat, last_hit[DBG_PID-1]);
   1466 #else
   1467 					dmstat, "unknown");
   1468 #endif
   1469 		return (0);
   1470 	}
   1471 
   1472 	/*
   1473 	 * Clear all (pending) interrupts.
   1474 	 */
   1475 	scsi_clr_ipend();
   1476 
   1477 	/*
   1478 	 * Update various transfer-pointers/lengths
   1479 	 */
   1480 	bytes_done = reqp->dm_cur->dm_count - bytes_left;
   1481 
   1482 	if ((reqp->dr_flag & DRIVER_BOUNCING) && (PH_IN(reqp->phase))) {
   1483 		/*
   1484 		 * Copy the bytes read until now from the bounce buffer
   1485 		 * to the 'real' destination. Flush the data-cache
   1486 		 * before copying.
   1487 		 */
   1488 		PCIA();
   1489 		bcopy(reqp->bouncerp, reqp->xdata_ptr, bytes_done);
   1490 		reqp->bouncerp += bytes_done;
   1491 	}
   1492 
   1493 	reqp->xdata_ptr  = &reqp->xdata_ptr[bytes_done];	/* XXX */
   1494 	reqp->xdata_len -= bytes_done;				/* XXX */
   1495 	if ((reqp->dm_cur->dm_count -= bytes_done) == 0)
   1496 		reqp->dm_cur++;
   1497 	else reqp->dm_cur->dm_addr += bytes_done;
   1498 
   1499 	if (PH_IN(reqp->phase) && (dmstat & SC_PAR_ERR)) {
   1500 		if (!(ncr5380_no_parchk & (1 << reqp->targ_id)))
   1501 			/* XXX: Should be parity error ???? */
   1502 			reqp->xs->error = XS_DRIVER_STUFFUP;
   1503 	}
   1504 
   1505 	/*
   1506 	 * DMA mode should always be reset even when we will continue with the
   1507 	 * next chain. It is also essential to clear the MON_BUSY because
   1508 	 * when LOST_BUSY is unexpectedly set, we will not be able to drive
   1509 	 * the bus....
   1510 	 */
   1511 	SET_5380_REG(NCR5380_MODE, IMODE_BASE);
   1512 
   1513 
   1514 	if ((dmstat & SC_BSY_ERR) || !(dmstat & SC_PHS_MTCH)
   1515 		 || (reqp->dm_cur > reqp->dm_last) || (reqp->xs->error)) {
   1516 
   1517 		/*
   1518 		 * Tell interrupt functions DMA mode has ended.
   1519 		 */
   1520 		reqp->dr_flag &= ~DRIVER_IN_DMA;
   1521 
   1522 		/*
   1523 		 * Clear mode and icom
   1524 		 */
   1525 		SET_5380_REG(NCR5380_MODE, IMODE_BASE);
   1526 		SET_5380_REG(NCR5380_ICOM, 0);
   1527 
   1528 		if (dmstat & SC_BSY_ERR) {
   1529 			if (!reqp->xs->error)
   1530 				reqp->xs->error = XS_BUSY;
   1531 			finish_req(reqp);
   1532 			PID("dma_ready1");
   1533 			return (1);
   1534 		}
   1535 
   1536 		if (reqp->xs->error != 0) {
   1537 ncr_tprint(reqp, "dma-ready: code = %d\n", reqp->xs->error); /* LWP */
   1538 			reqp->msgout = MSG_ABORT;
   1539 			SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
   1540 		}
   1541 		PID("dma_ready2");
   1542 		return (1);
   1543 	}
   1544 	return (0);
   1545 }
   1546 #endif /* REAL_DMA */
   1547 
   1548 static int
   1549 check_autosense(reqp, linked)
   1550 SC_REQ	*reqp;
   1551 int	linked;
   1552 {
   1553 	int	sps;
   1554 
   1555 	/*
   1556 	 * If this is the driver's Link Check for this target, ignore
   1557 	 * the results of the command.  All we care about is whether we
   1558 	 * got here from a LINK_CMD_COMPLETE or CMD_COMPLETE message.
   1559 	 */
   1560 	PID("linkcheck");
   1561 	if (reqp->dr_flag & DRIVER_LINKCHK) {
   1562 		if (linked)
   1563 			ncr_will_link |= 1<<reqp->targ_id;
   1564 		else ncr_tprint(reqp, "Does not support linked commands\n");
   1565 		return (0);
   1566 	}
   1567 	/*
   1568 	 * If we not executing an auto-sense and the status code
   1569 	 * is request-sense, we automatically issue a request
   1570 	 * sense command.
   1571 	 */
   1572 	PID("cautos1");
   1573 	if (!(reqp->dr_flag & DRIVER_AUTOSEN)) {
   1574 		switch (reqp->status & SCSMASK) {
   1575 		    case SCSCHKC:
   1576 			bcopy(sense_cmd, &reqp->xcmd, sizeof(sense_cmd));
   1577 			reqp->xdata_ptr = (u_char *)&reqp->xs->sense;
   1578 			reqp->xdata_len = sizeof(reqp->xs->sense);
   1579 			reqp->dr_flag  |= DRIVER_AUTOSEN;
   1580 			reqp->dr_flag  &= ~DRIVER_DMAOK;
   1581 			if (!linked) {
   1582 				sps = splbio();
   1583 				reqp->next = issue_q;
   1584 				issue_q    = reqp;
   1585 				splx(sps);
   1586 			}
   1587 			else reqp->xcmd.bytes[sizeof(sense_cmd)-2] |= 1;
   1588 
   1589 #ifdef DBG_REQ
   1590 			bzero(reqp->xdata_ptr, reqp->xdata_len);
   1591 			if (dbg_target_mask & (1 << reqp->targ_id))
   1592 				show_request(reqp, "AUTO-SENSE");
   1593 #endif
   1594 			PID("cautos2");
   1595 			return (-1);
   1596 		    case SCSBUSY:
   1597 			reqp->xs->error = XS_BUSY;
   1598 			return (0);
   1599 		}
   1600 	}
   1601 	else {
   1602 		/*
   1603 		 * An auto-sense has finished
   1604 		 */
   1605 		if ((reqp->status & SCSMASK) != SCSGOOD)
   1606 			reqp->xs->error = XS_DRIVER_STUFFUP; /* SC_E_AUTOSEN; */
   1607 		else reqp->xs->error = XS_SENSE;
   1608 		reqp->status = SCSCHKC;
   1609 	}
   1610 	PID("cautos3");
   1611 	return (0);
   1612 }
   1613 
   1614 static int
   1615 reach_msg_out(sc, len)
   1616 struct ncr_softc *sc;
   1617 u_long		 len;
   1618 {
   1619 	u_char	phase;
   1620 	u_char	data;
   1621 
   1622 	ncr_aprint(sc, "Trying to reach Message-out phase\n");
   1623 	if ((phase = GET_5380_REG(NCR5380_IDSTAT)) & SC_S_REQ)
   1624 		phase = (phase >> 2) & 7;
   1625 	else return (-1);
   1626 	ncr_aprint(sc, "Trying to reach Message-out phase, now: %d\n", phase);
   1627 	if (phase == PH_MSGOUT)
   1628 		return (0);
   1629 
   1630 	SET_5380_REG(NCR5380_TCOM, phase);
   1631 
   1632 	do {
   1633 		if (!wait_req_true())
   1634 			break;
   1635 		if (((GET_5380_REG(NCR5380_IDSTAT) >> 2) & 7) != phase)
   1636 			break;
   1637 		if (PH_IN(phase)) {
   1638 			data = GET_5380_REG(NCR5380_DATA);
   1639 			SET_5380_REG(NCR5380_ICOM, SC_A_ACK | SC_A_ATN);
   1640 		}
   1641 		else {
   1642 			SET_5380_REG(NCR5380_DATA, 0);
   1643 			SET_5380_REG(NCR5380_ICOM, SC_ADTB|SC_A_ACK|SC_A_ATN);
   1644 		}
   1645 		if (!wait_req_false())
   1646 			break;
   1647 		SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
   1648 	} while (--len);
   1649 
   1650 	if ((phase = GET_5380_REG(NCR5380_IDSTAT)) & SC_S_REQ) {
   1651 		phase = (phase >> 2) & 7;
   1652 		if (phase == PH_MSGOUT) {
   1653 			ncr_aprint(sc, "Message-out phase reached.\n");
   1654 			return (0);
   1655 		}
   1656 	}
   1657 	return (-1);
   1658 }
   1659 
   1660 static void
   1661 scsi_reset(sc)
   1662 struct ncr_softc *sc;
   1663 {
   1664 	SC_REQ	*tmp, *next;
   1665 	int	sps;
   1666 
   1667 	ncr_aprint(sc, "Resetting SCSI-bus\n");
   1668 
   1669 	PID("scsi_reset1");
   1670 	sps = splbio();
   1671 	SET_5380_REG(NCR5380_ICOM, SC_A_RST);
   1672 	delay(1);
   1673 	SET_5380_REG(NCR5380_ICOM, 0);
   1674 
   1675 	/*
   1676 	 * None of the jobs in the discon_q will ever be reconnected,
   1677 	 * notify this to the higher level code.
   1678 	 */
   1679 	for (tmp = discon_q; tmp ;) {
   1680 		next = tmp->next;
   1681 		tmp->next = NULL;
   1682 		tmp->xs->error = XS_TIMEOUT;
   1683 		busy &= ~(1 << tmp->targ_id);
   1684 		finish_req(tmp);
   1685 		tmp = next;
   1686 	}
   1687 	discon_q = NULL;
   1688 
   1689 	/*
   1690 	 * The current job will never finish either.
   1691 	 * The problem is that we can't finish the job because an instance
   1692 	 * of main is running on it. Our best guess is that the job is currently
   1693 	 * doing REAL-DMA. In that case 'dma_ready()' should correctly finish
   1694 	 * the job because it detects BSY-loss.
   1695 	 */
   1696 	if (tmp = connected) {
   1697 		if (tmp->dr_flag & DRIVER_IN_DMA) {
   1698 			tmp->xs->error = XS_DRIVER_STUFFUP;
   1699 #ifdef REAL_DMA
   1700 			dma_ready();
   1701 #endif
   1702 		}
   1703 	}
   1704 	splx(sps);
   1705 	PID("scsi_reset2");
   1706 }
   1707 
   1708 /*
   1709  * Check validity of the IRQ set by the 5380. If the interrupt is valid,
   1710  * the appropriate action is carried out (reselection or DMA ready) and
   1711  * INTR_RESEL or INTR_DMA is returned. Otherwise a console notice is written
   1712  * and INTR_SPURIOUS is returned.
   1713  */
   1714 static int
   1715 check_intr(sc)
   1716 struct ncr_softc *sc;
   1717 {
   1718 	SC_REQ	*reqp;
   1719 
   1720 	if ((GET_5380_REG(NCR5380_IDSTAT) & (SC_S_SEL|SC_S_IO))
   1721 							==(SC_S_SEL|SC_S_IO))
   1722 		return (INTR_RESEL);
   1723 	else {
   1724 		if ((reqp = connected) && (reqp->dr_flag & DRIVER_IN_DMA)){
   1725 			reqp->dr_flag &= ~DRIVER_IN_DMA;
   1726 			return (INTR_DMA);
   1727 		}
   1728 	}
   1729 	scsi_clr_ipend();
   1730 	printf("-->");
   1731 	scsi_show();
   1732 	ncr_aprint(sc, "Spurious interrupt.\n");
   1733 	return (INTR_SPURIOUS);
   1734 }
   1735 
   1736 #ifdef REAL_DMA
   1737 /*
   1738  * Check if DMA can be used for this request. This function also builds
   1739  * the dma-chain.
   1740  */
   1741 static int
   1742 scsi_dmaok(reqp)
   1743 SC_REQ	*reqp;
   1744 {
   1745 	u_long			phy_buf;
   1746 	u_long			phy_len;
   1747 	void			*req_addr;
   1748 	u_long			req_len;
   1749 	struct dma_chain	*dm;
   1750 
   1751 	/*
   1752 	 * Initialize locals and requests' DMA-chain.
   1753 	 */
   1754 	req_len        = reqp->xdata_len;
   1755 	req_addr       = (void*)reqp->xdata_ptr;
   1756 	dm             = reqp->dm_cur = reqp->dm_last = reqp->dm_chain;
   1757 	dm->dm_count   = dm->dm_addr = 0;
   1758 	reqp->dr_flag &= ~DRIVER_BOUNCING;
   1759 
   1760 	/*
   1761 	 * Do not accept zero length DMA.
   1762 	 */
   1763 	if (req_len == 0)
   1764 		return (0);
   1765 
   1766 	/*
   1767 	 * LWP: I think that this restriction is not strictly nessecary.
   1768 	 */
   1769 	if ((req_len & 0x1) || ((u_int)req_addr & 0x3))
   1770 		return (0);
   1771 
   1772 	/*
   1773 	 * Build the DMA-chain.
   1774 	 */
   1775 	dm->dm_addr = phy_buf = kvtop(req_addr);
   1776 	while (req_len) {
   1777 		if (req_len < (phy_len = NBPG - ((u_long)req_addr & PGOFSET)))
   1778 			phy_len = req_len;
   1779 
   1780 		req_addr     += phy_len;
   1781 		req_len      -= phy_len;
   1782 		dm->dm_count += phy_len;
   1783 
   1784 		if (req_len) {
   1785 			u_long	tmp = kvtop(req_addr);
   1786 
   1787 			if ((phy_buf + phy_len) != tmp) {
   1788 			    if (wrong_dma_range(reqp, dm)) {
   1789 				if (reqp->dr_flag & DRIVER_BOUNCING)
   1790 					goto bounceit;
   1791 				return (0);
   1792 			    }
   1793 
   1794 			    if (++dm >= &reqp->dm_chain[MAXDMAIO]) {
   1795 				ncr_tprint(reqp,"dmaok: DMA chain too long!\n");
   1796 				return (0);
   1797 			    }
   1798 			    dm->dm_count = 0;
   1799 			    dm->dm_addr  = tmp;
   1800 			}
   1801 			phy_buf = tmp;
   1802 		}
   1803 	}
   1804         if (wrong_dma_range(reqp, dm)) {
   1805 		if (reqp->dr_flag & DRIVER_BOUNCING)
   1806 			goto bounceit;
   1807 		return (0);
   1808 	}
   1809 	reqp->dm_last = dm;
   1810 	return (1);
   1811 
   1812 bounceit:
   1813 	if ((reqp->bounceb = alloc_bounceb(reqp->xdata_len)) == NULL) {
   1814 		/*
   1815 		 * If we can't get a bounce buffer, forget DMA
   1816 		 */
   1817 		reqp->dr_flag &= ~DRIVER_BOUNCING;
   1818 		return(0);
   1819 	}
   1820 	/*
   1821 	 * Initialize a single DMA-range containing the bounced request
   1822 	 */
   1823 	dm = reqp->dm_cur = reqp->dm_last = reqp->dm_chain;
   1824 	dm->dm_addr    = kvtop(reqp->bounceb);
   1825 	dm->dm_count   = reqp->xdata_len;
   1826 	reqp->bouncerp = reqp->bounceb;
   1827 
   1828 	return (1);
   1829 }
   1830 #endif /* REAL_DMA */
   1831 
   1832 static void
   1833 run_main(sc)
   1834 struct ncr_softc *sc;
   1835 {
   1836 	int	sps = splbio();
   1837 
   1838 	if (!main_running) {
   1839 		/*
   1840 		 * If shared resources are required, claim them
   1841 		 * before entering 'scsi_main'. If we can't get them
   1842 		 * now, assume 'run_main' will be called when the resource
   1843 		 * becomes available.
   1844 		 */
   1845 		if (!claimed_dma()) {
   1846 			splx(sps);
   1847 			return;
   1848 		}
   1849 		main_running = 1;
   1850 		splx(sps);
   1851 		scsi_main(sc);
   1852 	}
   1853 	else splx(sps);
   1854 }
   1855 
   1856 /*
   1857  * Prefix message with full target info.
   1858  */
   1859 static void
   1860 ncr_tprint(SC_REQ *reqp, char *fmt, ...)
   1861 {
   1862 	va_list	ap;
   1863 
   1864 	va_start(ap, fmt);
   1865 	sc_print_addr(reqp->xs->sc_link);
   1866 	printf("%r", fmt, ap);
   1867 	va_end(ap);
   1868 }
   1869 
   1870 /*
   1871  * Prefix message with adapter info.
   1872  */
   1873 static void
   1874 ncr_aprint(struct ncr_softc *sc, char *fmt, ...)
   1875 {
   1876 	va_list	ap;
   1877 
   1878 	va_start(ap, fmt);
   1879 	printf("%s : %r", sc->sc_dev.dv_xname, fmt, ap);
   1880 	va_end(ap);
   1881 }
   1882 /****************************************************************************
   1883  *		Start Debugging Functions				    *
   1884  ****************************************************************************/
   1885 static void
   1886 show_data_sense(xs)
   1887 struct scsi_xfer	*xs;
   1888 {
   1889 	u_char	*p1, *p2;
   1890 	int	i;
   1891 	int	sz;
   1892 
   1893 	p1 = (u_char *) xs->cmd;
   1894 	p2 = (u_char *)&xs->sense;
   1895 	if(*p2 == 0)
   1896 		return;	/* No(n)sense */
   1897 	printf("cmd[%d,%d]: ", xs->cmdlen, sz = command_size(*p1));
   1898 	for (i = 0; i < sz; i++)
   1899 		printf("%x ", p1[i]);
   1900 	printf("\nsense: ");
   1901 	for (i = 0; i < sizeof(xs->sense); i++)
   1902 		printf("%x ", p2[i]);
   1903 	printf("\n");
   1904 }
   1905 
   1906 static void
   1907 show_request(reqp, qtxt)
   1908 SC_REQ	*reqp;
   1909 char	*qtxt;
   1910 {
   1911 	printf("REQ-%s: %d %x[%d] cmd[0]=%x S=%x M=%x R=%x resid=%d dr_flag=%x %s\n",
   1912 			qtxt, reqp->targ_id, reqp->xdata_ptr, reqp->xdata_len,
   1913 			reqp->xcmd.opcode, reqp->status, reqp->message,
   1914 			reqp->xs->error, reqp->xs->resid, reqp->dr_flag,
   1915 			reqp->link ? "L":"");
   1916 	if (reqp->status == SCSCHKC)
   1917 		show_data_sense(reqp->xs);
   1918 }
   1919 
   1920 static char *sig_names[] = {
   1921 	"PAR", "SEL", "I/O", "C/D", "MSG", "REQ", "BSY", "RST",
   1922 	"ACK", "ATN", "LBSY", "PMATCH", "IRQ", "EPAR", "DREQ", "EDMA"
   1923 };
   1924 
   1925 static void
   1926 show_signals(dmstat, idstat)
   1927 u_char	dmstat, idstat;
   1928 {
   1929 	u_short	tmp, mask;
   1930 	int	i, j, need_pipe;
   1931 
   1932 	tmp = idstat | ((dmstat & 3) << 8);
   1933 	printf("Bus signals (%02x/%02x): ", idstat, dmstat & 3);
   1934 	for (mask = 1, j = need_pipe = 0; mask <= tmp; mask <<= 1, j++) {
   1935 		if (tmp & mask)
   1936 			printf("%s%s", need_pipe++ ? "|" : "", sig_names[j]);
   1937 	}
   1938 	printf("\nDma status (%02x): ", dmstat);
   1939 	for (mask = 4, j = 10, need_pipe = 0; mask <= dmstat; mask <<= 1, j++) {
   1940 		if (dmstat & mask)
   1941 			printf("%s%s", need_pipe++ ? "|" : "", sig_names[j]);
   1942 	}
   1943 	printf("\n");
   1944 }
   1945 
   1946 scsi_show()
   1947 {
   1948 	SC_REQ	*tmp;
   1949 	int	sps = splhigh();
   1950 	u_char	idstat, dmstat;
   1951 	int	i;
   1952 
   1953 	printf("scsi_show: main_running is%s running\n",
   1954 		main_running?"":" not");
   1955 	for (tmp = issue_q; tmp; tmp = tmp->next)
   1956 		show_request(tmp, "ISSUED");
   1957 	for (tmp = discon_q; tmp; tmp = tmp->next)
   1958 		show_request(tmp, "DISCONNECTED");
   1959 	if (connected)
   1960 		show_request(connected, "CONNECTED");
   1961 	idstat = GET_5380_REG(NCR5380_IDSTAT);
   1962 	dmstat = GET_5380_REG(NCR5380_DMSTAT);
   1963 	show_signals(dmstat, idstat);
   1964 	if (connected)
   1965 		printf("phase = %d, ", connected->phase);
   1966 	printf("busy:%x, spl:%04x\n", busy, sps);
   1967 #ifdef	DBG_PID
   1968 	for (i=0; i<DBG_PID; i++)
   1969 		printf("\t%d\t%s\n", i, last_hit[i]);
   1970 #endif
   1971 
   1972 	splx(sps);
   1973 }
   1974