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