upgrade to linux 2.6.10-1.12_FC2
[linux-2.6.git] / drivers / ide / ide-disk.c
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 Development
6  *                              Andre Hedrick <andre@linux-ide.org>
7  *  Copyright (C) 2003       Red Hat <alan@redhat.com>
8  */
9
10 /*
11  *  Mostly written by Mark Lord <mlord@pobox.com>
12  *                and Gadi Oxman <gadio@netvision.net.il>
13  *                and Andre Hedrick <andre@linux-ide.org>
14  *
15  * This is the IDE/ATA disk driver, as evolved from hd.c and ide.c.
16  *
17  * Version 1.00         move disk only code from ide.c to ide-disk.c
18  *                      support optional byte-swapping of all data
19  * Version 1.01         fix previous byte-swapping code
20  * Version 1.02         remove ", LBA" from drive identification msgs
21  * Version 1.03         fix display of id->buf_size for big-endian
22  * Version 1.04         add /proc configurable settings and S.M.A.R.T support
23  * Version 1.05         add capacity support for ATA3 >= 8GB
24  * Version 1.06         get boot-up messages to show full cyl count
25  * Version 1.07         disable door-locking if it fails
26  * Version 1.08         fixed CHS/LBA translations for ATA4 > 8GB,
27  *                      process of adding new ATA4 compliance.
28  *                      fixed problems in allowing fdisk to see
29  *                      the entire disk.
30  * Version 1.09         added increment of rq->sector in ide_multwrite
31  *                      added UDMA 3/4 reporting
32  * Version 1.10         request queue changes, Ultra DMA 100
33  * Version 1.11         added 48-bit lba
34  * Version 1.12         adding taskfile io access method
35  * Version 1.13         added standby and flush-cache for notifier
36  * Version 1.14         added acoustic-wcache
37  * Version 1.15         convert all calls to ide_raw_taskfile
38  *                              since args will return register content.
39  * Version 1.16         added suspend-resume-checkpower
40  * Version 1.17         do flush on standy, do flush on ATA < ATA6
41  *                      fix wcache setup.
42  */
43
44 #define IDEDISK_VERSION "1.18"
45
46 #undef REALLY_SLOW_IO           /* most systems can safely undef this */
47
48 //#define DEBUG
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 #include <asm/div64.h>
73
74 /*
75  * lba_capacity_is_ok() performs a sanity check on the claimed "lba_capacity"
76  * value for this drive (from its reported identification information).
77  *
78  * Returns:     1 if lba_capacity looks sensible
79  *              0 otherwise
80  *
81  * It is called only once for each drive.
82  */
83 static int lba_capacity_is_ok (struct hd_driveid *id)
84 {
85         unsigned long lba_sects, chs_sects, head, tail;
86
87         /* No non-LBA info .. so valid! */
88         if (id->cyls == 0)
89                 return 1;
90                 
91         /*
92          * The ATA spec tells large drives to return
93          * C/H/S = 16383/16/63 independent of their size.
94          * Some drives can be jumpered to use 15 heads instead of 16.
95          * Some drives can be jumpered to use 4092 cyls instead of 16383.
96          */
97         if ((id->cyls == 16383
98              || (id->cyls == 4092 && id->cur_cyls == 16383)) &&
99             id->sectors == 63 &&
100             (id->heads == 15 || id->heads == 16) &&
101             (id->lba_capacity >= 16383*63*id->heads))
102                 return 1;
103
104         lba_sects   = id->lba_capacity;
105         chs_sects   = id->cyls * id->heads * id->sectors;
106
107         /* perform a rough sanity check on lba_sects:  within 10% is OK */
108         if ((lba_sects - chs_sects) < chs_sects/10)
109                 return 1;
110
111         /* some drives have the word order reversed */
112         head = ((lba_sects >> 16) & 0xffff);
113         tail = (lba_sects & 0xffff);
114         lba_sects = (head | (tail << 16));
115         if ((lba_sects - chs_sects) < chs_sects/10) {
116                 id->lba_capacity = lba_sects;
117                 return 1;       /* lba_capacity is (now) good */
118         }
119
120         return 0;       /* lba_capacity value may be bad */
121 }
122
123 /*
124  * __ide_do_rw_disk() issues READ and WRITE commands to a disk,
125  * using LBA if supported, or CHS otherwise, to address sectors.
126  * It also takes care of issuing special DRIVE_CMDs.
127  */
128 ide_startstop_t __ide_do_rw_disk (ide_drive_t *drive, struct request *rq, sector_t block)
129 {
130         ide_hwif_t *hwif        = HWIF(drive);
131         unsigned int dma        = drive->using_dma;
132         u8 lba48                = (drive->addressing == 1) ? 1 : 0;
133         task_ioreg_t command    = WIN_NOP;
134         ata_nsector_t           nsectors;
135
136         nsectors.all            = (u16) rq->nr_sectors;
137
138         if (hwif->no_lba48_dma && lba48 && dma) {
139                 if (rq->sector + rq->nr_sectors > 1ULL << 28)
140                         dma = 0;
141         }
142
143         if (!dma) {
144                 ide_init_sg_cmd(drive, rq);
145                 ide_map_sg(drive, rq);
146         }
147
148         if (IDE_CONTROL_REG)
149                 hwif->OUTB(drive->ctl, IDE_CONTROL_REG);
150
151         /* FIXME: SELECT_MASK(drive, 0) ? */
152
153         if (drive->select.b.lba) {
154                 if (drive->addressing == 1) {
155                         task_ioreg_t tasklets[10];
156
157                         pr_debug("%s: LBA=0x%012llx\n", drive->name, block);
158
159                         tasklets[0] = 0;
160                         tasklets[1] = 0;
161                         tasklets[2] = nsectors.b.low;
162                         tasklets[3] = nsectors.b.high;
163                         tasklets[4] = (task_ioreg_t) block;
164                         tasklets[5] = (task_ioreg_t) (block>>8);
165                         tasklets[6] = (task_ioreg_t) (block>>16);
166                         tasklets[7] = (task_ioreg_t) (block>>24);
167                         if (sizeof(block) == 4) {
168                                 tasklets[8] = (task_ioreg_t) 0;
169                                 tasklets[9] = (task_ioreg_t) 0;
170                         } else {
171                                 tasklets[8] = (task_ioreg_t)((u64)block >> 32);
172                                 tasklets[9] = (task_ioreg_t)((u64)block >> 40);
173                         }
174 #ifdef DEBUG
175                         printk("%s: 0x%02x%02x 0x%02x%02x%02x%02x%02x%02x\n",
176                                 drive->name, tasklets[3], tasklets[2],
177                                 tasklets[9], tasklets[8], tasklets[7],
178                                 tasklets[6], tasklets[5], tasklets[4]);
179 #endif
180                         hwif->OUTB(tasklets[1], IDE_FEATURE_REG);
181                         hwif->OUTB(tasklets[3], IDE_NSECTOR_REG);
182                         hwif->OUTB(tasklets[7], IDE_SECTOR_REG);
183                         hwif->OUTB(tasklets[8], IDE_LCYL_REG);
184                         hwif->OUTB(tasklets[9], IDE_HCYL_REG);
185
186                         hwif->OUTB(tasklets[0], IDE_FEATURE_REG);
187                         hwif->OUTB(tasklets[2], IDE_NSECTOR_REG);
188                         hwif->OUTB(tasklets[4], IDE_SECTOR_REG);
189                         hwif->OUTB(tasklets[5], IDE_LCYL_REG);
190                         hwif->OUTB(tasklets[6], IDE_HCYL_REG);
191                         hwif->OUTB(0x00|drive->select.all,IDE_SELECT_REG);
192                 } else {
193                         hwif->OUTB(0x00, IDE_FEATURE_REG);
194                         hwif->OUTB(nsectors.b.low, IDE_NSECTOR_REG);
195                         hwif->OUTB(block, IDE_SECTOR_REG);
196                         hwif->OUTB(block>>=8, IDE_LCYL_REG);
197                         hwif->OUTB(block>>=8, IDE_HCYL_REG);
198                         hwif->OUTB(((block>>8)&0x0f)|drive->select.all,IDE_SELECT_REG);
199                 }
200         } else {
201                 unsigned int sect,head,cyl,track;
202                 track = (int)block / drive->sect;
203                 sect  = (int)block % drive->sect + 1;
204                 hwif->OUTB(sect, IDE_SECTOR_REG);
205                 head  = track % drive->head;
206                 cyl   = track / drive->head;
207
208                 pr_debug("%s: CHS=%u/%u/%u\n", drive->name, cyl, head, sect);
209
210                 hwif->OUTB(0x00, IDE_FEATURE_REG);
211                 hwif->OUTB(nsectors.b.low, IDE_NSECTOR_REG);
212                 hwif->OUTB(cyl, IDE_LCYL_REG);
213                 hwif->OUTB(cyl>>8, IDE_HCYL_REG);
214                 hwif->OUTB(head|drive->select.all,IDE_SELECT_REG);
215         }
216
217         if (dma) {
218                 if (!hwif->ide_dma_setup(drive)) {
219                         if (rq_data_dir(rq)) {
220                                 command = lba48 ? WIN_WRITEDMA_EXT : WIN_WRITEDMA;
221                                 if (drive->vdma)
222                                         command = lba48 ? WIN_WRITE_EXT: WIN_WRITE;
223                         } else {
224                                 command = lba48 ? WIN_READDMA_EXT : WIN_READDMA;
225                                 if (drive->vdma)
226                                         command = lba48 ? WIN_READ_EXT: WIN_READ;
227                         }
228                         hwif->ide_dma_exec_cmd(drive, command);
229                         hwif->ide_dma_start(drive);
230                         return ide_started;
231                 }
232                 /* fallback to PIO */
233                 ide_init_sg_cmd(drive, rq);
234         }
235
236         if (rq_data_dir(rq) == READ) {
237
238                 if (drive->mult_count) {
239                         hwif->data_phase = TASKFILE_MULTI_IN;
240                         command = lba48 ? WIN_MULTREAD_EXT : WIN_MULTREAD;
241                 } else {
242                         hwif->data_phase = TASKFILE_IN;
243                         command = lba48 ? WIN_READ_EXT : WIN_READ;
244                 }
245
246                 ide_execute_command(drive, command, &task_in_intr, WAIT_CMD, NULL);
247                 return ide_started;
248         } else {
249                 if (drive->mult_count) {
250                         hwif->data_phase = TASKFILE_MULTI_OUT;
251                         command = lba48 ? WIN_MULTWRITE_EXT : WIN_MULTWRITE;
252                 } else {
253                         hwif->data_phase = TASKFILE_OUT;
254                         command = lba48 ? WIN_WRITE_EXT : WIN_WRITE;
255                 }
256
257                 /* FIXME: ->OUTBSYNC ? */
258                 hwif->OUTB(command, IDE_COMMAND_REG);
259
260                 pre_task_out_intr(drive, rq);
261                 return ide_started;
262         }
263 }
264 EXPORT_SYMBOL_GPL(__ide_do_rw_disk);
265
266 /*
267  * 268435455  == 137439 MB or 28bit limit
268  * 320173056  == 163929 MB or 48bit addressing
269  * 1073741822 == 549756 MB or 48bit addressing fake drive
270  */
271
272 static ide_startstop_t ide_do_rw_disk (ide_drive_t *drive, struct request *rq, sector_t block)
273 {
274         ide_hwif_t *hwif = HWIF(drive);
275
276         BUG_ON(drive->blocked);
277
278         if (!blk_fs_request(rq)) {
279                 blk_dump_rq_flags(rq, "ide_do_rw_disk - bad command");
280                 ide_end_request(drive, 0, 0);
281                 return ide_stopped;
282         }
283
284         pr_debug("%s: %sing: block=%llu, sectors=%lu, buffer=0x%08lx\n",
285                  drive->name, rq_data_dir(rq) == READ ? "read" : "writ",
286                  block, rq->nr_sectors, (unsigned long)rq->buffer);
287
288         if (hwif->rw_disk)
289                 return hwif->rw_disk(drive, rq, block);
290         else
291                 return __ide_do_rw_disk(drive, rq, block);
292 }
293
294 static u8 idedisk_dump_status (ide_drive_t *drive, const char *msg, u8 stat)
295 {
296         ide_hwif_t *hwif = HWIF(drive);
297         unsigned long flags;
298         u8 err = 0;
299
300         local_irq_set(flags);
301         printk("%s: %s: status=0x%02x", drive->name, msg, stat);
302         printk(" { ");
303         if (stat & BUSY_STAT)
304                 printk("Busy ");
305         else {
306                 if (stat & READY_STAT)  printk("DriveReady ");
307                 if (stat & WRERR_STAT)  printk("DeviceFault ");
308                 if (stat & SEEK_STAT)   printk("SeekComplete ");
309                 if (stat & DRQ_STAT)    printk("DataRequest ");
310                 if (stat & ECC_STAT)    printk("CorrectedError ");
311                 if (stat & INDEX_STAT)  printk("Index ");
312                 if (stat & ERR_STAT)    printk("Error ");
313         }
314         printk("}");
315         printk("\n");
316         if ((stat & (BUSY_STAT|ERR_STAT)) == ERR_STAT) {
317                 err = hwif->INB(IDE_ERROR_REG);
318                 printk("%s: %s: error=0x%02x", drive->name, msg, err);
319                 printk(" { ");
320                 if (err & ABRT_ERR)     printk("DriveStatusError ");
321                 if (err & ICRC_ERR)
322                         printk("Bad%s ", (err & ABRT_ERR) ? "CRC" : "Sector");
323                 if (err & ECC_ERR)      printk("UncorrectableError ");
324                 if (err & ID_ERR)       printk("SectorIdNotFound ");
325                 if (err & TRK0_ERR)     printk("TrackZeroNotFound ");
326                 if (err & MARK_ERR)     printk("AddrMarkNotFound ");
327                 printk("}");
328                 if ((err & (BBD_ERR | ABRT_ERR)) == BBD_ERR ||
329                     (err & (ECC_ERR|ID_ERR|MARK_ERR))) {
330                         if (drive->addressing == 1) {
331                                 __u64 sectors = 0;
332                                 u32 low = 0, high = 0;
333                                 low = ide_read_24(drive);
334                                 hwif->OUTB(drive->ctl|0x80, IDE_CONTROL_REG);
335                                 high = ide_read_24(drive);
336                                 sectors = ((__u64)high << 24) | low;
337                                 printk(", LBAsect=%llu, high=%d, low=%d",
338                                        (unsigned long long) sectors,
339                                        high, low);
340                         } else {
341                                 u8 cur = hwif->INB(IDE_SELECT_REG);
342                                 if (cur & 0x40) {       /* using LBA? */
343                                         printk(", LBAsect=%ld", (unsigned long)
344                                          ((cur&0xf)<<24)
345                                          |(hwif->INB(IDE_HCYL_REG)<<16)
346                                          |(hwif->INB(IDE_LCYL_REG)<<8)
347                                          | hwif->INB(IDE_SECTOR_REG));
348                                 } else {
349                                         printk(", CHS=%d/%d/%d",
350                                          (hwif->INB(IDE_HCYL_REG)<<8) +
351                                           hwif->INB(IDE_LCYL_REG),
352                                           cur & 0xf,
353                                           hwif->INB(IDE_SECTOR_REG));
354                                 }
355                         }
356                         if (HWGROUP(drive) && HWGROUP(drive)->rq)
357                                 printk(", sector=%llu",
358                                         (unsigned long long)HWGROUP(drive)->rq->sector);
359                 }
360         }
361         printk("\n");
362         {
363                 struct request *rq;
364                 unsigned char opcode = 0;
365                 int found = 0;
366
367                 spin_lock(&ide_lock);
368                 rq = HWGROUP(drive)->rq;
369                 spin_unlock(&ide_lock);
370                 if (!rq)
371                         goto out;
372                 if (rq->flags & (REQ_DRIVE_CMD | REQ_DRIVE_TASK)) {
373                         char *args = rq->buffer;
374                         if (args) {
375                                 opcode = args[0];
376                                 found = 1;
377                         }
378                 } else if (rq->flags & REQ_DRIVE_TASKFILE) {
379                         ide_task_t *args = rq->special;
380                         if (args) {
381                                 task_struct_t *tf = (task_struct_t *) args->tfRegister;
382                                 opcode = tf->command;
383                                 found = 1;
384                         }
385                 }
386                 printk("ide: failed opcode was: ");
387                 if (!found)
388                         printk("unknown\n");
389                 else
390                         printk("0x%02x\n", opcode);
391         }
392 out:
393         local_irq_restore(flags);
394         return err;
395 }
396
397 static ide_startstop_t idedisk_error (ide_drive_t *drive, const char *msg, u8 stat)
398 {
399         ide_hwif_t *hwif;
400         struct request *rq;
401         u8 err;
402
403         err = idedisk_dump_status(drive, msg, stat);
404
405         if (drive == NULL || (rq = HWGROUP(drive)->rq) == NULL)
406                 return ide_stopped;
407
408         hwif = HWIF(drive);
409         /* retry only "normal" I/O: */
410         if (rq->flags & (REQ_DRIVE_CMD | REQ_DRIVE_TASK | REQ_DRIVE_TASKFILE)) {
411                 rq->errors = 1;
412                 ide_end_drive_cmd(drive, stat, err);
413                 return ide_stopped;
414         }
415
416         if (stat & BUSY_STAT || ((stat & WRERR_STAT) && !drive->nowerr)) {
417                 /* other bits are useless when BUSY */
418                 rq->errors |= ERROR_RESET;
419         } else if (stat & ERR_STAT) {
420                 /* err has different meaning on cdrom and tape */
421                 if (err == ABRT_ERR) {
422                         if (drive->select.b.lba &&
423                             /* some newer drives don't support WIN_SPECIFY */
424                             hwif->INB(IDE_COMMAND_REG) == WIN_SPECIFY)
425                                 return ide_stopped;
426                 } else if ((err & BAD_CRC) == BAD_CRC) {
427                         /* UDMA crc error, just retry the operation */
428                         drive->crc_count++;
429                 } else if (err & (BBD_ERR | ECC_ERR)) {
430                         /* retries won't help these */
431                         rq->errors = ERROR_MAX;
432                 } else if (err & TRK0_ERR) {
433                         /* help it find track zero */
434                         rq->errors |= ERROR_RECAL;
435                 }
436         }
437         if ((stat & DRQ_STAT) && rq_data_dir(rq) == READ)
438                 try_to_flush_leftover_data(drive);
439         if (hwif->INB(IDE_STATUS_REG) & (BUSY_STAT|DRQ_STAT)) {
440                 /* force an abort */
441                 hwif->OUTB(WIN_IDLEIMMEDIATE,IDE_COMMAND_REG);
442         }
443         if (rq->errors >= ERROR_MAX || blk_noretry_request(rq))
444                 DRIVER(drive)->end_request(drive, 0, 0);
445         else {
446                 if ((rq->errors & ERROR_RESET) == ERROR_RESET) {
447                         ++rq->errors;
448                         return ide_do_reset(drive);
449                 }
450                 if ((rq->errors & ERROR_RECAL) == ERROR_RECAL)
451                         drive->special.b.recalibrate = 1;
452                 ++rq->errors;
453         }
454         return ide_stopped;
455 }
456
457 static ide_startstop_t idedisk_abort(ide_drive_t *drive, const char *msg)
458 {
459         ide_hwif_t *hwif;
460         struct request *rq;
461
462         if (drive == NULL || (rq = HWGROUP(drive)->rq) == NULL)
463                 return ide_stopped;
464
465         hwif = HWIF(drive);
466
467         if (rq->flags & (REQ_DRIVE_CMD | REQ_DRIVE_TASK | REQ_DRIVE_TASKFILE)) {
468                 rq->errors = 1;
469                 ide_end_drive_cmd(drive, BUSY_STAT, 0);
470                 return ide_stopped;
471         }
472
473         DRIVER(drive)->end_request(drive, 0, 0);
474         return ide_stopped;
475 }
476
477 /*
478  * Queries for true maximum capacity of the drive.
479  * Returns maximum LBA address (> 0) of the drive, 0 if failed.
480  */
481 static unsigned long idedisk_read_native_max_address(ide_drive_t *drive)
482 {
483         ide_task_t args;
484         unsigned long addr = 0;
485
486         /* Create IDE/ATA command request structure */
487         memset(&args, 0, sizeof(ide_task_t));
488         args.tfRegister[IDE_SELECT_OFFSET]      = 0x40;
489         args.tfRegister[IDE_COMMAND_OFFSET]     = WIN_READ_NATIVE_MAX;
490         args.command_type                       = IDE_DRIVE_TASK_NO_DATA;
491         args.handler                            = &task_no_data_intr;
492         /* submit command request */
493         ide_raw_taskfile(drive, &args, NULL);
494
495         /* if OK, compute maximum address value */
496         if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
497                 addr = ((args.tfRegister[IDE_SELECT_OFFSET] & 0x0f) << 24)
498                      | ((args.tfRegister[  IDE_HCYL_OFFSET]       ) << 16)
499                      | ((args.tfRegister[  IDE_LCYL_OFFSET]       ) <<  8)
500                      | ((args.tfRegister[IDE_SECTOR_OFFSET]       ));
501                 addr++; /* since the return value is (maxlba - 1), we add 1 */
502         }
503         return addr;
504 }
505
506 static unsigned long long idedisk_read_native_max_address_ext(ide_drive_t *drive)
507 {
508         ide_task_t args;
509         unsigned long long addr = 0;
510
511         /* Create IDE/ATA command request structure */
512         memset(&args, 0, sizeof(ide_task_t));
513
514         args.tfRegister[IDE_SELECT_OFFSET]      = 0x40;
515         args.tfRegister[IDE_COMMAND_OFFSET]     = WIN_READ_NATIVE_MAX_EXT;
516         args.command_type                       = IDE_DRIVE_TASK_NO_DATA;
517         args.handler                            = &task_no_data_intr;
518         /* submit command request */
519         ide_raw_taskfile(drive, &args, NULL);
520
521         /* if OK, compute maximum address value */
522         if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
523                 u32 high = (args.hobRegister[IDE_HCYL_OFFSET] << 16) |
524                            (args.hobRegister[IDE_LCYL_OFFSET] <<  8) |
525                             args.hobRegister[IDE_SECTOR_OFFSET];
526                 u32 low  = ((args.tfRegister[IDE_HCYL_OFFSET])<<16) |
527                            ((args.tfRegister[IDE_LCYL_OFFSET])<<8) |
528                             (args.tfRegister[IDE_SECTOR_OFFSET]);
529                 addr = ((__u64)high << 24) | low;
530                 addr++; /* since the return value is (maxlba - 1), we add 1 */
531         }
532         return addr;
533 }
534
535 /*
536  * Sets maximum virtual LBA address of the drive.
537  * Returns new maximum virtual LBA address (> 0) or 0 on failure.
538  */
539 static unsigned long idedisk_set_max_address(ide_drive_t *drive, unsigned long addr_req)
540 {
541         ide_task_t args;
542         unsigned long addr_set = 0;
543         
544         addr_req--;
545         /* Create IDE/ATA command request structure */
546         memset(&args, 0, sizeof(ide_task_t));
547         args.tfRegister[IDE_SECTOR_OFFSET]      = ((addr_req >>  0) & 0xff);
548         args.tfRegister[IDE_LCYL_OFFSET]        = ((addr_req >>  8) & 0xff);
549         args.tfRegister[IDE_HCYL_OFFSET]        = ((addr_req >> 16) & 0xff);
550         args.tfRegister[IDE_SELECT_OFFSET]      = ((addr_req >> 24) & 0x0f) | 0x40;
551         args.tfRegister[IDE_COMMAND_OFFSET]     = WIN_SET_MAX;
552         args.command_type                       = IDE_DRIVE_TASK_NO_DATA;
553         args.handler                            = &task_no_data_intr;
554         /* submit command request */
555         ide_raw_taskfile(drive, &args, NULL);
556         /* if OK, read new maximum address value */
557         if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
558                 addr_set = ((args.tfRegister[IDE_SELECT_OFFSET] & 0x0f) << 24)
559                          | ((args.tfRegister[  IDE_HCYL_OFFSET]       ) << 16)
560                          | ((args.tfRegister[  IDE_LCYL_OFFSET]       ) <<  8)
561                          | ((args.tfRegister[IDE_SECTOR_OFFSET]       ));
562                 addr_set++;
563         }
564         return addr_set;
565 }
566
567 static unsigned long long idedisk_set_max_address_ext(ide_drive_t *drive, unsigned long long addr_req)
568 {
569         ide_task_t args;
570         unsigned long long addr_set = 0;
571
572         addr_req--;
573         /* Create IDE/ATA command request structure */
574         memset(&args, 0, sizeof(ide_task_t));
575         args.tfRegister[IDE_SECTOR_OFFSET]      = ((addr_req >>  0) & 0xff);
576         args.tfRegister[IDE_LCYL_OFFSET]        = ((addr_req >>= 8) & 0xff);
577         args.tfRegister[IDE_HCYL_OFFSET]        = ((addr_req >>= 8) & 0xff);
578         args.tfRegister[IDE_SELECT_OFFSET]      = 0x40;
579         args.tfRegister[IDE_COMMAND_OFFSET]     = WIN_SET_MAX_EXT;
580         args.hobRegister[IDE_SECTOR_OFFSET]     = (addr_req >>= 8) & 0xff;
581         args.hobRegister[IDE_LCYL_OFFSET]       = (addr_req >>= 8) & 0xff;
582         args.hobRegister[IDE_HCYL_OFFSET]       = (addr_req >>= 8) & 0xff;
583         args.hobRegister[IDE_SELECT_OFFSET]     = 0x40;
584         args.hobRegister[IDE_CONTROL_OFFSET_HOB]= (drive->ctl|0x80);
585         args.command_type                       = IDE_DRIVE_TASK_NO_DATA;
586         args.handler                            = &task_no_data_intr;
587         /* submit command request */
588         ide_raw_taskfile(drive, &args, NULL);
589         /* if OK, compute maximum address value */
590         if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
591                 u32 high = (args.hobRegister[IDE_HCYL_OFFSET] << 16) |
592                            (args.hobRegister[IDE_LCYL_OFFSET] <<  8) |
593                             args.hobRegister[IDE_SECTOR_OFFSET];
594                 u32 low  = ((args.tfRegister[IDE_HCYL_OFFSET])<<16) |
595                            ((args.tfRegister[IDE_LCYL_OFFSET])<<8) |
596                             (args.tfRegister[IDE_SECTOR_OFFSET]);
597                 addr_set = ((__u64)high << 24) | low;
598                 addr_set++;
599         }
600         return addr_set;
601 }
602
603 static unsigned long long sectors_to_MB(unsigned long long n)
604 {
605         n <<= 9;                /* make it bytes */
606         do_div(n, 1000000);     /* make it MB */
607         return n;
608 }
609
610 /*
611  * Bits 10 of command_set_1 and cfs_enable_1 must be equal,
612  * so on non-buggy drives we need test only one.
613  * However, we should also check whether these fields are valid.
614  */
615 static inline int idedisk_supports_hpa(const struct hd_driveid *id)
616 {
617         return (id->command_set_1 & 0x0400) && (id->cfs_enable_1 & 0x0400);
618 }
619
620 /*
621  * The same here.
622  */
623 static inline int idedisk_supports_lba48(const struct hd_driveid *id)
624 {
625         return (id->command_set_2 & 0x0400) && (id->cfs_enable_2 & 0x0400)
626                && id->lba_capacity_2;
627 }
628
629 static inline void idedisk_check_hpa(ide_drive_t *drive)
630 {
631         unsigned long long capacity, set_max = 0;
632         int lba48 = idedisk_supports_lba48(drive->id);
633
634         
635         capacity = drive->capacity64;
636         if (lba48)
637                 set_max = idedisk_read_native_max_address_ext(drive);
638         if (set_max == 0)       /* LBA28 or LBA48 failed */
639                 set_max = idedisk_read_native_max_address(drive);
640
641         if (set_max <= capacity)
642                 return;
643
644         printk(KERN_INFO "%s: Host Protected Area detected.\n"
645                          "\tcurrent capacity is %llu sectors (%llu MB)\n"
646                          "\tnative  capacity is %llu sectors (%llu MB)\n",
647                          drive->name,
648                          capacity, sectors_to_MB(capacity),
649                          set_max, sectors_to_MB(set_max));
650
651         /* Some maxtor support LBA48 but do not accept LBA48  set max... */
652         if (lba48 || set_max < (1ULL << 28))
653                 set_max = idedisk_set_max_address_ext(drive, set_max);
654         else
655                 set_max = idedisk_set_max_address(drive, set_max);
656         if (set_max) {
657                 drive->capacity64 = set_max;
658                 printk(KERN_INFO "%s: Host Protected Area disabled.\n",
659                                  drive->name);
660         }
661 }
662
663 /*
664  * Compute drive->capacity, the full capacity of the drive
665  * Called with drive->id != NULL.
666  *
667  * To compute capacity, this uses either of
668  *
669  *    1. CHS value set by user       (whatever user sets will be trusted)
670  *    2. LBA value from target drive (require new ATA feature)
671  *    3. LBA value from system BIOS  (new one is OK, old one may break)
672  *    4. CHS value from system BIOS  (traditional style)
673  *
674  * in above order (i.e., if value of higher priority is available,
675  * reset will be ignored).
676  */
677 static void init_idedisk_capacity (ide_drive_t  *drive)
678 {
679         struct hd_driveid *id = drive->id;
680         /*
681          * If this drive supports the Host Protected Area feature set,
682          * then we may need to change our opinion about the drive's capacity.
683          */
684         int hpa = idedisk_supports_hpa(id);
685
686         if (idedisk_supports_lba48(id)) {
687                 /* drive speaks 48-bit LBA */
688                 drive->select.b.lba = 1;
689                 drive->capacity64 = id->lba_capacity_2;
690                 if (hpa)
691                         idedisk_check_hpa(drive);
692         } else if ((id->capability & 2) && lba_capacity_is_ok(id)) {
693                 /* drive speaks 28-bit LBA */
694                 drive->select.b.lba = 1;
695                 drive->capacity64 = id->lba_capacity;
696                 if (hpa)
697                         idedisk_check_hpa(drive);
698         } else {
699                 /* drive speaks boring old 28-bit CHS */
700                 drive->capacity64 = drive->cyl * drive->head * drive->sect;
701         }
702 }
703
704 static sector_t idedisk_capacity (ide_drive_t *drive)
705 {
706         return drive->capacity64 - drive->sect0;
707 }
708
709 #define IS_PDC4030_DRIVE        0
710
711 static ide_startstop_t idedisk_special (ide_drive_t *drive)
712 {
713         special_t *s = &drive->special;
714
715         if (s->b.set_geometry) {
716                 s->b.set_geometry       = 0;
717                 if (!IS_PDC4030_DRIVE) {
718                         ide_task_t args;
719                         memset(&args, 0, sizeof(ide_task_t));
720                         args.tfRegister[IDE_NSECTOR_OFFSET] = drive->sect;
721                         args.tfRegister[IDE_SECTOR_OFFSET]  = drive->sect;
722                         args.tfRegister[IDE_LCYL_OFFSET]    = drive->cyl;
723                         args.tfRegister[IDE_HCYL_OFFSET]    = drive->cyl>>8;
724                         args.tfRegister[IDE_SELECT_OFFSET]  = ((drive->head-1)|drive->select.all)&0xBF;
725                         args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SPECIFY;
726                         args.command_type = IDE_DRIVE_TASK_NO_DATA;
727                         args.handler      = &set_geometry_intr;
728                         do_rw_taskfile(drive, &args);
729                 }
730         } else if (s->b.recalibrate) {
731                 s->b.recalibrate = 0;
732                 if (!IS_PDC4030_DRIVE) {
733                         ide_task_t args;
734                         memset(&args, 0, sizeof(ide_task_t));
735                         args.tfRegister[IDE_NSECTOR_OFFSET] = drive->sect;
736                         args.tfRegister[IDE_COMMAND_OFFSET] = WIN_RESTORE;
737                         args.command_type = IDE_DRIVE_TASK_NO_DATA;
738                         args.handler      = &recal_intr;
739                         do_rw_taskfile(drive, &args);
740                 }
741         } else if (s->b.set_multmode) {
742                 s->b.set_multmode = 0;
743                 if (drive->mult_req > drive->id->max_multsect)
744                         drive->mult_req = drive->id->max_multsect;
745                 if (!IS_PDC4030_DRIVE) {
746                         ide_task_t args;
747                         memset(&args, 0, sizeof(ide_task_t));
748                         args.tfRegister[IDE_NSECTOR_OFFSET] = drive->mult_req;
749                         args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SETMULT;
750                         args.command_type = IDE_DRIVE_TASK_NO_DATA;
751                         args.handler      = &set_multmode_intr;
752                         do_rw_taskfile(drive, &args);
753                 }
754         } else if (s->all) {
755                 int special = s->all;
756                 s->all = 0;
757                 printk(KERN_ERR "%s: bad special flag: 0x%02x\n", drive->name, special);
758                 return ide_stopped;
759         }
760         return IS_PDC4030_DRIVE ? ide_stopped : ide_started;
761 }
762
763 static void idedisk_pre_reset (ide_drive_t *drive)
764 {
765         int legacy = (drive->id->cfs_enable_2 & 0x0400) ? 0 : 1;
766
767         drive->special.all = 0;
768         drive->special.b.set_geometry = legacy;
769         drive->special.b.recalibrate  = legacy;
770         if (OK_TO_RESET_CONTROLLER)
771                 drive->mult_count = 0;
772         if (!drive->keep_settings && !drive->using_dma)
773                 drive->mult_req = 0;
774         if (drive->mult_req != drive->mult_count)
775                 drive->special.b.set_multmode = 1;
776 }
777
778 #ifdef CONFIG_PROC_FS
779
780 static int smart_enable(ide_drive_t *drive)
781 {
782         ide_task_t args;
783
784         memset(&args, 0, sizeof(ide_task_t));
785         args.tfRegister[IDE_FEATURE_OFFSET]     = SMART_ENABLE;
786         args.tfRegister[IDE_LCYL_OFFSET]        = SMART_LCYL_PASS;
787         args.tfRegister[IDE_HCYL_OFFSET]        = SMART_HCYL_PASS;
788         args.tfRegister[IDE_COMMAND_OFFSET]     = WIN_SMART;
789         args.command_type                       = IDE_DRIVE_TASK_NO_DATA;
790         args.handler                            = &task_no_data_intr;
791         return ide_raw_taskfile(drive, &args, NULL);
792 }
793
794 static int get_smart_values(ide_drive_t *drive, u8 *buf)
795 {
796         ide_task_t args;
797
798         memset(&args, 0, sizeof(ide_task_t));
799         args.tfRegister[IDE_FEATURE_OFFSET]     = SMART_READ_VALUES;
800         args.tfRegister[IDE_NSECTOR_OFFSET]     = 0x01;
801         args.tfRegister[IDE_LCYL_OFFSET]        = SMART_LCYL_PASS;
802         args.tfRegister[IDE_HCYL_OFFSET]        = SMART_HCYL_PASS;
803         args.tfRegister[IDE_COMMAND_OFFSET]     = WIN_SMART;
804         args.command_type                       = IDE_DRIVE_TASK_IN;
805         args.data_phase                         = TASKFILE_IN;
806         args.handler                            = &task_in_intr;
807         (void) smart_enable(drive);
808         return ide_raw_taskfile(drive, &args, buf);
809 }
810
811 static int get_smart_thresholds(ide_drive_t *drive, u8 *buf)
812 {
813         ide_task_t args;
814         memset(&args, 0, sizeof(ide_task_t));
815         args.tfRegister[IDE_FEATURE_OFFSET]     = SMART_READ_THRESHOLDS;
816         args.tfRegister[IDE_NSECTOR_OFFSET]     = 0x01;
817         args.tfRegister[IDE_LCYL_OFFSET]        = SMART_LCYL_PASS;
818         args.tfRegister[IDE_HCYL_OFFSET]        = SMART_HCYL_PASS;
819         args.tfRegister[IDE_COMMAND_OFFSET]     = WIN_SMART;
820         args.command_type                       = IDE_DRIVE_TASK_IN;
821         args.data_phase                         = TASKFILE_IN;
822         args.handler                            = &task_in_intr;
823         (void) smart_enable(drive);
824         return ide_raw_taskfile(drive, &args, buf);
825 }
826
827 static int proc_idedisk_read_cache
828         (char *page, char **start, off_t off, int count, int *eof, void *data)
829 {
830         ide_drive_t     *drive;
831         char            *out = page;
832         int             len;
833
834         down(&ide_cfg_sem);
835         drive = ide_drive_from_key(data);
836         if (drive && drive->id_read)
837                 len = sprintf(out,"%i\n", drive->id->buf_size / 2);
838         else
839                 len = sprintf(out,"(none)\n");
840         up(&ide_cfg_sem);
841         PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
842 }
843
844 static int proc_idedisk_read_smart_thresholds
845         (char *page, char **start, off_t off, int count, int *eof, void *data)
846 {
847         ide_drive_t     *drive;
848         int             len = 0, i = 0;
849
850         down(&ide_cfg_sem);
851
852         drive = ide_drive_from_key(data);
853         if (drive && !get_smart_thresholds(drive, page)) {
854                 unsigned short *val = (unsigned short *) page;
855                 char *out = ((char *)val) + (SECTOR_WORDS * 4);
856                 page = out;
857                 do {
858                         out += sprintf(out, "%04x%c", le16_to_cpu(*val), (++i & 7) ? ' ' : '\n');
859                         val += 1;
860                 } while (i < (SECTOR_WORDS * 2));
861                 len = out - page;
862         }
863         up(&ide_cfg_sem);
864         PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
865 }
866
867 static int proc_idedisk_read_smart_values
868         (char *page, char **start, off_t off, int count, int *eof, void *data)
869 {
870         ide_drive_t     *drive;
871         int             len = 0, i = 0;
872
873         down(&ide_cfg_sem);
874
875         drive = ide_drive_from_key(data);
876         if (drive && !get_smart_values(drive, page)) {
877                 unsigned short *val = (unsigned short *) page;
878                 char *out = ((char *)val) + (SECTOR_WORDS * 4);
879                 page = out;
880                 do {
881                         out += sprintf(out, "%04x%c", le16_to_cpu(*val), (++i & 7) ? ' ' : '\n');
882                         val += 1;
883                 } while (i < (SECTOR_WORDS * 2));
884                 len = out - page;
885         }
886         up(&ide_cfg_sem);
887         PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
888 }
889
890 static ide_proc_entry_t idedisk_proc[] = {
891         { "cache",              S_IFREG|S_IRUGO,        proc_idedisk_read_cache,                NULL },
892         { "geometry",           S_IFREG|S_IRUGO,        proc_ide_read_geometry,                 NULL },
893         { "smart_values",       S_IFREG|S_IRUSR,        proc_idedisk_read_smart_values,         NULL },
894         { "smart_thresholds",   S_IFREG|S_IRUSR,        proc_idedisk_read_smart_thresholds,     NULL },
895         { NULL, 0, NULL, NULL }
896 };
897
898 #else
899
900 #define idedisk_proc    NULL
901
902 #endif  /* CONFIG_PROC_FS */
903
904 static int idedisk_issue_flush(request_queue_t *q, struct gendisk *disk,
905                                sector_t *error_sector)
906 {
907         ide_drive_t *drive = q->queuedata;
908         struct request *rq;
909         int ret;
910
911         if (!drive->wcache)
912                 return 0;
913
914         rq = blk_get_request(q, WRITE, __GFP_WAIT);
915
916         memset(rq->cmd, 0, sizeof(rq->cmd));
917
918         if (ide_id_has_flush_cache_ext(drive->id) &&
919             (drive->capacity64 >= (1UL << 28)))
920                 rq->cmd[0] = WIN_FLUSH_CACHE_EXT;
921         else
922                 rq->cmd[0] = WIN_FLUSH_CACHE;
923
924
925         rq->flags |= REQ_DRIVE_TASK | REQ_SOFTBARRIER;
926         rq->buffer = rq->cmd;
927
928         ret = blk_execute_rq(q, disk, rq);
929
930         /*
931          * if we failed and caller wants error offset, get it
932          */
933         if (ret && error_sector)
934                 *error_sector = ide_get_error_location(drive, rq->cmd);
935
936         blk_put_request(rq);
937         return ret;
938 }
939
940 /*
941  * This is tightly woven into the driver->do_special can not touch.
942  * DON'T do it again until a total personality rewrite is committed.
943  */
944 static int set_multcount(ide_drive_t *drive, int arg)
945 {
946         struct request rq;
947
948         if (drive->special.b.set_multmode)
949                 return -EBUSY;
950         ide_init_drive_cmd (&rq);
951         rq.flags = REQ_DRIVE_CMD;
952         drive->mult_req = arg;
953         drive->special.b.set_multmode = 1;
954         (void) ide_do_drive_cmd (drive, &rq, ide_wait);
955         return (drive->mult_count == arg) ? 0 : -EIO;
956 }
957
958 static int set_nowerr(ide_drive_t *drive, int arg)
959 {
960         if (ide_spin_wait_hwgroup(drive))
961                 return -EBUSY;
962         drive->nowerr = arg;
963         drive->bad_wstat = arg ? BAD_R_STAT : BAD_W_STAT;
964         spin_unlock_irq(&ide_lock);
965         return 0;
966 }
967
968 static int write_cache(ide_drive_t *drive, int arg)
969 {
970         ide_task_t args;
971         int err;
972
973         if (!ide_id_has_flush_cache(drive->id))
974                 return 1;
975
976         memset(&args, 0, sizeof(ide_task_t));
977         args.tfRegister[IDE_FEATURE_OFFSET]     = (arg) ?
978                         SETFEATURES_EN_WCACHE : SETFEATURES_DIS_WCACHE;
979         args.tfRegister[IDE_COMMAND_OFFSET]     = WIN_SETFEATURES;
980         args.command_type                       = IDE_DRIVE_TASK_NO_DATA;
981         args.handler                            = &task_no_data_intr;
982
983         err = ide_raw_taskfile(drive, &args, NULL);
984         if (err)
985                 return err;
986
987         drive->wcache = arg;
988         return 0;
989 }
990
991 static int do_idedisk_flushcache (ide_drive_t *drive)
992 {
993         ide_task_t args;
994
995         memset(&args, 0, sizeof(ide_task_t));
996         if (ide_id_has_flush_cache_ext(drive->id))
997                 args.tfRegister[IDE_COMMAND_OFFSET]     = WIN_FLUSH_CACHE_EXT;
998         else
999                 args.tfRegister[IDE_COMMAND_OFFSET]     = WIN_FLUSH_CACHE;
1000         args.command_type                       = IDE_DRIVE_TASK_NO_DATA;
1001         args.handler                            = &task_no_data_intr;
1002         return ide_raw_taskfile(drive, &args, NULL);
1003 }
1004
1005 static int set_acoustic (ide_drive_t *drive, int arg)
1006 {
1007         ide_task_t args;
1008
1009         memset(&args, 0, sizeof(ide_task_t));
1010         args.tfRegister[IDE_FEATURE_OFFSET]     = (arg) ? SETFEATURES_EN_AAM :
1011                                                           SETFEATURES_DIS_AAM;
1012         args.tfRegister[IDE_NSECTOR_OFFSET]     = arg;
1013         args.tfRegister[IDE_COMMAND_OFFSET]     = WIN_SETFEATURES;
1014         args.command_type = IDE_DRIVE_TASK_NO_DATA;
1015         args.handler      = &task_no_data_intr;
1016         ide_raw_taskfile(drive, &args, NULL);
1017         drive->acoustic = arg;
1018         return 0;
1019 }
1020
1021 /*
1022  * drive->addressing:
1023  *      0: 28-bit
1024  *      1: 48-bit
1025  *      2: 48-bit capable doing 28-bit
1026  */
1027 static int set_lba_addressing(ide_drive_t *drive, int arg)
1028 {
1029         drive->addressing =  0;
1030
1031         if (HWIF(drive)->no_lba48)
1032                 return 0;
1033
1034         if (!idedisk_supports_lba48(drive->id))
1035                 return -EIO;
1036         drive->addressing = arg;
1037         return 0;
1038 }
1039
1040 static void idedisk_add_settings(ide_drive_t *drive)
1041 {
1042         struct hd_driveid *id = drive->id;
1043
1044         ide_add_setting(drive,  "bios_cyl",             SETTING_RW,                                     -1,                     -1,                     TYPE_INT,       0,      65535,                          1,      1,      &drive->bios_cyl,               NULL);
1045         ide_add_setting(drive,  "bios_head",            SETTING_RW,                                     -1,                     -1,                     TYPE_BYTE,      0,      255,                            1,      1,      &drive->bios_head,              NULL);
1046         ide_add_setting(drive,  "bios_sect",            SETTING_RW,                                     -1,                     -1,                     TYPE_BYTE,      0,      63,                             1,      1,      &drive->bios_sect,              NULL);
1047         ide_add_setting(drive,  "address",              SETTING_RW,                                     HDIO_GET_ADDRESS,       HDIO_SET_ADDRESS,       TYPE_INTA,      0,      2,                              1,      1,      &drive->addressing,     set_lba_addressing);
1048         ide_add_setting(drive,  "bswap",                SETTING_READ,                                   -1,                     -1,                     TYPE_BYTE,      0,      1,                              1,      1,      &drive->bswap,                  NULL);
1049         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);
1050         ide_add_setting(drive,  "nowerr",               SETTING_RW,                                     HDIO_GET_NOWERR,        HDIO_SET_NOWERR,        TYPE_BYTE,      0,      1,                              1,      1,      &drive->nowerr,                 set_nowerr);
1051         ide_add_setting(drive,  "lun",                  SETTING_RW,                                     -1,                     -1,                     TYPE_INT,       0,      7,                              1,      1,      &drive->lun,                    NULL);
1052         ide_add_setting(drive,  "wcache",               SETTING_RW,                                     HDIO_GET_WCACHE,        HDIO_SET_WCACHE,        TYPE_BYTE,      0,      1,                              1,      1,      &drive->wcache,                 write_cache);
1053         ide_add_setting(drive,  "acoustic",             SETTING_RW,                                     HDIO_GET_ACOUSTIC,      HDIO_SET_ACOUSTIC,      TYPE_BYTE,      0,      254,                            1,      1,      &drive->acoustic,               set_acoustic);
1054         ide_add_setting(drive,  "failures",             SETTING_RW,                                     -1,                     -1,                     TYPE_INT,       0,      65535,                          1,      1,      &drive->failures,               NULL);
1055         ide_add_setting(drive,  "max_failures",         SETTING_RW,                                     -1,                     -1,                     TYPE_INT,       0,      65535,                          1,      1,      &drive->max_failures,           NULL);
1056 }
1057
1058 /*
1059  * Power Management state machine. This one is rather trivial for now,
1060  * we should probably add more, like switching back to PIO on suspend
1061  * to help some BIOSes, re-do the door locking on resume, etc...
1062  */
1063
1064 enum {
1065         idedisk_pm_flush_cache  = ide_pm_state_start_suspend,
1066         idedisk_pm_standby,
1067
1068         idedisk_pm_idle         = ide_pm_state_start_resume,
1069         idedisk_pm_restore_dma,
1070 };
1071
1072 static void idedisk_complete_power_step (ide_drive_t *drive, struct request *rq, u8 stat, u8 error)
1073 {
1074         switch (rq->pm->pm_step) {
1075         case idedisk_pm_flush_cache:    /* Suspend step 1 (flush cache) complete */
1076                 if (rq->pm->pm_state == 4)
1077                         rq->pm->pm_step = ide_pm_state_completed;
1078                 else
1079                         rq->pm->pm_step = idedisk_pm_standby;
1080                 break;
1081         case idedisk_pm_standby:        /* Suspend step 2 (standby) complete */
1082                 rq->pm->pm_step = ide_pm_state_completed;
1083                 break;
1084         case idedisk_pm_idle:           /* Resume step 1 (idle) complete */
1085                 rq->pm->pm_step = idedisk_pm_restore_dma;
1086                 break;
1087         }
1088 }
1089
1090 static ide_startstop_t idedisk_start_power_step (ide_drive_t *drive, struct request *rq)
1091 {
1092         ide_task_t *args = rq->special;
1093
1094         memset(args, 0, sizeof(*args));
1095
1096         switch (rq->pm->pm_step) {
1097         case idedisk_pm_flush_cache:    /* Suspend step 1 (flush cache) */
1098                 /* Not supported? Switch to next step now. */
1099                 if (!drive->wcache || !ide_id_has_flush_cache(drive->id)) {
1100                         idedisk_complete_power_step(drive, rq, 0, 0);
1101                         return ide_stopped;
1102                 }
1103                 if (ide_id_has_flush_cache_ext(drive->id))
1104                         args->tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE_EXT;
1105                 else
1106                         args->tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE;
1107                 args->command_type = IDE_DRIVE_TASK_NO_DATA;
1108                 args->handler      = &task_no_data_intr;
1109                 return do_rw_taskfile(drive, args);
1110
1111         case idedisk_pm_standby:        /* Suspend step 2 (standby) */
1112                 args->tfRegister[IDE_COMMAND_OFFSET] = WIN_STANDBYNOW1;
1113                 args->command_type = IDE_DRIVE_TASK_NO_DATA;
1114                 args->handler      = &task_no_data_intr;
1115                 return do_rw_taskfile(drive, args);
1116
1117         case idedisk_pm_idle:           /* Resume step 1 (idle) */
1118                 args->tfRegister[IDE_COMMAND_OFFSET] = WIN_IDLEIMMEDIATE;
1119                 args->command_type = IDE_DRIVE_TASK_NO_DATA;
1120                 args->handler = task_no_data_intr;
1121                 return do_rw_taskfile(drive, args);
1122
1123         case idedisk_pm_restore_dma:    /* Resume step 2 (restore DMA) */
1124                 /*
1125                  * Right now, all we do is call hwif->ide_dma_check(drive),
1126                  * we could be smarter and check for current xfer_speed
1127                  * in struct drive etc...
1128                  * Also, this step could be implemented as a generic helper
1129                  * as most subdrivers will use it
1130                  */
1131                 if ((drive->id->capability & 1) == 0)
1132                         break;
1133                 if (HWIF(drive)->ide_dma_check == NULL)
1134                         break;
1135                 HWIF(drive)->ide_dma_check(drive);
1136                 break;
1137         }
1138         rq->pm->pm_step = ide_pm_state_completed;
1139         return ide_stopped;
1140 }
1141
1142 static void idedisk_setup (ide_drive_t *drive)
1143 {
1144         struct hd_driveid *id = drive->id;
1145         unsigned long long capacity;
1146         int barrier;
1147
1148         idedisk_add_settings(drive);
1149
1150         if (drive->id_read == 0)
1151                 return;
1152
1153         /*
1154          * CompactFlash cards and their brethern look just like hard drives
1155          * to us, but they are removable and don't have a doorlock mechanism.
1156          */
1157         if (drive->removable && !(drive->is_flash)) {
1158                 /*
1159                  * Removable disks (eg. SYQUEST); ignore 'WD' drives 
1160                  */
1161                 if (id->model[0] != 'W' || id->model[1] != 'D') {
1162                         drive->doorlocking = 1;
1163                 }
1164         }
1165
1166         (void)set_lba_addressing(drive, 1);
1167
1168         if (drive->addressing == 1) {
1169                 ide_hwif_t *hwif = HWIF(drive);
1170                 int max_s = 2048;
1171
1172                 if (max_s > hwif->rqsize)
1173                         max_s = hwif->rqsize;
1174
1175                 blk_queue_max_sectors(drive->queue, max_s);
1176         }
1177
1178         printk(KERN_INFO "%s: max request size: %dKiB\n", drive->name, drive->queue->max_sectors / 2);
1179
1180         /* Extract geometry if we did not already have one for the drive */
1181         if (!drive->cyl || !drive->head || !drive->sect) {
1182                 drive->cyl     = drive->bios_cyl  = id->cyls;
1183                 drive->head    = drive->bios_head = id->heads;
1184                 drive->sect    = drive->bios_sect = id->sectors;
1185         }
1186
1187         /* Handle logical geometry translation by the drive */
1188         if ((id->field_valid & 1) && id->cur_cyls &&
1189             id->cur_heads && (id->cur_heads <= 16) && id->cur_sectors) {
1190                 drive->cyl  = id->cur_cyls;
1191                 drive->head = id->cur_heads;
1192                 drive->sect = id->cur_sectors;
1193         }
1194
1195         /* Use physical geometry if what we have still makes no sense */
1196         if (drive->head > 16 && id->heads && id->heads <= 16) {
1197                 drive->cyl  = id->cyls;
1198                 drive->head = id->heads;
1199                 drive->sect = id->sectors;
1200         }
1201
1202         /* calculate drive capacity, and select LBA if possible */
1203         init_idedisk_capacity (drive);
1204
1205         /* limit drive capacity to 137GB if LBA48 cannot be used */
1206         if (drive->addressing == 0 && drive->capacity64 > 1ULL << 28) {
1207                 printk(KERN_WARNING "%s: cannot use LBA48 - full capacity "
1208                        "%llu sectors (%llu MB)\n",
1209                        drive->name, (unsigned long long)drive->capacity64,
1210                        sectors_to_MB(drive->capacity64));
1211                 drive->capacity64 = 1ULL << 28;
1212         }
1213
1214         if (drive->hwif->no_lba48_dma && drive->addressing) {
1215                 if (drive->capacity64 > 1ULL << 28) {
1216                         printk(KERN_INFO "%s: cannot use LBA48 DMA - PIO mode will"
1217                                          " be used for accessing sectors > %u\n",
1218                                          drive->name, 1 << 28);
1219                 } else
1220                         drive->addressing = 0;
1221         }
1222
1223         /*
1224          * if possible, give fdisk access to more of the drive,
1225          * by correcting bios_cyls:
1226          */
1227         capacity = idedisk_capacity (drive);
1228         if (!drive->forced_geom) {
1229
1230                 if (idedisk_supports_lba48(drive->id)) {
1231                         /* compatibility */
1232                         drive->bios_sect = 63;
1233                         drive->bios_head = 255;
1234                 }
1235
1236                 if (drive->bios_sect && drive->bios_head) {
1237                         unsigned int cap0 = capacity; /* truncate to 32 bits */
1238                         unsigned int cylsz, cyl;
1239
1240                         if (cap0 != capacity)
1241                                 drive->bios_cyl = 65535;
1242                         else {
1243                                 cylsz = drive->bios_sect * drive->bios_head;
1244                                 cyl = cap0 / cylsz;
1245                                 if (cyl > 65535)
1246                                         cyl = 65535;
1247                                 if (cyl > drive->bios_cyl)
1248                                         drive->bios_cyl = cyl;
1249                         }
1250                 }
1251         }
1252         printk(KERN_INFO "%s: %llu sectors (%llu MB)",
1253                          drive->name, capacity, sectors_to_MB(capacity));
1254
1255         /* Only print cache size when it was specified */
1256         if (id->buf_size)
1257                 printk (" w/%dKiB Cache", id->buf_size/2);
1258
1259         if(drive->bios_cyl)
1260                 printk(", CHS=%d/%d/%d", 
1261                         drive->bios_cyl, drive->bios_head, drive->bios_sect);
1262         if (drive->using_dma)
1263                 ide_dma_verbose(drive);
1264         printk("\n");
1265
1266         drive->mult_count = 0;
1267         if (id->max_multsect) {
1268 #ifdef CONFIG_IDEDISK_MULTI_MODE
1269                 id->multsect = ((id->max_multsect/2) > 1) ? id->max_multsect : 0;
1270                 id->multsect_valid = id->multsect ? 1 : 0;
1271                 drive->mult_req = id->multsect_valid ? id->max_multsect : INITIAL_MULT_COUNT;
1272                 drive->special.b.set_multmode = drive->mult_req ? 1 : 0;
1273 #else   /* original, pre IDE-NFG, per request of AC */
1274                 drive->mult_req = INITIAL_MULT_COUNT;
1275                 if (drive->mult_req > id->max_multsect)
1276                         drive->mult_req = id->max_multsect;
1277                 if (drive->mult_req || ((id->multsect_valid & 1) && id->multsect))
1278                         drive->special.b.set_multmode = 1;
1279 #endif  /* CONFIG_IDEDISK_MULTI_MODE */
1280         }
1281         drive->no_io_32bit = id->dword_io ? 1 : 0;
1282
1283         /* write cache enabled? */
1284         if ((id->csfo & 1) || (id->cfs_enable_1 & (1 << 5)))
1285                 drive->wcache = 1;
1286
1287         write_cache(drive, 1);
1288
1289         /*
1290          * We must avoid issuing commands a drive does not understand
1291          * or we may crash it. We check flush cache is supported. We also
1292          * check we have the LBA48 flush cache if the drive capacity is
1293          * too large. By this time we have trimmed the drive capacity if
1294          * LBA48 is not available so we don't need to recheck that.
1295          */
1296         barrier = 0;
1297         if (ide_id_has_flush_cache(id))
1298                 barrier = 1;
1299         if (drive->addressing == 1) {
1300                 /* Can't issue the correct flush ? */
1301                 if (capacity > (1ULL << 28) && !ide_id_has_flush_cache_ext(id))
1302                         barrier = 0;
1303         }
1304
1305         printk(KERN_DEBUG "%s: cache flushes %ssupported\n",
1306                 drive->name, barrier ? "" : "not ");
1307         if (barrier) {
1308                 blk_queue_ordered(drive->queue, 1);
1309                 blk_queue_issue_flush_fn(drive->queue, idedisk_issue_flush);
1310         }
1311 }
1312
1313 static void ide_cacheflush_p(ide_drive_t *drive)
1314 {
1315         if (!drive->wcache || !ide_id_has_flush_cache(drive->id))
1316                 return;
1317
1318         if (do_idedisk_flushcache(drive))
1319                 printk(KERN_INFO "%s: wcache flush failed!\n", drive->name);
1320 }
1321
1322 static int idedisk_cleanup (ide_drive_t *drive)
1323 {
1324         struct gendisk *g = drive->disk;
1325         ide_cacheflush_p(drive);
1326         if (ide_unregister_subdriver(drive))
1327                 return 1;
1328         del_gendisk(g);
1329         drive->devfs_name[0] = '\0';
1330         g->fops = ide_fops;
1331         return 0;
1332 }
1333
1334 static int idedisk_attach(ide_drive_t *drive);
1335
1336 static void ide_device_shutdown(struct device *dev)
1337 {
1338         ide_drive_t *drive = container_of(dev, ide_drive_t, gendev);
1339
1340 #ifdef  CONFIG_ALPHA
1341         /* On Alpha, halt(8) doesn't actually turn the machine off,
1342            it puts you into the sort of firmware monitor. Typically,
1343            it's used to boot another kernel image, so it's not much
1344            different from reboot(8). Therefore, we don't need to
1345            spin down the disk in this case, especially since Alpha
1346            firmware doesn't handle disks in standby mode properly.
1347            On the other hand, it's reasonably safe to turn the power
1348            off when the shutdown process reaches the firmware prompt,
1349            as the firmware initialization takes rather long time -
1350            at least 10 seconds, which should be sufficient for
1351            the disk to expire its write cache. */
1352         if (system_state != SYSTEM_POWER_OFF) {
1353 #else
1354         if (system_state == SYSTEM_RESTART) {
1355 #endif
1356                 ide_cacheflush_p(drive);
1357                 return;
1358         }
1359
1360         printk("Shutdown: %s\n", drive->name);
1361         dev->bus->suspend(dev, PM_SUSPEND_STANDBY);
1362 }
1363
1364 /*
1365  *      IDE subdriver functions, registered with ide.c
1366  */
1367 static ide_driver_t idedisk_driver = {
1368         .owner                  = THIS_MODULE,
1369         .gen_driver = {
1370                 .shutdown       = ide_device_shutdown,
1371         },
1372         .name                   = "ide-disk",
1373         .version                = IDEDISK_VERSION,
1374         .media                  = ide_disk,
1375         .busy                   = 0,
1376         .supports_dsc_overlap   = 0,
1377         .cleanup                = idedisk_cleanup,
1378         .do_request             = ide_do_rw_disk,
1379         .sense                  = idedisk_dump_status,
1380         .error                  = idedisk_error,
1381         .abort                  = idedisk_abort,
1382         .pre_reset              = idedisk_pre_reset,
1383         .capacity               = idedisk_capacity,
1384         .special                = idedisk_special,
1385         .proc                   = idedisk_proc,
1386         .attach                 = idedisk_attach,
1387         .drives                 = LIST_HEAD_INIT(idedisk_driver.drives),
1388         .start_power_step       = idedisk_start_power_step,
1389         .complete_power_step    = idedisk_complete_power_step,
1390 };
1391
1392 static int idedisk_open(struct inode *inode, struct file *filp)
1393 {
1394         ide_drive_t *drive = inode->i_bdev->bd_disk->private_data;
1395         drive->usage++;
1396         if (drive->removable && drive->usage == 1) {
1397                 ide_task_t args;
1398                 memset(&args, 0, sizeof(ide_task_t));
1399                 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_DOORLOCK;
1400                 args.command_type = IDE_DRIVE_TASK_NO_DATA;
1401                 args.handler      = &task_no_data_intr;
1402                 check_disk_change(inode->i_bdev);
1403                 /*
1404                  * Ignore the return code from door_lock,
1405                  * since the open() has already succeeded,
1406                  * and the door_lock is irrelevant at this point.
1407                  */
1408                 if (drive->doorlocking && ide_raw_taskfile(drive, &args, NULL))
1409                         drive->doorlocking = 0;
1410         }
1411         return 0;
1412 }
1413
1414 static int idedisk_release(struct inode *inode, struct file *filp)
1415 {
1416         ide_drive_t *drive = inode->i_bdev->bd_disk->private_data;
1417         if (drive->usage == 1)
1418                 ide_cacheflush_p(drive);
1419         if (drive->removable && drive->usage == 1) {
1420                 ide_task_t args;
1421                 memset(&args, 0, sizeof(ide_task_t));
1422                 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_DOORUNLOCK;
1423                 args.command_type = IDE_DRIVE_TASK_NO_DATA;
1424                 args.handler      = &task_no_data_intr;
1425                 if (drive->doorlocking && ide_raw_taskfile(drive, &args, NULL))
1426                         drive->doorlocking = 0;
1427         }
1428         drive->usage--;
1429         return 0;
1430 }
1431
1432 static int idedisk_ioctl(struct inode *inode, struct file *file,
1433                         unsigned int cmd, unsigned long arg)
1434 {
1435         struct block_device *bdev = inode->i_bdev;
1436         return generic_ide_ioctl(file, bdev, cmd, arg);
1437 }
1438
1439 static int idedisk_media_changed(struct gendisk *disk)
1440 {
1441         ide_drive_t *drive = disk->private_data;
1442
1443         /* do not scan partitions twice if this is a removable device */
1444         if (drive->attach) {
1445                 drive->attach = 0;
1446                 return 0;
1447         }
1448         /* if removable, always assume it was changed */
1449         return drive->removable;
1450 }
1451
1452 static int idedisk_revalidate_disk(struct gendisk *disk)
1453 {
1454         ide_drive_t *drive = disk->private_data;
1455         set_capacity(disk, current_capacity(drive));
1456         return 0;
1457 }
1458
1459 static struct block_device_operations idedisk_ops = {
1460         .owner          = THIS_MODULE,
1461         .open           = idedisk_open,
1462         .release        = idedisk_release,
1463         .ioctl          = idedisk_ioctl,
1464         .media_changed  = idedisk_media_changed,
1465         .revalidate_disk= idedisk_revalidate_disk
1466 };
1467
1468 MODULE_DESCRIPTION("ATA DISK Driver");
1469
1470 static int idedisk_attach(ide_drive_t *drive)
1471 {
1472         struct gendisk *g = drive->disk;
1473
1474         /* strstr("foo", "") is non-NULL */
1475         if (!strstr("ide-disk", drive->driver_req))
1476                 goto failed;
1477         if (!drive->present)
1478                 goto failed;
1479         if (drive->media != ide_disk)
1480                 goto failed;
1481
1482         if (ide_register_subdriver(drive, &idedisk_driver)) {
1483                 printk (KERN_ERR "ide-disk: %s: Failed to register the driver with ide.c\n", drive->name);
1484                 goto failed;
1485         }
1486         DRIVER(drive)->busy++;
1487         idedisk_setup(drive);
1488         if ((!drive->head || drive->head > 16) && !drive->select.b.lba) {
1489                 printk(KERN_ERR "%s: INVALID GEOMETRY: %d PHYSICAL HEADS?\n",
1490                         drive->name, drive->head);
1491                 drive->attach = 0;
1492         } else
1493                 drive->attach = 1;
1494         DRIVER(drive)->busy--;
1495         g->minors = 1 << PARTN_BITS;
1496         strcpy(g->devfs_name, drive->devfs_name);
1497         g->driverfs_dev = &drive->gendev;
1498         g->flags = drive->removable ? GENHD_FL_REMOVABLE : 0;
1499         set_capacity(g, current_capacity(drive));
1500         g->fops = &idedisk_ops;
1501         add_disk(g);
1502         return 0;
1503 failed:
1504         return 1;
1505 }
1506
1507 static void __exit idedisk_exit (void)
1508 {
1509         ide_unregister_driver(&idedisk_driver);
1510 }
1511
1512 static int idedisk_init (void)
1513 {
1514         return ide_register_driver(&idedisk_driver);
1515 }
1516
1517 module_init(idedisk_init);
1518 module_exit(idedisk_exit);
1519 MODULE_LICENSE("GPL");