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