jme: Do not enable NIC WoL functions on S0
[linux/fpc-iii.git] / drivers / pci / xen-pcifront.c
blob626360b07701f542ca25602aa96824c2a8f81c53
1 /*
2 * Xen PCI Frontend.
4 * Author: Ryan Wilson <hap9@epoch.ncsc.mil>
5 */
6 #include <linux/module.h>
7 #include <linux/init.h>
8 #include <linux/mm.h>
9 #include <xen/xenbus.h>
10 #include <xen/events.h>
11 #include <xen/grant_table.h>
12 #include <xen/page.h>
13 #include <linux/spinlock.h>
14 #include <linux/pci.h>
15 #include <linux/msi.h>
16 #include <xen/interface/io/pciif.h>
17 #include <asm/xen/pci.h>
18 #include <linux/interrupt.h>
19 #include <linux/atomic.h>
20 #include <linux/workqueue.h>
21 #include <linux/bitops.h>
22 #include <linux/time.h>
23 #include <xen/platform_pci.h>
25 #include <asm/xen/swiotlb-xen.h>
26 #define INVALID_GRANT_REF (0)
27 #define INVALID_EVTCHN (-1)
29 struct pci_bus_entry {
30 struct list_head list;
31 struct pci_bus *bus;
34 #define _PDEVB_op_active (0)
35 #define PDEVB_op_active (1 << (_PDEVB_op_active))
37 struct pcifront_device {
38 struct xenbus_device *xdev;
39 struct list_head root_buses;
41 int evtchn;
42 int gnt_ref;
44 int irq;
46 /* Lock this when doing any operations in sh_info */
47 spinlock_t sh_info_lock;
48 struct xen_pci_sharedinfo *sh_info;
49 struct work_struct op_work;
50 unsigned long flags;
54 struct pcifront_sd {
55 struct pci_sysdata sd;
56 struct pcifront_device *pdev;
59 static inline struct pcifront_device *
60 pcifront_get_pdev(struct pcifront_sd *sd)
62 return sd->pdev;
65 static inline void pcifront_init_sd(struct pcifront_sd *sd,
66 unsigned int domain, unsigned int bus,
67 struct pcifront_device *pdev)
69 /* Because we do not expose that information via XenBus. */
70 sd->sd.node = first_online_node;
71 sd->sd.domain = domain;
72 sd->pdev = pdev;
75 static DEFINE_SPINLOCK(pcifront_dev_lock);
76 static struct pcifront_device *pcifront_dev;
78 static int verbose_request;
79 module_param(verbose_request, int, 0644);
81 static int errno_to_pcibios_err(int errno)
83 switch (errno) {
84 case XEN_PCI_ERR_success:
85 return PCIBIOS_SUCCESSFUL;
87 case XEN_PCI_ERR_dev_not_found:
88 return PCIBIOS_DEVICE_NOT_FOUND;
90 case XEN_PCI_ERR_invalid_offset:
91 case XEN_PCI_ERR_op_failed:
92 return PCIBIOS_BAD_REGISTER_NUMBER;
94 case XEN_PCI_ERR_not_implemented:
95 return PCIBIOS_FUNC_NOT_SUPPORTED;
97 case XEN_PCI_ERR_access_denied:
98 return PCIBIOS_SET_FAILED;
100 return errno;
103 static inline void schedule_pcifront_aer_op(struct pcifront_device *pdev)
105 if (test_bit(_XEN_PCIB_active, (unsigned long *)&pdev->sh_info->flags)
106 && !test_and_set_bit(_PDEVB_op_active, &pdev->flags)) {
107 dev_dbg(&pdev->xdev->dev, "schedule aer frontend job\n");
108 schedule_work(&pdev->op_work);
112 static int do_pci_op(struct pcifront_device *pdev, struct xen_pci_op *op)
114 int err = 0;
115 struct xen_pci_op *active_op = &pdev->sh_info->op;
116 unsigned long irq_flags;
117 evtchn_port_t port = pdev->evtchn;
118 unsigned irq = pdev->irq;
119 s64 ns, ns_timeout;
120 struct timeval tv;
122 spin_lock_irqsave(&pdev->sh_info_lock, irq_flags);
124 memcpy(active_op, op, sizeof(struct xen_pci_op));
126 /* Go */
127 wmb();
128 set_bit(_XEN_PCIF_active, (unsigned long *)&pdev->sh_info->flags);
129 notify_remote_via_evtchn(port);
132 * We set a poll timeout of 3 seconds but give up on return after
133 * 2 seconds. It is better to time out too late rather than too early
134 * (in the latter case we end up continually re-executing poll() with a
135 * timeout in the past). 1s difference gives plenty of slack for error.
137 do_gettimeofday(&tv);
138 ns_timeout = timeval_to_ns(&tv) + 2 * (s64)NSEC_PER_SEC;
140 xen_clear_irq_pending(irq);
142 while (test_bit(_XEN_PCIF_active,
143 (unsigned long *)&pdev->sh_info->flags)) {
144 xen_poll_irq_timeout(irq, jiffies + 3*HZ);
145 xen_clear_irq_pending(irq);
146 do_gettimeofday(&tv);
147 ns = timeval_to_ns(&tv);
148 if (ns > ns_timeout) {
149 dev_err(&pdev->xdev->dev,
150 "pciback not responding!!!\n");
151 clear_bit(_XEN_PCIF_active,
152 (unsigned long *)&pdev->sh_info->flags);
153 err = XEN_PCI_ERR_dev_not_found;
154 goto out;
159 * We might lose backend service request since we
160 * reuse same evtchn with pci_conf backend response. So re-schedule
161 * aer pcifront service.
163 if (test_bit(_XEN_PCIB_active,
164 (unsigned long *)&pdev->sh_info->flags)) {
165 dev_err(&pdev->xdev->dev,
166 "schedule aer pcifront service\n");
167 schedule_pcifront_aer_op(pdev);
170 memcpy(op, active_op, sizeof(struct xen_pci_op));
172 err = op->err;
173 out:
174 spin_unlock_irqrestore(&pdev->sh_info_lock, irq_flags);
175 return err;
178 /* Access to this function is spinlocked in drivers/pci/access.c */
179 static int pcifront_bus_read(struct pci_bus *bus, unsigned int devfn,
180 int where, int size, u32 *val)
182 int err = 0;
183 struct xen_pci_op op = {
184 .cmd = XEN_PCI_OP_conf_read,
185 .domain = pci_domain_nr(bus),
186 .bus = bus->number,
187 .devfn = devfn,
188 .offset = where,
189 .size = size,
191 struct pcifront_sd *sd = bus->sysdata;
192 struct pcifront_device *pdev = pcifront_get_pdev(sd);
194 if (verbose_request)
195 dev_info(&pdev->xdev->dev,
196 "read dev=%04x:%02x:%02x.%d - offset %x size %d\n",
197 pci_domain_nr(bus), bus->number, PCI_SLOT(devfn),
198 PCI_FUNC(devfn), where, size);
200 err = do_pci_op(pdev, &op);
202 if (likely(!err)) {
203 if (verbose_request)
204 dev_info(&pdev->xdev->dev, "read got back value %x\n",
205 op.value);
207 *val = op.value;
208 } else if (err == -ENODEV) {
209 /* No device here, pretend that it just returned 0 */
210 err = 0;
211 *val = 0;
214 return errno_to_pcibios_err(err);
217 /* Access to this function is spinlocked in drivers/pci/access.c */
218 static int pcifront_bus_write(struct pci_bus *bus, unsigned int devfn,
219 int where, int size, u32 val)
221 struct xen_pci_op op = {
222 .cmd = XEN_PCI_OP_conf_write,
223 .domain = pci_domain_nr(bus),
224 .bus = bus->number,
225 .devfn = devfn,
226 .offset = where,
227 .size = size,
228 .value = val,
230 struct pcifront_sd *sd = bus->sysdata;
231 struct pcifront_device *pdev = pcifront_get_pdev(sd);
233 if (verbose_request)
234 dev_info(&pdev->xdev->dev,
235 "write dev=%04x:%02x:%02x.%d - "
236 "offset %x size %d val %x\n",
237 pci_domain_nr(bus), bus->number,
238 PCI_SLOT(devfn), PCI_FUNC(devfn), where, size, val);
240 return errno_to_pcibios_err(do_pci_op(pdev, &op));
243 static struct pci_ops pcifront_bus_ops = {
244 .read = pcifront_bus_read,
245 .write = pcifront_bus_write,
248 #ifdef CONFIG_PCI_MSI
249 static int pci_frontend_enable_msix(struct pci_dev *dev,
250 int vector[], int nvec)
252 int err;
253 int i;
254 struct xen_pci_op op = {
255 .cmd = XEN_PCI_OP_enable_msix,
256 .domain = pci_domain_nr(dev->bus),
257 .bus = dev->bus->number,
258 .devfn = dev->devfn,
259 .value = nvec,
261 struct pcifront_sd *sd = dev->bus->sysdata;
262 struct pcifront_device *pdev = pcifront_get_pdev(sd);
263 struct msi_desc *entry;
265 if (nvec > SH_INFO_MAX_VEC) {
266 dev_err(&dev->dev, "too much vector for pci frontend: %x."
267 " Increase SH_INFO_MAX_VEC.\n", nvec);
268 return -EINVAL;
271 i = 0;
272 list_for_each_entry(entry, &dev->msi_list, list) {
273 op.msix_entries[i].entry = entry->msi_attrib.entry_nr;
274 /* Vector is useless at this point. */
275 op.msix_entries[i].vector = -1;
276 i++;
279 err = do_pci_op(pdev, &op);
281 if (likely(!err)) {
282 if (likely(!op.value)) {
283 /* we get the result */
284 for (i = 0; i < nvec; i++) {
285 if (op.msix_entries[i].vector <= 0) {
286 dev_warn(&dev->dev, "MSI-X entry %d is invalid: %d!\n",
287 i, op.msix_entries[i].vector);
288 err = -EINVAL;
289 vector[i] = -1;
290 continue;
292 vector[i] = op.msix_entries[i].vector;
294 } else {
295 printk(KERN_DEBUG "enable msix get value %x\n",
296 op.value);
297 err = op.value;
299 } else {
300 dev_err(&dev->dev, "enable msix get err %x\n", err);
302 return err;
305 static void pci_frontend_disable_msix(struct pci_dev *dev)
307 int err;
308 struct xen_pci_op op = {
309 .cmd = XEN_PCI_OP_disable_msix,
310 .domain = pci_domain_nr(dev->bus),
311 .bus = dev->bus->number,
312 .devfn = dev->devfn,
314 struct pcifront_sd *sd = dev->bus->sysdata;
315 struct pcifront_device *pdev = pcifront_get_pdev(sd);
317 err = do_pci_op(pdev, &op);
319 /* What should do for error ? */
320 if (err)
321 dev_err(&dev->dev, "pci_disable_msix get err %x\n", err);
324 static int pci_frontend_enable_msi(struct pci_dev *dev, int vector[])
326 int err;
327 struct xen_pci_op op = {
328 .cmd = XEN_PCI_OP_enable_msi,
329 .domain = pci_domain_nr(dev->bus),
330 .bus = dev->bus->number,
331 .devfn = dev->devfn,
333 struct pcifront_sd *sd = dev->bus->sysdata;
334 struct pcifront_device *pdev = pcifront_get_pdev(sd);
336 err = do_pci_op(pdev, &op);
337 if (likely(!err)) {
338 vector[0] = op.value;
339 if (op.value <= 0) {
340 dev_warn(&dev->dev, "MSI entry is invalid: %d!\n",
341 op.value);
342 err = -EINVAL;
343 vector[0] = -1;
345 } else {
346 dev_err(&dev->dev, "pci frontend enable msi failed for dev "
347 "%x:%x\n", op.bus, op.devfn);
348 err = -EINVAL;
350 return err;
353 static void pci_frontend_disable_msi(struct pci_dev *dev)
355 int err;
356 struct xen_pci_op op = {
357 .cmd = XEN_PCI_OP_disable_msi,
358 .domain = pci_domain_nr(dev->bus),
359 .bus = dev->bus->number,
360 .devfn = dev->devfn,
362 struct pcifront_sd *sd = dev->bus->sysdata;
363 struct pcifront_device *pdev = pcifront_get_pdev(sd);
365 err = do_pci_op(pdev, &op);
366 if (err == XEN_PCI_ERR_dev_not_found) {
367 /* XXX No response from backend, what shall we do? */
368 printk(KERN_DEBUG "get no response from backend for disable MSI\n");
369 return;
371 if (err)
372 /* how can pciback notify us fail? */
373 printk(KERN_DEBUG "get fake response frombackend\n");
376 static struct xen_pci_frontend_ops pci_frontend_ops = {
377 .enable_msi = pci_frontend_enable_msi,
378 .disable_msi = pci_frontend_disable_msi,
379 .enable_msix = pci_frontend_enable_msix,
380 .disable_msix = pci_frontend_disable_msix,
383 static void pci_frontend_registrar(int enable)
385 if (enable)
386 xen_pci_frontend = &pci_frontend_ops;
387 else
388 xen_pci_frontend = NULL;
390 #else
391 static inline void pci_frontend_registrar(int enable) { };
392 #endif /* CONFIG_PCI_MSI */
394 /* Claim resources for the PCI frontend as-is, backend won't allow changes */
395 static int pcifront_claim_resource(struct pci_dev *dev, void *data)
397 struct pcifront_device *pdev = data;
398 int i;
399 struct resource *r;
401 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
402 r = &dev->resource[i];
404 if (!r->parent && r->start && r->flags) {
405 dev_info(&pdev->xdev->dev, "claiming resource %s/%d\n",
406 pci_name(dev), i);
407 if (pci_claim_resource(dev, i)) {
408 dev_err(&pdev->xdev->dev, "Could not claim resource %s/%d! "
409 "Device offline. Try using e820_host=1 in the guest config.\n",
410 pci_name(dev), i);
415 return 0;
418 static int pcifront_scan_bus(struct pcifront_device *pdev,
419 unsigned int domain, unsigned int bus,
420 struct pci_bus *b)
422 struct pci_dev *d;
423 unsigned int devfn;
425 /* Scan the bus for functions and add.
426 * We omit handling of PCI bridge attachment because pciback prevents
427 * bridges from being exported.
429 for (devfn = 0; devfn < 0x100; devfn++) {
430 d = pci_get_slot(b, devfn);
431 if (d) {
432 /* Device is already known. */
433 pci_dev_put(d);
434 continue;
437 d = pci_scan_single_device(b, devfn);
438 if (d)
439 dev_info(&pdev->xdev->dev, "New device on "
440 "%04x:%02x:%02x.%d found.\n", domain, bus,
441 PCI_SLOT(devfn), PCI_FUNC(devfn));
444 return 0;
447 static int pcifront_scan_root(struct pcifront_device *pdev,
448 unsigned int domain, unsigned int bus)
450 struct pci_bus *b;
451 struct pcifront_sd *sd = NULL;
452 struct pci_bus_entry *bus_entry = NULL;
453 int err = 0;
455 #ifndef CONFIG_PCI_DOMAINS
456 if (domain != 0) {
457 dev_err(&pdev->xdev->dev,
458 "PCI Root in non-zero PCI Domain! domain=%d\n", domain);
459 dev_err(&pdev->xdev->dev,
460 "Please compile with CONFIG_PCI_DOMAINS\n");
461 err = -EINVAL;
462 goto err_out;
464 #endif
466 dev_info(&pdev->xdev->dev, "Creating PCI Frontend Bus %04x:%02x\n",
467 domain, bus);
469 bus_entry = kzalloc(sizeof(*bus_entry), GFP_KERNEL);
470 sd = kzalloc(sizeof(*sd), GFP_KERNEL);
471 if (!bus_entry || !sd) {
472 err = -ENOMEM;
473 goto err_out;
475 pcifront_init_sd(sd, domain, bus, pdev);
477 pci_lock_rescan_remove();
479 b = pci_scan_bus_parented(&pdev->xdev->dev, bus,
480 &pcifront_bus_ops, sd);
481 if (!b) {
482 dev_err(&pdev->xdev->dev,
483 "Error creating PCI Frontend Bus!\n");
484 err = -ENOMEM;
485 pci_unlock_rescan_remove();
486 goto err_out;
489 bus_entry->bus = b;
491 list_add(&bus_entry->list, &pdev->root_buses);
493 /* pci_scan_bus_parented skips devices which do not have a have
494 * devfn==0. The pcifront_scan_bus enumerates all devfn. */
495 err = pcifront_scan_bus(pdev, domain, bus, b);
497 /* Claim resources before going "live" with our devices */
498 pci_walk_bus(b, pcifront_claim_resource, pdev);
500 /* Create SysFS and notify udev of the devices. Aka: "going live" */
501 pci_bus_add_devices(b);
503 pci_unlock_rescan_remove();
504 return err;
506 err_out:
507 kfree(bus_entry);
508 kfree(sd);
510 return err;
513 static int pcifront_rescan_root(struct pcifront_device *pdev,
514 unsigned int domain, unsigned int bus)
516 int err;
517 struct pci_bus *b;
519 #ifndef CONFIG_PCI_DOMAINS
520 if (domain != 0) {
521 dev_err(&pdev->xdev->dev,
522 "PCI Root in non-zero PCI Domain! domain=%d\n", domain);
523 dev_err(&pdev->xdev->dev,
524 "Please compile with CONFIG_PCI_DOMAINS\n");
525 return -EINVAL;
527 #endif
529 dev_info(&pdev->xdev->dev, "Rescanning PCI Frontend Bus %04x:%02x\n",
530 domain, bus);
532 b = pci_find_bus(domain, bus);
533 if (!b)
534 /* If the bus is unknown, create it. */
535 return pcifront_scan_root(pdev, domain, bus);
537 err = pcifront_scan_bus(pdev, domain, bus, b);
539 /* Claim resources before going "live" with our devices */
540 pci_walk_bus(b, pcifront_claim_resource, pdev);
542 /* Create SysFS and notify udev of the devices. Aka: "going live" */
543 pci_bus_add_devices(b);
545 return err;
548 static void free_root_bus_devs(struct pci_bus *bus)
550 struct pci_dev *dev;
552 while (!list_empty(&bus->devices)) {
553 dev = container_of(bus->devices.next, struct pci_dev,
554 bus_list);
555 dev_dbg(&dev->dev, "removing device\n");
556 pci_stop_and_remove_bus_device(dev);
560 static void pcifront_free_roots(struct pcifront_device *pdev)
562 struct pci_bus_entry *bus_entry, *t;
564 dev_dbg(&pdev->xdev->dev, "cleaning up root buses\n");
566 pci_lock_rescan_remove();
567 list_for_each_entry_safe(bus_entry, t, &pdev->root_buses, list) {
568 list_del(&bus_entry->list);
570 free_root_bus_devs(bus_entry->bus);
572 kfree(bus_entry->bus->sysdata);
574 device_unregister(bus_entry->bus->bridge);
575 pci_remove_bus(bus_entry->bus);
577 kfree(bus_entry);
579 pci_unlock_rescan_remove();
582 static pci_ers_result_t pcifront_common_process(int cmd,
583 struct pcifront_device *pdev,
584 pci_channel_state_t state)
586 pci_ers_result_t result;
587 struct pci_driver *pdrv;
588 int bus = pdev->sh_info->aer_op.bus;
589 int devfn = pdev->sh_info->aer_op.devfn;
590 struct pci_dev *pcidev;
591 int flag = 0;
593 dev_dbg(&pdev->xdev->dev,
594 "pcifront AER process: cmd %x (bus:%x, devfn%x)",
595 cmd, bus, devfn);
596 result = PCI_ERS_RESULT_NONE;
598 pcidev = pci_get_bus_and_slot(bus, devfn);
599 if (!pcidev || !pcidev->driver) {
600 dev_err(&pdev->xdev->dev, "device or AER driver is NULL\n");
601 if (pcidev)
602 pci_dev_put(pcidev);
603 return result;
605 pdrv = pcidev->driver;
607 if (pdrv) {
608 if (pdrv->err_handler && pdrv->err_handler->error_detected) {
609 dev_dbg(&pcidev->dev,
610 "trying to call AER service\n");
611 if (pcidev) {
612 flag = 1;
613 switch (cmd) {
614 case XEN_PCI_OP_aer_detected:
615 result = pdrv->err_handler->
616 error_detected(pcidev, state);
617 break;
618 case XEN_PCI_OP_aer_mmio:
619 result = pdrv->err_handler->
620 mmio_enabled(pcidev);
621 break;
622 case XEN_PCI_OP_aer_slotreset:
623 result = pdrv->err_handler->
624 slot_reset(pcidev);
625 break;
626 case XEN_PCI_OP_aer_resume:
627 pdrv->err_handler->resume(pcidev);
628 break;
629 default:
630 dev_err(&pdev->xdev->dev,
631 "bad request in aer recovery "
632 "operation!\n");
638 if (!flag)
639 result = PCI_ERS_RESULT_NONE;
641 return result;
645 static void pcifront_do_aer(struct work_struct *data)
647 struct pcifront_device *pdev =
648 container_of(data, struct pcifront_device, op_work);
649 int cmd = pdev->sh_info->aer_op.cmd;
650 pci_channel_state_t state =
651 (pci_channel_state_t)pdev->sh_info->aer_op.err;
653 /*If a pci_conf op is in progress,
654 we have to wait until it is done before service aer op*/
655 dev_dbg(&pdev->xdev->dev,
656 "pcifront service aer bus %x devfn %x\n",
657 pdev->sh_info->aer_op.bus, pdev->sh_info->aer_op.devfn);
659 pdev->sh_info->aer_op.err = pcifront_common_process(cmd, pdev, state);
661 /* Post the operation to the guest. */
662 wmb();
663 clear_bit(_XEN_PCIB_active, (unsigned long *)&pdev->sh_info->flags);
664 notify_remote_via_evtchn(pdev->evtchn);
666 /*in case of we lost an aer request in four lines time_window*/
667 smp_mb__before_atomic();
668 clear_bit(_PDEVB_op_active, &pdev->flags);
669 smp_mb__after_atomic();
671 schedule_pcifront_aer_op(pdev);
675 static irqreturn_t pcifront_handler_aer(int irq, void *dev)
677 struct pcifront_device *pdev = dev;
678 schedule_pcifront_aer_op(pdev);
679 return IRQ_HANDLED;
681 static int pcifront_connect_and_init_dma(struct pcifront_device *pdev)
683 int err = 0;
685 spin_lock(&pcifront_dev_lock);
687 if (!pcifront_dev) {
688 dev_info(&pdev->xdev->dev, "Installing PCI frontend\n");
689 pcifront_dev = pdev;
690 } else
691 err = -EEXIST;
693 spin_unlock(&pcifront_dev_lock);
695 if (!err && !swiotlb_nr_tbl()) {
696 err = pci_xen_swiotlb_init_late();
697 if (err)
698 dev_err(&pdev->xdev->dev, "Could not setup SWIOTLB!\n");
700 return err;
703 static void pcifront_disconnect(struct pcifront_device *pdev)
705 spin_lock(&pcifront_dev_lock);
707 if (pdev == pcifront_dev) {
708 dev_info(&pdev->xdev->dev,
709 "Disconnecting PCI Frontend Buses\n");
710 pcifront_dev = NULL;
713 spin_unlock(&pcifront_dev_lock);
715 static struct pcifront_device *alloc_pdev(struct xenbus_device *xdev)
717 struct pcifront_device *pdev;
719 pdev = kzalloc(sizeof(struct pcifront_device), GFP_KERNEL);
720 if (pdev == NULL)
721 goto out;
723 pdev->sh_info =
724 (struct xen_pci_sharedinfo *)__get_free_page(GFP_KERNEL);
725 if (pdev->sh_info == NULL) {
726 kfree(pdev);
727 pdev = NULL;
728 goto out;
730 pdev->sh_info->flags = 0;
732 /*Flag for registering PV AER handler*/
733 set_bit(_XEN_PCIB_AERHANDLER, (void *)&pdev->sh_info->flags);
735 dev_set_drvdata(&xdev->dev, pdev);
736 pdev->xdev = xdev;
738 INIT_LIST_HEAD(&pdev->root_buses);
740 spin_lock_init(&pdev->sh_info_lock);
742 pdev->evtchn = INVALID_EVTCHN;
743 pdev->gnt_ref = INVALID_GRANT_REF;
744 pdev->irq = -1;
746 INIT_WORK(&pdev->op_work, pcifront_do_aer);
748 dev_dbg(&xdev->dev, "Allocated pdev @ 0x%p pdev->sh_info @ 0x%p\n",
749 pdev, pdev->sh_info);
750 out:
751 return pdev;
754 static void free_pdev(struct pcifront_device *pdev)
756 dev_dbg(&pdev->xdev->dev, "freeing pdev @ 0x%p\n", pdev);
758 pcifront_free_roots(pdev);
760 cancel_work_sync(&pdev->op_work);
762 if (pdev->irq >= 0)
763 unbind_from_irqhandler(pdev->irq, pdev);
765 if (pdev->evtchn != INVALID_EVTCHN)
766 xenbus_free_evtchn(pdev->xdev, pdev->evtchn);
768 if (pdev->gnt_ref != INVALID_GRANT_REF)
769 gnttab_end_foreign_access(pdev->gnt_ref, 0 /* r/w page */,
770 (unsigned long)pdev->sh_info);
771 else
772 free_page((unsigned long)pdev->sh_info);
774 dev_set_drvdata(&pdev->xdev->dev, NULL);
776 kfree(pdev);
779 static int pcifront_publish_info(struct pcifront_device *pdev)
781 int err = 0;
782 struct xenbus_transaction trans;
784 err = xenbus_grant_ring(pdev->xdev, virt_to_mfn(pdev->sh_info));
785 if (err < 0)
786 goto out;
788 pdev->gnt_ref = err;
790 err = xenbus_alloc_evtchn(pdev->xdev, &pdev->evtchn);
791 if (err)
792 goto out;
794 err = bind_evtchn_to_irqhandler(pdev->evtchn, pcifront_handler_aer,
795 0, "pcifront", pdev);
797 if (err < 0)
798 return err;
800 pdev->irq = err;
802 do_publish:
803 err = xenbus_transaction_start(&trans);
804 if (err) {
805 xenbus_dev_fatal(pdev->xdev, err,
806 "Error writing configuration for backend "
807 "(start transaction)");
808 goto out;
811 err = xenbus_printf(trans, pdev->xdev->nodename,
812 "pci-op-ref", "%u", pdev->gnt_ref);
813 if (!err)
814 err = xenbus_printf(trans, pdev->xdev->nodename,
815 "event-channel", "%u", pdev->evtchn);
816 if (!err)
817 err = xenbus_printf(trans, pdev->xdev->nodename,
818 "magic", XEN_PCI_MAGIC);
820 if (err) {
821 xenbus_transaction_end(trans, 1);
822 xenbus_dev_fatal(pdev->xdev, err,
823 "Error writing configuration for backend");
824 goto out;
825 } else {
826 err = xenbus_transaction_end(trans, 0);
827 if (err == -EAGAIN)
828 goto do_publish;
829 else if (err) {
830 xenbus_dev_fatal(pdev->xdev, err,
831 "Error completing transaction "
832 "for backend");
833 goto out;
837 xenbus_switch_state(pdev->xdev, XenbusStateInitialised);
839 dev_dbg(&pdev->xdev->dev, "publishing successful!\n");
841 out:
842 return err;
845 static int pcifront_try_connect(struct pcifront_device *pdev)
847 int err = -EFAULT;
848 int i, num_roots, len;
849 char str[64];
850 unsigned int domain, bus;
853 /* Only connect once */
854 if (xenbus_read_driver_state(pdev->xdev->nodename) !=
855 XenbusStateInitialised)
856 goto out;
858 err = pcifront_connect_and_init_dma(pdev);
859 if (err && err != -EEXIST) {
860 xenbus_dev_fatal(pdev->xdev, err,
861 "Error setting up PCI Frontend");
862 goto out;
865 err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend,
866 "root_num", "%d", &num_roots);
867 if (err == -ENOENT) {
868 xenbus_dev_error(pdev->xdev, err,
869 "No PCI Roots found, trying 0000:00");
870 err = pcifront_scan_root(pdev, 0, 0);
871 num_roots = 0;
872 } else if (err != 1) {
873 if (err == 0)
874 err = -EINVAL;
875 xenbus_dev_fatal(pdev->xdev, err,
876 "Error reading number of PCI roots");
877 goto out;
880 for (i = 0; i < num_roots; i++) {
881 len = snprintf(str, sizeof(str), "root-%d", i);
882 if (unlikely(len >= (sizeof(str) - 1))) {
883 err = -ENOMEM;
884 goto out;
887 err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend, str,
888 "%x:%x", &domain, &bus);
889 if (err != 2) {
890 if (err >= 0)
891 err = -EINVAL;
892 xenbus_dev_fatal(pdev->xdev, err,
893 "Error reading PCI root %d", i);
894 goto out;
897 err = pcifront_scan_root(pdev, domain, bus);
898 if (err) {
899 xenbus_dev_fatal(pdev->xdev, err,
900 "Error scanning PCI root %04x:%02x",
901 domain, bus);
902 goto out;
906 err = xenbus_switch_state(pdev->xdev, XenbusStateConnected);
908 out:
909 return err;
912 static int pcifront_try_disconnect(struct pcifront_device *pdev)
914 int err = 0;
915 enum xenbus_state prev_state;
918 prev_state = xenbus_read_driver_state(pdev->xdev->nodename);
920 if (prev_state >= XenbusStateClosing)
921 goto out;
923 if (prev_state == XenbusStateConnected) {
924 pcifront_free_roots(pdev);
925 pcifront_disconnect(pdev);
928 err = xenbus_switch_state(pdev->xdev, XenbusStateClosed);
930 out:
932 return err;
935 static int pcifront_attach_devices(struct pcifront_device *pdev)
937 int err = -EFAULT;
938 int i, num_roots, len;
939 unsigned int domain, bus;
940 char str[64];
942 if (xenbus_read_driver_state(pdev->xdev->nodename) !=
943 XenbusStateReconfiguring)
944 goto out;
946 err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend,
947 "root_num", "%d", &num_roots);
948 if (err == -ENOENT) {
949 xenbus_dev_error(pdev->xdev, err,
950 "No PCI Roots found, trying 0000:00");
951 err = pcifront_rescan_root(pdev, 0, 0);
952 num_roots = 0;
953 } else if (err != 1) {
954 if (err == 0)
955 err = -EINVAL;
956 xenbus_dev_fatal(pdev->xdev, err,
957 "Error reading number of PCI roots");
958 goto out;
961 for (i = 0; i < num_roots; i++) {
962 len = snprintf(str, sizeof(str), "root-%d", i);
963 if (unlikely(len >= (sizeof(str) - 1))) {
964 err = -ENOMEM;
965 goto out;
968 err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend, str,
969 "%x:%x", &domain, &bus);
970 if (err != 2) {
971 if (err >= 0)
972 err = -EINVAL;
973 xenbus_dev_fatal(pdev->xdev, err,
974 "Error reading PCI root %d", i);
975 goto out;
978 err = pcifront_rescan_root(pdev, domain, bus);
979 if (err) {
980 xenbus_dev_fatal(pdev->xdev, err,
981 "Error scanning PCI root %04x:%02x",
982 domain, bus);
983 goto out;
987 xenbus_switch_state(pdev->xdev, XenbusStateConnected);
989 out:
990 return err;
993 static int pcifront_detach_devices(struct pcifront_device *pdev)
995 int err = 0;
996 int i, num_devs;
997 unsigned int domain, bus, slot, func;
998 struct pci_dev *pci_dev;
999 char str[64];
1001 if (xenbus_read_driver_state(pdev->xdev->nodename) !=
1002 XenbusStateConnected)
1003 goto out;
1005 err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend, "num_devs", "%d",
1006 &num_devs);
1007 if (err != 1) {
1008 if (err >= 0)
1009 err = -EINVAL;
1010 xenbus_dev_fatal(pdev->xdev, err,
1011 "Error reading number of PCI devices");
1012 goto out;
1015 /* Find devices being detached and remove them. */
1016 for (i = 0; i < num_devs; i++) {
1017 int l, state;
1018 l = snprintf(str, sizeof(str), "state-%d", i);
1019 if (unlikely(l >= (sizeof(str) - 1))) {
1020 err = -ENOMEM;
1021 goto out;
1023 err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend, str, "%d",
1024 &state);
1025 if (err != 1)
1026 state = XenbusStateUnknown;
1028 if (state != XenbusStateClosing)
1029 continue;
1031 /* Remove device. */
1032 l = snprintf(str, sizeof(str), "vdev-%d", i);
1033 if (unlikely(l >= (sizeof(str) - 1))) {
1034 err = -ENOMEM;
1035 goto out;
1037 err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend, str,
1038 "%x:%x:%x.%x", &domain, &bus, &slot, &func);
1039 if (err != 4) {
1040 if (err >= 0)
1041 err = -EINVAL;
1042 xenbus_dev_fatal(pdev->xdev, err,
1043 "Error reading PCI device %d", i);
1044 goto out;
1047 pci_dev = pci_get_domain_bus_and_slot(domain, bus,
1048 PCI_DEVFN(slot, func));
1049 if (!pci_dev) {
1050 dev_dbg(&pdev->xdev->dev,
1051 "Cannot get PCI device %04x:%02x:%02x.%d\n",
1052 domain, bus, slot, func);
1053 continue;
1055 pci_lock_rescan_remove();
1056 pci_stop_and_remove_bus_device(pci_dev);
1057 pci_dev_put(pci_dev);
1058 pci_unlock_rescan_remove();
1060 dev_dbg(&pdev->xdev->dev,
1061 "PCI device %04x:%02x:%02x.%d removed.\n",
1062 domain, bus, slot, func);
1065 err = xenbus_switch_state(pdev->xdev, XenbusStateReconfiguring);
1067 out:
1068 return err;
1071 static void __init_refok pcifront_backend_changed(struct xenbus_device *xdev,
1072 enum xenbus_state be_state)
1074 struct pcifront_device *pdev = dev_get_drvdata(&xdev->dev);
1076 switch (be_state) {
1077 case XenbusStateUnknown:
1078 case XenbusStateInitialising:
1079 case XenbusStateInitWait:
1080 case XenbusStateInitialised:
1081 break;
1083 case XenbusStateConnected:
1084 pcifront_try_connect(pdev);
1085 break;
1087 case XenbusStateClosed:
1088 if (xdev->state == XenbusStateClosed)
1089 break;
1090 /* Missed the backend's CLOSING state -- fallthrough */
1091 case XenbusStateClosing:
1092 dev_warn(&xdev->dev, "backend going away!\n");
1093 pcifront_try_disconnect(pdev);
1094 break;
1096 case XenbusStateReconfiguring:
1097 pcifront_detach_devices(pdev);
1098 break;
1100 case XenbusStateReconfigured:
1101 pcifront_attach_devices(pdev);
1102 break;
1106 static int pcifront_xenbus_probe(struct xenbus_device *xdev,
1107 const struct xenbus_device_id *id)
1109 int err = 0;
1110 struct pcifront_device *pdev = alloc_pdev(xdev);
1112 if (pdev == NULL) {
1113 err = -ENOMEM;
1114 xenbus_dev_fatal(xdev, err,
1115 "Error allocating pcifront_device struct");
1116 goto out;
1119 err = pcifront_publish_info(pdev);
1120 if (err)
1121 free_pdev(pdev);
1123 out:
1124 return err;
1127 static int pcifront_xenbus_remove(struct xenbus_device *xdev)
1129 struct pcifront_device *pdev = dev_get_drvdata(&xdev->dev);
1130 if (pdev)
1131 free_pdev(pdev);
1133 return 0;
1136 static const struct xenbus_device_id xenpci_ids[] = {
1137 {"pci"},
1138 {""},
1141 static DEFINE_XENBUS_DRIVER(xenpci, "pcifront",
1142 .probe = pcifront_xenbus_probe,
1143 .remove = pcifront_xenbus_remove,
1144 .otherend_changed = pcifront_backend_changed,
1147 static int __init pcifront_init(void)
1149 if (!xen_pv_domain() || xen_initial_domain())
1150 return -ENODEV;
1152 if (!xen_has_pv_devices())
1153 return -ENODEV;
1155 pci_frontend_registrar(1 /* enable */);
1157 return xenbus_register_frontend(&xenpci_driver);
1160 static void __exit pcifront_cleanup(void)
1162 xenbus_unregister_driver(&xenpci_driver);
1163 pci_frontend_registrar(0 /* disable */);
1165 module_init(pcifront_init);
1166 module_exit(pcifront_cleanup);
1168 MODULE_DESCRIPTION("Xen PCI passthrough frontend.");
1169 MODULE_LICENSE("GPL");
1170 MODULE_ALIAS("xen:pci");