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