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