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