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