From nobody Mon Feb 9 14:38:08 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=1641823553; cv=none; d=zohomail.com; s=zohoarc; b=PhPpGmecHtCADbfQkWHo1NM3HNok8pkc9NSghGOmahswQBYnadO6yDFSIlMJS38Vi1bB9uDoCJx68DrNCWOWDgjRIQimOAQUfuhp8Je4nG1oxuiUBMRLIWuYUICnt/FTGILFM8t1iKZ0FeNOnHj7y5E1bRxxjqwe93TGkqpUUf4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1641823553; h=Content-Type:Content-Transfer-Encoding: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=kE9QTO0QrbN3FbKC9QgtZf3QAJc0flztkO6IMgwqh2o=; b=OMI4ablCfU05t06EmJQxItDghyyHN0Vo+UTsZX4T0AxuDtvVHE5A6bjuFXrom0+QfUVKg1eskXa4fl81h+6hd6icsVDqnnaR+GLOWqKo1GmfOv8nWQJfwZVIX7UG+Ldn08HgzLsaEEISFpeeOwR1ftXRUDEqQoNr57dEkLP0H14= 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) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1641823553737833.7796918360337; Mon, 10 Jan 2022 06:05:53 -0800 (PST) Received: from localhost ([::1]:47842 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1n6vJ0-0006gN-UU for importer@patchew.org; Mon, 10 Jan 2022 09:05:52 -0500 Received: from eggs.gnu.org ([209.51.188.92]:39076) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1n6v0m-0002EC-VG for qemu-devel@nongnu.org; Mon, 10 Jan 2022 08:47:01 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:25232) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1n6v0k-000722-RC for qemu-devel@nongnu.org; Mon, 10 Jan 2022 08:47:00 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-572-_ZsksWMTPNem5ip-dZ2NJw-1; Mon, 10 Jan 2022 08:46:55 -0500 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 524681006AA4; Mon, 10 Jan 2022 13:46:54 +0000 (UTC) Received: from kostyanf14nb.Dlink (unknown [10.40.193.252]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 6C49D7BB53; Mon, 10 Jan 2022 13:46:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1641822418; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=kE9QTO0QrbN3FbKC9QgtZf3QAJc0flztkO6IMgwqh2o=; b=hAOa/S746lSkI4F/Nge/k46SK2Trf/dy7pLTjvad6bcnMjyVOaJZM2WR+DSRe438seyQ0U QOGujRU4Xaw139+rDtuTq3SLchrjCtK16JgXSYVInEzHLbGRjQsNV/n3msXtjs54j+66Ze uOPSBnI8JebdhF3EX+E0+p2tl7/69mI= X-MC-Unique: _ZsksWMTPNem5ip-dZ2NJw-1 From: Konstantin Kostiuk To: qemu-devel@nongnu.org, Peter Maydell Subject: [PULL 5/9] gqa-win: get_pci_info: Split logic to separate functions Date: Mon, 10 Jan 2022 15:46:40 +0200 Message-Id: <20220110134644.107375-6-kkostiuk@redhat.com> In-Reply-To: <20220110134644.107375-1-kkostiuk@redhat.com> References: <20220110134644.107375-1-kkostiuk@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=kkostiuk@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" 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=170.10.133.124; envelope-from=kkostiuk@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -33 X-Spam_score: -3.4 X-Spam_bar: --- X-Spam_report: (-3.4 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.597, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1641823556334100001 From: Kostiantyn Kostiuk Signed-off-by: Kostiantyn Kostiuk Reviewed-by: Marc-Andr=C3=A9 Lureau Signed-off-by: Kostiantyn Kostiuk --- qga/commands-win32.c | 161 +++++++++++++++++++++++-------------------- 1 file changed, 87 insertions(+), 74 deletions(-) diff --git a/qga/commands-win32.c b/qga/commands-win32.c index f6de9e2676..8588fa8633 100644 --- a/qga/commands-win32.c +++ b/qga/commands-win32.c @@ -512,6 +512,92 @@ DEFINE_GUID(GUID_DEVINTERFACE_STORAGEPORT, 0x2accfe60L, 0xc130, 0x11d2, 0xb0, 0x82, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b); =20 +static void get_pci_address_for_device(GuestPCIAddress *pci, + HDEVINFO dev_info) +{ + SP_DEVINFO_DATA dev_info_data; + DWORD j; + DWORD size; + bool partial_pci =3D false; + + dev_info_data.cbSize =3D sizeof(SP_DEVINFO_DATA); + + for (j =3D 0; + SetupDiEnumDeviceInfo(dev_info, j, &dev_info_data); + j++) { + DWORD addr, bus, ui_slot, type; + int func, slot; + size =3D sizeof(DWORD); + + /* + * There is no need to allocate buffer in the next functions. The + * size is known and ULONG according to + * https://msdn.microsoft.com/en-us/library/windows/hardware/ff5430= 95(v=3Dvs.85).aspx + */ + if (!SetupDiGetDeviceRegistryProperty( + dev_info, &dev_info_data, SPDRP_BUSNUMBER, + &type, (PBYTE)&bus, size, NULL)) { + debug_error("failed to get PCI bus"); + bus =3D -1; + partial_pci =3D true; + } + + /* + * The function retrieves the device's address. This value will be + * transformed into device function and number + */ + if (!SetupDiGetDeviceRegistryProperty( + dev_info, &dev_info_data, SPDRP_ADDRESS, + &type, (PBYTE)&addr, size, NULL)) { + debug_error("failed to get PCI address"); + addr =3D -1; + partial_pci =3D true; + } + + /* + * This call returns UINumber of DEVICE_CAPABILITIES structure. + * This number is typically a user-perceived slot number. + */ + if (!SetupDiGetDeviceRegistryProperty( + dev_info, &dev_info_data, SPDRP_UI_NUMBER, + &type, (PBYTE)&ui_slot, size, NULL)) { + debug_error("failed to get PCI slot"); + ui_slot =3D -1; + partial_pci =3D true; + } + + /* + * SetupApi gives us the same information as driver with + * IoGetDeviceProperty. According to Microsoft: + * + * FunctionNumber =3D (USHORT)((propertyAddress) & 0x0000FFFF) + * DeviceNumber =3D (USHORT)(((propertyAddress) >> 16) & 0x0000FF= FF) + * SPDRP_ADDRESS is propertyAddress, so we do the same. + * + * https://docs.microsoft.com/en-us/windows/desktop/api/setupapi/nf= -setupapi-setupdigetdeviceregistrypropertya + */ + if (partial_pci) { + pci->domain =3D -1; + pci->slot =3D -1; + pci->function =3D -1; + pci->bus =3D -1; + continue; + } else { + func =3D ((int)addr =3D=3D -1) ? -1 : addr & 0x0000FFFF; + slot =3D ((int)addr =3D=3D -1) ? -1 : (addr >> 16) & 0x0000FFF= F; + if ((int)ui_slot !=3D slot) { + g_debug("mismatch with reported slot values: %d vs %d", + (int)ui_slot, slot); + } + pci->domain =3D 0; + pci->slot =3D (int)ui_slot; + pci->function =3D func; + pci->bus =3D (int)bus; + return; + } + } +} + static GuestPCIAddress *get_pci_info(int number, Error **errp) { HDEVINFO dev_info =3D INVALID_HANDLE_VALUE; @@ -522,7 +608,6 @@ static GuestPCIAddress *get_pci_info(int number, Error = **errp) HANDLE dev_file; int i; GuestPCIAddress *pci =3D NULL; - bool partial_pci =3D false; =20 pci =3D g_malloc0(sizeof(*pci)); pci->domain =3D -1; @@ -545,7 +630,6 @@ static GuestPCIAddress *get_pci_info(int number, Error = **errp) STORAGE_DEVICE_NUMBER sdn; char *parent_dev_id =3D NULL; SP_DEVINFO_DATA parent_dev_info_data; - DWORD j; DWORD size =3D 0; =20 g_debug("getting device path"); @@ -672,79 +756,8 @@ static GuestPCIAddress *get_pci_info(int number, Error= **errp) goto end; } =20 - for (j =3D 0; - SetupDiEnumDeviceInfo(parent_dev_info, j, &parent_dev_info_da= ta); - j++) { - DWORD addr, bus, ui_slot, type; - int func, slot; + get_pci_address_for_device(pci, parent_dev_info); =20 - /* - * There is no need to allocate buffer in the next functions. = The - * size is known and ULONG according to - * https://msdn.microsoft.com/en-us/library/windows/hardware/f= f543095(v=3Dvs.85).aspx - */ - if (!SetupDiGetDeviceRegistryProperty( - parent_dev_info, &parent_dev_info_data, SPDRP_BUSNUMBER, - &type, (PBYTE)&bus, size, NULL)) { - debug_error("failed to get PCI bus"); - bus =3D -1; - partial_pci =3D true; - } - - /* - * The function retrieves the device's address. This value wil= l be - * transformed into device function and number - */ - if (!SetupDiGetDeviceRegistryProperty( - parent_dev_info, &parent_dev_info_data, SPDRP_ADDRESS, - &type, (PBYTE)&addr, size, NULL)) { - debug_error("failed to get PCI address"); - addr =3D -1; - partial_pci =3D true; - } - - /* - * This call returns UINumber of DEVICE_CAPABILITIES structure. - * This number is typically a user-perceived slot number. - */ - if (!SetupDiGetDeviceRegistryProperty( - parent_dev_info, &parent_dev_info_data, SPDRP_UI_NUMBE= R, - &type, (PBYTE)&ui_slot, size, NULL)) { - debug_error("failed to get PCI slot"); - ui_slot =3D -1; - partial_pci =3D true; - } - - /* - * SetupApi gives us the same information as driver with - * IoGetDeviceProperty. According to Microsoft: - * - * FunctionNumber =3D (USHORT)((propertyAddress) & 0x0000FFF= F) - * DeviceNumber =3D (USHORT)(((propertyAddress) >> 16) & 0x0= 000FFFF) - * SPDRP_ADDRESS is propertyAddress, so we do the same. - * - * https://docs.microsoft.com/en-us/windows/desktop/api/setupa= pi/nf-setupapi-setupdigetdeviceregistrypropertya - */ - if (partial_pci) { - pci->domain =3D -1; - pci->slot =3D -1; - pci->function =3D -1; - pci->bus =3D -1; - continue; - } else { - func =3D ((int)addr =3D=3D -1) ? -1 : addr & 0x0000FFFF; - slot =3D ((int)addr =3D=3D -1) ? -1 : (addr >> 16) & 0x000= 0FFFF; - if ((int)ui_slot !=3D slot) { - g_debug("mismatch with reported slot values: %d vs %d", - (int)ui_slot, slot); - } - pci->domain =3D 0; - pci->slot =3D (int)ui_slot; - pci->function =3D func; - pci->bus =3D (int)bus; - break; - } - } break; } =20 --=20 2.25.1