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