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