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