*/
 void ide_acpi_set_state(ide_hwif_t *hwif, int on)
 {
-       int unit;
+       ide_drive_t *drive;
+       int i;
 
        if (ide_noacpi || ide_noacpi_psx)
                return;
        /* channel first and then drives for power on and verse versa for power off */
        if (on)
                acpi_bus_set_power(hwif->acpidata->obj_handle, ACPI_STATE_D0);
-       for (unit = 0; unit < MAX_DRIVES; ++unit) {
-               ide_drive_t *drive = hwif->devices[unit];
 
+       ide_port_for_each_dev(i, drive, hwif) {
                if (!drive->acpidata->obj_handle)
                        drive->acpidata->obj_handle = ide_acpi_drive_get_handle(drive);
 
        /*
         * Send IDENTIFY for each drive
         */
-       for (i = 0; i < MAX_DRIVES; i++) {
-               drive = hwif->devices[i];
-
+       ide_port_for_each_dev(i, drive, hwif) {
                memset(drive->acpidata, 0, sizeof(*drive->acpidata));
 
                if ((drive->dev_flags & IDE_DFLAG_PRESENT) == 0)
        ide_acpi_get_timing(hwif);
        ide_acpi_push_timing(hwif);
 
-       for (i = 0; i < MAX_DRIVES; i++) {
-               drive = hwif->devices[i];
-
+       ide_port_for_each_dev(i, drive, hwif) {
                if (drive->dev_flags & IDE_DFLAG_PRESENT)
                        /* Execute ACPI startup code */
                        ide_acpi_exec_tfs(drive);
 
        struct ide_io_ports *io_ports = &hwif->io_ports;
        const struct ide_tp_ops *tp_ops = hwif->tp_ops;
        const struct ide_port_ops *port_ops;
+       ide_drive_t *tdrive;
        unsigned long flags, timeout;
-       unsigned int unit;
+       int i;
        DEFINE_WAIT(wait);
 
        spin_lock_irqsave(&hwif->lock, flags);
 
                prepare_to_wait(&ide_park_wq, &wait, TASK_UNINTERRUPTIBLE);
                timeout = jiffies;
-               for (unit = 0; unit < MAX_DRIVES; unit++) {
-                       ide_drive_t *tdrive = hwif->devices[unit];
-
+               ide_port_for_each_dev(i, tdrive, hwif) {
                        if (tdrive->dev_flags & IDE_DFLAG_PRESENT &&
                            tdrive->dev_flags & IDE_DFLAG_PARKED &&
                            time_after(tdrive->sleep, timeout))
         * First, reset any device state data we were maintaining
         * for any of the drives on this interface.
         */
-       for (unit = 0; unit < MAX_DRIVES; ++unit)
-               pre_reset(hwif->devices[unit]);
+       ide_port_for_each_dev(i, tdrive, hwif)
+               pre_reset(tdrive);
 
        if (io_ports->ctl_addr == 0) {
                spin_unlock_irqrestore(&hwif->lock, flags);
 
 
 static int ide_port_wait_ready(ide_hwif_t *hwif)
 {
-       int unit, rc;
+       ide_drive_t *drive;
+       int i, rc;
 
        printk(KERN_DEBUG "Probing IDE interface %s...\n", hwif->name);
 
                return rc;
 
        /* Now make sure both master & slave are ready */
-       for (unit = 0; unit < MAX_DRIVES; unit++) {
-               ide_drive_t *drive = hwif->devices[unit];
-
+       ide_port_for_each_dev(i, drive, hwif) {
                /* Ignore disks that we will not probe for later. */
                if ((drive->dev_flags & IDE_DFLAG_NOPROBE) == 0 ||
                    (drive->dev_flags & IDE_DFLAG_PRESENT)) {
        }
 out:
        /* Exit function with master reselected (let's be sane) */
-       if (unit)
+       if (i)
                SELECT_DRIVE(hwif->devices[0]);
 
        return rc;
 
 static int ide_probe_port(ide_hwif_t *hwif)
 {
+       ide_drive_t *drive;
        unsigned long flags;
        unsigned int irqd;
-       int unit, rc = -ENODEV;
+       int i, rc = -ENODEV;
 
        BUG_ON(hwif->present);
 
         * Second drive should only exist if first drive was found,
         * but a lot of cdrom drives are configured as single slaves.
         */
-       for (unit = 0; unit < MAX_DRIVES; ++unit) {
-               ide_drive_t *drive = hwif->devices[unit];
-
+       ide_port_for_each_dev(i, drive, hwif) {
                (void) probe_for_drive(drive);
                if (drive->dev_flags & IDE_DFLAG_PRESENT)
                        rc = 0;
 static void ide_port_tune_devices(ide_hwif_t *hwif)
 {
        const struct ide_port_ops *port_ops = hwif->port_ops;
-       int unit;
-
-       for (unit = 0; unit < MAX_DRIVES; unit++) {
-               ide_drive_t *drive = hwif->devices[unit];
+       ide_drive_t *drive;
+       int i;
 
+       ide_port_for_each_dev(i, drive, hwif) {
                if (drive->dev_flags & IDE_DFLAG_PRESENT) {
                        if (port_ops && port_ops->quirkproc)
                                port_ops->quirkproc(drive);
                }
        }
 
-       for (unit = 0; unit < MAX_DRIVES; ++unit) {
-               ide_drive_t *drive = hwif->devices[unit];
-
+       ide_port_for_each_dev(i, drive, hwif) {
                if (drive->dev_flags & IDE_DFLAG_PRESENT) {
                        ide_set_max_pio(drive);
 
                }
        }
 
-       for (unit = 0; unit < MAX_DRIVES; ++unit) {
-               ide_drive_t *drive = hwif->devices[unit];
-
+       ide_port_for_each_dev(i, drive, hwif) {
                if ((hwif->host_flags & IDE_HFLAG_NO_IO_32BIT) ||
                    drive->id[ATA_ID_DWORD_IO])
                        drive->dev_flags |= IDE_DFLAG_NO_IO_32BIT;
  */
 static int ide_port_setup_devices(ide_hwif_t *hwif)
 {
+       ide_drive_t *drive;
        int i, j = 0;
 
        mutex_lock(&ide_cfg_mtx);
-       for (i = 0; i < MAX_DRIVES; i++) {
-               ide_drive_t *drive = hwif->devices[i];
-
+       ide_port_for_each_dev(i, drive, hwif) {
                if ((drive->dev_flags & IDE_DFLAG_PRESENT) == 0)
                        continue;
 
 
 static void hwif_register_devices(ide_hwif_t *hwif)
 {
+       ide_drive_t *drive;
        unsigned int i;
 
-       for (i = 0; i < MAX_DRIVES; i++) {
-               ide_drive_t *drive = hwif->devices[i];
+       ide_port_for_each_dev(i, drive, hwif) {
                struct device *dev = &drive->gendev;
                int ret;
 
 static void ide_port_init_devices(ide_hwif_t *hwif)
 {
        const struct ide_port_ops *port_ops = hwif->port_ops;
+       ide_drive_t *drive;
        int i;
 
-       for (i = 0; i < MAX_DRIVES; i++) {
-               ide_drive_t *drive = hwif->devices[i];
-
+       ide_port_for_each_dev(i, drive, hwif) {
                drive->dn = i + hwif->channel * 2;
 
                if (hwif->host_flags & IDE_HFLAG_IO_32BIT)
 
 static void ide_port_init_devices_data(ide_hwif_t *hwif)
 {
-       int unit;
+       ide_drive_t *drive;
+       int i;
 
-       for (unit = 0; unit < MAX_DRIVES; ++unit) {
-               ide_drive_t *drive = hwif->devices[unit];
-               u8 j = (hwif->index * MAX_DRIVES) + unit;
+       ide_port_for_each_dev(i, drive, hwif) {
+               u8 j = (hwif->index * MAX_DRIVES) + i;
 
                memset(drive, 0, sizeof(*drive));
 
                drive->media                    = ide_disk;
-               drive->select                   = (unit << 4) | ATA_DEVICE_OBS;
+               drive->select                   = (i << 4) | ATA_DEVICE_OBS;
                drive->hwif                     = hwif;
                drive->ready_stat               = ATA_DRDY;
                drive->bad_wstat                = BAD_W_STAT;
 
 static void ide_port_free_devices(ide_hwif_t *hwif)
 {
+       ide_drive_t *drive;
        int i;
 
-       for (i = 0; i < MAX_DRIVES; i++)
-               kfree(hwif->devices[i]);
+       ide_port_for_each_dev(i, drive, hwif)
+               kfree(drive);
 }
 
 static int ide_port_alloc_devices(ide_hwif_t *hwif, int node)
        ide_hwif_t *hwif, *mate = NULL;
        int i, j = 0;
 
-       for (i = 0; i < MAX_HOST_PORTS; i++) {
-               hwif = host->ports[i];
-
+       ide_host_for_each_port(i, hwif, host) {
                if (hwif == NULL) {
                        mate = NULL;
                        continue;
                ide_port_init_devices(hwif);
        }
 
-       for (i = 0; i < MAX_HOST_PORTS; i++) {
-               hwif = host->ports[i];
-
+       ide_host_for_each_port(i, hwif, host) {
                if (hwif == NULL)
                        continue;
 
                        ide_port_tune_devices(hwif);
        }
 
-       for (i = 0; i < MAX_HOST_PORTS; i++) {
-               hwif = host->ports[i];
-
+       ide_host_for_each_port(i, hwif, host) {
                if (hwif == NULL)
                        continue;
 
                        ide_acpi_port_init_devices(hwif);
        }
 
-       for (i = 0; i < MAX_HOST_PORTS; i++) {
-               hwif = host->ports[i];
-
+       ide_host_for_each_port(i, hwif, host) {
                if (hwif == NULL)
                        continue;
 
                        hwif_register_devices(hwif);
        }
 
-       for (i = 0; i < MAX_HOST_PORTS; i++) {
-               hwif = host->ports[i];
-
+       ide_host_for_each_port(i, hwif, host) {
                if (hwif == NULL)
                        continue;
 
 
 static void __ide_port_unregister_devices(ide_hwif_t *hwif)
 {
+       ide_drive_t *drive;
        int i;
 
-       for (i = 0; i < MAX_DRIVES; i++) {
-               ide_drive_t *drive = hwif->devices[i];
-
+       ide_port_for_each_dev(i, drive, hwif) {
                if (drive->dev_flags & IDE_DFLAG_PRESENT) {
                        device_unregister(&drive->gendev);
                        wait_for_completion(&drive->gendev_rel_comp);
        ide_hwif_t *hwif;
        int i;
 
-       for (i = 0; i < MAX_HOST_PORTS; i++) {
-               hwif = host->ports[i];
-
+       ide_host_for_each_port(i, hwif, host) {
                if (hwif == NULL)
                        continue;
 
 
 void ide_host_remove(struct ide_host *host)
 {
+       ide_hwif_t *hwif;
        int i;
 
-       for (i = 0; i < MAX_HOST_PORTS; i++) {
-               if (host->ports[i])
-                       ide_unregister(host->ports[i]);
+       ide_host_for_each_port(i, hwif, host) {
+               if (hwif)
+                       ide_unregister(hwif);
        }
 
        ide_host_free(host);