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