vserver 2.0-rc4
[linux-2.6.git] / drivers / ide / ide-disk.c
1 /*
2  *  linux/drivers/ide/ide-disk.c        Version 1.18    Mar 05, 2003
3  *
4  *  Copyright (C) 1994-1998  Linus Torvalds & authors (see below)
5  *  Copyright (C) 1998-2002  Linux ATA Development
6  *                              Andre Hedrick <andre@linux-ide.org>
7  *  Copyright (C) 2003       Red Hat <alan@redhat.com>
8  */
9
10 /*
11  *  Mostly written by Mark Lord <mlord@pobox.com>
12  *                and Gadi Oxman <gadio@netvision.net.il>
13  *                and Andre Hedrick <andre@linux-ide.org>
14  *
15  * This is the IDE/ATA disk driver, as evolved from hd.c and ide.c.
16  *
17  * Version 1.00         move disk only code from ide.c to ide-disk.c
18  *                      support optional byte-swapping of all data
19  * Version 1.01         fix previous byte-swapping code
20  * Version 1.02         remove ", LBA" from drive identification msgs
21  * Version 1.03         fix display of id->buf_size for big-endian
22  * Version 1.04         add /proc configurable settings and S.M.A.R.T support
23  * Version 1.05         add capacity support for ATA3 >= 8GB
24  * Version 1.06         get boot-up messages to show full cyl count
25  * Version 1.07         disable door-locking if it fails
26  * Version 1.08         fixed CHS/LBA translations for ATA4 > 8GB,
27  *                      process of adding new ATA4 compliance.
28  *                      fixed problems in allowing fdisk to see
29  *                      the entire disk.
30  * Version 1.09         added increment of rq->sector in ide_multwrite
31  *                      added UDMA 3/4 reporting
32  * Version 1.10         request queue changes, Ultra DMA 100
33  * Version 1.11         added 48-bit lba
34  * Version 1.12         adding taskfile io access method
35  * Version 1.13         added standby and flush-cache for notifier
36  * Version 1.14         added acoustic-wcache
37  * Version 1.15         convert all calls to ide_raw_taskfile
38  *                              since args will return register content.
39  * Version 1.16         added suspend-resume-checkpower
40  * Version 1.17         do flush on standy, do flush on ATA < ATA6
41  *                      fix wcache setup.
42  */
43
44 #define IDEDISK_VERSION "1.18"
45
46 #undef REALLY_SLOW_IO           /* most systems can safely undef this */
47
48 //#define DEBUG
49
50 #include <linux/config.h>
51 #include <linux/module.h>
52 #include <linux/types.h>
53 #include <linux/string.h>
54 #include <linux/kernel.h>
55 #include <linux/timer.h>
56 #include <linux/mm.h>
57 #include <linux/interrupt.h>
58 #include <linux/major.h>
59 #include <linux/errno.h>
60 #include <linux/genhd.h>
61 #include <linux/slab.h>
62 #include <linux/delay.h>
63
64 #define _IDE_DISK
65
66 #include <linux/ide.h>
67
68 #include <asm/byteorder.h>
69 #include <asm/irq.h>
70 #include <asm/uaccess.h>
71 #include <asm/io.h>
72 #include <asm/div64.h>
73
74 /*
75  * lba_capacity_is_ok() performs a sanity check on the claimed "lba_capacity"
76  * value for this drive (from its reported identification information).
77  *
78  * Returns:     1 if lba_capacity looks sensible
79  *              0 otherwise
80  *
81  * It is called only once for each drive.
82  */
83 static int lba_capacity_is_ok (struct hd_driveid *id)
84 {
85         unsigned long lba_sects, chs_sects, head, tail;
86
87         /*
88          * The ATA spec tells large drives to return
89          * C/H/S = 16383/16/63 independent of their size.
90          * Some drives can be jumpered to use 15 heads instead of 16.
91          * Some drives can be jumpered to use 4092 cyls instead of 16383.
92          */
93         if ((id->cyls == 16383
94              || (id->cyls == 4092 && id->cur_cyls == 16383)) &&
95             id->sectors == 63 &&
96             (id->heads == 15 || id->heads == 16) &&
97             (id->lba_capacity >= 16383*63*id->heads))
98                 return 1;
99
100         lba_sects   = id->lba_capacity;
101         chs_sects   = id->cyls * id->heads * id->sectors;
102
103         /* perform a rough sanity check on lba_sects:  within 10% is OK */
104         if ((lba_sects - chs_sects) < chs_sects/10)
105                 return 1;
106
107         /* some drives have the word order reversed */
108         head = ((lba_sects >> 16) & 0xffff);
109         tail = (lba_sects & 0xffff);
110         lba_sects = (head | (tail << 16));
111         if ((lba_sects - chs_sects) < chs_sects/10) {
112                 id->lba_capacity = lba_sects;
113                 return 1;       /* lba_capacity is (now) good */
114         }
115
116         return 0;       /* lba_capacity value may be bad */
117 }
118
119 /*
120  * __ide_do_rw_disk() issues READ and WRITE commands to a disk,
121  * using LBA if supported, or CHS otherwise, to address sectors.
122  */
123 static ide_startstop_t __ide_do_rw_disk(ide_drive_t *drive, struct request *rq, sector_t block)
124 {
125         ide_hwif_t *hwif        = HWIF(drive);
126         unsigned int dma        = drive->using_dma;
127         u8 lba48                = (drive->addressing == 1) ? 1 : 0;
128         task_ioreg_t command    = WIN_NOP;
129         ata_nsector_t           nsectors;
130
131         nsectors.all            = (u16) rq->nr_sectors;
132
133         if (hwif->no_lba48_dma && lba48 && dma) {
134                 if (block + rq->nr_sectors > 1ULL << 28)
135                         dma = 0;
136                 else
137                         lba48 = 0;
138         }
139
140         if (!dma) {
141                 ide_init_sg_cmd(drive, rq);
142                 ide_map_sg(drive, rq);
143         }
144
145         if (IDE_CONTROL_REG)
146                 hwif->OUTB(drive->ctl, IDE_CONTROL_REG);
147
148         /* FIXME: SELECT_MASK(drive, 0) ? */
149
150         if (drive->select.b.lba) {
151                 if (lba48) {
152                         task_ioreg_t tasklets[10];
153
154                         pr_debug("%s: LBA=0x%012llx\n", drive->name, block);
155
156                         tasklets[0] = 0;
157                         tasklets[1] = 0;
158                         tasklets[2] = nsectors.b.low;
159                         tasklets[3] = nsectors.b.high;
160                         tasklets[4] = (task_ioreg_t) block;
161                         tasklets[5] = (task_ioreg_t) (block>>8);
162                         tasklets[6] = (task_ioreg_t) (block>>16);
163                         tasklets[7] = (task_ioreg_t) (block>>24);
164                         if (sizeof(block) == 4) {
165                                 tasklets[8] = (task_ioreg_t) 0;
166                                 tasklets[9] = (task_ioreg_t) 0;
167                         } else {
168                                 tasklets[8] = (task_ioreg_t)((u64)block >> 32);
169                                 tasklets[9] = (task_ioreg_t)((u64)block >> 40);
170                         }
171 #ifdef DEBUG
172                         printk("%s: 0x%02x%02x 0x%02x%02x%02x%02x%02x%02x\n",
173                                 drive->name, tasklets[3], tasklets[2],
174                                 tasklets[9], tasklets[8], tasklets[7],
175                                 tasklets[6], tasklets[5], tasklets[4]);
176 #endif
177                         hwif->OUTB(tasklets[1], IDE_FEATURE_REG);
178                         hwif->OUTB(tasklets[3], IDE_NSECTOR_REG);
179                         hwif->OUTB(tasklets[7], IDE_SECTOR_REG);
180                         hwif->OUTB(tasklets[8], IDE_LCYL_REG);
181                         hwif->OUTB(tasklets[9], IDE_HCYL_REG);
182
183                         hwif->OUTB(tasklets[0], IDE_FEATURE_REG);
184                         hwif->OUTB(tasklets[2], IDE_NSECTOR_REG);
185                         hwif->OUTB(tasklets[4], IDE_SECTOR_REG);
186                         hwif->OUTB(tasklets[5], IDE_LCYL_REG);
187                         hwif->OUTB(tasklets[6], IDE_HCYL_REG);
188                         hwif->OUTB(0x00|drive->select.all,IDE_SELECT_REG);
189                 } else {
190                         hwif->OUTB(0x00, IDE_FEATURE_REG);
191                         hwif->OUTB(nsectors.b.low, IDE_NSECTOR_REG);
192                         hwif->OUTB(block, IDE_SECTOR_REG);
193                         hwif->OUTB(block>>=8, IDE_LCYL_REG);
194                         hwif->OUTB(block>>=8, IDE_HCYL_REG);
195                         hwif->OUTB(((block>>8)&0x0f)|drive->select.all,IDE_SELECT_REG);
196                 }
197         } else {
198                 unsigned int sect,head,cyl,track;
199                 track = (int)block / drive->sect;
200                 sect  = (int)block % drive->sect + 1;
201                 hwif->OUTB(sect, IDE_SECTOR_REG);
202                 head  = track % drive->head;
203                 cyl   = track / drive->head;
204
205                 pr_debug("%s: CHS=%u/%u/%u\n", drive->name, cyl, head, sect);
206
207                 hwif->OUTB(0x00, IDE_FEATURE_REG);
208                 hwif->OUTB(nsectors.b.low, IDE_NSECTOR_REG);
209                 hwif->OUTB(cyl, IDE_LCYL_REG);
210                 hwif->OUTB(cyl>>8, IDE_HCYL_REG);
211                 hwif->OUTB(head|drive->select.all,IDE_SELECT_REG);
212         }
213
214         if (dma) {
215                 if (!hwif->dma_setup(drive)) {
216                         if (rq_data_dir(rq)) {
217                                 command = lba48 ? WIN_WRITEDMA_EXT : WIN_WRITEDMA;
218                                 if (drive->vdma)
219                                         command = lba48 ? WIN_WRITE_EXT: WIN_WRITE;
220                         } else {
221                                 command = lba48 ? WIN_READDMA_EXT : WIN_READDMA;
222                                 if (drive->vdma)
223                                         command = lba48 ? WIN_READ_EXT: WIN_READ;
224                         }
225                         hwif->dma_exec_cmd(drive, command);
226                         hwif->dma_start(drive);
227                         return ide_started;
228                 }
229                 /* fallback to PIO */
230                 ide_init_sg_cmd(drive, rq);
231         }
232
233         if (rq_data_dir(rq) == READ) {
234
235                 if (drive->mult_count) {
236                         hwif->data_phase = TASKFILE_MULTI_IN;
237                         command = lba48 ? WIN_MULTREAD_EXT : WIN_MULTREAD;
238                 } else {
239                         hwif->data_phase = TASKFILE_IN;
240                         command = lba48 ? WIN_READ_EXT : WIN_READ;
241                 }
242
243                 ide_execute_command(drive, command, &task_in_intr, WAIT_CMD, NULL);
244                 return ide_started;
245         } else {
246                 if (drive->mult_count) {
247                         hwif->data_phase = TASKFILE_MULTI_OUT;
248                         command = lba48 ? WIN_MULTWRITE_EXT : WIN_MULTWRITE;
249                 } else {
250                         hwif->data_phase = TASKFILE_OUT;
251                         command = lba48 ? WIN_WRITE_EXT : WIN_WRITE;
252                 }
253
254                 /* FIXME: ->OUTBSYNC ? */
255                 hwif->OUTB(command, IDE_COMMAND_REG);
256
257                 return pre_task_out_intr(drive, rq);
258         }
259 }
260
261 /*
262  * 268435455  == 137439 MB or 28bit limit
263  * 320173056  == 163929 MB or 48bit addressing
264  * 1073741822 == 549756 MB or 48bit addressing fake drive
265  */
266
267 static ide_startstop_t ide_do_rw_disk (ide_drive_t *drive, struct request *rq, sector_t block)
268 {
269         ide_hwif_t *hwif = HWIF(drive);
270
271         BUG_ON(drive->blocked);
272
273         if (!blk_fs_request(rq)) {
274                 blk_dump_rq_flags(rq, "ide_do_rw_disk - bad command");
275                 ide_end_request(drive, 0, 0);
276                 return ide_stopped;
277         }
278
279         pr_debug("%s: %sing: block=%llu, sectors=%lu, buffer=0x%08lx\n",
280                  drive->name, rq_data_dir(rq) == READ ? "read" : "writ",
281                  block, rq->nr_sectors, (unsigned long)rq->buffer);
282
283         if (hwif->rw_disk)
284                 hwif->rw_disk(drive, rq);
285
286         return __ide_do_rw_disk(drive, rq, block);
287 }
288
289 /*
290  * Queries for true maximum capacity of the drive.
291  * Returns maximum LBA address (> 0) of the drive, 0 if failed.
292  */
293 static unsigned long idedisk_read_native_max_address(ide_drive_t *drive)
294 {
295         ide_task_t args;
296         unsigned long addr = 0;
297
298         /* Create IDE/ATA command request structure */
299         memset(&args, 0, sizeof(ide_task_t));
300         args.tfRegister[IDE_SELECT_OFFSET]      = 0x40;
301         args.tfRegister[IDE_COMMAND_OFFSET]     = WIN_READ_NATIVE_MAX;
302         args.command_type                       = IDE_DRIVE_TASK_NO_DATA;
303         args.handler                            = &task_no_data_intr;
304         /* submit command request */
305         ide_raw_taskfile(drive, &args, NULL);
306
307         /* if OK, compute maximum address value */
308         if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
309                 addr = ((args.tfRegister[IDE_SELECT_OFFSET] & 0x0f) << 24)
310                      | ((args.tfRegister[  IDE_HCYL_OFFSET]       ) << 16)
311                      | ((args.tfRegister[  IDE_LCYL_OFFSET]       ) <<  8)
312                      | ((args.tfRegister[IDE_SECTOR_OFFSET]       ));
313                 addr++; /* since the return value is (maxlba - 1), we add 1 */
314         }
315         return addr;
316 }
317
318 static unsigned long long idedisk_read_native_max_address_ext(ide_drive_t *drive)
319 {
320         ide_task_t args;
321         unsigned long long addr = 0;
322
323         /* Create IDE/ATA command request structure */
324         memset(&args, 0, sizeof(ide_task_t));
325
326         args.tfRegister[IDE_SELECT_OFFSET]      = 0x40;
327         args.tfRegister[IDE_COMMAND_OFFSET]     = WIN_READ_NATIVE_MAX_EXT;
328         args.command_type                       = IDE_DRIVE_TASK_NO_DATA;
329         args.handler                            = &task_no_data_intr;
330         /* submit command request */
331         ide_raw_taskfile(drive, &args, NULL);
332
333         /* if OK, compute maximum address value */
334         if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
335                 u32 high = (args.hobRegister[IDE_HCYL_OFFSET] << 16) |
336                            (args.hobRegister[IDE_LCYL_OFFSET] <<  8) |
337                             args.hobRegister[IDE_SECTOR_OFFSET];
338                 u32 low  = ((args.tfRegister[IDE_HCYL_OFFSET])<<16) |
339                            ((args.tfRegister[IDE_LCYL_OFFSET])<<8) |
340                             (args.tfRegister[IDE_SECTOR_OFFSET]);
341                 addr = ((__u64)high << 24) | low;
342                 addr++; /* since the return value is (maxlba - 1), we add 1 */
343         }
344         return addr;
345 }
346
347 /*
348  * Sets maximum virtual LBA address of the drive.
349  * Returns new maximum virtual LBA address (> 0) or 0 on failure.
350  */
351 static unsigned long idedisk_set_max_address(ide_drive_t *drive, unsigned long addr_req)
352 {
353         ide_task_t args;
354         unsigned long addr_set = 0;
355         
356         addr_req--;
357         /* Create IDE/ATA command request structure */
358         memset(&args, 0, sizeof(ide_task_t));
359         args.tfRegister[IDE_SECTOR_OFFSET]      = ((addr_req >>  0) & 0xff);
360         args.tfRegister[IDE_LCYL_OFFSET]        = ((addr_req >>  8) & 0xff);
361         args.tfRegister[IDE_HCYL_OFFSET]        = ((addr_req >> 16) & 0xff);
362         args.tfRegister[IDE_SELECT_OFFSET]      = ((addr_req >> 24) & 0x0f) | 0x40;
363         args.tfRegister[IDE_COMMAND_OFFSET]     = WIN_SET_MAX;
364         args.command_type                       = IDE_DRIVE_TASK_NO_DATA;
365         args.handler                            = &task_no_data_intr;
366         /* submit command request */
367         ide_raw_taskfile(drive, &args, NULL);
368         /* if OK, read new maximum address value */
369         if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
370                 addr_set = ((args.tfRegister[IDE_SELECT_OFFSET] & 0x0f) << 24)
371                          | ((args.tfRegister[  IDE_HCYL_OFFSET]       ) << 16)
372                          | ((args.tfRegister[  IDE_LCYL_OFFSET]       ) <<  8)
373                          | ((args.tfRegister[IDE_SECTOR_OFFSET]       ));
374                 addr_set++;
375         }
376         return addr_set;
377 }
378
379 static unsigned long long idedisk_set_max_address_ext(ide_drive_t *drive, unsigned long long addr_req)
380 {
381         ide_task_t args;
382         unsigned long long addr_set = 0;
383
384         addr_req--;
385         /* Create IDE/ATA command request structure */
386         memset(&args, 0, sizeof(ide_task_t));
387         args.tfRegister[IDE_SECTOR_OFFSET]      = ((addr_req >>  0) & 0xff);
388         args.tfRegister[IDE_LCYL_OFFSET]        = ((addr_req >>= 8) & 0xff);
389         args.tfRegister[IDE_HCYL_OFFSET]        = ((addr_req >>= 8) & 0xff);
390         args.tfRegister[IDE_SELECT_OFFSET]      = 0x40;
391         args.tfRegister[IDE_COMMAND_OFFSET]     = WIN_SET_MAX_EXT;
392         args.hobRegister[IDE_SECTOR_OFFSET]     = (addr_req >>= 8) & 0xff;
393         args.hobRegister[IDE_LCYL_OFFSET]       = (addr_req >>= 8) & 0xff;
394         args.hobRegister[IDE_HCYL_OFFSET]       = (addr_req >>= 8) & 0xff;
395         args.hobRegister[IDE_SELECT_OFFSET]     = 0x40;
396         args.hobRegister[IDE_CONTROL_OFFSET_HOB]= (drive->ctl|0x80);
397         args.command_type                       = IDE_DRIVE_TASK_NO_DATA;
398         args.handler                            = &task_no_data_intr;
399         /* submit command request */
400         ide_raw_taskfile(drive, &args, NULL);
401         /* if OK, compute maximum address value */
402         if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
403                 u32 high = (args.hobRegister[IDE_HCYL_OFFSET] << 16) |
404                            (args.hobRegister[IDE_LCYL_OFFSET] <<  8) |
405                             args.hobRegister[IDE_SECTOR_OFFSET];
406                 u32 low  = ((args.tfRegister[IDE_HCYL_OFFSET])<<16) |
407                            ((args.tfRegister[IDE_LCYL_OFFSET])<<8) |
408                             (args.tfRegister[IDE_SECTOR_OFFSET]);
409                 addr_set = ((__u64)high << 24) | low;
410                 addr_set++;
411         }
412         return addr_set;
413 }
414
415 static unsigned long long sectors_to_MB(unsigned long long n)
416 {
417         n <<= 9;                /* make it bytes */
418         do_div(n, 1000000);     /* make it MB */
419         return n;
420 }
421
422 /*
423  * Bits 10 of command_set_1 and cfs_enable_1 must be equal,
424  * so on non-buggy drives we need test only one.
425  * However, we should also check whether these fields are valid.
426  */
427 static inline int idedisk_supports_hpa(const struct hd_driveid *id)
428 {
429         return (id->command_set_1 & 0x0400) && (id->cfs_enable_1 & 0x0400);
430 }
431
432 /*
433  * The same here.
434  */
435 static inline int idedisk_supports_lba48(const struct hd_driveid *id)
436 {
437         return (id->command_set_2 & 0x0400) && (id->cfs_enable_2 & 0x0400)
438                && id->lba_capacity_2;
439 }
440
441 static inline void idedisk_check_hpa(ide_drive_t *drive)
442 {
443         unsigned long long capacity, set_max;
444         int lba48 = idedisk_supports_lba48(drive->id);
445
446         capacity = drive->capacity64;
447         if (lba48)
448                 set_max = idedisk_read_native_max_address_ext(drive);
449         else
450                 set_max = idedisk_read_native_max_address(drive);
451
452         if (set_max <= capacity)
453                 return;
454
455         printk(KERN_INFO "%s: Host Protected Area detected.\n"
456                          "\tcurrent capacity is %llu sectors (%llu MB)\n"
457                          "\tnative  capacity is %llu sectors (%llu MB)\n",
458                          drive->name,
459                          capacity, sectors_to_MB(capacity),
460                          set_max, sectors_to_MB(set_max));
461
462         if (lba48)
463                 set_max = idedisk_set_max_address_ext(drive, set_max);
464         else
465                 set_max = idedisk_set_max_address(drive, set_max);
466         if (set_max) {
467                 drive->capacity64 = set_max;
468                 printk(KERN_INFO "%s: Host Protected Area disabled.\n",
469                                  drive->name);
470         }
471 }
472
473 /*
474  * Compute drive->capacity, the full capacity of the drive
475  * Called with drive->id != NULL.
476  *
477  * To compute capacity, this uses either of
478  *
479  *    1. CHS value set by user       (whatever user sets will be trusted)
480  *    2. LBA value from target drive (require new ATA feature)
481  *    3. LBA value from system BIOS  (new one is OK, old one may break)
482  *    4. CHS value from system BIOS  (traditional style)
483  *
484  * in above order (i.e., if value of higher priority is available,
485  * reset will be ignored).
486  */
487 static void init_idedisk_capacity (ide_drive_t  *drive)
488 {
489         struct hd_driveid *id = drive->id;
490         /*
491          * If this drive supports the Host Protected Area feature set,
492          * then we may need to change our opinion about the drive's capacity.
493          */
494         int hpa = idedisk_supports_hpa(id);
495
496         if (idedisk_supports_lba48(id)) {
497                 /* drive speaks 48-bit LBA */
498                 drive->select.b.lba = 1;
499                 drive->capacity64 = id->lba_capacity_2;
500                 if (hpa)
501                         idedisk_check_hpa(drive);
502         } else if ((id->capability & 2) && lba_capacity_is_ok(id)) {
503                 /* drive speaks 28-bit LBA */
504                 drive->select.b.lba = 1;
505                 drive->capacity64 = id->lba_capacity;
506                 if (hpa)
507                         idedisk_check_hpa(drive);
508         } else {
509                 /* drive speaks boring old 28-bit CHS */
510                 drive->capacity64 = drive->cyl * drive->head * drive->sect;
511         }
512 }
513
514 static sector_t idedisk_capacity (ide_drive_t *drive)
515 {
516         return drive->capacity64 - drive->sect0;
517 }
518
519 #define IS_PDC4030_DRIVE        0
520
521 static ide_startstop_t idedisk_special (ide_drive_t *drive)
522 {
523         special_t *s = &drive->special;
524
525         if (s->b.set_geometry) {
526                 s->b.set_geometry       = 0;
527                 if (!IS_PDC4030_DRIVE) {
528                         ide_task_t args;
529                         memset(&args, 0, sizeof(ide_task_t));
530                         args.tfRegister[IDE_NSECTOR_OFFSET] = drive->sect;
531                         args.tfRegister[IDE_SECTOR_OFFSET]  = drive->sect;
532                         args.tfRegister[IDE_LCYL_OFFSET]    = drive->cyl;
533                         args.tfRegister[IDE_HCYL_OFFSET]    = drive->cyl>>8;
534                         args.tfRegister[IDE_SELECT_OFFSET]  = ((drive->head-1)|drive->select.all)&0xBF;
535                         args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SPECIFY;
536                         args.command_type = IDE_DRIVE_TASK_NO_DATA;
537                         args.handler      = &set_geometry_intr;
538                         do_rw_taskfile(drive, &args);
539                 }
540         } else if (s->b.recalibrate) {
541                 s->b.recalibrate = 0;
542                 if (!IS_PDC4030_DRIVE) {
543                         ide_task_t args;
544                         memset(&args, 0, sizeof(ide_task_t));
545                         args.tfRegister[IDE_NSECTOR_OFFSET] = drive->sect;
546                         args.tfRegister[IDE_COMMAND_OFFSET] = WIN_RESTORE;
547                         args.command_type = IDE_DRIVE_TASK_NO_DATA;
548                         args.handler      = &recal_intr;
549                         do_rw_taskfile(drive, &args);
550                 }
551         } else if (s->b.set_multmode) {
552                 s->b.set_multmode = 0;
553                 if (drive->mult_req > drive->id->max_multsect)
554                         drive->mult_req = drive->id->max_multsect;
555                 if (!IS_PDC4030_DRIVE) {
556                         ide_task_t args;
557                         memset(&args, 0, sizeof(ide_task_t));
558                         args.tfRegister[IDE_NSECTOR_OFFSET] = drive->mult_req;
559                         args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SETMULT;
560                         args.command_type = IDE_DRIVE_TASK_NO_DATA;
561                         args.handler      = &set_multmode_intr;
562                         do_rw_taskfile(drive, &args);
563                 }
564         } else if (s->all) {
565                 int special = s->all;
566                 s->all = 0;
567                 printk(KERN_ERR "%s: bad special flag: 0x%02x\n", drive->name, special);
568                 return ide_stopped;
569         }
570         return IS_PDC4030_DRIVE ? ide_stopped : ide_started;
571 }
572
573 static void idedisk_pre_reset (ide_drive_t *drive)
574 {
575         int legacy = (drive->id->cfs_enable_2 & 0x0400) ? 0 : 1;
576
577         drive->special.all = 0;
578         drive->special.b.set_geometry = legacy;
579         drive->special.b.recalibrate  = legacy;
580         if (OK_TO_RESET_CONTROLLER)
581                 drive->mult_count = 0;
582         if (!drive->keep_settings && !drive->using_dma)
583                 drive->mult_req = 0;
584         if (drive->mult_req != drive->mult_count)
585                 drive->special.b.set_multmode = 1;
586 }
587
588 #ifdef CONFIG_PROC_FS
589
590 static int smart_enable(ide_drive_t *drive)
591 {
592         ide_task_t args;
593
594         memset(&args, 0, sizeof(ide_task_t));
595         args.tfRegister[IDE_FEATURE_OFFSET]     = SMART_ENABLE;
596         args.tfRegister[IDE_LCYL_OFFSET]        = SMART_LCYL_PASS;
597         args.tfRegister[IDE_HCYL_OFFSET]        = SMART_HCYL_PASS;
598         args.tfRegister[IDE_COMMAND_OFFSET]     = WIN_SMART;
599         args.command_type                       = IDE_DRIVE_TASK_NO_DATA;
600         args.handler                            = &task_no_data_intr;
601         return ide_raw_taskfile(drive, &args, NULL);
602 }
603
604 static int get_smart_values(ide_drive_t *drive, u8 *buf)
605 {
606         ide_task_t args;
607
608         memset(&args, 0, sizeof(ide_task_t));
609         args.tfRegister[IDE_FEATURE_OFFSET]     = SMART_READ_VALUES;
610         args.tfRegister[IDE_NSECTOR_OFFSET]     = 0x01;
611         args.tfRegister[IDE_LCYL_OFFSET]        = SMART_LCYL_PASS;
612         args.tfRegister[IDE_HCYL_OFFSET]        = SMART_HCYL_PASS;
613         args.tfRegister[IDE_COMMAND_OFFSET]     = WIN_SMART;
614         args.command_type                       = IDE_DRIVE_TASK_IN;
615         args.data_phase                         = TASKFILE_IN;
616         args.handler                            = &task_in_intr;
617         (void) smart_enable(drive);
618         return ide_raw_taskfile(drive, &args, buf);
619 }
620
621 static int get_smart_thresholds(ide_drive_t *drive, u8 *buf)
622 {
623         ide_task_t args;
624         memset(&args, 0, sizeof(ide_task_t));
625         args.tfRegister[IDE_FEATURE_OFFSET]     = SMART_READ_THRESHOLDS;
626         args.tfRegister[IDE_NSECTOR_OFFSET]     = 0x01;
627         args.tfRegister[IDE_LCYL_OFFSET]        = SMART_LCYL_PASS;
628         args.tfRegister[IDE_HCYL_OFFSET]        = SMART_HCYL_PASS;
629         args.tfRegister[IDE_COMMAND_OFFSET]     = WIN_SMART;
630         args.command_type                       = IDE_DRIVE_TASK_IN;
631         args.data_phase                         = TASKFILE_IN;
632         args.handler                            = &task_in_intr;
633         (void) smart_enable(drive);
634         return ide_raw_taskfile(drive, &args, buf);
635 }
636
637 static int proc_idedisk_read_cache
638         (char *page, char **start, off_t off, int count, int *eof, void *data)
639 {
640         ide_drive_t     *drive = (ide_drive_t *) data;
641         char            *out = page;
642         int             len;
643
644         if (drive->id_read)
645                 len = sprintf(out,"%i\n", drive->id->buf_size / 2);
646         else
647                 len = sprintf(out,"(none)\n");
648         PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
649 }
650
651 static int proc_idedisk_read_smart_thresholds
652         (char *page, char **start, off_t off, int count, int *eof, void *data)
653 {
654         ide_drive_t     *drive = (ide_drive_t *)data;
655         int             len = 0, i = 0;
656
657         if (!get_smart_thresholds(drive, page)) {
658                 unsigned short *val = (unsigned short *) page;
659                 char *out = ((char *)val) + (SECTOR_WORDS * 4);
660                 page = out;
661                 do {
662                         out += sprintf(out, "%04x%c", le16_to_cpu(*val), (++i & 7) ? ' ' : '\n');
663                         val += 1;
664                 } while (i < (SECTOR_WORDS * 2));
665                 len = out - page;
666         }
667         PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
668 }
669
670 static int proc_idedisk_read_smart_values
671         (char *page, char **start, off_t off, int count, int *eof, void *data)
672 {
673         ide_drive_t     *drive = (ide_drive_t *)data;
674         int             len = 0, i = 0;
675
676         if (!get_smart_values(drive, page)) {
677                 unsigned short *val = (unsigned short *) page;
678                 char *out = ((char *)val) + (SECTOR_WORDS * 4);
679                 page = out;
680                 do {
681                         out += sprintf(out, "%04x%c", le16_to_cpu(*val), (++i & 7) ? ' ' : '\n');
682                         val += 1;
683                 } while (i < (SECTOR_WORDS * 2));
684                 len = out - page;
685         }
686         PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
687 }
688
689 static ide_proc_entry_t idedisk_proc[] = {
690         { "cache",              S_IFREG|S_IRUGO,        proc_idedisk_read_cache,                NULL },
691         { "geometry",           S_IFREG|S_IRUGO,        proc_ide_read_geometry,                 NULL },
692         { "smart_values",       S_IFREG|S_IRUSR,        proc_idedisk_read_smart_values,         NULL },
693         { "smart_thresholds",   S_IFREG|S_IRUSR,        proc_idedisk_read_smart_thresholds,     NULL },
694         { NULL, 0, NULL, NULL }
695 };
696
697 #else
698
699 #define idedisk_proc    NULL
700
701 #endif  /* CONFIG_PROC_FS */
702
703 static int idedisk_issue_flush(request_queue_t *q, struct gendisk *disk,
704                                sector_t *error_sector)
705 {
706         ide_drive_t *drive = q->queuedata;
707         struct request *rq;
708         int ret;
709
710         if (!drive->wcache)
711                 return 0;
712
713         rq = blk_get_request(q, WRITE, __GFP_WAIT);
714
715         memset(rq->cmd, 0, sizeof(rq->cmd));
716
717         if (ide_id_has_flush_cache_ext(drive->id) &&
718             (drive->capacity64 >= (1UL << 28)))
719                 rq->cmd[0] = WIN_FLUSH_CACHE_EXT;
720         else
721                 rq->cmd[0] = WIN_FLUSH_CACHE;
722
723
724         rq->flags |= REQ_DRIVE_TASK | REQ_SOFTBARRIER;
725         rq->buffer = rq->cmd;
726
727         ret = blk_execute_rq(q, disk, rq);
728
729         /*
730          * if we failed and caller wants error offset, get it
731          */
732         if (ret && error_sector)
733                 *error_sector = ide_get_error_location(drive, rq->cmd);
734
735         blk_put_request(rq);
736         return ret;
737 }
738
739 /*
740  * This is tightly woven into the driver->do_special can not touch.
741  * DON'T do it again until a total personality rewrite is committed.
742  */
743 static int set_multcount(ide_drive_t *drive, int arg)
744 {
745         struct request rq;
746
747         if (drive->special.b.set_multmode)
748                 return -EBUSY;
749         ide_init_drive_cmd (&rq);
750         rq.flags = REQ_DRIVE_CMD;
751         drive->mult_req = arg;
752         drive->special.b.set_multmode = 1;
753         (void) ide_do_drive_cmd (drive, &rq, ide_wait);
754         return (drive->mult_count == arg) ? 0 : -EIO;
755 }
756
757 static int set_nowerr(ide_drive_t *drive, int arg)
758 {
759         if (ide_spin_wait_hwgroup(drive))
760                 return -EBUSY;
761         drive->nowerr = arg;
762         drive->bad_wstat = arg ? BAD_R_STAT : BAD_W_STAT;
763         spin_unlock_irq(&ide_lock);
764         return 0;
765 }
766
767 static int write_cache(ide_drive_t *drive, int arg)
768 {
769         ide_task_t args;
770         int err;
771
772         if (!ide_id_has_flush_cache(drive->id))
773                 return 1;
774
775         memset(&args, 0, sizeof(ide_task_t));
776         args.tfRegister[IDE_FEATURE_OFFSET]     = (arg) ?
777                         SETFEATURES_EN_WCACHE : SETFEATURES_DIS_WCACHE;
778         args.tfRegister[IDE_COMMAND_OFFSET]     = WIN_SETFEATURES;
779         args.command_type                       = IDE_DRIVE_TASK_NO_DATA;
780         args.handler                            = &task_no_data_intr;
781
782         err = ide_raw_taskfile(drive, &args, NULL);
783         if (err)
784                 return err;
785
786         drive->wcache = arg;
787         return 0;
788 }
789
790 static int do_idedisk_flushcache (ide_drive_t *drive)
791 {
792         ide_task_t args;
793
794         memset(&args, 0, sizeof(ide_task_t));
795         if (ide_id_has_flush_cache_ext(drive->id))
796                 args.tfRegister[IDE_COMMAND_OFFSET]     = WIN_FLUSH_CACHE_EXT;
797         else
798                 args.tfRegister[IDE_COMMAND_OFFSET]     = WIN_FLUSH_CACHE;
799         args.command_type                       = IDE_DRIVE_TASK_NO_DATA;
800         args.handler                            = &task_no_data_intr;
801         return ide_raw_taskfile(drive, &args, NULL);
802 }
803
804 static int set_acoustic (ide_drive_t *drive, int arg)
805 {
806         ide_task_t args;
807
808         memset(&args, 0, sizeof(ide_task_t));
809         args.tfRegister[IDE_FEATURE_OFFSET]     = (arg) ? SETFEATURES_EN_AAM :
810                                                           SETFEATURES_DIS_AAM;
811         args.tfRegister[IDE_NSECTOR_OFFSET]     = arg;
812         args.tfRegister[IDE_COMMAND_OFFSET]     = WIN_SETFEATURES;
813         args.command_type = IDE_DRIVE_TASK_NO_DATA;
814         args.handler      = &task_no_data_intr;
815         ide_raw_taskfile(drive, &args, NULL);
816         drive->acoustic = arg;
817         return 0;
818 }
819
820 /*
821  * drive->addressing:
822  *      0: 28-bit
823  *      1: 48-bit
824  *      2: 48-bit capable doing 28-bit
825  */
826 static int set_lba_addressing(ide_drive_t *drive, int arg)
827 {
828         drive->addressing =  0;
829
830         if (HWIF(drive)->no_lba48)
831                 return 0;
832
833         if (!idedisk_supports_lba48(drive->id))
834                 return -EIO;
835         drive->addressing = arg;
836         return 0;
837 }
838
839 static void idedisk_add_settings(ide_drive_t *drive)
840 {
841         struct hd_driveid *id = drive->id;
842
843         ide_add_setting(drive,  "bios_cyl",             SETTING_RW,                                     -1,                     -1,                     TYPE_INT,       0,      65535,                          1,      1,      &drive->bios_cyl,               NULL);
844         ide_add_setting(drive,  "bios_head",            SETTING_RW,                                     -1,                     -1,                     TYPE_BYTE,      0,      255,                            1,      1,      &drive->bios_head,              NULL);
845         ide_add_setting(drive,  "bios_sect",            SETTING_RW,                                     -1,                     -1,                     TYPE_BYTE,      0,      63,                             1,      1,      &drive->bios_sect,              NULL);
846         ide_add_setting(drive,  "address",              SETTING_RW,                                     HDIO_GET_ADDRESS,       HDIO_SET_ADDRESS,       TYPE_INTA,      0,      2,                              1,      1,      &drive->addressing,     set_lba_addressing);
847         ide_add_setting(drive,  "bswap",                SETTING_READ,                                   -1,                     -1,                     TYPE_BYTE,      0,      1,                              1,      1,      &drive->bswap,                  NULL);
848         ide_add_setting(drive,  "multcount",            id ? SETTING_RW : SETTING_READ,                 HDIO_GET_MULTCOUNT,     HDIO_SET_MULTCOUNT,     TYPE_BYTE,      0,      id ? id->max_multsect : 0,      1,      1,      &drive->mult_count,             set_multcount);
849         ide_add_setting(drive,  "nowerr",               SETTING_RW,                                     HDIO_GET_NOWERR,        HDIO_SET_NOWERR,        TYPE_BYTE,      0,      1,                              1,      1,      &drive->nowerr,                 set_nowerr);
850         ide_add_setting(drive,  "lun",                  SETTING_RW,                                     -1,                     -1,                     TYPE_INT,       0,      7,                              1,      1,      &drive->lun,                    NULL);
851         ide_add_setting(drive,  "wcache",               SETTING_RW,                                     HDIO_GET_WCACHE,        HDIO_SET_WCACHE,        TYPE_BYTE,      0,      1,                              1,      1,      &drive->wcache,                 write_cache);
852         ide_add_setting(drive,  "acoustic",             SETTING_RW,                                     HDIO_GET_ACOUSTIC,      HDIO_SET_ACOUSTIC,      TYPE_BYTE,      0,      254,                            1,      1,      &drive->acoustic,               set_acoustic);
853         ide_add_setting(drive,  "failures",             SETTING_RW,                                     -1,                     -1,                     TYPE_INT,       0,      65535,                          1,      1,      &drive->failures,               NULL);
854         ide_add_setting(drive,  "max_failures",         SETTING_RW,                                     -1,                     -1,                     TYPE_INT,       0,      65535,                          1,      1,      &drive->max_failures,           NULL);
855 }
856
857 /*
858  * Power Management state machine. This one is rather trivial for now,
859  * we should probably add more, like switching back to PIO on suspend
860  * to help some BIOSes, re-do the door locking on resume, etc...
861  */
862
863 enum {
864         idedisk_pm_flush_cache  = ide_pm_state_start_suspend,
865         idedisk_pm_standby,
866
867         idedisk_pm_idle         = ide_pm_state_start_resume,
868         idedisk_pm_restore_dma,
869 };
870
871 static void idedisk_complete_power_step (ide_drive_t *drive, struct request *rq, u8 stat, u8 error)
872 {
873         switch (rq->pm->pm_step) {
874         case idedisk_pm_flush_cache:    /* Suspend step 1 (flush cache) complete */
875                 if (rq->pm->pm_state == 4)
876                         rq->pm->pm_step = ide_pm_state_completed;
877                 else
878                         rq->pm->pm_step = idedisk_pm_standby;
879                 break;
880         case idedisk_pm_standby:        /* Suspend step 2 (standby) complete */
881                 rq->pm->pm_step = ide_pm_state_completed;
882                 break;
883         case idedisk_pm_idle:           /* Resume step 1 (idle) complete */
884                 rq->pm->pm_step = idedisk_pm_restore_dma;
885                 break;
886         }
887 }
888
889 static ide_startstop_t idedisk_start_power_step (ide_drive_t *drive, struct request *rq)
890 {
891         ide_task_t *args = rq->special;
892
893         memset(args, 0, sizeof(*args));
894
895         switch (rq->pm->pm_step) {
896         case idedisk_pm_flush_cache:    /* Suspend step 1 (flush cache) */
897                 /* Not supported? Switch to next step now. */
898                 if (!drive->wcache || !ide_id_has_flush_cache(drive->id)) {
899                         idedisk_complete_power_step(drive, rq, 0, 0);
900                         return ide_stopped;
901                 }
902                 if (ide_id_has_flush_cache_ext(drive->id))
903                         args->tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE_EXT;
904                 else
905                         args->tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE;
906                 args->command_type = IDE_DRIVE_TASK_NO_DATA;
907                 args->handler      = &task_no_data_intr;
908                 return do_rw_taskfile(drive, args);
909
910         case idedisk_pm_standby:        /* Suspend step 2 (standby) */
911                 args->tfRegister[IDE_COMMAND_OFFSET] = WIN_STANDBYNOW1;
912                 args->command_type = IDE_DRIVE_TASK_NO_DATA;
913                 args->handler      = &task_no_data_intr;
914                 return do_rw_taskfile(drive, args);
915
916         case idedisk_pm_idle:           /* Resume step 1 (idle) */
917                 args->tfRegister[IDE_COMMAND_OFFSET] = WIN_IDLEIMMEDIATE;
918                 args->command_type = IDE_DRIVE_TASK_NO_DATA;
919                 args->handler = task_no_data_intr;
920                 return do_rw_taskfile(drive, args);
921
922         case idedisk_pm_restore_dma:    /* Resume step 2 (restore DMA) */
923                 /*
924                  * Right now, all we do is call hwif->ide_dma_check(drive),
925                  * we could be smarter and check for current xfer_speed
926                  * in struct drive etc...
927                  * Also, this step could be implemented as a generic helper
928                  * as most subdrivers will use it
929                  */
930                 if ((drive->id->capability & 1) == 0)
931                         break;
932                 if (HWIF(drive)->ide_dma_check == NULL)
933                         break;
934                 HWIF(drive)->ide_dma_check(drive);
935                 break;
936         }
937         rq->pm->pm_step = ide_pm_state_completed;
938         return ide_stopped;
939 }
940
941 static void idedisk_setup (ide_drive_t *drive)
942 {
943         struct hd_driveid *id = drive->id;
944         unsigned long long capacity;
945         int barrier;
946
947         idedisk_add_settings(drive);
948
949         if (drive->id_read == 0)
950                 return;
951
952         /*
953          * CompactFlash cards and their brethern look just like hard drives
954          * to us, but they are removable and don't have a doorlock mechanism.
955          */
956         if (drive->removable && !(drive->is_flash)) {
957                 /*
958                  * Removable disks (eg. SYQUEST); ignore 'WD' drives 
959                  */
960                 if (id->model[0] != 'W' || id->model[1] != 'D') {
961                         drive->doorlocking = 1;
962                 }
963         }
964
965         (void)set_lba_addressing(drive, 1);
966
967         if (drive->addressing == 1) {
968                 ide_hwif_t *hwif = HWIF(drive);
969                 int max_s = 2048;
970
971                 if (max_s > hwif->rqsize)
972                         max_s = hwif->rqsize;
973
974                 blk_queue_max_sectors(drive->queue, max_s);
975         }
976
977         printk(KERN_INFO "%s: max request size: %dKiB\n", drive->name, drive->queue->max_sectors / 2);
978
979         /* Extract geometry if we did not already have one for the drive */
980         if (!drive->cyl || !drive->head || !drive->sect) {
981                 drive->cyl     = drive->bios_cyl  = id->cyls;
982                 drive->head    = drive->bios_head = id->heads;
983                 drive->sect    = drive->bios_sect = id->sectors;
984         }
985
986         /* Handle logical geometry translation by the drive */
987         if ((id->field_valid & 1) && id->cur_cyls &&
988             id->cur_heads && (id->cur_heads <= 16) && id->cur_sectors) {
989                 drive->cyl  = id->cur_cyls;
990                 drive->head = id->cur_heads;
991                 drive->sect = id->cur_sectors;
992         }
993
994         /* Use physical geometry if what we have still makes no sense */
995         if (drive->head > 16 && id->heads && id->heads <= 16) {
996                 drive->cyl  = id->cyls;
997                 drive->head = id->heads;
998                 drive->sect = id->sectors;
999         }
1000
1001         /* calculate drive capacity, and select LBA if possible */
1002         init_idedisk_capacity (drive);
1003
1004         /* limit drive capacity to 137GB if LBA48 cannot be used */
1005         if (drive->addressing == 0 && drive->capacity64 > 1ULL << 28) {
1006                 printk(KERN_WARNING "%s: cannot use LBA48 - full capacity "
1007                        "%llu sectors (%llu MB)\n",
1008                        drive->name, (unsigned long long)drive->capacity64,
1009                        sectors_to_MB(drive->capacity64));
1010                 drive->capacity64 = 1ULL << 28;
1011         }
1012
1013         if (drive->hwif->no_lba48_dma && drive->addressing) {
1014                 if (drive->capacity64 > 1ULL << 28) {
1015                         printk(KERN_INFO "%s: cannot use LBA48 DMA - PIO mode will"
1016                                          " be used for accessing sectors > %u\n",
1017                                          drive->name, 1 << 28);
1018                 } else
1019                         drive->addressing = 0;
1020         }
1021
1022         /*
1023          * if possible, give fdisk access to more of the drive,
1024          * by correcting bios_cyls:
1025          */
1026         capacity = idedisk_capacity (drive);
1027         if (!drive->forced_geom) {
1028
1029                 if (idedisk_supports_lba48(drive->id)) {
1030                         /* compatibility */
1031                         drive->bios_sect = 63;
1032                         drive->bios_head = 255;
1033                 }
1034
1035                 if (drive->bios_sect && drive->bios_head) {
1036                         unsigned int cap0 = capacity; /* truncate to 32 bits */
1037                         unsigned int cylsz, cyl;
1038
1039                         if (cap0 != capacity)
1040                                 drive->bios_cyl = 65535;
1041                         else {
1042                                 cylsz = drive->bios_sect * drive->bios_head;
1043                                 cyl = cap0 / cylsz;
1044                                 if (cyl > 65535)
1045                                         cyl = 65535;
1046                                 if (cyl > drive->bios_cyl)
1047                                         drive->bios_cyl = cyl;
1048                         }
1049                 }
1050         }
1051         printk(KERN_INFO "%s: %llu sectors (%llu MB)",
1052                          drive->name, capacity, sectors_to_MB(capacity));
1053
1054         /* Only print cache size when it was specified */
1055         if (id->buf_size)
1056                 printk (" w/%dKiB Cache", id->buf_size/2);
1057
1058         printk(", CHS=%d/%d/%d", 
1059                drive->bios_cyl, drive->bios_head, drive->bios_sect);
1060         if (drive->using_dma)
1061                 ide_dma_verbose(drive);
1062         printk("\n");
1063
1064         drive->mult_count = 0;
1065         if (id->max_multsect) {
1066 #ifdef CONFIG_IDEDISK_MULTI_MODE
1067                 id->multsect = ((id->max_multsect/2) > 1) ? id->max_multsect : 0;
1068                 id->multsect_valid = id->multsect ? 1 : 0;
1069                 drive->mult_req = id->multsect_valid ? id->max_multsect : INITIAL_MULT_COUNT;
1070                 drive->special.b.set_multmode = drive->mult_req ? 1 : 0;
1071 #else   /* original, pre IDE-NFG, per request of AC */
1072                 drive->mult_req = INITIAL_MULT_COUNT;
1073                 if (drive->mult_req > id->max_multsect)
1074                         drive->mult_req = id->max_multsect;
1075                 if (drive->mult_req || ((id->multsect_valid & 1) && id->multsect))
1076                         drive->special.b.set_multmode = 1;
1077 #endif  /* CONFIG_IDEDISK_MULTI_MODE */
1078         }
1079         drive->no_io_32bit = id->dword_io ? 1 : 0;
1080
1081         /* write cache enabled? */
1082         if ((id->csfo & 1) || (id->cfs_enable_1 & (1 << 5)))
1083                 drive->wcache = 1;
1084
1085         write_cache(drive, 1);
1086
1087         /*
1088          * We must avoid issuing commands a drive does not understand
1089          * or we may crash it. We check flush cache is supported. We also
1090          * check we have the LBA48 flush cache if the drive capacity is
1091          * too large. By this time we have trimmed the drive capacity if
1092          * LBA48 is not available so we don't need to recheck that.
1093          */
1094         barrier = 0;
1095         if (ide_id_has_flush_cache(id))
1096                 barrier = 1;
1097         if (drive->addressing == 1) {
1098                 /* Can't issue the correct flush ? */
1099                 if (capacity > (1ULL << 28) && !ide_id_has_flush_cache_ext(id))
1100                         barrier = 0;
1101         }
1102
1103         printk(KERN_DEBUG "%s: cache flushes %ssupported\n",
1104                 drive->name, barrier ? "" : "not ");
1105         if (barrier) {
1106                 blk_queue_ordered(drive->queue, 1);
1107                 blk_queue_issue_flush_fn(drive->queue, idedisk_issue_flush);
1108         }
1109 }
1110
1111 static void ide_cacheflush_p(ide_drive_t *drive)
1112 {
1113         if (!drive->wcache || !ide_id_has_flush_cache(drive->id))
1114                 return;
1115
1116         if (do_idedisk_flushcache(drive))
1117                 printk(KERN_INFO "%s: wcache flush failed!\n", drive->name);
1118 }
1119
1120 static int idedisk_cleanup (ide_drive_t *drive)
1121 {
1122         struct gendisk *g = drive->disk;
1123         ide_cacheflush_p(drive);
1124         if (ide_unregister_subdriver(drive))
1125                 return 1;
1126         del_gendisk(g);
1127         drive->devfs_name[0] = '\0';
1128         g->fops = ide_fops;
1129         return 0;
1130 }
1131
1132 static int idedisk_attach(ide_drive_t *drive);
1133
1134 static void ide_device_shutdown(struct device *dev)
1135 {
1136         ide_drive_t *drive = container_of(dev, ide_drive_t, gendev);
1137
1138 #ifdef  CONFIG_ALPHA
1139         /* On Alpha, halt(8) doesn't actually turn the machine off,
1140            it puts you into the sort of firmware monitor. Typically,
1141            it's used to boot another kernel image, so it's not much
1142            different from reboot(8). Therefore, we don't need to
1143            spin down the disk in this case, especially since Alpha
1144            firmware doesn't handle disks in standby mode properly.
1145            On the other hand, it's reasonably safe to turn the power
1146            off when the shutdown process reaches the firmware prompt,
1147            as the firmware initialization takes rather long time -
1148            at least 10 seconds, which should be sufficient for
1149            the disk to expire its write cache. */
1150         if (system_state != SYSTEM_POWER_OFF) {
1151 #else
1152         if (system_state == SYSTEM_RESTART) {
1153 #endif
1154                 ide_cacheflush_p(drive);
1155                 return;
1156         }
1157
1158         printk("Shutdown: %s\n", drive->name);
1159         dev->bus->suspend(dev, PM_SUSPEND_STANDBY);
1160 }
1161
1162 /*
1163  *      IDE subdriver functions, registered with ide.c
1164  */
1165 static ide_driver_t idedisk_driver = {
1166         .owner                  = THIS_MODULE,
1167         .gen_driver = {
1168                 .shutdown       = ide_device_shutdown,
1169         },
1170         .name                   = "ide-disk",
1171         .version                = IDEDISK_VERSION,
1172         .media                  = ide_disk,
1173         .busy                   = 0,
1174         .supports_dsc_overlap   = 0,
1175         .cleanup                = idedisk_cleanup,
1176         .do_request             = ide_do_rw_disk,
1177         .pre_reset              = idedisk_pre_reset,
1178         .capacity               = idedisk_capacity,
1179         .special                = idedisk_special,
1180         .proc                   = idedisk_proc,
1181         .attach                 = idedisk_attach,
1182         .drives                 = LIST_HEAD_INIT(idedisk_driver.drives),
1183         .start_power_step       = idedisk_start_power_step,
1184         .complete_power_step    = idedisk_complete_power_step,
1185 };
1186
1187 static int idedisk_open(struct inode *inode, struct file *filp)
1188 {
1189         ide_drive_t *drive = inode->i_bdev->bd_disk->private_data;
1190         drive->usage++;
1191         if (drive->removable && drive->usage == 1) {
1192                 ide_task_t args;
1193                 memset(&args, 0, sizeof(ide_task_t));
1194                 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_DOORLOCK;
1195                 args.command_type = IDE_DRIVE_TASK_NO_DATA;
1196                 args.handler      = &task_no_data_intr;
1197                 check_disk_change(inode->i_bdev);
1198                 /*
1199                  * Ignore the return code from door_lock,
1200                  * since the open() has already succeeded,
1201                  * and the door_lock is irrelevant at this point.
1202                  */
1203                 if (drive->doorlocking && ide_raw_taskfile(drive, &args, NULL))
1204                         drive->doorlocking = 0;
1205         }
1206         return 0;
1207 }
1208
1209 static int idedisk_release(struct inode *inode, struct file *filp)
1210 {
1211         ide_drive_t *drive = inode->i_bdev->bd_disk->private_data;
1212         if (drive->usage == 1)
1213                 ide_cacheflush_p(drive);
1214         if (drive->removable && drive->usage == 1) {
1215                 ide_task_t args;
1216                 memset(&args, 0, sizeof(ide_task_t));
1217                 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_DOORUNLOCK;
1218                 args.command_type = IDE_DRIVE_TASK_NO_DATA;
1219                 args.handler      = &task_no_data_intr;
1220                 if (drive->doorlocking && ide_raw_taskfile(drive, &args, NULL))
1221                         drive->doorlocking = 0;
1222         }
1223         drive->usage--;
1224         return 0;
1225 }
1226
1227 static int idedisk_ioctl(struct inode *inode, struct file *file,
1228                         unsigned int cmd, unsigned long arg)
1229 {
1230         struct block_device *bdev = inode->i_bdev;
1231         return generic_ide_ioctl(file, bdev, cmd, arg);
1232 }
1233
1234 static int idedisk_media_changed(struct gendisk *disk)
1235 {
1236         ide_drive_t *drive = disk->private_data;
1237
1238         /* do not scan partitions twice if this is a removable device */
1239         if (drive->attach) {
1240                 drive->attach = 0;
1241                 return 0;
1242         }
1243         /* if removable, always assume it was changed */
1244         return drive->removable;
1245 }
1246
1247 static int idedisk_revalidate_disk(struct gendisk *disk)
1248 {
1249         ide_drive_t *drive = disk->private_data;
1250         set_capacity(disk, idedisk_capacity(drive));
1251         return 0;
1252 }
1253
1254 static struct block_device_operations idedisk_ops = {
1255         .owner          = THIS_MODULE,
1256         .open           = idedisk_open,
1257         .release        = idedisk_release,
1258         .ioctl          = idedisk_ioctl,
1259         .media_changed  = idedisk_media_changed,
1260         .revalidate_disk= idedisk_revalidate_disk
1261 };
1262
1263 MODULE_DESCRIPTION("ATA DISK Driver");
1264
1265 static int idedisk_attach(ide_drive_t *drive)
1266 {
1267         struct gendisk *g = drive->disk;
1268
1269         /* strstr("foo", "") is non-NULL */
1270         if (!strstr("ide-disk", drive->driver_req))
1271                 goto failed;
1272         if (!drive->present)
1273                 goto failed;
1274         if (drive->media != ide_disk)
1275                 goto failed;
1276
1277         if (ide_register_subdriver(drive, &idedisk_driver)) {
1278                 printk (KERN_ERR "ide-disk: %s: Failed to register the driver with ide.c\n", drive->name);
1279                 goto failed;
1280         }
1281         DRIVER(drive)->busy++;
1282         idedisk_setup(drive);
1283         if ((!drive->head || drive->head > 16) && !drive->select.b.lba) {
1284                 printk(KERN_ERR "%s: INVALID GEOMETRY: %d PHYSICAL HEADS?\n",
1285                         drive->name, drive->head);
1286                 drive->attach = 0;
1287         } else
1288                 drive->attach = 1;
1289         DRIVER(drive)->busy--;
1290         g->minors = 1 << PARTN_BITS;
1291         strcpy(g->devfs_name, drive->devfs_name);
1292         g->driverfs_dev = &drive->gendev;
1293         g->flags = drive->removable ? GENHD_FL_REMOVABLE : 0;
1294         set_capacity(g, idedisk_capacity(drive));
1295         g->fops = &idedisk_ops;
1296         add_disk(g);
1297         return 0;
1298 failed:
1299         return 1;
1300 }
1301
1302 static void __exit idedisk_exit (void)
1303 {
1304         ide_unregister_driver(&idedisk_driver);
1305 }
1306
1307 static int idedisk_init (void)
1308 {
1309         return ide_register_driver(&idedisk_driver);
1310 }
1311
1312 module_init(idedisk_init);
1313 module_exit(idedisk_exit);
1314 MODULE_LICENSE("GPL");