Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1 | /* |
| 2 | * PCI Stub Driver - Grabs devices in backend to be exported later |
| 3 | * |
| 4 | * Ryan Wilson <hap9@epoch.ncsc.mil> |
| 5 | * Chris Bookholt <hap10@epoch.ncsc.mil> |
| 6 | */ |
| 7 | #include <linux/module.h> |
| 8 | #include <linux/init.h> |
| 9 | #include <linux/rwsem.h> |
| 10 | #include <linux/list.h> |
| 11 | #include <linux/spinlock.h> |
| 12 | #include <linux/kref.h> |
| 13 | #include <linux/pci.h> |
| 14 | #include <linux/wait.h> |
| 15 | #include <linux/sched.h> |
Konrad Rzeszutek Wilk | 8bfd4e0 | 2011-07-19 20:09:43 -0400 | [diff] [blame] | 16 | #include <linux/atomic.h> |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 17 | #include <xen/events.h> |
| 18 | #include <asm/xen/pci.h> |
| 19 | #include <asm/xen/hypervisor.h> |
| 20 | #include "pciback.h" |
| 21 | #include "conf_space.h" |
| 22 | #include "conf_space_quirks.h" |
| 23 | |
| 24 | static char *pci_devs_to_hide; |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 25 | wait_queue_head_t xen_pcibk_aer_wait_queue; |
| 26 | /*Add sem for sync AER handling and xen_pcibk remove/reconfigue ops, |
| 27 | * We want to avoid in middle of AER ops, xen_pcibk devices is being removed |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 28 | */ |
| 29 | static DECLARE_RWSEM(pcistub_sem); |
| 30 | module_param_named(hide, pci_devs_to_hide, charp, 0444); |
| 31 | |
| 32 | struct pcistub_device_id { |
| 33 | struct list_head slot_list; |
| 34 | int domain; |
| 35 | unsigned char bus; |
| 36 | unsigned int devfn; |
| 37 | }; |
| 38 | static LIST_HEAD(pcistub_device_ids); |
| 39 | static DEFINE_SPINLOCK(device_ids_lock); |
| 40 | |
| 41 | struct pcistub_device { |
| 42 | struct kref kref; |
| 43 | struct list_head dev_list; |
| 44 | spinlock_t lock; |
| 45 | |
| 46 | struct pci_dev *dev; |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 47 | struct xen_pcibk_device *pdev;/* non-NULL if struct pci_dev is in use */ |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | /* Access to pcistub_devices & seized_devices lists and the initialize_devices |
| 51 | * flag must be locked with pcistub_devices_lock |
| 52 | */ |
| 53 | static DEFINE_SPINLOCK(pcistub_devices_lock); |
| 54 | static LIST_HEAD(pcistub_devices); |
| 55 | |
| 56 | /* wait for device_initcall before initializing our devices |
| 57 | * (see pcistub_init_devices_late) |
| 58 | */ |
| 59 | static int initialize_devices; |
| 60 | static LIST_HEAD(seized_devices); |
| 61 | |
| 62 | static struct pcistub_device *pcistub_device_alloc(struct pci_dev *dev) |
| 63 | { |
| 64 | struct pcistub_device *psdev; |
| 65 | |
| 66 | dev_dbg(&dev->dev, "pcistub_device_alloc\n"); |
| 67 | |
| 68 | psdev = kzalloc(sizeof(*psdev), GFP_ATOMIC); |
| 69 | if (!psdev) |
| 70 | return NULL; |
| 71 | |
| 72 | psdev->dev = pci_dev_get(dev); |
| 73 | if (!psdev->dev) { |
| 74 | kfree(psdev); |
| 75 | return NULL; |
| 76 | } |
| 77 | |
| 78 | kref_init(&psdev->kref); |
| 79 | spin_lock_init(&psdev->lock); |
| 80 | |
| 81 | return psdev; |
| 82 | } |
| 83 | |
| 84 | /* Don't call this directly as it's called by pcistub_device_put */ |
| 85 | static void pcistub_device_release(struct kref *kref) |
| 86 | { |
| 87 | struct pcistub_device *psdev; |
| 88 | |
| 89 | psdev = container_of(kref, struct pcistub_device, kref); |
| 90 | |
| 91 | dev_dbg(&psdev->dev->dev, "pcistub_device_release\n"); |
| 92 | |
Konrad Rzeszutek Wilk | 6221a9b | 2009-12-09 17:43:15 -0500 | [diff] [blame] | 93 | xen_unregister_device_domain_owner(psdev->dev); |
| 94 | |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 95 | /* Clean-up the device */ |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 96 | xen_pcibk_reset_device(psdev->dev); |
| 97 | xen_pcibk_config_free_dyn_fields(psdev->dev); |
| 98 | xen_pcibk_config_free_dev(psdev->dev); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 99 | kfree(pci_get_drvdata(psdev->dev)); |
| 100 | pci_set_drvdata(psdev->dev, NULL); |
| 101 | |
Konrad Rzeszutek Wilk | 97309d3 | 2012-01-04 14:10:32 -0500 | [diff] [blame] | 102 | psdev->dev->dev_flags &= ~PCI_DEV_FLAGS_ASSIGNED; |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 103 | pci_dev_put(psdev->dev); |
| 104 | |
| 105 | kfree(psdev); |
| 106 | } |
| 107 | |
| 108 | static inline void pcistub_device_get(struct pcistub_device *psdev) |
| 109 | { |
| 110 | kref_get(&psdev->kref); |
| 111 | } |
| 112 | |
| 113 | static inline void pcistub_device_put(struct pcistub_device *psdev) |
| 114 | { |
| 115 | kref_put(&psdev->kref, pcistub_device_release); |
| 116 | } |
| 117 | |
| 118 | static struct pcistub_device *pcistub_device_find(int domain, int bus, |
| 119 | int slot, int func) |
| 120 | { |
| 121 | struct pcistub_device *psdev = NULL; |
| 122 | unsigned long flags; |
| 123 | |
| 124 | spin_lock_irqsave(&pcistub_devices_lock, flags); |
| 125 | |
| 126 | list_for_each_entry(psdev, &pcistub_devices, dev_list) { |
| 127 | if (psdev->dev != NULL |
| 128 | && domain == pci_domain_nr(psdev->dev->bus) |
| 129 | && bus == psdev->dev->bus->number |
| 130 | && PCI_DEVFN(slot, func) == psdev->dev->devfn) { |
| 131 | pcistub_device_get(psdev); |
| 132 | goto out; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | /* didn't find it */ |
| 137 | psdev = NULL; |
| 138 | |
| 139 | out: |
| 140 | spin_unlock_irqrestore(&pcistub_devices_lock, flags); |
| 141 | return psdev; |
| 142 | } |
| 143 | |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 144 | static struct pci_dev *pcistub_device_get_pci_dev(struct xen_pcibk_device *pdev, |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 145 | struct pcistub_device *psdev) |
| 146 | { |
| 147 | struct pci_dev *pci_dev = NULL; |
| 148 | unsigned long flags; |
| 149 | |
| 150 | pcistub_device_get(psdev); |
| 151 | |
| 152 | spin_lock_irqsave(&psdev->lock, flags); |
| 153 | if (!psdev->pdev) { |
| 154 | psdev->pdev = pdev; |
| 155 | pci_dev = psdev->dev; |
| 156 | } |
| 157 | spin_unlock_irqrestore(&psdev->lock, flags); |
| 158 | |
| 159 | if (!pci_dev) |
| 160 | pcistub_device_put(psdev); |
| 161 | |
| 162 | return pci_dev; |
| 163 | } |
| 164 | |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 165 | struct pci_dev *pcistub_get_pci_dev_by_slot(struct xen_pcibk_device *pdev, |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 166 | int domain, int bus, |
| 167 | int slot, int func) |
| 168 | { |
| 169 | struct pcistub_device *psdev; |
| 170 | struct pci_dev *found_dev = NULL; |
| 171 | unsigned long flags; |
| 172 | |
| 173 | spin_lock_irqsave(&pcistub_devices_lock, flags); |
| 174 | |
| 175 | list_for_each_entry(psdev, &pcistub_devices, dev_list) { |
| 176 | if (psdev->dev != NULL |
| 177 | && domain == pci_domain_nr(psdev->dev->bus) |
| 178 | && bus == psdev->dev->bus->number |
| 179 | && PCI_DEVFN(slot, func) == psdev->dev->devfn) { |
| 180 | found_dev = pcistub_device_get_pci_dev(pdev, psdev); |
| 181 | break; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | spin_unlock_irqrestore(&pcistub_devices_lock, flags); |
| 186 | return found_dev; |
| 187 | } |
| 188 | |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 189 | struct pci_dev *pcistub_get_pci_dev(struct xen_pcibk_device *pdev, |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 190 | struct pci_dev *dev) |
| 191 | { |
| 192 | struct pcistub_device *psdev; |
| 193 | struct pci_dev *found_dev = NULL; |
| 194 | unsigned long flags; |
| 195 | |
| 196 | spin_lock_irqsave(&pcistub_devices_lock, flags); |
| 197 | |
| 198 | list_for_each_entry(psdev, &pcistub_devices, dev_list) { |
| 199 | if (psdev->dev == dev) { |
| 200 | found_dev = pcistub_device_get_pci_dev(pdev, psdev); |
| 201 | break; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | spin_unlock_irqrestore(&pcistub_devices_lock, flags); |
| 206 | return found_dev; |
| 207 | } |
| 208 | |
| 209 | void pcistub_put_pci_dev(struct pci_dev *dev) |
| 210 | { |
| 211 | struct pcistub_device *psdev, *found_psdev = NULL; |
| 212 | unsigned long flags; |
| 213 | |
| 214 | spin_lock_irqsave(&pcistub_devices_lock, flags); |
| 215 | |
| 216 | list_for_each_entry(psdev, &pcistub_devices, dev_list) { |
| 217 | if (psdev->dev == dev) { |
| 218 | found_psdev = psdev; |
| 219 | break; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | spin_unlock_irqrestore(&pcistub_devices_lock, flags); |
Konrad Rzeszutek Wilk | 4645bf3 | 2011-09-29 13:12:43 -0400 | [diff] [blame] | 224 | if (WARN_ON(!found_psdev)) |
| 225 | return; |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 226 | |
| 227 | /*hold this lock for avoiding breaking link between |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 228 | * pcistub and xen_pcibk when AER is in processing |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 229 | */ |
| 230 | down_write(&pcistub_sem); |
| 231 | /* Cleanup our device |
| 232 | * (so it's ready for the next domain) |
| 233 | */ |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 234 | xen_pcibk_reset_device(found_psdev->dev); |
| 235 | xen_pcibk_config_free_dyn_fields(found_psdev->dev); |
| 236 | xen_pcibk_config_reset_dev(found_psdev->dev); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 237 | |
Konrad Rzeszutek Wilk | 3167355 | 2012-01-04 15:11:02 -0500 | [diff] [blame^] | 238 | xen_unregister_device_domain_owner(found_psdev->dev); |
| 239 | |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 240 | spin_lock_irqsave(&found_psdev->lock, flags); |
| 241 | found_psdev->pdev = NULL; |
| 242 | spin_unlock_irqrestore(&found_psdev->lock, flags); |
| 243 | |
| 244 | pcistub_device_put(found_psdev); |
| 245 | up_write(&pcistub_sem); |
| 246 | } |
| 247 | |
| 248 | static int __devinit pcistub_match_one(struct pci_dev *dev, |
| 249 | struct pcistub_device_id *pdev_id) |
| 250 | { |
| 251 | /* Match the specified device by domain, bus, slot, func and also if |
| 252 | * any of the device's parent bridges match. |
| 253 | */ |
| 254 | for (; dev != NULL; dev = dev->bus->self) { |
| 255 | if (pci_domain_nr(dev->bus) == pdev_id->domain |
| 256 | && dev->bus->number == pdev_id->bus |
| 257 | && dev->devfn == pdev_id->devfn) |
| 258 | return 1; |
| 259 | |
| 260 | /* Sometimes topmost bridge links to itself. */ |
| 261 | if (dev == dev->bus->self) |
| 262 | break; |
| 263 | } |
| 264 | |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | static int __devinit pcistub_match(struct pci_dev *dev) |
| 269 | { |
| 270 | struct pcistub_device_id *pdev_id; |
| 271 | unsigned long flags; |
| 272 | int found = 0; |
| 273 | |
| 274 | spin_lock_irqsave(&device_ids_lock, flags); |
| 275 | list_for_each_entry(pdev_id, &pcistub_device_ids, slot_list) { |
| 276 | if (pcistub_match_one(dev, pdev_id)) { |
| 277 | found = 1; |
| 278 | break; |
| 279 | } |
| 280 | } |
| 281 | spin_unlock_irqrestore(&device_ids_lock, flags); |
| 282 | |
| 283 | return found; |
| 284 | } |
| 285 | |
| 286 | static int __devinit pcistub_init_device(struct pci_dev *dev) |
| 287 | { |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 288 | struct xen_pcibk_dev_data *dev_data; |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 289 | int err = 0; |
| 290 | |
| 291 | dev_dbg(&dev->dev, "initializing...\n"); |
| 292 | |
| 293 | /* The PCI backend is not intended to be a module (or to work with |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 294 | * removable PCI devices (yet). If it were, xen_pcibk_config_free() |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 295 | * would need to be called somewhere to free the memory allocated |
| 296 | * here and then to call kfree(pci_get_drvdata(psdev->dev)). |
| 297 | */ |
Konrad Rzeszutek Wilk | 0513fe9 | 2011-07-19 18:56:39 -0400 | [diff] [blame] | 298 | dev_data = kzalloc(sizeof(*dev_data) + strlen(DRV_NAME "[]") |
| 299 | + strlen(pci_name(dev)) + 1, GFP_ATOMIC); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 300 | if (!dev_data) { |
| 301 | err = -ENOMEM; |
| 302 | goto out; |
| 303 | } |
| 304 | pci_set_drvdata(dev, dev_data); |
| 305 | |
Konrad Rzeszutek Wilk | 0513fe9 | 2011-07-19 18:56:39 -0400 | [diff] [blame] | 306 | /* |
| 307 | * Setup name for fake IRQ handler. It will only be enabled |
| 308 | * once the device is turned on by the guest. |
| 309 | */ |
| 310 | sprintf(dev_data->irq_name, DRV_NAME "[%s]", pci_name(dev)); |
| 311 | |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 312 | dev_dbg(&dev->dev, "initializing config\n"); |
| 313 | |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 314 | init_waitqueue_head(&xen_pcibk_aer_wait_queue); |
| 315 | err = xen_pcibk_config_init_dev(dev); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 316 | if (err) |
| 317 | goto out; |
| 318 | |
| 319 | /* HACK: Force device (& ACPI) to determine what IRQ it's on - we |
| 320 | * must do this here because pcibios_enable_device may specify |
| 321 | * the pci device's true irq (and possibly its other resources) |
| 322 | * if they differ from what's in the configuration space. |
| 323 | * This makes the assumption that the device's resources won't |
| 324 | * change after this point (otherwise this code may break!) |
| 325 | */ |
| 326 | dev_dbg(&dev->dev, "enabling device\n"); |
| 327 | err = pci_enable_device(dev); |
| 328 | if (err) |
| 329 | goto config_release; |
| 330 | |
| 331 | /* Now disable the device (this also ensures some private device |
| 332 | * data is setup before we export) |
| 333 | */ |
| 334 | dev_dbg(&dev->dev, "reset device\n"); |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 335 | xen_pcibk_reset_device(dev); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 336 | |
Konrad Rzeszutek Wilk | 97309d3 | 2012-01-04 14:10:32 -0500 | [diff] [blame] | 337 | dev->dev_flags |= PCI_DEV_FLAGS_ASSIGNED; |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 338 | return 0; |
| 339 | |
| 340 | config_release: |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 341 | xen_pcibk_config_free_dev(dev); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 342 | |
| 343 | out: |
| 344 | pci_set_drvdata(dev, NULL); |
| 345 | kfree(dev_data); |
| 346 | return err; |
| 347 | } |
| 348 | |
| 349 | /* |
| 350 | * Because some initialization still happens on |
| 351 | * devices during fs_initcall, we need to defer |
| 352 | * full initialization of our devices until |
| 353 | * device_initcall. |
| 354 | */ |
| 355 | static int __init pcistub_init_devices_late(void) |
| 356 | { |
| 357 | struct pcistub_device *psdev; |
| 358 | unsigned long flags; |
| 359 | int err = 0; |
| 360 | |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 361 | pr_debug(DRV_NAME ": pcistub_init_devices_late\n"); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 362 | |
| 363 | spin_lock_irqsave(&pcistub_devices_lock, flags); |
| 364 | |
| 365 | while (!list_empty(&seized_devices)) { |
| 366 | psdev = container_of(seized_devices.next, |
| 367 | struct pcistub_device, dev_list); |
| 368 | list_del(&psdev->dev_list); |
| 369 | |
| 370 | spin_unlock_irqrestore(&pcistub_devices_lock, flags); |
| 371 | |
| 372 | err = pcistub_init_device(psdev->dev); |
| 373 | if (err) { |
| 374 | dev_err(&psdev->dev->dev, |
| 375 | "error %d initializing device\n", err); |
| 376 | kfree(psdev); |
| 377 | psdev = NULL; |
| 378 | } |
| 379 | |
| 380 | spin_lock_irqsave(&pcistub_devices_lock, flags); |
| 381 | |
| 382 | if (psdev) |
| 383 | list_add_tail(&psdev->dev_list, &pcistub_devices); |
| 384 | } |
| 385 | |
| 386 | initialize_devices = 1; |
| 387 | |
| 388 | spin_unlock_irqrestore(&pcistub_devices_lock, flags); |
| 389 | |
| 390 | return 0; |
| 391 | } |
| 392 | |
| 393 | static int __devinit pcistub_seize(struct pci_dev *dev) |
| 394 | { |
| 395 | struct pcistub_device *psdev; |
| 396 | unsigned long flags; |
| 397 | int err = 0; |
| 398 | |
| 399 | psdev = pcistub_device_alloc(dev); |
| 400 | if (!psdev) |
| 401 | return -ENOMEM; |
| 402 | |
| 403 | spin_lock_irqsave(&pcistub_devices_lock, flags); |
| 404 | |
| 405 | if (initialize_devices) { |
| 406 | spin_unlock_irqrestore(&pcistub_devices_lock, flags); |
| 407 | |
| 408 | /* don't want irqs disabled when calling pcistub_init_device */ |
| 409 | err = pcistub_init_device(psdev->dev); |
| 410 | |
| 411 | spin_lock_irqsave(&pcistub_devices_lock, flags); |
| 412 | |
| 413 | if (!err) |
| 414 | list_add(&psdev->dev_list, &pcistub_devices); |
| 415 | } else { |
| 416 | dev_dbg(&dev->dev, "deferring initialization\n"); |
| 417 | list_add(&psdev->dev_list, &seized_devices); |
| 418 | } |
| 419 | |
| 420 | spin_unlock_irqrestore(&pcistub_devices_lock, flags); |
| 421 | |
| 422 | if (err) |
| 423 | pcistub_device_put(psdev); |
| 424 | |
| 425 | return err; |
| 426 | } |
| 427 | |
| 428 | static int __devinit pcistub_probe(struct pci_dev *dev, |
| 429 | const struct pci_device_id *id) |
| 430 | { |
| 431 | int err = 0; |
| 432 | |
| 433 | dev_dbg(&dev->dev, "probing...\n"); |
| 434 | |
| 435 | if (pcistub_match(dev)) { |
| 436 | |
| 437 | if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL |
| 438 | && dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) { |
| 439 | dev_err(&dev->dev, "can't export pci devices that " |
| 440 | "don't have a normal (0) or bridge (1) " |
| 441 | "header type!\n"); |
| 442 | err = -ENODEV; |
| 443 | goto out; |
| 444 | } |
| 445 | |
| 446 | dev_info(&dev->dev, "seizing device\n"); |
| 447 | err = pcistub_seize(dev); |
| 448 | } else |
| 449 | /* Didn't find the device */ |
| 450 | err = -ENODEV; |
| 451 | |
| 452 | out: |
| 453 | return err; |
| 454 | } |
| 455 | |
| 456 | static void pcistub_remove(struct pci_dev *dev) |
| 457 | { |
| 458 | struct pcistub_device *psdev, *found_psdev = NULL; |
| 459 | unsigned long flags; |
| 460 | |
| 461 | dev_dbg(&dev->dev, "removing\n"); |
| 462 | |
| 463 | spin_lock_irqsave(&pcistub_devices_lock, flags); |
| 464 | |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 465 | xen_pcibk_config_quirk_release(dev); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 466 | |
| 467 | list_for_each_entry(psdev, &pcistub_devices, dev_list) { |
| 468 | if (psdev->dev == dev) { |
| 469 | found_psdev = psdev; |
| 470 | break; |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | spin_unlock_irqrestore(&pcistub_devices_lock, flags); |
| 475 | |
| 476 | if (found_psdev) { |
| 477 | dev_dbg(&dev->dev, "found device to remove - in use? %p\n", |
| 478 | found_psdev->pdev); |
| 479 | |
| 480 | if (found_psdev->pdev) { |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 481 | printk(KERN_WARNING DRV_NAME ": ****** removing device " |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 482 | "%s while still in-use! ******\n", |
| 483 | pci_name(found_psdev->dev)); |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 484 | printk(KERN_WARNING DRV_NAME ": ****** driver domain may" |
| 485 | " still access this device's i/o resources!\n"); |
| 486 | printk(KERN_WARNING DRV_NAME ": ****** shutdown driver " |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 487 | "domain before binding device\n"); |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 488 | printk(KERN_WARNING DRV_NAME ": ****** to other drivers " |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 489 | "or domains\n"); |
| 490 | |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 491 | xen_pcibk_release_pci_dev(found_psdev->pdev, |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 492 | found_psdev->dev); |
| 493 | } |
| 494 | |
| 495 | spin_lock_irqsave(&pcistub_devices_lock, flags); |
| 496 | list_del(&found_psdev->dev_list); |
| 497 | spin_unlock_irqrestore(&pcistub_devices_lock, flags); |
| 498 | |
| 499 | /* the final put for releasing from the list */ |
| 500 | pcistub_device_put(found_psdev); |
| 501 | } |
| 502 | } |
| 503 | |
Konrad Rzeszutek Wilk | 8bfd4e0 | 2011-07-19 20:09:43 -0400 | [diff] [blame] | 504 | static DEFINE_PCI_DEVICE_TABLE(pcistub_ids) = { |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 505 | { |
| 506 | .vendor = PCI_ANY_ID, |
| 507 | .device = PCI_ANY_ID, |
| 508 | .subvendor = PCI_ANY_ID, |
| 509 | .subdevice = PCI_ANY_ID, |
| 510 | }, |
| 511 | {0,}, |
| 512 | }; |
| 513 | |
| 514 | #define PCI_NODENAME_MAX 40 |
| 515 | static void kill_domain_by_device(struct pcistub_device *psdev) |
| 516 | { |
| 517 | struct xenbus_transaction xbt; |
| 518 | int err; |
| 519 | char nodename[PCI_NODENAME_MAX]; |
| 520 | |
Konrad Rzeszutek Wilk | 72bf809 | 2011-09-29 13:43:28 -0400 | [diff] [blame] | 521 | BUG_ON(!psdev); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 522 | snprintf(nodename, PCI_NODENAME_MAX, "/local/domain/0/backend/pci/%d/0", |
| 523 | psdev->pdev->xdev->otherend_id); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 524 | |
| 525 | again: |
| 526 | err = xenbus_transaction_start(&xbt); |
| 527 | if (err) { |
| 528 | dev_err(&psdev->dev->dev, |
| 529 | "error %d when start xenbus transaction\n", err); |
| 530 | return; |
| 531 | } |
| 532 | /*PV AER handlers will set this flag*/ |
| 533 | xenbus_printf(xbt, nodename, "aerState" , "aerfail"); |
| 534 | err = xenbus_transaction_end(xbt, 0); |
| 535 | if (err) { |
| 536 | if (err == -EAGAIN) |
| 537 | goto again; |
| 538 | dev_err(&psdev->dev->dev, |
| 539 | "error %d when end xenbus transaction\n", err); |
| 540 | return; |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | /* For each aer recovery step error_detected, mmio_enabled, etc, front_end and |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 545 | * backend need to have cooperation. In xen_pcibk, those steps will do similar |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 546 | * jobs: send service request and waiting for front_end response. |
| 547 | */ |
| 548 | static pci_ers_result_t common_process(struct pcistub_device *psdev, |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 549 | pci_channel_state_t state, int aer_cmd, |
| 550 | pci_ers_result_t result) |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 551 | { |
| 552 | pci_ers_result_t res = result; |
| 553 | struct xen_pcie_aer_op *aer_op; |
| 554 | int ret; |
| 555 | |
| 556 | /*with PV AER drivers*/ |
| 557 | aer_op = &(psdev->pdev->sh_info->aer_op); |
| 558 | aer_op->cmd = aer_cmd ; |
| 559 | /*useful for error_detected callback*/ |
| 560 | aer_op->err = state; |
| 561 | /*pcifront_end BDF*/ |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 562 | ret = xen_pcibk_get_pcifront_dev(psdev->dev, psdev->pdev, |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 563 | &aer_op->domain, &aer_op->bus, &aer_op->devfn); |
| 564 | if (!ret) { |
| 565 | dev_err(&psdev->dev->dev, |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 566 | DRV_NAME ": failed to get pcifront device\n"); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 567 | return PCI_ERS_RESULT_NONE; |
| 568 | } |
| 569 | wmb(); |
| 570 | |
| 571 | dev_dbg(&psdev->dev->dev, |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 572 | DRV_NAME ": aer_op %x dom %x bus %x devfn %x\n", |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 573 | aer_cmd, aer_op->domain, aer_op->bus, aer_op->devfn); |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 574 | /*local flag to mark there's aer request, xen_pcibk callback will use |
| 575 | * this flag to judge whether we need to check pci-front give aer |
| 576 | * service ack signal |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 577 | */ |
| 578 | set_bit(_PCIB_op_pending, (unsigned long *)&psdev->pdev->flags); |
| 579 | |
| 580 | /*It is possible that a pcifront conf_read_write ops request invokes |
| 581 | * the callback which cause the spurious execution of wake_up. |
| 582 | * Yet it is harmless and better than a spinlock here |
| 583 | */ |
| 584 | set_bit(_XEN_PCIB_active, |
| 585 | (unsigned long *)&psdev->pdev->sh_info->flags); |
| 586 | wmb(); |
| 587 | notify_remote_via_irq(psdev->pdev->evtchn_irq); |
| 588 | |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 589 | ret = wait_event_timeout(xen_pcibk_aer_wait_queue, |
| 590 | !(test_bit(_XEN_PCIB_active, (unsigned long *) |
| 591 | &psdev->pdev->sh_info->flags)), 300*HZ); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 592 | |
| 593 | if (!ret) { |
| 594 | if (test_bit(_XEN_PCIB_active, |
| 595 | (unsigned long *)&psdev->pdev->sh_info->flags)) { |
| 596 | dev_err(&psdev->dev->dev, |
| 597 | "pcifront aer process not responding!\n"); |
| 598 | clear_bit(_XEN_PCIB_active, |
| 599 | (unsigned long *)&psdev->pdev->sh_info->flags); |
| 600 | aer_op->err = PCI_ERS_RESULT_NONE; |
| 601 | return res; |
| 602 | } |
| 603 | } |
| 604 | clear_bit(_PCIB_op_pending, (unsigned long *)&psdev->pdev->flags); |
| 605 | |
| 606 | if (test_bit(_XEN_PCIF_active, |
| 607 | (unsigned long *)&psdev->pdev->sh_info->flags)) { |
| 608 | dev_dbg(&psdev->dev->dev, |
Jan Beulich | 402c5e1 | 2011-09-21 16:22:11 -0400 | [diff] [blame] | 609 | "schedule pci_conf service in " DRV_NAME "\n"); |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 610 | xen_pcibk_test_and_schedule_op(psdev->pdev); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | res = (pci_ers_result_t)aer_op->err; |
| 614 | return res; |
| 615 | } |
| 616 | |
| 617 | /* |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 618 | * xen_pcibk_slot_reset: it will send the slot_reset request to pcifront in case |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 619 | * of the device driver could provide this service, and then wait for pcifront |
| 620 | * ack. |
| 621 | * @dev: pointer to PCI devices |
| 622 | * return value is used by aer_core do_recovery policy |
| 623 | */ |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 624 | static pci_ers_result_t xen_pcibk_slot_reset(struct pci_dev *dev) |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 625 | { |
| 626 | struct pcistub_device *psdev; |
| 627 | pci_ers_result_t result; |
| 628 | |
| 629 | result = PCI_ERS_RESULT_RECOVERED; |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 630 | dev_dbg(&dev->dev, "xen_pcibk_slot_reset(bus:%x,devfn:%x)\n", |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 631 | dev->bus->number, dev->devfn); |
| 632 | |
| 633 | down_write(&pcistub_sem); |
| 634 | psdev = pcistub_device_find(pci_domain_nr(dev->bus), |
| 635 | dev->bus->number, |
| 636 | PCI_SLOT(dev->devfn), |
| 637 | PCI_FUNC(dev->devfn)); |
| 638 | |
| 639 | if (!psdev || !psdev->pdev) { |
| 640 | dev_err(&dev->dev, |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 641 | DRV_NAME " device is not found/assigned\n"); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 642 | goto end; |
| 643 | } |
| 644 | |
| 645 | if (!psdev->pdev->sh_info) { |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 646 | dev_err(&dev->dev, DRV_NAME " device is not connected or owned" |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 647 | " by HVM, kill it\n"); |
| 648 | kill_domain_by_device(psdev); |
| 649 | goto release; |
| 650 | } |
| 651 | |
| 652 | if (!test_bit(_XEN_PCIB_AERHANDLER, |
| 653 | (unsigned long *)&psdev->pdev->sh_info->flags)) { |
| 654 | dev_err(&dev->dev, |
| 655 | "guest with no AER driver should have been killed\n"); |
| 656 | goto release; |
| 657 | } |
| 658 | result = common_process(psdev, 1, XEN_PCI_OP_aer_slotreset, result); |
| 659 | |
| 660 | if (result == PCI_ERS_RESULT_NONE || |
| 661 | result == PCI_ERS_RESULT_DISCONNECT) { |
| 662 | dev_dbg(&dev->dev, |
| 663 | "No AER slot_reset service or disconnected!\n"); |
| 664 | kill_domain_by_device(psdev); |
| 665 | } |
| 666 | release: |
| 667 | pcistub_device_put(psdev); |
| 668 | end: |
| 669 | up_write(&pcistub_sem); |
| 670 | return result; |
| 671 | |
| 672 | } |
| 673 | |
| 674 | |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 675 | /*xen_pcibk_mmio_enabled: it will send the mmio_enabled request to pcifront |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 676 | * in case of the device driver could provide this service, and then wait |
| 677 | * for pcifront ack |
| 678 | * @dev: pointer to PCI devices |
| 679 | * return value is used by aer_core do_recovery policy |
| 680 | */ |
| 681 | |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 682 | static pci_ers_result_t xen_pcibk_mmio_enabled(struct pci_dev *dev) |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 683 | { |
| 684 | struct pcistub_device *psdev; |
| 685 | pci_ers_result_t result; |
| 686 | |
| 687 | result = PCI_ERS_RESULT_RECOVERED; |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 688 | dev_dbg(&dev->dev, "xen_pcibk_mmio_enabled(bus:%x,devfn:%x)\n", |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 689 | dev->bus->number, dev->devfn); |
| 690 | |
| 691 | down_write(&pcistub_sem); |
| 692 | psdev = pcistub_device_find(pci_domain_nr(dev->bus), |
| 693 | dev->bus->number, |
| 694 | PCI_SLOT(dev->devfn), |
| 695 | PCI_FUNC(dev->devfn)); |
| 696 | |
| 697 | if (!psdev || !psdev->pdev) { |
| 698 | dev_err(&dev->dev, |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 699 | DRV_NAME " device is not found/assigned\n"); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 700 | goto end; |
| 701 | } |
| 702 | |
| 703 | if (!psdev->pdev->sh_info) { |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 704 | dev_err(&dev->dev, DRV_NAME " device is not connected or owned" |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 705 | " by HVM, kill it\n"); |
| 706 | kill_domain_by_device(psdev); |
| 707 | goto release; |
| 708 | } |
| 709 | |
| 710 | if (!test_bit(_XEN_PCIB_AERHANDLER, |
| 711 | (unsigned long *)&psdev->pdev->sh_info->flags)) { |
| 712 | dev_err(&dev->dev, |
| 713 | "guest with no AER driver should have been killed\n"); |
| 714 | goto release; |
| 715 | } |
| 716 | result = common_process(psdev, 1, XEN_PCI_OP_aer_mmio, result); |
| 717 | |
| 718 | if (result == PCI_ERS_RESULT_NONE || |
| 719 | result == PCI_ERS_RESULT_DISCONNECT) { |
| 720 | dev_dbg(&dev->dev, |
| 721 | "No AER mmio_enabled service or disconnected!\n"); |
| 722 | kill_domain_by_device(psdev); |
| 723 | } |
| 724 | release: |
| 725 | pcistub_device_put(psdev); |
| 726 | end: |
| 727 | up_write(&pcistub_sem); |
| 728 | return result; |
| 729 | } |
| 730 | |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 731 | /*xen_pcibk_error_detected: it will send the error_detected request to pcifront |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 732 | * in case of the device driver could provide this service, and then wait |
| 733 | * for pcifront ack. |
| 734 | * @dev: pointer to PCI devices |
| 735 | * @error: the current PCI connection state |
| 736 | * return value is used by aer_core do_recovery policy |
| 737 | */ |
| 738 | |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 739 | static pci_ers_result_t xen_pcibk_error_detected(struct pci_dev *dev, |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 740 | pci_channel_state_t error) |
| 741 | { |
| 742 | struct pcistub_device *psdev; |
| 743 | pci_ers_result_t result; |
| 744 | |
| 745 | result = PCI_ERS_RESULT_CAN_RECOVER; |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 746 | dev_dbg(&dev->dev, "xen_pcibk_error_detected(bus:%x,devfn:%x)\n", |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 747 | dev->bus->number, dev->devfn); |
| 748 | |
| 749 | down_write(&pcistub_sem); |
| 750 | psdev = pcistub_device_find(pci_domain_nr(dev->bus), |
| 751 | dev->bus->number, |
| 752 | PCI_SLOT(dev->devfn), |
| 753 | PCI_FUNC(dev->devfn)); |
| 754 | |
| 755 | if (!psdev || !psdev->pdev) { |
| 756 | dev_err(&dev->dev, |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 757 | DRV_NAME " device is not found/assigned\n"); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 758 | goto end; |
| 759 | } |
| 760 | |
| 761 | if (!psdev->pdev->sh_info) { |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 762 | dev_err(&dev->dev, DRV_NAME " device is not connected or owned" |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 763 | " by HVM, kill it\n"); |
| 764 | kill_domain_by_device(psdev); |
| 765 | goto release; |
| 766 | } |
| 767 | |
| 768 | /*Guest owns the device yet no aer handler regiested, kill guest*/ |
| 769 | if (!test_bit(_XEN_PCIB_AERHANDLER, |
| 770 | (unsigned long *)&psdev->pdev->sh_info->flags)) { |
| 771 | dev_dbg(&dev->dev, "guest may have no aer driver, kill it\n"); |
| 772 | kill_domain_by_device(psdev); |
| 773 | goto release; |
| 774 | } |
| 775 | result = common_process(psdev, error, XEN_PCI_OP_aer_detected, result); |
| 776 | |
| 777 | if (result == PCI_ERS_RESULT_NONE || |
| 778 | result == PCI_ERS_RESULT_DISCONNECT) { |
| 779 | dev_dbg(&dev->dev, |
| 780 | "No AER error_detected service or disconnected!\n"); |
| 781 | kill_domain_by_device(psdev); |
| 782 | } |
| 783 | release: |
| 784 | pcistub_device_put(psdev); |
| 785 | end: |
| 786 | up_write(&pcistub_sem); |
| 787 | return result; |
| 788 | } |
| 789 | |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 790 | /*xen_pcibk_error_resume: it will send the error_resume request to pcifront |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 791 | * in case of the device driver could provide this service, and then wait |
| 792 | * for pcifront ack. |
| 793 | * @dev: pointer to PCI devices |
| 794 | */ |
| 795 | |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 796 | static void xen_pcibk_error_resume(struct pci_dev *dev) |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 797 | { |
| 798 | struct pcistub_device *psdev; |
| 799 | |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 800 | dev_dbg(&dev->dev, "xen_pcibk_error_resume(bus:%x,devfn:%x)\n", |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 801 | dev->bus->number, dev->devfn); |
| 802 | |
| 803 | down_write(&pcistub_sem); |
| 804 | psdev = pcistub_device_find(pci_domain_nr(dev->bus), |
| 805 | dev->bus->number, |
| 806 | PCI_SLOT(dev->devfn), |
| 807 | PCI_FUNC(dev->devfn)); |
| 808 | |
| 809 | if (!psdev || !psdev->pdev) { |
| 810 | dev_err(&dev->dev, |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 811 | DRV_NAME " device is not found/assigned\n"); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 812 | goto end; |
| 813 | } |
| 814 | |
| 815 | if (!psdev->pdev->sh_info) { |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 816 | dev_err(&dev->dev, DRV_NAME " device is not connected or owned" |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 817 | " by HVM, kill it\n"); |
| 818 | kill_domain_by_device(psdev); |
| 819 | goto release; |
| 820 | } |
| 821 | |
| 822 | if (!test_bit(_XEN_PCIB_AERHANDLER, |
| 823 | (unsigned long *)&psdev->pdev->sh_info->flags)) { |
| 824 | dev_err(&dev->dev, |
| 825 | "guest with no AER driver should have been killed\n"); |
| 826 | kill_domain_by_device(psdev); |
| 827 | goto release; |
| 828 | } |
| 829 | common_process(psdev, 1, XEN_PCI_OP_aer_resume, |
| 830 | PCI_ERS_RESULT_RECOVERED); |
| 831 | release: |
| 832 | pcistub_device_put(psdev); |
| 833 | end: |
| 834 | up_write(&pcistub_sem); |
| 835 | return; |
| 836 | } |
| 837 | |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 838 | /*add xen_pcibk AER handling*/ |
| 839 | static struct pci_error_handlers xen_pcibk_error_handler = { |
| 840 | .error_detected = xen_pcibk_error_detected, |
| 841 | .mmio_enabled = xen_pcibk_mmio_enabled, |
| 842 | .slot_reset = xen_pcibk_slot_reset, |
| 843 | .resume = xen_pcibk_error_resume, |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 844 | }; |
| 845 | |
| 846 | /* |
| 847 | * Note: There is no MODULE_DEVICE_TABLE entry here because this isn't |
| 848 | * for a normal device. I don't want it to be loaded automatically. |
| 849 | */ |
| 850 | |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 851 | static struct pci_driver xen_pcibk_pci_driver = { |
| 852 | /* The name should be xen_pciback, but until the tools are updated |
| 853 | * we will keep it as pciback. */ |
| 854 | .name = "pciback", |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 855 | .id_table = pcistub_ids, |
| 856 | .probe = pcistub_probe, |
| 857 | .remove = pcistub_remove, |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 858 | .err_handler = &xen_pcibk_error_handler, |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 859 | }; |
| 860 | |
| 861 | static inline int str_to_slot(const char *buf, int *domain, int *bus, |
| 862 | int *slot, int *func) |
| 863 | { |
| 864 | int err; |
| 865 | |
| 866 | err = sscanf(buf, " %x:%x:%x.%x", domain, bus, slot, func); |
| 867 | if (err == 4) |
| 868 | return 0; |
| 869 | else if (err < 0) |
| 870 | return -EINVAL; |
| 871 | |
| 872 | /* try again without domain */ |
| 873 | *domain = 0; |
| 874 | err = sscanf(buf, " %x:%x.%x", bus, slot, func); |
| 875 | if (err == 3) |
| 876 | return 0; |
| 877 | |
| 878 | return -EINVAL; |
| 879 | } |
| 880 | |
| 881 | static inline int str_to_quirk(const char *buf, int *domain, int *bus, int |
| 882 | *slot, int *func, int *reg, int *size, int *mask) |
| 883 | { |
| 884 | int err; |
| 885 | |
| 886 | err = |
| 887 | sscanf(buf, " %04x:%02x:%02x.%1x-%08x:%1x:%08x", domain, bus, slot, |
| 888 | func, reg, size, mask); |
| 889 | if (err == 7) |
| 890 | return 0; |
| 891 | return -EINVAL; |
| 892 | } |
| 893 | |
| 894 | static int pcistub_device_id_add(int domain, int bus, int slot, int func) |
| 895 | { |
| 896 | struct pcistub_device_id *pci_dev_id; |
| 897 | unsigned long flags; |
| 898 | |
| 899 | pci_dev_id = kmalloc(sizeof(*pci_dev_id), GFP_KERNEL); |
| 900 | if (!pci_dev_id) |
| 901 | return -ENOMEM; |
| 902 | |
| 903 | pci_dev_id->domain = domain; |
| 904 | pci_dev_id->bus = bus; |
| 905 | pci_dev_id->devfn = PCI_DEVFN(slot, func); |
| 906 | |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 907 | pr_debug(DRV_NAME ": wants to seize %04x:%02x:%02x.%01x\n", |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 908 | domain, bus, slot, func); |
| 909 | |
| 910 | spin_lock_irqsave(&device_ids_lock, flags); |
| 911 | list_add_tail(&pci_dev_id->slot_list, &pcistub_device_ids); |
| 912 | spin_unlock_irqrestore(&device_ids_lock, flags); |
| 913 | |
| 914 | return 0; |
| 915 | } |
| 916 | |
| 917 | static int pcistub_device_id_remove(int domain, int bus, int slot, int func) |
| 918 | { |
| 919 | struct pcistub_device_id *pci_dev_id, *t; |
| 920 | int devfn = PCI_DEVFN(slot, func); |
| 921 | int err = -ENOENT; |
| 922 | unsigned long flags; |
| 923 | |
| 924 | spin_lock_irqsave(&device_ids_lock, flags); |
| 925 | list_for_each_entry_safe(pci_dev_id, t, &pcistub_device_ids, |
| 926 | slot_list) { |
| 927 | if (pci_dev_id->domain == domain |
| 928 | && pci_dev_id->bus == bus && pci_dev_id->devfn == devfn) { |
| 929 | /* Don't break; here because it's possible the same |
| 930 | * slot could be in the list more than once |
| 931 | */ |
| 932 | list_del(&pci_dev_id->slot_list); |
| 933 | kfree(pci_dev_id); |
| 934 | |
| 935 | err = 0; |
| 936 | |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 937 | pr_debug(DRV_NAME ": removed %04x:%02x:%02x.%01x from " |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 938 | "seize list\n", domain, bus, slot, func); |
| 939 | } |
| 940 | } |
| 941 | spin_unlock_irqrestore(&device_ids_lock, flags); |
| 942 | |
| 943 | return err; |
| 944 | } |
| 945 | |
| 946 | static int pcistub_reg_add(int domain, int bus, int slot, int func, int reg, |
| 947 | int size, int mask) |
| 948 | { |
| 949 | int err = 0; |
| 950 | struct pcistub_device *psdev; |
| 951 | struct pci_dev *dev; |
| 952 | struct config_field *field; |
| 953 | |
| 954 | psdev = pcistub_device_find(domain, bus, slot, func); |
| 955 | if (!psdev || !psdev->dev) { |
| 956 | err = -ENODEV; |
| 957 | goto out; |
| 958 | } |
| 959 | dev = psdev->dev; |
| 960 | |
| 961 | field = kzalloc(sizeof(*field), GFP_ATOMIC); |
| 962 | if (!field) { |
| 963 | err = -ENOMEM; |
| 964 | goto out; |
| 965 | } |
| 966 | |
| 967 | field->offset = reg; |
| 968 | field->size = size; |
| 969 | field->mask = mask; |
| 970 | field->init = NULL; |
| 971 | field->reset = NULL; |
| 972 | field->release = NULL; |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 973 | field->clean = xen_pcibk_config_field_free; |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 974 | |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 975 | err = xen_pcibk_config_quirks_add_field(dev, field); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 976 | if (err) |
| 977 | kfree(field); |
| 978 | out: |
| 979 | return err; |
| 980 | } |
| 981 | |
| 982 | static ssize_t pcistub_slot_add(struct device_driver *drv, const char *buf, |
| 983 | size_t count) |
| 984 | { |
| 985 | int domain, bus, slot, func; |
| 986 | int err; |
| 987 | |
| 988 | err = str_to_slot(buf, &domain, &bus, &slot, &func); |
| 989 | if (err) |
| 990 | goto out; |
| 991 | |
| 992 | err = pcistub_device_id_add(domain, bus, slot, func); |
| 993 | |
| 994 | out: |
| 995 | if (!err) |
| 996 | err = count; |
| 997 | return err; |
| 998 | } |
Jan Beulich | 402c5e1 | 2011-09-21 16:22:11 -0400 | [diff] [blame] | 999 | static DRIVER_ATTR(new_slot, S_IWUSR, NULL, pcistub_slot_add); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1000 | |
| 1001 | static ssize_t pcistub_slot_remove(struct device_driver *drv, const char *buf, |
| 1002 | size_t count) |
| 1003 | { |
| 1004 | int domain, bus, slot, func; |
| 1005 | int err; |
| 1006 | |
| 1007 | err = str_to_slot(buf, &domain, &bus, &slot, &func); |
| 1008 | if (err) |
| 1009 | goto out; |
| 1010 | |
| 1011 | err = pcistub_device_id_remove(domain, bus, slot, func); |
| 1012 | |
| 1013 | out: |
| 1014 | if (!err) |
| 1015 | err = count; |
| 1016 | return err; |
| 1017 | } |
Jan Beulich | 402c5e1 | 2011-09-21 16:22:11 -0400 | [diff] [blame] | 1018 | static DRIVER_ATTR(remove_slot, S_IWUSR, NULL, pcistub_slot_remove); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1019 | |
| 1020 | static ssize_t pcistub_slot_show(struct device_driver *drv, char *buf) |
| 1021 | { |
| 1022 | struct pcistub_device_id *pci_dev_id; |
| 1023 | size_t count = 0; |
| 1024 | unsigned long flags; |
| 1025 | |
| 1026 | spin_lock_irqsave(&device_ids_lock, flags); |
| 1027 | list_for_each_entry(pci_dev_id, &pcistub_device_ids, slot_list) { |
| 1028 | if (count >= PAGE_SIZE) |
| 1029 | break; |
| 1030 | |
| 1031 | count += scnprintf(buf + count, PAGE_SIZE - count, |
| 1032 | "%04x:%02x:%02x.%01x\n", |
| 1033 | pci_dev_id->domain, pci_dev_id->bus, |
| 1034 | PCI_SLOT(pci_dev_id->devfn), |
| 1035 | PCI_FUNC(pci_dev_id->devfn)); |
| 1036 | } |
| 1037 | spin_unlock_irqrestore(&device_ids_lock, flags); |
| 1038 | |
| 1039 | return count; |
| 1040 | } |
Jan Beulich | 402c5e1 | 2011-09-21 16:22:11 -0400 | [diff] [blame] | 1041 | static DRIVER_ATTR(slots, S_IRUSR, pcistub_slot_show, NULL); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1042 | |
Konrad Rzeszutek Wilk | 0513fe9 | 2011-07-19 18:56:39 -0400 | [diff] [blame] | 1043 | static ssize_t pcistub_irq_handler_show(struct device_driver *drv, char *buf) |
| 1044 | { |
| 1045 | struct pcistub_device *psdev; |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 1046 | struct xen_pcibk_dev_data *dev_data; |
Konrad Rzeszutek Wilk | 0513fe9 | 2011-07-19 18:56:39 -0400 | [diff] [blame] | 1047 | size_t count = 0; |
| 1048 | unsigned long flags; |
| 1049 | |
| 1050 | spin_lock_irqsave(&pcistub_devices_lock, flags); |
| 1051 | list_for_each_entry(psdev, &pcistub_devices, dev_list) { |
| 1052 | if (count >= PAGE_SIZE) |
| 1053 | break; |
| 1054 | if (!psdev->dev) |
| 1055 | continue; |
| 1056 | dev_data = pci_get_drvdata(psdev->dev); |
| 1057 | if (!dev_data) |
| 1058 | continue; |
| 1059 | count += |
| 1060 | scnprintf(buf + count, PAGE_SIZE - count, |
| 1061 | "%s:%s:%sing:%ld\n", |
| 1062 | pci_name(psdev->dev), |
| 1063 | dev_data->isr_on ? "on" : "off", |
| 1064 | dev_data->ack_intr ? "ack" : "not ack", |
| 1065 | dev_data->handled); |
| 1066 | } |
| 1067 | spin_unlock_irqrestore(&pcistub_devices_lock, flags); |
| 1068 | return count; |
| 1069 | } |
Jan Beulich | 402c5e1 | 2011-09-21 16:22:11 -0400 | [diff] [blame] | 1070 | static DRIVER_ATTR(irq_handlers, S_IRUSR, pcistub_irq_handler_show, NULL); |
Konrad Rzeszutek Wilk | 0513fe9 | 2011-07-19 18:56:39 -0400 | [diff] [blame] | 1071 | |
| 1072 | static ssize_t pcistub_irq_handler_switch(struct device_driver *drv, |
| 1073 | const char *buf, |
| 1074 | size_t count) |
| 1075 | { |
| 1076 | struct pcistub_device *psdev; |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 1077 | struct xen_pcibk_dev_data *dev_data; |
Konrad Rzeszutek Wilk | 0513fe9 | 2011-07-19 18:56:39 -0400 | [diff] [blame] | 1078 | int domain, bus, slot, func; |
| 1079 | int err = -ENOENT; |
| 1080 | |
| 1081 | err = str_to_slot(buf, &domain, &bus, &slot, &func); |
| 1082 | if (err) |
| 1083 | goto out; |
| 1084 | |
| 1085 | psdev = pcistub_device_find(domain, bus, slot, func); |
| 1086 | |
| 1087 | if (!psdev) |
| 1088 | goto out; |
| 1089 | |
| 1090 | dev_data = pci_get_drvdata(psdev->dev); |
| 1091 | if (!dev_data) |
| 1092 | goto out; |
| 1093 | |
| 1094 | dev_dbg(&psdev->dev->dev, "%s fake irq handler: %d->%d\n", |
| 1095 | dev_data->irq_name, dev_data->isr_on, |
| 1096 | !dev_data->isr_on); |
| 1097 | |
| 1098 | dev_data->isr_on = !(dev_data->isr_on); |
| 1099 | if (dev_data->isr_on) |
| 1100 | dev_data->ack_intr = 1; |
| 1101 | out: |
| 1102 | if (!err) |
| 1103 | err = count; |
| 1104 | return err; |
| 1105 | } |
Jan Beulich | 402c5e1 | 2011-09-21 16:22:11 -0400 | [diff] [blame] | 1106 | static DRIVER_ATTR(irq_handler_state, S_IWUSR, NULL, |
| 1107 | pcistub_irq_handler_switch); |
Konrad Rzeszutek Wilk | 0513fe9 | 2011-07-19 18:56:39 -0400 | [diff] [blame] | 1108 | |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1109 | static ssize_t pcistub_quirk_add(struct device_driver *drv, const char *buf, |
| 1110 | size_t count) |
| 1111 | { |
| 1112 | int domain, bus, slot, func, reg, size, mask; |
| 1113 | int err; |
| 1114 | |
| 1115 | err = str_to_quirk(buf, &domain, &bus, &slot, &func, ®, &size, |
| 1116 | &mask); |
| 1117 | if (err) |
| 1118 | goto out; |
| 1119 | |
| 1120 | err = pcistub_reg_add(domain, bus, slot, func, reg, size, mask); |
| 1121 | |
| 1122 | out: |
| 1123 | if (!err) |
| 1124 | err = count; |
| 1125 | return err; |
| 1126 | } |
| 1127 | |
| 1128 | static ssize_t pcistub_quirk_show(struct device_driver *drv, char *buf) |
| 1129 | { |
| 1130 | int count = 0; |
| 1131 | unsigned long flags; |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 1132 | struct xen_pcibk_config_quirk *quirk; |
| 1133 | struct xen_pcibk_dev_data *dev_data; |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1134 | const struct config_field *field; |
| 1135 | const struct config_field_entry *cfg_entry; |
| 1136 | |
| 1137 | spin_lock_irqsave(&device_ids_lock, flags); |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 1138 | list_for_each_entry(quirk, &xen_pcibk_quirks, quirks_list) { |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1139 | if (count >= PAGE_SIZE) |
| 1140 | goto out; |
| 1141 | |
| 1142 | count += scnprintf(buf + count, PAGE_SIZE - count, |
| 1143 | "%02x:%02x.%01x\n\t%04x:%04x:%04x:%04x\n", |
| 1144 | quirk->pdev->bus->number, |
| 1145 | PCI_SLOT(quirk->pdev->devfn), |
| 1146 | PCI_FUNC(quirk->pdev->devfn), |
| 1147 | quirk->devid.vendor, quirk->devid.device, |
| 1148 | quirk->devid.subvendor, |
| 1149 | quirk->devid.subdevice); |
| 1150 | |
| 1151 | dev_data = pci_get_drvdata(quirk->pdev); |
| 1152 | |
| 1153 | list_for_each_entry(cfg_entry, &dev_data->config_fields, list) { |
| 1154 | field = cfg_entry->field; |
| 1155 | if (count >= PAGE_SIZE) |
| 1156 | goto out; |
| 1157 | |
| 1158 | count += scnprintf(buf + count, PAGE_SIZE - count, |
| 1159 | "\t\t%08x:%01x:%08x\n", |
| 1160 | cfg_entry->base_offset + |
| 1161 | field->offset, field->size, |
| 1162 | field->mask); |
| 1163 | } |
| 1164 | } |
| 1165 | |
| 1166 | out: |
| 1167 | spin_unlock_irqrestore(&device_ids_lock, flags); |
| 1168 | |
| 1169 | return count; |
| 1170 | } |
Jan Beulich | 402c5e1 | 2011-09-21 16:22:11 -0400 | [diff] [blame] | 1171 | static DRIVER_ATTR(quirks, S_IRUSR | S_IWUSR, pcistub_quirk_show, |
| 1172 | pcistub_quirk_add); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1173 | |
| 1174 | static ssize_t permissive_add(struct device_driver *drv, const char *buf, |
| 1175 | size_t count) |
| 1176 | { |
| 1177 | int domain, bus, slot, func; |
| 1178 | int err; |
| 1179 | struct pcistub_device *psdev; |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 1180 | struct xen_pcibk_dev_data *dev_data; |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1181 | err = str_to_slot(buf, &domain, &bus, &slot, &func); |
| 1182 | if (err) |
| 1183 | goto out; |
| 1184 | psdev = pcistub_device_find(domain, bus, slot, func); |
| 1185 | if (!psdev) { |
| 1186 | err = -ENODEV; |
| 1187 | goto out; |
| 1188 | } |
| 1189 | if (!psdev->dev) { |
| 1190 | err = -ENODEV; |
| 1191 | goto release; |
| 1192 | } |
| 1193 | dev_data = pci_get_drvdata(psdev->dev); |
| 1194 | /* the driver data for a device should never be null at this point */ |
| 1195 | if (!dev_data) { |
| 1196 | err = -ENXIO; |
| 1197 | goto release; |
| 1198 | } |
| 1199 | if (!dev_data->permissive) { |
| 1200 | dev_data->permissive = 1; |
| 1201 | /* Let user know that what they're doing could be unsafe */ |
| 1202 | dev_warn(&psdev->dev->dev, "enabling permissive mode " |
| 1203 | "configuration space accesses!\n"); |
| 1204 | dev_warn(&psdev->dev->dev, |
| 1205 | "permissive mode is potentially unsafe!\n"); |
| 1206 | } |
| 1207 | release: |
| 1208 | pcistub_device_put(psdev); |
| 1209 | out: |
| 1210 | if (!err) |
| 1211 | err = count; |
| 1212 | return err; |
| 1213 | } |
| 1214 | |
| 1215 | static ssize_t permissive_show(struct device_driver *drv, char *buf) |
| 1216 | { |
| 1217 | struct pcistub_device *psdev; |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 1218 | struct xen_pcibk_dev_data *dev_data; |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1219 | size_t count = 0; |
| 1220 | unsigned long flags; |
| 1221 | spin_lock_irqsave(&pcistub_devices_lock, flags); |
| 1222 | list_for_each_entry(psdev, &pcistub_devices, dev_list) { |
| 1223 | if (count >= PAGE_SIZE) |
| 1224 | break; |
| 1225 | if (!psdev->dev) |
| 1226 | continue; |
| 1227 | dev_data = pci_get_drvdata(psdev->dev); |
| 1228 | if (!dev_data || !dev_data->permissive) |
| 1229 | continue; |
| 1230 | count += |
| 1231 | scnprintf(buf + count, PAGE_SIZE - count, "%s\n", |
| 1232 | pci_name(psdev->dev)); |
| 1233 | } |
| 1234 | spin_unlock_irqrestore(&pcistub_devices_lock, flags); |
| 1235 | return count; |
| 1236 | } |
Jan Beulich | 402c5e1 | 2011-09-21 16:22:11 -0400 | [diff] [blame] | 1237 | static DRIVER_ATTR(permissive, S_IRUSR | S_IWUSR, permissive_show, |
| 1238 | permissive_add); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1239 | |
| 1240 | static void pcistub_exit(void) |
| 1241 | { |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 1242 | driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_new_slot); |
| 1243 | driver_remove_file(&xen_pcibk_pci_driver.driver, |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1244 | &driver_attr_remove_slot); |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 1245 | driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_slots); |
| 1246 | driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_quirks); |
| 1247 | driver_remove_file(&xen_pcibk_pci_driver.driver, |
| 1248 | &driver_attr_permissive); |
| 1249 | driver_remove_file(&xen_pcibk_pci_driver.driver, |
Konrad Rzeszutek Wilk | 0513fe9 | 2011-07-19 18:56:39 -0400 | [diff] [blame] | 1250 | &driver_attr_irq_handlers); |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 1251 | driver_remove_file(&xen_pcibk_pci_driver.driver, |
Konrad Rzeszutek Wilk | 0513fe9 | 2011-07-19 18:56:39 -0400 | [diff] [blame] | 1252 | &driver_attr_irq_handler_state); |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 1253 | pci_unregister_driver(&xen_pcibk_pci_driver); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1254 | } |
| 1255 | |
| 1256 | static int __init pcistub_init(void) |
| 1257 | { |
| 1258 | int pos = 0; |
| 1259 | int err = 0; |
| 1260 | int domain, bus, slot, func; |
| 1261 | int parsed; |
| 1262 | |
| 1263 | if (pci_devs_to_hide && *pci_devs_to_hide) { |
| 1264 | do { |
| 1265 | parsed = 0; |
| 1266 | |
| 1267 | err = sscanf(pci_devs_to_hide + pos, |
| 1268 | " (%x:%x:%x.%x) %n", |
| 1269 | &domain, &bus, &slot, &func, &parsed); |
| 1270 | if (err != 4) { |
| 1271 | domain = 0; |
| 1272 | err = sscanf(pci_devs_to_hide + pos, |
| 1273 | " (%x:%x.%x) %n", |
| 1274 | &bus, &slot, &func, &parsed); |
| 1275 | if (err != 3) |
| 1276 | goto parse_error; |
| 1277 | } |
| 1278 | |
| 1279 | err = pcistub_device_id_add(domain, bus, slot, func); |
| 1280 | if (err) |
| 1281 | goto out; |
| 1282 | |
| 1283 | /* if parsed<=0, we've reached the end of the string */ |
| 1284 | pos += parsed; |
| 1285 | } while (parsed > 0 && pci_devs_to_hide[pos]); |
| 1286 | } |
| 1287 | |
| 1288 | /* If we're the first PCI Device Driver to register, we're the |
| 1289 | * first one to get offered PCI devices as they become |
| 1290 | * available (and thus we can be the first to grab them) |
| 1291 | */ |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 1292 | err = pci_register_driver(&xen_pcibk_pci_driver); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1293 | if (err < 0) |
| 1294 | goto out; |
| 1295 | |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 1296 | err = driver_create_file(&xen_pcibk_pci_driver.driver, |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1297 | &driver_attr_new_slot); |
| 1298 | if (!err) |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 1299 | err = driver_create_file(&xen_pcibk_pci_driver.driver, |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1300 | &driver_attr_remove_slot); |
| 1301 | if (!err) |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 1302 | err = driver_create_file(&xen_pcibk_pci_driver.driver, |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1303 | &driver_attr_slots); |
| 1304 | if (!err) |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 1305 | err = driver_create_file(&xen_pcibk_pci_driver.driver, |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1306 | &driver_attr_quirks); |
| 1307 | if (!err) |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 1308 | err = driver_create_file(&xen_pcibk_pci_driver.driver, |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1309 | &driver_attr_permissive); |
| 1310 | |
Konrad Rzeszutek Wilk | 0513fe9 | 2011-07-19 18:56:39 -0400 | [diff] [blame] | 1311 | if (!err) |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 1312 | err = driver_create_file(&xen_pcibk_pci_driver.driver, |
Konrad Rzeszutek Wilk | 0513fe9 | 2011-07-19 18:56:39 -0400 | [diff] [blame] | 1313 | &driver_attr_irq_handlers); |
| 1314 | if (!err) |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 1315 | err = driver_create_file(&xen_pcibk_pci_driver.driver, |
Konrad Rzeszutek Wilk | 0513fe9 | 2011-07-19 18:56:39 -0400 | [diff] [blame] | 1316 | &driver_attr_irq_handler_state); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1317 | if (err) |
| 1318 | pcistub_exit(); |
| 1319 | |
| 1320 | out: |
| 1321 | return err; |
| 1322 | |
| 1323 | parse_error: |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 1324 | printk(KERN_ERR DRV_NAME ": Error parsing pci_devs_to_hide at \"%s\"\n", |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1325 | pci_devs_to_hide + pos); |
| 1326 | return -EINVAL; |
| 1327 | } |
| 1328 | |
| 1329 | #ifndef MODULE |
| 1330 | /* |
| 1331 | * fs_initcall happens before device_initcall |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 1332 | * so xen_pcibk *should* get called first (b/c we |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1333 | * want to suck up any device before other drivers |
| 1334 | * get a chance by being the first pci device |
| 1335 | * driver to register) |
| 1336 | */ |
| 1337 | fs_initcall(pcistub_init); |
| 1338 | #endif |
| 1339 | |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 1340 | static int __init xen_pcibk_init(void) |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1341 | { |
| 1342 | int err; |
| 1343 | |
| 1344 | if (!xen_initial_domain()) |
| 1345 | return -ENODEV; |
| 1346 | |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 1347 | err = xen_pcibk_config_init(); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1348 | if (err) |
| 1349 | return err; |
| 1350 | |
| 1351 | #ifdef MODULE |
| 1352 | err = pcistub_init(); |
| 1353 | if (err < 0) |
| 1354 | return err; |
| 1355 | #endif |
| 1356 | |
| 1357 | pcistub_init_devices_late(); |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 1358 | err = xen_pcibk_xenbus_register(); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1359 | if (err) |
| 1360 | pcistub_exit(); |
| 1361 | |
| 1362 | return err; |
| 1363 | } |
| 1364 | |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 1365 | static void __exit xen_pcibk_cleanup(void) |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1366 | { |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 1367 | xen_pcibk_xenbus_unregister(); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1368 | pcistub_exit(); |
| 1369 | } |
| 1370 | |
Konrad Rzeszutek Wilk | a92336a | 2011-07-19 19:40:51 -0400 | [diff] [blame] | 1371 | module_init(xen_pcibk_init); |
| 1372 | module_exit(xen_pcibk_cleanup); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 1373 | |
| 1374 | MODULE_LICENSE("Dual BSD/GPL"); |
Jan Beulich | 402c5e1 | 2011-09-21 16:22:11 -0400 | [diff] [blame] | 1375 | MODULE_ALIAS("xen-backend:pci"); |