Home | History | Annotate | Line # | Download | only in isa
seagate.c revision 1.10
      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 
    305 struct cfdriver seacd = {
    306 	NULL, "sea", seaprobe, seaattach, DV_DULL, sizeof(struct sea_softc)
    307 };
    308 
    309 #ifdef SEA_DEBUGQUEUE
    310 void
    311 sea_queue_length(sea)
    312 	struct sea_softc *sea;
    313 {
    314 	struct sea_scb *scb;
    315 	int connected, issued, disconnected;
    316 
    317 	connected = sea->nexus ? 1 : 0;
    318 	for (scb = sea->ready_list.tqh_first, issued = 0; scb;
    319 	    scb = scb->chain.tqe_next, issued++);
    320 	for (scb = sea->nexus_list.tqh_first, disconnected = 0; scb;
    321 	    scb = scb->chain.tqe_next, disconnected++);
    322 	printf("%s: length: %d/%d/%d\n", sea->sc_dev.dv_xname, connected,
    323 	    issued, disconnected);
    324 }
    325 #endif
    326 
    327 /*
    328  * Check if the device can be found at the port given and if so, detect the
    329  * type the type of board.  Set it up ready for further work. Takes the isa_dev
    330  * structure from autoconf as an argument.
    331  * Returns 1 if card recognized, 0 if errors.
    332  */
    333 int
    334 seaprobe(parent, match, aux)
    335 	struct device *parent;
    336 	void *match, *aux;
    337 {
    338 	struct sea_softc *sea = match;
    339 	struct isa_attach_args *ia = aux;
    340 	int i;
    341 
    342 	/*
    343 	 * Could try to find a board by looking through all possible addresses.
    344 	 * This is not done the right way now, because I have not found a way
    345 	 * to get a boards virtual memory address given its physical.  There is
    346 	 * a function that returns the physical address for a given virtual
    347 	 * address, but not the other way around.
    348 	 */
    349 
    350 	if (ia->ia_maddr == MADDRUNK) {
    351 		/* XXX */
    352 		return 0;
    353 	} else
    354 		sea->maddr = ISA_HOLE_VADDR(ia->ia_maddr);
    355 
    356 	/* check board type */	/* No way to define this through config */
    357 	for (i = 0; i < nsignatures; i++)
    358 		if (!memcmp(sea->maddr + signatures[i].offset,
    359 		    signatures[i].signature, signatures[i].length)) {
    360 			sea->type = signatures[i].type;
    361 			break;
    362 		}
    363 
    364 	/* Find controller and data memory addresses */
    365 	switch (sea->type) {
    366 	case SEAGATE:
    367 	case FDOMAIN840:
    368 		sea->maddr_cr_sr =
    369 		    (void *) (((u_char *)sea->maddr) + 0x1a00);
    370 		sea->maddr_dr =
    371 		    (void *) (((u_char *)sea->maddr) + 0x1c00);
    372 		break;
    373 	case FDOMAIN:
    374 		sea->maddr_cr_sr =
    375 		    (void *) (((u_char *)sea->maddr) + 0x1c00);
    376 		sea->maddr_dr =
    377 		    (void *) (((u_char *)sea->maddr) + 0x1e00);
    378 		break;
    379 	default:
    380 #ifdef DIAGNOSTIC
    381 		printf("%s: board type unknown at address 0x%lx\n",
    382 		    sea->sc_dev.dv_xname, sea->maddr);
    383 #endif
    384 		return 0;
    385 	}
    386 
    387 	/* Test controller RAM (works the same way on future domain cards?) */
    388 	*((u_char *)sea->maddr + SEA_RAMOFFSET) = 0xa5;
    389 	*((u_char *)sea->maddr + SEA_RAMOFFSET + 1) = 0x5a;
    390 
    391 	if ((*((u_char *)sea->maddr + SEA_RAMOFFSET) != 0xa5) ||
    392 	    (*((u_char *)sea->maddr + SEA_RAMOFFSET + 1) != 0x5a)) {
    393 		printf("%s: board RAM failure\n", sea->sc_dev.dv_xname);
    394 		return 0;
    395 	}
    396 
    397 	ia->ia_drq = DRQUNK;
    398 	ia->ia_msize = 0x2000;
    399 	ia->ia_iosize = 0;
    400 	return 1;
    401 }
    402 
    403 seaprint()
    404 {
    405 
    406 }
    407 
    408 /*
    409  * Attach all sub-devices we can find
    410  */
    411 void
    412 seaattach(parent, self, aux)
    413 	struct device *parent, *self;
    414 	void *aux;
    415 {
    416 	struct isa_attach_args *ia = aux;
    417 	struct sea_softc *sea = (void *)self;
    418 
    419 	sea_init(sea);
    420 
    421 	/*
    422 	 * fill in the prototype scsi_link.
    423 	 */
    424 	sea->sc_link.adapter_softc = sea;
    425 	sea->sc_link.adapter_target = sea->our_id;
    426 	sea->sc_link.adapter = &sea_switch;
    427 	sea->sc_link.device = &sea_dev;
    428 	sea->sc_link.openings = 1;
    429 
    430 	printf("\n");
    431 
    432 #ifdef NEWCONFIG
    433 	isa_establish(&sea->sc_id, &sea->sc_deV);
    434 #endif
    435 	sea->sc_ih = isa_intr_establish(ia->ia_irq, ISA_IST_EDGE, ISA_IPL_BIO,
    436 	    seaintr, sea);
    437 
    438 	/*
    439 	 * ask the adapter what subunits are present
    440 	 */
    441 	config_found(self, &sea->sc_link, seaprint);
    442 }
    443 
    444 /*
    445  * Catch an interrupt from the adaptor
    446  */
    447 int
    448 seaintr(arg)
    449 	void *arg;
    450 {
    451 	struct sea_softc *sea = arg;
    452 
    453 #ifdef DEBUG	/* extra overhead, and only needed for intr debugging */
    454 	if ((STATUS & STAT_PARITY) == 0 &&
    455 	    (STATUS & (STAT_SEL | STAT_IO)) != (STAT_SEL | STAT_IO))
    456 		return 0;
    457 #endif
    458 
    459 loop:
    460 	/* dispatch to appropriate routine if found and done=0 */
    461 	/* should check to see that this card really caused the interrupt */
    462 
    463 	if (STATUS & STAT_PARITY) {
    464 		/* Parity error interrupt */
    465 		printf("%s: parity error\n", sea->sc_dev.dv_xname);
    466 		return 1;
    467 	}
    468 
    469 	if ((STATUS & (STAT_SEL | STAT_IO)) == (STAT_SEL | STAT_IO)) {
    470 		/* Reselect interrupt */
    471 		sea_reselect(sea);
    472 		if (!main_running)
    473 			sea_main();
    474 		goto loop;
    475 	}
    476 
    477 	return 1;
    478 }
    479 
    480 /*
    481  * Setup data structures, and reset the board and the SCSI bus.
    482  */
    483 void
    484 sea_init(sea)
    485 	struct sea_softc *sea;
    486 {
    487 	int i;
    488 
    489 	/* Reset the scsi bus (I don't know if this is needed */
    490 	CONTROL = BASE_CMD | CMD_DRVR_ENABLE | CMD_RST;
    491 	delay(25);	/* hold reset for at least 25 microseconds */
    492 	CONTROL = BASE_CMD;
    493 	delay(10); 	/* wait a Bus Clear Delay (800 ns + bus free delay (800 ns) */
    494 
    495 	/* Set our id (don't know anything about this) */
    496 	switch (sea->type) {
    497 	case SEAGATE:
    498 		sea->our_id = 7;
    499 		break;
    500 	case FDOMAIN:
    501 	case FDOMAIN840:
    502 		sea->our_id = 6;
    503 		break;
    504 	}
    505 	sea->our_id_mask = 1 << sea->our_id;
    506 
    507 	/* init fields used by our routines */
    508 	sea->nexus = 0;
    509 	TAILQ_INIT(&sea->ready_list);
    510 	TAILQ_INIT(&sea->nexus_list);
    511 	TAILQ_INIT(&sea->free_list);
    512 	for (i = 0; i < 8; i++)
    513 		sea->busy[i] = 0x00;
    514 
    515 	/* link up the free list of scbs */
    516 	sea->numscbs = SCB_TABLE_SIZE;
    517 	for (i = 0; i < SCB_TABLE_SIZE; i++) {
    518 		TAILQ_INSERT_TAIL(&sea->free_list, &sea->scb[i], chain);
    519 	}
    520 }
    521 
    522 /*
    523  * start a scsi operation given the command and the data address. Also needs
    524  * the unit, target and lu.
    525  */
    526 int
    527 sea_scsi_cmd(xs)
    528 	struct scsi_xfer *xs;
    529 {
    530 	struct scsi_link *sc_link = xs->sc_link;
    531 	struct sea_softc *sea = sc_link->adapter_softc;
    532 	struct sea_scb *scb;
    533 	int flags;
    534 	int s;
    535 
    536 	SC_DEBUG(sc_link, SDEV_DB2, ("sea_scsi_cmd\n"));
    537 
    538 	flags = xs->flags;
    539 	if ((flags & (ITSDONE|INUSE)) != INUSE) {
    540 		printf("%s: done or not in use?\n", sea->sc_dev.dv_xname);
    541 		xs->flags &= ~ITSDONE;
    542 		xs->flags |= INUSE;
    543 	}
    544 	if ((scb = sea_get_scb(sea, flags)) == NULL) {
    545 		xs->error = XS_DRIVER_STUFFUP;
    546 		return TRY_AGAIN_LATER;
    547 	}
    548 	scb->flags = SCB_ACTIVE;
    549 	scb->xs = xs;
    550 
    551 	if (flags & SCSI_RESET) {
    552 		/*
    553 		 * Try to send a reset command to the card.
    554 		 * XXX Not implemented.
    555 		 */
    556 		printf("%s: resetting\n", sea->sc_dev.dv_xname);
    557 		xs->error = XS_DRIVER_STUFFUP;
    558 		return COMPLETE;
    559 	}
    560 
    561 	/*
    562 	 * Put all the arguments for the xfer in the scb
    563 	 */
    564 	scb->datalen = xs->datalen;
    565 	scb->data = xs->data;
    566 
    567 #ifdef SEA_DEBUGQUEUE
    568 	sea_queue_length(sea);
    569 #endif
    570 
    571 	s = splbio();
    572 
    573 	sea_send_scb(sea, scb);
    574 
    575 	/*
    576 	 * Usually return SUCCESSFULLY QUEUED
    577 	 */
    578 	if ((flags & SCSI_POLL) == 0) {
    579 		timeout(sea_timeout, scb, (xs->timeout * hz) / 1000);
    580 		splx(s);
    581 		return SUCCESSFULLY_QUEUED;
    582 	}
    583 
    584 	splx(s);
    585 
    586 	/*
    587 	 * If we can't use interrupts, poll on completion
    588 	 */
    589 	if (sea_poll(sea, xs, xs->timeout)) {
    590 		sea_timeout(scb);
    591 		if (sea_poll(sea, xs, 2000))
    592 			sea_timeout(scb);
    593 	}
    594 	return COMPLETE;
    595 }
    596 
    597 /*
    598  * Get a free scb. If there are none, see if we can allocate a new one.  If so,
    599  * put it in the hash table too; otherwise return an error or sleep.
    600  */
    601 struct sea_scb *
    602 sea_get_scb(sea, flags)
    603 	struct sea_softc *sea;
    604 	int flags;
    605 {
    606 	int s;
    607 	struct sea_scb *scb;
    608 
    609 	s = splbio();
    610 
    611 	/*
    612 	 * If we can and have to, sleep waiting for one to come free
    613 	 * but only if we can't allocate a new one.
    614 	 */
    615 	for (;;) {
    616 		scb = sea->free_list.tqh_first;
    617 		if (scb) {
    618 			TAILQ_REMOVE(&sea->free_list, scb, chain);
    619 			break;
    620 		}
    621 		if (sea->numscbs < SEA_SCB_MAX) {
    622 			if (scb = (struct sea_scb *) malloc(sizeof(struct sea_scb),
    623 			    M_TEMP, M_NOWAIT)) {
    624 				bzero(scb, sizeof(struct sea_scb));
    625 				sea->numscbs++;
    626 			} else
    627 				printf("%s: can't malloc scb\n",
    628 				    sea->sc_dev.dv_xname);
    629 			break;
    630 		}
    631 		if ((flags & SCSI_NOSLEEP) != 0)
    632 			break;
    633 		tsleep(&sea->free_list, PRIBIO, "seascb", 0);
    634 	}
    635 
    636 	splx(s);
    637 	return scb;
    638 }
    639 
    640 /*
    641  * Try to send this command to the board. Because this board does not use any
    642  * mailboxes, this routine simply adds the command to the queue held by the
    643  * sea_softc structure.
    644  * A check is done to see if the command contains a REQUEST_SENSE command, and
    645  * if so the command is put first in the queue, otherwise the command is added
    646  * to the end of the queue. ?? Not correct ??
    647  */
    648 void
    649 sea_send_scb(sea, scb)
    650 	struct sea_softc *sea;
    651 	struct sea_scb *scb;
    652 {
    653 
    654 	TAILQ_INSERT_TAIL(&sea->ready_list, scb, chain);
    655 	/* Try to do some work on the card. */
    656 	if (!main_running)
    657 		sea_main();
    658 }
    659 
    660 /*
    661  * Coroutine that runs as long as more work can be done on the seagate host
    662  * adapter in a system.  Both sea_scsi_cmd and sea_intr will try to start it in
    663  * case it is not running.
    664  */
    665 void
    666 sea_main()
    667 {
    668 	struct sea_softc *sea;
    669 	struct sea_scb *scb;
    670 	int done;
    671 	int unit;
    672 	int s;
    673 
    674 	main_running = 1;
    675 
    676 	/*
    677 	 * This should not be run with interrupts disabled, but use the splx
    678 	 * code instead.
    679 	 */
    680 loop:
    681 	done = 1;
    682 	for (unit = 0; unit < seacd.cd_ndevs; unit++) {
    683 		sea = seacd.cd_devs[unit];
    684 		if (!sea)
    685 			continue;
    686 		s = splbio();
    687 		if (!sea->nexus) {
    688 			/*
    689 			 * Search through the ready_list for a command
    690 			 * destined for a target that's not busy.
    691 			 */
    692 			for (scb = sea->ready_list.tqh_first; scb;
    693 			    scb = scb->chain.tqe_next) {
    694 				if (!(sea->busy[scb->xs->sc_link->target] &
    695 				    (1 << scb->xs->sc_link->lun))) {
    696 					TAILQ_REMOVE(&sea->ready_list, scb,
    697 					    chain);
    698 
    699 					/* Re-enable interrupts. */
    700 					splx(s);
    701 
    702 					/*
    703 					 * Attempt to establish an I_T_L nexus.
    704 					 * On success, sea->nexus is set.
    705 					 * On failure, we must add the command
    706 					 * back to the issue queue so we can
    707 					 * keep trying.
    708 					 */
    709 
    710 					/*
    711 					 * REQUEST_SENSE commands are issued
    712 					 * without tagged queueing, even on
    713 					 * SCSI-II devices because the
    714 					 * contingent alligence condition
    715 					 * exists for the entire unit.
    716 					 */
    717 
    718 					/*
    719 					 * First check that if any device has
    720 					 * tried a reconnect while we have done
    721 					 * other things with interrupts
    722 					 * disabled.
    723 					 */
    724 
    725 					if ((STATUS & (STAT_SEL | STAT_IO)) ==
    726 					    (STAT_SEL | STAT_IO)) {
    727 						sea_reselect(sea);
    728 						break;
    729 					}
    730 					if (sea_select(sea, scb)) {
    731 						s = splbio();
    732 						TAILQ_INSERT_HEAD(&sea->ready_list,
    733 						    scb, chain);
    734 						splx(s);
    735 					} else
    736 						break;
    737 				} /* if target/lun is not busy */
    738 			} /* for scb */
    739 			if (!sea->nexus) {
    740 				/* check for reselection phase */
    741 				if ((STATUS & (STAT_SEL | STAT_IO)) ==
    742 				    (STAT_SEL | STAT_IO)) {
    743 					sea_reselect(sea);
    744 				}
    745 			}
    746 		} /* if (!sea->nexus) */
    747 
    748 		splx(s);
    749 		if (sea->nexus) {	/* we are connected. Do the task */
    750 			sea_information_transfer(sea);
    751 			done = 0;
    752 		} else
    753 			break;
    754 	} /* for instance */
    755 
    756 	if (!done)
    757 		goto loop;
    758 
    759 	main_running = 0;
    760 }
    761 
    762 void
    763 sea_free_scb(sea, scb, flags)
    764 	struct sea_softc *sea;
    765 	struct sea_scb *scb;
    766 	int flags;
    767 {
    768 	int s;
    769 
    770 	s = splbio();
    771 
    772 	scb->flags = SCB_FREE;
    773 	TAILQ_INSERT_HEAD(&sea->free_list, scb, chain);
    774 
    775 	/*
    776 	 * If there were none, wake anybody waiting for one to come free,
    777 	 * starting with queued entries.
    778 	 */
    779 	if (!scb->chain.tqe_next)
    780 		wakeup((caddr_t)&sea->free_list);
    781 
    782 	splx(s);
    783 }
    784 
    785 void
    786 sea_timeout(arg)
    787 	void *arg;
    788 {
    789 	struct sea_scb *scb = arg;
    790 	struct scsi_xfer *xs = scb->xs;
    791 	struct scsi_link *sc_link = xs->sc_link;
    792 	struct sea_softc *sea = sc_link->adapter_softc;
    793 	int s;
    794 
    795 	sc_print_addr(sc_link);
    796 	printf("timed out");
    797 
    798 	s = splbio();
    799 
    800 	/*
    801 	 * If it has been through before, then
    802 	 * a previous abort has failed, don't
    803 	 * try abort again
    804 	 */
    805 	if (scb->flags & SCB_ABORTED) {
    806 		/* abort timed out */
    807 		printf(" AGAIN\n");
    808 	 	scb->xs->retries = 0;
    809 		scb->flags |= SCB_ABORTED;
    810 		sea_done(sea, scb);
    811 	} else {
    812 		/* abort the operation that has timed out */
    813 		printf("\n");
    814 		scb->flags |= SCB_ABORTED;
    815 		sea_abort(sea, scb);
    816 		/* 2 secs for the abort */
    817 		if ((xs->flags & SCSI_POLL) == 0)
    818 			timeout(sea_timeout, scb, 2 * hz);
    819 	}
    820 
    821 	splx(s);
    822 }
    823 
    824 void
    825 sea_reselect(sea)
    826 	struct sea_softc *sea;
    827 {
    828 	u_char target_mask;
    829 	int i;
    830 	u_char lun, phase;
    831 	u_char msg[3];
    832 	int len;
    833 	u_char *data;
    834 	struct sea_scb *scb;
    835 	int abort = 0;
    836 
    837 	if (!((target_mask = STATUS) & STAT_SEL)) {
    838 		printf("%s: wrong state 0x%x\n", sea->sc_dev.dv_xname,
    839 		    target_mask);
    840 		return;
    841 	}
    842 
    843 	/* wait for a device to win the reselection phase */
    844 	/* signals this by asserting the I/O signal */
    845 	for (i = 10; i && (STATUS & (STAT_SEL | STAT_IO | STAT_BSY)) !=
    846 	    (STAT_SEL | STAT_IO | 0); i--);
    847 	/* !! Check for timeout here */
    848 	/* the data bus contains original initiator id ORed with target id */
    849 	target_mask = DATA;
    850 	/* see that we really are the initiator */
    851 	if (!(target_mask & sea->our_id_mask)) {
    852 		printf("%s: polled reselection was not for me: 0x%x\n",
    853 		    sea->sc_dev.dv_xname, target_mask);
    854 		return;
    855 	}
    856 	/* find target who won */
    857 	target_mask &= ~sea->our_id_mask;
    858 	/* host responds by asserting the BSY signal */
    859 	CONTROL = BASE_CMD | CMD_DRVR_ENABLE | CMD_BSY;
    860 	/* target should respond by deasserting the SEL signal */
    861 	for (i = 50000; i && (STATUS & STAT_SEL); i++);
    862 	/* remove the busy status */
    863 	CONTROL = BASE_CMD | CMD_DRVR_ENABLE;
    864 	/* we are connected. Now we wait for the MSGIN condition */
    865 	for (i = 50000; i && !(STATUS & STAT_REQ); i--);
    866 	/* !! Add timeout check here */
    867 	/* hope we get an IDENTIFY message */
    868 	len = 3;
    869 	data = msg;
    870 	phase = PH_MSGIN;
    871 	sea_transfer_pio(sea, &phase, &len, &data);
    872 
    873 	if (MSG_ISIDENTIFY(msg[0])) {
    874 		printf("%s: expecting IDENTIFY message, got 0x%x\n",
    875 		    sea->sc_dev.dv_xname, msg[0]);
    876 		abort = 1;
    877 	} else {
    878 		lun = msg[0] & 0x07;
    879 
    880 		/*
    881 		 * Find the command corresponding to the I_T_L or I_T_L_Q nexus
    882 		 * we just reestablished, and remove it from the disconnected
    883 		 * queue.
    884 		 */
    885 		for (scb = sea->nexus_list.tqh_first; scb;
    886 		    scb = scb->chain.tqe_next)
    887 			if (target_mask == (1 << scb->xs->sc_link->target) &&
    888 			    lun == scb->xs->sc_link->lun) {
    889 				TAILQ_REMOVE(&sea->nexus_list, scb,
    890 				    chain);
    891 				break;
    892 			}
    893 		if (!scb) {
    894 			printf("%s: target %02x lun %d not disconnected\n",
    895 			    sea->sc_dev.dv_xname, target_mask, lun);
    896 			/*
    897 			 * Since we have an established nexus that we can't do
    898 			 * anything with, we must abort it.
    899 			 */
    900 			abort = 1;
    901 		}
    902 	}
    903 
    904 	if (abort) {
    905 		msg[0] = MSG_ABORT;
    906 		len = 1;
    907 		data = msg;
    908 		phase = PH_MSGOUT;
    909 		CONTROL = BASE_CMD | CMD_ATTN;
    910 		sea_transfer_pio(sea, &phase, &len, &data);
    911 	} else
    912 		sea->nexus = scb;
    913 
    914 	return;
    915 }
    916 
    917 /*
    918  * Transfer data in given phase using polled I/O.
    919  */
    920 int
    921 sea_transfer_pio(sea, phase, count, data)
    922 	struct sea_softc *sea;
    923 	u_char *phase;
    924 	int *count;
    925 	u_char **data;
    926 {
    927 	register u_char p = *phase, tmp;
    928 	register int c = *count;
    929 	register u_char *d = *data;
    930 	int timeout;
    931 
    932 	do {
    933 		/*
    934 		 * Wait for assertion of REQ, after which the phase bits will
    935 		 * be valid.
    936 		 */
    937 		for (timeout = 0; timeout < 50000; timeout++)
    938 			if ((tmp = STATUS) & STAT_REQ)
    939 				break;
    940 		if (!(tmp & STAT_REQ)) {
    941 			printf("%s: timeout waiting for STAT_REQ\n",
    942 			    sea->sc_dev.dv_xname);
    943 			break;
    944 		}
    945 
    946 		/*
    947 		 * Check for phase mismatch.  Reached if the target decides
    948 		 * that it has finished the transfer.
    949 		 */
    950 		if (sea->type == FDOMAIN840)
    951 			tmp = ((tmp & 0x08) >> 2) |
    952 			      ((tmp & 0x02) << 2) |
    953 			       (tmp & 0xf5);
    954 		if ((tmp & PH_MASK) != p)
    955 			break;
    956 
    957 		/* Do actual transfer from SCSI bus to/from memory. */
    958 		if (!(p & STAT_IO))
    959 			DATA = *d;
    960 		else
    961 			*d = DATA;
    962 		++d;
    963 
    964 		/*
    965 		 * The SCSI standard suggests that in MSGOUT phase, the
    966 		 * initiator should drop ATN on the last byte of the message
    967 		 * phase after REQ has been asserted for the handshake but
    968 		 * before the initiator raises ACK.
    969 		 * Don't know how to accomplish this on the ST01/02.
    970 		 */
    971 
    972 #if 0
    973 		/*
    974 		 * XXX
    975 		 * The st01 code doesn't wait for STAT_REQ to be deasserted.
    976 		 * Is this ok?
    977 		 */
    978 		for (timeout = 0; timeout < 200000L; timeout++)
    979 			if (!(STATUS & STAT_REQ))
    980 				break;
    981 		if (STATUS & STAT_REQ)
    982 			printf("%s: timeout on wait for !STAT_REQ",
    983 			    sea->sc_dev.dv_xname);
    984 #endif
    985 	} while (--c);
    986 
    987 	*count = c;
    988 	*data = d;
    989 	tmp = STATUS;
    990 	if (tmp & STAT_REQ)
    991 		*phase = tmp & PH_MASK;
    992 	else
    993 		*phase = PH_INVALID;
    994 
    995 	if (c && (*phase != p))
    996 		return -1;
    997 	return 0;
    998 }
    999 
   1000 /*
   1001  * Establish I_T_L or I_T_L_Q nexus for new or existing command including
   1002  * ARBITRATION, SELECTION, and initial message out for IDENTIFY and queue
   1003  * messages.  Return -1 if selection could not execute for some reason, 0 if
   1004  * selection succeded or failed because the target did not respond.
   1005  */
   1006 int
   1007 sea_select(sea, scb)
   1008 	struct sea_softc *sea;
   1009 	struct sea_scb *scb;
   1010 {
   1011 	u_char msg[3], phase;
   1012 	u_char *data;
   1013 	int len;
   1014 	int timeout;
   1015 
   1016 	CONTROL = BASE_CMD;
   1017 	DATA = sea->our_id_mask;
   1018 	CONTROL = (BASE_CMD & ~CMD_INTR) | CMD_START_ARB;
   1019 
   1020 	/* wait for arbitration to complete */
   1021 	for (timeout = 0; timeout < 3000000L; timeout++)
   1022 		if (STATUS & STAT_ARB_CMPL)
   1023 			break;
   1024 	if (!(STATUS & STAT_ARB_CMPL)) {
   1025 		if (STATUS & STAT_SEL) {
   1026 			printf("%s: arbitration lost\n", sea->sc_dev.dv_xname);
   1027 			scb->flags |= SCB_ERROR;
   1028 		} else {
   1029 			printf("%s: arbitration timeout\n",
   1030 			    sea->sc_dev.dv_xname);
   1031 			scb->flags |= SCB_TIMEOUT;
   1032 		}
   1033 		CONTROL = BASE_CMD;
   1034 		return -1;
   1035 	}
   1036 
   1037 	delay(2);
   1038 	DATA = (u_char)((1 << scb->xs->sc_link->target) | sea->our_id_mask);
   1039 	CONTROL =
   1040 #ifdef SEA_NOMSGS
   1041 	    (BASE_CMD & ~CMD_INTR) | CMD_DRVR_ENABLE | CMD_SEL;
   1042 #else
   1043 	    (BASE_CMD & ~CMD_INTR) | CMD_DRVR_ENABLE | CMD_SEL | CMD_ATTN;
   1044 #endif
   1045 	delay(1);
   1046 
   1047 	/* wait for a bsy from target */
   1048 	for (timeout = 0; timeout < 2000000L; timeout++)
   1049 		if (STATUS & STAT_BSY)
   1050 			break;
   1051 	if (!(STATUS & STAT_BSY)) {
   1052 		/* should return some error to the higher level driver */
   1053 		CONTROL = BASE_CMD;
   1054 		scb->flags |= SCB_TIMEOUT;
   1055 		return 0;
   1056 	}
   1057 
   1058 	/* Try to make the target to take a message from us */
   1059 #ifdef SEA_NOMSGS
   1060 	CONTROL = (BASE_CMD & ~CMD_INTR) | CMD_DRVR_ENABLE;
   1061 #else
   1062 	CONTROL = (BASE_CMD & ~CMD_INTR) | CMD_DRVR_ENABLE | CMD_ATTN;
   1063 #endif
   1064 	delay(1);
   1065 
   1066 	/* should start a msg_out phase */
   1067 	for (timeout = 0; timeout < 2000000L; timeout++)
   1068 		if (STATUS & STAT_REQ)
   1069 			break;
   1070 	/* Remove ATN. */
   1071 	CONTROL = BASE_CMD | CMD_DRVR_ENABLE;
   1072 	if (!(STATUS & STAT_REQ)) {
   1073 		/*
   1074 		 * This should not be taken as an error, but more like an
   1075 		 * unsupported feature!  Should set a flag indicating that the
   1076 		 * target don't support messages, and continue without failure.
   1077 		 * (THIS IS NOT AN ERROR!)
   1078 		 */
   1079 	} else {
   1080 		msg[0] = MSG_IDENTIFY(scb->xs->sc_link->lun, 1);
   1081 		len = 1;
   1082 		data = msg;
   1083 		phase = PH_MSGOUT;
   1084 		/* Should do test on result of sea_transfer_pio(). */
   1085 		sea_transfer_pio(sea, &phase, &len, &data);
   1086 	}
   1087 	if (!(STATUS & STAT_BSY))
   1088 		printf("%s: after successful arbitrate: no STAT_BSY!\n",
   1089 		    sea->sc_dev.dv_xname);
   1090 
   1091 	sea->nexus = scb;
   1092 	sea->busy[scb->xs->sc_link->target] |= 1 << scb->xs->sc_link->lun;
   1093 	/* This assignment should depend on possibility to send a message to target. */
   1094 	CONTROL = BASE_CMD | CMD_DRVR_ENABLE;
   1095 	/* XXX Reset pointer in command? */
   1096 	return 0;
   1097 }
   1098 
   1099 /*
   1100  * Send an abort to the target.  Return 1 success, 0 on failure.
   1101  */
   1102 int
   1103 sea_abort(sea, scb)
   1104 	struct sea_softc *sea;
   1105 	struct sea_scb *scb;
   1106 {
   1107 	struct sea_scb *tmp;
   1108 	u_char msg, phase, *msgptr;
   1109 	int len;
   1110 
   1111 	/*
   1112 	 * If the command hasn't been issued yet, we simply remove it from the
   1113 	 * issue queue
   1114 	 * XXX Could avoid this loop.
   1115 	 */
   1116 	for (tmp = sea->ready_list.tqh_first; tmp; tmp = tmp->chain.tqe_next)
   1117 		if (scb == tmp) {
   1118 			TAILQ_REMOVE(&sea->ready_list, scb, chain);
   1119 			/* XXX Set some type of error result for operation. */
   1120 			return 1;
   1121 		}
   1122 
   1123 	/*
   1124 	 * If any commands are connected, we're going to fail the abort and let
   1125 	 * the high level SCSI driver retry at a later time or issue a reset.
   1126 	 */
   1127 	if (sea->nexus)
   1128 		return 0;
   1129 
   1130 	/*
   1131 	 * If the command is currently disconnected from the bus, and there are
   1132 	 * no connected commands, we reconnect the I_T_L or I_T_L_Q nexus
   1133 	 * associated with it, go into message out, and send an abort message.
   1134 	 */
   1135 	for (tmp = sea->nexus_list.tqh_first; tmp;
   1136 	    tmp = tmp->chain.tqe_next)
   1137 		if (scb == tmp) {
   1138 			if (sea_select(sea, scb))
   1139 				return 0;
   1140 
   1141 			msg = MSG_ABORT;
   1142 			msgptr = &msg;
   1143 			len = 1;
   1144 			phase = PH_MSGOUT;
   1145 			CONTROL = BASE_CMD | CMD_ATTN;
   1146 			sea_transfer_pio(sea, &phase, &len, &msgptr);
   1147 
   1148 			for (tmp = sea->nexus_list.tqh_first; tmp;
   1149 			    tmp = tmp->chain.tqe_next)
   1150 				if (scb == tmp) {
   1151 					TAILQ_REMOVE(&sea->nexus_list,
   1152 					    scb, chain);
   1153 					/* XXX Set some type of error result
   1154 					   for the operation. */
   1155 					return 1;
   1156 				}
   1157 		}
   1158 
   1159 	/* Command not found in any queue; race condition? */
   1160 	return 1;
   1161 }
   1162 
   1163 void
   1164 sea_done(sea, scb)
   1165 	struct sea_softc *sea;
   1166 	struct sea_scb *scb;
   1167 {
   1168 	struct scsi_xfer *xs = scb->xs;
   1169 
   1170 	untimeout(sea_timeout, scb);
   1171 
   1172 	xs->resid = scb->datalen;
   1173 
   1174 	/* XXXX need to get status */
   1175 	if (scb->flags == SCB_ACTIVE) {
   1176 		xs->resid = 0;
   1177 	} else {
   1178 		if (scb->flags & (SCB_TIMEOUT | SCB_ABORTED))
   1179 			xs->error = XS_TIMEOUT;
   1180 		if (scb->flags & SCB_ERROR)
   1181 			xs->error = XS_DRIVER_STUFFUP;
   1182 	}
   1183 	xs->flags |= ITSDONE;
   1184 	sea_free_scb(sea, scb, xs->flags);
   1185 	scsi_done(xs);
   1186 }
   1187 
   1188 /*
   1189  * Wait for completion of command in polled mode.
   1190  */
   1191 int
   1192 sea_poll(sea, xs, count)
   1193 	struct sea_softc *sea;
   1194 	struct scsi_xfer *xs;
   1195 	int count;
   1196 {
   1197 	int s;
   1198 
   1199 	while (count) {
   1200 		/* try to do something */
   1201 		s = splbio();
   1202 		if (!main_running)
   1203 			sea_main();
   1204 		splx(s);
   1205 		if (xs->flags & ITSDONE)
   1206 			return 0;
   1207 		delay(1000);
   1208 		count--;
   1209 	}
   1210 	return 1;
   1211 }
   1212 
   1213 /*
   1214  * Do the transfer.  We know we are connected.  Update the flags, and call
   1215  * sea_done() when task accomplished.  Dialog controlled by the target.
   1216  */
   1217 void
   1218 sea_information_transfer(sea)
   1219 	struct sea_softc *sea;
   1220 {
   1221 	int timeout;
   1222 	u_char msgout = MSG_NOOP;
   1223 	int len;
   1224 	int s;
   1225 	u_char *data;
   1226 	u_char phase, tmp, old_phase = PH_INVALID;
   1227 	struct sea_scb *scb = sea->nexus;
   1228 	int loop;
   1229 
   1230 	for (timeout = 0; timeout < 10000000L; timeout++) {
   1231 		tmp = STATUS;
   1232 		if (tmp & STAT_PARITY)
   1233 			printf("%s: parity error detected\n",
   1234 			    sea->sc_dev.dv_xname);
   1235 		if (!(tmp & STAT_BSY)) {
   1236 			for (loop = 0; loop < 20; loop++)
   1237 				if ((tmp = STATUS) & STAT_BSY)
   1238 					break;
   1239 			if (!(tmp & STAT_BSY)) {
   1240 				printf("%s: !STAT_BSY unit in data transfer!\n",
   1241 				    sea->sc_dev.dv_xname);
   1242 				s = splbio();
   1243 				sea->nexus = NULL;
   1244 				scb->flags = SCB_ERROR;
   1245 				splx(s);
   1246 				sea_done(sea, scb);
   1247 				return;
   1248 			}
   1249 		}
   1250 
   1251 		/* we only have a valid SCSI phase when REQ is asserted */
   1252 		if (!(tmp & STAT_REQ))
   1253 			continue;
   1254 
   1255 		if (sea->type == FDOMAIN840)
   1256 			tmp = ((tmp & 0x08) >> 2) |
   1257 			      ((tmp & 0x02) << 2) |
   1258 			       (tmp & 0xf5);
   1259 		phase = tmp & PH_MASK;
   1260 		if (phase != old_phase)
   1261 			old_phase = phase;
   1262 
   1263 		switch (phase) {
   1264 		case PH_DATAOUT:
   1265 #ifdef SEA_NODATAOUT
   1266 			printf("%s: SEA_NODATAOUT set, attempted DATAOUT aborted\n",
   1267 			    sea->sc_dev.dv_xname);
   1268 			msgout = MSG_ABORT;
   1269 			CONTROL = BASE_CMD | CMD_ATTN;
   1270 			break;
   1271 #endif
   1272 		case PH_DATAIN:
   1273 			if (!scb->data)
   1274 				printf("no data address!\n");
   1275 #ifdef SEA_BLINDTRANSFER
   1276 			if (scb->datalen && !(scb->datalen % BLOCK_SIZE)) {
   1277 				while (scb->datalen) {
   1278 					for (loop = 0; loop < 50000; loop++)
   1279 						if ((tmp = STATUS) & STAT_REQ)
   1280 							break;
   1281 					if (!(tmp & STAT_REQ)) {
   1282 						printf("%s: timeout waiting for STAT_REQ\n",
   1283 						    sea->sc_dev.dv_xname);
   1284 						/* XXX Do something? */
   1285 					}
   1286 					if (sea->type == FDOMAIN840)
   1287 						tmp = ((tmp & 0x08) >> 2) |
   1288 						      ((tmp & 0x02) << 2) |
   1289 						       (tmp & 0xf5);
   1290 					if ((tmp & PH_MASK) != phase)
   1291 						break;
   1292 					if (!(phase & STAT_IO)) {
   1293 #ifdef SEA_ASSEMBLER
   1294 						asm("shr $2, %%ecx\n\t\
   1295 						    cld\n\t\
   1296 						    rep\n\t\
   1297 						    movsl" :
   1298 						    "=S" (scb->data) :
   1299 						    "0" (scb->data),
   1300 						    "D" (sea->maddr_dr),
   1301 						    "c" (BLOCK_SIZE) :
   1302 						    "%ecx", "%edi");
   1303 #else
   1304 						for (count = 0;
   1305 						    count < BLOCK_SIZE;
   1306 						    count++)
   1307 							DATA = *(scb->data++);
   1308 #endif
   1309 					} else {
   1310 #ifdef SEA_ASSEMBLER
   1311 						asm("shr $2, %%ecx\n\t\
   1312 						    cld\n\t\
   1313 						    rep\n\t\
   1314 						    movsl" :
   1315 						    "=D" (scb->data) :
   1316 						    "S" (sea->maddr_dr),
   1317 						    "0" (scb->data),
   1318 						    "c" (BLOCK_SIZE) :
   1319 						    "%ecx", "%esi");
   1320 #else
   1321 					        for (count = 0;
   1322 						    count < BLOCK_SIZE;
   1323 						    count++)
   1324 							*(scb->data++) = DATA;
   1325 #endif
   1326 					}
   1327 					scb->datalen -= BLOCK_SIZE;
   1328 				}
   1329 			}
   1330 #endif
   1331 			if (scb->datalen)
   1332 				sea_transfer_pio(sea, &phase, &scb->datalen,
   1333 				    &scb->data);
   1334 			break;
   1335 		case PH_MSGIN:
   1336 			/* Multibyte messages should not be present here. */
   1337 			len = 1;
   1338 			data = &tmp;
   1339 			sea_transfer_pio(sea, &phase, &len, &data);
   1340 			/* scb->MessageIn = tmp; */
   1341 
   1342 			switch (tmp) {
   1343 			case MSG_ABORT:
   1344 				scb->flags = SCB_ABORTED;
   1345 				printf("sea: command aborted by target\n");
   1346 				CONTROL = BASE_CMD;
   1347 				sea_done(sea, scb);
   1348 				return;
   1349 			case MSG_CMDCOMPLETE:
   1350 				s = splbio();
   1351 				sea->nexus = NULL;
   1352 				splx(s);
   1353 				sea->busy[scb->xs->sc_link->target] &=
   1354 				    ~(1 << scb->xs->sc_link->lun);
   1355 				CONTROL = BASE_CMD;
   1356 				sea_done(sea, scb);
   1357 				return;
   1358 			case MSG_MESSAGE_REJECT:
   1359 				printf("%s: message_reject recieved\n",
   1360 				    sea->sc_dev.dv_xname);
   1361 				break;
   1362 			case MSG_DISCONNECT:
   1363 				s = splbio();
   1364 				TAILQ_INSERT_TAIL(&sea->nexus_list,
   1365 				    scb, chain);
   1366 				sea->nexus = NULL;
   1367 				CONTROL = BASE_CMD;
   1368 				splx(s);
   1369 				return;
   1370 			case MSG_SAVEDATAPOINTER:
   1371 			case MSG_RESTOREPOINTERS:
   1372 				/* save/restore of pointers are ignored */
   1373 				break;
   1374 			default:
   1375 				/*
   1376 				 * This should be handled in the pio data
   1377 				 * transfer phase, as the ATN should be raised
   1378 				 * before ACK goes false when rejecting a
   1379 				 * message.
   1380 				 */
   1381 				printf("%s: unknown message in: %x\n",
   1382 				    sea->sc_dev.dv_xname, tmp);
   1383 				break;
   1384 			} /* switch (tmp) */
   1385 			break;
   1386 		case PH_MSGOUT:
   1387 			len = 1;
   1388 			data = &msgout;
   1389 			/* sea->last_message = msgout; */
   1390 			sea_transfer_pio(sea, &phase, &len, &data);
   1391 			if (msgout == MSG_ABORT) {
   1392 				printf("%s: sent message abort to target\n",
   1393 				    sea->sc_dev.dv_xname);
   1394 				s = splbio();
   1395 				sea->busy[scb->xs->sc_link->target] &=
   1396 				    ~(1 << scb->xs->sc_link->lun);
   1397 				sea->nexus = NULL;
   1398 				scb->flags = SCB_ABORTED;
   1399 				splx(s);
   1400 				/* enable interrupt from scsi */
   1401 				sea_done(sea, scb);
   1402 				return;
   1403 			}
   1404 			msgout = MSG_NOOP;
   1405 			break;
   1406 		case PH_CMD:
   1407 			len = scb->xs->cmdlen;
   1408 			data = (char *) scb->xs->cmd;
   1409 			sea_transfer_pio(sea, &phase, &len, &data);
   1410 			break;
   1411 		case PH_STAT:
   1412 			len = 1;
   1413 			data = &tmp;
   1414 			sea_transfer_pio(sea, &phase, &len, &data);
   1415 			scb->xs->status = tmp;
   1416 			break;
   1417 		default:
   1418 			printf("sea: unknown phase\n");
   1419 		} /* switch (phase) */
   1420 	} /* for (...) */
   1421 
   1422 	/* If we get here we have got a timeout! */
   1423 	printf("%s: timeout in data transfer\n", sea->sc_dev.dv_xname);
   1424 	scb->flags = SCB_TIMEOUT;
   1425 	/* XXX Should I clear scsi-bus state? */
   1426 	sea_done(sea, scb);
   1427 }
   1428