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