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
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>
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>
38 #include <asm/uaccess.h>
40 #include <asm/div64.h>
42 #if !defined(CONFIG_DEBUG_BLOCK_EXT_DEVT)
43 #define IDE_DISK_MINORS (1 << PARTN_BITS)
45 #define IDE_DISK_MINORS 0
50 static DEFINE_MUTEX(idedisk_ref_mutex);
52 #define to_ide_disk(obj) container_of(obj, struct ide_disk_obj, kref)
54 static void ide_disk_release(struct kref *);
56 static struct ide_disk_obj *ide_disk_get(struct gendisk *disk)
58 struct ide_disk_obj *idkp = NULL;
60 mutex_lock(&idedisk_ref_mutex);
61 idkp = ide_disk_g(disk);
63 if (ide_device_get(idkp->drive))
66 kref_get(&idkp->kref);
68 mutex_unlock(&idedisk_ref_mutex);
72 static void ide_disk_put(struct ide_disk_obj *idkp)
74 ide_drive_t *drive = idkp->drive;
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);
82 static const u8 ide_rw_cmds[] = {
85 ATA_CMD_READ_MULTI_EXT,
86 ATA_CMD_WRITE_MULTI_EXT,
90 ATA_CMD_PIO_WRITE_EXT,
97 static const u8 ide_data_phases[] = {
106 static void ide_tf_set_cmd(ide_drive_t *drive, ide_task_t *task, u8 dma)
108 u8 index, lba48, write;
110 lba48 = (task->tf_flags & IDE_TFLAG_LBA48) ? 2 : 0;
111 write = (task->tf_flags & IDE_TFLAG_WRITE) ? 1 : 0;
116 index = drive->mult_count ? 0 : 4;
118 task->tf.command = ide_rw_cmds[index + lba48 + write];
121 index = 8; /* fixup index */
123 task->data_phase = ide_data_phases[index / 2 + write];
127 * __ide_do_rw_disk() issues READ and WRITE commands to a disk,
128 * using LBA if supported, or CHS otherwise, to address sectors.
130 static ide_startstop_t __ide_do_rw_disk(ide_drive_t *drive, struct request *rq,
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);
138 struct ide_taskfile *tf = &task.tf;
141 if ((hwif->host_flags & IDE_HFLAG_NO_LBA48_DMA) && lba48 && dma) {
142 if (block + rq->nr_sectors > 1ULL << 28)
149 ide_init_sg_cmd(drive, rq);
150 ide_map_sg(drive, rq);
153 memset(&task, 0, sizeof(task));
154 task.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
156 if (drive->dev_flags & IDE_DFLAG_LBA) {
158 pr_debug("%s: LBA=0x%012llx\n", drive->name,
159 (unsigned long long)block);
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);
168 tf->nsect = nsectors & 0xff;
169 tf->lbal = (u8) block;
170 tf->lbam = (u8)(block >> 8);
171 tf->lbah = (u8)(block >> 16);
173 task.tf_flags |= (IDE_TFLAG_LBA48 | IDE_TFLAG_HOB);
175 tf->nsect = nsectors & 0xff;
177 tf->lbam = block >>= 8;
178 tf->lbah = block >>= 8;
179 tf->device = (block >> 8) & 0xf;
182 tf->device |= ATA_LBA;
184 unsigned int sect, head, cyl, track;
186 track = (int)block / drive->sect;
187 sect = (int)block % drive->sect + 1;
188 head = track % drive->head;
189 cyl = track / drive->head;
191 pr_debug("%s: CHS=%u/%u/%u\n", drive->name, cyl, head, sect);
193 tf->nsect = nsectors & 0xff;
201 task.tf_flags |= IDE_TFLAG_WRITE;
203 ide_tf_set_cmd(drive, &task, dma);
205 hwif->data_phase = task.data_phase;
208 rc = do_rw_taskfile(drive, &task);
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);
223 * 268435455 == 137439 MB or 28bit limit
224 * 320173056 == 163929 MB or 48bit addressing
225 * 1073741822 == 549756 MB or 48bit addressing fake drive
228 static ide_startstop_t ide_do_rw_disk(ide_drive_t *drive, struct request *rq,
231 ide_hwif_t *hwif = HWIF(drive);
233 BUG_ON(drive->dev_flags & IDE_DFLAG_BLOCKED);
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);
241 ledtrig_ide_activity();
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);
249 hwif->rw_disk(drive, rq);
251 return __ide_do_rw_disk(drive, rq, block);
255 * Queries for true maximum capacity of the drive.
256 * Returns maximum LBA address (> 0) of the drive, 0 if failed.
258 static u64 idedisk_read_native_max_address(ide_drive_t *drive, int lba48)
261 struct ide_taskfile *tf = &args.tf;
264 /* Create IDE/ATA command request structure */
265 memset(&args, 0, sizeof(ide_task_t));
267 tf->command = ATA_CMD_READ_NATIVE_MAX_EXT;
269 tf->command = ATA_CMD_READ_NATIVE_MAX;
270 tf->device = ATA_LBA;
271 args.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
273 args.tf_flags |= (IDE_TFLAG_LBA48 | IDE_TFLAG_HOB);
274 /* submit command request */
275 ide_no_data_taskfile(drive, &args);
277 /* if OK, compute maximum address value */
278 if ((tf->status & 0x01) == 0)
279 addr = ide_get_lba_addr(tf, lba48) + 1;
285 * Sets maximum virtual LBA address of the drive.
286 * Returns new maximum virtual LBA address (> 0) or 0 on failure.
288 static u64 idedisk_set_max_address(ide_drive_t *drive, u64 addr_req, int lba48)
291 struct ide_taskfile *tf = &args.tf;
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;
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;
306 tf->device = (addr_req >>= 8) & 0x0f;
307 tf->command = ATA_CMD_SET_MAX;
309 tf->device |= ATA_LBA;
310 args.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
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;
322 static unsigned long long sectors_to_MB(unsigned long long n)
324 n <<= 9; /* make it bytes */
325 do_div(n, 1000000); /* make it MB */
330 * Some disks report total number of sectors instead of
331 * maximum sector address. We list them here.
333 static const struct drive_list_entry hpa_list[] = {
334 { "ST340823A", NULL },
335 { "ST320413A", NULL },
336 { "ST310211A", NULL },
340 static void idedisk_check_hpa(ide_drive_t *drive)
342 unsigned long long capacity, set_max;
343 int lba48 = ata_id_lba48_enabled(drive->id);
345 capacity = drive->capacity64;
347 set_max = idedisk_read_native_max_address(drive, lba48);
349 if (ide_in_drive_list(drive->id, hpa_list)) {
351 * Since we are inclusive wrt to firmware revisions do this
352 * extra check and apply the workaround only when needed.
354 if (set_max == capacity + 1)
358 if (set_max <= capacity)
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",
365 capacity, sectors_to_MB(capacity),
366 set_max, sectors_to_MB(set_max));
368 set_max = idedisk_set_max_address(drive, set_max, lba48);
371 drive->capacity64 = set_max;
372 printk(KERN_INFO "%s: Host Protected Area disabled.\n",
377 static void init_idedisk_capacity(ide_drive_t *drive)
382 if (ata_id_lba48_enabled(id)) {
383 /* drive speaks 48-bit LBA */
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 */
389 drive->capacity64 = ata_id_u32(id, ATA_ID_LBA_CAPACITY);
391 /* drive speaks boring old 28-bit CHS */
393 drive->capacity64 = drive->cyl * drive->head * drive->sect;
397 drive->dev_flags |= IDE_DFLAG_LBA;
400 * If this device supports the Host Protected Area feature set,
401 * then we may need to change our opinion about its capacity.
403 if (ata_id_hpa_enabled(id))
404 idedisk_check_hpa(drive);
408 static sector_t idedisk_capacity(ide_drive_t *drive)
410 return drive->capacity64;
413 #ifdef CONFIG_IDE_PROC_FS
414 static int smart_enable(ide_drive_t *drive)
417 struct ide_taskfile *tf = &args.tf;
419 memset(&args, 0, sizeof(ide_task_t));
420 tf->feature = ATA_SMART_ENABLE;
421 tf->lbam = ATA_SMART_LBAM_PASS;
422 tf->lbah = ATA_SMART_LBAH_PASS;
423 tf->command = ATA_CMD_SMART;
424 args.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
425 return ide_no_data_taskfile(drive, &args);
428 static int get_smart_data(ide_drive_t *drive, u8 *buf, u8 sub_cmd)
431 struct ide_taskfile *tf = &args.tf;
433 memset(&args, 0, sizeof(ide_task_t));
434 tf->feature = sub_cmd;
436 tf->lbam = ATA_SMART_LBAM_PASS;
437 tf->lbah = ATA_SMART_LBAH_PASS;
438 tf->command = ATA_CMD_SMART;
439 args.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
440 args.data_phase = TASKFILE_IN;
441 (void) smart_enable(drive);
442 return ide_raw_taskfile(drive, &args, buf, 1);
445 static int proc_idedisk_read_cache
446 (char *page, char **start, off_t off, int count, int *eof, void *data)
448 ide_drive_t *drive = (ide_drive_t *) data;
452 if (drive->dev_flags & IDE_DFLAG_ID_READ)
453 len = sprintf(out, "%i\n", drive->id[ATA_ID_BUF_SIZE] / 2);
455 len = sprintf(out, "(none)\n");
457 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
460 static int proc_idedisk_read_capacity
461 (char *page, char **start, off_t off, int count, int *eof, void *data)
463 ide_drive_t*drive = (ide_drive_t *)data;
466 len = sprintf(page, "%llu\n", (long long)idedisk_capacity(drive));
468 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
471 static int proc_idedisk_read_smart(char *page, char **start, off_t off,
472 int count, int *eof, void *data, u8 sub_cmd)
474 ide_drive_t *drive = (ide_drive_t *)data;
477 if (get_smart_data(drive, page, sub_cmd) == 0) {
478 unsigned short *val = (unsigned short *) page;
479 char *out = (char *)val + SECTOR_SIZE;
483 out += sprintf(out, "%04x%c", le16_to_cpu(*val),
484 (++i & 7) ? ' ' : '\n');
486 } while (i < SECTOR_SIZE / 2);
490 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
493 static int proc_idedisk_read_sv
494 (char *page, char **start, off_t off, int count, int *eof, void *data)
496 return proc_idedisk_read_smart(page, start, off, count, eof, data,
497 ATA_SMART_READ_VALUES);
500 static int proc_idedisk_read_st
501 (char *page, char **start, off_t off, int count, int *eof, void *data)
503 return proc_idedisk_read_smart(page, start, off, count, eof, data,
504 ATA_SMART_READ_THRESHOLDS);
507 static ide_proc_entry_t idedisk_proc[] = {
508 { "cache", S_IFREG|S_IRUGO, proc_idedisk_read_cache, NULL },
509 { "capacity", S_IFREG|S_IRUGO, proc_idedisk_read_capacity, NULL },
510 { "geometry", S_IFREG|S_IRUGO, proc_ide_read_geometry, NULL },
511 { "smart_values", S_IFREG|S_IRUSR, proc_idedisk_read_sv, NULL },
512 { "smart_thresholds", S_IFREG|S_IRUSR, proc_idedisk_read_st, NULL },
513 { NULL, 0, NULL, NULL }
515 #endif /* CONFIG_IDE_PROC_FS */
517 static void idedisk_prepare_flush(struct request_queue *q, struct request *rq)
519 ide_drive_t *drive = q->queuedata;
520 ide_task_t *task = kmalloc(sizeof(*task), GFP_ATOMIC);
522 /* FIXME: map struct ide_taskfile on rq->cmd[] */
523 BUG_ON(task == NULL);
525 memset(task, 0, sizeof(*task));
526 if (ata_id_flush_ext_enabled(drive->id) &&
527 (drive->capacity64 >= (1UL << 28)))
528 task->tf.command = ATA_CMD_FLUSH_EXT;
530 task->tf.command = ATA_CMD_FLUSH;
531 task->tf_flags = IDE_TFLAG_OUT_TF | IDE_TFLAG_OUT_DEVICE |
533 task->data_phase = TASKFILE_NO_DATA;
535 rq->cmd_type = REQ_TYPE_ATA_TASKFILE;
536 rq->cmd_flags |= REQ_SOFTBARRIER;
540 ide_devset_get(multcount, mult_count);
543 * This is tightly woven into the driver->do_special can not touch.
544 * DON'T do it again until a total personality rewrite is committed.
546 static int set_multcount(ide_drive_t *drive, int arg)
551 if (arg < 0 || arg > (drive->id[ATA_ID_MAX_MULTSECT] & 0xff))
554 if (drive->special.b.set_multmode)
557 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
558 rq->cmd_type = REQ_TYPE_ATA_TASKFILE;
560 drive->mult_req = arg;
561 drive->special.b.set_multmode = 1;
562 error = blk_execute_rq(drive->queue, NULL, rq, 0);
565 return (drive->mult_count == arg) ? 0 : -EIO;
568 ide_devset_get_flag(nowerr, IDE_DFLAG_NOWERR);
570 static int set_nowerr(ide_drive_t *drive, int arg)
572 if (arg < 0 || arg > 1)
576 drive->dev_flags |= IDE_DFLAG_NOWERR;
578 drive->dev_flags &= ~IDE_DFLAG_NOWERR;
580 drive->bad_wstat = arg ? BAD_R_STAT : BAD_W_STAT;
585 static int ide_do_setfeature(ide_drive_t *drive, u8 feature, u8 nsect)
589 memset(&task, 0, sizeof(task));
590 task.tf.feature = feature;
591 task.tf.nsect = nsect;
592 task.tf.command = ATA_CMD_SET_FEATURES;
593 task.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
595 return ide_no_data_taskfile(drive, &task);
598 static void update_ordered(ide_drive_t *drive)
601 unsigned ordered = QUEUE_ORDERED_NONE;
602 prepare_flush_fn *prep_fn = NULL;
604 if (drive->dev_flags & IDE_DFLAG_WCACHE) {
605 unsigned long long capacity;
608 * We must avoid issuing commands a drive does not
609 * understand or we may crash it. We check flush cache
610 * is supported. We also check we have the LBA48 flush
611 * cache if the drive capacity is too large. By this
612 * time we have trimmed the drive capacity if LBA48 is
613 * not available so we don't need to recheck that.
615 capacity = idedisk_capacity(drive);
616 barrier = ata_id_flush_enabled(id) &&
617 (drive->dev_flags & IDE_DFLAG_NOFLUSH) == 0 &&
618 ((drive->dev_flags & IDE_DFLAG_LBA48) == 0 ||
619 capacity <= (1ULL << 28) ||
620 ata_id_flush_ext_enabled(id));
622 printk(KERN_INFO "%s: cache flushes %ssupported\n",
623 drive->name, barrier ? "" : "not ");
626 ordered = QUEUE_ORDERED_DRAIN_FLUSH;
627 prep_fn = idedisk_prepare_flush;
630 ordered = QUEUE_ORDERED_DRAIN;
632 blk_queue_ordered(drive->queue, ordered, prep_fn);
635 ide_devset_get_flag(wcache, IDE_DFLAG_WCACHE);
637 static int set_wcache(ide_drive_t *drive, int arg)
641 if (arg < 0 || arg > 1)
644 if (ata_id_flush_enabled(drive->id)) {
645 err = ide_do_setfeature(drive,
646 arg ? SETFEATURES_WC_ON : SETFEATURES_WC_OFF, 0);
649 drive->dev_flags |= IDE_DFLAG_WCACHE;
651 drive->dev_flags &= ~IDE_DFLAG_WCACHE;
655 update_ordered(drive);
660 static int do_idedisk_flushcache(ide_drive_t *drive)
664 memset(&args, 0, sizeof(ide_task_t));
665 if (ata_id_flush_ext_enabled(drive->id))
666 args.tf.command = ATA_CMD_FLUSH_EXT;
668 args.tf.command = ATA_CMD_FLUSH;
669 args.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
670 return ide_no_data_taskfile(drive, &args);
673 ide_devset_get(acoustic, acoustic);
675 static int set_acoustic(ide_drive_t *drive, int arg)
677 if (arg < 0 || arg > 254)
680 ide_do_setfeature(drive,
681 arg ? SETFEATURES_AAM_ON : SETFEATURES_AAM_OFF, arg);
683 drive->acoustic = arg;
688 ide_devset_get_flag(addressing, IDE_DFLAG_LBA48);
694 * 2: 48-bit capable doing 28-bit
696 static int set_addressing(ide_drive_t *drive, int arg)
698 if (arg < 0 || arg > 2)
701 if (arg && ((drive->hwif->host_flags & IDE_HFLAG_NO_LBA48) ||
702 ata_id_lba48_enabled(drive->id) == 0))
709 drive->dev_flags |= IDE_DFLAG_LBA48;
711 drive->dev_flags &= ~IDE_DFLAG_LBA48;
716 ide_ext_devset_rw(acoustic, acoustic);
717 ide_ext_devset_rw(address, addressing);
718 ide_ext_devset_rw(multcount, multcount);
719 ide_ext_devset_rw(wcache, wcache);
721 ide_ext_devset_rw_sync(nowerr, nowerr);
723 #ifdef CONFIG_IDE_PROC_FS
724 ide_devset_rw_field(bios_cyl, bios_cyl);
725 ide_devset_rw_field(bios_head, bios_head);
726 ide_devset_rw_field(bios_sect, bios_sect);
727 ide_devset_rw_field(failures, failures);
728 ide_devset_rw_field(lun, lun);
729 ide_devset_rw_field(max_failures, max_failures);
731 static const struct ide_proc_devset idedisk_settings[] = {
732 IDE_PROC_DEVSET(acoustic, 0, 254),
733 IDE_PROC_DEVSET(address, 0, 2),
734 IDE_PROC_DEVSET(bios_cyl, 0, 65535),
735 IDE_PROC_DEVSET(bios_head, 0, 255),
736 IDE_PROC_DEVSET(bios_sect, 0, 63),
737 IDE_PROC_DEVSET(failures, 0, 65535),
738 IDE_PROC_DEVSET(lun, 0, 7),
739 IDE_PROC_DEVSET(max_failures, 0, 65535),
740 IDE_PROC_DEVSET(multcount, 0, 16),
741 IDE_PROC_DEVSET(nowerr, 0, 1),
742 IDE_PROC_DEVSET(wcache, 0, 1),
747 static void idedisk_setup(ide_drive_t *drive)
749 struct ide_disk_obj *idkp = drive->driver_data;
750 ide_hwif_t *hwif = drive->hwif;
752 char *m = (char *)&id[ATA_ID_PROD];
753 unsigned long long capacity;
755 ide_proc_register_driver(drive, idkp->driver);
757 if ((drive->dev_flags & IDE_DFLAG_ID_READ) == 0)
760 if (drive->dev_flags & IDE_DFLAG_REMOVABLE) {
762 * Removable disks (eg. SYQUEST); ignore 'WD' drives
764 if (m[0] != 'W' || m[1] != 'D')
765 drive->dev_flags |= IDE_DFLAG_DOORLOCKING;
768 (void)set_addressing(drive, 1);
770 if (drive->dev_flags & IDE_DFLAG_LBA48) {
773 if (max_s > hwif->rqsize)
774 max_s = hwif->rqsize;
776 blk_queue_max_sectors(drive->queue, max_s);
779 printk(KERN_INFO "%s: max request size: %dKiB\n", drive->name,
780 drive->queue->max_sectors / 2);
782 /* calculate drive capacity, and select LBA if possible */
783 init_idedisk_capacity(drive);
785 /* limit drive capacity to 137GB if LBA48 cannot be used */
786 if ((drive->dev_flags & IDE_DFLAG_LBA48) == 0 &&
787 drive->capacity64 > 1ULL << 28) {
788 printk(KERN_WARNING "%s: cannot use LBA48 - full capacity "
789 "%llu sectors (%llu MB)\n",
790 drive->name, (unsigned long long)drive->capacity64,
791 sectors_to_MB(drive->capacity64));
792 drive->capacity64 = 1ULL << 28;
795 if ((hwif->host_flags & IDE_HFLAG_NO_LBA48_DMA) &&
796 (drive->dev_flags & IDE_DFLAG_LBA48)) {
797 if (drive->capacity64 > 1ULL << 28) {
798 printk(KERN_INFO "%s: cannot use LBA48 DMA - PIO mode"
799 " will be used for accessing sectors "
800 "> %u\n", drive->name, 1 << 28);
802 drive->dev_flags &= ~IDE_DFLAG_LBA48;
806 * if possible, give fdisk access to more of the drive,
807 * by correcting bios_cyls:
809 capacity = idedisk_capacity(drive);
811 if ((drive->dev_flags & IDE_DFLAG_FORCED_GEOM) == 0) {
812 if (ata_id_lba48_enabled(drive->id)) {
814 drive->bios_sect = 63;
815 drive->bios_head = 255;
818 if (drive->bios_sect && drive->bios_head) {
819 unsigned int cap0 = capacity; /* truncate to 32 bits */
820 unsigned int cylsz, cyl;
822 if (cap0 != capacity)
823 drive->bios_cyl = 65535;
825 cylsz = drive->bios_sect * drive->bios_head;
829 if (cyl > drive->bios_cyl)
830 drive->bios_cyl = cyl;
834 printk(KERN_INFO "%s: %llu sectors (%llu MB)",
835 drive->name, capacity, sectors_to_MB(capacity));
837 /* Only print cache size when it was specified */
838 if (id[ATA_ID_BUF_SIZE])
839 printk(KERN_CONT " w/%dKiB Cache", id[ATA_ID_BUF_SIZE] / 2);
841 printk(KERN_CONT ", CHS=%d/%d/%d\n",
842 drive->bios_cyl, drive->bios_head, drive->bios_sect);
844 /* write cache enabled? */
845 if ((id[ATA_ID_CSFO] & 1) || ata_id_wcache_enabled(id))
846 drive->dev_flags |= IDE_DFLAG_WCACHE;
848 set_wcache(drive, 1);
851 static void ide_cacheflush_p(ide_drive_t *drive)
853 if (ata_id_flush_enabled(drive->id) == 0 ||
854 (drive->dev_flags & IDE_DFLAG_WCACHE) == 0)
857 if (do_idedisk_flushcache(drive))
858 printk(KERN_INFO "%s: wcache flush failed!\n", drive->name);
861 static void ide_disk_remove(ide_drive_t *drive)
863 struct ide_disk_obj *idkp = drive->driver_data;
864 struct gendisk *g = idkp->disk;
866 ide_proc_unregister_driver(drive, idkp->driver);
870 ide_cacheflush_p(drive);
875 static void ide_disk_release(struct kref *kref)
877 struct ide_disk_obj *idkp = to_ide_disk(kref);
878 ide_drive_t *drive = idkp->drive;
879 struct gendisk *g = idkp->disk;
881 drive->driver_data = NULL;
882 g->private_data = NULL;
887 static int ide_disk_probe(ide_drive_t *drive);
890 * On HPA drives the capacity needs to be
891 * reinitilized on resume otherwise the disk
892 * can not be used and a hard reset is required
894 static void ide_disk_resume(ide_drive_t *drive)
896 if (ata_id_hpa_enabled(drive->id))
897 init_idedisk_capacity(drive);
900 static void ide_device_shutdown(ide_drive_t *drive)
903 /* On Alpha, halt(8) doesn't actually turn the machine off,
904 it puts you into the sort of firmware monitor. Typically,
905 it's used to boot another kernel image, so it's not much
906 different from reboot(8). Therefore, we don't need to
907 spin down the disk in this case, especially since Alpha
908 firmware doesn't handle disks in standby mode properly.
909 On the other hand, it's reasonably safe to turn the power
910 off when the shutdown process reaches the firmware prompt,
911 as the firmware initialization takes rather long time -
912 at least 10 seconds, which should be sufficient for
913 the disk to expire its write cache. */
914 if (system_state != SYSTEM_POWER_OFF) {
916 if (system_state == SYSTEM_RESTART) {
918 ide_cacheflush_p(drive);
922 printk(KERN_INFO "Shutdown: %s\n", drive->name);
924 drive->gendev.bus->suspend(&drive->gendev, PMSG_SUSPEND);
927 static ide_driver_t idedisk_driver = {
929 .owner = THIS_MODULE,
931 .bus = &ide_bus_type,
933 .probe = ide_disk_probe,
934 .remove = ide_disk_remove,
935 .resume = ide_disk_resume,
936 .shutdown = ide_device_shutdown,
937 .version = IDEDISK_VERSION,
938 .do_request = ide_do_rw_disk,
939 .end_request = ide_end_request,
940 .error = __ide_error,
941 #ifdef CONFIG_IDE_PROC_FS
942 .proc = idedisk_proc,
943 .settings = idedisk_settings,
947 static int idedisk_set_doorlock(ide_drive_t *drive, int on)
951 memset(&task, 0, sizeof(task));
952 task.tf.command = on ? ATA_CMD_MEDIA_LOCK : ATA_CMD_MEDIA_UNLOCK;
953 task.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
955 return ide_no_data_taskfile(drive, &task);
958 static int idedisk_open(struct inode *inode, struct file *filp)
960 struct gendisk *disk = inode->i_bdev->bd_disk;
961 struct ide_disk_obj *idkp;
964 idkp = ide_disk_get(disk);
972 if ((drive->dev_flags & IDE_DFLAG_REMOVABLE) && idkp->openers == 1) {
973 check_disk_change(inode->i_bdev);
975 * Ignore the return code from door_lock,
976 * since the open() has already succeeded,
977 * and the door_lock is irrelevant at this point.
979 if ((drive->dev_flags & IDE_DFLAG_DOORLOCKING) &&
980 idedisk_set_doorlock(drive, 1))
981 drive->dev_flags &= ~IDE_DFLAG_DOORLOCKING;
986 static int idedisk_release(struct inode *inode, struct file *filp)
988 struct gendisk *disk = inode->i_bdev->bd_disk;
989 struct ide_disk_obj *idkp = ide_disk_g(disk);
990 ide_drive_t *drive = idkp->drive;
992 if (idkp->openers == 1)
993 ide_cacheflush_p(drive);
995 if ((drive->dev_flags & IDE_DFLAG_REMOVABLE) && idkp->openers == 1) {
996 if ((drive->dev_flags & IDE_DFLAG_DOORLOCKING) &&
997 idedisk_set_doorlock(drive, 0))
998 drive->dev_flags &= ~IDE_DFLAG_DOORLOCKING;
1008 static int idedisk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1010 struct ide_disk_obj *idkp = ide_disk_g(bdev->bd_disk);
1011 ide_drive_t *drive = idkp->drive;
1013 geo->heads = drive->bios_head;
1014 geo->sectors = drive->bios_sect;
1015 geo->cylinders = (u16)drive->bios_cyl; /* truncate */
1019 static int idedisk_media_changed(struct gendisk *disk)
1021 struct ide_disk_obj *idkp = ide_disk_g(disk);
1022 ide_drive_t *drive = idkp->drive;
1024 /* do not scan partitions twice if this is a removable device */
1025 if (drive->dev_flags & IDE_DFLAG_ATTACH) {
1026 drive->dev_flags &= ~IDE_DFLAG_ATTACH;
1030 /* if removable, always assume it was changed */
1031 return !!(drive->dev_flags & IDE_DFLAG_REMOVABLE);
1034 static int idedisk_revalidate_disk(struct gendisk *disk)
1036 struct ide_disk_obj *idkp = ide_disk_g(disk);
1037 set_capacity(disk, idedisk_capacity(idkp->drive));
1041 static struct block_device_operations idedisk_ops = {
1042 .owner = THIS_MODULE,
1043 .open = idedisk_open,
1044 .release = idedisk_release,
1045 .ioctl = ide_disk_ioctl,
1046 .getgeo = idedisk_getgeo,
1047 .media_changed = idedisk_media_changed,
1048 .revalidate_disk = idedisk_revalidate_disk
1051 MODULE_DESCRIPTION("ATA DISK Driver");
1053 static int ide_disk_probe(ide_drive_t *drive)
1055 struct ide_disk_obj *idkp;
1058 /* strstr("foo", "") is non-NULL */
1059 if (!strstr("ide-disk", drive->driver_req))
1062 if (drive->media != ide_disk)
1065 idkp = kzalloc(sizeof(*idkp), GFP_KERNEL);
1069 g = alloc_disk_node(IDE_DISK_MINORS, hwif_to_node(drive->hwif));
1073 ide_init_disk(g, drive);
1075 kref_init(&idkp->kref);
1077 idkp->drive = drive;
1078 idkp->driver = &idedisk_driver;
1081 g->private_data = &idkp->driver;
1083 drive->driver_data = idkp;
1085 idedisk_setup(drive);
1086 if ((drive->dev_flags & IDE_DFLAG_LBA) == 0 &&
1087 (drive->head == 0 || drive->head > 16)) {
1088 printk(KERN_ERR "%s: INVALID GEOMETRY: %d PHYSICAL HEADS?\n",
1089 drive->name, drive->head);
1090 drive->dev_flags &= ~IDE_DFLAG_ATTACH;
1092 drive->dev_flags |= IDE_DFLAG_ATTACH;
1094 g->minors = IDE_DISK_MINORS;
1095 g->driverfs_dev = &drive->gendev;
1096 g->flags |= GENHD_FL_EXT_DEVT;
1097 if (drive->dev_flags & IDE_DFLAG_REMOVABLE)
1098 g->flags = GENHD_FL_REMOVABLE;
1099 set_capacity(g, idedisk_capacity(drive));
1100 g->fops = &idedisk_ops;
1110 static void __exit idedisk_exit(void)
1112 driver_unregister(&idedisk_driver.gen_driver);
1115 static int __init idedisk_init(void)
1117 return driver_register(&idedisk_driver.gen_driver);
1120 MODULE_ALIAS("ide:*m-disk*");
1121 MODULE_ALIAS("ide-disk");
1122 module_init(idedisk_init);
1123 module_exit(idedisk_exit);
1124 MODULE_LICENSE("GPL");