Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux...
[linux-2.6] / drivers / ide / ide-probe.c
1 /*
2  *  Copyright (C) 1994-1998   Linus Torvalds & authors (see below)
3  *  Copyright (C) 2005, 2007  Bartlomiej Zolnierkiewicz
4  */
5
6 /*
7  *  Mostly written by Mark Lord <mlord@pobox.com>
8  *                and Gadi Oxman <gadio@netvision.net.il>
9  *                and Andre Hedrick <andre@linux-ide.org>
10  *
11  *  See linux/MAINTAINERS for address of current maintainer.
12  *
13  * This is the IDE probe module, as evolved from hd.c and ide.c.
14  *
15  * -- increase WAIT_PIDENTIFY to avoid CD-ROM locking at boot
16  *       by Andrea Arcangeli
17  */
18
19 #include <linux/module.h>
20 #include <linux/types.h>
21 #include <linux/string.h>
22 #include <linux/kernel.h>
23 #include <linux/timer.h>
24 #include <linux/mm.h>
25 #include <linux/interrupt.h>
26 #include <linux/major.h>
27 #include <linux/errno.h>
28 #include <linux/genhd.h>
29 #include <linux/slab.h>
30 #include <linux/delay.h>
31 #include <linux/ide.h>
32 #include <linux/spinlock.h>
33 #include <linux/kmod.h>
34 #include <linux/pci.h>
35 #include <linux/scatterlist.h>
36
37 #include <asm/byteorder.h>
38 #include <asm/irq.h>
39 #include <asm/uaccess.h>
40 #include <asm/io.h>
41
42 /**
43  *      generic_id              -       add a generic drive id
44  *      @drive: drive to make an ID block for
45  *      
46  *      Add a fake id field to the drive we are passed. This allows
47  *      use to skip a ton of NULL checks (which people always miss) 
48  *      and make drive properties unconditional outside of this file
49  */
50  
51 static void generic_id(ide_drive_t *drive)
52 {
53         u16 *id = drive->id;
54
55         id[ATA_ID_CUR_CYLS]     = id[ATA_ID_CYLS]       = drive->cyl;
56         id[ATA_ID_CUR_HEADS]    = id[ATA_ID_HEADS]      = drive->head;
57         id[ATA_ID_CUR_SECTORS]  = id[ATA_ID_SECTORS]    = drive->sect;
58 }
59
60 static void ide_disk_init_chs(ide_drive_t *drive)
61 {
62         u16 *id = drive->id;
63
64         /* Extract geometry if we did not already have one for the drive */
65         if (!drive->cyl || !drive->head || !drive->sect) {
66                 drive->cyl  = drive->bios_cyl  = id[ATA_ID_CYLS];
67                 drive->head = drive->bios_head = id[ATA_ID_HEADS];
68                 drive->sect = drive->bios_sect = id[ATA_ID_SECTORS];
69         }
70
71         /* Handle logical geometry translation by the drive */
72         if (ata_id_current_chs_valid(id)) {
73                 drive->cyl  = id[ATA_ID_CUR_CYLS];
74                 drive->head = id[ATA_ID_CUR_HEADS];
75                 drive->sect = id[ATA_ID_CUR_SECTORS];
76         }
77
78         /* Use physical geometry if what we have still makes no sense */
79         if (drive->head > 16 && id[ATA_ID_HEADS] && id[ATA_ID_HEADS] <= 16) {
80                 drive->cyl  = id[ATA_ID_CYLS];
81                 drive->head = id[ATA_ID_HEADS];
82                 drive->sect = id[ATA_ID_SECTORS];
83         }
84 }
85
86 static void ide_disk_init_mult_count(ide_drive_t *drive)
87 {
88         u16 *id = drive->id;
89         u8 max_multsect = id[ATA_ID_MAX_MULTSECT] & 0xff;
90
91         if (max_multsect) {
92                 if ((max_multsect / 2) > 1)
93                         id[ATA_ID_MULTSECT] = max_multsect | 0x100;
94                 else
95                         id[ATA_ID_MULTSECT] &= ~0x1ff;
96
97                 drive->mult_req = id[ATA_ID_MULTSECT] & 0xff;
98
99                 if (drive->mult_req)
100                         drive->special.b.set_multmode = 1;
101         }
102 }
103
104 static void ide_classify_ata_dev(ide_drive_t *drive)
105 {
106         u16 *id = drive->id;
107         char *m = (char *)&id[ATA_ID_PROD];
108         int is_cfa = ata_id_is_cfa(id);
109
110         /* CF devices are *not* removable in Linux definition of the term */
111         if (is_cfa == 0 && (id[ATA_ID_CONFIG] & (1 << 7)))
112                 drive->dev_flags |= IDE_DFLAG_REMOVABLE;
113
114         drive->media = ide_disk;
115
116         if (!ata_id_has_unload(drive->id))
117                 drive->dev_flags |= IDE_DFLAG_NO_UNLOAD;
118
119         printk(KERN_INFO "%s: %s, %s DISK drive\n", drive->name, m,
120                 is_cfa ? "CFA" : "ATA");
121 }
122
123 static void ide_classify_atapi_dev(ide_drive_t *drive)
124 {
125         u16 *id = drive->id;
126         char *m = (char *)&id[ATA_ID_PROD];
127         u8 type = (id[ATA_ID_CONFIG] >> 8) & 0x1f;
128
129         printk(KERN_INFO "%s: %s, ATAPI ", drive->name, m);
130         switch (type) {
131         case ide_floppy:
132                 if (!strstr(m, "CD-ROM")) {
133                         if (!strstr(m, "oppy") &&
134                             !strstr(m, "poyp") &&
135                             !strstr(m, "ZIP"))
136                                 printk(KERN_CONT "cdrom or floppy?, assuming ");
137                         if (drive->media != ide_cdrom) {
138                                 printk(KERN_CONT "FLOPPY");
139                                 drive->dev_flags |= IDE_DFLAG_REMOVABLE;
140                                 break;
141                         }
142                 }
143                 /* Early cdrom models used zero */
144                 type = ide_cdrom;
145         case ide_cdrom:
146                 drive->dev_flags |= IDE_DFLAG_REMOVABLE;
147 #ifdef CONFIG_PPC
148                 /* kludge for Apple PowerBook internal zip */
149                 if (!strstr(m, "CD-ROM") && strstr(m, "ZIP")) {
150                         printk(KERN_CONT "FLOPPY");
151                         type = ide_floppy;
152                         break;
153                 }
154 #endif
155                 printk(KERN_CONT "CD/DVD-ROM");
156                 break;
157         case ide_tape:
158                 printk(KERN_CONT "TAPE");
159                 break;
160         case ide_optical:
161                 printk(KERN_CONT "OPTICAL");
162                 drive->dev_flags |= IDE_DFLAG_REMOVABLE;
163                 break;
164         default:
165                 printk(KERN_CONT "UNKNOWN (type %d)", type);
166                 break;
167         }
168
169         printk(KERN_CONT " drive\n");
170         drive->media = type;
171         /* an ATAPI device ignores DRDY */
172         drive->ready_stat = 0;
173         if (ata_id_cdb_intr(id))
174                 drive->atapi_flags |= IDE_AFLAG_DRQ_INTERRUPT;
175         drive->dev_flags |= IDE_DFLAG_DOORLOCKING;
176         /* we don't do head unloading on ATAPI devices */
177         drive->dev_flags |= IDE_DFLAG_NO_UNLOAD;
178 }
179
180 /**
181  *      do_identify     -       identify a drive
182  *      @drive: drive to identify 
183  *      @cmd: command used
184  *
185  *      Called when we have issued a drive identify command to
186  *      read and parse the results. This function is run with
187  *      interrupts disabled. 
188  */
189
190 static void do_identify(ide_drive_t *drive, u8 cmd)
191 {
192         ide_hwif_t *hwif = drive->hwif;
193         u16 *id = drive->id;
194         char *m = (char *)&id[ATA_ID_PROD];
195         unsigned long flags;
196         int bswap = 1;
197
198         /* local CPU only; some systems need this */
199         local_irq_save(flags);
200         /* read 512 bytes of id info */
201         hwif->tp_ops->input_data(drive, NULL, id, SECTOR_SIZE);
202         local_irq_restore(flags);
203
204         drive->dev_flags |= IDE_DFLAG_ID_READ;
205 #ifdef DEBUG
206         printk(KERN_INFO "%s: dumping identify data\n", drive->name);
207         ide_dump_identify((u8 *)id);
208 #endif
209         ide_fix_driveid(id);
210
211         /*
212          *  ATA_CMD_ID_ATA returns little-endian info,
213          *  ATA_CMD_ID_ATAPI *usually* returns little-endian info.
214          */
215         if (cmd == ATA_CMD_ID_ATAPI) {
216                 if ((m[0] == 'N' && m[1] == 'E') ||  /* NEC */
217                     (m[0] == 'F' && m[1] == 'X') ||  /* Mitsumi */
218                     (m[0] == 'P' && m[1] == 'i'))    /* Pioneer */
219                         /* Vertos drives may still be weird */
220                         bswap ^= 1;
221         }
222
223         ide_fixstring(m, ATA_ID_PROD_LEN, bswap);
224         ide_fixstring((char *)&id[ATA_ID_FW_REV], ATA_ID_FW_REV_LEN, bswap);
225         ide_fixstring((char *)&id[ATA_ID_SERNO], ATA_ID_SERNO_LEN, bswap);
226
227         /* we depend on this a lot! */
228         m[ATA_ID_PROD_LEN - 1] = '\0';
229
230         if (strstr(m, "E X A B Y T E N E S T"))
231                 goto err_misc;
232
233         drive->dev_flags |= IDE_DFLAG_PRESENT;
234         drive->dev_flags &= ~IDE_DFLAG_DEAD;
235
236         /*
237          * Check for an ATAPI device
238          */
239         if (cmd == ATA_CMD_ID_ATAPI)
240                 ide_classify_atapi_dev(drive);
241         else
242         /*
243          * Not an ATAPI device: looks like a "regular" hard disk
244          */
245                 ide_classify_ata_dev(drive);
246         return;
247 err_misc:
248         kfree(id);
249         drive->dev_flags &= ~IDE_DFLAG_PRESENT;
250 }
251
252 /**
253  *      actual_try_to_identify  -       send ata/atapi identify
254  *      @drive: drive to identify
255  *      @cmd: command to use
256  *
257  *      try_to_identify() sends an ATA(PI) IDENTIFY request to a drive
258  *      and waits for a response.  It also monitors irqs while this is
259  *      happening, in hope of automatically determining which one is
260  *      being used by the interface.
261  *
262  *      Returns:        0  device was identified
263  *                      1  device timed-out (no response to identify request)
264  *                      2  device aborted the command (refused to identify itself)
265  */
266
267 static int actual_try_to_identify (ide_drive_t *drive, u8 cmd)
268 {
269         ide_hwif_t *hwif = drive->hwif;
270         struct ide_io_ports *io_ports = &hwif->io_ports;
271         const struct ide_tp_ops *tp_ops = hwif->tp_ops;
272         int use_altstatus = 0, rc;
273         unsigned long timeout;
274         u8 s = 0, a = 0;
275
276         /* take a deep breath */
277         msleep(50);
278
279         if (io_ports->ctl_addr &&
280             (hwif->host_flags & IDE_HFLAG_BROKEN_ALTSTATUS) == 0) {
281                 a = tp_ops->read_altstatus(hwif);
282                 s = tp_ops->read_status(hwif);
283                 if ((a ^ s) & ~ATA_IDX)
284                         /* ancient Seagate drives, broken interfaces */
285                         printk(KERN_INFO "%s: probing with STATUS(0x%02x) "
286                                          "instead of ALTSTATUS(0x%02x)\n",
287                                          drive->name, s, a);
288                 else
289                         /* use non-intrusive polling */
290                         use_altstatus = 1;
291         }
292
293         /* set features register for atapi
294          * identify command to be sure of reply
295          */
296         if (cmd == ATA_CMD_ID_ATAPI) {
297                 ide_task_t task;
298
299                 memset(&task, 0, sizeof(task));
300                 /* disable DMA & overlap */
301                 task.tf_flags = IDE_TFLAG_OUT_FEATURE;
302
303                 tp_ops->tf_load(drive, &task);
304         }
305
306         /* ask drive for ID */
307         tp_ops->exec_command(hwif, cmd);
308
309         timeout = ((cmd == ATA_CMD_ID_ATA) ? WAIT_WORSTCASE : WAIT_PIDENTIFY) / 2;
310
311         if (ide_busy_sleep(hwif, timeout, use_altstatus))
312                 return 1;
313
314         /* wait for IRQ and ATA_DRQ */
315         msleep(50);
316         s = tp_ops->read_status(hwif);
317
318         if (OK_STAT(s, ATA_DRQ, BAD_R_STAT)) {
319                 /* drive returned ID */
320                 do_identify(drive, cmd);
321                 /* drive responded with ID */
322                 rc = 0;
323                 /* clear drive IRQ */
324                 (void)tp_ops->read_status(hwif);
325         } else {
326                 /* drive refused ID */
327                 rc = 2;
328         }
329         return rc;
330 }
331
332 /**
333  *      try_to_identify -       try to identify a drive
334  *      @drive: drive to probe
335  *      @cmd: command to use
336  *
337  *      Issue the identify command and then do IRQ probing to
338  *      complete the identification when needed by finding the
339  *      IRQ the drive is attached to
340  */
341  
342 static int try_to_identify (ide_drive_t *drive, u8 cmd)
343 {
344         ide_hwif_t *hwif = drive->hwif;
345         const struct ide_tp_ops *tp_ops = hwif->tp_ops;
346         int retval;
347         int autoprobe = 0;
348         unsigned long cookie = 0;
349
350         /*
351          * Disable device irq unless we need to
352          * probe for it. Otherwise we'll get spurious
353          * interrupts during the identify-phase that
354          * the irq handler isn't expecting.
355          */
356         if (hwif->io_ports.ctl_addr) {
357                 if (!hwif->irq) {
358                         autoprobe = 1;
359                         cookie = probe_irq_on();
360                 }
361                 tp_ops->set_irq(hwif, autoprobe);
362         }
363
364         retval = actual_try_to_identify(drive, cmd);
365
366         if (autoprobe) {
367                 int irq;
368
369                 tp_ops->set_irq(hwif, 0);
370                 /* clear drive IRQ */
371                 (void)tp_ops->read_status(hwif);
372                 udelay(5);
373                 irq = probe_irq_off(cookie);
374                 if (!hwif->irq) {
375                         if (irq > 0) {
376                                 hwif->irq = irq;
377                         } else {
378                                 /* Mmmm.. multiple IRQs..
379                                  * don't know which was ours
380                                  */
381                                 printk(KERN_ERR "%s: IRQ probe failed (0x%lx)\n",
382                                         drive->name, cookie);
383                         }
384                 }
385         }
386         return retval;
387 }
388
389 int ide_busy_sleep(ide_hwif_t *hwif, unsigned long timeout, int altstatus)
390 {
391         u8 stat;
392
393         timeout += jiffies;
394
395         do {
396                 msleep(50);     /* give drive a breather */
397                 stat = altstatus ? hwif->tp_ops->read_altstatus(hwif)
398                                  : hwif->tp_ops->read_status(hwif);
399                 if ((stat & ATA_BUSY) == 0)
400                         return 0;
401         } while (time_before(jiffies, timeout));
402
403         return 1;       /* drive timed-out */
404 }
405
406 static u8 ide_read_device(ide_drive_t *drive)
407 {
408         ide_task_t task;
409
410         memset(&task, 0, sizeof(task));
411         task.tf_flags = IDE_TFLAG_IN_DEVICE;
412
413         drive->hwif->tp_ops->tf_read(drive, &task);
414
415         return task.tf.device;
416 }
417
418 /**
419  *      do_probe                -       probe an IDE device
420  *      @drive: drive to probe
421  *      @cmd: command to use
422  *
423  *      do_probe() has the difficult job of finding a drive if it exists,
424  *      without getting hung up if it doesn't exist, without trampling on
425  *      ethernet cards, and without leaving any IRQs dangling to haunt us later.
426  *
427  *      If a drive is "known" to exist (from CMOS or kernel parameters),
428  *      but does not respond right away, the probe will "hang in there"
429  *      for the maximum wait time (about 30 seconds), otherwise it will
430  *      exit much more quickly.
431  *
432  * Returns:     0  device was identified
433  *              1  device timed-out (no response to identify request)
434  *              2  device aborted the command (refused to identify itself)
435  *              3  bad status from device (possible for ATAPI drives)
436  *              4  probe was not attempted because failure was obvious
437  */
438
439 static int do_probe (ide_drive_t *drive, u8 cmd)
440 {
441         ide_hwif_t *hwif = drive->hwif;
442         const struct ide_tp_ops *tp_ops = hwif->tp_ops;
443         int rc;
444         u8 present = !!(drive->dev_flags & IDE_DFLAG_PRESENT), stat;
445
446         /* avoid waiting for inappropriate probes */
447         if (present && drive->media != ide_disk && cmd == ATA_CMD_ID_ATA)
448                 return 4;
449
450 #ifdef DEBUG
451         printk(KERN_INFO "probing for %s: present=%d, media=%d, probetype=%s\n",
452                 drive->name, present, drive->media,
453                 (cmd == ATA_CMD_ID_ATA) ? "ATA" : "ATAPI");
454 #endif
455
456         /* needed for some systems
457          * (e.g. crw9624 as drive0 with disk as slave)
458          */
459         msleep(50);
460         SELECT_DRIVE(drive);
461         msleep(50);
462
463         if (ide_read_device(drive) != drive->select && present == 0) {
464                 if (drive->dn & 1) {
465                         /* exit with drive0 selected */
466                         SELECT_DRIVE(hwif->devices[0]);
467                         /* allow ATA_BUSY to assert & clear */
468                         msleep(50);
469                 }
470                 /* no i/f present: mmm.. this should be a 4 -ml */
471                 return 3;
472         }
473
474         stat = tp_ops->read_status(hwif);
475
476         if (OK_STAT(stat, ATA_DRDY, ATA_BUSY) ||
477             present || cmd == ATA_CMD_ID_ATAPI) {
478                 /* send cmd and wait */
479                 if ((rc = try_to_identify(drive, cmd))) {
480                         /* failed: try again */
481                         rc = try_to_identify(drive,cmd);
482                 }
483
484                 stat = tp_ops->read_status(hwif);
485
486                 if (stat == (ATA_BUSY | ATA_DRDY))
487                         return 4;
488
489                 if (rc == 1 && cmd == ATA_CMD_ID_ATAPI) {
490                         printk(KERN_ERR "%s: no response (status = 0x%02x), "
491                                         "resetting drive\n", drive->name, stat);
492                         msleep(50);
493                         SELECT_DRIVE(drive);
494                         msleep(50);
495                         tp_ops->exec_command(hwif, ATA_CMD_DEV_RESET);
496                         (void)ide_busy_sleep(hwif, WAIT_WORSTCASE, 0);
497                         rc = try_to_identify(drive, cmd);
498                 }
499
500                 /* ensure drive IRQ is clear */
501                 stat = tp_ops->read_status(hwif);
502
503                 if (rc == 1)
504                         printk(KERN_ERR "%s: no response (status = 0x%02x)\n",
505                                         drive->name, stat);
506         } else {
507                 /* not present or maybe ATAPI */
508                 rc = 3;
509         }
510         if (drive->dn & 1) {
511                 /* exit with drive0 selected */
512                 SELECT_DRIVE(hwif->devices[0]);
513                 msleep(50);
514                 /* ensure drive irq is clear */
515                 (void)tp_ops->read_status(hwif);
516         }
517         return rc;
518 }
519
520 /*
521  *
522  */
523 static void enable_nest (ide_drive_t *drive)
524 {
525         ide_hwif_t *hwif = drive->hwif;
526         const struct ide_tp_ops *tp_ops = hwif->tp_ops;
527         u8 stat;
528
529         printk(KERN_INFO "%s: enabling %s -- ",
530                 hwif->name, (char *)&drive->id[ATA_ID_PROD]);
531
532         SELECT_DRIVE(drive);
533         msleep(50);
534         tp_ops->exec_command(hwif, ATA_EXABYTE_ENABLE_NEST);
535
536         if (ide_busy_sleep(hwif, WAIT_WORSTCASE, 0)) {
537                 printk(KERN_CONT "failed (timeout)\n");
538                 return;
539         }
540
541         msleep(50);
542
543         stat = tp_ops->read_status(hwif);
544
545         if (!OK_STAT(stat, 0, BAD_STAT))
546                 printk(KERN_CONT "failed (status = 0x%02x)\n", stat);
547         else
548                 printk(KERN_CONT "success\n");
549 }
550
551 /**
552  *      probe_for_drives        -       upper level drive probe
553  *      @drive: drive to probe for
554  *
555  *      probe_for_drive() tests for existence of a given drive using do_probe()
556  *      and presents things to the user as needed.
557  *
558  *      Returns:        0  no device was found
559  *                      1  device was found
560  *                         (note: IDE_DFLAG_PRESENT might still be not set)
561  */
562
563 static u8 probe_for_drive(ide_drive_t *drive)
564 {
565         char *m;
566
567         /*
568          *      In order to keep things simple we have an id
569          *      block for all drives at all times. If the device
570          *      is pre ATA or refuses ATA/ATAPI identify we
571          *      will add faked data to this.
572          *
573          *      Also note that 0 everywhere means "can't do X"
574          */
575  
576         drive->dev_flags &= ~IDE_DFLAG_ID_READ;
577
578         drive->id = kzalloc(SECTOR_SIZE, GFP_KERNEL);
579         if (drive->id == NULL) {
580                 printk(KERN_ERR "ide: out of memory for id data.\n");
581                 return 0;
582         }
583
584         m = (char *)&drive->id[ATA_ID_PROD];
585         strcpy(m, "UNKNOWN");
586
587         /* skip probing? */
588         if ((drive->dev_flags & IDE_DFLAG_NOPROBE) == 0) {
589 retry:
590                 /* if !(success||timed-out) */
591                 if (do_probe(drive, ATA_CMD_ID_ATA) >= 2)
592                         /* look for ATAPI device */
593                         (void)do_probe(drive, ATA_CMD_ID_ATAPI);
594
595                 if ((drive->dev_flags & IDE_DFLAG_PRESENT) == 0)
596                         /* drive not found */
597                         return 0;
598
599                 if (strstr(m, "E X A B Y T E N E S T")) {
600                         enable_nest(drive);
601                         goto retry;
602                 }
603
604                 /* identification failed? */
605                 if ((drive->dev_flags & IDE_DFLAG_ID_READ) == 0) {
606                         if (drive->media == ide_disk) {
607                                 printk(KERN_INFO "%s: non-IDE drive, CHS=%d/%d/%d\n",
608                                         drive->name, drive->cyl,
609                                         drive->head, drive->sect);
610                         } else if (drive->media == ide_cdrom) {
611                                 printk(KERN_INFO "%s: ATAPI cdrom (?)\n", drive->name);
612                         } else {
613                                 /* nuke it */
614                                 printk(KERN_WARNING "%s: Unknown device on bus refused identification. Ignoring.\n", drive->name);
615                                 drive->dev_flags &= ~IDE_DFLAG_PRESENT;
616                         }
617                 }
618                 /* drive was found */
619         }
620
621         if ((drive->dev_flags & IDE_DFLAG_PRESENT) == 0)
622                 return 0;
623
624         /* The drive wasn't being helpful. Add generic info only */
625         if ((drive->dev_flags & IDE_DFLAG_ID_READ) == 0) {
626                 generic_id(drive);
627                 return 1;
628         }
629
630         if (drive->media == ide_disk) {
631                 ide_disk_init_chs(drive);
632                 ide_disk_init_mult_count(drive);
633         }
634
635         return !!(drive->dev_flags & IDE_DFLAG_PRESENT);
636 }
637
638 static void hwif_release_dev(struct device *dev)
639 {
640         ide_hwif_t *hwif = container_of(dev, ide_hwif_t, gendev);
641
642         complete(&hwif->gendev_rel_comp);
643 }
644
645 static int ide_register_port(ide_hwif_t *hwif)
646 {
647         int ret;
648
649         /* register with global device tree */
650         dev_set_name(&hwif->gendev, hwif->name);
651         hwif->gendev.driver_data = hwif;
652         hwif->gendev.parent = hwif->dev;
653         hwif->gendev.release = hwif_release_dev;
654
655         ret = device_register(&hwif->gendev);
656         if (ret < 0) {
657                 printk(KERN_WARNING "IDE: %s: device_register error: %d\n",
658                         __func__, ret);
659                 goto out;
660         }
661
662         hwif->portdev = device_create(ide_port_class, &hwif->gendev,
663                                       MKDEV(0, 0), hwif, hwif->name);
664         if (IS_ERR(hwif->portdev)) {
665                 ret = PTR_ERR(hwif->portdev);
666                 device_unregister(&hwif->gendev);
667         }
668 out:
669         return ret;
670 }
671
672 /**
673  *      ide_port_wait_ready     -       wait for port to become ready
674  *      @hwif: IDE port
675  *
676  *      This is needed on some PPCs and a bunch of BIOS-less embedded
677  *      platforms.  Typical cases are:
678  *
679  *      - The firmware hard reset the disk before booting the kernel,
680  *        the drive is still doing it's poweron-reset sequence, that
681  *        can take up to 30 seconds.
682  *
683  *      - The firmware does nothing (or no firmware), the device is
684  *        still in POST state (same as above actually).
685  *
686  *      - Some CD/DVD/Writer combo drives tend to drive the bus during
687  *        their reset sequence even when they are non-selected slave
688  *        devices, thus preventing discovery of the main HD.
689  *
690  *      Doing this wait-for-non-busy should not harm any existing
691  *      configuration and fix some issues like the above.
692  *
693  *      BenH.
694  *
695  *      Returns 0 on success, error code (< 0) otherwise.
696  */
697
698 static int ide_port_wait_ready(ide_hwif_t *hwif)
699 {
700         ide_drive_t *drive;
701         int i, rc;
702
703         printk(KERN_DEBUG "Probing IDE interface %s...\n", hwif->name);
704
705         /* Let HW settle down a bit from whatever init state we
706          * come from */
707         mdelay(2);
708
709         /* Wait for BSY bit to go away, spec timeout is 30 seconds,
710          * I know of at least one disk who takes 31 seconds, I use 35
711          * here to be safe
712          */
713         rc = ide_wait_not_busy(hwif, 35000);
714         if (rc)
715                 return rc;
716
717         /* Now make sure both master & slave are ready */
718         ide_port_for_each_dev(i, drive, hwif) {
719                 /* Ignore disks that we will not probe for later. */
720                 if ((drive->dev_flags & IDE_DFLAG_NOPROBE) == 0 ||
721                     (drive->dev_flags & IDE_DFLAG_PRESENT)) {
722                         SELECT_DRIVE(drive);
723                         hwif->tp_ops->set_irq(hwif, 1);
724                         mdelay(2);
725                         rc = ide_wait_not_busy(hwif, 35000);
726                         if (rc)
727                                 goto out;
728                 } else
729                         printk(KERN_DEBUG "%s: ide_wait_not_busy() skipped\n",
730                                           drive->name);
731         }
732 out:
733         /* Exit function with master reselected (let's be sane) */
734         if (i)
735                 SELECT_DRIVE(hwif->devices[0]);
736
737         return rc;
738 }
739
740 /**
741  *      ide_undecoded_slave     -       look for bad CF adapters
742  *      @dev1: slave device
743  *
744  *      Analyse the drives on the interface and attempt to decide if we
745  *      have the same drive viewed twice. This occurs with crap CF adapters
746  *      and PCMCIA sometimes.
747  */
748
749 void ide_undecoded_slave(ide_drive_t *dev1)
750 {
751         ide_drive_t *dev0 = dev1->hwif->devices[0];
752
753         if ((dev1->dn & 1) == 0 || (dev0->dev_flags & IDE_DFLAG_PRESENT) == 0)
754                 return;
755
756         /* If the models don't match they are not the same product */
757         if (strcmp((char *)&dev0->id[ATA_ID_PROD],
758                    (char *)&dev1->id[ATA_ID_PROD]))
759                 return;
760
761         /* Serial numbers do not match */
762         if (strncmp((char *)&dev0->id[ATA_ID_SERNO],
763                     (char *)&dev1->id[ATA_ID_SERNO], ATA_ID_SERNO_LEN))
764                 return;
765
766         /* No serial number, thankfully very rare for CF */
767         if (*(char *)&dev0->id[ATA_ID_SERNO] == 0)
768                 return;
769
770         /* Appears to be an IDE flash adapter with decode bugs */
771         printk(KERN_WARNING "ide-probe: ignoring undecoded slave\n");
772
773         dev1->dev_flags &= ~IDE_DFLAG_PRESENT;
774 }
775
776 EXPORT_SYMBOL_GPL(ide_undecoded_slave);
777
778 static int ide_probe_port(ide_hwif_t *hwif)
779 {
780         ide_drive_t *drive;
781         unsigned long flags;
782         unsigned int irqd;
783         int i, rc = -ENODEV;
784
785         BUG_ON(hwif->present);
786
787         if ((hwif->devices[0]->dev_flags & IDE_DFLAG_NOPROBE) &&
788             (hwif->devices[1]->dev_flags & IDE_DFLAG_NOPROBE))
789                 return -EACCES;
790
791         /*
792          * We must always disable IRQ, as probe_for_drive will assert IRQ, but
793          * we'll install our IRQ driver much later...
794          */
795         irqd = hwif->irq;
796         if (irqd)
797                 disable_irq(hwif->irq);
798
799         local_save_flags(flags);
800         local_irq_enable_in_hardirq();
801
802         if (ide_port_wait_ready(hwif) == -EBUSY)
803                 printk(KERN_DEBUG "%s: Wait for ready failed before probe !\n", hwif->name);
804
805         /*
806          * Second drive should only exist if first drive was found,
807          * but a lot of cdrom drives are configured as single slaves.
808          */
809         ide_port_for_each_dev(i, drive, hwif) {
810                 (void) probe_for_drive(drive);
811                 if (drive->dev_flags & IDE_DFLAG_PRESENT)
812                         rc = 0;
813         }
814
815         local_irq_restore(flags);
816
817         /*
818          * Use cached IRQ number. It might be (and is...) changed by probe
819          * code above
820          */
821         if (irqd)
822                 enable_irq(irqd);
823
824         return rc;
825 }
826
827 static void ide_port_tune_devices(ide_hwif_t *hwif)
828 {
829         const struct ide_port_ops *port_ops = hwif->port_ops;
830         ide_drive_t *drive;
831         int i;
832
833         ide_port_for_each_dev(i, drive, hwif) {
834                 if (drive->dev_flags & IDE_DFLAG_PRESENT) {
835                         if (port_ops && port_ops->quirkproc)
836                                 port_ops->quirkproc(drive);
837                 }
838         }
839
840         ide_port_for_each_dev(i, drive, hwif) {
841                 if (drive->dev_flags & IDE_DFLAG_PRESENT) {
842                         ide_set_max_pio(drive);
843
844                         drive->dev_flags |= IDE_DFLAG_NICE1;
845
846                         if (hwif->dma_ops)
847                                 ide_set_dma(drive);
848                 }
849         }
850
851         ide_port_for_each_dev(i, drive, hwif) {
852                 if (hwif->host_flags & IDE_HFLAG_NO_IO_32BIT)
853                         drive->dev_flags |= IDE_DFLAG_NO_IO_32BIT;
854                 else
855                         drive->dev_flags &= ~IDE_DFLAG_NO_IO_32BIT;
856         }
857 }
858
859 /*
860  * init request queue
861  */
862 static int ide_init_queue(ide_drive_t *drive)
863 {
864         struct request_queue *q;
865         ide_hwif_t *hwif = drive->hwif;
866         int max_sectors = 256;
867         int max_sg_entries = PRD_ENTRIES;
868
869         /*
870          *      Our default set up assumes the normal IDE case,
871          *      that is 64K segmenting, standard PRD setup
872          *      and LBA28. Some drivers then impose their own
873          *      limits and LBA48 we could raise it but as yet
874          *      do not.
875          */
876
877         q = blk_init_queue_node(do_ide_request, NULL, hwif_to_node(hwif));
878         if (!q)
879                 return 1;
880
881         q->queuedata = drive;
882         blk_queue_segment_boundary(q, 0xffff);
883
884         if (hwif->rqsize < max_sectors)
885                 max_sectors = hwif->rqsize;
886         blk_queue_max_sectors(q, max_sectors);
887
888 #ifdef CONFIG_PCI
889         /* When we have an IOMMU, we may have a problem where pci_map_sg()
890          * creates segments that don't completely match our boundary
891          * requirements and thus need to be broken up again. Because it
892          * doesn't align properly either, we may actually have to break up
893          * to more segments than what was we got in the first place, a max
894          * worst case is twice as many.
895          * This will be fixed once we teach pci_map_sg() about our boundary
896          * requirements, hopefully soon. *FIXME*
897          */
898         if (!PCI_DMA_BUS_IS_PHYS)
899                 max_sg_entries >>= 1;
900 #endif /* CONFIG_PCI */
901
902         blk_queue_max_hw_segments(q, max_sg_entries);
903         blk_queue_max_phys_segments(q, max_sg_entries);
904
905         /* assign drive queue */
906         drive->queue = q;
907
908         /* needs drive->queue to be set */
909         ide_toggle_bounce(drive, 1);
910
911         return 0;
912 }
913
914 static DEFINE_MUTEX(ide_cfg_mtx);
915
916 /*
917  * For any present drive:
918  * - allocate the block device queue
919  */
920 static int ide_port_setup_devices(ide_hwif_t *hwif)
921 {
922         ide_drive_t *drive;
923         int i, j = 0;
924
925         mutex_lock(&ide_cfg_mtx);
926         ide_port_for_each_dev(i, drive, hwif) {
927                 if ((drive->dev_flags & IDE_DFLAG_PRESENT) == 0)
928                         continue;
929
930                 if (ide_init_queue(drive)) {
931                         printk(KERN_ERR "ide: failed to init %s\n",
932                                         drive->name);
933                         kfree(drive->id);
934                         drive->id = NULL;
935                         drive->dev_flags &= ~IDE_DFLAG_PRESENT;
936                         continue;
937                 }
938
939                 j++;
940         }
941         mutex_unlock(&ide_cfg_mtx);
942
943         return j;
944 }
945
946 /*
947  * This routine sets up the IRQ for an IDE interface.
948  */
949 static int init_irq (ide_hwif_t *hwif)
950 {
951         struct ide_io_ports *io_ports = &hwif->io_ports;
952         int sa = 0;
953
954         mutex_lock(&ide_cfg_mtx);
955         spin_lock_init(&hwif->lock);
956
957         init_timer(&hwif->timer);
958         hwif->timer.function = &ide_timer_expiry;
959         hwif->timer.data = (unsigned long)hwif;
960
961 #if defined(__mc68000__)
962         sa = IRQF_SHARED;
963 #endif /* __mc68000__ */
964
965         if (hwif->chipset == ide_pci)
966                 sa = IRQF_SHARED;
967
968         if (io_ports->ctl_addr)
969                 hwif->tp_ops->set_irq(hwif, 1);
970
971         if (request_irq(hwif->irq, &ide_intr, sa, hwif->name, hwif))
972                 goto out_up;
973
974         if (!hwif->rqsize) {
975                 if ((hwif->host_flags & IDE_HFLAG_NO_LBA48) ||
976                     (hwif->host_flags & IDE_HFLAG_NO_LBA48_DMA))
977                         hwif->rqsize = 256;
978                 else
979                         hwif->rqsize = 65536;
980         }
981
982 #if !defined(__mc68000__)
983         printk(KERN_INFO "%s at 0x%03lx-0x%03lx,0x%03lx on irq %d", hwif->name,
984                 io_ports->data_addr, io_ports->status_addr,
985                 io_ports->ctl_addr, hwif->irq);
986 #else
987         printk(KERN_INFO "%s at 0x%08lx on irq %d", hwif->name,
988                 io_ports->data_addr, hwif->irq);
989 #endif /* __mc68000__ */
990         if (hwif->host->host_flags & IDE_HFLAG_SERIALIZE)
991                 printk(KERN_CONT " (serialized)");
992         printk(KERN_CONT "\n");
993
994         mutex_unlock(&ide_cfg_mtx);
995         return 0;
996 out_up:
997         mutex_unlock(&ide_cfg_mtx);
998         return 1;
999 }
1000
1001 static int ata_lock(dev_t dev, void *data)
1002 {
1003         /* FIXME: we want to pin hwif down */
1004         return 0;
1005 }
1006
1007 static struct kobject *ata_probe(dev_t dev, int *part, void *data)
1008 {
1009         ide_hwif_t *hwif = data;
1010         int unit = *part >> PARTN_BITS;
1011         ide_drive_t *drive = hwif->devices[unit];
1012
1013         if ((drive->dev_flags & IDE_DFLAG_PRESENT) == 0)
1014                 return NULL;
1015
1016         if (drive->media == ide_disk)
1017                 request_module("ide-disk");
1018         if (drive->media == ide_cdrom || drive->media == ide_optical)
1019                 request_module("ide-cd");
1020         if (drive->media == ide_tape)
1021                 request_module("ide-tape");
1022         if (drive->media == ide_floppy)
1023                 request_module("ide-floppy");
1024
1025         return NULL;
1026 }
1027
1028 static struct kobject *exact_match(dev_t dev, int *part, void *data)
1029 {
1030         struct gendisk *p = data;
1031         *part &= (1 << PARTN_BITS) - 1;
1032         return &disk_to_dev(p)->kobj;
1033 }
1034
1035 static int exact_lock(dev_t dev, void *data)
1036 {
1037         struct gendisk *p = data;
1038
1039         if (!get_disk(p))
1040                 return -1;
1041         return 0;
1042 }
1043
1044 void ide_register_region(struct gendisk *disk)
1045 {
1046         blk_register_region(MKDEV(disk->major, disk->first_minor),
1047                             disk->minors, NULL, exact_match, exact_lock, disk);
1048 }
1049
1050 EXPORT_SYMBOL_GPL(ide_register_region);
1051
1052 void ide_unregister_region(struct gendisk *disk)
1053 {
1054         blk_unregister_region(MKDEV(disk->major, disk->first_minor),
1055                               disk->minors);
1056 }
1057
1058 EXPORT_SYMBOL_GPL(ide_unregister_region);
1059
1060 void ide_init_disk(struct gendisk *disk, ide_drive_t *drive)
1061 {
1062         ide_hwif_t *hwif = drive->hwif;
1063         unsigned int unit = drive->dn & 1;
1064
1065         disk->major = hwif->major;
1066         disk->first_minor = unit << PARTN_BITS;
1067         sprintf(disk->disk_name, "hd%c", 'a' + hwif->index * MAX_DRIVES + unit);
1068         disk->queue = drive->queue;
1069 }
1070
1071 EXPORT_SYMBOL_GPL(ide_init_disk);
1072
1073 static void drive_release_dev (struct device *dev)
1074 {
1075         ide_drive_t *drive = container_of(dev, ide_drive_t, gendev);
1076         ide_hwif_t *hwif = drive->hwif;
1077
1078         ide_proc_unregister_device(drive);
1079
1080         spin_lock_irq(&hwif->lock);
1081         kfree(drive->id);
1082         drive->id = NULL;
1083         drive->dev_flags &= ~IDE_DFLAG_PRESENT;
1084         /* Messed up locking ... */
1085         spin_unlock_irq(&hwif->lock);
1086         blk_cleanup_queue(drive->queue);
1087         spin_lock_irq(&hwif->lock);
1088         drive->queue = NULL;
1089         spin_unlock_irq(&hwif->lock);
1090
1091         complete(&drive->gendev_rel_comp);
1092 }
1093
1094 static int hwif_init(ide_hwif_t *hwif)
1095 {
1096         int old_irq;
1097
1098         if (!hwif->irq) {
1099                 hwif->irq = __ide_default_irq(hwif->io_ports.data_addr);
1100                 if (!hwif->irq) {
1101                         printk(KERN_ERR "%s: disabled, no IRQ\n", hwif->name);
1102                         return 0;
1103                 }
1104         }
1105
1106         if (register_blkdev(hwif->major, hwif->name))
1107                 return 0;
1108
1109         if (!hwif->sg_max_nents)
1110                 hwif->sg_max_nents = PRD_ENTRIES;
1111
1112         hwif->sg_table = kmalloc(sizeof(struct scatterlist)*hwif->sg_max_nents,
1113                                  GFP_KERNEL);
1114         if (!hwif->sg_table) {
1115                 printk(KERN_ERR "%s: unable to allocate SG table.\n", hwif->name);
1116                 goto out;
1117         }
1118
1119         sg_init_table(hwif->sg_table, hwif->sg_max_nents);
1120         
1121         if (init_irq(hwif) == 0)
1122                 goto done;
1123
1124         old_irq = hwif->irq;
1125         /*
1126          *      It failed to initialise. Find the default IRQ for 
1127          *      this port and try that.
1128          */
1129         hwif->irq = __ide_default_irq(hwif->io_ports.data_addr);
1130         if (!hwif->irq) {
1131                 printk(KERN_ERR "%s: disabled, unable to get IRQ %d\n",
1132                         hwif->name, old_irq);
1133                 goto out;
1134         }
1135         if (init_irq(hwif)) {
1136                 printk(KERN_ERR "%s: probed IRQ %d and default IRQ %d failed\n",
1137                         hwif->name, old_irq, hwif->irq);
1138                 goto out;
1139         }
1140         printk(KERN_WARNING "%s: probed IRQ %d failed, using default\n",
1141                 hwif->name, hwif->irq);
1142
1143 done:
1144         blk_register_region(MKDEV(hwif->major, 0), MAX_DRIVES << PARTN_BITS,
1145                             THIS_MODULE, ata_probe, ata_lock, hwif);
1146         return 1;
1147
1148 out:
1149         unregister_blkdev(hwif->major, hwif->name);
1150         return 0;
1151 }
1152
1153 static void hwif_register_devices(ide_hwif_t *hwif)
1154 {
1155         ide_drive_t *drive;
1156         unsigned int i;
1157
1158         ide_port_for_each_dev(i, drive, hwif) {
1159                 struct device *dev = &drive->gendev;
1160                 int ret;
1161
1162                 if ((drive->dev_flags & IDE_DFLAG_PRESENT) == 0)
1163                         continue;
1164
1165                 dev_set_name(dev, "%u.%u", hwif->index, i);
1166                 dev->parent = &hwif->gendev;
1167                 dev->bus = &ide_bus_type;
1168                 dev->driver_data = drive;
1169                 dev->release = drive_release_dev;
1170
1171                 ret = device_register(dev);
1172                 if (ret < 0)
1173                         printk(KERN_WARNING "IDE: %s: device_register error: "
1174                                             "%d\n", __func__, ret);
1175         }
1176 }
1177
1178 static void ide_port_init_devices(ide_hwif_t *hwif)
1179 {
1180         const struct ide_port_ops *port_ops = hwif->port_ops;
1181         ide_drive_t *drive;
1182         int i;
1183
1184         ide_port_for_each_dev(i, drive, hwif) {
1185                 drive->dn = i + hwif->channel * 2;
1186
1187                 if (hwif->host_flags & IDE_HFLAG_IO_32BIT)
1188                         drive->io_32bit = 1;
1189                 if (hwif->host_flags & IDE_HFLAG_UNMASK_IRQS)
1190                         drive->dev_flags |= IDE_DFLAG_UNMASK;
1191                 if (hwif->host_flags & IDE_HFLAG_NO_UNMASK_IRQS)
1192                         drive->dev_flags |= IDE_DFLAG_NO_UNMASK;
1193
1194                 if (port_ops && port_ops->init_dev)
1195                         port_ops->init_dev(drive);
1196         }
1197 }
1198
1199 static void ide_init_port(ide_hwif_t *hwif, unsigned int port,
1200                           const struct ide_port_info *d)
1201 {
1202         hwif->channel = port;
1203
1204         if (d->chipset)
1205                 hwif->chipset = d->chipset;
1206
1207         if (d->init_iops)
1208                 d->init_iops(hwif);
1209
1210         if ((!hwif->irq && (d->host_flags & IDE_HFLAG_LEGACY_IRQS)) ||
1211             (d->host_flags & IDE_HFLAG_FORCE_LEGACY_IRQS))
1212                 hwif->irq = port ? 15 : 14;
1213
1214         /* ->host_flags may be set by ->init_iops (or even earlier...) */
1215         hwif->host_flags |= d->host_flags;
1216         hwif->pio_mask = d->pio_mask;
1217
1218         if (d->tp_ops)
1219                 hwif->tp_ops = d->tp_ops;
1220
1221         /* ->set_pio_mode for DTC2278 is currently limited to port 0 */
1222         if (hwif->chipset != ide_dtc2278 || hwif->channel == 0)
1223                 hwif->port_ops = d->port_ops;
1224
1225         hwif->swdma_mask = d->swdma_mask;
1226         hwif->mwdma_mask = d->mwdma_mask;
1227         hwif->ultra_mask = d->udma_mask;
1228
1229         if ((d->host_flags & IDE_HFLAG_NO_DMA) == 0) {
1230                 int rc;
1231
1232                 hwif->dma_ops = d->dma_ops;
1233
1234                 if (d->init_dma)
1235                         rc = d->init_dma(hwif, d);
1236                 else
1237                         rc = ide_hwif_setup_dma(hwif, d);
1238
1239                 if (rc < 0) {
1240                         printk(KERN_INFO "%s: DMA disabled\n", hwif->name);
1241
1242                         hwif->dma_ops = NULL;
1243                         hwif->dma_base = 0;
1244                         hwif->swdma_mask = 0;
1245                         hwif->mwdma_mask = 0;
1246                         hwif->ultra_mask = 0;
1247                 }
1248         }
1249
1250         if ((d->host_flags & IDE_HFLAG_SERIALIZE) ||
1251             ((d->host_flags & IDE_HFLAG_SERIALIZE_DMA) && hwif->dma_base))
1252                 hwif->host->host_flags |= IDE_HFLAG_SERIALIZE;
1253
1254         if (d->max_sectors)
1255                 hwif->rqsize = d->max_sectors;
1256
1257         /* call chipset specific routine for each enabled port */
1258         if (d->init_hwif)
1259                 d->init_hwif(hwif);
1260 }
1261
1262 static void ide_port_cable_detect(ide_hwif_t *hwif)
1263 {
1264         const struct ide_port_ops *port_ops = hwif->port_ops;
1265
1266         if (port_ops && port_ops->cable_detect && (hwif->ultra_mask & 0x78)) {
1267                 if (hwif->cbl != ATA_CBL_PATA40_SHORT)
1268                         hwif->cbl = port_ops->cable_detect(hwif);
1269         }
1270 }
1271
1272 static const u8 ide_hwif_to_major[] =
1273         { IDE0_MAJOR, IDE1_MAJOR, IDE2_MAJOR, IDE3_MAJOR, IDE4_MAJOR,
1274           IDE5_MAJOR, IDE6_MAJOR, IDE7_MAJOR, IDE8_MAJOR, IDE9_MAJOR };
1275
1276 static void ide_port_init_devices_data(ide_hwif_t *hwif)
1277 {
1278         ide_drive_t *drive;
1279         int i;
1280
1281         ide_port_for_each_dev(i, drive, hwif) {
1282                 u8 j = (hwif->index * MAX_DRIVES) + i;
1283
1284                 memset(drive, 0, sizeof(*drive));
1285
1286                 drive->media                    = ide_disk;
1287                 drive->select                   = (i << 4) | ATA_DEVICE_OBS;
1288                 drive->hwif                     = hwif;
1289                 drive->ready_stat               = ATA_DRDY;
1290                 drive->bad_wstat                = BAD_W_STAT;
1291                 drive->special.b.recalibrate    = 1;
1292                 drive->special.b.set_geometry   = 1;
1293                 drive->name[0]                  = 'h';
1294                 drive->name[1]                  = 'd';
1295                 drive->name[2]                  = 'a' + j;
1296                 drive->max_failures             = IDE_DEFAULT_MAX_FAILURES;
1297
1298                 INIT_LIST_HEAD(&drive->list);
1299                 init_completion(&drive->gendev_rel_comp);
1300         }
1301 }
1302
1303 static void ide_init_port_data(ide_hwif_t *hwif, unsigned int index)
1304 {
1305         /* fill in any non-zero initial values */
1306         hwif->index     = index;
1307         hwif->major     = ide_hwif_to_major[index];
1308
1309         hwif->name[0]   = 'i';
1310         hwif->name[1]   = 'd';
1311         hwif->name[2]   = 'e';
1312         hwif->name[3]   = '0' + index;
1313
1314         init_completion(&hwif->gendev_rel_comp);
1315
1316         hwif->tp_ops = &default_tp_ops;
1317
1318         ide_port_init_devices_data(hwif);
1319 }
1320
1321 static void ide_init_port_hw(ide_hwif_t *hwif, hw_regs_t *hw)
1322 {
1323         memcpy(&hwif->io_ports, &hw->io_ports, sizeof(hwif->io_ports));
1324         hwif->irq = hw->irq;
1325         hwif->chipset = hw->chipset;
1326         hwif->dev = hw->dev;
1327         hwif->gendev.parent = hw->parent ? hw->parent : hw->dev;
1328         hwif->ack_intr = hw->ack_intr;
1329         hwif->config_data = hw->config;
1330 }
1331
1332 static unsigned int ide_indexes;
1333
1334 /**
1335  *      ide_find_port_slot      -       find free port slot
1336  *      @d: IDE port info
1337  *
1338  *      Return the new port slot index or -ENOENT if we are out of free slots.
1339  */
1340
1341 static int ide_find_port_slot(const struct ide_port_info *d)
1342 {
1343         int idx = -ENOENT;
1344         u8 bootable = (d && (d->host_flags & IDE_HFLAG_NON_BOOTABLE)) ? 0 : 1;
1345         u8 i = (d && (d->host_flags & IDE_HFLAG_QD_2ND_PORT)) ? 1 : 0;;
1346
1347         /*
1348          * Claim an unassigned slot.
1349          *
1350          * Give preference to claiming other slots before claiming ide0/ide1,
1351          * just in case there's another interface yet-to-be-scanned
1352          * which uses ports 0x1f0/0x170 (the ide0/ide1 defaults).
1353          *
1354          * Unless there is a bootable card that does not use the standard
1355          * ports 0x1f0/0x170 (the ide0/ide1 defaults).
1356          */
1357         mutex_lock(&ide_cfg_mtx);
1358         if (bootable) {
1359                 if ((ide_indexes | i) != (1 << MAX_HWIFS) - 1)
1360                         idx = ffz(ide_indexes | i);
1361         } else {
1362                 if ((ide_indexes | 3) != (1 << MAX_HWIFS) - 1)
1363                         idx = ffz(ide_indexes | 3);
1364                 else if ((ide_indexes & 3) != 3)
1365                         idx = ffz(ide_indexes);
1366         }
1367         if (idx >= 0)
1368                 ide_indexes |= (1 << idx);
1369         mutex_unlock(&ide_cfg_mtx);
1370
1371         return idx;
1372 }
1373
1374 static void ide_free_port_slot(int idx)
1375 {
1376         mutex_lock(&ide_cfg_mtx);
1377         ide_indexes &= ~(1 << idx);
1378         mutex_unlock(&ide_cfg_mtx);
1379 }
1380
1381 static void ide_port_free_devices(ide_hwif_t *hwif)
1382 {
1383         ide_drive_t *drive;
1384         int i;
1385
1386         ide_port_for_each_dev(i, drive, hwif)
1387                 kfree(drive);
1388 }
1389
1390 static int ide_port_alloc_devices(ide_hwif_t *hwif, int node)
1391 {
1392         int i;
1393
1394         for (i = 0; i < MAX_DRIVES; i++) {
1395                 ide_drive_t *drive;
1396
1397                 drive = kzalloc_node(sizeof(*drive), GFP_KERNEL, node);
1398                 if (drive == NULL)
1399                         goto out_nomem;
1400
1401                 hwif->devices[i] = drive;
1402         }
1403         return 0;
1404
1405 out_nomem:
1406         ide_port_free_devices(hwif);
1407         return -ENOMEM;
1408 }
1409
1410 struct ide_host *ide_host_alloc(const struct ide_port_info *d, hw_regs_t **hws)
1411 {
1412         struct ide_host *host;
1413         struct device *dev = hws[0] ? hws[0]->dev : NULL;
1414         int node = dev ? dev_to_node(dev) : -1;
1415         int i;
1416
1417         host = kzalloc_node(sizeof(*host), GFP_KERNEL, node);
1418         if (host == NULL)
1419                 return NULL;
1420
1421         for (i = 0; i < MAX_HOST_PORTS; i++) {
1422                 ide_hwif_t *hwif;
1423                 int idx;
1424
1425                 if (hws[i] == NULL)
1426                         continue;
1427
1428                 hwif = kzalloc_node(sizeof(*hwif), GFP_KERNEL, node);
1429                 if (hwif == NULL)
1430                         continue;
1431
1432                 if (ide_port_alloc_devices(hwif, node) < 0) {
1433                         kfree(hwif);
1434                         continue;
1435                 }
1436
1437                 idx = ide_find_port_slot(d);
1438                 if (idx < 0) {
1439                         printk(KERN_ERR "%s: no free slot for interface\n",
1440                                         d ? d->name : "ide");
1441                         kfree(hwif);
1442                         continue;
1443                 }
1444
1445                 ide_init_port_data(hwif, idx);
1446
1447                 hwif->host = host;
1448
1449                 host->ports[i] = hwif;
1450                 host->n_ports++;
1451         }
1452
1453         if (host->n_ports == 0) {
1454                 kfree(host);
1455                 return NULL;
1456         }
1457
1458         host->dev[0] = dev;
1459
1460         if (d) {
1461                 host->init_chipset = d->init_chipset;
1462                 host->host_flags = d->host_flags;
1463         }
1464
1465         return host;
1466 }
1467 EXPORT_SYMBOL_GPL(ide_host_alloc);
1468
1469 int ide_host_register(struct ide_host *host, const struct ide_port_info *d,
1470                       hw_regs_t **hws)
1471 {
1472         ide_hwif_t *hwif, *mate = NULL;
1473         int i, j = 0;
1474
1475         ide_host_for_each_port(i, hwif, host) {
1476                 if (hwif == NULL) {
1477                         mate = NULL;
1478                         continue;
1479                 }
1480
1481                 ide_init_port_hw(hwif, hws[i]);
1482                 ide_port_apply_params(hwif);
1483
1484                 if (d == NULL) {
1485                         mate = NULL;
1486                 } else {
1487                         if ((i & 1) && mate) {
1488                                 hwif->mate = mate;
1489                                 mate->mate = hwif;
1490                         }
1491
1492                         mate = (i & 1) ? NULL : hwif;
1493
1494                         ide_init_port(hwif, i & 1, d);
1495                         ide_port_cable_detect(hwif);
1496                 }
1497
1498                 ide_port_init_devices(hwif);
1499         }
1500
1501         ide_host_for_each_port(i, hwif, host) {
1502                 if (hwif == NULL)
1503                         continue;
1504
1505                 if (ide_probe_port(hwif) == 0)
1506                         hwif->present = 1;
1507
1508                 if (hwif->chipset != ide_4drives || !hwif->mate ||
1509                     !hwif->mate->present)
1510                         ide_register_port(hwif);
1511
1512                 if (hwif->present)
1513                         ide_port_tune_devices(hwif);
1514         }
1515
1516         ide_host_for_each_port(i, hwif, host) {
1517                 if (hwif == NULL)
1518                         continue;
1519
1520                 if (hwif_init(hwif) == 0) {
1521                         printk(KERN_INFO "%s: failed to initialize IDE "
1522                                          "interface\n", hwif->name);
1523                         hwif->present = 0;
1524                         continue;
1525                 }
1526
1527                 if (hwif->present)
1528                         if (ide_port_setup_devices(hwif) == 0) {
1529                                 hwif->present = 0;
1530                                 continue;
1531                         }
1532
1533                 j++;
1534
1535                 ide_acpi_init(hwif);
1536
1537                 if (hwif->present)
1538                         ide_acpi_port_init_devices(hwif);
1539         }
1540
1541         ide_host_for_each_port(i, hwif, host) {
1542                 if (hwif == NULL)
1543                         continue;
1544
1545                 if (hwif->present)
1546                         hwif_register_devices(hwif);
1547         }
1548
1549         ide_host_for_each_port(i, hwif, host) {
1550                 if (hwif == NULL)
1551                         continue;
1552
1553                 ide_sysfs_register_port(hwif);
1554                 ide_proc_register_port(hwif);
1555
1556                 if (hwif->present)
1557                         ide_proc_port_register_devices(hwif);
1558         }
1559
1560         return j ? 0 : -1;
1561 }
1562 EXPORT_SYMBOL_GPL(ide_host_register);
1563
1564 int ide_host_add(const struct ide_port_info *d, hw_regs_t **hws,
1565                  struct ide_host **hostp)
1566 {
1567         struct ide_host *host;
1568         int rc;
1569
1570         host = ide_host_alloc(d, hws);
1571         if (host == NULL)
1572                 return -ENOMEM;
1573
1574         rc = ide_host_register(host, d, hws);
1575         if (rc) {
1576                 ide_host_free(host);
1577                 return rc;
1578         }
1579
1580         if (hostp)
1581                 *hostp = host;
1582
1583         return 0;
1584 }
1585 EXPORT_SYMBOL_GPL(ide_host_add);
1586
1587 static void __ide_port_unregister_devices(ide_hwif_t *hwif)
1588 {
1589         ide_drive_t *drive;
1590         int i;
1591
1592         ide_port_for_each_dev(i, drive, hwif) {
1593                 if (drive->dev_flags & IDE_DFLAG_PRESENT) {
1594                         device_unregister(&drive->gendev);
1595                         wait_for_completion(&drive->gendev_rel_comp);
1596                 }
1597         }
1598 }
1599
1600 void ide_port_unregister_devices(ide_hwif_t *hwif)
1601 {
1602         mutex_lock(&ide_cfg_mtx);
1603         __ide_port_unregister_devices(hwif);
1604         hwif->present = 0;
1605         ide_port_init_devices_data(hwif);
1606         mutex_unlock(&ide_cfg_mtx);
1607 }
1608 EXPORT_SYMBOL_GPL(ide_port_unregister_devices);
1609
1610 /**
1611  *      ide_unregister          -       free an IDE interface
1612  *      @hwif: IDE interface
1613  *
1614  *      Perform the final unregister of an IDE interface.
1615  *
1616  *      Locking:
1617  *      The caller must not hold the IDE locks.
1618  *
1619  *      It is up to the caller to be sure there is no pending I/O here,
1620  *      and that the interface will not be reopened (present/vanishing
1621  *      locking isn't yet done BTW).
1622  */
1623
1624 static void ide_unregister(ide_hwif_t *hwif)
1625 {
1626         BUG_ON(in_interrupt());
1627         BUG_ON(irqs_disabled());
1628
1629         mutex_lock(&ide_cfg_mtx);
1630
1631         if (hwif->present) {
1632                 __ide_port_unregister_devices(hwif);
1633                 hwif->present = 0;
1634         }
1635
1636         ide_proc_unregister_port(hwif);
1637
1638         free_irq(hwif->irq, hwif);
1639
1640         device_unregister(hwif->portdev);
1641         device_unregister(&hwif->gendev);
1642         wait_for_completion(&hwif->gendev_rel_comp);
1643
1644         /*
1645          * Remove us from the kernel's knowledge
1646          */
1647         blk_unregister_region(MKDEV(hwif->major, 0), MAX_DRIVES<<PARTN_BITS);
1648         kfree(hwif->sg_table);
1649         unregister_blkdev(hwif->major, hwif->name);
1650
1651         ide_release_dma_engine(hwif);
1652
1653         mutex_unlock(&ide_cfg_mtx);
1654 }
1655
1656 void ide_host_free(struct ide_host *host)
1657 {
1658         ide_hwif_t *hwif;
1659         int i;
1660
1661         ide_host_for_each_port(i, hwif, host) {
1662                 if (hwif == NULL)
1663                         continue;
1664
1665                 ide_port_free_devices(hwif);
1666                 ide_free_port_slot(hwif->index);
1667                 kfree(hwif);
1668         }
1669
1670         kfree(host);
1671 }
1672 EXPORT_SYMBOL_GPL(ide_host_free);
1673
1674 void ide_host_remove(struct ide_host *host)
1675 {
1676         ide_hwif_t *hwif;
1677         int i;
1678
1679         ide_host_for_each_port(i, hwif, host) {
1680                 if (hwif)
1681                         ide_unregister(hwif);
1682         }
1683
1684         ide_host_free(host);
1685 }
1686 EXPORT_SYMBOL_GPL(ide_host_remove);
1687
1688 void ide_port_scan(ide_hwif_t *hwif)
1689 {
1690         ide_port_apply_params(hwif);
1691         ide_port_cable_detect(hwif);
1692         ide_port_init_devices(hwif);
1693
1694         if (ide_probe_port(hwif) < 0)
1695                 return;
1696
1697         hwif->present = 1;
1698
1699         ide_port_tune_devices(hwif);
1700         ide_port_setup_devices(hwif);
1701         ide_acpi_port_init_devices(hwif);
1702         hwif_register_devices(hwif);
1703         ide_proc_port_register_devices(hwif);
1704 }
1705 EXPORT_SYMBOL_GPL(ide_port_scan);