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