WIP FPC-III support
[linux/fpc-iii.git] / drivers / ide / ide-probe.c
blobaefd74c0d8628f747081728f9daa0d44ea79e660
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 fallthrough;
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 void ata_probe(dev_t dev)
907 request_module("ide-disk");
908 request_module("ide-cd");
909 request_module("ide-tape");
910 request_module("ide-floppy");
913 void ide_init_disk(struct gendisk *disk, ide_drive_t *drive)
915 ide_hwif_t *hwif = drive->hwif;
916 unsigned int unit = drive->dn & 1;
918 disk->major = hwif->major;
919 disk->first_minor = unit << PARTN_BITS;
920 sprintf(disk->disk_name, "hd%c", 'a' + hwif->index * MAX_DRIVES + unit);
921 disk->queue = drive->queue;
924 EXPORT_SYMBOL_GPL(ide_init_disk);
926 static void drive_release_dev (struct device *dev)
928 ide_drive_t *drive = container_of(dev, ide_drive_t, gendev);
930 ide_proc_unregister_device(drive);
932 if (drive->sense_rq)
933 blk_mq_free_request(drive->sense_rq);
935 blk_cleanup_queue(drive->queue);
936 drive->queue = NULL;
937 blk_mq_free_tag_set(&drive->tag_set);
939 drive->dev_flags &= ~IDE_DFLAG_PRESENT;
941 complete(&drive->gendev_rel_comp);
944 static int hwif_init(ide_hwif_t *hwif)
946 if (!hwif->irq) {
947 printk(KERN_ERR "%s: disabled, no IRQ\n", hwif->name);
948 return 0;
951 if (__register_blkdev(hwif->major, hwif->name, ata_probe))
952 return 0;
954 if (!hwif->sg_max_nents)
955 hwif->sg_max_nents = PRD_ENTRIES;
957 hwif->sg_table = kmalloc_array(hwif->sg_max_nents,
958 sizeof(struct scatterlist),
959 GFP_KERNEL);
960 if (!hwif->sg_table) {
961 printk(KERN_ERR "%s: unable to allocate SG table.\n", hwif->name);
962 goto out;
965 sg_init_table(hwif->sg_table, hwif->sg_max_nents);
967 if (init_irq(hwif)) {
968 printk(KERN_ERR "%s: disabled, unable to get IRQ %d\n",
969 hwif->name, hwif->irq);
970 goto out;
973 return 1;
975 out:
976 unregister_blkdev(hwif->major, hwif->name);
977 return 0;
980 static void hwif_register_devices(ide_hwif_t *hwif)
982 ide_drive_t *drive;
983 unsigned int i;
985 ide_port_for_each_present_dev(i, drive, hwif) {
986 struct device *dev = &drive->gendev;
987 int ret;
989 dev_set_name(dev, "%u.%u", hwif->index, i);
990 dev_set_drvdata(dev, drive);
991 dev->parent = &hwif->gendev;
992 dev->bus = &ide_bus_type;
993 dev->release = drive_release_dev;
995 ret = device_register(dev);
996 if (ret < 0)
997 printk(KERN_WARNING "IDE: %s: device_register error: "
998 "%d\n", __func__, ret);
1002 static void ide_port_init_devices(ide_hwif_t *hwif)
1004 const struct ide_port_ops *port_ops = hwif->port_ops;
1005 ide_drive_t *drive;
1006 int i;
1008 ide_port_for_each_dev(i, drive, hwif) {
1009 drive->dn = i + hwif->channel * 2;
1011 if (hwif->host_flags & IDE_HFLAG_IO_32BIT)
1012 drive->io_32bit = 1;
1013 if (hwif->host_flags & IDE_HFLAG_NO_IO_32BIT)
1014 drive->dev_flags |= IDE_DFLAG_NO_IO_32BIT;
1015 if (hwif->host_flags & IDE_HFLAG_UNMASK_IRQS)
1016 drive->dev_flags |= IDE_DFLAG_UNMASK;
1017 if (hwif->host_flags & IDE_HFLAG_NO_UNMASK_IRQS)
1018 drive->dev_flags |= IDE_DFLAG_NO_UNMASK;
1020 drive->pio_mode = XFER_PIO_0;
1022 if (port_ops && port_ops->init_dev)
1023 port_ops->init_dev(drive);
1027 static void ide_init_port(ide_hwif_t *hwif, unsigned int port,
1028 const struct ide_port_info *d)
1030 hwif->channel = port;
1032 hwif->chipset = d->chipset ? d->chipset : ide_pci;
1034 if (d->init_iops)
1035 d->init_iops(hwif);
1037 /* ->host_flags may be set by ->init_iops (or even earlier...) */
1038 hwif->host_flags |= d->host_flags;
1039 hwif->pio_mask = d->pio_mask;
1041 if (d->tp_ops)
1042 hwif->tp_ops = d->tp_ops;
1044 /* ->set_pio_mode for DTC2278 is currently limited to port 0 */
1045 if ((hwif->host_flags & IDE_HFLAG_DTC2278) == 0 || hwif->channel == 0)
1046 hwif->port_ops = d->port_ops;
1048 hwif->swdma_mask = d->swdma_mask;
1049 hwif->mwdma_mask = d->mwdma_mask;
1050 hwif->ultra_mask = d->udma_mask;
1052 if ((d->host_flags & IDE_HFLAG_NO_DMA) == 0) {
1053 int rc;
1055 hwif->dma_ops = d->dma_ops;
1057 if (d->init_dma)
1058 rc = d->init_dma(hwif, d);
1059 else
1060 rc = ide_hwif_setup_dma(hwif, d);
1062 if (rc < 0) {
1063 printk(KERN_INFO "%s: DMA disabled\n", hwif->name);
1065 hwif->dma_ops = NULL;
1066 hwif->dma_base = 0;
1067 hwif->swdma_mask = 0;
1068 hwif->mwdma_mask = 0;
1069 hwif->ultra_mask = 0;
1073 if ((d->host_flags & IDE_HFLAG_SERIALIZE) ||
1074 ((d->host_flags & IDE_HFLAG_SERIALIZE_DMA) && hwif->dma_base))
1075 hwif->host->host_flags |= IDE_HFLAG_SERIALIZE;
1077 if (d->max_sectors)
1078 hwif->rqsize = d->max_sectors;
1079 else {
1080 if ((hwif->host_flags & IDE_HFLAG_NO_LBA48) ||
1081 (hwif->host_flags & IDE_HFLAG_NO_LBA48_DMA))
1082 hwif->rqsize = 256;
1083 else
1084 hwif->rqsize = 65536;
1087 /* call chipset specific routine for each enabled port */
1088 if (d->init_hwif)
1089 d->init_hwif(hwif);
1092 static void ide_port_cable_detect(ide_hwif_t *hwif)
1094 const struct ide_port_ops *port_ops = hwif->port_ops;
1096 if (port_ops && port_ops->cable_detect && (hwif->ultra_mask & 0x78)) {
1097 if (hwif->cbl != ATA_CBL_PATA40_SHORT)
1098 hwif->cbl = port_ops->cable_detect(hwif);
1103 * Deferred request list insertion handler
1105 static void drive_rq_insert_work(struct work_struct *work)
1107 ide_drive_t *drive = container_of(work, ide_drive_t, rq_work);
1108 ide_hwif_t *hwif = drive->hwif;
1109 struct request *rq;
1110 blk_status_t ret;
1111 LIST_HEAD(list);
1113 blk_mq_quiesce_queue(drive->queue);
1115 ret = BLK_STS_OK;
1116 spin_lock_irq(&hwif->lock);
1117 while (!list_empty(&drive->rq_list)) {
1118 rq = list_first_entry(&drive->rq_list, struct request, queuelist);
1119 list_del_init(&rq->queuelist);
1121 spin_unlock_irq(&hwif->lock);
1122 ret = ide_issue_rq(drive, rq, true);
1123 spin_lock_irq(&hwif->lock);
1125 spin_unlock_irq(&hwif->lock);
1127 blk_mq_unquiesce_queue(drive->queue);
1129 if (ret != BLK_STS_OK)
1130 kblockd_schedule_work(&drive->rq_work);
1133 static const u8 ide_hwif_to_major[] =
1134 { IDE0_MAJOR, IDE1_MAJOR, IDE2_MAJOR, IDE3_MAJOR, IDE4_MAJOR,
1135 IDE5_MAJOR, IDE6_MAJOR, IDE7_MAJOR, IDE8_MAJOR, IDE9_MAJOR };
1137 static void ide_port_init_devices_data(ide_hwif_t *hwif)
1139 ide_drive_t *drive;
1140 int i;
1142 ide_port_for_each_dev(i, drive, hwif) {
1143 u8 j = (hwif->index * MAX_DRIVES) + i;
1144 u16 *saved_id = drive->id;
1146 memset(drive, 0, sizeof(*drive));
1147 memset(saved_id, 0, SECTOR_SIZE);
1148 drive->id = saved_id;
1150 drive->media = ide_disk;
1151 drive->select = (i << 4) | ATA_DEVICE_OBS;
1152 drive->hwif = hwif;
1153 drive->ready_stat = ATA_DRDY;
1154 drive->bad_wstat = BAD_W_STAT;
1155 drive->special_flags = IDE_SFLAG_RECALIBRATE |
1156 IDE_SFLAG_SET_GEOMETRY;
1157 drive->name[0] = 'h';
1158 drive->name[1] = 'd';
1159 drive->name[2] = 'a' + j;
1160 drive->max_failures = IDE_DEFAULT_MAX_FAILURES;
1162 INIT_LIST_HEAD(&drive->list);
1163 init_completion(&drive->gendev_rel_comp);
1165 INIT_WORK(&drive->rq_work, drive_rq_insert_work);
1166 INIT_LIST_HEAD(&drive->rq_list);
1170 static void ide_init_port_data(ide_hwif_t *hwif, unsigned int index)
1172 /* fill in any non-zero initial values */
1173 hwif->index = index;
1174 hwif->major = ide_hwif_to_major[index];
1176 hwif->name[0] = 'i';
1177 hwif->name[1] = 'd';
1178 hwif->name[2] = 'e';
1179 hwif->name[3] = '0' + index;
1181 spin_lock_init(&hwif->lock);
1183 timer_setup(&hwif->timer, ide_timer_expiry, 0);
1185 init_completion(&hwif->gendev_rel_comp);
1187 hwif->tp_ops = &default_tp_ops;
1189 ide_port_init_devices_data(hwif);
1192 static void ide_init_port_hw(ide_hwif_t *hwif, struct ide_hw *hw)
1194 memcpy(&hwif->io_ports, &hw->io_ports, sizeof(hwif->io_ports));
1195 hwif->irq = hw->irq;
1196 hwif->dev = hw->dev;
1197 hwif->gendev.parent = hw->parent ? hw->parent : hw->dev;
1198 hwif->config_data = hw->config;
1201 static unsigned int ide_indexes;
1204 * ide_find_port_slot - find free port slot
1205 * @d: IDE port info
1207 * Return the new port slot index or -ENOENT if we are out of free slots.
1210 static int ide_find_port_slot(const struct ide_port_info *d)
1212 int idx = -ENOENT;
1213 u8 bootable = (d && (d->host_flags & IDE_HFLAG_NON_BOOTABLE)) ? 0 : 1;
1214 u8 i = (d && (d->host_flags & IDE_HFLAG_QD_2ND_PORT)) ? 1 : 0;
1217 * Claim an unassigned slot.
1219 * Give preference to claiming other slots before claiming ide0/ide1,
1220 * just in case there's another interface yet-to-be-scanned
1221 * which uses ports 0x1f0/0x170 (the ide0/ide1 defaults).
1223 * Unless there is a bootable card that does not use the standard
1224 * ports 0x1f0/0x170 (the ide0/ide1 defaults).
1226 mutex_lock(&ide_cfg_mtx);
1227 if (bootable) {
1228 if ((ide_indexes | i) != (1 << MAX_HWIFS) - 1)
1229 idx = ffz(ide_indexes | i);
1230 } else {
1231 if ((ide_indexes | 3) != (1 << MAX_HWIFS) - 1)
1232 idx = ffz(ide_indexes | 3);
1233 else if ((ide_indexes & 3) != 3)
1234 idx = ffz(ide_indexes);
1236 if (idx >= 0)
1237 ide_indexes |= (1 << idx);
1238 mutex_unlock(&ide_cfg_mtx);
1240 return idx;
1243 static void ide_free_port_slot(int idx)
1245 mutex_lock(&ide_cfg_mtx);
1246 ide_indexes &= ~(1 << idx);
1247 mutex_unlock(&ide_cfg_mtx);
1250 static void ide_port_free_devices(ide_hwif_t *hwif)
1252 ide_drive_t *drive;
1253 int i;
1255 ide_port_for_each_dev(i, drive, hwif) {
1256 kfree(drive->id);
1257 kfree(drive);
1261 static int ide_port_alloc_devices(ide_hwif_t *hwif, int node)
1263 ide_drive_t *drive;
1264 int i;
1266 for (i = 0; i < MAX_DRIVES; i++) {
1267 drive = kzalloc_node(sizeof(*drive), GFP_KERNEL, node);
1268 if (drive == NULL)
1269 goto out_nomem;
1272 * In order to keep things simple we have an id
1273 * block for all drives at all times. If the device
1274 * is pre ATA or refuses ATA/ATAPI identify we
1275 * will add faked data to this.
1277 * Also note that 0 everywhere means "can't do X"
1279 drive->id = kzalloc_node(SECTOR_SIZE, GFP_KERNEL, node);
1280 if (drive->id == NULL)
1281 goto out_free_drive;
1283 hwif->devices[i] = drive;
1285 return 0;
1287 out_free_drive:
1288 kfree(drive);
1289 out_nomem:
1290 ide_port_free_devices(hwif);
1291 return -ENOMEM;
1294 struct ide_host *ide_host_alloc(const struct ide_port_info *d,
1295 struct ide_hw **hws, unsigned int n_ports)
1297 struct ide_host *host;
1298 struct device *dev = hws[0] ? hws[0]->dev : NULL;
1299 int node = dev ? dev_to_node(dev) : -1;
1300 int i;
1302 host = kzalloc_node(sizeof(*host), GFP_KERNEL, node);
1303 if (host == NULL)
1304 return NULL;
1306 for (i = 0; i < n_ports; i++) {
1307 ide_hwif_t *hwif;
1308 int idx;
1310 if (hws[i] == NULL)
1311 continue;
1313 hwif = kzalloc_node(sizeof(*hwif), GFP_KERNEL, node);
1314 if (hwif == NULL)
1315 continue;
1317 if (ide_port_alloc_devices(hwif, node) < 0) {
1318 kfree(hwif);
1319 continue;
1322 idx = ide_find_port_slot(d);
1323 if (idx < 0) {
1324 printk(KERN_ERR "%s: no free slot for interface\n",
1325 d ? d->name : "ide");
1326 ide_port_free_devices(hwif);
1327 kfree(hwif);
1328 continue;
1331 ide_init_port_data(hwif, idx);
1333 hwif->host = host;
1335 host->ports[i] = hwif;
1336 host->n_ports++;
1339 if (host->n_ports == 0) {
1340 kfree(host);
1341 return NULL;
1344 host->dev[0] = dev;
1346 if (d) {
1347 host->init_chipset = d->init_chipset;
1348 host->get_lock = d->get_lock;
1349 host->release_lock = d->release_lock;
1350 host->host_flags = d->host_flags;
1351 host->irq_flags = d->irq_flags;
1354 return host;
1356 EXPORT_SYMBOL_GPL(ide_host_alloc);
1358 static void ide_port_free(ide_hwif_t *hwif)
1360 ide_port_free_devices(hwif);
1361 ide_free_port_slot(hwif->index);
1362 kfree(hwif);
1365 static void ide_disable_port(ide_hwif_t *hwif)
1367 struct ide_host *host = hwif->host;
1368 int i;
1370 printk(KERN_INFO "%s: disabling port\n", hwif->name);
1372 for (i = 0; i < MAX_HOST_PORTS; i++) {
1373 if (host->ports[i] == hwif) {
1374 host->ports[i] = NULL;
1375 host->n_ports--;
1379 ide_port_free(hwif);
1382 int ide_host_register(struct ide_host *host, const struct ide_port_info *d,
1383 struct ide_hw **hws)
1385 ide_hwif_t *hwif, *mate = NULL;
1386 int i, j = 0;
1388 pr_warn("legacy IDE will be removed in 2021, please switch to libata\n"
1389 "Report any missing HW support to linux-ide@vger.kernel.org\n");
1391 ide_host_for_each_port(i, hwif, host) {
1392 if (hwif == NULL) {
1393 mate = NULL;
1394 continue;
1397 ide_init_port_hw(hwif, hws[i]);
1398 ide_port_apply_params(hwif);
1400 if ((i & 1) && mate) {
1401 hwif->mate = mate;
1402 mate->mate = hwif;
1405 mate = (i & 1) ? NULL : hwif;
1407 ide_init_port(hwif, i & 1, d);
1408 ide_port_cable_detect(hwif);
1410 hwif->port_flags |= IDE_PFLAG_PROBING;
1412 ide_port_init_devices(hwif);
1415 ide_host_for_each_port(i, hwif, host) {
1416 if (hwif == NULL)
1417 continue;
1419 if (ide_probe_port(hwif) == 0)
1420 hwif->present = 1;
1422 hwif->port_flags &= ~IDE_PFLAG_PROBING;
1424 if ((hwif->host_flags & IDE_HFLAG_4DRIVES) == 0 ||
1425 hwif->mate == NULL || hwif->mate->present == 0) {
1426 if (ide_register_port(hwif)) {
1427 ide_disable_port(hwif);
1428 continue;
1432 if (hwif->present)
1433 ide_port_tune_devices(hwif);
1436 ide_host_enable_irqs(host);
1438 ide_host_for_each_port(i, hwif, host) {
1439 if (hwif == NULL)
1440 continue;
1442 if (hwif_init(hwif) == 0) {
1443 printk(KERN_INFO "%s: failed to initialize IDE "
1444 "interface\n", hwif->name);
1445 device_unregister(hwif->portdev);
1446 device_unregister(&hwif->gendev);
1447 ide_disable_port(hwif);
1448 continue;
1451 if (hwif->present)
1452 if (ide_port_setup_devices(hwif) == 0) {
1453 hwif->present = 0;
1454 continue;
1457 j++;
1459 ide_acpi_init_port(hwif);
1461 if (hwif->present)
1462 ide_acpi_port_init_devices(hwif);
1465 ide_host_for_each_port(i, hwif, host) {
1466 if (hwif == NULL)
1467 continue;
1469 ide_sysfs_register_port(hwif);
1470 ide_proc_register_port(hwif);
1472 if (hwif->present) {
1473 ide_proc_port_register_devices(hwif);
1474 hwif_register_devices(hwif);
1478 return j ? 0 : -1;
1480 EXPORT_SYMBOL_GPL(ide_host_register);
1482 int ide_host_add(const struct ide_port_info *d, struct ide_hw **hws,
1483 unsigned int n_ports, struct ide_host **hostp)
1485 struct ide_host *host;
1486 int rc;
1488 host = ide_host_alloc(d, hws, n_ports);
1489 if (host == NULL)
1490 return -ENOMEM;
1492 rc = ide_host_register(host, d, hws);
1493 if (rc) {
1494 ide_host_free(host);
1495 return rc;
1498 if (hostp)
1499 *hostp = host;
1501 return 0;
1503 EXPORT_SYMBOL_GPL(ide_host_add);
1505 static void __ide_port_unregister_devices(ide_hwif_t *hwif)
1507 ide_drive_t *drive;
1508 int i;
1510 ide_port_for_each_present_dev(i, drive, hwif) {
1511 device_unregister(&drive->gendev);
1512 wait_for_completion(&drive->gendev_rel_comp);
1516 void ide_port_unregister_devices(ide_hwif_t *hwif)
1518 mutex_lock(&ide_cfg_mtx);
1519 __ide_port_unregister_devices(hwif);
1520 hwif->present = 0;
1521 ide_port_init_devices_data(hwif);
1522 mutex_unlock(&ide_cfg_mtx);
1524 EXPORT_SYMBOL_GPL(ide_port_unregister_devices);
1527 * ide_unregister - free an IDE interface
1528 * @hwif: IDE interface
1530 * Perform the final unregister of an IDE interface.
1532 * Locking:
1533 * The caller must not hold the IDE locks.
1535 * It is up to the caller to be sure there is no pending I/O here,
1536 * and that the interface will not be reopened (present/vanishing
1537 * locking isn't yet done BTW).
1540 static void ide_unregister(ide_hwif_t *hwif)
1542 mutex_lock(&ide_cfg_mtx);
1544 if (hwif->present) {
1545 __ide_port_unregister_devices(hwif);
1546 hwif->present = 0;
1549 ide_proc_unregister_port(hwif);
1551 if (!hwif->host->get_lock)
1552 free_irq(hwif->irq, hwif);
1554 device_unregister(hwif->portdev);
1555 device_unregister(&hwif->gendev);
1556 wait_for_completion(&hwif->gendev_rel_comp);
1559 * Remove us from the kernel's knowledge
1561 kfree(hwif->sg_table);
1562 unregister_blkdev(hwif->major, hwif->name);
1564 ide_release_dma_engine(hwif);
1566 mutex_unlock(&ide_cfg_mtx);
1569 void ide_host_free(struct ide_host *host)
1571 ide_hwif_t *hwif;
1572 int i;
1574 ide_host_for_each_port(i, hwif, host) {
1575 if (hwif)
1576 ide_port_free(hwif);
1579 kfree(host);
1581 EXPORT_SYMBOL_GPL(ide_host_free);
1583 void ide_host_remove(struct ide_host *host)
1585 ide_hwif_t *hwif;
1586 int i;
1588 ide_host_for_each_port(i, hwif, host) {
1589 if (hwif)
1590 ide_unregister(hwif);
1593 ide_host_free(host);
1595 EXPORT_SYMBOL_GPL(ide_host_remove);
1597 void ide_port_scan(ide_hwif_t *hwif)
1599 int rc;
1601 ide_port_apply_params(hwif);
1602 ide_port_cable_detect(hwif);
1604 hwif->port_flags |= IDE_PFLAG_PROBING;
1606 ide_port_init_devices(hwif);
1608 rc = ide_probe_port(hwif);
1610 hwif->port_flags &= ~IDE_PFLAG_PROBING;
1612 if (rc < 0)
1613 return;
1615 hwif->present = 1;
1617 ide_port_tune_devices(hwif);
1618 ide_port_setup_devices(hwif);
1619 ide_acpi_port_init_devices(hwif);
1620 hwif_register_devices(hwif);
1621 ide_proc_port_register_devices(hwif);
1623 EXPORT_SYMBOL_GPL(ide_port_scan);