From nobody Wed May 8 11:07:20 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1568049703; cv=none; d=zoho.com; s=zohoarc; b=iQujvRHwplGVxZCoW6e5ynKljyrKGWZyVIKQkgHX+yvVYBHQPpeSGtmQuefcrDTiuEO6645TzK+7EVNx197AF01/JGV6joMDWB8utPS/7ojqWjpLV31XwhCnAfw/wpZ6LDbD2aboouKSqdOgqbS6DXP+t3wKaZ+RuJJcF2CdhWM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1568049703; h=Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:Reply-To:Sender:Subject:To:ARC-Authentication-Results; bh=4Qqz4xjQvmoD7XrHQwLBwGSHHOfhN2FB7JfY2vRU24M=; b=eAe3m2lW0NkdsiJ4cdxyWNITkChRfbiG3hwTemO2UBSY+YYl5ithuqd3bhyabSvmc5kk3qOnJi90tJVpJxKrHvnJVpDhlXeD46Ftxs17iaIfyAcFMcKKurhdEI2QUjwHIWLDHXzhYhKUFO1Mso/jF0LBxfJSeZMb56f7NEA0QJM= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1568049702859869.9988253479385; Mon, 9 Sep 2019 10:21:42 -0700 (PDT) Received: from localhost ([::1]:59386 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i7NMD-0002e3-Ok for importer@patchew.org; Mon, 09 Sep 2019 13:21:41 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:47061) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i7NJe-0000eK-3z for qemu-devel@nongnu.org; Mon, 09 Sep 2019 13:19:03 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1i7NJc-0002Cv-VH for qemu-devel@nongnu.org; Mon, 09 Sep 2019 13:19:02 -0400 Received: from mail.synology.com ([211.23.38.101]:49488 helo=synology.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1i7NJY-0001zr-Gq; Mon, 09 Sep 2019 13:18:57 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=synology.com; s=123; t=1568049525; bh=de2UEnntffiGmiMGCYzD4cvGPp0KLbbKHu1ehSBcQkI=; h=From:To:Cc:Subject:Date; b=p3X7HSNS4P3S6+WI80pgScY7xLzz4uffVBrnXN7bwKvG7MtrqDaXudJqj9saz9niG b84laCgR6c3Dwm27dC+Q4IJyk0elVYk+p4k9gEN2DAOuhJi4nt2SQPE7UeSBmxbvKC dspNXJLazs3ArQg8vTeaGvIyIhntaRAWn+ecIVrM= To: qemu-devel@nongnu.org Date: Tue, 10 Sep 2019 01:18:37 +0800 Message-Id: <1568049517-10261-1-git-send-email-andychiu@synology.com> X-Synology-MCP-Status: no X-Synology-Spam-Flag: no X-Synology-Spam-Status: score=0, required 6, WHITELIST_FROM_ADDRESS 0 X-Synology-Virus-Status: no X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 211.23.38.101 Subject: [Qemu-devel] [PATCH] ahci: enable pci bus master MemoryRegion before loading ahci engines 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: , From: andychiu via Qemu-devel Reply-To: andychiu Cc: andychiu , jsnow@redhat.com, qemu-block@nongnu.org, mst@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" If Windows 10 guests have enabled 'turn off hard disk after idle' option in power settings, and the guest has a SATA disk plugged in, the SATA disk will be turned off after a specified idle time. If the guest is live migrated or saved/loaded with its SATA disk turned off, the following error will occur: qemu-system-x86_64: AHCI: Failed to start FIS receive engine: bad FIS recei= ve buffer address qemu-system-x86_64: Failed to load ich9_ahci:ahci qemu-system-x86_64: error while loading state for instance 0x0 of device '0= 000:00:1a.0/ich9_ahci' qemu-system-x86_64: load of migration failed: Operation not permitted Observation from trace logs shows that a while after Windows 10 turns off a SATA disk (IDE disks don't have the following behavior), it will disable the PCI_COMMAND_MASTER flag of the pci device containing the ahci device. When the the disk is turning back on, the PCI_COMMAND_MASTER flag will be restored first. But if the guest is migrated or saved/loaded while the disk is off, the post_load callback of ahci device, ahci_state_post_load(), will fail at ahci_cond_start_engines() if the MemoryRegion pci_dev->bus_master_enable_region is not enabled, with pci_dev pointing to the PCIDevice struct containing the ahci device. This patch enables pci_dev->bus_master_enable_region before calling ahci_cond_start_engines() in ahci_state_post_load(), and restore the MemoryRegion to its original state afterwards. Signed-off-by: andychiu --- hw/ide/ahci.c | 53 ++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 36 insertions(+), 17 deletions(-) diff --git a/hw/ide/ahci.c b/hw/ide/ahci.c index d45393c..83f8c30 100644 --- a/hw/ide/ahci.c +++ b/hw/ide/ahci.c @@ -1649,33 +1649,52 @@ static const VMStateDescription vmstate_ahci_device= =3D { }, }; =20 +static int ahci_state_load_engines(AHCIState *s, AHCIDevice *ad) +{ + AHCIPortRegs *pr =3D &ad->port_regs; + DeviceState *dev_state =3D s->container; + PCIDevice *pci_dev =3D (PCIDevice *) object_dynamic_cast(OBJECT(dev_st= ate), + TYPE_PCI_DEVICE= ); + bool pci_bus_master_enabled =3D pci_dev->bus_master_enable_region.enab= led; + + if (!(pr->cmd & PORT_CMD_START) && (pr->cmd & PORT_CMD_LIST_ON)) { + error_report("AHCI: DMA engine should be off, but status bit " + "indicates it is still running."); + return -1; + } + if (!(pr->cmd & PORT_CMD_FIS_RX) && (pr->cmd & PORT_CMD_FIS_ON)) { + error_report("AHCI: FIS RX engine should be off, but status bit " + "indicates it is still running."); + return -1; + } + + memory_region_set_enabled(&pci_dev->bus_master_enable_region, true); + + /* + * After a migrate, the DMA/FIS engines are "off" and + * need to be conditionally restarted + */ + pr->cmd &=3D ~(PORT_CMD_LIST_ON | PORT_CMD_FIS_ON); + if (ahci_cond_start_engines(ad) !=3D 0) { + return -1; + } + memory_region_set_enabled(&pci_dev->bus_master_enable_region, + pci_bus_master_enabled); + + return 0; +} + static int ahci_state_post_load(void *opaque, int version_id) { int i, j; struct AHCIDevice *ad; NCQTransferState *ncq_tfs; - AHCIPortRegs *pr; AHCIState *s =3D opaque; =20 for (i =3D 0; i < s->ports; i++) { ad =3D &s->dev[i]; - pr =3D &ad->port_regs; - - if (!(pr->cmd & PORT_CMD_START) && (pr->cmd & PORT_CMD_LIST_ON)) { - error_report("AHCI: DMA engine should be off, but status bit " - "indicates it is still running."); - return -1; - } - if (!(pr->cmd & PORT_CMD_FIS_RX) && (pr->cmd & PORT_CMD_FIS_ON)) { - error_report("AHCI: FIS RX engine should be off, but status bi= t " - "indicates it is still running."); - return -1; - } =20 - /* After a migrate, the DMA/FIS engines are "off" and - * need to be conditionally restarted */ - pr->cmd &=3D ~(PORT_CMD_LIST_ON | PORT_CMD_FIS_ON); - if (ahci_cond_start_engines(ad) !=3D 0) { + if (ahci_state_load_engines(s, ad)) { return -1; } =20 --=20 2.7.4