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 unsigned int dma = drive->using_dma;
344 u8 lba48 = (drive->addressing == 1) ? 1 : 0;
345 task_ioreg_t command = WIN_NOP;
346 ata_nsector_t nsectors;
348 nsectors.all = (u16) rq->nr_sectors;
350 if (hwif->no_lba48_dma && lba48 && dma) {
351 if (rq->sector + rq->nr_sectors > 1ULL << 28)
356 hwif->OUTB(drive->ctl, IDE_CONTROL_REG);
358 if (drive->select.b.lba) {
359 if (drive->addressing == 1) {
360 task_ioreg_t tasklets[10];
362 pr_debug("%s: LBA=0x%012llx\n", drive->name, block);
366 tasklets[2] = nsectors.b.low;
367 tasklets[3] = nsectors.b.high;
368 tasklets[4] = (task_ioreg_t) block;
369 tasklets[5] = (task_ioreg_t) (block>>8);
370 tasklets[6] = (task_ioreg_t) (block>>16);
371 tasklets[7] = (task_ioreg_t) (block>>24);
372 if (sizeof(block) == 4) {
373 tasklets[8] = (task_ioreg_t) 0;
374 tasklets[9] = (task_ioreg_t) 0;
376 tasklets[8] = (task_ioreg_t)((u64)block >> 32);
377 tasklets[9] = (task_ioreg_t)((u64)block >> 40);
380 printk("%s: 0x%02x%02x 0x%02x%02x%02x%02x%02x%02x\n",
381 drive->name, tasklets[3], tasklets[2],
382 tasklets[9], tasklets[8], tasklets[7],
383 tasklets[6], tasklets[5], tasklets[4]);
385 hwif->OUTB(tasklets[1], IDE_FEATURE_REG);
386 hwif->OUTB(tasklets[3], IDE_NSECTOR_REG);
387 hwif->OUTB(tasklets[7], IDE_SECTOR_REG);
388 hwif->OUTB(tasklets[8], IDE_LCYL_REG);
389 hwif->OUTB(tasklets[9], IDE_HCYL_REG);
391 hwif->OUTB(tasklets[0], IDE_FEATURE_REG);
392 hwif->OUTB(tasklets[2], IDE_NSECTOR_REG);
393 hwif->OUTB(tasklets[4], IDE_SECTOR_REG);
394 hwif->OUTB(tasklets[5], IDE_LCYL_REG);
395 hwif->OUTB(tasklets[6], IDE_HCYL_REG);
396 hwif->OUTB(0x00|drive->select.all,IDE_SELECT_REG);
398 hwif->OUTB(0x00, IDE_FEATURE_REG);
399 hwif->OUTB(nsectors.b.low, IDE_NSECTOR_REG);
400 hwif->OUTB(block, IDE_SECTOR_REG);
401 hwif->OUTB(block>>=8, IDE_LCYL_REG);
402 hwif->OUTB(block>>=8, IDE_HCYL_REG);
403 hwif->OUTB(((block>>8)&0x0f)|drive->select.all,IDE_SELECT_REG);
406 unsigned int sect,head,cyl,track;
407 track = (int)block / drive->sect;
408 sect = (int)block % drive->sect + 1;
409 hwif->OUTB(sect, IDE_SECTOR_REG);
410 head = track % drive->head;
411 cyl = track / drive->head;
413 pr_debug("%s: CHS=%u/%u/%u\n", drive->name, cyl, head, sect);
415 hwif->OUTB(0x00, IDE_FEATURE_REG);
416 hwif->OUTB(nsectors.b.low, IDE_NSECTOR_REG);
417 hwif->OUTB(cyl, IDE_LCYL_REG);
418 hwif->OUTB(cyl>>8, IDE_HCYL_REG);
419 hwif->OUTB(head|drive->select.all,IDE_SELECT_REG);
422 if (rq_data_dir(rq) == READ) {
423 if (dma && !hwif->ide_dma_read(drive))
426 command = ((drive->mult_count) ?
427 ((lba48) ? WIN_MULTREAD_EXT : WIN_MULTREAD) :
428 ((lba48) ? WIN_READ_EXT : WIN_READ));
429 ide_execute_command(drive, command, &read_intr, WAIT_CMD, NULL);
432 ide_startstop_t startstop;
434 if (dma && !hwif->ide_dma_write(drive))
437 command = ((drive->mult_count) ?
438 ((lba48) ? WIN_MULTWRITE_EXT : WIN_MULTWRITE) :
439 ((lba48) ? WIN_WRITE_EXT : WIN_WRITE));
440 hwif->OUTB(command, IDE_COMMAND_REG);
442 if (ide_wait_stat(&startstop, drive, DATA_READY,
443 drive->bad_wstat, WAIT_DRQ)) {
444 printk(KERN_ERR "%s: no DRQ after issuing %s\n",
446 drive->mult_count ? "MULTWRITE" : "WRITE");
451 if (drive->mult_count) {
452 ide_hwgroup_t *hwgroup = HWGROUP(drive);
454 hwgroup->wrq = *rq; /* scratchpad */
455 ide_set_handler(drive, &multwrite_intr, WAIT_CMD, NULL);
456 ide_multwrite(drive, drive->mult_count);
459 char *to = ide_map_buffer(rq, &flags);
460 ide_set_handler(drive, &write_intr, WAIT_CMD, NULL);
461 taskfile_output_data(drive, to, SECTOR_WORDS);
462 ide_unmap_buffer(rq, to, &flags);
467 EXPORT_SYMBOL_GPL(__ide_do_rw_disk);
469 #else /* CONFIG_IDE_TASKFILE_IO */
471 static ide_startstop_t chs_rw_disk(ide_drive_t *, struct request *, unsigned long);
472 static ide_startstop_t lba_28_rw_disk(ide_drive_t *, struct request *, unsigned long);
473 static ide_startstop_t lba_48_rw_disk(ide_drive_t *, struct request *, unsigned long long);
476 * __ide_do_rw_disk() issues READ and WRITE commands to a disk,
477 * using LBA if supported, or CHS otherwise, to address sectors.
478 * It also takes care of issuing special DRIVE_CMDs.
480 ide_startstop_t __ide_do_rw_disk (ide_drive_t *drive, struct request *rq, sector_t block)
483 * 268435455 == 137439 MB or 28bit limit
485 * need to add split taskfile operations based on 28bit threshold.
487 if (drive->addressing == 1) /* 48-bit LBA */
488 return lba_48_rw_disk(drive, rq, (unsigned long long) block);
489 if (drive->select.b.lba) /* 28-bit LBA */
490 return lba_28_rw_disk(drive, rq, (unsigned long) block);
492 /* 28-bit CHS : DIE DIE DIE piece of legacy crap!!! */
493 return chs_rw_disk(drive, rq, (unsigned long) block);
495 EXPORT_SYMBOL_GPL(__ide_do_rw_disk);
497 static u8 get_command(ide_drive_t *drive, struct request *rq, ide_task_t *task)
499 unsigned int lba48 = (drive->addressing == 1) ? 1 : 0;
500 unsigned int dma = drive->using_dma;
502 if (drive->hwif->no_lba48_dma && lba48 && dma) {
503 if (rq->sector + rq->nr_sectors > 1ULL << 28)
507 if (rq_data_dir(rq) == READ) {
508 task->command_type = IDE_DRIVE_TASK_IN;
510 return lba48 ? WIN_READDMA_EXT : WIN_READDMA;
511 task->handler = &task_in_intr;
512 if (drive->mult_count) {
513 task->data_phase = TASKFILE_MULTI_IN;
514 return lba48 ? WIN_MULTREAD_EXT : WIN_MULTREAD;
516 task->data_phase = TASKFILE_IN;
517 return lba48 ? WIN_READ_EXT : WIN_READ;
519 task->command_type = IDE_DRIVE_TASK_RAW_WRITE;
521 return lba48 ? WIN_WRITEDMA_EXT : WIN_WRITEDMA;
522 task->prehandler = &pre_task_out_intr;
523 task->handler = &task_out_intr;
524 if (drive->mult_count) {
525 task->data_phase = TASKFILE_MULTI_OUT;
526 return lba48 ? WIN_MULTWRITE_EXT : WIN_MULTWRITE;
528 task->data_phase = TASKFILE_OUT;
529 return lba48 ? WIN_WRITE_EXT : WIN_WRITE;
533 static ide_startstop_t chs_rw_disk (ide_drive_t *drive, struct request *rq, unsigned long block)
537 ata_nsector_t nsectors;
538 unsigned int track = (block / drive->sect);
539 unsigned int sect = (block % drive->sect) + 1;
540 unsigned int head = (track % drive->head);
541 unsigned int cyl = (track / drive->head);
543 nsectors.all = (u16) rq->nr_sectors;
545 pr_debug("%s: CHS=%u/%u/%u\n", drive->name, cyl, head, sect);
547 memset(&args, 0, sizeof(ide_task_t));
549 sectors = (rq->nr_sectors == 256) ? 0x00 : rq->nr_sectors;
551 args.tfRegister[IDE_NSECTOR_OFFSET] = sectors;
552 args.tfRegister[IDE_SECTOR_OFFSET] = sect;
553 args.tfRegister[IDE_LCYL_OFFSET] = cyl;
554 args.tfRegister[IDE_HCYL_OFFSET] = (cyl>>8);
555 args.tfRegister[IDE_SELECT_OFFSET] = head;
556 args.tfRegister[IDE_SELECT_OFFSET] |= drive->select.all;
557 args.tfRegister[IDE_COMMAND_OFFSET] = get_command(drive, rq, &args);
558 args.rq = (struct request *) rq;
559 rq->special = (ide_task_t *)&args;
560 drive->hwif->data_phase = args.data_phase;
561 return do_rw_taskfile(drive, &args);
564 static ide_startstop_t lba_28_rw_disk (ide_drive_t *drive, struct request *rq, unsigned long block)
568 ata_nsector_t nsectors;
570 nsectors.all = (u16) rq->nr_sectors;
572 memset(&args, 0, sizeof(ide_task_t));
574 sectors = (rq->nr_sectors == 256) ? 0x00 : rq->nr_sectors;
576 args.tfRegister[IDE_NSECTOR_OFFSET] = sectors;
577 args.tfRegister[IDE_SECTOR_OFFSET] = block;
578 args.tfRegister[IDE_LCYL_OFFSET] = (block>>=8);
579 args.tfRegister[IDE_HCYL_OFFSET] = (block>>=8);
580 args.tfRegister[IDE_SELECT_OFFSET] = ((block>>8)&0x0f);
581 args.tfRegister[IDE_SELECT_OFFSET] |= drive->select.all;
582 args.tfRegister[IDE_COMMAND_OFFSET] = get_command(drive, rq, &args);
583 args.rq = (struct request *) rq;
584 rq->special = (ide_task_t *)&args;
585 drive->hwif->data_phase = args.data_phase;
586 return do_rw_taskfile(drive, &args);
590 * 268435455 == 137439 MB or 28bit limit
591 * 320173056 == 163929 MB or 48bit addressing
592 * 1073741822 == 549756 MB or 48bit addressing fake drive
595 static ide_startstop_t lba_48_rw_disk (ide_drive_t *drive, struct request *rq, unsigned long long block)
599 ata_nsector_t nsectors;
601 nsectors.all = (u16) rq->nr_sectors;
603 memset(&args, 0, sizeof(ide_task_t));
605 sectors = (rq->nr_sectors == 65536) ? 0 : rq->nr_sectors;
607 args.tfRegister[IDE_NSECTOR_OFFSET] = sectors;
608 args.hobRegister[IDE_NSECTOR_OFFSET] = sectors >> 8;
609 args.tfRegister[IDE_SECTOR_OFFSET] = block; /* low lba */
610 args.tfRegister[IDE_LCYL_OFFSET] = (block>>=8); /* mid lba */
611 args.tfRegister[IDE_HCYL_OFFSET] = (block>>=8); /* hi lba */
612 args.tfRegister[IDE_SELECT_OFFSET] = drive->select.all;
613 args.tfRegister[IDE_COMMAND_OFFSET] = get_command(drive, rq, &args);
614 args.hobRegister[IDE_SECTOR_OFFSET] = (block>>=8); /* low lba */
615 args.hobRegister[IDE_LCYL_OFFSET] = (block>>=8); /* mid lba */
616 args.hobRegister[IDE_HCYL_OFFSET] = (block>>=8); /* hi lba */
617 args.hobRegister[IDE_SELECT_OFFSET] = drive->select.all;
618 args.hobRegister[IDE_CONTROL_OFFSET_HOB]= (drive->ctl|0x80);
619 args.rq = (struct request *) rq;
620 rq->special = (ide_task_t *)&args;
621 drive->hwif->data_phase = args.data_phase;
622 return do_rw_taskfile(drive, &args);
625 #endif /* CONFIG_IDE_TASKFILE_IO */
627 static ide_startstop_t ide_do_rw_disk (ide_drive_t *drive, struct request *rq, sector_t block)
629 ide_hwif_t *hwif = HWIF(drive);
631 BUG_ON(drive->blocked);
633 if (!blk_fs_request(rq)) {
634 blk_dump_rq_flags(rq, "ide_do_rw_disk - bad command");
635 ide_end_request(drive, 0, 0);
639 pr_debug("%s: %sing: block=%llu, sectors=%lu, buffer=0x%08lx\n",
640 drive->name, rq_data_dir(rq) == READ ? "read" : "writ",
641 block, rq->nr_sectors, (unsigned long)rq->buffer);
644 return hwif->rw_disk(drive, rq, block);
646 return __ide_do_rw_disk(drive, rq, block);
649 static u8 idedisk_dump_status (ide_drive_t *drive, const char *msg, u8 stat)
651 ide_hwif_t *hwif = HWIF(drive);
655 local_irq_set(flags);
656 printk("%s: %s: status=0x%02x", drive->name, msg, stat);
658 if (stat & BUSY_STAT)
661 if (stat & READY_STAT) printk("DriveReady ");
662 if (stat & WRERR_STAT) printk("DeviceFault ");
663 if (stat & SEEK_STAT) printk("SeekComplete ");
664 if (stat & DRQ_STAT) printk("DataRequest ");
665 if (stat & ECC_STAT) printk("CorrectedError ");
666 if (stat & INDEX_STAT) printk("Index ");
667 if (stat & ERR_STAT) printk("Error ");
671 if ((stat & (BUSY_STAT|ERR_STAT)) == ERR_STAT) {
672 err = hwif->INB(IDE_ERROR_REG);
673 printk("%s: %s: error=0x%02x", drive->name, msg, err);
675 if (err & ABRT_ERR) printk("DriveStatusError ");
677 printk("Bad%s ", (err & ABRT_ERR) ? "CRC" : "Sector");
678 if (err & ECC_ERR) printk("UncorrectableError ");
679 if (err & ID_ERR) printk("SectorIdNotFound ");
680 if (err & TRK0_ERR) printk("TrackZeroNotFound ");
681 if (err & MARK_ERR) printk("AddrMarkNotFound ");
683 if ((err & (BBD_ERR | ABRT_ERR)) == BBD_ERR ||
684 (err & (ECC_ERR|ID_ERR|MARK_ERR))) {
685 if (drive->addressing == 1) {
687 u32 low = 0, high = 0;
688 low = ide_read_24(drive);
689 hwif->OUTB(drive->ctl|0x80, IDE_CONTROL_REG);
690 high = ide_read_24(drive);
691 sectors = ((__u64)high << 24) | low;
692 printk(", LBAsect=%llu, high=%d, low=%d",
693 (unsigned long long) sectors,
696 u8 cur = hwif->INB(IDE_SELECT_REG);
697 if (cur & 0x40) { /* using LBA? */
698 printk(", LBAsect=%ld", (unsigned long)
700 |(hwif->INB(IDE_HCYL_REG)<<16)
701 |(hwif->INB(IDE_LCYL_REG)<<8)
702 | hwif->INB(IDE_SECTOR_REG));
704 printk(", CHS=%d/%d/%d",
705 (hwif->INB(IDE_HCYL_REG)<<8) +
706 hwif->INB(IDE_LCYL_REG),
708 hwif->INB(IDE_SECTOR_REG));
711 if (HWGROUP(drive) && HWGROUP(drive)->rq)
712 printk(", sector=%llu",
713 (unsigned long long)HWGROUP(drive)->rq->sector);
719 unsigned char opcode = 0;
722 spin_lock(&ide_lock);
723 rq = HWGROUP(drive)->rq;
724 spin_unlock(&ide_lock);
727 if (rq->flags & (REQ_DRIVE_CMD | REQ_DRIVE_TASK)) {
728 char *args = rq->buffer;
733 } else if (rq->flags & REQ_DRIVE_TASKFILE) {
734 ide_task_t *args = rq->special;
736 task_struct_t *tf = (task_struct_t *) args->tfRegister;
737 opcode = tf->command;
741 printk("ide: failed opcode was: ");
745 printk("0x%02x\n", opcode);
748 local_irq_restore(flags);
752 ide_startstop_t idedisk_error (ide_drive_t *drive, const char *msg, u8 stat)
758 err = idedisk_dump_status(drive, msg, stat);
760 if (drive == NULL || (rq = HWGROUP(drive)->rq) == NULL)
764 /* retry only "normal" I/O: */
765 if (rq->flags & (REQ_DRIVE_CMD | REQ_DRIVE_TASK | REQ_DRIVE_TASKFILE)) {
767 ide_end_drive_cmd(drive, stat, err);
770 #ifdef CONFIG_IDE_TASKFILE_IO
771 /* make rq completion pointers new submission pointers */
772 blk_rq_prep_restart(rq);
775 if (stat & BUSY_STAT || ((stat & WRERR_STAT) && !drive->nowerr)) {
776 /* other bits are useless when BUSY */
777 rq->errors |= ERROR_RESET;
778 } else if (stat & ERR_STAT) {
779 /* err has different meaning on cdrom and tape */
780 if (err == ABRT_ERR) {
781 if (drive->select.b.lba &&
782 /* some newer drives don't support WIN_SPECIFY */
783 hwif->INB(IDE_COMMAND_REG) == WIN_SPECIFY)
785 } else if ((err & BAD_CRC) == BAD_CRC) {
786 /* UDMA crc error, just retry the operation */
788 } else if (err & (BBD_ERR | ECC_ERR)) {
789 /* retries won't help these */
790 rq->errors = ERROR_MAX;
791 } else if (err & TRK0_ERR) {
792 /* help it find track zero */
793 rq->errors |= ERROR_RECAL;
796 if ((stat & DRQ_STAT) && rq_data_dir(rq) == READ)
797 try_to_flush_leftover_data(drive);
798 if (hwif->INB(IDE_STATUS_REG) & (BUSY_STAT|DRQ_STAT)) {
800 hwif->OUTB(WIN_IDLEIMMEDIATE,IDE_COMMAND_REG);
802 if (rq->errors >= ERROR_MAX || blk_noretry_request(rq))
803 DRIVER(drive)->end_request(drive, 0, 0);
805 if ((rq->errors & ERROR_RESET) == ERROR_RESET) {
807 return ide_do_reset(drive);
809 if ((rq->errors & ERROR_RECAL) == ERROR_RECAL)
810 drive->special.b.recalibrate = 1;
816 ide_startstop_t idedisk_abort(ide_drive_t *drive, const char *msg)
821 if (drive == NULL || (rq = HWGROUP(drive)->rq) == NULL)
826 if (rq->flags & (REQ_DRIVE_CMD | REQ_DRIVE_TASK | REQ_DRIVE_TASKFILE)) {
828 ide_end_drive_cmd(drive, BUSY_STAT, 0);
832 DRIVER(drive)->end_request(drive, 0, 0);
837 * Queries for true maximum capacity of the drive.
838 * Returns maximum LBA address (> 0) of the drive, 0 if failed.
840 static unsigned long idedisk_read_native_max_address(ide_drive_t *drive)
843 unsigned long addr = 0;
845 /* Create IDE/ATA command request structure */
846 memset(&args, 0, sizeof(ide_task_t));
847 args.tfRegister[IDE_SELECT_OFFSET] = 0x40;
848 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_READ_NATIVE_MAX;
849 args.command_type = IDE_DRIVE_TASK_NO_DATA;
850 args.handler = &task_no_data_intr;
851 /* submit command request */
852 ide_raw_taskfile(drive, &args, NULL);
854 /* if OK, compute maximum address value */
855 if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
856 addr = ((args.tfRegister[IDE_SELECT_OFFSET] & 0x0f) << 24)
857 | ((args.tfRegister[ IDE_HCYL_OFFSET] ) << 16)
858 | ((args.tfRegister[ IDE_LCYL_OFFSET] ) << 8)
859 | ((args.tfRegister[IDE_SECTOR_OFFSET] ));
860 addr++; /* since the return value is (maxlba - 1), we add 1 */
865 static unsigned long long idedisk_read_native_max_address_ext(ide_drive_t *drive)
868 unsigned long long addr = 0;
870 /* Create IDE/ATA command request structure */
871 memset(&args, 0, sizeof(ide_task_t));
873 args.tfRegister[IDE_SELECT_OFFSET] = 0x40;
874 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_READ_NATIVE_MAX_EXT;
875 args.command_type = IDE_DRIVE_TASK_NO_DATA;
876 args.handler = &task_no_data_intr;
877 /* submit command request */
878 ide_raw_taskfile(drive, &args, NULL);
880 /* if OK, compute maximum address value */
881 if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
882 u32 high = (args.hobRegister[IDE_HCYL_OFFSET] << 16) |
883 (args.hobRegister[IDE_LCYL_OFFSET] << 8) |
884 args.hobRegister[IDE_SECTOR_OFFSET];
885 u32 low = ((args.tfRegister[IDE_HCYL_OFFSET])<<16) |
886 ((args.tfRegister[IDE_LCYL_OFFSET])<<8) |
887 (args.tfRegister[IDE_SECTOR_OFFSET]);
888 addr = ((__u64)high << 24) | low;
889 addr++; /* since the return value is (maxlba - 1), we add 1 */
895 * Sets maximum virtual LBA address of the drive.
896 * Returns new maximum virtual LBA address (> 0) or 0 on failure.
898 static unsigned long idedisk_set_max_address(ide_drive_t *drive, unsigned long addr_req)
901 unsigned long addr_set = 0;
904 /* Create IDE/ATA command request structure */
905 memset(&args, 0, sizeof(ide_task_t));
906 args.tfRegister[IDE_SECTOR_OFFSET] = ((addr_req >> 0) & 0xff);
907 args.tfRegister[IDE_LCYL_OFFSET] = ((addr_req >> 8) & 0xff);
908 args.tfRegister[IDE_HCYL_OFFSET] = ((addr_req >> 16) & 0xff);
909 args.tfRegister[IDE_SELECT_OFFSET] = ((addr_req >> 24) & 0x0f) | 0x40;
910 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SET_MAX;
911 args.command_type = IDE_DRIVE_TASK_NO_DATA;
912 args.handler = &task_no_data_intr;
913 /* submit command request */
914 ide_raw_taskfile(drive, &args, NULL);
915 /* if OK, read new maximum address value */
916 if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
917 addr_set = ((args.tfRegister[IDE_SELECT_OFFSET] & 0x0f) << 24)
918 | ((args.tfRegister[ IDE_HCYL_OFFSET] ) << 16)
919 | ((args.tfRegister[ IDE_LCYL_OFFSET] ) << 8)
920 | ((args.tfRegister[IDE_SECTOR_OFFSET] ));
926 static unsigned long long idedisk_set_max_address_ext(ide_drive_t *drive, unsigned long long addr_req)
929 unsigned long long addr_set = 0;
932 /* Create IDE/ATA command request structure */
933 memset(&args, 0, sizeof(ide_task_t));
934 args.tfRegister[IDE_SECTOR_OFFSET] = ((addr_req >> 0) & 0xff);
935 args.tfRegister[IDE_LCYL_OFFSET] = ((addr_req >>= 8) & 0xff);
936 args.tfRegister[IDE_HCYL_OFFSET] = ((addr_req >>= 8) & 0xff);
937 args.tfRegister[IDE_SELECT_OFFSET] = 0x40;
938 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SET_MAX_EXT;
939 args.hobRegister[IDE_SECTOR_OFFSET] = (addr_req >>= 8) & 0xff;
940 args.hobRegister[IDE_LCYL_OFFSET] = (addr_req >>= 8) & 0xff;
941 args.hobRegister[IDE_HCYL_OFFSET] = (addr_req >>= 8) & 0xff;
942 args.hobRegister[IDE_SELECT_OFFSET] = 0x40;
943 args.hobRegister[IDE_CONTROL_OFFSET_HOB]= (drive->ctl|0x80);
944 args.command_type = IDE_DRIVE_TASK_NO_DATA;
945 args.handler = &task_no_data_intr;
946 /* submit command request */
947 ide_raw_taskfile(drive, &args, NULL);
948 /* if OK, compute maximum address value */
949 if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
950 u32 high = (args.hobRegister[IDE_HCYL_OFFSET] << 16) |
951 (args.hobRegister[IDE_LCYL_OFFSET] << 8) |
952 args.hobRegister[IDE_SECTOR_OFFSET];
953 u32 low = ((args.tfRegister[IDE_HCYL_OFFSET])<<16) |
954 ((args.tfRegister[IDE_LCYL_OFFSET])<<8) |
955 (args.tfRegister[IDE_SECTOR_OFFSET]);
956 addr_set = ((__u64)high << 24) | low;
962 static unsigned long long sectors_to_MB(unsigned long long n)
964 n <<= 9; /* make it bytes */
965 do_div(n, 1000000); /* make it MB */
970 * Bits 10 of command_set_1 and cfs_enable_1 must be equal,
971 * so on non-buggy drives we need test only one.
972 * However, we should also check whether these fields are valid.
974 static inline int idedisk_supports_hpa(const struct hd_driveid *id)
976 return (id->command_set_1 & 0x0400) && (id->cfs_enable_1 & 0x0400);
982 static inline int idedisk_supports_lba48(const struct hd_driveid *id)
984 return (id->command_set_2 & 0x0400) && (id->cfs_enable_2 & 0x0400)
985 && id->lba_capacity_2;
988 static inline void idedisk_check_hpa(ide_drive_t *drive)
990 unsigned long long capacity, set_max;
991 int lba48 = idedisk_supports_lba48(drive->id);
993 capacity = drive->capacity64;
995 set_max = idedisk_read_native_max_address_ext(drive);
997 set_max = idedisk_read_native_max_address(drive);
999 if (set_max <= capacity)
1002 printk(KERN_INFO "%s: Host Protected Area detected.\n"
1003 "\tcurrent capacity is %llu sectors (%llu MB)\n"
1004 "\tnative capacity is %llu sectors (%llu MB)\n",
1006 capacity, sectors_to_MB(capacity),
1007 set_max, sectors_to_MB(set_max));
1013 set_max = idedisk_set_max_address_ext(drive, set_max);
1015 set_max = idedisk_set_max_address(drive, set_max);
1017 drive->capacity64 = set_max;
1018 printk(KERN_INFO "%s: Host Protected Area disabled.\n",
1024 * Compute drive->capacity, the full capacity of the drive
1025 * Called with drive->id != NULL.
1027 * To compute capacity, this uses either of
1029 * 1. CHS value set by user (whatever user sets will be trusted)
1030 * 2. LBA value from target drive (require new ATA feature)
1031 * 3. LBA value from system BIOS (new one is OK, old one may break)
1032 * 4. CHS value from system BIOS (traditional style)
1034 * in above order (i.e., if value of higher priority is available,
1035 * reset will be ignored).
1037 static void init_idedisk_capacity (ide_drive_t *drive)
1039 struct hd_driveid *id = drive->id;
1041 * If this drive supports the Host Protected Area feature set,
1042 * then we may need to change our opinion about the drive's capacity.
1044 int hpa = idedisk_supports_hpa(id);
1046 if (idedisk_supports_lba48(id)) {
1047 /* drive speaks 48-bit LBA */
1048 drive->select.b.lba = 1;
1049 drive->capacity64 = id->lba_capacity_2;
1051 idedisk_check_hpa(drive);
1052 } else if ((id->capability & 2) && lba_capacity_is_ok(id)) {
1053 /* drive speaks 28-bit LBA */
1054 drive->select.b.lba = 1;
1055 drive->capacity64 = id->lba_capacity;
1057 idedisk_check_hpa(drive);
1059 /* drive speaks boring old 28-bit CHS */
1060 drive->capacity64 = drive->cyl * drive->head * drive->sect;
1064 static sector_t idedisk_capacity (ide_drive_t *drive)
1066 return drive->capacity64 - drive->sect0;
1069 static ide_startstop_t idedisk_special (ide_drive_t *drive)
1071 special_t *s = &drive->special;
1073 if (s->b.set_geometry) {
1074 s->b.set_geometry = 0;
1075 if (!IS_PDC4030_DRIVE) {
1077 memset(&args, 0, sizeof(ide_task_t));
1078 args.tfRegister[IDE_NSECTOR_OFFSET] = drive->sect;
1079 args.tfRegister[IDE_SECTOR_OFFSET] = drive->sect;
1080 args.tfRegister[IDE_LCYL_OFFSET] = drive->cyl;
1081 args.tfRegister[IDE_HCYL_OFFSET] = drive->cyl>>8;
1082 args.tfRegister[IDE_SELECT_OFFSET] = ((drive->head-1)|drive->select.all)&0xBF;
1083 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SPECIFY;
1084 args.command_type = IDE_DRIVE_TASK_NO_DATA;
1085 args.handler = &set_geometry_intr;
1086 do_rw_taskfile(drive, &args);
1088 } else if (s->b.recalibrate) {
1089 s->b.recalibrate = 0;
1090 if (!IS_PDC4030_DRIVE) {
1092 memset(&args, 0, sizeof(ide_task_t));
1093 args.tfRegister[IDE_NSECTOR_OFFSET] = drive->sect;
1094 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_RESTORE;
1095 args.command_type = IDE_DRIVE_TASK_NO_DATA;
1096 args.handler = &recal_intr;
1097 do_rw_taskfile(drive, &args);
1099 } else if (s->b.set_multmode) {
1100 s->b.set_multmode = 0;
1101 if (drive->mult_req > drive->id->max_multsect)
1102 drive->mult_req = drive->id->max_multsect;
1103 if (!IS_PDC4030_DRIVE) {
1105 memset(&args, 0, sizeof(ide_task_t));
1106 args.tfRegister[IDE_NSECTOR_OFFSET] = drive->mult_req;
1107 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SETMULT;
1108 args.command_type = IDE_DRIVE_TASK_NO_DATA;
1109 args.handler = &set_multmode_intr;
1110 do_rw_taskfile(drive, &args);
1112 } else if (s->all) {
1113 int special = s->all;
1115 printk(KERN_ERR "%s: bad special flag: 0x%02x\n", drive->name, special);
1118 return IS_PDC4030_DRIVE ? ide_stopped : ide_started;
1121 static void idedisk_pre_reset (ide_drive_t *drive)
1123 int legacy = (drive->id->cfs_enable_2 & 0x0400) ? 0 : 1;
1125 drive->special.all = 0;
1126 drive->special.b.set_geometry = legacy;
1127 drive->special.b.recalibrate = legacy;
1128 if (OK_TO_RESET_CONTROLLER)
1129 drive->mult_count = 0;
1130 if (!drive->keep_settings && !drive->using_dma)
1131 drive->mult_req = 0;
1132 if (drive->mult_req != drive->mult_count)
1133 drive->special.b.set_multmode = 1;
1136 #ifdef CONFIG_PROC_FS
1138 static int smart_enable(ide_drive_t *drive)
1142 memset(&args, 0, sizeof(ide_task_t));
1143 args.tfRegister[IDE_FEATURE_OFFSET] = SMART_ENABLE;
1144 args.tfRegister[IDE_LCYL_OFFSET] = SMART_LCYL_PASS;
1145 args.tfRegister[IDE_HCYL_OFFSET] = SMART_HCYL_PASS;
1146 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SMART;
1147 args.command_type = IDE_DRIVE_TASK_NO_DATA;
1148 args.handler = &task_no_data_intr;
1149 return ide_raw_taskfile(drive, &args, NULL);
1152 static int get_smart_values(ide_drive_t *drive, u8 *buf)
1156 memset(&args, 0, sizeof(ide_task_t));
1157 args.tfRegister[IDE_FEATURE_OFFSET] = SMART_READ_VALUES;
1158 args.tfRegister[IDE_NSECTOR_OFFSET] = 0x01;
1159 args.tfRegister[IDE_LCYL_OFFSET] = SMART_LCYL_PASS;
1160 args.tfRegister[IDE_HCYL_OFFSET] = SMART_HCYL_PASS;
1161 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SMART;
1162 args.command_type = IDE_DRIVE_TASK_IN;
1163 args.data_phase = TASKFILE_IN;
1164 args.handler = &task_in_intr;
1165 (void) smart_enable(drive);
1166 return ide_raw_taskfile(drive, &args, buf);
1169 static int get_smart_thresholds(ide_drive_t *drive, u8 *buf)
1172 memset(&args, 0, sizeof(ide_task_t));
1173 args.tfRegister[IDE_FEATURE_OFFSET] = SMART_READ_THRESHOLDS;
1174 args.tfRegister[IDE_NSECTOR_OFFSET] = 0x01;
1175 args.tfRegister[IDE_LCYL_OFFSET] = SMART_LCYL_PASS;
1176 args.tfRegister[IDE_HCYL_OFFSET] = SMART_HCYL_PASS;
1177 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SMART;
1178 args.command_type = IDE_DRIVE_TASK_IN;
1179 args.handler = &task_in_intr;
1180 (void) smart_enable(drive);
1181 return ide_raw_taskfile(drive, &args, buf);
1184 static int proc_idedisk_read_cache
1185 (char *page, char **start, off_t off, int count, int *eof, void *data)
1187 ide_drive_t *drive = (ide_drive_t *) data;
1192 len = sprintf(out,"%i\n", drive->id->buf_size / 2);
1194 len = sprintf(out,"(none)\n");
1195 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
1198 static int proc_idedisk_read_smart_thresholds
1199 (char *page, char **start, off_t off, int count, int *eof, void *data)
1201 ide_drive_t *drive = (ide_drive_t *)data;
1204 if (!get_smart_thresholds(drive, page)) {
1205 unsigned short *val = (unsigned short *) page;
1206 char *out = ((char *)val) + (SECTOR_WORDS * 4);
1209 out += sprintf(out, "%04x%c", le16_to_cpu(*val), (++i & 7) ? ' ' : '\n');
1211 } while (i < (SECTOR_WORDS * 2));
1214 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
1217 static int proc_idedisk_read_smart_values
1218 (char *page, char **start, off_t off, int count, int *eof, void *data)
1220 ide_drive_t *drive = (ide_drive_t *)data;
1223 if (!get_smart_values(drive, page)) {
1224 unsigned short *val = (unsigned short *) page;
1225 char *out = ((char *)val) + (SECTOR_WORDS * 4);
1228 out += sprintf(out, "%04x%c", le16_to_cpu(*val), (++i & 7) ? ' ' : '\n');
1230 } while (i < (SECTOR_WORDS * 2));
1233 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
1236 static ide_proc_entry_t idedisk_proc[] = {
1237 { "cache", S_IFREG|S_IRUGO, proc_idedisk_read_cache, NULL },
1238 { "geometry", S_IFREG|S_IRUGO, proc_ide_read_geometry, NULL },
1239 { "smart_values", S_IFREG|S_IRUSR, proc_idedisk_read_smart_values, NULL },
1240 { "smart_thresholds", S_IFREG|S_IRUSR, proc_idedisk_read_smart_thresholds, NULL },
1241 { NULL, 0, NULL, NULL }
1246 #define idedisk_proc NULL
1248 #endif /* CONFIG_PROC_FS */
1250 static int idedisk_issue_flush(request_queue_t *q, struct gendisk *disk,
1251 sector_t *error_sector)
1253 ide_drive_t *drive = q->queuedata;
1260 rq = blk_get_request(q, WRITE, __GFP_WAIT);
1262 memset(rq->cmd, 0, sizeof(rq->cmd));
1264 if (ide_id_has_flush_cache_ext(drive->id) &&
1265 (drive->capacity64 >= (1UL << 28)))
1266 rq->cmd[0] = WIN_FLUSH_CACHE_EXT;
1268 rq->cmd[0] = WIN_FLUSH_CACHE;
1271 rq->flags |= REQ_DRIVE_TASK | REQ_SOFTBARRIER;
1272 rq->buffer = rq->cmd;
1274 ret = blk_execute_rq(q, disk, rq);
1277 * if we failed and caller wants error offset, get it
1279 if (ret && error_sector)
1280 *error_sector = ide_get_error_location(drive, rq->cmd);
1282 blk_put_request(rq);
1287 * This is tightly woven into the driver->do_special can not touch.
1288 * DON'T do it again until a total personality rewrite is committed.
1290 static int set_multcount(ide_drive_t *drive, int arg)
1294 if (drive->special.b.set_multmode)
1296 ide_init_drive_cmd (&rq);
1297 rq.flags = REQ_DRIVE_CMD;
1298 drive->mult_req = arg;
1299 drive->special.b.set_multmode = 1;
1300 (void) ide_do_drive_cmd (drive, &rq, ide_wait);
1301 return (drive->mult_count == arg) ? 0 : -EIO;
1304 static int set_nowerr(ide_drive_t *drive, int arg)
1306 if (ide_spin_wait_hwgroup(drive))
1308 drive->nowerr = arg;
1309 drive->bad_wstat = arg ? BAD_R_STAT : BAD_W_STAT;
1310 spin_unlock_irq(&ide_lock);
1314 static int write_cache(ide_drive_t *drive, int arg)
1319 if (!ide_id_has_flush_cache(drive->id))
1322 memset(&args, 0, sizeof(ide_task_t));
1323 args.tfRegister[IDE_FEATURE_OFFSET] = (arg) ?
1324 SETFEATURES_EN_WCACHE : SETFEATURES_DIS_WCACHE;
1325 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SETFEATURES;
1326 args.command_type = IDE_DRIVE_TASK_NO_DATA;
1327 args.handler = &task_no_data_intr;
1329 err = ide_raw_taskfile(drive, &args, NULL);
1333 drive->wcache = arg;
1337 static int do_idedisk_flushcache (ide_drive_t *drive)
1341 memset(&args, 0, sizeof(ide_task_t));
1342 if (ide_id_has_flush_cache_ext(drive->id))
1343 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE_EXT;
1345 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE;
1346 args.command_type = IDE_DRIVE_TASK_NO_DATA;
1347 args.handler = &task_no_data_intr;
1348 return ide_raw_taskfile(drive, &args, NULL);
1351 static int set_acoustic (ide_drive_t *drive, int arg)
1355 memset(&args, 0, sizeof(ide_task_t));
1356 args.tfRegister[IDE_FEATURE_OFFSET] = (arg) ? SETFEATURES_EN_AAM :
1357 SETFEATURES_DIS_AAM;
1358 args.tfRegister[IDE_NSECTOR_OFFSET] = arg;
1359 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SETFEATURES;
1360 args.command_type = IDE_DRIVE_TASK_NO_DATA;
1361 args.handler = &task_no_data_intr;
1362 ide_raw_taskfile(drive, &args, NULL);
1363 drive->acoustic = arg;
1368 * drive->addressing:
1371 * 2: 48-bit capable doing 28-bit
1373 static int set_lba_addressing(ide_drive_t *drive, int arg)
1375 drive->addressing = 0;
1377 if (HWIF(drive)->no_lba48)
1380 if (!idedisk_supports_lba48(drive->id))
1382 drive->addressing = arg;
1386 static void idedisk_add_settings(ide_drive_t *drive)
1388 struct hd_driveid *id = drive->id;
1390 ide_add_setting(drive, "bios_cyl", SETTING_RW, -1, -1, TYPE_INT, 0, 65535, 1, 1, &drive->bios_cyl, NULL);
1391 ide_add_setting(drive, "bios_head", SETTING_RW, -1, -1, TYPE_BYTE, 0, 255, 1, 1, &drive->bios_head, NULL);
1392 ide_add_setting(drive, "bios_sect", SETTING_RW, -1, -1, TYPE_BYTE, 0, 63, 1, 1, &drive->bios_sect, NULL);
1393 ide_add_setting(drive, "address", SETTING_RW, HDIO_GET_ADDRESS, HDIO_SET_ADDRESS, TYPE_INTA, 0, 2, 1, 1, &drive->addressing, set_lba_addressing);
1394 ide_add_setting(drive, "bswap", SETTING_READ, -1, -1, TYPE_BYTE, 0, 1, 1, 1, &drive->bswap, NULL);
1395 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);
1396 ide_add_setting(drive, "nowerr", SETTING_RW, HDIO_GET_NOWERR, HDIO_SET_NOWERR, TYPE_BYTE, 0, 1, 1, 1, &drive->nowerr, set_nowerr);
1397 ide_add_setting(drive, "lun", SETTING_RW, -1, -1, TYPE_INT, 0, 7, 1, 1, &drive->lun, NULL);
1398 ide_add_setting(drive, "wcache", SETTING_RW, HDIO_GET_WCACHE, HDIO_SET_WCACHE, TYPE_BYTE, 0, 1, 1, 1, &drive->wcache, write_cache);
1399 ide_add_setting(drive, "acoustic", SETTING_RW, HDIO_GET_ACOUSTIC, HDIO_SET_ACOUSTIC, TYPE_BYTE, 0, 254, 1, 1, &drive->acoustic, set_acoustic);
1400 ide_add_setting(drive, "failures", SETTING_RW, -1, -1, TYPE_INT, 0, 65535, 1, 1, &drive->failures, NULL);
1401 ide_add_setting(drive, "max_failures", SETTING_RW, -1, -1, TYPE_INT, 0, 65535, 1, 1, &drive->max_failures, NULL);
1405 * Power Management state machine. This one is rather trivial for now,
1406 * we should probably add more, like switching back to PIO on suspend
1407 * to help some BIOSes, re-do the door locking on resume, etc...
1411 idedisk_pm_flush_cache = ide_pm_state_start_suspend,
1414 idedisk_pm_idle = ide_pm_state_start_resume,
1415 idedisk_pm_restore_dma,
1418 static void idedisk_complete_power_step (ide_drive_t *drive, struct request *rq, u8 stat, u8 error)
1420 switch (rq->pm->pm_step) {
1421 case idedisk_pm_flush_cache: /* Suspend step 1 (flush cache) complete */
1422 if (rq->pm->pm_state == 4)
1423 rq->pm->pm_step = ide_pm_state_completed;
1425 rq->pm->pm_step = idedisk_pm_standby;
1427 case idedisk_pm_standby: /* Suspend step 2 (standby) complete */
1428 rq->pm->pm_step = ide_pm_state_completed;
1430 case idedisk_pm_idle: /* Resume step 1 (idle) complete */
1431 rq->pm->pm_step = idedisk_pm_restore_dma;
1436 static ide_startstop_t idedisk_start_power_step (ide_drive_t *drive, struct request *rq)
1438 ide_task_t *args = rq->special;
1440 memset(args, 0, sizeof(*args));
1442 switch (rq->pm->pm_step) {
1443 case idedisk_pm_flush_cache: /* Suspend step 1 (flush cache) */
1444 /* Not supported? Switch to next step now. */
1445 if (!drive->wcache || !ide_id_has_flush_cache(drive->id)) {
1446 idedisk_complete_power_step(drive, rq, 0, 0);
1449 if (ide_id_has_flush_cache_ext(drive->id))
1450 args->tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE_EXT;
1452 args->tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE;
1453 args->command_type = IDE_DRIVE_TASK_NO_DATA;
1454 args->handler = &task_no_data_intr;
1455 return do_rw_taskfile(drive, args);
1457 case idedisk_pm_standby: /* Suspend step 2 (standby) */
1458 args->tfRegister[IDE_COMMAND_OFFSET] = WIN_STANDBYNOW1;
1459 args->command_type = IDE_DRIVE_TASK_NO_DATA;
1460 args->handler = &task_no_data_intr;
1461 return do_rw_taskfile(drive, args);
1463 case idedisk_pm_idle: /* Resume step 1 (idle) */
1464 args->tfRegister[IDE_COMMAND_OFFSET] = WIN_IDLEIMMEDIATE;
1465 args->command_type = IDE_DRIVE_TASK_NO_DATA;
1466 args->handler = task_no_data_intr;
1467 return do_rw_taskfile(drive, args);
1469 case idedisk_pm_restore_dma: /* Resume step 2 (restore DMA) */
1471 * Right now, all we do is call hwif->ide_dma_check(drive),
1472 * we could be smarter and check for current xfer_speed
1473 * in struct drive etc...
1474 * Also, this step could be implemented as a generic helper
1475 * as most subdrivers will use it
1477 if ((drive->id->capability & 1) == 0)
1479 if (HWIF(drive)->ide_dma_check == NULL)
1481 HWIF(drive)->ide_dma_check(drive);
1484 rq->pm->pm_step = ide_pm_state_completed;
1488 static void idedisk_setup (ide_drive_t *drive)
1490 struct hd_driveid *id = drive->id;
1491 unsigned long long capacity;
1494 idedisk_add_settings(drive);
1496 if (drive->id_read == 0)
1500 * CompactFlash cards and their brethern look just like hard drives
1501 * to us, but they are removable and don't have a doorlock mechanism.
1503 if (drive->removable && !(drive->is_flash)) {
1505 * Removable disks (eg. SYQUEST); ignore 'WD' drives
1507 if (id->model[0] != 'W' || id->model[1] != 'D') {
1508 drive->doorlocking = 1;
1512 (void)set_lba_addressing(drive, 1);
1514 if (drive->addressing == 1) {
1515 ide_hwif_t *hwif = HWIF(drive);
1518 if (max_s > hwif->rqsize)
1519 max_s = hwif->rqsize;
1521 blk_queue_max_sectors(drive->queue, max_s);
1524 printk(KERN_INFO "%s: max request size: %dKiB\n", drive->name, drive->queue->max_sectors / 2);
1526 /* Extract geometry if we did not already have one for the drive */
1527 if (!drive->cyl || !drive->head || !drive->sect) {
1528 drive->cyl = drive->bios_cyl = id->cyls;
1529 drive->head = drive->bios_head = id->heads;
1530 drive->sect = drive->bios_sect = id->sectors;
1533 /* Handle logical geometry translation by the drive */
1534 if ((id->field_valid & 1) && id->cur_cyls &&
1535 id->cur_heads && (id->cur_heads <= 16) && id->cur_sectors) {
1536 drive->cyl = id->cur_cyls;
1537 drive->head = id->cur_heads;
1538 drive->sect = id->cur_sectors;
1541 /* Use physical geometry if what we have still makes no sense */
1542 if (drive->head > 16 && id->heads && id->heads <= 16) {
1543 drive->cyl = id->cyls;
1544 drive->head = id->heads;
1545 drive->sect = id->sectors;
1548 /* calculate drive capacity, and select LBA if possible */
1549 init_idedisk_capacity (drive);
1551 /* limit drive capacity to 137GB if LBA48 cannot be used */
1552 if (drive->addressing == 0 && drive->capacity64 > 1ULL << 28) {
1553 printk(KERN_WARNING "%s: cannot use LBA48 - full capacity "
1554 "%llu sectors (%llu MB)\n",
1555 drive->name, (unsigned long long)drive->capacity64,
1556 sectors_to_MB(drive->capacity64));
1557 drive->capacity64 = 1ULL << 28;
1560 if (drive->hwif->no_lba48_dma && drive->addressing) {
1561 if (drive->capacity64 > 1ULL << 28) {
1562 printk(KERN_INFO "%s: cannot use LBA48 DMA - PIO mode will"
1563 " be used for accessing sectors > %u\n",
1564 drive->name, 1 << 28);
1566 drive->addressing = 0;
1570 * if possible, give fdisk access to more of the drive,
1571 * by correcting bios_cyls:
1573 capacity = idedisk_capacity (drive);
1574 if (!drive->forced_geom) {
1576 if (idedisk_supports_lba48(drive->id)) {
1578 drive->bios_sect = 63;
1579 drive->bios_head = 255;
1582 if (drive->bios_sect && drive->bios_head) {
1583 unsigned int cap0 = capacity; /* truncate to 32 bits */
1584 unsigned int cylsz, cyl;
1586 if (cap0 != capacity)
1587 drive->bios_cyl = 65535;
1589 cylsz = drive->bios_sect * drive->bios_head;
1593 if (cyl > drive->bios_cyl)
1594 drive->bios_cyl = cyl;
1598 printk(KERN_INFO "%s: %llu sectors (%llu MB)",
1599 drive->name, capacity, sectors_to_MB(capacity));
1601 /* Only print cache size when it was specified */
1603 printk (" w/%dKiB Cache", id->buf_size/2);
1605 printk(", CHS=%d/%d/%d",
1606 drive->bios_cyl, drive->bios_head, drive->bios_sect);
1607 if (drive->using_dma)
1608 (void) HWIF(drive)->ide_dma_verbose(drive);
1611 drive->mult_count = 0;
1612 if (id->max_multsect) {
1613 #ifdef CONFIG_IDEDISK_MULTI_MODE
1614 id->multsect = ((id->max_multsect/2) > 1) ? id->max_multsect : 0;
1615 id->multsect_valid = id->multsect ? 1 : 0;
1616 drive->mult_req = id->multsect_valid ? id->max_multsect : INITIAL_MULT_COUNT;
1617 drive->special.b.set_multmode = drive->mult_req ? 1 : 0;
1618 #else /* original, pre IDE-NFG, per request of AC */
1619 drive->mult_req = INITIAL_MULT_COUNT;
1620 if (drive->mult_req > id->max_multsect)
1621 drive->mult_req = id->max_multsect;
1622 if (drive->mult_req || ((id->multsect_valid & 1) && id->multsect))
1623 drive->special.b.set_multmode = 1;
1624 #endif /* CONFIG_IDEDISK_MULTI_MODE */
1626 drive->no_io_32bit = id->dword_io ? 1 : 0;
1628 /* write cache enabled? */
1629 if ((id->csfo & 1) || (id->cfs_enable_1 & (1 << 5)))
1632 write_cache(drive, 1);
1635 * We must avoid issuing commands a drive does not understand
1636 * or we may crash it. We check flush cache is supported. We also
1637 * check we have the LBA48 flush cache if the drive capacity is
1638 * too large. By this time we have trimmed the drive capacity if
1639 * LBA48 is not available so we don't need to recheck that.
1642 if (ide_id_has_flush_cache(id))
1644 if (drive->addressing == 1) {
1645 /* Can't issue the correct flush ? */
1646 if (capacity > (1ULL << 28) && !ide_id_has_flush_cache_ext(id))
1650 printk(KERN_DEBUG "%s: cache flushes %ssupported\n",
1651 drive->name, barrier ? "" : "not ");
1653 blk_queue_ordered(drive->queue, 1);
1654 blk_queue_issue_flush_fn(drive->queue, idedisk_issue_flush);
1658 static void ide_cacheflush_p(ide_drive_t *drive)
1660 if (!drive->wcache || !ide_id_has_flush_cache(drive->id))
1663 if (do_idedisk_flushcache(drive))
1664 printk(KERN_INFO "%s: wcache flush failed!\n", drive->name);
1667 static int idedisk_cleanup (ide_drive_t *drive)
1669 struct gendisk *g = drive->disk;
1670 ide_cacheflush_p(drive);
1671 if (ide_unregister_subdriver(drive))
1674 drive->devfs_name[0] = '\0';
1679 static int idedisk_attach(ide_drive_t *drive);
1681 static void ide_device_shutdown(struct device *dev)
1683 ide_drive_t *drive = container_of(dev, ide_drive_t, gendev);
1686 /* On Alpha, halt(8) doesn't actually turn the machine off,
1687 it puts you into the sort of firmware monitor. Typically,
1688 it's used to boot another kernel image, so it's not much
1689 different from reboot(8). Therefore, we don't need to
1690 spin down the disk in this case, especially since Alpha
1691 firmware doesn't handle disks in standby mode properly.
1692 On the other hand, it's reasonably safe to turn the power
1693 off when the shutdown process reaches the firmware prompt,
1694 as the firmware initialization takes rather long time -
1695 at least 10 seconds, which should be sufficient for
1696 the disk to expire its write cache. */
1697 if (system_state != SYSTEM_POWER_OFF) {
1699 if (system_state == SYSTEM_RESTART) {
1701 ide_cacheflush_p(drive);
1705 printk("Shutdown: %s\n", drive->name);
1706 dev->bus->suspend(dev, PM_SUSPEND_STANDBY);
1710 * IDE subdriver functions, registered with ide.c
1712 static ide_driver_t idedisk_driver = {
1713 .owner = THIS_MODULE,
1715 .shutdown = ide_device_shutdown,
1718 .version = IDEDISK_VERSION,
1721 .supports_dsc_overlap = 0,
1722 .cleanup = idedisk_cleanup,
1723 .do_request = ide_do_rw_disk,
1724 .sense = idedisk_dump_status,
1725 .error = idedisk_error,
1726 .abort = idedisk_abort,
1727 .pre_reset = idedisk_pre_reset,
1728 .capacity = idedisk_capacity,
1729 .special = idedisk_special,
1730 .proc = idedisk_proc,
1731 .attach = idedisk_attach,
1732 .drives = LIST_HEAD_INIT(idedisk_driver.drives),
1733 .start_power_step = idedisk_start_power_step,
1734 .complete_power_step = idedisk_complete_power_step,
1737 static int idedisk_open(struct inode *inode, struct file *filp)
1739 ide_drive_t *drive = inode->i_bdev->bd_disk->private_data;
1741 if (drive->removable && drive->usage == 1) {
1743 memset(&args, 0, sizeof(ide_task_t));
1744 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_DOORLOCK;
1745 args.command_type = IDE_DRIVE_TASK_NO_DATA;
1746 args.handler = &task_no_data_intr;
1747 check_disk_change(inode->i_bdev);
1749 * Ignore the return code from door_lock,
1750 * since the open() has already succeeded,
1751 * and the door_lock is irrelevant at this point.
1753 if (drive->doorlocking && ide_raw_taskfile(drive, &args, NULL))
1754 drive->doorlocking = 0;
1759 static int idedisk_release(struct inode *inode, struct file *filp)
1761 ide_drive_t *drive = inode->i_bdev->bd_disk->private_data;
1762 if (drive->usage == 1)
1763 ide_cacheflush_p(drive);
1764 if (drive->removable && drive->usage == 1) {
1766 memset(&args, 0, sizeof(ide_task_t));
1767 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_DOORUNLOCK;
1768 args.command_type = IDE_DRIVE_TASK_NO_DATA;
1769 args.handler = &task_no_data_intr;
1770 if (drive->doorlocking && ide_raw_taskfile(drive, &args, NULL))
1771 drive->doorlocking = 0;
1777 static int idedisk_ioctl(struct inode *inode, struct file *file,
1778 unsigned int cmd, unsigned long arg)
1780 struct block_device *bdev = inode->i_bdev;
1781 return generic_ide_ioctl(file, bdev, cmd, arg);
1784 static int idedisk_media_changed(struct gendisk *disk)
1786 ide_drive_t *drive = disk->private_data;
1788 /* do not scan partitions twice if this is a removable device */
1789 if (drive->attach) {
1793 /* if removable, always assume it was changed */
1794 return drive->removable;
1797 static int idedisk_revalidate_disk(struct gendisk *disk)
1799 ide_drive_t *drive = disk->private_data;
1800 set_capacity(disk, current_capacity(drive));
1804 static struct block_device_operations idedisk_ops = {
1805 .owner = THIS_MODULE,
1806 .open = idedisk_open,
1807 .release = idedisk_release,
1808 .ioctl = idedisk_ioctl,
1809 .media_changed = idedisk_media_changed,
1810 .revalidate_disk= idedisk_revalidate_disk
1813 MODULE_DESCRIPTION("ATA DISK Driver");
1815 static int idedisk_attach(ide_drive_t *drive)
1817 struct gendisk *g = drive->disk;
1819 /* strstr("foo", "") is non-NULL */
1820 if (!strstr("ide-disk", drive->driver_req))
1822 if (!drive->present)
1824 if (drive->media != ide_disk)
1827 if (ide_register_subdriver(drive, &idedisk_driver)) {
1828 printk (KERN_ERR "ide-disk: %s: Failed to register the driver with ide.c\n", drive->name);
1831 DRIVER(drive)->busy++;
1832 idedisk_setup(drive);
1833 if ((!drive->head || drive->head > 16) && !drive->select.b.lba) {
1834 printk(KERN_ERR "%s: INVALID GEOMETRY: %d PHYSICAL HEADS?\n",
1835 drive->name, drive->head);
1839 DRIVER(drive)->busy--;
1840 g->minors = 1 << PARTN_BITS;
1841 strcpy(g->devfs_name, drive->devfs_name);
1842 g->driverfs_dev = &drive->gendev;
1843 g->flags = drive->removable ? GENHD_FL_REMOVABLE : 0;
1844 set_capacity(g, current_capacity(drive));
1845 g->fops = &idedisk_ops;
1852 static void __exit idedisk_exit (void)
1854 ide_unregister_driver(&idedisk_driver);
1857 static int idedisk_init (void)
1859 return ide_register_driver(&idedisk_driver);
1862 module_init(idedisk_init);
1863 module_exit(idedisk_exit);
1864 MODULE_LICENSE("GPL");