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