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