blob: 3cc3fbe5bf8d7eb6b2729602f15662fca9ba009a [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
102 pci_dev_put(psdev->dev);
103
104 kfree(psdev);
105}
106
107static inline void pcistub_device_get(struct pcistub_device *psdev)
108{
109 kref_get(&psdev->kref);
110}
111
112static inline void pcistub_device_put(struct pcistub_device *psdev)
113{
114 kref_put(&psdev->kref, pcistub_device_release);
115}
116
117static struct pcistub_device *pcistub_device_find(int domain, int bus,
118 int slot, int func)
119{
120 struct pcistub_device *psdev = NULL;
121 unsigned long flags;
122
123 spin_lock_irqsave(&pcistub_devices_lock, flags);
124
125 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
126 if (psdev->dev != NULL
127 && domain == pci_domain_nr(psdev->dev->bus)
128 && bus == psdev->dev->bus->number
129 && PCI_DEVFN(slot, func) == psdev->dev->devfn) {
130 pcistub_device_get(psdev);
131 goto out;
132 }
133 }
134
135 /* didn't find it */
136 psdev = NULL;
137
138out:
139 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
140 return psdev;
141}
142
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400143static struct pci_dev *pcistub_device_get_pci_dev(struct xen_pcibk_device *pdev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400144 struct pcistub_device *psdev)
145{
146 struct pci_dev *pci_dev = NULL;
147 unsigned long flags;
148
149 pcistub_device_get(psdev);
150
151 spin_lock_irqsave(&psdev->lock, flags);
152 if (!psdev->pdev) {
153 psdev->pdev = pdev;
154 pci_dev = psdev->dev;
155 }
156 spin_unlock_irqrestore(&psdev->lock, flags);
157
158 if (!pci_dev)
159 pcistub_device_put(psdev);
160
161 return pci_dev;
162}
163
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400164struct pci_dev *pcistub_get_pci_dev_by_slot(struct xen_pcibk_device *pdev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400165 int domain, int bus,
166 int slot, int func)
167{
168 struct pcistub_device *psdev;
169 struct pci_dev *found_dev = NULL;
170 unsigned long flags;
171
172 spin_lock_irqsave(&pcistub_devices_lock, flags);
173
174 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
175 if (psdev->dev != NULL
176 && domain == pci_domain_nr(psdev->dev->bus)
177 && bus == psdev->dev->bus->number
178 && PCI_DEVFN(slot, func) == psdev->dev->devfn) {
179 found_dev = pcistub_device_get_pci_dev(pdev, psdev);
180 break;
181 }
182 }
183
184 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
185 return found_dev;
186}
187
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400188struct pci_dev *pcistub_get_pci_dev(struct xen_pcibk_device *pdev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400189 struct pci_dev *dev)
190{
191 struct pcistub_device *psdev;
192 struct pci_dev *found_dev = NULL;
193 unsigned long flags;
194
195 spin_lock_irqsave(&pcistub_devices_lock, flags);
196
197 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
198 if (psdev->dev == dev) {
199 found_dev = pcistub_device_get_pci_dev(pdev, psdev);
200 break;
201 }
202 }
203
204 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
205 return found_dev;
206}
207
208void pcistub_put_pci_dev(struct pci_dev *dev)
209{
210 struct pcistub_device *psdev, *found_psdev = 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_psdev = psdev;
218 break;
219 }
220 }
221
222 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
223
224 /*hold this lock for avoiding breaking link between
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400225 * pcistub and xen_pcibk when AER is in processing
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400226 */
227 down_write(&pcistub_sem);
228 /* Cleanup our device
229 * (so it's ready for the next domain)
230 */
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400231 xen_pcibk_reset_device(found_psdev->dev);
232 xen_pcibk_config_free_dyn_fields(found_psdev->dev);
233 xen_pcibk_config_reset_dev(found_psdev->dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400234
235 spin_lock_irqsave(&found_psdev->lock, flags);
236 found_psdev->pdev = NULL;
237 spin_unlock_irqrestore(&found_psdev->lock, flags);
238
239 pcistub_device_put(found_psdev);
240 up_write(&pcistub_sem);
241}
242
243static int __devinit pcistub_match_one(struct pci_dev *dev,
244 struct pcistub_device_id *pdev_id)
245{
246 /* Match the specified device by domain, bus, slot, func and also if
247 * any of the device's parent bridges match.
248 */
249 for (; dev != NULL; dev = dev->bus->self) {
250 if (pci_domain_nr(dev->bus) == pdev_id->domain
251 && dev->bus->number == pdev_id->bus
252 && dev->devfn == pdev_id->devfn)
253 return 1;
254
255 /* Sometimes topmost bridge links to itself. */
256 if (dev == dev->bus->self)
257 break;
258 }
259
260 return 0;
261}
262
263static int __devinit pcistub_match(struct pci_dev *dev)
264{
265 struct pcistub_device_id *pdev_id;
266 unsigned long flags;
267 int found = 0;
268
269 spin_lock_irqsave(&device_ids_lock, flags);
270 list_for_each_entry(pdev_id, &pcistub_device_ids, slot_list) {
271 if (pcistub_match_one(dev, pdev_id)) {
272 found = 1;
273 break;
274 }
275 }
276 spin_unlock_irqrestore(&device_ids_lock, flags);
277
278 return found;
279}
280
281static int __devinit pcistub_init_device(struct pci_dev *dev)
282{
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400283 struct xen_pcibk_dev_data *dev_data;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400284 int err = 0;
285
286 dev_dbg(&dev->dev, "initializing...\n");
287
288 /* The PCI backend is not intended to be a module (or to work with
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400289 * removable PCI devices (yet). If it were, xen_pcibk_config_free()
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400290 * would need to be called somewhere to free the memory allocated
291 * here and then to call kfree(pci_get_drvdata(psdev->dev)).
292 */
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -0400293 dev_data = kzalloc(sizeof(*dev_data) + strlen(DRV_NAME "[]")
294 + strlen(pci_name(dev)) + 1, GFP_ATOMIC);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400295 if (!dev_data) {
296 err = -ENOMEM;
297 goto out;
298 }
299 pci_set_drvdata(dev, dev_data);
300
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -0400301 /*
302 * Setup name for fake IRQ handler. It will only be enabled
303 * once the device is turned on by the guest.
304 */
305 sprintf(dev_data->irq_name, DRV_NAME "[%s]", pci_name(dev));
306
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400307 dev_dbg(&dev->dev, "initializing config\n");
308
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400309 init_waitqueue_head(&xen_pcibk_aer_wait_queue);
310 err = xen_pcibk_config_init_dev(dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400311 if (err)
312 goto out;
313
314 /* HACK: Force device (& ACPI) to determine what IRQ it's on - we
315 * must do this here because pcibios_enable_device may specify
316 * the pci device's true irq (and possibly its other resources)
317 * if they differ from what's in the configuration space.
318 * This makes the assumption that the device's resources won't
319 * change after this point (otherwise this code may break!)
320 */
321 dev_dbg(&dev->dev, "enabling device\n");
322 err = pci_enable_device(dev);
323 if (err)
324 goto config_release;
325
326 /* Now disable the device (this also ensures some private device
327 * data is setup before we export)
328 */
329 dev_dbg(&dev->dev, "reset device\n");
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400330 xen_pcibk_reset_device(dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400331
332 return 0;
333
334config_release:
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400335 xen_pcibk_config_free_dev(dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400336
337out:
338 pci_set_drvdata(dev, NULL);
339 kfree(dev_data);
340 return err;
341}
342
343/*
344 * Because some initialization still happens on
345 * devices during fs_initcall, we need to defer
346 * full initialization of our devices until
347 * device_initcall.
348 */
349static int __init pcistub_init_devices_late(void)
350{
351 struct pcistub_device *psdev;
352 unsigned long flags;
353 int err = 0;
354
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400355 pr_debug(DRV_NAME ": pcistub_init_devices_late\n");
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400356
357 spin_lock_irqsave(&pcistub_devices_lock, flags);
358
359 while (!list_empty(&seized_devices)) {
360 psdev = container_of(seized_devices.next,
361 struct pcistub_device, dev_list);
362 list_del(&psdev->dev_list);
363
364 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
365
366 err = pcistub_init_device(psdev->dev);
367 if (err) {
368 dev_err(&psdev->dev->dev,
369 "error %d initializing device\n", err);
370 kfree(psdev);
371 psdev = NULL;
372 }
373
374 spin_lock_irqsave(&pcistub_devices_lock, flags);
375
376 if (psdev)
377 list_add_tail(&psdev->dev_list, &pcistub_devices);
378 }
379
380 initialize_devices = 1;
381
382 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
383
384 return 0;
385}
386
387static int __devinit pcistub_seize(struct pci_dev *dev)
388{
389 struct pcistub_device *psdev;
390 unsigned long flags;
391 int err = 0;
392
393 psdev = pcistub_device_alloc(dev);
394 if (!psdev)
395 return -ENOMEM;
396
397 spin_lock_irqsave(&pcistub_devices_lock, flags);
398
399 if (initialize_devices) {
400 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
401
402 /* don't want irqs disabled when calling pcistub_init_device */
403 err = pcistub_init_device(psdev->dev);
404
405 spin_lock_irqsave(&pcistub_devices_lock, flags);
406
407 if (!err)
408 list_add(&psdev->dev_list, &pcistub_devices);
409 } else {
410 dev_dbg(&dev->dev, "deferring initialization\n");
411 list_add(&psdev->dev_list, &seized_devices);
412 }
413
414 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
415
416 if (err)
417 pcistub_device_put(psdev);
418
419 return err;
420}
421
422static int __devinit pcistub_probe(struct pci_dev *dev,
423 const struct pci_device_id *id)
424{
425 int err = 0;
426
427 dev_dbg(&dev->dev, "probing...\n");
428
429 if (pcistub_match(dev)) {
430
431 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL
432 && dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
433 dev_err(&dev->dev, "can't export pci devices that "
434 "don't have a normal (0) or bridge (1) "
435 "header type!\n");
436 err = -ENODEV;
437 goto out;
438 }
439
440 dev_info(&dev->dev, "seizing device\n");
441 err = pcistub_seize(dev);
442 } else
443 /* Didn't find the device */
444 err = -ENODEV;
445
446out:
447 return err;
448}
449
450static void pcistub_remove(struct pci_dev *dev)
451{
452 struct pcistub_device *psdev, *found_psdev = NULL;
453 unsigned long flags;
454
455 dev_dbg(&dev->dev, "removing\n");
456
457 spin_lock_irqsave(&pcistub_devices_lock, flags);
458
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400459 xen_pcibk_config_quirk_release(dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400460
461 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
462 if (psdev->dev == dev) {
463 found_psdev = psdev;
464 break;
465 }
466 }
467
468 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
469
470 if (found_psdev) {
471 dev_dbg(&dev->dev, "found device to remove - in use? %p\n",
472 found_psdev->pdev);
473
474 if (found_psdev->pdev) {
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400475 printk(KERN_WARNING DRV_NAME ": ****** removing device "
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400476 "%s while still in-use! ******\n",
477 pci_name(found_psdev->dev));
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400478 printk(KERN_WARNING DRV_NAME ": ****** driver domain may"
479 " still access this device's i/o resources!\n");
480 printk(KERN_WARNING DRV_NAME ": ****** shutdown driver "
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400481 "domain before binding device\n");
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400482 printk(KERN_WARNING DRV_NAME ": ****** to other drivers "
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400483 "or domains\n");
484
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400485 xen_pcibk_release_pci_dev(found_psdev->pdev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400486 found_psdev->dev);
487 }
488
489 spin_lock_irqsave(&pcistub_devices_lock, flags);
490 list_del(&found_psdev->dev_list);
491 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
492
493 /* the final put for releasing from the list */
494 pcistub_device_put(found_psdev);
495 }
496}
497
Konrad Rzeszutek Wilk8bfd4e02011-07-19 20:09:43 -0400498static DEFINE_PCI_DEVICE_TABLE(pcistub_ids) = {
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400499 {
500 .vendor = PCI_ANY_ID,
501 .device = PCI_ANY_ID,
502 .subvendor = PCI_ANY_ID,
503 .subdevice = PCI_ANY_ID,
504 },
505 {0,},
506};
507
508#define PCI_NODENAME_MAX 40
509static void kill_domain_by_device(struct pcistub_device *psdev)
510{
511 struct xenbus_transaction xbt;
512 int err;
513 char nodename[PCI_NODENAME_MAX];
514
Konrad Rzeszutek Wilk72bf8092011-09-29 13:43:28 -0400515 BUG_ON(!psdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400516 snprintf(nodename, PCI_NODENAME_MAX, "/local/domain/0/backend/pci/%d/0",
517 psdev->pdev->xdev->otherend_id);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400518
519again:
520 err = xenbus_transaction_start(&xbt);
521 if (err) {
522 dev_err(&psdev->dev->dev,
523 "error %d when start xenbus transaction\n", err);
524 return;
525 }
526 /*PV AER handlers will set this flag*/
527 xenbus_printf(xbt, nodename, "aerState" , "aerfail");
528 err = xenbus_transaction_end(xbt, 0);
529 if (err) {
530 if (err == -EAGAIN)
531 goto again;
532 dev_err(&psdev->dev->dev,
533 "error %d when end xenbus transaction\n", err);
534 return;
535 }
536}
537
538/* For each aer recovery step error_detected, mmio_enabled, etc, front_end and
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400539 * backend need to have cooperation. In xen_pcibk, those steps will do similar
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400540 * jobs: send service request and waiting for front_end response.
541*/
542static pci_ers_result_t common_process(struct pcistub_device *psdev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400543 pci_channel_state_t state, int aer_cmd,
544 pci_ers_result_t result)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400545{
546 pci_ers_result_t res = result;
547 struct xen_pcie_aer_op *aer_op;
548 int ret;
549
550 /*with PV AER drivers*/
551 aer_op = &(psdev->pdev->sh_info->aer_op);
552 aer_op->cmd = aer_cmd ;
553 /*useful for error_detected callback*/
554 aer_op->err = state;
555 /*pcifront_end BDF*/
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400556 ret = xen_pcibk_get_pcifront_dev(psdev->dev, psdev->pdev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400557 &aer_op->domain, &aer_op->bus, &aer_op->devfn);
558 if (!ret) {
559 dev_err(&psdev->dev->dev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400560 DRV_NAME ": failed to get pcifront device\n");
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400561 return PCI_ERS_RESULT_NONE;
562 }
563 wmb();
564
565 dev_dbg(&psdev->dev->dev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400566 DRV_NAME ": aer_op %x dom %x bus %x devfn %x\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400567 aer_cmd, aer_op->domain, aer_op->bus, aer_op->devfn);
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400568 /*local flag to mark there's aer request, xen_pcibk callback will use
569 * this flag to judge whether we need to check pci-front give aer
570 * service ack signal
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400571 */
572 set_bit(_PCIB_op_pending, (unsigned long *)&psdev->pdev->flags);
573
574 /*It is possible that a pcifront conf_read_write ops request invokes
575 * the callback which cause the spurious execution of wake_up.
576 * Yet it is harmless and better than a spinlock here
577 */
578 set_bit(_XEN_PCIB_active,
579 (unsigned long *)&psdev->pdev->sh_info->flags);
580 wmb();
581 notify_remote_via_irq(psdev->pdev->evtchn_irq);
582
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400583 ret = wait_event_timeout(xen_pcibk_aer_wait_queue,
584 !(test_bit(_XEN_PCIB_active, (unsigned long *)
585 &psdev->pdev->sh_info->flags)), 300*HZ);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400586
587 if (!ret) {
588 if (test_bit(_XEN_PCIB_active,
589 (unsigned long *)&psdev->pdev->sh_info->flags)) {
590 dev_err(&psdev->dev->dev,
591 "pcifront aer process not responding!\n");
592 clear_bit(_XEN_PCIB_active,
593 (unsigned long *)&psdev->pdev->sh_info->flags);
594 aer_op->err = PCI_ERS_RESULT_NONE;
595 return res;
596 }
597 }
598 clear_bit(_PCIB_op_pending, (unsigned long *)&psdev->pdev->flags);
599
600 if (test_bit(_XEN_PCIF_active,
601 (unsigned long *)&psdev->pdev->sh_info->flags)) {
602 dev_dbg(&psdev->dev->dev,
Jan Beulich402c5e12011-09-21 16:22:11 -0400603 "schedule pci_conf service in " DRV_NAME "\n");
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400604 xen_pcibk_test_and_schedule_op(psdev->pdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400605 }
606
607 res = (pci_ers_result_t)aer_op->err;
608 return res;
609}
610
611/*
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400612* xen_pcibk_slot_reset: it will send the slot_reset request to pcifront in case
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400613* of the device driver could provide this service, and then wait for pcifront
614* ack.
615* @dev: pointer to PCI devices
616* return value is used by aer_core do_recovery policy
617*/
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400618static pci_ers_result_t xen_pcibk_slot_reset(struct pci_dev *dev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400619{
620 struct pcistub_device *psdev;
621 pci_ers_result_t result;
622
623 result = PCI_ERS_RESULT_RECOVERED;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400624 dev_dbg(&dev->dev, "xen_pcibk_slot_reset(bus:%x,devfn:%x)\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400625 dev->bus->number, dev->devfn);
626
627 down_write(&pcistub_sem);
628 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
629 dev->bus->number,
630 PCI_SLOT(dev->devfn),
631 PCI_FUNC(dev->devfn));
632
633 if (!psdev || !psdev->pdev) {
634 dev_err(&dev->dev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400635 DRV_NAME " device is not found/assigned\n");
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400636 goto end;
637 }
638
639 if (!psdev->pdev->sh_info) {
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400640 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400641 " by HVM, kill it\n");
642 kill_domain_by_device(psdev);
643 goto release;
644 }
645
646 if (!test_bit(_XEN_PCIB_AERHANDLER,
647 (unsigned long *)&psdev->pdev->sh_info->flags)) {
648 dev_err(&dev->dev,
649 "guest with no AER driver should have been killed\n");
650 goto release;
651 }
652 result = common_process(psdev, 1, XEN_PCI_OP_aer_slotreset, result);
653
654 if (result == PCI_ERS_RESULT_NONE ||
655 result == PCI_ERS_RESULT_DISCONNECT) {
656 dev_dbg(&dev->dev,
657 "No AER slot_reset service or disconnected!\n");
658 kill_domain_by_device(psdev);
659 }
660release:
661 pcistub_device_put(psdev);
662end:
663 up_write(&pcistub_sem);
664 return result;
665
666}
667
668
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400669/*xen_pcibk_mmio_enabled: it will send the mmio_enabled request to pcifront
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400670* in case of the device driver could provide this service, and then wait
671* for pcifront ack
672* @dev: pointer to PCI devices
673* return value is used by aer_core do_recovery policy
674*/
675
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400676static pci_ers_result_t xen_pcibk_mmio_enabled(struct pci_dev *dev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400677{
678 struct pcistub_device *psdev;
679 pci_ers_result_t result;
680
681 result = PCI_ERS_RESULT_RECOVERED;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400682 dev_dbg(&dev->dev, "xen_pcibk_mmio_enabled(bus:%x,devfn:%x)\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400683 dev->bus->number, dev->devfn);
684
685 down_write(&pcistub_sem);
686 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
687 dev->bus->number,
688 PCI_SLOT(dev->devfn),
689 PCI_FUNC(dev->devfn));
690
691 if (!psdev || !psdev->pdev) {
692 dev_err(&dev->dev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400693 DRV_NAME " device is not found/assigned\n");
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400694 goto end;
695 }
696
697 if (!psdev->pdev->sh_info) {
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400698 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400699 " by HVM, kill it\n");
700 kill_domain_by_device(psdev);
701 goto release;
702 }
703
704 if (!test_bit(_XEN_PCIB_AERHANDLER,
705 (unsigned long *)&psdev->pdev->sh_info->flags)) {
706 dev_err(&dev->dev,
707 "guest with no AER driver should have been killed\n");
708 goto release;
709 }
710 result = common_process(psdev, 1, XEN_PCI_OP_aer_mmio, result);
711
712 if (result == PCI_ERS_RESULT_NONE ||
713 result == PCI_ERS_RESULT_DISCONNECT) {
714 dev_dbg(&dev->dev,
715 "No AER mmio_enabled service or disconnected!\n");
716 kill_domain_by_device(psdev);
717 }
718release:
719 pcistub_device_put(psdev);
720end:
721 up_write(&pcistub_sem);
722 return result;
723}
724
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400725/*xen_pcibk_error_detected: it will send the error_detected request to pcifront
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400726* in case of the device driver could provide this service, and then wait
727* for pcifront ack.
728* @dev: pointer to PCI devices
729* @error: the current PCI connection state
730* return value is used by aer_core do_recovery policy
731*/
732
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400733static pci_ers_result_t xen_pcibk_error_detected(struct pci_dev *dev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400734 pci_channel_state_t error)
735{
736 struct pcistub_device *psdev;
737 pci_ers_result_t result;
738
739 result = PCI_ERS_RESULT_CAN_RECOVER;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400740 dev_dbg(&dev->dev, "xen_pcibk_error_detected(bus:%x,devfn:%x)\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400741 dev->bus->number, dev->devfn);
742
743 down_write(&pcistub_sem);
744 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
745 dev->bus->number,
746 PCI_SLOT(dev->devfn),
747 PCI_FUNC(dev->devfn));
748
749 if (!psdev || !psdev->pdev) {
750 dev_err(&dev->dev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400751 DRV_NAME " device is not found/assigned\n");
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400752 goto end;
753 }
754
755 if (!psdev->pdev->sh_info) {
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400756 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400757 " by HVM, kill it\n");
758 kill_domain_by_device(psdev);
759 goto release;
760 }
761
762 /*Guest owns the device yet no aer handler regiested, kill guest*/
763 if (!test_bit(_XEN_PCIB_AERHANDLER,
764 (unsigned long *)&psdev->pdev->sh_info->flags)) {
765 dev_dbg(&dev->dev, "guest may have no aer driver, kill it\n");
766 kill_domain_by_device(psdev);
767 goto release;
768 }
769 result = common_process(psdev, error, XEN_PCI_OP_aer_detected, result);
770
771 if (result == PCI_ERS_RESULT_NONE ||
772 result == PCI_ERS_RESULT_DISCONNECT) {
773 dev_dbg(&dev->dev,
774 "No AER error_detected service or disconnected!\n");
775 kill_domain_by_device(psdev);
776 }
777release:
778 pcistub_device_put(psdev);
779end:
780 up_write(&pcistub_sem);
781 return result;
782}
783
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400784/*xen_pcibk_error_resume: it will send the error_resume request to pcifront
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400785* in case of the device driver could provide this service, and then wait
786* for pcifront ack.
787* @dev: pointer to PCI devices
788*/
789
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400790static void xen_pcibk_error_resume(struct pci_dev *dev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400791{
792 struct pcistub_device *psdev;
793
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400794 dev_dbg(&dev->dev, "xen_pcibk_error_resume(bus:%x,devfn:%x)\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400795 dev->bus->number, dev->devfn);
796
797 down_write(&pcistub_sem);
798 psdev = pcistub_device_find(pci_domain_nr(dev->bus),
799 dev->bus->number,
800 PCI_SLOT(dev->devfn),
801 PCI_FUNC(dev->devfn));
802
803 if (!psdev || !psdev->pdev) {
804 dev_err(&dev->dev,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400805 DRV_NAME " device is not found/assigned\n");
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400806 goto end;
807 }
808
809 if (!psdev->pdev->sh_info) {
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400810 dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400811 " by HVM, kill it\n");
812 kill_domain_by_device(psdev);
813 goto release;
814 }
815
816 if (!test_bit(_XEN_PCIB_AERHANDLER,
817 (unsigned long *)&psdev->pdev->sh_info->flags)) {
818 dev_err(&dev->dev,
819 "guest with no AER driver should have been killed\n");
820 kill_domain_by_device(psdev);
821 goto release;
822 }
823 common_process(psdev, 1, XEN_PCI_OP_aer_resume,
824 PCI_ERS_RESULT_RECOVERED);
825release:
826 pcistub_device_put(psdev);
827end:
828 up_write(&pcistub_sem);
829 return;
830}
831
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400832/*add xen_pcibk AER handling*/
833static struct pci_error_handlers xen_pcibk_error_handler = {
834 .error_detected = xen_pcibk_error_detected,
835 .mmio_enabled = xen_pcibk_mmio_enabled,
836 .slot_reset = xen_pcibk_slot_reset,
837 .resume = xen_pcibk_error_resume,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400838};
839
840/*
841 * Note: There is no MODULE_DEVICE_TABLE entry here because this isn't
842 * for a normal device. I don't want it to be loaded automatically.
843 */
844
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400845static struct pci_driver xen_pcibk_pci_driver = {
846 /* The name should be xen_pciback, but until the tools are updated
847 * we will keep it as pciback. */
848 .name = "pciback",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400849 .id_table = pcistub_ids,
850 .probe = pcistub_probe,
851 .remove = pcistub_remove,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400852 .err_handler = &xen_pcibk_error_handler,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400853};
854
855static inline int str_to_slot(const char *buf, int *domain, int *bus,
856 int *slot, int *func)
857{
858 int err;
859
860 err = sscanf(buf, " %x:%x:%x.%x", domain, bus, slot, func);
861 if (err == 4)
862 return 0;
863 else if (err < 0)
864 return -EINVAL;
865
866 /* try again without domain */
867 *domain = 0;
868 err = sscanf(buf, " %x:%x.%x", bus, slot, func);
869 if (err == 3)
870 return 0;
871
872 return -EINVAL;
873}
874
875static inline int str_to_quirk(const char *buf, int *domain, int *bus, int
876 *slot, int *func, int *reg, int *size, int *mask)
877{
878 int err;
879
880 err =
881 sscanf(buf, " %04x:%02x:%02x.%1x-%08x:%1x:%08x", domain, bus, slot,
882 func, reg, size, mask);
883 if (err == 7)
884 return 0;
885 return -EINVAL;
886}
887
888static int pcistub_device_id_add(int domain, int bus, int slot, int func)
889{
890 struct pcistub_device_id *pci_dev_id;
891 unsigned long flags;
892
893 pci_dev_id = kmalloc(sizeof(*pci_dev_id), GFP_KERNEL);
894 if (!pci_dev_id)
895 return -ENOMEM;
896
897 pci_dev_id->domain = domain;
898 pci_dev_id->bus = bus;
899 pci_dev_id->devfn = PCI_DEVFN(slot, func);
900
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400901 pr_debug(DRV_NAME ": wants to seize %04x:%02x:%02x.%01x\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400902 domain, bus, slot, func);
903
904 spin_lock_irqsave(&device_ids_lock, flags);
905 list_add_tail(&pci_dev_id->slot_list, &pcistub_device_ids);
906 spin_unlock_irqrestore(&device_ids_lock, flags);
907
908 return 0;
909}
910
911static int pcistub_device_id_remove(int domain, int bus, int slot, int func)
912{
913 struct pcistub_device_id *pci_dev_id, *t;
914 int devfn = PCI_DEVFN(slot, func);
915 int err = -ENOENT;
916 unsigned long flags;
917
918 spin_lock_irqsave(&device_ids_lock, flags);
919 list_for_each_entry_safe(pci_dev_id, t, &pcistub_device_ids,
920 slot_list) {
921 if (pci_dev_id->domain == domain
922 && pci_dev_id->bus == bus && pci_dev_id->devfn == devfn) {
923 /* Don't break; here because it's possible the same
924 * slot could be in the list more than once
925 */
926 list_del(&pci_dev_id->slot_list);
927 kfree(pci_dev_id);
928
929 err = 0;
930
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400931 pr_debug(DRV_NAME ": removed %04x:%02x:%02x.%01x from "
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400932 "seize list\n", domain, bus, slot, func);
933 }
934 }
935 spin_unlock_irqrestore(&device_ids_lock, flags);
936
937 return err;
938}
939
940static int pcistub_reg_add(int domain, int bus, int slot, int func, int reg,
941 int size, int mask)
942{
943 int err = 0;
944 struct pcistub_device *psdev;
945 struct pci_dev *dev;
946 struct config_field *field;
947
948 psdev = pcistub_device_find(domain, bus, slot, func);
949 if (!psdev || !psdev->dev) {
950 err = -ENODEV;
951 goto out;
952 }
953 dev = psdev->dev;
954
955 field = kzalloc(sizeof(*field), GFP_ATOMIC);
956 if (!field) {
957 err = -ENOMEM;
958 goto out;
959 }
960
961 field->offset = reg;
962 field->size = size;
963 field->mask = mask;
964 field->init = NULL;
965 field->reset = NULL;
966 field->release = NULL;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400967 field->clean = xen_pcibk_config_field_free;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400968
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400969 err = xen_pcibk_config_quirks_add_field(dev, field);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400970 if (err)
971 kfree(field);
972out:
973 return err;
974}
975
976static ssize_t pcistub_slot_add(struct device_driver *drv, const char *buf,
977 size_t count)
978{
979 int domain, bus, slot, func;
980 int err;
981
982 err = str_to_slot(buf, &domain, &bus, &slot, &func);
983 if (err)
984 goto out;
985
986 err = pcistub_device_id_add(domain, bus, slot, func);
987
988out:
989 if (!err)
990 err = count;
991 return err;
992}
Jan Beulich402c5e12011-09-21 16:22:11 -0400993static DRIVER_ATTR(new_slot, S_IWUSR, NULL, pcistub_slot_add);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400994
995static ssize_t pcistub_slot_remove(struct device_driver *drv, const char *buf,
996 size_t count)
997{
998 int domain, bus, slot, func;
999 int err;
1000
1001 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1002 if (err)
1003 goto out;
1004
1005 err = pcistub_device_id_remove(domain, bus, slot, func);
1006
1007out:
1008 if (!err)
1009 err = count;
1010 return err;
1011}
Jan Beulich402c5e12011-09-21 16:22:11 -04001012static DRIVER_ATTR(remove_slot, S_IWUSR, NULL, pcistub_slot_remove);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001013
1014static ssize_t pcistub_slot_show(struct device_driver *drv, char *buf)
1015{
1016 struct pcistub_device_id *pci_dev_id;
1017 size_t count = 0;
1018 unsigned long flags;
1019
1020 spin_lock_irqsave(&device_ids_lock, flags);
1021 list_for_each_entry(pci_dev_id, &pcistub_device_ids, slot_list) {
1022 if (count >= PAGE_SIZE)
1023 break;
1024
1025 count += scnprintf(buf + count, PAGE_SIZE - count,
1026 "%04x:%02x:%02x.%01x\n",
1027 pci_dev_id->domain, pci_dev_id->bus,
1028 PCI_SLOT(pci_dev_id->devfn),
1029 PCI_FUNC(pci_dev_id->devfn));
1030 }
1031 spin_unlock_irqrestore(&device_ids_lock, flags);
1032
1033 return count;
1034}
Jan Beulich402c5e12011-09-21 16:22:11 -04001035static DRIVER_ATTR(slots, S_IRUSR, pcistub_slot_show, NULL);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001036
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001037static ssize_t pcistub_irq_handler_show(struct device_driver *drv, char *buf)
1038{
1039 struct pcistub_device *psdev;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001040 struct xen_pcibk_dev_data *dev_data;
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001041 size_t count = 0;
1042 unsigned long flags;
1043
1044 spin_lock_irqsave(&pcistub_devices_lock, flags);
1045 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1046 if (count >= PAGE_SIZE)
1047 break;
1048 if (!psdev->dev)
1049 continue;
1050 dev_data = pci_get_drvdata(psdev->dev);
1051 if (!dev_data)
1052 continue;
1053 count +=
1054 scnprintf(buf + count, PAGE_SIZE - count,
1055 "%s:%s:%sing:%ld\n",
1056 pci_name(psdev->dev),
1057 dev_data->isr_on ? "on" : "off",
1058 dev_data->ack_intr ? "ack" : "not ack",
1059 dev_data->handled);
1060 }
1061 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1062 return count;
1063}
Jan Beulich402c5e12011-09-21 16:22:11 -04001064static DRIVER_ATTR(irq_handlers, S_IRUSR, pcistub_irq_handler_show, NULL);
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001065
1066static ssize_t pcistub_irq_handler_switch(struct device_driver *drv,
1067 const char *buf,
1068 size_t count)
1069{
1070 struct pcistub_device *psdev;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001071 struct xen_pcibk_dev_data *dev_data;
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001072 int domain, bus, slot, func;
1073 int err = -ENOENT;
1074
1075 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1076 if (err)
1077 goto out;
1078
1079 psdev = pcistub_device_find(domain, bus, slot, func);
1080
1081 if (!psdev)
1082 goto out;
1083
1084 dev_data = pci_get_drvdata(psdev->dev);
1085 if (!dev_data)
1086 goto out;
1087
1088 dev_dbg(&psdev->dev->dev, "%s fake irq handler: %d->%d\n",
1089 dev_data->irq_name, dev_data->isr_on,
1090 !dev_data->isr_on);
1091
1092 dev_data->isr_on = !(dev_data->isr_on);
1093 if (dev_data->isr_on)
1094 dev_data->ack_intr = 1;
1095out:
1096 if (!err)
1097 err = count;
1098 return err;
1099}
Jan Beulich402c5e12011-09-21 16:22:11 -04001100static DRIVER_ATTR(irq_handler_state, S_IWUSR, NULL,
1101 pcistub_irq_handler_switch);
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001102
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001103static ssize_t pcistub_quirk_add(struct device_driver *drv, const char *buf,
1104 size_t count)
1105{
1106 int domain, bus, slot, func, reg, size, mask;
1107 int err;
1108
1109 err = str_to_quirk(buf, &domain, &bus, &slot, &func, &reg, &size,
1110 &mask);
1111 if (err)
1112 goto out;
1113
1114 err = pcistub_reg_add(domain, bus, slot, func, reg, size, mask);
1115
1116out:
1117 if (!err)
1118 err = count;
1119 return err;
1120}
1121
1122static ssize_t pcistub_quirk_show(struct device_driver *drv, char *buf)
1123{
1124 int count = 0;
1125 unsigned long flags;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001126 struct xen_pcibk_config_quirk *quirk;
1127 struct xen_pcibk_dev_data *dev_data;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001128 const struct config_field *field;
1129 const struct config_field_entry *cfg_entry;
1130
1131 spin_lock_irqsave(&device_ids_lock, flags);
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001132 list_for_each_entry(quirk, &xen_pcibk_quirks, quirks_list) {
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001133 if (count >= PAGE_SIZE)
1134 goto out;
1135
1136 count += scnprintf(buf + count, PAGE_SIZE - count,
1137 "%02x:%02x.%01x\n\t%04x:%04x:%04x:%04x\n",
1138 quirk->pdev->bus->number,
1139 PCI_SLOT(quirk->pdev->devfn),
1140 PCI_FUNC(quirk->pdev->devfn),
1141 quirk->devid.vendor, quirk->devid.device,
1142 quirk->devid.subvendor,
1143 quirk->devid.subdevice);
1144
1145 dev_data = pci_get_drvdata(quirk->pdev);
1146
1147 list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
1148 field = cfg_entry->field;
1149 if (count >= PAGE_SIZE)
1150 goto out;
1151
1152 count += scnprintf(buf + count, PAGE_SIZE - count,
1153 "\t\t%08x:%01x:%08x\n",
1154 cfg_entry->base_offset +
1155 field->offset, field->size,
1156 field->mask);
1157 }
1158 }
1159
1160out:
1161 spin_unlock_irqrestore(&device_ids_lock, flags);
1162
1163 return count;
1164}
Jan Beulich402c5e12011-09-21 16:22:11 -04001165static DRIVER_ATTR(quirks, S_IRUSR | S_IWUSR, pcistub_quirk_show,
1166 pcistub_quirk_add);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001167
1168static ssize_t permissive_add(struct device_driver *drv, const char *buf,
1169 size_t count)
1170{
1171 int domain, bus, slot, func;
1172 int err;
1173 struct pcistub_device *psdev;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001174 struct xen_pcibk_dev_data *dev_data;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001175 err = str_to_slot(buf, &domain, &bus, &slot, &func);
1176 if (err)
1177 goto out;
1178 psdev = pcistub_device_find(domain, bus, slot, func);
1179 if (!psdev) {
1180 err = -ENODEV;
1181 goto out;
1182 }
1183 if (!psdev->dev) {
1184 err = -ENODEV;
1185 goto release;
1186 }
1187 dev_data = pci_get_drvdata(psdev->dev);
1188 /* the driver data for a device should never be null at this point */
1189 if (!dev_data) {
1190 err = -ENXIO;
1191 goto release;
1192 }
1193 if (!dev_data->permissive) {
1194 dev_data->permissive = 1;
1195 /* Let user know that what they're doing could be unsafe */
1196 dev_warn(&psdev->dev->dev, "enabling permissive mode "
1197 "configuration space accesses!\n");
1198 dev_warn(&psdev->dev->dev,
1199 "permissive mode is potentially unsafe!\n");
1200 }
1201release:
1202 pcistub_device_put(psdev);
1203out:
1204 if (!err)
1205 err = count;
1206 return err;
1207}
1208
1209static ssize_t permissive_show(struct device_driver *drv, char *buf)
1210{
1211 struct pcistub_device *psdev;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001212 struct xen_pcibk_dev_data *dev_data;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001213 size_t count = 0;
1214 unsigned long flags;
1215 spin_lock_irqsave(&pcistub_devices_lock, flags);
1216 list_for_each_entry(psdev, &pcistub_devices, dev_list) {
1217 if (count >= PAGE_SIZE)
1218 break;
1219 if (!psdev->dev)
1220 continue;
1221 dev_data = pci_get_drvdata(psdev->dev);
1222 if (!dev_data || !dev_data->permissive)
1223 continue;
1224 count +=
1225 scnprintf(buf + count, PAGE_SIZE - count, "%s\n",
1226 pci_name(psdev->dev));
1227 }
1228 spin_unlock_irqrestore(&pcistub_devices_lock, flags);
1229 return count;
1230}
Jan Beulich402c5e12011-09-21 16:22:11 -04001231static DRIVER_ATTR(permissive, S_IRUSR | S_IWUSR, permissive_show,
1232 permissive_add);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001233
1234static void pcistub_exit(void)
1235{
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001236 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_new_slot);
1237 driver_remove_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001238 &driver_attr_remove_slot);
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001239 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_slots);
1240 driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_quirks);
1241 driver_remove_file(&xen_pcibk_pci_driver.driver,
1242 &driver_attr_permissive);
1243 driver_remove_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001244 &driver_attr_irq_handlers);
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001245 driver_remove_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001246 &driver_attr_irq_handler_state);
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001247 pci_unregister_driver(&xen_pcibk_pci_driver);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001248}
1249
1250static int __init pcistub_init(void)
1251{
1252 int pos = 0;
1253 int err = 0;
1254 int domain, bus, slot, func;
1255 int parsed;
1256
1257 if (pci_devs_to_hide && *pci_devs_to_hide) {
1258 do {
1259 parsed = 0;
1260
1261 err = sscanf(pci_devs_to_hide + pos,
1262 " (%x:%x:%x.%x) %n",
1263 &domain, &bus, &slot, &func, &parsed);
1264 if (err != 4) {
1265 domain = 0;
1266 err = sscanf(pci_devs_to_hide + pos,
1267 " (%x:%x.%x) %n",
1268 &bus, &slot, &func, &parsed);
1269 if (err != 3)
1270 goto parse_error;
1271 }
1272
1273 err = pcistub_device_id_add(domain, bus, slot, func);
1274 if (err)
1275 goto out;
1276
1277 /* if parsed<=0, we've reached the end of the string */
1278 pos += parsed;
1279 } while (parsed > 0 && pci_devs_to_hide[pos]);
1280 }
1281
1282 /* If we're the first PCI Device Driver to register, we're the
1283 * first one to get offered PCI devices as they become
1284 * available (and thus we can be the first to grab them)
1285 */
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001286 err = pci_register_driver(&xen_pcibk_pci_driver);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001287 if (err < 0)
1288 goto out;
1289
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001290 err = driver_create_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001291 &driver_attr_new_slot);
1292 if (!err)
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001293 err = driver_create_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001294 &driver_attr_remove_slot);
1295 if (!err)
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001296 err = driver_create_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001297 &driver_attr_slots);
1298 if (!err)
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001299 err = driver_create_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001300 &driver_attr_quirks);
1301 if (!err)
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001302 err = driver_create_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001303 &driver_attr_permissive);
1304
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001305 if (!err)
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001306 err = driver_create_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001307 &driver_attr_irq_handlers);
1308 if (!err)
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001309 err = driver_create_file(&xen_pcibk_pci_driver.driver,
Konrad Rzeszutek Wilk0513fe92011-07-19 18:56:39 -04001310 &driver_attr_irq_handler_state);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001311 if (err)
1312 pcistub_exit();
1313
1314out:
1315 return err;
1316
1317parse_error:
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001318 printk(KERN_ERR DRV_NAME ": Error parsing pci_devs_to_hide at \"%s\"\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001319 pci_devs_to_hide + pos);
1320 return -EINVAL;
1321}
1322
1323#ifndef MODULE
1324/*
1325 * fs_initcall happens before device_initcall
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001326 * so xen_pcibk *should* get called first (b/c we
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001327 * want to suck up any device before other drivers
1328 * get a chance by being the first pci device
1329 * driver to register)
1330 */
1331fs_initcall(pcistub_init);
1332#endif
1333
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001334static int __init xen_pcibk_init(void)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001335{
1336 int err;
1337
1338 if (!xen_initial_domain())
1339 return -ENODEV;
1340
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001341 err = xen_pcibk_config_init();
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001342 if (err)
1343 return err;
1344
1345#ifdef MODULE
1346 err = pcistub_init();
1347 if (err < 0)
1348 return err;
1349#endif
1350
1351 pcistub_init_devices_late();
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001352 err = xen_pcibk_xenbus_register();
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001353 if (err)
1354 pcistub_exit();
1355
1356 return err;
1357}
1358
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001359static void __exit xen_pcibk_cleanup(void)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001360{
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001361 xen_pcibk_xenbus_unregister();
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001362 pcistub_exit();
1363}
1364
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -04001365module_init(xen_pcibk_init);
1366module_exit(xen_pcibk_cleanup);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001367
1368MODULE_LICENSE("Dual BSD/GPL");
Jan Beulich402c5e12011-09-21 16:22:11 -04001369MODULE_ALIAS("xen-backend:pci");