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