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