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