Home | History | Annotate | Line # | Download | only in ata
atavar.h revision 1.28
      1 /*	$NetBSD: atavar.h,v 1.28 2003/10/05 17:48:49 bouyer Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1998, 2001 Manuel Bouyer.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *	This product includes software developed by Manuel Bouyer.
     17  * 4. The name of the author may not be used to endorse or promote products
     18  *    derived from this software without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  *
     31  */
     32 
     33 /* Hight-level functions and structures used by both ATA and ATAPI devices */
     34 
     35 /* Datas common to drives and controller drivers */
     36 struct ata_drive_datas {
     37     u_int8_t drive; /* drive number */
     38     int8_t ata_vers; /* ATA version supported */
     39     u_int16_t drive_flags; /* bitmask for drives present/absent and cap */
     40 #define DRIVE_ATA	0x0001
     41 #define DRIVE_ATAPI	0x0002
     42 #define DRIVE_OLD	0x0004
     43 #define DRIVE (DRIVE_ATA|DRIVE_ATAPI|DRIVE_OLD)
     44 #define DRIVE_CAP32	0x0008
     45 #define DRIVE_DMA	0x0010
     46 #define DRIVE_UDMA	0x0020
     47 #define DRIVE_MODE	0x0040 /* the drive reported its mode */
     48 #define DRIVE_RESET	0x0080 /* reset the drive state at next xfer */
     49 #define DRIVE_DMAERR	0x0100 /* Udma transfer had crc error, don't try DMA */
     50 #define DRIVE_ATAPIST	0x0100 /* device is an ATAPI tape drive */
     51     /*
     52      * Current setting of drive's PIO, DMA and UDMA modes.
     53      * Is initialised by the disks drivers at attach time, and may be
     54      * changed later by the controller's code if needed
     55      */
     56     u_int8_t PIO_mode; /* Current setting of drive's PIO mode */
     57     u_int8_t DMA_mode; /* Current setting of drive's DMA mode */
     58     u_int8_t UDMA_mode; /* Current setting of drive's UDMA mode */
     59     /* Supported modes for this drive */
     60     u_int8_t PIO_cap; /* supported drive's PIO mode */
     61     u_int8_t DMA_cap; /* supported drive's DMA mode */
     62     u_int8_t UDMA_cap; /* supported drive's UDMA mode */
     63     /*
     64      * Drive state.
     65      * This is reset to 0 after a channel reset.
     66      */
     67     u_int8_t state;
     68 #define RESET          0
     69 #define RECAL          1
     70 #define RECAL_WAIT     2
     71 #define PIOMODE        3
     72 #define PIOMODE_WAIT   4
     73 #define DMAMODE        5
     74 #define DMAMODE_WAIT   6
     75 #define GEOMETRY       7
     76 #define GEOMETRY_WAIT  8
     77 #define MULTIMODE      9
     78 #define MULTIMODE_WAIT 10
     79 #define READY          11
     80 
     81     /* numbers of xfers and DMA errs. Used by ata_dmaerr() */
     82     u_int8_t n_dmaerrs;
     83     u_int32_t n_xfers;
     84     /* Downgrade after NERRS_MAX errors in at most NXFER xfers */
     85 #define NERRS_MAX 4
     86 #define NXFER 4000
     87 
     88     struct device *drv_softc; /* ATA drives softc, if any */
     89     void *chnl_softc; /* channel softc */
     90 };
     91 
     92 /* User config flags that force (or disable) the use of a mode */
     93 #define ATA_CONFIG_PIO_MODES	0x0007
     94 #define ATA_CONFIG_PIO_SET	0x0008
     95 #define ATA_CONFIG_PIO_OFF	0
     96 #define ATA_CONFIG_DMA_MODES	0x0070
     97 #define ATA_CONFIG_DMA_SET	0x0080
     98 #define ATA_CONFIG_DMA_DISABLE	0x0070
     99 #define ATA_CONFIG_DMA_OFF	4
    100 #define ATA_CONFIG_UDMA_MODES	0x0700
    101 #define ATA_CONFIG_UDMA_SET	0x0800
    102 #define ATA_CONFIG_UDMA_DISABLE	0x0700
    103 #define ATA_CONFIG_UDMA_OFF	8
    104 
    105 /*
    106  * ATA/ATAPI commands description
    107  *
    108  * This structure defines the interface between the ATA/ATAPI device driver
    109  * and the controller for short commands. It contains the command's parameter,
    110  * the len of data's to read/write (if any), and a function to call upon
    111  * completion.
    112  * If no sleep is allowed, the driver can poll for command completion.
    113  * Once the command completed, if the error registed is valid, the flag
    114  * AT_ERROR is set and the error register value is copied to r_error .
    115  * A separate interface is needed for read/write or ATAPI packet commands
    116  * (which need multiple interrupts per commands).
    117  */
    118 struct wdc_command {
    119     u_int8_t r_command;  /* Parameters to upload to registers */
    120     u_int8_t r_head;
    121     u_int16_t r_cyl;
    122     u_int8_t r_sector;
    123     u_int8_t r_count;
    124     u_int8_t r_precomp;
    125     u_int8_t r_st_bmask; /* status register mask to wait for before command */
    126     u_int8_t r_st_pmask; /* status register mask to wait for after command */
    127     u_int8_t r_error;    /* error register after command done */
    128     volatile u_int16_t flags;
    129 #define AT_READ     0x0001 /* There is data to read */
    130 #define AT_WRITE    0x0002 /* There is data to write (excl. with AT_READ) */
    131 #define AT_WAIT     0x0008 /* wait in controller code for command completion */
    132 #define AT_POLL     0x0010 /* poll for command completion (no interrupts) */
    133 #define AT_DONE     0x0020 /* command is done */
    134 #define AT_XFDONE   0x0040 /* data xfer is done */
    135 #define AT_ERROR    0x0080 /* command is done with error */
    136 #define AT_TIMEOU   0x0100 /* command timed out */
    137 #define AT_DF       0x0200 /* Drive fault */
    138 #define AT_READREG  0x0400 /* Read registers on completion */
    139     int timeout;	 /* timeout (in ms) */
    140     void *data;          /* Data buffer address */
    141     int bcount;           /* number of bytes to transfer */
    142     void (*callback) __P((void *)); /* command to call once command completed */
    143     void *callback_arg;  /* argument passed to *callback() */
    144 };
    145 
    146 /*
    147  * If WDSM_ATTR_ADVISORY, device exceeded intended design life period.
    148  * If not WDSM_ATTR_ADVISORY, imminent data loss predicted.
    149  */
    150 #define WDSM_ATTR_ADVISORY	1
    151 /*
    152  * If WDSM_ATTR_COLLECTIVE, attribute only updated in off-line testing.
    153  * If not WDSM_ATTR_COLLECTIVE, attribute updated also in on-line testing.
    154  */
    155 #define WDSM_ATTR_COLLECTIVE	2
    156 
    157 struct ata_smart_attr {
    158 	u_int8_t		id;		/* attribute id number */
    159 	u_int16_t		flags;
    160 	u_int8_t		value;		/* attribute value */
    161 	u_int8_t		vendor_specific[8];
    162 } __attribute__((packed));
    163 
    164 struct ata_smart_attributes {
    165 	u_int16_t		data_structure_revision;
    166 	struct ata_smart_attr	attributes[30];
    167 	u_int8_t		offline_data_collection_status;
    168 	u_int8_t		self_test_exec_status;
    169 	u_int16_t		total_time_to_complete_off_line;
    170 	u_int8_t		vendor_specific_366;
    171 	u_int8_t		offline_data_collection_capability;
    172 	u_int16_t		smart_capability;
    173 	u_int8_t		errorlog_capability;
    174 	u_int8_t		vendor_specific_371;
    175 	u_int8_t		short_test_completion_time;
    176 	u_int8_t		extend_test_completion_time;
    177 	u_int8_t		reserved_374_385[12];
    178 	u_int8_t		vendor_specific_386_509[125];
    179 	int8_t			checksum;
    180 } __attribute__((packed));
    181 
    182 struct ata_smart_thresh {
    183 	u_int8_t		id;
    184 	u_int8_t		value;
    185 	u_int8_t		reserved[10];
    186 } __attribute__((packed));
    187 
    188 struct ata_smart_thresholds {
    189 	u_int16_t		data_structure_revision;
    190 	struct ata_smart_thresh	thresholds[30];
    191 	u_int8_t		reserved[18];
    192 	u_int8_t		vendor_specific[131];
    193 	int8_t			checksum;
    194 } __attribute__((packed));
    195 
    196 int  wdc_downgrade_mode __P((struct ata_drive_datas *));
    197 
    198 struct ataparams;
    199 int ata_get_params __P((struct ata_drive_datas *, u_int8_t,
    200 	 struct ataparams *));
    201 int ata_set_mode __P((struct ata_drive_datas *, u_int8_t, u_int8_t));
    202 /* return code for these cmds */
    203 #define CMD_OK    0
    204 #define CMD_ERR   1
    205 #define CMD_AGAIN 2
    206 
    207 void ata_dmaerr __P((struct ata_drive_datas *));
    208