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