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