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