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