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