Skip to contentSkip to conversation

amd74xx: use ide_get_pair_dev() helper

PATCH In review Bartlomiej Zolnierkiewicz 2 participants 11 messages Dec 21, 2008 – Dec 22, 2008 libata

lore
Share & tools
lore
#0 lore raw

PATCH 1/7 amd74xx: use ide_get_pair_dev() helper

From: Bartlomiej Zolnierkiewicz <[email protected]>
Subject: [PATCH] amd74xx: use ide_get_pair_dev() helper

There should be no functional changes caused by this patch.

---
updates for pata-2.6 tree

 drivers/ide/amd74xx.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/ide/amd74xx.c +2 -2
Index: b/drivers/ide/amd74xx.c
===================================================================
@@ -82,7 +82,7 @@ static void amd_set_drive(ide_drive_t *d
 {
 	ide_hwif_t *hwif = drive->hwif;
 	struct pci_dev *dev = to_pci_dev(hwif->dev);
-	ide_drive_t *peer = hwif->drives + (~drive->dn & 1);
+	ide_drive_t *peer = ide_get_pair_dev(drive);
 	struct ide_timing t, p;
 	int T, UT;
 	u8 udma_mask = hwif->ultra_mask;
@@ -92,7 +92,7 @@ static void amd_set_drive(ide_drive_t *d
 
 	ide_timing_compute(drive, speed, &t, T, UT);
 
-	if (peer->dev_flags & IDE_DFLAG_PRESENT) {
+	if (peer) {
 		ide_timing_compute(peer, peer->current_speed, &p, T, UT);
 		ide_timing_merge(&p, &t, &t, IDE_TIMING_8BIT);
 	}
#1 lore raw

PATCH 2/7 cmd640: use ide_get_pair_dev() helper

From: Bartlomiej Zolnierkiewicz <[email protected]>
Subject: [PATCH] cmd640: use ide_get_pair_dev() helper

There should be no functional changes caused by this patch.

---
 drivers/ide/cmd640.c |    5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/ide/cmd640.c +2 -3
Index: b/drivers/ide/cmd640.c
===================================================================
@@ -467,11 +467,10 @@ static void program_drive_counts(ide_dri
 	 * so we merge the timings, using the slowest value for each timing.
 	 */
 	if (index > 1) {
-		ide_hwif_t *hwif = drive->hwif;
-		ide_drive_t *peer = &hwif->drives[!(drive->dn & 1)];
+		ide_drive_t *peer = ide_get_pair_dev(drive);
 		unsigned int mate = index ^ 1;
 
-		if (peer->dev_flags & IDE_DFLAG_PRESENT) {
+		if (peer) {
 			if (setup_count < setup_counts[mate])
 				setup_count = setup_counts[mate];
 			if (active_count < active_counts[mate])
#3 lore raw

PATCH 3/7 cmd64x: use ide_get_pair_dev() helper

In reply to Bartlomiej Zolnierkiewicz

From: Bartlomiej Zolnierkiewicz <[email protected]>
Subject: [PATCH] cmd64x: use ide_get_pair_dev() helper

There should be no functional changes caused by this patch.

---
 drivers/ide/cmd64x.c |    6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/ide/cmd64x.c +4 -2
Index: b/drivers/ide/cmd64x.c
===================================================================
@@ -138,10 +138,12 @@ static void cmd64x_tune_pio(ide_drive_t 
 	 * the slowest address setup timing ourselves.
 	 */
 	if (hwif->channel) {
-		ide_drive_t *drives = hwif->drives;
+		ide_drive_t *pair = ide_get_pair_dev(drive);
 
 		drive->drive_data = setup_count;
-		setup_count = max(drives[0].drive_data, drives[1].drive_data);
+
+		if (pair)
+			setup_count = max_t(u8, setup_count, pair->drive_data);
 	}
 
 	if (setup_count > 5)		/* shouldn't actually happen... */
#5 lore raw

PATCH 4/7 it821x: use ide_get_pair_dev() helper

In reply to Bartlomiej Zolnierkiewicz

From: Bartlomiej Zolnierkiewicz <[email protected]>
Subject: [PATCH] it821x: use ide_get_pair_dev() helper

Because presence of the peer device was not checked in it821x_set_pio_mode() PIO0 mode was used for taskfile PIO in single device configurations.

---
 drivers/ide/it821x.c |    8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
drivers/ide/it821x.c +2 -6
Index: b/drivers/ide/it821x.c
===================================================================
@@ -167,12 +167,10 @@ static void it821x_clock_strategy(ide_dr
 	ide_hwif_t *hwif = drive->hwif;
 	struct pci_dev *dev = to_pci_dev(hwif->dev);
 	struct it821x_dev *itdev = ide_get_hwifdata(hwif);
-	ide_drive_t *pair;
+	ide_drive_t *pair = ide_get_pair_dev(drive);
 	int clock, altclock, sel = 0;
 	u8 unit = drive->dn & 1, v;
 
-	pair = &hwif->drives[1 - unit];
-
 	if(itdev->want[0][0] > itdev->want[1][0]) {
 		clock = itdev->want[0][1];
 		altclock = itdev->want[1][1];
@@ -239,15 +237,13 @@ static void it821x_set_pio_mode(ide_driv
 {
 	ide_hwif_t *hwif = drive->hwif;
 	struct it821x_dev *itdev = ide_get_hwifdata(hwif);
-	ide_drive_t *pair;
+	ide_drive_t *pair = ide_get_pair_dev(drive);
 	u8 unit = drive->dn & 1, set_pio = pio;
 
 	/* Spec says 89 ref driver uses 88 */
 	static u16 pio_timings[]= { 0xAA88, 0xA382, 0xA181, 0x3332, 0x3121 };
 	static u8 pio_want[]    = { ATA_66, ATA_66, ATA_66, ATA_66, ATA_ANY };
 
-	pair = &hwif->drives[1 - unit];
-
 	/*
 	 * Compute the best PIO mode we can for a given device. We must
 	 * pick a speed that does not cause problems with the other device
Acked-by on v1 4/7
#6 lore raw

Re: [PATCH 4/7] it821x: use ide_get_pair_dev() helper

Quoted 6 lines · Bartlomiej Zolnierkiewicz

Because presence of the peer device was not checked in it821x_set_pio_mode() PIO0 mode was used for taskfile PIO in single device configurations.

Quoted 25 lines

Index: b/drivers/ide/it821x.c
===================================================================

drivers/ide/it821x.c +2 -4
@@ -167,12 +167,10 @@ static void it821x_clock_strategy(ide_dr
 	ide_hwif_t *hwif = drive->hwif;
 	struct pci_dev *dev = to_pci_dev(hwif->dev);
 	struct it821x_dev *itdev = ide_get_hwifdata(hwif);
-	ide_drive_t *pair;
+	ide_drive_t *pair = ide_get_pair_dev(drive);
 	int clock, altclock, sel = 0;
 	u8 unit = drive->dn & 1, v;
 
-	pair = &hwif->drives[1 - unit];
-
 	if(itdev->want[0][0] > itdev->want[1][0]) {
 		clock = itdev->want[0][1];
 		altclock = itdev->want[1][1];
@@ -239,15 +237,13 @@ static void it821x_set_pio_mode(ide_driv
 {
 	ide_hwif_t *hwif = drive->hwif;
 	struct it821x_dev *itdev = ide_get_hwifdata(hwif);
-	ide_drive_t *pair;
+	ide_drive_t *pair = ide_get_pair_dev(drive);
 	u8 unit = drive->dn & 1, set_pio = pio;

drivers/ide/it821x.c static void it821x_set_pio_mode(ide_driv · line 249

Quoted 10 lines
drivers/ide/it821x.c +0 -2
 
 	/* Spec says 89 ref driver uses 88 */
 	static u16 pio_timings[]= { 0xAA88, 0xA382, 0xA181, 0x3332, 0x3121 };
 	static u8 pio_want[]    = { ATA_66, ATA_66, ATA_66, ATA_66, ATA_ANY };
 
-	pair = &hwif->drives[1 - unit];
-
 	/*
 	 * Compute the best PIO mode we can for a given device. We must
 	 * pick a speed that does not cause problems with the other device

I wonder why this driver was testing 'pair', there was absolutely no
chance for it being NULL. :-)

MBR, Sergei

#7 lore raw

PATCH 5/7 ide: NUMA aware allocation of host and port structures

In reply to Bartlomiej Zolnierkiewicz

From: Bartlomiej Zolnierkiewicz <[email protected]>
Subject: [PATCH] ide: NUMA aware allocation of host and port structures

kzalloc() -> kzalloc_node() in ide_host_alloc().

---
 drivers/ide/ide-probe.c |    9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/ide/ide-probe.c +5 -4
Index: b/drivers/ide/ide-probe.c
===================================================================
@@ -1391,9 +1391,11 @@ static void ide_free_port_slot(int idx)
 struct ide_host *ide_host_alloc(const struct ide_port_info *d, hw_regs_t **hws)
 {
 	struct ide_host *host;
+	struct device *dev = hws[0] ? hws[0]->dev : NULL;
+	int node = dev ? dev_to_node(dev) : -1;
 	int i;
 
-	host = kzalloc(sizeof(*host), GFP_KERNEL);
+	host = kzalloc_node(sizeof(*host), GFP_KERNEL, node);
 	if (host == NULL)
 		return NULL;
 
@@ -1404,7 +1406,7 @@ struct ide_host *ide_host_alloc(const st
 		if (hws[i] == NULL)
 			continue;
 
-		hwif = kzalloc(sizeof(*hwif), GFP_KERNEL);
+		hwif = kzalloc_node(sizeof(*hwif), GFP_KERNEL, node);
 		if (hwif == NULL)
 			continue;
 
@@ -1429,8 +1431,7 @@ struct ide_host *ide_host_alloc(const st
 		return NULL;
 	}
 
-	if (hws[0])
-		host->dev[0] = hws[0]->dev;
+	host->dev[0] = dev;
 
 	if (d) {
 		host->init_chipset = d->init_chipset;
#8 lore raw

PATCH 6/7 ide: dynamic allocation of device structures

In reply to Bartlomiej Zolnierkiewicz

From: Bartlomiej Zolnierkiewicz <[email protected]>
Subject: [PATCH] ide: dynamic allocation of device structures

Allocate device structures dynamically instead of having them embedded in ide_hwif_t:

* Remove needless zeroing of port structure from ide_init_port_data().

* Add ide_hwif_t.devices[MAX_DRIVES] (table of pointers to the devices).

* Add ide_port_{alloc,free}_devices() helpers and use them respectively in ide_{host,free}_alloc().

* Convert all users of ->drives[] to use ->devices[] instead.

While at it:

* Use drive->dn for the slave device check in scc_pata.c.

As a nice side-effect this patch cuts ~1kB (x86-32) from the resulting code size:

   text    data     bss     dec     hex filename
  53963    1244     237   55444    d894 drivers/ide/ide-core.o.before
  52981    1244     237   54462    d4be drivers/ide/ide-core.o.after
---
 drivers/ide/ide-acpi.c  |   10 +++---
 drivers/ide/ide-iops.c  |    4 +-
 drivers/ide/ide-probe.c |   71 ++++++++++++++++++++++++++++++++++--------------
 drivers/ide/ide-proc.c  |    2 -
 drivers/ide/ide.c       |    2 -
 drivers/ide/scc_pata.c  |    5 ++-
 include/linux/ide.h     |    4 +-
 7 files changed, 65 insertions(+), 33 deletions(-)
drivers/ide/ide-acpi.c +5 -5
Index: b/drivers/ide/ide-acpi.c
===================================================================
@@ -656,7 +656,7 @@ void ide_acpi_set_state(ide_hwif_t *hwif
 	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->drives[unit];
+		ide_drive_t *drive = hwif->devices[unit];
 
 		if (!drive->acpidata->obj_handle)
 			drive->acpidata->obj_handle = ide_acpi_drive_get_handle(drive);
@@ -711,14 +711,14 @@ void ide_acpi_port_init_devices(ide_hwif
 	 * for both drives, regardless whether they are connected
 	 * or not.
 	 */
-	hwif->drives[0].acpidata = &hwif->acpidata->master;
-	hwif->drives[1].acpidata = &hwif->acpidata->slave;
+	hwif->devices[0]->acpidata = &hwif->acpidata->master;
+	hwif->devices[1]->acpidata = &hwif->acpidata->slave;
 
 	/*
 	 * Send IDENTIFY for each drive
 	 */
 	for (i = 0; i < MAX_DRIVES; i++) {
-		drive = &hwif->drives[i];
+		drive = hwif->devices[i];
 
 		memset(drive->acpidata, 0, sizeof(*drive->acpidata));
 
@@ -745,7 +745,7 @@ void ide_acpi_port_init_devices(ide_hwif
 	ide_acpi_push_timing(hwif);
 
 	for (i = 0; i < MAX_DRIVES; i++) {
-		drive = &hwif->drives[i];
+		drive = hwif->devices[i];
 
 		if (drive->dev_flags & IDE_DFLAG_PRESENT)
 			/* Execute ACPI startup code */
drivers/ide/ide-iops.c +2 -2
Index: b/drivers/ide/ide-iops.c
===================================================================
@@ -1111,7 +1111,7 @@ static ide_startstop_t do_reset1 (ide_dr
 		prepare_to_wait(&ide_park_wq, &wait, TASK_UNINTERRUPTIBLE);
 		timeout = jiffies;
 		for (unit = 0; unit < MAX_DRIVES; unit++) {
-			ide_drive_t *tdrive = &hwif->drives[unit];
+			ide_drive_t *tdrive = hwif->devices[unit];
 
 			if (tdrive->dev_flags & IDE_DFLAG_PRESENT &&
 			    tdrive->dev_flags & IDE_DFLAG_PARKED &&
@@ -1134,7 +1134,7 @@ static ide_startstop_t do_reset1 (ide_dr
 	 * for any of the drives on this interface.
 	 */
 	for (unit = 0; unit < MAX_DRIVES; ++unit)
-		pre_reset(&hwif->drives[unit]);
+		pre_reset(hwif->devices[unit]);
 
 	if (io_ports->ctl_addr == 0) {
 		spin_unlock_irqrestore(&hwif->lock, flags);
drivers/ide/ide-probe.c +51 -20

191 lines not on this page · raw message

drivers/ide/ide-proc.c +1 -1
Index: b/drivers/ide/ide-proc.c
===================================================================
@@ -599,7 +599,7 @@ void ide_proc_port_register_devices(ide_
 	char name[64];
 
 	for (d = 0; d < MAX_DRIVES; d++) {
-		ide_drive_t *drive = &hwif->drives[d];
+		ide_drive_t *drive = hwif->devices[d];
 
 		if ((drive->dev_flags & IDE_DFLAG_PRESENT) == 0 || drive->proc)
 			continue;
drivers/ide/ide.c +1 -1
Index: b/drivers/ide/ide.c
===================================================================
@@ -497,7 +497,7 @@ void ide_port_apply_params(ide_hwif_t *h
 	}
 
 	for (i = 0; i < MAX_DRIVES; i++)
-		ide_dev_apply_params(&hwif->drives[i], i);
+		ide_dev_apply_params(hwif->devices[i], i);
 }
 
 /*
drivers/ide/scc_pata.c +3 -2
Index: b/drivers/ide/scc_pata.c
===================================================================
@@ -259,7 +259,7 @@ static void scc_set_dma_mode(ide_drive_t
 	unsigned long scrcst_port = ctl_base + 0x014;
 	unsigned long udenvt_port = ctl_base + 0x018;
 	unsigned long tdvhsel_port   = ctl_base + 0x020;
-	int is_slave = (&hwif->drives[1] == drive);
+	int is_slave = drive->dn & 1;
 	int offset, idx;
 	unsigned long reg;
 	unsigned long jcactsel;
@@ -413,7 +413,8 @@ static int scc_dma_end(ide_drive_t *driv
 				if (rq)
 					rq->errors |= ERROR_RESET;
 				for (unit = 0; unit < MAX_DRIVES; unit++) {
-					ide_drive_t *drive = &hwif->drives[unit];
+					ide_drive_t *drive = hwif->devices[unit];
+
 					drive->crc_count++;
 				}
 			}
include/linux/ide.h +2 -2
Index: b/include/linux/ide.h
===================================================================
@@ -753,7 +753,7 @@ typedef struct hwif_s {
 
 	unsigned long	sata_scr[SATA_NR_PORTS];
 
-	ide_drive_t	drives[MAX_DRIVES];	/* drive info */
+	ide_drive_t	*devices[MAX_DRIVES];
 
 	u8 major;	/* our major number */
 	u8 index;	/* 0 for ide0; 1 for ide1; ... */
@@ -1599,7 +1599,7 @@ static inline int hwif_to_node(ide_hwif_
 
 static inline ide_drive_t *ide_get_pair_dev(ide_drive_t *drive)
 {
-	ide_drive_t *peer = &drive->hwif->drives[(drive->dn ^ 1) & 1];
+	ide_drive_t *peer = drive->hwif->devices[(drive->dn ^ 1) & 1];
 
 	return (peer->dev_flags & IDE_DFLAG_PRESENT) ? peer : NULL;
 }
#9 lore raw

PATCH 7/7 ide: add port and host iterators

In reply to Bartlomiej Zolnierkiewicz

From: Bartlomiej Zolnierkiewicz <[email protected]>
Subject: [PATCH] ide: add port and host iterators

Add ide_port_for_each_dev() / ide_host_for_each_port() iterators and update IDE code to use them.

While at it:
- s/unit/i/ variable in ide_port_wait_ready(), ide_probe_port(), ide_port_tune_devices(), ide_port_init_devices_data(), do_reset1(), ide_acpi_set_state() and scc_dma_end()
- s/d/i/ variable in ide_proc_port_register_devices()

There should be no functional changes caused by this patch.

---
 drivers/ide/ide-acpi.c  |   14 ++-----
 drivers/ide/ide-iops.c  |   15 +++----
 drivers/ide/ide-probe.c |   96 +++++++++++++++++++-----------------------------
 drivers/ide/ide-proc.c  |    7 +--
 drivers/ide/ide.c       |    5 +-
 drivers/ide/scc_pata.c  |    8 ++--
 include/linux/ide.h     |   11 ++++-
 7 files changed, 69 insertions(+), 87 deletions(-)
drivers/ide/ide-acpi.c +5 -9
Index: b/drivers/ide/ide-acpi.c
===================================================================
@@ -641,7 +641,8 @@ void ide_acpi_push_timing(ide_hwif_t *hw
  */
 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;
@@ -655,9 +656,8 @@ void ide_acpi_set_state(ide_hwif_t *hwif
 	/* 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);
 
@@ -717,9 +717,7 @@ void ide_acpi_port_init_devices(ide_hwif
 	/*
 	 * 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)
@@ -744,9 +742,7 @@ void ide_acpi_port_init_devices(ide_hwif
 	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);
drivers/ide/ide-iops.c +7 -8
Index: b/drivers/ide/ide-iops.c
===================================================================
@@ -1081,8 +1081,9 @@ static ide_startstop_t do_reset1 (ide_dr
 	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);
@@ -1110,11 +1111,9 @@ static ide_startstop_t do_reset1 (ide_dr
 
 		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];
-
-			if (tdrive->dev_flags & IDE_DFLAG_PRESENT &&
-			    tdrive->dev_flags & IDE_DFLAG_PARKED &&
+		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))
 				timeout = tdrive->sleep;
 		}
@@ -1133,8 +1132,8 @@ static ide_startstop_t do_reset1 (ide_dr
 	 * 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);
drivers/ide/ide-probe.c +38 -58

243 lines not on this page · raw message

drivers/ide/ide-proc.c +3 -4
Index: b/drivers/ide/ide-proc.c
===================================================================
@@ -593,14 +593,13 @@ EXPORT_SYMBOL(ide_proc_unregister_driver
 
 void ide_proc_port_register_devices(ide_hwif_t *hwif)
 {
-	int	d;
 	struct proc_dir_entry *ent;
 	struct proc_dir_entry *parent = hwif->proc;
+	ide_drive_t *drive;
 	char name[64];
+	int i;
 
-	for (d = 0; d < MAX_DRIVES; d++) {
-		ide_drive_t *drive = hwif->devices[d];
-
+	ide_port_for_each_dev(i, drive, hwif) {
 		if ((drive->dev_flags & IDE_DFLAG_PRESENT) == 0 || drive->proc)
 			continue;
 
drivers/ide/ide.c +3 -2
Index: b/drivers/ide/ide.c
===================================================================
@@ -488,6 +488,7 @@ MODULE_PARM_DESC(ignore_cable, "ignore c
 
 void ide_port_apply_params(ide_hwif_t *hwif)
 {
+	ide_drive_t *drive;
 	int i;
 
 	if (ide_ignore_cable & (1 << hwif->index)) {
@@ -496,8 +497,8 @@ void ide_port_apply_params(ide_hwif_t *h
 		hwif->cbl = ATA_CBL_PATA40_SHORT;
 	}
 
-	for (i = 0; i < MAX_DRIVES; i++)
-		ide_dev_apply_params(hwif->devices[i], i);
+	ide_port_for_each_dev(i, drive, hwif)
+		ide_dev_apply_params(drive, i);
 }
 
 /*
drivers/ide/scc_pata.c +4 -4
Index: b/drivers/ide/scc_pata.c
===================================================================
@@ -406,17 +406,17 @@ static int scc_dma_end(ide_drive_t *driv
 			data_loss = 1;
 			if (retry++) {
 				struct request *rq = hwif->rq;
-				int unit;
+				ide_drive_t *drive;
+				int i;
+
 				/* ERROR_RESET and drive->crc_count are needed
 				 * to reduce DMA transfer mode in retry process.
 				 */
 				if (rq)
 					rq->errors |= ERROR_RESET;
-				for (unit = 0; unit < MAX_DRIVES; unit++) {
-					ide_drive_t *drive = hwif->devices[unit];
 
+				ide_port_for_each_dev(i, drive, hwif)
 					drive->crc_count++;
-				}
 			}
 		}
 	}
include/linux/ide.h +9 -2
Index: b/include/linux/ide.h
===================================================================
@@ -753,7 +753,7 @@ typedef struct hwif_s {
 
 	unsigned long	sata_scr[SATA_NR_PORTS];
 
-	ide_drive_t	*devices[MAX_DRIVES];
+	ide_drive_t	*devices[MAX_DRIVES + 1];
 
 	u8 major;	/* our major number */
 	u8 index;	/* 0 for ide0; 1 for ide1; ... */
@@ -860,7 +860,7 @@ typedef struct hwif_s {
 #define MAX_HOST_PORTS 4
 
 struct ide_host {
-	ide_hwif_t	*ports[MAX_HOST_PORTS];
+	ide_hwif_t	*ports[MAX_HOST_PORTS + 1];
 	unsigned int	n_ports;
 	struct device	*dev[2];
 	unsigned int	(*init_chipset)(struct pci_dev *);
@@ -1603,4 +1603,11 @@ static inline ide_drive_t *ide_get_pair_
 
 	return (peer->dev_flags & IDE_DFLAG_PRESENT) ? peer : NULL;
 }
+
+#define ide_port_for_each_dev(i, dev, port) \
+	for ((i) = 0; ((dev) = (port)->devices[i]) || (i) < MAX_DRIVES; (i)++)
+
+#define ide_host_for_each_port(i, port, host) \
+	for ((i) = 0; ((port) = (host)->ports[i]) || (i) < MAX_HOST_PORTS; (i)++)
+
 #endif /* _IDE_H */