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