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