Home | History | Annotate | Line # | Download | only in isa
seagate.c revision 1.35
      1 /*	$NetBSD: seagate.c,v 1.35 2000/03/23 07:01:35 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 		callout_reset(&scb->xs->xs_callout, (xs->timeout * hz) / 1000,
    603 		    sea_timeout, scb);
    604 		splx(s);
    605 		return SUCCESSFULLY_QUEUED;
    606 	}
    607 
    608 	splx(s);
    609 
    610 	/*
    611 	 * If we can't use interrupts, poll on completion
    612 	 */
    613 	if (sea_poll(sea, xs, xs->timeout)) {
    614 		sea_timeout(scb);
    615 		if (sea_poll(sea, xs, 2000))
    616 			sea_timeout(scb);
    617 	}
    618 	return COMPLETE;
    619 }
    620 
    621 /*
    622  * Get a free scb. If there are none, see if we can allocate a new one.  If so,
    623  * put it in the hash table too; otherwise return an error or sleep.
    624  */
    625 struct sea_scb *
    626 sea_get_scb(sea, flags)
    627 	struct sea_softc *sea;
    628 	int flags;
    629 {
    630 	int s;
    631 	struct sea_scb *scb;
    632 
    633 	s = splbio();
    634 
    635 	/*
    636 	 * If we can and have to, sleep waiting for one to come free
    637 	 * but only if we can't allocate a new one.
    638 	 */
    639 	for (;;) {
    640 		scb = sea->free_list.tqh_first;
    641 		if (scb) {
    642 			TAILQ_REMOVE(&sea->free_list, scb, chain);
    643 			break;
    644 		}
    645 		if (sea->numscbs < SEA_SCB_MAX) {
    646 			scb = (struct sea_scb *) malloc(sizeof(struct sea_scb),
    647 			    M_TEMP, M_NOWAIT);
    648 			if (scb) {
    649 				bzero(scb, sizeof(struct sea_scb));
    650 				sea->numscbs++;
    651 			} else
    652 				printf("%s: can't malloc scb\n",
    653 				    sea->sc_dev.dv_xname);
    654 			break;
    655 		}
    656 		if ((flags & XS_CTL_NOSLEEP) != 0)
    657 			break;
    658 		tsleep(&sea->free_list, PRIBIO, "seascb", 0);
    659 	}
    660 
    661 	splx(s);
    662 	return scb;
    663 }
    664 
    665 /*
    666  * Try to send this command to the board. Because this board does not use any
    667  * mailboxes, this routine simply adds the command to the queue held by the
    668  * sea_softc structure.
    669  * A check is done to see if the command contains a REQUEST_SENSE command, and
    670  * if so the command is put first in the queue, otherwise the command is added
    671  * to the end of the queue. ?? Not correct ??
    672  */
    673 void
    674 sea_send_scb(sea, scb)
    675 	struct sea_softc *sea;
    676 	struct sea_scb *scb;
    677 {
    678 
    679 	TAILQ_INSERT_TAIL(&sea->ready_list, scb, chain);
    680 	/* Try to do some work on the card. */
    681 	if (!main_running)
    682 		sea_main();
    683 }
    684 
    685 /*
    686  * Coroutine that runs as long as more work can be done on the seagate host
    687  * adapter in a system.  Both sea_scsi_cmd and sea_intr will try to start it in
    688  * case it is not running.
    689  */
    690 void
    691 sea_main()
    692 {
    693 	struct sea_softc *sea;
    694 	struct sea_scb *scb;
    695 	int done;
    696 	int unit;
    697 	int s;
    698 
    699 	main_running = 1;
    700 
    701 	/*
    702 	 * This should not be run with interrupts disabled, but use the splx
    703 	 * code instead.
    704 	 */
    705 loop:
    706 	done = 1;
    707 	for (unit = 0; unit < sea_cd.cd_ndevs; unit++) {
    708 		sea = sea_cd.cd_devs[unit];
    709 		if (!sea)
    710 			continue;
    711 		s = splbio();
    712 		if (!sea->nexus) {
    713 			/*
    714 			 * Search through the ready_list for a command
    715 			 * destined for a target that's not busy.
    716 			 */
    717 			for (scb = sea->ready_list.tqh_first; scb;
    718 			    scb = scb->chain.tqe_next) {
    719 				if (!(sea->busy[scb->xs->sc_link->scsipi_scsi.target] &
    720 				    (1 << scb->xs->sc_link->scsipi_scsi.lun))) {
    721 					TAILQ_REMOVE(&sea->ready_list, scb,
    722 					    chain);
    723 
    724 					/* Re-enable interrupts. */
    725 					splx(s);
    726 
    727 					/*
    728 					 * Attempt to establish an I_T_L nexus.
    729 					 * On success, sea->nexus is set.
    730 					 * On failure, we must add the command
    731 					 * back to the issue queue so we can
    732 					 * keep trying.
    733 					 */
    734 
    735 					/*
    736 					 * REQUEST_SENSE commands are issued
    737 					 * without tagged queueing, even on
    738 					 * SCSI-II devices because the
    739 					 * contingent alligence condition
    740 					 * exists for the entire unit.
    741 					 */
    742 
    743 					/*
    744 					 * First check that if any device has
    745 					 * tried a reconnect while we have done
    746 					 * other things with interrupts
    747 					 * disabled.
    748 					 */
    749 
    750 					if ((STATUS & (STAT_SEL | STAT_IO)) ==
    751 					    (STAT_SEL | STAT_IO)) {
    752 						sea_reselect(sea);
    753 						break;
    754 					}
    755 					if (sea_select(sea, scb)) {
    756 						s = splbio();
    757 						TAILQ_INSERT_HEAD(&sea->ready_list,
    758 						    scb, chain);
    759 						splx(s);
    760 					} else
    761 						break;
    762 				} /* if target/lun is not busy */
    763 			} /* for scb */
    764 			if (!sea->nexus) {
    765 				/* check for reselection phase */
    766 				if ((STATUS & (STAT_SEL | STAT_IO)) ==
    767 				    (STAT_SEL | STAT_IO)) {
    768 					sea_reselect(sea);
    769 				}
    770 			}
    771 		} /* if (!sea->nexus) */
    772 
    773 		splx(s);
    774 		if (sea->nexus) {	/* we are connected. Do the task */
    775 			sea_information_transfer(sea);
    776 			done = 0;
    777 		} else
    778 			break;
    779 	} /* for instance */
    780 
    781 	if (!done)
    782 		goto loop;
    783 
    784 	main_running = 0;
    785 }
    786 
    787 void
    788 sea_free_scb(sea, scb, flags)
    789 	struct sea_softc *sea;
    790 	struct sea_scb *scb;
    791 	int flags;
    792 {
    793 	int s;
    794 
    795 	s = splbio();
    796 
    797 	scb->flags = SCB_FREE;
    798 	TAILQ_INSERT_HEAD(&sea->free_list, scb, chain);
    799 
    800 	/*
    801 	 * If there were none, wake anybody waiting for one to come free,
    802 	 * starting with queued entries.
    803 	 */
    804 	if (!scb->chain.tqe_next)
    805 		wakeup((caddr_t)&sea->free_list);
    806 
    807 	splx(s);
    808 }
    809 
    810 void
    811 sea_timeout(arg)
    812 	void *arg;
    813 {
    814 	struct sea_scb *scb = arg;
    815 	struct scsipi_xfer *xs = scb->xs;
    816 	struct scsipi_link *sc_link = xs->sc_link;
    817 	struct sea_softc *sea = sc_link->adapter_softc;
    818 	int s;
    819 
    820 	scsi_print_addr(sc_link);
    821 	printf("timed out");
    822 
    823 	s = splbio();
    824 
    825 	/*
    826 	 * If it has been through before, then
    827 	 * a previous abort has failed, don't
    828 	 * try abort again
    829 	 */
    830 	if (scb->flags & SCB_ABORTED) {
    831 		/* abort timed out */
    832 		printf(" AGAIN\n");
    833 	 	scb->xs->retries = 0;
    834 		scb->flags |= SCB_ABORTED;
    835 		sea_done(sea, scb);
    836 	} else {
    837 		/* abort the operation that has timed out */
    838 		printf("\n");
    839 		scb->flags |= SCB_ABORTED;
    840 		sea_abort(sea, scb);
    841 		/* 2 secs for the abort */
    842 		if ((xs->xs_control & XS_CTL_POLL) == 0)
    843 			callout_reset(&scb->xs->xs_callout, 2 * hz,
    844 			    sea_timeout, scb);
    845 	}
    846 
    847 	splx(s);
    848 }
    849 
    850 void
    851 sea_reselect(sea)
    852 	struct sea_softc *sea;
    853 {
    854 	u_char target_mask;
    855 	int i;
    856 	u_char lun, phase;
    857 	u_char msg[3];
    858 	int len;
    859 	u_char *data;
    860 	struct sea_scb *scb;
    861 	int abort = 0;
    862 
    863 	if (!((target_mask = STATUS) & STAT_SEL)) {
    864 		printf("%s: wrong state 0x%x\n", sea->sc_dev.dv_xname,
    865 		    target_mask);
    866 		return;
    867 	}
    868 
    869 	/* wait for a device to win the reselection phase */
    870 	/* signals this by asserting the I/O signal */
    871 	for (i = 10; i && (STATUS & (STAT_SEL | STAT_IO | STAT_BSY)) !=
    872 	    (STAT_SEL | STAT_IO | 0); i--);
    873 	/* !! Check for timeout here */
    874 	/* the data bus contains original initiator id ORed with target id */
    875 	target_mask = DATA;
    876 	/* see that we really are the initiator */
    877 	if (!(target_mask & sea->our_id_mask)) {
    878 		printf("%s: polled reselection was not for me: 0x%x\n",
    879 		    sea->sc_dev.dv_xname, target_mask);
    880 		return;
    881 	}
    882 	/* find target who won */
    883 	target_mask &= ~sea->our_id_mask;
    884 	/* host responds by asserting the BSY signal */
    885 	CONTROL = BASE_CMD | CMD_DRVR_ENABLE | CMD_BSY;
    886 	/* target should respond by deasserting the SEL signal */
    887 	for (i = 50000; i && (STATUS & STAT_SEL); i++);
    888 	/* remove the busy status */
    889 	CONTROL = BASE_CMD | CMD_DRVR_ENABLE;
    890 	/* we are connected. Now we wait for the MSGIN condition */
    891 	for (i = 50000; i && !(STATUS & STAT_REQ); i--);
    892 	/* !! Add timeout check here */
    893 	/* hope we get an IDENTIFY message */
    894 	len = 3;
    895 	data = msg;
    896 	phase = PH_MSGIN;
    897 	sea_transfer_pio(sea, &phase, &len, &data);
    898 
    899 	if (MSG_ISIDENTIFY(msg[0])) {
    900 		printf("%s: expecting IDENTIFY message, got 0x%x\n",
    901 		    sea->sc_dev.dv_xname, msg[0]);
    902 		abort = 1;
    903 		scb = NULL;
    904 	} else {
    905 		lun = msg[0] & 0x07;
    906 
    907 		/*
    908 		 * Find the command corresponding to the I_T_L or I_T_L_Q nexus
    909 		 * we just reestablished, and remove it from the disconnected
    910 		 * queue.
    911 		 */
    912 		for (scb = sea->nexus_list.tqh_first; scb;
    913 		    scb = scb->chain.tqe_next)
    914 			if (target_mask == (1 << scb->xs->sc_link->scsipi_scsi.target) &&
    915 			    lun == scb->xs->sc_link->scsipi_scsi.lun) {
    916 				TAILQ_REMOVE(&sea->nexus_list, scb,
    917 				    chain);
    918 				break;
    919 			}
    920 		if (!scb) {
    921 			printf("%s: target %02x lun %d not disconnected\n",
    922 			    sea->sc_dev.dv_xname, target_mask, lun);
    923 			/*
    924 			 * Since we have an established nexus that we can't do
    925 			 * anything with, we must abort it.
    926 			 */
    927 			abort = 1;
    928 		}
    929 	}
    930 
    931 	if (abort) {
    932 		msg[0] = MSG_ABORT;
    933 		len = 1;
    934 		data = msg;
    935 		phase = PH_MSGOUT;
    936 		CONTROL = BASE_CMD | CMD_ATTN;
    937 		sea_transfer_pio(sea, &phase, &len, &data);
    938 	} else
    939 		sea->nexus = scb;
    940 
    941 	return;
    942 }
    943 
    944 /*
    945  * Transfer data in given phase using polled I/O.
    946  */
    947 int
    948 sea_transfer_pio(sea, phase, count, data)
    949 	struct sea_softc *sea;
    950 	u_char *phase;
    951 	int *count;
    952 	u_char **data;
    953 {
    954 	register u_char p = *phase, tmp;
    955 	register int c = *count;
    956 	register u_char *d = *data;
    957 	int timeout;
    958 
    959 	do {
    960 		/*
    961 		 * Wait for assertion of REQ, after which the phase bits will
    962 		 * be valid.
    963 		 */
    964 		for (timeout = 0; timeout < 50000; timeout++)
    965 			if ((tmp = STATUS) & STAT_REQ)
    966 				break;
    967 		if (!(tmp & STAT_REQ)) {
    968 			printf("%s: timeout waiting for STAT_REQ\n",
    969 			    sea->sc_dev.dv_xname);
    970 			break;
    971 		}
    972 
    973 		/*
    974 		 * Check for phase mismatch.  Reached if the target decides
    975 		 * that it has finished the transfer.
    976 		 */
    977 		if (sea->type == FDOMAIN840)
    978 			tmp = ((tmp & 0x08) >> 2) |
    979 			      ((tmp & 0x02) << 2) |
    980 			       (tmp & 0xf5);
    981 		if ((tmp & PH_MASK) != p)
    982 			break;
    983 
    984 		/* Do actual transfer from SCSI bus to/from memory. */
    985 		if (!(p & STAT_IO))
    986 			DATA = *d;
    987 		else
    988 			*d = DATA;
    989 		++d;
    990 
    991 		/*
    992 		 * The SCSI standard suggests that in MSGOUT phase, the
    993 		 * initiator should drop ATN on the last byte of the message
    994 		 * phase after REQ has been asserted for the handshake but
    995 		 * before the initiator raises ACK.
    996 		 * Don't know how to accomplish this on the ST01/02.
    997 		 */
    998 
    999 #if 0
   1000 		/*
   1001 		 * XXX
   1002 		 * The st01 code doesn't wait for STAT_REQ to be deasserted.
   1003 		 * Is this ok?
   1004 		 */
   1005 		for (timeout = 0; timeout < 200000L; timeout++)
   1006 			if (!(STATUS & STAT_REQ))
   1007 				break;
   1008 		if (STATUS & STAT_REQ)
   1009 			printf("%s: timeout on wait for !STAT_REQ",
   1010 			    sea->sc_dev.dv_xname);
   1011 #endif
   1012 	} while (--c);
   1013 
   1014 	*count = c;
   1015 	*data = d;
   1016 	tmp = STATUS;
   1017 	if (tmp & STAT_REQ)
   1018 		*phase = tmp & PH_MASK;
   1019 	else
   1020 		*phase = PH_INVALID;
   1021 
   1022 	if (c && (*phase != p))
   1023 		return -1;
   1024 	return 0;
   1025 }
   1026 
   1027 /*
   1028  * Establish I_T_L or I_T_L_Q nexus for new or existing command including
   1029  * ARBITRATION, SELECTION, and initial message out for IDENTIFY and queue
   1030  * messages.  Return -1 if selection could not execute for some reason, 0 if
   1031  * selection succeded or failed because the target did not respond.
   1032  */
   1033 int
   1034 sea_select(sea, scb)
   1035 	struct sea_softc *sea;
   1036 	struct sea_scb *scb;
   1037 {
   1038 	u_char msg[3], phase;
   1039 	u_char *data;
   1040 	int len;
   1041 	int timeout;
   1042 
   1043 	CONTROL = BASE_CMD;
   1044 	DATA = sea->our_id_mask;
   1045 	CONTROL = (BASE_CMD & ~CMD_INTR) | CMD_START_ARB;
   1046 
   1047 	/* wait for arbitration to complete */
   1048 	for (timeout = 0; timeout < 3000000L; timeout++)
   1049 		if (STATUS & STAT_ARB_CMPL)
   1050 			break;
   1051 	if (!(STATUS & STAT_ARB_CMPL)) {
   1052 		if (STATUS & STAT_SEL) {
   1053 			printf("%s: arbitration lost\n", sea->sc_dev.dv_xname);
   1054 			scb->flags |= SCB_ERROR;
   1055 		} else {
   1056 			printf("%s: arbitration timeout\n",
   1057 			    sea->sc_dev.dv_xname);
   1058 			scb->flags |= SCB_TIMEOUT;
   1059 		}
   1060 		CONTROL = BASE_CMD;
   1061 		return -1;
   1062 	}
   1063 
   1064 	delay(2);
   1065 	DATA = (u_char)((1 << scb->xs->sc_link->scsipi_scsi.target) |
   1066 		sea->our_id_mask);
   1067 	CONTROL =
   1068 #ifdef SEA_NOMSGS
   1069 	    (BASE_CMD & ~CMD_INTR) | CMD_DRVR_ENABLE | CMD_SEL;
   1070 #else
   1071 	    (BASE_CMD & ~CMD_INTR) | CMD_DRVR_ENABLE | CMD_SEL | CMD_ATTN;
   1072 #endif
   1073 	delay(1);
   1074 
   1075 	/* wait for a bsy from target */
   1076 	for (timeout = 0; timeout < 2000000L; timeout++)
   1077 		if (STATUS & STAT_BSY)
   1078 			break;
   1079 	if (!(STATUS & STAT_BSY)) {
   1080 		/* should return some error to the higher level driver */
   1081 		CONTROL = BASE_CMD;
   1082 		scb->flags |= SCB_TIMEOUT;
   1083 		return 0;
   1084 	}
   1085 
   1086 	/* Try to make the target to take a message from us */
   1087 #ifdef SEA_NOMSGS
   1088 	CONTROL = (BASE_CMD & ~CMD_INTR) | CMD_DRVR_ENABLE;
   1089 #else
   1090 	CONTROL = (BASE_CMD & ~CMD_INTR) | CMD_DRVR_ENABLE | CMD_ATTN;
   1091 #endif
   1092 	delay(1);
   1093 
   1094 	/* should start a msg_out phase */
   1095 	for (timeout = 0; timeout < 2000000L; timeout++)
   1096 		if (STATUS & STAT_REQ)
   1097 			break;
   1098 	/* Remove ATN. */
   1099 	CONTROL = BASE_CMD | CMD_DRVR_ENABLE;
   1100 	if (!(STATUS & STAT_REQ)) {
   1101 		/*
   1102 		 * This should not be taken as an error, but more like an
   1103 		 * unsupported feature!  Should set a flag indicating that the
   1104 		 * target don't support messages, and continue without failure.
   1105 		 * (THIS IS NOT AN ERROR!)
   1106 		 */
   1107 	} else {
   1108 		msg[0] = MSG_IDENTIFY(scb->xs->sc_link->scsipi_scsi.lun, 1);
   1109 		len = 1;
   1110 		data = msg;
   1111 		phase = PH_MSGOUT;
   1112 		/* Should do test on result of sea_transfer_pio(). */
   1113 		sea_transfer_pio(sea, &phase, &len, &data);
   1114 	}
   1115 	if (!(STATUS & STAT_BSY))
   1116 		printf("%s: after successful arbitrate: no STAT_BSY!\n",
   1117 		    sea->sc_dev.dv_xname);
   1118 
   1119 	sea->nexus = scb;
   1120 	sea->busy[scb->xs->sc_link->scsipi_scsi.target] |=
   1121 						1 << scb->xs->sc_link->scsipi_scsi.lun;
   1122 	/* This assignment should depend on possibility to send a message to target. */
   1123 	CONTROL = BASE_CMD | CMD_DRVR_ENABLE;
   1124 	/* XXX Reset pointer in command? */
   1125 	return 0;
   1126 }
   1127 
   1128 /*
   1129  * Send an abort to the target.  Return 1 success, 0 on failure.
   1130  */
   1131 int
   1132 sea_abort(sea, scb)
   1133 	struct sea_softc *sea;
   1134 	struct sea_scb *scb;
   1135 {
   1136 	struct sea_scb *tmp;
   1137 	u_char msg, phase, *msgptr;
   1138 	int len;
   1139 
   1140 	/*
   1141 	 * If the command hasn't been issued yet, we simply remove it from the
   1142 	 * issue queue
   1143 	 * XXX Could avoid this loop.
   1144 	 */
   1145 	for (tmp = sea->ready_list.tqh_first; tmp; tmp = tmp->chain.tqe_next)
   1146 		if (scb == tmp) {
   1147 			TAILQ_REMOVE(&sea->ready_list, scb, chain);
   1148 			/* XXX Set some type of error result for operation. */
   1149 			return 1;
   1150 		}
   1151 
   1152 	/*
   1153 	 * If any commands are connected, we're going to fail the abort and let
   1154 	 * the high level SCSI driver retry at a later time or issue a reset.
   1155 	 */
   1156 	if (sea->nexus)
   1157 		return 0;
   1158 
   1159 	/*
   1160 	 * If the command is currently disconnected from the bus, and there are
   1161 	 * no connected commands, we reconnect the I_T_L or I_T_L_Q nexus
   1162 	 * associated with it, go into message out, and send an abort message.
   1163 	 */
   1164 	for (tmp = sea->nexus_list.tqh_first; tmp;
   1165 	    tmp = tmp->chain.tqe_next)
   1166 		if (scb == tmp) {
   1167 			if (sea_select(sea, scb))
   1168 				return 0;
   1169 
   1170 			msg = MSG_ABORT;
   1171 			msgptr = &msg;
   1172 			len = 1;
   1173 			phase = PH_MSGOUT;
   1174 			CONTROL = BASE_CMD | CMD_ATTN;
   1175 			sea_transfer_pio(sea, &phase, &len, &msgptr);
   1176 
   1177 			for (tmp = sea->nexus_list.tqh_first; tmp;
   1178 			    tmp = tmp->chain.tqe_next)
   1179 				if (scb == tmp) {
   1180 					TAILQ_REMOVE(&sea->nexus_list,
   1181 					    scb, chain);
   1182 					/* XXX Set some type of error result
   1183 					   for the operation. */
   1184 					return 1;
   1185 				}
   1186 		}
   1187 
   1188 	/* Command not found in any queue; race condition? */
   1189 	return 1;
   1190 }
   1191 
   1192 void
   1193 sea_done(sea, scb)
   1194 	struct sea_softc *sea;
   1195 	struct sea_scb *scb;
   1196 {
   1197 	struct scsipi_xfer *xs = scb->xs;
   1198 
   1199 	callout_stop(&scb->xs->xs_callout);
   1200 
   1201 	xs->resid = scb->datalen;
   1202 
   1203 	/* XXXX need to get status */
   1204 	if (scb->flags == SCB_ACTIVE) {
   1205 		xs->resid = 0;
   1206 	} else {
   1207 		if (scb->flags & (SCB_TIMEOUT | SCB_ABORTED))
   1208 			xs->error = XS_TIMEOUT;
   1209 		if (scb->flags & SCB_ERROR)
   1210 			xs->error = XS_DRIVER_STUFFUP;
   1211 	}
   1212 	xs->xs_status |= XS_STS_DONE;
   1213 	sea_free_scb(sea, scb, xs->xs_control);
   1214 	scsipi_done(xs);
   1215 }
   1216 
   1217 /*
   1218  * Wait for completion of command in polled mode.
   1219  */
   1220 int
   1221 sea_poll(sea, xs, count)
   1222 	struct sea_softc *sea;
   1223 	struct scsipi_xfer *xs;
   1224 	int count;
   1225 {
   1226 	int s;
   1227 
   1228 	while (count) {
   1229 		/* try to do something */
   1230 		s = splbio();
   1231 		if (!main_running)
   1232 			sea_main();
   1233 		splx(s);
   1234 		if (xs->xs_status & XS_STS_DONE)
   1235 			return 0;
   1236 		delay(1000);
   1237 		count--;
   1238 	}
   1239 	return 1;
   1240 }
   1241 
   1242 /*
   1243  * Do the transfer.  We know we are connected.  Update the flags, and call
   1244  * sea_done() when task accomplished.  Dialog controlled by the target.
   1245  */
   1246 void
   1247 sea_information_transfer(sea)
   1248 	struct sea_softc *sea;
   1249 {
   1250 	int timeout;
   1251 	u_char msgout = MSG_NOOP;
   1252 	int len;
   1253 	int s;
   1254 	u_char *data;
   1255 	u_char phase, tmp, old_phase = PH_INVALID;
   1256 	struct sea_scb *scb = sea->nexus;
   1257 	int loop;
   1258 
   1259 	for (timeout = 0; timeout < 10000000L; timeout++) {
   1260 		tmp = STATUS;
   1261 		if (tmp & STAT_PARITY)
   1262 			printf("%s: parity error detected\n",
   1263 			    sea->sc_dev.dv_xname);
   1264 		if (!(tmp & STAT_BSY)) {
   1265 			for (loop = 0; loop < 20; loop++)
   1266 				if ((tmp = STATUS) & STAT_BSY)
   1267 					break;
   1268 			if (!(tmp & STAT_BSY)) {
   1269 				printf("%s: !STAT_BSY unit in data transfer!\n",
   1270 				    sea->sc_dev.dv_xname);
   1271 				s = splbio();
   1272 				sea->nexus = NULL;
   1273 				scb->flags = SCB_ERROR;
   1274 				splx(s);
   1275 				sea_done(sea, scb);
   1276 				return;
   1277 			}
   1278 		}
   1279 
   1280 		/* we only have a valid SCSI phase when REQ is asserted */
   1281 		if (!(tmp & STAT_REQ))
   1282 			continue;
   1283 
   1284 		if (sea->type == FDOMAIN840)
   1285 			tmp = ((tmp & 0x08) >> 2) |
   1286 			      ((tmp & 0x02) << 2) |
   1287 			       (tmp & 0xf5);
   1288 		phase = tmp & PH_MASK;
   1289 		if (phase != old_phase)
   1290 			old_phase = phase;
   1291 
   1292 		switch (phase) {
   1293 		case PH_DATAOUT:
   1294 #ifdef SEA_NODATAOUT
   1295 			printf("%s: SEA_NODATAOUT set, attempted DATAOUT aborted\n",
   1296 			    sea->sc_dev.dv_xname);
   1297 			msgout = MSG_ABORT;
   1298 			CONTROL = BASE_CMD | CMD_ATTN;
   1299 			break;
   1300 #endif
   1301 		case PH_DATAIN:
   1302 			if (!scb->data)
   1303 				printf("no data address!\n");
   1304 #ifdef SEA_BLINDTRANSFER
   1305 			if (scb->datalen && !(scb->datalen % BLOCK_SIZE)) {
   1306 				while (scb->datalen) {
   1307 					for (loop = 0; loop < 50000; loop++)
   1308 						if ((tmp = STATUS) & STAT_REQ)
   1309 							break;
   1310 					if (!(tmp & STAT_REQ)) {
   1311 						printf("%s: timeout waiting for STAT_REQ\n",
   1312 						    sea->sc_dev.dv_xname);
   1313 						/* XXX Do something? */
   1314 					}
   1315 					if (sea->type == FDOMAIN840)
   1316 						tmp = ((tmp & 0x08) >> 2) |
   1317 						      ((tmp & 0x02) << 2) |
   1318 						       (tmp & 0xf5);
   1319 					if ((tmp & PH_MASK) != phase)
   1320 						break;
   1321 					if (!(phase & STAT_IO)) {
   1322 #ifdef SEA_ASSEMBLER
   1323 						asm("shr $2, %%ecx\n\t\
   1324 						    cld\n\t\
   1325 						    rep\n\t\
   1326 						    movsl" :
   1327 						    "=S" (scb->data) :
   1328 						    "0" (scb->data),
   1329 						    "D" (sea->maddr_dr),
   1330 						    "c" (BLOCK_SIZE) :
   1331 						    "%ecx", "%edi");
   1332 #else
   1333 						for (count = 0;
   1334 						    count < BLOCK_SIZE;
   1335 						    count++)
   1336 							DATA = *(scb->data++);
   1337 #endif
   1338 					} else {
   1339 #ifdef SEA_ASSEMBLER
   1340 						asm("shr $2, %%ecx\n\t\
   1341 						    cld\n\t\
   1342 						    rep\n\t\
   1343 						    movsl" :
   1344 						    "=D" (scb->data) :
   1345 						    "S" (sea->maddr_dr),
   1346 						    "0" (scb->data),
   1347 						    "c" (BLOCK_SIZE) :
   1348 						    "%ecx", "%esi");
   1349 #else
   1350 					        for (count = 0;
   1351 						    count < BLOCK_SIZE;
   1352 						    count++)
   1353 							*(scb->data++) = DATA;
   1354 #endif
   1355 					}
   1356 					scb->datalen -= BLOCK_SIZE;
   1357 				}
   1358 			}
   1359 #endif
   1360 			if (scb->datalen)
   1361 				sea_transfer_pio(sea, &phase, &scb->datalen,
   1362 				    &scb->data);
   1363 			break;
   1364 		case PH_MSGIN:
   1365 			/* Multibyte messages should not be present here. */
   1366 			len = 1;
   1367 			data = &tmp;
   1368 			sea_transfer_pio(sea, &phase, &len, &data);
   1369 			/* scb->MessageIn = tmp; */
   1370 
   1371 			switch (tmp) {
   1372 			case MSG_ABORT:
   1373 				scb->flags = SCB_ABORTED;
   1374 				printf("sea: command aborted by target\n");
   1375 				CONTROL = BASE_CMD;
   1376 				sea_done(sea, scb);
   1377 				return;
   1378 			case MSG_CMDCOMPLETE:
   1379 				s = splbio();
   1380 				sea->nexus = NULL;
   1381 				splx(s);
   1382 				sea->busy[scb->xs->sc_link->scsipi_scsi.target] &=
   1383 				    ~(1 << scb->xs->sc_link->scsipi_scsi.lun);
   1384 				CONTROL = BASE_CMD;
   1385 				sea_done(sea, scb);
   1386 				return;
   1387 			case MSG_MESSAGE_REJECT:
   1388 				printf("%s: message_reject recieved\n",
   1389 				    sea->sc_dev.dv_xname);
   1390 				break;
   1391 			case MSG_DISCONNECT:
   1392 				s = splbio();
   1393 				TAILQ_INSERT_TAIL(&sea->nexus_list,
   1394 				    scb, chain);
   1395 				sea->nexus = NULL;
   1396 				CONTROL = BASE_CMD;
   1397 				splx(s);
   1398 				return;
   1399 			case MSG_SAVEDATAPOINTER:
   1400 			case MSG_RESTOREPOINTERS:
   1401 				/* save/restore of pointers are ignored */
   1402 				break;
   1403 			default:
   1404 				/*
   1405 				 * This should be handled in the pio data
   1406 				 * transfer phase, as the ATN should be raised
   1407 				 * before ACK goes false when rejecting a
   1408 				 * message.
   1409 				 */
   1410 				printf("%s: unknown message in: %x\n",
   1411 				    sea->sc_dev.dv_xname, tmp);
   1412 				break;
   1413 			} /* switch (tmp) */
   1414 			break;
   1415 		case PH_MSGOUT:
   1416 			len = 1;
   1417 			data = &msgout;
   1418 			/* sea->last_message = msgout; */
   1419 			sea_transfer_pio(sea, &phase, &len, &data);
   1420 			if (msgout == MSG_ABORT) {
   1421 				printf("%s: sent message abort to target\n",
   1422 				    sea->sc_dev.dv_xname);
   1423 				s = splbio();
   1424 				sea->busy[scb->xs->sc_link->scsipi_scsi.target] &=
   1425 				    ~(1 << scb->xs->sc_link->scsipi_scsi.lun);
   1426 				sea->nexus = NULL;
   1427 				scb->flags = SCB_ABORTED;
   1428 				splx(s);
   1429 				/* enable interrupt from scsi */
   1430 				sea_done(sea, scb);
   1431 				return;
   1432 			}
   1433 			msgout = MSG_NOOP;
   1434 			break;
   1435 		case PH_CMD:
   1436 			len = scb->xs->cmdlen;
   1437 			data = (char *) scb->xs->cmd;
   1438 			sea_transfer_pio(sea, &phase, &len, &data);
   1439 			break;
   1440 		case PH_STAT:
   1441 			len = 1;
   1442 			data = &tmp;
   1443 			sea_transfer_pio(sea, &phase, &len, &data);
   1444 			scb->xs->status = tmp;
   1445 			break;
   1446 		default:
   1447 			printf("sea: unknown phase\n");
   1448 		} /* switch (phase) */
   1449 	} /* for (...) */
   1450 
   1451 	/* If we get here we have got a timeout! */
   1452 	printf("%s: timeout in data transfer\n", sea->sc_dev.dv_xname);
   1453 	scb->flags = SCB_TIMEOUT;
   1454 	/* XXX Should I clear scsi-bus state? */
   1455 	sea_done(sea, scb);
   1456 }
   1457