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