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