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