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