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