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