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