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