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