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