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