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