ide: add ide_tf_read() helper
[safe/jmp/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  * -- 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                                 /*
838                                  * Force DMAing for the beginning of the check.
839                                  * Some chipsets appear to do interesting
840                                  * things, if not checked and cleared.
841                                  *   PARANOIA!!!
842                                  */
843                                 hwif->dma_off_quietly(drive);
844                                 ide_set_dma(drive);
845                         }
846                 }
847         }
848
849         for (unit = 0; unit < MAX_DRIVES; ++unit) {
850                 ide_drive_t *drive = &hwif->drives[unit];
851
852                 if (hwif->no_io_32bit)
853                         drive->no_io_32bit = 1;
854                 else
855                         drive->no_io_32bit = drive->id->dword_io ? 1 : 0;
856         }
857 }
858
859 static int hwif_init(ide_hwif_t *hwif);
860 static void hwif_register_devices(ide_hwif_t *hwif);
861
862 static int probe_hwif_init(ide_hwif_t *hwif)
863 {
864         probe_hwif(hwif);
865
866         if (!hwif_init(hwif)) {
867                 printk(KERN_INFO "%s: failed to initialize IDE interface\n",
868                                  hwif->name);
869                 return -1;
870         }
871
872         if (hwif->present)
873                 hwif_register_devices(hwif);
874
875         return 0;
876 }
877
878 #if MAX_HWIFS > 1
879 /*
880  * save_match() is used to simplify logic in init_irq() below.
881  *
882  * A loophole here is that we may not know about a particular
883  * hwif's irq until after that hwif is actually probed/initialized..
884  * This could be a problem for the case where an hwif is on a
885  * dual interface that requires serialization (eg. cmd640) and another
886  * hwif using one of the same irqs is initialized beforehand.
887  *
888  * This routine detects and reports such situations, but does not fix them.
889  */
890 static void save_match(ide_hwif_t *hwif, ide_hwif_t *new, ide_hwif_t **match)
891 {
892         ide_hwif_t *m = *match;
893
894         if (m && m->hwgroup && m->hwgroup != new->hwgroup) {
895                 if (!new->hwgroup)
896                         return;
897                 printk("%s: potential irq problem with %s and %s\n",
898                         hwif->name, new->name, m->name);
899         }
900         if (!m || m->irq != hwif->irq) /* don't undo a prior perfect match */
901                 *match = new;
902 }
903 #endif /* MAX_HWIFS > 1 */
904
905 /*
906  * init request queue
907  */
908 static int ide_init_queue(ide_drive_t *drive)
909 {
910         struct request_queue *q;
911         ide_hwif_t *hwif = HWIF(drive);
912         int max_sectors = 256;
913         int max_sg_entries = PRD_ENTRIES;
914
915         /*
916          *      Our default set up assumes the normal IDE case,
917          *      that is 64K segmenting, standard PRD setup
918          *      and LBA28. Some drivers then impose their own
919          *      limits and LBA48 we could raise it but as yet
920          *      do not.
921          */
922
923         q = blk_init_queue_node(do_ide_request, &ide_lock, hwif_to_node(hwif));
924         if (!q)
925                 return 1;
926
927         q->queuedata = drive;
928         blk_queue_segment_boundary(q, 0xffff);
929
930         if (!hwif->rqsize) {
931                 if ((hwif->host_flags & IDE_HFLAG_NO_LBA48) ||
932                     (hwif->host_flags & IDE_HFLAG_NO_LBA48_DMA))
933                         hwif->rqsize = 256;
934                 else
935                         hwif->rqsize = 65536;
936         }
937         if (hwif->rqsize < max_sectors)
938                 max_sectors = hwif->rqsize;
939         blk_queue_max_sectors(q, max_sectors);
940
941 #ifdef CONFIG_PCI
942         /* When we have an IOMMU, we may have a problem where pci_map_sg()
943          * creates segments that don't completely match our boundary
944          * requirements and thus need to be broken up again. Because it
945          * doesn't align properly either, we may actually have to break up
946          * to more segments than what was we got in the first place, a max
947          * worst case is twice as many.
948          * This will be fixed once we teach pci_map_sg() about our boundary
949          * requirements, hopefully soon. *FIXME*
950          */
951         if (!PCI_DMA_BUS_IS_PHYS)
952                 max_sg_entries >>= 1;
953 #endif /* CONFIG_PCI */
954
955         blk_queue_max_hw_segments(q, max_sg_entries);
956         blk_queue_max_phys_segments(q, max_sg_entries);
957
958         /* assign drive queue */
959         drive->queue = q;
960
961         /* needs drive->queue to be set */
962         ide_toggle_bounce(drive, 1);
963
964         return 0;
965 }
966
967 /*
968  * This routine sets up the irq for an ide interface, and creates a new
969  * hwgroup for the irq/hwif if none was previously assigned.
970  *
971  * Much of the code is for correctly detecting/handling irq sharing
972  * and irq serialization situations.  This is somewhat complex because
973  * it handles static as well as dynamic (PCMCIA) IDE interfaces.
974  */
975 static int init_irq (ide_hwif_t *hwif)
976 {
977         unsigned int index;
978         ide_hwgroup_t *hwgroup;
979         ide_hwif_t *match = NULL;
980
981
982         BUG_ON(in_interrupt());
983         BUG_ON(irqs_disabled());        
984         BUG_ON(hwif == NULL);
985
986         mutex_lock(&ide_cfg_mtx);
987         hwif->hwgroup = NULL;
988 #if MAX_HWIFS > 1
989         /*
990          * Group up with any other hwifs that share our irq(s).
991          */
992         for (index = 0; index < MAX_HWIFS; index++) {
993                 ide_hwif_t *h = &ide_hwifs[index];
994                 if (h->hwgroup) {  /* scan only initialized hwif's */
995                         if (hwif->irq == h->irq) {
996                                 hwif->sharing_irq = h->sharing_irq = 1;
997                                 if (hwif->chipset != ide_pci ||
998                                     h->chipset != ide_pci) {
999                                         save_match(hwif, h, &match);
1000                                 }
1001                         }
1002                         if (hwif->serialized) {
1003                                 if (hwif->mate && hwif->mate->irq == h->irq)
1004                                         save_match(hwif, h, &match);
1005                         }
1006                         if (h->serialized) {
1007                                 if (h->mate && hwif->irq == h->mate->irq)
1008                                         save_match(hwif, h, &match);
1009                         }
1010                 }
1011         }
1012 #endif /* MAX_HWIFS > 1 */
1013         /*
1014          * If we are still without a hwgroup, then form a new one
1015          */
1016         if (match) {
1017                 hwgroup = match->hwgroup;
1018                 hwif->hwgroup = hwgroup;
1019                 /*
1020                  * Link us into the hwgroup.
1021                  * This must be done early, do ensure that unexpected_intr
1022                  * can find the hwif and prevent irq storms.
1023                  * No drives are attached to the new hwif, choose_drive
1024                  * can't do anything stupid (yet).
1025                  * Add ourself as the 2nd entry to the hwgroup->hwif
1026                  * linked list, the first entry is the hwif that owns
1027                  * hwgroup->handler - do not change that.
1028                  */
1029                 spin_lock_irq(&ide_lock);
1030                 hwif->next = hwgroup->hwif->next;
1031                 hwgroup->hwif->next = hwif;
1032                 spin_unlock_irq(&ide_lock);
1033         } else {
1034                 hwgroup = kmalloc_node(sizeof(ide_hwgroup_t),
1035                                         GFP_KERNEL | __GFP_ZERO,
1036                                         hwif_to_node(hwif->drives[0].hwif));
1037                 if (!hwgroup)
1038                         goto out_up;
1039
1040                 hwif->hwgroup = hwgroup;
1041
1042                 hwgroup->hwif     = hwif->next = hwif;
1043                 hwgroup->rq       = NULL;
1044                 hwgroup->handler  = NULL;
1045                 hwgroup->drive    = NULL;
1046                 hwgroup->busy     = 0;
1047                 init_timer(&hwgroup->timer);
1048                 hwgroup->timer.function = &ide_timer_expiry;
1049                 hwgroup->timer.data = (unsigned long) hwgroup;
1050         }
1051
1052         /*
1053          * Allocate the irq, if not already obtained for another hwif
1054          */
1055         if (!match || match->irq != hwif->irq) {
1056                 int sa = 0;
1057 #if defined(__mc68000__) || defined(CONFIG_APUS)
1058                 sa = IRQF_SHARED;
1059 #endif /* __mc68000__ || CONFIG_APUS */
1060
1061                 if (IDE_CHIPSET_IS_PCI(hwif->chipset))
1062                         sa = IRQF_SHARED;
1063
1064                 if (hwif->io_ports[IDE_CONTROL_OFFSET])
1065                         /* clear nIEN */
1066                         hwif->OUTB(0x08, hwif->io_ports[IDE_CONTROL_OFFSET]);
1067
1068                 if (request_irq(hwif->irq,&ide_intr,sa,hwif->name,hwgroup))
1069                         goto out_unlink;
1070         }
1071
1072         /*
1073          * For any present drive:
1074          * - allocate the block device queue
1075          * - link drive into the hwgroup
1076          */
1077         for (index = 0; index < MAX_DRIVES; ++index) {
1078                 ide_drive_t *drive = &hwif->drives[index];
1079                 if (!drive->present)
1080                         continue;
1081                 if (ide_init_queue(drive)) {
1082                         printk(KERN_ERR "ide: failed to init %s\n",drive->name);
1083                         continue;
1084                 }
1085                 spin_lock_irq(&ide_lock);
1086                 if (!hwgroup->drive) {
1087                         /* first drive for hwgroup. */
1088                         drive->next = drive;
1089                         hwgroup->drive = drive;
1090                         hwgroup->hwif = HWIF(hwgroup->drive);
1091                 } else {
1092                         drive->next = hwgroup->drive->next;
1093                         hwgroup->drive->next = drive;
1094                 }
1095                 spin_unlock_irq(&ide_lock);
1096         }
1097
1098 #if !defined(__mc68000__) && !defined(CONFIG_APUS)
1099         printk("%s at 0x%03lx-0x%03lx,0x%03lx on irq %d", hwif->name,
1100                 hwif->io_ports[IDE_DATA_OFFSET],
1101                 hwif->io_ports[IDE_DATA_OFFSET]+7,
1102                 hwif->io_ports[IDE_CONTROL_OFFSET], hwif->irq);
1103 #else
1104         printk("%s at 0x%08lx on irq %d", hwif->name,
1105                 hwif->io_ports[IDE_DATA_OFFSET], hwif->irq);
1106 #endif /* __mc68000__ && CONFIG_APUS */
1107         if (match)
1108                 printk(" (%sed with %s)",
1109                         hwif->sharing_irq ? "shar" : "serializ", match->name);
1110         printk("\n");
1111         mutex_unlock(&ide_cfg_mtx);
1112         return 0;
1113 out_unlink:
1114         spin_lock_irq(&ide_lock);
1115         if (hwif->next == hwif) {
1116                 BUG_ON(match);
1117                 BUG_ON(hwgroup->hwif != hwif);
1118                 kfree(hwgroup);
1119         } else {
1120                 ide_hwif_t *g;
1121                 g = hwgroup->hwif;
1122                 while (g->next != hwif)
1123                         g = g->next;
1124                 g->next = hwif->next;
1125                 if (hwgroup->hwif == hwif) {
1126                         /* Impossible. */
1127                         printk(KERN_ERR "Duh. Uninitialized hwif listed as active hwif.\n");
1128                         hwgroup->hwif = g;
1129                 }
1130                 BUG_ON(hwgroup->hwif == hwif);
1131         }
1132         spin_unlock_irq(&ide_lock);
1133 out_up:
1134         mutex_unlock(&ide_cfg_mtx);
1135         return 1;
1136 }
1137
1138 static int ata_lock(dev_t dev, void *data)
1139 {
1140         /* FIXME: we want to pin hwif down */
1141         return 0;
1142 }
1143
1144 static struct kobject *ata_probe(dev_t dev, int *part, void *data)
1145 {
1146         ide_hwif_t *hwif = data;
1147         int unit = *part >> PARTN_BITS;
1148         ide_drive_t *drive = &hwif->drives[unit];
1149         if (!drive->present)
1150                 return NULL;
1151
1152         if (drive->media == ide_disk)
1153                 request_module("ide-disk");
1154         if (drive->scsi)
1155                 request_module("ide-scsi");
1156         if (drive->media == ide_cdrom || drive->media == ide_optical)
1157                 request_module("ide-cd");
1158         if (drive->media == ide_tape)
1159                 request_module("ide-tape");
1160         if (drive->media == ide_floppy)
1161                 request_module("ide-floppy");
1162
1163         return NULL;
1164 }
1165
1166 static struct kobject *exact_match(dev_t dev, int *part, void *data)
1167 {
1168         struct gendisk *p = data;
1169         *part &= (1 << PARTN_BITS) - 1;
1170         return &p->dev.kobj;
1171 }
1172
1173 static int exact_lock(dev_t dev, void *data)
1174 {
1175         struct gendisk *p = data;
1176
1177         if (!get_disk(p))
1178                 return -1;
1179         return 0;
1180 }
1181
1182 void ide_register_region(struct gendisk *disk)
1183 {
1184         blk_register_region(MKDEV(disk->major, disk->first_minor),
1185                             disk->minors, NULL, exact_match, exact_lock, disk);
1186 }
1187
1188 EXPORT_SYMBOL_GPL(ide_register_region);
1189
1190 void ide_unregister_region(struct gendisk *disk)
1191 {
1192         blk_unregister_region(MKDEV(disk->major, disk->first_minor),
1193                               disk->minors);
1194 }
1195
1196 EXPORT_SYMBOL_GPL(ide_unregister_region);
1197
1198 void ide_init_disk(struct gendisk *disk, ide_drive_t *drive)
1199 {
1200         ide_hwif_t *hwif = drive->hwif;
1201         unsigned int unit = (drive->select.all >> 4) & 1;
1202
1203         disk->major = hwif->major;
1204         disk->first_minor = unit << PARTN_BITS;
1205         sprintf(disk->disk_name, "hd%c", 'a' + hwif->index * MAX_DRIVES + unit);
1206         disk->queue = drive->queue;
1207 }
1208
1209 EXPORT_SYMBOL_GPL(ide_init_disk);
1210
1211 static void ide_remove_drive_from_hwgroup(ide_drive_t *drive)
1212 {
1213         ide_hwgroup_t *hwgroup = drive->hwif->hwgroup;
1214
1215         if (drive == drive->next) {
1216                 /* special case: last drive from hwgroup. */
1217                 BUG_ON(hwgroup->drive != drive);
1218                 hwgroup->drive = NULL;
1219         } else {
1220                 ide_drive_t *walk;
1221
1222                 walk = hwgroup->drive;
1223                 while (walk->next != drive)
1224                         walk = walk->next;
1225                 walk->next = drive->next;
1226                 if (hwgroup->drive == drive) {
1227                         hwgroup->drive = drive->next;
1228                         hwgroup->hwif = hwgroup->drive->hwif;
1229                 }
1230         }
1231         BUG_ON(hwgroup->drive == drive);
1232 }
1233
1234 static void drive_release_dev (struct device *dev)
1235 {
1236         ide_drive_t *drive = container_of(dev, ide_drive_t, gendev);
1237
1238         spin_lock_irq(&ide_lock);
1239         ide_remove_drive_from_hwgroup(drive);
1240         kfree(drive->id);
1241         drive->id = NULL;
1242         drive->present = 0;
1243         /* Messed up locking ... */
1244         spin_unlock_irq(&ide_lock);
1245         blk_cleanup_queue(drive->queue);
1246         spin_lock_irq(&ide_lock);
1247         drive->queue = NULL;
1248         spin_unlock_irq(&ide_lock);
1249
1250         complete(&drive->gendev_rel_comp);
1251 }
1252
1253 /*
1254  * init_gendisk() (as opposed to ide_geninit) is called for each major device,
1255  * after probing for drives, to allocate partition tables and other data
1256  * structures needed for the routines in genhd.c.  ide_geninit() gets called
1257  * somewhat later, during the partition check.
1258  */
1259 static void init_gendisk (ide_hwif_t *hwif)
1260 {
1261         unsigned int unit;
1262
1263         for (unit = 0; unit < MAX_DRIVES; ++unit) {
1264                 ide_drive_t * drive = &hwif->drives[unit];
1265                 ide_add_generic_settings(drive);
1266                 snprintf(drive->gendev.bus_id,BUS_ID_SIZE,"%u.%u",
1267                          hwif->index,unit);
1268                 drive->gendev.parent = &hwif->gendev;
1269                 drive->gendev.bus = &ide_bus_type;
1270                 drive->gendev.driver_data = drive;
1271                 drive->gendev.release = drive_release_dev;
1272         }
1273         blk_register_region(MKDEV(hwif->major, 0), MAX_DRIVES << PARTN_BITS,
1274                         THIS_MODULE, ata_probe, ata_lock, hwif);
1275 }
1276
1277 static int hwif_init(ide_hwif_t *hwif)
1278 {
1279         int old_irq;
1280
1281         /* Return success if no device is connected */
1282         if (!hwif->present)
1283                 return 1;
1284
1285         if (!hwif->irq) {
1286                 if (!(hwif->irq = ide_default_irq(hwif->io_ports[IDE_DATA_OFFSET])))
1287                 {
1288                         printk("%s: DISABLED, NO IRQ\n", hwif->name);
1289                         return (hwif->present = 0);
1290                 }
1291         }
1292 #ifdef CONFIG_BLK_DEV_HD
1293         if (hwif->irq == HD_IRQ && hwif->io_ports[IDE_DATA_OFFSET] != HD_DATA) {
1294                 printk("%s: CANNOT SHARE IRQ WITH OLD "
1295                         "HARDDISK DRIVER (hd.c)\n", hwif->name);
1296                 return (hwif->present = 0);
1297         }
1298 #endif /* CONFIG_BLK_DEV_HD */
1299
1300         /* we set it back to 1 if all is ok below */    
1301         hwif->present = 0;
1302
1303         if (register_blkdev(hwif->major, hwif->name))
1304                 return 0;
1305
1306         if (!hwif->sg_max_nents)
1307                 hwif->sg_max_nents = PRD_ENTRIES;
1308
1309         hwif->sg_table = kmalloc(sizeof(struct scatterlist)*hwif->sg_max_nents,
1310                                  GFP_KERNEL);
1311         if (!hwif->sg_table) {
1312                 printk(KERN_ERR "%s: unable to allocate SG table.\n", hwif->name);
1313                 goto out;
1314         }
1315
1316         sg_init_table(hwif->sg_table, hwif->sg_max_nents);
1317         
1318         if (init_irq(hwif) == 0)
1319                 goto done;
1320
1321         old_irq = hwif->irq;
1322         /*
1323          *      It failed to initialise. Find the default IRQ for 
1324          *      this port and try that.
1325          */
1326         if (!(hwif->irq = ide_default_irq(hwif->io_ports[IDE_DATA_OFFSET]))) {
1327                 printk("%s: Disabled unable to get IRQ %d.\n",
1328                         hwif->name, old_irq);
1329                 goto out;
1330         }
1331         if (init_irq(hwif)) {
1332                 printk("%s: probed IRQ %d and default IRQ %d failed.\n",
1333                         hwif->name, old_irq, hwif->irq);
1334                 goto out;
1335         }
1336         printk("%s: probed IRQ %d failed, using default.\n",
1337                 hwif->name, hwif->irq);
1338
1339 done:
1340         init_gendisk(hwif);
1341
1342         ide_acpi_init(hwif);
1343
1344         hwif->present = 1;      /* success */
1345         return 1;
1346
1347 out:
1348         unregister_blkdev(hwif->major, hwif->name);
1349         return 0;
1350 }
1351
1352 static void hwif_register_devices(ide_hwif_t *hwif)
1353 {
1354         unsigned int i;
1355
1356         for (i = 0; i < MAX_DRIVES; i++) {
1357                 ide_drive_t *drive = &hwif->drives[i];
1358
1359                 if (drive->present) {
1360                         int ret = device_register(&drive->gendev);
1361
1362                         if (ret < 0)
1363                                 printk(KERN_WARNING "IDE: %s: "
1364                                         "device_register error: %d\n",
1365                                         __FUNCTION__, ret);
1366                 }
1367         }
1368 }
1369
1370 int ideprobe_init (void)
1371 {
1372         unsigned int index;
1373         int probe[MAX_HWIFS];
1374
1375         memset(probe, 0, MAX_HWIFS * sizeof(int));
1376         for (index = 0; index < MAX_HWIFS; ++index)
1377                 probe[index] = !ide_hwifs[index].present;
1378
1379         for (index = 0; index < MAX_HWIFS; ++index)
1380                 if (probe[index])
1381                         probe_hwif(&ide_hwifs[index]);
1382         for (index = 0; index < MAX_HWIFS; ++index)
1383                 if (probe[index])
1384                         hwif_init(&ide_hwifs[index]);
1385         for (index = 0; index < MAX_HWIFS; ++index) {
1386                 if (probe[index]) {
1387                         ide_hwif_t *hwif = &ide_hwifs[index];
1388                         if (!hwif->present)
1389                                 continue;
1390                         if (hwif->chipset == ide_unknown || hwif->chipset == ide_forced)
1391                                 hwif->chipset = ide_generic;
1392                         hwif_register_devices(hwif);
1393                 }
1394         }
1395         for (index = 0; index < MAX_HWIFS; ++index)
1396                 if (probe[index])
1397                         ide_proc_register_port(&ide_hwifs[index]);
1398         return 0;
1399 }
1400
1401 EXPORT_SYMBOL_GPL(ideprobe_init);
1402
1403 int ide_device_add(u8 idx[4])
1404 {
1405         int i, rc = 0;
1406
1407         for (i = 0; i < 4; i++) {
1408                 if (idx[i] != 0xff)
1409                         rc |= probe_hwif_init(&ide_hwifs[idx[i]]);
1410         }
1411
1412         for (i = 0; i < 4; i++) {
1413                 if (idx[i] != 0xff)
1414                         ide_proc_register_port(&ide_hwifs[idx[i]]);
1415         }
1416
1417         return rc;
1418 }
1419
1420 EXPORT_SYMBOL_GPL(ide_device_add);