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