This commit was manufactured by cvs2svn to create tag
[linux-2.6.git] / drivers / ide / ide-disk.c
1 /*
2  *  linux/drivers/ide/ide-disk.c        Version 1.18    Mar 05, 2003
3  *
4  *  Copyright (C) 1994-1998  Linus Torvalds & authors (see below)
5  *  Copyright (C) 1998-2002  Linux ATA Development
6  *                              Andre Hedrick <andre@linux-ide.org>
7  *  Copyright (C) 2003       Red Hat <alan@redhat.com>
8  */
9
10 /*
11  *  Mostly written by Mark Lord <mlord@pobox.com>
12  *                and Gadi Oxman <gadio@netvision.net.il>
13  *                and Andre Hedrick <andre@linux-ide.org>
14  *
15  * This is the IDE/ATA disk driver, as evolved from hd.c and ide.c.
16  *
17  * Version 1.00         move disk only code from ide.c to ide-disk.c
18  *                      support optional byte-swapping of all data
19  * Version 1.01         fix previous byte-swapping code
20  * Version 1.02         remove ", LBA" from drive identification msgs
21  * Version 1.03         fix display of id->buf_size for big-endian
22  * Version 1.04         add /proc configurable settings and S.M.A.R.T support
23  * Version 1.05         add capacity support for ATA3 >= 8GB
24  * Version 1.06         get boot-up messages to show full cyl count
25  * Version 1.07         disable door-locking if it fails
26  * Version 1.08         fixed CHS/LBA translations for ATA4 > 8GB,
27  *                      process of adding new ATA4 compliance.
28  *                      fixed problems in allowing fdisk to see
29  *                      the entire disk.
30  * Version 1.09         added increment of rq->sector in ide_multwrite
31  *                      added UDMA 3/4 reporting
32  * Version 1.10         request queue changes, Ultra DMA 100
33  * Version 1.11         added 48-bit lba
34  * Version 1.12         adding taskfile io access method
35  * Version 1.13         added standby and flush-cache for notifier
36  * Version 1.14         added acoustic-wcache
37  * Version 1.15         convert all calls to ide_raw_taskfile
38  *                              since args will return register content.
39  * Version 1.16         added suspend-resume-checkpower
40  * Version 1.17         do flush on standy, do flush on ATA < ATA6
41  *                      fix wcache setup.
42  */
43
44 #define IDEDISK_VERSION "1.18"
45
46 #undef REALLY_SLOW_IO           /* most systems can safely undef this */
47
48 #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 (IDE_CONTROL_REG)
362                 hwif->OUTB(drive->ctl, IDE_CONTROL_REG);
363
364         if (drive->select.b.lba) {
365                 if (drive->addressing == 1) {
366                         task_ioreg_t tasklets[10];
367
368                         if (blk_rq_tagged(rq)) {
369                                 tasklets[0] = nsectors.b.low;
370                                 tasklets[1] = nsectors.b.high;
371                                 tasklets[2] = rq->tag << 3;
372                                 tasklets[3] = 0;
373                         } else {
374                                 tasklets[0] = 0;
375                                 tasklets[1] = 0;
376                                 tasklets[2] = nsectors.b.low;
377                                 tasklets[3] = nsectors.b.high;
378                         }
379
380                         tasklets[4] = (task_ioreg_t) block;
381                         tasklets[5] = (task_ioreg_t) (block>>8);
382                         tasklets[6] = (task_ioreg_t) (block>>16);
383                         tasklets[7] = (task_ioreg_t) (block>>24);
384                         if (sizeof(block) == 4) {
385                                 tasklets[8] = (task_ioreg_t) 0;
386                                 tasklets[9] = (task_ioreg_t) 0;
387                         } else {
388                                 tasklets[8] = (task_ioreg_t)((u64)block >> 32);
389                                 tasklets[9] = (task_ioreg_t)((u64)block >> 40);
390                         }
391 #ifdef DEBUG
392                         printk("%s: %sing: LBAsect=%lu, sectors=%ld, "
393                                 "buffer=0x%08lx, LBAsect=0x%012lx\n",
394                                 drive->name,
395                                 rq_data_dir(rq)==READ?"read":"writ",
396                                 block,
397                                 rq->nr_sectors,
398                                 (unsigned long) rq->buffer,
399                                 block);
400                         printk("%s: 0x%02x%02x 0x%02x%02x%02x%02x%02x%02x\n",
401                                 drive->name, tasklets[3], tasklets[2],
402                                 tasklets[9], tasklets[8], tasklets[7],
403                                 tasklets[6], tasklets[5], tasklets[4]);
404 #endif
405                         hwif->OUTB(tasklets[1], IDE_FEATURE_REG);
406                         hwif->OUTB(tasklets[3], IDE_NSECTOR_REG);
407                         hwif->OUTB(tasklets[7], IDE_SECTOR_REG);
408                         hwif->OUTB(tasklets[8], IDE_LCYL_REG);
409                         hwif->OUTB(tasklets[9], IDE_HCYL_REG);
410
411                         hwif->OUTB(tasklets[0], IDE_FEATURE_REG);
412                         hwif->OUTB(tasklets[2], IDE_NSECTOR_REG);
413                         hwif->OUTB(tasklets[4], IDE_SECTOR_REG);
414                         hwif->OUTB(tasklets[5], IDE_LCYL_REG);
415                         hwif->OUTB(tasklets[6], IDE_HCYL_REG);
416                         hwif->OUTB(0x00|drive->select.all,IDE_SELECT_REG);
417                 } else {
418 #ifdef DEBUG
419                         printk("%s: %sing: LBAsect=%llu, sectors=%ld, "
420                                 "buffer=0x%08lx\n",
421                                 drive->name,
422                                 rq_data_dir(rq)==READ?"read":"writ",
423                                 (unsigned long long)block, rq->nr_sectors,
424                                 (unsigned long) rq->buffer);
425 #endif
426                         if (blk_rq_tagged(rq)) {
427                                 hwif->OUTB(nsectors.b.low, IDE_FEATURE_REG);
428                                 hwif->OUTB(rq->tag << 3, IDE_NSECTOR_REG);
429                         } else {
430                                 hwif->OUTB(0x00, IDE_FEATURE_REG);
431                                 hwif->OUTB(nsectors.b.low, IDE_NSECTOR_REG);
432                         }
433
434                         hwif->OUTB(block, IDE_SECTOR_REG);
435                         hwif->OUTB(block>>=8, IDE_LCYL_REG);
436                         hwif->OUTB(block>>=8, IDE_HCYL_REG);
437                         hwif->OUTB(((block>>8)&0x0f)|drive->select.all,IDE_SELECT_REG);
438                 }
439         } else {
440                 unsigned int sect,head,cyl,track;
441                 track = (int)block / drive->sect;
442                 sect  = (int)block % drive->sect + 1;
443                 hwif->OUTB(sect, IDE_SECTOR_REG);
444                 head  = track % drive->head;
445                 cyl   = track / drive->head;
446
447                 if (blk_rq_tagged(rq)) {
448                         hwif->OUTB(nsectors.b.low, IDE_FEATURE_REG);
449                         hwif->OUTB(rq->tag << 3, IDE_NSECTOR_REG);
450                 } else {
451                         hwif->OUTB(0x00, IDE_FEATURE_REG);
452                         hwif->OUTB(nsectors.b.low, IDE_NSECTOR_REG);
453                 }
454
455                 hwif->OUTB(cyl, IDE_LCYL_REG);
456                 hwif->OUTB(cyl>>8, IDE_HCYL_REG);
457                 hwif->OUTB(head|drive->select.all,IDE_SELECT_REG);
458 #ifdef DEBUG
459                 printk("%s: %sing: CHS=%d/%d/%d, sectors=%ld, buffer=0x%08lx\n",
460                         drive->name, rq_data_dir(rq)==READ?"read":"writ", cyl,
461                         head, sect, rq->nr_sectors, (unsigned long) rq->buffer);
462 #endif
463         }
464
465         if (rq_data_dir(rq) == READ) {
466 #ifdef CONFIG_BLK_DEV_IDE_TCQ
467                 if (blk_rq_tagged(rq))
468                         return __ide_dma_queued_read(drive);
469 #endif
470                 if (drive->using_dma && !hwif->ide_dma_read(drive))
471                         return ide_started;
472
473                 command = ((drive->mult_count) ?
474                            ((lba48) ? WIN_MULTREAD_EXT : WIN_MULTREAD) :
475                            ((lba48) ? WIN_READ_EXT : WIN_READ));
476                 ide_execute_command(drive, command, &read_intr, WAIT_CMD, NULL);
477                 return ide_started;
478         } else {
479                 ide_startstop_t startstop;
480 #ifdef CONFIG_BLK_DEV_IDE_TCQ
481                 if (blk_rq_tagged(rq))
482                         return __ide_dma_queued_write(drive);
483 #endif
484                 if (drive->using_dma && !(HWIF(drive)->ide_dma_write(drive)))
485                         return ide_started;
486
487                 command = ((drive->mult_count) ?
488                            ((lba48) ? WIN_MULTWRITE_EXT : WIN_MULTWRITE) :
489                            ((lba48) ? WIN_WRITE_EXT : WIN_WRITE));
490                 hwif->OUTB(command, IDE_COMMAND_REG);
491
492                 if (ide_wait_stat(&startstop, drive, DATA_READY,
493                                 drive->bad_wstat, WAIT_DRQ)) {
494                         printk(KERN_ERR "%s: no DRQ after issuing %s\n",
495                                 drive->name,
496                                 drive->mult_count ? "MULTWRITE" : "WRITE");
497                         return startstop;
498                 }
499                 if (!drive->unmask)
500                         local_irq_disable();
501                 if (drive->mult_count) {
502                         ide_hwgroup_t *hwgroup = HWGROUP(drive);
503
504                         hwgroup->wrq = *rq; /* scratchpad */
505                         ide_set_handler(drive, &multwrite_intr, WAIT_CMD, NULL);
506                         ide_multwrite(drive, drive->mult_count);
507                 } else {
508                         unsigned long flags;
509                         char *to = ide_map_buffer(rq, &flags);
510                         ide_set_handler(drive, &write_intr, WAIT_CMD, NULL);
511                         taskfile_output_data(drive, to, SECTOR_WORDS);
512                         ide_unmap_buffer(rq, to, &flags);
513                 }
514                 return ide_started;
515         }
516 }
517 EXPORT_SYMBOL_GPL(__ide_do_rw_disk);
518
519 #else /* CONFIG_IDE_TASKFILE_IO */
520
521 static ide_startstop_t chs_rw_disk(ide_drive_t *, struct request *, unsigned long);
522 static ide_startstop_t lba_28_rw_disk(ide_drive_t *, struct request *, unsigned long);
523 static ide_startstop_t lba_48_rw_disk(ide_drive_t *, struct request *, unsigned long long);
524
525 /*
526  * __ide_do_rw_disk() issues READ and WRITE commands to a disk,
527  * using LBA if supported, or CHS otherwise, to address sectors.
528  * It also takes care of issuing special DRIVE_CMDs.
529  */
530 ide_startstop_t __ide_do_rw_disk (ide_drive_t *drive, struct request *rq, sector_t block)
531 {
532         /*
533          * 268435455  == 137439 MB or 28bit limit
534          *
535          * need to add split taskfile operations based on 28bit threshold.
536          */
537         if (drive->addressing == 1)             /* 48-bit LBA */
538                 return lba_48_rw_disk(drive, rq, (unsigned long long) block);
539         if (drive->select.b.lba)                /* 28-bit LBA */
540                 return lba_28_rw_disk(drive, rq, (unsigned long) block);
541
542         /* 28-bit CHS : DIE DIE DIE piece of legacy crap!!! */
543         return chs_rw_disk(drive, rq, (unsigned long) block);
544 }
545 EXPORT_SYMBOL_GPL(__ide_do_rw_disk);
546
547 static u8 get_command(ide_drive_t *drive, int cmd, ide_task_t *task)
548 {
549         unsigned int lba48 = (drive->addressing == 1) ? 1 : 0;
550
551         if (cmd == READ) {
552                 task->command_type = IDE_DRIVE_TASK_IN;
553                 if (drive->using_tcq)
554                         return lba48 ? WIN_READDMA_QUEUED_EXT : WIN_READDMA_QUEUED;
555                 if (drive->using_dma)
556                         return lba48 ? WIN_READDMA_EXT : WIN_READDMA;
557                 if (drive->mult_count) {
558                         task->handler = &task_mulin_intr;
559                         return lba48 ? WIN_MULTREAD_EXT : WIN_MULTREAD;
560                 }
561                 task->handler = &task_in_intr;
562                 return lba48 ? WIN_READ_EXT : WIN_READ;
563         } else {
564                 task->command_type = IDE_DRIVE_TASK_RAW_WRITE;
565                 if (drive->using_tcq)
566                         return lba48 ? WIN_WRITEDMA_QUEUED_EXT : WIN_WRITEDMA_QUEUED;
567                 if (drive->using_dma)
568                         return lba48 ? WIN_WRITEDMA_EXT : WIN_WRITEDMA;
569                 if (drive->mult_count) {
570                         task->prehandler = &pre_task_mulout_intr;
571                         task->handler = &task_mulout_intr;
572                         return lba48 ? WIN_MULTWRITE_EXT : WIN_MULTWRITE;
573                 }
574                 task->prehandler = &pre_task_out_intr;
575                 task->handler = &task_out_intr;
576                 return lba48 ? WIN_WRITE_EXT : WIN_WRITE;
577         }
578 }
579
580 static ide_startstop_t chs_rw_disk (ide_drive_t *drive, struct request *rq, unsigned long block)
581 {
582         ide_task_t              args;
583         int                     sectors;
584         ata_nsector_t           nsectors;
585         unsigned int track      = (block / drive->sect);
586         unsigned int sect       = (block % drive->sect) + 1;
587         unsigned int head       = (track % drive->head);
588         unsigned int cyl        = (track / drive->head);
589
590         nsectors.all = (u16) rq->nr_sectors;
591
592 #ifdef DEBUG
593         printk("%s: %sing: ", drive->name, (rq_data_dir(rq)==READ) ? "read" : "writ");
594         printk("CHS=%d/%d/%d, ", cyl, head, sect);
595         printk("sectors=%ld, ", rq->nr_sectors);
596         printk("buffer=0x%08lx\n", (unsigned long) rq->buffer);
597 #endif
598
599         memset(&args, 0, sizeof(ide_task_t));
600
601         sectors = (rq->nr_sectors == 256) ? 0x00 : rq->nr_sectors;
602
603         if (blk_rq_tagged(rq)) {
604                 args.tfRegister[IDE_FEATURE_OFFSET] = sectors;
605                 args.tfRegister[IDE_NSECTOR_OFFSET] = rq->tag << 3;
606         } else
607                 args.tfRegister[IDE_NSECTOR_OFFSET] = sectors;
608
609         args.tfRegister[IDE_SECTOR_OFFSET]      = sect;
610         args.tfRegister[IDE_LCYL_OFFSET]        = cyl;
611         args.tfRegister[IDE_HCYL_OFFSET]        = (cyl>>8);
612         args.tfRegister[IDE_SELECT_OFFSET]      = head;
613         args.tfRegister[IDE_SELECT_OFFSET]      |= drive->select.all;
614         args.tfRegister[IDE_COMMAND_OFFSET]     = get_command(drive, rq_data_dir(rq), &args);
615         args.rq                                 = (struct request *) rq;
616         rq->special                             = (ide_task_t *)&args;
617         return do_rw_taskfile(drive, &args);
618 }
619
620 static ide_startstop_t lba_28_rw_disk (ide_drive_t *drive, struct request *rq, unsigned long block)
621 {
622         ide_task_t              args;
623         int                     sectors;
624         ata_nsector_t           nsectors;
625
626         nsectors.all = (u16) rq->nr_sectors;
627
628 #ifdef DEBUG
629         printk("%s: %sing: ", drive->name, (rq_data_dir(rq)==READ) ? "read" : "writ");
630         printk("LBAsect=%lld, ", block);
631         printk("sectors=%ld, ", rq->nr_sectors);
632         printk("buffer=0x%08lx\n", (unsigned long) rq->buffer);
633 #endif
634
635         memset(&args, 0, sizeof(ide_task_t));
636
637         sectors = (rq->nr_sectors == 256) ? 0x00 : rq->nr_sectors;
638
639         if (blk_rq_tagged(rq)) {
640                 args.tfRegister[IDE_FEATURE_OFFSET] = sectors;
641                 args.tfRegister[IDE_NSECTOR_OFFSET] = rq->tag << 3;
642         } else
643                 args.tfRegister[IDE_NSECTOR_OFFSET] = sectors;
644
645         args.tfRegister[IDE_SECTOR_OFFSET]      = block;
646         args.tfRegister[IDE_LCYL_OFFSET]        = (block>>=8);
647         args.tfRegister[IDE_HCYL_OFFSET]        = (block>>=8);
648         args.tfRegister[IDE_SELECT_OFFSET]      = ((block>>8)&0x0f);
649         args.tfRegister[IDE_SELECT_OFFSET]      |= drive->select.all;
650         args.tfRegister[IDE_COMMAND_OFFSET]     = get_command(drive, rq_data_dir(rq), &args);
651         args.rq                                 = (struct request *) rq;
652         rq->special                             = (ide_task_t *)&args;
653         return do_rw_taskfile(drive, &args);
654 }
655
656 /*
657  * 268435455  == 137439 MB or 28bit limit
658  * 320173056  == 163929 MB or 48bit addressing
659  * 1073741822 == 549756 MB or 48bit addressing fake drive
660  */
661
662 static ide_startstop_t lba_48_rw_disk (ide_drive_t *drive, struct request *rq, unsigned long long block)
663 {
664         ide_task_t              args;
665         int                     sectors;
666         ata_nsector_t           nsectors;
667
668         nsectors.all = (u16) rq->nr_sectors;
669
670 #ifdef DEBUG
671         printk("%s: %sing: ", drive->name, (rq_data_dir(rq)==READ) ? "read" : "writ");
672         printk("LBAsect=%lld, ", block);
673         printk("sectors=%ld, ", rq->nr_sectors);
674         printk("buffer=0x%08lx\n", (unsigned long) rq->buffer);
675 #endif
676
677         memset(&args, 0, sizeof(ide_task_t));
678
679         sectors = (rq->nr_sectors == 65536) ? 0 : rq->nr_sectors;
680
681         if (blk_rq_tagged(rq)) {
682                 args.tfRegister[IDE_FEATURE_OFFSET] = sectors;
683                 args.tfRegister[IDE_NSECTOR_OFFSET] = rq->tag << 3;
684                 args.hobRegister[IDE_FEATURE_OFFSET] = sectors >> 8;
685                 args.hobRegister[IDE_NSECTOR_OFFSET] = 0;
686         } else {
687                 args.tfRegister[IDE_NSECTOR_OFFSET] = sectors;
688                 args.hobRegister[IDE_NSECTOR_OFFSET] = sectors >> 8;
689         }
690
691         args.tfRegister[IDE_SECTOR_OFFSET]      = block;        /* low lba */
692         args.tfRegister[IDE_LCYL_OFFSET]        = (block>>=8);  /* mid lba */
693         args.tfRegister[IDE_HCYL_OFFSET]        = (block>>=8);  /* hi  lba */
694         args.tfRegister[IDE_SELECT_OFFSET]      = drive->select.all;
695         args.tfRegister[IDE_COMMAND_OFFSET]     = get_command(drive, rq_data_dir(rq), &args);
696         args.hobRegister[IDE_SECTOR_OFFSET]     = (block>>=8);  /* low lba */
697         args.hobRegister[IDE_LCYL_OFFSET]       = (block>>=8);  /* mid lba */
698         args.hobRegister[IDE_HCYL_OFFSET]       = (block>>=8);  /* hi  lba */
699         args.hobRegister[IDE_SELECT_OFFSET]     = drive->select.all;
700         args.hobRegister[IDE_CONTROL_OFFSET_HOB]= (drive->ctl|0x80);
701         args.rq                                 = (struct request *) rq;
702         rq->special                             = (ide_task_t *)&args;
703         return do_rw_taskfile(drive, &args);
704 }
705
706 #endif /* CONFIG_IDE_TASKFILE_IO */
707
708 static ide_startstop_t ide_do_rw_disk (ide_drive_t *drive, struct request *rq, sector_t block)
709 {
710         ide_hwif_t *hwif = HWIF(drive);
711
712         BUG_ON(drive->blocked);
713
714         if (!blk_fs_request(rq)) {
715                 blk_dump_rq_flags(rq, "ide_do_rw_disk - bad command");
716                 ide_end_request(drive, 0, 0);
717                 return ide_stopped;
718         }
719
720         if (drive->using_tcq && idedisk_start_tag(drive, rq)) {
721                 if (!ata_pending_commands(drive))
722                         BUG();
723
724                 return ide_started;
725         }
726
727         if (hwif->rw_disk)
728                 return hwif->rw_disk(drive, rq, block);
729         else
730                 return __ide_do_rw_disk(drive, rq, block);
731 }
732
733 static u8 idedisk_dump_status (ide_drive_t *drive, const char *msg, u8 stat)
734 {
735         ide_hwif_t *hwif = HWIF(drive);
736         unsigned long flags;
737         u8 err = 0;
738
739         local_irq_set(flags);
740         printk("%s: %s: status=0x%02x", drive->name, msg, stat);
741 #if FANCY_STATUS_DUMPS
742         printk(" { ");
743         if (stat & BUSY_STAT)
744                 printk("Busy ");
745         else {
746                 if (stat & READY_STAT)  printk("DriveReady ");
747                 if (stat & WRERR_STAT)  printk("DeviceFault ");
748                 if (stat & SEEK_STAT)   printk("SeekComplete ");
749                 if (stat & DRQ_STAT)    printk("DataRequest ");
750                 if (stat & ECC_STAT)    printk("CorrectedError ");
751                 if (stat & INDEX_STAT)  printk("Index ");
752                 if (stat & ERR_STAT)    printk("Error ");
753         }
754         printk("}");
755 #endif  /* FANCY_STATUS_DUMPS */
756         printk("\n");
757         if ((stat & (BUSY_STAT|ERR_STAT)) == ERR_STAT) {
758                 err = hwif->INB(IDE_ERROR_REG);
759                 printk("%s: %s: error=0x%02x", drive->name, msg, err);
760 #if FANCY_STATUS_DUMPS
761                 printk(" { ");
762                 if (err & ABRT_ERR)     printk("DriveStatusError ");
763                 if (err & ICRC_ERR)
764                         printk("Bad%s ", (err & ABRT_ERR) ? "CRC" : "Sector");
765                 if (err & ECC_ERR)      printk("UncorrectableError ");
766                 if (err & ID_ERR)       printk("SectorIdNotFound ");
767                 if (err & TRK0_ERR)     printk("TrackZeroNotFound ");
768                 if (err & MARK_ERR)     printk("AddrMarkNotFound ");
769                 printk("}");
770                 if ((err & (BBD_ERR | ABRT_ERR)) == BBD_ERR ||
771                     (err & (ECC_ERR|ID_ERR|MARK_ERR))) {
772                         if (drive->addressing == 1) {
773                                 __u64 sectors = 0;
774                                 u32 low = 0, high = 0;
775                                 low = ide_read_24(drive);
776                                 hwif->OUTB(drive->ctl|0x80, IDE_CONTROL_REG);
777                                 high = ide_read_24(drive);
778                                 sectors = ((__u64)high << 24) | low;
779                                 printk(", LBAsect=%llu, high=%d, low=%d",
780                                        (unsigned long long) sectors,
781                                        high, low);
782                         } else {
783                                 u8 cur = hwif->INB(IDE_SELECT_REG);
784                                 if (cur & 0x40) {       /* using LBA? */
785                                         printk(", LBAsect=%ld", (unsigned long)
786                                          ((cur&0xf)<<24)
787                                          |(hwif->INB(IDE_HCYL_REG)<<16)
788                                          |(hwif->INB(IDE_LCYL_REG)<<8)
789                                          | hwif->INB(IDE_SECTOR_REG));
790                                 } else {
791                                         printk(", CHS=%d/%d/%d",
792                                          (hwif->INB(IDE_HCYL_REG)<<8) +
793                                           hwif->INB(IDE_LCYL_REG),
794                                           cur & 0xf,
795                                           hwif->INB(IDE_SECTOR_REG));
796                                 }
797                         }
798                         if (HWGROUP(drive) && HWGROUP(drive)->rq)
799                                 printk(", sector=%llu",
800                                         (unsigned long long)HWGROUP(drive)->rq->sector);
801                 }
802         }
803 #endif  /* FANCY_STATUS_DUMPS */
804         printk("\n");
805         local_irq_restore(flags);
806         return err;
807 }
808
809 ide_startstop_t idedisk_error (ide_drive_t *drive, const char *msg, u8 stat)
810 {
811         ide_hwif_t *hwif;
812         struct request *rq;
813         u8 err;
814         int i = (drive->mult_count ? drive->mult_count : 1) * SECTOR_WORDS;
815
816         err = idedisk_dump_status(drive, msg, stat);
817
818         if (drive == NULL || (rq = HWGROUP(drive)->rq) == NULL)
819                 return ide_stopped;
820
821         hwif = HWIF(drive);
822         /* retry only "normal" I/O: */
823         if (rq->flags & (REQ_DRIVE_CMD | REQ_DRIVE_TASK | REQ_DRIVE_TASKFILE)) {
824                 rq->errors = 1;
825                 ide_end_drive_cmd(drive, stat, err);
826                 return ide_stopped;
827         }
828 #ifdef CONFIG_IDE_TASKFILE_IO
829         /* make rq completion pointers new submission pointers */
830         blk_rq_prep_restart(rq);
831 #endif
832
833         if (stat & BUSY_STAT || ((stat & WRERR_STAT) && !drive->nowerr)) {
834                 /* other bits are useless when BUSY */
835                 rq->errors |= ERROR_RESET;
836         } else if (stat & ERR_STAT) {
837                 /* err has different meaning on cdrom and tape */
838                 if (err == ABRT_ERR) {
839                         if (drive->select.b.lba &&
840                             /* some newer drives don't support WIN_SPECIFY */
841                             hwif->INB(IDE_COMMAND_REG) == WIN_SPECIFY)
842                                 return ide_stopped;
843                 } else if ((err & BAD_CRC) == BAD_CRC) {
844                         /* UDMA crc error, just retry the operation */
845                         drive->crc_count++;
846                 } else if (err & (BBD_ERR | ECC_ERR)) {
847                         /* retries won't help these */
848                         rq->errors = ERROR_MAX;
849                 } else if (err & TRK0_ERR) {
850                         /* help it find track zero */
851                         rq->errors |= ERROR_RECAL;
852                 }
853         }
854         if ((stat & DRQ_STAT) && rq_data_dir(rq) == READ) {
855                 /*
856                  * try_to_flush_leftover_data() is invoked in response to
857                  * a drive unexpectedly having its DRQ_STAT bit set.  As
858                  * an alternative to resetting the drive, this routine
859                  * tries to clear the condition by read a sector's worth
860                  * of data from the drive.  Of course, this may not help
861                  * if the drive is *waiting* for data from *us*.
862                  */
863                 while (i > 0) {
864                         u32 buffer[16];
865                         unsigned int wcount = (i > 16) ? 16 : i;
866                         i -= wcount;
867                         taskfile_input_data(drive, buffer, wcount);
868                 }
869         }
870         if (hwif->INB(IDE_STATUS_REG) & (BUSY_STAT|DRQ_STAT)) {
871                 /* force an abort */
872                 hwif->OUTB(WIN_IDLEIMMEDIATE,IDE_COMMAND_REG);
873         }
874         if (rq->errors >= ERROR_MAX || blk_noretry_request(rq))
875                 DRIVER(drive)->end_request(drive, 0, 0);
876         else {
877                 if ((rq->errors & ERROR_RESET) == ERROR_RESET) {
878                         ++rq->errors;
879                         return ide_do_reset(drive);
880                 }
881                 if ((rq->errors & ERROR_RECAL) == ERROR_RECAL)
882                         drive->special.b.recalibrate = 1;
883                 ++rq->errors;
884         }
885         return ide_stopped;
886 }
887
888 ide_startstop_t idedisk_abort(ide_drive_t *drive, const char *msg)
889 {
890         ide_hwif_t *hwif;
891         struct request *rq;
892
893         if (drive == NULL || (rq = HWGROUP(drive)->rq) == NULL)
894                 return ide_stopped;
895
896         hwif = HWIF(drive);
897
898         if (rq->flags & (REQ_DRIVE_CMD | REQ_DRIVE_TASK | REQ_DRIVE_TASKFILE)) {
899                 rq->errors = 1;
900                 ide_end_drive_cmd(drive, BUSY_STAT, 0);
901                 return ide_stopped;
902         }
903
904         DRIVER(drive)->end_request(drive, 0, 0);
905         return ide_stopped;
906 }
907
908 /*
909  * Queries for true maximum capacity of the drive.
910  * Returns maximum LBA address (> 0) of the drive, 0 if failed.
911  */
912 static unsigned long idedisk_read_native_max_address(ide_drive_t *drive)
913 {
914         ide_task_t args;
915         unsigned long addr = 0;
916
917         /* Create IDE/ATA command request structure */
918         memset(&args, 0, sizeof(ide_task_t));
919         args.tfRegister[IDE_SELECT_OFFSET]      = 0x40;
920         args.tfRegister[IDE_COMMAND_OFFSET]     = WIN_READ_NATIVE_MAX;
921         args.command_type                       = IDE_DRIVE_TASK_NO_DATA;
922         args.handler                            = &task_no_data_intr;
923         /* submit command request */
924         ide_raw_taskfile(drive, &args, NULL);
925
926         /* if OK, compute maximum address value */
927         if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
928                 addr = ((args.tfRegister[IDE_SELECT_OFFSET] & 0x0f) << 24)
929                      | ((args.tfRegister[  IDE_HCYL_OFFSET]       ) << 16)
930                      | ((args.tfRegister[  IDE_LCYL_OFFSET]       ) <<  8)
931                      | ((args.tfRegister[IDE_SECTOR_OFFSET]       ));
932                 addr++; /* since the return value is (maxlba - 1), we add 1 */
933         }
934         return addr;
935 }
936
937 static unsigned long long idedisk_read_native_max_address_ext(ide_drive_t *drive)
938 {
939         ide_task_t args;
940         unsigned long long addr = 0;
941
942         /* Create IDE/ATA command request structure */
943         memset(&args, 0, sizeof(ide_task_t));
944
945         args.tfRegister[IDE_SELECT_OFFSET]      = 0x40;
946         args.tfRegister[IDE_COMMAND_OFFSET]     = WIN_READ_NATIVE_MAX_EXT;
947         args.command_type                       = IDE_DRIVE_TASK_NO_DATA;
948         args.handler                            = &task_no_data_intr;
949         /* submit command request */
950         ide_raw_taskfile(drive, &args, NULL);
951
952         /* if OK, compute maximum address value */
953         if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
954                 u32 high = (args.hobRegister[IDE_HCYL_OFFSET] << 16) |
955                            (args.hobRegister[IDE_LCYL_OFFSET] <<  8) |
956                             args.hobRegister[IDE_SECTOR_OFFSET];
957                 u32 low  = ((args.tfRegister[IDE_HCYL_OFFSET])<<16) |
958                            ((args.tfRegister[IDE_LCYL_OFFSET])<<8) |
959                             (args.tfRegister[IDE_SECTOR_OFFSET]);
960                 addr = ((__u64)high << 24) | low;
961                 addr++; /* since the return value is (maxlba - 1), we add 1 */
962         }
963         return addr;
964 }
965
966 #ifdef CONFIG_IDEDISK_STROKE
967 /*
968  * Sets maximum virtual LBA address of the drive.
969  * Returns new maximum virtual LBA address (> 0) or 0 on failure.
970  */
971 static unsigned long idedisk_set_max_address(ide_drive_t *drive, unsigned long addr_req)
972 {
973         ide_task_t args;
974         unsigned long addr_set = 0;
975         
976         addr_req--;
977         /* Create IDE/ATA command request structure */
978         memset(&args, 0, sizeof(ide_task_t));
979         args.tfRegister[IDE_SECTOR_OFFSET]      = ((addr_req >>  0) & 0xff);
980         args.tfRegister[IDE_LCYL_OFFSET]        = ((addr_req >>  8) & 0xff);
981         args.tfRegister[IDE_HCYL_OFFSET]        = ((addr_req >> 16) & 0xff);
982         args.tfRegister[IDE_SELECT_OFFSET]      = ((addr_req >> 24) & 0x0f) | 0x40;
983         args.tfRegister[IDE_COMMAND_OFFSET]     = WIN_SET_MAX;
984         args.command_type                       = IDE_DRIVE_TASK_NO_DATA;
985         args.handler                            = &task_no_data_intr;
986         /* submit command request */
987         ide_raw_taskfile(drive, &args, NULL);
988         /* if OK, read new maximum address value */
989         if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
990                 addr_set = ((args.tfRegister[IDE_SELECT_OFFSET] & 0x0f) << 24)
991                          | ((args.tfRegister[  IDE_HCYL_OFFSET]       ) << 16)
992                          | ((args.tfRegister[  IDE_LCYL_OFFSET]       ) <<  8)
993                          | ((args.tfRegister[IDE_SECTOR_OFFSET]       ));
994                 addr_set++;
995         }
996         return addr_set;
997 }
998
999 static unsigned long long idedisk_set_max_address_ext(ide_drive_t *drive, unsigned long long addr_req)
1000 {
1001         ide_task_t args;
1002         unsigned long long addr_set = 0;
1003
1004         addr_req--;
1005         /* Create IDE/ATA command request structure */
1006         memset(&args, 0, sizeof(ide_task_t));
1007         args.tfRegister[IDE_SECTOR_OFFSET]      = ((addr_req >>  0) & 0xff);
1008         args.tfRegister[IDE_LCYL_OFFSET]        = ((addr_req >>= 8) & 0xff);
1009         args.tfRegister[IDE_HCYL_OFFSET]        = ((addr_req >>= 8) & 0xff);
1010         args.tfRegister[IDE_SELECT_OFFSET]      = 0x40;
1011         args.tfRegister[IDE_COMMAND_OFFSET]     = WIN_SET_MAX_EXT;
1012         args.hobRegister[IDE_SECTOR_OFFSET]     = (addr_req >>= 8) & 0xff;
1013         args.hobRegister[IDE_LCYL_OFFSET]       = (addr_req >>= 8) & 0xff;
1014         args.hobRegister[IDE_HCYL_OFFSET]       = (addr_req >>= 8) & 0xff;
1015         args.hobRegister[IDE_SELECT_OFFSET]     = 0x40;
1016         args.hobRegister[IDE_CONTROL_OFFSET_HOB]= (drive->ctl|0x80);
1017         args.command_type                       = IDE_DRIVE_TASK_NO_DATA;
1018         args.handler                            = &task_no_data_intr;
1019         /* submit command request */
1020         ide_raw_taskfile(drive, &args, NULL);
1021         /* if OK, compute maximum address value */
1022         if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
1023                 u32 high = (args.hobRegister[IDE_HCYL_OFFSET] << 16) |
1024                            (args.hobRegister[IDE_LCYL_OFFSET] <<  8) |
1025                             args.hobRegister[IDE_SECTOR_OFFSET];
1026                 u32 low  = ((args.tfRegister[IDE_HCYL_OFFSET])<<16) |
1027                            ((args.tfRegister[IDE_LCYL_OFFSET])<<8) |
1028                             (args.tfRegister[IDE_SECTOR_OFFSET]);
1029                 addr_set = ((__u64)high << 24) | low;
1030                 addr_set++;
1031         }
1032         return addr_set;
1033 }
1034
1035 #endif /* CONFIG_IDEDISK_STROKE */
1036
1037 static unsigned long long sectors_to_MB(unsigned long long n)
1038 {
1039         n <<= 9;                /* make it bytes */
1040         do_div(n, 1000000);     /* make it MB */
1041         return n;
1042 }
1043
1044 /*
1045  * Bits 10 of command_set_1 and cfs_enable_1 must be equal,
1046  * so on non-buggy drives we need test only one.
1047  * However, we should also check whether these fields are valid.
1048  */
1049 static inline int idedisk_supports_hpa(const struct hd_driveid *id)
1050 {
1051         return (id->command_set_1 & 0x0400) && (id->cfs_enable_1 & 0x0400);
1052 }
1053
1054 /*
1055  * The same here.
1056  */
1057 static inline int idedisk_supports_lba48(const struct hd_driveid *id)
1058 {
1059         return (id->command_set_2 & 0x0400) && (id->cfs_enable_2 & 0x0400)
1060                && id->lba_capacity_2;
1061 }
1062
1063 static inline void idedisk_check_hpa(ide_drive_t *drive)
1064 {
1065         unsigned long long capacity, set_max;
1066         int lba48 = idedisk_supports_lba48(drive->id);
1067
1068         capacity = drive->capacity64;
1069         if (lba48)
1070                 set_max = idedisk_read_native_max_address_ext(drive);
1071         else
1072                 set_max = idedisk_read_native_max_address(drive);
1073
1074         if (set_max <= capacity)
1075                 return;
1076
1077         printk(KERN_INFO "%s: Host Protected Area detected.\n"
1078                          "\tcurrent capacity is %llu sectors (%llu MB)\n"
1079                          "\tnative  capacity is %llu sectors (%llu MB)\n",
1080                          drive->name,
1081                          capacity, sectors_to_MB(capacity),
1082                          set_max, sectors_to_MB(set_max));
1083 #ifdef CONFIG_IDEDISK_STROKE
1084         if (lba48)
1085                 set_max = idedisk_set_max_address_ext(drive, set_max);
1086         else
1087                 set_max = idedisk_set_max_address(drive, set_max);
1088         if (set_max) {
1089                 drive->capacity64 = set_max;
1090                 printk(KERN_INFO "%s: Host Protected Area disabled.\n",
1091                                  drive->name);
1092         }
1093 #endif
1094 }
1095
1096 /*
1097  * Compute drive->capacity, the full capacity of the drive
1098  * Called with drive->id != NULL.
1099  *
1100  * To compute capacity, this uses either of
1101  *
1102  *    1. CHS value set by user       (whatever user sets will be trusted)
1103  *    2. LBA value from target drive (require new ATA feature)
1104  *    3. LBA value from system BIOS  (new one is OK, old one may break)
1105  *    4. CHS value from system BIOS  (traditional style)
1106  *
1107  * in above order (i.e., if value of higher priority is available,
1108  * reset will be ignored).
1109  */
1110 static void init_idedisk_capacity (ide_drive_t  *drive)
1111 {
1112         struct hd_driveid *id = drive->id;
1113         /*
1114          * If this drive supports the Host Protected Area feature set,
1115          * then we may need to change our opinion about the drive's capacity.
1116          */
1117         int hpa = idedisk_supports_hpa(id);
1118
1119         if (idedisk_supports_lba48(id)) {
1120                 /* drive speaks 48-bit LBA */
1121                 drive->select.b.lba = 1;
1122                 drive->capacity64 = id->lba_capacity_2;
1123                 if (hpa)
1124                         idedisk_check_hpa(drive);
1125         } else if ((id->capability & 2) && lba_capacity_is_ok(id)) {
1126                 /* drive speaks 28-bit LBA */
1127                 drive->select.b.lba = 1;
1128                 drive->capacity64 = id->lba_capacity;
1129                 if (hpa)
1130                         idedisk_check_hpa(drive);
1131         } else {
1132                 /* drive speaks boring old 28-bit CHS */
1133                 drive->capacity64 = drive->cyl * drive->head * drive->sect;
1134         }
1135 }
1136
1137 static sector_t idedisk_capacity (ide_drive_t *drive)
1138 {
1139         return drive->capacity64 - drive->sect0;
1140 }
1141
1142 static ide_startstop_t idedisk_special (ide_drive_t *drive)
1143 {
1144         special_t *s = &drive->special;
1145
1146         if (s->b.set_geometry) {
1147                 s->b.set_geometry       = 0;
1148                 if (!IS_PDC4030_DRIVE) {
1149                         ide_task_t args;
1150                         memset(&args, 0, sizeof(ide_task_t));
1151                         args.tfRegister[IDE_NSECTOR_OFFSET] = drive->sect;
1152                         args.tfRegister[IDE_SECTOR_OFFSET]  = drive->sect;
1153                         args.tfRegister[IDE_LCYL_OFFSET]    = drive->cyl;
1154                         args.tfRegister[IDE_HCYL_OFFSET]    = drive->cyl>>8;
1155                         args.tfRegister[IDE_SELECT_OFFSET]  = ((drive->head-1)|drive->select.all)&0xBF;
1156                         args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SPECIFY;
1157                         args.command_type = IDE_DRIVE_TASK_NO_DATA;
1158                         args.handler      = &set_geometry_intr;
1159                         do_rw_taskfile(drive, &args);
1160                 }
1161         } else if (s->b.recalibrate) {
1162                 s->b.recalibrate = 0;
1163                 if (!IS_PDC4030_DRIVE) {
1164                         ide_task_t args;
1165                         memset(&args, 0, sizeof(ide_task_t));
1166                         args.tfRegister[IDE_NSECTOR_OFFSET] = drive->sect;
1167                         args.tfRegister[IDE_COMMAND_OFFSET] = WIN_RESTORE;
1168                         args.command_type = IDE_DRIVE_TASK_NO_DATA;
1169                         args.handler      = &recal_intr;
1170                         do_rw_taskfile(drive, &args);
1171                 }
1172         } else if (s->b.set_multmode) {
1173                 s->b.set_multmode = 0;
1174                 if (drive->mult_req > drive->id->max_multsect)
1175                         drive->mult_req = drive->id->max_multsect;
1176                 if (!IS_PDC4030_DRIVE) {
1177                         ide_task_t args;
1178                         memset(&args, 0, sizeof(ide_task_t));
1179                         args.tfRegister[IDE_NSECTOR_OFFSET] = drive->mult_req;
1180                         args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SETMULT;
1181                         args.command_type = IDE_DRIVE_TASK_NO_DATA;
1182                         args.handler      = &set_multmode_intr;
1183                         do_rw_taskfile(drive, &args);
1184                 }
1185         } else if (s->all) {
1186                 int special = s->all;
1187                 s->all = 0;
1188                 printk(KERN_ERR "%s: bad special flag: 0x%02x\n", drive->name, special);
1189                 return ide_stopped;
1190         }
1191         return IS_PDC4030_DRIVE ? ide_stopped : ide_started;
1192 }
1193
1194 static void idedisk_pre_reset (ide_drive_t *drive)
1195 {
1196         int legacy = (drive->id->cfs_enable_2 & 0x0400) ? 0 : 1;
1197
1198         drive->special.all = 0;
1199         drive->special.b.set_geometry = legacy;
1200         drive->special.b.recalibrate  = legacy;
1201         if (OK_TO_RESET_CONTROLLER)
1202                 drive->mult_count = 0;
1203         if (!drive->keep_settings && !drive->using_dma)
1204                 drive->mult_req = 0;
1205         if (drive->mult_req != drive->mult_count)
1206                 drive->special.b.set_multmode = 1;
1207 }
1208
1209 #ifdef CONFIG_PROC_FS
1210
1211 static int smart_enable(ide_drive_t *drive)
1212 {
1213         ide_task_t args;
1214
1215         memset(&args, 0, sizeof(ide_task_t));
1216         args.tfRegister[IDE_FEATURE_OFFSET]     = SMART_ENABLE;
1217         args.tfRegister[IDE_LCYL_OFFSET]        = SMART_LCYL_PASS;
1218         args.tfRegister[IDE_HCYL_OFFSET]        = SMART_HCYL_PASS;
1219         args.tfRegister[IDE_COMMAND_OFFSET]     = WIN_SMART;
1220         args.command_type                       = IDE_DRIVE_TASK_NO_DATA;
1221         args.handler                            = &task_no_data_intr;
1222         return ide_raw_taskfile(drive, &args, NULL);
1223 }
1224
1225 static int get_smart_values(ide_drive_t *drive, u8 *buf)
1226 {
1227         ide_task_t args;
1228
1229         memset(&args, 0, sizeof(ide_task_t));
1230         args.tfRegister[IDE_FEATURE_OFFSET]     = SMART_READ_VALUES;
1231         args.tfRegister[IDE_NSECTOR_OFFSET]     = 0x01;
1232         args.tfRegister[IDE_LCYL_OFFSET]        = SMART_LCYL_PASS;
1233         args.tfRegister[IDE_HCYL_OFFSET]        = SMART_HCYL_PASS;
1234         args.tfRegister[IDE_COMMAND_OFFSET]     = WIN_SMART;
1235         args.command_type                       = IDE_DRIVE_TASK_IN;
1236         args.handler                            = &task_in_intr;
1237         (void) smart_enable(drive);
1238         return ide_raw_taskfile(drive, &args, buf);
1239 }
1240
1241 static int get_smart_thresholds(ide_drive_t *drive, u8 *buf)
1242 {
1243         ide_task_t args;
1244         memset(&args, 0, sizeof(ide_task_t));
1245         args.tfRegister[IDE_FEATURE_OFFSET]     = SMART_READ_THRESHOLDS;
1246         args.tfRegister[IDE_NSECTOR_OFFSET]     = 0x01;
1247         args.tfRegister[IDE_LCYL_OFFSET]        = SMART_LCYL_PASS;
1248         args.tfRegister[IDE_HCYL_OFFSET]        = SMART_HCYL_PASS;
1249         args.tfRegister[IDE_COMMAND_OFFSET]     = WIN_SMART;
1250         args.command_type                       = IDE_DRIVE_TASK_IN;
1251         args.handler                            = &task_in_intr;
1252         (void) smart_enable(drive);
1253         return ide_raw_taskfile(drive, &args, buf);
1254 }
1255
1256 static int proc_idedisk_read_cache
1257         (char *page, char **start, off_t off, int count, int *eof, void *data)
1258 {
1259         ide_drive_t     *drive = (ide_drive_t *) data;
1260         char            *out = page;
1261         int             len;
1262
1263         if (drive->id_read)
1264                 len = sprintf(out,"%i\n", drive->id->buf_size / 2);
1265         else
1266                 len = sprintf(out,"(none)\n");
1267         PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
1268 }
1269
1270 static int proc_idedisk_read_smart_thresholds
1271         (char *page, char **start, off_t off, int count, int *eof, void *data)
1272 {
1273         ide_drive_t     *drive = (ide_drive_t *)data;
1274         int             len = 0, i = 0;
1275
1276         if (!get_smart_thresholds(drive, page)) {
1277                 unsigned short *val = (unsigned short *) page;
1278                 char *out = ((char *)val) + (SECTOR_WORDS * 4);
1279                 page = out;
1280                 do {
1281                         out += sprintf(out, "%04x%c", le16_to_cpu(*val), (++i & 7) ? ' ' : '\n');
1282                         val += 1;
1283                 } while (i < (SECTOR_WORDS * 2));
1284                 len = out - page;
1285         }
1286         PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
1287 }
1288
1289 static int proc_idedisk_read_smart_values
1290         (char *page, char **start, off_t off, int count, int *eof, void *data)
1291 {
1292         ide_drive_t     *drive = (ide_drive_t *)data;
1293         int             len = 0, i = 0;
1294
1295         if (!get_smart_values(drive, page)) {
1296                 unsigned short *val = (unsigned short *) page;
1297                 char *out = ((char *)val) + (SECTOR_WORDS * 4);
1298                 page = out;
1299                 do {
1300                         out += sprintf(out, "%04x%c", le16_to_cpu(*val), (++i & 7) ? ' ' : '\n');
1301                         val += 1;
1302                 } while (i < (SECTOR_WORDS * 2));
1303                 len = out - page;
1304         }
1305         PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
1306 }
1307
1308 static ide_proc_entry_t idedisk_proc[] = {
1309         { "cache",              S_IFREG|S_IRUGO,        proc_idedisk_read_cache,                NULL },
1310         { "geometry",           S_IFREG|S_IRUGO,        proc_ide_read_geometry,                 NULL },
1311         { "smart_values",       S_IFREG|S_IRUSR,        proc_idedisk_read_smart_values,         NULL },
1312         { "smart_thresholds",   S_IFREG|S_IRUSR,        proc_idedisk_read_smart_thresholds,     NULL },
1313         { NULL, 0, NULL, NULL }
1314 };
1315
1316 #else
1317
1318 #define idedisk_proc    NULL
1319
1320 #endif  /* CONFIG_PROC_FS */
1321
1322 /*
1323  * This is tightly woven into the driver->do_special can not touch.
1324  * DON'T do it again until a total personality rewrite is committed.
1325  */
1326 static int set_multcount(ide_drive_t *drive, int arg)
1327 {
1328         struct request rq;
1329
1330         if (drive->special.b.set_multmode)
1331                 return -EBUSY;
1332         ide_init_drive_cmd (&rq);
1333         rq.flags = REQ_DRIVE_CMD;
1334         drive->mult_req = arg;
1335         drive->special.b.set_multmode = 1;
1336         (void) ide_do_drive_cmd (drive, &rq, ide_wait);
1337         return (drive->mult_count == arg) ? 0 : -EIO;
1338 }
1339
1340 static int set_nowerr(ide_drive_t *drive, int arg)
1341 {
1342         if (ide_spin_wait_hwgroup(drive))
1343                 return -EBUSY;
1344         drive->nowerr = arg;
1345         drive->bad_wstat = arg ? BAD_R_STAT : BAD_W_STAT;
1346         spin_unlock_irq(&ide_lock);
1347         return 0;
1348 }
1349
1350 /* check if CACHE FLUSH (EXT) command is supported (bits defined in ATA-6) */
1351 #define ide_id_has_flush_cache(id)      ((id)->cfs_enable_2 & 0x3000)
1352
1353 /* some Maxtor disks have bit 13 defined incorrectly so check bit 10 too */
1354 #define ide_id_has_flush_cache_ext(id)  \
1355         (((id)->cfs_enable_2 & 0x2400) == 0x2400)
1356
1357 static int write_cache (ide_drive_t *drive, int arg)
1358 {
1359         ide_task_t args;
1360
1361         if (!ide_id_has_flush_cache(drive->id))
1362                 return 1;
1363
1364         memset(&args, 0, sizeof(ide_task_t));
1365         args.tfRegister[IDE_FEATURE_OFFSET]     = (arg) ?
1366                         SETFEATURES_EN_WCACHE : SETFEATURES_DIS_WCACHE;
1367         args.tfRegister[IDE_COMMAND_OFFSET]     = WIN_SETFEATURES;
1368         args.command_type                       = IDE_DRIVE_TASK_NO_DATA;
1369         args.handler                            = &task_no_data_intr;
1370         (void) ide_raw_taskfile(drive, &args, NULL);
1371
1372         drive->wcache = arg;
1373         return 0;
1374 }
1375
1376 static int do_idedisk_flushcache (ide_drive_t *drive)
1377 {
1378         ide_task_t args;
1379
1380         memset(&args, 0, sizeof(ide_task_t));
1381         if (ide_id_has_flush_cache_ext(drive->id))
1382                 args.tfRegister[IDE_COMMAND_OFFSET]     = WIN_FLUSH_CACHE_EXT;
1383         else
1384                 args.tfRegister[IDE_COMMAND_OFFSET]     = WIN_FLUSH_CACHE;
1385         args.command_type                       = IDE_DRIVE_TASK_NO_DATA;
1386         args.handler                            = &task_no_data_intr;
1387         return ide_raw_taskfile(drive, &args, NULL);
1388 }
1389
1390 static int set_acoustic (ide_drive_t *drive, int arg)
1391 {
1392         ide_task_t args;
1393
1394         memset(&args, 0, sizeof(ide_task_t));
1395         args.tfRegister[IDE_FEATURE_OFFSET]     = (arg) ? SETFEATURES_EN_AAM :
1396                                                           SETFEATURES_DIS_AAM;
1397         args.tfRegister[IDE_NSECTOR_OFFSET]     = arg;
1398         args.tfRegister[IDE_COMMAND_OFFSET]     = WIN_SETFEATURES;
1399         args.command_type = IDE_DRIVE_TASK_NO_DATA;
1400         args.handler      = &task_no_data_intr;
1401         ide_raw_taskfile(drive, &args, NULL);
1402         drive->acoustic = arg;
1403         return 0;
1404 }
1405
1406 #ifdef CONFIG_BLK_DEV_IDE_TCQ
1407 static int set_using_tcq(ide_drive_t *drive, int arg)
1408 {
1409         int ret;
1410
1411         if (!drive->driver)
1412                 return -EPERM;
1413         if (arg == drive->queue_depth && drive->using_tcq)
1414                 return 0;
1415
1416         /*
1417          * set depth, but check also id for max supported depth
1418          */
1419         drive->queue_depth = arg ? arg : 1;
1420         if (drive->id) {
1421                 if (drive->queue_depth > drive->id->queue_depth + 1)
1422                         drive->queue_depth = drive->id->queue_depth + 1;
1423         }
1424
1425         if (arg)
1426                 ret = __ide_dma_queued_on(drive);
1427         else
1428                 ret = __ide_dma_queued_off(drive);
1429
1430         return ret ? -EIO : 0;
1431 }
1432 #endif
1433
1434 /*
1435  * drive->addressing:
1436  *      0: 28-bit
1437  *      1: 48-bit
1438  *      2: 48-bit capable doing 28-bit
1439  */
1440 static int set_lba_addressing(ide_drive_t *drive, int arg)
1441 {
1442         drive->addressing =  0;
1443
1444         if (HWIF(drive)->no_lba48)
1445                 return 0;
1446
1447         if (!idedisk_supports_lba48(drive->id))
1448                 return -EIO;
1449         drive->addressing = arg;
1450         return 0;
1451 }
1452
1453 static void idedisk_add_settings(ide_drive_t *drive)
1454 {
1455         struct hd_driveid *id = drive->id;
1456
1457         ide_add_setting(drive,  "bios_cyl",             SETTING_RW,                                     -1,                     -1,                     TYPE_INT,       0,      65535,                          1,      1,      &drive->bios_cyl,               NULL);
1458         ide_add_setting(drive,  "bios_head",            SETTING_RW,                                     -1,                     -1,                     TYPE_BYTE,      0,      255,                            1,      1,      &drive->bios_head,              NULL);
1459         ide_add_setting(drive,  "bios_sect",            SETTING_RW,                                     -1,                     -1,                     TYPE_BYTE,      0,      63,                             1,      1,      &drive->bios_sect,              NULL);
1460         ide_add_setting(drive,  "address",              SETTING_RW,                                     HDIO_GET_ADDRESS,       HDIO_SET_ADDRESS,       TYPE_INTA,      0,      2,                              1,      1,      &drive->addressing,     set_lba_addressing);
1461         ide_add_setting(drive,  "bswap",                SETTING_READ,                                   -1,                     -1,                     TYPE_BYTE,      0,      1,                              1,      1,      &drive->bswap,                  NULL);
1462         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);
1463         ide_add_setting(drive,  "nowerr",               SETTING_RW,                                     HDIO_GET_NOWERR,        HDIO_SET_NOWERR,        TYPE_BYTE,      0,      1,                              1,      1,      &drive->nowerr,                 set_nowerr);
1464         ide_add_setting(drive,  "lun",                  SETTING_RW,                                     -1,                     -1,                     TYPE_INT,       0,      7,                              1,      1,      &drive->lun,                    NULL);
1465         ide_add_setting(drive,  "wcache",               SETTING_RW,                                     HDIO_GET_WCACHE,        HDIO_SET_WCACHE,        TYPE_BYTE,      0,      1,                              1,      1,      &drive->wcache,                 write_cache);
1466         ide_add_setting(drive,  "acoustic",             SETTING_RW,                                     HDIO_GET_ACOUSTIC,      HDIO_SET_ACOUSTIC,      TYPE_BYTE,      0,      254,                            1,      1,      &drive->acoustic,               set_acoustic);
1467         ide_add_setting(drive,  "failures",             SETTING_RW,                                     -1,                     -1,                     TYPE_INT,       0,      65535,                          1,      1,      &drive->failures,               NULL);
1468         ide_add_setting(drive,  "max_failures",         SETTING_RW,                                     -1,                     -1,                     TYPE_INT,       0,      65535,                          1,      1,      &drive->max_failures,           NULL);
1469 #ifdef CONFIG_BLK_DEV_IDE_TCQ
1470         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);
1471 #endif
1472 }
1473
1474 /*
1475  * Power Management state machine. This one is rather trivial for now,
1476  * we should probably add more, like switching back to PIO on suspend
1477  * to help some BIOSes, re-do the door locking on resume, etc...
1478  */
1479
1480 enum {
1481         idedisk_pm_flush_cache  = ide_pm_state_start_suspend,
1482         idedisk_pm_standby,
1483
1484         idedisk_pm_restore_dma  = ide_pm_state_start_resume,
1485 };
1486
1487 static void idedisk_complete_power_step (ide_drive_t *drive, struct request *rq, u8 stat, u8 error)
1488 {
1489         switch (rq->pm->pm_step) {
1490         case idedisk_pm_flush_cache:    /* Suspend step 1 (flush cache) complete */
1491                 if (rq->pm->pm_state == 4)
1492                         rq->pm->pm_step = ide_pm_state_completed;
1493                 else
1494                         rq->pm->pm_step = idedisk_pm_standby;
1495                 break;
1496         case idedisk_pm_standby:        /* Suspend step 2 (standby) complete */
1497                 rq->pm->pm_step = ide_pm_state_completed;
1498                 break;
1499         }
1500 }
1501
1502 static ide_startstop_t idedisk_start_power_step (ide_drive_t *drive, struct request *rq)
1503 {
1504         ide_task_t *args = rq->special;
1505
1506         memset(args, 0, sizeof(*args));
1507
1508         switch (rq->pm->pm_step) {
1509         case idedisk_pm_flush_cache:    /* Suspend step 1 (flush cache) */
1510                 /* Not supported? Switch to next step now. */
1511                 if (!drive->wcache || !ide_id_has_flush_cache(drive->id)) {
1512                         idedisk_complete_power_step(drive, rq, 0, 0);
1513                         return ide_stopped;
1514                 }
1515                 if (ide_id_has_flush_cache_ext(drive->id))
1516                         args->tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE_EXT;
1517                 else
1518                         args->tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE;
1519                 args->command_type = IDE_DRIVE_TASK_NO_DATA;
1520                 args->handler      = &task_no_data_intr;
1521                 return do_rw_taskfile(drive, args);
1522         case idedisk_pm_standby:        /* Suspend step 2 (standby) */
1523                 args->tfRegister[IDE_COMMAND_OFFSET] = WIN_STANDBYNOW1;
1524                 args->command_type = IDE_DRIVE_TASK_NO_DATA;
1525                 args->handler      = &task_no_data_intr;
1526                 return do_rw_taskfile(drive, args);
1527
1528         case idedisk_pm_restore_dma:    /* Resume step 1 (restore DMA) */
1529                 /*
1530                  * Right now, all we do is call hwif->ide_dma_check(drive),
1531                  * we could be smarter and check for current xfer_speed
1532                  * in struct drive etc...
1533                  * Also, this step could be implemented as a generic helper
1534                  * as most subdrivers will use it
1535                  */
1536                 if ((drive->id->capability & 1) == 0)
1537                         break;
1538                 if (HWIF(drive)->ide_dma_check == NULL)
1539                         break;
1540                 HWIF(drive)->ide_dma_check(drive);
1541                 break;
1542         }
1543         rq->pm->pm_step = ide_pm_state_completed;
1544         return ide_stopped;
1545 }
1546
1547 static void idedisk_setup (ide_drive_t *drive)
1548 {
1549         struct hd_driveid *id = drive->id;
1550         unsigned long long capacity;
1551
1552         idedisk_add_settings(drive);
1553
1554         if (drive->id_read == 0)
1555                 return;
1556
1557         /*
1558          * CompactFlash cards and their brethern look just like hard drives
1559          * to us, but they are removable and don't have a doorlock mechanism.
1560          */
1561         if (drive->removable && !(drive->is_flash)) {
1562                 /*
1563                  * Removable disks (eg. SYQUEST); ignore 'WD' drives 
1564                  */
1565                 if (id->model[0] != 'W' || id->model[1] != 'D') {
1566                         drive->doorlocking = 1;
1567                 }
1568         }
1569
1570         (void)set_lba_addressing(drive, 1);
1571
1572         if (drive->addressing == 1) {
1573                 ide_hwif_t *hwif = HWIF(drive);
1574                 int max_s = 2048;
1575
1576                 if (max_s > hwif->rqsize)
1577                         max_s = hwif->rqsize;
1578
1579                 blk_queue_max_sectors(drive->queue, max_s);
1580         }
1581
1582         printk("%s: max request size: %dKiB\n", drive->name, drive->queue->max_sectors / 2);
1583
1584         /* Extract geometry if we did not already have one for the drive */
1585         if (!drive->cyl || !drive->head || !drive->sect) {
1586                 drive->cyl     = drive->bios_cyl  = id->cyls;
1587                 drive->head    = drive->bios_head = id->heads;
1588                 drive->sect    = drive->bios_sect = id->sectors;
1589         }
1590
1591         /* Handle logical geometry translation by the drive */
1592         if ((id->field_valid & 1) && id->cur_cyls &&
1593             id->cur_heads && (id->cur_heads <= 16) && id->cur_sectors) {
1594                 drive->cyl  = id->cur_cyls;
1595                 drive->head = id->cur_heads;
1596                 drive->sect = id->cur_sectors;
1597         }
1598
1599         /* Use physical geometry if what we have still makes no sense */
1600         if (drive->head > 16 && id->heads && id->heads <= 16) {
1601                 drive->cyl  = id->cyls;
1602                 drive->head = id->heads;
1603                 drive->sect = id->sectors;
1604         }
1605
1606         /* calculate drive capacity, and select LBA if possible */
1607         init_idedisk_capacity (drive);
1608
1609         /* limit drive capacity to 137GB if LBA48 cannot be used */
1610         if (drive->addressing == 0 && drive->capacity64 > 1ULL << 28) {
1611                 printk("%s: cannot use LBA48 - full capacity "
1612                        "%llu sectors (%llu MB)\n",
1613                        drive->name, (unsigned long long)drive->capacity64,
1614                        sectors_to_MB(drive->capacity64));
1615                 drive->capacity64 = 1ULL << 28;
1616         }
1617
1618         /*
1619          * if possible, give fdisk access to more of the drive,
1620          * by correcting bios_cyls:
1621          */
1622         capacity = idedisk_capacity (drive);
1623         if (!drive->forced_geom) {
1624
1625                 if (idedisk_supports_lba48(drive->id)) {
1626                         /* compatibility */
1627                         drive->bios_sect = 63;
1628                         drive->bios_head = 255;
1629                 }
1630
1631                 if (drive->bios_sect && drive->bios_head) {
1632                         unsigned int cap0 = capacity; /* truncate to 32 bits */
1633                         unsigned int cylsz, cyl;
1634
1635                         if (cap0 != capacity)
1636                                 drive->bios_cyl = 65535;
1637                         else {
1638                                 cylsz = drive->bios_sect * drive->bios_head;
1639                                 cyl = cap0 / cylsz;
1640                                 if (cyl > 65535)
1641                                         cyl = 65535;
1642                                 if (cyl > drive->bios_cyl)
1643                                         drive->bios_cyl = cyl;
1644                         }
1645                 }
1646         }
1647         printk(KERN_INFO "%s: %llu sectors (%llu MB)",
1648                          drive->name, capacity, sectors_to_MB(capacity));
1649
1650         /* Only print cache size when it was specified */
1651         if (id->buf_size)
1652                 printk (" w/%dKiB Cache", id->buf_size/2);
1653
1654         printk(", CHS=%d/%d/%d", 
1655                drive->bios_cyl, drive->bios_head, drive->bios_sect);
1656         if (drive->using_dma)
1657                 (void) HWIF(drive)->ide_dma_verbose(drive);
1658         printk("\n");
1659
1660         drive->mult_count = 0;
1661         if (id->max_multsect) {
1662 #ifdef CONFIG_IDEDISK_MULTI_MODE
1663                 id->multsect = ((id->max_multsect/2) > 1) ? id->max_multsect : 0;
1664                 id->multsect_valid = id->multsect ? 1 : 0;
1665                 drive->mult_req = id->multsect_valid ? id->max_multsect : INITIAL_MULT_COUNT;
1666                 drive->special.b.set_multmode = drive->mult_req ? 1 : 0;
1667 #else   /* original, pre IDE-NFG, per request of AC */
1668                 drive->mult_req = INITIAL_MULT_COUNT;
1669                 if (drive->mult_req > id->max_multsect)
1670                         drive->mult_req = id->max_multsect;
1671                 if (drive->mult_req || ((id->multsect_valid & 1) && id->multsect))
1672                         drive->special.b.set_multmode = 1;
1673 #endif  /* CONFIG_IDEDISK_MULTI_MODE */
1674         }
1675         drive->no_io_32bit = id->dword_io ? 1 : 0;
1676
1677         /* write cache enabled? */
1678         if ((id->csfo & 1) || (id->cfs_enable_1 & (1 << 5)))
1679                 drive->wcache = 1;
1680
1681         write_cache(drive, 1);
1682
1683 #ifdef CONFIG_BLK_DEV_IDE_TCQ_DEFAULT
1684         if (drive->using_dma)
1685                 __ide_dma_queued_on(drive);
1686 #endif
1687 }
1688
1689 static void ide_cacheflush_p(ide_drive_t *drive)
1690 {
1691         if (!drive->wcache || !ide_id_has_flush_cache(drive->id))
1692                 return;
1693
1694         if (do_idedisk_flushcache(drive))
1695                 printk(KERN_INFO "%s: wcache flush failed!\n", drive->name);
1696 }
1697
1698 static int idedisk_cleanup (ide_drive_t *drive)
1699 {
1700         struct gendisk *g = drive->disk;
1701         ide_cacheflush_p(drive);
1702         if (ide_unregister_subdriver(drive))
1703                 return 1;
1704         del_gendisk(g);
1705         drive->devfs_name[0] = '\0';
1706         g->fops = ide_fops;
1707         return 0;
1708 }
1709
1710 static int idedisk_attach(ide_drive_t *drive);
1711
1712 static void ide_device_shutdown(struct device *dev)
1713 {
1714         ide_drive_t *drive = container_of(dev, ide_drive_t, gendev);
1715
1716         if (system_state == SYSTEM_RESTART) {
1717                 ide_cacheflush_p(drive);
1718                 return;
1719         }
1720
1721         printk("Shutdown: %s\n", drive->name);
1722         dev->bus->suspend(dev, PM_SUSPEND_STANDBY);
1723 }
1724
1725 /*
1726  *      IDE subdriver functions, registered with ide.c
1727  */
1728 static ide_driver_t idedisk_driver = {
1729         .owner                  = THIS_MODULE,
1730         .gen_driver = {
1731                 .shutdown       = ide_device_shutdown,
1732         },
1733         .name                   = "ide-disk",
1734         .version                = IDEDISK_VERSION,
1735         .media                  = ide_disk,
1736         .busy                   = 0,
1737         .supports_dsc_overlap   = 0,
1738         .cleanup                = idedisk_cleanup,
1739         .do_request             = ide_do_rw_disk,
1740         .sense                  = idedisk_dump_status,
1741         .error                  = idedisk_error,
1742         .abort                  = idedisk_abort,
1743         .pre_reset              = idedisk_pre_reset,
1744         .capacity               = idedisk_capacity,
1745         .special                = idedisk_special,
1746         .proc                   = idedisk_proc,
1747         .attach                 = idedisk_attach,
1748         .drives                 = LIST_HEAD_INIT(idedisk_driver.drives),
1749         .start_power_step       = idedisk_start_power_step,
1750         .complete_power_step    = idedisk_complete_power_step,
1751 };
1752
1753 static int idedisk_open(struct inode *inode, struct file *filp)
1754 {
1755         ide_drive_t *drive = inode->i_bdev->bd_disk->private_data;
1756         drive->usage++;
1757         if (drive->removable && drive->usage == 1) {
1758                 ide_task_t args;
1759                 memset(&args, 0, sizeof(ide_task_t));
1760                 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_DOORLOCK;
1761                 args.command_type = IDE_DRIVE_TASK_NO_DATA;
1762                 args.handler      = &task_no_data_intr;
1763                 check_disk_change(inode->i_bdev);
1764                 /*
1765                  * Ignore the return code from door_lock,
1766                  * since the open() has already succeeded,
1767                  * and the door_lock is irrelevant at this point.
1768                  */
1769                 if (drive->doorlocking && ide_raw_taskfile(drive, &args, NULL))
1770                         drive->doorlocking = 0;
1771         }
1772         return 0;
1773 }
1774
1775 static int idedisk_release(struct inode *inode, struct file *filp)
1776 {
1777         ide_drive_t *drive = inode->i_bdev->bd_disk->private_data;
1778         if (drive->usage == 1)
1779                 ide_cacheflush_p(drive);
1780         if (drive->removable && drive->usage == 1) {
1781                 ide_task_t args;
1782                 memset(&args, 0, sizeof(ide_task_t));
1783                 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_DOORUNLOCK;
1784                 args.command_type = IDE_DRIVE_TASK_NO_DATA;
1785                 args.handler      = &task_no_data_intr;
1786                 if (drive->doorlocking && ide_raw_taskfile(drive, &args, NULL))
1787                         drive->doorlocking = 0;
1788         }
1789         drive->usage--;
1790         return 0;
1791 }
1792
1793 static int idedisk_ioctl(struct inode *inode, struct file *file,
1794                         unsigned int cmd, unsigned long arg)
1795 {
1796         struct block_device *bdev = inode->i_bdev;
1797         return generic_ide_ioctl(bdev, cmd, arg);
1798 }
1799
1800 static int idedisk_media_changed(struct gendisk *disk)
1801 {
1802         ide_drive_t *drive = disk->private_data;
1803
1804         /* do not scan partitions twice if this is a removable device */
1805         if (drive->attach) {
1806                 drive->attach = 0;
1807                 return 0;
1808         }
1809         /* if removable, always assume it was changed */
1810         return drive->removable;
1811 }
1812
1813 static int idedisk_revalidate_disk(struct gendisk *disk)
1814 {
1815         ide_drive_t *drive = disk->private_data;
1816         set_capacity(disk, current_capacity(drive));
1817         return 0;
1818 }
1819
1820 static struct block_device_operations idedisk_ops = {
1821         .owner          = THIS_MODULE,
1822         .open           = idedisk_open,
1823         .release        = idedisk_release,
1824         .ioctl          = idedisk_ioctl,
1825         .media_changed  = idedisk_media_changed,
1826         .revalidate_disk= idedisk_revalidate_disk
1827 };
1828
1829 MODULE_DESCRIPTION("ATA DISK Driver");
1830
1831 static int idedisk_attach(ide_drive_t *drive)
1832 {
1833         struct gendisk *g = drive->disk;
1834
1835         /* strstr("foo", "") is non-NULL */
1836         if (!strstr("ide-disk", drive->driver_req))
1837                 goto failed;
1838         if (!drive->present)
1839                 goto failed;
1840         if (drive->media != ide_disk)
1841                 goto failed;
1842
1843         if (ide_register_subdriver(drive, &idedisk_driver)) {
1844                 printk (KERN_ERR "ide-disk: %s: Failed to register the driver with ide.c\n", drive->name);
1845                 goto failed;
1846         }
1847         DRIVER(drive)->busy++;
1848         idedisk_setup(drive);
1849         if ((!drive->head || drive->head > 16) && !drive->select.b.lba) {
1850                 printk(KERN_ERR "%s: INVALID GEOMETRY: %d PHYSICAL HEADS?\n",
1851                         drive->name, drive->head);
1852                 ide_cacheflush_p(drive);
1853                 ide_unregister_subdriver(drive);
1854                 DRIVER(drive)->busy--;
1855                 goto failed;
1856         }
1857         DRIVER(drive)->busy--;
1858         g->minors = 1 << PARTN_BITS;
1859         strcpy(g->devfs_name, drive->devfs_name);
1860         g->driverfs_dev = &drive->gendev;
1861         g->flags = drive->removable ? GENHD_FL_REMOVABLE : 0;
1862         set_capacity(g, current_capacity(drive));
1863         g->fops = &idedisk_ops;
1864         drive->attach = 1;
1865         add_disk(g);
1866         return 0;
1867 failed:
1868         return 1;
1869 }
1870
1871 static void __exit idedisk_exit (void)
1872 {
1873         ide_unregister_driver(&idedisk_driver);
1874 }
1875
1876 static int idedisk_init (void)
1877 {
1878         return ide_register_driver(&idedisk_driver);
1879 }
1880
1881 module_init(idedisk_init);
1882 module_exit(idedisk_exit);
1883 MODULE_LICENSE("GPL");