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