linux-old/drivers/ide/ide-disk.c
<<
>>
Prefs
   1/*
   2 *  linux/drivers/ide/ide-disk.c        Version 1.18    Mar 05, 2003
   3 *
   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>
   8 *
   9 *
  10 */
  11
  12/*
  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>
  16 *
  17 * This is the IDE/ATA disk driver, as evolved from hd.c and ide.c.
  18 *
  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
  31 *                      the entire disk.
  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
  43 *                      fix wcache setup.
  44 */
  45
  46#define IDEDISK_VERSION "1.17"
  47
  48#undef REALLY_SLOW_IO           /* most systems can safely undef this */
  49
  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>
  56#include <linux/mm.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>
  63
  64#define _IDE_DISK
  65
  66#include <linux/ide.h>
  67
  68#include <asm/byteorder.h>
  69#include <asm/irq.h>
  70#include <asm/uaccess.h>
  71#include <asm/io.h>
  72
  73/* FIXME: some day we shouldnt need to look in here! */
  74
  75#include "legacy/pdc4030.h"
  76
  77static int driver_blocked;
  78
  79static inline u32 idedisk_read_24 (ide_drive_t *drive)
  80{
  81#if 0
  82        return  (HWIF(drive)->INB(IDE_HCYL_REG)<<16) |
  83                (HWIF(drive)->INB(IDE_LCYL_REG)<<8) |
  84                 HWIF(drive)->INB(IDE_SECTOR_REG);
  85#else
  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;
  90#endif
  91}
  92
  93static int idedisk_end_request(ide_drive_t *drive, int uptodate);
  94
  95/*
  96 * lba_capacity_is_ok() performs a sanity check on the claimed "lba_capacity"
  97 * value for this drive (from its reported identification information).
  98 *
  99 * Returns:     1 if lba_capacity looks sensible
 100 *              0 otherwise
 101 *
 102 * It is called only once for each drive.
 103 */
 104static int lba_capacity_is_ok (struct hd_driveid *id)
 105{
 106        unsigned long lba_sects, chs_sects, head, tail;
 107
 108        if ((id->command_set_2 & 0x0400) && (id->cfs_enable_2 & 0x0400)) {
 109                printk("48-bit Drive: %llu \n", id->lba_capacity_2);
 110                return 1;
 111        }
 112
 113        /*
 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.
 118         */
 119        if ((id->cyls == 16383
 120             || (id->cyls == 4092 && id->cur_cyls == 16383)) &&
 121            id->sectors == 63 &&
 122            (id->heads == 15 || id->heads == 16) &&
 123            (id->lba_capacity >= 16383*63*id->heads))
 124                return 1;
 125
 126        lba_sects   = id->lba_capacity;
 127        chs_sects   = id->cyls * id->heads * id->sectors;
 128
 129        /* perform a rough sanity check on lba_sects:  within 10% is OK */
 130        if ((lba_sects - chs_sects) < chs_sects/10)
 131                return 1;
 132
 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 */
 140        }
 141
 142        return 0;       /* lba_capacity value may be bad */
 143}
 144
 145#ifndef CONFIG_IDE_TASKFILE_IO
 146
 147/*
 148 * read_intr() is the handler for disk read/multread interrupts
 149 */
 150static ide_startstop_t read_intr (ide_drive_t *drive)
 151{
 152        ide_hwif_t *hwif        = HWIF(drive);
 153        u32 i = 0, nsect        = 0, msect = drive->mult_count;
 154        struct request *rq;
 155        unsigned long flags;
 156        u8 stat;
 157        char *to;
 158
 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);
 163                }
 164                /* no data yet, so wait for another interrupt */
 165                ide_set_handler(drive, &read_intr, WAIT_CMD, NULL);
 166                return ide_started;
 167        }
 168        
 169read_next:
 170        rq = HWGROUP(drive)->rq;
 171        if (msect) {
 172                if ((nsect = rq->current_nr_sectors) > msect)
 173                        nsect = msect;
 174                msect -= nsect;
 175        } else
 176                nsect = 1;
 177        to = ide_map_buffer(rq, &flags);
 178        taskfile_input_data(drive, to, nsect * SECTOR_WORDS);
 179
 180#ifdef DEBUG
 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);
 184#endif
 185
 186        ide_unmap_buffer(to, &flags);
 187        rq->sector += nsect;
 188        rq->errors = 0;
 189        i = (rq->nr_sectors -= nsect);
 190        if (((long)(rq->current_nr_sectors -= nsect)) <= 0)
 191                idedisk_end_request(drive, 1);
 192        /*
 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 ...
 196         */
 197        if (i > 0) {
 198                if (msect)
 199                        goto read_next;
 200                ide_set_handler(drive, &read_intr, WAIT_CMD, NULL);
 201                return ide_started;
 202        }
 203        return ide_stopped;
 204}
 205
 206/*
 207 * write_intr() is the handler for disk write interrupts
 208 */
 209static ide_startstop_t write_intr (ide_drive_t *drive)
 210{
 211        ide_hwgroup_t *hwgroup  = HWGROUP(drive);
 212        ide_hwif_t *hwif        = HWIF(drive);
 213        struct request *rq      = hwgroup->rq;
 214        u32 i = 0;
 215        u8 stat;
 216
 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);
 221        } else {
 222#ifdef DEBUG
 223                printk("%s: write: sector %ld, buffer=0x%08lx, remaining=%ld\n",
 224                        drive->name, rq->sector, (unsigned long) rq->buffer,
 225                        rq->nr_sectors-1);
 226#endif
 227                if ((rq->nr_sectors == 1) ^ ((stat & DRQ_STAT) != 0)) {
 228                        rq->sector++;
 229                        rq->errors = 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);
 234                        if (i > 0) {
 235                                unsigned long flags;
 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);
 240                                return ide_started;
 241                        }
 242                        return ide_stopped;
 243                }
 244                /* the original code did this here (?) */
 245                return ide_stopped;
 246        }
 247        return DRIVER(drive)->error(drive, "write_intr", stat);
 248}
 249
 250/*
 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.
 253 *
 254 * Returns 0 on success.
 255 *
 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!
 260 *
 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.
 265 */
 266int ide_multwrite (ide_drive_t *drive, unsigned int mcount)
 267{
 268        ide_hwgroup_t *hwgroup  = HWGROUP(drive);
 269        struct request *rq      = &hwgroup->wrq;
 270 
 271        do {
 272                char *buffer;
 273                int nsect = rq->current_nr_sectors;
 274                unsigned long flags;
 275 
 276                if (nsect > mcount)
 277                        nsect = mcount;
 278                mcount -= nsect;
 279                buffer = ide_map_buffer(rq, &flags);
 280
 281                rq->sector += nsect;
 282                rq->nr_sectors -= nsect;
 283                rq->current_nr_sectors -= nsect;
 284
 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;
 288
 289                        /* end early early we ran out of requests */
 290                        if (!bh) {
 291                                mcount = 0;
 292                        } else {
 293                                rq->bh = bh;
 294                                rq->current_nr_sectors = bh->b_size >> 9;
 295                                rq->hard_cur_sectors = rq->current_nr_sectors;
 296                                rq->buffer             = bh->b_data;
 297                        }
 298                }
 299
 300                /*
 301                 * Ok, we're all setup for the interrupt
 302                 * re-entering us on the last transfer.
 303                 */
 304                taskfile_output_data(drive, buffer, nsect<<7);
 305                ide_unmap_buffer(buffer, &flags);
 306        } while (mcount);
 307
 308        return 0;
 309}
 310
 311/*
 312 * multwrite_intr() is the handler for disk multwrite interrupts
 313 */
 314static ide_startstop_t multwrite_intr (ide_drive_t *drive)
 315{
 316        ide_hwgroup_t *hwgroup  = HWGROUP(drive);
 317        ide_hwif_t *hwif        = HWIF(drive);
 318        struct request *rq      = &hwgroup->wrq;
 319        u32 i = 0;
 320        u8 stat;
 321
 322        if (OK_STAT(stat = hwif->INB(IDE_STATUS_REG),
 323                        DRIVE_READY, drive->bad_wstat)) {
 324                if (stat & DRQ_STAT) {
 325                        /*
 326                         *      The drive wants data. Remember rq is the copy
 327                         *      of the request
 328                         */
 329                        if (rq->nr_sectors) {
 330                                if (ide_multwrite(drive, drive->mult_count))
 331                                        return ide_stopped;
 332                                ide_set_handler(drive, &multwrite_intr, WAIT_CMD, NULL);
 333                                return ide_started;
 334                        }
 335                } else {
 336                        /*
 337                         *      If the copy has all the blocks completed then
 338                         *      we can end the original request.
 339                         */
 340                        if (!rq->nr_sectors) {  /* all done? */
 341                                rq = hwgroup->rq;
 342                                for (i = rq->nr_sectors; i > 0;) {
 343                                        i -= rq->current_nr_sectors;
 344                                        idedisk_end_request(drive, 1);
 345                                }
 346                                return ide_stopped;
 347                        }
 348                }
 349                /* the original code did this here (?) */
 350                return ide_stopped;
 351        }
 352        return DRIVER(drive)->error(drive, "multwrite_intr", stat);
 353}
 354
 355
 356/*
 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.
 360 */
 361
 362ide_startstop_t __ide_do_rw_disk (ide_drive_t *drive, struct request *rq, unsigned long block)
 363{
 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;
 368
 369        nsectors.all            = (u16) rq->nr_sectors;
 370
 371        if (driver_blocked)
 372                panic("Request while ide driver is blocked?");
 373
 374        if (IDE_CONTROL_REG)
 375                hwif->OUTB(drive->ctl, IDE_CONTROL_REG);
 376
 377        if (drive->select.b.lba) {
 378                if (drive->addressing == 1) {
 379                        task_ioreg_t tasklets[10];
 380
 381                        tasklets[0] = 0;
 382                        tasklets[1] = 0;
 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);
 393#ifdef DEBUG
 394                        printk("%s: %sing: LBAsect=%lu, sectors=%ld, "
 395                                "buffer=0x%08lx, LBAsect=0x%012lx\n",
 396                                drive->name,
 397                                (rq->cmd==READ)?"read":"writ",
 398                                block,
 399                                rq->nr_sectors,
 400                                (unsigned long) rq->buffer,
 401                                block);
 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]);
 406#endif
 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);
 412
 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);
 419                } else {
 420#ifdef DEBUG
 421                        printk("%s: %sing: LBAsect=%ld, sectors=%ld, "
 422                                "buffer=0x%08lx\n",
 423                                drive->name, (rq->cmd==READ)?"read":"writ",
 424                                block, rq->nr_sectors,
 425                                (unsigned long) rq->buffer);
 426#endif
 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);
 433                }
 434        } else {
 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;
 441
 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);
 447#ifdef DEBUG
 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);
 451#endif
 452        }
 453
 454        if (rq_data_dir(rq) == READ) {
 455#ifdef CONFIG_BLK_DEV_IDEDMA
 456                if (drive->using_dma && !hwif->ide_dma_read(drive))
 457                        return ide_started;
 458#endif /* CONFIG_BLK_DEV_IDEDMA */
 459                if (HWGROUP(drive)->handler != NULL)
 460                        BUG();
 461
 462                command = ((drive->mult_count) ?
 463                           ((lba48) ? WIN_MULTREAD_EXT : WIN_MULTREAD) :
 464                           ((lba48) ? WIN_READ_EXT : WIN_READ));
 465        
 466                ide_execute_command(drive, command, &read_intr, WAIT_CMD, NULL);
 467                return ide_started;
 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)))
 472                        return ide_started;
 473#endif /* CONFIG_BLK_DEV_IDEDMA */
 474
 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);
 479
 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",
 483                                drive->name,
 484                                drive->mult_count ? "MULTWRITE" : "WRITE");
 485                        return startstop;
 486                }
 487                if (!drive->unmask)
 488                        local_irq_disable();
 489                if (drive->mult_count) {
 490                        ide_hwgroup_t *hwgroup = HWGROUP(drive);
 491        /*
 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?).
 497         *
 498         * Except when you get an error it seems...
 499         *
 500         * MAJOR DATA INTEGRITY BUG !!! only if we error 
 501         */
 502                        hwgroup->wrq = *rq; /* scratchpad */
 503                        ide_set_handler(drive, &multwrite_intr, WAIT_CMD, NULL);
 504                        if (ide_multwrite(drive, drive->mult_count)) {
 505                                unsigned long flags;
 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);
 510                                return ide_stopped;
 511                        }
 512                } else {
 513                        unsigned long 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);
 518                }
 519                return ide_started;
 520        }
 521        printk(KERN_ERR "%s: bad command: %d\n", drive->name, rq->cmd);
 522        idedisk_end_request(drive, 0);
 523        return ide_stopped;
 524}
 525
 526
 527#else /* CONFIG_IDE_TASKFILE_IO */
 528
 529static ide_startstop_t chs_rw_disk(ide_drive_t *, struct request *, unsigned long);
 530static ide_startstop_t lba_28_rw_disk(ide_drive_t *, struct request *, unsigned long);
 531static ide_startstop_t lba_48_rw_disk(ide_drive_t *, struct request *, unsigned long long);
 532
 533/*
 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.
 537 */
 538ide_startstop_t __ide_do_rw_disk (ide_drive_t *drive, struct request *rq, unsigned long block)
 539{
 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);
 543                return ide_stopped;
 544        }
 545
 546        /*
 547         * 268435455  == 137439 MB or 28bit limit
 548         *
 549         * need to add split taskfile operations based on 28bit threshold.
 550         */
 551
 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);
 556
 557        /* 28-bit CHS : DIE DIE DIE piece of legacy crap!!! */
 558        return chs_rw_disk(drive, rq, (unsigned long) block);
 559}
 560
 561static task_ioreg_t get_command (ide_drive_t *drive, int cmd)
 562{
 563        int lba48bit = (drive->id->cfs_enable_2 & 0x0400) ? 1 : 0;
 564
 565#if 1
 566        lba48bit = (drive->addressing == 1) ? 1 : 0;
 567#endif
 568
 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;
 581        else
 582                return WIN_NOP;
 583}
 584
 585static ide_startstop_t chs_rw_disk (ide_drive_t *drive, struct request *rq, unsigned long block)
 586{
 587        ide_task_t              args;
 588        int                     sectors;
 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);
 595
 596        nsectors.all = (u16) rq->nr_sectors;
 597#ifdef DEBUG
 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);
 602#endif
 603
 604        memset(&args, 0, sizeof(ide_task_t));
 605
 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);
 618}
 619
 620static ide_startstop_t lba_28_rw_disk (ide_drive_t *drive, struct request *rq, unsigned long block)
 621{
 622        ide_task_t              args;
 623        int                     sectors;
 624        ata_nsector_t           nsectors;
 625        task_ioreg_t command    = get_command(drive, rq_data_dir(rq));
 626
 627        nsectors.all = (u16) rq->nr_sectors;
 628
 629#ifdef DEBUG
 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);
 634#endif
 635
 636        memset(&args, 0, sizeof(ide_task_t));
 637
 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);
 650}
 651
 652/*
 653 * 268435455  == 137439 MB or 28bit limit
 654 * 320173056  == 163929 MB or 48bit addressing
 655 * 1073741822 == 549756 MB or 48bit addressing fake drive
 656 */
 657
 658static ide_startstop_t lba_48_rw_disk (ide_drive_t *drive, struct request *rq, unsigned long long block)
 659{
 660        ide_task_t              args;
 661        int                     sectors;
 662        ata_nsector_t           nsectors;
 663        task_ioreg_t command    = get_command(drive, rq_data_dir(rq));
 664
 665        nsectors.all = (u16) rq->nr_sectors;
 666
 667#ifdef DEBUG
 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);
 672#endif
 673
 674        memset(&args, 0, sizeof(ide_task_t));
 675
 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);
 693}
 694
 695#endif /* CONFIG_IDE_TASKFILE_IO */
 696
 697static ide_startstop_t ide_do_rw_disk (ide_drive_t *drive, struct request *rq, unsigned long block)
 698{
 699        ide_hwif_t *hwif        = HWIF(drive);
 700        if (hwif->rw_disk)
 701                return hwif->rw_disk(drive, rq, block);
 702        else 
 703                return __ide_do_rw_disk(drive, rq, block);
 704}
 705
 706EXPORT_SYMBOL_GPL(__ide_do_rw_disk);
 707
 708static int idedisk_open (struct inode *inode, struct file *filp, ide_drive_t *drive)
 709{
 710        MOD_INC_USE_COUNT;
 711        if (drive->removable && drive->usage == 1) {
 712                ide_task_t args;
 713                int cf;
 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);
 718                /*
 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.
 722                 */
 723                if (drive->doorlocking && ide_raw_taskfile(drive, &args, NULL))
 724                        drive->doorlocking = 0;
 725                drive->wcache = 0;
 726                /* Cache enabled ? */
 727                if (drive->id->csfo & 1)
 728                        drive->wcache = 1;
 729                /* Cache command set available ? */
 730                if (drive->id->cfs_enable_1 & (1<<5))
 731                        drive->wcache = 1;
 732                /* ATA6 cache extended commands */
 733                cf = drive->id->command_set_2 >> 24;
 734                if((cf & 0xC0) == 0x40 && (cf & 0x30) != 0)
 735                        drive->wcache = 1;
 736        }
 737        return 0;
 738}
 739
 740static int do_idedisk_flushcache(ide_drive_t *drive);
 741
 742static int ide_cacheflush_p(ide_drive_t *drive)
 743{
 744        if(drive->wcache)
 745        {
 746                if (do_idedisk_flushcache(drive))
 747                {
 748                        printk (KERN_INFO "%s: Write Cache FAILED Flushing!\n",
 749                                drive->name);
 750                        return -EIO;
 751                }
 752                return 1;
 753        }
 754        return 0;
 755}
 756
 757static void idedisk_release (struct inode *inode, struct file *filp, ide_drive_t *drive)
 758{
 759        if (drive->removable && !drive->usage) {
 760                ide_task_t args;
 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;
 767        }
 768        ide_cacheflush_p(drive);
 769        MOD_DEC_USE_COUNT;
 770}
 771
 772static int idedisk_media_change (ide_drive_t *drive)
 773{
 774        /* if removable, always assume it was changed */
 775        return drive->removable;
 776}
 777
 778static void idedisk_revalidate (ide_drive_t *drive)
 779{
 780        grok_partitions(HWIF(drive)->gd, drive->select.b.unit,
 781                        1<<PARTN_BITS,
 782                        current_capacity(drive));
 783}
 784
 785static int idedisk_end_request (ide_drive_t *drive, int uptodate)
 786{
 787        struct request *rq;
 788        unsigned long flags;
 789        int ret = 1;
 790
 791        spin_lock_irqsave(&io_request_lock, flags);
 792        rq = HWGROUP(drive)->rq;
 793
 794        /*
 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
 797         */
 798        if (drive->state == DMA_PIO_RETRY && drive->retry_pio <= 3) {
 799                drive->state = 0;
 800                HWGROUP(drive)->hwif->ide_dma_on(drive);
 801        }
 802
 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);
 808                ret = 0;
 809        }
 810
 811        spin_unlock_irqrestore(&io_request_lock, flags);
 812        return ret;
 813}
 814
 815static u8 idedisk_dump_status (ide_drive_t *drive, const char *msg, u8 stat)
 816{
 817        ide_hwif_t *hwif = HWIF(drive);
 818        unsigned long flags;
 819        u8 err = 0;
 820
 821        local_irq_set(flags);
 822        printk("%s: %s: status=0x%02x", drive->name, msg, stat);
 823#if FANCY_STATUS_DUMPS
 824        printk(" { ");
 825        if (stat & BUSY_STAT)
 826                printk("Busy ");
 827        else {
 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 ");
 835        }
 836        printk("}");
 837#endif  /* FANCY_STATUS_DUMPS */
 838        printk("\n");
 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
 843                printk(" { ");
 844                if (err & ABRT_ERR)     printk("DriveStatusError ");
 845                if (err & ICRC_ERR)
 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 ");
 851                printk("}");
 852                if ((err & (BBD_ERR | ABRT_ERR)) == BBD_ERR ||
 853                    (err & (ECC_ERR|ID_ERR|MARK_ERR))) {
 854                        if (drive->addressing == 1) {
 855                                __u64 sectors = 0;
 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,
 863                                       high, low);
 864                        } else {
 865                                u8 cur = hwif->INB(IDE_SELECT_REG);
 866                                if (cur & 0x40) {       /* using LBA? */
 867                                        printk(", LBAsect=%ld", (unsigned long)
 868                                         ((cur&0xf)<<24)
 869                                         |(hwif->INB(IDE_HCYL_REG)<<16)
 870                                         |(hwif->INB(IDE_LCYL_REG)<<8)
 871                                         | hwif->INB(IDE_SECTOR_REG));
 872                                } else {
 873                                        printk(", CHS=%d/%d/%d",
 874                                         (hwif->INB(IDE_HCYL_REG)<<8) +
 875                                          hwif->INB(IDE_LCYL_REG),
 876                                          cur & 0xf,
 877                                          hwif->INB(IDE_SECTOR_REG));
 878                                }
 879                        }
 880                        if (HWGROUP(drive) && HWGROUP(drive)->rq)
 881                                printk(", sector=%ld",
 882                                        HWGROUP(drive)->rq->sector);
 883                }
 884        }
 885#endif  /* FANCY_STATUS_DUMPS */
 886        printk("\n");
 887        local_irq_restore(flags);
 888        return err;
 889}
 890
 891ide_startstop_t idedisk_error (ide_drive_t *drive, const char *msg, u8 stat)
 892{
 893        ide_hwif_t *hwif;
 894        struct request *rq;
 895        u8 err;
 896        int i = (drive->mult_count ? drive->mult_count : 1) * SECTOR_WORDS;
 897
 898        err = idedisk_dump_status(drive, msg, stat);
 899
 900        if (drive == NULL || (rq = HWGROUP(drive)->rq) == NULL)
 901                return ide_stopped;
 902
 903        hwif = HWIF(drive);
 904        /* retry only "normal" I/O: */
 905        switch (rq->cmd) {
 906                case IDE_DRIVE_CMD:
 907                case IDE_DRIVE_TASK:
 908                case IDE_DRIVE_TASKFILE:
 909                        rq->errors = 1;
 910                        ide_end_drive_cmd(drive, stat, err);
 911                        return ide_stopped;
 912#if 0
 913                case IDE_DRIVE_TASKFILE:
 914                        rq->errors = 1;
 915                        ide_end_taskfile(drive, stat, err);
 916                        return ide_stopped;
 917#endif
 918                default:
 919                        break;
 920        }
 921
 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)
 931                                return ide_stopped;
 932                } else if ((err & BAD_CRC) == BAD_CRC) {
 933                        /* UDMA crc error, just retry the operation */
 934                        drive->crc_count++;
 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;
 941                }
 942        }
 943        if ((stat & DRQ_STAT) && rq_data_dir(rq) == READ) {
 944                /*
 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*.
 951                 */
 952                while (i > 0) {
 953                        u32 buffer[16];
 954                        unsigned int wcount = (i > 16) ? 16 : i;
 955                        i -= wcount;
 956                        taskfile_input_data(drive, buffer, wcount);
 957                }
 958        }
 959        if (hwif->INB(IDE_STATUS_REG) & (BUSY_STAT|DRQ_STAT)) {
 960                /* force an abort */
 961                hwif->OUTB(WIN_IDLEIMMEDIATE,IDE_COMMAND_REG);
 962        }
 963        if (rq->errors >= ERROR_MAX)
 964                DRIVER(drive)->end_request(drive, 0);
 965        else {
 966                if ((rq->errors & ERROR_RESET) == ERROR_RESET) {
 967                        ++rq->errors;
 968                        return ide_do_reset(drive);
 969                }
 970                if ((rq->errors & ERROR_RECAL) == ERROR_RECAL)
 971                        drive->special.b.recalibrate = 1;
 972                ++rq->errors;
 973        }
 974        return ide_stopped;
 975}
 976
 977ide_startstop_t idedisk_abort(ide_drive_t *drive, const char *msg)
 978{
 979        ide_hwif_t *hwif;
 980        struct request *rq;
 981
 982        if (drive == NULL || (rq = HWGROUP(drive)->rq) == NULL)
 983                return ide_stopped;
 984
 985        hwif = HWIF(drive);
 986        /* retry only "normal" I/O: */
 987        switch (rq->cmd) {
 988                case IDE_DRIVE_CMD:
 989                case IDE_DRIVE_TASK:
 990                case IDE_DRIVE_TASKFILE:
 991                        rq->errors = 1;
 992                        ide_end_drive_cmd(drive, BUSY_STAT, 0);
 993                        return ide_stopped;
 994#if 0
 995                case IDE_DRIVE_TASKFILE:
 996                        rq->errors = 1;
 997                        ide_end_taskfile(drive, BUSY_STAT, 0);
 998                        return ide_stopped;
 999#endif
1000                default:
1001                        break;
1002        }
1003
1004        rq->errors |= ERROR_RESET;
1005        DRIVER(drive)->end_request(drive, 0);
1006        return ide_stopped;
1007}
1008
1009/*
1010 * Queries for true maximum capacity of the drive.
1011 * Returns maximum LBA address (> 0) of the drive, 0 if failed.
1012 */
1013static unsigned long idedisk_read_native_max_address(ide_drive_t *drive)
1014{
1015        ide_task_t args;
1016        unsigned long addr = 0;
1017
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);
1025
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]       ));
1032        }
1033        addr++; /* since the return value is (maxlba - 1), we add 1 */
1034        return addr;
1035}
1036
1037static unsigned long long idedisk_read_native_max_address_ext(ide_drive_t *drive)
1038{
1039        ide_task_t args;
1040        unsigned long long addr = 0;
1041
1042        /* Create IDE/ATA command request structure */
1043        memset(&args, 0, sizeof(ide_task_t));
1044
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);
1050
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;
1060        }
1061        addr++; /* since the return value is (maxlba - 1), we add 1 */
1062        return addr;
1063}
1064
1065#ifdef CONFIG_IDEDISK_STROKE
1066/*
1067 * Sets maximum virtual LBA address of the drive.
1068 * Returns new maximum virtual LBA address (> 0) or 0 on failure.
1069 */
1070static unsigned long idedisk_set_max_address(ide_drive_t *drive, unsigned long addr_req)
1071{
1072        ide_task_t args;
1073        unsigned long addr_set = 0;
1074        
1075        addr_req--;
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]       ));
1092        }
1093        addr_set++;
1094        return addr_set;
1095}
1096
1097static unsigned long long idedisk_set_max_address_ext(ide_drive_t *drive, unsigned long long addr_req)
1098{
1099        ide_task_t args;
1100        unsigned long long addr_set = 0;
1101
1102        addr_req--;
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;
1127        }
1128        return addr_set;
1129}
1130
1131#endif /* CONFIG_IDEDISK_STROKE */
1132
1133/*
1134 * Tests if the drive supports Host Protected Area feature.
1135 * Returns true if supported, false otherwise.
1136 */
1137static inline int idedisk_supports_host_protected_area(ide_drive_t *drive)
1138{
1139        int flag = (drive->id->cfs_enable_1 & 0x0400) ? 1 : 0;
1140        if (flag)
1141                printk("%s: host protected area => %d\n", drive->name, flag);
1142        return flag;
1143}
1144
1145/*
1146 * Compute drive->capacity, the full capacity of the drive
1147 * Called with drive->id != NULL.
1148 *
1149 * To compute capacity, this uses either of
1150 *
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)
1155 *
1156 * in above order (i.e., if value of higher priority is available,
1157 * reset will be ignored).
1158 */
1159#define IDE_STROKE_LIMIT        (32000*1024*2)
1160static void init_idedisk_capacity (ide_drive_t  *drive)
1161{
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;
1169
1170        drive->capacity48 = 0;
1171        drive->select.b.lba = 0;
1172
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);
1184                        if (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;
1189                        }
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 */
1194                }
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;
1205        }
1206
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);
1211                if (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;
1216                }
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 */
1221        }
1222
1223        drive->capacity = capacity;
1224
1225        if ((id->command_set_2 & 0x0400) && (id->cfs_enable_2 & 0x0400)) {
1226                drive->capacity48 = id->lba_capacity_2;
1227                drive->head = 255;
1228                drive->sect = 63;
1229                drive->cyl = (unsigned long)(drive->capacity48) / (drive->head * drive->sect);
1230        }
1231
1232check_capacity48:
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;
1239        }
1240}
1241
1242static unsigned long idedisk_capacity (ide_drive_t *drive)
1243{
1244        if (drive->id->cfs_enable_2 & 0x0400)
1245                return (drive->capacity48 - drive->sect0);
1246        return (drive->capacity - drive->sect0);
1247}
1248
1249static ide_startstop_t idedisk_special (ide_drive_t *drive)
1250{
1251        special_t *s = &drive->special;
1252
1253        if (s->b.set_geometry) {
1254                s->b.set_geometry       = 0;
1255                if (!IS_PDC4030_DRIVE) {
1256                        ide_task_t args;
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);
1266                }
1267        } else if (s->b.recalibrate) {
1268                s->b.recalibrate = 0;
1269                if (!IS_PDC4030_DRIVE) {
1270                        ide_task_t args;
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);
1276                }
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) {
1282                        ide_task_t args;
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);
1288                }
1289        } else if (s->all) {
1290                int special = s->all;
1291                s->all = 0;
1292                printk(KERN_ERR "%s: bad special flag: 0x%02x\n", drive->name, special);
1293                return ide_stopped;
1294        }
1295        return IS_PDC4030_DRIVE ? ide_stopped : ide_started;
1296}
1297
1298static void idedisk_pre_reset (ide_drive_t *drive)
1299{
1300        int legacy = (drive->id->cfs_enable_2 & 0x0400) ? 0 : 1;
1301
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;
1311}
1312
1313#ifdef CONFIG_PROC_FS
1314
1315static int smart_enable(ide_drive_t *drive)
1316{
1317        ide_task_t args;
1318
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);
1326}
1327
1328static int get_smart_values(ide_drive_t *drive, u8 *buf)
1329{
1330        ide_task_t args;
1331
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);
1341}
1342
1343static int get_smart_thresholds(ide_drive_t *drive, u8 *buf)
1344{
1345        ide_task_t args;
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);
1355}
1356
1357static int proc_idedisk_read_cache
1358        (char *page, char **start, off_t off, int count, int *eof, void *data)
1359{
1360        ide_drive_t     *drive = (ide_drive_t *) data;
1361        char            *out = page;
1362        int             len;
1363
1364        if (drive->id_read)
1365                len = sprintf(out,"%i\n", drive->id->buf_size / 2);
1366        else
1367                len = sprintf(out,"(none)\n");
1368        PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
1369}
1370
1371static int proc_idedisk_read_smart_thresholds
1372        (char *page, char **start, off_t off, int count, int *eof, void *data)
1373{
1374        ide_drive_t     *drive = (ide_drive_t *)data;
1375        int             len = 0, i = 0;
1376
1377        if (!get_smart_thresholds(drive, page)) {
1378                unsigned short *val = (unsigned short *) page;
1379                char *out = ((char *)val) + (SECTOR_WORDS * 4);
1380                page = out;
1381                do {
1382                        out += sprintf(out, "%04x%c", le16_to_cpu(*val), (++i & 7) ? ' ' : '\n');
1383                        val += 1;
1384                } while (i < (SECTOR_WORDS * 2));
1385                len = out - page;
1386        }
1387        PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
1388}
1389
1390static int proc_idedisk_read_smart_values
1391        (char *page, char **start, off_t off, int count, int *eof, void *data)
1392{
1393        ide_drive_t     *drive = (ide_drive_t *)data;
1394        int             len = 0, i = 0;
1395
1396        if (!get_smart_values(drive, page)) {
1397                unsigned short *val = (unsigned short *) page;
1398                char *out = ((char *)val) + (SECTOR_WORDS * 4);
1399                page = out;
1400                do {
1401                        out += sprintf(out, "%04x%c", le16_to_cpu(*val), (++i & 7) ? ' ' : '\n');
1402                        val += 1;
1403                } while (i < (SECTOR_WORDS * 2));
1404                len = out - page;
1405        }
1406        PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
1407}
1408
1409static 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 }
1415};
1416
1417#else
1418
1419#define idedisk_proc    NULL
1420
1421#endif  /* CONFIG_PROC_FS */
1422
1423/*
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.
1426 */
1427static int set_multcount(ide_drive_t *drive, int arg)
1428{
1429        struct request rq;
1430
1431        if (drive->special.b.set_multmode)
1432                return -EBUSY;
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;
1439}
1440
1441static int set_nowerr(ide_drive_t *drive, int arg)
1442{
1443        if (ide_spin_wait_hwgroup(drive))
1444                return -EBUSY;
1445        drive->nowerr = arg;
1446        drive->bad_wstat = arg ? BAD_R_STAT : BAD_W_STAT;
1447        spin_unlock_irq(&io_request_lock);
1448        return 0;
1449}
1450
1451static int write_cache (ide_drive_t *drive, int arg)
1452{
1453        ide_task_t args;
1454
1455        if (!(drive->id->cfs_enable_2 & 0x3000))
1456                return 1;
1457
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);
1464
1465        drive->wcache = arg;
1466        return 0;
1467}
1468
1469static int call_idedisk_standby (ide_drive_t *drive, int arg)
1470{
1471        ide_task_t args;
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);
1477}
1478
1479static int do_idedisk_standby (ide_drive_t *drive)
1480{
1481        return call_idedisk_standby(drive, 0);
1482}
1483
1484static int call_idedisk_suspend (ide_drive_t *drive, int arg)
1485{
1486        ide_task_t args;
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);
1492}
1493
1494static int do_idedisk_suspend (ide_drive_t *drive)
1495{
1496        if (drive->suspend_reset)
1497                return 1;
1498        ide_cacheflush_p(drive);
1499        return call_idedisk_suspend(drive, 0);
1500}
1501
1502#if 0
1503static int call_idedisk_checkpower (ide_drive_t *drive, int arg)
1504{
1505        ide_task_t args;
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);
1511#if 0
1512if (errno != EIO || args[0] != 0 || args[1] != 0)
1513   state = "unknown";
1514else
1515   state = "sleeping";
1516} else {
1517   state = (args[2] == 255) ? "active/idle" : "standby";
1518#endif
1519        return 0;
1520}
1521
1522static int do_idedisk_checkpower (ide_drive_t *drive)
1523{
1524        return call_idedisk_checkpower(drive, 0);
1525}
1526#endif
1527
1528static int do_idedisk_resume (ide_drive_t *drive)
1529{
1530        if (!drive->suspend_reset)
1531                return 1;
1532        return 0;
1533}
1534
1535static int do_idedisk_flushcache (ide_drive_t *drive)
1536{
1537        ide_task_t args;
1538
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;
1542        else
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);
1546}
1547
1548static int set_acoustic (ide_drive_t *drive, int arg)
1549{
1550        ide_task_t args;
1551
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;
1560        return 0;
1561}
1562
1563static int probe_lba_addressing (ide_drive_t *drive, int arg)
1564{
1565        drive->addressing =  0;
1566
1567        if (HWIF(drive)->addressing)
1568                return 0;
1569
1570        if (!(drive->id->cfs_enable_2 & 0x0400))
1571                return -EIO;
1572        drive->addressing = arg;
1573        return 0;
1574}
1575
1576static int set_lba_addressing (ide_drive_t *drive, int arg)
1577{
1578        return (probe_lba_addressing(drive, arg));
1579}
1580
1581static void idedisk_add_settings(ide_drive_t *drive)
1582{
1583        struct hd_driveid *id = drive->id;
1584        int major = HWIF(drive)->major;
1585        int minor = drive->select.b.unit << PARTN_BITS;
1586
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);
1602}
1603
1604static int idedisk_ioctl (ide_drive_t *drive, struct inode *inode,
1605                struct file *file, unsigned int cmd, unsigned long arg)
1606{
1607#if 0
1608HDIO_GET_ADDRESS
1609HDIO_SET_ADDRESS
1610HDIO_GET_WCACHE
1611HDIO_SET_WCACHE
1612HDIO_GET_ACOUSTIC
1613HDIO_SET_ACOUSTIC
1614HDIO_GET_MULTCOUNT
1615HDIO_SET_MULTCOUNT
1616HDIO_GET_NOWERR
1617HDIO_SET_NOWERR
1618#endif
1619        return -EINVAL;
1620}
1621
1622static void idedisk_setup (ide_drive_t *drive)
1623{
1624        int i;
1625        
1626        struct hd_driveid *id = drive->id;
1627        unsigned long capacity;
1628
1629        idedisk_add_settings(drive);
1630
1631        if (drive->id_read == 0)
1632                return;
1633
1634        /*
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.
1637         */
1638        if (drive->removable && !(drive->is_flash)) {
1639                /*
1640                 * Removable disks (eg. SYQUEST); ignore 'WD' drives 
1641                 */
1642                if (id->model[0] != 'W' || id->model[1] != 'D') {
1643                        drive->doorlocking = 1;
1644                }
1645        }
1646        for (i = 0; i < MAX_DRIVES; ++i) {
1647                ide_hwif_t *hwif = HWIF(drive);
1648
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;
1653                break;
1654        }
1655
1656#if 1
1657        (void) probe_lba_addressing(drive, 1);
1658#else
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);
1662#endif
1663
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;
1669        }
1670
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;
1677        }
1678
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;
1684        }
1685
1686        /* calculate drive capacity, and select LBA if possible */
1687        init_idedisk_capacity (drive);
1688
1689        /*
1690         * if possible, give fdisk access to more of the drive,
1691         * by correcting bios_cyls:
1692         */
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);
1698
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);
1702
1703        /* Only print cache size when it was specified */
1704        if (id->buf_size)
1705                printk (" w/%dKiB Cache", id->buf_size/2);
1706
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 */
1713        printk("\n");
1714
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;
1721        }
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));
1725}
1726
1727static int idedisk_cleanup(ide_drive_t *drive)
1728{
1729        ide_cacheflush_p(drive);
1730        return ide_unregister_subdriver(drive);
1731}
1732
1733int idedisk_init (void);
1734int idedisk_attach(ide_drive_t *drive);
1735
1736/*
1737 *      IDE subdriver functions, registered with ide.c
1738 */
1739static ide_driver_t idedisk_driver = {
1740        name:                   "ide-disk",
1741        version:                IDEDISK_VERSION,
1742        media:                  ide_disk,
1743        busy:                   0,
1744        supports_dma:           1,
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,
1757        open:                   idedisk_open,
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,
1764        proc:                   idedisk_proc,
1765        init:                   idedisk_init,
1766        attach:                 idedisk_attach,
1767        ata_prebuilder:         NULL,
1768        atapi_prebuilder:       NULL,
1769};
1770
1771static ide_module_t idedisk_module = {
1772        IDE_DRIVER_MODULE,
1773        idedisk_init,
1774        &idedisk_driver,
1775        NULL
1776};
1777
1778MODULE_DESCRIPTION("ATA DISK Driver");
1779
1780int idedisk_attach (ide_drive_t *drive)
1781{
1782        int ret = 0;
1783
1784        MOD_INC_USE_COUNT;
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);
1789                ret= 1;
1790                goto bye_game_over;
1791        }
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);
1798                ret= 1;
1799        }
1800        DRIVER(drive)->busy--;
1801bye_game_over:
1802        MOD_DEC_USE_COUNT;
1803        return ret;
1804}
1805
1806static void __exit idedisk_exit (void)
1807{
1808        ide_drive_t *drive;
1809        int failed = 0;
1810
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);
1816                        failed++;
1817                }
1818#ifdef CONFIG_PROC_FS
1819                /* We must remove proc entries defined in this module.
1820                 * Otherwise we oops while accessing these entries
1821                 */
1822                if (drive->proc)
1823                        ide_remove_proc_entries(drive->proc, idedisk_proc);
1824#endif
1825        }
1826        ide_unregister_module(&idedisk_module);
1827}
1828
1829int idedisk_init (void)
1830{
1831#ifdef CLASSIC_BUILTINS_METHOD
1832        ide_drive_t *drive;
1833        int failed = 0;
1834#endif /* CLASSIC_BUILTINS_METHOD */
1835
1836        MOD_INC_USE_COUNT;
1837
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);
1845                        continue;
1846                }
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--;
1855                        continue;
1856                }
1857                DRIVER(drive)->busy--;
1858                failed--;
1859        }
1860#endif /* CLASSIC_BUILTINS_METHOD */
1861
1862        ide_register_module(&idedisk_module);
1863        MOD_DEC_USE_COUNT;
1864        return 0;
1865}
1866
1867ide_startstop_t panic_box(ide_drive_t *drive)
1868{
1869#if 0
1870        panic("%s: Attempted to corrupt something: ide operation "
1871#else
1872        printk(KERN_ERR "%s: Attempted to corrupt something: ide operation "
1873#endif
1874                "was pending accross suspend/resume.\n", drive->name);
1875        return ide_stopped;
1876}
1877
1878int ide_disks_busy(void)
1879{
1880        int i;
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))
1885                        return 1;
1886        }
1887        return 0;
1888}
1889
1890void ide_disk_suspend(void)
1891{
1892        int i;
1893        while (ide_disks_busy()) {
1894                printk("*");
1895                schedule();
1896        }
1897        for (i=0; i<MAX_HWIFS; i++) {
1898                struct hwgroup_s *hwgroup = ide_hwifs[i].hwgroup;
1899
1900                if (!hwgroup) continue;
1901                hwgroup->handler_save = hwgroup->handler;
1902                hwgroup->handler = panic_box;
1903        }
1904        driver_blocked = 1;
1905        if (ide_disks_busy())
1906                panic("How did you get that request through?!");
1907}
1908
1909/* unsuspend and resume should be equal in the ideal world */
1910
1911void ide_disk_unsuspend(void)
1912{
1913        int i;
1914        for (i=0; i<MAX_HWIFS; i++) {
1915                struct hwgroup_s *hwgroup = ide_hwifs[i].hwgroup;
1916
1917                if (!hwgroup) continue;
1918                hwgroup->handler = NULL; /* hwgroup->handler_save; */
1919                hwgroup->handler_save = NULL;
1920        }
1921        driver_blocked = 0;
1922}
1923
1924void ide_disk_resume(void)
1925{
1926        int i;
1927        for (i=0; i<MAX_HWIFS; i++) {
1928                struct hwgroup_s *hwgroup = ide_hwifs[i].hwgroup;
1929
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;
1935        }
1936        driver_blocked = 0;
1937}
1938
1939module_init(idedisk_init);
1940module_exit(idedisk_exit);
1941MODULE_LICENSE("GPL");
1942
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.