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