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