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