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