staticchar *pci_devs_to_hide;
wait_queue_head_t xen_pcibk_aer_wait_queue; /*Add sem for sync AER handling and xen_pcibk remove/reconfigue ops, * We want to avoid in middle of AER ops, xen_pcibk devices is being removed
*/ static DECLARE_RWSEM(pcistub_sem);
module_param_named(hide, pci_devs_to_hide, charp, 0444);
struct pci_dev *dev; struct xen_pcibk_device *pdev;/* non-NULL if struct pci_dev is in use */ #ifdef CONFIG_XEN_ACPI int gsi; #endif
};
/* Access to pcistub_devices & seized_devices lists and the initialize_devices * flag must be locked with pcistub_devices_lock
*/ static DEFINE_SPINLOCK(pcistub_devices_lock); static LIST_HEAD(pcistub_devices);
/* wait for device_initcall before initializing our devices * (see pcistub_init_devices_late)
*/ staticint initialize_devices; static LIST_HEAD(seized_devices);
/* Call the reset function which does not take lock as this * is called from "unbind" which takes a device_lock mutex.
*/
pcistub_reset_device_state(dev); if (dev_data &&
pci_load_and_free_saved_state(dev, &dev_data->pci_saved_state))
dev_info(&dev->dev, "Could not reload PCI state\n"); else
pci_restore_state(dev);
/* * Called when: * - XenBus state has been reconfigure (pci unplug). See xen_pcibk_remove_device * - XenBus state has been disconnected (guest shutdown). See xen_pcibk_xenbus_remove * - 'echo BDF > unbind' on pciback module with no guest attached. See pcistub_remove * - 'echo BDF > unbind' with a guest still using it. See pcistub_remove * * As such we have to be careful. * * To make this easier, the caller has to hold the device lock.
*/ void pcistub_put_pci_dev(struct pci_dev *dev)
{ struct pcistub_device *psdev, *found_psdev = NULL; unsignedlong flags; struct xen_pcibk_dev_data *dev_data; int ret;
spin_unlock_irqrestore(&pcistub_devices_lock, flags); if (WARN_ON(!found_psdev)) return;
/*hold this lock for avoiding breaking link between * pcistub and xen_pcibk when AER is in processing
*/
down_write(&pcistub_sem); /* Cleanup our device * (so it's ready for the next domain)
*/
device_lock_assert(&dev->dev);
pcistub_reset_device_state(dev);
dev_data = pci_get_drvdata(dev);
ret = pci_load_saved_state(dev, dev_data->pci_saved_state); if (!ret) { /* * The usual sequence is pci_save_state & pci_restore_state * but the guest might have messed the configuration space up. * Use the initial version (when device was bound to us).
*/
pci_restore_state(dev);
} else
dev_info(&dev->dev, "Could not reload PCI state\n"); /* This disables the device. */
xen_pcibk_reset_device(dev);
/* And cleanup up our emulated fields. */
xen_pcibk_config_reset_dev(dev);
xen_pcibk_config_free_dyn_fields(dev);
staticint pcistub_match_one(struct pci_dev *dev, struct pcistub_device_id *pdev_id)
{ /* Match the specified device by domain, bus, slot, func and also if * any of the device's parent bridges match.
*/ for (; dev != NULL; dev = dev->bus->self) { if (pci_domain_nr(dev->bus) == pdev_id->domain
&& dev->bus->number == pdev_id->bus
&& dev->devfn == pdev_id->devfn) return 1;
/* Sometimes topmost bridge links to itself. */ if (dev == dev->bus->self) break;
}
return 0;
}
staticint pcistub_match(struct pci_dev *dev)
{ struct pcistub_device_id *pdev_id; unsignedlong flags; int found = 0;
spin_lock_irqsave(&device_ids_lock, flags);
list_for_each_entry(pdev_id, &pcistub_device_ids, slot_list) { if (pcistub_match_one(dev, pdev_id)) {
found = 1; break;
}
}
spin_unlock_irqrestore(&device_ids_lock, flags);
/* The PCI backend is not intended to be a module (or to work with * removable PCI devices (yet). If it were, xen_pcibk_config_free() * would need to be called somewhere to free the memory allocated * here and then to call kfree(pci_get_drvdata(psdev->dev)).
*/
dev_data = kzalloc(sizeof(*dev_data) + strlen(DRV_NAME "[]")
+ strlen(pci_name(dev)) + 1, GFP_KERNEL); if (!dev_data) {
err = -ENOMEM; goto out;
}
pci_set_drvdata(dev, dev_data);
/* * Setup name for fake IRQ handler. It will only be enabled * once the device is turned on by the guest.
*/
sprintf(dev_data->irq_name, DRV_NAME "[%s]", pci_name(dev));
dev_dbg(&dev->dev, "initializing config\n");
init_waitqueue_head(&xen_pcibk_aer_wait_queue);
err = xen_pcibk_config_init_dev(dev); if (err) goto out;
/* HACK: Force device (& ACPI) to determine what IRQ it's on - we * must do this here because pcibios_enable_device may specify * the pci device's true irq (and possibly its other resources) * if they differ from what's in the configuration space. * This makes the assumption that the device's resources won't * change after this point (otherwise this code may break!)
*/
dev_dbg(&dev->dev, "enabling device\n");
err = pci_enable_device(dev); if (err) goto config_release;
/* We need the device active to save the state. */
dev_dbg(&dev->dev, "save state of device\n");
pci_save_state(dev);
dev_data->pci_saved_state = pci_store_saved_state(dev); if (!dev_data->pci_saved_state)
dev_err(&dev->dev, "Could not store PCI conf saved state!\n"); else {
dev_dbg(&dev->dev, "resetting (FLR, D3, etc) the device\n");
err = pcistub_reset_device_state(dev); if (err) goto config_release;
pci_restore_state(dev);
}
#ifdef CONFIG_XEN_ACPI if (xen_initial_domain() && xen_pvh_domain()) {
err = xen_acpi_get_gsi_info(dev, &gsi, &trigger, &polarity); if (err) {
dev_err(&dev->dev, "Fail to get gsi info!\n"); goto config_release;
}
err = xen_pvh_setup_gsi(gsi, trigger, polarity); if (err) goto config_release;
psdev->gsi = gsi;
} #endif
/* Now disable the device (this also ensures some private device * data is setup before we export)
*/
dev_dbg(&dev->dev, "reset device\n");
xen_pcibk_reset_device(dev);
/* * Because some initialization still happens on * devices during fs_initcall, we need to defer * full initialization of our devices until * device_initcall.
*/ staticint __init pcistub_init_devices_late(void)
{ struct pcistub_device *psdev; unsignedlong flags; int err = 0;
spin_lock_irqsave(&pcistub_devices_lock, flags);
while (!list_empty(&seized_devices)) {
psdev = container_of(seized_devices.next, struct pcistub_device, dev_list);
list_del(&psdev->dev_list);
staticvoid pcistub_device_id_add_list(struct pcistub_device_id *new, int domain, int bus, unsignedint devfn)
{ struct pcistub_device_id *pci_dev_id; unsignedlong flags; int found = 0;
spin_lock_irqsave(&device_ids_lock, flags);
list_for_each_entry(pci_dev_id, &pcistub_device_ids, slot_list) { if (pci_dev_id->domain == domain && pci_dev_id->bus == bus &&
pci_dev_id->devfn == devfn) {
found = 1; break;
}
}
/* Called when 'bind'. This means we must _NOT_ call pci_reset_function or
* other functions that take the sysfs lock. */ staticint pcistub_probe(struct pci_dev *dev, conststruct pci_device_id *id)
{ int err = 0, match; struct pcistub_device_id *pci_dev_id = NULL;
dev_dbg(&dev->dev, "probing...\n");
match = pcistub_match(dev);
if ((dev->driver_override &&
!strcmp(dev->driver_override, PCISTUB_DRIVER_NAME)) ||
match) {
if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL
&& dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
dev_err(&dev->dev, "can't export pci devices that " "don't have a normal (0) or bridge (1) " "header type!\n");
err = -ENODEV; goto out;
}
if (!match) {
pci_dev_id = kmalloc(sizeof(*pci_dev_id), GFP_KERNEL); if (!pci_dev_id) {
err = -ENOMEM; goto out;
}
}
/* Called when 'unbind'. This means we must _NOT_ call pci_reset_function or
* other functions that take the sysfs lock. */ staticvoid pcistub_remove(struct pci_dev *dev)
{ struct pcistub_device *psdev, *found_psdev = NULL; unsignedlong flags;
if (found_psdev) {
dev_dbg(&dev->dev, "found device to remove %s\n",
found_psdev->pdev ? "- in-use" : "");
if (found_psdev->pdev) { int domid = xen_find_device_domain_owner(dev);
dev_warn(&dev->dev, "****** removing device %s while still in-use by domain %d! ******\n",
pci_name(found_psdev->dev), domid);
dev_warn(&dev->dev, "****** driver domain may still access this device's i/o resources!\n");
dev_warn(&dev->dev, "****** shutdown driver domain before binding device\n");
dev_warn(&dev->dev, "****** to other drivers or domains\n");
/* N.B. This ends up calling pcistub_put_pci_dev which ends up
* doing the FLR. */
xen_pcibk_release_pci_dev(found_psdev->pdev,
found_psdev->dev, false/* caller holds the lock. */);
}
again:
err = xenbus_transaction_start(&xbt); if (err) {
dev_err(&psdev->dev->dev, "error %d when start xenbus transaction\n", err); return;
} /*PV AER handlers will set this flag*/
xenbus_printf(xbt, nodename, "aerState" , "aerfail");
err = xenbus_transaction_end(xbt, 0); if (err) { if (err == -EAGAIN) goto again;
dev_err(&psdev->dev->dev, "error %d when end xenbus transaction\n", err); return;
}
}
/* For each aer recovery step error_detected, mmio_enabled, etc, front_end and * backend need to have cooperation. In xen_pcibk, those steps will do similar * jobs: send service request and waiting for front_end response.
*/ static pci_ers_result_t common_process(struct pcistub_device *psdev,
pci_channel_state_t state, int aer_cmd,
pci_ers_result_t result)
{
pci_ers_result_t res = result; struct xen_pcie_aer_op *aer_op; struct xen_pcibk_device *pdev = psdev->pdev; struct xen_pci_sharedinfo *sh_info = pdev->sh_info; int ret;
/*with PV AER drivers*/
aer_op = &(sh_info->aer_op);
aer_op->cmd = aer_cmd ; /*useful for error_detected callback*/
aer_op->err = state; /*pcifront_end BDF*/
ret = xen_pcibk_get_pcifront_dev(psdev->dev, psdev->pdev,
&aer_op->domain, &aer_op->bus, &aer_op->devfn); if (!ret) {
dev_err(&psdev->dev->dev, "failed to get pcifront device\n"); return PCI_ERS_RESULT_NONE;
}
wmb();
dev_dbg(&psdev->dev->dev, "aer_op %x dom %x bus %x devfn %x\n",
aer_cmd, aer_op->domain, aer_op->bus, aer_op->devfn); /*local flag to mark there's aer request, xen_pcibk callback will use * this flag to judge whether we need to check pci-front give aer * service ack signal
*/
set_bit(_PCIB_op_pending, (unsignedlong *)&pdev->flags);
/*It is possible that a pcifront conf_read_write ops request invokes * the callback which cause the spurious execution of wake_up. * Yet it is harmless and better than a spinlock here
*/
set_bit(_XEN_PCIB_active,
(unsignedlong *)&sh_info->flags);
wmb();
notify_remote_via_irq(pdev->evtchn_irq);
/* Enable IRQ to signal "request done". */
xen_pcibk_lateeoi(pdev, 0);
ret = wait_event_timeout(xen_pcibk_aer_wait_queue,
!(test_bit(_XEN_PCIB_active, (unsignedlong *)
&sh_info->flags)), 300*HZ);
/* Enable IRQ for pcifront request if not already active. */ if (!test_bit(_PDEVF_op_active, &pdev->flags))
xen_pcibk_lateeoi(pdev, 0);
if (!ret) { if (test_bit(_XEN_PCIB_active,
(unsignedlong *)&sh_info->flags)) {
dev_err(&psdev->dev->dev, "pcifront aer process not responding!\n");
clear_bit(_XEN_PCIB_active,
(unsignedlong *)&sh_info->flags);
aer_op->err = PCI_ERS_RESULT_NONE; return res;
}
}
clear_bit(_PCIB_op_pending, (unsignedlong *)&pdev->flags);
res = (__force pci_ers_result_t)aer_op->err; return res;
}
/* * xen_pcibk_slot_reset: it will send the slot_reset request to pcifront in case * of the device driver could provide this service, and then wait for pcifront * ack. * @dev: pointer to PCI devices * return value is used by aer_core do_recovery policy
*/ static pci_ers_result_t xen_pcibk_slot_reset(struct pci_dev *dev)
{ struct pcistub_device *psdev;
pci_ers_result_t result;
result = PCI_ERS_RESULT_RECOVERED;
dev_dbg(&dev->dev, "xen_pcibk_slot_reset(bus:%x,devfn:%x)\n",
dev->bus->number, dev->devfn);
if (!psdev || !psdev->pdev) {
dev_err(&dev->dev, "device is not found/assigned\n"); goto end;
}
if (!psdev->pdev->sh_info) {
dev_err(&dev->dev, "device is not connected or owned" " by HVM, kill it\n");
kill_domain_by_device(psdev); goto end;
}
if (!test_bit(_XEN_PCIB_AERHANDLER,
(unsignedlong *)&psdev->pdev->sh_info->flags)) {
dev_err(&dev->dev, "guest with no AER driver should have been killed\n"); goto end;
}
result = common_process(psdev, pci_channel_io_normal, XEN_PCI_OP_aer_slotreset, result);
if (result == PCI_ERS_RESULT_NONE ||
result == PCI_ERS_RESULT_DISCONNECT) {
dev_dbg(&dev->dev, "No AER slot_reset service or disconnected!\n");
kill_domain_by_device(psdev);
}
end: if (psdev)
pcistub_device_put(psdev);
up_write(&pcistub_sem); return result;
}
/*xen_pcibk_mmio_enabled: it will send the mmio_enabled request to pcifront * in case of the device driver could provide this service, and then wait * for pcifront ack * @dev: pointer to PCI devices * return value is used by aer_core do_recovery policy
*/
if (!psdev || !psdev->pdev) {
dev_err(&dev->dev, "device is not found/assigned\n"); goto end;
}
if (!psdev->pdev->sh_info) {
dev_err(&dev->dev, "device is not connected or owned" " by HVM, kill it\n");
kill_domain_by_device(psdev); goto end;
}
if (!test_bit(_XEN_PCIB_AERHANDLER,
(unsignedlong *)&psdev->pdev->sh_info->flags)) {
dev_err(&dev->dev, "guest with no AER driver should have been killed\n"); goto end;
}
result = common_process(psdev, pci_channel_io_normal, XEN_PCI_OP_aer_mmio, result);
if (result == PCI_ERS_RESULT_NONE ||
result == PCI_ERS_RESULT_DISCONNECT) {
dev_dbg(&dev->dev, "No AER mmio_enabled service or disconnected!\n");
kill_domain_by_device(psdev);
}
end: if (psdev)
pcistub_device_put(psdev);
up_write(&pcistub_sem); return result;
}
/*xen_pcibk_error_detected: it will send the error_detected request to pcifront * in case of the device driver could provide this service, and then wait * for pcifront ack. * @dev: pointer to PCI devices * @error: the current PCI connection state * return value is used by aer_core do_recovery policy
*/
if (!psdev || !psdev->pdev) {
dev_err(&dev->dev, "device is not found/assigned\n"); goto end;
}
if (!psdev->pdev->sh_info) {
dev_err(&dev->dev, "device is not connected or owned" " by HVM, kill it\n");
kill_domain_by_device(psdev); goto end;
}
/*Guest owns the device yet no aer handler regiested, kill guest*/ if (!test_bit(_XEN_PCIB_AERHANDLER,
(unsignedlong *)&psdev->pdev->sh_info->flags)) {
dev_dbg(&dev->dev, "guest may have no aer driver, kill it\n");
kill_domain_by_device(psdev); goto end;
}
result = common_process(psdev, error, XEN_PCI_OP_aer_detected, result);
if (result == PCI_ERS_RESULT_NONE ||
result == PCI_ERS_RESULT_DISCONNECT) {
dev_dbg(&dev->dev, "No AER error_detected service or disconnected!\n");
kill_domain_by_device(psdev);
}
end: if (psdev)
pcistub_device_put(psdev);
up_write(&pcistub_sem); return result;
}
/*xen_pcibk_error_resume: it will send the error_resume request to pcifront * in case of the device driver could provide this service, and then wait * for pcifront ack. * @dev: pointer to PCI devices
*/
if (!psdev || !psdev->pdev) {
dev_err(&dev->dev, "device is not found/assigned\n"); goto end;
}
if (!psdev->pdev->sh_info) {
dev_err(&dev->dev, "device is not connected or owned" " by HVM, kill it\n");
kill_domain_by_device(psdev); goto end;
}
if (!test_bit(_XEN_PCIB_AERHANDLER,
(unsignedlong *)&psdev->pdev->sh_info->flags)) {
dev_err(&dev->dev, "guest with no AER driver should have been killed\n");
kill_domain_by_device(psdev); goto end;
}
common_process(psdev, pci_channel_io_normal, XEN_PCI_OP_aer_resume,
PCI_ERS_RESULT_RECOVERED);
end: if (psdev)
pcistub_device_put(psdev);
up_write(&pcistub_sem); return;
}
/* * Note: There is no MODULE_DEVICE_TABLE entry here because this isn't * for a normal device. I don't want it to be loaded automatically.
*/
staticstruct pci_driver xen_pcibk_pci_driver = { /* The name should be xen_pciback, but until the tools are updated
* we will keep it as pciback. */
.name = PCISTUB_DRIVER_NAME,
.id_table = pcistub_ids,
.probe = pcistub_probe,
.remove = pcistub_remove,
.err_handler = &xen_pcibk_error_handler,
};
staticinlineint str_to_slot(constchar *buf, int *domain, int *bus, int *slot, int *func)
{ int parsed = 0;
staticint pcistub_device_id_remove(int domain, int bus, int slot, int func)
{ struct pcistub_device_id *pci_dev_id, *t; int err = -ENOENT; unsignedlong flags;
spin_lock_irqsave(&device_ids_lock, flags);
list_for_each_entry_safe(pci_dev_id, t, &pcistub_device_ids,
slot_list) { if (pci_dev_id->domain == domain && pci_dev_id->bus == bus
&& (slot < 0 || PCI_SLOT(pci_dev_id->devfn) == slot)
&& (func < 0 || PCI_FUNC(pci_dev_id->devfn) == func)) { /* Don't break; here because it's possible the same * slot could be in the list more than once
*/
list_del(&pci_dev_id->slot_list);
kfree(pci_dev_id);
dev_data = pci_get_drvdata(psdev->dev); /* the driver data for a device should never be null at this point */ if (!dev_data) {
err = -ENXIO; goto release;
} if (!dev_data->permissive) {
dev_data->permissive = 1; /* Let user know that what they're doing could be unsafe */
dev_warn(&psdev->dev->dev, "enabling permissive mode " "configuration space accesses!\n");
dev_warn(&psdev->dev->dev, "permissive mode is potentially unsafe!\n");
}
release:
pcistub_device_put(psdev);
out: if (!err)
err = count; return err;
}
dev_data = pci_get_drvdata(psdev->dev); /* the driver data for a device should never be null at this point */ if (!dev_data) {
err = -ENXIO; goto release;
}
dev_data->allow_interrupt_control = 1;
release:
pcistub_device_put(psdev);
out: if (!err)
err = count; return err;
}
err = pcistub_device_id_add(domain, bus, slot, func); if (err) goto out;
pos += parsed;
} while (pci_devs_to_hide[pos]);
}
/* If we're the first PCI Device Driver to register, we're the * first one to get offered PCI devices as they become * available (and thus we can be the first to grab them)
*/
err = pci_register_driver(&xen_pcibk_pci_driver); if (err < 0) goto out;
err = driver_create_file(&xen_pcibk_pci_driver.driver,
&driver_attr_new_slot); if (!err)
err = driver_create_file(&xen_pcibk_pci_driver.driver,
&driver_attr_remove_slot); if (!err)
err = driver_create_file(&xen_pcibk_pci_driver.driver,
&driver_attr_slots); if (!err)
err = driver_create_file(&xen_pcibk_pci_driver.driver,
&driver_attr_quirks); if (!err)
err = driver_create_file(&xen_pcibk_pci_driver.driver,
&driver_attr_permissive); if (!err)
err = driver_create_file(&xen_pcibk_pci_driver.driver,
&driver_attr_allow_interrupt_control);
if (!err)
err = driver_create_file(&xen_pcibk_pci_driver.driver,
&driver_attr_irq_handlers); if (!err)
err = driver_create_file(&xen_pcibk_pci_driver.driver,
&driver_attr_irq_handler_state); if (err)
pcistub_exit();
#ifndef MODULE /* * fs_initcall happens before device_initcall * so xen_pcibk *should* get called first (b/c we * want to suck up any device before other drivers * get a chance by being the first pci device * driver to register)
*/
fs_initcall(pcistub_init); #endif
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.