Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

xen/pci: Add a function to reset device for xen

When device on dom0 side has been reset, the vpci on Xen side
won't get notification, so that the cached state in vpci is
all out of date with the real device state.
To solve that problem, add a new function to clear all vpci
device state when device is reset on dom0 side.

And call that function in pcistub_init_device. Because when
using "pci-assignable-add" to assign a passthrough device in
Xen, it will reset passthrough device and the vpci state will
out of date, and then device will fail to restore bar state.

Signed-off-by: Jiqian Chen <Jiqian.Chen@amd.com>
Signed-off-by: Huang Rui <ray.huang@amd.com>
Signed-off-by: Jiqian Chen <Jiqian.Chen@amd.com>
Reviewed-by: Stefano Stabellini <sstabellini@kernel.org>
Message-ID: <20240924061437.2636766-2-Jiqian.Chen@amd.com>
Signed-off-by: Juergen Gross <jgross@suse.com>

authored by

Jiqian Chen and committed by
Juergen Gross
88801d04 c3dea3d5

+51 -3
+13
drivers/xen/pci.c
··· 173 173 return r; 174 174 } 175 175 176 + int xen_reset_device(const struct pci_dev *dev) 177 + { 178 + struct pci_device_reset device = { 179 + .dev.seg = pci_domain_nr(dev->bus), 180 + .dev.bus = dev->bus->number, 181 + .dev.devfn = dev->devfn, 182 + .flags = PCI_DEVICE_RESET_FLR, 183 + }; 184 + 185 + return HYPERVISOR_physdev_op(PHYSDEVOP_pci_device_reset, &device); 186 + } 187 + EXPORT_SYMBOL_GPL(xen_reset_device); 188 + 176 189 static int xen_pci_notifier(struct notifier_block *nb, 177 190 unsigned long action, void *data) 178 191 {
+15 -3
drivers/xen/xen-pciback/pci_stub.c
··· 89 89 return psdev; 90 90 } 91 91 92 + static int pcistub_reset_device_state(struct pci_dev *dev) 93 + { 94 + __pci_reset_function_locked(dev); 95 + 96 + if (!xen_pv_domain()) 97 + return xen_reset_device(dev); 98 + else 99 + return 0; 100 + } 101 + 92 102 /* Don't call this directly as it's called by pcistub_device_put */ 93 103 static void pcistub_device_release(struct kref *kref) 94 104 { ··· 117 107 /* Call the reset function which does not take lock as this 118 108 * is called from "unbind" which takes a device_lock mutex. 119 109 */ 120 - __pci_reset_function_locked(dev); 110 + pcistub_reset_device_state(dev); 121 111 if (dev_data && 122 112 pci_load_and_free_saved_state(dev, &dev_data->pci_saved_state)) 123 113 dev_info(&dev->dev, "Could not reload PCI state\n"); ··· 294 284 * (so it's ready for the next domain) 295 285 */ 296 286 device_lock_assert(&dev->dev); 297 - __pci_reset_function_locked(dev); 287 + pcistub_reset_device_state(dev); 298 288 299 289 dev_data = pci_get_drvdata(dev); 300 290 ret = pci_load_saved_state(dev, dev_data->pci_saved_state); ··· 430 420 dev_err(&dev->dev, "Could not store PCI conf saved state!\n"); 431 421 else { 432 422 dev_dbg(&dev->dev, "resetting (FLR, D3, etc) the device\n"); 433 - __pci_reset_function_locked(dev); 423 + err = pcistub_reset_device_state(dev); 424 + if (err) 425 + goto config_release; 434 426 pci_restore_state(dev); 435 427 } 436 428 /* Now disable the device (this also ensures some private device
+17
include/xen/interface/physdev.h
··· 256 256 */ 257 257 #define PHYSDEVOP_prepare_msix 30 258 258 #define PHYSDEVOP_release_msix 31 259 + /* 260 + * Notify the hypervisor that a PCI device has been reset, so that any 261 + * internally cached state is regenerated. Should be called after any 262 + * device reset performed by the hardware domain. 263 + */ 264 + #define PHYSDEVOP_pci_device_reset 32 265 + 259 266 struct physdev_pci_device { 260 267 /* IN */ 261 268 uint16_t seg; 262 269 uint8_t bus; 263 270 uint8_t devfn; 271 + }; 272 + 273 + struct pci_device_reset { 274 + struct physdev_pci_device dev; 275 + #define PCI_DEVICE_RESET_COLD 0x0 276 + #define PCI_DEVICE_RESET_WARM 0x1 277 + #define PCI_DEVICE_RESET_HOT 0x2 278 + #define PCI_DEVICE_RESET_FLR 0x3 279 + #define PCI_DEVICE_RESET_MASK 0x3 280 + uint32_t flags; 264 281 }; 265 282 266 283 #define PHYSDEVOP_DBGP_RESET_PREPARE 1
+6
include/xen/pci.h
··· 4 4 #define __XEN_PCI_H__ 5 5 6 6 #if defined(CONFIG_XEN_DOM0) 7 + int xen_reset_device(const struct pci_dev *dev); 7 8 int xen_find_device_domain_owner(struct pci_dev *dev); 8 9 int xen_register_device_domain_owner(struct pci_dev *dev, uint16_t domain); 9 10 int xen_unregister_device_domain_owner(struct pci_dev *dev); 10 11 #else 12 + static inline int xen_reset_device(const struct pci_dev *dev) 13 + { 14 + return -1; 15 + } 16 + 11 17 static inline int xen_find_device_domain_owner(struct pci_dev *dev) 12 18 { 13 19 return -1;