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