ide-disk: move IDE_DFLAG_DOORLOCKING flag handling to idedisk_set_doorlock()
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / ide / ide-disk.c
blob289a533afbd699be220689c3b70b9e2ce9c30891
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 */
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>
14 * This is the IDE/ATA disk driver, as evolved from hd.c and ide.c.
17 #define IDEDISK_VERSION "1.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>
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>
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
48 #include "ide-disk.h"
50 static DEFINE_MUTEX(idedisk_ref_mutex);
52 static void ide_disk_release(struct kref *);
54 static struct ide_disk_obj *ide_disk_get(struct gendisk *disk)
56 struct ide_disk_obj *idkp = NULL;
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);
66 mutex_unlock(&idedisk_ref_mutex);
67 return idkp;
70 static void ide_disk_put(struct ide_disk_obj *idkp)
72 ide_drive_t *drive = idkp->drive;
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);
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,
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,
104 static void ide_tf_set_cmd(ide_drive_t *drive, ide_task_t *task, u8 dma)
106 u8 index, lba48, write;
108 lba48 = (task->tf_flags & IDE_TFLAG_LBA48) ? 2 : 0;
109 write = (task->tf_flags & IDE_TFLAG_WRITE) ? 1 : 0;
111 if (dma)
112 index = 8;
113 else
114 index = drive->mult_count ? 0 : 4;
116 task->tf.command = ide_rw_cmds[index + lba48 + write];
118 if (dma)
119 index = 8; /* fixup index */
121 task->data_phase = ide_data_phases[index / 2 + write];
125 * __ide_do_rw_disk() issues READ and WRITE commands to a disk,
126 * using LBA if supported, or CHS otherwise, to address sectors.
128 static ide_startstop_t __ide_do_rw_disk(ide_drive_t *drive, struct request *rq,
129 sector_t block)
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;
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;
146 if (!dma) {
147 ide_init_sg_cmd(drive, rq);
148 ide_map_sg(drive, rq);
151 memset(&task, 0, sizeof(task));
152 task.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
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);
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);
166 tf->nsect = nsectors & 0xff;
167 tf->lbal = (u8) block;
168 tf->lbam = (u8)(block >> 8);
169 tf->lbah = (u8)(block >> 16);
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;
180 tf->device |= ATA_LBA;
181 } else {
182 unsigned int sect, head, cyl, track;
184 track = (int)block / drive->sect;
185 sect = (int)block % drive->sect + 1;
186 head = track % drive->head;
187 cyl = track / drive->head;
189 pr_debug("%s: CHS=%u/%u/%u\n", drive->name, cyl, head, sect);
191 tf->nsect = nsectors & 0xff;
192 tf->lbal = sect;
193 tf->lbam = cyl;
194 tf->lbah = cyl >> 8;
195 tf->device = head;
198 if (rq_data_dir(rq))
199 task.tf_flags |= IDE_TFLAG_WRITE;
201 ide_tf_set_cmd(drive, &task, dma);
202 if (!dma)
203 hwif->data_phase = task.data_phase;
204 task.rq = rq;
206 rc = do_rw_taskfile(drive, &task);
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);
217 return rc;
221 * 268435455 == 137439 MB or 28bit limit
222 * 320173056 == 163929 MB or 48bit addressing
223 * 1073741822 == 549756 MB or 48bit addressing fake drive
226 static ide_startstop_t ide_do_rw_disk(ide_drive_t *drive, struct request *rq,
227 sector_t block)
229 ide_hwif_t *hwif = HWIF(drive);
231 BUG_ON(drive->dev_flags & IDE_DFLAG_BLOCKED);
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;
239 ledtrig_ide_activity();
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);
246 if (hwif->rw_disk)
247 hwif->rw_disk(drive, rq);
249 return __ide_do_rw_disk(drive, rq, block);
253 * Queries for true maximum capacity of the drive.
254 * Returns maximum LBA address (> 0) of the drive, 0 if failed.
256 static u64 idedisk_read_native_max_address(ide_drive_t *drive, int lba48)
258 ide_task_t args;
259 struct ide_taskfile *tf = &args.tf;
260 u64 addr = 0;
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);
275 /* if OK, compute maximum address value */
276 if ((tf->status & 0x01) == 0)
277 addr = ide_get_lba_addr(tf, lba48) + 1;
279 return addr;
283 * Sets maximum virtual LBA address of the drive.
284 * Returns new maximum virtual LBA address (> 0) or 0 on failure.
286 static u64 idedisk_set_max_address(ide_drive_t *drive, u64 addr_req, int lba48)
288 ide_task_t args;
289 struct ide_taskfile *tf = &args.tf;
290 u64 addr_set = 0;
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;
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;
317 return addr_set;
320 static unsigned long long sectors_to_MB(unsigned long long n)
322 n <<= 9; /* make it bytes */
323 do_div(n, 1000000); /* make it MB */
324 return n;
328 * Some disks report total number of sectors instead of
329 * maximum sector address. We list them here.
331 static const struct drive_list_entry hpa_list[] = {
332 { "ST340823A", NULL },
333 { "ST320413A", NULL },
334 { "ST310211A", NULL },
335 { NULL, NULL }
338 static void idedisk_check_hpa(ide_drive_t *drive)
340 unsigned long long capacity, set_max;
341 int lba48 = ata_id_lba48_enabled(drive->id);
343 capacity = drive->capacity64;
345 set_max = idedisk_read_native_max_address(drive, lba48);
347 if (ide_in_drive_list(drive->id, hpa_list)) {
349 * Since we are inclusive wrt to firmware revisions do this
350 * extra check and apply the workaround only when needed.
352 if (set_max == capacity + 1)
353 set_max--;
356 if (set_max <= capacity)
357 return;
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));
366 set_max = idedisk_set_max_address(drive, set_max, lba48);
368 if (set_max) {
369 drive->capacity64 = set_max;
370 printk(KERN_INFO "%s: Host Protected Area disabled.\n",
371 drive->name);
375 static void init_idedisk_capacity(ide_drive_t *drive)
377 u16 *id = drive->id;
378 int lba;
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;
394 if (lba) {
395 drive->dev_flags |= IDE_DFLAG_LBA;
398 * If this device supports the Host Protected Area feature set,
399 * then we may need to change our opinion about its capacity.
401 if (ata_id_hpa_enabled(id))
402 idedisk_check_hpa(drive);
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;
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;
426 sector_t ide_disk_capacity(ide_drive_t *drive)
428 return drive->capacity64;
431 static void idedisk_prepare_flush(struct request_queue *q, struct request *rq)
433 ide_drive_t *drive = q->queuedata;
434 ide_task_t *task = kmalloc(sizeof(*task), GFP_ATOMIC);
436 /* FIXME: map struct ide_taskfile on rq->cmd[] */
437 BUG_ON(task == NULL);
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;
449 rq->cmd_type = REQ_TYPE_ATA_TASKFILE;
450 rq->cmd_flags |= REQ_SOFTBARRIER;
451 rq->special = task;
454 ide_devset_get(multcount, mult_count);
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.
460 static int set_multcount(ide_drive_t *drive, int arg)
462 struct request *rq;
463 int error;
465 if (arg < 0 || arg > (drive->id[ATA_ID_MAX_MULTSECT] & 0xff))
466 return -EINVAL;
468 if (drive->special.b.set_multmode)
469 return -EBUSY;
471 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
472 rq->cmd_type = REQ_TYPE_ATA_TASKFILE;
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);
479 return (drive->mult_count == arg) ? 0 : -EIO;
482 ide_devset_get_flag(nowerr, IDE_DFLAG_NOWERR);
484 static int set_nowerr(ide_drive_t *drive, int arg)
486 if (arg < 0 || arg > 1)
487 return -EINVAL;
489 if (arg)
490 drive->dev_flags |= IDE_DFLAG_NOWERR;
491 else
492 drive->dev_flags &= ~IDE_DFLAG_NOWERR;
494 drive->bad_wstat = arg ? BAD_R_STAT : BAD_W_STAT;
496 return 0;
499 static int ide_do_setfeature(ide_drive_t *drive, u8 feature, u8 nsect)
501 ide_task_t task;
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;
509 return ide_no_data_taskfile(drive, &task);
512 static void update_ordered(ide_drive_t *drive)
514 u16 *id = drive->id;
515 unsigned ordered = QUEUE_ORDERED_NONE;
516 prepare_flush_fn *prep_fn = NULL;
518 if (drive->dev_flags & IDE_DFLAG_WCACHE) {
519 unsigned long long capacity;
520 int barrier;
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.
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));
536 printk(KERN_INFO "%s: cache flushes %ssupported\n",
537 drive->name, barrier ? "" : "not ");
539 if (barrier) {
540 ordered = QUEUE_ORDERED_DRAIN_FLUSH;
541 prep_fn = idedisk_prepare_flush;
543 } else
544 ordered = QUEUE_ORDERED_DRAIN;
546 blk_queue_ordered(drive->queue, ordered, prep_fn);
549 ide_devset_get_flag(wcache, IDE_DFLAG_WCACHE);
551 static int set_wcache(ide_drive_t *drive, int arg)
553 int err = 1;
555 if (arg < 0 || arg > 1)
556 return -EINVAL;
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;
569 update_ordered(drive);
571 return err;
574 static int do_idedisk_flushcache(ide_drive_t *drive)
576 ide_task_t args;
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);
587 ide_devset_get(acoustic, acoustic);
589 static int set_acoustic(ide_drive_t *drive, int arg)
591 if (arg < 0 || arg > 254)
592 return -EINVAL;
594 ide_do_setfeature(drive,
595 arg ? SETFEATURES_AAM_ON : SETFEATURES_AAM_OFF, arg);
597 drive->acoustic = arg;
599 return 0;
602 ide_devset_get_flag(addressing, IDE_DFLAG_LBA48);
605 * drive->addressing:
606 * 0: 28-bit
607 * 1: 48-bit
608 * 2: 48-bit capable doing 28-bit
610 static int set_addressing(ide_drive_t *drive, int arg)
612 if (arg < 0 || arg > 2)
613 return -EINVAL;
615 if (arg && ((drive->hwif->host_flags & IDE_HFLAG_NO_LBA48) ||
616 ata_id_lba48_enabled(drive->id) == 0))
617 return -EIO;
619 if (arg == 2)
620 arg = 0;
622 if (arg)
623 drive->dev_flags |= IDE_DFLAG_LBA48;
624 else
625 drive->dev_flags &= ~IDE_DFLAG_LBA48;
627 return 0;
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);
635 ide_ext_devset_rw_sync(nowerr, nowerr);
637 static void idedisk_setup(ide_drive_t *drive)
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;
645 ide_proc_register_driver(drive, idkp->driver);
647 if ((drive->dev_flags & IDE_DFLAG_ID_READ) == 0)
648 return;
650 if (drive->dev_flags & IDE_DFLAG_REMOVABLE) {
652 * Removable disks (eg. SYQUEST); ignore 'WD' drives
654 if (m[0] != 'W' || m[1] != 'D')
655 drive->dev_flags |= IDE_DFLAG_DOORLOCKING;
658 (void)set_addressing(drive, 1);
660 if (drive->dev_flags & IDE_DFLAG_LBA48) {
661 int max_s = 2048;
663 if (max_s > hwif->rqsize)
664 max_s = hwif->rqsize;
666 blk_queue_max_sectors(drive->queue, max_s);
669 printk(KERN_INFO "%s: max request size: %dKiB\n", drive->name,
670 drive->queue->max_sectors / 2);
672 /* calculate drive capacity, and select LBA if possible */
673 init_idedisk_capacity(drive);
676 * if possible, give fdisk access to more of the drive,
677 * by correcting bios_cyls:
679 capacity = ide_disk_capacity(drive);
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;
688 if (drive->bios_sect && drive->bios_head) {
689 unsigned int cap0 = capacity; /* truncate to 32 bits */
690 unsigned int cylsz, cyl;
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;
704 printk(KERN_INFO "%s: %llu sectors (%llu MB)",
705 drive->name, capacity, sectors_to_MB(capacity));
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);
711 printk(KERN_CONT ", CHS=%d/%d/%d\n",
712 drive->bios_cyl, drive->bios_head, drive->bios_sect);
714 /* write cache enabled? */
715 if ((id[ATA_ID_CSFO] & 1) || ata_id_wcache_enabled(id))
716 drive->dev_flags |= IDE_DFLAG_WCACHE;
718 set_wcache(drive, 1);
721 static void ide_cacheflush_p(ide_drive_t *drive)
723 if (ata_id_flush_enabled(drive->id) == 0 ||
724 (drive->dev_flags & IDE_DFLAG_WCACHE) == 0)
725 return;
727 if (do_idedisk_flushcache(drive))
728 printk(KERN_INFO "%s: wcache flush failed!\n", drive->name);
731 static void ide_disk_remove(ide_drive_t *drive)
733 struct ide_disk_obj *idkp = drive->driver_data;
734 struct gendisk *g = idkp->disk;
736 ide_proc_unregister_driver(drive, idkp->driver);
738 del_gendisk(g);
740 ide_cacheflush_p(drive);
742 ide_disk_put(idkp);
745 static void ide_disk_release(struct kref *kref)
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;
751 drive->driver_data = NULL;
752 g->private_data = NULL;
753 put_disk(g);
754 kfree(idkp);
757 static int ide_disk_probe(ide_drive_t *drive);
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
764 static void ide_disk_resume(ide_drive_t *drive)
766 if (ata_id_hpa_enabled(drive->id))
767 init_idedisk_capacity(drive);
770 static void ide_device_shutdown(ide_drive_t *drive)
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;
792 printk(KERN_INFO "Shutdown: %s\n", drive->name);
794 drive->gendev.bus->suspend(&drive->gendev, PMSG_SUSPEND);
797 static ide_driver_t idedisk_driver = {
798 .gen_driver = {
799 .owner = THIS_MODULE,
800 .name = "ide-disk",
801 .bus = &ide_bus_type,
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
817 static int idedisk_set_doorlock(ide_drive_t *drive, int on)
819 ide_task_t task;
820 int ret;
822 if ((drive->dev_flags & IDE_DFLAG_DOORLOCKING) == 0)
823 return 0;
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;
829 ret = ide_no_data_taskfile(drive, &task);
831 if (ret)
832 drive->dev_flags &= ~IDE_DFLAG_DOORLOCKING;
834 return ret;
837 static int idedisk_open(struct inode *inode, struct file *filp)
839 struct gendisk *disk = inode->i_bdev->bd_disk;
840 struct ide_disk_obj *idkp;
841 ide_drive_t *drive;
843 idkp = ide_disk_get(disk);
844 if (idkp == NULL)
845 return -ENXIO;
847 drive = idkp->drive;
849 idkp->openers++;
851 if ((drive->dev_flags & IDE_DFLAG_REMOVABLE) && idkp->openers == 1) {
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.
857 idedisk_set_doorlock(drive, 1);
858 check_disk_change(inode->i_bdev);
860 return 0;
863 static int idedisk_release(struct inode *inode, struct file *filp)
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;
869 if (idkp->openers == 1)
870 ide_cacheflush_p(drive);
872 if ((drive->dev_flags & IDE_DFLAG_REMOVABLE) && idkp->openers == 1)
873 idedisk_set_doorlock(drive, 0);
875 idkp->openers--;
877 ide_disk_put(idkp);
879 return 0;
882 static int idedisk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
884 struct ide_disk_obj *idkp = ide_drv_g(bdev->bd_disk, ide_disk_obj);
885 ide_drive_t *drive = idkp->drive;
887 geo->heads = drive->bios_head;
888 geo->sectors = drive->bios_sect;
889 geo->cylinders = (u16)drive->bios_cyl; /* truncate */
890 return 0;
893 static int idedisk_media_changed(struct gendisk *disk)
895 struct ide_disk_obj *idkp = ide_drv_g(disk, ide_disk_obj);
896 ide_drive_t *drive = idkp->drive;
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;
904 /* if removable, always assume it was changed */
905 return !!(drive->dev_flags & IDE_DFLAG_REMOVABLE);
908 static int idedisk_revalidate_disk(struct gendisk *disk)
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;
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
925 MODULE_DESCRIPTION("ATA DISK Driver");
927 static int ide_disk_probe(ide_drive_t *drive)
929 struct ide_disk_obj *idkp;
930 struct gendisk *g;
932 /* strstr("foo", "") is non-NULL */
933 if (!strstr("ide-disk", drive->driver_req))
934 goto failed;
936 if (drive->media != ide_disk)
937 goto failed;
939 idkp = kzalloc(sizeof(*idkp), GFP_KERNEL);
940 if (!idkp)
941 goto failed;
943 g = alloc_disk_node(IDE_DISK_MINORS, hwif_to_node(drive->hwif));
944 if (!g)
945 goto out_free_idkp;
947 ide_init_disk(g, drive);
949 kref_init(&idkp->kref);
951 idkp->drive = drive;
952 idkp->driver = &idedisk_driver;
953 idkp->disk = g;
955 g->private_data = &idkp->driver;
957 drive->driver_data = idkp;
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;
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;
978 out_free_idkp:
979 kfree(idkp);
980 failed:
981 return -ENODEV;
984 static void __exit idedisk_exit(void)
986 driver_unregister(&idedisk_driver.gen_driver);
989 static int __init idedisk_init(void)
991 return driver_register(&idedisk_driver.gen_driver);
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");