1 /*
2 * linux/drivers/block/ide.h
3 *
4 * Copyright (C) 1994, 1995 Linus Torvalds & authors
5 */
6
7 /*
8 * This is the multiple IDE interface driver, as evolved from hd.c.
9 * It supports up to four IDE interfaces, on one or more IRQs (usually 14 & 15).
10 * There can be up to two drives per interface, as per the ATA-2 spec.
11 *
12 * Primary i/f: ide0: major=3; (hda) minor=0; (hdb) minor=64
13 * Secondary i/f: ide1: major=22; (hdc or hd1a) minor=0; (hdd or hd1b) minor=64
14 * Tertiary i/f: ide2: major=33; (hde) minor=0; (hdf) minor=64
15 * Quaternary i/f: ide3: major=34; (hdg) minor=0; (hdh) minor=64
16 */
17
18 /******************************************************************************
19 * IDE driver configuration options (play with these as desired):
20 */
21 #undef REALLY_SLOW_IO /* most systems can safely undef this */
22 #include <asm/io.h>
23
24 #undef REALLY_FAST_IO /* define if ide ports are perfect */
25 #define INITIAL_MULT_COUNT 0 /* off=0; on=2,4,8,16,32, etc.. */
26
27 #ifndef DISK_RECOVERY_TIME /* off=0; on=access_delay_time */
28 #define DISK_RECOVERY_TIME 0 /* for hardware that needs it */
29 #endif
30 #ifndef OK_TO_RESET_CONTROLLER /* 1 needed for good error recovery */
31 #define OK_TO_RESET_CONTROLLER 1 /* 0 for use with AH2372A/B interface */
32 #endif
33 #ifndef SUPPORT_RZ1000 /* 1 to support RZ1000 chipset */
34 #define SUPPORT_RZ1000 1 /* 0 to reduce kernel size */
35 #endif
36 #ifndef SUPPORT_CMD640 /* 1 to support CMD640 chipset */
37 #define SUPPORT_CMD640 1 /* 0 to reduce kernel size */
38 #endif
39 #ifndef SUPPORT_HT6560B /* 1 to support HT6560B chipset */
40 #define SUPPORT_HT6560B 1 /* 0 to reduce kernel size */
41 #endif
42 #ifndef SUPPORT_DTC2278 /* 1 to support DTC2278 chipset */
43 #define SUPPORT_DTC2278 1 /* 0 to reduce kernel size */
44 #ifndef SET_DTC2278_MODE4
45 #define SET_DTC2278_MODE4 0 /* 1 to init primary i/f for PIO mode4 */
46 #endif
47 #endif
48 #ifndef FANCY_STATUS_DUMPS /* 1 for human-readable drive errors */
49 #define FANCY_STATUS_DUMPS 1 /* 0 to reduce kernel size */
50 #endif
51
52 /*
53 * IDE_DRIVE_CMD is used to implement many features of the hdparm utility
54 */
55 #define IDE_DRIVE_CMD 99 /* (magic) undef to reduce kernel size*/
56
57 /*
58 * "No user-serviceable parts" beyond this point :)
59 *****************************************************************************/
60
61 typedef unsigned char byte; /* used everywhere */
62
63 /*
64 * Probably not wise to fiddle with these
65 */
66 #define ERROR_MAX 8 /* Max read/write errors per sector */
67 #define ERROR_RESET 3 /* Reset controller every 4th retry */
68 #define ERROR_RECAL 1 /* Recalibrate every 2nd retry */
69
70 /*
71 * Ensure that various configuration flags have compatible settings
72 */
73 #ifdef REALLY_SLOW_IO
74 #undef REALLY_FAST_IO
75 #endif
76
77 /*
78 * Definitions for accessing IDE controller registers
79 */
80
81 #define HWIF(drive) ((ide_hwif_t *)drive->hwif)
82 #define HWGROUP(drive) ((ide_hwgroup_t *)(HWIF(drive)->hwgroup))
83
84 #define IDE_DATA_REG (HWIF(drive)->io_base)
85 #define IDE_ERROR_REG (HWIF(drive)->io_base+1)
86 #define IDE_NSECTOR_REG (HWIF(drive)->io_base+2)
87 #define IDE_SECTOR_REG (HWIF(drive)->io_base+3)
88 #define IDE_LCYL_REG (HWIF(drive)->io_base+4)
89 #define IDE_HCYL_REG (HWIF(drive)->io_base+5)
90 #define IDE_SELECT_REG (HWIF(drive)->io_base+6)
91 #define IDE_STATUS_REG (HWIF(drive)->io_base+7)
92 #define IDE_CONTROL_REG (HWIF(drive)->ctl_port)
93 #define IDE_FEATURE_REG IDE_ERROR_REG
94 #define IDE_COMMAND_REG IDE_STATUS_REG
95 #define IDE_ALTSTATUS_REG IDE_CONTROL_REG
96
97 #ifdef REALLY_FAST_IO
98 #define OUT_BYTE(b,p) outb((b),p)
99 #define IN_BYTE(p) (byte)inb(p)
100 #else
101 #define OUT_BYTE(b,p) outb_p((b),p)
102 #define IN_BYTE(p) (byte)inb_p(p)
103 #endif /* REALLY_FAST_IO */
104
105 #define GET_ERR() IN_BYTE(IDE_ERROR_REG)
106 #define GET_STAT() IN_BYTE(IDE_STATUS_REG)
107 #define OK_STAT(stat,good,bad) (((stat)&((good)|(bad)))==(good))
108 #define BAD_R_STAT (BUSY_STAT | ERR_STAT)
109 #define BAD_W_STAT (BAD_R_STAT | WRERR_STAT)
110 #define BAD_STAT (BAD_R_STAT | DRQ_STAT)
111 #define DRIVE_READY (READY_STAT | SEEK_STAT)
112 #define DATA_READY (DRIVE_READY | DRQ_STAT)
113
114 /*
115 * Some more useful definitions
116 */
117 #define IDE_MAJOR_NAME "ide" /* the same for all i/f; see also genhd.c */
118 #define MAJOR_NAME IDE_MAJOR_NAME
119 #define PARTN_BITS 6 /* number of minor dev bits for partitions */
120 #define PARTN_MASK ((1<<PARTN_BITS)-1) /* a useful bit mask */
121 #define MAX_DRIVES 2 /* per interface; 2 assumed by lots of code */
122 #define MAX_HWIFS 4 /* an arbitrary, but realistic limit */
123 #define SECTOR_WORDS (512 / 4) /* number of 32bit words per sector */
124
125 /*
126 * Timeouts for various operations:
127 */
128 #define WAIT_DRQ (5*HZ/100) /* 50msec - spec allows up to 20ms */
129 #define WAIT_READY (3*HZ/100) /* 30msec - should be instantaneous */
130 #define WAIT_PIDENTIFY (1*HZ) /* 1sec - should be less than 3ms (?) */
131 #define WAIT_WORSTCASE (30*HZ) /* 30sec - worst case when spinning up */
132 #define WAIT_CMD (10*HZ) /* 10sec - maximum wait for an IRQ to happen */
133
134 #ifdef CONFIG_BLK_DEV_IDECD
135
136 struct atapi_request_sense {
137 unsigned char error_code : 7;
138 unsigned char valid : 1;
139 byte reserved1;
140 unsigned char sense_key : 4;
141 unsigned char reserved2 : 1;
142 unsigned char ili : 1;
143 unsigned char reserved3 : 2;
144 byte info[4];
145 byte sense_len;
146 byte command_info[4];
147 byte asc;
148 byte ascq;
149 byte fru;
150 byte sense_key_specific[3];
151 };
152
153 struct packet_command {
154 char *buffer;
155 int buflen;
156 int stat;
157 struct atapi_request_sense *sense_data;
158 unsigned char c[12];
159 };
160
161 /* Space to hold the disk TOC. */
162
163 #define MAX_TRACKS 99
164 struct atapi_toc_header {
165 unsigned short toc_length;
166 byte first_track;
167 byte last_track;
168 };
169
170 struct atapi_toc_entry {
171 byte reserved1;
172 unsigned control : 4;
173 unsigned adr : 4;
174 byte track;
175 byte reserved2;
176 unsigned lba;
177 };
178
179 struct atapi_toc {
180 int last_session_lba;
181 int xa_flag;
182 unsigned capacity;
183 struct atapi_toc_header hdr;
184 struct atapi_toc_entry ent[MAX_TRACKS+1]; /* One extra for the leadout. */
185 };
186
187 /* Extra per-device info for cdrom drives. */
188 struct cdrom_info {
189
190 /* Buffer for table of contents. NULL if we haven't allocated
191 a TOC buffer for this device yet. */
192
193 struct atapi_toc *toc;
194
195 /* Sector buffer. If a read request wants only the first part of a cdrom
196 block, we cache the rest of the block here, in the expectation that that
197 data is going to be wanted soon. SECTOR_BUFFERED is the number of the
198 first buffered sector, and NSECTORS_BUFFERED is the number of sectors
199 in the buffer. Before the buffer is allocated, we should have
200 SECTOR_BUFFER == NULL and NSECTORS_BUFFERED == 0. */
201
202 unsigned long sector_buffered;
203 unsigned long nsectors_buffered;
204 char *sector_buffer;
205
206 /* The result of the last successful request sense command
207 on this device. */
208 struct atapi_request_sense sense_data;
209 };
210
211 #endif /* CONFIG_BLK_DEV_IDECD */
212
213 /*
214 * Now for the data we need to maintain per-drive: ide_drive_t
215 */
216 typedef enum {disk, cdrom} media_t;
217
218 typedef union {
219 unsigned all : 8; /* all of the bits together */
220 struct {
221 unsigned set_geometry : 1; /* respecify drive geometry */
222 unsigned recalibrate : 1; /* seek to cyl 0 */
223 unsigned set_multmode : 1; /* set multmode count */
224 unsigned reserved : 5; /* unused */
225 } b;
226 } special_t;
227
228 typedef union {
229 unsigned all : 8; /* all of the bits together */
230 struct {
231 unsigned head : 4; /* always zeros here */
232 unsigned unit : 1; /* drive select number, 0 or 1 */
233 unsigned bit5 : 1; /* always 1 */
234 unsigned lba : 1; /* using LBA instead of CHS */
235 unsigned bit7 : 1; /* always 1 */
236 } b;
237 } select_t;
238
239 typedef struct ide_drive_s {
240 special_t special; /* special action flags */
241 unsigned present : 1; /* drive is physically present */
242 unsigned noprobe : 1; /* from: hdx=noprobe */
243 unsigned keep_settings : 1; /* restore settings after drive reset */
244 unsigned busy : 1; /* currently doing revalidate_disk() */
245 unsigned vlb_32bit : 1; /* use 32bit in/out for data */
246 unsigned vlb_sync : 1; /* needed for some 32bit chip sets */
247 unsigned removeable : 1; /* 1 if need to do check_media_change */
248 unsigned using_dma : 1; /* disk is using dma for read/write */
249 unsigned unmask : 1; /* flag: okay to unmask other irqs */
250 media_t media; /* disk, cdrom, tape */
251 select_t select; /* basic drive/head select reg value */
252 void *hwif; /* actually (ide_hwif_t *) */
253 byte ctl; /* "normal" value for IDE_CONTROL_REG */
254 byte ready_stat; /* min status value for drive ready */
255 byte mult_count; /* current multiple sector setting */
256 byte mult_req; /* requested multiple sector setting */
257 byte chipset; /* interface chipset access method */
258 byte bad_wstat; /* used for ignoring WRERR_STAT */
259 byte sect0; /* offset of first sector for DM6:DDO */
260 byte usage; /* current "open()" count for drive */
261 byte head; /* "real" number of heads */
262 byte sect; /* "real" sectors per track */
263 byte bios_head; /* BIOS/fdisk/LILO number of heads */
264 byte bios_sect; /* BIOS/fdisk/LILO sectors per track */
265 unsigned short bios_cyl; /* BIOS/fdisk/LILO number of cyls */
266 unsigned short cyl; /* "real" number of cyls */
267 struct wait_queue *wqueue; /* used to wait for drive in open() */
268 struct hd_driveid *id; /* drive model identification info */
269 struct hd_struct *part; /* drive partition table */
270 char name[4]; /* drive name, such as "hda" */
271 #ifdef CONFIG_BLK_DEV_IDECD
272 struct cdrom_info cdrom_info; /* from ide-cd.c */
273 #endif /* CONFIG_BLK_DEV_IDECD */
274 } ide_drive_t;
275
276 /*
277 * An ide_dmaproc_t() initiates/aborts DMA read/write operations on a drive.
278 *
279 * The caller is assumed to have selected the drive and programmed the drive's
280 * sector address using CHS or LBA. All that remains is to prepare for DMA
281 * and then issue the actual read/write DMA/PIO command to the drive.
282 *
283 * Returns 0 if all went well.
284 * Returns 1 if DMA read/write could not be started, in which case the caller
285 * should either try again later, or revert to PIO for the current request.
286 */
287 typedef enum {ide_dma_read = 0, ide_dma_write = 1, ide_dma_abort = 2, ide_dma_check = 3} ide_dma_action_t;
288 typedef int (ide_dmaproc_t)(ide_dma_action_t, ide_drive_t *);
289
290 typedef struct hwif_s {
291 struct hwif_s *next; /* for linked-list in ide_hwgroup_t */
292 void *hwgroup; /* actually (ide_hwgroup_t *) */
293 unsigned short io_base; /* base io port addr */
294 unsigned short ctl_port; /* usually io_base+0x206 */
295 ide_drive_t drives[MAX_DRIVES]; /* drive info */
296 struct gendisk *gd; /* gendisk structure */
297 ide_dmaproc_t *dmaproc; /* dma read/write/abort routine */
298 unsigned long *dmatable; /* dma physical region descriptor table */
299 unsigned short dma_base; /* base addr for dma ports (triton) */
300 byte irq; /* our irq number */
301 byte major; /* our major number */
302 byte select; /* pri/sec hwif select for ht6560b */
303 char name[5]; /* name of interface, eg. "ide0" */
304 unsigned noprobe : 1; /* don't probe for this interface */
305 unsigned present : 1; /* this interface exists */
306 #if (DISK_RECOVERY_TIME > 0)
307 unsigned long last_time; /* time when previous rq was done */
308 #endif
309 #ifdef CONFIG_BLK_DEV_IDECD
310 struct request request_sense_request; /* from ide-cd.c */
311 struct packet_command request_sense_pc; /* from ide-cd.c */
312 #endif /* CONFIG_BLK_DEV_IDECD */
313 } ide_hwif_t;
314
315 /*
316 * internal ide interrupt handler type
317 */
318 typedef void (ide_handler_t)(ide_drive_t *);
319
320 typedef struct hwgroup_s {
321 ide_handler_t *handler;/* irq handler, if active */
322 ide_drive_t *drive; /* current drive */
323 ide_hwif_t *hwif; /* ptr to current hwif in linked-list */
324 struct request *rq; /* current request */
325 struct timer_list timer; /* failsafe timer */
326 struct request wrq; /* local copy of current write rq */
327 unsigned long reset_timeout; /* timeout value during ide resets */
328 #ifdef CONFIG_BLK_DEV_IDECD
329 int doing_atapi_reset;
330 #endif /* CONFIG_BLK_DEV_IDECD */
331 } ide_hwgroup_t;
332
333 /*
334 * One final include file, which references some of the data/defns from above
335 */
336 #define IDE_DRIVER /* "parameter" for blk.h */
337 #include "blk.h"
338
339 #if (DISK_RECOVERY_TIME > 0)
340 void ide_set_recovery_timer (ide_hwif_t *);
341 #define SET_RECOVERY_TIMER(drive) ide_set_recovery_timer (drive)
342 #else
343 #define SET_RECOVERY_TIMER(drive)
344 #endif
345
346 /*
347 * The main (re-)entry point for handling a new request is IDE_DO_REQUEST.
348 * Note that IDE_DO_REQUEST should *only* ever be invoked from an interrupt
349 * handler. All others, such as a timer expiry handler, should call
350 * do_hwgroup_request() instead (currently local to ide.c).
351 */
352 void ide_do_request (ide_hwgroup_t *);
353 #define IDE_DO_REQUEST { SET_RECOVERY_TIMER(HWIF(drive)); ide_do_request(HWGROUP(drive)); }
354
355
356 /*
357 * This is used for (nearly) all data transfers from the IDE interface
358 */
359 void ide_input_data (ide_drive_t *drive, void *buffer, unsigned int wcount);
360
361 /*
362 * This is used for (nearly) all data transfers to the IDE interface
363 */
364 void ide_output_data (ide_drive_t *drive, void *buffer, unsigned int wcount);
365
366 /*
367 * This is used on exit from the driver, to designate the next irq handler
368 * and also to start the safety timer.
369 */
370 void ide_set_handler (ide_drive_t *drive, ide_handler_t *handler);
371
372 /*
373 * Error reporting, in human readable form (luxurious, but a memory hog).
374 */
375 byte ide_dump_status (ide_drive_t *drive, const char *msg, byte stat);
376
377 /*
378 * ide_error() takes action based on the error returned by the controller.
379 *
380 * Returns 1 if an ide reset operation has been initiated, in which case
381 * the caller MUST simply return from the driver (through however many levels).
382 * Returns 0 otherwise.
383 */
384 int ide_error (ide_drive_t *drive, const char *msg, byte stat);
385
386 /*
387 * This routine busy-waits for the drive status to be not "busy".
388 * It then checks the status for all of the "good" bits and none
389 * of the "bad" bits, and if all is okay it returns 0. All other
390 * cases return 1 after invoking ide_error()
391 *
392 */
393 int ide_wait_stat (ide_drive_t *drive, byte good, byte bad, unsigned long timeout);
394
395 /*
396 * This is called from genhd.c to correct DiskManager/EZ-Drive geometries
397 */
398 int ide_xlate_1024(dev_t, int, const char *);
399
400 /*
401 * Start a reset operation for an IDE interface.
402 * Returns 0 if the reset operation is still in progress,
403 * in which case the drive MUST return, to await completion.
404 * Returns 1 if the reset is complete (success or failure).
405 */
406 int ide_do_reset (ide_drive_t *);
407
408 /*
409 * ide_alloc(): memory allocation for use *only* during driver initialization.
410 * If "within_area" is non-zero, the memory will be allocated such that
411 * it lies entirely within a "within_area" sized area (eg. 4096). This is
412 * needed for DMA stuff. "within_area" must be a power of two (not validated).
413 * All allocations are longword aligned.
414 */
415 void *ide_alloc (unsigned long bytecount, unsigned long within_area);
416
417 #ifdef CONFIG_BLK_DEV_IDECD
418 /*
419 * These are routines in ide-cd.c invoked from ide.c
420 */
421 void ide_do_rw_cdrom (ide_drive_t *, unsigned long);
422 int ide_cdrom_ioctl (ide_drive_t *, struct inode *, struct file *, unsigned int, unsigned long);
423 int ide_cdrom_check_media_change (ide_drive_t *);
424 int ide_cdrom_open (struct inode *, struct file *, ide_drive_t *);
425 void ide_cdrom_release (struct inode *, struct file *, ide_drive_t *);
426 void ide_cdrom_setup (ide_drive_t *);
427 #endif /* CONFIG_BLK_DEV_IDECD */
428
429 #ifdef CONFIG_BLK_DEV_TRITON
430 void ide_init_triton (byte, byte);
431 #endif /* CONFIG_BLK_DEV_TRITON */
432