| 1 |
/* |
| 2 |
* PCI Stub Driver - Grabs devices in backend to be exported later |
| 3 |
* |
| 4 |
* Ryan Wilson <[email protected]> |
| 5 |
* Chris Bookholt <[email protected]> |
| 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 |
|
| 35 |
static char *pci_devs_to_hide; |
| 36 |
wait_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 |
*/ |
| 40 |
static DECLARE_RWSEM(pcistub_sem); |
| 41 |
module_param_named(hide, pci_devs_to_hide, charp, 0444); |
| 42 |
|
| 43 |
struct pcistub_device_id { |
| 44 |
struct list_head slot_list; |
| 45 |
int domain; |
| 46 |
unsigned char bus; |
| 47 |
unsigned int devfn; |
| 48 |
}; |
| 49 |
static LIST_HEAD(pcistub_device_ids); |
| 50 |
static DEFINE_SPINLOCK(device_ids_lock); |
| 51 |
|
| 52 |
struct 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 |
*/ |
| 67 |
static DEFINE_SPINLOCK(pcistub_devices_lock); |
| 68 |
static LIST_HEAD(pcistub_devices); |
| 69 |
|
| 70 |
/* wait for device_initcall before initializing our devices |
| 71 |
* (see pcistub_init_devices_late) |
| 72 |
*/ |
| 73 |
static int initialize_devices; |
| 74 |
static LIST_HEAD(seized_devices); |
| 75 |
|
| 76 |
static 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_obj(*psdev); |
| 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 |
|
| 101 |
static 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 */ |
| 112 |
static 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 |
|
| 166 |
static inline void pcistub_device_get(struct pcistub_device *psdev) |
| 167 |
{ |
| 168 |
kref_get(&psdev->kref); |
| 169 |
} |
| 170 |
|
| 171 |
static inline void pcistub_device_put(struct pcistub_device *psdev) |
| 172 |
{ |
| 173 |
kref_put(&psdev->kref, pcistub_device_release); |
| 174 |
} |
| 175 |
|
| 176 |
static 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 |
|
| 194 |
static 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 |
|
| 210 |
static 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 |
| 230 |
static 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 |
|
| 247 |
struct 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 |
*/ |
| 276 |
void 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 |
|
| 336 |
static 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 |
|
| 356 |
static 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 |
|
| 374 |
static 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 |
|
| 478 |
config_release: |
| 479 |
xen_pcibk_config_free_dev(dev); |
| 480 |
|
| 481 |
out: |
| 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 |
*/ |
| 493 |
static 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 |
|
| 529 |
static 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 |
|
| 559 |
static 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 |
static struct pci_driver xen_pcibk_pci_driver; |
| 602 |
|
| 603 |
/* Called when 'bind'. This means we must _NOT_ call pci_reset_function or |
| 604 |
* other functions that take the sysfs lock. */ |
| 605 |
static int pcistub_probe(struct pci_dev *dev, const struct pci_device_id *id) |
| 606 |
{ |
| 607 |
int err = 0, match; |
| 608 |
struct pcistub_device_id *pci_dev_id = NULL; |
| 609 |
|
| 610 |
dev_dbg(&dev->dev, "probing...\n"); |
| 611 |
|
| 612 |
match = pcistub_match(dev); |
| 613 |
|
| 614 |
if (device_match_driver_override(&dev->dev, |
| 615 |
&xen_pcibk_pci_driver.driver) > 0 || |
| 616 |
match) { |
| 617 |
|
| 618 |
if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL |
| 619 |
&& dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) { |
| 620 |
dev_err(&dev->dev, "can't export pci devices that " |
| 621 |
"don't have a normal (0) or bridge (1) " |
| 622 |
"header type!\n"); |
| 623 |
err = -ENODEV; |
| 624 |
goto out; |
| 625 |
} |
| 626 |
|
| 627 |
if (!match) { |
| 628 |
pci_dev_id = kmalloc_obj(*pci_dev_id); |
| 629 |
if (!pci_dev_id) { |
| 630 |
err = -ENOMEM; |
| 631 |
goto out; |
| 632 |
} |
| 633 |
} |
| 634 |
|
| 635 |
dev_info(&dev->dev, "seizing device\n"); |
| 636 |
err = pcistub_seize(dev, pci_dev_id); |
| 637 |
} else |
| 638 |
/* Didn't find the device */ |
| 639 |
err = -ENODEV; |
| 640 |
|
| 641 |
out: |
| 642 |
return err; |
| 643 |
} |
| 644 |
|
| 645 |
/* Called when 'unbind'. This means we must _NOT_ call pci_reset_function or |
| 646 |
* other functions that take the sysfs lock. */ |
| 647 |
static void pcistub_remove(struct pci_dev *dev) |
| 648 |
{ |
| 649 |
struct pcistub_device *psdev, *found_psdev = NULL; |
| 650 |
unsigned long flags; |
| 651 |
|
| 652 |
dev_dbg(&dev->dev, "removing\n"); |
| 653 |
|
| 654 |
spin_lock_irqsave(&pcistub_devices_lock, flags); |
| 655 |
|
| 656 |
xen_pcibk_config_quirk_release(dev); |
| 657 |
|
| 658 |
list_for_each_entry(psdev, &pcistub_devices, dev_list) { |
| 659 |
if (psdev->dev == dev) { |
| 660 |
found_psdev = psdev; |
| 661 |
break; |
| 662 |
} |
| 663 |
} |
| 664 |
|
| 665 |
spin_unlock_irqrestore(&pcistub_devices_lock, flags); |
| 666 |
|
| 667 |
if (found_psdev) { |
| 668 |
dev_dbg(&dev->dev, "found device to remove %s\n", |
| 669 |
found_psdev->pdev ? "- in-use" : ""); |
| 670 |
|
| 671 |
if (found_psdev->pdev) { |
| 672 |
int domid = xen_find_device_domain_owner(dev); |
| 673 |
|
| 674 |
dev_warn(&dev->dev, "****** removing device %s while still in-use by domain %d! ******\n", |
| 675 |
pci_name(found_psdev->dev), domid); |
| 676 |
dev_warn(&dev->dev, "****** driver domain may still access this device's i/o resources!\n"); |
| 677 |
dev_warn(&dev->dev, "****** shutdown driver domain before binding device\n"); |
| 678 |
dev_warn(&dev->dev, "****** to other drivers or domains\n"); |
| 679 |
|
| 680 |
/* N.B. This ends up calling pcistub_put_pci_dev which ends up |
| 681 |
* doing the FLR. */ |
| 682 |
xen_pcibk_release_pci_dev(found_psdev->pdev, |
| 683 |
found_psdev->dev, |
| 684 |
false /* caller holds the lock. */); |
| 685 |
} |
| 686 |
|
| 687 |
spin_lock_irqsave(&pcistub_devices_lock, flags); |
| 688 |
list_del(&found_psdev->dev_list); |
| 689 |
spin_unlock_irqrestore(&pcistub_devices_lock, flags); |
| 690 |
|
| 691 |
/* the final put for releasing from the list */ |
| 692 |
pcistub_device_put(found_psdev); |
| 693 |
} |
| 694 |
} |
| 695 |
|
| 696 |
static const struct pci_device_id pcistub_ids[] = { |
| 697 |
{ |
| 698 |
.vendor = PCI_ANY_ID, |
| 699 |
.device = PCI_ANY_ID, |
| 700 |
.subvendor = PCI_ANY_ID, |
| 701 |
.subdevice = PCI_ANY_ID, |
| 702 |
}, |
| 703 |
{0,}, |
| 704 |
}; |
| 705 |
|
| 706 |
#define PCI_NODENAME_MAX 40 |
| 707 |
static void kill_domain_by_device(struct pcistub_device *psdev) |
| 708 |
{ |
| 709 |
struct xenbus_transaction xbt; |
| 710 |
int err; |
| 711 |
char nodename[PCI_NODENAME_MAX]; |
| 712 |
|
| 713 |
BUG_ON(!psdev); |
| 714 |
snprintf(nodename, PCI_NODENAME_MAX, "/local/domain/0/backend/pci/%d/0", |
| 715 |
psdev->pdev->xdev->otherend_id); |
| 716 |
|
| 717 |
again: |
| 718 |
err = xenbus_transaction_start(&xbt); |
| 719 |
if (err) { |
| 720 |
dev_err(&psdev->dev->dev, |
| 721 |
"error %d when start xenbus transaction\n", err); |
| 722 |
return; |
| 723 |
} |
| 724 |
/*PV AER handlers will set this flag*/ |
| 725 |
xenbus_printf(xbt, nodename, "aerState" , "aerfail"); |
| 726 |
err = xenbus_transaction_end(xbt, 0); |
| 727 |
if (err) { |
| 728 |
if (err == -EAGAIN) |
| 729 |
goto again; |
| 730 |
dev_err(&psdev->dev->dev, |
| 731 |
"error %d when end xenbus transaction\n", err); |
| 732 |
return; |
| 733 |
} |
| 734 |
} |
| 735 |
|
| 736 |
/* For each aer recovery step error_detected, mmio_enabled, etc, front_end and |
| 737 |
* backend need to have cooperation. In xen_pcibk, those steps will do similar |
| 738 |
* jobs: send service request and waiting for front_end response. |
| 739 |
*/ |
| 740 |
static pci_ers_result_t common_process(struct pcistub_device *psdev, |
| 741 |
pci_channel_state_t state, int aer_cmd, |
| 742 |
pci_ers_result_t result) |
| 743 |
{ |
| 744 |
pci_ers_result_t res = result; |
| 745 |
struct xen_pcie_aer_op *aer_op; |
| 746 |
struct xen_pcibk_device *pdev = psdev->pdev; |
| 747 |
struct xen_pci_sharedinfo *sh_info = pdev->sh_info; |
| 748 |
int ret; |
| 749 |
|
| 750 |
/*with PV AER drivers*/ |
| 751 |
aer_op = &(sh_info->aer_op); |
| 752 |
aer_op->cmd = aer_cmd ; |
| 753 |
/*useful for error_detected callback*/ |
| 754 |
aer_op->err = state; |
| 755 |
/*pcifront_end BDF*/ |
| 756 |
ret = xen_pcibk_get_pcifront_dev(psdev->dev, psdev->pdev, |
| 757 |
&aer_op->domain, &aer_op->bus, &aer_op->devfn); |
| 758 |
if (!ret) { |
| 759 |
dev_err(&psdev->dev->dev, "failed to get pcifront device\n"); |
| 760 |
return PCI_ERS_RESULT_NONE; |
| 761 |
} |
| 762 |
wmb(); |
| 763 |
|
| 764 |
dev_dbg(&psdev->dev->dev, "aer_op %x dom %x bus %x devfn %x\n", |
| 765 |
aer_cmd, aer_op->domain, aer_op->bus, aer_op->devfn); |
| 766 |
/*local flag to mark there's aer request, xen_pcibk callback will use |
| 767 |
* this flag to judge whether we need to check pci-front give aer |
| 768 |
* service ack signal |
| 769 |
*/ |
| 770 |
set_bit(_PCIB_op_pending, (unsigned long *)&pdev->flags); |
| 771 |
|
| 772 |
/*It is possible that a pcifront conf_read_write ops request invokes |
| 773 |
* the callback which cause the spurious execution of wake_up. |
| 774 |
* Yet it is harmless and better than a spinlock here |
| 775 |
*/ |
| 776 |
set_bit(_XEN_PCIB_active, |
| 777 |
(unsigned long *)&sh_info->flags); |
| 778 |
wmb(); |
| 779 |
notify_remote_via_irq(pdev->evtchn_irq); |
| 780 |
|
| 781 |
/* Enable IRQ to signal "request done". */ |
| 782 |
xen_pcibk_lateeoi(pdev, 0); |
| 783 |
|
| 784 |
ret = wait_event_timeout(xen_pcibk_aer_wait_queue, |
| 785 |
!(test_bit(_XEN_PCIB_active, (unsigned long *) |
| 786 |
&sh_info->flags)), 300*HZ); |
| 787 |
|
| 788 |
/* Enable IRQ for pcifront request if not already active. */ |
| 789 |
if (!test_bit(_PDEVF_op_active, &pdev->flags)) |
| 790 |
xen_pcibk_lateeoi(pdev, 0); |
| 791 |
|
| 792 |
if (!ret) { |
| 793 |
if (test_bit(_XEN_PCIB_active, |
| 794 |
(unsigned long *)&sh_info->flags)) { |
| 795 |
dev_err(&psdev->dev->dev, |
| 796 |
"pcifront aer process not responding!\n"); |
| 797 |
clear_bit(_XEN_PCIB_active, |
| 798 |
(unsigned long *)&sh_info->flags); |
| 799 |
aer_op->err = PCI_ERS_RESULT_NONE; |
| 800 |
return res; |
| 801 |
} |
| 802 |
} |
| 803 |
clear_bit(_PCIB_op_pending, (unsigned long *)&pdev->flags); |
| 804 |
|
| 805 |
res = (__force pci_ers_result_t)aer_op->err; |
| 806 |
return res; |
| 807 |
} |
| 808 |
|
| 809 |
/* |
| 810 |
* xen_pcibk_slot_reset: it will send the slot_reset request to pcifront in case |
| 811 |
* of the device driver could provide this service, and then wait for pcifront |
| 812 |
* ack. |
| 813 |
* @dev: pointer to PCI devices |
| 814 |
* return value is used by aer_core do_recovery policy |
| 815 |
*/ |
| 816 |
static pci_ers_result_t xen_pcibk_slot_reset(struct pci_dev *dev) |
| 817 |
{ |
| 818 |
struct pcistub_device *psdev; |
| 819 |
pci_ers_result_t result; |
| 820 |
|
| 821 |
result = PCI_ERS_RESULT_RECOVERED; |
| 822 |
dev_dbg(&dev->dev, "xen_pcibk_slot_reset(bus:%x,devfn:%x)\n", |
| 823 |
dev->bus->number, dev->devfn); |
| 824 |
|
| 825 |
down_write(&pcistub_sem); |
| 826 |
psdev = pcistub_device_find(pci_domain_nr(dev->bus), |
| 827 |
dev->bus->number, |
| 828 |
PCI_SLOT(dev->devfn), |
| 829 |
PCI_FUNC(dev->devfn)); |
| 830 |
|
| 831 |
if (!psdev || !psdev->pdev) { |
| 832 |
dev_err(&dev->dev, "device is not found/assigned\n"); |
| 833 |
goto end; |
| 834 |
} |
| 835 |
|
| 836 |
if (!psdev->pdev->sh_info) { |
| 837 |
dev_err(&dev->dev, "device is not connected or owned" |
| 838 |
" by HVM, kill it\n"); |
| 839 |
kill_domain_by_device(psdev); |
| 840 |
goto end; |
| 841 |
} |
| 842 |
|
| 843 |
if (!test_bit(_XEN_PCIB_AERHANDLER, |
| 844 |
(unsigned long *)&psdev->pdev->sh_info->flags)) { |
| 845 |
dev_err(&dev->dev, |
| 846 |
"guest with no AER driver should have been killed\n"); |
| 847 |
goto end; |
| 848 |
} |
| 849 |
result = common_process(psdev, pci_channel_io_normal, XEN_PCI_OP_aer_slotreset, result); |
| 850 |
|
| 851 |
if (result == PCI_ERS_RESULT_NONE || |
| 852 |
result == PCI_ERS_RESULT_DISCONNECT) { |
| 853 |
dev_dbg(&dev->dev, |
| 854 |
"No AER slot_reset service or disconnected!\n"); |
| 855 |
kill_domain_by_device(psdev); |
| 856 |
} |
| 857 |
end: |
| 858 |
if (psdev) |
| 859 |
pcistub_device_put(psdev); |
| 860 |
up_write(&pcistub_sem); |
| 861 |
return result; |
| 862 |
|
| 863 |
} |
| 864 |
|
| 865 |
|
| 866 |
/*xen_pcibk_mmio_enabled: it will send the mmio_enabled request to pcifront |
| 867 |
* in case of the device driver could provide this service, and then wait |
| 868 |
* for pcifront ack |
| 869 |
* @dev: pointer to PCI devices |
| 870 |
* return value is used by aer_core do_recovery policy |
| 871 |
*/ |
| 872 |
|
| 873 |
static pci_ers_result_t xen_pcibk_mmio_enabled(struct pci_dev *dev) |
| 874 |
{ |
| 875 |
struct pcistub_device *psdev; |
| 876 |
pci_ers_result_t result; |
| 877 |
|
| 878 |
result = PCI_ERS_RESULT_RECOVERED; |
| 879 |
dev_dbg(&dev->dev, "xen_pcibk_mmio_enabled(bus:%x,devfn:%x)\n", |
| 880 |
dev->bus->number, dev->devfn); |
| 881 |
|
| 882 |
down_write(&pcistub_sem); |
| 883 |
psdev = pcistub_device_find(pci_domain_nr(dev->bus), |
| 884 |
dev->bus->number, |
| 885 |
PCI_SLOT(dev->devfn), |
| 886 |
PCI_FUNC(dev->devfn)); |
| 887 |
|
| 888 |
if (!psdev || !psdev->pdev) { |
| 889 |
dev_err(&dev->dev, "device is not found/assigned\n"); |
| 890 |
goto end; |
| 891 |
} |
| 892 |
|
| 893 |
if (!psdev->pdev->sh_info) { |
| 894 |
dev_err(&dev->dev, "device is not connected or owned" |
| 895 |
" by HVM, kill it\n"); |
| 896 |
kill_domain_by_device(psdev); |
| 897 |
goto end; |
| 898 |
} |
| 899 |
|
| 900 |
if (!test_bit(_XEN_PCIB_AERHANDLER, |
| 901 |
(unsigned long *)&psdev->pdev->sh_info->flags)) { |
| 902 |
dev_err(&dev->dev, |
| 903 |
"guest with no AER driver should have been killed\n"); |
| 904 |
goto end; |
| 905 |
} |
| 906 |
result = common_process(psdev, pci_channel_io_normal, XEN_PCI_OP_aer_mmio, result); |
| 907 |
|
| 908 |
if (result == PCI_ERS_RESULT_NONE || |
| 909 |
result == PCI_ERS_RESULT_DISCONNECT) { |
| 910 |
dev_dbg(&dev->dev, |
| 911 |
"No AER mmio_enabled service or disconnected!\n"); |
| 912 |
kill_domain_by_device(psdev); |
| 913 |
} |
| 914 |
end: |
| 915 |
if (psdev) |
| 916 |
pcistub_device_put(psdev); |
| 917 |
up_write(&pcistub_sem); |
| 918 |
return result; |
| 919 |
} |
| 920 |
|
| 921 |
/*xen_pcibk_error_detected: it will send the error_detected request to pcifront |
| 922 |
* in case of the device driver could provide this service, and then wait |
| 923 |
* for pcifront ack. |
| 924 |
* @dev: pointer to PCI devices |
| 925 |
* @error: the current PCI connection state |
| 926 |
* return value is used by aer_core do_recovery policy |
| 927 |
*/ |
| 928 |
|
| 929 |
static pci_ers_result_t xen_pcibk_error_detected(struct pci_dev *dev, |
| 930 |
pci_channel_state_t error) |
| 931 |
{ |
| 932 |
struct pcistub_device *psdev; |
| 933 |
pci_ers_result_t result; |
| 934 |
|
| 935 |
result = PCI_ERS_RESULT_CAN_RECOVER; |
| 936 |
dev_dbg(&dev->dev, "xen_pcibk_error_detected(bus:%x,devfn:%x)\n", |
| 937 |
dev->bus->number, dev->devfn); |
| 938 |
|
| 939 |
down_write(&pcistub_sem); |
| 940 |
psdev = pcistub_device_find(pci_domain_nr(dev->bus), |
| 941 |
dev->bus->number, |
| 942 |
PCI_SLOT(dev->devfn), |
| 943 |
PCI_FUNC(dev->devfn)); |
| 944 |
|
| 945 |
if (!psdev || !psdev->pdev) { |
| 946 |
dev_err(&dev->dev, "device is not found/assigned\n"); |
| 947 |
goto end; |
| 948 |
} |
| 949 |
|
| 950 |
if (!psdev->pdev->sh_info) { |
| 951 |
dev_err(&dev->dev, "device is not connected or owned" |
| 952 |
" by HVM, kill it\n"); |
| 953 |
kill_domain_by_device(psdev); |
| 954 |
goto end; |
| 955 |
} |
| 956 |
|
| 957 |
/*Guest owns the device yet no aer handler regiested, kill guest*/ |
| 958 |
if (!test_bit(_XEN_PCIB_AERHANDLER, |
| 959 |
(unsigned long *)&psdev->pdev->sh_info->flags)) { |
| 960 |
dev_dbg(&dev->dev, "guest may have no aer driver, kill it\n"); |
| 961 |
kill_domain_by_device(psdev); |
| 962 |
goto end; |
| 963 |
} |
| 964 |
result = common_process(psdev, error, XEN_PCI_OP_aer_detected, result); |
| 965 |
|
| 966 |
if (result == PCI_ERS_RESULT_NONE || |
| 967 |
result == PCI_ERS_RESULT_DISCONNECT) { |
| 968 |
dev_dbg(&dev->dev, |
| 969 |
"No AER error_detected service or disconnected!\n"); |
| 970 |
kill_domain_by_device(psdev); |
| 971 |
} |
| 972 |
end: |
| 973 |
if (psdev) |
| 974 |
pcistub_device_put(psdev); |
| 975 |
up_write(&pcistub_sem); |
| 976 |
return result; |
| 977 |
} |
| 978 |
|
| 979 |
/*xen_pcibk_error_resume: it will send the error_resume request to pcifront |
| 980 |
* in case of the device driver could provide this service, and then wait |
| 981 |
* for pcifront ack. |
| 982 |
* @dev: pointer to PCI devices |
| 983 |
*/ |
| 984 |
|
| 985 |
static void xen_pcibk_error_resume(struct pci_dev *dev) |
| 986 |
{ |
| 987 |
struct pcistub_device *psdev; |
| 988 |
|
| 989 |
dev_dbg(&dev->dev, "xen_pcibk_error_resume(bus:%x,devfn:%x)\n", |
| 990 |
dev->bus->number, dev->devfn); |
| 991 |
|
| 992 |
down_write(&pcistub_sem); |
| 993 |
psdev = pcistub_device_find(pci_domain_nr(dev->bus), |
| 994 |
dev->bus->number, |
| 995 |
PCI_SLOT(dev->devfn), |
| 996 |
PCI_FUNC(dev->devfn)); |
| 997 |
|
| 998 |
if (!psdev || !psdev->pdev) { |
| 999 |
dev_err(&dev->dev, "device is not found/assigned\n"); |
| 1000 |
goto end; |
| 1001 |
} |
| 1002 |
|
| 1003 |
if (!psdev->pdev->sh_info) { |
| 1004 |
dev_err(&dev->dev, "device is not connected or owned" |
| 1005 |
" by HVM, kill it\n"); |
| 1006 |
kill_domain_by_device(psdev); |
| 1007 |
goto end; |
| 1008 |
} |
| 1009 |
|
| 1010 |
if (!test_bit(_XEN_PCIB_AERHANDLER, |
| 1011 |
(unsigned long *)&psdev->pdev->sh_info->flags)) { |
| 1012 |
dev_err(&dev->dev, |
| 1013 |
"guest with no AER driver should have been killed\n"); |
| 1014 |
kill_domain_by_device(psdev); |
| 1015 |
goto end; |
| 1016 |
} |
| 1017 |
common_process(psdev, pci_channel_io_normal, XEN_PCI_OP_aer_resume, |
| 1018 |
PCI_ERS_RESULT_RECOVERED); |
| 1019 |
end: |
| 1020 |
if (psdev) |
| 1021 |
pcistub_device_put(psdev); |
| 1022 |
up_write(&pcistub_sem); |
| 1023 |
return; |
| 1024 |
} |
| 1025 |
|
| 1026 |
/*add xen_pcibk AER handling*/ |
| 1027 |
static const struct pci_error_handlers xen_pcibk_error_handler = { |
| 1028 |
.error_detected = xen_pcibk_error_detected, |
| 1029 |
.mmio_enabled = xen_pcibk_mmio_enabled, |
| 1030 |
.slot_reset = xen_pcibk_slot_reset, |
| 1031 |
.resume = xen_pcibk_error_resume, |
| 1032 |
}; |
| 1033 |
|
| 1034 |
/* |
| 1035 |
* Note: There is no MODULE_DEVICE_TABLE entry here because this isn't |
| 1036 |
* for a normal device. I don't want it to be loaded automatically. |
| 1037 |
*/ |
| 1038 |
|
| 1039 |
static struct pci_driver xen_pcibk_pci_driver = { |
| 1040 |
/* The name should be xen_pciback, but until the tools are updated |
| 1041 |
* we will keep it as pciback. */ |
| 1042 |
.name = PCISTUB_DRIVER_NAME, |
| 1043 |
.id_table = pcistub_ids, |
| 1044 |
.probe = pcistub_probe, |
| 1045 |
.remove = pcistub_remove, |
| 1046 |
.err_handler = &xen_pcibk_error_handler, |
| 1047 |
}; |
| 1048 |
|
| 1049 |
static inline int str_to_slot(const char *buf, int *domain, int *bus, |
| 1050 |
int *slot, int *func) |
| 1051 |
{ |
| 1052 |
int parsed = 0; |
| 1053 |
|
| 1054 |
switch (sscanf(buf, " %x:%x:%x.%x %n", domain, bus, slot, func, |
| 1055 |
&parsed)) { |
| 1056 |
case 3: |
| 1057 |
*func = -1; |
| 1058 |
sscanf(buf, " %x:%x:%x.* %n", domain, bus, slot, &parsed); |
| 1059 |
break; |
| 1060 |
case 2: |
| 1061 |
*slot = *func = -1; |
| 1062 |
sscanf(buf, " %x:%x:*.* %n", domain, bus, &parsed); |
| 1063 |
break; |
| 1064 |
} |
| 1065 |
if (parsed && !buf[parsed]) |
| 1066 |
return 0; |
| 1067 |
|
| 1068 |
/* try again without domain */ |
| 1069 |
*domain = 0; |
| 1070 |
switch (sscanf(buf, " %x:%x.%x %n", bus, slot, func, &parsed)) { |
| 1071 |
case 2: |
| 1072 |
*func = -1; |
| 1073 |
sscanf(buf, " %x:%x.* %n", bus, slot, &parsed); |
| 1074 |
break; |
| 1075 |
case 1: |
| 1076 |
*slot = *func = -1; |
| 1077 |
sscanf(buf, " %x:*.* %n", bus, &parsed); |
| 1078 |
break; |
| 1079 |
} |
| 1080 |
if (parsed && !buf[parsed]) |
| 1081 |
return 0; |
| 1082 |
|
| 1083 |
return -EINVAL; |
| 1084 |
} |
| 1085 |
|
| 1086 |
static inline int str_to_quirk(const char *buf, int *domain, int *bus, int |
| 1087 |
*slot, int *func, int *reg, int *size, int *mask) |
| 1088 |
{ |
| 1089 |
int parsed = 0; |
| 1090 |
|
| 1091 |
sscanf(buf, " %x:%x:%x.%x-%x:%x:%x %n", domain, bus, slot, func, |
| 1092 |
reg, size, mask, &parsed); |
| 1093 |
if (parsed && !buf[parsed]) |
| 1094 |
return 0; |
| 1095 |
|
| 1096 |
/* try again without domain */ |
| 1097 |
*domain = 0; |
| 1098 |
sscanf(buf, " %x:%x.%x-%x:%x:%x %n", bus, slot, func, reg, size, |
| 1099 |
mask, &parsed); |
| 1100 |
if (parsed && !buf[parsed]) |
| 1101 |
return 0; |
| 1102 |
|
| 1103 |
return -EINVAL; |
| 1104 |
} |
| 1105 |
|
| 1106 |
static int pcistub_device_id_add(int domain, int bus, int slot, int func) |
| 1107 |
{ |
| 1108 |
struct pcistub_device_id *pci_dev_id; |
| 1109 |
int rc = 0, devfn = PCI_DEVFN(slot, func); |
| 1110 |
|
| 1111 |
if (slot < 0) { |
| 1112 |
for (slot = 0; !rc && slot < 32; ++slot) |
| 1113 |
rc = pcistub_device_id_add(domain, bus, slot, func); |
| 1114 |
return rc; |
| 1115 |
} |
| 1116 |
|
| 1117 |
if (func < 0) { |
| 1118 |
for (func = 0; !rc && func < 8; ++func) |
| 1119 |
rc = pcistub_device_id_add(domain, bus, slot, func); |
| 1120 |
return rc; |
| 1121 |
} |
| 1122 |
|
| 1123 |
if (( |
| 1124 |
#if !defined(MODULE) /* pci_domains_supported is not being exported */ \ |
| 1125 |
|| !defined(CONFIG_PCI_DOMAINS) |
| 1126 |
!pci_domains_supported ? domain : |
| 1127 |
#endif |
| 1128 |
domain < 0 || domain > 0xffff) |
| 1129 |
|| bus < 0 || bus > 0xff |
| 1130 |
|| PCI_SLOT(devfn) != slot |
| 1131 |
|| PCI_FUNC(devfn) != func) |
| 1132 |
return -EINVAL; |
| 1133 |
|
| 1134 |
pci_dev_id = kmalloc_obj(*pci_dev_id); |
| 1135 |
if (!pci_dev_id) |
| 1136 |
return -ENOMEM; |
| 1137 |
|
| 1138 |
pr_debug("wants to seize %04x:%02x:%02x.%d\n", |
| 1139 |
domain, bus, slot, func); |
| 1140 |
|
| 1141 |
pcistub_device_id_add_list(pci_dev_id, domain, bus, devfn); |
| 1142 |
|
| 1143 |
return 0; |
| 1144 |
} |
| 1145 |
|
| 1146 |
static int pcistub_device_id_remove(int domain, int bus, int slot, int func) |
| 1147 |
{ |
| 1148 |
struct pcistub_device_id *pci_dev_id, *t; |
| 1149 |
int err = -ENOENT; |
| 1150 |
unsigned long flags; |
| 1151 |
|
| 1152 |
spin_lock_irqsave(&device_ids_lock, flags); |
| 1153 |
list_for_each_entry_safe(pci_dev_id, t, &pcistub_device_ids, |
| 1154 |
slot_list) { |
| 1155 |
if (pci_dev_id->domain == domain && pci_dev_id->bus == bus |
| 1156 |
&& (slot < 0 || PCI_SLOT(pci_dev_id->devfn) == slot) |
| 1157 |
&& (func < 0 || PCI_FUNC(pci_dev_id->devfn) == func)) { |
| 1158 |
/* Don't break; here because it's possible the same |
| 1159 |
* slot could be in the list more than once |
| 1160 |
*/ |
| 1161 |
list_del(&pci_dev_id->slot_list); |
| 1162 |
kfree(pci_dev_id); |
| 1163 |
|
| 1164 |
err = 0; |
| 1165 |
|
| 1166 |
pr_debug("removed %04x:%02x:%02x.%d from seize list\n", |
| 1167 |
domain, bus, slot, func); |
| 1168 |
} |
| 1169 |
} |
| 1170 |
spin_unlock_irqrestore(&device_ids_lock, flags); |
| 1171 |
|
| 1172 |
return err; |
| 1173 |
} |
| 1174 |
|
| 1175 |
static int pcistub_reg_add(int domain, int bus, int slot, int func, |
| 1176 |
unsigned int reg, unsigned int size, |
| 1177 |
unsigned int mask) |
| 1178 |
{ |
| 1179 |
int err = 0; |
| 1180 |
struct pcistub_device *psdev; |
| 1181 |
struct pci_dev *dev; |
| 1182 |
struct config_field *field; |
| 1183 |
|
| 1184 |
if (reg > 0xfff || (size < 4 && (mask >> (size * 8)))) |
| 1185 |
return -EINVAL; |
| 1186 |
|
| 1187 |
psdev = pcistub_device_find(domain, bus, slot, func); |
| 1188 |
if (!psdev) { |
| 1189 |
err = -ENODEV; |
| 1190 |
goto out; |
| 1191 |
} |
| 1192 |
dev = psdev->dev; |
| 1193 |
|
| 1194 |
field = kzalloc_obj(*field); |
| 1195 |
if (!field) { |
| 1196 |
err = -ENOMEM; |
| 1197 |
goto out; |
| 1198 |
} |
| 1199 |
|
| 1200 |
field->offset = reg; |
| 1201 |
field->size = size; |
| 1202 |
field->mask = mask; |
| 1203 |
field->init = NULL; |
| 1204 |
field->reset = NULL; |
| 1205 |
field->release = NULL; |
| 1206 |
field->clean = xen_pcibk_config_field_free; |
| 1207 |
|
| 1208 |
err = xen_pcibk_config_quirks_add_field(dev, field); |
| 1209 |
if (err) |
| 1210 |
kfree(field); |
| 1211 |
out: |
| 1212 |
if (psdev) |
| 1213 |
pcistub_device_put(psdev); |
| 1214 |
return err; |
| 1215 |
} |
| 1216 |
|
| 1217 |
static ssize_t new_slot_store(struct device_driver *drv, const char *buf, |
| 1218 |
size_t count) |
| 1219 |
{ |
| 1220 |
int domain, bus, slot, func; |
| 1221 |
int err; |
| 1222 |
|
| 1223 |
err = str_to_slot(buf, &domain, &bus, &slot, &func); |
| 1224 |
if (err) |
| 1225 |
goto out; |
| 1226 |
|
| 1227 |
err = pcistub_device_id_add(domain, bus, slot, func); |
| 1228 |
|
| 1229 |
out: |
| 1230 |
if (!err) |
| 1231 |
err = count; |
| 1232 |
return err; |
| 1233 |
} |
| 1234 |
static DRIVER_ATTR_WO(new_slot); |
| 1235 |
|
| 1236 |
static ssize_t remove_slot_store(struct device_driver *drv, const char *buf, |
| 1237 |
size_t count) |
| 1238 |
{ |
| 1239 |
int domain, bus, slot, func; |
| 1240 |
int err; |
| 1241 |
|
| 1242 |
err = str_to_slot(buf, &domain, &bus, &slot, &func); |
| 1243 |
if (err) |
| 1244 |
goto out; |
| 1245 |
|
| 1246 |
err = pcistub_device_id_remove(domain, bus, slot, func); |
| 1247 |
|
| 1248 |
out: |
| 1249 |
if (!err) |
| 1250 |
err = count; |
| 1251 |
return err; |
| 1252 |
} |
| 1253 |
static DRIVER_ATTR_WO(remove_slot); |
| 1254 |
|
| 1255 |
static ssize_t slots_show(struct device_driver *drv, char *buf) |
| 1256 |
{ |
| 1257 |
struct pcistub_device_id *pci_dev_id; |
| 1258 |
size_t count = 0; |
| 1259 |
unsigned long flags; |
| 1260 |
|
| 1261 |
spin_lock_irqsave(&device_ids_lock, flags); |
| 1262 |
list_for_each_entry(pci_dev_id, &pcistub_device_ids, slot_list) { |
| 1263 |
if (count >= PAGE_SIZE) |
| 1264 |
break; |
| 1265 |
|
| 1266 |
count += sysfs_emit_at(buf, count, |
| 1267 |
"%04x:%02x:%02x.%d\n", |
| 1268 |
pci_dev_id->domain, pci_dev_id->bus, |
| 1269 |
PCI_SLOT(pci_dev_id->devfn), |
| 1270 |
PCI_FUNC(pci_dev_id->devfn)); |
| 1271 |
} |
| 1272 |
spin_unlock_irqrestore(&device_ids_lock, flags); |
| 1273 |
|
| 1274 |
return count; |
| 1275 |
} |
| 1276 |
static DRIVER_ATTR_RO(slots); |
| 1277 |
|
| 1278 |
static ssize_t irq_handlers_show(struct device_driver *drv, char *buf) |
| 1279 |
{ |
| 1280 |
struct pcistub_device *psdev; |
| 1281 |
struct xen_pcibk_dev_data *dev_data; |
| 1282 |
size_t count = 0; |
| 1283 |
unsigned long flags; |
| 1284 |
|
| 1285 |
spin_lock_irqsave(&pcistub_devices_lock, flags); |
| 1286 |
list_for_each_entry(psdev, &pcistub_devices, dev_list) { |
| 1287 |
if (count >= PAGE_SIZE) |
| 1288 |
break; |
| 1289 |
if (!psdev->dev) |
| 1290 |
continue; |
| 1291 |
dev_data = pci_get_drvdata(psdev->dev); |
| 1292 |
if (!dev_data) |
| 1293 |
continue; |
| 1294 |
count += |
| 1295 |
sysfs_emit_at(buf, count, |
| 1296 |
"%s:%s:%sing:%ld\n", |
| 1297 |
pci_name(psdev->dev), |
| 1298 |
dev_data->isr_on ? "on" : "off", |
| 1299 |
dev_data->ack_intr ? "ack" : "not ack", |
| 1300 |
dev_data->handled); |
| 1301 |
} |
| 1302 |
spin_unlock_irqrestore(&pcistub_devices_lock, flags); |
| 1303 |
return count; |
| 1304 |
} |
| 1305 |
static DRIVER_ATTR_RO(irq_handlers); |
| 1306 |
|
| 1307 |
static ssize_t irq_handler_state_store(struct device_driver *drv, |
| 1308 |
const char *buf, size_t count) |
| 1309 |
{ |
| 1310 |
struct pcistub_device *psdev; |
| 1311 |
struct xen_pcibk_dev_data *dev_data; |
| 1312 |
int domain, bus, slot, func; |
| 1313 |
int err; |
| 1314 |
|
| 1315 |
err = str_to_slot(buf, &domain, &bus, &slot, &func); |
| 1316 |
if (err) |
| 1317 |
return err; |
| 1318 |
|
| 1319 |
psdev = pcistub_device_find(domain, bus, slot, func); |
| 1320 |
if (!psdev) { |
| 1321 |
err = -ENOENT; |
| 1322 |
goto out; |
| 1323 |
} |
| 1324 |
|
| 1325 |
dev_data = pci_get_drvdata(psdev->dev); |
| 1326 |
if (!dev_data) { |
| 1327 |
err = -ENOENT; |
| 1328 |
goto out; |
| 1329 |
} |
| 1330 |
|
| 1331 |
dev_dbg(&psdev->dev->dev, "%s fake irq handler: %d->%d\n", |
| 1332 |
dev_data->irq_name, dev_data->isr_on, |
| 1333 |
!dev_data->isr_on); |
| 1334 |
|
| 1335 |
dev_data->isr_on = !(dev_data->isr_on); |
| 1336 |
if (dev_data->isr_on) |
| 1337 |
dev_data->ack_intr = 1; |
| 1338 |
out: |
| 1339 |
if (psdev) |
| 1340 |
pcistub_device_put(psdev); |
| 1341 |
if (!err) |
| 1342 |
err = count; |
| 1343 |
return err; |
| 1344 |
} |
| 1345 |
static DRIVER_ATTR_WO(irq_handler_state); |
| 1346 |
|
| 1347 |
static ssize_t quirks_store(struct device_driver *drv, const char *buf, |
| 1348 |
size_t count) |
| 1349 |
{ |
| 1350 |
int domain, bus, slot, func, reg, size, mask; |
| 1351 |
int err; |
| 1352 |
|
| 1353 |
err = str_to_quirk(buf, &domain, &bus, &slot, &func, ®, &size, |
| 1354 |
&mask); |
| 1355 |
if (err) |
| 1356 |
goto out; |
| 1357 |
|
| 1358 |
err = pcistub_reg_add(domain, bus, slot, func, reg, size, mask); |
| 1359 |
|
| 1360 |
out: |
| 1361 |
if (!err) |
| 1362 |
err = count; |
| 1363 |
return err; |
| 1364 |
} |
| 1365 |
|
| 1366 |
static ssize_t quirks_show(struct device_driver *drv, char *buf) |
| 1367 |
{ |
| 1368 |
int count = 0; |
| 1369 |
unsigned long flags; |
| 1370 |
struct xen_pcibk_config_quirk *quirk; |
| 1371 |
struct xen_pcibk_dev_data *dev_data; |
| 1372 |
const struct config_field *field; |
| 1373 |
const struct config_field_entry *cfg_entry; |
| 1374 |
|
| 1375 |
spin_lock_irqsave(&device_ids_lock, flags); |
| 1376 |
list_for_each_entry(quirk, &xen_pcibk_quirks, quirks_list) { |
| 1377 |
if (count >= PAGE_SIZE) |
| 1378 |
goto out; |
| 1379 |
|
| 1380 |
count += sysfs_emit_at(buf, count, |
| 1381 |
"%02x:%02x.%01x\n\t%04x:%04x:%04x:%04x\n", |
| 1382 |
quirk->pdev->bus->number, |
| 1383 |
PCI_SLOT(quirk->pdev->devfn), |
| 1384 |
PCI_FUNC(quirk->pdev->devfn), |
| 1385 |
quirk->devid.vendor, quirk->devid.device, |
| 1386 |
quirk->devid.subvendor, |
| 1387 |
quirk->devid.subdevice); |
| 1388 |
|
| 1389 |
dev_data = pci_get_drvdata(quirk->pdev); |
| 1390 |
|
| 1391 |
list_for_each_entry(cfg_entry, &dev_data->config_fields, list) { |
| 1392 |
field = cfg_entry->field; |
| 1393 |
if (count >= PAGE_SIZE) |
| 1394 |
goto out; |
| 1395 |
|
| 1396 |
count += sysfs_emit_at(buf, count, |
| 1397 |
"\t\t%08x:%01x:%08x\n", |
| 1398 |
cfg_entry->base_offset + |
| 1399 |
field->offset, field->size, |
| 1400 |
field->mask); |
| 1401 |
} |
| 1402 |
} |
| 1403 |
|
| 1404 |
out: |
| 1405 |
spin_unlock_irqrestore(&device_ids_lock, flags); |
| 1406 |
|
| 1407 |
return count; |
| 1408 |
} |
| 1409 |
static DRIVER_ATTR_RW(quirks); |
| 1410 |
|
| 1411 |
static ssize_t permissive_store(struct device_driver *drv, const char *buf, |
| 1412 |
size_t count) |
| 1413 |
{ |
| 1414 |
int domain, bus, slot, func; |
| 1415 |
int err; |
| 1416 |
struct pcistub_device *psdev; |
| 1417 |
struct xen_pcibk_dev_data *dev_data; |
| 1418 |
|
| 1419 |
err = str_to_slot(buf, &domain, &bus, &slot, &func); |
| 1420 |
if (err) |
| 1421 |
goto out; |
| 1422 |
|
| 1423 |
psdev = pcistub_device_find(domain, bus, slot, func); |
| 1424 |
if (!psdev) { |
| 1425 |
err = -ENODEV; |
| 1426 |
goto out; |
| 1427 |
} |
| 1428 |
|
| 1429 |
dev_data = pci_get_drvdata(psdev->dev); |
| 1430 |
/* the driver data for a device should never be null at this point */ |
| 1431 |
if (!dev_data) { |
| 1432 |
err = -ENXIO; |
| 1433 |
goto release; |
| 1434 |
} |
| 1435 |
if (!dev_data->permissive) { |
| 1436 |
dev_data->permissive = 1; |
| 1437 |
/* Let user know that what they're doing could be unsafe */ |
| 1438 |
dev_warn(&psdev->dev->dev, "enabling permissive mode " |
| 1439 |
"configuration space accesses!\n"); |
| 1440 |
dev_warn(&psdev->dev->dev, |
| 1441 |
"permissive mode is potentially unsafe!\n"); |
| 1442 |
} |
| 1443 |
release: |
| 1444 |
pcistub_device_put(psdev); |
| 1445 |
out: |
| 1446 |
if (!err) |
| 1447 |
err = count; |
| 1448 |
return err; |
| 1449 |
} |
| 1450 |
|
| 1451 |
static ssize_t permissive_show(struct device_driver *drv, char *buf) |
| 1452 |
{ |
| 1453 |
struct pcistub_device *psdev; |
| 1454 |
struct xen_pcibk_dev_data *dev_data; |
| 1455 |
size_t count = 0; |
| 1456 |
unsigned long flags; |
| 1457 |
spin_lock_irqsave(&pcistub_devices_lock, flags); |
| 1458 |
list_for_each_entry(psdev, &pcistub_devices, dev_list) { |
| 1459 |
if (count >= PAGE_SIZE) |
| 1460 |
break; |
| 1461 |
if (!psdev->dev) |
| 1462 |
continue; |
| 1463 |
dev_data = pci_get_drvdata(psdev->dev); |
| 1464 |
if (!dev_data || !dev_data->permissive) |
| 1465 |
continue; |
| 1466 |
count += |
| 1467 |
sysfs_emit_at(buf, count, "%s\n", |
| 1468 |
pci_name(psdev->dev)); |
| 1469 |
} |
| 1470 |
spin_unlock_irqrestore(&pcistub_devices_lock, flags); |
| 1471 |
return count; |
| 1472 |
} |
| 1473 |
static DRIVER_ATTR_RW(permissive); |
| 1474 |
|
| 1475 |
static ssize_t allow_interrupt_control_store(struct device_driver *drv, |
| 1476 |
const char *buf, size_t count) |
| 1477 |
{ |
| 1478 |
int domain, bus, slot, func; |
| 1479 |
int err; |
| 1480 |
struct pcistub_device *psdev; |
| 1481 |
struct xen_pcibk_dev_data *dev_data; |
| 1482 |
|
| 1483 |
err = str_to_slot(buf, &domain, &bus, &slot, &func); |
| 1484 |
if (err) |
| 1485 |
goto out; |
| 1486 |
|
| 1487 |
psdev = pcistub_device_find(domain, bus, slot, func); |
| 1488 |
if (!psdev) { |
| 1489 |
err = -ENODEV; |
| 1490 |
goto out; |
| 1491 |
} |
| 1492 |
|
| 1493 |
dev_data = pci_get_drvdata(psdev->dev); |
| 1494 |
/* the driver data for a device should never be null at this point */ |
| 1495 |
if (!dev_data) { |
| 1496 |
err = -ENXIO; |
| 1497 |
goto release; |
| 1498 |
} |
| 1499 |
dev_data->allow_interrupt_control = 1; |
| 1500 |
release: |
| 1501 |
pcistub_device_put(psdev); |
| 1502 |
out: |
| 1503 |
if (!err) |
| 1504 |
err = count; |
| 1505 |
return err; |
| 1506 |
} |
| 1507 |
|
| 1508 |
static ssize_t allow_interrupt_control_show(struct device_driver *drv, |
| 1509 |
char *buf) |
| 1510 |
{ |
| 1511 |
struct pcistub_device *psdev; |
| 1512 |
struct xen_pcibk_dev_data *dev_data; |
| 1513 |
size_t count = 0; |
| 1514 |
unsigned long flags; |
| 1515 |
|
| 1516 |
spin_lock_irqsave(&pcistub_devices_lock, flags); |
| 1517 |
list_for_each_entry(psdev, &pcistub_devices, dev_list) { |
| 1518 |
if (count >= PAGE_SIZE) |
| 1519 |
break; |
| 1520 |
if (!psdev->dev) |
| 1521 |
continue; |
| 1522 |
dev_data = pci_get_drvdata(psdev->dev); |
| 1523 |
if (!dev_data || !dev_data->allow_interrupt_control) |
| 1524 |
continue; |
| 1525 |
count += |
| 1526 |
sysfs_emit_at(buf, count, "%s\n", |
| 1527 |
pci_name(psdev->dev)); |
| 1528 |
} |
| 1529 |
spin_unlock_irqrestore(&pcistub_devices_lock, flags); |
| 1530 |
return count; |
| 1531 |
} |
| 1532 |
static DRIVER_ATTR_RW(allow_interrupt_control); |
| 1533 |
|
| 1534 |
static void pcistub_exit(void) |
| 1535 |
{ |
| 1536 |
driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_new_slot); |
| 1537 |
driver_remove_file(&xen_pcibk_pci_driver.driver, |
| 1538 |
&driver_attr_remove_slot); |
| 1539 |
driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_slots); |
| 1540 |
driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_quirks); |
| 1541 |
driver_remove_file(&xen_pcibk_pci_driver.driver, |
| 1542 |
&driver_attr_permissive); |
| 1543 |
driver_remove_file(&xen_pcibk_pci_driver.driver, |
| 1544 |
&driver_attr_allow_interrupt_control); |
| 1545 |
driver_remove_file(&xen_pcibk_pci_driver.driver, |
| 1546 |
&driver_attr_irq_handlers); |
| 1547 |
driver_remove_file(&xen_pcibk_pci_driver.driver, |
| 1548 |
&driver_attr_irq_handler_state); |
| 1549 |
pci_unregister_driver(&xen_pcibk_pci_driver); |
| 1550 |
} |
| 1551 |
|
| 1552 |
static int __init pcistub_init(void) |
| 1553 |
{ |
| 1554 |
int pos = 0; |
| 1555 |
int err = 0; |
| 1556 |
int domain, bus, slot, func; |
| 1557 |
int parsed; |
| 1558 |
|
| 1559 |
if (pci_devs_to_hide && *pci_devs_to_hide) { |
| 1560 |
do { |
| 1561 |
parsed = 0; |
| 1562 |
|
| 1563 |
err = sscanf(pci_devs_to_hide + pos, |
| 1564 |
" (%x:%x:%x.%x) %n", |
| 1565 |
&domain, &bus, &slot, &func, &parsed); |
| 1566 |
switch (err) { |
| 1567 |
case 3: |
| 1568 |
func = -1; |
| 1569 |
sscanf(pci_devs_to_hide + pos, |
| 1570 |
" (%x:%x:%x.*) %n", |
| 1571 |
&domain, &bus, &slot, &parsed); |
| 1572 |
break; |
| 1573 |
case 2: |
| 1574 |
slot = func = -1; |
| 1575 |
sscanf(pci_devs_to_hide + pos, |
| 1576 |
" (%x:%x:*.*) %n", |
| 1577 |
&domain, &bus, &parsed); |
| 1578 |
break; |
| 1579 |
} |
| 1580 |
|
| 1581 |
if (!parsed) { |
| 1582 |
domain = 0; |
| 1583 |
err = sscanf(pci_devs_to_hide + pos, |
| 1584 |
" (%x:%x.%x) %n", |
| 1585 |
&bus, &slot, &func, &parsed); |
| 1586 |
switch (err) { |
| 1587 |
case 2: |
| 1588 |
func = -1; |
| 1589 |
sscanf(pci_devs_to_hide + pos, |
| 1590 |
" (%x:%x.*) %n", |
| 1591 |
&bus, &slot, &parsed); |
| 1592 |
break; |
| 1593 |
case 1: |
| 1594 |
slot = func = -1; |
| 1595 |
sscanf(pci_devs_to_hide + pos, |
| 1596 |
" (%x:*.*) %n", |
| 1597 |
&bus, &parsed); |
| 1598 |
break; |
| 1599 |
} |
| 1600 |
} |
| 1601 |
|
| 1602 |
if (parsed <= 0) |
| 1603 |
goto parse_error; |
| 1604 |
|
| 1605 |
err = pcistub_device_id_add(domain, bus, slot, func); |
| 1606 |
if (err) |
| 1607 |
goto out; |
| 1608 |
|
| 1609 |
pos += parsed; |
| 1610 |
} while (pci_devs_to_hide[pos]); |
| 1611 |
} |
| 1612 |
|
| 1613 |
/* If we're the first PCI Device Driver to register, we're the |
| 1614 |
* first one to get offered PCI devices as they become |
| 1615 |
* available (and thus we can be the first to grab them) |
| 1616 |
*/ |
| 1617 |
err = pci_register_driver(&xen_pcibk_pci_driver); |
| 1618 |
if (err < 0) |
| 1619 |
goto out; |
| 1620 |
|
| 1621 |
err = driver_create_file(&xen_pcibk_pci_driver.driver, |
| 1622 |
&driver_attr_new_slot); |
| 1623 |
if (!err) |
| 1624 |
err = driver_create_file(&xen_pcibk_pci_driver.driver, |
| 1625 |
&driver_attr_remove_slot); |
| 1626 |
if (!err) |
| 1627 |
err = driver_create_file(&xen_pcibk_pci_driver.driver, |
| 1628 |
&driver_attr_slots); |
| 1629 |
if (!err) |
| 1630 |
err = driver_create_file(&xen_pcibk_pci_driver.driver, |
| 1631 |
&driver_attr_quirks); |
| 1632 |
if (!err) |
| 1633 |
err = driver_create_file(&xen_pcibk_pci_driver.driver, |
| 1634 |
&driver_attr_permissive); |
| 1635 |
if (!err) |
| 1636 |
err = driver_create_file(&xen_pcibk_pci_driver.driver, |
| 1637 |
&driver_attr_allow_interrupt_control); |
| 1638 |
|
| 1639 |
if (!err) |
| 1640 |
err = driver_create_file(&xen_pcibk_pci_driver.driver, |
| 1641 |
&driver_attr_irq_handlers); |
| 1642 |
if (!err) |
| 1643 |
err = driver_create_file(&xen_pcibk_pci_driver.driver, |
| 1644 |
&driver_attr_irq_handler_state); |
| 1645 |
if (err) |
| 1646 |
pcistub_exit(); |
| 1647 |
|
| 1648 |
out: |
| 1649 |
return err; |
| 1650 |
|
| 1651 |
parse_error: |
| 1652 |
pr_err("Error parsing pci_devs_to_hide at \"%s\"\n", |
| 1653 |
pci_devs_to_hide + pos); |
| 1654 |
return -EINVAL; |
| 1655 |
} |
| 1656 |
|
| 1657 |
#ifndef MODULE |
| 1658 |
/* |
| 1659 |
* fs_initcall happens before device_initcall |
| 1660 |
* so xen_pcibk *should* get called first (b/c we |
| 1661 |
* want to suck up any device before other drivers |
| 1662 |
* get a chance by being the first pci device |
| 1663 |
* driver to register) |
| 1664 |
*/ |
| 1665 |
fs_initcall(pcistub_init); |
| 1666 |
#endif |
| 1667 |
|
| 1668 |
#ifdef CONFIG_PCI_IOV |
| 1669 |
static struct pcistub_device *find_vfs(const struct pci_dev *pdev) |
| 1670 |
{ |
| 1671 |
struct pcistub_device *psdev = NULL; |
| 1672 |
unsigned long flags; |
| 1673 |
bool found = false; |
| 1674 |
|
| 1675 |
spin_lock_irqsave(&pcistub_devices_lock, flags); |
| 1676 |
list_for_each_entry(psdev, &pcistub_devices, dev_list) { |
| 1677 |
if (!psdev->pdev && psdev->dev != pdev |
| 1678 |
&& pci_physfn(psdev->dev) == pdev) { |
| 1679 |
found = true; |
| 1680 |
break; |
| 1681 |
} |
| 1682 |
} |
| 1683 |
spin_unlock_irqrestore(&pcistub_devices_lock, flags); |
| 1684 |
if (found) |
| 1685 |
return psdev; |
| 1686 |
return NULL; |
| 1687 |
} |
| 1688 |
|
| 1689 |
static int pci_stub_notifier(struct notifier_block *nb, |
| 1690 |
unsigned long action, void *data) |
| 1691 |
{ |
| 1692 |
struct device *dev = data; |
| 1693 |
const struct pci_dev *pdev = to_pci_dev(dev); |
| 1694 |
|
| 1695 |
if (action != BUS_NOTIFY_UNBIND_DRIVER) |
| 1696 |
return NOTIFY_DONE; |
| 1697 |
|
| 1698 |
if (!pdev->is_physfn) |
| 1699 |
return NOTIFY_DONE; |
| 1700 |
|
| 1701 |
for (;;) { |
| 1702 |
struct pcistub_device *psdev = find_vfs(pdev); |
| 1703 |
if (!psdev) |
| 1704 |
break; |
| 1705 |
device_release_driver(&psdev->dev->dev); |
| 1706 |
} |
| 1707 |
return NOTIFY_DONE; |
| 1708 |
} |
| 1709 |
|
| 1710 |
static struct notifier_block pci_stub_nb = { |
| 1711 |
.notifier_call = pci_stub_notifier, |
| 1712 |
}; |
| 1713 |
#endif |
| 1714 |
|
| 1715 |
static int __init xen_pcibk_init(void) |
| 1716 |
{ |
| 1717 |
int err; |
| 1718 |
|
| 1719 |
if (!xen_initial_domain()) |
| 1720 |
return -ENODEV; |
| 1721 |
|
| 1722 |
err = xen_pcibk_config_init(); |
| 1723 |
if (err) |
| 1724 |
return err; |
| 1725 |
|
| 1726 |
#ifdef MODULE |
| 1727 |
err = pcistub_init(); |
| 1728 |
if (err < 0) |
| 1729 |
return err; |
| 1730 |
#endif |
| 1731 |
|
| 1732 |
pcistub_init_devices_late(); |
| 1733 |
err = xen_pcibk_xenbus_register(); |
| 1734 |
if (err) |
| 1735 |
pcistub_exit(); |
| 1736 |
#ifdef CONFIG_PCI_IOV |
| 1737 |
else |
| 1738 |
bus_register_notifier(&pci_bus_type, &pci_stub_nb); |
| 1739 |
#endif |
| 1740 |
|
| 1741 |
#ifdef CONFIG_XEN_ACPI |
| 1742 |
xen_acpi_register_get_gsi_func(pcistub_get_gsi_from_sbdf); |
| 1743 |
#endif |
| 1744 |
|
| 1745 |
return err; |
| 1746 |
} |
| 1747 |
|
| 1748 |
static void __exit xen_pcibk_cleanup(void) |
| 1749 |
{ |
| 1750 |
#ifdef CONFIG_XEN_ACPI |
| 1751 |
xen_acpi_register_get_gsi_func(NULL); |
| 1752 |
#endif |
| 1753 |
|
| 1754 |
#ifdef CONFIG_PCI_IOV |
| 1755 |
bus_unregister_notifier(&pci_bus_type, &pci_stub_nb); |
| 1756 |
#endif |
| 1757 |
xen_pcibk_xenbus_unregister(); |
| 1758 |
pcistub_exit(); |
| 1759 |
} |
| 1760 |
|
| 1761 |
module_init(xen_pcibk_init); |
| 1762 |
module_exit(xen_pcibk_cleanup); |
| 1763 |
|
| 1764 |
MODULE_DESCRIPTION("Xen PCI-device stub driver"); |
| 1765 |
MODULE_LICENSE("Dual BSD/GPL"); |
| 1766 |
MODULE_ALIAS("xen-backend:pci"); |