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 Developemt
6 * Andre Hedrick <andre@linux-ide.org>
7 * Copyright (C) 2003 Red Hat <alan@redhat.com>
13 * Mostly written by Mark Lord <mlord@pobox.com>
14 * and Gadi Oxman <gadio@netvision.net.il>
15 * and Andre Hedrick <andre@linux-ide.org>
17 * This is the IDE/ATA disk driver, as evolved from hd.c and ide.c.
19 * Version 1.00 move disk only code from ide.c to ide-disk.c
20 * support optional byte-swapping of all data
21 * Version 1.01 fix previous byte-swapping code
22 * Version 1.02 remove ", LBA" from drive identification msgs
23 * Version 1.03 fix display of id->buf_size for big-endian
24 * Version 1.04 add /proc configurable settings and S.M.A.R.T support
25 * Version 1.05 add capacity support for ATA3 >= 8GB
26 * Version 1.06 get boot-up messages to show full cyl count
27 * Version 1.07 disable door-locking if it fails
28 * Version 1.08 fixed CHS/LBA translations for ATA4 > 8GB,
29 * process of adding new ATA4 compliance.
30 * fixed problems in allowing fdisk to see
32 * Version 1.09 added increment of rq->sector in ide_multwrite
33 * added UDMA 3/4 reporting
34 * Version 1.10 request queue changes, Ultra DMA 100
35 * Version 1.11 added 48-bit lba
36 * Version 1.12 adding taskfile io access method
37 * Version 1.13 added standby and flush-cache for notifier
38 * Version 1.14 added acoustic-wcache
39 * Version 1.15 convert all calls to ide_raw_taskfile
40 * since args will return register content.
41 * Version 1.16 added suspend-resume-checkpower
42 * Version 1.17 do flush on standy, do flush on ATA < ATA6
46 #define IDEDISK_VERSION "1.17"
48 #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>
73 /* FIXME: some day we shouldnt need to look in here! */
75 #include "legacy/pdc4030.h"
77 static int driver_blocked;
79 static inline u32 idedisk_read_24 (ide_drive_t *drive)
82 return (HWIF(drive)->INB(IDE_HCYL_REG)<<16) |
83 (HWIF(drive)->INB(IDE_LCYL_REG)<<8) |
84 HWIF(drive)->INB(IDE_SECTOR_REG);
86 u8 hcyl = HWIF(drive)->INB(IDE_HCYL_REG);
87 u8 lcyl = HWIF(drive)->INB(IDE_LCYL_REG);
88 u8 sect = HWIF(drive)->INB(IDE_SECTOR_REG);
89 return (hcyl<<16)|(lcyl<<8)|sect;
93 static int idedisk_end_request(ide_drive_t *drive, int uptodate);
96 * lba_capacity_is_ok() performs a sanity check on the claimed "lba_capacity"
97 * value for this drive (from its reported identification information).
99 * Returns: 1 if lba_capacity looks sensible
102 * It is called only once for each drive.
104 static int lba_capacity_is_ok (struct hd_driveid *id)
106 unsigned long lba_sects, chs_sects, head, tail;
108 if ((id->command_set_2 & 0x0400) && (id->cfs_enable_2 & 0x0400)) {
109 printk("48-bit Drive: %llu \n", id->lba_capacity_2);
114 * The ATA spec tells large drives to return
115 * C/H/S = 16383/16/63 independent of their size.
116 * Some drives can be jumpered to use 15 heads instead of 16.
117 * Some drives can be jumpered to use 4092 cyls instead of 16383.
119 if ((id->cyls == 16383
120 || (id->cyls == 4092 && id->cur_cyls == 16383)) &&
122 (id->heads == 15 || id->heads == 16) &&
123 (id->lba_capacity >= 16383*63*id->heads))
126 lba_sects = id->lba_capacity;
127 chs_sects = id->cyls * id->heads * id->sectors;
129 /* perform a rough sanity check on lba_sects: within 10% is OK */
130 if ((lba_sects - chs_sects) < chs_sects/10)
133 /* some drives have the word order reversed */
134 head = ((lba_sects >> 16) & 0xffff);
135 tail = (lba_sects & 0xffff);
136 lba_sects = (head | (tail << 16));
137 if ((lba_sects - chs_sects) < chs_sects/10) {
138 id->lba_capacity = lba_sects;
139 return 1; /* lba_capacity is (now) good */
142 return 0; /* lba_capacity value may be bad */
145 #ifndef CONFIG_IDE_TASKFILE_IO
148 * read_intr() is the handler for disk read/multread interrupts
150 static ide_startstop_t read_intr (ide_drive_t *drive)
152 ide_hwif_t *hwif = HWIF(drive);
153 u32 i = 0, nsect = 0, msect = drive->mult_count;
159 /* new way for dealing with premature shared PCI interrupts */
160 if (!OK_STAT(stat=hwif->INB(IDE_STATUS_REG),DATA_READY,BAD_R_STAT)) {
161 if (stat & (ERR_STAT|DRQ_STAT)) {
162 return DRIVER(drive)->error(drive, "read_intr", stat);
164 /* no data yet, so wait for another interrupt */
165 ide_set_handler(drive, &read_intr, WAIT_CMD, NULL);
170 rq = HWGROUP(drive)->rq;
172 if ((nsect = rq->current_nr_sectors) > msect)
177 to = ide_map_buffer(rq, &flags);
178 taskfile_input_data(drive, to, nsect * SECTOR_WORDS);
181 printk("%s: read: sectors(%ld-%ld), buffer=0x%08lx, remaining=%ld\n",
182 drive->name, rq->sector, rq->sector+nsect-1,
183 (unsigned long) rq->buffer+(nsect<<9), rq->nr_sectors-nsect);
186 ide_unmap_buffer(to, &flags);
189 i = (rq->nr_sectors -= nsect);
190 if (((long)(rq->current_nr_sectors -= nsect)) <= 0)
191 idedisk_end_request(drive, 1);
193 * Another BH Page walker and DATA INTERGRITY Questioned on ERROR.
194 * If passed back up on multimode read, BAD DATA could be ACKED
195 * to FILE SYSTEMS above ...
200 ide_set_handler(drive, &read_intr, WAIT_CMD, NULL);
207 * write_intr() is the handler for disk write interrupts
209 static ide_startstop_t write_intr (ide_drive_t *drive)
211 ide_hwgroup_t *hwgroup = HWGROUP(drive);
212 ide_hwif_t *hwif = HWIF(drive);
213 struct request *rq = hwgroup->rq;
217 if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG),
218 DRIVE_READY, drive->bad_wstat)) {
219 printk("%s: write_intr error1: nr_sectors=%ld, stat=0x%02x\n",
220 drive->name, rq->nr_sectors, stat);
223 printk("%s: write: sector %ld, buffer=0x%08lx, remaining=%ld\n",
224 drive->name, rq->sector, (unsigned long) rq->buffer,
227 if ((rq->nr_sectors == 1) ^ ((stat & DRQ_STAT) != 0)) {
230 i = --rq->nr_sectors;
231 --rq->current_nr_sectors;
232 if (((long)rq->current_nr_sectors) <= 0)
233 idedisk_end_request(drive, 1);
236 char *to = ide_map_buffer(rq, &flags);
237 taskfile_output_data(drive, to, SECTOR_WORDS);
238 ide_unmap_buffer(to, &flags);
239 ide_set_handler(drive, &write_intr, WAIT_CMD, NULL);
244 /* the original code did this here (?) */
247 return DRIVER(drive)->error(drive, "write_intr", stat);
251 * ide_multwrite() transfers a block of up to mcount sectors of data
252 * to a drive as part of a disk multiple-sector write operation.
254 * Returns 0 on success.
256 * Note that we may be called from two contexts - the do_rw_disk context
257 * and IRQ context. The IRQ can happen any time after we've output the
258 * full "mcount" number of sectors, so we must make sure we update the
259 * state _before_ we output the final part of the data!
261 * The update and return to BH is a BLOCK Layer Fakey to get more data
262 * to satisfy the hardware atomic segment. If the hardware atomic segment
263 * is shorter or smaller than the BH segment then we should be OKAY.
264 * This is only valid if we can rewind the rq->current_nr_sectors counter.
266 int ide_multwrite (ide_drive_t *drive, unsigned int mcount)
268 ide_hwgroup_t *hwgroup = HWGROUP(drive);
269 struct request *rq = &hwgroup->wrq;
273 int nsect = rq->current_nr_sectors;
279 buffer = ide_map_buffer(rq, &flags);
282 rq->nr_sectors -= nsect;
283 rq->current_nr_sectors -= nsect;
285 /* Do we move to the next bh after this? */
286 if (!rq->current_nr_sectors) {
287 struct buffer_head *bh = rq->bh->b_reqnext;
289 /* end early early we ran out of requests */
294 rq->current_nr_sectors = bh->b_size >> 9;
295 rq->hard_cur_sectors = rq->current_nr_sectors;
296 rq->buffer = bh->b_data;
301 * Ok, we're all setup for the interrupt
302 * re-entering us on the last transfer.
304 taskfile_output_data(drive, buffer, nsect<<7);
305 ide_unmap_buffer(buffer, &flags);
312 * multwrite_intr() is the handler for disk multwrite interrupts
314 static ide_startstop_t multwrite_intr (ide_drive_t *drive)
316 ide_hwgroup_t *hwgroup = HWGROUP(drive);
317 ide_hwif_t *hwif = HWIF(drive);
318 struct request *rq = &hwgroup->wrq;
322 if (OK_STAT(stat = hwif->INB(IDE_STATUS_REG),
323 DRIVE_READY, drive->bad_wstat)) {
324 if (stat & DRQ_STAT) {
326 * The drive wants data. Remember rq is the copy
329 if (rq->nr_sectors) {
330 if (ide_multwrite(drive, drive->mult_count))
332 ide_set_handler(drive, &multwrite_intr, WAIT_CMD, NULL);
337 * If the copy has all the blocks completed then
338 * we can end the original request.
340 if (!rq->nr_sectors) { /* all done? */
342 for (i = rq->nr_sectors; i > 0;) {
343 i -= rq->current_nr_sectors;
344 idedisk_end_request(drive, 1);
349 /* the original code did this here (?) */
352 return DRIVER(drive)->error(drive, "multwrite_intr", stat);
357 * __ide_do_rw_disk() issues READ and WRITE commands to a disk,
358 * using LBA if supported, or CHS otherwise, to address sectors.
359 * It also takes care of issuing special DRIVE_CMDs.
362 ide_startstop_t __ide_do_rw_disk (ide_drive_t *drive, struct request *rq, unsigned long block)
364 ide_hwif_t *hwif = HWIF(drive);
365 u8 lba48 = (drive->addressing == 1) ? 1 : 0;
366 task_ioreg_t command = WIN_NOP;
367 ata_nsector_t nsectors;
369 nsectors.all = (u16) rq->nr_sectors;
372 panic("Request while ide driver is blocked?");
375 hwif->OUTB(drive->ctl, IDE_CONTROL_REG);
377 if (drive->select.b.lba) {
378 if (drive->addressing == 1) {
379 task_ioreg_t tasklets[10];
383 tasklets[2] = nsectors.b.low;
384 tasklets[3] = nsectors.b.high;
385 tasklets[4] = (task_ioreg_t) block;
386 tasklets[5] = (task_ioreg_t) (block>>8);
387 tasklets[6] = (task_ioreg_t) (block>>16);
388 tasklets[7] = (task_ioreg_t) (block>>24);
389 tasklets[8] = (task_ioreg_t) 0;
390 tasklets[9] = (task_ioreg_t) 0;
391 // tasklets[8] = (task_ioreg_t) (block>>32);
392 // tasklets[9] = (task_ioreg_t) (block>>40);
394 printk("%s: %sing: LBAsect=%lu, sectors=%ld, "
395 "buffer=0x%08lx, LBAsect=0x%012lx\n",
397 (rq->cmd==READ)?"read":"writ",
400 (unsigned long) rq->buffer,
402 printk("%s: 0x%02x%02x 0x%02x%02x%02x%02x%02x%02x\n",
403 drive->name, tasklets[3], tasklets[2],
404 tasklets[9], tasklets[8], tasklets[7],
405 tasklets[6], tasklets[5], tasklets[4]);
407 hwif->OUTB(tasklets[1], IDE_FEATURE_REG);
408 hwif->OUTB(tasklets[3], IDE_NSECTOR_REG);
409 hwif->OUTB(tasklets[7], IDE_SECTOR_REG);
410 hwif->OUTB(tasklets[8], IDE_LCYL_REG);
411 hwif->OUTB(tasklets[9], IDE_HCYL_REG);
413 hwif->OUTB(tasklets[0], IDE_FEATURE_REG);
414 hwif->OUTB(tasklets[2], IDE_NSECTOR_REG);
415 hwif->OUTB(tasklets[4], IDE_SECTOR_REG);
416 hwif->OUTB(tasklets[5], IDE_LCYL_REG);
417 hwif->OUTB(tasklets[6], IDE_HCYL_REG);
418 hwif->OUTB(0x00|drive->select.all,IDE_SELECT_REG);
421 printk("%s: %sing: LBAsect=%ld, sectors=%ld, "
423 drive->name, (rq->cmd==READ)?"read":"writ",
424 block, rq->nr_sectors,
425 (unsigned long) rq->buffer);
427 hwif->OUTB(0x00, IDE_FEATURE_REG);
428 hwif->OUTB(nsectors.b.low, IDE_NSECTOR_REG);
429 hwif->OUTB(block, IDE_SECTOR_REG);
430 hwif->OUTB(block>>=8, IDE_LCYL_REG);
431 hwif->OUTB(block>>=8, IDE_HCYL_REG);
432 hwif->OUTB(((block>>8)&0x0f)|drive->select.all,IDE_SELECT_REG);
435 unsigned int sect,head,cyl,track;
436 track = block / drive->sect;
437 sect = block % drive->sect + 1;
438 hwif->OUTB(sect, IDE_SECTOR_REG);
439 head = track % drive->head;
440 cyl = track / drive->head;
442 hwif->OUTB(0x00, IDE_FEATURE_REG);
443 hwif->OUTB(nsectors.b.low, IDE_NSECTOR_REG);
444 hwif->OUTB(cyl, IDE_LCYL_REG);
445 hwif->OUTB(cyl>>8, IDE_HCYL_REG);
446 hwif->OUTB(head|drive->select.all,IDE_SELECT_REG);
448 printk("%s: %sing: CHS=%d/%d/%d, sectors=%ld, buffer=0x%08lx\n",
449 drive->name, (rq->cmd==READ)?"read":"writ", cyl,
450 head, sect, rq->nr_sectors, (unsigned long) rq->buffer);
454 if (rq_data_dir(rq) == READ) {
455 #ifdef CONFIG_BLK_DEV_IDEDMA
456 if (drive->using_dma && !hwif->ide_dma_read(drive))
458 #endif /* CONFIG_BLK_DEV_IDEDMA */
459 if (HWGROUP(drive)->handler != NULL)
462 command = ((drive->mult_count) ?
463 ((lba48) ? WIN_MULTREAD_EXT : WIN_MULTREAD) :
464 ((lba48) ? WIN_READ_EXT : WIN_READ));
466 ide_execute_command(drive, command, &read_intr, WAIT_CMD, NULL);
468 } else if (rq_data_dir(rq) == WRITE) {
469 ide_startstop_t startstop;
470 #ifdef CONFIG_BLK_DEV_IDEDMA
471 if (drive->using_dma && !(HWIF(drive)->ide_dma_write(drive)))
473 #endif /* CONFIG_BLK_DEV_IDEDMA */
475 command = ((drive->mult_count) ?
476 ((lba48) ? WIN_MULTWRITE_EXT : WIN_MULTWRITE) :
477 ((lba48) ? WIN_WRITE_EXT : WIN_WRITE));
478 hwif->OUTB(command, IDE_COMMAND_REG);
480 if (ide_wait_stat(&startstop, drive, DATA_READY,
481 drive->bad_wstat, WAIT_DRQ)) {
482 printk(KERN_ERR "%s: no DRQ after issuing %s\n",
484 drive->mult_count ? "MULTWRITE" : "WRITE");
489 if (drive->mult_count) {
490 ide_hwgroup_t *hwgroup = HWGROUP(drive);
492 * Ugh.. this part looks ugly because we MUST set up
493 * the interrupt handler before outputting the first block
494 * of data to be written. If we hit an error (corrupted buffer list)
495 * in ide_multwrite(), then we need to remove the handler/timer
496 * before returning. Fortunately, this NEVER happens (right?).
498 * Except when you get an error it seems...
500 * MAJOR DATA INTEGRITY BUG !!! only if we error
502 hwgroup->wrq = *rq; /* scratchpad */
503 ide_set_handler(drive, &multwrite_intr, WAIT_CMD, NULL);
504 if (ide_multwrite(drive, drive->mult_count)) {
506 spin_lock_irqsave(&io_request_lock, flags);
507 hwgroup->handler = NULL;
508 del_timer(&hwgroup->timer);
509 spin_unlock_irqrestore(&io_request_lock, flags);
514 char *to = ide_map_buffer(rq, &flags);
515 ide_set_handler(drive, &write_intr, WAIT_CMD, NULL);
516 taskfile_output_data(drive, to, SECTOR_WORDS);
517 ide_unmap_buffer(to, &flags);
521 printk(KERN_ERR "%s: bad command: %d\n", drive->name, rq->cmd);
522 idedisk_end_request(drive, 0);
527 #else /* CONFIG_IDE_TASKFILE_IO */
529 static ide_startstop_t chs_rw_disk(ide_drive_t *, struct request *, unsigned long);
530 static ide_startstop_t lba_28_rw_disk(ide_drive_t *, struct request *, unsigned long);
531 static ide_startstop_t lba_48_rw_disk(ide_drive_t *, struct request *, unsigned long long);
534 * __ide_do_rw_disk() issues READ and WRITE commands to a disk,
535 * using LBA if supported, or CHS otherwise, to address sectors.
536 * It also takes care of issuing special DRIVE_CMDs.
538 ide_startstop_t __ide_do_rw_disk (ide_drive_t *drive, struct request *rq, unsigned long block)
540 if (!blk_fs_request(rq)) {
541 printk(KERN_ERR "%s: bad command: %d\n", drive->name, rq->cmd);
542 idedisk_end_request(drive, 0);
547 * 268435455 == 137439 MB or 28bit limit
549 * need to add split taskfile operations based on 28bit threshold.
552 if (drive->addressing == 1) /* 48-bit LBA */
553 return lba_48_rw_disk(drive, rq, (unsigned long long) block);
554 if (drive->select.b.lba) /* 28-bit LBA */
555 return lba_28_rw_disk(drive, rq, (unsigned long) block);
557 /* 28-bit CHS : DIE DIE DIE piece of legacy crap!!! */
558 return chs_rw_disk(drive, rq, (unsigned long) block);
561 static task_ioreg_t get_command (ide_drive_t *drive, int cmd)
563 int lba48bit = (drive->id->cfs_enable_2 & 0x0400) ? 1 : 0;
566 lba48bit = (drive->addressing == 1) ? 1 : 0;
569 if ((cmd == READ) && (drive->using_dma))
570 return (lba48bit) ? WIN_READDMA_EXT : WIN_READDMA;
571 else if ((cmd == READ) && (drive->mult_count))
572 return (lba48bit) ? WIN_MULTREAD_EXT : WIN_MULTREAD;
573 else if (cmd == READ)
574 return (lba48bit) ? WIN_READ_EXT : WIN_READ;
575 else if ((cmd == WRITE) && (drive->using_dma))
576 return (lba48bit) ? WIN_WRITEDMA_EXT : WIN_WRITEDMA;
577 else if ((cmd == WRITE) && (drive->mult_count))
578 return (lba48bit) ? WIN_MULTWRITE_EXT : WIN_MULTWRITE;
579 else if (cmd == WRITE)
580 return (lba48bit) ? WIN_WRITE_EXT : WIN_WRITE;
585 static ide_startstop_t chs_rw_disk (ide_drive_t *drive, struct request *rq, unsigned long block)
589 ata_nsector_t nsectors;
590 task_ioreg_t command = get_command(drive, rq_data_dir(rq));
591 unsigned int track = (block / drive->sect);
592 unsigned int sect = (block % drive->sect) + 1;
593 unsigned int head = (track % drive->head);
594 unsigned int cyl = (track / drive->head);
596 nsectors.all = (u16) rq->nr_sectors;
598 printk("%s: %sing: ", drive->name, (rq_data_dir(rq)==READ) ? "read" : "writ");
599 printk("CHS=%d/%d/%d, ", cyl, head, sect);
600 printk("sectors=%ld, ", rq->nr_sectors);
601 printk("buffer=0x%08lx\n", (unsigned long) rq->buffer);
604 memset(&args, 0, sizeof(ide_task_t));
606 sectors = (rq->nr_sectors == 256) ? 0x00 : rq->nr_sectors;
607 args.tfRegister[IDE_NSECTOR_OFFSET] = sectors;
608 args.tfRegister[IDE_SECTOR_OFFSET] = sect;
609 args.tfRegister[IDE_LCYL_OFFSET] = cyl;
610 args.tfRegister[IDE_HCYL_OFFSET] = (cyl>>8);
611 args.tfRegister[IDE_SELECT_OFFSET] = head;
612 args.tfRegister[IDE_SELECT_OFFSET] |= drive->select.all;
613 args.tfRegister[IDE_COMMAND_OFFSET] = command;
614 args.command_type = ide_cmd_type_parser(&args);
615 args.rq = (struct request *) rq;
616 rq->special = (ide_task_t *)&args;
617 return do_rw_taskfile(drive, &args);
620 static ide_startstop_t lba_28_rw_disk (ide_drive_t *drive, struct request *rq, unsigned long block)
624 ata_nsector_t nsectors;
625 task_ioreg_t command = get_command(drive, rq_data_dir(rq));
627 nsectors.all = (u16) rq->nr_sectors;
630 printk("%s: %sing: ", drive->name, (rq_data_dir(rq)==READ) ? "read" : "writ");
631 printk("LBAsect=%lld, ", block);
632 printk("sectors=%ld, ", rq->nr_sectors);
633 printk("buffer=0x%08lx\n", (unsigned long) rq->buffer);
636 memset(&args, 0, sizeof(ide_task_t));
638 sectors = (rq->nr_sectors == 256) ? 0x00 : rq->nr_sectors;
639 args.tfRegister[IDE_NSECTOR_OFFSET] = sectors;
640 args.tfRegister[IDE_SECTOR_OFFSET] = block;
641 args.tfRegister[IDE_LCYL_OFFSET] = (block>>=8);
642 args.tfRegister[IDE_HCYL_OFFSET] = (block>>=8);
643 args.tfRegister[IDE_SELECT_OFFSET] = ((block>>8)&0x0f);
644 args.tfRegister[IDE_SELECT_OFFSET] |= drive->select.all;
645 args.tfRegister[IDE_COMMAND_OFFSET] = command;
646 args.command_type = ide_cmd_type_parser(&args);
647 args.rq = (struct request *) rq;
648 rq->special = (ide_task_t *)&args;
649 return do_rw_taskfile(drive, &args);
653 * 268435455 == 137439 MB or 28bit limit
654 * 320173056 == 163929 MB or 48bit addressing
655 * 1073741822 == 549756 MB or 48bit addressing fake drive
658 static ide_startstop_t lba_48_rw_disk (ide_drive_t *drive, struct request *rq, unsigned long long block)
662 ata_nsector_t nsectors;
663 task_ioreg_t command = get_command(drive, rq_data_dir(rq));
665 nsectors.all = (u16) rq->nr_sectors;
668 printk("%s: %sing: ", drive->name, (rq_data_dir(rq)==READ) ? "read" : "writ");
669 printk("LBAsect=%lld, ", block);
670 printk("sectors=%ld, ", rq->nr_sectors);
671 printk("buffer=0x%08lx\n", (unsigned long) rq->buffer);
674 memset(&args, 0, sizeof(ide_task_t));
676 sectors = (rq->nr_sectors == 65536) ? 0 : rq->nr_sectors;
677 args.tfRegister[IDE_NSECTOR_OFFSET] = sectors;
678 args.tfRegister[IDE_SECTOR_OFFSET] = block; /* low lba */
679 args.tfRegister[IDE_LCYL_OFFSET] = (block>>=8); /* mid lba */
680 args.tfRegister[IDE_HCYL_OFFSET] = (block>>=8); /* hi lba */
681 args.tfRegister[IDE_SELECT_OFFSET] = drive->select.all;
682 args.tfRegister[IDE_COMMAND_OFFSET] = command;
683 args.hobRegister[IDE_NSECTOR_OFFSET_HOB]= sectors >> 8;
684 args.hobRegister[IDE_SECTOR_OFFSET_HOB] = (block>>=8); /* low lba */
685 args.hobRegister[IDE_LCYL_OFFSET_HOB] = (block>>=8); /* mid lba */
686 args.hobRegister[IDE_HCYL_OFFSET_HOB] = (block>>=8); /* hi lba */
687 args.hobRegister[IDE_SELECT_OFFSET_HOB] = drive->select.all;
688 args.hobRegister[IDE_CONTROL_OFFSET_HOB]= (drive->ctl|0x80);
689 args.command_type = ide_cmd_type_parser(&args);
690 args.rq = (struct request *) rq;
691 rq->special = (ide_task_t *)&args;
692 return do_rw_taskfile(drive, &args);
695 #endif /* CONFIG_IDE_TASKFILE_IO */
697 static ide_startstop_t ide_do_rw_disk (ide_drive_t *drive, struct request *rq, unsigned long block)
699 ide_hwif_t *hwif = HWIF(drive);
701 return hwif->rw_disk(drive, rq, block);
703 return __ide_do_rw_disk(drive, rq, block);
706 EXPORT_SYMBOL_GPL(__ide_do_rw_disk);
708 static int idedisk_open (struct inode *inode, struct file *filp, ide_drive_t *drive)
711 if (drive->removable && drive->usage == 1) {
714 memset(&args, 0, sizeof(ide_task_t));
715 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_DOORLOCK;
716 args.command_type = ide_cmd_type_parser(&args);
717 check_disk_change(inode->i_rdev);
719 * Ignore the return code from door_lock,
720 * since the open() has already succeeded,
721 * and the door_lock is irrelevant at this point.
723 if (drive->doorlocking && ide_raw_taskfile(drive, &args, NULL))
724 drive->doorlocking = 0;
726 /* Cache enabled ? */
727 if (drive->id->csfo & 1)
729 /* Cache command set available ? */
730 if (drive->id->cfs_enable_1 & (1<<5))
732 /* ATA6 cache extended commands */
733 cf = drive->id->command_set_2 >> 24;
734 if((cf & 0xC0) == 0x40 && (cf & 0x30) != 0)
740 static int do_idedisk_flushcache(ide_drive_t *drive);
742 static int ide_cacheflush_p(ide_drive_t *drive)
746 if (do_idedisk_flushcache(drive))
748 printk (KERN_INFO "%s: Write Cache FAILED Flushing!\n",
757 static void idedisk_release (struct inode *inode, struct file *filp, ide_drive_t *drive)
759 if (drive->removable && !drive->usage) {
761 memset(&args, 0, sizeof(ide_task_t));
762 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_DOORUNLOCK;
763 args.command_type = ide_cmd_type_parser(&args);
764 invalidate_bdev(inode->i_bdev, 0);
765 if (drive->doorlocking && ide_raw_taskfile(drive, &args, NULL))
766 drive->doorlocking = 0;
768 ide_cacheflush_p(drive);
772 static int idedisk_media_change (ide_drive_t *drive)
774 /* if removable, always assume it was changed */
775 return drive->removable;
778 static void idedisk_revalidate (ide_drive_t *drive)
780 grok_partitions(HWIF(drive)->gd, drive->select.b.unit,
782 current_capacity(drive));
785 static int idedisk_end_request (ide_drive_t *drive, int uptodate)
791 spin_lock_irqsave(&io_request_lock, flags);
792 rq = HWGROUP(drive)->rq;
795 * decide whether to reenable DMA -- 3 is a random magic for now,
796 * if we DMA timeout more than 3 times, just stay in PIO
798 if (drive->state == DMA_PIO_RETRY && drive->retry_pio <= 3) {
800 HWGROUP(drive)->hwif->ide_dma_on(drive);
803 if (!end_that_request_first(rq, uptodate, drive->name)) {
804 add_blkdev_randomness(MAJOR(rq->rq_dev));
805 blkdev_dequeue_request(rq);
806 HWGROUP(drive)->rq = NULL;
807 end_that_request_last(rq);
811 spin_unlock_irqrestore(&io_request_lock, flags);
815 static u8 idedisk_dump_status (ide_drive_t *drive, const char *msg, u8 stat)
817 ide_hwif_t *hwif = HWIF(drive);
821 local_irq_set(flags);
822 printk("%s: %s: status=0x%02x", drive->name, msg, stat);
823 #if FANCY_STATUS_DUMPS
825 if (stat & BUSY_STAT)
828 if (stat & READY_STAT) printk("DriveReady ");
829 if (stat & WRERR_STAT) printk("DeviceFault ");
830 if (stat & SEEK_STAT) printk("SeekComplete ");
831 if (stat & DRQ_STAT) printk("DataRequest ");
832 if (stat & ECC_STAT) printk("CorrectedError ");
833 if (stat & INDEX_STAT) printk("Index ");
834 if (stat & ERR_STAT) printk("Error ");
837 #endif /* FANCY_STATUS_DUMPS */
839 if ((stat & (BUSY_STAT|ERR_STAT)) == ERR_STAT) {
840 err = hwif->INB(IDE_ERROR_REG);
841 printk("%s: %s: error=0x%02x", drive->name, msg, err);
842 #if FANCY_STATUS_DUMPS
844 if (err & ABRT_ERR) printk("DriveStatusError ");
846 printk("Bad%s ", (err & ABRT_ERR) ? "CRC" : "Sector");
847 if (err & ECC_ERR) printk("UncorrectableError ");
848 if (err & ID_ERR) printk("SectorIdNotFound ");
849 if (err & TRK0_ERR) printk("TrackZeroNotFound ");
850 if (err & MARK_ERR) printk("AddrMarkNotFound ");
852 if ((err & (BBD_ERR | ABRT_ERR)) == BBD_ERR ||
853 (err & (ECC_ERR|ID_ERR|MARK_ERR))) {
854 if (drive->addressing == 1) {
856 u32 low = 0, high = 0;
857 low = idedisk_read_24(drive);
858 hwif->OUTB(drive->ctl|0x80, IDE_CONTROL_REG);
859 high = idedisk_read_24(drive);
860 sectors = ((__u64)high << 24) | low;
861 printk(", LBAsect=%llu, high=%d, low=%d",
862 (unsigned long long) sectors,
865 u8 cur = hwif->INB(IDE_SELECT_REG);
866 if (cur & 0x40) { /* using LBA? */
867 printk(", LBAsect=%ld", (unsigned long)
869 |(hwif->INB(IDE_HCYL_REG)<<16)
870 |(hwif->INB(IDE_LCYL_REG)<<8)
871 | hwif->INB(IDE_SECTOR_REG));
873 printk(", CHS=%d/%d/%d",
874 (hwif->INB(IDE_HCYL_REG)<<8) +
875 hwif->INB(IDE_LCYL_REG),
877 hwif->INB(IDE_SECTOR_REG));
880 if (HWGROUP(drive) && HWGROUP(drive)->rq)
881 printk(", sector=%ld",
882 HWGROUP(drive)->rq->sector);
885 #endif /* FANCY_STATUS_DUMPS */
887 local_irq_restore(flags);
891 ide_startstop_t idedisk_error (ide_drive_t *drive, const char *msg, u8 stat)
896 int i = (drive->mult_count ? drive->mult_count : 1) * SECTOR_WORDS;
898 err = idedisk_dump_status(drive, msg, stat);
900 if (drive == NULL || (rq = HWGROUP(drive)->rq) == NULL)
904 /* retry only "normal" I/O: */
908 case IDE_DRIVE_TASKFILE:
910 ide_end_drive_cmd(drive, stat, err);
913 case IDE_DRIVE_TASKFILE:
915 ide_end_taskfile(drive, stat, err);
922 if (stat & BUSY_STAT || ((stat & WRERR_STAT) && !drive->nowerr)) {
923 /* other bits are useless when BUSY */
924 rq->errors |= ERROR_RESET;
925 } else if (stat & ERR_STAT) {
926 /* err has different meaning on cdrom and tape */
927 if (err == ABRT_ERR) {
928 if (drive->select.b.lba &&
929 /* some newer drives don't support WIN_SPECIFY */
930 hwif->INB(IDE_COMMAND_REG) == WIN_SPECIFY)
932 } else if ((err & BAD_CRC) == BAD_CRC) {
933 /* UDMA crc error, just retry the operation */
935 } else if (err & (BBD_ERR | ECC_ERR)) {
936 /* retries won't help these */
937 rq->errors = ERROR_MAX;
938 } else if (err & TRK0_ERR) {
939 /* help it find track zero */
940 rq->errors |= ERROR_RECAL;
943 if ((stat & DRQ_STAT) && rq_data_dir(rq) == READ) {
945 * try_to_flush_leftover_data() is invoked in response to
946 * a drive unexpectedly having its DRQ_STAT bit set. As
947 * an alternative to resetting the drive, this routine
948 * tries to clear the condition by read a sector's worth
949 * of data from the drive. Of course, this may not help
950 * if the drive is *waiting* for data from *us*.
954 unsigned int wcount = (i > 16) ? 16 : i;
956 taskfile_input_data(drive, buffer, wcount);
959 if (hwif->INB(IDE_STATUS_REG) & (BUSY_STAT|DRQ_STAT)) {
961 hwif->OUTB(WIN_IDLEIMMEDIATE,IDE_COMMAND_REG);
963 if (rq->errors >= ERROR_MAX)
964 DRIVER(drive)->end_request(drive, 0);
966 if ((rq->errors & ERROR_RESET) == ERROR_RESET) {
968 return ide_do_reset(drive);
970 if ((rq->errors & ERROR_RECAL) == ERROR_RECAL)
971 drive->special.b.recalibrate = 1;
977 ide_startstop_t idedisk_abort(ide_drive_t *drive, const char *msg)
982 if (drive == NULL || (rq = HWGROUP(drive)->rq) == NULL)
986 /* retry only "normal" I/O: */
990 case IDE_DRIVE_TASKFILE:
992 ide_end_drive_cmd(drive, BUSY_STAT, 0);
995 case IDE_DRIVE_TASKFILE:
997 ide_end_taskfile(drive, BUSY_STAT, 0);
1004 rq->errors |= ERROR_RESET;
1005 DRIVER(drive)->end_request(drive, 0);
1010 * Queries for true maximum capacity of the drive.
1011 * Returns maximum LBA address (> 0) of the drive, 0 if failed.
1013 static unsigned long idedisk_read_native_max_address(ide_drive_t *drive)
1016 unsigned long addr = 0;
1018 /* Create IDE/ATA command request structure */
1019 memset(&args, 0, sizeof(ide_task_t));
1020 args.tfRegister[IDE_SELECT_OFFSET] = 0x40;
1021 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_READ_NATIVE_MAX;
1022 args.command_type = ide_cmd_type_parser(&args);
1023 /* submit command request */
1024 ide_raw_taskfile(drive, &args, NULL);
1026 /* if OK, compute maximum address value */
1027 if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
1028 addr = ((args.tfRegister[IDE_SELECT_OFFSET] & 0x0f) << 24)
1029 | ((args.tfRegister[ IDE_HCYL_OFFSET] ) << 16)
1030 | ((args.tfRegister[ IDE_LCYL_OFFSET] ) << 8)
1031 | ((args.tfRegister[IDE_SECTOR_OFFSET] ));
1033 addr++; /* since the return value is (maxlba - 1), we add 1 */
1037 static unsigned long long idedisk_read_native_max_address_ext(ide_drive_t *drive)
1040 unsigned long long addr = 0;
1042 /* Create IDE/ATA command request structure */
1043 memset(&args, 0, sizeof(ide_task_t));
1045 args.tfRegister[IDE_SELECT_OFFSET] = 0x40;
1046 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_READ_NATIVE_MAX_EXT;
1047 args.command_type = ide_cmd_type_parser(&args);
1048 /* submit command request */
1049 ide_raw_taskfile(drive, &args, NULL);
1051 /* if OK, compute maximum address value */
1052 if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
1053 u32 high = ((args.hobRegister[IDE_HCYL_OFFSET_HOB])<<16) |
1054 ((args.hobRegister[IDE_LCYL_OFFSET_HOB])<<8) |
1055 (args.hobRegister[IDE_SECTOR_OFFSET_HOB]);
1056 u32 low = ((args.tfRegister[IDE_HCYL_OFFSET])<<16) |
1057 ((args.tfRegister[IDE_LCYL_OFFSET])<<8) |
1058 (args.tfRegister[IDE_SECTOR_OFFSET]);
1059 addr = ((__u64)high << 24) | low;
1061 addr++; /* since the return value is (maxlba - 1), we add 1 */
1065 #ifdef CONFIG_IDEDISK_STROKE
1067 * Sets maximum virtual LBA address of the drive.
1068 * Returns new maximum virtual LBA address (> 0) or 0 on failure.
1070 static unsigned long idedisk_set_max_address(ide_drive_t *drive, unsigned long addr_req)
1073 unsigned long addr_set = 0;
1076 /* Create IDE/ATA command request structure */
1077 memset(&args, 0, sizeof(ide_task_t));
1078 args.tfRegister[IDE_SECTOR_OFFSET] = ((addr_req >> 0) & 0xff);
1079 args.tfRegister[IDE_LCYL_OFFSET] = ((addr_req >> 8) & 0xff);
1080 args.tfRegister[IDE_HCYL_OFFSET] = ((addr_req >> 16) & 0xff);
1081 args.tfRegister[IDE_SELECT_OFFSET] = ((addr_req >> 24) & 0x0f) | 0x40;
1082 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SET_MAX;
1083 args.command_type = ide_cmd_type_parser(&args);
1084 /* submit command request */
1085 ide_raw_taskfile(drive, &args, NULL);
1086 /* if OK, read new maximum address value */
1087 if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
1088 addr_set = ((args.tfRegister[IDE_SELECT_OFFSET] & 0x0f) << 24)
1089 | ((args.tfRegister[ IDE_HCYL_OFFSET] ) << 16)
1090 | ((args.tfRegister[ IDE_LCYL_OFFSET] ) << 8)
1091 | ((args.tfRegister[IDE_SECTOR_OFFSET] ));
1097 static unsigned long long idedisk_set_max_address_ext(ide_drive_t *drive, unsigned long long addr_req)
1100 unsigned long long addr_set = 0;
1103 /* Create IDE/ATA command request structure */
1104 memset(&args, 0, sizeof(ide_task_t));
1105 args.tfRegister[IDE_SECTOR_OFFSET] = ((addr_req >> 0) & 0xff);
1106 args.tfRegister[IDE_LCYL_OFFSET] = ((addr_req >>= 8) & 0xff);
1107 args.tfRegister[IDE_HCYL_OFFSET] = ((addr_req >>= 8) & 0xff);
1108 args.tfRegister[IDE_SELECT_OFFSET] = 0x40;
1109 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SET_MAX_EXT;
1110 args.hobRegister[IDE_SECTOR_OFFSET_HOB] = ((addr_req >>= 8) & 0xff);
1111 args.hobRegister[IDE_LCYL_OFFSET_HOB] = ((addr_req >>= 8) & 0xff);
1112 args.hobRegister[IDE_HCYL_OFFSET_HOB] = ((addr_req >>= 8) & 0xff);
1113 args.hobRegister[IDE_SELECT_OFFSET_HOB] = 0x40;
1114 args.hobRegister[IDE_CONTROL_OFFSET_HOB]= (drive->ctl|0x80);
1115 args.command_type = ide_cmd_type_parser(&args);
1116 /* submit command request */
1117 ide_raw_taskfile(drive, &args, NULL);
1118 /* if OK, compute maximum address value */
1119 if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
1120 u32 high = ((args.hobRegister[IDE_HCYL_OFFSET_HOB])<<16) |
1121 ((args.hobRegister[IDE_LCYL_OFFSET_HOB])<<8) |
1122 (args.hobRegister[IDE_SECTOR_OFFSET_HOB]);
1123 u32 low = ((args.tfRegister[IDE_HCYL_OFFSET])<<16) |
1124 ((args.tfRegister[IDE_LCYL_OFFSET])<<8) |
1125 (args.tfRegister[IDE_SECTOR_OFFSET]);
1126 addr_set = ((__u64)high << 24) | low;
1131 #endif /* CONFIG_IDEDISK_STROKE */
1134 * Tests if the drive supports Host Protected Area feature.
1135 * Returns true if supported, false otherwise.
1137 static inline int idedisk_supports_host_protected_area(ide_drive_t *drive)
1139 int flag = (drive->id->cfs_enable_1 & 0x0400) ? 1 : 0;
1141 printk("%s: host protected area => %d\n", drive->name, flag);
1146 * Compute drive->capacity, the full capacity of the drive
1147 * Called with drive->id != NULL.
1149 * To compute capacity, this uses either of
1151 * 1. CHS value set by user (whatever user sets will be trusted)
1152 * 2. LBA value from target drive (require new ATA feature)
1153 * 3. LBA value from system BIOS (new one is OK, old one may break)
1154 * 4. CHS value from system BIOS (traditional style)
1156 * in above order (i.e., if value of higher priority is available,
1157 * reset will be ignored).
1159 #define IDE_STROKE_LIMIT (32000*1024*2)
1160 static void init_idedisk_capacity (ide_drive_t *drive)
1162 struct hd_driveid *id = drive->id;
1163 unsigned long capacity = drive->cyl * drive->head * drive->sect;
1164 int have_setmax = idedisk_supports_host_protected_area(drive);
1165 unsigned long set_max =
1166 (have_setmax ? idedisk_read_native_max_address(drive) : 0);
1167 unsigned long long capacity_2 = capacity;
1168 unsigned long long set_max_ext;
1170 drive->capacity48 = 0;
1171 drive->select.b.lba = 0;
1173 if (id->cfs_enable_2 & 0x0400) {
1174 capacity_2 = id->lba_capacity_2;
1175 drive->head = drive->bios_head = 255;
1176 drive->sect = drive->bios_sect = 63;
1177 drive->cyl = (unsigned int) capacity_2 / (drive->head * drive->sect);
1178 drive->select.b.lba = 1;
1179 set_max_ext = idedisk_read_native_max_address_ext(drive);
1180 if (set_max_ext > capacity_2 && capacity_2 > IDE_STROKE_LIMIT) {
1181 #ifdef CONFIG_IDEDISK_STROKE
1182 set_max_ext = idedisk_read_native_max_address_ext(drive);
1183 set_max_ext = idedisk_set_max_address_ext(drive, set_max_ext);
1185 drive->capacity48 = capacity_2 = set_max_ext;
1186 drive->cyl = (unsigned int) set_max_ext / (drive->head * drive->sect);
1187 drive->select.b.lba = 1;
1188 drive->id->lba_capacity_2 = capacity_2;
1190 #else /* !CONFIG_IDEDISK_STROKE */
1191 printk(KERN_INFO "%s: setmax_ext LBA %llu, native %llu\n",
1192 drive->name, set_max_ext, capacity_2);
1193 #endif /* CONFIG_IDEDISK_STROKE */
1195 drive->cyl = (unsigned int) capacity_2 / (drive->head * drive->sect);
1196 drive->bios_cyl = drive->cyl;
1197 drive->capacity48 = capacity_2;
1198 drive->capacity = (unsigned long) capacity_2;
1199 goto check_capacity48;
1200 /* Determine capacity, and use LBA if the drive properly supports it */
1201 } else if ((id->capability & 2) && lba_capacity_is_ok(id)) {
1202 capacity = id->lba_capacity;
1203 drive->cyl = capacity / (drive->head * drive->sect);
1204 drive->select.b.lba = 1;
1207 if (set_max > capacity && capacity > IDE_STROKE_LIMIT) {
1208 #ifdef CONFIG_IDEDISK_STROKE
1209 set_max = idedisk_read_native_max_address(drive);
1210 set_max = idedisk_set_max_address(drive, set_max);
1212 drive->capacity = capacity = set_max;
1213 drive->cyl = set_max / (drive->head * drive->sect);
1214 drive->select.b.lba = 1;
1215 drive->id->lba_capacity = capacity;
1217 #else /* !CONFIG_IDEDISK_STROKE */
1218 printk(KERN_INFO "%s: setmax LBA %lu, native %lu\n",
1219 drive->name, set_max, capacity);
1220 #endif /* CONFIG_IDEDISK_STROKE */
1223 drive->capacity = capacity;
1225 if ((id->command_set_2 & 0x0400) && (id->cfs_enable_2 & 0x0400)) {
1226 drive->capacity48 = id->lba_capacity_2;
1229 drive->cyl = (unsigned long)(drive->capacity48) / (drive->head * drive->sect);
1233 /* Limit disk size to 137GB if LBA48 addressing is not supported */
1234 if (drive->addressing == 0 && drive->capacity48 > (1ULL)<<28) {
1235 printk("%s: cannot use LBA48 - capacity reset "
1236 "from %llu to %llu\n",
1237 drive->name, drive->capacity48, (1ULL)<<28);
1238 drive->capacity48 = (1ULL)<<28;
1242 static unsigned long idedisk_capacity (ide_drive_t *drive)
1244 if (drive->id->cfs_enable_2 & 0x0400)
1245 return (drive->capacity48 - drive->sect0);
1246 return (drive->capacity - drive->sect0);
1249 static ide_startstop_t idedisk_special (ide_drive_t *drive)
1251 special_t *s = &drive->special;
1253 if (s->b.set_geometry) {
1254 s->b.set_geometry = 0;
1255 if (!IS_PDC4030_DRIVE) {
1257 memset(&args, 0, sizeof(ide_task_t));
1258 args.tfRegister[IDE_NSECTOR_OFFSET] = drive->sect;
1259 args.tfRegister[IDE_SECTOR_OFFSET] = drive->sect;
1260 args.tfRegister[IDE_LCYL_OFFSET] = drive->cyl;
1261 args.tfRegister[IDE_HCYL_OFFSET] = drive->cyl>>8;
1262 args.tfRegister[IDE_SELECT_OFFSET] = ((drive->head-1)|drive->select.all)&0xBF;
1263 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SPECIFY;
1264 args.command_type = ide_cmd_type_parser(&args);
1265 do_rw_taskfile(drive, &args);
1267 } else if (s->b.recalibrate) {
1268 s->b.recalibrate = 0;
1269 if (!IS_PDC4030_DRIVE) {
1271 memset(&args, 0, sizeof(ide_task_t));
1272 args.tfRegister[IDE_NSECTOR_OFFSET] = drive->sect;
1273 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_RESTORE;
1274 args.command_type = ide_cmd_type_parser(&args);
1275 do_rw_taskfile(drive, &args);
1277 } else if (s->b.set_multmode) {
1278 s->b.set_multmode = 0;
1279 if (drive->mult_req > drive->id->max_multsect)
1280 drive->mult_req = drive->id->max_multsect;
1281 if (!IS_PDC4030_DRIVE) {
1283 memset(&args, 0, sizeof(ide_task_t));
1284 args.tfRegister[IDE_NSECTOR_OFFSET] = drive->mult_req;
1285 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SETMULT;
1286 args.command_type = ide_cmd_type_parser(&args);
1287 do_rw_taskfile(drive, &args);
1289 } else if (s->all) {
1290 int special = s->all;
1292 printk(KERN_ERR "%s: bad special flag: 0x%02x\n", drive->name, special);
1295 return IS_PDC4030_DRIVE ? ide_stopped : ide_started;
1298 static void idedisk_pre_reset (ide_drive_t *drive)
1300 int legacy = (drive->id->cfs_enable_2 & 0x0400) ? 0 : 1;
1302 drive->special.all = 0;
1303 drive->special.b.set_geometry = legacy;
1304 drive->special.b.recalibrate = legacy;
1305 if (OK_TO_RESET_CONTROLLER)
1306 drive->mult_count = 0;
1307 if (!drive->keep_settings && !drive->using_dma)
1308 drive->mult_req = 0;
1309 if (drive->mult_req != drive->mult_count)
1310 drive->special.b.set_multmode = 1;
1313 #ifdef CONFIG_PROC_FS
1315 static int smart_enable(ide_drive_t *drive)
1319 memset(&args, 0, sizeof(ide_task_t));
1320 args.tfRegister[IDE_FEATURE_OFFSET] = SMART_ENABLE;
1321 args.tfRegister[IDE_LCYL_OFFSET] = SMART_LCYL_PASS;
1322 args.tfRegister[IDE_HCYL_OFFSET] = SMART_HCYL_PASS;
1323 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SMART;
1324 args.command_type = ide_cmd_type_parser(&args);
1325 return ide_raw_taskfile(drive, &args, NULL);
1328 static int get_smart_values(ide_drive_t *drive, u8 *buf)
1332 memset(&args, 0, sizeof(ide_task_t));
1333 args.tfRegister[IDE_FEATURE_OFFSET] = SMART_READ_VALUES;
1334 args.tfRegister[IDE_NSECTOR_OFFSET] = 0x01;
1335 args.tfRegister[IDE_LCYL_OFFSET] = SMART_LCYL_PASS;
1336 args.tfRegister[IDE_HCYL_OFFSET] = SMART_HCYL_PASS;
1337 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SMART;
1338 args.command_type = ide_cmd_type_parser(&args);
1339 (void) smart_enable(drive);
1340 return ide_raw_taskfile(drive, &args, buf);
1343 static int get_smart_thresholds(ide_drive_t *drive, u8 *buf)
1346 memset(&args, 0, sizeof(ide_task_t));
1347 args.tfRegister[IDE_FEATURE_OFFSET] = SMART_READ_THRESHOLDS;
1348 args.tfRegister[IDE_NSECTOR_OFFSET] = 0x01;
1349 args.tfRegister[IDE_LCYL_OFFSET] = SMART_LCYL_PASS;
1350 args.tfRegister[IDE_HCYL_OFFSET] = SMART_HCYL_PASS;
1351 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SMART;
1352 args.command_type = ide_cmd_type_parser(&args);
1353 (void) smart_enable(drive);
1354 return ide_raw_taskfile(drive, &args, buf);
1357 static int proc_idedisk_read_cache
1358 (char *page, char **start, off_t off, int count, int *eof, void *data)
1360 ide_drive_t *drive = (ide_drive_t *) data;
1365 len = sprintf(out,"%i\n", drive->id->buf_size / 2);
1367 len = sprintf(out,"(none)\n");
1368 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
1371 static int proc_idedisk_read_smart_thresholds
1372 (char *page, char **start, off_t off, int count, int *eof, void *data)
1374 ide_drive_t *drive = (ide_drive_t *)data;
1377 if (!get_smart_thresholds(drive, page)) {
1378 unsigned short *val = (unsigned short *) page;
1379 char *out = ((char *)val) + (SECTOR_WORDS * 4);
1382 out += sprintf(out, "%04x%c", le16_to_cpu(*val), (++i & 7) ? ' ' : '\n');
1384 } while (i < (SECTOR_WORDS * 2));
1387 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
1390 static int proc_idedisk_read_smart_values
1391 (char *page, char **start, off_t off, int count, int *eof, void *data)
1393 ide_drive_t *drive = (ide_drive_t *)data;
1396 if (!get_smart_values(drive, page)) {
1397 unsigned short *val = (unsigned short *) page;
1398 char *out = ((char *)val) + (SECTOR_WORDS * 4);
1401 out += sprintf(out, "%04x%c", le16_to_cpu(*val), (++i & 7) ? ' ' : '\n');
1403 } while (i < (SECTOR_WORDS * 2));
1406 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
1409 static ide_proc_entry_t idedisk_proc[] = {
1410 { "cache", S_IFREG|S_IRUGO, proc_idedisk_read_cache, NULL },
1411 { "geometry", S_IFREG|S_IRUGO, proc_ide_read_geometry, NULL },
1412 { "smart_values", S_IFREG|S_IRUSR, proc_idedisk_read_smart_values, NULL },
1413 { "smart_thresholds", S_IFREG|S_IRUSR, proc_idedisk_read_smart_thresholds, NULL },
1414 { NULL, 0, NULL, NULL }
1419 #define idedisk_proc NULL
1421 #endif /* CONFIG_PROC_FS */
1424 * This is tightly woven into the driver->do_special can not touch.
1425 * DON'T do it again until a total personality rewrite is committed.
1427 static int set_multcount(ide_drive_t *drive, int arg)
1431 if (drive->special.b.set_multmode)
1433 ide_init_drive_cmd (&rq);
1434 rq.cmd = IDE_DRIVE_CMD;
1435 drive->mult_req = arg;
1436 drive->special.b.set_multmode = 1;
1437 (void) ide_do_drive_cmd (drive, &rq, ide_wait);
1438 return (drive->mult_count == arg) ? 0 : -EIO;
1441 static int set_nowerr(ide_drive_t *drive, int arg)
1443 if (ide_spin_wait_hwgroup(drive))
1445 drive->nowerr = arg;
1446 drive->bad_wstat = arg ? BAD_R_STAT : BAD_W_STAT;
1447 spin_unlock_irq(&io_request_lock);
1451 static int write_cache (ide_drive_t *drive, int arg)
1455 if (!(drive->id->cfs_enable_2 & 0x3000))
1458 memset(&args, 0, sizeof(ide_task_t));
1459 args.tfRegister[IDE_FEATURE_OFFSET] = (arg) ?
1460 SETFEATURES_EN_WCACHE : SETFEATURES_DIS_WCACHE;
1461 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SETFEATURES;
1462 args.command_type = ide_cmd_type_parser(&args);
1463 (void) ide_raw_taskfile(drive, &args, NULL);
1465 drive->wcache = arg;
1469 static int call_idedisk_standby (ide_drive_t *drive, int arg)
1472 u8 standby = (arg) ? WIN_STANDBYNOW2 : WIN_STANDBYNOW1;
1473 memset(&args, 0, sizeof(ide_task_t));
1474 args.tfRegister[IDE_COMMAND_OFFSET] = standby;
1475 args.command_type = ide_cmd_type_parser(&args);
1476 return ide_raw_taskfile(drive, &args, NULL);
1479 static int do_idedisk_standby (ide_drive_t *drive)
1481 return call_idedisk_standby(drive, 0);
1484 static int call_idedisk_suspend (ide_drive_t *drive, int arg)
1487 u8 suspend = (arg) ? WIN_SLEEPNOW2 : WIN_SLEEPNOW1;
1488 memset(&args, 0, sizeof(ide_task_t));
1489 args.tfRegister[IDE_COMMAND_OFFSET] = suspend;
1490 args.command_type = ide_cmd_type_parser(&args);
1491 return ide_raw_taskfile(drive, &args, NULL);
1494 static int do_idedisk_suspend (ide_drive_t *drive)
1496 if (drive->suspend_reset)
1498 ide_cacheflush_p(drive);
1499 return call_idedisk_suspend(drive, 0);
1503 static int call_idedisk_checkpower (ide_drive_t *drive, int arg)
1506 u8 ckpw = (arg) ? WIN_CHECKPOWERMODE2 : WIN_CHECKPOWERMODE1;
1507 memset(&args, 0, sizeof(ide_task_t));
1508 args.tfRegister[IDE_COMMAND_OFFSET] = ckpw;
1509 args.command_type = ide_cmd_type_parser(&args);
1510 ide_raw_taskfile(drive, &args, NULL);
1512 if (errno != EIO || args[0] != 0 || args[1] != 0)
1517 state = (args[2] == 255) ? "active/idle" : "standby";
1522 static int do_idedisk_checkpower (ide_drive_t *drive)
1524 return call_idedisk_checkpower(drive, 0);
1528 static int do_idedisk_resume (ide_drive_t *drive)
1530 if (!drive->suspend_reset)
1535 static int do_idedisk_flushcache (ide_drive_t *drive)
1539 memset(&args, 0, sizeof(ide_task_t));
1540 if (drive->id->cfs_enable_2 & 0x2400)
1541 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE_EXT;
1543 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE;
1544 args.command_type = ide_cmd_type_parser(&args);
1545 return ide_raw_taskfile(drive, &args, NULL);
1548 static int set_acoustic (ide_drive_t *drive, int arg)
1552 memset(&args, 0, sizeof(ide_task_t));
1553 args.tfRegister[IDE_FEATURE_OFFSET] = (arg) ? SETFEATURES_EN_AAM :
1554 SETFEATURES_DIS_AAM;
1555 args.tfRegister[IDE_NSECTOR_OFFSET] = arg;
1556 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SETFEATURES;
1557 args.command_type = ide_cmd_type_parser(&args);
1558 ide_raw_taskfile(drive, &args, NULL);
1559 drive->acoustic = arg;
1563 static int probe_lba_addressing (ide_drive_t *drive, int arg)
1565 drive->addressing = 0;
1567 if (HWIF(drive)->addressing)
1570 if (!(drive->id->cfs_enable_2 & 0x0400))
1572 drive->addressing = arg;
1576 static int set_lba_addressing (ide_drive_t *drive, int arg)
1578 return (probe_lba_addressing(drive, arg));
1581 static void idedisk_add_settings(ide_drive_t *drive)
1583 struct hd_driveid *id = drive->id;
1584 int major = HWIF(drive)->major;
1585 int minor = drive->select.b.unit << PARTN_BITS;
1587 ide_add_setting(drive, "bios_cyl", SETTING_RW, -1, -1, TYPE_INT, 0, 65535, 1, 1, &drive->bios_cyl, NULL);
1588 ide_add_setting(drive, "bios_head", SETTING_RW, -1, -1, TYPE_BYTE, 0, 255, 1, 1, &drive->bios_head, NULL);
1589 ide_add_setting(drive, "bios_sect", SETTING_RW, -1, -1, TYPE_BYTE, 0, 63, 1, 1, &drive->bios_sect, NULL);
1590 ide_add_setting(drive, "address", SETTING_RW, HDIO_GET_ADDRESS, HDIO_SET_ADDRESS, TYPE_INTA, 0, 2, 1, 1, &drive->addressing, set_lba_addressing);
1591 ide_add_setting(drive, "bswap", SETTING_READ, -1, -1, TYPE_BYTE, 0, 1, 1, 1, &drive->bswap, NULL);
1592 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);
1593 ide_add_setting(drive, "nowerr", SETTING_RW, HDIO_GET_NOWERR, HDIO_SET_NOWERR, TYPE_BYTE, 0, 1, 1, 1, &drive->nowerr, set_nowerr);
1594 ide_add_setting(drive, "breada_readahead", SETTING_RW, BLKRAGET, BLKRASET, TYPE_INT, 0, 255, 1, 1, &read_ahead[major], NULL);
1595 ide_add_setting(drive, "file_readahead", SETTING_RW, BLKFRAGET, BLKFRASET, TYPE_INTA, 0, 4096, PAGE_SIZE, 1024, &max_readahead[major][minor], NULL);
1596 ide_add_setting(drive, "max_kb_per_request", SETTING_RW, BLKSECTGET, BLKSECTSET, TYPE_INTA, 1, 255, 1, 1, &max_sectors[major][minor], NULL);
1597 ide_add_setting(drive, "lun", SETTING_RW, -1, -1, TYPE_INT, 0, 7, 1, 1, &drive->lun, NULL);
1598 ide_add_setting(drive, "wcache", SETTING_RW, HDIO_GET_WCACHE, HDIO_SET_WCACHE, TYPE_BYTE, 0, 1, 1, 1, &drive->wcache, write_cache);
1599 ide_add_setting(drive, "acoustic", SETTING_RW, HDIO_GET_ACOUSTIC, HDIO_SET_ACOUSTIC, TYPE_BYTE, 0, 254, 1, 1, &drive->acoustic, set_acoustic);
1600 ide_add_setting(drive, "failures", SETTING_RW, -1, -1, TYPE_INT, 0, 65535, 1, 1, &drive->failures, NULL);
1601 ide_add_setting(drive, "max_failures", SETTING_RW, -1, -1, TYPE_INT, 0, 65535, 1, 1, &drive->max_failures, NULL);
1604 static int idedisk_ioctl (ide_drive_t *drive, struct inode *inode,
1605 struct file *file, unsigned int cmd, unsigned long arg)
1622 static void idedisk_setup (ide_drive_t *drive)
1626 struct hd_driveid *id = drive->id;
1627 unsigned long capacity;
1629 idedisk_add_settings(drive);
1631 if (drive->id_read == 0)
1635 * CompactFlash cards and their brethern look just like hard drives
1636 * to us, but they are removable and don't have a doorlock mechanism.
1638 if (drive->removable && !(drive->is_flash)) {
1640 * Removable disks (eg. SYQUEST); ignore 'WD' drives
1642 if (id->model[0] != 'W' || id->model[1] != 'D') {
1643 drive->doorlocking = 1;
1646 for (i = 0; i < MAX_DRIVES; ++i) {
1647 ide_hwif_t *hwif = HWIF(drive);
1649 if (drive != &hwif->drives[i]) continue;
1650 hwif->gd->de_arr[i] = drive->de;
1651 if (drive->removable)
1652 hwif->gd->flags[i] |= GENHD_FL_REMOVABLE;
1657 (void) probe_lba_addressing(drive, 1);
1659 /* if using 48-bit addressing bump the request size up */
1660 if (probe_lba_addressing(drive, 1))
1661 blk_queue_max_sectors(&drive->queue, 2048);
1664 /* Extract geometry if we did not already have one for the drive */
1665 if (!drive->cyl || !drive->head || !drive->sect) {
1666 drive->cyl = drive->bios_cyl = id->cyls;
1667 drive->head = drive->bios_head = id->heads;
1668 drive->sect = drive->bios_sect = id->sectors;
1671 /* Handle logical geometry translation by the drive */
1672 if ((id->field_valid & 1) && id->cur_cyls &&
1673 id->cur_heads && (id->cur_heads <= 16) && id->cur_sectors) {
1674 drive->cyl = id->cur_cyls;
1675 drive->head = id->cur_heads;
1676 drive->sect = id->cur_sectors;
1679 /* Use physical geometry if what we have still makes no sense */
1680 if (drive->head > 16 && id->heads && id->heads <= 16) {
1681 drive->cyl = id->cyls;
1682 drive->head = id->heads;
1683 drive->sect = id->sectors;
1686 /* calculate drive capacity, and select LBA if possible */
1687 init_idedisk_capacity (drive);
1690 * if possible, give fdisk access to more of the drive,
1691 * by correcting bios_cyls:
1693 capacity = idedisk_capacity (drive);
1694 if ((capacity >= (drive->bios_cyl * drive->bios_sect * drive->bios_head)) &&
1695 (!drive->forced_geom) && drive->bios_sect && drive->bios_head)
1696 drive->bios_cyl = (capacity / drive->bios_sect) / drive->bios_head;
1697 printk (KERN_INFO "%s: %ld sectors", drive->name, capacity);
1699 /* Give size in megabytes (MB), not mebibytes (MiB). */
1700 /* We compute the exact rounded value, avoiding overflow. */
1701 printk (" (%ld MB)", (capacity - capacity/625 + 974)/1950);
1703 /* Only print cache size when it was specified */
1705 printk (" w/%dKiB Cache", id->buf_size/2);
1707 printk(", CHS=%d/%d/%d",
1708 drive->bios_cyl, drive->bios_head, drive->bios_sect);
1709 #ifdef CONFIG_BLK_DEV_IDEDMA
1710 if (drive->using_dma)
1711 (void) HWIF(drive)->ide_dma_verbose(drive);
1712 #endif /* CONFIG_BLK_DEV_IDEDMA */
1715 drive->mult_count = 0;
1716 if (id->max_multsect) {
1717 id->multsect = ((id->max_multsect/2) > 1) ? id->max_multsect : 0;
1718 id->multsect_valid = id->multsect ? 1 : 0;
1719 drive->mult_req = id->multsect_valid ? id->max_multsect : INITIAL_MULT_COUNT;
1720 drive->special.b.set_multmode = drive->mult_req ? 1 : 0;
1722 drive->no_io_32bit = id->dword_io ? 1 : 0;
1723 if (drive->id->cfs_enable_2 & 0x3000)
1724 write_cache(drive, (id->cfs_enable_2 & 0x3000));
1727 static int idedisk_cleanup(ide_drive_t *drive)
1729 ide_cacheflush_p(drive);
1730 return ide_unregister_subdriver(drive);
1733 int idedisk_init (void);
1734 int idedisk_attach(ide_drive_t *drive);
1737 * IDE subdriver functions, registered with ide.c
1739 static ide_driver_t idedisk_driver = {
1741 version: IDEDISK_VERSION,
1745 supports_dsc_overlap: 0,
1746 cleanup: idedisk_cleanup,
1747 standby: do_idedisk_standby,
1748 suspend: do_idedisk_suspend,
1749 resume: do_idedisk_resume,
1750 flushcache: do_idedisk_flushcache,
1751 do_request: ide_do_rw_disk,
1752 end_request: idedisk_end_request,
1753 sense: idedisk_dump_status,
1754 error: idedisk_error,
1755 abort: idedisk_abort,
1756 ioctl: idedisk_ioctl,
1758 release: idedisk_release,
1759 media_change: idedisk_media_change,
1760 revalidate: idedisk_revalidate,
1761 pre_reset: idedisk_pre_reset,
1762 capacity: idedisk_capacity,
1763 special: idedisk_special,
1766 attach: idedisk_attach,
1767 ata_prebuilder: NULL,
1768 atapi_prebuilder: NULL,
1771 static ide_module_t idedisk_module = {
1778 MODULE_DESCRIPTION("ATA DISK Driver");
1780 int idedisk_attach (ide_drive_t *drive)
1785 if (ide_register_subdriver(drive,
1786 &idedisk_driver, IDE_SUBDRIVER_VERSION)) {
1787 printk(KERN_ERR "ide-disk: %s: Failed to register the "
1788 "driver with ide.c\n", drive->name);
1792 DRIVER(drive)->busy++;
1793 idedisk_setup(drive);
1794 if ((!drive->head || drive->head > 16) && !drive->select.b.lba) {
1795 printk(KERN_ERR "%s: INVALID GEOMETRY: %d PHYSICAL HEADS?\n",
1796 drive->name, drive->head);
1797 (void) idedisk_cleanup(drive);
1800 DRIVER(drive)->busy--;
1806 static void __exit idedisk_exit (void)
1811 while ((drive = ide_scan_devices(ide_disk, idedisk_driver.name,
1812 &idedisk_driver, failed)) != NULL) {
1813 if (idedisk_cleanup (drive)) {
1814 printk(KERN_ERR "%s: cleanup_module() called while "
1815 "still busy\n", drive->name);
1818 #ifdef CONFIG_PROC_FS
1819 /* We must remove proc entries defined in this module.
1820 * Otherwise we oops while accessing these entries
1823 ide_remove_proc_entries(drive->proc, idedisk_proc);
1826 ide_unregister_module(&idedisk_module);
1829 int idedisk_init (void)
1831 #ifdef CLASSIC_BUILTINS_METHOD
1834 #endif /* CLASSIC_BUILTINS_METHOD */
1838 #ifdef CLASSIC_BUILTINS_METHOD
1839 while ((drive = ide_scan_devices(ide_disk,
1840 idedisk_driver.name, NULL, failed++)) != NULL) {
1841 if (ide_register_subdriver(drive,
1842 &idedisk_driver, IDE_SUBDRIVER_VERSION)) {
1843 printk(KERN_ERR "ide-disk: %s: Failed to register "
1844 "the driver with ide.c\n", drive->name);
1847 DRIVER(drive)->busy++;
1848 idedisk_setup(drive);
1849 if ((!drive->head || drive->head > 16) &&
1850 (!drive->select.b.lba)) {
1851 printk(KERN_ERR "%s: INVALID GEOMETRY: %d "
1852 "PHYSICAL HEADS?\n", drive->name, drive->head);
1853 (void) idedisk_cleanup(drive);
1854 DRIVER(drive)->busy--;
1857 DRIVER(drive)->busy--;
1860 #endif /* CLASSIC_BUILTINS_METHOD */
1862 ide_register_module(&idedisk_module);
1867 ide_startstop_t panic_box(ide_drive_t *drive)
1870 panic("%s: Attempted to corrupt something: ide operation "
1872 printk(KERN_ERR "%s: Attempted to corrupt something: ide operation "
1874 "was pending accross suspend/resume.\n", drive->name);
1878 int ide_disks_busy(void)
1881 for (i=0; i<MAX_HWIFS; i++) {
1882 struct hwgroup_s *hwgroup = ide_hwifs[i].hwgroup;
1883 if (!hwgroup) continue;
1884 if ((hwgroup->handler) && (hwgroup->handler != panic_box))
1890 void ide_disk_suspend(void)
1893 while (ide_disks_busy()) {
1897 for (i=0; i<MAX_HWIFS; i++) {
1898 struct hwgroup_s *hwgroup = ide_hwifs[i].hwgroup;
1900 if (!hwgroup) continue;
1901 hwgroup->handler_save = hwgroup->handler;
1902 hwgroup->handler = panic_box;
1905 if (ide_disks_busy())
1906 panic("How did you get that request through?!");
1909 /* unsuspend and resume should be equal in the ideal world */
1911 void ide_disk_unsuspend(void)
1914 for (i=0; i<MAX_HWIFS; i++) {
1915 struct hwgroup_s *hwgroup = ide_hwifs[i].hwgroup;
1917 if (!hwgroup) continue;
1918 hwgroup->handler = NULL; /* hwgroup->handler_save; */
1919 hwgroup->handler_save = NULL;
1924 void ide_disk_resume(void)
1927 for (i=0; i<MAX_HWIFS; i++) {
1928 struct hwgroup_s *hwgroup = ide_hwifs[i].hwgroup;
1930 if (!hwgroup) continue;
1931 if (hwgroup->handler != panic_box)
1932 panic("Handler was not set to panic?");
1933 hwgroup->handler_save = NULL;
1934 hwgroup->handler = NULL;
1939 module_init(idedisk_init);
1940 module_exit(idedisk_exit);
1941 MODULE_LICENSE("GPL");