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 */
48 #include <linux/config.h>
49 #include <linux/module.h>
50 #include <linux/types.h>
51 #include <linux/string.h>
52 #include <linux/kernel.h>
53 #include <linux/timer.h>
55 #include <linux/interrupt.h>
56 #include <linux/major.h>
57 #include <linux/errno.h>
58 #include <linux/genhd.h>
59 #include <linux/slab.h>
60 #include <linux/delay.h>
64 #include <linux/ide.h>
66 #include <asm/byteorder.h>
68 #include <asm/uaccess.h>
70 #include <asm/div64.h>
72 /* FIXME: some day we shouldn't need to look in here! */
74 #include "legacy/pdc4030.h"
77 * lba_capacity_is_ok() performs a sanity check on the claimed "lba_capacity"
78 * value for this drive (from its reported identification information).
80 * Returns: 1 if lba_capacity looks sensible
83 * It is called only once for each drive.
85 static int lba_capacity_is_ok (struct hd_driveid *id)
87 unsigned long lba_sects, chs_sects, head, tail;
90 * The ATA spec tells large drives to return
91 * C/H/S = 16383/16/63 independent of their size.
92 * Some drives can be jumpered to use 15 heads instead of 16.
93 * Some drives can be jumpered to use 4092 cyls instead of 16383.
95 if ((id->cyls == 16383
96 || (id->cyls == 4092 && id->cur_cyls == 16383)) &&
98 (id->heads == 15 || id->heads == 16) &&
99 (id->lba_capacity >= 16383*63*id->heads))
102 lba_sects = id->lba_capacity;
103 chs_sects = id->cyls * id->heads * id->sectors;
105 /* perform a rough sanity check on lba_sects: within 10% is OK */
106 if ((lba_sects - chs_sects) < chs_sects/10)
109 /* some drives have the word order reversed */
110 head = ((lba_sects >> 16) & 0xffff);
111 tail = (lba_sects & 0xffff);
112 lba_sects = (head | (tail << 16));
113 if ((lba_sects - chs_sects) < chs_sects/10) {
114 id->lba_capacity = lba_sects;
115 return 1; /* lba_capacity is (now) good */
118 return 0; /* lba_capacity value may be bad */
121 static int idedisk_start_tag(ide_drive_t *drive, struct request *rq)
126 spin_lock_irqsave(&ide_lock, flags);
128 if (ata_pending_commands(drive) < drive->queue_depth)
129 ret = blk_queue_start_tag(drive->queue, rq);
131 spin_unlock_irqrestore(&ide_lock, flags);
135 #ifndef CONFIG_IDE_TASKFILE_IO
138 * read_intr() is the handler for disk read/multread interrupts
140 static ide_startstop_t read_intr (ide_drive_t *drive)
142 ide_hwif_t *hwif = HWIF(drive);
143 u32 i = 0, nsect = 0, msect = drive->mult_count;
149 /* new way for dealing with premature shared PCI interrupts */
150 if (!OK_STAT(stat=hwif->INB(IDE_STATUS_REG),DATA_READY,BAD_R_STAT)) {
151 if (stat & (ERR_STAT|DRQ_STAT)) {
152 return DRIVER(drive)->error(drive, "read_intr", stat);
154 /* no data yet, so wait for another interrupt */
155 ide_set_handler(drive, &read_intr, WAIT_CMD, NULL);
160 rq = HWGROUP(drive)->rq;
162 if ((nsect = rq->current_nr_sectors) > msect)
167 to = ide_map_buffer(rq, &flags);
168 taskfile_input_data(drive, to, nsect * SECTOR_WORDS);
170 printk("%s: read: sectors(%ld-%ld), buffer=0x%08lx, remaining=%ld\n",
171 drive->name, rq->sector, rq->sector+nsect-1,
172 (unsigned long) rq->buffer+(nsect<<9), rq->nr_sectors-nsect);
174 ide_unmap_buffer(rq, to, &flags);
177 i = (rq->nr_sectors -= nsect);
178 if (((long)(rq->current_nr_sectors -= nsect)) <= 0)
179 ide_end_request(drive, 1, rq->hard_cur_sectors);
181 * Another BH Page walker and DATA INTEGRITY Questioned on ERROR.
182 * If passed back up on multimode read, BAD DATA could be ACKED
183 * to FILE SYSTEMS above ...
188 ide_set_handler(drive, &read_intr, WAIT_CMD, NULL);
195 * write_intr() is the handler for disk write interrupts
197 static ide_startstop_t write_intr (ide_drive_t *drive)
199 ide_hwgroup_t *hwgroup = HWGROUP(drive);
200 ide_hwif_t *hwif = HWIF(drive);
201 struct request *rq = hwgroup->rq;
205 if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG),
206 DRIVE_READY, drive->bad_wstat)) {
207 printk("%s: write_intr error1: nr_sectors=%ld, stat=0x%02x\n",
208 drive->name, rq->nr_sectors, stat);
211 printk("%s: write: sector %ld, buffer=0x%08lx, remaining=%ld\n",
212 drive->name, rq->sector, (unsigned long) rq->buffer,
215 if ((rq->nr_sectors == 1) ^ ((stat & DRQ_STAT) != 0)) {
218 i = --rq->nr_sectors;
219 --rq->current_nr_sectors;
220 if (((long)rq->current_nr_sectors) <= 0)
221 ide_end_request(drive, 1, rq->hard_cur_sectors);
224 char *to = ide_map_buffer(rq, &flags);
225 taskfile_output_data(drive, to, SECTOR_WORDS);
226 ide_unmap_buffer(rq, to, &flags);
227 ide_set_handler(drive, &write_intr, WAIT_CMD, NULL);
232 /* the original code did this here (?) */
235 return DRIVER(drive)->error(drive, "write_intr", stat);
239 * ide_multwrite() transfers a block of up to mcount sectors of data
240 * to a drive as part of a disk multiple-sector write operation.
242 * Note that we may be called from two contexts - __ide_do_rw_disk() context
243 * and IRQ context. The IRQ can happen any time after we've output the
244 * full "mcount" number of sectors, so we must make sure we update the
245 * state _before_ we output the final part of the data!
247 * The update and return to BH is a BLOCK Layer Fakey to get more data
248 * to satisfy the hardware atomic segment. If the hardware atomic segment
249 * is shorter or smaller than the BH segment then we should be OKAY.
250 * This is only valid if we can rewind the rq->current_nr_sectors counter.
252 static void ide_multwrite(ide_drive_t *drive, unsigned int mcount)
254 ide_hwgroup_t *hwgroup = HWGROUP(drive);
255 struct request *rq = &hwgroup->wrq;
259 int nsect = rq->current_nr_sectors;
265 buffer = ide_map_buffer(rq, &flags);
268 rq->nr_sectors -= nsect;
269 rq->current_nr_sectors -= nsect;
271 /* Do we move to the next bh after this? */
272 if (!rq->current_nr_sectors) {
273 struct bio *bio = rq->bio;
276 * only move to next bio, when we have processed
277 * all bvecs in this one.
279 if (++bio->bi_idx >= bio->bi_vcnt) {
280 bio->bi_idx = bio->bi_vcnt - rq->nr_cbio_segments;
284 /* end early early we ran out of requests */
289 rq->nr_cbio_segments = bio_segments(bio);
290 rq->current_nr_sectors = bio_cur_sectors(bio);
291 rq->hard_cur_sectors = rq->current_nr_sectors;
296 * Ok, we're all setup for the interrupt
297 * re-entering us on the last transfer.
299 taskfile_output_data(drive, buffer, nsect<<7);
300 ide_unmap_buffer(rq, buffer, &flags);
305 * multwrite_intr() is the handler for disk multwrite interrupts
307 static ide_startstop_t multwrite_intr (ide_drive_t *drive)
309 ide_hwgroup_t *hwgroup = HWGROUP(drive);
310 ide_hwif_t *hwif = HWIF(drive);
311 struct request *rq = &hwgroup->wrq;
312 struct bio *bio = rq->bio;
315 stat = hwif->INB(IDE_STATUS_REG);
316 if (OK_STAT(stat, DRIVE_READY, drive->bad_wstat)) {
317 if (stat & DRQ_STAT) {
319 * The drive wants data. Remember rq is the copy
322 if (rq->nr_sectors) {
323 ide_multwrite(drive, drive->mult_count);
324 ide_set_handler(drive, &multwrite_intr, WAIT_CMD, NULL);
329 * If the copy has all the blocks completed then
330 * we can end the original request.
332 if (!rq->nr_sectors) { /* all done? */
333 bio->bi_idx = bio->bi_vcnt - rq->nr_cbio_segments;
335 ide_end_request(drive, 1, rq->nr_sectors);
339 bio->bi_idx = bio->bi_vcnt - rq->nr_cbio_segments;
340 /* the original code did this here (?) */
343 bio->bi_idx = bio->bi_vcnt - rq->nr_cbio_segments;
344 return DRIVER(drive)->error(drive, "multwrite_intr", stat);
348 * __ide_do_rw_disk() issues READ and WRITE commands to a disk,
349 * using LBA if supported, or CHS otherwise, to address sectors.
350 * It also takes care of issuing special DRIVE_CMDs.
352 ide_startstop_t __ide_do_rw_disk (ide_drive_t *drive, struct request *rq, sector_t block)
354 ide_hwif_t *hwif = HWIF(drive);
355 u8 lba48 = (drive->addressing == 1) ? 1 : 0;
356 task_ioreg_t command = WIN_NOP;
357 ata_nsector_t nsectors;
359 nsectors.all = (u16) rq->nr_sectors;
361 if (drive->using_tcq && idedisk_start_tag(drive, rq)) {
362 if (!ata_pending_commands(drive))
369 hwif->OUTB(drive->ctl, IDE_CONTROL_REG);
371 if (drive->select.b.lba) {
372 if (drive->addressing == 1) {
373 task_ioreg_t tasklets[10];
375 if (blk_rq_tagged(rq)) {
376 tasklets[0] = nsectors.b.low;
377 tasklets[1] = nsectors.b.high;
378 tasklets[2] = rq->tag << 3;
383 tasklets[2] = nsectors.b.low;
384 tasklets[3] = nsectors.b.high;
387 tasklets[4] = (task_ioreg_t) block;
388 tasklets[5] = (task_ioreg_t) (block>>8);
389 tasklets[6] = (task_ioreg_t) (block>>16);
390 tasklets[7] = (task_ioreg_t) (block>>24);
391 if (sizeof(block) == 4) {
392 tasklets[8] = (task_ioreg_t) 0;
393 tasklets[9] = (task_ioreg_t) 0;
395 tasklets[8] = (task_ioreg_t)((u64)block >> 32);
396 tasklets[9] = (task_ioreg_t)((u64)block >> 40);
399 printk("%s: %sing: LBAsect=%lu, sectors=%ld, "
400 "buffer=0x%08lx, LBAsect=0x%012lx\n",
402 rq_data_dir(rq)==READ?"read":"writ",
405 (unsigned long) rq->buffer,
407 printk("%s: 0x%02x%02x 0x%02x%02x%02x%02x%02x%02x\n",
408 drive->name, tasklets[3], tasklets[2],
409 tasklets[9], tasklets[8], tasklets[7],
410 tasklets[6], tasklets[5], tasklets[4]);
412 hwif->OUTB(tasklets[1], IDE_FEATURE_REG);
413 hwif->OUTB(tasklets[3], IDE_NSECTOR_REG);
414 hwif->OUTB(tasklets[7], IDE_SECTOR_REG);
415 hwif->OUTB(tasklets[8], IDE_LCYL_REG);
416 hwif->OUTB(tasklets[9], IDE_HCYL_REG);
418 hwif->OUTB(tasklets[0], IDE_FEATURE_REG);
419 hwif->OUTB(tasklets[2], IDE_NSECTOR_REG);
420 hwif->OUTB(tasklets[4], IDE_SECTOR_REG);
421 hwif->OUTB(tasklets[5], IDE_LCYL_REG);
422 hwif->OUTB(tasklets[6], IDE_HCYL_REG);
423 hwif->OUTB(0x00|drive->select.all,IDE_SELECT_REG);
426 printk("%s: %sing: LBAsect=%llu, sectors=%ld, "
429 rq_data_dir(rq)==READ?"read":"writ",
430 (unsigned long long)block, rq->nr_sectors,
431 (unsigned long) rq->buffer);
433 if (blk_rq_tagged(rq)) {
434 hwif->OUTB(nsectors.b.low, IDE_FEATURE_REG);
435 hwif->OUTB(rq->tag << 3, IDE_NSECTOR_REG);
437 hwif->OUTB(0x00, IDE_FEATURE_REG);
438 hwif->OUTB(nsectors.b.low, IDE_NSECTOR_REG);
441 hwif->OUTB(block, IDE_SECTOR_REG);
442 hwif->OUTB(block>>=8, IDE_LCYL_REG);
443 hwif->OUTB(block>>=8, IDE_HCYL_REG);
444 hwif->OUTB(((block>>8)&0x0f)|drive->select.all,IDE_SELECT_REG);
447 unsigned int sect,head,cyl,track;
448 track = (int)block / drive->sect;
449 sect = (int)block % drive->sect + 1;
450 hwif->OUTB(sect, IDE_SECTOR_REG);
451 head = track % drive->head;
452 cyl = track / drive->head;
454 if (blk_rq_tagged(rq)) {
455 hwif->OUTB(nsectors.b.low, IDE_FEATURE_REG);
456 hwif->OUTB(rq->tag << 3, IDE_NSECTOR_REG);
458 hwif->OUTB(0x00, IDE_FEATURE_REG);
459 hwif->OUTB(nsectors.b.low, IDE_NSECTOR_REG);
462 hwif->OUTB(cyl, IDE_LCYL_REG);
463 hwif->OUTB(cyl>>8, IDE_HCYL_REG);
464 hwif->OUTB(head|drive->select.all,IDE_SELECT_REG);
466 printk("%s: %sing: CHS=%d/%d/%d, sectors=%ld, buffer=0x%08lx\n",
467 drive->name, rq_data_dir(rq)==READ?"read":"writ", cyl,
468 head, sect, rq->nr_sectors, (unsigned long) rq->buffer);
472 if (rq_data_dir(rq) == READ) {
473 #ifdef CONFIG_BLK_DEV_IDE_TCQ
474 if (blk_rq_tagged(rq))
475 return __ide_dma_queued_read(drive);
477 if (drive->using_dma && !hwif->ide_dma_read(drive))
480 command = ((drive->mult_count) ?
481 ((lba48) ? WIN_MULTREAD_EXT : WIN_MULTREAD) :
482 ((lba48) ? WIN_READ_EXT : WIN_READ));
483 ide_execute_command(drive, command, &read_intr, WAIT_CMD, NULL);
485 } else if (rq_data_dir(rq) == WRITE) {
486 ide_startstop_t startstop;
487 #ifdef CONFIG_BLK_DEV_IDE_TCQ
488 if (blk_rq_tagged(rq))
489 return __ide_dma_queued_write(drive);
491 if (drive->using_dma && !(HWIF(drive)->ide_dma_write(drive)))
494 command = ((drive->mult_count) ?
495 ((lba48) ? WIN_MULTWRITE_EXT : WIN_MULTWRITE) :
496 ((lba48) ? WIN_WRITE_EXT : WIN_WRITE));
497 hwif->OUTB(command, IDE_COMMAND_REG);
499 if (ide_wait_stat(&startstop, drive, DATA_READY,
500 drive->bad_wstat, WAIT_DRQ)) {
501 printk(KERN_ERR "%s: no DRQ after issuing %s\n",
503 drive->mult_count ? "MULTWRITE" : "WRITE");
508 if (drive->mult_count) {
509 ide_hwgroup_t *hwgroup = HWGROUP(drive);
511 hwgroup->wrq = *rq; /* scratchpad */
512 ide_set_handler(drive, &multwrite_intr, WAIT_CMD, NULL);
513 ide_multwrite(drive, drive->mult_count);
516 char *to = ide_map_buffer(rq, &flags);
517 ide_set_handler(drive, &write_intr, WAIT_CMD, NULL);
518 taskfile_output_data(drive, to, SECTOR_WORDS);
519 ide_unmap_buffer(rq, to, &flags);
523 blk_dump_rq_flags(rq, "__ide_do_rw_disk - bad command");
524 ide_end_request(drive, 0, 0);
527 EXPORT_SYMBOL_GPL(__ide_do_rw_disk);
529 #else /* CONFIG_IDE_TASKFILE_IO */
531 static ide_startstop_t chs_rw_disk(ide_drive_t *, struct request *, unsigned long);
532 static ide_startstop_t lba_28_rw_disk(ide_drive_t *, struct request *, unsigned long);
533 static ide_startstop_t lba_48_rw_disk(ide_drive_t *, struct request *, unsigned long long);
536 * __ide_do_rw_disk() issues READ and WRITE commands to a disk,
537 * using LBA if supported, or CHS otherwise, to address sectors.
538 * It also takes care of issuing special DRIVE_CMDs.
540 ide_startstop_t __ide_do_rw_disk (ide_drive_t *drive, struct request *rq, sector_t block)
542 BUG_ON(drive->blocked);
543 if (!blk_fs_request(rq)) {
544 blk_dump_rq_flags(rq, "__ide_do_rw_disk - bad command");
545 ide_end_request(drive, 0, 0);
550 * 268435455 == 137439 MB or 28bit limit
552 * need to add split taskfile operations based on 28bit threshold.
555 if (drive->using_tcq && idedisk_start_tag(drive, rq)) {
556 if (!ata_pending_commands(drive))
562 if (drive->addressing == 1) /* 48-bit LBA */
563 return lba_48_rw_disk(drive, rq, (unsigned long long) block);
564 if (drive->select.b.lba) /* 28-bit LBA */
565 return lba_28_rw_disk(drive, rq, (unsigned long) block);
567 /* 28-bit CHS : DIE DIE DIE piece of legacy crap!!! */
568 return chs_rw_disk(drive, rq, (unsigned long) block);
570 EXPORT_SYMBOL_GPL(__ide_do_rw_disk);
572 static task_ioreg_t get_command (ide_drive_t *drive, int cmd)
574 int lba48bit = (drive->addressing == 1) ? 1 : 0;
576 if ((cmd == READ) && drive->using_tcq)
577 return lba48bit ? WIN_READDMA_QUEUED_EXT : WIN_READDMA_QUEUED;
578 if ((cmd == READ) && (drive->using_dma))
579 return (lba48bit) ? WIN_READDMA_EXT : WIN_READDMA;
580 else if ((cmd == READ) && (drive->mult_count))
581 return (lba48bit) ? WIN_MULTREAD_EXT : WIN_MULTREAD;
582 else if (cmd == READ)
583 return (lba48bit) ? WIN_READ_EXT : WIN_READ;
584 else if ((cmd == WRITE) && drive->using_tcq)
585 return lba48bit ? WIN_WRITEDMA_QUEUED_EXT : WIN_WRITEDMA_QUEUED;
586 else if ((cmd == WRITE) && (drive->using_dma))
587 return (lba48bit) ? WIN_WRITEDMA_EXT : WIN_WRITEDMA;
588 else if ((cmd == WRITE) && (drive->mult_count))
589 return (lba48bit) ? WIN_MULTWRITE_EXT : WIN_MULTWRITE;
590 else if (cmd == WRITE)
591 return (lba48bit) ? WIN_WRITE_EXT : WIN_WRITE;
596 static ide_startstop_t chs_rw_disk (ide_drive_t *drive, struct request *rq, unsigned long block)
600 ata_nsector_t nsectors;
601 task_ioreg_t command = get_command(drive, rq_data_dir(rq));
602 unsigned int track = (block / drive->sect);
603 unsigned int sect = (block % drive->sect) + 1;
604 unsigned int head = (track % drive->head);
605 unsigned int cyl = (track / drive->head);
607 nsectors.all = (u16) rq->nr_sectors;
610 printk("%s: %sing: ", drive->name, (rq_data_dir(rq)==READ) ? "read" : "writ");
611 printk("CHS=%d/%d/%d, ", cyl, head, sect);
612 printk("sectors=%ld, ", rq->nr_sectors);
613 printk("buffer=0x%08lx\n", (unsigned long) rq->buffer);
616 memset(&args, 0, sizeof(ide_task_t));
618 sectors = (rq->nr_sectors == 256) ? 0x00 : rq->nr_sectors;
620 if (blk_rq_tagged(rq)) {
621 args.tfRegister[IDE_FEATURE_OFFSET] = sectors;
622 args.tfRegister[IDE_NSECTOR_OFFSET] = rq->tag << 3;
624 args.tfRegister[IDE_NSECTOR_OFFSET] = sectors;
626 args.tfRegister[IDE_SECTOR_OFFSET] = sect;
627 args.tfRegister[IDE_LCYL_OFFSET] = cyl;
628 args.tfRegister[IDE_HCYL_OFFSET] = (cyl>>8);
629 args.tfRegister[IDE_SELECT_OFFSET] = head;
630 args.tfRegister[IDE_SELECT_OFFSET] |= drive->select.all;
631 args.tfRegister[IDE_COMMAND_OFFSET] = command;
632 args.command_type = ide_cmd_type_parser(&args);
633 args.rq = (struct request *) rq;
634 rq->special = (ide_task_t *)&args;
635 return do_rw_taskfile(drive, &args);
638 static ide_startstop_t lba_28_rw_disk (ide_drive_t *drive, struct request *rq, unsigned long block)
642 ata_nsector_t nsectors;
643 task_ioreg_t command = get_command(drive, rq_data_dir(rq));
645 nsectors.all = (u16) rq->nr_sectors;
648 printk("%s: %sing: ", drive->name, (rq_data_dir(rq)==READ) ? "read" : "writ");
649 printk("LBAsect=%lld, ", block);
650 printk("sectors=%ld, ", rq->nr_sectors);
651 printk("buffer=0x%08lx\n", (unsigned long) rq->buffer);
654 memset(&args, 0, sizeof(ide_task_t));
656 sectors = (rq->nr_sectors == 256) ? 0x00 : rq->nr_sectors;
658 if (blk_rq_tagged(rq)) {
659 args.tfRegister[IDE_FEATURE_OFFSET] = sectors;
660 args.tfRegister[IDE_NSECTOR_OFFSET] = rq->tag << 3;
662 args.tfRegister[IDE_NSECTOR_OFFSET] = sectors;
664 args.tfRegister[IDE_SECTOR_OFFSET] = block;
665 args.tfRegister[IDE_LCYL_OFFSET] = (block>>=8);
666 args.tfRegister[IDE_HCYL_OFFSET] = (block>>=8);
667 args.tfRegister[IDE_SELECT_OFFSET] = ((block>>8)&0x0f);
668 args.tfRegister[IDE_SELECT_OFFSET] |= drive->select.all;
669 args.tfRegister[IDE_COMMAND_OFFSET] = command;
670 args.command_type = ide_cmd_type_parser(&args);
671 args.rq = (struct request *) rq;
672 rq->special = (ide_task_t *)&args;
673 return do_rw_taskfile(drive, &args);
677 * 268435455 == 137439 MB or 28bit limit
678 * 320173056 == 163929 MB or 48bit addressing
679 * 1073741822 == 549756 MB or 48bit addressing fake drive
682 static ide_startstop_t lba_48_rw_disk (ide_drive_t *drive, struct request *rq, unsigned long long block)
686 ata_nsector_t nsectors;
687 task_ioreg_t command = get_command(drive, rq_data_dir(rq));
689 nsectors.all = (u16) rq->nr_sectors;
692 printk("%s: %sing: ", drive->name, (rq_data_dir(rq)==READ) ? "read" : "writ");
693 printk("LBAsect=%lld, ", block);
694 printk("sectors=%ld, ", rq->nr_sectors);
695 printk("buffer=0x%08lx\n", (unsigned long) rq->buffer);
698 memset(&args, 0, sizeof(ide_task_t));
700 sectors = (rq->nr_sectors == 65536) ? 0 : rq->nr_sectors;
702 if (blk_rq_tagged(rq)) {
703 args.tfRegister[IDE_FEATURE_OFFSET] = sectors;
704 args.tfRegister[IDE_NSECTOR_OFFSET] = rq->tag << 3;
705 args.hobRegister[IDE_FEATURE_OFFSET_HOB] = sectors >> 8;
706 args.hobRegister[IDE_NSECTOR_OFFSET_HOB] = 0;
708 args.tfRegister[IDE_NSECTOR_OFFSET] = sectors;
709 args.hobRegister[IDE_NSECTOR_OFFSET_HOB] = sectors >> 8;
712 args.tfRegister[IDE_SECTOR_OFFSET] = block; /* low lba */
713 args.tfRegister[IDE_LCYL_OFFSET] = (block>>=8); /* mid lba */
714 args.tfRegister[IDE_HCYL_OFFSET] = (block>>=8); /* hi lba */
715 args.tfRegister[IDE_SELECT_OFFSET] = drive->select.all;
716 args.tfRegister[IDE_COMMAND_OFFSET] = command;
717 args.hobRegister[IDE_SECTOR_OFFSET_HOB] = (block>>=8); /* low lba */
718 args.hobRegister[IDE_LCYL_OFFSET_HOB] = (block>>=8); /* mid lba */
719 args.hobRegister[IDE_HCYL_OFFSET_HOB] = (block>>=8); /* hi lba */
720 args.hobRegister[IDE_SELECT_OFFSET_HOB] = drive->select.all;
721 args.hobRegister[IDE_CONTROL_OFFSET_HOB]= (drive->ctl|0x80);
722 args.command_type = ide_cmd_type_parser(&args);
723 args.rq = (struct request *) rq;
724 rq->special = (ide_task_t *)&args;
725 return do_rw_taskfile(drive, &args);
728 #endif /* CONFIG_IDE_TASKFILE_IO */
730 static ide_startstop_t ide_do_rw_disk (ide_drive_t *drive, struct request *rq, sector_t block)
732 ide_hwif_t *hwif = HWIF(drive);
735 return hwif->rw_disk(drive, rq, block);
737 return __ide_do_rw_disk(drive, rq, block);
740 static int do_idedisk_flushcache(ide_drive_t *drive);
742 static u8 idedisk_dump_status (ide_drive_t *drive, const char *msg, u8 stat)
744 ide_hwif_t *hwif = HWIF(drive);
748 local_irq_set(flags);
749 printk("%s: %s: status=0x%02x", drive->name, msg, stat);
750 #if FANCY_STATUS_DUMPS
752 if (stat & BUSY_STAT)
755 if (stat & READY_STAT) printk("DriveReady ");
756 if (stat & WRERR_STAT) printk("DeviceFault ");
757 if (stat & SEEK_STAT) printk("SeekComplete ");
758 if (stat & DRQ_STAT) printk("DataRequest ");
759 if (stat & ECC_STAT) printk("CorrectedError ");
760 if (stat & INDEX_STAT) printk("Index ");
761 if (stat & ERR_STAT) printk("Error ");
764 #endif /* FANCY_STATUS_DUMPS */
766 if ((stat & (BUSY_STAT|ERR_STAT)) == ERR_STAT) {
767 err = hwif->INB(IDE_ERROR_REG);
768 printk("%s: %s: error=0x%02x", drive->name, msg, err);
769 #if FANCY_STATUS_DUMPS
771 if (err & ABRT_ERR) printk("DriveStatusError ");
773 printk("Bad%s ", (err & ABRT_ERR) ? "CRC" : "Sector");
774 if (err & ECC_ERR) printk("UncorrectableError ");
775 if (err & ID_ERR) printk("SectorIdNotFound ");
776 if (err & TRK0_ERR) printk("TrackZeroNotFound ");
777 if (err & MARK_ERR) printk("AddrMarkNotFound ");
779 if ((err & (BBD_ERR | ABRT_ERR)) == BBD_ERR ||
780 (err & (ECC_ERR|ID_ERR|MARK_ERR))) {
781 if (drive->addressing == 1) {
783 u32 low = 0, high = 0;
784 low = ide_read_24(drive);
785 hwif->OUTB(drive->ctl|0x80, IDE_CONTROL_REG);
786 high = ide_read_24(drive);
787 sectors = ((__u64)high << 24) | low;
788 printk(", LBAsect=%llu, high=%d, low=%d",
789 (unsigned long long) sectors,
792 u8 cur = hwif->INB(IDE_SELECT_REG);
793 if (cur & 0x40) { /* using LBA? */
794 printk(", LBAsect=%ld", (unsigned long)
796 |(hwif->INB(IDE_HCYL_REG)<<16)
797 |(hwif->INB(IDE_LCYL_REG)<<8)
798 | hwif->INB(IDE_SECTOR_REG));
800 printk(", CHS=%d/%d/%d",
801 (hwif->INB(IDE_HCYL_REG)<<8) +
802 hwif->INB(IDE_LCYL_REG),
804 hwif->INB(IDE_SECTOR_REG));
807 if (HWGROUP(drive) && HWGROUP(drive)->rq)
808 printk(", sector=%llu",
809 (unsigned long long)HWGROUP(drive)->rq->sector);
812 #endif /* FANCY_STATUS_DUMPS */
814 local_irq_restore(flags);
818 ide_startstop_t idedisk_error (ide_drive_t *drive, const char *msg, u8 stat)
823 int i = (drive->mult_count ? drive->mult_count : 1) * SECTOR_WORDS;
825 err = idedisk_dump_status(drive, msg, stat);
827 if (drive == NULL || (rq = HWGROUP(drive)->rq) == NULL)
831 /* retry only "normal" I/O: */
832 if (rq->flags & (REQ_DRIVE_CMD | REQ_DRIVE_TASK | REQ_DRIVE_TASKFILE)) {
834 ide_end_drive_cmd(drive, stat, err);
837 #ifdef CONFIG_IDE_TASKFILE_IO
838 /* make rq completion pointers new submission pointers */
839 blk_rq_prep_restart(rq);
842 if (stat & BUSY_STAT || ((stat & WRERR_STAT) && !drive->nowerr)) {
843 /* other bits are useless when BUSY */
844 rq->errors |= ERROR_RESET;
845 } else if (stat & ERR_STAT) {
846 /* err has different meaning on cdrom and tape */
847 if (err == ABRT_ERR) {
848 if (drive->select.b.lba &&
849 /* some newer drives don't support WIN_SPECIFY */
850 hwif->INB(IDE_COMMAND_REG) == WIN_SPECIFY)
852 } else if ((err & BAD_CRC) == BAD_CRC) {
853 /* UDMA crc error, just retry the operation */
855 } else if (err & (BBD_ERR | ECC_ERR)) {
856 /* retries won't help these */
857 rq->errors = ERROR_MAX;
858 } else if (err & TRK0_ERR) {
859 /* help it find track zero */
860 rq->errors |= ERROR_RECAL;
863 if ((stat & DRQ_STAT) && rq_data_dir(rq) == READ) {
865 * try_to_flush_leftover_data() is invoked in response to
866 * a drive unexpectedly having its DRQ_STAT bit set. As
867 * an alternative to resetting the drive, this routine
868 * tries to clear the condition by read a sector's worth
869 * of data from the drive. Of course, this may not help
870 * if the drive is *waiting* for data from *us*.
874 unsigned int wcount = (i > 16) ? 16 : i;
876 taskfile_input_data(drive, buffer, wcount);
879 if (hwif->INB(IDE_STATUS_REG) & (BUSY_STAT|DRQ_STAT)) {
881 hwif->OUTB(WIN_IDLEIMMEDIATE,IDE_COMMAND_REG);
883 if (rq->errors >= ERROR_MAX || blk_noretry_request(rq))
884 DRIVER(drive)->end_request(drive, 0, 0);
886 if ((rq->errors & ERROR_RESET) == ERROR_RESET) {
888 return ide_do_reset(drive);
890 if ((rq->errors & ERROR_RECAL) == ERROR_RECAL)
891 drive->special.b.recalibrate = 1;
897 ide_startstop_t idedisk_abort(ide_drive_t *drive, const char *msg)
902 if (drive == NULL || (rq = HWGROUP(drive)->rq) == NULL)
907 if (rq->flags & (REQ_DRIVE_CMD | REQ_DRIVE_TASK | REQ_DRIVE_TASKFILE)) {
909 ide_end_drive_cmd(drive, BUSY_STAT, 0);
913 DRIVER(drive)->end_request(drive, 0, 0);
918 * Queries for true maximum capacity of the drive.
919 * Returns maximum LBA address (> 0) of the drive, 0 if failed.
921 static unsigned long idedisk_read_native_max_address(ide_drive_t *drive)
924 unsigned long addr = 0;
926 /* Create IDE/ATA command request structure */
927 memset(&args, 0, sizeof(ide_task_t));
928 args.tfRegister[IDE_SELECT_OFFSET] = 0x40;
929 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_READ_NATIVE_MAX;
930 args.command_type = ide_cmd_type_parser(&args);
931 /* submit command request */
932 ide_raw_taskfile(drive, &args, NULL);
934 /* if OK, compute maximum address value */
935 if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
936 addr = ((args.tfRegister[IDE_SELECT_OFFSET] & 0x0f) << 24)
937 | ((args.tfRegister[ IDE_HCYL_OFFSET] ) << 16)
938 | ((args.tfRegister[ IDE_LCYL_OFFSET] ) << 8)
939 | ((args.tfRegister[IDE_SECTOR_OFFSET] ));
940 addr++; /* since the return value is (maxlba - 1), we add 1 */
945 static unsigned long long idedisk_read_native_max_address_ext(ide_drive_t *drive)
948 unsigned long long addr = 0;
950 /* Create IDE/ATA command request structure */
951 memset(&args, 0, sizeof(ide_task_t));
953 args.tfRegister[IDE_SELECT_OFFSET] = 0x40;
954 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_READ_NATIVE_MAX_EXT;
955 args.command_type = ide_cmd_type_parser(&args);
956 /* submit command request */
957 ide_raw_taskfile(drive, &args, NULL);
959 /* if OK, compute maximum address value */
960 if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
961 u32 high = ((args.hobRegister[IDE_HCYL_OFFSET_HOB])<<16) |
962 ((args.hobRegister[IDE_LCYL_OFFSET_HOB])<<8) |
963 (args.hobRegister[IDE_SECTOR_OFFSET_HOB]);
964 u32 low = ((args.tfRegister[IDE_HCYL_OFFSET])<<16) |
965 ((args.tfRegister[IDE_LCYL_OFFSET])<<8) |
966 (args.tfRegister[IDE_SECTOR_OFFSET]);
967 addr = ((__u64)high << 24) | low;
968 addr++; /* since the return value is (maxlba - 1), we add 1 */
973 #ifdef CONFIG_IDEDISK_STROKE
975 * Sets maximum virtual LBA address of the drive.
976 * Returns new maximum virtual LBA address (> 0) or 0 on failure.
978 static unsigned long idedisk_set_max_address(ide_drive_t *drive, unsigned long addr_req)
981 unsigned long addr_set = 0;
984 /* Create IDE/ATA command request structure */
985 memset(&args, 0, sizeof(ide_task_t));
986 args.tfRegister[IDE_SECTOR_OFFSET] = ((addr_req >> 0) & 0xff);
987 args.tfRegister[IDE_LCYL_OFFSET] = ((addr_req >> 8) & 0xff);
988 args.tfRegister[IDE_HCYL_OFFSET] = ((addr_req >> 16) & 0xff);
989 args.tfRegister[IDE_SELECT_OFFSET] = ((addr_req >> 24) & 0x0f) | 0x40;
990 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SET_MAX;
991 args.command_type = ide_cmd_type_parser(&args);
992 /* submit command request */
993 ide_raw_taskfile(drive, &args, NULL);
994 /* if OK, read new maximum address value */
995 if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
996 addr_set = ((args.tfRegister[IDE_SELECT_OFFSET] & 0x0f) << 24)
997 | ((args.tfRegister[ IDE_HCYL_OFFSET] ) << 16)
998 | ((args.tfRegister[ IDE_LCYL_OFFSET] ) << 8)
999 | ((args.tfRegister[IDE_SECTOR_OFFSET] ));
1005 static unsigned long long idedisk_set_max_address_ext(ide_drive_t *drive, unsigned long long addr_req)
1008 unsigned long long addr_set = 0;
1011 /* Create IDE/ATA command request structure */
1012 memset(&args, 0, sizeof(ide_task_t));
1013 args.tfRegister[IDE_SECTOR_OFFSET] = ((addr_req >> 0) & 0xff);
1014 args.tfRegister[IDE_LCYL_OFFSET] = ((addr_req >>= 8) & 0xff);
1015 args.tfRegister[IDE_HCYL_OFFSET] = ((addr_req >>= 8) & 0xff);
1016 args.tfRegister[IDE_SELECT_OFFSET] = 0x40;
1017 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SET_MAX_EXT;
1018 args.hobRegister[IDE_SECTOR_OFFSET_HOB] = ((addr_req >>= 8) & 0xff);
1019 args.hobRegister[IDE_LCYL_OFFSET_HOB] = ((addr_req >>= 8) & 0xff);
1020 args.hobRegister[IDE_HCYL_OFFSET_HOB] = ((addr_req >>= 8) & 0xff);
1021 args.hobRegister[IDE_SELECT_OFFSET_HOB] = 0x40;
1022 args.hobRegister[IDE_CONTROL_OFFSET_HOB]= (drive->ctl|0x80);
1023 args.command_type = ide_cmd_type_parser(&args);
1024 /* submit command request */
1025 ide_raw_taskfile(drive, &args, NULL);
1026 /* if OK, compute maximum address value */
1027 if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
1028 u32 high = ((args.hobRegister[IDE_HCYL_OFFSET_HOB])<<16) |
1029 ((args.hobRegister[IDE_LCYL_OFFSET_HOB])<<8) |
1030 (args.hobRegister[IDE_SECTOR_OFFSET_HOB]);
1031 u32 low = ((args.tfRegister[IDE_HCYL_OFFSET])<<16) |
1032 ((args.tfRegister[IDE_LCYL_OFFSET])<<8) |
1033 (args.tfRegister[IDE_SECTOR_OFFSET]);
1034 addr_set = ((__u64)high << 24) | low;
1040 #endif /* CONFIG_IDEDISK_STROKE */
1042 static unsigned long long sectors_to_MB(unsigned long long n)
1044 n <<= 9; /* make it bytes */
1045 do_div(n, 1000000); /* make it MB */
1050 * Bits 10 of command_set_1 and cfs_enable_1 must be equal,
1051 * so on non-buggy drives we need test only one.
1052 * However, we should also check whether these fields are valid.
1054 static inline int idedisk_supports_hpa(const struct hd_driveid *id)
1056 return (id->command_set_1 & 0x0400) && (id->cfs_enable_1 & 0x0400);
1062 static inline int idedisk_supports_lba48(const struct hd_driveid *id)
1064 return (id->command_set_2 & 0x0400) && (id->cfs_enable_2 & 0x0400);
1067 static inline void idedisk_check_hpa(ide_drive_t *drive)
1069 unsigned long long capacity, set_max;
1070 int lba48 = idedisk_supports_lba48(drive->id);
1072 capacity = drive->capacity64;
1074 set_max = idedisk_read_native_max_address_ext(drive);
1076 set_max = idedisk_read_native_max_address(drive);
1078 if (set_max <= capacity)
1081 printk(KERN_INFO "%s: Host Protected Area detected.\n"
1082 "\tcurrent capacity is %llu sectors (%llu MB)\n"
1083 "\tnative capacity is %llu sectors (%llu MB)\n",
1085 capacity, sectors_to_MB(capacity),
1086 set_max, sectors_to_MB(set_max));
1087 #ifdef CONFIG_IDEDISK_STROKE
1089 set_max = idedisk_set_max_address_ext(drive, set_max);
1091 set_max = idedisk_set_max_address(drive, set_max);
1093 drive->capacity64 = set_max;
1094 printk(KERN_INFO "%s: Host Protected Area disabled.\n",
1101 * Compute drive->capacity, the full capacity of the drive
1102 * Called with drive->id != NULL.
1104 * To compute capacity, this uses either of
1106 * 1. CHS value set by user (whatever user sets will be trusted)
1107 * 2. LBA value from target drive (require new ATA feature)
1108 * 3. LBA value from system BIOS (new one is OK, old one may break)
1109 * 4. CHS value from system BIOS (traditional style)
1111 * in above order (i.e., if value of higher priority is available,
1112 * reset will be ignored).
1114 static void init_idedisk_capacity (ide_drive_t *drive)
1116 struct hd_driveid *id = drive->id;
1118 * If this drive supports the Host Protected Area feature set,
1119 * then we may need to change our opinion about the drive's capacity.
1121 int hpa = idedisk_supports_hpa(id);
1123 if (idedisk_supports_lba48(id)) {
1124 /* drive speaks 48-bit LBA */
1125 drive->select.b.lba = 1;
1126 drive->capacity64 = id->lba_capacity_2;
1128 idedisk_check_hpa(drive);
1129 } else if ((id->capability & 2) && lba_capacity_is_ok(id)) {
1130 /* drive speaks 28-bit LBA */
1131 drive->select.b.lba = 1;
1132 drive->capacity64 = id->lba_capacity;
1134 idedisk_check_hpa(drive);
1136 /* drive speaks boring old 28-bit CHS */
1137 drive->capacity64 = drive->cyl * drive->head * drive->sect;
1141 static sector_t idedisk_capacity (ide_drive_t *drive)
1143 return drive->capacity64 - drive->sect0;
1146 static ide_startstop_t idedisk_special (ide_drive_t *drive)
1148 special_t *s = &drive->special;
1150 if (s->b.set_geometry) {
1151 s->b.set_geometry = 0;
1152 if (!IS_PDC4030_DRIVE) {
1154 memset(&args, 0, sizeof(ide_task_t));
1155 args.tfRegister[IDE_NSECTOR_OFFSET] = drive->sect;
1156 args.tfRegister[IDE_SECTOR_OFFSET] = drive->sect;
1157 args.tfRegister[IDE_LCYL_OFFSET] = drive->cyl;
1158 args.tfRegister[IDE_HCYL_OFFSET] = drive->cyl>>8;
1159 args.tfRegister[IDE_SELECT_OFFSET] = ((drive->head-1)|drive->select.all)&0xBF;
1160 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SPECIFY;
1161 args.command_type = ide_cmd_type_parser(&args);
1162 do_rw_taskfile(drive, &args);
1164 } else if (s->b.recalibrate) {
1165 s->b.recalibrate = 0;
1166 if (!IS_PDC4030_DRIVE) {
1168 memset(&args, 0, sizeof(ide_task_t));
1169 args.tfRegister[IDE_NSECTOR_OFFSET] = drive->sect;
1170 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_RESTORE;
1171 args.command_type = ide_cmd_type_parser(&args);
1172 do_rw_taskfile(drive, &args);
1174 } else if (s->b.set_multmode) {
1175 s->b.set_multmode = 0;
1176 if (drive->mult_req > drive->id->max_multsect)
1177 drive->mult_req = drive->id->max_multsect;
1178 if (!IS_PDC4030_DRIVE) {
1180 memset(&args, 0, sizeof(ide_task_t));
1181 args.tfRegister[IDE_NSECTOR_OFFSET] = drive->mult_req;
1182 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SETMULT;
1183 args.command_type = ide_cmd_type_parser(&args);
1184 do_rw_taskfile(drive, &args);
1186 } else if (s->all) {
1187 int special = s->all;
1189 printk(KERN_ERR "%s: bad special flag: 0x%02x\n", drive->name, special);
1192 return IS_PDC4030_DRIVE ? ide_stopped : ide_started;
1195 static void idedisk_pre_reset (ide_drive_t *drive)
1197 int legacy = (drive->id->cfs_enable_2 & 0x0400) ? 0 : 1;
1199 drive->special.all = 0;
1200 drive->special.b.set_geometry = legacy;
1201 drive->special.b.recalibrate = legacy;
1202 if (OK_TO_RESET_CONTROLLER)
1203 drive->mult_count = 0;
1204 if (!drive->keep_settings && !drive->using_dma)
1205 drive->mult_req = 0;
1206 if (drive->mult_req != drive->mult_count)
1207 drive->special.b.set_multmode = 1;
1210 #ifdef CONFIG_PROC_FS
1212 static int smart_enable(ide_drive_t *drive)
1216 memset(&args, 0, sizeof(ide_task_t));
1217 args.tfRegister[IDE_FEATURE_OFFSET] = SMART_ENABLE;
1218 args.tfRegister[IDE_LCYL_OFFSET] = SMART_LCYL_PASS;
1219 args.tfRegister[IDE_HCYL_OFFSET] = SMART_HCYL_PASS;
1220 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SMART;
1221 args.command_type = ide_cmd_type_parser(&args);
1222 return ide_raw_taskfile(drive, &args, NULL);
1225 static int get_smart_values(ide_drive_t *drive, u8 *buf)
1229 memset(&args, 0, sizeof(ide_task_t));
1230 args.tfRegister[IDE_FEATURE_OFFSET] = SMART_READ_VALUES;
1231 args.tfRegister[IDE_NSECTOR_OFFSET] = 0x01;
1232 args.tfRegister[IDE_LCYL_OFFSET] = SMART_LCYL_PASS;
1233 args.tfRegister[IDE_HCYL_OFFSET] = SMART_HCYL_PASS;
1234 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SMART;
1235 args.command_type = ide_cmd_type_parser(&args);
1236 (void) smart_enable(drive);
1237 return ide_raw_taskfile(drive, &args, buf);
1240 static int get_smart_thresholds(ide_drive_t *drive, u8 *buf)
1243 memset(&args, 0, sizeof(ide_task_t));
1244 args.tfRegister[IDE_FEATURE_OFFSET] = SMART_READ_THRESHOLDS;
1245 args.tfRegister[IDE_NSECTOR_OFFSET] = 0x01;
1246 args.tfRegister[IDE_LCYL_OFFSET] = SMART_LCYL_PASS;
1247 args.tfRegister[IDE_HCYL_OFFSET] = SMART_HCYL_PASS;
1248 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SMART;
1249 args.command_type = ide_cmd_type_parser(&args);
1250 (void) smart_enable(drive);
1251 return ide_raw_taskfile(drive, &args, buf);
1254 static int proc_idedisk_read_cache
1255 (char *page, char **start, off_t off, int count, int *eof, void *data)
1257 ide_drive_t *drive = (ide_drive_t *) data;
1262 len = sprintf(out,"%i\n", drive->id->buf_size / 2);
1264 len = sprintf(out,"(none)\n");
1265 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
1268 static int proc_idedisk_read_smart_thresholds
1269 (char *page, char **start, off_t off, int count, int *eof, void *data)
1271 ide_drive_t *drive = (ide_drive_t *)data;
1274 if (!get_smart_thresholds(drive, page)) {
1275 unsigned short *val = (unsigned short *) page;
1276 char *out = ((char *)val) + (SECTOR_WORDS * 4);
1279 out += sprintf(out, "%04x%c", le16_to_cpu(*val), (++i & 7) ? ' ' : '\n');
1281 } while (i < (SECTOR_WORDS * 2));
1284 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
1287 static int proc_idedisk_read_smart_values
1288 (char *page, char **start, off_t off, int count, int *eof, void *data)
1290 ide_drive_t *drive = (ide_drive_t *)data;
1293 if (!get_smart_values(drive, page)) {
1294 unsigned short *val = (unsigned short *) page;
1295 char *out = ((char *)val) + (SECTOR_WORDS * 4);
1298 out += sprintf(out, "%04x%c", le16_to_cpu(*val), (++i & 7) ? ' ' : '\n');
1300 } while (i < (SECTOR_WORDS * 2));
1303 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
1306 static ide_proc_entry_t idedisk_proc[] = {
1307 { "cache", S_IFREG|S_IRUGO, proc_idedisk_read_cache, NULL },
1308 { "geometry", S_IFREG|S_IRUGO, proc_ide_read_geometry, NULL },
1309 { "smart_values", S_IFREG|S_IRUSR, proc_idedisk_read_smart_values, NULL },
1310 { "smart_thresholds", S_IFREG|S_IRUSR, proc_idedisk_read_smart_thresholds, NULL },
1311 { NULL, 0, NULL, NULL }
1316 #define idedisk_proc NULL
1318 #endif /* CONFIG_PROC_FS */
1321 * This is tightly woven into the driver->do_special can not touch.
1322 * DON'T do it again until a total personality rewrite is committed.
1324 static int set_multcount(ide_drive_t *drive, int arg)
1328 if (drive->special.b.set_multmode)
1330 ide_init_drive_cmd (&rq);
1331 rq.flags = REQ_DRIVE_CMD;
1332 drive->mult_req = arg;
1333 drive->special.b.set_multmode = 1;
1334 (void) ide_do_drive_cmd (drive, &rq, ide_wait);
1335 return (drive->mult_count == arg) ? 0 : -EIO;
1338 static int set_nowerr(ide_drive_t *drive, int arg)
1340 if (ide_spin_wait_hwgroup(drive))
1342 drive->nowerr = arg;
1343 drive->bad_wstat = arg ? BAD_R_STAT : BAD_W_STAT;
1344 spin_unlock_irq(&ide_lock);
1348 static int write_cache (ide_drive_t *drive, int arg)
1352 if (!(drive->id->cfs_enable_2 & 0x3000))
1355 memset(&args, 0, sizeof(ide_task_t));
1356 args.tfRegister[IDE_FEATURE_OFFSET] = (arg) ?
1357 SETFEATURES_EN_WCACHE : SETFEATURES_DIS_WCACHE;
1358 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SETFEATURES;
1359 args.command_type = ide_cmd_type_parser(&args);
1360 (void) ide_raw_taskfile(drive, &args, NULL);
1362 drive->wcache = arg;
1366 static int do_idedisk_flushcache (ide_drive_t *drive)
1370 memset(&args, 0, sizeof(ide_task_t));
1371 if (drive->id->cfs_enable_2 & 0x2400)
1372 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE_EXT;
1374 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE;
1375 args.command_type = ide_cmd_type_parser(&args);
1376 return ide_raw_taskfile(drive, &args, NULL);
1379 static int set_acoustic (ide_drive_t *drive, int arg)
1383 memset(&args, 0, sizeof(ide_task_t));
1384 args.tfRegister[IDE_FEATURE_OFFSET] = (arg) ? SETFEATURES_EN_AAM :
1385 SETFEATURES_DIS_AAM;
1386 args.tfRegister[IDE_NSECTOR_OFFSET] = arg;
1387 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SETFEATURES;
1388 args.command_type = ide_cmd_type_parser(&args);
1389 ide_raw_taskfile(drive, &args, NULL);
1390 drive->acoustic = arg;
1394 #ifdef CONFIG_BLK_DEV_IDE_TCQ
1395 static int set_using_tcq(ide_drive_t *drive, int arg)
1401 if (arg == drive->queue_depth && drive->using_tcq)
1405 * set depth, but check also id for max supported depth
1407 drive->queue_depth = arg ? arg : 1;
1409 if (drive->queue_depth > drive->id->queue_depth + 1)
1410 drive->queue_depth = drive->id->queue_depth + 1;
1414 ret = __ide_dma_queued_on(drive);
1416 ret = __ide_dma_queued_off(drive);
1418 return ret ? -EIO : 0;
1423 * drive->addressing:
1426 * 2: 48-bit capable doing 28-bit
1428 static int set_lba_addressing(ide_drive_t *drive, int arg)
1430 drive->addressing = 0;
1432 if (HWIF(drive)->no_lba48)
1435 if (!idedisk_supports_lba48(drive->id))
1437 drive->addressing = arg;
1441 static void idedisk_add_settings(ide_drive_t *drive)
1443 struct hd_driveid *id = drive->id;
1445 ide_add_setting(drive, "bios_cyl", SETTING_RW, -1, -1, TYPE_INT, 0, 65535, 1, 1, &drive->bios_cyl, NULL);
1446 ide_add_setting(drive, "bios_head", SETTING_RW, -1, -1, TYPE_BYTE, 0, 255, 1, 1, &drive->bios_head, NULL);
1447 ide_add_setting(drive, "bios_sect", SETTING_RW, -1, -1, TYPE_BYTE, 0, 63, 1, 1, &drive->bios_sect, NULL);
1448 ide_add_setting(drive, "address", SETTING_RW, HDIO_GET_ADDRESS, HDIO_SET_ADDRESS, TYPE_INTA, 0, 2, 1, 1, &drive->addressing, set_lba_addressing);
1449 ide_add_setting(drive, "bswap", SETTING_READ, -1, -1, TYPE_BYTE, 0, 1, 1, 1, &drive->bswap, NULL);
1450 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);
1451 ide_add_setting(drive, "nowerr", SETTING_RW, HDIO_GET_NOWERR, HDIO_SET_NOWERR, TYPE_BYTE, 0, 1, 1, 1, &drive->nowerr, set_nowerr);
1452 ide_add_setting(drive, "lun", SETTING_RW, -1, -1, TYPE_INT, 0, 7, 1, 1, &drive->lun, NULL);
1453 ide_add_setting(drive, "wcache", SETTING_RW, HDIO_GET_WCACHE, HDIO_SET_WCACHE, TYPE_BYTE, 0, 1, 1, 1, &drive->wcache, write_cache);
1454 ide_add_setting(drive, "acoustic", SETTING_RW, HDIO_GET_ACOUSTIC, HDIO_SET_ACOUSTIC, TYPE_BYTE, 0, 254, 1, 1, &drive->acoustic, set_acoustic);
1455 ide_add_setting(drive, "failures", SETTING_RW, -1, -1, TYPE_INT, 0, 65535, 1, 1, &drive->failures, NULL);
1456 ide_add_setting(drive, "max_failures", SETTING_RW, -1, -1, TYPE_INT, 0, 65535, 1, 1, &drive->max_failures, NULL);
1457 #ifdef CONFIG_BLK_DEV_IDE_TCQ
1458 ide_add_setting(drive, "using_tcq", SETTING_RW, HDIO_GET_QDMA, HDIO_SET_QDMA, TYPE_BYTE, 0, IDE_MAX_TAG, 1, 1, &drive->using_tcq, set_using_tcq);
1463 * Power Management state machine. This one is rather trivial for now,
1464 * we should probably add more, like switching back to PIO on suspend
1465 * to help some BIOSes, re-do the door locking on resume, etc...
1469 idedisk_pm_flush_cache = ide_pm_state_start_suspend,
1472 idedisk_pm_restore_dma = ide_pm_state_start_resume,
1475 static void idedisk_complete_power_step (ide_drive_t *drive, struct request *rq, u8 stat, u8 error)
1477 switch (rq->pm->pm_step) {
1478 case idedisk_pm_flush_cache: /* Suspend step 1 (flush cache) complete */
1479 if (rq->pm->pm_state == 4)
1480 rq->pm->pm_step = ide_pm_state_completed;
1482 rq->pm->pm_step = idedisk_pm_standby;
1484 case idedisk_pm_standby: /* Suspend step 2 (standby) complete */
1485 rq->pm->pm_step = ide_pm_state_completed;
1490 static ide_startstop_t idedisk_start_power_step (ide_drive_t *drive, struct request *rq)
1492 ide_task_t *args = rq->special;
1494 memset(args, 0, sizeof(*args));
1496 switch (rq->pm->pm_step) {
1497 case idedisk_pm_flush_cache: /* Suspend step 1 (flush cache) */
1498 /* Not supported? Switch to next step now. */
1499 if (!drive->wcache) {
1500 idedisk_complete_power_step(drive, rq, 0, 0);
1503 if (drive->id->cfs_enable_2 & 0x2400)
1504 args->tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE_EXT;
1506 args->tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE;
1507 args->command_type = ide_cmd_type_parser(args);
1508 return do_rw_taskfile(drive, args);
1509 case idedisk_pm_standby: /* Suspend step 2 (standby) */
1510 args->tfRegister[IDE_COMMAND_OFFSET] = WIN_STANDBYNOW1;
1511 args->command_type = ide_cmd_type_parser(args);
1512 return do_rw_taskfile(drive, args);
1514 case idedisk_pm_restore_dma: /* Resume step 1 (restore DMA) */
1516 * Right now, all we do is call hwif->ide_dma_check(drive),
1517 * we could be smarter and check for current xfer_speed
1518 * in struct drive etc...
1519 * Also, this step could be implemented as a generic helper
1520 * as most subdrivers will use it
1522 if ((drive->id->capability & 1) == 0)
1524 if (HWIF(drive)->ide_dma_check == NULL)
1526 HWIF(drive)->ide_dma_check(drive);
1529 rq->pm->pm_step = ide_pm_state_completed;
1533 static void idedisk_setup (ide_drive_t *drive)
1535 struct hd_driveid *id = drive->id;
1536 unsigned long long capacity;
1538 idedisk_add_settings(drive);
1540 if (drive->id_read == 0)
1544 * CompactFlash cards and their brethern look just like hard drives
1545 * to us, but they are removable and don't have a doorlock mechanism.
1547 if (drive->removable && !(drive->is_flash)) {
1549 * Removable disks (eg. SYQUEST); ignore 'WD' drives
1551 if (id->model[0] != 'W' || id->model[1] != 'D') {
1552 drive->doorlocking = 1;
1556 (void)set_lba_addressing(drive, 1);
1558 if (drive->addressing == 1) {
1559 ide_hwif_t *hwif = HWIF(drive);
1562 if (max_s > hwif->rqsize)
1563 max_s = hwif->rqsize;
1565 blk_queue_max_sectors(drive->queue, max_s);
1568 printk("%s: max request size: %dKiB\n", drive->name, drive->queue->max_sectors / 2);
1570 /* Extract geometry if we did not already have one for the drive */
1571 if (!drive->cyl || !drive->head || !drive->sect) {
1572 drive->cyl = drive->bios_cyl = id->cyls;
1573 drive->head = drive->bios_head = id->heads;
1574 drive->sect = drive->bios_sect = id->sectors;
1577 /* Handle logical geometry translation by the drive */
1578 if ((id->field_valid & 1) && id->cur_cyls &&
1579 id->cur_heads && (id->cur_heads <= 16) && id->cur_sectors) {
1580 drive->cyl = id->cur_cyls;
1581 drive->head = id->cur_heads;
1582 drive->sect = id->cur_sectors;
1585 /* Use physical geometry if what we have still makes no sense */
1586 if (drive->head > 16 && id->heads && id->heads <= 16) {
1587 drive->cyl = id->cyls;
1588 drive->head = id->heads;
1589 drive->sect = id->sectors;
1592 /* calculate drive capacity, and select LBA if possible */
1593 init_idedisk_capacity (drive);
1595 /* limit drive capacity to 137GB if LBA48 cannot be used */
1596 if (drive->addressing == 0 && drive->capacity64 > 1ULL << 28) {
1597 printk("%s: cannot use LBA48 - full capacity "
1598 "%llu sectors (%llu MB)\n",
1599 drive->name, (unsigned long long)drive->capacity64,
1600 sectors_to_MB(drive->capacity64));
1601 drive->capacity64 = 1ULL << 28;
1605 * if possible, give fdisk access to more of the drive,
1606 * by correcting bios_cyls:
1608 capacity = idedisk_capacity (drive);
1609 if (!drive->forced_geom) {
1611 if (idedisk_supports_lba48(drive->id)) {
1613 drive->bios_sect = 63;
1614 drive->bios_head = 255;
1617 if (drive->bios_sect && drive->bios_head) {
1618 unsigned int cap0 = capacity; /* truncate to 32 bits */
1619 unsigned int cylsz, cyl;
1621 if (cap0 != capacity)
1622 drive->bios_cyl = 65535;
1624 cylsz = drive->bios_sect * drive->bios_head;
1628 if (cyl > drive->bios_cyl)
1629 drive->bios_cyl = cyl;
1633 printk(KERN_INFO "%s: %llu sectors (%llu MB)",
1634 drive->name, capacity, sectors_to_MB(capacity));
1636 /* Only print cache size when it was specified */
1638 printk (" w/%dKiB Cache", id->buf_size/2);
1640 printk(", CHS=%d/%d/%d",
1641 drive->bios_cyl, drive->bios_head, drive->bios_sect);
1642 if (drive->using_dma)
1643 (void) HWIF(drive)->ide_dma_verbose(drive);
1646 drive->mult_count = 0;
1647 if (id->max_multsect) {
1648 #ifdef CONFIG_IDEDISK_MULTI_MODE
1649 id->multsect = ((id->max_multsect/2) > 1) ? id->max_multsect : 0;
1650 id->multsect_valid = id->multsect ? 1 : 0;
1651 drive->mult_req = id->multsect_valid ? id->max_multsect : INITIAL_MULT_COUNT;
1652 drive->special.b.set_multmode = drive->mult_req ? 1 : 0;
1653 #else /* original, pre IDE-NFG, per request of AC */
1654 drive->mult_req = INITIAL_MULT_COUNT;
1655 if (drive->mult_req > id->max_multsect)
1656 drive->mult_req = id->max_multsect;
1657 if (drive->mult_req || ((id->multsect_valid & 1) && id->multsect))
1658 drive->special.b.set_multmode = 1;
1659 #endif /* CONFIG_IDEDISK_MULTI_MODE */
1661 drive->no_io_32bit = id->dword_io ? 1 : 0;
1662 if (drive->id->cfs_enable_2 & 0x3000)
1663 write_cache(drive, (id->cfs_enable_2 & 0x3000));
1665 #ifdef CONFIG_BLK_DEV_IDE_TCQ_DEFAULT
1666 if (drive->using_dma)
1667 __ide_dma_queued_on(drive);
1671 static int idedisk_cleanup (ide_drive_t *drive)
1673 static int ide_cacheflush_p(ide_drive_t *drive);
1674 struct gendisk *g = drive->disk;
1675 ide_cacheflush_p(drive);
1676 if (ide_unregister_subdriver(drive))
1683 static int idedisk_attach(ide_drive_t *drive);
1686 * IDE subdriver functions, registered with ide.c
1688 static ide_driver_t idedisk_driver = {
1689 .owner = THIS_MODULE,
1691 .version = IDEDISK_VERSION,
1694 .supports_dsc_overlap = 0,
1695 .cleanup = idedisk_cleanup,
1696 .do_request = ide_do_rw_disk,
1697 .sense = idedisk_dump_status,
1698 .error = idedisk_error,
1699 .abort = idedisk_abort,
1700 .pre_reset = idedisk_pre_reset,
1701 .capacity = idedisk_capacity,
1702 .special = idedisk_special,
1703 .proc = idedisk_proc,
1704 .attach = idedisk_attach,
1705 .drives = LIST_HEAD_INIT(idedisk_driver.drives),
1706 .start_power_step = idedisk_start_power_step,
1707 .complete_power_step = idedisk_complete_power_step,
1710 static int idedisk_open(struct inode *inode, struct file *filp)
1712 ide_drive_t *drive = inode->i_bdev->bd_disk->private_data;
1714 if (drive->removable && drive->usage == 1) {
1717 memset(&args, 0, sizeof(ide_task_t));
1718 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_DOORLOCK;
1719 args.command_type = ide_cmd_type_parser(&args);
1720 check_disk_change(inode->i_bdev);
1722 * Ignore the return code from door_lock,
1723 * since the open() has already succeeded,
1724 * and the door_lock is irrelevant at this point.
1726 if (drive->doorlocking && ide_raw_taskfile(drive, &args, NULL))
1727 drive->doorlocking = 0;
1729 /* Cache enabled ? */
1730 if (drive->id->csfo & 1)
1732 /* Cache command set available ? */
1733 if (drive->id->cfs_enable_1 & (1<<5))
1735 /* ATA6 cache extended commands */
1736 cf = drive->id->command_set_2 >> 24;
1737 if((cf & 0xC0) == 0x40 && (cf & 0x30) != 0)
1743 static int ide_cacheflush_p(ide_drive_t *drive)
1747 if (do_idedisk_flushcache(drive))
1749 printk (KERN_INFO "%s: Write Cache FAILED Flushing!\n",
1758 static int idedisk_release(struct inode *inode, struct file *filp)
1760 ide_drive_t *drive = inode->i_bdev->bd_disk->private_data;
1761 if (drive->removable && drive->usage == 1) {
1763 memset(&args, 0, sizeof(ide_task_t));
1764 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_DOORUNLOCK;
1765 args.command_type = ide_cmd_type_parser(&args);
1766 if (drive->doorlocking && ide_raw_taskfile(drive, &args, NULL))
1767 drive->doorlocking = 0;
1769 ide_cacheflush_p(drive);
1774 static int idedisk_ioctl(struct inode *inode, struct file *file,
1775 unsigned int cmd, unsigned long arg)
1777 struct block_device *bdev = inode->i_bdev;
1778 return generic_ide_ioctl(bdev, cmd, arg);
1781 static int idedisk_media_changed(struct gendisk *disk)
1783 ide_drive_t *drive = disk->private_data;
1785 /* do not scan partitions twice if this is a removable device */
1786 if (drive->attach) {
1790 /* if removable, always assume it was changed */
1791 return drive->removable;
1794 static int idedisk_revalidate_disk(struct gendisk *disk)
1796 ide_drive_t *drive = disk->private_data;
1797 set_capacity(disk, current_capacity(drive));
1801 static struct block_device_operations idedisk_ops = {
1802 .owner = THIS_MODULE,
1803 .open = idedisk_open,
1804 .release = idedisk_release,
1805 .ioctl = idedisk_ioctl,
1806 .media_changed = idedisk_media_changed,
1807 .revalidate_disk= idedisk_revalidate_disk
1810 MODULE_DESCRIPTION("ATA DISK Driver");
1812 static int idedisk_attach(ide_drive_t *drive)
1814 struct gendisk *g = drive->disk;
1816 /* strstr("foo", "") is non-NULL */
1817 if (!strstr("ide-disk", drive->driver_req))
1819 if (!drive->present)
1821 if (drive->media != ide_disk)
1824 if (ide_register_subdriver(drive, &idedisk_driver)) {
1825 printk (KERN_ERR "ide-disk: %s: Failed to register the driver with ide.c\n", drive->name);
1828 DRIVER(drive)->busy++;
1829 idedisk_setup(drive);
1830 if ((!drive->head || drive->head > 16) && !drive->select.b.lba) {
1831 printk(KERN_ERR "%s: INVALID GEOMETRY: %d PHYSICAL HEADS?\n",
1832 drive->name, drive->head);
1833 if ((drive->id->cfs_enable_2 & 0x3000) && drive->wcache)
1834 if (do_idedisk_flushcache(drive))
1835 printk (KERN_INFO "%s: Write Cache FAILED Flushing!\n",
1837 ide_unregister_subdriver(drive);
1838 DRIVER(drive)->busy--;
1841 DRIVER(drive)->busy--;
1842 g->minors = 1 << PARTN_BITS;
1843 strcpy(g->devfs_name, drive->devfs_name);
1844 g->driverfs_dev = &drive->gendev;
1845 g->flags = drive->removable ? GENHD_FL_REMOVABLE : 0;
1846 set_capacity(g, current_capacity(drive));
1847 g->fops = &idedisk_ops;
1855 static void __exit idedisk_exit (void)
1857 ide_unregister_driver(&idedisk_driver);
1860 static int idedisk_init (void)
1862 return ide_register_driver(&idedisk_driver);
1865 module_init(idedisk_init);
1866 module_exit(idedisk_exit);
1867 MODULE_LICENSE("GPL");