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