brintos

brintos / linux-shallow public Read only

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