upgrade to linux 2.6.10-1.12_FC2
[linux-2.6.git] / drivers / ide / ide-probe.c
1 /*
2  *  linux/drivers/ide/ide-probe.c       Version 1.11    Mar 05, 2003
3  *
4  *  Copyright (C) 1994-1998  Linus Torvalds & authors (see below)
5  */
6
7 /*
8  *  Mostly written by Mark Lord <mlord@pobox.com>
9  *                and Gadi Oxman <gadio@netvision.net.il>
10  *                and Andre Hedrick <andre@linux-ide.org>
11  *
12  *  See linux/MAINTAINERS for address of current maintainer.
13  *
14  * This is the IDE probe module, as evolved from hd.c and ide.c.
15  *
16  * Version 1.00         move drive probing code from ide.c to ide-probe.c
17  * Version 1.01         fix compilation problem for m68k
18  * Version 1.02         increase WAIT_PIDENTIFY to avoid CD-ROM locking at boot
19  *                       by Andrea Arcangeli
20  * Version 1.03         fix for (hwif->chipset == ide_4drives)
21  * Version 1.04         fixed buggy treatments of known flash memory cards
22  *
23  * Version 1.05         fix for (hwif->chipset == ide_pdc4030)
24  *                      added ide6/7/8/9
25  *                      allowed for secondary flash card to be detectable
26  *                       with new flag : drive->ata_flash : 1;
27  * Version 1.06         stream line request queue and prep for cascade project.
28  * Version 1.07         max_sect <= 255; slower disks would get behind and
29  *                      then fall over when they get to 256.    Paul G.
30  * Version 1.10         Update set for new IDE. drive->id is now always
31  *                      valid after probe time even with noprobe
32  */
33
34 #undef REALLY_SLOW_IO           /* most systems can safely undef this */
35
36 #include <linux/config.h>
37 #include <linux/module.h>
38 #include <linux/types.h>
39 #include <linux/string.h>
40 #include <linux/kernel.h>
41 #include <linux/timer.h>
42 #include <linux/mm.h>
43 #include <linux/interrupt.h>
44 #include <linux/major.h>
45 #include <linux/errno.h>
46 #include <linux/genhd.h>
47 #include <linux/slab.h>
48 #include <linux/delay.h>
49 #include <linux/ide.h>
50 #include <linux/spinlock.h>
51 #include <linux/kmod.h>
52 #include <linux/pci.h>
53
54 #include <asm/byteorder.h>
55 #include <asm/irq.h>
56 #include <asm/uaccess.h>
57 #include <asm/io.h>
58
59 /**
60  *      generic_id              -       add a generic drive id
61  *      @drive: drive to make an ID block for
62  *      
63  *      Add a fake id field to the drive we are passed. This allows
64  *      use to skip a ton of NULL checks (which people always miss) 
65  *      and make drive properties unconditional outside of this file
66  */
67  
68 static void generic_id(ide_drive_t *drive)
69 {
70         drive->id->cyls = drive->cyl;
71         drive->id->heads = drive->head;
72         drive->id->sectors = drive->sect;
73         drive->id->cur_cyls = drive->cyl;
74         drive->id->cur_heads = drive->head;
75         drive->id->cur_sectors = drive->sect;
76 }
77                 
78 /**
79  *      drive_is_flashcard      -       check for compact flash
80  *      @drive: drive to check
81  *
82  *      CompactFlash cards and their brethern pretend to be removable
83  *      hard disks, except:
84  *              (1) they never have a slave unit, and
85  *              (2) they don't have doorlock mechanisms.
86  *      This test catches them, and is invoked elsewhere when setting
87  *      appropriate config bits.
88  *
89  *      FIXME: This treatment is probably applicable for *all* PCMCIA (PC CARD)
90  *      devices, so in linux 2.3.x we should change this to just treat all
91  *      PCMCIA  drives this way, and get rid of the model-name tests below
92  *      (too big of an interface change for 2.4.x).
93  *      At that time, we might also consider parameterizing the timeouts and
94  *      retries, since these are MUCH faster than mechanical drives. -M.Lord
95  */
96  
97 static inline int drive_is_flashcard (ide_drive_t *drive)
98 {
99         struct hd_driveid *id = drive->id;
100
101         if (drive->removable) {
102                 if (id->config == 0x848a) return 1;     /* CompactFlash */
103                 if (!strncmp(id->model, "KODAK ATA_FLASH", 15)  /* Kodak */
104                  || !strncmp(id->model, "Hitachi CV", 10)       /* Hitachi */
105                  || !strncmp(id->model, "SunDisk SDCFB", 13)    /* old SanDisk */
106                  || !strncmp(id->model, "SanDisk SDCFB", 13)    /* SanDisk */
107                  || !strncmp(id->model, "HAGIWARA HPC", 12)     /* Hagiwara */
108                  || !strncmp(id->model, "LEXAR ATA_FLASH", 15)  /* Lexar */
109                  || !strncmp(id->model, "ATA_FLASH", 9))        /* Simple Tech */
110                 {
111                         return 1;       /* yes, it is a flash memory card */
112                 }
113         }
114         return 0;       /* no, it is not a flash memory card */
115 }
116
117 /**
118  *      do_identify     -       identify a drive
119  *      @drive: drive to identify 
120  *      @cmd: command used
121  *
122  *      Called when we have issued a drive identify command to
123  *      read and parse the results. This function is run with
124  *      interrupts disabled. 
125  */
126  
127 static inline void do_identify (ide_drive_t *drive, u8 cmd)
128 {
129         ide_hwif_t *hwif = HWIF(drive);
130         int bswap = 1;
131         struct hd_driveid *id;
132
133         id = drive->id;
134         /* read 512 bytes of id info */
135         hwif->ata_input_data(drive, id, SECTOR_WORDS);
136
137         drive->id_read = 1;
138         local_irq_enable();
139         ide_fix_driveid(id);
140
141 #if defined (CONFIG_SCSI_EATA_DMA) || defined (CONFIG_SCSI_EATA_PIO) || defined (CONFIG_SCSI_EATA)
142         /*
143          * EATA SCSI controllers do a hardware ATA emulation:
144          * Ignore them if there is a driver for them available.
145          */
146         if ((id->model[0] == 'P' && id->model[1] == 'M') ||
147             (id->model[0] == 'S' && id->model[1] == 'K')) {
148                 printk("%s: EATA SCSI HBA %.10s\n", drive->name, id->model);
149                 goto err_misc;
150         }
151 #endif /* CONFIG_SCSI_EATA_DMA || CONFIG_SCSI_EATA_PIO */
152
153         /*
154          *  WIN_IDENTIFY returns little-endian info,
155          *  WIN_PIDENTIFY *usually* returns little-endian info.
156          */
157         if (cmd == WIN_PIDENTIFY) {
158                 if ((id->model[0] == 'N' && id->model[1] == 'E') /* NEC */
159                  || (id->model[0] == 'F' && id->model[1] == 'X') /* Mitsumi */
160                  || (id->model[0] == 'P' && id->model[1] == 'i'))/* Pioneer */
161                         /* Vertos drives may still be weird */
162                         bswap ^= 1;     
163         }
164         ide_fixstring(id->model,     sizeof(id->model),     bswap);
165         ide_fixstring(id->fw_rev,    sizeof(id->fw_rev),    bswap);
166         ide_fixstring(id->serial_no, sizeof(id->serial_no), bswap);
167
168         if (strstr(id->model, "E X A B Y T E N E S T"))
169                 goto err_misc;
170
171         /* we depend on this a lot! */
172         id->model[sizeof(id->model)-1] = '\0';
173         printk("%s: %s, ", drive->name, id->model);
174         drive->present = 1;
175         drive->dead = 0;
176
177         /*
178          * Check for an ATAPI device
179          */
180         if (cmd == WIN_PIDENTIFY) {
181                 u8 type = (id->config >> 8) & 0x1f;
182                 printk("ATAPI ");
183                 switch (type) {
184                         case ide_floppy:
185                                 if (!strstr(id->model, "CD-ROM")) {
186                                         if (!strstr(id->model, "oppy") &&
187                                             !strstr(id->model, "poyp") &&
188                                             !strstr(id->model, "ZIP"))
189                                                 printk("cdrom or floppy?, assuming ");
190                                         if (drive->media != ide_cdrom) {
191                                                 printk ("FLOPPY");
192                                                 drive->removable = 1;
193                                                 break;
194                                         }
195                                 }
196                                 /* Early cdrom models used zero */
197                                 type = ide_cdrom;
198                         case ide_cdrom:
199                                 drive->removable = 1;
200 #ifdef CONFIG_PPC
201                                 /* kludge for Apple PowerBook internal zip */
202                                 if (!strstr(id->model, "CD-ROM") &&
203                                     strstr(id->model, "ZIP")) {
204                                         printk ("FLOPPY");
205                                         type = ide_floppy;
206                                         break;
207                                 }
208 #endif
209                                 printk ("CD/DVD-ROM");
210                                 break;
211                         case ide_tape:
212                                 printk ("TAPE");
213                                 break;
214                         case ide_optical:
215                                 printk ("OPTICAL");
216                                 drive->removable = 1;
217                                 break;
218                         default:
219                                 printk("UNKNOWN (type %d)", type);
220                                 break;
221                 }
222                 printk (" drive\n");
223                 drive->media = type;
224                 return;
225         }
226
227         /*
228          * Not an ATAPI device: looks like a "regular" hard disk
229          */
230         if (id->config & (1<<7))
231                 drive->removable = 1;
232
233         if (drive_is_flashcard(drive))
234                 drive->is_flash = 1;
235         drive->media = ide_disk;
236         printk("%s DISK drive\n", (drive->is_flash) ? "CFA" : "ATA" );
237         QUIRK_LIST(drive);
238         return;
239
240 err_misc:
241         kfree(id);
242         drive->present = 0;
243         return;
244 }
245
246 /**
247  *      actual_try_to_identify  -       send ata/atapi identify
248  *      @drive: drive to identify
249  *      @cmd: command to use
250  *
251  *      try_to_identify() sends an ATA(PI) IDENTIFY request to a drive
252  *      and waits for a response.  It also monitors irqs while this is
253  *      happening, in hope of automatically determining which one is
254  *      being used by the interface.
255  *
256  *      Returns:        0  device was identified
257  *                      1  device timed-out (no response to identify request)
258  *                      2  device aborted the command (refused to identify itself)
259  */
260
261 static int actual_try_to_identify (ide_drive_t *drive, u8 cmd)
262 {
263         ide_hwif_t *hwif = HWIF(drive);
264         int rc;
265         unsigned long hd_status;
266         unsigned long timeout;
267         u8 s = 0, a = 0;
268
269         /* take a deep breath */
270         msleep(50);
271
272         if (IDE_CONTROL_REG) {
273                 a = hwif->INB(IDE_ALTSTATUS_REG);
274                 s = hwif->INB(IDE_STATUS_REG);
275                 if ((a ^ s) & ~INDEX_STAT) {
276                         printk(KERN_INFO "%s: probing with STATUS(0x%02x) instead of "
277                                 "ALTSTATUS(0x%02x)\n", drive->name, s, a);
278                         /* ancient Seagate drives, broken interfaces */
279                         hd_status = IDE_STATUS_REG;
280                 } else {
281                         /* use non-intrusive polling */
282                         hd_status = IDE_ALTSTATUS_REG;
283                 }
284         } else
285                 hd_status = IDE_STATUS_REG;
286
287         /* set features register for atapi
288          * identify command to be sure of reply
289          */
290         if ((cmd == WIN_PIDENTIFY))
291                 /* disable dma & overlap */
292                 hwif->OUTB(0, IDE_FEATURE_REG);
293
294         /* ask drive for ID */
295         hwif->OUTB(cmd, IDE_COMMAND_REG);
296
297         timeout = ((cmd == WIN_IDENTIFY) ? WAIT_WORSTCASE : WAIT_PIDENTIFY) / 2;
298         timeout += jiffies;
299         do {
300                 if (time_after(jiffies, timeout)) {
301                         /* drive timed-out */
302                         return 1;
303                 }
304                 /* give drive a breather */
305                 msleep(50);
306         } while ((hwif->INB(hd_status)) & BUSY_STAT);
307
308         /* wait for IRQ and DRQ_STAT */
309         msleep(50);
310         if (OK_STAT((hwif->INB(IDE_STATUS_REG)), DRQ_STAT, BAD_R_STAT)) {
311                 unsigned long flags;
312
313                 /* local CPU only; some systems need this */
314                 local_irq_save(flags);
315                 /* drive returned ID */
316                 do_identify(drive, cmd);
317                 /* drive responded with ID */
318                 rc = 0;
319                 /* clear drive IRQ */
320                 (void) hwif->INB(IDE_STATUS_REG);
321                 local_irq_restore(flags);
322         } else {
323                 /* drive refused ID */
324                 rc = 2;
325         }
326         return rc;
327 }
328
329 /**
330  *      try_to_identify -       try to identify a drive
331  *      @drive: drive to probe
332  *      @cmd: command to use
333  *
334  *      Issue the identify command and then do IRQ probing to
335  *      complete the identification when needed by finding the
336  *      IRQ the drive is attached to
337  */
338  
339 static int try_to_identify (ide_drive_t *drive, u8 cmd)
340 {
341         ide_hwif_t *hwif = HWIF(drive);
342         int retval;
343         int autoprobe = 0;
344         unsigned long cookie = 0;
345
346         /*
347          * Disable device irq unless we need to
348          * probe for it. Otherwise we'll get spurious
349          * interrupts during the identify-phase that
350          * the irq handler isn't expecting.
351          */
352         if (IDE_CONTROL_REG) {
353                 u8 ctl = drive->ctl | 2;
354                 if (!hwif->irq) {
355                         autoprobe = 1;
356                         cookie = probe_irq_on();
357                         /* enable device irq */
358                         ctl &= ~2;
359                 }
360                 hwif->OUTB(ctl, IDE_CONTROL_REG);
361         }
362
363         retval = actual_try_to_identify(drive, cmd);
364
365         if (autoprobe) {
366                 int irq;
367                 /* mask device irq */
368                 hwif->OUTB(drive->ctl|2, IDE_CONTROL_REG);
369                 /* clear drive IRQ */
370                 (void) hwif->INB(IDE_STATUS_REG);
371                 udelay(5);
372                 irq = probe_irq_off(cookie);
373                 if (!hwif->irq) {
374                         if (irq > 0) {
375                                 hwif->irq = irq;
376                         } else {
377                                 /* Mmmm.. multiple IRQs..
378                                  * don't know which was ours
379                                  */
380                                 printk("%s: IRQ probe failed (0x%lx)\n",
381                                         drive->name, cookie);
382                         }
383                 }
384         }
385         return retval;
386 }
387
388
389 /**
390  *      do_probe                -       probe an IDE device
391  *      @drive: drive to probe
392  *      @cmd: command to use
393  *
394  *      do_probe() has the difficult job of finding a drive if it exists,
395  *      without getting hung up if it doesn't exist, without trampling on
396  *      ethernet cards, and without leaving any IRQs dangling to haunt us later.
397  *
398  *      If a drive is "known" to exist (from CMOS or kernel parameters),
399  *      but does not respond right away, the probe will "hang in there"
400  *      for the maximum wait time (about 30 seconds), otherwise it will
401  *      exit much more quickly.
402  *
403  * Returns:     0  device was identified
404  *              1  device timed-out (no response to identify request)
405  *              2  device aborted the command (refused to identify itself)
406  *              3  bad status from device (possible for ATAPI drives)
407  *              4  probe was not attempted because failure was obvious
408  */
409
410 static int do_probe (ide_drive_t *drive, u8 cmd)
411 {
412         int rc;
413         ide_hwif_t *hwif = HWIF(drive);
414
415         if (drive->present) {
416                 /* avoid waiting for inappropriate probes */
417                 if ((drive->media != ide_disk) && (cmd == WIN_IDENTIFY))
418                         return 4;
419         }
420 #ifdef DEBUG
421         printk("probing for %s: present=%d, media=%d, probetype=%s\n",
422                 drive->name, drive->present, drive->media,
423                 (cmd == WIN_IDENTIFY) ? "ATA" : "ATAPI");
424 #endif
425
426         /* needed for some systems
427          * (e.g. crw9624 as drive0 with disk as slave)
428          */
429         msleep(50);
430         SELECT_DRIVE(drive);
431         msleep(50);
432         if (hwif->INB(IDE_SELECT_REG) != drive->select.all && !drive->present) {
433                 if (drive->select.b.unit != 0) {
434                         /* exit with drive0 selected */
435                         SELECT_DRIVE(&hwif->drives[0]);
436                         /* allow BUSY_STAT to assert & clear */
437                         msleep(50);
438                 }
439                 /* no i/f present: mmm.. this should be a 4 -ml */
440                 return 3;
441         }
442
443         if (OK_STAT((hwif->INB(IDE_STATUS_REG)), READY_STAT, BUSY_STAT) ||
444             drive->present || cmd == WIN_PIDENTIFY) {
445                 /* send cmd and wait */
446                 if ((rc = try_to_identify(drive, cmd))) {
447                         /* failed: try again */
448                         rc = try_to_identify(drive,cmd);
449                 }
450                 if (hwif->INB(IDE_STATUS_REG) == (BUSY_STAT|READY_STAT))
451                         return 4;
452
453                 if ((rc == 1 && cmd == WIN_PIDENTIFY) &&
454                         ((drive->autotune == IDE_TUNE_DEFAULT) ||
455                         (drive->autotune == IDE_TUNE_AUTO))) {
456                         unsigned long timeout;
457                         printk("%s: no response (status = 0x%02x), "
458                                 "resetting drive\n", drive->name,
459                                 hwif->INB(IDE_STATUS_REG));
460                         msleep(50);
461                         hwif->OUTB(drive->select.all, IDE_SELECT_REG);
462                         msleep(50);
463                         hwif->OUTB(WIN_SRST, IDE_COMMAND_REG);
464                         timeout = jiffies;
465                         while (((hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) &&
466                                time_before(jiffies, timeout + WAIT_WORSTCASE))
467                                 msleep(50);
468                         rc = try_to_identify(drive, cmd);
469                 }
470                 if (rc == 1)
471                         printk("%s: no response (status = 0x%02x)\n",
472                                 drive->name, hwif->INB(IDE_STATUS_REG));
473                 /* ensure drive irq is clear */
474                 (void) hwif->INB(IDE_STATUS_REG);
475         } else {
476                 /* not present or maybe ATAPI */
477                 rc = 3;
478         }
479         if (drive->select.b.unit != 0) {
480                 /* exit with drive0 selected */
481                 SELECT_DRIVE(&hwif->drives[0]);
482                 msleep(50);
483                 /* ensure drive irq is clear */
484                 (void) hwif->INB(IDE_STATUS_REG);
485         }
486         return rc;
487 }
488
489 /*
490  *
491  */
492 static void enable_nest (ide_drive_t *drive)
493 {
494         ide_hwif_t *hwif = HWIF(drive);
495         unsigned long timeout;
496
497         printk("%s: enabling %s -- ", hwif->name, drive->id->model);
498         SELECT_DRIVE(drive);
499         msleep(50);
500         hwif->OUTB(EXABYTE_ENABLE_NEST, IDE_COMMAND_REG);
501         timeout = jiffies + WAIT_WORSTCASE;
502         do {
503                 if (time_after(jiffies, timeout)) {
504                         printk("failed (timeout)\n");
505                         return;
506                 }
507                 msleep(50);
508         } while ((hwif->INB(IDE_STATUS_REG)) & BUSY_STAT);
509
510         msleep(50);
511
512         if (!OK_STAT((hwif->INB(IDE_STATUS_REG)), 0, BAD_STAT)) {
513                 printk("failed (status = 0x%02x)\n", hwif->INB(IDE_STATUS_REG));
514         } else {
515                 printk("success\n");
516         }
517
518         /* if !(success||timed-out) */
519         if (do_probe(drive, WIN_IDENTIFY) >= 2) {
520                 /* look for ATAPI device */
521                 (void) do_probe(drive, WIN_PIDENTIFY);
522         }
523 }
524
525 /**
526  *      probe_for_drives        -       upper level drive probe
527  *      @drive: drive to probe for
528  *
529  *      probe_for_drive() tests for existence of a given drive using do_probe()
530  *      and presents things to the user as needed.
531  *
532  *      Returns:        0  no device was found
533  *                      1  device was found (note: drive->present might
534  *                         still be 0)
535  */
536  
537 static inline u8 probe_for_drive (ide_drive_t *drive)
538 {
539         /*
540          *      In order to keep things simple we have an id
541          *      block for all drives at all times. If the device
542          *      is pre ATA or refuses ATA/ATAPI identify we
543          *      will add faked data to this.
544          *
545          *      Also note that 0 everywhere means "can't do X"
546          */
547  
548         drive->id = kmalloc(SECTOR_WORDS *4, GFP_KERNEL);
549         drive->id_read = 0;
550         if(drive->id == NULL)
551         {
552                 printk(KERN_ERR "ide: out of memory for id data.\n");
553                 return 0;
554         }
555         memset(drive->id, 0, SECTOR_WORDS * 4);
556         strcpy(drive->id->model, "UNKNOWN");
557         
558         /* skip probing? */
559         if (!drive->noprobe)
560         {
561                 /* if !(success||timed-out) */
562                 if (do_probe(drive, WIN_IDENTIFY) >= 2) {
563                         /* look for ATAPI device */
564                         (void) do_probe(drive, WIN_PIDENTIFY);
565                 }
566                 if (strstr(drive->id->model, "E X A B Y T E N E S T"))
567                         enable_nest(drive);
568                 if (!drive->present)
569                         /* drive not found */
570                         return 0;
571         
572                 /* identification failed? */
573                 if (!drive->id_read) {
574                         if (drive->media == ide_disk) {
575                                 printk(KERN_INFO "%s: non-IDE drive, CHS=%d/%d/%d\n",
576                                         drive->name, drive->cyl,
577                                         drive->head, drive->sect);
578                         } else if (drive->media == ide_cdrom) {
579                                 printk(KERN_INFO "%s: ATAPI cdrom (?)\n", drive->name);
580                         } else {
581                                 /* nuke it */
582                                 printk(KERN_WARNING "%s: Unknown device on bus refused identification. Ignoring.\n", drive->name);
583                                 drive->present = 0;
584                         }
585                 }
586                 /* drive was found */
587         }
588         if(!drive->present)
589                 return 0;
590         /* The drive wasn't being helpful. Add generic info only */
591         if(!drive->id_read)
592                 generic_id(drive);
593         return drive->present;
594 }
595
596 static void hwif_release_dev (struct device *dev)
597 {
598         ide_hwif_t *hwif = container_of(dev, ide_hwif_t, gendev);
599
600         up(&hwif->gendev_rel_sem);
601 }
602
603 static void hwif_register (ide_hwif_t *hwif)
604 {
605         /* register with global device tree */
606         strlcpy(hwif->gendev.bus_id,hwif->name,BUS_ID_SIZE);
607         hwif->gendev.driver_data = hwif;
608         if (hwif->gendev.parent == NULL) {
609                 if (hwif->pci_dev)
610                         hwif->gendev.parent = &hwif->pci_dev->dev;
611                 else
612                         /* Would like to do = &device_legacy */
613                         hwif->gendev.parent = NULL;
614         }
615         hwif->gendev.release = hwif_release_dev;
616         device_register(&hwif->gendev);
617         hwif->configured = 1;
618 }
619
620 static int wait_hwif_ready(ide_hwif_t *hwif)
621 {
622         int rc;
623
624         printk(KERN_DEBUG "Probing IDE interface %s...\n", hwif->name);
625
626         /* Let HW settle down a bit from whatever init state we
627          * come from */
628         mdelay(2);
629
630         /* Wait for BSY bit to go away, spec timeout is 30 seconds,
631          * I know of at least one disk who takes 31 seconds, I use 35
632          * here to be safe
633          */
634         rc = ide_wait_not_busy(hwif, 35000);
635         if (rc)
636                 return rc;
637
638         /* Now make sure both master & slave are ready */
639         SELECT_DRIVE(&hwif->drives[0]);
640         hwif->OUTB(8, hwif->io_ports[IDE_CONTROL_OFFSET]);
641         mdelay(2);
642         rc = ide_wait_not_busy(hwif, 10000);
643         if (rc)
644                 return rc;
645         SELECT_DRIVE(&hwif->drives[1]);
646         hwif->OUTB(8, hwif->io_ports[IDE_CONTROL_OFFSET]);
647         mdelay(2);
648         rc = ide_wait_not_busy(hwif, 10000);
649
650         /* Exit function with master reselected (let's be sane) */
651         SELECT_DRIVE(&hwif->drives[0]);
652         
653         return rc;
654 }
655
656 /**
657  *      ide_undecoded_slave     -       look for bad CF adapters
658  *      @hwif: interface
659  *
660  *      Analyse the drives on the interface and attempt to decide if we
661  *      have the same drive viewed twice. This occurs with crap CF adapters
662  *      and PCMCIA sometimes.
663  */
664
665 void ide_undecoded_slave(ide_hwif_t *hwif)
666 {
667         ide_drive_t *drive0 = &hwif->drives[0];
668         ide_drive_t *drive1 = &hwif->drives[1];
669
670         if (drive0->present == 0 || drive1->present == 0)
671                 return;
672
673         /* If the models don't match they are not the same product */
674         if (strcmp(drive0->id->model, drive1->id->model))
675                 return;
676
677         /* Serial numbers do not match */
678         if (strncmp(drive0->id->serial_no, drive1->id->serial_no, 20))
679                 return;
680
681         /* No serial number, thankfully very rare for CF */
682         if (drive0->id->serial_no[0] == 0)
683                 return;
684
685         /* Appears to be an IDE flash adapter with decode bugs */
686         printk(KERN_WARNING "ide-probe: ignoring undecoded slave\n");
687
688         drive1->present = 0;
689 }
690
691 EXPORT_SYMBOL_GPL(ide_undecoded_slave);
692
693 /*
694  * This routine only knows how to look for drive units 0 and 1
695  * on an interface, so any setting of MAX_DRIVES > 2 won't work here.
696  */
697 static void probe_hwif(ide_hwif_t *hwif)
698 {
699         unsigned int unit;
700         unsigned long flags;
701         unsigned int irqd;
702
703         if (hwif->noprobe)
704                 return;
705
706         if ((hwif->chipset != ide_4drives || !hwif->mate || !hwif->mate->present) &&
707             (ide_hwif_request_regions(hwif))) {
708                 u16 msgout = 0;
709                 for (unit = 0; unit < MAX_DRIVES; ++unit) {
710                         ide_drive_t *drive = &hwif->drives[unit];
711                         if (drive->present) {
712                                 drive->present = 0;
713                                 printk(KERN_ERR "%s: ERROR, PORTS ALREADY IN USE\n",
714                                         drive->name);
715                                 msgout = 1;
716                         }
717                 }
718                 if (!msgout)
719                         printk(KERN_ERR "%s: ports already in use, skipping probe\n",
720                                 hwif->name);
721                 return; 
722         }
723
724         /*
725          * We must always disable IRQ, as probe_for_drive will assert IRQ, but
726          * we'll install our IRQ driver much later...
727          */
728         irqd = hwif->irq;
729         if (irqd)
730                 disable_irq(hwif->irq);
731
732         local_irq_set(flags);
733
734         /* This is needed on some PPCs and a bunch of BIOS-less embedded
735          * platforms. Typical cases are:
736          * 
737          *  - The firmware hard reset the disk before booting the kernel,
738          *    the drive is still doing it's poweron-reset sequence, that
739          *    can take up to 30 seconds
740          *  - The firmware does nothing (or no firmware), the device is
741          *    still in POST state (same as above actually).
742          *  - Some CD/DVD/Writer combo drives tend to drive the bus during
743          *    their reset sequence even when they are non-selected slave
744          *    devices, thus preventing discovery of the main HD
745          *    
746          *  Doing this wait-for-busy should not harm any existing configuration
747          *  (at least things won't be worse than what current code does, that
748          *  is blindly go & talk to the drive) and fix some issues like the
749          *  above.
750          *  
751          *  BenH.
752          */
753         if (wait_hwif_ready(hwif))
754                 printk(KERN_DEBUG "%s: Wait for ready failed before probe !\n", hwif->name);
755
756         /*
757          * Second drive should only exist if first drive was found,
758          * but a lot of cdrom drives are configured as single slaves.
759          */
760         for (unit = 0; unit < MAX_DRIVES; ++unit) {
761                 ide_drive_t *drive = &hwif->drives[unit];
762                 drive->dn = (hwif->channel ? 2 : 0) + unit;
763                 (void) probe_for_drive(drive);
764                 if (drive->present && !hwif->present) {
765                         hwif->present = 1;
766                         if (hwif->chipset != ide_4drives ||
767                             !hwif->mate || 
768                             !hwif->mate->present) {
769                                 hwif_register(hwif);
770                         }
771                 }
772         }
773         if (hwif->io_ports[IDE_CONTROL_OFFSET] && hwif->reset) {
774                 unsigned long timeout = jiffies + WAIT_WORSTCASE;
775                 u8 stat;
776
777                 printk(KERN_WARNING "%s: reset\n", hwif->name);
778                 hwif->OUTB(12, hwif->io_ports[IDE_CONTROL_OFFSET]);
779                 udelay(10);
780                 hwif->OUTB(8, hwif->io_ports[IDE_CONTROL_OFFSET]);
781                 do {
782                         msleep(50);
783                         stat = hwif->INB(hwif->io_ports[IDE_STATUS_OFFSET]);
784                 } while ((stat & BUSY_STAT) && time_after(timeout, jiffies));
785
786         }
787         local_irq_restore(flags);
788         /*
789          * Use cached IRQ number. It might be (and is...) changed by probe
790          * code above
791          */
792         if (irqd)
793                 enable_irq(irqd);
794
795         if (!hwif->present) {
796                 ide_hwif_release_regions(hwif);
797                 return;
798         }
799
800         for (unit = 0; unit < MAX_DRIVES; ++unit) {
801                 ide_drive_t *drive = &hwif->drives[unit];
802
803                 if (drive->present) {
804                         if (hwif->tuneproc != NULL && 
805                                 drive->autotune == IDE_TUNE_AUTO)
806                                 /* auto-tune PIO mode */
807                                 hwif->tuneproc(drive, 255);
808                         /*
809                          * MAJOR HACK BARF :-/
810                          *
811                          * FIXME: chipsets own this cruft!
812                          */
813                         /*
814                          * Move here to prevent module loading clashing.
815                          */
816         //              drive->autodma = hwif->autodma;
817                         if ((hwif->ide_dma_check) &&
818                                 ((drive->autotune == IDE_TUNE_DEFAULT) ||
819                                 (drive->autotune == IDE_TUNE_AUTO))) {
820                                 /*
821                                  * Force DMAing for the beginning of the check.
822                                  * Some chipsets appear to do interesting
823                                  * things, if not checked and cleared.
824                                  *   PARANOIA!!!
825                                  */
826                                 hwif->ide_dma_off_quietly(drive);
827 #ifdef CONFIG_IDEDMA_ONLYDISK
828                                 if (drive->media == ide_disk)
829 #endif
830                                         hwif->ide_dma_check(drive);
831                         }
832                 }
833         }
834 }
835
836 static int hwif_init(ide_hwif_t *hwif);
837
838 int probe_hwif_init_with_fixup(ide_hwif_t *hwif, void (*fixup)(ide_hwif_t *hwif))
839 {
840         probe_hwif(hwif);
841
842         if (fixup)
843                 fixup(hwif);
844
845         hwif_init(hwif);
846
847         if (hwif->present) {
848                 u16 unit = 0;
849                 for (unit = 0; unit < MAX_DRIVES; ++unit) {
850                         ide_drive_t *drive = &hwif->drives[unit];
851                         /* For now don't attach absent drives, we may
852                            want them on default or a new "empty" class
853                            for hotplug reprobing ? */
854                         if (drive->present) {
855                                 ata_attach(drive);
856                         }
857                 }
858         }
859         return 0;
860 }
861
862 int probe_hwif_init(ide_hwif_t *hwif)
863 {
864         return probe_hwif_init_with_fixup(hwif, NULL);
865 }
866
867 EXPORT_SYMBOL(probe_hwif_init);
868
869 #if MAX_HWIFS > 1
870 /*
871  * save_match() is used to simplify logic in init_irq() below.
872  *
873  * A loophole here is that we may not know about a particular
874  * hwif's irq until after that hwif is actually probed/initialized..
875  * This could be a problem for the case where an hwif is on a
876  * dual interface that requires serialization (eg. cmd640) and another
877  * hwif using one of the same irqs is initialized beforehand.
878  *
879  * This routine detects and reports such situations, but does not fix them.
880  */
881 static void save_match(ide_hwif_t *hwif, ide_hwif_t *new, ide_hwif_t **match)
882 {
883         ide_hwif_t *m = *match;
884
885         if (m && m->hwgroup && m->hwgroup != new->hwgroup) {
886                 if (!new->hwgroup)
887                         return;
888                 printk("%s: potential irq problem with %s and %s\n",
889                         hwif->name, new->name, m->name);
890         }
891         if (!m || m->irq != hwif->irq) /* don't undo a prior perfect match */
892                 *match = new;
893 }
894 #endif /* MAX_HWIFS > 1 */
895
896 /*
897  * init request queue
898  */
899 static int ide_init_queue(ide_drive_t *drive)
900 {
901         request_queue_t *q;
902         ide_hwif_t *hwif = HWIF(drive);
903         int max_sectors = 256;
904         int max_sg_entries = PRD_ENTRIES;
905
906         /*
907          *      Our default set up assumes the normal IDE case,
908          *      that is 64K segmenting, standard PRD setup
909          *      and LBA28. Some drivers then impose their own
910          *      limits and LBA48 we could raise it but as yet
911          *      do not.
912          */
913          
914         q = blk_init_queue(do_ide_request, &ide_lock);
915         if (!q)
916                 return 1;
917
918         q->queuedata = drive;
919         blk_queue_segment_boundary(q, 0xffff);
920
921         if (!hwif->rqsize) {
922                 if (hwif->no_lba48 || hwif->no_lba48_dma)
923                         hwif->rqsize = 256;
924                 else
925                         hwif->rqsize = 65536;
926         }
927         if (hwif->rqsize < max_sectors)
928                 max_sectors = hwif->rqsize;
929         blk_queue_max_sectors(q, max_sectors);
930
931 #ifdef CONFIG_PCI
932         /* When we have an IOMMU, we may have a problem where pci_map_sg()
933          * creates segments that don't completely match our boundary
934          * requirements and thus need to be broken up again. Because it
935          * doesn't align properly either, we may actually have to break up
936          * to more segments than what was we got in the first place, a max
937          * worst case is twice as many.
938          * This will be fixed once we teach pci_map_sg() about our boundary
939          * requirements, hopefully soon. *FIXME*
940          */
941         if (!PCI_DMA_BUS_IS_PHYS)
942                 max_sg_entries >>= 1;
943 #endif /* CONFIG_PCI */
944
945         blk_queue_max_hw_segments(q, max_sg_entries);
946         blk_queue_max_phys_segments(q, max_sg_entries);
947
948         /* assign drive and gendisk queue */
949         drive->queue = q;
950         if (drive->disk)
951                 drive->disk->queue = drive->queue;
952
953         /* needs drive->queue to be set */
954         ide_toggle_bounce(drive, 1);
955
956         /* enable led activity for disk drives only */
957         if (drive->media == ide_disk && hwif->led_act)
958                 blk_queue_activity_fn(q, hwif->led_act, drive);
959
960         return 0;
961 }
962
963 /*
964  * This routine sets up the irq for an ide interface, and creates a new
965  * hwgroup for the irq/hwif if none was previously assigned.
966  *
967  * Much of the code is for correctly detecting/handling irq sharing
968  * and irq serialization situations.  This is somewhat complex because
969  * it handles static as well as dynamic (PCMCIA) IDE interfaces.
970  *
971  * The SA_INTERRUPT in sa_flags means ide_intr() is always entered with
972  * interrupts completely disabled.  This can be bad for interrupt latency,
973  * but anything else has led to problems on some machines.  We re-enable
974  * interrupts as much as we can safely do in most places.
975  */
976 static int init_irq (ide_hwif_t *hwif)
977 {
978         unsigned int index;
979         ide_hwgroup_t *hwgroup;
980         ide_hwif_t *match = NULL;
981
982
983         BUG_ON(in_interrupt());
984         BUG_ON(irqs_disabled());        
985         down(&ide_cfg_sem);
986         hwif->hwgroup = NULL;
987 #if MAX_HWIFS > 1
988         /*
989          * Group up with any other hwifs that share our irq(s).
990          */
991         for (index = 0; index < MAX_HWIFS; index++) {
992                 ide_hwif_t *h = &ide_hwifs[index];
993                 if (h->hwgroup) {  /* scan only initialized hwif's */
994                         if (hwif->irq == h->irq) {
995                                 hwif->sharing_irq = h->sharing_irq = 1;
996                                 if (hwif->chipset != ide_pci ||
997                                     h->chipset != ide_pci) {
998                                         save_match(hwif, h, &match);
999                                 }
1000                         }
1001                         if (hwif->serialized) {
1002                                 if (hwif->mate && hwif->mate->irq == h->irq)
1003                                         save_match(hwif, h, &match);
1004                         }
1005                         if (h->serialized) {
1006                                 if (h->mate && hwif->irq == h->mate->irq)
1007                                         save_match(hwif, h, &match);
1008                         }
1009                 }
1010         }
1011 #endif /* MAX_HWIFS > 1 */
1012         /*
1013          * If we are still without a hwgroup, then form a new one
1014          */
1015         if (match) {
1016                 hwgroup = match->hwgroup;
1017                 hwif->hwgroup = hwgroup;
1018                 /*
1019                  * Link us into the hwgroup.
1020                  * This must be done early, do ensure that unexpected_intr
1021                  * can find the hwif and prevent irq storms.
1022                  * No drives are attached to the new hwif, choose_drive
1023                  * can't do anything stupid (yet).
1024                  * Add ourself as the 2nd entry to the hwgroup->hwif
1025                  * linked list, the first entry is the hwif that owns
1026                  * hwgroup->handler - do not change that.
1027                  */
1028                 spin_lock_irq(&ide_lock);
1029                 hwif->next = hwgroup->hwif->next;
1030                 hwgroup->hwif->next = hwif;
1031                 spin_unlock_irq(&ide_lock);
1032         } else {
1033                 hwgroup = kmalloc(sizeof(ide_hwgroup_t),GFP_KERNEL);
1034                 if (!hwgroup)
1035                         goto out_up;
1036
1037                 hwif->hwgroup = hwgroup;
1038
1039                 memset(hwgroup, 0, sizeof(ide_hwgroup_t));
1040                 hwgroup->hwif     = hwif->next = hwif;
1041                 hwgroup->rq       = NULL;
1042                 hwgroup->handler  = NULL;
1043                 hwgroup->drive    = NULL;
1044                 hwgroup->busy     = 0;
1045                 init_timer(&hwgroup->timer);
1046                 hwgroup->timer.function = &ide_timer_expiry;
1047                 hwgroup->timer.data = (unsigned long) hwgroup;
1048         }
1049
1050         /*
1051          * Allocate the irq, if not already obtained for another hwif
1052          */
1053         if (!match || match->irq != hwif->irq) {
1054                 int sa = SA_INTERRUPT;
1055 #if defined(__mc68000__) || defined(CONFIG_APUS)
1056                 sa = SA_SHIRQ;
1057 #endif /* __mc68000__ || CONFIG_APUS */
1058
1059                 if (IDE_CHIPSET_IS_PCI(hwif->chipset)) {
1060                         sa = SA_SHIRQ;
1061 #ifndef CONFIG_IDEPCI_SHARE_IRQ
1062                         sa |= SA_INTERRUPT;
1063 #endif /* CONFIG_IDEPCI_SHARE_IRQ */
1064                 }
1065
1066                 if (hwif->io_ports[IDE_CONTROL_OFFSET])
1067                         /* clear nIEN */
1068                         hwif->OUTB(0x08, hwif->io_ports[IDE_CONTROL_OFFSET]);
1069
1070                 if (request_irq(hwif->irq,&ide_intr,sa,hwif->name,hwgroup))
1071                         goto out_unlink;
1072         }
1073
1074         /*
1075          * For any present drive:
1076          * - allocate the block device queue
1077          * - link drive into the hwgroup
1078          */
1079         for (index = 0; index < MAX_DRIVES; ++index) {
1080                 ide_drive_t *drive = &hwif->drives[index];
1081                 if (!drive->present)
1082                         continue;
1083                 if (ide_init_queue(drive)) {
1084                         printk(KERN_ERR "ide: failed to init %s\n",drive->name);
1085                         continue;
1086                 }
1087                 spin_lock_irq(&ide_lock);
1088                 if (!hwgroup->drive) {
1089                         /* first drive for hwgroup. */
1090                         drive->next = drive;
1091                         hwgroup->drive = drive;
1092                         hwgroup->hwif = HWIF(hwgroup->drive);
1093                 } else {
1094                         drive->next = hwgroup->drive->next;
1095                         hwgroup->drive->next = drive;
1096                 }
1097                 spin_unlock_irq(&ide_lock);
1098         }
1099
1100 #if !defined(__mc68000__) && !defined(CONFIG_APUS) && !defined(__sparc__)
1101         printk("%s at 0x%03lx-0x%03lx,0x%03lx on irq %d", hwif->name,
1102                 hwif->io_ports[IDE_DATA_OFFSET],
1103                 hwif->io_ports[IDE_DATA_OFFSET]+7,
1104                 hwif->io_ports[IDE_CONTROL_OFFSET], hwif->irq);
1105 #elif defined(__sparc__)
1106         printk("%s at 0x%03lx-0x%03lx,0x%03lx on irq %s", hwif->name,
1107                 hwif->io_ports[IDE_DATA_OFFSET],
1108                 hwif->io_ports[IDE_DATA_OFFSET]+7,
1109                 hwif->io_ports[IDE_CONTROL_OFFSET], __irq_itoa(hwif->irq));
1110 #else
1111         printk("%s at 0x%08lx on irq %d", hwif->name,
1112                 hwif->io_ports[IDE_DATA_OFFSET], hwif->irq);
1113 #endif /* __mc68000__ && CONFIG_APUS */
1114         if (match)
1115                 printk(" (%sed with %s)",
1116                         hwif->sharing_irq ? "shar" : "serializ", match->name);
1117         printk("\n");
1118         up(&ide_cfg_sem);
1119         return 0;
1120 out_unlink:
1121         spin_lock_irq(&ide_lock);
1122         if (hwif->next == hwif) {
1123                 BUG_ON(match);
1124                 BUG_ON(hwgroup->hwif != hwif);
1125                 kfree(hwgroup);
1126         } else {
1127                 ide_hwif_t *g;
1128                 g = hwgroup->hwif;
1129                 while (g->next != hwif)
1130                         g = g->next;
1131                 g->next = hwif->next;
1132                 if (hwgroup->hwif == hwif) {
1133                         /* Impossible. */
1134                         printk(KERN_ERR "Duh. Uninitialized hwif listed as active hwif.\n");
1135                         hwgroup->hwif = g;
1136                 }
1137                 BUG_ON(hwgroup->hwif == hwif);
1138         }
1139         spin_unlock_irq(&ide_lock);
1140 out_up:
1141         up(&ide_cfg_sem);
1142         return 1;
1143 }
1144
1145 static int ata_lock(dev_t dev, void *data)
1146 {
1147         /* FIXME: we want to pin hwif down */
1148         return 0;
1149 }
1150
1151 extern ide_driver_t idedefault_driver;
1152
1153 static struct kobject *ata_probe(dev_t dev, int *part, void *data)
1154 {
1155         ide_hwif_t *hwif = data;
1156         int unit = *part >> PARTN_BITS;
1157         ide_drive_t *drive = &hwif->drives[unit];
1158         if (!drive->present)
1159                 return NULL;
1160         if (drive->driver == &idedefault_driver) {
1161                 if (drive->media == ide_disk)
1162                         (void) request_module("ide-disk");
1163                 if (drive->scsi)
1164                         (void) request_module("ide-scsi");
1165                 if (drive->media == ide_cdrom || drive->media == ide_optical)
1166                         (void) request_module("ide-cd");
1167                 if (drive->media == ide_tape)
1168                         (void) request_module("ide-tape");
1169                 if (drive->media == ide_floppy)
1170                         (void) request_module("ide-floppy");
1171         }
1172         if (drive->driver == &idedefault_driver)
1173                 return NULL;
1174         *part &= (1 << PARTN_BITS) - 1;
1175         return get_disk(drive->disk);
1176 }
1177
1178 static int alloc_disks(ide_hwif_t *hwif)
1179 {
1180         unsigned int unit;
1181         struct gendisk *disks[MAX_DRIVES];
1182
1183         for (unit = 0; unit < MAX_DRIVES; unit++) {
1184                 disks[unit] = alloc_disk(1 << PARTN_BITS);
1185                 if (!disks[unit])
1186                         goto Enomem;
1187         }
1188         for (unit = 0; unit < MAX_DRIVES; ++unit) {
1189                 ide_drive_t *drive = &hwif->drives[unit];
1190                 struct gendisk *disk = disks[unit];
1191                 disk->major  = hwif->major;
1192                 disk->first_minor = unit << PARTN_BITS;
1193                 sprintf(disk->disk_name,"hd%c",'a'+hwif->index*MAX_DRIVES+unit);
1194                 disk->fops = ide_fops;
1195                 disk->private_data = drive;
1196                 drive->disk = disk;
1197         }
1198         return 0;
1199 Enomem:
1200         printk(KERN_WARNING "(ide::init_gendisk) Out of memory\n");
1201         while (unit--)
1202                 put_disk(disks[unit]);
1203         return -ENOMEM;
1204 }
1205
1206 static void drive_release_dev (struct device *dev)
1207 {
1208         ide_drive_t *drive = container_of(dev, ide_drive_t, gendev);
1209
1210         up(&drive->gendev_rel_sem);
1211 }
1212
1213 /*
1214  * init_gendisk() (as opposed to ide_geninit) is called for each major device,
1215  * after probing for drives, to allocate partition tables and other data
1216  * structures needed for the routines in genhd.c.  ide_geninit() gets called
1217  * somewhat later, during the partition check.
1218  */
1219 static void init_gendisk (ide_hwif_t *hwif)
1220 {
1221         unsigned int unit;
1222
1223         for (unit = 0; unit < MAX_DRIVES; ++unit) {
1224                 ide_drive_t * drive = &hwif->drives[unit];
1225                 ide_add_generic_settings(drive);
1226                 snprintf(drive->gendev.bus_id,BUS_ID_SIZE,"%u.%u",
1227                          hwif->index,unit);
1228                 drive->gendev.parent = &hwif->gendev;
1229                 drive->gendev.bus = &ide_bus_type;
1230                 drive->gendev.driver_data = drive;
1231                 drive->gendev.release = drive_release_dev;
1232                 if (drive->present) {
1233                         device_register(&drive->gendev);
1234                         sprintf(drive->devfs_name, "ide/host%d/bus%d/target%d/lun%d",
1235                                 (hwif->channel && hwif->mate) ?
1236                                 hwif->mate->index : hwif->index,
1237                                 hwif->channel, unit, drive->lun);
1238                 }
1239         }
1240         blk_register_region(MKDEV(hwif->major, 0), MAX_DRIVES << PARTN_BITS,
1241                         THIS_MODULE, ata_probe, ata_lock, hwif);
1242 }
1243
1244 static int hwif_init(ide_hwif_t *hwif)
1245 {
1246         int old_irq, unit;
1247
1248         if (!hwif->present)
1249                 return 0;
1250
1251         if (!hwif->irq) {
1252                 if (!(hwif->irq = ide_default_irq(hwif->io_ports[IDE_DATA_OFFSET])))
1253                 {
1254                         printk("%s: DISABLED, NO IRQ\n", hwif->name);
1255                         return (hwif->present = 0);
1256                 }
1257         }
1258 #ifdef CONFIG_BLK_DEV_HD
1259         if (hwif->irq == HD_IRQ && hwif->io_ports[IDE_DATA_OFFSET] != HD_DATA) {
1260                 printk("%s: CANNOT SHARE IRQ WITH OLD "
1261                         "HARDDISK DRIVER (hd.c)\n", hwif->name);
1262                 return (hwif->present = 0);
1263         }
1264 #endif /* CONFIG_BLK_DEV_HD */
1265
1266         /* we set it back to 1 if all is ok below */    
1267         hwif->present = 0;
1268
1269         if (register_blkdev(hwif->major, hwif->name))
1270                 return 0;
1271
1272         if (!hwif->sg_max_nents)
1273                 hwif->sg_max_nents = PRD_ENTRIES;
1274
1275         hwif->sg_table = kmalloc(sizeof(struct scatterlist)*hwif->sg_max_nents,
1276                                  GFP_KERNEL);
1277         if (!hwif->sg_table) {
1278                 printk(KERN_ERR "%s: unable to allocate SG table.\n", hwif->name);
1279                 goto out;
1280         }
1281
1282         if (alloc_disks(hwif) < 0)
1283                 goto out;
1284         
1285         if (init_irq(hwif) == 0)
1286                 goto done;
1287
1288         old_irq = hwif->irq;
1289         /*
1290          *      It failed to initialise. Find the default IRQ for 
1291          *      this port and try that.
1292          */
1293         if (!(hwif->irq = ide_default_irq(hwif->io_ports[IDE_DATA_OFFSET]))) {
1294                 printk("%s: Disabled unable to get IRQ %d.\n",
1295                         hwif->name, old_irq);
1296                 goto out_disks;
1297         }
1298         if (init_irq(hwif)) {
1299                 printk("%s: probed IRQ %d and default IRQ %d failed.\n",
1300                         hwif->name, old_irq, hwif->irq);
1301                 goto out_disks;
1302         }
1303         printk("%s: probed IRQ %d failed, using default.\n",
1304                 hwif->name, hwif->irq);
1305
1306 done:
1307         init_gendisk(hwif);
1308         hwif->present = 1;      /* success */
1309         return 1;
1310
1311 out_disks:
1312         for (unit = 0; unit < MAX_DRIVES; unit++) {
1313                 struct gendisk *disk = hwif->drives[unit].disk;
1314                 hwif->drives[unit].disk = NULL;
1315                 put_disk(disk);
1316         }
1317 out:
1318         unregister_blkdev(hwif->major, hwif->name);
1319         return 0;
1320 }
1321
1322 int ideprobe_init (void)
1323 {
1324         unsigned int index;
1325         int probe[MAX_HWIFS];
1326
1327         memset(probe, 0, MAX_HWIFS * sizeof(int));
1328         for (index = 0; index < MAX_HWIFS; ++index)
1329                 probe[index] = !ide_hwifs[index].present;
1330
1331         for (index = 0; index < MAX_HWIFS; ++index)
1332                 if (probe[index])
1333                         probe_hwif(&ide_hwifs[index]);
1334         for (index = 0; index < MAX_HWIFS; ++index)
1335                 if (probe[index])
1336                         hwif_init(&ide_hwifs[index]);
1337         for (index = 0; index < MAX_HWIFS; ++index) {
1338                 if (probe[index]) {
1339                         ide_hwif_t *hwif = &ide_hwifs[index];
1340                         int unit;
1341                         if (!hwif->present)
1342                                 continue;
1343                         if (hwif->chipset == ide_unknown || hwif->chipset == ide_forced)
1344                                 hwif->chipset = ide_generic;
1345                         for (unit = 0; unit < MAX_DRIVES; ++unit)
1346                                 if (hwif->drives[unit].present)
1347                                         ata_attach(&hwif->drives[unit]);
1348                 }
1349         }
1350         return 0;
1351 }
1352
1353 EXPORT_SYMBOL_GPL(ideprobe_init);