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