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