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
53 unsigned int openers; /* protected by BKL for now */
56 static DEFINE_MUTEX(idedisk_ref_mutex);
58 #define to_ide_disk(obj) container_of(obj, struct ide_disk_obj, kref)
60 #define ide_disk_g(disk) \
61 container_of((disk)->private_data, struct ide_disk_obj, driver)
63 static void ide_disk_release(struct kref *);
65 static struct ide_disk_obj *ide_disk_get(struct gendisk *disk)
67 struct ide_disk_obj *idkp = NULL;
69 mutex_lock(&idedisk_ref_mutex);
70 idkp = ide_disk_g(disk);
72 if (ide_device_get(idkp->drive))
75 kref_get(&idkp->kref);
77 mutex_unlock(&idedisk_ref_mutex);
81 static void ide_disk_put(struct ide_disk_obj *idkp)
83 ide_drive_t *drive = idkp->drive;
85 mutex_lock(&idedisk_ref_mutex);
86 kref_put(&idkp->kref, ide_disk_release);
87 ide_device_put(drive);
88 mutex_unlock(&idedisk_ref_mutex);
91 static const u8 ide_rw_cmds[] = {
94 ATA_CMD_READ_MULTI_EXT,
95 ATA_CMD_WRITE_MULTI_EXT,
99 ATA_CMD_PIO_WRITE_EXT,
106 static const u8 ide_data_phases[] = {
115 static void ide_tf_set_cmd(ide_drive_t *drive, ide_task_t *task, u8 dma)
117 u8 index, lba48, write;
119 lba48 = (task->tf_flags & IDE_TFLAG_LBA48) ? 2 : 0;
120 write = (task->tf_flags & IDE_TFLAG_WRITE) ? 1 : 0;
125 index = drive->mult_count ? 0 : 4;
127 task->tf.command = ide_rw_cmds[index + lba48 + write];
130 index = 8; /* fixup index */
132 task->data_phase = ide_data_phases[index / 2 + write];
136 * __ide_do_rw_disk() issues READ and WRITE commands to a disk,
137 * using LBA if supported, or CHS otherwise, to address sectors.
139 static ide_startstop_t __ide_do_rw_disk(ide_drive_t *drive, struct request *rq,
142 ide_hwif_t *hwif = HWIF(drive);
143 u16 nsectors = (u16)rq->nr_sectors;
144 u8 lba48 = !!(drive->dev_flags & IDE_DFLAG_LBA48);
145 u8 dma = !!(drive->dev_flags & IDE_DFLAG_USING_DMA);
147 struct ide_taskfile *tf = &task.tf;
150 if ((hwif->host_flags & IDE_HFLAG_NO_LBA48_DMA) && lba48 && dma) {
151 if (block + rq->nr_sectors > 1ULL << 28)
158 ide_init_sg_cmd(drive, rq);
159 ide_map_sg(drive, rq);
162 memset(&task, 0, sizeof(task));
163 task.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
165 if (drive->select.b.lba) {
167 pr_debug("%s: LBA=0x%012llx\n", drive->name,
168 (unsigned long long)block);
170 tf->hob_nsect = (nsectors >> 8) & 0xff;
171 tf->hob_lbal = (u8)(block >> 24);
172 if (sizeof(block) != 4) {
173 tf->hob_lbam = (u8)((u64)block >> 32);
174 tf->hob_lbah = (u8)((u64)block >> 40);
177 tf->nsect = nsectors & 0xff;
178 tf->lbal = (u8) block;
179 tf->lbam = (u8)(block >> 8);
180 tf->lbah = (u8)(block >> 16);
182 task.tf_flags |= (IDE_TFLAG_LBA48 | IDE_TFLAG_HOB);
184 tf->nsect = nsectors & 0xff;
186 tf->lbam = block >>= 8;
187 tf->lbah = block >>= 8;
188 tf->device = (block >> 8) & 0xf;
191 unsigned int sect, head, cyl, track;
193 track = (int)block / drive->sect;
194 sect = (int)block % drive->sect + 1;
195 head = track % drive->head;
196 cyl = track / drive->head;
198 pr_debug("%s: CHS=%u/%u/%u\n", drive->name, cyl, head, sect);
200 tf->nsect = nsectors & 0xff;
208 task.tf_flags |= IDE_TFLAG_WRITE;
210 ide_tf_set_cmd(drive, &task, dma);
212 hwif->data_phase = task.data_phase;
215 rc = do_rw_taskfile(drive, &task);
217 if (rc == ide_stopped && dma) {
218 /* fallback to PIO */
219 task.tf_flags |= IDE_TFLAG_DMA_PIO_FALLBACK;
220 ide_tf_set_cmd(drive, &task, 0);
221 hwif->data_phase = task.data_phase;
222 ide_init_sg_cmd(drive, rq);
223 rc = do_rw_taskfile(drive, &task);
230 * 268435455 == 137439 MB or 28bit limit
231 * 320173056 == 163929 MB or 48bit addressing
232 * 1073741822 == 549756 MB or 48bit addressing fake drive
235 static ide_startstop_t ide_do_rw_disk(ide_drive_t *drive, struct request *rq,
238 ide_hwif_t *hwif = HWIF(drive);
240 BUG_ON(drive->dev_flags & IDE_DFLAG_BLOCKED);
242 if (!blk_fs_request(rq)) {
243 blk_dump_rq_flags(rq, "ide_do_rw_disk - bad command");
244 ide_end_request(drive, 0, 0);
248 ledtrig_ide_activity();
250 pr_debug("%s: %sing: block=%llu, sectors=%lu, buffer=0x%08lx\n",
251 drive->name, rq_data_dir(rq) == READ ? "read" : "writ",
252 (unsigned long long)block, rq->nr_sectors,
253 (unsigned long)rq->buffer);
256 hwif->rw_disk(drive, rq);
258 return __ide_do_rw_disk(drive, rq, block);
262 * Queries for true maximum capacity of the drive.
263 * Returns maximum LBA address (> 0) of the drive, 0 if failed.
265 static u64 idedisk_read_native_max_address(ide_drive_t *drive, int lba48)
268 struct ide_taskfile *tf = &args.tf;
271 /* Create IDE/ATA command request structure */
272 memset(&args, 0, sizeof(ide_task_t));
274 tf->command = ATA_CMD_READ_NATIVE_MAX_EXT;
276 tf->command = ATA_CMD_READ_NATIVE_MAX;
277 tf->device = ATA_LBA;
278 args.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
280 args.tf_flags |= (IDE_TFLAG_LBA48 | IDE_TFLAG_HOB);
281 /* submit command request */
282 ide_no_data_taskfile(drive, &args);
284 /* if OK, compute maximum address value */
285 if ((tf->status & 0x01) == 0)
286 addr = ide_get_lba_addr(tf, lba48) + 1;
292 * Sets maximum virtual LBA address of the drive.
293 * Returns new maximum virtual LBA address (> 0) or 0 on failure.
295 static u64 idedisk_set_max_address(ide_drive_t *drive, u64 addr_req, int lba48)
298 struct ide_taskfile *tf = &args.tf;
302 /* Create IDE/ATA command request structure */
303 memset(&args, 0, sizeof(ide_task_t));
304 tf->lbal = (addr_req >> 0) & 0xff;
305 tf->lbam = (addr_req >>= 8) & 0xff;
306 tf->lbah = (addr_req >>= 8) & 0xff;
308 tf->hob_lbal = (addr_req >>= 8) & 0xff;
309 tf->hob_lbam = (addr_req >>= 8) & 0xff;
310 tf->hob_lbah = (addr_req >>= 8) & 0xff;
311 tf->command = ATA_CMD_SET_MAX_EXT;
313 tf->device = (addr_req >>= 8) & 0x0f;
314 tf->command = ATA_CMD_SET_MAX;
316 tf->device |= ATA_LBA;
317 args.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
319 args.tf_flags |= (IDE_TFLAG_LBA48 | IDE_TFLAG_HOB);
320 /* submit command request */
321 ide_no_data_taskfile(drive, &args);
322 /* if OK, compute maximum address value */
323 if ((tf->status & 0x01) == 0)
324 addr_set = ide_get_lba_addr(tf, lba48) + 1;
329 static unsigned long long sectors_to_MB(unsigned long long n)
331 n <<= 9; /* make it bytes */
332 do_div(n, 1000000); /* make it MB */
337 * Some disks report total number of sectors instead of
338 * maximum sector address. We list them here.
340 static const struct drive_list_entry hpa_list[] = {
341 { "ST340823A", NULL },
342 { "ST320413A", NULL },
343 { "ST310211A", NULL },
347 static void idedisk_check_hpa(ide_drive_t *drive)
349 unsigned long long capacity, set_max;
350 int lba48 = ata_id_lba48_enabled(drive->id);
352 capacity = drive->capacity64;
354 set_max = idedisk_read_native_max_address(drive, lba48);
356 if (ide_in_drive_list(drive->id, hpa_list)) {
358 * Since we are inclusive wrt to firmware revisions do this
359 * extra check and apply the workaround only when needed.
361 if (set_max == capacity + 1)
365 if (set_max <= capacity)
368 printk(KERN_INFO "%s: Host Protected Area detected.\n"
369 "\tcurrent capacity is %llu sectors (%llu MB)\n"
370 "\tnative capacity is %llu sectors (%llu MB)\n",
372 capacity, sectors_to_MB(capacity),
373 set_max, sectors_to_MB(set_max));
375 set_max = idedisk_set_max_address(drive, set_max, lba48);
378 drive->capacity64 = set_max;
379 printk(KERN_INFO "%s: Host Protected Area disabled.\n",
384 static void init_idedisk_capacity(ide_drive_t *drive)
388 * If this drive supports the Host Protected Area feature set,
389 * then we may need to change our opinion about the drive's capacity.
391 int hpa = ata_id_hpa_enabled(id);
393 if (ata_id_lba48_enabled(id)) {
394 /* drive speaks 48-bit LBA */
395 drive->select.b.lba = 1;
396 drive->capacity64 = ata_id_u64(id, ATA_ID_LBA_CAPACITY_2);
398 idedisk_check_hpa(drive);
399 } else if (ata_id_has_lba(id) && ata_id_is_lba_capacity_ok(id)) {
400 /* drive speaks 28-bit LBA */
401 drive->select.b.lba = 1;
402 drive->capacity64 = ata_id_u32(id, ATA_ID_LBA_CAPACITY);
404 idedisk_check_hpa(drive);
406 /* drive speaks boring old 28-bit CHS */
407 drive->capacity64 = drive->cyl * drive->head * drive->sect;
411 static sector_t idedisk_capacity(ide_drive_t *drive)
413 return drive->capacity64;
416 #ifdef CONFIG_IDE_PROC_FS
417 static int smart_enable(ide_drive_t *drive)
420 struct ide_taskfile *tf = &args.tf;
422 memset(&args, 0, sizeof(ide_task_t));
423 tf->feature = ATA_SMART_ENABLE;
424 tf->lbam = ATA_SMART_LBAM_PASS;
425 tf->lbah = ATA_SMART_LBAH_PASS;
426 tf->command = ATA_CMD_SMART;
427 args.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
428 return ide_no_data_taskfile(drive, &args);
431 static int get_smart_data(ide_drive_t *drive, u8 *buf, u8 sub_cmd)
434 struct ide_taskfile *tf = &args.tf;
436 memset(&args, 0, sizeof(ide_task_t));
437 tf->feature = sub_cmd;
439 tf->lbam = ATA_SMART_LBAM_PASS;
440 tf->lbah = ATA_SMART_LBAH_PASS;
441 tf->command = ATA_CMD_SMART;
442 args.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
443 args.data_phase = TASKFILE_IN;
444 (void) smart_enable(drive);
445 return ide_raw_taskfile(drive, &args, buf, 1);
448 static int proc_idedisk_read_cache
449 (char *page, char **start, off_t off, int count, int *eof, void *data)
451 ide_drive_t *drive = (ide_drive_t *) data;
455 if (drive->dev_flags & IDE_DFLAG_ID_READ)
456 len = sprintf(out, "%i\n", drive->id[ATA_ID_BUF_SIZE] / 2);
458 len = sprintf(out, "(none)\n");
460 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
463 static int proc_idedisk_read_capacity
464 (char *page, char **start, off_t off, int count, int *eof, void *data)
466 ide_drive_t*drive = (ide_drive_t *)data;
469 len = sprintf(page, "%llu\n", (long long)idedisk_capacity(drive));
471 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
474 static int proc_idedisk_read_smart(char *page, char **start, off_t off,
475 int count, int *eof, void *data, u8 sub_cmd)
477 ide_drive_t *drive = (ide_drive_t *)data;
480 if (get_smart_data(drive, page, sub_cmd) == 0) {
481 unsigned short *val = (unsigned short *) page;
482 char *out = (char *)val + SECTOR_SIZE;
486 out += sprintf(out, "%04x%c", le16_to_cpu(*val),
487 (++i & 7) ? ' ' : '\n');
489 } while (i < SECTOR_SIZE / 2);
493 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
496 static int proc_idedisk_read_sv
497 (char *page, char **start, off_t off, int count, int *eof, void *data)
499 return proc_idedisk_read_smart(page, start, off, count, eof, data,
500 ATA_SMART_READ_VALUES);
503 static int proc_idedisk_read_st
504 (char *page, char **start, off_t off, int count, int *eof, void *data)
506 return proc_idedisk_read_smart(page, start, off, count, eof, data,
507 ATA_SMART_READ_THRESHOLDS);
510 static ide_proc_entry_t idedisk_proc[] = {
511 { "cache", S_IFREG|S_IRUGO, proc_idedisk_read_cache, NULL },
512 { "capacity", S_IFREG|S_IRUGO, proc_idedisk_read_capacity, NULL },
513 { "geometry", S_IFREG|S_IRUGO, proc_ide_read_geometry, NULL },
514 { "smart_values", S_IFREG|S_IRUSR, proc_idedisk_read_sv, NULL },
515 { "smart_thresholds", S_IFREG|S_IRUSR, proc_idedisk_read_st, NULL },
516 { NULL, 0, NULL, NULL }
518 #endif /* CONFIG_IDE_PROC_FS */
520 static void idedisk_prepare_flush(struct request_queue *q, struct request *rq)
522 ide_drive_t *drive = q->queuedata;
523 ide_task_t *task = kmalloc(sizeof(*task), GFP_ATOMIC);
525 /* FIXME: map struct ide_taskfile on rq->cmd[] */
526 BUG_ON(task == NULL);
528 memset(task, 0, sizeof(*task));
529 if (ata_id_flush_ext_enabled(drive->id) &&
530 (drive->capacity64 >= (1UL << 28)))
531 task->tf.command = ATA_CMD_FLUSH_EXT;
533 task->tf.command = ATA_CMD_FLUSH;
534 task->tf_flags = IDE_TFLAG_OUT_TF | IDE_TFLAG_OUT_DEVICE |
536 task->data_phase = TASKFILE_NO_DATA;
538 rq->cmd_type = REQ_TYPE_ATA_TASKFILE;
539 rq->cmd_flags |= REQ_SOFTBARRIER;
543 ide_devset_get(multcount, mult_count);
546 * This is tightly woven into the driver->do_special can not touch.
547 * DON'T do it again until a total personality rewrite is committed.
549 static int set_multcount(ide_drive_t *drive, int arg)
554 if (arg < 0 || arg > (drive->id[ATA_ID_MAX_MULTSECT] & 0xff))
557 if (drive->special.b.set_multmode)
560 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
561 rq->cmd_type = REQ_TYPE_ATA_TASKFILE;
563 drive->mult_req = arg;
564 drive->special.b.set_multmode = 1;
565 error = blk_execute_rq(drive->queue, NULL, rq, 0);
568 return (drive->mult_count == arg) ? 0 : -EIO;
571 ide_devset_get_flag(nowerr, IDE_DFLAG_NOWERR);
573 static int set_nowerr(ide_drive_t *drive, int arg)
575 if (arg < 0 || arg > 1)
579 drive->dev_flags |= IDE_DFLAG_NOWERR;
581 drive->dev_flags &= ~IDE_DFLAG_NOWERR;
583 drive->bad_wstat = arg ? BAD_R_STAT : BAD_W_STAT;
588 static int ide_do_setfeature(ide_drive_t *drive, u8 feature, u8 nsect)
592 memset(&task, 0, sizeof(task));
593 task.tf.feature = feature;
594 task.tf.nsect = nsect;
595 task.tf.command = ATA_CMD_SET_FEATURES;
596 task.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
598 return ide_no_data_taskfile(drive, &task);
601 static void update_ordered(ide_drive_t *drive)
604 unsigned ordered = QUEUE_ORDERED_NONE;
605 prepare_flush_fn *prep_fn = NULL;
607 if (drive->dev_flags & IDE_DFLAG_WCACHE) {
608 unsigned long long capacity;
611 * We must avoid issuing commands a drive does not
612 * understand or we may crash it. We check flush cache
613 * is supported. We also check we have the LBA48 flush
614 * cache if the drive capacity is too large. By this
615 * time we have trimmed the drive capacity if LBA48 is
616 * not available so we don't need to recheck that.
618 capacity = idedisk_capacity(drive);
619 barrier = ata_id_flush_enabled(id) &&
620 (drive->dev_flags & IDE_DFLAG_NOFLUSH) == 0 &&
621 ((drive->dev_flags & IDE_DFLAG_LBA48) == 0 ||
622 capacity <= (1ULL << 28) ||
623 ata_id_flush_ext_enabled(id));
625 printk(KERN_INFO "%s: cache flushes %ssupported\n",
626 drive->name, barrier ? "" : "not ");
629 ordered = QUEUE_ORDERED_DRAIN_FLUSH;
630 prep_fn = idedisk_prepare_flush;
633 ordered = QUEUE_ORDERED_DRAIN;
635 blk_queue_ordered(drive->queue, ordered, prep_fn);
638 ide_devset_get_flag(wcache, IDE_DFLAG_WCACHE);
640 static int set_wcache(ide_drive_t *drive, int arg)
644 if (arg < 0 || arg > 1)
647 if (ata_id_flush_enabled(drive->id)) {
648 err = ide_do_setfeature(drive,
649 arg ? SETFEATURES_WC_ON : SETFEATURES_WC_OFF, 0);
652 drive->dev_flags |= IDE_DFLAG_WCACHE;
654 drive->dev_flags &= ~IDE_DFLAG_WCACHE;
658 update_ordered(drive);
663 static int do_idedisk_flushcache(ide_drive_t *drive)
667 memset(&args, 0, sizeof(ide_task_t));
668 if (ata_id_flush_ext_enabled(drive->id))
669 args.tf.command = ATA_CMD_FLUSH_EXT;
671 args.tf.command = ATA_CMD_FLUSH;
672 args.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
673 return ide_no_data_taskfile(drive, &args);
676 ide_devset_get(acoustic, acoustic);
678 static int set_acoustic(ide_drive_t *drive, int arg)
680 if (arg < 0 || arg > 254)
683 ide_do_setfeature(drive,
684 arg ? SETFEATURES_AAM_ON : SETFEATURES_AAM_OFF, arg);
686 drive->acoustic = arg;
691 ide_devset_get_flag(addressing, IDE_DFLAG_LBA48);
697 * 2: 48-bit capable doing 28-bit
699 static int set_addressing(ide_drive_t *drive, int arg)
701 if (arg < 0 || arg > 2)
704 if (arg && ((drive->hwif->host_flags & IDE_HFLAG_NO_LBA48) ||
705 ata_id_lba48_enabled(drive->id) == 0))
712 drive->dev_flags |= IDE_DFLAG_LBA48;
714 drive->dev_flags &= ~IDE_DFLAG_LBA48;
719 ide_devset_rw(acoustic, acoustic);
720 ide_devset_rw(address, addressing);
721 ide_devset_rw(multcount, multcount);
722 ide_devset_rw(wcache, wcache);
724 ide_devset_rw_sync(nowerr, nowerr);
726 #ifdef CONFIG_IDE_PROC_FS
727 ide_devset_rw_field(bios_cyl, bios_cyl);
728 ide_devset_rw_field(bios_head, bios_head);
729 ide_devset_rw_field(bios_sect, bios_sect);
730 ide_devset_rw_field(failures, failures);
731 ide_devset_rw_field(lun, lun);
732 ide_devset_rw_field(max_failures, max_failures);
734 static const struct ide_proc_devset idedisk_settings[] = {
735 IDE_PROC_DEVSET(acoustic, 0, 254),
736 IDE_PROC_DEVSET(address, 0, 2),
737 IDE_PROC_DEVSET(bios_cyl, 0, 65535),
738 IDE_PROC_DEVSET(bios_head, 0, 255),
739 IDE_PROC_DEVSET(bios_sect, 0, 63),
740 IDE_PROC_DEVSET(failures, 0, 65535),
741 IDE_PROC_DEVSET(lun, 0, 7),
742 IDE_PROC_DEVSET(max_failures, 0, 65535),
743 IDE_PROC_DEVSET(multcount, 0, 16),
744 IDE_PROC_DEVSET(nowerr, 0, 1),
745 IDE_PROC_DEVSET(wcache, 0, 1),
750 static void idedisk_setup(ide_drive_t *drive)
752 struct ide_disk_obj *idkp = drive->driver_data;
753 ide_hwif_t *hwif = drive->hwif;
755 char *m = (char *)&id[ATA_ID_PROD];
756 unsigned long long capacity;
758 ide_proc_register_driver(drive, idkp->driver);
760 if ((drive->dev_flags & IDE_DFLAG_ID_READ) == 0)
763 if (drive->dev_flags & IDE_DFLAG_REMOVABLE) {
765 * Removable disks (eg. SYQUEST); ignore 'WD' drives
767 if (m[0] != 'W' || m[1] != 'D')
768 drive->dev_flags |= IDE_DFLAG_DOORLOCKING;
771 (void)set_addressing(drive, 1);
773 if (drive->dev_flags & IDE_DFLAG_LBA48) {
776 if (max_s > hwif->rqsize)
777 max_s = hwif->rqsize;
779 blk_queue_max_sectors(drive->queue, max_s);
782 printk(KERN_INFO "%s: max request size: %dKiB\n", drive->name,
783 drive->queue->max_sectors / 2);
785 /* calculate drive capacity, and select LBA if possible */
786 init_idedisk_capacity(drive);
788 /* limit drive capacity to 137GB if LBA48 cannot be used */
789 if ((drive->dev_flags & IDE_DFLAG_LBA48) == 0 &&
790 drive->capacity64 > 1ULL << 28) {
791 printk(KERN_WARNING "%s: cannot use LBA48 - full capacity "
792 "%llu sectors (%llu MB)\n",
793 drive->name, (unsigned long long)drive->capacity64,
794 sectors_to_MB(drive->capacity64));
795 drive->capacity64 = 1ULL << 28;
798 if ((hwif->host_flags & IDE_HFLAG_NO_LBA48_DMA) &&
799 (drive->dev_flags & IDE_DFLAG_LBA48)) {
800 if (drive->capacity64 > 1ULL << 28) {
801 printk(KERN_INFO "%s: cannot use LBA48 DMA - PIO mode"
802 " will be used for accessing sectors "
803 "> %u\n", drive->name, 1 << 28);
805 drive->dev_flags &= ~IDE_DFLAG_LBA48;
809 * if possible, give fdisk access to more of the drive,
810 * by correcting bios_cyls:
812 capacity = idedisk_capacity(drive);
814 if ((drive->dev_flags & IDE_DFLAG_FORCED_GEOM) == 0) {
815 if (ata_id_lba48_enabled(drive->id)) {
817 drive->bios_sect = 63;
818 drive->bios_head = 255;
821 if (drive->bios_sect && drive->bios_head) {
822 unsigned int cap0 = capacity; /* truncate to 32 bits */
823 unsigned int cylsz, cyl;
825 if (cap0 != capacity)
826 drive->bios_cyl = 65535;
828 cylsz = drive->bios_sect * drive->bios_head;
832 if (cyl > drive->bios_cyl)
833 drive->bios_cyl = cyl;
837 printk(KERN_INFO "%s: %llu sectors (%llu MB)",
838 drive->name, capacity, sectors_to_MB(capacity));
840 /* Only print cache size when it was specified */
841 if (id[ATA_ID_BUF_SIZE])
842 printk(KERN_CONT " w/%dKiB Cache", id[ATA_ID_BUF_SIZE] / 2);
844 printk(KERN_CONT ", CHS=%d/%d/%d\n",
845 drive->bios_cyl, drive->bios_head, drive->bios_sect);
847 /* write cache enabled? */
848 if ((id[ATA_ID_CSFO] & 1) || ata_id_wcache_enabled(id))
849 drive->dev_flags |= IDE_DFLAG_WCACHE;
851 set_wcache(drive, 1);
854 static void ide_cacheflush_p(ide_drive_t *drive)
856 if (ata_id_flush_enabled(drive->id) == 0 ||
857 (drive->dev_flags & IDE_DFLAG_WCACHE) == 0)
860 if (do_idedisk_flushcache(drive))
861 printk(KERN_INFO "%s: wcache flush failed!\n", drive->name);
864 static void ide_disk_remove(ide_drive_t *drive)
866 struct ide_disk_obj *idkp = drive->driver_data;
867 struct gendisk *g = idkp->disk;
869 ide_proc_unregister_driver(drive, idkp->driver);
873 ide_cacheflush_p(drive);
878 static void ide_disk_release(struct kref *kref)
880 struct ide_disk_obj *idkp = to_ide_disk(kref);
881 ide_drive_t *drive = idkp->drive;
882 struct gendisk *g = idkp->disk;
884 drive->driver_data = NULL;
885 g->private_data = NULL;
890 static int ide_disk_probe(ide_drive_t *drive);
893 * On HPA drives the capacity needs to be
894 * reinitilized on resume otherwise the disk
895 * can not be used and a hard reset is required
897 static void ide_disk_resume(ide_drive_t *drive)
899 if (ata_id_hpa_enabled(drive->id))
900 init_idedisk_capacity(drive);
903 static void ide_device_shutdown(ide_drive_t *drive)
906 /* On Alpha, halt(8) doesn't actually turn the machine off,
907 it puts you into the sort of firmware monitor. Typically,
908 it's used to boot another kernel image, so it's not much
909 different from reboot(8). Therefore, we don't need to
910 spin down the disk in this case, especially since Alpha
911 firmware doesn't handle disks in standby mode properly.
912 On the other hand, it's reasonably safe to turn the power
913 off when the shutdown process reaches the firmware prompt,
914 as the firmware initialization takes rather long time -
915 at least 10 seconds, which should be sufficient for
916 the disk to expire its write cache. */
917 if (system_state != SYSTEM_POWER_OFF) {
919 if (system_state == SYSTEM_RESTART) {
921 ide_cacheflush_p(drive);
925 printk(KERN_INFO "Shutdown: %s\n", drive->name);
927 drive->gendev.bus->suspend(&drive->gendev, PMSG_SUSPEND);
930 static ide_driver_t idedisk_driver = {
932 .owner = THIS_MODULE,
934 .bus = &ide_bus_type,
936 .probe = ide_disk_probe,
937 .remove = ide_disk_remove,
938 .resume = ide_disk_resume,
939 .shutdown = ide_device_shutdown,
940 .version = IDEDISK_VERSION,
941 .do_request = ide_do_rw_disk,
942 .end_request = ide_end_request,
943 .error = __ide_error,
944 #ifdef CONFIG_IDE_PROC_FS
945 .proc = idedisk_proc,
946 .settings = idedisk_settings,
950 static int idedisk_set_doorlock(ide_drive_t *drive, int on)
954 memset(&task, 0, sizeof(task));
955 task.tf.command = on ? ATA_CMD_MEDIA_LOCK : ATA_CMD_MEDIA_UNLOCK;
956 task.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
958 return ide_no_data_taskfile(drive, &task);
961 static int idedisk_open(struct inode *inode, struct file *filp)
963 struct gendisk *disk = inode->i_bdev->bd_disk;
964 struct ide_disk_obj *idkp;
967 idkp = ide_disk_get(disk);
975 if ((drive->dev_flags & IDE_DFLAG_REMOVABLE) && idkp->openers == 1) {
976 check_disk_change(inode->i_bdev);
978 * Ignore the return code from door_lock,
979 * since the open() has already succeeded,
980 * and the door_lock is irrelevant at this point.
982 if ((drive->dev_flags & IDE_DFLAG_DOORLOCKING) &&
983 idedisk_set_doorlock(drive, 1))
984 drive->dev_flags &= ~IDE_DFLAG_DOORLOCKING;
989 static int idedisk_release(struct inode *inode, struct file *filp)
991 struct gendisk *disk = inode->i_bdev->bd_disk;
992 struct ide_disk_obj *idkp = ide_disk_g(disk);
993 ide_drive_t *drive = idkp->drive;
995 if (idkp->openers == 1)
996 ide_cacheflush_p(drive);
998 if ((drive->dev_flags & IDE_DFLAG_REMOVABLE) && idkp->openers == 1) {
999 if ((drive->dev_flags & IDE_DFLAG_DOORLOCKING) &&
1000 idedisk_set_doorlock(drive, 0))
1001 drive->dev_flags &= ~IDE_DFLAG_DOORLOCKING;
1011 static int idedisk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1013 struct ide_disk_obj *idkp = ide_disk_g(bdev->bd_disk);
1014 ide_drive_t *drive = idkp->drive;
1016 geo->heads = drive->bios_head;
1017 geo->sectors = drive->bios_sect;
1018 geo->cylinders = (u16)drive->bios_cyl; /* truncate */
1022 static const struct ide_ioctl_devset ide_disk_ioctl_settings[] = {
1023 { HDIO_GET_ADDRESS, HDIO_SET_ADDRESS, &ide_devset_address },
1024 { HDIO_GET_MULTCOUNT, HDIO_SET_MULTCOUNT, &ide_devset_multcount },
1025 { HDIO_GET_NOWERR, HDIO_SET_NOWERR, &ide_devset_nowerr },
1026 { HDIO_GET_WCACHE, HDIO_SET_WCACHE, &ide_devset_wcache },
1027 { HDIO_GET_ACOUSTIC, HDIO_SET_ACOUSTIC, &ide_devset_acoustic },
1031 static int idedisk_ioctl(struct inode *inode, struct file *file,
1032 unsigned int cmd, unsigned long arg)
1034 struct block_device *bdev = inode->i_bdev;
1035 struct ide_disk_obj *idkp = ide_disk_g(bdev->bd_disk);
1036 ide_drive_t *drive = idkp->drive;
1039 err = ide_setting_ioctl(drive, bdev, cmd, arg, ide_disk_ioctl_settings);
1040 if (err != -EOPNOTSUPP)
1043 return generic_ide_ioctl(drive, file, bdev, cmd, arg);
1046 static int idedisk_media_changed(struct gendisk *disk)
1048 struct ide_disk_obj *idkp = ide_disk_g(disk);
1049 ide_drive_t *drive = idkp->drive;
1051 /* do not scan partitions twice if this is a removable device */
1052 if (drive->dev_flags & IDE_DFLAG_ATTACH) {
1053 drive->dev_flags &= ~IDE_DFLAG_ATTACH;
1057 /* if removable, always assume it was changed */
1058 return !!(drive->dev_flags & IDE_DFLAG_REMOVABLE);
1061 static int idedisk_revalidate_disk(struct gendisk *disk)
1063 struct ide_disk_obj *idkp = ide_disk_g(disk);
1064 set_capacity(disk, idedisk_capacity(idkp->drive));
1068 static struct block_device_operations idedisk_ops = {
1069 .owner = THIS_MODULE,
1070 .open = idedisk_open,
1071 .release = idedisk_release,
1072 .ioctl = idedisk_ioctl,
1073 .getgeo = idedisk_getgeo,
1074 .media_changed = idedisk_media_changed,
1075 .revalidate_disk = idedisk_revalidate_disk
1078 MODULE_DESCRIPTION("ATA DISK Driver");
1080 static int ide_disk_probe(ide_drive_t *drive)
1082 struct ide_disk_obj *idkp;
1085 /* strstr("foo", "") is non-NULL */
1086 if (!strstr("ide-disk", drive->driver_req))
1089 if (drive->media != ide_disk)
1092 idkp = kzalloc(sizeof(*idkp), GFP_KERNEL);
1096 g = alloc_disk_node(IDE_DISK_MINORS, hwif_to_node(drive->hwif));
1100 ide_init_disk(g, drive);
1102 kref_init(&idkp->kref);
1104 idkp->drive = drive;
1105 idkp->driver = &idedisk_driver;
1108 g->private_data = &idkp->driver;
1110 drive->driver_data = idkp;
1112 idedisk_setup(drive);
1113 if ((!drive->head || drive->head > 16) && !drive->select.b.lba) {
1114 printk(KERN_ERR "%s: INVALID GEOMETRY: %d PHYSICAL HEADS?\n",
1115 drive->name, drive->head);
1116 drive->dev_flags &= ~IDE_DFLAG_ATTACH;
1118 drive->dev_flags |= IDE_DFLAG_ATTACH;
1120 g->minors = IDE_DISK_MINORS;
1121 g->driverfs_dev = &drive->gendev;
1122 g->flags |= GENHD_FL_EXT_DEVT;
1123 if (drive->dev_flags & IDE_DFLAG_REMOVABLE)
1124 g->flags = GENHD_FL_REMOVABLE;
1125 set_capacity(g, idedisk_capacity(drive));
1126 g->fops = &idedisk_ops;
1136 static void __exit idedisk_exit(void)
1138 driver_unregister(&idedisk_driver.gen_driver);
1141 static int __init idedisk_init(void)
1143 return driver_register(&idedisk_driver.gen_driver);
1146 MODULE_ALIAS("ide:*m-disk*");
1147 module_init(idedisk_init);
1148 module_exit(idedisk_exit);
1149 MODULE_LICENSE("GPL");