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;
91 /* No non-LBA info .. so valid! */
96 * The ATA spec tells large drives to return
97 * C/H/S = 16383/16/63 independent of their size.
98 * Some drives can be jumpered to use 15 heads instead of 16.
99 * Some drives can be jumpered to use 4092 cyls instead of 16383.
101 if ((id->cyls == 16383
102 || (id->cyls == 4092 && id->cur_cyls == 16383)) &&
104 (id->heads == 15 || id->heads == 16) &&
105 (id->lba_capacity >= 16383*63*id->heads))
108 lba_sects = id->lba_capacity;
109 chs_sects = id->cyls * id->heads * id->sectors;
111 /* perform a rough sanity check on lba_sects: within 10% is OK */
112 if ((lba_sects - chs_sects) < chs_sects/10)
115 /* some drives have the word order reversed */
116 head = ((lba_sects >> 16) & 0xffff);
117 tail = (lba_sects & 0xffff);
118 lba_sects = (head | (tail << 16));
119 if ((lba_sects - chs_sects) < chs_sects/10) {
120 id->lba_capacity = lba_sects;
121 return 1; /* lba_capacity is (now) good */
124 return 0; /* lba_capacity value may be bad */
127 #ifndef CONFIG_IDE_TASKFILE_IO
130 * read_intr() is the handler for disk read/multread interrupts
132 static ide_startstop_t read_intr (ide_drive_t *drive)
134 ide_hwif_t *hwif = HWIF(drive);
135 u32 i = 0, nsect = 0, msect = drive->mult_count;
141 /* new way for dealing with premature shared PCI interrupts */
142 if (!OK_STAT(stat=hwif->INB(IDE_STATUS_REG),DATA_READY,BAD_R_STAT)) {
143 if (stat & (ERR_STAT|DRQ_STAT)) {
144 return DRIVER(drive)->error(drive, "read_intr", stat);
146 /* no data yet, so wait for another interrupt */
147 ide_set_handler(drive, &read_intr, WAIT_CMD, NULL);
152 rq = HWGROUP(drive)->rq;
154 if ((nsect = rq->current_nr_sectors) > msect)
159 to = ide_map_buffer(rq, &flags);
160 taskfile_input_data(drive, to, nsect * SECTOR_WORDS);
162 printk("%s: read: sectors(%ld-%ld), buffer=0x%08lx, remaining=%ld\n",
163 drive->name, rq->sector, rq->sector+nsect-1,
164 (unsigned long) rq->buffer+(nsect<<9), rq->nr_sectors-nsect);
166 ide_unmap_buffer(rq, to, &flags);
169 i = (rq->nr_sectors -= nsect);
170 if (((long)(rq->current_nr_sectors -= nsect)) <= 0)
171 ide_end_request(drive, 1, rq->hard_cur_sectors);
173 * Another BH Page walker and DATA INTEGRITY Questioned on ERROR.
174 * If passed back up on multimode read, BAD DATA could be ACKED
175 * to FILE SYSTEMS above ...
180 ide_set_handler(drive, &read_intr, WAIT_CMD, NULL);
187 * write_intr() is the handler for disk write interrupts
189 static ide_startstop_t write_intr (ide_drive_t *drive)
191 ide_hwgroup_t *hwgroup = HWGROUP(drive);
192 ide_hwif_t *hwif = HWIF(drive);
193 struct request *rq = hwgroup->rq;
197 if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG),
198 DRIVE_READY, drive->bad_wstat)) {
199 printk("%s: write_intr error1: nr_sectors=%ld, stat=0x%02x\n",
200 drive->name, rq->nr_sectors, stat);
203 printk("%s: write: sector %ld, buffer=0x%08lx, remaining=%ld\n",
204 drive->name, rq->sector, (unsigned long) rq->buffer,
207 if ((rq->nr_sectors == 1) ^ ((stat & DRQ_STAT) != 0)) {
210 i = --rq->nr_sectors;
211 --rq->current_nr_sectors;
212 if (((long)rq->current_nr_sectors) <= 0)
213 ide_end_request(drive, 1, rq->hard_cur_sectors);
216 char *to = ide_map_buffer(rq, &flags);
217 taskfile_output_data(drive, to, SECTOR_WORDS);
218 ide_unmap_buffer(rq, to, &flags);
219 ide_set_handler(drive, &write_intr, WAIT_CMD, NULL);
224 /* the original code did this here (?) */
227 return DRIVER(drive)->error(drive, "write_intr", stat);
231 * ide_multwrite() transfers a block of up to mcount sectors of data
232 * to a drive as part of a disk multiple-sector write operation.
234 * Note that we may be called from two contexts - __ide_do_rw_disk() context
235 * and IRQ context. The IRQ can happen any time after we've output the
236 * full "mcount" number of sectors, so we must make sure we update the
237 * state _before_ we output the final part of the data!
239 * The update and return to BH is a BLOCK Layer Fakey to get more data
240 * to satisfy the hardware atomic segment. If the hardware atomic segment
241 * is shorter or smaller than the BH segment then we should be OKAY.
242 * This is only valid if we can rewind the rq->current_nr_sectors counter.
244 static void ide_multwrite(ide_drive_t *drive, unsigned int mcount)
246 ide_hwgroup_t *hwgroup = HWGROUP(drive);
247 struct request *rq = &hwgroup->wrq;
251 int nsect = rq->current_nr_sectors;
257 buffer = ide_map_buffer(rq, &flags);
260 rq->nr_sectors -= nsect;
261 rq->current_nr_sectors -= nsect;
263 /* Do we move to the next bh after this? */
264 if (!rq->current_nr_sectors) {
265 struct bio *bio = rq->bio;
268 * only move to next bio, when we have processed
269 * all bvecs in this one.
271 if (++bio->bi_idx >= bio->bi_vcnt) {
272 bio->bi_idx = bio->bi_vcnt - rq->nr_cbio_segments;
276 /* end early early we ran out of requests */
281 rq->nr_cbio_segments = bio_segments(bio);
282 rq->current_nr_sectors = bio_cur_sectors(bio);
283 rq->hard_cur_sectors = rq->current_nr_sectors;
288 * Ok, we're all setup for the interrupt
289 * re-entering us on the last transfer.
291 taskfile_output_data(drive, buffer, nsect<<7);
292 ide_unmap_buffer(rq, buffer, &flags);
297 * multwrite_intr() is the handler for disk multwrite interrupts
299 static ide_startstop_t multwrite_intr (ide_drive_t *drive)
301 ide_hwgroup_t *hwgroup = HWGROUP(drive);
302 ide_hwif_t *hwif = HWIF(drive);
303 struct request *rq = &hwgroup->wrq;
304 struct bio *bio = rq->bio;
307 stat = hwif->INB(IDE_STATUS_REG);
308 if (OK_STAT(stat, DRIVE_READY, drive->bad_wstat)) {
309 if (stat & DRQ_STAT) {
311 * The drive wants data. Remember rq is the copy
314 if (rq->nr_sectors) {
315 ide_multwrite(drive, drive->mult_count);
316 ide_set_handler(drive, &multwrite_intr, WAIT_CMD, NULL);
321 * If the copy has all the blocks completed then
322 * we can end the original request.
324 if (!rq->nr_sectors) { /* all done? */
325 bio->bi_idx = bio->bi_vcnt - rq->nr_cbio_segments;
327 ide_end_request(drive, 1, rq->nr_sectors);
331 bio->bi_idx = bio->bi_vcnt - rq->nr_cbio_segments;
332 /* the original code did this here (?) */
335 bio->bi_idx = bio->bi_vcnt - rq->nr_cbio_segments;
336 return DRIVER(drive)->error(drive, "multwrite_intr", stat);
340 * __ide_do_rw_disk() issues READ and WRITE commands to a disk,
341 * using LBA if supported, or CHS otherwise, to address sectors.
342 * It also takes care of issuing special DRIVE_CMDs.
344 ide_startstop_t __ide_do_rw_disk (ide_drive_t *drive, struct request *rq, sector_t block)
346 ide_hwif_t *hwif = HWIF(drive);
347 unsigned int dma = drive->using_dma;
348 u8 lba48 = (drive->addressing == 1) ? 1 : 0;
349 task_ioreg_t command = WIN_NOP;
350 ata_nsector_t nsectors;
352 nsectors.all = (u16) rq->nr_sectors;
354 if (hwif->no_lba48_dma && lba48 && dma) {
355 if (rq->sector + rq->nr_sectors > 1ULL << 28)
360 hwif->OUTB(drive->ctl, IDE_CONTROL_REG);
362 if (drive->select.b.lba) {
363 if (drive->addressing == 1) {
364 task_ioreg_t tasklets[10];
366 pr_debug("%s: LBA=0x%012llx\n", drive->name, block);
370 tasklets[2] = nsectors.b.low;
371 tasklets[3] = nsectors.b.high;
372 tasklets[4] = (task_ioreg_t) block;
373 tasklets[5] = (task_ioreg_t) (block>>8);
374 tasklets[6] = (task_ioreg_t) (block>>16);
375 tasklets[7] = (task_ioreg_t) (block>>24);
376 if (sizeof(block) == 4) {
377 tasklets[8] = (task_ioreg_t) 0;
378 tasklets[9] = (task_ioreg_t) 0;
380 tasklets[8] = (task_ioreg_t)((u64)block >> 32);
381 tasklets[9] = (task_ioreg_t)((u64)block >> 40);
384 printk("%s: 0x%02x%02x 0x%02x%02x%02x%02x%02x%02x\n",
385 drive->name, tasklets[3], tasklets[2],
386 tasklets[9], tasklets[8], tasklets[7],
387 tasklets[6], tasklets[5], tasklets[4]);
389 hwif->OUTB(tasklets[1], IDE_FEATURE_REG);
390 hwif->OUTB(tasklets[3], IDE_NSECTOR_REG);
391 hwif->OUTB(tasklets[7], IDE_SECTOR_REG);
392 hwif->OUTB(tasklets[8], IDE_LCYL_REG);
393 hwif->OUTB(tasklets[9], IDE_HCYL_REG);
395 hwif->OUTB(tasklets[0], IDE_FEATURE_REG);
396 hwif->OUTB(tasklets[2], IDE_NSECTOR_REG);
397 hwif->OUTB(tasklets[4], IDE_SECTOR_REG);
398 hwif->OUTB(tasklets[5], IDE_LCYL_REG);
399 hwif->OUTB(tasklets[6], IDE_HCYL_REG);
400 hwif->OUTB(0x00|drive->select.all,IDE_SELECT_REG);
402 hwif->OUTB(0x00, IDE_FEATURE_REG);
403 hwif->OUTB(nsectors.b.low, IDE_NSECTOR_REG);
404 hwif->OUTB(block, IDE_SECTOR_REG);
405 hwif->OUTB(block>>=8, IDE_LCYL_REG);
406 hwif->OUTB(block>>=8, IDE_HCYL_REG);
407 hwif->OUTB(((block>>8)&0x0f)|drive->select.all,IDE_SELECT_REG);
410 unsigned int sect,head,cyl,track;
411 track = (int)block / drive->sect;
412 sect = (int)block % drive->sect + 1;
413 hwif->OUTB(sect, IDE_SECTOR_REG);
414 head = track % drive->head;
415 cyl = track / drive->head;
417 pr_debug("%s: CHS=%u/%u/%u\n", drive->name, cyl, head, sect);
419 hwif->OUTB(0x00, IDE_FEATURE_REG);
420 hwif->OUTB(nsectors.b.low, IDE_NSECTOR_REG);
421 hwif->OUTB(cyl, IDE_LCYL_REG);
422 hwif->OUTB(cyl>>8, IDE_HCYL_REG);
423 hwif->OUTB(head|drive->select.all,IDE_SELECT_REG);
426 if (rq_data_dir(rq) == READ) {
427 if (dma && !hwif->ide_dma_read(drive))
430 command = ((drive->mult_count) ?
431 ((lba48) ? WIN_MULTREAD_EXT : WIN_MULTREAD) :
432 ((lba48) ? WIN_READ_EXT : WIN_READ));
433 ide_execute_command(drive, command, &read_intr, WAIT_CMD, NULL);
436 ide_startstop_t startstop;
438 if (dma && !hwif->ide_dma_write(drive))
441 command = ((drive->mult_count) ?
442 ((lba48) ? WIN_MULTWRITE_EXT : WIN_MULTWRITE) :
443 ((lba48) ? WIN_WRITE_EXT : WIN_WRITE));
444 hwif->OUTB(command, IDE_COMMAND_REG);
446 if (ide_wait_stat(&startstop, drive, DATA_READY,
447 drive->bad_wstat, WAIT_DRQ)) {
448 printk(KERN_ERR "%s: no DRQ after issuing %s\n",
450 drive->mult_count ? "MULTWRITE" : "WRITE");
455 if (drive->mult_count) {
456 ide_hwgroup_t *hwgroup = HWGROUP(drive);
458 hwgroup->wrq = *rq; /* scratchpad */
459 ide_set_handler(drive, &multwrite_intr, WAIT_CMD, NULL);
460 ide_multwrite(drive, drive->mult_count);
463 char *to = ide_map_buffer(rq, &flags);
464 ide_set_handler(drive, &write_intr, WAIT_CMD, NULL);
465 taskfile_output_data(drive, to, SECTOR_WORDS);
466 ide_unmap_buffer(rq, to, &flags);
471 EXPORT_SYMBOL_GPL(__ide_do_rw_disk);
473 #else /* CONFIG_IDE_TASKFILE_IO */
475 static ide_startstop_t chs_rw_disk(ide_drive_t *, struct request *, unsigned long);
476 static ide_startstop_t lba_28_rw_disk(ide_drive_t *, struct request *, unsigned long);
477 static ide_startstop_t lba_48_rw_disk(ide_drive_t *, struct request *, unsigned long long);
480 * __ide_do_rw_disk() issues READ and WRITE commands to a disk,
481 * using LBA if supported, or CHS otherwise, to address sectors.
482 * It also takes care of issuing special DRIVE_CMDs.
484 ide_startstop_t __ide_do_rw_disk (ide_drive_t *drive, struct request *rq, sector_t block)
487 * 268435455 == 137439 MB or 28bit limit
489 * need to add split taskfile operations based on 28bit threshold.
491 if (drive->addressing == 1) /* 48-bit LBA */
492 return lba_48_rw_disk(drive, rq, (unsigned long long) block);
493 if (drive->select.b.lba) /* 28-bit LBA */
494 return lba_28_rw_disk(drive, rq, (unsigned long) block);
496 /* 28-bit CHS : DIE DIE DIE piece of legacy crap!!! */
497 return chs_rw_disk(drive, rq, (unsigned long) block);
499 EXPORT_SYMBOL_GPL(__ide_do_rw_disk);
501 static u8 get_command(ide_drive_t *drive, struct request *rq, ide_task_t *task)
503 unsigned int lba48 = (drive->addressing == 1) ? 1 : 0;
504 unsigned int dma = drive->using_dma;
506 if (drive->hwif->no_lba48_dma && lba48 && dma) {
507 if (rq->sector + rq->nr_sectors > 1ULL << 28)
511 if (rq_data_dir(rq) == READ) {
512 task->command_type = IDE_DRIVE_TASK_IN;
514 return lba48 ? WIN_READDMA_EXT : WIN_READDMA;
515 task->handler = &task_in_intr;
516 if (drive->mult_count) {
517 task->data_phase = TASKFILE_MULTI_IN;
518 return lba48 ? WIN_MULTREAD_EXT : WIN_MULTREAD;
520 task->data_phase = TASKFILE_IN;
521 return lba48 ? WIN_READ_EXT : WIN_READ;
523 task->command_type = IDE_DRIVE_TASK_RAW_WRITE;
525 return lba48 ? WIN_WRITEDMA_EXT : WIN_WRITEDMA;
526 task->prehandler = &pre_task_out_intr;
527 task->handler = &task_out_intr;
528 if (drive->mult_count) {
529 task->data_phase = TASKFILE_MULTI_OUT;
530 return lba48 ? WIN_MULTWRITE_EXT : WIN_MULTWRITE;
532 task->data_phase = TASKFILE_OUT;
533 return lba48 ? WIN_WRITE_EXT : WIN_WRITE;
537 static ide_startstop_t chs_rw_disk (ide_drive_t *drive, struct request *rq, unsigned long block)
541 ata_nsector_t nsectors;
542 unsigned int track = (block / drive->sect);
543 unsigned int sect = (block % drive->sect) + 1;
544 unsigned int head = (track % drive->head);
545 unsigned int cyl = (track / drive->head);
547 nsectors.all = (u16) rq->nr_sectors;
549 pr_debug("%s: CHS=%u/%u/%u\n", drive->name, cyl, head, sect);
551 memset(&args, 0, sizeof(ide_task_t));
553 sectors = (rq->nr_sectors == 256) ? 0x00 : rq->nr_sectors;
555 args.tfRegister[IDE_NSECTOR_OFFSET] = sectors;
556 args.tfRegister[IDE_SECTOR_OFFSET] = sect;
557 args.tfRegister[IDE_LCYL_OFFSET] = cyl;
558 args.tfRegister[IDE_HCYL_OFFSET] = (cyl>>8);
559 args.tfRegister[IDE_SELECT_OFFSET] = head;
560 args.tfRegister[IDE_SELECT_OFFSET] |= drive->select.all;
561 args.tfRegister[IDE_COMMAND_OFFSET] = get_command(drive, rq, &args);
562 args.rq = (struct request *) rq;
563 rq->special = (ide_task_t *)&args;
564 drive->hwif->data_phase = args.data_phase;
565 return do_rw_taskfile(drive, &args);
568 static ide_startstop_t lba_28_rw_disk (ide_drive_t *drive, struct request *rq, unsigned long block)
572 ata_nsector_t nsectors;
574 nsectors.all = (u16) rq->nr_sectors;
576 memset(&args, 0, sizeof(ide_task_t));
578 sectors = (rq->nr_sectors == 256) ? 0x00 : rq->nr_sectors;
580 args.tfRegister[IDE_NSECTOR_OFFSET] = sectors;
581 args.tfRegister[IDE_SECTOR_OFFSET] = block;
582 args.tfRegister[IDE_LCYL_OFFSET] = (block>>=8);
583 args.tfRegister[IDE_HCYL_OFFSET] = (block>>=8);
584 args.tfRegister[IDE_SELECT_OFFSET] = ((block>>8)&0x0f);
585 args.tfRegister[IDE_SELECT_OFFSET] |= drive->select.all;
586 args.tfRegister[IDE_COMMAND_OFFSET] = get_command(drive, rq, &args);
587 args.rq = (struct request *) rq;
588 rq->special = (ide_task_t *)&args;
589 drive->hwif->data_phase = args.data_phase;
590 return do_rw_taskfile(drive, &args);
594 * 268435455 == 137439 MB or 28bit limit
595 * 320173056 == 163929 MB or 48bit addressing
596 * 1073741822 == 549756 MB or 48bit addressing fake drive
599 static ide_startstop_t lba_48_rw_disk (ide_drive_t *drive, struct request *rq, unsigned long long block)
603 ata_nsector_t nsectors;
605 nsectors.all = (u16) rq->nr_sectors;
607 memset(&args, 0, sizeof(ide_task_t));
609 sectors = (rq->nr_sectors == 65536) ? 0 : rq->nr_sectors;
611 args.tfRegister[IDE_NSECTOR_OFFSET] = sectors;
612 args.hobRegister[IDE_NSECTOR_OFFSET] = sectors >> 8;
613 args.tfRegister[IDE_SECTOR_OFFSET] = block; /* low lba */
614 args.tfRegister[IDE_LCYL_OFFSET] = (block>>=8); /* mid lba */
615 args.tfRegister[IDE_HCYL_OFFSET] = (block>>=8); /* hi lba */
616 args.tfRegister[IDE_SELECT_OFFSET] = drive->select.all;
617 args.tfRegister[IDE_COMMAND_OFFSET] = get_command(drive, rq, &args);
618 args.hobRegister[IDE_SECTOR_OFFSET] = (block>>=8); /* low lba */
619 args.hobRegister[IDE_LCYL_OFFSET] = (block>>=8); /* mid lba */
620 args.hobRegister[IDE_HCYL_OFFSET] = (block>>=8); /* hi lba */
621 args.hobRegister[IDE_SELECT_OFFSET] = drive->select.all;
622 args.hobRegister[IDE_CONTROL_OFFSET_HOB]= (drive->ctl|0x80);
623 args.rq = (struct request *) rq;
624 rq->special = (ide_task_t *)&args;
625 drive->hwif->data_phase = args.data_phase;
626 return do_rw_taskfile(drive, &args);
629 #endif /* CONFIG_IDE_TASKFILE_IO */
631 static ide_startstop_t ide_do_rw_disk (ide_drive_t *drive, struct request *rq, sector_t block)
633 ide_hwif_t *hwif = HWIF(drive);
635 BUG_ON(drive->blocked);
637 if (!blk_fs_request(rq)) {
638 blk_dump_rq_flags(rq, "ide_do_rw_disk - bad command");
639 ide_end_request(drive, 0, 0);
643 pr_debug("%s: %sing: block=%llu, sectors=%lu, buffer=0x%08lx\n",
644 drive->name, rq_data_dir(rq) == READ ? "read" : "writ",
645 block, rq->nr_sectors, (unsigned long)rq->buffer);
648 return hwif->rw_disk(drive, rq, block);
650 return __ide_do_rw_disk(drive, rq, block);
653 static u8 idedisk_dump_status (ide_drive_t *drive, const char *msg, u8 stat)
655 ide_hwif_t *hwif = HWIF(drive);
659 local_irq_set(flags);
660 printk("%s: %s: status=0x%02x", drive->name, msg, stat);
662 if (stat & BUSY_STAT)
665 if (stat & READY_STAT) printk("DriveReady ");
666 if (stat & WRERR_STAT) printk("DeviceFault ");
667 if (stat & SEEK_STAT) printk("SeekComplete ");
668 if (stat & DRQ_STAT) printk("DataRequest ");
669 if (stat & ECC_STAT) printk("CorrectedError ");
670 if (stat & INDEX_STAT) printk("Index ");
671 if (stat & ERR_STAT) printk("Error ");
675 if ((stat & (BUSY_STAT|ERR_STAT)) == ERR_STAT) {
676 err = hwif->INB(IDE_ERROR_REG);
677 printk("%s: %s: error=0x%02x", drive->name, msg, err);
679 if (err & ABRT_ERR) printk("DriveStatusError ");
681 printk("Bad%s ", (err & ABRT_ERR) ? "CRC" : "Sector");
682 if (err & ECC_ERR) printk("UncorrectableError ");
683 if (err & ID_ERR) printk("SectorIdNotFound ");
684 if (err & TRK0_ERR) printk("TrackZeroNotFound ");
685 if (err & MARK_ERR) printk("AddrMarkNotFound ");
687 if ((err & (BBD_ERR | ABRT_ERR)) == BBD_ERR ||
688 (err & (ECC_ERR|ID_ERR|MARK_ERR))) {
689 if (drive->addressing == 1) {
691 u32 low = 0, high = 0;
692 low = ide_read_24(drive);
693 hwif->OUTB(drive->ctl|0x80, IDE_CONTROL_REG);
694 high = ide_read_24(drive);
695 sectors = ((__u64)high << 24) | low;
696 printk(", LBAsect=%llu, high=%d, low=%d",
697 (unsigned long long) sectors,
700 u8 cur = hwif->INB(IDE_SELECT_REG);
701 if (cur & 0x40) { /* using LBA? */
702 printk(", LBAsect=%ld", (unsigned long)
704 |(hwif->INB(IDE_HCYL_REG)<<16)
705 |(hwif->INB(IDE_LCYL_REG)<<8)
706 | hwif->INB(IDE_SECTOR_REG));
708 printk(", CHS=%d/%d/%d",
709 (hwif->INB(IDE_HCYL_REG)<<8) +
710 hwif->INB(IDE_LCYL_REG),
712 hwif->INB(IDE_SECTOR_REG));
715 if (HWGROUP(drive) && HWGROUP(drive)->rq)
716 printk(", sector=%llu",
717 (unsigned long long)HWGROUP(drive)->rq->sector);
723 unsigned char opcode = 0;
726 spin_lock(&ide_lock);
727 rq = HWGROUP(drive)->rq;
728 spin_unlock(&ide_lock);
731 if (rq->flags & (REQ_DRIVE_CMD | REQ_DRIVE_TASK)) {
732 char *args = rq->buffer;
737 } else if (rq->flags & REQ_DRIVE_TASKFILE) {
738 ide_task_t *args = rq->special;
740 task_struct_t *tf = (task_struct_t *) args->tfRegister;
741 opcode = tf->command;
745 printk("ide: failed opcode was: ");
749 printk("0x%02x\n", opcode);
752 local_irq_restore(flags);
756 ide_startstop_t idedisk_error (ide_drive_t *drive, const char *msg, u8 stat)
762 err = idedisk_dump_status(drive, msg, stat);
764 if (drive == NULL || (rq = HWGROUP(drive)->rq) == NULL)
768 /* retry only "normal" I/O: */
769 if (rq->flags & (REQ_DRIVE_CMD | REQ_DRIVE_TASK | REQ_DRIVE_TASKFILE)) {
771 ide_end_drive_cmd(drive, stat, err);
774 #ifdef CONFIG_IDE_TASKFILE_IO
775 /* make rq completion pointers new submission pointers */
776 blk_rq_prep_restart(rq);
779 if (stat & BUSY_STAT || ((stat & WRERR_STAT) && !drive->nowerr)) {
780 /* other bits are useless when BUSY */
781 rq->errors |= ERROR_RESET;
782 } else if (stat & ERR_STAT) {
783 /* err has different meaning on cdrom and tape */
784 if (err == ABRT_ERR) {
785 if (drive->select.b.lba &&
786 /* some newer drives don't support WIN_SPECIFY */
787 hwif->INB(IDE_COMMAND_REG) == WIN_SPECIFY)
789 } else if ((err & BAD_CRC) == BAD_CRC) {
790 /* UDMA crc error, just retry the operation */
792 } else if (err & (BBD_ERR | ECC_ERR)) {
793 /* retries won't help these */
794 rq->errors = ERROR_MAX;
795 } else if (err & TRK0_ERR) {
796 /* help it find track zero */
797 rq->errors |= ERROR_RECAL;
800 if ((stat & DRQ_STAT) && rq_data_dir(rq) == READ)
801 try_to_flush_leftover_data(drive);
802 if (hwif->INB(IDE_STATUS_REG) & (BUSY_STAT|DRQ_STAT)) {
804 hwif->OUTB(WIN_IDLEIMMEDIATE,IDE_COMMAND_REG);
806 if (rq->errors >= ERROR_MAX || blk_noretry_request(rq))
807 DRIVER(drive)->end_request(drive, 0, 0);
809 if ((rq->errors & ERROR_RESET) == ERROR_RESET) {
811 return ide_do_reset(drive);
813 if ((rq->errors & ERROR_RECAL) == ERROR_RECAL)
814 drive->special.b.recalibrate = 1;
820 ide_startstop_t idedisk_abort(ide_drive_t *drive, const char *msg)
825 if (drive == NULL || (rq = HWGROUP(drive)->rq) == NULL)
830 if (rq->flags & (REQ_DRIVE_CMD | REQ_DRIVE_TASK | REQ_DRIVE_TASKFILE)) {
832 ide_end_drive_cmd(drive, BUSY_STAT, 0);
836 DRIVER(drive)->end_request(drive, 0, 0);
841 * Queries for true maximum capacity of the drive.
842 * Returns maximum LBA address (> 0) of the drive, 0 if failed.
844 static unsigned long idedisk_read_native_max_address(ide_drive_t *drive)
847 unsigned long addr = 0;
849 /* Create IDE/ATA command request structure */
850 memset(&args, 0, sizeof(ide_task_t));
851 args.tfRegister[IDE_SELECT_OFFSET] = 0x40;
852 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_READ_NATIVE_MAX;
853 args.command_type = IDE_DRIVE_TASK_NO_DATA;
854 args.handler = &task_no_data_intr;
855 /* submit command request */
856 ide_raw_taskfile(drive, &args, NULL);
858 /* if OK, compute maximum address value */
859 if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
860 addr = ((args.tfRegister[IDE_SELECT_OFFSET] & 0x0f) << 24)
861 | ((args.tfRegister[ IDE_HCYL_OFFSET] ) << 16)
862 | ((args.tfRegister[ IDE_LCYL_OFFSET] ) << 8)
863 | ((args.tfRegister[IDE_SECTOR_OFFSET] ));
864 addr++; /* since the return value is (maxlba - 1), we add 1 */
869 static unsigned long long idedisk_read_native_max_address_ext(ide_drive_t *drive)
872 unsigned long long addr = 0;
874 /* Create IDE/ATA command request structure */
875 memset(&args, 0, sizeof(ide_task_t));
877 args.tfRegister[IDE_SELECT_OFFSET] = 0x40;
878 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_READ_NATIVE_MAX_EXT;
879 args.command_type = IDE_DRIVE_TASK_NO_DATA;
880 args.handler = &task_no_data_intr;
881 /* submit command request */
882 ide_raw_taskfile(drive, &args, NULL);
884 /* if OK, compute maximum address value */
885 if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
886 u32 high = (args.hobRegister[IDE_HCYL_OFFSET] << 16) |
887 (args.hobRegister[IDE_LCYL_OFFSET] << 8) |
888 args.hobRegister[IDE_SECTOR_OFFSET];
889 u32 low = ((args.tfRegister[IDE_HCYL_OFFSET])<<16) |
890 ((args.tfRegister[IDE_LCYL_OFFSET])<<8) |
891 (args.tfRegister[IDE_SECTOR_OFFSET]);
892 addr = ((__u64)high << 24) | low;
893 addr++; /* since the return value is (maxlba - 1), we add 1 */
899 * Sets maximum virtual LBA address of the drive.
900 * Returns new maximum virtual LBA address (> 0) or 0 on failure.
902 static unsigned long idedisk_set_max_address(ide_drive_t *drive, unsigned long addr_req)
905 unsigned long addr_set = 0;
908 /* Create IDE/ATA command request structure */
909 memset(&args, 0, sizeof(ide_task_t));
910 args.tfRegister[IDE_SECTOR_OFFSET] = ((addr_req >> 0) & 0xff);
911 args.tfRegister[IDE_LCYL_OFFSET] = ((addr_req >> 8) & 0xff);
912 args.tfRegister[IDE_HCYL_OFFSET] = ((addr_req >> 16) & 0xff);
913 args.tfRegister[IDE_SELECT_OFFSET] = ((addr_req >> 24) & 0x0f) | 0x40;
914 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SET_MAX;
915 args.command_type = IDE_DRIVE_TASK_NO_DATA;
916 args.handler = &task_no_data_intr;
917 /* submit command request */
918 ide_raw_taskfile(drive, &args, NULL);
919 /* if OK, read new maximum address value */
920 if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
921 addr_set = ((args.tfRegister[IDE_SELECT_OFFSET] & 0x0f) << 24)
922 | ((args.tfRegister[ IDE_HCYL_OFFSET] ) << 16)
923 | ((args.tfRegister[ IDE_LCYL_OFFSET] ) << 8)
924 | ((args.tfRegister[IDE_SECTOR_OFFSET] ));
930 static unsigned long long idedisk_set_max_address_ext(ide_drive_t *drive, unsigned long long addr_req)
933 unsigned long long addr_set = 0;
936 /* Create IDE/ATA command request structure */
937 memset(&args, 0, sizeof(ide_task_t));
938 args.tfRegister[IDE_SECTOR_OFFSET] = ((addr_req >> 0) & 0xff);
939 args.tfRegister[IDE_LCYL_OFFSET] = ((addr_req >>= 8) & 0xff);
940 args.tfRegister[IDE_HCYL_OFFSET] = ((addr_req >>= 8) & 0xff);
941 args.tfRegister[IDE_SELECT_OFFSET] = 0x40;
942 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SET_MAX_EXT;
943 args.hobRegister[IDE_SECTOR_OFFSET] = (addr_req >>= 8) & 0xff;
944 args.hobRegister[IDE_LCYL_OFFSET] = (addr_req >>= 8) & 0xff;
945 args.hobRegister[IDE_HCYL_OFFSET] = (addr_req >>= 8) & 0xff;
946 args.hobRegister[IDE_SELECT_OFFSET] = 0x40;
947 args.hobRegister[IDE_CONTROL_OFFSET_HOB]= (drive->ctl|0x80);
948 args.command_type = IDE_DRIVE_TASK_NO_DATA;
949 args.handler = &task_no_data_intr;
950 /* submit command request */
951 ide_raw_taskfile(drive, &args, NULL);
952 /* if OK, compute maximum address value */
953 if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
954 u32 high = (args.hobRegister[IDE_HCYL_OFFSET] << 16) |
955 (args.hobRegister[IDE_LCYL_OFFSET] << 8) |
956 args.hobRegister[IDE_SECTOR_OFFSET];
957 u32 low = ((args.tfRegister[IDE_HCYL_OFFSET])<<16) |
958 ((args.tfRegister[IDE_LCYL_OFFSET])<<8) |
959 (args.tfRegister[IDE_SECTOR_OFFSET]);
960 addr_set = ((__u64)high << 24) | low;
966 static unsigned long long sectors_to_MB(unsigned long long n)
968 n <<= 9; /* make it bytes */
969 do_div(n, 1000000); /* make it MB */
974 * Bits 10 of command_set_1 and cfs_enable_1 must be equal,
975 * so on non-buggy drives we need test only one.
976 * However, we should also check whether these fields are valid.
978 static inline int idedisk_supports_hpa(const struct hd_driveid *id)
980 return (id->command_set_1 & 0x0400) && (id->cfs_enable_1 & 0x0400);
986 static inline int idedisk_supports_lba48(const struct hd_driveid *id)
988 return (id->command_set_2 & 0x0400) && (id->cfs_enable_2 & 0x0400)
989 && id->lba_capacity_2;
992 static inline void idedisk_check_hpa(ide_drive_t *drive)
994 unsigned long long capacity, set_max;
995 int lba48 = idedisk_supports_lba48(drive->id);
997 capacity = drive->capacity64;
999 set_max = idedisk_read_native_max_address_ext(drive);
1001 set_max = idedisk_read_native_max_address(drive);
1003 if (set_max <= capacity)
1006 printk(KERN_INFO "%s: Host Protected Area detected.\n"
1007 "\tcurrent capacity is %llu sectors (%llu MB)\n"
1008 "\tnative capacity is %llu sectors (%llu MB)\n",
1010 capacity, sectors_to_MB(capacity),
1011 set_max, sectors_to_MB(set_max));
1017 set_max = idedisk_set_max_address_ext(drive, set_max);
1019 set_max = idedisk_set_max_address(drive, set_max);
1021 drive->capacity64 = set_max;
1022 printk(KERN_INFO "%s: Host Protected Area disabled.\n",
1028 * Compute drive->capacity, the full capacity of the drive
1029 * Called with drive->id != NULL.
1031 * To compute capacity, this uses either of
1033 * 1. CHS value set by user (whatever user sets will be trusted)
1034 * 2. LBA value from target drive (require new ATA feature)
1035 * 3. LBA value from system BIOS (new one is OK, old one may break)
1036 * 4. CHS value from system BIOS (traditional style)
1038 * in above order (i.e., if value of higher priority is available,
1039 * reset will be ignored).
1041 static void init_idedisk_capacity (ide_drive_t *drive)
1043 struct hd_driveid *id = drive->id;
1045 * If this drive supports the Host Protected Area feature set,
1046 * then we may need to change our opinion about the drive's capacity.
1048 int hpa = idedisk_supports_hpa(id);
1050 if (idedisk_supports_lba48(id)) {
1051 /* drive speaks 48-bit LBA */
1052 drive->select.b.lba = 1;
1053 drive->capacity64 = id->lba_capacity_2;
1055 idedisk_check_hpa(drive);
1056 } else if ((id->capability & 2) && lba_capacity_is_ok(id)) {
1057 /* drive speaks 28-bit LBA */
1058 drive->select.b.lba = 1;
1059 drive->capacity64 = id->lba_capacity;
1061 idedisk_check_hpa(drive);
1063 /* drive speaks boring old 28-bit CHS */
1064 drive->capacity64 = drive->cyl * drive->head * drive->sect;
1068 static sector_t idedisk_capacity (ide_drive_t *drive)
1070 return drive->capacity64 - drive->sect0;
1073 static ide_startstop_t idedisk_special (ide_drive_t *drive)
1075 special_t *s = &drive->special;
1077 if (s->b.set_geometry) {
1078 s->b.set_geometry = 0;
1079 if (!IS_PDC4030_DRIVE) {
1081 memset(&args, 0, sizeof(ide_task_t));
1082 args.tfRegister[IDE_NSECTOR_OFFSET] = drive->sect;
1083 args.tfRegister[IDE_SECTOR_OFFSET] = drive->sect;
1084 args.tfRegister[IDE_LCYL_OFFSET] = drive->cyl;
1085 args.tfRegister[IDE_HCYL_OFFSET] = drive->cyl>>8;
1086 args.tfRegister[IDE_SELECT_OFFSET] = ((drive->head-1)|drive->select.all)&0xBF;
1087 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SPECIFY;
1088 args.command_type = IDE_DRIVE_TASK_NO_DATA;
1089 args.handler = &set_geometry_intr;
1090 do_rw_taskfile(drive, &args);
1092 } else if (s->b.recalibrate) {
1093 s->b.recalibrate = 0;
1094 if (!IS_PDC4030_DRIVE) {
1096 memset(&args, 0, sizeof(ide_task_t));
1097 args.tfRegister[IDE_NSECTOR_OFFSET] = drive->sect;
1098 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_RESTORE;
1099 args.command_type = IDE_DRIVE_TASK_NO_DATA;
1100 args.handler = &recal_intr;
1101 do_rw_taskfile(drive, &args);
1103 } else if (s->b.set_multmode) {
1104 s->b.set_multmode = 0;
1105 if (drive->mult_req > drive->id->max_multsect)
1106 drive->mult_req = drive->id->max_multsect;
1107 if (!IS_PDC4030_DRIVE) {
1109 memset(&args, 0, sizeof(ide_task_t));
1110 args.tfRegister[IDE_NSECTOR_OFFSET] = drive->mult_req;
1111 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SETMULT;
1112 args.command_type = IDE_DRIVE_TASK_NO_DATA;
1113 args.handler = &set_multmode_intr;
1114 do_rw_taskfile(drive, &args);
1116 } else if (s->all) {
1117 int special = s->all;
1119 printk(KERN_ERR "%s: bad special flag: 0x%02x\n", drive->name, special);
1122 return IS_PDC4030_DRIVE ? ide_stopped : ide_started;
1125 static void idedisk_pre_reset (ide_drive_t *drive)
1127 int legacy = (drive->id->cfs_enable_2 & 0x0400) ? 0 : 1;
1129 drive->special.all = 0;
1130 drive->special.b.set_geometry = legacy;
1131 drive->special.b.recalibrate = legacy;
1132 if (OK_TO_RESET_CONTROLLER)
1133 drive->mult_count = 0;
1134 if (!drive->keep_settings && !drive->using_dma)
1135 drive->mult_req = 0;
1136 if (drive->mult_req != drive->mult_count)
1137 drive->special.b.set_multmode = 1;
1140 #ifdef CONFIG_PROC_FS
1142 static int smart_enable(ide_drive_t *drive)
1146 memset(&args, 0, sizeof(ide_task_t));
1147 args.tfRegister[IDE_FEATURE_OFFSET] = SMART_ENABLE;
1148 args.tfRegister[IDE_LCYL_OFFSET] = SMART_LCYL_PASS;
1149 args.tfRegister[IDE_HCYL_OFFSET] = SMART_HCYL_PASS;
1150 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SMART;
1151 args.command_type = IDE_DRIVE_TASK_NO_DATA;
1152 args.handler = &task_no_data_intr;
1153 return ide_raw_taskfile(drive, &args, NULL);
1156 static int get_smart_values(ide_drive_t *drive, u8 *buf)
1160 memset(&args, 0, sizeof(ide_task_t));
1161 args.tfRegister[IDE_FEATURE_OFFSET] = SMART_READ_VALUES;
1162 args.tfRegister[IDE_NSECTOR_OFFSET] = 0x01;
1163 args.tfRegister[IDE_LCYL_OFFSET] = SMART_LCYL_PASS;
1164 args.tfRegister[IDE_HCYL_OFFSET] = SMART_HCYL_PASS;
1165 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SMART;
1166 args.command_type = IDE_DRIVE_TASK_IN;
1167 args.data_phase = TASKFILE_IN;
1168 args.handler = &task_in_intr;
1169 (void) smart_enable(drive);
1170 return ide_raw_taskfile(drive, &args, buf);
1173 static int get_smart_thresholds(ide_drive_t *drive, u8 *buf)
1176 memset(&args, 0, sizeof(ide_task_t));
1177 args.tfRegister[IDE_FEATURE_OFFSET] = SMART_READ_THRESHOLDS;
1178 args.tfRegister[IDE_NSECTOR_OFFSET] = 0x01;
1179 args.tfRegister[IDE_LCYL_OFFSET] = SMART_LCYL_PASS;
1180 args.tfRegister[IDE_HCYL_OFFSET] = SMART_HCYL_PASS;
1181 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SMART;
1182 args.command_type = IDE_DRIVE_TASK_IN;
1183 args.handler = &task_in_intr;
1184 (void) smart_enable(drive);
1185 return ide_raw_taskfile(drive, &args, buf);
1188 static int proc_idedisk_read_cache
1189 (char *page, char **start, off_t off, int count, int *eof, void *data)
1196 drive = ide_drive_from_key(data);
1197 if (drive && drive->id_read)
1198 len = sprintf(out,"%i\n", drive->id->buf_size / 2);
1200 len = sprintf(out,"(none)\n");
1202 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
1205 static int proc_idedisk_read_smart_thresholds
1206 (char *page, char **start, off_t off, int count, int *eof, void *data)
1213 drive = ide_drive_from_key(data);
1214 if (drive && !get_smart_thresholds(drive, page)) {
1215 unsigned short *val = (unsigned short *) page;
1216 char *out = ((char *)val) + (SECTOR_WORDS * 4);
1219 out += sprintf(out, "%04x%c", le16_to_cpu(*val), (++i & 7) ? ' ' : '\n');
1221 } while (i < (SECTOR_WORDS * 2));
1225 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
1228 static int proc_idedisk_read_smart_values
1229 (char *page, char **start, off_t off, int count, int *eof, void *data)
1236 drive = ide_drive_from_key(data);
1237 if (drive && !get_smart_values(drive, page)) {
1238 unsigned short *val = (unsigned short *) page;
1239 char *out = ((char *)val) + (SECTOR_WORDS * 4);
1242 out += sprintf(out, "%04x%c", le16_to_cpu(*val), (++i & 7) ? ' ' : '\n');
1244 } while (i < (SECTOR_WORDS * 2));
1248 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
1251 static ide_proc_entry_t idedisk_proc[] = {
1252 { "cache", S_IFREG|S_IRUGO, proc_idedisk_read_cache, NULL },
1253 { "geometry", S_IFREG|S_IRUGO, proc_ide_read_geometry, NULL },
1254 { "smart_values", S_IFREG|S_IRUSR, proc_idedisk_read_smart_values, NULL },
1255 { "smart_thresholds", S_IFREG|S_IRUSR, proc_idedisk_read_smart_thresholds, NULL },
1256 { NULL, 0, NULL, NULL }
1261 #define idedisk_proc NULL
1263 #endif /* CONFIG_PROC_FS */
1265 static int idedisk_issue_flush(request_queue_t *q, struct gendisk *disk,
1266 sector_t *error_sector)
1268 ide_drive_t *drive = q->queuedata;
1275 rq = blk_get_request(q, WRITE, __GFP_WAIT);
1277 memset(rq->cmd, 0, sizeof(rq->cmd));
1279 if (ide_id_has_flush_cache_ext(drive->id) &&
1280 (drive->capacity64 >= (1UL << 28)))
1281 rq->cmd[0] = WIN_FLUSH_CACHE_EXT;
1283 rq->cmd[0] = WIN_FLUSH_CACHE;
1286 rq->flags |= REQ_DRIVE_TASK | REQ_SOFTBARRIER;
1287 rq->buffer = rq->cmd;
1289 ret = blk_execute_rq(q, disk, rq);
1292 * if we failed and caller wants error offset, get it
1294 if (ret && error_sector)
1295 *error_sector = ide_get_error_location(drive, rq->cmd);
1297 blk_put_request(rq);
1302 * This is tightly woven into the driver->do_special can not touch.
1303 * DON'T do it again until a total personality rewrite is committed.
1305 static int set_multcount(ide_drive_t *drive, int arg)
1309 if (drive->special.b.set_multmode)
1311 ide_init_drive_cmd (&rq);
1312 rq.flags = REQ_DRIVE_CMD;
1313 drive->mult_req = arg;
1314 drive->special.b.set_multmode = 1;
1315 (void) ide_do_drive_cmd (drive, &rq, ide_wait);
1316 return (drive->mult_count == arg) ? 0 : -EIO;
1319 static int set_nowerr(ide_drive_t *drive, int arg)
1321 if (ide_spin_wait_hwgroup(drive))
1323 drive->nowerr = arg;
1324 drive->bad_wstat = arg ? BAD_R_STAT : BAD_W_STAT;
1325 spin_unlock_irq(&ide_lock);
1329 static int write_cache(ide_drive_t *drive, int arg)
1334 if (!ide_id_has_flush_cache(drive->id))
1337 memset(&args, 0, sizeof(ide_task_t));
1338 args.tfRegister[IDE_FEATURE_OFFSET] = (arg) ?
1339 SETFEATURES_EN_WCACHE : SETFEATURES_DIS_WCACHE;
1340 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SETFEATURES;
1341 args.command_type = IDE_DRIVE_TASK_NO_DATA;
1342 args.handler = &task_no_data_intr;
1344 err = ide_raw_taskfile(drive, &args, NULL);
1348 drive->wcache = arg;
1352 static int do_idedisk_flushcache (ide_drive_t *drive)
1356 memset(&args, 0, sizeof(ide_task_t));
1357 if (ide_id_has_flush_cache_ext(drive->id))
1358 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE_EXT;
1360 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE;
1361 args.command_type = IDE_DRIVE_TASK_NO_DATA;
1362 args.handler = &task_no_data_intr;
1363 return ide_raw_taskfile(drive, &args, NULL);
1366 static int set_acoustic (ide_drive_t *drive, int arg)
1370 memset(&args, 0, sizeof(ide_task_t));
1371 args.tfRegister[IDE_FEATURE_OFFSET] = (arg) ? SETFEATURES_EN_AAM :
1372 SETFEATURES_DIS_AAM;
1373 args.tfRegister[IDE_NSECTOR_OFFSET] = arg;
1374 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SETFEATURES;
1375 args.command_type = IDE_DRIVE_TASK_NO_DATA;
1376 args.handler = &task_no_data_intr;
1377 ide_raw_taskfile(drive, &args, NULL);
1378 drive->acoustic = arg;
1383 * drive->addressing:
1386 * 2: 48-bit capable doing 28-bit
1388 static int set_lba_addressing(ide_drive_t *drive, int arg)
1390 drive->addressing = 0;
1392 if (HWIF(drive)->no_lba48)
1395 if (!idedisk_supports_lba48(drive->id))
1397 drive->addressing = arg;
1401 static void idedisk_add_settings(ide_drive_t *drive)
1403 struct hd_driveid *id = drive->id;
1405 ide_add_setting(drive, "bios_cyl", SETTING_RW, -1, -1, TYPE_INT, 0, 65535, 1, 1, &drive->bios_cyl, NULL);
1406 ide_add_setting(drive, "bios_head", SETTING_RW, -1, -1, TYPE_BYTE, 0, 255, 1, 1, &drive->bios_head, NULL);
1407 ide_add_setting(drive, "bios_sect", SETTING_RW, -1, -1, TYPE_BYTE, 0, 63, 1, 1, &drive->bios_sect, NULL);
1408 ide_add_setting(drive, "address", SETTING_RW, HDIO_GET_ADDRESS, HDIO_SET_ADDRESS, TYPE_INTA, 0, 2, 1, 1, &drive->addressing, set_lba_addressing);
1409 ide_add_setting(drive, "bswap", SETTING_READ, -1, -1, TYPE_BYTE, 0, 1, 1, 1, &drive->bswap, NULL);
1410 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);
1411 ide_add_setting(drive, "nowerr", SETTING_RW, HDIO_GET_NOWERR, HDIO_SET_NOWERR, TYPE_BYTE, 0, 1, 1, 1, &drive->nowerr, set_nowerr);
1412 ide_add_setting(drive, "lun", SETTING_RW, -1, -1, TYPE_INT, 0, 7, 1, 1, &drive->lun, NULL);
1413 ide_add_setting(drive, "wcache", SETTING_RW, HDIO_GET_WCACHE, HDIO_SET_WCACHE, TYPE_BYTE, 0, 1, 1, 1, &drive->wcache, write_cache);
1414 ide_add_setting(drive, "acoustic", SETTING_RW, HDIO_GET_ACOUSTIC, HDIO_SET_ACOUSTIC, TYPE_BYTE, 0, 254, 1, 1, &drive->acoustic, set_acoustic);
1415 ide_add_setting(drive, "failures", SETTING_RW, -1, -1, TYPE_INT, 0, 65535, 1, 1, &drive->failures, NULL);
1416 ide_add_setting(drive, "max_failures", SETTING_RW, -1, -1, TYPE_INT, 0, 65535, 1, 1, &drive->max_failures, NULL);
1420 * Power Management state machine. This one is rather trivial for now,
1421 * we should probably add more, like switching back to PIO on suspend
1422 * to help some BIOSes, re-do the door locking on resume, etc...
1426 idedisk_pm_flush_cache = ide_pm_state_start_suspend,
1429 idedisk_pm_idle = ide_pm_state_start_resume,
1430 idedisk_pm_restore_dma,
1433 static void idedisk_complete_power_step (ide_drive_t *drive, struct request *rq, u8 stat, u8 error)
1435 switch (rq->pm->pm_step) {
1436 case idedisk_pm_flush_cache: /* Suspend step 1 (flush cache) complete */
1437 if (rq->pm->pm_state == 4)
1438 rq->pm->pm_step = ide_pm_state_completed;
1440 rq->pm->pm_step = idedisk_pm_standby;
1442 case idedisk_pm_standby: /* Suspend step 2 (standby) complete */
1443 rq->pm->pm_step = ide_pm_state_completed;
1445 case idedisk_pm_idle: /* Resume step 1 (idle) complete */
1446 rq->pm->pm_step = idedisk_pm_restore_dma;
1451 static ide_startstop_t idedisk_start_power_step (ide_drive_t *drive, struct request *rq)
1453 ide_task_t *args = rq->special;
1455 memset(args, 0, sizeof(*args));
1457 switch (rq->pm->pm_step) {
1458 case idedisk_pm_flush_cache: /* Suspend step 1 (flush cache) */
1459 /* Not supported? Switch to next step now. */
1460 if (!drive->wcache || !ide_id_has_flush_cache(drive->id)) {
1461 idedisk_complete_power_step(drive, rq, 0, 0);
1464 if (ide_id_has_flush_cache_ext(drive->id))
1465 args->tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE_EXT;
1467 args->tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE;
1468 args->command_type = IDE_DRIVE_TASK_NO_DATA;
1469 args->handler = &task_no_data_intr;
1470 return do_rw_taskfile(drive, args);
1472 case idedisk_pm_standby: /* Suspend step 2 (standby) */
1473 args->tfRegister[IDE_COMMAND_OFFSET] = WIN_STANDBYNOW1;
1474 args->command_type = IDE_DRIVE_TASK_NO_DATA;
1475 args->handler = &task_no_data_intr;
1476 return do_rw_taskfile(drive, args);
1478 case idedisk_pm_idle: /* Resume step 1 (idle) */
1479 args->tfRegister[IDE_COMMAND_OFFSET] = WIN_IDLEIMMEDIATE;
1480 args->command_type = IDE_DRIVE_TASK_NO_DATA;
1481 args->handler = task_no_data_intr;
1482 return do_rw_taskfile(drive, args);
1484 case idedisk_pm_restore_dma: /* Resume step 2 (restore DMA) */
1486 * Right now, all we do is call hwif->ide_dma_check(drive),
1487 * we could be smarter and check for current xfer_speed
1488 * in struct drive etc...
1489 * Also, this step could be implemented as a generic helper
1490 * as most subdrivers will use it
1492 if ((drive->id->capability & 1) == 0)
1494 if (HWIF(drive)->ide_dma_check == NULL)
1496 HWIF(drive)->ide_dma_check(drive);
1499 rq->pm->pm_step = ide_pm_state_completed;
1503 static void idedisk_setup (ide_drive_t *drive)
1505 struct hd_driveid *id = drive->id;
1506 unsigned long long capacity;
1509 idedisk_add_settings(drive);
1511 if (drive->id_read == 0)
1515 * CompactFlash cards and their brethern look just like hard drives
1516 * to us, but they are removable and don't have a doorlock mechanism.
1518 if (drive->removable && !(drive->is_flash)) {
1520 * Removable disks (eg. SYQUEST); ignore 'WD' drives
1522 if (id->model[0] != 'W' || id->model[1] != 'D') {
1523 drive->doorlocking = 1;
1527 (void)set_lba_addressing(drive, 1);
1529 if (drive->addressing == 1) {
1530 ide_hwif_t *hwif = HWIF(drive);
1533 if (max_s > hwif->rqsize)
1534 max_s = hwif->rqsize;
1536 blk_queue_max_sectors(drive->queue, max_s);
1539 printk(KERN_INFO "%s: max request size: %dKiB\n", drive->name, drive->queue->max_sectors / 2);
1541 /* Extract geometry if we did not already have one for the drive */
1542 if (!drive->cyl || !drive->head || !drive->sect) {
1543 drive->cyl = drive->bios_cyl = id->cyls;
1544 drive->head = drive->bios_head = id->heads;
1545 drive->sect = drive->bios_sect = id->sectors;
1548 /* Handle logical geometry translation by the drive */
1549 if ((id->field_valid & 1) && id->cur_cyls &&
1550 id->cur_heads && (id->cur_heads <= 16) && id->cur_sectors) {
1551 drive->cyl = id->cur_cyls;
1552 drive->head = id->cur_heads;
1553 drive->sect = id->cur_sectors;
1556 /* Use physical geometry if what we have still makes no sense */
1557 if (drive->head > 16 && id->heads && id->heads <= 16) {
1558 drive->cyl = id->cyls;
1559 drive->head = id->heads;
1560 drive->sect = id->sectors;
1563 /* calculate drive capacity, and select LBA if possible */
1564 init_idedisk_capacity (drive);
1566 /* limit drive capacity to 137GB if LBA48 cannot be used */
1567 if (drive->addressing == 0 && drive->capacity64 > 1ULL << 28) {
1568 printk(KERN_WARNING "%s: cannot use LBA48 - full capacity "
1569 "%llu sectors (%llu MB)\n",
1570 drive->name, (unsigned long long)drive->capacity64,
1571 sectors_to_MB(drive->capacity64));
1572 drive->capacity64 = 1ULL << 28;
1575 if (drive->hwif->no_lba48_dma && drive->addressing) {
1576 if (drive->capacity64 > 1ULL << 28) {
1577 printk(KERN_INFO "%s: cannot use LBA48 DMA - PIO mode will"
1578 " be used for accessing sectors > %u\n",
1579 drive->name, 1 << 28);
1581 drive->addressing = 0;
1585 * if possible, give fdisk access to more of the drive,
1586 * by correcting bios_cyls:
1588 capacity = idedisk_capacity (drive);
1589 if (!drive->forced_geom) {
1591 if (idedisk_supports_lba48(drive->id)) {
1593 drive->bios_sect = 63;
1594 drive->bios_head = 255;
1597 if (drive->bios_sect && drive->bios_head) {
1598 unsigned int cap0 = capacity; /* truncate to 32 bits */
1599 unsigned int cylsz, cyl;
1601 if (cap0 != capacity)
1602 drive->bios_cyl = 65535;
1604 cylsz = drive->bios_sect * drive->bios_head;
1608 if (cyl > drive->bios_cyl)
1609 drive->bios_cyl = cyl;
1613 printk(KERN_INFO "%s: %llu sectors (%llu MB)",
1614 drive->name, capacity, sectors_to_MB(capacity));
1616 /* Only print cache size when it was specified */
1618 printk (" w/%dKiB Cache", id->buf_size/2);
1621 printk(", CHS=%d/%d/%d",
1622 drive->bios_cyl, drive->bios_head, drive->bios_sect);
1624 if (drive->using_dma)
1625 (void) HWIF(drive)->ide_dma_verbose(drive);
1628 drive->mult_count = 0;
1629 if (id->max_multsect) {
1630 #ifdef CONFIG_IDEDISK_MULTI_MODE
1631 id->multsect = ((id->max_multsect/2) > 1) ? id->max_multsect : 0;
1632 id->multsect_valid = id->multsect ? 1 : 0;
1633 drive->mult_req = id->multsect_valid ? id->max_multsect : INITIAL_MULT_COUNT;
1634 drive->special.b.set_multmode = drive->mult_req ? 1 : 0;
1635 #else /* original, pre IDE-NFG, per request of AC */
1636 drive->mult_req = INITIAL_MULT_COUNT;
1637 if (drive->mult_req > id->max_multsect)
1638 drive->mult_req = id->max_multsect;
1639 if (drive->mult_req || ((id->multsect_valid & 1) && id->multsect))
1640 drive->special.b.set_multmode = 1;
1641 #endif /* CONFIG_IDEDISK_MULTI_MODE */
1643 drive->no_io_32bit = id->dword_io ? 1 : 0;
1645 /* write cache enabled? */
1646 if ((id->csfo & 1) || (id->cfs_enable_1 & (1 << 5)))
1649 write_cache(drive, 1);
1652 * We must avoid issuing commands a drive does not understand
1653 * or we may crash it. We check flush cache is supported. We also
1654 * check we have the LBA48 flush cache if the drive capacity is
1655 * too large. By this time we have trimmed the drive capacity if
1656 * LBA48 is not available so we don't need to recheck that.
1659 if (ide_id_has_flush_cache(id))
1661 if (drive->addressing == 1) {
1662 /* Can't issue the correct flush ? */
1663 if (capacity > (1ULL << 28) && !ide_id_has_flush_cache_ext(id))
1667 printk(KERN_DEBUG "%s: cache flushes %ssupported\n",
1668 drive->name, barrier ? "" : "not ");
1670 blk_queue_ordered(drive->queue, 1);
1671 blk_queue_issue_flush_fn(drive->queue, idedisk_issue_flush);
1675 static void ide_cacheflush_p(ide_drive_t *drive)
1677 if (!drive->wcache || !ide_id_has_flush_cache(drive->id))
1680 if (do_idedisk_flushcache(drive))
1681 printk(KERN_INFO "%s: wcache flush failed!\n", drive->name);
1684 static int idedisk_cleanup (ide_drive_t *drive)
1686 struct gendisk *g = drive->disk;
1687 ide_cacheflush_p(drive);
1688 if (ide_unregister_subdriver(drive))
1691 drive->devfs_name[0] = '\0';
1696 static int idedisk_attach(ide_drive_t *drive);
1698 static void ide_device_shutdown(struct device *dev)
1700 ide_drive_t *drive = container_of(dev, ide_drive_t, gendev);
1703 /* On Alpha, halt(8) doesn't actually turn the machine off,
1704 it puts you into the sort of firmware monitor. Typically,
1705 it's used to boot another kernel image, so it's not much
1706 different from reboot(8). Therefore, we don't need to
1707 spin down the disk in this case, especially since Alpha
1708 firmware doesn't handle disks in standby mode properly.
1709 On the other hand, it's reasonably safe to turn the power
1710 off when the shutdown process reaches the firmware prompt,
1711 as the firmware initialization takes rather long time -
1712 at least 10 seconds, which should be sufficient for
1713 the disk to expire its write cache. */
1714 if (system_state != SYSTEM_POWER_OFF) {
1716 if (system_state == SYSTEM_RESTART) {
1718 ide_cacheflush_p(drive);
1722 printk("Shutdown: %s\n", drive->name);
1723 dev->bus->suspend(dev, PM_SUSPEND_STANDBY);
1727 * IDE subdriver functions, registered with ide.c
1729 static ide_driver_t idedisk_driver = {
1730 .owner = THIS_MODULE,
1732 .shutdown = ide_device_shutdown,
1735 .version = IDEDISK_VERSION,
1738 .supports_dsc_overlap = 0,
1739 .cleanup = idedisk_cleanup,
1740 .do_request = ide_do_rw_disk,
1741 .sense = idedisk_dump_status,
1742 .error = idedisk_error,
1743 .abort = idedisk_abort,
1744 .pre_reset = idedisk_pre_reset,
1745 .capacity = idedisk_capacity,
1746 .special = idedisk_special,
1747 .proc = idedisk_proc,
1748 .attach = idedisk_attach,
1749 .drives = LIST_HEAD_INIT(idedisk_driver.drives),
1750 .start_power_step = idedisk_start_power_step,
1751 .complete_power_step = idedisk_complete_power_step,
1754 static int idedisk_open(struct inode *inode, struct file *filp)
1756 ide_drive_t *drive = inode->i_bdev->bd_disk->private_data;
1758 if (drive->removable && drive->usage == 1) {
1760 memset(&args, 0, sizeof(ide_task_t));
1761 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_DOORLOCK;
1762 args.command_type = IDE_DRIVE_TASK_NO_DATA;
1763 args.handler = &task_no_data_intr;
1764 check_disk_change(inode->i_bdev);
1766 * Ignore the return code from door_lock,
1767 * since the open() has already succeeded,
1768 * and the door_lock is irrelevant at this point.
1770 if (drive->doorlocking && ide_raw_taskfile(drive, &args, NULL))
1771 drive->doorlocking = 0;
1776 static int idedisk_release(struct inode *inode, struct file *filp)
1778 ide_drive_t *drive = inode->i_bdev->bd_disk->private_data;
1779 if (drive->usage == 1)
1780 ide_cacheflush_p(drive);
1781 if (drive->removable && drive->usage == 1) {
1783 memset(&args, 0, sizeof(ide_task_t));
1784 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_DOORUNLOCK;
1785 args.command_type = IDE_DRIVE_TASK_NO_DATA;
1786 args.handler = &task_no_data_intr;
1787 if (drive->doorlocking && ide_raw_taskfile(drive, &args, NULL))
1788 drive->doorlocking = 0;
1794 static int idedisk_ioctl(struct inode *inode, struct file *file,
1795 unsigned int cmd, unsigned long arg)
1797 struct block_device *bdev = inode->i_bdev;
1798 return generic_ide_ioctl(file, bdev, cmd, arg);
1801 static int idedisk_media_changed(struct gendisk *disk)
1803 ide_drive_t *drive = disk->private_data;
1805 /* do not scan partitions twice if this is a removable device */
1806 if (drive->attach) {
1810 /* if removable, always assume it was changed */
1811 return drive->removable;
1814 static int idedisk_revalidate_disk(struct gendisk *disk)
1816 ide_drive_t *drive = disk->private_data;
1817 set_capacity(disk, current_capacity(drive));
1821 static struct block_device_operations idedisk_ops = {
1822 .owner = THIS_MODULE,
1823 .open = idedisk_open,
1824 .release = idedisk_release,
1825 .ioctl = idedisk_ioctl,
1826 .media_changed = idedisk_media_changed,
1827 .revalidate_disk= idedisk_revalidate_disk
1830 MODULE_DESCRIPTION("ATA DISK Driver");
1832 static int idedisk_attach(ide_drive_t *drive)
1834 struct gendisk *g = drive->disk;
1836 /* strstr("foo", "") is non-NULL */
1837 if (!strstr("ide-disk", drive->driver_req))
1839 if (!drive->present)
1841 if (drive->media != ide_disk)
1844 if (ide_register_subdriver(drive, &idedisk_driver)) {
1845 printk (KERN_ERR "ide-disk: %s: Failed to register the driver with ide.c\n", drive->name);
1848 DRIVER(drive)->busy++;
1849 idedisk_setup(drive);
1850 if ((!drive->head || drive->head > 16) && !drive->select.b.lba) {
1851 printk(KERN_ERR "%s: INVALID GEOMETRY: %d PHYSICAL HEADS?\n",
1852 drive->name, drive->head);
1856 DRIVER(drive)->busy--;
1857 g->minors = 1 << PARTN_BITS;
1858 strcpy(g->devfs_name, drive->devfs_name);
1859 g->driverfs_dev = &drive->gendev;
1860 g->flags = drive->removable ? GENHD_FL_REMOVABLE : 0;
1861 set_capacity(g, current_capacity(drive));
1862 g->fops = &idedisk_ops;
1869 static void __exit idedisk_exit (void)
1871 ide_unregister_driver(&idedisk_driver);
1874 static int idedisk_init (void)
1876 return ide_register_driver(&idedisk_driver);
1879 module_init(idedisk_init);
1880 module_exit(idedisk_exit);
1881 MODULE_LICENSE("GPL");