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