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