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