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