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