4 * Author: Ryan Wilson <hap9@epoch.ncsc.mil>
6 #include <linux/module.h>
7 #include <linux/init.h>
9 #include <xen/xenbus.h>
10 #include <xen/events.h>
11 #include <xen/grant_table.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 <linux/ktime.h>
24 #include <xen/platform_pci.h>
26 #include <asm/xen/swiotlb-xen.h>
27 #define INVALID_GRANT_REF (0)
28 #define INVALID_EVTCHN (-1)
30 struct pci_bus_entry {
31 struct list_head list;
35 #define _PDEVB_op_active (0)
36 #define PDEVB_op_active (1 << (_PDEVB_op_active))
38 struct pcifront_device {
39 struct xenbus_device *xdev;
40 struct list_head root_buses;
47 /* Lock this when doing any operations in sh_info */
48 spinlock_t sh_info_lock;
49 struct xen_pci_sharedinfo *sh_info;
50 struct work_struct op_work;
57 struct pcifront_device *pdev;
60 static inline struct pcifront_device *
61 pcifront_get_pdev(struct pcifront_sd *sd)
66 static inline void pcifront_init_sd(struct pcifront_sd *sd,
67 unsigned int domain, unsigned int bus,
68 struct pcifront_device *pdev)
74 static DEFINE_SPINLOCK(pcifront_dev_lock);
75 static struct pcifront_device *pcifront_dev;
77 static int verbose_request;
78 module_param(verbose_request, int, 0644);
80 static int errno_to_pcibios_err(int errno)
83 case XEN_PCI_ERR_success:
84 return PCIBIOS_SUCCESSFUL;
86 case XEN_PCI_ERR_dev_not_found:
87 return PCIBIOS_DEVICE_NOT_FOUND;
89 case XEN_PCI_ERR_invalid_offset:
90 case XEN_PCI_ERR_op_failed:
91 return PCIBIOS_BAD_REGISTER_NUMBER;
93 case XEN_PCI_ERR_not_implemented:
94 return PCIBIOS_FUNC_NOT_SUPPORTED;
96 case XEN_PCI_ERR_access_denied:
97 return PCIBIOS_SET_FAILED;
102 static inline void schedule_pcifront_aer_op(struct pcifront_device *pdev)
104 if (test_bit(_XEN_PCIB_active, (unsigned long *)&pdev->sh_info->flags)
105 && !test_and_set_bit(_PDEVB_op_active, &pdev->flags)) {
106 dev_dbg(&pdev->xdev->dev, "schedule aer frontend job\n");
107 schedule_work(&pdev->op_work);
111 static int do_pci_op(struct pcifront_device *pdev, struct xen_pci_op *op)
114 struct xen_pci_op *active_op = &pdev->sh_info->op;
115 unsigned long irq_flags;
116 evtchn_port_t port = pdev->evtchn;
117 unsigned irq = pdev->irq;
120 spin_lock_irqsave(&pdev->sh_info_lock, irq_flags);
122 memcpy(active_op, op, sizeof(struct xen_pci_op));
126 set_bit(_XEN_PCIF_active, (unsigned long *)&pdev->sh_info->flags);
127 notify_remote_via_evtchn(port);
130 * We set a poll timeout of 3 seconds but give up on return after
131 * 2 seconds. It is better to time out too late rather than too early
132 * (in the latter case we end up continually re-executing poll() with a
133 * timeout in the past). 1s difference gives plenty of slack for error.
135 ns_timeout = ktime_get_ns() + 2 * (s64)NSEC_PER_SEC;
137 xen_clear_irq_pending(irq);
139 while (test_bit(_XEN_PCIF_active,
140 (unsigned long *)&pdev->sh_info->flags)) {
141 xen_poll_irq_timeout(irq, jiffies + 3*HZ);
142 xen_clear_irq_pending(irq);
144 if (ns > ns_timeout) {
145 dev_err(&pdev->xdev->dev,
146 "pciback not responding!!!\n");
147 clear_bit(_XEN_PCIF_active,
148 (unsigned long *)&pdev->sh_info->flags);
149 err = XEN_PCI_ERR_dev_not_found;
155 * We might lose backend service request since we
156 * reuse same evtchn with pci_conf backend response. So re-schedule
157 * aer pcifront service.
159 if (test_bit(_XEN_PCIB_active,
160 (unsigned long *)&pdev->sh_info->flags)) {
161 dev_err(&pdev->xdev->dev,
162 "schedule aer pcifront service\n");
163 schedule_pcifront_aer_op(pdev);
166 memcpy(op, active_op, sizeof(struct xen_pci_op));
170 spin_unlock_irqrestore(&pdev->sh_info_lock, irq_flags);
174 /* Access to this function is spinlocked in drivers/pci/access.c */
175 static int pcifront_bus_read(struct pci_bus *bus, unsigned int devfn,
176 int where, int size, u32 *val)
179 struct xen_pci_op op = {
180 .cmd = XEN_PCI_OP_conf_read,
181 .domain = pci_domain_nr(bus),
187 struct pcifront_sd *sd = bus->sysdata;
188 struct pcifront_device *pdev = pcifront_get_pdev(sd);
191 dev_info(&pdev->xdev->dev,
192 "read dev=%04x:%02x:%02x.%d - offset %x size %d\n",
193 pci_domain_nr(bus), bus->number, PCI_SLOT(devfn),
194 PCI_FUNC(devfn), where, size);
196 err = do_pci_op(pdev, &op);
200 dev_info(&pdev->xdev->dev, "read got back value %x\n",
204 } else if (err == -ENODEV) {
205 /* No device here, pretend that it just returned 0 */
210 return errno_to_pcibios_err(err);
213 /* Access to this function is spinlocked in drivers/pci/access.c */
214 static int pcifront_bus_write(struct pci_bus *bus, unsigned int devfn,
215 int where, int size, u32 val)
217 struct xen_pci_op op = {
218 .cmd = XEN_PCI_OP_conf_write,
219 .domain = pci_domain_nr(bus),
226 struct pcifront_sd *sd = bus->sysdata;
227 struct pcifront_device *pdev = pcifront_get_pdev(sd);
230 dev_info(&pdev->xdev->dev,
231 "write dev=%04x:%02x:%02x.%d - "
232 "offset %x size %d val %x\n",
233 pci_domain_nr(bus), bus->number,
234 PCI_SLOT(devfn), PCI_FUNC(devfn), where, size, val);
236 return errno_to_pcibios_err(do_pci_op(pdev, &op));
239 static struct pci_ops pcifront_bus_ops = {
240 .read = pcifront_bus_read,
241 .write = pcifront_bus_write,
244 #ifdef CONFIG_PCI_MSI
245 static int pci_frontend_enable_msix(struct pci_dev *dev,
246 int vector[], int nvec)
250 struct xen_pci_op op = {
251 .cmd = XEN_PCI_OP_enable_msix,
252 .domain = pci_domain_nr(dev->bus),
253 .bus = dev->bus->number,
257 struct pcifront_sd *sd = dev->bus->sysdata;
258 struct pcifront_device *pdev = pcifront_get_pdev(sd);
259 struct msi_desc *entry;
261 if (nvec > SH_INFO_MAX_VEC) {
262 dev_err(&dev->dev, "too much vector for pci frontend: %x."
263 " Increase SH_INFO_MAX_VEC.\n", nvec);
268 for_each_pci_msi_entry(entry, dev) {
269 op.msix_entries[i].entry = entry->msi_attrib.entry_nr;
270 /* Vector is useless at this point. */
271 op.msix_entries[i].vector = -1;
275 err = do_pci_op(pdev, &op);
278 if (likely(!op.value)) {
279 /* we get the result */
280 for (i = 0; i < nvec; i++) {
281 if (op.msix_entries[i].vector <= 0) {
282 dev_warn(&dev->dev, "MSI-X entry %d is invalid: %d!\n",
283 i, op.msix_entries[i].vector);
288 vector[i] = op.msix_entries[i].vector;
291 printk(KERN_DEBUG "enable msix get value %x\n",
296 dev_err(&dev->dev, "enable msix get err %x\n", err);
301 static void pci_frontend_disable_msix(struct pci_dev *dev)
304 struct xen_pci_op op = {
305 .cmd = XEN_PCI_OP_disable_msix,
306 .domain = pci_domain_nr(dev->bus),
307 .bus = dev->bus->number,
310 struct pcifront_sd *sd = dev->bus->sysdata;
311 struct pcifront_device *pdev = pcifront_get_pdev(sd);
313 err = do_pci_op(pdev, &op);
315 /* What should do for error ? */
317 dev_err(&dev->dev, "pci_disable_msix get err %x\n", err);
320 static int pci_frontend_enable_msi(struct pci_dev *dev, int vector[])
323 struct xen_pci_op op = {
324 .cmd = XEN_PCI_OP_enable_msi,
325 .domain = pci_domain_nr(dev->bus),
326 .bus = dev->bus->number,
329 struct pcifront_sd *sd = dev->bus->sysdata;
330 struct pcifront_device *pdev = pcifront_get_pdev(sd);
332 err = do_pci_op(pdev, &op);
334 vector[0] = op.value;
336 dev_warn(&dev->dev, "MSI entry is invalid: %d!\n",
342 dev_err(&dev->dev, "pci frontend enable msi failed for dev "
343 "%x:%x\n", op.bus, op.devfn);
349 static void pci_frontend_disable_msi(struct pci_dev *dev)
352 struct xen_pci_op op = {
353 .cmd = XEN_PCI_OP_disable_msi,
354 .domain = pci_domain_nr(dev->bus),
355 .bus = dev->bus->number,
358 struct pcifront_sd *sd = dev->bus->sysdata;
359 struct pcifront_device *pdev = pcifront_get_pdev(sd);
361 err = do_pci_op(pdev, &op);
362 if (err == XEN_PCI_ERR_dev_not_found) {
363 /* XXX No response from backend, what shall we do? */
364 printk(KERN_DEBUG "get no response from backend for disable MSI\n");
368 /* how can pciback notify us fail? */
369 printk(KERN_DEBUG "get fake response frombackend\n");
372 static struct xen_pci_frontend_ops pci_frontend_ops = {
373 .enable_msi = pci_frontend_enable_msi,
374 .disable_msi = pci_frontend_disable_msi,
375 .enable_msix = pci_frontend_enable_msix,
376 .disable_msix = pci_frontend_disable_msix,
379 static void pci_frontend_registrar(int enable)
382 xen_pci_frontend = &pci_frontend_ops;
384 xen_pci_frontend = NULL;
387 static inline void pci_frontend_registrar(int enable) { };
388 #endif /* CONFIG_PCI_MSI */
390 /* Claim resources for the PCI frontend as-is, backend won't allow changes */
391 static int pcifront_claim_resource(struct pci_dev *dev, void *data)
393 struct pcifront_device *pdev = data;
397 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
398 r = &dev->resource[i];
400 if (!r->parent && r->start && r->flags) {
401 dev_info(&pdev->xdev->dev, "claiming resource %s/%d\n",
403 if (pci_claim_resource(dev, i)) {
404 dev_err(&pdev->xdev->dev, "Could not claim resource %s/%d! "
405 "Device offline. Try using e820_host=1 in the guest config.\n",
414 static int pcifront_scan_bus(struct pcifront_device *pdev,
415 unsigned int domain, unsigned int bus,
421 /* Scan the bus for functions and add.
422 * We omit handling of PCI bridge attachment because pciback prevents
423 * bridges from being exported.
425 for (devfn = 0; devfn < 0x100; devfn++) {
426 d = pci_get_slot(b, devfn);
428 /* Device is already known. */
433 d = pci_scan_single_device(b, devfn);
435 dev_info(&pdev->xdev->dev, "New device on "
436 "%04x:%02x:%02x.%d found.\n", domain, bus,
437 PCI_SLOT(devfn), PCI_FUNC(devfn));
443 static int pcifront_scan_root(struct pcifront_device *pdev,
444 unsigned int domain, unsigned int bus)
447 LIST_HEAD(resources);
448 struct pcifront_sd *sd = NULL;
449 struct pci_bus_entry *bus_entry = NULL;
451 static struct resource busn_res = {
454 .flags = IORESOURCE_BUS,
457 #ifndef CONFIG_PCI_DOMAINS
459 dev_err(&pdev->xdev->dev,
460 "PCI Root in non-zero PCI Domain! domain=%d\n", domain);
461 dev_err(&pdev->xdev->dev,
462 "Please compile with CONFIG_PCI_DOMAINS\n");
468 dev_info(&pdev->xdev->dev, "Creating PCI Frontend Bus %04x:%02x\n",
471 bus_entry = kmalloc(sizeof(*bus_entry), GFP_KERNEL);
472 sd = kmalloc(sizeof(*sd), GFP_KERNEL);
473 if (!bus_entry || !sd) {
477 pci_add_resource(&resources, &ioport_resource);
478 pci_add_resource(&resources, &iomem_resource);
479 pci_add_resource(&resources, &busn_res);
480 pcifront_init_sd(sd, domain, bus, pdev);
482 pci_lock_rescan_remove();
484 b = pci_scan_root_bus(&pdev->xdev->dev, bus,
485 &pcifront_bus_ops, sd, &resources);
487 dev_err(&pdev->xdev->dev,
488 "Error creating PCI Frontend Bus!\n");
490 pci_unlock_rescan_remove();
491 pci_free_resource_list(&resources);
497 list_add(&bus_entry->list, &pdev->root_buses);
499 /* pci_scan_root_bus skips devices which do not have a
500 * devfn==0. The pcifront_scan_bus enumerates all devfn. */
501 err = pcifront_scan_bus(pdev, domain, bus, b);
503 /* Claim resources before going "live" with our devices */
504 pci_walk_bus(b, pcifront_claim_resource, pdev);
506 /* Create SysFS and notify udev of the devices. Aka: "going live" */
507 pci_bus_add_devices(b);
509 pci_unlock_rescan_remove();
519 static int pcifront_rescan_root(struct pcifront_device *pdev,
520 unsigned int domain, unsigned int bus)
525 #ifndef CONFIG_PCI_DOMAINS
527 dev_err(&pdev->xdev->dev,
528 "PCI Root in non-zero PCI Domain! domain=%d\n", domain);
529 dev_err(&pdev->xdev->dev,
530 "Please compile with CONFIG_PCI_DOMAINS\n");
535 dev_info(&pdev->xdev->dev, "Rescanning PCI Frontend Bus %04x:%02x\n",
538 b = pci_find_bus(domain, bus);
540 /* If the bus is unknown, create it. */
541 return pcifront_scan_root(pdev, domain, bus);
543 err = pcifront_scan_bus(pdev, domain, bus, b);
545 /* Claim resources before going "live" with our devices */
546 pci_walk_bus(b, pcifront_claim_resource, pdev);
548 /* Create SysFS and notify udev of the devices. Aka: "going live" */
549 pci_bus_add_devices(b);
554 static void free_root_bus_devs(struct pci_bus *bus)
558 while (!list_empty(&bus->devices)) {
559 dev = container_of(bus->devices.next, struct pci_dev,
561 dev_dbg(&dev->dev, "removing device\n");
562 pci_stop_and_remove_bus_device(dev);
566 static void pcifront_free_roots(struct pcifront_device *pdev)
568 struct pci_bus_entry *bus_entry, *t;
570 dev_dbg(&pdev->xdev->dev, "cleaning up root buses\n");
572 pci_lock_rescan_remove();
573 list_for_each_entry_safe(bus_entry, t, &pdev->root_buses, list) {
574 list_del(&bus_entry->list);
576 free_root_bus_devs(bus_entry->bus);
578 kfree(bus_entry->bus->sysdata);
580 device_unregister(bus_entry->bus->bridge);
581 pci_remove_bus(bus_entry->bus);
585 pci_unlock_rescan_remove();
588 static pci_ers_result_t pcifront_common_process(int cmd,
589 struct pcifront_device *pdev,
590 pci_channel_state_t state)
592 pci_ers_result_t result;
593 struct pci_driver *pdrv;
594 int bus = pdev->sh_info->aer_op.bus;
595 int devfn = pdev->sh_info->aer_op.devfn;
596 struct pci_dev *pcidev;
599 dev_dbg(&pdev->xdev->dev,
600 "pcifront AER process: cmd %x (bus:%x, devfn%x)",
602 result = PCI_ERS_RESULT_NONE;
604 pcidev = pci_get_bus_and_slot(bus, devfn);
605 if (!pcidev || !pcidev->driver) {
606 dev_err(&pdev->xdev->dev, "device or AER driver is NULL\n");
610 pdrv = pcidev->driver;
613 if (pdrv->err_handler && pdrv->err_handler->error_detected) {
614 dev_dbg(&pcidev->dev,
615 "trying to call AER service\n");
619 case XEN_PCI_OP_aer_detected:
620 result = pdrv->err_handler->
621 error_detected(pcidev, state);
623 case XEN_PCI_OP_aer_mmio:
624 result = pdrv->err_handler->
625 mmio_enabled(pcidev);
627 case XEN_PCI_OP_aer_slotreset:
628 result = pdrv->err_handler->
631 case XEN_PCI_OP_aer_resume:
632 pdrv->err_handler->resume(pcidev);
635 dev_err(&pdev->xdev->dev,
636 "bad request in aer recovery "
644 result = PCI_ERS_RESULT_NONE;
650 static void pcifront_do_aer(struct work_struct *data)
652 struct pcifront_device *pdev =
653 container_of(data, struct pcifront_device, op_work);
654 int cmd = pdev->sh_info->aer_op.cmd;
655 pci_channel_state_t state =
656 (pci_channel_state_t)pdev->sh_info->aer_op.err;
658 /*If a pci_conf op is in progress,
659 we have to wait until it is done before service aer op*/
660 dev_dbg(&pdev->xdev->dev,
661 "pcifront service aer bus %x devfn %x\n",
662 pdev->sh_info->aer_op.bus, pdev->sh_info->aer_op.devfn);
664 pdev->sh_info->aer_op.err = pcifront_common_process(cmd, pdev, state);
666 /* Post the operation to the guest. */
668 clear_bit(_XEN_PCIB_active, (unsigned long *)&pdev->sh_info->flags);
669 notify_remote_via_evtchn(pdev->evtchn);
671 /*in case of we lost an aer request in four lines time_window*/
672 smp_mb__before_atomic();
673 clear_bit(_PDEVB_op_active, &pdev->flags);
674 smp_mb__after_atomic();
676 schedule_pcifront_aer_op(pdev);
680 static irqreturn_t pcifront_handler_aer(int irq, void *dev)
682 struct pcifront_device *pdev = dev;
683 schedule_pcifront_aer_op(pdev);
686 static int pcifront_connect_and_init_dma(struct pcifront_device *pdev)
690 spin_lock(&pcifront_dev_lock);
693 dev_info(&pdev->xdev->dev, "Installing PCI frontend\n");
698 spin_unlock(&pcifront_dev_lock);
700 if (!err && !swiotlb_nr_tbl()) {
701 err = pci_xen_swiotlb_init_late();
703 dev_err(&pdev->xdev->dev, "Could not setup SWIOTLB!\n");
708 static void pcifront_disconnect(struct pcifront_device *pdev)
710 spin_lock(&pcifront_dev_lock);
712 if (pdev == pcifront_dev) {
713 dev_info(&pdev->xdev->dev,
714 "Disconnecting PCI Frontend Buses\n");
718 spin_unlock(&pcifront_dev_lock);
720 static struct pcifront_device *alloc_pdev(struct xenbus_device *xdev)
722 struct pcifront_device *pdev;
724 pdev = kzalloc(sizeof(struct pcifront_device), GFP_KERNEL);
729 (struct xen_pci_sharedinfo *)__get_free_page(GFP_KERNEL);
730 if (pdev->sh_info == NULL) {
735 pdev->sh_info->flags = 0;
737 /*Flag for registering PV AER handler*/
738 set_bit(_XEN_PCIB_AERHANDLER, (void *)&pdev->sh_info->flags);
740 dev_set_drvdata(&xdev->dev, pdev);
743 INIT_LIST_HEAD(&pdev->root_buses);
745 spin_lock_init(&pdev->sh_info_lock);
747 pdev->evtchn = INVALID_EVTCHN;
748 pdev->gnt_ref = INVALID_GRANT_REF;
751 INIT_WORK(&pdev->op_work, pcifront_do_aer);
753 dev_dbg(&xdev->dev, "Allocated pdev @ 0x%p pdev->sh_info @ 0x%p\n",
754 pdev, pdev->sh_info);
759 static void free_pdev(struct pcifront_device *pdev)
761 dev_dbg(&pdev->xdev->dev, "freeing pdev @ 0x%p\n", pdev);
763 pcifront_free_roots(pdev);
765 cancel_work_sync(&pdev->op_work);
768 unbind_from_irqhandler(pdev->irq, pdev);
770 if (pdev->evtchn != INVALID_EVTCHN)
771 xenbus_free_evtchn(pdev->xdev, pdev->evtchn);
773 if (pdev->gnt_ref != INVALID_GRANT_REF)
774 gnttab_end_foreign_access(pdev->gnt_ref, 0 /* r/w page */,
775 (unsigned long)pdev->sh_info);
777 free_page((unsigned long)pdev->sh_info);
779 dev_set_drvdata(&pdev->xdev->dev, NULL);
784 static int pcifront_publish_info(struct pcifront_device *pdev)
787 struct xenbus_transaction trans;
790 err = xenbus_grant_ring(pdev->xdev, pdev->sh_info, 1, &gref);
794 pdev->gnt_ref = gref;
796 err = xenbus_alloc_evtchn(pdev->xdev, &pdev->evtchn);
800 err = bind_evtchn_to_irqhandler(pdev->evtchn, pcifront_handler_aer,
801 0, "pcifront", pdev);
809 err = xenbus_transaction_start(&trans);
811 xenbus_dev_fatal(pdev->xdev, err,
812 "Error writing configuration for backend "
813 "(start transaction)");
817 err = xenbus_printf(trans, pdev->xdev->nodename,
818 "pci-op-ref", "%u", pdev->gnt_ref);
820 err = xenbus_printf(trans, pdev->xdev->nodename,
821 "event-channel", "%u", pdev->evtchn);
823 err = xenbus_printf(trans, pdev->xdev->nodename,
824 "magic", XEN_PCI_MAGIC);
827 xenbus_transaction_end(trans, 1);
828 xenbus_dev_fatal(pdev->xdev, err,
829 "Error writing configuration for backend");
832 err = xenbus_transaction_end(trans, 0);
836 xenbus_dev_fatal(pdev->xdev, err,
837 "Error completing transaction "
843 xenbus_switch_state(pdev->xdev, XenbusStateInitialised);
845 dev_dbg(&pdev->xdev->dev, "publishing successful!\n");
851 static int pcifront_try_connect(struct pcifront_device *pdev)
854 int i, num_roots, len;
856 unsigned int domain, bus;
859 /* Only connect once */
860 if (xenbus_read_driver_state(pdev->xdev->nodename) !=
861 XenbusStateInitialised)
864 err = pcifront_connect_and_init_dma(pdev);
865 if (err && err != -EEXIST) {
866 xenbus_dev_fatal(pdev->xdev, err,
867 "Error setting up PCI Frontend");
871 err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend,
872 "root_num", "%d", &num_roots);
873 if (err == -ENOENT) {
874 xenbus_dev_error(pdev->xdev, err,
875 "No PCI Roots found, trying 0000:00");
876 err = pcifront_scan_root(pdev, 0, 0);
878 xenbus_dev_fatal(pdev->xdev, err,
879 "Error scanning PCI root 0000:00");
883 } else if (err != 1) {
886 xenbus_dev_fatal(pdev->xdev, err,
887 "Error reading number of PCI roots");
891 for (i = 0; i < num_roots; i++) {
892 len = snprintf(str, sizeof(str), "root-%d", i);
893 if (unlikely(len >= (sizeof(str) - 1))) {
898 err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend, str,
899 "%x:%x", &domain, &bus);
903 xenbus_dev_fatal(pdev->xdev, err,
904 "Error reading PCI root %d", i);
908 err = pcifront_scan_root(pdev, domain, bus);
910 xenbus_dev_fatal(pdev->xdev, err,
911 "Error scanning PCI root %04x:%02x",
917 err = xenbus_switch_state(pdev->xdev, XenbusStateConnected);
923 static int pcifront_try_disconnect(struct pcifront_device *pdev)
926 enum xenbus_state prev_state;
929 prev_state = xenbus_read_driver_state(pdev->xdev->nodename);
931 if (prev_state >= XenbusStateClosing)
934 if (prev_state == XenbusStateConnected) {
935 pcifront_free_roots(pdev);
936 pcifront_disconnect(pdev);
939 err = xenbus_switch_state(pdev->xdev, XenbusStateClosed);
946 static int pcifront_attach_devices(struct pcifront_device *pdev)
949 int i, num_roots, len;
950 unsigned int domain, bus;
953 if (xenbus_read_driver_state(pdev->xdev->nodename) !=
954 XenbusStateReconfiguring)
957 err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend,
958 "root_num", "%d", &num_roots);
959 if (err == -ENOENT) {
960 xenbus_dev_error(pdev->xdev, err,
961 "No PCI Roots found, trying 0000:00");
962 err = pcifront_rescan_root(pdev, 0, 0);
964 xenbus_dev_fatal(pdev->xdev, err,
965 "Error scanning PCI root 0000:00");
969 } else if (err != 1) {
972 xenbus_dev_fatal(pdev->xdev, err,
973 "Error reading number of PCI roots");
977 for (i = 0; i < num_roots; i++) {
978 len = snprintf(str, sizeof(str), "root-%d", i);
979 if (unlikely(len >= (sizeof(str) - 1))) {
984 err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend, str,
985 "%x:%x", &domain, &bus);
989 xenbus_dev_fatal(pdev->xdev, err,
990 "Error reading PCI root %d", i);
994 err = pcifront_rescan_root(pdev, domain, bus);
996 xenbus_dev_fatal(pdev->xdev, err,
997 "Error scanning PCI root %04x:%02x",
1003 xenbus_switch_state(pdev->xdev, XenbusStateConnected);
1009 static int pcifront_detach_devices(struct pcifront_device *pdev)
1013 unsigned int domain, bus, slot, func;
1014 struct pci_dev *pci_dev;
1017 if (xenbus_read_driver_state(pdev->xdev->nodename) !=
1018 XenbusStateConnected)
1021 err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend, "num_devs", "%d",
1026 xenbus_dev_fatal(pdev->xdev, err,
1027 "Error reading number of PCI devices");
1031 /* Find devices being detached and remove them. */
1032 for (i = 0; i < num_devs; i++) {
1034 l = snprintf(str, sizeof(str), "state-%d", i);
1035 if (unlikely(l >= (sizeof(str) - 1))) {
1039 err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend, str, "%d",
1042 state = XenbusStateUnknown;
1044 if (state != XenbusStateClosing)
1047 /* Remove device. */
1048 l = snprintf(str, sizeof(str), "vdev-%d", i);
1049 if (unlikely(l >= (sizeof(str) - 1))) {
1053 err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend, str,
1054 "%x:%x:%x.%x", &domain, &bus, &slot, &func);
1058 xenbus_dev_fatal(pdev->xdev, err,
1059 "Error reading PCI device %d", i);
1063 pci_dev = pci_get_domain_bus_and_slot(domain, bus,
1064 PCI_DEVFN(slot, func));
1066 dev_dbg(&pdev->xdev->dev,
1067 "Cannot get PCI device %04x:%02x:%02x.%d\n",
1068 domain, bus, slot, func);
1071 pci_lock_rescan_remove();
1072 pci_stop_and_remove_bus_device(pci_dev);
1073 pci_dev_put(pci_dev);
1074 pci_unlock_rescan_remove();
1076 dev_dbg(&pdev->xdev->dev,
1077 "PCI device %04x:%02x:%02x.%d removed.\n",
1078 domain, bus, slot, func);
1081 err = xenbus_switch_state(pdev->xdev, XenbusStateReconfiguring);
1087 static void __init_refok pcifront_backend_changed(struct xenbus_device *xdev,
1088 enum xenbus_state be_state)
1090 struct pcifront_device *pdev = dev_get_drvdata(&xdev->dev);
1093 case XenbusStateUnknown:
1094 case XenbusStateInitialising:
1095 case XenbusStateInitWait:
1096 case XenbusStateInitialised:
1099 case XenbusStateConnected:
1100 pcifront_try_connect(pdev);
1103 case XenbusStateClosed:
1104 if (xdev->state == XenbusStateClosed)
1106 /* Missed the backend's CLOSING state -- fallthrough */
1107 case XenbusStateClosing:
1108 dev_warn(&xdev->dev, "backend going away!\n");
1109 pcifront_try_disconnect(pdev);
1112 case XenbusStateReconfiguring:
1113 pcifront_detach_devices(pdev);
1116 case XenbusStateReconfigured:
1117 pcifront_attach_devices(pdev);
1122 static int pcifront_xenbus_probe(struct xenbus_device *xdev,
1123 const struct xenbus_device_id *id)
1126 struct pcifront_device *pdev = alloc_pdev(xdev);
1130 xenbus_dev_fatal(xdev, err,
1131 "Error allocating pcifront_device struct");
1135 err = pcifront_publish_info(pdev);
1143 static int pcifront_xenbus_remove(struct xenbus_device *xdev)
1145 struct pcifront_device *pdev = dev_get_drvdata(&xdev->dev);
1152 static const struct xenbus_device_id xenpci_ids[] = {
1157 static struct xenbus_driver xenpci_driver = {
1160 .probe = pcifront_xenbus_probe,
1161 .remove = pcifront_xenbus_remove,
1162 .otherend_changed = pcifront_backend_changed,
1165 static int __init pcifront_init(void)
1167 if (!xen_pv_domain() || xen_initial_domain())
1170 if (!xen_has_pv_devices())
1173 pci_frontend_registrar(1 /* enable */);
1175 return xenbus_register_frontend(&xenpci_driver);
1178 static void __exit pcifront_cleanup(void)
1180 xenbus_unregister_driver(&xenpci_driver);
1181 pci_frontend_registrar(0 /* disable */);
1183 module_init(pcifront_init);
1184 module_exit(pcifront_cleanup);
1186 MODULE_DESCRIPTION("Xen PCI passthrough frontend.");
1187 MODULE_LICENSE("GPL");
1188 MODULE_ALIAS("xen:pci");