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