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