Home | History | Annotate | Line # | Download | only in dev
ncr5380.c revision 1.38
      1 /*	$NetBSD: ncr5380.c,v 1.38 1996/12/19 21:48:18 scottr 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 u_char	ncr_will_link = 0x00;
     55 #ifdef	TRY_SCSI_LINKED_COMMANDS
     56 u_char	ncr_test_link = ((~TRY_SCSI_LINKED_COMMANDS) & 0x7f);
     57 #else
     58 u_char	ncr_test_link = 0x7f;
     59 #endif
     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  * -- A sleeping Fujitsu M2512 is even worse; try 2.5 sec     -hf 20 Jun
    134  */
    135 extern __inline__ int wait_req_true(void)
    136 {
    137 	int	timeout = 2500000;
    138 
    139 	while (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_REQ) && --timeout)
    140 		delay(1);
    141 	return (GET_5380_REG(NCR5380_IDSTAT) & SC_S_REQ);
    142 }
    143 
    144 /*
    145  * Wait for request-line to become inactive. When it doesn't return 0.
    146  * Otherwise return != 0.
    147  */
    148 extern __inline__ int wait_req_false(void)
    149 {
    150 	int	timeout = 2500000;
    151 
    152 	while ((GET_5380_REG(NCR5380_IDSTAT) & SC_S_REQ) && --timeout)
    153 		delay(1);
    154 	return (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_REQ));
    155 }
    156 
    157 extern __inline__ void ack_message()
    158 {
    159 	SET_5380_REG(NCR5380_ICOM, 0);
    160 }
    161 
    162 extern __inline__ void nack_message(SC_REQ *reqp, u_char msg)
    163 {
    164 	SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
    165 	reqp->msgout = msg;
    166 }
    167 
    168 extern __inline__ void finish_req(SC_REQ *reqp)
    169 {
    170 	int			sps;
    171 	struct scsi_xfer	*xs = reqp->xs;
    172 
    173 #ifdef REAL_DMA
    174 	/*
    175 	 * If we bounced, free the bounce buffer
    176 	 */
    177 	if (reqp->dr_flag & DRIVER_BOUNCING)
    178 		free_bounceb(reqp->bounceb);
    179 #endif /* REAL_DMA */
    180 #ifdef DBG_REQ
    181 	if (dbg_target_mask & (1 << reqp->targ_id))
    182 		show_request(reqp, "DONE");
    183 #endif
    184 #ifdef DBG_ERR_RET
    185 	if (reqp->xs->error != 0)
    186 		show_request(reqp, "ERR_RET");
    187 #endif
    188 	/*
    189 	 * Return request to free-q
    190 	 */
    191 	sps = splbio();
    192 	reqp->next = free_head;
    193 	free_head  = reqp;
    194 	splx(sps);
    195 
    196 	xs->flags |= ITSDONE;
    197 	if (!(reqp->dr_flag & DRIVER_LINKCHK))
    198 		scsi_done(xs);
    199 }
    200 
    201 /*
    202  * Auto config stuff....
    203  */
    204 void	ncr_attach __P((struct device *, struct device *, void *));
    205 int	ncr_match __P((struct device *, struct cfdata *, void *));
    206 
    207 /*
    208  * Tricks to make driver-name configurable
    209  */
    210 #define CFNAME(n)	__CONCAT(n,_cd)
    211 #define CANAME(n)	__CONCAT(n,_ca)
    212 #define CFSTRING(n)	__STRING(n)
    213 
    214 struct cfattach CANAME(DRNAME) = {
    215 	sizeof(struct ncr_softc), ncr_match, ncr_attach
    216 };
    217 
    218 struct cfdriver CFNAME(DRNAME) = {
    219 	NULL, CFSTRING(DRNAME), DV_DULL
    220 };
    221 
    222 int
    223 ncr_match(parent, cf, aux)
    224 	struct device *parent;
    225 	struct cfdata *cf;
    226 	void *aux;
    227 {
    228 	return (machine_match(parent, cf, aux, &CFNAME(DRNAME)));
    229 }
    230 
    231 void
    232 ncr_attach(pdp, dp, auxp)
    233 struct device	*pdp, *dp;
    234 void		*auxp;
    235 {
    236 	struct ncr_softc	*sc;
    237 	int			i;
    238 
    239 	sc = (struct ncr_softc *)dp;
    240 
    241 	sc->sc_link.channel         = SCSI_CHANNEL_ONLY_ONE;
    242 	sc->sc_link.adapter_softc   = sc;
    243 	sc->sc_link.adapter_target  = 7;
    244 	sc->sc_link.adapter         = &ncr5380_switch;
    245 	sc->sc_link.device          = &ncr5380_dev;
    246 	sc->sc_link.openings        = NREQ - 1;
    247 	sc->sc_link.max_target      = 7;
    248 
    249 	/*
    250 	 * bitmasks
    251 	 */
    252 	sc->sc_noselatn = 0;
    253 	sc->sc_selected = 0;
    254 
    255 	/*
    256 	 * Initialize machine-type specific things...
    257 	 */
    258 	scsi_mach_init(sc);
    259 	printf("\n");
    260 
    261 	/*
    262 	 * Initialize request queue freelist.
    263 	 */
    264 	for (i = 0; i < NREQ; i++) {
    265 		req_queue[i].next = free_head;
    266 		free_head = &req_queue[i];
    267 	}
    268 
    269 	/*
    270 	 * Initialize the host adapter
    271 	 */
    272 	scsi_idisable();
    273 	ENABLE_NCR5380(sc);
    274 	SET_5380_REG(NCR5380_ICOM, 0);
    275 	SET_5380_REG(NCR5380_MODE, IMODE_BASE);
    276 	SET_5380_REG(NCR5380_TCOM, 0);
    277 	SET_5380_REG(NCR5380_IDSTAT, 0);
    278 	scsi_ienable();
    279 
    280 	/*
    281 	 * attach all scsi units on us
    282 	 */
    283 	config_found(dp, &sc->sc_link, scsiprint);
    284 }
    285 
    286 /*
    287  * End of auto config stuff....
    288  */
    289 
    290 /*
    291  * Carry out a request from the high level driver.
    292  */
    293 static int
    294 ncr5380_scsi_cmd(struct scsi_xfer *xs)
    295 {
    296 	int	sps;
    297 	SC_REQ	*reqp, *link, *tmp;
    298 	int	flags = xs->flags;
    299 
    300 	/*
    301 	 * We do not queue RESET commands
    302 	 */
    303 	if (flags & SCSI_RESET) {
    304 		scsi_reset_verbose(xs->sc_link->adapter_softc,
    305 				   "Got reset-command");
    306 		return (COMPLETE);
    307 	}
    308 
    309 	/*
    310 	 * Get a request block
    311 	 */
    312 	sps = splbio();
    313 	if ((reqp = free_head) == 0) {
    314 		splx(sps);
    315 		return (TRY_AGAIN_LATER);
    316 	}
    317 	free_head  = reqp->next;
    318 	reqp->next = NULL;
    319 	splx(sps);
    320 
    321 	/*
    322 	 * Initialize our private fields
    323 	 */
    324 	reqp->dr_flag   = (flags & SCSI_POLL) ? DRIVER_NOINT : 0;
    325 	reqp->phase     = NR_PHASE;
    326 	reqp->msgout    = MSG_NOOP;
    327 	reqp->status    = SCSGOOD;
    328 	reqp->message   = 0xff;
    329 	reqp->link      = NULL;
    330 	reqp->xs        = xs;
    331 	reqp->targ_id   = xs->sc_link->target;
    332 	reqp->targ_lun  = xs->sc_link->lun;
    333 	reqp->xdata_ptr = (u_char*)xs->data;
    334 	reqp->xdata_len = xs->datalen;
    335 	memcpy(&reqp->xcmd, xs->cmd, sizeof(struct scsi_generic));
    336 	reqp->xcmd.bytes[0] |= reqp->targ_lun << 5;
    337 
    338 	/*
    339 	 * Sanity check on flags...
    340 	 */
    341 	if (flags & ITSDONE) {
    342 		ncr_tprint(reqp, "scsi_cmd: command already done.....\n");
    343 		xs->flags &= ~ITSDONE;
    344 	}
    345 	if (!(flags & INUSE)) {
    346 		ncr_tprint(reqp, "scsi_cmd: command not in use.....\n");
    347 		xs->flags |= INUSE;
    348 	}
    349 
    350 #ifdef REAL_DMA
    351 	/*
    352 	 * Check if DMA can be used on this request
    353 	 */
    354 	if (scsi_dmaok(reqp))
    355 		reqp->dr_flag |= DRIVER_DMAOK;
    356 #endif /* REAL_DMA */
    357 
    358 	/*
    359 	 * Insert the command into the issue queue. Note that 'REQUEST SENSE'
    360 	 * commands are inserted at the head of the queue since any command
    361 	 * will clear the existing contingent allegience condition and the sense
    362 	 * data is only valid while the condition exists.
    363 	 * When possible, link the command to a previous command to the same
    364 	 * target. This is not very sensible when AUTO_SENSE is not defined!
    365 	 * Interrupts are disabled while we are fiddling with the issue-queue.
    366 	 */
    367 	sps = splbio();
    368 	link = NULL;
    369 	if ((issue_q == NULL) || (reqp->xcmd.opcode == REQUEST_SENSE)) {
    370 		reqp->next = issue_q;
    371 		issue_q    = reqp;
    372 	}
    373 	else {
    374 		tmp  = issue_q;
    375 		do {
    376 		    if (!link && (tmp->targ_id == reqp->targ_id) && !tmp->link)
    377 				link = tmp;
    378 		} while (tmp->next && (tmp = tmp->next));
    379 		tmp->next = reqp;
    380 #ifdef AUTO_SENSE
    381 		if (link && (ncr_will_link & (1<<reqp->targ_id))) {
    382 			link->link = reqp;
    383 			link->xcmd.bytes[link->xs->cmdlen-2] |= 1;
    384 		}
    385 #endif
    386 	}
    387 #ifdef AUTO_SENSE
    388 	/*
    389 	 * If we haven't already, check the target for link support.
    390 	 * Do this by prefixing the current command with a dummy
    391 	 * Request_Sense command, link the dummy to the current
    392 	 * command, and insert the dummy command at the head of the
    393 	 * issue queue.  Set the DRIVER_LINKCHK flag so that we'll
    394 	 * ignore the results of the dummy command, since we only
    395 	 * care about whether it was accepted or not.
    396 	 */
    397 	if (!link && !(ncr_test_link & (1<<reqp->targ_id)) &&
    398 	    (tmp = free_head) && !(reqp->dr_flag & DRIVER_NOINT)) {
    399 		free_head = tmp->next;
    400 		tmp->dr_flag = (reqp->dr_flag & ~DRIVER_DMAOK) | DRIVER_LINKCHK;
    401 		tmp->phase = NR_PHASE;
    402 		tmp->msgout = MSG_NOOP;
    403 		tmp->status = SCSGOOD;
    404 		tmp->xs = reqp->xs;
    405 		tmp->targ_id = reqp->targ_id;
    406 		tmp->targ_lun = reqp->targ_lun;
    407 		bcopy(sense_cmd, &tmp->xcmd, sizeof(sense_cmd));
    408 		tmp->xdata_ptr = (u_char *)&tmp->xs->sense;
    409 		tmp->xdata_len = sizeof(tmp->xs->sense);
    410 		ncr_test_link |= 1<<tmp->targ_id;
    411 		tmp->link = reqp;
    412 		tmp->xcmd.bytes[sizeof(sense_cmd)-2] |= 1;
    413 		tmp->next = issue_q;
    414 		issue_q = tmp;
    415 #ifdef DBG_REQ
    416 		if (dbg_target_mask & (1 << tmp->targ_id))
    417 			show_request(tmp, "LINKCHK");
    418 #endif
    419 	}
    420 #endif
    421 	splx(sps);
    422 
    423 #ifdef DBG_REQ
    424 	if (dbg_target_mask & (1 << reqp->targ_id))
    425 		show_request(reqp, (reqp->xcmd.opcode == REQUEST_SENSE) ?
    426 								"HEAD":"TAIL");
    427 #endif
    428 
    429 	run_main(xs->sc_link->adapter_softc);
    430 
    431 	if (xs->flags & (SCSI_POLL|ITSDONE))
    432 		return (COMPLETE); /* We're booting or run_main has completed */
    433 	return (SUCCESSFULLY_QUEUED);
    434 }
    435 
    436 static void
    437 ncr5380_minphys(struct buf *bp)
    438 {
    439     if (bp->b_bcount > MIN_PHYS)
    440 	bp->b_bcount = MIN_PHYS;
    441     minphys(bp);
    442 }
    443 #undef MIN_PHYS
    444 
    445 static void
    446 ncr5380_show_scsi_cmd(struct scsi_xfer *xs)
    447 {
    448 	u_char	*b = (u_char *) xs->cmd;
    449 	int	i  = 0;
    450 
    451 	if (!(xs->flags & SCSI_RESET)) {
    452 		printf("(%d:%d:%d,0x%x)-", xs->sc_link->scsibus,
    453 		    xs->sc_link->target, xs->sc_link->lun, xs->sc_link->flags);
    454 		while (i < xs->cmdlen) {
    455 			if (i)
    456 				printf(",");
    457 			printf("%x",b[i++]);
    458 		}
    459 		printf("-\n");
    460 	}
    461 	else {
    462 		printf("(%d:%d:%d)-RESET-\n",
    463 		    xs->sc_link->scsibus,xs->sc_link->target, xs->sc_link->lun);
    464 	}
    465 }
    466 
    467 /*
    468  * The body of the driver.
    469  */
    470 static void
    471 scsi_main(sc)
    472 struct ncr_softc *sc;
    473 {
    474 	SC_REQ	*req, *prev;
    475 	int	itype;
    476 	int	sps;
    477 
    478 	/*
    479 	 * While running in the driver SCSI-interrupts are disabled.
    480 	 */
    481 	scsi_idisable();
    482 	ENABLE_NCR5380(sc);
    483 
    484 	PID("scsi_main1");
    485 	for (;;) {
    486 	    sps = splbio();
    487 	    if (!connected) {
    488 		/*
    489 		 * Check if it is fair keep any exclusive access to DMA
    490 		 * claimed. If not, stop queueing new jobs so the discon_q
    491 		 * will be eventually drained and DMA can be given up.
    492 		 */
    493 		if (!fair_to_keep_dma())
    494 			goto main_exit;
    495 
    496 		/*
    497 		 * Search through the issue-queue for a command
    498 		 * destined for a target that isn't busy.
    499 		 */
    500 		prev = NULL;
    501 		for (req=issue_q; req != NULL; prev = req, req = req->next) {
    502 			if (!(busy & (1 << req->targ_id))) {
    503 				/*
    504 				 * Found one, remove it from the issue queue
    505 				 */
    506 				if (prev == NULL)
    507 					issue_q = req->next;
    508 				else prev->next = req->next;
    509 				req->next = NULL;
    510 				break;
    511 			}
    512 		}
    513 
    514 		/*
    515 		 * When a request has just ended, we get here before an other
    516 		 * device detects that the bus is free and that it can
    517 		 * reconnect. The problem is that when this happens, we always
    518 		 * baffle the device because our (initiator) id is higher. This
    519 		 * can cause a sort of starvation on slow devices. So we check
    520 		 * for a pending reselection here.
    521 		 * Note that 'connected' will be non-null if the reselection
    522 		 * succeeds.
    523 		 */
    524 		if ((GET_5380_REG(NCR5380_IDSTAT) & (SC_S_SEL|SC_S_IO))
    525 						== (SC_S_SEL|SC_S_IO)){
    526 			if (req != NULL) {
    527 				req->next = issue_q;
    528 				issue_q = req;
    529 			}
    530 			splx(sps);
    531 
    532 			reselect(sc);
    533 			scsi_clr_ipend();
    534 			goto connected;
    535 		}
    536 
    537 		/*
    538 		 * The host is not connected and there is no request
    539 		 * pending, exit.
    540 		 */
    541 		if (req == NULL) {
    542 			PID("scsi_main2");
    543 			goto main_exit;
    544 		}
    545 
    546 		/*
    547 		 * Re-enable interrupts before handling the request.
    548 		 */
    549 		splx(sps);
    550 
    551 #ifdef DBG_REQ
    552 		if (dbg_target_mask & (1 << req->targ_id))
    553 			show_request(req, "TARGET");
    554 #endif
    555 		/*
    556 		 * We found a request. Try to connect to the target. If the
    557 		 * initiator fails arbitration, the command is put back in the
    558 		 * issue queue.
    559 		 */
    560 		if (scsi_select(req, 0)) {
    561 			sps = splbio();
    562 			req->next = issue_q;
    563 			issue_q = req;
    564 			splx(sps);
    565 #ifdef DBG_REQ
    566 			if (dbg_target_mask & (1 << req->targ_id))
    567 				ncr_tprint(req, "Select failed\n");
    568 #endif
    569 		}
    570 	    }
    571 	    else splx(sps);
    572 connected:
    573 	    if (connected) {
    574 		/*
    575 		 * If the host is currently connected but a 'real-dma' transfer
    576 		 * is in progress, the 'end-of-dma' interrupt restarts main.
    577 		 * So quit.
    578 		 */
    579 		sps = splbio();
    580 		if (connected && (connected->dr_flag & DRIVER_IN_DMA)) {
    581 			PID("scsi_main3");
    582 			goto main_exit;
    583 		}
    584 		splx(sps);
    585 
    586 		/*
    587 		 * Let the target guide us through the bus-phases
    588 		 */
    589 		while (information_transfer(sc) == -1)
    590 			;
    591 	    }
    592 	}
    593 	/* NEVER TO REACH HERE */
    594 	panic("ncr5380-SCSI: not designed to come here");
    595 
    596 main_exit:
    597 	/*
    598 	 * We enter here with interrupts disabled. We are about to exit main
    599 	 * so interrupts should be re-enabled. Because interrupts are edge
    600 	 * triggered, we could already have missed the interrupt. Therefore
    601 	 * we check the IRQ-line here and re-enter when we really missed a
    602 	 * valid interrupt.
    603 	 */
    604 	PID("scsi_main4");
    605 	scsi_ienable();
    606 
    607 	/*
    608 	 * If we're not currently connected, enable reselection
    609 	 * interrupts.
    610 	 */
    611 	if (!connected)
    612 		SET_5380_REG(NCR5380_IDSTAT, SC_HOST_ID);
    613 
    614 	if (scsi_ipending()) {
    615 		if ((itype = check_intr(sc)) != INTR_SPURIOUS) {
    616 			scsi_idisable();
    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 			scsi_clr_ipend();
    631 			goto connected;
    632 		}
    633 	}
    634 	reconsider_dma();
    635 
    636 	main_running = 0;
    637 	splx(sps);
    638 	PID("scsi_main5");
    639 }
    640 
    641 #ifdef REAL_DMA
    642 /*
    643  * The SCSI-DMA interrupt.
    644  * This interrupt can only be triggered when running in non-polled DMA
    645  * mode. When DMA is not active, it will be silently ignored, it is usually
    646  * to late because the EOP interrupt of the controller happens just a tiny
    647  * bit earlier. It might become usefull when scatter/gather is implemented,
    648  * because in that case only part of the DATAIN/DATAOUT transfer is taken
    649  * out of a single buffer.
    650  */
    651 static void
    652 ncr_dma_intr(sc)
    653 struct ncr_softc *sc;
    654 {
    655 	SC_REQ	*reqp;
    656 	int	dma_done;
    657 
    658 	PID("ncr_dma_intr");
    659 	if ((reqp = connected) && (reqp->dr_flag & DRIVER_IN_DMA)) {
    660 		scsi_idisable();
    661 		if (!(dma_done = dma_ready())) {
    662 			transfer_dma(reqp, reqp->phase, 0);
    663 			return;
    664 		}
    665 		run_main(sc);
    666 	}
    667 }
    668 #endif /* REAL_DMA */
    669 
    670 /*
    671  * The SCSI-controller interrupt. This interrupt occurs on reselections and
    672  * at the end of non-polled DMA-interrupts. It is assumed to be called from
    673  * the machine-dependent hardware interrupt.
    674  */
    675 static void
    676 ncr_ctrl_intr(sc)
    677 struct ncr_softc *sc;
    678 {
    679 	int	itype;
    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 		DBG_INFPRINT(show_phase, reqp, phase);
   1016 	}
   1017 	else {
   1018 		/*
   1019 		 * Same data-phase. If same error give up
   1020 		 */
   1021 		if ((reqp->msgout == MSG_ABORT)
   1022 		     && ((phase == PH_DATAOUT) || (phase == PH_DATAIN))) {
   1023 			busy     &= ~(1 << reqp->targ_id);
   1024 			connected = NULL;
   1025 			finish_req(reqp);
   1026 			scsi_reset_verbose(sc, "Failure to abort command");
   1027 			return (0);
   1028 		}
   1029 	}
   1030 
   1031 	switch (phase) {
   1032 	    case PH_DATAOUT:
   1033 #ifdef DBG_NOWRITE
   1034 		ncr_tprint(reqp, "NOWRITE set -- write attempt aborted.");
   1035 		reqp->msgout = MSG_ABORT;
   1036 		SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
   1037 		return (-1);
   1038 #endif /* DBG_NOWRITE */
   1039 		/*
   1040 		 * If this is the first write using DMA, fill
   1041 		 * the bounce buffer.
   1042 		 */
   1043 		if (reqp->xdata_ptr == reqp->xs->data) { /* XXX */
   1044 		    if (reqp->dr_flag & DRIVER_BOUNCING)
   1045 			bcopy(reqp->xdata_ptr, reqp->bounceb, reqp->xdata_len);
   1046 		}
   1047 
   1048 	   case PH_DATAIN:
   1049 		if (reqp->xdata_len <= 0) {
   1050 			/*
   1051 			 * Target keeps requesting data. Try to get into
   1052 			 * message-out phase by feeding/taking 100 byte.
   1053 			 */
   1054 			ncr_tprint(reqp, "Target requests too much data\n");
   1055 			reqp->msgout = MSG_ABORT;
   1056 			SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
   1057 			reach_msg_out(sc, 100);
   1058 			return (-1);
   1059 		}
   1060 #ifdef REAL_DMA
   1061 		if (reqp->dr_flag & DRIVER_DMAOK) {
   1062 			int poll = REAL_DMA_POLL|(reqp->dr_flag & DRIVER_NOINT);
   1063 			transfer_dma(reqp, phase, poll);
   1064 			if (!poll)
   1065 				return (0);
   1066 		}
   1067 		else
   1068 #endif
   1069 		{
   1070 			PID("info_transf3");
   1071 			len = reqp->xdata_len;
   1072 #ifdef USE_PDMA
   1073 			if (transfer_pdma(&phase, reqp->xdata_ptr, &len) == 0)
   1074 				return (0);
   1075 #else
   1076 			transfer_pio(&phase, reqp->xdata_ptr, &len, 0);
   1077 #endif
   1078 			reqp->xdata_ptr += reqp->xdata_len - len;
   1079 			reqp->xdata_len  = len;
   1080 		}
   1081 		return (-1);
   1082 	   case PH_MSGIN:
   1083 		/*
   1084 		 * We only expect single byte messages here.
   1085 		 */
   1086 		len = 1;
   1087 		transfer_pio(&phase, &tmp, &len, 1);
   1088 		reqp->message = tmp;
   1089 		return (handle_message(reqp, tmp));
   1090 	   case PH_MSGOUT:
   1091 		len = 1;
   1092 		transfer_pio(&phase, &reqp->msgout, &len, 0);
   1093 		if (reqp->msgout == MSG_ABORT) {
   1094 			busy     &= ~(1 << reqp->targ_id);
   1095 			connected = NULL;
   1096 			if (!reqp->xs->error)
   1097 				reqp->xs->error = XS_DRIVER_STUFFUP;
   1098 			finish_req(reqp);
   1099 			PID("info_transf4");
   1100 			return (0);
   1101 		}
   1102 		reqp->msgout = MSG_NOOP;
   1103 		return (-1);
   1104 	   case PH_CMD :
   1105 		len = command_size(reqp->xcmd.opcode);
   1106 		transfer_pio(&phase, (u_char *)&reqp->xcmd, &len, 0);
   1107 		PID("info_transf5");
   1108 		return (-1);
   1109 	   case PH_STATUS:
   1110 		len = 1;
   1111 		transfer_pio(&phase, &tmp, &len, 0);
   1112 		reqp->status = tmp;
   1113 		PID("info_transf6");
   1114 		return (-1);
   1115 	   default :
   1116 		ncr_tprint(reqp, "Unknown phase\n");
   1117 	}
   1118 	PID("info_transf7");
   1119 	return (-1);
   1120 }
   1121 
   1122 /*
   1123  * Handle the message 'msg' send to us by the target.
   1124  * Return values:
   1125  *	 0 : The current command has completed.
   1126  *	-1 : Get on to the next phase.
   1127  */
   1128 static int
   1129 handle_message(reqp, msg)
   1130 SC_REQ	*reqp;
   1131 u_int	msg;
   1132 {
   1133 	int	sps;
   1134 	SC_REQ	*prev, *req;
   1135 
   1136 	PID("hmessage1");
   1137 	switch (msg) {
   1138 		/*
   1139 		 * Linking lets us reduce the time required to get
   1140 		 * the next command to the device, skipping the arbitration
   1141 		 * and selection time. In the current implementation,
   1142 		 * we merely have to start the next command pointed
   1143 		 * to by 'next_link'.
   1144 		 */
   1145 		case MSG_LINK_CMD_COMPLETE:
   1146 		case MSG_LINK_CMD_COMPLETEF:
   1147 			if (reqp->link == NULL) {
   1148 				ncr_tprint(reqp, "No link for linked command");
   1149 				nack_message(reqp, MSG_ABORT);
   1150 				PID("hmessage2");
   1151 				return (-1);
   1152 			}
   1153 			ack_message();
   1154 			if (!(reqp->dr_flag & DRIVER_AUTOSEN)) {
   1155 				reqp->xs->resid = reqp->xdata_len;
   1156 				reqp->xs->error = 0;
   1157 			}
   1158 
   1159 #ifdef AUTO_SENSE
   1160 			if (check_autosense(reqp, 1) == -1)
   1161 				return (-1);
   1162 #endif /* AUTO_SENSE */
   1163 
   1164 #ifdef DBG_REQ
   1165 			if (dbg_target_mask & (1 << reqp->targ_id))
   1166 				show_request(reqp->link, "LINK");
   1167 #endif
   1168 			connected = reqp->link;
   1169 
   1170 			/*
   1171 			 * Unlink the 'linked' request from the issue_q
   1172 			 */
   1173 			sps  = splbio();
   1174 			prev = NULL;
   1175 			req  = issue_q;
   1176 			for (; req != NULL; prev = req, req = req->next) {
   1177 				if (req == connected)
   1178 					break;
   1179 			}
   1180 			if (req == NULL)
   1181 				panic("Inconsistent issue_q");
   1182 			if (prev == NULL)
   1183 				issue_q = req->next;
   1184 			else prev->next = req->next;
   1185 			req->next = NULL;
   1186 			splx(sps);
   1187 
   1188 			finish_req(reqp);
   1189 			PID("hmessage3");
   1190 			return (-1);
   1191 		case MSG_ABORT:
   1192 		case MSG_CMDCOMPLETE:
   1193 			ack_message();
   1194 			connected = NULL;
   1195 			busy     &= ~(1 << reqp->targ_id);
   1196 			if (!(reqp->dr_flag & DRIVER_AUTOSEN)) {
   1197 				reqp->xs->resid = reqp->xdata_len;
   1198 				reqp->xs->error = 0;
   1199 			}
   1200 
   1201 #ifdef AUTO_SENSE
   1202 			if (check_autosense(reqp, 0) == -1) {
   1203 				PID("hmessage4");
   1204 				return (0);
   1205 			}
   1206 #endif /* AUTO_SENSE */
   1207 
   1208 			finish_req(reqp);
   1209 			PID("hmessage5");
   1210 			return (0);
   1211 		case MSG_MESSAGE_REJECT:
   1212 			ack_message();
   1213 			PID("hmessage6");
   1214 			return (-1);
   1215 		case MSG_DISCONNECT:
   1216 			ack_message();
   1217 #ifdef DBG_REQ
   1218 			if (dbg_target_mask & (1 << reqp->targ_id))
   1219 				show_request(reqp, "DISCON");
   1220 #endif
   1221 			sps = splbio();
   1222 			connected  = NULL;
   1223 			reqp->next = discon_q;
   1224 			discon_q   = reqp;
   1225 			splx(sps);
   1226 			PID("hmessage7");
   1227 			return (0);
   1228 		case MSG_SAVEDATAPOINTER:
   1229 		case MSG_RESTOREPOINTERS:
   1230 			/*
   1231 			 * We save pointers implicitely at disconnect.
   1232 			 * So we can ignore these messages.
   1233 			 */
   1234 			ack_message();
   1235 			PID("hmessage8");
   1236 			return (-1);
   1237 		case MSG_EXTENDED:
   1238 			nack_message(reqp, MSG_MESSAGE_REJECT);
   1239 			PID("hmessage9");
   1240 			return (-1);
   1241 		default:
   1242 			if ((msg & 0x80) && !(msg & 0x18)) {	/* IDENTIFY */
   1243 				PID("hmessage10");
   1244 				ack_message();
   1245 				return (0);
   1246 			} else {
   1247 				ncr_tprint(reqp,
   1248 					   "Unknown message %x.  Rejecting.\n",
   1249 					   msg);
   1250 				nack_message(reqp, MSG_MESSAGE_REJECT);
   1251 			}
   1252 			return (-1);
   1253 	}
   1254 	PID("hmessage11");
   1255 	return (-1);
   1256 }
   1257 
   1258 /*
   1259  * Handle reselection. If a valid reconnection occurs, connected
   1260  * points at the reconnected command. The command is removed from the
   1261  * disconnected queue.
   1262  */
   1263 static void
   1264 reselect(sc)
   1265 struct ncr_softc *sc;
   1266 {
   1267 	u_char	phase;
   1268 	u_long	len;
   1269 	u_char	msg;
   1270 	u_char	target_mask;
   1271 	int	abort = 0;
   1272 	SC_REQ	*tmp, *prev;
   1273 
   1274 	PID("reselect1");
   1275 	target_mask = GET_5380_REG(NCR5380_DATA) & ~SC_HOST_ID;
   1276 
   1277 	/*
   1278 	 * At this point, we have detected that our SCSI-id is on the bus,
   1279 	 * SEL is true and BSY was false for at least one bus settle
   1280 	 * delay (400 ns.).
   1281 	 * We must assert BSY ourselves, until the target drops the SEL signal.
   1282 	 * The SCSI-spec specifies no maximum time for this, so we have to
   1283 	 * choose something long enough to suit all targets.
   1284 	 */
   1285 	SET_5380_REG(NCR5380_ICOM, SC_A_BSY);
   1286 	len = 250000;
   1287 	while ((GET_5380_REG(NCR5380_IDSTAT) & SC_S_SEL) && (len > 0)) {
   1288 		delay(1);
   1289 		len--;
   1290 	}
   1291 	if (GET_5380_REG(NCR5380_IDSTAT) & SC_S_SEL) {
   1292 		/* Damn SEL isn't dropping */
   1293 		scsi_reset_verbose(sc, "Target won't drop SEL during Reselect");
   1294 		return;
   1295 	}
   1296 
   1297 	SET_5380_REG(NCR5380_ICOM, 0);
   1298 
   1299 	/*
   1300 	 * Check if the reselection is still valid. Check twice because
   1301 	 * of possible line glitches - cheaper than delay(1) and we need
   1302 	 * only a few nanoseconds.
   1303 	 */
   1304 	if (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY)) {
   1305 	    if (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY)) {
   1306 		ncr_aprint(sc, "Stepped into the reselection timeout\n");
   1307 		return;
   1308 	    }
   1309 	}
   1310 
   1311 	/*
   1312 	 * Get the expected identify message.
   1313 	 */
   1314 	phase = PH_MSGIN;
   1315 	len   = 1;
   1316 	transfer_pio(&phase, &msg, &len, 0);
   1317 	if (len || !MSG_ISIDENTIFY(msg)) {
   1318 		ncr_aprint(sc, "Expecting IDENTIFY, got 0x%x\n", msg);
   1319 		abort = 1;
   1320 		tmp = NULL;
   1321 	}
   1322 	else {
   1323 	    /*
   1324 	     * Find the command reconnecting
   1325 	     */
   1326 	    for (tmp = discon_q, prev = NULL; tmp; prev = tmp, tmp = tmp->next){
   1327 		if (target_mask == (1 << tmp->targ_id)) {
   1328 			if (prev)
   1329 				prev->next = tmp->next;
   1330 			else discon_q = tmp->next;
   1331 			tmp->next = NULL;
   1332 			break;
   1333 		}
   1334 	    }
   1335 	    if (tmp == NULL) {
   1336 		ncr_aprint(sc, "No disconnected job for targetmask %x\n",
   1337 								target_mask);
   1338 		abort = 1;
   1339 	    }
   1340 	}
   1341 	if (abort) {
   1342 		msg   = MSG_ABORT;
   1343 		len   = 1;
   1344 		phase = PH_MSGOUT;
   1345 
   1346 		SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
   1347 		if (transfer_pio(&phase, &msg, &len, 0) || len)
   1348 			scsi_reset_verbose(sc, "Failure to abort reselection");
   1349 	}
   1350 	else {
   1351 		connected = tmp;
   1352 #ifdef DBG_REQ
   1353 		if (dbg_target_mask & (1 << tmp->targ_id))
   1354 			show_request(tmp, "RECON");
   1355 #endif
   1356 	}
   1357 	PID("reselect2");
   1358 }
   1359 
   1360 /*
   1361  * Transfer data in a given phase using programmed I/O.
   1362  * Returns -1 when a different phase is entered without transferring the
   1363  * maximum number of bytes, 0 if all bytes transferred or exit is in the same
   1364  * phase.
   1365  */
   1366 static int
   1367 transfer_pio(phase, data, len, dont_drop_ack)
   1368 u_char	*phase;
   1369 u_char	*data;
   1370 u_long	*len;
   1371 int	dont_drop_ack;
   1372 {
   1373 	u_int	cnt = *len;
   1374 	u_char	ph  = *phase;
   1375 	u_char	tmp, new_icom;
   1376 
   1377 	DBG_PIOPRINT ("SCSI: transfer_pio start: phase: %d, len: %d\n", ph,cnt);
   1378 	PID("tpio1");
   1379 	SET_5380_REG(NCR5380_TCOM, ph);
   1380 	do {
   1381 	    if (!wait_req_true()) {
   1382 		DBG_PIOPRINT ("SCSI: transfer_pio: missing REQ\n", 0, 0);
   1383 		break;
   1384 	    }
   1385 	    if (((GET_5380_REG(NCR5380_IDSTAT) >> 2) & 7) != ph) {
   1386 		DBG_PIOPRINT ("SCSI: transfer_pio: phase mismatch\n", 0, 0);
   1387 		break;
   1388 	    }
   1389 	    if (PH_IN(ph)) {
   1390 		*data++ = GET_5380_REG(NCR5380_DATA);
   1391 		SET_5380_REG(NCR5380_ICOM, SC_A_ACK);
   1392 		if ((cnt == 1) && dont_drop_ack)
   1393 			new_icom = SC_A_ACK;
   1394 		else new_icom = 0;
   1395 	    }
   1396 	    else {
   1397 		SET_5380_REG(NCR5380_DATA, *data++);
   1398 
   1399 		/*
   1400 		 * The SCSI-standard suggests that in the 'MESSAGE OUT' phase,
   1401 		 * the initiator should drop ATN on the last byte of the
   1402 		 * message phase after REQ has been asserted for the handshake
   1403 		 * but before the initiator raises ACK.
   1404 		 */
   1405 		if (!( (ph == PH_MSGOUT) && (cnt > 1) )) {
   1406 			SET_5380_REG(NCR5380_ICOM, SC_ADTB);
   1407 			SET_5380_REG(NCR5380_ICOM, SC_ADTB | SC_A_ACK);
   1408 			new_icom = 0;
   1409 		}
   1410 		else {
   1411 			SET_5380_REG(NCR5380_ICOM, SC_ADTB | SC_A_ATN);
   1412 			SET_5380_REG(NCR5380_ICOM, SC_ADTB|SC_A_ATN|SC_A_ACK);
   1413 			new_icom = SC_A_ATN;
   1414 		}
   1415 	    }
   1416 	    if (!wait_req_false()) {
   1417 		DBG_PIOPRINT ("SCSI: transfer_pio - REQ not dropping\n", 0, 0);
   1418 		break;
   1419 	    }
   1420 	    SET_5380_REG(NCR5380_ICOM, new_icom);
   1421 
   1422 	} while (--cnt);
   1423 
   1424 	if ((tmp = GET_5380_REG(NCR5380_IDSTAT)) & SC_S_REQ)
   1425 		*phase = (tmp >> 2) & 7;
   1426 	else *phase = NR_PHASE;
   1427 	*len = cnt;
   1428 	DBG_PIOPRINT ("SCSI: transfer_pio done: phase: %d, len: %d\n",
   1429 								*phase, cnt);
   1430 	PID("tpio2");
   1431 	if (!cnt || (*phase == ph))
   1432 		return (0);
   1433 	return (-1);
   1434 }
   1435 
   1436 #ifdef REAL_DMA
   1437 /*
   1438  * Start a DMA-transfer on the device using the current pointers.
   1439  * If 'poll' is true, the function busy-waits until DMA has completed.
   1440  */
   1441 static void
   1442 transfer_dma(reqp, phase, poll)
   1443 SC_REQ	*reqp;
   1444 u_int	phase;
   1445 int	poll;
   1446 {
   1447 	int	dma_done;
   1448 	u_char	mbase = 0;
   1449 	int	sps;
   1450 
   1451 again:
   1452 	PID("tdma1");
   1453 
   1454 	/*
   1455 	 * We should be in phase, otherwise we are not allowed to
   1456 	 * drive the bus.
   1457 	 */
   1458 	SET_5380_REG(NCR5380_TCOM, phase);
   1459 
   1460 	/*
   1461 	 * Defer interrupts until DMA is fully running.
   1462 	 */
   1463 	sps = splbio();
   1464 
   1465 	/*
   1466 	 * Clear pending interrupts and parity errors.
   1467 	 */
   1468 	scsi_clr_ipend();
   1469 
   1470 	if (!poll) {
   1471 		/*
   1472 		 * Enable SCSI interrupts and set IN_DMA flag, set 'mbase'
   1473 		 * to the interrupts we want enabled.
   1474 		 */
   1475 		scsi_ienable();
   1476 		reqp->dr_flag |= DRIVER_IN_DMA;
   1477 		mbase = SC_E_EOPI | SC_MON_BSY;
   1478 	}
   1479 	else scsi_idisable();
   1480 	mbase |=  IMODE_BASE | SC_M_DMA;
   1481 	scsi_dma_setup(reqp, phase, mbase);
   1482 
   1483 	splx(sps);
   1484 
   1485 	if (poll) {
   1486 		/*
   1487 		 * On polled-dma transfers, we wait here until the
   1488 		 * 'end-of-dma' condition occurs.
   1489 		 */
   1490 		poll_edma(reqp);
   1491 		if (!(dma_done = dma_ready()))
   1492 			goto again;
   1493 	}
   1494 	PID("tdma2");
   1495 }
   1496 
   1497 /*
   1498  * Check results of a DMA data-transfer.
   1499  */
   1500 static int
   1501 dma_ready()
   1502 {
   1503 	SC_REQ	*reqp = connected;
   1504 	int	dmstat, is_edma;
   1505 	long	bytes_left, bytes_done;
   1506 
   1507 	is_edma = get_dma_result(reqp, &bytes_left);
   1508 	dmstat  = GET_5380_REG(NCR5380_DMSTAT);
   1509 
   1510 	/*
   1511 	 * Check if the call is sensible and not caused by any spurious
   1512 	 * interrupt.
   1513 	 */
   1514 	if (!is_edma && !(dmstat & (SC_END_DMA|SC_BSY_ERR))
   1515 		     && (dmstat & SC_PHS_MTCH) ) {
   1516 		ncr_tprint(reqp, "dma_ready: spurious call "
   1517 				 "(dm:%x,last_hit: %s)\n",
   1518 #ifdef DBG_PID
   1519 					dmstat, last_hit[DBG_PID-1]);
   1520 #else
   1521 					dmstat, "unknown");
   1522 #endif
   1523 		return (0);
   1524 	}
   1525 
   1526 	/*
   1527 	 * Clear all (pending) interrupts.
   1528 	 */
   1529 	scsi_clr_ipend();
   1530 
   1531 	/*
   1532 	 * Update various transfer-pointers/lengths
   1533 	 */
   1534 	bytes_done = reqp->dm_cur->dm_count - bytes_left;
   1535 
   1536 	if ((reqp->dr_flag & DRIVER_BOUNCING) && (PH_IN(reqp->phase))) {
   1537 		/*
   1538 		 * Copy the bytes read until now from the bounce buffer
   1539 		 * to the 'real' destination. Flush the data-cache
   1540 		 * before copying.
   1541 		 */
   1542 		PCIA();
   1543 		bcopy(reqp->bouncerp, reqp->xdata_ptr, bytes_done);
   1544 		reqp->bouncerp += bytes_done;
   1545 	}
   1546 
   1547 	reqp->xdata_ptr  = &reqp->xdata_ptr[bytes_done];	/* XXX */
   1548 	reqp->xdata_len -= bytes_done;				/* XXX */
   1549 	if ((reqp->dm_cur->dm_count -= bytes_done) == 0)
   1550 		reqp->dm_cur++;
   1551 	else reqp->dm_cur->dm_addr += bytes_done;
   1552 
   1553 	if (PH_IN(reqp->phase) && (dmstat & SC_PAR_ERR)) {
   1554 		if (!(ncr5380_no_parchk & (1 << reqp->targ_id))) {
   1555 			ncr_tprint(reqp, "parity error in data-phase\n");
   1556 			reqp->xs->error = XS_TIMEOUT;
   1557 		}
   1558 	}
   1559 
   1560 	/*
   1561 	 * DMA mode should always be reset even when we will continue with the
   1562 	 * next chain. It is also essential to clear the MON_BUSY because
   1563 	 * when LOST_BUSY is unexpectedly set, we will not be able to drive
   1564 	 * the bus....
   1565 	 */
   1566 	SET_5380_REG(NCR5380_MODE, IMODE_BASE);
   1567 
   1568 
   1569 	if ((dmstat & SC_BSY_ERR) || !(dmstat & SC_PHS_MTCH)
   1570 		 || (reqp->dm_cur > reqp->dm_last) || (reqp->xs->error)) {
   1571 
   1572 		/*
   1573 		 * Tell interrupt functions DMA mode has ended.
   1574 		 */
   1575 		reqp->dr_flag &= ~DRIVER_IN_DMA;
   1576 
   1577 		/*
   1578 		 * Clear mode and icom
   1579 		 */
   1580 		SET_5380_REG(NCR5380_MODE, IMODE_BASE);
   1581 		SET_5380_REG(NCR5380_ICOM, 0);
   1582 
   1583 		if (dmstat & SC_BSY_ERR) {
   1584 			if (!reqp->xs->error)
   1585 				reqp->xs->error = XS_TIMEOUT;
   1586 			finish_req(reqp);
   1587 			PID("dma_ready1");
   1588 			return (1);
   1589 		}
   1590 
   1591 		if (reqp->xs->error != 0) {
   1592 ncr_tprint(reqp, "dma-ready: code = %d\n", reqp->xs->error); /* LWP */
   1593 			reqp->msgout = MSG_ABORT;
   1594 			SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
   1595 		}
   1596 		PID("dma_ready2");
   1597 		return (1);
   1598 	}
   1599 	return (0);
   1600 }
   1601 #endif /* REAL_DMA */
   1602 
   1603 static int
   1604 check_autosense(reqp, linked)
   1605 SC_REQ	*reqp;
   1606 int	linked;
   1607 {
   1608 	int	sps;
   1609 
   1610 	/*
   1611 	 * If this is the driver's Link Check for this target, ignore
   1612 	 * the results of the command.  All we care about is whether we
   1613 	 * got here from a LINK_CMD_COMPLETE or CMD_COMPLETE message.
   1614 	 */
   1615 	PID("linkcheck");
   1616 	if (reqp->dr_flag & DRIVER_LINKCHK) {
   1617 		if (linked)
   1618 			ncr_will_link |= 1<<reqp->targ_id;
   1619 		else ncr_tprint(reqp, "Does not support linked commands\n");
   1620 		return (0);
   1621 	}
   1622 	/*
   1623 	 * If we not executing an auto-sense and the status code
   1624 	 * is request-sense, we automatically issue a request
   1625 	 * sense command.
   1626 	 */
   1627 	PID("cautos1");
   1628 	if (!(reqp->dr_flag & DRIVER_AUTOSEN)) {
   1629 		switch (reqp->status & SCSMASK) {
   1630 		    case SCSCHKC:
   1631 			bcopy(sense_cmd, &reqp->xcmd, sizeof(sense_cmd));
   1632 			reqp->xdata_ptr = (u_char *)&reqp->xs->sense;
   1633 			reqp->xdata_len = sizeof(reqp->xs->sense);
   1634 			reqp->dr_flag  |= DRIVER_AUTOSEN;
   1635 			reqp->dr_flag  &= ~DRIVER_DMAOK;
   1636 			if (!linked) {
   1637 				sps = splbio();
   1638 				reqp->next = issue_q;
   1639 				issue_q    = reqp;
   1640 				splx(sps);
   1641 			}
   1642 			else reqp->xcmd.bytes[sizeof(sense_cmd)-2] |= 1;
   1643 
   1644 #ifdef DBG_REQ
   1645 			bzero(reqp->xdata_ptr, reqp->xdata_len);
   1646 			if (dbg_target_mask & (1 << reqp->targ_id))
   1647 				show_request(reqp, "AUTO-SENSE");
   1648 #endif
   1649 			PID("cautos2");
   1650 			return (-1);
   1651 		    case SCSBUSY:
   1652 			reqp->xs->error = XS_BUSY;
   1653 			return (0);
   1654 		}
   1655 	}
   1656 	else {
   1657 		/*
   1658 		 * An auto-sense has finished
   1659 		 */
   1660 		if ((reqp->status & SCSMASK) != SCSGOOD)
   1661 			reqp->xs->error = XS_DRIVER_STUFFUP; /* SC_E_AUTOSEN; */
   1662 		else reqp->xs->error = XS_SENSE;
   1663 		reqp->status = SCSCHKC;
   1664 	}
   1665 	PID("cautos3");
   1666 	return (0);
   1667 }
   1668 
   1669 static int
   1670 reach_msg_out(sc, len)
   1671 struct ncr_softc *sc;
   1672 u_long		 len;
   1673 {
   1674 	u_char	phase;
   1675 	u_char	data;
   1676 	u_long	n = len;
   1677 
   1678 	ncr_aprint(sc, "Trying to reach Message-out phase\n");
   1679 	if ((phase = GET_5380_REG(NCR5380_IDSTAT)) & SC_S_REQ)
   1680 		phase = (phase >> 2) & 7;
   1681 	else return (-1);
   1682 	ncr_aprint(sc, "Trying to reach Message-out phase, now: %d\n", phase);
   1683 	if (phase == PH_MSGOUT)
   1684 		return (0);
   1685 
   1686 	SET_5380_REG(NCR5380_TCOM, phase);
   1687 
   1688 	do {
   1689 		if (!wait_req_true())
   1690 			break;
   1691 		if (((GET_5380_REG(NCR5380_IDSTAT) >> 2) & 7) != phase)
   1692 			break;
   1693 		if (PH_IN(phase)) {
   1694 			data = GET_5380_REG(NCR5380_DATA);
   1695 			SET_5380_REG(NCR5380_ICOM, SC_A_ACK | SC_A_ATN);
   1696 		}
   1697 		else {
   1698 			SET_5380_REG(NCR5380_DATA, 0);
   1699 			SET_5380_REG(NCR5380_ICOM, SC_ADTB|SC_A_ACK|SC_A_ATN);
   1700 		}
   1701 		if (!wait_req_false())
   1702 			break;
   1703 		SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
   1704 	} while (--n);
   1705 
   1706 	if ((phase = GET_5380_REG(NCR5380_IDSTAT)) & SC_S_REQ) {
   1707 		phase = (phase >> 2) & 7;
   1708 		if (phase == PH_MSGOUT) {
   1709 			ncr_aprint(sc, "Message-out phase reached after "
   1710 					"%ld bytes.\n", len - n);
   1711 			return (0);
   1712 		}
   1713 	}
   1714 	return (-1);
   1715 }
   1716 
   1717 void
   1718 scsi_reset()
   1719 {
   1720 	SC_REQ	*tmp, *next;
   1721 	int	sps;
   1722 
   1723 	PID("scsi_reset1");
   1724 	sps = splbio();
   1725 	SET_5380_REG(NCR5380_ICOM, SC_A_RST);
   1726 	delay(100);
   1727 	SET_5380_REG(NCR5380_ICOM, 0);
   1728 	scsi_clr_ipend();
   1729 
   1730 	/*
   1731 	 * None of the jobs in the discon_q will ever be reconnected,
   1732 	 * notify this to the higher level code.
   1733 	 */
   1734 	for (tmp = discon_q; tmp ;) {
   1735 		next = tmp->next;
   1736 		tmp->next = NULL;
   1737 		tmp->xs->error = XS_TIMEOUT;
   1738 		busy &= ~(1 << tmp->targ_id);
   1739 		finish_req(tmp);
   1740 		tmp = next;
   1741 	}
   1742 	discon_q = NULL;
   1743 
   1744 	/*
   1745 	 * The current job will never finish either.
   1746 	 * The problem is that we can't finish the job because an instance
   1747 	 * of main is running on it. Our best guess is that the job is currently
   1748 	 * doing REAL-DMA. In that case 'dma_ready()' should correctly finish
   1749 	 * the job because it detects BSY-loss.
   1750 	 */
   1751 	if ((tmp = connected) != NULL) {
   1752 		if (tmp->dr_flag & DRIVER_IN_DMA) {
   1753 			tmp->xs->error = XS_DRIVER_STUFFUP;
   1754 #ifdef REAL_DMA
   1755 			dma_ready();
   1756 #endif
   1757 		}
   1758 	}
   1759 	splx(sps);
   1760 	PID("scsi_reset2");
   1761 
   1762 	/*
   1763 	 * Give the attached devices some time to handle the reset. This
   1764 	 * value is arbitrary but should be relatively long.
   1765 	 */
   1766 	delay(100000);
   1767 }
   1768 
   1769 static void
   1770 scsi_reset_verbose(sc, why)
   1771 struct ncr_softc *sc;
   1772 const char	 *why;
   1773 {
   1774 	ncr_aprint(sc, "Resetting SCSI-bus (%s)\n", why);
   1775 
   1776 	scsi_reset();
   1777 }
   1778 
   1779 /*
   1780  * Check validity of the IRQ set by the 5380. If the interrupt is valid,
   1781  * the appropriate action is carried out (reselection or DMA ready) and
   1782  * INTR_RESEL or INTR_DMA is returned. Otherwise a console notice is written
   1783  * and INTR_SPURIOUS is returned.
   1784  */
   1785 static int
   1786 check_intr(sc)
   1787 struct ncr_softc *sc;
   1788 {
   1789 	SC_REQ	*reqp;
   1790 
   1791 	if ((GET_5380_REG(NCR5380_IDSTAT) & (SC_S_SEL|SC_S_IO))
   1792 							==(SC_S_SEL|SC_S_IO))
   1793 		return (INTR_RESEL);
   1794 	else {
   1795 		if ((reqp = connected) && (reqp->dr_flag & DRIVER_IN_DMA)){
   1796 			reqp->dr_flag &= ~DRIVER_IN_DMA;
   1797 			return (INTR_DMA);
   1798 		}
   1799 	}
   1800 	scsi_clr_ipend();
   1801 	printf("-->");
   1802 	scsi_show();
   1803 	ncr_aprint(sc, "Spurious interrupt.\n");
   1804 	return (INTR_SPURIOUS);
   1805 }
   1806 
   1807 #ifdef REAL_DMA
   1808 /*
   1809  * Check if DMA can be used for this request. This function also builds
   1810  * the dma-chain.
   1811  */
   1812 static int
   1813 scsi_dmaok(reqp)
   1814 SC_REQ	*reqp;
   1815 {
   1816 	u_long			phy_buf;
   1817 	u_long			phy_len;
   1818 	void			*req_addr;
   1819 	u_long			req_len;
   1820 	struct dma_chain	*dm;
   1821 
   1822 	/*
   1823 	 * Initialize locals and requests' DMA-chain.
   1824 	 */
   1825 	req_len        = reqp->xdata_len;
   1826 	req_addr       = (void*)reqp->xdata_ptr;
   1827 	dm             = reqp->dm_cur = reqp->dm_last = reqp->dm_chain;
   1828 	dm->dm_count   = dm->dm_addr = 0;
   1829 	reqp->dr_flag &= ~DRIVER_BOUNCING;
   1830 
   1831 	/*
   1832 	 * Do not accept zero length DMA.
   1833 	 */
   1834 	if (req_len == 0)
   1835 		return (0);
   1836 
   1837 	/*
   1838 	 * LWP: I think that this restriction is not strictly nessecary.
   1839 	 */
   1840 	if ((req_len & 0x1) || ((u_int)req_addr & 0x3))
   1841 		return (0);
   1842 
   1843 	/*
   1844 	 * Build the DMA-chain.
   1845 	 */
   1846 	dm->dm_addr = phy_buf = kvtop(req_addr);
   1847 	while (req_len) {
   1848 		if (req_len < (phy_len = NBPG - ((u_long)req_addr & PGOFSET)))
   1849 			phy_len = req_len;
   1850 
   1851 		req_addr     += phy_len;
   1852 		req_len      -= phy_len;
   1853 		dm->dm_count += phy_len;
   1854 
   1855 		if (req_len) {
   1856 			u_long	tmp = kvtop(req_addr);
   1857 
   1858 			if ((phy_buf + phy_len) != tmp) {
   1859 			    if (wrong_dma_range(reqp, dm)) {
   1860 				if (reqp->dr_flag & DRIVER_BOUNCING)
   1861 					goto bounceit;
   1862 				return (0);
   1863 			    }
   1864 
   1865 			    if (++dm >= &reqp->dm_chain[MAXDMAIO]) {
   1866 				ncr_tprint(reqp,"dmaok: DMA chain too long!\n");
   1867 				return (0);
   1868 			    }
   1869 			    dm->dm_count = 0;
   1870 			    dm->dm_addr  = tmp;
   1871 			}
   1872 			phy_buf = tmp;
   1873 		}
   1874 	}
   1875         if (wrong_dma_range(reqp, dm)) {
   1876 		if (reqp->dr_flag & DRIVER_BOUNCING)
   1877 			goto bounceit;
   1878 		return (0);
   1879 	}
   1880 	reqp->dm_last = dm;
   1881 	return (1);
   1882 
   1883 bounceit:
   1884 	if ((reqp->bounceb = alloc_bounceb(reqp->xdata_len)) == NULL) {
   1885 		/*
   1886 		 * If we can't get a bounce buffer, forget DMA
   1887 		 */
   1888 		reqp->dr_flag &= ~DRIVER_BOUNCING;
   1889 		return(0);
   1890 	}
   1891 	/*
   1892 	 * Initialize a single DMA-range containing the bounced request
   1893 	 */
   1894 	dm = reqp->dm_cur = reqp->dm_last = reqp->dm_chain;
   1895 	dm->dm_addr    = kvtop(reqp->bounceb);
   1896 	dm->dm_count   = reqp->xdata_len;
   1897 	reqp->bouncerp = reqp->bounceb;
   1898 
   1899 	return (1);
   1900 }
   1901 #endif /* REAL_DMA */
   1902 
   1903 static void
   1904 run_main(sc)
   1905 struct ncr_softc *sc;
   1906 {
   1907 	int	sps = splbio();
   1908 
   1909 	if (!main_running) {
   1910 		/*
   1911 		 * If shared resources are required, claim them
   1912 		 * before entering 'scsi_main'. If we can't get them
   1913 		 * now, assume 'run_main' will be called when the resource
   1914 		 * becomes available.
   1915 		 */
   1916 		if (!claimed_dma()) {
   1917 			splx(sps);
   1918 			return;
   1919 		}
   1920 		main_running = 1;
   1921 		splx(sps);
   1922 		scsi_main(sc);
   1923 	}
   1924 	else splx(sps);
   1925 }
   1926 
   1927 /*
   1928  * Prefix message with full target info.
   1929  */
   1930 static void
   1931 ncr_tprint(SC_REQ *reqp, char *fmt, ...)
   1932 {
   1933 	va_list	ap;
   1934 
   1935 	va_start(ap, fmt);
   1936 	sc_print_addr(reqp->xs->sc_link);
   1937 	printf("%:", fmt, ap);
   1938 	va_end(ap);
   1939 }
   1940 
   1941 /*
   1942  * Prefix message with adapter info.
   1943  */
   1944 static void
   1945 ncr_aprint(struct ncr_softc *sc, char *fmt, ...)
   1946 {
   1947 	va_list	ap;
   1948 
   1949 	va_start(ap, fmt);
   1950 	printf("%s: %:", sc->sc_dev.dv_xname, fmt, ap);
   1951 	va_end(ap);
   1952 }
   1953 /****************************************************************************
   1954  *		Start Debugging Functions				    *
   1955  ****************************************************************************/
   1956 static void
   1957 show_data_sense(xs)
   1958 struct scsi_xfer	*xs;
   1959 {
   1960 	u_char	*p1, *p2;
   1961 	int	i;
   1962 	int	sz;
   1963 
   1964 	p1 = (u_char *) xs->cmd;
   1965 	p2 = (u_char *)&xs->sense;
   1966 	if(*p2 == 0)
   1967 		return;	/* No(n)sense */
   1968 	printf("cmd[%d,%d]: ", xs->cmdlen, sz = command_size(*p1));
   1969 	for (i = 0; i < sz; i++)
   1970 		printf("%x ", p1[i]);
   1971 	printf("\nsense: ");
   1972 	for (i = 0; i < sizeof(xs->sense); i++)
   1973 		printf("%x ", p2[i]);
   1974 	printf("\n");
   1975 }
   1976 
   1977 static void
   1978 show_request(reqp, qtxt)
   1979 SC_REQ	*reqp;
   1980 char	*qtxt;
   1981 {
   1982 	printf("REQ-%s: %d %p[%ld] cmd[0]=%x S=%x M=%x R=%x resid=%d dr_flag=%x %s\n",
   1983 			qtxt, reqp->targ_id, reqp->xdata_ptr, reqp->xdata_len,
   1984 			reqp->xcmd.opcode, reqp->status, reqp->message,
   1985 			reqp->xs->error, reqp->xs->resid, reqp->dr_flag,
   1986 			reqp->link ? "L":"");
   1987 	if (reqp->status == SCSCHKC)
   1988 		show_data_sense(reqp->xs);
   1989 }
   1990 
   1991 static char *sig_names[] = {
   1992 	"PAR", "SEL", "I/O", "C/D", "MSG", "REQ", "BSY", "RST",
   1993 	"ACK", "ATN", "LBSY", "PMATCH", "IRQ", "EPAR", "DREQ", "EDMA"
   1994 };
   1995 
   1996 static void
   1997 show_signals(dmstat, idstat)
   1998 u_char	dmstat, idstat;
   1999 {
   2000 	u_short	tmp, mask;
   2001 	int	j, need_pipe;
   2002 
   2003 	tmp = idstat | ((dmstat & 3) << 8);
   2004 	printf("Bus signals (%02x/%02x): ", idstat, dmstat & 3);
   2005 	for (mask = 1, j = need_pipe = 0; mask <= tmp; mask <<= 1, j++) {
   2006 		if (tmp & mask)
   2007 			printf("%s%s", need_pipe++ ? "|" : "", sig_names[j]);
   2008 	}
   2009 	printf("\nDma status (%02x): ", dmstat);
   2010 	for (mask = 4, j = 10, need_pipe = 0; mask <= dmstat; mask <<= 1, j++) {
   2011 		if (dmstat & mask)
   2012 			printf("%s%s", need_pipe++ ? "|" : "", sig_names[j]);
   2013 	}
   2014 	printf("\n");
   2015 }
   2016 
   2017 void
   2018 scsi_show()
   2019 {
   2020 	SC_REQ	*tmp;
   2021 	int	sps = splhigh();
   2022 	u_char	idstat, dmstat;
   2023 #ifdef	DBG_PID
   2024 	int	i;
   2025 #endif
   2026 
   2027 	printf("scsi_show: scsi_main is%s running\n",
   2028 		main_running ? "" : " not");
   2029 	for (tmp = issue_q; tmp; tmp = tmp->next)
   2030 		show_request(tmp, "ISSUED");
   2031 	for (tmp = discon_q; tmp; tmp = tmp->next)
   2032 		show_request(tmp, "DISCONNECTED");
   2033 	if (connected)
   2034 		show_request(connected, "CONNECTED");
   2035 	idstat = GET_5380_REG(NCR5380_IDSTAT);
   2036 	dmstat = GET_5380_REG(NCR5380_DMSTAT);
   2037 	show_signals(dmstat, idstat);
   2038 	if (connected)
   2039 		printf("phase = %d, ", connected->phase);
   2040 	printf("busy:%x, spl:%04x\n", busy, sps);
   2041 #ifdef	DBG_PID
   2042 	for (i=0; i<DBG_PID; i++)
   2043 		printf("\t%d\t%s\n", i, last_hit[i]);
   2044 #endif
   2045 
   2046 	splx(sps);
   2047 }
   2048