blob: 405445965690eac42b7d32e4e86dfa7eb317cde2 [file] [log] [blame]
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001/*
2 * PCI Stub Driver - Grabs devices in backend to be exported later
3 *
4 * Ryan Wilson <hap9@epoch.ncsc.mil>
5 * Chris Bookholt <hap10@epoch.ncsc.mil>
6 */
7#include <linux/module.h>
8#include <linux/init.h>
9#include <linux/rwsem.h>
10#include <linux/list.h>
11#include <linux/spinlock.h>
12#include <linux/kref.h>
13#include <linux/pci.h>
14#include <linux/wait.h>
15#include <linux/sched.h>
Konrad Rzeszutek Wilk8bfd4e02011-07-19 20:09:43 -040016#include <linux/atomic.h>
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040017#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;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -040025wait_queue_head_t xen_pcibk_aer_wait_queue;
26/*Add sem for sync AER handling and xen_pcibk remove/reconfigue ops,
27* We want to avoid in middle of AER ops, xen_pcibk devices is being removed
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040028*/
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;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -040047 struct xen_pcibk_device *pdev;/* non-NULL if struct pci_dev is in use */
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040048};
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;
88
89 psdev = container_of(kref, struct pcistub_device, kref);
90
91 dev_dbg(&psdev->dev->dev, "pcistub_device_release\n");
92
Konrad Rzeszutek Wilk6221a9b2009-12-09 17:43:15 -050093 xen_unregister_device_domain_owner(psdev->dev);
94
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040095 /* Clean-up the device */
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -040096 xen_pcibk_reset_device(psdev->dev);
97 xen_pcibk_config_free_dyn_fields(psdev->dev);
98 xen_pcibk_config_free_dev(psdev->dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040099 kfree(pci_get_drvdata(psdev->dev));
100 pci_set_drvdata(psdev->dev, NULL);
101
Konrad Rzeszutek Wilk97309d32012-01-04 14:10:32 -0500102 psdev->dev->dev_flags &= ~PCI_DEV_FLAGS_ASSIGNED;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400103 pci_dev_put(psdev->dev);
104
105 kfree(psdev);
106}
107
108static inline void pcistub_device_get(struct pcistub_device *psdev)
109{
110 kref_get(&psdev->kref);
111}
112
113static inline void pcistub_device_put(struct pcistub_device *psdev)
114{
115 kref_put(&psdev->kref, pcistub_device_release);
116}
117
118static struct pcistub_device *pcistub_device_find(int domain, int bus,
119 int slot, int func)
120{
121 struct pcistub_device *psdev = NULL;
122 unsigned long flags;
123
124 spin_lock_irqsave(&pcistub_devices_lock, flags);
125
126 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
127 if (psdev->dev != NULL
128 && domain == pci_domain_nr(psdev->dev->bus)
129 && bus == psdev->dev->bus->number
130 && PCI_DEVFN(slot, func) == psdev->dev->devfn) {
131 pcistub_device_get(psdev);
132 goto out;
133 }
134 }
135
136 /* didn't find it */
137 psdev = NULL;
138
139out:
140 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
141 return psdev;
142}
143
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400144static struct pci_dev *pcistub_device_get_pci_dev(struct xen_pcibk_device *pdev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400145 struct pcistub_device *psdev)
146{
147 struct pci_dev *pci_dev = NULL;
148 unsigned long flags;
149
150 pcistub_device_get(psdev);
151
152 spin_lock_irqsave(&psdev->lock, flags);
153 if (!psdev->pdev) {
154 psdev->pdev = pdev;
155 pci_dev = psdev->dev;
156 }
157 spin_unlock_irqrestore(&psdev->lock, flags);
158
159 if (!pci_dev)
160 pcistub_device_put(psdev);
161
162 return pci_dev;
163}
164
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400165struct pci_dev *pcistub_get_pci_dev_by_slot(struct xen_pcibk_device *pdev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400166 int domain, int bus,
167 int slot, int func)
168{
169 struct pcistub_device *psdev;
170 struct pci_dev *found_dev = NULL;
171 unsigned long flags;
172
173 spin_lock_irqsave(&pcistub_devices_lock, flags);
174
175 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
176 if (psdev->dev != NULL
177 && domain == pci_domain_nr(psdev->dev->bus)
178 && bus == psdev->dev->bus->number
179 && PCI_DEVFN(slot, func) == psdev->dev->devfn) {
180 found_dev = pcistub_device_get_pci_dev(pdev, psdev);
181 break;
182 }
183 }
184
185 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
186 return found_dev;
187}
188
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400189struct pci_dev *pcistub_get_pci_dev(struct xen_pcibk_device *pdev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400190 struct pci_dev *dev)
191{
192 struct pcistub_device *psdev;
193 struct pci_dev *found_dev = NULL;
194 unsigned long flags;
195
196 spin_lock_irqsave(&pcistub_devices_lock, flags);
197
198 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
199 if (psdev->dev == dev) {
200 found_dev = pcistub_device_get_pci_dev(pdev, psdev);
201 break;
202 }
203 }
204
205 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
206 return found_dev;
207}
208
209void pcistub_put_pci_dev(struct pci_dev *dev)
210{
211 struct pcistub_device *psdev, *found_psdev = NULL;
212 unsigned long flags;
213
214 spin_lock_irqsave(&pcistub_devices_lock, flags);
215
216 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
217 if (psdev->dev == dev) {
218 found_psdev = psdev;
219 break;
220 }
221 }
222
223 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
Konrad Rzeszutek Wilk4645bf32011-09-29 13:12:43 -0400224 if (WARN_ON(!found_psdev))
225 return;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400226
227 /*hold this lock for avoiding breaking link between
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400228 * pcistub and xen_pcibk when AER is in processing
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400229 */
230 down_write(&pcistub_sem);
231 /* Cleanup our device
232 * (so it's ready for the next domain)
233 */
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400234 xen_pcibk_reset_device(found_psdev->dev);
235 xen_pcibk_config_free_dyn_fields(found_psdev->dev);
236 xen_pcibk_config_reset_dev(found_psdev->dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400237
238 spin_lock_irqsave(&found_psdev->lock, flags);
239 found_psdev->pdev = NULL;
240 spin_unlock_irqrestore(&found_psdev->lock, flags);
241
242 pcistub_device_put(found_psdev);
243 up_write(&pcistub_sem);
244}
245
246static int __devinit pcistub_match_one(struct pci_dev *dev,
247 struct pcistub_device_id *pdev_id)
248{
249 /* Match the specified device by domain, bus, slot, func and also if
250 * any of the device's parent bridges match.
251 */
252 for (; dev != NULL; dev = dev->bus->self) {
253 if (pci_domain_nr(dev->bus) == pdev_id->domain
254 && dev->bus->number == pdev_id->bus
255 && dev->devfn == pdev_id->devfn)
256 return 1;
257
258 /* Sometimes topmost bridge links to itself. */
259 if (dev == dev->bus->self)
260 break;
261 }
262
263 return 0;
264}
265
266static int __devinit pcistub_match(struct pci_dev *dev)
267{
268 struct pcistub_device_id *pdev_id;
269 unsigned long flags;
270 int found = 0;
271
272 spin_lock_irqsave(&device_ids_lock, flags);
273 list_for_each_entry(pdev_id, &pcistub_device_ids, slot_list) {
274 if (pcistub_match_one(dev, pdev_id)) {
275 found = 1;
276 break;
277 }
278 }
279 spin_unlock_irqrestore(&device_ids_lock, flags);
280
281 return found;
282}
283
284static int __devinit pcistub_init_device(struct pci_dev *dev)
285{
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400286 struct xen_pcibk_dev_data *dev_data;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400287 int err = 0;
288
289 dev_dbg(&dev->dev, "initializing...\n");
290
291 /* The PCI backend is not intended to be a module (or to work with
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400292 * removable PCI devices (yet). If it were, xen_pcibk_config_free()
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400293 * would need to be called somewhere to free the memory allocated
294 * here and then to call kfree(pci_get_drvdata(psdev->dev)).
295 */
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -0400296 dev_data = kzalloc(sizeof(*dev_data) + strlen(DRV_NAME "[]")
297 + strlen(pci_name(dev)) + 1, GFP_ATOMIC);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400298 if (!dev_data) {
299 err = -ENOMEM;
300 goto out;
301 }
302 pci_set_drvdata(dev, dev_data);
303
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -0400304 /*
305 * Setup name for fake IRQ handler. It will only be enabled
306 * once the device is turned on by the guest.
307 */
308 sprintf(dev_data->irq_name, DRV_NAME "[%s]", pci_name(dev));
309
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400310 dev_dbg(&dev->dev, "initializing config\n");
311
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400312 init_waitqueue_head(&xen_pcibk_aer_wait_queue);
313 err = xen_pcibk_config_init_dev(dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400314 if (err)
315 goto out;
316
317 /* HACK: Force device (& ACPI) to determine what IRQ it's on - we
318 * must do this here because pcibios_enable_device may specify
319 * the pci device's true irq (and possibly its other resources)
320 * if they differ from what's in the configuration space.
321 * This makes the assumption that the device's resources won't
322 * change after this point (otherwise this code may break!)
323 */
324 dev_dbg(&dev->dev, "enabling device\n");
325 err = pci_enable_device(dev);
326 if (err)
327 goto config_release;
328
329 /* Now disable the device (this also ensures some private device
330 * data is setup before we export)
331 */
332 dev_dbg(&dev->dev, "reset device\n");
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400333 xen_pcibk_reset_device(dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400334
Konrad Rzeszutek Wilk97309d32012-01-04 14:10:32 -0500335 dev->dev_flags |= PCI_DEV_FLAGS_ASSIGNED;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400336 return 0;
337
338config_release:
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400339 xen_pcibk_config_free_dev(dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400340
341out:
342 pci_set_drvdata(dev, NULL);
343 kfree(dev_data);
344 return err;
345}
346
347/*
348 * Because some initialization still happens on
349 * devices during fs_initcall, we need to defer
350 * full initialization of our devices until
351 * device_initcall.
352 */
353static int __init pcistub_init_devices_late(void)
354{
355 struct pcistub_device *psdev;
356 unsigned long flags;
357 int err = 0;
358
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400359 pr_debug(DRV_NAME ": pcistub_init_devices_late\n");
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400360
361 spin_lock_irqsave(&pcistub_devices_lock, flags);
362
363 while (!list_empty(&seized_devices)) {
364 psdev = container_of(seized_devices.next,
365 struct pcistub_device, dev_list);
366 list_del(&psdev->dev_list);
367
368 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
369
370 err = pcistub_init_device(psdev->dev);
371 if (err) {
372 dev_err(&psdev->dev->dev,
373 "error %d initializing device\n", err);
374 kfree(psdev);
375 psdev = NULL;
376 }
377
378 spin_lock_irqsave(&pcistub_devices_lock, flags);
379
380 if (psdev)
381 list_add_tail(&psdev->dev_list, &pcistub_devices);
382 }
383
384 initialize_devices = 1;
385
386 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
387
388 return 0;
389}
390
391static int __devinit pcistub_seize(struct pci_dev *dev)
392{
393 struct pcistub_device *psdev;
394 unsigned long flags;
395 int err = 0;
396
397 psdev = pcistub_device_alloc(dev);
398 if (!psdev)
399 return -ENOMEM;
400
401 spin_lock_irqsave(&pcistub_devices_lock, flags);
402
403 if (initialize_devices) {
404 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
405
406 /* don't want irqs disabled when calling pcistub_init_device */
407 err = pcistub_init_device(psdev->dev);
408
409 spin_lock_irqsave(&pcistub_devices_lock, flags);
410
411 if (!err)
412 list_add(&psdev->dev_list, &pcistub_devices);
413 } else {
414 dev_dbg(&dev->dev, "deferring initialization\n");
415 list_add(&psdev->dev_list, &seized_devices);
416 }
417
418 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
419
420 if (err)
421 pcistub_device_put(psdev);
422
423 return err;
424}
425
426static int __devinit pcistub_probe(struct pci_dev *dev,
427 const struct pci_device_id *id)
428{
429 int err = 0;
430
431 dev_dbg(&dev->dev, "probing...\n");
432
433 if (pcistub_match(dev)) {
434
435 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL
436 && dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
437 dev_err(&dev->dev, "can't export pci devices that "
438 "don't have a normal (0) or bridge (1) "
439 "header type!\n");
440 err = -ENODEV;
441 goto out;
442 }
443
444 dev_info(&dev->dev, "seizing device\n");
445 err = pcistub_seize(dev);
446 } else
447 /* Didn't find the device */
448 err = -ENODEV;
449
450out:
451 return err;
452}
453
454static void pcistub_remove(struct pci_dev *dev)
455{
456 struct pcistub_device *psdev, *found_psdev = NULL;
457 unsigned long flags;
458
459 dev_dbg(&dev->dev, "removing\n");
460
461 spin_lock_irqsave(&pcistub_devices_lock, flags);
462
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400463 xen_pcibk_config_quirk_release(dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400464
465 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
466 if (psdev->dev == dev) {
467 found_psdev = psdev;
468 break;
469 }
470 }
471
472 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
473
474 if (found_psdev) {
475 dev_dbg(&dev->dev, "found device to remove - in use? %p\n",
476 found_psdev->pdev);
477
478 if (found_psdev->pdev) {
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400479 printk(KERN_WARNING DRV_NAME ": ****** removing device "
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400480 "%s while still in-use! ******\n",
481 pci_name(found_psdev->dev));
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400482 printk(KERN_WARNING DRV_NAME ": ****** driver domain may"
483 " still access this device's i/o resources!\n");
484 printk(KERN_WARNING DRV_NAME ": ****** shutdown driver "
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400485 "domain before binding device\n");
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400486 printk(KERN_WARNING DRV_NAME ": ****** to other drivers "
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400487 "or domains\n");
488
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400489 xen_pcibk_release_pci_dev(found_psdev->pdev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400490 found_psdev->dev);
491 }
492
493 spin_lock_irqsave(&pcistub_devices_lock, flags);
494 list_del(&found_psdev->dev_list);
495 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
496
497 /* the final put for releasing from the list */
498 pcistub_device_put(found_psdev);
499 }
500}
501
Konrad Rzeszutek Wilk8bfd4e02011-07-19 20:09:43 -0400502static DEFINE_PCI_DEVICE_TABLE(pcistub_ids) = {
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400503 {
504 .vendor = PCI_ANY_ID,
505 .device = PCI_ANY_ID,
506 .subvendor = PCI_ANY_ID,
507 .subdevice = PCI_ANY_ID,
508 },
509 {0,},
510};
511
512#define PCI_NODENAME_MAX 40
513static void kill_domain_by_device(struct pcistub_device *psdev)
514{
515 struct xenbus_transaction xbt;
516 int err;
517 char nodename[PCI_NODENAME_MAX];
518
Konrad Rzeszutek Wilk72bf8092011-09-29 13:43:28 -0400519 BUG_ON(!psdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400520 snprintf(nodename, PCI_NODENAME_MAX, "/local/domain/0/backend/pci/%d/0",
521 psdev->pdev->xdev->otherend_id);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400522
523again:
524 err = xenbus_transaction_start(&xbt);
525 if (err) {
526 dev_err(&psdev->dev->dev,
527 "error %d when start xenbus transaction\n", err);
528 return;
529 }
530 /*PV AER handlers will set this flag*/
531 xenbus_printf(xbt, nodename, "aerState" , "aerfail");
532 err = xenbus_transaction_end(xbt, 0);
533 if (err) {
534 if (err == -EAGAIN)
535 goto again;
536 dev_err(&psdev->dev->dev,
537 "error %d when end xenbus transaction\n", err);
538 return;
539 }
540}
541
542/* For each aer recovery step error_detected, mmio_enabled, etc, front_end and
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400543 * backend need to have cooperation. In xen_pcibk, those steps will do similar
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400544 * jobs: send service request and waiting for front_end response.
545*/
546static pci_ers_result_t common_process(struct pcistub_device *psdev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400547 pci_channel_state_t state, int aer_cmd,
548 pci_ers_result_t result)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400549{
550 pci_ers_result_t res = result;
551 struct xen_pcie_aer_op *aer_op;
552 int ret;
553
554 /*with PV AER drivers*/
555 aer_op = &(psdev->pdev->sh_info->aer_op);
556 aer_op->cmd = aer_cmd ;
557 /*useful for error_detected callback*/
558 aer_op->err = state;
559 /*pcifront_end BDF*/
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400560 ret = xen_pcibk_get_pcifront_dev(psdev->dev, psdev->pdev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400561 &aer_op->domain, &aer_op->bus, &aer_op->devfn);
562 if (!ret) {
563 dev_err(&psdev->dev->dev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400564 DRV_NAME ": failed to get pcifront device\n");
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400565 return PCI_ERS_RESULT_NONE;
566 }
567 wmb();
568
569 dev_dbg(&psdev->dev->dev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400570 DRV_NAME ": aer_op %x dom %x bus %x devfn %x\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400571 aer_cmd, aer_op->domain, aer_op->bus, aer_op->devfn);
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400572 /*local flag to mark there's aer request, xen_pcibk callback will use
573 * this flag to judge whether we need to check pci-front give aer
574 * service ack signal
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400575 */
576 set_bit(_PCIB_op_pending, (unsigned long *)&psdev->pdev->flags);
577
578 /*It is possible that a pcifront conf_read_write ops request invokes
579 * the callback which cause the spurious execution of wake_up.
580 * Yet it is harmless and better than a spinlock here
581 */
582 set_bit(_XEN_PCIB_active,
583 (unsigned long *)&psdev->pdev->sh_info->flags);
584 wmb();
585 notify_remote_via_irq(psdev->pdev->evtchn_irq);
586
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400587 ret = wait_event_timeout(xen_pcibk_aer_wait_queue,
588 !(test_bit(_XEN_PCIB_active, (unsigned long *)
589 &psdev->pdev->sh_info->flags)), 300*HZ);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400590
591 if (!ret) {
592 if (test_bit(_XEN_PCIB_active,
593 (unsigned long *)&psdev->pdev->sh_info->flags)) {
594 dev_err(&psdev->dev->dev,
595 "pcifront aer process not responding!\n");
596 clear_bit(_XEN_PCIB_active,
597 (unsigned long *)&psdev->pdev->sh_info->flags);
598 aer_op->err = PCI_ERS_RESULT_NONE;
599 return res;
600 }
601 }
602 clear_bit(_PCIB_op_pending, (unsigned long *)&psdev->pdev->flags);
603
604 if (test_bit(_XEN_PCIF_active,
605 (unsigned long *)&psdev->pdev->sh_info->flags)) {
606 dev_dbg(&psdev->dev->dev,
Jan Beulich402c5e12011-09-21 16:22:11 -0400607 "schedule pci_conf service in " DRV_NAME "\n");
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400608 xen_pcibk_test_and_schedule_op(psdev->pdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400609 }
610
611 res = (pci_ers_result_t)aer_op->err;
612 return res;
613}
614
615/*
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400616* xen_pcibk_slot_reset: it will send the slot_reset request to pcifront in case
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400617* of the device driver could provide this service, and then wait for pcifront
618* ack.
619* @dev: pointer to PCI devices
620* return value is used by aer_core do_recovery policy
621*/
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400622static pci_ers_result_t xen_pcibk_slot_reset(struct pci_dev *dev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400623{
624 struct pcistub_device *psdev;
625 pci_ers_result_t result;
626
627 result = PCI_ERS_RESULT_RECOVERED;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400628 dev_dbg(&dev->dev, "xen_pcibk_slot_reset(bus:%x,devfn:%x)\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400629 dev->bus->number, dev->devfn);
630
631 down_write(&pcistub_sem);
632 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
633 dev->bus->number,
634 PCI_SLOT(dev->devfn),
635 PCI_FUNC(dev->devfn));
636
637 if (!psdev || !psdev->pdev) {
638 dev_err(&dev->dev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400639 DRV_NAME " device is not found/assigned\n");
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400640 goto end;
641 }
642
643 if (!psdev->pdev->sh_info) {
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400644 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400645 " by HVM, kill it\n");
646 kill_domain_by_device(psdev);
647 goto release;
648 }
649
650 if (!test_bit(_XEN_PCIB_AERHANDLER,
651 (unsigned long *)&psdev->pdev->sh_info->flags)) {
652 dev_err(&dev->dev,
653 "guest with no AER driver should have been killed\n");
654 goto release;
655 }
656 result = common_process(psdev, 1, XEN_PCI_OP_aer_slotreset, result);
657
658 if (result == PCI_ERS_RESULT_NONE ||
659 result == PCI_ERS_RESULT_DISCONNECT) {
660 dev_dbg(&dev->dev,
661 "No AER slot_reset service or disconnected!\n");
662 kill_domain_by_device(psdev);
663 }
664release:
665 pcistub_device_put(psdev);
666end:
667 up_write(&pcistub_sem);
668 return result;
669
670}
671
672
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400673/*xen_pcibk_mmio_enabled: it will send the mmio_enabled request to pcifront
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400674* in case of the device driver could provide this service, and then wait
675* for pcifront ack
676* @dev: pointer to PCI devices
677* return value is used by aer_core do_recovery policy
678*/
679
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400680static pci_ers_result_t xen_pcibk_mmio_enabled(struct pci_dev *dev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400681{
682 struct pcistub_device *psdev;
683 pci_ers_result_t result;
684
685 result = PCI_ERS_RESULT_RECOVERED;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400686 dev_dbg(&dev->dev, "xen_pcibk_mmio_enabled(bus:%x,devfn:%x)\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400687 dev->bus->number, dev->devfn);
688
689 down_write(&pcistub_sem);
690 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
691 dev->bus->number,
692 PCI_SLOT(dev->devfn),
693 PCI_FUNC(dev->devfn));
694
695 if (!psdev || !psdev->pdev) {
696 dev_err(&dev->dev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400697 DRV_NAME " device is not found/assigned\n");
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400698 goto end;
699 }
700
701 if (!psdev->pdev->sh_info) {
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400702 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400703 " by HVM, kill it\n");
704 kill_domain_by_device(psdev);
705 goto release;
706 }
707
708 if (!test_bit(_XEN_PCIB_AERHANDLER,
709 (unsigned long *)&psdev->pdev->sh_info->flags)) {
710 dev_err(&dev->dev,
711 "guest with no AER driver should have been killed\n");
712 goto release;
713 }
714 result = common_process(psdev, 1, XEN_PCI_OP_aer_mmio, result);
715
716 if (result == PCI_ERS_RESULT_NONE ||
717 result == PCI_ERS_RESULT_DISCONNECT) {
718 dev_dbg(&dev->dev,
719 "No AER mmio_enabled service or disconnected!\n");
720 kill_domain_by_device(psdev);
721 }
722release:
723 pcistub_device_put(psdev);
724end:
725 up_write(&pcistub_sem);
726 return result;
727}
728
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400729/*xen_pcibk_error_detected: it will send the error_detected request to pcifront
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400730* in case of the device driver could provide this service, and then wait
731* for pcifront ack.
732* @dev: pointer to PCI devices
733* @error: the current PCI connection state
734* return value is used by aer_core do_recovery policy
735*/
736
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400737static pci_ers_result_t xen_pcibk_error_detected(struct pci_dev *dev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400738 pci_channel_state_t error)
739{
740 struct pcistub_device *psdev;
741 pci_ers_result_t result;
742
743 result = PCI_ERS_RESULT_CAN_RECOVER;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400744 dev_dbg(&dev->dev, "xen_pcibk_error_detected(bus:%x,devfn:%x)\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400745 dev->bus->number, dev->devfn);
746
747 down_write(&pcistub_sem);
748 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
749 dev->bus->number,
750 PCI_SLOT(dev->devfn),
751 PCI_FUNC(dev->devfn));
752
753 if (!psdev || !psdev->pdev) {
754 dev_err(&dev->dev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400755 DRV_NAME " device is not found/assigned\n");
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400756 goto end;
757 }
758
759 if (!psdev->pdev->sh_info) {
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400760 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400761 " by HVM, kill it\n");
762 kill_domain_by_device(psdev);
763 goto release;
764 }
765
766 /*Guest owns the device yet no aer handler regiested, kill guest*/
767 if (!test_bit(_XEN_PCIB_AERHANDLER,
768 (unsigned long *)&psdev->pdev->sh_info->flags)) {
769 dev_dbg(&dev->dev, "guest may have no aer driver, kill it\n");
770 kill_domain_by_device(psdev);
771 goto release;
772 }
773 result = common_process(psdev, error, XEN_PCI_OP_aer_detected, result);
774
775 if (result == PCI_ERS_RESULT_NONE ||
776 result == PCI_ERS_RESULT_DISCONNECT) {
777 dev_dbg(&dev->dev,
778 "No AER error_detected service or disconnected!\n");
779 kill_domain_by_device(psdev);
780 }
781release:
782 pcistub_device_put(psdev);
783end:
784 up_write(&pcistub_sem);
785 return result;
786}
787
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400788/*xen_pcibk_error_resume: it will send the error_resume request to pcifront
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400789* in case of the device driver could provide this service, and then wait
790* for pcifront ack.
791* @dev: pointer to PCI devices
792*/
793
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400794static void xen_pcibk_error_resume(struct pci_dev *dev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400795{
796 struct pcistub_device *psdev;
797
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400798 dev_dbg(&dev->dev, "xen_pcibk_error_resume(bus:%x,devfn:%x)\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400799 dev->bus->number, dev->devfn);
800
801 down_write(&pcistub_sem);
802 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
803 dev->bus->number,
804 PCI_SLOT(dev->devfn),
805 PCI_FUNC(dev->devfn));
806
807 if (!psdev || !psdev->pdev) {
808 dev_err(&dev->dev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400809 DRV_NAME " device is not found/assigned\n");
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400810 goto end;
811 }
812
813 if (!psdev->pdev->sh_info) {
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400814 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400815 " by HVM, kill it\n");
816 kill_domain_by_device(psdev);
817 goto release;
818 }
819
820 if (!test_bit(_XEN_PCIB_AERHANDLER,
821 (unsigned long *)&psdev->pdev->sh_info->flags)) {
822 dev_err(&dev->dev,
823 "guest with no AER driver should have been killed\n");
824 kill_domain_by_device(psdev);
825 goto release;
826 }
827 common_process(psdev, 1, XEN_PCI_OP_aer_resume,
828 PCI_ERS_RESULT_RECOVERED);
829release:
830 pcistub_device_put(psdev);
831end:
832 up_write(&pcistub_sem);
833 return;
834}
835
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400836/*add xen_pcibk AER handling*/
837static struct pci_error_handlers xen_pcibk_error_handler = {
838 .error_detected = xen_pcibk_error_detected,
839 .mmio_enabled = xen_pcibk_mmio_enabled,
840 .slot_reset = xen_pcibk_slot_reset,
841 .resume = xen_pcibk_error_resume,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400842};
843
844/*
845 * Note: There is no MODULE_DEVICE_TABLE entry here because this isn't
846 * for a normal device. I don't want it to be loaded automatically.
847 */
848
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400849static struct pci_driver xen_pcibk_pci_driver = {
850 /* The name should be xen_pciback, but until the tools are updated
851 * we will keep it as pciback. */
852 .name = "pciback",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400853 .id_table = pcistub_ids,
854 .probe = pcistub_probe,
855 .remove = pcistub_remove,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400856 .err_handler = &xen_pcibk_error_handler,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400857};
858
859static inline int str_to_slot(const char *buf, int *domain, int *bus,
860 int *slot, int *func)
861{
862 int err;
863
864 err = sscanf(buf, " %x:%x:%x.%x", domain, bus, slot, func);
865 if (err == 4)
866 return 0;
867 else if (err < 0)
868 return -EINVAL;
869
870 /* try again without domain */
871 *domain = 0;
872 err = sscanf(buf, " %x:%x.%x", bus, slot, func);
873 if (err == 3)
874 return 0;
875
876 return -EINVAL;
877}
878
879static inline int str_to_quirk(const char *buf, int *domain, int *bus, int
880 *slot, int *func, int *reg, int *size, int *mask)
881{
882 int err;
883
884 err =
885 sscanf(buf, " %04x:%02x:%02x.%1x-%08x:%1x:%08x", domain, bus, slot,
886 func, reg, size, mask);
887 if (err == 7)
888 return 0;
889 return -EINVAL;
890}
891
892static int pcistub_device_id_add(int domain, int bus, int slot, int func)
893{
894 struct pcistub_device_id *pci_dev_id;
895 unsigned long flags;
896
897 pci_dev_id = kmalloc(sizeof(*pci_dev_id), GFP_KERNEL);
898 if (!pci_dev_id)
899 return -ENOMEM;
900
901 pci_dev_id->domain = domain;
902 pci_dev_id->bus = bus;
903 pci_dev_id->devfn = PCI_DEVFN(slot, func);
904
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400905 pr_debug(DRV_NAME ": wants to seize %04x:%02x:%02x.%01x\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400906 domain, bus, slot, func);
907
908 spin_lock_irqsave(&device_ids_lock, flags);
909 list_add_tail(&pci_dev_id->slot_list, &pcistub_device_ids);
910 spin_unlock_irqrestore(&device_ids_lock, flags);
911
912 return 0;
913}
914
915static int pcistub_device_id_remove(int domain, int bus, int slot, int func)
916{
917 struct pcistub_device_id *pci_dev_id, *t;
918 int devfn = PCI_DEVFN(slot, func);
919 int err = -ENOENT;
920 unsigned long flags;
921
922 spin_lock_irqsave(&device_ids_lock, flags);
923 list_for_each_entry_safe(pci_dev_id, t, &pcistub_device_ids,
924 slot_list) {
925 if (pci_dev_id->domain == domain
926 && pci_dev_id->bus == bus && pci_dev_id->devfn == devfn) {
927 /* Don't break; here because it's possible the same
928 * slot could be in the list more than once
929 */
930 list_del(&pci_dev_id->slot_list);
931 kfree(pci_dev_id);
932
933 err = 0;
934
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400935 pr_debug(DRV_NAME ": removed %04x:%02x:%02x.%01x from "
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400936 "seize list\n", domain, bus, slot, func);
937 }
938 }
939 spin_unlock_irqrestore(&device_ids_lock, flags);
940
941 return err;
942}
943
944static int pcistub_reg_add(int domain, int bus, int slot, int func, int reg,
945 int size, int mask)
946{
947 int err = 0;
948 struct pcistub_device *psdev;
949 struct pci_dev *dev;
950 struct config_field *field;
951
952 psdev = pcistub_device_find(domain, bus, slot, func);
953 if (!psdev || !psdev->dev) {
954 err = -ENODEV;
955 goto out;
956 }
957 dev = psdev->dev;
958
959 field = kzalloc(sizeof(*field), GFP_ATOMIC);
960 if (!field) {
961 err = -ENOMEM;
962 goto out;
963 }
964
965 field->offset = reg;
966 field->size = size;
967 field->mask = mask;
968 field->init = NULL;
969 field->reset = NULL;
970 field->release = NULL;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400971 field->clean = xen_pcibk_config_field_free;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400972
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400973 err = xen_pcibk_config_quirks_add_field(dev, field);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400974 if (err)
975 kfree(field);
976out:
977 return err;
978}
979
980static ssize_t pcistub_slot_add(struct device_driver *drv, const char *buf,
981 size_t count)
982{
983 int domain, bus, slot, func;
984 int err;
985
986 err = str_to_slot(buf, &domain, &bus, &slot, &func);
987 if (err)
988 goto out;
989
990 err = pcistub_device_id_add(domain, bus, slot, func);
991
992out:
993 if (!err)
994 err = count;
995 return err;
996}
Jan Beulich402c5e12011-09-21 16:22:11 -0400997static DRIVER_ATTR(new_slot, S_IWUSR, NULL, pcistub_slot_add);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400998
999static ssize_t pcistub_slot_remove(struct device_driver *drv, const char *buf,
1000 size_t count)
1001{
1002 int domain, bus, slot, func;
1003 int err;
1004
1005 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1006 if (err)
1007 goto out;
1008
1009 err = pcistub_device_id_remove(domain, bus, slot, func);
1010
1011out:
1012 if (!err)
1013 err = count;
1014 return err;
1015}
Jan Beulich402c5e12011-09-21 16:22:11 -04001016static DRIVER_ATTR(remove_slot, S_IWUSR, NULL, pcistub_slot_remove);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001017
1018static ssize_t pcistub_slot_show(struct device_driver *drv, char *buf)
1019{
1020 struct pcistub_device_id *pci_dev_id;
1021 size_t count = 0;
1022 unsigned long flags;
1023
1024 spin_lock_irqsave(&device_ids_lock, flags);
1025 list_for_each_entry(pci_dev_id, &pcistub_device_ids, slot_list) {
1026 if (count >= PAGE_SIZE)
1027 break;
1028
1029 count += scnprintf(buf + count, PAGE_SIZE - count,
1030 "%04x:%02x:%02x.%01x\n",
1031 pci_dev_id->domain, pci_dev_id->bus,
1032 PCI_SLOT(pci_dev_id->devfn),
1033 PCI_FUNC(pci_dev_id->devfn));
1034 }
1035 spin_unlock_irqrestore(&device_ids_lock, flags);
1036
1037 return count;
1038}
Jan Beulich402c5e12011-09-21 16:22:11 -04001039static DRIVER_ATTR(slots, S_IRUSR, pcistub_slot_show, NULL);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001040
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001041static ssize_t pcistub_irq_handler_show(struct device_driver *drv, char *buf)
1042{
1043 struct pcistub_device *psdev;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001044 struct xen_pcibk_dev_data *dev_data;
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001045 size_t count = 0;
1046 unsigned long flags;
1047
1048 spin_lock_irqsave(&pcistub_devices_lock, flags);
1049 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1050 if (count >= PAGE_SIZE)
1051 break;
1052 if (!psdev->dev)
1053 continue;
1054 dev_data = pci_get_drvdata(psdev->dev);
1055 if (!dev_data)
1056 continue;
1057 count +=
1058 scnprintf(buf + count, PAGE_SIZE - count,
1059 "%s:%s:%sing:%ld\n",
1060 pci_name(psdev->dev),
1061 dev_data->isr_on ? "on" : "off",
1062 dev_data->ack_intr ? "ack" : "not ack",
1063 dev_data->handled);
1064 }
1065 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1066 return count;
1067}
Jan Beulich402c5e12011-09-21 16:22:11 -04001068static DRIVER_ATTR(irq_handlers, S_IRUSR, pcistub_irq_handler_show, NULL);
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001069
1070static ssize_t pcistub_irq_handler_switch(struct device_driver *drv,
1071 const char *buf,
1072 size_t count)
1073{
1074 struct pcistub_device *psdev;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001075 struct xen_pcibk_dev_data *dev_data;
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001076 int domain, bus, slot, func;
1077 int err = -ENOENT;
1078
1079 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1080 if (err)
1081 goto out;
1082
1083 psdev = pcistub_device_find(domain, bus, slot, func);
1084
1085 if (!psdev)
1086 goto out;
1087
1088 dev_data = pci_get_drvdata(psdev->dev);
1089 if (!dev_data)
1090 goto out;
1091
1092 dev_dbg(&psdev->dev->dev, "%s fake irq handler: %d->%d\n",
1093 dev_data->irq_name, dev_data->isr_on,
1094 !dev_data->isr_on);
1095
1096 dev_data->isr_on = !(dev_data->isr_on);
1097 if (dev_data->isr_on)
1098 dev_data->ack_intr = 1;
1099out:
1100 if (!err)
1101 err = count;
1102 return err;
1103}
Jan Beulich402c5e12011-09-21 16:22:11 -04001104static DRIVER_ATTR(irq_handler_state, S_IWUSR, NULL,
1105 pcistub_irq_handler_switch);
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001106
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001107static ssize_t pcistub_quirk_add(struct device_driver *drv, const char *buf,
1108 size_t count)
1109{
1110 int domain, bus, slot, func, reg, size, mask;
1111 int err;
1112
1113 err = str_to_quirk(buf, &domain, &bus, &slot, &func, &reg, &size,
1114 &mask);
1115 if (err)
1116 goto out;
1117
1118 err = pcistub_reg_add(domain, bus, slot, func, reg, size, mask);
1119
1120out:
1121 if (!err)
1122 err = count;
1123 return err;
1124}
1125
1126static ssize_t pcistub_quirk_show(struct device_driver *drv, char *buf)
1127{
1128 int count = 0;
1129 unsigned long flags;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001130 struct xen_pcibk_config_quirk *quirk;
1131 struct xen_pcibk_dev_data *dev_data;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001132 const struct config_field *field;
1133 const struct config_field_entry *cfg_entry;
1134
1135 spin_lock_irqsave(&device_ids_lock, flags);
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001136 list_for_each_entry(quirk, &xen_pcibk_quirks, quirks_list) {
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001137 if (count >= PAGE_SIZE)
1138 goto out;
1139
1140 count += scnprintf(buf + count, PAGE_SIZE - count,
1141 "%02x:%02x.%01x\n\t%04x:%04x:%04x:%04x\n",
1142 quirk->pdev->bus->number,
1143 PCI_SLOT(quirk->pdev->devfn),
1144 PCI_FUNC(quirk->pdev->devfn),
1145 quirk->devid.vendor, quirk->devid.device,
1146 quirk->devid.subvendor,
1147 quirk->devid.subdevice);
1148
1149 dev_data = pci_get_drvdata(quirk->pdev);
1150
1151 list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
1152 field = cfg_entry->field;
1153 if (count >= PAGE_SIZE)
1154 goto out;
1155
1156 count += scnprintf(buf + count, PAGE_SIZE - count,
1157 "\t\t%08x:%01x:%08x\n",
1158 cfg_entry->base_offset +
1159 field->offset, field->size,
1160 field->mask);
1161 }
1162 }
1163
1164out:
1165 spin_unlock_irqrestore(&device_ids_lock, flags);
1166
1167 return count;
1168}
Jan Beulich402c5e12011-09-21 16:22:11 -04001169static DRIVER_ATTR(quirks, S_IRUSR | S_IWUSR, pcistub_quirk_show,
1170 pcistub_quirk_add);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001171
1172static ssize_t permissive_add(struct device_driver *drv, const char *buf,
1173 size_t count)
1174{
1175 int domain, bus, slot, func;
1176 int err;
1177 struct pcistub_device *psdev;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001178 struct xen_pcibk_dev_data *dev_data;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001179 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1180 if (err)
1181 goto out;
1182 psdev = pcistub_device_find(domain, bus, slot, func);
1183 if (!psdev) {
1184 err = -ENODEV;
1185 goto out;
1186 }
1187 if (!psdev->dev) {
1188 err = -ENODEV;
1189 goto release;
1190 }
1191 dev_data = pci_get_drvdata(psdev->dev);
1192 /* the driver data for a device should never be null at this point */
1193 if (!dev_data) {
1194 err = -ENXIO;
1195 goto release;
1196 }
1197 if (!dev_data->permissive) {
1198 dev_data->permissive = 1;
1199 /* Let user know that what they're doing could be unsafe */
1200 dev_warn(&psdev->dev->dev, "enabling permissive mode "
1201 "configuration space accesses!\n");
1202 dev_warn(&psdev->dev->dev,
1203 "permissive mode is potentially unsafe!\n");
1204 }
1205release:
1206 pcistub_device_put(psdev);
1207out:
1208 if (!err)
1209 err = count;
1210 return err;
1211}
1212
1213static ssize_t permissive_show(struct device_driver *drv, char *buf)
1214{
1215 struct pcistub_device *psdev;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001216 struct xen_pcibk_dev_data *dev_data;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001217 size_t count = 0;
1218 unsigned long flags;
1219 spin_lock_irqsave(&pcistub_devices_lock, flags);
1220 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1221 if (count >= PAGE_SIZE)
1222 break;
1223 if (!psdev->dev)
1224 continue;
1225 dev_data = pci_get_drvdata(psdev->dev);
1226 if (!dev_data || !dev_data->permissive)
1227 continue;
1228 count +=
1229 scnprintf(buf + count, PAGE_SIZE - count, "%s\n",
1230 pci_name(psdev->dev));
1231 }
1232 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1233 return count;
1234}
Jan Beulich402c5e12011-09-21 16:22:11 -04001235static DRIVER_ATTR(permissive, S_IRUSR | S_IWUSR, permissive_show,
1236 permissive_add);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001237
1238static void pcistub_exit(void)
1239{
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001240 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_new_slot);
1241 driver_remove_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001242 &driver_attr_remove_slot);
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001243 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_slots);
1244 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_quirks);
1245 driver_remove_file(&xen_pcibk_pci_driver.driver,
1246 &driver_attr_permissive);
1247 driver_remove_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001248 &driver_attr_irq_handlers);
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001249 driver_remove_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001250 &driver_attr_irq_handler_state);
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001251 pci_unregister_driver(&xen_pcibk_pci_driver);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001252}
1253
1254static int __init pcistub_init(void)
1255{
1256 int pos = 0;
1257 int err = 0;
1258 int domain, bus, slot, func;
1259 int parsed;
1260
1261 if (pci_devs_to_hide && *pci_devs_to_hide) {
1262 do {
1263 parsed = 0;
1264
1265 err = sscanf(pci_devs_to_hide + pos,
1266 " (%x:%x:%x.%x) %n",
1267 &domain, &bus, &slot, &func, &parsed);
1268 if (err != 4) {
1269 domain = 0;
1270 err = sscanf(pci_devs_to_hide + pos,
1271 " (%x:%x.%x) %n",
1272 &bus, &slot, &func, &parsed);
1273 if (err != 3)
1274 goto parse_error;
1275 }
1276
1277 err = pcistub_device_id_add(domain, bus, slot, func);
1278 if (err)
1279 goto out;
1280
1281 /* if parsed<=0, we've reached the end of the string */
1282 pos += parsed;
1283 } while (parsed > 0 && pci_devs_to_hide[pos]);
1284 }
1285
1286 /* If we're the first PCI Device Driver to register, we're the
1287 * first one to get offered PCI devices as they become
1288 * available (and thus we can be the first to grab them)
1289 */
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001290 err = pci_register_driver(&xen_pcibk_pci_driver);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001291 if (err < 0)
1292 goto out;
1293
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001294 err = driver_create_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001295 &driver_attr_new_slot);
1296 if (!err)
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001297 err = driver_create_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001298 &driver_attr_remove_slot);
1299 if (!err)
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001300 err = driver_create_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001301 &driver_attr_slots);
1302 if (!err)
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001303 err = driver_create_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001304 &driver_attr_quirks);
1305 if (!err)
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001306 err = driver_create_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001307 &driver_attr_permissive);
1308
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001309 if (!err)
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001310 err = driver_create_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001311 &driver_attr_irq_handlers);
1312 if (!err)
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001313 err = driver_create_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001314 &driver_attr_irq_handler_state);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001315 if (err)
1316 pcistub_exit();
1317
1318out:
1319 return err;
1320
1321parse_error:
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001322 printk(KERN_ERR DRV_NAME ": Error parsing pci_devs_to_hide at \"%s\"\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001323 pci_devs_to_hide + pos);
1324 return -EINVAL;
1325}
1326
1327#ifndef MODULE
1328/*
1329 * fs_initcall happens before device_initcall
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001330 * so xen_pcibk *should* get called first (b/c we
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001331 * want to suck up any device before other drivers
1332 * get a chance by being the first pci device
1333 * driver to register)
1334 */
1335fs_initcall(pcistub_init);
1336#endif
1337
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001338static int __init xen_pcibk_init(void)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001339{
1340 int err;
1341
1342 if (!xen_initial_domain())
1343 return -ENODEV;
1344
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001345 err = xen_pcibk_config_init();
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001346 if (err)
1347 return err;
1348
1349#ifdef MODULE
1350 err = pcistub_init();
1351 if (err < 0)
1352 return err;
1353#endif
1354
1355 pcistub_init_devices_late();
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001356 err = xen_pcibk_xenbus_register();
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001357 if (err)
1358 pcistub_exit();
1359
1360 return err;
1361}
1362
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001363static void __exit xen_pcibk_cleanup(void)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001364{
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001365 xen_pcibk_xenbus_unregister();
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001366 pcistub_exit();
1367}
1368
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001369module_init(xen_pcibk_init);
1370module_exit(xen_pcibk_cleanup);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001371
1372MODULE_LICENSE("Dual BSD/GPL");
Jan Beulich402c5e12011-09-21 16:22:11 -04001373MODULE_ALIAS("xen-backend:pci");