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