File Index
Files and directories
Sign in
FILE

pci_stub.c

File preview with syntax highlighting.

linux-6.19.14/drivers/xen/xen-pciback/pci_stub.c C
   1/*
   2 * PCI Stub Driver - Grabs devices in backend to be exported later
   3 *
   4 * Ryan Wilson <hap9@epoch.ncsc.mil>
   5 * Chris Bookholt <hap10@epoch.ncsc.mil>
   6 */
   7
   8#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
   9#define dev_fmt pr_fmt
  10
  11#include <linux/module.h>
  12#include <linux/init.h>
  13#include <linux/rwsem.h>
  14#include <linux/list.h>
  15#include <linux/spinlock.h>
  16#include <linux/kref.h>
  17#include <linux/pci.h>
  18#include <linux/wait.h>
  19#include <linux/sched.h>
  20#include <linux/atomic.h>
  21#include <xen/events.h>
  22#include <xen/pci.h>
  23#include <xen/xen.h>
  24#ifdef CONFIG_XEN_ACPI
  25#include <xen/acpi.h>
  26#endif
  27#include <asm/xen/hypervisor.h>
  28#include <xen/interface/physdev.h>
  29#include "pciback.h"
  30#include "conf_space.h"
  31#include "conf_space_quirks.h"
  32
  33#define PCISTUB_DRIVER_NAME "pciback"
  34
  35static char *pci_devs_to_hide;
  36wait_queue_head_t xen_pcibk_aer_wait_queue;
  37/*Add sem for sync AER handling and xen_pcibk remove/reconfigue ops,
  38* We want to avoid in middle of AER ops, xen_pcibk devices is being removed
  39*/
  40static DECLARE_RWSEM(pcistub_sem);
  41module_param_named(hide, pci_devs_to_hide, charp, 0444);
  42
  43struct pcistub_device_id {
  44	struct list_head slot_list;
  45	int domain;
  46	unsigned char bus;
  47	unsigned int devfn;
  48};
  49static LIST_HEAD(pcistub_device_ids);
  50static DEFINE_SPINLOCK(device_ids_lock);
  51
  52struct pcistub_device {
  53	struct kref kref;
  54	struct list_head dev_list;
  55	spinlock_t lock;
  56
  57	struct pci_dev *dev;
  58	struct xen_pcibk_device *pdev;/* non-NULL if struct pci_dev is in use */
  59#ifdef CONFIG_XEN_ACPI
  60	int gsi;
  61#endif
  62};
  63
  64/* Access to pcistub_devices & seized_devices lists and the initialize_devices
  65 * flag must be locked with pcistub_devices_lock
  66 */
  67static DEFINE_SPINLOCK(pcistub_devices_lock);
  68static LIST_HEAD(pcistub_devices);
  69
  70/* wait for device_initcall before initializing our devices
  71 * (see pcistub_init_devices_late)
  72 */
  73static int initialize_devices;
  74static LIST_HEAD(seized_devices);
  75
  76static struct pcistub_device *pcistub_device_alloc(struct pci_dev *dev)
  77{
  78	struct pcistub_device *psdev;
  79
  80	dev_dbg(&dev->dev, "pcistub_device_alloc\n");
  81
  82	psdev = kzalloc(sizeof(*psdev), GFP_KERNEL);
  83	if (!psdev)
  84		return NULL;
  85
  86	psdev->dev = pci_dev_get(dev);
  87	if (!psdev->dev) {
  88		kfree(psdev);
  89		return NULL;
  90	}
  91
  92	kref_init(&psdev->kref);
  93	spin_lock_init(&psdev->lock);
  94#ifdef CONFIG_XEN_ACPI
  95	psdev->gsi = -1;
  96#endif
  97
  98	return psdev;
  99}
 100
 101static int pcistub_reset_device_state(struct pci_dev *dev)
 102{
 103	__pci_reset_function_locked(dev);
 104
 105	if (!xen_pv_domain())
 106		return xen_reset_device(dev);
 107	else
 108		return 0;
 109}
 110
 111/* Don't call this directly as it's called by pcistub_device_put */
 112static void pcistub_device_release(struct kref *kref)
 113{
 114	struct pcistub_device *psdev;
 115	struct pci_dev *dev;
 116	struct xen_pcibk_dev_data *dev_data;
 117
 118	psdev = container_of(kref, struct pcistub_device, kref);
 119	dev = psdev->dev;
 120	dev_data = pci_get_drvdata(dev);
 121
 122	dev_dbg(&dev->dev, "pcistub_device_release\n");
 123
 124	xen_unregister_device_domain_owner(dev);
 125
 126	/* Call the reset function which does not take lock as this
 127	 * is called from "unbind" which takes a device_lock mutex.
 128	 */
 129	pcistub_reset_device_state(dev);
 130	if (dev_data &&
 131	    pci_load_and_free_saved_state(dev, &dev_data->pci_saved_state))
 132		dev_info(&dev->dev, "Could not reload PCI state\n");
 133	else
 134		pci_restore_state(dev);
 135
 136	if (dev->msix_cap) {
 137		struct physdev_pci_device ppdev = {
 138			.seg = pci_domain_nr(dev->bus),
 139			.bus = dev->bus->number,
 140			.devfn = dev->devfn
 141		};
 142		int err = HYPERVISOR_physdev_op(PHYSDEVOP_release_msix,
 143						&ppdev);
 144
 145		if (err && err != -ENOSYS)
 146			dev_warn(&dev->dev, "MSI-X release failed (%d)\n",
 147				 err);
 148	}
 149
 150	/* Disable the device */
 151	xen_pcibk_reset_device(dev);
 152
 153	kfree(dev_data);
 154	pci_set_drvdata(dev, NULL);
 155
 156	/* Clean-up the device */
 157	xen_pcibk_config_free_dyn_fields(dev);
 158	xen_pcibk_config_free_dev(dev);
 159
 160	pci_clear_dev_assigned(dev);
 161	pci_dev_put(dev);
 162
 163	kfree(psdev);
 164}
 165
 166static inline void pcistub_device_get(struct pcistub_device *psdev)
 167{
 168	kref_get(&psdev->kref);
 169}
 170
 171static inline void pcistub_device_put(struct pcistub_device *psdev)
 172{
 173	kref_put(&psdev->kref, pcistub_device_release);
 174}
 175
 176static struct pcistub_device *pcistub_device_find_locked(int domain, int bus,
 177							 int slot, int func)
 178{
 179	struct pcistub_device *psdev;
 180
 181	list_for_each_entry(psdev, &pcistub_devices, dev_list) {
 182		if (psdev->dev != NULL
 183		    && domain == pci_domain_nr(psdev->dev->bus)
 184		    && bus == psdev->dev->bus->number
 185		    && slot == PCI_SLOT(psdev->dev->devfn)
 186		    && func == PCI_FUNC(psdev->dev->devfn)) {
 187			return psdev;
 188		}
 189	}
 190
 191	return NULL;
 192}
 193
 194static struct pcistub_device *pcistub_device_find(int domain, int bus,
 195						  int slot, int func)
 196{
 197	struct pcistub_device *psdev;
 198	unsigned long flags;
 199
 200	spin_lock_irqsave(&pcistub_devices_lock, flags);
 201
 202	psdev = pcistub_device_find_locked(domain, bus, slot, func);
 203	if (psdev)
 204		pcistub_device_get(psdev);
 205
 206	spin_unlock_irqrestore(&pcistub_devices_lock, flags);
 207	return psdev;
 208}
 209
 210static struct pci_dev *pcistub_device_get_pci_dev(struct xen_pcibk_device *pdev,
 211						  struct pcistub_device *psdev)
 212{
 213	struct pci_dev *pci_dev = NULL;
 214	unsigned long flags;
 215
 216	spin_lock_irqsave(&psdev->lock, flags);
 217	if (!psdev->pdev) {
 218		psdev->pdev = pdev;
 219		pci_dev = psdev->dev;
 220	}
 221	spin_unlock_irqrestore(&psdev->lock, flags);
 222
 223	if (pci_dev)
 224		pcistub_device_get(psdev);
 225
 226	return pci_dev;
 227}
 228
 229#ifdef CONFIG_XEN_ACPI
 230static int pcistub_get_gsi_from_sbdf(unsigned int sbdf)
 231{
 232	struct pcistub_device *psdev;
 233	int domain = (sbdf >> 16) & 0xffff;
 234	int bus = PCI_BUS_NUM(sbdf);
 235	int slot = PCI_SLOT(sbdf);
 236	int func = PCI_FUNC(sbdf);
 237
 238	psdev = pcistub_device_find(domain, bus, slot, func);
 239
 240	if (!psdev)
 241		return -ENODEV;
 242
 243	return psdev->gsi;
 244}
 245#endif
 246
 247struct pci_dev *pcistub_get_pci_dev_by_slot(struct xen_pcibk_device *pdev,
 248					    int domain, int bus,
 249					    int slot, int func)
 250{
 251	struct pcistub_device *psdev;
 252	struct pci_dev *found_dev = NULL;
 253	unsigned long flags;
 254
 255	spin_lock_irqsave(&pcistub_devices_lock, flags);
 256
 257	psdev = pcistub_device_find_locked(domain, bus, slot, func);
 258	if (psdev)
 259		found_dev = pcistub_device_get_pci_dev(pdev, psdev);
 260
 261	spin_unlock_irqrestore(&pcistub_devices_lock, flags);
 262	return found_dev;
 263}
 264
 265/*
 266 * Called when:
 267 *  - XenBus state has been reconfigure (pci unplug). See xen_pcibk_remove_device
 268 *  - XenBus state has been disconnected (guest shutdown). See xen_pcibk_xenbus_remove
 269 *  - 'echo BDF > unbind' on pciback module with no guest attached. See pcistub_remove
 270 *  - 'echo BDF > unbind' with a guest still using it. See pcistub_remove
 271 *
 272 *  As such we have to be careful.
 273 *
 274 *  To make this easier, the caller has to hold the device lock.
 275 */
 276void pcistub_put_pci_dev(struct pci_dev *dev)
 277{
 278	struct pcistub_device *psdev, *found_psdev = NULL;
 279	unsigned long flags;
 280	struct xen_pcibk_dev_data *dev_data;
 281	int ret;
 282
 283	spin_lock_irqsave(&pcistub_devices_lock, flags);
 284
 285	list_for_each_entry(psdev, &pcistub_devices, dev_list) {
 286		if (psdev->dev == dev) {
 287			found_psdev = psdev;
 288			break;
 289		}
 290	}
 291
 292	spin_unlock_irqrestore(&pcistub_devices_lock, flags);
 293	if (WARN_ON(!found_psdev))
 294		return;
 295
 296	/*hold this lock for avoiding breaking link between
 297	* pcistub and xen_pcibk when AER is in processing
 298	*/
 299	down_write(&pcistub_sem);
 300	/* Cleanup our device
 301	 * (so it's ready for the next domain)
 302	 */
 303	device_lock_assert(&dev->dev);
 304	pcistub_reset_device_state(dev);
 305
 306	dev_data = pci_get_drvdata(dev);
 307	ret = pci_load_saved_state(dev, dev_data->pci_saved_state);
 308	if (!ret) {
 309		/*
 310		 * The usual sequence is pci_save_state & pci_restore_state
 311		 * but the guest might have messed the configuration space up.
 312		 * Use the initial version (when device was bound to us).
 313		 */
 314		pci_restore_state(dev);
 315	} else
 316		dev_info(&dev->dev, "Could not reload PCI state\n");
 317	/* This disables the device. */
 318	xen_pcibk_reset_device(dev);
 319
 320	/* And cleanup up our emulated fields. */
 321	xen_pcibk_config_reset_dev(dev);
 322	xen_pcibk_config_free_dyn_fields(dev);
 323
 324	dev_data->allow_interrupt_control = 0;
 325
 326	xen_unregister_device_domain_owner(dev);
 327
 328	spin_lock_irqsave(&found_psdev->lock, flags);
 329	found_psdev->pdev = NULL;
 330	spin_unlock_irqrestore(&found_psdev->lock, flags);
 331
 332	pcistub_device_put(found_psdev);
 333	up_write(&pcistub_sem);
 334}
 335
 336static int pcistub_match_one(struct pci_dev *dev,
 337			     struct pcistub_device_id *pdev_id)
 338{
 339	/* Match the specified device by domain, bus, slot, func and also if
 340	 * any of the device's parent bridges match.
 341	 */
 342	for (; dev != NULL; dev = dev->bus->self) {
 343		if (pci_domain_nr(dev->bus) == pdev_id->domain
 344		    && dev->bus->number == pdev_id->bus
 345		    && dev->devfn == pdev_id->devfn)
 346			return 1;
 347
 348		/* Sometimes topmost bridge links to itself. */
 349		if (dev == dev->bus->self)
 350			break;
 351	}
 352
 353	return 0;
 354}
 355
 356static int pcistub_match(struct pci_dev *dev)
 357{
 358	struct pcistub_device_id *pdev_id;
 359	unsigned long flags;
 360	int found = 0;
 361
 362	spin_lock_irqsave(&device_ids_lock, flags);
 363	list_for_each_entry(pdev_id, &pcistub_device_ids, slot_list) {
 364		if (pcistub_match_one(dev, pdev_id)) {
 365			found = 1;
 366			break;
 367		}
 368	}
 369	spin_unlock_irqrestore(&device_ids_lock, flags);
 370
 371	return found;
 372}
 373
 374static int pcistub_init_device(struct pcistub_device *psdev)
 375{
 376	struct xen_pcibk_dev_data *dev_data;
 377	struct pci_dev *dev;
 378#ifdef CONFIG_XEN_ACPI
 379	int gsi, trigger, polarity;
 380#endif
 381	int err = 0;
 382
 383	if (!psdev)
 384		return -EINVAL;
 385
 386	dev = psdev->dev;
 387
 388	dev_dbg(&dev->dev, "initializing...\n");
 389
 390	/* The PCI backend is not intended to be a module (or to work with
 391	 * removable PCI devices (yet). If it were, xen_pcibk_config_free()
 392	 * would need to be called somewhere to free the memory allocated
 393	 * here and then to call kfree(pci_get_drvdata(psdev->dev)).
 394	 */
 395	dev_data = kzalloc(sizeof(*dev_data) +  strlen(DRV_NAME "[]")
 396				+ strlen(pci_name(dev)) + 1, GFP_KERNEL);
 397	if (!dev_data) {
 398		err = -ENOMEM;
 399		goto out;
 400	}
 401	pci_set_drvdata(dev, dev_data);
 402
 403	/*
 404	 * Setup name for fake IRQ handler. It will only be enabled
 405	 * once the device is turned on by the guest.
 406	 */
 407	sprintf(dev_data->irq_name, DRV_NAME "[%s]", pci_name(dev));
 408
 409	dev_dbg(&dev->dev, "initializing config\n");
 410
 411	init_waitqueue_head(&xen_pcibk_aer_wait_queue);
 412	err = xen_pcibk_config_init_dev(dev);
 413	if (err)
 414		goto out;
 415
 416	/* HACK: Force device (& ACPI) to determine what IRQ it's on - we
 417	 * must do this here because pcibios_enable_device may specify
 418	 * the pci device's true irq (and possibly its other resources)
 419	 * if they differ from what's in the configuration space.
 420	 * This makes the assumption that the device's resources won't
 421	 * change after this point (otherwise this code may break!)
 422	 */
 423	dev_dbg(&dev->dev, "enabling device\n");
 424	err = pci_enable_device(dev);
 425	if (err)
 426		goto config_release;
 427
 428	if (dev->msix_cap) {
 429		struct physdev_pci_device ppdev = {
 430			.seg = pci_domain_nr(dev->bus),
 431			.bus = dev->bus->number,
 432			.devfn = dev->devfn
 433		};
 434
 435		err = HYPERVISOR_physdev_op(PHYSDEVOP_prepare_msix, &ppdev);
 436		if (err && err != -ENOSYS)
 437			dev_err(&dev->dev, "MSI-X preparation failed (%d)\n",
 438				err);
 439	}
 440
 441	/* We need the device active to save the state. */
 442	dev_dbg(&dev->dev, "save state of device\n");
 443	pci_save_state(dev);
 444	dev_data->pci_saved_state = pci_store_saved_state(dev);
 445	if (!dev_data->pci_saved_state)
 446		dev_err(&dev->dev, "Could not store PCI conf saved state!\n");
 447	else {
 448		dev_dbg(&dev->dev, "resetting (FLR, D3, etc) the device\n");
 449		err = pcistub_reset_device_state(dev);
 450		if (err)
 451			goto config_release;
 452		pci_restore_state(dev);
 453	}
 454
 455#ifdef CONFIG_XEN_ACPI
 456	if (xen_initial_domain() && xen_pvh_domain()) {
 457		err = xen_acpi_get_gsi_info(dev, &gsi, &trigger, &polarity);
 458		if (err) {
 459			dev_err(&dev->dev, "Fail to get gsi info!\n");
 460			goto config_release;
 461		}
 462		err = xen_pvh_setup_gsi(gsi, trigger, polarity);
 463		if (err)
 464			goto config_release;
 465		psdev->gsi = gsi;
 466	}
 467#endif
 468
 469	/* Now disable the device (this also ensures some private device
 470	 * data is setup before we export)
 471	 */
 472	dev_dbg(&dev->dev, "reset device\n");
 473	xen_pcibk_reset_device(dev);
 474
 475	pci_set_dev_assigned(dev);
 476	return 0;
 477
 478config_release:
 479	xen_pcibk_config_free_dev(dev);
 480
 481out:
 482	pci_set_drvdata(dev, NULL);
 483	kfree(dev_data);
 484	return err;
 485}
 486
 487/*
 488 * Because some initialization still happens on
 489 * devices during fs_initcall, we need to defer
 490 * full initialization of our devices until
 491 * device_initcall.
 492 */
 493static int __init pcistub_init_devices_late(void)
 494{
 495	struct pcistub_device *psdev;
 496	unsigned long flags;
 497	int err = 0;
 498
 499	spin_lock_irqsave(&pcistub_devices_lock, flags);
 500
 501	while (!list_empty(&seized_devices)) {
 502		psdev = container_of(seized_devices.next,
 503				     struct pcistub_device, dev_list);
 504		list_del(&psdev->dev_list);
 505
 506		spin_unlock_irqrestore(&pcistub_devices_lock, flags);
 507
 508		err = pcistub_init_device(psdev);
 509		if (err) {
 510			dev_err(&psdev->dev->dev,
 511				"error %d initializing device\n", err);
 512			kfree(psdev);
 513			psdev = NULL;
 514		}
 515
 516		spin_lock_irqsave(&pcistub_devices_lock, flags);
 517
 518		if (psdev)
 519			list_add_tail(&psdev->dev_list, &pcistub_devices);
 520	}
 521
 522	initialize_devices = 1;
 523
 524	spin_unlock_irqrestore(&pcistub_devices_lock, flags);
 525
 526	return 0;
 527}
 528
 529static void pcistub_device_id_add_list(struct pcistub_device_id *new,
 530				       int domain, int bus, unsigned int devfn)
 531{
 532	struct pcistub_device_id *pci_dev_id;
 533	unsigned long flags;
 534	int found = 0;
 535
 536	spin_lock_irqsave(&device_ids_lock, flags);
 537
 538	list_for_each_entry(pci_dev_id, &pcistub_device_ids, slot_list) {
 539		if (pci_dev_id->domain == domain && pci_dev_id->bus == bus &&
 540		    pci_dev_id->devfn == devfn) {
 541			found = 1;
 542			break;
 543		}
 544	}
 545
 546	if (!found) {
 547		new->domain = domain;
 548		new->bus = bus;
 549		new->devfn = devfn;
 550		list_add_tail(&new->slot_list, &pcistub_device_ids);
 551	}
 552
 553	spin_unlock_irqrestore(&device_ids_lock, flags);
 554
 555	if (found)
 556		kfree(new);
 557}
 558
 559static int pcistub_seize(struct pci_dev *dev,
 560			 struct pcistub_device_id *pci_dev_id)
 561{
 562	struct pcistub_device *psdev;
 563	unsigned long flags;
 564	int err = 0;
 565
 566	psdev = pcistub_device_alloc(dev);
 567	if (!psdev) {
 568		kfree(pci_dev_id);
 569		return -ENOMEM;
 570	}
 571
 572	spin_lock_irqsave(&pcistub_devices_lock, flags);
 573
 574	if (initialize_devices) {
 575		spin_unlock_irqrestore(&pcistub_devices_lock, flags);
 576
 577		/* don't want irqs disabled when calling pcistub_init_device */
 578		err = pcistub_init_device(psdev);
 579
 580		spin_lock_irqsave(&pcistub_devices_lock, flags);
 581
 582		if (!err)
 583			list_add(&psdev->dev_list, &pcistub_devices);
 584	} else {
 585		dev_dbg(&dev->dev, "deferring initialization\n");
 586		list_add(&psdev->dev_list, &seized_devices);
 587	}
 588
 589	spin_unlock_irqrestore(&pcistub_devices_lock, flags);
 590
 591	if (err) {
 592		kfree(pci_dev_id);
 593		pcistub_device_put(psdev);
 594	} else if (pci_dev_id)
 595		pcistub_device_id_add_list(pci_dev_id, pci_domain_nr(dev->bus),
 596					   dev->bus->number, dev->devfn);
 597
 598	return err;
 599}
 600
 601/* Called when 'bind'. This means we must _NOT_ call pci_reset_function or
 602 * other functions that take the sysfs lock. */
 603static int pcistub_probe(struct pci_dev *dev, const struct pci_device_id *id)
 604{
 605	int err = 0, match;
 606	struct pcistub_device_id *pci_dev_id = NULL;
 607
 608	dev_dbg(&dev->dev, "probing...\n");
 609
 610	match = pcistub_match(dev);
 611
 612	if ((dev->driver_override &&
 613	     !strcmp(dev->driver_override, PCISTUB_DRIVER_NAME)) ||
 614	    match) {
 615
 616		if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL
 617		    && dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
 618			dev_err(&dev->dev, "can't export pci devices that "
 619				"don't have a normal (0) or bridge (1) "
 620				"header type!\n");
 621			err = -ENODEV;
 622			goto out;
 623		}
 624
 625		if (!match) {
 626			pci_dev_id = kmalloc(sizeof(*pci_dev_id), GFP_KERNEL);
 627			if (!pci_dev_id) {
 628				err = -ENOMEM;
 629				goto out;
 630			}
 631		}
 632
 633		dev_info(&dev->dev, "seizing device\n");
 634		err = pcistub_seize(dev, pci_dev_id);
 635	} else
 636		/* Didn't find the device */
 637		err = -ENODEV;
 638
 639out:
 640	return err;
 641}
 642
 643/* Called when 'unbind'. This means we must _NOT_ call pci_reset_function or
 644 * other functions that take the sysfs lock. */
 645static void pcistub_remove(struct pci_dev *dev)
 646{
 647	struct pcistub_device *psdev, *found_psdev = NULL;
 648	unsigned long flags;
 649
 650	dev_dbg(&dev->dev, "removing\n");
 651
 652	spin_lock_irqsave(&pcistub_devices_lock, flags);
 653
 654	xen_pcibk_config_quirk_release(dev);
 655
 656	list_for_each_entry(psdev, &pcistub_devices, dev_list) {
 657		if (psdev->dev == dev) {
 658			found_psdev = psdev;
 659			break;
 660		}
 661	}
 662
 663	spin_unlock_irqrestore(&pcistub_devices_lock, flags);
 664
 665	if (found_psdev) {
 666		dev_dbg(&dev->dev, "found device to remove %s\n",
 667			found_psdev->pdev ? "- in-use" : "");
 668
 669		if (found_psdev->pdev) {
 670			int domid = xen_find_device_domain_owner(dev);
 671
 672			dev_warn(&dev->dev, "****** removing device %s while still in-use by domain %d! ******\n",
 673			       pci_name(found_psdev->dev), domid);
 674			dev_warn(&dev->dev, "****** driver domain may still access this device's i/o resources!\n");
 675			dev_warn(&dev->dev, "****** shutdown driver domain before binding device\n");
 676			dev_warn(&dev->dev, "****** to other drivers or domains\n");
 677
 678			/* N.B. This ends up calling pcistub_put_pci_dev which ends up
 679			 * doing the FLR. */
 680			xen_pcibk_release_pci_dev(found_psdev->pdev,
 681						found_psdev->dev,
 682						false /* caller holds the lock. */);
 683		}
 684
 685		spin_lock_irqsave(&pcistub_devices_lock, flags);
 686		list_del(&found_psdev->dev_list);
 687		spin_unlock_irqrestore(&pcistub_devices_lock, flags);
 688
 689		/* the final put for releasing from the list */
 690		pcistub_device_put(found_psdev);
 691	}
 692}
 693
 694static const struct pci_device_id pcistub_ids[] = {
 695	{
 696	 .vendor = PCI_ANY_ID,
 697	 .device = PCI_ANY_ID,
 698	 .subvendor = PCI_ANY_ID,
 699	 .subdevice = PCI_ANY_ID,
 700	 },
 701	{0,},
 702};
 703
 704#define PCI_NODENAME_MAX 40
 705static void kill_domain_by_device(struct pcistub_device *psdev)
 706{
 707	struct xenbus_transaction xbt;
 708	int err;
 709	char nodename[PCI_NODENAME_MAX];
 710
 711	BUG_ON(!psdev);
 712	snprintf(nodename, PCI_NODENAME_MAX, "/local/domain/0/backend/pci/%d/0",
 713		psdev->pdev->xdev->otherend_id);
 714
 715again:
 716	err = xenbus_transaction_start(&xbt);
 717	if (err) {
 718		dev_err(&psdev->dev->dev,
 719			"error %d when start xenbus transaction\n", err);
 720		return;
 721	}
 722	/*PV AER handlers will set this flag*/
 723	xenbus_printf(xbt, nodename, "aerState" , "aerfail");
 724	err = xenbus_transaction_end(xbt, 0);
 725	if (err) {
 726		if (err == -EAGAIN)
 727			goto again;
 728		dev_err(&psdev->dev->dev,
 729			"error %d when end xenbus transaction\n", err);
 730		return;
 731	}
 732}
 733
 734/* For each aer recovery step error_detected, mmio_enabled, etc, front_end and
 735 * backend need to have cooperation. In xen_pcibk, those steps will do similar
 736 * jobs: send service request and waiting for front_end response.
 737*/
 738static pci_ers_result_t common_process(struct pcistub_device *psdev,
 739				       pci_channel_state_t state, int aer_cmd,
 740				       pci_ers_result_t result)
 741{
 742	pci_ers_result_t res = result;
 743	struct xen_pcie_aer_op *aer_op;
 744	struct xen_pcibk_device *pdev = psdev->pdev;
 745	struct xen_pci_sharedinfo *sh_info = pdev->sh_info;
 746	int ret;
 747
 748	/*with PV AER drivers*/
 749	aer_op = &(sh_info->aer_op);
 750	aer_op->cmd = aer_cmd ;
 751	/*useful for error_detected callback*/
 752	aer_op->err = state;
 753	/*pcifront_end BDF*/
 754	ret = xen_pcibk_get_pcifront_dev(psdev->dev, psdev->pdev,
 755		&aer_op->domain, &aer_op->bus, &aer_op->devfn);
 756	if (!ret) {
 757		dev_err(&psdev->dev->dev, "failed to get pcifront device\n");
 758		return PCI_ERS_RESULT_NONE;
 759	}
 760	wmb();
 761
 762	dev_dbg(&psdev->dev->dev, "aer_op %x dom %x bus %x devfn %x\n",
 763			aer_cmd, aer_op->domain, aer_op->bus, aer_op->devfn);
 764	/*local flag to mark there's aer request, xen_pcibk callback will use
 765	* this flag to judge whether we need to check pci-front give aer
 766	* service ack signal
 767	*/
 768	set_bit(_PCIB_op_pending, (unsigned long *)&pdev->flags);
 769
 770	/*It is possible that a pcifront conf_read_write ops request invokes
 771	* the callback which cause the spurious execution of wake_up.
 772	* Yet it is harmless and better than a spinlock here
 773	*/
 774	set_bit(_XEN_PCIB_active,
 775		(unsigned long *)&sh_info->flags);
 776	wmb();
 777	notify_remote_via_irq(pdev->evtchn_irq);
 778
 779	/* Enable IRQ to signal "request done". */
 780	xen_pcibk_lateeoi(pdev, 0);
 781
 782	ret = wait_event_timeout(xen_pcibk_aer_wait_queue,
 783				 !(test_bit(_XEN_PCIB_active, (unsigned long *)
 784				 &sh_info->flags)), 300*HZ);
 785
 786	/* Enable IRQ for pcifront request if not already active. */
 787	if (!test_bit(_PDEVF_op_active, &pdev->flags))
 788		xen_pcibk_lateeoi(pdev, 0);
 789
 790	if (!ret) {
 791		if (test_bit(_XEN_PCIB_active,
 792			(unsigned long *)&sh_info->flags)) {
 793			dev_err(&psdev->dev->dev,
 794				"pcifront aer process not responding!\n");
 795			clear_bit(_XEN_PCIB_active,
 796			  (unsigned long *)&sh_info->flags);
 797			aer_op->err = PCI_ERS_RESULT_NONE;
 798			return res;
 799		}
 800	}
 801	clear_bit(_PCIB_op_pending, (unsigned long *)&pdev->flags);
 802
 803	res = (__force pci_ers_result_t)aer_op->err;
 804	return res;
 805}
 806
 807/*
 808* xen_pcibk_slot_reset: it will send the slot_reset request to  pcifront in case
 809* of the device driver could provide this service, and then wait for pcifront
 810* ack.
 811* @dev: pointer to PCI devices
 812* return value is used by aer_core do_recovery policy
 813*/
 814static pci_ers_result_t xen_pcibk_slot_reset(struct pci_dev *dev)
 815{
 816	struct pcistub_device *psdev;
 817	pci_ers_result_t result;
 818
 819	result = PCI_ERS_RESULT_RECOVERED;
 820	dev_dbg(&dev->dev, "xen_pcibk_slot_reset(bus:%x,devfn:%x)\n",
 821		dev->bus->number, dev->devfn);
 822
 823	down_write(&pcistub_sem);
 824	psdev = pcistub_device_find(pci_domain_nr(dev->bus),
 825				dev->bus->number,
 826				PCI_SLOT(dev->devfn),
 827				PCI_FUNC(dev->devfn));
 828
 829	if (!psdev || !psdev->pdev) {
 830		dev_err(&dev->dev, "device is not found/assigned\n");
 831		goto end;
 832	}
 833
 834	if (!psdev->pdev->sh_info) {
 835		dev_err(&dev->dev, "device is not connected or owned"
 836			" by HVM, kill it\n");
 837		kill_domain_by_device(psdev);
 838		goto end;
 839	}
 840
 841	if (!test_bit(_XEN_PCIB_AERHANDLER,
 842		(unsigned long *)&psdev->pdev->sh_info->flags)) {
 843		dev_err(&dev->dev,
 844			"guest with no AER driver should have been killed\n");
 845		goto end;
 846	}
 847	result = common_process(psdev, pci_channel_io_normal, XEN_PCI_OP_aer_slotreset, result);
 848
 849	if (result == PCI_ERS_RESULT_NONE ||
 850		result == PCI_ERS_RESULT_DISCONNECT) {
 851		dev_dbg(&dev->dev,
 852			"No AER slot_reset service or disconnected!\n");
 853		kill_domain_by_device(psdev);
 854	}
 855end:
 856	if (psdev)
 857		pcistub_device_put(psdev);
 858	up_write(&pcistub_sem);
 859	return result;
 860
 861}
 862
 863
 864/*xen_pcibk_mmio_enabled: it will send the mmio_enabled request to  pcifront
 865* in case of the device driver could provide this service, and then wait
 866* for pcifront ack
 867* @dev: pointer to PCI devices
 868* return value is used by aer_core do_recovery policy
 869*/
 870
 871static pci_ers_result_t xen_pcibk_mmio_enabled(struct pci_dev *dev)
 872{
 873	struct pcistub_device *psdev;
 874	pci_ers_result_t result;
 875
 876	result = PCI_ERS_RESULT_RECOVERED;
 877	dev_dbg(&dev->dev, "xen_pcibk_mmio_enabled(bus:%x,devfn:%x)\n",
 878		dev->bus->number, dev->devfn);
 879
 880	down_write(&pcistub_sem);
 881	psdev = pcistub_device_find(pci_domain_nr(dev->bus),
 882				dev->bus->number,
 883				PCI_SLOT(dev->devfn),
 884				PCI_FUNC(dev->devfn));
 885
 886	if (!psdev || !psdev->pdev) {
 887		dev_err(&dev->dev, "device is not found/assigned\n");
 888		goto end;
 889	}
 890
 891	if (!psdev->pdev->sh_info) {
 892		dev_err(&dev->dev, "device is not connected or owned"
 893			" by HVM, kill it\n");
 894		kill_domain_by_device(psdev);
 895		goto end;
 896	}
 897
 898	if (!test_bit(_XEN_PCIB_AERHANDLER,
 899		(unsigned long *)&psdev->pdev->sh_info->flags)) {
 900		dev_err(&dev->dev,
 901			"guest with no AER driver should have been killed\n");
 902		goto end;
 903	}
 904	result = common_process(psdev, pci_channel_io_normal, XEN_PCI_OP_aer_mmio, result);
 905
 906	if (result == PCI_ERS_RESULT_NONE ||
 907		result == PCI_ERS_RESULT_DISCONNECT) {
 908		dev_dbg(&dev->dev,
 909			"No AER mmio_enabled service or disconnected!\n");
 910		kill_domain_by_device(psdev);
 911	}
 912end:
 913	if (psdev)
 914		pcistub_device_put(psdev);
 915	up_write(&pcistub_sem);
 916	return result;
 917}
 918
 919/*xen_pcibk_error_detected: it will send the error_detected request to  pcifront
 920* in case of the device driver could provide this service, and then wait
 921* for pcifront ack.
 922* @dev: pointer to PCI devices
 923* @error: the current PCI connection state
 924* return value is used by aer_core do_recovery policy
 925*/
 926
 927static pci_ers_result_t xen_pcibk_error_detected(struct pci_dev *dev,
 928	pci_channel_state_t error)
 929{
 930	struct pcistub_device *psdev;
 931	pci_ers_result_t result;
 932
 933	result = PCI_ERS_RESULT_CAN_RECOVER;
 934	dev_dbg(&dev->dev, "xen_pcibk_error_detected(bus:%x,devfn:%x)\n",
 935		dev->bus->number, dev->devfn);
 936
 937	down_write(&pcistub_sem);
 938	psdev = pcistub_device_find(pci_domain_nr(dev->bus),
 939				dev->bus->number,
 940				PCI_SLOT(dev->devfn),
 941				PCI_FUNC(dev->devfn));
 942
 943	if (!psdev || !psdev->pdev) {
 944		dev_err(&dev->dev, "device is not found/assigned\n");
 945		goto end;
 946	}
 947
 948	if (!psdev->pdev->sh_info) {
 949		dev_err(&dev->dev, "device is not connected or owned"
 950			" by HVM, kill it\n");
 951		kill_domain_by_device(psdev);
 952		goto end;
 953	}
 954
 955	/*Guest owns the device yet no aer handler regiested, kill guest*/
 956	if (!test_bit(_XEN_PCIB_AERHANDLER,
 957		(unsigned long *)&psdev->pdev->sh_info->flags)) {
 958		dev_dbg(&dev->dev, "guest may have no aer driver, kill it\n");
 959		kill_domain_by_device(psdev);
 960		goto end;
 961	}
 962	result = common_process(psdev, error, XEN_PCI_OP_aer_detected, result);
 963
 964	if (result == PCI_ERS_RESULT_NONE ||
 965		result == PCI_ERS_RESULT_DISCONNECT) {
 966		dev_dbg(&dev->dev,
 967			"No AER error_detected service or disconnected!\n");
 968		kill_domain_by_device(psdev);
 969	}
 970end:
 971	if (psdev)
 972		pcistub_device_put(psdev);
 973	up_write(&pcistub_sem);
 974	return result;
 975}
 976
 977/*xen_pcibk_error_resume: it will send the error_resume request to  pcifront
 978* in case of the device driver could provide this service, and then wait
 979* for pcifront ack.
 980* @dev: pointer to PCI devices
 981*/
 982
 983static void xen_pcibk_error_resume(struct pci_dev *dev)
 984{
 985	struct pcistub_device *psdev;
 986
 987	dev_dbg(&dev->dev, "xen_pcibk_error_resume(bus:%x,devfn:%x)\n",
 988		dev->bus->number, dev->devfn);
 989
 990	down_write(&pcistub_sem);
 991	psdev = pcistub_device_find(pci_domain_nr(dev->bus),
 992				dev->bus->number,
 993				PCI_SLOT(dev->devfn),
 994				PCI_FUNC(dev->devfn));
 995
 996	if (!psdev || !psdev->pdev) {
 997		dev_err(&dev->dev, "device is not found/assigned\n");
 998		goto end;
 999	}
1000
1001	if (!psdev->pdev->sh_info) {
1002		dev_err(&dev->dev, "device is not connected or owned"
1003			" by HVM, kill it\n");
1004		kill_domain_by_device(psdev);
1005		goto end;
1006	}
1007
1008	if (!test_bit(_XEN_PCIB_AERHANDLER,
1009		(unsigned long *)&psdev->pdev->sh_info->flags)) {
1010		dev_err(&dev->dev,
1011			"guest with no AER driver should have been killed\n");
1012		kill_domain_by_device(psdev);
1013		goto end;
1014	}
1015	common_process(psdev, pci_channel_io_normal, XEN_PCI_OP_aer_resume,
1016		       PCI_ERS_RESULT_RECOVERED);
1017end:
1018	if (psdev)
1019		pcistub_device_put(psdev);
1020	up_write(&pcistub_sem);
1021	return;
1022}
1023
1024/*add xen_pcibk AER handling*/
1025static const struct pci_error_handlers xen_pcibk_error_handler = {
1026	.error_detected = xen_pcibk_error_detected,
1027	.mmio_enabled = xen_pcibk_mmio_enabled,
1028	.slot_reset = xen_pcibk_slot_reset,
1029	.resume = xen_pcibk_error_resume,
1030};
1031
1032/*
1033 * Note: There is no MODULE_DEVICE_TABLE entry here because this isn't
1034 * for a normal device. I don't want it to be loaded automatically.
1035 */
1036
1037static struct pci_driver xen_pcibk_pci_driver = {
1038	/* The name should be xen_pciback, but until the tools are updated
1039	 * we will keep it as pciback. */
1040	.name = PCISTUB_DRIVER_NAME,
1041	.id_table = pcistub_ids,
1042	.probe = pcistub_probe,
1043	.remove = pcistub_remove,
1044	.err_handler = &xen_pcibk_error_handler,
1045};
1046
1047static inline int str_to_slot(const char *buf, int *domain, int *bus,
1048			      int *slot, int *func)
1049{
1050	int parsed = 0;
1051
1052	switch (sscanf(buf, " %x:%x:%x.%x %n", domain, bus, slot, func,
1053		       &parsed)) {
1054	case 3:
1055		*func = -1;
1056		sscanf(buf, " %x:%x:%x.* %n", domain, bus, slot, &parsed);
1057		break;
1058	case 2:
1059		*slot = *func = -1;
1060		sscanf(buf, " %x:%x:*.* %n", domain, bus, &parsed);
1061		break;
1062	}
1063	if (parsed && !buf[parsed])
1064		return 0;
1065
1066	/* try again without domain */
1067	*domain = 0;
1068	switch (sscanf(buf, " %x:%x.%x %n", bus, slot, func, &parsed)) {
1069	case 2:
1070		*func = -1;
1071		sscanf(buf, " %x:%x.* %n", bus, slot, &parsed);
1072		break;
1073	case 1:
1074		*slot = *func = -1;
1075		sscanf(buf, " %x:*.* %n", bus, &parsed);
1076		break;
1077	}
1078	if (parsed && !buf[parsed])
1079		return 0;
1080
1081	return -EINVAL;
1082}
1083
1084static inline int str_to_quirk(const char *buf, int *domain, int *bus, int
1085			       *slot, int *func, int *reg, int *size, int *mask)
1086{
1087	int parsed = 0;
1088
1089	sscanf(buf, " %x:%x:%x.%x-%x:%x:%x %n", domain, bus, slot, func,
1090	       reg, size, mask, &parsed);
1091	if (parsed && !buf[parsed])
1092		return 0;
1093
1094	/* try again without domain */
1095	*domain = 0;
1096	sscanf(buf, " %x:%x.%x-%x:%x:%x %n", bus, slot, func, reg, size,
1097	       mask, &parsed);
1098	if (parsed && !buf[parsed])
1099		return 0;
1100
1101	return -EINVAL;
1102}
1103
1104static int pcistub_device_id_add(int domain, int bus, int slot, int func)
1105{
1106	struct pcistub_device_id *pci_dev_id;
1107	int rc = 0, devfn = PCI_DEVFN(slot, func);
1108
1109	if (slot < 0) {
1110		for (slot = 0; !rc && slot < 32; ++slot)
1111			rc = pcistub_device_id_add(domain, bus, slot, func);
1112		return rc;
1113	}
1114
1115	if (func < 0) {
1116		for (func = 0; !rc && func < 8; ++func)
1117			rc = pcistub_device_id_add(domain, bus, slot, func);
1118		return rc;
1119	}
1120
1121	if ((
1122#if !defined(MODULE) /* pci_domains_supported is not being exported */ \
1123    || !defined(CONFIG_PCI_DOMAINS)
1124	     !pci_domains_supported ? domain :
1125#endif
1126	     domain < 0 || domain > 0xffff)
1127	    || bus < 0 || bus > 0xff
1128	    || PCI_SLOT(devfn) != slot
1129	    || PCI_FUNC(devfn) != func)
1130		return -EINVAL;
1131
1132	pci_dev_id = kmalloc(sizeof(*pci_dev_id), GFP_KERNEL);
1133	if (!pci_dev_id)
1134		return -ENOMEM;
1135
1136	pr_debug("wants to seize %04x:%02x:%02x.%d\n",
1137		 domain, bus, slot, func);
1138
1139	pcistub_device_id_add_list(pci_dev_id, domain, bus, devfn);
1140
1141	return 0;
1142}
1143
1144static int pcistub_device_id_remove(int domain, int bus, int slot, int func)
1145{
1146	struct pcistub_device_id *pci_dev_id, *t;
1147	int err = -ENOENT;
1148	unsigned long flags;
1149
1150	spin_lock_irqsave(&device_ids_lock, flags);
1151	list_for_each_entry_safe(pci_dev_id, t, &pcistub_device_ids,
1152				 slot_list) {
1153		if (pci_dev_id->domain == domain && pci_dev_id->bus == bus
1154		    && (slot < 0 || PCI_SLOT(pci_dev_id->devfn) == slot)
1155		    && (func < 0 || PCI_FUNC(pci_dev_id->devfn) == func)) {
1156			/* Don't break; here because it's possible the same
1157			 * slot could be in the list more than once
1158			 */
1159			list_del(&pci_dev_id->slot_list);
1160			kfree(pci_dev_id);
1161
1162			err = 0;
1163
1164			pr_debug("removed %04x:%02x:%02x.%d from seize list\n",
1165				 domain, bus, slot, func);
1166		}
1167	}
1168	spin_unlock_irqrestore(&device_ids_lock, flags);
1169
1170	return err;
1171}
1172
1173static int pcistub_reg_add(int domain, int bus, int slot, int func,
1174			   unsigned int reg, unsigned int size,
1175			   unsigned int mask)
1176{
1177	int err = 0;
1178	struct pcistub_device *psdev;
1179	struct pci_dev *dev;
1180	struct config_field *field;
1181
1182	if (reg > 0xfff || (size < 4 && (mask >> (size * 8))))
1183		return -EINVAL;
1184
1185	psdev = pcistub_device_find(domain, bus, slot, func);
1186	if (!psdev) {
1187		err = -ENODEV;
1188		goto out;
1189	}
1190	dev = psdev->dev;
1191
1192	field = kzalloc(sizeof(*field), GFP_KERNEL);
1193	if (!field) {
1194		err = -ENOMEM;
1195		goto out;
1196	}
1197
1198	field->offset = reg;
1199	field->size = size;
1200	field->mask = mask;
1201	field->init = NULL;
1202	field->reset = NULL;
1203	field->release = NULL;
1204	field->clean = xen_pcibk_config_field_free;
1205
1206	err = xen_pcibk_config_quirks_add_field(dev, field);
1207	if (err)
1208		kfree(field);
1209out:
1210	if (psdev)
1211		pcistub_device_put(psdev);
1212	return err;
1213}
1214
1215static ssize_t new_slot_store(struct device_driver *drv, const char *buf,
1216			      size_t count)
1217{
1218	int domain, bus, slot, func;
1219	int err;
1220
1221	err = str_to_slot(buf, &domain, &bus, &slot, &func);
1222	if (err)
1223		goto out;
1224
1225	err = pcistub_device_id_add(domain, bus, slot, func);
1226
1227out:
1228	if (!err)
1229		err = count;
1230	return err;
1231}
1232static DRIVER_ATTR_WO(new_slot);
1233
1234static ssize_t remove_slot_store(struct device_driver *drv, const char *buf,
1235				 size_t count)
1236{
1237	int domain, bus, slot, func;
1238	int err;
1239
1240	err = str_to_slot(buf, &domain, &bus, &slot, &func);
1241	if (err)
1242		goto out;
1243
1244	err = pcistub_device_id_remove(domain, bus, slot, func);
1245
1246out:
1247	if (!err)
1248		err = count;
1249	return err;
1250}
1251static DRIVER_ATTR_WO(remove_slot);
1252
1253static ssize_t slots_show(struct device_driver *drv, char *buf)
1254{
1255	struct pcistub_device_id *pci_dev_id;
1256	size_t count = 0;
1257	unsigned long flags;
1258
1259	spin_lock_irqsave(&device_ids_lock, flags);
1260	list_for_each_entry(pci_dev_id, &pcistub_device_ids, slot_list) {
1261		if (count >= PAGE_SIZE)
1262			break;
1263
1264		count += sysfs_emit_at(buf, count,
1265				   "%04x:%02x:%02x.%d\n",
1266				   pci_dev_id->domain, pci_dev_id->bus,
1267				   PCI_SLOT(pci_dev_id->devfn),
1268				   PCI_FUNC(pci_dev_id->devfn));
1269	}
1270	spin_unlock_irqrestore(&device_ids_lock, flags);
1271
1272	return count;
1273}
1274static DRIVER_ATTR_RO(slots);
1275
1276static ssize_t irq_handlers_show(struct device_driver *drv, char *buf)
1277{
1278	struct pcistub_device *psdev;
1279	struct xen_pcibk_dev_data *dev_data;
1280	size_t count = 0;
1281	unsigned long flags;
1282
1283	spin_lock_irqsave(&pcistub_devices_lock, flags);
1284	list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1285		if (count >= PAGE_SIZE)
1286			break;
1287		if (!psdev->dev)
1288			continue;
1289		dev_data = pci_get_drvdata(psdev->dev);
1290		if (!dev_data)
1291			continue;
1292		count +=
1293		    sysfs_emit_at(buf, count,
1294			      "%s:%s:%sing:%ld\n",
1295			      pci_name(psdev->dev),
1296			      dev_data->isr_on ? "on" : "off",
1297			      dev_data->ack_intr ? "ack" : "not ack",
1298			      dev_data->handled);
1299	}
1300	spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1301	return count;
1302}
1303static DRIVER_ATTR_RO(irq_handlers);
1304
1305static ssize_t irq_handler_state_store(struct device_driver *drv,
1306				       const char *buf, size_t count)
1307{
1308	struct pcistub_device *psdev;
1309	struct xen_pcibk_dev_data *dev_data;
1310	int domain, bus, slot, func;
1311	int err;
1312
1313	err = str_to_slot(buf, &domain, &bus, &slot, &func);
1314	if (err)
1315		return err;
1316
1317	psdev = pcistub_device_find(domain, bus, slot, func);
1318	if (!psdev) {
1319		err = -ENOENT;
1320		goto out;
1321	}
1322
1323	dev_data = pci_get_drvdata(psdev->dev);
1324	if (!dev_data) {
1325		err = -ENOENT;
1326		goto out;
1327	}
1328
1329	dev_dbg(&psdev->dev->dev, "%s fake irq handler: %d->%d\n",
1330		dev_data->irq_name, dev_data->isr_on,
1331		!dev_data->isr_on);
1332
1333	dev_data->isr_on = !(dev_data->isr_on);
1334	if (dev_data->isr_on)
1335		dev_data->ack_intr = 1;
1336out:
1337	if (psdev)
1338		pcistub_device_put(psdev);
1339	if (!err)
1340		err = count;
1341	return err;
1342}
1343static DRIVER_ATTR_WO(irq_handler_state);
1344
1345static ssize_t quirks_store(struct device_driver *drv, const char *buf,
1346			    size_t count)
1347{
1348	int domain, bus, slot, func, reg, size, mask;
1349	int err;
1350
1351	err = str_to_quirk(buf, &domain, &bus, &slot, &func, &reg, &size,
1352			   &mask);
1353	if (err)
1354		goto out;
1355
1356	err = pcistub_reg_add(domain, bus, slot, func, reg, size, mask);
1357
1358out:
1359	if (!err)
1360		err = count;
1361	return err;
1362}
1363
1364static ssize_t quirks_show(struct device_driver *drv, char *buf)
1365{
1366	int count = 0;
1367	unsigned long flags;
1368	struct xen_pcibk_config_quirk *quirk;
1369	struct xen_pcibk_dev_data *dev_data;
1370	const struct config_field *field;
1371	const struct config_field_entry *cfg_entry;
1372
1373	spin_lock_irqsave(&device_ids_lock, flags);
1374	list_for_each_entry(quirk, &xen_pcibk_quirks, quirks_list) {
1375		if (count >= PAGE_SIZE)
1376			goto out;
1377
1378		count += sysfs_emit_at(buf, count,
1379				   "%02x:%02x.%01x\n\t%04x:%04x:%04x:%04x\n",
1380				   quirk->pdev->bus->number,
1381				   PCI_SLOT(quirk->pdev->devfn),
1382				   PCI_FUNC(quirk->pdev->devfn),
1383				   quirk->devid.vendor, quirk->devid.device,
1384				   quirk->devid.subvendor,
1385				   quirk->devid.subdevice);
1386
1387		dev_data = pci_get_drvdata(quirk->pdev);
1388
1389		list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
1390			field = cfg_entry->field;
1391			if (count >= PAGE_SIZE)
1392				goto out;
1393
1394			count += sysfs_emit_at(buf, count,
1395					   "\t\t%08x:%01x:%08x\n",
1396					   cfg_entry->base_offset +
1397					   field->offset, field->size,
1398					   field->mask);
1399		}
1400	}
1401
1402out:
1403	spin_unlock_irqrestore(&device_ids_lock, flags);
1404
1405	return count;
1406}
1407static DRIVER_ATTR_RW(quirks);
1408
1409static ssize_t permissive_store(struct device_driver *drv, const char *buf,
1410				size_t count)
1411{
1412	int domain, bus, slot, func;
1413	int err;
1414	struct pcistub_device *psdev;
1415	struct xen_pcibk_dev_data *dev_data;
1416
1417	err = str_to_slot(buf, &domain, &bus, &slot, &func);
1418	if (err)
1419		goto out;
1420
1421	psdev = pcistub_device_find(domain, bus, slot, func);
1422	if (!psdev) {
1423		err = -ENODEV;
1424		goto out;
1425	}
1426
1427	dev_data = pci_get_drvdata(psdev->dev);
1428	/* the driver data for a device should never be null at this point */
1429	if (!dev_data) {
1430		err = -ENXIO;
1431		goto release;
1432	}
1433	if (!dev_data->permissive) {
1434		dev_data->permissive = 1;
1435		/* Let user know that what they're doing could be unsafe */
1436		dev_warn(&psdev->dev->dev, "enabling permissive mode "
1437			 "configuration space accesses!\n");
1438		dev_warn(&psdev->dev->dev,
1439			 "permissive mode is potentially unsafe!\n");
1440	}
1441release:
1442	pcistub_device_put(psdev);
1443out:
1444	if (!err)
1445		err = count;
1446	return err;
1447}
1448
1449static ssize_t permissive_show(struct device_driver *drv, char *buf)
1450{
1451	struct pcistub_device *psdev;
1452	struct xen_pcibk_dev_data *dev_data;
1453	size_t count = 0;
1454	unsigned long flags;
1455	spin_lock_irqsave(&pcistub_devices_lock, flags);
1456	list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1457		if (count >= PAGE_SIZE)
1458			break;
1459		if (!psdev->dev)
1460			continue;
1461		dev_data = pci_get_drvdata(psdev->dev);
1462		if (!dev_data || !dev_data->permissive)
1463			continue;
1464		count +=
1465		    sysfs_emit_at(buf, count, "%s\n",
1466			      pci_name(psdev->dev));
1467	}
1468	spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1469	return count;
1470}
1471static DRIVER_ATTR_RW(permissive);
1472
1473static ssize_t allow_interrupt_control_store(struct device_driver *drv,
1474					     const char *buf, size_t count)
1475{
1476	int domain, bus, slot, func;
1477	int err;
1478	struct pcistub_device *psdev;
1479	struct xen_pcibk_dev_data *dev_data;
1480
1481	err = str_to_slot(buf, &domain, &bus, &slot, &func);
1482	if (err)
1483		goto out;
1484
1485	psdev = pcistub_device_find(domain, bus, slot, func);
1486	if (!psdev) {
1487		err = -ENODEV;
1488		goto out;
1489	}
1490
1491	dev_data = pci_get_drvdata(psdev->dev);
1492	/* the driver data for a device should never be null at this point */
1493	if (!dev_data) {
1494		err = -ENXIO;
1495		goto release;
1496	}
1497	dev_data->allow_interrupt_control = 1;
1498release:
1499	pcistub_device_put(psdev);
1500out:
1501	if (!err)
1502		err = count;
1503	return err;
1504}
1505
1506static ssize_t allow_interrupt_control_show(struct device_driver *drv,
1507					    char *buf)
1508{
1509	struct pcistub_device *psdev;
1510	struct xen_pcibk_dev_data *dev_data;
1511	size_t count = 0;
1512	unsigned long flags;
1513
1514	spin_lock_irqsave(&pcistub_devices_lock, flags);
1515	list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1516		if (count >= PAGE_SIZE)
1517			break;
1518		if (!psdev->dev)
1519			continue;
1520		dev_data = pci_get_drvdata(psdev->dev);
1521		if (!dev_data || !dev_data->allow_interrupt_control)
1522			continue;
1523		count +=
1524		    sysfs_emit_at(buf, count, "%s\n",
1525			      pci_name(psdev->dev));
1526	}
1527	spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1528	return count;
1529}
1530static DRIVER_ATTR_RW(allow_interrupt_control);
1531
1532static void pcistub_exit(void)
1533{
1534	driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_new_slot);
1535	driver_remove_file(&xen_pcibk_pci_driver.driver,
1536			   &driver_attr_remove_slot);
1537	driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_slots);
1538	driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_quirks);
1539	driver_remove_file(&xen_pcibk_pci_driver.driver,
1540			   &driver_attr_permissive);
1541	driver_remove_file(&xen_pcibk_pci_driver.driver,
1542			   &driver_attr_allow_interrupt_control);
1543	driver_remove_file(&xen_pcibk_pci_driver.driver,
1544			   &driver_attr_irq_handlers);
1545	driver_remove_file(&xen_pcibk_pci_driver.driver,
1546			   &driver_attr_irq_handler_state);
1547	pci_unregister_driver(&xen_pcibk_pci_driver);
1548}
1549
1550static int __init pcistub_init(void)
1551{
1552	int pos = 0;
1553	int err = 0;
1554	int domain, bus, slot, func;
1555	int parsed;
1556
1557	if (pci_devs_to_hide && *pci_devs_to_hide) {
1558		do {
1559			parsed = 0;
1560
1561			err = sscanf(pci_devs_to_hide + pos,
1562				     " (%x:%x:%x.%x) %n",
1563				     &domain, &bus, &slot, &func, &parsed);
1564			switch (err) {
1565			case 3:
1566				func = -1;
1567				sscanf(pci_devs_to_hide + pos,
1568				       " (%x:%x:%x.*) %n",
1569				       &domain, &bus, &slot, &parsed);
1570				break;
1571			case 2:
1572				slot = func = -1;
1573				sscanf(pci_devs_to_hide + pos,
1574				       " (%x:%x:*.*) %n",
1575				       &domain, &bus, &parsed);
1576				break;
1577			}
1578
1579			if (!parsed) {
1580				domain = 0;
1581				err = sscanf(pci_devs_to_hide + pos,
1582					     " (%x:%x.%x) %n",
1583					     &bus, &slot, &func, &parsed);
1584				switch (err) {
1585				case 2:
1586					func = -1;
1587					sscanf(pci_devs_to_hide + pos,
1588					       " (%x:%x.*) %n",
1589					       &bus, &slot, &parsed);
1590					break;
1591				case 1:
1592					slot = func = -1;
1593					sscanf(pci_devs_to_hide + pos,
1594					       " (%x:*.*) %n",
1595					       &bus, &parsed);
1596					break;
1597				}
1598			}
1599
1600			if (parsed <= 0)
1601				goto parse_error;
1602
1603			err = pcistub_device_id_add(domain, bus, slot, func);
1604			if (err)
1605				goto out;
1606
1607			pos += parsed;
1608		} while (pci_devs_to_hide[pos]);
1609	}
1610
1611	/* If we're the first PCI Device Driver to register, we're the
1612	 * first one to get offered PCI devices as they become
1613	 * available (and thus we can be the first to grab them)
1614	 */
1615	err = pci_register_driver(&xen_pcibk_pci_driver);
1616	if (err < 0)
1617		goto out;
1618
1619	err = driver_create_file(&xen_pcibk_pci_driver.driver,
1620				 &driver_attr_new_slot);
1621	if (!err)
1622		err = driver_create_file(&xen_pcibk_pci_driver.driver,
1623					 &driver_attr_remove_slot);
1624	if (!err)
1625		err = driver_create_file(&xen_pcibk_pci_driver.driver,
1626					 &driver_attr_slots);
1627	if (!err)
1628		err = driver_create_file(&xen_pcibk_pci_driver.driver,
1629					 &driver_attr_quirks);
1630	if (!err)
1631		err = driver_create_file(&xen_pcibk_pci_driver.driver,
1632					 &driver_attr_permissive);
1633	if (!err)
1634		err = driver_create_file(&xen_pcibk_pci_driver.driver,
1635					 &driver_attr_allow_interrupt_control);
1636
1637	if (!err)
1638		err = driver_create_file(&xen_pcibk_pci_driver.driver,
1639					 &driver_attr_irq_handlers);
1640	if (!err)
1641		err = driver_create_file(&xen_pcibk_pci_driver.driver,
1642					&driver_attr_irq_handler_state);
1643	if (err)
1644		pcistub_exit();
1645
1646out:
1647	return err;
1648
1649parse_error:
1650	pr_err("Error parsing pci_devs_to_hide at \"%s\"\n",
1651	       pci_devs_to_hide + pos);
1652	return -EINVAL;
1653}
1654
1655#ifndef MODULE
1656/*
1657 * fs_initcall happens before device_initcall
1658 * so xen_pcibk *should* get called first (b/c we
1659 * want to suck up any device before other drivers
1660 * get a chance by being the first pci device
1661 * driver to register)
1662 */
1663fs_initcall(pcistub_init);
1664#endif
1665
1666#ifdef CONFIG_PCI_IOV
1667static struct pcistub_device *find_vfs(const struct pci_dev *pdev)
1668{
1669	struct pcistub_device *psdev = NULL;
1670	unsigned long flags;
1671	bool found = false;
1672
1673	spin_lock_irqsave(&pcistub_devices_lock, flags);
1674	list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1675		if (!psdev->pdev && psdev->dev != pdev
1676		    && pci_physfn(psdev->dev) == pdev) {
1677			found = true;
1678			break;
1679		}
1680	}
1681	spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1682	if (found)
1683		return psdev;
1684	return NULL;
1685}
1686
1687static int pci_stub_notifier(struct notifier_block *nb,
1688			     unsigned long action, void *data)
1689{
1690	struct device *dev = data;
1691	const struct pci_dev *pdev = to_pci_dev(dev);
1692
1693	if (action != BUS_NOTIFY_UNBIND_DRIVER)
1694		return NOTIFY_DONE;
1695
1696	if (!pdev->is_physfn)
1697		return NOTIFY_DONE;
1698
1699	for (;;) {
1700		struct pcistub_device *psdev = find_vfs(pdev);
1701		if (!psdev)
1702			break;
1703		device_release_driver(&psdev->dev->dev);
1704	}
1705	return NOTIFY_DONE;
1706}
1707
1708static struct notifier_block pci_stub_nb = {
1709	.notifier_call = pci_stub_notifier,
1710};
1711#endif
1712
1713static int __init xen_pcibk_init(void)
1714{
1715	int err;
1716
1717	if (!xen_initial_domain())
1718		return -ENODEV;
1719
1720	err = xen_pcibk_config_init();
1721	if (err)
1722		return err;
1723
1724#ifdef MODULE
1725	err = pcistub_init();
1726	if (err < 0)
1727		return err;
1728#endif
1729
1730	pcistub_init_devices_late();
1731	err = xen_pcibk_xenbus_register();
1732	if (err)
1733		pcistub_exit();
1734#ifdef CONFIG_PCI_IOV
1735	else
1736		bus_register_notifier(&pci_bus_type, &pci_stub_nb);
1737#endif
1738
1739#ifdef CONFIG_XEN_ACPI
1740	xen_acpi_register_get_gsi_func(pcistub_get_gsi_from_sbdf);
1741#endif
1742
1743	return err;
1744}
1745
1746static void __exit xen_pcibk_cleanup(void)
1747{
1748#ifdef CONFIG_XEN_ACPI
1749	xen_acpi_register_get_gsi_func(NULL);
1750#endif
1751
1752#ifdef CONFIG_PCI_IOV
1753	bus_unregister_notifier(&pci_bus_type, &pci_stub_nb);
1754#endif
1755	xen_pcibk_xenbus_unregister();
1756	pcistub_exit();
1757}
1758
1759module_init(xen_pcibk_init);
1760module_exit(xen_pcibk_cleanup);
1761
1762MODULE_DESCRIPTION("Xen PCI-device stub driver");
1763MODULE_LICENSE("Dual BSD/GPL");
1764MODULE_ALIAS("xen-backend:pci");
~/ Read-only file preview.