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