]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/ide/ide-disk.c
Merge branch 'x86-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-omap-h63xx.git] / drivers / ide / ide-disk.c
1 /*
2  *  Copyright (C) 1994-1998        Linus Torvalds & authors (see below)
3  *  Copyright (C) 1998-2002        Linux ATA Development
4  *                                    Andre Hedrick <andre@linux-ide.org>
5  *  Copyright (C) 2003             Red Hat <alan@redhat.com>
6  *  Copyright (C) 2003-2005, 2007  Bartlomiej Zolnierkiewicz
7  */
8
9 /*
10  *  Mostly written by Mark Lord <mlord@pobox.com>
11  *                and Gadi Oxman <gadio@netvision.net.il>
12  *                and Andre Hedrick <andre@linux-ide.org>
13  *
14  * This is the IDE/ATA disk driver, as evolved from hd.c and ide.c.
15  */
16
17 #define IDEDISK_VERSION "1.18"
18
19 #include <linux/module.h>
20 #include <linux/types.h>
21 #include <linux/string.h>
22 #include <linux/kernel.h>
23 #include <linux/timer.h>
24 #include <linux/mm.h>
25 #include <linux/interrupt.h>
26 #include <linux/major.h>
27 #include <linux/errno.h>
28 #include <linux/genhd.h>
29 #include <linux/slab.h>
30 #include <linux/delay.h>
31 #include <linux/mutex.h>
32 #include <linux/leds.h>
33 #include <linux/ide.h>
34 #include <linux/hdreg.h>
35
36 #include <asm/byteorder.h>
37 #include <asm/irq.h>
38 #include <asm/uaccess.h>
39 #include <asm/io.h>
40 #include <asm/div64.h>
41
42 #if !defined(CONFIG_DEBUG_BLOCK_EXT_DEVT)
43 #define IDE_DISK_MINORS         (1 << PARTN_BITS)
44 #else
45 #define IDE_DISK_MINORS         0
46 #endif
47
48 #include "ide-disk.h"
49
50 static DEFINE_MUTEX(idedisk_ref_mutex);
51
52 #define to_ide_disk(obj) container_of(obj, struct ide_disk_obj, kref)
53
54 static void ide_disk_release(struct kref *);
55
56 static struct ide_disk_obj *ide_disk_get(struct gendisk *disk)
57 {
58         struct ide_disk_obj *idkp = NULL;
59
60         mutex_lock(&idedisk_ref_mutex);
61         idkp = ide_disk_g(disk);
62         if (idkp) {
63                 if (ide_device_get(idkp->drive))
64                         idkp = NULL;
65                 else
66                         kref_get(&idkp->kref);
67         }
68         mutex_unlock(&idedisk_ref_mutex);
69         return idkp;
70 }
71
72 static void ide_disk_put(struct ide_disk_obj *idkp)
73 {
74         ide_drive_t *drive = idkp->drive;
75
76         mutex_lock(&idedisk_ref_mutex);
77         kref_put(&idkp->kref, ide_disk_release);
78         ide_device_put(drive);
79         mutex_unlock(&idedisk_ref_mutex);
80 }
81
82 static const u8 ide_rw_cmds[] = {
83         ATA_CMD_READ_MULTI,
84         ATA_CMD_WRITE_MULTI,
85         ATA_CMD_READ_MULTI_EXT,
86         ATA_CMD_WRITE_MULTI_EXT,
87         ATA_CMD_PIO_READ,
88         ATA_CMD_PIO_WRITE,
89         ATA_CMD_PIO_READ_EXT,
90         ATA_CMD_PIO_WRITE_EXT,
91         ATA_CMD_READ,
92         ATA_CMD_WRITE,
93         ATA_CMD_READ_EXT,
94         ATA_CMD_WRITE_EXT,
95 };
96
97 static const u8 ide_data_phases[] = {
98         TASKFILE_MULTI_IN,
99         TASKFILE_MULTI_OUT,
100         TASKFILE_IN,
101         TASKFILE_OUT,
102         TASKFILE_IN_DMA,
103         TASKFILE_OUT_DMA,
104 };
105
106 static void ide_tf_set_cmd(ide_drive_t *drive, ide_task_t *task, u8 dma)
107 {
108         u8 index, lba48, write;
109
110         lba48 = (task->tf_flags & IDE_TFLAG_LBA48) ? 2 : 0;
111         write = (task->tf_flags & IDE_TFLAG_WRITE) ? 1 : 0;
112
113         if (dma)
114                 index = 8;
115         else
116                 index = drive->mult_count ? 0 : 4;
117
118         task->tf.command = ide_rw_cmds[index + lba48 + write];
119
120         if (dma)
121                 index = 8; /* fixup index */
122
123         task->data_phase = ide_data_phases[index / 2 + write];
124 }
125
126 /*
127  * __ide_do_rw_disk() issues READ and WRITE commands to a disk,
128  * using LBA if supported, or CHS otherwise, to address sectors.
129  */
130 static ide_startstop_t __ide_do_rw_disk(ide_drive_t *drive, struct request *rq,
131                                         sector_t block)
132 {
133         ide_hwif_t *hwif        = HWIF(drive);
134         u16 nsectors            = (u16)rq->nr_sectors;
135         u8 lba48                = !!(drive->dev_flags & IDE_DFLAG_LBA48);
136         u8 dma                  = !!(drive->dev_flags & IDE_DFLAG_USING_DMA);
137         ide_task_t              task;
138         struct ide_taskfile     *tf = &task.tf;
139         ide_startstop_t         rc;
140
141         if ((hwif->host_flags & IDE_HFLAG_NO_LBA48_DMA) && lba48 && dma) {
142                 if (block + rq->nr_sectors > 1ULL << 28)
143                         dma = 0;
144                 else
145                         lba48 = 0;
146         }
147
148         if (!dma) {
149                 ide_init_sg_cmd(drive, rq);
150                 ide_map_sg(drive, rq);
151         }
152
153         memset(&task, 0, sizeof(task));
154         task.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
155
156         if (drive->dev_flags & IDE_DFLAG_LBA) {
157                 if (lba48) {
158                         pr_debug("%s: LBA=0x%012llx\n", drive->name,
159                                         (unsigned long long)block);
160
161                         tf->hob_nsect = (nsectors >> 8) & 0xff;
162                         tf->hob_lbal  = (u8)(block >> 24);
163                         if (sizeof(block) != 4) {
164                                 tf->hob_lbam = (u8)((u64)block >> 32);
165                                 tf->hob_lbah = (u8)((u64)block >> 40);
166                         }
167
168                         tf->nsect  = nsectors & 0xff;
169                         tf->lbal   = (u8) block;
170                         tf->lbam   = (u8)(block >>  8);
171                         tf->lbah   = (u8)(block >> 16);
172
173                         task.tf_flags |= (IDE_TFLAG_LBA48 | IDE_TFLAG_HOB);
174                 } else {
175                         tf->nsect  = nsectors & 0xff;
176                         tf->lbal   = block;
177                         tf->lbam   = block >>= 8;
178                         tf->lbah   = block >>= 8;
179                         tf->device = (block >> 8) & 0xf;
180                 }
181
182                 tf->device |= ATA_LBA;
183         } else {
184                 unsigned int sect, head, cyl, track;
185
186                 track = (int)block / drive->sect;
187                 sect  = (int)block % drive->sect + 1;
188                 head  = track % drive->head;
189                 cyl   = track / drive->head;
190
191                 pr_debug("%s: CHS=%u/%u/%u\n", drive->name, cyl, head, sect);
192
193                 tf->nsect  = nsectors & 0xff;
194                 tf->lbal   = sect;
195                 tf->lbam   = cyl;
196                 tf->lbah   = cyl >> 8;
197                 tf->device = head;
198         }
199
200         if (rq_data_dir(rq))
201                 task.tf_flags |= IDE_TFLAG_WRITE;
202
203         ide_tf_set_cmd(drive, &task, dma);
204         if (!dma)
205                 hwif->data_phase = task.data_phase;
206         task.rq = rq;
207
208         rc = do_rw_taskfile(drive, &task);
209
210         if (rc == ide_stopped && dma) {
211                 /* fallback to PIO */
212                 task.tf_flags |= IDE_TFLAG_DMA_PIO_FALLBACK;
213                 ide_tf_set_cmd(drive, &task, 0);
214                 hwif->data_phase = task.data_phase;
215                 ide_init_sg_cmd(drive, rq);
216                 rc = do_rw_taskfile(drive, &task);
217         }
218
219         return rc;
220 }
221
222 /*
223  * 268435455  == 137439 MB or 28bit limit
224  * 320173056  == 163929 MB or 48bit addressing
225  * 1073741822 == 549756 MB or 48bit addressing fake drive
226  */
227
228 static ide_startstop_t ide_do_rw_disk(ide_drive_t *drive, struct request *rq,
229                                       sector_t block)
230 {
231         ide_hwif_t *hwif = HWIF(drive);
232
233         BUG_ON(drive->dev_flags & IDE_DFLAG_BLOCKED);
234
235         if (!blk_fs_request(rq)) {
236                 blk_dump_rq_flags(rq, "ide_do_rw_disk - bad command");
237                 ide_end_request(drive, 0, 0);
238                 return ide_stopped;
239         }
240
241         ledtrig_ide_activity();
242
243         pr_debug("%s: %sing: block=%llu, sectors=%lu, buffer=0x%08lx\n",
244                  drive->name, rq_data_dir(rq) == READ ? "read" : "writ",
245                  (unsigned long long)block, rq->nr_sectors,
246                  (unsigned long)rq->buffer);
247
248         if (hwif->rw_disk)
249                 hwif->rw_disk(drive, rq);
250
251         return __ide_do_rw_disk(drive, rq, block);
252 }
253
254 /*
255  * Queries for true maximum capacity of the drive.
256  * Returns maximum LBA address (> 0) of the drive, 0 if failed.
257  */
258 static u64 idedisk_read_native_max_address(ide_drive_t *drive, int lba48)
259 {
260         ide_task_t args;
261         struct ide_taskfile *tf = &args.tf;
262         u64 addr = 0;
263
264         /* Create IDE/ATA command request structure */
265         memset(&args, 0, sizeof(ide_task_t));
266         if (lba48)
267                 tf->command = ATA_CMD_READ_NATIVE_MAX_EXT;
268         else
269                 tf->command = ATA_CMD_READ_NATIVE_MAX;
270         tf->device  = ATA_LBA;
271         args.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
272         if (lba48)
273                 args.tf_flags |= (IDE_TFLAG_LBA48 | IDE_TFLAG_HOB);
274         /* submit command request */
275         ide_no_data_taskfile(drive, &args);
276
277         /* if OK, compute maximum address value */
278         if ((tf->status & 0x01) == 0)
279                 addr = ide_get_lba_addr(tf, lba48) + 1;
280
281         return addr;
282 }
283
284 /*
285  * Sets maximum virtual LBA address of the drive.
286  * Returns new maximum virtual LBA address (> 0) or 0 on failure.
287  */
288 static u64 idedisk_set_max_address(ide_drive_t *drive, u64 addr_req, int lba48)
289 {
290         ide_task_t args;
291         struct ide_taskfile *tf = &args.tf;
292         u64 addr_set = 0;
293
294         addr_req--;
295         /* Create IDE/ATA command request structure */
296         memset(&args, 0, sizeof(ide_task_t));
297         tf->lbal     = (addr_req >>  0) & 0xff;
298         tf->lbam     = (addr_req >>= 8) & 0xff;
299         tf->lbah     = (addr_req >>= 8) & 0xff;
300         if (lba48) {
301                 tf->hob_lbal = (addr_req >>= 8) & 0xff;
302                 tf->hob_lbam = (addr_req >>= 8) & 0xff;
303                 tf->hob_lbah = (addr_req >>= 8) & 0xff;
304                 tf->command  = ATA_CMD_SET_MAX_EXT;
305         } else {
306                 tf->device   = (addr_req >>= 8) & 0x0f;
307                 tf->command  = ATA_CMD_SET_MAX;
308         }
309         tf->device |= ATA_LBA;
310         args.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
311         if (lba48)
312                 args.tf_flags |= (IDE_TFLAG_LBA48 | IDE_TFLAG_HOB);
313         /* submit command request */
314         ide_no_data_taskfile(drive, &args);
315         /* if OK, compute maximum address value */
316         if ((tf->status & 0x01) == 0)
317                 addr_set = ide_get_lba_addr(tf, lba48) + 1;
318
319         return addr_set;
320 }
321
322 static unsigned long long sectors_to_MB(unsigned long long n)
323 {
324         n <<= 9;                /* make it bytes */
325         do_div(n, 1000000);     /* make it MB */
326         return n;
327 }
328
329 /*
330  * Some disks report total number of sectors instead of
331  * maximum sector address.  We list them here.
332  */
333 static const struct drive_list_entry hpa_list[] = {
334         { "ST340823A",  NULL },
335         { "ST320413A",  NULL },
336         { "ST310211A",  NULL },
337         { NULL,         NULL }
338 };
339
340 static void idedisk_check_hpa(ide_drive_t *drive)
341 {
342         unsigned long long capacity, set_max;
343         int lba48 = ata_id_lba48_enabled(drive->id);
344
345         capacity = drive->capacity64;
346
347         set_max = idedisk_read_native_max_address(drive, lba48);
348
349         if (ide_in_drive_list(drive->id, hpa_list)) {
350                 /*
351                  * Since we are inclusive wrt to firmware revisions do this
352                  * extra check and apply the workaround only when needed.
353                  */
354                 if (set_max == capacity + 1)
355                         set_max--;
356         }
357
358         if (set_max <= capacity)
359                 return;
360
361         printk(KERN_INFO "%s: Host Protected Area detected.\n"
362                          "\tcurrent capacity is %llu sectors (%llu MB)\n"
363                          "\tnative  capacity is %llu sectors (%llu MB)\n",
364                          drive->name,
365                          capacity, sectors_to_MB(capacity),
366                          set_max, sectors_to_MB(set_max));
367
368         set_max = idedisk_set_max_address(drive, set_max, lba48);
369
370         if (set_max) {
371                 drive->capacity64 = set_max;
372                 printk(KERN_INFO "%s: Host Protected Area disabled.\n",
373                                  drive->name);
374         }
375 }
376
377 static void init_idedisk_capacity(ide_drive_t *drive)
378 {
379         u16 *id = drive->id;
380         int lba;
381
382         if (ata_id_lba48_enabled(id)) {
383                 /* drive speaks 48-bit LBA */
384                 lba = 1;
385                 drive->capacity64 = ata_id_u64(id, ATA_ID_LBA_CAPACITY_2);
386         } else if (ata_id_has_lba(id) && ata_id_is_lba_capacity_ok(id)) {
387                 /* drive speaks 28-bit LBA */
388                 lba = 1;
389                 drive->capacity64 = ata_id_u32(id, ATA_ID_LBA_CAPACITY);
390         } else {
391                 /* drive speaks boring old 28-bit CHS */
392                 lba = 0;
393                 drive->capacity64 = drive->cyl * drive->head * drive->sect;
394         }
395
396         if (lba) {
397                 drive->dev_flags |= IDE_DFLAG_LBA;
398
399                 /*
400                 * If this device supports the Host Protected Area feature set,
401                 * then we may need to change our opinion about its capacity.
402                 */
403                 if (ata_id_hpa_enabled(id))
404                         idedisk_check_hpa(drive);
405         }
406 }
407
408 sector_t ide_disk_capacity(ide_drive_t *drive)
409 {
410         return drive->capacity64;
411 }
412
413 static void idedisk_prepare_flush(struct request_queue *q, struct request *rq)
414 {
415         ide_drive_t *drive = q->queuedata;
416         ide_task_t *task = kmalloc(sizeof(*task), GFP_ATOMIC);
417
418         /* FIXME: map struct ide_taskfile on rq->cmd[] */
419         BUG_ON(task == NULL);
420
421         memset(task, 0, sizeof(*task));
422         if (ata_id_flush_ext_enabled(drive->id) &&
423             (drive->capacity64 >= (1UL << 28)))
424                 task->tf.command = ATA_CMD_FLUSH_EXT;
425         else
426                 task->tf.command = ATA_CMD_FLUSH;
427         task->tf_flags   = IDE_TFLAG_OUT_TF | IDE_TFLAG_OUT_DEVICE |
428                            IDE_TFLAG_DYN;
429         task->data_phase = TASKFILE_NO_DATA;
430
431         rq->cmd_type = REQ_TYPE_ATA_TASKFILE;
432         rq->cmd_flags |= REQ_SOFTBARRIER;
433         rq->special = task;
434 }
435
436 ide_devset_get(multcount, mult_count);
437
438 /*
439  * This is tightly woven into the driver->do_special can not touch.
440  * DON'T do it again until a total personality rewrite is committed.
441  */
442 static int set_multcount(ide_drive_t *drive, int arg)
443 {
444         struct request *rq;
445         int error;
446
447         if (arg < 0 || arg > (drive->id[ATA_ID_MAX_MULTSECT] & 0xff))
448                 return -EINVAL;
449
450         if (drive->special.b.set_multmode)
451                 return -EBUSY;
452
453         rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
454         rq->cmd_type = REQ_TYPE_ATA_TASKFILE;
455
456         drive->mult_req = arg;
457         drive->special.b.set_multmode = 1;
458         error = blk_execute_rq(drive->queue, NULL, rq, 0);
459         blk_put_request(rq);
460
461         return (drive->mult_count == arg) ? 0 : -EIO;
462 }
463
464 ide_devset_get_flag(nowerr, IDE_DFLAG_NOWERR);
465
466 static int set_nowerr(ide_drive_t *drive, int arg)
467 {
468         if (arg < 0 || arg > 1)
469                 return -EINVAL;
470
471         if (arg)
472                 drive->dev_flags |= IDE_DFLAG_NOWERR;
473         else
474                 drive->dev_flags &= ~IDE_DFLAG_NOWERR;
475
476         drive->bad_wstat = arg ? BAD_R_STAT : BAD_W_STAT;
477
478         return 0;
479 }
480
481 static int ide_do_setfeature(ide_drive_t *drive, u8 feature, u8 nsect)
482 {
483         ide_task_t task;
484
485         memset(&task, 0, sizeof(task));
486         task.tf.feature = feature;
487         task.tf.nsect   = nsect;
488         task.tf.command = ATA_CMD_SET_FEATURES;
489         task.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
490
491         return ide_no_data_taskfile(drive, &task);
492 }
493
494 static void update_ordered(ide_drive_t *drive)
495 {
496         u16 *id = drive->id;
497         unsigned ordered = QUEUE_ORDERED_NONE;
498         prepare_flush_fn *prep_fn = NULL;
499
500         if (drive->dev_flags & IDE_DFLAG_WCACHE) {
501                 unsigned long long capacity;
502                 int barrier;
503                 /*
504                  * We must avoid issuing commands a drive does not
505                  * understand or we may crash it. We check flush cache
506                  * is supported. We also check we have the LBA48 flush
507                  * cache if the drive capacity is too large. By this
508                  * time we have trimmed the drive capacity if LBA48 is
509                  * not available so we don't need to recheck that.
510                  */
511                 capacity = ide_disk_capacity(drive);
512                 barrier = ata_id_flush_enabled(id) &&
513                         (drive->dev_flags & IDE_DFLAG_NOFLUSH) == 0 &&
514                         ((drive->dev_flags & IDE_DFLAG_LBA48) == 0 ||
515                          capacity <= (1ULL << 28) ||
516                          ata_id_flush_ext_enabled(id));
517
518                 printk(KERN_INFO "%s: cache flushes %ssupported\n",
519                        drive->name, barrier ? "" : "not ");
520
521                 if (barrier) {
522                         ordered = QUEUE_ORDERED_DRAIN_FLUSH;
523                         prep_fn = idedisk_prepare_flush;
524                 }
525         } else
526                 ordered = QUEUE_ORDERED_DRAIN;
527
528         blk_queue_ordered(drive->queue, ordered, prep_fn);
529 }
530
531 ide_devset_get_flag(wcache, IDE_DFLAG_WCACHE);
532
533 static int set_wcache(ide_drive_t *drive, int arg)
534 {
535         int err = 1;
536
537         if (arg < 0 || arg > 1)
538                 return -EINVAL;
539
540         if (ata_id_flush_enabled(drive->id)) {
541                 err = ide_do_setfeature(drive,
542                         arg ? SETFEATURES_WC_ON : SETFEATURES_WC_OFF, 0);
543                 if (err == 0) {
544                         if (arg)
545                                 drive->dev_flags |= IDE_DFLAG_WCACHE;
546                         else
547                                 drive->dev_flags &= ~IDE_DFLAG_WCACHE;
548                 }
549         }
550
551         update_ordered(drive);
552
553         return err;
554 }
555
556 static int do_idedisk_flushcache(ide_drive_t *drive)
557 {
558         ide_task_t args;
559
560         memset(&args, 0, sizeof(ide_task_t));
561         if (ata_id_flush_ext_enabled(drive->id))
562                 args.tf.command = ATA_CMD_FLUSH_EXT;
563         else
564                 args.tf.command = ATA_CMD_FLUSH;
565         args.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
566         return ide_no_data_taskfile(drive, &args);
567 }
568
569 ide_devset_get(acoustic, acoustic);
570
571 static int set_acoustic(ide_drive_t *drive, int arg)
572 {
573         if (arg < 0 || arg > 254)
574                 return -EINVAL;
575
576         ide_do_setfeature(drive,
577                 arg ? SETFEATURES_AAM_ON : SETFEATURES_AAM_OFF, arg);
578
579         drive->acoustic = arg;
580
581         return 0;
582 }
583
584 ide_devset_get_flag(addressing, IDE_DFLAG_LBA48);
585
586 /*
587  * drive->addressing:
588  *      0: 28-bit
589  *      1: 48-bit
590  *      2: 48-bit capable doing 28-bit
591  */
592 static int set_addressing(ide_drive_t *drive, int arg)
593 {
594         if (arg < 0 || arg > 2)
595                 return -EINVAL;
596
597         if (arg && ((drive->hwif->host_flags & IDE_HFLAG_NO_LBA48) ||
598             ata_id_lba48_enabled(drive->id) == 0))
599                 return -EIO;
600
601         if (arg == 2)
602                 arg = 0;
603
604         if (arg)
605                 drive->dev_flags |= IDE_DFLAG_LBA48;
606         else
607                 drive->dev_flags &= ~IDE_DFLAG_LBA48;
608
609         return 0;
610 }
611
612 ide_ext_devset_rw(acoustic, acoustic);
613 ide_ext_devset_rw(address, addressing);
614 ide_ext_devset_rw(multcount, multcount);
615 ide_ext_devset_rw(wcache, wcache);
616
617 ide_ext_devset_rw_sync(nowerr, nowerr);
618
619 static void idedisk_setup(ide_drive_t *drive)
620 {
621         struct ide_disk_obj *idkp = drive->driver_data;
622         ide_hwif_t *hwif = drive->hwif;
623         u16 *id = drive->id;
624         char *m = (char *)&id[ATA_ID_PROD];
625         unsigned long long capacity;
626
627         ide_proc_register_driver(drive, idkp->driver);
628
629         if ((drive->dev_flags & IDE_DFLAG_ID_READ) == 0)
630                 return;
631
632         if (drive->dev_flags & IDE_DFLAG_REMOVABLE) {
633                 /*
634                  * Removable disks (eg. SYQUEST); ignore 'WD' drives
635                  */
636                 if (m[0] != 'W' || m[1] != 'D')
637                         drive->dev_flags |= IDE_DFLAG_DOORLOCKING;
638         }
639
640         (void)set_addressing(drive, 1);
641
642         if (drive->dev_flags & IDE_DFLAG_LBA48) {
643                 int max_s = 2048;
644
645                 if (max_s > hwif->rqsize)
646                         max_s = hwif->rqsize;
647
648                 blk_queue_max_sectors(drive->queue, max_s);
649         }
650
651         printk(KERN_INFO "%s: max request size: %dKiB\n", drive->name,
652                          drive->queue->max_sectors / 2);
653
654         /* calculate drive capacity, and select LBA if possible */
655         init_idedisk_capacity(drive);
656
657         /* limit drive capacity to 137GB if LBA48 cannot be used */
658         if ((drive->dev_flags & IDE_DFLAG_LBA48) == 0 &&
659             drive->capacity64 > 1ULL << 28) {
660                 printk(KERN_WARNING "%s: cannot use LBA48 - full capacity "
661                        "%llu sectors (%llu MB)\n",
662                        drive->name, (unsigned long long)drive->capacity64,
663                        sectors_to_MB(drive->capacity64));
664                 drive->capacity64 = 1ULL << 28;
665         }
666
667         if ((hwif->host_flags & IDE_HFLAG_NO_LBA48_DMA) &&
668             (drive->dev_flags & IDE_DFLAG_LBA48)) {
669                 if (drive->capacity64 > 1ULL << 28) {
670                         printk(KERN_INFO "%s: cannot use LBA48 DMA - PIO mode"
671                                          " will be used for accessing sectors "
672                                          "> %u\n", drive->name, 1 << 28);
673                 } else
674                         drive->dev_flags &= ~IDE_DFLAG_LBA48;
675         }
676
677         /*
678          * if possible, give fdisk access to more of the drive,
679          * by correcting bios_cyls:
680          */
681         capacity = ide_disk_capacity(drive);
682
683         if ((drive->dev_flags & IDE_DFLAG_FORCED_GEOM) == 0) {
684                 if (ata_id_lba48_enabled(drive->id)) {
685                         /* compatibility */
686                         drive->bios_sect = 63;
687                         drive->bios_head = 255;
688                 }
689
690                 if (drive->bios_sect && drive->bios_head) {
691                         unsigned int cap0 = capacity; /* truncate to 32 bits */
692                         unsigned int cylsz, cyl;
693
694                         if (cap0 != capacity)
695                                 drive->bios_cyl = 65535;
696                         else {
697                                 cylsz = drive->bios_sect * drive->bios_head;
698                                 cyl = cap0 / cylsz;
699                                 if (cyl > 65535)
700                                         cyl = 65535;
701                                 if (cyl > drive->bios_cyl)
702                                         drive->bios_cyl = cyl;
703                         }
704                 }
705         }
706         printk(KERN_INFO "%s: %llu sectors (%llu MB)",
707                          drive->name, capacity, sectors_to_MB(capacity));
708
709         /* Only print cache size when it was specified */
710         if (id[ATA_ID_BUF_SIZE])
711                 printk(KERN_CONT " w/%dKiB Cache", id[ATA_ID_BUF_SIZE] / 2);
712
713         printk(KERN_CONT ", CHS=%d/%d/%d\n",
714                          drive->bios_cyl, drive->bios_head, drive->bios_sect);
715
716         /* write cache enabled? */
717         if ((id[ATA_ID_CSFO] & 1) || ata_id_wcache_enabled(id))
718                 drive->dev_flags |= IDE_DFLAG_WCACHE;
719
720         set_wcache(drive, 1);
721 }
722
723 static void ide_cacheflush_p(ide_drive_t *drive)
724 {
725         if (ata_id_flush_enabled(drive->id) == 0 ||
726             (drive->dev_flags & IDE_DFLAG_WCACHE) == 0)
727                 return;
728
729         if (do_idedisk_flushcache(drive))
730                 printk(KERN_INFO "%s: wcache flush failed!\n", drive->name);
731 }
732
733 static void ide_disk_remove(ide_drive_t *drive)
734 {
735         struct ide_disk_obj *idkp = drive->driver_data;
736         struct gendisk *g = idkp->disk;
737
738         ide_proc_unregister_driver(drive, idkp->driver);
739
740         del_gendisk(g);
741
742         ide_cacheflush_p(drive);
743
744         ide_disk_put(idkp);
745 }
746
747 static void ide_disk_release(struct kref *kref)
748 {
749         struct ide_disk_obj *idkp = to_ide_disk(kref);
750         ide_drive_t *drive = idkp->drive;
751         struct gendisk *g = idkp->disk;
752
753         drive->driver_data = NULL;
754         g->private_data = NULL;
755         put_disk(g);
756         kfree(idkp);
757 }
758
759 static int ide_disk_probe(ide_drive_t *drive);
760
761 /*
762  * On HPA drives the capacity needs to be
763  * reinitilized on resume otherwise the disk
764  * can not be used and a hard reset is required
765  */
766 static void ide_disk_resume(ide_drive_t *drive)
767 {
768         if (ata_id_hpa_enabled(drive->id))
769                 init_idedisk_capacity(drive);
770 }
771
772 static void ide_device_shutdown(ide_drive_t *drive)
773 {
774 #ifdef  CONFIG_ALPHA
775         /* On Alpha, halt(8) doesn't actually turn the machine off,
776            it puts you into the sort of firmware monitor. Typically,
777            it's used to boot another kernel image, so it's not much
778            different from reboot(8). Therefore, we don't need to
779            spin down the disk in this case, especially since Alpha
780            firmware doesn't handle disks in standby mode properly.
781            On the other hand, it's reasonably safe to turn the power
782            off when the shutdown process reaches the firmware prompt,
783            as the firmware initialization takes rather long time -
784            at least 10 seconds, which should be sufficient for
785            the disk to expire its write cache. */
786         if (system_state != SYSTEM_POWER_OFF) {
787 #else
788         if (system_state == SYSTEM_RESTART) {
789 #endif
790                 ide_cacheflush_p(drive);
791                 return;
792         }
793
794         printk(KERN_INFO "Shutdown: %s\n", drive->name);
795
796         drive->gendev.bus->suspend(&drive->gendev, PMSG_SUSPEND);
797 }
798
799 static ide_driver_t idedisk_driver = {
800         .gen_driver = {
801                 .owner          = THIS_MODULE,
802                 .name           = "ide-disk",
803                 .bus            = &ide_bus_type,
804         },
805         .probe                  = ide_disk_probe,
806         .remove                 = ide_disk_remove,
807         .resume                 = ide_disk_resume,
808         .shutdown               = ide_device_shutdown,
809         .version                = IDEDISK_VERSION,
810         .do_request             = ide_do_rw_disk,
811         .end_request            = ide_end_request,
812         .error                  = __ide_error,
813 #ifdef CONFIG_IDE_PROC_FS
814         .proc                   = ide_disk_proc,
815         .settings               = ide_disk_settings,
816 #endif
817 };
818
819 static int idedisk_set_doorlock(ide_drive_t *drive, int on)
820 {
821         ide_task_t task;
822
823         memset(&task, 0, sizeof(task));
824         task.tf.command = on ? ATA_CMD_MEDIA_LOCK : ATA_CMD_MEDIA_UNLOCK;
825         task.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
826
827         return ide_no_data_taskfile(drive, &task);
828 }
829
830 static int idedisk_open(struct inode *inode, struct file *filp)
831 {
832         struct gendisk *disk = inode->i_bdev->bd_disk;
833         struct ide_disk_obj *idkp;
834         ide_drive_t *drive;
835
836         idkp = ide_disk_get(disk);
837         if (idkp == NULL)
838                 return -ENXIO;
839
840         drive = idkp->drive;
841
842         idkp->openers++;
843
844         if ((drive->dev_flags & IDE_DFLAG_REMOVABLE) && idkp->openers == 1) {
845                 check_disk_change(inode->i_bdev);
846                 /*
847                  * Ignore the return code from door_lock,
848                  * since the open() has already succeeded,
849                  * and the door_lock is irrelevant at this point.
850                  */
851                 if ((drive->dev_flags & IDE_DFLAG_DOORLOCKING) &&
852                     idedisk_set_doorlock(drive, 1))
853                         drive->dev_flags &= ~IDE_DFLAG_DOORLOCKING;
854         }
855         return 0;
856 }
857
858 static int idedisk_release(struct inode *inode, struct file *filp)
859 {
860         struct gendisk *disk = inode->i_bdev->bd_disk;
861         struct ide_disk_obj *idkp = ide_disk_g(disk);
862         ide_drive_t *drive = idkp->drive;
863
864         if (idkp->openers == 1)
865                 ide_cacheflush_p(drive);
866
867         if ((drive->dev_flags & IDE_DFLAG_REMOVABLE) && idkp->openers == 1) {
868                 if ((drive->dev_flags & IDE_DFLAG_DOORLOCKING) &&
869                     idedisk_set_doorlock(drive, 0))
870                         drive->dev_flags &= ~IDE_DFLAG_DOORLOCKING;
871         }
872
873         idkp->openers--;
874
875         ide_disk_put(idkp);
876
877         return 0;
878 }
879
880 static int idedisk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
881 {
882         struct ide_disk_obj *idkp = ide_disk_g(bdev->bd_disk);
883         ide_drive_t *drive = idkp->drive;
884
885         geo->heads = drive->bios_head;
886         geo->sectors = drive->bios_sect;
887         geo->cylinders = (u16)drive->bios_cyl; /* truncate */
888         return 0;
889 }
890
891 static int idedisk_media_changed(struct gendisk *disk)
892 {
893         struct ide_disk_obj *idkp = ide_disk_g(disk);
894         ide_drive_t *drive = idkp->drive;
895
896         /* do not scan partitions twice if this is a removable device */
897         if (drive->dev_flags & IDE_DFLAG_ATTACH) {
898                 drive->dev_flags &= ~IDE_DFLAG_ATTACH;
899                 return 0;
900         }
901
902         /* if removable, always assume it was changed */
903         return !!(drive->dev_flags & IDE_DFLAG_REMOVABLE);
904 }
905
906 static int idedisk_revalidate_disk(struct gendisk *disk)
907 {
908         struct ide_disk_obj *idkp = ide_disk_g(disk);
909         set_capacity(disk, ide_disk_capacity(idkp->drive));
910         return 0;
911 }
912
913 static struct block_device_operations idedisk_ops = {
914         .owner                  = THIS_MODULE,
915         .open                   = idedisk_open,
916         .release                = idedisk_release,
917         .ioctl                  = ide_disk_ioctl,
918         .getgeo                 = idedisk_getgeo,
919         .media_changed          = idedisk_media_changed,
920         .revalidate_disk        = idedisk_revalidate_disk
921 };
922
923 MODULE_DESCRIPTION("ATA DISK Driver");
924
925 static int ide_disk_probe(ide_drive_t *drive)
926 {
927         struct ide_disk_obj *idkp;
928         struct gendisk *g;
929
930         /* strstr("foo", "") is non-NULL */
931         if (!strstr("ide-disk", drive->driver_req))
932                 goto failed;
933
934         if (drive->media != ide_disk)
935                 goto failed;
936
937         idkp = kzalloc(sizeof(*idkp), GFP_KERNEL);
938         if (!idkp)
939                 goto failed;
940
941         g = alloc_disk_node(IDE_DISK_MINORS, hwif_to_node(drive->hwif));
942         if (!g)
943                 goto out_free_idkp;
944
945         ide_init_disk(g, drive);
946
947         kref_init(&idkp->kref);
948
949         idkp->drive = drive;
950         idkp->driver = &idedisk_driver;
951         idkp->disk = g;
952
953         g->private_data = &idkp->driver;
954
955         drive->driver_data = idkp;
956
957         idedisk_setup(drive);
958         if ((drive->dev_flags & IDE_DFLAG_LBA) == 0 &&
959             (drive->head == 0 || drive->head > 16)) {
960                 printk(KERN_ERR "%s: INVALID GEOMETRY: %d PHYSICAL HEADS?\n",
961                         drive->name, drive->head);
962                 drive->dev_flags &= ~IDE_DFLAG_ATTACH;
963         } else
964                 drive->dev_flags |= IDE_DFLAG_ATTACH;
965
966         g->minors = IDE_DISK_MINORS;
967         g->driverfs_dev = &drive->gendev;
968         g->flags |= GENHD_FL_EXT_DEVT;
969         if (drive->dev_flags & IDE_DFLAG_REMOVABLE)
970                 g->flags = GENHD_FL_REMOVABLE;
971         set_capacity(g, ide_disk_capacity(drive));
972         g->fops = &idedisk_ops;
973         add_disk(g);
974         return 0;
975
976 out_free_idkp:
977         kfree(idkp);
978 failed:
979         return -ENODEV;
980 }
981
982 static void __exit idedisk_exit(void)
983 {
984         driver_unregister(&idedisk_driver.gen_driver);
985 }
986
987 static int __init idedisk_init(void)
988 {
989         return driver_register(&idedisk_driver.gen_driver);
990 }
991
992 MODULE_ALIAS("ide:*m-disk*");
993 MODULE_ALIAS("ide-disk");
994 module_init(idedisk_init);
995 module_exit(idedisk_exit);
996 MODULE_LICENSE("GPL");