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