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