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 <asm/atomic.h>
20 #include <linux/workqueue.h>
21 #include <linux/bitops.h>
22 #include <linux/time.h>
24 #define INVALID_GRANT_REF (0)
25 #define INVALID_EVTCHN (-1)
27 struct pci_bus_entry
{
28 struct list_head list
;
32 #define _PDEVB_op_active (0)
33 #define PDEVB_op_active (1 << (_PDEVB_op_active))
35 struct pcifront_device
{
36 struct xenbus_device
*xdev
;
37 struct list_head root_buses
;
44 /* Lock this when doing any operations in sh_info */
45 spinlock_t sh_info_lock
;
46 struct xen_pci_sharedinfo
*sh_info
;
47 struct work_struct op_work
;
54 struct pcifront_device
*pdev
;
57 static inline struct pcifront_device
*
58 pcifront_get_pdev(struct pcifront_sd
*sd
)
63 static inline void pcifront_init_sd(struct pcifront_sd
*sd
,
64 unsigned int domain
, unsigned int bus
,
65 struct pcifront_device
*pdev
)
71 static DEFINE_SPINLOCK(pcifront_dev_lock
);
72 static struct pcifront_device
*pcifront_dev
;
74 static int verbose_request
;
75 module_param(verbose_request
, int, 0644);
77 static int errno_to_pcibios_err(int errno
)
80 case XEN_PCI_ERR_success
:
81 return PCIBIOS_SUCCESSFUL
;
83 case XEN_PCI_ERR_dev_not_found
:
84 return PCIBIOS_DEVICE_NOT_FOUND
;
86 case XEN_PCI_ERR_invalid_offset
:
87 case XEN_PCI_ERR_op_failed
:
88 return PCIBIOS_BAD_REGISTER_NUMBER
;
90 case XEN_PCI_ERR_not_implemented
:
91 return PCIBIOS_FUNC_NOT_SUPPORTED
;
93 case XEN_PCI_ERR_access_denied
:
94 return PCIBIOS_SET_FAILED
;
99 static inline void schedule_pcifront_aer_op(struct pcifront_device
*pdev
)
101 if (test_bit(_XEN_PCIB_active
, (unsigned long *)&pdev
->sh_info
->flags
)
102 && !test_and_set_bit(_PDEVB_op_active
, &pdev
->flags
)) {
103 dev_dbg(&pdev
->xdev
->dev
, "schedule aer frontend job\n");
104 schedule_work(&pdev
->op_work
);
108 static int do_pci_op(struct pcifront_device
*pdev
, struct xen_pci_op
*op
)
111 struct xen_pci_op
*active_op
= &pdev
->sh_info
->op
;
112 unsigned long irq_flags
;
113 evtchn_port_t port
= pdev
->evtchn
;
114 unsigned irq
= pdev
->irq
;
118 spin_lock_irqsave(&pdev
->sh_info_lock
, irq_flags
);
120 memcpy(active_op
, op
, sizeof(struct xen_pci_op
));
124 set_bit(_XEN_PCIF_active
, (unsigned long *)&pdev
->sh_info
->flags
);
125 notify_remote_via_evtchn(port
);
128 * We set a poll timeout of 3 seconds but give up on return after
129 * 2 seconds. It is better to time out too late rather than too early
130 * (in the latter case we end up continually re-executing poll() with a
131 * timeout in the past). 1s difference gives plenty of slack for error.
133 do_gettimeofday(&tv
);
134 ns_timeout
= timeval_to_ns(&tv
) + 2 * (s64
)NSEC_PER_SEC
;
136 xen_clear_irq_pending(irq
);
138 while (test_bit(_XEN_PCIF_active
,
139 (unsigned long *)&pdev
->sh_info
->flags
)) {
140 xen_poll_irq_timeout(irq
, jiffies
+ 3*HZ
);
141 xen_clear_irq_pending(irq
);
142 do_gettimeofday(&tv
);
143 ns
= timeval_to_ns(&tv
);
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.%01x - 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.%01x - "
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 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 list_for_each_entry(entry
, &dev
->msi_list
, list
) {
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 *(*vector
+i
) = op
.msix_entries
[i
].vector
;
284 printk(KERN_DEBUG
"enable msix get value %x\n",
289 dev_err(&dev
->dev
, "enable msix get err %x\n", err
);
294 static void pci_frontend_disable_msix(struct pci_dev
*dev
)
297 struct xen_pci_op op
= {
298 .cmd
= XEN_PCI_OP_disable_msix
,
299 .domain
= pci_domain_nr(dev
->bus
),
300 .bus
= dev
->bus
->number
,
303 struct pcifront_sd
*sd
= dev
->bus
->sysdata
;
304 struct pcifront_device
*pdev
= pcifront_get_pdev(sd
);
306 err
= do_pci_op(pdev
, &op
);
308 /* What should do for error ? */
310 dev_err(&dev
->dev
, "pci_disable_msix get err %x\n", err
);
313 static int pci_frontend_enable_msi(struct pci_dev
*dev
, int **vector
)
316 struct xen_pci_op op
= {
317 .cmd
= XEN_PCI_OP_enable_msi
,
318 .domain
= pci_domain_nr(dev
->bus
),
319 .bus
= dev
->bus
->number
,
322 struct pcifront_sd
*sd
= dev
->bus
->sysdata
;
323 struct pcifront_device
*pdev
= pcifront_get_pdev(sd
);
325 err
= do_pci_op(pdev
, &op
);
327 *(*vector
) = op
.value
;
329 dev_err(&dev
->dev
, "pci frontend enable msi failed for dev "
330 "%x:%x\n", op
.bus
, op
.devfn
);
336 static void pci_frontend_disable_msi(struct pci_dev
*dev
)
339 struct xen_pci_op op
= {
340 .cmd
= XEN_PCI_OP_disable_msi
,
341 .domain
= pci_domain_nr(dev
->bus
),
342 .bus
= dev
->bus
->number
,
345 struct pcifront_sd
*sd
= dev
->bus
->sysdata
;
346 struct pcifront_device
*pdev
= pcifront_get_pdev(sd
);
348 err
= do_pci_op(pdev
, &op
);
349 if (err
== XEN_PCI_ERR_dev_not_found
) {
350 /* XXX No response from backend, what shall we do? */
351 printk(KERN_DEBUG
"get no response from backend for disable MSI\n");
355 /* how can pciback notify us fail? */
356 printk(KERN_DEBUG
"get fake response frombackend\n");
359 static struct xen_pci_frontend_ops pci_frontend_ops
= {
360 .enable_msi
= pci_frontend_enable_msi
,
361 .disable_msi
= pci_frontend_disable_msi
,
362 .enable_msix
= pci_frontend_enable_msix
,
363 .disable_msix
= pci_frontend_disable_msix
,
366 static void pci_frontend_registrar(int enable
)
369 xen_pci_frontend
= &pci_frontend_ops
;
371 xen_pci_frontend
= NULL
;
374 static inline void pci_frontend_registrar(int enable
) { };
375 #endif /* CONFIG_PCI_MSI */
377 /* Claim resources for the PCI frontend as-is, backend won't allow changes */
378 static int pcifront_claim_resource(struct pci_dev
*dev
, void *data
)
380 struct pcifront_device
*pdev
= data
;
384 for (i
= 0; i
< PCI_NUM_RESOURCES
; i
++) {
385 r
= &dev
->resource
[i
];
387 if (!r
->parent
&& r
->start
&& r
->flags
) {
388 dev_info(&pdev
->xdev
->dev
, "claiming resource %s/%d\n",
390 if (pci_claim_resource(dev
, i
)) {
391 dev_err(&pdev
->xdev
->dev
, "Could not claim "
392 "resource %s/%d! Device offline. Try "
393 "giving less than 4GB to domain.\n",
402 static int __devinit
pcifront_scan_bus(struct pcifront_device
*pdev
,
403 unsigned int domain
, unsigned int bus
,
409 /* Scan the bus for functions and add.
410 * We omit handling of PCI bridge attachment because pciback prevents
411 * bridges from being exported.
413 for (devfn
= 0; devfn
< 0x100; devfn
++) {
414 d
= pci_get_slot(b
, devfn
);
416 /* Device is already known. */
421 d
= pci_scan_single_device(b
, devfn
);
423 dev_info(&pdev
->xdev
->dev
, "New device on "
424 "%04x:%02x:%02x.%02x found.\n", domain
, bus
,
425 PCI_SLOT(devfn
), PCI_FUNC(devfn
));
431 static int __devinit
pcifront_scan_root(struct pcifront_device
*pdev
,
432 unsigned int domain
, unsigned int bus
)
435 struct pcifront_sd
*sd
= NULL
;
436 struct pci_bus_entry
*bus_entry
= NULL
;
439 #ifndef CONFIG_PCI_DOMAINS
441 dev_err(&pdev
->xdev
->dev
,
442 "PCI Root in non-zero PCI Domain! domain=%d\n", domain
);
443 dev_err(&pdev
->xdev
->dev
,
444 "Please compile with CONFIG_PCI_DOMAINS\n");
450 dev_info(&pdev
->xdev
->dev
, "Creating PCI Frontend Bus %04x:%02x\n",
453 bus_entry
= kmalloc(sizeof(*bus_entry
), GFP_KERNEL
);
454 sd
= kmalloc(sizeof(*sd
), GFP_KERNEL
);
455 if (!bus_entry
|| !sd
) {
459 pcifront_init_sd(sd
, domain
, bus
, pdev
);
461 b
= pci_scan_bus_parented(&pdev
->xdev
->dev
, bus
,
462 &pcifront_bus_ops
, sd
);
464 dev_err(&pdev
->xdev
->dev
,
465 "Error creating PCI Frontend Bus!\n");
472 list_add(&bus_entry
->list
, &pdev
->root_buses
);
474 /* pci_scan_bus_parented skips devices which do not have a have
475 * devfn==0. The pcifront_scan_bus enumerates all devfn. */
476 err
= pcifront_scan_bus(pdev
, domain
, bus
, b
);
478 /* Claim resources before going "live" with our devices */
479 pci_walk_bus(b
, pcifront_claim_resource
, pdev
);
481 /* Create SysFS and notify udev of the devices. Aka: "going live" */
482 pci_bus_add_devices(b
);
493 static int __devinit
pcifront_rescan_root(struct pcifront_device
*pdev
,
494 unsigned int domain
, unsigned int bus
)
499 #ifndef CONFIG_PCI_DOMAINS
501 dev_err(&pdev
->xdev
->dev
,
502 "PCI Root in non-zero PCI Domain! domain=%d\n", domain
);
503 dev_err(&pdev
->xdev
->dev
,
504 "Please compile with CONFIG_PCI_DOMAINS\n");
509 dev_info(&pdev
->xdev
->dev
, "Rescanning PCI Frontend Bus %04x:%02x\n",
512 b
= pci_find_bus(domain
, bus
);
514 /* If the bus is unknown, create it. */
515 return pcifront_scan_root(pdev
, domain
, bus
);
517 err
= pcifront_scan_bus(pdev
, domain
, bus
, b
);
519 /* Claim resources before going "live" with our devices */
520 pci_walk_bus(b
, pcifront_claim_resource
, pdev
);
522 /* Create SysFS and notify udev of the devices. Aka: "going live" */
523 pci_bus_add_devices(b
);
528 static void free_root_bus_devs(struct pci_bus
*bus
)
532 while (!list_empty(&bus
->devices
)) {
533 dev
= container_of(bus
->devices
.next
, struct pci_dev
,
535 dev_dbg(&dev
->dev
, "removing device\n");
536 pci_remove_bus_device(dev
);
540 static void pcifront_free_roots(struct pcifront_device
*pdev
)
542 struct pci_bus_entry
*bus_entry
, *t
;
544 dev_dbg(&pdev
->xdev
->dev
, "cleaning up root buses\n");
546 list_for_each_entry_safe(bus_entry
, t
, &pdev
->root_buses
, list
) {
547 list_del(&bus_entry
->list
);
549 free_root_bus_devs(bus_entry
->bus
);
551 kfree(bus_entry
->bus
->sysdata
);
553 device_unregister(bus_entry
->bus
->bridge
);
554 pci_remove_bus(bus_entry
->bus
);
560 static pci_ers_result_t
pcifront_common_process(int cmd
,
561 struct pcifront_device
*pdev
,
562 pci_channel_state_t state
)
564 pci_ers_result_t result
;
565 struct pci_driver
*pdrv
;
566 int bus
= pdev
->sh_info
->aer_op
.bus
;
567 int devfn
= pdev
->sh_info
->aer_op
.devfn
;
568 struct pci_dev
*pcidev
;
571 dev_dbg(&pdev
->xdev
->dev
,
572 "pcifront AER process: cmd %x (bus:%x, devfn%x)",
574 result
= PCI_ERS_RESULT_NONE
;
576 pcidev
= pci_get_bus_and_slot(bus
, devfn
);
577 if (!pcidev
|| !pcidev
->driver
) {
578 dev_err(&pdev
->xdev
->dev
, "device or AER driver is NULL\n");
583 pdrv
= pcidev
->driver
;
585 if (get_driver(&pdrv
->driver
)) {
586 if (pdrv
->err_handler
&& pdrv
->err_handler
->error_detected
) {
587 dev_dbg(&pcidev
->dev
,
588 "trying to call AER service\n");
592 case XEN_PCI_OP_aer_detected
:
593 result
= pdrv
->err_handler
->
594 error_detected(pcidev
, state
);
596 case XEN_PCI_OP_aer_mmio
:
597 result
= pdrv
->err_handler
->
598 mmio_enabled(pcidev
);
600 case XEN_PCI_OP_aer_slotreset
:
601 result
= pdrv
->err_handler
->
604 case XEN_PCI_OP_aer_resume
:
605 pdrv
->err_handler
->resume(pcidev
);
608 dev_err(&pdev
->xdev
->dev
,
609 "bad request in aer recovery "
615 put_driver(&pdrv
->driver
);
618 result
= PCI_ERS_RESULT_NONE
;
624 static void pcifront_do_aer(struct work_struct
*data
)
626 struct pcifront_device
*pdev
=
627 container_of(data
, struct pcifront_device
, op_work
);
628 int cmd
= pdev
->sh_info
->aer_op
.cmd
;
629 pci_channel_state_t state
=
630 (pci_channel_state_t
)pdev
->sh_info
->aer_op
.err
;
632 /*If a pci_conf op is in progress,
633 we have to wait until it is done before service aer op*/
634 dev_dbg(&pdev
->xdev
->dev
,
635 "pcifront service aer bus %x devfn %x\n",
636 pdev
->sh_info
->aer_op
.bus
, pdev
->sh_info
->aer_op
.devfn
);
638 pdev
->sh_info
->aer_op
.err
= pcifront_common_process(cmd
, pdev
, state
);
640 /* Post the operation to the guest. */
642 clear_bit(_XEN_PCIB_active
, (unsigned long *)&pdev
->sh_info
->flags
);
643 notify_remote_via_evtchn(pdev
->evtchn
);
645 /*in case of we lost an aer request in four lines time_window*/
646 smp_mb__before_clear_bit();
647 clear_bit(_PDEVB_op_active
, &pdev
->flags
);
648 smp_mb__after_clear_bit();
650 schedule_pcifront_aer_op(pdev
);
654 static irqreturn_t
pcifront_handler_aer(int irq
, void *dev
)
656 struct pcifront_device
*pdev
= dev
;
657 schedule_pcifront_aer_op(pdev
);
660 static int pcifront_connect(struct pcifront_device
*pdev
)
664 spin_lock(&pcifront_dev_lock
);
667 dev_info(&pdev
->xdev
->dev
, "Installing PCI frontend\n");
670 dev_err(&pdev
->xdev
->dev
, "PCI frontend already installed!\n");
674 spin_unlock(&pcifront_dev_lock
);
679 static void pcifront_disconnect(struct pcifront_device
*pdev
)
681 spin_lock(&pcifront_dev_lock
);
683 if (pdev
== pcifront_dev
) {
684 dev_info(&pdev
->xdev
->dev
,
685 "Disconnecting PCI Frontend Buses\n");
689 spin_unlock(&pcifront_dev_lock
);
691 static struct pcifront_device
*alloc_pdev(struct xenbus_device
*xdev
)
693 struct pcifront_device
*pdev
;
695 pdev
= kzalloc(sizeof(struct pcifront_device
), GFP_KERNEL
);
700 (struct xen_pci_sharedinfo
*)__get_free_page(GFP_KERNEL
);
701 if (pdev
->sh_info
== NULL
) {
706 pdev
->sh_info
->flags
= 0;
708 /*Flag for registering PV AER handler*/
709 set_bit(_XEN_PCIB_AERHANDLER
, (void *)&pdev
->sh_info
->flags
);
711 dev_set_drvdata(&xdev
->dev
, pdev
);
714 INIT_LIST_HEAD(&pdev
->root_buses
);
716 spin_lock_init(&pdev
->sh_info_lock
);
718 pdev
->evtchn
= INVALID_EVTCHN
;
719 pdev
->gnt_ref
= INVALID_GRANT_REF
;
722 INIT_WORK(&pdev
->op_work
, pcifront_do_aer
);
724 dev_dbg(&xdev
->dev
, "Allocated pdev @ 0x%p pdev->sh_info @ 0x%p\n",
725 pdev
, pdev
->sh_info
);
730 static void free_pdev(struct pcifront_device
*pdev
)
732 dev_dbg(&pdev
->xdev
->dev
, "freeing pdev @ 0x%p\n", pdev
);
734 pcifront_free_roots(pdev
);
736 /*For PCIE_AER error handling job*/
737 flush_scheduled_work();
740 unbind_from_irqhandler(pdev
->irq
, pdev
);
742 if (pdev
->evtchn
!= INVALID_EVTCHN
)
743 xenbus_free_evtchn(pdev
->xdev
, pdev
->evtchn
);
745 if (pdev
->gnt_ref
!= INVALID_GRANT_REF
)
746 gnttab_end_foreign_access(pdev
->gnt_ref
, 0 /* r/w page */,
747 (unsigned long)pdev
->sh_info
);
749 free_page((unsigned long)pdev
->sh_info
);
751 dev_set_drvdata(&pdev
->xdev
->dev
, NULL
);
756 static int pcifront_publish_info(struct pcifront_device
*pdev
)
759 struct xenbus_transaction trans
;
761 err
= xenbus_grant_ring(pdev
->xdev
, virt_to_mfn(pdev
->sh_info
));
767 err
= xenbus_alloc_evtchn(pdev
->xdev
, &pdev
->evtchn
);
771 err
= bind_evtchn_to_irqhandler(pdev
->evtchn
, pcifront_handler_aer
,
772 0, "pcifront", pdev
);
780 err
= xenbus_transaction_start(&trans
);
782 xenbus_dev_fatal(pdev
->xdev
, err
,
783 "Error writing configuration for backend "
784 "(start transaction)");
788 err
= xenbus_printf(trans
, pdev
->xdev
->nodename
,
789 "pci-op-ref", "%u", pdev
->gnt_ref
);
791 err
= xenbus_printf(trans
, pdev
->xdev
->nodename
,
792 "event-channel", "%u", pdev
->evtchn
);
794 err
= xenbus_printf(trans
, pdev
->xdev
->nodename
,
795 "magic", XEN_PCI_MAGIC
);
798 xenbus_transaction_end(trans
, 1);
799 xenbus_dev_fatal(pdev
->xdev
, err
,
800 "Error writing configuration for backend");
803 err
= xenbus_transaction_end(trans
, 0);
807 xenbus_dev_fatal(pdev
->xdev
, err
,
808 "Error completing transaction "
814 xenbus_switch_state(pdev
->xdev
, XenbusStateInitialised
);
816 dev_dbg(&pdev
->xdev
->dev
, "publishing successful!\n");
822 static int __devinit
pcifront_try_connect(struct pcifront_device
*pdev
)
825 int i
, num_roots
, len
;
827 unsigned int domain
, bus
;
830 /* Only connect once */
831 if (xenbus_read_driver_state(pdev
->xdev
->nodename
) !=
832 XenbusStateInitialised
)
835 err
= pcifront_connect(pdev
);
837 xenbus_dev_fatal(pdev
->xdev
, err
,
838 "Error connecting PCI Frontend");
842 err
= xenbus_scanf(XBT_NIL
, pdev
->xdev
->otherend
,
843 "root_num", "%d", &num_roots
);
844 if (err
== -ENOENT
) {
845 xenbus_dev_error(pdev
->xdev
, err
,
846 "No PCI Roots found, trying 0000:00");
847 err
= pcifront_scan_root(pdev
, 0, 0);
849 } else if (err
!= 1) {
852 xenbus_dev_fatal(pdev
->xdev
, err
,
853 "Error reading number of PCI roots");
857 for (i
= 0; i
< num_roots
; i
++) {
858 len
= snprintf(str
, sizeof(str
), "root-%d", i
);
859 if (unlikely(len
>= (sizeof(str
) - 1))) {
864 err
= xenbus_scanf(XBT_NIL
, pdev
->xdev
->otherend
, str
,
865 "%x:%x", &domain
, &bus
);
869 xenbus_dev_fatal(pdev
->xdev
, err
,
870 "Error reading PCI root %d", i
);
874 err
= pcifront_scan_root(pdev
, domain
, bus
);
876 xenbus_dev_fatal(pdev
->xdev
, err
,
877 "Error scanning PCI root %04x:%02x",
883 err
= xenbus_switch_state(pdev
->xdev
, XenbusStateConnected
);
889 static int pcifront_try_disconnect(struct pcifront_device
*pdev
)
892 enum xenbus_state prev_state
;
895 prev_state
= xenbus_read_driver_state(pdev
->xdev
->nodename
);
897 if (prev_state
>= XenbusStateClosing
)
900 if (prev_state
== XenbusStateConnected
) {
901 pcifront_free_roots(pdev
);
902 pcifront_disconnect(pdev
);
905 err
= xenbus_switch_state(pdev
->xdev
, XenbusStateClosed
);
912 static int __devinit
pcifront_attach_devices(struct pcifront_device
*pdev
)
915 int i
, num_roots
, len
;
916 unsigned int domain
, bus
;
919 if (xenbus_read_driver_state(pdev
->xdev
->nodename
) !=
920 XenbusStateReconfiguring
)
923 err
= xenbus_scanf(XBT_NIL
, pdev
->xdev
->otherend
,
924 "root_num", "%d", &num_roots
);
925 if (err
== -ENOENT
) {
926 xenbus_dev_error(pdev
->xdev
, err
,
927 "No PCI Roots found, trying 0000:00");
928 err
= pcifront_rescan_root(pdev
, 0, 0);
930 } else if (err
!= 1) {
933 xenbus_dev_fatal(pdev
->xdev
, err
,
934 "Error reading number of PCI roots");
938 for (i
= 0; i
< num_roots
; i
++) {
939 len
= snprintf(str
, sizeof(str
), "root-%d", i
);
940 if (unlikely(len
>= (sizeof(str
) - 1))) {
945 err
= xenbus_scanf(XBT_NIL
, pdev
->xdev
->otherend
, str
,
946 "%x:%x", &domain
, &bus
);
950 xenbus_dev_fatal(pdev
->xdev
, err
,
951 "Error reading PCI root %d", i
);
955 err
= pcifront_rescan_root(pdev
, domain
, bus
);
957 xenbus_dev_fatal(pdev
->xdev
, err
,
958 "Error scanning PCI root %04x:%02x",
964 xenbus_switch_state(pdev
->xdev
, XenbusStateConnected
);
970 static int pcifront_detach_devices(struct pcifront_device
*pdev
)
974 unsigned int domain
, bus
, slot
, func
;
975 struct pci_bus
*pci_bus
;
976 struct pci_dev
*pci_dev
;
979 if (xenbus_read_driver_state(pdev
->xdev
->nodename
) !=
980 XenbusStateConnected
)
983 err
= xenbus_scanf(XBT_NIL
, pdev
->xdev
->otherend
, "num_devs", "%d",
988 xenbus_dev_fatal(pdev
->xdev
, err
,
989 "Error reading number of PCI devices");
993 /* Find devices being detached and remove them. */
994 for (i
= 0; i
< num_devs
; i
++) {
996 l
= snprintf(str
, sizeof(str
), "state-%d", i
);
997 if (unlikely(l
>= (sizeof(str
) - 1))) {
1001 err
= xenbus_scanf(XBT_NIL
, pdev
->xdev
->otherend
, str
, "%d",
1004 state
= XenbusStateUnknown
;
1006 if (state
!= XenbusStateClosing
)
1009 /* Remove device. */
1010 l
= snprintf(str
, sizeof(str
), "vdev-%d", i
);
1011 if (unlikely(l
>= (sizeof(str
) - 1))) {
1015 err
= xenbus_scanf(XBT_NIL
, pdev
->xdev
->otherend
, str
,
1016 "%x:%x:%x.%x", &domain
, &bus
, &slot
, &func
);
1020 xenbus_dev_fatal(pdev
->xdev
, err
,
1021 "Error reading PCI device %d", i
);
1025 pci_bus
= pci_find_bus(domain
, bus
);
1027 dev_dbg(&pdev
->xdev
->dev
, "Cannot get bus %04x:%02x\n",
1031 pci_dev
= pci_get_slot(pci_bus
, PCI_DEVFN(slot
, func
));
1033 dev_dbg(&pdev
->xdev
->dev
,
1034 "Cannot get PCI device %04x:%02x:%02x.%02x\n",
1035 domain
, bus
, slot
, func
);
1038 pci_remove_bus_device(pci_dev
);
1039 pci_dev_put(pci_dev
);
1041 dev_dbg(&pdev
->xdev
->dev
,
1042 "PCI device %04x:%02x:%02x.%02x removed.\n",
1043 domain
, bus
, slot
, func
);
1046 err
= xenbus_switch_state(pdev
->xdev
, XenbusStateReconfiguring
);
1052 static void __init_refok
pcifront_backend_changed(struct xenbus_device
*xdev
,
1053 enum xenbus_state be_state
)
1055 struct pcifront_device
*pdev
= dev_get_drvdata(&xdev
->dev
);
1058 case XenbusStateUnknown
:
1059 case XenbusStateInitialising
:
1060 case XenbusStateInitWait
:
1061 case XenbusStateInitialised
:
1062 case XenbusStateClosed
:
1065 case XenbusStateConnected
:
1066 pcifront_try_connect(pdev
);
1069 case XenbusStateClosing
:
1070 dev_warn(&xdev
->dev
, "backend going away!\n");
1071 pcifront_try_disconnect(pdev
);
1074 case XenbusStateReconfiguring
:
1075 pcifront_detach_devices(pdev
);
1078 case XenbusStateReconfigured
:
1079 pcifront_attach_devices(pdev
);
1084 static int pcifront_xenbus_probe(struct xenbus_device
*xdev
,
1085 const struct xenbus_device_id
*id
)
1088 struct pcifront_device
*pdev
= alloc_pdev(xdev
);
1092 xenbus_dev_fatal(xdev
, err
,
1093 "Error allocating pcifront_device struct");
1097 err
= pcifront_publish_info(pdev
);
1105 static int pcifront_xenbus_remove(struct xenbus_device
*xdev
)
1107 struct pcifront_device
*pdev
= dev_get_drvdata(&xdev
->dev
);
1114 static const struct xenbus_device_id xenpci_ids
[] = {
1119 static struct xenbus_driver xenbus_pcifront_driver
= {
1121 .owner
= THIS_MODULE
,
1123 .probe
= pcifront_xenbus_probe
,
1124 .remove
= pcifront_xenbus_remove
,
1125 .otherend_changed
= pcifront_backend_changed
,
1128 static int __init
pcifront_init(void)
1130 if (!xen_pv_domain() || xen_initial_domain())
1133 pci_frontend_registrar(1 /* enable */);
1135 return xenbus_register_frontend(&xenbus_pcifront_driver
);
1138 static void __exit
pcifront_cleanup(void)
1140 xenbus_unregister_driver(&xenbus_pcifront_driver
);
1141 pci_frontend_registrar(0 /* disable */);
1143 module_init(pcifront_init
);
1144 module_exit(pcifront_cleanup
);
1146 MODULE_DESCRIPTION("Xen PCI passthrough frontend.");
1147 MODULE_LICENSE("GPL");
1148 MODULE_ALIAS("xen:pci");