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