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