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