This commit was manufactured by cvs2svn to create tag
[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 /* FIXME: some day we shouldn't need to look in here! */
75
76 #include "legacy/pdc4030.h"
77
78 /*
79  * lba_capacity_is_ok() performs a sanity check on the claimed "lba_capacity"
80  * value for this drive (from its reported identification information).
81  *
82  * Returns:     1 if lba_capacity looks sensible
83  *              0 otherwise
84  *
85  * It is called only once for each drive.
86  */
87 static int lba_capacity_is_ok (struct hd_driveid *id)
88 {
89         unsigned long lba_sects, chs_sects, head, tail;
90
91         /* No non-LBA info .. so valid! */
92         if (id->cyls == 0)
93                 return 1;
94                 
95         /*
96          * The ATA spec tells large drives to return
97          * C/H/S = 16383/16/63 independent of their size.
98          * Some drives can be jumpered to use 15 heads instead of 16.
99          * Some drives can be jumpered to use 4092 cyls instead of 16383.
100          */
101         if ((id->cyls == 16383
102              || (id->cyls == 4092 && id->cur_cyls == 16383)) &&
103             id->sectors == 63 &&
104             (id->heads == 15 || id->heads == 16) &&
105             (id->lba_capacity >= 16383*63*id->heads))
106                 return 1;
107
108         lba_sects   = id->lba_capacity;
109         chs_sects   = id->cyls * id->heads * id->sectors;
110
111         /* perform a rough sanity check on lba_sects:  within 10% is OK */
112         if ((lba_sects - chs_sects) < chs_sects/10)
113                 return 1;
114
115         /* some drives have the word order reversed */
116         head = ((lba_sects >> 16) & 0xffff);
117         tail = (lba_sects & 0xffff);
118         lba_sects = (head | (tail << 16));
119         if ((lba_sects - chs_sects) < chs_sects/10) {
120                 id->lba_capacity = lba_sects;
121                 return 1;       /* lba_capacity is (now) good */
122         }
123
124         return 0;       /* lba_capacity value may be bad */
125 }
126
127 #ifndef CONFIG_IDE_TASKFILE_IO
128
129 /*
130  * read_intr() is the handler for disk read/multread interrupts
131  */
132 static ide_startstop_t read_intr (ide_drive_t *drive)
133 {
134         ide_hwif_t *hwif        = HWIF(drive);
135         u32 i = 0, nsect        = 0, msect = drive->mult_count;
136         struct request *rq;
137         unsigned long flags;
138         u8 stat;
139         char *to;
140
141         /* new way for dealing with premature shared PCI interrupts */
142         if (!OK_STAT(stat=hwif->INB(IDE_STATUS_REG),DATA_READY,BAD_R_STAT)) {
143                 if (stat & (ERR_STAT|DRQ_STAT)) {
144                         return DRIVER(drive)->error(drive, "read_intr", stat);
145                 }
146                 /* no data yet, so wait for another interrupt */
147                 ide_set_handler(drive, &read_intr, WAIT_CMD, NULL);
148                 return ide_started;
149         }
150         
151 read_next:
152         rq = HWGROUP(drive)->rq;
153         if (msect) {
154                 if ((nsect = rq->current_nr_sectors) > msect)
155                         nsect = msect;
156                 msect -= nsect;
157         } else
158                 nsect = 1;
159         to = ide_map_buffer(rq, &flags);
160         taskfile_input_data(drive, to, nsect * SECTOR_WORDS);
161 #ifdef DEBUG
162         printk("%s:  read: sectors(%ld-%ld), buffer=0x%08lx, remaining=%ld\n",
163                 drive->name, rq->sector, rq->sector+nsect-1,
164                 (unsigned long) rq->buffer+(nsect<<9), rq->nr_sectors-nsect);
165 #endif
166         ide_unmap_buffer(rq, to, &flags);
167         rq->sector += nsect;
168         rq->errors = 0;
169         i = (rq->nr_sectors -= nsect);
170         if (((long)(rq->current_nr_sectors -= nsect)) <= 0)
171                 ide_end_request(drive, 1, rq->hard_cur_sectors);
172         /*
173          * Another BH Page walker and DATA INTEGRITY Questioned on ERROR.
174          * If passed back up on multimode read, BAD DATA could be ACKED
175          * to FILE SYSTEMS above ...
176          */
177         if (i > 0) {
178                 if (msect)
179                         goto read_next;
180                 ide_set_handler(drive, &read_intr, WAIT_CMD, NULL);
181                 return ide_started;
182         }
183         return ide_stopped;
184 }
185
186 /*
187  * write_intr() is the handler for disk write interrupts
188  */
189 static ide_startstop_t write_intr (ide_drive_t *drive)
190 {
191         ide_hwgroup_t *hwgroup  = HWGROUP(drive);
192         ide_hwif_t *hwif        = HWIF(drive);
193         struct request *rq      = hwgroup->rq;
194         u32 i = 0;
195         u8 stat;
196
197         if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG),
198                         DRIVE_READY, drive->bad_wstat)) {
199                 printk("%s: write_intr error1: nr_sectors=%ld, stat=0x%02x\n",
200                         drive->name, rq->nr_sectors, stat);
201         } else {
202 #ifdef DEBUG
203                 printk("%s: write: sector %ld, buffer=0x%08lx, remaining=%ld\n",
204                         drive->name, rq->sector, (unsigned long) rq->buffer,
205                         rq->nr_sectors-1);
206 #endif
207                 if ((rq->nr_sectors == 1) ^ ((stat & DRQ_STAT) != 0)) {
208                         rq->sector++;
209                         rq->errors = 0;
210                         i = --rq->nr_sectors;
211                         --rq->current_nr_sectors;
212                         if (((long)rq->current_nr_sectors) <= 0)
213                                 ide_end_request(drive, 1, rq->hard_cur_sectors);
214                         if (i > 0) {
215                                 unsigned long flags;
216                                 char *to = ide_map_buffer(rq, &flags);
217                                 taskfile_output_data(drive, to, SECTOR_WORDS);
218                                 ide_unmap_buffer(rq, to, &flags);
219                                 ide_set_handler(drive, &write_intr, WAIT_CMD, NULL);
220                                 return ide_started;
221                         }
222                         return ide_stopped;
223                 }
224                 /* the original code did this here (?) */
225                 return ide_stopped;
226         }
227         return DRIVER(drive)->error(drive, "write_intr", stat);
228 }
229
230 /*
231  * ide_multwrite() transfers a block of up to mcount sectors of data
232  * to a drive as part of a disk multiple-sector write operation.
233  *
234  * Note that we may be called from two contexts - __ide_do_rw_disk() context
235  * and IRQ context. The IRQ can happen any time after we've output the
236  * full "mcount" number of sectors, so we must make sure we update the
237  * state _before_ we output the final part of the data!
238  *
239  * The update and return to BH is a BLOCK Layer Fakey to get more data
240  * to satisfy the hardware atomic segment.  If the hardware atomic segment
241  * is shorter or smaller than the BH segment then we should be OKAY.
242  * This is only valid if we can rewind the rq->current_nr_sectors counter.
243  */
244 static void ide_multwrite(ide_drive_t *drive, unsigned int mcount)
245 {
246         ide_hwgroup_t *hwgroup  = HWGROUP(drive);
247         struct request *rq      = &hwgroup->wrq;
248  
249         do {
250                 char *buffer;
251                 int nsect = rq->current_nr_sectors;
252                 unsigned long flags;
253  
254                 if (nsect > mcount)
255                         nsect = mcount;
256                 mcount -= nsect;
257                 buffer = ide_map_buffer(rq, &flags);
258
259                 rq->sector += nsect;
260                 rq->nr_sectors -= nsect;
261                 rq->current_nr_sectors -= nsect;
262
263                 /* Do we move to the next bh after this? */
264                 if (!rq->current_nr_sectors) {
265                         struct bio *bio = rq->bio;
266
267                         /*
268                          * only move to next bio, when we have processed
269                          * all bvecs in this one.
270                          */
271                         if (++bio->bi_idx >= bio->bi_vcnt) {
272                                 bio->bi_idx = bio->bi_vcnt - rq->nr_cbio_segments;
273                                 bio = bio->bi_next;
274                         }
275
276                         /* end early early we ran out of requests */
277                         if (!bio) {
278                                 mcount = 0;
279                         } else {
280                                 rq->bio = bio;
281                                 rq->nr_cbio_segments = bio_segments(bio);
282                                 rq->current_nr_sectors = bio_cur_sectors(bio);
283                                 rq->hard_cur_sectors = rq->current_nr_sectors;
284                         }
285                 }
286
287                 /*
288                  * Ok, we're all setup for the interrupt
289                  * re-entering us on the last transfer.
290                  */
291                 taskfile_output_data(drive, buffer, nsect<<7);
292                 ide_unmap_buffer(rq, buffer, &flags);
293         } while (mcount);
294 }
295
296 /*
297  * multwrite_intr() is the handler for disk multwrite interrupts
298  */
299 static ide_startstop_t multwrite_intr (ide_drive_t *drive)
300 {
301         ide_hwgroup_t *hwgroup  = HWGROUP(drive);
302         ide_hwif_t *hwif        = HWIF(drive);
303         struct request *rq      = &hwgroup->wrq;
304         struct bio *bio         = rq->bio;
305         u8 stat;
306
307         stat = hwif->INB(IDE_STATUS_REG);
308         if (OK_STAT(stat, DRIVE_READY, drive->bad_wstat)) {
309                 if (stat & DRQ_STAT) {
310                         /*
311                          *      The drive wants data. Remember rq is the copy
312                          *      of the request
313                          */
314                         if (rq->nr_sectors) {
315                                 ide_multwrite(drive, drive->mult_count);
316                                 ide_set_handler(drive, &multwrite_intr, WAIT_CMD, NULL);
317                                 return ide_started;
318                         }
319                 } else {
320                         /*
321                          *      If the copy has all the blocks completed then
322                          *      we can end the original request.
323                          */
324                         if (!rq->nr_sectors) {  /* all done? */
325                                 bio->bi_idx = bio->bi_vcnt - rq->nr_cbio_segments;
326                                 rq = hwgroup->rq;
327                                 ide_end_request(drive, 1, rq->nr_sectors);
328                                 return ide_stopped;
329                         }
330                 }
331                 bio->bi_idx = bio->bi_vcnt - rq->nr_cbio_segments;
332                 /* the original code did this here (?) */
333                 return ide_stopped;
334         }
335         bio->bi_idx = bio->bi_vcnt - rq->nr_cbio_segments;
336         return DRIVER(drive)->error(drive, "multwrite_intr", stat);
337 }
338
339 /*
340  * __ide_do_rw_disk() issues READ and WRITE commands to a disk,
341  * using LBA if supported, or CHS otherwise, to address sectors.
342  * It also takes care of issuing special DRIVE_CMDs.
343  */
344 ide_startstop_t __ide_do_rw_disk (ide_drive_t *drive, struct request *rq, sector_t block)
345 {
346         ide_hwif_t *hwif        = HWIF(drive);
347         unsigned int dma        = drive->using_dma;
348         u8 lba48                = (drive->addressing == 1) ? 1 : 0;
349         task_ioreg_t command    = WIN_NOP;
350         ata_nsector_t           nsectors;
351
352         nsectors.all            = (u16) rq->nr_sectors;
353
354         if (hwif->no_lba48_dma && lba48 && dma) {
355                 if (rq->sector + rq->nr_sectors > 1ULL << 28)
356                         dma = 0;
357         }
358
359         if (IDE_CONTROL_REG)
360                 hwif->OUTB(drive->ctl, IDE_CONTROL_REG);
361
362         if (drive->select.b.lba) {
363                 if (drive->addressing == 1) {
364                         task_ioreg_t tasklets[10];
365
366                         pr_debug("%s: LBA=0x%012llx\n", drive->name, block);
367
368                         tasklets[0] = 0;
369                         tasklets[1] = 0;
370                         tasklets[2] = nsectors.b.low;
371                         tasklets[3] = nsectors.b.high;
372                         tasklets[4] = (task_ioreg_t) block;
373                         tasklets[5] = (task_ioreg_t) (block>>8);
374                         tasklets[6] = (task_ioreg_t) (block>>16);
375                         tasklets[7] = (task_ioreg_t) (block>>24);
376                         if (sizeof(block) == 4) {
377                                 tasklets[8] = (task_ioreg_t) 0;
378                                 tasklets[9] = (task_ioreg_t) 0;
379                         } else {
380                                 tasklets[8] = (task_ioreg_t)((u64)block >> 32);
381                                 tasklets[9] = (task_ioreg_t)((u64)block >> 40);
382                         }
383 #ifdef DEBUG
384                         printk("%s: 0x%02x%02x 0x%02x%02x%02x%02x%02x%02x\n",
385                                 drive->name, tasklets[3], tasklets[2],
386                                 tasklets[9], tasklets[8], tasklets[7],
387                                 tasklets[6], tasklets[5], tasklets[4]);
388 #endif
389                         hwif->OUTB(tasklets[1], IDE_FEATURE_REG);
390                         hwif->OUTB(tasklets[3], IDE_NSECTOR_REG);
391                         hwif->OUTB(tasklets[7], IDE_SECTOR_REG);
392                         hwif->OUTB(tasklets[8], IDE_LCYL_REG);
393                         hwif->OUTB(tasklets[9], IDE_HCYL_REG);
394
395                         hwif->OUTB(tasklets[0], IDE_FEATURE_REG);
396                         hwif->OUTB(tasklets[2], IDE_NSECTOR_REG);
397                         hwif->OUTB(tasklets[4], IDE_SECTOR_REG);
398                         hwif->OUTB(tasklets[5], IDE_LCYL_REG);
399                         hwif->OUTB(tasklets[6], IDE_HCYL_REG);
400                         hwif->OUTB(0x00|drive->select.all,IDE_SELECT_REG);
401                 } else {
402                         hwif->OUTB(0x00, IDE_FEATURE_REG);
403                         hwif->OUTB(nsectors.b.low, IDE_NSECTOR_REG);
404                         hwif->OUTB(block, IDE_SECTOR_REG);
405                         hwif->OUTB(block>>=8, IDE_LCYL_REG);
406                         hwif->OUTB(block>>=8, IDE_HCYL_REG);
407                         hwif->OUTB(((block>>8)&0x0f)|drive->select.all,IDE_SELECT_REG);
408                 }
409         } else {
410                 unsigned int sect,head,cyl,track;
411                 track = (int)block / drive->sect;
412                 sect  = (int)block % drive->sect + 1;
413                 hwif->OUTB(sect, IDE_SECTOR_REG);
414                 head  = track % drive->head;
415                 cyl   = track / drive->head;
416
417                 pr_debug("%s: CHS=%u/%u/%u\n", drive->name, cyl, head, sect);
418
419                 hwif->OUTB(0x00, IDE_FEATURE_REG);
420                 hwif->OUTB(nsectors.b.low, IDE_NSECTOR_REG);
421                 hwif->OUTB(cyl, IDE_LCYL_REG);
422                 hwif->OUTB(cyl>>8, IDE_HCYL_REG);
423                 hwif->OUTB(head|drive->select.all,IDE_SELECT_REG);
424         }
425
426         if (rq_data_dir(rq) == READ) {
427                 if (dma && !hwif->ide_dma_read(drive))
428                         return ide_started;
429
430                 command = ((drive->mult_count) ?
431                            ((lba48) ? WIN_MULTREAD_EXT : WIN_MULTREAD) :
432                            ((lba48) ? WIN_READ_EXT : WIN_READ));
433                 ide_execute_command(drive, command, &read_intr, WAIT_CMD, NULL);
434                 return ide_started;
435         } else {
436                 ide_startstop_t startstop;
437
438                 if (dma && !hwif->ide_dma_write(drive))
439                         return ide_started;
440
441                 command = ((drive->mult_count) ?
442                            ((lba48) ? WIN_MULTWRITE_EXT : WIN_MULTWRITE) :
443                            ((lba48) ? WIN_WRITE_EXT : WIN_WRITE));
444                 hwif->OUTB(command, IDE_COMMAND_REG);
445
446                 if (ide_wait_stat(&startstop, drive, DATA_READY,
447                                 drive->bad_wstat, WAIT_DRQ)) {
448                         printk(KERN_ERR "%s: no DRQ after issuing %s\n",
449                                 drive->name,
450                                 drive->mult_count ? "MULTWRITE" : "WRITE");
451                         return startstop;
452                 }
453                 if (!drive->unmask)
454                         local_irq_disable();
455                 if (drive->mult_count) {
456                         ide_hwgroup_t *hwgroup = HWGROUP(drive);
457
458                         hwgroup->wrq = *rq; /* scratchpad */
459                         ide_set_handler(drive, &multwrite_intr, WAIT_CMD, NULL);
460                         ide_multwrite(drive, drive->mult_count);
461                 } else {
462                         unsigned long flags;
463                         char *to = ide_map_buffer(rq, &flags);
464                         ide_set_handler(drive, &write_intr, WAIT_CMD, NULL);
465                         taskfile_output_data(drive, to, SECTOR_WORDS);
466                         ide_unmap_buffer(rq, to, &flags);
467                 }
468                 return ide_started;
469         }
470 }
471 EXPORT_SYMBOL_GPL(__ide_do_rw_disk);
472
473 #else /* CONFIG_IDE_TASKFILE_IO */
474
475 static ide_startstop_t chs_rw_disk(ide_drive_t *, struct request *, unsigned long);
476 static ide_startstop_t lba_28_rw_disk(ide_drive_t *, struct request *, unsigned long);
477 static ide_startstop_t lba_48_rw_disk(ide_drive_t *, struct request *, unsigned long long);
478
479 /*
480  * __ide_do_rw_disk() issues READ and WRITE commands to a disk,
481  * using LBA if supported, or CHS otherwise, to address sectors.
482  * It also takes care of issuing special DRIVE_CMDs.
483  */
484 ide_startstop_t __ide_do_rw_disk (ide_drive_t *drive, struct request *rq, sector_t block)
485 {
486         /*
487          * 268435455  == 137439 MB or 28bit limit
488          *
489          * need to add split taskfile operations based on 28bit threshold.
490          */
491         if (drive->addressing == 1)             /* 48-bit LBA */
492                 return lba_48_rw_disk(drive, rq, (unsigned long long) block);
493         if (drive->select.b.lba)                /* 28-bit LBA */
494                 return lba_28_rw_disk(drive, rq, (unsigned long) block);
495
496         /* 28-bit CHS : DIE DIE DIE piece of legacy crap!!! */
497         return chs_rw_disk(drive, rq, (unsigned long) block);
498 }
499 EXPORT_SYMBOL_GPL(__ide_do_rw_disk);
500
501 static u8 get_command(ide_drive_t *drive, struct request *rq, ide_task_t *task)
502 {
503         unsigned int lba48 = (drive->addressing == 1) ? 1 : 0;
504         unsigned int dma = drive->using_dma;
505
506         if (drive->hwif->no_lba48_dma && lba48 && dma) {
507                 if (rq->sector + rq->nr_sectors > 1ULL << 28)
508                         dma = 0;
509         }
510
511         if (rq_data_dir(rq) == READ) {
512                 task->command_type = IDE_DRIVE_TASK_IN;
513                 if (dma)
514                         return lba48 ? WIN_READDMA_EXT : WIN_READDMA;
515                 task->handler = &task_in_intr;
516                 if (drive->mult_count) {
517                         task->data_phase = TASKFILE_MULTI_IN;
518                         return lba48 ? WIN_MULTREAD_EXT : WIN_MULTREAD;
519                 }
520                 task->data_phase = TASKFILE_IN;
521                 return lba48 ? WIN_READ_EXT : WIN_READ;
522         } else {
523                 task->command_type = IDE_DRIVE_TASK_RAW_WRITE;
524                 if (dma)
525                         return lba48 ? WIN_WRITEDMA_EXT : WIN_WRITEDMA;
526                 task->prehandler = &pre_task_out_intr;
527                 task->handler = &task_out_intr;
528                 if (drive->mult_count) {
529                         task->data_phase = TASKFILE_MULTI_OUT;
530                         return lba48 ? WIN_MULTWRITE_EXT : WIN_MULTWRITE;
531                 }
532                 task->data_phase = TASKFILE_OUT;
533                 return lba48 ? WIN_WRITE_EXT : WIN_WRITE;
534         }
535 }
536
537 static ide_startstop_t chs_rw_disk (ide_drive_t *drive, struct request *rq, unsigned long block)
538 {
539         ide_task_t              args;
540         int                     sectors;
541         ata_nsector_t           nsectors;
542         unsigned int track      = (block / drive->sect);
543         unsigned int sect       = (block % drive->sect) + 1;
544         unsigned int head       = (track % drive->head);
545         unsigned int cyl        = (track / drive->head);
546
547         nsectors.all = (u16) rq->nr_sectors;
548
549         pr_debug("%s: CHS=%u/%u/%u\n", drive->name, cyl, head, sect);
550
551         memset(&args, 0, sizeof(ide_task_t));
552
553         sectors = (rq->nr_sectors == 256) ? 0x00 : rq->nr_sectors;
554
555         args.tfRegister[IDE_NSECTOR_OFFSET]     = sectors;
556         args.tfRegister[IDE_SECTOR_OFFSET]      = sect;
557         args.tfRegister[IDE_LCYL_OFFSET]        = cyl;
558         args.tfRegister[IDE_HCYL_OFFSET]        = (cyl>>8);
559         args.tfRegister[IDE_SELECT_OFFSET]      = head;
560         args.tfRegister[IDE_SELECT_OFFSET]      |= drive->select.all;
561         args.tfRegister[IDE_COMMAND_OFFSET]     = get_command(drive, rq, &args);
562         args.rq                                 = (struct request *) rq;
563         rq->special                             = (ide_task_t *)&args;
564         drive->hwif->data_phase = args.data_phase;
565         return do_rw_taskfile(drive, &args);
566 }
567
568 static ide_startstop_t lba_28_rw_disk (ide_drive_t *drive, struct request *rq, unsigned long block)
569 {
570         ide_task_t              args;
571         int                     sectors;
572         ata_nsector_t           nsectors;
573
574         nsectors.all = (u16) rq->nr_sectors;
575
576         memset(&args, 0, sizeof(ide_task_t));
577
578         sectors = (rq->nr_sectors == 256) ? 0x00 : rq->nr_sectors;
579
580         args.tfRegister[IDE_NSECTOR_OFFSET]     = sectors;
581         args.tfRegister[IDE_SECTOR_OFFSET]      = block;
582         args.tfRegister[IDE_LCYL_OFFSET]        = (block>>=8);
583         args.tfRegister[IDE_HCYL_OFFSET]        = (block>>=8);
584         args.tfRegister[IDE_SELECT_OFFSET]      = ((block>>8)&0x0f);
585         args.tfRegister[IDE_SELECT_OFFSET]      |= drive->select.all;
586         args.tfRegister[IDE_COMMAND_OFFSET]     = get_command(drive, rq, &args);
587         args.rq                                 = (struct request *) rq;
588         rq->special                             = (ide_task_t *)&args;
589         drive->hwif->data_phase = args.data_phase;
590         return do_rw_taskfile(drive, &args);
591 }
592
593 /*
594  * 268435455  == 137439 MB or 28bit limit
595  * 320173056  == 163929 MB or 48bit addressing
596  * 1073741822 == 549756 MB or 48bit addressing fake drive
597  */
598
599 static ide_startstop_t lba_48_rw_disk (ide_drive_t *drive, struct request *rq, unsigned long long block)
600 {
601         ide_task_t              args;
602         int                     sectors;
603         ata_nsector_t           nsectors;
604
605         nsectors.all = (u16) rq->nr_sectors;
606
607         memset(&args, 0, sizeof(ide_task_t));
608
609         sectors = (rq->nr_sectors == 65536) ? 0 : rq->nr_sectors;
610
611         args.tfRegister[IDE_NSECTOR_OFFSET]     = sectors;
612         args.hobRegister[IDE_NSECTOR_OFFSET]    = sectors >> 8;
613         args.tfRegister[IDE_SECTOR_OFFSET]      = block;        /* low lba */
614         args.tfRegister[IDE_LCYL_OFFSET]        = (block>>=8);  /* mid lba */
615         args.tfRegister[IDE_HCYL_OFFSET]        = (block>>=8);  /* hi  lba */
616         args.tfRegister[IDE_SELECT_OFFSET]      = drive->select.all;
617         args.tfRegister[IDE_COMMAND_OFFSET]     = get_command(drive, rq, &args);
618         args.hobRegister[IDE_SECTOR_OFFSET]     = (block>>=8);  /* low lba */
619         args.hobRegister[IDE_LCYL_OFFSET]       = (block>>=8);  /* mid lba */
620         args.hobRegister[IDE_HCYL_OFFSET]       = (block>>=8);  /* hi  lba */
621         args.hobRegister[IDE_SELECT_OFFSET]     = drive->select.all;
622         args.hobRegister[IDE_CONTROL_OFFSET_HOB]= (drive->ctl|0x80);
623         args.rq                                 = (struct request *) rq;
624         rq->special                             = (ide_task_t *)&args;
625         drive->hwif->data_phase = args.data_phase;
626         return do_rw_taskfile(drive, &args);
627 }
628
629 #endif /* CONFIG_IDE_TASKFILE_IO */
630
631 static ide_startstop_t ide_do_rw_disk (ide_drive_t *drive, struct request *rq, sector_t block)
632 {
633         ide_hwif_t *hwif = HWIF(drive);
634
635         BUG_ON(drive->blocked);
636
637         if (!blk_fs_request(rq)) {
638                 blk_dump_rq_flags(rq, "ide_do_rw_disk - bad command");
639                 ide_end_request(drive, 0, 0);
640                 return ide_stopped;
641         }
642
643         pr_debug("%s: %sing: block=%llu, sectors=%lu, buffer=0x%08lx\n",
644                  drive->name, rq_data_dir(rq) == READ ? "read" : "writ",
645                  block, rq->nr_sectors, (unsigned long)rq->buffer);
646
647         if (hwif->rw_disk)
648                 return hwif->rw_disk(drive, rq, block);
649         else
650                 return __ide_do_rw_disk(drive, rq, block);
651 }
652
653 static u8 idedisk_dump_status (ide_drive_t *drive, const char *msg, u8 stat)
654 {
655         ide_hwif_t *hwif = HWIF(drive);
656         unsigned long flags;
657         u8 err = 0;
658
659         local_irq_set(flags);
660         printk("%s: %s: status=0x%02x", drive->name, msg, stat);
661         printk(" { ");
662         if (stat & BUSY_STAT)
663                 printk("Busy ");
664         else {
665                 if (stat & READY_STAT)  printk("DriveReady ");
666                 if (stat & WRERR_STAT)  printk("DeviceFault ");
667                 if (stat & SEEK_STAT)   printk("SeekComplete ");
668                 if (stat & DRQ_STAT)    printk("DataRequest ");
669                 if (stat & ECC_STAT)    printk("CorrectedError ");
670                 if (stat & INDEX_STAT)  printk("Index ");
671                 if (stat & ERR_STAT)    printk("Error ");
672         }
673         printk("}");
674         printk("\n");
675         if ((stat & (BUSY_STAT|ERR_STAT)) == ERR_STAT) {
676                 err = hwif->INB(IDE_ERROR_REG);
677                 printk("%s: %s: error=0x%02x", drive->name, msg, err);
678                 printk(" { ");
679                 if (err & ABRT_ERR)     printk("DriveStatusError ");
680                 if (err & ICRC_ERR)
681                         printk("Bad%s ", (err & ABRT_ERR) ? "CRC" : "Sector");
682                 if (err & ECC_ERR)      printk("UncorrectableError ");
683                 if (err & ID_ERR)       printk("SectorIdNotFound ");
684                 if (err & TRK0_ERR)     printk("TrackZeroNotFound ");
685                 if (err & MARK_ERR)     printk("AddrMarkNotFound ");
686                 printk("}");
687                 if ((err & (BBD_ERR | ABRT_ERR)) == BBD_ERR ||
688                     (err & (ECC_ERR|ID_ERR|MARK_ERR))) {
689                         if (drive->addressing == 1) {
690                                 __u64 sectors = 0;
691                                 u32 low = 0, high = 0;
692                                 low = ide_read_24(drive);
693                                 hwif->OUTB(drive->ctl|0x80, IDE_CONTROL_REG);
694                                 high = ide_read_24(drive);
695                                 sectors = ((__u64)high << 24) | low;
696                                 printk(", LBAsect=%llu, high=%d, low=%d",
697                                        (unsigned long long) sectors,
698                                        high, low);
699                         } else {
700                                 u8 cur = hwif->INB(IDE_SELECT_REG);
701                                 if (cur & 0x40) {       /* using LBA? */
702                                         printk(", LBAsect=%ld", (unsigned long)
703                                          ((cur&0xf)<<24)
704                                          |(hwif->INB(IDE_HCYL_REG)<<16)
705                                          |(hwif->INB(IDE_LCYL_REG)<<8)
706                                          | hwif->INB(IDE_SECTOR_REG));
707                                 } else {
708                                         printk(", CHS=%d/%d/%d",
709                                          (hwif->INB(IDE_HCYL_REG)<<8) +
710                                           hwif->INB(IDE_LCYL_REG),
711                                           cur & 0xf,
712                                           hwif->INB(IDE_SECTOR_REG));
713                                 }
714                         }
715                         if (HWGROUP(drive) && HWGROUP(drive)->rq)
716                                 printk(", sector=%llu",
717                                         (unsigned long long)HWGROUP(drive)->rq->sector);
718                 }
719         }
720         printk("\n");
721         {
722                 struct request *rq;
723                 unsigned char opcode = 0;
724                 int found = 0;
725
726                 spin_lock(&ide_lock);
727                 rq = HWGROUP(drive)->rq;
728                 spin_unlock(&ide_lock);
729                 if (!rq)
730                         goto out;
731                 if (rq->flags & (REQ_DRIVE_CMD | REQ_DRIVE_TASK)) {
732                         char *args = rq->buffer;
733                         if (args) {
734                                 opcode = args[0];
735                                 found = 1;
736                         }
737                 } else if (rq->flags & REQ_DRIVE_TASKFILE) {
738                         ide_task_t *args = rq->special;
739                         if (args) {
740                                 task_struct_t *tf = (task_struct_t *) args->tfRegister;
741                                 opcode = tf->command;
742                                 found = 1;
743                         }
744                 }
745                 printk("ide: failed opcode was: ");
746                 if (!found)
747                         printk("unknown\n");
748                 else
749                         printk("0x%02x\n", opcode);
750         }
751 out:
752         local_irq_restore(flags);
753         return err;
754 }
755
756 ide_startstop_t idedisk_error (ide_drive_t *drive, const char *msg, u8 stat)
757 {
758         ide_hwif_t *hwif;
759         struct request *rq;
760         u8 err;
761
762         err = idedisk_dump_status(drive, msg, stat);
763
764         if (drive == NULL || (rq = HWGROUP(drive)->rq) == NULL)
765                 return ide_stopped;
766
767         hwif = HWIF(drive);
768         /* retry only "normal" I/O: */
769         if (rq->flags & (REQ_DRIVE_CMD | REQ_DRIVE_TASK | REQ_DRIVE_TASKFILE)) {
770                 rq->errors = 1;
771                 ide_end_drive_cmd(drive, stat, err);
772                 return ide_stopped;
773         }
774 #ifdef CONFIG_IDE_TASKFILE_IO
775         /* make rq completion pointers new submission pointers */
776         blk_rq_prep_restart(rq);
777 #endif
778
779         if (stat & BUSY_STAT || ((stat & WRERR_STAT) && !drive->nowerr)) {
780                 /* other bits are useless when BUSY */
781                 rq->errors |= ERROR_RESET;
782         } else if (stat & ERR_STAT) {
783                 /* err has different meaning on cdrom and tape */
784                 if (err == ABRT_ERR) {
785                         if (drive->select.b.lba &&
786                             /* some newer drives don't support WIN_SPECIFY */
787                             hwif->INB(IDE_COMMAND_REG) == WIN_SPECIFY)
788                                 return ide_stopped;
789                 } else if ((err & BAD_CRC) == BAD_CRC) {
790                         /* UDMA crc error, just retry the operation */
791                         drive->crc_count++;
792                 } else if (err & (BBD_ERR | ECC_ERR)) {
793                         /* retries won't help these */
794                         rq->errors = ERROR_MAX;
795                 } else if (err & TRK0_ERR) {
796                         /* help it find track zero */
797                         rq->errors |= ERROR_RECAL;
798                 }
799         }
800         if ((stat & DRQ_STAT) && rq_data_dir(rq) == READ)
801                 try_to_flush_leftover_data(drive);
802         if (hwif->INB(IDE_STATUS_REG) & (BUSY_STAT|DRQ_STAT)) {
803                 /* force an abort */
804                 hwif->OUTB(WIN_IDLEIMMEDIATE,IDE_COMMAND_REG);
805         }
806         if (rq->errors >= ERROR_MAX || blk_noretry_request(rq))
807                 DRIVER(drive)->end_request(drive, 0, 0);
808         else {
809                 if ((rq->errors & ERROR_RESET) == ERROR_RESET) {
810                         ++rq->errors;
811                         return ide_do_reset(drive);
812                 }
813                 if ((rq->errors & ERROR_RECAL) == ERROR_RECAL)
814                         drive->special.b.recalibrate = 1;
815                 ++rq->errors;
816         }
817         return ide_stopped;
818 }
819
820 ide_startstop_t idedisk_abort(ide_drive_t *drive, const char *msg)
821 {
822         ide_hwif_t *hwif;
823         struct request *rq;
824
825         if (drive == NULL || (rq = HWGROUP(drive)->rq) == NULL)
826                 return ide_stopped;
827
828         hwif = HWIF(drive);
829
830         if (rq->flags & (REQ_DRIVE_CMD | REQ_DRIVE_TASK | REQ_DRIVE_TASKFILE)) {
831                 rq->errors = 1;
832                 ide_end_drive_cmd(drive, BUSY_STAT, 0);
833                 return ide_stopped;
834         }
835
836         DRIVER(drive)->end_request(drive, 0, 0);
837         return ide_stopped;
838 }
839
840 /*
841  * Queries for true maximum capacity of the drive.
842  * Returns maximum LBA address (> 0) of the drive, 0 if failed.
843  */
844 static unsigned long idedisk_read_native_max_address(ide_drive_t *drive)
845 {
846         ide_task_t args;
847         unsigned long addr = 0;
848
849         /* Create IDE/ATA command request structure */
850         memset(&args, 0, sizeof(ide_task_t));
851         args.tfRegister[IDE_SELECT_OFFSET]      = 0x40;
852         args.tfRegister[IDE_COMMAND_OFFSET]     = WIN_READ_NATIVE_MAX;
853         args.command_type                       = IDE_DRIVE_TASK_NO_DATA;
854         args.handler                            = &task_no_data_intr;
855         /* submit command request */
856         ide_raw_taskfile(drive, &args, NULL);
857
858         /* if OK, compute maximum address value */
859         if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
860                 addr = ((args.tfRegister[IDE_SELECT_OFFSET] & 0x0f) << 24)
861                      | ((args.tfRegister[  IDE_HCYL_OFFSET]       ) << 16)
862                      | ((args.tfRegister[  IDE_LCYL_OFFSET]       ) <<  8)
863                      | ((args.tfRegister[IDE_SECTOR_OFFSET]       ));
864                 addr++; /* since the return value is (maxlba - 1), we add 1 */
865         }
866         return addr;
867 }
868
869 static unsigned long long idedisk_read_native_max_address_ext(ide_drive_t *drive)
870 {
871         ide_task_t args;
872         unsigned long long addr = 0;
873
874         /* Create IDE/ATA command request structure */
875         memset(&args, 0, sizeof(ide_task_t));
876
877         args.tfRegister[IDE_SELECT_OFFSET]      = 0x40;
878         args.tfRegister[IDE_COMMAND_OFFSET]     = WIN_READ_NATIVE_MAX_EXT;
879         args.command_type                       = IDE_DRIVE_TASK_NO_DATA;
880         args.handler                            = &task_no_data_intr;
881         /* submit command request */
882         ide_raw_taskfile(drive, &args, NULL);
883
884         /* if OK, compute maximum address value */
885         if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
886                 u32 high = (args.hobRegister[IDE_HCYL_OFFSET] << 16) |
887                            (args.hobRegister[IDE_LCYL_OFFSET] <<  8) |
888                             args.hobRegister[IDE_SECTOR_OFFSET];
889                 u32 low  = ((args.tfRegister[IDE_HCYL_OFFSET])<<16) |
890                            ((args.tfRegister[IDE_LCYL_OFFSET])<<8) |
891                             (args.tfRegister[IDE_SECTOR_OFFSET]);
892                 addr = ((__u64)high << 24) | low;
893                 addr++; /* since the return value is (maxlba - 1), we add 1 */
894         }
895         return addr;
896 }
897
898 /*
899  * Sets maximum virtual LBA address of the drive.
900  * Returns new maximum virtual LBA address (> 0) or 0 on failure.
901  */
902 static unsigned long idedisk_set_max_address(ide_drive_t *drive, unsigned long addr_req)
903 {
904         ide_task_t args;
905         unsigned long addr_set = 0;
906         
907         addr_req--;
908         /* Create IDE/ATA command request structure */
909         memset(&args, 0, sizeof(ide_task_t));
910         args.tfRegister[IDE_SECTOR_OFFSET]      = ((addr_req >>  0) & 0xff);
911         args.tfRegister[IDE_LCYL_OFFSET]        = ((addr_req >>  8) & 0xff);
912         args.tfRegister[IDE_HCYL_OFFSET]        = ((addr_req >> 16) & 0xff);
913         args.tfRegister[IDE_SELECT_OFFSET]      = ((addr_req >> 24) & 0x0f) | 0x40;
914         args.tfRegister[IDE_COMMAND_OFFSET]     = WIN_SET_MAX;
915         args.command_type                       = IDE_DRIVE_TASK_NO_DATA;
916         args.handler                            = &task_no_data_intr;
917         /* submit command request */
918         ide_raw_taskfile(drive, &args, NULL);
919         /* if OK, read new maximum address value */
920         if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
921                 addr_set = ((args.tfRegister[IDE_SELECT_OFFSET] & 0x0f) << 24)
922                          | ((args.tfRegister[  IDE_HCYL_OFFSET]       ) << 16)
923                          | ((args.tfRegister[  IDE_LCYL_OFFSET]       ) <<  8)
924                          | ((args.tfRegister[IDE_SECTOR_OFFSET]       ));
925                 addr_set++;
926         }
927         return addr_set;
928 }
929
930 static unsigned long long idedisk_set_max_address_ext(ide_drive_t *drive, unsigned long long addr_req)
931 {
932         ide_task_t args;
933         unsigned long long addr_set = 0;
934
935         addr_req--;
936         /* Create IDE/ATA command request structure */
937         memset(&args, 0, sizeof(ide_task_t));
938         args.tfRegister[IDE_SECTOR_OFFSET]      = ((addr_req >>  0) & 0xff);
939         args.tfRegister[IDE_LCYL_OFFSET]        = ((addr_req >>= 8) & 0xff);
940         args.tfRegister[IDE_HCYL_OFFSET]        = ((addr_req >>= 8) & 0xff);
941         args.tfRegister[IDE_SELECT_OFFSET]      = 0x40;
942         args.tfRegister[IDE_COMMAND_OFFSET]     = WIN_SET_MAX_EXT;
943         args.hobRegister[IDE_SECTOR_OFFSET]     = (addr_req >>= 8) & 0xff;
944         args.hobRegister[IDE_LCYL_OFFSET]       = (addr_req >>= 8) & 0xff;
945         args.hobRegister[IDE_HCYL_OFFSET]       = (addr_req >>= 8) & 0xff;
946         args.hobRegister[IDE_SELECT_OFFSET]     = 0x40;
947         args.hobRegister[IDE_CONTROL_OFFSET_HOB]= (drive->ctl|0x80);
948         args.command_type                       = IDE_DRIVE_TASK_NO_DATA;
949         args.handler                            = &task_no_data_intr;
950         /* submit command request */
951         ide_raw_taskfile(drive, &args, NULL);
952         /* if OK, compute maximum address value */
953         if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
954                 u32 high = (args.hobRegister[IDE_HCYL_OFFSET] << 16) |
955                            (args.hobRegister[IDE_LCYL_OFFSET] <<  8) |
956                             args.hobRegister[IDE_SECTOR_OFFSET];
957                 u32 low  = ((args.tfRegister[IDE_HCYL_OFFSET])<<16) |
958                            ((args.tfRegister[IDE_LCYL_OFFSET])<<8) |
959                             (args.tfRegister[IDE_SECTOR_OFFSET]);
960                 addr_set = ((__u64)high << 24) | low;
961                 addr_set++;
962         }
963         return addr_set;
964 }
965
966 static unsigned long long sectors_to_MB(unsigned long long n)
967 {
968         n <<= 9;                /* make it bytes */
969         do_div(n, 1000000);     /* make it MB */
970         return n;
971 }
972
973 /*
974  * Bits 10 of command_set_1 and cfs_enable_1 must be equal,
975  * so on non-buggy drives we need test only one.
976  * However, we should also check whether these fields are valid.
977  */
978 static inline int idedisk_supports_hpa(const struct hd_driveid *id)
979 {
980         return (id->command_set_1 & 0x0400) && (id->cfs_enable_1 & 0x0400);
981 }
982
983 /*
984  * The same here.
985  */
986 static inline int idedisk_supports_lba48(const struct hd_driveid *id)
987 {
988         return (id->command_set_2 & 0x0400) && (id->cfs_enable_2 & 0x0400)
989                && id->lba_capacity_2;
990 }
991
992 static inline void idedisk_check_hpa(ide_drive_t *drive)
993 {
994         unsigned long long capacity, set_max;
995         int lba48 = idedisk_supports_lba48(drive->id);
996
997         capacity = drive->capacity64;
998         if (lba48)
999                 set_max = idedisk_read_native_max_address_ext(drive);
1000         else
1001                 set_max = idedisk_read_native_max_address(drive);
1002
1003         if (set_max <= capacity)
1004                 return;
1005
1006         printk(KERN_INFO "%s: Host Protected Area detected.\n"
1007                          "\tcurrent capacity is %llu sectors (%llu MB)\n"
1008                          "\tnative  capacity is %llu sectors (%llu MB)\n",
1009                          drive->name,
1010                          capacity, sectors_to_MB(capacity),
1011                          set_max, sectors_to_MB(set_max));
1012
1013         if (!drive->stroke)
1014                 return;
1015
1016         if (lba48)
1017                 set_max = idedisk_set_max_address_ext(drive, set_max);
1018         else
1019                 set_max = idedisk_set_max_address(drive, set_max);
1020         if (set_max) {
1021                 drive->capacity64 = set_max;
1022                 printk(KERN_INFO "%s: Host Protected Area disabled.\n",
1023                                  drive->name);
1024         }
1025 }
1026
1027 /*
1028  * Compute drive->capacity, the full capacity of the drive
1029  * Called with drive->id != NULL.
1030  *
1031  * To compute capacity, this uses either of
1032  *
1033  *    1. CHS value set by user       (whatever user sets will be trusted)
1034  *    2. LBA value from target drive (require new ATA feature)
1035  *    3. LBA value from system BIOS  (new one is OK, old one may break)
1036  *    4. CHS value from system BIOS  (traditional style)
1037  *
1038  * in above order (i.e., if value of higher priority is available,
1039  * reset will be ignored).
1040  */
1041 static void init_idedisk_capacity (ide_drive_t  *drive)
1042 {
1043         struct hd_driveid *id = drive->id;
1044         /*
1045          * If this drive supports the Host Protected Area feature set,
1046          * then we may need to change our opinion about the drive's capacity.
1047          */
1048         int hpa = idedisk_supports_hpa(id);
1049
1050         if (idedisk_supports_lba48(id)) {
1051                 /* drive speaks 48-bit LBA */
1052                 drive->select.b.lba = 1;
1053                 drive->capacity64 = id->lba_capacity_2;
1054                 if (hpa)
1055                         idedisk_check_hpa(drive);
1056         } else if ((id->capability & 2) && lba_capacity_is_ok(id)) {
1057                 /* drive speaks 28-bit LBA */
1058                 drive->select.b.lba = 1;
1059                 drive->capacity64 = id->lba_capacity;
1060                 if (hpa)
1061                         idedisk_check_hpa(drive);
1062         } else {
1063                 /* drive speaks boring old 28-bit CHS */
1064                 drive->capacity64 = drive->cyl * drive->head * drive->sect;
1065         }
1066 }
1067
1068 static sector_t idedisk_capacity (ide_drive_t *drive)
1069 {
1070         return drive->capacity64 - drive->sect0;
1071 }
1072
1073 static ide_startstop_t idedisk_special (ide_drive_t *drive)
1074 {
1075         special_t *s = &drive->special;
1076
1077         if (s->b.set_geometry) {
1078                 s->b.set_geometry       = 0;
1079                 if (!IS_PDC4030_DRIVE) {
1080                         ide_task_t args;
1081                         memset(&args, 0, sizeof(ide_task_t));
1082                         args.tfRegister[IDE_NSECTOR_OFFSET] = drive->sect;
1083                         args.tfRegister[IDE_SECTOR_OFFSET]  = drive->sect;
1084                         args.tfRegister[IDE_LCYL_OFFSET]    = drive->cyl;
1085                         args.tfRegister[IDE_HCYL_OFFSET]    = drive->cyl>>8;
1086                         args.tfRegister[IDE_SELECT_OFFSET]  = ((drive->head-1)|drive->select.all)&0xBF;
1087                         args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SPECIFY;
1088                         args.command_type = IDE_DRIVE_TASK_NO_DATA;
1089                         args.handler      = &set_geometry_intr;
1090                         do_rw_taskfile(drive, &args);
1091                 }
1092         } else if (s->b.recalibrate) {
1093                 s->b.recalibrate = 0;
1094                 if (!IS_PDC4030_DRIVE) {
1095                         ide_task_t args;
1096                         memset(&args, 0, sizeof(ide_task_t));
1097                         args.tfRegister[IDE_NSECTOR_OFFSET] = drive->sect;
1098                         args.tfRegister[IDE_COMMAND_OFFSET] = WIN_RESTORE;
1099                         args.command_type = IDE_DRIVE_TASK_NO_DATA;
1100                         args.handler      = &recal_intr;
1101                         do_rw_taskfile(drive, &args);
1102                 }
1103         } else if (s->b.set_multmode) {
1104                 s->b.set_multmode = 0;
1105                 if (drive->mult_req > drive->id->max_multsect)
1106                         drive->mult_req = drive->id->max_multsect;
1107                 if (!IS_PDC4030_DRIVE) {
1108                         ide_task_t args;
1109                         memset(&args, 0, sizeof(ide_task_t));
1110                         args.tfRegister[IDE_NSECTOR_OFFSET] = drive->mult_req;
1111                         args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SETMULT;
1112                         args.command_type = IDE_DRIVE_TASK_NO_DATA;
1113                         args.handler      = &set_multmode_intr;
1114                         do_rw_taskfile(drive, &args);
1115                 }
1116         } else if (s->all) {
1117                 int special = s->all;
1118                 s->all = 0;
1119                 printk(KERN_ERR "%s: bad special flag: 0x%02x\n", drive->name, special);
1120                 return ide_stopped;
1121         }
1122         return IS_PDC4030_DRIVE ? ide_stopped : ide_started;
1123 }
1124
1125 static void idedisk_pre_reset (ide_drive_t *drive)
1126 {
1127         int legacy = (drive->id->cfs_enable_2 & 0x0400) ? 0 : 1;
1128
1129         drive->special.all = 0;
1130         drive->special.b.set_geometry = legacy;
1131         drive->special.b.recalibrate  = legacy;
1132         if (OK_TO_RESET_CONTROLLER)
1133                 drive->mult_count = 0;
1134         if (!drive->keep_settings && !drive->using_dma)
1135                 drive->mult_req = 0;
1136         if (drive->mult_req != drive->mult_count)
1137                 drive->special.b.set_multmode = 1;
1138 }
1139
1140 #ifdef CONFIG_PROC_FS
1141
1142 static int smart_enable(ide_drive_t *drive)
1143 {
1144         ide_task_t args;
1145
1146         memset(&args, 0, sizeof(ide_task_t));
1147         args.tfRegister[IDE_FEATURE_OFFSET]     = SMART_ENABLE;
1148         args.tfRegister[IDE_LCYL_OFFSET]        = SMART_LCYL_PASS;
1149         args.tfRegister[IDE_HCYL_OFFSET]        = SMART_HCYL_PASS;
1150         args.tfRegister[IDE_COMMAND_OFFSET]     = WIN_SMART;
1151         args.command_type                       = IDE_DRIVE_TASK_NO_DATA;
1152         args.handler                            = &task_no_data_intr;
1153         return ide_raw_taskfile(drive, &args, NULL);
1154 }
1155
1156 static int get_smart_values(ide_drive_t *drive, u8 *buf)
1157 {
1158         ide_task_t args;
1159
1160         memset(&args, 0, sizeof(ide_task_t));
1161         args.tfRegister[IDE_FEATURE_OFFSET]     = SMART_READ_VALUES;
1162         args.tfRegister[IDE_NSECTOR_OFFSET]     = 0x01;
1163         args.tfRegister[IDE_LCYL_OFFSET]        = SMART_LCYL_PASS;
1164         args.tfRegister[IDE_HCYL_OFFSET]        = SMART_HCYL_PASS;
1165         args.tfRegister[IDE_COMMAND_OFFSET]     = WIN_SMART;
1166         args.command_type                       = IDE_DRIVE_TASK_IN;
1167         args.data_phase                         = TASKFILE_IN;
1168         args.handler                            = &task_in_intr;
1169         (void) smart_enable(drive);
1170         return ide_raw_taskfile(drive, &args, buf);
1171 }
1172
1173 static int get_smart_thresholds(ide_drive_t *drive, u8 *buf)
1174 {
1175         ide_task_t args;
1176         memset(&args, 0, sizeof(ide_task_t));
1177         args.tfRegister[IDE_FEATURE_OFFSET]     = SMART_READ_THRESHOLDS;
1178         args.tfRegister[IDE_NSECTOR_OFFSET]     = 0x01;
1179         args.tfRegister[IDE_LCYL_OFFSET]        = SMART_LCYL_PASS;
1180         args.tfRegister[IDE_HCYL_OFFSET]        = SMART_HCYL_PASS;
1181         args.tfRegister[IDE_COMMAND_OFFSET]     = WIN_SMART;
1182         args.command_type                       = IDE_DRIVE_TASK_IN;
1183         args.handler                            = &task_in_intr;
1184         (void) smart_enable(drive);
1185         return ide_raw_taskfile(drive, &args, buf);
1186 }
1187
1188 static int proc_idedisk_read_cache
1189         (char *page, char **start, off_t off, int count, int *eof, void *data)
1190 {
1191         ide_drive_t     *drive;
1192         char            *out = page;
1193         int             len;
1194
1195         down(&ide_cfg_sem);
1196         drive = ide_drive_from_key(data);
1197         if (drive && drive->id_read)
1198                 len = sprintf(out,"%i\n", drive->id->buf_size / 2);
1199         else
1200                 len = sprintf(out,"(none)\n");
1201         up(&ide_cfg_sem);
1202         PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
1203 }
1204
1205 static int proc_idedisk_read_smart_thresholds
1206         (char *page, char **start, off_t off, int count, int *eof, void *data)
1207 {
1208         ide_drive_t     *drive;
1209         int             len = 0, i = 0;
1210
1211         down(&ide_cfg_sem);
1212
1213         drive = ide_drive_from_key(data);
1214         if (drive && !get_smart_thresholds(drive, page)) {
1215                 unsigned short *val = (unsigned short *) page;
1216                 char *out = ((char *)val) + (SECTOR_WORDS * 4);
1217                 page = out;
1218                 do {
1219                         out += sprintf(out, "%04x%c", le16_to_cpu(*val), (++i & 7) ? ' ' : '\n');
1220                         val += 1;
1221                 } while (i < (SECTOR_WORDS * 2));
1222                 len = out - page;
1223         }
1224         up(&ide_cfg_sem);
1225         PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
1226 }
1227
1228 static int proc_idedisk_read_smart_values
1229         (char *page, char **start, off_t off, int count, int *eof, void *data)
1230 {
1231         ide_drive_t     *drive;
1232         int             len = 0, i = 0;
1233
1234         down(&ide_cfg_sem);
1235
1236         drive = ide_drive_from_key(data);
1237         if (drive && !get_smart_values(drive, page)) {
1238                 unsigned short *val = (unsigned short *) page;
1239                 char *out = ((char *)val) + (SECTOR_WORDS * 4);
1240                 page = out;
1241                 do {
1242                         out += sprintf(out, "%04x%c", le16_to_cpu(*val), (++i & 7) ? ' ' : '\n');
1243                         val += 1;
1244                 } while (i < (SECTOR_WORDS * 2));
1245                 len = out - page;
1246         }
1247         up(&ide_cfg_sem);
1248         PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
1249 }
1250
1251 static ide_proc_entry_t idedisk_proc[] = {
1252         { "cache",              S_IFREG|S_IRUGO,        proc_idedisk_read_cache,                NULL },
1253         { "geometry",           S_IFREG|S_IRUGO,        proc_ide_read_geometry,                 NULL },
1254         { "smart_values",       S_IFREG|S_IRUSR,        proc_idedisk_read_smart_values,         NULL },
1255         { "smart_thresholds",   S_IFREG|S_IRUSR,        proc_idedisk_read_smart_thresholds,     NULL },
1256         { NULL, 0, NULL, NULL }
1257 };
1258
1259 #else
1260
1261 #define idedisk_proc    NULL
1262
1263 #endif  /* CONFIG_PROC_FS */
1264
1265 static int idedisk_issue_flush(request_queue_t *q, struct gendisk *disk,
1266                                sector_t *error_sector)
1267 {
1268         ide_drive_t *drive = q->queuedata;
1269         struct request *rq;
1270         int ret;
1271
1272         if (!drive->wcache)
1273                 return 0;
1274
1275         rq = blk_get_request(q, WRITE, __GFP_WAIT);
1276
1277         memset(rq->cmd, 0, sizeof(rq->cmd));
1278
1279         if (ide_id_has_flush_cache_ext(drive->id) &&
1280             (drive->capacity64 >= (1UL << 28)))
1281                 rq->cmd[0] = WIN_FLUSH_CACHE_EXT;
1282         else
1283                 rq->cmd[0] = WIN_FLUSH_CACHE;
1284
1285
1286         rq->flags |= REQ_DRIVE_TASK | REQ_SOFTBARRIER;
1287         rq->buffer = rq->cmd;
1288
1289         ret = blk_execute_rq(q, disk, rq);
1290
1291         /*
1292          * if we failed and caller wants error offset, get it
1293          */
1294         if (ret && error_sector)
1295                 *error_sector = ide_get_error_location(drive, rq->cmd);
1296
1297         blk_put_request(rq);
1298         return ret;
1299 }
1300
1301 /*
1302  * This is tightly woven into the driver->do_special can not touch.
1303  * DON'T do it again until a total personality rewrite is committed.
1304  */
1305 static int set_multcount(ide_drive_t *drive, int arg)
1306 {
1307         struct request rq;
1308
1309         if (drive->special.b.set_multmode)
1310                 return -EBUSY;
1311         ide_init_drive_cmd (&rq);
1312         rq.flags = REQ_DRIVE_CMD;
1313         drive->mult_req = arg;
1314         drive->special.b.set_multmode = 1;
1315         (void) ide_do_drive_cmd (drive, &rq, ide_wait);
1316         return (drive->mult_count == arg) ? 0 : -EIO;
1317 }
1318
1319 static int set_nowerr(ide_drive_t *drive, int arg)
1320 {
1321         if (ide_spin_wait_hwgroup(drive))
1322                 return -EBUSY;
1323         drive->nowerr = arg;
1324         drive->bad_wstat = arg ? BAD_R_STAT : BAD_W_STAT;
1325         spin_unlock_irq(&ide_lock);
1326         return 0;
1327 }
1328
1329 static int write_cache(ide_drive_t *drive, int arg)
1330 {
1331         ide_task_t args;
1332         int err;
1333
1334         if (!ide_id_has_flush_cache(drive->id))
1335                 return 1;
1336
1337         memset(&args, 0, sizeof(ide_task_t));
1338         args.tfRegister[IDE_FEATURE_OFFSET]     = (arg) ?
1339                         SETFEATURES_EN_WCACHE : SETFEATURES_DIS_WCACHE;
1340         args.tfRegister[IDE_COMMAND_OFFSET]     = WIN_SETFEATURES;
1341         args.command_type                       = IDE_DRIVE_TASK_NO_DATA;
1342         args.handler                            = &task_no_data_intr;
1343
1344         err = ide_raw_taskfile(drive, &args, NULL);
1345         if (err)
1346                 return err;
1347
1348         drive->wcache = arg;
1349         return 0;
1350 }
1351
1352 static int do_idedisk_flushcache (ide_drive_t *drive)
1353 {
1354         ide_task_t args;
1355
1356         memset(&args, 0, sizeof(ide_task_t));
1357         if (ide_id_has_flush_cache_ext(drive->id))
1358                 args.tfRegister[IDE_COMMAND_OFFSET]     = WIN_FLUSH_CACHE_EXT;
1359         else
1360                 args.tfRegister[IDE_COMMAND_OFFSET]     = WIN_FLUSH_CACHE;
1361         args.command_type                       = IDE_DRIVE_TASK_NO_DATA;
1362         args.handler                            = &task_no_data_intr;
1363         return ide_raw_taskfile(drive, &args, NULL);
1364 }
1365
1366 static int set_acoustic (ide_drive_t *drive, int arg)
1367 {
1368         ide_task_t args;
1369
1370         memset(&args, 0, sizeof(ide_task_t));
1371         args.tfRegister[IDE_FEATURE_OFFSET]     = (arg) ? SETFEATURES_EN_AAM :
1372                                                           SETFEATURES_DIS_AAM;
1373         args.tfRegister[IDE_NSECTOR_OFFSET]     = arg;
1374         args.tfRegister[IDE_COMMAND_OFFSET]     = WIN_SETFEATURES;
1375         args.command_type = IDE_DRIVE_TASK_NO_DATA;
1376         args.handler      = &task_no_data_intr;
1377         ide_raw_taskfile(drive, &args, NULL);
1378         drive->acoustic = arg;
1379         return 0;
1380 }
1381
1382 /*
1383  * drive->addressing:
1384  *      0: 28-bit
1385  *      1: 48-bit
1386  *      2: 48-bit capable doing 28-bit
1387  */
1388 static int set_lba_addressing(ide_drive_t *drive, int arg)
1389 {
1390         drive->addressing =  0;
1391
1392         if (HWIF(drive)->no_lba48)
1393                 return 0;
1394
1395         if (!idedisk_supports_lba48(drive->id))
1396                 return -EIO;
1397         drive->addressing = arg;
1398         return 0;
1399 }
1400
1401 static void idedisk_add_settings(ide_drive_t *drive)
1402 {
1403         struct hd_driveid *id = drive->id;
1404
1405         ide_add_setting(drive,  "bios_cyl",             SETTING_RW,                                     -1,                     -1,                     TYPE_INT,       0,      65535,                          1,      1,      &drive->bios_cyl,               NULL);
1406         ide_add_setting(drive,  "bios_head",            SETTING_RW,                                     -1,                     -1,                     TYPE_BYTE,      0,      255,                            1,      1,      &drive->bios_head,              NULL);
1407         ide_add_setting(drive,  "bios_sect",            SETTING_RW,                                     -1,                     -1,                     TYPE_BYTE,      0,      63,                             1,      1,      &drive->bios_sect,              NULL);
1408         ide_add_setting(drive,  "address",              SETTING_RW,                                     HDIO_GET_ADDRESS,       HDIO_SET_ADDRESS,       TYPE_INTA,      0,      2,                              1,      1,      &drive->addressing,     set_lba_addressing);
1409         ide_add_setting(drive,  "bswap",                SETTING_READ,                                   -1,                     -1,                     TYPE_BYTE,      0,      1,                              1,      1,      &drive->bswap,                  NULL);
1410         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);
1411         ide_add_setting(drive,  "nowerr",               SETTING_RW,                                     HDIO_GET_NOWERR,        HDIO_SET_NOWERR,        TYPE_BYTE,      0,      1,                              1,      1,      &drive->nowerr,                 set_nowerr);
1412         ide_add_setting(drive,  "lun",                  SETTING_RW,                                     -1,                     -1,                     TYPE_INT,       0,      7,                              1,      1,      &drive->lun,                    NULL);
1413         ide_add_setting(drive,  "wcache",               SETTING_RW,                                     HDIO_GET_WCACHE,        HDIO_SET_WCACHE,        TYPE_BYTE,      0,      1,                              1,      1,      &drive->wcache,                 write_cache);
1414         ide_add_setting(drive,  "acoustic",             SETTING_RW,                                     HDIO_GET_ACOUSTIC,      HDIO_SET_ACOUSTIC,      TYPE_BYTE,      0,      254,                            1,      1,      &drive->acoustic,               set_acoustic);
1415         ide_add_setting(drive,  "failures",             SETTING_RW,                                     -1,                     -1,                     TYPE_INT,       0,      65535,                          1,      1,      &drive->failures,               NULL);
1416         ide_add_setting(drive,  "max_failures",         SETTING_RW,                                     -1,                     -1,                     TYPE_INT,       0,      65535,                          1,      1,      &drive->max_failures,           NULL);
1417 }
1418
1419 /*
1420  * Power Management state machine. This one is rather trivial for now,
1421  * we should probably add more, like switching back to PIO on suspend
1422  * to help some BIOSes, re-do the door locking on resume, etc...
1423  */
1424
1425 enum {
1426         idedisk_pm_flush_cache  = ide_pm_state_start_suspend,
1427         idedisk_pm_standby,
1428
1429         idedisk_pm_idle         = ide_pm_state_start_resume,
1430         idedisk_pm_restore_dma,
1431 };
1432
1433 static void idedisk_complete_power_step (ide_drive_t *drive, struct request *rq, u8 stat, u8 error)
1434 {
1435         switch (rq->pm->pm_step) {
1436         case idedisk_pm_flush_cache:    /* Suspend step 1 (flush cache) complete */
1437                 if (rq->pm->pm_state == 4)
1438                         rq->pm->pm_step = ide_pm_state_completed;
1439                 else
1440                         rq->pm->pm_step = idedisk_pm_standby;
1441                 break;
1442         case idedisk_pm_standby:        /* Suspend step 2 (standby) complete */
1443                 rq->pm->pm_step = ide_pm_state_completed;
1444                 break;
1445         case idedisk_pm_idle:           /* Resume step 1 (idle) complete */
1446                 rq->pm->pm_step = idedisk_pm_restore_dma;
1447                 break;
1448         }
1449 }
1450
1451 static ide_startstop_t idedisk_start_power_step (ide_drive_t *drive, struct request *rq)
1452 {
1453         ide_task_t *args = rq->special;
1454
1455         memset(args, 0, sizeof(*args));
1456
1457         switch (rq->pm->pm_step) {
1458         case idedisk_pm_flush_cache:    /* Suspend step 1 (flush cache) */
1459                 /* Not supported? Switch to next step now. */
1460                 if (!drive->wcache || !ide_id_has_flush_cache(drive->id)) {
1461                         idedisk_complete_power_step(drive, rq, 0, 0);
1462                         return ide_stopped;
1463                 }
1464                 if (ide_id_has_flush_cache_ext(drive->id))
1465                         args->tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE_EXT;
1466                 else
1467                         args->tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE;
1468                 args->command_type = IDE_DRIVE_TASK_NO_DATA;
1469                 args->handler      = &task_no_data_intr;
1470                 return do_rw_taskfile(drive, args);
1471
1472         case idedisk_pm_standby:        /* Suspend step 2 (standby) */
1473                 args->tfRegister[IDE_COMMAND_OFFSET] = WIN_STANDBYNOW1;
1474                 args->command_type = IDE_DRIVE_TASK_NO_DATA;
1475                 args->handler      = &task_no_data_intr;
1476                 return do_rw_taskfile(drive, args);
1477
1478         case idedisk_pm_idle:           /* Resume step 1 (idle) */
1479                 args->tfRegister[IDE_COMMAND_OFFSET] = WIN_IDLEIMMEDIATE;
1480                 args->command_type = IDE_DRIVE_TASK_NO_DATA;
1481                 args->handler = task_no_data_intr;
1482                 return do_rw_taskfile(drive, args);
1483
1484         case idedisk_pm_restore_dma:    /* Resume step 2 (restore DMA) */
1485                 /*
1486                  * Right now, all we do is call hwif->ide_dma_check(drive),
1487                  * we could be smarter and check for current xfer_speed
1488                  * in struct drive etc...
1489                  * Also, this step could be implemented as a generic helper
1490                  * as most subdrivers will use it
1491                  */
1492                 if ((drive->id->capability & 1) == 0)
1493                         break;
1494                 if (HWIF(drive)->ide_dma_check == NULL)
1495                         break;
1496                 HWIF(drive)->ide_dma_check(drive);
1497                 break;
1498         }
1499         rq->pm->pm_step = ide_pm_state_completed;
1500         return ide_stopped;
1501 }
1502
1503 static void idedisk_setup (ide_drive_t *drive)
1504 {
1505         struct hd_driveid *id = drive->id;
1506         unsigned long long capacity;
1507         int barrier;
1508
1509         idedisk_add_settings(drive);
1510
1511         if (drive->id_read == 0)
1512                 return;
1513
1514         /*
1515          * CompactFlash cards and their brethern look just like hard drives
1516          * to us, but they are removable and don't have a doorlock mechanism.
1517          */
1518         if (drive->removable && !(drive->is_flash)) {
1519                 /*
1520                  * Removable disks (eg. SYQUEST); ignore 'WD' drives 
1521                  */
1522                 if (id->model[0] != 'W' || id->model[1] != 'D') {
1523                         drive->doorlocking = 1;
1524                 }
1525         }
1526
1527         (void)set_lba_addressing(drive, 1);
1528
1529         if (drive->addressing == 1) {
1530                 ide_hwif_t *hwif = HWIF(drive);
1531                 int max_s = 2048;
1532
1533                 if (max_s > hwif->rqsize)
1534                         max_s = hwif->rqsize;
1535
1536                 blk_queue_max_sectors(drive->queue, max_s);
1537         }
1538
1539         printk(KERN_INFO "%s: max request size: %dKiB\n", drive->name, drive->queue->max_sectors / 2);
1540
1541         /* Extract geometry if we did not already have one for the drive */
1542         if (!drive->cyl || !drive->head || !drive->sect) {
1543                 drive->cyl     = drive->bios_cyl  = id->cyls;
1544                 drive->head    = drive->bios_head = id->heads;
1545                 drive->sect    = drive->bios_sect = id->sectors;
1546         }
1547
1548         /* Handle logical geometry translation by the drive */
1549         if ((id->field_valid & 1) && id->cur_cyls &&
1550             id->cur_heads && (id->cur_heads <= 16) && id->cur_sectors) {
1551                 drive->cyl  = id->cur_cyls;
1552                 drive->head = id->cur_heads;
1553                 drive->sect = id->cur_sectors;
1554         }
1555
1556         /* Use physical geometry if what we have still makes no sense */
1557         if (drive->head > 16 && id->heads && id->heads <= 16) {
1558                 drive->cyl  = id->cyls;
1559                 drive->head = id->heads;
1560                 drive->sect = id->sectors;
1561         }
1562
1563         /* calculate drive capacity, and select LBA if possible */
1564         init_idedisk_capacity (drive);
1565
1566         /* limit drive capacity to 137GB if LBA48 cannot be used */
1567         if (drive->addressing == 0 && drive->capacity64 > 1ULL << 28) {
1568                 printk(KERN_WARNING "%s: cannot use LBA48 - full capacity "
1569                        "%llu sectors (%llu MB)\n",
1570                        drive->name, (unsigned long long)drive->capacity64,
1571                        sectors_to_MB(drive->capacity64));
1572                 drive->capacity64 = 1ULL << 28;
1573         }
1574
1575         if (drive->hwif->no_lba48_dma && drive->addressing) {
1576                 if (drive->capacity64 > 1ULL << 28) {
1577                         printk(KERN_INFO "%s: cannot use LBA48 DMA - PIO mode will"
1578                                          " be used for accessing sectors > %u\n",
1579                                          drive->name, 1 << 28);
1580                 } else
1581                         drive->addressing = 0;
1582         }
1583
1584         /*
1585          * if possible, give fdisk access to more of the drive,
1586          * by correcting bios_cyls:
1587          */
1588         capacity = idedisk_capacity (drive);
1589         if (!drive->forced_geom) {
1590
1591                 if (idedisk_supports_lba48(drive->id)) {
1592                         /* compatibility */
1593                         drive->bios_sect = 63;
1594                         drive->bios_head = 255;
1595                 }
1596
1597                 if (drive->bios_sect && drive->bios_head) {
1598                         unsigned int cap0 = capacity; /* truncate to 32 bits */
1599                         unsigned int cylsz, cyl;
1600
1601                         if (cap0 != capacity)
1602                                 drive->bios_cyl = 65535;
1603                         else {
1604                                 cylsz = drive->bios_sect * drive->bios_head;
1605                                 cyl = cap0 / cylsz;
1606                                 if (cyl > 65535)
1607                                         cyl = 65535;
1608                                 if (cyl > drive->bios_cyl)
1609                                         drive->bios_cyl = cyl;
1610                         }
1611                 }
1612         }
1613         printk(KERN_INFO "%s: %llu sectors (%llu MB)",
1614                          drive->name, capacity, sectors_to_MB(capacity));
1615
1616         /* Only print cache size when it was specified */
1617         if (id->buf_size)
1618                 printk (" w/%dKiB Cache", id->buf_size/2);
1619
1620         if(drive->bios_cyl)
1621                 printk(", CHS=%d/%d/%d", 
1622                         drive->bios_cyl, drive->bios_head, drive->bios_sect);
1623                         
1624         if (drive->using_dma)
1625                 (void) HWIF(drive)->ide_dma_verbose(drive);
1626         printk("\n");
1627
1628         drive->mult_count = 0;
1629         if (id->max_multsect) {
1630 #ifdef CONFIG_IDEDISK_MULTI_MODE
1631                 id->multsect = ((id->max_multsect/2) > 1) ? id->max_multsect : 0;
1632                 id->multsect_valid = id->multsect ? 1 : 0;
1633                 drive->mult_req = id->multsect_valid ? id->max_multsect : INITIAL_MULT_COUNT;
1634                 drive->special.b.set_multmode = drive->mult_req ? 1 : 0;
1635 #else   /* original, pre IDE-NFG, per request of AC */
1636                 drive->mult_req = INITIAL_MULT_COUNT;
1637                 if (drive->mult_req > id->max_multsect)
1638                         drive->mult_req = id->max_multsect;
1639                 if (drive->mult_req || ((id->multsect_valid & 1) && id->multsect))
1640                         drive->special.b.set_multmode = 1;
1641 #endif  /* CONFIG_IDEDISK_MULTI_MODE */
1642         }
1643         drive->no_io_32bit = id->dword_io ? 1 : 0;
1644
1645         /* write cache enabled? */
1646         if ((id->csfo & 1) || (id->cfs_enable_1 & (1 << 5)))
1647                 drive->wcache = 1;
1648
1649         write_cache(drive, 1);
1650
1651         /*
1652          * We must avoid issuing commands a drive does not understand
1653          * or we may crash it. We check flush cache is supported. We also
1654          * check we have the LBA48 flush cache if the drive capacity is
1655          * too large. By this time we have trimmed the drive capacity if
1656          * LBA48 is not available so we don't need to recheck that.
1657          */
1658         barrier = 0;
1659         if (ide_id_has_flush_cache(id))
1660                 barrier = 1;
1661         if (drive->addressing == 1) {
1662                 /* Can't issue the correct flush ? */
1663                 if (capacity > (1ULL << 28) && !ide_id_has_flush_cache_ext(id))
1664                         barrier = 0;
1665         }
1666
1667         printk(KERN_DEBUG "%s: cache flushes %ssupported\n",
1668                 drive->name, barrier ? "" : "not ");
1669         if (barrier) {
1670                 blk_queue_ordered(drive->queue, 1);
1671                 blk_queue_issue_flush_fn(drive->queue, idedisk_issue_flush);
1672         }
1673 }
1674
1675 static void ide_cacheflush_p(ide_drive_t *drive)
1676 {
1677         if (!drive->wcache || !ide_id_has_flush_cache(drive->id))
1678                 return;
1679
1680         if (do_idedisk_flushcache(drive))
1681                 printk(KERN_INFO "%s: wcache flush failed!\n", drive->name);
1682 }
1683
1684 static int idedisk_cleanup (ide_drive_t *drive)
1685 {
1686         struct gendisk *g = drive->disk;
1687         ide_cacheflush_p(drive);
1688         if (ide_unregister_subdriver(drive))
1689                 return 1;
1690         del_gendisk(g);
1691         drive->devfs_name[0] = '\0';
1692         g->fops = ide_fops;
1693         return 0;
1694 }
1695
1696 static int idedisk_attach(ide_drive_t *drive);
1697
1698 static void ide_device_shutdown(struct device *dev)
1699 {
1700         ide_drive_t *drive = container_of(dev, ide_drive_t, gendev);
1701
1702 #ifdef  CONFIG_ALPHA
1703         /* On Alpha, halt(8) doesn't actually turn the machine off,
1704            it puts you into the sort of firmware monitor. Typically,
1705            it's used to boot another kernel image, so it's not much
1706            different from reboot(8). Therefore, we don't need to
1707            spin down the disk in this case, especially since Alpha
1708            firmware doesn't handle disks in standby mode properly.
1709            On the other hand, it's reasonably safe to turn the power
1710            off when the shutdown process reaches the firmware prompt,
1711            as the firmware initialization takes rather long time -
1712            at least 10 seconds, which should be sufficient for
1713            the disk to expire its write cache. */
1714         if (system_state != SYSTEM_POWER_OFF) {
1715 #else
1716         if (system_state == SYSTEM_RESTART) {
1717 #endif
1718                 ide_cacheflush_p(drive);
1719                 return;
1720         }
1721
1722         printk("Shutdown: %s\n", drive->name);
1723         dev->bus->suspend(dev, PM_SUSPEND_STANDBY);
1724 }
1725
1726 /*
1727  *      IDE subdriver functions, registered with ide.c
1728  */
1729 static ide_driver_t idedisk_driver = {
1730         .owner                  = THIS_MODULE,
1731         .gen_driver = {
1732                 .shutdown       = ide_device_shutdown,
1733         },
1734         .name                   = "ide-disk",
1735         .version                = IDEDISK_VERSION,
1736         .media                  = ide_disk,
1737         .busy                   = 0,
1738         .supports_dsc_overlap   = 0,
1739         .cleanup                = idedisk_cleanup,
1740         .do_request             = ide_do_rw_disk,
1741         .sense                  = idedisk_dump_status,
1742         .error                  = idedisk_error,
1743         .abort                  = idedisk_abort,
1744         .pre_reset              = idedisk_pre_reset,
1745         .capacity               = idedisk_capacity,
1746         .special                = idedisk_special,
1747         .proc                   = idedisk_proc,
1748         .attach                 = idedisk_attach,
1749         .drives                 = LIST_HEAD_INIT(idedisk_driver.drives),
1750         .start_power_step       = idedisk_start_power_step,
1751         .complete_power_step    = idedisk_complete_power_step,
1752 };
1753
1754 static int idedisk_open(struct inode *inode, struct file *filp)
1755 {
1756         ide_drive_t *drive = inode->i_bdev->bd_disk->private_data;
1757         drive->usage++;
1758         if (drive->removable && drive->usage == 1) {
1759                 ide_task_t args;
1760                 memset(&args, 0, sizeof(ide_task_t));
1761                 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_DOORLOCK;
1762                 args.command_type = IDE_DRIVE_TASK_NO_DATA;
1763                 args.handler      = &task_no_data_intr;
1764                 check_disk_change(inode->i_bdev);
1765                 /*
1766                  * Ignore the return code from door_lock,
1767                  * since the open() has already succeeded,
1768                  * and the door_lock is irrelevant at this point.
1769                  */
1770                 if (drive->doorlocking && ide_raw_taskfile(drive, &args, NULL))
1771                         drive->doorlocking = 0;
1772         }
1773         return 0;
1774 }
1775
1776 static int idedisk_release(struct inode *inode, struct file *filp)
1777 {
1778         ide_drive_t *drive = inode->i_bdev->bd_disk->private_data;
1779         if (drive->usage == 1)
1780                 ide_cacheflush_p(drive);
1781         if (drive->removable && drive->usage == 1) {
1782                 ide_task_t args;
1783                 memset(&args, 0, sizeof(ide_task_t));
1784                 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_DOORUNLOCK;
1785                 args.command_type = IDE_DRIVE_TASK_NO_DATA;
1786                 args.handler      = &task_no_data_intr;
1787                 if (drive->doorlocking && ide_raw_taskfile(drive, &args, NULL))
1788                         drive->doorlocking = 0;
1789         }
1790         drive->usage--;
1791         return 0;
1792 }
1793
1794 static int idedisk_ioctl(struct inode *inode, struct file *file,
1795                         unsigned int cmd, unsigned long arg)
1796 {
1797         struct block_device *bdev = inode->i_bdev;
1798         return generic_ide_ioctl(file, bdev, cmd, arg);
1799 }
1800
1801 static int idedisk_media_changed(struct gendisk *disk)
1802 {
1803         ide_drive_t *drive = disk->private_data;
1804
1805         /* do not scan partitions twice if this is a removable device */
1806         if (drive->attach) {
1807                 drive->attach = 0;
1808                 return 0;
1809         }
1810         /* if removable, always assume it was changed */
1811         return drive->removable;
1812 }
1813
1814 static int idedisk_revalidate_disk(struct gendisk *disk)
1815 {
1816         ide_drive_t *drive = disk->private_data;
1817         set_capacity(disk, current_capacity(drive));
1818         return 0;
1819 }
1820
1821 static struct block_device_operations idedisk_ops = {
1822         .owner          = THIS_MODULE,
1823         .open           = idedisk_open,
1824         .release        = idedisk_release,
1825         .ioctl          = idedisk_ioctl,
1826         .media_changed  = idedisk_media_changed,
1827         .revalidate_disk= idedisk_revalidate_disk
1828 };
1829
1830 MODULE_DESCRIPTION("ATA DISK Driver");
1831
1832 static int idedisk_attach(ide_drive_t *drive)
1833 {
1834         struct gendisk *g = drive->disk;
1835
1836         /* strstr("foo", "") is non-NULL */
1837         if (!strstr("ide-disk", drive->driver_req))
1838                 goto failed;
1839         if (!drive->present)
1840                 goto failed;
1841         if (drive->media != ide_disk)
1842                 goto failed;
1843
1844         if (ide_register_subdriver(drive, &idedisk_driver)) {
1845                 printk (KERN_ERR "ide-disk: %s: Failed to register the driver with ide.c\n", drive->name);
1846                 goto failed;
1847         }
1848         DRIVER(drive)->busy++;
1849         idedisk_setup(drive);
1850         if ((!drive->head || drive->head > 16) && !drive->select.b.lba) {
1851                 printk(KERN_ERR "%s: INVALID GEOMETRY: %d PHYSICAL HEADS?\n",
1852                         drive->name, drive->head);
1853                 drive->attach = 0;
1854         } else
1855                 drive->attach = 1;
1856         DRIVER(drive)->busy--;
1857         g->minors = 1 << PARTN_BITS;
1858         strcpy(g->devfs_name, drive->devfs_name);
1859         g->driverfs_dev = &drive->gendev;
1860         g->flags = drive->removable ? GENHD_FL_REMOVABLE : 0;
1861         set_capacity(g, current_capacity(drive));
1862         g->fops = &idedisk_ops;
1863         add_disk(g);
1864         return 0;
1865 failed:
1866         return 1;
1867 }
1868
1869 static void __exit idedisk_exit (void)
1870 {
1871         ide_unregister_driver(&idedisk_driver);
1872 }
1873
1874 static int idedisk_init (void)
1875 {
1876         return ide_register_driver(&idedisk_driver);
1877 }
1878
1879 module_init(idedisk_init);
1880 module_exit(idedisk_exit);
1881 MODULE_LICENSE("GPL");