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