Merge
[opensuse:kernel.git] / drivers / ide / ide.c
1 /*
2  *  Copyright (C) 1994-1998  Linus Torvalds & authors (see below)
3  *
4  *  Mostly written by Mark Lord  <mlord@pobox.com>
5  *                and Gadi Oxman <gadio@netvision.net.il>
6  *                and Andre Hedrick <andre@linux-ide.org>
7  *
8  *  See linux/MAINTAINERS for address of current maintainer.
9  *
10  * This is the multiple IDE interface driver, as evolved from hd.c.
11  * It supports up to MAX_HWIFS IDE interfaces, on one or more IRQs (usually 14 & 15).
12  * There can be up to two drives per interface, as per the ATA-2 spec.
13  *
14  * Primary:    ide0, port 0x1f0; major=3;  hda is minor=0; hdb is minor=64
15  * Secondary:  ide1, port 0x170; major=22; hdc is minor=0; hdd is minor=64
16  * Tertiary:   ide2, port 0x???; major=33; hde is minor=0; hdf is minor=64
17  * Quaternary: ide3, port 0x???; major=34; hdg is minor=0; hdh is minor=64
18  * ...
19  *
20  *  From hd.c:
21  *  |
22  *  | It traverses the request-list, using interrupts to jump between functions.
23  *  | As nearly all functions can be called within interrupts, we may not sleep.
24  *  | Special care is recommended.  Have Fun!
25  *  |
26  *  | modified by Drew Eckhardt to check nr of hd's from the CMOS.
27  *  |
28  *  | Thanks to Branko Lankester, lankeste@fwi.uva.nl, who found a bug
29  *  | in the early extended-partition checks and added DM partitions.
30  *  |
31  *  | Early work on error handling by Mika Liljeberg (liljeber@cs.Helsinki.FI).
32  *  |
33  *  | IRQ-unmask, drive-id, multiple-mode, support for ">16 heads",
34  *  | and general streamlining by Mark Lord (mlord@pobox.com).
35  *
36  *  October, 1994 -- Complete line-by-line overhaul for linux 1.1.x, by:
37  *
38  *      Mark Lord       (mlord@pobox.com)               (IDE Perf.Pkg)
39  *      Delman Lee      (delman@ieee.org)               ("Mr. atdisk2")
40  *      Scott Snyder    (snyder@fnald0.fnal.gov)        (ATAPI IDE cd-rom)
41  *
42  *  This was a rewrite of just about everything from hd.c, though some original
43  *  code is still sprinkled about.  Think of it as a major evolution, with
44  *  inspiration from lots of linux users, esp.  hamish@zot.apana.org.au
45  *
46  *  Version 1.0 ALPHA   initial code, primary i/f working okay
47  *  Version 1.3 BETA    dual i/f on shared irq tested & working!
48  *  Version 1.4 BETA    added auto probing for irq(s)
49  *  Version 1.5 BETA    added ALPHA (untested) support for IDE cd-roms,
50  *  ...
51  * Version 5.50         allow values as small as 20 for idebus=
52  * Version 5.51         force non io_32bit in drive_cmd_intr()
53  *                      change delay_10ms() to delay_50ms() to fix problems
54  * Version 5.52         fix incorrect invalidation of removable devices
55  *                      add "hdx=slow" command line option
56  * Version 5.60         start to modularize the driver; the disk and ATAPI
57  *                       drivers can be compiled as loadable modules.
58  *                      move IDE probe code to ide-probe.c
59  *                      move IDE disk code to ide-disk.c
60  *                      add support for generic IDE device subdrivers
61  *                      add m68k code from Geert Uytterhoeven
62  *                      probe all interfaces by default
63  *                      add ioctl to (re)probe an interface
64  * Version 6.00         use per device request queues
65  *                      attempt to optimize shared hwgroup performance
66  *                      add ioctl to manually adjust bandwidth algorithms
67  *                      add kerneld support for the probe module
68  *                      fix bug in ide_error()
69  *                      fix bug in the first ide_get_lock() call for Atari
70  *                      don't flush leftover data for ATAPI devices
71  * Version 6.01         clear hwgroup->active while the hwgroup sleeps
72  *                      support HDIO_GETGEO for floppies
73  * Version 6.02         fix ide_ack_intr() call
74  *                      check partition table on floppies
75  * Version 6.03         handle bad status bit sequencing in ide_wait_stat()
76  * Version 6.10         deleted old entries from this list of updates
77  *                      replaced triton.c with ide-dma.c generic PCI DMA
78  *                      added support for BIOS-enabled UltraDMA
79  *                      rename all "promise" things to "pdc4030"
80  *                      fix EZ-DRIVE handling on small disks
81  * Version 6.11         fix probe error in ide_scan_devices()
82  *                      fix ancient "jiffies" polling bugs
83  *                      mask all hwgroup interrupts on each irq entry
84  * Version 6.12         integrate ioctl and proc interfaces
85  *                      fix parsing of "idex=" command line parameter
86  * Version 6.13         add support for ide4/ide5 courtesy rjones@orchestream.com
87  * Version 6.14         fixed IRQ sharing among PCI devices
88  * Version 6.15         added SMP awareness to IDE drivers
89  * Version 6.16         fixed various bugs; even more SMP friendly
90  * Version 6.17         fix for newest EZ-Drive problem
91  * Version 6.18         default unpartitioned-disk translation now "BIOS LBA"
92  * Version 6.19         Re-design for a UNIFORM driver for all platforms,
93  *                        model based on suggestions from Russell King and
94  *                        Geert Uytterhoeven
95  *                      Promise DC4030VL now supported.
96  *                      add support for ide6/ide7
97  *                      delay_50ms() changed to ide_delay_50ms() and exported.
98  * Version 6.20         Added/Fixed Generic ATA-66 support and hwif detection.
99  *                      Added hdx=flash to allow for second flash disk
100  *                        detection w/o the hang loop.
101  *                      Added support for ide8/ide9
102  *                      Added idex=ata66 for the quirky chipsets that are
103  *                        ATA-66 compliant, but have yet to determine a method
104  *                        of verification of the 80c cable presence.
105  *                        Specifically Promise's PDC20262 chipset.
106  * Version 6.21         Fixing/Fixed SMP spinlock issue with insight from an old
107  *                        hat that clarified original low level driver design.
108  * Version 6.30         Added SMP support; fixed multmode issues.  -ml
109  * Version 6.31         Debug Share INTR's and request queue streaming
110  *                      Native ATA-100 support
111  *                      Prep for Cascades Project
112  * Version 6.32         4GB highmem support for DMA, and mapping of those for
113  *                      PIO transfer (Jens Axboe)
114  *
115  *  Some additional driver compile-time options are in ./include/linux/ide.h
116  */
117
118 #define VERSION "7.0.0"
119
120 #include <linux/config.h>
121 #include <linux/module.h>
122 #include <linux/types.h>
123 #include <linux/string.h>
124 #include <linux/kernel.h>
125 #include <linux/timer.h>
126 #include <linux/mm.h>
127 #include <linux/interrupt.h>
128 #include <linux/major.h>
129 #include <linux/errno.h>
130 #include <linux/genhd.h>
131 #include <linux/blkpg.h>
132 #include <linux/slab.h>
133 #ifndef MODULE
134 # include <linux/init.h>
135 #endif
136 #include <linux/pci.h>
137 #include <linux/delay.h>
138 #include <linux/ide.h>
139 #include <linux/devfs_fs_kernel.h>
140 #include <linux/completion.h>
141 #include <linux/reboot.h>
142 #include <linux/cdrom.h>
143 #include <linux/device.h>
144 #include <linux/kmod.h>
145
146 #include <asm/byteorder.h>
147 #include <asm/irq.h>
148 #include <asm/uaccess.h>
149 #include <asm/io.h>
150 #include <asm/bitops.h>
151
152 #include "ata-timing.h"
153
154 /*
155  * Those will be moved into separate header files eventually.
156  */
157 #ifdef CONFIG_BLK_DEV_RZ1000
158 extern void ide_probe_for_rz100x(void);
159 #endif
160 #ifdef CONFIG_ETRAX_IDE
161 extern void init_e100_ide(void);
162 #endif
163 #ifdef CONFIG_BLK_DEV_CMD640
164 extern void ide_probe_for_cmd640x(void);
165 #endif
166 #ifdef CONFIG_BLK_DEV_PDC4030
167 extern int ide_probe_for_pdc4030(void);
168 #endif
169 #ifdef CONFIG_BLK_DEV_IDE_PMAC
170 extern void pmac_ide_probe(void);
171 #endif
172 #ifdef CONFIG_BLK_DEV_IDE_ICSIDE
173 extern void icside_init(void);
174 #endif
175 #ifdef CONFIG_BLK_DEV_IDE_RAPIDE
176 extern void rapide_init(void);
177 #endif
178 #ifdef CONFIG_BLK_DEV_GAYLE
179 extern void gayle_init(void);
180 #endif
181 #ifdef CONFIG_BLK_DEV_FALCON_IDE
182 extern void falconide_init(void);
183 #endif
184 #ifdef CONFIG_BLK_DEV_MAC_IDE
185 extern void macide_init(void);
186 #endif
187 #ifdef CONFIG_BLK_DEV_Q40IDE
188 extern void q40ide_init(void);
189 #endif
190 #ifdef CONFIG_BLK_DEV_BUDDHA
191 extern void buddha_init(void);
192 #endif
193 #if defined(CONFIG_BLK_DEV_ISAPNP) && defined(CONFIG_ISAPNP)
194 extern void pnpide_init(int);
195 #endif
196
197 /* default maximum number of failures */
198 #define IDE_DEFAULT_MAX_FAILURES        1
199
200 static int idebus_parameter;    /* holds the "idebus=" parameter */
201 int system_bus_speed;           /* holds what we think is VESA/PCI bus speed */
202 static int initializing;        /* set while initializing built-in drivers */
203
204 /*
205  * Protects access to global structures etc.
206  */
207 spinlock_t ide_lock __cacheline_aligned = SPIN_LOCK_UNLOCKED;
208
209 #ifdef CONFIG_BLK_DEV_IDEPCI
210 static int ide_scan_direction;  /* THIS was formerly 2.2.x pci=reverse */
211 #endif
212
213 #if defined(__mc68000__) || defined(CONFIG_APUS)
214 /*
215  * This is used by the Atari code to obtain access to the IDE interrupt,
216  * which is shared between several drivers.
217  */
218 static int irq_lock;
219 #endif
220
221 int noautodma = 0;
222
223 /*
224  * This is declared extern in ide.h, for access by other IDE modules:
225  */
226 struct ata_channel ide_hwifs[MAX_HWIFS];        /* master data repository */
227
228 #if (DISK_RECOVERY_TIME > 0)
229 /*
230  * For really screwed hardware (hey, at least it *can* be used with Linux)
231  * we can enforce a minimum delay time between successive operations.
232  */
233 static unsigned long read_timer (void)
234 {
235         unsigned long t, flags;
236         int i;
237
238         __save_flags(flags);    /* local CPU only */
239         __cli();                /* local CPU only */
240         t = jiffies * 11932;
241         outb_p(0, 0x43);
242         i = inb_p(0x40);
243         i |= inb(0x40) << 8;
244         __restore_flags(flags); /* local CPU only */
245         return (t - i);
246 }
247 #endif
248
249 static inline void set_recovery_timer(struct ata_channel *channel)
250 {
251 #if (DISK_RECOVERY_TIME > 0)
252         channel->last_time = read_timer();
253 #endif
254 }
255
256 /*
257  * Do not even *think* about calling this!
258  */
259 static void init_hwif_data(struct ata_channel *hwif, unsigned int index)
260 {
261         static const byte ide_major[] = {
262                 IDE0_MAJOR, IDE1_MAJOR, IDE2_MAJOR, IDE3_MAJOR, IDE4_MAJOR,
263                 IDE5_MAJOR, IDE6_MAJOR, IDE7_MAJOR, IDE8_MAJOR, IDE9_MAJOR
264         };
265
266         unsigned int unit;
267         hw_regs_t hw;
268
269         /* bulk initialize hwif & drive info with zeros */
270         memset(hwif, 0, sizeof(struct ata_channel));
271         memset(&hw, 0, sizeof(hw_regs_t));
272
273         /* fill in any non-zero initial values */
274         hwif->index     = index;
275         ide_init_hwif_ports(&hw, ide_default_io_base(index), 0, &hwif->irq);
276         memcpy(&hwif->hw, &hw, sizeof(hw));
277         memcpy(hwif->io_ports, hw.io_ports, sizeof(hw.io_ports));
278         hwif->noprobe   = !hwif->io_ports[IDE_DATA_OFFSET];
279 #ifdef CONFIG_BLK_DEV_HD
280         if (hwif->io_ports[IDE_DATA_OFFSET] == HD_DATA)
281                 hwif->noprobe = 1; /* may be overridden by ide_setup() */
282 #endif /* CONFIG_BLK_DEV_HD */
283         hwif->major = ide_major[index];
284         sprintf(hwif->name, "ide%d", index);
285         hwif->bus_state = BUSSTATE_ON;
286
287         for (unit = 0; unit < MAX_DRIVES; ++unit) {
288                 struct ata_device *drive = &hwif->drives[unit];
289
290                 drive->type                     = ATA_DISK;
291                 drive->select.all               = (unit<<4)|0xa0;
292                 drive->channel                  = hwif;
293                 drive->ctl                      = 0x08;
294                 drive->ready_stat               = READY_STAT;
295                 drive->bad_wstat                = BAD_W_STAT;
296                 drive->special_cmd = (ATA_SPECIAL_RECALIBRATE | ATA_SPECIAL_GEOMETRY);
297                 sprintf(drive->name, "hd%c", 'a' + (index * MAX_DRIVES) + unit);
298                 drive->max_failures             = IDE_DEFAULT_MAX_FAILURES;
299
300                 init_waitqueue_head(&drive->wqueue);
301         }
302 }
303
304 /*
305  * init_ide_data() sets reasonable default values into all fields
306  * of all instances of the hwifs and drives, but only on the first call.
307  * Subsequent calls have no effect (they don't wipe out anything).
308  *
309  * This routine is normally called at driver initialization time,
310  * but may also be called MUCH earlier during kernel "command-line"
311  * parameter processing.  As such, we cannot depend on any other parts
312  * of the kernel (such as memory allocation) to be functioning yet.
313  *
314  * This is too bad, as otherwise we could dynamically allocate the
315  * ide_drive_t structs as needed, rather than always consuming memory
316  * for the max possible number (MAX_HWIFS * MAX_DRIVES) of them.
317  */
318 #define MAGIC_COOKIE 0x12345678
319 static void __init init_ide_data (void)
320 {
321         unsigned int h;
322         static unsigned long magic_cookie = MAGIC_COOKIE;
323
324         if (magic_cookie != MAGIC_COOKIE)
325                 return;         /* already initialized */
326         magic_cookie = 0;
327
328         /* Initialize all interface structures */
329         for (h = 0; h < MAX_HWIFS; ++h)
330                 init_hwif_data(&ide_hwifs[h], h);
331
332         /* Add default hw interfaces */
333         ide_init_default_hwifs();
334
335         idebus_parameter = 0;
336 }
337
338 /*
339  * CompactFlash cards and their relatives pretend to be removable hard disks, except:
340  *      (1) they never have a slave unit, and
341  *      (2) they don't have a door lock mechanisms.
342  * This test catches them, and is invoked elsewhere when setting appropriate config bits.
343  *
344  * FIXME FIXME: Yes this is for certain applicable for all of them as time has shown.
345  *
346  * FIXME: This treatment is probably applicable for *all* PCMCIA (PC CARD) devices,
347  * so in linux 2.3.x we should change this to just treat all PCMCIA drives this way,
348  * and get rid of the model-name tests below (too big of an interface change for 2.2.x).
349  * At that time, we might also consider parameterizing the timeouts and retries,
350  * since these are MUCH faster than mechanical drives.  -M.Lord
351  */
352 int drive_is_flashcard (ide_drive_t *drive)
353 {
354         struct hd_driveid *id = drive->id;
355
356         if (drive->removable && id != NULL) {
357                 if (id->config == 0x848a)
358                         return 1;       /* CompactFlash */
359                 if (!strncmp(id->model, "KODAK ATA_FLASH", 15)  /* Kodak */
360                  || !strncmp(id->model, "Hitachi CV", 10)       /* Hitachi */
361                  || !strncmp(id->model, "SunDisk SDCFB", 13)    /* SunDisk */
362                  || !strncmp(id->model, "HAGIWARA HPC", 12)     /* Hagiwara */
363                  || !strncmp(id->model, "LEXAR ATA_FLASH", 15)  /* Lexar */
364                  || !strncmp(id->model, "ATA_FLASH", 9))        /* Simple Tech */
365                 {
366                         return 1;       /* yes, it is a flash memory card */
367                 }
368         }
369         return 0;       /* no, it is not a flash memory card */
370 }
371
372 int __ide_end_request(struct ata_device *drive, struct request *rq, int uptodate, int nr_secs)
373 {
374         unsigned long flags;
375         int ret = 1;
376
377         spin_lock_irqsave(&ide_lock, flags);
378
379         BUG_ON(!(rq->flags & REQ_STARTED));
380
381         /*
382          * small hack to eliminate locking from ide_end_request to grab
383          * the first segment number of sectors
384          */
385         if (!nr_secs)
386                 nr_secs = rq->hard_cur_sectors;
387
388         /*
389          * Decide whether to reenable DMA -- 3 is a random magic for now,
390          * if we DMA timeout more than 3 times, just stay in PIO.
391          */
392
393         if (drive->state == DMA_PIO_RETRY && drive->retry_pio <= 3) {
394                 drive->state = 0;
395                 drive->channel->udma(ide_dma_on, drive, rq);
396         }
397
398         if (!end_that_request_first(rq, uptodate, nr_secs)) {
399                 add_blkdev_randomness(major(rq->rq_dev));
400                 blkdev_dequeue_request(rq);
401                 HWGROUP(drive)->rq = NULL;
402                 end_that_request_last(rq);
403                 ret = 0;
404         }
405
406         spin_unlock_irqrestore(&ide_lock, flags);
407         return ret;
408 }
409
410 /*
411  * This should get invoked any time we exit the driver to
412  * wait for an interrupt response from a drive.  handler() points
413  * at the appropriate code to handle the next interrupt, and a
414  * timer is started to prevent us from waiting forever in case
415  * something goes wrong (see the ide_timer_expiry() handler later on).
416  */
417 void ide_set_handler(struct ata_device *drive, ata_handler_t handler,
418                       unsigned long timeout, ata_expiry_t expiry)
419 {
420         unsigned long flags;
421         ide_hwgroup_t *hwgroup = HWGROUP(drive);
422
423         spin_lock_irqsave(&ide_lock, flags);
424         if (hwgroup->handler != NULL) {
425                 printk("%s: ide_set_handler: handler not null; old=%p, new=%p, from %p\n",
426                         drive->name, hwgroup->handler, handler, __builtin_return_address(0));
427         }
428         hwgroup->handler        = handler;
429         hwgroup->expiry         = expiry;
430         hwgroup->timer.expires  = jiffies + timeout;
431         add_timer(&hwgroup->timer);
432         spin_unlock_irqrestore(&ide_lock, flags);
433 }
434
435 static void ata_pre_reset(struct ata_device *drive)
436 {
437         if (ata_ops(drive) && ata_ops(drive)->pre_reset)
438                 ata_ops(drive)->pre_reset(drive);
439
440         if (!drive->using_dma)
441             return;
442
443         /* check the DMA crc count */
444         if (drive->crc_count) {
445                 drive->channel->udma(ide_dma_off_quietly, drive, NULL);
446                 if ((drive->channel->speedproc) != NULL)
447                         drive->channel->speedproc(drive, ide_auto_reduce_xfer(drive));
448                 if (drive->current_speed >= XFER_SW_DMA_0)
449                         drive->channel->udma(ide_dma_on, drive, NULL);
450         } else
451                 drive->channel->udma(ide_dma_off, drive, NULL);
452 }
453
454 /*
455  * The capacity of a drive according to its current geometry/LBA settings in
456  * sectors.
457  */
458 sector_t ata_capacity(struct ata_device *drive)
459 {
460         if (!drive->present || !drive->driver)
461                 return 0;
462
463         if (ata_ops(drive) && ata_ops(drive)->capacity)
464                 return ata_ops(drive)->capacity(drive);
465
466         /* This used to be 0x7fffffff, but since now we use the maximal drive
467          * capacity value used by other kernel subsystems as well.
468          */
469
470         return ~0UL;
471 }
472
473 /*
474  * This is used to issue WIN_SPECIFY, WIN_RESTORE, and WIN_SETMULT commands to
475  * a drive.
476  */
477 static ide_startstop_t ata_special(struct ata_device *drive)
478 {
479         unsigned char special_cmd = drive->special_cmd;
480
481 #ifdef DEBUG
482         printk("%s: ata_special: 0x%02x\n", drive->name, special_cmd);
483 #endif
484         if (special_cmd & ATA_SPECIAL_TUNE) {
485                 drive->special_cmd &= ~ATA_SPECIAL_TUNE;
486                 if (drive->channel->tuneproc != NULL)
487                         drive->channel->tuneproc(drive, drive->tune_req);
488         } else if (drive->driver != NULL) {
489                 if (ata_ops(drive)->special)
490                         return ata_ops(drive)->special(drive);
491                 else {
492                         drive->special_cmd = 0;
493                         drive->mult_req = 0;
494
495                         return ide_stopped;
496                 }
497         } else if (special_cmd) {
498                 printk("%s: bad special flag: 0x%02x\n", drive->name, special_cmd);
499                 drive->special_cmd = 0;
500         }
501
502         return ide_stopped;
503 }
504
505 extern struct block_device_operations ide_fops[];
506
507 /*
508  * This is called exactly *once* for each channel.
509  */
510 void ide_geninit(struct ata_channel *ch)
511 {
512         unsigned int unit;
513         struct gendisk *gd = ch->gd;
514
515         for (unit = 0; unit < MAX_DRIVES; ++unit) {
516                 struct ata_device *drive = &ch->drives[unit];
517
518                 if (!drive->present)
519                         continue;
520
521                 if (drive->type != ATA_DISK && drive->type != ATA_FLOPPY)
522                         continue;
523
524                 register_disk(gd,mk_kdev(ch->major,unit<<PARTN_BITS),
525 #ifdef CONFIG_BLK_DEV_ISAPNP
526                         (drive->forced_geom && drive->noprobe) ? 1 :
527 #endif
528                         1 << PARTN_BITS, ide_fops, ata_capacity(drive));
529         }
530 }
531
532 static ide_startstop_t do_reset1(ide_drive_t *, int);           /* needed below */
533
534 /*
535  * Poll the interface for completion every 50ms during an ATAPI drive reset
536  * operation. If the drive has not yet responded, and we have not yet hit our
537  * maximum waiting time, then the timer is restarted for another 50ms.
538  */
539 static ide_startstop_t atapi_reset_pollfunc(struct ata_device *drive, struct request *__rq)
540 {
541         ide_hwgroup_t *hwgroup = HWGROUP(drive);
542         byte stat;
543
544         SELECT_DRIVE(drive->channel,drive);
545         udelay (10);
546
547         if (OK_STAT(stat=GET_STAT(), 0, BUSY_STAT)) {
548                 printk("%s: ATAPI reset complete\n", drive->name);
549         } else {
550                 if (time_before(jiffies, hwgroup->poll_timeout)) {
551                         ide_set_handler (drive, atapi_reset_pollfunc, HZ/20, NULL);
552                         return ide_started;     /* continue polling */
553                 }
554                 hwgroup->poll_timeout = 0;      /* end of polling */
555                 printk("%s: ATAPI reset timed-out, status=0x%02x\n", drive->name, stat);
556                 return do_reset1 (drive, 1);    /* do it the old fashioned way */
557         }
558         hwgroup->poll_timeout = 0;      /* done polling */
559
560         return ide_stopped;
561 }
562
563 /*
564  * Poll the interface for completion every 50ms during an ata reset operation.
565  * If the drives have not yet responded, and we have not yet hit our maximum
566  * waiting time, then the timer is restarted for another 50ms.
567  */
568 static ide_startstop_t reset_pollfunc(struct ata_device *drive, struct request *__rq)
569 {
570         ide_hwgroup_t *hwgroup = HWGROUP(drive);
571         struct ata_channel *hwif = drive->channel;
572         u8 stat;
573
574         if (!OK_STAT(stat=GET_STAT(), 0, BUSY_STAT)) {
575                 if (time_before(jiffies, hwgroup->poll_timeout)) {
576                         ide_set_handler(drive, reset_pollfunc, HZ/20, NULL);
577                         return ide_started;     /* continue polling */
578                 }
579                 printk("%s: reset timed-out, status=0x%02x\n", hwif->name, stat);
580                 drive->failures++;
581         } else  {
582                 printk("%s: reset: ", hwif->name);
583                 if ((stat = GET_ERR()) == 1) {
584                         printk("success\n");
585                         drive->failures = 0;
586                 } else {
587                         char *msg;
588
589 #if FANCY_STATUS_DUMPS
590                         u8 val;
591                         static char *messages[5] = {
592                                 " passed",
593                                 " formatter device",
594                                 " sector buffer",
595                                 " ECC circuitry",
596                                 " controlling MPU error"
597                         };
598
599                         printk("master:");
600                         val = stat & 0x7f;
601                         if (val >= 1 && val <= 5)
602                                 msg = messages[val -1];
603                         else
604                                 msg = "";
605                         if (stat & 0x80)
606                                 printk("; slave:");
607 #endif
608                         printk("%s error [%02x]\n", msg, stat);
609                         drive->failures++;
610                 }
611         }
612         hwgroup->poll_timeout = 0;      /* done polling */
613         return ide_stopped;
614 }
615
616 /*
617  * Attempt to recover a confused drive by resetting it.  Unfortunately,
618  * resetting a disk drive actually resets all devices on the same interface, so
619  * it can really be thought of as resetting the interface rather than resetting
620  * the drive.
621  *
622  * ATAPI devices have their own reset mechanism which allows them to be
623  * individually reset without clobbering other devices on the same interface.
624  *
625  * Unfortunately, the IDE interface does not generate an interrupt to let us
626  * know when the reset operation has finished, so we must poll for this.
627  * Equally poor, though, is the fact that this may a very long time to
628  * complete, (up to 30 seconds worst case).  So, instead of busy-waiting here
629  * for it, we set a timer to poll at 50ms intervals.
630  */
631
632 static ide_startstop_t do_reset1 (ide_drive_t *drive, int do_not_try_atapi)
633 {
634         unsigned int unit;
635         unsigned long flags;
636         struct ata_channel *hwif = drive->channel;
637         ide_hwgroup_t *hwgroup = HWGROUP(drive);
638
639         __save_flags(flags);    /* local CPU only */
640         __cli();                /* local CPU only */
641
642         /* For an ATAPI device, first try an ATAPI SRST. */
643         if (drive->type != ATA_DISK && !do_not_try_atapi) {
644                 ata_pre_reset(drive);
645                 SELECT_DRIVE(hwif,drive);
646                 udelay (20);
647                 OUT_BYTE(WIN_SRST, IDE_COMMAND_REG);
648                 hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
649                 ide_set_handler(drive, atapi_reset_pollfunc, HZ/20, NULL);
650                 __restore_flags(flags); /* local CPU only */
651                 return ide_started;
652         }
653
654         /*
655          * First, reset any device state data we were maintaining
656          * for any of the drives on this interface.
657          */
658         for (unit = 0; unit < MAX_DRIVES; ++unit)
659                 ata_pre_reset(&hwif->drives[unit]);
660
661 #if OK_TO_RESET_CONTROLLER
662         if (!IDE_CONTROL_REG) {
663                 __restore_flags(flags);
664                 return ide_stopped;
665         }
666         /*
667          * Note that we also set nIEN while resetting the device,
668          * to mask unwanted interrupts from the interface during the reset.
669          * However, due to the design of PC hardware, this will cause an
670          * immediate interrupt due to the edge transition it produces.
671          * This single interrupt gives us a "fast poll" for drives that
672          * recover from reset very quickly, saving us the first 50ms wait time.
673          */
674         OUT_BYTE(drive->ctl|6,IDE_CONTROL_REG); /* set SRST and nIEN */
675         udelay(10);                     /* more than enough time */
676         if (drive->quirk_list == 2) {
677                 OUT_BYTE(drive->ctl,IDE_CONTROL_REG);   /* clear SRST and nIEN */
678         } else {
679                 OUT_BYTE(drive->ctl|2,IDE_CONTROL_REG); /* clear SRST, leave nIEN */
680         }
681         udelay(10);                     /* more than enough time */
682         hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
683         ide_set_handler(drive, reset_pollfunc, HZ/20, NULL);
684
685         /*
686          * Some weird controller like resetting themselves to a strange
687          * state when the disks are reset this way. At least, the Winbond
688          * 553 documentation says that
689          */
690         if (hwif->resetproc != NULL)
691                 hwif->resetproc(drive);
692
693 #endif
694
695         __restore_flags (flags);        /* local CPU only */
696
697         return ide_started;
698 }
699
700 static inline u32 read_24 (ide_drive_t *drive)
701 {
702         return  (IN_BYTE(IDE_HCYL_REG)<<16) |
703                 (IN_BYTE(IDE_LCYL_REG)<<8) |
704                  IN_BYTE(IDE_SECTOR_REG);
705 }
706
707 /*
708  * Clean up after success/failure of an explicit drive cmd
709  */
710 void ide_end_drive_cmd(struct ata_device *drive, byte stat, byte err)
711 {
712         unsigned long flags;
713         struct request *rq;
714
715         spin_lock_irqsave(&ide_lock, flags);
716         rq = HWGROUP(drive)->rq;
717
718         if (rq->flags & REQ_DRIVE_CMD) {
719                 u8 *args = rq->buffer;
720                 rq->errors = !OK_STAT(stat, READY_STAT, BAD_STAT);
721                 if (args) {
722                         args[0] = stat;
723                         args[1] = err;
724                         args[2] = IN_BYTE(IDE_NSECTOR_REG);
725                 }
726         } else if (rq->flags & REQ_DRIVE_ACB) {
727                 struct ata_taskfile *args = rq->special;
728
729                 rq->errors = !OK_STAT(stat, READY_STAT, BAD_STAT);
730                 if (args) {
731                         args->taskfile.feature = err;
732                         args->taskfile.sector_count = IN_BYTE(IDE_NSECTOR_REG);
733                         args->taskfile.sector_number = IN_BYTE(IDE_SECTOR_REG);
734                         args->taskfile.low_cylinder = IN_BYTE(IDE_LCYL_REG);
735                         args->taskfile.high_cylinder = IN_BYTE(IDE_HCYL_REG);
736                         args->taskfile.device_head = IN_BYTE(IDE_SELECT_REG);
737                         args->taskfile.command = stat;
738                         if ((drive->id->command_set_2 & 0x0400) &&
739                             (drive->id->cfs_enable_2 & 0x0400) &&
740                             (drive->addressing == 1)) {
741                                 /* The following command goes to the hob file! */
742
743                                 OUT_BYTE(drive->ctl|0x80, IDE_CONTROL_REG);
744                                 args->hobfile.feature = IN_BYTE(IDE_FEATURE_REG);
745                                 args->hobfile.sector_count = IN_BYTE(IDE_NSECTOR_REG);
746
747                                 args->hobfile.sector_number = IN_BYTE(IDE_SECTOR_REG);
748                                 args->hobfile.low_cylinder = IN_BYTE(IDE_LCYL_REG);
749                                 args->hobfile.high_cylinder = IN_BYTE(IDE_HCYL_REG);
750                         }
751                 }
752         }
753
754         blkdev_dequeue_request(rq);
755         HWGROUP(drive)->rq = NULL;
756         end_that_request_last(rq);
757
758         spin_unlock_irqrestore(&ide_lock, flags);
759 }
760
761 /*
762  * Error reporting, in human readable form (luxurious, but a memory hog).
763  */
764 byte ide_dump_status (ide_drive_t *drive, const char *msg, byte stat)
765 {
766         unsigned long flags;
767         byte err = 0;
768
769         __save_flags (flags);   /* local CPU only */
770         ide__sti();             /* local CPU only */
771 #if !(FANCY_STATUS_DUMPS)
772         printk("%s: %s: status=0x%02x\n", drive->name, msg, stat);
773 #else
774         printk(" { ");
775         {
776                 char *msg = "";
777                 if (stat & BUSY_STAT)
778                         msg = "Busy";
779                 else {
780                         if (stat & READY_STAT)
781                                 msg = "DriveReady";
782                         if (stat & WRERR_STAT)
783                                 msg = "DeviceFault";
784                         if (stat & SEEK_STAT)
785                                 msg = "SeekComplete";
786                         if (stat & DRQ_STAT)
787                                 msg = "DataRequest";
788                         if (stat & ECC_STAT)
789                                 msg = "CorrectedError";
790                         if (stat & INDEX_STAT)
791                                 msg = "Index";
792                         if (stat & ERR_STAT)
793                                 msg = "Error";
794                 }
795         }
796         printk("%s }\n", msg);
797 #endif
798         if ((stat & (BUSY_STAT|ERR_STAT)) == ERR_STAT) {
799                 err = GET_ERR();
800                 printk("%s: %s: error=0x%02x", drive->name, msg, err);
801 #if FANCY_STATUS_DUMPS
802                 if (drive->type == ATA_DISK) {
803                         printk(" { ");
804                         if (err & ABRT_ERR)     printk("DriveStatusError ");
805                         if (err & ICRC_ERR)     printk("%s", (err & ABRT_ERR) ? "BadCRC " : "BadSector ");
806                         if (err & ECC_ERR)      printk("UncorrectableError ");
807                         if (err & ID_ERR)       printk("SectorIdNotFound ");
808                         if (err & TRK0_ERR)     printk("TrackZeroNotFound ");
809                         if (err & MARK_ERR)     printk("AddrMarkNotFound ");
810                         printk("}");
811                         if ((err & (BBD_ERR | ABRT_ERR)) == BBD_ERR || (err & (ECC_ERR|ID_ERR|MARK_ERR))) {
812                                 if ((drive->id->command_set_2 & 0x0400) &&
813                                     (drive->id->cfs_enable_2 & 0x0400) &&
814                                     (drive->addressing == 1)) {
815                                         __u64 sectors = 0;
816                                         u32 low = 0, high = 0;
817                                         low = read_24(drive);
818                                         OUT_BYTE(drive->ctl|0x80, IDE_CONTROL_REG);
819                                         high = read_24(drive);
820
821                                         sectors = ((__u64)high << 24) | low;
822                                         printk(", LBAsect=%lld, high=%d, low=%d", (long long) sectors, high, low);
823                                 } else {
824                                         byte cur = IN_BYTE(IDE_SELECT_REG);
825                                         if (cur & 0x40) {       /* using LBA? */
826                                                 printk(", LBAsect=%ld", (unsigned long)
827                                                  ((cur&0xf)<<24)
828                                                  |(IN_BYTE(IDE_HCYL_REG)<<16)
829                                                  |(IN_BYTE(IDE_LCYL_REG)<<8)
830                                                  | IN_BYTE(IDE_SECTOR_REG));
831                                         } else {
832                                                 printk(", CHS=%d/%d/%d",
833                                                  (IN_BYTE(IDE_HCYL_REG)<<8) +
834                                                   IN_BYTE(IDE_LCYL_REG),
835                                                   cur & 0xf,
836                                                   IN_BYTE(IDE_SECTOR_REG));
837                                         }
838                                 }
839                                 if (HWGROUP(drive) && HWGROUP(drive)->rq)
840                                         printk(", sector=%ld", HWGROUP(drive)->rq->sector);
841                         }
842                 }
843 #endif
844                 printk("\n");
845         }
846         __restore_flags (flags);        /* local CPU only */
847         return err;
848 }
849
850 /*
851  * This gets invoked in response to a drive unexpectedly having its DRQ_STAT
852  * bit set.  As an alternative to resetting the drive, it tries to clear the
853  * condition by reading a sector's worth of data from the drive.  Of course,
854  * this may not help if the drive is *waiting* for data from *us*.
855  */
856 static void try_to_flush_leftover_data (ide_drive_t *drive)
857 {
858         int i;
859
860         if (drive->type != ATA_DISK)
861                 return;
862
863         for (i = (drive->mult_count ? drive->mult_count : 1); i > 0; --i) {
864                 u32 buffer[SECTOR_WORDS];
865
866                 ata_read(drive, buffer, SECTOR_WORDS);
867         }
868 }
869
870 /*
871  * Take action based on the error returned by the drive.
872  */
873 ide_startstop_t ide_error(ide_drive_t *drive, const char *msg, byte stat)
874 {
875         struct request *rq;
876         byte err;
877
878         err = ide_dump_status(drive, msg, stat);
879         if (drive == NULL || (rq = HWGROUP(drive)->rq) == NULL)
880                 return ide_stopped;
881         /* retry only "normal" I/O: */
882         if (!(rq->flags & REQ_CMD)) {
883                 rq->errors = 1;
884                 ide_end_drive_cmd(drive, stat, err);
885                 return ide_stopped;
886         }
887
888         if (stat & BUSY_STAT || ((stat & WRERR_STAT) && !drive->nowerr)) { /* other bits are useless when BUSY */
889                 rq->errors |= ERROR_RESET;
890         } else {
891                 if (drive->type == ATA_DISK && (stat & ERR_STAT)) {
892                         /* err has different meaning on cdrom and tape */
893                         if (err == ABRT_ERR) {
894                                 if (drive->select.b.lba && IN_BYTE(IDE_COMMAND_REG) == WIN_SPECIFY)
895                                         return ide_stopped; /* some newer drives don't support WIN_SPECIFY */
896                         } else if ((err & (ABRT_ERR | ICRC_ERR)) == (ABRT_ERR | ICRC_ERR)) {
897                                 drive->crc_count++; /* UDMA crc error -- just retry the operation */
898                         } else if (err & (BBD_ERR | ECC_ERR))   /* retries won't help these */
899                                 rq->errors = ERROR_MAX;
900                         else if (err & TRK0_ERR)        /* help it find track zero */
901                                 rq->errors |= ERROR_RECAL;
902                 }
903                 /* pre bio (rq->cmd != WRITE) */
904                 if ((stat & DRQ_STAT) && rq_data_dir(rq) == READ)
905                         try_to_flush_leftover_data(drive);
906         }
907         if (GET_STAT() & (BUSY_STAT|DRQ_STAT))
908                 OUT_BYTE(WIN_IDLEIMMEDIATE,IDE_COMMAND_REG);    /* force an abort */
909
910         if (rq->errors >= ERROR_MAX) {
911                 if (ata_ops(drive) && ata_ops(drive)->end_request)
912                         ata_ops(drive)->end_request(drive, rq, 0);
913                 else
914                         ide_end_request(drive, rq, 0);
915         } else {
916                 if ((rq->errors & ERROR_RESET) == ERROR_RESET) {
917                         ++rq->errors;
918                         return do_reset1(drive, 0);
919                 }
920                 if ((rq->errors & ERROR_RECAL) == ERROR_RECAL)
921                         drive->special_cmd |= ATA_SPECIAL_RECALIBRATE;
922                 ++rq->errors;
923         }
924         return ide_stopped;
925 }
926
927 /*
928  * Issue a simple drive command.  The drive must be selected beforehand.
929  */
930 void ide_cmd(ide_drive_t *drive, byte cmd, byte nsect, ata_handler_t handler)
931 {
932         ide_set_handler (drive, handler, WAIT_CMD, NULL);
933         if (IDE_CONTROL_REG)
934                 OUT_BYTE(drive->ctl,IDE_CONTROL_REG);   /* clear nIEN */
935         SELECT_MASK(drive->channel, drive, 0);
936         OUT_BYTE(nsect,IDE_NSECTOR_REG);
937         OUT_BYTE(cmd,IDE_COMMAND_REG);
938 }
939
940 /*
941  * Invoked on completion of a special DRIVE_CMD.
942  */
943 static ide_startstop_t drive_cmd_intr(struct ata_device *drive, struct request *rq)
944 {
945         u8 *args = rq->buffer;
946         u8 stat = GET_STAT();
947         int retries = 10;
948
949         ide__sti();     /* local CPU only */
950         if ((stat & DRQ_STAT) && args && args[3]) {
951                 ata_read(drive, &args[4], args[3] * SECTOR_WORDS);
952
953                 while (((stat = GET_STAT()) & BUSY_STAT) && retries--)
954                         udelay(100);
955         }
956
957         if (!OK_STAT(stat, READY_STAT, BAD_STAT))
958                 return ide_error(drive, "drive_cmd", stat); /* calls ide_end_drive_cmd */
959         ide_end_drive_cmd (drive, stat, GET_ERR());
960
961         return ide_stopped;
962 }
963
964 /*
965  * Busy-wait for the drive status to be not "busy".  Check then the status for
966  * all of the "good" bits and none of the "bad" bits, and if all is okay it
967  * returns 0.  All other cases return 1 after invoking ide_error() -- caller
968  * should just return.
969  *
970  * This routine should get fixed to not hog the cpu during extra long waits..
971  * That could be done by busy-waiting for the first jiffy or two, and then
972  * setting a timer to wake up at half second intervals thereafter, until
973  * timeout is achieved, before timing out.
974  */
975 int ide_wait_stat(ide_startstop_t *startstop, ide_drive_t *drive, byte good, byte bad, unsigned long timeout) {
976         byte stat;
977         int i;
978         unsigned long flags;
979
980         /* bail early if we've exceeded max_failures */
981         if (drive->max_failures && (drive->failures > drive->max_failures)) {
982                 *startstop = ide_stopped;
983                 return 1;
984         }
985
986         udelay(1);      /* spec allows drive 400ns to assert "BUSY" */
987         if ((stat = GET_STAT()) & BUSY_STAT) {
988                 __save_flags(flags);    /* local CPU only */
989                 ide__sti();             /* local CPU only */
990                 timeout += jiffies;
991                 while ((stat = GET_STAT()) & BUSY_STAT) {
992                         if (0 < (signed long)(jiffies - timeout)) {
993                                 __restore_flags(flags); /* local CPU only */
994                                 *startstop = ide_error(drive, "status timeout", stat);
995                                 return 1;
996                         }
997                 }
998                 __restore_flags(flags); /* local CPU only */
999         }
1000         /*
1001          * Allow status to settle, then read it again.
1002          * A few rare drives vastly violate the 400ns spec here,
1003          * so we'll wait up to 10usec for a "good" status
1004          * rather than expensively fail things immediately.
1005          * This fix courtesy of Matthew Faupel & Niccolo Rigacci.
1006          */
1007         for (i = 0; i < 10; i++) {
1008                 udelay(1);
1009                 if (OK_STAT((stat = GET_STAT()), good, bad))
1010                         return 0;
1011         }
1012         *startstop = ide_error(drive, "status error", stat);
1013         return 1;
1014 }
1015
1016 /*
1017  * This initiates handling of a new I/O request.
1018  */
1019 static ide_startstop_t start_request(ide_drive_t *drive, struct request *rq)
1020 {
1021         sector_t block;
1022         unsigned int minor = minor(rq->rq_dev);
1023         unsigned int unit = minor >> PARTN_BITS;
1024         struct ata_channel *ch = drive->channel;
1025
1026         BUG_ON(!(rq->flags & REQ_STARTED));
1027
1028 #ifdef DEBUG
1029         printk("%s: start_request: current=0x%08lx\n", ch->name, (unsigned long) rq);
1030 #endif
1031
1032         /* bail early if we've exceeded max_failures */
1033         if (drive->max_failures && (drive->failures > drive->max_failures))
1034                 goto kill_rq;
1035
1036         if (unit >= MAX_DRIVES) {
1037                 printk(KERN_ERR "%s: bad device number: %s\n", ch->name, kdevname(rq->rq_dev));
1038                 goto kill_rq;
1039         }
1040
1041         block    = rq->sector;
1042
1043         /* Strange disk manager remap.
1044          */
1045         if ((rq->flags & REQ_CMD) &&
1046             (drive->type == ATA_DISK || drive->type == ATA_FLOPPY)) {
1047                 block += drive->sect0;
1048         }
1049
1050         /* Yecch - this will shift the entire interval, possibly killing some
1051          * innocent following sector.
1052          */
1053         if (block == 0 && drive->remap_0_to_1 == 1)
1054                 block = 1;  /* redirect MBR access to EZ-Drive partn table */
1055
1056 #if (DISK_RECOVERY_TIME > 0)
1057         while ((read_timer() - ch->last_time) < DISK_RECOVERY_TIME);
1058 #endif
1059
1060         {
1061                 ide_startstop_t res;
1062
1063                 SELECT_DRIVE(ch, drive);
1064                 if (ide_wait_stat(&res, drive, drive->ready_stat,
1065                                         BUSY_STAT|DRQ_STAT, WAIT_READY)) {
1066                         printk(KERN_WARNING "%s: drive not ready for command\n", drive->name);
1067
1068                         return res;
1069                 }
1070         }
1071
1072         /* FIXME: We can see nicely here that all commands should be submitted
1073          * through the request queue and that the special field in drive should
1074          * go as soon as possible!
1075          */
1076
1077         if (drive->special_cmd)
1078                 return ata_special(drive);
1079
1080         /* This issues a special drive command, usually initiated by ioctl()
1081          * from the external hdparm program.
1082          */
1083         if (rq->flags & REQ_DRIVE_ACB) {
1084                 struct ata_taskfile *args = rq->special;
1085
1086                 if (!(args))
1087                         goto args_error;
1088
1089                 ata_taskfile(drive, args, NULL);
1090
1091                 if (((args->command_type == IDE_DRIVE_TASK_RAW_WRITE) ||
1092                                         (args->command_type == IDE_DRIVE_TASK_OUT)) &&
1093                                 args->prehandler && args->handler)
1094                         return args->prehandler(drive, rq);
1095
1096                 return ide_started;
1097         }
1098
1099         if (rq->flags & REQ_DRIVE_CMD) {
1100                 u8 *args = rq->buffer;
1101
1102                 if (!(args))
1103                         goto args_error;
1104 #ifdef DEBUG
1105                 printk("%s: DRIVE_CMD ", drive->name);
1106                 printk("cmd=0x%02x ", args[0]);
1107                 printk("sc=0x%02x ", args[1]);
1108                 printk("fr=0x%02x ", args[2]);
1109                 printk("xx=0x%02x\n", args[3]);
1110 #endif
1111                 if (args[0] == WIN_SMART) {
1112                         OUT_BYTE(0x4f, IDE_LCYL_REG);
1113                         OUT_BYTE(0xc2, IDE_HCYL_REG);
1114                         OUT_BYTE(args[2],IDE_FEATURE_REG);
1115                         OUT_BYTE(args[1],IDE_SECTOR_REG);
1116                         ide_cmd(drive, args[0], args[3], &drive_cmd_intr);
1117
1118                         return ide_started;
1119                 }
1120                 OUT_BYTE(args[2],IDE_FEATURE_REG);
1121                 ide_cmd(drive, args[0], args[1], &drive_cmd_intr);
1122
1123                 return ide_started;
1124         }
1125
1126         /* The normal way of execution is to pass and execute the request
1127          * handler down to the device type driver.
1128          */
1129         if (ata_ops(drive)) {
1130                 if (ata_ops(drive)->do_request)
1131                         return ata_ops(drive)->do_request(drive, rq, block);
1132                 else {
1133                         ide_end_request(drive, rq, 0);
1134                         return ide_stopped;
1135                 }
1136         }
1137
1138         /*
1139          * Error handling:
1140          */
1141
1142         printk(KERN_WARNING "%s: device type %d not supported\n",
1143                         drive->name, drive->type);
1144
1145 kill_rq:
1146         if (ata_ops(drive) && ata_ops(drive)->end_request)
1147                 ata_ops(drive)->end_request(drive, rq, 0);
1148         else
1149                 ide_end_request(drive, rq, 0);
1150
1151         return ide_stopped;
1152
1153 args_error:
1154
1155         /* NULL as arguemnt is used by ioctls as a way of waiting for all
1156          * current requests to be flushed from the queue.
1157          */
1158
1159 #ifdef DEBUG
1160         printk("%s: DRIVE_CMD (null)\n", drive->name);
1161 #endif
1162         ide_end_drive_cmd(drive, GET_STAT(), GET_ERR());
1163
1164         return ide_stopped;
1165 }
1166
1167 ide_startstop_t restart_request(ide_drive_t *drive)
1168 {
1169         ide_hwgroup_t *hwgroup = HWGROUP(drive);
1170         unsigned long flags;
1171         struct request *rq;
1172
1173         spin_lock_irqsave(&ide_lock, flags);
1174         hwgroup->handler = NULL;
1175         del_timer(&hwgroup->timer);
1176         rq = hwgroup->rq;
1177         spin_unlock_irqrestore(&ide_lock, flags);
1178
1179         return start_request(drive, rq);
1180 }
1181
1182 /*
1183  * This is used by a drive to give excess bandwidth back to the hwgroup by
1184  * sleeping for timeout jiffies.
1185  */
1186 void ide_stall_queue(ide_drive_t *drive, unsigned long timeout)
1187 {
1188         if (timeout > WAIT_WORSTCASE)
1189                 timeout = WAIT_WORSTCASE;
1190         drive->PADAM_sleep = timeout + jiffies;
1191 }
1192
1193
1194 /*
1195  * Determine the longes sleep time for the devices in our hwgroup.
1196  */
1197 static unsigned long longest_sleep(struct ata_channel *channel)
1198 {
1199         unsigned long sleep = 0;
1200         int i;
1201
1202         for (i = 0; i < MAX_HWIFS; ++i) {
1203                 int unit;
1204                 struct ata_channel *ch = &ide_hwifs[i];
1205
1206                 if (!ch->present)
1207                         continue;
1208
1209                 if (ch->hwgroup != channel->hwgroup)
1210                         continue;
1211
1212                 for (unit = 0; unit < MAX_DRIVES; ++unit) {
1213                         struct ata_device *drive = &ch->drives[unit];
1214
1215                         if (!drive->present)
1216                                 continue;
1217
1218                         /* This device is sleeping and waiting to be serviced
1219                          * later than any other device we checked thus far.
1220                          */
1221                         if (drive->PADAM_sleep && (!sleep || time_after(sleep, drive->PADAM_sleep)))
1222                                 sleep = drive->PADAM_sleep;
1223                 }
1224         }
1225
1226         return sleep;
1227 }
1228
1229 /*
1230  * Select the next device which will be serviced.
1231  */
1232 static struct ata_device *choose_urgent_device(struct ata_channel *channel)
1233 {
1234         struct ata_device *choice = NULL;
1235         unsigned long sleep = 0;
1236         int i;
1237
1238         for (i = 0; i < MAX_HWIFS; ++i) {
1239                 int unit;
1240                 struct ata_channel *ch = &ide_hwifs[i];
1241
1242                 if (!ch->present)
1243                         continue;
1244
1245                 if (ch->hwgroup != channel->hwgroup)
1246                         continue;
1247
1248                 for (unit = 0; unit < MAX_DRIVES; ++unit) {
1249                         struct ata_device *drive = &ch->drives[unit];
1250
1251                         if (!drive->present)
1252                                 continue;
1253
1254                         /* There are no request pending for this device.
1255                          */
1256                         if (list_empty(&drive->queue.queue_head))
1257                                 continue;
1258
1259                         /* This device still want's to remain idle.
1260                          */
1261                         if (drive->PADAM_sleep && time_after(jiffies, drive->PADAM_sleep))
1262                                 continue;
1263
1264                         /* Take this device, if there is no device choosen thus far or
1265                          * it's more urgent.
1266                          */
1267                         if (!choice || (drive->PADAM_sleep && (!choice->PADAM_sleep || time_after(choice->PADAM_sleep, drive->PADAM_sleep))))
1268                         {
1269                                 if (!blk_queue_plugged(&drive->queue))
1270                                         choice = drive;
1271                         }
1272                 }
1273         }
1274
1275         if (choice)
1276                 return choice;
1277
1278         channel->hwgroup->rq = NULL;
1279         sleep = longest_sleep(channel);
1280
1281         if (sleep) {
1282
1283                 /*
1284                  * Take a short snooze, and then wake up again.  Just in case
1285                  * there are big differences in relative throughputs.. don't
1286                  * want to hog the cpu too much.
1287                  */
1288
1289                 if (0 < (signed long)(jiffies + WAIT_MIN_SLEEP - sleep))
1290                     sleep = jiffies + WAIT_MIN_SLEEP;
1291 #if 1
1292                 if (timer_pending(&channel->hwgroup->timer))
1293                         printk(KERN_ERR "ide_set_handler: timer already active\n");
1294 #endif
1295                 set_bit(IDE_SLEEP, &channel->hwgroup->flags);
1296                 mod_timer(&channel->hwgroup->timer, sleep);
1297                 /* we purposely leave hwgroup busy while sleeping */
1298         } else {
1299                 /* Ugly, but how can we sleep for the lock otherwise? perhaps
1300                  * from tq_disk? */
1301                 ide_release_lock(&irq_lock);/* for atari only */
1302                 clear_bit(IDE_BUSY, &channel->hwgroup->flags);
1303         }
1304
1305         return NULL;
1306 }
1307
1308
1309 /* Place holders for later expansion of functionality.
1310  */
1311 #define ata_pending_commands(drive)     (0)
1312 #define ata_can_queue(drive)            (1)
1313
1314 /*
1315  * Feed commands to a drive until it barfs.  Called with ide_lock/DRIVE_LOCK
1316  * held and busy channel.
1317  */
1318
1319 static void queue_commands(struct ata_device *drive, int masked_irq)
1320 {
1321         ide_hwgroup_t *hwgroup = drive->channel->hwgroup;
1322         ide_startstop_t startstop = -1;
1323
1324         for (;;) {
1325                 struct request *rq = NULL;
1326
1327                 if (!test_bit(IDE_BUSY, &hwgroup->flags))
1328                         printk(KERN_ERR"%s: hwgroup not busy while queueing\n", drive->name);
1329
1330                 /* Abort early if we can't queue another command. for non
1331                  * tcq, ata_can_queue is always 1 since we never get here
1332                  * unless the drive is idle.
1333                  */
1334                 if (!ata_can_queue(drive)) {
1335                         if (!ata_pending_commands(drive))
1336                                 clear_bit(IDE_BUSY, &hwgroup->flags);
1337                         break;
1338                 }
1339
1340                 drive->PADAM_sleep = 0;
1341
1342                 if (test_bit(IDE_DMA, &hwgroup->flags)) {
1343                         printk("ide_do_request: DMA in progress...\n");
1344                         break;
1345                 }
1346
1347                 /* There's a small window between where the queue could be
1348                  * replugged while we are in here when using tcq (in which
1349                  * case the queue is probably empty anyways...), so check
1350                  * and leave if appropriate. When not using tcq, this is
1351                  * still a severe BUG!
1352                  */
1353                 if (blk_queue_plugged(&drive->queue)) {
1354                         BUG();
1355                         break;
1356                 }
1357
1358                 if (!(rq = elv_next_request(&drive->queue))) {
1359                         if (!ata_pending_commands(drive))
1360                                 clear_bit(IDE_BUSY, &hwgroup->flags);
1361                         hwgroup->rq = NULL;
1362                         break;
1363                 }
1364
1365                 /* If there are queued commands, we can't start a non-fs
1366                  * request (really, a non-queuable command) until the
1367                  * queue is empty.
1368                  */
1369                 if (!(rq->flags & REQ_CMD) && ata_pending_commands(drive))
1370                         break;
1371
1372                 hwgroup->rq = rq;
1373
1374                 /* Some systems have trouble with IDE IRQs arriving while the
1375                  * driver is still setting things up.  So, here we disable the
1376                  * IRQ used by this interface while the request is being
1377                  * started.  This may look bad at first, but pretty much the
1378                  * same thing happens anyway when any interrupt comes in, IDE
1379                  * or otherwise -- the kernel masks the IRQ while it is being
1380                  * handled.
1381                  */
1382
1383                 if (masked_irq && drive->channel->irq != masked_irq)
1384                         disable_irq_nosync(drive->channel->irq);
1385
1386                 spin_unlock(&ide_lock);
1387                 ide__sti();     /* allow other IRQs while we start this request */
1388                 startstop = start_request(drive, rq);
1389
1390                 spin_lock_irq(&ide_lock);
1391                 if (masked_irq && drive->channel->irq != masked_irq)
1392                         enable_irq(drive->channel->irq);
1393
1394                 /* command started, we are busy */
1395                 if (startstop == ide_started)
1396                         break;
1397
1398                 /* start_request() can return either ide_stopped (no command
1399                  * was started), ide_started (command started, don't queue
1400                  * more), or ide_released (command started, try and queue
1401                  * more).
1402                  */
1403 #if 0
1404                 if (startstop == ide_stopped)
1405                         set_bit(IDE_BUSY, &hwgroup->flags);
1406 #endif
1407
1408         }
1409 }
1410
1411 /*
1412  * Issue a new request.
1413  * Caller must have already done spin_lock_irqsave(&ide_lock, ...)
1414  *
1415  * A hwgroup is a serialized group of IDE interfaces.  Usually there is
1416  * exactly one hwif (interface) per hwgroup, but buggy controllers (eg. CMD640)
1417  * may have both interfaces in a single hwgroup to "serialize" access.
1418  * Or possibly multiple ISA interfaces can share a common IRQ by being grouped
1419  * together into one hwgroup for serialized access.
1420  *
1421  * Note also that several hwgroups can end up sharing a single IRQ,
1422  * possibly along with many other devices.  This is especially common in
1423  * PCI-based systems with off-board IDE controller cards.
1424  *
1425  * The IDE driver uses the queue spinlock to protect access to the request
1426  * queues.
1427  *
1428  * The first thread into the driver for a particular hwgroup sets the
1429  * hwgroup->flags IDE_BUSY flag to indicate that this hwgroup is now active,
1430  * and then initiates processing of the top request from the request queue.
1431  *
1432  * Other threads attempting entry notice the busy setting, and will simply
1433  * queue their new requests and exit immediately.  Note that hwgroup->flags
1434  * remains busy even when the driver is merely awaiting the next interrupt.
1435  * Thus, the meaning is "this hwgroup is busy processing a request".
1436  *
1437  * When processing of a request completes, the completing thread or IRQ-handler
1438  * will start the next request from the queue.  If no more work remains,
1439  * the driver will clear the hwgroup->flags IDE_BUSY flag and exit.
1440  */
1441 static void ide_do_request(struct ata_channel *channel, int masked_irq)
1442 {
1443         ide_hwgroup_t *hwgroup = channel->hwgroup;
1444         ide_get_lock(&irq_lock, ata_irq_request, hwgroup);/* for atari only: POSSIBLY BROKEN HERE(?) */
1445
1446         __cli();        /* necessary paranoia: ensure IRQs are masked on local CPU */
1447
1448         while (!test_and_set_bit(IDE_BUSY, &hwgroup->flags)) {
1449                 struct ata_channel *ch;
1450                 struct ata_device *drive;
1451
1452                 /* this will clear IDE_BUSY, if appropriate */
1453                 drive = choose_urgent_device(channel);
1454
1455                 if (!drive)
1456                         break;
1457
1458                 ch = drive->channel;
1459
1460                 if (hwgroup->XXX_drive->channel->sharing_irq && ch != hwgroup->XXX_drive->channel && ch->io_ports[IDE_CONTROL_OFFSET]) {
1461                         /* set nIEN for previous channel */
1462                         /* FIXME: check this! It appears to act on the current channel! */
1463
1464                         if (ch->intrproc)
1465                                 ch->intrproc(drive);
1466                         else
1467                                 OUT_BYTE((drive)->ctl|2, ch->io_ports[IDE_CONTROL_OFFSET]);
1468                 }
1469
1470                 /* Remember the last drive we where acting on.
1471                  */
1472                 hwgroup->XXX_drive = drive;
1473
1474                 queue_commands(drive, masked_irq);
1475         }
1476 }
1477
1478 void do_ide_request(request_queue_t *q)
1479 {
1480         ide_do_request(q->queuedata, 0);
1481 }
1482
1483 /*
1484  * Un-busy the hwgroup etc, and clear any pending DMA status. we want to
1485  * retry the current request in PIO mode instead of risking tossing it
1486  * all away
1487  */
1488 static void dma_timeout_retry(struct ata_device *drive, struct request *rq)
1489 {
1490         struct ata_channel *ch = drive->channel;
1491
1492         /*
1493          * end current dma transaction
1494          */
1495         ch->udma(ide_dma_end, drive, rq);
1496
1497         /*
1498          * complain a little, later we might remove some of this verbosity
1499          */
1500         printk("%s: timeout waiting for DMA\n", drive->name);
1501         ch->udma(ide_dma_timeout, drive, rq);
1502
1503         /*
1504          * Disable dma for now, but remember that we did so because of
1505          * a timeout -- we'll reenable after we finish this next request
1506          * (or rather the first chunk of it) in pio.
1507          */
1508         drive->retry_pio++;
1509         drive->state = DMA_PIO_RETRY;
1510         ch->udma(ide_dma_off_quietly, drive, rq);
1511
1512         /*
1513          * un-busy drive etc (hwgroup->busy is cleared on return) and
1514          * make sure request is sane
1515          */
1516         HWGROUP(drive)->rq = NULL;
1517
1518         rq->errors = 0;
1519         if (rq->bio) {
1520                 rq->sector = rq->bio->bi_sector;
1521                 rq->current_nr_sectors = bio_iovec(rq->bio)->bv_len >> 9;
1522                 rq->buffer = NULL;
1523         }
1524 }
1525
1526 /*
1527  * This is our timeout function for all drive operations.  But note that it can
1528  * also be invoked as a result of a "sleep" operation triggered by the
1529  * mod_timer() call in ide_do_request.
1530  */
1531 void ide_timer_expiry(unsigned long data)
1532 {
1533         ide_hwgroup_t *hwgroup = (ide_hwgroup_t *) data;
1534         ata_handler_t *handler;
1535         ata_expiry_t *expiry;
1536         unsigned long flags;
1537         unsigned long wait;
1538
1539         /*
1540          * A global lock protects timers etc -- shouldn't get contention
1541          * worth mentioning.
1542          */
1543
1544         spin_lock_irqsave(&ide_lock, flags);
1545         del_timer(&hwgroup->timer);
1546
1547         if ((handler = hwgroup->handler) == NULL) {
1548
1549                 /*
1550                  * Either a marginal timeout occurred (got the interrupt just
1551                  * as timer expired), or we were "sleeping" to give other
1552                  * devices a chance.  Either way, we don't really want to
1553                  * complain about anything.
1554                  */
1555
1556                 if (test_and_clear_bit(IDE_SLEEP, &hwgroup->flags))
1557                         clear_bit(IDE_BUSY, &hwgroup->flags);
1558         } else {
1559                 struct ata_device *drive = hwgroup->XXX_drive;
1560                 if (!drive) {
1561                         printk("ide_timer_expiry: hwgroup->drive was NULL\n");
1562                         hwgroup->handler = NULL;
1563                 } else {
1564                         struct ata_channel *ch;
1565                         ide_startstop_t startstop;
1566
1567                         /* paranoia */
1568                         if (!test_and_set_bit(IDE_BUSY, &hwgroup->flags))
1569                                 printk("%s: ide_timer_expiry: hwgroup was not busy??\n", drive->name);
1570                         if ((expiry = hwgroup->expiry) != NULL) {
1571                                 /* continue */
1572                                 if ((wait = expiry(drive, HWGROUP(drive)->rq)) != 0) {
1573                                         /* reengage timer */
1574                                         hwgroup->timer.expires  = jiffies + wait;
1575                                         add_timer(&hwgroup->timer);
1576                                         spin_unlock_irqrestore(&ide_lock, flags);
1577                                         return;
1578                                 }
1579                         }
1580                         hwgroup->handler = NULL;
1581                         /*
1582                          * We need to simulate a real interrupt when invoking
1583                          * the handler() function, which means we need to globally
1584                          * mask the specific IRQ:
1585                          */
1586                         spin_unlock(&ide_lock);
1587                         ch = drive->channel;
1588 #if DISABLE_IRQ_NOSYNC
1589                         disable_irq_nosync(ch->irq);
1590 #else
1591                         disable_irq(ch->irq);   /* disable_irq_nosync ?? */
1592 #endif
1593                         __cli();        /* local CPU only, as if we were handling an interrupt */
1594                         if (hwgroup->poll_timeout != 0) {
1595                                 startstop = handler(drive, ch->hwgroup->rq);
1596                         } else if (drive_is_ready(drive)) {
1597                                 if (drive->waiting_for_dma)
1598                                         ch->udma(ide_dma_lostirq, drive, ch->hwgroup->rq);
1599                                 (void) ide_ack_intr(ch);
1600                                 printk("%s: lost interrupt\n", drive->name);
1601                                 startstop = handler(drive, ch->hwgroup->rq);
1602                         } else {
1603                                 if (drive->waiting_for_dma) {
1604                                         startstop = ide_stopped;
1605                                         dma_timeout_retry(drive, ch->hwgroup->rq);
1606                                 } else
1607                                         startstop = ide_error(drive, "irq timeout", GET_STAT());
1608                         }
1609                         set_recovery_timer(ch);
1610                         enable_irq(ch->irq);
1611                         spin_lock_irq(&ide_lock);
1612                         if (startstop == ide_stopped)
1613                                 clear_bit(IDE_BUSY, &hwgroup->flags);
1614                 }
1615         }
1616
1617         ide_do_request(hwgroup->XXX_drive->channel, 0);
1618
1619         spin_unlock_irqrestore(&ide_lock, flags);
1620 }
1621
1622 /*
1623  * There's nothing really useful we can do with an unexpected interrupt,
1624  * other than reading the status register (to clear it), and logging it.
1625  * There should be no way that an irq can happen before we're ready for it,
1626  * so we needn't worry much about losing an "important" interrupt here.
1627  *
1628  * On laptops (and "green" PCs), an unexpected interrupt occurs whenever the
1629  * drive enters "idle", "standby", or "sleep" mode, so if the status looks
1630  * "good", we just ignore the interrupt completely.
1631  *
1632  * This routine assumes __cli() is in effect when called.
1633  *
1634  * If an unexpected interrupt happens on irq15 while we are handling irq14
1635  * and if the two interfaces are "serialized" (CMD640), then it looks like
1636  * we could screw up by interfering with a new request being set up for irq15.
1637  *
1638  * In reality, this is a non-issue.  The new command is not sent unless the
1639  * drive is ready to accept one, in which case we know the drive is not
1640  * trying to interrupt us.  And ide_set_handler() is always invoked before
1641  * completing the issuance of any new drive command, so we will not be
1642  * accidentally invoked as a result of any valid command completion interrupt.
1643  *
1644  */
1645 static void unexpected_irq(int irq)
1646 {
1647         int i;
1648
1649         for (i = 0; i < MAX_HWIFS; ++i) {
1650                 u8 stat;
1651                 struct ata_channel *ch = &ide_hwifs[i];
1652
1653                 if (!ch->present)
1654                         continue;
1655
1656                 if (ch->irq != irq)
1657                         continue;
1658
1659                 stat = IN_BYTE(ch->io_ports[IDE_STATUS_OFFSET]);
1660                 if (!OK_STAT(stat, READY_STAT, BAD_STAT)) {
1661                         /* Try to not flood the console with msgs */
1662                         static unsigned long last_msgtime;
1663                         static int count;
1664
1665                         ++count;
1666                         if (time_after(jiffies, last_msgtime + HZ)) {
1667                                 last_msgtime = jiffies;
1668                                 printk("%s: unexpected interrupt, status=0x%02x, count=%d\n",
1669                                                 ch->name, stat, count);
1670                         }
1671                 }
1672         }
1673 }
1674
1675 /*
1676  * Entry point for all interrupts, caller does __cli() for us.
1677  */
1678 void ata_irq_request(int irq, void *data, struct pt_regs *regs)
1679 {
1680         struct ata_channel *ch = data;
1681         ide_hwgroup_t *hwgroup = ch->hwgroup;
1682
1683         unsigned long flags;
1684         struct ata_device *drive;
1685         ata_handler_t *handler = hwgroup->handler;
1686         ide_startstop_t startstop;
1687
1688         spin_lock_irqsave(&ide_lock, flags);
1689
1690         if (!ide_ack_intr(ch))
1691                 goto out_lock;
1692
1693         if (handler == NULL || hwgroup->poll_timeout != 0) {
1694 #if 0
1695                 printk(KERN_INFO "ide: unexpected interrupt %d %d\n", ch->unit, irq);
1696 #endif
1697                 /*
1698                  * Not expecting an interrupt from this drive.
1699                  * That means this could be:
1700                  *      (1) an interrupt from another PCI device
1701                  *      sharing the same PCI INT# as us.
1702                  * or   (2) a drive just entered sleep or standby mode,
1703                  *      and is interrupting to let us know.
1704                  * or   (3) a spurious interrupt of unknown origin.
1705                  *
1706                  * For PCI, we cannot tell the difference,
1707                  * so in that case we just ignore it and hope it goes away.
1708                  */
1709 #ifdef CONFIG_BLK_DEV_IDEPCI
1710                 if (ch->pci_dev && !ch->pci_dev->vendor)
1711 #endif
1712                 {
1713                         /* Probably not a shared PCI interrupt, so we can
1714                          * safely try to do something about it:
1715                          */
1716                         unexpected_irq(irq);
1717 #ifdef CONFIG_BLK_DEV_IDEPCI
1718                 } else {
1719                         /*
1720                          * Whack the status register, just in case we have a leftover pending IRQ.
1721                          */
1722                         IN_BYTE(ch->io_ports[IDE_STATUS_OFFSET]);
1723 #endif
1724                 }
1725                 goto out_lock;
1726         }
1727         drive = hwgroup->XXX_drive;
1728         if (!drive_is_ready(drive)) {
1729                 /*
1730                  * This happens regularly when we share a PCI IRQ with another device.
1731                  * Unfortunately, it can also happen with some buggy drives that trigger
1732                  * the IRQ before their status register is up to date.  Hopefully we have
1733                  * enough advance overhead that the latter isn't a problem.
1734                  */
1735                 goto out_lock;
1736         }
1737         /* paranoia */
1738         if (!test_and_set_bit(IDE_BUSY, &hwgroup->flags))
1739                 printk(KERN_ERR "%s: %s: hwgroup was not busy!?\n", drive->name, __FUNCTION__);
1740         hwgroup->handler = NULL;
1741         del_timer(&hwgroup->timer);
1742         spin_unlock(&ide_lock);
1743
1744         if (ch->unmask)
1745                 ide__sti();     /* local CPU only */
1746
1747         /* service this interrupt, may set handler for next interrupt */
1748         startstop = handler(drive, hwgroup->rq);
1749         spin_lock_irq(&ide_lock);
1750
1751         /*
1752          * Note that handler() may have set things up for another
1753          * interrupt to occur soon, but it cannot happen until
1754          * we exit from this routine, because it will be the
1755          * same irq as is currently being serviced here, and Linux
1756          * won't allow another of the same (on any CPU) until we return.
1757          */
1758         set_recovery_timer(drive->channel);
1759         if (startstop == ide_stopped) {
1760                 if (hwgroup->handler == NULL) { /* paranoia */
1761                         clear_bit(IDE_BUSY, &hwgroup->flags);
1762                         ide_do_request(ch, ch->irq);
1763                 } else {
1764                         printk("%s: %s: huh? expected NULL handler on exit\n", drive->name, __FUNCTION__);
1765                 }
1766         }
1767
1768 out_lock:
1769         spin_unlock_irqrestore(&ide_lock, flags);
1770 }
1771
1772 /*
1773  * get_info_ptr() returns the (ide_drive_t *) for a given device number.
1774  * It returns NULL if the given device number does not match any present drives.
1775  */
1776 ide_drive_t *get_info_ptr(kdev_t i_rdev)
1777 {
1778         unsigned int major = major(i_rdev);
1779         int h;
1780
1781         for (h = 0; h < MAX_HWIFS; ++h) {
1782                 struct ata_channel *ch = &ide_hwifs[h];
1783                 if (ch->present && major == ch->major) {
1784                         int unit = DEVICE_NR(i_rdev);
1785                         if (unit < MAX_DRIVES) {
1786                                 struct ata_device *drive = &ch->drives[unit];
1787                                 if (drive->present)
1788                                         return drive;
1789                         }
1790                         break;
1791                 }
1792         }
1793         return NULL;
1794 }
1795
1796 /*
1797  * This function is intended to be used prior to invoking ide_do_drive_cmd().
1798  */
1799 void ide_init_drive_cmd(struct request *rq)
1800 {
1801         memset(rq, 0, sizeof(*rq));
1802         rq->flags = REQ_DRIVE_CMD;
1803 }
1804
1805 /*
1806  * This function issues a special IDE device request onto the request queue.
1807  *
1808  * If action is ide_wait, then the rq is queued at the end of the request
1809  * queue, and the function sleeps until it has been processed.  This is for use
1810  * when invoked from an ioctl handler.
1811  *
1812  * If action is ide_preempt, then the rq is queued at the head of the request
1813  * queue, displacing the currently-being-processed request and this function
1814  * returns immediately without waiting for the new rq to be completed.  This is
1815  * VERY DANGEROUS, and is intended for careful use by the ATAPI tape/cdrom
1816  * driver code.
1817  *
1818  * If action is ide_next, then the rq is queued immediately after the
1819  * currently-being-processed-request (if any), and the function returns without
1820  * waiting for the new rq to be completed.  As above, This is VERY DANGEROUS,
1821  * and is intended for careful use by the ATAPI tape/cdrom driver code.
1822  *
1823  * If action is ide_end, then the rq is queued at the end of the request queue,
1824  * and the function returns immediately without waiting for the new rq to be
1825  * completed. This is again intended for careful use by the ATAPI tape/cdrom
1826  * driver code.
1827  */
1828 int ide_do_drive_cmd(ide_drive_t *drive, struct request *rq, ide_action_t action)
1829 {
1830         unsigned long flags;
1831         ide_hwgroup_t *hwgroup = HWGROUP(drive);
1832         unsigned int major = drive->channel->major;
1833         request_queue_t *q = &drive->queue;
1834         struct list_head *queue_head = &q->queue_head;
1835         DECLARE_COMPLETION(wait);
1836
1837 #ifdef CONFIG_BLK_DEV_PDC4030
1838         if (drive->channel->chipset == ide_pdc4030 && rq->buffer != NULL)
1839                 return -ENOSYS;  /* special drive cmds not supported */
1840 #endif
1841         rq->errors = 0;
1842         rq->rq_status = RQ_ACTIVE;
1843         rq->rq_dev = mk_kdev(major,(drive->select.b.unit)<<PARTN_BITS);
1844         if (action == ide_wait)
1845                 rq->waiting = &wait;
1846         spin_lock_irqsave(&ide_lock, flags);
1847         if (blk_queue_empty(&drive->queue) || action == ide_preempt) {
1848                 if (action == ide_preempt)
1849                         hwgroup->rq = NULL;
1850         } else {
1851                 if (action == ide_wait || action == ide_end)
1852                         queue_head = queue_head->prev;
1853                 else
1854                         queue_head = queue_head->next;
1855         }
1856         q->elevator.elevator_add_req_fn(q, rq, queue_head);
1857         ide_do_request(drive->channel, 0);
1858         spin_unlock_irqrestore(&ide_lock, flags);
1859         if (action == ide_wait) {
1860                 wait_for_completion(&wait);     /* wait for it to be serviced */
1861                 return rq->errors ? -EIO : 0;   /* return -EIO if errors */
1862         }
1863
1864         return 0;
1865
1866 }
1867
1868 /*
1869  * This routine is called to flush all partitions and partition tables
1870  * for a changed disk, and then re-read the new partition table.
1871  * If we are revalidating a disk because of a media change, then we
1872  * enter with usage == 0.  If we are using an ioctl, we automatically have
1873  * usage == 1 (we need an open channel to use an ioctl :-), so this
1874  * is our limit.
1875  */
1876 int ide_revalidate_disk (kdev_t i_rdev)
1877 {
1878         ide_drive_t *drive;
1879         ide_hwgroup_t *hwgroup;
1880         unsigned long flags;
1881         int res;
1882
1883         if ((drive = get_info_ptr(i_rdev)) == NULL)
1884                 return -ENODEV;
1885         hwgroup = HWGROUP(drive);
1886         spin_lock_irqsave(&ide_lock, flags);
1887         if (drive->busy || (drive->usage > 1)) {
1888                 spin_unlock_irqrestore(&ide_lock, flags);
1889                 return -EBUSY;
1890         }
1891         drive->busy = 1;
1892         MOD_INC_USE_COUNT;
1893         spin_unlock_irqrestore(&ide_lock, flags);
1894
1895         res = wipe_partitions(i_rdev);
1896         if (!res) {
1897                 if (ata_ops(drive) && ata_ops(drive)->revalidate) {
1898                         ata_get(ata_ops(drive));
1899                         /* this is a no-op for tapes and SCSI based access */
1900                         ata_ops(drive)->revalidate(drive);
1901                         ata_put(ata_ops(drive));
1902                 } else
1903                         grok_partitions(i_rdev, ata_capacity(drive));
1904         }
1905
1906         drive->busy = 0;
1907         wake_up(&drive->wqueue);
1908         MOD_DEC_USE_COUNT;
1909         return res;
1910 }
1911
1912 /*
1913  * Look again for all drives in the system on all interfaces.  This is used
1914  * after a new driver category has been loaded as module.
1915  */
1916 void revalidate_drives(void)
1917 {
1918         int i;
1919
1920         for (i = 0; i < MAX_HWIFS; ++i) {
1921                 int unit;
1922                 struct ata_channel *ch = &ide_hwifs[i];
1923
1924                 for (unit = 0; unit < MAX_DRIVES; ++unit) {
1925                         struct ata_device *drive = &ch->drives[unit];
1926
1927                         if (drive->revalidate) {
1928                                 drive->revalidate = 0;
1929                                 if (!initializing)
1930                                         ide_revalidate_disk(mk_kdev(ch->major, unit<<PARTN_BITS));
1931                         }
1932                 }
1933         }
1934 }
1935
1936 static void ide_driver_module (void)
1937 {
1938         int i;
1939
1940         /* Don't reinit the probe if there is already one channel detected. */
1941
1942         for (i = 0; i < MAX_HWIFS; ++i) {
1943                 if (ide_hwifs[i].present)
1944                         goto revalidate;
1945         }
1946
1947         ideprobe_init();
1948
1949 revalidate:
1950         revalidate_drives();
1951 }
1952
1953 static int ide_open(struct inode * inode, struct file * filp)
1954 {
1955         struct ata_device *drive;
1956
1957         if ((drive = get_info_ptr(inode->i_rdev)) == NULL)
1958                 return -ENXIO;
1959         if (drive->driver == NULL)
1960                 ide_driver_module();
1961
1962         /* Request a particular device type module.
1963          *
1964          * FIXME: The function which should rather requests the drivers in
1965          * ide_driver_module(), since it seems illogical and even a bit
1966          * dangerous to delay this until open time!
1967          */
1968
1969 #ifdef CONFIG_KMOD
1970         if (drive->driver == NULL) {
1971                 char *module = NULL;
1972
1973                 switch (drive->type) {
1974                         case ATA_DISK:
1975                                 module = "ide-disk";
1976                                 break;
1977                         case ATA_ROM:
1978                                 module = "ide-cd";
1979                                 break;
1980                         case ATA_TAPE:
1981                                 module = "ide-tape";
1982                                 break;
1983                         case ATA_FLOPPY:
1984                                 module = "ide-floppy";
1985                                 break;
1986                         case ATA_SCSI:
1987                                 module = "ide-scsi";
1988                                 break;
1989                         default:
1990                                 /* nothing we can do about it */ ;
1991                 }
1992                 if (module)
1993                         request_module(module);
1994         }
1995 #endif
1996         while (drive->busy)
1997                 sleep_on(&drive->wqueue);
1998         ++drive->usage;
1999         if (ata_ops(drive) && ata_ops(drive)->open)
2000                 return ata_ops(drive)->open(inode, filp, drive);
2001         else {
2002                 --drive->usage;
2003                 return -ENODEV;
2004         }
2005
2006         printk(KERN_INFO "%s: driver not present\n", drive->name);
2007         drive->usage--;
2008         return -ENXIO;
2009 }
2010
2011 /*
2012  * Releasing a block device means we sync() it, so that it can safely
2013  * be forgotten about...
2014  */
2015 static int ide_release(struct inode * inode, struct file * file)
2016 {
2017         struct ata_device *drive;
2018
2019         if (!(drive = get_info_ptr(inode->i_rdev)))
2020                 return 0;
2021
2022         drive->usage--;
2023         if (ata_ops(drive) && ata_ops(drive)->release)
2024                 ata_ops(drive)->release(inode, file, drive);
2025         return 0;
2026 }
2027
2028 #ifdef CONFIG_PROC_FS
2029 ide_proc_entry_t generic_subdriver_entries[] = {
2030         { "capacity",   S_IFREG|S_IRUGO,        proc_ide_read_capacity, NULL },
2031         { NULL, 0, NULL, NULL }
2032 };
2033 #endif
2034
2035 /*
2036  * Note that we only release the standard ports, and do not even try to handle
2037  * any extra ports allocated for weird IDE interface chipsets.
2038  */
2039 static void hwif_unregister(struct ata_channel *ch)
2040 {
2041         int i;
2042         ide_hwgroup_t *hwgroup = ch->hwgroup;
2043
2044         /*
2045          * Free the irq if we were the only channel using it.
2046          */
2047         int n = 0;
2048
2049         for (i = 0; i < MAX_HWIFS; ++i) {
2050                 struct ata_channel *tmp = &ide_hwifs[i];
2051
2052                 if (!tmp->present)
2053                         continue;
2054
2055                 if (tmp->irq == ch->irq)
2056                         ++n;
2057         }
2058         if (n == 1)
2059                 free_irq(ch->irq, hwgroup);
2060
2061
2062         if (ch->straight8) {
2063                 release_region(ch->io_ports[IDE_DATA_OFFSET], 8);
2064         } else {
2065                 if (ch->io_ports[IDE_DATA_OFFSET])
2066                         release_region(ch->io_ports[IDE_DATA_OFFSET], 1);
2067                 if (ch->io_ports[IDE_ERROR_OFFSET])
2068                         release_region(ch->io_ports[IDE_ERROR_OFFSET], 1);
2069                 if (ch->io_ports[IDE_NSECTOR_OFFSET])
2070                         release_region(ch->io_ports[IDE_NSECTOR_OFFSET], 1);
2071                 if (ch->io_ports[IDE_SECTOR_OFFSET])
2072                         release_region(ch->io_ports[IDE_SECTOR_OFFSET], 1);
2073                 if (ch->io_ports[IDE_LCYL_OFFSET])
2074                         release_region(ch->io_ports[IDE_LCYL_OFFSET], 1);
2075                 if (ch->io_ports[IDE_HCYL_OFFSET])
2076                         release_region(ch->io_ports[IDE_HCYL_OFFSET], 1);
2077                 if (ch->io_ports[IDE_SELECT_OFFSET])
2078                         release_region(ch->io_ports[IDE_SELECT_OFFSET], 1);
2079                 if (ch->io_ports[IDE_STATUS_OFFSET])
2080                         release_region(ch->io_ports[IDE_STATUS_OFFSET], 1);
2081         }
2082         if (ch->io_ports[IDE_CONTROL_OFFSET])
2083                 release_region(ch->io_ports[IDE_CONTROL_OFFSET], 1);
2084 #if defined(CONFIG_AMIGA) || defined(CONFIG_MAC)
2085         if (ch->io_ports[IDE_IRQ_OFFSET])
2086                 release_region(ch->io_ports[IDE_IRQ_OFFSET], 1);
2087 #endif
2088 }
2089
2090 void ide_unregister(struct ata_channel *ch)
2091 {
2092         struct gendisk *gd;
2093         struct ata_device *d;
2094         ide_hwgroup_t *hwgroup;
2095         int unit, i;
2096         unsigned long flags;
2097         unsigned int p, minor;
2098         struct ata_channel old;
2099         int n = 0;
2100
2101         spin_lock_irqsave(&ide_lock, flags);
2102
2103         if (!ch->present)
2104                 goto abort;
2105
2106         put_device(&ch->dev);
2107         for (unit = 0; unit < MAX_DRIVES; ++unit) {
2108                 struct ata_device * drive = &ch->drives[unit];
2109
2110                 if (!drive->present)
2111                         continue;
2112
2113                 if (drive->busy || drive->usage)
2114                         goto abort;
2115
2116                 if (ata_ops(drive)) {
2117                         if (ata_ops(drive)->cleanup) {
2118                                 if (ata_ops(drive)->cleanup(drive))
2119                                         goto abort;
2120                         } else
2121                                 ide_unregister_subdriver(drive);
2122                 }
2123         }
2124         ch->present = 0;
2125
2126         /*
2127          * All clear?  Then blow away the buffer cache
2128          */
2129         spin_unlock_irqrestore(&ide_lock, flags);
2130         for (unit = 0; unit < MAX_DRIVES; ++unit) {
2131                 struct ata_device * drive = &ch->drives[unit];
2132
2133                 if (!drive->present)
2134                         continue;
2135
2136                 minor = drive->select.b.unit << PARTN_BITS;
2137                 for (p = 0; p < (1<<PARTN_BITS); ++p) {
2138                         if (drive->part[p].nr_sects > 0) {
2139                                 kdev_t devp = mk_kdev(ch->major, minor+p);
2140                                 invalidate_device(devp, 0);
2141                         }
2142                 }
2143         }
2144 #ifdef CONFIG_PROC_FS
2145         destroy_proc_ide_drives(ch);
2146 #endif
2147         spin_lock_irqsave(&ide_lock, flags);
2148
2149         hwif_unregister(ch);
2150
2151         /*
2152          * Remove us from the hwgroup
2153          */
2154
2155         hwgroup = ch->hwgroup;
2156         d = hwgroup->XXX_drive;
2157         for (i = 0; i < MAX_DRIVES; ++i) {
2158                 struct ata_device *drive = &ch->drives[i];
2159
2160                 if (drive->de) {
2161                         devfs_unregister (drive->de);
2162                         drive->de = NULL;
2163                 }
2164                 if (!drive->present)
2165                         continue;
2166
2167                 if (hwgroup->XXX_drive == drive)
2168                         hwgroup->XXX_drive = NULL;
2169
2170                 if (drive->id != NULL) {
2171                         kfree(drive->id);
2172                         drive->id = NULL;
2173                 }
2174                 drive->present = 0;
2175                 blk_cleanup_queue(&drive->queue);
2176         }
2177         if (d->present)
2178                 hwgroup->XXX_drive = d;
2179
2180         /* Free the hwgroup if we were the only member.
2181          */
2182         n = 0;
2183         for (i = 0; i < MAX_HWIFS; ++i) {
2184                 struct ata_channel *tmp = &ide_hwifs[i];
2185
2186                 if (!tmp->present)
2187                         continue;
2188
2189                 if (tmp->hwgroup == ch->hwgroup)
2190                         ++n;
2191         }
2192         if (n == 1)
2193                 kfree(ch->hwgroup);
2194
2195 #if defined(CONFIG_BLK_DEV_IDEDMA) && !defined(CONFIG_DMA_NONPCI)
2196         ide_release_dma(ch);
2197 #endif
2198
2199         /*
2200          * Remove us from the kernel's knowledge.
2201          */
2202         unregister_blkdev(ch->major, ch->name);
2203         kfree(blksize_size[ch->major]);
2204         blk_dev[ch->major].data = NULL;
2205         blk_dev[ch->major].queue = NULL;
2206         blk_clear(ch->major);
2207         gd = ch->gd;
2208         if (gd) {
2209                 del_gendisk(gd);
2210                 kfree(gd->sizes);
2211                 kfree(gd->part);
2212                 if (gd->de_arr)
2213                         kfree (gd->de_arr);
2214                 if (gd->flags)
2215                         kfree (gd->flags);
2216                 kfree(gd);
2217                 ch->gd = NULL;
2218         }
2219
2220         /*
2221          * Reinitialize the channel handler, but preserve any special methods for
2222          * it.
2223          */
2224
2225         old = *ch;
2226         init_hwif_data(ch, ch->index);
2227         ch->hwgroup = old.hwgroup;
2228         ch->tuneproc = old.tuneproc;
2229         ch->speedproc = old.speedproc;
2230         ch->selectproc = old.selectproc;
2231         ch->resetproc = old.resetproc;
2232         ch->intrproc = old.intrproc;
2233         ch->maskproc = old.maskproc;
2234         ch->quirkproc = old.quirkproc;
2235         ch->rwproc      = old.rwproc;
2236         ch->ata_read = old.ata_read;
2237         ch->ata_write = old.ata_write;
2238         ch->atapi_read = old.atapi_read;
2239         ch->atapi_write = old.atapi_write;
2240         ch->udma = old.udma;
2241         ch->busproc = old.busproc;
2242         ch->bus_state = old.bus_state;
2243         ch->dma_base = old.dma_base;
2244         ch->dma_extra = old.dma_extra;
2245         ch->config_data = old.config_data;
2246         ch->select_data = old.select_data;
2247         ch->proc = old.proc;
2248         /* FIXME: most propably this is always right:! */
2249 #ifndef CONFIG_BLK_DEV_IDECS
2250         ch->irq = old.irq;
2251 #endif
2252         ch->major = old.major;
2253         ch->chipset = old.chipset;
2254         ch->autodma = old.autodma;
2255         ch->udma_four = old.udma_four;
2256 #ifdef CONFIG_BLK_DEV_IDEPCI
2257         ch->pci_dev = old.pci_dev;
2258 #endif
2259         ch->straight8 = old.straight8;
2260
2261 abort:
2262         spin_unlock_irqrestore(&ide_lock, flags);
2263 }
2264
2265 /*
2266  * Setup hw_regs_t structure described by parameters.  You
2267  * may set up the hw structure yourself OR use this routine to
2268  * do it for you.
2269  */
2270 void ide_setup_ports (  hw_regs_t *hw,
2271                         ide_ioreg_t base, int *offsets,
2272                         ide_ioreg_t ctrl, ide_ioreg_t intr,
2273                         ide_ack_intr_t *ack_intr, int irq)
2274 {
2275         int i;
2276
2277         for (i = 0; i < IDE_NR_PORTS; i++) {
2278                 if (offsets[i] == -1) {
2279                         switch(i) {
2280                                 case IDE_CONTROL_OFFSET:
2281                                         hw->io_ports[i] = ctrl;
2282                                         break;
2283 #if defined(CONFIG_AMIGA) || defined(CONFIG_MAC)
2284                                 case IDE_IRQ_OFFSET:
2285                                         hw->io_ports[i] = intr;
2286                                         break;
2287 #endif
2288                                 default:
2289                                         hw->io_ports[i] = 0;
2290                                         break;
2291                         }
2292                 } else {
2293                         hw->io_ports[i] = base + offsets[i];
2294                 }
2295         }
2296         hw->irq = irq;
2297         hw->dma = NO_DMA;
2298         hw->ack_intr = ack_intr;
2299 }
2300
2301 /*
2302  * Register an IDE interface, specifing exactly the registers etc
2303  * Set init=1 iff calling before probes have taken place.
2304  */
2305 int ide_register_hw(hw_regs_t *hw, struct ata_channel **hwifp)
2306 {
2307         int h;
2308         int retry = 1;
2309         struct ata_channel *hwif;
2310
2311         do {
2312                 for (h = 0; h < MAX_HWIFS; ++h) {
2313                         hwif = &ide_hwifs[h];
2314                         if (hwif->hw.io_ports[IDE_DATA_OFFSET] == hw->io_ports[IDE_DATA_OFFSET])
2315                                 goto found;
2316                 }
2317                 for (h = 0; h < MAX_HWIFS; ++h) {
2318                         hwif = &ide_hwifs[h];
2319                         if ((!hwif->present && (hwif->unit == ATA_PRIMARY) && !initializing) ||
2320                             (!hwif->hw.io_ports[IDE_DATA_OFFSET] && initializing))
2321                                 goto found;
2322                 }
2323                 for (h = 0; h < MAX_HWIFS; ++h)
2324                         ide_unregister(&ide_hwifs[h]);
2325         } while (retry--);
2326
2327         return -1;
2328
2329 found:
2330         ide_unregister(hwif);
2331         if (hwif->present)
2332                 return -1;
2333         memcpy(&hwif->hw, hw, sizeof(*hw));
2334         memcpy(hwif->io_ports, hwif->hw.io_ports, sizeof(hwif->hw.io_ports));
2335         hwif->irq = hw->irq;
2336         hwif->noprobe = 0;
2337         hwif->chipset = hw->chipset;
2338
2339         if (!initializing) {
2340                 ideprobe_init();
2341                 revalidate_drives();
2342 #ifdef CONFIG_PROC_FS
2343                 create_proc_ide_interfaces();
2344 #endif
2345                 /* FIXME: Do we really have to call it second time here?! */
2346                 ide_driver_module();
2347         }
2348
2349         if (hwifp)
2350                 *hwifp = hwif;
2351
2352         return (initializing || hwif->present) ? h : -1;
2353 }
2354
2355 /*
2356  * Compatability function for existing drivers.  If you want
2357  * something different, use the function above.
2358  */
2359 int ide_register(int arg1, int arg2, int irq)
2360 {
2361         hw_regs_t hw;
2362         ide_init_hwif_ports(&hw, (ide_ioreg_t) arg1, (ide_ioreg_t) arg2, NULL);
2363         hw.irq = irq;
2364         return ide_register_hw(&hw, NULL);
2365 }
2366
2367 void ide_add_setting(ide_drive_t *drive, const char *name, int rw, int read_ioctl, int write_ioctl, int data_type, int min, int max, int mul_factor, int div_factor, void *data, ide_procset_t *set)
2368 {
2369         ide_settings_t **p = &drive->settings;
2370         ide_settings_t *setting = NULL;
2371
2372         while ((*p) && strcmp((*p)->name, name) < 0)
2373                 p = &((*p)->next);
2374         if ((setting = kmalloc(sizeof(*setting), GFP_KERNEL)) == NULL)
2375                 goto abort;
2376         memset(setting, 0, sizeof(*setting));
2377         if ((setting->name = kmalloc(strlen(name) + 1, GFP_KERNEL)) == NULL)
2378                 goto abort;
2379         strcpy(setting->name, name);            setting->rw = rw;
2380         setting->read_ioctl = read_ioctl;       setting->write_ioctl = write_ioctl;
2381         setting->data_type = data_type;         setting->min = min;
2382         setting->max = max;                     setting->mul_factor = mul_factor;
2383         setting->div_factor = div_factor;       setting->data = data;
2384         setting->set = set;                     setting->next = *p;
2385         if (drive->driver)
2386                 setting->auto_remove = 1;
2387         *p = setting;
2388         return;
2389 abort:
2390         if (setting)
2391                 kfree(setting);
2392 }
2393
2394 void ide_remove_setting (ide_drive_t *drive, char *name)
2395 {
2396         ide_settings_t **p = &drive->settings, *setting;
2397
2398         while ((*p) && strcmp((*p)->name, name))
2399                 p = &((*p)->next);
2400         if ((setting = (*p)) == NULL)
2401                 return;
2402         (*p) = setting->next;
2403         kfree(setting->name);
2404         kfree(setting);
2405 }
2406
2407 static void auto_remove_settings (ide_drive_t *drive)
2408 {
2409         ide_settings_t *setting;
2410 repeat:
2411         setting = drive->settings;
2412         while (setting) {
2413                 if (setting->auto_remove) {
2414                         ide_remove_setting(drive, setting->name);
2415                         goto repeat;
2416                 }
2417                 setting = setting->next;
2418         }
2419 }
2420
2421 int ide_read_setting (ide_drive_t *drive, ide_settings_t *setting)
2422 {
2423         int             val = -EINVAL;
2424         unsigned long   flags;
2425
2426         if ((setting->rw & SETTING_READ)) {
2427                 spin_lock_irqsave(&ide_lock, flags);
2428                 switch(setting->data_type) {
2429                         case TYPE_BYTE:
2430                                 val = *((u8 *) setting->data);
2431                                 break;
2432                         case TYPE_SHORT:
2433                                 val = *((u16 *) setting->data);
2434                                 break;
2435                         case TYPE_INT:
2436                         case TYPE_INTA:
2437                                 val = *((u32 *) setting->data);
2438                                 break;
2439                 }
2440                 spin_unlock_irqrestore(&ide_lock, flags);
2441         }
2442         return val;
2443 }
2444
2445 int ide_spin_wait_hwgroup (ide_drive_t *drive)
2446 {
2447         ide_hwgroup_t *hwgroup = HWGROUP(drive);
2448         unsigned long timeout = jiffies + (3 * HZ);
2449
2450         spin_lock_irq(&ide_lock);
2451
2452         while (test_bit(IDE_BUSY, &hwgroup->flags)) {
2453                 unsigned long lflags;
2454                 spin_unlock_irq(&ide_lock);
2455                 __save_flags(lflags);   /* local CPU only */
2456                 __sti();                /* local CPU only; needed for jiffies */
2457                 if (0 < (signed long)(jiffies - timeout)) {
2458                         __restore_flags(lflags);        /* local CPU only */
2459                         printk("%s: channel busy\n", drive->name);
2460                         return -EBUSY;
2461                 }
2462                 __restore_flags(lflags);        /* local CPU only */
2463                 spin_lock_irq(&ide_lock);
2464         }
2465         return 0;
2466 }
2467
2468 /*
2469  * FIXME:  This should be changed to enqueue a special request
2470  * to the driver to change settings, and then wait on a semaphore for completion.
2471  * The current scheme of polling is kludgey, though safe enough.
2472  */
2473 int ide_write_setting (ide_drive_t *drive, ide_settings_t *setting, int val)
2474 {
2475         int i;
2476         u32 *p;
2477
2478         if (!capable(CAP_SYS_ADMIN))
2479                 return -EACCES;
2480         if (!(setting->rw & SETTING_WRITE))
2481                 return -EPERM;
2482         if (val < setting->min || val > setting->max)
2483                 return -EINVAL;
2484         if (setting->set)
2485                 return setting->set(drive, val);
2486         if (ide_spin_wait_hwgroup(drive))
2487                 return -EBUSY;
2488         switch (setting->data_type) {
2489                 case TYPE_BYTE:
2490                         *((u8 *) setting->data) = val;
2491                         break;
2492                 case TYPE_SHORT:
2493                         *((u16 *) setting->data) = val;
2494                         break;
2495                 case TYPE_INT:
2496                         *((u32 *) setting->data) = val;
2497                         break;
2498                 case TYPE_INTA:
2499                         p = (u32 *) setting->data;
2500                         for (i = 0; i < 1 << PARTN_BITS; i++, p++)
2501                                 *p = val;
2502                         break;
2503         }
2504         spin_unlock_irq(&ide_lock);
2505         return 0;
2506 }
2507
2508 static int set_io_32bit(struct ata_device *drive, int arg)
2509 {
2510         if (drive->channel->no_io_32bit)
2511                 return -EIO;
2512
2513         drive->channel->io_32bit = arg;
2514
2515         return 0;
2516 }
2517
2518 static int set_using_dma(ide_drive_t *drive, int arg)
2519 {
2520         if (!drive->driver)
2521                 return -EPERM;
2522         if (!drive->id || !(drive->id->capability & 1) || !drive->channel->udma)
2523                 return -EPERM;
2524         if (drive->channel->udma(arg ? ide_dma_on : ide_dma_off, drive, NULL))
2525                 return -EIO;
2526         return 0;
2527 }
2528
2529 static int set_pio_mode(ide_drive_t *drive, int arg)
2530 {
2531         struct request rq;
2532
2533         if (!drive->channel->tuneproc)
2534                 return -ENOSYS;
2535
2536         if (drive->special_cmd & ATA_SPECIAL_TUNE)
2537                 return -EBUSY;
2538
2539         ide_init_drive_cmd(&rq);
2540         drive->tune_req = (u8) arg;
2541         drive->special_cmd |= ATA_SPECIAL_TUNE;
2542         ide_do_drive_cmd(drive, &rq, ide_wait);
2543
2544         return 0;
2545 }
2546
2547 void ide_add_generic_settings (ide_drive_t *drive)
2548 {
2549 /*                      drive   setting name            read/write access                               read ioctl              write ioctl             data type       min     max                             mul_factor      div_factor      data pointer                    set function */
2550         ide_add_setting(drive,  "io_32bit",             drive->channel->no_io_32bit ? SETTING_READ : SETTING_RW,        HDIO_GET_32BIT,         HDIO_SET_32BIT,         TYPE_BYTE,      0,      1 + (SUPPORT_VLB_SYNC << 1),    1,              1,              &drive->channel->io_32bit,              set_io_32bit);
2551         ide_add_setting(drive,  "pio_mode",             SETTING_WRITE,                                  -1,                     HDIO_SET_PIO_MODE,      TYPE_BYTE,      0,      255,                            1,              1,              NULL,                           set_pio_mode);
2552         ide_add_setting(drive,  "slow",                 SETTING_RW,                                     -1,                     -1,                     TYPE_BYTE,      0,      1,                              1,              1,              &drive->channel->slow,                  NULL);
2553         ide_add_setting(drive,  "unmaskirq",            drive->channel->no_unmask ? SETTING_READ : SETTING_RW,  HDIO_GET_UNMASKINTR,    HDIO_SET_UNMASKINTR,    TYPE_BYTE,      0,      1,                              1,              1,              &drive->channel->unmask,                        NULL);
2554         ide_add_setting(drive,  "using_dma",            SETTING_RW,                                     HDIO_GET_DMA,           HDIO_SET_DMA,           TYPE_BYTE,      0,      1,                              1,              1,              &drive->using_dma,              set_using_dma);
2555         ide_add_setting(drive,  "ide_scsi",             SETTING_RW,                                     -1,                     -1,                     TYPE_BYTE,      0,      1,                              1,              1,              &drive->scsi,                   NULL);
2556         ide_add_setting(drive,  "init_speed",           SETTING_RW,                                     -1,                     -1,                     TYPE_BYTE,      0,      69,                             1,              1,              &drive->init_speed,             NULL);
2557         ide_add_setting(drive,  "current_speed",        SETTING_RW,                                     -1,                     -1,                     TYPE_BYTE,      0,      69,                             1,              1,              &drive->current_speed,          NULL);
2558         ide_add_setting(drive,  "number",               SETTING_RW,                                     -1,                     -1,                     TYPE_BYTE,      0,      3,                              1,              1,              &drive->dn,                     NULL);
2559 }
2560
2561 /*
2562  * Delay for *at least* 50ms.  As we don't know how much time is left
2563  * until the next tick occurs, we wait an extra tick to be safe.
2564  * This is used only during the probing/polling for drives at boot time.
2565  *
2566  * However, its usefullness may be needed in other places, thus we export it now.
2567  * The future may change this to a millisecond setable delay.
2568  */
2569 void ide_delay_50ms (void)
2570 {
2571 #ifndef CONFIG_BLK_DEV_IDECS
2572         mdelay(50);
2573 #else
2574         __set_current_state(TASK_UNINTERRUPTIBLE);
2575         schedule_timeout(HZ/20);
2576 #endif /* CONFIG_BLK_DEV_IDECS */
2577 }
2578
2579 static int ide_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
2580 {
2581         int err = 0, major, minor;
2582         ide_drive_t *drive;
2583         struct request rq;
2584         kdev_t dev;
2585         ide_settings_t *setting;
2586
2587         dev = inode->i_rdev;
2588         major = major(dev); minor = minor(dev);
2589         if ((drive = get_info_ptr(inode->i_rdev)) == NULL)
2590                 return -ENODEV;
2591
2592         /* Find setting by ioctl */
2593
2594         setting = drive->settings;
2595
2596         while (setting) {
2597                 if (setting->read_ioctl == cmd || setting->write_ioctl == cmd)
2598                         break;
2599                 setting = setting->next;
2600         }
2601
2602         if (setting != NULL) {
2603                 if (cmd == setting->read_ioctl) {
2604                         err = ide_read_setting(drive, setting);
2605                         return err >= 0 ? put_user(err, (long *) arg) : err;
2606                 } else {
2607                         if ((minor(inode->i_rdev) & PARTN_MASK))
2608                                 return -EINVAL;
2609                         return ide_write_setting(drive, setting, arg);
2610                 }
2611         }
2612
2613         ide_init_drive_cmd(&rq);
2614         switch (cmd) {
2615                 case HDIO_GETGEO:
2616                 {
2617                         struct hd_geometry *loc = (struct hd_geometry *) arg;
2618                         unsigned short bios_cyl = drive->bios_cyl; /* truncate */
2619
2620                         if (!loc || (drive->type != ATA_DISK && drive->type != ATA_FLOPPY))
2621                                 return -EINVAL;
2622                         if (put_user(drive->bios_head, (byte *) &loc->heads)) return -EFAULT;
2623                         if (put_user(drive->bios_sect, (byte *) &loc->sectors)) return -EFAULT;
2624                         if (put_user(bios_cyl, (unsigned short *) &loc->cylinders)) return -EFAULT;
2625                         if (put_user((unsigned)drive->part[minor(inode->i_rdev)&PARTN_MASK].start_sect,
2626                                 (unsigned long *) &loc->start)) return -EFAULT;
2627                         return 0;
2628                 }
2629
2630                 case HDIO_GETGEO_BIG:
2631                 {
2632                         struct hd_big_geometry *loc = (struct hd_big_geometry *) arg;
2633
2634                         if (!loc || (drive->type != ATA_DISK && drive->type != ATA_FLOPPY))
2635                                 return -EINVAL;
2636
2637                         if (put_user(drive->bios_head, (byte *) &loc->heads)) return -EFAULT;
2638                         if (put_user(drive->bios_sect, (byte *) &loc->sectors)) return -EFAULT;
2639                         if (put_user(drive->bios_cyl, (unsigned int *) &loc->cylinders)) return -EFAULT;
2640                         if (put_user((unsigned)drive->part[minor(inode->i_rdev)&PARTN_MASK].start_sect,
2641                                 (unsigned long *) &loc->start)) return -EFAULT;
2642                         return 0;
2643                 }
2644
2645                 case HDIO_GETGEO_BIG_RAW:
2646                 {
2647                         struct hd_big_geometry *loc = (struct hd_big_geometry *) arg;
2648                         if (!loc || (drive->type != ATA_DISK && drive->type != ATA_FLOPPY))
2649                                 return -EINVAL;
2650                         if (put_user(drive->head, (byte *) &loc->heads)) return -EFAULT;
2651                         if (put_user(drive->sect, (byte *) &loc->sectors)) return -EFAULT;
2652                         if (put_user(drive->cyl, (unsigned int *) &loc->cylinders)) return -EFAULT;
2653                         if (put_user((unsigned)drive->part[minor(inode->i_rdev)&PARTN_MASK].start_sect,
2654                                 (unsigned long *) &loc->start)) return -EFAULT;
2655                         return 0;
2656                 }
2657
2658                 case BLKRRPART: /* Re-read partition tables */
2659                         if (!capable(CAP_SYS_ADMIN))
2660                                 return -EACCES;
2661                         return ide_revalidate_disk(inode->i_rdev);
2662
2663                 case HDIO_GET_IDENTITY:
2664                         if (minor(inode->i_rdev) & PARTN_MASK)
2665                                 return -EINVAL;
2666                         if (drive->id == NULL)
2667                                 return -ENOMSG;
2668                         if (copy_to_user((char *)arg, (char *)drive->id, sizeof(*drive->id)))
2669                                 return -EFAULT;
2670                         return 0;
2671
2672                 case HDIO_GET_NICE:
2673                         return put_user(drive->dsc_overlap      <<      IDE_NICE_DSC_OVERLAP    |
2674                                         drive->atapi_overlap    <<      IDE_NICE_ATAPI_OVERLAP,
2675                                         (long *) arg);
2676
2677                 case HDIO_DRIVE_CMD:
2678                         if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2679                                 return -EACCES;
2680                         return ide_cmd_ioctl(drive, arg);
2681
2682                 case HDIO_SET_NICE:
2683                         if (!capable(CAP_SYS_ADMIN)) return -EACCES;
2684                         if (arg != (arg & ((1 << IDE_NICE_DSC_OVERLAP))))
2685                                 return -EPERM;
2686                         drive->dsc_overlap = (arg >> IDE_NICE_DSC_OVERLAP) & 1;
2687                         /* Only CD-ROM's and tapes support DSC overlap. */
2688                         if (drive->dsc_overlap && !(drive->type == ATA_ROM || drive->type == ATA_TAPE)) {
2689                                 drive->dsc_overlap = 0;
2690                                 return -EPERM;
2691                         }
2692                         return 0;
2693                 case BLKGETSIZE:
2694                 case BLKGETSIZE64:
2695                 case BLKROSET:
2696                 case BLKROGET:
2697                 case BLKFLSBUF:
2698                 case BLKSSZGET:
2699                 case BLKPG:
2700                 case BLKELVGET:
2701                 case BLKELVSET:
2702                 case BLKBSZGET:
2703                 case BLKBSZSET:
2704                         return blk_ioctl(inode->i_bdev, cmd, arg);
2705
2706                 /*
2707                  * uniform packet command handling
2708                  */
2709                 case CDROMEJECT:
2710                 case CDROMCLOSETRAY:
2711                         return block_ioctl(inode->i_bdev, cmd, arg);
2712
2713                 case HDIO_GET_BUSSTATE:
2714                         if (!capable(CAP_SYS_ADMIN))
2715                                 return -EACCES;
2716                         if (put_user(drive->channel->bus_state, (long *)arg))
2717                                 return -EFAULT;
2718                         return 0;
2719
2720                 case HDIO_SET_BUSSTATE:
2721                         if (!capable(CAP_SYS_ADMIN))
2722                                 return -EACCES;
2723                         if (drive->channel->busproc)
2724                                 drive->channel->busproc(drive, (int)arg);
2725                         return 0;
2726
2727                 default:
2728                         if (ata_ops(drive) && ata_ops(drive)->ioctl)
2729                                 return ata_ops(drive)->ioctl(drive, inode, file, cmd, arg);
2730                         return -EINVAL;
2731         }
2732 }
2733
2734 static int ide_check_media_change (kdev_t i_rdev)
2735 {
2736         ide_drive_t *drive;
2737         int res = 0; /* not changed */
2738
2739         drive = get_info_ptr(i_rdev);
2740         if (!drive)
2741                 return -ENODEV;
2742
2743         if (ata_ops(drive)) {
2744                 ata_get(ata_ops(drive));
2745                 if (ata_ops(drive)->check_media_change)
2746                         res = ata_ops(drive)->check_media_change(drive);
2747                 else
2748                         res = 1; /* assume it was changed */
2749                 ata_put(ata_ops(drive));
2750         }
2751         return res;
2752 }
2753
2754 void ide_fixstring (byte *s, const int bytecount, const int byteswap)
2755 {
2756         byte *p = s, *end = &s[bytecount & ~1]; /* bytecount must be even */
2757
2758         if (byteswap) {
2759                 /* convert from big-endian to host byte order */
2760                 for (p = end ; p != s;) {
2761                         unsigned short *pp = (unsigned short *) (p -= 2);
2762                         *pp = ntohs(*pp);
2763                 }
2764         }
2765
2766         /* strip leading blanks */
2767         while (s != end && *s == ' ')
2768                 ++s;
2769
2770         /* compress internal blanks and strip trailing blanks */
2771         while (s != end && *s) {
2772                 if (*s++ != ' ' || (s != end && *s && *s != ' '))
2773                         *p++ = *(s-1);
2774         }
2775
2776         /* wipe out trailing garbage */
2777         while (p != end)
2778                 *p++ = '\0';
2779 }
2780
2781 /****************************************************************************
2782  * FIXME: rewrite the following crap:
2783  */
2784
2785 /*
2786  * stridx() returns the offset of c within s,
2787  * or -1 if c is '\0' or not found within s.
2788  */
2789 static int __init stridx (const char *s, char c)
2790 {
2791         char *i = strchr(s, c);
2792
2793         return (i && c) ? i - s : -1;
2794 }
2795
2796 /*
2797  * Parsing for ide_setup():
2798  *
2799  * 1. the first char of s must be '='.
2800  * 2. if the remainder matches one of the supplied keywords,
2801  *     the index (1 based) of the keyword is negated and returned.
2802  * 3. if the remainder is a series of no more than max_vals numbers
2803  *     separated by commas, the numbers are saved in vals[] and a
2804  *     count of how many were saved is returned.  Base10 is assumed,
2805  *     and base16 is allowed when prefixed with "0x".
2806  * 4. otherwise, zero is returned.
2807  */
2808 static int __init match_parm (char *s, const char *keywords[], int vals[], int max_vals)
2809 {
2810         static const char *decimal = "0123456789";
2811         static const char *hex = "0123456789abcdef";
2812         int i, n;
2813
2814         if (*s++ == '=') {
2815                 /*
2816                  * Try matching against the supplied keywords,
2817                  * and return -(index+1) if we match one
2818                  */
2819                 if (keywords != NULL) {
2820                         for (i = 0; *keywords != NULL; ++i) {
2821                                 if (!strcmp(s, *keywords++))
2822                                         return -(i+1);
2823                         }
2824                 }
2825                 /*
2826                  * Look for a series of no more than "max_vals"
2827                  * numeric values separated by commas, in base10,
2828                  * or base16 when prefixed with "0x".
2829                  * Return a count of how many were found.
2830                  */
2831                 for (n = 0; (i = stridx(decimal, *s)) >= 0;) {
2832                         vals[n] = i;
2833                         while ((i = stridx(decimal, *++s)) >= 0)
2834                                 vals[n] = (vals[n] * 10) + i;
2835                         if (*s == 'x' && !vals[n]) {
2836                                 while ((i = stridx(hex, *++s)) >= 0)
2837                                         vals[n] = (vals[n] * 0x10) + i;
2838                         }
2839                         if (++n == max_vals)
2840                                 break;
2841                         if (*s == ',' || *s == ';')
2842                                 ++s;
2843                 }
2844                 if (!*s)
2845                         return n;
2846         }
2847         return 0;       /* zero = nothing matched */
2848 }
2849
2850 /*
2851  * This gets called VERY EARLY during initialization, to handle kernel "command
2852  * line" strings beginning with "hdx=" or "ide".It gets called even before the
2853  * actual module gets initialized.
2854  *
2855  * Here is the complete set currently supported comand line options:
2856  *
2857  * "hdx="  is recognized for all "x" from "a" to "h", such as "hdc".
2858  * "idex=" is recognized for all "x" from "0" to "3", such as "ide1".
2859  *
2860  * "hdx=noprobe"        : drive may be present, but do not probe for it
2861  * "hdx=none"           : drive is NOT present, ignore cmos and do not probe
2862  * "hdx=nowerr"         : ignore the WRERR_STAT bit on this drive
2863  * "hdx=cdrom"          : drive is present, and is a cdrom drive
2864  * "hdx=cyl,head,sect"  : disk drive is present, with specified geometry
2865  * "hdx=noremap"        : do not remap 0->1 even though EZD was detected
2866  * "hdx=autotune"       : driver will attempt to tune interface speed
2867  *                              to the fastest PIO mode supported,
2868  *                              if possible for this drive only.
2869  *                              Not fully supported by all chipset types,
2870  *                              and quite likely to cause trouble with
2871  *                              older/odd IDE drives.
2872  *
2873  * "hdx=slow"           : insert a huge pause after each access to the data
2874  *                              port. Should be used only as a last resort.
2875  *
2876  * "hdxlun=xx"          : set the drive last logical unit.
2877  * "hdx=flash"          : allows for more than one ata_flash disk to be
2878  *                              registered. In most cases, only one device
2879  *                              will be present.
2880  * "hdx=scsi"           : the return of the ide-scsi flag, this is useful for
2881  *                              allowwing ide-floppy, ide-tape, and ide-cdrom|writers
2882  *                              to use ide-scsi emulation on a device specific option.
2883  * "idebus=xx"          : inform IDE driver of VESA/PCI bus speed in MHz,
2884  *                              where "xx" is between 20 and 66 inclusive,
2885  *                              used when tuning chipset PIO modes.
2886  *                              For PCI bus, 25 is correct for a P75 system,
2887  *                              30 is correct for P90,P120,P180 systems,
2888  *                              and 33 is used for P100,P133,P166 systems.
2889  *                              If in doubt, use idebus=33 for PCI.
2890  *                              As for VLB, it is safest to not specify it.
2891  *
2892  * "idex=noprobe"       : do not attempt to access/use this interface
2893  * "idex=base"          : probe for an interface at the address specified,
2894  *                              where "base" is usually 0x1f0 or 0x170
2895  *                              and "ctl" is assumed to be "base"+0x206
2896  * "idex=base,ctl"      : specify both base and ctl
2897  * "idex=base,ctl,irq"  : specify base, ctl, and irq number
2898  * "idex=autotune"      : driver will attempt to tune interface speed
2899  *                              to the fastest PIO mode supported,
2900  *                              for all drives on this interface.
2901  *                              Not fully supported by all chipset types,
2902  *                              and quite likely to cause trouble with
2903  *                              older/odd IDE drives.
2904  * "idex=noautotune"    : driver will NOT attempt to tune interface speed
2905  *                              This is the default for most chipsets,
2906  *                              except the cmd640.
2907  * "idex=serialize"     : do not overlap operations on idex and ide(x^1)
2908  * "idex=four"          : four drives on idex and ide(x^1) share same ports
2909  * "idex=reset"         : reset interface before first use
2910  * "idex=dma"           : enable DMA by default on both drives if possible
2911  * "idex=ata66"         : informs the interface that it has an 80c cable
2912  *                              for chipsets that are ATA-66 capable, but
2913  *                              the ablity to bit test for detection is
2914  *                              currently unknown.
2915  * "ide=reverse"        : Formerly called to pci sub-system, but now local.
2916  *
2917  * The following are valid ONLY on ide0, (except dc4030)
2918  * and the defaults for the base,ctl ports must not be altered.
2919  *
2920  * "ide0=dtc2278"       : probe/support DTC2278 interface
2921  * "ide0=ht6560b"       : probe/support HT6560B interface
2922  * "ide0=cmd640_vlb"    : *REQUIRED* for VLB cards with the CMD640 chip
2923  *                        (not for PCI -- automatically detected)
2924  * "ide0=qd65xx"        : probe/support qd65xx interface
2925  * "ide0=ali14xx"       : probe/support ali14xx chipsets (ALI M1439, M1443, M1445)
2926  * "ide0=umc8672"       : probe/support umc8672 chipsets
2927  * "idex=dc4030"        : probe/support Promise DC4030VL interface
2928  * "ide=doubler"        : probe/support IDE doublers on Amiga
2929  */
2930 int __init ide_setup (char *s)
2931 {
2932         int i, vals[3];
2933         struct ata_channel *hwif;
2934         ide_drive_t *drive;
2935         unsigned int hw, unit;
2936         const char max_drive = 'a' + ((MAX_HWIFS * MAX_DRIVES) - 1);
2937         const char max_hwif  = '0' + (MAX_HWIFS - 1);
2938
2939         if (!strncmp(s, "hd=", 3))      /* hd= is for hd.c driver and not us */
2940                 return 0;
2941
2942         if (strncmp(s,"ide",3) &&
2943             strncmp(s,"idebus",6) &&
2944             strncmp(s,"hd",2))          /* hdx= & hdxlun= */
2945                 return 0;
2946
2947         printk("ide_setup: %s", s);
2948         init_ide_data ();
2949
2950 #ifdef CONFIG_BLK_DEV_IDEDOUBLER
2951         if (!strcmp(s, "ide=doubler")) {
2952                 extern int ide_doubler;
2953
2954                 printk(" : Enabled support for IDE doublers\n");
2955                 ide_doubler = 1;
2956
2957                 return 1;
2958         }
2959 #endif
2960
2961         if (!strcmp(s, "ide=nodma")) {
2962                 printk("IDE: Prevented DMA\n");
2963                 noautodma = 1;
2964
2965                 return 1;
2966         }
2967
2968 #ifdef CONFIG_BLK_DEV_IDEPCI
2969         if (!strcmp(s, "ide=reverse")) {
2970                 ide_scan_direction = 1;
2971                 printk(" : Enabled support for IDE inverse scan order.\n");
2972
2973                 return 1;
2974         }
2975 #endif
2976
2977         /*
2978          * Look for drive options:  "hdx="
2979          */
2980         if (!strncmp(s, "hd", 2) && s[2] >= 'a' && s[2] <= max_drive) {
2981                 const char *hd_words[] = {"none", "noprobe", "nowerr", "cdrom",
2982                                 "serialize", "autotune", "noautotune",
2983                                 "slow", "flash", "remap", "noremap", "scsi", NULL};
2984                 unit = s[2] - 'a';
2985                 hw   = unit / MAX_DRIVES;
2986                 unit = unit % MAX_DRIVES;
2987                 hwif = &ide_hwifs[hw];
2988                 drive = &hwif->drives[unit];
2989                 if (!strncmp(s + 4, "ide-", 4)) {
2990                         strncpy(drive->driver_req, s + 4, 9);
2991                         goto done;
2992                 }
2993                 /*
2994                  * Look for last lun option:  "hdxlun="
2995                  */
2996                 if (!strncmp(&s[3], "lun", 3)) {
2997                         if (match_parm(&s[6], NULL, vals, 1) != 1)
2998                                 goto bad_option;
2999                         if (vals[0] >= 0 && vals[0] <= 7) {
3000                                 drive->last_lun = vals[0];
3001                                 drive->forced_lun = 1;
3002                         } else
3003                                 printk(" -- BAD LAST LUN! Expected value from 0 to 7");
3004                         goto done;
3005                 }
3006                 switch (match_parm(&s[3], hd_words, vals, 3)) {
3007                         case -1: /* "none" */
3008                                 drive->nobios = 1;  /* drop into "noprobe" */
3009                         case -2: /* "noprobe" */
3010                                 drive->noprobe = 1;
3011                                 goto done;
3012                         case -3: /* "nowerr" */
3013                                 drive->bad_wstat = BAD_R_STAT;
3014                                 hwif->noprobe = 0;
3015                                 goto done;
3016                         case -4: /* "cdrom" */
3017                                 drive->present = 1;
3018                                 drive->type = ATA_ROM;
3019                                 hwif->noprobe = 0;
3020                                 goto done;
3021                         case -5: /* "serialize" */
3022                                 printk(" -- USE \"ide%d=serialize\" INSTEAD", hw);
3023