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