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