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