2 * linux/drivers/ide/ide-disk.c Version 1.18 Mar 05, 2003
4 * Copyright (C) 1994-1998 Linus Torvalds & authors (see below)
5 * Copyright (C) 1998-2002 Linux ATA Development
6 * Andre Hedrick <andre@linux-ide.org>
7 * Copyright (C) 2003 Red Hat <alan@redhat.com>
11 * Mostly written by Mark Lord <mlord@pobox.com>
12 * and Gadi Oxman <gadio@netvision.net.il>
13 * and Andre Hedrick <andre@linux-ide.org>
15 * This is the IDE/ATA disk driver, as evolved from hd.c and ide.c.
17 * Version 1.00 move disk only code from ide.c to ide-disk.c
18 * support optional byte-swapping of all data
19 * Version 1.01 fix previous byte-swapping code
20 * Version 1.02 remove ", LBA" from drive identification msgs
21 * Version 1.03 fix display of id->buf_size for big-endian
22 * Version 1.04 add /proc configurable settings and S.M.A.R.T support
23 * Version 1.05 add capacity support for ATA3 >= 8GB
24 * Version 1.06 get boot-up messages to show full cyl count
25 * Version 1.07 disable door-locking if it fails
26 * Version 1.08 fixed CHS/LBA translations for ATA4 > 8GB,
27 * process of adding new ATA4 compliance.
28 * fixed problems in allowing fdisk to see
30 * Version 1.09 added increment of rq->sector in ide_multwrite
31 * added UDMA 3/4 reporting
32 * Version 1.10 request queue changes, Ultra DMA 100
33 * Version 1.11 added 48-bit lba
34 * Version 1.12 adding taskfile io access method
35 * Version 1.13 added standby and flush-cache for notifier
36 * Version 1.14 added acoustic-wcache
37 * Version 1.15 convert all calls to ide_raw_taskfile
38 * since args will return register content.
39 * Version 1.16 added suspend-resume-checkpower
40 * Version 1.17 do flush on standy, do flush on ATA < ATA6
44 #define IDEDISK_VERSION "1.18"
46 #undef REALLY_SLOW_IO /* most systems can safely undef this */
50 #include <linux/config.h>
51 #include <linux/module.h>
52 #include <linux/types.h>
53 #include <linux/string.h>
54 #include <linux/kernel.h>
55 #include <linux/timer.h>
57 #include <linux/interrupt.h>
58 #include <linux/major.h>
59 #include <linux/errno.h>
60 #include <linux/genhd.h>
61 #include <linux/slab.h>
62 #include <linux/delay.h>
66 #include <linux/ide.h>
68 #include <asm/byteorder.h>
70 #include <asm/uaccess.h>
72 #include <asm/div64.h>
74 /* FIXME: some day we shouldn't need to look in here! */
76 #include "legacy/pdc4030.h"
79 * lba_capacity_is_ok() performs a sanity check on the claimed "lba_capacity"
80 * value for this drive (from its reported identification information).
82 * Returns: 1 if lba_capacity looks sensible
85 * It is called only once for each drive.
87 static int lba_capacity_is_ok (struct hd_driveid *id)
89 unsigned long lba_sects, chs_sects, head, tail;
92 * The ATA spec tells large drives to return
93 * C/H/S = 16383/16/63 independent of their size.
94 * Some drives can be jumpered to use 15 heads instead of 16.
95 * Some drives can be jumpered to use 4092 cyls instead of 16383.
97 if ((id->cyls == 16383
98 || (id->cyls == 4092 && id->cur_cyls == 16383)) &&
100 (id->heads == 15 || id->heads == 16) &&
101 (id->lba_capacity >= 16383*63*id->heads))
104 lba_sects = id->lba_capacity;
105 chs_sects = id->cyls * id->heads * id->sectors;
107 /* perform a rough sanity check on lba_sects: within 10% is OK */
108 if ((lba_sects - chs_sects) < chs_sects/10)
111 /* some drives have the word order reversed */
112 head = ((lba_sects >> 16) & 0xffff);
113 tail = (lba_sects & 0xffff);
114 lba_sects = (head | (tail << 16));
115 if ((lba_sects - chs_sects) < chs_sects/10) {
116 id->lba_capacity = lba_sects;
117 return 1; /* lba_capacity is (now) good */
120 return 0; /* lba_capacity value may be bad */
123 #ifndef CONFIG_IDE_TASKFILE_IO
126 * read_intr() is the handler for disk read/multread interrupts
128 static ide_startstop_t read_intr (ide_drive_t *drive)
130 ide_hwif_t *hwif = HWIF(drive);
131 u32 i = 0, nsect = 0, msect = drive->mult_count;
137 /* new way for dealing with premature shared PCI interrupts */
138 if (!OK_STAT(stat=hwif->INB(IDE_STATUS_REG),DATA_READY,BAD_R_STAT)) {
139 if (stat & (ERR_STAT|DRQ_STAT)) {
140 return DRIVER(drive)->error(drive, "read_intr", stat);
142 /* no data yet, so wait for another interrupt */
143 ide_set_handler(drive, &read_intr, WAIT_CMD, NULL);
148 rq = HWGROUP(drive)->rq;
150 if ((nsect = rq->current_nr_sectors) > msect)
155 to = ide_map_buffer(rq, &flags);
156 taskfile_input_data(drive, to, nsect * SECTOR_WORDS);
158 printk("%s: read: sectors(%ld-%ld), buffer=0x%08lx, remaining=%ld\n",
159 drive->name, rq->sector, rq->sector+nsect-1,
160 (unsigned long) rq->buffer+(nsect<<9), rq->nr_sectors-nsect);
162 ide_unmap_buffer(rq, to, &flags);
165 i = (rq->nr_sectors -= nsect);
166 if (((long)(rq->current_nr_sectors -= nsect)) <= 0)
167 ide_end_request(drive, 1, rq->hard_cur_sectors);
169 * Another BH Page walker and DATA INTEGRITY Questioned on ERROR.
170 * If passed back up on multimode read, BAD DATA could be ACKED
171 * to FILE SYSTEMS above ...
176 ide_set_handler(drive, &read_intr, WAIT_CMD, NULL);
183 * write_intr() is the handler for disk write interrupts
185 static ide_startstop_t write_intr (ide_drive_t *drive)
187 ide_hwgroup_t *hwgroup = HWGROUP(drive);
188 ide_hwif_t *hwif = HWIF(drive);
189 struct request *rq = hwgroup->rq;
193 if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG),
194 DRIVE_READY, drive->bad_wstat)) {
195 printk("%s: write_intr error1: nr_sectors=%ld, stat=0x%02x\n",
196 drive->name, rq->nr_sectors, stat);
199 printk("%s: write: sector %ld, buffer=0x%08lx, remaining=%ld\n",
200 drive->name, rq->sector, (unsigned long) rq->buffer,
203 if ((rq->nr_sectors == 1) ^ ((stat & DRQ_STAT) != 0)) {
206 i = --rq->nr_sectors;
207 --rq->current_nr_sectors;
208 if (((long)rq->current_nr_sectors) <= 0)
209 ide_end_request(drive, 1, rq->hard_cur_sectors);
212 char *to = ide_map_buffer(rq, &flags);
213 taskfile_output_data(drive, to, SECTOR_WORDS);
214 ide_unmap_buffer(rq, to, &flags);
215 ide_set_handler(drive, &write_intr, WAIT_CMD, NULL);
220 /* the original code did this here (?) */
223 return DRIVER(drive)->error(drive, "write_intr", stat);
227 * ide_multwrite() transfers a block of up to mcount sectors of data
228 * to a drive as part of a disk multiple-sector write operation.
230 * Note that we may be called from two contexts - __ide_do_rw_disk() context
231 * and IRQ context. The IRQ can happen any time after we've output the
232 * full "mcount" number of sectors, so we must make sure we update the
233 * state _before_ we output the final part of the data!
235 * The update and return to BH is a BLOCK Layer Fakey to get more data
236 * to satisfy the hardware atomic segment. If the hardware atomic segment
237 * is shorter or smaller than the BH segment then we should be OKAY.
238 * This is only valid if we can rewind the rq->current_nr_sectors counter.
240 static void ide_multwrite(ide_drive_t *drive, unsigned int mcount)
242 ide_hwgroup_t *hwgroup = HWGROUP(drive);
243 struct request *rq = &hwgroup->wrq;
247 int nsect = rq->current_nr_sectors;
253 buffer = ide_map_buffer(rq, &flags);
256 rq->nr_sectors -= nsect;
257 rq->current_nr_sectors -= nsect;
259 /* Do we move to the next bh after this? */
260 if (!rq->current_nr_sectors) {
261 struct bio *bio = rq->bio;
264 * only move to next bio, when we have processed
265 * all bvecs in this one.
267 if (++bio->bi_idx >= bio->bi_vcnt) {
268 bio->bi_idx = bio->bi_vcnt - rq->nr_cbio_segments;
272 /* end early early we ran out of requests */
277 rq->nr_cbio_segments = bio_segments(bio);
278 rq->current_nr_sectors = bio_cur_sectors(bio);
279 rq->hard_cur_sectors = rq->current_nr_sectors;
284 * Ok, we're all setup for the interrupt
285 * re-entering us on the last transfer.
287 taskfile_output_data(drive, buffer, nsect<<7);
288 ide_unmap_buffer(rq, buffer, &flags);
293 * multwrite_intr() is the handler for disk multwrite interrupts
295 static ide_startstop_t multwrite_intr (ide_drive_t *drive)
297 ide_hwgroup_t *hwgroup = HWGROUP(drive);
298 ide_hwif_t *hwif = HWIF(drive);
299 struct request *rq = &hwgroup->wrq;
300 struct bio *bio = rq->bio;
303 stat = hwif->INB(IDE_STATUS_REG);
304 if (OK_STAT(stat, DRIVE_READY, drive->bad_wstat)) {
305 if (stat & DRQ_STAT) {
307 * The drive wants data. Remember rq is the copy
310 if (rq->nr_sectors) {
311 ide_multwrite(drive, drive->mult_count);
312 ide_set_handler(drive, &multwrite_intr, WAIT_CMD, NULL);
317 * If the copy has all the blocks completed then
318 * we can end the original request.
320 if (!rq->nr_sectors) { /* all done? */
321 bio->bi_idx = bio->bi_vcnt - rq->nr_cbio_segments;
323 ide_end_request(drive, 1, rq->nr_sectors);
327 bio->bi_idx = bio->bi_vcnt - rq->nr_cbio_segments;
328 /* the original code did this here (?) */
331 bio->bi_idx = bio->bi_vcnt - rq->nr_cbio_segments;
332 return DRIVER(drive)->error(drive, "multwrite_intr", stat);
336 * __ide_do_rw_disk() issues READ and WRITE commands to a disk,
337 * using LBA if supported, or CHS otherwise, to address sectors.
338 * It also takes care of issuing special DRIVE_CMDs.
340 ide_startstop_t __ide_do_rw_disk (ide_drive_t *drive, struct request *rq, sector_t block)
342 ide_hwif_t *hwif = HWIF(drive);
343 u8 lba48 = (drive->addressing == 1) ? 1 : 0;
344 task_ioreg_t command = WIN_NOP;
345 ata_nsector_t nsectors;
347 nsectors.all = (u16) rq->nr_sectors;
350 hwif->OUTB(drive->ctl, IDE_CONTROL_REG);
352 if (drive->select.b.lba) {
353 if (drive->addressing == 1) {
354 task_ioreg_t tasklets[10];
356 pr_debug("%s: LBA=0x%012llx\n", drive->name, block);
360 tasklets[2] = nsectors.b.low;
361 tasklets[3] = nsectors.b.high;
362 tasklets[4] = (task_ioreg_t) block;
363 tasklets[5] = (task_ioreg_t) (block>>8);
364 tasklets[6] = (task_ioreg_t) (block>>16);
365 tasklets[7] = (task_ioreg_t) (block>>24);
366 if (sizeof(block) == 4) {
367 tasklets[8] = (task_ioreg_t) 0;
368 tasklets[9] = (task_ioreg_t) 0;
370 tasklets[8] = (task_ioreg_t)((u64)block >> 32);
371 tasklets[9] = (task_ioreg_t)((u64)block >> 40);
374 printk("%s: 0x%02x%02x 0x%02x%02x%02x%02x%02x%02x\n",
375 drive->name, tasklets[3], tasklets[2],
376 tasklets[9], tasklets[8], tasklets[7],
377 tasklets[6], tasklets[5], tasklets[4]);
379 hwif->OUTB(tasklets[1], IDE_FEATURE_REG);
380 hwif->OUTB(tasklets[3], IDE_NSECTOR_REG);
381 hwif->OUTB(tasklets[7], IDE_SECTOR_REG);
382 hwif->OUTB(tasklets[8], IDE_LCYL_REG);
383 hwif->OUTB(tasklets[9], IDE_HCYL_REG);
385 hwif->OUTB(tasklets[0], IDE_FEATURE_REG);
386 hwif->OUTB(tasklets[2], IDE_NSECTOR_REG);
387 hwif->OUTB(tasklets[4], IDE_SECTOR_REG);
388 hwif->OUTB(tasklets[5], IDE_LCYL_REG);
389 hwif->OUTB(tasklets[6], IDE_HCYL_REG);
390 hwif->OUTB(0x00|drive->select.all,IDE_SELECT_REG);
392 hwif->OUTB(0x00, IDE_FEATURE_REG);
393 hwif->OUTB(nsectors.b.low, IDE_NSECTOR_REG);
394 hwif->OUTB(block, IDE_SECTOR_REG);
395 hwif->OUTB(block>>=8, IDE_LCYL_REG);
396 hwif->OUTB(block>>=8, IDE_HCYL_REG);
397 hwif->OUTB(((block>>8)&0x0f)|drive->select.all,IDE_SELECT_REG);
400 unsigned int sect,head,cyl,track;
401 track = (int)block / drive->sect;
402 sect = (int)block % drive->sect + 1;
403 hwif->OUTB(sect, IDE_SECTOR_REG);
404 head = track % drive->head;
405 cyl = track / drive->head;
407 pr_debug("%s: CHS=%u/%u/%u\n", drive->name, cyl, head, sect);
409 hwif->OUTB(0x00, IDE_FEATURE_REG);
410 hwif->OUTB(nsectors.b.low, IDE_NSECTOR_REG);
411 hwif->OUTB(cyl, IDE_LCYL_REG);
412 hwif->OUTB(cyl>>8, IDE_HCYL_REG);
413 hwif->OUTB(head|drive->select.all,IDE_SELECT_REG);
416 if (rq_data_dir(rq) == READ) {
417 if (drive->using_dma && !hwif->ide_dma_read(drive))
420 command = ((drive->mult_count) ?
421 ((lba48) ? WIN_MULTREAD_EXT : WIN_MULTREAD) :
422 ((lba48) ? WIN_READ_EXT : WIN_READ));
423 ide_execute_command(drive, command, &read_intr, WAIT_CMD, NULL);
426 ide_startstop_t startstop;
428 if (drive->using_dma && !(HWIF(drive)->ide_dma_write(drive)))
431 command = ((drive->mult_count) ?
432 ((lba48) ? WIN_MULTWRITE_EXT : WIN_MULTWRITE) :
433 ((lba48) ? WIN_WRITE_EXT : WIN_WRITE));
434 hwif->OUTB(command, IDE_COMMAND_REG);
436 if (ide_wait_stat(&startstop, drive, DATA_READY,
437 drive->bad_wstat, WAIT_DRQ)) {
438 printk(KERN_ERR "%s: no DRQ after issuing %s\n",
440 drive->mult_count ? "MULTWRITE" : "WRITE");
445 if (drive->mult_count) {
446 ide_hwgroup_t *hwgroup = HWGROUP(drive);
448 hwgroup->wrq = *rq; /* scratchpad */
449 ide_set_handler(drive, &multwrite_intr, WAIT_CMD, NULL);
450 ide_multwrite(drive, drive->mult_count);
453 char *to = ide_map_buffer(rq, &flags);
454 ide_set_handler(drive, &write_intr, WAIT_CMD, NULL);
455 taskfile_output_data(drive, to, SECTOR_WORDS);
456 ide_unmap_buffer(rq, to, &flags);
461 EXPORT_SYMBOL_GPL(__ide_do_rw_disk);
463 #else /* CONFIG_IDE_TASKFILE_IO */
465 static ide_startstop_t chs_rw_disk(ide_drive_t *, struct request *, unsigned long);
466 static ide_startstop_t lba_28_rw_disk(ide_drive_t *, struct request *, unsigned long);
467 static ide_startstop_t lba_48_rw_disk(ide_drive_t *, struct request *, unsigned long long);
470 * __ide_do_rw_disk() issues READ and WRITE commands to a disk,
471 * using LBA if supported, or CHS otherwise, to address sectors.
472 * It also takes care of issuing special DRIVE_CMDs.
474 ide_startstop_t __ide_do_rw_disk (ide_drive_t *drive, struct request *rq, sector_t block)
477 * 268435455 == 137439 MB or 28bit limit
479 * need to add split taskfile operations based on 28bit threshold.
481 if (drive->addressing == 1) /* 48-bit LBA */
482 return lba_48_rw_disk(drive, rq, (unsigned long long) block);
483 if (drive->select.b.lba) /* 28-bit LBA */
484 return lba_28_rw_disk(drive, rq, (unsigned long) block);
486 /* 28-bit CHS : DIE DIE DIE piece of legacy crap!!! */
487 return chs_rw_disk(drive, rq, (unsigned long) block);
489 EXPORT_SYMBOL_GPL(__ide_do_rw_disk);
491 static u8 get_command(ide_drive_t *drive, int cmd, ide_task_t *task)
493 unsigned int lba48 = (drive->addressing == 1) ? 1 : 0;
496 task->command_type = IDE_DRIVE_TASK_IN;
497 if (drive->using_dma)
498 return lba48 ? WIN_READDMA_EXT : WIN_READDMA;
499 if (drive->mult_count) {
500 task->handler = &task_mulin_intr;
501 return lba48 ? WIN_MULTREAD_EXT : WIN_MULTREAD;
503 task->handler = &task_in_intr;
504 return lba48 ? WIN_READ_EXT : WIN_READ;
506 task->command_type = IDE_DRIVE_TASK_RAW_WRITE;
507 if (drive->using_dma)
508 return lba48 ? WIN_WRITEDMA_EXT : WIN_WRITEDMA;
509 if (drive->mult_count) {
510 task->prehandler = &pre_task_mulout_intr;
511 task->handler = &task_mulout_intr;
512 return lba48 ? WIN_MULTWRITE_EXT : WIN_MULTWRITE;
514 task->prehandler = &pre_task_out_intr;
515 task->handler = &task_out_intr;
516 return lba48 ? WIN_WRITE_EXT : WIN_WRITE;
520 static ide_startstop_t chs_rw_disk (ide_drive_t *drive, struct request *rq, unsigned long block)
524 ata_nsector_t nsectors;
525 unsigned int track = (block / drive->sect);
526 unsigned int sect = (block % drive->sect) + 1;
527 unsigned int head = (track % drive->head);
528 unsigned int cyl = (track / drive->head);
530 nsectors.all = (u16) rq->nr_sectors;
532 pr_debug("%s: CHS=%u/%u/%u\n", drive->name, cyl, head, sect);
534 memset(&args, 0, sizeof(ide_task_t));
536 sectors = (rq->nr_sectors == 256) ? 0x00 : rq->nr_sectors;
538 args.tfRegister[IDE_NSECTOR_OFFSET] = sectors;
539 args.tfRegister[IDE_SECTOR_OFFSET] = sect;
540 args.tfRegister[IDE_LCYL_OFFSET] = cyl;
541 args.tfRegister[IDE_HCYL_OFFSET] = (cyl>>8);
542 args.tfRegister[IDE_SELECT_OFFSET] = head;
543 args.tfRegister[IDE_SELECT_OFFSET] |= drive->select.all;
544 args.tfRegister[IDE_COMMAND_OFFSET] = get_command(drive, rq_data_dir(rq), &args);
545 args.rq = (struct request *) rq;
546 rq->special = (ide_task_t *)&args;
547 return do_rw_taskfile(drive, &args);
550 static ide_startstop_t lba_28_rw_disk (ide_drive_t *drive, struct request *rq, unsigned long block)
554 ata_nsector_t nsectors;
556 nsectors.all = (u16) rq->nr_sectors;
558 memset(&args, 0, sizeof(ide_task_t));
560 sectors = (rq->nr_sectors == 256) ? 0x00 : rq->nr_sectors;
562 args.tfRegister[IDE_NSECTOR_OFFSET] = sectors;
563 args.tfRegister[IDE_SECTOR_OFFSET] = block;
564 args.tfRegister[IDE_LCYL_OFFSET] = (block>>=8);
565 args.tfRegister[IDE_HCYL_OFFSET] = (block>>=8);
566 args.tfRegister[IDE_SELECT_OFFSET] = ((block>>8)&0x0f);
567 args.tfRegister[IDE_SELECT_OFFSET] |= drive->select.all;
568 args.tfRegister[IDE_COMMAND_OFFSET] = get_command(drive, rq_data_dir(rq), &args);
569 args.rq = (struct request *) rq;
570 rq->special = (ide_task_t *)&args;
571 return do_rw_taskfile(drive, &args);
575 * 268435455 == 137439 MB or 28bit limit
576 * 320173056 == 163929 MB or 48bit addressing
577 * 1073741822 == 549756 MB or 48bit addressing fake drive
580 static ide_startstop_t lba_48_rw_disk (ide_drive_t *drive, struct request *rq, unsigned long long block)
584 ata_nsector_t nsectors;
586 nsectors.all = (u16) rq->nr_sectors;
588 memset(&args, 0, sizeof(ide_task_t));
590 sectors = (rq->nr_sectors == 65536) ? 0 : rq->nr_sectors;
592 args.tfRegister[IDE_NSECTOR_OFFSET] = sectors;
593 args.hobRegister[IDE_NSECTOR_OFFSET] = sectors >> 8;
594 args.tfRegister[IDE_SECTOR_OFFSET] = block; /* low lba */
595 args.tfRegister[IDE_LCYL_OFFSET] = (block>>=8); /* mid lba */
596 args.tfRegister[IDE_HCYL_OFFSET] = (block>>=8); /* hi lba */
597 args.tfRegister[IDE_SELECT_OFFSET] = drive->select.all;
598 args.tfRegister[IDE_COMMAND_OFFSET] = get_command(drive, rq_data_dir(rq), &args);
599 args.hobRegister[IDE_SECTOR_OFFSET] = (block>>=8); /* low lba */
600 args.hobRegister[IDE_LCYL_OFFSET] = (block>>=8); /* mid lba */
601 args.hobRegister[IDE_HCYL_OFFSET] = (block>>=8); /* hi lba */
602 args.hobRegister[IDE_SELECT_OFFSET] = drive->select.all;
603 args.hobRegister[IDE_CONTROL_OFFSET_HOB]= (drive->ctl|0x80);
604 args.rq = (struct request *) rq;
605 rq->special = (ide_task_t *)&args;
606 return do_rw_taskfile(drive, &args);
609 #endif /* CONFIG_IDE_TASKFILE_IO */
611 static ide_startstop_t ide_do_rw_disk (ide_drive_t *drive, struct request *rq, sector_t block)
613 ide_hwif_t *hwif = HWIF(drive);
615 BUG_ON(drive->blocked);
617 if (!blk_fs_request(rq)) {
618 blk_dump_rq_flags(rq, "ide_do_rw_disk - bad command");
619 ide_end_request(drive, 0, 0);
623 pr_debug("%s: %sing: block=%llu, sectors=%lu, buffer=0x%08lx\n",
624 drive->name, rq_data_dir(rq) == READ ? "read" : "writ",
625 block, rq->nr_sectors, (unsigned long)rq->buffer);
628 return hwif->rw_disk(drive, rq, block);
630 return __ide_do_rw_disk(drive, rq, block);
633 static u8 idedisk_dump_status (ide_drive_t *drive, const char *msg, u8 stat)
635 ide_hwif_t *hwif = HWIF(drive);
639 local_irq_set(flags);
640 printk("%s: %s: status=0x%02x", drive->name, msg, stat);
641 #if FANCY_STATUS_DUMPS
643 if (stat & BUSY_STAT)
646 if (stat & READY_STAT) printk("DriveReady ");
647 if (stat & WRERR_STAT) printk("DeviceFault ");
648 if (stat & SEEK_STAT) printk("SeekComplete ");
649 if (stat & DRQ_STAT) printk("DataRequest ");
650 if (stat & ECC_STAT) printk("CorrectedError ");
651 if (stat & INDEX_STAT) printk("Index ");
652 if (stat & ERR_STAT) printk("Error ");
655 #endif /* FANCY_STATUS_DUMPS */
657 if ((stat & (BUSY_STAT|ERR_STAT)) == ERR_STAT) {
658 err = hwif->INB(IDE_ERROR_REG);
659 printk("%s: %s: error=0x%02x", drive->name, msg, err);
660 #if FANCY_STATUS_DUMPS
662 if (err & ABRT_ERR) printk("DriveStatusError ");
664 printk("Bad%s ", (err & ABRT_ERR) ? "CRC" : "Sector");
665 if (err & ECC_ERR) printk("UncorrectableError ");
666 if (err & ID_ERR) printk("SectorIdNotFound ");
667 if (err & TRK0_ERR) printk("TrackZeroNotFound ");
668 if (err & MARK_ERR) printk("AddrMarkNotFound ");
670 if ((err & (BBD_ERR | ABRT_ERR)) == BBD_ERR ||
671 (err & (ECC_ERR|ID_ERR|MARK_ERR))) {
672 if (drive->addressing == 1) {
674 u32 low = 0, high = 0;
675 low = ide_read_24(drive);
676 hwif->OUTB(drive->ctl|0x80, IDE_CONTROL_REG);
677 high = ide_read_24(drive);
678 sectors = ((__u64)high << 24) | low;
679 printk(", LBAsect=%llu, high=%d, low=%d",
680 (unsigned long long) sectors,
683 u8 cur = hwif->INB(IDE_SELECT_REG);
684 if (cur & 0x40) { /* using LBA? */
685 printk(", LBAsect=%ld", (unsigned long)
687 |(hwif->INB(IDE_HCYL_REG)<<16)
688 |(hwif->INB(IDE_LCYL_REG)<<8)
689 | hwif->INB(IDE_SECTOR_REG));
691 printk(", CHS=%d/%d/%d",
692 (hwif->INB(IDE_HCYL_REG)<<8) +
693 hwif->INB(IDE_LCYL_REG),
695 hwif->INB(IDE_SECTOR_REG));
698 if (HWGROUP(drive) && HWGROUP(drive)->rq)
699 printk(", sector=%llu",
700 (unsigned long long)HWGROUP(drive)->rq->sector);
703 #endif /* FANCY_STATUS_DUMPS */
705 local_irq_restore(flags);
709 ide_startstop_t idedisk_error (ide_drive_t *drive, const char *msg, u8 stat)
715 err = idedisk_dump_status(drive, msg, stat);
717 if (drive == NULL || (rq = HWGROUP(drive)->rq) == NULL)
721 /* retry only "normal" I/O: */
722 if (rq->flags & (REQ_DRIVE_CMD | REQ_DRIVE_TASK | REQ_DRIVE_TASKFILE)) {
724 ide_end_drive_cmd(drive, stat, err);
727 #ifdef CONFIG_IDE_TASKFILE_IO
728 /* make rq completion pointers new submission pointers */
729 blk_rq_prep_restart(rq);
732 if (stat & BUSY_STAT || ((stat & WRERR_STAT) && !drive->nowerr)) {
733 /* other bits are useless when BUSY */
734 rq->errors |= ERROR_RESET;
735 } else if (stat & ERR_STAT) {
736 /* err has different meaning on cdrom and tape */
737 if (err == ABRT_ERR) {
738 if (drive->select.b.lba &&
739 /* some newer drives don't support WIN_SPECIFY */
740 hwif->INB(IDE_COMMAND_REG) == WIN_SPECIFY)
742 } else if ((err & BAD_CRC) == BAD_CRC) {
743 /* UDMA crc error, just retry the operation */
745 } else if (err & (BBD_ERR | ECC_ERR)) {
746 /* retries won't help these */
747 rq->errors = ERROR_MAX;
748 } else if (err & TRK0_ERR) {
749 /* help it find track zero */
750 rq->errors |= ERROR_RECAL;
753 if ((stat & DRQ_STAT) && rq_data_dir(rq) == READ)
754 try_to_flush_leftover_data(drive);
755 if (hwif->INB(IDE_STATUS_REG) & (BUSY_STAT|DRQ_STAT)) {
757 hwif->OUTB(WIN_IDLEIMMEDIATE,IDE_COMMAND_REG);
759 if (rq->errors >= ERROR_MAX || blk_noretry_request(rq))
760 DRIVER(drive)->end_request(drive, 0, 0);
762 if ((rq->errors & ERROR_RESET) == ERROR_RESET) {
764 return ide_do_reset(drive);
766 if ((rq->errors & ERROR_RECAL) == ERROR_RECAL)
767 drive->special.b.recalibrate = 1;
773 ide_startstop_t idedisk_abort(ide_drive_t *drive, const char *msg)
778 if (drive == NULL || (rq = HWGROUP(drive)->rq) == NULL)
783 if (rq->flags & (REQ_DRIVE_CMD | REQ_DRIVE_TASK | REQ_DRIVE_TASKFILE)) {
785 ide_end_drive_cmd(drive, BUSY_STAT, 0);
789 DRIVER(drive)->end_request(drive, 0, 0);
794 * Queries for true maximum capacity of the drive.
795 * Returns maximum LBA address (> 0) of the drive, 0 if failed.
797 static unsigned long idedisk_read_native_max_address(ide_drive_t *drive)
800 unsigned long addr = 0;
802 /* Create IDE/ATA command request structure */
803 memset(&args, 0, sizeof(ide_task_t));
804 args.tfRegister[IDE_SELECT_OFFSET] = 0x40;
805 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_READ_NATIVE_MAX;
806 args.command_type = IDE_DRIVE_TASK_NO_DATA;
807 args.handler = &task_no_data_intr;
808 /* submit command request */
809 ide_raw_taskfile(drive, &args, NULL);
811 /* if OK, compute maximum address value */
812 if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
813 addr = ((args.tfRegister[IDE_SELECT_OFFSET] & 0x0f) << 24)
814 | ((args.tfRegister[ IDE_HCYL_OFFSET] ) << 16)
815 | ((args.tfRegister[ IDE_LCYL_OFFSET] ) << 8)
816 | ((args.tfRegister[IDE_SECTOR_OFFSET] ));
817 addr++; /* since the return value is (maxlba - 1), we add 1 */
822 static unsigned long long idedisk_read_native_max_address_ext(ide_drive_t *drive)
825 unsigned long long addr = 0;
827 /* Create IDE/ATA command request structure */
828 memset(&args, 0, sizeof(ide_task_t));
830 args.tfRegister[IDE_SELECT_OFFSET] = 0x40;
831 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_READ_NATIVE_MAX_EXT;
832 args.command_type = IDE_DRIVE_TASK_NO_DATA;
833 args.handler = &task_no_data_intr;
834 /* submit command request */
835 ide_raw_taskfile(drive, &args, NULL);
837 /* if OK, compute maximum address value */
838 if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
839 u32 high = (args.hobRegister[IDE_HCYL_OFFSET] << 16) |
840 (args.hobRegister[IDE_LCYL_OFFSET] << 8) |
841 args.hobRegister[IDE_SECTOR_OFFSET];
842 u32 low = ((args.tfRegister[IDE_HCYL_OFFSET])<<16) |
843 ((args.tfRegister[IDE_LCYL_OFFSET])<<8) |
844 (args.tfRegister[IDE_SECTOR_OFFSET]);
845 addr = ((__u64)high << 24) | low;
846 addr++; /* since the return value is (maxlba - 1), we add 1 */
852 * Sets maximum virtual LBA address of the drive.
853 * Returns new maximum virtual LBA address (> 0) or 0 on failure.
855 static unsigned long idedisk_set_max_address(ide_drive_t *drive, unsigned long addr_req)
858 unsigned long addr_set = 0;
861 /* Create IDE/ATA command request structure */
862 memset(&args, 0, sizeof(ide_task_t));
863 args.tfRegister[IDE_SECTOR_OFFSET] = ((addr_req >> 0) & 0xff);
864 args.tfRegister[IDE_LCYL_OFFSET] = ((addr_req >> 8) & 0xff);
865 args.tfRegister[IDE_HCYL_OFFSET] = ((addr_req >> 16) & 0xff);
866 args.tfRegister[IDE_SELECT_OFFSET] = ((addr_req >> 24) & 0x0f) | 0x40;
867 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SET_MAX;
868 args.command_type = IDE_DRIVE_TASK_NO_DATA;
869 args.handler = &task_no_data_intr;
870 /* submit command request */
871 ide_raw_taskfile(drive, &args, NULL);
872 /* if OK, read new maximum address value */
873 if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
874 addr_set = ((args.tfRegister[IDE_SELECT_OFFSET] & 0x0f) << 24)
875 | ((args.tfRegister[ IDE_HCYL_OFFSET] ) << 16)
876 | ((args.tfRegister[ IDE_LCYL_OFFSET] ) << 8)
877 | ((args.tfRegister[IDE_SECTOR_OFFSET] ));
883 static unsigned long long idedisk_set_max_address_ext(ide_drive_t *drive, unsigned long long addr_req)
886 unsigned long long addr_set = 0;
889 /* Create IDE/ATA command request structure */
890 memset(&args, 0, sizeof(ide_task_t));
891 args.tfRegister[IDE_SECTOR_OFFSET] = ((addr_req >> 0) & 0xff);
892 args.tfRegister[IDE_LCYL_OFFSET] = ((addr_req >>= 8) & 0xff);
893 args.tfRegister[IDE_HCYL_OFFSET] = ((addr_req >>= 8) & 0xff);
894 args.tfRegister[IDE_SELECT_OFFSET] = 0x40;
895 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SET_MAX_EXT;
896 args.hobRegister[IDE_SECTOR_OFFSET] = (addr_req >>= 8) & 0xff;
897 args.hobRegister[IDE_LCYL_OFFSET] = (addr_req >>= 8) & 0xff;
898 args.hobRegister[IDE_HCYL_OFFSET] = (addr_req >>= 8) & 0xff;
899 args.hobRegister[IDE_SELECT_OFFSET] = 0x40;
900 args.hobRegister[IDE_CONTROL_OFFSET_HOB]= (drive->ctl|0x80);
901 args.command_type = IDE_DRIVE_TASK_NO_DATA;
902 args.handler = &task_no_data_intr;
903 /* submit command request */
904 ide_raw_taskfile(drive, &args, NULL);
905 /* if OK, compute maximum address value */
906 if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
907 u32 high = (args.hobRegister[IDE_HCYL_OFFSET] << 16) |
908 (args.hobRegister[IDE_LCYL_OFFSET] << 8) |
909 args.hobRegister[IDE_SECTOR_OFFSET];
910 u32 low = ((args.tfRegister[IDE_HCYL_OFFSET])<<16) |
911 ((args.tfRegister[IDE_LCYL_OFFSET])<<8) |
912 (args.tfRegister[IDE_SECTOR_OFFSET]);
913 addr_set = ((__u64)high << 24) | low;
919 static unsigned long long sectors_to_MB(unsigned long long n)
921 n <<= 9; /* make it bytes */
922 do_div(n, 1000000); /* make it MB */
927 * Bits 10 of command_set_1 and cfs_enable_1 must be equal,
928 * so on non-buggy drives we need test only one.
929 * However, we should also check whether these fields are valid.
931 static inline int idedisk_supports_hpa(const struct hd_driveid *id)
933 return (id->command_set_1 & 0x0400) && (id->cfs_enable_1 & 0x0400);
939 static inline int idedisk_supports_lba48(const struct hd_driveid *id)
941 return (id->command_set_2 & 0x0400) && (id->cfs_enable_2 & 0x0400)
942 && id->lba_capacity_2;
945 static inline void idedisk_check_hpa(ide_drive_t *drive)
947 unsigned long long capacity, set_max;
948 int lba48 = idedisk_supports_lba48(drive->id);
950 capacity = drive->capacity64;
952 set_max = idedisk_read_native_max_address_ext(drive);
954 set_max = idedisk_read_native_max_address(drive);
956 if (set_max <= capacity)
959 printk(KERN_INFO "%s: Host Protected Area detected.\n"
960 "\tcurrent capacity is %llu sectors (%llu MB)\n"
961 "\tnative capacity is %llu sectors (%llu MB)\n",
963 capacity, sectors_to_MB(capacity),
964 set_max, sectors_to_MB(set_max));
970 set_max = idedisk_set_max_address_ext(drive, set_max);
972 set_max = idedisk_set_max_address(drive, set_max);
974 drive->capacity64 = set_max;
975 printk(KERN_INFO "%s: Host Protected Area disabled.\n",
981 * Compute drive->capacity, the full capacity of the drive
982 * Called with drive->id != NULL.
984 * To compute capacity, this uses either of
986 * 1. CHS value set by user (whatever user sets will be trusted)
987 * 2. LBA value from target drive (require new ATA feature)
988 * 3. LBA value from system BIOS (new one is OK, old one may break)
989 * 4. CHS value from system BIOS (traditional style)
991 * in above order (i.e., if value of higher priority is available,
992 * reset will be ignored).
994 static void init_idedisk_capacity (ide_drive_t *drive)
996 struct hd_driveid *id = drive->id;
998 * If this drive supports the Host Protected Area feature set,
999 * then we may need to change our opinion about the drive's capacity.
1001 int hpa = idedisk_supports_hpa(id);
1003 if (idedisk_supports_lba48(id)) {
1004 /* drive speaks 48-bit LBA */
1005 drive->select.b.lba = 1;
1006 drive->capacity64 = id->lba_capacity_2;
1008 idedisk_check_hpa(drive);
1009 } else if ((id->capability & 2) && lba_capacity_is_ok(id)) {
1010 /* drive speaks 28-bit LBA */
1011 drive->select.b.lba = 1;
1012 drive->capacity64 = id->lba_capacity;
1014 idedisk_check_hpa(drive);
1016 /* drive speaks boring old 28-bit CHS */
1017 drive->capacity64 = drive->cyl * drive->head * drive->sect;
1021 static sector_t idedisk_capacity (ide_drive_t *drive)
1023 return drive->capacity64 - drive->sect0;
1026 static ide_startstop_t idedisk_special (ide_drive_t *drive)
1028 special_t *s = &drive->special;
1030 if (s->b.set_geometry) {
1031 s->b.set_geometry = 0;
1032 if (!IS_PDC4030_DRIVE) {
1034 memset(&args, 0, sizeof(ide_task_t));
1035 args.tfRegister[IDE_NSECTOR_OFFSET] = drive->sect;
1036 args.tfRegister[IDE_SECTOR_OFFSET] = drive->sect;
1037 args.tfRegister[IDE_LCYL_OFFSET] = drive->cyl;
1038 args.tfRegister[IDE_HCYL_OFFSET] = drive->cyl>>8;
1039 args.tfRegister[IDE_SELECT_OFFSET] = ((drive->head-1)|drive->select.all)&0xBF;
1040 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SPECIFY;
1041 args.command_type = IDE_DRIVE_TASK_NO_DATA;
1042 args.handler = &set_geometry_intr;
1043 do_rw_taskfile(drive, &args);
1045 } else if (s->b.recalibrate) {
1046 s->b.recalibrate = 0;
1047 if (!IS_PDC4030_DRIVE) {
1049 memset(&args, 0, sizeof(ide_task_t));
1050 args.tfRegister[IDE_NSECTOR_OFFSET] = drive->sect;
1051 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_RESTORE;
1052 args.command_type = IDE_DRIVE_TASK_NO_DATA;
1053 args.handler = &recal_intr;
1054 do_rw_taskfile(drive, &args);
1056 } else if (s->b.set_multmode) {
1057 s->b.set_multmode = 0;
1058 if (drive->mult_req > drive->id->max_multsect)
1059 drive->mult_req = drive->id->max_multsect;
1060 if (!IS_PDC4030_DRIVE) {
1062 memset(&args, 0, sizeof(ide_task_t));
1063 args.tfRegister[IDE_NSECTOR_OFFSET] = drive->mult_req;
1064 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SETMULT;
1065 args.command_type = IDE_DRIVE_TASK_NO_DATA;
1066 args.handler = &set_multmode_intr;
1067 do_rw_taskfile(drive, &args);
1069 } else if (s->all) {
1070 int special = s->all;
1072 printk(KERN_ERR "%s: bad special flag: 0x%02x\n", drive->name, special);
1075 return IS_PDC4030_DRIVE ? ide_stopped : ide_started;
1078 static void idedisk_pre_reset (ide_drive_t *drive)
1080 int legacy = (drive->id->cfs_enable_2 & 0x0400) ? 0 : 1;
1082 drive->special.all = 0;
1083 drive->special.b.set_geometry = legacy;
1084 drive->special.b.recalibrate = legacy;
1085 if (OK_TO_RESET_CONTROLLER)
1086 drive->mult_count = 0;
1087 if (!drive->keep_settings && !drive->using_dma)
1088 drive->mult_req = 0;
1089 if (drive->mult_req != drive->mult_count)
1090 drive->special.b.set_multmode = 1;
1093 #ifdef CONFIG_PROC_FS
1095 static int smart_enable(ide_drive_t *drive)
1099 memset(&args, 0, sizeof(ide_task_t));
1100 args.tfRegister[IDE_FEATURE_OFFSET] = SMART_ENABLE;
1101 args.tfRegister[IDE_LCYL_OFFSET] = SMART_LCYL_PASS;
1102 args.tfRegister[IDE_HCYL_OFFSET] = SMART_HCYL_PASS;
1103 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SMART;
1104 args.command_type = IDE_DRIVE_TASK_NO_DATA;
1105 args.handler = &task_no_data_intr;
1106 return ide_raw_taskfile(drive, &args, NULL);
1109 static int get_smart_values(ide_drive_t *drive, u8 *buf)
1113 memset(&args, 0, sizeof(ide_task_t));
1114 args.tfRegister[IDE_FEATURE_OFFSET] = SMART_READ_VALUES;
1115 args.tfRegister[IDE_NSECTOR_OFFSET] = 0x01;
1116 args.tfRegister[IDE_LCYL_OFFSET] = SMART_LCYL_PASS;
1117 args.tfRegister[IDE_HCYL_OFFSET] = SMART_HCYL_PASS;
1118 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SMART;
1119 args.command_type = IDE_DRIVE_TASK_IN;
1120 args.handler = &task_in_intr;
1121 (void) smart_enable(drive);
1122 return ide_raw_taskfile(drive, &args, buf);
1125 static int get_smart_thresholds(ide_drive_t *drive, u8 *buf)
1128 memset(&args, 0, sizeof(ide_task_t));
1129 args.tfRegister[IDE_FEATURE_OFFSET] = SMART_READ_THRESHOLDS;
1130 args.tfRegister[IDE_NSECTOR_OFFSET] = 0x01;
1131 args.tfRegister[IDE_LCYL_OFFSET] = SMART_LCYL_PASS;
1132 args.tfRegister[IDE_HCYL_OFFSET] = SMART_HCYL_PASS;
1133 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SMART;
1134 args.command_type = IDE_DRIVE_TASK_IN;
1135 args.handler = &task_in_intr;
1136 (void) smart_enable(drive);
1137 return ide_raw_taskfile(drive, &args, buf);
1140 static int proc_idedisk_read_cache
1141 (char *page, char **start, off_t off, int count, int *eof, void *data)
1143 ide_drive_t *drive = (ide_drive_t *) data;
1148 len = sprintf(out,"%i\n", drive->id->buf_size / 2);
1150 len = sprintf(out,"(none)\n");
1151 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
1154 static int proc_idedisk_read_smart_thresholds
1155 (char *page, char **start, off_t off, int count, int *eof, void *data)
1157 ide_drive_t *drive = (ide_drive_t *)data;
1160 if (!get_smart_thresholds(drive, page)) {
1161 unsigned short *val = (unsigned short *) page;
1162 char *out = ((char *)val) + (SECTOR_WORDS * 4);
1165 out += sprintf(out, "%04x%c", le16_to_cpu(*val), (++i & 7) ? ' ' : '\n');
1167 } while (i < (SECTOR_WORDS * 2));
1170 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
1173 static int proc_idedisk_read_smart_values
1174 (char *page, char **start, off_t off, int count, int *eof, void *data)
1176 ide_drive_t *drive = (ide_drive_t *)data;
1179 if (!get_smart_values(drive, page)) {
1180 unsigned short *val = (unsigned short *) page;
1181 char *out = ((char *)val) + (SECTOR_WORDS * 4);
1184 out += sprintf(out, "%04x%c", le16_to_cpu(*val), (++i & 7) ? ' ' : '\n');
1186 } while (i < (SECTOR_WORDS * 2));
1189 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
1192 static ide_proc_entry_t idedisk_proc[] = {
1193 { "cache", S_IFREG|S_IRUGO, proc_idedisk_read_cache, NULL },
1194 { "geometry", S_IFREG|S_IRUGO, proc_ide_read_geometry, NULL },
1195 { "smart_values", S_IFREG|S_IRUSR, proc_idedisk_read_smart_values, NULL },
1196 { "smart_thresholds", S_IFREG|S_IRUSR, proc_idedisk_read_smart_thresholds, NULL },
1197 { NULL, 0, NULL, NULL }
1202 #define idedisk_proc NULL
1204 #endif /* CONFIG_PROC_FS */
1207 * This is tightly woven into the driver->do_special can not touch.
1208 * DON'T do it again until a total personality rewrite is committed.
1210 static int set_multcount(ide_drive_t *drive, int arg)
1214 if (drive->special.b.set_multmode)
1216 ide_init_drive_cmd (&rq);
1217 rq.flags = REQ_DRIVE_CMD;
1218 drive->mult_req = arg;
1219 drive->special.b.set_multmode = 1;
1220 (void) ide_do_drive_cmd (drive, &rq, ide_wait);
1221 return (drive->mult_count == arg) ? 0 : -EIO;
1224 static int set_nowerr(ide_drive_t *drive, int arg)
1226 if (ide_spin_wait_hwgroup(drive))
1228 drive->nowerr = arg;
1229 drive->bad_wstat = arg ? BAD_R_STAT : BAD_W_STAT;
1230 spin_unlock_irq(&ide_lock);
1234 /* check if CACHE FLUSH (EXT) command is supported (bits defined in ATA-6) */
1235 #define ide_id_has_flush_cache(id) ((id)->cfs_enable_2 & 0x3000)
1237 /* some Maxtor disks have bit 13 defined incorrectly so check bit 10 too */
1238 #define ide_id_has_flush_cache_ext(id) \
1239 (((id)->cfs_enable_2 & 0x2400) == 0x2400)
1241 static int write_cache (ide_drive_t *drive, int arg)
1245 if (!ide_id_has_flush_cache(drive->id))
1248 memset(&args, 0, sizeof(ide_task_t));
1249 args.tfRegister[IDE_FEATURE_OFFSET] = (arg) ?
1250 SETFEATURES_EN_WCACHE : SETFEATURES_DIS_WCACHE;
1251 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SETFEATURES;
1252 args.command_type = IDE_DRIVE_TASK_NO_DATA;
1253 args.handler = &task_no_data_intr;
1254 (void) ide_raw_taskfile(drive, &args, NULL);
1256 drive->wcache = arg;
1260 static int do_idedisk_flushcache (ide_drive_t *drive)
1264 memset(&args, 0, sizeof(ide_task_t));
1265 if (ide_id_has_flush_cache_ext(drive->id))
1266 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE_EXT;
1268 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE;
1269 args.command_type = IDE_DRIVE_TASK_NO_DATA;
1270 args.handler = &task_no_data_intr;
1271 return ide_raw_taskfile(drive, &args, NULL);
1274 static int set_acoustic (ide_drive_t *drive, int arg)
1278 memset(&args, 0, sizeof(ide_task_t));
1279 args.tfRegister[IDE_FEATURE_OFFSET] = (arg) ? SETFEATURES_EN_AAM :
1280 SETFEATURES_DIS_AAM;
1281 args.tfRegister[IDE_NSECTOR_OFFSET] = arg;
1282 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SETFEATURES;
1283 args.command_type = IDE_DRIVE_TASK_NO_DATA;
1284 args.handler = &task_no_data_intr;
1285 ide_raw_taskfile(drive, &args, NULL);
1286 drive->acoustic = arg;
1291 * drive->addressing:
1294 * 2: 48-bit capable doing 28-bit
1296 static int set_lba_addressing(ide_drive_t *drive, int arg)
1298 drive->addressing = 0;
1300 if (HWIF(drive)->no_lba48)
1303 if (!idedisk_supports_lba48(drive->id))
1305 drive->addressing = arg;
1309 static void idedisk_add_settings(ide_drive_t *drive)
1311 struct hd_driveid *id = drive->id;
1313 ide_add_setting(drive, "bios_cyl", SETTING_RW, -1, -1, TYPE_INT, 0, 65535, 1, 1, &drive->bios_cyl, NULL);
1314 ide_add_setting(drive, "bios_head", SETTING_RW, -1, -1, TYPE_BYTE, 0, 255, 1, 1, &drive->bios_head, NULL);
1315 ide_add_setting(drive, "bios_sect", SETTING_RW, -1, -1, TYPE_BYTE, 0, 63, 1, 1, &drive->bios_sect, NULL);
1316 ide_add_setting(drive, "address", SETTING_RW, HDIO_GET_ADDRESS, HDIO_SET_ADDRESS, TYPE_INTA, 0, 2, 1, 1, &drive->addressing, set_lba_addressing);
1317 ide_add_setting(drive, "bswap", SETTING_READ, -1, -1, TYPE_BYTE, 0, 1, 1, 1, &drive->bswap, NULL);
1318 ide_add_setting(drive, "multcount", id ? SETTING_RW : SETTING_READ, HDIO_GET_MULTCOUNT, HDIO_SET_MULTCOUNT, TYPE_BYTE, 0, id ? id->max_multsect : 0, 1, 1, &drive->mult_count, set_multcount);
1319 ide_add_setting(drive, "nowerr", SETTING_RW, HDIO_GET_NOWERR, HDIO_SET_NOWERR, TYPE_BYTE, 0, 1, 1, 1, &drive->nowerr, set_nowerr);
1320 ide_add_setting(drive, "lun", SETTING_RW, -1, -1, TYPE_INT, 0, 7, 1, 1, &drive->lun, NULL);
1321 ide_add_setting(drive, "wcache", SETTING_RW, HDIO_GET_WCACHE, HDIO_SET_WCACHE, TYPE_BYTE, 0, 1, 1, 1, &drive->wcache, write_cache);
1322 ide_add_setting(drive, "acoustic", SETTING_RW, HDIO_GET_ACOUSTIC, HDIO_SET_ACOUSTIC, TYPE_BYTE, 0, 254, 1, 1, &drive->acoustic, set_acoustic);
1323 ide_add_setting(drive, "failures", SETTING_RW, -1, -1, TYPE_INT, 0, 65535, 1, 1, &drive->failures, NULL);
1324 ide_add_setting(drive, "max_failures", SETTING_RW, -1, -1, TYPE_INT, 0, 65535, 1, 1, &drive->max_failures, NULL);
1328 * Power Management state machine. This one is rather trivial for now,
1329 * we should probably add more, like switching back to PIO on suspend
1330 * to help some BIOSes, re-do the door locking on resume, etc...
1334 idedisk_pm_flush_cache = ide_pm_state_start_suspend,
1337 idedisk_pm_restore_dma = ide_pm_state_start_resume,
1340 static void idedisk_complete_power_step (ide_drive_t *drive, struct request *rq, u8 stat, u8 error)
1342 switch (rq->pm->pm_step) {
1343 case idedisk_pm_flush_cache: /* Suspend step 1 (flush cache) complete */
1344 if (rq->pm->pm_state == 4)
1345 rq->pm->pm_step = ide_pm_state_completed;
1347 rq->pm->pm_step = idedisk_pm_standby;
1349 case idedisk_pm_standby: /* Suspend step 2 (standby) complete */
1350 rq->pm->pm_step = ide_pm_state_completed;
1355 static ide_startstop_t idedisk_start_power_step (ide_drive_t *drive, struct request *rq)
1357 ide_task_t *args = rq->special;
1359 memset(args, 0, sizeof(*args));
1361 switch (rq->pm->pm_step) {
1362 case idedisk_pm_flush_cache: /* Suspend step 1 (flush cache) */
1363 /* Not supported? Switch to next step now. */
1364 if (!drive->wcache || !ide_id_has_flush_cache(drive->id)) {
1365 idedisk_complete_power_step(drive, rq, 0, 0);
1368 if (ide_id_has_flush_cache_ext(drive->id))
1369 args->tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE_EXT;
1371 args->tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE;
1372 args->command_type = IDE_DRIVE_TASK_NO_DATA;
1373 args->handler = &task_no_data_intr;
1374 return do_rw_taskfile(drive, args);
1375 case idedisk_pm_standby: /* Suspend step 2 (standby) */
1376 args->tfRegister[IDE_COMMAND_OFFSET] = WIN_STANDBYNOW1;
1377 args->command_type = IDE_DRIVE_TASK_NO_DATA;
1378 args->handler = &task_no_data_intr;
1379 return do_rw_taskfile(drive, args);
1381 case idedisk_pm_restore_dma: /* Resume step 1 (restore DMA) */
1383 * Right now, all we do is call hwif->ide_dma_check(drive),
1384 * we could be smarter and check for current xfer_speed
1385 * in struct drive etc...
1386 * Also, this step could be implemented as a generic helper
1387 * as most subdrivers will use it
1389 if ((drive->id->capability & 1) == 0)
1391 if (HWIF(drive)->ide_dma_check == NULL)
1393 HWIF(drive)->ide_dma_check(drive);
1396 rq->pm->pm_step = ide_pm_state_completed;
1400 static void idedisk_setup (ide_drive_t *drive)
1402 struct hd_driveid *id = drive->id;
1403 unsigned long long capacity;
1405 idedisk_add_settings(drive);
1407 if (drive->id_read == 0)
1411 * CompactFlash cards and their brethern look just like hard drives
1412 * to us, but they are removable and don't have a doorlock mechanism.
1414 if (drive->removable && !(drive->is_flash)) {
1416 * Removable disks (eg. SYQUEST); ignore 'WD' drives
1418 if (id->model[0] != 'W' || id->model[1] != 'D') {
1419 drive->doorlocking = 1;
1423 (void)set_lba_addressing(drive, 1);
1425 if (drive->addressing == 1) {
1426 ide_hwif_t *hwif = HWIF(drive);
1429 if (max_s > hwif->rqsize)
1430 max_s = hwif->rqsize;
1432 blk_queue_max_sectors(drive->queue, max_s);
1435 printk("%s: max request size: %dKiB\n", drive->name, drive->queue->max_sectors / 2);
1437 /* Extract geometry if we did not already have one for the drive */
1438 if (!drive->cyl || !drive->head || !drive->sect) {
1439 drive->cyl = drive->bios_cyl = id->cyls;
1440 drive->head = drive->bios_head = id->heads;
1441 drive->sect = drive->bios_sect = id->sectors;
1444 /* Handle logical geometry translation by the drive */
1445 if ((id->field_valid & 1) && id->cur_cyls &&
1446 id->cur_heads && (id->cur_heads <= 16) && id->cur_sectors) {
1447 drive->cyl = id->cur_cyls;
1448 drive->head = id->cur_heads;
1449 drive->sect = id->cur_sectors;
1452 /* Use physical geometry if what we have still makes no sense */
1453 if (drive->head > 16 && id->heads && id->heads <= 16) {
1454 drive->cyl = id->cyls;
1455 drive->head = id->heads;
1456 drive->sect = id->sectors;
1459 /* calculate drive capacity, and select LBA if possible */
1460 init_idedisk_capacity (drive);
1462 /* limit drive capacity to 137GB if LBA48 cannot be used */
1463 if (drive->addressing == 0 && drive->capacity64 > 1ULL << 28) {
1464 printk("%s: cannot use LBA48 - full capacity "
1465 "%llu sectors (%llu MB)\n",
1466 drive->name, (unsigned long long)drive->capacity64,
1467 sectors_to_MB(drive->capacity64));
1468 drive->capacity64 = 1ULL << 28;
1472 * if possible, give fdisk access to more of the drive,
1473 * by correcting bios_cyls:
1475 capacity = idedisk_capacity (drive);
1476 if (!drive->forced_geom) {
1478 if (idedisk_supports_lba48(drive->id)) {
1480 drive->bios_sect = 63;
1481 drive->bios_head = 255;
1484 if (drive->bios_sect && drive->bios_head) {
1485 unsigned int cap0 = capacity; /* truncate to 32 bits */
1486 unsigned int cylsz, cyl;
1488 if (cap0 != capacity)
1489 drive->bios_cyl = 65535;
1491 cylsz = drive->bios_sect * drive->bios_head;
1495 if (cyl > drive->bios_cyl)
1496 drive->bios_cyl = cyl;
1500 printk(KERN_INFO "%s: %llu sectors (%llu MB)",
1501 drive->name, capacity, sectors_to_MB(capacity));
1503 /* Only print cache size when it was specified */
1505 printk (" w/%dKiB Cache", id->buf_size/2);
1507 printk(", CHS=%d/%d/%d",
1508 drive->bios_cyl, drive->bios_head, drive->bios_sect);
1509 if (drive->using_dma)
1510 (void) HWIF(drive)->ide_dma_verbose(drive);
1513 drive->mult_count = 0;
1514 if (id->max_multsect) {
1515 #ifdef CONFIG_IDEDISK_MULTI_MODE
1516 id->multsect = ((id->max_multsect/2) > 1) ? id->max_multsect : 0;
1517 id->multsect_valid = id->multsect ? 1 : 0;
1518 drive->mult_req = id->multsect_valid ? id->max_multsect : INITIAL_MULT_COUNT;
1519 drive->special.b.set_multmode = drive->mult_req ? 1 : 0;
1520 #else /* original, pre IDE-NFG, per request of AC */
1521 drive->mult_req = INITIAL_MULT_COUNT;
1522 if (drive->mult_req > id->max_multsect)
1523 drive->mult_req = id->max_multsect;
1524 if (drive->mult_req || ((id->multsect_valid & 1) && id->multsect))
1525 drive->special.b.set_multmode = 1;
1526 #endif /* CONFIG_IDEDISK_MULTI_MODE */
1528 drive->no_io_32bit = id->dword_io ? 1 : 0;
1530 /* write cache enabled? */
1531 if ((id->csfo & 1) || (id->cfs_enable_1 & (1 << 5)))
1534 write_cache(drive, 1);
1537 static void ide_cacheflush_p(ide_drive_t *drive)
1539 if (!drive->wcache || !ide_id_has_flush_cache(drive->id))
1542 if (do_idedisk_flushcache(drive))
1543 printk(KERN_INFO "%s: wcache flush failed!\n", drive->name);
1546 static int idedisk_cleanup (ide_drive_t *drive)
1548 struct gendisk *g = drive->disk;
1549 ide_cacheflush_p(drive);
1550 if (ide_unregister_subdriver(drive))
1553 drive->devfs_name[0] = '\0';
1558 static int idedisk_attach(ide_drive_t *drive);
1560 static void ide_device_shutdown(struct device *dev)
1562 ide_drive_t *drive = container_of(dev, ide_drive_t, gendev);
1565 /* On Alpha, halt(8) doesn't actually turn the machine off,
1566 it puts you into the sort of firmware monitor. Typically,
1567 it's used to boot another kernel image, so it's not much
1568 different from reboot(8). Therefore, we don't need to
1569 spin down the disk in this case, especially since Alpha
1570 firmware doesn't handle disks in standby mode properly.
1571 On the other hand, it's reasonably safe to turn the power
1572 off when the shutdown process reaches the firmware prompt,
1573 as the firmware initialization takes rather long time -
1574 at least 10 seconds, which should be sufficient for
1575 the disk to expire its write cache. */
1576 if (system_state != SYSTEM_POWER_OFF) {
1578 if (system_state == SYSTEM_RESTART) {
1580 ide_cacheflush_p(drive);
1584 printk("Shutdown: %s\n", drive->name);
1585 dev->bus->suspend(dev, PM_SUSPEND_STANDBY);
1589 * IDE subdriver functions, registered with ide.c
1591 static ide_driver_t idedisk_driver = {
1592 .owner = THIS_MODULE,
1594 .shutdown = ide_device_shutdown,
1597 .version = IDEDISK_VERSION,
1600 .supports_dsc_overlap = 0,
1601 .cleanup = idedisk_cleanup,
1602 .do_request = ide_do_rw_disk,
1603 .sense = idedisk_dump_status,
1604 .error = idedisk_error,
1605 .abort = idedisk_abort,
1606 .pre_reset = idedisk_pre_reset,
1607 .capacity = idedisk_capacity,
1608 .special = idedisk_special,
1609 .proc = idedisk_proc,
1610 .attach = idedisk_attach,
1611 .drives = LIST_HEAD_INIT(idedisk_driver.drives),
1612 .start_power_step = idedisk_start_power_step,
1613 .complete_power_step = idedisk_complete_power_step,
1616 static int idedisk_open(struct inode *inode, struct file *filp)
1618 ide_drive_t *drive = inode->i_bdev->bd_disk->private_data;
1620 if (drive->removable && drive->usage == 1) {
1622 memset(&args, 0, sizeof(ide_task_t));
1623 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_DOORLOCK;
1624 args.command_type = IDE_DRIVE_TASK_NO_DATA;
1625 args.handler = &task_no_data_intr;
1626 check_disk_change(inode->i_bdev);
1628 * Ignore the return code from door_lock,
1629 * since the open() has already succeeded,
1630 * and the door_lock is irrelevant at this point.
1632 if (drive->doorlocking && ide_raw_taskfile(drive, &args, NULL))
1633 drive->doorlocking = 0;
1638 static int idedisk_release(struct inode *inode, struct file *filp)
1640 ide_drive_t *drive = inode->i_bdev->bd_disk->private_data;
1641 if (drive->usage == 1)
1642 ide_cacheflush_p(drive);
1643 if (drive->removable && drive->usage == 1) {
1645 memset(&args, 0, sizeof(ide_task_t));
1646 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_DOORUNLOCK;
1647 args.command_type = IDE_DRIVE_TASK_NO_DATA;
1648 args.handler = &task_no_data_intr;
1649 if (drive->doorlocking && ide_raw_taskfile(drive, &args, NULL))
1650 drive->doorlocking = 0;
1656 static int idedisk_ioctl(struct inode *inode, struct file *file,
1657 unsigned int cmd, unsigned long arg)
1659 struct block_device *bdev = inode->i_bdev;
1660 return generic_ide_ioctl(bdev, cmd, arg);
1663 static int idedisk_media_changed(struct gendisk *disk)
1665 ide_drive_t *drive = disk->private_data;
1667 /* do not scan partitions twice if this is a removable device */
1668 if (drive->attach) {
1672 /* if removable, always assume it was changed */
1673 return drive->removable;
1676 static int idedisk_revalidate_disk(struct gendisk *disk)
1678 ide_drive_t *drive = disk->private_data;
1679 set_capacity(disk, current_capacity(drive));
1683 static struct block_device_operations idedisk_ops = {
1684 .owner = THIS_MODULE,
1685 .open = idedisk_open,
1686 .release = idedisk_release,
1687 .ioctl = idedisk_ioctl,
1688 .media_changed = idedisk_media_changed,
1689 .revalidate_disk= idedisk_revalidate_disk
1692 MODULE_DESCRIPTION("ATA DISK Driver");
1694 static int idedisk_attach(ide_drive_t *drive)
1696 struct gendisk *g = drive->disk;
1698 /* strstr("foo", "") is non-NULL */
1699 if (!strstr("ide-disk", drive->driver_req))
1701 if (!drive->present)
1703 if (drive->media != ide_disk)
1706 if (ide_register_subdriver(drive, &idedisk_driver)) {
1707 printk (KERN_ERR "ide-disk: %s: Failed to register the driver with ide.c\n", drive->name);
1710 DRIVER(drive)->busy++;
1711 idedisk_setup(drive);
1712 if ((!drive->head || drive->head > 16) && !drive->select.b.lba) {
1713 printk(KERN_ERR "%s: INVALID GEOMETRY: %d PHYSICAL HEADS?\n",
1714 drive->name, drive->head);
1715 ide_cacheflush_p(drive);
1716 ide_unregister_subdriver(drive);
1717 DRIVER(drive)->busy--;
1720 DRIVER(drive)->busy--;
1721 g->minors = 1 << PARTN_BITS;
1722 strcpy(g->devfs_name, drive->devfs_name);
1723 g->driverfs_dev = &drive->gendev;
1724 g->flags = drive->removable ? GENHD_FL_REMOVABLE : 0;
1725 set_capacity(g, current_capacity(drive));
1726 g->fops = &idedisk_ops;
1734 static void __exit idedisk_exit (void)
1736 ide_unregister_driver(&idedisk_driver);
1739 static int idedisk_init (void)
1741 return ide_register_driver(&idedisk_driver);
1744 module_init(idedisk_init);
1745 module_exit(idedisk_exit);
1746 MODULE_LICENSE("GPL");