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