Home | History | Annotate | Line # | Download | only in isa
seagate.c revision 1.33
      1 /*	$NetBSD: seagate.c,v 1.33 1999/03/19 05:42:00 cgd Exp $	*/
      2 
      3 /*
      4  * ST01/02, Future Domain TMC-885, TMC-950 SCSI driver
      5  *
      6  * Copyright 1994, Charles M. Hannum (mycroft (at) ai.mit.edu)
      7  * Copyright 1994, Kent Palmkvist (kentp (at) isy.liu.se)
      8  * Copyright 1994, Robert Knier (rknier (at) qgraph.com)
      9  * Copyright 1992, 1994 Drew Eckhardt (drew (at) colorado.edu)
     10  * Copyright 1994, Julian Elischer (julian (at) tfs.com)
     11  *
     12  * Others that has contributed by example code is
     13  * 		Glen Overby (overby (at) cray.com)
     14  *		Tatu Yllnen
     15  *		Brian E Litzinger
     16  *
     17  * Redistribution and use in source and binary forms, with or without
     18  * modification, are permitted provided that the following conditions
     19  * are met:
     20  * 1. Redistributions of source code must retain the above copyright
     21  *    notice, this list of conditions and the following disclaimer.
     22  * 2. Redistributions in binary form must reproduce the above copyright
     23  *    notice, this list of conditions and the following disclaimer in the
     24  *    documentation and/or other materials provided with the distribution.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE DEVELOPERS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * kentp  940307 alpha version based on newscsi-03 version of Julians SCSI-code
     41  * kentp  940314 Added possibility to not use messages
     42  * rknier 940331 Added fast transfer code
     43  * rknier 940407 Added assembler coded data transfers
     44  */
     45 
     46 /*
     47  * What should really be done:
     48  *
     49  * Add missing tests for timeouts
     50  * Restructure interrupt enable/disable code (runs to long with int disabled)
     51  * Find bug? giving problem with tape status
     52  * Add code to handle Future Domain 840, 841, 880 and 881
     53  * adjust timeouts (startup is very slow)
     54  * add code to use tagged commands in SCSI2
     55  * Add code to handle slow devices better (sleep if device not disconnecting)
     56  * Fix unnecessary interrupts
     57  */
     58 
     59 /*
     60  * Note to users trying to share a disk between DOS and unix:
     61  * The ST01/02 is a translating host-adapter. It is not giving DOS
     62  * the same number of heads/tracks/sectors as specified by the disk.
     63  * It is therefore important to look at what numbers DOS thinks the
     64  * disk has. Use these to disklabel your disk in an appropriate manner
     65  */
     66 
     67 #include <sys/types.h>
     68 #include <sys/param.h>
     69 #include <sys/systm.h>
     70 #include <sys/kernel.h>
     71 #include <sys/errno.h>
     72 #include <sys/ioctl.h>
     73 #include <sys/device.h>
     74 #include <sys/buf.h>
     75 #include <sys/proc.h>
     76 #include <sys/user.h>
     77 #include <sys/queue.h>
     78 #include <sys/malloc.h>
     79 
     80 #include <machine/intr.h>
     81 #include <machine/pio.h>
     82 
     83 #include <dev/scsipi/scsi_all.h>
     84 #include <dev/scsipi/scsipi_all.h>
     85 #include <dev/scsipi/scsi_message.h>
     86 #include <dev/scsipi/scsiconf.h>
     87 
     88 #include <dev/isa/isareg.h>
     89 #include <dev/isa/isavar.h>	/* XXX USES ISA HOLE DIRECTLY */
     90 
     91 #define	SEA_SCB_MAX	32	/* allow maximally 8 scsi control blocks */
     92 #define SCB_TABLE_SIZE	8	/* start with 8 scb entries in table */
     93 #define BLOCK_SIZE	512	/* size of READ/WRITE areas on SCSI card */
     94 
     95 /*
     96  * defining SEA_BLINDTRANSFER will make DATA IN and DATA OUT to be done with
     97  * blind transfers, i.e. no check is done for scsi phase changes. This will
     98  * result in data loss if the scsi device does not send its data using
     99  * BLOCK_SIZE bytes at a time.
    100  * If SEA_BLINDTRANSFER defined and SEA_ASSEMBLER also defined will result in
    101  * the use of blind transfers coded in assembler. SEA_ASSEMBLER is no good
    102  * without SEA_BLINDTRANSFER defined.
    103  */
    104 #define	SEA_BLINDTRANSFER	/* do blind transfers */
    105 #define	SEA_ASSEMBLER		/* Use assembly code for fast transfers */
    106 
    107 /*
    108  * defining SEA_NOMSGS causes messages not to be used (thereby disabling
    109  * disconnects)
    110  */
    111 #undef	SEA_NOMSGS
    112 
    113 /*
    114  * defining SEA_NODATAOUT makes dataout phase being aborted
    115  */
    116 #undef	SEA_NODATAOUT
    117 
    118 /* Debugging definitions. Should not be used unless you want a lot of
    119    printouts even under normal conditions */
    120 
    121 #undef	SEA_DEBUGQUEUE		/* Display info about queue-lengths */
    122 
    123 /******************************* board definitions **************************/
    124 /*
    125  * CONTROL defines
    126  */
    127 #define CMD_RST		0x01		/* scsi reset */
    128 #define CMD_SEL		0x02		/* scsi select */
    129 #define CMD_BSY		0x04		/* scsi busy */
    130 #define	CMD_ATTN	0x08		/* scsi attention */
    131 #define CMD_START_ARB	0x10		/* start arbitration bit */
    132 #define	CMD_EN_PARITY	0x20		/* enable scsi parity generation */
    133 #define CMD_INTR	0x40		/* enable scsi interrupts */
    134 #define CMD_DRVR_ENABLE	0x80		/* scsi enable */
    135 
    136 /*
    137  * STATUS
    138  */
    139 #define STAT_BSY	0x01		/* scsi busy */
    140 #define STAT_MSG	0x02		/* scsi msg */
    141 #define STAT_IO		0x04		/* scsi I/O */
    142 #define STAT_CD		0x08		/* scsi C/D */
    143 #define STAT_REQ	0x10		/* scsi req */
    144 #define STAT_SEL	0x20		/* scsi select */
    145 #define STAT_PARITY	0x40		/* parity error bit */
    146 #define STAT_ARB_CMPL	0x80		/* arbitration complete bit */
    147 
    148 /*
    149  * REQUESTS
    150  */
    151 #define PH_DATAOUT	(0)
    152 #define PH_DATAIN	(STAT_IO)
    153 #define PH_CMD		(STAT_CD)
    154 #define PH_STAT		(STAT_CD | STAT_IO)
    155 #define PH_MSGOUT	(STAT_MSG | STAT_CD)
    156 #define PH_MSGIN	(STAT_MSG | STAT_CD | STAT_IO)
    157 
    158 #define PH_MASK		(STAT_MSG | STAT_CD | STAT_IO)
    159 
    160 #define PH_INVALID	0xff
    161 
    162 #define SEA_RAMOFFSET	0x00001800
    163 
    164 #define BASE_CMD	(CMD_INTR | CMD_EN_PARITY)
    165 
    166 #define	SEAGATE		1	/* Seagate ST0[12] */
    167 #define	FDOMAIN		2	/* Future Domain TMC-{885,950} */
    168 #define	FDOMAIN840	3	/* Future Domain TMC-{84[01],88[01]} */
    169 
    170 /******************************************************************************/
    171 
    172 /* scsi control block used to keep info about a scsi command */
    173 struct sea_scb {
    174         u_char *data;			/* position in data buffer so far */
    175 	int datalen;			/* bytes remaining to transfer */
    176 	TAILQ_ENTRY(sea_scb) chain;
    177 	struct scsipi_xfer *xs;		/* the scsipi_xfer for this cmd */
    178 	int flags;			/* status of the instruction */
    179 #define	SCB_FREE	0
    180 #define	SCB_ACTIVE	1
    181 #define SCB_ABORTED	2
    182 #define SCB_TIMEOUT	4
    183 #define SCB_ERROR	8
    184 };
    185 
    186 /*
    187  * data structure describing current status of the scsi bus. One for each
    188  * controller card.
    189  */
    190 struct sea_softc {
    191 	struct device sc_dev;
    192 	void *sc_ih;
    193 
    194 	int type;			/* board type */
    195 	caddr_t	maddr;			/* Base address for card */
    196 	caddr_t	maddr_cr_sr;		/* Address of control and status reg */
    197 	caddr_t	maddr_dr;		/* Address of data register */
    198 
    199 	struct scsipi_link sc_link;	/* prototype for subdevs */
    200 	struct scsipi_adapter sc_adapter;
    201 	TAILQ_HEAD(, sea_scb) free_list, ready_list, nexus_list;
    202 	struct sea_scb *nexus;		/* currently connected command */
    203 	int numscbs;			/* number of scsi control blocks */
    204 	struct sea_scb scb[SCB_TABLE_SIZE];
    205 
    206 	int our_id;			/* our scsi id */
    207 	u_char our_id_mask;
    208 	volatile u_char busy[8];	/* index=target, bit=lun, Keep track of
    209 					   busy luns at device target */
    210 };
    211 
    212 /* flag showing if main routine is running. */
    213 static volatile int main_running = 0;
    214 
    215 #define	STATUS	(*(volatile u_char *)sea->maddr_cr_sr)
    216 #define CONTROL	STATUS
    217 #define DATA	(*(volatile u_char *)sea->maddr_dr)
    218 
    219 /*
    220  * These are "special" values for the tag parameter passed to sea_select
    221  * Not implemented right now.
    222  */
    223 #define TAG_NEXT	-1	/* Use next free tag */
    224 #define TAG_NONE	-2	/*
    225 				 * Establish I_T_L nexus instead of I_T_L_Q
    226 				 * even on SCSI-II devices.
    227 				 */
    228 
    229 typedef struct {
    230 	char *signature;
    231 	int offset, length;
    232 	int type;
    233 } BiosSignature;
    234 
    235 /*
    236  * Signatures for automatic recognition of board type
    237  */
    238 static const BiosSignature signatures[] = {
    239 {"ST01 v1.7  (C) Copyright 1987 Seagate", 15, 37, SEAGATE},
    240 {"SCSI BIOS 2.00  (C) Copyright 1987 Seagate", 15, 40, SEAGATE},
    241 
    242 /*
    243  * The following two lines are NOT mistakes. One detects ROM revision
    244  * 3.0.0, the other 3.2. Since seagate has only one type of SCSI adapter,
    245  * and this is not going to change, the "SEAGATE" and "SCSI" together
    246  * are probably "good enough"
    247  */
    248 {"SEAGATE SCSI BIOS ", 16, 17, SEAGATE},
    249 {"SEAGATE SCSI BIOS ", 17, 17, SEAGATE},
    250 
    251 /*
    252  * However, future domain makes several incompatible SCSI boards, so specific
    253  * signatures must be used.
    254  */
    255 {"FUTURE DOMAIN CORP. (C) 1986-1989 V5.0C2/14/89", 5, 45, FDOMAIN},
    256 {"FUTURE DOMAIN CORP. (C) 1986-1989 V6.0A7/28/89", 5, 46, FDOMAIN},
    257 {"FUTURE DOMAIN CORP. (C) 1986-1990 V6.0105/31/90",5, 47, FDOMAIN},
    258 {"FUTURE DOMAIN CORP. (C) 1986-1990 V6.0209/18/90",5, 47, FDOMAIN},
    259 {"FUTURE DOMAIN CORP. (C) 1986-1990 V7.009/18/90", 5, 46, FDOMAIN},
    260 {"FUTURE DOMAIN CORP. (C) 1992 V8.00.004/02/92",   5, 44, FDOMAIN},
    261 {"FUTURE DOMAIN TMC-950",			   5, 21, FDOMAIN},
    262 };
    263 
    264 #define	nsignatures	(sizeof(signatures) / sizeof(signatures[0]))
    265 
    266 #ifdef notdef
    267 static const char *bases[] = {
    268 	(char *) 0xc8000, (char *) 0xca000, (char *) 0xcc000,
    269 	(char *) 0xce000, (char *) 0xdc000, (char *) 0xde000
    270 };
    271 
    272 #define	nbases		(sizeof(bases) / sizeof(bases[0]))
    273 #endif
    274 
    275 int seaintr __P((void *));
    276 int sea_scsi_cmd __P((struct scsipi_xfer *));
    277 void sea_timeout __P((void *));
    278 void sea_done __P((struct sea_softc *, struct sea_scb *));
    279 struct sea_scb *sea_get_scb __P((struct sea_softc *, int));
    280 void sea_free_scb __P((struct sea_softc *, struct sea_scb *, int));
    281 static void sea_main __P((void));
    282 static void sea_information_transfer __P((struct sea_softc *));
    283 int sea_poll __P((struct sea_softc *, struct scsipi_xfer *, int));
    284 void sea_init __P((struct sea_softc *));
    285 void sea_send_scb __P((struct sea_softc *sea, struct sea_scb *scb));
    286 void sea_reselect __P((struct sea_softc *sea));
    287 int sea_select __P((struct sea_softc *sea, struct sea_scb *scb));
    288 int sea_transfer_pio __P((struct sea_softc *sea, u_char *phase,
    289     int *count, u_char **data));
    290 int sea_abort __P((struct sea_softc *, struct sea_scb *scb));
    291 
    292 /* the below structure is so we have a default dev struct for our link struct */
    293 struct scsipi_device sea_dev = {
    294 	NULL,		/* use default error handler */
    295 	NULL,		/* have a queue, served by this */
    296 	NULL,		/* have no async handler */
    297 	NULL,		/* Use default 'done' routine */
    298 };
    299 
    300 int	seaprobe __P((struct device *, struct cfdata *, void *));
    301 void	seaattach __P((struct device *, struct device *, void *));
    302 
    303 struct cfattach sea_ca = {
    304 	sizeof(struct sea_softc), seaprobe, seaattach
    305 };
    306 
    307 extern struct cfdriver sea_cd;
    308 
    309 #ifdef SEA_DEBUGQUEUE
    310 void
    311 sea_queue_length(sea)
    312 	struct sea_softc *sea;
    313 {
    314 	struct sea_scb *scb;
    315 	int connected, issued, disconnected;
    316 
    317 	connected = sea->nexus ? 1 : 0;
    318 	for (scb = sea->ready_list.tqh_first, issued = 0; scb;
    319 	    scb = scb->chain.tqe_next, issued++);
    320 	for (scb = sea->nexus_list.tqh_first, disconnected = 0; scb;
    321 	    scb = scb->chain.tqe_next, disconnected++);
    322 	printf("%s: length: %d/%d/%d\n", sea->sc_dev.dv_xname, connected,
    323 	    issued, disconnected);
    324 }
    325 #endif
    326 
    327 /*
    328  * Check if the device can be found at the port given and if so, detect the
    329  * type the type of board.  Set it up ready for further work. Takes the isa_dev
    330  * structure from autoconf as an argument.
    331  * Returns 1 if card recognized, 0 if errors.
    332  */
    333 int
    334 seaprobe(parent, match, aux)
    335 	struct device *parent;
    336 	struct cfdata *match;
    337 	void *aux;
    338 {
    339 	struct isa_attach_args *ia = aux;
    340 	int i, type = 0;
    341 	caddr_t maddr;
    342 
    343 	/*
    344 	 * Could try to find a board by looking through all possible addresses.
    345 	 * This is not done the right way now, because I have not found a way
    346 	 * to get a boards virtual memory address given its physical.  There is
    347 	 * a function that returns the physical address for a given virtual
    348 	 * address, but not the other way around.
    349 	 */
    350 
    351 	if (ia->ia_maddr == MADDRUNK) {
    352 		/* XXX */
    353 		return 0;
    354 	} else
    355 		maddr = ISA_HOLE_VADDR(ia->ia_maddr);
    356 
    357 	/* check board type */	/* No way to define this through config */
    358 	for (i = 0; i < nsignatures; i++)
    359 		if (!bcmp(maddr + signatures[i].offset,
    360 		    signatures[i].signature, signatures[i].length)) {
    361 			type = signatures[i].type;
    362 			break;
    363 		}
    364 
    365 	/* Find controller and data memory addresses */
    366 	switch (type) {
    367 	case SEAGATE:
    368 	case FDOMAIN840:
    369 	case FDOMAIN:
    370 		break;
    371 	default:
    372 #ifdef DEBUG
    373 		printf("seaprobe: board type unknown at address 0x%x\n",
    374 		    ia->ia_maddr);
    375 #endif
    376 		return 0;
    377 	}
    378 
    379 	ia->ia_drq = DRQUNK;
    380 	ia->ia_msize = 0x2000;
    381 	ia->ia_iosize = 0;
    382 	return 1;
    383 }
    384 
    385 /*
    386  * Attach all sub-devices we can find
    387  */
    388 void
    389 seaattach(parent, self, aux)
    390 	struct device *parent, *self;
    391 	void *aux;
    392 {
    393 	struct isa_attach_args *ia = aux;
    394 	struct sea_softc *sea = (void *)self;
    395 	int i;
    396 
    397 	sea->maddr = ISA_HOLE_VADDR(ia->ia_maddr);
    398 
    399 	/* check board type */	/* No way to define this through config */
    400 	for (i = 0; i < nsignatures; i++)
    401 		if (!bcmp(sea->maddr + signatures[i].offset,
    402 		    signatures[i].signature, signatures[i].length)) {
    403 			sea->type = signatures[i].type;
    404 			break;
    405 		}
    406 
    407 	/* Find controller and data memory addresses */
    408 	switch (sea->type) {
    409 	case SEAGATE:
    410 	case FDOMAIN840:
    411 		sea->maddr_cr_sr =
    412 		    (void *) (((u_char *)sea->maddr) + 0x1a00);
    413 		sea->maddr_dr =
    414 		    (void *) (((u_char *)sea->maddr) + 0x1c00);
    415 		break;
    416 	case FDOMAIN:
    417 		sea->maddr_cr_sr =
    418 		    (void *) (((u_char *)sea->maddr) + 0x1c00);
    419 		sea->maddr_dr =
    420 		    (void *) (((u_char *)sea->maddr) + 0x1e00);
    421 		break;
    422 	default:
    423 #ifdef DEBUG
    424 		printf("%s: board type unknown at address 0x%x\n",
    425 		    sea->sc_dev.dv_xname, ia->ia_maddr);
    426 #endif
    427 		return;
    428 	}
    429 
    430 	/* Test controller RAM (works the same way on future domain cards?) */
    431 	*((u_char *)sea->maddr + SEA_RAMOFFSET) = 0xa5;
    432 	*((u_char *)sea->maddr + SEA_RAMOFFSET + 1) = 0x5a;
    433 
    434 	if ((*((u_char *)sea->maddr + SEA_RAMOFFSET) != 0xa5) ||
    435 	    (*((u_char *)sea->maddr + SEA_RAMOFFSET + 1) != 0x5a)) {
    436 		printf("%s: board RAM failure\n", sea->sc_dev.dv_xname);
    437 		return;
    438 	}
    439 
    440 	sea_init(sea);
    441 
    442 	/*
    443 	 * Fill in the adapter.
    444 	 */
    445 	sea->sc_adapter.scsipi_cmd = sea_scsi_cmd;
    446 	sea->sc_adapter.scsipi_minphys = minphys;
    447 
    448 	/*
    449 	 * fill in the prototype scsipi_link.
    450 	 */
    451 	sea->sc_link.scsipi_scsi.channel = SCSI_CHANNEL_ONLY_ONE;
    452 	sea->sc_link.adapter_softc = sea;
    453 	sea->sc_link.scsipi_scsi.adapter_target = sea->our_id;
    454 	sea->sc_link.adapter = &sea->sc_adapter;
    455 	sea->sc_link.device = &sea_dev;
    456 	sea->sc_link.openings = 1;
    457 	sea->sc_link.scsipi_scsi.max_target = 7;
    458 	sea->sc_link.scsipi_scsi.max_lun = 7;
    459 	sea->sc_link.type = BUS_SCSI;
    460 
    461 	printf("\n");
    462 
    463 	sea->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
    464 	    IPL_BIO, seaintr, sea);
    465 
    466 	/*
    467 	 * ask the adapter what subunits are present
    468 	 */
    469 	config_found(self, &sea->sc_link, scsiprint);
    470 }
    471 
    472 /*
    473  * Catch an interrupt from the adaptor
    474  */
    475 int
    476 seaintr(arg)
    477 	void *arg;
    478 {
    479 	struct sea_softc *sea = arg;
    480 
    481 #ifdef DEBUG	/* extra overhead, and only needed for intr debugging */
    482 	if ((STATUS & STAT_PARITY) == 0 &&
    483 	    (STATUS & (STAT_SEL | STAT_IO)) != (STAT_SEL | STAT_IO))
    484 		return 0;
    485 #endif
    486 
    487 loop:
    488 	/* dispatch to appropriate routine if found and done=0 */
    489 	/* should check to see that this card really caused the interrupt */
    490 
    491 	if (STATUS & STAT_PARITY) {
    492 		/* Parity error interrupt */
    493 		printf("%s: parity error\n", sea->sc_dev.dv_xname);
    494 		return 1;
    495 	}
    496 
    497 	if ((STATUS & (STAT_SEL | STAT_IO)) == (STAT_SEL | STAT_IO)) {
    498 		/* Reselect interrupt */
    499 		sea_reselect(sea);
    500 		if (!main_running)
    501 			sea_main();
    502 		goto loop;
    503 	}
    504 
    505 	return 1;
    506 }
    507 
    508 /*
    509  * Setup data structures, and reset the board and the SCSI bus.
    510  */
    511 void
    512 sea_init(sea)
    513 	struct sea_softc *sea;
    514 {
    515 	int i;
    516 
    517 	/* Reset the scsi bus (I don't know if this is needed */
    518 	CONTROL = BASE_CMD | CMD_DRVR_ENABLE | CMD_RST;
    519 	delay(25);	/* hold reset for at least 25 microseconds */
    520 	CONTROL = BASE_CMD;
    521 	delay(10); 	/* wait a Bus Clear Delay (800 ns + bus free delay (800 ns) */
    522 
    523 	/* Set our id (don't know anything about this) */
    524 	switch (sea->type) {
    525 	case SEAGATE:
    526 		sea->our_id = 7;
    527 		break;
    528 	case FDOMAIN:
    529 	case FDOMAIN840:
    530 		sea->our_id = 6;
    531 		break;
    532 	}
    533 	sea->our_id_mask = 1 << sea->our_id;
    534 
    535 	/* init fields used by our routines */
    536 	sea->nexus = 0;
    537 	TAILQ_INIT(&sea->ready_list);
    538 	TAILQ_INIT(&sea->nexus_list);
    539 	TAILQ_INIT(&sea->free_list);
    540 	for (i = 0; i < 8; i++)
    541 		sea->busy[i] = 0x00;
    542 
    543 	/* link up the free list of scbs */
    544 	sea->numscbs = SCB_TABLE_SIZE;
    545 	for (i = 0; i < SCB_TABLE_SIZE; i++) {
    546 		TAILQ_INSERT_TAIL(&sea->free_list, &sea->scb[i], chain);
    547 	}
    548 }
    549 
    550 /*
    551  * start a scsi operation given the command and the data address. Also needs
    552  * the unit, target and lu.
    553  */
    554 int
    555 sea_scsi_cmd(xs)
    556 	struct scsipi_xfer *xs;
    557 {
    558 	struct scsipi_link *sc_link = xs->sc_link;
    559 	struct sea_softc *sea = sc_link->adapter_softc;
    560 	struct sea_scb *scb;
    561 	int flags;
    562 	int s;
    563 
    564 	SC_DEBUG(sc_link, SDEV_DB2, ("sea_scsi_cmd\n"));
    565 
    566 	flags = xs->flags;
    567 	if ((flags & (ITSDONE|INUSE)) != INUSE) {
    568 		printf("%s: done or not in use?\n", sea->sc_dev.dv_xname);
    569 		xs->flags &= ~ITSDONE;
    570 		xs->flags |= INUSE;
    571 	}
    572 	if ((scb = sea_get_scb(sea, flags)) == NULL) {
    573 		xs->error = XS_DRIVER_STUFFUP;
    574 		return TRY_AGAIN_LATER;
    575 	}
    576 	scb->flags = SCB_ACTIVE;
    577 	scb->xs = xs;
    578 
    579 	if (flags & SCSI_RESET) {
    580 		/*
    581 		 * Try to send a reset command to the card.
    582 		 * XXX Not implemented.
    583 		 */
    584 		printf("%s: resetting\n", sea->sc_dev.dv_xname);
    585 		xs->error = XS_DRIVER_STUFFUP;
    586 		return COMPLETE;
    587 	}
    588 
    589 	/*
    590 	 * Put all the arguments for the xfer in the scb
    591 	 */
    592 	scb->datalen = xs->datalen;
    593 	scb->data = xs->data;
    594 
    595 #ifdef SEA_DEBUGQUEUE
    596 	sea_queue_length(sea);
    597 #endif
    598 
    599 	s = splbio();
    600 
    601 	sea_send_scb(sea, scb);
    602 
    603 	/*
    604 	 * Usually return SUCCESSFULLY QUEUED
    605 	 */
    606 	if ((flags & SCSI_POLL) == 0) {
    607 		timeout(sea_timeout, scb, (xs->timeout * hz) / 1000);
    608 		splx(s);
    609 		return SUCCESSFULLY_QUEUED;
    610 	}
    611 
    612 	splx(s);
    613 
    614 	/*
    615 	 * If we can't use interrupts, poll on completion
    616 	 */
    617 	if (sea_poll(sea, xs, xs->timeout)) {
    618 		sea_timeout(scb);
    619 		if (sea_poll(sea, xs, 2000))
    620 			sea_timeout(scb);
    621 	}
    622 	return COMPLETE;
    623 }
    624 
    625 /*
    626  * Get a free scb. If there are none, see if we can allocate a new one.  If so,
    627  * put it in the hash table too; otherwise return an error or sleep.
    628  */
    629 struct sea_scb *
    630 sea_get_scb(sea, flags)
    631 	struct sea_softc *sea;
    632 	int flags;
    633 {
    634 	int s;
    635 	struct sea_scb *scb;
    636 
    637 	s = splbio();
    638 
    639 	/*
    640 	 * If we can and have to, sleep waiting for one to come free
    641 	 * but only if we can't allocate a new one.
    642 	 */
    643 	for (;;) {
    644 		scb = sea->free_list.tqh_first;
    645 		if (scb) {
    646 			TAILQ_REMOVE(&sea->free_list, scb, chain);
    647 			break;
    648 		}
    649 		if (sea->numscbs < SEA_SCB_MAX) {
    650 			scb = (struct sea_scb *) malloc(sizeof(struct sea_scb),
    651 			    M_TEMP, M_NOWAIT);
    652 			if (scb) {
    653 				bzero(scb, sizeof(struct sea_scb));
    654 				sea->numscbs++;
    655 			} else
    656 				printf("%s: can't malloc scb\n",
    657 				    sea->sc_dev.dv_xname);
    658 			break;
    659 		}
    660 		if ((flags & SCSI_NOSLEEP) != 0)
    661 			break;
    662 		tsleep(&sea->free_list, PRIBIO, "seascb", 0);
    663 	}
    664 
    665 	splx(s);
    666 	return scb;
    667 }
    668 
    669 /*
    670  * Try to send this command to the board. Because this board does not use any
    671  * mailboxes, this routine simply adds the command to the queue held by the
    672  * sea_softc structure.
    673  * A check is done to see if the command contains a REQUEST_SENSE command, and
    674  * if so the command is put first in the queue, otherwise the command is added
    675  * to the end of the queue. ?? Not correct ??
    676  */
    677 void
    678 sea_send_scb(sea, scb)
    679 	struct sea_softc *sea;
    680 	struct sea_scb *scb;
    681 {
    682 
    683 	TAILQ_INSERT_TAIL(&sea->ready_list, scb, chain);
    684 	/* Try to do some work on the card. */
    685 	if (!main_running)
    686 		sea_main();
    687 }
    688 
    689 /*
    690  * Coroutine that runs as long as more work can be done on the seagate host
    691  * adapter in a system.  Both sea_scsi_cmd and sea_intr will try to start it in
    692  * case it is not running.
    693  */
    694 void
    695 sea_main()
    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 = sea_cd.cd_devs[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->sc_link->scsipi_scsi.target] &
    724 				    (1 << scb->xs->sc_link->scsipi_scsi.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 void
    792 sea_free_scb(sea, scb, flags)
    793 	struct sea_softc *sea;
    794 	struct sea_scb *scb;
    795 	int flags;
    796 {
    797 	int s;
    798 
    799 	s = splbio();
    800 
    801 	scb->flags = SCB_FREE;
    802 	TAILQ_INSERT_HEAD(&sea->free_list, scb, chain);
    803 
    804 	/*
    805 	 * If there were none, wake anybody waiting for one to come free,
    806 	 * starting with queued entries.
    807 	 */
    808 	if (!scb->chain.tqe_next)
    809 		wakeup((caddr_t)&sea->free_list);
    810 
    811 	splx(s);
    812 }
    813 
    814 void
    815 sea_timeout(arg)
    816 	void *arg;
    817 {
    818 	struct sea_scb *scb = arg;
    819 	struct scsipi_xfer *xs = scb->xs;
    820 	struct scsipi_link *sc_link = xs->sc_link;
    821 	struct sea_softc *sea = sc_link->adapter_softc;
    822 	int s;
    823 
    824 	scsi_print_addr(sc_link);
    825 	printf("timed out");
    826 
    827 	s = splbio();
    828 
    829 	/*
    830 	 * If it has been through before, then
    831 	 * a previous abort has failed, don't
    832 	 * try abort again
    833 	 */
    834 	if (scb->flags & SCB_ABORTED) {
    835 		/* abort timed out */
    836 		printf(" AGAIN\n");
    837 	 	scb->xs->retries = 0;
    838 		scb->flags |= SCB_ABORTED;
    839 		sea_done(sea, scb);
    840 	} else {
    841 		/* abort the operation that has timed out */
    842 		printf("\n");
    843 		scb->flags |= SCB_ABORTED;
    844 		sea_abort(sea, scb);
    845 		/* 2 secs for the abort */
    846 		if ((xs->flags & SCSI_POLL) == 0)
    847 			timeout(sea_timeout, scb, 2 * hz);
    848 	}
    849 
    850 	splx(s);
    851 }
    852 
    853 void
    854 sea_reselect(sea)
    855 	struct sea_softc *sea;
    856 {
    857 	u_char target_mask;
    858 	int i;
    859 	u_char lun, phase;
    860 	u_char msg[3];
    861 	int len;
    862 	u_char *data;
    863 	struct sea_scb *scb;
    864 	int abort = 0;
    865 
    866 	if (!((target_mask = STATUS) & STAT_SEL)) {
    867 		printf("%s: wrong state 0x%x\n", sea->sc_dev.dv_xname,
    868 		    target_mask);
    869 		return;
    870 	}
    871 
    872 	/* wait for a device to win the reselection phase */
    873 	/* signals this by asserting the I/O signal */
    874 	for (i = 10; i && (STATUS & (STAT_SEL | STAT_IO | STAT_BSY)) !=
    875 	    (STAT_SEL | STAT_IO | 0); i--);
    876 	/* !! Check for timeout here */
    877 	/* the data bus contains original initiator id ORed with target id */
    878 	target_mask = DATA;
    879 	/* see that we really are the initiator */
    880 	if (!(target_mask & sea->our_id_mask)) {
    881 		printf("%s: polled reselection was not for me: 0x%x\n",
    882 		    sea->sc_dev.dv_xname, target_mask);
    883 		return;
    884 	}
    885 	/* find target who won */
    886 	target_mask &= ~sea->our_id_mask;
    887 	/* host responds by asserting the BSY signal */
    888 	CONTROL = BASE_CMD | CMD_DRVR_ENABLE | CMD_BSY;
    889 	/* target should respond by deasserting the SEL signal */
    890 	for (i = 50000; i && (STATUS & STAT_SEL); i++);
    891 	/* remove the busy status */
    892 	CONTROL = BASE_CMD | CMD_DRVR_ENABLE;
    893 	/* we are connected. Now we wait for the MSGIN condition */
    894 	for (i = 50000; i && !(STATUS & STAT_REQ); i--);
    895 	/* !! Add timeout check here */
    896 	/* hope we get an IDENTIFY message */
    897 	len = 3;
    898 	data = msg;
    899 	phase = PH_MSGIN;
    900 	sea_transfer_pio(sea, &phase, &len, &data);
    901 
    902 	if (MSG_ISIDENTIFY(msg[0])) {
    903 		printf("%s: expecting IDENTIFY message, got 0x%x\n",
    904 		    sea->sc_dev.dv_xname, msg[0]);
    905 		abort = 1;
    906 		scb = NULL;
    907 	} else {
    908 		lun = msg[0] & 0x07;
    909 
    910 		/*
    911 		 * Find the command corresponding to the I_T_L or I_T_L_Q nexus
    912 		 * we just reestablished, and remove it from the disconnected
    913 		 * queue.
    914 		 */
    915 		for (scb = sea->nexus_list.tqh_first; scb;
    916 		    scb = scb->chain.tqe_next)
    917 			if (target_mask == (1 << scb->xs->sc_link->scsipi_scsi.target) &&
    918 			    lun == scb->xs->sc_link->scsipi_scsi.lun) {
    919 				TAILQ_REMOVE(&sea->nexus_list, scb,
    920 				    chain);
    921 				break;
    922 			}
    923 		if (!scb) {
    924 			printf("%s: target %02x lun %d not disconnected\n",
    925 			    sea->sc_dev.dv_xname, target_mask, lun);
    926 			/*
    927 			 * Since we have an established nexus that we can't do
    928 			 * anything with, we must abort it.
    929 			 */
    930 			abort = 1;
    931 		}
    932 	}
    933 
    934 	if (abort) {
    935 		msg[0] = MSG_ABORT;
    936 		len = 1;
    937 		data = msg;
    938 		phase = PH_MSGOUT;
    939 		CONTROL = BASE_CMD | CMD_ATTN;
    940 		sea_transfer_pio(sea, &phase, &len, &data);
    941 	} else
    942 		sea->nexus = scb;
    943 
    944 	return;
    945 }
    946 
    947 /*
    948  * Transfer data in given phase using polled I/O.
    949  */
    950 int
    951 sea_transfer_pio(sea, phase, count, data)
    952 	struct sea_softc *sea;
    953 	u_char *phase;
    954 	int *count;
    955 	u_char **data;
    956 {
    957 	register u_char p = *phase, tmp;
    958 	register int c = *count;
    959 	register u_char *d = *data;
    960 	int timeout;
    961 
    962 	do {
    963 		/*
    964 		 * Wait for assertion of REQ, after which the phase bits will
    965 		 * be valid.
    966 		 */
    967 		for (timeout = 0; timeout < 50000; timeout++)
    968 			if ((tmp = STATUS) & STAT_REQ)
    969 				break;
    970 		if (!(tmp & STAT_REQ)) {
    971 			printf("%s: timeout waiting for STAT_REQ\n",
    972 			    sea->sc_dev.dv_xname);
    973 			break;
    974 		}
    975 
    976 		/*
    977 		 * Check for phase mismatch.  Reached if the target decides
    978 		 * that it has finished the transfer.
    979 		 */
    980 		if (sea->type == FDOMAIN840)
    981 			tmp = ((tmp & 0x08) >> 2) |
    982 			      ((tmp & 0x02) << 2) |
    983 			       (tmp & 0xf5);
    984 		if ((tmp & PH_MASK) != p)
    985 			break;
    986 
    987 		/* Do actual transfer from SCSI bus to/from memory. */
    988 		if (!(p & STAT_IO))
    989 			DATA = *d;
    990 		else
    991 			*d = DATA;
    992 		++d;
    993 
    994 		/*
    995 		 * The SCSI standard suggests that in MSGOUT phase, the
    996 		 * initiator should drop ATN on the last byte of the message
    997 		 * phase after REQ has been asserted for the handshake but
    998 		 * before the initiator raises ACK.
    999 		 * Don't know how to accomplish this on the ST01/02.
   1000 		 */
   1001 
   1002 #if 0
   1003 		/*
   1004 		 * XXX
   1005 		 * The st01 code doesn't wait for STAT_REQ to be deasserted.
   1006 		 * Is this ok?
   1007 		 */
   1008 		for (timeout = 0; timeout < 200000L; timeout++)
   1009 			if (!(STATUS & STAT_REQ))
   1010 				break;
   1011 		if (STATUS & STAT_REQ)
   1012 			printf("%s: timeout on wait for !STAT_REQ",
   1013 			    sea->sc_dev.dv_xname);
   1014 #endif
   1015 	} while (--c);
   1016 
   1017 	*count = c;
   1018 	*data = d;
   1019 	tmp = STATUS;
   1020 	if (tmp & STAT_REQ)
   1021 		*phase = tmp & PH_MASK;
   1022 	else
   1023 		*phase = PH_INVALID;
   1024 
   1025 	if (c && (*phase != p))
   1026 		return -1;
   1027 	return 0;
   1028 }
   1029 
   1030 /*
   1031  * Establish I_T_L or I_T_L_Q nexus for new or existing command including
   1032  * ARBITRATION, SELECTION, and initial message out for IDENTIFY and queue
   1033  * messages.  Return -1 if selection could not execute for some reason, 0 if
   1034  * selection succeded or failed because the target did not respond.
   1035  */
   1036 int
   1037 sea_select(sea, scb)
   1038 	struct sea_softc *sea;
   1039 	struct sea_scb *scb;
   1040 {
   1041 	u_char msg[3], phase;
   1042 	u_char *data;
   1043 	int len;
   1044 	int timeout;
   1045 
   1046 	CONTROL = BASE_CMD;
   1047 	DATA = sea->our_id_mask;
   1048 	CONTROL = (BASE_CMD & ~CMD_INTR) | CMD_START_ARB;
   1049 
   1050 	/* wait for arbitration to complete */
   1051 	for (timeout = 0; timeout < 3000000L; timeout++)
   1052 		if (STATUS & STAT_ARB_CMPL)
   1053 			break;
   1054 	if (!(STATUS & STAT_ARB_CMPL)) {
   1055 		if (STATUS & STAT_SEL) {
   1056 			printf("%s: arbitration lost\n", sea->sc_dev.dv_xname);
   1057 			scb->flags |= SCB_ERROR;
   1058 		} else {
   1059 			printf("%s: arbitration timeout\n",
   1060 			    sea->sc_dev.dv_xname);
   1061 			scb->flags |= SCB_TIMEOUT;
   1062 		}
   1063 		CONTROL = BASE_CMD;
   1064 		return -1;
   1065 	}
   1066 
   1067 	delay(2);
   1068 	DATA = (u_char)((1 << scb->xs->sc_link->scsipi_scsi.target) |
   1069 		sea->our_id_mask);
   1070 	CONTROL =
   1071 #ifdef SEA_NOMSGS
   1072 	    (BASE_CMD & ~CMD_INTR) | CMD_DRVR_ENABLE | CMD_SEL;
   1073 #else
   1074 	    (BASE_CMD & ~CMD_INTR) | CMD_DRVR_ENABLE | CMD_SEL | CMD_ATTN;
   1075 #endif
   1076 	delay(1);
   1077 
   1078 	/* wait for a bsy from target */
   1079 	for (timeout = 0; timeout < 2000000L; timeout++)
   1080 		if (STATUS & STAT_BSY)
   1081 			break;
   1082 	if (!(STATUS & STAT_BSY)) {
   1083 		/* should return some error to the higher level driver */
   1084 		CONTROL = BASE_CMD;
   1085 		scb->flags |= SCB_TIMEOUT;
   1086 		return 0;
   1087 	}
   1088 
   1089 	/* Try to make the target to take a message from us */
   1090 #ifdef SEA_NOMSGS
   1091 	CONTROL = (BASE_CMD & ~CMD_INTR) | CMD_DRVR_ENABLE;
   1092 #else
   1093 	CONTROL = (BASE_CMD & ~CMD_INTR) | CMD_DRVR_ENABLE | CMD_ATTN;
   1094 #endif
   1095 	delay(1);
   1096 
   1097 	/* should start a msg_out phase */
   1098 	for (timeout = 0; timeout < 2000000L; timeout++)
   1099 		if (STATUS & STAT_REQ)
   1100 			break;
   1101 	/* Remove ATN. */
   1102 	CONTROL = BASE_CMD | CMD_DRVR_ENABLE;
   1103 	if (!(STATUS & STAT_REQ)) {
   1104 		/*
   1105 		 * This should not be taken as an error, but more like an
   1106 		 * unsupported feature!  Should set a flag indicating that the
   1107 		 * target don't support messages, and continue without failure.
   1108 		 * (THIS IS NOT AN ERROR!)
   1109 		 */
   1110 	} else {
   1111 		msg[0] = MSG_IDENTIFY(scb->xs->sc_link->scsipi_scsi.lun, 1);
   1112 		len = 1;
   1113 		data = msg;
   1114 		phase = PH_MSGOUT;
   1115 		/* Should do test on result of sea_transfer_pio(). */
   1116 		sea_transfer_pio(sea, &phase, &len, &data);
   1117 	}
   1118 	if (!(STATUS & STAT_BSY))
   1119 		printf("%s: after successful arbitrate: no STAT_BSY!\n",
   1120 		    sea->sc_dev.dv_xname);
   1121 
   1122 	sea->nexus = scb;
   1123 	sea->busy[scb->xs->sc_link->scsipi_scsi.target] |=
   1124 						1 << scb->xs->sc_link->scsipi_scsi.lun;
   1125 	/* This assignment should depend on possibility to send a message to target. */
   1126 	CONTROL = BASE_CMD | CMD_DRVR_ENABLE;
   1127 	/* XXX Reset pointer in command? */
   1128 	return 0;
   1129 }
   1130 
   1131 /*
   1132  * Send an abort to the target.  Return 1 success, 0 on failure.
   1133  */
   1134 int
   1135 sea_abort(sea, scb)
   1136 	struct sea_softc *sea;
   1137 	struct sea_scb *scb;
   1138 {
   1139 	struct sea_scb *tmp;
   1140 	u_char msg, phase, *msgptr;
   1141 	int len;
   1142 
   1143 	/*
   1144 	 * If the command hasn't been issued yet, we simply remove it from the
   1145 	 * issue queue
   1146 	 * XXX Could avoid this loop.
   1147 	 */
   1148 	for (tmp = sea->ready_list.tqh_first; tmp; tmp = tmp->chain.tqe_next)
   1149 		if (scb == tmp) {
   1150 			TAILQ_REMOVE(&sea->ready_list, scb, chain);
   1151 			/* XXX Set some type of error result for operation. */
   1152 			return 1;
   1153 		}
   1154 
   1155 	/*
   1156 	 * If any commands are connected, we're going to fail the abort and let
   1157 	 * the high level SCSI driver retry at a later time or issue a reset.
   1158 	 */
   1159 	if (sea->nexus)
   1160 		return 0;
   1161 
   1162 	/*
   1163 	 * If the command is currently disconnected from the bus, and there are
   1164 	 * no connected commands, we reconnect the I_T_L or I_T_L_Q nexus
   1165 	 * associated with it, go into message out, and send an abort message.
   1166 	 */
   1167 	for (tmp = sea->nexus_list.tqh_first; tmp;
   1168 	    tmp = tmp->chain.tqe_next)
   1169 		if (scb == tmp) {
   1170 			if (sea_select(sea, scb))
   1171 				return 0;
   1172 
   1173 			msg = MSG_ABORT;
   1174 			msgptr = &msg;
   1175 			len = 1;
   1176 			phase = PH_MSGOUT;
   1177 			CONTROL = BASE_CMD | CMD_ATTN;
   1178 			sea_transfer_pio(sea, &phase, &len, &msgptr);
   1179 
   1180 			for (tmp = sea->nexus_list.tqh_first; tmp;
   1181 			    tmp = tmp->chain.tqe_next)
   1182 				if (scb == tmp) {
   1183 					TAILQ_REMOVE(&sea->nexus_list,
   1184 					    scb, chain);
   1185 					/* XXX Set some type of error result
   1186 					   for the operation. */
   1187 					return 1;
   1188 				}
   1189 		}
   1190 
   1191 	/* Command not found in any queue; race condition? */
   1192 	return 1;
   1193 }
   1194 
   1195 void
   1196 sea_done(sea, scb)
   1197 	struct sea_softc *sea;
   1198 	struct sea_scb *scb;
   1199 {
   1200 	struct scsipi_xfer *xs = scb->xs;
   1201 
   1202 	untimeout(sea_timeout, scb);
   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 	xs->flags |= ITSDONE;
   1216 	sea_free_scb(sea, scb, xs->flags);
   1217 	scsipi_done(xs);
   1218 }
   1219 
   1220 /*
   1221  * Wait for completion of command in polled mode.
   1222  */
   1223 int
   1224 sea_poll(sea, xs, count)
   1225 	struct sea_softc *sea;
   1226 	struct scsipi_xfer *xs;
   1227 	int count;
   1228 {
   1229 	int s;
   1230 
   1231 	while (count) {
   1232 		/* try to do something */
   1233 		s = splbio();
   1234 		if (!main_running)
   1235 			sea_main();
   1236 		splx(s);
   1237 		if (xs->flags & ITSDONE)
   1238 			return 0;
   1239 		delay(1000);
   1240 		count--;
   1241 	}
   1242 	return 1;
   1243 }
   1244 
   1245 /*
   1246  * Do the transfer.  We know we are connected.  Update the flags, and call
   1247  * sea_done() when task accomplished.  Dialog controlled by the target.
   1248  */
   1249 void
   1250 sea_information_transfer(sea)
   1251 	struct sea_softc *sea;
   1252 {
   1253 	int timeout;
   1254 	u_char msgout = MSG_NOOP;
   1255 	int len;
   1256 	int s;
   1257 	u_char *data;
   1258 	u_char phase, tmp, old_phase = PH_INVALID;
   1259 	struct sea_scb *scb = sea->nexus;
   1260 	int loop;
   1261 
   1262 	for (timeout = 0; timeout < 10000000L; timeout++) {
   1263 		tmp = STATUS;
   1264 		if (tmp & STAT_PARITY)
   1265 			printf("%s: parity error detected\n",
   1266 			    sea->sc_dev.dv_xname);
   1267 		if (!(tmp & STAT_BSY)) {
   1268 			for (loop = 0; loop < 20; loop++)
   1269 				if ((tmp = STATUS) & STAT_BSY)
   1270 					break;
   1271 			if (!(tmp & STAT_BSY)) {
   1272 				printf("%s: !STAT_BSY unit in data transfer!\n",
   1273 				    sea->sc_dev.dv_xname);
   1274 				s = splbio();
   1275 				sea->nexus = NULL;
   1276 				scb->flags = SCB_ERROR;
   1277 				splx(s);
   1278 				sea_done(sea, scb);
   1279 				return;
   1280 			}
   1281 		}
   1282 
   1283 		/* we only have a valid SCSI phase when REQ is asserted */
   1284 		if (!(tmp & STAT_REQ))
   1285 			continue;
   1286 
   1287 		if (sea->type == FDOMAIN840)
   1288 			tmp = ((tmp & 0x08) >> 2) |
   1289 			      ((tmp & 0x02) << 2) |
   1290 			       (tmp & 0xf5);
   1291 		phase = tmp & PH_MASK;
   1292 		if (phase != old_phase)
   1293 			old_phase = phase;
   1294 
   1295 		switch (phase) {
   1296 		case PH_DATAOUT:
   1297 #ifdef SEA_NODATAOUT
   1298 			printf("%s: SEA_NODATAOUT set, attempted DATAOUT aborted\n",
   1299 			    sea->sc_dev.dv_xname);
   1300 			msgout = MSG_ABORT;
   1301 			CONTROL = BASE_CMD | CMD_ATTN;
   1302 			break;
   1303 #endif
   1304 		case PH_DATAIN:
   1305 			if (!scb->data)
   1306 				printf("no data address!\n");
   1307 #ifdef SEA_BLINDTRANSFER
   1308 			if (scb->datalen && !(scb->datalen % BLOCK_SIZE)) {
   1309 				while (scb->datalen) {
   1310 					for (loop = 0; loop < 50000; loop++)
   1311 						if ((tmp = STATUS) & STAT_REQ)
   1312 							break;
   1313 					if (!(tmp & STAT_REQ)) {
   1314 						printf("%s: timeout waiting for STAT_REQ\n",
   1315 						    sea->sc_dev.dv_xname);
   1316 						/* XXX Do something? */
   1317 					}
   1318 					if (sea->type == FDOMAIN840)
   1319 						tmp = ((tmp & 0x08) >> 2) |
   1320 						      ((tmp & 0x02) << 2) |
   1321 						       (tmp & 0xf5);
   1322 					if ((tmp & PH_MASK) != phase)
   1323 						break;
   1324 					if (!(phase & STAT_IO)) {
   1325 #ifdef SEA_ASSEMBLER
   1326 						asm("shr $2, %%ecx\n\t\
   1327 						    cld\n\t\
   1328 						    rep\n\t\
   1329 						    movsl" :
   1330 						    "=S" (scb->data) :
   1331 						    "0" (scb->data),
   1332 						    "D" (sea->maddr_dr),
   1333 						    "c" (BLOCK_SIZE) :
   1334 						    "%ecx", "%edi");
   1335 #else
   1336 						for (count = 0;
   1337 						    count < BLOCK_SIZE;
   1338 						    count++)
   1339 							DATA = *(scb->data++);
   1340 #endif
   1341 					} else {
   1342 #ifdef SEA_ASSEMBLER
   1343 						asm("shr $2, %%ecx\n\t\
   1344 						    cld\n\t\
   1345 						    rep\n\t\
   1346 						    movsl" :
   1347 						    "=D" (scb->data) :
   1348 						    "S" (sea->maddr_dr),
   1349 						    "0" (scb->data),
   1350 						    "c" (BLOCK_SIZE) :
   1351 						    "%ecx", "%esi");
   1352 #else
   1353 					        for (count = 0;
   1354 						    count < BLOCK_SIZE;
   1355 						    count++)
   1356 							*(scb->data++) = DATA;
   1357 #endif
   1358 					}
   1359 					scb->datalen -= BLOCK_SIZE;
   1360 				}
   1361 			}
   1362 #endif
   1363 			if (scb->datalen)
   1364 				sea_transfer_pio(sea, &phase, &scb->datalen,
   1365 				    &scb->data);
   1366 			break;
   1367 		case PH_MSGIN:
   1368 			/* Multibyte messages should not be present here. */
   1369 			len = 1;
   1370 			data = &tmp;
   1371 			sea_transfer_pio(sea, &phase, &len, &data);
   1372 			/* scb->MessageIn = tmp; */
   1373 
   1374 			switch (tmp) {
   1375 			case MSG_ABORT:
   1376 				scb->flags = SCB_ABORTED;
   1377 				printf("sea: command aborted by target\n");
   1378 				CONTROL = BASE_CMD;
   1379 				sea_done(sea, scb);
   1380 				return;
   1381 			case MSG_CMDCOMPLETE:
   1382 				s = splbio();
   1383 				sea->nexus = NULL;
   1384 				splx(s);
   1385 				sea->busy[scb->xs->sc_link->scsipi_scsi.target] &=
   1386 				    ~(1 << scb->xs->sc_link->scsipi_scsi.lun);
   1387 				CONTROL = BASE_CMD;
   1388 				sea_done(sea, scb);
   1389 				return;
   1390 			case MSG_MESSAGE_REJECT:
   1391 				printf("%s: message_reject recieved\n",
   1392 				    sea->sc_dev.dv_xname);
   1393 				break;
   1394 			case MSG_DISCONNECT:
   1395 				s = splbio();
   1396 				TAILQ_INSERT_TAIL(&sea->nexus_list,
   1397 				    scb, chain);
   1398 				sea->nexus = NULL;
   1399 				CONTROL = BASE_CMD;
   1400 				splx(s);
   1401 				return;
   1402 			case MSG_SAVEDATAPOINTER:
   1403 			case MSG_RESTOREPOINTERS:
   1404 				/* save/restore of pointers are ignored */
   1405 				break;
   1406 			default:
   1407 				/*
   1408 				 * This should be handled in the pio data
   1409 				 * transfer phase, as the ATN should be raised
   1410 				 * before ACK goes false when rejecting a
   1411 				 * message.
   1412 				 */
   1413 				printf("%s: unknown message in: %x\n",
   1414 				    sea->sc_dev.dv_xname, tmp);
   1415 				break;
   1416 			} /* switch (tmp) */
   1417 			break;
   1418 		case PH_MSGOUT:
   1419 			len = 1;
   1420 			data = &msgout;
   1421 			/* sea->last_message = msgout; */
   1422 			sea_transfer_pio(sea, &phase, &len, &data);
   1423 			if (msgout == MSG_ABORT) {
   1424 				printf("%s: sent message abort to target\n",
   1425 				    sea->sc_dev.dv_xname);
   1426 				s = splbio();
   1427 				sea->busy[scb->xs->sc_link->scsipi_scsi.target] &=
   1428 				    ~(1 << scb->xs->sc_link->scsipi_scsi.lun);
   1429 				sea->nexus = NULL;
   1430 				scb->flags = SCB_ABORTED;
   1431 				splx(s);
   1432 				/* enable interrupt from scsi */
   1433 				sea_done(sea, scb);
   1434 				return;
   1435 			}
   1436 			msgout = MSG_NOOP;
   1437 			break;
   1438 		case PH_CMD:
   1439 			len = scb->xs->cmdlen;
   1440 			data = (char *) scb->xs->cmd;
   1441 			sea_transfer_pio(sea, &phase, &len, &data);
   1442 			break;
   1443 		case PH_STAT:
   1444 			len = 1;
   1445 			data = &tmp;
   1446 			sea_transfer_pio(sea, &phase, &len, &data);
   1447 			scb->xs->status = tmp;
   1448 			break;
   1449 		default:
   1450 			printf("sea: unknown phase\n");
   1451 		} /* switch (phase) */
   1452 	} /* for (...) */
   1453 
   1454 	/* If we get here we have got a timeout! */
   1455 	printf("%s: timeout in data transfer\n", sea->sc_dev.dv_xname);
   1456 	scb->flags = SCB_TIMEOUT;
   1457 	/* XXX Should I clear scsi-bus state? */
   1458 	sea_done(sea, scb);
   1459 }
   1460