atavar.h revision 1.41 1 1.41 minoura /* $NetBSD: atavar.h,v 1.41 2004/04/14 05:26:29 minoura 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.39 thorpej /*
39 1.39 thorpej * Description of a command to be handled by an ATA controller. These
40 1.39 thorpej * commands are queued in a list.
41 1.39 thorpej */
42 1.39 thorpej struct ata_xfer {
43 1.39 thorpej __volatile u_int c_flags; /* command state flags */
44 1.39 thorpej
45 1.39 thorpej /* Channel and drive that are to process the request. */
46 1.40 thorpej struct wdc_channel *c_chp;
47 1.39 thorpej int c_drive;
48 1.39 thorpej
49 1.39 thorpej void *c_cmd; /* private request structure pointer */
50 1.39 thorpej void *c_databuf; /* pointer to data buffer */
51 1.39 thorpej int c_bcount; /* byte count left */
52 1.39 thorpej int c_skip; /* bytes already transferred */
53 1.39 thorpej int c_dscpoll; /* counter for dsc polling (ATAPI) */
54 1.39 thorpej
55 1.39 thorpej /* Link on the command queue. */
56 1.39 thorpej TAILQ_ENTRY(ata_xfer) c_xferchain;
57 1.39 thorpej
58 1.39 thorpej /* Low-level protocol handlers. */
59 1.40 thorpej void (*c_start)(struct wdc_channel *, struct ata_xfer *);
60 1.40 thorpej int (*c_intr)(struct wdc_channel *, struct ata_xfer *, int);
61 1.40 thorpej void (*c_kill_xfer)(struct wdc_channel *, struct ata_xfer *);
62 1.39 thorpej };
63 1.39 thorpej
64 1.39 thorpej #define C_ATAPI 0x0001 /* xfer is ATAPI request */
65 1.39 thorpej #define C_TIMEOU 0x0002 /* xfer processing timed out */
66 1.39 thorpej #define C_POLL 0x0004 /* command is polled */
67 1.39 thorpej #define C_DMA 0x0008 /* command uses DMA */
68 1.39 thorpej
69 1.39 thorpej /* Per-channel queue of ata_xfers. May be shared by multiple channels. */
70 1.39 thorpej struct ata_queue {
71 1.39 thorpej TAILQ_HEAD(, ata_xfer) queue_xfer;
72 1.39 thorpej int queue_freeze;
73 1.39 thorpej };
74 1.39 thorpej
75 1.37 thorpej /* ATA bus instance state information. */
76 1.37 thorpej struct atabus_softc {
77 1.37 thorpej struct device sc_dev;
78 1.40 thorpej struct wdc_channel *sc_chan; /* XXXwdc */
79 1.37 thorpej };
80 1.37 thorpej
81 1.37 thorpej /*
82 1.37 thorpej * A queue of atabus instances, used to ensure the same bus probe order
83 1.37 thorpej * for a given hardware configuration at each boot.
84 1.37 thorpej */
85 1.37 thorpej struct atabus_initq {
86 1.37 thorpej TAILQ_ENTRY(atabus_initq) atabus_initq;
87 1.37 thorpej struct atabus_softc *atabus_sc;
88 1.37 thorpej };
89 1.37 thorpej
90 1.37 thorpej #ifdef _KERNEL
91 1.37 thorpej TAILQ_HEAD(atabus_initq_head, atabus_initq);
92 1.37 thorpej extern struct atabus_initq_head atabus_initq_head;
93 1.37 thorpej extern struct simplelock atabus_interlock;
94 1.37 thorpej #endif /* _KERNEL */
95 1.37 thorpej
96 1.31 thorpej /* High-level functions and structures used by both ATA and ATAPI devices */
97 1.33 thorpej struct ataparams;
98 1.33 thorpej
99 1.2 bouyer /* Datas common to drives and controller drivers */
100 1.2 bouyer struct ata_drive_datas {
101 1.31 thorpej u_int8_t drive; /* drive number */
102 1.31 thorpej int8_t ata_vers; /* ATA version supported */
103 1.31 thorpej u_int16_t drive_flags; /* bitmask for drives present/absent and cap */
104 1.31 thorpej
105 1.31 thorpej #define DRIVE_ATA 0x0001
106 1.31 thorpej #define DRIVE_ATAPI 0x0002
107 1.31 thorpej #define DRIVE_OLD 0x0004
108 1.31 thorpej #define DRIVE (DRIVE_ATA|DRIVE_ATAPI|DRIVE_OLD)
109 1.31 thorpej #define DRIVE_CAP32 0x0008
110 1.31 thorpej #define DRIVE_DMA 0x0010
111 1.31 thorpej #define DRIVE_UDMA 0x0020
112 1.31 thorpej #define DRIVE_MODE 0x0040 /* the drive reported its mode */
113 1.31 thorpej #define DRIVE_RESET 0x0080 /* reset the drive state at next xfer */
114 1.31 thorpej #define DRIVE_DMAERR 0x0100 /* Udma transfer had crc error, don't try DMA */
115 1.41 minoura #define DRIVE_ATAPIST 0x0200 /* device is an ATAPI tape drive */
116 1.31 thorpej
117 1.31 thorpej /*
118 1.31 thorpej * Current setting of drive's PIO, DMA and UDMA modes.
119 1.31 thorpej * Is initialised by the disks drivers at attach time, and may be
120 1.31 thorpej * changed later by the controller's code if needed
121 1.31 thorpej */
122 1.31 thorpej u_int8_t PIO_mode; /* Current setting of drive's PIO mode */
123 1.31 thorpej u_int8_t DMA_mode; /* Current setting of drive's DMA mode */
124 1.31 thorpej u_int8_t UDMA_mode; /* Current setting of drive's UDMA mode */
125 1.31 thorpej
126 1.31 thorpej /* Supported modes for this drive */
127 1.31 thorpej u_int8_t PIO_cap; /* supported drive's PIO mode */
128 1.31 thorpej u_int8_t DMA_cap; /* supported drive's DMA mode */
129 1.31 thorpej u_int8_t UDMA_cap; /* supported drive's UDMA mode */
130 1.31 thorpej
131 1.31 thorpej /*
132 1.31 thorpej * Drive state.
133 1.31 thorpej * This is reset to 0 after a channel reset.
134 1.31 thorpej */
135 1.31 thorpej u_int8_t state;
136 1.31 thorpej
137 1.16 bouyer #define RESET 0
138 1.29 bouyer #define READY 1
139 1.2 bouyer
140 1.31 thorpej /* numbers of xfers and DMA errs. Used by ata_dmaerr() */
141 1.31 thorpej u_int8_t n_dmaerrs;
142 1.31 thorpej u_int32_t n_xfers;
143 1.31 thorpej
144 1.31 thorpej /* Downgrade after NERRS_MAX errors in at most NXFER xfers */
145 1.15 bouyer #define NERRS_MAX 4
146 1.15 bouyer #define NXFER 4000
147 1.35 thorpej
148 1.35 thorpej /* Callbacks into the drive's driver. */
149 1.35 thorpej void (*drv_done)(void *); /* transfer is done */
150 1.9 bouyer
151 1.31 thorpej struct device *drv_softc; /* ATA drives softc, if any */
152 1.31 thorpej void *chnl_softc; /* channel softc */
153 1.2 bouyer };
154 1.2 bouyer
155 1.7 bouyer /* User config flags that force (or disable) the use of a mode */
156 1.7 bouyer #define ATA_CONFIG_PIO_MODES 0x0007
157 1.7 bouyer #define ATA_CONFIG_PIO_SET 0x0008
158 1.7 bouyer #define ATA_CONFIG_PIO_OFF 0
159 1.7 bouyer #define ATA_CONFIG_DMA_MODES 0x0070
160 1.7 bouyer #define ATA_CONFIG_DMA_SET 0x0080
161 1.7 bouyer #define ATA_CONFIG_DMA_DISABLE 0x0070
162 1.7 bouyer #define ATA_CONFIG_DMA_OFF 4
163 1.7 bouyer #define ATA_CONFIG_UDMA_MODES 0x0700
164 1.7 bouyer #define ATA_CONFIG_UDMA_SET 0x0800
165 1.7 bouyer #define ATA_CONFIG_UDMA_DISABLE 0x0700
166 1.7 bouyer #define ATA_CONFIG_UDMA_OFF 8
167 1.32 thorpej
168 1.32 thorpej /*
169 1.32 thorpej * Parameters/state needed by the controller to perform an ATA bio.
170 1.32 thorpej */
171 1.32 thorpej struct ata_bio {
172 1.32 thorpej volatile u_int16_t flags;/* cmd flags */
173 1.32 thorpej #define ATA_NOSLEEP 0x0001 /* Can't sleep */
174 1.32 thorpej #define ATA_POLL 0x0002 /* poll for completion */
175 1.32 thorpej #define ATA_ITSDONE 0x0004 /* the transfer is as done as it gets */
176 1.32 thorpej #define ATA_SINGLE 0x0008 /* transfer must be done in singlesector mode */
177 1.32 thorpej #define ATA_LBA 0x0010 /* transfer uses LBA addressing */
178 1.32 thorpej #define ATA_READ 0x0020 /* transfer is a read (otherwise a write) */
179 1.32 thorpej #define ATA_CORR 0x0040 /* transfer had a corrected error */
180 1.32 thorpej #define ATA_LBA48 0x0080 /* transfer uses 48-bit LBA addressing */
181 1.32 thorpej int multi; /* # of blocks to transfer in multi-mode */
182 1.32 thorpej struct disklabel *lp; /* pointer to drive's label info */
183 1.32 thorpej daddr_t blkno; /* block addr */
184 1.32 thorpej daddr_t blkdone;/* number of blks transferred */
185 1.32 thorpej daddr_t nblks; /* number of block currently transferring */
186 1.32 thorpej int nbytes; /* number of bytes currently transferring */
187 1.32 thorpej long bcount; /* total number of bytes */
188 1.32 thorpej char *databuf;/* data buffer address */
189 1.32 thorpej volatile int error;
190 1.32 thorpej #define NOERROR 0 /* There was no error (r_error invalid) */
191 1.32 thorpej #define ERROR 1 /* check r_error */
192 1.32 thorpej #define ERR_DF 2 /* Drive fault */
193 1.32 thorpej #define ERR_DMA 3 /* DMA error */
194 1.32 thorpej #define TIMEOUT 4 /* device timed out */
195 1.32 thorpej #define ERR_NODEV 5 /* device has been gone */
196 1.32 thorpej u_int8_t r_error;/* copy of error register */
197 1.32 thorpej daddr_t badsect[127];/* 126 plus trailing -1 marker */
198 1.32 thorpej };
199 1.2 bouyer
200 1.2 bouyer /*
201 1.2 bouyer * ATA/ATAPI commands description
202 1.2 bouyer *
203 1.2 bouyer * This structure defines the interface between the ATA/ATAPI device driver
204 1.2 bouyer * and the controller for short commands. It contains the command's parameter,
205 1.2 bouyer * the len of data's to read/write (if any), and a function to call upon
206 1.2 bouyer * completion.
207 1.2 bouyer * If no sleep is allowed, the driver can poll for command completion.
208 1.2 bouyer * Once the command completed, if the error registed is valid, the flag
209 1.2 bouyer * AT_ERROR is set and the error register value is copied to r_error .
210 1.2 bouyer * A separate interface is needed for read/write or ATAPI packet commands
211 1.2 bouyer * (which need multiple interrupts per commands).
212 1.2 bouyer */
213 1.2 bouyer struct wdc_command {
214 1.31 thorpej u_int8_t r_command; /* Parameters to upload to registers */
215 1.31 thorpej u_int8_t r_head;
216 1.31 thorpej u_int16_t r_cyl;
217 1.31 thorpej u_int8_t r_sector;
218 1.31 thorpej u_int8_t r_count;
219 1.31 thorpej u_int8_t r_precomp;
220 1.31 thorpej u_int8_t r_st_bmask; /* status register mask to wait for before
221 1.31 thorpej command */
222 1.31 thorpej u_int8_t r_st_pmask; /* status register mask to wait for after
223 1.31 thorpej command */
224 1.31 thorpej u_int8_t r_error; /* error register after command done */
225 1.31 thorpej volatile u_int16_t flags;
226 1.31 thorpej
227 1.2 bouyer #define AT_READ 0x0001 /* There is data to read */
228 1.2 bouyer #define AT_WRITE 0x0002 /* There is data to write (excl. with AT_READ) */
229 1.2 bouyer #define AT_WAIT 0x0008 /* wait in controller code for command completion */
230 1.2 bouyer #define AT_POLL 0x0010 /* poll for command completion (no interrupts) */
231 1.2 bouyer #define AT_DONE 0x0020 /* command is done */
232 1.24 bouyer #define AT_XFDONE 0x0040 /* data xfer is done */
233 1.24 bouyer #define AT_ERROR 0x0080 /* command is done with error */
234 1.24 bouyer #define AT_TIMEOU 0x0100 /* command timed out */
235 1.24 bouyer #define AT_DF 0x0200 /* Drive fault */
236 1.24 bouyer #define AT_READREG 0x0400 /* Read registers on completion */
237 1.31 thorpej
238 1.31 thorpej int timeout; /* timeout (in ms) */
239 1.31 thorpej void *data; /* Data buffer address */
240 1.31 thorpej int bcount; /* number of bytes to transfer */
241 1.31 thorpej void (*callback)(void *); /* command to call once command completed */
242 1.31 thorpej void *callback_arg; /* argument passed to *callback() */
243 1.2 bouyer };
244 1.33 thorpej
245 1.33 thorpej /*
246 1.33 thorpej * ata_bustype. The first field must be compatible with scsipi_bustype,
247 1.33 thorpej * as it's used for autoconfig by both ata and atapi drivers.
248 1.33 thorpej */
249 1.33 thorpej struct ata_bustype {
250 1.33 thorpej int bustype_type; /* symbolic name of type */
251 1.33 thorpej int (*ata_bio)(struct ata_drive_datas *, struct ata_bio *);
252 1.33 thorpej void (*ata_reset_channel)(struct ata_drive_datas *, int);
253 1.33 thorpej int (*ata_exec_command)(struct ata_drive_datas *,
254 1.33 thorpej struct wdc_command *);
255 1.33 thorpej
256 1.33 thorpej #define WDC_COMPLETE 0x01
257 1.33 thorpej #define WDC_QUEUED 0x02
258 1.33 thorpej #define WDC_TRY_AGAIN 0x03
259 1.33 thorpej
260 1.33 thorpej int (*ata_get_params)(struct ata_drive_datas *, u_int8_t,
261 1.33 thorpej struct ataparams *);
262 1.33 thorpej int (*ata_addref)(struct ata_drive_datas *);
263 1.33 thorpej void (*ata_delref)(struct ata_drive_datas *);
264 1.33 thorpej void (*ata_killpending)(struct ata_drive_datas *);
265 1.33 thorpej };
266 1.33 thorpej
267 1.33 thorpej /* bustype_type */ /* XXX XXX XXX */
268 1.33 thorpej /* #define SCSIPI_BUSTYPE_SCSI 0 */
269 1.33 thorpej /* #define SCSIPI_BUSTYPE_ATAPI 1 */
270 1.33 thorpej #define SCSIPI_BUSTYPE_ATA 2
271 1.34 thorpej
272 1.34 thorpej /*
273 1.34 thorpej * Describe an ATA device. Has to be compatible with scsipi_channel, so
274 1.34 thorpej * start with a pointer to ata_bustype.
275 1.34 thorpej */
276 1.34 thorpej struct ata_device {
277 1.34 thorpej const struct ata_bustype *adev_bustype;
278 1.34 thorpej int adev_channel;
279 1.34 thorpej int adev_openings;
280 1.34 thorpej struct ata_drive_datas *adev_drv_data;
281 1.34 thorpej };
282 1.26 soren
283 1.36 lha #ifdef _KERNEL
284 1.37 thorpej int atabusprint(void *aux, const char *);
285 1.37 thorpej int ataprint(void *aux, const char *);
286 1.36 lha
287 1.30 thorpej int wdc_downgrade_mode(struct ata_drive_datas *, int);
288 1.2 bouyer
289 1.2 bouyer struct ataparams;
290 1.30 thorpej int ata_get_params(struct ata_drive_datas *, u_int8_t, struct ataparams *);
291 1.30 thorpej int ata_set_mode(struct ata_drive_datas *, u_int8_t, u_int8_t);
292 1.2 bouyer /* return code for these cmds */
293 1.2 bouyer #define CMD_OK 0
294 1.2 bouyer #define CMD_ERR 1
295 1.2 bouyer #define CMD_AGAIN 2
296 1.10 bouyer
297 1.30 thorpej void ata_dmaerr(struct ata_drive_datas *, int);
298 1.37 thorpej #endif /* _KERNEL */
299 1.31 thorpej
300 1.31 thorpej #endif /* _DEV_ATA_ATAVAR_H_ */
301