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