Commit b5542796b3 for qemu.org

commit b5542796b3cbe052daa3660d00ec1d4a1033e7ff
Author: Denis V. Lunev <den@openvz.org>
Date:   Fri Aug 14 17:43:26 2026 +0200

    hw/ide: keep the IDENTIFY DEVICE current geometry in sync

    Bit 0 of IDENTIFY DEVICE word 53 says that words 54 to 58 describe the CHS
    translation in effect, and ATA-5 8.16.8 has INITIALIZE DEVICE PARAMETERS
    set words 55 and 56 to the heads and sectors per track it was given. The
    data is built once and then cached, so those words kept describing
    whatever was in effect when a guest first asked for IDENTIFY DEVICE: the
    device reported one geometry while addressing the medium with another, and
    nothing reported an error. The revert SET FEATURES 0xCC asks for on the
    next reset left the same disagreement.

    Do not drop the cached data on a change, as parts of it are guest state
    rather than a description of the drive: SET FEATURES records the write
    cache setting in word 85, which ide_drive_post_load() reads back after
    migration. Refresh the affected words in place instead, the way
    ide_identify_size() does for the capacity words.

    An ATAPI device has no translation but does take SET FEATURES 0xCC, so
    leave its IDENTIFY PACKET DEVICE data alone, where those words differ.

    Cc: John Snow <jsnow@redhat.com>
    Cc: Peter Maydell <peter.maydell@linaro.org>
    Cc: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com>
    Fixes: 176e4961bb33 ("hw/ide/core.c: Implement ATA INITIALIZE_DEVICE_PARAMETERS command")
    Signed-off-by: Denis V. Lunev <den@openvz.org>

diff --git a/hw/ide/core.c b/hw/ide/core.c
index e5fd570575..048655b2d0 100644
--- a/hw/ide/core.c
+++ b/hw/ide/core.c
@@ -1360,6 +1360,10 @@ static void ide_reset(IDEState *s)
         s->reset_reverts = false;
         s->heads         = s->drive_heads;
         s->sectors       = s->drive_sectors;
+        /* An ATAPI device takes SET FEATURES 0xCC but has no translation */
+        if (s->identify_set && s->drive_kind != IDE_CD) {
+            ide_identify_chs(s);
+        }
     }
     if (s->drive_kind == IDE_CFATA)
         s->mult_sectors = 0;
@@ -1668,6 +1672,9 @@ static bool cmd_specify(IDEState *s, uint8_t cmd)

     s->heads = (s->select & (ATA_DEV_HS)) + 1;
     s->sectors = s->nsector;
+    if (s->identify_set) {
+        ide_identify_chs(s);
+    }
     ide_bus_set_irq(s->bus);

     return true;