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 <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
;
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
;
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
;
55 struct pci_sysdata sd
;
56 struct pcifront_device
*pdev
;
59 static inline struct pcifront_device
*
60 pcifront_get_pdev(struct pcifront_sd
*sd
)
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
;
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
)
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
;
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
)
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
;
122 spin_lock_irqsave(&pdev
->sh_info_lock
, irq_flags
);
124 memcpy(active_op
, op
, sizeof(struct xen_pci_op
));
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
;
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
));
174 spin_unlock_irqrestore(&pdev
->sh_info_lock
, irq_flags
);
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
)
183 struct xen_pci_op op
= {
184 .cmd
= XEN_PCI_OP_conf_read
,
185 .domain
= pci_domain_nr(bus
),
191 struct pcifront_sd
*sd
= bus
->sysdata
;
192 struct pcifront_device
*pdev
= pcifront_get_pdev(sd
);
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
);
204 dev_info(&pdev
->xdev
->dev
, "read got back value %x\n",
208 } else if (err
== -ENODEV
) {
209 /* No device here, pretend that it just returned 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
),
230 struct pcifront_sd
*sd
= bus
->sysdata
;
231 struct pcifront_device
*pdev
= pcifront_get_pdev(sd
);
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
)
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
,
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
);
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;
279 err
= do_pci_op(pdev
, &op
);
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
);
292 vector
[i
] = op
.msix_entries
[i
].vector
;
295 printk(KERN_DEBUG
"enable msix get value %x\n",
300 dev_err(&dev
->dev
, "enable msix get err %x\n", err
);
305 static void pci_frontend_disable_msix(struct pci_dev
*dev
)
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
,
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 ? */
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
[])
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
,
333 struct pcifront_sd
*sd
= dev
->bus
->sysdata
;
334 struct pcifront_device
*pdev
= pcifront_get_pdev(sd
);
336 err
= do_pci_op(pdev
, &op
);
338 vector
[0] = op
.value
;
340 dev_warn(&dev
->dev
, "MSI entry is invalid: %d!\n",
346 dev_err(&dev
->dev
, "pci frontend enable msi failed for dev "
347 "%x:%x\n", op
.bus
, op
.devfn
);
353 static void pci_frontend_disable_msi(struct pci_dev
*dev
)
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
,
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");
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
)
386 xen_pci_frontend
= &pci_frontend_ops
;
388 xen_pci_frontend
= NULL
;
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
;
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",
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",
418 static int pcifront_scan_bus(struct pcifront_device
*pdev
,
419 unsigned int domain
, unsigned int bus
,
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
);
432 /* Device is already known. */
437 d
= pci_scan_single_device(b
, devfn
);
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
));
447 static int pcifront_scan_root(struct pcifront_device
*pdev
,
448 unsigned int domain
, unsigned int bus
)
451 struct pcifront_sd
*sd
= NULL
;
452 struct pci_bus_entry
*bus_entry
= NULL
;
455 #ifndef CONFIG_PCI_DOMAINS
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");
466 dev_info(&pdev
->xdev
->dev
, "Creating PCI Frontend Bus %04x:%02x\n",
469 bus_entry
= kzalloc(sizeof(*bus_entry
), GFP_KERNEL
);
470 sd
= kzalloc(sizeof(*sd
), GFP_KERNEL
);
471 if (!bus_entry
|| !sd
) {
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
);
482 dev_err(&pdev
->xdev
->dev
,
483 "Error creating PCI Frontend Bus!\n");
485 pci_unlock_rescan_remove();
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();
513 static int pcifront_rescan_root(struct pcifront_device
*pdev
,
514 unsigned int domain
, unsigned int bus
)
519 #ifndef CONFIG_PCI_DOMAINS
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");
529 dev_info(&pdev
->xdev
->dev
, "Rescanning PCI Frontend Bus %04x:%02x\n",
532 b
= pci_find_bus(domain
, bus
);
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
);
548 static void free_root_bus_devs(struct pci_bus
*bus
)
552 while (!list_empty(&bus
->devices
)) {
553 dev
= container_of(bus
->devices
.next
, struct pci_dev
,
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
);
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
;
593 dev_dbg(&pdev
->xdev
->dev
,
594 "pcifront AER process: cmd %x (bus:%x, devfn%x)",
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");
605 pdrv
= pcidev
->driver
;
608 if (pdrv
->err_handler
&& pdrv
->err_handler
->error_detected
) {
609 dev_dbg(&pcidev
->dev
,
610 "trying to call AER service\n");
614 case XEN_PCI_OP_aer_detected
:
615 result
= pdrv
->err_handler
->
616 error_detected(pcidev
, state
);
618 case XEN_PCI_OP_aer_mmio
:
619 result
= pdrv
->err_handler
->
620 mmio_enabled(pcidev
);
622 case XEN_PCI_OP_aer_slotreset
:
623 result
= pdrv
->err_handler
->
626 case XEN_PCI_OP_aer_resume
:
627 pdrv
->err_handler
->resume(pcidev
);
630 dev_err(&pdev
->xdev
->dev
,
631 "bad request in aer recovery "
639 result
= PCI_ERS_RESULT_NONE
;
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. */
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
);
681 static int pcifront_connect_and_init_dma(struct pcifront_device
*pdev
)
685 spin_lock(&pcifront_dev_lock
);
688 dev_info(&pdev
->xdev
->dev
, "Installing PCI frontend\n");
693 spin_unlock(&pcifront_dev_lock
);
695 if (!err
&& !swiotlb_nr_tbl()) {
696 err
= pci_xen_swiotlb_init_late();
698 dev_err(&pdev
->xdev
->dev
, "Could not setup SWIOTLB!\n");
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");
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
);
724 (struct xen_pci_sharedinfo
*)__get_free_page(GFP_KERNEL
);
725 if (pdev
->sh_info
== NULL
) {
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
);
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
;
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
);
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
);
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
);
772 free_page((unsigned long)pdev
->sh_info
);
774 dev_set_drvdata(&pdev
->xdev
->dev
, NULL
);
779 static int pcifront_publish_info(struct pcifront_device
*pdev
)
782 struct xenbus_transaction trans
;
784 err
= xenbus_grant_ring(pdev
->xdev
, virt_to_mfn(pdev
->sh_info
));
790 err
= xenbus_alloc_evtchn(pdev
->xdev
, &pdev
->evtchn
);
794 err
= bind_evtchn_to_irqhandler(pdev
->evtchn
, pcifront_handler_aer
,
795 0, "pcifront", pdev
);
803 err
= xenbus_transaction_start(&trans
);
805 xenbus_dev_fatal(pdev
->xdev
, err
,
806 "Error writing configuration for backend "
807 "(start transaction)");
811 err
= xenbus_printf(trans
, pdev
->xdev
->nodename
,
812 "pci-op-ref", "%u", pdev
->gnt_ref
);
814 err
= xenbus_printf(trans
, pdev
->xdev
->nodename
,
815 "event-channel", "%u", pdev
->evtchn
);
817 err
= xenbus_printf(trans
, pdev
->xdev
->nodename
,
818 "magic", XEN_PCI_MAGIC
);
821 xenbus_transaction_end(trans
, 1);
822 xenbus_dev_fatal(pdev
->xdev
, err
,
823 "Error writing configuration for backend");
826 err
= xenbus_transaction_end(trans
, 0);
830 xenbus_dev_fatal(pdev
->xdev
, err
,
831 "Error completing transaction "
837 xenbus_switch_state(pdev
->xdev
, XenbusStateInitialised
);
839 dev_dbg(&pdev
->xdev
->dev
, "publishing successful!\n");
845 static int pcifront_try_connect(struct pcifront_device
*pdev
)
848 int i
, num_roots
, len
;
850 unsigned int domain
, bus
;
853 /* Only connect once */
854 if (xenbus_read_driver_state(pdev
->xdev
->nodename
) !=
855 XenbusStateInitialised
)
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");
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);
872 } else if (err
!= 1) {
875 xenbus_dev_fatal(pdev
->xdev
, err
,
876 "Error reading number of PCI roots");
880 for (i
= 0; i
< num_roots
; i
++) {
881 len
= snprintf(str
, sizeof(str
), "root-%d", i
);
882 if (unlikely(len
>= (sizeof(str
) - 1))) {
887 err
= xenbus_scanf(XBT_NIL
, pdev
->xdev
->otherend
, str
,
888 "%x:%x", &domain
, &bus
);
892 xenbus_dev_fatal(pdev
->xdev
, err
,
893 "Error reading PCI root %d", i
);
897 err
= pcifront_scan_root(pdev
, domain
, bus
);
899 xenbus_dev_fatal(pdev
->xdev
, err
,
900 "Error scanning PCI root %04x:%02x",
906 err
= xenbus_switch_state(pdev
->xdev
, XenbusStateConnected
);
912 static int pcifront_try_disconnect(struct pcifront_device
*pdev
)
915 enum xenbus_state prev_state
;
918 prev_state
= xenbus_read_driver_state(pdev
->xdev
->nodename
);
920 if (prev_state
>= XenbusStateClosing
)
923 if (prev_state
== XenbusStateConnected
) {
924 pcifront_free_roots(pdev
);
925 pcifront_disconnect(pdev
);
928 err
= xenbus_switch_state(pdev
->xdev
, XenbusStateClosed
);
935 static int pcifront_attach_devices(struct pcifront_device
*pdev
)
938 int i
, num_roots
, len
;
939 unsigned int domain
, bus
;
942 if (xenbus_read_driver_state(pdev
->xdev
->nodename
) !=
943 XenbusStateReconfiguring
)
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);
953 } else if (err
!= 1) {
956 xenbus_dev_fatal(pdev
->xdev
, err
,
957 "Error reading number of PCI roots");
961 for (i
= 0; i
< num_roots
; i
++) {
962 len
= snprintf(str
, sizeof(str
), "root-%d", i
);
963 if (unlikely(len
>= (sizeof(str
) - 1))) {
968 err
= xenbus_scanf(XBT_NIL
, pdev
->xdev
->otherend
, str
,
969 "%x:%x", &domain
, &bus
);
973 xenbus_dev_fatal(pdev
->xdev
, err
,
974 "Error reading PCI root %d", i
);
978 err
= pcifront_rescan_root(pdev
, domain
, bus
);
980 xenbus_dev_fatal(pdev
->xdev
, err
,
981 "Error scanning PCI root %04x:%02x",
987 xenbus_switch_state(pdev
->xdev
, XenbusStateConnected
);
993 static int pcifront_detach_devices(struct pcifront_device
*pdev
)
997 unsigned int domain
, bus
, slot
, func
;
998 struct pci_dev
*pci_dev
;
1001 if (xenbus_read_driver_state(pdev
->xdev
->nodename
) !=
1002 XenbusStateConnected
)
1005 err
= xenbus_scanf(XBT_NIL
, pdev
->xdev
->otherend
, "num_devs", "%d",
1010 xenbus_dev_fatal(pdev
->xdev
, err
,
1011 "Error reading number of PCI devices");
1015 /* Find devices being detached and remove them. */
1016 for (i
= 0; i
< num_devs
; i
++) {
1018 l
= snprintf(str
, sizeof(str
), "state-%d", i
);
1019 if (unlikely(l
>= (sizeof(str
) - 1))) {
1023 err
= xenbus_scanf(XBT_NIL
, pdev
->xdev
->otherend
, str
, "%d",
1026 state
= XenbusStateUnknown
;
1028 if (state
!= XenbusStateClosing
)
1031 /* Remove device. */
1032 l
= snprintf(str
, sizeof(str
), "vdev-%d", i
);
1033 if (unlikely(l
>= (sizeof(str
) - 1))) {
1037 err
= xenbus_scanf(XBT_NIL
, pdev
->xdev
->otherend
, str
,
1038 "%x:%x:%x.%x", &domain
, &bus
, &slot
, &func
);
1042 xenbus_dev_fatal(pdev
->xdev
, err
,
1043 "Error reading PCI device %d", i
);
1047 pci_dev
= pci_get_domain_bus_and_slot(domain
, bus
,
1048 PCI_DEVFN(slot
, func
));
1050 dev_dbg(&pdev
->xdev
->dev
,
1051 "Cannot get PCI device %04x:%02x:%02x.%d\n",
1052 domain
, bus
, slot
, func
);
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
);
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
);
1077 case XenbusStateUnknown
:
1078 case XenbusStateInitialising
:
1079 case XenbusStateInitWait
:
1080 case XenbusStateInitialised
:
1083 case XenbusStateConnected
:
1084 pcifront_try_connect(pdev
);
1087 case XenbusStateClosed
:
1088 if (xdev
->state
== XenbusStateClosed
)
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
);
1096 case XenbusStateReconfiguring
:
1097 pcifront_detach_devices(pdev
);
1100 case XenbusStateReconfigured
:
1101 pcifront_attach_devices(pdev
);
1106 static int pcifront_xenbus_probe(struct xenbus_device
*xdev
,
1107 const struct xenbus_device_id
*id
)
1110 struct pcifront_device
*pdev
= alloc_pdev(xdev
);
1114 xenbus_dev_fatal(xdev
, err
,
1115 "Error allocating pcifront_device struct");
1119 err
= pcifront_publish_info(pdev
);
1127 static int pcifront_xenbus_remove(struct xenbus_device
*xdev
)
1129 struct pcifront_device
*pdev
= dev_get_drvdata(&xdev
->dev
);
1136 static const struct xenbus_device_id xenpci_ids
[] = {
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())
1152 if (!xen_has_pv_devices())
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");