7 #include <linux/module.h>
10 #include <linux/list.h>
13 #include <linux/pci.h>
14 #include <linux/wait.h>
15 #include <linux/sched.h>
17 #include <xen/events.h>
19 #include <asm/xen/hypervisor.h>
24 static char *pci_devs_to_hide;
59 static int initialize_devices;
78 kref_init(&psdev->
kref);
85 static void pcistub_device_release(
struct kref *
kref)
91 dev_data = pci_get_drvdata(psdev->
dev);
93 dev_dbg(&psdev->
dev->dev,
"pcistub_device_release\n");
95 xen_unregister_device_domain_owner(psdev->
dev);
103 dev_dbg(&psdev->
dev->dev,
"Could not reload PCI state\n");
111 pci_set_drvdata(psdev->
dev,
NULL);
123 static inline void pcistub_device_get(
struct pcistub_device *psdev)
125 kref_get(&psdev->
kref);
128 static inline void pcistub_device_put(
struct pcistub_device *psdev)
130 kref_put(&psdev->
kref, pcistub_device_release);
144 && bus == psdev->
dev->bus->number
146 pcistub_device_get(psdev);
155 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
165 pcistub_device_get(psdev);
170 pci_dev = psdev->
dev;
172 spin_unlock_irqrestore(&psdev->
lock, flags);
175 pcistub_device_put(psdev);
185 struct pci_dev *found_dev =
NULL;
193 && bus == psdev->
dev->bus->number
195 found_dev = pcistub_device_get_pci_dev(pdev, psdev);
200 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
208 struct pci_dev *found_dev =
NULL;
214 if (psdev->
dev == dev) {
215 found_dev = pcistub_device_get_pci_dev(pdev, psdev);
220 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
232 if (psdev->
dev == dev) {
238 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
263 xen_unregister_device_domain_owner(found_psdev->
dev);
267 spin_unlock_irqrestore(&found_psdev->
lock, flags);
269 pcistub_device_put(found_psdev);
273 static int __devinit pcistub_match_one(
struct pci_dev *dev,
279 for (; dev !=
NULL; dev = dev->
bus->self) {
281 && dev->
bus->number == pdev_id->
bus
286 if (dev == dev->
bus->self)
293 static int __devinit pcistub_match(
struct pci_dev *dev)
301 if (pcistub_match_one(dev, pdev_id)) {
306 spin_unlock_irqrestore(&device_ids_lock, flags);
311 static int __devinit pcistub_init_device(
struct pci_dev *dev)
329 pci_set_drvdata(dev, dev_data);
361 dev_err(&dev->
dev,
"Could not store PCI conf saved state!\n");
363 dev_dbg(&dev->
dev,
"reseting (FLR, D3, etc) the device\n");
380 pci_set_drvdata(dev,
NULL);
391 static int __init pcistub_init_devices_late(
void)
401 while (!list_empty(&seized_devices)) {
406 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
408 err = pcistub_init_device(psdev->
dev);
411 "error %d initializing device\n", err);
422 initialize_devices = 1;
424 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
429 static int __devinit pcistub_seize(
struct pci_dev *dev)
435 psdev = pcistub_device_alloc(dev);
441 if (initialize_devices) {
442 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
445 err = pcistub_init_device(psdev->
dev);
450 list_add(&psdev->
dev_list, &pcistub_devices);
452 dev_dbg(&dev->
dev,
"deferring initialization\n");
453 list_add(&psdev->
dev_list, &seized_devices);
456 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
459 pcistub_device_put(psdev);
464 static int __devinit pcistub_probe(
struct pci_dev *dev,
471 if (pcistub_match(dev)) {
475 dev_err(&dev->
dev,
"can't export pci devices that "
476 "don't have a normal (0) or bridge (1) "
483 err = pcistub_seize(dev);
492 static void pcistub_remove(
struct pci_dev *dev)
504 if (psdev->
dev == dev) {
510 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
513 dev_dbg(&dev->
dev,
"found device to remove - in use? %p\n",
516 if (found_psdev->
pdev) {
518 "%s while still in-use! ******\n",
519 pci_name(found_psdev->
dev));
521 " still access this device's i/o resources!\n");
523 "domain before binding device\n");
527 xen_pcibk_release_pci_dev(found_psdev->
pdev,
533 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
536 pcistub_device_put(found_psdev);
550 #define PCI_NODENAME_MAX 40
559 psdev->
pdev->xdev->otherend_id);
565 "error %d when start xenbus transaction\n", err);
575 "error %d when end xenbus transaction\n", err);
593 aer_op = &(psdev->
pdev->sh_info->aer_op);
594 aer_op->
cmd = aer_cmd ;
598 ret = xen_pcibk_get_pcifront_dev(psdev->
dev, psdev->
pdev,
602 DRV_NAME ": failed to get pcifront device\n");
608 DRV_NAME ": aer_op %x dom %x bus %x devfn %x\n",
621 (
unsigned long *)&psdev->
pdev->sh_info->flags);
627 &psdev->
pdev->sh_info->flags)), 300*
HZ);
631 (
unsigned long *)&psdev->
pdev->sh_info->flags)) {
633 "pcifront aer process not responding!\n");
635 (
unsigned long *)&psdev->
pdev->sh_info->flags);
643 (
unsigned long *)&psdev->
pdev->sh_info->flags)) {
645 "schedule pci_conf service in " DRV_NAME "\n");
666 dev_dbg(&dev->
dev,
"xen_pcibk_slot_reset(bus:%x,devfn:%x)\n",
675 if (!psdev || !psdev->
pdev) {
677 DRV_NAME " device is not found/assigned\n");
681 if (!psdev->
pdev->sh_info) {
683 " by HVM, kill it\n");
684 kill_domain_by_device(psdev);
689 (
unsigned long *)&psdev->
pdev->sh_info->flags)) {
691 "guest with no AER driver should have been killed\n");
699 "No AER slot_reset service or disconnected!\n");
700 kill_domain_by_device(psdev);
704 pcistub_device_put(psdev);
724 dev_dbg(&dev->
dev,
"xen_pcibk_mmio_enabled(bus:%x,devfn:%x)\n",
733 if (!psdev || !psdev->
pdev) {
735 DRV_NAME " device is not found/assigned\n");
739 if (!psdev->
pdev->sh_info) {
741 " by HVM, kill it\n");
742 kill_domain_by_device(psdev);
747 (
unsigned long *)&psdev->
pdev->sh_info->flags)) {
749 "guest with no AER driver should have been killed\n");
757 "No AER mmio_enabled service or disconnected!\n");
758 kill_domain_by_device(psdev);
762 pcistub_device_put(psdev);
782 dev_dbg(&dev->
dev,
"xen_pcibk_error_detected(bus:%x,devfn:%x)\n",
791 if (!psdev || !psdev->
pdev) {
793 DRV_NAME " device is not found/assigned\n");
797 if (!psdev->
pdev->sh_info) {
799 " by HVM, kill it\n");
800 kill_domain_by_device(psdev);
806 (
unsigned long *)&psdev->
pdev->sh_info->flags)) {
807 dev_dbg(&dev->
dev,
"guest may have no aer driver, kill it\n");
808 kill_domain_by_device(psdev);
816 "No AER error_detected service or disconnected!\n");
817 kill_domain_by_device(psdev);
821 pcistub_device_put(psdev);
832 static void xen_pcibk_error_resume(
struct pci_dev *dev)
836 dev_dbg(&dev->
dev,
"xen_pcibk_error_resume(bus:%x,devfn:%x)\n",
845 if (!psdev || !psdev->
pdev) {
847 DRV_NAME " device is not found/assigned\n");
851 if (!psdev->
pdev->sh_info) {
853 " by HVM, kill it\n");
854 kill_domain_by_device(psdev);
859 (
unsigned long *)&psdev->
pdev->sh_info->flags)) {
861 "guest with no AER driver should have been killed\n");
862 kill_domain_by_device(psdev);
869 pcistub_device_put(psdev);
876 .error_detected = xen_pcibk_error_detected,
877 .mmio_enabled = xen_pcibk_mmio_enabled,
878 .slot_reset = xen_pcibk_slot_reset,
879 .resume = xen_pcibk_error_resume,
887 static struct pci_driver xen_pcibk_pci_driver = {
891 .id_table = pcistub_ids,
892 .probe = pcistub_probe,
893 .remove = pcistub_remove,
894 .err_handler = &xen_pcibk_error_handler,
897 static inline int str_to_slot(
const char *
buf,
int *domain,
int *bus,
898 int *slot,
int *func)
903 err =
sscanf(buf,
" %x:%x:%x.%x", domain, bus, slot, func);
907 err =
sscanf(buf,
" %x:%x:%x.%c", domain, bus, slot, &wc);
911 err =
sscanf(buf,
" %x:%x:*.%c", domain, bus, &wc);
916 if (err == 4 && wc ==
'*')
924 err =
sscanf(buf,
" %x:%x.%x", bus, slot, func);
928 err =
sscanf(buf,
" %x:%x.%c", bus, slot, &wc);
932 err =
sscanf(buf,
" %x:*.%c", bus, &wc) + 1;
935 if (err == 3 && wc ==
'*')
941 static inline int str_to_quirk(
const char *buf,
int *domain,
int *bus,
int
947 sscanf(buf,
" %04x:%02x:%02x.%d-%08x:%1x:%08x", domain, bus, slot,
948 func, reg, size, mask);
954 static int pcistub_device_id_add(
int domain,
int bus,
int slot,
int func)
961 for (slot = 0; !rc && slot < 32; ++
slot)
962 rc = pcistub_device_id_add(domain, bus, slot, func);
967 for (func = 0; !rc && func < 8; ++
func)
968 rc = pcistub_device_id_add(domain, bus, slot, func);
981 domain, bus, slot, func);
985 spin_unlock_irqrestore(&device_ids_lock, flags);
990 static int pcistub_device_id_remove(
int domain,
int bus,
int slot,
int func)
999 if (pci_dev_id->
domain == domain && pci_dev_id->
bus == bus
1011 "seize list\n", domain, bus, slot, func);
1014 spin_unlock_irqrestore(&device_ids_lock, flags);
1019 static int pcistub_reg_add(
int domain,
int bus,
int slot,
int func,
int reg,
1024 struct pci_dev *
dev;
1027 psdev = pcistub_device_find(domain, bus, slot, func);
1053 pcistub_device_put(psdev);
1063 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1067 err = pcistub_device_id_add(domain, bus, slot, func);
1082 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1086 err = pcistub_device_id_remove(domain, bus, slot, func);
1099 unsigned long flags;
1107 "%04x:%02x:%02x.%d\n",
1112 spin_unlock_irqrestore(&device_ids_lock, flags);
1123 unsigned long flags;
1131 dev_data = pci_get_drvdata(psdev->
dev);
1136 "%s:%s:%sing:%ld\n",
1137 pci_name(psdev->
dev),
1138 dev_data->
isr_on ?
"on" :
"off",
1139 dev_data->
ack_intr ?
"ack" :
"not ack",
1142 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1156 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1160 psdev = pcistub_device_find(domain, bus, slot, func);
1164 dev_data = pci_get_drvdata(psdev->
dev);
1168 dev_dbg(&psdev->
dev->dev,
"%s fake irq handler: %d->%d\n",
1177 pcistub_device_put(psdev);
1183 pcistub_irq_handler_switch);
1191 err = str_to_quirk(buf, &domain, &bus, &slot, &func, ®, &size,
1196 err = pcistub_reg_add(domain, bus, slot, func, reg, size, mask);
1207 unsigned long flags;
1219 "%02x:%02x.%01x\n\t%04x:%04x:%04x:%04x\n",
1220 quirk->
pdev->bus->number,
1224 quirk->
devid.subvendor,
1225 quirk->
devid.subdevice);
1227 dev_data = pci_get_drvdata(quirk->
pdev);
1230 field = cfg_entry->
field;
1235 "\t\t%08x:%01x:%08x\n",
1243 spin_unlock_irqrestore(&device_ids_lock, flags);
1257 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1260 if (slot < 0 || func < 0) {
1264 psdev = pcistub_device_find(domain, bus, slot, func);
1270 dev_data = pci_get_drvdata(psdev->
dev);
1279 dev_warn(&psdev->
dev->dev,
"enabling permissive mode "
1280 "configuration space accesses!\n");
1282 "permissive mode is potentially unsafe!\n");
1285 pcistub_device_put(psdev);
1297 unsigned long flags;
1304 dev_data = pci_get_drvdata(psdev->
dev);
1309 pci_name(psdev->
dev));
1311 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1317 static void pcistub_exit(
void)
1321 &driver_attr_remove_slot);
1325 &driver_attr_permissive);
1327 &driver_attr_irq_handlers);
1329 &driver_attr_irq_handler_state);
1333 static int __init pcistub_init(
void)
1340 if (pci_devs_to_hide && *pci_devs_to_hide) {
1346 err =
sscanf(pci_devs_to_hide + pos,
1347 " (%x:%x:%x.%x) %n",
1348 &domain, &bus, &slot, &func, &parsed);
1352 err =
sscanf(pci_devs_to_hide + pos,
1353 " (%x:%x:%x.%c) %n",
1354 &domain, &bus, &slot, &wc,
1359 err =
sscanf(pci_devs_to_hide + pos,
1361 &domain, &bus, &wc, &parsed) + 1;
1365 if (err != 4 || wc !=
'*') {
1368 err =
sscanf(pci_devs_to_hide + pos,
1370 &bus, &slot, &func, &parsed);
1374 err =
sscanf(pci_devs_to_hide + pos,
1381 err =
sscanf(pci_devs_to_hide + pos,
1383 &bus, &wc, &parsed) + 1;
1386 if (err != 3 || wc !=
'*')
1390 err = pcistub_device_id_add(domain, bus, slot, func);
1396 }
while (parsed > 0 && pci_devs_to_hide[pos]);
1403 err = pci_register_driver(&xen_pcibk_pci_driver);
1408 &driver_attr_new_slot);
1411 &driver_attr_remove_slot);
1414 &driver_attr_slots);
1417 &driver_attr_quirks);
1420 &driver_attr_permissive);
1424 &driver_attr_irq_handlers);
1427 &driver_attr_irq_handler_state);
1436 pci_devs_to_hide + pos);
1451 static int __init xen_pcibk_init(
void)
1463 err = pcistub_init();
1468 pcistub_init_devices_late();
1476 static void __exit xen_pcibk_cleanup(
void)