2 * linux/drivers/ide/ide-disk.c Version 1.16 April 7, 2002
4 * Copyright (C) 1998-2002 Linux ATA Developemt
5 * Andre Hedrick <andre@linux-ide.org>
8 * Copyright (C) 1994-1998 Linus Torvalds & authors (see below)
12 * Mostly written by Mark Lord <mlord@pobox.com>
13 * and Gadi Oxman <gadio@netvision.net.il>
14 * and Andre Hedrick <andre@linux-ide.org>
16 * This is the IDE/ATA disk driver, as evolved from hd.c and ide.c.
18 * Version 1.00 move disk only code from ide.c to ide-disk.c
19 * support optional byte-swapping of all data
20 * Version 1.01 fix previous byte-swapping code
21 * Version 1.02 remove ", LBA" from drive identification msgs
22 * Version 1.03 fix display of id->buf_size for big-endian
23 * Version 1.04 add /proc configurable settings and S.M.A.R.T support
24 * Version 1.05 add capacity support for ATA3 >= 8GB
25 * Version 1.06 get boot-up messages to show full cyl count
26 * Version 1.07 disable door-locking if it fails
27 * Version 1.08 fixed CHS/LBA translations for ATA4 > 8GB,
28 * process of adding new ATA4 compliance.
29 * fixed problems in allowing fdisk to see
31 * Version 1.09 added increment of rq->sector in ide_multwrite
32 * added UDMA 3/4 reporting
33 * Version 1.10 request queue changes, Ultra DMA 100
34 * Version 1.11 added 48-bit lba
35 * Version 1.12 adding taskfile io access method
36 * Version 1.13 added standby and flush-cache for notifier
37 * Version 1.14 added acoustic-wcache
38 * Version 1.15 convert all calls to ide_raw_taskfile
39 * since args will return register content.
40 * Version 1.16 added suspend-resume-checkpower
43 #define IDEDISK_VERSION "1.16"
45 #undef REALLY_SLOW_IO /* most systems can safely undef this */
47 #include <linux/config.h>
48 #include <linux/module.h>
49 #include <linux/types.h>
50 #include <linux/string.h>
51 #include <linux/kernel.h>
52 #include <linux/timer.h>
54 #include <linux/interrupt.h>
55 #include <linux/major.h>
56 #include <linux/errno.h>
57 #include <linux/genhd.h>
58 #include <linux/slab.h>
59 #include <linux/delay.h>
63 #include <linux/ide.h>
65 #include <asm/byteorder.h>
67 #include <asm/uaccess.h>
70 /* FIXME: soem day we shouldnt need to look in here! */
72 #include "legacy/pdc4030.h"
74 static int driver_blocked;
76 static inline u32 idedisk_read_24 (ide_drive_t *drive)
79 return (HWIF(drive)->INB(IDE_HCYL_REG)<<16) |
80 (HWIF(drive)->INB(IDE_LCYL_REG)<<8) |
81 HWIF(drive)->INB(IDE_SECTOR_REG);
83 u8 hcyl = HWIF(drive)->INB(IDE_HCYL_REG);
84 u8 lcyl = HWIF(drive)->INB(IDE_LCYL_REG);
85 u8 sect = HWIF(drive)->INB(IDE_SECTOR_REG);
86 return (hcyl<<16)|(lcyl<<8)|sect;
90 static int idedisk_end_request(ide_drive_t *drive, int uptodate);
93 * lba_capacity_is_ok() performs a sanity check on the claimed "lba_capacity"
94 * value for this drive (from its reported identification information).
96 * Returns: 1 if lba_capacity looks sensible
99 * It is called only once for each drive.
101 static int lba_capacity_is_ok (struct hd_driveid *id)
103 unsigned long lba_sects, chs_sects, head, tail;
105 if ((id->command_set_2 & 0x0400) && (id->cfs_enable_2 & 0x0400)) {
106 printk("48-bit Drive: %llu \n", id->lba_capacity_2);
111 * The ATA spec tells large drives to return
112 * C/H/S = 16383/16/63 independent of their size.
113 * Some drives can be jumpered to use 15 heads instead of 16.
114 * Some drives can be jumpered to use 4092 cyls instead of 16383.
116 if ((id->cyls == 16383
117 || (id->cyls == 4092 && id->cur_cyls == 16383)) &&
119 (id->heads == 15 || id->heads == 16) &&
120 (id->lba_capacity >= 16383*63*id->heads))
123 lba_sects = id->lba_capacity;
124 chs_sects = id->cyls * id->heads * id->sectors;
126 /* perform a rough sanity check on lba_sects: within 10% is OK */
127 if ((lba_sects - chs_sects) < chs_sects/10)
130 /* some drives have the word order reversed */
131 head = ((lba_sects >> 16) & 0xffff);
132 tail = (lba_sects & 0xffff);
133 lba_sects = (head | (tail << 16));
134 if ((lba_sects - chs_sects) < chs_sects/10) {
135 id->lba_capacity = lba_sects;
136 return 1; /* lba_capacity is (now) good */
139 return 0; /* lba_capacity value may be bad */
142 #ifndef CONFIG_IDE_TASKFILE_IO
145 * read_intr() is the handler for disk read/multread interrupts
147 static ide_startstop_t read_intr (ide_drive_t *drive)
149 ide_hwif_t *hwif = HWIF(drive);
150 u32 i = 0, nsect = 0, msect = drive->mult_count;
156 /* new way for dealing with premature shared PCI interrupts */
157 if (!OK_STAT(stat=hwif->INB(IDE_STATUS_REG),DATA_READY,BAD_R_STAT)) {
158 if (stat & (ERR_STAT|DRQ_STAT)) {
159 return DRIVER(drive)->error(drive, "read_intr", stat);
161 /* no data yet, so wait for another interrupt */
162 if (HWGROUP(drive)->handler != NULL)
164 ide_set_handler(drive, &read_intr, WAIT_CMD, NULL);
169 rq = HWGROUP(drive)->rq;
171 if ((nsect = rq->current_nr_sectors) > msect)
176 to = ide_map_buffer(rq, &flags);
177 taskfile_input_data(drive, to, nsect * SECTOR_WORDS);
180 printk("%s: read: sectors(%ld-%ld), buffer=0x%08lx, remaining=%ld\n",
181 drive->name, rq->sector, rq->sector+nsect-1,
182 (unsigned long) rq->buffer+(nsect<<9), rq->nr_sectors-nsect);
185 ide_unmap_buffer(to, &flags);
188 i = (rq->nr_sectors -= nsect);
189 if (((long)(rq->current_nr_sectors -= nsect)) <= 0)
190 idedisk_end_request(drive, 1);
192 * Another BH Page walker and DATA INTERGRITY Questioned on ERROR.
193 * If passed back up on multimode read, BAD DATA could be ACKED
194 * to FILE SYSTEMS above ...
199 if (HWGROUP(drive)->handler != NULL)
201 ide_set_handler(drive, &read_intr, WAIT_CMD, NULL);
208 * write_intr() is the handler for disk write interrupts
210 static ide_startstop_t write_intr (ide_drive_t *drive)
212 ide_hwgroup_t *hwgroup = HWGROUP(drive);
213 ide_hwif_t *hwif = HWIF(drive);
214 struct request *rq = hwgroup->rq;
218 if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG),
219 DRIVE_READY, drive->bad_wstat)) {
220 printk("%s: write_intr error1: nr_sectors=%ld, stat=0x%02x\n",
221 drive->name, rq->nr_sectors, stat);
224 printk("%s: write: sector %ld, buffer=0x%08lx, remaining=%ld\n",
225 drive->name, rq->sector, (unsigned long) rq->buffer,
228 if ((rq->nr_sectors == 1) ^ ((stat & DRQ_STAT) != 0)) {
231 i = --rq->nr_sectors;
232 --rq->current_nr_sectors;
233 if (((long)rq->current_nr_sectors) <= 0)
234 idedisk_end_request(drive, 1);
237 char *to = ide_map_buffer(rq, &flags);
238 taskfile_output_data(drive, to, SECTOR_WORDS);
239 ide_unmap_buffer(to, &flags);
240 if (HWGROUP(drive)->handler != NULL)
242 ide_set_handler(drive, &write_intr, WAIT_CMD, NULL);
247 /* the original code did this here (?) */
250 return DRIVER(drive)->error(drive, "write_intr", stat);
254 * ide_multwrite() transfers a block of up to mcount sectors of data
255 * to a drive as part of a disk multiple-sector write operation.
257 * Returns 0 on success.
259 * Note that we may be called from two contexts - the do_rw_disk context
260 * and IRQ context. The IRQ can happen any time after we've output the
261 * full "mcount" number of sectors, so we must make sure we update the
262 * state _before_ we output the final part of the data!
264 * The update and return to BH is a BLOCK Layer Fakey to get more data
265 * to satisfy the hardware atomic segment. If the hardware atomic segment
266 * is shorter or smaller than the BH segment then we should be OKAY.
267 * This is only valid if we can rewind the rq->current_nr_sectors counter.
269 int ide_multwrite (ide_drive_t *drive, unsigned int mcount)
271 ide_hwgroup_t *hwgroup = HWGROUP(drive);
272 struct request *rq = &hwgroup->wrq;
276 int nsect = rq->current_nr_sectors;
282 buffer = ide_map_buffer(rq, &flags);
285 rq->nr_sectors -= nsect;
286 rq->current_nr_sectors -= nsect;
288 /* Do we move to the next bh after this? */
289 if (!rq->current_nr_sectors) {
290 struct buffer_head *bh = rq->bh->b_reqnext;
292 /* end early early we ran out of requests */
297 rq->current_nr_sectors = bh->b_size >> 9;
298 rq->hard_cur_sectors = rq->current_nr_sectors;
299 rq->buffer = bh->b_data;
304 * Ok, we're all setup for the interrupt
305 * re-entering us on the last transfer.
307 taskfile_output_data(drive, buffer, nsect<<7);
308 ide_unmap_buffer(buffer, &flags);
315 * multwrite_intr() is the handler for disk multwrite interrupts
317 static ide_startstop_t multwrite_intr (ide_drive_t *drive)
319 ide_hwgroup_t *hwgroup = HWGROUP(drive);
320 ide_hwif_t *hwif = HWIF(drive);
321 struct request *rq = &hwgroup->wrq;
325 if (OK_STAT(stat = hwif->INB(IDE_STATUS_REG),
326 DRIVE_READY, drive->bad_wstat)) {
327 if (stat & DRQ_STAT) {
329 * The drive wants data. Remember rq is the copy
332 if (rq->nr_sectors) {
333 if (ide_multwrite(drive, drive->mult_count))
335 if (HWGROUP(drive)->handler != NULL)
337 ide_set_handler(drive, &multwrite_intr, WAIT_CMD, NULL);
342 * If the copy has all the blocks completed then
343 * we can end the original request.
345 if (!rq->nr_sectors) { /* all done? */
347 for (i = rq->nr_sectors; i > 0;) {
348 i -= rq->current_nr_sectors;
349 idedisk_end_request(drive, 1);
354 /* the original code did this here (?) */
357 return DRIVER(drive)->error(drive, "multwrite_intr", stat);
362 * do_rw_disk() issues READ and WRITE commands to a disk,
363 * using LBA if supported, or CHS otherwise, to address sectors.
364 * It also takes care of issuing special DRIVE_CMDs.
366 static ide_startstop_t do_rw_disk (ide_drive_t *drive, struct request *rq, unsigned long block)
368 ide_hwif_t *hwif = HWIF(drive);
369 u8 lba48 = (drive->addressing == 1) ? 1 : 0;
370 task_ioreg_t command = WIN_NOP;
371 ata_nsector_t nsectors;
373 nsectors.all = (u16) rq->nr_sectors;
376 panic("Request while ide driver is blocked?");
378 #if defined(CONFIG_BLK_DEV_PDC4030) || defined(CONFIG_BLK_DEV_PDC4030_MODULE)
379 if (IS_PDC4030_DRIVE)
380 return promise_rw_disk(drive, rq, block);
381 #endif /* CONFIG_BLK_DEV_PDC4030 */
384 hwif->OUTB(drive->ctl, IDE_CONTROL_REG);
386 if (drive->select.b.lba) {
387 if (drive->addressing == 1) {
388 task_ioreg_t tasklets[10];
392 tasklets[2] = nsectors.b.low;
393 tasklets[3] = nsectors.b.high;
394 tasklets[4] = (task_ioreg_t) block;
395 tasklets[5] = (task_ioreg_t) (block>>8);
396 tasklets[6] = (task_ioreg_t) (block>>16);
397 tasklets[7] = (task_ioreg_t) (block>>24);
398 tasklets[8] = (task_ioreg_t) 0;
399 tasklets[9] = (task_ioreg_t) 0;
400 // tasklets[8] = (task_ioreg_t) (block>>32);
401 // tasklets[9] = (task_ioreg_t) (block>>40);
403 printk("%s: %sing: LBAsect=%lu, sectors=%ld, "
404 "buffer=0x%08lx, LBAsect=0x%012lx\n",
406 (rq->cmd==READ)?"read":"writ",
409 (unsigned long) rq->buffer,
411 printk("%s: 0x%02x%02x 0x%02x%02x%02x%02x%02x%02x\n",
412 drive->name, tasklets[3], tasklets[2],
413 tasklets[9], tasklets[8], tasklets[7],
414 tasklets[6], tasklets[5], tasklets[4]);
416 hwif->OUTB(tasklets[1], IDE_FEATURE_REG);
417 hwif->OUTB(tasklets[3], IDE_NSECTOR_REG);
418 hwif->OUTB(tasklets[7], IDE_SECTOR_REG);
419 hwif->OUTB(tasklets[8], IDE_LCYL_REG);
420 hwif->OUTB(tasklets[9], IDE_HCYL_REG);
422 hwif->OUTB(tasklets[0], IDE_FEATURE_REG);
423 hwif->OUTB(tasklets[2], IDE_NSECTOR_REG);
424 hwif->OUTB(tasklets[4], IDE_SECTOR_REG);
425 hwif->OUTB(tasklets[5], IDE_LCYL_REG);
426 hwif->OUTB(tasklets[6], IDE_HCYL_REG);
427 hwif->OUTB(0x00|drive->select.all,IDE_SELECT_REG);
430 printk("%s: %sing: LBAsect=%ld, sectors=%ld, "
432 drive->name, (rq->cmd==READ)?"read":"writ",
433 block, rq->nr_sectors,
434 (unsigned long) rq->buffer);
436 hwif->OUTB(0x00, IDE_FEATURE_REG);
437 hwif->OUTB(nsectors.b.low, IDE_NSECTOR_REG);
438 hwif->OUTB(block, IDE_SECTOR_REG);
439 hwif->OUTB(block>>=8, IDE_LCYL_REG);
440 hwif->OUTB(block>>=8, IDE_HCYL_REG);
441 hwif->OUTB(((block>>8)&0x0f)|drive->select.all,IDE_SELECT_REG);
444 unsigned int sect,head,cyl,track;
445 track = block / drive->sect;
446 sect = block % drive->sect + 1;
447 hwif->OUTB(sect, IDE_SECTOR_REG);
448 head = track % drive->head;
449 cyl = track / drive->head;
451 hwif->OUTB(0x00, IDE_FEATURE_REG);
452 hwif->OUTB(nsectors.b.low, IDE_NSECTOR_REG);
453 hwif->OUTB(cyl, IDE_LCYL_REG);
454 hwif->OUTB(cyl>>8, IDE_HCYL_REG);
455 hwif->OUTB(head|drive->select.all,IDE_SELECT_REG);
457 printk("%s: %sing: CHS=%d/%d/%d, sectors=%ld, buffer=0x%08lx\n",
458 drive->name, (rq->cmd==READ)?"read":"writ", cyl,
459 head, sect, rq->nr_sectors, (unsigned long) rq->buffer);
463 if (rq_data_dir(rq) == READ) {
464 #ifdef CONFIG_BLK_DEV_IDEDMA
465 if (drive->using_dma && !hwif->ide_dma_read(drive))
467 #endif /* CONFIG_BLK_DEV_IDEDMA */
468 if (HWGROUP(drive)->handler != NULL)
470 ide_set_handler(drive, &read_intr, WAIT_CMD, NULL);
472 command = ((drive->mult_count) ?
473 ((lba48) ? WIN_MULTREAD_EXT : WIN_MULTREAD) :
474 ((lba48) ? WIN_READ_EXT : WIN_READ));
475 hwif->OUTB(command, IDE_COMMAND_REG);
477 } else if (rq_data_dir(rq) == WRITE) {
478 ide_startstop_t startstop;
479 #ifdef CONFIG_BLK_DEV_IDEDMA
480 if (drive->using_dma && !(HWIF(drive)->ide_dma_write(drive)))
482 #endif /* CONFIG_BLK_DEV_IDEDMA */
484 command = ((drive->mult_count) ?
485 ((lba48) ? WIN_MULTWRITE_EXT : WIN_MULTWRITE) :
486 ((lba48) ? WIN_WRITE_EXT : WIN_WRITE));
487 hwif->OUTB(command, IDE_COMMAND_REG);
489 if (ide_wait_stat(&startstop, drive, DATA_READY,
490 drive->bad_wstat, WAIT_DRQ)) {
491 printk(KERN_ERR "%s: no DRQ after issuing %s\n",
493 drive->mult_count ? "MULTWRITE" : "WRITE");
498 if (drive->mult_count) {
499 ide_hwgroup_t *hwgroup = HWGROUP(drive);
501 * Ugh.. this part looks ugly because we MUST set up
502 * the interrupt handler before outputting the first block
503 * of data to be written. If we hit an error (corrupted buffer list)
504 * in ide_multwrite(), then we need to remove the handler/timer
505 * before returning. Fortunately, this NEVER happens (right?).
507 * Except when you get an error it seems...
509 * MAJOR DATA INTEGRITY BUG !!! only if we error
511 hwgroup->wrq = *rq; /* scratchpad */
512 if (HWGROUP(drive)->handler != NULL)
514 ide_set_handler(drive, &multwrite_intr, WAIT_CMD, NULL);
515 if (ide_multwrite(drive, drive->mult_count)) {
517 spin_lock_irqsave(&io_request_lock, flags);
518 hwgroup->handler = NULL;
519 del_timer(&hwgroup->timer);
520 spin_unlock_irqrestore(&io_request_lock, flags);
525 char *to = ide_map_buffer(rq, &flags);
526 if (HWGROUP(drive)->handler != NULL)
528 ide_set_handler(drive, &write_intr, WAIT_CMD, NULL);
529 taskfile_output_data(drive, to, SECTOR_WORDS);
530 ide_unmap_buffer(to, &flags);
534 printk(KERN_ERR "%s: bad command: %d\n", drive->name, rq->cmd);
535 idedisk_end_request(drive, 0);
539 #else /* CONFIG_IDE_TASKFILE_IO */
541 static ide_startstop_t chs_rw_disk(ide_drive_t *, struct request *, unsigned long);
542 static ide_startstop_t lba_28_rw_disk(ide_drive_t *, struct request *, unsigned long);
543 static ide_startstop_t lba_48_rw_disk(ide_drive_t *, struct request *, unsigned long long);
546 * do_rw_disk() issues READ and WRITE commands to a disk,
547 * using LBA if supported, or CHS otherwise, to address sectors.
548 * It also takes care of issuing special DRIVE_CMDs.
550 static ide_startstop_t do_rw_disk (ide_drive_t *drive, struct request *rq, unsigned long block)
552 if (!blk_fs_request(rq)) {
553 printk(KERN_ERR "%s: bad command: %d\n", drive->name, rq->cmd);
554 idedisk_end_request(drive, 0);
559 * 268435455 == 137439 MB or 28bit limit
561 * need to add split taskfile operations based on 28bit threshold.
564 #if defined(CONFIG_BLK_DEV_PDC4030) || defined(CONFIG_BLK_DEV_PDC4030_MODULE)
565 if (IS_PDC4030_DRIVE)
566 return promise_rw_disk(drive, rq, block);
567 #endif /* CONFIG_BLK_DEV_PDC4030 */
569 if (drive->addressing == 1) /* 48-bit LBA */
570 return lba_48_rw_disk(drive, rq, (unsigned long long) block);
571 if (drive->select.b.lba) /* 28-bit LBA */
572 return lba_28_rw_disk(drive, rq, (unsigned long) block);
574 /* 28-bit CHS : DIE DIE DIE piece of legacy crap!!! */
575 return chs_rw_disk(drive, rq, (unsigned long) block);
578 static task_ioreg_t get_command (ide_drive_t *drive, int cmd)
580 int lba48bit = (drive->id->cfs_enable_2 & 0x0400) ? 1 : 0;
583 lba48bit = (drive->addressing == 1) ? 1 : 0;
586 if ((cmd == READ) && (drive->using_dma))
587 return (lba48bit) ? WIN_READDMA_EXT : WIN_READDMA;
588 else if ((cmd == READ) && (drive->mult_count))
589 return (lba48bit) ? WIN_MULTREAD_EXT : WIN_MULTREAD;
590 else if (cmd == READ)
591 return (lba48bit) ? WIN_READ_EXT : WIN_READ;
592 else if ((cmd == WRITE) && (drive->using_dma))
593 return (lba48bit) ? WIN_WRITEDMA_EXT : WIN_WRITEDMA;
594 else if ((cmd == WRITE) && (drive->mult_count))
595 return (lba48bit) ? WIN_MULTWRITE_EXT : WIN_MULTWRITE;
596 else if (cmd == WRITE)
597 return (lba48bit) ? WIN_WRITE_EXT : WIN_WRITE;
602 static ide_startstop_t chs_rw_disk (ide_drive_t *drive, struct request *rq, unsigned long block)
606 ata_nsector_t nsectors;
607 task_ioreg_t command = get_command(drive, rq_data_dir(rq));
608 unsigned int track = (block / drive->sect);
609 unsigned int sect = (block % drive->sect) + 1;
610 unsigned int head = (track % drive->head);
611 unsigned int cyl = (track / drive->head);
613 nsectors.all = (u16) rq->nr_sectors;
615 printk("%s: %sing: ", drive->name, (rq_data_dir(rq)==READ) ? "read" : "writ");
616 printk("CHS=%d/%d/%d, ", cyl, head, sect);
617 printk("sectors=%ld, ", rq->nr_sectors);
618 printk("buffer=0x%08lx\n", (unsigned long) rq->buffer);
621 memset(&args, 0, sizeof(ide_task_t));
623 sectors = (rq->nr_sectors == 256) ? 0x00 : rq->nr_sectors;
624 args.tfRegister[IDE_NSECTOR_OFFSET] = sectors;
625 args.tfRegister[IDE_SECTOR_OFFSET] = sect;
626 args.tfRegister[IDE_LCYL_OFFSET] = cyl;
627 args.tfRegister[IDE_HCYL_OFFSET] = (cyl>>8);
628 args.tfRegister[IDE_SELECT_OFFSET] = head;
629 args.tfRegister[IDE_SELECT_OFFSET] |= drive->select.all;
630 args.tfRegister[IDE_COMMAND_OFFSET] = command;
631 args.command_type = ide_cmd_type_parser(&args);
632 args.rq = (struct request *) rq;
633 rq->special = (ide_task_t *)&args;
634 return do_rw_taskfile(drive, &args);
637 static ide_startstop_t lba_28_rw_disk (ide_drive_t *drive, struct request *rq, unsigned long block)
641 ata_nsector_t nsectors;
642 task_ioreg_t command = get_command(drive, rq_data_dir(rq));
644 nsectors.all = (u16) rq->nr_sectors;
647 printk("%s: %sing: ", drive->name, (rq_data_dir(rq)==READ) ? "read" : "writ");
648 printk("LBAsect=%lld, ", block);
649 printk("sectors=%ld, ", rq->nr_sectors);
650 printk("buffer=0x%08lx\n", (unsigned long) rq->buffer);
653 memset(&args, 0, sizeof(ide_task_t));
655 sectors = (rq->nr_sectors == 256) ? 0x00 : rq->nr_sectors;
656 args.tfRegister[IDE_NSECTOR_OFFSET] = sectors;
657 args.tfRegister[IDE_SECTOR_OFFSET] = block;
658 args.tfRegister[IDE_LCYL_OFFSET] = (block>>=8);
659 args.tfRegister[IDE_HCYL_OFFSET] = (block>>=8);
660 args.tfRegister[IDE_SELECT_OFFSET] = ((block>>8)&0x0f);
661 args.tfRegister[IDE_SELECT_OFFSET] |= drive->select.all;
662 args.tfRegister[IDE_COMMAND_OFFSET] = command;
663 args.command_type = ide_cmd_type_parser(&args);
664 args.rq = (struct request *) rq;
665 rq->special = (ide_task_t *)&args;
666 return do_rw_taskfile(drive, &args);
670 * 268435455 == 137439 MB or 28bit limit
671 * 320173056 == 163929 MB or 48bit addressing
672 * 1073741822 == 549756 MB or 48bit addressing fake drive
675 static ide_startstop_t lba_48_rw_disk (ide_drive_t *drive, struct request *rq, unsigned long long block)
679 ata_nsector_t nsectors;
680 task_ioreg_t command = get_command(drive, rq_data_dir(rq));
682 nsectors.all = (u16) rq->nr_sectors;
685 printk("%s: %sing: ", drive->name, (rq_data_dir(rq)==READ) ? "read" : "writ");
686 printk("LBAsect=%lld, ", block);
687 printk("sectors=%ld, ", rq->nr_sectors);
688 printk("buffer=0x%08lx\n", (unsigned long) rq->buffer);
691 memset(&args, 0, sizeof(ide_task_t));
693 sectors = (rq->nr_sectors == 65536) ? 0 : rq->nr_sectors;
694 args.tfRegister[IDE_NSECTOR_OFFSET] = sectors;
695 args.tfRegister[IDE_SECTOR_OFFSET] = block; /* low lba */
696 args.tfRegister[IDE_LCYL_OFFSET] = (block>>=8); /* mid lba */
697 args.tfRegister[IDE_HCYL_OFFSET] = (block>>=8); /* hi lba */
698 args.tfRegister[IDE_SELECT_OFFSET] = drive->select.all;
699 args.tfRegister[IDE_COMMAND_OFFSET] = command;
700 args.hobRegister[IDE_NSECTOR_OFFSET_HOB]= sectors >> 8;
701 args.hobRegister[IDE_SECTOR_OFFSET_HOB] = (block>>=8); /* low lba */
702 args.hobRegister[IDE_LCYL_OFFSET_HOB] = (block>>=8); /* mid lba */
703 args.hobRegister[IDE_HCYL_OFFSET_HOB] = (block>>=8); /* hi lba */
704 args.hobRegister[IDE_SELECT_OFFSET_HOB] = drive->select.all;
705 args.hobRegister[IDE_CONTROL_OFFSET_HOB]= (drive->ctl|0x80);
706 args.command_type = ide_cmd_type_parser(&args);
707 args.rq = (struct request *) rq;
708 rq->special = (ide_task_t *)&args;
709 return do_rw_taskfile(drive, &args);
712 #endif /* CONFIG_IDE_TASKFILE_IO */
714 static int idedisk_open (struct inode *inode, struct file *filp, ide_drive_t *drive)
717 if (drive->removable && drive->usage == 1) {
719 memset(&args, 0, sizeof(ide_task_t));
720 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_DOORLOCK;
721 args.command_type = ide_cmd_type_parser(&args);
722 check_disk_change(inode->i_rdev);
724 * Ignore the return code from door_lock,
725 * since the open() has already succeeded,
726 * and the door_lock is irrelevant at this point.
728 if (drive->doorlocking && ide_raw_taskfile(drive, &args, NULL))
729 drive->doorlocking = 0;
734 static int do_idedisk_flushcache(ide_drive_t *drive);
736 static void idedisk_release (struct inode *inode, struct file *filp, ide_drive_t *drive)
738 if (drive->removable && !drive->usage) {
740 memset(&args, 0, sizeof(ide_task_t));
741 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_DOORUNLOCK;
742 args.command_type = ide_cmd_type_parser(&args);
743 invalidate_bdev(inode->i_bdev, 0);
744 if (drive->doorlocking && ide_raw_taskfile(drive, &args, NULL))
745 drive->doorlocking = 0;
747 if ((drive->id->cfs_enable_2 & 0x3000) && drive->wcache)
748 if (do_idedisk_flushcache(drive))
749 printk (KERN_INFO "%s: Write Cache FAILED Flushing!\n",
754 static int idedisk_media_change (ide_drive_t *drive)
756 /* if removable, always assume it was changed */
757 return drive->removable;
760 static void idedisk_revalidate (ide_drive_t *drive)
762 grok_partitions(HWIF(drive)->gd, drive->select.b.unit,
764 current_capacity(drive));
767 static int idedisk_end_request (ide_drive_t *drive, int uptodate)
773 spin_lock_irqsave(&io_request_lock, flags);
774 rq = HWGROUP(drive)->rq;
777 * decide whether to reenable DMA -- 3 is a random magic for now,
778 * if we DMA timeout more than 3 times, just stay in PIO
780 if (drive->state == DMA_PIO_RETRY && drive->retry_pio <= 3) {
782 HWGROUP(drive)->hwif->ide_dma_on(drive);
785 if (!end_that_request_first(rq, uptodate, drive->name)) {
786 add_blkdev_randomness(MAJOR(rq->rq_dev));
787 blkdev_dequeue_request(rq);
788 HWGROUP(drive)->rq = NULL;
789 end_that_request_last(rq);
793 spin_unlock_irqrestore(&io_request_lock, flags);
797 static u8 idedisk_dump_status (ide_drive_t *drive, const char *msg, u8 stat)
799 ide_hwif_t *hwif = HWIF(drive);
803 local_irq_set(flags);
804 printk("%s: %s: status=0x%02x", drive->name, msg, stat);
805 #if FANCY_STATUS_DUMPS
807 if (stat & BUSY_STAT)
810 if (stat & READY_STAT) printk("DriveReady ");
811 if (stat & WRERR_STAT) printk("DeviceFault ");
812 if (stat & SEEK_STAT) printk("SeekComplete ");
813 if (stat & DRQ_STAT) printk("DataRequest ");
814 if (stat & ECC_STAT) printk("CorrectedError ");
815 if (stat & INDEX_STAT) printk("Index ");
816 if (stat & ERR_STAT) printk("Error ");
819 #endif /* FANCY_STATUS_DUMPS */
821 if ((stat & (BUSY_STAT|ERR_STAT)) == ERR_STAT) {
822 err = hwif->INB(IDE_ERROR_REG);
823 printk("%s: %s: error=0x%02x", drive->name, msg, err);
824 #if FANCY_STATUS_DUMPS
826 if (err & ABRT_ERR) printk("DriveStatusError ");
828 printk("Bad%s ", (err & ABRT_ERR) ? "CRC" : "Sector");
829 if (err & ECC_ERR) printk("UncorrectableError ");
830 if (err & ID_ERR) printk("SectorIdNotFound ");
831 if (err & TRK0_ERR) printk("TrackZeroNotFound ");
832 if (err & MARK_ERR) printk("AddrMarkNotFound ");
834 if ((err & (BBD_ERR | ABRT_ERR)) == BBD_ERR ||
835 (err & (ECC_ERR|ID_ERR|MARK_ERR))) {
836 if (drive->addressing == 1) {
838 u32 low = 0, high = 0;
839 low = idedisk_read_24(drive);
840 hwif->OUTB(drive->ctl|0x80, IDE_CONTROL_REG);
841 high = idedisk_read_24(drive);
842 sectors = ((__u64)high << 24) | low;
843 printk(", LBAsect=%llu, high=%d, low=%d",
844 (unsigned long long) sectors,
847 u8 cur = hwif->INB(IDE_SELECT_REG);
848 if (cur & 0x40) { /* using LBA? */
849 printk(", LBAsect=%ld", (unsigned long)
851 |(hwif->INB(IDE_HCYL_REG)<<16)
852 |(hwif->INB(IDE_LCYL_REG)<<8)
853 | hwif->INB(IDE_SECTOR_REG));
855 printk(", CHS=%d/%d/%d",
856 (hwif->INB(IDE_HCYL_REG)<<8) +
857 hwif->INB(IDE_LCYL_REG),
859 hwif->INB(IDE_SECTOR_REG));
862 if (HWGROUP(drive) && HWGROUP(drive)->rq)
863 printk(", sector=%ld",
864 HWGROUP(drive)->rq->sector);
867 #endif /* FANCY_STATUS_DUMPS */
869 local_irq_restore(flags);
873 ide_startstop_t idedisk_error (ide_drive_t *drive, const char *msg, u8 stat)
878 int i = (drive->mult_count ? drive->mult_count : 1) * SECTOR_WORDS;
880 err = idedisk_dump_status(drive, msg, stat);
882 if (drive == NULL || (rq = HWGROUP(drive)->rq) == NULL)
886 /* retry only "normal" I/O: */
890 case IDE_DRIVE_TASKFILE:
892 ide_end_drive_cmd(drive, stat, err);
895 case IDE_DRIVE_TASKFILE:
897 ide_end_taskfile(drive, stat, err);
904 if (stat & BUSY_STAT || ((stat & WRERR_STAT) && !drive->nowerr)) {
905 /* other bits are useless when BUSY */
906 rq->errors |= ERROR_RESET;
907 } else if (stat & ERR_STAT) {
908 /* err has different meaning on cdrom and tape */
909 if (err == ABRT_ERR) {
910 if (drive->select.b.lba &&
911 /* some newer drives don't support WIN_SPECIFY */
912 hwif->INB(IDE_COMMAND_REG) == WIN_SPECIFY)
914 } else if ((err & BAD_CRC) == BAD_CRC) {
915 /* UDMA crc error, just retry the operation */
917 } else if (err & (BBD_ERR | ECC_ERR)) {
918 /* retries won't help these */
919 rq->errors = ERROR_MAX;
920 } else if (err & TRK0_ERR) {
921 /* help it find track zero */
922 rq->errors |= ERROR_RECAL;
925 if ((stat & DRQ_STAT) && rq_data_dir(rq) == READ) {
927 * try_to_flush_leftover_data() is invoked in response to
928 * a drive unexpectedly having its DRQ_STAT bit set. As
929 * an alternative to resetting the drive, this routine
930 * tries to clear the condition by read a sector's worth
931 * of data from the drive. Of course, this may not help
932 * if the drive is *waiting* for data from *us*.
936 unsigned int wcount = (i > 16) ? 16 : i;
938 taskfile_input_data(drive, buffer, wcount);
941 if (hwif->INB(IDE_STATUS_REG) & (BUSY_STAT|DRQ_STAT)) {
943 hwif->OUTB(WIN_IDLEIMMEDIATE,IDE_COMMAND_REG);
945 if (rq->errors >= ERROR_MAX)
946 DRIVER(drive)->end_request(drive, 0);
948 if ((rq->errors & ERROR_RESET) == ERROR_RESET) {
950 return ide_do_reset(drive);
952 if ((rq->errors & ERROR_RECAL) == ERROR_RECAL)
953 drive->special.b.recalibrate = 1;
960 * Queries for true maximum capacity of the drive.
961 * Returns maximum LBA address (> 0) of the drive, 0 if failed.
963 static unsigned long idedisk_read_native_max_address(ide_drive_t *drive)
966 unsigned long addr = 0;
968 /* Create IDE/ATA command request structure */
969 memset(&args, 0, sizeof(ide_task_t));
970 args.tfRegister[IDE_SELECT_OFFSET] = 0x40;
971 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_READ_NATIVE_MAX;
972 args.command_type = ide_cmd_type_parser(&args);
973 /* submit command request */
974 ide_raw_taskfile(drive, &args, NULL);
976 /* if OK, compute maximum address value */
977 if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
978 addr = ((args.tfRegister[IDE_SELECT_OFFSET] & 0x0f) << 24)
979 | ((args.tfRegister[ IDE_HCYL_OFFSET] ) << 16)
980 | ((args.tfRegister[ IDE_LCYL_OFFSET] ) << 8)
981 | ((args.tfRegister[IDE_SECTOR_OFFSET] ));
983 addr++; /* since the return value is (maxlba - 1), we add 1 */
987 static unsigned long long idedisk_read_native_max_address_ext(ide_drive_t *drive)
990 unsigned long long addr = 0;
992 /* Create IDE/ATA command request structure */
993 memset(&args, 0, sizeof(ide_task_t));
995 args.tfRegister[IDE_SELECT_OFFSET] = 0x40;
996 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_READ_NATIVE_MAX_EXT;
997 args.command_type = ide_cmd_type_parser(&args);
998 /* submit command request */
999 ide_raw_taskfile(drive, &args, NULL);
1001 /* if OK, compute maximum address value */
1002 if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
1003 u32 high = ((args.hobRegister[IDE_HCYL_OFFSET_HOB])<<16) |
1004 ((args.hobRegister[IDE_LCYL_OFFSET_HOB])<<8) |
1005 (args.hobRegister[IDE_SECTOR_OFFSET_HOB]);
1006 u32 low = ((args.tfRegister[IDE_HCYL_OFFSET])<<16) |
1007 ((args.tfRegister[IDE_LCYL_OFFSET])<<8) |
1008 (args.tfRegister[IDE_SECTOR_OFFSET]);
1009 addr = ((__u64)high << 24) | low;
1011 addr++; /* since the return value is (maxlba - 1), we add 1 */
1015 #ifdef CONFIG_IDEDISK_STROKE
1017 * Sets maximum virtual LBA address of the drive.
1018 * Returns new maximum virtual LBA address (> 0) or 0 on failure.
1020 static unsigned long idedisk_set_max_address(ide_drive_t *drive, unsigned long addr_req)
1023 unsigned long addr_set = 0;
1026 /* Create IDE/ATA command request structure */
1027 memset(&args, 0, sizeof(ide_task_t));
1028 args.tfRegister[IDE_SECTOR_OFFSET] = ((addr_req >> 0) & 0xff);
1029 args.tfRegister[IDE_LCYL_OFFSET] = ((addr_req >> 8) & 0xff);
1030 args.tfRegister[IDE_HCYL_OFFSET] = ((addr_req >> 16) & 0xff);
1031 args.tfRegister[IDE_SELECT_OFFSET] = ((addr_req >> 24) & 0x0f) | 0x40;
1032 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SET_MAX;
1033 args.command_type = ide_cmd_type_parser(&args);
1034 /* submit command request */
1035 ide_raw_taskfile(drive, &args, NULL);
1036 /* if OK, read new maximum address value */
1037 if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
1038 addr_set = ((args.tfRegister[IDE_SELECT_OFFSET] & 0x0f) << 24)
1039 | ((args.tfRegister[ IDE_HCYL_OFFSET] ) << 16)
1040 | ((args.tfRegister[ IDE_LCYL_OFFSET] ) << 8)
1041 | ((args.tfRegister[IDE_SECTOR_OFFSET] ));
1047 static unsigned long long idedisk_set_max_address_ext(ide_drive_t *drive, unsigned long long addr_req)
1050 unsigned long long addr_set = 0;
1053 /* Create IDE/ATA command request structure */
1054 memset(&args, 0, sizeof(ide_task_t));
1055 args.tfRegister[IDE_SECTOR_OFFSET] = ((addr_req >> 0) & 0xff);
1056 args.tfRegister[IDE_LCYL_OFFSET] = ((addr_req >>= 8) & 0xff);
1057 args.tfRegister[IDE_HCYL_OFFSET] = ((addr_req >>= 8) & 0xff);
1058 args.tfRegister[IDE_SELECT_OFFSET] = 0x40;
1059 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SET_MAX_EXT;
1060 args.hobRegister[IDE_SECTOR_OFFSET_HOB] = ((addr_req >>= 8) & 0xff);
1061 args.hobRegister[IDE_LCYL_OFFSET_HOB] = ((addr_req >>= 8) & 0xff);
1062 args.hobRegister[IDE_HCYL_OFFSET_HOB] = ((addr_req >>= 8) & 0xff);
1063 args.hobRegister[IDE_SELECT_OFFSET_HOB] = 0x40;
1064 args.hobRegister[IDE_CONTROL_OFFSET_HOB]= (drive->ctl|0x80);
1065 args.command_type = ide_cmd_type_parser(&args);
1066 /* submit command request */
1067 ide_raw_taskfile(drive, &args, NULL);
1068 /* if OK, compute maximum address value */
1069 if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
1070 u32 high = ((args.hobRegister[IDE_HCYL_OFFSET_HOB])<<16) |
1071 ((args.hobRegister[IDE_LCYL_OFFSET_HOB])<<8) |
1072 (args.hobRegister[IDE_SECTOR_OFFSET_HOB]);
1073 u32 low = ((args.tfRegister[IDE_HCYL_OFFSET])<<16) |
1074 ((args.tfRegister[IDE_LCYL_OFFSET])<<8) |
1075 (args.tfRegister[IDE_SECTOR_OFFSET]);
1076 addr_set = ((__u64)high << 24) | low;
1081 #endif /* CONFIG_IDEDISK_STROKE */
1084 * Tests if the drive supports Host Protected Area feature.
1085 * Returns true if supported, false otherwise.
1087 static inline int idedisk_supports_host_protected_area(ide_drive_t *drive)
1089 int flag = (drive->id->cfs_enable_1 & 0x0400) ? 1 : 0;
1091 printk("%s: host protected area => %d\n", drive->name, flag);
1096 * Compute drive->capacity, the full capacity of the drive
1097 * Called with drive->id != NULL.
1099 * To compute capacity, this uses either of
1101 * 1. CHS value set by user (whatever user sets will be trusted)
1102 * 2. LBA value from target drive (require new ATA feature)
1103 * 3. LBA value from system BIOS (new one is OK, old one may break)
1104 * 4. CHS value from system BIOS (traditional style)
1106 * in above order (i.e., if value of higher priority is available,
1107 * reset will be ignored).
1109 static void init_idedisk_capacity (ide_drive_t *drive)
1111 struct hd_driveid *id = drive->id;
1112 unsigned long capacity = drive->cyl * drive->head * drive->sect;
1113 unsigned long set_max = idedisk_read_native_max_address(drive);
1114 unsigned long long capacity_2 = capacity;
1115 unsigned long long set_max_ext;
1117 drive->capacity48 = 0;
1118 drive->select.b.lba = 0;
1120 (void) idedisk_supports_host_protected_area(drive);
1122 if (id->cfs_enable_2 & 0x0400) {
1123 capacity_2 = id->lba_capacity_2;
1124 drive->head = drive->bios_head = 255;
1125 drive->sect = drive->bios_sect = 63;
1126 drive->cyl = (unsigned int) capacity_2 / (drive->head * drive->sect);
1127 drive->select.b.lba = 1;
1128 set_max_ext = idedisk_read_native_max_address_ext(drive);
1129 if (set_max_ext > capacity_2) {
1130 #ifdef CONFIG_IDEDISK_STROKE
1131 set_max_ext = idedisk_read_native_max_address_ext(drive);
1132 set_max_ext = idedisk_set_max_address_ext(drive, set_max_ext);
1134 drive->capacity48 = capacity_2 = set_max_ext;
1135 drive->cyl = (unsigned int) set_max_ext / (drive->head * drive->sect);
1136 drive->select.b.lba = 1;
1137 drive->id->lba_capacity_2 = capacity_2;
1139 #else /* !CONFIG_IDEDISK_STROKE */
1140 printk(KERN_INFO "%s: setmax_ext LBA %llu, native %llu\n",
1141 drive->name, set_max_ext, capacity_2);
1142 #endif /* CONFIG_IDEDISK_STROKE */
1144 drive->cyl = (unsigned int) capacity_2 / (drive->head * drive->sect);
1145 drive->bios_cyl = drive->cyl;
1146 drive->capacity48 = capacity_2;
1147 drive->capacity = (unsigned long) capacity_2;
1149 /* Determine capacity, and use LBA if the drive properly supports it */
1150 } else if ((id->capability & 2) && lba_capacity_is_ok(id)) {
1151 capacity = id->lba_capacity;
1152 drive->cyl = capacity / (drive->head * drive->sect);
1153 drive->select.b.lba = 1;
1156 if (set_max > capacity) {
1157 #ifdef CONFIG_IDEDISK_STROKE
1158 set_max = idedisk_read_native_max_address(drive);
1159 set_max = idedisk_set_max_address(drive, set_max);
1161 drive->capacity = capacity = set_max;
1162 drive->cyl = set_max / (drive->head * drive->sect);
1163 drive->select.b.lba = 1;
1164 drive->id->lba_capacity = capacity;
1166 #else /* !CONFIG_IDEDISK_STROKE */
1167 printk(KERN_INFO "%s: setmax LBA %lu, native %lu\n",
1168 drive->name, set_max, capacity);
1169 #endif /* CONFIG_IDEDISK_STROKE */
1172 drive->capacity = capacity;
1174 if ((id->command_set_2 & 0x0400) && (id->cfs_enable_2 & 0x0400)) {
1175 drive->capacity48 = id->lba_capacity_2;
1178 drive->cyl = (unsigned long)(drive->capacity48) / (drive->head * drive->sect);
1182 static unsigned long idedisk_capacity (ide_drive_t *drive)
1184 if (drive->id->cfs_enable_2 & 0x0400)
1185 return (drive->capacity48 - drive->sect0);
1186 return (drive->capacity - drive->sect0);
1189 static ide_startstop_t idedisk_special (ide_drive_t *drive)
1191 special_t *s = &drive->special;
1193 if (s->b.set_geometry) {
1194 s->b.set_geometry = 0;
1195 if (!IS_PDC4030_DRIVE) {
1197 memset(&args, 0, sizeof(ide_task_t));
1198 args.tfRegister[IDE_NSECTOR_OFFSET] = drive->sect;
1199 args.tfRegister[IDE_SECTOR_OFFSET] = drive->sect;
1200 args.tfRegister[IDE_LCYL_OFFSET] = drive->cyl;
1201 args.tfRegister[IDE_HCYL_OFFSET] = drive->cyl>>8;
1202 args.tfRegister[IDE_SELECT_OFFSET] = ((drive->head-1)|drive->select.all)&0xBF;
1203 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SPECIFY;
1204 args.command_type = ide_cmd_type_parser(&args);
1205 do_rw_taskfile(drive, &args);
1207 } else if (s->b.recalibrate) {
1208 s->b.recalibrate = 0;
1209 if (!IS_PDC4030_DRIVE) {
1211 memset(&args, 0, sizeof(ide_task_t));
1212 args.tfRegister[IDE_NSECTOR_OFFSET] = drive->sect;
1213 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_RESTORE;
1214 args.command_type = ide_cmd_type_parser(&args);
1215 do_rw_taskfile(drive, &args);
1217 } else if (s->b.set_multmode) {
1218 s->b.set_multmode = 0;
1219 if (drive->id && drive->mult_req > drive->id->max_multsect)
1220 drive->mult_req = drive->id->max_multsect;
1221 if (!IS_PDC4030_DRIVE) {
1223 memset(&args, 0, sizeof(ide_task_t));
1224 args.tfRegister[IDE_NSECTOR_OFFSET] = drive->mult_req;
1225 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SETMULT;
1226 args.command_type = ide_cmd_type_parser(&args);
1227 do_rw_taskfile(drive, &args);
1229 } else if (s->all) {
1230 int special = s->all;
1232 printk(KERN_ERR "%s: bad special flag: 0x%02x\n", drive->name, special);
1235 return IS_PDC4030_DRIVE ? ide_stopped : ide_started;
1238 static void idedisk_pre_reset (ide_drive_t *drive)
1240 int legacy = (drive->id->cfs_enable_2 & 0x0400) ? 0 : 1;
1242 drive->special.all = 0;
1243 drive->special.b.set_geometry = legacy;
1244 drive->special.b.recalibrate = legacy;
1245 if (OK_TO_RESET_CONTROLLER)
1246 drive->mult_count = 0;
1247 if (!drive->keep_settings && !drive->using_dma)
1248 drive->mult_req = 0;
1249 if (drive->mult_req != drive->mult_count)
1250 drive->special.b.set_multmode = 1;
1253 #ifdef CONFIG_PROC_FS
1255 static int smart_enable(ide_drive_t *drive)
1259 memset(&args, 0, sizeof(ide_task_t));
1260 args.tfRegister[IDE_FEATURE_OFFSET] = SMART_ENABLE;
1261 args.tfRegister[IDE_LCYL_OFFSET] = SMART_LCYL_PASS;
1262 args.tfRegister[IDE_HCYL_OFFSET] = SMART_HCYL_PASS;
1263 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SMART;
1264 args.command_type = ide_cmd_type_parser(&args);
1265 return ide_raw_taskfile(drive, &args, NULL);
1268 static int get_smart_values(ide_drive_t *drive, u8 *buf)
1272 memset(&args, 0, sizeof(ide_task_t));
1273 args.tfRegister[IDE_FEATURE_OFFSET] = SMART_READ_VALUES;
1274 args.tfRegister[IDE_NSECTOR_OFFSET] = 0x01;
1275 args.tfRegister[IDE_LCYL_OFFSET] = SMART_LCYL_PASS;
1276 args.tfRegister[IDE_HCYL_OFFSET] = SMART_HCYL_PASS;
1277 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SMART;
1278 args.command_type = ide_cmd_type_parser(&args);
1279 (void) smart_enable(drive);
1280 return ide_raw_taskfile(drive, &args, buf);
1283 static int get_smart_thresholds(ide_drive_t *drive, u8 *buf)
1286 memset(&args, 0, sizeof(ide_task_t));
1287 args.tfRegister[IDE_FEATURE_OFFSET] = SMART_READ_THRESHOLDS;
1288 args.tfRegister[IDE_NSECTOR_OFFSET] = 0x01;
1289 args.tfRegister[IDE_LCYL_OFFSET] = SMART_LCYL_PASS;
1290 args.tfRegister[IDE_HCYL_OFFSET] = SMART_HCYL_PASS;
1291 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SMART;
1292 args.command_type = ide_cmd_type_parser(&args);
1293 (void) smart_enable(drive);
1294 return ide_raw_taskfile(drive, &args, buf);
1297 static int proc_idedisk_read_cache
1298 (char *page, char **start, off_t off, int count, int *eof, void *data)
1300 ide_drive_t *drive = (ide_drive_t *) data;
1305 len = sprintf(out,"%i\n", drive->id->buf_size / 2);
1307 len = sprintf(out,"(none)\n");
1308 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
1311 static int proc_idedisk_read_smart_thresholds
1312 (char *page, char **start, off_t off, int count, int *eof, void *data)
1314 ide_drive_t *drive = (ide_drive_t *)data;
1317 if (!get_smart_thresholds(drive, page)) {
1318 unsigned short *val = (unsigned short *) page;
1319 char *out = ((char *)val) + (SECTOR_WORDS * 4);
1322 out += sprintf(out, "%04x%c", le16_to_cpu(*val), (++i & 7) ? ' ' : '\n');
1324 } while (i < (SECTOR_WORDS * 2));
1327 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
1330 static int proc_idedisk_read_smart_values
1331 (char *page, char **start, off_t off, int count, int *eof, void *data)
1333 ide_drive_t *drive = (ide_drive_t *)data;
1336 if (!get_smart_values(drive, page)) {
1337 unsigned short *val = (unsigned short *) page;
1338 char *out = ((char *)val) + (SECTOR_WORDS * 4);
1341 out += sprintf(out, "%04x%c", le16_to_cpu(*val), (++i & 7) ? ' ' : '\n');
1343 } while (i < (SECTOR_WORDS * 2));
1346 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
1349 static ide_proc_entry_t idedisk_proc[] = {
1350 { "cache", S_IFREG|S_IRUGO, proc_idedisk_read_cache, NULL },
1351 { "geometry", S_IFREG|S_IRUGO, proc_ide_read_geometry, NULL },
1352 { "smart_values", S_IFREG|S_IRUSR, proc_idedisk_read_smart_values, NULL },
1353 { "smart_thresholds", S_IFREG|S_IRUSR, proc_idedisk_read_smart_thresholds, NULL },
1354 { NULL, 0, NULL, NULL }
1359 #define idedisk_proc NULL
1361 #endif /* CONFIG_PROC_FS */
1364 * This is tightly woven into the driver->do_special can not touch.
1365 * DON'T do it again until a total personality rewrite is committed.
1367 static int set_multcount(ide_drive_t *drive, int arg)
1371 if (drive->special.b.set_multmode)
1373 ide_init_drive_cmd (&rq);
1374 rq.cmd = IDE_DRIVE_CMD;
1375 drive->mult_req = arg;
1376 drive->special.b.set_multmode = 1;
1377 (void) ide_do_drive_cmd (drive, &rq, ide_wait);
1378 return (drive->mult_count == arg) ? 0 : -EIO;
1381 static int set_nowerr(ide_drive_t *drive, int arg)
1383 if (ide_spin_wait_hwgroup(drive))
1385 drive->nowerr = arg;
1386 drive->bad_wstat = arg ? BAD_R_STAT : BAD_W_STAT;
1387 spin_unlock_irq(&io_request_lock);
1391 static int write_cache (ide_drive_t *drive, int arg)
1395 if (!(drive->id->cfs_enable_2 & 0x3000))
1398 memset(&args, 0, sizeof(ide_task_t));
1399 args.tfRegister[IDE_FEATURE_OFFSET] = (arg) ?
1400 SETFEATURES_EN_WCACHE : SETFEATURES_DIS_WCACHE;
1401 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SETFEATURES;
1402 args.command_type = ide_cmd_type_parser(&args);
1403 (void) ide_raw_taskfile(drive, &args, NULL);
1405 drive->wcache = arg;
1409 static int call_idedisk_standby (ide_drive_t *drive, int arg)
1412 u8 standby = (arg) ? WIN_STANDBYNOW2 : WIN_STANDBYNOW1;
1413 memset(&args, 0, sizeof(ide_task_t));
1414 args.tfRegister[IDE_COMMAND_OFFSET] = standby;
1415 args.command_type = ide_cmd_type_parser(&args);
1416 return ide_raw_taskfile(drive, &args, NULL);
1419 static int do_idedisk_standby (ide_drive_t *drive)
1421 return call_idedisk_standby(drive, 0);
1424 static int call_idedisk_suspend (ide_drive_t *drive, int arg)
1427 u8 suspend = (arg) ? WIN_SLEEPNOW2 : WIN_SLEEPNOW1;
1428 memset(&args, 0, sizeof(ide_task_t));
1429 args.tfRegister[IDE_COMMAND_OFFSET] = suspend;
1430 args.command_type = ide_cmd_type_parser(&args);
1431 return ide_raw_taskfile(drive, &args, NULL);
1434 static int do_idedisk_suspend (ide_drive_t *drive)
1436 if (drive->suspend_reset)
1439 return call_idedisk_suspend(drive, 0);
1443 static int call_idedisk_checkpower (ide_drive_t *drive, int arg)
1446 u8 ckpw = (arg) ? WIN_CHECKPOWERMODE2 : WIN_CHECKPOWERMODE1;
1447 memset(&args, 0, sizeof(ide_task_t));
1448 args.tfRegister[IDE_COMMAND_OFFSET] = ckpw;
1449 args.command_type = ide_cmd_type_parser(&args);
1450 ide_raw_taskfile(drive, &args, NULL);
1452 if (errno != EIO || args[0] != 0 || args[1] != 0)
1457 state = (args[2] == 255) ? "active/idle" : "standby";
1462 static int do_idedisk_checkpower (ide_drive_t *drive)
1464 return call_idedisk_checkpower(drive, 0);
1468 static int do_idedisk_resume (ide_drive_t *drive)
1470 if (!drive->suspend_reset)
1475 static int do_idedisk_flushcache (ide_drive_t *drive)
1479 memset(&args, 0, sizeof(ide_task_t));
1480 if (drive->id->cfs_enable_2 & 0x2400)
1481 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE_EXT;
1483 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE;
1484 args.command_type = ide_cmd_type_parser(&args);
1485 return ide_raw_taskfile(drive, &args, NULL);
1488 static int set_acoustic (ide_drive_t *drive, int arg)
1492 memset(&args, 0, sizeof(ide_task_t));
1493 args.tfRegister[IDE_FEATURE_OFFSET] = (arg) ? SETFEATURES_EN_AAM :
1494 SETFEATURES_DIS_AAM;
1495 args.tfRegister[IDE_NSECTOR_OFFSET] = arg;
1496 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SETFEATURES;
1497 args.command_type = ide_cmd_type_parser(&args);
1498 ide_raw_taskfile(drive, &args, NULL);
1499 drive->acoustic = arg;
1503 static int probe_lba_addressing (ide_drive_t *drive, int arg)
1505 drive->addressing = 0;
1507 if (HWIF(drive)->addressing)
1510 if (!(drive->id->cfs_enable_2 & 0x0400))
1512 drive->addressing = arg;
1516 static int set_lba_addressing (ide_drive_t *drive, int arg)
1518 return (probe_lba_addressing(drive, arg));
1521 static void idedisk_add_settings(ide_drive_t *drive)
1523 struct hd_driveid *id = drive->id;
1524 int major = HWIF(drive)->major;
1525 int minor = drive->select.b.unit << PARTN_BITS;
1527 ide_add_setting(drive, "bios_cyl", SETTING_RW, -1, -1, TYPE_INT, 0, 65535, 1, 1, &drive->bios_cyl, NULL);
1528 ide_add_setting(drive, "bios_head", SETTING_RW, -1, -1, TYPE_BYTE, 0, 255, 1, 1, &drive->bios_head, NULL);
1529 ide_add_setting(drive, "bios_sect", SETTING_RW, -1, -1, TYPE_BYTE, 0, 63, 1, 1, &drive->bios_sect, NULL);
1530 ide_add_setting(drive, "address", SETTING_RW, HDIO_GET_ADDRESS, HDIO_SET_ADDRESS, TYPE_INTA, 0, 2, 1, 1, &drive->addressing, set_lba_addressing);
1531 ide_add_setting(drive, "bswap", SETTING_READ, -1, -1, TYPE_BYTE, 0, 1, 1, 1, &drive->bswap, NULL);
1532 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);
1533 ide_add_setting(drive, "nowerr", SETTING_RW, HDIO_GET_NOWERR, HDIO_SET_NOWERR, TYPE_BYTE, 0, 1, 1, 1, &drive->nowerr, set_nowerr);
1534 ide_add_setting(drive, "breada_readahead", SETTING_RW, BLKRAGET, BLKRASET, TYPE_INT, 0, 255, 1, 1, &read_ahead[major], NULL);
1535 ide_add_setting(drive, "file_readahead", SETTING_RW, BLKFRAGET, BLKFRASET, TYPE_INTA, 0, 4096, PAGE_SIZE, 1024, &max_readahead[major][minor], NULL);
1536 ide_add_setting(drive, "max_kb_per_request", SETTING_RW, BLKSECTGET, BLKSECTSET, TYPE_INTA, 1, 255, 1, 1, &max_sectors[major][minor], NULL);
1537 ide_add_setting(drive, "lun", SETTING_RW, -1, -1, TYPE_INT, 0, 7, 1, 1, &drive->lun, NULL);
1538 ide_add_setting(drive, "wcache", SETTING_RW, HDIO_GET_WCACHE, HDIO_SET_WCACHE, TYPE_BYTE, 0, 1, 1, 1, &drive->wcache, write_cache);
1539 ide_add_setting(drive, "acoustic", SETTING_RW, HDIO_GET_ACOUSTIC, HDIO_SET_ACOUSTIC, TYPE_BYTE, 0, 254, 1, 1, &drive->acoustic, set_acoustic);
1540 ide_add_setting(drive, "failures", SETTING_RW, -1, -1, TYPE_INT, 0, 65535, 1, 1, &drive->failures, NULL);
1541 ide_add_setting(drive, "max_failures", SETTING_RW, -1, -1, TYPE_INT, 0, 65535, 1, 1, &drive->max_failures, NULL);
1544 static int idedisk_ioctl (ide_drive_t *drive, struct inode *inode,
1545 struct file *file, unsigned int cmd, unsigned long arg)
1562 static void idedisk_setup (ide_drive_t *drive)
1566 struct hd_driveid *id = drive->id;
1567 unsigned long capacity;
1570 if (IS_PDC4030_DRIVE)
1571 DRIVER(drive)->do_request = promise_rw_disk;
1574 idedisk_add_settings(drive);
1580 * CompactFlash cards and their brethern look just like hard drives
1581 * to us, but they are removable and don't have a doorlock mechanism.
1583 if (drive->removable && !(drive->is_flash)) {
1585 * Removable disks (eg. SYQUEST); ignore 'WD' drives
1587 if (id->model[0] != 'W' || id->model[1] != 'D') {
1588 drive->doorlocking = 1;
1591 for (i = 0; i < MAX_DRIVES; ++i) {
1592 ide_hwif_t *hwif = HWIF(drive);
1594 if (drive != &hwif->drives[i]) continue;
1595 hwif->gd->de_arr[i] = drive->de;
1596 if (drive->removable)
1597 hwif->gd->flags[i] |= GENHD_FL_REMOVABLE;
1602 (void) probe_lba_addressing(drive, 1);
1604 /* if using 48-bit addressing bump the request size up */
1605 if (probe_lba_addressing(drive, 1))
1606 blk_queue_max_sectors(&drive->queue, 2048);
1609 /* Extract geometry if we did not already have one for the drive */
1610 if (!drive->cyl || !drive->head || !drive->sect) {
1611 drive->cyl = drive->bios_cyl = id->cyls;
1612 drive->head = drive->bios_head = id->heads;
1613 drive->sect = drive->bios_sect = id->sectors;
1616 /* Handle logical geometry translation by the drive */
1617 if ((id->field_valid & 1) && id->cur_cyls &&
1618 id->cur_heads && (id->cur_heads <= 16) && id->cur_sectors) {
1619 drive->cyl = id->cur_cyls;
1620 drive->head = id->cur_heads;
1621 drive->sect = id->cur_sectors;
1624 /* Use physical geometry if what we have still makes no sense */
1625 if (drive->head > 16 && id->heads && id->heads <= 16) {
1626 drive->cyl = id->cyls;
1627 drive->head = id->heads;
1628 drive->sect = id->sectors;
1631 /* calculate drive capacity, and select LBA if possible */
1632 init_idedisk_capacity (drive);
1635 * if possible, give fdisk access to more of the drive,
1636 * by correcting bios_cyls:
1638 capacity = idedisk_capacity (drive);
1639 if ((capacity >= (drive->bios_cyl * drive->bios_sect * drive->bios_head)) &&
1640 (!drive->forced_geom) && drive->bios_sect && drive->bios_head)
1641 drive->bios_cyl = (capacity / drive->bios_sect) / drive->bios_head;
1642 printk (KERN_INFO "%s: %ld sectors", drive->name, capacity);
1644 /* Give size in megabytes (MB), not mebibytes (MiB). */
1645 /* We compute the exact rounded value, avoiding overflow. */
1646 printk (" (%ld MB)", (capacity - capacity/625 + 974)/1950);
1648 /* Only print cache size when it was specified */
1650 printk (" w/%dKiB Cache", id->buf_size/2);
1652 printk(", CHS=%d/%d/%d",
1653 drive->bios_cyl, drive->bios_head, drive->bios_sect);
1654 #ifdef CONFIG_BLK_DEV_IDEDMA
1655 if (drive->using_dma)
1656 (void) HWIF(drive)->ide_dma_verbose(drive);
1657 #endif /* CONFIG_BLK_DEV_IDEDMA */
1660 drive->mult_count = 0;
1661 if (id->max_multsect) {
1662 #ifdef CONFIG_IDEDISK_MULTI_MODE
1663 id->multsect = ((id->max_multsect/2) > 1) ? id->max_multsect : 0;
1664 id->multsect_valid = id->multsect ? 1 : 0;
1665 drive->mult_req = id->multsect_valid ? id->max_multsect : INITIAL_MULT_COUNT;
1666 drive->special.b.set_multmode = drive->mult_req ? 1 : 0;
1667 #else /* original, pre IDE-NFG, per request of AC */
1668 drive->mult_req = INITIAL_MULT_COUNT;
1669 if (drive->mult_req > id->max_multsect)
1670 drive->mult_req = id->max_multsect;
1671 if (drive->mult_req || ((id->multsect_valid & 1) && id->multsect))
1672 drive->special.b.set_multmode = 1;
1673 #endif /* CONFIG_IDEDISK_MULTI_MODE */
1675 drive->no_io_32bit = id->dword_io ? 1 : 0;
1676 if (drive->id->cfs_enable_2 & 0x3000)
1677 write_cache(drive, (id->cfs_enable_2 & 0x3000));
1680 static int idedisk_cleanup(ide_drive_t *drive)
1682 if ((drive->id->cfs_enable_2 & 0x3000) && drive->wcache)
1683 if (do_idedisk_flushcache(drive))
1684 printk (KERN_INFO "%s: Write Cache FAILED Flushing!\n",
1686 return ide_unregister_subdriver(drive);
1689 int idedisk_init (void);
1690 int idedisk_attach(ide_drive_t *drive);
1693 * IDE subdriver functions, registered with ide.c
1695 static ide_driver_t idedisk_driver = {
1697 version: IDEDISK_VERSION,
1701 supports_dsc_overlap: 0,
1702 cleanup: idedisk_cleanup,
1703 standby: do_idedisk_standby,
1704 suspend: do_idedisk_suspend,
1705 resume: do_idedisk_resume,
1706 flushcache: do_idedisk_flushcache,
1707 do_request: do_rw_disk,
1708 end_request: idedisk_end_request,
1709 sense: idedisk_dump_status,
1710 error: idedisk_error,
1711 ioctl: idedisk_ioctl,
1713 release: idedisk_release,
1714 media_change: idedisk_media_change,
1715 revalidate: idedisk_revalidate,
1716 pre_reset: idedisk_pre_reset,
1717 capacity: idedisk_capacity,
1718 special: idedisk_special,
1721 attach: idedisk_attach,
1722 ata_prebuilder: NULL,
1723 atapi_prebuilder: NULL,
1726 static ide_module_t idedisk_module = {
1733 MODULE_DESCRIPTION("ATA DISK Driver");
1735 int idedisk_attach (ide_drive_t *drive)
1740 if (ide_register_subdriver(drive,
1741 &idedisk_driver, IDE_SUBDRIVER_VERSION)) {
1742 printk(KERN_ERR "ide-disk: %s: Failed to register the "
1743 "driver with ide.c\n", drive->name);
1747 DRIVER(drive)->busy++;
1748 idedisk_setup(drive);
1749 if ((!drive->head || drive->head > 16) && !drive->select.b.lba) {
1750 printk(KERN_ERR "%s: INVALID GEOMETRY: %d PHYSICAL HEADS?\n",
1751 drive->name, drive->head);
1752 (void) idedisk_cleanup(drive);
1755 DRIVER(drive)->busy--;
1761 static void __exit idedisk_exit (void)
1766 while ((drive = ide_scan_devices(ide_disk, idedisk_driver.name,
1767 &idedisk_driver, failed)) != NULL) {
1768 if (idedisk_cleanup (drive)) {
1769 printk(KERN_ERR "%s: cleanup_module() called while "
1770 "still busy\n", drive->name);
1773 #ifdef CONFIG_PROC_FS
1774 /* We must remove proc entries defined in this module.
1775 * Otherwise we oops while accessing these entries
1778 ide_remove_proc_entries(drive->proc, idedisk_proc);
1781 ide_unregister_module(&idedisk_module);
1784 int idedisk_init (void)
1786 #ifdef CLASSIC_BUILTINS_METHOD
1789 #endif /* CLASSIC_BUILTINS_METHOD */
1793 #ifdef CLASSIC_BUILTINS_METHOD
1794 while ((drive = ide_scan_devices(ide_disk,
1795 idedisk_driver.name, NULL, failed++)) != NULL) {
1796 if (ide_register_subdriver(drive,
1797 &idedisk_driver, IDE_SUBDRIVER_VERSION)) {
1798 printk(KERN_ERR "ide-disk: %s: Failed to register "
1799 "the driver with ide.c\n", drive->name);
1802 DRIVER(drive)->busy++;
1803 idedisk_setup(drive);
1804 if ((!drive->head || drive->head > 16) &&
1805 (!drive->select.b.lba)) {
1806 printk(KERN_ERR "%s: INVALID GEOMETRY: %d "
1807 "PHYSICAL HEADS?\n", drive->name, drive->head);
1808 (void) idedisk_cleanup(drive);
1809 DRIVER(drive)->busy--;
1812 DRIVER(drive)->busy--;
1815 #endif /* CLASSIC_BUILTINS_METHOD */
1817 ide_register_module(&idedisk_module);
1822 ide_startstop_t panic_box(ide_drive_t *drive)
1825 panic("%s: Attempted to corrupt something: ide operation "
1827 printk(KERN_ERR "%s: Attempted to corrupt something: ide operation "
1829 "was pending accross suspend/resume.\n", drive->name);
1833 int ide_disks_busy(void)
1836 for (i=0; i<MAX_HWIFS; i++) {
1837 struct hwgroup_s *hwgroup = ide_hwifs[i].hwgroup;
1838 if (!hwgroup) continue;
1839 if ((hwgroup->handler) && (hwgroup->handler != panic_box))
1845 void ide_disk_suspend(void)
1848 while (ide_disks_busy()) {
1852 for (i=0; i<MAX_HWIFS; i++) {
1853 struct hwgroup_s *hwgroup = ide_hwifs[i].hwgroup;
1855 if (!hwgroup) continue;
1856 hwgroup->handler_save = hwgroup->handler;
1857 hwgroup->handler = panic_box;
1860 if (ide_disks_busy())
1861 panic("How did you get that request through?!");
1864 /* unsuspend and resume should be equal in the ideal world */
1866 void ide_disk_unsuspend(void)
1869 for (i=0; i<MAX_HWIFS; i++) {
1870 struct hwgroup_s *hwgroup = ide_hwifs[i].hwgroup;
1872 if (!hwgroup) continue;
1873 hwgroup->handler = NULL; /* hwgroup->handler_save; */
1874 hwgroup->handler_save = NULL;
1879 void ide_disk_resume(void)
1882 for (i=0; i<MAX_HWIFS; i++) {
1883 struct hwgroup_s *hwgroup = ide_hwifs[i].hwgroup;
1885 if (!hwgroup) continue;
1886 if (hwgroup->handler != panic_box)
1887 panic("Handler was not set to panic?");
1888 hwgroup->handler_save = NULL;
1889 hwgroup->handler = NULL;
1894 module_init(idedisk_init);
1895 module_exit(idedisk_exit);
1896 MODULE_LICENSE("GPL");