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