Merge branch 'work.regset' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
[linux/fpc-iii.git] / drivers / ide / ide-probe.c
blobe867129466b058c4095aacb813f7c3fee6b3a7dd
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 1994-1998 Linus Torvalds & authors (see below)
4 * Copyright (C) 2005, 2007 Bartlomiej Zolnierkiewicz
5 */
7 /*
8 * Mostly written by Mark Lord <mlord@pobox.com>
9 * and Gadi Oxman <gadio@netvision.net.il>
10 * and Andre Hedrick <andre@linux-ide.org>
12 * See linux/MAINTAINERS for address of current maintainer.
14 * This is the IDE probe module, as evolved from hd.c and ide.c.
16 * -- increase WAIT_PIDENTIFY to avoid CD-ROM locking at boot
17 * by Andrea Arcangeli
20 #include <linux/module.h>
21 #include <linux/types.h>
22 #include <linux/string.h>
23 #include <linux/kernel.h>
24 #include <linux/timer.h>
25 #include <linux/mm.h>
26 #include <linux/interrupt.h>
27 #include <linux/major.h>
28 #include <linux/errno.h>
29 #include <linux/genhd.h>
30 #include <linux/slab.h>
31 #include <linux/delay.h>
32 #include <linux/ide.h>
33 #include <linux/spinlock.h>
34 #include <linux/kmod.h>
35 #include <linux/pci.h>
36 #include <linux/scatterlist.h>
38 #include <asm/byteorder.h>
39 #include <asm/irq.h>
40 #include <linux/uaccess.h>
41 #include <asm/io.h>
43 /**
44 * generic_id - add a generic drive id
45 * @drive: drive to make an ID block for
47 * Add a fake id field to the drive we are passed. This allows
48 * use to skip a ton of NULL checks (which people always miss)
49 * and make drive properties unconditional outside of this file
52 static void generic_id(ide_drive_t *drive)
54 u16 *id = drive->id;
56 id[ATA_ID_CUR_CYLS] = id[ATA_ID_CYLS] = drive->cyl;
57 id[ATA_ID_CUR_HEADS] = id[ATA_ID_HEADS] = drive->head;
58 id[ATA_ID_CUR_SECTORS] = id[ATA_ID_SECTORS] = drive->sect;
61 static void ide_disk_init_chs(ide_drive_t *drive)
63 u16 *id = drive->id;
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[ATA_ID_CYLS];
68 drive->head = drive->bios_head = id[ATA_ID_HEADS];
69 drive->sect = drive->bios_sect = id[ATA_ID_SECTORS];
72 /* Handle logical geometry translation by the drive */
73 if (ata_id_current_chs_valid(id)) {
74 drive->cyl = id[ATA_ID_CUR_CYLS];
75 drive->head = id[ATA_ID_CUR_HEADS];
76 drive->sect = id[ATA_ID_CUR_SECTORS];
79 /* Use physical geometry if what we have still makes no sense */
80 if (drive->head > 16 && id[ATA_ID_HEADS] && id[ATA_ID_HEADS] <= 16) {
81 drive->cyl = id[ATA_ID_CYLS];
82 drive->head = id[ATA_ID_HEADS];
83 drive->sect = id[ATA_ID_SECTORS];
87 static void ide_disk_init_mult_count(ide_drive_t *drive)
89 u16 *id = drive->id;
90 u8 max_multsect = id[ATA_ID_MAX_MULTSECT] & 0xff;
92 if (max_multsect) {
93 if ((max_multsect / 2) > 1)
94 id[ATA_ID_MULTSECT] = max_multsect | 0x100;
95 else
96 id[ATA_ID_MULTSECT] &= ~0x1ff;
98 drive->mult_req = id[ATA_ID_MULTSECT] & 0xff;
100 if (drive->mult_req)
101 drive->special_flags |= IDE_SFLAG_SET_MULTMODE;
105 static void ide_classify_ata_dev(ide_drive_t *drive)
107 u16 *id = drive->id;
108 char *m = (char *)&id[ATA_ID_PROD];
109 int is_cfa = ata_id_is_cfa(id);
111 /* CF devices are *not* removable in Linux definition of the term */
112 if (is_cfa == 0 && (id[ATA_ID_CONFIG] & (1 << 7)))
113 drive->dev_flags |= IDE_DFLAG_REMOVABLE;
115 drive->media = ide_disk;
117 if (!ata_id_has_unload(drive->id))
118 drive->dev_flags |= IDE_DFLAG_NO_UNLOAD;
120 printk(KERN_INFO "%s: %s, %s DISK drive\n", drive->name, m,
121 is_cfa ? "CFA" : "ATA");
124 static void ide_classify_atapi_dev(ide_drive_t *drive)
126 u16 *id = drive->id;
127 char *m = (char *)&id[ATA_ID_PROD];
128 u8 type = (id[ATA_ID_CONFIG] >> 8) & 0x1f;
130 printk(KERN_INFO "%s: %s, ATAPI ", drive->name, m);
131 switch (type) {
132 case ide_floppy:
133 if (!strstr(m, "CD-ROM")) {
134 if (!strstr(m, "oppy") &&
135 !strstr(m, "poyp") &&
136 !strstr(m, "ZIP"))
137 printk(KERN_CONT "cdrom or floppy?, assuming ");
138 if (drive->media != ide_cdrom) {
139 printk(KERN_CONT "FLOPPY");
140 drive->dev_flags |= IDE_DFLAG_REMOVABLE;
141 break;
144 /* Early cdrom models used zero */
145 type = ide_cdrom;
146 /* fall through */
147 case ide_cdrom:
148 drive->dev_flags |= IDE_DFLAG_REMOVABLE;
149 #ifdef CONFIG_PPC
150 /* kludge for Apple PowerBook internal zip */
151 if (!strstr(m, "CD-ROM") && strstr(m, "ZIP")) {
152 printk(KERN_CONT "FLOPPY");
153 type = ide_floppy;
154 break;
156 #endif
157 printk(KERN_CONT "CD/DVD-ROM");
158 break;
159 case ide_tape:
160 printk(KERN_CONT "TAPE");
161 break;
162 case ide_optical:
163 printk(KERN_CONT "OPTICAL");
164 drive->dev_flags |= IDE_DFLAG_REMOVABLE;
165 break;
166 default:
167 printk(KERN_CONT "UNKNOWN (type %d)", type);
168 break;
171 printk(KERN_CONT " drive\n");
172 drive->media = type;
173 /* an ATAPI device ignores DRDY */
174 drive->ready_stat = 0;
175 if (ata_id_cdb_intr(id))
176 drive->atapi_flags |= IDE_AFLAG_DRQ_INTERRUPT;
177 drive->dev_flags |= IDE_DFLAG_DOORLOCKING;
178 /* we don't do head unloading on ATAPI devices */
179 drive->dev_flags |= IDE_DFLAG_NO_UNLOAD;
183 * do_identify - identify a drive
184 * @drive: drive to identify
185 * @cmd: command used
186 * @id: buffer for IDENTIFY data
188 * Called when we have issued a drive identify command to
189 * read and parse the results. This function is run with
190 * interrupts disabled.
193 static void do_identify(ide_drive_t *drive, u8 cmd, u16 *id)
195 ide_hwif_t *hwif = drive->hwif;
196 char *m = (char *)&id[ATA_ID_PROD];
197 unsigned long flags;
198 int bswap = 1;
200 /* local CPU only; some systems need this */
201 local_irq_save(flags);
202 /* read 512 bytes of id info */
203 hwif->tp_ops->input_data(drive, NULL, id, SECTOR_SIZE);
204 local_irq_restore(flags);
206 drive->dev_flags |= IDE_DFLAG_ID_READ;
207 #ifdef DEBUG
208 printk(KERN_INFO "%s: dumping identify data\n", drive->name);
209 ide_dump_identify((u8 *)id);
210 #endif
211 ide_fix_driveid(id);
214 * ATA_CMD_ID_ATA returns little-endian info,
215 * ATA_CMD_ID_ATAPI *usually* returns little-endian info.
217 if (cmd == ATA_CMD_ID_ATAPI) {
218 if ((m[0] == 'N' && m[1] == 'E') || /* NEC */
219 (m[0] == 'F' && m[1] == 'X') || /* Mitsumi */
220 (m[0] == 'P' && m[1] == 'i')) /* Pioneer */
221 /* Vertos drives may still be weird */
222 bswap ^= 1;
225 ide_fixstring(m, ATA_ID_PROD_LEN, bswap);
226 ide_fixstring((char *)&id[ATA_ID_FW_REV], ATA_ID_FW_REV_LEN, bswap);
227 ide_fixstring((char *)&id[ATA_ID_SERNO], ATA_ID_SERNO_LEN, bswap);
229 /* we depend on this a lot! */
230 m[ATA_ID_PROD_LEN - 1] = '\0';
232 if (strstr(m, "E X A B Y T E N E S T"))
233 drive->dev_flags &= ~IDE_DFLAG_PRESENT;
234 else
235 drive->dev_flags |= IDE_DFLAG_PRESENT;
239 * ide_dev_read_id - send ATA/ATAPI IDENTIFY command
240 * @drive: drive to identify
241 * @cmd: command to use
242 * @id: buffer for IDENTIFY data
243 * @irq_ctx: flag set when called from the IRQ context
245 * Sends an ATA(PI) IDENTIFY request to a drive and waits for a response.
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)
252 int ide_dev_read_id(ide_drive_t *drive, u8 cmd, u16 *id, int irq_ctx)
254 ide_hwif_t *hwif = drive->hwif;
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;
262 * Disable device IRQ. Otherwise we'll get spurious interrupts
263 * during the identify phase that the IRQ handler isn't expecting.
265 if (io_ports->ctl_addr)
266 tp_ops->write_devctl(hwif, ATA_NIEN | ATA_DEVCTL_OBS);
268 /* take a deep breath */
269 if (irq_ctx)
270 mdelay(50);
271 else
272 msleep(50);
274 if (io_ports->ctl_addr &&
275 (hwif->host_flags & IDE_HFLAG_BROKEN_ALTSTATUS) == 0) {
276 a = tp_ops->read_altstatus(hwif);
277 s = tp_ops->read_status(hwif);
278 if ((a ^ s) & ~ATA_SENSE)
279 /* ancient Seagate drives, broken interfaces */
280 printk(KERN_INFO "%s: probing with STATUS(0x%02x) "
281 "instead of ALTSTATUS(0x%02x)\n",
282 drive->name, s, a);
283 else
284 /* use non-intrusive polling */
285 use_altstatus = 1;
288 /* set features register for atapi
289 * identify command to be sure of reply
291 if (cmd == ATA_CMD_ID_ATAPI) {
292 struct ide_taskfile tf;
294 memset(&tf, 0, sizeof(tf));
295 /* disable DMA & overlap */
296 tp_ops->tf_load(drive, &tf, IDE_VALID_FEATURE);
299 /* ask drive for ID */
300 tp_ops->exec_command(hwif, cmd);
302 timeout = ((cmd == ATA_CMD_ID_ATA) ? WAIT_WORSTCASE : WAIT_PIDENTIFY) / 2;
304 /* wait for IRQ and ATA_DRQ */
305 if (irq_ctx) {
306 rc = __ide_wait_stat(drive, ATA_DRQ, BAD_R_STAT, timeout, &s);
307 if (rc)
308 return 1;
309 } else {
310 rc = ide_busy_sleep(drive, timeout, use_altstatus);
311 if (rc)
312 return 1;
314 msleep(50);
315 s = tp_ops->read_status(hwif);
318 if (OK_STAT(s, ATA_DRQ, BAD_R_STAT)) {
319 /* drive returned ID */
320 do_identify(drive, cmd, id);
321 /* drive responded with ID */
322 rc = 0;
323 /* clear drive IRQ */
324 (void)tp_ops->read_status(hwif);
325 } else {
326 /* drive refused ID */
327 rc = 2;
329 return rc;
332 int ide_busy_sleep(ide_drive_t *drive, unsigned long timeout, int altstatus)
334 ide_hwif_t *hwif = drive->hwif;
335 u8 stat;
337 timeout += jiffies;
339 do {
340 msleep(50); /* give drive a breather */
341 stat = altstatus ? hwif->tp_ops->read_altstatus(hwif)
342 : hwif->tp_ops->read_status(hwif);
343 if ((stat & ATA_BUSY) == 0)
344 return 0;
345 } while (time_before(jiffies, timeout));
347 printk(KERN_ERR "%s: timeout in %s\n", drive->name, __func__);
349 return 1; /* drive timed-out */
352 static u8 ide_read_device(ide_drive_t *drive)
354 struct ide_taskfile tf;
356 drive->hwif->tp_ops->tf_read(drive, &tf, IDE_VALID_DEVICE);
358 return tf.device;
362 * do_probe - probe an IDE device
363 * @drive: drive to probe
364 * @cmd: command to use
366 * do_probe() has the difficult job of finding a drive if it exists,
367 * without getting hung up if it doesn't exist, without trampling on
368 * ethernet cards, and without leaving any IRQs dangling to haunt us later.
370 * If a drive is "known" to exist (from CMOS or kernel parameters),
371 * but does not respond right away, the probe will "hang in there"
372 * for the maximum wait time (about 30 seconds), otherwise it will
373 * exit much more quickly.
375 * Returns: 0 device was identified
376 * 1 device timed-out (no response to identify request)
377 * 2 device aborted the command (refused to identify itself)
378 * 3 bad status from device (possible for ATAPI drives)
379 * 4 probe was not attempted because failure was obvious
382 static int do_probe (ide_drive_t *drive, u8 cmd)
384 ide_hwif_t *hwif = drive->hwif;
385 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
386 u16 *id = drive->id;
387 int rc;
388 u8 present = !!(drive->dev_flags & IDE_DFLAG_PRESENT), stat;
390 /* avoid waiting for inappropriate probes */
391 if (present && drive->media != ide_disk && cmd == ATA_CMD_ID_ATA)
392 return 4;
394 #ifdef DEBUG
395 printk(KERN_INFO "probing for %s: present=%d, media=%d, probetype=%s\n",
396 drive->name, present, drive->media,
397 (cmd == ATA_CMD_ID_ATA) ? "ATA" : "ATAPI");
398 #endif
400 /* needed for some systems
401 * (e.g. crw9624 as drive0 with disk as slave)
403 msleep(50);
404 tp_ops->dev_select(drive);
405 msleep(50);
407 if (ide_read_device(drive) != drive->select && present == 0) {
408 if (drive->dn & 1) {
409 /* exit with drive0 selected */
410 tp_ops->dev_select(hwif->devices[0]);
411 /* allow ATA_BUSY to assert & clear */
412 msleep(50);
414 /* no i/f present: mmm.. this should be a 4 -ml */
415 return 3;
418 stat = tp_ops->read_status(hwif);
420 if (OK_STAT(stat, ATA_DRDY, ATA_BUSY) ||
421 present || cmd == ATA_CMD_ID_ATAPI) {
422 rc = ide_dev_read_id(drive, cmd, id, 0);
423 if (rc)
424 /* failed: try again */
425 rc = ide_dev_read_id(drive, cmd, id, 0);
427 stat = tp_ops->read_status(hwif);
429 if (stat == (ATA_BUSY | ATA_DRDY))
430 return 4;
432 if (rc == 1 && cmd == ATA_CMD_ID_ATAPI) {
433 printk(KERN_ERR "%s: no response (status = 0x%02x), "
434 "resetting drive\n", drive->name, stat);
435 msleep(50);
436 tp_ops->dev_select(drive);
437 msleep(50);
438 tp_ops->exec_command(hwif, ATA_CMD_DEV_RESET);
439 (void)ide_busy_sleep(drive, WAIT_WORSTCASE, 0);
440 rc = ide_dev_read_id(drive, cmd, id, 0);
443 /* ensure drive IRQ is clear */
444 stat = tp_ops->read_status(hwif);
446 if (rc == 1)
447 printk(KERN_ERR "%s: no response (status = 0x%02x)\n",
448 drive->name, stat);
449 } else {
450 /* not present or maybe ATAPI */
451 rc = 3;
453 if (drive->dn & 1) {
454 /* exit with drive0 selected */
455 tp_ops->dev_select(hwif->devices[0]);
456 msleep(50);
457 /* ensure drive irq is clear */
458 (void)tp_ops->read_status(hwif);
460 return rc;
464 * probe_for_drives - upper level drive probe
465 * @drive: drive to probe for
467 * probe_for_drive() tests for existence of a given drive using do_probe()
468 * and presents things to the user as needed.
470 * Returns: 0 no device was found
471 * 1 device was found
472 * (note: IDE_DFLAG_PRESENT might still be not set)
475 static u8 probe_for_drive(ide_drive_t *drive)
477 char *m;
478 int rc;
479 u8 cmd;
481 drive->dev_flags &= ~IDE_DFLAG_ID_READ;
483 m = (char *)&drive->id[ATA_ID_PROD];
484 strcpy(m, "UNKNOWN");
486 /* skip probing? */
487 if ((drive->dev_flags & IDE_DFLAG_NOPROBE) == 0) {
488 /* if !(success||timed-out) */
489 cmd = ATA_CMD_ID_ATA;
490 rc = do_probe(drive, cmd);
491 if (rc >= 2) {
492 /* look for ATAPI device */
493 cmd = ATA_CMD_ID_ATAPI;
494 rc = do_probe(drive, cmd);
497 if ((drive->dev_flags & IDE_DFLAG_PRESENT) == 0)
498 return 0;
500 /* identification failed? */
501 if ((drive->dev_flags & IDE_DFLAG_ID_READ) == 0) {
502 if (drive->media == ide_disk) {
503 printk(KERN_INFO "%s: non-IDE drive, CHS=%d/%d/%d\n",
504 drive->name, drive->cyl,
505 drive->head, drive->sect);
506 } else if (drive->media == ide_cdrom) {
507 printk(KERN_INFO "%s: ATAPI cdrom (?)\n", drive->name);
508 } else {
509 /* nuke it */
510 printk(KERN_WARNING "%s: Unknown device on bus refused identification. Ignoring.\n", drive->name);
511 drive->dev_flags &= ~IDE_DFLAG_PRESENT;
513 } else {
514 if (cmd == ATA_CMD_ID_ATAPI)
515 ide_classify_atapi_dev(drive);
516 else
517 ide_classify_ata_dev(drive);
521 if ((drive->dev_flags & IDE_DFLAG_PRESENT) == 0)
522 return 0;
524 /* The drive wasn't being helpful. Add generic info only */
525 if ((drive->dev_flags & IDE_DFLAG_ID_READ) == 0) {
526 generic_id(drive);
527 return 1;
530 if (drive->media == ide_disk) {
531 ide_disk_init_chs(drive);
532 ide_disk_init_mult_count(drive);
535 return 1;
538 static void hwif_release_dev(struct device *dev)
540 ide_hwif_t *hwif = container_of(dev, ide_hwif_t, gendev);
542 complete(&hwif->gendev_rel_comp);
545 static int ide_register_port(ide_hwif_t *hwif)
547 int ret;
549 /* register with global device tree */
550 dev_set_name(&hwif->gendev, "%s", hwif->name);
551 dev_set_drvdata(&hwif->gendev, hwif);
552 if (hwif->gendev.parent == NULL)
553 hwif->gendev.parent = hwif->dev;
554 hwif->gendev.release = hwif_release_dev;
556 ret = device_register(&hwif->gendev);
557 if (ret < 0) {
558 printk(KERN_WARNING "IDE: %s: device_register error: %d\n",
559 __func__, ret);
560 goto out;
563 hwif->portdev = device_create(ide_port_class, &hwif->gendev,
564 MKDEV(0, 0), hwif, "%s", hwif->name);
565 if (IS_ERR(hwif->portdev)) {
566 ret = PTR_ERR(hwif->portdev);
567 device_unregister(&hwif->gendev);
569 out:
570 return ret;
574 * ide_port_wait_ready - wait for port to become ready
575 * @hwif: IDE port
577 * This is needed on some PPCs and a bunch of BIOS-less embedded
578 * platforms. Typical cases are:
580 * - The firmware hard reset the disk before booting the kernel,
581 * the drive is still doing it's poweron-reset sequence, that
582 * can take up to 30 seconds.
584 * - The firmware does nothing (or no firmware), the device is
585 * still in POST state (same as above actually).
587 * - Some CD/DVD/Writer combo drives tend to drive the bus during
588 * their reset sequence even when they are non-selected slave
589 * devices, thus preventing discovery of the main HD.
591 * Doing this wait-for-non-busy should not harm any existing
592 * configuration and fix some issues like the above.
594 * BenH.
596 * Returns 0 on success, error code (< 0) otherwise.
599 static int ide_port_wait_ready(ide_hwif_t *hwif)
601 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
602 ide_drive_t *drive;
603 int i, rc;
605 printk(KERN_DEBUG "Probing IDE interface %s...\n", hwif->name);
607 /* Let HW settle down a bit from whatever init state we
608 * come from */
609 mdelay(2);
611 /* Wait for BSY bit to go away, spec timeout is 30 seconds,
612 * I know of at least one disk who takes 31 seconds, I use 35
613 * here to be safe
615 rc = ide_wait_not_busy(hwif, 35000);
616 if (rc)
617 return rc;
619 /* Now make sure both master & slave are ready */
620 ide_port_for_each_dev(i, drive, hwif) {
621 /* Ignore disks that we will not probe for later. */
622 if ((drive->dev_flags & IDE_DFLAG_NOPROBE) == 0 ||
623 (drive->dev_flags & IDE_DFLAG_PRESENT)) {
624 tp_ops->dev_select(drive);
625 tp_ops->write_devctl(hwif, ATA_DEVCTL_OBS);
626 mdelay(2);
627 rc = ide_wait_not_busy(hwif, 35000);
628 if (rc)
629 goto out;
630 } else
631 printk(KERN_DEBUG "%s: ide_wait_not_busy() skipped\n",
632 drive->name);
634 out:
635 /* Exit function with master reselected (let's be sane) */
636 if (i)
637 tp_ops->dev_select(hwif->devices[0]);
639 return rc;
643 * ide_undecoded_slave - look for bad CF adapters
644 * @dev1: slave device
646 * Analyse the drives on the interface and attempt to decide if we
647 * have the same drive viewed twice. This occurs with crap CF adapters
648 * and PCMCIA sometimes.
651 void ide_undecoded_slave(ide_drive_t *dev1)
653 ide_drive_t *dev0 = dev1->hwif->devices[0];
655 if ((dev1->dn & 1) == 0 || (dev0->dev_flags & IDE_DFLAG_PRESENT) == 0)
656 return;
658 /* If the models don't match they are not the same product */
659 if (strcmp((char *)&dev0->id[ATA_ID_PROD],
660 (char *)&dev1->id[ATA_ID_PROD]))
661 return;
663 /* Serial numbers do not match */
664 if (strncmp((char *)&dev0->id[ATA_ID_SERNO],
665 (char *)&dev1->id[ATA_ID_SERNO], ATA_ID_SERNO_LEN))
666 return;
668 /* No serial number, thankfully very rare for CF */
669 if (*(char *)&dev0->id[ATA_ID_SERNO] == 0)
670 return;
672 /* Appears to be an IDE flash adapter with decode bugs */
673 printk(KERN_WARNING "ide-probe: ignoring undecoded slave\n");
675 dev1->dev_flags &= ~IDE_DFLAG_PRESENT;
678 EXPORT_SYMBOL_GPL(ide_undecoded_slave);
680 static int ide_probe_port(ide_hwif_t *hwif)
682 ide_drive_t *drive;
683 unsigned int irqd;
684 int i, rc = -ENODEV;
686 BUG_ON(hwif->present);
688 if ((hwif->devices[0]->dev_flags & IDE_DFLAG_NOPROBE) &&
689 (hwif->devices[1]->dev_flags & IDE_DFLAG_NOPROBE))
690 return -EACCES;
693 * We must always disable IRQ, as probe_for_drive will assert IRQ, but
694 * we'll install our IRQ driver much later...
696 irqd = hwif->irq;
697 if (irqd)
698 disable_irq(hwif->irq);
700 if (ide_port_wait_ready(hwif) == -EBUSY)
701 printk(KERN_DEBUG "%s: Wait for ready failed before probe !\n", hwif->name);
704 * Second drive should only exist if first drive was found,
705 * but a lot of cdrom drives are configured as single slaves.
707 ide_port_for_each_dev(i, drive, hwif) {
708 (void) probe_for_drive(drive);
709 if (drive->dev_flags & IDE_DFLAG_PRESENT)
710 rc = 0;
714 * Use cached IRQ number. It might be (and is...) changed by probe
715 * code above
717 if (irqd)
718 enable_irq(irqd);
720 return rc;
723 static void ide_port_tune_devices(ide_hwif_t *hwif)
725 const struct ide_port_ops *port_ops = hwif->port_ops;
726 ide_drive_t *drive;
727 int i;
729 ide_port_for_each_present_dev(i, drive, hwif) {
730 ide_check_nien_quirk_list(drive);
732 if (port_ops && port_ops->quirkproc)
733 port_ops->quirkproc(drive);
736 ide_port_for_each_present_dev(i, drive, hwif) {
737 ide_set_max_pio(drive);
739 drive->dev_flags |= IDE_DFLAG_NICE1;
741 if (hwif->dma_ops)
742 ide_set_dma(drive);
746 static void ide_initialize_rq(struct request *rq)
748 struct ide_request *req = blk_mq_rq_to_pdu(rq);
750 req->special = NULL;
751 scsi_req_init(&req->sreq);
752 req->sreq.sense = req->sense;
755 static const struct blk_mq_ops ide_mq_ops = {
756 .queue_rq = ide_queue_rq,
757 .initialize_rq_fn = ide_initialize_rq,
761 * init request queue
763 static int ide_init_queue(ide_drive_t *drive)
765 struct request_queue *q;
766 ide_hwif_t *hwif = drive->hwif;
767 int max_sectors = 256;
768 int max_sg_entries = PRD_ENTRIES;
769 struct blk_mq_tag_set *set;
772 * Our default set up assumes the normal IDE case,
773 * that is 64K segmenting, standard PRD setup
774 * and LBA28. Some drivers then impose their own
775 * limits and LBA48 we could raise it but as yet
776 * do not.
779 set = &drive->tag_set;
780 set->ops = &ide_mq_ops;
781 set->nr_hw_queues = 1;
782 set->queue_depth = 32;
783 set->reserved_tags = 1;
784 set->cmd_size = sizeof(struct ide_request);
785 set->numa_node = hwif_to_node(hwif);
786 set->flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_BLOCKING;
787 if (blk_mq_alloc_tag_set(set))
788 return 1;
790 q = blk_mq_init_queue(set);
791 if (IS_ERR(q)) {
792 blk_mq_free_tag_set(set);
793 return 1;
796 blk_queue_flag_set(QUEUE_FLAG_SCSI_PASSTHROUGH, q);
798 q->queuedata = drive;
799 blk_queue_segment_boundary(q, 0xffff);
801 if (hwif->rqsize < max_sectors)
802 max_sectors = hwif->rqsize;
803 blk_queue_max_hw_sectors(q, max_sectors);
805 #ifdef CONFIG_PCI
806 /* When we have an IOMMU, we may have a problem where pci_map_sg()
807 * creates segments that don't completely match our boundary
808 * requirements and thus need to be broken up again. Because it
809 * doesn't align properly either, we may actually have to break up
810 * to more segments than what was we got in the first place, a max
811 * worst case is twice as many.
812 * This will be fixed once we teach pci_map_sg() about our boundary
813 * requirements, hopefully soon. *FIXME*
815 max_sg_entries >>= 1;
816 #endif /* CONFIG_PCI */
818 blk_queue_max_segments(q, max_sg_entries);
820 /* assign drive queue */
821 drive->queue = q;
823 return 0;
826 static DEFINE_MUTEX(ide_cfg_mtx);
829 * For any present drive:
830 * - allocate the block device queue
832 static int ide_port_setup_devices(ide_hwif_t *hwif)
834 ide_drive_t *drive;
835 int i, j = 0;
837 mutex_lock(&ide_cfg_mtx);
838 ide_port_for_each_present_dev(i, drive, hwif) {
839 if (ide_init_queue(drive)) {
840 printk(KERN_ERR "ide: failed to init %s\n",
841 drive->name);
842 drive->dev_flags &= ~IDE_DFLAG_PRESENT;
843 continue;
846 j++;
848 mutex_unlock(&ide_cfg_mtx);
850 return j;
853 static void ide_host_enable_irqs(struct ide_host *host)
855 ide_hwif_t *hwif;
856 int i;
858 ide_host_for_each_port(i, hwif, host) {
859 if (hwif == NULL)
860 continue;
862 /* clear any pending IRQs */
863 hwif->tp_ops->read_status(hwif);
865 /* unmask IRQs */
866 if (hwif->io_ports.ctl_addr)
867 hwif->tp_ops->write_devctl(hwif, ATA_DEVCTL_OBS);
872 * This routine sets up the IRQ for an IDE interface.
874 static int init_irq (ide_hwif_t *hwif)
876 struct ide_io_ports *io_ports = &hwif->io_ports;
877 struct ide_host *host = hwif->host;
878 irq_handler_t irq_handler = host->irq_handler;
879 int sa = host->irq_flags;
881 if (irq_handler == NULL)
882 irq_handler = ide_intr;
884 if (!host->get_lock)
885 if (request_irq(hwif->irq, irq_handler, sa, hwif->name, hwif))
886 goto out_up;
888 #if !defined(__mc68000__)
889 printk(KERN_INFO "%s at 0x%03lx-0x%03lx,0x%03lx on irq %d", hwif->name,
890 io_ports->data_addr, io_ports->status_addr,
891 io_ports->ctl_addr, hwif->irq);
892 #else
893 printk(KERN_INFO "%s at 0x%08lx on irq %d", hwif->name,
894 io_ports->data_addr, hwif->irq);
895 #endif /* __mc68000__ */
896 if (hwif->host->host_flags & IDE_HFLAG_SERIALIZE)
897 printk(KERN_CONT " (serialized)");
898 printk(KERN_CONT "\n");
900 return 0;
901 out_up:
902 return 1;
905 static int ata_lock(dev_t dev, void *data)
907 /* FIXME: we want to pin hwif down */
908 return 0;
911 static struct kobject *ata_probe(dev_t dev, int *part, void *data)
913 ide_hwif_t *hwif = data;
914 int unit = *part >> PARTN_BITS;
915 ide_drive_t *drive = hwif->devices[unit];
917 if ((drive->dev_flags & IDE_DFLAG_PRESENT) == 0)
918 return NULL;
920 if (drive->media == ide_disk)
921 request_module("ide-disk");
922 if (drive->media == ide_cdrom || drive->media == ide_optical)
923 request_module("ide-cd");
924 if (drive->media == ide_tape)
925 request_module("ide-tape");
926 if (drive->media == ide_floppy)
927 request_module("ide-floppy");
929 return NULL;
932 static struct kobject *exact_match(dev_t dev, int *part, void *data)
934 struct gendisk *p = data;
935 *part &= (1 << PARTN_BITS) - 1;
936 return &disk_to_dev(p)->kobj;
939 static int exact_lock(dev_t dev, void *data)
941 struct gendisk *p = data;
943 if (!get_disk_and_module(p))
944 return -1;
945 return 0;
948 void ide_register_region(struct gendisk *disk)
950 blk_register_region(MKDEV(disk->major, disk->first_minor),
951 disk->minors, NULL, exact_match, exact_lock, disk);
954 EXPORT_SYMBOL_GPL(ide_register_region);
956 void ide_unregister_region(struct gendisk *disk)
958 blk_unregister_region(MKDEV(disk->major, disk->first_minor),
959 disk->minors);
962 EXPORT_SYMBOL_GPL(ide_unregister_region);
964 void ide_init_disk(struct gendisk *disk, ide_drive_t *drive)
966 ide_hwif_t *hwif = drive->hwif;
967 unsigned int unit = drive->dn & 1;
969 disk->major = hwif->major;
970 disk->first_minor = unit << PARTN_BITS;
971 sprintf(disk->disk_name, "hd%c", 'a' + hwif->index * MAX_DRIVES + unit);
972 disk->queue = drive->queue;
975 EXPORT_SYMBOL_GPL(ide_init_disk);
977 static void drive_release_dev (struct device *dev)
979 ide_drive_t *drive = container_of(dev, ide_drive_t, gendev);
981 ide_proc_unregister_device(drive);
983 if (drive->sense_rq)
984 blk_mq_free_request(drive->sense_rq);
986 blk_cleanup_queue(drive->queue);
987 drive->queue = NULL;
988 blk_mq_free_tag_set(&drive->tag_set);
990 drive->dev_flags &= ~IDE_DFLAG_PRESENT;
992 complete(&drive->gendev_rel_comp);
995 static int hwif_init(ide_hwif_t *hwif)
997 if (!hwif->irq) {
998 printk(KERN_ERR "%s: disabled, no IRQ\n", hwif->name);
999 return 0;
1002 if (register_blkdev(hwif->major, hwif->name))
1003 return 0;
1005 if (!hwif->sg_max_nents)
1006 hwif->sg_max_nents = PRD_ENTRIES;
1008 hwif->sg_table = kmalloc_array(hwif->sg_max_nents,
1009 sizeof(struct scatterlist),
1010 GFP_KERNEL);
1011 if (!hwif->sg_table) {
1012 printk(KERN_ERR "%s: unable to allocate SG table.\n", hwif->name);
1013 goto out;
1016 sg_init_table(hwif->sg_table, hwif->sg_max_nents);
1018 if (init_irq(hwif)) {
1019 printk(KERN_ERR "%s: disabled, unable to get IRQ %d\n",
1020 hwif->name, hwif->irq);
1021 goto out;
1024 blk_register_region(MKDEV(hwif->major, 0), MAX_DRIVES << PARTN_BITS,
1025 THIS_MODULE, ata_probe, ata_lock, hwif);
1026 return 1;
1028 out:
1029 unregister_blkdev(hwif->major, hwif->name);
1030 return 0;
1033 static void hwif_register_devices(ide_hwif_t *hwif)
1035 ide_drive_t *drive;
1036 unsigned int i;
1038 ide_port_for_each_present_dev(i, drive, hwif) {
1039 struct device *dev = &drive->gendev;
1040 int ret;
1042 dev_set_name(dev, "%u.%u", hwif->index, i);
1043 dev_set_drvdata(dev, drive);
1044 dev->parent = &hwif->gendev;
1045 dev->bus = &ide_bus_type;
1046 dev->release = drive_release_dev;
1048 ret = device_register(dev);
1049 if (ret < 0)
1050 printk(KERN_WARNING "IDE: %s: device_register error: "
1051 "%d\n", __func__, ret);
1055 static void ide_port_init_devices(ide_hwif_t *hwif)
1057 const struct ide_port_ops *port_ops = hwif->port_ops;
1058 ide_drive_t *drive;
1059 int i;
1061 ide_port_for_each_dev(i, drive, hwif) {
1062 drive->dn = i + hwif->channel * 2;
1064 if (hwif->host_flags & IDE_HFLAG_IO_32BIT)
1065 drive->io_32bit = 1;
1066 if (hwif->host_flags & IDE_HFLAG_NO_IO_32BIT)
1067 drive->dev_flags |= IDE_DFLAG_NO_IO_32BIT;
1068 if (hwif->host_flags & IDE_HFLAG_UNMASK_IRQS)
1069 drive->dev_flags |= IDE_DFLAG_UNMASK;
1070 if (hwif->host_flags & IDE_HFLAG_NO_UNMASK_IRQS)
1071 drive->dev_flags |= IDE_DFLAG_NO_UNMASK;
1073 drive->pio_mode = XFER_PIO_0;
1075 if (port_ops && port_ops->init_dev)
1076 port_ops->init_dev(drive);
1080 static void ide_init_port(ide_hwif_t *hwif, unsigned int port,
1081 const struct ide_port_info *d)
1083 hwif->channel = port;
1085 hwif->chipset = d->chipset ? d->chipset : ide_pci;
1087 if (d->init_iops)
1088 d->init_iops(hwif);
1090 /* ->host_flags may be set by ->init_iops (or even earlier...) */
1091 hwif->host_flags |= d->host_flags;
1092 hwif->pio_mask = d->pio_mask;
1094 if (d->tp_ops)
1095 hwif->tp_ops = d->tp_ops;
1097 /* ->set_pio_mode for DTC2278 is currently limited to port 0 */
1098 if ((hwif->host_flags & IDE_HFLAG_DTC2278) == 0 || hwif->channel == 0)
1099 hwif->port_ops = d->port_ops;
1101 hwif->swdma_mask = d->swdma_mask;
1102 hwif->mwdma_mask = d->mwdma_mask;
1103 hwif->ultra_mask = d->udma_mask;
1105 if ((d->host_flags & IDE_HFLAG_NO_DMA) == 0) {
1106 int rc;
1108 hwif->dma_ops = d->dma_ops;
1110 if (d->init_dma)
1111 rc = d->init_dma(hwif, d);
1112 else
1113 rc = ide_hwif_setup_dma(hwif, d);
1115 if (rc < 0) {
1116 printk(KERN_INFO "%s: DMA disabled\n", hwif->name);
1118 hwif->dma_ops = NULL;
1119 hwif->dma_base = 0;
1120 hwif->swdma_mask = 0;
1121 hwif->mwdma_mask = 0;
1122 hwif->ultra_mask = 0;
1126 if ((d->host_flags & IDE_HFLAG_SERIALIZE) ||
1127 ((d->host_flags & IDE_HFLAG_SERIALIZE_DMA) && hwif->dma_base))
1128 hwif->host->host_flags |= IDE_HFLAG_SERIALIZE;
1130 if (d->max_sectors)
1131 hwif->rqsize = d->max_sectors;
1132 else {
1133 if ((hwif->host_flags & IDE_HFLAG_NO_LBA48) ||
1134 (hwif->host_flags & IDE_HFLAG_NO_LBA48_DMA))
1135 hwif->rqsize = 256;
1136 else
1137 hwif->rqsize = 65536;
1140 /* call chipset specific routine for each enabled port */
1141 if (d->init_hwif)
1142 d->init_hwif(hwif);
1145 static void ide_port_cable_detect(ide_hwif_t *hwif)
1147 const struct ide_port_ops *port_ops = hwif->port_ops;
1149 if (port_ops && port_ops->cable_detect && (hwif->ultra_mask & 0x78)) {
1150 if (hwif->cbl != ATA_CBL_PATA40_SHORT)
1151 hwif->cbl = port_ops->cable_detect(hwif);
1156 * Deferred request list insertion handler
1158 static void drive_rq_insert_work(struct work_struct *work)
1160 ide_drive_t *drive = container_of(work, ide_drive_t, rq_work);
1161 ide_hwif_t *hwif = drive->hwif;
1162 struct request *rq;
1163 blk_status_t ret;
1164 LIST_HEAD(list);
1166 blk_mq_quiesce_queue(drive->queue);
1168 ret = BLK_STS_OK;
1169 spin_lock_irq(&hwif->lock);
1170 while (!list_empty(&drive->rq_list)) {
1171 rq = list_first_entry(&drive->rq_list, struct request, queuelist);
1172 list_del_init(&rq->queuelist);
1174 spin_unlock_irq(&hwif->lock);
1175 ret = ide_issue_rq(drive, rq, true);
1176 spin_lock_irq(&hwif->lock);
1178 spin_unlock_irq(&hwif->lock);
1180 blk_mq_unquiesce_queue(drive->queue);
1182 if (ret != BLK_STS_OK)
1183 kblockd_schedule_work(&drive->rq_work);
1186 static const u8 ide_hwif_to_major[] =
1187 { IDE0_MAJOR, IDE1_MAJOR, IDE2_MAJOR, IDE3_MAJOR, IDE4_MAJOR,
1188 IDE5_MAJOR, IDE6_MAJOR, IDE7_MAJOR, IDE8_MAJOR, IDE9_MAJOR };
1190 static void ide_port_init_devices_data(ide_hwif_t *hwif)
1192 ide_drive_t *drive;
1193 int i;
1195 ide_port_for_each_dev(i, drive, hwif) {
1196 u8 j = (hwif->index * MAX_DRIVES) + i;
1197 u16 *saved_id = drive->id;
1199 memset(drive, 0, sizeof(*drive));
1200 memset(saved_id, 0, SECTOR_SIZE);
1201 drive->id = saved_id;
1203 drive->media = ide_disk;
1204 drive->select = (i << 4) | ATA_DEVICE_OBS;
1205 drive->hwif = hwif;
1206 drive->ready_stat = ATA_DRDY;
1207 drive->bad_wstat = BAD_W_STAT;
1208 drive->special_flags = IDE_SFLAG_RECALIBRATE |
1209 IDE_SFLAG_SET_GEOMETRY;
1210 drive->name[0] = 'h';
1211 drive->name[1] = 'd';
1212 drive->name[2] = 'a' + j;
1213 drive->max_failures = IDE_DEFAULT_MAX_FAILURES;
1215 INIT_LIST_HEAD(&drive->list);
1216 init_completion(&drive->gendev_rel_comp);
1218 INIT_WORK(&drive->rq_work, drive_rq_insert_work);
1219 INIT_LIST_HEAD(&drive->rq_list);
1223 static void ide_init_port_data(ide_hwif_t *hwif, unsigned int index)
1225 /* fill in any non-zero initial values */
1226 hwif->index = index;
1227 hwif->major = ide_hwif_to_major[index];
1229 hwif->name[0] = 'i';
1230 hwif->name[1] = 'd';
1231 hwif->name[2] = 'e';
1232 hwif->name[3] = '0' + index;
1234 spin_lock_init(&hwif->lock);
1236 timer_setup(&hwif->timer, ide_timer_expiry, 0);
1238 init_completion(&hwif->gendev_rel_comp);
1240 hwif->tp_ops = &default_tp_ops;
1242 ide_port_init_devices_data(hwif);
1245 static void ide_init_port_hw(ide_hwif_t *hwif, struct ide_hw *hw)
1247 memcpy(&hwif->io_ports, &hw->io_ports, sizeof(hwif->io_ports));
1248 hwif->irq = hw->irq;
1249 hwif->dev = hw->dev;
1250 hwif->gendev.parent = hw->parent ? hw->parent : hw->dev;
1251 hwif->config_data = hw->config;
1254 static unsigned int ide_indexes;
1257 * ide_find_port_slot - find free port slot
1258 * @d: IDE port info
1260 * Return the new port slot index or -ENOENT if we are out of free slots.
1263 static int ide_find_port_slot(const struct ide_port_info *d)
1265 int idx = -ENOENT;
1266 u8 bootable = (d && (d->host_flags & IDE_HFLAG_NON_BOOTABLE)) ? 0 : 1;
1267 u8 i = (d && (d->host_flags & IDE_HFLAG_QD_2ND_PORT)) ? 1 : 0;
1270 * Claim an unassigned slot.
1272 * Give preference to claiming other slots before claiming ide0/ide1,
1273 * just in case there's another interface yet-to-be-scanned
1274 * which uses ports 0x1f0/0x170 (the ide0/ide1 defaults).
1276 * Unless there is a bootable card that does not use the standard
1277 * ports 0x1f0/0x170 (the ide0/ide1 defaults).
1279 mutex_lock(&ide_cfg_mtx);
1280 if (bootable) {
1281 if ((ide_indexes | i) != (1 << MAX_HWIFS) - 1)
1282 idx = ffz(ide_indexes | i);
1283 } else {
1284 if ((ide_indexes | 3) != (1 << MAX_HWIFS) - 1)
1285 idx = ffz(ide_indexes | 3);
1286 else if ((ide_indexes & 3) != 3)
1287 idx = ffz(ide_indexes);
1289 if (idx >= 0)
1290 ide_indexes |= (1 << idx);
1291 mutex_unlock(&ide_cfg_mtx);
1293 return idx;
1296 static void ide_free_port_slot(int idx)
1298 mutex_lock(&ide_cfg_mtx);
1299 ide_indexes &= ~(1 << idx);
1300 mutex_unlock(&ide_cfg_mtx);
1303 static void ide_port_free_devices(ide_hwif_t *hwif)
1305 ide_drive_t *drive;
1306 int i;
1308 ide_port_for_each_dev(i, drive, hwif) {
1309 kfree(drive->id);
1310 kfree(drive);
1314 static int ide_port_alloc_devices(ide_hwif_t *hwif, int node)
1316 ide_drive_t *drive;
1317 int i;
1319 for (i = 0; i < MAX_DRIVES; i++) {
1320 drive = kzalloc_node(sizeof(*drive), GFP_KERNEL, node);
1321 if (drive == NULL)
1322 goto out_nomem;
1325 * In order to keep things simple we have an id
1326 * block for all drives at all times. If the device
1327 * is pre ATA or refuses ATA/ATAPI identify we
1328 * will add faked data to this.
1330 * Also note that 0 everywhere means "can't do X"
1332 drive->id = kzalloc_node(SECTOR_SIZE, GFP_KERNEL, node);
1333 if (drive->id == NULL)
1334 goto out_free_drive;
1336 hwif->devices[i] = drive;
1338 return 0;
1340 out_free_drive:
1341 kfree(drive);
1342 out_nomem:
1343 ide_port_free_devices(hwif);
1344 return -ENOMEM;
1347 struct ide_host *ide_host_alloc(const struct ide_port_info *d,
1348 struct ide_hw **hws, unsigned int n_ports)
1350 struct ide_host *host;
1351 struct device *dev = hws[0] ? hws[0]->dev : NULL;
1352 int node = dev ? dev_to_node(dev) : -1;
1353 int i;
1355 host = kzalloc_node(sizeof(*host), GFP_KERNEL, node);
1356 if (host == NULL)
1357 return NULL;
1359 for (i = 0; i < n_ports; i++) {
1360 ide_hwif_t *hwif;
1361 int idx;
1363 if (hws[i] == NULL)
1364 continue;
1366 hwif = kzalloc_node(sizeof(*hwif), GFP_KERNEL, node);
1367 if (hwif == NULL)
1368 continue;
1370 if (ide_port_alloc_devices(hwif, node) < 0) {
1371 kfree(hwif);
1372 continue;
1375 idx = ide_find_port_slot(d);
1376 if (idx < 0) {
1377 printk(KERN_ERR "%s: no free slot for interface\n",
1378 d ? d->name : "ide");
1379 ide_port_free_devices(hwif);
1380 kfree(hwif);
1381 continue;
1384 ide_init_port_data(hwif, idx);
1386 hwif->host = host;
1388 host->ports[i] = hwif;
1389 host->n_ports++;
1392 if (host->n_ports == 0) {
1393 kfree(host);
1394 return NULL;
1397 host->dev[0] = dev;
1399 if (d) {
1400 host->init_chipset = d->init_chipset;
1401 host->get_lock = d->get_lock;
1402 host->release_lock = d->release_lock;
1403 host->host_flags = d->host_flags;
1404 host->irq_flags = d->irq_flags;
1407 return host;
1409 EXPORT_SYMBOL_GPL(ide_host_alloc);
1411 static void ide_port_free(ide_hwif_t *hwif)
1413 ide_port_free_devices(hwif);
1414 ide_free_port_slot(hwif->index);
1415 kfree(hwif);
1418 static void ide_disable_port(ide_hwif_t *hwif)
1420 struct ide_host *host = hwif->host;
1421 int i;
1423 printk(KERN_INFO "%s: disabling port\n", hwif->name);
1425 for (i = 0; i < MAX_HOST_PORTS; i++) {
1426 if (host->ports[i] == hwif) {
1427 host->ports[i] = NULL;
1428 host->n_ports--;
1432 ide_port_free(hwif);
1435 int ide_host_register(struct ide_host *host, const struct ide_port_info *d,
1436 struct ide_hw **hws)
1438 ide_hwif_t *hwif, *mate = NULL;
1439 int i, j = 0;
1441 pr_warn("legacy IDE will be removed in 2021, please switch to libata\n"
1442 "Report any missing HW support to linux-ide@vger.kernel.org\n");
1444 ide_host_for_each_port(i, hwif, host) {
1445 if (hwif == NULL) {
1446 mate = NULL;
1447 continue;
1450 ide_init_port_hw(hwif, hws[i]);
1451 ide_port_apply_params(hwif);
1453 if ((i & 1) && mate) {
1454 hwif->mate = mate;
1455 mate->mate = hwif;
1458 mate = (i & 1) ? NULL : hwif;
1460 ide_init_port(hwif, i & 1, d);
1461 ide_port_cable_detect(hwif);
1463 hwif->port_flags |= IDE_PFLAG_PROBING;
1465 ide_port_init_devices(hwif);
1468 ide_host_for_each_port(i, hwif, host) {
1469 if (hwif == NULL)
1470 continue;
1472 if (ide_probe_port(hwif) == 0)
1473 hwif->present = 1;
1475 hwif->port_flags &= ~IDE_PFLAG_PROBING;
1477 if ((hwif->host_flags & IDE_HFLAG_4DRIVES) == 0 ||
1478 hwif->mate == NULL || hwif->mate->present == 0) {
1479 if (ide_register_port(hwif)) {
1480 ide_disable_port(hwif);
1481 continue;
1485 if (hwif->present)
1486 ide_port_tune_devices(hwif);
1489 ide_host_enable_irqs(host);
1491 ide_host_for_each_port(i, hwif, host) {
1492 if (hwif == NULL)
1493 continue;
1495 if (hwif_init(hwif) == 0) {
1496 printk(KERN_INFO "%s: failed to initialize IDE "
1497 "interface\n", hwif->name);
1498 device_unregister(hwif->portdev);
1499 device_unregister(&hwif->gendev);
1500 ide_disable_port(hwif);
1501 continue;
1504 if (hwif->present)
1505 if (ide_port_setup_devices(hwif) == 0) {
1506 hwif->present = 0;
1507 continue;
1510 j++;
1512 ide_acpi_init_port(hwif);
1514 if (hwif->present)
1515 ide_acpi_port_init_devices(hwif);
1518 ide_host_for_each_port(i, hwif, host) {
1519 if (hwif == NULL)
1520 continue;
1522 ide_sysfs_register_port(hwif);
1523 ide_proc_register_port(hwif);
1525 if (hwif->present) {
1526 ide_proc_port_register_devices(hwif);
1527 hwif_register_devices(hwif);
1531 return j ? 0 : -1;
1533 EXPORT_SYMBOL_GPL(ide_host_register);
1535 int ide_host_add(const struct ide_port_info *d, struct ide_hw **hws,
1536 unsigned int n_ports, struct ide_host **hostp)
1538 struct ide_host *host;
1539 int rc;
1541 host = ide_host_alloc(d, hws, n_ports);
1542 if (host == NULL)
1543 return -ENOMEM;
1545 rc = ide_host_register(host, d, hws);
1546 if (rc) {
1547 ide_host_free(host);
1548 return rc;
1551 if (hostp)
1552 *hostp = host;
1554 return 0;
1556 EXPORT_SYMBOL_GPL(ide_host_add);
1558 static void __ide_port_unregister_devices(ide_hwif_t *hwif)
1560 ide_drive_t *drive;
1561 int i;
1563 ide_port_for_each_present_dev(i, drive, hwif) {
1564 device_unregister(&drive->gendev);
1565 wait_for_completion(&drive->gendev_rel_comp);
1569 void ide_port_unregister_devices(ide_hwif_t *hwif)
1571 mutex_lock(&ide_cfg_mtx);
1572 __ide_port_unregister_devices(hwif);
1573 hwif->present = 0;
1574 ide_port_init_devices_data(hwif);
1575 mutex_unlock(&ide_cfg_mtx);
1577 EXPORT_SYMBOL_GPL(ide_port_unregister_devices);
1580 * ide_unregister - free an IDE interface
1581 * @hwif: IDE interface
1583 * Perform the final unregister of an IDE interface.
1585 * Locking:
1586 * The caller must not hold the IDE locks.
1588 * It is up to the caller to be sure there is no pending I/O here,
1589 * and that the interface will not be reopened (present/vanishing
1590 * locking isn't yet done BTW).
1593 static void ide_unregister(ide_hwif_t *hwif)
1595 BUG_ON(in_interrupt());
1596 BUG_ON(irqs_disabled());
1598 mutex_lock(&ide_cfg_mtx);
1600 if (hwif->present) {
1601 __ide_port_unregister_devices(hwif);
1602 hwif->present = 0;
1605 ide_proc_unregister_port(hwif);
1607 if (!hwif->host->get_lock)
1608 free_irq(hwif->irq, hwif);
1610 device_unregister(hwif->portdev);
1611 device_unregister(&hwif->gendev);
1612 wait_for_completion(&hwif->gendev_rel_comp);
1615 * Remove us from the kernel's knowledge
1617 blk_unregister_region(MKDEV(hwif->major, 0), MAX_DRIVES<<PARTN_BITS);
1618 kfree(hwif->sg_table);
1619 unregister_blkdev(hwif->major, hwif->name);
1621 ide_release_dma_engine(hwif);
1623 mutex_unlock(&ide_cfg_mtx);
1626 void ide_host_free(struct ide_host *host)
1628 ide_hwif_t *hwif;
1629 int i;
1631 ide_host_for_each_port(i, hwif, host) {
1632 if (hwif)
1633 ide_port_free(hwif);
1636 kfree(host);
1638 EXPORT_SYMBOL_GPL(ide_host_free);
1640 void ide_host_remove(struct ide_host *host)
1642 ide_hwif_t *hwif;
1643 int i;
1645 ide_host_for_each_port(i, hwif, host) {
1646 if (hwif)
1647 ide_unregister(hwif);
1650 ide_host_free(host);
1652 EXPORT_SYMBOL_GPL(ide_host_remove);
1654 void ide_port_scan(ide_hwif_t *hwif)
1656 int rc;
1658 ide_port_apply_params(hwif);
1659 ide_port_cable_detect(hwif);
1661 hwif->port_flags |= IDE_PFLAG_PROBING;
1663 ide_port_init_devices(hwif);
1665 rc = ide_probe_port(hwif);
1667 hwif->port_flags &= ~IDE_PFLAG_PROBING;
1669 if (rc < 0)
1670 return;
1672 hwif->present = 1;
1674 ide_port_tune_devices(hwif);
1675 ide_port_setup_devices(hwif);
1676 ide_acpi_port_init_devices(hwif);
1677 hwif_register_devices(hwif);
1678 ide_proc_port_register_devices(hwif);
1680 EXPORT_SYMBOL_GPL(ide_port_scan);