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