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