Home | History | Annotate | Line # | Download | only in isa
seagate.c revision 1.15
      1 /*
      2  * ST01/02, Future Domain TMC-885, TMC-950 SCSI driver
      3  *
      4  * Copyright 1994, Charles Hannum (mycroft (at) ai.mit.edu)
      5  * Copyright 1994, Kent Palmkvist (kentp (at) isy.liu.se)
      6  * Copyright 1994, Robert Knier (rknier (at) qgraph.com)
      7  * Copyright 1992, 1994 Drew Eckhardt (drew (at) colorado.edu)
      8  * Copyright 1994, Julian Elischer (julian (at) tfs.com)
      9  *
     10  * Others that has contributed by example code is
     11  * 		Glen Overby (overby (at) cray.com)
     12  *		Tatu Yllnen
     13  *		Brian E Litzinger
     14  *
     15  * Redistribution and use in source and binary forms, with or without
     16  * modification, are permitted provided that the following conditions
     17  * are met:
     18  * 1. Redistributions of source code must retain the above copyright
     19  *    notice, this list of conditions and the following disclaimer.
     20  * 2. Redistributions in binary form must reproduce the above copyright
     21  *    notice, this list of conditions and the following disclaimer in the
     22  *    documentation and/or other materials provided with the distribution.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE DEVELOPERS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  */
     36 
     37 /*
     38  * kentp  940307 alpha version based on newscsi-03 version of Julians SCSI-code
     39  * kentp  940314 Added possibility to not use messages
     40  * rknier 940331 Added fast transfer code
     41  * rknier 940407 Added assembler coded data transfers
     42  */
     43 
     44 /*
     45  * What should really be done:
     46  *
     47  * Add missing tests for timeouts
     48  * Restructure interrupt enable/disable code (runs to long with int disabled)
     49  * Find bug? giving problem with tape status
     50  * Add code to handle Future Domain 840, 841, 880 and 881
     51  * adjust timeouts (startup is very slow)
     52  * add code to use tagged commands in SCSI2
     53  * Add code to handle slow devices better (sleep if device not disconnecting)
     54  * Fix unnecessary interrupts
     55  */
     56 
     57 /*
     58  * Note to users trying to share a disk between DOS and unix:
     59  * The ST01/02 is a translating host-adapter. It is not giving DOS
     60  * the same number of heads/tracks/sectors as specified by the disk.
     61  * It is therefore important to look at what numbers DOS thinks the
     62  * disk has. Use these to disklabel your disk in an appropriate manner
     63  */
     64 
     65 #include <sys/types.h>
     66 #include <sys/param.h>
     67 #include <sys/systm.h>
     68 #include <sys/kernel.h>
     69 #include <sys/errno.h>
     70 #include <sys/ioctl.h>
     71 #include <sys/device.h>
     72 #include <sys/buf.h>
     73 #include <sys/proc.h>
     74 #include <sys/user.h>
     75 #include <sys/queue.h>
     76 #include <sys/malloc.h>
     77 
     78 #include <machine/pio.h>
     79 
     80 #include <scsi/scsi_all.h>
     81 #include <scsi/scsi_message.h>
     82 #include <scsi/scsiconf.h>
     83 
     84 #include <dev/isa/isareg.h>
     85 #include <dev/isa/isavar.h>
     86 #include <i386/isa/isa_machdep.h>	/* XXX USES ISA HOLE DIRECTLY */
     87 
     88 #define	SEA_SCB_MAX	32	/* allow maximally 8 scsi control blocks */
     89 #define SCB_TABLE_SIZE	8	/* start with 8 scb entries in table */
     90 #define BLOCK_SIZE	512	/* size of READ/WRITE areas on SCSI card */
     91 
     92 /*
     93  * defining SEA_BLINDTRANSFER will make DATA IN and DATA OUT to be done with
     94  * blind transfers, i.e. no check is done for scsi phase changes. This will
     95  * result in data loss if the scsi device does not send its data using
     96  * BLOCK_SIZE bytes at a time.
     97  * If SEA_BLINDTRANSFER defined and SEA_ASSEMBLER also defined will result in
     98  * the use of blind transfers coded in assembler. SEA_ASSEMBLER is no good
     99  * without SEA_BLINDTRANSFER defined.
    100  */
    101 #define	SEA_BLINDTRANSFER	/* do blind transfers */
    102 #define	SEA_ASSEMBLER		/* Use assembly code for fast transfers */
    103 
    104 /*
    105  * defining SEA_NOMSGS causes messages not to be used (thereby disabling
    106  * disconnects)
    107  */
    108 #undef	SEA_NOMSGS
    109 
    110 /*
    111  * defining SEA_NODATAOUT makes dataout phase being aborted
    112  */
    113 #undef	SEA_NODATAOUT
    114 
    115 /* Debugging definitions. Should not be used unless you want a lot of
    116    printouts even under normal conditions */
    117 
    118 #undef	SEA_DEBUGQUEUE		/* Display info about queue-lengths */
    119 
    120 /******************************* board definitions **************************/
    121 /*
    122  * CONTROL defines
    123  */
    124 #define CMD_RST		0x01		/* scsi reset */
    125 #define CMD_SEL		0x02		/* scsi select */
    126 #define CMD_BSY		0x04		/* scsi busy */
    127 #define	CMD_ATTN	0x08		/* scsi attention */
    128 #define CMD_START_ARB	0x10		/* start arbitration bit */
    129 #define	CMD_EN_PARITY	0x20		/* enable scsi parity generation */
    130 #define CMD_INTR	0x40		/* enable scsi interrupts */
    131 #define CMD_DRVR_ENABLE	0x80		/* scsi enable */
    132 
    133 /*
    134  * STATUS
    135  */
    136 #define STAT_BSY	0x01		/* scsi busy */
    137 #define STAT_MSG	0x02		/* scsi msg */
    138 #define STAT_IO		0x04		/* scsi I/O */
    139 #define STAT_CD		0x08		/* scsi C/D */
    140 #define STAT_REQ	0x10		/* scsi req */
    141 #define STAT_SEL	0x20		/* scsi select */
    142 #define STAT_PARITY	0x40		/* parity error bit */
    143 #define STAT_ARB_CMPL	0x80		/* arbitration complete bit */
    144 
    145 /*
    146  * REQUESTS
    147  */
    148 #define PH_DATAOUT	(0)
    149 #define PH_DATAIN	(STAT_IO)
    150 #define PH_CMD		(STAT_CD)
    151 #define PH_STAT		(STAT_CD | STAT_IO)
    152 #define PH_MSGOUT	(STAT_MSG | STAT_CD)
    153 #define PH_MSGIN	(STAT_MSG | STAT_CD | STAT_IO)
    154 
    155 #define PH_MASK		(STAT_MSG | STAT_CD | STAT_IO)
    156 
    157 #define PH_INVALID	0xff
    158 
    159 #define SEA_RAMOFFSET	0x00001800
    160 
    161 #define BASE_CMD	(CMD_INTR | CMD_EN_PARITY)
    162 
    163 #define	SEAGATE		1	/* Seagate ST0[12] */
    164 #define	FDOMAIN		2	/* Future Domain TMC-{885,950} */
    165 #define	FDOMAIN840	3	/* Future Domain TMC-{84[01],88[01]} */
    166 
    167 /******************************************************************************/
    168 
    169 /* scsi control block used to keep info about a scsi command */
    170 struct sea_scb {
    171         u_char *data;			/* position in data buffer so far */
    172 	int datalen;			/* bytes remaining to transfer */
    173 	TAILQ_ENTRY(sea_scb) chain;
    174 	struct scsi_xfer *xs;		/* the scsi_xfer for this cmd */
    175 	int flags;			/* status of the instruction */
    176 #define	SCB_FREE	0
    177 #define	SCB_ACTIVE	1
    178 #define SCB_ABORTED	2
    179 #define SCB_TIMEOUT	4
    180 #define SCB_ERROR	8
    181 };
    182 
    183 /*
    184  * data structure describing current status of the scsi bus. One for each
    185  * controller card.
    186  */
    187 struct sea_softc {
    188 	struct device sc_dev;
    189 	struct isadev sc_id;
    190 	void *sc_ih;
    191 
    192 	int type;			/* board type */
    193 	caddr_t	maddr;			/* Base address for card */
    194 	caddr_t	maddr_cr_sr;		/* Address of control and status reg */
    195 	caddr_t	maddr_dr;		/* Address of data register */
    196 
    197 	struct scsi_link sc_link;	/* prototype for subdevs */
    198 	TAILQ_HEAD(, sea_scb) free_list, ready_list, nexus_list;
    199 	struct sea_scb *nexus;		/* currently connected command */
    200 	int numscbs;			/* number of scsi control blocks */
    201 	struct sea_scb scb[SCB_TABLE_SIZE];
    202 
    203 	int our_id;			/* our scsi id */
    204 	u_char our_id_mask;
    205 	volatile u_char busy[8];	/* index=target, bit=lun, Keep track of
    206 					   busy luns at device target */
    207 };
    208 
    209 /* flag showing if main routine is running. */
    210 static volatile int main_running = 0;
    211 
    212 #define	STATUS	(*(volatile u_char *)sea->maddr_cr_sr)
    213 #define CONTROL	STATUS
    214 #define DATA	(*(volatile u_char *)sea->maddr_dr)
    215 
    216 /*
    217  * These are "special" values for the tag parameter passed to sea_select
    218  * Not implemented right now.
    219  */
    220 #define TAG_NEXT	-1	/* Use next free tag */
    221 #define TAG_NONE	-2	/*
    222 				 * Establish I_T_L nexus instead of I_T_L_Q
    223 				 * even on SCSI-II devices.
    224 				 */
    225 
    226 typedef struct {
    227 	char *signature;
    228 	int offset, length;
    229 	int type;
    230 } BiosSignature;
    231 
    232 /*
    233  * Signatures for automatic recognition of board type
    234  */
    235 static const BiosSignature signatures[] = {
    236 {"ST01 v1.7  (C) Copyright 1987 Seagate", 15, 37, SEAGATE},
    237 {"SCSI BIOS 2.00  (C) Copyright 1987 Seagate", 15, 40, SEAGATE},
    238 
    239 /*
    240  * The following two lines are NOT mistakes. One detects ROM revision
    241  * 3.0.0, the other 3.2. Since seagate has only one type of SCSI adapter,
    242  * and this is not going to change, the "SEAGATE" and "SCSI" together
    243  * are probably "good enough"
    244  */
    245 {"SEAGATE SCSI BIOS ", 16, 17, SEAGATE},
    246 {"SEAGATE SCSI BIOS ", 17, 17, SEAGATE},
    247 
    248 /*
    249  * However, future domain makes several incompatible SCSI boards, so specific
    250  * signatures must be used.
    251  */
    252 {"FUTURE DOMAIN CORP. (C) 1986-1989 V5.0C2/14/89", 5, 45, FDOMAIN},
    253 {"FUTURE DOMAIN CORP. (C) 1986-1989 V6.0A7/28/89", 5, 46, FDOMAIN},
    254 {"FUTURE DOMAIN CORP. (C) 1986-1990 V6.0105/31/90",5, 47, FDOMAIN},
    255 {"FUTURE DOMAIN CORP. (C) 1986-1990 V6.0209/18/90",5, 47, FDOMAIN},
    256 {"FUTURE DOMAIN CORP. (C) 1986-1990 V7.009/18/90", 5, 46, FDOMAIN},
    257 {"FUTURE DOMAIN CORP. (C) 1992 V8.00.004/02/92",   5, 44, FDOMAIN},
    258 {"FUTURE DOMAIN TMC-950",			   5, 21, FDOMAIN},
    259 };
    260 
    261 #define	nsignatures	(sizeof(signatures) / sizeof(signatures[0]))
    262 
    263 static const char *bases[] = {
    264 	(char *) 0xc8000, (char *) 0xca000, (char *) 0xcc000,
    265 	(char *) 0xce000, (char *) 0xdc000, (char *) 0xde000
    266 };
    267 
    268 #define	nbases		(sizeof(bases) / sizeof(bases[0]))
    269 
    270 int seaintr __P((void *));
    271 int sea_scsi_cmd __P((struct scsi_xfer *));
    272 void sea_timeout __P((void *));
    273 void sea_done __P((struct sea_softc *, struct sea_scb *));
    274 struct sea_scb *sea_get_scb __P((struct sea_softc *, int));
    275 void sea_free_scb __P((struct sea_softc *, struct sea_scb *, int));
    276 static void sea_main __P((void));
    277 static void sea_information_transfer __P((struct sea_softc *));
    278 int sea_poll __P((struct sea_softc *, struct scsi_xfer *, int));
    279 void sea_init __P((struct sea_softc *));
    280 void sea_send_scb __P((struct sea_softc *sea, struct sea_scb *scb));
    281 void sea_reselect __P((struct sea_softc *sea));
    282 int sea_select __P((struct sea_softc *sea, struct sea_scb *scb));
    283 int sea_transfer_pio __P((struct sea_softc *sea, u_char *phase,
    284     int *count, u_char **data));
    285 int sea_abort __P((struct sea_softc *, struct sea_scb *scb));
    286 
    287 struct scsi_adapter sea_switch = {
    288 	sea_scsi_cmd,
    289 	minphys,	/* no special minphys(), since driver uses PIO */
    290 	0,
    291 	0,
    292 };
    293 
    294 /* the below structure is so we have a default dev struct for our link struct */
    295 struct scsi_device sea_dev = {
    296 	NULL,		/* use default error handler */
    297 	NULL,		/* have a queue, served by this */
    298 	NULL,		/* have no async handler */
    299 	NULL,		/* Use default 'done' routine */
    300 };
    301 
    302 int	seaprobe __P((struct device *, void *, void *));
    303 void	seaattach __P((struct device *, struct device *, void *));
    304 int	seaprint __P((void *, char *));
    305 
    306 struct cfattach sea_ca = {
    307 	sizeof(struct sea_softc), seaprobe, seaattach
    308 };
    309 
    310 struct cfdriver sea_cd = {
    311 	NULL, "sea", DV_DULL
    312 };
    313 
    314 #ifdef SEA_DEBUGQUEUE
    315 void
    316 sea_queue_length(sea)
    317 	struct sea_softc *sea;
    318 {
    319 	struct sea_scb *scb;
    320 	int connected, issued, disconnected;
    321 
    322 	connected = sea->nexus ? 1 : 0;
    323 	for (scb = sea->ready_list.tqh_first, issued = 0; scb;
    324 	    scb = scb->chain.tqe_next, issued++);
    325 	for (scb = sea->nexus_list.tqh_first, disconnected = 0; scb;
    326 	    scb = scb->chain.tqe_next, disconnected++);
    327 	printf("%s: length: %d/%d/%d\n", sea->sc_dev.dv_xname, connected,
    328 	    issued, disconnected);
    329 }
    330 #endif
    331 
    332 /*
    333  * Check if the device can be found at the port given and if so, detect the
    334  * type the type of board.  Set it up ready for further work. Takes the isa_dev
    335  * structure from autoconf as an argument.
    336  * Returns 1 if card recognized, 0 if errors.
    337  */
    338 int
    339 seaprobe(parent, match, aux)
    340 	struct device *parent;
    341 	void *match, *aux;
    342 {
    343 	struct sea_softc *sea = match;
    344 	struct isa_attach_args *ia = aux;
    345 	int i;
    346 
    347 	/*
    348 	 * Could try to find a board by looking through all possible addresses.
    349 	 * This is not done the right way now, because I have not found a way
    350 	 * to get a boards virtual memory address given its physical.  There is
    351 	 * a function that returns the physical address for a given virtual
    352 	 * address, but not the other way around.
    353 	 */
    354 
    355 	if (ia->ia_maddr == MADDRUNK) {
    356 		/* XXX */
    357 		return 0;
    358 	} else
    359 		sea->maddr = ISA_HOLE_VADDR(ia->ia_maddr);
    360 
    361 	/* check board type */	/* No way to define this through config */
    362 	for (i = 0; i < nsignatures; i++)
    363 		if (!bcmp(sea->maddr + signatures[i].offset,
    364 		    signatures[i].signature, signatures[i].length)) {
    365 			sea->type = signatures[i].type;
    366 			break;
    367 		}
    368 
    369 	/* Find controller and data memory addresses */
    370 	switch (sea->type) {
    371 	case SEAGATE:
    372 	case FDOMAIN840:
    373 		sea->maddr_cr_sr =
    374 		    (void *) (((u_char *)sea->maddr) + 0x1a00);
    375 		sea->maddr_dr =
    376 		    (void *) (((u_char *)sea->maddr) + 0x1c00);
    377 		break;
    378 	case FDOMAIN:
    379 		sea->maddr_cr_sr =
    380 		    (void *) (((u_char *)sea->maddr) + 0x1c00);
    381 		sea->maddr_dr =
    382 		    (void *) (((u_char *)sea->maddr) + 0x1e00);
    383 		break;
    384 	default:
    385 #ifdef DIAGNOSTIC
    386 		printf("%s: board type unknown at address 0x%lx\n",
    387 		    sea->sc_dev.dv_xname, sea->maddr);
    388 #endif
    389 		return 0;
    390 	}
    391 
    392 	/* Test controller RAM (works the same way on future domain cards?) */
    393 	*((u_char *)sea->maddr + SEA_RAMOFFSET) = 0xa5;
    394 	*((u_char *)sea->maddr + SEA_RAMOFFSET + 1) = 0x5a;
    395 
    396 	if ((*((u_char *)sea->maddr + SEA_RAMOFFSET) != 0xa5) ||
    397 	    (*((u_char *)sea->maddr + SEA_RAMOFFSET + 1) != 0x5a)) {
    398 		printf("%s: board RAM failure\n", sea->sc_dev.dv_xname);
    399 		return 0;
    400 	}
    401 
    402 	ia->ia_drq = DRQUNK;
    403 	ia->ia_msize = 0x2000;
    404 	ia->ia_iosize = 0;
    405 	return 1;
    406 }
    407 
    408 int
    409 seaprint(aux, name)
    410 	void *aux;
    411 	char *name;
    412 {
    413 	if (name != NULL)
    414 		printf("%s: scsibus ", name);
    415 	return UNCONF;
    416 }
    417 
    418 /*
    419  * Attach all sub-devices we can find
    420  */
    421 void
    422 seaattach(parent, self, aux)
    423 	struct device *parent, *self;
    424 	void *aux;
    425 {
    426 	struct isa_attach_args *ia = aux;
    427 	struct sea_softc *sea = (void *)self;
    428 
    429 	sea_init(sea);
    430 
    431 	/*
    432 	 * fill in the prototype scsi_link.
    433 	 */
    434 	sea->sc_link.adapter_softc = sea;
    435 	sea->sc_link.adapter_target = sea->our_id;
    436 	sea->sc_link.adapter = &sea_switch;
    437 	sea->sc_link.device = &sea_dev;
    438 	sea->sc_link.openings = 1;
    439 
    440 	printf("\n");
    441 
    442 #ifdef NEWCONFIG
    443 	isa_establish(&sea->sc_id, &sea->sc_deV);
    444 #endif
    445 	sea->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
    446 	    IPL_BIO, seaintr, sea);
    447 
    448 	/*
    449 	 * ask the adapter what subunits are present
    450 	 */
    451 	config_found(self, &sea->sc_link, seaprint);
    452 }
    453 
    454 /*
    455  * Catch an interrupt from the adaptor
    456  */
    457 int
    458 seaintr(arg)
    459 	void *arg;
    460 {
    461 	struct sea_softc *sea = arg;
    462 
    463 #ifdef DEBUG	/* extra overhead, and only needed for intr debugging */
    464 	if ((STATUS & STAT_PARITY) == 0 &&
    465 	    (STATUS & (STAT_SEL | STAT_IO)) != (STAT_SEL | STAT_IO))
    466 		return 0;
    467 #endif
    468 
    469 loop:
    470 	/* dispatch to appropriate routine if found and done=0 */
    471 	/* should check to see that this card really caused the interrupt */
    472 
    473 	if (STATUS & STAT_PARITY) {
    474 		/* Parity error interrupt */
    475 		printf("%s: parity error\n", sea->sc_dev.dv_xname);
    476 		return 1;
    477 	}
    478 
    479 	if ((STATUS & (STAT_SEL | STAT_IO)) == (STAT_SEL | STAT_IO)) {
    480 		/* Reselect interrupt */
    481 		sea_reselect(sea);
    482 		if (!main_running)
    483 			sea_main();
    484 		goto loop;
    485 	}
    486 
    487 	return 1;
    488 }
    489 
    490 /*
    491  * Setup data structures, and reset the board and the SCSI bus.
    492  */
    493 void
    494 sea_init(sea)
    495 	struct sea_softc *sea;
    496 {
    497 	int i;
    498 
    499 	/* Reset the scsi bus (I don't know if this is needed */
    500 	CONTROL = BASE_CMD | CMD_DRVR_ENABLE | CMD_RST;
    501 	delay(25);	/* hold reset for at least 25 microseconds */
    502 	CONTROL = BASE_CMD;
    503 	delay(10); 	/* wait a Bus Clear Delay (800 ns + bus free delay (800 ns) */
    504 
    505 	/* Set our id (don't know anything about this) */
    506 	switch (sea->type) {
    507 	case SEAGATE:
    508 		sea->our_id = 7;
    509 		break;
    510 	case FDOMAIN:
    511 	case FDOMAIN840:
    512 		sea->our_id = 6;
    513 		break;
    514 	}
    515 	sea->our_id_mask = 1 << sea->our_id;
    516 
    517 	/* init fields used by our routines */
    518 	sea->nexus = 0;
    519 	TAILQ_INIT(&sea->ready_list);
    520 	TAILQ_INIT(&sea->nexus_list);
    521 	TAILQ_INIT(&sea->free_list);
    522 	for (i = 0; i < 8; i++)
    523 		sea->busy[i] = 0x00;
    524 
    525 	/* link up the free list of scbs */
    526 	sea->numscbs = SCB_TABLE_SIZE;
    527 	for (i = 0; i < SCB_TABLE_SIZE; i++) {
    528 		TAILQ_INSERT_TAIL(&sea->free_list, &sea->scb[i], chain);
    529 	}
    530 }
    531 
    532 /*
    533  * start a scsi operation given the command and the data address. Also needs
    534  * the unit, target and lu.
    535  */
    536 int
    537 sea_scsi_cmd(xs)
    538 	struct scsi_xfer *xs;
    539 {
    540 	struct scsi_link *sc_link = xs->sc_link;
    541 	struct sea_softc *sea = sc_link->adapter_softc;
    542 	struct sea_scb *scb;
    543 	int flags;
    544 	int s;
    545 
    546 	SC_DEBUG(sc_link, SDEV_DB2, ("sea_scsi_cmd\n"));
    547 
    548 	flags = xs->flags;
    549 	if ((flags & (ITSDONE|INUSE)) != INUSE) {
    550 		printf("%s: done or not in use?\n", sea->sc_dev.dv_xname);
    551 		xs->flags &= ~ITSDONE;
    552 		xs->flags |= INUSE;
    553 	}
    554 	if ((scb = sea_get_scb(sea, flags)) == NULL) {
    555 		xs->error = XS_DRIVER_STUFFUP;
    556 		return TRY_AGAIN_LATER;
    557 	}
    558 	scb->flags = SCB_ACTIVE;
    559 	scb->xs = xs;
    560 
    561 	if (flags & SCSI_RESET) {
    562 		/*
    563 		 * Try to send a reset command to the card.
    564 		 * XXX Not implemented.
    565 		 */
    566 		printf("%s: resetting\n", sea->sc_dev.dv_xname);
    567 		xs->error = XS_DRIVER_STUFFUP;
    568 		return COMPLETE;
    569 	}
    570 
    571 	/*
    572 	 * Put all the arguments for the xfer in the scb
    573 	 */
    574 	scb->datalen = xs->datalen;
    575 	scb->data = xs->data;
    576 
    577 #ifdef SEA_DEBUGQUEUE
    578 	sea_queue_length(sea);
    579 #endif
    580 
    581 	s = splbio();
    582 
    583 	sea_send_scb(sea, scb);
    584 
    585 	/*
    586 	 * Usually return SUCCESSFULLY QUEUED
    587 	 */
    588 	if ((flags & SCSI_POLL) == 0) {
    589 		timeout(sea_timeout, scb, (xs->timeout * hz) / 1000);
    590 		splx(s);
    591 		return SUCCESSFULLY_QUEUED;
    592 	}
    593 
    594 	splx(s);
    595 
    596 	/*
    597 	 * If we can't use interrupts, poll on completion
    598 	 */
    599 	if (sea_poll(sea, xs, xs->timeout)) {
    600 		sea_timeout(scb);
    601 		if (sea_poll(sea, xs, 2000))
    602 			sea_timeout(scb);
    603 	}
    604 	return COMPLETE;
    605 }
    606 
    607 /*
    608  * Get a free scb. If there are none, see if we can allocate a new one.  If so,
    609  * put it in the hash table too; otherwise return an error or sleep.
    610  */
    611 struct sea_scb *
    612 sea_get_scb(sea, flags)
    613 	struct sea_softc *sea;
    614 	int flags;
    615 {
    616 	int s;
    617 	struct sea_scb *scb;
    618 
    619 	s = splbio();
    620 
    621 	/*
    622 	 * If we can and have to, sleep waiting for one to come free
    623 	 * but only if we can't allocate a new one.
    624 	 */
    625 	for (;;) {
    626 		scb = sea->free_list.tqh_first;
    627 		if (scb) {
    628 			TAILQ_REMOVE(&sea->free_list, scb, chain);
    629 			break;
    630 		}
    631 		if (sea->numscbs < SEA_SCB_MAX) {
    632 			if (scb = (struct sea_scb *) malloc(sizeof(struct sea_scb),
    633 			    M_TEMP, M_NOWAIT)) {
    634 				bzero(scb, sizeof(struct sea_scb));
    635 				sea->numscbs++;
    636 			} else
    637 				printf("%s: can't malloc scb\n",
    638 				    sea->sc_dev.dv_xname);
    639 			break;
    640 		}
    641 		if ((flags & SCSI_NOSLEEP) != 0)
    642 			break;
    643 		tsleep(&sea->free_list, PRIBIO, "seascb", 0);
    644 	}
    645 
    646 	splx(s);
    647 	return scb;
    648 }
    649 
    650 /*
    651  * Try to send this command to the board. Because this board does not use any
    652  * mailboxes, this routine simply adds the command to the queue held by the
    653  * sea_softc structure.
    654  * A check is done to see if the command contains a REQUEST_SENSE command, and
    655  * if so the command is put first in the queue, otherwise the command is added
    656  * to the end of the queue. ?? Not correct ??
    657  */
    658 void
    659 sea_send_scb(sea, scb)
    660 	struct sea_softc *sea;
    661 	struct sea_scb *scb;
    662 {
    663 
    664 	TAILQ_INSERT_TAIL(&sea->ready_list, scb, chain);
    665 	/* Try to do some work on the card. */
    666 	if (!main_running)
    667 		sea_main();
    668 }
    669 
    670 /*
    671  * Coroutine that runs as long as more work can be done on the seagate host
    672  * adapter in a system.  Both sea_scsi_cmd and sea_intr will try to start it in
    673  * case it is not running.
    674  */
    675 void
    676 sea_main()
    677 {
    678 	struct sea_softc *sea;
    679 	struct sea_scb *scb;
    680 	int done;
    681 	int unit;
    682 	int s;
    683 
    684 	main_running = 1;
    685 
    686 	/*
    687 	 * This should not be run with interrupts disabled, but use the splx
    688 	 * code instead.
    689 	 */
    690 loop:
    691 	done = 1;
    692 	for (unit = 0; unit < sea_cd.cd_ndevs; unit++) {
    693 		sea = sea_cd.cd_devs[unit];
    694 		if (!sea)
    695 			continue;
    696 		s = splbio();
    697 		if (!sea->nexus) {
    698 			/*
    699 			 * Search through the ready_list for a command
    700 			 * destined for a target that's not busy.
    701 			 */
    702 			for (scb = sea->ready_list.tqh_first; scb;
    703 			    scb = scb->chain.tqe_next) {
    704 				if (!(sea->busy[scb->xs->sc_link->target] &
    705 				    (1 << scb->xs->sc_link->lun))) {
    706 					TAILQ_REMOVE(&sea->ready_list, scb,
    707 					    chain);
    708 
    709 					/* Re-enable interrupts. */
    710 					splx(s);
    711 
    712 					/*
    713 					 * Attempt to establish an I_T_L nexus.
    714 					 * On success, sea->nexus is set.
    715 					 * On failure, we must add the command
    716 					 * back to the issue queue so we can
    717 					 * keep trying.
    718 					 */
    719 
    720 					/*
    721 					 * REQUEST_SENSE commands are issued
    722 					 * without tagged queueing, even on
    723 					 * SCSI-II devices because the
    724 					 * contingent alligence condition
    725 					 * exists for the entire unit.
    726 					 */
    727 
    728 					/*
    729 					 * First check that if any device has
    730 					 * tried a reconnect while we have done
    731 					 * other things with interrupts
    732 					 * disabled.
    733 					 */
    734 
    735 					if ((STATUS & (STAT_SEL | STAT_IO)) ==
    736 					    (STAT_SEL | STAT_IO)) {
    737 						sea_reselect(sea);
    738 						break;
    739 					}
    740 					if (sea_select(sea, scb)) {
    741 						s = splbio();
    742 						TAILQ_INSERT_HEAD(&sea->ready_list,
    743 						    scb, chain);
    744 						splx(s);
    745 					} else
    746 						break;
    747 				} /* if target/lun is not busy */
    748 			} /* for scb */
    749 			if (!sea->nexus) {
    750 				/* check for reselection phase */
    751 				if ((STATUS & (STAT_SEL | STAT_IO)) ==
    752 				    (STAT_SEL | STAT_IO)) {
    753 					sea_reselect(sea);
    754 				}
    755 			}
    756 		} /* if (!sea->nexus) */
    757 
    758 		splx(s);
    759 		if (sea->nexus) {	/* we are connected. Do the task */
    760 			sea_information_transfer(sea);
    761 			done = 0;
    762 		} else
    763 			break;
    764 	} /* for instance */
    765 
    766 	if (!done)
    767 		goto loop;
    768 
    769 	main_running = 0;
    770 }
    771 
    772 void
    773 sea_free_scb(sea, scb, flags)
    774 	struct sea_softc *sea;
    775 	struct sea_scb *scb;
    776 	int flags;
    777 {
    778 	int s;
    779 
    780 	s = splbio();
    781 
    782 	scb->flags = SCB_FREE;
    783 	TAILQ_INSERT_HEAD(&sea->free_list, scb, chain);
    784 
    785 	/*
    786 	 * If there were none, wake anybody waiting for one to come free,
    787 	 * starting with queued entries.
    788 	 */
    789 	if (!scb->chain.tqe_next)
    790 		wakeup((caddr_t)&sea->free_list);
    791 
    792 	splx(s);
    793 }
    794 
    795 void
    796 sea_timeout(arg)
    797 	void *arg;
    798 {
    799 	struct sea_scb *scb = arg;
    800 	struct scsi_xfer *xs = scb->xs;
    801 	struct scsi_link *sc_link = xs->sc_link;
    802 	struct sea_softc *sea = sc_link->adapter_softc;
    803 	int s;
    804 
    805 	sc_print_addr(sc_link);
    806 	printf("timed out");
    807 
    808 	s = splbio();
    809 
    810 	/*
    811 	 * If it has been through before, then
    812 	 * a previous abort has failed, don't
    813 	 * try abort again
    814 	 */
    815 	if (scb->flags & SCB_ABORTED) {
    816 		/* abort timed out */
    817 		printf(" AGAIN\n");
    818 	 	scb->xs->retries = 0;
    819 		scb->flags |= SCB_ABORTED;
    820 		sea_done(sea, scb);
    821 	} else {
    822 		/* abort the operation that has timed out */
    823 		printf("\n");
    824 		scb->flags |= SCB_ABORTED;
    825 		sea_abort(sea, scb);
    826 		/* 2 secs for the abort */
    827 		if ((xs->flags & SCSI_POLL) == 0)
    828 			timeout(sea_timeout, scb, 2 * hz);
    829 	}
    830 
    831 	splx(s);
    832 }
    833 
    834 void
    835 sea_reselect(sea)
    836 	struct sea_softc *sea;
    837 {
    838 	u_char target_mask;
    839 	int i;
    840 	u_char lun, phase;
    841 	u_char msg[3];
    842 	int len;
    843 	u_char *data;
    844 	struct sea_scb *scb;
    845 	int abort = 0;
    846 
    847 	if (!((target_mask = STATUS) & STAT_SEL)) {
    848 		printf("%s: wrong state 0x%x\n", sea->sc_dev.dv_xname,
    849 		    target_mask);
    850 		return;
    851 	}
    852 
    853 	/* wait for a device to win the reselection phase */
    854 	/* signals this by asserting the I/O signal */
    855 	for (i = 10; i && (STATUS & (STAT_SEL | STAT_IO | STAT_BSY)) !=
    856 	    (STAT_SEL | STAT_IO | 0); i--);
    857 	/* !! Check for timeout here */
    858 	/* the data bus contains original initiator id ORed with target id */
    859 	target_mask = DATA;
    860 	/* see that we really are the initiator */
    861 	if (!(target_mask & sea->our_id_mask)) {
    862 		printf("%s: polled reselection was not for me: 0x%x\n",
    863 		    sea->sc_dev.dv_xname, target_mask);
    864 		return;
    865 	}
    866 	/* find target who won */
    867 	target_mask &= ~sea->our_id_mask;
    868 	/* host responds by asserting the BSY signal */
    869 	CONTROL = BASE_CMD | CMD_DRVR_ENABLE | CMD_BSY;
    870 	/* target should respond by deasserting the SEL signal */
    871 	for (i = 50000; i && (STATUS & STAT_SEL); i++);
    872 	/* remove the busy status */
    873 	CONTROL = BASE_CMD | CMD_DRVR_ENABLE;
    874 	/* we are connected. Now we wait for the MSGIN condition */
    875 	for (i = 50000; i && !(STATUS & STAT_REQ); i--);
    876 	/* !! Add timeout check here */
    877 	/* hope we get an IDENTIFY message */
    878 	len = 3;
    879 	data = msg;
    880 	phase = PH_MSGIN;
    881 	sea_transfer_pio(sea, &phase, &len, &data);
    882 
    883 	if (MSG_ISIDENTIFY(msg[0])) {
    884 		printf("%s: expecting IDENTIFY message, got 0x%x\n",
    885 		    sea->sc_dev.dv_xname, msg[0]);
    886 		abort = 1;
    887 	} else {
    888 		lun = msg[0] & 0x07;
    889 
    890 		/*
    891 		 * Find the command corresponding to the I_T_L or I_T_L_Q nexus
    892 		 * we just reestablished, and remove it from the disconnected
    893 		 * queue.
    894 		 */
    895 		for (scb = sea->nexus_list.tqh_first; scb;
    896 		    scb = scb->chain.tqe_next)
    897 			if (target_mask == (1 << scb->xs->sc_link->target) &&
    898 			    lun == scb->xs->sc_link->lun) {
    899 				TAILQ_REMOVE(&sea->nexus_list, scb,
    900 				    chain);
    901 				break;
    902 			}
    903 		if (!scb) {
    904 			printf("%s: target %02x lun %d not disconnected\n",
    905 			    sea->sc_dev.dv_xname, target_mask, lun);
    906 			/*
    907 			 * Since we have an established nexus that we can't do
    908 			 * anything with, we must abort it.
    909 			 */
    910 			abort = 1;
    911 		}
    912 	}
    913 
    914 	if (abort) {
    915 		msg[0] = MSG_ABORT;
    916 		len = 1;
    917 		data = msg;
    918 		phase = PH_MSGOUT;
    919 		CONTROL = BASE_CMD | CMD_ATTN;
    920 		sea_transfer_pio(sea, &phase, &len, &data);
    921 	} else
    922 		sea->nexus = scb;
    923 
    924 	return;
    925 }
    926 
    927 /*
    928  * Transfer data in given phase using polled I/O.
    929  */
    930 int
    931 sea_transfer_pio(sea, phase, count, data)
    932 	struct sea_softc *sea;
    933 	u_char *phase;
    934 	int *count;
    935 	u_char **data;
    936 {
    937 	register u_char p = *phase, tmp;
    938 	register int c = *count;
    939 	register u_char *d = *data;
    940 	int timeout;
    941 
    942 	do {
    943 		/*
    944 		 * Wait for assertion of REQ, after which the phase bits will
    945 		 * be valid.
    946 		 */
    947 		for (timeout = 0; timeout < 50000; timeout++)
    948 			if ((tmp = STATUS) & STAT_REQ)
    949 				break;
    950 		if (!(tmp & STAT_REQ)) {
    951 			printf("%s: timeout waiting for STAT_REQ\n",
    952 			    sea->sc_dev.dv_xname);
    953 			break;
    954 		}
    955 
    956 		/*
    957 		 * Check for phase mismatch.  Reached if the target decides
    958 		 * that it has finished the transfer.
    959 		 */
    960 		if (sea->type == FDOMAIN840)
    961 			tmp = ((tmp & 0x08) >> 2) |
    962 			      ((tmp & 0x02) << 2) |
    963 			       (tmp & 0xf5);
    964 		if ((tmp & PH_MASK) != p)
    965 			break;
    966 
    967 		/* Do actual transfer from SCSI bus to/from memory. */
    968 		if (!(p & STAT_IO))
    969 			DATA = *d;
    970 		else
    971 			*d = DATA;
    972 		++d;
    973 
    974 		/*
    975 		 * The SCSI standard suggests that in MSGOUT phase, the
    976 		 * initiator should drop ATN on the last byte of the message
    977 		 * phase after REQ has been asserted for the handshake but
    978 		 * before the initiator raises ACK.
    979 		 * Don't know how to accomplish this on the ST01/02.
    980 		 */
    981 
    982 #if 0
    983 		/*
    984 		 * XXX
    985 		 * The st01 code doesn't wait for STAT_REQ to be deasserted.
    986 		 * Is this ok?
    987 		 */
    988 		for (timeout = 0; timeout < 200000L; timeout++)
    989 			if (!(STATUS & STAT_REQ))
    990 				break;
    991 		if (STATUS & STAT_REQ)
    992 			printf("%s: timeout on wait for !STAT_REQ",
    993 			    sea->sc_dev.dv_xname);
    994 #endif
    995 	} while (--c);
    996 
    997 	*count = c;
    998 	*data = d;
    999 	tmp = STATUS;
   1000 	if (tmp & STAT_REQ)
   1001 		*phase = tmp & PH_MASK;
   1002 	else
   1003 		*phase = PH_INVALID;
   1004 
   1005 	if (c && (*phase != p))
   1006 		return -1;
   1007 	return 0;
   1008 }
   1009 
   1010 /*
   1011  * Establish I_T_L or I_T_L_Q nexus for new or existing command including
   1012  * ARBITRATION, SELECTION, and initial message out for IDENTIFY and queue
   1013  * messages.  Return -1 if selection could not execute for some reason, 0 if
   1014  * selection succeded or failed because the target did not respond.
   1015  */
   1016 int
   1017 sea_select(sea, scb)
   1018 	struct sea_softc *sea;
   1019 	struct sea_scb *scb;
   1020 {
   1021 	u_char msg[3], phase;
   1022 	u_char *data;
   1023 	int len;
   1024 	int timeout;
   1025 
   1026 	CONTROL = BASE_CMD;
   1027 	DATA = sea->our_id_mask;
   1028 	CONTROL = (BASE_CMD & ~CMD_INTR) | CMD_START_ARB;
   1029 
   1030 	/* wait for arbitration to complete */
   1031 	for (timeout = 0; timeout < 3000000L; timeout++)
   1032 		if (STATUS & STAT_ARB_CMPL)
   1033 			break;
   1034 	if (!(STATUS & STAT_ARB_CMPL)) {
   1035 		if (STATUS & STAT_SEL) {
   1036 			printf("%s: arbitration lost\n", sea->sc_dev.dv_xname);
   1037 			scb->flags |= SCB_ERROR;
   1038 		} else {
   1039 			printf("%s: arbitration timeout\n",
   1040 			    sea->sc_dev.dv_xname);
   1041 			scb->flags |= SCB_TIMEOUT;
   1042 		}
   1043 		CONTROL = BASE_CMD;
   1044 		return -1;
   1045 	}
   1046 
   1047 	delay(2);
   1048 	DATA = (u_char)((1 << scb->xs->sc_link->target) | sea->our_id_mask);
   1049 	CONTROL =
   1050 #ifdef SEA_NOMSGS
   1051 	    (BASE_CMD & ~CMD_INTR) | CMD_DRVR_ENABLE | CMD_SEL;
   1052 #else
   1053 	    (BASE_CMD & ~CMD_INTR) | CMD_DRVR_ENABLE | CMD_SEL | CMD_ATTN;
   1054 #endif
   1055 	delay(1);
   1056 
   1057 	/* wait for a bsy from target */
   1058 	for (timeout = 0; timeout < 2000000L; timeout++)
   1059 		if (STATUS & STAT_BSY)
   1060 			break;
   1061 	if (!(STATUS & STAT_BSY)) {
   1062 		/* should return some error to the higher level driver */
   1063 		CONTROL = BASE_CMD;
   1064 		scb->flags |= SCB_TIMEOUT;
   1065 		return 0;
   1066 	}
   1067 
   1068 	/* Try to make the target to take a message from us */
   1069 #ifdef SEA_NOMSGS
   1070 	CONTROL = (BASE_CMD & ~CMD_INTR) | CMD_DRVR_ENABLE;
   1071 #else
   1072 	CONTROL = (BASE_CMD & ~CMD_INTR) | CMD_DRVR_ENABLE | CMD_ATTN;
   1073 #endif
   1074 	delay(1);
   1075 
   1076 	/* should start a msg_out phase */
   1077 	for (timeout = 0; timeout < 2000000L; timeout++)
   1078 		if (STATUS & STAT_REQ)
   1079 			break;
   1080 	/* Remove ATN. */
   1081 	CONTROL = BASE_CMD | CMD_DRVR_ENABLE;
   1082 	if (!(STATUS & STAT_REQ)) {
   1083 		/*
   1084 		 * This should not be taken as an error, but more like an
   1085 		 * unsupported feature!  Should set a flag indicating that the
   1086 		 * target don't support messages, and continue without failure.
   1087 		 * (THIS IS NOT AN ERROR!)
   1088 		 */
   1089 	} else {
   1090 		msg[0] = MSG_IDENTIFY(scb->xs->sc_link->lun, 1);
   1091 		len = 1;
   1092 		data = msg;
   1093 		phase = PH_MSGOUT;
   1094 		/* Should do test on result of sea_transfer_pio(). */
   1095 		sea_transfer_pio(sea, &phase, &len, &data);
   1096 	}
   1097 	if (!(STATUS & STAT_BSY))
   1098 		printf("%s: after successful arbitrate: no STAT_BSY!\n",
   1099 		    sea->sc_dev.dv_xname);
   1100 
   1101 	sea->nexus = scb;
   1102 	sea->busy[scb->xs->sc_link->target] |= 1 << scb->xs->sc_link->lun;
   1103 	/* This assignment should depend on possibility to send a message to target. */
   1104 	CONTROL = BASE_CMD | CMD_DRVR_ENABLE;
   1105 	/* XXX Reset pointer in command? */
   1106 	return 0;
   1107 }
   1108 
   1109 /*
   1110  * Send an abort to the target.  Return 1 success, 0 on failure.
   1111  */
   1112 int
   1113 sea_abort(sea, scb)
   1114 	struct sea_softc *sea;
   1115 	struct sea_scb *scb;
   1116 {
   1117 	struct sea_scb *tmp;
   1118 	u_char msg, phase, *msgptr;
   1119 	int len;
   1120 
   1121 	/*
   1122 	 * If the command hasn't been issued yet, we simply remove it from the
   1123 	 * issue queue
   1124 	 * XXX Could avoid this loop.
   1125 	 */
   1126 	for (tmp = sea->ready_list.tqh_first; tmp; tmp = tmp->chain.tqe_next)
   1127 		if (scb == tmp) {
   1128 			TAILQ_REMOVE(&sea->ready_list, scb, chain);
   1129 			/* XXX Set some type of error result for operation. */
   1130 			return 1;
   1131 		}
   1132 
   1133 	/*
   1134 	 * If any commands are connected, we're going to fail the abort and let
   1135 	 * the high level SCSI driver retry at a later time or issue a reset.
   1136 	 */
   1137 	if (sea->nexus)
   1138 		return 0;
   1139 
   1140 	/*
   1141 	 * If the command is currently disconnected from the bus, and there are
   1142 	 * no connected commands, we reconnect the I_T_L or I_T_L_Q nexus
   1143 	 * associated with it, go into message out, and send an abort message.
   1144 	 */
   1145 	for (tmp = sea->nexus_list.tqh_first; tmp;
   1146 	    tmp = tmp->chain.tqe_next)
   1147 		if (scb == tmp) {
   1148 			if (sea_select(sea, scb))
   1149 				return 0;
   1150 
   1151 			msg = MSG_ABORT;
   1152 			msgptr = &msg;
   1153 			len = 1;
   1154 			phase = PH_MSGOUT;
   1155 			CONTROL = BASE_CMD | CMD_ATTN;
   1156 			sea_transfer_pio(sea, &phase, &len, &msgptr);
   1157 
   1158 			for (tmp = sea->nexus_list.tqh_first; tmp;
   1159 			    tmp = tmp->chain.tqe_next)
   1160 				if (scb == tmp) {
   1161 					TAILQ_REMOVE(&sea->nexus_list,
   1162 					    scb, chain);
   1163 					/* XXX Set some type of error result
   1164 					   for the operation. */
   1165 					return 1;
   1166 				}
   1167 		}
   1168 
   1169 	/* Command not found in any queue; race condition? */
   1170 	return 1;
   1171 }
   1172 
   1173 void
   1174 sea_done(sea, scb)
   1175 	struct sea_softc *sea;
   1176 	struct sea_scb *scb;
   1177 {
   1178 	struct scsi_xfer *xs = scb->xs;
   1179 
   1180 	untimeout(sea_timeout, scb);
   1181 
   1182 	xs->resid = scb->datalen;
   1183 
   1184 	/* XXXX need to get status */
   1185 	if (scb->flags == SCB_ACTIVE) {
   1186 		xs->resid = 0;
   1187 	} else {
   1188 		if (scb->flags & (SCB_TIMEOUT | SCB_ABORTED))
   1189 			xs->error = XS_TIMEOUT;
   1190 		if (scb->flags & SCB_ERROR)
   1191 			xs->error = XS_DRIVER_STUFFUP;
   1192 	}
   1193 	xs->flags |= ITSDONE;
   1194 	sea_free_scb(sea, scb, xs->flags);
   1195 	scsi_done(xs);
   1196 }
   1197 
   1198 /*
   1199  * Wait for completion of command in polled mode.
   1200  */
   1201 int
   1202 sea_poll(sea, xs, count)
   1203 	struct sea_softc *sea;
   1204 	struct scsi_xfer *xs;
   1205 	int count;
   1206 {
   1207 	int s;
   1208 
   1209 	while (count) {
   1210 		/* try to do something */
   1211 		s = splbio();
   1212 		if (!main_running)
   1213 			sea_main();
   1214 		splx(s);
   1215 		if (xs->flags & ITSDONE)
   1216 			return 0;
   1217 		delay(1000);
   1218 		count--;
   1219 	}
   1220 	return 1;
   1221 }
   1222 
   1223 /*
   1224  * Do the transfer.  We know we are connected.  Update the flags, and call
   1225  * sea_done() when task accomplished.  Dialog controlled by the target.
   1226  */
   1227 void
   1228 sea_information_transfer(sea)
   1229 	struct sea_softc *sea;
   1230 {
   1231 	int timeout;
   1232 	u_char msgout = MSG_NOOP;
   1233 	int len;
   1234 	int s;
   1235 	u_char *data;
   1236 	u_char phase, tmp, old_phase = PH_INVALID;
   1237 	struct sea_scb *scb = sea->nexus;
   1238 	int loop;
   1239 
   1240 	for (timeout = 0; timeout < 10000000L; timeout++) {
   1241 		tmp = STATUS;
   1242 		if (tmp & STAT_PARITY)
   1243 			printf("%s: parity error detected\n",
   1244 			    sea->sc_dev.dv_xname);
   1245 		if (!(tmp & STAT_BSY)) {
   1246 			for (loop = 0; loop < 20; loop++)
   1247 				if ((tmp = STATUS) & STAT_BSY)
   1248 					break;
   1249 			if (!(tmp & STAT_BSY)) {
   1250 				printf("%s: !STAT_BSY unit in data transfer!\n",
   1251 				    sea->sc_dev.dv_xname);
   1252 				s = splbio();
   1253 				sea->nexus = NULL;
   1254 				scb->flags = SCB_ERROR;
   1255 				splx(s);
   1256 				sea_done(sea, scb);
   1257 				return;
   1258 			}
   1259 		}
   1260 
   1261 		/* we only have a valid SCSI phase when REQ is asserted */
   1262 		if (!(tmp & STAT_REQ))
   1263 			continue;
   1264 
   1265 		if (sea->type == FDOMAIN840)
   1266 			tmp = ((tmp & 0x08) >> 2) |
   1267 			      ((tmp & 0x02) << 2) |
   1268 			       (tmp & 0xf5);
   1269 		phase = tmp & PH_MASK;
   1270 		if (phase != old_phase)
   1271 			old_phase = phase;
   1272 
   1273 		switch (phase) {
   1274 		case PH_DATAOUT:
   1275 #ifdef SEA_NODATAOUT
   1276 			printf("%s: SEA_NODATAOUT set, attempted DATAOUT aborted\n",
   1277 			    sea->sc_dev.dv_xname);
   1278 			msgout = MSG_ABORT;
   1279 			CONTROL = BASE_CMD | CMD_ATTN;
   1280 			break;
   1281 #endif
   1282 		case PH_DATAIN:
   1283 			if (!scb->data)
   1284 				printf("no data address!\n");
   1285 #ifdef SEA_BLINDTRANSFER
   1286 			if (scb->datalen && !(scb->datalen % BLOCK_SIZE)) {
   1287 				while (scb->datalen) {
   1288 					for (loop = 0; loop < 50000; loop++)
   1289 						if ((tmp = STATUS) & STAT_REQ)
   1290 							break;
   1291 					if (!(tmp & STAT_REQ)) {
   1292 						printf("%s: timeout waiting for STAT_REQ\n",
   1293 						    sea->sc_dev.dv_xname);
   1294 						/* XXX Do something? */
   1295 					}
   1296 					if (sea->type == FDOMAIN840)
   1297 						tmp = ((tmp & 0x08) >> 2) |
   1298 						      ((tmp & 0x02) << 2) |
   1299 						       (tmp & 0xf5);
   1300 					if ((tmp & PH_MASK) != phase)
   1301 						break;
   1302 					if (!(phase & STAT_IO)) {
   1303 #ifdef SEA_ASSEMBLER
   1304 						asm("shr $2, %%ecx\n\t\
   1305 						    cld\n\t\
   1306 						    rep\n\t\
   1307 						    movsl" :
   1308 						    "=S" (scb->data) :
   1309 						    "0" (scb->data),
   1310 						    "D" (sea->maddr_dr),
   1311 						    "c" (BLOCK_SIZE) :
   1312 						    "%ecx", "%edi");
   1313 #else
   1314 						for (count = 0;
   1315 						    count < BLOCK_SIZE;
   1316 						    count++)
   1317 							DATA = *(scb->data++);
   1318 #endif
   1319 					} else {
   1320 #ifdef SEA_ASSEMBLER
   1321 						asm("shr $2, %%ecx\n\t\
   1322 						    cld\n\t\
   1323 						    rep\n\t\
   1324 						    movsl" :
   1325 						    "=D" (scb->data) :
   1326 						    "S" (sea->maddr_dr),
   1327 						    "0" (scb->data),
   1328 						    "c" (BLOCK_SIZE) :
   1329 						    "%ecx", "%esi");
   1330 #else
   1331 					        for (count = 0;
   1332 						    count < BLOCK_SIZE;
   1333 						    count++)
   1334 							*(scb->data++) = DATA;
   1335 #endif
   1336 					}
   1337 					scb->datalen -= BLOCK_SIZE;
   1338 				}
   1339 			}
   1340 #endif
   1341 			if (scb->datalen)
   1342 				sea_transfer_pio(sea, &phase, &scb->datalen,
   1343 				    &scb->data);
   1344 			break;
   1345 		case PH_MSGIN:
   1346 			/* Multibyte messages should not be present here. */
   1347 			len = 1;
   1348 			data = &tmp;
   1349 			sea_transfer_pio(sea, &phase, &len, &data);
   1350 			/* scb->MessageIn = tmp; */
   1351 
   1352 			switch (tmp) {
   1353 			case MSG_ABORT:
   1354 				scb->flags = SCB_ABORTED;
   1355 				printf("sea: command aborted by target\n");
   1356 				CONTROL = BASE_CMD;
   1357 				sea_done(sea, scb);
   1358 				return;
   1359 			case MSG_CMDCOMPLETE:
   1360 				s = splbio();
   1361 				sea->nexus = NULL;
   1362 				splx(s);
   1363 				sea->busy[scb->xs->sc_link->target] &=
   1364 				    ~(1 << scb->xs->sc_link->lun);
   1365 				CONTROL = BASE_CMD;
   1366 				sea_done(sea, scb);
   1367 				return;
   1368 			case MSG_MESSAGE_REJECT:
   1369 				printf("%s: message_reject recieved\n",
   1370 				    sea->sc_dev.dv_xname);
   1371 				break;
   1372 			case MSG_DISCONNECT:
   1373 				s = splbio();
   1374 				TAILQ_INSERT_TAIL(&sea->nexus_list,
   1375 				    scb, chain);
   1376 				sea->nexus = NULL;
   1377 				CONTROL = BASE_CMD;
   1378 				splx(s);
   1379 				return;
   1380 			case MSG_SAVEDATAPOINTER:
   1381 			case MSG_RESTOREPOINTERS:
   1382 				/* save/restore of pointers are ignored */
   1383 				break;
   1384 			default:
   1385 				/*
   1386 				 * This should be handled in the pio data
   1387 				 * transfer phase, as the ATN should be raised
   1388 				 * before ACK goes false when rejecting a
   1389 				 * message.
   1390 				 */
   1391 				printf("%s: unknown message in: %x\n",
   1392 				    sea->sc_dev.dv_xname, tmp);
   1393 				break;
   1394 			} /* switch (tmp) */
   1395 			break;
   1396 		case PH_MSGOUT:
   1397 			len = 1;
   1398 			data = &msgout;
   1399 			/* sea->last_message = msgout; */
   1400 			sea_transfer_pio(sea, &phase, &len, &data);
   1401 			if (msgout == MSG_ABORT) {
   1402 				printf("%s: sent message abort to target\n",
   1403 				    sea->sc_dev.dv_xname);
   1404 				s = splbio();
   1405 				sea->busy[scb->xs->sc_link->target] &=
   1406 				    ~(1 << scb->xs->sc_link->lun);
   1407 				sea->nexus = NULL;
   1408 				scb->flags = SCB_ABORTED;
   1409 				splx(s);
   1410 				/* enable interrupt from scsi */
   1411 				sea_done(sea, scb);
   1412 				return;
   1413 			}
   1414 			msgout = MSG_NOOP;
   1415 			break;
   1416 		case PH_CMD:
   1417 			len = scb->xs->cmdlen;
   1418 			data = (char *) scb->xs->cmd;
   1419 			sea_transfer_pio(sea, &phase, &len, &data);
   1420 			break;
   1421 		case PH_STAT:
   1422 			len = 1;
   1423 			data = &tmp;
   1424 			sea_transfer_pio(sea, &phase, &len, &data);
   1425 			scb->xs->status = tmp;
   1426 			break;
   1427 		default:
   1428 			printf("sea: unknown phase\n");
   1429 		} /* switch (phase) */
   1430 	} /* for (...) */
   1431 
   1432 	/* If we get here we have got a timeout! */
   1433 	printf("%s: timeout in data transfer\n", sea->sc_dev.dv_xname);
   1434 	scb->flags = SCB_TIMEOUT;
   1435 	/* XXX Should I clear scsi-bus state? */
   1436 	sea_done(sea, scb);
   1437 }
   1438