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