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