Home | History | Annotate | Line # | Download | only in ata
atavar.h revision 1.37
      1  1.37  thorpej /*	$NetBSD: atavar.h,v 1.37 2003/12/30 16:28:37 thorpej Exp $	*/
      2   1.2   bouyer 
      3   1.2   bouyer /*
      4  1.23   bouyer  * Copyright (c) 1998, 2001 Manuel Bouyer.
      5   1.2   bouyer  *
      6   1.2   bouyer  * Redistribution and use in source and binary forms, with or without
      7   1.2   bouyer  * modification, are permitted provided that the following conditions
      8   1.2   bouyer  * are met:
      9   1.2   bouyer  * 1. Redistributions of source code must retain the above copyright
     10   1.2   bouyer  *    notice, this list of conditions and the following disclaimer.
     11   1.2   bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     12   1.2   bouyer  *    notice, this list of conditions and the following disclaimer in the
     13   1.2   bouyer  *    documentation and/or other materials provided with the distribution.
     14   1.2   bouyer  * 3. All advertising materials mentioning features or use of this software
     15   1.2   bouyer  *    must display the following acknowledgement:
     16  1.25   bouyer  *	This product includes software developed by Manuel Bouyer.
     17  1.28   bouyer  * 4. The name of the author may not be used to endorse or promote products
     18  1.28   bouyer  *    derived from this software without specific prior written permission.
     19   1.2   bouyer  *
     20  1.18   bouyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  1.18   bouyer  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  1.18   bouyer  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  1.18   bouyer  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  1.18   bouyer  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  1.18   bouyer  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  1.18   bouyer  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  1.18   bouyer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  1.18   bouyer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  1.18   bouyer  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30   1.2   bouyer  */
     31   1.2   bouyer 
     32  1.31  thorpej #ifndef _DEV_ATA_ATAVAR_H_
     33  1.31  thorpej #define	_DEV_ATA_ATAVAR_H_
     34  1.31  thorpej 
     35  1.37  thorpej #include <sys/lock.h>
     36  1.37  thorpej #include <sys/queue.h>
     37  1.37  thorpej 
     38  1.37  thorpej /* ATA bus instance state information. */
     39  1.37  thorpej struct atabus_softc {
     40  1.37  thorpej 	struct device sc_dev;
     41  1.37  thorpej 	struct channel_softc *sc_chan;	/* XXXwdc */
     42  1.37  thorpej };
     43  1.37  thorpej 
     44  1.37  thorpej /*
     45  1.37  thorpej  * A queue of atabus instances, used to ensure the same bus probe order
     46  1.37  thorpej  * for a given hardware configuration at each boot.
     47  1.37  thorpej  */
     48  1.37  thorpej struct atabus_initq {
     49  1.37  thorpej 	TAILQ_ENTRY(atabus_initq) atabus_initq;
     50  1.37  thorpej 	struct atabus_softc *atabus_sc;
     51  1.37  thorpej };
     52  1.37  thorpej 
     53  1.37  thorpej #ifdef _KERNEL
     54  1.37  thorpej TAILQ_HEAD(atabus_initq_head, atabus_initq);
     55  1.37  thorpej extern struct atabus_initq_head atabus_initq_head;
     56  1.37  thorpej extern struct simplelock atabus_interlock;
     57  1.37  thorpej #endif /* _KERNEL */
     58  1.37  thorpej 
     59  1.31  thorpej /* High-level functions and structures used by both ATA and ATAPI devices */
     60  1.33  thorpej struct ataparams;
     61  1.33  thorpej 
     62   1.2   bouyer /* Datas common to drives and controller drivers */
     63   1.2   bouyer struct ata_drive_datas {
     64  1.31  thorpej 	u_int8_t drive;		/* drive number */
     65  1.31  thorpej 	int8_t ata_vers;	/* ATA version supported */
     66  1.31  thorpej 	u_int16_t drive_flags;	/* bitmask for drives present/absent and cap */
     67  1.31  thorpej 
     68  1.31  thorpej #define	DRIVE_ATA	0x0001
     69  1.31  thorpej #define	DRIVE_ATAPI	0x0002
     70  1.31  thorpej #define	DRIVE_OLD	0x0004
     71  1.31  thorpej #define	DRIVE		(DRIVE_ATA|DRIVE_ATAPI|DRIVE_OLD)
     72  1.31  thorpej #define	DRIVE_CAP32	0x0008
     73  1.31  thorpej #define	DRIVE_DMA	0x0010
     74  1.31  thorpej #define	DRIVE_UDMA	0x0020
     75  1.31  thorpej #define	DRIVE_MODE	0x0040	/* the drive reported its mode */
     76  1.31  thorpej #define	DRIVE_RESET	0x0080	/* reset the drive state at next xfer */
     77  1.31  thorpej #define	DRIVE_DMAERR	0x0100	/* Udma transfer had crc error, don't try DMA */
     78  1.31  thorpej #define	DRIVE_ATAPIST	0x0100	/* device is an ATAPI tape drive */
     79  1.31  thorpej 
     80  1.31  thorpej 	/*
     81  1.31  thorpej 	 * Current setting of drive's PIO, DMA and UDMA modes.
     82  1.31  thorpej 	 * Is initialised by the disks drivers at attach time, and may be
     83  1.31  thorpej 	 * changed later by the controller's code if needed
     84  1.31  thorpej 	 */
     85  1.31  thorpej 	u_int8_t PIO_mode;	/* Current setting of drive's PIO mode */
     86  1.31  thorpej 	u_int8_t DMA_mode;	/* Current setting of drive's DMA mode */
     87  1.31  thorpej 	u_int8_t UDMA_mode;	/* Current setting of drive's UDMA mode */
     88  1.31  thorpej 
     89  1.31  thorpej 	/* Supported modes for this drive */
     90  1.31  thorpej 	u_int8_t PIO_cap;	/* supported drive's PIO mode */
     91  1.31  thorpej 	u_int8_t DMA_cap;	/* supported drive's DMA mode */
     92  1.31  thorpej 	u_int8_t UDMA_cap;	/* supported drive's UDMA mode */
     93  1.31  thorpej 
     94  1.31  thorpej 	/*
     95  1.31  thorpej 	 * Drive state.
     96  1.31  thorpej 	 * This is reset to 0 after a channel reset.
     97  1.31  thorpej 	 */
     98  1.31  thorpej 	u_int8_t state;
     99  1.31  thorpej 
    100  1.16   bouyer #define RESET          0
    101  1.29   bouyer #define READY          1
    102   1.2   bouyer 
    103  1.31  thorpej 	/* numbers of xfers and DMA errs. Used by ata_dmaerr() */
    104  1.31  thorpej 	u_int8_t n_dmaerrs;
    105  1.31  thorpej 	u_int32_t n_xfers;
    106  1.31  thorpej 
    107  1.31  thorpej 	/* Downgrade after NERRS_MAX errors in at most NXFER xfers */
    108  1.15   bouyer #define NERRS_MAX 4
    109  1.15   bouyer #define NXFER 4000
    110  1.35  thorpej 
    111  1.35  thorpej 	/* Callbacks into the drive's driver. */
    112  1.35  thorpej 	void	(*drv_done)(void *);	/* transfer is done */
    113   1.9   bouyer 
    114  1.31  thorpej 	struct device *drv_softc;	/* ATA drives softc, if any */
    115  1.31  thorpej 	void *chnl_softc;		/* channel softc */
    116   1.2   bouyer };
    117   1.2   bouyer 
    118   1.7   bouyer /* User config flags that force (or disable) the use of a mode */
    119   1.7   bouyer #define ATA_CONFIG_PIO_MODES	0x0007
    120   1.7   bouyer #define ATA_CONFIG_PIO_SET	0x0008
    121   1.7   bouyer #define ATA_CONFIG_PIO_OFF	0
    122   1.7   bouyer #define ATA_CONFIG_DMA_MODES	0x0070
    123   1.7   bouyer #define ATA_CONFIG_DMA_SET	0x0080
    124   1.7   bouyer #define ATA_CONFIG_DMA_DISABLE	0x0070
    125   1.7   bouyer #define ATA_CONFIG_DMA_OFF	4
    126   1.7   bouyer #define ATA_CONFIG_UDMA_MODES	0x0700
    127   1.7   bouyer #define ATA_CONFIG_UDMA_SET	0x0800
    128   1.7   bouyer #define ATA_CONFIG_UDMA_DISABLE	0x0700
    129   1.7   bouyer #define ATA_CONFIG_UDMA_OFF	8
    130  1.32  thorpej 
    131  1.32  thorpej /*
    132  1.32  thorpej  * Parameters/state needed by the controller to perform an ATA bio.
    133  1.32  thorpej  */
    134  1.32  thorpej struct ata_bio {
    135  1.32  thorpej 	volatile u_int16_t flags;/* cmd flags */
    136  1.32  thorpej #define	ATA_NOSLEEP	0x0001	/* Can't sleep */
    137  1.32  thorpej #define	ATA_POLL	0x0002	/* poll for completion */
    138  1.32  thorpej #define	ATA_ITSDONE	0x0004	/* the transfer is as done as it gets */
    139  1.32  thorpej #define	ATA_SINGLE	0x0008	/* transfer must be done in singlesector mode */
    140  1.32  thorpej #define	ATA_LBA		0x0010	/* transfer uses LBA addressing */
    141  1.32  thorpej #define	ATA_READ	0x0020	/* transfer is a read (otherwise a write) */
    142  1.32  thorpej #define	ATA_CORR	0x0040	/* transfer had a corrected error */
    143  1.32  thorpej #define	ATA_LBA48	0x0080	/* transfer uses 48-bit LBA addressing */
    144  1.32  thorpej 	int		multi;	/* # of blocks to transfer in multi-mode */
    145  1.32  thorpej 	struct disklabel *lp;	/* pointer to drive's label info */
    146  1.32  thorpej 	daddr_t		blkno;	/* block addr */
    147  1.32  thorpej 	daddr_t		blkdone;/* number of blks transferred */
    148  1.32  thorpej 	daddr_t		nblks;	/* number of block currently transferring */
    149  1.32  thorpej 	int		nbytes;	/* number of bytes currently transferring */
    150  1.32  thorpej 	long		bcount;	/* total number of bytes */
    151  1.32  thorpej 	char		*databuf;/* data buffer address */
    152  1.32  thorpej 	volatile int	error;
    153  1.32  thorpej #define	NOERROR 	0	/* There was no error (r_error invalid) */
    154  1.32  thorpej #define	ERROR		1	/* check r_error */
    155  1.32  thorpej #define	ERR_DF		2	/* Drive fault */
    156  1.32  thorpej #define	ERR_DMA		3	/* DMA error */
    157  1.32  thorpej #define	TIMEOUT		4	/* device timed out */
    158  1.32  thorpej #define	ERR_NODEV	5	/* device has been gone */
    159  1.32  thorpej 	u_int8_t	r_error;/* copy of error register */
    160  1.32  thorpej 	daddr_t		badsect[127];/* 126 plus trailing -1 marker */
    161  1.32  thorpej };
    162   1.2   bouyer 
    163   1.2   bouyer /*
    164   1.2   bouyer  * ATA/ATAPI commands description
    165   1.2   bouyer  *
    166   1.2   bouyer  * This structure defines the interface between the ATA/ATAPI device driver
    167   1.2   bouyer  * and the controller for short commands. It contains the command's parameter,
    168   1.2   bouyer  * the len of data's to read/write (if any), and a function to call upon
    169   1.2   bouyer  * completion.
    170   1.2   bouyer  * If no sleep is allowed, the driver can poll for command completion.
    171   1.2   bouyer  * Once the command completed, if the error registed is valid, the flag
    172   1.2   bouyer  * AT_ERROR is set and the error register value is copied to r_error .
    173   1.2   bouyer  * A separate interface is needed for read/write or ATAPI packet commands
    174   1.2   bouyer  * (which need multiple interrupts per commands).
    175   1.2   bouyer  */
    176   1.2   bouyer struct wdc_command {
    177  1.31  thorpej 	u_int8_t r_command;	/* Parameters to upload to registers */
    178  1.31  thorpej 	u_int8_t r_head;
    179  1.31  thorpej 	u_int16_t r_cyl;
    180  1.31  thorpej 	u_int8_t r_sector;
    181  1.31  thorpej 	u_int8_t r_count;
    182  1.31  thorpej 	u_int8_t r_precomp;
    183  1.31  thorpej 	u_int8_t r_st_bmask;	/* status register mask to wait for before
    184  1.31  thorpej 				   command */
    185  1.31  thorpej 	u_int8_t r_st_pmask;	/* status register mask to wait for after
    186  1.31  thorpej 				   command */
    187  1.31  thorpej 	u_int8_t r_error;	/* error register after command done */
    188  1.31  thorpej 	volatile u_int16_t flags;
    189  1.31  thorpej 
    190   1.2   bouyer #define AT_READ     0x0001 /* There is data to read */
    191   1.2   bouyer #define AT_WRITE    0x0002 /* There is data to write (excl. with AT_READ) */
    192   1.2   bouyer #define AT_WAIT     0x0008 /* wait in controller code for command completion */
    193   1.2   bouyer #define AT_POLL     0x0010 /* poll for command completion (no interrupts) */
    194   1.2   bouyer #define AT_DONE     0x0020 /* command is done */
    195  1.24   bouyer #define AT_XFDONE   0x0040 /* data xfer is done */
    196  1.24   bouyer #define AT_ERROR    0x0080 /* command is done with error */
    197  1.24   bouyer #define AT_TIMEOU   0x0100 /* command timed out */
    198  1.24   bouyer #define AT_DF       0x0200 /* Drive fault */
    199  1.24   bouyer #define AT_READREG  0x0400 /* Read registers on completion */
    200  1.31  thorpej 
    201  1.31  thorpej 	int timeout;		/* timeout (in ms) */
    202  1.31  thorpej 	void *data;		/* Data buffer address */
    203  1.31  thorpej 	int bcount;		/* number of bytes to transfer */
    204  1.31  thorpej 	void (*callback)(void *); /* command to call once command completed */
    205  1.31  thorpej 	void *callback_arg;	/* argument passed to *callback() */
    206   1.2   bouyer };
    207  1.33  thorpej 
    208  1.33  thorpej /*
    209  1.33  thorpej  * ata_bustype.  The first field must be compatible with scsipi_bustype,
    210  1.33  thorpej  * as it's used for autoconfig by both ata and atapi drivers.
    211  1.33  thorpej  */
    212  1.33  thorpej struct ata_bustype {
    213  1.33  thorpej 	int	bustype_type;	/* symbolic name of type */
    214  1.33  thorpej 	int	(*ata_bio)(struct ata_drive_datas *, struct ata_bio *);
    215  1.33  thorpej 	void	(*ata_reset_channel)(struct ata_drive_datas *, int);
    216  1.33  thorpej 	int	(*ata_exec_command)(struct ata_drive_datas *,
    217  1.33  thorpej 				    struct wdc_command *);
    218  1.33  thorpej 
    219  1.33  thorpej #define	WDC_COMPLETE	0x01
    220  1.33  thorpej #define	WDC_QUEUED	0x02
    221  1.33  thorpej #define	WDC_TRY_AGAIN	0x03
    222  1.33  thorpej 
    223  1.33  thorpej 	int	(*ata_get_params)(struct ata_drive_datas *, u_int8_t,
    224  1.33  thorpej 				  struct ataparams *);
    225  1.33  thorpej 	int	(*ata_addref)(struct ata_drive_datas *);
    226  1.33  thorpej 	void	(*ata_delref)(struct ata_drive_datas *);
    227  1.33  thorpej 	void	(*ata_killpending)(struct ata_drive_datas *);
    228  1.33  thorpej };
    229  1.33  thorpej 
    230  1.33  thorpej /* bustype_type */	/* XXX XXX XXX */
    231  1.33  thorpej /* #define SCSIPI_BUSTYPE_SCSI	0 */
    232  1.33  thorpej /* #define SCSIPI_BUSTYPE_ATAPI	1 */
    233  1.33  thorpej #define	SCSIPI_BUSTYPE_ATA	2
    234  1.34  thorpej 
    235  1.34  thorpej /*
    236  1.34  thorpej  * Describe an ATA device.  Has to be compatible with scsipi_channel, so
    237  1.34  thorpej  * start with a pointer to ata_bustype.
    238  1.34  thorpej  */
    239  1.34  thorpej struct ata_device {
    240  1.34  thorpej 	const struct ata_bustype *adev_bustype;
    241  1.34  thorpej 	int adev_channel;
    242  1.34  thorpej 	int adev_openings;
    243  1.34  thorpej 	struct ata_drive_datas *adev_drv_data;
    244  1.34  thorpej };
    245  1.26    soren 
    246  1.26    soren /*
    247  1.26    soren  * If WDSM_ATTR_ADVISORY, device exceeded intended design life period.
    248  1.26    soren  * If not WDSM_ATTR_ADVISORY, imminent data loss predicted.
    249  1.26    soren  */
    250  1.26    soren #define WDSM_ATTR_ADVISORY	1
    251  1.31  thorpej 
    252  1.26    soren /*
    253  1.26    soren  * If WDSM_ATTR_COLLECTIVE, attribute only updated in off-line testing.
    254  1.26    soren  * If not WDSM_ATTR_COLLECTIVE, attribute updated also in on-line testing.
    255  1.26    soren  */
    256  1.26    soren #define WDSM_ATTR_COLLECTIVE	2
    257  1.26    soren 
    258  1.26    soren struct ata_smart_attr {
    259  1.26    soren 	u_int8_t		id;		/* attribute id number */
    260  1.26    soren 	u_int16_t		flags;
    261  1.26    soren 	u_int8_t		value;		/* attribute value */
    262  1.36      lha 	u_int8_t		worst;
    263  1.36      lha 	u_int8_t		raw[6];
    264  1.36      lha 	u_int8_t		reserved;
    265  1.26    soren } __attribute__((packed));
    266  1.26    soren 
    267  1.26    soren struct ata_smart_attributes {
    268  1.26    soren 	u_int16_t		data_structure_revision;
    269  1.26    soren 	struct ata_smart_attr	attributes[30];
    270  1.26    soren 	u_int8_t		offline_data_collection_status;
    271  1.26    soren 	u_int8_t		self_test_exec_status;
    272  1.26    soren 	u_int16_t		total_time_to_complete_off_line;
    273  1.26    soren 	u_int8_t		vendor_specific_366;
    274  1.26    soren 	u_int8_t		offline_data_collection_capability;
    275  1.26    soren 	u_int16_t		smart_capability;
    276  1.26    soren 	u_int8_t		errorlog_capability;
    277  1.26    soren 	u_int8_t		vendor_specific_371;
    278  1.26    soren 	u_int8_t		short_test_completion_time;
    279  1.26    soren 	u_int8_t		extend_test_completion_time;
    280  1.26    soren 	u_int8_t		reserved_374_385[12];
    281  1.26    soren 	u_int8_t		vendor_specific_386_509[125];
    282  1.26    soren 	int8_t			checksum;
    283  1.26    soren } __attribute__((packed));
    284  1.26    soren 
    285  1.26    soren struct ata_smart_thresh {
    286  1.26    soren 	u_int8_t		id;
    287  1.26    soren 	u_int8_t		value;
    288  1.26    soren 	u_int8_t		reserved[10];
    289  1.26    soren } __attribute__((packed));
    290  1.26    soren 
    291  1.26    soren struct ata_smart_thresholds {
    292  1.26    soren 	u_int16_t		data_structure_revision;
    293  1.26    soren 	struct ata_smart_thresh	thresholds[30];
    294  1.26    soren 	u_int8_t		reserved[18];
    295  1.26    soren 	u_int8_t		vendor_specific[131];
    296  1.26    soren 	int8_t			checksum;
    297  1.26    soren } __attribute__((packed));
    298   1.2   bouyer 
    299  1.36      lha struct ata_smart_selftest {
    300  1.36      lha 	u_int8_t		number;
    301  1.36      lha 	u_int8_t		status;
    302  1.36      lha 	uint16_t		time_stamp;
    303  1.36      lha 	u_int8_t		failure_check_point;
    304  1.36      lha 	u_int32_t		lba_first_error;
    305  1.36      lha 	u_int8_t		vendor_specific[15];
    306  1.36      lha } __attribute__((packed));
    307  1.36      lha 
    308  1.36      lha struct ata_smart_selftestlog {
    309  1.36      lha 	u_int16_t		data_structure_revision;
    310  1.36      lha 	struct ata_smart_selftest log_entries[21];
    311  1.36      lha 	u_int8_t		vendorspecific[2];
    312  1.36      lha 	u_int8_t		mostrecenttest;
    313  1.36      lha 	u_int8_t		reserved[2];
    314  1.36      lha 	u_int8_t		checksum;
    315  1.36      lha } __attribute__((packed));
    316  1.36      lha 
    317  1.36      lha #ifdef _KERNEL
    318  1.37  thorpej int	atabusprint(void *aux, const char *);
    319  1.37  thorpej int	ataprint(void *aux, const char *);
    320  1.36      lha 
    321  1.30  thorpej int	wdc_downgrade_mode(struct ata_drive_datas *, int);
    322   1.2   bouyer 
    323   1.2   bouyer struct ataparams;
    324  1.30  thorpej int	ata_get_params(struct ata_drive_datas *, u_int8_t, struct ataparams *);
    325  1.30  thorpej int	ata_set_mode(struct ata_drive_datas *, u_int8_t, u_int8_t);
    326   1.2   bouyer /* return code for these cmds */
    327   1.2   bouyer #define CMD_OK    0
    328   1.2   bouyer #define CMD_ERR   1
    329   1.2   bouyer #define CMD_AGAIN 2
    330  1.10   bouyer 
    331  1.30  thorpej void	ata_dmaerr(struct ata_drive_datas *, int);
    332  1.37  thorpej #endif /* _KERNEL */
    333  1.31  thorpej 
    334  1.31  thorpej #endif /* _DEV_ATA_ATAVAR_H_ */
    335