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