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