From nobody Mon Feb 9 09:16:34 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1601577043; cv=none; d=zohomail.com; s=zohoarc; b=Tb9FjocGlZMEmiKn8o1KhLeHDOv4vs6ZIp8VD7qCEitdwsGF0eParvFL2TY28/D4qkn86+ZBiVu+Kj9oeMUwU0/4v4kDZFJ6LGZG8cT3AIfXr4G3I7+2rTRIHhQxUqFujMpfKrEJJm66Iq0XAYHjKR6kdavLdtc70k+GfAInlcE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1601577043; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=xb25lG+d7LBHIhF0zG8Hh1bU27aibEmQRHXuEo87TmM=; b=F/DJJoSJoMIvPmoIYoQDfDQer3iPdTb0jRVhKDsTAENbu0SiMkq848RFqjpRQ2Kg3GzDo4OoKBRetf+CsftCosmGMWRH8Ro4iD84g6bK0Y8kW7LkSWCpZQK4eqMguvWHTf68iKdhy/Ily3OxnhDzqotQxw08irKpf2HGKKNBNw8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1601577043487967.2923331753834; Thu, 1 Oct 2020 11:30:43 -0700 (PDT) Received: from localhost ([::1]:43158 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kO3Ll-0006kW-IM for importer@patchew.org; Thu, 01 Oct 2020 14:30:42 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:36804) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kO2fb-0003ag-VB for qemu-devel@nongnu.org; Thu, 01 Oct 2020 13:47:09 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:54483) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1kO2fZ-0006pz-3u for qemu-devel@nongnu.org; Thu, 01 Oct 2020 13:47:07 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-537-U965u9ivPo6AL3GFiINPGw-1; Thu, 01 Oct 2020 13:47:02 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 48CCC801AF6; Thu, 1 Oct 2020 17:47:01 +0000 (UTC) Received: from scv.redhat.com (ovpn-120-38.rdu2.redhat.com [10.10.120.38]) by smtp.corp.redhat.com (Postfix) with ESMTP id C68235C1CF; Thu, 1 Oct 2020 17:47:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1601574424; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=xb25lG+d7LBHIhF0zG8Hh1bU27aibEmQRHXuEo87TmM=; b=MDbs/FwIewlFaKizkfKT+qBA78nxIVQKDdss3li2TMfeGGSNfOxyB/ozUfbpaRHGmz2dsJ 4saR8u7LpkLQFMjCj9ZYxASWxqqxw9hZvtRtjZoqtpKbP7o0VY16EOqZ7QnfW6kSb2m+kh 3xlcZQxFDZR5JKBix0fYHiU1rxWvlHo= X-MC-Unique: U965u9ivPo6AL3GFiINPGw-1 From: John Snow To: Peter Maydell , qemu-devel@nongnu.org Subject: [PULL 7/9] ide: remove magic constants from the device register Date: Thu, 1 Oct 2020 13:46:47 -0400 Message-Id: <20201001174649.1911016-8-jsnow@redhat.com> In-Reply-To: <20201001174649.1911016-1-jsnow@redhat.com> References: <20201001174649.1911016-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=jsnow@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=216.205.24.124; envelope-from=jsnow@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/10/01 04:25:37 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: John Snow , qemu-block@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" (In QEMU, we call this the "select" register.) My memory isn't good enough to memorize what these magic runes do. Label them to prevent mixups from happening in the future. Side note: I assume it's safe to always set 0xA0 even though ATA2 claims these bits are reserved, because ATA3 immediately reinstated that these bits should be always on. ATA4 and subsequent specs only claim that the fields are obsolete, so I assume it's safe to leave these set and that it should work with the widest array of guests. Signed-off-by: John Snow --- include/hw/ide/internal.h | 11 +++++++++++ hw/ide/core.c | 26 ++++++++++++++------------ 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/include/hw/ide/internal.h b/include/hw/ide/internal.h index 5b7b0e47e60..2d09162eeb7 100644 --- a/include/hw/ide/internal.h +++ b/include/hw/ide/internal.h @@ -29,6 +29,17 @@ OBJECT_DECLARE_SIMPLE_TYPE(IDEBus, IDE_BUS) =20 #define MAX_IDE_DEVS 2 =20 +/* Device/Head ("select") Register */ +#define ATA_DEV_SELECT 0x10 +/* ATA1,3: Defined as '1'. + * ATA2: Reserved. + * ATA3-7: obsolete. */ +#define ATA_DEV_ALWAYS_ON 0xA0 +#define ATA_DEV_LBA 0x40 +#define ATA_DEV_LBA_MSB 0x0F /* LBA 24:27 */ +#define ATA_DEV_HS 0x0F /* HS 3:0 */ + + /* Bits of HD_STATUS */ #define ERR_STAT 0x01 #define INDEX_STAT 0x02 diff --git a/hw/ide/core.c b/hw/ide/core.c index afff355e5c6..8a55352e4b2 100644 --- a/hw/ide/core.c +++ b/hw/ide/core.c @@ -367,7 +367,7 @@ fill_buffer: =20 static void ide_set_signature(IDEState *s) { - s->select &=3D 0xf0; /* clear head */ + s->select &=3D ~(ATA_DEV_HS); /* clear head */ /* put signature */ s->nsector =3D 1; s->sector =3D 1; @@ -586,7 +586,7 @@ void ide_transfer_stop(IDEState *s) int64_t ide_get_sector(IDEState *s) { int64_t sector_num; - if (s->select & 0x40) { + if (s->select & (ATA_DEV_LBA)) { if (s->lba48) { sector_num =3D ((int64_t)s->hob_hcyl << 40) | ((int64_t) s->hob_lcyl << 32) | @@ -595,13 +595,13 @@ int64_t ide_get_sector(IDEState *s) ((int64_t) s->lcyl << 8) | s->sector; } else { /* LBA28 */ - sector_num =3D ((s->select & 0x0f) << 24) | (s->hcyl << 16) | - (s->lcyl << 8) | s->sector; + sector_num =3D ((s->select & (ATA_DEV_LBA_MSB)) << 24) | + (s->hcyl << 16) | (s->lcyl << 8) | s->sector; } } else { /* CHS */ sector_num =3D ((s->hcyl << 8) | s->lcyl) * s->heads * s->sectors + - (s->select & 0x0f) * s->sectors + (s->sector - 1); + (s->select & (ATA_DEV_HS)) * s->sectors + (s->sector - 1); } =20 return sector_num; @@ -610,7 +610,7 @@ int64_t ide_get_sector(IDEState *s) void ide_set_sector(IDEState *s, int64_t sector_num) { unsigned int cyl, r; - if (s->select & 0x40) { + if (s->select & (ATA_DEV_LBA)) { if (s->lba48) { s->sector =3D sector_num; s->lcyl =3D sector_num >> 8; @@ -620,7 +620,8 @@ void ide_set_sector(IDEState *s, int64_t sector_num) s->hob_hcyl =3D sector_num >> 40; } else { /* LBA28 */ - s->select =3D (s->select & 0xf0) | (sector_num >> 24); + s->select =3D (s->select & ~(ATA_DEV_LBA_MSB)) | + ((sector_num >> 24) & (ATA_DEV_LBA_MSB)); s->hcyl =3D (sector_num >> 16); s->lcyl =3D (sector_num >> 8); s->sector =3D (sector_num); @@ -631,7 +632,8 @@ void ide_set_sector(IDEState *s, int64_t sector_num) r =3D sector_num % (s->heads * s->sectors); s->hcyl =3D cyl >> 8; s->lcyl =3D cyl; - s->select =3D (s->select & 0xf0) | ((r / s->sectors) & 0x0f); + s->select =3D (s->select & ~(ATA_DEV_HS)) | + ((r / s->sectors) & (ATA_DEV_HS)); s->sector =3D (r % s->sectors) + 1; } } @@ -1302,10 +1304,10 @@ void ide_ioport_write(void *opaque, uint32_t addr, = uint32_t val) break; case ATA_IOPORT_WR_DEVICE_HEAD: ide_clear_hob(bus); - bus->ifs[0].select =3D val | 0xa0; - bus->ifs[1].select =3D val | 0xa0; + bus->ifs[0].select =3D val | (ATA_DEV_ALWAYS_ON); + bus->ifs[1].select =3D val | (ATA_DEV_ALWAYS_ON); /* select drive */ - bus->unit =3D (val >> 4) & 1; + bus->unit =3D (val & (ATA_DEV_SELECT)) ? 1 : 0; break; default: case ATA_IOPORT_WR_COMMAND: @@ -1343,7 +1345,7 @@ static void ide_reset(IDEState *s) s->hob_lcyl =3D 0; s->hob_hcyl =3D 0; =20 - s->select =3D 0xa0; + s->select =3D (ATA_DEV_ALWAYS_ON); s->status =3D READY_STAT | SEEK_STAT; =20 s->lba48 =3D 0; --=20 2.26.2