xref: /netbsd-src/sys/dev/ata/atavar.h (revision fad4c9f71477ae11cea2ee75ec82151ac770a534)
1 /*	$NetBSD: atavar.h,v 1.71 2005/12/24 20:27:29 perry 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 #ifndef _DEV_ATA_ATAVAR_H_
33 #define	_DEV_ATA_ATAVAR_H_
34 
35 #include <sys/lock.h>
36 #include <sys/queue.h>
37 
38 /* XXX For scsipi_adapter and scsipi_channel. */
39 #include <dev/scsipi/scsipi_all.h>
40 #include <dev/scsipi/atapiconf.h>
41 
42 /*
43  * Max number of drives per channel.
44  */
45 #define	ATA_MAXDRIVES		2
46 
47 /*
48  * Description of a command to be handled by an ATA controller.  These
49  * commands are queued in a list.
50  */
51 struct ata_xfer {
52 	volatile u_int c_flags;	/* command state flags */
53 
54 	/* Channel and drive that are to process the request. */
55 	struct ata_channel *c_chp;
56 	int	c_drive;
57 
58 	void	*c_cmd;			/* private request structure pointer */
59 	void	*c_databuf;		/* pointer to data buffer */
60 	int	c_bcount;		/* byte count left */
61 	int	c_skip;			/* bytes already transferred */
62 	int	c_dscpoll;		/* counter for dsc polling (ATAPI) */
63 
64 	/* Link on the command queue. */
65 	TAILQ_ENTRY(ata_xfer) c_xferchain;
66 
67 	/* Low-level protocol handlers. */
68 	void	(*c_start)(struct ata_channel *, struct ata_xfer *);
69 	int	(*c_intr)(struct ata_channel *, struct ata_xfer *, int);
70 	void	(*c_kill_xfer)(struct ata_channel *, struct ata_xfer *, int);
71 };
72 
73 /* flags in c_flags */
74 #define	C_ATAPI		0x0001		/* xfer is ATAPI request */
75 #define	C_TIMEOU	0x0002		/* xfer processing timed out */
76 #define	C_POLL		0x0004		/* command is polled */
77 #define	C_DMA		0x0008		/* command uses DMA */
78 #define C_WAIT		0x0010		/* can use tsleep */
79 #define C_WAITACT	0x0020		/* wakeup when active */
80 #define C_FREE		0x0040		/* call ata_free_xfer() asap */
81 
82 /* reasons for c_kill_xfer() */
83 #define KILL_GONE 1 /* device is gone */
84 #define KILL_RESET 2 /* xfer was reset */
85 
86 /* Per-channel queue of ata_xfers.  May be shared by multiple channels. */
87 struct ata_queue {
88 	TAILQ_HEAD(, ata_xfer) queue_xfer; /* queue of pending commands */
89 	int queue_freeze; /* freeze count for the queue */
90 	struct ata_xfer *active_xfer; /* active command */
91 	int queue_flags;	/* flags for this queue */
92 #define QF_IDLE_WAIT   0x01    /* someone is wants the controller idle */
93 };
94 
95 /* ATA bus instance state information. */
96 struct atabus_softc {
97 	struct device sc_dev;
98 	struct ata_channel *sc_chan;
99 	int sc_flags;
100 #define ATABUSCF_OPEN	0x01
101 	void *sc_powerhook;
102 };
103 
104 /*
105  * A queue of atabus instances, used to ensure the same bus probe order
106  * for a given hardware configuration at each boot.
107  */
108 struct atabus_initq {
109 	TAILQ_ENTRY(atabus_initq) atabus_initq;
110 	struct atabus_softc *atabus_sc;
111 };
112 
113 #ifdef _KERNEL
114 TAILQ_HEAD(atabus_initq_head, atabus_initq);
115 extern struct atabus_initq_head atabus_initq_head;
116 extern struct simplelock atabus_interlock;
117 #endif /* _KERNEL */
118 
119 /* High-level functions and structures used by both ATA and ATAPI devices */
120 struct ataparams;
121 
122 /* Datas common to drives and controller drivers */
123 struct ata_drive_datas {
124 	u_int8_t drive;		/* drive number */
125 	int8_t ata_vers;	/* ATA version supported */
126 	u_int16_t drive_flags;	/* bitmask for drives present/absent and cap */
127 
128 #define	DRIVE_ATA	0x0001
129 #define	DRIVE_ATAPI	0x0002
130 #define	DRIVE_OLD	0x0004
131 #define	DRIVE		(DRIVE_ATA|DRIVE_ATAPI|DRIVE_OLD)
132 #define	DRIVE_CAP32	0x0008
133 #define	DRIVE_DMA	0x0010
134 #define	DRIVE_UDMA	0x0020
135 #define	DRIVE_MODE	0x0040	/* the drive reported its mode */
136 #define	DRIVE_RESET	0x0080	/* reset the drive state at next xfer */
137 #define	DRIVE_WAITDRAIN	0x0100	/* device is waiting for the queue to drain */
138 #define	DRIVE_ATAPIST	0x0200	/* device is an ATAPI tape drive */
139 #define	DRIVE_NOSTREAM	0x0400	/* no stream methods on this drive */
140 
141 	/*
142 	 * Current setting of drive's PIO, DMA and UDMA modes.
143 	 * Is initialised by the disks drivers at attach time, and may be
144 	 * changed later by the controller's code if needed
145 	 */
146 	u_int8_t PIO_mode;	/* Current setting of drive's PIO mode */
147 	u_int8_t DMA_mode;	/* Current setting of drive's DMA mode */
148 	u_int8_t UDMA_mode;	/* Current setting of drive's UDMA mode */
149 
150 	/* Supported modes for this drive */
151 	u_int8_t PIO_cap;	/* supported drive's PIO mode */
152 	u_int8_t DMA_cap;	/* supported drive's DMA mode */
153 	u_int8_t UDMA_cap;	/* supported drive's UDMA mode */
154 
155 	/*
156 	 * Drive state.
157 	 * This is reset to 0 after a channel reset.
158 	 */
159 	u_int8_t state;
160 
161 #define RESET          0
162 #define READY          1
163 
164 	/* numbers of xfers and DMA errs. Used by ata_dmaerr() */
165 	u_int8_t n_dmaerrs;
166 	u_int32_t n_xfers;
167 
168 	/* Downgrade after NERRS_MAX errors in at most NXFER xfers */
169 #define NERRS_MAX 4
170 #define NXFER 4000
171 
172 	/* Callbacks into the drive's driver. */
173 	void	(*drv_done)(void *);	/* transfer is done */
174 
175 	struct device *drv_softc;	/* ATA drives softc, if any */
176 	void *chnl_softc;		/* channel softc */
177 };
178 
179 /* User config flags that force (or disable) the use of a mode */
180 #define ATA_CONFIG_PIO_MODES	0x0007
181 #define ATA_CONFIG_PIO_SET	0x0008
182 #define ATA_CONFIG_PIO_OFF	0
183 #define ATA_CONFIG_DMA_MODES	0x0070
184 #define ATA_CONFIG_DMA_SET	0x0080
185 #define ATA_CONFIG_DMA_DISABLE	0x0070
186 #define ATA_CONFIG_DMA_OFF	4
187 #define ATA_CONFIG_UDMA_MODES	0x0700
188 #define ATA_CONFIG_UDMA_SET	0x0800
189 #define ATA_CONFIG_UDMA_DISABLE	0x0700
190 #define ATA_CONFIG_UDMA_OFF	8
191 
192 /*
193  * Parameters/state needed by the controller to perform an ATA bio.
194  */
195 struct ata_bio {
196 	volatile u_int16_t flags;/* cmd flags */
197 #define	ATA_NOSLEEP	0x0001	/* Can't sleep */
198 #define	ATA_POLL	0x0002	/* poll for completion */
199 #define	ATA_ITSDONE	0x0004	/* the transfer is as done as it gets */
200 #define	ATA_SINGLE	0x0008	/* transfer must be done in singlesector mode */
201 #define	ATA_LBA		0x0010	/* transfer uses LBA addressing */
202 #define	ATA_READ	0x0020	/* transfer is a read (otherwise a write) */
203 #define	ATA_CORR	0x0040	/* transfer had a corrected error */
204 #define	ATA_LBA48	0x0080	/* transfer uses 48-bit LBA addressing */
205 	int		multi;	/* # of blocks to transfer in multi-mode */
206 	struct disklabel *lp;	/* pointer to drive's label info */
207 	daddr_t		blkno;	/* block addr */
208 	daddr_t		blkdone;/* number of blks transferred */
209 	daddr_t		nblks;	/* number of block currently transferring */
210 	int		nbytes;	/* number of bytes currently transferring */
211 	long		bcount;	/* total number of bytes */
212 	char		*databuf;/* data buffer address */
213 	volatile int	error;
214 #define	NOERROR 	0	/* There was no error (r_error invalid) */
215 #define	ERROR		1	/* check r_error */
216 #define	ERR_DF		2	/* Drive fault */
217 #define	ERR_DMA		3	/* DMA error */
218 #define	TIMEOUT		4	/* device timed out */
219 #define	ERR_NODEV	5	/* device has been gone */
220 #define ERR_RESET	6	/* command was terminated by channel reset */
221 	u_int8_t	r_error;/* copy of error register */
222 	daddr_t		badsect[127];/* 126 plus trailing -1 marker */
223 };
224 
225 /*
226  * ATA/ATAPI commands description
227  *
228  * This structure defines the interface between the ATA/ATAPI device driver
229  * and the controller for short commands. It contains the command's parameter,
230  * the len of data's to read/write (if any), and a function to call upon
231  * completion.
232  * If no sleep is allowed, the driver can poll for command completion.
233  * Once the command completed, if the error registed is valid, the flag
234  * AT_ERROR is set and the error register value is copied to r_error .
235  * A separate interface is needed for read/write or ATAPI packet commands
236  * (which need multiple interrupts per commands).
237  */
238 struct ata_command {
239 	u_int8_t r_command;	/* Parameters to upload to registers */
240 	u_int8_t r_head;
241 	u_int16_t r_cyl;
242 	u_int8_t r_sector;
243 	u_int8_t r_count;
244 	u_int8_t r_features;
245 	u_int8_t r_st_bmask;	/* status register mask to wait for before
246 				   command */
247 	u_int8_t r_st_pmask;	/* status register mask to wait for after
248 				   command */
249 	u_int8_t r_error;	/* error register after command done */
250 	volatile u_int16_t flags;
251 
252 #define AT_READ     0x0001 /* There is data to read */
253 #define AT_WRITE    0x0002 /* There is data to write (excl. with AT_READ) */
254 #define AT_WAIT     0x0008 /* wait in controller code for command completion */
255 #define AT_POLL     0x0010 /* poll for command completion (no interrupts) */
256 #define AT_DONE     0x0020 /* command is done */
257 #define AT_XFDONE   0x0040 /* data xfer is done */
258 #define AT_ERROR    0x0080 /* command is done with error */
259 #define AT_TIMEOU   0x0100 /* command timed out */
260 #define AT_DF       0x0200 /* Drive fault */
261 #define AT_RESET    0x0400 /* command terminated by channel reset */
262 #define AT_GONE     0x0800 /* command terminated because device is gone */
263 #define AT_READREG  0x1000 /* Read registers on completion */
264 
265 	int timeout;		/* timeout (in ms) */
266 	void *data;		/* Data buffer address */
267 	int bcount;		/* number of bytes to transfer */
268 	void (*callback)(void *); /* command to call once command completed */
269 	void *callback_arg;	/* argument passed to *callback() */
270 };
271 
272 /*
273  * ata_bustype.  The first field must be compatible with scsipi_bustype,
274  * as it's used for autoconfig by both ata and atapi drivers.
275  */
276 struct ata_bustype {
277 	int	bustype_type;	/* symbolic name of type */
278 	int	(*ata_bio)(struct ata_drive_datas *, struct ata_bio *);
279 	void	(*ata_reset_drive)(struct ata_drive_datas *, int);
280 	void	(*ata_reset_channel)(struct ata_channel *, int);
281 /* extra flags for ata_reset_*(), in addition to AT_* */
282 #define AT_RST_EMERG 0x10000 /* emergency - e.g. for a dump */
283 #define	AT_RST_NOCMD 0x20000 /* XXX has to go - temporary until we have tagged queuing */
284 
285 	int	(*ata_exec_command)(struct ata_drive_datas *,
286 				    struct ata_command *);
287 
288 #define	ATACMD_COMPLETE		0x01
289 #define	ATACMD_QUEUED		0x02
290 #define	ATACMD_TRY_AGAIN	0x03
291 
292 	int	(*ata_get_params)(struct ata_drive_datas *, u_int8_t,
293 				  struct ataparams *);
294 	int	(*ata_addref)(struct ata_drive_datas *);
295 	void	(*ata_delref)(struct ata_drive_datas *);
296 	void	(*ata_killpending)(struct ata_drive_datas *);
297 };
298 
299 /* bustype_type */	/* XXX XXX XXX */
300 /* #define SCSIPI_BUSTYPE_SCSI	0 */
301 /* #define SCSIPI_BUSTYPE_ATAPI	1 */
302 #define	SCSIPI_BUSTYPE_ATA	2
303 
304 /*
305  * Describe an ATA device.  Has to be compatible with scsipi_channel, so
306  * start with a pointer to ata_bustype.
307  */
308 struct ata_device {
309 	const struct ata_bustype *adev_bustype;
310 	int adev_channel;
311 	int adev_openings;
312 	struct ata_drive_datas *adev_drv_data;
313 };
314 
315 /*
316  * Per-channel data
317  */
318 struct ata_channel {
319 	struct callout ch_callout;	/* callout handle */
320 	int ch_channel;			/* location */
321 	struct atac_softc *ch_atac;	/* ATA controller softc */
322 
323 	/* Our state */
324 	volatile int ch_flags;
325 #define ATACH_SHUTDOWN 0x02	/* channel is shutting down */
326 #define ATACH_IRQ_WAIT 0x10	/* controller is waiting for irq */
327 #define ATACH_DMA_WAIT 0x20	/* controller is waiting for DMA */
328 #define	ATACH_DISABLED 0x80	/* channel is disabled */
329 #define ATACH_TH_RUN   0x100	/* the kernel thread is working */
330 #define ATACH_TH_RESET 0x200	/* someone ask the thread to reset */
331 	u_int8_t ch_status;	/* copy of status register */
332 	u_int8_t ch_error;	/* copy of error register */
333 
334 	/* for the reset callback */
335 	int ch_reset_flags;
336 
337 	/* per-drive info */
338 	int ch_ndrive;
339 	struct ata_drive_datas ch_drive[ATA_MAXDRIVES];
340 
341 	struct device *atabus;	/* self */
342 
343 	/* ATAPI children */
344 	struct device *atapibus;
345 	struct scsipi_channel ch_atapi_channel;
346 
347 	/* ATA children */
348 	struct device *ata_drives[ATA_MAXDRIVES];
349 
350 	/*
351 	 * Channel queues.  May be the same for all channels, if hw
352 	 * channels are not independent.
353 	 */
354 	struct ata_queue *ch_queue;
355 
356 	/* The channel kernel thread */
357 	struct proc *ch_thread;
358 };
359 
360 /*
361  * ATA controller softc.
362  *
363  * This contains a bunch of generic info that all ATA controllers need
364  * to have.
365  *
366  * XXX There is still some lingering wdc-centricity here.
367  */
368 struct atac_softc {
369 	struct device atac_dev;		/* generic device info */
370 
371 	int	atac_cap;		/* controller capabilities */
372 
373 #define	ATAC_CAP_DATA16	0x0001		/* can do 16-bit data access */
374 #define	ATAC_CAP_DATA32	0x0002		/* can do 32-bit data access */
375 #define	ATAC_CAP_DMA	0x0008		/* can do ATA DMA modes */
376 #define	ATAC_CAP_UDMA	0x0010		/* can do ATA Ultra DMA modes */
377 #define	ATAC_CAP_ATA_NOSTREAM 0x0040	/* don't use stream funcs on ATA */
378 #define	ATAC_CAP_ATAPI_NOSTREAM 0x0080	/* don't use stream funcs on ATAPI */
379 #define	ATAC_CAP_NOIRQ	0x1000		/* controller never interrupts */
380 #define	ATAC_CAP_RAID	0x4000		/* controller "supports" RAID */
381 
382 	uint8_t	atac_pio_cap;		/* highest PIO mode supported */
383 	uint8_t	atac_dma_cap;		/* highest DMA mode supported */
384 	uint8_t	atac_udma_cap;		/* highest UDMA mode supported */
385 
386 	/* Array of pointers to channel-specific data. */
387 	struct ata_channel **atac_channels;
388 	int		     atac_nchannels;
389 
390 	const struct ata_bustype *atac_bustype_ata;
391 
392 	/*
393 	 * Glue between ATA and SCSIPI for the benefit of ATAPI.
394 	 *
395 	 * Note: The reference count here is used for both ATA and ATAPI
396 	 * devices.
397 	 */
398 	struct atapi_adapter atac_atapi_adapter;
399 	void (*atac_atapibus_attach)(struct atabus_softc *);
400 
401 	/* Driver callback to probe for drives. */
402 	void (*atac_probe)(struct ata_channel *);
403 
404 	/* Optional callbacks to lock/unlock hardware. */
405 	int  (*atac_claim_hw)(struct ata_channel *, int);
406 	void (*atac_free_hw)(struct ata_channel *);
407 
408 	/*
409 	 * Optional callbacks to set drive mode.  Required for anything
410 	 * but basic PIO operation.
411 	 */
412 	void (*atac_set_modes)(struct ata_channel *);
413 };
414 
415 #ifdef _KERNEL
416 void	ata_channel_attach(struct ata_channel *);
417 int	atabusprint(void *aux, const char *);
418 int	ataprint(void *aux, const char *);
419 
420 struct ataparams;
421 int	ata_get_params(struct ata_drive_datas *, u_int8_t, struct ataparams *);
422 int	ata_set_mode(struct ata_drive_datas *, u_int8_t, u_int8_t);
423 /* return code for these cmds */
424 #define CMD_OK    0
425 #define CMD_ERR   1
426 #define CMD_AGAIN 2
427 
428 struct ata_xfer *ata_get_xfer(int);
429 void	ata_free_xfer(struct ata_channel *, struct ata_xfer *);
430 #define	ATAXF_CANSLEEP	0x00
431 #define	ATAXF_NOSLEEP	0x01
432 
433 void	ata_exec_xfer(struct ata_channel *, struct ata_xfer *);
434 void	ata_kill_pending(struct ata_drive_datas *);
435 void	ata_reset_channel(struct ata_channel *, int);
436 
437 int	ata_addref(struct ata_channel *);
438 void	ata_delref(struct ata_channel *);
439 void	atastart(struct ata_channel *);
440 void	ata_print_modes(struct ata_channel *);
441 int	ata_downgrade_mode(struct ata_drive_datas *, int);
442 void	ata_probe_caps(struct ata_drive_datas *);
443 
444 void	ata_dmaerr(struct ata_drive_datas *, int);
445 void	ata_queue_idle(struct ata_queue *);
446 #endif /* _KERNEL */
447 
448 #endif /* _DEV_ATA_ATAVAR_H_ */
449