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