From nobody Wed Feb 11 12:08:31 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1770720880; cv=none; d=zohomail.com; s=zohoarc; b=NFDLqHuMmhuo2UYqN4pJzfbVm5+kTB2lwASgaiZ+1uwbiUFHdcVSpI9SPFYbCNaPf4ycuj6bKpSXgW+owufGEc1401BkP6hhf02mLt2F0a502pnRbAS5m2oScqyMNoKVS2m3VRZeCFfGTyxFCL2YlK/lhwjG/Xo0LUuuljvE8vo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1770720880; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=j/dEX2kKzzmAnt1Zegeuwsix6l/RE3FSLEbF5w2fmL0=; b=j7Qt8lssh+BHiQPO4di8aNvBjhWqeiNiRl7fjP1qf3D+gdaJSrOZMpT+VkUm/yJQVO5qZ9LAUBx5MHxyUt1bpvn7Jihy/tMYKXzNe2CJACUcqAq1WXkIpOLTDuiIXDvXRzn+MmUXzvRzg15UF/9gWtGXCsasOKZsatEKDnakZCw= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1770720880498515.5889568367124; Tue, 10 Feb 2026 02:54:40 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1226249.1532786 (Exim 4.92) (envelope-from ) id 1vplNv-0005mc-TV; Tue, 10 Feb 2026 10:54:23 +0000 Received: by outflank-mailman (output) from mailman id 1226249.1532786; Tue, 10 Feb 2026 10:54:23 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1vplNv-0005mV-QV; Tue, 10 Feb 2026 10:54:23 +0000 Received: by outflank-mailman (input) for mailman id 1226249; Tue, 10 Feb 2026 10:54:21 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1vplNt-0004jf-Kd for xen-devel@lists.xenproject.org; Tue, 10 Feb 2026 10:54:21 +0000 Received: from mail-wm1-x336.google.com (mail-wm1-x336.google.com [2a00:1450:4864:20::336]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id da7965a1-066e-11f1-b162-2bf370ae4941; Tue, 10 Feb 2026 11:54:20 +0100 (CET) Received: by mail-wm1-x336.google.com with SMTP id 5b1f17b1804b1-4801eb2c0a5so51890865e9.3 for ; Tue, 10 Feb 2026 02:54:20 -0800 (PST) Received: from [10.156.60.236] (ip-037-024-206-209.um08.pools.vodafone-ip.de. [37.24.206.209]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4832096c438sm197308905e9.3.2026.02.10.02.54.19 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 10 Feb 2026 02:54:19 -0800 (PST) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: da7965a1-066e-11f1-b162-2bf370ae4941 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=google; t=1770720860; x=1771325660; darn=lists.xenproject.org; h=content-transfer-encoding:in-reply-to:autocrypt:content-language :references:cc:to:from:subject:user-agent:mime-version:date :message-id:from:to:cc:subject:date:message-id:reply-to; bh=j/dEX2kKzzmAnt1Zegeuwsix6l/RE3FSLEbF5w2fmL0=; b=GH1gfZAP6BIdiBmwRzePRkLmuMuCXtsTmnsZsT1pxgxKBrhqoJ1QShilf1K/CQb50R nAPOADpjC1138amQR5T1vYHsUxBxOG48orp+AxcdyG+XPjVxfK207qEkGyV1G9RTFC5m 6QJrgZ7DjPrq7UMBLlFEbdDnNqAZbcVqVAPM4uNXlh8128Tzs1F3SjTVdyuhT+c1ksDA aPSMwzqAKIK+C02iRNoUoE+l+pwCld2hdjVgb3i+JCcPaoFk9Rttd7645dOTYl4S8AeV BMU1kxpep7GKWmShyBHa8rsnEPWTb9X6Dkkng7PqSPYo5CUHEXIzkSVRDTQdzlJ7puXX ZSHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770720860; x=1771325660; h=content-transfer-encoding:in-reply-to:autocrypt:content-language :references:cc:to:from:subject:user-agent:mime-version:date :message-id:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=j/dEX2kKzzmAnt1Zegeuwsix6l/RE3FSLEbF5w2fmL0=; b=Td9Cf8Gve6O8QCShTaJiIhER5ERgdSO3/RjiA8d3Q8ZjwstiB78zmLKy0xNMbqKseG 4F2iySOltQfFjfsDM82NqSbYmTVQ4ai2QzRA40r3hbjN5qAnODwdIM8AQFgu+bCaPfth UQkXpNrliuHM+T+kmzcYiqg5NQrcVk5nEH6VWosCsel5sGhMoMWAf3sRtEDt4STe7KtH uXKMVBm2hQbGpZg2AC/a+Xves0OALAMLi1g8Ab77u1L/MF7YrVyqcAzJwHkQVcqCCJOe KhzOPKTw67h2U9wvyd7UOFx6+klYnJIuguvnxLfCPivqyKoApT+8AGzhEcqgV9brtbGl pvzA== X-Gm-Message-State: AOJu0YwqIZsyvYBpfNdYACgmzRHn+uq3jgTWYcQds8PByqUSGvu25pf3 Tg5HXjxa0dW4UzUuynekO+tasgTm4jr0d33wQSU6cJLQBk4wGTG5+gQgZUH6melhbfYCYROsyM/ dBjI= X-Gm-Gg: AZuq6aL6bkm5OQ9KebhLwzmNPSpXGokPha6dCoOPKRNJJn9uTpxJShZAkDu/KuTHPE5 OQ/KvkjsowQvHxJzRLz7Qc8iymiT+aGAuqb4cihkeJl8TSJXsphnjoaDEolf9w/wQP6YzVHzwcX OOlttMcLE5r/S47X+jMnen8qJ+8YbatyUhw8NQ+p0hsK2M10D4hc+EfopwA/WxoCzKRxJzZe6sP gYR4WuVL1JRILWFpZ7ct1GjCngKaodRhIDMHM5Iwx0DtI9F5LmWUkWKf9wxB0jWnDMSzAnDYcdD UxyyaO37PgWvHuZEHaFDE2BD+KbkJyn/Xp3K5w+NwhMck5QI9LZ343f9yYaTvKX8JDoW9wpRDJG 5JpzU807HZJ8DG5ShPynEXNNElaMm5EWDUFJsTTgcd4cSe87dA5uwmbGFRixtjQlEEmJE0j9mDu 5VVYpf9lB07qmMCXLLpocjCdGElf4pkWkpDmndOrIRdhqm22EMoRWzaKoEzieO9azewxS0qotVv EUaI+NVWt4vXLg= X-Received: by 2002:a05:600c:4e8b:b0:477:7bca:8b2b with SMTP id 5b1f17b1804b1-483507f2bb3mr22749045e9.15.1770720859895; Tue, 10 Feb 2026 02:54:19 -0800 (PST) Message-ID: Date: Tue, 10 Feb 2026 11:54:18 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: [PATCH v4 3/5] vPCI: move vpci_init_capabilities() to a separate file From: Jan Beulich To: "xen-devel@lists.xenproject.org" Cc: =?UTF-8?Q?Roger_Pau_Monn=C3=A9?= , Stewart Hildebrand References: <25c65557-c97c-499c-a698-571e40cde563@suse.com> Content-Language: en-US Autocrypt: addr=jbeulich@suse.com; keydata= xsDiBFk3nEQRBADAEaSw6zC/EJkiwGPXbWtPxl2xCdSoeepS07jW8UgcHNurfHvUzogEq5xk hu507c3BarVjyWCJOylMNR98Yd8VqD9UfmX0Hb8/BrA+Hl6/DB/eqGptrf4BSRwcZQM32aZK 7Pj2XbGWIUrZrd70x1eAP9QE3P79Y2oLrsCgbZJfEwCgvz9JjGmQqQkRiTVzlZVCJYcyGGsD /0tbFCzD2h20ahe8rC1gbb3K3qk+LpBtvjBu1RY9drYk0NymiGbJWZgab6t1jM7sk2vuf0Py O9Hf9XBmK0uE9IgMaiCpc32XV9oASz6UJebwkX+zF2jG5I1BfnO9g7KlotcA/v5ClMjgo6Gl MDY4HxoSRu3i1cqqSDtVlt+AOVBJBACrZcnHAUSuCXBPy0jOlBhxPqRWv6ND4c9PH1xjQ3NP nxJuMBS8rnNg22uyfAgmBKNLpLgAGVRMZGaGoJObGf72s6TeIqKJo/LtggAS9qAUiuKVnygo 3wjfkS9A3DRO+SpU7JqWdsveeIQyeyEJ/8PTowmSQLakF+3fote9ybzd880fSmFuIEJldWxp Y2ggPGpiZXVsaWNoQHN1c2UuY29tPsJgBBMRAgAgBQJZN5xEAhsDBgsJCAcDAgQVAggDBBYC AwECHgECF4AACgkQoDSui/t3IH4J+wCfQ5jHdEjCRHj23O/5ttg9r9OIruwAn3103WUITZee e7Sbg12UgcQ5lv7SzsFNBFk3nEQQCACCuTjCjFOUdi5Nm244F+78kLghRcin/awv+IrTcIWF hUpSs1Y91iQQ7KItirz5uwCPlwejSJDQJLIS+QtJHaXDXeV6NI0Uef1hP20+y8qydDiVkv6l IreXjTb7DvksRgJNvCkWtYnlS3mYvQ9NzS9PhyALWbXnH6sIJd2O9lKS1Mrfq+y0IXCP10eS FFGg+Av3IQeFatkJAyju0PPthyTqxSI4lZYuJVPknzgaeuJv/2NccrPvmeDg6Coe7ZIeQ8Yj t0ARxu2xytAkkLCel1Lz1WLmwLstV30g80nkgZf/wr+/BXJW/oIvRlonUkxv+IbBM3dX2OV8 AmRv1ySWPTP7AAMFB/9PQK/VtlNUJvg8GXj9ootzrteGfVZVVT4XBJkfwBcpC/XcPzldjv+3 HYudvpdNK3lLujXeA5fLOH+Z/G9WBc5pFVSMocI71I8bT8lIAzreg0WvkWg5V2WZsUMlnDL9 mpwIGFhlbM3gfDMs7MPMu8YQRFVdUvtSpaAs8OFfGQ0ia3LGZcjA6Ik2+xcqscEJzNH+qh8V m5jjp28yZgaqTaRbg3M/+MTbMpicpZuqF4rnB0AQD12/3BNWDR6bmh+EkYSMcEIpQmBM51qM EKYTQGybRCjpnKHGOxG0rfFY1085mBDZCH5Kx0cl0HVJuQKC+dV2ZY5AqjcKwAxpE75MLFkr wkkEGBECAAkFAlk3nEQCGwwACgkQoDSui/t3IH7nnwCfcJWUDUFKdCsBH/E5d+0ZnMQi+G0A nAuWpQkjM1ASeQwSHEeAWPgskBQL In-Reply-To: <25c65557-c97c-499c-a698-571e40cde563@suse.com> Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1770720882056154100 Content-Type: text/plain; charset="utf-8" Let's keep capability handling together. Start with moving vpci_init_capabilities() and its helpers, plus splitting out of its cleanup counterpart. Signed-off-by: Jan Beulich --- vpci_get_register(), while now only used by cap.c, didn't look like it would want moving there. --- v4: New. --- a/xen/drivers/vpci/Makefile +++ b/xen/drivers/vpci/Makefile @@ -1,2 +1,3 @@ +obj-y +=3D cap.o obj-y +=3D vpci.o header.o rebar.o obj-$(CONFIG_HAS_PCI_MSI) +=3D msi.o msix.o --- /dev/null +++ b/xen/drivers/vpci/cap.c @@ -0,0 +1,252 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Capability handling for guest PCI configuration space. + */ + +#include "private.h" + +#include + +extern const vpci_capability_t __start_vpci_array[]; +extern const vpci_capability_t __end_vpci_array[]; +#define NUM_VPCI_INIT (__end_vpci_array - __start_vpci_array) + +static struct vpci_register *vpci_get_previous_cap_register( + const struct vpci *vpci, unsigned int offset) +{ + unsigned int next; + struct vpci_register *r; + + if ( offset < 0x40 ) + { + ASSERT_UNREACHABLE(); + return NULL; + } + + for ( r =3D vpci_get_register(vpci, PCI_CAPABILITY_LIST, 1); r; + r =3D next >=3D 0x40 ? vpci_get_register(vpci, + next + PCI_CAP_LIST_NEXT, 1) + : NULL ) + { + next =3D (unsigned int)(uintptr_t)r->private; + ASSERT(next =3D=3D (uintptr_t)r->private); + if ( next =3D=3D offset ) + break; + } + + return r; +} + +static int vpci_capability_hide(const struct pci_dev *pdev, unsigned int c= ap) +{ + const unsigned int offset =3D pci_find_cap_offset(pdev->sbdf, cap); + struct vpci_register *prev_r, *next_r; + struct vpci *vpci =3D pdev->vpci; + + if ( !offset ) + { + ASSERT_UNREACHABLE(); + return 0; + } + + spin_lock(&vpci->lock); + prev_r =3D vpci_get_previous_cap_register(vpci, offset); + next_r =3D vpci_get_register(vpci, offset + PCI_CAP_LIST_NEXT, 1); + if ( !prev_r || !next_r ) + { + spin_unlock(&vpci->lock); + return -ENODEV; + } + + prev_r->private =3D next_r->private; + /* + * Not calling vpci_remove_registers() here is to avoid redoing + * the register search. + */ + list_del(&next_r->node); + spin_unlock(&vpci->lock); + xfree(next_r); + + if ( !is_hardware_domain(pdev->domain) ) + return vpci_remove_registers(vpci, offset + PCI_CAP_LIST_ID, 1); + + return 0; +} + +static struct vpci_register *vpci_get_previous_ext_cap_register( + const struct vpci *vpci, unsigned int offset) +{ + unsigned int pos =3D PCI_CFG_SPACE_SIZE; + struct vpci_register *r; + + if ( offset <=3D PCI_CFG_SPACE_SIZE ) + { + ASSERT_UNREACHABLE(); + return NULL; + } + + for ( r =3D vpci_get_register(vpci, pos, 4); r; + r =3D pos > PCI_CFG_SPACE_SIZE ? vpci_get_register(vpci, pos, 4) + : NULL ) + { + uint32_t header =3D (uint32_t)(uintptr_t)r->private; + + ASSERT(header =3D=3D (uintptr_t)r->private); + + pos =3D PCI_EXT_CAP_NEXT(header); + if ( pos =3D=3D offset ) + break; + } + + return r; +} + +static int vpci_ext_capability_hide( + const struct pci_dev *pdev, unsigned int cap) +{ + const unsigned int offset =3D pci_find_ext_capability(pdev, cap); + struct vpci_register *r, *prev_r; + struct vpci *vpci =3D pdev->vpci; + uint32_t header, pre_header; + + if ( offset < PCI_CFG_SPACE_SIZE ) + { + ASSERT_UNREACHABLE(); + return 0; + } + + spin_lock(&vpci->lock); + r =3D vpci_get_register(vpci, offset, 4); + if ( !r ) + { + spin_unlock(&vpci->lock); + return -ENODEV; + } + + header =3D (uint32_t)(uintptr_t)r->private; + if ( offset =3D=3D PCI_CFG_SPACE_SIZE ) + { + if ( PCI_EXT_CAP_NEXT(header) <=3D PCI_CFG_SPACE_SIZE ) + r->private =3D (void *)0; + else + /* + * The first extended capability (0x100) can not be removed fr= om + * the linked list, so instead mask its capability ID to retur= n 0 + * hopefully forcing OSes to skip it. + */ + r->private =3D (void *)(uintptr_t)(header & ~PCI_EXT_CAP_ID(he= ader)); + + spin_unlock(&vpci->lock); + return 0; + } + + prev_r =3D vpci_get_previous_ext_cap_register(vpci, offset); + if ( !prev_r ) + { + spin_unlock(&vpci->lock); + return -ENODEV; + } + + pre_header =3D (uint32_t)(uintptr_t)prev_r->private; + pre_header &=3D ~PCI_EXT_CAP_NEXT_MASK; + pre_header |=3D header & PCI_EXT_CAP_NEXT_MASK; + prev_r->private =3D (void *)(uintptr_t)pre_header; + + list_del(&r->node); + spin_unlock(&vpci->lock); + xfree(r); + + return 0; +} + +int vpci_init_capabilities(struct pci_dev *pdev) +{ + for ( unsigned int i =3D 0; i < NUM_VPCI_INIT; i++ ) + { + const vpci_capability_t *capability =3D &__start_vpci_array[i]; + const unsigned int cap =3D capability->id; + const bool is_ext =3D capability->is_ext; + unsigned int pos =3D 0; + int rc; + + if ( !is_ext ) + pos =3D pci_find_cap_offset(pdev->sbdf, cap); + else if ( is_hardware_domain(pdev->domain) ) + pos =3D pci_find_ext_capability(pdev, cap); + + if ( !pos ) + continue; + + rc =3D capability->init(pdev); + if ( rc ) + { + const char *type =3D is_ext ? "extended" : "legacy"; + + printk(XENLOG_WARNING + "%pd %pp: init %s cap %u fail rc=3D%d, mask it\n", + pdev->domain, &pdev->sbdf, type, cap, rc); + + if ( capability->cleanup ) + { + rc =3D capability->cleanup(pdev, true); + if ( rc ) + { + printk(XENLOG_ERR "%pd %pp: clean %s cap %u fail rc=3D= %d\n", + pdev->domain, &pdev->sbdf, type, cap, rc); + if ( !is_hardware_domain(pdev->domain) ) + return rc; + } + } + + if ( !is_ext ) + rc =3D vpci_capability_hide(pdev, cap); + else + rc =3D vpci_ext_capability_hide(pdev, cap); + if ( rc ) + { + printk(XENLOG_ERR "%pd %pp: hide %s cap %u fail rc=3D%d\n", + pdev->domain, &pdev->sbdf, type, cap, rc); + return rc; + } + } + } + + return 0; +} + +void vpci_cleanup_capabilities(struct pci_dev *pdev) +{ + for ( unsigned int i =3D 0; i < NUM_VPCI_INIT; i++ ) + { + const vpci_capability_t *capability =3D &__start_vpci_array[i]; + const unsigned int cap =3D capability->id; + unsigned int pos =3D 0; + + if ( !capability->cleanup ) + continue; + + if ( !capability->is_ext ) + pos =3D pci_find_cap_offset(pdev->sbdf, cap); + else if ( is_hardware_domain(pdev->domain) ) + pos =3D pci_find_ext_capability(pdev, cap); + if ( pos ) + { + int rc =3D capability->cleanup(pdev, false); + + if ( rc ) + printk(XENLOG_ERR "%pd %pp: clean %s cap %u fail rc=3D%d\n= ", + pdev->domain, &pdev->sbdf, + capability->is_ext ? "extended" : "legacy", cap, rc= ); + } + } +} + +/* + * Local variables: + * mode: C + * c-file-style: "BSD" + * c-basic-offset: 4 + * tab-width: 4 + * indent-tabs-mode: nil + * End: + */ --- a/xen/drivers/vpci/private.h +++ b/xen/drivers/vpci/private.h @@ -9,6 +9,20 @@ typedef uint32_t vpci_read_t(const struc typedef void vpci_write_t(const struct pci_dev *pdev, unsigned int reg, uint32_t val, void *data); =20 +/* Internal struct to store the emulated PCI registers. */ +struct vpci_register { + vpci_read_t *read; + vpci_write_t *write; + unsigned int size; + unsigned int offset; + void *private; + struct list_head node; + uint32_t ro_mask; + uint32_t rw1c_mask; + uint32_t rsvdp_mask; + uint32_t rsvdz_mask; +}; + typedef struct { unsigned int id; bool is_ext; @@ -30,6 +44,9 @@ typedef struct { #define REGISTER_VPCI_EXTCAP(name, finit, fclean) \ REGISTER_VPCI_CAPABILITY(PCI_EXT_CAP_ID_##name, name, finit, fclean, t= rue) =20 +int vpci_init_capabilities(struct pci_dev *pdev); +void vpci_cleanup_capabilities(struct pci_dev *pdev); + /* Add/remove a register handler. */ int __must_check vpci_add_register_mask(struct vpci *vpci, vpci_read_t *read_handler, @@ -47,6 +64,10 @@ int __must_check vpci_add_register(struc int vpci_remove_registers(struct vpci *vpci, unsigned int start, unsigned int size); =20 +struct vpci_register *vpci_get_register(const struct vpci *vpci, + unsigned int offset, + unsigned int size); + /* Helper to return the value passed in data. */ uint32_t cf_check vpci_read_val( const struct pci_dev *pdev, unsigned int reg, void *data); --- a/xen/drivers/vpci/vpci.c +++ b/xen/drivers/vpci/vpci.c @@ -22,24 +22,7 @@ #include #include =20 -/* Internal struct to store the emulated PCI registers. */ -struct vpci_register { - vpci_read_t *read; - vpci_write_t *write; - unsigned int size; - unsigned int offset; - void *private; - struct list_head node; - uint32_t ro_mask; - uint32_t rw1c_mask; - uint32_t rsvdp_mask; - uint32_t rsvdz_mask; -}; - #ifdef __XEN__ -extern const vpci_capability_t __start_vpci_array[]; -extern const vpci_capability_t __end_vpci_array[]; -#define NUM_VPCI_INIT (__end_vpci_array - __start_vpci_array) =20 #ifdef CONFIG_HAS_VPCI_GUEST_SUPPORT static int assign_virtual_sbdf(struct pci_dev *pdev) @@ -84,9 +67,9 @@ static int assign_virtual_sbdf(struct pc =20 #endif /* CONFIG_HAS_VPCI_GUEST_SUPPORT */ =20 -static struct vpci_register *vpci_get_register(const struct vpci *vpci, - unsigned int offset, - unsigned int size) +struct vpci_register *vpci_get_register(const struct vpci *vpci, + unsigned int offset, + unsigned int size) { struct vpci_register *r; =20 @@ -104,209 +87,6 @@ static struct vpci_register *vpci_get_re return NULL; } =20 -static struct vpci_register *vpci_get_previous_cap_register( - const struct vpci *vpci, unsigned int offset) -{ - unsigned int next; - struct vpci_register *r; - - if ( offset < 0x40 ) - { - ASSERT_UNREACHABLE(); - return NULL; - } - - for ( r =3D vpci_get_register(vpci, PCI_CAPABILITY_LIST, 1); r; - r =3D next >=3D 0x40 ? vpci_get_register(vpci, - next + PCI_CAP_LIST_NEXT, 1) - : NULL ) - { - next =3D (unsigned int)(uintptr_t)r->private; - ASSERT(next =3D=3D (uintptr_t)r->private); - if ( next =3D=3D offset ) - break; - } - - return r; -} - -static int vpci_capability_hide(const struct pci_dev *pdev, unsigned int c= ap) -{ - const unsigned int offset =3D pci_find_cap_offset(pdev->sbdf, cap); - struct vpci_register *prev_r, *next_r; - struct vpci *vpci =3D pdev->vpci; - - if ( !offset ) - { - ASSERT_UNREACHABLE(); - return 0; - } - - spin_lock(&vpci->lock); - prev_r =3D vpci_get_previous_cap_register(vpci, offset); - next_r =3D vpci_get_register(vpci, offset + PCI_CAP_LIST_NEXT, 1); - if ( !prev_r || !next_r ) - { - spin_unlock(&vpci->lock); - return -ENODEV; - } - - prev_r->private =3D next_r->private; - /* - * Not calling vpci_remove_registers() here is to avoid redoing - * the register search. - */ - list_del(&next_r->node); - spin_unlock(&vpci->lock); - xfree(next_r); - - if ( !is_hardware_domain(pdev->domain) ) - return vpci_remove_registers(vpci, offset + PCI_CAP_LIST_ID, 1); - - return 0; -} - -static struct vpci_register *vpci_get_previous_ext_cap_register( - const struct vpci *vpci, unsigned int offset) -{ - unsigned int pos =3D PCI_CFG_SPACE_SIZE; - struct vpci_register *r; - - if ( offset <=3D PCI_CFG_SPACE_SIZE ) - { - ASSERT_UNREACHABLE(); - return NULL; - } - - for ( r =3D vpci_get_register(vpci, pos, 4); r; - r =3D pos > PCI_CFG_SPACE_SIZE ? vpci_get_register(vpci, pos, 4) - : NULL ) - { - uint32_t header =3D (uint32_t)(uintptr_t)r->private; - - ASSERT(header =3D=3D (uintptr_t)r->private); - - pos =3D PCI_EXT_CAP_NEXT(header); - if ( pos =3D=3D offset ) - break; - } - - return r; -} - -static int vpci_ext_capability_hide( - const struct pci_dev *pdev, unsigned int cap) -{ - const unsigned int offset =3D pci_find_ext_capability(pdev, cap); - struct vpci_register *r, *prev_r; - struct vpci *vpci =3D pdev->vpci; - uint32_t header, pre_header; - - if ( offset < PCI_CFG_SPACE_SIZE ) - { - ASSERT_UNREACHABLE(); - return 0; - } - - spin_lock(&vpci->lock); - r =3D vpci_get_register(vpci, offset, 4); - if ( !r ) - { - spin_unlock(&vpci->lock); - return -ENODEV; - } - - header =3D (uint32_t)(uintptr_t)r->private; - if ( offset =3D=3D PCI_CFG_SPACE_SIZE ) - { - if ( PCI_EXT_CAP_NEXT(header) <=3D PCI_CFG_SPACE_SIZE ) - r->private =3D (void *)0; - else - /* - * The first extended capability (0x100) can not be removed fr= om - * the linked list, so instead mask its capability ID to retur= n 0 - * hopefully forcing OSes to skip it. - */ - r->private =3D (void *)(uintptr_t)(header & ~PCI_EXT_CAP_ID(he= ader)); - - spin_unlock(&vpci->lock); - return 0; - } - - prev_r =3D vpci_get_previous_ext_cap_register(vpci, offset); - if ( !prev_r ) - { - spin_unlock(&vpci->lock); - return -ENODEV; - } - - pre_header =3D (uint32_t)(uintptr_t)prev_r->private; - pre_header &=3D ~PCI_EXT_CAP_NEXT_MASK; - pre_header |=3D header & PCI_EXT_CAP_NEXT_MASK; - prev_r->private =3D (void *)(uintptr_t)pre_header; - - list_del(&r->node); - spin_unlock(&vpci->lock); - xfree(r); - - return 0; -} - -static int vpci_init_capabilities(struct pci_dev *pdev) -{ - for ( unsigned int i =3D 0; i < NUM_VPCI_INIT; i++ ) - { - const vpci_capability_t *capability =3D &__start_vpci_array[i]; - const unsigned int cap =3D capability->id; - const bool is_ext =3D capability->is_ext; - unsigned int pos =3D 0; - int rc; - - if ( !is_ext ) - pos =3D pci_find_cap_offset(pdev->sbdf, cap); - else if ( is_hardware_domain(pdev->domain) ) - pos =3D pci_find_ext_capability(pdev, cap); - - if ( !pos ) - continue; - - rc =3D capability->init(pdev); - if ( rc ) - { - const char *type =3D is_ext ? "extended" : "legacy"; - - printk(XENLOG_WARNING - "%pd %pp: init %s cap %u fail rc=3D%d, mask it\n", - pdev->domain, &pdev->sbdf, type, cap, rc); - - if ( capability->cleanup ) - { - rc =3D capability->cleanup(pdev, true); - if ( rc ) - { - printk(XENLOG_ERR "%pd %pp: clean %s cap %u fail rc=3D= %d\n", - pdev->domain, &pdev->sbdf, type, cap, rc); - if ( !is_hardware_domain(pdev->domain) ) - return rc; - } - } - - if ( !is_ext ) - rc =3D vpci_capability_hide(pdev, cap); - else - rc =3D vpci_ext_capability_hide(pdev, cap); - if ( rc ) - { - printk(XENLOG_ERR "%pd %pp: hide %s cap %u fail rc=3D%d\n", - pdev->domain, &pdev->sbdf, type, cap, rc); - return rc; - } - } - } - - return 0; -} - void vpci_deassign_device(struct pci_dev *pdev) { unsigned int i; @@ -322,29 +102,7 @@ void vpci_deassign_device(struct pci_dev &pdev->domain->vpci_dev_assigned_map); #endif =20 - for ( i =3D 0; i < NUM_VPCI_INIT; i++ ) - { - const vpci_capability_t *capability =3D &__start_vpci_array[i]; - const unsigned int cap =3D capability->id; - unsigned int pos =3D 0; - - if ( !capability->cleanup ) - continue; - - if ( !capability->is_ext ) - pos =3D pci_find_cap_offset(pdev->sbdf, cap); - else if ( is_hardware_domain(pdev->domain) ) - pos =3D pci_find_ext_capability(pdev, cap); - if ( pos ) - { - int rc =3D capability->cleanup(pdev, false); - - if ( rc ) - printk(XENLOG_ERR "%pd %pp: clean %s cap %u fail rc=3D%d\n= ", - pdev->domain, &pdev->sbdf, - capability->is_ext ? "extended" : "legacy", cap, rc= ); - } - } + vpci_cleanup_capabilities(pdev); =20 spin_lock(&pdev->vpci->lock); while ( !list_empty(&pdev->vpci->handlers) )