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