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